pci-acpi.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. /*
  2. * File: pci-acpi.c
  3. * Purpose: Provide PCI support in ACPI
  4. *
  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.
  8. */
  9. #include <linux/delay.h>
  10. #include <linux/init.h>
  11. #include <linux/pci.h>
  12. #include <linux/pci_hotplug.h>
  13. #include <linux/module.h>
  14. #include <linux/pci-aspm.h>
  15. #include <linux/pci-acpi.h>
  16. #include <linux/pm_runtime.h>
  17. #include <linux/pm_qos.h>
  18. #include "pci.h"
  19. phys_addr_t acpi_pci_root_get_mcfg_addr(acpi_handle handle)
  20. {
  21. acpi_status status = AE_NOT_EXIST;
  22. unsigned long long mcfg_addr;
  23. if (handle)
  24. status = acpi_evaluate_integer(handle, METHOD_NAME__CBA,
  25. NULL, &mcfg_addr);
  26. if (ACPI_FAILURE(status))
  27. return 0;
  28. return (phys_addr_t)mcfg_addr;
  29. }
  30. static acpi_status decode_type0_hpx_record(union acpi_object *record,
  31. struct hotplug_params *hpx)
  32. {
  33. int i;
  34. union acpi_object *fields = record->package.elements;
  35. u32 revision = fields[1].integer.value;
  36. switch (revision) {
  37. case 1:
  38. if (record->package.count != 6)
  39. return AE_ERROR;
  40. for (i = 2; i < 6; i++)
  41. if (fields[i].type != ACPI_TYPE_INTEGER)
  42. return AE_ERROR;
  43. hpx->t0 = &hpx->type0_data;
  44. hpx->t0->revision = revision;
  45. hpx->t0->cache_line_size = fields[2].integer.value;
  46. hpx->t0->latency_timer = fields[3].integer.value;
  47. hpx->t0->enable_serr = fields[4].integer.value;
  48. hpx->t0->enable_perr = fields[5].integer.value;
  49. break;
  50. default:
  51. printk(KERN_WARNING
  52. "%s: Type 0 Revision %d record not supported\n",
  53. __func__, revision);
  54. return AE_ERROR;
  55. }
  56. return AE_OK;
  57. }
  58. static acpi_status decode_type1_hpx_record(union acpi_object *record,
  59. struct hotplug_params *hpx)
  60. {
  61. int i;
  62. union acpi_object *fields = record->package.elements;
  63. u32 revision = fields[1].integer.value;
  64. switch (revision) {
  65. case 1:
  66. if (record->package.count != 5)
  67. return AE_ERROR;
  68. for (i = 2; i < 5; i++)
  69. if (fields[i].type != ACPI_TYPE_INTEGER)
  70. return AE_ERROR;
  71. hpx->t1 = &hpx->type1_data;
  72. hpx->t1->revision = revision;
  73. hpx->t1->max_mem_read = fields[2].integer.value;
  74. hpx->t1->avg_max_split = fields[3].integer.value;
  75. hpx->t1->tot_max_split = fields[4].integer.value;
  76. break;
  77. default:
  78. printk(KERN_WARNING
  79. "%s: Type 1 Revision %d record not supported\n",
  80. __func__, revision);
  81. return AE_ERROR;
  82. }
  83. return AE_OK;
  84. }
  85. static acpi_status decode_type2_hpx_record(union acpi_object *record,
  86. struct hotplug_params *hpx)
  87. {
  88. int i;
  89. union acpi_object *fields = record->package.elements;
  90. u32 revision = fields[1].integer.value;
  91. switch (revision) {
  92. case 1:
  93. if (record->package.count != 18)
  94. return AE_ERROR;
  95. for (i = 2; i < 18; i++)
  96. if (fields[i].type != ACPI_TYPE_INTEGER)
  97. return AE_ERROR;
  98. hpx->t2 = &hpx->type2_data;
  99. hpx->t2->revision = revision;
  100. hpx->t2->unc_err_mask_and = fields[2].integer.value;
  101. hpx->t2->unc_err_mask_or = fields[3].integer.value;
  102. hpx->t2->unc_err_sever_and = fields[4].integer.value;
  103. hpx->t2->unc_err_sever_or = fields[5].integer.value;
  104. hpx->t2->cor_err_mask_and = fields[6].integer.value;
  105. hpx->t2->cor_err_mask_or = fields[7].integer.value;
  106. hpx->t2->adv_err_cap_and = fields[8].integer.value;
  107. hpx->t2->adv_err_cap_or = fields[9].integer.value;
  108. hpx->t2->pci_exp_devctl_and = fields[10].integer.value;
  109. hpx->t2->pci_exp_devctl_or = fields[11].integer.value;
  110. hpx->t2->pci_exp_lnkctl_and = fields[12].integer.value;
  111. hpx->t2->pci_exp_lnkctl_or = fields[13].integer.value;
  112. hpx->t2->sec_unc_err_sever_and = fields[14].integer.value;
  113. hpx->t2->sec_unc_err_sever_or = fields[15].integer.value;
  114. hpx->t2->sec_unc_err_mask_and = fields[16].integer.value;
  115. hpx->t2->sec_unc_err_mask_or = fields[17].integer.value;
  116. break;
  117. default:
  118. printk(KERN_WARNING
  119. "%s: Type 2 Revision %d record not supported\n",
  120. __func__, revision);
  121. return AE_ERROR;
  122. }
  123. return AE_OK;
  124. }
  125. static acpi_status acpi_run_hpx(acpi_handle handle, struct hotplug_params *hpx)
  126. {
  127. acpi_status status;
  128. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  129. union acpi_object *package, *record, *fields;
  130. u32 type;
  131. int i;
  132. /* Clear the return buffer with zeros */
  133. memset(hpx, 0, sizeof(struct hotplug_params));
  134. status = acpi_evaluate_object(handle, "_HPX", NULL, &buffer);
  135. if (ACPI_FAILURE(status))
  136. return status;
  137. package = (union acpi_object *)buffer.pointer;
  138. if (package->type != ACPI_TYPE_PACKAGE) {
  139. status = AE_ERROR;
  140. goto exit;
  141. }
  142. for (i = 0; i < package->package.count; i++) {
  143. record = &package->package.elements[i];
  144. if (record->type != ACPI_TYPE_PACKAGE) {
  145. status = AE_ERROR;
  146. goto exit;
  147. }
  148. fields = record->package.elements;
  149. if (fields[0].type != ACPI_TYPE_INTEGER ||
  150. fields[1].type != ACPI_TYPE_INTEGER) {
  151. status = AE_ERROR;
  152. goto exit;
  153. }
  154. type = fields[0].integer.value;
  155. switch (type) {
  156. case 0:
  157. status = decode_type0_hpx_record(record, hpx);
  158. if (ACPI_FAILURE(status))
  159. goto exit;
  160. break;
  161. case 1:
  162. status = decode_type1_hpx_record(record, hpx);
  163. if (ACPI_FAILURE(status))
  164. goto exit;
  165. break;
  166. case 2:
  167. status = decode_type2_hpx_record(record, hpx);
  168. if (ACPI_FAILURE(status))
  169. goto exit;
  170. break;
  171. default:
  172. printk(KERN_ERR "%s: Type %d record not supported\n",
  173. __func__, type);
  174. status = AE_ERROR;
  175. goto exit;
  176. }
  177. }
  178. exit:
  179. kfree(buffer.pointer);
  180. return status;
  181. }
  182. static acpi_status acpi_run_hpp(acpi_handle handle, struct hotplug_params *hpp)
  183. {
  184. acpi_status status;
  185. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  186. union acpi_object *package, *fields;
  187. int i;
  188. memset(hpp, 0, sizeof(struct hotplug_params));
  189. status = acpi_evaluate_object(handle, "_HPP", NULL, &buffer);
  190. if (ACPI_FAILURE(status))
  191. return status;
  192. package = (union acpi_object *) buffer.pointer;
  193. if (package->type != ACPI_TYPE_PACKAGE ||
  194. package->package.count != 4) {
  195. status = AE_ERROR;
  196. goto exit;
  197. }
  198. fields = package->package.elements;
  199. for (i = 0; i < 4; i++) {
  200. if (fields[i].type != ACPI_TYPE_INTEGER) {
  201. status = AE_ERROR;
  202. goto exit;
  203. }
  204. }
  205. hpp->t0 = &hpp->type0_data;
  206. hpp->t0->revision = 1;
  207. hpp->t0->cache_line_size = fields[0].integer.value;
  208. hpp->t0->latency_timer = fields[1].integer.value;
  209. hpp->t0->enable_serr = fields[2].integer.value;
  210. hpp->t0->enable_perr = fields[3].integer.value;
  211. exit:
  212. kfree(buffer.pointer);
  213. return status;
  214. }
  215. /* pci_get_hp_params
  216. *
  217. * @dev - the pci_dev for which we want parameters
  218. * @hpp - allocated by the caller
  219. */
  220. int pci_get_hp_params(struct pci_dev *dev, struct hotplug_params *hpp)
  221. {
  222. acpi_status status;
  223. acpi_handle handle, phandle;
  224. struct pci_bus *pbus;
  225. handle = NULL;
  226. for (pbus = dev->bus; pbus; pbus = pbus->parent) {
  227. handle = acpi_pci_get_bridge_handle(pbus);
  228. if (handle)
  229. break;
  230. }
  231. /*
  232. * _HPP settings apply to all child buses, until another _HPP is
  233. * encountered. If we don't find an _HPP for the input pci dev,
  234. * look for it in the parent device scope since that would apply to
  235. * this pci dev.
  236. */
  237. while (handle) {
  238. status = acpi_run_hpx(handle, hpp);
  239. if (ACPI_SUCCESS(status))
  240. return 0;
  241. status = acpi_run_hpp(handle, hpp);
  242. if (ACPI_SUCCESS(status))
  243. return 0;
  244. if (acpi_is_root_bridge(handle))
  245. break;
  246. status = acpi_get_parent(handle, &phandle);
  247. if (ACPI_FAILURE(status))
  248. break;
  249. handle = phandle;
  250. }
  251. return -ENODEV;
  252. }
  253. EXPORT_SYMBOL_GPL(pci_get_hp_params);
  254. /**
  255. * pci_acpi_wake_bus - Root bus wakeup notification fork function.
  256. * @work: Work item to handle.
  257. */
  258. static void pci_acpi_wake_bus(struct work_struct *work)
  259. {
  260. struct acpi_device *adev;
  261. struct acpi_pci_root *root;
  262. adev = container_of(work, struct acpi_device, wakeup.context.work);
  263. root = acpi_driver_data(adev);
  264. pci_pme_wakeup_bus(root->bus);
  265. }
  266. /**
  267. * pci_acpi_wake_dev - PCI device wakeup notification work function.
  268. * @handle: ACPI handle of a device the notification is for.
  269. * @work: Work item to handle.
  270. */
  271. static void pci_acpi_wake_dev(struct work_struct *work)
  272. {
  273. struct acpi_device_wakeup_context *context;
  274. struct pci_dev *pci_dev;
  275. context = container_of(work, struct acpi_device_wakeup_context, work);
  276. pci_dev = to_pci_dev(context->dev);
  277. if (pci_dev->pme_poll)
  278. pci_dev->pme_poll = false;
  279. if (pci_dev->current_state == PCI_D3cold) {
  280. pci_wakeup_event(pci_dev);
  281. pm_runtime_resume(&pci_dev->dev);
  282. return;
  283. }
  284. /* Clear PME Status if set. */
  285. if (pci_dev->pme_support)
  286. pci_check_pme_status(pci_dev);
  287. pci_wakeup_event(pci_dev);
  288. pm_runtime_resume(&pci_dev->dev);
  289. if (pci_dev->subordinate)
  290. pci_pme_wakeup_bus(pci_dev->subordinate);
  291. }
  292. /**
  293. * pci_acpi_add_bus_pm_notifier - Register PM notifier for root PCI bus.
  294. * @dev: PCI root bridge ACPI device.
  295. */
  296. acpi_status pci_acpi_add_bus_pm_notifier(struct acpi_device *dev)
  297. {
  298. return acpi_add_pm_notifier(dev, NULL, pci_acpi_wake_bus);
  299. }
  300. /**
  301. * pci_acpi_add_pm_notifier - Register PM notifier for given PCI device.
  302. * @dev: ACPI device to add the notifier for.
  303. * @pci_dev: PCI device to check for the PME status if an event is signaled.
  304. */
  305. acpi_status pci_acpi_add_pm_notifier(struct acpi_device *dev,
  306. struct pci_dev *pci_dev)
  307. {
  308. return acpi_add_pm_notifier(dev, &pci_dev->dev, pci_acpi_wake_dev);
  309. }
  310. /*
  311. * _SxD returns the D-state with the highest power
  312. * (lowest D-state number) supported in the S-state "x".
  313. *
  314. * If the devices does not have a _PRW
  315. * (Power Resources for Wake) supporting system wakeup from "x"
  316. * then the OS is free to choose a lower power (higher number
  317. * D-state) than the return value from _SxD.
  318. *
  319. * But if _PRW is enabled at S-state "x", the OS
  320. * must not choose a power lower than _SxD --
  321. * unless the device has an _SxW method specifying
  322. * the lowest power (highest D-state number) the device
  323. * may enter while still able to wake the system.
  324. *
  325. * ie. depending on global OS policy:
  326. *
  327. * if (_PRW at S-state x)
  328. * choose from highest power _SxD to lowest power _SxW
  329. * else // no _PRW at S-state x
  330. * choose highest power _SxD or any lower power
  331. */
  332. static pci_power_t acpi_pci_choose_state(struct pci_dev *pdev)
  333. {
  334. int acpi_state, d_max;
  335. if (pdev->no_d3cold)
  336. d_max = ACPI_STATE_D3_HOT;
  337. else
  338. d_max = ACPI_STATE_D3_COLD;
  339. acpi_state = acpi_pm_device_sleep_state(&pdev->dev, NULL, d_max);
  340. if (acpi_state < 0)
  341. return PCI_POWER_ERROR;
  342. switch (acpi_state) {
  343. case ACPI_STATE_D0:
  344. return PCI_D0;
  345. case ACPI_STATE_D1:
  346. return PCI_D1;
  347. case ACPI_STATE_D2:
  348. return PCI_D2;
  349. case ACPI_STATE_D3_HOT:
  350. return PCI_D3hot;
  351. case ACPI_STATE_D3_COLD:
  352. return PCI_D3cold;
  353. }
  354. return PCI_POWER_ERROR;
  355. }
  356. static bool acpi_pci_power_manageable(struct pci_dev *dev)
  357. {
  358. struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
  359. return adev ? acpi_device_power_manageable(adev) : false;
  360. }
  361. static int acpi_pci_set_power_state(struct pci_dev *dev, pci_power_t state)
  362. {
  363. struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
  364. static const u8 state_conv[] = {
  365. [PCI_D0] = ACPI_STATE_D0,
  366. [PCI_D1] = ACPI_STATE_D1,
  367. [PCI_D2] = ACPI_STATE_D2,
  368. [PCI_D3hot] = ACPI_STATE_D3_COLD,
  369. [PCI_D3cold] = ACPI_STATE_D3_COLD,
  370. };
  371. int error = -EINVAL;
  372. /* If the ACPI device has _EJ0, ignore the device */
  373. if (!adev || acpi_has_method(adev->handle, "_EJ0"))
  374. return -ENODEV;
  375. switch (state) {
  376. case PCI_D3cold:
  377. if (dev_pm_qos_flags(&dev->dev, PM_QOS_FLAG_NO_POWER_OFF) ==
  378. PM_QOS_FLAGS_ALL) {
  379. error = -EBUSY;
  380. break;
  381. }
  382. case PCI_D0:
  383. case PCI_D1:
  384. case PCI_D2:
  385. case PCI_D3hot:
  386. error = acpi_device_set_power(adev, state_conv[state]);
  387. }
  388. if (!error)
  389. dev_dbg(&dev->dev, "power state changed by ACPI to %s\n",
  390. acpi_power_state_string(state_conv[state]));
  391. return error;
  392. }
  393. static bool acpi_pci_can_wakeup(struct pci_dev *dev)
  394. {
  395. struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
  396. return adev ? acpi_device_can_wakeup(adev) : false;
  397. }
  398. static void acpi_pci_propagate_wakeup_enable(struct pci_bus *bus, bool enable)
  399. {
  400. while (bus->parent) {
  401. if (!acpi_pm_device_sleep_wake(&bus->self->dev, enable))
  402. return;
  403. bus = bus->parent;
  404. }
  405. /* We have reached the root bus. */
  406. if (bus->bridge)
  407. acpi_pm_device_sleep_wake(bus->bridge, enable);
  408. }
  409. static int acpi_pci_sleep_wake(struct pci_dev *dev, bool enable)
  410. {
  411. if (acpi_pci_can_wakeup(dev))
  412. return acpi_pm_device_sleep_wake(&dev->dev, enable);
  413. acpi_pci_propagate_wakeup_enable(dev->bus, enable);
  414. return 0;
  415. }
  416. static void acpi_pci_propagate_run_wake(struct pci_bus *bus, bool enable)
  417. {
  418. while (bus->parent) {
  419. struct pci_dev *bridge = bus->self;
  420. if (bridge->pme_interrupt)
  421. return;
  422. if (!acpi_pm_device_run_wake(&bridge->dev, enable))
  423. return;
  424. bus = bus->parent;
  425. }
  426. /* We have reached the root bus. */
  427. if (bus->bridge)
  428. acpi_pm_device_run_wake(bus->bridge, enable);
  429. }
  430. static int acpi_pci_run_wake(struct pci_dev *dev, bool enable)
  431. {
  432. /*
  433. * Per PCI Express Base Specification Revision 2.0 section
  434. * 5.3.3.2 Link Wakeup, platform support is needed for D3cold
  435. * waking up to power on the main link even if there is PME
  436. * support for D3cold
  437. */
  438. if (dev->pme_interrupt && !dev->runtime_d3cold)
  439. return 0;
  440. if (!acpi_pm_device_run_wake(&dev->dev, enable))
  441. return 0;
  442. acpi_pci_propagate_run_wake(dev->bus, enable);
  443. return 0;
  444. }
  445. static struct pci_platform_pm_ops acpi_pci_platform_pm = {
  446. .is_manageable = acpi_pci_power_manageable,
  447. .set_state = acpi_pci_set_power_state,
  448. .choose_state = acpi_pci_choose_state,
  449. .sleep_wake = acpi_pci_sleep_wake,
  450. .run_wake = acpi_pci_run_wake,
  451. };
  452. void acpi_pci_add_bus(struct pci_bus *bus)
  453. {
  454. if (acpi_pci_disabled || !bus->bridge)
  455. return;
  456. acpi_pci_slot_enumerate(bus);
  457. acpiphp_enumerate_slots(bus);
  458. }
  459. void acpi_pci_remove_bus(struct pci_bus *bus)
  460. {
  461. if (acpi_pci_disabled || !bus->bridge)
  462. return;
  463. acpiphp_remove_slots(bus);
  464. acpi_pci_slot_remove(bus);
  465. }
  466. /* ACPI bus type */
  467. static struct acpi_device *acpi_pci_find_companion(struct device *dev)
  468. {
  469. struct pci_dev *pci_dev = to_pci_dev(dev);
  470. bool check_children;
  471. u64 addr;
  472. check_children = pci_is_bridge(pci_dev);
  473. /* Please ref to ACPI spec for the syntax of _ADR */
  474. addr = (PCI_SLOT(pci_dev->devfn) << 16) | PCI_FUNC(pci_dev->devfn);
  475. return acpi_find_child_device(ACPI_COMPANION(dev->parent), addr,
  476. check_children);
  477. }
  478. static void pci_acpi_setup(struct device *dev)
  479. {
  480. struct pci_dev *pci_dev = to_pci_dev(dev);
  481. struct acpi_device *adev = ACPI_COMPANION(dev);
  482. if (!adev)
  483. return;
  484. pci_acpi_add_pm_notifier(adev, pci_dev);
  485. if (!adev->wakeup.flags.valid)
  486. return;
  487. device_set_wakeup_capable(dev, true);
  488. acpi_pci_sleep_wake(pci_dev, false);
  489. if (adev->wakeup.flags.run_wake)
  490. device_set_run_wake(dev, true);
  491. }
  492. static void pci_acpi_cleanup(struct device *dev)
  493. {
  494. struct acpi_device *adev = ACPI_COMPANION(dev);
  495. if (!adev)
  496. return;
  497. pci_acpi_remove_pm_notifier(adev);
  498. if (adev->wakeup.flags.valid) {
  499. device_set_wakeup_capable(dev, false);
  500. device_set_run_wake(dev, false);
  501. }
  502. }
  503. static bool pci_acpi_bus_match(struct device *dev)
  504. {
  505. return dev_is_pci(dev);
  506. }
  507. static struct acpi_bus_type acpi_pci_bus = {
  508. .name = "PCI",
  509. .match = pci_acpi_bus_match,
  510. .find_companion = acpi_pci_find_companion,
  511. .setup = pci_acpi_setup,
  512. .cleanup = pci_acpi_cleanup,
  513. };
  514. static int __init acpi_pci_init(void)
  515. {
  516. int ret;
  517. if (acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_MSI) {
  518. pr_info("ACPI FADT declares the system doesn't support MSI, so disable it\n");
  519. pci_no_msi();
  520. }
  521. if (acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_ASPM) {
  522. pr_info("ACPI FADT declares the system doesn't support PCIe ASPM, so disable it\n");
  523. pcie_no_aspm();
  524. }
  525. ret = register_acpi_bus_type(&acpi_pci_bus);
  526. if (ret)
  527. return 0;
  528. pci_set_platform_pm(&acpi_pci_platform_pm);
  529. acpi_pci_slot_init();
  530. acpiphp_init();
  531. return 0;
  532. }
  533. arch_initcall(acpi_pci_init);