pci_root.c 20 KB

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