Add support for Intel Sandybridge CPU
[coreboot.git] / src / cpu / intel / model_206ax / cache_as_ram.inc
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) 2007-2008 coresystems GmbH
6  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <cpu/x86/stack.h>
22 #include <cpu/x86/mtrr.h>
23 #include <cpu/x86/post_code.h>
24 #include <cbmem.h>
25
26 #define CACHE_AS_RAM_SIZE CONFIG_DCACHE_RAM_SIZE
27 #define CACHE_AS_RAM_BASE CONFIG_DCACHE_RAM_BASE
28
29 /* Cache 4GB - MRC_SIZE_KB for MRC */
30 #define CACHE_MRC_BYTES   ((CONFIG_CACHE_MRC_SIZE_KB << 10) - 1)
31 #define CACHE_MRC_BASE    (0xFFFFFFFF - CACHE_MRC_BYTES)
32 #define CACHE_MRC_MASK    (~CACHE_MRC_BYTES)
33
34 #define CPU_MAXPHYSADDR 36
35 #define CPU_PHYSMASK_HI  (1 << (CPU_MAXPHYSADDR - 32) - 1)
36
37 #define NoEvictMod_MSR 0x2e0
38
39         /* Save the BIST result. */
40         movl    %eax, %ebp
41
42 cache_as_ram:
43         post_code(0x20)
44
45         /* Send INIT IPI to all excluding ourself. */
46         movl    $0x000C4500, %eax
47         movl    $0xFEE00300, %esi
48         movl    %eax, (%esi)
49
50         /* All CPUs need to be in Wait for SIPI state */
51 wait_for_sipi:
52         movl    (%esi), %eax
53         bt      $12, %eax
54         jc      wait_for_sipi
55
56         post_code(0x21)
57         /* Zero out all fixed range and variable range MTRRs. */
58         movl    $mtrr_table, %esi
59         movl    $((mtrr_table_end - mtrr_table) / 2), %edi
60         xorl    %eax, %eax
61         xorl    %edx, %edx
62 clear_mtrrs:
63         movw    (%esi), %bx
64         movzx   %bx, %ecx
65         wrmsr
66         add     $2, %esi
67         dec     %edi
68         jnz     clear_mtrrs
69
70         post_code(0x22)
71         /* Configure the default memory type to uncacheable. */
72         movl    $MTRRdefType_MSR, %ecx
73         rdmsr
74         andl    $(~0x00000cff), %eax
75         wrmsr
76
77         post_code(0x23)
78         /* Set Cache-as-RAM base address. */
79         movl    $(MTRRphysBase_MSR(0)), %ecx
80         movl    $(CACHE_AS_RAM_BASE | MTRR_TYPE_WRBACK), %eax
81         xorl    %edx, %edx
82         wrmsr
83
84         post_code(0x24)
85         /* Set Cache-as-RAM mask. */
86         movl    $(MTRRphysMask_MSR(0)), %ecx
87         movl    $(~(CACHE_AS_RAM_SIZE - 1) | MTRRphysMaskValid), %eax
88         movl    $CPU_PHYSMASK_HI, %edx
89         wrmsr
90
91         post_code(0x25)
92
93         /* Enable MTRR. */
94         movl    $MTRRdefType_MSR, %ecx
95         rdmsr
96         orl     $MTRRdefTypeEn, %eax
97         wrmsr
98
99         /* Enable cache (CR0.CD = 0, CR0.NW = 0). */
100         movl    %cr0, %eax
101         andl    $(~((1 << 30) | (1 << 29))), %eax
102         invd
103         movl    %eax, %cr0
104
105         /* enable the 'no eviction' mode */
106         movl    $NoEvictMod_MSR, %ecx
107         rdmsr
108         orl     $1, %eax
109         andl    $~2, %eax
110         wrmsr
111
112        /* Clear the cache memory region. This will also fill up the cache */
113         movl    $CACHE_AS_RAM_BASE, %esi
114         movl    %esi, %edi
115         movl    $(CACHE_AS_RAM_SIZE / 4), %ecx
116         // movl $0x23322332, %eax
117         xorl    %eax, %eax
118         rep     stosl
119
120         /* enable the 'no eviction run' state */
121         movl    $NoEvictMod_MSR, %ecx
122         rdmsr
123         orl     $3, %eax
124         wrmsr
125
126         post_code(0x26)
127         /* Enable Cache-as-RAM mode by disabling cache. */
128         movl    %cr0, %eax
129         orl     $(1 << 30), %eax
130         movl    %eax, %cr0
131
132         /* Enable cache for our code in Flash because we do XIP here */
133         movl    $MTRRphysBase_MSR(1), %ecx
134         xorl    %edx, %edx
135         /*
136          * IMPORTANT: The following calculation _must_ be done at runtime. See
137          * http://www.coreboot.org/pipermail/coreboot/2010-October/060855.html
138          */
139         movl    $copy_and_run, %eax
140         andl    $(~(CONFIG_XIP_ROM_SIZE - 1)), %eax
141         orl     $MTRR_TYPE_WRPROT, %eax
142         wrmsr
143
144         movl    $MTRRphysMask_MSR(1), %ecx
145         movl    $CPU_PHYSMASK_HI, %edx
146         movl    $(~(CONFIG_XIP_ROM_SIZE - 1) | MTRRphysMaskValid), %eax
147         wrmsr
148
149         post_code(0x27)
150 #if CONFIG_CACHE_MRC_BIN
151         /* Enable caching for ram init code to run faster */
152         movl    $MTRRphysBase_MSR(2), %ecx
153         movl    $(CACHE_MRC_BASE | MTRR_TYPE_WRPROT), %eax
154         xorl    %edx, %edx
155         wrmsr
156         movl    $MTRRphysMask_MSR(2), %ecx
157         movl    $(CACHE_MRC_MASK | MTRRphysMaskValid), %eax
158         movl    $CPU_PHYSMASK_HI, %edx
159         wrmsr
160 #endif
161
162         post_code(0x28)
163         /* Enable cache. */
164         movl    %cr0, %eax
165         andl    $(~((1 << 30) | (1 << 29))), %eax
166         movl    %eax, %cr0
167
168         /* Set up the stack pointer below MRC variable space. */
169         movl    $(CACHE_AS_RAM_SIZE + CACHE_AS_RAM_BASE - \
170                   CONFIG_DCACHE_RAM_MRC_VAR_SIZE - 4), %eax
171         movl    %eax, %esp
172
173         /* Restore the BIST result. */
174         movl    %ebp, %eax
175         movl    %esp, %ebp
176         pushl   %eax
177
178 before_romstage:
179         post_code(0x29)
180         /* Call romstage.c main function. */
181         call    main
182
183         post_code(0x2f)
184
185         /* Copy global variable space (for USBDEBUG) to memory */
186 #if CONFIG_USBDEBUG
187         cld
188         movl    $(CONFIG_DCACHE_RAM_BASE + CONFIG_DCACHE_RAM_SIZE - 24), %esi
189         movl    $(CONFIG_RAMTOP - 24), %edi
190         movl    $24, %ecx
191         rep     movsb
192 #endif
193
194         post_code(0x30)
195
196         /* Disable cache. */
197         movl    %cr0, %eax
198         orl     $(1 << 30), %eax
199         movl    %eax, %cr0
200
201         post_code(0x31)
202
203         /* Disable MTRR. */
204         movl    $MTRRdefType_MSR, %ecx
205         rdmsr
206         andl    $(~MTRRdefTypeEn), %eax
207         wrmsr
208
209         post_code(0x31)
210
211         /* Disable the no eviction run state */
212         movl    $NoEvictMod_MSR, %ecx
213         rdmsr
214         andl    $~2, %eax
215         wrmsr
216
217         invd
218
219         /* Disable the no eviction mode */
220         rdmsr
221         andl    $~1, %eax
222         wrmsr
223
224 #if CONFIG_CACHE_MRC_BIN
225         /* Clear MTRR that was used to cache MRC */
226         xorl    %eax, %eax
227         xorl    %edx, %edx
228         movl    $MTRRphysBase_MSR(2), %ecx
229         wrmsr
230         movl    $MTRRphysMask_MSR(2), %ecx
231         wrmsr
232 #endif
233
234         post_code(0x33)
235
236         /* Enable cache. */
237         movl    %cr0, %eax
238         andl    $~((1 << 30) | (1 << 29)), %eax
239         movl    %eax, %cr0
240
241         post_code(0x36)
242
243         /* Disable cache. */
244         movl    %cr0, %eax
245         orl     $(1 << 30), %eax
246         movl    %eax, %cr0
247
248         post_code(0x38)
249
250         /* Enable Write Back and Speculative Reads for the first MB
251          * and coreboot_ram.
252          */
253         movl    $MTRRphysBase_MSR(0), %ecx
254         movl    $(0x00000000 | MTRR_TYPE_WRBACK), %eax
255         xorl    %edx, %edx
256         wrmsr
257         movl    $MTRRphysMask_MSR(0), %ecx
258         movl    $(~(CONFIG_RAMTOP - 1) | MTRRphysMaskValid), %eax
259         movl    $CPU_PHYSMASK_HI, %edx  // 36bit address space
260         wrmsr
261
262         /* Enable Caching and speculative Reads for the
263          * complete ROM now that we actually have RAM.
264          */
265         movl    $MTRRphysBase_MSR(1), %ecx
266         movl    $(0xffc00000 | MTRR_TYPE_WRPROT), %eax
267         xorl    %edx, %edx
268         wrmsr
269         movl    $MTRRphysMask_MSR(1), %ecx
270         movl    $(~(4*1024*1024 - 1) | MTRRphysMaskValid), %eax
271         movl    $CPU_PHYSMASK_HI, %edx
272         wrmsr
273
274         post_code(0x39)
275
276         /* And enable cache again after setting MTRRs. */
277         movl    %cr0, %eax
278         andl    $~((1 << 30) | (1 << 29)), %eax
279         movl    %eax, %cr0
280
281         post_code(0x3a)
282
283         /* Enable MTRR. */
284         movl    $MTRRdefType_MSR, %ecx
285         rdmsr
286         orl     $MTRRdefTypeEn, %eax
287         wrmsr
288
289         post_code(0x3b)
290
291         /* Invalidate the cache again. */
292         invd
293
294         post_code(0x3c)
295
296 #if CONFIG_HAVE_ACPI_RESUME
297         movl    CBMEM_BOOT_MODE, %eax
298         cmpl    $0x2, %eax // Resume?
299         jne     __acpi_resume_backup_done
300
301         /* copy 1MB - 64K to high tables ram_base to prevent memory corruption
302          * through stage 2. We could keep stuff like stack and heap in high
303          * tables memory completely, but that's a wonderful clean up task for
304          * another day.
305          */
306         cld
307         movl    $CONFIG_RAMBASE, %esi
308         movl    CBMEM_RESUME_BACKUP, %edi
309         movl    $HIGH_MEMORY_SAVE / 4, %ecx
310         rep     movsl
311
312 __acpi_resume_backup_done:
313 #endif
314
315         post_code(0x3d)
316
317         /* Clear boot_complete flag. */
318         xorl    %ebp, %ebp
319 __main:
320         post_code(POST_PREPARE_RAMSTAGE)
321         cld                     /* Clear direction flag. */
322
323         movl    %ebp, %esi
324
325         movl    $ROMSTAGE_STACK, %esp
326         movl    %esp, %ebp
327         pushl   %esi
328         call    copy_and_run
329
330 .Lhlt:
331         post_code(POST_DEAD_CODE)
332         hlt
333         jmp     .Lhlt
334
335 mtrr_table:
336         /* Fixed MTRRs */
337         .word 0x250, 0x258, 0x259
338         .word 0x268, 0x269, 0x26A
339         .word 0x26B, 0x26C, 0x26D
340         .word 0x26E, 0x26F
341         /* Variable MTRRs */
342         .word 0x200, 0x201, 0x202, 0x203
343         .word 0x204, 0x205, 0x206, 0x207
344         .word 0x208, 0x209, 0x20A, 0x20B
345         .word 0x20C, 0x20D, 0x20E, 0x20F
346         .word 0x210, 0x211, 0x212, 0x213
347 mtrr_table_end:
348