pci_root.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  22. *
  23. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/init.h>
  28. #include <linux/types.h>
  29. #include <linux/mutex.h>
  30. #include <linux/pm.h>
  31. #include <linux/pm_runtime.h>
  32. #include <linux/pci.h>
  33. #include <linux/pci-acpi.h>
  34. #include <linux/pci-aspm.h>
  35. #include <linux/acpi.h>
  36. #include <linux/slab.h>
  37. #include <acpi/acpi_bus.h>
  38. #include <acpi/acpi_drivers.h>
  39. #include <acpi/apei.h>
  40. #include "internal.h"
  41. #define PREFIX "ACPI: "
  42. #define _COMPONENT ACPI_PCI_COMPONENT
  43. ACPI_MODULE_NAME("pci_root");
  44. #define ACPI_PCI_ROOT_CLASS "pci_bridge"
  45. #define ACPI_PCI_ROOT_DEVICE_NAME "PCI Root Bridge"
  46. static int acpi_pci_root_add(struct acpi_device *device,
  47. const struct acpi_device_id *not_used);
  48. static void acpi_pci_root_remove(struct acpi_device *device);
  49. #define ACPI_PCIE_REQ_SUPPORT (OSC_PCI_EXT_CONFIG_SUPPORT \
  50. | OSC_PCI_ASPM_SUPPORT \
  51. | OSC_PCI_CLOCK_PM_SUPPORT \
  52. | OSC_PCI_MSI_SUPPORT)
  53. static const struct acpi_device_id root_device_ids[] = {
  54. {"PNP0A03", 0},
  55. {"", 0},
  56. };
  57. static struct acpi_scan_handler pci_root_handler = {
  58. .ids = root_device_ids,
  59. .attach = acpi_pci_root_add,
  60. .detach = acpi_pci_root_remove,
  61. };
  62. static DEFINE_MUTEX(osc_lock);
  63. /**
  64. * acpi_is_root_bridge - determine whether an ACPI CA node is a PCI root bridge
  65. * @handle - the ACPI CA node in question.
  66. *
  67. * Note: we could make this API take a struct acpi_device * instead, but
  68. * for now, it's more convenient to operate on an acpi_handle.
  69. */
  70. int acpi_is_root_bridge(acpi_handle handle)
  71. {
  72. int ret;
  73. struct acpi_device *device;
  74. ret = acpi_bus_get_device(handle, &device);
  75. if (ret)
  76. return 0;
  77. ret = acpi_match_device_ids(device, root_device_ids);
  78. if (ret)
  79. return 0;
  80. else
  81. return 1;
  82. }
  83. EXPORT_SYMBOL_GPL(acpi_is_root_bridge);
  84. static acpi_status
  85. get_root_bridge_busnr_callback(struct acpi_resource *resource, void *data)
  86. {
  87. struct resource *res = data;
  88. struct acpi_resource_address64 address;
  89. acpi_status status;
  90. status = acpi_resource_to_address64(resource, &address);
  91. if (ACPI_FAILURE(status))
  92. return AE_OK;
  93. if ((address.address_length > 0) &&
  94. (address.resource_type == ACPI_BUS_NUMBER_RANGE)) {
  95. res->start = address.minimum;
  96. res->end = address.minimum + address.address_length - 1;
  97. }
  98. return AE_OK;
  99. }
  100. static acpi_status try_get_root_bridge_busnr(acpi_handle handle,
  101. struct resource *res)
  102. {
  103. acpi_status status;
  104. res->start = -1;
  105. status =
  106. acpi_walk_resources(handle, METHOD_NAME__CRS,
  107. get_root_bridge_busnr_callback, res);
  108. if (ACPI_FAILURE(status))
  109. return status;
  110. if (res->start == -1)
  111. return AE_ERROR;
  112. return AE_OK;
  113. }
  114. struct pci_osc_bit_struct {
  115. u32 bit;
  116. char *desc;
  117. };
  118. static struct pci_osc_bit_struct pci_osc_support_bit[] = {
  119. { OSC_PCI_EXT_CONFIG_SUPPORT, "ExtendedConfig" },
  120. { OSC_PCI_ASPM_SUPPORT, "ASPM" },
  121. { OSC_PCI_CLOCK_PM_SUPPORT, "ClockPM" },
  122. { OSC_PCI_SEGMENT_GROUPS_SUPPORT, "Segments" },
  123. { OSC_PCI_MSI_SUPPORT, "MSI" },
  124. };
  125. static struct pci_osc_bit_struct pci_osc_control_bit[] = {
  126. { OSC_PCI_EXPRESS_NATIVE_HP_CONTROL, "PCIeHotplug" },
  127. { OSC_PCI_SHPC_NATIVE_HP_CONTROL, "SHPCHotplug" },
  128. { OSC_PCI_EXPRESS_PME_CONTROL, "PME" },
  129. { OSC_PCI_EXPRESS_AER_CONTROL, "AER" },
  130. { OSC_PCI_EXPRESS_CAPABILITY_CONTROL, "PCIeCapability" },
  131. };
  132. static void decode_osc_bits(struct acpi_pci_root *root, char *msg, u32 word,
  133. struct pci_osc_bit_struct *table, int size)
  134. {
  135. char buf[80];
  136. int i, len = 0;
  137. struct pci_osc_bit_struct *entry;
  138. buf[0] = '\0';
  139. for (i = 0, entry = table; i < size; i++, entry++)
  140. if (word & entry->bit)
  141. len += snprintf(buf + len, sizeof(buf) - len, "%s%s",
  142. len ? " " : "", entry->desc);
  143. dev_info(&root->device->dev, "_OSC: %s [%s]\n", msg, buf);
  144. }
  145. static void decode_osc_support(struct acpi_pci_root *root, char *msg, u32 word)
  146. {
  147. decode_osc_bits(root, msg, word, pci_osc_support_bit,
  148. ARRAY_SIZE(pci_osc_support_bit));
  149. }
  150. static void decode_osc_control(struct acpi_pci_root *root, char *msg, u32 word)
  151. {
  152. decode_osc_bits(root, msg, word, pci_osc_control_bit,
  153. ARRAY_SIZE(pci_osc_control_bit));
  154. }
  155. static u8 pci_osc_uuid_str[] = "33DB4D5B-1FF7-401C-9657-7441C03DD766";
  156. static acpi_status acpi_pci_run_osc(acpi_handle handle,
  157. const u32 *capbuf, u32 *retval)
  158. {
  159. struct acpi_osc_context context = {
  160. .uuid_str = pci_osc_uuid_str,
  161. .rev = 1,
  162. .cap.length = 12,
  163. .cap.pointer = (void *)capbuf,
  164. };
  165. acpi_status status;
  166. status = acpi_run_osc(handle, &context);
  167. if (ACPI_SUCCESS(status)) {
  168. *retval = *((u32 *)(context.ret.pointer + 8));
  169. kfree(context.ret.pointer);
  170. }
  171. return status;
  172. }
  173. static acpi_status acpi_pci_query_osc(struct acpi_pci_root *root,
  174. u32 support,
  175. u32 *control)
  176. {
  177. acpi_status status;
  178. u32 result, capbuf[3];
  179. support &= OSC_PCI_SUPPORT_MASKS;
  180. support |= root->osc_support_set;
  181. capbuf[OSC_QUERY_DWORD] = OSC_QUERY_ENABLE;
  182. capbuf[OSC_SUPPORT_DWORD] = support;
  183. if (control) {
  184. *control &= OSC_PCI_CONTROL_MASKS;
  185. capbuf[OSC_CONTROL_DWORD] = *control | root->osc_control_set;
  186. } else {
  187. /* Run _OSC query only with existing controls. */
  188. capbuf[OSC_CONTROL_DWORD] = root->osc_control_set;
  189. }
  190. status = acpi_pci_run_osc(root->device->handle, capbuf, &result);
  191. if (ACPI_SUCCESS(status)) {
  192. root->osc_support_set = support;
  193. if (control)
  194. *control = result;
  195. }
  196. return status;
  197. }
  198. static acpi_status acpi_pci_osc_support(struct acpi_pci_root *root, u32 flags)
  199. {
  200. acpi_status status;
  201. mutex_lock(&osc_lock);
  202. status = acpi_pci_query_osc(root, flags, NULL);
  203. mutex_unlock(&osc_lock);
  204. return status;
  205. }
  206. struct acpi_pci_root *acpi_pci_find_root(acpi_handle handle)
  207. {
  208. struct acpi_pci_root *root;
  209. struct acpi_device *device;
  210. if (acpi_bus_get_device(handle, &device) ||
  211. acpi_match_device_ids(device, root_device_ids))
  212. return NULL;
  213. root = acpi_driver_data(device);
  214. return root;
  215. }
  216. EXPORT_SYMBOL_GPL(acpi_pci_find_root);
  217. struct acpi_handle_node {
  218. struct list_head node;
  219. acpi_handle handle;
  220. };
  221. /**
  222. * acpi_get_pci_dev - convert ACPI CA handle to struct pci_dev
  223. * @handle: the handle in question
  224. *
  225. * Given an ACPI CA handle, the desired PCI device is located in the
  226. * list of PCI devices.
  227. *
  228. * If the device is found, its reference count is increased and this
  229. * function returns a pointer to its data structure. The caller must
  230. * decrement the reference count by calling pci_dev_put().
  231. * If no device is found, %NULL is returned.
  232. */
  233. struct pci_dev *acpi_get_pci_dev(acpi_handle handle)
  234. {
  235. int dev, fn;
  236. unsigned long long adr;
  237. acpi_status status;
  238. acpi_handle phandle;
  239. struct pci_bus *pbus;
  240. struct pci_dev *pdev = NULL;
  241. struct acpi_handle_node *node, *tmp;
  242. struct acpi_pci_root *root;
  243. LIST_HEAD(device_list);
  244. /*
  245. * Walk up the ACPI CA namespace until we reach a PCI root bridge.
  246. */
  247. phandle = handle;
  248. while (!acpi_is_root_bridge(phandle)) {
  249. node = kzalloc(sizeof(struct acpi_handle_node), GFP_KERNEL);
  250. if (!node)
  251. goto out;
  252. INIT_LIST_HEAD(&node->node);
  253. node->handle = phandle;
  254. list_add(&node->node, &device_list);
  255. status = acpi_get_parent(phandle, &phandle);
  256. if (ACPI_FAILURE(status))
  257. goto out;
  258. }
  259. root = acpi_pci_find_root(phandle);
  260. if (!root)
  261. goto out;
  262. pbus = root->bus;
  263. /*
  264. * Now, walk back down the PCI device tree until we return to our
  265. * original handle. Assumes that everything between the PCI root
  266. * bridge and the device we're looking for must be a P2P bridge.
  267. */
  268. list_for_each_entry(node, &device_list, node) {
  269. acpi_handle hnd = node->handle;
  270. status = acpi_evaluate_integer(hnd, "_ADR", NULL, &adr);
  271. if (ACPI_FAILURE(status))
  272. goto out;
  273. dev = (adr >> 16) & 0xffff;
  274. fn = adr & 0xffff;
  275. pdev = pci_get_slot(pbus, PCI_DEVFN(dev, fn));
  276. if (!pdev || hnd == handle)
  277. break;
  278. pbus = pdev->subordinate;
  279. pci_dev_put(pdev);
  280. /*
  281. * This function may be called for a non-PCI device that has a
  282. * PCI parent (eg. a disk under a PCI SATA controller). In that
  283. * case pdev->subordinate will be NULL for the parent.
  284. */
  285. if (!pbus) {
  286. dev_dbg(&pdev->dev, "Not a PCI-to-PCI bridge\n");
  287. pdev = NULL;
  288. break;
  289. }
  290. }
  291. out:
  292. list_for_each_entry_safe(node, tmp, &device_list, node)
  293. kfree(node);
  294. return pdev;
  295. }
  296. EXPORT_SYMBOL_GPL(acpi_get_pci_dev);
  297. /**
  298. * acpi_pci_osc_control_set - Request control of PCI root _OSC features.
  299. * @handle: ACPI handle of a PCI root bridge (or PCIe Root Complex).
  300. * @mask: Mask of _OSC bits to request control of, place to store control mask.
  301. * @req: Mask of _OSC bits the control of is essential to the caller.
  302. *
  303. * Run _OSC query for @mask and if that is successful, compare the returned
  304. * mask of control bits with @req. If all of the @req bits are set in the
  305. * returned mask, run _OSC request for it.
  306. *
  307. * The variable at the @mask address may be modified regardless of whether or
  308. * not the function returns success. On success it will contain the mask of
  309. * _OSC bits the BIOS has granted control of, but its contents are meaningless
  310. * on failure.
  311. **/
  312. acpi_status acpi_pci_osc_control_set(acpi_handle handle, u32 *mask, u32 req)
  313. {
  314. struct acpi_pci_root *root;
  315. acpi_status status = AE_OK;
  316. u32 ctrl, capbuf[3];
  317. if (!mask)
  318. return AE_BAD_PARAMETER;
  319. ctrl = *mask & OSC_PCI_CONTROL_MASKS;
  320. if ((ctrl & req) != req)
  321. return AE_TYPE;
  322. root = acpi_pci_find_root(handle);
  323. if (!root)
  324. return AE_NOT_EXIST;
  325. mutex_lock(&osc_lock);
  326. *mask = ctrl | root->osc_control_set;
  327. /* No need to evaluate _OSC if the control was already granted. */
  328. if ((root->osc_control_set & ctrl) == ctrl)
  329. goto out;
  330. /* Need to check the available controls bits before requesting them. */
  331. while (*mask) {
  332. status = acpi_pci_query_osc(root, root->osc_support_set, mask);
  333. if (ACPI_FAILURE(status))
  334. goto out;
  335. if (ctrl == *mask)
  336. break;
  337. decode_osc_control(root, "platform does not support",
  338. ctrl & ~(*mask));
  339. ctrl = *mask;
  340. }
  341. if ((ctrl & req) != req) {
  342. decode_osc_control(root, "not requesting control; platform does not support",
  343. req & ~(ctrl));
  344. status = AE_SUPPORT;
  345. goto out;
  346. }
  347. capbuf[OSC_QUERY_DWORD] = 0;
  348. capbuf[OSC_SUPPORT_DWORD] = root->osc_support_set;
  349. capbuf[OSC_CONTROL_DWORD] = ctrl;
  350. status = acpi_pci_run_osc(handle, capbuf, mask);
  351. if (ACPI_SUCCESS(status))
  352. root->osc_control_set = *mask;
  353. out:
  354. mutex_unlock(&osc_lock);
  355. return status;
  356. }
  357. EXPORT_SYMBOL(acpi_pci_osc_control_set);
  358. static void negotiate_os_control(struct acpi_pci_root *root, int *no_aspm,
  359. int *clear_aspm)
  360. {
  361. u32 support, control, requested;
  362. acpi_status status;
  363. struct acpi_device *device = root->device;
  364. acpi_handle handle = device->handle;
  365. /*
  366. * All supported architectures that use ACPI have support for
  367. * PCI domains, so we indicate this in _OSC support capabilities.
  368. */
  369. support = OSC_PCI_SEGMENT_GROUPS_SUPPORT;
  370. if (pci_ext_cfg_avail())
  371. support |= OSC_PCI_EXT_CONFIG_SUPPORT;
  372. if (pcie_aspm_support_enabled())
  373. support |= OSC_PCI_ASPM_SUPPORT | OSC_PCI_CLOCK_PM_SUPPORT;
  374. if (pci_msi_enabled())
  375. support |= OSC_PCI_MSI_SUPPORT;
  376. decode_osc_support(root, "OS supports", support);
  377. status = acpi_pci_osc_support(root, support);
  378. if (ACPI_FAILURE(status)) {
  379. dev_info(&device->dev, "_OSC failed (%s); disabling ASPM\n",
  380. acpi_format_exception(status));
  381. *no_aspm = 1;
  382. return;
  383. }
  384. if (pcie_ports_disabled) {
  385. dev_info(&device->dev, "PCIe port services disabled; not requesting _OSC control\n");
  386. return;
  387. }
  388. if ((support & ACPI_PCIE_REQ_SUPPORT) != ACPI_PCIE_REQ_SUPPORT) {
  389. decode_osc_support(root, "not requesting OS control; OS requires",
  390. ACPI_PCIE_REQ_SUPPORT);
  391. return;
  392. }
  393. control = OSC_PCI_EXPRESS_CAPABILITY_CONTROL
  394. | OSC_PCI_EXPRESS_NATIVE_HP_CONTROL
  395. | OSC_PCI_EXPRESS_PME_CONTROL;
  396. if (pci_aer_available()) {
  397. if (aer_acpi_firmware_first())
  398. dev_info(&device->dev,
  399. "PCIe AER handled by firmware\n");
  400. else
  401. control |= OSC_PCI_EXPRESS_AER_CONTROL;
  402. }
  403. requested = control;
  404. status = acpi_pci_osc_control_set(handle, &control,
  405. OSC_PCI_EXPRESS_CAPABILITY_CONTROL);
  406. if (ACPI_SUCCESS(status)) {
  407. decode_osc_control(root, "OS now controls", control);
  408. if (acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_ASPM) {
  409. /*
  410. * We have ASPM control, but the FADT indicates
  411. * that it's unsupported. Clear it.
  412. */
  413. *clear_aspm = 1;
  414. }
  415. } else {
  416. decode_osc_control(root, "OS requested", requested);
  417. decode_osc_control(root, "platform willing to grant", control);
  418. dev_info(&device->dev, "_OSC failed (%s); disabling ASPM\n",
  419. acpi_format_exception(status));
  420. /*
  421. * We want to disable ASPM here, but aspm_disabled
  422. * needs to remain in its state from boot so that we
  423. * properly handle PCIe 1.1 devices. So we set this
  424. * flag here, to defer the action until after the ACPI
  425. * root scan.
  426. */
  427. *no_aspm = 1;
  428. }
  429. }
  430. static int acpi_pci_root_add(struct acpi_device *device,
  431. const struct acpi_device_id *not_used)
  432. {
  433. unsigned long long segment, bus;
  434. acpi_status status;
  435. int result;
  436. struct acpi_pci_root *root;
  437. acpi_handle handle = device->handle;
  438. int no_aspm = 0, clear_aspm = 0;
  439. root = kzalloc(sizeof(struct acpi_pci_root), GFP_KERNEL);
  440. if (!root)
  441. return -ENOMEM;
  442. segment = 0;
  443. status = acpi_evaluate_integer(handle, METHOD_NAME__SEG, NULL,
  444. &segment);
  445. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
  446. dev_err(&device->dev, "can't evaluate _SEG\n");
  447. result = -ENODEV;
  448. goto end;
  449. }
  450. /* Check _CRS first, then _BBN. If no _BBN, default to zero. */
  451. root->secondary.flags = IORESOURCE_BUS;
  452. status = try_get_root_bridge_busnr(handle, &root->secondary);
  453. if (ACPI_FAILURE(status)) {
  454. /*
  455. * We need both the start and end of the downstream bus range
  456. * to interpret _CBA (MMCONFIG base address), so it really is
  457. * supposed to be in _CRS. If we don't find it there, all we
  458. * can do is assume [_BBN-0xFF] or [0-0xFF].
  459. */
  460. root->secondary.end = 0xFF;
  461. dev_warn(&device->dev,
  462. FW_BUG "no secondary bus range in _CRS\n");
  463. status = acpi_evaluate_integer(handle, METHOD_NAME__BBN,
  464. NULL, &bus);
  465. if (ACPI_SUCCESS(status))
  466. root->secondary.start = bus;
  467. else if (status == AE_NOT_FOUND)
  468. root->secondary.start = 0;
  469. else {
  470. dev_err(&device->dev, "can't evaluate _BBN\n");
  471. result = -ENODEV;
  472. goto end;
  473. }
  474. }
  475. root->device = device;
  476. root->segment = segment & 0xFFFF;
  477. strcpy(acpi_device_name(device), ACPI_PCI_ROOT_DEVICE_NAME);
  478. strcpy(acpi_device_class(device), ACPI_PCI_ROOT_CLASS);
  479. device->driver_data = root;
  480. pr_info(PREFIX "%s [%s] (domain %04x %pR)\n",
  481. acpi_device_name(device), acpi_device_bid(device),
  482. root->segment, &root->secondary);
  483. root->mcfg_addr = acpi_pci_root_get_mcfg_addr(handle);
  484. negotiate_os_control(root, &no_aspm, &clear_aspm);
  485. /*
  486. * TBD: Need PCI interface for enumeration/configuration of roots.
  487. */
  488. /*
  489. * Scan the Root Bridge
  490. * --------------------
  491. * Must do this prior to any attempt to bind the root device, as the
  492. * PCI namespace does not get created until this call is made (and
  493. * thus the root bridge's pci_dev does not exist).
  494. */
  495. root->bus = pci_acpi_scan_root(root);
  496. if (!root->bus) {
  497. dev_err(&device->dev,
  498. "Bus %04x:%02x not present in PCI namespace\n",
  499. root->segment, (unsigned int)root->secondary.start);
  500. device->driver_data = NULL;
  501. result = -ENODEV;
  502. goto end;
  503. }
  504. if (clear_aspm) {
  505. dev_info(&device->dev, "Disabling ASPM (FADT indicates it is unsupported)\n");
  506. pcie_clear_aspm(root->bus);
  507. }
  508. if (no_aspm)
  509. pcie_no_aspm();
  510. pci_acpi_add_bus_pm_notifier(device, root->bus);
  511. if (device->wakeup.flags.run_wake)
  512. device_set_run_wake(root->bus->bridge, true);
  513. if (system_state != SYSTEM_BOOTING) {
  514. pcibios_resource_survey_bus(root->bus);
  515. pci_assign_unassigned_root_bus_resources(root->bus);
  516. }
  517. pci_bus_add_devices(root->bus);
  518. return 1;
  519. end:
  520. kfree(root);
  521. return result;
  522. }
  523. static void acpi_pci_root_remove(struct acpi_device *device)
  524. {
  525. struct acpi_pci_root *root = acpi_driver_data(device);
  526. pci_stop_root_bus(root->bus);
  527. device_set_run_wake(root->bus->bridge, false);
  528. pci_acpi_remove_bus_pm_notifier(device);
  529. pci_remove_root_bus(root->bus);
  530. kfree(root);
  531. }
  532. void __init acpi_pci_root_init(void)
  533. {
  534. acpi_hest_init();
  535. if (!acpi_pci_disabled) {
  536. pci_acpi_crs_quirks();
  537. acpi_scan_add_handler(&pci_root_handler);
  538. }
  539. }
  540. /* Support root bridge hotplug */
  541. static void handle_root_bridge_insertion(acpi_handle handle)
  542. {
  543. struct acpi_device *device;
  544. if (!acpi_bus_get_device(handle, &device)) {
  545. dev_printk(KERN_DEBUG, &device->dev,
  546. "acpi device already exists; ignoring notify\n");
  547. return;
  548. }
  549. if (acpi_bus_scan(handle))
  550. acpi_handle_err(handle, "cannot add bridge to acpi list\n");
  551. }
  552. static void hotplug_event_root(void *data, u32 type)
  553. {
  554. acpi_handle handle = data;
  555. struct acpi_pci_root *root;
  556. acpi_scan_lock_acquire();
  557. root = acpi_pci_find_root(handle);
  558. switch (type) {
  559. case ACPI_NOTIFY_BUS_CHECK:
  560. /* bus enumerate */
  561. acpi_handle_printk(KERN_DEBUG, handle,
  562. "Bus check notify on %s\n", __func__);
  563. if (root)
  564. acpiphp_check_host_bridge(handle);
  565. else
  566. handle_root_bridge_insertion(handle);
  567. break;
  568. case ACPI_NOTIFY_DEVICE_CHECK:
  569. /* device check */
  570. acpi_handle_printk(KERN_DEBUG, handle,
  571. "Device check notify on %s\n", __func__);
  572. if (!root)
  573. handle_root_bridge_insertion(handle);
  574. break;
  575. case ACPI_NOTIFY_EJECT_REQUEST:
  576. /* request device eject */
  577. acpi_handle_printk(KERN_DEBUG, handle,
  578. "Device eject notify on %s\n", __func__);
  579. if (!root)
  580. break;
  581. get_device(&root->device->dev);
  582. acpi_scan_lock_release();
  583. acpi_bus_device_eject(root->device, ACPI_NOTIFY_EJECT_REQUEST);
  584. return;
  585. default:
  586. acpi_handle_warn(handle,
  587. "notify_handler: unknown event type 0x%x\n",
  588. type);
  589. break;
  590. }
  591. acpi_scan_lock_release();
  592. }
  593. static void handle_hotplug_event_root(acpi_handle handle, u32 type,
  594. void *context)
  595. {
  596. acpi_hotplug_execute(hotplug_event_root, handle, type);
  597. }
  598. static acpi_status __init
  599. find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
  600. {
  601. acpi_status status;
  602. int *count = (int *)context;
  603. if (!acpi_is_root_bridge(handle))
  604. return AE_OK;
  605. (*count)++;
  606. status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  607. handle_hotplug_event_root, NULL);
  608. if (ACPI_FAILURE(status))
  609. acpi_handle_printk(KERN_DEBUG, handle,
  610. "notify handler is not installed, exit status: %u\n",
  611. (unsigned int)status);
  612. else
  613. acpi_handle_printk(KERN_DEBUG, handle,
  614. "notify handler is installed\n");
  615. return AE_OK;
  616. }
  617. void __init acpi_pci_root_hp_init(void)
  618. {
  619. int num = 0;
  620. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  621. ACPI_UINT32_MAX, find_root_bridges, NULL, &num, NULL);
  622. printk(KERN_DEBUG "Found %d acpi root devices\n", num);
  623. }