Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Device manager |
| 4 | * |
| 5 | * Copyright (c) 2014 Google, Inc |
| 6 | * |
| 7 | * (C) Copyright 2012 |
| 8 | * Pavel Herrmann <morpheus.ibis@gmail.com> |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
Simon Glass | df34d0b | 2021-01-24 14:32:45 -0700 | [diff] [blame] | 11 | #define LOG_CATEGORY LOGC_DM |
| 12 | |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 13 | #include <errno.h> |
Simon Glass | 5f3c9e0 | 2020-03-28 14:03:47 -0600 | [diff] [blame] | 14 | #include <log.h> |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 15 | #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 Gustschin | 98381da | 2019-09-27 13:48:15 +0530 | [diff] [blame] | 21 | #include <power-domain.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 22 | #include <asm/global_data.h> |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 23 | |
Jean-Jacques Hiblot | 2c556a1 | 2018-08-09 16:17:45 +0200 | [diff] [blame] | 24 | int device_chld_unbind(struct udevice *dev, struct driver *drv) |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 25 | { |
| 26 | struct udevice *pos, *n; |
| 27 | int ret, saved_ret = 0; |
| 28 | |
| 29 | assert(dev); |
| 30 | |
Sean Anderson | f69a330 | 2022-03-30 12:26:40 -0400 | [diff] [blame] | 31 | device_foreach_child_safe(pos, n, dev) { |
Jean-Jacques Hiblot | 2c556a1 | 2018-08-09 16:17:45 +0200 | [diff] [blame] | 32 | if (drv && (pos->driver != drv)) |
| 33 | continue; |
| 34 | |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 35 | ret = device_unbind(pos); |
Simon Glass | 5f3c9e0 | 2020-03-28 14:03:47 -0600 | [diff] [blame] | 36 | if (ret && !saved_ret) { |
| 37 | log_warning("device '%s' failed to unbind\n", |
| 38 | pos->name); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 39 | saved_ret = ret; |
Simon Glass | 5f3c9e0 | 2020-03-28 14:03:47 -0600 | [diff] [blame] | 40 | } |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Simon Glass | 5f3c9e0 | 2020-03-28 14:03:47 -0600 | [diff] [blame] | 43 | return log_ret(saved_ret); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Jean-Jacques Hiblot | 2c556a1 | 2018-08-09 16:17:45 +0200 | [diff] [blame] | 46 | int device_chld_remove(struct udevice *dev, struct driver *drv, |
| 47 | uint flags) |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 48 | { |
| 49 | struct udevice *pos, *n; |
Marek Vasut | abbdbbd | 2021-01-24 14:32:46 -0700 | [diff] [blame] | 50 | int result = 0; |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 51 | |
| 52 | assert(dev); |
| 53 | |
Sean Anderson | f69a330 | 2022-03-30 12:26:40 -0400 | [diff] [blame] | 54 | device_foreach_child_safe(pos, n, dev) { |
Marek Vasut | abbdbbd | 2021-01-24 14:32:46 -0700 | [diff] [blame] | 55 | int ret; |
| 56 | |
Jean-Jacques Hiblot | 2c556a1 | 2018-08-09 16:17:45 +0200 | [diff] [blame] | 57 | if (drv && (pos->driver != drv)) |
| 58 | continue; |
| 59 | |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 60 | ret = device_remove(pos, flags); |
Marek Vasut | abbdbbd | 2021-01-24 14:32:46 -0700 | [diff] [blame] | 61 | if (ret == -EPROBE_DEFER) |
| 62 | result = ret; |
| 63 | else if (ret && ret != -EKEYREJECTED) |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 64 | return ret; |
| 65 | } |
| 66 | |
Marek Vasut | abbdbbd | 2021-01-24 14:32:46 -0700 | [diff] [blame] | 67 | return result; |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | int device_unbind(struct udevice *dev) |
| 71 | { |
Simon Glass | a626dff | 2015-03-25 12:21:54 -0600 | [diff] [blame] | 72 | const struct driver *drv; |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 73 | int ret; |
| 74 | |
| 75 | if (!dev) |
Simon Glass | 5f3c9e0 | 2020-03-28 14:03:47 -0600 | [diff] [blame] | 76 | return log_msg_ret("dev", -EINVAL); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 77 | |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 78 | if (dev_get_flags(dev) & DM_FLAG_ACTIVATED) |
Simon Glass | 5f3c9e0 | 2020-03-28 14:03:47 -0600 | [diff] [blame] | 79 | return log_msg_ret("active", -EINVAL); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 80 | |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 81 | if (!(dev_get_flags(dev) & DM_FLAG_BOUND)) |
Simon Glass | 5f3c9e0 | 2020-03-28 14:03:47 -0600 | [diff] [blame] | 82 | return log_msg_ret("not-bound", -EINVAL); |
Masahiro Yamada | bdbb5dd | 2015-07-25 21:52:34 +0900 | [diff] [blame] | 83 | |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 84 | drv = dev->driver; |
| 85 | assert(drv); |
| 86 | |
| 87 | if (drv->unbind) { |
| 88 | ret = drv->unbind(dev); |
| 89 | if (ret) |
Simon Glass | 5f3c9e0 | 2020-03-28 14:03:47 -0600 | [diff] [blame] | 90 | return log_msg_ret("unbind", ret); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Jean-Jacques Hiblot | 2c556a1 | 2018-08-09 16:17:45 +0200 | [diff] [blame] | 93 | ret = device_chld_unbind(dev, NULL); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 94 | if (ret) |
Simon Glass | 5f3c9e0 | 2020-03-28 14:03:47 -0600 | [diff] [blame] | 95 | return log_msg_ret("child unbind", ret); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 96 | |
Simon Glass | ce15bfb | 2021-10-23 17:26:05 -0600 | [diff] [blame] | 97 | ret = uclass_pre_unbind_device(dev); |
| 98 | if (ret) |
| 99 | return log_msg_ret("uc", ret); |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 100 | if (dev_get_flags(dev) & DM_FLAG_ALLOC_PDATA) { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 101 | free(dev_get_plat(dev)); |
| 102 | dev_set_plat(dev, NULL); |
Simon Glass | ba75049 | 2015-01-25 08:27:00 -0700 | [diff] [blame] | 103 | } |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 104 | if (dev_get_flags(dev) & DM_FLAG_ALLOC_UCLASS_PDATA) { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 105 | free(dev_get_uclass_plat(dev)); |
Simon Glass | 2543b16 | 2020-12-22 19:30:29 -0700 | [diff] [blame] | 106 | dev_set_uclass_plat(dev, NULL); |
Przemyslaw Marczak | d850e67 | 2015-04-15 13:07:18 +0200 | [diff] [blame] | 107 | } |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 108 | if (dev_get_flags(dev) & DM_FLAG_ALLOC_PARENT_PDATA) { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 109 | free(dev_get_parent_plat(dev)); |
Simon Glass | 2543b16 | 2020-12-22 19:30:29 -0700 | [diff] [blame] | 110 | dev_set_parent_plat(dev, NULL); |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 111 | } |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 112 | ret = uclass_unbind_device(dev); |
| 113 | if (ret) |
Simon Glass | 5f3c9e0 | 2020-03-28 14:03:47 -0600 | [diff] [blame] | 114 | return log_msg_ret("uc", ret); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 115 | |
| 116 | if (dev->parent) |
| 117 | list_del(&dev->sibling_node); |
Masahiro Yamada | 8b15b16 | 2015-07-25 21:52:35 +0900 | [diff] [blame] | 118 | |
| 119 | devres_release_all(dev); |
| 120 | |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 121 | if (dev_get_flags(dev) & DM_FLAG_NAME_ALLOCED) |
Simon Glass | 7760ba2 | 2016-05-01 13:52:23 -0600 | [diff] [blame] | 122 | free((char *)dev->name); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 123 | 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 | */ |
| 132 | void device_free(struct udevice *dev) |
| 133 | { |
| 134 | int size; |
| 135 | |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 136 | if (dev->driver->priv_auto) { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 137 | free(dev_get_priv(dev)); |
| 138 | dev_set_priv(dev, NULL); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 139 | } |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 140 | size = dev->uclass->uc_drv->per_device_auto; |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 141 | if (size) { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 142 | free(dev_get_uclass_priv(dev)); |
Simon Glass | 2543b16 | 2020-12-22 19:30:29 -0700 | [diff] [blame] | 143 | dev_set_uclass_priv(dev, NULL); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 144 | } |
| 145 | if (dev->parent) { |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 146 | size = dev->parent->driver->per_child_auto; |
Simon Glass | ce15bfb | 2021-10-23 17:26:05 -0600 | [diff] [blame] | 147 | if (!size) |
| 148 | size = dev->parent->uclass->uc_drv->per_child_auto; |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 149 | if (size) { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 150 | free(dev_get_parent_priv(dev)); |
Simon Glass | 2543b16 | 2020-12-22 19:30:29 -0700 | [diff] [blame] | 151 | dev_set_parent_priv(dev, NULL); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 152 | } |
| 153 | } |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 154 | dev_bic_flags(dev, DM_FLAG_PLATDATA_VALID); |
Masahiro Yamada | 8b15b16 | 2015-07-25 21:52:35 +0900 | [diff] [blame] | 155 | |
| 156 | devres_release_probe(dev); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Simon Glass | df34d0b | 2021-01-24 14:32:45 -0700 | [diff] [blame] | 159 | /** |
| 160 | * flags_remove() - Figure out whether to remove a device |
| 161 | * |
Marek Vasut | abbdbbd | 2021-01-24 14:32:46 -0700 | [diff] [blame] | 162 | * 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 Glass | df34d0b | 2021-01-24 14:32:45 -0700 | [diff] [blame] | 170 | * @flags: Flags passed to device_remove() |
| 171 | * @drv_flags: Driver flags |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 172 | * Return: 0 if the device should be removed, |
Simon Glass | df34d0b | 2021-01-24 14:32:45 -0700 | [diff] [blame] | 173 | * -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 Vasut | abbdbbd | 2021-01-24 14:32:46 -0700 | [diff] [blame] | 176 | * -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 Glass | df34d0b | 2021-01-24 14:32:45 -0700 | [diff] [blame] | 178 | */ |
| 179 | static int flags_remove(uint flags, uint drv_flags) |
Stefan Roese | 0745676 | 2017-04-24 09:48:02 +0200 | [diff] [blame] | 180 | { |
Marek Vasut | abbdbbd | 2021-01-24 14:32:46 -0700 | [diff] [blame] | 181 | 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 Roese | 0745676 | 2017-04-24 09:48:02 +0200 | [diff] [blame] | 194 | |
Marek Vasut | abbdbbd | 2021-01-24 14:32:46 -0700 | [diff] [blame] | 195 | return 0; |
Stefan Roese | 0745676 | 2017-04-24 09:48:02 +0200 | [diff] [blame] | 196 | } |
| 197 | |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 198 | int device_remove(struct udevice *dev, uint flags) |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 199 | { |
Simon Glass | a626dff | 2015-03-25 12:21:54 -0600 | [diff] [blame] | 200 | const struct driver *drv; |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 201 | int ret; |
| 202 | |
| 203 | if (!dev) |
| 204 | return -EINVAL; |
| 205 | |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 206 | if (!(dev_get_flags(dev) & DM_FLAG_ACTIVATED)) |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 207 | return 0; |
| 208 | |
Simon Glass | 3ee337a | 2022-03-04 08:43:03 -0700 | [diff] [blame] | 209 | ret = device_notify(dev, EVT_DM_PRE_REMOVE); |
| 210 | if (ret) |
| 211 | return ret; |
| 212 | |
Simon Glass | df34d0b | 2021-01-24 14:32:45 -0700 | [diff] [blame] | 213 | /* |
| 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 Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 225 | drv = dev->driver; |
| 226 | assert(drv); |
Simon Glass | df34d0b | 2021-01-24 14:32:45 -0700 | [diff] [blame] | 227 | 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 Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 231 | return ret; |
Simon Glass | df34d0b | 2021-01-24 14:32:45 -0700 | [diff] [blame] | 232 | } |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 233 | |
Simon Glass | 6dd0d1d | 2021-01-24 14:32:44 -0700 | [diff] [blame] | 234 | ret = uclass_pre_remove_device(dev); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 235 | if (ret) |
Simon Glass | 6dd0d1d | 2021-01-24 14:32:44 -0700 | [diff] [blame] | 236 | return ret; |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 237 | |
Simon Glass | df34d0b | 2021-01-24 14:32:45 -0700 | [diff] [blame] | 238 | if (drv->remove) { |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 239 | 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 Glass | 42697f5 | 2020-03-28 14:03:48 -0600 | [diff] [blame] | 252 | if (!(flags & DM_REMOVE_NO_PD) && |
| 253 | !(drv->flags & |
Simon Glass | 1281065 | 2021-01-24 14:32:42 -0700 | [diff] [blame] | 254 | (DM_FLAG_DEFAULT_PD_CTRL_OFF | DM_FLAG_LEAVE_PD_ON)) && |
Anatolij Gustschin | cfb28f1 | 2020-02-17 12:36:43 +0100 | [diff] [blame] | 255 | dev != gd->cur_serial_dev) |
Anatolij Gustschin | 98381da | 2019-09-27 13:48:15 +0530 | [diff] [blame] | 256 | dev_power_domain_off(dev); |
| 257 | |
Simon Glass | df34d0b | 2021-01-24 14:32:45 -0700 | [diff] [blame] | 258 | device_free(dev); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 259 | |
Simon Glass | df34d0b | 2021-01-24 14:32:45 -0700 | [diff] [blame] | 260 | dev_bic_flags(dev, DM_FLAG_ACTIVATED); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 261 | |
Simon Glass | 3ee337a | 2022-03-04 08:43:03 -0700 | [diff] [blame] | 262 | ret = device_notify(dev, EVT_DM_POST_REMOVE); |
| 263 | if (ret) |
| 264 | goto err_remove; |
| 265 | |
Simon Glass | df34d0b | 2021-01-24 14:32:45 -0700 | [diff] [blame] | 266 | return 0; |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 267 | |
| 268 | err_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 Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 272 | |
| 273 | return ret; |
| 274 | } |