20.4 SMP多核启动以及CPU热插拔驱动
在Linux系统中,对于多核的ARM芯片,在Bootrom代码中,每个CPU都会识别自身ID,如果ID是0,则引导Bootloader和Linux内核执行,如果ID不是0,则Bootrom一般在上电时将自身置于WFI(WaitFortInerrupt)或者WFE(WaitForEvent)状态,并等待CPU0给其发CPU核间中断或事件(一般通过SEV指令)以唤醒它。一个典型的多核Linux启动过程如图20.6所示。
图20.6 一个典型的多核Linux启动过程
被CPU0唤醒的CPUn可以在运行过程中进行热插拔,如运行如下命令即可卸载CPU1,并且将CPU1上的任务全部迁移到其他CPU中:
echo 0 > /sys/devices/system/cpu/cpu1/online
同理,运行如下命令可以再次启动CPU1:
echo 1 > /sys/devices/system/cpu/cpu1/online
之后CPU1会主动参与系统中各个CPU之间要运行任务的负载均衡工作。
CPU0唤醒其他CPU的动作在内核中被封装为一个smp_operations的结构体,对于ARM而言,定义于
arch/arm/include/asm/smp.h中。该结构体的成员函数如代码清单20.8所示。
代码清单20.8 smp_operations结构体
struct smp_operations {
#ifdef CONFIG_SMP
/*
* Setup the set of possible CPUs (via set_cpu_possible)
*/
void (*smp_init_cpus)(void);
/*
* Initialize cpu_possible map, and enable coherency
*/
void (*smp_prepare_cpus)(unsigned int max_cpus);
/*
* Perform platform specific initialisation of the specified CPU.
*/
void (*smp_secondary_init)(unsigned int cpu);
/*
* Boot a secondary CPU, and assign it the specified idle task.
* This also gives us the initial stack to use for this CPU.
*/
int (*smp_boot_secondary)(unsigned int cpu, struct task_struct *idle);
#ifdef CONFIG_HOTPLUG_CPU
int (*cpu_kill)(unsigned int cpu);
void (*cpu_die)(unsigned int cpu);
bool (*cpu_can_disable)(unsigned int cpu);
int (*cpu_disable)(unsigned int cpu);
#endif
#endif
}