pci_root.c 8.4 KB

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