pci_root.c 16 KB

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