[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 | {
|
---|
[26e3db2] | 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 |
|
---|
[8ff9484] | 109 | // TODO CPUs with arch_num == 0xf use CPUID scheme for identification
|
---|
[bad1f53] | 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 | }
|
---|
[6b781c0] | 123 | }
|
---|
| 124 |
|
---|
[8316547f] | 125 | /** Enables unaligned access and caching for armv6+ */
|
---|
[d630139] | 126 | void cpu_arch_init(void)
|
---|
| 127 | {
|
---|
[a03b609] | 128 | uint32_t control_reg = SCTLR_read();
|
---|
[1bd99214] | 129 |
|
---|
[2826998] | 130 | /* Turn off tex remap, RAZ/WI prior to armv7 */
|
---|
[a03b609] | 131 | control_reg &= ~SCTLR_TEX_REMAP_EN_FLAG;
|
---|
[2826998] | 132 | /* Turn off accessed flag, RAZ/WI prior to armv7 */
|
---|
[a03b609] | 133 | control_reg &= ~(SCTLR_ACCESS_FLAG_EN_FLAG | SCTLR_HW_ACCESS_FLAG_EN_FLAG);
|
---|
[46a6a5d] | 134 |
|
---|
| 135 | /* Unaligned access is supported on armv6+ */
|
---|
| 136 | #if defined(PROCESSOR_ARCH_armv7_a) | defined(PROCESSOR_ARCH_armv6)
|
---|
[2826998] | 137 | /* Enable unaligned access, RAZ/WI prior to armv6
|
---|
| 138 | * switchable on armv6, RAO/WI writes on armv7,
|
---|
[8316547f] | 139 | * see ARM Architecture Reference Manual ARMv7-A and ARMv7-R edition
|
---|
| 140 | * L.3.1 (p. 2456) */
|
---|
[a03b609] | 141 | control_reg |= SCTLR_UNALIGNED_EN_FLAG;
|
---|
[8316547f] | 142 | /* Disable alignment checks, this turns unaligned access to undefined,
|
---|
| 143 | * unless U bit is set. */
|
---|
[a03b609] | 144 | control_reg &= ~SCTLR_ALIGN_CHECK_EN_FLAG;
|
---|
[8316547f] | 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)
|
---|
[46a6a5d] | 152 | * We are safe to turn this on. For arm v6 see ch L.6.2 (p. 2469)
|
---|
[7a38962] | 153 | * L2 Cache for armv7 is enabled by default (i.e. controlled by
|
---|
| 154 | * this flag).
|
---|
[8316547f] | 155 | */
|
---|
[a03b609] | 156 | control_reg |= SCTLR_CACHE_EN_FLAG;
|
---|
[46a6a5d] | 157 | #endif
|
---|
[7a38962] | 158 | #ifdef PROCESSOR_ARCH_armv7_a
|
---|
[8abcf4e] | 159 | /* ICache coherency is elaborated on in barrier.h.
|
---|
[7a38962] | 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;
|
---|
[8ff767b] | 168 | } else {
|
---|
| 169 | control_reg &=
|
---|
| 170 | ~(SCTLR_INST_CACHE_EN_FLAG | SCTLR_BRANCH_PREDICT_EN_FLAG);
|
---|
[7a38962] | 171 | }
|
---|
[46a6a5d] | 172 | #endif
|
---|
[a03b609] | 173 | SCTLR_write(control_reg);
|
---|
| 174 |
|
---|
[65871bb] | 175 | #ifdef CONFIG_FPU
|
---|
[36e5eb3] | 176 | fpu_setup();
|
---|
[65871bb] | 177 | #endif
|
---|
[3fa509b] | 178 |
|
---|
| 179 | #ifdef PROCESSOR_ARCH_armv7_a
|
---|
[c8a5c8c] | 180 | if ((ID_PFR1_read() & ID_PFR1_GEN_TIMER_EXT_MASK) !=
|
---|
| 181 | ID_PFR1_GEN_TIMER_EXT) {
|
---|
| 182 | PMCR_write(PMCR_read() | PMCR_E_FLAG | PMCR_D_FLAG);
|
---|
| 183 | PMCNTENSET_write(PMCNTENSET_CYCLE_COUNTER_EN_FLAG);
|
---|
| 184 | }
|
---|
[3fa509b] | 185 | #endif
|
---|
[d630139] | 186 | }
|
---|
| 187 |
|
---|
[6b781c0] | 188 | /** Retrieves processor identification and stores it to #CPU.arch */
|
---|
[f94b95b1] | 189 | void cpu_identify(void)
|
---|
[d630139] | 190 | {
|
---|
[6b781c0] | 191 | arch_cpu_identify(&CPU->arch);
|
---|
[d630139] | 192 | }
|
---|
| 193 |
|
---|
[6b781c0] | 194 | /** Prints CPU identification. */
|
---|
[d630139] | 195 | void cpu_print_report(cpu_t *m)
|
---|
| 196 | {
|
---|
[8ff9484] | 197 | printf("cpu%d: vendor=%s, architecture=%s, part number=%x, "
|
---|
[6b781c0] | 198 | "variant=%x, revision=%x\n",
|
---|
[8ff9484] | 199 | m->id, implementer(m->arch.imp_num),
|
---|
| 200 | architecture_string(&m->arch), m->arch.prim_part_num,
|
---|
| 201 | m->arch.variant_num, m->arch.rev_num);
|
---|
[d630139] | 202 | }
|
---|
| 203 |
|
---|
[bad1f53] | 204 | /** See chapter B4.1.19 of ARM Architecture Reference Manual */
|
---|
| 205 | static unsigned dcache_linesize_log(unsigned level)
|
---|
| 206 | {
|
---|
| 207 | #ifdef PROCESSOR_ARCH_armv7_a
|
---|
| 208 | CSSELR_write((level & CCSELR_LEVEL_MASK) << CCSELR_LEVEL_SHIFT);
|
---|
[8abcf4e] | 209 | const uint32_t ccsidr = CCSIDR_read();
|
---|
| 210 | return CCSIDR_LINESIZE_LOG(ccsidr);
|
---|
[bad1f53] | 211 | #endif
|
---|
| 212 | return 0;
|
---|
| 213 |
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | /** See chapter B4.1.19 of ARM Architecture Reference Manual */
|
---|
| 217 | static unsigned dcache_ways(unsigned level)
|
---|
| 218 | {
|
---|
| 219 | #ifdef PROCESSOR_ARCH_armv7_a
|
---|
| 220 | CSSELR_write((level & CCSELR_LEVEL_MASK) << CCSELR_LEVEL_SHIFT);
|
---|
[8abcf4e] | 221 | const uint32_t ccsidr = CCSIDR_read();
|
---|
| 222 | return CCSIDR_WAYS(ccsidr);
|
---|
[bad1f53] | 223 | #endif
|
---|
| 224 | return 0;
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | /** See chapter B4.1.19 of ARM Architecture Reference Manual */
|
---|
| 228 | static unsigned dcache_sets(unsigned level)
|
---|
| 229 | {
|
---|
| 230 | #ifdef PROCESSOR_ARCH_armv7_a
|
---|
| 231 | CSSELR_write((level & CCSELR_LEVEL_MASK) << CCSELR_LEVEL_SHIFT);
|
---|
[8abcf4e] | 232 | const uint32_t ccsidr = CCSIDR_read();
|
---|
| 233 | return CCSIDR_SETS(ccsidr);
|
---|
[bad1f53] | 234 | #endif
|
---|
| 235 | return 0;
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | unsigned dcache_levels(void)
|
---|
| 239 | {
|
---|
| 240 | unsigned levels = 0;
|
---|
[4b28c70] | 241 | #ifdef PROCESSOR_ARCH_armv7_a
|
---|
| 242 | const uint32_t val = CLIDR_read();
|
---|
[34847e2] | 243 | for (unsigned i = 0; i < 8; ++i) {
|
---|
[bad1f53] | 244 | const unsigned ctype = CLIDR_CACHE(i, val);
|
---|
| 245 | switch (ctype) {
|
---|
| 246 | case CLIDR_DCACHE_ONLY:
|
---|
| 247 | case CLIDR_SEP_CACHE:
|
---|
| 248 | case CLIDR_UNI_CACHE:
|
---|
| 249 | ++levels;
|
---|
| 250 | default:
|
---|
| 251 | (void)0;
|
---|
| 252 | }
|
---|
| 253 | }
|
---|
[4b28c70] | 254 | #endif
|
---|
[bad1f53] | 255 | return levels;
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | static void dcache_clean_manual(unsigned level, bool invalidate,
|
---|
| 259 | unsigned ways, unsigned sets, unsigned way_shift, unsigned set_shift)
|
---|
| 260 | {
|
---|
| 261 |
|
---|
| 262 | for (unsigned i = 0; i < ways; ++i) {
|
---|
| 263 | for (unsigned j = 0; j < sets; ++j) {
|
---|
| 264 | const uint32_t val =
|
---|
| 265 | ((level & 0x7) << 1) |
|
---|
| 266 | (j << set_shift) | (i << way_shift);
|
---|
| 267 | if (invalidate)
|
---|
| 268 | DCCISW_write(val);
|
---|
| 269 | else
|
---|
| 270 | DCCSW_write(val);
|
---|
| 271 | }
|
---|
| 272 | }
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | void dcache_flush(void)
|
---|
| 276 | {
|
---|
| 277 | /* See ARM Architecture Reference Manual ch. B4.2.1 p. B4-1724 */
|
---|
| 278 | const unsigned levels = dcache_levels();
|
---|
| 279 | for (unsigned i = 0; i < levels; ++i) {
|
---|
| 280 | const unsigned ways = dcache_ways(i);
|
---|
| 281 | const unsigned sets = dcache_sets(i);
|
---|
[8abcf4e] | 282 | const unsigned way_shift = 32 - log2(ways);
|
---|
[bad1f53] | 283 | const unsigned set_shift = dcache_linesize_log(i);
|
---|
| 284 | dcache_clean_manual(i, false, ways, sets, way_shift, set_shift);
|
---|
| 285 | }
|
---|
| 286 | }
|
---|
| 287 |
|
---|
| 288 | void dcache_flush_invalidate(void)
|
---|
| 289 | {
|
---|
| 290 | /* See ARM Architecture Reference Manual ch. B4.2.1 p. B4-1724 */
|
---|
| 291 | const unsigned levels = dcache_levels();
|
---|
| 292 | for (unsigned i = 0; i < levels; ++i) {
|
---|
| 293 | const unsigned ways = dcache_ways(i);
|
---|
| 294 | const unsigned sets = dcache_sets(i);
|
---|
[8abcf4e] | 295 | const unsigned way_shift = 32 - log2(ways);
|
---|
[bad1f53] | 296 | const unsigned set_shift = dcache_linesize_log(i);
|
---|
| 297 | dcache_clean_manual(i, true, ways, sets, way_shift, set_shift);
|
---|
| 298 | }
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 |
|
---|
| 302 | void cpu_dcache_flush(void)
|
---|
| 303 | {
|
---|
| 304 | for (unsigned i = 0; i < CPU->arch.dcache_levels; ++i)
|
---|
| 305 | dcache_clean_manual(i, false,
|
---|
| 306 | CPU->arch.dcache[i].ways, CPU->arch.dcache[i].sets,
|
---|
| 307 | CPU->arch.dcache[i].way_shift, CPU->arch.dcache[i].set_shift);
|
---|
| 308 | }
|
---|
| 309 |
|
---|
| 310 | void cpu_dcache_flush_invalidate(void)
|
---|
| 311 | {
|
---|
| 312 | const unsigned levels = dcache_levels();
|
---|
| 313 | for (unsigned i = 0; i < levels; ++i)
|
---|
| 314 | dcache_clean_manual(i, true,
|
---|
| 315 | CPU->arch.dcache[i].ways, CPU->arch.dcache[i].sets,
|
---|
| 316 | CPU->arch.dcache[i].way_shift, CPU->arch.dcache[i].set_shift);
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 | void icache_invalidate(void)
|
---|
| 320 | {
|
---|
| 321 | ICIALLU_write(0);
|
---|
| 322 | }
|
---|
| 323 |
|
---|
[d630139] | 324 | /** @}
|
---|
| 325 | */
|
---|