pci_root.c 19 KB

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