Duncan Laurie | 64bc26a | 2020-10-10 00:15:28 +0000 | [diff] [blame] | 1 | /* 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 Wawrzynczak | 32f883e | 2021-12-02 16:19:47 -0700 | [diff] [blame] | 12 | #include <intelblocks/pcie_rp.h> |
| 13 | #include <soc/iomap.h> |
Duncan Laurie | 64bc26a | 2020-10-10 00:15:28 +0000 | [diff] [blame] | 14 | #include "chip.h" |
| 15 | |
Duncan Laurie | 64bc26a | 2020-10-10 00:15:28 +0000 | [diff] [blame] | 16 | /* 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 Wawrzynczak | 32f883e | 2021-12-02 16:19:47 -0700 | [diff] [blame] | 28 | /* ACPI path to the mutex that protects accesses to PMC ModPhy power gating registers */ |
| 29 | #define RTD3_MUTEX_PATH "\\_SB.PCI0.R3MX" |
| 30 | |
| 31 | enum modphy_pg_state { |
| 32 | PG_DISABLE = 0, |
| 33 | PG_ENABLE = 1, |
| 34 | }; |
| 35 | |
Duncan Laurie | 64bc26a | 2020-10-10 00:15:28 +0000 | [diff] [blame] | 36 | /* Called from _ON to get PCIe link back to active state. */ |
| 37 | static void pcie_rtd3_acpi_l23_exit(void) |
| 38 | { |
| 39 | /* Skip if port is not in L2/L3. */ |
| 40 | acpigen_write_if_lequal_namestr_int(ACPI_REG_PCI_L23_SAVE_STATE, 1); |
| 41 | |
| 42 | /* Initiate L2/L3 Ready To Detect transition. */ |
| 43 | acpigen_write_store_int_to_namestr(1, ACPI_REG_PCI_L23_RDY_DETECT); |
| 44 | |
| 45 | /* Wait for transition to detect. */ |
| 46 | acpigen_write_delay_until_namestr_int(320, ACPI_REG_PCI_L23_RDY_DETECT, 0); |
| 47 | |
| 48 | acpigen_write_store_int_to_namestr(0, ACPI_REG_PCI_L23_SAVE_STATE); |
| 49 | |
| 50 | /* Once in detect, wait for link active. */ |
| 51 | acpigen_write_delay_until_namestr_int(128, ACPI_REG_PCI_LINK_ACTIVE, 1); |
| 52 | |
| 53 | acpigen_pop_len(); /* If */ |
| 54 | } |
| 55 | |
| 56 | /* Called from _OFF to put PCIe link into L2/L3 state. */ |
| 57 | static void pcie_rtd3_acpi_l23_entry(void) |
| 58 | { |
| 59 | /* Initiate L2/L3 Entry request. */ |
| 60 | acpigen_write_store_int_to_namestr(1, ACPI_REG_PCI_L23_RDY_ENTRY); |
| 61 | |
| 62 | /* Wait for L2/L3 Entry request to clear. */ |
| 63 | acpigen_write_delay_until_namestr_int(128, ACPI_REG_PCI_L23_RDY_ENTRY, 0); |
| 64 | |
| 65 | acpigen_write_store_int_to_namestr(1, ACPI_REG_PCI_L23_SAVE_STATE); |
| 66 | } |
| 67 | |
Tim Wawrzynczak | 32f883e | 2021-12-02 16:19:47 -0700 | [diff] [blame] | 68 | /* Called from _ON/_OFF to disable/enable ModPHY power gating */ |
| 69 | static void pcie_rtd3_enable_modphy_pg(unsigned int pcie_rp, enum modphy_pg_state state) |
| 70 | { |
| 71 | /* Enter the critical section */ |
| 72 | acpigen_emit_ext_op(ACQUIRE_OP); |
| 73 | acpigen_emit_namestring(RTD3_MUTEX_PATH); |
| 74 | acpigen_emit_word(ACPI_MUTEX_NO_TIMEOUT); |
| 75 | |
| 76 | acpigen_write_store_int_to_namestr(state, "EMPG"); |
| 77 | acpigen_write_delay_until_namestr_int(100, "AMPG", state); |
| 78 | |
| 79 | /* Exit the critical section */ |
| 80 | acpigen_emit_ext_op(RELEASE_OP); |
| 81 | acpigen_emit_namestring(RTD3_MUTEX_PATH); |
| 82 | } |
| 83 | |
Cliff Huang | 4bc9ac7 | 2022-01-21 00:23:15 -0800 | [diff] [blame] | 84 | /* Method to enter L2/L3 */ |
| 85 | static void pcie_rtd3_acpi_method_dl23(void) |
| 86 | { |
| 87 | acpigen_write_method_serialized("DL23", 0); |
| 88 | pcie_rtd3_acpi_l23_entry(); |
| 89 | acpigen_pop_len(); /* Method */ |
| 90 | } |
| 91 | |
| 92 | /* Method to exit L2/L3 */ |
| 93 | static void pcie_rtd3_acpi_method_l23d(void) |
| 94 | { |
| 95 | acpigen_write_method_serialized("L23D", 0); |
| 96 | pcie_rtd3_acpi_l23_exit(); |
| 97 | acpigen_pop_len(); /* Method */ |
| 98 | } |
| 99 | |
| 100 | /* Method to disable PCH modPHY power gating */ |
| 101 | static void pcie_rtd3_acpi_method_pds0(unsigned int pcie_rp) |
| 102 | { |
| 103 | acpigen_write_method_serialized("PSD0", 0); |
| 104 | pcie_rtd3_enable_modphy_pg(pcie_rp, PG_DISABLE); |
| 105 | acpigen_pop_len(); /* Method */ |
| 106 | } |
| 107 | |
| 108 | /* Method to enable/disable the source clock */ |
| 109 | static void pcie_rtd3_acpi_method_srck(unsigned int pcie_rp, |
| 110 | const struct soc_intel_common_block_pcie_rtd3_config *config) |
| 111 | { |
| 112 | acpigen_write_method_serialized("SRCK", 1); |
| 113 | |
| 114 | if (config->srcclk_pin >= 0) { |
| 115 | acpigen_write_if_lequal_op_op(ARG0_OP, 0); |
| 116 | pmc_ipc_acpi_set_pci_clock(pcie_rp, config->srcclk_pin, false); |
| 117 | acpigen_write_else(); |
| 118 | pmc_ipc_acpi_set_pci_clock(pcie_rp, config->srcclk_pin, true); |
| 119 | acpigen_pop_len(); /* If */ |
| 120 | } |
| 121 | acpigen_pop_len(); /* Method */ |
| 122 | } |
| 123 | |
Duncan Laurie | 64bc26a | 2020-10-10 00:15:28 +0000 | [diff] [blame] | 124 | static void |
| 125 | pcie_rtd3_acpi_method_on(unsigned int pcie_rp, |
Tim Wawrzynczak | 32f883e | 2021-12-02 16:19:47 -0700 | [diff] [blame] | 126 | const struct soc_intel_common_block_pcie_rtd3_config *config, |
Kane Chen | 11be556 | 2022-11-03 23:18:44 +0800 | [diff] [blame] | 127 | enum pcie_rp_type rp_type, |
| 128 | const struct device *dev) |
Duncan Laurie | 64bc26a | 2020-10-10 00:15:28 +0000 | [diff] [blame] | 129 | { |
Kane Chen | 11be556 | 2022-11-03 23:18:44 +0800 | [diff] [blame] | 130 | const struct device *parent = dev->bus->dev; |
| 131 | |
Duncan Laurie | 64bc26a | 2020-10-10 00:15:28 +0000 | [diff] [blame] | 132 | acpigen_write_method_serialized("_ON", 0); |
| 133 | |
Cliff Huang | 0539962 | 2023-02-04 21:20:08 -0800 | [diff] [blame] | 134 | /* When this feature is enabled, ONSK indicates if the previous _OFF was |
| 135 | * skipped. If so, since the device was not in Off state, and the current |
| 136 | * _ON can be skipped as well. |
| 137 | */ |
| 138 | if (config->skip_on_off_support) |
| 139 | acpigen_write_if_lequal_namestr_int("ONSK", 0); |
| 140 | |
Kane Chen | 11be556 | 2022-11-03 23:18:44 +0800 | [diff] [blame] | 141 | /* The _STA returns current power status of device, so we can skip _ON |
| 142 | * if _STA returns 1 |
| 143 | * Example: |
| 144 | * Local0 = \_SB.PCI0.RP01.RTD3._STA () |
| 145 | * If ((Local0 == One)) |
| 146 | * { |
| 147 | * Return (One) |
| 148 | * } |
| 149 | */ |
| 150 | acpigen_write_store(); |
| 151 | acpigen_emit_namestring(acpi_device_path_join(parent, "RTD3._STA")); |
| 152 | acpigen_emit_byte(LOCAL0_OP); |
| 153 | acpigen_write_if_lequal_op_int(LOCAL0_OP, ONE_OP); |
| 154 | acpigen_write_return_op(ONE_OP); |
| 155 | acpigen_write_if_end(); |
| 156 | |
Cliff Huang | 69564f3 | 2023-03-02 10:03:32 -0800 | [diff] [blame] | 157 | if (config->use_rp_mutex) |
| 158 | acpigen_write_acquire(acpi_device_path_join(parent, RP_MUTEX_NAME), |
| 159 | ACPI_MUTEX_NO_TIMEOUT); |
| 160 | |
Tim Wawrzynczak | 32f883e | 2021-12-02 16:19:47 -0700 | [diff] [blame] | 161 | /* Disable modPHY power gating for PCH RPs. */ |
| 162 | if (rp_type == PCIE_RP_PCH) |
| 163 | pcie_rtd3_enable_modphy_pg(pcie_rp, PG_DISABLE); |
| 164 | |
Duncan Laurie | 64bc26a | 2020-10-10 00:15:28 +0000 | [diff] [blame] | 165 | /* Assert enable GPIO to turn on device power. */ |
| 166 | if (config->enable_gpio.pin_count) { |
| 167 | acpigen_enable_tx_gpio(&config->enable_gpio); |
| 168 | if (config->enable_delay_ms) |
| 169 | acpigen_write_sleep(config->enable_delay_ms); |
| 170 | } |
| 171 | |
| 172 | /* Enable SRCCLK for root port if pin is defined. */ |
| 173 | if (config->srcclk_pin >= 0) |
| 174 | pmc_ipc_acpi_set_pci_clock(pcie_rp, config->srcclk_pin, true); |
| 175 | |
| 176 | /* De-assert reset GPIO to bring device out of reset. */ |
| 177 | if (config->reset_gpio.pin_count) { |
| 178 | acpigen_disable_tx_gpio(&config->reset_gpio); |
| 179 | if (config->reset_delay_ms) |
| 180 | acpigen_write_sleep(config->reset_delay_ms); |
| 181 | } |
| 182 | |
Tim Wawrzynczak | 32f883e | 2021-12-02 16:19:47 -0700 | [diff] [blame] | 183 | /* Trigger L23 ready exit flow unless disabled by config. */ |
Duncan Laurie | 64bc26a | 2020-10-10 00:15:28 +0000 | [diff] [blame] | 184 | if (!config->disable_l23) |
| 185 | pcie_rtd3_acpi_l23_exit(); |
| 186 | |
Cliff Huang | 69564f3 | 2023-03-02 10:03:32 -0800 | [diff] [blame] | 187 | if (config->use_rp_mutex) |
| 188 | acpigen_write_release(acpi_device_path_join(parent, RP_MUTEX_NAME)); |
| 189 | |
Cliff Huang | d1a7416 | 2022-01-21 14:54:32 -0800 | [diff] [blame] | 190 | if (config->skip_on_off_support) { |
| 191 | /* If current _ON is skipped, ONSK is decremented so that _ON will be |
| 192 | * executed normally until _OFF is skipped again. |
| 193 | */ |
| 194 | acpigen_write_else(); |
| 195 | acpigen_emit_byte(DECREMENT_OP); |
| 196 | acpigen_emit_namestring("ONSK"); |
| 197 | |
| 198 | acpigen_pop_len(); /* Else */ |
| 199 | } |
Duncan Laurie | 64bc26a | 2020-10-10 00:15:28 +0000 | [diff] [blame] | 200 | acpigen_pop_len(); /* Method */ |
| 201 | } |
| 202 | |
| 203 | static void |
| 204 | pcie_rtd3_acpi_method_off(int pcie_rp, |
Tim Wawrzynczak | 32f883e | 2021-12-02 16:19:47 -0700 | [diff] [blame] | 205 | const struct soc_intel_common_block_pcie_rtd3_config *config, |
Cliff Huang | 69564f3 | 2023-03-02 10:03:32 -0800 | [diff] [blame] | 206 | enum pcie_rp_type rp_type, |
| 207 | const struct device *dev) |
| 208 | |
Duncan Laurie | 64bc26a | 2020-10-10 00:15:28 +0000 | [diff] [blame] | 209 | { |
Cliff Huang | 69564f3 | 2023-03-02 10:03:32 -0800 | [diff] [blame] | 210 | const struct device *parent = dev->bus->dev; |
| 211 | |
Duncan Laurie | 64bc26a | 2020-10-10 00:15:28 +0000 | [diff] [blame] | 212 | acpigen_write_method_serialized("_OFF", 0); |
| 213 | |
Cliff Huang | d1a7416 | 2022-01-21 14:54:32 -0800 | [diff] [blame] | 214 | /* When this feature is enabled, ONSK is checked to see if the device |
| 215 | * wants _OFF to be skipped for once. ONSK is normally incremented in the |
| 216 | * device method, such as reset _RST, which is invoked during driver reload. |
| 217 | * In such case, _OFF needs to be avoided at the end of driver removal. |
| 218 | */ |
| 219 | if (config->skip_on_off_support) |
| 220 | acpigen_write_if_lequal_namestr_int("OFSK", 0); |
| 221 | |
Cliff Huang | 69564f3 | 2023-03-02 10:03:32 -0800 | [diff] [blame] | 222 | if (config->use_rp_mutex) |
| 223 | acpigen_write_acquire(acpi_device_path_join(parent, RP_MUTEX_NAME), |
| 224 | ACPI_MUTEX_NO_TIMEOUT); |
| 225 | |
Duncan Laurie | 64bc26a | 2020-10-10 00:15:28 +0000 | [diff] [blame] | 226 | /* Trigger L23 ready entry flow unless disabled by config. */ |
| 227 | if (!config->disable_l23) |
| 228 | pcie_rtd3_acpi_l23_entry(); |
| 229 | |
| 230 | /* Assert reset GPIO to place device into reset. */ |
| 231 | if (config->reset_gpio.pin_count) { |
| 232 | acpigen_enable_tx_gpio(&config->reset_gpio); |
| 233 | if (config->reset_off_delay_ms) |
| 234 | acpigen_write_sleep(config->reset_off_delay_ms); |
| 235 | } |
| 236 | |
Tim Wawrzynczak | 32f883e | 2021-12-02 16:19:47 -0700 | [diff] [blame] | 237 | /* Enable modPHY power gating for PCH RPs */ |
| 238 | if (rp_type == PCIE_RP_PCH) |
| 239 | pcie_rtd3_enable_modphy_pg(pcie_rp, PG_ENABLE); |
| 240 | |
Duncan Laurie | 64bc26a | 2020-10-10 00:15:28 +0000 | [diff] [blame] | 241 | /* Disable SRCCLK for this root port if pin is defined. */ |
| 242 | if (config->srcclk_pin >= 0) |
| 243 | pmc_ipc_acpi_set_pci_clock(pcie_rp, config->srcclk_pin, false); |
| 244 | |
| 245 | /* De-assert enable GPIO to turn off device power. */ |
| 246 | if (config->enable_gpio.pin_count) { |
| 247 | acpigen_disable_tx_gpio(&config->enable_gpio); |
| 248 | if (config->enable_off_delay_ms) |
| 249 | acpigen_write_sleep(config->enable_off_delay_ms); |
| 250 | } |
| 251 | |
Cliff Huang | 69564f3 | 2023-03-02 10:03:32 -0800 | [diff] [blame] | 252 | if (config->use_rp_mutex) |
| 253 | acpigen_write_release(acpi_device_path_join(parent, RP_MUTEX_NAME)); |
| 254 | |
Cliff Huang | d1a7416 | 2022-01-21 14:54:32 -0800 | [diff] [blame] | 255 | if (config->skip_on_off_support) { |
| 256 | /* If current _OFF is skipped, ONSK is incremented so that the |
| 257 | * following _ON will also be skipped. In addition, OFSK is decremented |
| 258 | * so that next _OFF will be executed normally until the device method |
| 259 | * increments OFSK again. |
| 260 | */ |
| 261 | acpigen_write_else(); |
| 262 | /* OFSK-- */ |
| 263 | acpigen_emit_byte(DECREMENT_OP); |
| 264 | acpigen_emit_namestring("OFSK"); |
| 265 | /* ONSK++ */ |
| 266 | acpigen_emit_byte(INCREMENT_OP); |
| 267 | acpigen_emit_namestring("ONSK"); |
| 268 | |
| 269 | acpigen_pop_len(); /* Else */ |
| 270 | } |
Duncan Laurie | 64bc26a | 2020-10-10 00:15:28 +0000 | [diff] [blame] | 271 | acpigen_pop_len(); /* Method */ |
| 272 | } |
| 273 | |
| 274 | static void |
Tim Wawrzynczak | b6a15a7 | 2021-12-08 10:43:56 -0700 | [diff] [blame] | 275 | pcie_rtd3_acpi_method_status(const struct soc_intel_common_block_pcie_rtd3_config *config) |
Duncan Laurie | 64bc26a | 2020-10-10 00:15:28 +0000 | [diff] [blame] | 276 | { |
| 277 | const struct acpi_gpio *gpio; |
| 278 | |
| 279 | acpigen_write_method("_STA", 0); |
Cliff Huang | 9a5a963 | 2023-01-24 17:05:17 -0800 | [diff] [blame] | 280 | /* |
| 281 | * Depending on the board configuration we use either the "enable" or |
| 282 | * the "reset" pin to detect the status of the device. The logic for |
| 283 | * each pin is detailed below. |
| 284 | * |
| 285 | * 1. For the "enable" pin: |
| 286 | * | polarity | tx value | get_tx_gpio() | State | |
| 287 | * |-------------+----------+---------------+-------| |
| 288 | * | active high | 0 | 0 | 0 | |
| 289 | * | active high | 1 | 1(active) | 1 | |
| 290 | * | active low | 0 | 1(active) | 1 | |
| 291 | * | active low | 1 | 0 | 0 | |
| 292 | * |
| 293 | * 2. For the "reset" pin: |
| 294 | * | polarity | tx value | get_tx_gpio() | State | |
| 295 | * |-------------+----------+---------------+-------| |
| 296 | * | active high | 0 | 0 | 1 | |
| 297 | * | active high | 1 | 1(active) | 0 | |
| 298 | * | active low | 0 | 1(active) | 0 | |
| 299 | * | active low | 1 | 0 | 1 | |
| 300 | */ |
Duncan Laurie | 64bc26a | 2020-10-10 00:15:28 +0000 | [diff] [blame] | 301 | |
| 302 | /* Use enable GPIO for status if provided, otherwise use reset GPIO. */ |
Cliff Huang | 9a5a963 | 2023-01-24 17:05:17 -0800 | [diff] [blame] | 303 | if (config->enable_gpio.pin_count) { |
Duncan Laurie | 64bc26a | 2020-10-10 00:15:28 +0000 | [diff] [blame] | 304 | gpio = &config->enable_gpio; |
Cliff Huang | 9a5a963 | 2023-01-24 17:05:17 -0800 | [diff] [blame] | 305 | /* Read current GPIO state into Local0. */ |
| 306 | acpigen_get_tx_gpio(gpio); |
| 307 | } else { |
Duncan Laurie | 64bc26a | 2020-10-10 00:15:28 +0000 | [diff] [blame] | 308 | gpio = &config->reset_gpio; |
Cliff Huang | 9a5a963 | 2023-01-24 17:05:17 -0800 | [diff] [blame] | 309 | /* Read current GPIO state into Local0. */ |
| 310 | acpigen_get_tx_gpio(gpio); |
| 311 | acpigen_write_not(LOCAL0_OP, LOCAL0_OP); |
| 312 | } |
| 313 | acpigen_write_return_op(LOCAL0_OP); |
Duncan Laurie | 64bc26a | 2020-10-10 00:15:28 +0000 | [diff] [blame] | 314 | acpigen_pop_len(); /* Method */ |
| 315 | } |
| 316 | |
Tim Wawrzynczak | 32f883e | 2021-12-02 16:19:47 -0700 | [diff] [blame] | 317 | static void write_modphy_opregion(unsigned int pcie_rp) |
| 318 | { |
| 319 | /* The register containing the Power Gate enable sequence bits is at |
| 320 | PCH_PWRM_BASE + 0x10D0, and the bits to check for sequence completion are at |
| 321 | PCH_PWRM_BASE + 0x10D4. */ |
| 322 | const struct opregion opregion = OPREGION("PMCP", SYSTEMMEMORY, |
| 323 | PCH_PWRM_BASE_ADDRESS + 0x1000, 0xff); |
| 324 | const struct fieldlist fieldlist[] = { |
| 325 | FIELDLIST_OFFSET(0xD0), |
| 326 | FIELDLIST_RESERVED(pcie_rp), |
| 327 | FIELDLIST_NAMESTR("EMPG", 1), /* Enable ModPHY Power Gate */ |
| 328 | FIELDLIST_OFFSET(0xD4), |
| 329 | FIELDLIST_RESERVED(pcie_rp), |
| 330 | FIELDLIST_NAMESTR("AMPG", 1), /* Is ModPHY Power Gate active? */ |
| 331 | }; |
| 332 | |
| 333 | acpigen_write_opregion(&opregion); |
| 334 | acpigen_write_field("PMCP", fieldlist, ARRAY_SIZE(fieldlist), |
| 335 | FIELD_DWORDACC | FIELD_NOLOCK | FIELD_PRESERVE); |
| 336 | } |
| 337 | |
Tim Wawrzynczak | b6a15a7 | 2021-12-08 10:43:56 -0700 | [diff] [blame] | 338 | static int get_pcie_rp_pmc_idx(enum pcie_rp_type rp_type, const struct device *dev) |
| 339 | { |
| 340 | int idx = -1; |
| 341 | |
| 342 | switch (rp_type) { |
| 343 | case PCIE_RP_PCH: |
| 344 | /* Read port number of root port that this device is attached to. */ |
| 345 | idx = pci_read_config8(dev, PCH_PCIE_CFG_LCAP_PN); |
| 346 | |
| 347 | /* Port number is 1-based, PMC IPC method expects 0-based. */ |
| 348 | idx--; |
| 349 | break; |
| 350 | case PCIE_RP_CPU: |
| 351 | /* CPU RPs are indexed by their "virtual wire index" to the PCH */ |
| 352 | idx = soc_get_cpu_rp_vw_idx(dev); |
| 353 | break; |
| 354 | default: |
| 355 | break; |
| 356 | } |
| 357 | |
| 358 | return idx; |
| 359 | } |
| 360 | |
Duncan Laurie | 64bc26a | 2020-10-10 00:15:28 +0000 | [diff] [blame] | 361 | static void pcie_rtd3_acpi_fill_ssdt(const struct device *dev) |
| 362 | { |
Tim Wawrzynczak | 32f883e | 2021-12-02 16:19:47 -0700 | [diff] [blame] | 363 | static bool mutex_created = false; |
| 364 | |
Duncan Laurie | 64bc26a | 2020-10-10 00:15:28 +0000 | [diff] [blame] | 365 | const struct soc_intel_common_block_pcie_rtd3_config *config = config_of(dev); |
| 366 | static const char *const power_res_states[] = {"_PR0"}; |
| 367 | const struct device *parent = dev->bus->dev; |
| 368 | const char *scope = acpi_device_path(parent); |
| 369 | const struct opregion opregion = OPREGION("PXCS", PCI_CONFIG, 0, 0xff); |
| 370 | const struct fieldlist fieldlist[] = { |
| 371 | FIELDLIST_OFFSET(PCH_PCIE_CFG_LSTS), |
| 372 | FIELDLIST_RESERVED(13), |
| 373 | FIELDLIST_NAMESTR(ACPI_REG_PCI_LINK_ACTIVE, 1), |
| 374 | FIELDLIST_OFFSET(PCH_PCIE_CFG_SPR), |
| 375 | FIELDLIST_RESERVED(7), |
| 376 | FIELDLIST_NAMESTR(ACPI_REG_PCI_L23_SAVE_STATE, 1), |
| 377 | FIELDLIST_OFFSET(PCH_PCIE_CFG_RPPGEN), |
| 378 | FIELDLIST_RESERVED(2), |
| 379 | FIELDLIST_NAMESTR(ACPI_REG_PCI_L23_RDY_ENTRY, 1), |
| 380 | FIELDLIST_NAMESTR(ACPI_REG_PCI_L23_RDY_DETECT, 1), |
| 381 | }; |
Tim Wawrzynczak | b6a15a7 | 2021-12-08 10:43:56 -0700 | [diff] [blame] | 382 | int pcie_rp; |
Kapil Porwal | 65bcb57 | 2022-11-28 18:53:40 +0530 | [diff] [blame] | 383 | struct acpi_dp *dsd; |
Duncan Laurie | 64bc26a | 2020-10-10 00:15:28 +0000 | [diff] [blame] | 384 | |
| 385 | if (!is_dev_enabled(parent)) { |
| 386 | printk(BIOS_ERR, "%s: root port not enabled\n", __func__); |
| 387 | return; |
| 388 | } |
| 389 | if (!scope) { |
| 390 | printk(BIOS_ERR, "%s: root port scope not found\n", __func__); |
| 391 | return; |
| 392 | } |
| 393 | if (!config->enable_gpio.pin_count && !config->reset_gpio.pin_count) { |
| 394 | printk(BIOS_ERR, "%s: Enable and/or Reset GPIO required for %s.\n", |
| 395 | __func__, scope); |
| 396 | return; |
| 397 | } |
Rizwan Qureshi | a979460 | 2021-04-08 20:31:47 +0530 | [diff] [blame] | 398 | if (config->srcclk_pin > CONFIG_MAX_PCIE_CLOCK_SRC) { |
Duncan Laurie | 64bc26a | 2020-10-10 00:15:28 +0000 | [diff] [blame] | 399 | printk(BIOS_ERR, "%s: Invalid clock pin %u for %s.\n", __func__, |
| 400 | config->srcclk_pin, scope); |
| 401 | return; |
| 402 | } |
| 403 | |
Tim Wawrzynczak | 32f883e | 2021-12-02 16:19:47 -0700 | [diff] [blame] | 404 | const enum pcie_rp_type rp_type = soc_get_pcie_rp_type(parent); |
Tim Wawrzynczak | b6a15a7 | 2021-12-08 10:43:56 -0700 | [diff] [blame] | 405 | pcie_rp = get_pcie_rp_pmc_idx(rp_type, parent); |
Tim Wawrzynczak | b3cd55b | 2022-01-20 14:06:29 -0700 | [diff] [blame] | 406 | if (pcie_rp < 0) { |
Tim Wawrzynczak | b6a15a7 | 2021-12-08 10:43:56 -0700 | [diff] [blame] | 407 | printk(BIOS_ERR, "%s: Unknown PCIe root port\n", __func__); |
Duncan Laurie | 64bc26a | 2020-10-10 00:15:28 +0000 | [diff] [blame] | 408 | return; |
| 409 | } |
Cliff Huang | 4bc9ac7 | 2022-01-21 00:23:15 -0800 | [diff] [blame] | 410 | if (config->disable_l23) { |
Angel Pons | d85319a | 2022-02-13 13:35:20 +0100 | [diff] [blame] | 411 | if (config->ext_pm_support & ACPI_PCIE_RP_EMIT_L23) { |
Cliff Huang | 4bc9ac7 | 2022-01-21 00:23:15 -0800 | [diff] [blame] | 412 | printk(BIOS_ERR, "%s: Can not export L23 methods\n", __func__); |
| 413 | return; |
| 414 | } |
| 415 | } |
| 416 | if (rp_type != PCIE_RP_PCH) { |
Angel Pons | d85319a | 2022-02-13 13:35:20 +0100 | [diff] [blame] | 417 | if (config->ext_pm_support & ACPI_PCIE_RP_EMIT_PSD0) { |
Cliff Huang | 4bc9ac7 | 2022-01-21 00:23:15 -0800 | [diff] [blame] | 418 | printk(BIOS_ERR, "%s: Can not export PSD0 method\n", __func__); |
| 419 | return; |
| 420 | } |
| 421 | } |
| 422 | if (config->srcclk_pin == 0) { |
Angel Pons | d85319a | 2022-02-13 13:35:20 +0100 | [diff] [blame] | 423 | if (config->ext_pm_support & ACPI_PCIE_RP_EMIT_SRCK) { |
Cliff Huang | 4bc9ac7 | 2022-01-21 00:23:15 -0800 | [diff] [blame] | 424 | printk(BIOS_ERR, "%s: Can not export SRCK method\n", __func__); |
| 425 | return; |
| 426 | } |
| 427 | } |
Duncan Laurie | 64bc26a | 2020-10-10 00:15:28 +0000 | [diff] [blame] | 428 | |
| 429 | printk(BIOS_INFO, "%s: Enable RTD3 for %s (%s)\n", scope, dev_path(parent), |
| 430 | config->desc ?: dev->chip_ops->name); |
| 431 | |
Tim Wawrzynczak | 32f883e | 2021-12-02 16:19:47 -0700 | [diff] [blame] | 432 | /* Create a mutex for exclusive access to the PMC registers. */ |
| 433 | if (rp_type == PCIE_RP_PCH && !mutex_created) { |
| 434 | acpigen_write_scope("\\_SB.PCI0"); |
| 435 | acpigen_write_mutex("R3MX", 0); |
| 436 | acpigen_write_scope_end(); |
| 437 | mutex_created = true; |
| 438 | } |
| 439 | |
Duncan Laurie | 64bc26a | 2020-10-10 00:15:28 +0000 | [diff] [blame] | 440 | /* The RTD3 power resource is added to the root port, not the device. */ |
| 441 | acpigen_write_scope(scope); |
| 442 | |
Cliff Huang | 69564f3 | 2023-03-02 10:03:32 -0800 | [diff] [blame] | 443 | if (config->use_rp_mutex) |
| 444 | acpigen_write_mutex(RP_MUTEX_NAME, 0); |
| 445 | |
Duncan Laurie | 64bc26a | 2020-10-10 00:15:28 +0000 | [diff] [blame] | 446 | if (config->desc) |
| 447 | acpigen_write_name_string("_DDN", config->desc); |
| 448 | |
Tim Wawrzynczak | 32f883e | 2021-12-02 16:19:47 -0700 | [diff] [blame] | 449 | /* Create OpRegions for MMIO accesses. */ |
Duncan Laurie | 64bc26a | 2020-10-10 00:15:28 +0000 | [diff] [blame] | 450 | acpigen_write_opregion(&opregion); |
| 451 | acpigen_write_field("PXCS", fieldlist, ARRAY_SIZE(fieldlist), |
| 452 | FIELD_ANYACC | FIELD_NOLOCK | FIELD_PRESERVE); |
| 453 | |
Angel Pons | d85319a | 2022-02-13 13:35:20 +0100 | [diff] [blame] | 454 | if (config->ext_pm_support & ACPI_PCIE_RP_EMIT_L23) { |
Cliff Huang | 4bc9ac7 | 2022-01-21 00:23:15 -0800 | [diff] [blame] | 455 | pcie_rtd3_acpi_method_dl23(); |
| 456 | pcie_rtd3_acpi_method_l23d(); |
| 457 | } |
| 458 | |
Tim Wawrzynczak | 32f883e | 2021-12-02 16:19:47 -0700 | [diff] [blame] | 459 | /* Create the OpRegion to access the ModPHY PG registers (PCH RPs only) */ |
| 460 | if (rp_type == PCIE_RP_PCH) |
| 461 | write_modphy_opregion(pcie_rp); |
| 462 | |
Angel Pons | d85319a | 2022-02-13 13:35:20 +0100 | [diff] [blame] | 463 | if (config->ext_pm_support & ACPI_PCIE_RP_EMIT_PSD0) |
Cliff Huang | 4bc9ac7 | 2022-01-21 00:23:15 -0800 | [diff] [blame] | 464 | pcie_rtd3_acpi_method_pds0(pcie_rp); |
| 465 | |
Angel Pons | d85319a | 2022-02-13 13:35:20 +0100 | [diff] [blame] | 466 | if (config->ext_pm_support & ACPI_PCIE_RP_EMIT_SRCK) |
Cliff Huang | 4bc9ac7 | 2022-01-21 00:23:15 -0800 | [diff] [blame] | 467 | pcie_rtd3_acpi_method_srck(pcie_rp, config); |
| 468 | |
Duncan Laurie | 64bc26a | 2020-10-10 00:15:28 +0000 | [diff] [blame] | 469 | /* ACPI Power Resource for controlling the attached device power. */ |
| 470 | acpigen_write_power_res("RTD3", 0, 0, power_res_states, ARRAY_SIZE(power_res_states)); |
Cliff Huang | d1a7416 | 2022-01-21 14:54:32 -0800 | [diff] [blame] | 471 | |
| 472 | if (config->skip_on_off_support) { |
| 473 | /* OFSK: 0 = _OFF Method will be executed normally when called; |
| 474 | * >1 = _OFF will be skipped. |
| 475 | * _OFF Method to decrement OFSK and increment ONSK if the |
| 476 | * current execution is skipped. |
| 477 | * ONSK: 0 = _ON Method will be executed normally when called; |
| 478 | * >1 = _ONF will be skipped. |
| 479 | * _ON Method to decrement ONSK if the current execution is |
| 480 | * skipped. |
| 481 | */ |
| 482 | acpigen_write_name_integer("ONSK", 0); |
| 483 | acpigen_write_name_integer("OFSK", 0); |
| 484 | } |
| 485 | |
Tim Wawrzynczak | b6a15a7 | 2021-12-08 10:43:56 -0700 | [diff] [blame] | 486 | pcie_rtd3_acpi_method_status(config); |
Kane Chen | 11be556 | 2022-11-03 23:18:44 +0800 | [diff] [blame] | 487 | pcie_rtd3_acpi_method_on(pcie_rp, config, rp_type, dev); |
Cliff Huang | 69564f3 | 2023-03-02 10:03:32 -0800 | [diff] [blame] | 488 | pcie_rtd3_acpi_method_off(pcie_rp, config, rp_type, dev); |
Duncan Laurie | 64bc26a | 2020-10-10 00:15:28 +0000 | [diff] [blame] | 489 | acpigen_pop_len(); /* PowerResource */ |
| 490 | |
| 491 | /* Indicate to the OS that device supports hotplug in D3. */ |
| 492 | dsd = acpi_dp_new_table("_DSD"); |
Kapil Porwal | 65bcb57 | 2022-11-28 18:53:40 +0530 | [diff] [blame] | 493 | acpi_device_add_hotplug_support_in_d3(dsd); |
Duncan Laurie | 64bc26a | 2020-10-10 00:15:28 +0000 | [diff] [blame] | 494 | |
| 495 | /* Indicate to the OS if the device provides an External facing port. */ |
Kapil Porwal | 65bcb57 | 2022-11-28 18:53:40 +0530 | [diff] [blame] | 496 | if (config->add_acpi_external_facing_port) |
| 497 | acpi_device_add_external_facing_port(dsd); |
Kapil Porwal | d7eacd7 | 2022-11-28 11:03:38 +0530 | [diff] [blame] | 498 | |
| 499 | /* Indicate to the OS if the device has DMA property. */ |
| 500 | if (config->add_acpi_dma_property) |
| 501 | acpi_device_add_dma_property(dsd); |
| 502 | |
Duncan Laurie | 64bc26a | 2020-10-10 00:15:28 +0000 | [diff] [blame] | 503 | acpi_dp_write(dsd); |
| 504 | |
| 505 | /* |
| 506 | * Check the sibling device on the root port to see if it is storage class and add the |
| 507 | * property for the OS to enable storage D3, or allow it to be enabled by config. |
| 508 | */ |
| 509 | if (config->is_storage |
| 510 | || (dev->sibling && (dev->sibling->class >> 16) == PCI_BASE_CLASS_STORAGE)) { |
| 511 | acpigen_write_device(acpi_device_name(dev)); |
| 512 | acpigen_write_ADR(0); |
| 513 | acpigen_write_STA(ACPI_STATUS_DEVICE_ALL_ON); |
Sean Rhodes | 648ff92 | 2023-04-13 13:25:14 +0100 | [diff] [blame] | 514 | if (CONFIG(D3COLD_SUPPORT)) |
| 515 | acpigen_write_name_integer("_S0W", ACPI_DEVICE_SLEEP_D3_COLD); |
| 516 | else |
| 517 | acpigen_write_name_integer("_S0W", ACPI_DEVICE_SLEEP_D3_HOT); |
Duncan Laurie | 64bc26a | 2020-10-10 00:15:28 +0000 | [diff] [blame] | 518 | |
Kapil Porwal | 65bcb57 | 2022-11-28 18:53:40 +0530 | [diff] [blame] | 519 | acpi_device_add_storage_d3_enable(NULL); |
Duncan Laurie | 64bc26a | 2020-10-10 00:15:28 +0000 | [diff] [blame] | 520 | |
| 521 | acpigen_pop_len(); /* Device */ |
| 522 | |
| 523 | printk(BIOS_INFO, "%s: Added StorageD3Enable property\n", scope); |
| 524 | } |
| 525 | |
| 526 | acpigen_pop_len(); /* Scope */ |
| 527 | } |
| 528 | |
| 529 | static const char *pcie_rtd3_acpi_name(const struct device *dev) |
| 530 | { |
| 531 | /* Attached device name must be "PXSX" for the Linux Kernel to recognize it. */ |
| 532 | return "PXSX"; |
| 533 | } |
| 534 | |
| 535 | static struct device_operations pcie_rtd3_ops = { |
| 536 | .read_resources = noop_read_resources, |
| 537 | .set_resources = noop_set_resources, |
| 538 | .acpi_fill_ssdt = pcie_rtd3_acpi_fill_ssdt, |
| 539 | .acpi_name = pcie_rtd3_acpi_name, |
| 540 | }; |
| 541 | |
| 542 | static void pcie_rtd3_acpi_enable(struct device *dev) |
| 543 | { |
| 544 | dev->ops = &pcie_rtd3_ops; |
| 545 | } |
| 546 | |
| 547 | struct chip_operations soc_intel_common_block_pcie_rtd3_ops = { |
| 548 | CHIP_NAME("Intel PCIe Runtime D3") |
| 549 | .enable_dev = pcie_rtd3_acpi_enable |
| 550 | }; |