more ifdef -> if fixes
[coreboot.git] / src / cpu / via / car / 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) 2005 Eswar Nallusamy, LANL
6  * Copyright (C) 2005 Tyan
7  * (Written by Yinghai Lu <yhlu@tyan.com> for Tyan)
8  * Copyright (C) 2007 coresystems GmbH
9  * (Written by Stefan Reinauer <stepan@coresystems.de> for coresystems GmbH)
10  * Copyright (C) 2007,2008 Carl-Daniel Hailfinger
11  * Copyright (C) 2008 VIA Technologies, Inc.
12  * (Written by Jason Zhao <jasonzhao@viatech.com.cn> for VIA)
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; version 2 of the License.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
26  */
27
28 #include <cpu/x86/stack.h>
29 #include <cpu/x86/mtrr.h>
30 #include <console/post_codes.h>
31
32 #define CacheSize               CONFIG_DCACHE_RAM_SIZE
33 #define CacheBase               CONFIG_DCACHE_RAM_BASE
34
35         /* Save the BIST result. */
36         movl    %eax, %ebp
37
38 CacheAsRam:
39
40         /* Disable cache. */
41         movl    %cr0, %eax
42         orl     $(1 << 30), %eax
43         movl    %eax, %cr0
44         invd
45
46         /* Set the default memory type and enable fixed and variable MTRRs. */
47         movl    $MTRRdefType_MSR, %ecx
48         xorl    %edx, %edx
49         movl    $(MTRRdefTypeEn | MTRRdefTypeFixEn), %eax
50         wrmsr
51
52         /* Clear all MTRRs. */
53         xorl    %edx, %edx
54         movl    $all_mtrr_msrs, %esi
55
56 clear_fixed_var_mtrr:
57         lodsl   (%esi), %eax
58         testl   %eax, %eax
59         jz      clear_fixed_var_mtrr_out
60
61         movl    %eax, %ecx
62         xorl    %eax, %eax
63         wrmsr
64
65         jmp     clear_fixed_var_mtrr
66
67 all_mtrr_msrs:
68         /* fixed MTRR MSRs */
69         .long   MTRRfix64K_00000_MSR
70         .long   MTRRfix16K_80000_MSR
71         .long   MTRRfix16K_A0000_MSR
72         .long   MTRRfix4K_C0000_MSR
73         .long   MTRRfix4K_C8000_MSR
74         .long   MTRRfix4K_D0000_MSR
75         .long   MTRRfix4K_D8000_MSR
76         .long   MTRRfix4K_E0000_MSR
77         .long   MTRRfix4K_E8000_MSR
78         .long   MTRRfix4K_F0000_MSR
79         .long   MTRRfix4K_F8000_MSR
80
81         /* var MTRR MSRs */
82         .long   MTRRphysBase_MSR(0)
83         .long   MTRRphysMask_MSR(0)
84         .long   MTRRphysBase_MSR(1)
85         .long   MTRRphysMask_MSR(1)
86         .long   MTRRphysBase_MSR(2)
87         .long   MTRRphysMask_MSR(2)
88         .long   MTRRphysBase_MSR(3)
89         .long   MTRRphysMask_MSR(3)
90         .long   MTRRphysBase_MSR(4)
91         .long   MTRRphysMask_MSR(4)
92         .long   MTRRphysBase_MSR(5)
93         .long   MTRRphysMask_MSR(5)
94         .long   MTRRphysBase_MSR(6)
95         .long   MTRRphysMask_MSR(6)
96         .long   MTRRphysBase_MSR(7)
97         .long   MTRRphysMask_MSR(7)
98
99         .long   0x000 /* NULL, end of table */
100
101 clear_fixed_var_mtrr_out:
102         movl    $MTRRphysBase_MSR(0), %ecx
103         xorl    %edx, %edx
104         movl    $(CacheBase | MTRR_TYPE_WRBACK), %eax
105         wrmsr
106
107         movl    $MTRRphysMask_MSR(0), %ecx
108         /* This assumes we never access addresses above 2^36 in CAR. */
109         movl    $0x0000000f, %edx
110         movl    $(~(CacheSize - 1) | MTRRphysMaskValid), %eax
111         wrmsr
112
113 #if CONFIG_TINY_BOOTBLOCK
114 #define REAL_XIP_ROM_BASE AUTO_XIP_ROM_BASE
115 #else
116 #define REAL_XIP_ROM_BASE CONFIG_XIP_ROM_BASE
117 #endif
118
119         /*
120          * Enable write base caching so we can do execute in place (XIP)
121          * on the flash ROM.
122          */
123         movl    $MTRRphysBase_MSR(1), %ecx
124         xorl    %edx, %edx
125         /*
126          * IMPORTANT: The two lines below can _not_ be written like this:
127          *   movl $(REAL_XIP_ROM_BASE | MTRR_TYPE_WRBACK), %eax
128          * http://www.coreboot.org/pipermail/coreboot/2010-October/060855.html
129          */
130         movl    $REAL_XIP_ROM_BASE, %eax
131         orl     $MTRR_TYPE_WRBACK, %eax
132         wrmsr
133
134         movl    $MTRRphysMask_MSR(1), %ecx
135         movl    $0x0000000f, %edx
136         movl    $(~(CONFIG_XIP_ROM_SIZE - 1) | MTRRphysMaskValid), %eax
137         wrmsr
138
139         /* Set the default memory type and enable fixed and variable MTRRs. */
140         /* TODO: Or also enable fixed MTRRs? Bug in the code? */
141         movl    $MTRRdefType_MSR, %ecx
142         xorl    %edx, %edx
143         movl    $(MTRRdefTypeEn), %eax
144         wrmsr
145
146         /* Enable cache. */
147         movl    %cr0, %eax
148         andl    $(~((1 << 30) | (1 << 29))), %eax
149         movl    %eax, %cr0
150
151         /* Read the range with lodsl. */
152         cld
153         movl    $CacheBase, %esi
154         movl    %esi, %edi
155         movl    $(CacheSize >> 2), %ecx
156         rep     lodsl
157
158         movl    $CacheBase, %esi
159         movl    %esi, %edi
160         movl    $(CacheSize >> 2), %ecx
161
162         /*
163          * 0x5c5c5c5c is a memory test pattern.
164          * TODO: Check if everything works with the zero pattern as well.
165          */
166         /* xorl %eax, %eax */
167         xorl    $0x5c5c5c5c, %eax
168         rep     stosl
169
170 #ifdef CARTEST
171         movl    REAL_XIP_ROM_BASE, %esi
172         movl    %esi, %edi
173         movl    $(CONFIG_XIP_ROM_SIZE >> 2), %ecx
174         rep     lodsl
175 #endif
176
177         /*
178          * The key point of this CAR code is C7 cache does not turn into
179          * "no fill" mode, which is not compatible with general CAR code.
180          */
181
182         movl    $(CacheBase + CacheSize - 4), %eax
183         movl    %eax, %esp
184
185 #ifdef CARTEST
186 testok: 
187         post_code(0x40)
188         xorl    %edx, %edx
189         xorl    %eax, %eax
190         movl    $0x5c5c, %edx
191         pushl   %edx
192         pushl   %edx
193         pushl   %edx
194         pushl   %edx
195         pushl   %edx
196         popl    %esi
197         popl    %esi
198         popl    %eax
199         popl    %eax
200         popl    %eax
201         cmpl    %edx, %eax
202         jne     stackerr
203 #endif
204
205         /* Restore the BIST result. */
206         movl    %ebp, %eax
207
208         /* We need to set EBP? No need. */
209         movl    %esp, %ebp
210         pushl   %eax    /* BIST */
211         call    main
212
213         /*
214          * TODO: Backup stack in CACHE_AS_RAM into MMX and SSE and after we
215          *       get STACK up, we restore that. It is only needed if we
216          *       want to go back.
217          */
218
219         /* We don't need CAR from now on. */
220
221         /* Disable cache. */
222         movl    %cr0, %eax
223         orl     $(1 << 30), %eax
224         movl    %eax, %cr0
225
226         /* Set the default memory type and enable variable MTRRs. */
227         /* TODO: Or also enable fixed MTRRs? Bug in the code? */
228         movl    $MTRRdefType_MSR, %ecx
229         xorl    %edx, %edx
230         movl    $(MTRRdefTypeEn), %eax
231         wrmsr
232
233         /* Enable caching for CONFIG_RAMBASE..CONFIG_RAMTOP. */
234         movl    $MTRRphysBase_MSR(0), %ecx
235         xorl    %edx, %edx
236         movl    $(CONFIG_RAMBASE | MTRR_TYPE_WRBACK), %eax
237         wrmsr
238
239         movl    $MTRRphysMask_MSR(0), %ecx
240         movl    $0x0000000f, %edx       /* AMD 40 bit 0xff */
241         movl    $(~(CONFIG_RAMTOP - CONFIG_RAMBASE - 1) | MTRRphysMaskValid), %eax
242         wrmsr
243
244         /* Cache XIP_ROM_BASE-SIZE to speedup coreboot code. */
245         movl    $MTRRphysBase_MSR(1), %ecx
246         xorl    %edx, %edx
247         movl    $REAL_XIP_ROM_BASE, %eax
248         orl     $MTRR_TYPE_WRBACK, %eax
249         wrmsr
250
251         movl    $MTRRphysMask_MSR(1), %ecx
252         xorl    %edx, %edx
253         movl    $(~(CONFIG_XIP_ROM_SIZE - 1) | MTRRphysMaskValid), %eax
254         wrmsr
255
256         /* Enable cache. */
257         movl    %cr0, %eax
258         andl    $(~((1 << 30) | (1 << 29))), %eax
259         movl    %eax, %cr0
260         invd
261
262         /* Clear boot_complete flag. */
263         xorl    %ebp, %ebp
264 __main:
265         post_code(POST_PREPARE_RAMSTAGE)
266         cld                     /* Clear direction flag. */
267
268         movl    %ebp, %esi
269
270         movl    $ROMSTAGE_STACK, %esp
271         movl    %esp, %ebp
272         pushl   %esi
273         call    copy_and_run
274
275 .Lhlt:
276         post_code(POST_DEAD_CODE)
277         hlt
278         jmp     .Lhlt
279