tree: drop last paragraph of GPL copyright header
[coreboot.git] / src / cpu / intel / car / cache_as_ram_ht.inc
blobee4a1bb3320b19a81b983d3365d666c898139f24
1 /*
2  * This file is part of the coreboot project.
3  *
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>
8  *
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  */
19 #include <cpu/x86/mtrr.h>
20 #include <cpu/x86/cache.h>
21 #include <cpu/x86/post_code.h>
22 #include <cpu/x86/lapic_def.h>
24 /* Macro to access Local APIC registers at default base. */
25 #define LAPIC(x)                $(LAPIC_DEFAULT_BASE | LAPIC_ ## x)
26 #define START_IPI_VECTOR        ((CONFIG_AP_SIPI_VECTOR >> 12) & 0xff)
28 #define CACHE_AS_RAM_SIZE CONFIG_DCACHE_RAM_SIZE
29 #define CACHE_AS_RAM_BASE CONFIG_DCACHE_RAM_BASE
31         /* Save the BIST result. */
32         movl    %eax, %ebp
34 cache_as_ram:
35         post_code(0x20)
37         movl    $LAPIC_BASE_MSR, %ecx
38         rdmsr
39         andl    $LAPIC_BASE_MSR_BOOTSTRAP_PROCESSOR, %eax
40         jz      ap_init
42         /* Zero out all fixed range and variable range MTRRs.
43          * For hyper-threaded CPUs these are shared.
44          */
45         movl    $mtrr_table, %esi
46         movl    $((mtrr_table_end - mtrr_table) >> 1), %edi
47         xorl    %eax, %eax
48         xorl    %edx, %edx
49 clear_mtrrs:
50         movw    (%esi), %bx
51         movzx   %bx, %ecx
52         wrmsr
53         add     $2, %esi
54         dec     %edi
55         jnz     clear_mtrrs
57         post_code(0x21)
59         /* Configure the default memory type to uncacheable. */
60         movl    $MTRR_DEF_TYPE_MSR, %ecx
61         rdmsr
62         andl    $(~0x00000cff), %eax
63         wrmsr
65         post_code(0x22)
67         /* Determine CPU_ADDR_BITS and load PHYSMASK high
68          * word to %edx.
69          */
70         movl    $0x80000000, %eax
71         cpuid
72         cmpl    $0x80000008, %eax
73         jc      addrsize_no_MSR
74         movl    $0x80000008, %eax
75         cpuid
76         movb    %al, %cl
77         sub     $32, %cl
78         movl    $1, %edx
79         shl     %cl, %edx
80         subl    $1, %edx
81         jmp     addrsize_set_high
82 addrsize_no_MSR:
83         movl    $1, %eax
84         cpuid
85         andl    $(1<<6 | 1<<17), %edx   /* PAE or PSE36 */
86         jz      addrsize_set_high
87         movl    $0x0f, %edx
89         /* Preload high word of address mask (in %edx) for Variable
90          * MTRRs 0 and 1 and enable local apic at default base.
91          */
92 addrsize_set_high:
93         xorl    %eax, %eax
94         movl    $MTRR_PHYS_MASK(0), %ecx
95         wrmsr
96         movl    $MTRR_PHYS_MASK(1), %ecx
97         wrmsr
98         movl    $LAPIC_BASE_MSR, %ecx
99         not     %edx
100         movl    %edx, %ebx
101         rdmsr
102         andl    %ebx, %edx
103         andl    $(~LAPIC_BASE_MSR_ADDR_MASK), %eax
104         orl     $(LAPIC_DEFAULT_BASE | LAPIC_BASE_MSR_ENABLE), %eax
105         wrmsr
107 bsp_init:
109         post_code(0x23)
111         /* Send INIT IPI to all excluding ourself. */
112         movl    LAPIC(ICR), %edi
113         movl    $(LAPIC_DEST_ALLBUT | LAPIC_INT_ASSERT | LAPIC_DM_INIT), %eax
114 1:      movl    %eax, (%edi)
115         movl    $0x30, %ecx
116 2:      pause
117         dec     %ecx
118         jnz     2b
119         movl    (%edi), %ecx
120         andl    $LAPIC_ICR_BUSY, %ecx
121         jnz     1b
123         post_code(0x24)
125         movl    $1, %eax
126         cpuid
127         btl     $28, %edx
128         jnc     sipi_complete
129         bswapl  %ebx
130         movzx   %bh, %edi
131         cmpb    $1, %bh
132         jbe     sipi_complete   /* only one LAPIC ID in package */
134         movl    $0, %eax
135         cpuid
136         movb    $1, %bl
137         cmpl    $4, %eax
138         jb      cores_counted
139         movl    $4, %eax
140         movl    $0, %ecx
141         cpuid
142         shr     $26, %eax
143         movb    %al, %bl
144         inc     %bl
146 cores_counted:
147         movl    %edi, %eax
148         divb    %bl
149         cmpb    $1, %al
150         jbe     sipi_complete   /* only LAPIC ID of a core */
152         /* For a hyper-threading processor, cache must not be disabled
153          * on an AP on the same physical package with the BSP.
154          */
156 hyper_threading_cpu:
158         /* delay 10 ms */
159         movl    $10000, %ecx
160 1:      inb     $0x80, %al
161         dec     %ecx
162         jnz     1b
164         post_code(0x25)
166         /* Send Start IPI to all excluding ourself. */
167         movl    LAPIC(ICR), %edi
168         movl    $(LAPIC_DEST_ALLBUT | LAPIC_DM_STARTUP | START_IPI_VECTOR), %eax
169 1:      movl    %eax, (%edi)
170         movl    $0x30, %ecx
171 2:      pause
172         dec     %ecx
173         jnz     2b
174         movl    (%edi), %ecx
175         andl    $LAPIC_ICR_BUSY, %ecx
176         jnz     1b
178         /* delay 250 us */
179         movl    $250, %ecx
180 1:      inb     $0x80, %al
181         dec     %ecx
182         jnz     1b
184         post_code(0x26)
186         /* Wait for sibling CPU to start. */
187 1:      movl    $(MTRR_PHYS_BASE(0)), %ecx
188         rdmsr
189         andl    %eax, %eax
190         jnz     sipi_complete
192         movl    $0x30, %ecx
193 2:      pause
194         dec     %ecx
195         jnz     2b
196         jmp     1b
199 ap_init:
200         post_code(0x27)
202         /* Do not disable cache (so BSP can enable it). */
203         movl    %cr0, %eax
204         andl    $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
205         movl    %eax, %cr0
207         post_code(0x28)
209         /* MTRR registers are shared between HT siblings. */
210         movl    $(MTRR_PHYS_BASE(0)), %ecx
211         movl    $(1<<12), %eax
212         xorl    %edx, %edx
213         wrmsr
215         post_code(0x29)
217 ap_halt:
218         cli
219 1:      hlt
220         jmp     1b
224 sipi_complete:
226         post_code(0x2a)
228         /* Set Cache-as-RAM base address. */
229         movl    $(MTRR_PHYS_BASE(0)), %ecx
230         movl    $(CACHE_AS_RAM_BASE | MTRR_TYPE_WRBACK), %eax
231         xorl    %edx, %edx
232         wrmsr
234         /* Set Cache-as-RAM mask. */
235         movl    $(MTRR_PHYS_MASK(0)), %ecx
236         rdmsr
237         movl    $(~(CACHE_AS_RAM_SIZE - 1) | MTRR_PHYS_MASK_VALID), %eax
238         wrmsr
240         /* Enable MTRR. */
241         movl    $MTRR_DEF_TYPE_MSR, %ecx
242         rdmsr
243         orl     $MTRR_DEF_TYPE_EN, %eax
244         wrmsr
246         post_code(0x2b)
248         /* Enable L2 cache Write-Back (WBINVD and FLUSH#).
249          *
250          * MSR is set when DisplayFamily_DisplayModel is one of:
251          * 06_0x, 06_17, 06_1C
252          *
253          * Description says this bit enables use of WBINVD and FLUSH#.
254          * Should this be set only after the system bus and/or memory
255          * controller can successfully handle write cycles?
256          */
258 #define EAX_FAMILY(a)   (a << 8)        /* for family <= 0fH */
259 #define EAX_MODEL(a)    (((a & 0xf0) << 12) | ((a & 0xf) << 4))
261         movl    $1, %eax
262         cpuid
263         movl    %eax, %ebx
264         andl    $EAX_FAMILY(0x0f), %eax
265         cmpl    $EAX_FAMILY(0x06), %eax
266         jne     no_msr_11e
267         movl    %ebx, %eax
268         andl    $EAX_MODEL(0xff), %eax
269         cmpl    $EAX_MODEL(0x17), %eax
270         je      has_msr_11e
271         cmpl    $EAX_MODEL(0x1c), %eax
272         je      has_msr_11e
273         andl    $EAX_MODEL(0xf0), %eax
274         cmpl    $EAX_MODEL(0x00), %eax
275         jne     no_msr_11e
276 has_msr_11e:
277         movl    $0x11e, %ecx
278         rdmsr
279         orl     $(1 << 8), %eax
280         wrmsr
281 no_msr_11e:
283         post_code(0x2c)
285         /* Enable cache (CR0.CD = 0, CR0.NW = 0). */
286         movl    %cr0, %eax
287         andl    $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
288         invd
289         movl    %eax, %cr0
291         /* Clear the cache memory reagion. */
292         cld
293         xorl    %eax, %eax
294         movl    $CACHE_AS_RAM_BASE, %edi
295         movl    $(CACHE_AS_RAM_SIZE >> 2), %ecx
296         rep     stosl
298         /* Enable Cache-as-RAM mode by disabling cache. */
299         movl    %cr0, %eax
300         orl     $CR0_CacheDisable, %eax
301         movl    %eax, %cr0
303         post_code(0x2d)
305 #if CONFIG_XIP_ROM_SIZE
306         /* Enable cache for our code in Flash because we do XIP here */
307         movl    $MTRR_PHYS_BASE(1), %ecx
308         xorl    %edx, %edx
309         /*
310          * IMPORTANT: The following calculation _must_ be done at runtime. See
311          * http://www.coreboot.org/pipermail/coreboot/2010-October/060855.html
312          */
313         movl    $copy_and_run, %eax
314         andl    $(~(CONFIG_XIP_ROM_SIZE - 1)), %eax
315         orl     $MTRR_TYPE_WRBACK, %eax
316         wrmsr
318         movl    $MTRR_PHYS_MASK(1), %ecx
319         rdmsr
320         movl    $(~(CONFIG_XIP_ROM_SIZE - 1) | MTRR_PHYS_MASK_VALID), %eax
321         wrmsr
322 #endif /* CONFIG_XIP_ROM_SIZE */
324         /* Enable cache. */
325         movl    %cr0, %eax
326         andl    $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
327         movl    %eax, %cr0
329         post_code(0x2e)
331         /* Set up the stack pointer. */
332         movl    $(CACHE_AS_RAM_BASE + CACHE_AS_RAM_SIZE - 4), %esp
334         /* Restore the BIST result. */
335         movl    %ebp, %eax
336         movl    %esp, %ebp
337         pushl   %eax
339         post_code(0x2f)
341         /* Call romstage.c main function. */
342         call    main
343         addl    $4, %esp
345         post_code(0x30)
347         /* Disable cache. */
348         movl    %cr0, %eax
349         orl     $CR0_CacheDisable, %eax
350         movl    %eax, %cr0
352         post_code(0x34)
354         /* Disable MTRR. */
355         movl    $MTRR_DEF_TYPE_MSR, %ecx
356         rdmsr
357         andl    $(~MTRR_DEF_TYPE_EN), %eax
358         wrmsr
360         post_code(0x35)
362         invd
364         post_code(0x36)
366         /* Enable cache. */
367         movl    %cr0, %eax
368         andl    $~(CR0_CacheDisable | CR0_NoWriteThrough), %eax
369         movl    %eax, %cr0
371         post_code(0x37)
373         /* Disable cache. */
374         movl    %cr0, %eax
375         orl     $CR0_CacheDisable, %eax
376         movl    %eax, %cr0
378         post_code(0x38)
380         /* Enable Write Back and Speculative Reads for low RAM. */
381         movl    $MTRR_PHYS_BASE(0), %ecx
382         movl    $(0x00000000 | MTRR_TYPE_WRBACK), %eax
383         xorl    %edx, %edx
384         wrmsr
385         movl    $MTRR_PHYS_MASK(0), %ecx
386         rdmsr
387         movl    $(~(CONFIG_RAMTOP - 1) | MTRR_PHYS_MASK_VALID), %eax
388         wrmsr
390 #if CACHE_ROM_SIZE
391         /* Enable caching and Speculative Reads for Flash ROM device. */
392         movl    $MTRR_PHYS_BASE(1), %ecx
393         movl    $(CACHE_ROM_BASE | MTRR_TYPE_WRPROT), %eax
394         xorl    %edx, %edx
395         wrmsr
396         movl    $MTRR_PHYS_MASK(1), %ecx
397         rdmsr
398         movl    $(~(CACHE_ROM_SIZE - 1) | MTRR_PHYS_MASK_VALID), %eax
399         wrmsr
400 #endif
402         post_code(0x39)
404         /* And enable cache again after setting MTRRs. */
405         movl    %cr0, %eax
406         andl    $~(CR0_CacheDisable | CR0_NoWriteThrough), %eax
407         movl    %eax, %cr0
409         post_code(0x3a)
411         /* Enable MTRR. */
412         movl    $MTRR_DEF_TYPE_MSR, %ecx
413         rdmsr
414         orl     $MTRR_DEF_TYPE_EN, %eax
415         wrmsr
417         post_code(0x3b)
419         /* Invalidate the cache again. */
420         invd
422         post_code(0x3c)
424 __main:
425         post_code(POST_PREPARE_RAMSTAGE)
426         cld                     /* Clear direction flag. */
428         movl    $CONFIG_RAMTOP, %esp
429         movl    %esp, %ebp
430         call    copy_and_run
432 .Lhlt:
433         post_code(POST_DEAD_CODE)
434         hlt
435         jmp     .Lhlt
437 mtrr_table:
438         /* Fixed MTRRs */
439         .word 0x250, 0x258, 0x259
440         .word 0x268, 0x269, 0x26A
441         .word 0x26B, 0x26C, 0x26D
442         .word 0x26E, 0x26F
443         /* Variable MTRRs */
444         .word 0x200, 0x201, 0x202, 0x203
445         .word 0x204, 0x205, 0x206, 0x207
446         .word 0x208, 0x209, 0x20A, 0x20B
447         .word 0x20C, 0x20D, 0x20E, 0x20F
448 mtrr_table_end: