2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2014-11-11 00:16:47 +00:00
|
|
|
/*
|
|
|
|
* Device manager
|
|
|
|
*
|
|
|
|
* Copyright (c) 2014 Google, Inc
|
|
|
|
*
|
|
|
|
* (C) Copyright 2012
|
|
|
|
* Pavel Herrmann <morpheus.ibis@gmail.com>
|
|
|
|
*/
|
|
|
|
|
2021-01-24 21:32:45 +00:00
|
|
|
#define LOG_CATEGORY LOGC_DM
|
|
|
|
|
2014-11-11 00:16:47 +00:00
|
|
|
#include <common.h>
|
|
|
|
#include <errno.h>
|
2020-03-28 20:03:47 +00:00
|
|
|
#include <log.h>
|
2014-11-11 00:16:47 +00:00
|
|
|
#include <malloc.h>
|
|
|
|
#include <dm/device.h>
|
|
|
|
#include <dm/device-internal.h>
|
|
|
|
#include <dm/uclass.h>
|
|
|
|
#include <dm/uclass-internal.h>
|
|
|
|
#include <dm/util.h>
|
2019-09-27 08:18:15 +00:00
|
|
|
#include <power-domain.h>
|
2020-10-31 03:38:53 +00:00
|
|
|
#include <asm/global_data.h>
|
2014-11-11 00:16:47 +00:00
|
|
|
|
2018-08-09 14:17:45 +00:00
|
|
|
int device_chld_unbind(struct udevice *dev, struct driver *drv)
|
2014-11-11 00:16:47 +00:00
|
|
|
{
|
|
|
|
struct udevice *pos, *n;
|
|
|
|
int ret, saved_ret = 0;
|
|
|
|
|
|
|
|
assert(dev);
|
|
|
|
|
|
|
|
list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) {
|
2018-08-09 14:17:45 +00:00
|
|
|
if (drv && (pos->driver != drv))
|
|
|
|
continue;
|
|
|
|
|
2014-11-11 00:16:47 +00:00
|
|
|
ret = device_unbind(pos);
|
2020-03-28 20:03:47 +00:00
|
|
|
if (ret && !saved_ret) {
|
|
|
|
log_warning("device '%s' failed to unbind\n",
|
|
|
|
pos->name);
|
2014-11-11 00:16:47 +00:00
|
|
|
saved_ret = ret;
|
2020-03-28 20:03:47 +00:00
|
|
|
}
|
2014-11-11 00:16:47 +00:00
|
|
|
}
|
|
|
|
|
2020-03-28 20:03:47 +00:00
|
|
|
return log_ret(saved_ret);
|
2014-11-11 00:16:47 +00:00
|
|
|
}
|
|
|
|
|
2018-08-09 14:17:45 +00:00
|
|
|
int device_chld_remove(struct udevice *dev, struct driver *drv,
|
|
|
|
uint flags)
|
2014-11-11 00:16:47 +00:00
|
|
|
{
|
|
|
|
struct udevice *pos, *n;
|
2021-01-24 21:32:46 +00:00
|
|
|
int result = 0;
|
2014-11-11 00:16:47 +00:00
|
|
|
|
|
|
|
assert(dev);
|
|
|
|
|
|
|
|
list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) {
|
2021-01-24 21:32:46 +00:00
|
|
|
int ret;
|
|
|
|
|
2018-08-09 14:17:45 +00:00
|
|
|
if (drv && (pos->driver != drv))
|
|
|
|
continue;
|
|
|
|
|
2017-03-20 11:51:48 +00:00
|
|
|
ret = device_remove(pos, flags);
|
2021-01-24 21:32:46 +00:00
|
|
|
if (ret == -EPROBE_DEFER)
|
|
|
|
result = ret;
|
|
|
|
else if (ret && ret != -EKEYREJECTED)
|
2014-11-11 00:16:47 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-01-24 21:32:46 +00:00
|
|
|
return result;
|
2014-11-11 00:16:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int device_unbind(struct udevice *dev)
|
|
|
|
{
|
2015-03-25 18:21:54 +00:00
|
|
|
const struct driver *drv;
|
2014-11-11 00:16:47 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!dev)
|
2020-03-28 20:03:47 +00:00
|
|
|
return log_msg_ret("dev", -EINVAL);
|
2014-11-11 00:16:47 +00:00
|
|
|
|
2020-12-19 17:40:10 +00:00
|
|
|
if (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
|
2020-03-28 20:03:47 +00:00
|
|
|
return log_msg_ret("active", -EINVAL);
|
2014-11-11 00:16:47 +00:00
|
|
|
|
2020-12-19 17:40:10 +00:00
|
|
|
if (!(dev_get_flags(dev) & DM_FLAG_BOUND))
|
2020-03-28 20:03:47 +00:00
|
|
|
return log_msg_ret("not-bound", -EINVAL);
|
2015-07-25 12:52:34 +00:00
|
|
|
|
2014-11-11 00:16:47 +00:00
|
|
|
drv = dev->driver;
|
|
|
|
assert(drv);
|
|
|
|
|
|
|
|
if (drv->unbind) {
|
|
|
|
ret = drv->unbind(dev);
|
|
|
|
if (ret)
|
2020-03-28 20:03:47 +00:00
|
|
|
return log_msg_ret("unbind", ret);
|
2014-11-11 00:16:47 +00:00
|
|
|
}
|
|
|
|
|
2018-08-09 14:17:45 +00:00
|
|
|
ret = device_chld_unbind(dev, NULL);
|
2014-11-11 00:16:47 +00:00
|
|
|
if (ret)
|
2020-03-28 20:03:47 +00:00
|
|
|
return log_msg_ret("child unbind", ret);
|
2014-11-11 00:16:47 +00:00
|
|
|
|
2021-10-23 23:26:05 +00:00
|
|
|
ret = uclass_pre_unbind_device(dev);
|
|
|
|
if (ret)
|
|
|
|
return log_msg_ret("uc", ret);
|
2020-12-19 17:40:10 +00:00
|
|
|
if (dev_get_flags(dev) & DM_FLAG_ALLOC_PDATA) {
|
2020-12-23 02:30:28 +00:00
|
|
|
free(dev_get_plat(dev));
|
|
|
|
dev_set_plat(dev, NULL);
|
2015-01-25 15:27:00 +00:00
|
|
|
}
|
2020-12-19 17:40:10 +00:00
|
|
|
if (dev_get_flags(dev) & DM_FLAG_ALLOC_UCLASS_PDATA) {
|
2020-12-23 02:30:28 +00:00
|
|
|
free(dev_get_uclass_plat(dev));
|
2020-12-23 02:30:29 +00:00
|
|
|
dev_set_uclass_plat(dev, NULL);
|
2015-04-15 11:07:18 +00:00
|
|
|
}
|
2020-12-19 17:40:10 +00:00
|
|
|
if (dev_get_flags(dev) & DM_FLAG_ALLOC_PARENT_PDATA) {
|
2020-12-23 02:30:28 +00:00
|
|
|
free(dev_get_parent_plat(dev));
|
2020-12-23 02:30:29 +00:00
|
|
|
dev_set_parent_plat(dev, NULL);
|
2015-01-25 15:27:01 +00:00
|
|
|
}
|
2014-11-11 00:16:47 +00:00
|
|
|
ret = uclass_unbind_device(dev);
|
|
|
|
if (ret)
|
2020-03-28 20:03:47 +00:00
|
|
|
return log_msg_ret("uc", ret);
|
2014-11-11 00:16:47 +00:00
|
|
|
|
|
|
|
if (dev->parent)
|
|
|
|
list_del(&dev->sibling_node);
|
devres: introduce Devres (Managed Device Resource) framework
In U-Boot's driver model, memory is basically allocated and freed
in the core framework. So, low level drivers generally only have
to specify the size of needed memory with .priv_auto_alloc_size,
.platdata_auto_alloc_size, etc. Nevertheless, some drivers still
need to allocate/free memory on their own in case they cannot
statically know the necessary memory size. So, I believe it is
reasonable enough to port Devres into U-boot.
Devres, which originates in Linux, manages device resources for each
device and automatically releases them on driver detach. With devres,
device resources are guaranteed to be freed whether initialization
fails half-way or the device gets detached.
The basic idea is totally the same to that of Linux, but I tweaked
it a bit so that it fits in U-Boot's driver model.
In U-Boot, drivers are activated in two steps: binding and probing.
Binding puts a driver and a device together. It is just data
manipulation on the system memory, so nothing has happened on the
hardware device at this moment. When the device is really used, it
is probed. Probing initializes the real hardware device to make it
really ready for use.
So, the resources acquired during the probing process must be freed
when the device is removed. Likewise, what has been allocated in
binding should be released when the device is unbound. The struct
devres has a member "probe" to remember when the resource was
allocated.
CONFIG_DEBUG_DEVRES is also supported for easier debugging.
If enabled, debug messages are printed each time a resource is
allocated/freed.
Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Acked-by: Simon Glass <sjg@chromium.org>
2015-07-25 12:52:35 +00:00
|
|
|
|
|
|
|
devres_release_all(dev);
|
|
|
|
|
2020-12-19 17:40:10 +00:00
|
|
|
if (dev_get_flags(dev) & DM_FLAG_NAME_ALLOCED)
|
2016-05-01 19:52:23 +00:00
|
|
|
free((char *)dev->name);
|
2014-11-11 00:16:47 +00:00
|
|
|
free(dev);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* device_free() - Free memory buffers allocated by a device
|
|
|
|
* @dev: Device that is to be started
|
|
|
|
*/
|
|
|
|
void device_free(struct udevice *dev)
|
|
|
|
{
|
|
|
|
int size;
|
|
|
|
|
2020-12-03 23:55:17 +00:00
|
|
|
if (dev->driver->priv_auto) {
|
2020-12-23 02:30:28 +00:00
|
|
|
free(dev_get_priv(dev));
|
|
|
|
dev_set_priv(dev, NULL);
|
2014-11-11 00:16:47 +00:00
|
|
|
}
|
2020-12-03 23:55:17 +00:00
|
|
|
size = dev->uclass->uc_drv->per_device_auto;
|
2014-11-11 00:16:47 +00:00
|
|
|
if (size) {
|
2020-12-23 02:30:28 +00:00
|
|
|
free(dev_get_uclass_priv(dev));
|
2020-12-23 02:30:29 +00:00
|
|
|
dev_set_uclass_priv(dev, NULL);
|
2014-11-11 00:16:47 +00:00
|
|
|
}
|
|
|
|
if (dev->parent) {
|
2020-12-03 23:55:17 +00:00
|
|
|
size = dev->parent->driver->per_child_auto;
|
2021-10-23 23:26:05 +00:00
|
|
|
if (!size)
|
|
|
|
size = dev->parent->uclass->uc_drv->per_child_auto;
|
2014-11-11 00:16:47 +00:00
|
|
|
if (size) {
|
2020-12-23 02:30:28 +00:00
|
|
|
free(dev_get_parent_priv(dev));
|
2020-12-23 02:30:29 +00:00
|
|
|
dev_set_parent_priv(dev, NULL);
|
2014-11-11 00:16:47 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-19 17:40:10 +00:00
|
|
|
dev_bic_flags(dev, DM_FLAG_PLATDATA_VALID);
|
devres: introduce Devres (Managed Device Resource) framework
In U-Boot's driver model, memory is basically allocated and freed
in the core framework. So, low level drivers generally only have
to specify the size of needed memory with .priv_auto_alloc_size,
.platdata_auto_alloc_size, etc. Nevertheless, some drivers still
need to allocate/free memory on their own in case they cannot
statically know the necessary memory size. So, I believe it is
reasonable enough to port Devres into U-boot.
Devres, which originates in Linux, manages device resources for each
device and automatically releases them on driver detach. With devres,
device resources are guaranteed to be freed whether initialization
fails half-way or the device gets detached.
The basic idea is totally the same to that of Linux, but I tweaked
it a bit so that it fits in U-Boot's driver model.
In U-Boot, drivers are activated in two steps: binding and probing.
Binding puts a driver and a device together. It is just data
manipulation on the system memory, so nothing has happened on the
hardware device at this moment. When the device is really used, it
is probed. Probing initializes the real hardware device to make it
really ready for use.
So, the resources acquired during the probing process must be freed
when the device is removed. Likewise, what has been allocated in
binding should be released when the device is unbound. The struct
devres has a member "probe" to remember when the resource was
allocated.
CONFIG_DEBUG_DEVRES is also supported for easier debugging.
If enabled, debug messages are printed each time a resource is
allocated/freed.
Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Acked-by: Simon Glass <sjg@chromium.org>
2015-07-25 12:52:35 +00:00
|
|
|
|
|
|
|
devres_release_probe(dev);
|
2014-11-11 00:16:47 +00:00
|
|
|
}
|
|
|
|
|
2021-01-24 21:32:45 +00:00
|
|
|
/**
|
|
|
|
* flags_remove() - Figure out whether to remove a device
|
|
|
|
*
|
2021-01-24 21:32:46 +00:00
|
|
|
* If this is called with @flags == DM_REMOVE_NON_VITAL | DM_REMOVE_ACTIVE_DMA,
|
|
|
|
* then it returns 0 (=go head and remove) if the device is not matked vital
|
|
|
|
* but is marked DM_REMOVE_ACTIVE_DMA.
|
|
|
|
*
|
|
|
|
* If this is called with @flags == DM_REMOVE_ACTIVE_DMA,
|
|
|
|
* then it returns 0 (=go head and remove) if the device is marked
|
|
|
|
* DM_REMOVE_ACTIVE_DMA, regardless of whether it is marked vital.
|
|
|
|
*
|
2021-01-24 21:32:45 +00:00
|
|
|
* @flags: Flags passed to device_remove()
|
|
|
|
* @drv_flags: Driver flags
|
2022-01-19 17:05:50 +00:00
|
|
|
* Return: 0 if the device should be removed,
|
2021-01-24 21:32:45 +00:00
|
|
|
* -EKEYREJECTED if @flags includes a flag in DM_REMOVE_ACTIVE_ALL but
|
|
|
|
* @drv_flags does not (indicates that this device has nothing to do for
|
|
|
|
* DMA shutdown or OS prepare)
|
2021-01-24 21:32:46 +00:00
|
|
|
* -EPROBE_DEFER if @flags is DM_REMOVE_NON_VITAL but @drv_flags contains
|
|
|
|
* DM_FLAG_VITAL (indicates the device is vital and should not be removed)
|
2021-01-24 21:32:45 +00:00
|
|
|
*/
|
|
|
|
static int flags_remove(uint flags, uint drv_flags)
|
2017-04-24 07:48:02 +00:00
|
|
|
{
|
2021-01-24 21:32:46 +00:00
|
|
|
if (!(flags & DM_REMOVE_NORMAL)) {
|
|
|
|
bool vital_match;
|
|
|
|
bool active_match;
|
|
|
|
|
|
|
|
active_match = !(flags & DM_REMOVE_ACTIVE_ALL) ||
|
|
|
|
(drv_flags & flags);
|
|
|
|
vital_match = !(flags & DM_REMOVE_NON_VITAL) ||
|
|
|
|
!(drv_flags & DM_FLAG_VITAL);
|
|
|
|
if (!vital_match)
|
|
|
|
return -EPROBE_DEFER;
|
|
|
|
if (!active_match)
|
|
|
|
return -EKEYREJECTED;
|
|
|
|
}
|
2017-04-24 07:48:02 +00:00
|
|
|
|
2021-01-24 21:32:46 +00:00
|
|
|
return 0;
|
2017-04-24 07:48:02 +00:00
|
|
|
}
|
|
|
|
|
2017-03-20 11:51:48 +00:00
|
|
|
int device_remove(struct udevice *dev, uint flags)
|
2014-11-11 00:16:47 +00:00
|
|
|
{
|
2015-03-25 18:21:54 +00:00
|
|
|
const struct driver *drv;
|
2014-11-11 00:16:47 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!dev)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2020-12-19 17:40:10 +00:00
|
|
|
if (!(dev_get_flags(dev) & DM_FLAG_ACTIVATED))
|
2014-11-11 00:16:47 +00:00
|
|
|
return 0;
|
|
|
|
|
2022-03-04 15:43:03 +00:00
|
|
|
ret = device_notify(dev, EVT_DM_PRE_REMOVE);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2021-01-24 21:32:45 +00:00
|
|
|
/*
|
|
|
|
* If the child returns EKEYREJECTED, continue. It just means that it
|
|
|
|
* didn't match the flags.
|
|
|
|
*/
|
|
|
|
ret = device_chld_remove(dev, NULL, flags);
|
|
|
|
if (ret && ret != -EKEYREJECTED)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove the device if called with the "normal" remove flag set,
|
|
|
|
* or if the remove flag matches any of the drivers remove flags
|
|
|
|
*/
|
2014-11-11 00:16:47 +00:00
|
|
|
drv = dev->driver;
|
|
|
|
assert(drv);
|
2021-01-24 21:32:45 +00:00
|
|
|
ret = flags_remove(flags, drv->flags);
|
|
|
|
if (ret) {
|
|
|
|
log_debug("%s: When removing: flags=%x, drv->flags=%x, err=%d\n",
|
|
|
|
dev->name, flags, drv->flags, ret);
|
2014-11-11 00:16:47 +00:00
|
|
|
return ret;
|
2021-01-24 21:32:45 +00:00
|
|
|
}
|
2014-11-11 00:16:47 +00:00
|
|
|
|
2021-01-24 21:32:44 +00:00
|
|
|
ret = uclass_pre_remove_device(dev);
|
2014-11-11 00:16:47 +00:00
|
|
|
if (ret)
|
2021-01-24 21:32:44 +00:00
|
|
|
return ret;
|
2014-11-11 00:16:47 +00:00
|
|
|
|
2021-01-24 21:32:45 +00:00
|
|
|
if (drv->remove) {
|
2014-11-11 00:16:47 +00:00
|
|
|
ret = drv->remove(dev);
|
|
|
|
if (ret)
|
|
|
|
goto err_remove;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dev->parent && dev->parent->driver->child_post_remove) {
|
|
|
|
ret = dev->parent->driver->child_post_remove(dev);
|
|
|
|
if (ret) {
|
|
|
|
dm_warn("%s: Device '%s' failed child_post_remove()",
|
|
|
|
__func__, dev->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-28 20:03:48 +00:00
|
|
|
if (!(flags & DM_REMOVE_NO_PD) &&
|
|
|
|
!(drv->flags &
|
2021-01-24 21:32:42 +00:00
|
|
|
(DM_FLAG_DEFAULT_PD_CTRL_OFF | DM_FLAG_LEAVE_PD_ON)) &&
|
2020-02-17 11:36:43 +00:00
|
|
|
dev != gd->cur_serial_dev)
|
2019-09-27 08:18:15 +00:00
|
|
|
dev_power_domain_off(dev);
|
|
|
|
|
2021-01-24 21:32:45 +00:00
|
|
|
device_free(dev);
|
2014-11-11 00:16:47 +00:00
|
|
|
|
2021-01-24 21:32:45 +00:00
|
|
|
dev_bic_flags(dev, DM_FLAG_ACTIVATED);
|
2014-11-11 00:16:47 +00:00
|
|
|
|
2022-03-04 15:43:03 +00:00
|
|
|
ret = device_notify(dev, EVT_DM_POST_REMOVE);
|
|
|
|
if (ret)
|
|
|
|
goto err_remove;
|
|
|
|
|
2021-01-24 21:32:45 +00:00
|
|
|
return 0;
|
2014-11-11 00:16:47 +00:00
|
|
|
|
|
|
|
err_remove:
|
|
|
|
/* We can't put the children back */
|
|
|
|
dm_warn("%s: Device '%s' failed to remove, but children are gone\n",
|
|
|
|
__func__, dev->name);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|