Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * File: pci-acpi.c |
Len Brown | a406d9e | 2005-03-23 16:16:03 -0500 | [diff] [blame] | 3 | * Purpose: Provide PCI support in ACPI |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
David Shaohua Li | 84df749f | 2005-03-18 18:53:36 -0500 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/delay.h> |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/pci.h> |
Bjorn Helgaas | 9ce90ea | 2014-09-12 15:23:14 -0600 | [diff] [blame] | 13 | #include <linux/pci_hotplug.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/module.h> |
Shaohua Li | 5fde244 | 2008-07-23 10:32:24 +0800 | [diff] [blame] | 15 | #include <linux/pci-aspm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/pci-acpi.h> |
Rafael J. Wysocki | b67ea76 | 2010-02-17 23:44:09 +0100 | [diff] [blame] | 17 | #include <linux/pm_runtime.h> |
Rafael J. Wysocki | 8b713a8 | 2012-10-24 02:08:38 +0200 | [diff] [blame] | 18 | #include <linux/pm_qos.h> |
David Shaohua Li | 0f64474 | 2005-03-19 00:15:48 -0500 | [diff] [blame] | 19 | #include "pci.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Jiang Liu | f4b57a3 | 2012-06-22 14:55:16 +0800 | [diff] [blame] | 21 | phys_addr_t acpi_pci_root_get_mcfg_addr(acpi_handle handle) |
| 22 | { |
| 23 | acpi_status status = AE_NOT_EXIST; |
| 24 | unsigned long long mcfg_addr; |
| 25 | |
| 26 | if (handle) |
| 27 | status = acpi_evaluate_integer(handle, METHOD_NAME__CBA, |
| 28 | NULL, &mcfg_addr); |
| 29 | if (ACPI_FAILURE(status)) |
| 30 | return 0; |
| 31 | |
| 32 | return (phys_addr_t)mcfg_addr; |
| 33 | } |
| 34 | |
Bjorn Helgaas | abbfec3 | 2014-09-12 15:29:55 -0600 | [diff] [blame] | 35 | static acpi_status decode_type0_hpx_record(union acpi_object *record, |
| 36 | struct hotplug_params *hpx) |
Bjorn Helgaas | 9ce90ea | 2014-09-12 15:23:14 -0600 | [diff] [blame] | 37 | { |
| 38 | int i; |
| 39 | union acpi_object *fields = record->package.elements; |
| 40 | u32 revision = fields[1].integer.value; |
| 41 | |
| 42 | switch (revision) { |
| 43 | case 1: |
| 44 | if (record->package.count != 6) |
| 45 | return AE_ERROR; |
| 46 | for (i = 2; i < 6; i++) |
| 47 | if (fields[i].type != ACPI_TYPE_INTEGER) |
| 48 | return AE_ERROR; |
| 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; |
| 55 | break; |
| 56 | default: |
| 57 | printk(KERN_WARNING |
| 58 | "%s: Type 0 Revision %d record not supported\n", |
| 59 | __func__, revision); |
| 60 | return AE_ERROR; |
| 61 | } |
| 62 | return AE_OK; |
| 63 | } |
| 64 | |
Bjorn Helgaas | abbfec3 | 2014-09-12 15:29:55 -0600 | [diff] [blame] | 65 | static acpi_status decode_type1_hpx_record(union acpi_object *record, |
| 66 | struct hotplug_params *hpx) |
Bjorn Helgaas | 9ce90ea | 2014-09-12 15:23:14 -0600 | [diff] [blame] | 67 | { |
| 68 | int i; |
| 69 | union acpi_object *fields = record->package.elements; |
| 70 | u32 revision = fields[1].integer.value; |
| 71 | |
| 72 | switch (revision) { |
| 73 | case 1: |
| 74 | if (record->package.count != 5) |
| 75 | return AE_ERROR; |
| 76 | for (i = 2; i < 5; i++) |
| 77 | if (fields[i].type != ACPI_TYPE_INTEGER) |
| 78 | return AE_ERROR; |
| 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; |
| 84 | break; |
| 85 | default: |
| 86 | printk(KERN_WARNING |
| 87 | "%s: Type 1 Revision %d record not supported\n", |
| 88 | __func__, revision); |
| 89 | return AE_ERROR; |
| 90 | } |
| 91 | return AE_OK; |
| 92 | } |
| 93 | |
Bjorn Helgaas | abbfec3 | 2014-09-12 15:29:55 -0600 | [diff] [blame] | 94 | static acpi_status decode_type2_hpx_record(union acpi_object *record, |
| 95 | struct hotplug_params *hpx) |
Bjorn Helgaas | 9ce90ea | 2014-09-12 15:23:14 -0600 | [diff] [blame] | 96 | { |
| 97 | int i; |
| 98 | union acpi_object *fields = record->package.elements; |
| 99 | u32 revision = fields[1].integer.value; |
| 100 | |
| 101 | switch (revision) { |
| 102 | case 1: |
| 103 | if (record->package.count != 18) |
| 104 | return AE_ERROR; |
| 105 | for (i = 2; i < 18; i++) |
| 106 | if (fields[i].type != ACPI_TYPE_INTEGER) |
| 107 | return AE_ERROR; |
| 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; |
| 126 | break; |
| 127 | default: |
| 128 | printk(KERN_WARNING |
| 129 | "%s: Type 2 Revision %d record not supported\n", |
| 130 | __func__, revision); |
| 131 | return AE_ERROR; |
| 132 | } |
| 133 | return AE_OK; |
| 134 | } |
| 135 | |
Bjorn Helgaas | abbfec3 | 2014-09-12 15:29:55 -0600 | [diff] [blame] | 136 | static acpi_status acpi_run_hpx(acpi_handle handle, struct hotplug_params *hpx) |
Bjorn Helgaas | 9ce90ea | 2014-09-12 15:23:14 -0600 | [diff] [blame] | 137 | { |
| 138 | acpi_status status; |
| 139 | struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL}; |
| 140 | union acpi_object *package, *record, *fields; |
| 141 | u32 type; |
| 142 | int i; |
| 143 | |
| 144 | /* Clear the return buffer with zeros */ |
| 145 | memset(hpx, 0, sizeof(struct hotplug_params)); |
| 146 | |
| 147 | status = acpi_evaluate_object(handle, "_HPX", NULL, &buffer); |
| 148 | if (ACPI_FAILURE(status)) |
| 149 | return status; |
| 150 | |
| 151 | package = (union acpi_object *)buffer.pointer; |
| 152 | if (package->type != ACPI_TYPE_PACKAGE) { |
| 153 | status = AE_ERROR; |
| 154 | goto exit; |
| 155 | } |
| 156 | |
| 157 | for (i = 0; i < package->package.count; i++) { |
| 158 | record = &package->package.elements[i]; |
| 159 | if (record->type != ACPI_TYPE_PACKAGE) { |
| 160 | status = AE_ERROR; |
| 161 | goto exit; |
| 162 | } |
| 163 | |
| 164 | fields = record->package.elements; |
| 165 | if (fields[0].type != ACPI_TYPE_INTEGER || |
| 166 | fields[1].type != ACPI_TYPE_INTEGER) { |
| 167 | status = AE_ERROR; |
| 168 | goto exit; |
| 169 | } |
| 170 | |
| 171 | type = fields[0].integer.value; |
| 172 | switch (type) { |
| 173 | case 0: |
| 174 | status = decode_type0_hpx_record(record, hpx); |
| 175 | if (ACPI_FAILURE(status)) |
| 176 | goto exit; |
| 177 | break; |
| 178 | case 1: |
| 179 | status = decode_type1_hpx_record(record, hpx); |
| 180 | if (ACPI_FAILURE(status)) |
| 181 | goto exit; |
| 182 | break; |
| 183 | case 2: |
| 184 | status = decode_type2_hpx_record(record, hpx); |
| 185 | if (ACPI_FAILURE(status)) |
| 186 | goto exit; |
| 187 | break; |
| 188 | default: |
| 189 | printk(KERN_ERR "%s: Type %d record not supported\n", |
| 190 | __func__, type); |
| 191 | status = AE_ERROR; |
| 192 | goto exit; |
| 193 | } |
| 194 | } |
| 195 | exit: |
| 196 | kfree(buffer.pointer); |
| 197 | return status; |
| 198 | } |
| 199 | |
Bjorn Helgaas | abbfec3 | 2014-09-12 15:29:55 -0600 | [diff] [blame] | 200 | static acpi_status acpi_run_hpp(acpi_handle handle, struct hotplug_params *hpp) |
Bjorn Helgaas | 9ce90ea | 2014-09-12 15:23:14 -0600 | [diff] [blame] | 201 | { |
| 202 | acpi_status status; |
| 203 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
| 204 | union acpi_object *package, *fields; |
| 205 | int i; |
| 206 | |
| 207 | memset(hpp, 0, sizeof(struct hotplug_params)); |
| 208 | |
| 209 | status = acpi_evaluate_object(handle, "_HPP", NULL, &buffer); |
| 210 | if (ACPI_FAILURE(status)) |
| 211 | return status; |
| 212 | |
| 213 | package = (union acpi_object *) buffer.pointer; |
| 214 | if (package->type != ACPI_TYPE_PACKAGE || |
| 215 | package->package.count != 4) { |
| 216 | status = AE_ERROR; |
| 217 | goto exit; |
| 218 | } |
| 219 | |
| 220 | fields = package->package.elements; |
| 221 | for (i = 0; i < 4; i++) { |
| 222 | if (fields[i].type != ACPI_TYPE_INTEGER) { |
| 223 | status = AE_ERROR; |
| 224 | goto exit; |
| 225 | } |
| 226 | } |
| 227 | |
| 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; |
| 234 | |
| 235 | exit: |
| 236 | kfree(buffer.pointer); |
| 237 | return status; |
| 238 | } |
| 239 | |
| 240 | /* pci_get_hp_params |
| 241 | * |
| 242 | * @dev - the pci_dev for which we want parameters |
| 243 | * @hpp - allocated by the caller |
| 244 | */ |
| 245 | int pci_get_hp_params(struct pci_dev *dev, struct hotplug_params *hpp) |
| 246 | { |
| 247 | acpi_status status; |
| 248 | acpi_handle handle, phandle; |
| 249 | struct pci_bus *pbus; |
| 250 | |
| 251 | handle = NULL; |
| 252 | for (pbus = dev->bus; pbus; pbus = pbus->parent) { |
| 253 | handle = acpi_pci_get_bridge_handle(pbus); |
| 254 | if (handle) |
| 255 | break; |
| 256 | } |
| 257 | |
| 258 | /* |
| 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 |
| 262 | * this pci dev. |
| 263 | */ |
| 264 | while (handle) { |
| 265 | status = acpi_run_hpx(handle, hpp); |
| 266 | if (ACPI_SUCCESS(status)) |
| 267 | return 0; |
| 268 | status = acpi_run_hpp(handle, hpp); |
| 269 | if (ACPI_SUCCESS(status)) |
| 270 | return 0; |
| 271 | if (acpi_is_root_bridge(handle)) |
| 272 | break; |
| 273 | status = acpi_get_parent(handle, &phandle); |
| 274 | if (ACPI_FAILURE(status)) |
| 275 | break; |
| 276 | handle = phandle; |
| 277 | } |
| 278 | return -ENODEV; |
| 279 | } |
| 280 | EXPORT_SYMBOL_GPL(pci_get_hp_params); |
| 281 | |
Bjorn Helgaas | 5e3d234 | 2014-09-12 15:36:29 -0600 | [diff] [blame] | 282 | /** |
| 283 | * pci_acpi_wake_bus - Root bus wakeup notification fork function. |
| 284 | * @work: Work item to handle. |
| 285 | */ |
| 286 | static void pci_acpi_wake_bus(struct work_struct *work) |
| 287 | { |
| 288 | struct acpi_device *adev; |
| 289 | struct acpi_pci_root *root; |
| 290 | |
| 291 | adev = container_of(work, struct acpi_device, wakeup.context.work); |
| 292 | root = acpi_driver_data(adev); |
| 293 | pci_pme_wakeup_bus(root->bus); |
| 294 | } |
| 295 | |
| 296 | /** |
| 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. |
| 300 | */ |
| 301 | static void pci_acpi_wake_dev(struct work_struct *work) |
| 302 | { |
| 303 | struct acpi_device_wakeup_context *context; |
| 304 | struct pci_dev *pci_dev; |
| 305 | |
| 306 | context = container_of(work, struct acpi_device_wakeup_context, work); |
| 307 | pci_dev = to_pci_dev(context->dev); |
| 308 | |
| 309 | if (pci_dev->pme_poll) |
| 310 | pci_dev->pme_poll = false; |
| 311 | |
| 312 | if (pci_dev->current_state == PCI_D3cold) { |
| 313 | pci_wakeup_event(pci_dev); |
| 314 | pm_runtime_resume(&pci_dev->dev); |
| 315 | return; |
| 316 | } |
| 317 | |
| 318 | /* Clear PME Status if set. */ |
| 319 | if (pci_dev->pme_support) |
| 320 | pci_check_pme_status(pci_dev); |
| 321 | |
| 322 | pci_wakeup_event(pci_dev); |
| 323 | pm_runtime_resume(&pci_dev->dev); |
| 324 | |
Markus Elfring | ff0387c | 2014-11-10 21:02:17 -0700 | [diff] [blame] | 325 | pci_pme_wakeup_bus(pci_dev->subordinate); |
Bjorn Helgaas | 5e3d234 | 2014-09-12 15:36:29 -0600 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | /** |
| 329 | * pci_acpi_add_bus_pm_notifier - Register PM notifier for root PCI bus. |
| 330 | * @dev: PCI root bridge ACPI device. |
| 331 | */ |
| 332 | acpi_status pci_acpi_add_bus_pm_notifier(struct acpi_device *dev) |
| 333 | { |
| 334 | return acpi_add_pm_notifier(dev, NULL, pci_acpi_wake_bus); |
| 335 | } |
| 336 | |
| 337 | /** |
| 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. |
| 341 | */ |
| 342 | acpi_status pci_acpi_add_pm_notifier(struct acpi_device *dev, |
| 343 | struct pci_dev *pci_dev) |
| 344 | { |
| 345 | return acpi_add_pm_notifier(dev, &pci_dev->dev, pci_acpi_wake_dev); |
| 346 | } |
| 347 | |
David Shaohua Li | 0f64474 | 2005-03-19 00:15:48 -0500 | [diff] [blame] | 348 | /* |
| 349 | * _SxD returns the D-state with the highest power |
| 350 | * (lowest D-state number) supported in the S-state "x". |
| 351 | * |
| 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. |
| 356 | * |
| 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. |
| 362 | * |
| 363 | * ie. depending on global OS policy: |
| 364 | * |
| 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 |
Bjorn Helgaas | f762598 | 2013-11-14 11:28:18 -0700 | [diff] [blame] | 368 | * choose highest power _SxD or any lower power |
David Shaohua Li | 0f64474 | 2005-03-19 00:15:48 -0500 | [diff] [blame] | 369 | */ |
| 370 | |
Rafael J. Wysocki | 8d2bdf4 | 2008-06-05 01:16:37 +0200 | [diff] [blame] | 371 | static pci_power_t acpi_pci_choose_state(struct pci_dev *pdev) |
David Shaohua Li | 0f64474 | 2005-03-19 00:15:48 -0500 | [diff] [blame] | 372 | { |
Huang Ying | 448bd85 | 2012-06-23 10:23:51 +0800 | [diff] [blame] | 373 | int acpi_state, d_max; |
David Shaohua Li | 0f64474 | 2005-03-19 00:15:48 -0500 | [diff] [blame] | 374 | |
Huang Ying | 448bd85 | 2012-06-23 10:23:51 +0800 | [diff] [blame] | 375 | if (pdev->no_d3cold) |
| 376 | d_max = ACPI_STATE_D3_HOT; |
| 377 | else |
| 378 | d_max = ACPI_STATE_D3_COLD; |
| 379 | acpi_state = acpi_pm_device_sleep_state(&pdev->dev, NULL, d_max); |
Shaohua Li | ab826ca | 2007-07-20 10:03:22 +0800 | [diff] [blame] | 380 | if (acpi_state < 0) |
| 381 | return PCI_POWER_ERROR; |
| 382 | |
| 383 | switch (acpi_state) { |
| 384 | case ACPI_STATE_D0: |
| 385 | return PCI_D0; |
| 386 | case ACPI_STATE_D1: |
| 387 | return PCI_D1; |
| 388 | case ACPI_STATE_D2: |
| 389 | return PCI_D2; |
Lin Ming | 1cc0c99 | 2012-04-23 09:03:49 +0800 | [diff] [blame] | 390 | case ACPI_STATE_D3_HOT: |
Shaohua Li | ab826ca | 2007-07-20 10:03:22 +0800 | [diff] [blame] | 391 | return PCI_D3hot; |
Lin Ming | 28c2103 | 2011-05-04 22:56:43 +0800 | [diff] [blame] | 392 | case ACPI_STATE_D3_COLD: |
| 393 | return PCI_D3cold; |
Shaohua Li | ab826ca | 2007-07-20 10:03:22 +0800 | [diff] [blame] | 394 | } |
| 395 | return PCI_POWER_ERROR; |
David Shaohua Li | 0f64474 | 2005-03-19 00:15:48 -0500 | [diff] [blame] | 396 | } |
Rafael J. Wysocki | 961d912 | 2008-07-07 03:32:02 +0200 | [diff] [blame] | 397 | |
| 398 | static bool acpi_pci_power_manageable(struct pci_dev *dev) |
| 399 | { |
Rafael J. Wysocki | 85dbb3d | 2014-07-24 01:18:53 +0200 | [diff] [blame] | 400 | struct acpi_device *adev = ACPI_COMPANION(&dev->dev); |
| 401 | return adev ? acpi_device_power_manageable(adev) : false; |
Rafael J. Wysocki | 961d912 | 2008-07-07 03:32:02 +0200 | [diff] [blame] | 402 | } |
David Shaohua Li | 0f64474 | 2005-03-19 00:15:48 -0500 | [diff] [blame] | 403 | |
David Shaohua Li | b913100 | 2005-03-19 00:16:18 -0500 | [diff] [blame] | 404 | static int acpi_pci_set_power_state(struct pci_dev *dev, pci_power_t state) |
| 405 | { |
Rafael J. Wysocki | 85dbb3d | 2014-07-24 01:18:53 +0200 | [diff] [blame] | 406 | struct acpi_device *adev = ACPI_COMPANION(&dev->dev); |
David Brownell | 583c377 | 2008-02-22 21:41:51 -0800 | [diff] [blame] | 407 | static const u8 state_conv[] = { |
| 408 | [PCI_D0] = ACPI_STATE_D0, |
| 409 | [PCI_D1] = ACPI_STATE_D1, |
| 410 | [PCI_D2] = ACPI_STATE_D2, |
Rafael J. Wysocki | fc6504b | 2013-06-14 00:29:50 +0200 | [diff] [blame] | 411 | [PCI_D3hot] = ACPI_STATE_D3_COLD, |
| 412 | [PCI_D3cold] = ACPI_STATE_D3_COLD, |
David Shaohua Li | b913100 | 2005-03-19 00:16:18 -0500 | [diff] [blame] | 413 | }; |
Rafael J. Wysocki | 44e4e66 | 2008-07-07 03:32:52 +0200 | [diff] [blame] | 414 | int error = -EINVAL; |
David Shaohua Li | b913100 | 2005-03-19 00:16:18 -0500 | [diff] [blame] | 415 | |
Shaohua Li | 10b3dca | 2007-07-20 10:03:25 +0800 | [diff] [blame] | 416 | /* If the ACPI device has _EJ0, ignore the device */ |
Rafael J. Wysocki | 85dbb3d | 2014-07-24 01:18:53 +0200 | [diff] [blame] | 417 | if (!adev || acpi_has_method(adev->handle, "_EJ0")) |
Rafael J. Wysocki | 44e4e66 | 2008-07-07 03:32:52 +0200 | [diff] [blame] | 418 | return -ENODEV; |
David Brownell | 583c377 | 2008-02-22 21:41:51 -0800 | [diff] [blame] | 419 | |
| 420 | switch (state) { |
Rafael J. Wysocki | 8b713a8 | 2012-10-24 02:08:38 +0200 | [diff] [blame] | 421 | case PCI_D3cold: |
| 422 | if (dev_pm_qos_flags(&dev->dev, PM_QOS_FLAG_NO_POWER_OFF) == |
| 423 | PM_QOS_FLAGS_ALL) { |
| 424 | error = -EBUSY; |
| 425 | break; |
| 426 | } |
David Brownell | 583c377 | 2008-02-22 21:41:51 -0800 | [diff] [blame] | 427 | case PCI_D0: |
| 428 | case PCI_D1: |
| 429 | case PCI_D2: |
| 430 | case PCI_D3hot: |
Rafael J. Wysocki | 85dbb3d | 2014-07-24 01:18:53 +0200 | [diff] [blame] | 431 | error = acpi_device_set_power(adev, state_conv[state]); |
David Brownell | 583c377 | 2008-02-22 21:41:51 -0800 | [diff] [blame] | 432 | } |
Rafael J. Wysocki | 44e4e66 | 2008-07-07 03:32:52 +0200 | [diff] [blame] | 433 | |
| 434 | if (!error) |
Lan Tianyu | d010e57 | 2013-07-30 10:32:30 +0800 | [diff] [blame] | 435 | dev_dbg(&dev->dev, "power state changed by ACPI to %s\n", |
Rafael J. Wysocki | fc6504b | 2013-06-14 00:29:50 +0200 | [diff] [blame] | 436 | acpi_power_state_string(state_conv[state])); |
Rafael J. Wysocki | 44e4e66 | 2008-07-07 03:32:52 +0200 | [diff] [blame] | 437 | |
| 438 | return error; |
David Shaohua Li | b913100 | 2005-03-19 00:16:18 -0500 | [diff] [blame] | 439 | } |
| 440 | |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 441 | static bool acpi_pci_can_wakeup(struct pci_dev *dev) |
| 442 | { |
Rafael J. Wysocki | 85dbb3d | 2014-07-24 01:18:53 +0200 | [diff] [blame] | 443 | struct acpi_device *adev = ACPI_COMPANION(&dev->dev); |
| 444 | return adev ? acpi_device_can_wakeup(adev) : false; |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 445 | } |
| 446 | |
Rafael J. Wysocki | 0baed8d | 2009-09-08 23:16:24 +0200 | [diff] [blame] | 447 | static void acpi_pci_propagate_wakeup_enable(struct pci_bus *bus, bool enable) |
| 448 | { |
| 449 | while (bus->parent) { |
Rafael J. Wysocki | dc1a94a | 2009-11-29 16:35:54 +0100 | [diff] [blame] | 450 | if (!acpi_pm_device_sleep_wake(&bus->self->dev, enable)) |
Rafael J. Wysocki | 0baed8d | 2009-09-08 23:16:24 +0200 | [diff] [blame] | 451 | return; |
| 452 | bus = bus->parent; |
| 453 | } |
| 454 | |
| 455 | /* We have reached the root bus. */ |
| 456 | if (bus->bridge) |
| 457 | acpi_pm_device_sleep_wake(bus->bridge, enable); |
| 458 | } |
| 459 | |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 460 | static int acpi_pci_sleep_wake(struct pci_dev *dev, bool enable) |
| 461 | { |
Rafael J. Wysocki | 0baed8d | 2009-09-08 23:16:24 +0200 | [diff] [blame] | 462 | if (acpi_pci_can_wakeup(dev)) |
| 463 | return acpi_pm_device_sleep_wake(&dev->dev, enable); |
| 464 | |
Rafael J. Wysocki | dc1a94a | 2009-11-29 16:35:54 +0100 | [diff] [blame] | 465 | acpi_pci_propagate_wakeup_enable(dev->bus, enable); |
Rafael J. Wysocki | 0baed8d | 2009-09-08 23:16:24 +0200 | [diff] [blame] | 466 | return 0; |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 467 | } |
| 468 | |
Rafael J. Wysocki | b67ea76 | 2010-02-17 23:44:09 +0100 | [diff] [blame] | 469 | static void acpi_pci_propagate_run_wake(struct pci_bus *bus, bool enable) |
| 470 | { |
| 471 | while (bus->parent) { |
| 472 | struct pci_dev *bridge = bus->self; |
| 473 | |
| 474 | if (bridge->pme_interrupt) |
| 475 | return; |
Lin Ming | b24e509 | 2012-03-27 15:43:25 +0800 | [diff] [blame] | 476 | if (!acpi_pm_device_run_wake(&bridge->dev, enable)) |
Rafael J. Wysocki | b67ea76 | 2010-02-17 23:44:09 +0100 | [diff] [blame] | 477 | return; |
| 478 | bus = bus->parent; |
| 479 | } |
| 480 | |
| 481 | /* We have reached the root bus. */ |
| 482 | if (bus->bridge) |
Lin Ming | b24e509 | 2012-03-27 15:43:25 +0800 | [diff] [blame] | 483 | acpi_pm_device_run_wake(bus->bridge, enable); |
Rafael J. Wysocki | b67ea76 | 2010-02-17 23:44:09 +0100 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | static int acpi_pci_run_wake(struct pci_dev *dev, bool enable) |
| 487 | { |
Huang Ying | 448bd85 | 2012-06-23 10:23:51 +0800 | [diff] [blame] | 488 | /* |
| 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 |
| 492 | * support for D3cold |
| 493 | */ |
| 494 | if (dev->pme_interrupt && !dev->runtime_d3cold) |
Rafael J. Wysocki | b67ea76 | 2010-02-17 23:44:09 +0100 | [diff] [blame] | 495 | return 0; |
| 496 | |
Lin Ming | b24e509 | 2012-03-27 15:43:25 +0800 | [diff] [blame] | 497 | if (!acpi_pm_device_run_wake(&dev->dev, enable)) |
Rafael J. Wysocki | b67ea76 | 2010-02-17 23:44:09 +0100 | [diff] [blame] | 498 | return 0; |
| 499 | |
| 500 | acpi_pci_propagate_run_wake(dev->bus, enable); |
| 501 | return 0; |
| 502 | } |
| 503 | |
Rafael J. Wysocki | bac2a90 | 2015-01-21 02:17:42 +0100 | [diff] [blame] | 504 | static bool acpi_pci_need_resume(struct pci_dev *dev) |
| 505 | { |
| 506 | struct acpi_device *adev = ACPI_COMPANION(&dev->dev); |
| 507 | |
| 508 | if (!adev || !acpi_device_power_manageable(adev)) |
| 509 | return false; |
| 510 | |
| 511 | if (device_may_wakeup(&dev->dev) != !!adev->wakeup.prepare_count) |
| 512 | return true; |
| 513 | |
| 514 | if (acpi_target_system_state() == ACPI_STATE_S0) |
| 515 | return false; |
| 516 | |
| 517 | return !!adev->power.flags.dsw_present; |
| 518 | } |
| 519 | |
Rafael J. Wysocki | 961d912 | 2008-07-07 03:32:02 +0200 | [diff] [blame] | 520 | static struct pci_platform_pm_ops acpi_pci_platform_pm = { |
| 521 | .is_manageable = acpi_pci_power_manageable, |
| 522 | .set_state = acpi_pci_set_power_state, |
| 523 | .choose_state = acpi_pci_choose_state, |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 524 | .sleep_wake = acpi_pci_sleep_wake, |
Rafael J. Wysocki | b67ea76 | 2010-02-17 23:44:09 +0100 | [diff] [blame] | 525 | .run_wake = acpi_pci_run_wake, |
Rafael J. Wysocki | bac2a90 | 2015-01-21 02:17:42 +0100 | [diff] [blame] | 526 | .need_resume = acpi_pci_need_resume, |
Rafael J. Wysocki | 961d912 | 2008-07-07 03:32:02 +0200 | [diff] [blame] | 527 | }; |
David Shaohua Li | b913100 | 2005-03-19 00:16:18 -0500 | [diff] [blame] | 528 | |
Jiang Liu | 5090d4a | 2013-04-12 05:44:21 +0000 | [diff] [blame] | 529 | void acpi_pci_add_bus(struct pci_bus *bus) |
| 530 | { |
Rafael J. Wysocki | be1c9de | 2013-07-13 23:27:23 +0200 | [diff] [blame] | 531 | if (acpi_pci_disabled || !bus->bridge) |
Jiang Liu | 5090d4a | 2013-04-12 05:44:21 +0000 | [diff] [blame] | 532 | return; |
| 533 | |
Rafael J. Wysocki | be1c9de | 2013-07-13 23:27:23 +0200 | [diff] [blame] | 534 | acpi_pci_slot_enumerate(bus); |
| 535 | acpiphp_enumerate_slots(bus); |
Jiang Liu | 5090d4a | 2013-04-12 05:44:21 +0000 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | void acpi_pci_remove_bus(struct pci_bus *bus) |
| 539 | { |
Rafael J. Wysocki | be1c9de | 2013-07-13 23:27:23 +0200 | [diff] [blame] | 540 | if (acpi_pci_disabled || !bus->bridge) |
Jiang Liu | 5090d4a | 2013-04-12 05:44:21 +0000 | [diff] [blame] | 541 | return; |
| 542 | |
Jiang Liu | 3b63aaa | 2013-04-12 05:44:26 +0000 | [diff] [blame] | 543 | acpiphp_remove_slots(bus); |
Jiang Liu | 5c0b04e | 2013-04-12 05:44:24 +0000 | [diff] [blame] | 544 | acpi_pci_slot_remove(bus); |
Jiang Liu | 5090d4a | 2013-04-12 05:44:21 +0000 | [diff] [blame] | 545 | } |
| 546 | |
David Shaohua Li | 84df749f | 2005-03-18 18:53:36 -0500 | [diff] [blame] | 547 | /* ACPI bus type */ |
Rafael J. Wysocki | e3f02c5 | 2013-11-29 16:27:34 +0100 | [diff] [blame] | 548 | static struct acpi_device *acpi_pci_find_companion(struct device *dev) |
David Shaohua Li | 84df749f | 2005-03-18 18:53:36 -0500 | [diff] [blame] | 549 | { |
Rafael J. Wysocki | 60f75b8 | 2013-08-07 22:55:00 +0200 | [diff] [blame] | 550 | struct pci_dev *pci_dev = to_pci_dev(dev); |
Rafael J. Wysocki | 5ce79d2 | 2013-11-28 23:58:08 +0100 | [diff] [blame] | 551 | bool check_children; |
Rafael J. Wysocki | 60f75b8 | 2013-08-07 22:55:00 +0200 | [diff] [blame] | 552 | u64 addr; |
David Shaohua Li | 84df749f | 2005-03-18 18:53:36 -0500 | [diff] [blame] | 553 | |
Yijing Wang | 6788a51 | 2014-05-04 12:23:38 +0800 | [diff] [blame] | 554 | check_children = pci_is_bridge(pci_dev); |
David Shaohua Li | 84df749f | 2005-03-18 18:53:36 -0500 | [diff] [blame] | 555 | /* Please ref to ACPI spec for the syntax of _ADR */ |
| 556 | addr = (PCI_SLOT(pci_dev->devfn) << 16) | PCI_FUNC(pci_dev->devfn); |
Rafael J. Wysocki | e3f02c5 | 2013-11-29 16:27:34 +0100 | [diff] [blame] | 557 | return acpi_find_child_device(ACPI_COMPANION(dev->parent), addr, |
Rafael J. Wysocki | 5ce79d2 | 2013-11-28 23:58:08 +0100 | [diff] [blame] | 558 | check_children); |
David Shaohua Li | 84df749f | 2005-03-18 18:53:36 -0500 | [diff] [blame] | 559 | } |
| 560 | |
Rafael J. Wysocki | 38a9a67 | 2012-12-23 00:02:54 +0100 | [diff] [blame] | 561 | static void pci_acpi_setup(struct device *dev) |
Rafael J. Wysocki | d2e5f0c | 2012-12-23 00:02:44 +0100 | [diff] [blame] | 562 | { |
Rafael J. Wysocki | d2e5f0c | 2012-12-23 00:02:44 +0100 | [diff] [blame] | 563 | struct pci_dev *pci_dev = to_pci_dev(dev); |
Rafael J. Wysocki | f084280 | 2013-12-29 23:37:15 +0100 | [diff] [blame] | 564 | struct acpi_device *adev = ACPI_COMPANION(dev); |
Rafael J. Wysocki | 38a9a67 | 2012-12-23 00:02:54 +0100 | [diff] [blame] | 565 | |
Rafael J. Wysocki | f084280 | 2013-12-29 23:37:15 +0100 | [diff] [blame] | 566 | if (!adev) |
| 567 | return; |
| 568 | |
| 569 | pci_acpi_add_pm_notifier(adev, pci_dev); |
| 570 | if (!adev->wakeup.flags.valid) |
Rafael J. Wysocki | d2e5f0c | 2012-12-23 00:02:44 +0100 | [diff] [blame] | 571 | return; |
| 572 | |
| 573 | device_set_wakeup_capable(dev, true); |
| 574 | acpi_pci_sleep_wake(pci_dev, false); |
Rafael J. Wysocki | d2e5f0c | 2012-12-23 00:02:44 +0100 | [diff] [blame] | 575 | if (adev->wakeup.flags.run_wake) |
| 576 | device_set_run_wake(dev, true); |
| 577 | } |
| 578 | |
Rafael J. Wysocki | 38a9a67 | 2012-12-23 00:02:54 +0100 | [diff] [blame] | 579 | static void pci_acpi_cleanup(struct device *dev) |
Rafael J. Wysocki | d2e5f0c | 2012-12-23 00:02:44 +0100 | [diff] [blame] | 580 | { |
Rafael J. Wysocki | f084280 | 2013-12-29 23:37:15 +0100 | [diff] [blame] | 581 | struct acpi_device *adev = ACPI_COMPANION(dev); |
Rafael J. Wysocki | d2e5f0c | 2012-12-23 00:02:44 +0100 | [diff] [blame] | 582 | |
Rafael J. Wysocki | f084280 | 2013-12-29 23:37:15 +0100 | [diff] [blame] | 583 | if (!adev) |
| 584 | return; |
| 585 | |
| 586 | pci_acpi_remove_pm_notifier(adev); |
| 587 | if (adev->wakeup.flags.valid) { |
Rafael J. Wysocki | d2e5f0c | 2012-12-23 00:02:44 +0100 | [diff] [blame] | 588 | device_set_wakeup_capable(dev, false); |
| 589 | device_set_run_wake(dev, false); |
Rafael J. Wysocki | d2e5f0c | 2012-12-23 00:02:44 +0100 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | |
Rafael J. Wysocki | 5354009 | 2013-03-03 22:35:20 +0100 | [diff] [blame] | 593 | static bool pci_acpi_bus_match(struct device *dev) |
| 594 | { |
Yijing Wang | 40c368c | 2013-12-05 19:52:53 +0800 | [diff] [blame] | 595 | return dev_is_pci(dev); |
Rafael J. Wysocki | 5354009 | 2013-03-03 22:35:20 +0100 | [diff] [blame] | 596 | } |
| 597 | |
Muthu Kumar | 9c273b9 | 2006-04-28 00:42:21 -0700 | [diff] [blame] | 598 | static struct acpi_bus_type acpi_pci_bus = { |
Rafael J. Wysocki | 5354009 | 2013-03-03 22:35:20 +0100 | [diff] [blame] | 599 | .name = "PCI", |
| 600 | .match = pci_acpi_bus_match, |
Rafael J. Wysocki | e3f02c5 | 2013-11-29 16:27:34 +0100 | [diff] [blame] | 601 | .find_companion = acpi_pci_find_companion, |
Rafael J. Wysocki | 38a9a67 | 2012-12-23 00:02:54 +0100 | [diff] [blame] | 602 | .setup = pci_acpi_setup, |
| 603 | .cleanup = pci_acpi_cleanup, |
David Shaohua Li | 84df749f | 2005-03-18 18:53:36 -0500 | [diff] [blame] | 604 | }; |
| 605 | |
Muthu Kumar | 9c273b9 | 2006-04-28 00:42:21 -0700 | [diff] [blame] | 606 | static int __init acpi_pci_init(void) |
David Shaohua Li | 84df749f | 2005-03-18 18:53:36 -0500 | [diff] [blame] | 607 | { |
| 608 | int ret; |
| 609 | |
Bob Moore | 993958f | 2009-02-03 15:14:33 +0800 | [diff] [blame] | 610 | if (acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_MSI) { |
Yijing Wang | e7d4515 | 2013-05-28 16:03:46 +0800 | [diff] [blame] | 611 | pr_info("ACPI FADT declares the system doesn't support MSI, so disable it\n"); |
Shaohua Li | f8993af | 2007-04-25 11:05:12 +0800 | [diff] [blame] | 612 | pci_no_msi(); |
| 613 | } |
Shaohua Li | 5fde244 | 2008-07-23 10:32:24 +0800 | [diff] [blame] | 614 | |
Bob Moore | 993958f | 2009-02-03 15:14:33 +0800 | [diff] [blame] | 615 | if (acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_ASPM) { |
Yijing Wang | e7d4515 | 2013-05-28 16:03:46 +0800 | [diff] [blame] | 616 | pr_info("ACPI FADT declares the system doesn't support PCIe ASPM, so disable it\n"); |
Shaohua Li | 5fde244 | 2008-07-23 10:32:24 +0800 | [diff] [blame] | 617 | pcie_no_aspm(); |
| 618 | } |
| 619 | |
Muthu Kumar | 9c273b9 | 2006-04-28 00:42:21 -0700 | [diff] [blame] | 620 | ret = register_acpi_bus_type(&acpi_pci_bus); |
David Shaohua Li | 84df749f | 2005-03-18 18:53:36 -0500 | [diff] [blame] | 621 | if (ret) |
| 622 | return 0; |
Jiang Liu | 5c0b04e | 2013-04-12 05:44:24 +0000 | [diff] [blame] | 623 | |
Rafael J. Wysocki | 961d912 | 2008-07-07 03:32:02 +0200 | [diff] [blame] | 624 | pci_set_platform_pm(&acpi_pci_platform_pm); |
Jiang Liu | 5c0b04e | 2013-04-12 05:44:24 +0000 | [diff] [blame] | 625 | acpi_pci_slot_init(); |
Jiang Liu | 3b63aaa | 2013-04-12 05:44:26 +0000 | [diff] [blame] | 626 | acpiphp_init(); |
Jiang Liu | 5c0b04e | 2013-04-12 05:44:24 +0000 | [diff] [blame] | 627 | |
David Shaohua Li | 84df749f | 2005-03-18 18:53:36 -0500 | [diff] [blame] | 628 | return 0; |
| 629 | } |
Muthu Kumar | 9c273b9 | 2006-04-28 00:42:21 -0700 | [diff] [blame] | 630 | arch_initcall(acpi_pci_init); |