blob: fb653168b2fd6e97a3c0aee5cba21698925101f2 [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
Paul Menzela46a7122013-02-23 18:37:27 +010020 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020021 */
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älkkidf0fbc72012-07-04 12:02:58 +0300130 movl $1, %eax
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +0200131 cpuid
132 btl $28, %edx
133 jnc sipi_complete
134 bswapl %ebx
Kyösti Mälkkidf0fbc72012-07-04 12:02:58 +0300135 movzx %bh, %edi
136 cmpb $1, %bh
137 jbe sipi_complete /* only one LAPIC ID in package */
138
139 movl $0, %eax
140 cpuid
141 movb $1, %bl
142 cmpl $4, %eax
143 jb cores_counted
144 movl $4, %eax
145 movl $0, %ecx
146 cpuid
147 shr $26, %eax
148 movb %al, %bl
149 inc %bl
150
151cores_counted:
152 movl %edi, %eax
153 divb %bl
154 cmpb $1, %al
155 jbe sipi_complete /* only LAPIC ID of a core */
156
157 /* For a hyper-threading processor, cache must not be disabled
158 * on an AP on the same physical package with the BSP.
159 */
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +0200160
161hyper_threading_cpu:
162
163 /* delay 10 ms */
164 movl $10000, %ecx
1651: inb $0x80, %al
166 dec %ecx
167 jnz 1b
168
169 post_code(0x25)
170
171 /* Send Start IPI to all excluding ourself. */
172 movl LAPIC(ICR), %edi
173 movl $(LAPIC_DEST_ALLBUT | LAPIC_DM_STARTUP | START_IPI_VECTOR), %eax
1741: movl %eax, (%edi)
175 movl $0x30, %ecx
1762: pause
177 dec %ecx
178 jnz 2b
179 movl (%edi), %ecx
180 andl $LAPIC_ICR_BUSY, %ecx
181 jnz 1b
182
183 /* delay 250 us */
184 movl $250, %ecx
1851: inb $0x80, %al
186 dec %ecx
187 jnz 1b
188
189 post_code(0x26)
190
191 /* Wait for sibling CPU to start. */
1921: movl $(MTRRphysBase_MSR(0)), %ecx
193 rdmsr
194 andl %eax, %eax
195 jnz sipi_complete
196
197 movl $0x30, %ecx
1982: pause
199 dec %ecx
200 jnz 2b
201 jmp 1b
202
203
204ap_init:
205 post_code(0x27)
206
207 /* Do not disable cache (so BSP can enable it). */
208 movl %cr0, %eax
Patrick Georgi05e740f2012-03-31 12:52:21 +0200209 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +0200210 movl %eax, %cr0
211
212 post_code(0x28)
213
214 /* MTRR registers are shared between HT siblings. */
215 movl $(MTRRphysBase_MSR(0)), %ecx
216 movl $(1<<12), %eax
217 xorl %edx, %edx
218 wrmsr
219
220 post_code(0x29)
221
222ap_halt:
223 cli
2241: hlt
Kyösti Mälkkidf0fbc72012-07-04 12:02:58 +0300225 jmp 1b
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +0200226
227
228
229sipi_complete:
230
231 post_code(0x2a)
232
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200233 /* Set Cache-as-RAM base address. */
234 movl $(MTRRphysBase_MSR(0)), %ecx
235 movl $(CACHE_AS_RAM_BASE | MTRR_TYPE_WRBACK), %eax
236 xorl %edx, %edx
237 wrmsr
238
239 /* Set Cache-as-RAM mask. */
240 movl $(MTRRphysMask_MSR(0)), %ecx
Kyösti Mälkkia860c682012-02-28 02:06:45 +0200241 rdmsr
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200242 movl $(~(CACHE_AS_RAM_SIZE - 1) | MTRRphysMaskValid), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200243 wrmsr
244
245 /* Enable MTRR. */
246 movl $MTRRdefType_MSR, %ecx
247 rdmsr
248 orl $MTRRdefTypeEn, %eax
249 wrmsr
250
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +0200251 post_code(0x2b)
252
Kyösti Mälkki05d6ffb2012-02-16 23:12:04 +0200253 /* Enable L2 cache Write-Back (WBINVD and FLUSH#).
254 *
255 * MSR is set when DisplayFamily_DisplayModel is one of:
256 * 06_0x, 06_17, 06_1C
257 *
258 * Description says this bit enables use of WBINVD and FLUSH#.
259 * Should this be set only after the system bus and/or memory
260 * controller can successfully handle write cycles?
261 */
262
263#define EAX_FAMILY(a) (a << 8) /* for family <= 0fH */
264#define EAX_MODEL(a) (((a & 0xf0) << 12) | ((a & 0xf) << 4))
265
266 movl $1, %eax
267 cpuid
268 movl %eax, %ebx
269 andl $EAX_FAMILY(0x0f), %eax
270 cmpl $EAX_FAMILY(0x06), %eax
271 jne no_msr_11e
272 movl %ebx, %eax
273 andl $EAX_MODEL(0xff), %eax
274 cmpl $EAX_MODEL(0x17), %eax
275 je has_msr_11e
276 cmpl $EAX_MODEL(0x1c), %eax
277 je has_msr_11e
278 andl $EAX_MODEL(0xf0), %eax
279 cmpl $EAX_MODEL(0x00), %eax
280 jne no_msr_11e
281has_msr_11e:
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200282 movl $0x11e, %ecx
283 rdmsr
284 orl $(1 << 8), %eax
285 wrmsr
Kyösti Mälkki05d6ffb2012-02-16 23:12:04 +0200286no_msr_11e:
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200287
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +0200288 post_code(0x2c)
289
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200290 /* Enable cache (CR0.CD = 0, CR0.NW = 0). */
Kyösti Mälkkif9d1a422012-02-28 01:45:44 +0200291 movl %cr0, %eax
Patrick Georgi05e740f2012-03-31 12:52:21 +0200292 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200293 invd
294 movl %eax, %cr0
295
296 /* Clear the cache memory reagion. */
Kyösti Mälkkif9d1a422012-02-28 01:45:44 +0200297 cld
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200298 xorl %eax, %eax
Kyösti Mälkkif9d1a422012-02-28 01:45:44 +0200299 movl $CACHE_AS_RAM_BASE, %edi
300 movl $(CACHE_AS_RAM_SIZE / 4), %ecx
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200301 rep stosl
302
303 /* Enable Cache-as-RAM mode by disabling cache. */
304 movl %cr0, %eax
Patrick Georgi05e740f2012-03-31 12:52:21 +0200305 orl $CR0_CacheDisable, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200306 movl %eax, %cr0
307
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +0200308 post_code(0x2d)
309
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200310#if CONFIG_XIP_ROM_SIZE
311 /* Enable cache for our code in Flash because we do XIP here */
312 movl $MTRRphysBase_MSR(1), %ecx
313 xorl %edx, %edx
314 /*
315 * IMPORTANT: The following calculation _must_ be done at runtime. See
316 * http://www.coreboot.org/pipermail/coreboot/2010-October/060855.html
317 */
318 movl $copy_and_run, %eax
319 andl $(~(CONFIG_XIP_ROM_SIZE - 1)), %eax
320 orl $MTRR_TYPE_WRBACK, %eax
321 wrmsr
322
323 movl $MTRRphysMask_MSR(1), %ecx
Kyösti Mälkkia860c682012-02-28 02:06:45 +0200324 rdmsr
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200325 movl $(~(CONFIG_XIP_ROM_SIZE - 1) | MTRRphysMaskValid), %eax
326 wrmsr
327#endif /* CONFIG_XIP_ROM_SIZE */
328
329 /* Enable cache. */
330 movl %cr0, %eax
Patrick Georgi05e740f2012-03-31 12:52:21 +0200331 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200332 movl %eax, %cr0
333
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +0200334 post_code(0x2e)
335
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200336 /* Set up the stack pointer. */
Kyösti Mälkkif9d1a422012-02-28 01:45:44 +0200337 movl $(CACHE_AS_RAM_BASE + CACHE_AS_RAM_SIZE - 4), %esp
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200338
339 /* Restore the BIST result. */
340 movl %ebp, %eax
341 movl %esp, %ebp
342 pushl %eax
343
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +0200344 post_code(0x2f)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200345
346 /* Call romstage.c main function. */
347 call main
Kyösti Mälkkif9d1a422012-02-28 01:45:44 +0200348 addl $4, %esp
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200349
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200350 post_code(0x30)
351
352 /* Disable cache. */
353 movl %cr0, %eax
Patrick Georgi05e740f2012-03-31 12:52:21 +0200354 orl $CR0_CacheDisable, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200355 movl %eax, %cr0
356
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +0200357 post_code(0x34)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200358
359 /* Disable MTRR. */
360 movl $MTRRdefType_MSR, %ecx
361 rdmsr
362 andl $(~MTRRdefTypeEn), %eax
363 wrmsr
364
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +0200365 post_code(0x35)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200366
367 invd
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200368
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +0200369 post_code(0x36)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200370
371 /* Enable cache. */
372 movl %cr0, %eax
Patrick Georgi05e740f2012-03-31 12:52:21 +0200373 andl $~(CR0_CacheDisable | CR0_NoWriteThrough), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200374 movl %eax, %cr0
375
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +0200376 post_code(0x37)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200377
378 /* Disable cache. */
379 movl %cr0, %eax
Patrick Georgi05e740f2012-03-31 12:52:21 +0200380 orl $CR0_CacheDisable, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200381 movl %eax, %cr0
382
383 post_code(0x38)
384
Kyösti Mälkkif9d1a422012-02-28 01:45:44 +0200385 /* Enable Write Back and Speculative Reads for low RAM. */
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200386 movl $MTRRphysBase_MSR(0), %ecx
387 movl $(0x00000000 | MTRR_TYPE_WRBACK), %eax
388 xorl %edx, %edx
389 wrmsr
390 movl $MTRRphysMask_MSR(0), %ecx
Kyösti Mälkkia860c682012-02-28 02:06:45 +0200391 rdmsr
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200392 movl $(~(CONFIG_RAMTOP - 1) | MTRRphysMaskValid), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200393 wrmsr
394
Kyösti Mälkki107f72e2014-01-06 11:06:26 +0200395#if CACHE_ROM_SIZE
Kyösti Mälkki325b92f2012-02-28 00:24:15 +0200396 /* Enable caching and Speculative Reads for Flash ROM device. */
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200397 movl $MTRRphysBase_MSR(1), %ecx
Kyösti Mälkki325b92f2012-02-28 00:24:15 +0200398 movl $(CACHE_ROM_BASE | MTRR_TYPE_WRPROT), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200399 xorl %edx, %edx
400 wrmsr
401 movl $MTRRphysMask_MSR(1), %ecx
Kyösti Mälkkia860c682012-02-28 02:06:45 +0200402 rdmsr
Kyösti Mälkki107f72e2014-01-06 11:06:26 +0200403 movl $(~(CACHE_ROM_SIZE - 1) | MTRRphysMaskValid), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200404 wrmsr
Kyösti Mälkki5458b9d2012-06-30 11:41:08 +0300405#endif
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200406
407 post_code(0x39)
408
409 /* And enable cache again after setting MTRRs. */
410 movl %cr0, %eax
Patrick Georgi05e740f2012-03-31 12:52:21 +0200411 andl $~(CR0_CacheDisable | CR0_NoWriteThrough), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200412 movl %eax, %cr0
413
414 post_code(0x3a)
415
416 /* Enable MTRR. */
417 movl $MTRRdefType_MSR, %ecx
418 rdmsr
419 orl $MTRRdefTypeEn, %eax
420 wrmsr
421
422 post_code(0x3b)
423
424 /* Invalidate the cache again. */
425 invd
426
427 post_code(0x3c)
428
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200429__main:
430 post_code(POST_PREPARE_RAMSTAGE)
431 cld /* Clear direction flag. */
432
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200433 movl $ROMSTAGE_STACK, %esp
434 movl %esp, %ebp
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200435 call copy_and_run
436
437.Lhlt:
438 post_code(POST_DEAD_CODE)
439 hlt
440 jmp .Lhlt
441
442mtrr_table:
443 /* Fixed MTRRs */
444 .word 0x250, 0x258, 0x259
445 .word 0x268, 0x269, 0x26A
446 .word 0x26B, 0x26C, 0x26D
447 .word 0x26E, 0x26F
448 /* Variable MTRRs */
449 .word 0x200, 0x201, 0x202, 0x203
450 .word 0x204, 0x205, 0x206, 0x207
451 .word 0x208, 0x209, 0x20A, 0x20B
452 .word 0x20C, 0x20D, 0x20E, 0x20F
453mtrr_table_end:
454