pci_root.c 14 KB

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