glue.c 8.4 KB

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