blob: 39c82b11f219d722d4f2ba96ad51e4368813ac72 [file] [log] [blame]
Duncan Laurie64bc26a2020-10-10 00:15:28 +00001/* SPDX-License-Identifier: GPL-2.0-or-later */
2
3#include <acpi/acpigen.h>
4#include <acpi/acpi_device.h>
5#include <console/console.h>
6#include <device/device.h>
7#include <device/pci_ids.h>
8#include <device/pci_ops.h>
9#include <device/pci.h>
10#include <intelblocks/pmc.h>
11#include <intelblocks/pmc_ipc.h>
Tim Wawrzynczak32f883e2021-12-02 16:19:47 -070012#include <intelblocks/pcie_rp.h>
13#include <soc/iomap.h>
Duncan Laurie64bc26a2020-10-10 00:15:28 +000014#include "chip.h"
15
Duncan Laurie64bc26a2020-10-10 00:15:28 +000016/* PCIe Root Port registers for link status and L23 control. */
17#define PCH_PCIE_CFG_LSTS 0x52 /* Link Status Register */
18#define PCH_PCIE_CFG_SPR 0xe0 /* Scratchpad */
19#define PCH_PCIE_CFG_RPPGEN 0xe2 /* Root Port Power Gating Enable */
20#define PCH_PCIE_CFG_LCAP_PN 0x4f /* Root Port Number */
21
22/* ACPI register names corresponding to PCIe root port registers. */
23#define ACPI_REG_PCI_LINK_ACTIVE "LASX" /* Link active status */
24#define ACPI_REG_PCI_L23_RDY_ENTRY "L23E" /* L23_Rdy Entry Request */
25#define ACPI_REG_PCI_L23_RDY_DETECT "L23R" /* L23_Rdy Detect Transition */
26#define ACPI_REG_PCI_L23_SAVE_STATE "NCB7" /* Scratch bit to save L23 state */
27
Tim Wawrzynczak32f883e2021-12-02 16:19:47 -070028/* ACPI path to the mutex that protects accesses to PMC ModPhy power gating registers */
29#define RTD3_MUTEX_PATH "\\_SB.PCI0.R3MX"
30
Kane Chenfa77ac92023-07-06 16:05:42 +080031/* ACPI path to control PCIE CLK by P2SB */
32#define RTD3_PCIE_CLK_ENABLE_PATH "\\_SB.PCI0.SPCO"
33
Tim Wawrzynczak32f883e2021-12-02 16:19:47 -070034enum modphy_pg_state {
35 PG_DISABLE = 0,
36 PG_ENABLE = 1,
37};
38
Duncan Laurie64bc26a2020-10-10 00:15:28 +000039/* Called from _ON to get PCIe link back to active state. */
40static void pcie_rtd3_acpi_l23_exit(void)
41{
42 /* Skip if port is not in L2/L3. */
43 acpigen_write_if_lequal_namestr_int(ACPI_REG_PCI_L23_SAVE_STATE, 1);
44
45 /* Initiate L2/L3 Ready To Detect transition. */
46 acpigen_write_store_int_to_namestr(1, ACPI_REG_PCI_L23_RDY_DETECT);
47
48 /* Wait for transition to detect. */
49 acpigen_write_delay_until_namestr_int(320, ACPI_REG_PCI_L23_RDY_DETECT, 0);
50
51 acpigen_write_store_int_to_namestr(0, ACPI_REG_PCI_L23_SAVE_STATE);
52
53 /* Once in detect, wait for link active. */
54 acpigen_write_delay_until_namestr_int(128, ACPI_REG_PCI_LINK_ACTIVE, 1);
55
56 acpigen_pop_len(); /* If */
57}
58
59/* Called from _OFF to put PCIe link into L2/L3 state. */
60static void pcie_rtd3_acpi_l23_entry(void)
61{
62 /* Initiate L2/L3 Entry request. */
63 acpigen_write_store_int_to_namestr(1, ACPI_REG_PCI_L23_RDY_ENTRY);
64
65 /* Wait for L2/L3 Entry request to clear. */
66 acpigen_write_delay_until_namestr_int(128, ACPI_REG_PCI_L23_RDY_ENTRY, 0);
67
68 acpigen_write_store_int_to_namestr(1, ACPI_REG_PCI_L23_SAVE_STATE);
69}
70
Tim Wawrzynczak32f883e2021-12-02 16:19:47 -070071/* Called from _ON/_OFF to disable/enable ModPHY power gating */
72static void pcie_rtd3_enable_modphy_pg(unsigned int pcie_rp, enum modphy_pg_state state)
73{
74 /* Enter the critical section */
75 acpigen_emit_ext_op(ACQUIRE_OP);
76 acpigen_emit_namestring(RTD3_MUTEX_PATH);
77 acpigen_emit_word(ACPI_MUTEX_NO_TIMEOUT);
78
79 acpigen_write_store_int_to_namestr(state, "EMPG");
80 acpigen_write_delay_until_namestr_int(100, "AMPG", state);
81
82 /* Exit the critical section */
83 acpigen_emit_ext_op(RELEASE_OP);
84 acpigen_emit_namestring(RTD3_MUTEX_PATH);
85}
86
Cliff Huang4bc9ac72022-01-21 00:23:15 -080087/* Method to enter L2/L3 */
88static void pcie_rtd3_acpi_method_dl23(void)
89{
90 acpigen_write_method_serialized("DL23", 0);
91 pcie_rtd3_acpi_l23_entry();
92 acpigen_pop_len(); /* Method */
93}
94
95/* Method to exit L2/L3 */
96static void pcie_rtd3_acpi_method_l23d(void)
97{
98 acpigen_write_method_serialized("L23D", 0);
99 pcie_rtd3_acpi_l23_exit();
100 acpigen_pop_len(); /* Method */
101}
102
103/* Method to disable PCH modPHY power gating */
104static void pcie_rtd3_acpi_method_pds0(unsigned int pcie_rp)
105{
106 acpigen_write_method_serialized("PSD0", 0);
107 pcie_rtd3_enable_modphy_pg(pcie_rp, PG_DISABLE);
108 acpigen_pop_len(); /* Method */
109}
110
111/* Method to enable/disable the source clock */
112static void pcie_rtd3_acpi_method_srck(unsigned int pcie_rp,
113 const struct soc_intel_common_block_pcie_rtd3_config *config)
114{
115 acpigen_write_method_serialized("SRCK", 1);
116
117 if (config->srcclk_pin >= 0) {
118 acpigen_write_if_lequal_op_op(ARG0_OP, 0);
119 pmc_ipc_acpi_set_pci_clock(pcie_rp, config->srcclk_pin, false);
120 acpigen_write_else();
121 pmc_ipc_acpi_set_pci_clock(pcie_rp, config->srcclk_pin, true);
122 acpigen_pop_len(); /* If */
123 }
124 acpigen_pop_len(); /* Method */
125}
126
Kane Chenfa77ac92023-07-06 16:05:42 +0800127/* Method to enable/disable pcie clock by p2sb*/
128static void p2sb_acpi_set_pci_clock(u8 srcclk_pin, bool enable)
129{
130 acpigen_write_if_cond_ref_of(RTD3_PCIE_CLK_ENABLE_PATH);
131 acpigen_emit_namestring(RTD3_PCIE_CLK_ENABLE_PATH);
132 acpigen_write_integer(srcclk_pin);
133 acpigen_write_integer(enable);
134 acpigen_write_if_end();
135}
136
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000137static void
138pcie_rtd3_acpi_method_on(unsigned int pcie_rp,
Tim Wawrzynczak32f883e2021-12-02 16:19:47 -0700139 const struct soc_intel_common_block_pcie_rtd3_config *config,
Kane Chen11be5562022-11-03 23:18:44 +0800140 enum pcie_rp_type rp_type,
141 const struct device *dev)
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000142{
Kane Chen11be5562022-11-03 23:18:44 +0800143 const struct device *parent = dev->bus->dev;
144
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000145 acpigen_write_method_serialized("_ON", 0);
146
Cliff Huang05399622023-02-04 21:20:08 -0800147 /* When this feature is enabled, ONSK indicates if the previous _OFF was
148 * skipped. If so, since the device was not in Off state, and the current
149 * _ON can be skipped as well.
150 */
151 if (config->skip_on_off_support)
152 acpigen_write_if_lequal_namestr_int("ONSK", 0);
153
Kane Chen11be5562022-11-03 23:18:44 +0800154 /* The _STA returns current power status of device, so we can skip _ON
155 * if _STA returns 1
156 * Example:
157 * Local0 = \_SB.PCI0.RP01.RTD3._STA ()
158 * If ((Local0 == One))
159 * {
160 * Return (One)
161 * }
162 */
163 acpigen_write_store();
164 acpigen_emit_namestring(acpi_device_path_join(parent, "RTD3._STA"));
165 acpigen_emit_byte(LOCAL0_OP);
166 acpigen_write_if_lequal_op_int(LOCAL0_OP, ONE_OP);
167 acpigen_write_return_op(ONE_OP);
168 acpigen_write_if_end();
169
Cliff Huang69564f32023-03-02 10:03:32 -0800170 if (config->use_rp_mutex)
171 acpigen_write_acquire(acpi_device_path_join(parent, RP_MUTEX_NAME),
172 ACPI_MUTEX_NO_TIMEOUT);
173
Tim Wawrzynczak32f883e2021-12-02 16:19:47 -0700174 /* Disable modPHY power gating for PCH RPs. */
175 if (rp_type == PCIE_RP_PCH)
176 pcie_rtd3_enable_modphy_pg(pcie_rp, PG_DISABLE);
177
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000178 /* Assert enable GPIO to turn on device power. */
179 if (config->enable_gpio.pin_count) {
180 acpigen_enable_tx_gpio(&config->enable_gpio);
181 if (config->enable_delay_ms)
182 acpigen_write_sleep(config->enable_delay_ms);
183 }
184
Kane Chenfa77ac92023-07-06 16:05:42 +0800185 /* Enable SRCCLK for this root port if pin is defined. */
186 if (config->srcclk_pin >= 0) {
187 if (CONFIG(PCIE_CLOCK_CONTROL_THROUGH_P2SB))
188 p2sb_acpi_set_pci_clock(config->srcclk_pin, true);
189 else
190 pmc_ipc_acpi_set_pci_clock(pcie_rp, config->srcclk_pin, true);
191 }
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000192
193 /* De-assert reset GPIO to bring device out of reset. */
194 if (config->reset_gpio.pin_count) {
195 acpigen_disable_tx_gpio(&config->reset_gpio);
196 if (config->reset_delay_ms)
197 acpigen_write_sleep(config->reset_delay_ms);
198 }
199
Tim Wawrzynczak32f883e2021-12-02 16:19:47 -0700200 /* Trigger L23 ready exit flow unless disabled by config. */
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000201 if (!config->disable_l23)
202 pcie_rtd3_acpi_l23_exit();
203
Cliff Huang69564f32023-03-02 10:03:32 -0800204 if (config->use_rp_mutex)
205 acpigen_write_release(acpi_device_path_join(parent, RP_MUTEX_NAME));
206
Cliff Huangd1a74162022-01-21 14:54:32 -0800207 if (config->skip_on_off_support) {
208 /* If current _ON is skipped, ONSK is decremented so that _ON will be
209 * executed normally until _OFF is skipped again.
210 */
211 acpigen_write_else();
212 acpigen_emit_byte(DECREMENT_OP);
213 acpigen_emit_namestring("ONSK");
214
215 acpigen_pop_len(); /* Else */
216 }
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000217 acpigen_pop_len(); /* Method */
218}
219
220static void
221pcie_rtd3_acpi_method_off(int pcie_rp,
Tim Wawrzynczak32f883e2021-12-02 16:19:47 -0700222 const struct soc_intel_common_block_pcie_rtd3_config *config,
Cliff Huang69564f32023-03-02 10:03:32 -0800223 enum pcie_rp_type rp_type,
224 const struct device *dev)
225
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000226{
Cliff Huang69564f32023-03-02 10:03:32 -0800227 const struct device *parent = dev->bus->dev;
228
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000229 acpigen_write_method_serialized("_OFF", 0);
230
Cliff Huangd1a74162022-01-21 14:54:32 -0800231 /* When this feature is enabled, ONSK is checked to see if the device
232 * wants _OFF to be skipped for once. ONSK is normally incremented in the
233 * device method, such as reset _RST, which is invoked during driver reload.
234 * In such case, _OFF needs to be avoided at the end of driver removal.
235 */
236 if (config->skip_on_off_support)
237 acpigen_write_if_lequal_namestr_int("OFSK", 0);
238
Cliff Huang69564f32023-03-02 10:03:32 -0800239 if (config->use_rp_mutex)
240 acpigen_write_acquire(acpi_device_path_join(parent, RP_MUTEX_NAME),
241 ACPI_MUTEX_NO_TIMEOUT);
242
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000243 /* Trigger L23 ready entry flow unless disabled by config. */
244 if (!config->disable_l23)
245 pcie_rtd3_acpi_l23_entry();
246
247 /* Assert reset GPIO to place device into reset. */
248 if (config->reset_gpio.pin_count) {
249 acpigen_enable_tx_gpio(&config->reset_gpio);
250 if (config->reset_off_delay_ms)
251 acpigen_write_sleep(config->reset_off_delay_ms);
252 }
253
Tim Wawrzynczak32f883e2021-12-02 16:19:47 -0700254 /* Enable modPHY power gating for PCH RPs */
255 if (rp_type == PCIE_RP_PCH)
256 pcie_rtd3_enable_modphy_pg(pcie_rp, PG_ENABLE);
257
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000258 /* Disable SRCCLK for this root port if pin is defined. */
Kane Chenfa77ac92023-07-06 16:05:42 +0800259 if (config->srcclk_pin >= 0) {
260 if (CONFIG(PCIE_CLOCK_CONTROL_THROUGH_P2SB))
261 p2sb_acpi_set_pci_clock(config->srcclk_pin, false);
262 else
263 pmc_ipc_acpi_set_pci_clock(pcie_rp, config->srcclk_pin, false);
264 }
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000265
266 /* De-assert enable GPIO to turn off device power. */
267 if (config->enable_gpio.pin_count) {
268 acpigen_disable_tx_gpio(&config->enable_gpio);
269 if (config->enable_off_delay_ms)
270 acpigen_write_sleep(config->enable_off_delay_ms);
271 }
272
Cliff Huang69564f32023-03-02 10:03:32 -0800273 if (config->use_rp_mutex)
274 acpigen_write_release(acpi_device_path_join(parent, RP_MUTEX_NAME));
275
Cliff Huangd1a74162022-01-21 14:54:32 -0800276 if (config->skip_on_off_support) {
277 /* If current _OFF is skipped, ONSK is incremented so that the
278 * following _ON will also be skipped. In addition, OFSK is decremented
279 * so that next _OFF will be executed normally until the device method
280 * increments OFSK again.
281 */
282 acpigen_write_else();
283 /* OFSK-- */
284 acpigen_emit_byte(DECREMENT_OP);
285 acpigen_emit_namestring("OFSK");
286 /* ONSK++ */
287 acpigen_emit_byte(INCREMENT_OP);
288 acpigen_emit_namestring("ONSK");
289
290 acpigen_pop_len(); /* Else */
291 }
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000292 acpigen_pop_len(); /* Method */
293}
294
295static void
Tim Wawrzynczakb6a15a72021-12-08 10:43:56 -0700296pcie_rtd3_acpi_method_status(const struct soc_intel_common_block_pcie_rtd3_config *config)
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000297{
298 const struct acpi_gpio *gpio;
299
300 acpigen_write_method("_STA", 0);
Cliff Huang9a5a9632023-01-24 17:05:17 -0800301 /*
302 * Depending on the board configuration we use either the "enable" or
303 * the "reset" pin to detect the status of the device. The logic for
304 * each pin is detailed below.
305 *
306 * 1. For the "enable" pin:
307 * | polarity | tx value | get_tx_gpio() | State |
308 * |-------------+----------+---------------+-------|
309 * | active high | 0 | 0 | 0 |
310 * | active high | 1 | 1(active) | 1 |
311 * | active low | 0 | 1(active) | 1 |
312 * | active low | 1 | 0 | 0 |
313 *
314 * 2. For the "reset" pin:
315 * | polarity | tx value | get_tx_gpio() | State |
316 * |-------------+----------+---------------+-------|
317 * | active high | 0 | 0 | 1 |
318 * | active high | 1 | 1(active) | 0 |
319 * | active low | 0 | 1(active) | 0 |
320 * | active low | 1 | 0 | 1 |
321 */
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000322
323 /* Use enable GPIO for status if provided, otherwise use reset GPIO. */
Cliff Huang9a5a9632023-01-24 17:05:17 -0800324 if (config->enable_gpio.pin_count) {
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000325 gpio = &config->enable_gpio;
Cliff Huang9a5a9632023-01-24 17:05:17 -0800326 /* Read current GPIO state into Local0. */
327 acpigen_get_tx_gpio(gpio);
328 } else {
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000329 gpio = &config->reset_gpio;
Cliff Huang9a5a9632023-01-24 17:05:17 -0800330 /* Read current GPIO state into Local0. */
331 acpigen_get_tx_gpio(gpio);
332 acpigen_write_not(LOCAL0_OP, LOCAL0_OP);
333 }
334 acpigen_write_return_op(LOCAL0_OP);
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000335 acpigen_pop_len(); /* Method */
336}
337
Tim Wawrzynczak32f883e2021-12-02 16:19:47 -0700338static void write_modphy_opregion(unsigned int pcie_rp)
339{
340 /* The register containing the Power Gate enable sequence bits is at
341 PCH_PWRM_BASE + 0x10D0, and the bits to check for sequence completion are at
342 PCH_PWRM_BASE + 0x10D4. */
343 const struct opregion opregion = OPREGION("PMCP", SYSTEMMEMORY,
344 PCH_PWRM_BASE_ADDRESS + 0x1000, 0xff);
345 const struct fieldlist fieldlist[] = {
346 FIELDLIST_OFFSET(0xD0),
347 FIELDLIST_RESERVED(pcie_rp),
348 FIELDLIST_NAMESTR("EMPG", 1), /* Enable ModPHY Power Gate */
349 FIELDLIST_OFFSET(0xD4),
350 FIELDLIST_RESERVED(pcie_rp),
351 FIELDLIST_NAMESTR("AMPG", 1), /* Is ModPHY Power Gate active? */
352 };
353
354 acpigen_write_opregion(&opregion);
355 acpigen_write_field("PMCP", fieldlist, ARRAY_SIZE(fieldlist),
356 FIELD_DWORDACC | FIELD_NOLOCK | FIELD_PRESERVE);
357}
358
Tim Wawrzynczakb6a15a72021-12-08 10:43:56 -0700359static int get_pcie_rp_pmc_idx(enum pcie_rp_type rp_type, const struct device *dev)
360{
361 int idx = -1;
362
363 switch (rp_type) {
364 case PCIE_RP_PCH:
365 /* Read port number of root port that this device is attached to. */
366 idx = pci_read_config8(dev, PCH_PCIE_CFG_LCAP_PN);
367
368 /* Port number is 1-based, PMC IPC method expects 0-based. */
369 idx--;
370 break;
371 case PCIE_RP_CPU:
372 /* CPU RPs are indexed by their "virtual wire index" to the PCH */
373 idx = soc_get_cpu_rp_vw_idx(dev);
374 break;
375 default:
376 break;
377 }
378
379 return idx;
380}
381
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000382static void pcie_rtd3_acpi_fill_ssdt(const struct device *dev)
383{
Tim Wawrzynczak32f883e2021-12-02 16:19:47 -0700384 static bool mutex_created = false;
385
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000386 const struct soc_intel_common_block_pcie_rtd3_config *config = config_of(dev);
387 static const char *const power_res_states[] = {"_PR0"};
388 const struct device *parent = dev->bus->dev;
389 const char *scope = acpi_device_path(parent);
390 const struct opregion opregion = OPREGION("PXCS", PCI_CONFIG, 0, 0xff);
391 const struct fieldlist fieldlist[] = {
392 FIELDLIST_OFFSET(PCH_PCIE_CFG_LSTS),
393 FIELDLIST_RESERVED(13),
394 FIELDLIST_NAMESTR(ACPI_REG_PCI_LINK_ACTIVE, 1),
395 FIELDLIST_OFFSET(PCH_PCIE_CFG_SPR),
396 FIELDLIST_RESERVED(7),
397 FIELDLIST_NAMESTR(ACPI_REG_PCI_L23_SAVE_STATE, 1),
398 FIELDLIST_OFFSET(PCH_PCIE_CFG_RPPGEN),
399 FIELDLIST_RESERVED(2),
400 FIELDLIST_NAMESTR(ACPI_REG_PCI_L23_RDY_ENTRY, 1),
401 FIELDLIST_NAMESTR(ACPI_REG_PCI_L23_RDY_DETECT, 1),
402 };
Tim Wawrzynczakb6a15a72021-12-08 10:43:56 -0700403 int pcie_rp;
Kapil Porwal65bcb572022-11-28 18:53:40 +0530404 struct acpi_dp *dsd;
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000405
406 if (!is_dev_enabled(parent)) {
407 printk(BIOS_ERR, "%s: root port not enabled\n", __func__);
408 return;
409 }
410 if (!scope) {
411 printk(BIOS_ERR, "%s: root port scope not found\n", __func__);
412 return;
413 }
414 if (!config->enable_gpio.pin_count && !config->reset_gpio.pin_count) {
415 printk(BIOS_ERR, "%s: Enable and/or Reset GPIO required for %s.\n",
416 __func__, scope);
417 return;
418 }
Rizwan Qureshia9794602021-04-08 20:31:47 +0530419 if (config->srcclk_pin > CONFIG_MAX_PCIE_CLOCK_SRC) {
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000420 printk(BIOS_ERR, "%s: Invalid clock pin %u for %s.\n", __func__,
421 config->srcclk_pin, scope);
422 return;
423 }
424
Tim Wawrzynczak32f883e2021-12-02 16:19:47 -0700425 const enum pcie_rp_type rp_type = soc_get_pcie_rp_type(parent);
Tim Wawrzynczakb6a15a72021-12-08 10:43:56 -0700426 pcie_rp = get_pcie_rp_pmc_idx(rp_type, parent);
Tim Wawrzynczakb3cd55b2022-01-20 14:06:29 -0700427 if (pcie_rp < 0) {
Tim Wawrzynczakb6a15a72021-12-08 10:43:56 -0700428 printk(BIOS_ERR, "%s: Unknown PCIe root port\n", __func__);
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000429 return;
430 }
Cliff Huang4bc9ac72022-01-21 00:23:15 -0800431 if (config->disable_l23) {
Angel Ponsd85319a2022-02-13 13:35:20 +0100432 if (config->ext_pm_support & ACPI_PCIE_RP_EMIT_L23) {
Cliff Huang4bc9ac72022-01-21 00:23:15 -0800433 printk(BIOS_ERR, "%s: Can not export L23 methods\n", __func__);
434 return;
435 }
436 }
437 if (rp_type != PCIE_RP_PCH) {
Angel Ponsd85319a2022-02-13 13:35:20 +0100438 if (config->ext_pm_support & ACPI_PCIE_RP_EMIT_PSD0) {
Cliff Huang4bc9ac72022-01-21 00:23:15 -0800439 printk(BIOS_ERR, "%s: Can not export PSD0 method\n", __func__);
440 return;
441 }
442 }
Cliff Huang8fbdefc2023-03-21 21:51:30 -0700443 if (config->srcclk_pin == -1) {
Angel Ponsd85319a2022-02-13 13:35:20 +0100444 if (config->ext_pm_support & ACPI_PCIE_RP_EMIT_SRCK) {
Cliff Huang8fbdefc2023-03-21 21:51:30 -0700445 printk(BIOS_ERR, "%s: Can not export SRCK method since clock source gating is not enabled\n",
446 __func__);
Cliff Huang4bc9ac72022-01-21 00:23:15 -0800447 return;
448 }
449 }
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000450
451 printk(BIOS_INFO, "%s: Enable RTD3 for %s (%s)\n", scope, dev_path(parent),
452 config->desc ?: dev->chip_ops->name);
453
Tim Wawrzynczak32f883e2021-12-02 16:19:47 -0700454 /* Create a mutex for exclusive access to the PMC registers. */
455 if (rp_type == PCIE_RP_PCH && !mutex_created) {
456 acpigen_write_scope("\\_SB.PCI0");
457 acpigen_write_mutex("R3MX", 0);
458 acpigen_write_scope_end();
459 mutex_created = true;
460 }
461
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000462 /* The RTD3 power resource is added to the root port, not the device. */
463 acpigen_write_scope(scope);
464
Cliff Huang69564f32023-03-02 10:03:32 -0800465 if (config->use_rp_mutex)
466 acpigen_write_mutex(RP_MUTEX_NAME, 0);
467
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000468 if (config->desc)
469 acpigen_write_name_string("_DDN", config->desc);
470
Tim Wawrzynczak32f883e2021-12-02 16:19:47 -0700471 /* Create OpRegions for MMIO accesses. */
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000472 acpigen_write_opregion(&opregion);
473 acpigen_write_field("PXCS", fieldlist, ARRAY_SIZE(fieldlist),
474 FIELD_ANYACC | FIELD_NOLOCK | FIELD_PRESERVE);
475
Angel Ponsd85319a2022-02-13 13:35:20 +0100476 if (config->ext_pm_support & ACPI_PCIE_RP_EMIT_L23) {
Cliff Huang4bc9ac72022-01-21 00:23:15 -0800477 pcie_rtd3_acpi_method_dl23();
478 pcie_rtd3_acpi_method_l23d();
479 }
480
Tim Wawrzynczak32f883e2021-12-02 16:19:47 -0700481 /* Create the OpRegion to access the ModPHY PG registers (PCH RPs only) */
482 if (rp_type == PCIE_RP_PCH)
483 write_modphy_opregion(pcie_rp);
484
Angel Ponsd85319a2022-02-13 13:35:20 +0100485 if (config->ext_pm_support & ACPI_PCIE_RP_EMIT_PSD0)
Cliff Huang4bc9ac72022-01-21 00:23:15 -0800486 pcie_rtd3_acpi_method_pds0(pcie_rp);
487
Angel Ponsd85319a2022-02-13 13:35:20 +0100488 if (config->ext_pm_support & ACPI_PCIE_RP_EMIT_SRCK)
Cliff Huang4bc9ac72022-01-21 00:23:15 -0800489 pcie_rtd3_acpi_method_srck(pcie_rp, config);
490
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000491 /* ACPI Power Resource for controlling the attached device power. */
492 acpigen_write_power_res("RTD3", 0, 0, power_res_states, ARRAY_SIZE(power_res_states));
Cliff Huangd1a74162022-01-21 14:54:32 -0800493
494 if (config->skip_on_off_support) {
495 /* OFSK: 0 = _OFF Method will be executed normally when called;
496 * >1 = _OFF will be skipped.
497 * _OFF Method to decrement OFSK and increment ONSK if the
498 * current execution is skipped.
499 * ONSK: 0 = _ON Method will be executed normally when called;
500 * >1 = _ONF will be skipped.
501 * _ON Method to decrement ONSK if the current execution is
502 * skipped.
503 */
504 acpigen_write_name_integer("ONSK", 0);
505 acpigen_write_name_integer("OFSK", 0);
506 }
507
Tim Wawrzynczakb6a15a72021-12-08 10:43:56 -0700508 pcie_rtd3_acpi_method_status(config);
Kane Chen11be5562022-11-03 23:18:44 +0800509 pcie_rtd3_acpi_method_on(pcie_rp, config, rp_type, dev);
Cliff Huang69564f32023-03-02 10:03:32 -0800510 pcie_rtd3_acpi_method_off(pcie_rp, config, rp_type, dev);
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000511 acpigen_pop_len(); /* PowerResource */
512
513 /* Indicate to the OS that device supports hotplug in D3. */
514 dsd = acpi_dp_new_table("_DSD");
Kapil Porwal65bcb572022-11-28 18:53:40 +0530515 acpi_device_add_hotplug_support_in_d3(dsd);
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000516
517 /* Indicate to the OS if the device provides an External facing port. */
Kapil Porwal65bcb572022-11-28 18:53:40 +0530518 if (config->add_acpi_external_facing_port)
519 acpi_device_add_external_facing_port(dsd);
Kapil Porwald7eacd72022-11-28 11:03:38 +0530520
521 /* Indicate to the OS if the device has DMA property. */
522 if (config->add_acpi_dma_property)
523 acpi_device_add_dma_property(dsd);
524
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000525 acpi_dp_write(dsd);
526
527 /*
528 * Check the sibling device on the root port to see if it is storage class and add the
529 * property for the OS to enable storage D3, or allow it to be enabled by config.
530 */
531 if (config->is_storage
532 || (dev->sibling && (dev->sibling->class >> 16) == PCI_BASE_CLASS_STORAGE)) {
533 acpigen_write_device(acpi_device_name(dev));
534 acpigen_write_ADR(0);
535 acpigen_write_STA(ACPI_STATUS_DEVICE_ALL_ON);
Sean Rhodes648ff922023-04-13 13:25:14 +0100536 if (CONFIG(D3COLD_SUPPORT))
537 acpigen_write_name_integer("_S0W", ACPI_DEVICE_SLEEP_D3_COLD);
538 else
539 acpigen_write_name_integer("_S0W", ACPI_DEVICE_SLEEP_D3_HOT);
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000540
Kapil Porwal65bcb572022-11-28 18:53:40 +0530541 acpi_device_add_storage_d3_enable(NULL);
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000542
543 acpigen_pop_len(); /* Device */
544
545 printk(BIOS_INFO, "%s: Added StorageD3Enable property\n", scope);
546 }
547
548 acpigen_pop_len(); /* Scope */
549}
550
551static const char *pcie_rtd3_acpi_name(const struct device *dev)
552{
553 /* Attached device name must be "PXSX" for the Linux Kernel to recognize it. */
554 return "PXSX";
555}
556
557static struct device_operations pcie_rtd3_ops = {
558 .read_resources = noop_read_resources,
559 .set_resources = noop_set_resources,
560 .acpi_fill_ssdt = pcie_rtd3_acpi_fill_ssdt,
561 .acpi_name = pcie_rtd3_acpi_name,
562};
563
564static void pcie_rtd3_acpi_enable(struct device *dev)
565{
566 dev->ops = &pcie_rtd3_ops;
567}
568
569struct chip_operations soc_intel_common_block_pcie_rtd3_ops = {
Nicholas Sudsgaardbfb11be2024-01-30 09:53:46 +0900570 .name = "Intel PCIe Runtime D3",
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000571 .enable_dev = pcie_rtd3_acpi_enable
572};