pci_root.c 20 KB

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