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>
21 phys_addr_t
acpi_pci_root_get_mcfg_addr(acpi_handle handle
)
23 acpi_status status
= AE_NOT_EXIST
;
24 unsigned long long mcfg_addr
;
27 status
= acpi_evaluate_integer(handle
, METHOD_NAME__CBA
,
29 if (ACPI_FAILURE(status
))
32 return (phys_addr_t
)mcfg_addr
;
35 static acpi_status
decode_type0_hpx_record(union acpi_object
*record
,
36 struct hotplug_params
*hpx
)
39 union acpi_object
*fields
= record
->package
.elements
;
40 u32 revision
= fields
[1].integer
.value
;
44 if (record
->package
.count
!= 6)
46 for (i
= 2; i
< 6; i
++)
47 if (fields
[i
].type
!= ACPI_TYPE_INTEGER
)
49 hpx
->t0
= &hpx
->type0_data
;
50 hpx
->t0
->revision
= revision
;
51 hpx
->t0
->cache_line_size
= fields
[2].integer
.value
;
52 hpx
->t0
->latency_timer
= fields
[3].integer
.value
;
53 hpx
->t0
->enable_serr
= fields
[4].integer
.value
;
54 hpx
->t0
->enable_perr
= fields
[5].integer
.value
;
58 "%s: Type 0 Revision %d record not supported\n",
65 static acpi_status
decode_type1_hpx_record(union acpi_object
*record
,
66 struct hotplug_params
*hpx
)
69 union acpi_object
*fields
= record
->package
.elements
;
70 u32 revision
= fields
[1].integer
.value
;
74 if (record
->package
.count
!= 5)
76 for (i
= 2; i
< 5; i
++)
77 if (fields
[i
].type
!= ACPI_TYPE_INTEGER
)
79 hpx
->t1
= &hpx
->type1_data
;
80 hpx
->t1
->revision
= revision
;
81 hpx
->t1
->max_mem_read
= fields
[2].integer
.value
;
82 hpx
->t1
->avg_max_split
= fields
[3].integer
.value
;
83 hpx
->t1
->tot_max_split
= fields
[4].integer
.value
;
87 "%s: Type 1 Revision %d record not supported\n",
94 static acpi_status
decode_type2_hpx_record(union acpi_object
*record
,
95 struct hotplug_params
*hpx
)
98 union acpi_object
*fields
= record
->package
.elements
;
99 u32 revision
= fields
[1].integer
.value
;
103 if (record
->package
.count
!= 18)
105 for (i
= 2; i
< 18; i
++)
106 if (fields
[i
].type
!= ACPI_TYPE_INTEGER
)
108 hpx
->t2
= &hpx
->type2_data
;
109 hpx
->t2
->revision
= revision
;
110 hpx
->t2
->unc_err_mask_and
= fields
[2].integer
.value
;
111 hpx
->t2
->unc_err_mask_or
= fields
[3].integer
.value
;
112 hpx
->t2
->unc_err_sever_and
= fields
[4].integer
.value
;
113 hpx
->t2
->unc_err_sever_or
= fields
[5].integer
.value
;
114 hpx
->t2
->cor_err_mask_and
= fields
[6].integer
.value
;
115 hpx
->t2
->cor_err_mask_or
= fields
[7].integer
.value
;
116 hpx
->t2
->adv_err_cap_and
= fields
[8].integer
.value
;
117 hpx
->t2
->adv_err_cap_or
= fields
[9].integer
.value
;
118 hpx
->t2
->pci_exp_devctl_and
= fields
[10].integer
.value
;
119 hpx
->t2
->pci_exp_devctl_or
= fields
[11].integer
.value
;
120 hpx
->t2
->pci_exp_lnkctl_and
= fields
[12].integer
.value
;
121 hpx
->t2
->pci_exp_lnkctl_or
= fields
[13].integer
.value
;
122 hpx
->t2
->sec_unc_err_sever_and
= fields
[14].integer
.value
;
123 hpx
->t2
->sec_unc_err_sever_or
= fields
[15].integer
.value
;
124 hpx
->t2
->sec_unc_err_mask_and
= fields
[16].integer
.value
;
125 hpx
->t2
->sec_unc_err_mask_or
= fields
[17].integer
.value
;
129 "%s: Type 2 Revision %d record not supported\n",
136 static acpi_status
acpi_run_hpx(acpi_handle handle
, struct hotplug_params
*hpx
)
139 struct acpi_buffer buffer
= {ACPI_ALLOCATE_BUFFER
, NULL
};
140 union acpi_object
*package
, *record
, *fields
;
144 /* Clear the return buffer with zeros */
145 memset(hpx
, 0, sizeof(struct hotplug_params
));
147 status
= acpi_evaluate_object(handle
, "_HPX", NULL
, &buffer
);
148 if (ACPI_FAILURE(status
))
151 package
= (union acpi_object
*)buffer
.pointer
;
152 if (package
->type
!= ACPI_TYPE_PACKAGE
) {
157 for (i
= 0; i
< package
->package
.count
; i
++) {
158 record
= &package
->package
.elements
[i
];
159 if (record
->type
!= ACPI_TYPE_PACKAGE
) {
164 fields
= record
->package
.elements
;
165 if (fields
[0].type
!= ACPI_TYPE_INTEGER
||
166 fields
[1].type
!= ACPI_TYPE_INTEGER
) {
171 type
= fields
[0].integer
.value
;
174 status
= decode_type0_hpx_record(record
, hpx
);
175 if (ACPI_FAILURE(status
))
179 status
= decode_type1_hpx_record(record
, hpx
);
180 if (ACPI_FAILURE(status
))
184 status
= decode_type2_hpx_record(record
, hpx
);
185 if (ACPI_FAILURE(status
))
189 printk(KERN_ERR
"%s: Type %d record not supported\n",
196 kfree(buffer
.pointer
);
200 static acpi_status
acpi_run_hpp(acpi_handle handle
, struct hotplug_params
*hpp
)
203 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
204 union acpi_object
*package
, *fields
;
207 memset(hpp
, 0, sizeof(struct hotplug_params
));
209 status
= acpi_evaluate_object(handle
, "_HPP", NULL
, &buffer
);
210 if (ACPI_FAILURE(status
))
213 package
= (union acpi_object
*) buffer
.pointer
;
214 if (package
->type
!= ACPI_TYPE_PACKAGE
||
215 package
->package
.count
!= 4) {
220 fields
= package
->package
.elements
;
221 for (i
= 0; i
< 4; i
++) {
222 if (fields
[i
].type
!= ACPI_TYPE_INTEGER
) {
228 hpp
->t0
= &hpp
->type0_data
;
229 hpp
->t0
->revision
= 1;
230 hpp
->t0
->cache_line_size
= fields
[0].integer
.value
;
231 hpp
->t0
->latency_timer
= fields
[1].integer
.value
;
232 hpp
->t0
->enable_serr
= fields
[2].integer
.value
;
233 hpp
->t0
->enable_perr
= fields
[3].integer
.value
;
236 kfree(buffer
.pointer
);
242 * @dev - the pci_dev for which we want parameters
243 * @hpp - allocated by the caller
245 int pci_get_hp_params(struct pci_dev
*dev
, struct hotplug_params
*hpp
)
248 acpi_handle handle
, phandle
;
249 struct pci_bus
*pbus
;
252 for (pbus
= dev
->bus
; pbus
; pbus
= pbus
->parent
) {
253 handle
= acpi_pci_get_bridge_handle(pbus
);
259 * _HPP settings apply to all child buses, until another _HPP is
260 * encountered. If we don't find an _HPP for the input pci dev,
261 * look for it in the parent device scope since that would apply to
265 status
= acpi_run_hpx(handle
, hpp
);
266 if (ACPI_SUCCESS(status
))
268 status
= acpi_run_hpp(handle
, hpp
);
269 if (ACPI_SUCCESS(status
))
271 if (acpi_is_root_bridge(handle
))
273 status
= acpi_get_parent(handle
, &phandle
);
274 if (ACPI_FAILURE(status
))
280 EXPORT_SYMBOL_GPL(pci_get_hp_params
);
283 * pci_acpi_wake_bus - Root bus wakeup notification fork function.
284 * @work: Work item to handle.
286 static void pci_acpi_wake_bus(struct work_struct
*work
)
288 struct acpi_device
*adev
;
289 struct acpi_pci_root
*root
;
291 adev
= container_of(work
, struct acpi_device
, wakeup
.context
.work
);
292 root
= acpi_driver_data(adev
);
293 pci_pme_wakeup_bus(root
->bus
);
297 * pci_acpi_wake_dev - PCI device wakeup notification work function.
298 * @handle: ACPI handle of a device the notification is for.
299 * @work: Work item to handle.
301 static void pci_acpi_wake_dev(struct work_struct
*work
)
303 struct acpi_device_wakeup_context
*context
;
304 struct pci_dev
*pci_dev
;
306 context
= container_of(work
, struct acpi_device_wakeup_context
, work
);
307 pci_dev
= to_pci_dev(context
->dev
);
309 if (pci_dev
->pme_poll
)
310 pci_dev
->pme_poll
= false;
312 if (pci_dev
->current_state
== PCI_D3cold
) {
313 pci_wakeup_event(pci_dev
);
314 pm_runtime_resume(&pci_dev
->dev
);
318 /* Clear PME Status if set. */
319 if (pci_dev
->pme_support
)
320 pci_check_pme_status(pci_dev
);
322 pci_wakeup_event(pci_dev
);
323 pm_runtime_resume(&pci_dev
->dev
);
325 pci_pme_wakeup_bus(pci_dev
->subordinate
);
329 * pci_acpi_add_bus_pm_notifier - Register PM notifier for root PCI bus.
330 * @dev: PCI root bridge ACPI device.
332 acpi_status
pci_acpi_add_bus_pm_notifier(struct acpi_device
*dev
)
334 return acpi_add_pm_notifier(dev
, NULL
, pci_acpi_wake_bus
);
338 * pci_acpi_add_pm_notifier - Register PM notifier for given PCI device.
339 * @dev: ACPI device to add the notifier for.
340 * @pci_dev: PCI device to check for the PME status if an event is signaled.
342 acpi_status
pci_acpi_add_pm_notifier(struct acpi_device
*dev
,
343 struct pci_dev
*pci_dev
)
345 return acpi_add_pm_notifier(dev
, &pci_dev
->dev
, pci_acpi_wake_dev
);
349 * _SxD returns the D-state with the highest power
350 * (lowest D-state number) supported in the S-state "x".
352 * If the devices does not have a _PRW
353 * (Power Resources for Wake) supporting system wakeup from "x"
354 * then the OS is free to choose a lower power (higher number
355 * D-state) than the return value from _SxD.
357 * But if _PRW is enabled at S-state "x", the OS
358 * must not choose a power lower than _SxD --
359 * unless the device has an _SxW method specifying
360 * the lowest power (highest D-state number) the device
361 * may enter while still able to wake the system.
363 * ie. depending on global OS policy:
365 * if (_PRW at S-state x)
366 * choose from highest power _SxD to lowest power _SxW
367 * else // no _PRW at S-state x
368 * choose highest power _SxD or any lower power
371 static pci_power_t
acpi_pci_choose_state(struct pci_dev
*pdev
)
373 int acpi_state
, d_max
;
376 d_max
= ACPI_STATE_D3_HOT
;
378 d_max
= ACPI_STATE_D3_COLD
;
379 acpi_state
= acpi_pm_device_sleep_state(&pdev
->dev
, NULL
, d_max
);
381 return PCI_POWER_ERROR
;
383 switch (acpi_state
) {
390 case ACPI_STATE_D3_HOT
:
392 case ACPI_STATE_D3_COLD
:
395 return PCI_POWER_ERROR
;
398 static bool acpi_pci_power_manageable(struct pci_dev
*dev
)
400 struct acpi_device
*adev
= ACPI_COMPANION(&dev
->dev
);
401 return adev
? acpi_device_power_manageable(adev
) : false;
404 static int acpi_pci_set_power_state(struct pci_dev
*dev
, pci_power_t state
)
406 struct acpi_device
*adev
= ACPI_COMPANION(&dev
->dev
);
407 static const u8 state_conv
[] = {
408 [PCI_D0
] = ACPI_STATE_D0
,
409 [PCI_D1
] = ACPI_STATE_D1
,
410 [PCI_D2
] = ACPI_STATE_D2
,
411 [PCI_D3hot
] = ACPI_STATE_D3_COLD
,
412 [PCI_D3cold
] = ACPI_STATE_D3_COLD
,
416 /* If the ACPI device has _EJ0, ignore the device */
417 if (!adev
|| acpi_has_method(adev
->handle
, "_EJ0"))
422 if (dev_pm_qos_flags(&dev
->dev
, PM_QOS_FLAG_NO_POWER_OFF
) ==
431 error
= acpi_device_set_power(adev
, state_conv
[state
]);
435 dev_dbg(&dev
->dev
, "power state changed by ACPI to %s\n",
436 acpi_power_state_string(state_conv
[state
]));
441 static bool acpi_pci_can_wakeup(struct pci_dev
*dev
)
443 struct acpi_device
*adev
= ACPI_COMPANION(&dev
->dev
);
444 return adev
? acpi_device_can_wakeup(adev
) : false;
447 static void acpi_pci_propagate_wakeup_enable(struct pci_bus
*bus
, bool enable
)
449 while (bus
->parent
) {
450 if (!acpi_pm_device_sleep_wake(&bus
->self
->dev
, enable
))
455 /* We have reached the root bus. */
457 acpi_pm_device_sleep_wake(bus
->bridge
, enable
);
460 static int acpi_pci_sleep_wake(struct pci_dev
*dev
, bool enable
)
462 if (acpi_pci_can_wakeup(dev
))
463 return acpi_pm_device_sleep_wake(&dev
->dev
, enable
);
465 acpi_pci_propagate_wakeup_enable(dev
->bus
, enable
);
469 static void acpi_pci_propagate_run_wake(struct pci_bus
*bus
, bool enable
)
471 while (bus
->parent
) {
472 struct pci_dev
*bridge
= bus
->self
;
474 if (bridge
->pme_interrupt
)
476 if (!acpi_pm_device_run_wake(&bridge
->dev
, enable
))
481 /* We have reached the root bus. */
483 acpi_pm_device_run_wake(bus
->bridge
, enable
);
486 static int acpi_pci_run_wake(struct pci_dev
*dev
, bool enable
)
489 * Per PCI Express Base Specification Revision 2.0 section
490 * 5.3.3.2 Link Wakeup, platform support is needed for D3cold
491 * waking up to power on the main link even if there is PME
494 if (dev
->pme_interrupt
&& !dev
->runtime_d3cold
)
497 if (!acpi_pm_device_run_wake(&dev
->dev
, enable
))
500 acpi_pci_propagate_run_wake(dev
->bus
, enable
);
504 static struct pci_platform_pm_ops acpi_pci_platform_pm
= {
505 .is_manageable
= acpi_pci_power_manageable
,
506 .set_state
= acpi_pci_set_power_state
,
507 .choose_state
= acpi_pci_choose_state
,
508 .sleep_wake
= acpi_pci_sleep_wake
,
509 .run_wake
= acpi_pci_run_wake
,
512 void acpi_pci_add_bus(struct pci_bus
*bus
)
514 if (acpi_pci_disabled
|| !bus
->bridge
)
517 acpi_pci_slot_enumerate(bus
);
518 acpiphp_enumerate_slots(bus
);
521 void acpi_pci_remove_bus(struct pci_bus
*bus
)
523 if (acpi_pci_disabled
|| !bus
->bridge
)
526 acpiphp_remove_slots(bus
);
527 acpi_pci_slot_remove(bus
);
531 static struct acpi_device
*acpi_pci_find_companion(struct device
*dev
)
533 struct pci_dev
*pci_dev
= to_pci_dev(dev
);
537 check_children
= pci_is_bridge(pci_dev
);
538 /* Please ref to ACPI spec for the syntax of _ADR */
539 addr
= (PCI_SLOT(pci_dev
->devfn
) << 16) | PCI_FUNC(pci_dev
->devfn
);
540 return acpi_find_child_device(ACPI_COMPANION(dev
->parent
), addr
,
544 static void pci_acpi_setup(struct device
*dev
)
546 struct pci_dev
*pci_dev
= to_pci_dev(dev
);
547 struct acpi_device
*adev
= ACPI_COMPANION(dev
);
552 pci_acpi_add_pm_notifier(adev
, pci_dev
);
553 if (!adev
->wakeup
.flags
.valid
)
556 device_set_wakeup_capable(dev
, true);
557 acpi_pci_sleep_wake(pci_dev
, false);
558 if (adev
->wakeup
.flags
.run_wake
)
559 device_set_run_wake(dev
, true);
562 static void pci_acpi_cleanup(struct device
*dev
)
564 struct acpi_device
*adev
= ACPI_COMPANION(dev
);
569 pci_acpi_remove_pm_notifier(adev
);
570 if (adev
->wakeup
.flags
.valid
) {
571 device_set_wakeup_capable(dev
, false);
572 device_set_run_wake(dev
, false);
576 static bool pci_acpi_bus_match(struct device
*dev
)
578 return dev_is_pci(dev
);
581 static struct acpi_bus_type acpi_pci_bus
= {
583 .match
= pci_acpi_bus_match
,
584 .find_companion
= acpi_pci_find_companion
,
585 .setup
= pci_acpi_setup
,
586 .cleanup
= pci_acpi_cleanup
,
589 static int __init
acpi_pci_init(void)
593 if (acpi_gbl_FADT
.boot_flags
& ACPI_FADT_NO_MSI
) {
594 pr_info("ACPI FADT declares the system doesn't support MSI, so disable it\n");
598 if (acpi_gbl_FADT
.boot_flags
& ACPI_FADT_NO_ASPM
) {
599 pr_info("ACPI FADT declares the system doesn't support PCIe ASPM, so disable it\n");
603 ret
= register_acpi_bus_type(&acpi_pci_bus
);
607 pci_set_platform_pm(&acpi_pci_platform_pm
);
608 acpi_pci_slot_init();
613 arch_initcall(acpi_pci_init
);