1 | /*
|
---|
2 | * Copyright (c) 2007 Michal Kebrt
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup arm32
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | * @brief CPU identification.
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <arch/cache.h>
|
---|
37 | #include <arch/cpu.h>
|
---|
38 | #include <arch/cp15.h>
|
---|
39 | #include <cpu.h>
|
---|
40 | #include <arch.h>
|
---|
41 | #include <print.h>
|
---|
42 |
|
---|
43 | static inline unsigned log2(unsigned val)
|
---|
44 | {
|
---|
45 | unsigned log = 0;
|
---|
46 | --val;
|
---|
47 | while (val) {
|
---|
48 | ++log;
|
---|
49 | val >>= 1;
|
---|
50 | }
|
---|
51 | return log;
|
---|
52 | }
|
---|
53 |
|
---|
54 | static unsigned dcache_ways(unsigned level);
|
---|
55 | static unsigned dcache_sets(unsigned level);
|
---|
56 | static unsigned dcache_linesize_log(unsigned level);
|
---|
57 |
|
---|
58 |
|
---|
59 | /** Implementers (vendor) names */
|
---|
60 | static const char * implementer(unsigned id)
|
---|
61 | {
|
---|
62 | switch (id)
|
---|
63 | {
|
---|
64 | case 0x41: return "ARM Limited";
|
---|
65 | case 0x44: return "Digital Equipment Corporation";
|
---|
66 | case 0x4d: return "Motorola, Freescale Semiconductor Inc.";
|
---|
67 | case 0x51: return "Qualcomm Inc.";
|
---|
68 | case 0x56: return "Marvell Semiconductor Inc.";
|
---|
69 | case 0x69: return "Intel Corporation";
|
---|
70 | }
|
---|
71 | return "Unknown implementer";
|
---|
72 | }
|
---|
73 |
|
---|
74 | /** Architecture names */
|
---|
75 | static const char * architecture_string(cpu_arch_t *arch)
|
---|
76 | {
|
---|
77 | static const char *arch_data[] = {
|
---|
78 | "ARM", /* 0x0 */
|
---|
79 | "ARMv4", /* 0x1 */
|
---|
80 | "ARMv4T", /* 0x2 */
|
---|
81 | "ARMv5", /* 0x3 */
|
---|
82 | "ARMv5T", /* 0x4 */
|
---|
83 | "ARMv5TE", /* 0x5 */
|
---|
84 | "ARMv5TEJ", /* 0x6 */
|
---|
85 | "ARMv6" /* 0x7 */
|
---|
86 | };
|
---|
87 | if (arch->arch_num < (sizeof(arch_data) / sizeof(arch_data[0])))
|
---|
88 | return arch_data[arch->arch_num];
|
---|
89 | else
|
---|
90 | return arch_data[0];
|
---|
91 | }
|
---|
92 |
|
---|
93 |
|
---|
94 | /** Retrieves processor identification from CP15 register 0.
|
---|
95 | *
|
---|
96 | * @param cpu Structure for storing CPU identification.
|
---|
97 | * See page B4-1630 of ARM Architecture Reference Manual.
|
---|
98 | */
|
---|
99 | static void arch_cpu_identify(cpu_arch_t *cpu)
|
---|
100 | {
|
---|
101 | const uint32_t ident = MIDR_read();
|
---|
102 |
|
---|
103 | cpu->imp_num = (ident >> MIDR_IMPLEMENTER_SHIFT) & MIDR_IMPLEMENTER_MASK;
|
---|
104 | cpu->variant_num = (ident >> MIDR_VARIANT_SHIFT) & MIDR_VARIANT_MASK;
|
---|
105 | cpu->arch_num = (ident >> MIDR_ARCHITECTURE_SHIFT) & MIDR_ARCHITECTURE_MASK;
|
---|
106 | cpu->prim_part_num = (ident >> MIDR_PART_NUMBER_SHIFT) & MIDR_PART_NUMBER_MASK;
|
---|
107 | cpu->rev_num = (ident >> MIDR_REVISION_SHIFT) & MIDR_REVISION_MASK;
|
---|
108 |
|
---|
109 | // TODO CPUs with arch_num == 0xf use CPUID scheme for identification
|
---|
110 | cpu->dcache_levels = dcache_levels();
|
---|
111 |
|
---|
112 | for (unsigned i = 0; i < cpu->dcache_levels; ++i) {
|
---|
113 | cpu->dcache[i].ways = dcache_ways(i);
|
---|
114 | cpu->dcache[i].sets = dcache_sets(i);
|
---|
115 | cpu->dcache[i].way_shift = 31 - log2(cpu->dcache[i].ways);
|
---|
116 | cpu->dcache[i].set_shift = dcache_linesize_log(i);
|
---|
117 | cpu->dcache[i].line_size = 1 << dcache_linesize_log(i);
|
---|
118 | printf("Found DCache L%u: %u-way, %u sets, %u byte lines "
|
---|
119 | "(shifts: w%u, s%u)\n", i + 1, cpu->dcache[i].ways,
|
---|
120 | cpu->dcache[i].sets, cpu->dcache[i].line_size,
|
---|
121 | cpu->dcache[i].way_shift, cpu->dcache[i].set_shift);
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | /** Enables unaligned access and caching for armv6+ */
|
---|
126 | void cpu_arch_init(void)
|
---|
127 | {
|
---|
128 | uint32_t control_reg = SCTLR_read();
|
---|
129 |
|
---|
130 | /* Turn off tex remap, RAZ/WI prior to armv7 */
|
---|
131 | control_reg &= ~SCTLR_TEX_REMAP_EN_FLAG;
|
---|
132 | /* Turn off accessed flag, RAZ/WI prior to armv7 */
|
---|
133 | control_reg &= ~(SCTLR_ACCESS_FLAG_EN_FLAG | SCTLR_HW_ACCESS_FLAG_EN_FLAG);
|
---|
134 |
|
---|
135 | /* Unaligned access is supported on armv6+ */
|
---|
136 | #if defined(PROCESSOR_ARCH_armv7_a) | defined(PROCESSOR_ARCH_armv6)
|
---|
137 | /* Enable unaligned access, RAZ/WI prior to armv6
|
---|
138 | * switchable on armv6, RAO/WI writes on armv7,
|
---|
139 | * see ARM Architecture Reference Manual ARMv7-A and ARMv7-R edition
|
---|
140 | * L.3.1 (p. 2456) */
|
---|
141 | control_reg |= SCTLR_UNALIGNED_EN_FLAG;
|
---|
142 | /* Disable alignment checks, this turns unaligned access to undefined,
|
---|
143 | * unless U bit is set. */
|
---|
144 | control_reg &= ~SCTLR_ALIGN_CHECK_EN_FLAG;
|
---|
145 | /* Enable caching, On arm prior to armv7 there is only one level
|
---|
146 | * of caches. Data cache is coherent.
|
---|
147 | * "This means that the behavior of accesses from the same observer to
|
---|
148 | * different VAs, that are translated to the same PA
|
---|
149 | * with the same memory attributes, is fully coherent."
|
---|
150 | * ARM Architecture Reference Manual ARMv7-A and ARMv7-R Edition
|
---|
151 | * B3.11.1 (p. 1383)
|
---|
152 | * We are safe to turn this on. For arm v6 see ch L.6.2 (p. 2469)
|
---|
153 | * L2 Cache for armv7 was enabled in boot code.
|
---|
154 | */
|
---|
155 | control_reg |= SCTLR_CACHE_EN_FLAG;
|
---|
156 | #endif
|
---|
157 | #ifdef PROCESSOR_cortex_a8
|
---|
158 | /* ICache coherency is elaborate on in barrier.h.
|
---|
159 | * Cortex-A8 implements IVIPT extension.
|
---|
160 | * Cortex-A8 TRM ch. 7.2.6 p. 7-4 (PDF 245) */
|
---|
161 | control_reg |= SCTLR_INST_CACHE_EN_FLAG;
|
---|
162 | /* Enable branch prediction RAZ/WI if not supported */
|
---|
163 | control_reg |= SCTLR_BRANCH_PREDICT_EN_FLAG;
|
---|
164 | #endif
|
---|
165 | SCTLR_write(control_reg);
|
---|
166 |
|
---|
167 | #ifdef CONFIG_FPU
|
---|
168 | fpu_setup();
|
---|
169 | #endif
|
---|
170 |
|
---|
171 | #ifdef PROCESSOR_ARCH_armv7_a
|
---|
172 | PMCR_write(PMCR_read() | PMCR_E_FLAG);
|
---|
173 | PMCNTENSET_write(PMCNTENSET_CYCLE_COUNTER_EN_FLAG);
|
---|
174 | #endif
|
---|
175 | }
|
---|
176 |
|
---|
177 | /** Retrieves processor identification and stores it to #CPU.arch */
|
---|
178 | void cpu_identify(void)
|
---|
179 | {
|
---|
180 | arch_cpu_identify(&CPU->arch);
|
---|
181 | }
|
---|
182 |
|
---|
183 | /** Prints CPU identification. */
|
---|
184 | void cpu_print_report(cpu_t *m)
|
---|
185 | {
|
---|
186 | printf("cpu%d: vendor=%s, architecture=%s, part number=%x, "
|
---|
187 | "variant=%x, revision=%x\n",
|
---|
188 | m->id, implementer(m->arch.imp_num),
|
---|
189 | architecture_string(&m->arch), m->arch.prim_part_num,
|
---|
190 | m->arch.variant_num, m->arch.rev_num);
|
---|
191 | }
|
---|
192 |
|
---|
193 | /** See chapter B4.1.19 of ARM Architecture Reference Manual */
|
---|
194 | static unsigned dcache_linesize_log(unsigned level)
|
---|
195 | {
|
---|
196 | #ifdef PROCESSOR_ARCH_armv7_a
|
---|
197 | CSSELR_write((level & CCSELR_LEVEL_MASK) << CCSELR_LEVEL_SHIFT);
|
---|
198 | const unsigned ls_log = 2 +
|
---|
199 | ((CCSIDR_read() >> CCSIDR_LINESIZE_SHIFT) & CCSIDR_LINESIZE_MASK);
|
---|
200 | return ls_log + 2; //return log2(bytes)
|
---|
201 | #endif
|
---|
202 | return 0;
|
---|
203 |
|
---|
204 | }
|
---|
205 |
|
---|
206 | /** See chapter B4.1.19 of ARM Architecture Reference Manual */
|
---|
207 | static unsigned dcache_ways(unsigned level)
|
---|
208 | {
|
---|
209 | #ifdef PROCESSOR_ARCH_armv7_a
|
---|
210 | CSSELR_write((level & CCSELR_LEVEL_MASK) << CCSELR_LEVEL_SHIFT);
|
---|
211 | const unsigned ways = 1 +
|
---|
212 | ((CCSIDR_read() >> CCSIDR_ASSOC_SHIFT) & CCSIDR_ASSOC_MASK);
|
---|
213 | return ways;
|
---|
214 | #endif
|
---|
215 | return 0;
|
---|
216 | }
|
---|
217 |
|
---|
218 | /** See chapter B4.1.19 of ARM Architecture Reference Manual */
|
---|
219 | static unsigned dcache_sets(unsigned level)
|
---|
220 | {
|
---|
221 | #ifdef PROCESSOR_ARCH_armv7_a
|
---|
222 | CSSELR_write((level & CCSELR_LEVEL_MASK) << CCSELR_LEVEL_SHIFT);
|
---|
223 | const unsigned sets = 1 +
|
---|
224 | ((CCSIDR_read() >> CCSIDR_NUMSETS_SHIFT) & CCSIDR_NUMSETS_MASK);
|
---|
225 | return sets;
|
---|
226 | #endif
|
---|
227 | return 0;
|
---|
228 | }
|
---|
229 |
|
---|
230 | unsigned dcache_levels(void)
|
---|
231 | {
|
---|
232 | unsigned levels = 0;
|
---|
233 | #ifdef PROCESSOR_ARCH_armv7_a
|
---|
234 | const uint32_t val = CLIDR_read();
|
---|
235 | for (unsigned i = 1; i <= 7; ++i) {
|
---|
236 | const unsigned ctype = CLIDR_CACHE(i, val);
|
---|
237 | switch (ctype) {
|
---|
238 | case CLIDR_DCACHE_ONLY:
|
---|
239 | case CLIDR_SEP_CACHE:
|
---|
240 | case CLIDR_UNI_CACHE:
|
---|
241 | ++levels;
|
---|
242 | default:
|
---|
243 | (void)0;
|
---|
244 | }
|
---|
245 | }
|
---|
246 | #endif
|
---|
247 | return levels;
|
---|
248 | }
|
---|
249 |
|
---|
250 | static void dcache_clean_manual(unsigned level, bool invalidate,
|
---|
251 | unsigned ways, unsigned sets, unsigned way_shift, unsigned set_shift)
|
---|
252 | {
|
---|
253 |
|
---|
254 | for (unsigned i = 0; i < ways; ++i) {
|
---|
255 | for (unsigned j = 0; j < sets; ++j) {
|
---|
256 | const uint32_t val =
|
---|
257 | ((level & 0x7) << 1) |
|
---|
258 | (j << set_shift) | (i << way_shift);
|
---|
259 | if (invalidate)
|
---|
260 | DCCISW_write(val);
|
---|
261 | else
|
---|
262 | DCCSW_write(val);
|
---|
263 | }
|
---|
264 | }
|
---|
265 | }
|
---|
266 |
|
---|
267 | void dcache_flush(void)
|
---|
268 | {
|
---|
269 | /* See ARM Architecture Reference Manual ch. B4.2.1 p. B4-1724 */
|
---|
270 | const unsigned levels = dcache_levels();
|
---|
271 | for (unsigned i = 0; i < levels; ++i) {
|
---|
272 | const unsigned ways = dcache_ways(i);
|
---|
273 | const unsigned sets = dcache_sets(i);
|
---|
274 | const unsigned way_shift = 31 - log2(ways);
|
---|
275 | const unsigned set_shift = dcache_linesize_log(i);
|
---|
276 | dcache_clean_manual(i, false, ways, sets, way_shift, set_shift);
|
---|
277 | }
|
---|
278 | }
|
---|
279 |
|
---|
280 | void dcache_flush_invalidate(void)
|
---|
281 | {
|
---|
282 | /* See ARM Architecture Reference Manual ch. B4.2.1 p. B4-1724 */
|
---|
283 | const unsigned levels = dcache_levels();
|
---|
284 | for (unsigned i = 0; i < levels; ++i) {
|
---|
285 | const unsigned ways = dcache_ways(i);
|
---|
286 | const unsigned sets = dcache_sets(i);
|
---|
287 | const unsigned way_shift = 31 - log2(ways);
|
---|
288 | const unsigned set_shift = dcache_linesize_log(i);
|
---|
289 | dcache_clean_manual(i, true, ways, sets, way_shift, set_shift);
|
---|
290 | }
|
---|
291 | }
|
---|
292 |
|
---|
293 |
|
---|
294 | void cpu_dcache_flush(void)
|
---|
295 | {
|
---|
296 | for (unsigned i = 0; i < CPU->arch.dcache_levels; ++i)
|
---|
297 | dcache_clean_manual(i, false,
|
---|
298 | CPU->arch.dcache[i].ways, CPU->arch.dcache[i].sets,
|
---|
299 | CPU->arch.dcache[i].way_shift, CPU->arch.dcache[i].set_shift);
|
---|
300 | }
|
---|
301 |
|
---|
302 | void cpu_dcache_flush_invalidate(void)
|
---|
303 | {
|
---|
304 | const unsigned levels = dcache_levels();
|
---|
305 | for (unsigned i = 0; i < levels; ++i)
|
---|
306 | dcache_clean_manual(i, true,
|
---|
307 | CPU->arch.dcache[i].ways, CPU->arch.dcache[i].sets,
|
---|
308 | CPU->arch.dcache[i].way_shift, CPU->arch.dcache[i].set_shift);
|
---|
309 | }
|
---|
310 |
|
---|
311 | void icache_invalidate(void)
|
---|
312 | {
|
---|
313 | ICIALLU_write(0);
|
---|
314 | }
|
---|
315 |
|
---|
316 | /** @}
|
---|
317 | */
|
---|