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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <cpu/x86/stack.h>
22 #include <cpu/x86/mtrr.h>
23 #include <cpu/x86/post_code.h>
26 #define CACHE_AS_RAM_SIZE CONFIG_DCACHE_RAM_SIZE
27 #define CACHE_AS_RAM_BASE CONFIG_DCACHE_RAM_BASE
29 /* Cache 4GB - MRC_SIZE_KB for MRC */
30 #define CACHE_MRC_BYTES ((CONFIG_CACHE_MRC_SIZE_KB << 10) - 1)
31 #define CACHE_MRC_BASE (0xFFFFFFFF - CACHE_MRC_BYTES)
32 #define CACHE_MRC_MASK (~CACHE_MRC_BYTES)
34 #define CPU_MAXPHYSADDR 36
35 #define CPU_PHYSMASK_HI (1 << (CPU_MAXPHYSADDR - 32) - 1)
37 #define NoEvictMod_MSR 0x2e0
39 /* Save the BIST result. */
45 /* Send INIT IPI to all excluding ourself. */
46 movl $0x000C4500, %eax
47 movl $0xFEE00300, %esi
50 /* All CPUs need to be in Wait for SIPI state */
57 /* Zero out all fixed range and variable range MTRRs. */
58 movl $mtrr_table, %esi
59 movl $((mtrr_table_end - mtrr_table) / 2), %edi
71 /* Configure the default memory type to uncacheable. */
72 movl $MTRRdefType_MSR, %ecx
74 andl $(~0x00000cff), %eax
78 /* Set Cache-as-RAM base address. */
79 movl $(MTRRphysBase_MSR(0)), %ecx
80 movl $(CACHE_AS_RAM_BASE | MTRR_TYPE_WRBACK), %eax
85 /* Set Cache-as-RAM mask. */
86 movl $(MTRRphysMask_MSR(0)), %ecx
87 movl $(~(CACHE_AS_RAM_SIZE - 1) | MTRRphysMaskValid), %eax
88 movl $CPU_PHYSMASK_HI, %edx
94 movl $MTRRdefType_MSR, %ecx
96 orl $MTRRdefTypeEn, %eax
99 /* Enable cache (CR0.CD = 0, CR0.NW = 0). */
101 andl $(~((1 << 30) | (1 << 29))), %eax
105 /* enable the 'no eviction' mode */
106 movl $NoEvictMod_MSR, %ecx
112 /* Clear the cache memory region. This will also fill up the cache */
113 movl $CACHE_AS_RAM_BASE, %esi
115 movl $(CACHE_AS_RAM_SIZE / 4), %ecx
116 // movl $0x23322332, %eax
120 /* enable the 'no eviction run' state */
121 movl $NoEvictMod_MSR, %ecx
127 /* Enable Cache-as-RAM mode by disabling cache. */
132 /* Enable cache for our code in Flash because we do XIP here */
133 movl $MTRRphysBase_MSR(1), %ecx
136 * IMPORTANT: The following calculation _must_ be done at runtime. See
137 * http://www.coreboot.org/pipermail/coreboot/2010-October/060855.html
139 movl $copy_and_run, %eax
140 andl $(~(CONFIG_XIP_ROM_SIZE - 1)), %eax
141 orl $MTRR_TYPE_WRPROT, %eax
144 movl $MTRRphysMask_MSR(1), %ecx
145 movl $CPU_PHYSMASK_HI, %edx
146 movl $(~(CONFIG_XIP_ROM_SIZE - 1) | MTRRphysMaskValid), %eax
150 #if CONFIG_CACHE_MRC_BIN
151 /* Enable caching for ram init code to run faster */
152 movl $MTRRphysBase_MSR(2), %ecx
153 movl $(CACHE_MRC_BASE | MTRR_TYPE_WRPROT), %eax
156 movl $MTRRphysMask_MSR(2), %ecx
157 movl $(CACHE_MRC_MASK | MTRRphysMaskValid), %eax
158 movl $CPU_PHYSMASK_HI, %edx
165 andl $(~((1 << 30) | (1 << 29))), %eax
168 /* Set up the stack pointer below MRC variable space. */
169 movl $(CACHE_AS_RAM_SIZE + CACHE_AS_RAM_BASE - \
170 CONFIG_DCACHE_RAM_MRC_VAR_SIZE - 4), %eax
173 /* Restore the BIST result. */
180 /* Call romstage.c main function. */
185 /* Copy global variable space (for USBDEBUG) to memory */
188 movl $(CONFIG_DCACHE_RAM_BASE + CONFIG_DCACHE_RAM_SIZE - 24), %esi
189 movl $(CONFIG_RAMTOP - 24), %edi
204 movl $MTRRdefType_MSR, %ecx
206 andl $(~MTRRdefTypeEn), %eax
211 /* Disable the no eviction run state */
212 movl $NoEvictMod_MSR, %ecx
219 /* Disable the no eviction mode */
224 #if CONFIG_CACHE_MRC_BIN
225 /* Clear MTRR that was used to cache MRC */
228 movl $MTRRphysBase_MSR(2), %ecx
230 movl $MTRRphysMask_MSR(2), %ecx
238 andl $~((1 << 30) | (1 << 29)), %eax
250 /* Enable Write Back and Speculative Reads for the first MB
253 movl $MTRRphysBase_MSR(0), %ecx
254 movl $(0x00000000 | MTRR_TYPE_WRBACK), %eax
257 movl $MTRRphysMask_MSR(0), %ecx
258 movl $(~(CONFIG_RAMTOP - 1) | MTRRphysMaskValid), %eax
259 movl $CPU_PHYSMASK_HI, %edx // 36bit address space
262 /* Enable Caching and speculative Reads for the
263 * complete ROM now that we actually have RAM.
265 movl $MTRRphysBase_MSR(1), %ecx
266 movl $(0xffc00000 | MTRR_TYPE_WRPROT), %eax
269 movl $MTRRphysMask_MSR(1), %ecx
270 movl $(~(4*1024*1024 - 1) | MTRRphysMaskValid), %eax
271 movl $CPU_PHYSMASK_HI, %edx
276 /* And enable cache again after setting MTRRs. */
278 andl $~((1 << 30) | (1 << 29)), %eax
284 movl $MTRRdefType_MSR, %ecx
286 orl $MTRRdefTypeEn, %eax
291 /* Invalidate the cache again. */
296 #if CONFIG_HAVE_ACPI_RESUME
297 movl CBMEM_BOOT_MODE, %eax
298 cmpl $0x2, %eax // Resume?
299 jne __acpi_resume_backup_done
301 /* copy 1MB - 64K to high tables ram_base to prevent memory corruption
302 * through stage 2. We could keep stuff like stack and heap in high
303 * tables memory completely, but that's a wonderful clean up task for
307 movl $CONFIG_RAMBASE, %esi
308 movl CBMEM_RESUME_BACKUP, %edi
309 movl $HIGH_MEMORY_SAVE / 4, %ecx
312 __acpi_resume_backup_done:
317 /* Clear boot_complete flag. */
320 post_code(POST_PREPARE_RAMSTAGE)
321 cld /* Clear direction flag. */
325 movl $ROMSTAGE_STACK, %esp
331 post_code(POST_DEAD_CODE)
337 .word 0x250, 0x258, 0x259
338 .word 0x268, 0x269, 0x26A
339 .word 0x26B, 0x26C, 0x26D
342 .word 0x200, 0x201, 0x202, 0x203
343 .word 0x204, 0x205, 0x206, 0x207
344 .word 0x208, 0x209, 0x20A, 0x20B
345 .word 0x20C, 0x20D, 0x20E, 0x20F
346 .word 0x210, 0x211, 0x212, 0x213