pci_root.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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/proc_fs.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/pm.h>
  32. #include <linux/pm_runtime.h>
  33. #include <linux/pci.h>
  34. #include <linux/pci-acpi.h>
  35. #include <linux/acpi.h>
  36. #include <acpi/acpi_bus.h>
  37. #include <acpi/acpi_drivers.h>
  38. #define PREFIX "ACPI: "
  39. #define _COMPONENT ACPI_PCI_COMPONENT
  40. ACPI_MODULE_NAME("pci_root");
  41. #define ACPI_PCI_ROOT_CLASS "pci_bridge"
  42. #define ACPI_PCI_ROOT_DEVICE_NAME "PCI Root Bridge"
  43. static int acpi_pci_root_add(struct acpi_device *device);
  44. static int acpi_pci_root_remove(struct acpi_device *device, int type);
  45. static int acpi_pci_root_start(struct acpi_device *device);
  46. static const struct acpi_device_id root_device_ids[] = {
  47. {"PNP0A03", 0},
  48. {"", 0},
  49. };
  50. MODULE_DEVICE_TABLE(acpi, root_device_ids);
  51. static struct acpi_driver acpi_pci_root_driver = {
  52. .name = "pci_root",
  53. .class = ACPI_PCI_ROOT_CLASS,
  54. .ids = root_device_ids,
  55. .ops = {
  56. .add = acpi_pci_root_add,
  57. .remove = acpi_pci_root_remove,
  58. .start = acpi_pci_root_start,
  59. },
  60. };
  61. static LIST_HEAD(acpi_pci_roots);
  62. static struct acpi_pci_driver *sub_driver;
  63. static DEFINE_MUTEX(osc_lock);
  64. int acpi_pci_register_driver(struct acpi_pci_driver *driver)
  65. {
  66. int n = 0;
  67. struct acpi_pci_root *root;
  68. struct acpi_pci_driver **pptr = &sub_driver;
  69. while (*pptr)
  70. pptr = &(*pptr)->next;
  71. *pptr = driver;
  72. if (!driver->add)
  73. return 0;
  74. list_for_each_entry(root, &acpi_pci_roots, node) {
  75. driver->add(root->device->handle);
  76. n++;
  77. }
  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. struct acpi_pci_driver **pptr = &sub_driver;
  85. while (*pptr) {
  86. if (*pptr == driver)
  87. break;
  88. pptr = &(*pptr)->next;
  89. }
  90. BUG_ON(!*pptr);
  91. *pptr = (*pptr)->next;
  92. if (!driver->remove)
  93. return;
  94. list_for_each_entry(root, &acpi_pci_roots, node)
  95. driver->remove(root->device->handle);
  96. }
  97. EXPORT_SYMBOL(acpi_pci_unregister_driver);
  98. acpi_handle acpi_get_pci_rootbridge_handle(unsigned int seg, unsigned int bus)
  99. {
  100. struct acpi_pci_root *root;
  101. list_for_each_entry(root, &acpi_pci_roots, node)
  102. if ((root->segment == (u16) seg) && (root->bus_nr == (u16) bus))
  103. return root->device->handle;
  104. return NULL;
  105. }
  106. EXPORT_SYMBOL_GPL(acpi_get_pci_rootbridge_handle);
  107. /**
  108. * acpi_is_root_bridge - determine whether an ACPI CA node is a PCI root bridge
  109. * @handle - the ACPI CA node in question.
  110. *
  111. * Note: we could make this API take a struct acpi_device * instead, but
  112. * for now, it's more convenient to operate on an acpi_handle.
  113. */
  114. int acpi_is_root_bridge(acpi_handle handle)
  115. {
  116. int ret;
  117. struct acpi_device *device;
  118. ret = acpi_bus_get_device(handle, &device);
  119. if (ret)
  120. return 0;
  121. ret = acpi_match_device_ids(device, root_device_ids);
  122. if (ret)
  123. return 0;
  124. else
  125. return 1;
  126. }
  127. EXPORT_SYMBOL_GPL(acpi_is_root_bridge);
  128. static acpi_status
  129. get_root_bridge_busnr_callback(struct acpi_resource *resource, void *data)
  130. {
  131. int *busnr = data;
  132. struct acpi_resource_address64 address;
  133. if (resource->type != ACPI_RESOURCE_TYPE_ADDRESS16 &&
  134. resource->type != ACPI_RESOURCE_TYPE_ADDRESS32 &&
  135. resource->type != ACPI_RESOURCE_TYPE_ADDRESS64)
  136. return AE_OK;
  137. acpi_resource_to_address64(resource, &address);
  138. if ((address.address_length > 0) &&
  139. (address.resource_type == ACPI_BUS_NUMBER_RANGE))
  140. *busnr = address.minimum;
  141. return AE_OK;
  142. }
  143. static acpi_status try_get_root_bridge_busnr(acpi_handle handle,
  144. unsigned long long *bus)
  145. {
  146. acpi_status status;
  147. int busnum;
  148. busnum = -1;
  149. status =
  150. acpi_walk_resources(handle, METHOD_NAME__CRS,
  151. get_root_bridge_busnr_callback, &busnum);
  152. if (ACPI_FAILURE(status))
  153. return status;
  154. /* Check if we really get a bus number from _CRS */
  155. if (busnum == -1)
  156. return AE_ERROR;
  157. *bus = busnum;
  158. return AE_OK;
  159. }
  160. static void acpi_pci_bridge_scan(struct acpi_device *device)
  161. {
  162. int status;
  163. struct acpi_device *child = NULL;
  164. if (device->flags.bus_address)
  165. if (device->parent && device->parent->ops.bind) {
  166. status = device->parent->ops.bind(device);
  167. if (!status) {
  168. list_for_each_entry(child, &device->children, node)
  169. acpi_pci_bridge_scan(child);
  170. }
  171. }
  172. }
  173. static u8 pci_osc_uuid_str[] = "33DB4D5B-1FF7-401C-9657-7441C03DD766";
  174. static acpi_status acpi_pci_run_osc(acpi_handle handle,
  175. const u32 *capbuf, u32 *retval)
  176. {
  177. struct acpi_osc_context context = {
  178. .uuid_str = pci_osc_uuid_str,
  179. .rev = 1,
  180. .cap.length = 12,
  181. .cap.pointer = (void *)capbuf,
  182. };
  183. acpi_status status;
  184. status = acpi_run_osc(handle, &context);
  185. if (ACPI_SUCCESS(status)) {
  186. *retval = *((u32 *)(context.ret.pointer + 8));
  187. kfree(context.ret.pointer);
  188. }
  189. return status;
  190. }
  191. static acpi_status acpi_pci_query_osc(struct acpi_pci_root *root, u32 flags)
  192. {
  193. acpi_status status;
  194. u32 support_set, result, capbuf[3];
  195. /* do _OSC query for all possible controls */
  196. support_set = root->osc_support_set | (flags & OSC_PCI_SUPPORT_MASKS);
  197. capbuf[OSC_QUERY_TYPE] = OSC_QUERY_ENABLE;
  198. capbuf[OSC_SUPPORT_TYPE] = support_set;
  199. capbuf[OSC_CONTROL_TYPE] = OSC_PCI_CONTROL_MASKS;
  200. status = acpi_pci_run_osc(root->device->handle, capbuf, &result);
  201. if (ACPI_SUCCESS(status)) {
  202. root->osc_support_set = support_set;
  203. root->osc_control_qry = result;
  204. root->osc_queried = 1;
  205. }
  206. return status;
  207. }
  208. static acpi_status acpi_pci_osc_support(struct acpi_pci_root *root, u32 flags)
  209. {
  210. acpi_status status;
  211. acpi_handle tmp;
  212. status = acpi_get_handle(root->device->handle, "_OSC", &tmp);
  213. if (ACPI_FAILURE(status))
  214. return status;
  215. mutex_lock(&osc_lock);
  216. status = acpi_pci_query_osc(root, flags);
  217. mutex_unlock(&osc_lock);
  218. return status;
  219. }
  220. struct acpi_pci_root *acpi_pci_find_root(acpi_handle handle)
  221. {
  222. struct acpi_pci_root *root;
  223. list_for_each_entry(root, &acpi_pci_roots, node) {
  224. if (root->device->handle == handle)
  225. return root;
  226. }
  227. return NULL;
  228. }
  229. EXPORT_SYMBOL_GPL(acpi_pci_find_root);
  230. struct acpi_handle_node {
  231. struct list_head node;
  232. acpi_handle handle;
  233. };
  234. /**
  235. * acpi_get_pci_dev - convert ACPI CA handle to struct pci_dev
  236. * @handle: the handle in question
  237. *
  238. * Given an ACPI CA handle, the desired PCI device is located in the
  239. * list of PCI devices.
  240. *
  241. * If the device is found, its reference count is increased and this
  242. * function returns a pointer to its data structure. The caller must
  243. * decrement the reference count by calling pci_dev_put().
  244. * If no device is found, %NULL is returned.
  245. */
  246. struct pci_dev *acpi_get_pci_dev(acpi_handle handle)
  247. {
  248. int dev, fn;
  249. unsigned long long adr;
  250. acpi_status status;
  251. acpi_handle phandle;
  252. struct pci_bus *pbus;
  253. struct pci_dev *pdev = NULL;
  254. struct acpi_handle_node *node, *tmp;
  255. struct acpi_pci_root *root;
  256. LIST_HEAD(device_list);
  257. /*
  258. * Walk up the ACPI CA namespace until we reach a PCI root bridge.
  259. */
  260. phandle = handle;
  261. while (!acpi_is_root_bridge(phandle)) {
  262. node = kzalloc(sizeof(struct acpi_handle_node), GFP_KERNEL);
  263. if (!node)
  264. goto out;
  265. INIT_LIST_HEAD(&node->node);
  266. node->handle = phandle;
  267. list_add(&node->node, &device_list);
  268. status = acpi_get_parent(phandle, &phandle);
  269. if (ACPI_FAILURE(status))
  270. goto out;
  271. }
  272. root = acpi_pci_find_root(phandle);
  273. if (!root)
  274. goto out;
  275. pbus = root->bus;
  276. /*
  277. * Now, walk back down the PCI device tree until we return to our
  278. * original handle. Assumes that everything between the PCI root
  279. * bridge and the device we're looking for must be a P2P bridge.
  280. */
  281. list_for_each_entry(node, &device_list, node) {
  282. acpi_handle hnd = node->handle;
  283. status = acpi_evaluate_integer(hnd, "_ADR", NULL, &adr);
  284. if (ACPI_FAILURE(status))
  285. goto out;
  286. dev = (adr >> 16) & 0xffff;
  287. fn = adr & 0xffff;
  288. pdev = pci_get_slot(pbus, PCI_DEVFN(dev, fn));
  289. if (!pdev || hnd == handle)
  290. break;
  291. pbus = pdev->subordinate;
  292. pci_dev_put(pdev);
  293. /*
  294. * This function may be called for a non-PCI device that has a
  295. * PCI parent (eg. a disk under a PCI SATA controller). In that
  296. * case pdev->subordinate will be NULL for the parent.
  297. */
  298. if (!pbus) {
  299. dev_dbg(&pdev->dev, "Not a PCI-to-PCI bridge\n");
  300. pdev = NULL;
  301. break;
  302. }
  303. }
  304. out:
  305. list_for_each_entry_safe(node, tmp, &device_list, node)
  306. kfree(node);
  307. return pdev;
  308. }
  309. EXPORT_SYMBOL_GPL(acpi_get_pci_dev);
  310. /**
  311. * acpi_pci_osc_control_set - commit requested control to Firmware
  312. * @handle: acpi_handle for the target ACPI object
  313. * @flags: driver's requested control bits
  314. *
  315. * Attempt to take control from Firmware on requested control bits.
  316. **/
  317. acpi_status acpi_pci_osc_control_set(acpi_handle handle, u32 flags)
  318. {
  319. acpi_status status;
  320. u32 control_req, result, capbuf[3];
  321. acpi_handle tmp;
  322. struct acpi_pci_root *root;
  323. status = acpi_get_handle(handle, "_OSC", &tmp);
  324. if (ACPI_FAILURE(status))
  325. return status;
  326. control_req = (flags & OSC_PCI_CONTROL_MASKS);
  327. if (!control_req)
  328. return AE_TYPE;
  329. root = acpi_pci_find_root(handle);
  330. if (!root)
  331. return AE_NOT_EXIST;
  332. mutex_lock(&osc_lock);
  333. /* No need to evaluate _OSC if the control was already granted. */
  334. if ((root->osc_control_set & control_req) == control_req)
  335. goto out;
  336. /* Need to query controls first before requesting them */
  337. if (!root->osc_queried) {
  338. status = acpi_pci_query_osc(root, root->osc_support_set);
  339. if (ACPI_FAILURE(status))
  340. goto out;
  341. }
  342. if ((root->osc_control_qry & control_req) != control_req) {
  343. printk(KERN_DEBUG
  344. "Firmware did not grant requested _OSC control\n");
  345. status = AE_SUPPORT;
  346. goto out;
  347. }
  348. capbuf[OSC_QUERY_TYPE] = 0;
  349. capbuf[OSC_SUPPORT_TYPE] = root->osc_support_set;
  350. capbuf[OSC_CONTROL_TYPE] = root->osc_control_set | control_req;
  351. status = acpi_pci_run_osc(handle, capbuf, &result);
  352. if (ACPI_SUCCESS(status))
  353. root->osc_control_set = result;
  354. out:
  355. mutex_unlock(&osc_lock);
  356. return status;
  357. }
  358. EXPORT_SYMBOL(acpi_pci_osc_control_set);
  359. static int __devinit acpi_pci_root_add(struct acpi_device *device)
  360. {
  361. unsigned long long segment, bus;
  362. acpi_status status;
  363. int result;
  364. struct acpi_pci_root *root;
  365. acpi_handle handle;
  366. struct acpi_device *child;
  367. u32 flags, base_flags;
  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. return -ENODEV;
  374. }
  375. /* Check _CRS first, then _BBN. If no _BBN, default to zero. */
  376. bus = 0;
  377. status = try_get_root_bridge_busnr(device->handle, &bus);
  378. if (ACPI_FAILURE(status)) {
  379. status = acpi_evaluate_integer(device->handle, METHOD_NAME__BBN, NULL, &bus);
  380. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
  381. printk(KERN_ERR PREFIX
  382. "no bus number in _CRS and can't evaluate _BBN\n");
  383. return -ENODEV;
  384. }
  385. }
  386. root = kzalloc(sizeof(struct acpi_pci_root), GFP_KERNEL);
  387. if (!root)
  388. return -ENOMEM;
  389. INIT_LIST_HEAD(&root->node);
  390. root->device = device;
  391. root->segment = segment & 0xFFFF;
  392. root->bus_nr = bus & 0xFF;
  393. strcpy(acpi_device_name(device), ACPI_PCI_ROOT_DEVICE_NAME);
  394. strcpy(acpi_device_class(device), ACPI_PCI_ROOT_CLASS);
  395. device->driver_data = root;
  396. /*
  397. * All supported architectures that use ACPI have support for
  398. * PCI domains, so we indicate this in _OSC support capabilities.
  399. */
  400. flags = base_flags = OSC_PCI_SEGMENT_GROUPS_SUPPORT;
  401. acpi_pci_osc_support(root, flags);
  402. /*
  403. * TBD: Need PCI interface for enumeration/configuration of roots.
  404. */
  405. /* TBD: Locking */
  406. list_add_tail(&root->node, &acpi_pci_roots);
  407. printk(KERN_INFO PREFIX "%s [%s] (%04x:%02x)\n",
  408. acpi_device_name(device), acpi_device_bid(device),
  409. root->segment, root->bus_nr);
  410. /*
  411. * Scan the Root Bridge
  412. * --------------------
  413. * Must do this prior to any attempt to bind the root device, as the
  414. * PCI namespace does not get created until this call is made (and
  415. * thus the root bridge's pci_dev does not exist).
  416. */
  417. root->bus = pci_acpi_scan_root(device, segment, bus);
  418. if (!root->bus) {
  419. printk(KERN_ERR PREFIX
  420. "Bus %04x:%02x not present in PCI namespace\n",
  421. root->segment, root->bus_nr);
  422. result = -ENODEV;
  423. goto end;
  424. }
  425. /*
  426. * Attach ACPI-PCI Context
  427. * -----------------------
  428. * Thus binding the ACPI and PCI devices.
  429. */
  430. result = acpi_pci_bind_root(device);
  431. if (result)
  432. goto end;
  433. /*
  434. * PCI Routing Table
  435. * -----------------
  436. * Evaluate and parse _PRT, if exists.
  437. */
  438. status = acpi_get_handle(device->handle, METHOD_NAME__PRT, &handle);
  439. if (ACPI_SUCCESS(status))
  440. result = acpi_pci_irq_add_prt(device->handle, root->bus);
  441. /*
  442. * Scan and bind all _ADR-Based Devices
  443. */
  444. list_for_each_entry(child, &device->children, node)
  445. acpi_pci_bridge_scan(child);
  446. /* Indicate support for various _OSC capabilities. */
  447. if (pci_ext_cfg_avail(root->bus->self))
  448. flags |= OSC_EXT_PCI_CONFIG_SUPPORT;
  449. if (pcie_aspm_enabled())
  450. flags |= OSC_ACTIVE_STATE_PWR_SUPPORT |
  451. OSC_CLOCK_PWR_CAPABILITY_SUPPORT;
  452. if (pci_msi_enabled())
  453. flags |= OSC_MSI_SUPPORT;
  454. if (flags != base_flags)
  455. acpi_pci_osc_support(root, flags);
  456. pci_acpi_add_bus_pm_notifier(device, root->bus);
  457. if (device->wakeup.flags.run_wake)
  458. device_set_run_wake(root->bus->bridge, true);
  459. return 0;
  460. end:
  461. if (!list_empty(&root->node))
  462. list_del(&root->node);
  463. kfree(root);
  464. return result;
  465. }
  466. static int acpi_pci_root_start(struct acpi_device *device)
  467. {
  468. struct acpi_pci_root *root = acpi_driver_data(device);
  469. pci_bus_add_devices(root->bus);
  470. return 0;
  471. }
  472. static int acpi_pci_root_remove(struct acpi_device *device, int type)
  473. {
  474. struct acpi_pci_root *root = acpi_driver_data(device);
  475. device_set_run_wake(root->bus->bridge, false);
  476. pci_acpi_remove_bus_pm_notifier(device);
  477. kfree(root);
  478. return 0;
  479. }
  480. static int __init acpi_pci_root_init(void)
  481. {
  482. if (acpi_pci_disabled)
  483. return 0;
  484. if (acpi_bus_register_driver(&acpi_pci_root_driver) < 0)
  485. return -ENODEV;
  486. return 0;
  487. }
  488. subsys_initcall(acpi_pci_root_init);