blob: 507c3c8dea5c9d6f4fe0d7b0e215872687f8ad24 [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
16/*
17 * The "ExternalFacingPort" and "HotPlugSupportInD3" properties are defined at
18 * https://docs.microsoft.com/en-us/windows-hardware/drivers/pci/dsd-for-pcie-root-ports
19 */
20#define PCIE_EXTERNAL_PORT_UUID "EFCC06CC-73AC-4BC3-BFF0-76143807C389"
21#define PCIE_EXTERNAL_PORT_PROPERTY "ExternalFacingPort"
22
23#define PCIE_HOTPLUG_IN_D3_UUID "6211E2C0-58A3-4AF3-90E1-927A4E0C55A4"
24#define PCIE_HOTPLUG_IN_D3_PROPERTY "HotPlugSupportInD3"
25
26/*
27 * This UUID and the resulting ACPI Device Property is defined by the
28 * Power Management for Storage Hardware Devices:
29 *
30 * https://docs.microsoft.com/en-us/windows-hardware/design/component-guidelines/power-management-for-storage-hardware-devices-intro
31 */
32#define PCIE_RTD3_STORAGE_UUID "5025030F-842F-4AB4-A561-99A5189762D0"
33#define PCIE_RTD3_STORAGE_PROPERTY "StorageD3Enable"
34
35/* PCIe Root Port registers for link status and L23 control. */
36#define PCH_PCIE_CFG_LSTS 0x52 /* Link Status Register */
37#define PCH_PCIE_CFG_SPR 0xe0 /* Scratchpad */
38#define PCH_PCIE_CFG_RPPGEN 0xe2 /* Root Port Power Gating Enable */
39#define PCH_PCIE_CFG_LCAP_PN 0x4f /* Root Port Number */
40
41/* ACPI register names corresponding to PCIe root port registers. */
42#define ACPI_REG_PCI_LINK_ACTIVE "LASX" /* Link active status */
43#define ACPI_REG_PCI_L23_RDY_ENTRY "L23E" /* L23_Rdy Entry Request */
44#define ACPI_REG_PCI_L23_RDY_DETECT "L23R" /* L23_Rdy Detect Transition */
45#define ACPI_REG_PCI_L23_SAVE_STATE "NCB7" /* Scratch bit to save L23 state */
46
Tim Wawrzynczak32f883e2021-12-02 16:19:47 -070047/* ACPI path to the mutex that protects accesses to PMC ModPhy power gating registers */
48#define RTD3_MUTEX_PATH "\\_SB.PCI0.R3MX"
49
50enum modphy_pg_state {
51 PG_DISABLE = 0,
52 PG_ENABLE = 1,
53};
54
Duncan Laurie64bc26a2020-10-10 00:15:28 +000055/* Called from _ON to get PCIe link back to active state. */
56static void pcie_rtd3_acpi_l23_exit(void)
57{
58 /* Skip if port is not in L2/L3. */
59 acpigen_write_if_lequal_namestr_int(ACPI_REG_PCI_L23_SAVE_STATE, 1);
60
61 /* Initiate L2/L3 Ready To Detect transition. */
62 acpigen_write_store_int_to_namestr(1, ACPI_REG_PCI_L23_RDY_DETECT);
63
64 /* Wait for transition to detect. */
65 acpigen_write_delay_until_namestr_int(320, ACPI_REG_PCI_L23_RDY_DETECT, 0);
66
67 acpigen_write_store_int_to_namestr(0, ACPI_REG_PCI_L23_SAVE_STATE);
68
69 /* Once in detect, wait for link active. */
70 acpigen_write_delay_until_namestr_int(128, ACPI_REG_PCI_LINK_ACTIVE, 1);
71
72 acpigen_pop_len(); /* If */
73}
74
75/* Called from _OFF to put PCIe link into L2/L3 state. */
76static void pcie_rtd3_acpi_l23_entry(void)
77{
78 /* Initiate L2/L3 Entry request. */
79 acpigen_write_store_int_to_namestr(1, ACPI_REG_PCI_L23_RDY_ENTRY);
80
81 /* Wait for L2/L3 Entry request to clear. */
82 acpigen_write_delay_until_namestr_int(128, ACPI_REG_PCI_L23_RDY_ENTRY, 0);
83
84 acpigen_write_store_int_to_namestr(1, ACPI_REG_PCI_L23_SAVE_STATE);
85}
86
Tim Wawrzynczak32f883e2021-12-02 16:19:47 -070087/* Called from _ON/_OFF to disable/enable ModPHY power gating */
88static void pcie_rtd3_enable_modphy_pg(unsigned int pcie_rp, enum modphy_pg_state state)
89{
90 /* Enter the critical section */
91 acpigen_emit_ext_op(ACQUIRE_OP);
92 acpigen_emit_namestring(RTD3_MUTEX_PATH);
93 acpigen_emit_word(ACPI_MUTEX_NO_TIMEOUT);
94
95 acpigen_write_store_int_to_namestr(state, "EMPG");
96 acpigen_write_delay_until_namestr_int(100, "AMPG", state);
97
98 /* Exit the critical section */
99 acpigen_emit_ext_op(RELEASE_OP);
100 acpigen_emit_namestring(RTD3_MUTEX_PATH);
101}
102
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000103static void
104pcie_rtd3_acpi_method_on(unsigned int pcie_rp,
Tim Wawrzynczak32f883e2021-12-02 16:19:47 -0700105 const struct soc_intel_common_block_pcie_rtd3_config *config,
106 enum pcie_rp_type rp_type)
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000107{
108 acpigen_write_method_serialized("_ON", 0);
109
Tim Wawrzynczak32f883e2021-12-02 16:19:47 -0700110 /* Disable modPHY power gating for PCH RPs. */
111 if (rp_type == PCIE_RP_PCH)
112 pcie_rtd3_enable_modphy_pg(pcie_rp, PG_DISABLE);
113
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000114 /* Assert enable GPIO to turn on device power. */
115 if (config->enable_gpio.pin_count) {
116 acpigen_enable_tx_gpio(&config->enable_gpio);
117 if (config->enable_delay_ms)
118 acpigen_write_sleep(config->enable_delay_ms);
119 }
120
121 /* Enable SRCCLK for root port if pin is defined. */
122 if (config->srcclk_pin >= 0)
123 pmc_ipc_acpi_set_pci_clock(pcie_rp, config->srcclk_pin, true);
124
125 /* De-assert reset GPIO to bring device out of reset. */
126 if (config->reset_gpio.pin_count) {
127 acpigen_disable_tx_gpio(&config->reset_gpio);
128 if (config->reset_delay_ms)
129 acpigen_write_sleep(config->reset_delay_ms);
130 }
131
Tim Wawrzynczak32f883e2021-12-02 16:19:47 -0700132 /* Trigger L23 ready exit flow unless disabled by config. */
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000133 if (!config->disable_l23)
134 pcie_rtd3_acpi_l23_exit();
135
136 acpigen_pop_len(); /* Method */
137}
138
139static void
140pcie_rtd3_acpi_method_off(int pcie_rp,
Tim Wawrzynczak32f883e2021-12-02 16:19:47 -0700141 const struct soc_intel_common_block_pcie_rtd3_config *config,
142 enum pcie_rp_type rp_type)
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000143{
144 acpigen_write_method_serialized("_OFF", 0);
145
146 /* Trigger L23 ready entry flow unless disabled by config. */
147 if (!config->disable_l23)
148 pcie_rtd3_acpi_l23_entry();
149
150 /* Assert reset GPIO to place device into reset. */
151 if (config->reset_gpio.pin_count) {
152 acpigen_enable_tx_gpio(&config->reset_gpio);
153 if (config->reset_off_delay_ms)
154 acpigen_write_sleep(config->reset_off_delay_ms);
155 }
156
Tim Wawrzynczak32f883e2021-12-02 16:19:47 -0700157 /* Enable modPHY power gating for PCH RPs */
158 if (rp_type == PCIE_RP_PCH)
159 pcie_rtd3_enable_modphy_pg(pcie_rp, PG_ENABLE);
160
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000161 /* Disable SRCCLK for this root port if pin is defined. */
162 if (config->srcclk_pin >= 0)
163 pmc_ipc_acpi_set_pci_clock(pcie_rp, config->srcclk_pin, false);
164
165 /* De-assert enable GPIO to turn off device power. */
166 if (config->enable_gpio.pin_count) {
167 acpigen_disable_tx_gpio(&config->enable_gpio);
168 if (config->enable_off_delay_ms)
169 acpigen_write_sleep(config->enable_off_delay_ms);
170 }
171
172 acpigen_pop_len(); /* Method */
173}
174
175static void
176pcie_rtd3_acpi_method_status(int pcie_rp,
177 const struct soc_intel_common_block_pcie_rtd3_config *config)
178{
179 const struct acpi_gpio *gpio;
180
181 acpigen_write_method("_STA", 0);
182
183 /* Use enable GPIO for status if provided, otherwise use reset GPIO. */
184 if (config->enable_gpio.pin_count)
185 gpio = &config->enable_gpio;
186 else
187 gpio = &config->reset_gpio;
188
189 /* Read current GPIO value into Local0. */
190 acpigen_get_tx_gpio(gpio);
191
192 /* Ensure check works for both active low and active high GPIOs. */
193 acpigen_write_store_int_to_op(gpio->active_low, LOCAL1_OP);
194
195 acpigen_write_if_lequal_op_op(LOCAL0_OP, LOCAL1_OP);
196 acpigen_write_return_op(ZERO_OP);
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000197 acpigen_write_else();
198 acpigen_write_return_op(ONE_OP);
199 acpigen_pop_len(); /* Else */
200
201 acpigen_pop_len(); /* Method */
202}
203
Tim Wawrzynczak32f883e2021-12-02 16:19:47 -0700204static void write_modphy_opregion(unsigned int pcie_rp)
205{
206 /* The register containing the Power Gate enable sequence bits is at
207 PCH_PWRM_BASE + 0x10D0, and the bits to check for sequence completion are at
208 PCH_PWRM_BASE + 0x10D4. */
209 const struct opregion opregion = OPREGION("PMCP", SYSTEMMEMORY,
210 PCH_PWRM_BASE_ADDRESS + 0x1000, 0xff);
211 const struct fieldlist fieldlist[] = {
212 FIELDLIST_OFFSET(0xD0),
213 FIELDLIST_RESERVED(pcie_rp),
214 FIELDLIST_NAMESTR("EMPG", 1), /* Enable ModPHY Power Gate */
215 FIELDLIST_OFFSET(0xD4),
216 FIELDLIST_RESERVED(pcie_rp),
217 FIELDLIST_NAMESTR("AMPG", 1), /* Is ModPHY Power Gate active? */
218 };
219
220 acpigen_write_opregion(&opregion);
221 acpigen_write_field("PMCP", fieldlist, ARRAY_SIZE(fieldlist),
222 FIELD_DWORDACC | FIELD_NOLOCK | FIELD_PRESERVE);
223}
224
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000225static void pcie_rtd3_acpi_fill_ssdt(const struct device *dev)
226{
Tim Wawrzynczak32f883e2021-12-02 16:19:47 -0700227 static bool mutex_created = false;
228
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000229 const struct soc_intel_common_block_pcie_rtd3_config *config = config_of(dev);
230 static const char *const power_res_states[] = {"_PR0"};
231 const struct device *parent = dev->bus->dev;
232 const char *scope = acpi_device_path(parent);
233 const struct opregion opregion = OPREGION("PXCS", PCI_CONFIG, 0, 0xff);
234 const struct fieldlist fieldlist[] = {
235 FIELDLIST_OFFSET(PCH_PCIE_CFG_LSTS),
236 FIELDLIST_RESERVED(13),
237 FIELDLIST_NAMESTR(ACPI_REG_PCI_LINK_ACTIVE, 1),
238 FIELDLIST_OFFSET(PCH_PCIE_CFG_SPR),
239 FIELDLIST_RESERVED(7),
240 FIELDLIST_NAMESTR(ACPI_REG_PCI_L23_SAVE_STATE, 1),
241 FIELDLIST_OFFSET(PCH_PCIE_CFG_RPPGEN),
242 FIELDLIST_RESERVED(2),
243 FIELDLIST_NAMESTR(ACPI_REG_PCI_L23_RDY_ENTRY, 1),
244 FIELDLIST_NAMESTR(ACPI_REG_PCI_L23_RDY_DETECT, 1),
245 };
246 uint8_t pcie_rp;
247 struct acpi_dp *dsd, *pkg;
248
249 if (!is_dev_enabled(parent)) {
250 printk(BIOS_ERR, "%s: root port not enabled\n", __func__);
251 return;
252 }
253 if (!scope) {
254 printk(BIOS_ERR, "%s: root port scope not found\n", __func__);
255 return;
256 }
257 if (!config->enable_gpio.pin_count && !config->reset_gpio.pin_count) {
258 printk(BIOS_ERR, "%s: Enable and/or Reset GPIO required for %s.\n",
259 __func__, scope);
260 return;
261 }
Rizwan Qureshia9794602021-04-08 20:31:47 +0530262 if (config->srcclk_pin > CONFIG_MAX_PCIE_CLOCK_SRC) {
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000263 printk(BIOS_ERR, "%s: Invalid clock pin %u for %s.\n", __func__,
264 config->srcclk_pin, scope);
265 return;
266 }
267
Tim Wawrzynczak32f883e2021-12-02 16:19:47 -0700268 const enum pcie_rp_type rp_type = soc_get_pcie_rp_type(parent);
269
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000270 /* Read port number of root port that this device is attached to. */
271 pcie_rp = pci_read_config8(parent, PCH_PCIE_CFG_LCAP_PN);
272 if (pcie_rp == 0 || pcie_rp > CONFIG_MAX_ROOT_PORTS) {
273 printk(BIOS_ERR, "%s: Invalid root port number: %u\n", __func__, pcie_rp);
274 return;
275 }
276 /* Port number is 1-based, PMC IPC method expects 0-based. */
277 pcie_rp--;
278
279 printk(BIOS_INFO, "%s: Enable RTD3 for %s (%s)\n", scope, dev_path(parent),
280 config->desc ?: dev->chip_ops->name);
281
Tim Wawrzynczak32f883e2021-12-02 16:19:47 -0700282 /* Create a mutex for exclusive access to the PMC registers. */
283 if (rp_type == PCIE_RP_PCH && !mutex_created) {
284 acpigen_write_scope("\\_SB.PCI0");
285 acpigen_write_mutex("R3MX", 0);
286 acpigen_write_scope_end();
287 mutex_created = true;
288 }
289
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000290 /* The RTD3 power resource is added to the root port, not the device. */
291 acpigen_write_scope(scope);
292
293 if (config->desc)
294 acpigen_write_name_string("_DDN", config->desc);
295
Tim Wawrzynczak32f883e2021-12-02 16:19:47 -0700296 /* Create OpRegions for MMIO accesses. */
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000297 acpigen_write_opregion(&opregion);
298 acpigen_write_field("PXCS", fieldlist, ARRAY_SIZE(fieldlist),
299 FIELD_ANYACC | FIELD_NOLOCK | FIELD_PRESERVE);
300
Tim Wawrzynczak32f883e2021-12-02 16:19:47 -0700301 /* Create the OpRegion to access the ModPHY PG registers (PCH RPs only) */
302 if (rp_type == PCIE_RP_PCH)
303 write_modphy_opregion(pcie_rp);
304
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000305 /* ACPI Power Resource for controlling the attached device power. */
306 acpigen_write_power_res("RTD3", 0, 0, power_res_states, ARRAY_SIZE(power_res_states));
307 pcie_rtd3_acpi_method_status(pcie_rp, config);
Tim Wawrzynczak32f883e2021-12-02 16:19:47 -0700308 pcie_rtd3_acpi_method_on(pcie_rp, config, rp_type);
309 pcie_rtd3_acpi_method_off(pcie_rp, config, rp_type);
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000310 acpigen_pop_len(); /* PowerResource */
311
312 /* Indicate to the OS that device supports hotplug in D3. */
313 dsd = acpi_dp_new_table("_DSD");
314 pkg = acpi_dp_new_table(PCIE_HOTPLUG_IN_D3_UUID);
315 acpi_dp_add_integer(pkg, PCIE_HOTPLUG_IN_D3_PROPERTY, 1);
316 acpi_dp_add_package(dsd, pkg);
317
318 /* Indicate to the OS if the device provides an External facing port. */
319 if (config->is_external) {
320 pkg = acpi_dp_new_table(PCIE_EXTERNAL_PORT_UUID);
321 acpi_dp_add_integer(pkg, PCIE_EXTERNAL_PORT_PROPERTY, 1);
322 acpi_dp_add_package(dsd, pkg);
323 }
324 acpi_dp_write(dsd);
325
326 /*
327 * Check the sibling device on the root port to see if it is storage class and add the
328 * property for the OS to enable storage D3, or allow it to be enabled by config.
329 */
330 if (config->is_storage
331 || (dev->sibling && (dev->sibling->class >> 16) == PCI_BASE_CLASS_STORAGE)) {
332 acpigen_write_device(acpi_device_name(dev));
333 acpigen_write_ADR(0);
334 acpigen_write_STA(ACPI_STATUS_DEVICE_ALL_ON);
Tim Wawrzynczak61083212021-08-05 09:40:19 -0600335 acpigen_write_name_integer("_S0W", ACPI_DEVICE_SLEEP_D3_COLD);
Duncan Laurie64bc26a2020-10-10 00:15:28 +0000336
337 dsd = acpi_dp_new_table("_DSD");
338 pkg = acpi_dp_new_table(PCIE_RTD3_STORAGE_UUID);
339 acpi_dp_add_integer(pkg, PCIE_RTD3_STORAGE_PROPERTY, 1);
340 acpi_dp_add_package(dsd, pkg);
341 acpi_dp_write(dsd);
342
343 acpigen_pop_len(); /* Device */
344
345 printk(BIOS_INFO, "%s: Added StorageD3Enable property\n", scope);
346 }
347
348 acpigen_pop_len(); /* Scope */
349}
350
351static const char *pcie_rtd3_acpi_name(const struct device *dev)
352{
353 /* Attached device name must be "PXSX" for the Linux Kernel to recognize it. */
354 return "PXSX";
355}
356
357static struct device_operations pcie_rtd3_ops = {
358 .read_resources = noop_read_resources,
359 .set_resources = noop_set_resources,
360 .acpi_fill_ssdt = pcie_rtd3_acpi_fill_ssdt,
361 .acpi_name = pcie_rtd3_acpi_name,
362};
363
364static void pcie_rtd3_acpi_enable(struct device *dev)
365{
366 dev->ops = &pcie_rtd3_ops;
367}
368
369struct chip_operations soc_intel_common_block_pcie_rtd3_ops = {
370 CHIP_NAME("Intel PCIe Runtime D3")
371 .enable_dev = pcie_rtd3_acpi_enable
372};