ACPI / bind: Redefine acpi_preset_companion()
[deliverable/linux.git] / drivers / pci / pci-acpi.c
CommitLineData
1da177e4
LT
1/*
2 * File: pci-acpi.c
a406d9e6 3 * Purpose: Provide PCI support in ACPI
1da177e4 4 *
84df749f
DSL
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.
1da177e4
LT
8 */
9
10#include <linux/delay.h>
11#include <linux/init.h>
12#include <linux/pci.h>
13#include <linux/module.h>
5fde244d 14#include <linux/pci-aspm.h>
1da177e4 15#include <linux/pci-acpi.h>
b67ea761 16#include <linux/pm_runtime.h>
8b713a88 17#include <linux/pm_qos.h>
0f64474b 18#include "pci.h"
1da177e4 19
b67ea761
RW
20/**
21 * pci_acpi_wake_bus - Wake-up notification handler for root buses.
22 * @handle: ACPI handle of a device the notification is for.
23 * @event: Type of the signaled event.
24 * @context: PCI root bus to wake up devices on.
25 */
26static void pci_acpi_wake_bus(acpi_handle handle, u32 event, void *context)
27{
28 struct pci_bus *pci_bus = context;
29
30 if (event == ACPI_NOTIFY_DEVICE_WAKE && pci_bus)
31 pci_pme_wakeup_bus(pci_bus);
32}
33
34/**
35 * pci_acpi_wake_dev - Wake-up notification handler for PCI devices.
36 * @handle: ACPI handle of a device the notification is for.
37 * @event: Type of the signaled event.
38 * @context: PCI device object to wake up.
39 */
40static void pci_acpi_wake_dev(acpi_handle handle, u32 event, void *context)
41{
42 struct pci_dev *pci_dev = context;
43
a424948d
RW
44 if (event != ACPI_NOTIFY_DEVICE_WAKE || !pci_dev)
45 return;
46
83414515
RW
47 if (pci_dev->pme_poll)
48 pci_dev->pme_poll = false;
49
448bd857
HY
50 if (pci_dev->current_state == PCI_D3cold) {
51 pci_wakeup_event(pci_dev);
52 pm_runtime_resume(&pci_dev->dev);
53 return;
54 }
55
24ad0ef9
RW
56 /* Clear PME Status if set. */
57 if (pci_dev->pme_support)
58 pci_check_pme_status(pci_dev);
379021d5 59
24ad0ef9
RW
60 pci_wakeup_event(pci_dev);
61 pm_runtime_resume(&pci_dev->dev);
a424948d
RW
62
63 if (pci_dev->subordinate)
64 pci_pme_wakeup_bus(pci_dev->subordinate);
b67ea761
RW
65}
66
b67ea761
RW
67/**
68 * pci_acpi_add_bus_pm_notifier - Register PM notifier for given PCI bus.
69 * @dev: ACPI device to add the notifier for.
70 * @pci_bus: PCI bus to walk checking for PME status if an event is signaled.
71 */
72acpi_status pci_acpi_add_bus_pm_notifier(struct acpi_device *dev,
73 struct pci_bus *pci_bus)
74{
ec2cd81c 75 return acpi_add_pm_notifier(dev, pci_acpi_wake_bus, pci_bus);
b67ea761
RW
76}
77
78/**
79 * pci_acpi_remove_bus_pm_notifier - Unregister PCI bus PM notifier.
80 * @dev: ACPI device to remove the notifier from.
81 */
82acpi_status pci_acpi_remove_bus_pm_notifier(struct acpi_device *dev)
83{
ec2cd81c 84 return acpi_remove_pm_notifier(dev, pci_acpi_wake_bus);
b67ea761
RW
85}
86
87/**
88 * pci_acpi_add_pm_notifier - Register PM notifier for given PCI device.
89 * @dev: ACPI device to add the notifier for.
90 * @pci_dev: PCI device to check for the PME status if an event is signaled.
91 */
92acpi_status pci_acpi_add_pm_notifier(struct acpi_device *dev,
93 struct pci_dev *pci_dev)
94{
ec2cd81c 95 return acpi_add_pm_notifier(dev, pci_acpi_wake_dev, pci_dev);
b67ea761
RW
96}
97
98/**
99 * pci_acpi_remove_pm_notifier - Unregister PCI device PM notifier.
100 * @dev: ACPI device to remove the notifier from.
101 */
102acpi_status pci_acpi_remove_pm_notifier(struct acpi_device *dev)
103{
ec2cd81c 104 return acpi_remove_pm_notifier(dev, pci_acpi_wake_dev);
b67ea761
RW
105}
106
f4b57a3b
JL
107phys_addr_t acpi_pci_root_get_mcfg_addr(acpi_handle handle)
108{
109 acpi_status status = AE_NOT_EXIST;
110 unsigned long long mcfg_addr;
111
112 if (handle)
113 status = acpi_evaluate_integer(handle, METHOD_NAME__CBA,
114 NULL, &mcfg_addr);
115 if (ACPI_FAILURE(status))
116 return 0;
117
118 return (phys_addr_t)mcfg_addr;
119}
120
0f64474b
DSL
121/*
122 * _SxD returns the D-state with the highest power
123 * (lowest D-state number) supported in the S-state "x".
124 *
125 * If the devices does not have a _PRW
126 * (Power Resources for Wake) supporting system wakeup from "x"
127 * then the OS is free to choose a lower power (higher number
128 * D-state) than the return value from _SxD.
129 *
130 * But if _PRW is enabled at S-state "x", the OS
131 * must not choose a power lower than _SxD --
132 * unless the device has an _SxW method specifying
133 * the lowest power (highest D-state number) the device
134 * may enter while still able to wake the system.
135 *
136 * ie. depending on global OS policy:
137 *
138 * if (_PRW at S-state x)
139 * choose from highest power _SxD to lowest power _SxW
140 * else // no _PRW at S-state x
f7625980 141 * choose highest power _SxD or any lower power
0f64474b
DSL
142 */
143
8d2bdf49 144static pci_power_t acpi_pci_choose_state(struct pci_dev *pdev)
0f64474b 145{
448bd857 146 int acpi_state, d_max;
0f64474b 147
448bd857
HY
148 if (pdev->no_d3cold)
149 d_max = ACPI_STATE_D3_HOT;
150 else
151 d_max = ACPI_STATE_D3_COLD;
152 acpi_state = acpi_pm_device_sleep_state(&pdev->dev, NULL, d_max);
ab826ca4
SL
153 if (acpi_state < 0)
154 return PCI_POWER_ERROR;
155
156 switch (acpi_state) {
157 case ACPI_STATE_D0:
158 return PCI_D0;
159 case ACPI_STATE_D1:
160 return PCI_D1;
161 case ACPI_STATE_D2:
162 return PCI_D2;
1cc0c998 163 case ACPI_STATE_D3_HOT:
ab826ca4 164 return PCI_D3hot;
28c2103d
LM
165 case ACPI_STATE_D3_COLD:
166 return PCI_D3cold;
ab826ca4
SL
167 }
168 return PCI_POWER_ERROR;
0f64474b 169}
961d9120
RW
170
171static bool acpi_pci_power_manageable(struct pci_dev *dev)
172{
3a83f992 173 acpi_handle handle = ACPI_HANDLE(&dev->dev);
961d9120
RW
174
175 return handle ? acpi_bus_power_manageable(handle) : false;
176}
0f64474b 177
b913100d
DSL
178static int acpi_pci_set_power_state(struct pci_dev *dev, pci_power_t state)
179{
3a83f992 180 acpi_handle handle = ACPI_HANDLE(&dev->dev);
583c377f
DB
181 static const u8 state_conv[] = {
182 [PCI_D0] = ACPI_STATE_D0,
183 [PCI_D1] = ACPI_STATE_D1,
184 [PCI_D2] = ACPI_STATE_D2,
fc6504b3
RW
185 [PCI_D3hot] = ACPI_STATE_D3_COLD,
186 [PCI_D3cold] = ACPI_STATE_D3_COLD,
b913100d 187 };
44e4e66e 188 int error = -EINVAL;
b913100d 189
10b3dcae 190 /* If the ACPI device has _EJ0, ignore the device */
be07c00e 191 if (!handle || acpi_has_method(handle, "_EJ0"))
44e4e66e 192 return -ENODEV;
583c377f
DB
193
194 switch (state) {
8b713a88
RW
195 case PCI_D3cold:
196 if (dev_pm_qos_flags(&dev->dev, PM_QOS_FLAG_NO_POWER_OFF) ==
197 PM_QOS_FLAGS_ALL) {
198 error = -EBUSY;
199 break;
200 }
583c377f
DB
201 case PCI_D0:
202 case PCI_D1:
203 case PCI_D2:
204 case PCI_D3hot:
44e4e66e 205 error = acpi_bus_set_power(handle, state_conv[state]);
583c377f 206 }
44e4e66e
RW
207
208 if (!error)
d010e576 209 dev_dbg(&dev->dev, "power state changed by ACPI to %s\n",
fc6504b3 210 acpi_power_state_string(state_conv[state]));
44e4e66e
RW
211
212 return error;
b913100d
DSL
213}
214
eb9d0fe4
RW
215static bool acpi_pci_can_wakeup(struct pci_dev *dev)
216{
3a83f992 217 acpi_handle handle = ACPI_HANDLE(&dev->dev);
eb9d0fe4
RW
218
219 return handle ? acpi_bus_can_wakeup(handle) : false;
220}
221
0baed8da
RW
222static void acpi_pci_propagate_wakeup_enable(struct pci_bus *bus, bool enable)
223{
224 while (bus->parent) {
dc1a94ae 225 if (!acpi_pm_device_sleep_wake(&bus->self->dev, enable))
0baed8da
RW
226 return;
227 bus = bus->parent;
228 }
229
230 /* We have reached the root bus. */
231 if (bus->bridge)
232 acpi_pm_device_sleep_wake(bus->bridge, enable);
233}
234
eb9d0fe4
RW
235static int acpi_pci_sleep_wake(struct pci_dev *dev, bool enable)
236{
0baed8da
RW
237 if (acpi_pci_can_wakeup(dev))
238 return acpi_pm_device_sleep_wake(&dev->dev, enable);
239
dc1a94ae 240 acpi_pci_propagate_wakeup_enable(dev->bus, enable);
0baed8da 241 return 0;
eb9d0fe4
RW
242}
243
b67ea761
RW
244static void acpi_pci_propagate_run_wake(struct pci_bus *bus, bool enable)
245{
246 while (bus->parent) {
247 struct pci_dev *bridge = bus->self;
248
249 if (bridge->pme_interrupt)
250 return;
b24e5098 251 if (!acpi_pm_device_run_wake(&bridge->dev, enable))
b67ea761
RW
252 return;
253 bus = bus->parent;
254 }
255
256 /* We have reached the root bus. */
257 if (bus->bridge)
b24e5098 258 acpi_pm_device_run_wake(bus->bridge, enable);
b67ea761
RW
259}
260
261static int acpi_pci_run_wake(struct pci_dev *dev, bool enable)
262{
448bd857
HY
263 /*
264 * Per PCI Express Base Specification Revision 2.0 section
265 * 5.3.3.2 Link Wakeup, platform support is needed for D3cold
266 * waking up to power on the main link even if there is PME
267 * support for D3cold
268 */
269 if (dev->pme_interrupt && !dev->runtime_d3cold)
b67ea761
RW
270 return 0;
271
b24e5098 272 if (!acpi_pm_device_run_wake(&dev->dev, enable))
b67ea761
RW
273 return 0;
274
275 acpi_pci_propagate_run_wake(dev->bus, enable);
276 return 0;
277}
278
961d9120
RW
279static struct pci_platform_pm_ops acpi_pci_platform_pm = {
280 .is_manageable = acpi_pci_power_manageable,
281 .set_state = acpi_pci_set_power_state,
282 .choose_state = acpi_pci_choose_state,
eb9d0fe4 283 .sleep_wake = acpi_pci_sleep_wake,
b67ea761 284 .run_wake = acpi_pci_run_wake,
961d9120 285};
b913100d 286
5090d4a6
JL
287void acpi_pci_add_bus(struct pci_bus *bus)
288{
be1c9de9 289 if (acpi_pci_disabled || !bus->bridge)
5090d4a6
JL
290 return;
291
be1c9de9
RW
292 acpi_pci_slot_enumerate(bus);
293 acpiphp_enumerate_slots(bus);
5090d4a6
JL
294}
295
296void acpi_pci_remove_bus(struct pci_bus *bus)
297{
be1c9de9 298 if (acpi_pci_disabled || !bus->bridge)
5090d4a6
JL
299 return;
300
3b63aaa7 301 acpiphp_remove_slots(bus);
5c0b04e3 302 acpi_pci_slot_remove(bus);
5090d4a6
JL
303}
304
84df749f 305/* ACPI bus type */
9c273b95 306static int acpi_pci_find_device(struct device *dev, acpi_handle *handle)
84df749f 307{
60f75b8e 308 struct pci_dev *pci_dev = to_pci_dev(dev);
5ce79d20
RW
309 struct acpi_device *adev;
310 bool check_children;
60f75b8e 311 u64 addr;
84df749f 312
60f75b8e
RW
313 /*
314 * pci_is_bridge() is not suitable here, because pci_dev->subordinate
315 * is set only after acpi_pci_find_device() has been called for the
316 * given device.
317 */
5ce79d20 318 check_children = pci_dev->hdr_type == PCI_HEADER_TYPE_BRIDGE
60f75b8e 319 || pci_dev->hdr_type == PCI_HEADER_TYPE_CARDBUS;
84df749f
DSL
320 /* Please ref to ACPI spec for the syntax of _ADR */
321 addr = (PCI_SLOT(pci_dev->devfn) << 16) | PCI_FUNC(pci_dev->devfn);
5ce79d20
RW
322 adev = acpi_find_child_device(ACPI_COMPANION(dev->parent), addr,
323 check_children);
324 if (adev) {
325 *handle = adev->handle;
326 return 0;
327 }
328 return -ENODEV;
84df749f
DSL
329}
330
38a9a67a 331static void pci_acpi_setup(struct device *dev)
d2e5f0c1 332{
d2e5f0c1 333 struct pci_dev *pci_dev = to_pci_dev(dev);
38a9a67a
RW
334 acpi_handle handle = ACPI_HANDLE(dev);
335 struct acpi_device *adev;
38a9a67a 336
38a9a67a 337 if (acpi_bus_get_device(handle, &adev) || !adev->wakeup.flags.valid)
d2e5f0c1
RW
338 return;
339
340 device_set_wakeup_capable(dev, true);
341 acpi_pci_sleep_wake(pci_dev, false);
342
343 pci_acpi_add_pm_notifier(adev, pci_dev);
344 if (adev->wakeup.flags.run_wake)
345 device_set_run_wake(dev, true);
346}
347
38a9a67a 348static void pci_acpi_cleanup(struct device *dev)
d2e5f0c1 349{
38a9a67a
RW
350 acpi_handle handle = ACPI_HANDLE(dev);
351 struct acpi_device *adev;
d2e5f0c1 352
38a9a67a 353 if (!acpi_bus_get_device(handle, &adev) && adev->wakeup.flags.valid) {
d2e5f0c1
RW
354 device_set_wakeup_capable(dev, false);
355 device_set_run_wake(dev, false);
356 pci_acpi_remove_pm_notifier(adev);
357 }
358}
359
53540098
RW
360static bool pci_acpi_bus_match(struct device *dev)
361{
362 return dev->bus == &pci_bus_type;
363}
364
9c273b95 365static struct acpi_bus_type acpi_pci_bus = {
53540098
RW
366 .name = "PCI",
367 .match = pci_acpi_bus_match,
9c273b95 368 .find_device = acpi_pci_find_device,
38a9a67a
RW
369 .setup = pci_acpi_setup,
370 .cleanup = pci_acpi_cleanup,
84df749f
DSL
371};
372
9c273b95 373static int __init acpi_pci_init(void)
84df749f
DSL
374{
375 int ret;
376
993958fe 377 if (acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_MSI) {
e7d45152 378 pr_info("ACPI FADT declares the system doesn't support MSI, so disable it\n");
f8993aff
SL
379 pci_no_msi();
380 }
5fde244d 381
993958fe 382 if (acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_ASPM) {
e7d45152 383 pr_info("ACPI FADT declares the system doesn't support PCIe ASPM, so disable it\n");
5fde244d
SL
384 pcie_no_aspm();
385 }
386
9c273b95 387 ret = register_acpi_bus_type(&acpi_pci_bus);
84df749f
DSL
388 if (ret)
389 return 0;
5c0b04e3 390
961d9120 391 pci_set_platform_pm(&acpi_pci_platform_pm);
5c0b04e3 392 acpi_pci_slot_init();
3b63aaa7 393 acpiphp_init();
5c0b04e3 394
84df749f
DSL
395 return 0;
396}
9c273b95 397arch_initcall(acpi_pci_init);
This page took 0.662548 seconds and 5 git commands to generate.