pci_bind.c 10 KB

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