2 * This file is part of the coreboot project.
4 * Copyright (C) 2000,2007 Ronald G. Minnich <rminnich@gmail.com>
5 * Copyright (C) 2007-2008 coresystems GmbH
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <cpu/x86/mtrr.h>
18 #include <cpu/x86/cache.h>
19 #include <cpu/x86/post_code.h>
21 /* The full cache-as-ram size includes the cache-as-ram portion from coreboot
22 * and the space used by the reference code. These 2 values combined should
23 * be a power of 2 because the MTRR setup assumes that. */
24 #define CACHE_AS_RAM_SIZE \
25 (CONFIG_DCACHE_RAM_SIZE + CONFIG_DCACHE_RAM_MRC_VAR_SIZE)
26 #define CACHE_AS_RAM_BASE CONFIG_DCACHE_RAM_BASE
28 /* Cache 4GB - MRC_SIZE_KB for MRC */
29 #define CACHE_MRC_BYTES ((CONFIG_CACHE_MRC_SIZE_KB << 10) - 1)
30 #define CACHE_MRC_BASE (0xFFFFFFFF - CACHE_MRC_BYTES)
31 #define CACHE_MRC_MASK (~CACHE_MRC_BYTES)
33 #define CPU_PHYSMASK_HI (1 << (CONFIG_CPU_ADDR_BITS - 32) - 1)
35 #define NoEvictMod_MSR 0x2e0
37 /* Save the BIST result. */
43 /* Send INIT IPI to all excluding ourself. */
44 movl $0x000C4500, %eax
45 movl $0xFEE00300, %esi
48 /* All CPUs need to be in Wait for SIPI state */
55 /* Zero out all fixed range and variable range MTRRs. */
56 movl $mtrr_table, %esi
57 movl $((mtrr_table_end - mtrr_table) >> 1), %edi
69 /* Configure the default memory type to uncacheable. */
70 movl $MTRR_DEF_TYPE_MSR, %ecx
72 andl $(~0x00000cff), %eax
76 /* Set Cache-as-RAM base address. */
77 movl $(MTRR_PHYS_BASE(0)), %ecx
78 movl $(CACHE_AS_RAM_BASE | MTRR_TYPE_WRBACK), %eax
83 /* Set Cache-as-RAM mask. */
84 movl $(MTRR_PHYS_MASK(0)), %ecx
85 movl $(~(CACHE_AS_RAM_SIZE - 1) | MTRR_PHYS_MASK_VALID), %eax
86 movl $CPU_PHYSMASK_HI, %edx
92 movl $MTRR_DEF_TYPE_MSR, %ecx
94 orl $MTRR_DEF_TYPE_EN, %eax
97 /* Enable cache (CR0.CD = 0, CR0.NW = 0). */
99 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
103 /* enable the 'no eviction' mode */
104 movl $NoEvictMod_MSR, %ecx
110 /* Clear the cache memory region. This will also fill up the cache. */
111 movl $CACHE_AS_RAM_BASE, %esi
113 movl $(CACHE_AS_RAM_SIZE >> 2), %ecx
114 // movl $0x23322332, %eax
118 /* enable the 'no eviction run' state */
119 movl $NoEvictMod_MSR, %ecx
125 /* Enable Cache-as-RAM mode by disabling cache. */
127 orl $CR0_CacheDisable, %eax
130 /* Enable cache for our code in Flash because we do XIP here */
131 movl $MTRR_PHYS_BASE(1), %ecx
134 * IMPORTANT: The following calculation _must_ be done at runtime. See
135 * https://www.coreboot.org/pipermail/coreboot/2010-October/060855.html
137 movl $copy_and_run, %eax
138 andl $(~(CONFIG_XIP_ROM_SIZE - 1)), %eax
139 orl $MTRR_TYPE_WRPROT, %eax
142 movl $MTRR_PHYS_MASK(1), %ecx
143 movl $CPU_PHYSMASK_HI, %edx
144 movl $(~(CONFIG_XIP_ROM_SIZE - 1) | MTRR_PHYS_MASK_VALID), %eax
148 /* Enable caching for RAM init code to run faster */
149 movl $MTRR_PHYS_BASE(2), %ecx
150 movl $(CACHE_MRC_BASE | MTRR_TYPE_WRPROT), %eax
153 movl $MTRR_PHYS_MASK(2), %ecx
154 movl $(CACHE_MRC_MASK | MTRR_PHYS_MASK_VALID), %eax
155 movl $CPU_PHYSMASK_HI, %edx
161 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
164 /* Setup the stack. */
165 movl $(CONFIG_DCACHE_RAM_BASE + CONFIG_DCACHE_RAM_SIZE), %eax
168 /* Restore the BIST result. */
175 /* Call romstage.c main function. */
177 /* Save return value from romstage_main. It contains the stack to use
178 * after cache-as-ram is torn down. It also contains the information
179 * for setting up MTRRs. */
186 orl $CR0_CacheDisable, %eax
192 movl $MTRR_DEF_TYPE_MSR, %ecx
194 andl $(~MTRR_DEF_TYPE_EN), %eax
199 /* Disable the no eviction run state */
200 movl $NoEvictMod_MSR, %ecx
207 /* Disable the no eviction mode */
216 andl $~(CR0_CacheDisable | CR0_NoWriteThrough), %eax
223 orl $CR0_CacheDisable, %eax
228 /* Get number of MTRRs. */
230 movl $MTRR_PHYS_BASE(0), %ecx
235 /* Low 32 bits of MTRR base. */
237 /* Upper 32 bits of MTRR base. */
239 /* Write MTRR base. */
242 /* Low 32 bits of MTRR mask. */
244 /* Upper 32 bits of MTRR mask. */
246 /* Write MTRR mask. */
255 /* And enable cache again after setting MTRRs. */
257 andl $~(CR0_CacheDisable | CR0_NoWriteThrough), %eax
263 movl $MTRR_DEF_TYPE_MSR, %ecx
265 orl $MTRR_DEF_TYPE_EN, %eax
270 /* Invalidate the cache again. */
276 post_code(POST_PREPARE_RAMSTAGE)
277 cld /* Clear direction flag. */
278 call romstage_after_car
281 post_code(POST_DEAD_CODE)
287 .word 0x250, 0x258, 0x259
288 .word 0x268, 0x269, 0x26A
289 .word 0x26B, 0x26C, 0x26D
292 .word 0x200, 0x201, 0x202, 0x203
293 .word 0x204, 0x205, 0x206, 0x207
294 .word 0x208, 0x209, 0x20A, 0x20B
295 .word 0x20C, 0x20D, 0x20E, 0x20F
296 .word 0x210, 0x211, 0x212, 0x213