pci_root.c 15 KB

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