linux read_cpuid

这段代码展示了如何封装汇编指令以读取CPU的只读寄存器,如MIDR_EL1和MPIDR_EL1,用于获取CPU标识和多核配置。在初始化SMP(对称多处理)时,这些信息用于设置处理器ID和逻辑映射。read_cpuid系列函数确保了在运行时获取常量CPUID,并提供了检查缓存类型的辅助函数。
摘要由CSDN通过智能技术生成

#define read_cpuid(reg) ({                      \
    u64 __val;                          \
    asm("mrs    %0, " #reg : "=r" (__val));         \
    __val;                              \
})

主要读取一些只读寄存器,封装如下

#ifndef __ASSEMBLY__

/*
 * The CPU ID never changes at run time, so we might as well tell the
 * compiler that it's constant.  Use this function to read the CPU ID
 * rather than directly reading processor_id or read_cpuid() directly.
 */
static inline u32 __attribute_const__ read_cpuid_id(void)
{
    return read_cpuid(MIDR_EL1);
}

static inline u64 __attribute_const__ read_cpuid_mpidr(void)-----------经常使用
{
    return read_cpuid(MPIDR_EL1);
}

static inline unsigned int __attribute_const__ read_cpuid_implementor(void)
{
    return MIDR_IMPLEMENTOR(read_cpuid_id());
}

static inline unsigned int __attribute_const__ read_cpuid_part_number(void)
{
    return MIDR_PARTNUM(read_cpuid_id());
}

static inline u32 __attribute_const__ read_cpuid_cachetype(void)
{
    return read_cpuid(CTR_EL0);
}

static inline bool id_aa64mmfr0_mixed_endian_el0(u64 mmfr0)
{
    return (ID_AA64MMFR0_BIGEND(mmfr0) == 0x1) ||
        (ID_AA64MMFR0_BIGENDEL0(mmfr0) == 0x1);
}
#endif /* __ASSEMBLY__ */

封装后的函数使用如下

void __init smp_setup_processor_id(void)
{
    u64 mpidr = read_cpuid_mpidr() & MPIDR_HWID_BITMASK;
    cpu_logical_map(0) = mpidr;

    /*
     * clear __my_cpu_offset on boot CPU to avoid hang caused by
     * using percpu variable early, for example, lockdep will
     * access percpu variable inside lock_release
     */
    set_my_cpu_offset(0);
    pr_info("Booting Linux on physical CPU 0x%lx\n", (unsigned long)mpidr);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值