glue.c 8.9 KB

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