2020-12-19 22:52:48 +08:00
|
|
|
/*
|
|
|
|
* Copyright 2020 Carlo Caione <ccaione@baylibre.com>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define DT_DRV_COMPAT arm_psci_0_2
|
|
|
|
|
2021-03-01 15:57:15 +08:00
|
|
|
#define LOG_LEVEL CONFIG_PM_CPU_OPS_LOG_LEVEL
|
2020-12-19 22:52:48 +08:00
|
|
|
#include <logging/log.h>
|
|
|
|
LOG_MODULE_REGISTER(psci);
|
|
|
|
|
|
|
|
#include <kernel.h>
|
|
|
|
#include <arch/cpu.h>
|
|
|
|
|
|
|
|
#include <soc.h>
|
|
|
|
#include <device.h>
|
|
|
|
#include <init.h>
|
|
|
|
|
2021-03-01 15:57:15 +08:00
|
|
|
#include <drivers/pm_cpu_ops.h>
|
|
|
|
#include "pm_cpu_ops_psci.h"
|
|
|
|
|
|
|
|
static struct psci psci_data;
|
2020-12-19 22:52:48 +08:00
|
|
|
|
|
|
|
static int psci_to_dev_err(int ret)
|
|
|
|
{
|
|
|
|
switch (ret) {
|
|
|
|
case PSCI_RET_SUCCESS:
|
|
|
|
return 0;
|
|
|
|
case PSCI_RET_NOT_SUPPORTED:
|
|
|
|
return -ENOTSUP;
|
|
|
|
case PSCI_RET_INVALID_PARAMS:
|
|
|
|
case PSCI_RET_INVALID_ADDRESS:
|
|
|
|
return -EINVAL;
|
|
|
|
case PSCI_RET_DENIED:
|
|
|
|
return -EPERM;
|
2021-03-25 07:39:15 +08:00
|
|
|
}
|
2020-12-19 22:52:48 +08:00
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2021-03-01 15:57:15 +08:00
|
|
|
int pm_cpu_off(void)
|
2020-12-19 22:52:48 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2021-03-01 15:57:15 +08:00
|
|
|
if (psci_data.conduit == SMCCC_CONDUIT_NONE)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
ret = psci_data.invoke_psci_fn(PSCI_0_2_FN_CPU_OFF, 0, 0, 0);
|
2020-12-19 22:52:48 +08:00
|
|
|
|
|
|
|
return psci_to_dev_err(ret);
|
|
|
|
}
|
|
|
|
|
2021-03-01 15:57:15 +08:00
|
|
|
int pm_cpu_on(unsigned long cpuid,
|
|
|
|
uintptr_t entry_point)
|
2020-12-19 22:52:48 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2021-03-01 15:57:15 +08:00
|
|
|
if (psci_data.conduit == SMCCC_CONDUIT_NONE)
|
|
|
|
return -EINVAL;
|
2020-12-19 22:52:48 +08:00
|
|
|
|
2021-03-01 15:57:15 +08:00
|
|
|
ret = psci_data.invoke_psci_fn(PSCI_FN_NATIVE(0_2, CPU_ON), cpuid,
|
|
|
|
(unsigned long) entry_point, 0);
|
2020-12-19 22:52:48 +08:00
|
|
|
|
2021-03-01 15:57:15 +08:00
|
|
|
return psci_to_dev_err(ret);
|
2020-12-19 22:52:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned long __invoke_psci_fn_hvc(unsigned long function_id,
|
|
|
|
unsigned long arg0,
|
|
|
|
unsigned long arg1,
|
|
|
|
unsigned long arg2)
|
|
|
|
{
|
|
|
|
struct arm_smccc_res res;
|
|
|
|
|
|
|
|
arm_smccc_hvc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res);
|
|
|
|
return res.a0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned long __invoke_psci_fn_smc(unsigned long function_id,
|
|
|
|
unsigned long arg0,
|
|
|
|
unsigned long arg1,
|
|
|
|
unsigned long arg2)
|
|
|
|
{
|
|
|
|
struct arm_smccc_res res;
|
|
|
|
|
|
|
|
arm_smccc_smc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res);
|
|
|
|
return res.a0;
|
|
|
|
}
|
|
|
|
|
2021-03-01 15:57:15 +08:00
|
|
|
static uint32_t psci_get_version(void)
|
|
|
|
{
|
|
|
|
return psci_data.invoke_psci_fn(PSCI_0_2_FN_PSCI_VERSION, 0, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int set_conduit_method(void)
|
2020-12-19 22:52:48 +08:00
|
|
|
{
|
|
|
|
const char *method;
|
|
|
|
|
|
|
|
method = DT_PROP(DT_INST(0, DT_DRV_COMPAT), method);
|
|
|
|
|
|
|
|
if (!strcmp("hvc", method)) {
|
2021-03-01 15:57:15 +08:00
|
|
|
psci_data.conduit = SMCCC_CONDUIT_HVC;
|
|
|
|
psci_data.invoke_psci_fn = __invoke_psci_fn_hvc;
|
2020-12-19 22:52:48 +08:00
|
|
|
} else if (!strcmp("smc", method)) {
|
2021-03-01 15:57:15 +08:00
|
|
|
psci_data.conduit = SMCCC_CONDUIT_SMC;
|
|
|
|
psci_data.invoke_psci_fn = __invoke_psci_fn_smc;
|
2020-12-19 22:52:48 +08:00
|
|
|
} else {
|
|
|
|
LOG_ERR("Invalid conduit method");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-03-01 15:57:15 +08:00
|
|
|
static int psci_detect(void)
|
2020-12-19 22:52:48 +08:00
|
|
|
{
|
2021-03-01 15:57:15 +08:00
|
|
|
uint32_t ver = psci_get_version();
|
2020-12-19 22:52:48 +08:00
|
|
|
|
|
|
|
LOG_DBG("Detected PSCIv%d.%d",
|
|
|
|
PSCI_VERSION_MAJOR(ver),
|
|
|
|
PSCI_VERSION_MINOR(ver));
|
|
|
|
|
|
|
|
if (PSCI_VERSION_MAJOR(ver) == 0 && PSCI_VERSION_MINOR(ver) < 2) {
|
|
|
|
LOG_ERR("PSCI unsupported version");
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
2021-03-01 15:57:15 +08:00
|
|
|
psci_data.ver = ver;
|
|
|
|
|
2020-12-19 22:52:48 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-03-01 15:57:15 +08:00
|
|
|
uint32_t psci_version(void)
|
|
|
|
{
|
|
|
|
return psci_data.ver;
|
|
|
|
}
|
|
|
|
|
2020-12-19 22:52:48 +08:00
|
|
|
static int psci_init(const struct device *dev)
|
|
|
|
{
|
2021-03-01 15:57:15 +08:00
|
|
|
psci_data.conduit = SMCCC_CONDUIT_NONE;
|
2020-12-19 22:52:48 +08:00
|
|
|
|
2021-03-01 15:57:15 +08:00
|
|
|
if (set_conduit_method()) {
|
2020-12-19 22:52:48 +08:00
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
2021-03-01 15:57:15 +08:00
|
|
|
return psci_detect();
|
2020-12-19 22:52:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
DEVICE_DT_INST_DEFINE(0, psci_init, device_pm_control_nop,
|
|
|
|
&psci_data, NULL, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEVICE,
|
2021-03-01 15:57:15 +08:00
|
|
|
NULL);
|