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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9048147 was 9048147, checked in by Jakub Jermar <jakub@…>, 11 years ago

Include fpu_context.h if necessary.

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