2 * This file is part of the coreboot project.
4 * Copyright (C) 2000,2007 Ronald G. Minnich <rminnich@gmail.com>
5 * Copyright (C) 2005 Tyan (written by Yinghai Lu for Tyan)
6 * Copyright (C) 2007-2008 coresystems GmbH
7 * Copyright (C) 2012 Kyösti Mälkki <kyosti.malkki@gmail.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2 of the License.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <cpu/x86/stack.h>
24 #include <cpu/x86/mtrr.h>
25 #include <cpu/x86/post_code.h>
26 #include <cpu/x86/lapic_def.h>
28 /* Macro to access Local APIC registers at default base. */
29 #define LAPIC(x) $(LAPIC_DEFAULT_BASE | LAPIC_ ## x)
30 #define START_IPI_VECTOR ((CONFIG_AP_SIPI_VECTOR >> 12) & 0xff)
32 #define CPU_MAXPHYADDR 36
33 #define CPU_PHYSMASK_HI (1 << (CPU_MAXPHYADDR - 32) - 1)
35 /* Base address to cache all of Flash ROM, just below 4GB. */
36 #define CACHE_ROM_BASE ((1<<22 - CONFIG_CACHE_ROM_SIZE>>10)<<10)
38 #define CACHE_AS_RAM_SIZE CONFIG_DCACHE_RAM_SIZE
39 #define CACHE_AS_RAM_BASE CONFIG_DCACHE_RAM_BASE
41 /* Save the BIST result. */
47 /* Zero out all fixed range and variable range MTRRs.
48 * For hyper-threaded CPU MTRRs are shared so we actually
49 * clear them more than once, but we don't care. */
50 movl $mtrr_table, %esi
51 movl $((mtrr_table_end - mtrr_table) / 2), %edi
64 /* Configure the default memory type to uncacheable. */
65 movl $MTRRdefType_MSR, %ecx
67 andl $(~0x00000cff), %eax
72 /* Enable local apic. */
73 movl $LAPIC_BASE_MSR, %ecx
75 andl $(~CPU_PHYSMASK_HI), %edx
76 andl $(~LAPIC_BASE_MSR_ADDR_MASK), %eax
77 orl $(LAPIC_DEFAULT_BASE | LAPIC_BASE_MSR_ENABLE), %eax
79 andl $LAPIC_BASE_MSR_BOOTSTRAP_PROCESSOR, %eax
86 /* Send INIT IPI to all excluding ourself. */
88 movl $(LAPIC_DEST_ALLBUT | LAPIC_INT_ASSERT | LAPIC_DM_INIT), %eax
95 andl $LAPIC_ICR_BUSY, %ecx
100 /* For a hyper-threading processor, cache must not be disabled
101 * on an AP on the same physical package with the BSP.
121 /* Send Start IPI to all excluding ourself. */
122 movl LAPIC(ICR), %edi
123 movl $(LAPIC_DEST_ALLBUT | LAPIC_DM_STARTUP | START_IPI_VECTOR), %eax
130 andl $LAPIC_ICR_BUSY, %ecx
141 /* Wait for sibling CPU to start. */
142 1: movl $(MTRRphysBase_MSR(0)), %ecx
157 /* Do not disable cache (so BSP can enable it). */
159 andl $(~((1 << 30) | (1 << 29))), %eax
164 /* MTRR registers are shared between HT siblings. */
165 movl $(MTRRphysBase_MSR(0)), %ecx
183 /* Set Cache-as-RAM base address. */
184 movl $(MTRRphysBase_MSR(0)), %ecx
185 movl $(CACHE_AS_RAM_BASE | MTRR_TYPE_WRBACK), %eax
189 /* Set Cache-as-RAM mask. */
190 movl $(MTRRphysMask_MSR(0)), %ecx
191 movl $(~(CACHE_AS_RAM_SIZE - 1) | MTRRphysMaskValid), %eax
192 movl $CPU_PHYSMASK_HI, %edx
196 movl $MTRRdefType_MSR, %ecx
198 orl $MTRRdefTypeEn, %eax
203 /* Enable L2 cache Write-Back (WBINVD and FLUSH#).
205 * MSR is set when DisplayFamily_DisplayModel is one of:
206 * 06_0x, 06_17, 06_1C
208 * Description says this bit enables use of WBINVD and FLUSH#.
209 * Should this be set only after the system bus and/or memory
210 * controller can successfully handle write cycles?
213 #define EAX_FAMILY(a) (a << 8) /* for family <= 0fH */
214 #define EAX_MODEL(a) (((a & 0xf0) << 12) | ((a & 0xf) << 4))
219 andl $EAX_FAMILY(0x0f), %eax
220 cmpl $EAX_FAMILY(0x06), %eax
223 andl $EAX_MODEL(0xff), %eax
224 cmpl $EAX_MODEL(0x17), %eax
226 cmpl $EAX_MODEL(0x1c), %eax
228 andl $EAX_MODEL(0xf0), %eax
229 cmpl $EAX_MODEL(0x00), %eax
240 /* Enable cache (CR0.CD = 0, CR0.NW = 0). */
242 andl $(~((1 << 30) | (1 << 29))), %eax
246 /* Clear the cache memory reagion. */
249 movl $CACHE_AS_RAM_BASE, %edi
250 movl $(CACHE_AS_RAM_SIZE / 4), %ecx
253 /* Enable Cache-as-RAM mode by disabling cache. */
260 #if CONFIG_XIP_ROM_SIZE
261 /* Enable cache for our code in Flash because we do XIP here */
262 movl $MTRRphysBase_MSR(1), %ecx
265 * IMPORTANT: The following calculation _must_ be done at runtime. See
266 * http://www.coreboot.org/pipermail/coreboot/2010-October/060855.html
268 movl $copy_and_run, %eax
269 andl $(~(CONFIG_XIP_ROM_SIZE - 1)), %eax
270 orl $MTRR_TYPE_WRBACK, %eax
273 movl $MTRRphysMask_MSR(1), %ecx
274 movl $CPU_PHYSMASK_HI, %edx
275 movl $(~(CONFIG_XIP_ROM_SIZE - 1) | MTRRphysMaskValid), %eax
277 #endif /* CONFIG_XIP_ROM_SIZE */
281 andl $(~((1 << 30) | (1 << 29))), %eax
286 /* Set up the stack pointer. */
288 /* Leave some space for the struct ehci_debug_info. */
289 movl $(CACHE_AS_RAM_BASE + CACHE_AS_RAM_SIZE - 4 - 128), %esp
291 movl $(CACHE_AS_RAM_BASE + CACHE_AS_RAM_SIZE - 4), %esp
294 /* Restore the BIST result. */
301 /* Call romstage.c main function. */
315 movl $MTRRdefType_MSR, %ecx
317 andl $(~MTRRdefTypeEn), %eax
328 andl $~((1 << 30) | (1 << 29)), %eax
340 /* Enable Write Back and Speculative Reads for low RAM. */
341 movl $MTRRphysBase_MSR(0), %ecx
342 movl $(0x00000000 | MTRR_TYPE_WRBACK), %eax
345 movl $MTRRphysMask_MSR(0), %ecx
346 movl $(~(CONFIG_RAMTOP - 1) | MTRRphysMaskValid), %eax
347 movl $CPU_PHYSMASK_HI, %edx
350 /* Enable caching and Speculative Reads for Flash ROM device. */
351 movl $MTRRphysBase_MSR(1), %ecx
352 movl $(CACHE_ROM_BASE | MTRR_TYPE_WRPROT), %eax
355 movl $MTRRphysMask_MSR(1), %ecx
356 movl $(~(CONFIG_CACHE_ROM_SIZE - 1) | MTRRphysMaskValid), %eax
357 movl $CPU_PHYSMASK_HI, %edx
362 /* And enable cache again after setting MTRRs. */
364 andl $~((1 << 30) | (1 << 29)), %eax
370 movl $MTRRdefType_MSR, %ecx
372 orl $MTRRdefTypeEn, %eax
377 /* Invalidate the cache again. */
382 /* Clear boot_complete flag. */
385 post_code(POST_PREPARE_RAMSTAGE)
386 cld /* Clear direction flag. */
390 movl $ROMSTAGE_STACK, %esp
396 post_code(POST_DEAD_CODE)
402 .word 0x250, 0x258, 0x259
403 .word 0x268, 0x269, 0x26A
404 .word 0x26B, 0x26C, 0x26D
407 .word 0x200, 0x201, 0x202, 0x203
408 .word 0x204, 0x205, 0x206, 0x207
409 .word 0x208, 0x209, 0x20A, 0x20B
410 .word 0x20C, 0x20D, 0x20E, 0x20F