pci_root.c 16 KB

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