pci_root.c 19 KB

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