pci_root.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  1. /*
  2. * pci_root.c - ACPI PCI Root Bridge Driver ($Revision: 40 $)
  3. *
  4. * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
  5. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  6. *
  7. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or (at
  12. * your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/types.h>
  25. #include <linux/mutex.h>
  26. #include <linux/pm.h>
  27. #include <linux/pm_runtime.h>
  28. #include <linux/pci.h>
  29. #include <linux/pci-acpi.h>
  30. #include <linux/pci-aspm.h>
  31. #include <linux/dmar.h>
  32. #include <linux/acpi.h>
  33. #include <linux/slab.h>
  34. #include <linux/dmi.h>
  35. #include <linux/platform_data/x86/apple.h>
  36. #include <acpi/apei.h> /* for acpi_hest_init() */
  37. #include "internal.h"
  38. #define _COMPONENT ACPI_PCI_COMPONENT
  39. ACPI_MODULE_NAME("pci_root");
  40. #define ACPI_PCI_ROOT_CLASS "pci_bridge"
  41. #define ACPI_PCI_ROOT_DEVICE_NAME "PCI Root Bridge"
  42. static int acpi_pci_root_add(struct acpi_device *device,
  43. const struct acpi_device_id *not_used);
  44. static void acpi_pci_root_remove(struct acpi_device *device);
  45. static int acpi_pci_root_scan_dependent(struct acpi_device *adev)
  46. {
  47. acpiphp_check_host_bridge(adev);
  48. return 0;
  49. }
  50. #define ACPI_PCIE_REQ_SUPPORT (OSC_PCI_EXT_CONFIG_SUPPORT \
  51. | OSC_PCI_ASPM_SUPPORT \
  52. | OSC_PCI_CLOCK_PM_SUPPORT \
  53. | OSC_PCI_MSI_SUPPORT)
  54. static const struct acpi_device_id root_device_ids[] = {
  55. {"PNP0A03", 0},
  56. {"", 0},
  57. };
  58. static struct acpi_scan_handler pci_root_handler = {
  59. .ids = root_device_ids,
  60. .attach = acpi_pci_root_add,
  61. .detach = acpi_pci_root_remove,
  62. .hotplug = {
  63. .enabled = true,
  64. .scan_dependent = acpi_pci_root_scan_dependent,
  65. },
  66. };
  67. static DEFINE_MUTEX(osc_lock);
  68. /**
  69. * acpi_is_root_bridge - determine whether an ACPI CA node is a PCI root bridge
  70. * @handle - the ACPI CA node in question.
  71. *
  72. * Note: we could make this API take a struct acpi_device * instead, but
  73. * for now, it's more convenient to operate on an acpi_handle.
  74. */
  75. int acpi_is_root_bridge(acpi_handle handle)
  76. {
  77. int ret;
  78. struct acpi_device *device;
  79. ret = acpi_bus_get_device(handle, &device);
  80. if (ret)
  81. return 0;
  82. ret = acpi_match_device_ids(device, root_device_ids);
  83. if (ret)
  84. return 0;
  85. else
  86. return 1;
  87. }
  88. EXPORT_SYMBOL_GPL(acpi_is_root_bridge);
  89. static acpi_status
  90. get_root_bridge_busnr_callback(struct acpi_resource *resource, void *data)
  91. {
  92. struct resource *res = data;
  93. struct acpi_resource_address64 address;
  94. acpi_status status;
  95. status = acpi_resource_to_address64(resource, &address);
  96. if (ACPI_FAILURE(status))
  97. return AE_OK;
  98. if ((address.address.address_length > 0) &&
  99. (address.resource_type == ACPI_BUS_NUMBER_RANGE)) {
  100. res->start = address.address.minimum;
  101. res->end = address.address.minimum + address.address.address_length - 1;
  102. }
  103. return AE_OK;
  104. }
  105. static acpi_status try_get_root_bridge_busnr(acpi_handle handle,
  106. struct resource *res)
  107. {
  108. acpi_status status;
  109. res->start = -1;
  110. status =
  111. acpi_walk_resources(handle, METHOD_NAME__CRS,
  112. get_root_bridge_busnr_callback, res);
  113. if (ACPI_FAILURE(status))
  114. return status;
  115. if (res->start == -1)
  116. return AE_ERROR;
  117. return AE_OK;
  118. }
  119. struct pci_osc_bit_struct {
  120. u32 bit;
  121. char *desc;
  122. };
  123. static struct pci_osc_bit_struct pci_osc_support_bit[] = {
  124. { OSC_PCI_EXT_CONFIG_SUPPORT, "ExtendedConfig" },
  125. { OSC_PCI_ASPM_SUPPORT, "ASPM" },
  126. { OSC_PCI_CLOCK_PM_SUPPORT, "ClockPM" },
  127. { OSC_PCI_SEGMENT_GROUPS_SUPPORT, "Segments" },
  128. { OSC_PCI_MSI_SUPPORT, "MSI" },
  129. };
  130. static struct pci_osc_bit_struct pci_osc_control_bit[] = {
  131. { OSC_PCI_EXPRESS_NATIVE_HP_CONTROL, "PCIeHotplug" },
  132. { OSC_PCI_SHPC_NATIVE_HP_CONTROL, "SHPCHotplug" },
  133. { OSC_PCI_EXPRESS_PME_CONTROL, "PME" },
  134. { OSC_PCI_EXPRESS_AER_CONTROL, "AER" },
  135. { OSC_PCI_EXPRESS_CAPABILITY_CONTROL, "PCIeCapability" },
  136. { OSC_PCI_EXPRESS_LTR_CONTROL, "LTR" },
  137. };
  138. static void decode_osc_bits(struct acpi_pci_root *root, char *msg, u32 word,
  139. struct pci_osc_bit_struct *table, int size)
  140. {
  141. char buf[80];
  142. int i, len = 0;
  143. struct pci_osc_bit_struct *entry;
  144. buf[0] = '\0';
  145. for (i = 0, entry = table; i < size; i++, entry++)
  146. if (word & entry->bit)
  147. len += snprintf(buf + len, sizeof(buf) - len, "%s%s",
  148. len ? " " : "", entry->desc);
  149. dev_info(&root->device->dev, "_OSC: %s [%s]\n", msg, buf);
  150. }
  151. static void decode_osc_support(struct acpi_pci_root *root, char *msg, u32 word)
  152. {
  153. decode_osc_bits(root, msg, word, pci_osc_support_bit,
  154. ARRAY_SIZE(pci_osc_support_bit));
  155. }
  156. static void decode_osc_control(struct acpi_pci_root *root, char *msg, u32 word)
  157. {
  158. decode_osc_bits(root, msg, word, pci_osc_control_bit,
  159. ARRAY_SIZE(pci_osc_control_bit));
  160. }
  161. static u8 pci_osc_uuid_str[] = "33DB4D5B-1FF7-401C-9657-7441C03DD766";
  162. static acpi_status acpi_pci_run_osc(acpi_handle handle,
  163. const u32 *capbuf, u32 *retval)
  164. {
  165. struct acpi_osc_context context = {
  166. .uuid_str = pci_osc_uuid_str,
  167. .rev = 1,
  168. .cap.length = 12,
  169. .cap.pointer = (void *)capbuf,
  170. };
  171. acpi_status status;
  172. status = acpi_run_osc(handle, &context);
  173. if (ACPI_SUCCESS(status)) {
  174. *retval = *((u32 *)(context.ret.pointer + 8));
  175. kfree(context.ret.pointer);
  176. }
  177. return status;
  178. }
  179. static acpi_status acpi_pci_query_osc(struct acpi_pci_root *root,
  180. u32 support,
  181. u32 *control)
  182. {
  183. acpi_status status;
  184. u32 result, capbuf[3];
  185. support &= OSC_PCI_SUPPORT_MASKS;
  186. support |= root->osc_support_set;
  187. capbuf[OSC_QUERY_DWORD] = OSC_QUERY_ENABLE;
  188. capbuf[OSC_SUPPORT_DWORD] = support;
  189. if (control) {
  190. *control &= OSC_PCI_CONTROL_MASKS;
  191. capbuf[OSC_CONTROL_DWORD] = *control | root->osc_control_set;
  192. } else {
  193. /* Run _OSC query only with existing controls. */
  194. capbuf[OSC_CONTROL_DWORD] = root->osc_control_set;
  195. }
  196. status = acpi_pci_run_osc(root->device->handle, capbuf, &result);
  197. if (ACPI_SUCCESS(status)) {
  198. root->osc_support_set = support;
  199. if (control)
  200. *control = result;
  201. }
  202. return status;
  203. }
  204. static acpi_status acpi_pci_osc_support(struct acpi_pci_root *root, u32 flags)
  205. {
  206. acpi_status status;
  207. mutex_lock(&osc_lock);
  208. status = acpi_pci_query_osc(root, flags, NULL);
  209. mutex_unlock(&osc_lock);
  210. return status;
  211. }
  212. struct acpi_pci_root *acpi_pci_find_root(acpi_handle handle)
  213. {
  214. struct acpi_pci_root *root;
  215. struct acpi_device *device;
  216. if (acpi_bus_get_device(handle, &device) ||
  217. acpi_match_device_ids(device, root_device_ids))
  218. return NULL;
  219. root = acpi_driver_data(device);
  220. return root;
  221. }
  222. EXPORT_SYMBOL_GPL(acpi_pci_find_root);
  223. struct acpi_handle_node {
  224. struct list_head node;
  225. acpi_handle handle;
  226. };
  227. /**
  228. * acpi_get_pci_dev - convert ACPI CA handle to struct pci_dev
  229. * @handle: the handle in question
  230. *
  231. * Given an ACPI CA handle, the desired PCI device is located in the
  232. * list of PCI devices.
  233. *
  234. * If the device is found, its reference count is increased and this
  235. * function returns a pointer to its data structure. The caller must
  236. * decrement the reference count by calling pci_dev_put().
  237. * If no device is found, %NULL is returned.
  238. */
  239. struct pci_dev *acpi_get_pci_dev(acpi_handle handle)
  240. {
  241. int dev, fn;
  242. unsigned long long adr;
  243. acpi_status status;
  244. acpi_handle phandle;
  245. struct pci_bus *pbus;
  246. struct pci_dev *pdev = NULL;
  247. struct acpi_handle_node *node, *tmp;
  248. struct acpi_pci_root *root;
  249. LIST_HEAD(device_list);
  250. /*
  251. * Walk up the ACPI CA namespace until we reach a PCI root bridge.
  252. */
  253. phandle = handle;
  254. while (!acpi_is_root_bridge(phandle)) {
  255. node = kzalloc(sizeof(struct acpi_handle_node), GFP_KERNEL);
  256. if (!node)
  257. goto out;
  258. INIT_LIST_HEAD(&node->node);
  259. node->handle = phandle;
  260. list_add(&node->node, &device_list);
  261. status = acpi_get_parent(phandle, &phandle);
  262. if (ACPI_FAILURE(status))
  263. goto out;
  264. }
  265. root = acpi_pci_find_root(phandle);
  266. if (!root)
  267. goto out;
  268. pbus = root->bus;
  269. /*
  270. * Now, walk back down the PCI device tree until we return to our
  271. * original handle. Assumes that everything between the PCI root
  272. * bridge and the device we're looking for must be a P2P bridge.
  273. */
  274. list_for_each_entry(node, &device_list, node) {
  275. acpi_handle hnd = node->handle;
  276. status = acpi_evaluate_integer(hnd, "_ADR", NULL, &adr);
  277. if (ACPI_FAILURE(status))
  278. goto out;
  279. dev = (adr >> 16) & 0xffff;
  280. fn = adr & 0xffff;
  281. pdev = pci_get_slot(pbus, PCI_DEVFN(dev, fn));
  282. if (!pdev || hnd == handle)
  283. break;
  284. pbus = pdev->subordinate;
  285. pci_dev_put(pdev);
  286. /*
  287. * This function may be called for a non-PCI device that has a
  288. * PCI parent (eg. a disk under a PCI SATA controller). In that
  289. * case pdev->subordinate will be NULL for the parent.
  290. */
  291. if (!pbus) {
  292. dev_dbg(&pdev->dev, "Not a PCI-to-PCI bridge\n");
  293. pdev = NULL;
  294. break;
  295. }
  296. }
  297. out:
  298. list_for_each_entry_safe(node, tmp, &device_list, node)
  299. kfree(node);
  300. return pdev;
  301. }
  302. EXPORT_SYMBOL_GPL(acpi_get_pci_dev);
  303. /**
  304. * acpi_pci_osc_control_set - Request control of PCI root _OSC features.
  305. * @handle: ACPI handle of a PCI root bridge (or PCIe Root Complex).
  306. * @mask: Mask of _OSC bits to request control of, place to store control mask.
  307. * @req: Mask of _OSC bits the control of is essential to the caller.
  308. *
  309. * Run _OSC query for @mask and if that is successful, compare the returned
  310. * mask of control bits with @req. If all of the @req bits are set in the
  311. * returned mask, run _OSC request for it.
  312. *
  313. * The variable at the @mask address may be modified regardless of whether or
  314. * not the function returns success. On success it will contain the mask of
  315. * _OSC bits the BIOS has granted control of, but its contents are meaningless
  316. * on failure.
  317. **/
  318. acpi_status acpi_pci_osc_control_set(acpi_handle handle, u32 *mask, u32 req)
  319. {
  320. struct acpi_pci_root *root;
  321. acpi_status status = AE_OK;
  322. u32 ctrl, capbuf[3];
  323. if (!mask)
  324. return AE_BAD_PARAMETER;
  325. ctrl = *mask & OSC_PCI_CONTROL_MASKS;
  326. if ((ctrl & req) != req)
  327. return AE_TYPE;
  328. root = acpi_pci_find_root(handle);
  329. if (!root)
  330. return AE_NOT_EXIST;
  331. mutex_lock(&osc_lock);
  332. *mask = ctrl | root->osc_control_set;
  333. /* No need to evaluate _OSC if the control was already granted. */
  334. if ((root->osc_control_set & ctrl) == ctrl)
  335. goto out;
  336. /* Need to check the available controls bits before requesting them. */
  337. while (*mask) {
  338. status = acpi_pci_query_osc(root, root->osc_support_set, mask);
  339. if (ACPI_FAILURE(status))
  340. goto out;
  341. if (ctrl == *mask)
  342. break;
  343. decode_osc_control(root, "platform does not support",
  344. ctrl & ~(*mask));
  345. ctrl = *mask;
  346. }
  347. if ((ctrl & req) != req) {
  348. decode_osc_control(root, "not requesting control; platform does not support",
  349. req & ~(ctrl));
  350. status = AE_SUPPORT;
  351. goto out;
  352. }
  353. capbuf[OSC_QUERY_DWORD] = 0;
  354. capbuf[OSC_SUPPORT_DWORD] = root->osc_support_set;
  355. capbuf[OSC_CONTROL_DWORD] = ctrl;
  356. status = acpi_pci_run_osc(handle, capbuf, mask);
  357. if (ACPI_SUCCESS(status))
  358. root->osc_control_set = *mask;
  359. out:
  360. mutex_unlock(&osc_lock);
  361. return status;
  362. }
  363. EXPORT_SYMBOL(acpi_pci_osc_control_set);
  364. static void negotiate_os_control(struct acpi_pci_root *root, int *no_aspm)
  365. {
  366. u32 support, control, requested;
  367. acpi_status status;
  368. struct acpi_device *device = root->device;
  369. acpi_handle handle = device->handle;
  370. /*
  371. * Apple always return failure on _OSC calls when _OSI("Darwin") has
  372. * been called successfully. We know the feature set supported by the
  373. * platform, so avoid calling _OSC at all
  374. */
  375. if (x86_apple_machine) {
  376. root->osc_control_set = ~OSC_PCI_EXPRESS_PME_CONTROL;
  377. decode_osc_control(root, "OS assumes control of",
  378. root->osc_control_set);
  379. return;
  380. }
  381. /*
  382. * All supported architectures that use ACPI have support for
  383. * PCI domains, so we indicate this in _OSC support capabilities.
  384. */
  385. support = OSC_PCI_SEGMENT_GROUPS_SUPPORT;
  386. if (pci_ext_cfg_avail())
  387. support |= OSC_PCI_EXT_CONFIG_SUPPORT;
  388. if (pcie_aspm_support_enabled())
  389. support |= OSC_PCI_ASPM_SUPPORT | OSC_PCI_CLOCK_PM_SUPPORT;
  390. if (pci_msi_enabled())
  391. support |= OSC_PCI_MSI_SUPPORT;
  392. decode_osc_support(root, "OS supports", support);
  393. status = acpi_pci_osc_support(root, support);
  394. if (ACPI_FAILURE(status)) {
  395. dev_info(&device->dev, "_OSC failed (%s)%s\n",
  396. acpi_format_exception(status),
  397. pcie_aspm_support_enabled() ? "; disabling ASPM" : "");
  398. *no_aspm = 1;
  399. return;
  400. }
  401. if (pcie_ports_disabled) {
  402. dev_info(&device->dev, "PCIe port services disabled; not requesting _OSC control\n");
  403. return;
  404. }
  405. if ((support & ACPI_PCIE_REQ_SUPPORT) != ACPI_PCIE_REQ_SUPPORT) {
  406. decode_osc_support(root, "not requesting OS control; OS requires",
  407. ACPI_PCIE_REQ_SUPPORT);
  408. return;
  409. }
  410. control = OSC_PCI_EXPRESS_CAPABILITY_CONTROL
  411. | OSC_PCI_EXPRESS_PME_CONTROL;
  412. if (IS_ENABLED(CONFIG_PCIEASPM))
  413. control |= OSC_PCI_EXPRESS_LTR_CONTROL;
  414. if (IS_ENABLED(CONFIG_HOTPLUG_PCI_PCIE))
  415. control |= OSC_PCI_EXPRESS_NATIVE_HP_CONTROL;
  416. if (IS_ENABLED(CONFIG_HOTPLUG_PCI_SHPC))
  417. control |= OSC_PCI_SHPC_NATIVE_HP_CONTROL;
  418. if (pci_aer_available()) {
  419. if (aer_acpi_firmware_first())
  420. dev_info(&device->dev,
  421. "PCIe AER handled by firmware\n");
  422. else
  423. control |= OSC_PCI_EXPRESS_AER_CONTROL;
  424. }
  425. requested = control;
  426. status = acpi_pci_osc_control_set(handle, &control,
  427. OSC_PCI_EXPRESS_CAPABILITY_CONTROL);
  428. if (ACPI_SUCCESS(status)) {
  429. decode_osc_control(root, "OS now controls", control);
  430. if (acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_ASPM) {
  431. /*
  432. * We have ASPM control, but the FADT indicates that
  433. * it's unsupported. Leave existing configuration
  434. * intact and prevent the OS from touching it.
  435. */
  436. dev_info(&device->dev, "FADT indicates ASPM is unsupported, using BIOS configuration\n");
  437. *no_aspm = 1;
  438. }
  439. } else {
  440. decode_osc_control(root, "OS requested", requested);
  441. decode_osc_control(root, "platform willing to grant", control);
  442. dev_info(&device->dev, "_OSC failed (%s); disabling ASPM\n",
  443. acpi_format_exception(status));
  444. /*
  445. * We want to disable ASPM here, but aspm_disabled
  446. * needs to remain in its state from boot so that we
  447. * properly handle PCIe 1.1 devices. So we set this
  448. * flag here, to defer the action until after the ACPI
  449. * root scan.
  450. */
  451. *no_aspm = 1;
  452. }
  453. }
  454. static int acpi_pci_root_add(struct acpi_device *device,
  455. const struct acpi_device_id *not_used)
  456. {
  457. unsigned long long segment, bus;
  458. acpi_status status;
  459. int result;
  460. struct acpi_pci_root *root;
  461. acpi_handle handle = device->handle;
  462. int no_aspm = 0;
  463. bool hotadd = system_state == SYSTEM_RUNNING;
  464. root = kzalloc(sizeof(struct acpi_pci_root), GFP_KERNEL);
  465. if (!root)
  466. return -ENOMEM;
  467. segment = 0;
  468. status = acpi_evaluate_integer(handle, METHOD_NAME__SEG, NULL,
  469. &segment);
  470. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
  471. dev_err(&device->dev, "can't evaluate _SEG\n");
  472. result = -ENODEV;
  473. goto end;
  474. }
  475. /* Check _CRS first, then _BBN. If no _BBN, default to zero. */
  476. root->secondary.flags = IORESOURCE_BUS;
  477. status = try_get_root_bridge_busnr(handle, &root->secondary);
  478. if (ACPI_FAILURE(status)) {
  479. /*
  480. * We need both the start and end of the downstream bus range
  481. * to interpret _CBA (MMCONFIG base address), so it really is
  482. * supposed to be in _CRS. If we don't find it there, all we
  483. * can do is assume [_BBN-0xFF] or [0-0xFF].
  484. */
  485. root->secondary.end = 0xFF;
  486. dev_warn(&device->dev,
  487. FW_BUG "no secondary bus range in _CRS\n");
  488. status = acpi_evaluate_integer(handle, METHOD_NAME__BBN,
  489. NULL, &bus);
  490. if (ACPI_SUCCESS(status))
  491. root->secondary.start = bus;
  492. else if (status == AE_NOT_FOUND)
  493. root->secondary.start = 0;
  494. else {
  495. dev_err(&device->dev, "can't evaluate _BBN\n");
  496. result = -ENODEV;
  497. goto end;
  498. }
  499. }
  500. root->device = device;
  501. root->segment = segment & 0xFFFF;
  502. strcpy(acpi_device_name(device), ACPI_PCI_ROOT_DEVICE_NAME);
  503. strcpy(acpi_device_class(device), ACPI_PCI_ROOT_CLASS);
  504. device->driver_data = root;
  505. if (hotadd && dmar_device_add(handle)) {
  506. result = -ENXIO;
  507. goto end;
  508. }
  509. pr_info(PREFIX "%s [%s] (domain %04x %pR)\n",
  510. acpi_device_name(device), acpi_device_bid(device),
  511. root->segment, &root->secondary);
  512. root->mcfg_addr = acpi_pci_root_get_mcfg_addr(handle);
  513. negotiate_os_control(root, &no_aspm);
  514. /*
  515. * TBD: Need PCI interface for enumeration/configuration of roots.
  516. */
  517. /*
  518. * Scan the Root Bridge
  519. * --------------------
  520. * Must do this prior to any attempt to bind the root device, as the
  521. * PCI namespace does not get created until this call is made (and
  522. * thus the root bridge's pci_dev does not exist).
  523. */
  524. root->bus = pci_acpi_scan_root(root);
  525. if (!root->bus) {
  526. dev_err(&device->dev,
  527. "Bus %04x:%02x not present in PCI namespace\n",
  528. root->segment, (unsigned int)root->secondary.start);
  529. device->driver_data = NULL;
  530. result = -ENODEV;
  531. goto remove_dmar;
  532. }
  533. if (no_aspm)
  534. pcie_no_aspm();
  535. pci_acpi_add_bus_pm_notifier(device);
  536. device_set_wakeup_capable(root->bus->bridge, device->wakeup.flags.valid);
  537. if (hotadd) {
  538. pcibios_resource_survey_bus(root->bus);
  539. pci_assign_unassigned_root_bus_resources(root->bus);
  540. /*
  541. * This is only called for the hotadd case. For the boot-time
  542. * case, we need to wait until after PCI initialization in
  543. * order to deal with IOAPICs mapped in on a PCI BAR.
  544. *
  545. * This is currently x86-specific, because acpi_ioapic_add()
  546. * is an empty function without CONFIG_ACPI_HOTPLUG_IOAPIC.
  547. * And CONFIG_ACPI_HOTPLUG_IOAPIC depends on CONFIG_X86_IO_APIC
  548. * (see drivers/acpi/Kconfig).
  549. */
  550. acpi_ioapic_add(root->device->handle);
  551. }
  552. pci_lock_rescan_remove();
  553. pci_bus_add_devices(root->bus);
  554. pci_unlock_rescan_remove();
  555. return 1;
  556. remove_dmar:
  557. if (hotadd)
  558. dmar_device_remove(handle);
  559. end:
  560. kfree(root);
  561. return result;
  562. }
  563. static void acpi_pci_root_remove(struct acpi_device *device)
  564. {
  565. struct acpi_pci_root *root = acpi_driver_data(device);
  566. pci_lock_rescan_remove();
  567. pci_stop_root_bus(root->bus);
  568. pci_ioapic_remove(root);
  569. device_set_wakeup_capable(root->bus->bridge, false);
  570. pci_acpi_remove_bus_pm_notifier(device);
  571. pci_remove_root_bus(root->bus);
  572. WARN_ON(acpi_ioapic_remove(root));
  573. dmar_device_remove(device->handle);
  574. pci_unlock_rescan_remove();
  575. kfree(root);
  576. }
  577. /*
  578. * Following code to support acpi_pci_root_create() is copied from
  579. * arch/x86/pci/acpi.c and modified so it could be reused by x86, IA64
  580. * and ARM64.
  581. */
  582. static void acpi_pci_root_validate_resources(struct device *dev,
  583. struct list_head *resources,
  584. unsigned long type)
  585. {
  586. LIST_HEAD(list);
  587. struct resource *res1, *res2, *root = NULL;
  588. struct resource_entry *tmp, *entry, *entry2;
  589. BUG_ON((type & (IORESOURCE_MEM | IORESOURCE_IO)) == 0);
  590. root = (type & IORESOURCE_MEM) ? &iomem_resource : &ioport_resource;
  591. list_splice_init(resources, &list);
  592. resource_list_for_each_entry_safe(entry, tmp, &list) {
  593. bool free = false;
  594. resource_size_t end;
  595. res1 = entry->res;
  596. if (!(res1->flags & type))
  597. goto next;
  598. /* Exclude non-addressable range or non-addressable portion */
  599. end = min(res1->end, root->end);
  600. if (end <= res1->start) {
  601. dev_info(dev, "host bridge window %pR (ignored, not CPU addressable)\n",
  602. res1);
  603. free = true;
  604. goto next;
  605. } else if (res1->end != end) {
  606. dev_info(dev, "host bridge window %pR ([%#llx-%#llx] ignored, not CPU addressable)\n",
  607. res1, (unsigned long long)end + 1,
  608. (unsigned long long)res1->end);
  609. res1->end = end;
  610. }
  611. resource_list_for_each_entry(entry2, resources) {
  612. res2 = entry2->res;
  613. if (!(res2->flags & type))
  614. continue;
  615. /*
  616. * I don't like throwing away windows because then
  617. * our resources no longer match the ACPI _CRS, but
  618. * the kernel resource tree doesn't allow overlaps.
  619. */
  620. if (resource_overlaps(res1, res2)) {
  621. res2->start = min(res1->start, res2->start);
  622. res2->end = max(res1->end, res2->end);
  623. dev_info(dev, "host bridge window expanded to %pR; %pR ignored\n",
  624. res2, res1);
  625. free = true;
  626. goto next;
  627. }
  628. }
  629. next:
  630. resource_list_del(entry);
  631. if (free)
  632. resource_list_free_entry(entry);
  633. else
  634. resource_list_add_tail(entry, resources);
  635. }
  636. }
  637. static void acpi_pci_root_remap_iospace(struct fwnode_handle *fwnode,
  638. struct resource_entry *entry)
  639. {
  640. #ifdef PCI_IOBASE
  641. struct resource *res = entry->res;
  642. resource_size_t cpu_addr = res->start;
  643. resource_size_t pci_addr = cpu_addr - entry->offset;
  644. resource_size_t length = resource_size(res);
  645. unsigned long port;
  646. if (pci_register_io_range(fwnode, cpu_addr, length))
  647. goto err;
  648. port = pci_address_to_pio(cpu_addr);
  649. if (port == (unsigned long)-1)
  650. goto err;
  651. res->start = port;
  652. res->end = port + length - 1;
  653. entry->offset = port - pci_addr;
  654. if (pci_remap_iospace(res, cpu_addr) < 0)
  655. goto err;
  656. pr_info("Remapped I/O %pa to %pR\n", &cpu_addr, res);
  657. return;
  658. err:
  659. res->flags |= IORESOURCE_DISABLED;
  660. #endif
  661. }
  662. int acpi_pci_probe_root_resources(struct acpi_pci_root_info *info)
  663. {
  664. int ret;
  665. struct list_head *list = &info->resources;
  666. struct acpi_device *device = info->bridge;
  667. struct resource_entry *entry, *tmp;
  668. unsigned long flags;
  669. flags = IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_MEM_8AND16BIT;
  670. ret = acpi_dev_get_resources(device, list,
  671. acpi_dev_filter_resource_type_cb,
  672. (void *)flags);
  673. if (ret < 0)
  674. dev_warn(&device->dev,
  675. "failed to parse _CRS method, error code %d\n", ret);
  676. else if (ret == 0)
  677. dev_dbg(&device->dev,
  678. "no IO and memory resources present in _CRS\n");
  679. else {
  680. resource_list_for_each_entry_safe(entry, tmp, list) {
  681. if (entry->res->flags & IORESOURCE_IO)
  682. acpi_pci_root_remap_iospace(&device->fwnode,
  683. entry);
  684. if (entry->res->flags & IORESOURCE_DISABLED)
  685. resource_list_destroy_entry(entry);
  686. else
  687. entry->res->name = info->name;
  688. }
  689. acpi_pci_root_validate_resources(&device->dev, list,
  690. IORESOURCE_MEM);
  691. acpi_pci_root_validate_resources(&device->dev, list,
  692. IORESOURCE_IO);
  693. }
  694. return ret;
  695. }
  696. static void pci_acpi_root_add_resources(struct acpi_pci_root_info *info)
  697. {
  698. struct resource_entry *entry, *tmp;
  699. struct resource *res, *conflict, *root = NULL;
  700. resource_list_for_each_entry_safe(entry, tmp, &info->resources) {
  701. res = entry->res;
  702. if (res->flags & IORESOURCE_MEM)
  703. root = &iomem_resource;
  704. else if (res->flags & IORESOURCE_IO)
  705. root = &ioport_resource;
  706. else
  707. continue;
  708. /*
  709. * Some legacy x86 host bridge drivers use iomem_resource and
  710. * ioport_resource as default resource pool, skip it.
  711. */
  712. if (res == root)
  713. continue;
  714. conflict = insert_resource_conflict(root, res);
  715. if (conflict) {
  716. dev_info(&info->bridge->dev,
  717. "ignoring host bridge window %pR (conflicts with %s %pR)\n",
  718. res, conflict->name, conflict);
  719. resource_list_destroy_entry(entry);
  720. }
  721. }
  722. }
  723. static void __acpi_pci_root_release_info(struct acpi_pci_root_info *info)
  724. {
  725. struct resource *res;
  726. struct resource_entry *entry, *tmp;
  727. if (!info)
  728. return;
  729. resource_list_for_each_entry_safe(entry, tmp, &info->resources) {
  730. res = entry->res;
  731. if (res->parent &&
  732. (res->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
  733. release_resource(res);
  734. resource_list_destroy_entry(entry);
  735. }
  736. info->ops->release_info(info);
  737. }
  738. static void acpi_pci_root_release_info(struct pci_host_bridge *bridge)
  739. {
  740. struct resource *res;
  741. struct resource_entry *entry;
  742. resource_list_for_each_entry(entry, &bridge->windows) {
  743. res = entry->res;
  744. if (res->flags & IORESOURCE_IO)
  745. pci_unmap_iospace(res);
  746. if (res->parent &&
  747. (res->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
  748. release_resource(res);
  749. }
  750. __acpi_pci_root_release_info(bridge->release_data);
  751. }
  752. struct pci_bus *acpi_pci_root_create(struct acpi_pci_root *root,
  753. struct acpi_pci_root_ops *ops,
  754. struct acpi_pci_root_info *info,
  755. void *sysdata)
  756. {
  757. int ret, busnum = root->secondary.start;
  758. struct acpi_device *device = root->device;
  759. int node = acpi_get_node(device->handle);
  760. struct pci_bus *bus;
  761. struct pci_host_bridge *host_bridge;
  762. info->root = root;
  763. info->bridge = device;
  764. info->ops = ops;
  765. INIT_LIST_HEAD(&info->resources);
  766. snprintf(info->name, sizeof(info->name), "PCI Bus %04x:%02x",
  767. root->segment, busnum);
  768. if (ops->init_info && ops->init_info(info))
  769. goto out_release_info;
  770. if (ops->prepare_resources)
  771. ret = ops->prepare_resources(info);
  772. else
  773. ret = acpi_pci_probe_root_resources(info);
  774. if (ret < 0)
  775. goto out_release_info;
  776. pci_acpi_root_add_resources(info);
  777. pci_add_resource(&info->resources, &root->secondary);
  778. bus = pci_create_root_bus(NULL, busnum, ops->pci_ops,
  779. sysdata, &info->resources);
  780. if (!bus)
  781. goto out_release_info;
  782. host_bridge = to_pci_host_bridge(bus->bridge);
  783. if (!(root->osc_control_set & OSC_PCI_EXPRESS_NATIVE_HP_CONTROL))
  784. host_bridge->native_pcie_hotplug = 0;
  785. if (!(root->osc_control_set & OSC_PCI_SHPC_NATIVE_HP_CONTROL))
  786. host_bridge->native_shpc_hotplug = 0;
  787. if (!(root->osc_control_set & OSC_PCI_EXPRESS_AER_CONTROL))
  788. host_bridge->native_aer = 0;
  789. if (!(root->osc_control_set & OSC_PCI_EXPRESS_PME_CONTROL))
  790. host_bridge->native_pme = 0;
  791. if (!(root->osc_control_set & OSC_PCI_EXPRESS_LTR_CONTROL))
  792. host_bridge->native_ltr = 0;
  793. pci_scan_child_bus(bus);
  794. pci_set_host_bridge_release(host_bridge, acpi_pci_root_release_info,
  795. info);
  796. if (node != NUMA_NO_NODE)
  797. dev_printk(KERN_DEBUG, &bus->dev, "on NUMA node %d\n", node);
  798. return bus;
  799. out_release_info:
  800. __acpi_pci_root_release_info(info);
  801. return NULL;
  802. }
  803. void __init acpi_pci_root_init(void)
  804. {
  805. acpi_hest_init();
  806. if (acpi_pci_disabled)
  807. return;
  808. pci_acpi_crs_quirks();
  809. acpi_scan_add_handler_with_hotplug(&pci_root_handler, "pci_root");
  810. }