pci_root.c 19 KB

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