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》。
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)。
psci_dt_init();
else
psci_acpi_init();
...
}
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},
{},
};
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;
}
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,
};
__invoke_psci_fn_hvc;
break;
case PSCI_CONDUIT_SMC:
invoke_psci_fn = __invoke_psci_fn_smc;
break;
default:
WARN(1, "Unexpected PSCI conduit %d\n", conduit);
}
psci_ops.conduit = conduit;
}
arm_smccc_hvc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res);
return res.a0;
}
arm_smccc_smc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res);
return res.a0;
}
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)
*/
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。
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()解析。
CPU_SUSPEND功能,state是将要进入的低功耗状态,entry_point是从低功耗状态返回后执行入口地址。
entry_point必须是物理地址或者虚拟机的IPA。
第三个参数是Powerdown功耗状态才会使用。
CPU_OFF功能,让关闭调用此功能的核。
CPU_ON功能,给一个核上电。
cpuid为需要上电cpu的id;entry_point是CPU上电后运行入口物理地址或IPA,比如这里为secondary_entry()。如果第一次启动,可以传入context_id参数。
MIGRATE功能,将TOS迁移到指定cpuid上执行。
cpuid将要迁移到cpu的id。
AFFINITY_INFO功能,
MIGRATE_INFO_TYPE功能,获取TOS在多核环境下迁移能力。
0 - TOS运行在一个核上,但是可以迁移到任何违背CPU_OFF的核。
1 - TOS仅运行在一个核上,不支持MIGRATE功能。
2 - TOS不存在或者不需要MIGRATE功能。
NOT_SUPPORTED - 不需要MIGRATE。
SYSTEM_RESET功能,执行系统复位功能。
SYSTEM_OFF功能, 关闭系统。无入参和返回值。
MIGRATE_INFO_UP_CPU获取TOS驻存CPU的mpidr值。
if (cpuid & ~MPIDR_HWID_BITMASK) {
pr_warn("MIGRATE_INFO_UP_CPU reported invalid physical ID (0x%lx)\n",
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\n", cpuid);
}
当需要CPU进行hotplug之前,调用cpu_disable来检查CPU是否支持hotplug。如果需要进行hotplug的cpu是resident_cpu,则返回EPERM错误。
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处理
psci_suspend_ops);
}
psci_system_suspend_enter,
};
psci_system_suspend);
}
对应psci的SYSTEM_SUSPEND功能,实现suspend到ram功能,类似于进入最深度低功耗的CPU_SUSPEND。
成功则没有返回值,失败则返回NOT_SUPPORTED、INVALID_ADDRESS、ALREADY_ON之一。
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();
...
}
cpu_read_ops(0);
}
cpu_get_ops(enable_method);
if (!cpu_ops[cpu]) {
pr_warn("Unsupported enable-method: %s\n", enable_method);
return -EOPNOTSUPP;
}
return 0;
}
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;
}
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
};
联系方式:arnoldlu@qq.com
entry_point必须是物理地址或者虚拟机的IPA。
第三个参数是Powerdown功耗状态才会使用。
CPU_OFF功能,让关闭调用此功能的核。CPU_ON功能,给一个核上电。cpuid为需要上电cpu的id;entry_point是CPU上电后运行入口物理地址或IPA,比如这里为secondary_entry()。如果第一次启动,可以传入context_id参数。
MIGRATE功能,将TOS迁移到指定cpuid上执行。cpuid将要迁移到cpu的id。
AFFINITY_INFO功能,MIGRATE_INFO_TYPE功能,获取TOS在多核环境下迁移能力。0 - TOS运行在一个核上,但是可以迁移到任何违背CPU_OFF的核。
1 - TOS仅运行在一个核上,不支持MIGRATE功能。
2 - TOS不存在或者不需要MIGRATE功能。
NOT_SUPPORTED - 不需要MIGRATE。
SYSTEM_RESET功能,执行系统复位功能。SYSTEM_OFF功能, 关闭系统。无入参和返回值。MIGRATE_INFO_UP_CPU获取TOS驻存CPU的mpidr值。 if (cpuid & ~MPIDR_HWID_BITMASK) { pr_warn("MIGRATE_INFO_UP_CPU reported invalid physical ID (0x%lx)\n", 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\n", cpuid); }当需要CPU进行hotplug之前,调用cpu_disable来检查CPU是否支持hotplug。如果需要进行hotplug的cpu是resident_cpu,则返回EPERM错误。
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处理
psci_suspend_ops); } psci_system_suspend_enter, }; psci_system_suspend); }对应psci的SYSTEM_SUSPEND功能,实现suspend到ram功能,类似于进入最深度低功耗的CPU_SUSPEND。
成功则没有返回值,失败则返回NOT_SUPPORTED、INVALID_ADDRESS、ALREADY_ON之一。
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(); ... } cpu_read_ops(0); } cpu_get_ops(enable_method); if (!cpu_ops[cpu]) { pr_warn("Unsupported enable-method: %s\n", enable_method); return -EOPNOTSUPP; } return 0; } 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; } 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 };联系方式:arnoldlu@qq.com