pci_root.c 25 KB

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