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", type->bus->name);
  31. return 0;
  32. }
  33. return -ENODEV;
  34. }
  35. EXPORT_SYMBOL(register_acpi_bus_type);
  36. int unregister_acpi_bus_type(struct acpi_bus_type *type)
  37. {
  38. if (acpi_disabled)
  39. return 0;
  40. if (type) {
  41. down_write(&bus_type_sem);
  42. list_del_init(&type->list);
  43. up_write(&bus_type_sem);
  44. printk(KERN_INFO PREFIX "ACPI bus type %s unregistered\n", type->bus->name);
  45. return 0;
  46. }
  47. return -ENODEV;
  48. }
  49. EXPORT_SYMBOL(unregister_acpi_bus_type);
  50. static struct acpi_bus_type *acpi_get_bus_type(struct bus_type *type)
  51. {
  52. struct acpi_bus_type *tmp, *ret = NULL;
  53. down_read(&bus_type_sem);
  54. list_for_each_entry(tmp, &bus_type_list, list) {
  55. if (tmp->bus == type) {
  56. ret = tmp;
  57. break;
  58. }
  59. }
  60. up_read(&bus_type_sem);
  61. return ret;
  62. }
  63. static int acpi_find_bridge_device(struct device *dev, acpi_handle * handle)
  64. {
  65. struct acpi_bus_type *tmp;
  66. int ret = -ENODEV;
  67. down_read(&bus_type_sem);
  68. list_for_each_entry(tmp, &bus_type_list, list) {
  69. if (tmp->find_bridge && !tmp->find_bridge(dev, handle)) {
  70. ret = 0;
  71. break;
  72. }
  73. }
  74. up_read(&bus_type_sem);
  75. return ret;
  76. }
  77. /* Get PCI root bridge's handle from its segment and bus number */
  78. struct acpi_find_pci_root {
  79. unsigned int seg;
  80. unsigned int bus;
  81. acpi_handle handle;
  82. };
  83. static acpi_status
  84. do_root_bridge_busnr_callback(struct acpi_resource *resource, void *data)
  85. {
  86. int *busnr = (int *)data;
  87. struct acpi_resource_address64 address;
  88. if (resource->id != ACPI_RSTYPE_ADDRESS16 &&
  89. resource->id != ACPI_RSTYPE_ADDRESS32 &&
  90. resource->id != ACPI_RSTYPE_ADDRESS64)
  91. return AE_OK;
  92. acpi_resource_to_address64(resource, &address);
  93. if ((address.address_length > 0) &&
  94. (address.resource_type == ACPI_BUS_NUMBER_RANGE))
  95. *busnr = address.min_address_range;
  96. return AE_OK;
  97. }
  98. static int get_root_bridge_busnr(acpi_handle handle)
  99. {
  100. acpi_status status;
  101. int bus, bbn;
  102. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  103. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  104. status = acpi_evaluate_integer(handle, METHOD_NAME__BBN, NULL,
  105. (unsigned long *)&bbn);
  106. if (status == AE_NOT_FOUND) {
  107. /* Assume bus = 0 */
  108. printk(KERN_INFO PREFIX
  109. "Assume root bridge [%s] bus is 0\n",
  110. (char *)buffer.pointer);
  111. status = AE_OK;
  112. bbn = 0;
  113. }
  114. if (ACPI_FAILURE(status)) {
  115. bbn = -ENODEV;
  116. goto exit;
  117. }
  118. if (bbn > 0)
  119. goto exit;
  120. /* _BBN in some systems return 0 for all root bridges */
  121. bus = -1;
  122. status = acpi_walk_resources(handle, METHOD_NAME__CRS,
  123. do_root_bridge_busnr_callback, &bus);
  124. /* If _CRS failed, we just use _BBN */
  125. if (ACPI_FAILURE(status) || (bus == -1))
  126. goto exit;
  127. /* We select _CRS */
  128. if (bbn != bus) {
  129. printk(KERN_INFO PREFIX
  130. "_BBN and _CRS returns different value for %s. Select _CRS\n",
  131. (char *)buffer.pointer);
  132. bbn = bus;
  133. }
  134. exit:
  135. acpi_os_free(buffer.pointer);
  136. return bbn;
  137. }
  138. static acpi_status
  139. find_pci_rootbridge(acpi_handle handle, u32 lvl, void *context, void **rv)
  140. {
  141. struct acpi_find_pci_root *find = (struct acpi_find_pci_root *)context;
  142. unsigned long seg, bus;
  143. acpi_status status;
  144. int tmp;
  145. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  146. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  147. status = acpi_evaluate_integer(handle, METHOD_NAME__SEG, NULL, &seg);
  148. if (status == AE_NOT_FOUND) {
  149. /* Assume seg = 0 */
  150. printk(KERN_INFO PREFIX
  151. "Assume root bridge [%s] segment is 0\n",
  152. (char *)buffer.pointer);
  153. status = AE_OK;
  154. seg = 0;
  155. }
  156. if (ACPI_FAILURE(status)) {
  157. status = AE_CTRL_DEPTH;
  158. goto exit;
  159. }
  160. tmp = get_root_bridge_busnr(handle);
  161. if (tmp < 0) {
  162. printk(KERN_ERR PREFIX
  163. "Find root bridge failed for %s\n",
  164. (char *)buffer.pointer);
  165. status = AE_CTRL_DEPTH;
  166. goto exit;
  167. }
  168. bus = tmp;
  169. if (seg == find->seg && bus == find->bus)
  170. find->handle = handle;
  171. status = AE_OK;
  172. exit:
  173. acpi_os_free(buffer.pointer);
  174. return status;
  175. }
  176. acpi_handle acpi_get_pci_rootbridge_handle(unsigned int seg, unsigned int bus)
  177. {
  178. struct acpi_find_pci_root find = { seg, bus, NULL };
  179. acpi_get_devices(PCI_ROOT_HID_STRING, find_pci_rootbridge, &find, NULL);
  180. return find.handle;
  181. }
  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);