pci-acpi.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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. * @work: Work item to handle.
  352. */
  353. static void pci_acpi_wake_bus(struct work_struct *work)
  354. {
  355. struct acpi_device *adev;
  356. struct acpi_pci_root *root;
  357. adev = container_of(work, struct acpi_device, wakeup.context.work);
  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. * @handle: ACPI handle of a device the notification is for.
  364. * @work: Work item to handle.
  365. */
  366. static void pci_acpi_wake_dev(struct work_struct *work)
  367. {
  368. struct acpi_device_wakeup_context *context;
  369. struct pci_dev *pci_dev;
  370. context = container_of(work, struct acpi_device_wakeup_context, work);
  371. pci_dev = to_pci_dev(context->dev);
  372. if (pci_dev->pme_poll)
  373. pci_dev->pme_poll = false;
  374. if (pci_dev->current_state == PCI_D3cold) {
  375. pci_wakeup_event(pci_dev);
  376. pm_runtime_resume(&pci_dev->dev);
  377. return;
  378. }
  379. /* Clear PME Status if set. */
  380. if (pci_dev->pme_support)
  381. pci_check_pme_status(pci_dev);
  382. pci_wakeup_event(pci_dev);
  383. pm_runtime_resume(&pci_dev->dev);
  384. pci_pme_wakeup_bus(pci_dev->subordinate);
  385. }
  386. /**
  387. * pci_acpi_add_bus_pm_notifier - Register PM notifier for root PCI bus.
  388. * @dev: PCI root bridge ACPI device.
  389. */
  390. acpi_status pci_acpi_add_bus_pm_notifier(struct acpi_device *dev)
  391. {
  392. return acpi_add_pm_notifier(dev, NULL, pci_acpi_wake_bus);
  393. }
  394. /**
  395. * pci_acpi_add_pm_notifier - Register PM notifier for given PCI device.
  396. * @dev: ACPI device to add the notifier for.
  397. * @pci_dev: PCI device to check for the PME status if an event is signaled.
  398. */
  399. acpi_status pci_acpi_add_pm_notifier(struct acpi_device *dev,
  400. struct pci_dev *pci_dev)
  401. {
  402. return acpi_add_pm_notifier(dev, &pci_dev->dev, pci_acpi_wake_dev);
  403. }
  404. /*
  405. * _SxD returns the D-state with the highest power
  406. * (lowest D-state number) supported in the S-state "x".
  407. *
  408. * If the devices does not have a _PRW
  409. * (Power Resources for Wake) supporting system wakeup from "x"
  410. * then the OS is free to choose a lower power (higher number
  411. * D-state) than the return value from _SxD.
  412. *
  413. * But if _PRW is enabled at S-state "x", the OS
  414. * must not choose a power lower than _SxD --
  415. * unless the device has an _SxW method specifying
  416. * the lowest power (highest D-state number) the device
  417. * may enter while still able to wake the system.
  418. *
  419. * ie. depending on global OS policy:
  420. *
  421. * if (_PRW at S-state x)
  422. * choose from highest power _SxD to lowest power _SxW
  423. * else // no _PRW at S-state x
  424. * choose highest power _SxD or any lower power
  425. */
  426. static pci_power_t acpi_pci_choose_state(struct pci_dev *pdev)
  427. {
  428. int acpi_state, d_max;
  429. if (pdev->no_d3cold)
  430. d_max = ACPI_STATE_D3_HOT;
  431. else
  432. d_max = ACPI_STATE_D3_COLD;
  433. acpi_state = acpi_pm_device_sleep_state(&pdev->dev, NULL, d_max);
  434. if (acpi_state < 0)
  435. return PCI_POWER_ERROR;
  436. switch (acpi_state) {
  437. case ACPI_STATE_D0:
  438. return PCI_D0;
  439. case ACPI_STATE_D1:
  440. return PCI_D1;
  441. case ACPI_STATE_D2:
  442. return PCI_D2;
  443. case ACPI_STATE_D3_HOT:
  444. return PCI_D3hot;
  445. case ACPI_STATE_D3_COLD:
  446. return PCI_D3cold;
  447. }
  448. return PCI_POWER_ERROR;
  449. }
  450. static bool acpi_pci_power_manageable(struct pci_dev *dev)
  451. {
  452. struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
  453. return adev ? acpi_device_power_manageable(adev) : false;
  454. }
  455. static int acpi_pci_set_power_state(struct pci_dev *dev, pci_power_t state)
  456. {
  457. struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
  458. static const u8 state_conv[] = {
  459. [PCI_D0] = ACPI_STATE_D0,
  460. [PCI_D1] = ACPI_STATE_D1,
  461. [PCI_D2] = ACPI_STATE_D2,
  462. [PCI_D3hot] = ACPI_STATE_D3_HOT,
  463. [PCI_D3cold] = ACPI_STATE_D3_COLD,
  464. };
  465. int error = -EINVAL;
  466. /* If the ACPI device has _EJ0, ignore the device */
  467. if (!adev || acpi_has_method(adev->handle, "_EJ0"))
  468. return -ENODEV;
  469. switch (state) {
  470. case PCI_D3cold:
  471. if (dev_pm_qos_flags(&dev->dev, PM_QOS_FLAG_NO_POWER_OFF) ==
  472. PM_QOS_FLAGS_ALL) {
  473. error = -EBUSY;
  474. break;
  475. }
  476. case PCI_D0:
  477. case PCI_D1:
  478. case PCI_D2:
  479. case PCI_D3hot:
  480. error = acpi_device_set_power(adev, state_conv[state]);
  481. }
  482. if (!error)
  483. dev_dbg(&dev->dev, "power state changed by ACPI to %s\n",
  484. acpi_power_state_string(state_conv[state]));
  485. return error;
  486. }
  487. static pci_power_t acpi_pci_get_power_state(struct pci_dev *dev)
  488. {
  489. struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
  490. static const pci_power_t state_conv[] = {
  491. [ACPI_STATE_D0] = PCI_D0,
  492. [ACPI_STATE_D1] = PCI_D1,
  493. [ACPI_STATE_D2] = PCI_D2,
  494. [ACPI_STATE_D3_HOT] = PCI_D3hot,
  495. [ACPI_STATE_D3_COLD] = PCI_D3cold,
  496. };
  497. int state;
  498. if (!adev || !acpi_device_power_manageable(adev))
  499. return PCI_UNKNOWN;
  500. if (acpi_device_get_power(adev, &state) || state == ACPI_STATE_UNKNOWN)
  501. return PCI_UNKNOWN;
  502. return state_conv[state];
  503. }
  504. static bool acpi_pci_can_wakeup(struct pci_dev *dev)
  505. {
  506. struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
  507. return adev ? acpi_device_can_wakeup(adev) : false;
  508. }
  509. static void acpi_pci_propagate_wakeup_enable(struct pci_bus *bus, bool enable)
  510. {
  511. while (bus->parent) {
  512. if (!acpi_pm_device_sleep_wake(&bus->self->dev, enable))
  513. return;
  514. bus = bus->parent;
  515. }
  516. /* We have reached the root bus. */
  517. if (bus->bridge)
  518. acpi_pm_device_sleep_wake(bus->bridge, enable);
  519. }
  520. static int acpi_pci_sleep_wake(struct pci_dev *dev, bool enable)
  521. {
  522. if (acpi_pci_can_wakeup(dev))
  523. return acpi_pm_device_sleep_wake(&dev->dev, enable);
  524. acpi_pci_propagate_wakeup_enable(dev->bus, enable);
  525. return 0;
  526. }
  527. static void acpi_pci_propagate_run_wake(struct pci_bus *bus, bool enable)
  528. {
  529. while (bus->parent) {
  530. struct pci_dev *bridge = bus->self;
  531. if (bridge->pme_interrupt)
  532. return;
  533. if (!acpi_pm_device_run_wake(&bridge->dev, enable))
  534. return;
  535. bus = bus->parent;
  536. }
  537. /* We have reached the root bus. */
  538. if (bus->bridge)
  539. acpi_pm_device_run_wake(bus->bridge, enable);
  540. }
  541. static int acpi_pci_run_wake(struct pci_dev *dev, bool enable)
  542. {
  543. /*
  544. * Per PCI Express Base Specification Revision 2.0 section
  545. * 5.3.3.2 Link Wakeup, platform support is needed for D3cold
  546. * waking up to power on the main link even if there is PME
  547. * support for D3cold
  548. */
  549. if (dev->pme_interrupt && !dev->runtime_d3cold)
  550. return 0;
  551. if (!acpi_pm_device_run_wake(&dev->dev, enable))
  552. return 0;
  553. acpi_pci_propagate_run_wake(dev->bus, enable);
  554. return 0;
  555. }
  556. static bool acpi_pci_need_resume(struct pci_dev *dev)
  557. {
  558. struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
  559. if (!adev || !acpi_device_power_manageable(adev))
  560. return false;
  561. if (device_may_wakeup(&dev->dev) != !!adev->wakeup.prepare_count)
  562. return true;
  563. if (acpi_target_system_state() == ACPI_STATE_S0)
  564. return false;
  565. return !!adev->power.flags.dsw_present;
  566. }
  567. static const struct pci_platform_pm_ops acpi_pci_platform_pm = {
  568. .is_manageable = acpi_pci_power_manageable,
  569. .set_state = acpi_pci_set_power_state,
  570. .get_state = acpi_pci_get_power_state,
  571. .choose_state = acpi_pci_choose_state,
  572. .sleep_wake = acpi_pci_sleep_wake,
  573. .run_wake = acpi_pci_run_wake,
  574. .need_resume = acpi_pci_need_resume,
  575. };
  576. void acpi_pci_add_bus(struct pci_bus *bus)
  577. {
  578. union acpi_object *obj;
  579. struct pci_host_bridge *bridge;
  580. if (acpi_pci_disabled || !bus->bridge)
  581. return;
  582. acpi_pci_slot_enumerate(bus);
  583. acpiphp_enumerate_slots(bus);
  584. /*
  585. * For a host bridge, check its _DSM for function 8 and if
  586. * that is available, mark it in pci_host_bridge.
  587. */
  588. if (!pci_is_root_bus(bus))
  589. return;
  590. obj = acpi_evaluate_dsm(ACPI_HANDLE(bus->bridge), &pci_acpi_dsm_guid, 3,
  591. RESET_DELAY_DSM, NULL);
  592. if (!obj)
  593. return;
  594. if (obj->type == ACPI_TYPE_INTEGER && obj->integer.value == 1) {
  595. bridge = pci_find_host_bridge(bus);
  596. bridge->ignore_reset_delay = 1;
  597. }
  598. ACPI_FREE(obj);
  599. }
  600. void acpi_pci_remove_bus(struct pci_bus *bus)
  601. {
  602. if (acpi_pci_disabled || !bus->bridge)
  603. return;
  604. acpiphp_remove_slots(bus);
  605. acpi_pci_slot_remove(bus);
  606. }
  607. /* ACPI bus type */
  608. static struct acpi_device *acpi_pci_find_companion(struct device *dev)
  609. {
  610. struct pci_dev *pci_dev = to_pci_dev(dev);
  611. bool check_children;
  612. u64 addr;
  613. check_children = pci_is_bridge(pci_dev);
  614. /* Please ref to ACPI spec for the syntax of _ADR */
  615. addr = (PCI_SLOT(pci_dev->devfn) << 16) | PCI_FUNC(pci_dev->devfn);
  616. return acpi_find_child_device(ACPI_COMPANION(dev->parent), addr,
  617. check_children);
  618. }
  619. /**
  620. * pci_acpi_optimize_delay - optimize PCI D3 and D3cold delay from ACPI
  621. * @pdev: the PCI device whose delay is to be updated
  622. * @handle: ACPI handle of this device
  623. *
  624. * Update the d3_delay and d3cold_delay of a PCI device from the ACPI _DSM
  625. * control method of either the device itself or the PCI host bridge.
  626. *
  627. * Function 8, "Reset Delay," applies to the entire hierarchy below a PCI
  628. * host bridge. If it returns one, the OS may assume that all devices in
  629. * the hierarchy have already completed power-on reset delays.
  630. *
  631. * Function 9, "Device Readiness Durations," applies only to the object
  632. * where it is located. It returns delay durations required after various
  633. * events if the device requires less time than the spec requires. Delays
  634. * from this function take precedence over the Reset Delay function.
  635. *
  636. * These _DSM functions are defined by the draft ECN of January 28, 2014,
  637. * titled "ACPI additions for FW latency optimizations."
  638. */
  639. static void pci_acpi_optimize_delay(struct pci_dev *pdev,
  640. acpi_handle handle)
  641. {
  642. struct pci_host_bridge *bridge = pci_find_host_bridge(pdev->bus);
  643. int value;
  644. union acpi_object *obj, *elements;
  645. if (bridge->ignore_reset_delay)
  646. pdev->d3cold_delay = 0;
  647. obj = acpi_evaluate_dsm(handle, &pci_acpi_dsm_guid, 3,
  648. FUNCTION_DELAY_DSM, NULL);
  649. if (!obj)
  650. return;
  651. if (obj->type == ACPI_TYPE_PACKAGE && obj->package.count == 5) {
  652. elements = obj->package.elements;
  653. if (elements[0].type == ACPI_TYPE_INTEGER) {
  654. value = (int)elements[0].integer.value / 1000;
  655. if (value < PCI_PM_D3COLD_WAIT)
  656. pdev->d3cold_delay = value;
  657. }
  658. if (elements[3].type == ACPI_TYPE_INTEGER) {
  659. value = (int)elements[3].integer.value / 1000;
  660. if (value < PCI_PM_D3_WAIT)
  661. pdev->d3_delay = value;
  662. }
  663. }
  664. ACPI_FREE(obj);
  665. }
  666. static void pci_acpi_setup(struct device *dev)
  667. {
  668. struct pci_dev *pci_dev = to_pci_dev(dev);
  669. struct acpi_device *adev = ACPI_COMPANION(dev);
  670. if (!adev)
  671. return;
  672. pci_acpi_optimize_delay(pci_dev, adev->handle);
  673. pci_acpi_add_pm_notifier(adev, pci_dev);
  674. if (!adev->wakeup.flags.valid)
  675. return;
  676. device_set_wakeup_capable(dev, true);
  677. acpi_pci_sleep_wake(pci_dev, false);
  678. if (adev->wakeup.flags.run_wake)
  679. device_set_run_wake(dev, true);
  680. }
  681. static void pci_acpi_cleanup(struct device *dev)
  682. {
  683. struct acpi_device *adev = ACPI_COMPANION(dev);
  684. if (!adev)
  685. return;
  686. pci_acpi_remove_pm_notifier(adev);
  687. if (adev->wakeup.flags.valid) {
  688. device_set_wakeup_capable(dev, false);
  689. device_set_run_wake(dev, false);
  690. }
  691. }
  692. static bool pci_acpi_bus_match(struct device *dev)
  693. {
  694. return dev_is_pci(dev);
  695. }
  696. static struct acpi_bus_type acpi_pci_bus = {
  697. .name = "PCI",
  698. .match = pci_acpi_bus_match,
  699. .find_companion = acpi_pci_find_companion,
  700. .setup = pci_acpi_setup,
  701. .cleanup = pci_acpi_cleanup,
  702. };
  703. static struct fwnode_handle *(*pci_msi_get_fwnode_cb)(struct device *dev);
  704. /**
  705. * pci_msi_register_fwnode_provider - Register callback to retrieve fwnode
  706. * @fn: Callback matching a device to a fwnode that identifies a PCI
  707. * MSI domain.
  708. *
  709. * This should be called by irqchip driver, which is the parent of
  710. * the MSI domain to provide callback interface to query fwnode.
  711. */
  712. void
  713. pci_msi_register_fwnode_provider(struct fwnode_handle *(*fn)(struct device *))
  714. {
  715. pci_msi_get_fwnode_cb = fn;
  716. }
  717. /**
  718. * pci_host_bridge_acpi_msi_domain - Retrieve MSI domain of a PCI host bridge
  719. * @bus: The PCI host bridge bus.
  720. *
  721. * This function uses the callback function registered by
  722. * pci_msi_register_fwnode_provider() to retrieve the irq_domain with
  723. * type DOMAIN_BUS_PCI_MSI of the specified host bridge bus.
  724. * This returns NULL on error or when the domain is not found.
  725. */
  726. struct irq_domain *pci_host_bridge_acpi_msi_domain(struct pci_bus *bus)
  727. {
  728. struct fwnode_handle *fwnode;
  729. if (!pci_msi_get_fwnode_cb)
  730. return NULL;
  731. fwnode = pci_msi_get_fwnode_cb(&bus->dev);
  732. if (!fwnode)
  733. return NULL;
  734. return irq_find_matching_fwnode(fwnode, DOMAIN_BUS_PCI_MSI);
  735. }
  736. static int __init acpi_pci_init(void)
  737. {
  738. int ret;
  739. if (acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_MSI) {
  740. pr_info("ACPI FADT declares the system doesn't support MSI, so disable it\n");
  741. pci_no_msi();
  742. }
  743. if (acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_ASPM) {
  744. pr_info("ACPI FADT declares the system doesn't support PCIe ASPM, so disable it\n");
  745. pcie_no_aspm();
  746. }
  747. ret = register_acpi_bus_type(&acpi_pci_bus);
  748. if (ret)
  749. return 0;
  750. pci_set_platform_pm(&acpi_pci_platform_pm);
  751. acpi_pci_slot_init();
  752. acpiphp_init();
  753. return 0;
  754. }
  755. arch_initcall(acpi_pci_init);