pci-acpi.c 22 KB

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