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