pci_root.c 16 KB

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