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 | |
Rafael J. Wysocki | b67ea76 | 2010-02-17 23:44:09 +0100 | [diff] [blame] | 21 | /** |
Rafael J. Wysocki | c072530 | 2014-07-23 01:00:45 +0200 | [diff] [blame] | 22 | * pci_acpi_wake_bus - Root bus wakeup notification fork function. |
| 23 | * @work: Work item to handle. |
Rafael J. Wysocki | b67ea76 | 2010-02-17 23:44:09 +0100 | [diff] [blame] | 24 | */ |
Rafael J. Wysocki | c072530 | 2014-07-23 01:00:45 +0200 | [diff] [blame] | 25 | static void pci_acpi_wake_bus(struct work_struct *work) |
Rafael J. Wysocki | b67ea76 | 2010-02-17 23:44:09 +0100 | [diff] [blame] | 26 | { |
Rafael J. Wysocki | c072530 | 2014-07-23 01:00:45 +0200 | [diff] [blame] | 27 | struct acpi_device *adev; |
| 28 | struct acpi_pci_root *root; |
Rafael J. Wysocki | b67ea76 | 2010-02-17 23:44:09 +0100 | [diff] [blame] | 29 | |
Rafael J. Wysocki | c072530 | 2014-07-23 01:00:45 +0200 | [diff] [blame] | 30 | 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. Wysocki | b67ea76 | 2010-02-17 23:44:09 +0100 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | /** |
Rafael J. Wysocki | c072530 | 2014-07-23 01:00:45 +0200 | [diff] [blame] | 36 | * pci_acpi_wake_dev - PCI device wakeup notification work function. |
Rafael J. Wysocki | b67ea76 | 2010-02-17 23:44:09 +0100 | [diff] [blame] | 37 | * @handle: ACPI handle of a device the notification is for. |
Rafael J. Wysocki | c072530 | 2014-07-23 01:00:45 +0200 | [diff] [blame] | 38 | * @work: Work item to handle. |
Rafael J. Wysocki | b67ea76 | 2010-02-17 23:44:09 +0100 | [diff] [blame] | 39 | */ |
Rafael J. Wysocki | c072530 | 2014-07-23 01:00:45 +0200 | [diff] [blame] | 40 | static void pci_acpi_wake_dev(struct work_struct *work) |
Rafael J. Wysocki | b67ea76 | 2010-02-17 23:44:09 +0100 | [diff] [blame] | 41 | { |
Rafael J. Wysocki | c072530 | 2014-07-23 01:00:45 +0200 | [diff] [blame] | 42 | struct acpi_device_wakeup_context *context; |
| 43 | struct pci_dev *pci_dev; |
Rafael J. Wysocki | b67ea76 | 2010-02-17 23:44:09 +0100 | [diff] [blame] | 44 | |
Rafael J. Wysocki | c072530 | 2014-07-23 01:00:45 +0200 | [diff] [blame] | 45 | context = container_of(work, struct acpi_device_wakeup_context, work); |
| 46 | pci_dev = to_pci_dev(context->dev); |
Rafael J. Wysocki | a424948 | 2011-11-06 22:21:46 +0100 | [diff] [blame] | 47 | |
Rafael J. Wysocki | 8341451 | 2013-09-14 03:38:20 +0200 | [diff] [blame] | 48 | if (pci_dev->pme_poll) |
| 49 | pci_dev->pme_poll = false; |
| 50 | |
Huang Ying | 448bd85 | 2012-06-23 10:23:51 +0800 | [diff] [blame] | 51 | 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. Wysocki | 24ad0ef | 2013-03-28 11:07:29 +0000 | [diff] [blame] | 57 | /* Clear PME Status if set. */ |
| 58 | if (pci_dev->pme_support) |
| 59 | pci_check_pme_status(pci_dev); |
Rafael J. Wysocki | 379021d | 2011-10-03 23:16:33 +0200 | [diff] [blame] | 60 | |
Rafael J. Wysocki | 24ad0ef | 2013-03-28 11:07:29 +0000 | [diff] [blame] | 61 | pci_wakeup_event(pci_dev); |
| 62 | pm_runtime_resume(&pci_dev->dev); |
Rafael J. Wysocki | a424948 | 2011-11-06 22:21:46 +0100 | [diff] [blame] | 63 | |
| 64 | if (pci_dev->subordinate) |
| 65 | pci_pme_wakeup_bus(pci_dev->subordinate); |
Rafael J. Wysocki | b67ea76 | 2010-02-17 23:44:09 +0100 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /** |
Rafael J. Wysocki | c072530 | 2014-07-23 01:00:45 +0200 | [diff] [blame] | 69 | * pci_acpi_add_bus_pm_notifier - Register PM notifier for root PCI bus. |
| 70 | * @dev: PCI root bridge ACPI device. |
Rafael J. Wysocki | b67ea76 | 2010-02-17 23:44:09 +0100 | [diff] [blame] | 71 | */ |
Rafael J. Wysocki | c072530 | 2014-07-23 01:00:45 +0200 | [diff] [blame] | 72 | acpi_status pci_acpi_add_bus_pm_notifier(struct acpi_device *dev) |
Rafael J. Wysocki | b67ea76 | 2010-02-17 23:44:09 +0100 | [diff] [blame] | 73 | { |
Rafael J. Wysocki | c072530 | 2014-07-23 01:00:45 +0200 | [diff] [blame] | 74 | return acpi_add_pm_notifier(dev, NULL, pci_acpi_wake_bus); |
Rafael J. Wysocki | b67ea76 | 2010-02-17 23:44:09 +0100 | [diff] [blame] | 75 | } |
| 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 | */ |
| 82 | acpi_status pci_acpi_add_pm_notifier(struct acpi_device *dev, |
| 83 | struct pci_dev *pci_dev) |
| 84 | { |
Rafael J. Wysocki | c072530 | 2014-07-23 01:00:45 +0200 | [diff] [blame] | 85 | return acpi_add_pm_notifier(dev, &pci_dev->dev, pci_acpi_wake_dev); |
Rafael J. Wysocki | b67ea76 | 2010-02-17 23:44:09 +0100 | [diff] [blame] | 86 | } |
| 87 | |
Jiang Liu | f4b57a3 | 2012-06-22 14:55:16 +0800 | [diff] [blame] | 88 | phys_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 Helgaas | 9ce90ea | 2014-09-12 15:23:14 -0600 | [diff] [blame^] | 102 | static acpi_status |
| 103 | decode_type0_hpx_record(union acpi_object *record, struct hotplug_params *hpx) |
| 104 | { |
| 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 | |
| 132 | static acpi_status |
| 133 | decode_type1_hpx_record(union acpi_object *record, struct hotplug_params *hpx) |
| 134 | { |
| 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 | |
| 161 | static acpi_status |
| 162 | decode_type2_hpx_record(union acpi_object *record, struct hotplug_params *hpx) |
| 163 | { |
| 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 | |
| 203 | static acpi_status |
| 204 | acpi_run_hpx(acpi_handle handle, struct hotplug_params *hpx) |
| 205 | { |
| 206 | acpi_status status; |
| 207 | struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL}; |
| 208 | union acpi_object *package, *record, *fields; |
| 209 | u32 type; |
| 210 | int i; |
| 211 | |
| 212 | /* Clear the return buffer with zeros */ |
| 213 | memset(hpx, 0, sizeof(struct hotplug_params)); |
| 214 | |
| 215 | status = acpi_evaluate_object(handle, "_HPX", NULL, &buffer); |
| 216 | if (ACPI_FAILURE(status)) |
| 217 | return status; |
| 218 | |
| 219 | package = (union acpi_object *)buffer.pointer; |
| 220 | if (package->type != ACPI_TYPE_PACKAGE) { |
| 221 | status = AE_ERROR; |
| 222 | goto exit; |
| 223 | } |
| 224 | |
| 225 | for (i = 0; i < package->package.count; i++) { |
| 226 | record = &package->package.elements[i]; |
| 227 | if (record->type != ACPI_TYPE_PACKAGE) { |
| 228 | status = AE_ERROR; |
| 229 | goto exit; |
| 230 | } |
| 231 | |
| 232 | fields = record->package.elements; |
| 233 | if (fields[0].type != ACPI_TYPE_INTEGER || |
| 234 | fields[1].type != ACPI_TYPE_INTEGER) { |
| 235 | status = AE_ERROR; |
| 236 | goto exit; |
| 237 | } |
| 238 | |
| 239 | type = fields[0].integer.value; |
| 240 | switch (type) { |
| 241 | case 0: |
| 242 | status = decode_type0_hpx_record(record, hpx); |
| 243 | if (ACPI_FAILURE(status)) |
| 244 | goto exit; |
| 245 | break; |
| 246 | case 1: |
| 247 | status = decode_type1_hpx_record(record, hpx); |
| 248 | if (ACPI_FAILURE(status)) |
| 249 | goto exit; |
| 250 | break; |
| 251 | case 2: |
| 252 | status = decode_type2_hpx_record(record, hpx); |
| 253 | if (ACPI_FAILURE(status)) |
| 254 | goto exit; |
| 255 | break; |
| 256 | default: |
| 257 | printk(KERN_ERR "%s: Type %d record not supported\n", |
| 258 | __func__, type); |
| 259 | status = AE_ERROR; |
| 260 | goto exit; |
| 261 | } |
| 262 | } |
| 263 | exit: |
| 264 | kfree(buffer.pointer); |
| 265 | return status; |
| 266 | } |
| 267 | |
| 268 | static acpi_status |
| 269 | acpi_run_hpp(acpi_handle handle, struct hotplug_params *hpp) |
| 270 | { |
| 271 | acpi_status status; |
| 272 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
| 273 | union acpi_object *package, *fields; |
| 274 | int i; |
| 275 | |
| 276 | memset(hpp, 0, sizeof(struct hotplug_params)); |
| 277 | |
| 278 | status = acpi_evaluate_object(handle, "_HPP", NULL, &buffer); |
| 279 | if (ACPI_FAILURE(status)) |
| 280 | return status; |
| 281 | |
| 282 | package = (union acpi_object *) buffer.pointer; |
| 283 | if (package->type != ACPI_TYPE_PACKAGE || |
| 284 | package->package.count != 4) { |
| 285 | status = AE_ERROR; |
| 286 | goto exit; |
| 287 | } |
| 288 | |
| 289 | fields = package->package.elements; |
| 290 | for (i = 0; i < 4; i++) { |
| 291 | if (fields[i].type != ACPI_TYPE_INTEGER) { |
| 292 | status = AE_ERROR; |
| 293 | goto exit; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | hpp->t0 = &hpp->type0_data; |
| 298 | hpp->t0->revision = 1; |
| 299 | hpp->t0->cache_line_size = fields[0].integer.value; |
| 300 | hpp->t0->latency_timer = fields[1].integer.value; |
| 301 | hpp->t0->enable_serr = fields[2].integer.value; |
| 302 | hpp->t0->enable_perr = fields[3].integer.value; |
| 303 | |
| 304 | exit: |
| 305 | kfree(buffer.pointer); |
| 306 | return status; |
| 307 | } |
| 308 | |
| 309 | /* pci_get_hp_params |
| 310 | * |
| 311 | * @dev - the pci_dev for which we want parameters |
| 312 | * @hpp - allocated by the caller |
| 313 | */ |
| 314 | int pci_get_hp_params(struct pci_dev *dev, struct hotplug_params *hpp) |
| 315 | { |
| 316 | acpi_status status; |
| 317 | acpi_handle handle, phandle; |
| 318 | struct pci_bus *pbus; |
| 319 | |
| 320 | handle = NULL; |
| 321 | for (pbus = dev->bus; pbus; pbus = pbus->parent) { |
| 322 | handle = acpi_pci_get_bridge_handle(pbus); |
| 323 | if (handle) |
| 324 | break; |
| 325 | } |
| 326 | |
| 327 | /* |
| 328 | * _HPP settings apply to all child buses, until another _HPP is |
| 329 | * encountered. If we don't find an _HPP for the input pci dev, |
| 330 | * look for it in the parent device scope since that would apply to |
| 331 | * this pci dev. |
| 332 | */ |
| 333 | while (handle) { |
| 334 | status = acpi_run_hpx(handle, hpp); |
| 335 | if (ACPI_SUCCESS(status)) |
| 336 | return 0; |
| 337 | status = acpi_run_hpp(handle, hpp); |
| 338 | if (ACPI_SUCCESS(status)) |
| 339 | return 0; |
| 340 | if (acpi_is_root_bridge(handle)) |
| 341 | break; |
| 342 | status = acpi_get_parent(handle, &phandle); |
| 343 | if (ACPI_FAILURE(status)) |
| 344 | break; |
| 345 | handle = phandle; |
| 346 | } |
| 347 | return -ENODEV; |
| 348 | } |
| 349 | EXPORT_SYMBOL_GPL(pci_get_hp_params); |
| 350 | |
David Shaohua Li | 0f64474 | 2005-03-19 00:15:48 -0500 | [diff] [blame] | 351 | /* |
| 352 | * _SxD returns the D-state with the highest power |
| 353 | * (lowest D-state number) supported in the S-state "x". |
| 354 | * |
| 355 | * If the devices does not have a _PRW |
| 356 | * (Power Resources for Wake) supporting system wakeup from "x" |
| 357 | * then the OS is free to choose a lower power (higher number |
| 358 | * D-state) than the return value from _SxD. |
| 359 | * |
| 360 | * But if _PRW is enabled at S-state "x", the OS |
| 361 | * must not choose a power lower than _SxD -- |
| 362 | * unless the device has an _SxW method specifying |
| 363 | * the lowest power (highest D-state number) the device |
| 364 | * may enter while still able to wake the system. |
| 365 | * |
| 366 | * ie. depending on global OS policy: |
| 367 | * |
| 368 | * if (_PRW at S-state x) |
| 369 | * choose from highest power _SxD to lowest power _SxW |
| 370 | * else // no _PRW at S-state x |
Bjorn Helgaas | f762598 | 2013-11-14 11:28:18 -0700 | [diff] [blame] | 371 | * choose highest power _SxD or any lower power |
David Shaohua Li | 0f64474 | 2005-03-19 00:15:48 -0500 | [diff] [blame] | 372 | */ |
| 373 | |
Rafael J. Wysocki | 8d2bdf4 | 2008-06-05 01:16:37 +0200 | [diff] [blame] | 374 | 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] | 375 | { |
Huang Ying | 448bd85 | 2012-06-23 10:23:51 +0800 | [diff] [blame] | 376 | int acpi_state, d_max; |
David Shaohua Li | 0f64474 | 2005-03-19 00:15:48 -0500 | [diff] [blame] | 377 | |
Huang Ying | 448bd85 | 2012-06-23 10:23:51 +0800 | [diff] [blame] | 378 | if (pdev->no_d3cold) |
| 379 | d_max = ACPI_STATE_D3_HOT; |
| 380 | else |
| 381 | d_max = ACPI_STATE_D3_COLD; |
| 382 | acpi_state = acpi_pm_device_sleep_state(&pdev->dev, NULL, d_max); |
Shaohua Li | ab826ca | 2007-07-20 10:03:22 +0800 | [diff] [blame] | 383 | if (acpi_state < 0) |
| 384 | return PCI_POWER_ERROR; |
| 385 | |
| 386 | switch (acpi_state) { |
| 387 | case ACPI_STATE_D0: |
| 388 | return PCI_D0; |
| 389 | case ACPI_STATE_D1: |
| 390 | return PCI_D1; |
| 391 | case ACPI_STATE_D2: |
| 392 | return PCI_D2; |
Lin Ming | 1cc0c99 | 2012-04-23 09:03:49 +0800 | [diff] [blame] | 393 | case ACPI_STATE_D3_HOT: |
Shaohua Li | ab826ca | 2007-07-20 10:03:22 +0800 | [diff] [blame] | 394 | return PCI_D3hot; |
Lin Ming | 28c2103 | 2011-05-04 22:56:43 +0800 | [diff] [blame] | 395 | case ACPI_STATE_D3_COLD: |
| 396 | return PCI_D3cold; |
Shaohua Li | ab826ca | 2007-07-20 10:03:22 +0800 | [diff] [blame] | 397 | } |
| 398 | return PCI_POWER_ERROR; |
David Shaohua Li | 0f64474 | 2005-03-19 00:15:48 -0500 | [diff] [blame] | 399 | } |
Rafael J. Wysocki | 961d912 | 2008-07-07 03:32:02 +0200 | [diff] [blame] | 400 | |
| 401 | static bool acpi_pci_power_manageable(struct pci_dev *dev) |
| 402 | { |
Rafael J. Wysocki | 85dbb3d | 2014-07-24 01:18:53 +0200 | [diff] [blame] | 403 | struct acpi_device *adev = ACPI_COMPANION(&dev->dev); |
| 404 | return adev ? acpi_device_power_manageable(adev) : false; |
Rafael J. Wysocki | 961d912 | 2008-07-07 03:32:02 +0200 | [diff] [blame] | 405 | } |
David Shaohua Li | 0f64474 | 2005-03-19 00:15:48 -0500 | [diff] [blame] | 406 | |
David Shaohua Li | b913100 | 2005-03-19 00:16:18 -0500 | [diff] [blame] | 407 | static int acpi_pci_set_power_state(struct pci_dev *dev, pci_power_t state) |
| 408 | { |
Rafael J. Wysocki | 85dbb3d | 2014-07-24 01:18:53 +0200 | [diff] [blame] | 409 | struct acpi_device *adev = ACPI_COMPANION(&dev->dev); |
David Brownell | 583c377 | 2008-02-22 21:41:51 -0800 | [diff] [blame] | 410 | static const u8 state_conv[] = { |
| 411 | [PCI_D0] = ACPI_STATE_D0, |
| 412 | [PCI_D1] = ACPI_STATE_D1, |
| 413 | [PCI_D2] = ACPI_STATE_D2, |
Rafael J. Wysocki | fc6504b | 2013-06-14 00:29:50 +0200 | [diff] [blame] | 414 | [PCI_D3hot] = ACPI_STATE_D3_COLD, |
| 415 | [PCI_D3cold] = ACPI_STATE_D3_COLD, |
David Shaohua Li | b913100 | 2005-03-19 00:16:18 -0500 | [diff] [blame] | 416 | }; |
Rafael J. Wysocki | 44e4e66 | 2008-07-07 03:32:52 +0200 | [diff] [blame] | 417 | int error = -EINVAL; |
David Shaohua Li | b913100 | 2005-03-19 00:16:18 -0500 | [diff] [blame] | 418 | |
Shaohua Li | 10b3dca | 2007-07-20 10:03:25 +0800 | [diff] [blame] | 419 | /* If the ACPI device has _EJ0, ignore the device */ |
Rafael J. Wysocki | 85dbb3d | 2014-07-24 01:18:53 +0200 | [diff] [blame] | 420 | if (!adev || acpi_has_method(adev->handle, "_EJ0")) |
Rafael J. Wysocki | 44e4e66 | 2008-07-07 03:32:52 +0200 | [diff] [blame] | 421 | return -ENODEV; |
David Brownell | 583c377 | 2008-02-22 21:41:51 -0800 | [diff] [blame] | 422 | |
| 423 | switch (state) { |
Rafael J. Wysocki | 8b713a8 | 2012-10-24 02:08:38 +0200 | [diff] [blame] | 424 | case PCI_D3cold: |
| 425 | if (dev_pm_qos_flags(&dev->dev, PM_QOS_FLAG_NO_POWER_OFF) == |
| 426 | PM_QOS_FLAGS_ALL) { |
| 427 | error = -EBUSY; |
| 428 | break; |
| 429 | } |
David Brownell | 583c377 | 2008-02-22 21:41:51 -0800 | [diff] [blame] | 430 | case PCI_D0: |
| 431 | case PCI_D1: |
| 432 | case PCI_D2: |
| 433 | case PCI_D3hot: |
Rafael J. Wysocki | 85dbb3d | 2014-07-24 01:18:53 +0200 | [diff] [blame] | 434 | error = acpi_device_set_power(adev, state_conv[state]); |
David Brownell | 583c377 | 2008-02-22 21:41:51 -0800 | [diff] [blame] | 435 | } |
Rafael J. Wysocki | 44e4e66 | 2008-07-07 03:32:52 +0200 | [diff] [blame] | 436 | |
| 437 | if (!error) |
Lan Tianyu | d010e57 | 2013-07-30 10:32:30 +0800 | [diff] [blame] | 438 | 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] | 439 | acpi_power_state_string(state_conv[state])); |
Rafael J. Wysocki | 44e4e66 | 2008-07-07 03:32:52 +0200 | [diff] [blame] | 440 | |
| 441 | return error; |
David Shaohua Li | b913100 | 2005-03-19 00:16:18 -0500 | [diff] [blame] | 442 | } |
| 443 | |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 444 | static bool acpi_pci_can_wakeup(struct pci_dev *dev) |
| 445 | { |
Rafael J. Wysocki | 85dbb3d | 2014-07-24 01:18:53 +0200 | [diff] [blame] | 446 | struct acpi_device *adev = ACPI_COMPANION(&dev->dev); |
| 447 | return adev ? acpi_device_can_wakeup(adev) : false; |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 448 | } |
| 449 | |
Rafael J. Wysocki | 0baed8d | 2009-09-08 23:16:24 +0200 | [diff] [blame] | 450 | static void acpi_pci_propagate_wakeup_enable(struct pci_bus *bus, bool enable) |
| 451 | { |
| 452 | while (bus->parent) { |
Rafael J. Wysocki | dc1a94a | 2009-11-29 16:35:54 +0100 | [diff] [blame] | 453 | if (!acpi_pm_device_sleep_wake(&bus->self->dev, enable)) |
Rafael J. Wysocki | 0baed8d | 2009-09-08 23:16:24 +0200 | [diff] [blame] | 454 | return; |
| 455 | bus = bus->parent; |
| 456 | } |
| 457 | |
| 458 | /* We have reached the root bus. */ |
| 459 | if (bus->bridge) |
| 460 | acpi_pm_device_sleep_wake(bus->bridge, enable); |
| 461 | } |
| 462 | |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 463 | static int acpi_pci_sleep_wake(struct pci_dev *dev, bool enable) |
| 464 | { |
Rafael J. Wysocki | 0baed8d | 2009-09-08 23:16:24 +0200 | [diff] [blame] | 465 | if (acpi_pci_can_wakeup(dev)) |
| 466 | return acpi_pm_device_sleep_wake(&dev->dev, enable); |
| 467 | |
Rafael J. Wysocki | dc1a94a | 2009-11-29 16:35:54 +0100 | [diff] [blame] | 468 | acpi_pci_propagate_wakeup_enable(dev->bus, enable); |
Rafael J. Wysocki | 0baed8d | 2009-09-08 23:16:24 +0200 | [diff] [blame] | 469 | return 0; |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 470 | } |
| 471 | |
Rafael J. Wysocki | b67ea76 | 2010-02-17 23:44:09 +0100 | [diff] [blame] | 472 | static void acpi_pci_propagate_run_wake(struct pci_bus *bus, bool enable) |
| 473 | { |
| 474 | while (bus->parent) { |
| 475 | struct pci_dev *bridge = bus->self; |
| 476 | |
| 477 | if (bridge->pme_interrupt) |
| 478 | return; |
Lin Ming | b24e509 | 2012-03-27 15:43:25 +0800 | [diff] [blame] | 479 | if (!acpi_pm_device_run_wake(&bridge->dev, enable)) |
Rafael J. Wysocki | b67ea76 | 2010-02-17 23:44:09 +0100 | [diff] [blame] | 480 | return; |
| 481 | bus = bus->parent; |
| 482 | } |
| 483 | |
| 484 | /* We have reached the root bus. */ |
| 485 | if (bus->bridge) |
Lin Ming | b24e509 | 2012-03-27 15:43:25 +0800 | [diff] [blame] | 486 | acpi_pm_device_run_wake(bus->bridge, enable); |
Rafael J. Wysocki | b67ea76 | 2010-02-17 23:44:09 +0100 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | static int acpi_pci_run_wake(struct pci_dev *dev, bool enable) |
| 490 | { |
Huang Ying | 448bd85 | 2012-06-23 10:23:51 +0800 | [diff] [blame] | 491 | /* |
| 492 | * Per PCI Express Base Specification Revision 2.0 section |
| 493 | * 5.3.3.2 Link Wakeup, platform support is needed for D3cold |
| 494 | * waking up to power on the main link even if there is PME |
| 495 | * support for D3cold |
| 496 | */ |
| 497 | if (dev->pme_interrupt && !dev->runtime_d3cold) |
Rafael J. Wysocki | b67ea76 | 2010-02-17 23:44:09 +0100 | [diff] [blame] | 498 | return 0; |
| 499 | |
Lin Ming | b24e509 | 2012-03-27 15:43:25 +0800 | [diff] [blame] | 500 | if (!acpi_pm_device_run_wake(&dev->dev, enable)) |
Rafael J. Wysocki | b67ea76 | 2010-02-17 23:44:09 +0100 | [diff] [blame] | 501 | return 0; |
| 502 | |
| 503 | acpi_pci_propagate_run_wake(dev->bus, enable); |
| 504 | return 0; |
| 505 | } |
| 506 | |
Rafael J. Wysocki | 961d912 | 2008-07-07 03:32:02 +0200 | [diff] [blame] | 507 | static struct pci_platform_pm_ops acpi_pci_platform_pm = { |
| 508 | .is_manageable = acpi_pci_power_manageable, |
| 509 | .set_state = acpi_pci_set_power_state, |
| 510 | .choose_state = acpi_pci_choose_state, |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 511 | .sleep_wake = acpi_pci_sleep_wake, |
Rafael J. Wysocki | b67ea76 | 2010-02-17 23:44:09 +0100 | [diff] [blame] | 512 | .run_wake = acpi_pci_run_wake, |
Rafael J. Wysocki | 961d912 | 2008-07-07 03:32:02 +0200 | [diff] [blame] | 513 | }; |
David Shaohua Li | b913100 | 2005-03-19 00:16:18 -0500 | [diff] [blame] | 514 | |
Jiang Liu | 5090d4a | 2013-04-12 05:44:21 +0000 | [diff] [blame] | 515 | void acpi_pci_add_bus(struct pci_bus *bus) |
| 516 | { |
Rafael J. Wysocki | be1c9de | 2013-07-13 23:27:23 +0200 | [diff] [blame] | 517 | if (acpi_pci_disabled || !bus->bridge) |
Jiang Liu | 5090d4a | 2013-04-12 05:44:21 +0000 | [diff] [blame] | 518 | return; |
| 519 | |
Rafael J. Wysocki | be1c9de | 2013-07-13 23:27:23 +0200 | [diff] [blame] | 520 | acpi_pci_slot_enumerate(bus); |
| 521 | acpiphp_enumerate_slots(bus); |
Jiang Liu | 5090d4a | 2013-04-12 05:44:21 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | void acpi_pci_remove_bus(struct pci_bus *bus) |
| 525 | { |
Rafael J. Wysocki | be1c9de | 2013-07-13 23:27:23 +0200 | [diff] [blame] | 526 | if (acpi_pci_disabled || !bus->bridge) |
Jiang Liu | 5090d4a | 2013-04-12 05:44:21 +0000 | [diff] [blame] | 527 | return; |
| 528 | |
Jiang Liu | 3b63aaa | 2013-04-12 05:44:26 +0000 | [diff] [blame] | 529 | acpiphp_remove_slots(bus); |
Jiang Liu | 5c0b04e | 2013-04-12 05:44:24 +0000 | [diff] [blame] | 530 | acpi_pci_slot_remove(bus); |
Jiang Liu | 5090d4a | 2013-04-12 05:44:21 +0000 | [diff] [blame] | 531 | } |
| 532 | |
David Shaohua Li | 84df749f | 2005-03-18 18:53:36 -0500 | [diff] [blame] | 533 | /* ACPI bus type */ |
Rafael J. Wysocki | e3f02c5 | 2013-11-29 16:27:34 +0100 | [diff] [blame] | 534 | static struct acpi_device *acpi_pci_find_companion(struct device *dev) |
David Shaohua Li | 84df749f | 2005-03-18 18:53:36 -0500 | [diff] [blame] | 535 | { |
Rafael J. Wysocki | 60f75b8 | 2013-08-07 22:55:00 +0200 | [diff] [blame] | 536 | struct pci_dev *pci_dev = to_pci_dev(dev); |
Rafael J. Wysocki | 5ce79d2 | 2013-11-28 23:58:08 +0100 | [diff] [blame] | 537 | bool check_children; |
Rafael J. Wysocki | 60f75b8 | 2013-08-07 22:55:00 +0200 | [diff] [blame] | 538 | u64 addr; |
David Shaohua Li | 84df749f | 2005-03-18 18:53:36 -0500 | [diff] [blame] | 539 | |
Yijing Wang | 6788a51 | 2014-05-04 12:23:38 +0800 | [diff] [blame] | 540 | check_children = pci_is_bridge(pci_dev); |
David Shaohua Li | 84df749f | 2005-03-18 18:53:36 -0500 | [diff] [blame] | 541 | /* Please ref to ACPI spec for the syntax of _ADR */ |
| 542 | 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] | 543 | return acpi_find_child_device(ACPI_COMPANION(dev->parent), addr, |
Rafael J. Wysocki | 5ce79d2 | 2013-11-28 23:58:08 +0100 | [diff] [blame] | 544 | check_children); |
David Shaohua Li | 84df749f | 2005-03-18 18:53:36 -0500 | [diff] [blame] | 545 | } |
| 546 | |
Rafael J. Wysocki | 38a9a67 | 2012-12-23 00:02:54 +0100 | [diff] [blame] | 547 | static void pci_acpi_setup(struct device *dev) |
Rafael J. Wysocki | d2e5f0c | 2012-12-23 00:02:44 +0100 | [diff] [blame] | 548 | { |
Rafael J. Wysocki | d2e5f0c | 2012-12-23 00:02:44 +0100 | [diff] [blame] | 549 | struct pci_dev *pci_dev = to_pci_dev(dev); |
Rafael J. Wysocki | f084280 | 2013-12-29 23:37:15 +0100 | [diff] [blame] | 550 | struct acpi_device *adev = ACPI_COMPANION(dev); |
Rafael J. Wysocki | 38a9a67 | 2012-12-23 00:02:54 +0100 | [diff] [blame] | 551 | |
Rafael J. Wysocki | f084280 | 2013-12-29 23:37:15 +0100 | [diff] [blame] | 552 | if (!adev) |
| 553 | return; |
| 554 | |
| 555 | pci_acpi_add_pm_notifier(adev, pci_dev); |
| 556 | if (!adev->wakeup.flags.valid) |
Rafael J. Wysocki | d2e5f0c | 2012-12-23 00:02:44 +0100 | [diff] [blame] | 557 | return; |
| 558 | |
| 559 | device_set_wakeup_capable(dev, true); |
| 560 | acpi_pci_sleep_wake(pci_dev, false); |
Rafael J. Wysocki | d2e5f0c | 2012-12-23 00:02:44 +0100 | [diff] [blame] | 561 | if (adev->wakeup.flags.run_wake) |
| 562 | device_set_run_wake(dev, true); |
| 563 | } |
| 564 | |
Rafael J. Wysocki | 38a9a67 | 2012-12-23 00:02:54 +0100 | [diff] [blame] | 565 | static void pci_acpi_cleanup(struct device *dev) |
Rafael J. Wysocki | d2e5f0c | 2012-12-23 00:02:44 +0100 | [diff] [blame] | 566 | { |
Rafael J. Wysocki | f084280 | 2013-12-29 23:37:15 +0100 | [diff] [blame] | 567 | struct acpi_device *adev = ACPI_COMPANION(dev); |
Rafael J. Wysocki | d2e5f0c | 2012-12-23 00:02:44 +0100 | [diff] [blame] | 568 | |
Rafael J. Wysocki | f084280 | 2013-12-29 23:37:15 +0100 | [diff] [blame] | 569 | if (!adev) |
| 570 | return; |
| 571 | |
| 572 | pci_acpi_remove_pm_notifier(adev); |
| 573 | if (adev->wakeup.flags.valid) { |
Rafael J. Wysocki | d2e5f0c | 2012-12-23 00:02:44 +0100 | [diff] [blame] | 574 | device_set_wakeup_capable(dev, false); |
| 575 | device_set_run_wake(dev, false); |
Rafael J. Wysocki | d2e5f0c | 2012-12-23 00:02:44 +0100 | [diff] [blame] | 576 | } |
| 577 | } |
| 578 | |
Rafael J. Wysocki | 5354009 | 2013-03-03 22:35:20 +0100 | [diff] [blame] | 579 | static bool pci_acpi_bus_match(struct device *dev) |
| 580 | { |
Yijing Wang | 40c368c | 2013-12-05 19:52:53 +0800 | [diff] [blame] | 581 | return dev_is_pci(dev); |
Rafael J. Wysocki | 5354009 | 2013-03-03 22:35:20 +0100 | [diff] [blame] | 582 | } |
| 583 | |
Muthu Kumar | 9c273b9 | 2006-04-28 00:42:21 -0700 | [diff] [blame] | 584 | static struct acpi_bus_type acpi_pci_bus = { |
Rafael J. Wysocki | 5354009 | 2013-03-03 22:35:20 +0100 | [diff] [blame] | 585 | .name = "PCI", |
| 586 | .match = pci_acpi_bus_match, |
Rafael J. Wysocki | e3f02c5 | 2013-11-29 16:27:34 +0100 | [diff] [blame] | 587 | .find_companion = acpi_pci_find_companion, |
Rafael J. Wysocki | 38a9a67 | 2012-12-23 00:02:54 +0100 | [diff] [blame] | 588 | .setup = pci_acpi_setup, |
| 589 | .cleanup = pci_acpi_cleanup, |
David Shaohua Li | 84df749f | 2005-03-18 18:53:36 -0500 | [diff] [blame] | 590 | }; |
| 591 | |
Muthu Kumar | 9c273b9 | 2006-04-28 00:42:21 -0700 | [diff] [blame] | 592 | static int __init acpi_pci_init(void) |
David Shaohua Li | 84df749f | 2005-03-18 18:53:36 -0500 | [diff] [blame] | 593 | { |
| 594 | int ret; |
| 595 | |
Bob Moore | 993958f | 2009-02-03 15:14:33 +0800 | [diff] [blame] | 596 | if (acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_MSI) { |
Yijing Wang | e7d4515 | 2013-05-28 16:03:46 +0800 | [diff] [blame] | 597 | 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] | 598 | pci_no_msi(); |
| 599 | } |
Shaohua Li | 5fde244 | 2008-07-23 10:32:24 +0800 | [diff] [blame] | 600 | |
Bob Moore | 993958f | 2009-02-03 15:14:33 +0800 | [diff] [blame] | 601 | if (acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_ASPM) { |
Yijing Wang | e7d4515 | 2013-05-28 16:03:46 +0800 | [diff] [blame] | 602 | 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] | 603 | pcie_no_aspm(); |
| 604 | } |
| 605 | |
Muthu Kumar | 9c273b9 | 2006-04-28 00:42:21 -0700 | [diff] [blame] | 606 | ret = register_acpi_bus_type(&acpi_pci_bus); |
David Shaohua Li | 84df749f | 2005-03-18 18:53:36 -0500 | [diff] [blame] | 607 | if (ret) |
| 608 | return 0; |
Jiang Liu | 5c0b04e | 2013-04-12 05:44:24 +0000 | [diff] [blame] | 609 | |
Rafael J. Wysocki | 961d912 | 2008-07-07 03:32:02 +0200 | [diff] [blame] | 610 | pci_set_platform_pm(&acpi_pci_platform_pm); |
Jiang Liu | 5c0b04e | 2013-04-12 05:44:24 +0000 | [diff] [blame] | 611 | acpi_pci_slot_init(); |
Jiang Liu | 3b63aaa | 2013-04-12 05:44:26 +0000 | [diff] [blame] | 612 | acpiphp_init(); |
Jiang Liu | 5c0b04e | 2013-04-12 05:44:24 +0000 | [diff] [blame] | 613 | |
David Shaohua Li | 84df749f | 2005-03-18 18:53:36 -0500 | [diff] [blame] | 614 | return 0; |
| 615 | } |
Muthu Kumar | 9c273b9 | 2006-04-28 00:42:21 -0700 | [diff] [blame] | 616 | arch_initcall(acpi_pci_init); |