pci-acpi.c 23 KB

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