pci_root.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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. struct acpi_pci_root {
  61. struct list_head node;
  62. struct acpi_device *device;
  63. struct pci_bus *bus;
  64. u16 segment;
  65. u8 bus_nr;
  66. u32 osc_support_set; /* _OSC state of support bits */
  67. u32 osc_control_set; /* _OSC state of control bits */
  68. u32 osc_control_qry; /* the latest _OSC query result */
  69. u32 osc_queried:1; /* has _OSC control been queried? */
  70. };
  71. static LIST_HEAD(acpi_pci_roots);
  72. static struct acpi_pci_driver *sub_driver;
  73. static DEFINE_MUTEX(osc_lock);
  74. int acpi_pci_register_driver(struct acpi_pci_driver *driver)
  75. {
  76. int n = 0;
  77. struct acpi_pci_root *root;
  78. struct acpi_pci_driver **pptr = &sub_driver;
  79. while (*pptr)
  80. pptr = &(*pptr)->next;
  81. *pptr = driver;
  82. if (!driver->add)
  83. return 0;
  84. list_for_each_entry(root, &acpi_pci_roots, node) {
  85. driver->add(root->device->handle);
  86. n++;
  87. }
  88. return n;
  89. }
  90. EXPORT_SYMBOL(acpi_pci_register_driver);
  91. void acpi_pci_unregister_driver(struct acpi_pci_driver *driver)
  92. {
  93. struct acpi_pci_root *root;
  94. struct acpi_pci_driver **pptr = &sub_driver;
  95. while (*pptr) {
  96. if (*pptr == driver)
  97. break;
  98. pptr = &(*pptr)->next;
  99. }
  100. BUG_ON(!*pptr);
  101. *pptr = (*pptr)->next;
  102. if (!driver->remove)
  103. return;
  104. list_for_each_entry(root, &acpi_pci_roots, node)
  105. driver->remove(root->device->handle);
  106. }
  107. EXPORT_SYMBOL(acpi_pci_unregister_driver);
  108. acpi_handle acpi_get_pci_rootbridge_handle(unsigned int seg, unsigned int bus)
  109. {
  110. struct acpi_pci_root *root;
  111. list_for_each_entry(root, &acpi_pci_roots, node)
  112. if ((root->segment == (u16) seg) && (root->bus_nr == (u16) bus))
  113. return root->device->handle;
  114. return NULL;
  115. }
  116. EXPORT_SYMBOL_GPL(acpi_get_pci_rootbridge_handle);
  117. /**
  118. * acpi_is_root_bridge - determine whether an ACPI CA node is a PCI root bridge
  119. * @handle - the ACPI CA node in question.
  120. *
  121. * Note: we could make this API take a struct acpi_device * instead, but
  122. * for now, it's more convenient to operate on an acpi_handle.
  123. */
  124. int acpi_is_root_bridge(acpi_handle handle)
  125. {
  126. int ret;
  127. struct acpi_device *device;
  128. ret = acpi_bus_get_device(handle, &device);
  129. if (ret)
  130. return 0;
  131. ret = acpi_match_device_ids(device, root_device_ids);
  132. if (ret)
  133. return 0;
  134. else
  135. return 1;
  136. }
  137. EXPORT_SYMBOL_GPL(acpi_is_root_bridge);
  138. static acpi_status
  139. get_root_bridge_busnr_callback(struct acpi_resource *resource, void *data)
  140. {
  141. int *busnr = data;
  142. struct acpi_resource_address64 address;
  143. if (resource->type != ACPI_RESOURCE_TYPE_ADDRESS16 &&
  144. resource->type != ACPI_RESOURCE_TYPE_ADDRESS32 &&
  145. resource->type != ACPI_RESOURCE_TYPE_ADDRESS64)
  146. return AE_OK;
  147. acpi_resource_to_address64(resource, &address);
  148. if ((address.address_length > 0) &&
  149. (address.resource_type == ACPI_BUS_NUMBER_RANGE))
  150. *busnr = address.minimum;
  151. return AE_OK;
  152. }
  153. static acpi_status try_get_root_bridge_busnr(acpi_handle handle,
  154. unsigned long long *bus)
  155. {
  156. acpi_status status;
  157. int busnum;
  158. busnum = -1;
  159. status =
  160. acpi_walk_resources(handle, METHOD_NAME__CRS,
  161. get_root_bridge_busnr_callback, &busnum);
  162. if (ACPI_FAILURE(status))
  163. return status;
  164. /* Check if we really get a bus number from _CRS */
  165. if (busnum == -1)
  166. return AE_ERROR;
  167. *bus = busnum;
  168. return AE_OK;
  169. }
  170. static void acpi_pci_bridge_scan(struct acpi_device *device)
  171. {
  172. int status;
  173. struct acpi_device *child = NULL;
  174. if (device->flags.bus_address)
  175. if (device->parent && device->parent->ops.bind) {
  176. status = device->parent->ops.bind(device);
  177. if (!status) {
  178. list_for_each_entry(child, &device->children, node)
  179. acpi_pci_bridge_scan(child);
  180. }
  181. }
  182. }
  183. static u8 OSC_UUID[16] = {0x5B, 0x4D, 0xDB, 0x33, 0xF7, 0x1F, 0x1C, 0x40,
  184. 0x96, 0x57, 0x74, 0x41, 0xC0, 0x3D, 0xD7, 0x66};
  185. static acpi_status acpi_pci_run_osc(acpi_handle handle,
  186. const u32 *capbuf, u32 *retval)
  187. {
  188. acpi_status status;
  189. struct acpi_object_list input;
  190. union acpi_object in_params[4];
  191. struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
  192. union acpi_object *out_obj;
  193. u32 errors;
  194. /* Setting up input parameters */
  195. input.count = 4;
  196. input.pointer = in_params;
  197. in_params[0].type = ACPI_TYPE_BUFFER;
  198. in_params[0].buffer.length = 16;
  199. in_params[0].buffer.pointer = OSC_UUID;
  200. in_params[1].type = ACPI_TYPE_INTEGER;
  201. in_params[1].integer.value = 1;
  202. in_params[2].type = ACPI_TYPE_INTEGER;
  203. in_params[2].integer.value = 3;
  204. in_params[3].type = ACPI_TYPE_BUFFER;
  205. in_params[3].buffer.length = 12;
  206. in_params[3].buffer.pointer = (u8 *)capbuf;
  207. status = acpi_evaluate_object(handle, "_OSC", &input, &output);
  208. if (ACPI_FAILURE(status))
  209. return status;
  210. if (!output.length)
  211. return AE_NULL_OBJECT;
  212. out_obj = output.pointer;
  213. if (out_obj->type != ACPI_TYPE_BUFFER) {
  214. printk(KERN_DEBUG "_OSC evaluation returned wrong type\n");
  215. status = AE_TYPE;
  216. goto out_kfree;
  217. }
  218. /* Need to ignore the bit0 in result code */
  219. errors = *((u32 *)out_obj->buffer.pointer) & ~(1 << 0);
  220. if (errors) {
  221. if (errors & OSC_REQUEST_ERROR)
  222. printk(KERN_DEBUG "_OSC request failed\n");
  223. if (errors & OSC_INVALID_UUID_ERROR)
  224. printk(KERN_DEBUG "_OSC invalid UUID\n");
  225. if (errors & OSC_INVALID_REVISION_ERROR)
  226. printk(KERN_DEBUG "_OSC invalid revision\n");
  227. if (errors & OSC_CAPABILITIES_MASK_ERROR) {
  228. if (capbuf[OSC_QUERY_TYPE] & OSC_QUERY_ENABLE)
  229. goto out_success;
  230. printk(KERN_DEBUG
  231. "Firmware did not grant requested _OSC control\n");
  232. status = AE_SUPPORT;
  233. goto out_kfree;
  234. }
  235. status = AE_ERROR;
  236. goto out_kfree;
  237. }
  238. out_success:
  239. *retval = *((u32 *)(out_obj->buffer.pointer + 8));
  240. status = AE_OK;
  241. out_kfree:
  242. kfree(output.pointer);
  243. return status;
  244. }
  245. static acpi_status acpi_pci_query_osc(struct acpi_pci_root *root, u32 flags)
  246. {
  247. acpi_status status;
  248. u32 support_set, result, capbuf[3];
  249. /* do _OSC query for all possible controls */
  250. support_set = root->osc_support_set | (flags & OSC_SUPPORT_MASKS);
  251. capbuf[OSC_QUERY_TYPE] = OSC_QUERY_ENABLE;
  252. capbuf[OSC_SUPPORT_TYPE] = support_set;
  253. capbuf[OSC_CONTROL_TYPE] = OSC_CONTROL_MASKS;
  254. status = acpi_pci_run_osc(root->device->handle, capbuf, &result);
  255. if (ACPI_SUCCESS(status)) {
  256. root->osc_support_set = support_set;
  257. root->osc_control_qry = result;
  258. root->osc_queried = 1;
  259. }
  260. return status;
  261. }
  262. static acpi_status acpi_pci_osc_support(struct acpi_pci_root *root, u32 flags)
  263. {
  264. acpi_status status;
  265. acpi_handle tmp;
  266. status = acpi_get_handle(root->device->handle, "_OSC", &tmp);
  267. if (ACPI_FAILURE(status))
  268. return status;
  269. mutex_lock(&osc_lock);
  270. status = acpi_pci_query_osc(root, flags);
  271. mutex_unlock(&osc_lock);
  272. return status;
  273. }
  274. static struct acpi_pci_root *acpi_pci_find_root(acpi_handle handle)
  275. {
  276. struct acpi_pci_root *root;
  277. list_for_each_entry(root, &acpi_pci_roots, node) {
  278. if (root->device->handle == handle)
  279. return root;
  280. }
  281. return NULL;
  282. }
  283. struct acpi_handle_node {
  284. struct list_head node;
  285. acpi_handle handle;
  286. };
  287. /**
  288. * acpi_get_pci_dev - convert ACPI CA handle to struct pci_dev
  289. * @handle: the handle in question
  290. *
  291. * Given an ACPI CA handle, the desired PCI device is located in the
  292. * list of PCI devices.
  293. *
  294. * If the device is found, its reference count is increased and this
  295. * function returns a pointer to its data structure. The caller must
  296. * decrement the reference count by calling pci_dev_put().
  297. * If no device is found, %NULL is returned.
  298. */
  299. struct pci_dev *acpi_get_pci_dev(acpi_handle handle)
  300. {
  301. int dev, fn;
  302. unsigned long long adr;
  303. acpi_status status;
  304. acpi_handle phandle;
  305. struct pci_bus *pbus;
  306. struct pci_dev *pdev = NULL;
  307. struct acpi_handle_node *node, *tmp;
  308. struct acpi_pci_root *root;
  309. LIST_HEAD(device_list);
  310. /*
  311. * Walk up the ACPI CA namespace until we reach a PCI root bridge.
  312. */
  313. phandle = handle;
  314. while (!acpi_is_root_bridge(phandle)) {
  315. node = kzalloc(sizeof(struct acpi_handle_node), GFP_KERNEL);
  316. if (!node)
  317. goto out;
  318. INIT_LIST_HEAD(&node->node);
  319. node->handle = phandle;
  320. list_add(&node->node, &device_list);
  321. status = acpi_get_parent(phandle, &phandle);
  322. if (ACPI_FAILURE(status))
  323. goto out;
  324. }
  325. root = acpi_pci_find_root(phandle);
  326. if (!root)
  327. goto out;
  328. pbus = root->bus;
  329. /*
  330. * Now, walk back down the PCI device tree until we return to our
  331. * original handle. Assumes that everything between the PCI root
  332. * bridge and the device we're looking for must be a P2P bridge.
  333. */
  334. list_for_each_entry(node, &device_list, node) {
  335. acpi_handle hnd = node->handle;
  336. status = acpi_evaluate_integer(hnd, "_ADR", NULL, &adr);
  337. if (ACPI_FAILURE(status))
  338. goto out;
  339. dev = (adr >> 16) & 0xffff;
  340. fn = adr & 0xffff;
  341. pdev = pci_get_slot(pbus, PCI_DEVFN(dev, fn));
  342. if (!pdev || hnd == handle)
  343. break;
  344. pbus = pdev->subordinate;
  345. pci_dev_put(pdev);
  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);