[d630139] | 1 | /*
|
---|
[6b781c0] | 2 | * Copyright (c) 2007 Michal Kebrt
|
---|
[d630139] | 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
|
---|
[6b781c0] | 33 | * @brief CPU identification.
|
---|
[d630139] | 34 | */
|
---|
| 35 |
|
---|
[bad1f53] | 36 | #include <arch/cache.h>
|
---|
[6b781c0] | 37 | #include <arch/cpu.h>
|
---|
[bad1f53] | 38 | #include <arch/cp15.h>
|
---|
[d630139] | 39 | #include <cpu.h>
|
---|
[6b781c0] | 40 | #include <arch.h>
|
---|
[e762b43] | 41 | #include <print.h>
|
---|
[d630139] | 42 |
|
---|
[bad1f53] | 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 |
|
---|
[8ff9484] | 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 | }
|
---|
[6b781c0] | 73 |
|
---|
| 74 | /** Architecture names */
|
---|
[8ff9484] | 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 | }
|
---|
[6b781c0] | 92 |
|
---|
| 93 |
|
---|
| 94 | /** Retrieves processor identification from CP15 register 0.
|
---|
[04cb6957] | 95 | *
|
---|
[6b781c0] | 96 | * @param cpu Structure for storing CPU identification.
|
---|
[8ff9484] | 97 | * See page B4-1630 of ARM Architecture Reference Manual.
|
---|
[6b781c0] | 98 | */
|
---|
| 99 | static void arch_cpu_identify(cpu_arch_t *cpu)
|
---|
| 100 | {
|
---|
| 101 | uint32_t ident;
|
---|
| 102 | asm volatile (
|
---|
[e762b43] | 103 | "mrc p15, 0, %[ident], c0, c0, 0\n"
|
---|
| 104 | : [ident] "=r" (ident)
|
---|
[6b781c0] | 105 | );
|
---|
[e762b43] | 106 |
|
---|
[6b781c0] | 107 | cpu->imp_num = ident >> 24;
|
---|
| 108 | cpu->variant_num = (ident << 8) >> 28;
|
---|
| 109 | cpu->arch_num = (ident << 12) >> 28;
|
---|
| 110 | cpu->prim_part_num = (ident << 16) >> 20;
|
---|
| 111 | cpu->rev_num = (ident << 28) >> 28;
|
---|
[8ff9484] | 112 | // TODO CPUs with arch_num == 0xf use CPUID scheme for identification
|
---|
[bad1f53] | 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 | }
|
---|
[6b781c0] | 126 | }
|
---|
| 127 |
|
---|
[8316547f] | 128 | /** Enables unaligned access and caching for armv6+ */
|
---|
[d630139] | 129 | void cpu_arch_init(void)
|
---|
| 130 | {
|
---|
[a03b609] | 131 | uint32_t control_reg = SCTLR_read();
|
---|
[1bd99214] | 132 |
|
---|
[2826998] | 133 | /* Turn off tex remap, RAZ/WI prior to armv7 */
|
---|
[a03b609] | 134 | control_reg &= ~SCTLR_TEX_REMAP_EN_FLAG;
|
---|
[2826998] | 135 | /* Turn off accessed flag, RAZ/WI prior to armv7 */
|
---|
[a03b609] | 136 | control_reg &= ~(SCTLR_ACCESS_FLAG_EN_FLAG | SCTLR_HW_ACCESS_FLAG_EN_FLAG);
|
---|
[e55fcd2] | 137 | /* Disable branch prediction RAZ/WI if not supported */
|
---|
[a03b609] | 138 | control_reg &= ~SCTLR_BRANCH_PREDICT_EN_FLAG;
|
---|
[46a6a5d] | 139 |
|
---|
| 140 | /* Unaligned access is supported on armv6+ */
|
---|
| 141 | #if defined(PROCESSOR_ARCH_armv7_a) | defined(PROCESSOR_ARCH_armv6)
|
---|
[2826998] | 142 | /* Enable unaligned access, RAZ/WI prior to armv6
|
---|
| 143 | * switchable on armv6, RAO/WI writes on armv7,
|
---|
[8316547f] | 144 | * see ARM Architecture Reference Manual ARMv7-A and ARMv7-R edition
|
---|
| 145 | * L.3.1 (p. 2456) */
|
---|
[a03b609] | 146 | control_reg |= SCTLR_UNALIGNED_EN_FLAG;
|
---|
[8316547f] | 147 | /* Disable alignment checks, this turns unaligned access to undefined,
|
---|
| 148 | * unless U bit is set. */
|
---|
[a03b609] | 149 | control_reg &= ~SCTLR_ALIGN_CHECK_EN_FLAG;
|
---|
[8316547f] | 150 | /* Enable caching, On arm prior to armv7 there is only one level
|
---|
| 151 | * of caches. Data cache is coherent.
|
---|
| 152 | * "This means that the behavior of accesses from the same observer to
|
---|
| 153 | * different VAs, that are translated to the same PA
|
---|
| 154 | * with the same memory attributes, is fully coherent."
|
---|
| 155 | * ARM Architecture Reference Manual ARMv7-A and ARMv7-R Edition
|
---|
| 156 | * B3.11.1 (p. 1383)
|
---|
[46a6a5d] | 157 | * We are safe to turn this on. For arm v6 see ch L.6.2 (p. 2469)
|
---|
| 158 | * L2 Cache for armv7 was enabled in boot code.
|
---|
[8316547f] | 159 | */
|
---|
[a03b609] | 160 | control_reg |= SCTLR_CACHE_EN_FLAG;
|
---|
[46a6a5d] | 161 | #endif
|
---|
| 162 | #ifdef PROCESSOR_cortex_a8
|
---|
| 163 | /* ICache coherency is elaborate on in barrier.h.
|
---|
| 164 | * Cortex-A8 implements IVIPT extension.
|
---|
| 165 | * Cortex-A8 TRM ch. 7.2.6 p. 7-4 (PDF 245) */
|
---|
[a03b609] | 166 | control_reg |= SCTLR_INST_CACHE_EN_FLAG;
|
---|
[46a6a5d] | 167 | #endif
|
---|
[a03b609] | 168 | SCTLR_write(control_reg);
|
---|
| 169 |
|
---|
[65871bb] | 170 | #ifdef CONFIG_FPU
|
---|
[36e5eb3] | 171 | fpu_setup();
|
---|
[65871bb] | 172 | #endif
|
---|
[d630139] | 173 | }
|
---|
| 174 |
|
---|
[6b781c0] | 175 | /** Retrieves processor identification and stores it to #CPU.arch */
|
---|
[f94b95b1] | 176 | void cpu_identify(void)
|
---|
[d630139] | 177 | {
|
---|
[6b781c0] | 178 | arch_cpu_identify(&CPU->arch);
|
---|
[d630139] | 179 | }
|
---|
| 180 |
|
---|
[6b781c0] | 181 | /** Prints CPU identification. */
|
---|
[d630139] | 182 | void cpu_print_report(cpu_t *m)
|
---|
| 183 | {
|
---|
[8ff9484] | 184 | printf("cpu%d: vendor=%s, architecture=%s, part number=%x, "
|
---|
[6b781c0] | 185 | "variant=%x, revision=%x\n",
|
---|
[8ff9484] | 186 | m->id, implementer(m->arch.imp_num),
|
---|
| 187 | architecture_string(&m->arch), m->arch.prim_part_num,
|
---|
| 188 | m->arch.variant_num, m->arch.rev_num);
|
---|
[d630139] | 189 | }
|
---|
| 190 |
|
---|
[bad1f53] | 191 | /** See chapter B4.1.19 of ARM Architecture Reference Manual */
|
---|
| 192 | static unsigned dcache_linesize_log(unsigned level)
|
---|
| 193 | {
|
---|
| 194 | #ifdef PROCESSOR_ARCH_armv7_a
|
---|
| 195 | CSSELR_write((level & CCSELR_LEVEL_MASK) << CCSELR_LEVEL_SHIFT);
|
---|
| 196 | const unsigned ls_log = 2 +
|
---|
| 197 | ((CCSIDR_read() >> CCSIDR_LINESIZE_SHIFT) & CCSIDR_LINESIZE_MASK);
|
---|
| 198 | return ls_log + 2; //return log2(bytes)
|
---|
| 199 | #endif
|
---|
| 200 | return 0;
|
---|
| 201 |
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | /** See chapter B4.1.19 of ARM Architecture Reference Manual */
|
---|
| 205 | static unsigned dcache_ways(unsigned level)
|
---|
| 206 | {
|
---|
| 207 | #ifdef PROCESSOR_ARCH_armv7_a
|
---|
| 208 | CSSELR_write((level & CCSELR_LEVEL_MASK) << CCSELR_LEVEL_SHIFT);
|
---|
| 209 | const unsigned ways = 1 +
|
---|
| 210 | ((CCSIDR_read() >> CCSIDR_ASSOC_SHIFT) & CCSIDR_ASSOC_MASK);
|
---|
| 211 | return ways;
|
---|
| 212 | #endif
|
---|
| 213 | return 0;
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | /** See chapter B4.1.19 of ARM Architecture Reference Manual */
|
---|
| 217 | static unsigned dcache_sets(unsigned level)
|
---|
| 218 | {
|
---|
| 219 | #ifdef PROCESSOR_ARCH_armv7_a
|
---|
| 220 | CSSELR_write((level & CCSELR_LEVEL_MASK) << CCSELR_LEVEL_SHIFT);
|
---|
| 221 | const unsigned sets = 1 +
|
---|
| 222 | ((CCSIDR_read() >> CCSIDR_NUMSETS_SHIFT) & CCSIDR_NUMSETS_MASK);
|
---|
| 223 | return sets;
|
---|
| 224 | #endif
|
---|
| 225 | return 0;
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | unsigned dcache_levels(void)
|
---|
| 229 | {
|
---|
| 230 | const uint32_t val = CLIDR_read();
|
---|
| 231 | unsigned levels = 0;
|
---|
| 232 | for (unsigned i = 1; i <= 7; ++i) {
|
---|
| 233 | const unsigned ctype = CLIDR_CACHE(i, val);
|
---|
| 234 | switch (ctype) {
|
---|
| 235 | case CLIDR_DCACHE_ONLY:
|
---|
| 236 | case CLIDR_SEP_CACHE:
|
---|
| 237 | case CLIDR_UNI_CACHE:
|
---|
| 238 | ++levels;
|
---|
| 239 | default:
|
---|
| 240 | (void)0;
|
---|
| 241 | }
|
---|
| 242 | }
|
---|
| 243 | return levels;
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | static void dcache_clean_manual(unsigned level, bool invalidate,
|
---|
| 247 | unsigned ways, unsigned sets, unsigned way_shift, unsigned set_shift)
|
---|
| 248 | {
|
---|
| 249 |
|
---|
| 250 | for (unsigned i = 0; i < ways; ++i) {
|
---|
| 251 | for (unsigned j = 0; j < sets; ++j) {
|
---|
| 252 | const uint32_t val =
|
---|
| 253 | ((level & 0x7) << 1) |
|
---|
| 254 | (j << set_shift) | (i << way_shift);
|
---|
| 255 | if (invalidate)
|
---|
| 256 | DCCISW_write(val);
|
---|
| 257 | else
|
---|
| 258 | DCCSW_write(val);
|
---|
| 259 | }
|
---|
| 260 | }
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | void dcache_flush(void)
|
---|
| 264 | {
|
---|
| 265 | /* See ARM Architecture Reference Manual ch. B4.2.1 p. B4-1724 */
|
---|
| 266 | const unsigned levels = dcache_levels();
|
---|
| 267 | for (unsigned i = 0; i < levels; ++i) {
|
---|
| 268 | const unsigned ways = dcache_ways(i);
|
---|
| 269 | const unsigned sets = dcache_sets(i);
|
---|
| 270 | const unsigned way_shift = 31 - log2(ways);
|
---|
| 271 | const unsigned set_shift = dcache_linesize_log(i);
|
---|
| 272 | dcache_clean_manual(i, false, ways, sets, way_shift, set_shift);
|
---|
| 273 | }
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | void dcache_flush_invalidate(void)
|
---|
| 277 | {
|
---|
| 278 | /* See ARM Architecture Reference Manual ch. B4.2.1 p. B4-1724 */
|
---|
| 279 | const unsigned levels = dcache_levels();
|
---|
| 280 | for (unsigned i = 0; i < levels; ++i) {
|
---|
| 281 | const unsigned ways = dcache_ways(i);
|
---|
| 282 | const unsigned sets = dcache_sets(i);
|
---|
| 283 | const unsigned way_shift = 31 - log2(ways);
|
---|
| 284 | const unsigned set_shift = dcache_linesize_log(i);
|
---|
| 285 | dcache_clean_manual(i, true, ways, sets, way_shift, set_shift);
|
---|
| 286 | }
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 |
|
---|
| 290 | void cpu_dcache_flush(void)
|
---|
| 291 | {
|
---|
| 292 | for (unsigned i = 0; i < CPU->arch.dcache_levels; ++i)
|
---|
| 293 | dcache_clean_manual(i, false,
|
---|
| 294 | CPU->arch.dcache[i].ways, CPU->arch.dcache[i].sets,
|
---|
| 295 | CPU->arch.dcache[i].way_shift, CPU->arch.dcache[i].set_shift);
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 | void cpu_dcache_flush_invalidate(void)
|
---|
| 299 | {
|
---|
| 300 | const unsigned levels = dcache_levels();
|
---|
| 301 | for (unsigned i = 0; i < levels; ++i)
|
---|
| 302 | dcache_clean_manual(i, true,
|
---|
| 303 | CPU->arch.dcache[i].ways, CPU->arch.dcache[i].sets,
|
---|
| 304 | CPU->arch.dcache[i].way_shift, CPU->arch.dcache[i].set_shift);
|
---|
| 305 | }
|
---|
| 306 |
|
---|
| 307 | void icache_invalidate(void)
|
---|
| 308 | {
|
---|
| 309 | ICIALLU_write(0);
|
---|
| 310 | }
|
---|
| 311 |
|
---|
[d630139] | 312 | /** @}
|
---|
| 313 | */
|
---|