blob: 65da51631b9ba0f153c6c399eedf63bafaa7765b [file] [log] [blame]
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +02001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2000,2007 Ronald G. Minnich <rminnich@gmail.com>
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +02005 * Copyright (C) 2005 Tyan (written by Yinghai Lu for Tyan)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +02006 * Copyright (C) 2007-2008 coresystems GmbH
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +02007 * Copyright (C) 2012 Kyösti Mälkki <kyosti.malkki@gmail.com>
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +02008 *
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.
12 *
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.
17 *
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
21 */
22
23#include <cpu/x86/stack.h>
24#include <cpu/x86/mtrr.h>
Patrick Georgi05e740f2012-03-31 12:52:21 +020025#include <cpu/x86/cache.h>
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020026#include <cpu/x86/post_code.h>
Kyösti Mälkkif9d1a422012-02-28 01:45:44 +020027#include <cpu/x86/lapic_def.h>
28
29/* Macro to access Local APIC registers at default base. */
30#define LAPIC(x) $(LAPIC_DEFAULT_BASE | LAPIC_ ## x)
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +020031#define START_IPI_VECTOR ((CONFIG_AP_SIPI_VECTOR >> 12) & 0xff)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020032
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020033#define CACHE_AS_RAM_SIZE CONFIG_DCACHE_RAM_SIZE
34#define CACHE_AS_RAM_BASE CONFIG_DCACHE_RAM_BASE
35
36 /* Save the BIST result. */
37 movl %eax, %ebp
38
39cache_as_ram:
40 post_code(0x20)
41
Kyösti Mälkkia860c682012-02-28 02:06:45 +020042 movl $LAPIC_BASE_MSR, %ecx
43 rdmsr
44 andl $LAPIC_BASE_MSR_BOOTSTRAP_PROCESSOR, %eax
45 jz ap_init
46
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +020047 /* Zero out all fixed range and variable range MTRRs.
Kyösti Mälkkia860c682012-02-28 02:06:45 +020048 * For hyper-threaded CPUs these are shared.
49 */
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020050 movl $mtrr_table, %esi
51 movl $((mtrr_table_end - mtrr_table) / 2), %edi
52 xorl %eax, %eax
53 xorl %edx, %edx
54clear_mtrrs:
55 movw (%esi), %bx
56 movzx %bx, %ecx
57 wrmsr
58 add $2, %esi
59 dec %edi
60 jnz clear_mtrrs
61
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +020062 post_code(0x21)
63
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020064 /* Configure the default memory type to uncacheable. */
65 movl $MTRRdefType_MSR, %ecx
66 rdmsr
67 andl $(~0x00000cff), %eax
68 wrmsr
69
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +020070 post_code(0x22)
71
Kyösti Mälkkia860c682012-02-28 02:06:45 +020072 /* Determine CPU_ADDR_BITS and load PHYSMASK high
73 * word to %edx.
74 */
75 movl $0x80000000, %eax
76 cpuid
77 cmpl $0x80000008, %eax
78 jc addrsize_no_MSR
79 movl $0x80000008, %eax
80 cpuid
81 movb %al, %cl
82 sub $32, %cl
83 movl $1, %edx
84 shl %cl, %edx
85 subl $1, %edx
86 jmp addrsize_set_high
87addrsize_no_MSR:
88 movl $1, %eax
89 cpuid
90 andl $(1<<6 | 1<<17), %edx /* PAE or PSE36 */
91 jz addrsize_set_high
92 movl $0x0f, %edx
93
94 /* Preload high word of address mask (in %edx) for Variable
95 * MTRRs 0 and 1 and enable local apic at default base.
96 */
97addrsize_set_high:
98 xorl %eax, %eax
99 movl $MTRRphysMask_MSR(0), %ecx
100 wrmsr
101 movl $MTRRphysMask_MSR(1), %ecx
102 wrmsr
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +0200103 movl $LAPIC_BASE_MSR, %ecx
Kyösti Mälkkia860c682012-02-28 02:06:45 +0200104 not %edx
105 movl %edx, %ebx
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +0200106 rdmsr
Kyösti Mälkkia860c682012-02-28 02:06:45 +0200107 andl %ebx, %edx
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +0200108 andl $(~LAPIC_BASE_MSR_ADDR_MASK), %eax
109 orl $(LAPIC_DEFAULT_BASE | LAPIC_BASE_MSR_ENABLE), %eax
110 wrmsr
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +0200111
112bsp_init:
113
114 post_code(0x23)
115
116 /* Send INIT IPI to all excluding ourself. */
117 movl LAPIC(ICR), %edi
118 movl $(LAPIC_DEST_ALLBUT | LAPIC_INT_ASSERT | LAPIC_DM_INIT), %eax
1191: movl %eax, (%edi)
120 movl $0x30, %ecx
1212: pause
122 dec %ecx
123 jnz 2b
124 movl (%edi), %ecx
125 andl $LAPIC_ICR_BUSY, %ecx
126 jnz 1b
127
128 post_code(0x24)
Patrick Georgi819c7d42012-03-31 13:08:12 +0200129
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +0200130 /* For a hyper-threading processor, cache must not be disabled
131 * on an AP on the same physical package with the BSP.
132 */
133 movl $01, %eax
134 cpuid
135 btl $28, %edx
136 jnc sipi_complete
137 bswapl %ebx
138 cmpb $01, %bh
139 jbe sipi_complete
140
141hyper_threading_cpu:
142
143 /* delay 10 ms */
144 movl $10000, %ecx
1451: inb $0x80, %al
146 dec %ecx
147 jnz 1b
148
149 post_code(0x25)
150
151 /* Send Start IPI to all excluding ourself. */
152 movl LAPIC(ICR), %edi
153 movl $(LAPIC_DEST_ALLBUT | LAPIC_DM_STARTUP | START_IPI_VECTOR), %eax
1541: movl %eax, (%edi)
155 movl $0x30, %ecx
1562: pause
157 dec %ecx
158 jnz 2b
159 movl (%edi), %ecx
160 andl $LAPIC_ICR_BUSY, %ecx
161 jnz 1b
162
163 /* delay 250 us */
164 movl $250, %ecx
1651: inb $0x80, %al
166 dec %ecx
167 jnz 1b
168
169 post_code(0x26)
170
171 /* Wait for sibling CPU to start. */
1721: movl $(MTRRphysBase_MSR(0)), %ecx
173 rdmsr
174 andl %eax, %eax
175 jnz sipi_complete
176
177 movl $0x30, %ecx
1782: pause
179 dec %ecx
180 jnz 2b
181 jmp 1b
182
183
184ap_init:
185 post_code(0x27)
186
187 /* Do not disable cache (so BSP can enable it). */
188 movl %cr0, %eax
Patrick Georgi05e740f2012-03-31 12:52:21 +0200189 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +0200190 movl %eax, %cr0
191
192 post_code(0x28)
193
194 /* MTRR registers are shared between HT siblings. */
195 movl $(MTRRphysBase_MSR(0)), %ecx
196 movl $(1<<12), %eax
197 xorl %edx, %edx
198 wrmsr
199
200 post_code(0x29)
201
202ap_halt:
203 cli
2041: hlt
205 jnz 1b
206
207
208
209sipi_complete:
210
211 post_code(0x2a)
212
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200213 /* Set Cache-as-RAM base address. */
214 movl $(MTRRphysBase_MSR(0)), %ecx
215 movl $(CACHE_AS_RAM_BASE | MTRR_TYPE_WRBACK), %eax
216 xorl %edx, %edx
217 wrmsr
218
219 /* Set Cache-as-RAM mask. */
220 movl $(MTRRphysMask_MSR(0)), %ecx
Kyösti Mälkkia860c682012-02-28 02:06:45 +0200221 rdmsr
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200222 movl $(~(CACHE_AS_RAM_SIZE - 1) | MTRRphysMaskValid), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200223 wrmsr
224
225 /* Enable MTRR. */
226 movl $MTRRdefType_MSR, %ecx
227 rdmsr
228 orl $MTRRdefTypeEn, %eax
229 wrmsr
230
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +0200231 post_code(0x2b)
232
Kyösti Mälkki05d6ffb2012-02-16 23:12:04 +0200233 /* Enable L2 cache Write-Back (WBINVD and FLUSH#).
234 *
235 * MSR is set when DisplayFamily_DisplayModel is one of:
236 * 06_0x, 06_17, 06_1C
237 *
238 * Description says this bit enables use of WBINVD and FLUSH#.
239 * Should this be set only after the system bus and/or memory
240 * controller can successfully handle write cycles?
241 */
242
243#define EAX_FAMILY(a) (a << 8) /* for family <= 0fH */
244#define EAX_MODEL(a) (((a & 0xf0) << 12) | ((a & 0xf) << 4))
245
246 movl $1, %eax
247 cpuid
248 movl %eax, %ebx
249 andl $EAX_FAMILY(0x0f), %eax
250 cmpl $EAX_FAMILY(0x06), %eax
251 jne no_msr_11e
252 movl %ebx, %eax
253 andl $EAX_MODEL(0xff), %eax
254 cmpl $EAX_MODEL(0x17), %eax
255 je has_msr_11e
256 cmpl $EAX_MODEL(0x1c), %eax
257 je has_msr_11e
258 andl $EAX_MODEL(0xf0), %eax
259 cmpl $EAX_MODEL(0x00), %eax
260 jne no_msr_11e
261has_msr_11e:
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200262 movl $0x11e, %ecx
263 rdmsr
264 orl $(1 << 8), %eax
265 wrmsr
Kyösti Mälkki05d6ffb2012-02-16 23:12:04 +0200266no_msr_11e:
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200267
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +0200268 post_code(0x2c)
269
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200270 /* Enable cache (CR0.CD = 0, CR0.NW = 0). */
Kyösti Mälkkif9d1a422012-02-28 01:45:44 +0200271 movl %cr0, %eax
Patrick Georgi05e740f2012-03-31 12:52:21 +0200272 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200273 invd
274 movl %eax, %cr0
275
276 /* Clear the cache memory reagion. */
Kyösti Mälkkif9d1a422012-02-28 01:45:44 +0200277 cld
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200278 xorl %eax, %eax
Kyösti Mälkkif9d1a422012-02-28 01:45:44 +0200279 movl $CACHE_AS_RAM_BASE, %edi
280 movl $(CACHE_AS_RAM_SIZE / 4), %ecx
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200281 rep stosl
282
283 /* Enable Cache-as-RAM mode by disabling cache. */
284 movl %cr0, %eax
Patrick Georgi05e740f2012-03-31 12:52:21 +0200285 orl $CR0_CacheDisable, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200286 movl %eax, %cr0
287
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +0200288 post_code(0x2d)
289
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200290#if CONFIG_XIP_ROM_SIZE
291 /* Enable cache for our code in Flash because we do XIP here */
292 movl $MTRRphysBase_MSR(1), %ecx
293 xorl %edx, %edx
294 /*
295 * IMPORTANT: The following calculation _must_ be done at runtime. See
296 * http://www.coreboot.org/pipermail/coreboot/2010-October/060855.html
297 */
298 movl $copy_and_run, %eax
299 andl $(~(CONFIG_XIP_ROM_SIZE - 1)), %eax
300 orl $MTRR_TYPE_WRBACK, %eax
301 wrmsr
302
303 movl $MTRRphysMask_MSR(1), %ecx
Kyösti Mälkkia860c682012-02-28 02:06:45 +0200304 rdmsr
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200305 movl $(~(CONFIG_XIP_ROM_SIZE - 1) | MTRRphysMaskValid), %eax
306 wrmsr
307#endif /* CONFIG_XIP_ROM_SIZE */
308
309 /* Enable cache. */
310 movl %cr0, %eax
Patrick Georgi05e740f2012-03-31 12:52:21 +0200311 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200312 movl %eax, %cr0
313
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +0200314 post_code(0x2e)
315
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200316 /* Set up the stack pointer. */
317#if CONFIG_USBDEBUG
318 /* Leave some space for the struct ehci_debug_info. */
Kyösti Mälkkif9d1a422012-02-28 01:45:44 +0200319 movl $(CACHE_AS_RAM_BASE + CACHE_AS_RAM_SIZE - 4 - 128), %esp
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200320#else
Kyösti Mälkkif9d1a422012-02-28 01:45:44 +0200321 movl $(CACHE_AS_RAM_BASE + CACHE_AS_RAM_SIZE - 4), %esp
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200322#endif
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200323
324 /* Restore the BIST result. */
325 movl %ebp, %eax
326 movl %esp, %ebp
327 pushl %eax
328
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +0200329 post_code(0x2f)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200330
331 /* Call romstage.c main function. */
332 call main
Kyösti Mälkkif9d1a422012-02-28 01:45:44 +0200333 addl $4, %esp
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200334
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200335 post_code(0x30)
336
337 /* Disable cache. */
338 movl %cr0, %eax
Patrick Georgi05e740f2012-03-31 12:52:21 +0200339 orl $CR0_CacheDisable, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200340 movl %eax, %cr0
341
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +0200342 post_code(0x34)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200343
344 /* Disable MTRR. */
345 movl $MTRRdefType_MSR, %ecx
346 rdmsr
347 andl $(~MTRRdefTypeEn), %eax
348 wrmsr
349
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +0200350 post_code(0x35)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200351
352 invd
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200353
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +0200354 post_code(0x36)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200355
356 /* Enable cache. */
357 movl %cr0, %eax
Patrick Georgi05e740f2012-03-31 12:52:21 +0200358 andl $~(CR0_CacheDisable | CR0_NoWriteThrough), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200359 movl %eax, %cr0
360
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +0200361 post_code(0x37)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200362
363 /* Disable cache. */
364 movl %cr0, %eax
Patrick Georgi05e740f2012-03-31 12:52:21 +0200365 orl $CR0_CacheDisable, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200366 movl %eax, %cr0
367
368 post_code(0x38)
369
Kyösti Mälkkif9d1a422012-02-28 01:45:44 +0200370 /* Enable Write Back and Speculative Reads for low RAM. */
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200371 movl $MTRRphysBase_MSR(0), %ecx
372 movl $(0x00000000 | MTRR_TYPE_WRBACK), %eax
373 xorl %edx, %edx
374 wrmsr
375 movl $MTRRphysMask_MSR(0), %ecx
Kyösti Mälkkia860c682012-02-28 02:06:45 +0200376 rdmsr
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200377 movl $(~(CONFIG_RAMTOP - 1) | MTRRphysMaskValid), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200378 wrmsr
379
Kyösti Mälkki5458b9d2012-06-30 11:41:08 +0300380#if CONFIG_CACHE_ROM_SIZE
Kyösti Mälkki325b92f2012-02-28 00:24:15 +0200381 /* Enable caching and Speculative Reads for Flash ROM device. */
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200382 movl $MTRRphysBase_MSR(1), %ecx
Kyösti Mälkki325b92f2012-02-28 00:24:15 +0200383 movl $(CACHE_ROM_BASE | MTRR_TYPE_WRPROT), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200384 xorl %edx, %edx
385 wrmsr
386 movl $MTRRphysMask_MSR(1), %ecx
Kyösti Mälkkia860c682012-02-28 02:06:45 +0200387 rdmsr
Kyösti Mälkki325b92f2012-02-28 00:24:15 +0200388 movl $(~(CONFIG_CACHE_ROM_SIZE - 1) | MTRRphysMaskValid), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200389 wrmsr
Kyösti Mälkki5458b9d2012-06-30 11:41:08 +0300390#endif
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200391
392 post_code(0x39)
393
394 /* And enable cache again after setting MTRRs. */
395 movl %cr0, %eax
Patrick Georgi05e740f2012-03-31 12:52:21 +0200396 andl $~(CR0_CacheDisable | CR0_NoWriteThrough), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200397 movl %eax, %cr0
398
399 post_code(0x3a)
400
401 /* Enable MTRR. */
402 movl $MTRRdefType_MSR, %ecx
403 rdmsr
404 orl $MTRRdefTypeEn, %eax
405 wrmsr
406
407 post_code(0x3b)
408
409 /* Invalidate the cache again. */
410 invd
411
412 post_code(0x3c)
413
414 /* Clear boot_complete flag. */
415 xorl %ebp, %ebp
416__main:
417 post_code(POST_PREPARE_RAMSTAGE)
418 cld /* Clear direction flag. */
419
420 movl %ebp, %esi
421
422 movl $ROMSTAGE_STACK, %esp
423 movl %esp, %ebp
424 pushl %esi
425 call copy_and_run
426
427.Lhlt:
428 post_code(POST_DEAD_CODE)
429 hlt
430 jmp .Lhlt
431
432mtrr_table:
433 /* Fixed MTRRs */
434 .word 0x250, 0x258, 0x259
435 .word 0x268, 0x269, 0x26A
436 .word 0x26B, 0x26C, 0x26D
437 .word 0x26E, 0x26F
438 /* Variable MTRRs */
439 .word 0x200, 0x201, 0x202, 0x203
440 .word 0x204, 0x205, 0x206, 0x207
441 .word 0x208, 0x209, 0x20A, 0x20B
442 .word 0x20C, 0x20D, 0x20E, 0x20F
443mtrr_table_end:
444