device-remove.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. device_foreach_child_safe(pos, n, dev) {
  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. device_foreach_child_safe(pos, n, dev) {
  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. ret = uclass_pre_unbind_device(dev);
  78. if (ret)
  79. return log_msg_ret("uc", ret);
  80. if (dev_get_flags(dev) & DM_FLAG_ALLOC_PDATA) {
  81. free(dev_get_plat(dev));
  82. dev_set_plat(dev, NULL);
  83. }
  84. if (dev_get_flags(dev) & DM_FLAG_ALLOC_UCLASS_PDATA) {
  85. free(dev_get_uclass_plat(dev));
  86. dev_set_uclass_plat(dev, NULL);
  87. }
  88. if (dev_get_flags(dev) & DM_FLAG_ALLOC_PARENT_PDATA) {
  89. free(dev_get_parent_plat(dev));
  90. dev_set_parent_plat(dev, NULL);
  91. }
  92. ret = uclass_unbind_device(dev);
  93. if (ret)
  94. return log_msg_ret("uc", ret);
  95. if (dev->parent)
  96. list_del(&dev->sibling_node);
  97. devres_release_all(dev);
  98. if (dev_get_flags(dev) & DM_FLAG_NAME_ALLOCED)
  99. free((char *)dev->name);
  100. free(dev);
  101. return 0;
  102. }
  103. /**
  104. * device_free() - Free memory buffers allocated by a device
  105. * @dev: Device that is to be started
  106. */
  107. void device_free(struct udevice *dev)
  108. {
  109. int size;
  110. if (dev->driver->priv_auto) {
  111. free(dev_get_priv(dev));
  112. dev_set_priv(dev, NULL);
  113. }
  114. size = dev->uclass->uc_drv->per_device_auto;
  115. if (size) {
  116. free(dev_get_uclass_priv(dev));
  117. dev_set_uclass_priv(dev, NULL);
  118. }
  119. if (dev->parent) {
  120. size = dev->parent->driver->per_child_auto;
  121. if (!size)
  122. size = dev->parent->uclass->uc_drv->per_child_auto;
  123. if (size) {
  124. free(dev_get_parent_priv(dev));
  125. dev_set_parent_priv(dev, NULL);
  126. }
  127. }
  128. dev_bic_flags(dev, DM_FLAG_PLATDATA_VALID);
  129. devres_release_probe(dev);
  130. }
  131. /**
  132. * flags_remove() - Figure out whether to remove a device
  133. *
  134. * If this is called with @flags == DM_REMOVE_NON_VITAL | DM_REMOVE_ACTIVE_DMA,
  135. * then it returns 0 (=go head and remove) if the device is not matked vital
  136. * but is marked DM_REMOVE_ACTIVE_DMA.
  137. *
  138. * If this is called with @flags == DM_REMOVE_ACTIVE_DMA,
  139. * then it returns 0 (=go head and remove) if the device is marked
  140. * DM_REMOVE_ACTIVE_DMA, regardless of whether it is marked vital.
  141. *
  142. * @flags: Flags passed to device_remove()
  143. * @drv_flags: Driver flags
  144. * Return: 0 if the device should be removed,
  145. * -EKEYREJECTED if @flags includes a flag in DM_REMOVE_ACTIVE_ALL but
  146. * @drv_flags does not (indicates that this device has nothing to do for
  147. * DMA shutdown or OS prepare)
  148. * -EPROBE_DEFER if @flags is DM_REMOVE_NON_VITAL but @drv_flags contains
  149. * DM_FLAG_VITAL (indicates the device is vital and should not be removed)
  150. */
  151. static int flags_remove(uint flags, uint drv_flags)
  152. {
  153. if (!(flags & DM_REMOVE_NORMAL)) {
  154. bool vital_match;
  155. bool active_match;
  156. active_match = !(flags & DM_REMOVE_ACTIVE_ALL) ||
  157. (drv_flags & flags);
  158. vital_match = !(flags & DM_REMOVE_NON_VITAL) ||
  159. !(drv_flags & DM_FLAG_VITAL);
  160. if (!vital_match)
  161. return -EPROBE_DEFER;
  162. if (!active_match)
  163. return -EKEYREJECTED;
  164. }
  165. return 0;
  166. }
  167. int device_remove(struct udevice *dev, uint flags)
  168. {
  169. const struct driver *drv;
  170. int ret;
  171. if (!dev)
  172. return -EINVAL;
  173. if (!(dev_get_flags(dev) & DM_FLAG_ACTIVATED))
  174. return 0;
  175. ret = device_notify(dev, EVT_DM_PRE_REMOVE);
  176. if (ret)
  177. return ret;
  178. /*
  179. * If the child returns EKEYREJECTED, continue. It just means that it
  180. * didn't match the flags.
  181. */
  182. ret = device_chld_remove(dev, NULL, flags);
  183. if (ret && ret != -EKEYREJECTED)
  184. return ret;
  185. /*
  186. * Remove the device if called with the "normal" remove flag set,
  187. * or if the remove flag matches any of the drivers remove flags
  188. */
  189. drv = dev->driver;
  190. assert(drv);
  191. ret = flags_remove(flags, drv->flags);
  192. if (ret) {
  193. log_debug("%s: When removing: flags=%x, drv->flags=%x, err=%d\n",
  194. dev->name, flags, drv->flags, ret);
  195. return ret;
  196. }
  197. ret = uclass_pre_remove_device(dev);
  198. if (ret)
  199. return ret;
  200. if (drv->remove) {
  201. ret = drv->remove(dev);
  202. if (ret)
  203. goto err_remove;
  204. }
  205. if (dev->parent && dev->parent->driver->child_post_remove) {
  206. ret = dev->parent->driver->child_post_remove(dev);
  207. if (ret) {
  208. dm_warn("%s: Device '%s' failed child_post_remove()",
  209. __func__, dev->name);
  210. }
  211. }
  212. if (!(flags & DM_REMOVE_NO_PD) &&
  213. !(drv->flags &
  214. (DM_FLAG_DEFAULT_PD_CTRL_OFF | DM_FLAG_LEAVE_PD_ON)) &&
  215. dev != gd->cur_serial_dev)
  216. dev_power_domain_off(dev);
  217. device_free(dev);
  218. dev_bic_flags(dev, DM_FLAG_ACTIVATED);
  219. ret = device_notify(dev, EVT_DM_POST_REMOVE);
  220. if (ret)
  221. goto err_remove;
  222. return 0;
  223. err_remove:
  224. /* We can't put the children back */
  225. dm_warn("%s: Device '%s' failed to remove, but children are gone\n",
  226. __func__, dev->name);
  227. return ret;
  228. }