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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 612edca was 26e3db2, checked in by Jan Vesely <jano.vesely@…>, 13 years ago

arm32: Switch cpu identification to cp15 helpers.

  • Property mode set to 100644
File size: 9.4 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
43static 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
54static unsigned dcache_ways(unsigned level);
55static unsigned dcache_sets(unsigned level);
56static unsigned dcache_linesize_log(unsigned level);
57
58
59/** Implementers (vendor) names */
60static 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 */
75static 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 */
99static 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+ */
126void 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 /* Disable branch prediction RAZ/WI if not supported */
135 control_reg &= ~SCTLR_BRANCH_PREDICT_EN_FLAG;
136
137 /* Unaligned access is supported on armv6+ */
138#if defined(PROCESSOR_ARCH_armv7_a) | defined(PROCESSOR_ARCH_armv6)
139 /* Enable unaligned access, RAZ/WI prior to armv6
140 * switchable on armv6, RAO/WI writes on armv7,
141 * see ARM Architecture Reference Manual ARMv7-A and ARMv7-R edition
142 * L.3.1 (p. 2456) */
143 control_reg |= SCTLR_UNALIGNED_EN_FLAG;
144 /* Disable alignment checks, this turns unaligned access to undefined,
145 * unless U bit is set. */
146 control_reg &= ~SCTLR_ALIGN_CHECK_EN_FLAG;
147 /* Enable caching, On arm prior to armv7 there is only one level
148 * of caches. Data cache is coherent.
149 * "This means that the behavior of accesses from the same observer to
150 * different VAs, that are translated to the same PA
151 * with the same memory attributes, is fully coherent."
152 * ARM Architecture Reference Manual ARMv7-A and ARMv7-R Edition
153 * B3.11.1 (p. 1383)
154 * We are safe to turn this on. For arm v6 see ch L.6.2 (p. 2469)
155 * L2 Cache for armv7 was enabled in boot code.
156 */
157 control_reg |= SCTLR_CACHE_EN_FLAG;
158#endif
159#ifdef PROCESSOR_cortex_a8
160 /* ICache coherency is elaborate on in barrier.h.
161 * Cortex-A8 implements IVIPT extension.
162 * Cortex-A8 TRM ch. 7.2.6 p. 7-4 (PDF 245) */
163 control_reg |= SCTLR_INST_CACHE_EN_FLAG;
164#endif
165 SCTLR_write(control_reg);
166
167#ifdef CONFIG_FPU
168 fpu_setup();
169#endif
170}
171
172/** Retrieves processor identification and stores it to #CPU.arch */
173void cpu_identify(void)
174{
175 arch_cpu_identify(&CPU->arch);
176}
177
178/** Prints CPU identification. */
179void cpu_print_report(cpu_t *m)
180{
181 printf("cpu%d: vendor=%s, architecture=%s, part number=%x, "
182 "variant=%x, revision=%x\n",
183 m->id, implementer(m->arch.imp_num),
184 architecture_string(&m->arch), m->arch.prim_part_num,
185 m->arch.variant_num, m->arch.rev_num);
186}
187
188/** See chapter B4.1.19 of ARM Architecture Reference Manual */
189static unsigned dcache_linesize_log(unsigned level)
190{
191#ifdef PROCESSOR_ARCH_armv7_a
192 CSSELR_write((level & CCSELR_LEVEL_MASK) << CCSELR_LEVEL_SHIFT);
193 const unsigned ls_log = 2 +
194 ((CCSIDR_read() >> CCSIDR_LINESIZE_SHIFT) & CCSIDR_LINESIZE_MASK);
195 return ls_log + 2; //return log2(bytes)
196#endif
197 return 0;
198
199}
200
201/** See chapter B4.1.19 of ARM Architecture Reference Manual */
202static unsigned dcache_ways(unsigned level)
203{
204#ifdef PROCESSOR_ARCH_armv7_a
205 CSSELR_write((level & CCSELR_LEVEL_MASK) << CCSELR_LEVEL_SHIFT);
206 const unsigned ways = 1 +
207 ((CCSIDR_read() >> CCSIDR_ASSOC_SHIFT) & CCSIDR_ASSOC_MASK);
208 return ways;
209#endif
210 return 0;
211}
212
213/** See chapter B4.1.19 of ARM Architecture Reference Manual */
214static unsigned dcache_sets(unsigned level)
215{
216#ifdef PROCESSOR_ARCH_armv7_a
217 CSSELR_write((level & CCSELR_LEVEL_MASK) << CCSELR_LEVEL_SHIFT);
218 const unsigned sets = 1 +
219 ((CCSIDR_read() >> CCSIDR_NUMSETS_SHIFT) & CCSIDR_NUMSETS_MASK);
220 return sets;
221#endif
222 return 0;
223}
224
225unsigned dcache_levels(void)
226{
227 const uint32_t val = CLIDR_read();
228 unsigned levels = 0;
229 for (unsigned i = 1; i <= 7; ++i) {
230 const unsigned ctype = CLIDR_CACHE(i, val);
231 switch (ctype) {
232 case CLIDR_DCACHE_ONLY:
233 case CLIDR_SEP_CACHE:
234 case CLIDR_UNI_CACHE:
235 ++levels;
236 default:
237 (void)0;
238 }
239 }
240 return levels;
241}
242
243static void dcache_clean_manual(unsigned level, bool invalidate,
244 unsigned ways, unsigned sets, unsigned way_shift, unsigned set_shift)
245{
246
247 for (unsigned i = 0; i < ways; ++i) {
248 for (unsigned j = 0; j < sets; ++j) {
249 const uint32_t val =
250 ((level & 0x7) << 1) |
251 (j << set_shift) | (i << way_shift);
252 if (invalidate)
253 DCCISW_write(val);
254 else
255 DCCSW_write(val);
256 }
257 }
258}
259
260void dcache_flush(void)
261{
262 /* See ARM Architecture Reference Manual ch. B4.2.1 p. B4-1724 */
263 const unsigned levels = dcache_levels();
264 for (unsigned i = 0; i < levels; ++i) {
265 const unsigned ways = dcache_ways(i);
266 const unsigned sets = dcache_sets(i);
267 const unsigned way_shift = 31 - log2(ways);
268 const unsigned set_shift = dcache_linesize_log(i);
269 dcache_clean_manual(i, false, ways, sets, way_shift, set_shift);
270 }
271}
272
273void dcache_flush_invalidate(void)
274{
275 /* See ARM Architecture Reference Manual ch. B4.2.1 p. B4-1724 */
276 const unsigned levels = dcache_levels();
277 for (unsigned i = 0; i < levels; ++i) {
278 const unsigned ways = dcache_ways(i);
279 const unsigned sets = dcache_sets(i);
280 const unsigned way_shift = 31 - log2(ways);
281 const unsigned set_shift = dcache_linesize_log(i);
282 dcache_clean_manual(i, true, ways, sets, way_shift, set_shift);
283 }
284}
285
286
287void cpu_dcache_flush(void)
288{
289 for (unsigned i = 0; i < CPU->arch.dcache_levels; ++i)
290 dcache_clean_manual(i, false,
291 CPU->arch.dcache[i].ways, CPU->arch.dcache[i].sets,
292 CPU->arch.dcache[i].way_shift, CPU->arch.dcache[i].set_shift);
293}
294
295void cpu_dcache_flush_invalidate(void)
296{
297 const unsigned levels = dcache_levels();
298 for (unsigned i = 0; i < levels; ++i)
299 dcache_clean_manual(i, true,
300 CPU->arch.dcache[i].ways, CPU->arch.dcache[i].sets,
301 CPU->arch.dcache[i].way_shift, CPU->arch.dcache[i].set_shift);
302}
303
304void icache_invalidate(void)
305{
306 ICIALLU_write(0);
307}
308
309/** @}
310 */
Note: See TracBrowser for help on using the repository browser.