port latest model 6ex car changes to 6fx car, which is almost identical and
[coreboot.git] / src / cpu / intel / model_6fx / 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 #define CACHE_AS_RAM_SIZE CONFIG_DCACHE_RAM_SIZE
22 #define CACHE_AS_RAM_BASE CONFIG_DCACHE_RAM_BASE
23
24 #include <cpu/x86/mtrr.h>
25 #include <cpu/amd/mtrr.h>
26
27         /* Save the BIST result */
28         movl    %eax, %ebp
29
30 cache_as_ram:
31         post_code(0x20)
32
33         /* Send INIT IPI to all excluding ourself */
34         movl    $0x000C4500, %eax
35         movl    $0xFEE00300, %esi
36         movl    %eax, (%esi)
37
38         /* Disable prefetchers */
39         movl    $0x01a0, %ecx
40         rdmsr
41         orl     $((1 << 9) | (1 << 19)), %eax
42         orl     $((1 << 5) | (1 << 7)), %edx
43         wrmsr
44
45         /* Zero out all Fixed Range and Variable Range MTRRs */
46         movl    $mtrr_table, %esi
47         movl    $( (mtrr_table_end - mtrr_table) / 2), %edi
48         xorl    %eax, %eax
49         xorl    %edx, %edx
50 clear_mtrrs:
51         movw    (%esi), %bx
52         movzx   %bx, %ecx
53         wrmsr
54         add     $2, %esi
55         dec     %edi
56         jnz     clear_mtrrs
57
58         /* Configure the default memory type to uncacheable */
59         movl    $MTRRdefType_MSR, %ecx
60         rdmsr
61         andl    $(~0x00000cff), %eax
62         wrmsr
63
64         /* Set cache as ram base address */
65         movl    $(MTRRphysBase_MSR(0)), %ecx
66         movl    $(CACHE_AS_RAM_BASE | MTRR_TYPE_WRBACK), %eax
67         xorl    %edx, %edx
68         wrmsr
69
70         /* Set cache as ram mask */
71         movl    $(MTRRphysMask_MSR(0)), %ecx
72         movl    $(~((CACHE_AS_RAM_SIZE-1)) | (1 << 11)), %eax
73         movl    $0x0000000f, %edx
74         wrmsr
75
76         /* Enable MTRR */
77         movl    $MTRRdefType_MSR, %ecx
78         rdmsr
79         orl     $(1 << 11), %eax
80         wrmsr
81
82         /* Enable L2 Cache */
83         movl    $0x11e, %ecx
84         rdmsr
85         orl     $(1 << 8), %eax
86         wrmsr
87
88         /* CR0.CD = 0, CR0.NW = 0 */
89         movl    %cr0, %eax
90         andl    $( ~( (1 << 30) | (1 << 29) ) ), %eax
91         invd
92         movl    %eax, %cr0
93
94         /* Clear the cache memory reagion */
95         movl    $CACHE_AS_RAM_BASE, %esi
96         movl    %esi, %edi
97         movl    $(CACHE_AS_RAM_SIZE / 4), %ecx
98         //movl  $0x23322332, %eax
99         xorl    %eax, %eax
100         rep     stosl
101
102         /* Enable Cache As RAM mode by disabling cache */
103         movl    %cr0, %eax
104         orl     $(1 << 30), %eax
105         movl    %eax, %cr0
106
107 #if defined(CONFIG_XIP_ROM_SIZE) && defined(CONFIG_XIP_ROM_BASE)
108         /* Enable cache for our code in Flash because we do XIP here */
109         movl    $MTRRphysBase_MSR(1), %ecx
110         xorl    %edx, %edx
111 #if defined(CONFIG_TINY_BOOTBLOCK) && CONFIG_TINY_BOOTBLOCK
112 #define REAL_XIP_ROM_BASE AUTO_XIP_ROM_BASE
113 #else
114 #define REAL_XIP_ROM_BASE CONFIG_XIP_ROM_BASE
115 #endif
116         movl    $REAL_XIP_ROM_BASE, %eax
117         orl     $MTRR_TYPE_WRBACK, %eax
118         wrmsr
119
120         movl    $MTRRphysMask_MSR(1), %ecx
121         movl    $0x0000000f, %edx
122         movl    $(~(CONFIG_XIP_ROM_SIZE - 1) | 0x800), %eax
123         wrmsr
124 #endif /* CONFIG_XIP_ROM_SIZE && CONFIG_XIP_ROM_BASE */
125
126         /* enable cache */
127         movl    %cr0, %eax
128         andl    $( ~( (1 << 30) | (1 << 29) ) ), %eax
129         movl    %eax, %cr0
130
131         /* Set up stack pointer */
132 #if defined(CONFIG_USBDEBUG_DIRECT) && (CONFIG_USBDEBUG_DIRECT == 1)
133         /* leave some space for the struct ehci_debug_info */
134         movl    $(CACHE_AS_RAM_BASE + CACHE_AS_RAM_SIZE - 4 - 128), %eax
135 #else
136         movl    $(CACHE_AS_RAM_BASE + CACHE_AS_RAM_SIZE - 4), %eax
137 #endif
138         movl    %eax, %esp
139
140         /* Restore the BIST result */
141         movl    %ebp, %eax
142         movl    %esp, %ebp
143         pushl   %eax
144
145         post_code(0x23)
146
147         /* Call romstage.c main function */
148         call    main
149
150         post_code(0x2f)
151
152         post_code(0x30)
153
154         /* Disable Cache */
155         movl    %cr0, %eax
156         orl    $(1 << 30), %eax
157         movl    %eax, %cr0
158
159         post_code(0x31)
160
161         /* Disable MTRR */
162         movl    $MTRRdefType_MSR, %ecx
163         rdmsr
164         andl    $(~(1 << 11)), %eax
165         wrmsr
166
167         post_code(0x31)
168
169         invd
170 #if 0
171         xorl    %eax, %eax
172         xorl    %edx, %edx
173         movl    $MTRRphysBase_MSR(0), %ecx
174         wrmsr
175         movl    $MTRRphysMask_MSR(0), %ecx
176         wrmsr
177         movl    $MTRRphysBase_MSR(1), %ecx
178         wrmsr
179         movl    $MTRRphysMask_MSR(1), %ecx
180         wrmsr
181 #endif
182
183         post_code(0x33)
184
185 #undef CLEAR_FIRST_1M_RAM
186 #ifdef CLEAR_FIRST_1M_RAM
187         post_code(0x34)
188         /* Enable Write Combining and Speculative Reads for the first 1MB */
189         movl    $MTRRphysBase_MSR(0), %ecx
190         movl    $(0x00000000 | MTRR_TYPE_WRCOMB), %eax
191         xorl    %edx, %edx
192         wrmsr
193         movl    $MTRRphysMask_MSR(0), %ecx
194         movl    $(~(1024*1024 -1) | (1 << 11)), %eax
195         movl    $0x0000000f, %edx       // 36bit address space
196         wrmsr
197         post_code(0x35)
198 #endif
199
200         /* Enable Cache */
201         movl    %cr0, %eax
202         andl    $~( (1 << 30) | (1 << 29) ), %eax
203         movl    %eax, %cr0
204
205
206         post_code(0x36)
207 #ifdef CLEAR_FIRST_1M_RAM
208
209         /* Clear first 1MB of RAM */
210         movl    $0x00000000, %edi
211         cld
212         xorl    %eax, %eax
213         movl    $((1024*1024) / 4), %ecx
214         rep stosl
215         
216         post_code(0x37)
217 #endif
218
219         /* Disable Cache */
220         movl    %cr0, %eax
221         orl    $(1 << 30), %eax
222         movl    %eax, %cr0
223
224         post_code(0x38)
225
226         /* Enable Write Back and Speculative Reads for the first 1MB */
227         movl    $MTRRphysBase_MSR(0), %ecx
228         movl    $(0x00000000 | MTRR_TYPE_WRBACK), %eax
229         xorl    %edx, %edx
230         wrmsr
231         movl    $MTRRphysMask_MSR(0), %ecx
232         movl    $(~(1024*1024 -1) | (1 << 11)), %eax
233         movl    $0x0000000f, %edx       // 36bit address space
234         wrmsr
235
236         post_code(0x39)
237
238         /* And Enable Cache again after setting MTRRs */
239         movl    %cr0, %eax
240         andl    $~( (1 << 30) | (1 << 29) ), %eax
241         movl    %eax, %cr0
242
243         post_code(0x3a)
244
245         /* Enable MTRR */
246         movl    $MTRRdefType_MSR, %ecx
247         rdmsr
248         orl     $(1 << 11), %eax
249         wrmsr
250
251         post_code(0x3b)
252
253         /* Enable prefetchers */
254         movl    $0x01a0, %ecx
255         rdmsr
256         andl    $~((1 << 9) | (1 << 19)), %eax
257         andl    $~((1 << 5) | (1 << 7)), %edx
258         wrmsr
259
260         /* Invalidate the cache again */
261         invd
262
263         post_code(0x3c)
264
265         /* clear boot_complete flag */
266         xorl    %ebp, %ebp
267 __main:
268         post_code(0x11)
269         cld                     /* clear direction flag */
270         
271         movl    %ebp, %esi
272
273         /* For now: use CONFIG_RAMBASE + 1MB - 64K (counting downwards) as stack. This
274          * makes sure that we stay completely within the 1M-64K of memory that we
275          * preserve for suspend/resume.
276          */
277
278 #ifndef HIGH_MEMORY_SAVE
279 #warning Need a central place for HIGH_MEMORY_SAVE
280 #define HIGH_MEMORY_SAVE ( (1024 - 64) * 1024 )
281 #endif
282         movl $(CONFIG_RAMBASE + HIGH_MEMORY_SAVE), %esp
283         movl    %esp, %ebp
284         pushl %esi
285         call copy_and_run
286
287 .Lhlt:  
288         post_code(0xee)
289         hlt
290         jmp     .Lhlt
291
292 mtrr_table:
293         /* Fixed MTRRs */
294         .word 0x250, 0x258, 0x259
295         .word 0x268, 0x269, 0x26A
296         .word 0x26B, 0x26C, 0x26D
297         .word 0x26E, 0x26F
298         /* Variable MTRRs */
299         .word 0x200, 0x201, 0x202, 0x203
300         .word 0x204, 0x205, 0x206, 0x207
301         .word 0x208, 0x209, 0x20A, 0x20B
302         .word 0x20C, 0x20D, 0x20E, 0x20F
303 mtrr_table_end:
304