| 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 is enabled by default (i.e. controlled by
|
|---|
| 154 | * this flag).
|
|---|
| 155 | */
|
|---|
| 156 | control_reg |= SCTLR_CACHE_EN_FLAG;
|
|---|
| 157 | #endif
|
|---|
| 158 | #ifdef PROCESSOR_ARCH_armv7_a
|
|---|
| 159 | /* ICache coherency is elaborate on in barrier.h.
|
|---|
| 160 | * VIPT and PIPT caches need maintenance only on code modify,
|
|---|
| 161 | * so it should be safe for general use.
|
|---|
| 162 | * Enable branch predictors too as they follow the same rules
|
|---|
| 163 | * as ICache and they can be flushed together
|
|---|
| 164 | */
|
|---|
| 165 | if ((CTR_read() & CTR_L1I_POLICY_MASK) != CTR_L1I_POLICY_AIVIVT) {
|
|---|
| 166 | control_reg |=
|
|---|
| 167 | SCTLR_INST_CACHE_EN_FLAG | SCTLR_BRANCH_PREDICT_EN_FLAG;
|
|---|
| 168 | }
|
|---|
| 169 | #endif
|
|---|
| 170 | SCTLR_write(control_reg);
|
|---|
| 171 |
|
|---|
| 172 | #ifdef CONFIG_FPU
|
|---|
| 173 | fpu_setup();
|
|---|
| 174 | #endif
|
|---|
| 175 |
|
|---|
| 176 | #ifdef PROCESSOR_ARCH_armv7_a
|
|---|
| 177 | PMCR_write(PMCR_read() | PMCR_E_FLAG | PMCR_D_FLAG);
|
|---|
| 178 | PMCNTENSET_write(PMCNTENSET_CYCLE_COUNTER_EN_FLAG);
|
|---|
| 179 | #endif
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | /** Retrieves processor identification and stores it to #CPU.arch */
|
|---|
| 183 | void cpu_identify(void)
|
|---|
| 184 | {
|
|---|
| 185 | arch_cpu_identify(&CPU->arch);
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | /** Prints CPU identification. */
|
|---|
| 189 | void cpu_print_report(cpu_t *m)
|
|---|
| 190 | {
|
|---|
| 191 | printf("cpu%d: vendor=%s, architecture=%s, part number=%x, "
|
|---|
| 192 | "variant=%x, revision=%x\n",
|
|---|
| 193 | m->id, implementer(m->arch.imp_num),
|
|---|
| 194 | architecture_string(&m->arch), m->arch.prim_part_num,
|
|---|
| 195 | m->arch.variant_num, m->arch.rev_num);
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | /** See chapter B4.1.19 of ARM Architecture Reference Manual */
|
|---|
| 199 | static unsigned dcache_linesize_log(unsigned level)
|
|---|
| 200 | {
|
|---|
| 201 | #ifdef PROCESSOR_ARCH_armv7_a
|
|---|
| 202 | CSSELR_write((level & CCSELR_LEVEL_MASK) << CCSELR_LEVEL_SHIFT);
|
|---|
| 203 | const unsigned ls_log = 2 +
|
|---|
| 204 | ((CCSIDR_read() >> CCSIDR_LINESIZE_SHIFT) & CCSIDR_LINESIZE_MASK);
|
|---|
| 205 | return ls_log + 2; //return log2(bytes)
|
|---|
| 206 | #endif
|
|---|
| 207 | return 0;
|
|---|
| 208 |
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | /** See chapter B4.1.19 of ARM Architecture Reference Manual */
|
|---|
| 212 | static unsigned dcache_ways(unsigned level)
|
|---|
| 213 | {
|
|---|
| 214 | #ifdef PROCESSOR_ARCH_armv7_a
|
|---|
| 215 | CSSELR_write((level & CCSELR_LEVEL_MASK) << CCSELR_LEVEL_SHIFT);
|
|---|
| 216 | const unsigned ways = 1 +
|
|---|
| 217 | ((CCSIDR_read() >> CCSIDR_ASSOC_SHIFT) & CCSIDR_ASSOC_MASK);
|
|---|
| 218 | return ways;
|
|---|
| 219 | #endif
|
|---|
| 220 | return 0;
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | /** See chapter B4.1.19 of ARM Architecture Reference Manual */
|
|---|
| 224 | static unsigned dcache_sets(unsigned level)
|
|---|
| 225 | {
|
|---|
| 226 | #ifdef PROCESSOR_ARCH_armv7_a
|
|---|
| 227 | CSSELR_write((level & CCSELR_LEVEL_MASK) << CCSELR_LEVEL_SHIFT);
|
|---|
| 228 | const unsigned sets = 1 +
|
|---|
| 229 | ((CCSIDR_read() >> CCSIDR_NUMSETS_SHIFT) & CCSIDR_NUMSETS_MASK);
|
|---|
| 230 | return sets;
|
|---|
| 231 | #endif
|
|---|
| 232 | return 0;
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| 235 | unsigned dcache_levels(void)
|
|---|
| 236 | {
|
|---|
| 237 | unsigned levels = 0;
|
|---|
| 238 | #ifdef PROCESSOR_ARCH_armv7_a
|
|---|
| 239 | const uint32_t val = CLIDR_read();
|
|---|
| 240 | for (unsigned i = 1; i <= 7; ++i) {
|
|---|
| 241 | const unsigned ctype = CLIDR_CACHE(i, val);
|
|---|
| 242 | switch (ctype) {
|
|---|
| 243 | case CLIDR_DCACHE_ONLY:
|
|---|
| 244 | case CLIDR_SEP_CACHE:
|
|---|
| 245 | case CLIDR_UNI_CACHE:
|
|---|
| 246 | ++levels;
|
|---|
| 247 | default:
|
|---|
| 248 | (void)0;
|
|---|
| 249 | }
|
|---|
| 250 | }
|
|---|
| 251 | #endif
|
|---|
| 252 | return levels;
|
|---|
| 253 | }
|
|---|
| 254 |
|
|---|
| 255 | static void dcache_clean_manual(unsigned level, bool invalidate,
|
|---|
| 256 | unsigned ways, unsigned sets, unsigned way_shift, unsigned set_shift)
|
|---|
| 257 | {
|
|---|
| 258 |
|
|---|
| 259 | for (unsigned i = 0; i < ways; ++i) {
|
|---|
| 260 | for (unsigned j = 0; j < sets; ++j) {
|
|---|
| 261 | const uint32_t val =
|
|---|
| 262 | ((level & 0x7) << 1) |
|
|---|
| 263 | (j << set_shift) | (i << way_shift);
|
|---|
| 264 | if (invalidate)
|
|---|
| 265 | DCCISW_write(val);
|
|---|
| 266 | else
|
|---|
| 267 | DCCSW_write(val);
|
|---|
| 268 | }
|
|---|
| 269 | }
|
|---|
| 270 | }
|
|---|
| 271 |
|
|---|
| 272 | void dcache_flush(void)
|
|---|
| 273 | {
|
|---|
| 274 | /* See ARM Architecture Reference Manual ch. B4.2.1 p. B4-1724 */
|
|---|
| 275 | const unsigned levels = dcache_levels();
|
|---|
| 276 | for (unsigned i = 0; i < levels; ++i) {
|
|---|
| 277 | const unsigned ways = dcache_ways(i);
|
|---|
| 278 | const unsigned sets = dcache_sets(i);
|
|---|
| 279 | const unsigned way_shift = 31 - log2(ways);
|
|---|
| 280 | const unsigned set_shift = dcache_linesize_log(i);
|
|---|
| 281 | dcache_clean_manual(i, false, ways, sets, way_shift, set_shift);
|
|---|
| 282 | }
|
|---|
| 283 | }
|
|---|
| 284 |
|
|---|
| 285 | void dcache_flush_invalidate(void)
|
|---|
| 286 | {
|
|---|
| 287 | /* See ARM Architecture Reference Manual ch. B4.2.1 p. B4-1724 */
|
|---|
| 288 | const unsigned levels = dcache_levels();
|
|---|
| 289 | for (unsigned i = 0; i < levels; ++i) {
|
|---|
| 290 | const unsigned ways = dcache_ways(i);
|
|---|
| 291 | const unsigned sets = dcache_sets(i);
|
|---|
| 292 | const unsigned way_shift = 31 - log2(ways);
|
|---|
| 293 | const unsigned set_shift = dcache_linesize_log(i);
|
|---|
| 294 | dcache_clean_manual(i, true, ways, sets, way_shift, set_shift);
|
|---|
| 295 | }
|
|---|
| 296 | }
|
|---|
| 297 |
|
|---|
| 298 |
|
|---|
| 299 | void cpu_dcache_flush(void)
|
|---|
| 300 | {
|
|---|
| 301 | for (unsigned i = 0; i < CPU->arch.dcache_levels; ++i)
|
|---|
| 302 | dcache_clean_manual(i, false,
|
|---|
| 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 cpu_dcache_flush_invalidate(void)
|
|---|
| 308 | {
|
|---|
| 309 | const unsigned levels = dcache_levels();
|
|---|
| 310 | for (unsigned i = 0; i < levels; ++i)
|
|---|
| 311 | dcache_clean_manual(i, true,
|
|---|
| 312 | CPU->arch.dcache[i].ways, CPU->arch.dcache[i].sets,
|
|---|
| 313 | CPU->arch.dcache[i].way_shift, CPU->arch.dcache[i].set_shift);
|
|---|
| 314 | }
|
|---|
| 315 |
|
|---|
| 316 | void icache_invalidate(void)
|
|---|
| 317 | {
|
|---|
| 318 | ICIALLU_write(0);
|
|---|
| 319 | }
|
|---|
| 320 |
|
|---|
| 321 | /** @}
|
|---|
| 322 | */
|
|---|