pci_root.c 15 KB

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