Yuhang Zheng

ARM平台获取CPUID的方法

N 人看过

目前工作中需要获取LS10xx平台的CPUID,然后网上搜到了一些方法,在这里先简单记录一下。

CPUID寄存器内容:

字段名:Implementer(venter 销售ID)|Variant(大版本号) | Architecture(架构版本)| Part Num(产品代码)|Revision(小版本号)

基址偏移量: [31-24] | [23-20] | [19-16] | [15-4] | [3-0]

下面直接上操作,以LS1046为例

1、使用cat /proc/cpuinfo命令可以看到

root@localhost:~# cat /proc/cpuinfo
processor       : 0
BogoMIPS        : 50.00
Features        : fp asimd evtstrm aes pmull sha1 sha2 crc32 cpuid
CPU implementer : 0x41
CPU architecture: 8
CPU variant     : 0x0
CPU part        : 0xd08
CPU revision    : 2

processor       : 1
BogoMIPS        : 50.00
Features        : fp asimd evtstrm aes pmull sha1 sha2 crc32 cpuid
CPU implementer : 0x41
CPU architecture: 8
CPU variant     : 0x0
CPU part        : 0xd08
CPU revision    : 2

processor       : 2
BogoMIPS        : 50.00
Features        : fp asimd evtstrm aes pmull sha1 sha2 crc32 cpuid
CPU implementer : 0x41
CPU architecture: 8
CPU variant     : 0x0
CPU part        : 0xd08
CPU revision    : 2

processor       : 3
BogoMIPS        : 50.00
Features        : fp asimd evtstrm aes pmull sha1 sha2 crc32 cpuid
CPU implementer : 0x41
CPU architecture: 8
CPU variant     : 0x0
CPU part        : 0xd08
CPU revision    : 2

按照上面的规则换算下来就是

Implementer        |Variant    | Architecture    |Part Num    |Revision
0x41            0            f                d08            2

2、通过命令也可以看到

root@localhost:~# cat /sys/devices/system/cpu/cpu0/regs/identification/midr_el1
0x00000000410fd082

3、采用辅助向量读取寄存器中的CPU信息(AT_HWCAP,有这个向量的话,位与HWCAP_CPUID(值是0x0800)为真即可以访问寄存器)

查看辅助变量条目:LD_SHOW_AUXV=1 sleep 1000

以下是官方例程,位置在内核源码的./Documentation/arm64/cpu-feature-registers.txt

/*
 * Sample program to demonstrate the MRS emulation ABI.
 *
 * Copyright (C) 2015-2016, ARM Ltd
 *
 * Author: Suzuki K Poulose <suzuki.poulose@arm.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <asm/hwcap.h>
#include <stdio.h>
#include <sys/auxv.h>

#define get_cpu_ftr(id) ({                                      \
                unsigned long __val;                            \
                asm("mrs %0, "#id : "=r" (__val));              \
                printf("%-20s: 0x%016lx\n", #id, __val);        \
        })

int main(void)
{

        if (!(getauxval(AT_HWCAP) & HWCAP_CPUID)) {
                fputs("CPUID registers unavailable\n", stderr);
                return 1;
        }

        get_cpu_ftr(ID_AA64ISAR0_EL1);
        get_cpu_ftr(ID_AA64ISAR1_EL1);
        get_cpu_ftr(ID_AA64MMFR0_EL1);
        get_cpu_ftr(ID_AA64MMFR1_EL1);
        get_cpu_ftr(ID_AA64PFR0_EL1);
        get_cpu_ftr(ID_AA64PFR1_EL1);
        get_cpu_ftr(ID_AA64DFR0_EL1);
        get_cpu_ftr(ID_AA64DFR1_EL1);

        get_cpu_ftr(MIDR_EL1);
        get_cpu_ftr(MPIDR_EL1);
        get_cpu_ftr(REVIDR_EL1);

#if 0
        /* Unexposed register access causes SIGILL */
        get_cpu_ftr(ID_MMFR0_EL1);
#endif

        return 0;
}

编译之后运行得到的结果是:

root@localhost:~# ./cpuid
ID_AA64ISAR0_EL1    : 0x0000000000011120
ID_AA64ISAR1_EL1    : 0x0000000000000000
ID_AA64MMFR0_EL1    : 0x00000000ff000000
ID_AA64MMFR1_EL1    : 0x0000000000000000
ID_AA64PFR0_EL1     : 0x0000000000000011
ID_AA64PFR1_EL1     : 0x0000000000000000
ID_AA64DFR0_EL1     : 0x0000000000000006
ID_AA64DFR1_EL1     : 0x0000000000000000
MIDR_EL1            : 0x00000000410fd082
MPIDR_EL1           : 0x0000000080000000
REVIDR_EL1          : 0x0000000000000000

同样可以得到结果