3 * Purpose: Provide PCI support in ACPI
5 * Copyright (C) 2005 David Shaohua Li <shaohua.li@intel.com>
6 * Copyright (C) 2004 Tom Long Nguyen <tom.l.nguyen@intel.com>
7 * Copyright (C) 2004 Intel Corp.
10 #include <linux/delay.h>
11 #include <linux/init.h>
12 #include <linux/pci.h>
13 #include <linux/pci_hotplug.h>
14 #include <linux/module.h>
15 #include <linux/pci-aspm.h>
16 #include <linux/pci-acpi.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/pm_qos.h>
22 * The UUID is defined in the PCI Firmware Specification available here:
23 * https://www.pcisig.com/members/downloads/pcifw_r3_1_13Dec10.pdf
25 const u8 pci_acpi_dsm_uuid[] = {
26 0xd0, 0x37, 0xc9, 0xe5, 0x53, 0x35, 0x7a, 0x4d,
27 0x91, 0x17, 0xea, 0x4d, 0x19, 0xc3, 0x43, 0x4d
30 phys_addr_t acpi_pci_root_get_mcfg_addr(acpi_handle handle)
32 acpi_status status = AE_NOT_EXIST;
33 unsigned long long mcfg_addr;
36 status = acpi_evaluate_integer(handle, METHOD_NAME__CBA,
38 if (ACPI_FAILURE(status))
41 return (phys_addr_t)mcfg_addr;
44 static acpi_status decode_type0_hpx_record(union acpi_object *record,
45 struct hotplug_params *hpx)
48 union acpi_object *fields = record->package.elements;
49 u32 revision = fields[1].integer.value;
53 if (record->package.count != 6)
55 for (i = 2; i < 6; i++)
56 if (fields[i].type != ACPI_TYPE_INTEGER)
58 hpx->t0 = &hpx->type0_data;
59 hpx->t0->revision = revision;
60 hpx->t0->cache_line_size = fields[2].integer.value;
61 hpx->t0->latency_timer = fields[3].integer.value;
62 hpx->t0->enable_serr = fields[4].integer.value;
63 hpx->t0->enable_perr = fields[5].integer.value;
67 "%s: Type 0 Revision %d record not supported\n",
74 static acpi_status decode_type1_hpx_record(union acpi_object *record,
75 struct hotplug_params *hpx)
78 union acpi_object *fields = record->package.elements;
79 u32 revision = fields[1].integer.value;
83 if (record->package.count != 5)
85 for (i = 2; i < 5; i++)
86 if (fields[i].type != ACPI_TYPE_INTEGER)
88 hpx->t1 = &hpx->type1_data;
89 hpx->t1->revision = revision;
90 hpx->t1->max_mem_read = fields[2].integer.value;
91 hpx->t1->avg_max_split = fields[3].integer.value;
92 hpx->t1->tot_max_split = fields[4].integer.value;
96 "%s: Type 1 Revision %d record not supported\n",
103 static acpi_status decode_type2_hpx_record(union acpi_object *record,
104 struct hotplug_params *hpx)
107 union acpi_object *fields = record->package.elements;
108 u32 revision = fields[1].integer.value;
112 if (record->package.count != 18)
114 for (i = 2; i < 18; i++)
115 if (fields[i].type != ACPI_TYPE_INTEGER)
117 hpx->t2 = &hpx->type2_data;
118 hpx->t2->revision = revision;
119 hpx->t2->unc_err_mask_and = fields[2].integer.value;
120 hpx->t2->unc_err_mask_or = fields[3].integer.value;
121 hpx->t2->unc_err_sever_and = fields[4].integer.value;
122 hpx->t2->unc_err_sever_or = fields[5].integer.value;
123 hpx->t2->cor_err_mask_and = fields[6].integer.value;
124 hpx->t2->cor_err_mask_or = fields[7].integer.value;
125 hpx->t2->adv_err_cap_and = fields[8].integer.value;
126 hpx->t2->adv_err_cap_or = fields[9].integer.value;
127 hpx->t2->pci_exp_devctl_and = fields[10].integer.value;
128 hpx->t2->pci_exp_devctl_or = fields[11].integer.value;
129 hpx->t2->pci_exp_lnkctl_and = fields[12].integer.value;
130 hpx->t2->pci_exp_lnkctl_or = fields[13].integer.value;
131 hpx->t2->sec_unc_err_sever_and = fields[14].integer.value;
132 hpx->t2->sec_unc_err_sever_or = fields[15].integer.value;
133 hpx->t2->sec_unc_err_mask_and = fields[16].integer.value;
134 hpx->t2->sec_unc_err_mask_or = fields[17].integer.value;
138 "%s: Type 2 Revision %d record not supported\n",
145 static acpi_status acpi_run_hpx(acpi_handle handle, struct hotplug_params *hpx)
148 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
149 union acpi_object *package, *record, *fields;
153 /* Clear the return buffer with zeros */
154 memset(hpx, 0, sizeof(struct hotplug_params));
156 status = acpi_evaluate_object(handle, "_HPX", NULL, &buffer);
157 if (ACPI_FAILURE(status))
160 package = (union acpi_object *)buffer.pointer;
161 if (package->type != ACPI_TYPE_PACKAGE) {
166 for (i = 0; i < package->package.count; i++) {
167 record = &package->package.elements[i];
168 if (record->type != ACPI_TYPE_PACKAGE) {
173 fields = record->package.elements;
174 if (fields[0].type != ACPI_TYPE_INTEGER ||
175 fields[1].type != ACPI_TYPE_INTEGER) {
180 type = fields[0].integer.value;
183 status = decode_type0_hpx_record(record, hpx);
184 if (ACPI_FAILURE(status))
188 status = decode_type1_hpx_record(record, hpx);
189 if (ACPI_FAILURE(status))
193 status = decode_type2_hpx_record(record, hpx);
194 if (ACPI_FAILURE(status))
198 printk(KERN_ERR "%s: Type %d record not supported\n",
205 kfree(buffer.pointer);
209 static acpi_status acpi_run_hpp(acpi_handle handle, struct hotplug_params *hpp)
212 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
213 union acpi_object *package, *fields;
216 memset(hpp, 0, sizeof(struct hotplug_params));
218 status = acpi_evaluate_object(handle, "_HPP", NULL, &buffer);
219 if (ACPI_FAILURE(status))
222 package = (union acpi_object *) buffer.pointer;
223 if (package->type != ACPI_TYPE_PACKAGE ||
224 package->package.count != 4) {
229 fields = package->package.elements;
230 for (i = 0; i < 4; i++) {
231 if (fields[i].type != ACPI_TYPE_INTEGER) {
237 hpp->t0 = &hpp->type0_data;
238 hpp->t0->revision = 1;
239 hpp->t0->cache_line_size = fields[0].integer.value;
240 hpp->t0->latency_timer = fields[1].integer.value;
241 hpp->t0->enable_serr = fields[2].integer.value;
242 hpp->t0->enable_perr = fields[3].integer.value;
245 kfree(buffer.pointer);
251 * @dev - the pci_dev for which we want parameters
252 * @hpp - allocated by the caller
254 int pci_get_hp_params(struct pci_dev *dev, struct hotplug_params *hpp)
257 acpi_handle handle, phandle;
258 struct pci_bus *pbus;
261 for (pbus = dev->bus; pbus; pbus = pbus->parent) {
262 handle = acpi_pci_get_bridge_handle(pbus);
268 * _HPP settings apply to all child buses, until another _HPP is
269 * encountered. If we don't find an _HPP for the input pci dev,
270 * look for it in the parent device scope since that would apply to
274 status = acpi_run_hpx(handle, hpp);
275 if (ACPI_SUCCESS(status))
277 status = acpi_run_hpp(handle, hpp);
278 if (ACPI_SUCCESS(status))
280 if (acpi_is_root_bridge(handle))
282 status = acpi_get_parent(handle, &phandle);
283 if (ACPI_FAILURE(status))
289 EXPORT_SYMBOL_GPL(pci_get_hp_params);
292 * pci_acpi_wake_bus - Root bus wakeup notification fork function.
293 * @work: Work item to handle.
295 static void pci_acpi_wake_bus(struct work_struct *work)
297 struct acpi_device *adev;
298 struct acpi_pci_root *root;
300 adev = container_of(work, struct acpi_device, wakeup.context.work);
301 root = acpi_driver_data(adev);
302 pci_pme_wakeup_bus(root->bus);
306 * pci_acpi_wake_dev - PCI device wakeup notification work function.
307 * @handle: ACPI handle of a device the notification is for.
308 * @work: Work item to handle.
310 static void pci_acpi_wake_dev(struct work_struct *work)
312 struct acpi_device_wakeup_context *context;
313 struct pci_dev *pci_dev;
315 context = container_of(work, struct acpi_device_wakeup_context, work);
316 pci_dev = to_pci_dev(context->dev);
318 if (pci_dev->pme_poll)
319 pci_dev->pme_poll = false;
321 if (pci_dev->current_state == PCI_D3cold) {
322 pci_wakeup_event(pci_dev);
323 pm_runtime_resume(&pci_dev->dev);
327 /* Clear PME Status if set. */
328 if (pci_dev->pme_support)
329 pci_check_pme_status(pci_dev);
331 pci_wakeup_event(pci_dev);
332 pm_runtime_resume(&pci_dev->dev);
334 pci_pme_wakeup_bus(pci_dev->subordinate);
338 * pci_acpi_add_bus_pm_notifier - Register PM notifier for root PCI bus.
339 * @dev: PCI root bridge ACPI device.
341 acpi_status pci_acpi_add_bus_pm_notifier(struct acpi_device *dev)
343 return acpi_add_pm_notifier(dev, NULL, pci_acpi_wake_bus);
347 * pci_acpi_add_pm_notifier - Register PM notifier for given PCI device.
348 * @dev: ACPI device to add the notifier for.
349 * @pci_dev: PCI device to check for the PME status if an event is signaled.
351 acpi_status pci_acpi_add_pm_notifier(struct acpi_device *dev,
352 struct pci_dev *pci_dev)
354 return acpi_add_pm_notifier(dev, &pci_dev->dev, pci_acpi_wake_dev);
358 * _SxD returns the D-state with the highest power
359 * (lowest D-state number) supported in the S-state "x".
361 * If the devices does not have a _PRW
362 * (Power Resources for Wake) supporting system wakeup from "x"
363 * then the OS is free to choose a lower power (higher number
364 * D-state) than the return value from _SxD.
366 * But if _PRW is enabled at S-state "x", the OS
367 * must not choose a power lower than _SxD --
368 * unless the device has an _SxW method specifying
369 * the lowest power (highest D-state number) the device
370 * may enter while still able to wake the system.
372 * ie. depending on global OS policy:
374 * if (_PRW at S-state x)
375 * choose from highest power _SxD to lowest power _SxW
376 * else // no _PRW at S-state x
377 * choose highest power _SxD or any lower power
380 static pci_power_t acpi_pci_choose_state(struct pci_dev *pdev)
382 int acpi_state, d_max;
385 d_max = ACPI_STATE_D3_HOT;
387 d_max = ACPI_STATE_D3_COLD;
388 acpi_state = acpi_pm_device_sleep_state(&pdev->dev, NULL, d_max);
390 return PCI_POWER_ERROR;
392 switch (acpi_state) {
399 case ACPI_STATE_D3_HOT:
401 case ACPI_STATE_D3_COLD:
404 return PCI_POWER_ERROR;
407 static bool acpi_pci_power_manageable(struct pci_dev *dev)
409 struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
410 return adev ? acpi_device_power_manageable(adev) : false;
413 static int acpi_pci_set_power_state(struct pci_dev *dev, pci_power_t state)
415 struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
416 static const u8 state_conv[] = {
417 [PCI_D0] = ACPI_STATE_D0,
418 [PCI_D1] = ACPI_STATE_D1,
419 [PCI_D2] = ACPI_STATE_D2,
420 [PCI_D3hot] = ACPI_STATE_D3_COLD,
421 [PCI_D3cold] = ACPI_STATE_D3_COLD,
425 /* If the ACPI device has _EJ0, ignore the device */
426 if (!adev || acpi_has_method(adev->handle, "_EJ0"))
431 if (dev_pm_qos_flags(&dev->dev, PM_QOS_FLAG_NO_POWER_OFF) ==
440 error = acpi_device_set_power(adev, state_conv[state]);
444 dev_dbg(&dev->dev, "power state changed by ACPI to %s\n",
445 acpi_power_state_string(state_conv[state]));
450 static bool acpi_pci_can_wakeup(struct pci_dev *dev)
452 struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
453 return adev ? acpi_device_can_wakeup(adev) : false;
456 static void acpi_pci_propagate_wakeup_enable(struct pci_bus *bus, bool enable)
458 while (bus->parent) {
459 if (!acpi_pm_device_sleep_wake(&bus->self->dev, enable))
464 /* We have reached the root bus. */
466 acpi_pm_device_sleep_wake(bus->bridge, enable);
469 static int acpi_pci_sleep_wake(struct pci_dev *dev, bool enable)
471 if (acpi_pci_can_wakeup(dev))
472 return acpi_pm_device_sleep_wake(&dev->dev, enable);
474 acpi_pci_propagate_wakeup_enable(dev->bus, enable);
478 static void acpi_pci_propagate_run_wake(struct pci_bus *bus, bool enable)
480 while (bus->parent) {
481 struct pci_dev *bridge = bus->self;
483 if (bridge->pme_interrupt)
485 if (!acpi_pm_device_run_wake(&bridge->dev, enable))
490 /* We have reached the root bus. */
492 acpi_pm_device_run_wake(bus->bridge, enable);
495 static int acpi_pci_run_wake(struct pci_dev *dev, bool enable)
498 * Per PCI Express Base Specification Revision 2.0 section
499 * 5.3.3.2 Link Wakeup, platform support is needed for D3cold
500 * waking up to power on the main link even if there is PME
503 if (dev->pme_interrupt && !dev->runtime_d3cold)
506 if (!acpi_pm_device_run_wake(&dev->dev, enable))
509 acpi_pci_propagate_run_wake(dev->bus, enable);
513 static bool acpi_pci_need_resume(struct pci_dev *dev)
515 struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
517 if (!adev || !acpi_device_power_manageable(adev))
520 if (device_may_wakeup(&dev->dev) != !!adev->wakeup.prepare_count)
523 if (acpi_target_system_state() == ACPI_STATE_S0)
526 return !!adev->power.flags.dsw_present;
529 static struct pci_platform_pm_ops acpi_pci_platform_pm = {
530 .is_manageable = acpi_pci_power_manageable,
531 .set_state = acpi_pci_set_power_state,
532 .choose_state = acpi_pci_choose_state,
533 .sleep_wake = acpi_pci_sleep_wake,
534 .run_wake = acpi_pci_run_wake,
535 .need_resume = acpi_pci_need_resume,
538 void acpi_pci_add_bus(struct pci_bus *bus)
540 if (acpi_pci_disabled || !bus->bridge)
543 acpi_pci_slot_enumerate(bus);
544 acpiphp_enumerate_slots(bus);
547 void acpi_pci_remove_bus(struct pci_bus *bus)
549 if (acpi_pci_disabled || !bus->bridge)
552 acpiphp_remove_slots(bus);
553 acpi_pci_slot_remove(bus);
557 static struct acpi_device *acpi_pci_find_companion(struct device *dev)
559 struct pci_dev *pci_dev = to_pci_dev(dev);
563 check_children = pci_is_bridge(pci_dev);
564 /* Please ref to ACPI spec for the syntax of _ADR */
565 addr = (PCI_SLOT(pci_dev->devfn) << 16) | PCI_FUNC(pci_dev->devfn);
566 return acpi_find_child_device(ACPI_COMPANION(dev->parent), addr,
570 static void pci_acpi_setup(struct device *dev)
572 struct pci_dev *pci_dev = to_pci_dev(dev);
573 struct acpi_device *adev = ACPI_COMPANION(dev);
578 pci_acpi_add_pm_notifier(adev, pci_dev);
579 if (!adev->wakeup.flags.valid)
582 device_set_wakeup_capable(dev, true);
583 acpi_pci_sleep_wake(pci_dev, false);
584 if (adev->wakeup.flags.run_wake)
585 device_set_run_wake(dev, true);
588 static void pci_acpi_cleanup(struct device *dev)
590 struct acpi_device *adev = ACPI_COMPANION(dev);
595 pci_acpi_remove_pm_notifier(adev);
596 if (adev->wakeup.flags.valid) {
597 device_set_wakeup_capable(dev, false);
598 device_set_run_wake(dev, false);
602 static bool pci_acpi_bus_match(struct device *dev)
604 return dev_is_pci(dev);
607 static struct acpi_bus_type acpi_pci_bus = {
609 .match = pci_acpi_bus_match,
610 .find_companion = acpi_pci_find_companion,
611 .setup = pci_acpi_setup,
612 .cleanup = pci_acpi_cleanup,
615 static int __init acpi_pci_init(void)
619 if (acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_MSI) {
620 pr_info("ACPI FADT declares the system doesn't support MSI, so disable it\n");
624 if (acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_ASPM) {
625 pr_info("ACPI FADT declares the system doesn't support PCIe ASPM, so disable it\n");
629 ret = register_acpi_bus_type(&acpi_pci_bus);
633 pci_set_platform_pm(&acpi_pci_platform_pm);
634 acpi_pci_slot_init();
639 arch_initcall(acpi_pci_init);