pci_bind.c 10 KB

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