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 <common.h> |
| 14 | #include <errno.h> |
Simon Glass | 5f3c9e0 | 2020-03-28 14:03:47 -0600 | [diff] [blame] | 15 | #include <log.h> |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 16 | #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 Gustschin | 98381da | 2019-09-27 13:48:15 +0530 | [diff] [blame] | 22 | #include <power-domain.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 23 | #include <asm/global_data.h> |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 24 | |
Jean-Jacques Hiblot | 2c556a1 | 2018-08-09 16:17:45 +0200 | [diff] [blame] | 25 | int device_chld_unbind(struct udevice *dev, struct driver *drv) |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 26 | { |
| 27 | struct udevice *pos, *n; |
| 28 | int ret, saved_ret = 0; |
| 29 | |
| 30 | assert(dev); |
| 31 | |
| 32 | list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) { |
Jean-Jacques Hiblot | 2c556a1 | 2018-08-09 16:17:45 +0200 | [diff] [blame] | 33 | if (drv && (pos->driver != drv)) |
| 34 | continue; |
| 35 | |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 36 | ret = device_unbind(pos); |
Simon Glass | 5f3c9e0 | 2020-03-28 14:03:47 -0600 | [diff] [blame] | 37 | if (ret && !saved_ret) { |
| 38 | log_warning("device '%s' failed to unbind\n", |
| 39 | pos->name); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 40 | saved_ret = ret; |
Simon Glass | 5f3c9e0 | 2020-03-28 14:03:47 -0600 | [diff] [blame] | 41 | } |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Simon Glass | 5f3c9e0 | 2020-03-28 14:03:47 -0600 | [diff] [blame] | 44 | return log_ret(saved_ret); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Jean-Jacques Hiblot | 2c556a1 | 2018-08-09 16:17:45 +0200 | [diff] [blame] | 47 | int device_chld_remove(struct udevice *dev, struct driver *drv, |
| 48 | uint flags) |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 49 | { |
| 50 | struct udevice *pos, *n; |
Marek Vasut | abbdbbd | 2021-01-24 14:32:46 -0700 | [diff] [blame] | 51 | int result = 0; |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 52 | |
| 53 | assert(dev); |
| 54 | |
| 55 | list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) { |
Marek Vasut | abbdbbd | 2021-01-24 14:32:46 -0700 | [diff] [blame] | 56 | int ret; |
| 57 | |
Jean-Jacques Hiblot | 2c556a1 | 2018-08-09 16:17:45 +0200 | [diff] [blame] | 58 | if (drv && (pos->driver != drv)) |
| 59 | continue; |
| 60 | |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 61 | ret = device_remove(pos, flags); |
Marek Vasut | abbdbbd | 2021-01-24 14:32:46 -0700 | [diff] [blame] | 62 | if (ret == -EPROBE_DEFER) |
| 63 | result = ret; |
| 64 | else if (ret && ret != -EKEYREJECTED) |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 65 | return ret; |
| 66 | } |
| 67 | |
Marek Vasut | abbdbbd | 2021-01-24 14:32:46 -0700 | [diff] [blame] | 68 | return result; |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | int device_unbind(struct udevice *dev) |
| 72 | { |
Simon Glass | a626dff | 2015-03-25 12:21:54 -0600 | [diff] [blame] | 73 | const struct driver *drv; |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 74 | int ret; |
| 75 | |
| 76 | if (!dev) |
Simon Glass | 5f3c9e0 | 2020-03-28 14:03:47 -0600 | [diff] [blame] | 77 | return log_msg_ret("dev", -EINVAL); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 78 | |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 79 | if (dev_get_flags(dev) & DM_FLAG_ACTIVATED) |
Simon Glass | 5f3c9e0 | 2020-03-28 14:03:47 -0600 | [diff] [blame] | 80 | return log_msg_ret("active", -EINVAL); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 81 | |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 82 | if (!(dev_get_flags(dev) & DM_FLAG_BOUND)) |
Simon Glass | 5f3c9e0 | 2020-03-28 14:03:47 -0600 | [diff] [blame] | 83 | return log_msg_ret("not-bound", -EINVAL); |
Masahiro Yamada | bdbb5dd | 2015-07-25 21:52:34 +0900 | [diff] [blame] | 84 | |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 85 | drv = dev->driver; |
| 86 | assert(drv); |
| 87 | |
| 88 | if (drv->unbind) { |
| 89 | ret = drv->unbind(dev); |
| 90 | if (ret) |
Simon Glass | 5f3c9e0 | 2020-03-28 14:03:47 -0600 | [diff] [blame] | 91 | return log_msg_ret("unbind", ret); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Jean-Jacques Hiblot | 2c556a1 | 2018-08-09 16:17:45 +0200 | [diff] [blame] | 94 | ret = device_chld_unbind(dev, NULL); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 95 | if (ret) |
Simon Glass | 5f3c9e0 | 2020-03-28 14:03:47 -0600 | [diff] [blame] | 96 | return log_msg_ret("child unbind", ret); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 97 | |
Simon Glass | ce15bfb | 2021-10-23 17:26:05 -0600 | [diff] [blame] | 98 | ret = uclass_pre_unbind_device(dev); |
| 99 | if (ret) |
| 100 | return log_msg_ret("uc", ret); |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 101 | if (dev_get_flags(dev) & DM_FLAG_ALLOC_PDATA) { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 102 | free(dev_get_plat(dev)); |
| 103 | dev_set_plat(dev, NULL); |
Simon Glass | ba75049 | 2015-01-25 08:27:00 -0700 | [diff] [blame] | 104 | } |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 105 | if (dev_get_flags(dev) & DM_FLAG_ALLOC_UCLASS_PDATA) { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 106 | free(dev_get_uclass_plat(dev)); |
Simon Glass | 2543b16 | 2020-12-22 19:30:29 -0700 | [diff] [blame] | 107 | dev_set_uclass_plat(dev, NULL); |
Przemyslaw Marczak | d850e67 | 2015-04-15 13:07:18 +0200 | [diff] [blame] | 108 | } |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 109 | if (dev_get_flags(dev) & DM_FLAG_ALLOC_PARENT_PDATA) { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 110 | free(dev_get_parent_plat(dev)); |
Simon Glass | 2543b16 | 2020-12-22 19:30:29 -0700 | [diff] [blame] | 111 | dev_set_parent_plat(dev, NULL); |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 112 | } |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 113 | ret = uclass_unbind_device(dev); |
| 114 | if (ret) |
Simon Glass | 5f3c9e0 | 2020-03-28 14:03:47 -0600 | [diff] [blame] | 115 | return log_msg_ret("uc", ret); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 116 | |
| 117 | if (dev->parent) |
| 118 | list_del(&dev->sibling_node); |
Masahiro Yamada | 8b15b16 | 2015-07-25 21:52:35 +0900 | [diff] [blame] | 119 | |
| 120 | devres_release_all(dev); |
| 121 | |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 122 | if (dev_get_flags(dev) & DM_FLAG_NAME_ALLOCED) |
Simon Glass | 7760ba2 | 2016-05-01 13:52:23 -0600 | [diff] [blame] | 123 | free((char *)dev->name); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 124 | 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 | */ |
| 133 | void device_free(struct udevice *dev) |
| 134 | { |
| 135 | int size; |
| 136 | |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 137 | if (dev->driver->priv_auto) { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 138 | free(dev_get_priv(dev)); |
| 139 | dev_set_priv(dev, NULL); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 140 | } |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 141 | size = dev->uclass->uc_drv->per_device_auto; |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 142 | if (size) { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 143 | free(dev_get_uclass_priv(dev)); |
Simon Glass | 2543b16 | 2020-12-22 19:30:29 -0700 | [diff] [blame] | 144 | dev_set_uclass_priv(dev, NULL); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 145 | } |
| 146 | if (dev->parent) { |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 147 | size = dev->parent->driver->per_child_auto; |
Simon Glass | ce15bfb | 2021-10-23 17:26:05 -0600 | [diff] [blame] | 148 | if (!size) |
| 149 | size = dev->parent->uclass->uc_drv->per_child_auto; |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 150 | if (size) { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 151 | free(dev_get_parent_priv(dev)); |
Simon Glass | 2543b16 | 2020-12-22 19:30:29 -0700 | [diff] [blame] | 152 | dev_set_parent_priv(dev, NULL); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 153 | } |
| 154 | } |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 155 | dev_bic_flags(dev, DM_FLAG_PLATDATA_VALID); |
Masahiro Yamada | 8b15b16 | 2015-07-25 21:52:35 +0900 | [diff] [blame] | 156 | |
| 157 | devres_release_probe(dev); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Simon Glass | df34d0b | 2021-01-24 14:32:45 -0700 | [diff] [blame] | 160 | /** |
| 161 | * flags_remove() - Figure out whether to remove a device |
| 162 | * |
Marek Vasut | abbdbbd | 2021-01-24 14:32:46 -0700 | [diff] [blame] | 163 | * 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 Glass | df34d0b | 2021-01-24 14:32:45 -0700 | [diff] [blame] | 171 | * @flags: Flags passed to device_remove() |
| 172 | * @drv_flags: Driver flags |
| 173 | * @return 0 if the device should be removed, |
| 174 | * -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 Vasut | abbdbbd | 2021-01-24 14:32:46 -0700 | [diff] [blame] | 177 | * -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 Glass | df34d0b | 2021-01-24 14:32:45 -0700 | [diff] [blame] | 179 | */ |
| 180 | static int flags_remove(uint flags, uint drv_flags) |
Stefan Roese | 0745676 | 2017-04-24 09:48:02 +0200 | [diff] [blame] | 181 | { |
Marek Vasut | abbdbbd | 2021-01-24 14:32:46 -0700 | [diff] [blame] | 182 | if (!(flags & DM_REMOVE_NORMAL)) { |
| 183 | bool vital_match; |
| 184 | bool active_match; |
| 185 | |
| 186 | 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 | } |
Stefan Roese | 0745676 | 2017-04-24 09:48:02 +0200 | [diff] [blame] | 195 | |
Marek Vasut | abbdbbd | 2021-01-24 14:32:46 -0700 | [diff] [blame] | 196 | return 0; |
Stefan Roese | 0745676 | 2017-04-24 09:48:02 +0200 | [diff] [blame] | 197 | } |
| 198 | |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 199 | int device_remove(struct udevice *dev, uint flags) |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 200 | { |
Simon Glass | a626dff | 2015-03-25 12:21:54 -0600 | [diff] [blame] | 201 | const struct driver *drv; |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 202 | int ret; |
| 203 | |
| 204 | if (!dev) |
| 205 | return -EINVAL; |
| 206 | |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 207 | if (!(dev_get_flags(dev) & DM_FLAG_ACTIVATED)) |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 208 | return 0; |
| 209 | |
Simon Glass | df34d0b | 2021-01-24 14:32:45 -0700 | [diff] [blame] | 210 | /* |
| 211 | * If the child returns EKEYREJECTED, continue. It just means that it |
| 212 | * didn't match the flags. |
| 213 | */ |
| 214 | ret = device_chld_remove(dev, NULL, flags); |
| 215 | if (ret && ret != -EKEYREJECTED) |
| 216 | return ret; |
| 217 | |
| 218 | /* |
| 219 | * Remove the device if called with the "normal" remove flag set, |
| 220 | * or if the remove flag matches any of the drivers remove flags |
| 221 | */ |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 222 | drv = dev->driver; |
| 223 | assert(drv); |
Simon Glass | df34d0b | 2021-01-24 14:32:45 -0700 | [diff] [blame] | 224 | ret = flags_remove(flags, drv->flags); |
| 225 | if (ret) { |
| 226 | log_debug("%s: When removing: flags=%x, drv->flags=%x, err=%d\n", |
| 227 | dev->name, flags, drv->flags, ret); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 228 | return ret; |
Simon Glass | df34d0b | 2021-01-24 14:32:45 -0700 | [diff] [blame] | 229 | } |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 230 | |
Simon Glass | 6dd0d1d | 2021-01-24 14:32:44 -0700 | [diff] [blame] | 231 | ret = uclass_pre_remove_device(dev); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 232 | if (ret) |
Simon Glass | 6dd0d1d | 2021-01-24 14:32:44 -0700 | [diff] [blame] | 233 | return ret; |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 234 | |
Simon Glass | df34d0b | 2021-01-24 14:32:45 -0700 | [diff] [blame] | 235 | if (drv->remove) { |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 236 | ret = drv->remove(dev); |
| 237 | if (ret) |
| 238 | goto err_remove; |
| 239 | } |
| 240 | |
| 241 | if (dev->parent && dev->parent->driver->child_post_remove) { |
| 242 | ret = dev->parent->driver->child_post_remove(dev); |
| 243 | if (ret) { |
| 244 | dm_warn("%s: Device '%s' failed child_post_remove()", |
| 245 | __func__, dev->name); |
| 246 | } |
| 247 | } |
| 248 | |
Simon Glass | 42697f5 | 2020-03-28 14:03:48 -0600 | [diff] [blame] | 249 | if (!(flags & DM_REMOVE_NO_PD) && |
| 250 | !(drv->flags & |
Simon Glass | 1281065 | 2021-01-24 14:32:42 -0700 | [diff] [blame] | 251 | (DM_FLAG_DEFAULT_PD_CTRL_OFF | DM_FLAG_LEAVE_PD_ON)) && |
Anatolij Gustschin | cfb28f1 | 2020-02-17 12:36:43 +0100 | [diff] [blame] | 252 | dev != gd->cur_serial_dev) |
Anatolij Gustschin | 98381da | 2019-09-27 13:48:15 +0530 | [diff] [blame] | 253 | dev_power_domain_off(dev); |
| 254 | |
Simon Glass | df34d0b | 2021-01-24 14:32:45 -0700 | [diff] [blame] | 255 | device_free(dev); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 256 | |
Simon Glass | df34d0b | 2021-01-24 14:32:45 -0700 | [diff] [blame] | 257 | dev_bic_flags(dev, DM_FLAG_ACTIVATED); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 258 | |
Simon Glass | df34d0b | 2021-01-24 14:32:45 -0700 | [diff] [blame] | 259 | return 0; |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 260 | |
| 261 | err_remove: |
| 262 | /* We can't put the children back */ |
| 263 | dm_warn("%s: Device '%s' failed to remove, but children are gone\n", |
| 264 | __func__, dev->name); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 265 | |
| 266 | return ret; |
| 267 | } |