glue.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 = (unsigned long *)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. acpi_os_free(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. find->handle = handle;
  170. status = AE_OK;
  171. exit:
  172. acpi_os_free(buffer.pointer);
  173. return status;
  174. }
  175. acpi_handle acpi_get_pci_rootbridge_handle(unsigned int seg, unsigned int bus)
  176. {
  177. struct acpi_find_pci_root find = { seg, bus, NULL };
  178. acpi_get_devices(PCI_ROOT_HID_STRING, find_pci_rootbridge, &find, NULL);
  179. return find.handle;
  180. }
  181. EXPORT_SYMBOL_GPL(acpi_get_pci_rootbridge_handle);
  182. /* Get device's handler per its address under its parent */
  183. struct acpi_find_child {
  184. acpi_handle handle;
  185. acpi_integer address;
  186. };
  187. static acpi_status
  188. do_acpi_find_child(acpi_handle handle, u32 lvl, void *context, void **rv)
  189. {
  190. acpi_status status;
  191. struct acpi_device_info *info;
  192. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  193. struct acpi_find_child *find = (struct acpi_find_child *)context;
  194. status = acpi_get_object_info(handle, &buffer);
  195. if (ACPI_SUCCESS(status)) {
  196. info = buffer.pointer;
  197. if (info->address == find->address)
  198. find->handle = handle;
  199. acpi_os_free(buffer.pointer);
  200. }
  201. return AE_OK;
  202. }
  203. acpi_handle acpi_get_child(acpi_handle parent, acpi_integer address)
  204. {
  205. struct acpi_find_child find = { NULL, address };
  206. if (!parent)
  207. return NULL;
  208. acpi_walk_namespace(ACPI_TYPE_DEVICE, parent,
  209. 1, do_acpi_find_child, &find, NULL);
  210. return find.handle;
  211. }
  212. EXPORT_SYMBOL(acpi_get_child);
  213. /* Link ACPI devices with physical devices */
  214. static void acpi_glue_data_handler(acpi_handle handle,
  215. u32 function, void *context)
  216. {
  217. /* we provide an empty handler */
  218. }
  219. /* Note: a success call will increase reference count by one */
  220. struct device *acpi_get_physical_device(acpi_handle handle)
  221. {
  222. acpi_status status;
  223. struct device *dev;
  224. status = acpi_get_data(handle, acpi_glue_data_handler, (void **)&dev);
  225. if (ACPI_SUCCESS(status))
  226. return get_device(dev);
  227. return NULL;
  228. }
  229. EXPORT_SYMBOL(acpi_get_physical_device);
  230. static int acpi_bind_one(struct device *dev, acpi_handle handle)
  231. {
  232. acpi_status status;
  233. if (dev->firmware_data) {
  234. printk(KERN_WARNING PREFIX
  235. "Drivers changed 'firmware_data' for %s\n", dev->bus_id);
  236. return -EINVAL;
  237. }
  238. get_device(dev);
  239. status = acpi_attach_data(handle, acpi_glue_data_handler, dev);
  240. if (ACPI_FAILURE(status)) {
  241. put_device(dev);
  242. return -EINVAL;
  243. }
  244. dev->firmware_data = handle;
  245. return 0;
  246. }
  247. static int acpi_unbind_one(struct device *dev)
  248. {
  249. if (!dev->firmware_data)
  250. return 0;
  251. if (dev == acpi_get_physical_device(dev->firmware_data)) {
  252. /* acpi_get_physical_device increase refcnt by one */
  253. put_device(dev);
  254. acpi_detach_data(dev->firmware_data, acpi_glue_data_handler);
  255. dev->firmware_data = NULL;
  256. /* acpi_bind_one increase refcnt by one */
  257. put_device(dev);
  258. } else {
  259. printk(KERN_ERR PREFIX
  260. "Oops, 'firmware_data' corrupt for %s\n", dev->bus_id);
  261. }
  262. return 0;
  263. }
  264. static int acpi_platform_notify(struct device *dev)
  265. {
  266. struct acpi_bus_type *type;
  267. acpi_handle handle;
  268. int ret = -EINVAL;
  269. if (!dev->bus || !dev->parent) {
  270. /* bridge devices genernally haven't bus or parent */
  271. ret = acpi_find_bridge_device(dev, &handle);
  272. goto end;
  273. }
  274. type = acpi_get_bus_type(dev->bus);
  275. if (!type) {
  276. DBG("No ACPI bus support for %s\n", dev->bus_id);
  277. ret = -EINVAL;
  278. goto end;
  279. }
  280. if ((ret = type->find_device(dev, &handle)) != 0)
  281. DBG("Can't get handler for %s\n", dev->bus_id);
  282. end:
  283. if (!ret)
  284. acpi_bind_one(dev, handle);
  285. #if ACPI_GLUE_DEBUG
  286. if (!ret) {
  287. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  288. acpi_get_name(dev->firmware_data, ACPI_FULL_PATHNAME, &buffer);
  289. DBG("Device %s -> %s\n", dev->bus_id, (char *)buffer.pointer);
  290. acpi_os_free(buffer.pointer);
  291. } else
  292. DBG("Device %s -> No ACPI support\n", dev->bus_id);
  293. #endif
  294. return ret;
  295. }
  296. static int acpi_platform_notify_remove(struct device *dev)
  297. {
  298. acpi_unbind_one(dev);
  299. return 0;
  300. }
  301. static int __init init_acpi_device_notify(void)
  302. {
  303. if (acpi_disabled)
  304. return 0;
  305. if (platform_notify || platform_notify_remove) {
  306. printk(KERN_ERR PREFIX "Can't use platform_notify\n");
  307. return 0;
  308. }
  309. platform_notify = acpi_platform_notify;
  310. platform_notify_remove = acpi_platform_notify_remove;
  311. return 0;
  312. }
  313. arch_initcall(init_acpi_device_notify);