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>
24 #include <cpu/x86/lapic_def.h>
26 /* Macro to access Local APIC registers at default base. */
27 #define LAPIC(x) $(LAPIC_DEFAULT_BASE | LAPIC_ ## x)
29 #define CPU_MAXPHYADDR 36
30 #define CPU_PHYSMASK_HI (1 << (CPU_MAXPHYADDR - 32) - 1)
32 /* Base address to cache all of Flash ROM, just below 4GB. */
33 #define CACHE_ROM_BASE ((1<<22 - CONFIG_CACHE_ROM_SIZE>>10)<<10)
35 #define CACHE_AS_RAM_SIZE CONFIG_DCACHE_RAM_SIZE
36 #define CACHE_AS_RAM_BASE CONFIG_DCACHE_RAM_BASE
38 /* Save the BIST result. */
44 /* Send INIT IPI to all excluding ourself. */
46 movl $(LAPIC_DEST_ALLBUT | LAPIC_INT_ASSERT | LAPIC_DM_INIT), %eax
49 /* Zero out all fixed range and variable range MTRRs. */
50 movl $mtrr_table, %esi
51 movl $((mtrr_table_end - mtrr_table) / 2), %edi
62 /* Configure the default memory type to uncacheable. */
63 movl $MTRRdefType_MSR, %ecx
65 andl $(~0x00000cff), %eax
68 /* Set Cache-as-RAM base address. */
69 movl $(MTRRphysBase_MSR(0)), %ecx
70 movl $(CACHE_AS_RAM_BASE | MTRR_TYPE_WRBACK), %eax
74 /* Set Cache-as-RAM mask. */
75 movl $(MTRRphysMask_MSR(0)), %ecx
76 movl $(~(CACHE_AS_RAM_SIZE - 1) | MTRRphysMaskValid), %eax
77 movl $CPU_PHYSMASK_HI, %edx
81 movl $MTRRdefType_MSR, %ecx
83 orl $MTRRdefTypeEn, %eax
86 /* Enable L2 cache. */
92 /* Enable cache (CR0.CD = 0, CR0.NW = 0). */
94 andl $(~((1 << 30) | (1 << 29))), %eax
98 /* Clear the cache memory reagion. */
101 movl $CACHE_AS_RAM_BASE, %edi
102 movl $(CACHE_AS_RAM_SIZE / 4), %ecx
105 /* Enable Cache-as-RAM mode by disabling cache. */
110 #if CONFIG_XIP_ROM_SIZE
111 /* Enable cache for our code in Flash because we do XIP here */
112 movl $MTRRphysBase_MSR(1), %ecx
115 * IMPORTANT: The following calculation _must_ be done at runtime. See
116 * http://www.coreboot.org/pipermail/coreboot/2010-October/060855.html
118 movl $copy_and_run, %eax
119 andl $(~(CONFIG_XIP_ROM_SIZE - 1)), %eax
120 orl $MTRR_TYPE_WRBACK, %eax
123 movl $MTRRphysMask_MSR(1), %ecx
124 movl $CPU_PHYSMASK_HI, %edx
125 movl $(~(CONFIG_XIP_ROM_SIZE - 1) | MTRRphysMaskValid), %eax
127 #endif /* CONFIG_XIP_ROM_SIZE */
131 andl $(~((1 << 30) | (1 << 29))), %eax
134 /* Set up the stack pointer. */
136 /* Leave some space for the struct ehci_debug_info. */
137 movl $(CACHE_AS_RAM_BASE + CACHE_AS_RAM_SIZE - 4 - 128), %esp
139 movl $(CACHE_AS_RAM_BASE + CACHE_AS_RAM_SIZE - 4), %esp
142 /* Restore the BIST result. */
149 /* Call romstage.c main function. */
165 movl $MTRRdefType_MSR, %ecx
167 andl $(~MTRRdefTypeEn), %eax
178 andl $~((1 << 30) | (1 << 29)), %eax
190 /* Enable Write Back and Speculative Reads for low RAM. */
191 movl $MTRRphysBase_MSR(0), %ecx
192 movl $(0x00000000 | MTRR_TYPE_WRBACK), %eax
195 movl $MTRRphysMask_MSR(0), %ecx
196 movl $(~(CONFIG_RAMTOP - 1) | MTRRphysMaskValid), %eax
197 movl $CPU_PHYSMASK_HI, %edx
200 /* Enable caching and Speculative Reads for Flash ROM device. */
201 movl $MTRRphysBase_MSR(1), %ecx
202 movl $(CACHE_ROM_BASE | MTRR_TYPE_WRPROT), %eax
205 movl $MTRRphysMask_MSR(1), %ecx
206 movl $(~(CONFIG_CACHE_ROM_SIZE - 1) | MTRRphysMaskValid), %eax
207 movl $CPU_PHYSMASK_HI, %edx
212 /* And enable cache again after setting MTRRs. */
214 andl $~((1 << 30) | (1 << 29)), %eax
220 movl $MTRRdefType_MSR, %ecx
222 orl $MTRRdefTypeEn, %eax
227 /* Invalidate the cache again. */
232 /* Clear boot_complete flag. */
235 post_code(POST_PREPARE_RAMSTAGE)
236 cld /* Clear direction flag. */
240 movl $ROMSTAGE_STACK, %esp
246 post_code(POST_DEAD_CODE)
252 .word 0x250, 0x258, 0x259
253 .word 0x268, 0x269, 0x26A
254 .word 0x26B, 0x26C, 0x26D
257 .word 0x200, 0x201, 0x202, 0x203
258 .word 0x204, 0x205, 0x206, 0x207
259 .word 0x208, 0x209, 0x20A, 0x20B
260 .word 0x20C, 0x20D, 0x20E, 0x20F