pci-acpi.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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/irqdomain.h>
  12. #include <linux/pci.h>
  13. #include <linux/msi.h>
  14. #include <linux/pci_hotplug.h>
  15. #include <linux/module.h>
  16. #include <linux/pci-aspm.h>
  17. #include <linux/pci-acpi.h>
  18. #include <linux/pm_runtime.h>
  19. #include <linux/pm_qos.h>
  20. #include "pci.h"
  21. /*
  22. * The UUID is defined in the PCI Firmware Specification available here:
  23. * https://www.pcisig.com/members/downloads/pcifw_r3_1_13Dec10.pdf
  24. */
  25. const u8 pci_acpi_dsm_uuid[] = {
  26. 0xd0, 0x37, 0xc9, 0xe5, 0x53, 0x35, 0x7a, 0x4d,
  27. 0x91, 0x17, 0xea, 0x4d, 0x19, 0xc3, 0x43, 0x4d
  28. };
  29. phys_addr_t acpi_pci_root_get_mcfg_addr(acpi_handle handle)
  30. {
  31. acpi_status status = AE_NOT_EXIST;
  32. unsigned long long mcfg_addr;
  33. if (handle)
  34. status = acpi_evaluate_integer(handle, METHOD_NAME__CBA,
  35. NULL, &mcfg_addr);
  36. if (ACPI_FAILURE(status))
  37. return 0;
  38. return (phys_addr_t)mcfg_addr;
  39. }
  40. static acpi_status decode_type0_hpx_record(union acpi_object *record,
  41. struct hotplug_params *hpx)
  42. {
  43. int i;
  44. union acpi_object *fields = record->package.elements;
  45. u32 revision = fields[1].integer.value;
  46. switch (revision) {
  47. case 1:
  48. if (record->package.count != 6)
  49. return AE_ERROR;
  50. for (i = 2; i < 6; i++)
  51. if (fields[i].type != ACPI_TYPE_INTEGER)
  52. return AE_ERROR;
  53. hpx->t0 = &hpx->type0_data;
  54. hpx->t0->revision = revision;
  55. hpx->t0->cache_line_size = fields[2].integer.value;
  56. hpx->t0->latency_timer = fields[3].integer.value;
  57. hpx->t0->enable_serr = fields[4].integer.value;
  58. hpx->t0->enable_perr = fields[5].integer.value;
  59. break;
  60. default:
  61. printk(KERN_WARNING
  62. "%s: Type 0 Revision %d record not supported\n",
  63. __func__, revision);
  64. return AE_ERROR;
  65. }
  66. return AE_OK;
  67. }
  68. static acpi_status decode_type1_hpx_record(union acpi_object *record,
  69. struct hotplug_params *hpx)
  70. {
  71. int i;
  72. union acpi_object *fields = record->package.elements;
  73. u32 revision = fields[1].integer.value;
  74. switch (revision) {
  75. case 1:
  76. if (record->package.count != 5)
  77. return AE_ERROR;
  78. for (i = 2; i < 5; i++)
  79. if (fields[i].type != ACPI_TYPE_INTEGER)
  80. return AE_ERROR;
  81. hpx->t1 = &hpx->type1_data;
  82. hpx->t1->revision = revision;
  83. hpx->t1->max_mem_read = fields[2].integer.value;
  84. hpx->t1->avg_max_split = fields[3].integer.value;
  85. hpx->t1->tot_max_split = fields[4].integer.value;
  86. break;
  87. default:
  88. printk(KERN_WARNING
  89. "%s: Type 1 Revision %d record not supported\n",
  90. __func__, revision);
  91. return AE_ERROR;
  92. }
  93. return AE_OK;
  94. }
  95. static acpi_status decode_type2_hpx_record(union acpi_object *record,
  96. struct hotplug_params *hpx)
  97. {
  98. int i;
  99. union acpi_object *fields = record->package.elements;
  100. u32 revision = fields[1].integer.value;
  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. static acpi_status acpi_run_hpx(acpi_handle handle, struct hotplug_params *hpx)
  136. {
  137. acpi_status status;
  138. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  139. union acpi_object *package, *record, *fields;
  140. u32 type;
  141. int i;
  142. /* Clear the return buffer with zeros */
  143. memset(hpx, 0, sizeof(struct hotplug_params));
  144. status = acpi_evaluate_object(handle, "_HPX", NULL, &buffer);
  145. if (ACPI_FAILURE(status))
  146. return status;
  147. package = (union acpi_object *)buffer.pointer;
  148. if (package->type != ACPI_TYPE_PACKAGE) {
  149. status = AE_ERROR;
  150. goto exit;
  151. }
  152. for (i = 0; i < package->package.count; i++) {
  153. record = &package->package.elements[i];
  154. if (record->type != ACPI_TYPE_PACKAGE) {
  155. status = AE_ERROR;
  156. goto exit;
  157. }
  158. fields = record->package.elements;
  159. if (fields[0].type != ACPI_TYPE_INTEGER ||
  160. fields[1].type != ACPI_TYPE_INTEGER) {
  161. status = AE_ERROR;
  162. goto exit;
  163. }
  164. type = fields[0].integer.value;
  165. switch (type) {
  166. case 0:
  167. status = decode_type0_hpx_record(record, hpx);
  168. if (ACPI_FAILURE(status))
  169. goto exit;
  170. break;
  171. case 1:
  172. status = decode_type1_hpx_record(record, hpx);
  173. if (ACPI_FAILURE(status))
  174. goto exit;
  175. break;
  176. case 2:
  177. status = decode_type2_hpx_record(record, hpx);
  178. if (ACPI_FAILURE(status))
  179. goto exit;
  180. break;
  181. default:
  182. printk(KERN_ERR "%s: Type %d record not supported\n",
  183. __func__, type);
  184. status = AE_ERROR;
  185. goto exit;
  186. }
  187. }
  188. exit:
  189. kfree(buffer.pointer);
  190. return status;
  191. }
  192. static acpi_status acpi_run_hpp(acpi_handle handle, struct hotplug_params *hpp)
  193. {
  194. acpi_status status;
  195. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  196. union acpi_object *package, *fields;
  197. int i;
  198. memset(hpp, 0, sizeof(struct hotplug_params));
  199. status = acpi_evaluate_object(handle, "_HPP", NULL, &buffer);
  200. if (ACPI_FAILURE(status))
  201. return status;
  202. package = (union acpi_object *) buffer.pointer;
  203. if (package->type != ACPI_TYPE_PACKAGE ||
  204. package->package.count != 4) {
  205. status = AE_ERROR;
  206. goto exit;
  207. }
  208. fields = package->package.elements;
  209. for (i = 0; i < 4; i++) {
  210. if (fields[i].type != ACPI_TYPE_INTEGER) {
  211. status = AE_ERROR;
  212. goto exit;
  213. }
  214. }
  215. hpp->t0 = &hpp->type0_data;
  216. hpp->t0->revision = 1;
  217. hpp->t0->cache_line_size = fields[0].integer.value;
  218. hpp->t0->latency_timer = fields[1].integer.value;
  219. hpp->t0->enable_serr = fields[2].integer.value;
  220. hpp->t0->enable_perr = fields[3].integer.value;
  221. exit:
  222. kfree(buffer.pointer);
  223. return status;
  224. }
  225. /* pci_get_hp_params
  226. *
  227. * @dev - the pci_dev for which we want parameters
  228. * @hpp - allocated by the caller
  229. */
  230. int pci_get_hp_params(struct pci_dev *dev, struct hotplug_params *hpp)
  231. {
  232. acpi_status status;
  233. acpi_handle handle, phandle;
  234. struct pci_bus *pbus;
  235. if (acpi_pci_disabled)
  236. return -ENODEV;
  237. handle = NULL;
  238. for (pbus = dev->bus; pbus; pbus = pbus->parent) {
  239. handle = acpi_pci_get_bridge_handle(pbus);
  240. if (handle)
  241. break;
  242. }
  243. /*
  244. * _HPP settings apply to all child buses, until another _HPP is
  245. * encountered. If we don't find an _HPP for the input pci dev,
  246. * look for it in the parent device scope since that would apply to
  247. * this pci dev.
  248. */
  249. while (handle) {
  250. status = acpi_run_hpx(handle, hpp);
  251. if (ACPI_SUCCESS(status))
  252. return 0;
  253. status = acpi_run_hpp(handle, hpp);
  254. if (ACPI_SUCCESS(status))
  255. return 0;
  256. if (acpi_is_root_bridge(handle))
  257. break;
  258. status = acpi_get_parent(handle, &phandle);
  259. if (ACPI_FAILURE(status))
  260. break;
  261. handle = phandle;
  262. }
  263. return -ENODEV;
  264. }
  265. EXPORT_SYMBOL_GPL(pci_get_hp_params);
  266. /**
  267. * pci_acpi_wake_bus - Root bus wakeup notification fork function.
  268. * @work: Work item to handle.
  269. */
  270. static void pci_acpi_wake_bus(struct work_struct *work)
  271. {
  272. struct acpi_device *adev;
  273. struct acpi_pci_root *root;
  274. adev = container_of(work, struct acpi_device, wakeup.context.work);
  275. root = acpi_driver_data(adev);
  276. pci_pme_wakeup_bus(root->bus);
  277. }
  278. /**
  279. * pci_acpi_wake_dev - PCI device wakeup notification work function.
  280. * @handle: ACPI handle of a device the notification is for.
  281. * @work: Work item to handle.
  282. */
  283. static void pci_acpi_wake_dev(struct work_struct *work)
  284. {
  285. struct acpi_device_wakeup_context *context;
  286. struct pci_dev *pci_dev;
  287. context = container_of(work, struct acpi_device_wakeup_context, work);
  288. pci_dev = to_pci_dev(context->dev);
  289. if (pci_dev->pme_poll)
  290. pci_dev->pme_poll = false;
  291. if (pci_dev->current_state == PCI_D3cold) {
  292. pci_wakeup_event(pci_dev);
  293. pm_runtime_resume(&pci_dev->dev);
  294. return;
  295. }
  296. /* Clear PME Status if set. */
  297. if (pci_dev->pme_support)
  298. pci_check_pme_status(pci_dev);
  299. pci_wakeup_event(pci_dev);
  300. pm_runtime_resume(&pci_dev->dev);
  301. pci_pme_wakeup_bus(pci_dev->subordinate);
  302. }
  303. /**
  304. * pci_acpi_add_bus_pm_notifier - Register PM notifier for root PCI bus.
  305. * @dev: PCI root bridge ACPI device.
  306. */
  307. acpi_status pci_acpi_add_bus_pm_notifier(struct acpi_device *dev)
  308. {
  309. return acpi_add_pm_notifier(dev, NULL, pci_acpi_wake_bus);
  310. }
  311. /**
  312. * pci_acpi_add_pm_notifier - Register PM notifier for given PCI device.
  313. * @dev: ACPI device to add the notifier for.
  314. * @pci_dev: PCI device to check for the PME status if an event is signaled.
  315. */
  316. acpi_status pci_acpi_add_pm_notifier(struct acpi_device *dev,
  317. struct pci_dev *pci_dev)
  318. {
  319. return acpi_add_pm_notifier(dev, &pci_dev->dev, pci_acpi_wake_dev);
  320. }
  321. /*
  322. * _SxD returns the D-state with the highest power
  323. * (lowest D-state number) supported in the S-state "x".
  324. *
  325. * If the devices does not have a _PRW
  326. * (Power Resources for Wake) supporting system wakeup from "x"
  327. * then the OS is free to choose a lower power (higher number
  328. * D-state) than the return value from _SxD.
  329. *
  330. * But if _PRW is enabled at S-state "x", the OS
  331. * must not choose a power lower than _SxD --
  332. * unless the device has an _SxW method specifying
  333. * the lowest power (highest D-state number) the device
  334. * may enter while still able to wake the system.
  335. *
  336. * ie. depending on global OS policy:
  337. *
  338. * if (_PRW at S-state x)
  339. * choose from highest power _SxD to lowest power _SxW
  340. * else // no _PRW at S-state x
  341. * choose highest power _SxD or any lower power
  342. */
  343. static pci_power_t acpi_pci_choose_state(struct pci_dev *pdev)
  344. {
  345. int acpi_state, d_max;
  346. if (pdev->no_d3cold)
  347. d_max = ACPI_STATE_D3_HOT;
  348. else
  349. d_max = ACPI_STATE_D3_COLD;
  350. acpi_state = acpi_pm_device_sleep_state(&pdev->dev, NULL, d_max);
  351. if (acpi_state < 0)
  352. return PCI_POWER_ERROR;
  353. switch (acpi_state) {
  354. case ACPI_STATE_D0:
  355. return PCI_D0;
  356. case ACPI_STATE_D1:
  357. return PCI_D1;
  358. case ACPI_STATE_D2:
  359. return PCI_D2;
  360. case ACPI_STATE_D3_HOT:
  361. return PCI_D3hot;
  362. case ACPI_STATE_D3_COLD:
  363. return PCI_D3cold;
  364. }
  365. return PCI_POWER_ERROR;
  366. }
  367. static bool acpi_pci_power_manageable(struct pci_dev *dev)
  368. {
  369. struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
  370. return adev ? acpi_device_power_manageable(adev) : false;
  371. }
  372. static int acpi_pci_set_power_state(struct pci_dev *dev, pci_power_t state)
  373. {
  374. struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
  375. static const u8 state_conv[] = {
  376. [PCI_D0] = ACPI_STATE_D0,
  377. [PCI_D1] = ACPI_STATE_D1,
  378. [PCI_D2] = ACPI_STATE_D2,
  379. [PCI_D3hot] = ACPI_STATE_D3_HOT,
  380. [PCI_D3cold] = ACPI_STATE_D3_COLD,
  381. };
  382. int error = -EINVAL;
  383. /* If the ACPI device has _EJ0, ignore the device */
  384. if (!adev || acpi_has_method(adev->handle, "_EJ0"))
  385. return -ENODEV;
  386. switch (state) {
  387. case PCI_D3cold:
  388. if (dev_pm_qos_flags(&dev->dev, PM_QOS_FLAG_NO_POWER_OFF) ==
  389. PM_QOS_FLAGS_ALL) {
  390. error = -EBUSY;
  391. break;
  392. }
  393. case PCI_D0:
  394. case PCI_D1:
  395. case PCI_D2:
  396. case PCI_D3hot:
  397. error = acpi_device_set_power(adev, state_conv[state]);
  398. }
  399. if (!error)
  400. dev_dbg(&dev->dev, "power state changed by ACPI to %s\n",
  401. acpi_power_state_string(state_conv[state]));
  402. return error;
  403. }
  404. static pci_power_t acpi_pci_get_power_state(struct pci_dev *dev)
  405. {
  406. struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
  407. static const pci_power_t state_conv[] = {
  408. [ACPI_STATE_D0] = PCI_D0,
  409. [ACPI_STATE_D1] = PCI_D1,
  410. [ACPI_STATE_D2] = PCI_D2,
  411. [ACPI_STATE_D3_HOT] = PCI_D3hot,
  412. [ACPI_STATE_D3_COLD] = PCI_D3cold,
  413. };
  414. int state;
  415. if (!adev || !acpi_device_power_manageable(adev))
  416. return PCI_UNKNOWN;
  417. if (acpi_device_get_power(adev, &state) || state == ACPI_STATE_UNKNOWN)
  418. return PCI_UNKNOWN;
  419. return state_conv[state];
  420. }
  421. static bool acpi_pci_can_wakeup(struct pci_dev *dev)
  422. {
  423. struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
  424. return adev ? acpi_device_can_wakeup(adev) : false;
  425. }
  426. static void acpi_pci_propagate_wakeup_enable(struct pci_bus *bus, bool enable)
  427. {
  428. while (bus->parent) {
  429. if (!acpi_pm_device_sleep_wake(&bus->self->dev, enable))
  430. return;
  431. bus = bus->parent;
  432. }
  433. /* We have reached the root bus. */
  434. if (bus->bridge)
  435. acpi_pm_device_sleep_wake(bus->bridge, enable);
  436. }
  437. static int acpi_pci_sleep_wake(struct pci_dev *dev, bool enable)
  438. {
  439. if (acpi_pci_can_wakeup(dev))
  440. return acpi_pm_device_sleep_wake(&dev->dev, enable);
  441. acpi_pci_propagate_wakeup_enable(dev->bus, enable);
  442. return 0;
  443. }
  444. static void acpi_pci_propagate_run_wake(struct pci_bus *bus, bool enable)
  445. {
  446. while (bus->parent) {
  447. struct pci_dev *bridge = bus->self;
  448. if (bridge->pme_interrupt)
  449. return;
  450. if (!acpi_pm_device_run_wake(&bridge->dev, enable))
  451. return;
  452. bus = bus->parent;
  453. }
  454. /* We have reached the root bus. */
  455. if (bus->bridge)
  456. acpi_pm_device_run_wake(bus->bridge, enable);
  457. }
  458. static int acpi_pci_run_wake(struct pci_dev *dev, bool enable)
  459. {
  460. /*
  461. * Per PCI Express Base Specification Revision 2.0 section
  462. * 5.3.3.2 Link Wakeup, platform support is needed for D3cold
  463. * waking up to power on the main link even if there is PME
  464. * support for D3cold
  465. */
  466. if (dev->pme_interrupt && !dev->runtime_d3cold)
  467. return 0;
  468. if (!acpi_pm_device_run_wake(&dev->dev, enable))
  469. return 0;
  470. acpi_pci_propagate_run_wake(dev->bus, enable);
  471. return 0;
  472. }
  473. static bool acpi_pci_need_resume(struct pci_dev *dev)
  474. {
  475. struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
  476. if (!adev || !acpi_device_power_manageable(adev))
  477. return false;
  478. if (device_may_wakeup(&dev->dev) != !!adev->wakeup.prepare_count)
  479. return true;
  480. if (acpi_target_system_state() == ACPI_STATE_S0)
  481. return false;
  482. return !!adev->power.flags.dsw_present;
  483. }
  484. static const struct pci_platform_pm_ops acpi_pci_platform_pm = {
  485. .is_manageable = acpi_pci_power_manageable,
  486. .set_state = acpi_pci_set_power_state,
  487. .get_state = acpi_pci_get_power_state,
  488. .choose_state = acpi_pci_choose_state,
  489. .sleep_wake = acpi_pci_sleep_wake,
  490. .run_wake = acpi_pci_run_wake,
  491. .need_resume = acpi_pci_need_resume,
  492. };
  493. void acpi_pci_add_bus(struct pci_bus *bus)
  494. {
  495. union acpi_object *obj;
  496. struct pci_host_bridge *bridge;
  497. if (acpi_pci_disabled || !bus->bridge)
  498. return;
  499. acpi_pci_slot_enumerate(bus);
  500. acpiphp_enumerate_slots(bus);
  501. /*
  502. * For a host bridge, check its _DSM for function 8 and if
  503. * that is available, mark it in pci_host_bridge.
  504. */
  505. if (!pci_is_root_bus(bus))
  506. return;
  507. obj = acpi_evaluate_dsm(ACPI_HANDLE(bus->bridge), pci_acpi_dsm_uuid, 3,
  508. RESET_DELAY_DSM, NULL);
  509. if (!obj)
  510. return;
  511. if (obj->type == ACPI_TYPE_INTEGER && obj->integer.value == 1) {
  512. bridge = pci_find_host_bridge(bus);
  513. bridge->ignore_reset_delay = 1;
  514. }
  515. ACPI_FREE(obj);
  516. }
  517. void acpi_pci_remove_bus(struct pci_bus *bus)
  518. {
  519. if (acpi_pci_disabled || !bus->bridge)
  520. return;
  521. acpiphp_remove_slots(bus);
  522. acpi_pci_slot_remove(bus);
  523. }
  524. /* ACPI bus type */
  525. static struct acpi_device *acpi_pci_find_companion(struct device *dev)
  526. {
  527. struct pci_dev *pci_dev = to_pci_dev(dev);
  528. bool check_children;
  529. u64 addr;
  530. check_children = pci_is_bridge(pci_dev);
  531. /* Please ref to ACPI spec for the syntax of _ADR */
  532. addr = (PCI_SLOT(pci_dev->devfn) << 16) | PCI_FUNC(pci_dev->devfn);
  533. return acpi_find_child_device(ACPI_COMPANION(dev->parent), addr,
  534. check_children);
  535. }
  536. /**
  537. * pci_acpi_optimize_delay - optimize PCI D3 and D3cold delay from ACPI
  538. * @pdev: the PCI device whose delay is to be updated
  539. * @handle: ACPI handle of this device
  540. *
  541. * Update the d3_delay and d3cold_delay of a PCI device from the ACPI _DSM
  542. * control method of either the device itself or the PCI host bridge.
  543. *
  544. * Function 8, "Reset Delay," applies to the entire hierarchy below a PCI
  545. * host bridge. If it returns one, the OS may assume that all devices in
  546. * the hierarchy have already completed power-on reset delays.
  547. *
  548. * Function 9, "Device Readiness Durations," applies only to the object
  549. * where it is located. It returns delay durations required after various
  550. * events if the device requires less time than the spec requires. Delays
  551. * from this function take precedence over the Reset Delay function.
  552. *
  553. * These _DSM functions are defined by the draft ECN of January 28, 2014,
  554. * titled "ACPI additions for FW latency optimizations."
  555. */
  556. static void pci_acpi_optimize_delay(struct pci_dev *pdev,
  557. acpi_handle handle)
  558. {
  559. struct pci_host_bridge *bridge = pci_find_host_bridge(pdev->bus);
  560. int value;
  561. union acpi_object *obj, *elements;
  562. if (bridge->ignore_reset_delay)
  563. pdev->d3cold_delay = 0;
  564. obj = acpi_evaluate_dsm(handle, pci_acpi_dsm_uuid, 3,
  565. FUNCTION_DELAY_DSM, NULL);
  566. if (!obj)
  567. return;
  568. if (obj->type == ACPI_TYPE_PACKAGE && obj->package.count == 5) {
  569. elements = obj->package.elements;
  570. if (elements[0].type == ACPI_TYPE_INTEGER) {
  571. value = (int)elements[0].integer.value / 1000;
  572. if (value < PCI_PM_D3COLD_WAIT)
  573. pdev->d3cold_delay = value;
  574. }
  575. if (elements[3].type == ACPI_TYPE_INTEGER) {
  576. value = (int)elements[3].integer.value / 1000;
  577. if (value < PCI_PM_D3_WAIT)
  578. pdev->d3_delay = value;
  579. }
  580. }
  581. ACPI_FREE(obj);
  582. }
  583. static void pci_acpi_setup(struct device *dev)
  584. {
  585. struct pci_dev *pci_dev = to_pci_dev(dev);
  586. struct acpi_device *adev = ACPI_COMPANION(dev);
  587. if (!adev)
  588. return;
  589. pci_acpi_optimize_delay(pci_dev, adev->handle);
  590. pci_acpi_add_pm_notifier(adev, pci_dev);
  591. if (!adev->wakeup.flags.valid)
  592. return;
  593. device_set_wakeup_capable(dev, true);
  594. acpi_pci_sleep_wake(pci_dev, false);
  595. if (adev->wakeup.flags.run_wake)
  596. device_set_run_wake(dev, true);
  597. }
  598. static void pci_acpi_cleanup(struct device *dev)
  599. {
  600. struct acpi_device *adev = ACPI_COMPANION(dev);
  601. if (!adev)
  602. return;
  603. pci_acpi_remove_pm_notifier(adev);
  604. if (adev->wakeup.flags.valid) {
  605. device_set_wakeup_capable(dev, false);
  606. device_set_run_wake(dev, false);
  607. }
  608. }
  609. static bool pci_acpi_bus_match(struct device *dev)
  610. {
  611. return dev_is_pci(dev);
  612. }
  613. static struct acpi_bus_type acpi_pci_bus = {
  614. .name = "PCI",
  615. .match = pci_acpi_bus_match,
  616. .find_companion = acpi_pci_find_companion,
  617. .setup = pci_acpi_setup,
  618. .cleanup = pci_acpi_cleanup,
  619. };
  620. static struct fwnode_handle *(*pci_msi_get_fwnode_cb)(struct device *dev);
  621. /**
  622. * pci_msi_register_fwnode_provider - Register callback to retrieve fwnode
  623. * @fn: Callback matching a device to a fwnode that identifies a PCI
  624. * MSI domain.
  625. *
  626. * This should be called by irqchip driver, which is the parent of
  627. * the MSI domain to provide callback interface to query fwnode.
  628. */
  629. void
  630. pci_msi_register_fwnode_provider(struct fwnode_handle *(*fn)(struct device *))
  631. {
  632. pci_msi_get_fwnode_cb = fn;
  633. }
  634. /**
  635. * pci_host_bridge_acpi_msi_domain - Retrieve MSI domain of a PCI host bridge
  636. * @bus: The PCI host bridge bus.
  637. *
  638. * This function uses the callback function registered by
  639. * pci_msi_register_fwnode_provider() to retrieve the irq_domain with
  640. * type DOMAIN_BUS_PCI_MSI of the specified host bridge bus.
  641. * This returns NULL on error or when the domain is not found.
  642. */
  643. struct irq_domain *pci_host_bridge_acpi_msi_domain(struct pci_bus *bus)
  644. {
  645. struct fwnode_handle *fwnode;
  646. if (!pci_msi_get_fwnode_cb)
  647. return NULL;
  648. fwnode = pci_msi_get_fwnode_cb(&bus->dev);
  649. if (!fwnode)
  650. return NULL;
  651. return irq_find_matching_fwnode(fwnode, DOMAIN_BUS_PCI_MSI);
  652. }
  653. static int __init acpi_pci_init(void)
  654. {
  655. int ret;
  656. if (acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_MSI) {
  657. pr_info("ACPI FADT declares the system doesn't support MSI, so disable it\n");
  658. pci_no_msi();
  659. }
  660. if (acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_ASPM) {
  661. pr_info("ACPI FADT declares the system doesn't support PCIe ASPM, so disable it\n");
  662. pcie_no_aspm();
  663. }
  664. ret = register_acpi_bus_type(&acpi_pci_bus);
  665. if (ret)
  666. return 0;
  667. pci_set_platform_pm(&acpi_pci_platform_pm);
  668. acpi_pci_slot_init();
  669. acpiphp_init();
  670. return 0;
  671. }
  672. arch_initcall(acpi_pci_init);