blob: e514d50eaddcac4352596bc8142745a77649d45f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * File: pci-acpi.c
Len Browna406d9e2005-03-23 16:16:03 -05003 * Purpose: Provide PCI support in ACPI
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
David Shaohua Li84df749f2005-03-18 18:53:36 -05005 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
10#include <linux/delay.h>
11#include <linux/init.h>
12#include <linux/pci.h>
Bjorn Helgaas9ce90ea2014-09-12 15:23:14 -060013#include <linux/pci_hotplug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
Shaohua Li5fde2442008-07-23 10:32:24 +080015#include <linux/pci-aspm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/pci-acpi.h>
Rafael J. Wysockib67ea762010-02-17 23:44:09 +010017#include <linux/pm_runtime.h>
Rafael J. Wysocki8b713a82012-10-24 02:08:38 +020018#include <linux/pm_qos.h>
David Shaohua Li0f644742005-03-19 00:15:48 -050019#include "pci.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Rafael J. Wysockib67ea762010-02-17 23:44:09 +010021/**
Rafael J. Wysockic0725302014-07-23 01:00:45 +020022 * pci_acpi_wake_bus - Root bus wakeup notification fork function.
23 * @work: Work item to handle.
Rafael J. Wysockib67ea762010-02-17 23:44:09 +010024 */
Rafael J. Wysockic0725302014-07-23 01:00:45 +020025static void pci_acpi_wake_bus(struct work_struct *work)
Rafael J. Wysockib67ea762010-02-17 23:44:09 +010026{
Rafael J. Wysockic0725302014-07-23 01:00:45 +020027 struct acpi_device *adev;
28 struct acpi_pci_root *root;
Rafael J. Wysockib67ea762010-02-17 23:44:09 +010029
Rafael J. Wysockic0725302014-07-23 01:00:45 +020030 adev = container_of(work, struct acpi_device, wakeup.context.work);
31 root = acpi_driver_data(adev);
32 pci_pme_wakeup_bus(root->bus);
Rafael J. Wysockib67ea762010-02-17 23:44:09 +010033}
34
35/**
Rafael J. Wysockic0725302014-07-23 01:00:45 +020036 * pci_acpi_wake_dev - PCI device wakeup notification work function.
Rafael J. Wysockib67ea762010-02-17 23:44:09 +010037 * @handle: ACPI handle of a device the notification is for.
Rafael J. Wysockic0725302014-07-23 01:00:45 +020038 * @work: Work item to handle.
Rafael J. Wysockib67ea762010-02-17 23:44:09 +010039 */
Rafael J. Wysockic0725302014-07-23 01:00:45 +020040static void pci_acpi_wake_dev(struct work_struct *work)
Rafael J. Wysockib67ea762010-02-17 23:44:09 +010041{
Rafael J. Wysockic0725302014-07-23 01:00:45 +020042 struct acpi_device_wakeup_context *context;
43 struct pci_dev *pci_dev;
Rafael J. Wysockib67ea762010-02-17 23:44:09 +010044
Rafael J. Wysockic0725302014-07-23 01:00:45 +020045 context = container_of(work, struct acpi_device_wakeup_context, work);
46 pci_dev = to_pci_dev(context->dev);
Rafael J. Wysockia4249482011-11-06 22:21:46 +010047
Rafael J. Wysocki83414512013-09-14 03:38:20 +020048 if (pci_dev->pme_poll)
49 pci_dev->pme_poll = false;
50
Huang Ying448bd852012-06-23 10:23:51 +080051 if (pci_dev->current_state == PCI_D3cold) {
52 pci_wakeup_event(pci_dev);
53 pm_runtime_resume(&pci_dev->dev);
54 return;
55 }
56
Rafael J. Wysocki24ad0ef2013-03-28 11:07:29 +000057 /* Clear PME Status if set. */
58 if (pci_dev->pme_support)
59 pci_check_pme_status(pci_dev);
Rafael J. Wysocki379021d2011-10-03 23:16:33 +020060
Rafael J. Wysocki24ad0ef2013-03-28 11:07:29 +000061 pci_wakeup_event(pci_dev);
62 pm_runtime_resume(&pci_dev->dev);
Rafael J. Wysockia4249482011-11-06 22:21:46 +010063
64 if (pci_dev->subordinate)
65 pci_pme_wakeup_bus(pci_dev->subordinate);
Rafael J. Wysockib67ea762010-02-17 23:44:09 +010066}
67
68/**
Rafael J. Wysockic0725302014-07-23 01:00:45 +020069 * pci_acpi_add_bus_pm_notifier - Register PM notifier for root PCI bus.
70 * @dev: PCI root bridge ACPI device.
Rafael J. Wysockib67ea762010-02-17 23:44:09 +010071 */
Rafael J. Wysockic0725302014-07-23 01:00:45 +020072acpi_status pci_acpi_add_bus_pm_notifier(struct acpi_device *dev)
Rafael J. Wysockib67ea762010-02-17 23:44:09 +010073{
Rafael J. Wysockic0725302014-07-23 01:00:45 +020074 return acpi_add_pm_notifier(dev, NULL, pci_acpi_wake_bus);
Rafael J. Wysockib67ea762010-02-17 23:44:09 +010075}
76
77/**
78 * pci_acpi_add_pm_notifier - Register PM notifier for given PCI device.
79 * @dev: ACPI device to add the notifier for.
80 * @pci_dev: PCI device to check for the PME status if an event is signaled.
81 */
82acpi_status pci_acpi_add_pm_notifier(struct acpi_device *dev,
83 struct pci_dev *pci_dev)
84{
Rafael J. Wysockic0725302014-07-23 01:00:45 +020085 return acpi_add_pm_notifier(dev, &pci_dev->dev, pci_acpi_wake_dev);
Rafael J. Wysockib67ea762010-02-17 23:44:09 +010086}
87
Jiang Liuf4b57a32012-06-22 14:55:16 +080088phys_addr_t acpi_pci_root_get_mcfg_addr(acpi_handle handle)
89{
90 acpi_status status = AE_NOT_EXIST;
91 unsigned long long mcfg_addr;
92
93 if (handle)
94 status = acpi_evaluate_integer(handle, METHOD_NAME__CBA,
95 NULL, &mcfg_addr);
96 if (ACPI_FAILURE(status))
97 return 0;
98
99 return (phys_addr_t)mcfg_addr;
100}
101
Bjorn Helgaasabbfec32014-09-12 15:29:55 -0600102static acpi_status decode_type0_hpx_record(union acpi_object *record,
103 struct hotplug_params *hpx)
Bjorn Helgaas9ce90ea2014-09-12 15:23:14 -0600104{
105 int i;
106 union acpi_object *fields = record->package.elements;
107 u32 revision = fields[1].integer.value;
108
109 switch (revision) {
110 case 1:
111 if (record->package.count != 6)
112 return AE_ERROR;
113 for (i = 2; i < 6; i++)
114 if (fields[i].type != ACPI_TYPE_INTEGER)
115 return AE_ERROR;
116 hpx->t0 = &hpx->type0_data;
117 hpx->t0->revision = revision;
118 hpx->t0->cache_line_size = fields[2].integer.value;
119 hpx->t0->latency_timer = fields[3].integer.value;
120 hpx->t0->enable_serr = fields[4].integer.value;
121 hpx->t0->enable_perr = fields[5].integer.value;
122 break;
123 default:
124 printk(KERN_WARNING
125 "%s: Type 0 Revision %d record not supported\n",
126 __func__, revision);
127 return AE_ERROR;
128 }
129 return AE_OK;
130}
131
Bjorn Helgaasabbfec32014-09-12 15:29:55 -0600132static acpi_status decode_type1_hpx_record(union acpi_object *record,
133 struct hotplug_params *hpx)
Bjorn Helgaas9ce90ea2014-09-12 15:23:14 -0600134{
135 int i;
136 union acpi_object *fields = record->package.elements;
137 u32 revision = fields[1].integer.value;
138
139 switch (revision) {
140 case 1:
141 if (record->package.count != 5)
142 return AE_ERROR;
143 for (i = 2; i < 5; i++)
144 if (fields[i].type != ACPI_TYPE_INTEGER)
145 return AE_ERROR;
146 hpx->t1 = &hpx->type1_data;
147 hpx->t1->revision = revision;
148 hpx->t1->max_mem_read = fields[2].integer.value;
149 hpx->t1->avg_max_split = fields[3].integer.value;
150 hpx->t1->tot_max_split = fields[4].integer.value;
151 break;
152 default:
153 printk(KERN_WARNING
154 "%s: Type 1 Revision %d record not supported\n",
155 __func__, revision);
156 return AE_ERROR;
157 }
158 return AE_OK;
159}
160
Bjorn Helgaasabbfec32014-09-12 15:29:55 -0600161static acpi_status decode_type2_hpx_record(union acpi_object *record,
162 struct hotplug_params *hpx)
Bjorn Helgaas9ce90ea2014-09-12 15:23:14 -0600163{
164 int i;
165 union acpi_object *fields = record->package.elements;
166 u32 revision = fields[1].integer.value;
167
168 switch (revision) {
169 case 1:
170 if (record->package.count != 18)
171 return AE_ERROR;
172 for (i = 2; i < 18; i++)
173 if (fields[i].type != ACPI_TYPE_INTEGER)
174 return AE_ERROR;
175 hpx->t2 = &hpx->type2_data;
176 hpx->t2->revision = revision;
177 hpx->t2->unc_err_mask_and = fields[2].integer.value;
178 hpx->t2->unc_err_mask_or = fields[3].integer.value;
179 hpx->t2->unc_err_sever_and = fields[4].integer.value;
180 hpx->t2->unc_err_sever_or = fields[5].integer.value;
181 hpx->t2->cor_err_mask_and = fields[6].integer.value;
182 hpx->t2->cor_err_mask_or = fields[7].integer.value;
183 hpx->t2->adv_err_cap_and = fields[8].integer.value;
184 hpx->t2->adv_err_cap_or = fields[9].integer.value;
185 hpx->t2->pci_exp_devctl_and = fields[10].integer.value;
186 hpx->t2->pci_exp_devctl_or = fields[11].integer.value;
187 hpx->t2->pci_exp_lnkctl_and = fields[12].integer.value;
188 hpx->t2->pci_exp_lnkctl_or = fields[13].integer.value;
189 hpx->t2->sec_unc_err_sever_and = fields[14].integer.value;
190 hpx->t2->sec_unc_err_sever_or = fields[15].integer.value;
191 hpx->t2->sec_unc_err_mask_and = fields[16].integer.value;
192 hpx->t2->sec_unc_err_mask_or = fields[17].integer.value;
193 break;
194 default:
195 printk(KERN_WARNING
196 "%s: Type 2 Revision %d record not supported\n",
197 __func__, revision);
198 return AE_ERROR;
199 }
200 return AE_OK;
201}
202
Bjorn Helgaasabbfec32014-09-12 15:29:55 -0600203static acpi_status acpi_run_hpx(acpi_handle handle, struct hotplug_params *hpx)
Bjorn Helgaas9ce90ea2014-09-12 15:23:14 -0600204{
205 acpi_status status;
206 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
207 union acpi_object *package, *record, *fields;
208 u32 type;
209 int i;
210
211 /* Clear the return buffer with zeros */
212 memset(hpx, 0, sizeof(struct hotplug_params));
213
214 status = acpi_evaluate_object(handle, "_HPX", NULL, &buffer);
215 if (ACPI_FAILURE(status))
216 return status;
217
218 package = (union acpi_object *)buffer.pointer;
219 if (package->type != ACPI_TYPE_PACKAGE) {
220 status = AE_ERROR;
221 goto exit;
222 }
223
224 for (i = 0; i < package->package.count; i++) {
225 record = &package->package.elements[i];
226 if (record->type != ACPI_TYPE_PACKAGE) {
227 status = AE_ERROR;
228 goto exit;
229 }
230
231 fields = record->package.elements;
232 if (fields[0].type != ACPI_TYPE_INTEGER ||
233 fields[1].type != ACPI_TYPE_INTEGER) {
234 status = AE_ERROR;
235 goto exit;
236 }
237
238 type = fields[0].integer.value;
239 switch (type) {
240 case 0:
241 status = decode_type0_hpx_record(record, hpx);
242 if (ACPI_FAILURE(status))
243 goto exit;
244 break;
245 case 1:
246 status = decode_type1_hpx_record(record, hpx);
247 if (ACPI_FAILURE(status))
248 goto exit;
249 break;
250 case 2:
251 status = decode_type2_hpx_record(record, hpx);
252 if (ACPI_FAILURE(status))
253 goto exit;
254 break;
255 default:
256 printk(KERN_ERR "%s: Type %d record not supported\n",
257 __func__, type);
258 status = AE_ERROR;
259 goto exit;
260 }
261 }
262 exit:
263 kfree(buffer.pointer);
264 return status;
265}
266
Bjorn Helgaasabbfec32014-09-12 15:29:55 -0600267static acpi_status acpi_run_hpp(acpi_handle handle, struct hotplug_params *hpp)
Bjorn Helgaas9ce90ea2014-09-12 15:23:14 -0600268{
269 acpi_status status;
270 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
271 union acpi_object *package, *fields;
272 int i;
273
274 memset(hpp, 0, sizeof(struct hotplug_params));
275
276 status = acpi_evaluate_object(handle, "_HPP", NULL, &buffer);
277 if (ACPI_FAILURE(status))
278 return status;
279
280 package = (union acpi_object *) buffer.pointer;
281 if (package->type != ACPI_TYPE_PACKAGE ||
282 package->package.count != 4) {
283 status = AE_ERROR;
284 goto exit;
285 }
286
287 fields = package->package.elements;
288 for (i = 0; i < 4; i++) {
289 if (fields[i].type != ACPI_TYPE_INTEGER) {
290 status = AE_ERROR;
291 goto exit;
292 }
293 }
294
295 hpp->t0 = &hpp->type0_data;
296 hpp->t0->revision = 1;
297 hpp->t0->cache_line_size = fields[0].integer.value;
298 hpp->t0->latency_timer = fields[1].integer.value;
299 hpp->t0->enable_serr = fields[2].integer.value;
300 hpp->t0->enable_perr = fields[3].integer.value;
301
302exit:
303 kfree(buffer.pointer);
304 return status;
305}
306
307/* pci_get_hp_params
308 *
309 * @dev - the pci_dev for which we want parameters
310 * @hpp - allocated by the caller
311 */
312int pci_get_hp_params(struct pci_dev *dev, struct hotplug_params *hpp)
313{
314 acpi_status status;
315 acpi_handle handle, phandle;
316 struct pci_bus *pbus;
317
318 handle = NULL;
319 for (pbus = dev->bus; pbus; pbus = pbus->parent) {
320 handle = acpi_pci_get_bridge_handle(pbus);
321 if (handle)
322 break;
323 }
324
325 /*
326 * _HPP settings apply to all child buses, until another _HPP is
327 * encountered. If we don't find an _HPP for the input pci dev,
328 * look for it in the parent device scope since that would apply to
329 * this pci dev.
330 */
331 while (handle) {
332 status = acpi_run_hpx(handle, hpp);
333 if (ACPI_SUCCESS(status))
334 return 0;
335 status = acpi_run_hpp(handle, hpp);
336 if (ACPI_SUCCESS(status))
337 return 0;
338 if (acpi_is_root_bridge(handle))
339 break;
340 status = acpi_get_parent(handle, &phandle);
341 if (ACPI_FAILURE(status))
342 break;
343 handle = phandle;
344 }
345 return -ENODEV;
346}
347EXPORT_SYMBOL_GPL(pci_get_hp_params);
348
David Shaohua Li0f644742005-03-19 00:15:48 -0500349/*
350 * _SxD returns the D-state with the highest power
351 * (lowest D-state number) supported in the S-state "x".
352 *
353 * If the devices does not have a _PRW
354 * (Power Resources for Wake) supporting system wakeup from "x"
355 * then the OS is free to choose a lower power (higher number
356 * D-state) than the return value from _SxD.
357 *
358 * But if _PRW is enabled at S-state "x", the OS
359 * must not choose a power lower than _SxD --
360 * unless the device has an _SxW method specifying
361 * the lowest power (highest D-state number) the device
362 * may enter while still able to wake the system.
363 *
364 * ie. depending on global OS policy:
365 *
366 * if (_PRW at S-state x)
367 * choose from highest power _SxD to lowest power _SxW
368 * else // no _PRW at S-state x
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700369 * choose highest power _SxD or any lower power
David Shaohua Li0f644742005-03-19 00:15:48 -0500370 */
371
Rafael J. Wysocki8d2bdf42008-06-05 01:16:37 +0200372static pci_power_t acpi_pci_choose_state(struct pci_dev *pdev)
David Shaohua Li0f644742005-03-19 00:15:48 -0500373{
Huang Ying448bd852012-06-23 10:23:51 +0800374 int acpi_state, d_max;
David Shaohua Li0f644742005-03-19 00:15:48 -0500375
Huang Ying448bd852012-06-23 10:23:51 +0800376 if (pdev->no_d3cold)
377 d_max = ACPI_STATE_D3_HOT;
378 else
379 d_max = ACPI_STATE_D3_COLD;
380 acpi_state = acpi_pm_device_sleep_state(&pdev->dev, NULL, d_max);
Shaohua Liab826ca2007-07-20 10:03:22 +0800381 if (acpi_state < 0)
382 return PCI_POWER_ERROR;
383
384 switch (acpi_state) {
385 case ACPI_STATE_D0:
386 return PCI_D0;
387 case ACPI_STATE_D1:
388 return PCI_D1;
389 case ACPI_STATE_D2:
390 return PCI_D2;
Lin Ming1cc0c992012-04-23 09:03:49 +0800391 case ACPI_STATE_D3_HOT:
Shaohua Liab826ca2007-07-20 10:03:22 +0800392 return PCI_D3hot;
Lin Ming28c21032011-05-04 22:56:43 +0800393 case ACPI_STATE_D3_COLD:
394 return PCI_D3cold;
Shaohua Liab826ca2007-07-20 10:03:22 +0800395 }
396 return PCI_POWER_ERROR;
David Shaohua Li0f644742005-03-19 00:15:48 -0500397}
Rafael J. Wysocki961d9122008-07-07 03:32:02 +0200398
399static bool acpi_pci_power_manageable(struct pci_dev *dev)
400{
Rafael J. Wysocki85dbb3d2014-07-24 01:18:53 +0200401 struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
402 return adev ? acpi_device_power_manageable(adev) : false;
Rafael J. Wysocki961d9122008-07-07 03:32:02 +0200403}
David Shaohua Li0f644742005-03-19 00:15:48 -0500404
David Shaohua Lib9131002005-03-19 00:16:18 -0500405static int acpi_pci_set_power_state(struct pci_dev *dev, pci_power_t state)
406{
Rafael J. Wysocki85dbb3d2014-07-24 01:18:53 +0200407 struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
David Brownell583c3772008-02-22 21:41:51 -0800408 static const u8 state_conv[] = {
409 [PCI_D0] = ACPI_STATE_D0,
410 [PCI_D1] = ACPI_STATE_D1,
411 [PCI_D2] = ACPI_STATE_D2,
Rafael J. Wysockifc6504b2013-06-14 00:29:50 +0200412 [PCI_D3hot] = ACPI_STATE_D3_COLD,
413 [PCI_D3cold] = ACPI_STATE_D3_COLD,
David Shaohua Lib9131002005-03-19 00:16:18 -0500414 };
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200415 int error = -EINVAL;
David Shaohua Lib9131002005-03-19 00:16:18 -0500416
Shaohua Li10b3dca2007-07-20 10:03:25 +0800417 /* If the ACPI device has _EJ0, ignore the device */
Rafael J. Wysocki85dbb3d2014-07-24 01:18:53 +0200418 if (!adev || acpi_has_method(adev->handle, "_EJ0"))
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200419 return -ENODEV;
David Brownell583c3772008-02-22 21:41:51 -0800420
421 switch (state) {
Rafael J. Wysocki8b713a82012-10-24 02:08:38 +0200422 case PCI_D3cold:
423 if (dev_pm_qos_flags(&dev->dev, PM_QOS_FLAG_NO_POWER_OFF) ==
424 PM_QOS_FLAGS_ALL) {
425 error = -EBUSY;
426 break;
427 }
David Brownell583c3772008-02-22 21:41:51 -0800428 case PCI_D0:
429 case PCI_D1:
430 case PCI_D2:
431 case PCI_D3hot:
Rafael J. Wysocki85dbb3d2014-07-24 01:18:53 +0200432 error = acpi_device_set_power(adev, state_conv[state]);
David Brownell583c3772008-02-22 21:41:51 -0800433 }
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200434
435 if (!error)
Lan Tianyud010e572013-07-30 10:32:30 +0800436 dev_dbg(&dev->dev, "power state changed by ACPI to %s\n",
Rafael J. Wysockifc6504b2013-06-14 00:29:50 +0200437 acpi_power_state_string(state_conv[state]));
Rafael J. Wysocki44e4e662008-07-07 03:32:52 +0200438
439 return error;
David Shaohua Lib9131002005-03-19 00:16:18 -0500440}
441
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +0200442static bool acpi_pci_can_wakeup(struct pci_dev *dev)
443{
Rafael J. Wysocki85dbb3d2014-07-24 01:18:53 +0200444 struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
445 return adev ? acpi_device_can_wakeup(adev) : false;
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +0200446}
447
Rafael J. Wysocki0baed8d2009-09-08 23:16:24 +0200448static void acpi_pci_propagate_wakeup_enable(struct pci_bus *bus, bool enable)
449{
450 while (bus->parent) {
Rafael J. Wysockidc1a94a2009-11-29 16:35:54 +0100451 if (!acpi_pm_device_sleep_wake(&bus->self->dev, enable))
Rafael J. Wysocki0baed8d2009-09-08 23:16:24 +0200452 return;
453 bus = bus->parent;
454 }
455
456 /* We have reached the root bus. */
457 if (bus->bridge)
458 acpi_pm_device_sleep_wake(bus->bridge, enable);
459}
460
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +0200461static int acpi_pci_sleep_wake(struct pci_dev *dev, bool enable)
462{
Rafael J. Wysocki0baed8d2009-09-08 23:16:24 +0200463 if (acpi_pci_can_wakeup(dev))
464 return acpi_pm_device_sleep_wake(&dev->dev, enable);
465
Rafael J. Wysockidc1a94a2009-11-29 16:35:54 +0100466 acpi_pci_propagate_wakeup_enable(dev->bus, enable);
Rafael J. Wysocki0baed8d2009-09-08 23:16:24 +0200467 return 0;
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +0200468}
469
Rafael J. Wysockib67ea762010-02-17 23:44:09 +0100470static void acpi_pci_propagate_run_wake(struct pci_bus *bus, bool enable)
471{
472 while (bus->parent) {
473 struct pci_dev *bridge = bus->self;
474
475 if (bridge->pme_interrupt)
476 return;
Lin Mingb24e5092012-03-27 15:43:25 +0800477 if (!acpi_pm_device_run_wake(&bridge->dev, enable))
Rafael J. Wysockib67ea762010-02-17 23:44:09 +0100478 return;
479 bus = bus->parent;
480 }
481
482 /* We have reached the root bus. */
483 if (bus->bridge)
Lin Mingb24e5092012-03-27 15:43:25 +0800484 acpi_pm_device_run_wake(bus->bridge, enable);
Rafael J. Wysockib67ea762010-02-17 23:44:09 +0100485}
486
487static int acpi_pci_run_wake(struct pci_dev *dev, bool enable)
488{
Huang Ying448bd852012-06-23 10:23:51 +0800489 /*
490 * Per PCI Express Base Specification Revision 2.0 section
491 * 5.3.3.2 Link Wakeup, platform support is needed for D3cold
492 * waking up to power on the main link even if there is PME
493 * support for D3cold
494 */
495 if (dev->pme_interrupt && !dev->runtime_d3cold)
Rafael J. Wysockib67ea762010-02-17 23:44:09 +0100496 return 0;
497
Lin Mingb24e5092012-03-27 15:43:25 +0800498 if (!acpi_pm_device_run_wake(&dev->dev, enable))
Rafael J. Wysockib67ea762010-02-17 23:44:09 +0100499 return 0;
500
501 acpi_pci_propagate_run_wake(dev->bus, enable);
502 return 0;
503}
504
Rafael J. Wysocki961d9122008-07-07 03:32:02 +0200505static struct pci_platform_pm_ops acpi_pci_platform_pm = {
506 .is_manageable = acpi_pci_power_manageable,
507 .set_state = acpi_pci_set_power_state,
508 .choose_state = acpi_pci_choose_state,
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +0200509 .sleep_wake = acpi_pci_sleep_wake,
Rafael J. Wysockib67ea762010-02-17 23:44:09 +0100510 .run_wake = acpi_pci_run_wake,
Rafael J. Wysocki961d9122008-07-07 03:32:02 +0200511};
David Shaohua Lib9131002005-03-19 00:16:18 -0500512
Jiang Liu5090d4a2013-04-12 05:44:21 +0000513void acpi_pci_add_bus(struct pci_bus *bus)
514{
Rafael J. Wysockibe1c9de2013-07-13 23:27:23 +0200515 if (acpi_pci_disabled || !bus->bridge)
Jiang Liu5090d4a2013-04-12 05:44:21 +0000516 return;
517
Rafael J. Wysockibe1c9de2013-07-13 23:27:23 +0200518 acpi_pci_slot_enumerate(bus);
519 acpiphp_enumerate_slots(bus);
Jiang Liu5090d4a2013-04-12 05:44:21 +0000520}
521
522void acpi_pci_remove_bus(struct pci_bus *bus)
523{
Rafael J. Wysockibe1c9de2013-07-13 23:27:23 +0200524 if (acpi_pci_disabled || !bus->bridge)
Jiang Liu5090d4a2013-04-12 05:44:21 +0000525 return;
526
Jiang Liu3b63aaa2013-04-12 05:44:26 +0000527 acpiphp_remove_slots(bus);
Jiang Liu5c0b04e2013-04-12 05:44:24 +0000528 acpi_pci_slot_remove(bus);
Jiang Liu5090d4a2013-04-12 05:44:21 +0000529}
530
David Shaohua Li84df749f2005-03-18 18:53:36 -0500531/* ACPI bus type */
Rafael J. Wysockie3f02c52013-11-29 16:27:34 +0100532static struct acpi_device *acpi_pci_find_companion(struct device *dev)
David Shaohua Li84df749f2005-03-18 18:53:36 -0500533{
Rafael J. Wysocki60f75b82013-08-07 22:55:00 +0200534 struct pci_dev *pci_dev = to_pci_dev(dev);
Rafael J. Wysocki5ce79d22013-11-28 23:58:08 +0100535 bool check_children;
Rafael J. Wysocki60f75b82013-08-07 22:55:00 +0200536 u64 addr;
David Shaohua Li84df749f2005-03-18 18:53:36 -0500537
Yijing Wang6788a512014-05-04 12:23:38 +0800538 check_children = pci_is_bridge(pci_dev);
David Shaohua Li84df749f2005-03-18 18:53:36 -0500539 /* Please ref to ACPI spec for the syntax of _ADR */
540 addr = (PCI_SLOT(pci_dev->devfn) << 16) | PCI_FUNC(pci_dev->devfn);
Rafael J. Wysockie3f02c52013-11-29 16:27:34 +0100541 return acpi_find_child_device(ACPI_COMPANION(dev->parent), addr,
Rafael J. Wysocki5ce79d22013-11-28 23:58:08 +0100542 check_children);
David Shaohua Li84df749f2005-03-18 18:53:36 -0500543}
544
Rafael J. Wysocki38a9a672012-12-23 00:02:54 +0100545static void pci_acpi_setup(struct device *dev)
Rafael J. Wysockid2e5f0c2012-12-23 00:02:44 +0100546{
Rafael J. Wysockid2e5f0c2012-12-23 00:02:44 +0100547 struct pci_dev *pci_dev = to_pci_dev(dev);
Rafael J. Wysockif0842802013-12-29 23:37:15 +0100548 struct acpi_device *adev = ACPI_COMPANION(dev);
Rafael J. Wysocki38a9a672012-12-23 00:02:54 +0100549
Rafael J. Wysockif0842802013-12-29 23:37:15 +0100550 if (!adev)
551 return;
552
553 pci_acpi_add_pm_notifier(adev, pci_dev);
554 if (!adev->wakeup.flags.valid)
Rafael J. Wysockid2e5f0c2012-12-23 00:02:44 +0100555 return;
556
557 device_set_wakeup_capable(dev, true);
558 acpi_pci_sleep_wake(pci_dev, false);
Rafael J. Wysockid2e5f0c2012-12-23 00:02:44 +0100559 if (adev->wakeup.flags.run_wake)
560 device_set_run_wake(dev, true);
561}
562
Rafael J. Wysocki38a9a672012-12-23 00:02:54 +0100563static void pci_acpi_cleanup(struct device *dev)
Rafael J. Wysockid2e5f0c2012-12-23 00:02:44 +0100564{
Rafael J. Wysockif0842802013-12-29 23:37:15 +0100565 struct acpi_device *adev = ACPI_COMPANION(dev);
Rafael J. Wysockid2e5f0c2012-12-23 00:02:44 +0100566
Rafael J. Wysockif0842802013-12-29 23:37:15 +0100567 if (!adev)
568 return;
569
570 pci_acpi_remove_pm_notifier(adev);
571 if (adev->wakeup.flags.valid) {
Rafael J. Wysockid2e5f0c2012-12-23 00:02:44 +0100572 device_set_wakeup_capable(dev, false);
573 device_set_run_wake(dev, false);
Rafael J. Wysockid2e5f0c2012-12-23 00:02:44 +0100574 }
575}
576
Rafael J. Wysocki53540092013-03-03 22:35:20 +0100577static bool pci_acpi_bus_match(struct device *dev)
578{
Yijing Wang40c368c2013-12-05 19:52:53 +0800579 return dev_is_pci(dev);
Rafael J. Wysocki53540092013-03-03 22:35:20 +0100580}
581
Muthu Kumar9c273b92006-04-28 00:42:21 -0700582static struct acpi_bus_type acpi_pci_bus = {
Rafael J. Wysocki53540092013-03-03 22:35:20 +0100583 .name = "PCI",
584 .match = pci_acpi_bus_match,
Rafael J. Wysockie3f02c52013-11-29 16:27:34 +0100585 .find_companion = acpi_pci_find_companion,
Rafael J. Wysocki38a9a672012-12-23 00:02:54 +0100586 .setup = pci_acpi_setup,
587 .cleanup = pci_acpi_cleanup,
David Shaohua Li84df749f2005-03-18 18:53:36 -0500588};
589
Muthu Kumar9c273b92006-04-28 00:42:21 -0700590static int __init acpi_pci_init(void)
David Shaohua Li84df749f2005-03-18 18:53:36 -0500591{
592 int ret;
593
Bob Moore993958f2009-02-03 15:14:33 +0800594 if (acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_MSI) {
Yijing Wange7d45152013-05-28 16:03:46 +0800595 pr_info("ACPI FADT declares the system doesn't support MSI, so disable it\n");
Shaohua Lif8993af2007-04-25 11:05:12 +0800596 pci_no_msi();
597 }
Shaohua Li5fde2442008-07-23 10:32:24 +0800598
Bob Moore993958f2009-02-03 15:14:33 +0800599 if (acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_ASPM) {
Yijing Wange7d45152013-05-28 16:03:46 +0800600 pr_info("ACPI FADT declares the system doesn't support PCIe ASPM, so disable it\n");
Shaohua Li5fde2442008-07-23 10:32:24 +0800601 pcie_no_aspm();
602 }
603
Muthu Kumar9c273b92006-04-28 00:42:21 -0700604 ret = register_acpi_bus_type(&acpi_pci_bus);
David Shaohua Li84df749f2005-03-18 18:53:36 -0500605 if (ret)
606 return 0;
Jiang Liu5c0b04e2013-04-12 05:44:24 +0000607
Rafael J. Wysocki961d9122008-07-07 03:32:02 +0200608 pci_set_platform_pm(&acpi_pci_platform_pm);
Jiang Liu5c0b04e2013-04-12 05:44:24 +0000609 acpi_pci_slot_init();
Jiang Liu3b63aaa2013-04-12 05:44:26 +0000610 acpiphp_init();
Jiang Liu5c0b04e2013-04-12 05:44:24 +0000611
David Shaohua Li84df749f2005-03-18 18:53:36 -0500612 return 0;
613}
Muthu Kumar9c273b92006-04-28 00:42:21 -0700614arch_initcall(acpi_pci_init);