pci-acpi.c 22 KB

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