pci_root.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  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); disabling ASPM\n",
  396. acpi_format_exception(status));
  397. *no_aspm = 1;
  398. return;
  399. }
  400. if (pcie_ports_disabled) {
  401. dev_info(&device->dev, "PCIe port services disabled; not requesting _OSC control\n");
  402. return;
  403. }
  404. if ((support & ACPI_PCIE_REQ_SUPPORT) != ACPI_PCIE_REQ_SUPPORT) {
  405. decode_osc_support(root, "not requesting OS control; OS requires",
  406. ACPI_PCIE_REQ_SUPPORT);
  407. return;
  408. }
  409. control = OSC_PCI_EXPRESS_CAPABILITY_CONTROL
  410. | OSC_PCI_EXPRESS_PME_CONTROL;
  411. if (IS_ENABLED(CONFIG_PCIEASPM))
  412. control |= OSC_PCI_EXPRESS_LTR_CONTROL;
  413. if (IS_ENABLED(CONFIG_HOTPLUG_PCI_PCIE))
  414. control |= OSC_PCI_EXPRESS_NATIVE_HP_CONTROL;
  415. if (IS_ENABLED(CONFIG_HOTPLUG_PCI_SHPC))
  416. control |= OSC_PCI_SHPC_NATIVE_HP_CONTROL;
  417. if (pci_aer_available()) {
  418. if (aer_acpi_firmware_first())
  419. dev_info(&device->dev,
  420. "PCIe AER handled by firmware\n");
  421. else
  422. control |= OSC_PCI_EXPRESS_AER_CONTROL;
  423. }
  424. requested = control;
  425. status = acpi_pci_osc_control_set(handle, &control,
  426. OSC_PCI_EXPRESS_CAPABILITY_CONTROL);
  427. if (ACPI_SUCCESS(status)) {
  428. decode_osc_control(root, "OS now controls", control);
  429. if (acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_ASPM) {
  430. /*
  431. * We have ASPM control, but the FADT indicates that
  432. * it's unsupported. Leave existing configuration
  433. * intact and prevent the OS from touching it.
  434. */
  435. dev_info(&device->dev, "FADT indicates ASPM is unsupported, using BIOS configuration\n");
  436. *no_aspm = 1;
  437. }
  438. } else {
  439. decode_osc_control(root, "OS requested", requested);
  440. decode_osc_control(root, "platform willing to grant", control);
  441. dev_info(&device->dev, "_OSC failed (%s); disabling ASPM\n",
  442. acpi_format_exception(status));
  443. /*
  444. * We want to disable ASPM here, but aspm_disabled
  445. * needs to remain in its state from boot so that we
  446. * properly handle PCIe 1.1 devices. So we set this
  447. * flag here, to defer the action until after the ACPI
  448. * root scan.
  449. */
  450. *no_aspm = 1;
  451. }
  452. }
  453. static int acpi_pci_root_add(struct acpi_device *device,
  454. const struct acpi_device_id *not_used)
  455. {
  456. unsigned long long segment, bus;
  457. acpi_status status;
  458. int result;
  459. struct acpi_pci_root *root;
  460. acpi_handle handle = device->handle;
  461. int no_aspm = 0;
  462. bool hotadd = system_state == SYSTEM_RUNNING;
  463. root = kzalloc(sizeof(struct acpi_pci_root), GFP_KERNEL);
  464. if (!root)
  465. return -ENOMEM;
  466. segment = 0;
  467. status = acpi_evaluate_integer(handle, METHOD_NAME__SEG, NULL,
  468. &segment);
  469. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
  470. dev_err(&device->dev, "can't evaluate _SEG\n");
  471. result = -ENODEV;
  472. goto end;
  473. }
  474. /* Check _CRS first, then _BBN. If no _BBN, default to zero. */
  475. root->secondary.flags = IORESOURCE_BUS;
  476. status = try_get_root_bridge_busnr(handle, &root->secondary);
  477. if (ACPI_FAILURE(status)) {
  478. /*
  479. * We need both the start and end of the downstream bus range
  480. * to interpret _CBA (MMCONFIG base address), so it really is
  481. * supposed to be in _CRS. If we don't find it there, all we
  482. * can do is assume [_BBN-0xFF] or [0-0xFF].
  483. */
  484. root->secondary.end = 0xFF;
  485. dev_warn(&device->dev,
  486. FW_BUG "no secondary bus range in _CRS\n");
  487. status = acpi_evaluate_integer(handle, METHOD_NAME__BBN,
  488. NULL, &bus);
  489. if (ACPI_SUCCESS(status))
  490. root->secondary.start = bus;
  491. else if (status == AE_NOT_FOUND)
  492. root->secondary.start = 0;
  493. else {
  494. dev_err(&device->dev, "can't evaluate _BBN\n");
  495. result = -ENODEV;
  496. goto end;
  497. }
  498. }
  499. root->device = device;
  500. root->segment = segment & 0xFFFF;
  501. strcpy(acpi_device_name(device), ACPI_PCI_ROOT_DEVICE_NAME);
  502. strcpy(acpi_device_class(device), ACPI_PCI_ROOT_CLASS);
  503. device->driver_data = root;
  504. if (hotadd && dmar_device_add(handle)) {
  505. result = -ENXIO;
  506. goto end;
  507. }
  508. pr_info(PREFIX "%s [%s] (domain %04x %pR)\n",
  509. acpi_device_name(device), acpi_device_bid(device),
  510. root->segment, &root->secondary);
  511. root->mcfg_addr = acpi_pci_root_get_mcfg_addr(handle);
  512. negotiate_os_control(root, &no_aspm);
  513. /*
  514. * TBD: Need PCI interface for enumeration/configuration of roots.
  515. */
  516. /*
  517. * Scan the Root Bridge
  518. * --------------------
  519. * Must do this prior to any attempt to bind the root device, as the
  520. * PCI namespace does not get created until this call is made (and
  521. * thus the root bridge's pci_dev does not exist).
  522. */
  523. root->bus = pci_acpi_scan_root(root);
  524. if (!root->bus) {
  525. dev_err(&device->dev,
  526. "Bus %04x:%02x not present in PCI namespace\n",
  527. root->segment, (unsigned int)root->secondary.start);
  528. device->driver_data = NULL;
  529. result = -ENODEV;
  530. goto remove_dmar;
  531. }
  532. if (no_aspm)
  533. pcie_no_aspm();
  534. pci_acpi_add_bus_pm_notifier(device);
  535. device_set_wakeup_capable(root->bus->bridge, device->wakeup.flags.valid);
  536. if (hotadd) {
  537. pcibios_resource_survey_bus(root->bus);
  538. pci_assign_unassigned_root_bus_resources(root->bus);
  539. /*
  540. * This is only called for the hotadd case. For the boot-time
  541. * case, we need to wait until after PCI initialization in
  542. * order to deal with IOAPICs mapped in on a PCI BAR.
  543. *
  544. * This is currently x86-specific, because acpi_ioapic_add()
  545. * is an empty function without CONFIG_ACPI_HOTPLUG_IOAPIC.
  546. * And CONFIG_ACPI_HOTPLUG_IOAPIC depends on CONFIG_X86_IO_APIC
  547. * (see drivers/acpi/Kconfig).
  548. */
  549. acpi_ioapic_add(root->device->handle);
  550. }
  551. pci_lock_rescan_remove();
  552. pci_bus_add_devices(root->bus);
  553. pci_unlock_rescan_remove();
  554. return 1;
  555. remove_dmar:
  556. if (hotadd)
  557. dmar_device_remove(handle);
  558. end:
  559. kfree(root);
  560. return result;
  561. }
  562. static void acpi_pci_root_remove(struct acpi_device *device)
  563. {
  564. struct acpi_pci_root *root = acpi_driver_data(device);
  565. pci_lock_rescan_remove();
  566. pci_stop_root_bus(root->bus);
  567. pci_ioapic_remove(root);
  568. device_set_wakeup_capable(root->bus->bridge, false);
  569. pci_acpi_remove_bus_pm_notifier(device);
  570. pci_remove_root_bus(root->bus);
  571. WARN_ON(acpi_ioapic_remove(root));
  572. dmar_device_remove(device->handle);
  573. pci_unlock_rescan_remove();
  574. kfree(root);
  575. }
  576. /*
  577. * Following code to support acpi_pci_root_create() is copied from
  578. * arch/x86/pci/acpi.c and modified so it could be reused by x86, IA64
  579. * and ARM64.
  580. */
  581. static void acpi_pci_root_validate_resources(struct device *dev,
  582. struct list_head *resources,
  583. unsigned long type)
  584. {
  585. LIST_HEAD(list);
  586. struct resource *res1, *res2, *root = NULL;
  587. struct resource_entry *tmp, *entry, *entry2;
  588. BUG_ON((type & (IORESOURCE_MEM | IORESOURCE_IO)) == 0);
  589. root = (type & IORESOURCE_MEM) ? &iomem_resource : &ioport_resource;
  590. list_splice_init(resources, &list);
  591. resource_list_for_each_entry_safe(entry, tmp, &list) {
  592. bool free = false;
  593. resource_size_t end;
  594. res1 = entry->res;
  595. if (!(res1->flags & type))
  596. goto next;
  597. /* Exclude non-addressable range or non-addressable portion */
  598. end = min(res1->end, root->end);
  599. if (end <= res1->start) {
  600. dev_info(dev, "host bridge window %pR (ignored, not CPU addressable)\n",
  601. res1);
  602. free = true;
  603. goto next;
  604. } else if (res1->end != end) {
  605. dev_info(dev, "host bridge window %pR ([%#llx-%#llx] ignored, not CPU addressable)\n",
  606. res1, (unsigned long long)end + 1,
  607. (unsigned long long)res1->end);
  608. res1->end = end;
  609. }
  610. resource_list_for_each_entry(entry2, resources) {
  611. res2 = entry2->res;
  612. if (!(res2->flags & type))
  613. continue;
  614. /*
  615. * I don't like throwing away windows because then
  616. * our resources no longer match the ACPI _CRS, but
  617. * the kernel resource tree doesn't allow overlaps.
  618. */
  619. if (resource_overlaps(res1, res2)) {
  620. res2->start = min(res1->start, res2->start);
  621. res2->end = max(res1->end, res2->end);
  622. dev_info(dev, "host bridge window expanded to %pR; %pR ignored\n",
  623. res2, res1);
  624. free = true;
  625. goto next;
  626. }
  627. }
  628. next:
  629. resource_list_del(entry);
  630. if (free)
  631. resource_list_free_entry(entry);
  632. else
  633. resource_list_add_tail(entry, resources);
  634. }
  635. }
  636. static void acpi_pci_root_remap_iospace(struct fwnode_handle *fwnode,
  637. struct resource_entry *entry)
  638. {
  639. #ifdef PCI_IOBASE
  640. struct resource *res = entry->res;
  641. resource_size_t cpu_addr = res->start;
  642. resource_size_t pci_addr = cpu_addr - entry->offset;
  643. resource_size_t length = resource_size(res);
  644. unsigned long port;
  645. if (pci_register_io_range(fwnode, cpu_addr, length))
  646. goto err;
  647. port = pci_address_to_pio(cpu_addr);
  648. if (port == (unsigned long)-1)
  649. goto err;
  650. res->start = port;
  651. res->end = port + length - 1;
  652. entry->offset = port - pci_addr;
  653. if (pci_remap_iospace(res, cpu_addr) < 0)
  654. goto err;
  655. pr_info("Remapped I/O %pa to %pR\n", &cpu_addr, res);
  656. return;
  657. err:
  658. res->flags |= IORESOURCE_DISABLED;
  659. #endif
  660. }
  661. int acpi_pci_probe_root_resources(struct acpi_pci_root_info *info)
  662. {
  663. int ret;
  664. struct list_head *list = &info->resources;
  665. struct acpi_device *device = info->bridge;
  666. struct resource_entry *entry, *tmp;
  667. unsigned long flags;
  668. flags = IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_MEM_8AND16BIT;
  669. ret = acpi_dev_get_resources(device, list,
  670. acpi_dev_filter_resource_type_cb,
  671. (void *)flags);
  672. if (ret < 0)
  673. dev_warn(&device->dev,
  674. "failed to parse _CRS method, error code %d\n", ret);
  675. else if (ret == 0)
  676. dev_dbg(&device->dev,
  677. "no IO and memory resources present in _CRS\n");
  678. else {
  679. resource_list_for_each_entry_safe(entry, tmp, list) {
  680. if (entry->res->flags & IORESOURCE_IO)
  681. acpi_pci_root_remap_iospace(&device->fwnode,
  682. entry);
  683. if (entry->res->flags & IORESOURCE_DISABLED)
  684. resource_list_destroy_entry(entry);
  685. else
  686. entry->res->name = info->name;
  687. }
  688. acpi_pci_root_validate_resources(&device->dev, list,
  689. IORESOURCE_MEM);
  690. acpi_pci_root_validate_resources(&device->dev, list,
  691. IORESOURCE_IO);
  692. }
  693. return ret;
  694. }
  695. static void pci_acpi_root_add_resources(struct acpi_pci_root_info *info)
  696. {
  697. struct resource_entry *entry, *tmp;
  698. struct resource *res, *conflict, *root = NULL;
  699. resource_list_for_each_entry_safe(entry, tmp, &info->resources) {
  700. res = entry->res;
  701. if (res->flags & IORESOURCE_MEM)
  702. root = &iomem_resource;
  703. else if (res->flags & IORESOURCE_IO)
  704. root = &ioport_resource;
  705. else
  706. continue;
  707. /*
  708. * Some legacy x86 host bridge drivers use iomem_resource and
  709. * ioport_resource as default resource pool, skip it.
  710. */
  711. if (res == root)
  712. continue;
  713. conflict = insert_resource_conflict(root, res);
  714. if (conflict) {
  715. dev_info(&info->bridge->dev,
  716. "ignoring host bridge window %pR (conflicts with %s %pR)\n",
  717. res, conflict->name, conflict);
  718. resource_list_destroy_entry(entry);
  719. }
  720. }
  721. }
  722. static void __acpi_pci_root_release_info(struct acpi_pci_root_info *info)
  723. {
  724. struct resource *res;
  725. struct resource_entry *entry, *tmp;
  726. if (!info)
  727. return;
  728. resource_list_for_each_entry_safe(entry, tmp, &info->resources) {
  729. res = entry->res;
  730. if (res->parent &&
  731. (res->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
  732. release_resource(res);
  733. resource_list_destroy_entry(entry);
  734. }
  735. info->ops->release_info(info);
  736. }
  737. static void acpi_pci_root_release_info(struct pci_host_bridge *bridge)
  738. {
  739. struct resource *res;
  740. struct resource_entry *entry;
  741. resource_list_for_each_entry(entry, &bridge->windows) {
  742. res = entry->res;
  743. if (res->flags & IORESOURCE_IO)
  744. pci_unmap_iospace(res);
  745. if (res->parent &&
  746. (res->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
  747. release_resource(res);
  748. }
  749. __acpi_pci_root_release_info(bridge->release_data);
  750. }
  751. struct pci_bus *acpi_pci_root_create(struct acpi_pci_root *root,
  752. struct acpi_pci_root_ops *ops,
  753. struct acpi_pci_root_info *info,
  754. void *sysdata)
  755. {
  756. int ret, busnum = root->secondary.start;
  757. struct acpi_device *device = root->device;
  758. int node = acpi_get_node(device->handle);
  759. struct pci_bus *bus;
  760. struct pci_host_bridge *host_bridge;
  761. info->root = root;
  762. info->bridge = device;
  763. info->ops = ops;
  764. INIT_LIST_HEAD(&info->resources);
  765. snprintf(info->name, sizeof(info->name), "PCI Bus %04x:%02x",
  766. root->segment, busnum);
  767. if (ops->init_info && ops->init_info(info))
  768. goto out_release_info;
  769. if (ops->prepare_resources)
  770. ret = ops->prepare_resources(info);
  771. else
  772. ret = acpi_pci_probe_root_resources(info);
  773. if (ret < 0)
  774. goto out_release_info;
  775. pci_acpi_root_add_resources(info);
  776. pci_add_resource(&info->resources, &root->secondary);
  777. bus = pci_create_root_bus(NULL, busnum, ops->pci_ops,
  778. sysdata, &info->resources);
  779. if (!bus)
  780. goto out_release_info;
  781. host_bridge = to_pci_host_bridge(bus->bridge);
  782. if (!(root->osc_control_set & OSC_PCI_EXPRESS_NATIVE_HP_CONTROL))
  783. host_bridge->native_pcie_hotplug = 0;
  784. if (!(root->osc_control_set & OSC_PCI_SHPC_NATIVE_HP_CONTROL))
  785. host_bridge->native_shpc_hotplug = 0;
  786. if (!(root->osc_control_set & OSC_PCI_EXPRESS_AER_CONTROL))
  787. host_bridge->native_aer = 0;
  788. if (!(root->osc_control_set & OSC_PCI_EXPRESS_PME_CONTROL))
  789. host_bridge->native_pme = 0;
  790. if (!(root->osc_control_set & OSC_PCI_EXPRESS_LTR_CONTROL))
  791. host_bridge->native_ltr = 0;
  792. pci_scan_child_bus(bus);
  793. pci_set_host_bridge_release(host_bridge, acpi_pci_root_release_info,
  794. info);
  795. if (node != NUMA_NO_NODE)
  796. dev_printk(KERN_DEBUG, &bus->dev, "on NUMA node %d\n", node);
  797. return bus;
  798. out_release_info:
  799. __acpi_pci_root_release_info(info);
  800. return NULL;
  801. }
  802. void __init acpi_pci_root_init(void)
  803. {
  804. acpi_hest_init();
  805. if (acpi_pci_disabled)
  806. return;
  807. pci_acpi_crs_quirks();
  808. acpi_scan_add_handler_with_hotplug(&pci_root_handler, "pci_root");
  809. }