pci_bind.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * pci_bind.c - ACPI PCI Device Binding ($Revision: 2 $)
  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_bind")
  38. struct acpi_pci_data {
  39. struct acpi_pci_id id;
  40. struct pci_bus *bus;
  41. struct pci_dev *dev;
  42. };
  43. static void acpi_pci_data_handler(acpi_handle handle, u32 function,
  44. void *context)
  45. {
  46. /* TBD: Anything we need to do here? */
  47. return;
  48. }
  49. /**
  50. * acpi_get_pci_id
  51. * ------------------
  52. * This function is used by the ACPI Interpreter (a.k.a. Core Subsystem)
  53. * to resolve PCI information for ACPI-PCI devices defined in the namespace.
  54. * This typically occurs when resolving PCI operation region information.
  55. */
  56. acpi_status acpi_get_pci_id(acpi_handle handle, struct acpi_pci_id *id)
  57. {
  58. int result = 0;
  59. acpi_status status = AE_OK;
  60. struct acpi_device *device = NULL;
  61. struct acpi_pci_data *data = NULL;
  62. if (!id)
  63. return AE_BAD_PARAMETER;
  64. result = acpi_bus_get_device(handle, &device);
  65. if (result) {
  66. printk(KERN_ERR PREFIX
  67. "Invalid ACPI Bus context for device %s\n",
  68. acpi_device_bid(device));
  69. return AE_NOT_EXIST;
  70. }
  71. status = acpi_get_data(handle, acpi_pci_data_handler, (void **)&data);
  72. if (ACPI_FAILURE(status) || !data) {
  73. ACPI_EXCEPTION((AE_INFO, status,
  74. "Invalid ACPI-PCI context for device %s",
  75. acpi_device_bid(device)));
  76. return status;
  77. }
  78. *id = data->id;
  79. /*
  80. id->segment = data->id.segment;
  81. id->bus = data->id.bus;
  82. id->device = data->id.device;
  83. id->function = data->id.function;
  84. */
  85. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  86. "Device %s has PCI address %02x:%02x:%02x.%02x\n",
  87. acpi_device_bid(device), id->segment, id->bus,
  88. id->device, id->function));
  89. return AE_OK;
  90. }
  91. EXPORT_SYMBOL(acpi_get_pci_id);
  92. int acpi_pci_bind(struct acpi_device *device)
  93. {
  94. int result = 0;
  95. acpi_status status = AE_OK;
  96. struct acpi_pci_data *data = NULL;
  97. struct acpi_pci_data *pdata = NULL;
  98. char *pathname = NULL;
  99. struct acpi_buffer buffer = { 0, NULL };
  100. acpi_handle handle = NULL;
  101. struct pci_dev *dev;
  102. struct pci_bus *bus;
  103. if (!device || !device->parent)
  104. return -EINVAL;
  105. pathname = kmalloc(ACPI_PATHNAME_MAX, GFP_KERNEL);
  106. if (!pathname)
  107. return -ENOMEM;
  108. memset(pathname, 0, ACPI_PATHNAME_MAX);
  109. buffer.length = ACPI_PATHNAME_MAX;
  110. buffer.pointer = pathname;
  111. data = kmalloc(sizeof(struct acpi_pci_data), GFP_KERNEL);
  112. if (!data) {
  113. kfree(pathname);
  114. return -ENOMEM;
  115. }
  116. memset(data, 0, sizeof(struct acpi_pci_data));
  117. acpi_get_name(device->handle, ACPI_FULL_PATHNAME, &buffer);
  118. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Binding PCI device [%s]...\n",
  119. pathname));
  120. /*
  121. * Segment & Bus
  122. * -------------
  123. * These are obtained via the parent device's ACPI-PCI context.
  124. */
  125. status = acpi_get_data(device->parent->handle, acpi_pci_data_handler,
  126. (void **)&pdata);
  127. if (ACPI_FAILURE(status) || !pdata || !pdata->bus) {
  128. ACPI_EXCEPTION((AE_INFO, status,
  129. "Invalid ACPI-PCI context for parent device %s",
  130. acpi_device_bid(device->parent)));
  131. result = -ENODEV;
  132. goto end;
  133. }
  134. data->id.segment = pdata->id.segment;
  135. data->id.bus = pdata->bus->number;
  136. /*
  137. * Device & Function
  138. * -----------------
  139. * These are simply obtained from the device's _ADR method. Note
  140. * that a value of zero is valid.
  141. */
  142. data->id.device = device->pnp.bus_address >> 16;
  143. data->id.function = device->pnp.bus_address & 0xFFFF;
  144. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "...to %02x:%02x:%02x.%02x\n",
  145. data->id.segment, data->id.bus, data->id.device,
  146. data->id.function));
  147. /*
  148. * TBD: Support slot devices (e.g. function=0xFFFF).
  149. */
  150. /*
  151. * Locate PCI Device
  152. * -----------------
  153. * Locate matching device in PCI namespace. If it doesn't exist
  154. * this typically means that the device isn't currently inserted
  155. * (e.g. docking station, port replicator, etc.).
  156. * We cannot simply search the global pci device list, since
  157. * PCI devices are added to the global pci list when the root
  158. * bridge start ops are run, which may not have happened yet.
  159. */
  160. bus = pci_find_bus(data->id.segment, data->id.bus);
  161. if (bus) {
  162. list_for_each_entry(dev, &bus->devices, bus_list) {
  163. if (dev->devfn == PCI_DEVFN(data->id.device,
  164. data->id.function)) {
  165. data->dev = dev;
  166. break;
  167. }
  168. }
  169. }
  170. if (!data->dev) {
  171. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  172. "Device %02x:%02x:%02x.%02x not present in PCI namespace\n",
  173. data->id.segment, data->id.bus,
  174. data->id.device, data->id.function));
  175. result = -ENODEV;
  176. goto end;
  177. }
  178. if (!data->dev->bus) {
  179. printk(KERN_ERR PREFIX
  180. "Device %02x:%02x:%02x.%02x has invalid 'bus' field\n",
  181. data->id.segment, data->id.bus,
  182. data->id.device, data->id.function);
  183. result = -ENODEV;
  184. goto end;
  185. }
  186. /*
  187. * PCI Bridge?
  188. * -----------
  189. * If so, set the 'bus' field and install the 'bind' function to
  190. * facilitate callbacks for all of its children.
  191. */
  192. if (data->dev->subordinate) {
  193. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  194. "Device %02x:%02x:%02x.%02x is a PCI bridge\n",
  195. data->id.segment, data->id.bus,
  196. data->id.device, data->id.function));
  197. data->bus = data->dev->subordinate;
  198. }
  199. /*
  200. * Attach ACPI-PCI Context
  201. * -----------------------
  202. * Thus binding the ACPI and PCI devices.
  203. */
  204. status = acpi_attach_data(device->handle, acpi_pci_data_handler, data);
  205. if (ACPI_FAILURE(status)) {
  206. ACPI_EXCEPTION((AE_INFO, status,
  207. "Unable to attach ACPI-PCI context to device %s",
  208. acpi_device_bid(device)));
  209. result = -ENODEV;
  210. goto end;
  211. }
  212. /*
  213. * PCI Routing Table
  214. * -----------------
  215. * Evaluate and parse _PRT, if exists. This code is independent of
  216. * PCI bridges (above) to allow parsing of _PRT objects within the
  217. * scope of non-bridge devices. Note that _PRTs within the scope of
  218. * a PCI bridge assume the bridge's subordinate bus number.
  219. *
  220. * TBD: Can _PRTs exist within the scope of non-bridge PCI devices?
  221. */
  222. status = acpi_get_handle(device->handle, METHOD_NAME__PRT, &handle);
  223. if (ACPI_SUCCESS(status)) {
  224. if (data->bus) /* PCI-PCI bridge */
  225. acpi_pci_irq_add_prt(device->handle, data->id.segment,
  226. data->bus->number);
  227. else /* non-bridge PCI device */
  228. acpi_pci_irq_add_prt(device->handle, data->id.segment,
  229. data->id.bus);
  230. }
  231. end:
  232. kfree(pathname);
  233. if (result)
  234. kfree(data);
  235. return result;
  236. }
  237. int acpi_pci_unbind(struct acpi_device *device)
  238. {
  239. int result = 0;
  240. acpi_status status = AE_OK;
  241. struct acpi_pci_data *data = NULL;
  242. char *pathname = NULL;
  243. struct acpi_buffer buffer = { 0, NULL };
  244. if (!device || !device->parent)
  245. return -EINVAL;
  246. pathname = (char *)kmalloc(ACPI_PATHNAME_MAX, GFP_KERNEL);
  247. if (!pathname)
  248. return -ENOMEM;
  249. memset(pathname, 0, ACPI_PATHNAME_MAX);
  250. buffer.length = ACPI_PATHNAME_MAX;
  251. buffer.pointer = pathname;
  252. acpi_get_name(device->handle, ACPI_FULL_PATHNAME, &buffer);
  253. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Unbinding PCI device [%s]...\n",
  254. pathname));
  255. kfree(pathname);
  256. status =
  257. acpi_get_data(device->handle, acpi_pci_data_handler,
  258. (void **)&data);
  259. if (ACPI_FAILURE(status)) {
  260. ACPI_EXCEPTION((AE_INFO, status,
  261. "Unable to get data from device %s",
  262. acpi_device_bid(device)));
  263. result = -ENODEV;
  264. goto end;
  265. }
  266. status = acpi_detach_data(device->handle, acpi_pci_data_handler);
  267. if (ACPI_FAILURE(status)) {
  268. ACPI_EXCEPTION((AE_INFO, status,
  269. "Unable to detach data from device %s",
  270. acpi_device_bid(device)));
  271. result = -ENODEV;
  272. goto end;
  273. }
  274. if (data->dev->subordinate) {
  275. acpi_pci_irq_del_prt(data->id.segment, data->bus->number);
  276. }
  277. kfree(data);
  278. end:
  279. return result;
  280. }
  281. int
  282. acpi_pci_bind_root(struct acpi_device *device,
  283. struct acpi_pci_id *id, struct pci_bus *bus)
  284. {
  285. int result = 0;
  286. acpi_status status = AE_OK;
  287. struct acpi_pci_data *data = NULL;
  288. char *pathname = NULL;
  289. struct acpi_buffer buffer = { 0, NULL };
  290. pathname = (char *)kmalloc(ACPI_PATHNAME_MAX, GFP_KERNEL);
  291. if (!pathname)
  292. return -ENOMEM;
  293. memset(pathname, 0, ACPI_PATHNAME_MAX);
  294. buffer.length = ACPI_PATHNAME_MAX;
  295. buffer.pointer = pathname;
  296. if (!device || !id || !bus) {
  297. kfree(pathname);
  298. return -EINVAL;
  299. }
  300. data = kmalloc(sizeof(struct acpi_pci_data), GFP_KERNEL);
  301. if (!data) {
  302. kfree(pathname);
  303. return -ENOMEM;
  304. }
  305. memset(data, 0, sizeof(struct acpi_pci_data));
  306. data->id = *id;
  307. data->bus = bus;
  308. acpi_get_name(device->handle, ACPI_FULL_PATHNAME, &buffer);
  309. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Binding PCI root bridge [%s] to "
  310. "%02x:%02x\n", pathname, id->segment, id->bus));
  311. status = acpi_attach_data(device->handle, acpi_pci_data_handler, data);
  312. if (ACPI_FAILURE(status)) {
  313. ACPI_EXCEPTION((AE_INFO, status,
  314. "Unable to attach ACPI-PCI context to device %s",
  315. pathname));
  316. result = -ENODEV;
  317. goto end;
  318. }
  319. end:
  320. kfree(pathname);
  321. if (result != 0)
  322. kfree(data);
  323. return result;
  324. }
  325. #define ACPI_PCI_BRIDGE_DRIVER_NAME "ACPI PCI Bridge Driver"
  326. static int acpi_pci_bridge_add(struct acpi_device *device);
  327. static int acpi_pci_bridge_remove(struct acpi_device *device, int type);
  328. static struct acpi_driver acpi_pci_bridge_driver = {
  329. .name = ACPI_PCI_BRIDGE_DRIVER_NAME,
  330. .ids = ACPI_PCI_BRIDGE_HID,
  331. .ops = {
  332. .add = acpi_pci_bridge_add,
  333. .remove = acpi_pci_bridge_remove,
  334. },
  335. };
  336. static int acpi_pci_bridge_add(struct acpi_device *device)
  337. {
  338. return acpi_pci_bind(device);
  339. }
  340. static int acpi_pci_bridge_remove(struct acpi_device *device, int type)
  341. {
  342. return acpi_pci_unbind(device);
  343. }
  344. static int __init acpi_pci_bridge_init(void)
  345. {
  346. if (acpi_pci_disabled)
  347. return 0;
  348. if (acpi_bus_register_driver(&acpi_pci_bridge_driver) < 0)
  349. return -ENODEV;
  350. return 0;
  351. }
  352. /* Should be called after ACPI pci root driver */
  353. subsys_initcall(acpi_pci_bridge_init);