1 /*
2 * Copyright (C) 2012 ARM Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16 #ifndef __ASM_CPUTYPE_H
17 #define __ASM_CPUTYPE_H
18
19 #define INVALID_HWID ULONG_MAX
20
21 #define MPIDR_UP_BITMASK (0x1 << 30)
22 #define MPIDR_MT_BITMASK (0x1 << 24)
23 #define MPIDR_HWID_BITMASK 0xff00ffffff
24
25 #define MPIDR_LEVEL_BITS_SHIFT 3
26 #define MPIDR_LEVEL_BITS (1 << MPIDR_LEVEL_BITS_SHIFT)
27 #define MPIDR_LEVEL_MASK ((1 << MPIDR_LEVEL_BITS) - 1)
28
29 #define MPIDR_LEVEL_SHIFT(level) \
30 (((1 << level) >> 1) << MPIDR_LEVEL_BITS_SHIFT)
31
32 #define MPIDR_AFFINITY_LEVEL(mpidr, level) \
33 ((mpidr >> MPIDR_LEVEL_SHIFT(level)) & MPIDR_LEVEL_MASK)
34
35 #define MIDR_REVISION_MASK 0xf
36 #define MIDR_REVISION(midr) ((midr) & MIDR_REVISION_MASK)
37 #define MIDR_PARTNUM_SHIFT 4
38 #define MIDR_PARTNUM_MASK (0xfff << MIDR_PARTNUM_SHIFT)
39 #define MIDR_PARTNUM(midr) \
40 (((midr) & MIDR_PARTNUM_MASK) >> MIDR_PARTNUM_SHIFT)
41 #define MIDR_ARCHITECTURE_SHIFT 16
42 #define MIDR_ARCHITECTURE_MASK (0xf << MIDR_ARCHITECTURE_SHIFT)
43 #define MIDR_ARCHITECTURE(midr) \
44 (((midr) & MIDR_ARCHITECTURE_MASK) >> MIDR_ARCHITECTURE_SHIFT)
45 #define MIDR_VARIANT_SHIFT 20
46 #define MIDR_VARIANT_MASK (0xf << MIDR_VARIANT_SHIFT)
47 #define MIDR_VARIANT(midr) \
48 (((midr) & MIDR_VARIANT_MASK) >> MIDR_VARIANT_SHIFT)
49 #define MIDR_IMPLEMENTOR_SHIFT 24
50 #define MIDR_IMPLEMENTOR_MASK (0xff << MIDR_IMPLEMENTOR_SHIFT)
51 #define MIDR_IMPLEMENTOR(midr) \
52 (((midr) & MIDR_IMPLEMENTOR_MASK) >> MIDR_IMPLEMENTOR_SHIFT)
53
54 #define MIDR_CPU_MODEL(imp, partnum) \
55 (((imp) << MIDR_IMPLEMENTOR_SHIFT) | \
56 (0xf << MIDR_ARCHITECTURE_SHIFT) | \
57 ((partnum) << MIDR_PARTNUM_SHIFT))
58
59 #define MIDR_CPU_VAR_REV(var, rev) \
60 (((var) << MIDR_VARIANT_SHIFT) | (rev))
61
62 #define MIDR_CPU_MODEL_MASK (MIDR_IMPLEMENTOR_MASK | MIDR_PARTNUM_MASK | \
63 MIDR_ARCHITECTURE_MASK)
64
65 #define MIDR_IS_CPU_MODEL_RANGE(midr, model, rv_min, rv_max) \
66 ({ \
67 u32 _model = (midr) & MIDR_CPU_MODEL_MASK; \
68 u32 rv = (midr) & (MIDR_REVISION_MASK | MIDR_VARIANT_MASK); \
69 \
70 _model == (model) && rv >= (rv_min) && rv <= (rv_max); \
71 })
72
73 #define ARM_CPU_IMP_ARM 0x41
74 #define ARM_CPU_IMP_APM 0x50
75 #define ARM_CPU_IMP_CAVIUM 0x43
76 #define ARM_CPU_IMP_BRCM 0x42
77 #define ARM_CPU_IMP_QCOM 0x51
78 #define ARM_CPU_IMP_NVIDIA 0x4E
79
80 #define ARM_CPU_PART_AEM_V8 0xD0F
81 #define ARM_CPU_PART_FOUNDATION 0xD00
82 #define ARM_CPU_PART_CORTEX_A55 0xD05
83 #define ARM_CPU_PART_CORTEX_A57 0xD07
84 #define ARM_CPU_PART_CORTEX_A72 0xD08
85 #define ARM_CPU_PART_CORTEX_A53 0xD03
86 #define ARM_CPU_PART_CORTEX_A73 0xD09
87 #define ARM_CPU_PART_CORTEX_A75 0xD0A
88 #define ARM_CPU_PART_CORTEX_A35 0xD04
89 #define ARM_CPU_PART_CORTEX_A55 0xD05
90
91 #define APM_CPU_PART_POTENZA 0x000
92
93 #define CAVIUM_CPU_PART_THUNDERX 0x0A1
94 #define CAVIUM_CPU_PART_THUNDERX_81XX 0x0A2
95 #define CAVIUM_CPU_PART_THUNDERX_83XX 0x0A3
96 #define CAVIUM_CPU_PART_THUNDERX2 0x0AF
97
98 #define BRCM_CPU_PART_VULCAN 0x516
99
100 #define QCOM_CPU_PART_FALKOR_V1 0x800
101 #define QCOM_CPU_PART_FALKOR 0xC00
102 #define QCOM_CPU_PART_KRYO 0x200
103
104 #define NVIDIA_CPU_PART_DENVER 0x003
105 #define NVIDIA_CPU_PART_CARMEL 0x004
106
107 #define MIDR_CORTEX_A53 MIDR_CPU_MODEL(ARM_CPU_IMP_ARM, ARM_CPU_PART_CORTEX_A53)
108 #define MIDR_CORTEX_A55 MIDR_CPU_MODEL(ARM_CPU_IMP_ARM, ARM_CPU_PART_CORTEX_A55)
109 #define MIDR_CORTEX_A57 MIDR_CPU_MODEL(ARM_CPU_IMP_ARM, ARM_CPU_PART_CORTEX_A57)
110 #define MIDR_CORTEX_A72 MIDR_CPU_MODEL(ARM_CPU_IMP_ARM, ARM_CPU_PART_CORTEX_A72)
111 #define MIDR_CORTEX_A73 MIDR_CPU_MODEL(ARM_CPU_IMP_ARM, ARM_CPU_PART_CORTEX_A73)
112 #define MIDR_CORTEX_A75 MIDR_CPU_MODEL(ARM_CPU_IMP_ARM, ARM_CPU_PART_CORTEX_A75)
113 #define MIDR_CORTEX_A35 MIDR_CPU_MODEL(ARM_CPU_IMP_ARM, ARM_CPU_PART_CORTEX_A35)
114 #define MIDR_CORTEX_A55 MIDR_CPU_MODEL(ARM_CPU_IMP_ARM, ARM_CPU_PART_CORTEX_A55)
115 #define MIDR_THUNDERX MIDR_CPU_MODEL(ARM_CPU_IMP_CAVIUM, CAVIUM_CPU_PART_THUNDERX)
116 #define MIDR_THUNDERX_81XX MIDR_CPU_MODEL(ARM_CPU_IMP_CAVIUM, CAVIUM_CPU_PART_THUNDERX_81XX)
117 #define MIDR_THUNDERX_83XX MIDR_CPU_MODEL(ARM_CPU_IMP_CAVIUM, CAVIUM_CPU_PART_THUNDERX_83XX)
118 #define MIDR_CAVIUM_THUNDERX2 MIDR_CPU_MODEL(ARM_CPU_IMP_CAVIUM, CAVIUM_CPU_PART_THUNDERX2)
119 #define MIDR_BRCM_VULCAN MIDR_CPU_MODEL(ARM_CPU_IMP_BRCM, BRCM_CPU_PART_VULCAN)
120 #define MIDR_QCOM_FALKOR_V1 MIDR_CPU_MODEL(ARM_CPU_IMP_QCOM, QCOM_CPU_PART_FALKOR_V1)
121 #define MIDR_QCOM_FALKOR MIDR_CPU_MODEL(ARM_CPU_IMP_QCOM, QCOM_CPU_PART_FALKOR)
122 #define MIDR_QCOM_KRYO MIDR_CPU_MODEL(ARM_CPU_IMP_QCOM, QCOM_CPU_PART_KRYO)
123 #define MIDR_NVIDIA_DENVER MIDR_CPU_MODEL(ARM_CPU_IMP_NVIDIA, NVIDIA_CPU_PART_DENVER)
124 #define MIDR_NVIDIA_CARMEL MIDR_CPU_MODEL(ARM_CPU_IMP_NVIDIA, NVIDIA_CPU_PART_CARMEL)
125
126 #ifndef __ASSEMBLY__
127
128 #include <asm/sysreg.h>
129
130 #define read_cpuid(reg) read_sysreg_s(SYS_ ## reg)
131
132 /*
133 * Represent a range of MIDR values for a given CPU model and a
134 * range of variant/revision values.
135 *
136 * @model - CPU model as defined by MIDR_CPU_MODEL
137 * @rv_min - Minimum value for the revision/variant as defined by
138 * MIDR_CPU_VAR_REV
139 * @rv_max - Maximum value for the variant/revision for the range.
140 */
141 struct midr_range {
142 u32 model;
143 u32 rv_min;
144 u32 rv_max;
145 };
146
147 #define MIDR_RANGE(m, v_min, r_min, v_max, r_max) \
148 { \
149 .model = m, \
150 .rv_min = MIDR_CPU_VAR_REV(v_min, r_min), \
151 .rv_max = MIDR_CPU_VAR_REV(v_max, r_max), \
152 }
153
154 #define MIDR_ALL_VERSIONS(m) MIDR_RANGE(m, 0, 0, 0xf, 0xf)
155
is_midr_in_range(u32 midr,struct midr_range const * range)156 static inline bool is_midr_in_range(u32 midr, struct midr_range const *range)
157 {
158 return MIDR_IS_CPU_MODEL_RANGE(midr, range->model,
159 range->rv_min, range->rv_max);
160 }
161
162 static inline bool
is_midr_in_range_list(u32 midr,struct midr_range const * ranges)163 is_midr_in_range_list(u32 midr, struct midr_range const *ranges)
164 {
165 while (ranges->model)
166 if (is_midr_in_range(midr, ranges++))
167 return true;
168 return false;
169 }
170
171 /*
172 * The CPU ID never changes at run time, so we might as well tell the
173 * compiler that it's constant. Use this function to read the CPU ID
174 * rather than directly reading processor_id or read_cpuid() directly.
175 */
read_cpuid_id(void)176 static inline u32 __attribute_const__ read_cpuid_id(void)
177 {
178 return read_cpuid(MIDR_EL1);
179 }
180
read_cpuid_mpidr(void)181 static inline u64 __attribute_const__ read_cpuid_mpidr(void)
182 {
183 return read_cpuid(MPIDR_EL1);
184 }
185
read_cpuid_implementor(void)186 static inline unsigned int __attribute_const__ read_cpuid_implementor(void)
187 {
188 return MIDR_IMPLEMENTOR(read_cpuid_id());
189 }
190
read_cpuid_part_number(void)191 static inline unsigned int __attribute_const__ read_cpuid_part_number(void)
192 {
193 return MIDR_PARTNUM(read_cpuid_id());
194 }
195
read_cpuid_cachetype(void)196 static inline u32 __attribute_const__ read_cpuid_cachetype(void)
197 {
198 return read_cpuid(CTR_EL0);
199 }
200 #endif /* __ASSEMBLY__ */
201
202 #endif
203