pci_bind.c 10 KB

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