device-remove.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Device manager
  4. *
  5. * Copyright (c) 2014 Google, Inc
  6. *
  7. * (C) Copyright 2012
  8. * Pavel Herrmann <morpheus.ibis@gmail.com>
  9. */
  10. #define LOG_CATEGORY LOGC_DM
  11. #include <common.h>
  12. #include <errno.h>
  13. #include <log.h>
  14. #include <malloc.h>
  15. #include <dm/device.h>
  16. #include <dm/device-internal.h>
  17. #include <dm/uclass.h>
  18. #include <dm/uclass-internal.h>
  19. #include <dm/util.h>
  20. #include <power-domain.h>
  21. #include <asm/global_data.h>
  22. int device_chld_unbind(struct udevice *dev, struct driver *drv)
  23. {
  24. struct udevice *pos, *n;
  25. int ret, saved_ret = 0;
  26. assert(dev);
  27. list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) {
  28. if (drv && (pos->driver != drv))
  29. continue;
  30. ret = device_unbind(pos);
  31. if (ret && !saved_ret) {
  32. log_warning("device '%s' failed to unbind\n",
  33. pos->name);
  34. saved_ret = ret;
  35. }
  36. }
  37. return log_ret(saved_ret);
  38. }
  39. int device_chld_remove(struct udevice *dev, struct driver *drv,
  40. uint flags)
  41. {
  42. struct udevice *pos, *n;
  43. int result = 0;
  44. assert(dev);
  45. list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) {
  46. int ret;
  47. if (drv && (pos->driver != drv))
  48. continue;
  49. ret = device_remove(pos, flags);
  50. if (ret == -EPROBE_DEFER)
  51. result = ret;
  52. else if (ret && ret != -EKEYREJECTED)
  53. return ret;
  54. }
  55. return result;
  56. }
  57. int device_unbind(struct udevice *dev)
  58. {
  59. const struct driver *drv;
  60. int ret;
  61. if (!dev)
  62. return log_msg_ret("dev", -EINVAL);
  63. if (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
  64. return log_msg_ret("active", -EINVAL);
  65. if (!(dev_get_flags(dev) & DM_FLAG_BOUND))
  66. return log_msg_ret("not-bound", -EINVAL);
  67. drv = dev->driver;
  68. assert(drv);
  69. if (drv->unbind) {
  70. ret = drv->unbind(dev);
  71. if (ret)
  72. return log_msg_ret("unbind", ret);
  73. }
  74. ret = device_chld_unbind(dev, NULL);
  75. if (ret)
  76. return log_msg_ret("child unbind", ret);
  77. if (dev_get_flags(dev) & DM_FLAG_ALLOC_PDATA) {
  78. free(dev_get_plat(dev));
  79. dev_set_plat(dev, NULL);
  80. }
  81. if (dev_get_flags(dev) & DM_FLAG_ALLOC_UCLASS_PDATA) {
  82. free(dev_get_uclass_plat(dev));
  83. dev_set_uclass_plat(dev, NULL);
  84. }
  85. if (dev_get_flags(dev) & DM_FLAG_ALLOC_PARENT_PDATA) {
  86. free(dev_get_parent_plat(dev));
  87. dev_set_parent_plat(dev, NULL);
  88. }
  89. ret = uclass_unbind_device(dev);
  90. if (ret)
  91. return log_msg_ret("uc", ret);
  92. if (dev->parent)
  93. list_del(&dev->sibling_node);
  94. devres_release_all(dev);
  95. if (dev_get_flags(dev) & DM_FLAG_NAME_ALLOCED)
  96. free((char *)dev->name);
  97. free(dev);
  98. return 0;
  99. }
  100. /**
  101. * device_free() - Free memory buffers allocated by a device
  102. * @dev: Device that is to be started
  103. */
  104. void device_free(struct udevice *dev)
  105. {
  106. int size;
  107. if (dev->driver->priv_auto) {
  108. free(dev_get_priv(dev));
  109. dev_set_priv(dev, NULL);
  110. }
  111. size = dev->uclass->uc_drv->per_device_auto;
  112. if (size) {
  113. free(dev_get_uclass_priv(dev));
  114. dev_set_uclass_priv(dev, NULL);
  115. }
  116. if (dev->parent) {
  117. size = dev->parent->driver->per_child_auto;
  118. if (!size) {
  119. size = dev->parent->uclass->uc_drv->
  120. per_child_auto;
  121. }
  122. if (size) {
  123. free(dev_get_parent_priv(dev));
  124. dev_set_parent_priv(dev, NULL);
  125. }
  126. }
  127. dev_bic_flags(dev, DM_FLAG_PLATDATA_VALID);
  128. devres_release_probe(dev);
  129. }
  130. /**
  131. * flags_remove() - Figure out whether to remove a device
  132. *
  133. * If this is called with @flags == DM_REMOVE_NON_VITAL | DM_REMOVE_ACTIVE_DMA,
  134. * then it returns 0 (=go head and remove) if the device is not matked vital
  135. * but is marked DM_REMOVE_ACTIVE_DMA.
  136. *
  137. * If this is called with @flags == DM_REMOVE_ACTIVE_DMA,
  138. * then it returns 0 (=go head and remove) if the device is marked
  139. * DM_REMOVE_ACTIVE_DMA, regardless of whether it is marked vital.
  140. *
  141. * @flags: Flags passed to device_remove()
  142. * @drv_flags: Driver flags
  143. * @return 0 if the device should be removed,
  144. * -EKEYREJECTED if @flags includes a flag in DM_REMOVE_ACTIVE_ALL but
  145. * @drv_flags does not (indicates that this device has nothing to do for
  146. * DMA shutdown or OS prepare)
  147. * -EPROBE_DEFER if @flags is DM_REMOVE_NON_VITAL but @drv_flags contains
  148. * DM_FLAG_VITAL (indicates the device is vital and should not be removed)
  149. */
  150. static int flags_remove(uint flags, uint drv_flags)
  151. {
  152. if (!(flags & DM_REMOVE_NORMAL)) {
  153. bool vital_match;
  154. bool active_match;
  155. active_match = !(flags & DM_REMOVE_ACTIVE_ALL) ||
  156. (drv_flags & flags);
  157. vital_match = !(flags & DM_REMOVE_NON_VITAL) ||
  158. !(drv_flags & DM_FLAG_VITAL);
  159. if (!vital_match)
  160. return -EPROBE_DEFER;
  161. if (!active_match)
  162. return -EKEYREJECTED;
  163. }
  164. return 0;
  165. }
  166. int device_remove(struct udevice *dev, uint flags)
  167. {
  168. const struct driver *drv;
  169. int ret;
  170. if (!dev)
  171. return -EINVAL;
  172. if (!(dev_get_flags(dev) & DM_FLAG_ACTIVATED))
  173. return 0;
  174. /*
  175. * If the child returns EKEYREJECTED, continue. It just means that it
  176. * didn't match the flags.
  177. */
  178. ret = device_chld_remove(dev, NULL, flags);
  179. if (ret && ret != -EKEYREJECTED)
  180. return ret;
  181. /*
  182. * Remove the device if called with the "normal" remove flag set,
  183. * or if the remove flag matches any of the drivers remove flags
  184. */
  185. drv = dev->driver;
  186. assert(drv);
  187. ret = flags_remove(flags, drv->flags);
  188. if (ret) {
  189. log_debug("%s: When removing: flags=%x, drv->flags=%x, err=%d\n",
  190. dev->name, flags, drv->flags, ret);
  191. return ret;
  192. }
  193. ret = uclass_pre_remove_device(dev);
  194. if (ret)
  195. return ret;
  196. if (drv->remove) {
  197. ret = drv->remove(dev);
  198. if (ret)
  199. goto err_remove;
  200. }
  201. if (dev->parent && dev->parent->driver->child_post_remove) {
  202. ret = dev->parent->driver->child_post_remove(dev);
  203. if (ret) {
  204. dm_warn("%s: Device '%s' failed child_post_remove()",
  205. __func__, dev->name);
  206. }
  207. }
  208. if (!(flags & DM_REMOVE_NO_PD) &&
  209. !(drv->flags &
  210. (DM_FLAG_DEFAULT_PD_CTRL_OFF | DM_FLAG_LEAVE_PD_ON)) &&
  211. dev != gd->cur_serial_dev)
  212. dev_power_domain_off(dev);
  213. device_free(dev);
  214. dev_bic_flags(dev, DM_FLAG_ACTIVATED);
  215. return 0;
  216. err_remove:
  217. /* We can't put the children back */
  218. dm_warn("%s: Device '%s' failed to remove, but children are gone\n",
  219. __func__, dev->name);
  220. return ret;
  221. }