pci_bind.c 10 KB

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