blob: a86b9325dd8d68047c52c54446ef2be2ef8290d7 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass3ac435d2014-11-10 17:16:47 -07002/*
3 * Device manager
4 *
5 * Copyright (c) 2014 Google, Inc
6 *
7 * (C) Copyright 2012
8 * Pavel Herrmann <morpheus.ibis@gmail.com>
Simon Glass3ac435d2014-11-10 17:16:47 -07009 */
10
Simon Glassc51d2e72021-01-24 14:32:45 -070011#define LOG_CATEGORY LOGC_DM
12
Simon Glass3ac435d2014-11-10 17:16:47 -070013#include <common.h>
14#include <errno.h>
Simon Glass8474da92020-03-28 14:03:47 -060015#include <log.h>
Simon Glass3ac435d2014-11-10 17:16:47 -070016#include <malloc.h>
17#include <dm/device.h>
18#include <dm/device-internal.h>
19#include <dm/uclass.h>
20#include <dm/uclass-internal.h>
21#include <dm/util.h>
Anatolij Gustschin52edfed2019-09-27 13:48:15 +053022#include <power-domain.h>
Simon Glass401d1c42020-10-30 21:38:53 -060023#include <asm/global_data.h>
Simon Glass3ac435d2014-11-10 17:16:47 -070024
Jean-Jacques Hiblot3be9bcb2018-08-09 16:17:45 +020025int device_chld_unbind(struct udevice *dev, struct driver *drv)
Simon Glass3ac435d2014-11-10 17:16:47 -070026{
27 struct udevice *pos, *n;
28 int ret, saved_ret = 0;
29
30 assert(dev);
31
Sean Anderson501a9a72022-03-30 12:26:40 -040032 device_foreach_child_safe(pos, n, dev) {
Jean-Jacques Hiblot3be9bcb2018-08-09 16:17:45 +020033 if (drv && (pos->driver != drv))
34 continue;
35
Simon Glass3ac435d2014-11-10 17:16:47 -070036 ret = device_unbind(pos);
Simon Glass8474da92020-03-28 14:03:47 -060037 if (ret && !saved_ret) {
38 log_warning("device '%s' failed to unbind\n",
39 pos->name);
Simon Glass3ac435d2014-11-10 17:16:47 -070040 saved_ret = ret;
Simon Glass8474da92020-03-28 14:03:47 -060041 }
Simon Glass3ac435d2014-11-10 17:16:47 -070042 }
43
Simon Glass8474da92020-03-28 14:03:47 -060044 return log_ret(saved_ret);
Simon Glass3ac435d2014-11-10 17:16:47 -070045}
46
Jean-Jacques Hiblot3be9bcb2018-08-09 16:17:45 +020047int device_chld_remove(struct udevice *dev, struct driver *drv,
48 uint flags)
Simon Glass3ac435d2014-11-10 17:16:47 -070049{
50 struct udevice *pos, *n;
Marek Vasutcc6f4c82021-01-24 14:32:46 -070051 int result = 0;
Simon Glass3ac435d2014-11-10 17:16:47 -070052
53 assert(dev);
54
Sean Anderson501a9a72022-03-30 12:26:40 -040055 device_foreach_child_safe(pos, n, dev) {
Marek Vasutcc6f4c82021-01-24 14:32:46 -070056 int ret;
57
Jean-Jacques Hiblot3be9bcb2018-08-09 16:17:45 +020058 if (drv && (pos->driver != drv))
59 continue;
60
Stefan Roese706865a2017-03-20 12:51:48 +010061 ret = device_remove(pos, flags);
Marek Vasutcc6f4c82021-01-24 14:32:46 -070062 if (ret == -EPROBE_DEFER)
63 result = ret;
64 else if (ret && ret != -EKEYREJECTED)
Simon Glass3ac435d2014-11-10 17:16:47 -070065 return ret;
66 }
67
Marek Vasutcc6f4c82021-01-24 14:32:46 -070068 return result;
Simon Glass3ac435d2014-11-10 17:16:47 -070069}
70
71int device_unbind(struct udevice *dev)
72{
Simon Glass34792532015-03-25 12:21:54 -060073 const struct driver *drv;
Simon Glass3ac435d2014-11-10 17:16:47 -070074 int ret;
75
76 if (!dev)
Simon Glass8474da92020-03-28 14:03:47 -060077 return log_msg_ret("dev", -EINVAL);
Simon Glass3ac435d2014-11-10 17:16:47 -070078
Simon Glass73466df2020-12-19 10:40:10 -070079 if (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
Simon Glass8474da92020-03-28 14:03:47 -060080 return log_msg_ret("active", -EINVAL);
Simon Glass3ac435d2014-11-10 17:16:47 -070081
Simon Glass73466df2020-12-19 10:40:10 -070082 if (!(dev_get_flags(dev) & DM_FLAG_BOUND))
Simon Glass8474da92020-03-28 14:03:47 -060083 return log_msg_ret("not-bound", -EINVAL);
Masahiro Yamadaaed1a4d2015-07-25 21:52:34 +090084
Simon Glass3ac435d2014-11-10 17:16:47 -070085 drv = dev->driver;
86 assert(drv);
87
88 if (drv->unbind) {
89 ret = drv->unbind(dev);
90 if (ret)
Simon Glass8474da92020-03-28 14:03:47 -060091 return log_msg_ret("unbind", ret);
Simon Glass3ac435d2014-11-10 17:16:47 -070092 }
93
Jean-Jacques Hiblot3be9bcb2018-08-09 16:17:45 +020094 ret = device_chld_unbind(dev, NULL);
Simon Glass3ac435d2014-11-10 17:16:47 -070095 if (ret)
Simon Glass8474da92020-03-28 14:03:47 -060096 return log_msg_ret("child unbind", ret);
Simon Glass3ac435d2014-11-10 17:16:47 -070097
Simon Glass80443182021-10-23 17:26:05 -060098 ret = uclass_pre_unbind_device(dev);
99 if (ret)
100 return log_msg_ret("uc", ret);
Simon Glass73466df2020-12-19 10:40:10 -0700101 if (dev_get_flags(dev) & DM_FLAG_ALLOC_PDATA) {
Simon Glass0fd3d912020-12-22 19:30:28 -0700102 free(dev_get_plat(dev));
103 dev_set_plat(dev, NULL);
Simon Glassf8a85442015-01-25 08:27:00 -0700104 }
Simon Glass73466df2020-12-19 10:40:10 -0700105 if (dev_get_flags(dev) & DM_FLAG_ALLOC_UCLASS_PDATA) {
Simon Glass0fd3d912020-12-22 19:30:28 -0700106 free(dev_get_uclass_plat(dev));
Simon Glass89ba6d52020-12-22 19:30:29 -0700107 dev_set_uclass_plat(dev, NULL);
Przemyslaw Marczak5eaed882015-04-15 13:07:18 +0200108 }
Simon Glass73466df2020-12-19 10:40:10 -0700109 if (dev_get_flags(dev) & DM_FLAG_ALLOC_PARENT_PDATA) {
Simon Glass0fd3d912020-12-22 19:30:28 -0700110 free(dev_get_parent_plat(dev));
Simon Glass89ba6d52020-12-22 19:30:29 -0700111 dev_set_parent_plat(dev, NULL);
Simon Glasscdc133b2015-01-25 08:27:01 -0700112 }
Simon Glass3ac435d2014-11-10 17:16:47 -0700113 ret = uclass_unbind_device(dev);
114 if (ret)
Simon Glass8474da92020-03-28 14:03:47 -0600115 return log_msg_ret("uc", ret);
Simon Glass3ac435d2014-11-10 17:16:47 -0700116
117 if (dev->parent)
118 list_del(&dev->sibling_node);
Masahiro Yamada608f26c2015-07-25 21:52:35 +0900119
120 devres_release_all(dev);
121
Simon Glass73466df2020-12-19 10:40:10 -0700122 if (dev_get_flags(dev) & DM_FLAG_NAME_ALLOCED)
Simon Glassa2040fa2016-05-01 13:52:23 -0600123 free((char *)dev->name);
Simon Glass3ac435d2014-11-10 17:16:47 -0700124 free(dev);
125
126 return 0;
127}
128
129/**
130 * device_free() - Free memory buffers allocated by a device
131 * @dev: Device that is to be started
132 */
133void device_free(struct udevice *dev)
134{
135 int size;
136
Simon Glass41575d82020-12-03 16:55:17 -0700137 if (dev->driver->priv_auto) {
Simon Glass0fd3d912020-12-22 19:30:28 -0700138 free(dev_get_priv(dev));
139 dev_set_priv(dev, NULL);
Simon Glass3ac435d2014-11-10 17:16:47 -0700140 }
Simon Glass41575d82020-12-03 16:55:17 -0700141 size = dev->uclass->uc_drv->per_device_auto;
Simon Glass3ac435d2014-11-10 17:16:47 -0700142 if (size) {
Simon Glass0fd3d912020-12-22 19:30:28 -0700143 free(dev_get_uclass_priv(dev));
Simon Glass89ba6d52020-12-22 19:30:29 -0700144 dev_set_uclass_priv(dev, NULL);
Simon Glass3ac435d2014-11-10 17:16:47 -0700145 }
146 if (dev->parent) {
Simon Glass41575d82020-12-03 16:55:17 -0700147 size = dev->parent->driver->per_child_auto;
Simon Glass80443182021-10-23 17:26:05 -0600148 if (!size)
149 size = dev->parent->uclass->uc_drv->per_child_auto;
Simon Glass3ac435d2014-11-10 17:16:47 -0700150 if (size) {
Simon Glass0fd3d912020-12-22 19:30:28 -0700151 free(dev_get_parent_priv(dev));
Simon Glass89ba6d52020-12-22 19:30:29 -0700152 dev_set_parent_priv(dev, NULL);
Simon Glass3ac435d2014-11-10 17:16:47 -0700153 }
154 }
Simon Glass73466df2020-12-19 10:40:10 -0700155 dev_bic_flags(dev, DM_FLAG_PLATDATA_VALID);
Masahiro Yamada608f26c2015-07-25 21:52:35 +0900156
157 devres_release_probe(dev);
Simon Glass3ac435d2014-11-10 17:16:47 -0700158}
159
Simon Glassc51d2e72021-01-24 14:32:45 -0700160/**
161 * flags_remove() - Figure out whether to remove a device
162 *
Marek Vasutcc6f4c82021-01-24 14:32:46 -0700163 * If this is called with @flags == DM_REMOVE_NON_VITAL | DM_REMOVE_ACTIVE_DMA,
164 * then it returns 0 (=go head and remove) if the device is not matked vital
165 * but is marked DM_REMOVE_ACTIVE_DMA.
166 *
167 * If this is called with @flags == DM_REMOVE_ACTIVE_DMA,
168 * then it returns 0 (=go head and remove) if the device is marked
169 * DM_REMOVE_ACTIVE_DMA, regardless of whether it is marked vital.
170 *
Simon Glassc51d2e72021-01-24 14:32:45 -0700171 * @flags: Flags passed to device_remove()
172 * @drv_flags: Driver flags
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100173 * Return: 0 if the device should be removed,
Simon Glassc51d2e72021-01-24 14:32:45 -0700174 * -EKEYREJECTED if @flags includes a flag in DM_REMOVE_ACTIVE_ALL but
175 * @drv_flags does not (indicates that this device has nothing to do for
176 * DMA shutdown or OS prepare)
Marek Vasutcc6f4c82021-01-24 14:32:46 -0700177 * -EPROBE_DEFER if @flags is DM_REMOVE_NON_VITAL but @drv_flags contains
178 * DM_FLAG_VITAL (indicates the device is vital and should not be removed)
Simon Glassc51d2e72021-01-24 14:32:45 -0700179 */
180static int flags_remove(uint flags, uint drv_flags)
Stefan Roese426f99f2017-04-24 09:48:02 +0200181{
Marek Vasutcc6f4c82021-01-24 14:32:46 -0700182 if (!(flags & DM_REMOVE_NORMAL)) {
183 bool vital_match;
184 bool active_match;
Stefan Roese426f99f2017-04-24 09:48:02 +0200185
Marek Vasutcc6f4c82021-01-24 14:32:46 -0700186 active_match = !(flags & DM_REMOVE_ACTIVE_ALL) ||
187 (drv_flags & flags);
188 vital_match = !(flags & DM_REMOVE_NON_VITAL) ||
189 !(drv_flags & DM_FLAG_VITAL);
190 if (!vital_match)
191 return -EPROBE_DEFER;
192 if (!active_match)
193 return -EKEYREJECTED;
194 }
195
196 return 0;
Stefan Roese426f99f2017-04-24 09:48:02 +0200197}
198
Stefan Roese706865a2017-03-20 12:51:48 +0100199int device_remove(struct udevice *dev, uint flags)
Simon Glass3ac435d2014-11-10 17:16:47 -0700200{
Simon Glass34792532015-03-25 12:21:54 -0600201 const struct driver *drv;
Simon Glass3ac435d2014-11-10 17:16:47 -0700202 int ret;
203
204 if (!dev)
205 return -EINVAL;
206
Simon Glass73466df2020-12-19 10:40:10 -0700207 if (!(dev_get_flags(dev) & DM_FLAG_ACTIVATED))
Simon Glass3ac435d2014-11-10 17:16:47 -0700208 return 0;
209
Simon Glass5b896ed2022-03-04 08:43:03 -0700210 ret = device_notify(dev, EVT_DM_PRE_REMOVE);
211 if (ret)
212 return ret;
213
Simon Glassc51d2e72021-01-24 14:32:45 -0700214 /*
215 * If the child returns EKEYREJECTED, continue. It just means that it
216 * didn't match the flags.
217 */
Simon Glassb1f25fc2021-01-24 14:32:44 -0700218 ret = device_chld_remove(dev, NULL, flags);
Simon Glassc51d2e72021-01-24 14:32:45 -0700219 if (ret && ret != -EKEYREJECTED)
Simon Glassb1f25fc2021-01-24 14:32:44 -0700220 return ret;
Simon Glass3ac435d2014-11-10 17:16:47 -0700221
Stefan Roesebc85aa42017-03-27 10:58:53 +0200222 /*
223 * Remove the device if called with the "normal" remove flag set,
224 * or if the remove flag matches any of the drivers remove flags
225 */
Simon Glassc51d2e72021-01-24 14:32:45 -0700226 drv = dev->driver;
227 assert(drv);
228 ret = flags_remove(flags, drv->flags);
229 if (ret) {
230 log_debug("%s: When removing: flags=%x, drv->flags=%x, err=%d\n",
231 dev->name, flags, drv->flags, ret);
232 return ret;
233 }
234
235 ret = uclass_pre_remove_device(dev);
236 if (ret)
237 return ret;
238
239 if (drv->remove) {
Simon Glass3ac435d2014-11-10 17:16:47 -0700240 ret = drv->remove(dev);
241 if (ret)
242 goto err_remove;
243 }
244
245 if (dev->parent && dev->parent->driver->child_post_remove) {
246 ret = dev->parent->driver->child_post_remove(dev);
247 if (ret) {
248 dm_warn("%s: Device '%s' failed child_post_remove()",
249 __func__, dev->name);
250 }
251 }
252
Simon Glassced10802020-03-28 14:03:48 -0600253 if (!(flags & DM_REMOVE_NO_PD) &&
254 !(drv->flags &
Simon Glassa547fcb2021-01-24 14:32:42 -0700255 (DM_FLAG_DEFAULT_PD_CTRL_OFF | DM_FLAG_LEAVE_PD_ON)) &&
Anatolij Gustschin5349e252020-02-17 12:36:43 +0100256 dev != gd->cur_serial_dev)
Anatolij Gustschin52edfed2019-09-27 13:48:15 +0530257 dev_power_domain_off(dev);
258
Simon Glassc51d2e72021-01-24 14:32:45 -0700259 device_free(dev);
Simon Glass3ac435d2014-11-10 17:16:47 -0700260
Simon Glassc51d2e72021-01-24 14:32:45 -0700261 dev_bic_flags(dev, DM_FLAG_ACTIVATED);
Simon Glass3ac435d2014-11-10 17:16:47 -0700262
Simon Glass5b896ed2022-03-04 08:43:03 -0700263 ret = device_notify(dev, EVT_DM_POST_REMOVE);
264 if (ret)
265 goto err_remove;
266
Simon Glassc51d2e72021-01-24 14:32:45 -0700267 return 0;
Simon Glass3ac435d2014-11-10 17:16:47 -0700268
269err_remove:
270 /* We can't put the children back */
271 dm_warn("%s: Device '%s' failed to remove, but children are gone\n",
272 __func__, dev->name);
Simon Glass3ac435d2014-11-10 17:16:47 -0700273
274 return ret;
275}