pci_root.c 21 KB

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