PSCI和ATF

Overview

ARM定义了一个标准接口用于电源管理,来简化不同权限软件的互操作,例如OS、hypervisor、ATF、secure firmware。

PSCI接口主要用于以下电源管理场景:

  1. core idle管理
  2. 动态添加和移除core;从核启动
  3. 系统shutdown和reset

安全与非安全OS分工:

  1. ARM建议安全世界主要负责状态的控制
  2. ARM建议电源和性能的管理策略放在非安全世界

PSCI版本:

date version
13 August 2012 0.1
24 June 2013 0.2
30 January 2015 1.0
21 April 2017 1.1

Idle management

不同的low-power state,具有不同的进出时延,以及不同的功耗级别。low-power state不仅与core有关,它是一组组件的状态集合(cluster、cache、memory),clock-gated or power-gated。

An Arm core can be in any of the following power states:

state description
run The core is powered up and operational.
standby The core is powered up the core enters standby by executing a WFI or WFE instruction and exits on a corresponding wakeup event. In standby, all core context is maintained, and can be directly accessed on wakeup. Changing from standby to running operation does not require a reset of the core . The core is gated
retention The core is partially turned off. Changing from low-power retention to running operation does not require a reset of the core. The saved core state is restored on changing from low-power retention state to running operation. From an OS point of view, there is no difference between a retention state and standby state, other than the method of entry, latency, and usage-related constraints.
powerdown the core is powered off. Software on the device needs to save all core state, so that it can be preserved over the powerdown. A reset of the core, after the power has been restored. Restoring the saved core state. When wake up core,the core restarts at the reset vector. After initializing, the Secure world must re-start at the OS required return address.

CPU hotplug and secondary CPU boot

hotplug与powerdown的区别

  1. 如果core 被拔掉,OS会停止其所有的中断响应。
  2. OS必须发出明确的命令来重新插入core。
  3. 被拔掉的core,不会再有唤醒powerdown core的中断。

OS通常会在primary core执行主要的boot流程,然后再引导secondary core。secondary boot和hotplug的流程接口是一样的。

Power state system topologies and coordination

Arm systems can have multiple cores or even multiple clusters of cores. 使用术语affinity hierarchy来时表示core的层次结构。使用术语affinity level 来表示层次。使用术语affinity instance 来表示一个core或者一个cluster。

Power topology中high-level nodes(system/cluster)的power states需要协调其children nodes。For example, entry into a cluster powerdown state is only possible when all cores in the cluster are powered down. PSCI supports two modes of power state coordination, *platform-coordinated mode*, and *OS-initiated mode*.

  • platform-coordinated mode

这是当前ATF默认支持的模式。When a core has no more work to do, the OSPM requests the deepest state it can tolerate for that core and its parent nodes. For power state requests that affect a topology node above the core level, the implementation chooses the deepest power state that can be tolerated by all the cores in the node.

Versions of PSCI prior to 1.0 support only the platform-coordinated mode.

  • OS-initiated mode

PSCI 1.0引入OS-initiated mode,把协调责任放在了OS侧。OSPM only requests an idle state for a particular topology node when the last underlying core goes idle。When a core goes idle it always selects an idle state for itself, but idle states for higher-level nodes such as clusters are only selected when the last running core in the node goes idle.

由于只有last core会发送cluster、system level的状态,会存在一种消息竞争问题,例如,开始时core1 处于powerdown,core 0作为last core发送消息到firmware请求core1进入powerdown,cluster进入retention。firmware还没收到消息,这时突然收到core 0唤醒中断,core 0再次作为last core,请求core 0进入powerdown,cluster进入powerdown。如果firmware先处理了core 0的消息,再处理core 1的消息。那么最终会造成OS认为cluster在powerdown,firmware把cluster处理成了retention。具体见ACPI SPEC 8.4.4.2.2章节。

  • 总结

两种模式的区别在于,platform-coordinated mode在firmware中维护拓扑结构的power状态,OS每次下发的请求都要包含core /cluster/system的每个level的投票信息。OS-initiated mode在OS侧维护拓扑结构的power状态,除了last core会发送其父结点的状态,其他core只会发送自己的状态。

