2 * Record and handle CPU attributes.
4 * Copyright (C) 2014 ARM Ltd.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include <asm/arch_timer.h>
18 #include <asm/cachetype.h>
20 #include <asm/cputype.h>
21 #include <asm/cpufeature.h>
23 #include <linux/bitops.h>
24 #include <linux/bug.h>
25 #include <linux/compat.h>
26 #include <linux/elf.h>
27 #include <linux/init.h>
28 #include <linux/kernel.h>
29 #include <linux/personality.h>
30 #include <linux/preempt.h>
31 #include <linux/printk.h>
32 #include <linux/seq_file.h>
33 #include <linux/sched.h>
34 #include <linux/smp.h>
35 #include <linux/delay.h>
36 #ifdef CONFIG_AMLOGIC_CPU_INFO
37 #include <linux/amlogic/cpu_version.h>
40 #ifdef CONFIG_ARCH_MESON64_ODROID_COMMON
41 #include <linux/amlogic/efuse.h>
45 * In case the boot CPU is hotpluggable, we record its initial state and
46 * current state separately. Certain system registers may contain different
47 * values depending on configuration at or after reset.
49 DEFINE_PER_CPU(struct cpuinfo_arm64, cpu_data);
50 static struct cpuinfo_arm64 boot_cpu_data;
52 static char *icache_policy_str[] = {
53 [ICACHE_POLICY_RESERVED] = "RESERVED/UNKNOWN",
54 [ICACHE_POLICY_AIVIVT] = "AIVIVT",
55 [ICACHE_POLICY_VIPT] = "VIPT",
56 [ICACHE_POLICY_PIPT] = "PIPT",
59 unsigned long __icache_flags;
61 static const char *const hwcap_str[] = {
77 static const char *const compat_hwcap_str[] = {
103 static const char *const compat_hwcap2_str[] = {
111 #endif /* CONFIG_COMPAT */
113 static int c_show(struct seq_file *m, void *v)
116 bool compat = personality(current->personality) == PER_LINUX32;
117 #ifdef CONFIG_AMLOGIC_CPU_INFO
118 unsigned char chipid[CHIPID_LEN];
121 for_each_online_cpu(i) {
122 struct cpuinfo_arm64 *cpuinfo = &per_cpu(cpu_data, i);
123 u32 midr = cpuinfo->reg_midr;
126 * glibc reads /proc/cpuinfo to determine the number of
127 * online processors, looking for lines beginning with
128 * "processor". Give glibc what it expects.
130 seq_printf(m, "processor\t: %d\n", i);
132 seq_printf(m, "model name\t: ARMv8 Processor rev %d (%s)\n",
133 MIDR_REVISION(midr), COMPAT_ELF_PLATFORM);
135 seq_printf(m, "BogoMIPS\t: %lu.%02lu\n",
136 loops_per_jiffy / (500000UL/HZ),
137 loops_per_jiffy / (5000UL/HZ) % 100);
140 * Dump out the common processor features in a single line.
141 * Userspace should read the hwcaps with getauxval(AT_HWCAP)
142 * rather than attempting to parse this, but there's a body of
143 * software which does already (at least for 32-bit).
145 seq_puts(m, "Features\t:");
148 for (j = 0; compat_hwcap_str[j]; j++)
149 if (compat_elf_hwcap & (1 << j))
150 seq_printf(m, " %s", compat_hwcap_str[j]);
152 for (j = 0; compat_hwcap2_str[j]; j++)
153 if (compat_elf_hwcap2 & (1 << j))
154 seq_printf(m, " %s", compat_hwcap2_str[j]);
155 #endif /* CONFIG_COMPAT */
157 for (j = 0; hwcap_str[j]; j++)
158 if (elf_hwcap & (1 << j))
159 seq_printf(m, " %s", hwcap_str[j]);
163 seq_printf(m, "CPU implementer\t: 0x%02x\n",
164 MIDR_IMPLEMENTOR(midr));
165 seq_printf(m, "CPU architecture: 8\n");
166 seq_printf(m, "CPU variant\t: 0x%x\n", MIDR_VARIANT(midr));
167 seq_printf(m, "CPU part\t: 0x%03x\n", MIDR_PARTNUM(midr));
168 seq_printf(m, "CPU revision\t: %d\n\n", MIDR_REVISION(midr));
170 #ifdef CONFIG_ARCH_MESON64_ODROID_COMMON
171 #ifdef CONFIG_AMLOGIC_CPU_INFO
172 cpuinfo_get_chipid(chipid, CHIPID_LEN);
173 seq_puts(m, "CPU info\t: ");
174 for (i = 0; i < 16; i++)
175 seq_printf(m, "%02x", chipid[i]);
183 seq_puts(m, "Serial\t\t: ");
185 ret = efuse_read_usr(uuid, sizeof(uuid), &pos);
186 if ((ret < 0) || (ret != sizeof(uuid))) {
187 seq_puts(m, "Unknown\n");
189 for (i = 0; i < sizeof(uuid); i++) {
190 if ((i == 8) || (i == 12) || (i == 16) || (i == 20))
192 seq_printf(m, "%c", uuid[i]);
198 #ifdef CONFIG_AMLOGIC_CPU_INFO
199 cpuinfo_get_chipid(chipid, CHIPID_LEN);
200 seq_puts(m, "Serial\t\t: ");
201 for (i = 0; i < 16; i++)
202 seq_printf(m, "%02x", chipid[i]);
206 #ifdef CONFIG_AMLOGIC_CPU_INFO
207 #ifdef CONFIG_ARCH_MESON64_ODROID_COMMON
208 seq_printf(m, "Hardware\t: %s\n", machine_name);
209 seq_printf(m, "Revision\t: %04x\n\n", system_rev);
211 seq_printf(m, "Hardware\t: %s\n", machine_model);
217 static void *c_start(struct seq_file *m, loff_t *pos)
219 return *pos < 1 ? (void *)1 : NULL;
222 static void *c_next(struct seq_file *m, void *v, loff_t *pos)
228 static void c_stop(struct seq_file *m, void *v)
232 const struct seq_operations cpuinfo_op = {
240 static struct kobj_type cpuregs_kobj_type = {
241 .sysfs_ops = &kobj_sysfs_ops,
245 * The ARM ARM uses the phrase "32-bit register" to describe a register
246 * whose upper 32 bits are RES0 (per C5.1.1, ARM DDI 0487A.i), however
247 * no statement is made as to whether the upper 32 bits will or will not
248 * be made use of in future, and between ARM DDI 0487A.c and ARM DDI
249 * 0487A.d CLIDR_EL1 was expanded from 32-bit to 64-bit.
251 * Thus, while both MIDR_EL1 and REVIDR_EL1 are described as 32-bit
252 * registers, we expose them both as 64 bit values to cater for possible
253 * future expansion without an ABI break.
255 #define kobj_to_cpuinfo(kobj) container_of(kobj, struct cpuinfo_arm64, kobj)
256 #define CPUREGS_ATTR_RO(_name, _field) \
257 static ssize_t _name##_show(struct kobject *kobj, \
258 struct kobj_attribute *attr, char *buf) \
260 struct cpuinfo_arm64 *info = kobj_to_cpuinfo(kobj); \
262 if (info->reg_midr) \
263 return sprintf(buf, "0x%016x\n", info->reg_##_field); \
267 static struct kobj_attribute cpuregs_attr_##_name = __ATTR_RO(_name)
269 CPUREGS_ATTR_RO(midr_el1, midr);
270 CPUREGS_ATTR_RO(revidr_el1, revidr);
272 static struct attribute *cpuregs_id_attrs[] = {
273 &cpuregs_attr_midr_el1.attr,
274 &cpuregs_attr_revidr_el1.attr,
278 static struct attribute_group cpuregs_attr_group = {
279 .attrs = cpuregs_id_attrs,
280 .name = "identification"
283 static int cpuid_add_regs(int cpu)
287 struct cpuinfo_arm64 *info = &per_cpu(cpu_data, cpu);
289 dev = get_cpu_device(cpu);
294 rc = kobject_add(&info->kobj, &dev->kobj, "regs");
297 rc = sysfs_create_group(&info->kobj, &cpuregs_attr_group);
299 kobject_del(&info->kobj);
304 static int cpuid_remove_regs(int cpu)
307 struct cpuinfo_arm64 *info = &per_cpu(cpu_data, cpu);
309 dev = get_cpu_device(cpu);
312 if (info->kobj.parent) {
313 sysfs_remove_group(&info->kobj, &cpuregs_attr_group);
314 kobject_del(&info->kobj);
320 static int cpuid_callback(struct notifier_block *nb,
321 unsigned long action, void *hcpu)
324 unsigned long cpu = (unsigned long)hcpu;
326 switch (action & ~CPU_TASKS_FROZEN) {
328 rc = cpuid_add_regs(cpu);
331 rc = cpuid_remove_regs(cpu);
335 return notifier_from_errno(rc);
338 static int __init cpuinfo_regs_init(void)
342 cpu_notifier_register_begin();
344 for_each_possible_cpu(cpu) {
345 struct cpuinfo_arm64 *info = &per_cpu(cpu_data, cpu);
347 kobject_init(&info->kobj, &cpuregs_kobj_type);
351 __hotcpu_notifier(cpuid_callback, 0);
353 cpu_notifier_register_done();
356 static void cpuinfo_detect_icache_policy(struct cpuinfo_arm64 *info)
358 unsigned int cpu = smp_processor_id();
359 u32 l1ip = CTR_L1IP(info->reg_ctr);
361 if (l1ip != ICACHE_POLICY_PIPT) {
363 * VIPT caches are non-aliasing if the VA always equals the PA
364 * in all bit positions that are covered by the index. This is
365 * the case if the size of a way (# of sets * line size) does
366 * not exceed PAGE_SIZE.
368 u32 waysize = icache_get_numsets() * icache_get_linesize();
370 if (l1ip != ICACHE_POLICY_VIPT || waysize > PAGE_SIZE)
371 set_bit(ICACHEF_ALIASING, &__icache_flags);
373 if (l1ip == ICACHE_POLICY_AIVIVT)
374 set_bit(ICACHEF_AIVIVT, &__icache_flags);
376 pr_info("Detected %s I-cache on CPU%d\n", icache_policy_str[l1ip], cpu);
379 static void __cpuinfo_store_cpu(struct cpuinfo_arm64 *info)
381 info->reg_cntfrq = arch_timer_get_cntfrq();
382 info->reg_ctr = read_cpuid_cachetype();
383 info->reg_dczid = read_cpuid(DCZID_EL0);
384 info->reg_midr = read_cpuid_id();
385 info->reg_revidr = read_cpuid(REVIDR_EL1);
387 info->reg_id_aa64dfr0 = read_cpuid(ID_AA64DFR0_EL1);
388 info->reg_id_aa64dfr1 = read_cpuid(ID_AA64DFR1_EL1);
389 info->reg_id_aa64isar0 = read_cpuid(ID_AA64ISAR0_EL1);
390 info->reg_id_aa64isar1 = read_cpuid(ID_AA64ISAR1_EL1);
391 info->reg_id_aa64mmfr0 = read_cpuid(ID_AA64MMFR0_EL1);
392 info->reg_id_aa64mmfr1 = read_cpuid(ID_AA64MMFR1_EL1);
393 info->reg_id_aa64mmfr2 = read_cpuid(ID_AA64MMFR2_EL1);
394 info->reg_id_aa64pfr0 = read_cpuid(ID_AA64PFR0_EL1);
395 info->reg_id_aa64pfr1 = read_cpuid(ID_AA64PFR1_EL1);
397 /* Update the 32bit ID registers only if AArch32 is implemented */
398 if (id_aa64pfr0_32bit_el0(info->reg_id_aa64pfr0)) {
399 info->reg_id_dfr0 = read_cpuid(ID_DFR0_EL1);
400 info->reg_id_isar0 = read_cpuid(ID_ISAR0_EL1);
401 info->reg_id_isar1 = read_cpuid(ID_ISAR1_EL1);
402 info->reg_id_isar2 = read_cpuid(ID_ISAR2_EL1);
403 info->reg_id_isar3 = read_cpuid(ID_ISAR3_EL1);
404 info->reg_id_isar4 = read_cpuid(ID_ISAR4_EL1);
405 info->reg_id_isar5 = read_cpuid(ID_ISAR5_EL1);
406 info->reg_id_mmfr0 = read_cpuid(ID_MMFR0_EL1);
407 info->reg_id_mmfr1 = read_cpuid(ID_MMFR1_EL1);
408 info->reg_id_mmfr2 = read_cpuid(ID_MMFR2_EL1);
409 info->reg_id_mmfr3 = read_cpuid(ID_MMFR3_EL1);
410 info->reg_id_pfr0 = read_cpuid(ID_PFR0_EL1);
411 info->reg_id_pfr1 = read_cpuid(ID_PFR1_EL1);
413 info->reg_mvfr0 = read_cpuid(MVFR0_EL1);
414 info->reg_mvfr1 = read_cpuid(MVFR1_EL1);
415 info->reg_mvfr2 = read_cpuid(MVFR2_EL1);
418 cpuinfo_detect_icache_policy(info);
421 void cpuinfo_store_cpu(void)
423 struct cpuinfo_arm64 *info = this_cpu_ptr(&cpu_data);
424 __cpuinfo_store_cpu(info);
425 update_cpu_features(smp_processor_id(), info, &boot_cpu_data);
428 void __init cpuinfo_store_boot_cpu(void)
430 struct cpuinfo_arm64 *info = &per_cpu(cpu_data, 0);
431 __cpuinfo_store_cpu(info);
433 boot_cpu_data = *info;
434 init_cpu_features(&boot_cpu_data);
437 device_initcall(cpuinfo_regs_init);