pci_root.c 9.4 KB

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