Functions

PSCI_VERSION

Return the version of PSCI implemented.

Return value:Bits[31:16] Major Version,Bits[15:0] Minor Version

unsigned int psci_version(void)
{
    return PSCI_MAJOR_VER | PSCI_MINOR_VER;
}

CPU_SUSPEND

The CPU_SUSPEND API is used to move a topology node into a low-power state. The function is called from a specific core in that topology node( a core or higher-level). This call is intended for use in idle subsystems where the core is expected to return to execution through a wakeup event.

函数原型:

int psci_cpu_suspend(unsigned int power_state, uintptr_t entrypoint, u_register_t context_id) 

power_state:请求状态,描述了本core所在拓扑的各level状态。

entrypoint:非安全世界时的返回地址,是一个物理地址。

context_id:仅当powerdown状态时有效。用于非安全世界指向保存上下文的指针。

power_state的编码格式分为original format和extended format,如下为original format(v1.0之前仅支持类型):

Bit field Description
31:26 Reserved. Must be zero.
25:24 PowerLevel Level 0: for cores Level 1: for clusters Level 2: for system
23:17 Reserved. Must be zero.
16 StateType Value 0: standby or retention Value 1: powerdown
15:0 StateID

CPU_OFF

Power down the calling core。用来动态关闭某个core,即hotplug。这个core可以使用CPU_ON命令再次power up。

函数原型:

int psci_cpu_off(void)

CPU_ON

Power up a core。这个API可以用来secondary boot某个core,也可以用来再次power up已经被CPU_OFF下电的core。CPU_ON被用来动态添加更多的core,即hotplug。

PSCI实现提供必要的特定于平台的代码来根据需要对电源控制器进行编程,以打开核心,在firmware初始化完成后,会跳转到指定的非安全世界入口地址执行。CPU_ON和CPU_OFF调用都只能来自Normal世界。如果一个Trusted OS或SP需要管理内核的数量,它需要使用一个Trusted OS或SP特定于IMPLEMENTATION DEFINED的通信与Normal世界来实现它的目标。

SYSTEM_OFF

SYSTEM_OFF提供了一个系统关机API。shutdown命令适用于OS看到的machine视角。OS必须在调用之前将所有cores置于已知状态。在SYSTEM_OFF调用中,firmware从最高电源级别移除电源。

OS必须完成一些操作来保证正常关机,比如确保cores处于已知状态、保存必要数据到non-volatile内存。

Compliance with the PSCI Specification

FUNCTION PSCI 0.2 PSCI 1.0 PSCI 1.1
PSCI_VERSION Mandatory Mandatory Mandatory
CPU_SUSPEND Mandatory Mandatory Mandatory
CPU_OFF Mandatory Mandatory Mandatory
CPU_ON Mandatory Mandatory Mandatory
AFFINITY_INFO Mandatory Mandatory Mandatory
MIGRATE Optional Optional Optional
MIGRATE_INFO_TYPE Optional(a) Optional(a) Optional(a)
MIGRATE_INFO_CPU Optional(b) Optional(b) Optional(b)
SYSTEM_OFF Mandatory Mandatory Mandatory
SYSTEM_RESET Mandatory Mandatory Mandatory
PSCI_FEATURES Not Applicable Mandatory Mandatory
CPU_FREEZE Not Applicable Optional Optional
CPU_DEFAULT_SUSPEND Not Applicable Optional Optional
NODE_HW_STATE Not Applicable Optional Optional
SYSTEM_SUSPEND Not Applicable Optional Optional
PSCI_SET_SUSPEND_MODE Not Applicable Optional Optional
PSCI_STAT_RESIDENCY Not Applicable Optional(c) Optional(c)
PSCI_STAT_COUNT Not Applicable Optional(c) Optional(c)
SYSTEM_RESET2 Not Applicable Not Applicable Optional
MEM_PROTECT Not Applicable Not Applicable Optional(d)
MEM_PROTECT_CHECK_RANGE Not Applicable Not Applicable Optional(d)

Reference

posted @ 2022-04-24 18:17  zephyr~  阅读(921)  评论(0编辑  收藏  举报