source: mainline/kernel/arch/arm32/src/cpu/cpu.c@ 6bf5b8c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6bf5b8c was 7c3fb9b, checked in by Jiri Svoboda <jiri@…>, 8 years ago

Fix block comment formatting (ccheck).

  • Property mode set to 100644
File size: 10.5 KB
Line 
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#ifdef CONFIG_FPU
44#include <arch/fpu_context.h>
45#endif
46
47static inline unsigned log2(unsigned val)
48{
49 unsigned log = 0;
50 --val;
51 while (val) {
52 ++log;
53 val >>= 1;
54 }
55 return log;
56}
57
58static unsigned dcache_ways(unsigned level);
59static unsigned dcache_sets(unsigned level);
60static unsigned dcache_linesize_log(unsigned level);
61
62
63/** Implementers (vendor) names */
64static const char *implementer(unsigned id)
65{
66 switch (id) {
67 case 0x41:
68 return "ARM Limited";
69 case 0x44:
70 return "Digital Equipment Corporation";
71 case 0x4d:
72 return "Motorola, Freescale Semiconductor Inc.";
73 case 0x51:
74 return "Qualcomm Inc.";
75 case 0x56:
76 return "Marvell Semiconductor Inc.";
77 case 0x69:
78 return "Intel Corporation";
79 }
80 return "Unknown implementer";
81}
82
83/** Architecture names */
84static const char *architecture_string(cpu_arch_t *arch)
85{
86 static const char *arch_data[] = {
87 "ARM", /* 0x0 */
88 "ARMv4", /* 0x1 */
89 "ARMv4T", /* 0x2 */
90 "ARMv5", /* 0x3 */
91 "ARMv5T", /* 0x4 */
92 "ARMv5TE", /* 0x5 */
93 "ARMv5TEJ", /* 0x6 */
94 "ARMv6" /* 0x7 */
95 };
96 if (arch->arch_num < (sizeof(arch_data) / sizeof(arch_data[0])))
97 return arch_data[arch->arch_num];
98 else
99 return arch_data[0];
100}
101
102
103/** Retrieves processor identification from CP15 register 0.
104 *
105 * @param cpu Structure for storing CPU identification.
106 * See page B4-1630 of ARM Architecture Reference Manual.
107 */
108static void arch_cpu_identify(cpu_arch_t *cpu)
109{
110 const uint32_t ident = MIDR_read();
111
112 cpu->imp_num = (ident >> MIDR_IMPLEMENTER_SHIFT) & MIDR_IMPLEMENTER_MASK;
113 cpu->variant_num = (ident >> MIDR_VARIANT_SHIFT) & MIDR_VARIANT_MASK;
114 cpu->arch_num = (ident >> MIDR_ARCHITECTURE_SHIFT) & MIDR_ARCHITECTURE_MASK;
115 cpu->prim_part_num = (ident >> MIDR_PART_NUMBER_SHIFT) & MIDR_PART_NUMBER_MASK;
116 cpu->rev_num = (ident >> MIDR_REVISION_SHIFT) & MIDR_REVISION_MASK;
117
118 // TODO CPUs with arch_num == 0xf use CPUID scheme for identification
119 cpu->dcache_levels = dcache_levels();
120
121 for (unsigned i = 0; i < cpu->dcache_levels; ++i) {
122 cpu->dcache[i].ways = dcache_ways(i);
123 cpu->dcache[i].sets = dcache_sets(i);
124 cpu->dcache[i].way_shift = 31 - log2(cpu->dcache[i].ways);
125 cpu->dcache[i].set_shift = dcache_linesize_log(i);
126 cpu->dcache[i].line_size = 1 << dcache_linesize_log(i);
127 printf("Found DCache L%u: %u-way, %u sets, %u byte lines "
128 "(shifts: w%u, s%u)\n", i + 1, cpu->dcache[i].ways,
129 cpu->dcache[i].sets, cpu->dcache[i].line_size,
130 cpu->dcache[i].way_shift, cpu->dcache[i].set_shift);
131 }
132}
133
134/** Enables unaligned access and caching for armv6+ */
135void cpu_arch_init(void)
136{
137 uint32_t control_reg = SCTLR_read();
138
139 dcache_invalidate();
140 read_barrier();
141
142 /* Turn off tex remap, RAZ/WI prior to armv7 */
143 control_reg &= ~SCTLR_TEX_REMAP_EN_FLAG;
144 /* Turn off accessed flag, RAZ/WI prior to armv7 */
145 control_reg &= ~(SCTLR_ACCESS_FLAG_EN_FLAG | SCTLR_HW_ACCESS_FLAG_EN_FLAG);
146
147 /* Unaligned access is supported on armv6+ */
148#if defined(PROCESSOR_ARCH_armv7_a) | defined(PROCESSOR_ARCH_armv6)
149 /*
150 * Enable unaligned access, RAZ/WI prior to armv6
151 * switchable on armv6, RAO/WI writes on armv7,
152 * see ARM Architecture Reference Manual ARMv7-A and ARMv7-R edition
153 * L.3.1 (p. 2456)
154 */
155 control_reg |= SCTLR_UNALIGNED_EN_FLAG;
156 /*
157 * Disable alignment checks, this turns unaligned access to undefined,
158 * unless U bit is set.
159 */
160 control_reg &= ~SCTLR_ALIGN_CHECK_EN_FLAG;
161 /*
162 * Enable caching, On arm prior to armv7 there is only one level
163 * of caches. Data cache is coherent.
164 * "This means that the behavior of accesses from the same observer to
165 * different VAs, that are translated to the same PA
166 * with the same memory attributes, is fully coherent."
167 * ARM Architecture Reference Manual ARMv7-A and ARMv7-R Edition
168 * B3.11.1 (p. 1383)
169 * We are safe to turn this on. For arm v6 see ch L.6.2 (p. 2469)
170 * L2 Cache for armv7 is enabled by default (i.e. controlled by
171 * this flag).
172 */
173 control_reg |= SCTLR_CACHE_EN_FLAG;
174#endif
175#ifdef PROCESSOR_ARCH_armv7_a
176 /*
177 * ICache coherency is elaborated on in barrier.h.
178 * VIPT and PIPT caches need maintenance only on code modify,
179 * so it should be safe for general use.
180 * Enable branch predictors too as they follow the same rules
181 * as ICache and they can be flushed together
182 */
183 if ((CTR_read() & CTR_L1I_POLICY_MASK) != CTR_L1I_POLICY_AIVIVT) {
184 control_reg |=
185 SCTLR_INST_CACHE_EN_FLAG | SCTLR_BRANCH_PREDICT_EN_FLAG;
186 } else {
187 control_reg &=
188 ~(SCTLR_INST_CACHE_EN_FLAG | SCTLR_BRANCH_PREDICT_EN_FLAG);
189 }
190#endif
191 SCTLR_write(control_reg);
192
193#ifdef CONFIG_FPU
194 fpu_setup();
195#endif
196
197#ifdef PROCESSOR_ARCH_armv7_a
198 if ((ID_PFR1_read() & ID_PFR1_GEN_TIMER_EXT_MASK) !=
199 ID_PFR1_GEN_TIMER_EXT) {
200 PMCR_write(PMCR_read() | PMCR_E_FLAG | PMCR_D_FLAG);
201 PMCNTENSET_write(PMCNTENSET_CYCLE_COUNTER_EN_FLAG);
202 }
203#endif
204}
205
206/** Retrieves processor identification and stores it to #CPU.arch */
207void cpu_identify(void)
208{
209 arch_cpu_identify(&CPU->arch);
210}
211
212/** Prints CPU identification. */
213void cpu_print_report(cpu_t *m)
214{
215 printf("cpu%d: vendor=%s, architecture=%s, part number=%x, "
216 "variant=%x, revision=%x\n",
217 m->id, implementer(m->arch.imp_num),
218 architecture_string(&m->arch), m->arch.prim_part_num,
219 m->arch.variant_num, m->arch.rev_num);
220}
221
222/** See chapter B4.1.19 of ARM Architecture Reference Manual */
223static unsigned dcache_linesize_log(unsigned level)
224{
225#ifdef PROCESSOR_ARCH_armv7_a
226 CSSELR_write((level & CCSELR_LEVEL_MASK) << CCSELR_LEVEL_SHIFT);
227 const uint32_t ccsidr = CCSIDR_read();
228 return CCSIDR_LINESIZE_LOG(ccsidr);
229#endif
230 return 0;
231
232}
233
234/** See chapter B4.1.19 of ARM Architecture Reference Manual */
235static unsigned dcache_ways(unsigned level)
236{
237#ifdef PROCESSOR_ARCH_armv7_a
238 CSSELR_write((level & CCSELR_LEVEL_MASK) << CCSELR_LEVEL_SHIFT);
239 const uint32_t ccsidr = CCSIDR_read();
240 return CCSIDR_WAYS(ccsidr);
241#endif
242 return 0;
243}
244
245/** See chapter B4.1.19 of ARM Architecture Reference Manual */
246static unsigned dcache_sets(unsigned level)
247{
248#ifdef PROCESSOR_ARCH_armv7_a
249 CSSELR_write((level & CCSELR_LEVEL_MASK) << CCSELR_LEVEL_SHIFT);
250 const uint32_t ccsidr = CCSIDR_read();
251 return CCSIDR_SETS(ccsidr);
252#endif
253 return 0;
254}
255
256unsigned dcache_levels(void)
257{
258 unsigned levels = 0;
259#ifdef PROCESSOR_ARCH_armv7_a
260 const uint32_t val = CLIDR_read();
261 for (unsigned i = 0; i < 8; ++i) {
262 const unsigned ctype = CLIDR_CACHE(i, val);
263 switch (ctype) {
264 case CLIDR_DCACHE_ONLY:
265 case CLIDR_SEP_CACHE:
266 case CLIDR_UNI_CACHE:
267 ++levels;
268 default:
269 (void)0;
270 }
271 }
272#endif
273 return levels;
274}
275
276static void dcache_clean_manual(unsigned level, bool invalidate,
277 unsigned ways, unsigned sets, unsigned way_shift, unsigned set_shift)
278{
279
280 for (unsigned i = 0; i < ways; ++i) {
281 for (unsigned j = 0; j < sets; ++j) {
282 const uint32_t val =
283 ((level & 0x7) << 1) |
284 (j << set_shift) | (i << way_shift);
285 if (invalidate)
286 DCCISW_write(val);
287 else
288 DCCSW_write(val);
289 }
290 }
291}
292
293void dcache_flush(void)
294{
295 /* See ARM Architecture Reference Manual ch. B4.2.1 p. B4-1724 */
296 const unsigned levels = dcache_levels();
297 for (unsigned i = 0; i < levels; ++i) {
298 const unsigned ways = dcache_ways(i);
299 const unsigned sets = dcache_sets(i);
300 const unsigned way_shift = 32 - log2(ways);
301 const unsigned set_shift = dcache_linesize_log(i);
302 dcache_clean_manual(i, false, ways, sets, way_shift, set_shift);
303 }
304}
305
306void dcache_flush_invalidate(void)
307{
308 /* See ARM Architecture Reference Manual ch. B4.2.1 p. B4-1724 */
309 const unsigned levels = dcache_levels();
310 for (unsigned i = 0; i < levels; ++i) {
311 const unsigned ways = dcache_ways(i);
312 const unsigned sets = dcache_sets(i);
313 const unsigned way_shift = 32 - log2(ways);
314 const unsigned set_shift = dcache_linesize_log(i);
315 dcache_clean_manual(i, true, ways, sets, way_shift, set_shift);
316 }
317}
318
319
320void cpu_dcache_flush(void)
321{
322 for (unsigned i = 0; i < CPU->arch.dcache_levels; ++i)
323 dcache_clean_manual(i, false,
324 CPU->arch.dcache[i].ways, CPU->arch.dcache[i].sets,
325 CPU->arch.dcache[i].way_shift, CPU->arch.dcache[i].set_shift);
326}
327
328void cpu_dcache_flush_invalidate(void)
329{
330 const unsigned levels = dcache_levels();
331 for (unsigned i = 0; i < levels; ++i)
332 dcache_clean_manual(i, true,
333 CPU->arch.dcache[i].ways, CPU->arch.dcache[i].sets,
334 CPU->arch.dcache[i].way_shift, CPU->arch.dcache[i].set_shift);
335}
336
337void icache_invalidate(void)
338{
339#if defined(PROCESSOR_ARCH_armv7_a)
340 ICIALLU_write(0);
341#else
342 ICIALL_write(0);
343#endif
344}
345
346#if !defined(PROCESSOR_ARCH_armv7_a)
347static bool cache_is_unified(void)
348{
349 if (MIDR_read() != CTR_read()) {
350 /* We have the CTR register */
351 return (CTR_read() & CTR_SEP_FLAG) != CTR_SEP_FLAG;
352 } else {
353 panic("Unknown cache type");
354 }
355}
356#endif
357
358void dcache_invalidate(void)
359{
360#if defined(PROCESSOR_ARCH_armv7_a)
361 dcache_flush_invalidate();
362#else
363 if (cache_is_unified())
364 CIALL_write(0);
365 else
366 DCIALL_write(0);
367#endif
368}
369
370void dcache_clean_mva_pou(uintptr_t mva)
371{
372#if defined(PROCESSOR_ARCH_armv7_a)
373 DCCMVAU_write(mva);
374#else
375 if (cache_is_unified())
376 CCMVA_write(mva);
377 else
378 DCCMVA_write(mva);
379#endif
380}
381
382/** @}
383 */
Note: See TracBrowser for help on using the repository browser.