pci_root.c 16 KB

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