xref: /openbmc/linux/arch/arm64/kernel/cpuinfo.c (revision 4b7444ff)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Record and handle CPU attributes.
4  *
5  * Copyright (C) 2014 ARM Ltd.
6  */
7 #include <asm/arch_timer.h>
8 #include <asm/cache.h>
9 #include <asm/cpu.h>
10 #include <asm/cputype.h>
11 #include <asm/cpufeature.h>
12 #include <asm/fpsimd.h>
13 
14 #include <linux/bitops.h>
15 #include <linux/bug.h>
16 #include <linux/compat.h>
17 #include <linux/elf.h>
18 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/personality.h>
21 #include <linux/preempt.h>
22 #include <linux/printk.h>
23 #include <linux/seq_file.h>
24 #include <linux/sched.h>
25 #include <linux/smp.h>
26 #include <linux/delay.h>
27 
28 /*
29  * In case the boot CPU is hotpluggable, we record its initial state and
30  * current state separately. Certain system registers may contain different
31  * values depending on configuration at or after reset.
32  */
33 DEFINE_PER_CPU(struct cpuinfo_arm64, cpu_data);
34 static struct cpuinfo_arm64 boot_cpu_data;
35 
36 static inline const char *icache_policy_str(int l1ip)
37 {
38 	switch (l1ip) {
39 	case CTR_EL0_L1Ip_VPIPT:
40 		return "VPIPT";
41 	case CTR_EL0_L1Ip_VIPT:
42 		return "VIPT";
43 	case CTR_EL0_L1Ip_PIPT:
44 		return "PIPT";
45 	default:
46 		return "RESERVED/UNKNOWN";
47 	}
48 }
49 
50 unsigned long __icache_flags;
51 
52 static const char *const hwcap_str[] = {
53 	[KERNEL_HWCAP_FP]		= "fp",
54 	[KERNEL_HWCAP_ASIMD]		= "asimd",
55 	[KERNEL_HWCAP_EVTSTRM]		= "evtstrm",
56 	[KERNEL_HWCAP_AES]		= "aes",
57 	[KERNEL_HWCAP_PMULL]		= "pmull",
58 	[KERNEL_HWCAP_SHA1]		= "sha1",
59 	[KERNEL_HWCAP_SHA2]		= "sha2",
60 	[KERNEL_HWCAP_CRC32]		= "crc32",
61 	[KERNEL_HWCAP_ATOMICS]		= "atomics",
62 	[KERNEL_HWCAP_FPHP]		= "fphp",
63 	[KERNEL_HWCAP_ASIMDHP]		= "asimdhp",
64 	[KERNEL_HWCAP_CPUID]		= "cpuid",
65 	[KERNEL_HWCAP_ASIMDRDM]		= "asimdrdm",
66 	[KERNEL_HWCAP_JSCVT]		= "jscvt",
67 	[KERNEL_HWCAP_FCMA]		= "fcma",
68 	[KERNEL_HWCAP_LRCPC]		= "lrcpc",
69 	[KERNEL_HWCAP_DCPOP]		= "dcpop",
70 	[KERNEL_HWCAP_SHA3]		= "sha3",
71 	[KERNEL_HWCAP_SM3]		= "sm3",
72 	[KERNEL_HWCAP_SM4]		= "sm4",
73 	[KERNEL_HWCAP_ASIMDDP]		= "asimddp",
74 	[KERNEL_HWCAP_SHA512]		= "sha512",
75 	[KERNEL_HWCAP_SVE]		= "sve",
76 	[KERNEL_HWCAP_ASIMDFHM]		= "asimdfhm",
77 	[KERNEL_HWCAP_DIT]		= "dit",
78 	[KERNEL_HWCAP_USCAT]		= "uscat",
79 	[KERNEL_HWCAP_ILRCPC]		= "ilrcpc",
80 	[KERNEL_HWCAP_FLAGM]		= "flagm",
81 	[KERNEL_HWCAP_SSBS]		= "ssbs",
82 	[KERNEL_HWCAP_SB]		= "sb",
83 	[KERNEL_HWCAP_PACA]		= "paca",
84 	[KERNEL_HWCAP_PACG]		= "pacg",
85 	[KERNEL_HWCAP_DCPODP]		= "dcpodp",
86 	[KERNEL_HWCAP_SVE2]		= "sve2",
87 	[KERNEL_HWCAP_SVEAES]		= "sveaes",
88 	[KERNEL_HWCAP_SVEPMULL]		= "svepmull",
89 	[KERNEL_HWCAP_SVEBITPERM]	= "svebitperm",
90 	[KERNEL_HWCAP_SVESHA3]		= "svesha3",
91 	[KERNEL_HWCAP_SVESM4]		= "svesm4",
92 	[KERNEL_HWCAP_FLAGM2]		= "flagm2",
93 	[KERNEL_HWCAP_FRINT]		= "frint",
94 	[KERNEL_HWCAP_SVEI8MM]		= "svei8mm",
95 	[KERNEL_HWCAP_SVEF32MM]		= "svef32mm",
96 	[KERNEL_HWCAP_SVEF64MM]		= "svef64mm",
97 	[KERNEL_HWCAP_SVEBF16]		= "svebf16",
98 	[KERNEL_HWCAP_I8MM]		= "i8mm",
99 	[KERNEL_HWCAP_BF16]		= "bf16",
100 	[KERNEL_HWCAP_DGH]		= "dgh",
101 	[KERNEL_HWCAP_RNG]		= "rng",
102 	[KERNEL_HWCAP_BTI]		= "bti",
103 	[KERNEL_HWCAP_MTE]		= "mte",
104 	[KERNEL_HWCAP_ECV]		= "ecv",
105 	[KERNEL_HWCAP_AFP]		= "afp",
106 	[KERNEL_HWCAP_RPRES]		= "rpres",
107 	[KERNEL_HWCAP_MTE3]		= "mte3",
108 	[KERNEL_HWCAP_SME]		= "sme",
109 	[KERNEL_HWCAP_SME_I16I64]	= "smei16i64",
110 	[KERNEL_HWCAP_SME_F64F64]	= "smef64f64",
111 	[KERNEL_HWCAP_SME_I8I32]	= "smei8i32",
112 	[KERNEL_HWCAP_SME_F16F32]	= "smef16f32",
113 	[KERNEL_HWCAP_SME_B16F32]	= "smeb16f32",
114 	[KERNEL_HWCAP_SME_F32F32]	= "smef32f32",
115 	[KERNEL_HWCAP_SME_FA64]		= "smefa64",
116 	[KERNEL_HWCAP_WFXT]		= "wfxt",
117 	[KERNEL_HWCAP_EBF16]		= "ebf16",
118 };
119 
120 #ifdef CONFIG_COMPAT
121 #define COMPAT_KERNEL_HWCAP(x)	const_ilog2(COMPAT_HWCAP_ ## x)
122 static const char *const compat_hwcap_str[] = {
123 	[COMPAT_KERNEL_HWCAP(SWP)]	= "swp",
124 	[COMPAT_KERNEL_HWCAP(HALF)]	= "half",
125 	[COMPAT_KERNEL_HWCAP(THUMB)]	= "thumb",
126 	[COMPAT_KERNEL_HWCAP(26BIT)]	= NULL,	/* Not possible on arm64 */
127 	[COMPAT_KERNEL_HWCAP(FAST_MULT)] = "fastmult",
128 	[COMPAT_KERNEL_HWCAP(FPA)]	= NULL,	/* Not possible on arm64 */
129 	[COMPAT_KERNEL_HWCAP(VFP)]	= "vfp",
130 	[COMPAT_KERNEL_HWCAP(EDSP)]	= "edsp",
131 	[COMPAT_KERNEL_HWCAP(JAVA)]	= NULL,	/* Not possible on arm64 */
132 	[COMPAT_KERNEL_HWCAP(IWMMXT)]	= NULL,	/* Not possible on arm64 */
133 	[COMPAT_KERNEL_HWCAP(CRUNCH)]	= NULL,	/* Not possible on arm64 */
134 	[COMPAT_KERNEL_HWCAP(THUMBEE)]	= NULL,	/* Not possible on arm64 */
135 	[COMPAT_KERNEL_HWCAP(NEON)]	= "neon",
136 	[COMPAT_KERNEL_HWCAP(VFPv3)]	= "vfpv3",
137 	[COMPAT_KERNEL_HWCAP(VFPV3D16)]	= NULL,	/* Not possible on arm64 */
138 	[COMPAT_KERNEL_HWCAP(TLS)]	= "tls",
139 	[COMPAT_KERNEL_HWCAP(VFPv4)]	= "vfpv4",
140 	[COMPAT_KERNEL_HWCAP(IDIVA)]	= "idiva",
141 	[COMPAT_KERNEL_HWCAP(IDIVT)]	= "idivt",
142 	[COMPAT_KERNEL_HWCAP(VFPD32)]	= NULL,	/* Not possible on arm64 */
143 	[COMPAT_KERNEL_HWCAP(LPAE)]	= "lpae",
144 	[COMPAT_KERNEL_HWCAP(EVTSTRM)]	= "evtstrm",
145 };
146 
147 #define COMPAT_KERNEL_HWCAP2(x)	const_ilog2(COMPAT_HWCAP2_ ## x)
148 static const char *const compat_hwcap2_str[] = {
149 	[COMPAT_KERNEL_HWCAP2(AES)]	= "aes",
150 	[COMPAT_KERNEL_HWCAP2(PMULL)]	= "pmull",
151 	[COMPAT_KERNEL_HWCAP2(SHA1)]	= "sha1",
152 	[COMPAT_KERNEL_HWCAP2(SHA2)]	= "sha2",
153 	[COMPAT_KERNEL_HWCAP2(CRC32)]	= "crc32",
154 };
155 #endif /* CONFIG_COMPAT */
156 
157 static int c_show(struct seq_file *m, void *v)
158 {
159 	int i, j;
160 	bool compat = personality(current->personality) == PER_LINUX32;
161 
162 	for_each_online_cpu(i) {
163 		struct cpuinfo_arm64 *cpuinfo = &per_cpu(cpu_data, i);
164 		u32 midr = cpuinfo->reg_midr;
165 
166 		/*
167 		 * glibc reads /proc/cpuinfo to determine the number of
168 		 * online processors, looking for lines beginning with
169 		 * "processor".  Give glibc what it expects.
170 		 */
171 		seq_printf(m, "processor\t: %d\n", i);
172 		if (compat)
173 			seq_printf(m, "model name\t: ARMv8 Processor rev %d (%s)\n",
174 				   MIDR_REVISION(midr), COMPAT_ELF_PLATFORM);
175 
176 		seq_printf(m, "BogoMIPS\t: %lu.%02lu\n",
177 			   loops_per_jiffy / (500000UL/HZ),
178 			   loops_per_jiffy / (5000UL/HZ) % 100);
179 
180 		/*
181 		 * Dump out the common processor features in a single line.
182 		 * Userspace should read the hwcaps with getauxval(AT_HWCAP)
183 		 * rather than attempting to parse this, but there's a body of
184 		 * software which does already (at least for 32-bit).
185 		 */
186 		seq_puts(m, "Features\t:");
187 		if (compat) {
188 #ifdef CONFIG_COMPAT
189 			for (j = 0; j < ARRAY_SIZE(compat_hwcap_str); j++) {
190 				if (compat_elf_hwcap & (1 << j)) {
191 					/*
192 					 * Warn once if any feature should not
193 					 * have been present on arm64 platform.
194 					 */
195 					if (WARN_ON_ONCE(!compat_hwcap_str[j]))
196 						continue;
197 
198 					seq_printf(m, " %s", compat_hwcap_str[j]);
199 				}
200 			}
201 
202 			for (j = 0; j < ARRAY_SIZE(compat_hwcap2_str); j++)
203 				if (compat_elf_hwcap2 & (1 << j))
204 					seq_printf(m, " %s", compat_hwcap2_str[j]);
205 #endif /* CONFIG_COMPAT */
206 		} else {
207 			for (j = 0; j < ARRAY_SIZE(hwcap_str); j++)
208 				if (cpu_have_feature(j))
209 					seq_printf(m, " %s", hwcap_str[j]);
210 		}
211 		seq_puts(m, "\n");
212 
213 		seq_printf(m, "CPU implementer\t: 0x%02x\n",
214 			   MIDR_IMPLEMENTOR(midr));
215 		seq_printf(m, "CPU architecture: 8\n");
216 		seq_printf(m, "CPU variant\t: 0x%x\n", MIDR_VARIANT(midr));
217 		seq_printf(m, "CPU part\t: 0x%03x\n", MIDR_PARTNUM(midr));
218 		seq_printf(m, "CPU revision\t: %d\n\n", MIDR_REVISION(midr));
219 	}
220 
221 	return 0;
222 }
223 
224 static void *c_start(struct seq_file *m, loff_t *pos)
225 {
226 	return *pos < 1 ? (void *)1 : NULL;
227 }
228 
229 static void *c_next(struct seq_file *m, void *v, loff_t *pos)
230 {
231 	++*pos;
232 	return NULL;
233 }
234 
235 static void c_stop(struct seq_file *m, void *v)
236 {
237 }
238 
239 const struct seq_operations cpuinfo_op = {
240 	.start	= c_start,
241 	.next	= c_next,
242 	.stop	= c_stop,
243 	.show	= c_show
244 };
245 
246 
247 static struct kobj_type cpuregs_kobj_type = {
248 	.sysfs_ops = &kobj_sysfs_ops,
249 };
250 
251 /*
252  * The ARM ARM uses the phrase "32-bit register" to describe a register
253  * whose upper 32 bits are RES0 (per C5.1.1, ARM DDI 0487A.i), however
254  * no statement is made as to whether the upper 32 bits will or will not
255  * be made use of in future, and between ARM DDI 0487A.c and ARM DDI
256  * 0487A.d CLIDR_EL1 was expanded from 32-bit to 64-bit.
257  *
258  * Thus, while both MIDR_EL1 and REVIDR_EL1 are described as 32-bit
259  * registers, we expose them both as 64 bit values to cater for possible
260  * future expansion without an ABI break.
261  */
262 #define kobj_to_cpuinfo(kobj)	container_of(kobj, struct cpuinfo_arm64, kobj)
263 #define CPUREGS_ATTR_RO(_name, _field)						\
264 	static ssize_t _name##_show(struct kobject *kobj,			\
265 			struct kobj_attribute *attr, char *buf)			\
266 	{									\
267 		struct cpuinfo_arm64 *info = kobj_to_cpuinfo(kobj);		\
268 										\
269 		if (info->reg_midr)						\
270 			return sprintf(buf, "0x%016llx\n", info->reg_##_field);	\
271 		else								\
272 			return 0;						\
273 	}									\
274 	static struct kobj_attribute cpuregs_attr_##_name = __ATTR_RO(_name)
275 
276 CPUREGS_ATTR_RO(midr_el1, midr);
277 CPUREGS_ATTR_RO(revidr_el1, revidr);
278 CPUREGS_ATTR_RO(smidr_el1, smidr);
279 
280 static struct attribute *cpuregs_id_attrs[] = {
281 	&cpuregs_attr_midr_el1.attr,
282 	&cpuregs_attr_revidr_el1.attr,
283 	NULL
284 };
285 
286 static const struct attribute_group cpuregs_attr_group = {
287 	.attrs = cpuregs_id_attrs,
288 	.name = "identification"
289 };
290 
291 static struct attribute *sme_cpuregs_id_attrs[] = {
292 	&cpuregs_attr_smidr_el1.attr,
293 	NULL
294 };
295 
296 static const struct attribute_group sme_cpuregs_attr_group = {
297 	.attrs = sme_cpuregs_id_attrs,
298 	.name = "identification"
299 };
300 
301 static int cpuid_cpu_online(unsigned int cpu)
302 {
303 	int rc;
304 	struct device *dev;
305 	struct cpuinfo_arm64 *info = &per_cpu(cpu_data, cpu);
306 
307 	dev = get_cpu_device(cpu);
308 	if (!dev) {
309 		rc = -ENODEV;
310 		goto out;
311 	}
312 	rc = kobject_add(&info->kobj, &dev->kobj, "regs");
313 	if (rc)
314 		goto out;
315 	rc = sysfs_create_group(&info->kobj, &cpuregs_attr_group);
316 	if (rc)
317 		kobject_del(&info->kobj);
318 	if (system_supports_sme())
319 		rc = sysfs_merge_group(&info->kobj, &sme_cpuregs_attr_group);
320 out:
321 	return rc;
322 }
323 
324 static int cpuid_cpu_offline(unsigned int cpu)
325 {
326 	struct device *dev;
327 	struct cpuinfo_arm64 *info = &per_cpu(cpu_data, cpu);
328 
329 	dev = get_cpu_device(cpu);
330 	if (!dev)
331 		return -ENODEV;
332 	if (info->kobj.parent) {
333 		sysfs_remove_group(&info->kobj, &cpuregs_attr_group);
334 		kobject_del(&info->kobj);
335 	}
336 
337 	return 0;
338 }
339 
340 static int __init cpuinfo_regs_init(void)
341 {
342 	int cpu, ret;
343 
344 	for_each_possible_cpu(cpu) {
345 		struct cpuinfo_arm64 *info = &per_cpu(cpu_data, cpu);
346 
347 		kobject_init(&info->kobj, &cpuregs_kobj_type);
348 	}
349 
350 	ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "arm64/cpuinfo:online",
351 				cpuid_cpu_online, cpuid_cpu_offline);
352 	if (ret < 0) {
353 		pr_err("cpuinfo: failed to register hotplug callbacks.\n");
354 		return ret;
355 	}
356 	return 0;
357 }
358 device_initcall(cpuinfo_regs_init);
359 
360 static void cpuinfo_detect_icache_policy(struct cpuinfo_arm64 *info)
361 {
362 	unsigned int cpu = smp_processor_id();
363 	u32 l1ip = CTR_L1IP(info->reg_ctr);
364 
365 	switch (l1ip) {
366 	case CTR_EL0_L1Ip_PIPT:
367 		break;
368 	case CTR_EL0_L1Ip_VPIPT:
369 		set_bit(ICACHEF_VPIPT, &__icache_flags);
370 		break;
371 	case CTR_EL0_L1Ip_VIPT:
372 	default:
373 		/* Assume aliasing */
374 		set_bit(ICACHEF_ALIASING, &__icache_flags);
375 		break;
376 	}
377 
378 	pr_info("Detected %s I-cache on CPU%d\n", icache_policy_str(l1ip), cpu);
379 }
380 
381 static void __cpuinfo_store_cpu_32bit(struct cpuinfo_32bit *info)
382 {
383 	info->reg_id_dfr0 = read_cpuid(ID_DFR0_EL1);
384 	info->reg_id_dfr1 = read_cpuid(ID_DFR1_EL1);
385 	info->reg_id_isar0 = read_cpuid(ID_ISAR0_EL1);
386 	info->reg_id_isar1 = read_cpuid(ID_ISAR1_EL1);
387 	info->reg_id_isar2 = read_cpuid(ID_ISAR2_EL1);
388 	info->reg_id_isar3 = read_cpuid(ID_ISAR3_EL1);
389 	info->reg_id_isar4 = read_cpuid(ID_ISAR4_EL1);
390 	info->reg_id_isar5 = read_cpuid(ID_ISAR5_EL1);
391 	info->reg_id_isar6 = read_cpuid(ID_ISAR6_EL1);
392 	info->reg_id_mmfr0 = read_cpuid(ID_MMFR0_EL1);
393 	info->reg_id_mmfr1 = read_cpuid(ID_MMFR1_EL1);
394 	info->reg_id_mmfr2 = read_cpuid(ID_MMFR2_EL1);
395 	info->reg_id_mmfr3 = read_cpuid(ID_MMFR3_EL1);
396 	info->reg_id_mmfr4 = read_cpuid(ID_MMFR4_EL1);
397 	info->reg_id_mmfr5 = read_cpuid(ID_MMFR5_EL1);
398 	info->reg_id_pfr0 = read_cpuid(ID_PFR0_EL1);
399 	info->reg_id_pfr1 = read_cpuid(ID_PFR1_EL1);
400 	info->reg_id_pfr2 = read_cpuid(ID_PFR2_EL1);
401 
402 	info->reg_mvfr0 = read_cpuid(MVFR0_EL1);
403 	info->reg_mvfr1 = read_cpuid(MVFR1_EL1);
404 	info->reg_mvfr2 = read_cpuid(MVFR2_EL1);
405 }
406 
407 static void __cpuinfo_store_cpu(struct cpuinfo_arm64 *info)
408 {
409 	info->reg_cntfrq = arch_timer_get_cntfrq();
410 	/*
411 	 * Use the effective value of the CTR_EL0 than the raw value
412 	 * exposed by the CPU. CTR_EL0.IDC field value must be interpreted
413 	 * with the CLIDR_EL1 fields to avoid triggering false warnings
414 	 * when there is a mismatch across the CPUs. Keep track of the
415 	 * effective value of the CTR_EL0 in our internal records for
416 	 * accurate sanity check and feature enablement.
417 	 */
418 	info->reg_ctr = read_cpuid_effective_cachetype();
419 	info->reg_dczid = read_cpuid(DCZID_EL0);
420 	info->reg_midr = read_cpuid_id();
421 	info->reg_revidr = read_cpuid(REVIDR_EL1);
422 
423 	info->reg_id_aa64dfr0 = read_cpuid(ID_AA64DFR0_EL1);
424 	info->reg_id_aa64dfr1 = read_cpuid(ID_AA64DFR1_EL1);
425 	info->reg_id_aa64isar0 = read_cpuid(ID_AA64ISAR0_EL1);
426 	info->reg_id_aa64isar1 = read_cpuid(ID_AA64ISAR1_EL1);
427 	info->reg_id_aa64isar2 = read_cpuid(ID_AA64ISAR2_EL1);
428 	info->reg_id_aa64mmfr0 = read_cpuid(ID_AA64MMFR0_EL1);
429 	info->reg_id_aa64mmfr1 = read_cpuid(ID_AA64MMFR1_EL1);
430 	info->reg_id_aa64mmfr2 = read_cpuid(ID_AA64MMFR2_EL1);
431 	info->reg_id_aa64pfr0 = read_cpuid(ID_AA64PFR0_EL1);
432 	info->reg_id_aa64pfr1 = read_cpuid(ID_AA64PFR1_EL1);
433 	info->reg_id_aa64zfr0 = read_cpuid(ID_AA64ZFR0_EL1);
434 	info->reg_id_aa64smfr0 = read_cpuid(ID_AA64SMFR0_EL1);
435 
436 	if (id_aa64pfr1_mte(info->reg_id_aa64pfr1))
437 		info->reg_gmid = read_cpuid(GMID_EL1);
438 
439 	if (id_aa64pfr0_32bit_el0(info->reg_id_aa64pfr0))
440 		__cpuinfo_store_cpu_32bit(&info->aarch32);
441 
442 	cpuinfo_detect_icache_policy(info);
443 }
444 
445 void cpuinfo_store_cpu(void)
446 {
447 	struct cpuinfo_arm64 *info = this_cpu_ptr(&cpu_data);
448 	__cpuinfo_store_cpu(info);
449 	update_cpu_features(smp_processor_id(), info, &boot_cpu_data);
450 }
451 
452 void __init cpuinfo_store_boot_cpu(void)
453 {
454 	struct cpuinfo_arm64 *info = &per_cpu(cpu_data, 0);
455 	__cpuinfo_store_cpu(info);
456 
457 	boot_cpu_data = *info;
458 	init_cpu_features(&boot_cpu_data);
459 }
460