glue.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 <[email protected]>
  6. * Copyright (c) 2005 Intel Corp.
  7. */
  8. #define pr_fmt(fmt) "ACPI: " fmt
  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/pci.h>
  19. #include <linux/pci-acpi.h>
  20. #include <linux/platform_device.h>
  21. #include "internal.h"
  22. static LIST_HEAD(bus_type_list);
  23. static DECLARE_RWSEM(bus_type_sem);
  24. #define PHYSICAL_NODE_STRING "physical_node"
  25. #define PHYSICAL_NODE_NAME_SIZE (sizeof(PHYSICAL_NODE_STRING) + 10)
  26. int register_acpi_bus_type(struct acpi_bus_type *type)
  27. {
  28. if (acpi_disabled)
  29. return -ENODEV;
  30. if (type && type->match && type->find_companion) {
  31. down_write(&bus_type_sem);
  32. list_add_tail(&type->list, &bus_type_list);
  33. up_write(&bus_type_sem);
  34. pr_info("bus type %s registered\n", type->name);
  35. return 0;
  36. }
  37. return -ENODEV;
  38. }
  39. EXPORT_SYMBOL_GPL(register_acpi_bus_type);
  40. int unregister_acpi_bus_type(struct acpi_bus_type *type)
  41. {
  42. if (acpi_disabled)
  43. return 0;
  44. if (type) {
  45. down_write(&bus_type_sem);
  46. list_del_init(&type->list);
  47. up_write(&bus_type_sem);
  48. pr_info("bus type %s unregistered\n", type->name);
  49. return 0;
  50. }
  51. return -ENODEV;
  52. }
  53. EXPORT_SYMBOL_GPL(unregister_acpi_bus_type);
  54. static struct acpi_bus_type *acpi_get_bus_type(struct device *dev)
  55. {
  56. struct acpi_bus_type *tmp, *ret = NULL;
  57. down_read(&bus_type_sem);
  58. list_for_each_entry(tmp, &bus_type_list, list) {
  59. if (tmp->match(dev)) {
  60. ret = tmp;
  61. break;
  62. }
  63. }
  64. up_read(&bus_type_sem);
  65. return ret;
  66. }
  67. #define FIND_CHILD_MIN_SCORE 1
  68. #define FIND_CHILD_MID_SCORE 2
  69. #define FIND_CHILD_MAX_SCORE 3
  70. static int match_any(struct acpi_device *adev, void *not_used)
  71. {
  72. return 1;
  73. }
  74. static bool acpi_dev_has_children(struct acpi_device *adev)
  75. {
  76. return acpi_dev_for_each_child(adev, match_any, NULL) > 0;
  77. }
  78. static int find_child_checks(struct acpi_device *adev, bool check_children)
  79. {
  80. unsigned long long sta;
  81. acpi_status status;
  82. if (check_children && !acpi_dev_has_children(adev))
  83. return -ENODEV;
  84. status = acpi_evaluate_integer(adev->handle, "_STA", NULL, &sta);
  85. if (status == AE_NOT_FOUND) {
  86. /*
  87. * Special case: backlight device objects without _STA are
  88. * preferred to other objects with the same _ADR value, because
  89. * it is more likely that they are actually useful.
  90. */
  91. if (adev->pnp.type.backlight)
  92. return FIND_CHILD_MID_SCORE;
  93. return FIND_CHILD_MIN_SCORE;
  94. }
  95. if (ACPI_FAILURE(status) || !(sta & ACPI_STA_DEVICE_ENABLED))
  96. return -ENODEV;
  97. /*
  98. * If the device has a _HID returning a valid ACPI/PNP device ID, it is
  99. * better to make it look less attractive here, so that the other device
  100. * with the same _ADR value (that may not have a valid device ID) can be
  101. * matched going forward. [This means a second spec violation in a row,
  102. * so whatever we do here is best effort anyway.]
  103. */
  104. if (adev->pnp.type.platform_id)
  105. return FIND_CHILD_MIN_SCORE;
  106. return FIND_CHILD_MAX_SCORE;
  107. }
  108. struct find_child_walk_data {
  109. struct acpi_device *adev;
  110. u64 address;
  111. int score;
  112. bool check_sta;
  113. bool check_children;
  114. };
  115. static int check_one_child(struct acpi_device *adev, void *data)
  116. {
  117. struct find_child_walk_data *wd = data;
  118. int score;
  119. if (!adev->pnp.type.bus_address || acpi_device_adr(adev) != wd->address)
  120. return 0;
  121. if (!wd->adev) {
  122. /*
  123. * This is the first matching object, so save it. If it is not
  124. * necessary to look for any other matching objects, stop the
  125. * search.
  126. */
  127. wd->adev = adev;
  128. return !(wd->check_sta || wd->check_children);
  129. }
  130. /*
  131. * There is more than one matching device object with the same _ADR
  132. * value. That really is unexpected, so we are kind of beyond the scope
  133. * of the spec here. We have to choose which one to return, though.
  134. *
  135. * First, get the score for the previously found object and terminate
  136. * the walk if it is maximum.
  137. */
  138. if (!wd->score) {
  139. score = find_child_checks(wd->adev, wd->check_children);
  140. if (score == FIND_CHILD_MAX_SCORE)
  141. return 1;
  142. wd->score = score;
  143. }
  144. /*
  145. * Second, if the object that has just been found has a better score,
  146. * replace the previously found one with it and terminate the walk if
  147. * the new score is maximum.
  148. */
  149. score = find_child_checks(adev, wd->check_children);
  150. if (score > wd->score) {
  151. wd->adev = adev;
  152. if (score == FIND_CHILD_MAX_SCORE)
  153. return 1;
  154. wd->score = score;
  155. }
  156. /* Continue, because there may be better matches. */
  157. return 0;
  158. }
  159. static struct acpi_device *acpi_find_child(struct acpi_device *parent,
  160. u64 address, bool check_children,
  161. bool check_sta)
  162. {
  163. struct find_child_walk_data wd = {
  164. .address = address,
  165. .check_children = check_children,
  166. .check_sta = check_sta,
  167. .adev = NULL,
  168. .score = 0,
  169. };
  170. if (parent)
  171. acpi_dev_for_each_child(parent, check_one_child, &wd);
  172. return wd.adev;
  173. }
  174. struct acpi_device *acpi_find_child_device(struct acpi_device *parent,
  175. u64 address, bool check_children)
  176. {
  177. return acpi_find_child(parent, address, check_children, true);
  178. }
  179. EXPORT_SYMBOL_GPL(acpi_find_child_device);
  180. struct acpi_device *acpi_find_child_by_adr(struct acpi_device *adev,
  181. acpi_bus_address adr)
  182. {
  183. return acpi_find_child(adev, adr, false, false);
  184. }
  185. EXPORT_SYMBOL_GPL(acpi_find_child_by_adr);
  186. static void acpi_physnode_link_name(char *buf, unsigned int node_id)
  187. {
  188. if (node_id > 0)
  189. snprintf(buf, PHYSICAL_NODE_NAME_SIZE,
  190. PHYSICAL_NODE_STRING "%u", node_id);
  191. else
  192. strcpy(buf, PHYSICAL_NODE_STRING);
  193. }
  194. int acpi_bind_one(struct device *dev, struct acpi_device *acpi_dev)
  195. {
  196. struct acpi_device_physical_node *physical_node, *pn;
  197. char physical_node_name[PHYSICAL_NODE_NAME_SIZE];
  198. struct list_head *physnode_list;
  199. unsigned int node_id;
  200. int retval = -EINVAL;
  201. if (has_acpi_companion(dev)) {
  202. if (acpi_dev) {
  203. dev_warn(dev, "ACPI companion already set\n");
  204. return -EINVAL;
  205. } else {
  206. acpi_dev = ACPI_COMPANION(dev);
  207. }
  208. }
  209. if (!acpi_dev)
  210. return -EINVAL;
  211. acpi_dev_get(acpi_dev);
  212. get_device(dev);
  213. physical_node = kzalloc(sizeof(*physical_node), GFP_KERNEL);
  214. if (!physical_node) {
  215. retval = -ENOMEM;
  216. goto err;
  217. }
  218. mutex_lock(&acpi_dev->physical_node_lock);
  219. /*
  220. * Keep the list sorted by node_id so that the IDs of removed nodes can
  221. * be recycled easily.
  222. */
  223. physnode_list = &acpi_dev->physical_node_list;
  224. node_id = 0;
  225. list_for_each_entry(pn, &acpi_dev->physical_node_list, node) {
  226. /* Sanity check. */
  227. if (pn->dev == dev) {
  228. mutex_unlock(&acpi_dev->physical_node_lock);
  229. dev_warn(dev, "Already associated with ACPI node\n");
  230. kfree(physical_node);
  231. if (ACPI_COMPANION(dev) != acpi_dev)
  232. goto err;
  233. put_device(dev);
  234. acpi_dev_put(acpi_dev);
  235. return 0;
  236. }
  237. if (pn->node_id == node_id) {
  238. physnode_list = &pn->node;
  239. node_id++;
  240. }
  241. }
  242. physical_node->node_id = node_id;
  243. physical_node->dev = dev;
  244. list_add(&physical_node->node, physnode_list);
  245. acpi_dev->physical_node_count++;
  246. if (!has_acpi_companion(dev))
  247. ACPI_COMPANION_SET(dev, acpi_dev);
  248. acpi_physnode_link_name(physical_node_name, node_id);
  249. retval = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
  250. physical_node_name);
  251. if (retval)
  252. dev_err(&acpi_dev->dev, "Failed to create link %s (%d)\n",
  253. physical_node_name, retval);
  254. retval = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj,
  255. "firmware_node");
  256. if (retval)
  257. dev_err(dev, "Failed to create link firmware_node (%d)\n",
  258. retval);
  259. mutex_unlock(&acpi_dev->physical_node_lock);
  260. if (acpi_dev->wakeup.flags.valid)
  261. device_set_wakeup_capable(dev, true);
  262. return 0;
  263. err:
  264. ACPI_COMPANION_SET(dev, NULL);
  265. put_device(dev);
  266. acpi_dev_put(acpi_dev);
  267. return retval;
  268. }
  269. EXPORT_SYMBOL_GPL(acpi_bind_one);
  270. int acpi_unbind_one(struct device *dev)
  271. {
  272. struct acpi_device *acpi_dev = ACPI_COMPANION(dev);
  273. struct acpi_device_physical_node *entry;
  274. if (!acpi_dev)
  275. return 0;
  276. mutex_lock(&acpi_dev->physical_node_lock);
  277. list_for_each_entry(entry, &acpi_dev->physical_node_list, node)
  278. if (entry->dev == dev) {
  279. char physnode_name[PHYSICAL_NODE_NAME_SIZE];
  280. list_del(&entry->node);
  281. acpi_dev->physical_node_count--;
  282. acpi_physnode_link_name(physnode_name, entry->node_id);
  283. sysfs_remove_link(&acpi_dev->dev.kobj, physnode_name);
  284. sysfs_remove_link(&dev->kobj, "firmware_node");
  285. ACPI_COMPANION_SET(dev, NULL);
  286. /* Drop references taken by acpi_bind_one(). */
  287. put_device(dev);
  288. acpi_dev_put(acpi_dev);
  289. kfree(entry);
  290. break;
  291. }
  292. mutex_unlock(&acpi_dev->physical_node_lock);
  293. return 0;
  294. }
  295. EXPORT_SYMBOL_GPL(acpi_unbind_one);
  296. void acpi_device_notify(struct device *dev)
  297. {
  298. struct acpi_device *adev;
  299. int ret;
  300. ret = acpi_bind_one(dev, NULL);
  301. if (ret) {
  302. struct acpi_bus_type *type = acpi_get_bus_type(dev);
  303. if (!type)
  304. goto err;
  305. adev = type->find_companion(dev);
  306. if (!adev) {
  307. dev_dbg(dev, "ACPI companion not found\n");
  308. goto err;
  309. }
  310. ret = acpi_bind_one(dev, adev);
  311. if (ret)
  312. goto err;
  313. if (type->setup) {
  314. type->setup(dev);
  315. goto done;
  316. }
  317. } else {
  318. adev = ACPI_COMPANION(dev);
  319. if (dev_is_pci(dev)) {
  320. pci_acpi_setup(dev, adev);
  321. goto done;
  322. } else if (dev_is_platform(dev)) {
  323. acpi_configure_pmsi_domain(dev);
  324. }
  325. }
  326. if (adev->handler && adev->handler->bind)
  327. adev->handler->bind(dev);
  328. done:
  329. acpi_handle_debug(ACPI_HANDLE(dev), "Bound to device %s\n",
  330. dev_name(dev));
  331. return;
  332. err:
  333. dev_dbg(dev, "No ACPI support\n");
  334. }
  335. void acpi_device_notify_remove(struct device *dev)
  336. {
  337. struct acpi_device *adev = ACPI_COMPANION(dev);
  338. if (!adev)
  339. return;
  340. if (dev_is_pci(dev))
  341. pci_acpi_cleanup(dev, adev);
  342. else if (adev->handler && adev->handler->unbind)
  343. adev->handler->unbind(dev);
  344. acpi_unbind_one(dev);
  345. }