ARM PSCI在ATF和Linux kernel中的实现
Linux內核中cpu_ops的實現因架構而已,對于ARM64架構一般通過執行smc指令進入EL3異常,由ATF執行PSCI功能。然后將結果返回給Linux。
這中間涉及到【Linux kernel的cpu_ops、psci_ops】、【SMC/HVC】、【PSCI】、【ATF的PSCI】相關等等。
1. PSCI規格
目前PSCI最新規格為v1.1,這里以v1.0為參考:《POWER STATE COORDINATION INTERFACE (PSCI)System Software on ARM?Systems》。
1.1 PSCI_VERSION
返回當前psci固件版本號。
1.2 CPU_SUSPEND
執行核的suspend操作,一般用于等待被喚醒后繼續執行的子系統。
1.3CPU_OFF
用于hotplug中關閉調用此功能的核。被CPU_OFF關閉的僅能被CPU_ON打開。
1.4 CPU_ON
上電一個核,用于以下兩種情況:
還未被啟動的核
已經被CPU_OFF關閉的核。
1.5 AFFINITY_INFO
1.6 MIGRATE
可選。要求單核TOS將指向上下文遷移到一個指定核。
1.7 MIGRATE_INFO_TYPE
允許調用者查詢當前TOS對多核支持情況。
1.8 MIGRATE_INFO_UP_CPU
可選。對單核TOS系統,此功能返回TOS當前駐存在哪個核上。
1.9 SYSTEM_OFF
1.10SYSTEM_RESET
進行系統復位,沒有入參也沒有返回值。
1.11PSCI_FEATURES
查詢psci固件是否支持指定功能id及其特性。
1.12CPU_FREEZE
1.13 CPU_DEFAULT_SUSPEND
1.14 NODE_HW_STATE
1.15 SYSTEM_SUSPEND
讓系統進入深度低功耗模式。
1.16 SUSPEND_MODE
1.17 PSCI_STAT_RESIDENCY
1.18 PSCI_STAT_COUNT
2. ATF PSCI實現
參考:《《ARM Trusted Firmware》閱讀筆記 PSCI》
3. Linux PSCI實現
PSCI主要負責CPU低功耗、熱插拔功能,對接cpu_ops實現一系列函數。
在dt中配置psci屬性,以及在
3.1 psci dts配置
psci相關配置在dts中定義:
psci {
compatible = "arm,psci-0.2";
method = "smc";
};
說明使用的驅動是psci v0.2標準的接口。
3.2 psci驅動初始化
對psci初始化在setup_arch()中調用,psci_dt_init()從dt中解析出psci版本以及實現psci調用的方式(smc)。
void __init setup_arch(char **cmdline_p)
{
...
if (acpi_disabled)
psci_dt_init();
else
psci_acpi_init();
...
}
int __init psci_dt_init(void)
{
struct device_node *np;
const struct of_device_id *matched_np;
psci_initcall_t init_fn;
np = of_find_matching_node_and_match(NULL, psci_of_match, &matched_np);----進行dts設備匹配,這里對應psci-0.2。
if (!np)
return -ENODEV;
init_fn = (psci_initcall_t)matched_np->data;-------------------------------對應的函數為psci_0_2_init()。
return init_fn(np);
}
static const struct of_device_id psci_of_match[] __initconst = {
{ .compatible = "arm,psci", .data = psci_0_1_init},
{ .compatible = "arm,psci-0.2", .data = psci_0_2_init},
{ .compatible = "arm,psci-1.0", .data = psci_0_2_init},
{},
};
static int __init psci_0_2_init(struct device_node *np)
{
int err;
err = get_set_conduit_method(np);--------------------------------------從dt中解析出psci的method,這里為smc,表示psci功能通過smc(Secure Monitor Call:->EL3調用)實現。其他方式還有svc(Supervisor call:->EL1調用)和hvc(Hypervisor call:->EL2調用)。
if (err)
goto out_put_node;
/*
* Starting with v0.2, the PSCI specification introduced a call
* (PSCI_VERSION) that allows probing the firmware version, so
* that PSCI function IDs and version specific initialization
* can be carried out according to the specific version reported
* by firmware
*/
err = psci_probe();
out_put_node:
of_node_put(np);
return err;
}
static int __init psci_probe(void)
{
u32 ver = psci_get_version();--------------------------------------------------通過SMC的PSCI_0_2_FN_PSCI_VERSION功能id獲取PSCI固件版本號。
pr_info("PSCIv%d.%d detected in firmware.
",
PSCI_VERSION_MAJOR(ver),
PSCI_VERSION_MINOR(ver));
if (PSCI_VERSION_MAJOR(ver) == 0 && PSCI_VERSION_MINOR(ver) < 2) {-------------驅動只支持psci 0.2及以上的psci固件。
pr_err("Conflicting PSCI version detected.
");
return -EINVAL;
}
psci_0_2_set_functions();------------------------------------------------------將linux中使用的psci_ops、arm_pm_off、pm_power_off對齊到具體PSCI的SMC功能id。
psci_init_migrate();
if (PSCI_VERSION_MAJOR(ver) >= 1) {--------------------------------------------對于>=v1.0版本psci,特殊處理suspend。
psci_init_smccc();
psci_init_cpu_suspend();
psci_init_system_suspend();
}
return 0;
}
3.2.1 PSCI功能實現中轉通道:SMC或HVC
kernel實現SMC調用的兩種方式:SMC和HVC。get_set_conduit_method()的核心是根據dt中的method字段,選擇合適的invoke_psci_fn函數。
enum psci_conduit {
PSCI_CONDUIT_NONE,
PSCI_CONDUIT_SMC,
PSCI_CONDUIT_HVC,
};
static int get_set_conduit_method(struct device_node *np)
{
const char *method;
pr_info("probing for conduit method from DT.
");
if (of_property_read_string(np, "method", &method)) {
pr_warn("missing "method" property
");
return -ENXIO;
}
if (!strcmp("hvc", method)) {
set_conduit(PSCI_CONDUIT_HVC);
} else if (!strcmp("smc", method)) {------------------------------根據dt中的method字段,設置invoke_psci_fn函數。
set_conduit(PSCI_CONDUIT_SMC);
} else {
pr_warn("invalid "method" property: %s
", method);
return -EINVAL;
}
return 0;
}
static void set_conduit(enum psci_conduit conduit)-----------------------HVC和SMC兩種訪問psci固件的方式,HVC表示當前OS為guest os;SMC表示從EL1直接訪問EL3 psci固件。
{
switch (conduit) {
case PSCI_CONDUIT_HVC:
invoke_psci_fn = __invoke_psci_fn_hvc;
break;
case PSCI_CONDUIT_SMC:
invoke_psci_fn = __invoke_psci_fn_smc;
break;
default:
WARN(1, "Unexpected PSCI conduit %d
", conduit);
}
psci_ops.conduit = conduit;
}
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;
}
.macro SMCCC instr
.cfi_startproc
instr #0
ldr x4, [sp]
stp x0, x1, [x4, #ARM_SMCCC_RES_X0_OFFS]
stp x2, x3, [x4, #ARM_SMCCC_RES_X2_OFFS]
ldr x4, [sp, #8]
cbz x4, 1f /* no quirk structure */
ldr x9, [x4, #ARM_SMCCC_QUIRK_ID_OFFS]
cmp x9, #ARM_SMCCC_QUIRK_QCOM_A6
b.ne 1f
str x6, [x4, ARM_SMCCC_QUIRK_STATE_OFFS]
1: ret
.cfi_endproc
.endm
/*
* void arm_smccc_smc(unsigned long a0, unsigned long a1, unsigned long a2,
* unsigned long a3, unsigned long a4, unsigned long a5,
* unsigned long a6, unsigned long a7, struct arm_smccc_res *res,
* struct arm_smccc_quirk *quirk)
*/
ENTRY(__arm_smccc_smc)
SMCCC smc
ENDPROC(__arm_smccc_smc)
/*
* void arm_smccc_hvc(unsigned long a0, unsigned long a1, unsigned long a2,
* unsigned long a3, unsigned long a4, unsigned long a5,
* unsigned long a6, unsigned long a7, struct arm_smccc_res *res,
* struct arm_smccc_quirk *quirk)
*/
ENTRY(__arm_smccc_hvc)
SMCCC hvc
ENDPROC(__arm_smccc_hvc)
3.2.2 psci_ops函數集
struct psci_operations psci_ops是Linux下對應psci功能函數集,另外psci_function_id[]下標為LInux psci功能id,值為具體psci規格功能id,psci_function_id[]進行兩者的轉換。
struct psci_operations {
u32 (*get_version)(void);------------------------------------------獲取psci固件版本號。
int (*cpu_suspend)(u32 state, unsigned long entry_point);----------
int (*cpu_off)(u32 state);
int (*cpu_on)(unsigned long cpuid, unsigned long entry_point);
int (*migrate)(unsigned long cpuid);
int (*affinity_info)(unsigned long target_affinity,
unsigned long lowest_affinity_level);
int (*migrate_info_type)(void);
enum psci_conduit conduit;
enum smccc_version smccc_version;
};
struct psci_operations psci_ops = {
.conduit = PSCI_CONDUIT_NONE,
.smccc_version = SMCCC_VERSION_1_0,
};
enum psci_function {
PSCI_FN_CPU_SUSPEND,
PSCI_FN_CPU_ON,
PSCI_FN_CPU_OFF,
PSCI_FN_MIGRATE,
PSCI_FN_MAX,
};
static u32 psci_function_id[PSCI_FN_MAX];
psci_0_2_setfunction()主要設置了psci_ops函數集,以及arm_pm_restart和pm_power_off。
static void __init psci_0_2_set_functions(void)
{
pr_info("Using standard PSCI v0.2 function IDs
");
psci_ops.get_version = psci_get_version;
psci_function_id[PSCI_FN_CPU_SUSPEND] =
PSCI_FN_NATIVE(0_2, CPU_SUSPEND);
psci_ops.cpu_suspend = psci_cpu_suspend;
psci_function_id[PSCI_FN_CPU_OFF] = PSCI_0_2_FN_CPU_OFF;
psci_ops.cpu_off = psci_cpu_off;
psci_function_id[PSCI_FN_CPU_ON] = PSCI_FN_NATIVE(0_2, CPU_ON);
psci_ops.cpu_on = psci_cpu_on;
psci_function_id[PSCI_FN_MIGRATE] = PSCI_FN_NATIVE(0_2, MIGRATE);
psci_ops.migrate = psci_migrate;
psci_ops.affinity_info = psci_affinity_info;
psci_ops.migrate_info_type = psci_migrate_info_type;
arm_pm_restart = psci_sys_reset;
pm_power_off = psci_sys_poweroff;
}
對應psci的PSCI_VERSION功能,返回psci固件版本號。
通過PSCI_VERSION_MAJOR()和PSCI_VERSION_MINOR()解析。
static u32 psci_get_version(void)
{
return invoke_psci_fn(PSCI_0_2_FN_PSCI_VERSION, 0, 0, 0);
}
對應psci的CPU_SUSPEND功能,state是將要進入的低功耗狀態,entry_point是從低功耗狀態返回后執行入口地址。
entry_point必須是物理地址或者虛擬機的IPA。
第三個參數是Powerdown功耗狀態才會使用。
static int psci_cpu_suspend(u32 state, unsigned long entry_point)
{
int err;
u32 fn;
fn = psci_function_id[PSCI_FN_CPU_SUSPEND];
err = invoke_psci_fn(fn, state, entry_point, 0);
return psci_to_linux_errno(err);
}
對應psci的CPU_OFF功能,讓關閉調用此功能的核。
static int psci_cpu_off(u32 state)
{
int err;
u32 fn;
fn = psci_function_id[PSCI_FN_CPU_OFF];
err = invoke_psci_fn(fn, state, 0, 0);
return psci_to_linux_errno(err);
}
對應psci的CPU_ON功能,給一個核上電。
cpuid為需要上電cpu的id;entry_point是CPU上電后運行入口物理地址或IPA,比如這里為secondary_entry()。如果第一次啟動,可以傳入context_id參數。
static int psci_cpu_on(unsigned long cpuid, unsigned long entry_point)
{
int err;
u32 fn;
fn = psci_function_id[PSCI_FN_CPU_ON];
err = invoke_psci_fn(fn, cpuid, entry_point, 0);
return psci_to_linux_errno(err);
}
static int cpu_psci_cpu_boot(unsigned int cpu)
{
int err = psci_ops.cpu_on(cpu_logical_map(cpu), __pa(secondary_entry));
if (err)
pr_err("failed to boot CPU%d (%d)
", cpu, err);
return err;
}
/*
* Secondary entry point that jumps straight into the kernel. Only to
* be used where CPUs are brought online dynamically by the kernel.
*/
ENTRY(secondary_entry)
bl el2_setup // Drop to EL1
bl set_cpu_boot_mode_flag
b secondary_startup
ENDPROC(secondary_entry)
對應psci的MIGRATE功能,將TOS遷移到指定cpuid上執行。
cpuid將要遷移到cpu的id。
static int psci_migrate(unsigned long cpuid)
{
int err;
u32 fn;
fn = psci_function_id[PSCI_FN_MIGRATE];
err = invoke_psci_fn(fn, cpuid, 0, 0);
return psci_to_linux_errno(err);
}
對應psci的AFFINITY_INFO功能,
static int psci_affinity_info(unsigned long target_affinity,
unsigned long lowest_affinity_level)
{
return invoke_psci_fn(PSCI_FN_NATIVE(0_2, AFFINITY_INFO),
target_affinity, lowest_affinity_level, 0);
}
對應psci的MIGRATE_INFO_TYPE功能,獲取TOS在多核環境下遷移能力。
0 - TOS運行在一個核上,但是可以遷移到任何違背CPU_OFF的核。
1 - TOS僅運行在一個核上,不支持MIGRATE功能。
2 - TOS不存在或者不需要MIGRATE功能。
NOT_SUPPORTED - 不需要MIGRATE。
static int psci_migrate_info_type(void)
{
return invoke_psci_fn(PSCI_0_2_FN_MIGRATE_INFO_TYPE, 0, 0, 0);
}
/* PSCI v0.2 multicore support in Trusted OS returned by MIGRATE_INFO_TYPE */
#define PSCI_0_2_TOS_UP_MIGRATE 0
#define PSCI_0_2_TOS_UP_NO_MIGRATE 1
#define PSCI_0_2_TOS_MP 2
對應psci的SYSTEM_RESET功能,執行系統復位功能。
static void psci_sys_reset(enum reboot_mode reboot_mode, const char *cmd)
{
invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
}
/*
* Restart requires that the secondary CPUs stop performing any activity
* while the primary CPU resets the system. Systems with multiple CPUs must
* provide a HW restart implementation, to ensure that all CPUs reset at once.
* This is required so that any code running after reset on the primary CPU
* doesn't have to co-ordinate with other CPUs to ensure they aren't still
* executing pre-reset code, and using RAM that the primary CPU's code wishes
* to use. Implementing such co-ordination would be essentially impossible.
*/
void machine_restart(char *cmd)
{
...
/* Now call the architecture specific reboot code. */
if (arm_pm_restart)
arm_pm_restart(reboot_mode, cmd);------------------調用psci_sys_reset()函數。
else
do_kernel_restart(cmd);
/*
* Whoops - the architecture was unable to reboot.
*/
printk("Reboot failed -- System halted
");
while (1);
}
對應psci的SYSTEM_OFF功能,關閉系統。無入參和返回值。
static void psci_sys_poweroff(void)
{
invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
}
/*
* Power-off simply requires that the secondary CPUs stop performing any
* activity (executing tasks, handling interrupts). smp_send_stop()
* achieves this. When the system power is turned off, it will take all CPUs
* with it.
*/
void machine_power_off(void)
{
local_irq_disable();
smp_send_stop();
if (pm_power_off)
pm_power_off();---------------------------調用psci_sys_poweroff()。
}
3.3 TOS駐存CPU不允許hotplug
psci_init_migrate()獲取當前TOS駐存的CPU id,并賦值給resident_cpu。
/*
* Detect the presence of a resident Trusted OS which may cause CPU_OFF to
* return DENIED (which would be fatal).
*/
static void __init psci_init_migrate(void)
{
unsigned long cpuid;
int type, cpu = -1;
type = psci_ops.migrate_info_type();-----------------------------------------獲取psci支持的TOS服務遷移類型。
if (type == PSCI_0_2_TOS_MP) {
pr_info("Trusted OS migration not required
");
return;
}
if (type == PSCI_RET_NOT_SUPPORTED) {
pr_info("MIGRATE_INFO_TYPE not supported.
");
return;
}
if (type != PSCI_0_2_TOS_UP_MIGRATE &&
type != PSCI_0_2_TOS_UP_NO_MIGRATE) {
pr_err("MIGRATE_INFO_TYPE returned unknown type (%d)
", type);
return;
}
cpuid = psci_migrate_info_up_cpu();------------------------------------------MIGRATE_INFO_UP_CPU獲取TOS駐存CPU的mpidr值。
if (cpuid & ~MPIDR_HWID_BITMASK) {
pr_warn("MIGRATE_INFO_UP_CPU reported invalid physical ID (0x%lx)
",
cpuid);
return;
}
cpu = get_logical_index(cpuid);----------------------------------------------將mpidr值轉換成cpu邏輯id,并賦值給resident_cpu。
resident_cpu = cpu >= 0 ? cpu : -1;
pr_info("Trusted OS resident on physical CPU 0x%lx
", cpuid);
}
當需要CPU進行hotplug之前,調用cpu_disable來檢查CPU是否支持hotplug。如果需要進行hotplug的cpu是resident_cpu,則返回EPERM錯誤。
bool psci_tos_resident_on(int cpu)
{
return cpu == resident_cpu;
}
static int cpu_psci_cpu_disable(unsigned int cpu)
{
/* Fail early if we don't have CPU_OFF support */
if (!psci_ops.cpu_off)
return -EOPNOTSUPP;
/* Trusted OS will deny CPU_OFF */
if (psci_tos_resident_on(cpu))
return -EPERM;
return 0;
}
const struct cpu_operations cpu_psci_ops = {
.name = "psci",
...
#ifdef CONFIG_HOTPLUG_CPU
.cpu_disable = cpu_psci_cpu_disable,
.cpu_die = cpu_psci_cpu_die,
.cpu_kill = cpu_psci_cpu_kill,
#endif
};
3.4 v1.0及以上suspend處理
static void __init psci_init_cpu_suspend(void)
{
int feature = psci_features(psci_function_id[PSCI_FN_CPU_SUSPEND]);
if (feature != PSCI_RET_NOT_SUPPORTED)
psci_cpu_suspend_feature = feature;
}
static void __init psci_init_system_suspend(void)
{
int ret;
if (!IS_ENABLED(CONFIG_SUSPEND))
return;
ret = psci_features(PSCI_FN_NATIVE(1_0, SYSTEM_SUSPEND));
if (ret != PSCI_RET_NOT_SUPPORTED)
suspend_set_ops(&psci_suspend_ops);
}
static const struct platform_suspend_ops psci_suspend_ops = {
.valid = suspend_valid_only_mem,
.enter = psci_system_suspend_enter,
};
static int psci_system_suspend_enter(suspend_state_t state)
{
return cpu_suspend(0, psci_system_suspend);
}
對應psci的SYSTEM_SUSPEND功能,實現suspend到ram功能,類似于進入最深度低功耗的CPU_SUSPEND。
成功則沒有返回值,失敗則返回NOT_SUPPORTED、INVALID_ADDRESS、ALREADY_ON之一。
static int psci_system_suspend(unsigned long unused)
{
return invoke_psci_fn(PSCI_FN_NATIVE(1_0, SYSTEM_SUSPEND),
virt_to_phys(cpu_resume), 0, 0);
}
4. cpu_ops到psci固件通路
大致調用路徑:cpu_ops->cpu_psci_ops->psci_ops->invoke_psci_fn()->SMCC。
dt中低功耗配置:
cpus {
#address-cells = <0x2>;
#size-cells = <0x0>;
cpu@0 {
compatible = "arm,cortex-a53";
device_type = "cpu";
reg = <0x0 0x0>;
enable-method = "psci";
clock-latency = <0x186a0>;
cpu-idle-states = <0xc 0xd>;
};
cpu@1 {
...
};
...
};
setup_arch()中調用cpu_read_bootcpu_ops(),經過一系列判斷cpu_ops[0]指向cpu_psci_ops。cpu_psci_ops中大部分實現通過調用psci_ops,在函數psci_0_2_set_functions()中指定了psci_ops函數集,基本通過invoke_psci_fn()發送SMC調用由psci固件在EL3執行。
void __init setup_arch(char **cmdline_p)
{
...
if (acpi_disabled)
psci_dt_init();
else
psci_acpi_init();
cpu_read_bootcpu_ops();
...
}
static inline void __init cpu_read_bootcpu_ops(void)
{
cpu_read_ops(0);
}
int __init cpu_read_ops(int cpu)
{
const char *enable_method = cpu_read_enable_method(cpu);------------------------讀取當前cpu在dt中的enable-method配置,這里以psci為例。
if (!enable_method)
return -ENODEV;
cpu_ops[cpu] = cpu_get_ops(enable_method);
if (!cpu_ops[cpu]) {
pr_warn("Unsupported enable-method: %s
", enable_method);
return -EOPNOTSUPP;
}
return 0;
}
static const struct cpu_operations * __init cpu_get_ops(const char *name)
{
const struct cpu_operations **ops;
ops = acpi_disabled ? dt_supported_cpu_ops : acpi_supported_cpu_ops;
while (*ops) {
if (!strcmp(name, (*ops)->name))-----------------------------------------------在關閉acpi情況下,根據從dt中讀取的字符串匹配到cpu_psci_ops函數集。
return *ops;
ops++;
}
return NULL;
}
static const struct cpu_operations *dt_supported_cpu_ops[] __initconst = {
&smp_spin_table_ops,
&cpu_psci_ops,
NULL,
};
const struct cpu_operations cpu_psci_ops = {
.name = "psci",
#ifdef CONFIG_CPU_IDLE
.cpu_init_idle = psci_cpu_init_idle,
.cpu_suspend = psci_cpu_suspend_enter,
#endif
.cpu_init = cpu_psci_cpu_init,
.cpu_prepare = cpu_psci_cpu_prepare,
.cpu_boot = cpu_psci_cpu_boot,
#ifdef CONFIG_HOTPLUG_CPU
.cpu_disable = cpu_psci_cpu_disable,
.cpu_die = cpu_psci_cpu_die,
.cpu_kill = cpu_psci_cpu_kill,
#endif
};
總結
以上是生活随笔為你收集整理的ARM PSCI在ATF和Linux kernel中的实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: diamond简介和使用
- 下一篇: 【几何系列】向量:向量乘法(标量积、向量