glue.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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/acpi_iort.h>
  10. #include <linux/export.h>
  11. #include <linux/init.h>
  12. #include <linux/list.h>
  13. #include <linux/device.h>
  14. #include <linux/slab.h>
  15. #include <linux/rwsem.h>
  16. #include <linux/acpi.h>
  17. #include <linux/dma-mapping.h>
  18. #include <linux/platform_device.h>
  19. #include "internal.h"
  20. #define ACPI_GLUE_DEBUG 0
  21. #if ACPI_GLUE_DEBUG
  22. #define DBG(fmt, ...) \
  23. printk(KERN_DEBUG PREFIX fmt, ##__VA_ARGS__)
  24. #else
  25. #define DBG(fmt, ...) \
  26. do { \
  27. if (0) \
  28. printk(KERN_DEBUG PREFIX fmt, ##__VA_ARGS__); \
  29. } while (0)
  30. #endif
  31. static LIST_HEAD(bus_type_list);
  32. static DECLARE_RWSEM(bus_type_sem);
  33. #define PHYSICAL_NODE_STRING "physical_node"
  34. #define PHYSICAL_NODE_NAME_SIZE (sizeof(PHYSICAL_NODE_STRING) + 10)
  35. int register_acpi_bus_type(struct acpi_bus_type *type)
  36. {
  37. if (acpi_disabled)
  38. return -ENODEV;
  39. if (type && type->match && type->find_companion) {
  40. down_write(&bus_type_sem);
  41. list_add_tail(&type->list, &bus_type_list);
  42. up_write(&bus_type_sem);
  43. printk(KERN_INFO PREFIX "bus type %s registered\n", type->name);
  44. return 0;
  45. }
  46. return -ENODEV;
  47. }
  48. EXPORT_SYMBOL_GPL(register_acpi_bus_type);
  49. int unregister_acpi_bus_type(struct acpi_bus_type *type)
  50. {
  51. if (acpi_disabled)
  52. return 0;
  53. if (type) {
  54. down_write(&bus_type_sem);
  55. list_del_init(&type->list);
  56. up_write(&bus_type_sem);
  57. printk(KERN_INFO PREFIX "bus type %s unregistered\n",
  58. type->name);
  59. return 0;
  60. }
  61. return -ENODEV;
  62. }
  63. EXPORT_SYMBOL_GPL(unregister_acpi_bus_type);
  64. static struct acpi_bus_type *acpi_get_bus_type(struct device *dev)
  65. {
  66. struct acpi_bus_type *tmp, *ret = NULL;
  67. down_read(&bus_type_sem);
  68. list_for_each_entry(tmp, &bus_type_list, list) {
  69. if (tmp->match(dev)) {
  70. ret = tmp;
  71. break;
  72. }
  73. }
  74. up_read(&bus_type_sem);
  75. return ret;
  76. }
  77. #define FIND_CHILD_MIN_SCORE 1
  78. #define FIND_CHILD_MAX_SCORE 2
  79. static int find_child_checks(struct acpi_device *adev, bool check_children)
  80. {
  81. bool sta_present = true;
  82. unsigned long long sta;
  83. acpi_status status;
  84. status = acpi_evaluate_integer(adev->handle, "_STA", NULL, &sta);
  85. if (status == AE_NOT_FOUND)
  86. sta_present = false;
  87. else if (ACPI_FAILURE(status) || !(sta & ACPI_STA_DEVICE_ENABLED))
  88. return -ENODEV;
  89. if (check_children && list_empty(&adev->children))
  90. return -ENODEV;
  91. /*
  92. * If the device has a _HID returning a valid ACPI/PNP device ID, it is
  93. * better to make it look less attractive here, so that the other device
  94. * with the same _ADR value (that may not have a valid device ID) can be
  95. * matched going forward. [This means a second spec violation in a row,
  96. * so whatever we do here is best effort anyway.]
  97. */
  98. return sta_present && !adev->pnp.type.platform_id ?
  99. FIND_CHILD_MAX_SCORE : FIND_CHILD_MIN_SCORE;
  100. }
  101. struct acpi_device *acpi_find_child_device(struct acpi_device *parent,
  102. u64 address, bool check_children)
  103. {
  104. struct acpi_device *adev, *ret = NULL;
  105. int ret_score = 0;
  106. if (!parent)
  107. return NULL;
  108. list_for_each_entry(adev, &parent->children, node) {
  109. unsigned long long addr;
  110. acpi_status status;
  111. int score;
  112. status = acpi_evaluate_integer(adev->handle, METHOD_NAME__ADR,
  113. NULL, &addr);
  114. if (ACPI_FAILURE(status) || addr != address)
  115. continue;
  116. if (!ret) {
  117. /* This is the first matching object. Save it. */
  118. ret = adev;
  119. continue;
  120. }
  121. /*
  122. * There is more than one matching device object with the same
  123. * _ADR value. That really is unexpected, so we are kind of
  124. * beyond the scope of the spec here. We have to choose which
  125. * one to return, though.
  126. *
  127. * First, check if the previously found object is good enough
  128. * and return it if so. Second, do the same for the object that
  129. * we've just found.
  130. */
  131. if (!ret_score) {
  132. ret_score = find_child_checks(ret, check_children);
  133. if (ret_score == FIND_CHILD_MAX_SCORE)
  134. return ret;
  135. }
  136. score = find_child_checks(adev, check_children);
  137. if (score == FIND_CHILD_MAX_SCORE) {
  138. return adev;
  139. } else if (score > ret_score) {
  140. ret = adev;
  141. ret_score = score;
  142. }
  143. }
  144. return ret;
  145. }
  146. EXPORT_SYMBOL_GPL(acpi_find_child_device);
  147. static void acpi_physnode_link_name(char *buf, unsigned int node_id)
  148. {
  149. if (node_id > 0)
  150. snprintf(buf, PHYSICAL_NODE_NAME_SIZE,
  151. PHYSICAL_NODE_STRING "%u", node_id);
  152. else
  153. strcpy(buf, PHYSICAL_NODE_STRING);
  154. }
  155. int acpi_bind_one(struct device *dev, struct acpi_device *acpi_dev)
  156. {
  157. struct acpi_device_physical_node *physical_node, *pn;
  158. char physical_node_name[PHYSICAL_NODE_NAME_SIZE];
  159. struct list_head *physnode_list;
  160. unsigned int node_id;
  161. int retval = -EINVAL;
  162. if (has_acpi_companion(dev)) {
  163. if (acpi_dev) {
  164. dev_warn(dev, "ACPI companion already set\n");
  165. return -EINVAL;
  166. } else {
  167. acpi_dev = ACPI_COMPANION(dev);
  168. }
  169. }
  170. if (!acpi_dev)
  171. return -EINVAL;
  172. get_device(&acpi_dev->dev);
  173. get_device(dev);
  174. physical_node = kzalloc(sizeof(*physical_node), GFP_KERNEL);
  175. if (!physical_node) {
  176. retval = -ENOMEM;
  177. goto err;
  178. }
  179. mutex_lock(&acpi_dev->physical_node_lock);
  180. /*
  181. * Keep the list sorted by node_id so that the IDs of removed nodes can
  182. * be recycled easily.
  183. */
  184. physnode_list = &acpi_dev->physical_node_list;
  185. node_id = 0;
  186. list_for_each_entry(pn, &acpi_dev->physical_node_list, node) {
  187. /* Sanity check. */
  188. if (pn->dev == dev) {
  189. mutex_unlock(&acpi_dev->physical_node_lock);
  190. dev_warn(dev, "Already associated with ACPI node\n");
  191. kfree(physical_node);
  192. if (ACPI_COMPANION(dev) != acpi_dev)
  193. goto err;
  194. put_device(dev);
  195. put_device(&acpi_dev->dev);
  196. return 0;
  197. }
  198. if (pn->node_id == node_id) {
  199. physnode_list = &pn->node;
  200. node_id++;
  201. }
  202. }
  203. physical_node->node_id = node_id;
  204. physical_node->dev = dev;
  205. list_add(&physical_node->node, physnode_list);
  206. acpi_dev->physical_node_count++;
  207. if (!has_acpi_companion(dev))
  208. ACPI_COMPANION_SET(dev, acpi_dev);
  209. acpi_physnode_link_name(physical_node_name, node_id);
  210. retval = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
  211. physical_node_name);
  212. if (retval)
  213. dev_err(&acpi_dev->dev, "Failed to create link %s (%d)\n",
  214. physical_node_name, retval);
  215. retval = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj,
  216. "firmware_node");
  217. if (retval)
  218. dev_err(dev, "Failed to create link firmware_node (%d)\n",
  219. retval);
  220. mutex_unlock(&acpi_dev->physical_node_lock);
  221. if (acpi_dev->wakeup.flags.valid)
  222. device_set_wakeup_capable(dev, true);
  223. return 0;
  224. err:
  225. ACPI_COMPANION_SET(dev, NULL);
  226. put_device(dev);
  227. put_device(&acpi_dev->dev);
  228. return retval;
  229. }
  230. EXPORT_SYMBOL_GPL(acpi_bind_one);
  231. int acpi_unbind_one(struct device *dev)
  232. {
  233. struct acpi_device *acpi_dev = ACPI_COMPANION(dev);
  234. struct acpi_device_physical_node *entry;
  235. if (!acpi_dev)
  236. return 0;
  237. mutex_lock(&acpi_dev->physical_node_lock);
  238. list_for_each_entry(entry, &acpi_dev->physical_node_list, node)
  239. if (entry->dev == dev) {
  240. char physnode_name[PHYSICAL_NODE_NAME_SIZE];
  241. list_del(&entry->node);
  242. acpi_dev->physical_node_count--;
  243. acpi_physnode_link_name(physnode_name, entry->node_id);
  244. sysfs_remove_link(&acpi_dev->dev.kobj, physnode_name);
  245. sysfs_remove_link(&dev->kobj, "firmware_node");
  246. ACPI_COMPANION_SET(dev, NULL);
  247. /* Drop references taken by acpi_bind_one(). */
  248. put_device(dev);
  249. put_device(&acpi_dev->dev);
  250. kfree(entry);
  251. break;
  252. }
  253. mutex_unlock(&acpi_dev->physical_node_lock);
  254. return 0;
  255. }
  256. EXPORT_SYMBOL_GPL(acpi_unbind_one);
  257. static int acpi_platform_notify(struct device *dev)
  258. {
  259. struct acpi_bus_type *type = acpi_get_bus_type(dev);
  260. struct acpi_device *adev;
  261. int ret;
  262. ret = acpi_bind_one(dev, NULL);
  263. if (ret && type) {
  264. struct acpi_device *adev;
  265. adev = type->find_companion(dev);
  266. if (!adev) {
  267. DBG("Unable to get handle for %s\n", dev_name(dev));
  268. ret = -ENODEV;
  269. goto out;
  270. }
  271. ret = acpi_bind_one(dev, adev);
  272. if (ret)
  273. goto out;
  274. }
  275. adev = ACPI_COMPANION(dev);
  276. if (!adev)
  277. goto out;
  278. if (dev->bus == &platform_bus_type)
  279. acpi_configure_pmsi_domain(dev);
  280. if (type && type->setup)
  281. type->setup(dev);
  282. else if (adev->handler && adev->handler->bind)
  283. adev->handler->bind(dev);
  284. out:
  285. #if ACPI_GLUE_DEBUG
  286. if (!ret) {
  287. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  288. acpi_get_name(ACPI_HANDLE(dev), ACPI_FULL_PATHNAME, &buffer);
  289. DBG("Device %s -> %s\n", dev_name(dev), (char *)buffer.pointer);
  290. kfree(buffer.pointer);
  291. } else
  292. DBG("Device %s -> No ACPI support\n", dev_name(dev));
  293. #endif
  294. return ret;
  295. }
  296. static int acpi_platform_notify_remove(struct device *dev)
  297. {
  298. struct acpi_device *adev = ACPI_COMPANION(dev);
  299. struct acpi_bus_type *type;
  300. if (!adev)
  301. return 0;
  302. type = acpi_get_bus_type(dev);
  303. if (type && type->cleanup)
  304. type->cleanup(dev);
  305. else if (adev->handler && adev->handler->unbind)
  306. adev->handler->unbind(dev);
  307. acpi_unbind_one(dev);
  308. return 0;
  309. }
  310. void __init init_acpi_device_notify(void)
  311. {
  312. if (platform_notify || platform_notify_remove) {
  313. printk(KERN_ERR PREFIX "Can't use platform_notify\n");
  314. return;
  315. }
  316. platform_notify = acpi_platform_notify;
  317. platform_notify_remove = acpi_platform_notify_remove;
  318. }