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