arm64: add helper functions to read I-cache attributes
[linux-2.6-block.git] / arch / arm64 / kernel / cpuinfo.c
CommitLineData
df857416
MR
1/*
2 * Record and handle CPU attributes.
3 *
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.
8 *
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.
13 *
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/>.
16 */
17#include <asm/arch_timer.h>
18#include <asm/cachetype.h>
19#include <asm/cpu.h>
20#include <asm/cputype.h>
21
59ccc0d4 22#include <linux/bitops.h>
80c517b0 23#include <linux/bug.h>
df857416 24#include <linux/init.h>
127161aa 25#include <linux/kernel.h>
80c517b0 26#include <linux/preempt.h>
59ccc0d4 27#include <linux/printk.h>
df857416
MR
28#include <linux/smp.h>
29
30/*
31 * In case the boot CPU is hotpluggable, we record its initial state and
32 * current state separately. Certain system registers may contain different
33 * values depending on configuration at or after reset.
34 */
35DEFINE_PER_CPU(struct cpuinfo_arm64, cpu_data);
36static struct cpuinfo_arm64 boot_cpu_data;
37
59ccc0d4
MR
38static char *icache_policy_str[] = {
39 [ICACHE_POLICY_RESERVED] = "RESERVED/UNKNOWN",
40 [ICACHE_POLICY_AIVIVT] = "AIVIVT",
41 [ICACHE_POLICY_VIPT] = "VIPT",
42 [ICACHE_POLICY_PIPT] = "PIPT",
43};
44
45unsigned long __icache_flags;
46
47static void cpuinfo_detect_icache_policy(struct cpuinfo_arm64 *info)
48{
49 unsigned int cpu = smp_processor_id();
50 u32 l1ip = CTR_L1IP(info->reg_ctr);
51
52 if (l1ip != ICACHE_POLICY_PIPT)
53 set_bit(ICACHEF_ALIASING, &__icache_flags);
a3a80544 54 if (l1ip == ICACHE_POLICY_AIVIVT)
59ccc0d4
MR
55 set_bit(ICACHEF_AIVIVT, &__icache_flags);
56
ea171967 57 pr_info("Detected %s I-cache on CPU%d\n", icache_policy_str[l1ip], cpu);
59ccc0d4
MR
58}
59
127161aa
MR
60static int check_reg_mask(char *name, u64 mask, u64 boot, u64 cur, int cpu)
61{
62 if ((boot & mask) == (cur & mask))
63 return 0;
64
65 pr_warn("SANITY CHECK: Unexpected variation in %s. Boot CPU: %#016lx, CPU%d: %#016lx\n",
66 name, (unsigned long)boot, cpu, (unsigned long)cur);
67
68 return 1;
69}
70
71#define CHECK_MASK(field, mask, boot, cur, cpu) \
72 check_reg_mask(#field, mask, (boot)->reg_ ## field, (cur)->reg_ ## field, cpu)
73
74#define CHECK(field, boot, cur, cpu) \
75 CHECK_MASK(field, ~0ULL, boot, cur, cpu)
76
77/*
78 * Verify that CPUs don't have unexpected differences that will cause problems.
79 */
80static void cpuinfo_sanity_check(struct cpuinfo_arm64 *cur)
81{
82 unsigned int cpu = smp_processor_id();
83 struct cpuinfo_arm64 *boot = &boot_cpu_data;
84 unsigned int diff = 0;
85
86 /*
87 * The kernel can handle differing I-cache policies, but otherwise
88 * caches should look identical. Userspace JITs will make use of
89 * *minLine.
90 */
91 diff |= CHECK_MASK(ctr, 0xffff3fff, boot, cur, cpu);
92
93 /*
94 * Userspace may perform DC ZVA instructions. Mismatched block sizes
95 * could result in too much or too little memory being zeroed if a
96 * process is preempted and migrated between CPUs.
97 */
98 diff |= CHECK(dczid, boot, cur, cpu);
99
100 /* If different, timekeeping will be broken (especially with KVM) */
101 diff |= CHECK(cntfrq, boot, cur, cpu);
102
103 /*
104 * Even in big.LITTLE, processors should be identical instruction-set
105 * wise.
106 */
107 diff |= CHECK(id_aa64isar0, boot, cur, cpu);
108 diff |= CHECK(id_aa64isar1, boot, cur, cpu);
109
110 /*
111 * Differing PARange support is fine as long as all peripherals and
112 * memory are mapped within the minimum PARange of all CPUs.
113 * Linux should not care about secure memory.
114 * ID_AA64MMFR1 is currently RES0.
115 */
116 diff |= CHECK_MASK(id_aa64mmfr0, 0xffffffffffff0ff0, boot, cur, cpu);
117 diff |= CHECK(id_aa64mmfr1, boot, cur, cpu);
118
119 /*
120 * EL3 is not our concern.
121 * ID_AA64PFR1 is currently RES0.
122 */
123 diff |= CHECK_MASK(id_aa64pfr0, 0xffffffffffff0fff, boot, cur, cpu);
124 diff |= CHECK(id_aa64pfr1, boot, cur, cpu);
125
126 /*
127 * If we have AArch32, we care about 32-bit features for compat. These
128 * registers should be RES0 otherwise.
129 */
130 diff |= CHECK(id_isar0, boot, cur, cpu);
131 diff |= CHECK(id_isar1, boot, cur, cpu);
132 diff |= CHECK(id_isar2, boot, cur, cpu);
133 diff |= CHECK(id_isar3, boot, cur, cpu);
134 diff |= CHECK(id_isar4, boot, cur, cpu);
135 diff |= CHECK(id_isar5, boot, cur, cpu);
136 diff |= CHECK(id_mmfr0, boot, cur, cpu);
137 diff |= CHECK(id_mmfr1, boot, cur, cpu);
138 diff |= CHECK(id_mmfr2, boot, cur, cpu);
139 diff |= CHECK(id_mmfr3, boot, cur, cpu);
140 diff |= CHECK(id_pfr0, boot, cur, cpu);
141 diff |= CHECK(id_pfr1, boot, cur, cpu);
142
143 /*
144 * Mismatched CPU features are a recipe for disaster. Don't even
145 * pretend to support them.
146 */
147 WARN_TAINT_ONCE(diff, TAINT_CPU_OUT_OF_SPEC,
148 "Unsupported CPU feature variation.");
149}
150
df857416
MR
151static void __cpuinfo_store_cpu(struct cpuinfo_arm64 *info)
152{
153 info->reg_cntfrq = arch_timer_get_cntfrq();
154 info->reg_ctr = read_cpuid_cachetype();
155 info->reg_dczid = read_cpuid(DCZID_EL0);
156 info->reg_midr = read_cpuid_id();
157
158 info->reg_id_aa64isar0 = read_cpuid(ID_AA64ISAR0_EL1);
159 info->reg_id_aa64isar1 = read_cpuid(ID_AA64ISAR1_EL1);
160 info->reg_id_aa64mmfr0 = read_cpuid(ID_AA64MMFR0_EL1);
161 info->reg_id_aa64mmfr1 = read_cpuid(ID_AA64MMFR1_EL1);
162 info->reg_id_aa64pfr0 = read_cpuid(ID_AA64PFR0_EL1);
163 info->reg_id_aa64pfr1 = read_cpuid(ID_AA64PFR1_EL1);
164
165 info->reg_id_isar0 = read_cpuid(ID_ISAR0_EL1);
166 info->reg_id_isar1 = read_cpuid(ID_ISAR1_EL1);
167 info->reg_id_isar2 = read_cpuid(ID_ISAR2_EL1);
168 info->reg_id_isar3 = read_cpuid(ID_ISAR3_EL1);
169 info->reg_id_isar4 = read_cpuid(ID_ISAR4_EL1);
170 info->reg_id_isar5 = read_cpuid(ID_ISAR5_EL1);
171 info->reg_id_mmfr0 = read_cpuid(ID_MMFR0_EL1);
172 info->reg_id_mmfr1 = read_cpuid(ID_MMFR1_EL1);
173 info->reg_id_mmfr2 = read_cpuid(ID_MMFR2_EL1);
174 info->reg_id_mmfr3 = read_cpuid(ID_MMFR3_EL1);
175 info->reg_id_pfr0 = read_cpuid(ID_PFR0_EL1);
176 info->reg_id_pfr1 = read_cpuid(ID_PFR1_EL1);
59ccc0d4
MR
177
178 cpuinfo_detect_icache_policy(info);
df857416
MR
179}
180
181void cpuinfo_store_cpu(void)
182{
183 struct cpuinfo_arm64 *info = this_cpu_ptr(&cpu_data);
184 __cpuinfo_store_cpu(info);
127161aa 185 cpuinfo_sanity_check(info);
df857416
MR
186}
187
188void __init cpuinfo_store_boot_cpu(void)
189{
190 struct cpuinfo_arm64 *info = &per_cpu(cpu_data, 0);
191 __cpuinfo_store_cpu(info);
192
193 boot_cpu_data = *info;
194}
80c517b0
AB
195
196u64 __attribute_const__ icache_get_ccsidr(void)
197{
198 u64 ccsidr;
199
200 WARN_ON(preemptible());
201
202 /* Select L1 I-cache and read its size ID register */
203 asm("msr csselr_el1, %1; isb; mrs %0, ccsidr_el1"
204 : "=r"(ccsidr) : "r"(1L));
205 return ccsidr;
206}