pci_bind.c 9.4 KB

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