pci_root.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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/pci.h>
  33. #include <linux/pci-acpi.h>
  34. #include <linux/acpi.h>
  35. #include <acpi/acpi_bus.h>
  36. #include <acpi/acpi_drivers.h>
  37. #define _COMPONENT ACPI_PCI_COMPONENT
  38. ACPI_MODULE_NAME("pci_root");
  39. #define ACPI_PCI_ROOT_CLASS "pci_bridge"
  40. #define ACPI_PCI_ROOT_DEVICE_NAME "PCI Root Bridge"
  41. static int acpi_pci_root_add(struct acpi_device *device);
  42. static int acpi_pci_root_remove(struct acpi_device *device, int type);
  43. static int acpi_pci_root_start(struct acpi_device *device);
  44. static struct acpi_device_id root_device_ids[] = {
  45. {"PNP0A03", 0},
  46. {"", 0},
  47. };
  48. MODULE_DEVICE_TABLE(acpi, root_device_ids);
  49. static struct acpi_driver acpi_pci_root_driver = {
  50. .name = "pci_root",
  51. .class = ACPI_PCI_ROOT_CLASS,
  52. .ids = root_device_ids,
  53. .ops = {
  54. .add = acpi_pci_root_add,
  55. .remove = acpi_pci_root_remove,
  56. .start = acpi_pci_root_start,
  57. },
  58. };
  59. struct acpi_pci_root {
  60. struct list_head node;
  61. struct acpi_device * device;
  62. struct acpi_pci_id id;
  63. struct pci_bus *bus;
  64. };
  65. static LIST_HEAD(acpi_pci_roots);
  66. static struct acpi_pci_driver *sub_driver;
  67. int acpi_pci_register_driver(struct acpi_pci_driver *driver)
  68. {
  69. int n = 0;
  70. struct list_head *entry;
  71. struct acpi_pci_driver **pptr = &sub_driver;
  72. while (*pptr)
  73. pptr = &(*pptr)->next;
  74. *pptr = driver;
  75. if (!driver->add)
  76. return 0;
  77. list_for_each(entry, &acpi_pci_roots) {
  78. struct acpi_pci_root *root;
  79. root = list_entry(entry, struct acpi_pci_root, node);
  80. driver->add(root->device->handle);
  81. n++;
  82. }
  83. return n;
  84. }
  85. EXPORT_SYMBOL(acpi_pci_register_driver);
  86. void acpi_pci_unregister_driver(struct acpi_pci_driver *driver)
  87. {
  88. struct list_head *entry;
  89. struct acpi_pci_driver **pptr = &sub_driver;
  90. while (*pptr) {
  91. if (*pptr == driver)
  92. break;
  93. pptr = &(*pptr)->next;
  94. }
  95. BUG_ON(!*pptr);
  96. *pptr = (*pptr)->next;
  97. if (!driver->remove)
  98. return;
  99. list_for_each(entry, &acpi_pci_roots) {
  100. struct acpi_pci_root *root;
  101. root = list_entry(entry, struct acpi_pci_root, node);
  102. driver->remove(root->device->handle);
  103. }
  104. }
  105. EXPORT_SYMBOL(acpi_pci_unregister_driver);
  106. acpi_handle acpi_get_pci_rootbridge_handle(unsigned int seg, unsigned int bus)
  107. {
  108. struct acpi_pci_root *tmp;
  109. list_for_each_entry(tmp, &acpi_pci_roots, node) {
  110. if ((tmp->id.segment == (u16) seg) && (tmp->id.bus == (u16) bus))
  111. return tmp->device->handle;
  112. }
  113. return NULL;
  114. }
  115. EXPORT_SYMBOL_GPL(acpi_get_pci_rootbridge_handle);
  116. static acpi_status
  117. get_root_bridge_busnr_callback(struct acpi_resource *resource, void *data)
  118. {
  119. int *busnr = data;
  120. struct acpi_resource_address64 address;
  121. if (resource->type != ACPI_RESOURCE_TYPE_ADDRESS16 &&
  122. resource->type != ACPI_RESOURCE_TYPE_ADDRESS32 &&
  123. resource->type != ACPI_RESOURCE_TYPE_ADDRESS64)
  124. return AE_OK;
  125. acpi_resource_to_address64(resource, &address);
  126. if ((address.address_length > 0) &&
  127. (address.resource_type == ACPI_BUS_NUMBER_RANGE))
  128. *busnr = address.minimum;
  129. return AE_OK;
  130. }
  131. static acpi_status try_get_root_bridge_busnr(acpi_handle handle, int *busnum)
  132. {
  133. acpi_status status;
  134. *busnum = -1;
  135. status =
  136. acpi_walk_resources(handle, METHOD_NAME__CRS,
  137. get_root_bridge_busnr_callback, busnum);
  138. if (ACPI_FAILURE(status))
  139. return status;
  140. /* Check if we really get a bus number from _CRS */
  141. if (*busnum == -1)
  142. return AE_ERROR;
  143. return AE_OK;
  144. }
  145. static void acpi_pci_bridge_scan(struct acpi_device *device)
  146. {
  147. int status;
  148. struct acpi_device *child = NULL;
  149. if (device->flags.bus_address)
  150. if (device->parent && device->parent->ops.bind) {
  151. status = device->parent->ops.bind(device);
  152. if (!status) {
  153. list_for_each_entry(child, &device->children, node)
  154. acpi_pci_bridge_scan(child);
  155. }
  156. }
  157. }
  158. static int __devinit acpi_pci_root_add(struct acpi_device *device)
  159. {
  160. int result = 0;
  161. struct acpi_pci_root *root = NULL;
  162. struct acpi_pci_root *tmp;
  163. acpi_status status = AE_OK;
  164. unsigned long long value = 0;
  165. acpi_handle handle = NULL;
  166. struct acpi_device *child;
  167. u32 flags, base_flags;
  168. if (!device)
  169. return -EINVAL;
  170. root = kzalloc(sizeof(struct acpi_pci_root), GFP_KERNEL);
  171. if (!root)
  172. return -ENOMEM;
  173. INIT_LIST_HEAD(&root->node);
  174. root->device = device;
  175. strcpy(acpi_device_name(device), ACPI_PCI_ROOT_DEVICE_NAME);
  176. strcpy(acpi_device_class(device), ACPI_PCI_ROOT_CLASS);
  177. device->driver_data = root;
  178. device->ops.bind = acpi_pci_bind;
  179. /*
  180. * All supported architectures that use ACPI have support for
  181. * PCI domains, so we indicate this in _OSC support capabilities.
  182. */
  183. flags = base_flags = OSC_PCI_SEGMENT_GROUPS_SUPPORT;
  184. pci_acpi_osc_support(device->handle, flags);
  185. /*
  186. * Segment
  187. * -------
  188. * Obtained via _SEG, if exists, otherwise assumed to be zero (0).
  189. */
  190. status = acpi_evaluate_integer(device->handle, METHOD_NAME__SEG, NULL,
  191. &value);
  192. switch (status) {
  193. case AE_OK:
  194. root->id.segment = (u16) value;
  195. break;
  196. case AE_NOT_FOUND:
  197. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  198. "Assuming segment 0 (no _SEG)\n"));
  199. root->id.segment = 0;
  200. break;
  201. default:
  202. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _SEG"));
  203. result = -ENODEV;
  204. goto end;
  205. }
  206. /*
  207. * Bus
  208. * ---
  209. * Obtained via _BBN, if exists, otherwise assumed to be zero (0).
  210. */
  211. status = acpi_evaluate_integer(device->handle, METHOD_NAME__BBN, NULL,
  212. &value);
  213. switch (status) {
  214. case AE_OK:
  215. root->id.bus = (u16) value;
  216. break;
  217. case AE_NOT_FOUND:
  218. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Assuming bus 0 (no _BBN)\n"));
  219. root->id.bus = 0;
  220. break;
  221. default:
  222. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BBN"));
  223. result = -ENODEV;
  224. goto end;
  225. }
  226. /* Some systems have wrong _BBN */
  227. list_for_each_entry(tmp, &acpi_pci_roots, node) {
  228. if ((tmp->id.segment == root->id.segment)
  229. && (tmp->id.bus == root->id.bus)) {
  230. int bus = 0;
  231. acpi_status status;
  232. printk(KERN_ERR PREFIX
  233. "Wrong _BBN value, reboot"
  234. " and use option 'pci=noacpi'\n");
  235. status = try_get_root_bridge_busnr(device->handle, &bus);
  236. if (ACPI_FAILURE(status))
  237. break;
  238. if (bus != root->id.bus) {
  239. printk(KERN_INFO PREFIX
  240. "PCI _CRS %d overrides _BBN 0\n", bus);
  241. root->id.bus = bus;
  242. }
  243. break;
  244. }
  245. }
  246. /*
  247. * Device & Function
  248. * -----------------
  249. * Obtained from _ADR (which has already been evaluated for us).
  250. */
  251. root->id.device = device->pnp.bus_address >> 16;
  252. root->id.function = device->pnp.bus_address & 0xFFFF;
  253. /*
  254. * TBD: Need PCI interface for enumeration/configuration of roots.
  255. */
  256. /* TBD: Locking */
  257. list_add_tail(&root->node, &acpi_pci_roots);
  258. printk(KERN_INFO PREFIX "%s [%s] (%04x:%02x)\n",
  259. acpi_device_name(device), acpi_device_bid(device),
  260. root->id.segment, root->id.bus);
  261. /*
  262. * Scan the Root Bridge
  263. * --------------------
  264. * Must do this prior to any attempt to bind the root device, as the
  265. * PCI namespace does not get created until this call is made (and
  266. * thus the root bridge's pci_dev does not exist).
  267. */
  268. root->bus = pci_acpi_scan_root(device, root->id.segment, root->id.bus);
  269. if (!root->bus) {
  270. printk(KERN_ERR PREFIX
  271. "Bus %04x:%02x not present in PCI namespace\n",
  272. root->id.segment, root->id.bus);
  273. result = -ENODEV;
  274. goto end;
  275. }
  276. /*
  277. * Attach ACPI-PCI Context
  278. * -----------------------
  279. * Thus binding the ACPI and PCI devices.
  280. */
  281. result = acpi_pci_bind_root(device, &root->id, root->bus);
  282. if (result)
  283. goto end;
  284. /*
  285. * PCI Routing Table
  286. * -----------------
  287. * Evaluate and parse _PRT, if exists.
  288. */
  289. status = acpi_get_handle(device->handle, METHOD_NAME__PRT, &handle);
  290. if (ACPI_SUCCESS(status))
  291. result = acpi_pci_irq_add_prt(device->handle, root->id.segment,
  292. root->id.bus);
  293. /*
  294. * Scan and bind all _ADR-Based Devices
  295. */
  296. list_for_each_entry(child, &device->children, node)
  297. acpi_pci_bridge_scan(child);
  298. /* Indicate support for various _OSC capabilities. */
  299. if (pci_ext_cfg_avail(root->bus->self))
  300. flags |= OSC_EXT_PCI_CONFIG_SUPPORT;
  301. if (flags != base_flags)
  302. pci_acpi_osc_support(device->handle, flags);
  303. end:
  304. if (result) {
  305. if (!list_empty(&root->node))
  306. list_del(&root->node);
  307. kfree(root);
  308. }
  309. return result;
  310. }
  311. static int acpi_pci_root_start(struct acpi_device *device)
  312. {
  313. struct acpi_pci_root *root;
  314. list_for_each_entry(root, &acpi_pci_roots, node) {
  315. if (root->device == device) {
  316. pci_bus_add_devices(root->bus);
  317. return 0;
  318. }
  319. }
  320. return -ENODEV;
  321. }
  322. static int acpi_pci_root_remove(struct acpi_device *device, int type)
  323. {
  324. struct acpi_pci_root *root = NULL;
  325. if (!device || !acpi_driver_data(device))
  326. return -EINVAL;
  327. root = acpi_driver_data(device);
  328. kfree(root);
  329. return 0;
  330. }
  331. static int __init acpi_pci_root_init(void)
  332. {
  333. if (acpi_pci_disabled)
  334. return 0;
  335. if (acpi_bus_register_driver(&acpi_pci_root_driver) < 0)
  336. return -ENODEV;
  337. return 0;
  338. }
  339. subsys_initcall(acpi_pci_root_init);