u-boot/drivers/firmware/scmi/sandbox-scmi_devices.c
Miquel Raynal 9086b64ca0 power-domain: Add support for refcounting (again)
It is very surprising that such an uclass, specifically designed to
handle resources that may be shared by different devices, is not keeping
the count of the number of times a power domain has been
enabled/disabled to avoid shutting it down unexpectedly or disabling it
several times.

Doing this causes troubles on eg. i.MX8MP because disabling power
domains can be done in recursive loops were the same power domain
disabled up to 4 times in a row. PGCs seem to have tight FSM internal
timings to respect and it is easy to produce a race condition that puts
the power domains in an unstable state, leading to ADB400 errors and
later crashes in Linux.

Some drivers implement their own mechanism for that, but it is probably
best to add this feature in the uclass and share the common code across
drivers. In order to avoid breaking existing drivers, refcounting is
only enabled if the number of subdomains a device node supports is
explicitly set in the probe function. ->xlate() callbacks will return
the power domain ID which is then being used as the array index to reach
the correct refcounter.

As we do not want to break existing users while stile getting
interesting error codes, the implementation is split between:
- a low-level helper reporting error codes if the requested transition
  could not be operated,
- a higher-level helper ignoring the "non error" codes, like EALREADY and
  EBUSY.

CI tests using power domains are slightly updated to make sure the count
of on/off calls is even and the results match what we *now* expect. They
are also extended to test the low-level functions.

Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
2025-04-28 10:41:19 -03:00

168 lines
4.0 KiB
C

// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2020-2021, Linaro Limited
*/
#define LOG_CATEGORY UCLASS_MISC
#include <clk.h>
#include <dm.h>
#include <log.h>
#include <malloc.h>
#include <reset.h>
#include <asm/io.h>
#include <asm/scmi_test.h>
#include <dm/device_compat.h>
#include <power/regulator.h>
/*
* Simulate to some extent a SCMI exchange.
* This drivers gets SCMI resources and offers API function to the
* SCMI test sequence manipulate the resources, currently clock
* and reset controllers.
*/
#define SCMI_TEST_DEVICES_CLK_COUNT 2
#define SCMI_TEST_DEVICES_RD_COUNT 1
#define SCMI_TEST_DEVICES_VOLTD_COUNT 2
/*
* struct sandbox_scmi_device_priv - Storage for device handles used by test
* @pwdom: Power domain device
* @clk: Array of clock instances used by tests
* @reset_clt: Array of the reset controller instances used by tests
* @regulators: Array of regulator device references used by the tests
* @devices: Resources exposed by sandbox_scmi_devices_ctx()
*/
struct sandbox_scmi_device_priv {
struct power_domain pwdom;
struct clk clk[SCMI_TEST_DEVICES_CLK_COUNT];
struct reset_ctl reset_ctl[SCMI_TEST_DEVICES_RD_COUNT];
struct udevice *regulators[SCMI_TEST_DEVICES_VOLTD_COUNT];
struct sandbox_scmi_devices devices;
};
struct sandbox_scmi_devices *sandbox_scmi_devices_ctx(struct udevice *dev)
{
struct sandbox_scmi_device_priv *priv = dev_get_priv(dev);
if (priv)
return &priv->devices;
return NULL;
}
static int sandbox_scmi_devices_remove(struct udevice *dev)
{
struct sandbox_scmi_devices *devices = sandbox_scmi_devices_ctx(dev);
int ret = 0;
size_t n;
if (!devices)
return 0;
if (CONFIG_IS_ENABLED(RESET_SCMI))
for (n = 0; n < SCMI_TEST_DEVICES_RD_COUNT; n++) {
int ret2 = reset_free(devices->reset + n);
if (ret2 && !ret)
ret = ret2;
}
return ret;
}
static int sandbox_scmi_devices_probe(struct udevice *dev)
{
struct sandbox_scmi_device_priv *priv = dev_get_priv(dev);
int ret;
size_t n;
priv->devices = (struct sandbox_scmi_devices){
.pwdom = &priv->pwdom,
.pwdom_count = 1,
.clk = priv->clk,
.clk_count = SCMI_TEST_DEVICES_CLK_COUNT,
.reset = priv->reset_ctl,
.reset_count = SCMI_TEST_DEVICES_RD_COUNT,
.regul = priv->regulators,
.regul_count = SCMI_TEST_DEVICES_VOLTD_COUNT,
};
if (CONFIG_IS_ENABLED(SCMI_POWER_DOMAIN)) {
ret = power_domain_get_by_index(dev, priv->devices.pwdom, 0);
if (ret) {
dev_err(dev, "%s: Failed on power domain\n", __func__);
return ret;
}
}
if (CONFIG_IS_ENABLED(CLK_SCMI)) {
for (n = 0; n < SCMI_TEST_DEVICES_CLK_COUNT; n++) {
ret = clk_get_by_index(dev, n, priv->devices.clk + n);
if (ret) {
dev_err(dev, "%s: Failed on clk %zu\n",
__func__, n);
return ret;
}
}
}
if (CONFIG_IS_ENABLED(RESET_SCMI)) {
for (n = 0; n < SCMI_TEST_DEVICES_RD_COUNT; n++) {
ret = reset_get_by_index(dev, n,
priv->devices.reset + n);
if (ret) {
dev_err(dev, "%s: Failed on reset %zu\n",
__func__, n);
goto err_reset;
}
}
}
if (CONFIG_IS_ENABLED(DM_REGULATOR_SCMI)) {
for (n = 0; n < SCMI_TEST_DEVICES_VOLTD_COUNT; n++) {
char name[32];
ret = snprintf(name, sizeof(name), "regul%zu-supply",
n);
assert(ret >= 0 && ret < sizeof(name));
ret = device_get_supply_regulator(dev, name,
priv->devices.regul
+ n);
if (ret) {
dev_err(dev, "%s: Failed on voltd %zu\n",
__func__, n);
goto err_regul;
}
}
}
return 0;
err_regul:
n = SCMI_TEST_DEVICES_RD_COUNT;
err_reset:
if (CONFIG_IS_ENABLED(RESET_SCMI))
for (; n > 0; n--)
reset_free(priv->devices.reset + n - 1);
return ret;
}
static const struct udevice_id sandbox_scmi_devices_ids[] = {
{ .compatible = "sandbox,scmi-devices" },
{ }
};
U_BOOT_DRIVER(sandbox_scmi_devices) = {
.name = "sandbox-scmi_devices",
.id = UCLASS_MISC,
.of_match = sandbox_scmi_devices_ids,
.priv_auto = sizeof(struct sandbox_scmi_device_priv),
.remove = sandbox_scmi_devices_remove,
.probe = sandbox_scmi_devices_probe,
.flags = DM_FLAG_DEFAULT_PD_CTRL_OFF,
};