pci_root.c 15 KB

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