glue.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * Link physical devices with ACPI devices support
  3. *
  4. * Copyright (c) 2005 David Shaohua Li <shaohua.li@intel.com>
  5. * Copyright (c) 2005 Intel Corp.
  6. *
  7. * This file is released under the GPLv2.
  8. */
  9. #include <linux/init.h>
  10. #include <linux/list.h>
  11. #include <linux/device.h>
  12. #include <linux/rwsem.h>
  13. #include <linux/acpi.h>
  14. #define ACPI_GLUE_DEBUG 0
  15. #if ACPI_GLUE_DEBUG
  16. #define DBG(x...) printk(PREFIX x)
  17. #else
  18. #define DBG(x...)
  19. #endif
  20. static LIST_HEAD(bus_type_list);
  21. static DECLARE_RWSEM(bus_type_sem);
  22. int register_acpi_bus_type(struct acpi_bus_type *type)
  23. {
  24. if (acpi_disabled)
  25. return -ENODEV;
  26. if (type && type->bus && type->find_device) {
  27. down_write(&bus_type_sem);
  28. list_add_tail(&type->list, &bus_type_list);
  29. up_write(&bus_type_sem);
  30. printk(KERN_INFO PREFIX "bus type %s registered\n",
  31. type->bus->name);
  32. return 0;
  33. }
  34. return -ENODEV;
  35. }
  36. EXPORT_SYMBOL(register_acpi_bus_type);
  37. int unregister_acpi_bus_type(struct acpi_bus_type *type)
  38. {
  39. if (acpi_disabled)
  40. return 0;
  41. if (type) {
  42. down_write(&bus_type_sem);
  43. list_del_init(&type->list);
  44. up_write(&bus_type_sem);
  45. printk(KERN_INFO PREFIX "ACPI bus type %s unregistered\n",
  46. type->bus->name);
  47. return 0;
  48. }
  49. return -ENODEV;
  50. }
  51. EXPORT_SYMBOL(unregister_acpi_bus_type);
  52. static struct acpi_bus_type *acpi_get_bus_type(struct bus_type *type)
  53. {
  54. struct acpi_bus_type *tmp, *ret = NULL;
  55. down_read(&bus_type_sem);
  56. list_for_each_entry(tmp, &bus_type_list, list) {
  57. if (tmp->bus == type) {
  58. ret = tmp;
  59. break;
  60. }
  61. }
  62. up_read(&bus_type_sem);
  63. return ret;
  64. }
  65. static int acpi_find_bridge_device(struct device *dev, acpi_handle * handle)
  66. {
  67. struct acpi_bus_type *tmp;
  68. int ret = -ENODEV;
  69. down_read(&bus_type_sem);
  70. list_for_each_entry(tmp, &bus_type_list, list) {
  71. if (tmp->find_bridge && !tmp->find_bridge(dev, handle)) {
  72. ret = 0;
  73. break;
  74. }
  75. }
  76. up_read(&bus_type_sem);
  77. return ret;
  78. }
  79. /* Get PCI root bridge's handle from its segment and bus number */
  80. struct acpi_find_pci_root {
  81. unsigned int seg;
  82. unsigned int bus;
  83. acpi_handle handle;
  84. };
  85. static acpi_status
  86. do_root_bridge_busnr_callback(struct acpi_resource *resource, void *data)
  87. {
  88. unsigned long *busnr = data;
  89. struct acpi_resource_address64 address;
  90. if (resource->type != ACPI_RESOURCE_TYPE_ADDRESS16 &&
  91. resource->type != ACPI_RESOURCE_TYPE_ADDRESS32 &&
  92. resource->type != ACPI_RESOURCE_TYPE_ADDRESS64)
  93. return AE_OK;
  94. acpi_resource_to_address64(resource, &address);
  95. if ((address.address_length > 0) &&
  96. (address.resource_type == ACPI_BUS_NUMBER_RANGE))
  97. *busnr = address.minimum;
  98. return AE_OK;
  99. }
  100. static int get_root_bridge_busnr(acpi_handle handle)
  101. {
  102. acpi_status status;
  103. unsigned long bus, bbn;
  104. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  105. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  106. status = acpi_evaluate_integer(handle, METHOD_NAME__BBN, NULL,
  107. &bbn);
  108. if (status == AE_NOT_FOUND) {
  109. /* Assume bus = 0 */
  110. printk(KERN_INFO PREFIX
  111. "Assume root bridge [%s] bus is 0\n",
  112. (char *)buffer.pointer);
  113. status = AE_OK;
  114. bbn = 0;
  115. }
  116. if (ACPI_FAILURE(status)) {
  117. bbn = -ENODEV;
  118. goto exit;
  119. }
  120. if (bbn > 0)
  121. goto exit;
  122. /* _BBN in some systems return 0 for all root bridges */
  123. bus = -1;
  124. status = acpi_walk_resources(handle, METHOD_NAME__CRS,
  125. do_root_bridge_busnr_callback, &bus);
  126. /* If _CRS failed, we just use _BBN */
  127. if (ACPI_FAILURE(status) || (bus == -1))
  128. goto exit;
  129. /* We select _CRS */
  130. if (bbn != bus) {
  131. printk(KERN_INFO PREFIX
  132. "_BBN and _CRS returns different value for %s. Select _CRS\n",
  133. (char *)buffer.pointer);
  134. bbn = bus;
  135. }
  136. exit:
  137. kfree(buffer.pointer);
  138. return (int)bbn;
  139. }
  140. static acpi_status
  141. find_pci_rootbridge(acpi_handle handle, u32 lvl, void *context, void **rv)
  142. {
  143. struct acpi_find_pci_root *find = (struct acpi_find_pci_root *)context;
  144. unsigned long seg, bus;
  145. acpi_status status;
  146. int tmp;
  147. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  148. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  149. status = acpi_evaluate_integer(handle, METHOD_NAME__SEG, NULL, &seg);
  150. if (status == AE_NOT_FOUND) {
  151. /* Assume seg = 0 */
  152. status = AE_OK;
  153. seg = 0;
  154. }
  155. if (ACPI_FAILURE(status)) {
  156. status = AE_CTRL_DEPTH;
  157. goto exit;
  158. }
  159. tmp = get_root_bridge_busnr(handle);
  160. if (tmp < 0) {
  161. printk(KERN_ERR PREFIX
  162. "Find root bridge failed for %s\n",
  163. (char *)buffer.pointer);
  164. status = AE_CTRL_DEPTH;
  165. goto exit;
  166. }
  167. bus = tmp;
  168. if (seg == find->seg && bus == find->bus)
  169. {
  170. find->handle = handle;
  171. status = AE_CTRL_TERMINATE;
  172. }
  173. else
  174. status = AE_OK;
  175. exit:
  176. kfree(buffer.pointer);
  177. return status;
  178. }
  179. acpi_handle acpi_get_pci_rootbridge_handle(unsigned int seg, unsigned int bus)
  180. {
  181. struct acpi_find_pci_root find = { seg, bus, NULL };
  182. acpi_get_devices(PCI_ROOT_HID_STRING, find_pci_rootbridge, &find, NULL);
  183. return find.handle;
  184. }
  185. EXPORT_SYMBOL_GPL(acpi_get_pci_rootbridge_handle);
  186. /* Get device's handler per its address under its parent */
  187. struct acpi_find_child {
  188. acpi_handle handle;
  189. acpi_integer address;
  190. };
  191. static acpi_status
  192. do_acpi_find_child(acpi_handle handle, u32 lvl, void *context, void **rv)
  193. {
  194. acpi_status status;
  195. struct acpi_device_info *info;
  196. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  197. struct acpi_find_child *find = context;
  198. status = acpi_get_object_info(handle, &buffer);
  199. if (ACPI_SUCCESS(status)) {
  200. info = buffer.pointer;
  201. if (info->address == find->address)
  202. find->handle = handle;
  203. kfree(buffer.pointer);
  204. }
  205. return AE_OK;
  206. }
  207. acpi_handle acpi_get_child(acpi_handle parent, acpi_integer address)
  208. {
  209. struct acpi_find_child find = { NULL, address };
  210. if (!parent)
  211. return NULL;
  212. acpi_walk_namespace(ACPI_TYPE_DEVICE, parent,
  213. 1, do_acpi_find_child, &find, NULL);
  214. return find.handle;
  215. }
  216. EXPORT_SYMBOL(acpi_get_child);
  217. /* Link ACPI devices with physical devices */
  218. static void acpi_glue_data_handler(acpi_handle handle,
  219. u32 function, void *context)
  220. {
  221. /* we provide an empty handler */
  222. }
  223. /* Note: a success call will increase reference count by one */
  224. struct device *acpi_get_physical_device(acpi_handle handle)
  225. {
  226. acpi_status status;
  227. struct device *dev;
  228. status = acpi_get_data(handle, acpi_glue_data_handler, (void **)&dev);
  229. if (ACPI_SUCCESS(status))
  230. return get_device(dev);
  231. return NULL;
  232. }
  233. EXPORT_SYMBOL(acpi_get_physical_device);
  234. static int acpi_bind_one(struct device *dev, acpi_handle handle)
  235. {
  236. acpi_status status;
  237. if (dev->archdata.acpi_handle) {
  238. printk(KERN_WARNING PREFIX
  239. "Drivers changed 'acpi_handle' for %s\n", dev->bus_id);
  240. return -EINVAL;
  241. }
  242. get_device(dev);
  243. status = acpi_attach_data(handle, acpi_glue_data_handler, dev);
  244. if (ACPI_FAILURE(status)) {
  245. put_device(dev);
  246. return -EINVAL;
  247. }
  248. dev->archdata.acpi_handle = handle;
  249. return 0;
  250. }
  251. static int acpi_unbind_one(struct device *dev)
  252. {
  253. if (!dev->archdata.acpi_handle)
  254. return 0;
  255. if (dev == acpi_get_physical_device(dev->archdata.acpi_handle)) {
  256. /* acpi_get_physical_device increase refcnt by one */
  257. put_device(dev);
  258. acpi_detach_data(dev->archdata.acpi_handle,
  259. acpi_glue_data_handler);
  260. dev->archdata.acpi_handle = NULL;
  261. /* acpi_bind_one increase refcnt by one */
  262. put_device(dev);
  263. } else {
  264. printk(KERN_ERR PREFIX
  265. "Oops, 'acpi_handle' corrupt for %s\n", dev->bus_id);
  266. }
  267. return 0;
  268. }
  269. static int acpi_platform_notify(struct device *dev)
  270. {
  271. struct acpi_bus_type *type;
  272. acpi_handle handle;
  273. int ret = -EINVAL;
  274. if (!dev->bus || !dev->parent) {
  275. /* bridge devices genernally haven't bus or parent */
  276. ret = acpi_find_bridge_device(dev, &handle);
  277. goto end;
  278. }
  279. type = acpi_get_bus_type(dev->bus);
  280. if (!type) {
  281. DBG("No ACPI bus support for %s\n", dev->bus_id);
  282. ret = -EINVAL;
  283. goto end;
  284. }
  285. if ((ret = type->find_device(dev, &handle)) != 0)
  286. DBG("Can't get handler for %s\n", dev->bus_id);
  287. end:
  288. if (!ret)
  289. acpi_bind_one(dev, handle);
  290. #if ACPI_GLUE_DEBUG
  291. if (!ret) {
  292. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  293. acpi_get_name(dev->archdata.acpi_handle,
  294. ACPI_FULL_PATHNAME, &buffer);
  295. DBG("Device %s -> %s\n", dev->bus_id, (char *)buffer.pointer);
  296. kfree(buffer.pointer);
  297. } else
  298. DBG("Device %s -> No ACPI support\n", dev->bus_id);
  299. #endif
  300. return ret;
  301. }
  302. static int acpi_platform_notify_remove(struct device *dev)
  303. {
  304. acpi_unbind_one(dev);
  305. return 0;
  306. }
  307. static int __init init_acpi_device_notify(void)
  308. {
  309. if (acpi_disabled)
  310. return 0;
  311. if (platform_notify || platform_notify_remove) {
  312. printk(KERN_ERR PREFIX "Can't use platform_notify\n");
  313. return 0;
  314. }
  315. platform_notify = acpi_platform_notify;
  316. platform_notify_remove = acpi_platform_notify_remove;
  317. return 0;
  318. }
  319. arch_initcall(init_acpi_device_notify);