pci_root.c 8.9 KB

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