pci_root.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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 OSC_UUID[16] = {0x5B, 0x4D, 0xDB, 0x33, 0xF7, 0x1F, 0x1C, 0x40,
  173. 0x96, 0x57, 0x74, 0x41, 0xC0, 0x3D, 0xD7, 0x66};
  174. static acpi_status acpi_pci_run_osc(acpi_handle handle,
  175. const u32 *capbuf, u32 *retval)
  176. {
  177. acpi_status status;
  178. struct acpi_object_list input;
  179. union acpi_object in_params[4];
  180. struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
  181. union acpi_object *out_obj;
  182. u32 errors;
  183. /* Setting up input parameters */
  184. input.count = 4;
  185. input.pointer = in_params;
  186. in_params[0].type = ACPI_TYPE_BUFFER;
  187. in_params[0].buffer.length = 16;
  188. in_params[0].buffer.pointer = OSC_UUID;
  189. in_params[1].type = ACPI_TYPE_INTEGER;
  190. in_params[1].integer.value = 1;
  191. in_params[2].type = ACPI_TYPE_INTEGER;
  192. in_params[2].integer.value = 3;
  193. in_params[3].type = ACPI_TYPE_BUFFER;
  194. in_params[3].buffer.length = 12;
  195. in_params[3].buffer.pointer = (u8 *)capbuf;
  196. status = acpi_evaluate_object(handle, "_OSC", &input, &output);
  197. if (ACPI_FAILURE(status))
  198. return status;
  199. if (!output.length)
  200. return AE_NULL_OBJECT;
  201. out_obj = output.pointer;
  202. if (out_obj->type != ACPI_TYPE_BUFFER) {
  203. printk(KERN_DEBUG "_OSC evaluation returned wrong type\n");
  204. status = AE_TYPE;
  205. goto out_kfree;
  206. }
  207. /* Need to ignore the bit0 in result code */
  208. errors = *((u32 *)out_obj->buffer.pointer) & ~(1 << 0);
  209. if (errors) {
  210. if (errors & OSC_REQUEST_ERROR)
  211. printk(KERN_DEBUG "_OSC request failed\n");
  212. if (errors & OSC_INVALID_UUID_ERROR)
  213. printk(KERN_DEBUG "_OSC invalid UUID\n");
  214. if (errors & OSC_INVALID_REVISION_ERROR)
  215. printk(KERN_DEBUG "_OSC invalid revision\n");
  216. if (errors & OSC_CAPABILITIES_MASK_ERROR) {
  217. if (capbuf[OSC_QUERY_TYPE] & OSC_QUERY_ENABLE)
  218. goto out_success;
  219. printk(KERN_DEBUG
  220. "Firmware did not grant requested _OSC control\n");
  221. status = AE_SUPPORT;
  222. goto out_kfree;
  223. }
  224. status = AE_ERROR;
  225. goto out_kfree;
  226. }
  227. out_success:
  228. *retval = *((u32 *)(out_obj->buffer.pointer + 8));
  229. status = AE_OK;
  230. out_kfree:
  231. kfree(output.pointer);
  232. return status;
  233. }
  234. static acpi_status acpi_pci_query_osc(struct acpi_pci_root *root, u32 flags)
  235. {
  236. acpi_status status;
  237. u32 support_set, result, capbuf[3];
  238. /* do _OSC query for all possible controls */
  239. support_set = root->osc_support_set | (flags & OSC_SUPPORT_MASKS);
  240. capbuf[OSC_QUERY_TYPE] = OSC_QUERY_ENABLE;
  241. capbuf[OSC_SUPPORT_TYPE] = support_set;
  242. capbuf[OSC_CONTROL_TYPE] = OSC_CONTROL_MASKS;
  243. status = acpi_pci_run_osc(root->device->handle, capbuf, &result);
  244. if (ACPI_SUCCESS(status)) {
  245. root->osc_support_set = support_set;
  246. root->osc_control_qry = result;
  247. root->osc_queried = 1;
  248. }
  249. return status;
  250. }
  251. static acpi_status acpi_pci_osc_support(struct acpi_pci_root *root, u32 flags)
  252. {
  253. acpi_status status;
  254. acpi_handle tmp;
  255. status = acpi_get_handle(root->device->handle, "_OSC", &tmp);
  256. if (ACPI_FAILURE(status))
  257. return status;
  258. mutex_lock(&osc_lock);
  259. status = acpi_pci_query_osc(root, flags);
  260. mutex_unlock(&osc_lock);
  261. return status;
  262. }
  263. struct acpi_pci_root *acpi_pci_find_root(acpi_handle handle)
  264. {
  265. struct acpi_pci_root *root;
  266. list_for_each_entry(root, &acpi_pci_roots, node) {
  267. if (root->device->handle == handle)
  268. return root;
  269. }
  270. return NULL;
  271. }
  272. EXPORT_SYMBOL_GPL(acpi_pci_find_root);
  273. struct acpi_handle_node {
  274. struct list_head node;
  275. acpi_handle handle;
  276. };
  277. /**
  278. * acpi_get_pci_dev - convert ACPI CA handle to struct pci_dev
  279. * @handle: the handle in question
  280. *
  281. * Given an ACPI CA handle, the desired PCI device is located in the
  282. * list of PCI devices.
  283. *
  284. * If the device is found, its reference count is increased and this
  285. * function returns a pointer to its data structure. The caller must
  286. * decrement the reference count by calling pci_dev_put().
  287. * If no device is found, %NULL is returned.
  288. */
  289. struct pci_dev *acpi_get_pci_dev(acpi_handle handle)
  290. {
  291. int dev, fn;
  292. unsigned long long adr;
  293. acpi_status status;
  294. acpi_handle phandle;
  295. struct pci_bus *pbus;
  296. struct pci_dev *pdev = NULL;
  297. struct acpi_handle_node *node, *tmp;
  298. struct acpi_pci_root *root;
  299. LIST_HEAD(device_list);
  300. /*
  301. * Walk up the ACPI CA namespace until we reach a PCI root bridge.
  302. */
  303. phandle = handle;
  304. while (!acpi_is_root_bridge(phandle)) {
  305. node = kzalloc(sizeof(struct acpi_handle_node), GFP_KERNEL);
  306. if (!node)
  307. goto out;
  308. INIT_LIST_HEAD(&node->node);
  309. node->handle = phandle;
  310. list_add(&node->node, &device_list);
  311. status = acpi_get_parent(phandle, &phandle);
  312. if (ACPI_FAILURE(status))
  313. goto out;
  314. }
  315. root = acpi_pci_find_root(phandle);
  316. if (!root)
  317. goto out;
  318. pbus = root->bus;
  319. /*
  320. * Now, walk back down the PCI device tree until we return to our
  321. * original handle. Assumes that everything between the PCI root
  322. * bridge and the device we're looking for must be a P2P bridge.
  323. */
  324. list_for_each_entry(node, &device_list, node) {
  325. acpi_handle hnd = node->handle;
  326. status = acpi_evaluate_integer(hnd, "_ADR", NULL, &adr);
  327. if (ACPI_FAILURE(status))
  328. goto out;
  329. dev = (adr >> 16) & 0xffff;
  330. fn = adr & 0xffff;
  331. pdev = pci_get_slot(pbus, PCI_DEVFN(dev, fn));
  332. if (!pdev || hnd == handle)
  333. break;
  334. pbus = pdev->subordinate;
  335. pci_dev_put(pdev);
  336. /*
  337. * This function may be called for a non-PCI device that has a
  338. * PCI parent (eg. a disk under a PCI SATA controller). In that
  339. * case pdev->subordinate will be NULL for the parent.
  340. */
  341. if (!pbus) {
  342. dev_dbg(&pdev->dev, "Not a PCI-to-PCI bridge\n");
  343. pdev = NULL;
  344. break;
  345. }
  346. }
  347. out:
  348. list_for_each_entry_safe(node, tmp, &device_list, node)
  349. kfree(node);
  350. return pdev;
  351. }
  352. EXPORT_SYMBOL_GPL(acpi_get_pci_dev);
  353. /**
  354. * acpi_pci_osc_control_set - commit requested control to Firmware
  355. * @handle: acpi_handle for the target ACPI object
  356. * @flags: driver's requested control bits
  357. *
  358. * Attempt to take control from Firmware on requested control bits.
  359. **/
  360. acpi_status acpi_pci_osc_control_set(acpi_handle handle, u32 flags)
  361. {
  362. acpi_status status;
  363. u32 control_req, result, capbuf[3];
  364. acpi_handle tmp;
  365. struct acpi_pci_root *root;
  366. status = acpi_get_handle(handle, "_OSC", &tmp);
  367. if (ACPI_FAILURE(status))
  368. return status;
  369. control_req = (flags & OSC_CONTROL_MASKS);
  370. if (!control_req)
  371. return AE_TYPE;
  372. root = acpi_pci_find_root(handle);
  373. if (!root)
  374. return AE_NOT_EXIST;
  375. mutex_lock(&osc_lock);
  376. /* No need to evaluate _OSC if the control was already granted. */
  377. if ((root->osc_control_set & control_req) == control_req)
  378. goto out;
  379. /* Need to query controls first before requesting them */
  380. if (!root->osc_queried) {
  381. status = acpi_pci_query_osc(root, root->osc_support_set);
  382. if (ACPI_FAILURE(status))
  383. goto out;
  384. }
  385. if ((root->osc_control_qry & control_req) != control_req) {
  386. printk(KERN_DEBUG
  387. "Firmware did not grant requested _OSC control\n");
  388. status = AE_SUPPORT;
  389. goto out;
  390. }
  391. capbuf[OSC_QUERY_TYPE] = 0;
  392. capbuf[OSC_SUPPORT_TYPE] = root->osc_support_set;
  393. capbuf[OSC_CONTROL_TYPE] = root->osc_control_set | control_req;
  394. status = acpi_pci_run_osc(handle, capbuf, &result);
  395. if (ACPI_SUCCESS(status))
  396. root->osc_control_set = result;
  397. out:
  398. mutex_unlock(&osc_lock);
  399. return status;
  400. }
  401. EXPORT_SYMBOL(acpi_pci_osc_control_set);
  402. static int __devinit acpi_pci_root_add(struct acpi_device *device)
  403. {
  404. unsigned long long segment, bus;
  405. acpi_status status;
  406. int result;
  407. struct acpi_pci_root *root;
  408. acpi_handle handle;
  409. struct acpi_device *child;
  410. u32 flags, base_flags;
  411. segment = 0;
  412. status = acpi_evaluate_integer(device->handle, METHOD_NAME__SEG, NULL,
  413. &segment);
  414. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
  415. printk(KERN_ERR PREFIX "can't evaluate _SEG\n");
  416. return -ENODEV;
  417. }
  418. /* Check _CRS first, then _BBN. If no _BBN, default to zero. */
  419. bus = 0;
  420. status = try_get_root_bridge_busnr(device->handle, &bus);
  421. if (ACPI_FAILURE(status)) {
  422. status = acpi_evaluate_integer(device->handle, METHOD_NAME__BBN, NULL, &bus);
  423. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
  424. printk(KERN_ERR PREFIX
  425. "no bus number in _CRS and can't evaluate _BBN\n");
  426. return -ENODEV;
  427. }
  428. }
  429. root = kzalloc(sizeof(struct acpi_pci_root), GFP_KERNEL);
  430. if (!root)
  431. return -ENOMEM;
  432. INIT_LIST_HEAD(&root->node);
  433. root->device = device;
  434. root->segment = segment & 0xFFFF;
  435. root->bus_nr = bus & 0xFF;
  436. strcpy(acpi_device_name(device), ACPI_PCI_ROOT_DEVICE_NAME);
  437. strcpy(acpi_device_class(device), ACPI_PCI_ROOT_CLASS);
  438. device->driver_data = root;
  439. /*
  440. * All supported architectures that use ACPI have support for
  441. * PCI domains, so we indicate this in _OSC support capabilities.
  442. */
  443. flags = base_flags = OSC_PCI_SEGMENT_GROUPS_SUPPORT;
  444. acpi_pci_osc_support(root, flags);
  445. /*
  446. * TBD: Need PCI interface for enumeration/configuration of roots.
  447. */
  448. /* TBD: Locking */
  449. list_add_tail(&root->node, &acpi_pci_roots);
  450. printk(KERN_INFO PREFIX "%s [%s] (%04x:%02x)\n",
  451. acpi_device_name(device), acpi_device_bid(device),
  452. root->segment, root->bus_nr);
  453. /*
  454. * Scan the Root Bridge
  455. * --------------------
  456. * Must do this prior to any attempt to bind the root device, as the
  457. * PCI namespace does not get created until this call is made (and
  458. * thus the root bridge's pci_dev does not exist).
  459. */
  460. root->bus = pci_acpi_scan_root(device, segment, bus);
  461. if (!root->bus) {
  462. printk(KERN_ERR PREFIX
  463. "Bus %04x:%02x not present in PCI namespace\n",
  464. root->segment, root->bus_nr);
  465. result = -ENODEV;
  466. goto end;
  467. }
  468. /*
  469. * Attach ACPI-PCI Context
  470. * -----------------------
  471. * Thus binding the ACPI and PCI devices.
  472. */
  473. result = acpi_pci_bind_root(device);
  474. if (result)
  475. goto end;
  476. /*
  477. * PCI Routing Table
  478. * -----------------
  479. * Evaluate and parse _PRT, if exists.
  480. */
  481. status = acpi_get_handle(device->handle, METHOD_NAME__PRT, &handle);
  482. if (ACPI_SUCCESS(status))
  483. result = acpi_pci_irq_add_prt(device->handle, root->bus);
  484. /*
  485. * Scan and bind all _ADR-Based Devices
  486. */
  487. list_for_each_entry(child, &device->children, node)
  488. acpi_pci_bridge_scan(child);
  489. /* Indicate support for various _OSC capabilities. */
  490. if (pci_ext_cfg_avail(root->bus->self))
  491. flags |= OSC_EXT_PCI_CONFIG_SUPPORT;
  492. if (pcie_aspm_enabled())
  493. flags |= OSC_ACTIVE_STATE_PWR_SUPPORT |
  494. OSC_CLOCK_PWR_CAPABILITY_SUPPORT;
  495. if (pci_msi_enabled())
  496. flags |= OSC_MSI_SUPPORT;
  497. if (flags != base_flags)
  498. acpi_pci_osc_support(root, flags);
  499. return 0;
  500. end:
  501. if (!list_empty(&root->node))
  502. list_del(&root->node);
  503. kfree(root);
  504. return result;
  505. }
  506. static int acpi_pci_root_start(struct acpi_device *device)
  507. {
  508. struct acpi_pci_root *root = acpi_driver_data(device);
  509. pci_bus_add_devices(root->bus);
  510. return 0;
  511. }
  512. static int acpi_pci_root_remove(struct acpi_device *device, int type)
  513. {
  514. struct acpi_pci_root *root = acpi_driver_data(device);
  515. kfree(root);
  516. return 0;
  517. }
  518. static int __init acpi_pci_root_init(void)
  519. {
  520. if (acpi_pci_disabled)
  521. return 0;
  522. if (acpi_bus_register_driver(&acpi_pci_root_driver) < 0)
  523. return -ENODEV;
  524. return 0;
  525. }
  526. subsys_initcall(acpi_pci_root_init);