pci_root.c 16 KB

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