blob: 437080ed778099bfd4eb71a2ff58ad8206b2192d [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassb38ea5a2014-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 Glassb38ea5a2014-11-10 17:16:47 -07009 */
10
Simon Glassdf34d0b2021-01-24 14:32:45 -070011#define LOG_CATEGORY LOGC_DM
12
Simon Glassb38ea5a2014-11-10 17:16:47 -070013#include <errno.h>
Simon Glass5f3c9e02020-03-28 14:03:47 -060014#include <log.h>
Simon Glassb38ea5a2014-11-10 17:16:47 -070015#include <malloc.h>
16#include <dm/device.h>
17#include <dm/device-internal.h>
18#include <dm/uclass.h>
19#include <dm/uclass-internal.h>
20#include <dm/util.h>
Anatolij Gustschin98381da2019-09-27 13:48:15 +053021#include <power-domain.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060022#include <asm/global_data.h>
Simon Glassb38ea5a2014-11-10 17:16:47 -070023
Jean-Jacques Hiblot2c556a12018-08-09 16:17:45 +020024int device_chld_unbind(struct udevice *dev, struct driver *drv)
Simon Glassb38ea5a2014-11-10 17:16:47 -070025{
26 struct udevice *pos, *n;
27 int ret, saved_ret = 0;
28
29 assert(dev);
30
Sean Andersonf69a3302022-03-30 12:26:40 -040031 device_foreach_child_safe(pos, n, dev) {
Jean-Jacques Hiblot2c556a12018-08-09 16:17:45 +020032 if (drv && (pos->driver != drv))
33 continue;
34
Simon Glassb38ea5a2014-11-10 17:16:47 -070035 ret = device_unbind(pos);
Simon Glass5f3c9e02020-03-28 14:03:47 -060036 if (ret && !saved_ret) {
37 log_warning("device '%s' failed to unbind\n",
38 pos->name);
Simon Glassb38ea5a2014-11-10 17:16:47 -070039 saved_ret = ret;
Simon Glass5f3c9e02020-03-28 14:03:47 -060040 }
Simon Glassb38ea5a2014-11-10 17:16:47 -070041 }
42
Simon Glass5f3c9e02020-03-28 14:03:47 -060043 return log_ret(saved_ret);
Simon Glassb38ea5a2014-11-10 17:16:47 -070044}
45
Jean-Jacques Hiblot2c556a12018-08-09 16:17:45 +020046int device_chld_remove(struct udevice *dev, struct driver *drv,
47 uint flags)
Simon Glassb38ea5a2014-11-10 17:16:47 -070048{
49 struct udevice *pos, *n;
Marek Vasutabbdbbd2021-01-24 14:32:46 -070050 int result = 0;
Simon Glassb38ea5a2014-11-10 17:16:47 -070051
52 assert(dev);
53
Sean Andersonf69a3302022-03-30 12:26:40 -040054 device_foreach_child_safe(pos, n, dev) {
Marek Vasutabbdbbd2021-01-24 14:32:46 -070055 int ret;
56
Jean-Jacques Hiblot2c556a12018-08-09 16:17:45 +020057 if (drv && (pos->driver != drv))
58 continue;
59
Stefan Roese80b5bc92017-03-20 12:51:48 +010060 ret = device_remove(pos, flags);
Marek Vasutabbdbbd2021-01-24 14:32:46 -070061 if (ret == -EPROBE_DEFER)
62 result = ret;
63 else if (ret && ret != -EKEYREJECTED)
Simon Glassb38ea5a2014-11-10 17:16:47 -070064 return ret;
65 }
66
Marek Vasutabbdbbd2021-01-24 14:32:46 -070067 return result;
Simon Glassb38ea5a2014-11-10 17:16:47 -070068}
69
70int device_unbind(struct udevice *dev)
71{
Simon Glassa626dff2015-03-25 12:21:54 -060072 const struct driver *drv;
Simon Glassb38ea5a2014-11-10 17:16:47 -070073 int ret;
74
75 if (!dev)
Simon Glass5f3c9e02020-03-28 14:03:47 -060076 return log_msg_ret("dev", -EINVAL);
Simon Glassb38ea5a2014-11-10 17:16:47 -070077
Simon Glass6211d762020-12-19 10:40:10 -070078 if (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
Simon Glass5f3c9e02020-03-28 14:03:47 -060079 return log_msg_ret("active", -EINVAL);
Simon Glassb38ea5a2014-11-10 17:16:47 -070080
Simon Glass6211d762020-12-19 10:40:10 -070081 if (!(dev_get_flags(dev) & DM_FLAG_BOUND))
Simon Glass5f3c9e02020-03-28 14:03:47 -060082 return log_msg_ret("not-bound", -EINVAL);
Masahiro Yamadabdbb5dd2015-07-25 21:52:34 +090083
Simon Glassb38ea5a2014-11-10 17:16:47 -070084 drv = dev->driver;
85 assert(drv);
86
87 if (drv->unbind) {
88 ret = drv->unbind(dev);
89 if (ret)
Simon Glass5f3c9e02020-03-28 14:03:47 -060090 return log_msg_ret("unbind", ret);
Simon Glassb38ea5a2014-11-10 17:16:47 -070091 }
92
Jean-Jacques Hiblot2c556a12018-08-09 16:17:45 +020093 ret = device_chld_unbind(dev, NULL);
Simon Glassb38ea5a2014-11-10 17:16:47 -070094 if (ret)
Simon Glass5f3c9e02020-03-28 14:03:47 -060095 return log_msg_ret("child unbind", ret);
Simon Glassb38ea5a2014-11-10 17:16:47 -070096
Simon Glassce15bfb2021-10-23 17:26:05 -060097 ret = uclass_pre_unbind_device(dev);
98 if (ret)
99 return log_msg_ret("uc", ret);
Simon Glass6211d762020-12-19 10:40:10 -0700100 if (dev_get_flags(dev) & DM_FLAG_ALLOC_PDATA) {
Simon Glass95588622020-12-22 19:30:28 -0700101 free(dev_get_plat(dev));
102 dev_set_plat(dev, NULL);
Simon Glassba750492015-01-25 08:27:00 -0700103 }
Simon Glass6211d762020-12-19 10:40:10 -0700104 if (dev_get_flags(dev) & DM_FLAG_ALLOC_UCLASS_PDATA) {
Simon Glass95588622020-12-22 19:30:28 -0700105 free(dev_get_uclass_plat(dev));
Simon Glass2543b162020-12-22 19:30:29 -0700106 dev_set_uclass_plat(dev, NULL);
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200107 }
Simon Glass6211d762020-12-19 10:40:10 -0700108 if (dev_get_flags(dev) & DM_FLAG_ALLOC_PARENT_PDATA) {
Simon Glass95588622020-12-22 19:30:28 -0700109 free(dev_get_parent_plat(dev));
Simon Glass2543b162020-12-22 19:30:29 -0700110 dev_set_parent_plat(dev, NULL);
Simon Glass11b61732015-01-25 08:27:01 -0700111 }
Simon Glassb38ea5a2014-11-10 17:16:47 -0700112 ret = uclass_unbind_device(dev);
113 if (ret)
Simon Glass5f3c9e02020-03-28 14:03:47 -0600114 return log_msg_ret("uc", ret);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700115
116 if (dev->parent)
117 list_del(&dev->sibling_node);
Masahiro Yamada8b15b162015-07-25 21:52:35 +0900118
119 devres_release_all(dev);
120
Simon Glass6211d762020-12-19 10:40:10 -0700121 if (dev_get_flags(dev) & DM_FLAG_NAME_ALLOCED)
Simon Glass7760ba22016-05-01 13:52:23 -0600122 free((char *)dev->name);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700123 free(dev);
124
125 return 0;
126}
127
128/**
129 * device_free() - Free memory buffers allocated by a device
130 * @dev: Device that is to be started
131 */
132void device_free(struct udevice *dev)
133{
134 int size;
135
Simon Glass8a2b47f2020-12-03 16:55:17 -0700136 if (dev->driver->priv_auto) {
Simon Glass95588622020-12-22 19:30:28 -0700137 free(dev_get_priv(dev));
138 dev_set_priv(dev, NULL);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700139 }
Simon Glass8a2b47f2020-12-03 16:55:17 -0700140 size = dev->uclass->uc_drv->per_device_auto;
Simon Glassb38ea5a2014-11-10 17:16:47 -0700141 if (size) {
Simon Glass95588622020-12-22 19:30:28 -0700142 free(dev_get_uclass_priv(dev));
Simon Glass2543b162020-12-22 19:30:29 -0700143 dev_set_uclass_priv(dev, NULL);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700144 }
145 if (dev->parent) {
Simon Glass8a2b47f2020-12-03 16:55:17 -0700146 size = dev->parent->driver->per_child_auto;
Simon Glassce15bfb2021-10-23 17:26:05 -0600147 if (!size)
148 size = dev->parent->uclass->uc_drv->per_child_auto;
Simon Glassb38ea5a2014-11-10 17:16:47 -0700149 if (size) {
Simon Glass95588622020-12-22 19:30:28 -0700150 free(dev_get_parent_priv(dev));
Simon Glass2543b162020-12-22 19:30:29 -0700151 dev_set_parent_priv(dev, NULL);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700152 }
153 }
Simon Glass6211d762020-12-19 10:40:10 -0700154 dev_bic_flags(dev, DM_FLAG_PLATDATA_VALID);
Masahiro Yamada8b15b162015-07-25 21:52:35 +0900155
156 devres_release_probe(dev);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700157}
158
Simon Glassdf34d0b2021-01-24 14:32:45 -0700159/**
160 * flags_remove() - Figure out whether to remove a device
161 *
Marek Vasutabbdbbd2021-01-24 14:32:46 -0700162 * If this is called with @flags == DM_REMOVE_NON_VITAL | DM_REMOVE_ACTIVE_DMA,
163 * then it returns 0 (=go head and remove) if the device is not matked vital
164 * but is marked DM_REMOVE_ACTIVE_DMA.
165 *
166 * If this is called with @flags == DM_REMOVE_ACTIVE_DMA,
167 * then it returns 0 (=go head and remove) if the device is marked
168 * DM_REMOVE_ACTIVE_DMA, regardless of whether it is marked vital.
169 *
Simon Glassdf34d0b2021-01-24 14:32:45 -0700170 * @flags: Flags passed to device_remove()
171 * @drv_flags: Driver flags
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100172 * Return: 0 if the device should be removed,
Simon Glassdf34d0b2021-01-24 14:32:45 -0700173 * -EKEYREJECTED if @flags includes a flag in DM_REMOVE_ACTIVE_ALL but
174 * @drv_flags does not (indicates that this device has nothing to do for
175 * DMA shutdown or OS prepare)
Marek Vasutabbdbbd2021-01-24 14:32:46 -0700176 * -EPROBE_DEFER if @flags is DM_REMOVE_NON_VITAL but @drv_flags contains
177 * DM_FLAG_VITAL (indicates the device is vital and should not be removed)
Simon Glassdf34d0b2021-01-24 14:32:45 -0700178 */
179static int flags_remove(uint flags, uint drv_flags)
Stefan Roese07456762017-04-24 09:48:02 +0200180{
Marek Vasutabbdbbd2021-01-24 14:32:46 -0700181 if (!(flags & DM_REMOVE_NORMAL)) {
182 bool vital_match;
183 bool active_match;
184
185 active_match = !(flags & DM_REMOVE_ACTIVE_ALL) ||
186 (drv_flags & flags);
187 vital_match = !(flags & DM_REMOVE_NON_VITAL) ||
188 !(drv_flags & DM_FLAG_VITAL);
189 if (!vital_match)
190 return -EPROBE_DEFER;
191 if (!active_match)
192 return -EKEYREJECTED;
193 }
Stefan Roese07456762017-04-24 09:48:02 +0200194
Marek Vasutabbdbbd2021-01-24 14:32:46 -0700195 return 0;
Stefan Roese07456762017-04-24 09:48:02 +0200196}
197
Stefan Roese80b5bc92017-03-20 12:51:48 +0100198int device_remove(struct udevice *dev, uint flags)
Simon Glassb38ea5a2014-11-10 17:16:47 -0700199{
Simon Glassa626dff2015-03-25 12:21:54 -0600200 const struct driver *drv;
Simon Glassb38ea5a2014-11-10 17:16:47 -0700201 int ret;
202
203 if (!dev)
204 return -EINVAL;
205
Simon Glass6211d762020-12-19 10:40:10 -0700206 if (!(dev_get_flags(dev) & DM_FLAG_ACTIVATED))
Simon Glassb38ea5a2014-11-10 17:16:47 -0700207 return 0;
208
Simon Glass3ee337a2022-03-04 08:43:03 -0700209 ret = device_notify(dev, EVT_DM_PRE_REMOVE);
210 if (ret)
211 return ret;
212
Simon Glassdf34d0b2021-01-24 14:32:45 -0700213 /*
214 * If the child returns EKEYREJECTED, continue. It just means that it
215 * didn't match the flags.
216 */
217 ret = device_chld_remove(dev, NULL, flags);
218 if (ret && ret != -EKEYREJECTED)
219 return ret;
220
221 /*
222 * Remove the device if called with the "normal" remove flag set,
223 * or if the remove flag matches any of the drivers remove flags
224 */
Simon Glassb38ea5a2014-11-10 17:16:47 -0700225 drv = dev->driver;
226 assert(drv);
Simon Glassdf34d0b2021-01-24 14:32:45 -0700227 ret = flags_remove(flags, drv->flags);
228 if (ret) {
229 log_debug("%s: When removing: flags=%x, drv->flags=%x, err=%d\n",
230 dev->name, flags, drv->flags, ret);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700231 return ret;
Simon Glassdf34d0b2021-01-24 14:32:45 -0700232 }
Simon Glassb38ea5a2014-11-10 17:16:47 -0700233
Simon Glass6dd0d1d2021-01-24 14:32:44 -0700234 ret = uclass_pre_remove_device(dev);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700235 if (ret)
Simon Glass6dd0d1d2021-01-24 14:32:44 -0700236 return ret;
Simon Glassb38ea5a2014-11-10 17:16:47 -0700237
Simon Glassdf34d0b2021-01-24 14:32:45 -0700238 if (drv->remove) {
Simon Glassb38ea5a2014-11-10 17:16:47 -0700239 ret = drv->remove(dev);
240 if (ret)
241 goto err_remove;
242 }
243
244 if (dev->parent && dev->parent->driver->child_post_remove) {
245 ret = dev->parent->driver->child_post_remove(dev);
246 if (ret) {
247 dm_warn("%s: Device '%s' failed child_post_remove()",
248 __func__, dev->name);
249 }
250 }
251
Simon Glass42697f52020-03-28 14:03:48 -0600252 if (!(flags & DM_REMOVE_NO_PD) &&
253 !(drv->flags &
Simon Glass12810652021-01-24 14:32:42 -0700254 (DM_FLAG_DEFAULT_PD_CTRL_OFF | DM_FLAG_LEAVE_PD_ON)) &&
Anatolij Gustschincfb28f12020-02-17 12:36:43 +0100255 dev != gd->cur_serial_dev)
Anatolij Gustschin98381da2019-09-27 13:48:15 +0530256 dev_power_domain_off(dev);
257
Simon Glassdf34d0b2021-01-24 14:32:45 -0700258 device_free(dev);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700259
Simon Glassdf34d0b2021-01-24 14:32:45 -0700260 dev_bic_flags(dev, DM_FLAG_ACTIVATED);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700261
Simon Glass3ee337a2022-03-04 08:43:03 -0700262 ret = device_notify(dev, EVT_DM_POST_REMOVE);
263 if (ret)
264 goto err_remove;
265
Simon Glassdf34d0b2021-01-24 14:32:45 -0700266 return 0;
Simon Glassb38ea5a2014-11-10 17:16:47 -0700267
268err_remove:
269 /* We can't put the children back */
270 dm_warn("%s: Device '%s' failed to remove, but children are gone\n",
271 __func__, dev->name);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700272
273 return ret;
274}