Revert r5902 to make code more readable again. At least three people like to
[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
31 #define CacheSize               CONFIG_DCACHE_RAM_SIZE
32 #define CacheBase               CONFIG_DCACHE_RAM_BASE
33
34         /* Save the BIST result. */
35         movl    %eax, %ebp
36
37 CacheAsRam:
38
39         /* Disable cache. */
40         movl    %cr0, %eax
41         orl     $(1 << 30), %eax
42         movl    %eax, %cr0
43         invd
44
45         /* Set the default memory type and enable fixed and variable MTRRs. */
46         movl    $MTRRdefType_MSR, %ecx
47         xorl    %edx, %edx
48         movl    $(MTRRdefTypeEn | MTRRdefTypeFixEn), %eax
49         wrmsr
50
51         /* Clear all MTRRs. */
52         xorl    %edx, %edx
53         movl    $all_mtrr_msrs, %esi
54
55 clear_fixed_var_mtrr:
56         lodsl   (%esi), %eax
57         testl   %eax, %eax
58         jz      clear_fixed_var_mtrr_out
59
60         movl    %eax, %ecx
61         xorl    %eax, %eax
62         wrmsr
63
64         jmp     clear_fixed_var_mtrr
65
66 all_mtrr_msrs:
67         /* fixed MTRR MSRs */
68         .long   MTRRfix64K_00000_MSR
69         .long   MTRRfix16K_80000_MSR
70         .long   MTRRfix16K_A0000_MSR
71         .long   MTRRfix4K_C0000_MSR
72         .long   MTRRfix4K_C8000_MSR
73         .long   MTRRfix4K_D0000_MSR
74         .long   MTRRfix4K_D8000_MSR
75         .long   MTRRfix4K_E0000_MSR
76         .long   MTRRfix4K_E8000_MSR
77         .long   MTRRfix4K_F0000_MSR
78         .long   MTRRfix4K_F8000_MSR
79
80         /* var MTRR MSRs */
81         .long   MTRRphysBase_MSR(0)
82         .long   MTRRphysMask_MSR(0)
83         .long   MTRRphysBase_MSR(1)
84         .long   MTRRphysMask_MSR(1)
85         .long   MTRRphysBase_MSR(2)
86         .long   MTRRphysMask_MSR(2)
87         .long   MTRRphysBase_MSR(3)
88         .long   MTRRphysMask_MSR(3)
89         .long   MTRRphysBase_MSR(4)
90         .long   MTRRphysMask_MSR(4)
91         .long   MTRRphysBase_MSR(5)
92         .long   MTRRphysMask_MSR(5)
93         .long   MTRRphysBase_MSR(6)
94         .long   MTRRphysMask_MSR(6)
95         .long   MTRRphysBase_MSR(7)
96         .long   MTRRphysMask_MSR(7)
97
98         .long   0x000 /* NULL, end of table */
99
100 clear_fixed_var_mtrr_out:
101         movl    $MTRRphysBase_MSR(0), %ecx
102         xorl    %edx, %edx
103         movl    $(CacheBase | MTRR_TYPE_WRBACK), %eax
104         wrmsr
105
106         movl    $MTRRphysMask_MSR(0), %ecx
107         /* This assumes we never access addresses above 2^36 in CAR. */
108         movl    $0x0000000f, %edx
109         movl    $(~(CacheSize - 1) | MTRRphysMaskValid), %eax
110         wrmsr
111
112 #if defined(CONFIG_TINY_BOOTBLOCK) && CONFIG_TINY_BOOTBLOCK
113 #define REAL_XIP_ROM_BASE AUTO_XIP_ROM_BASE
114 #else
115 #define REAL_XIP_ROM_BASE CONFIG_XIP_ROM_BASE
116 #endif
117
118         /*
119          * Enable write base caching so we can do execute in place (XIP)
120          * on the flash ROM.
121          */
122         movl    $MTRRphysBase_MSR(1), %ecx
123         xorl    %edx, %edx
124         /*
125          * IMPORTANT: The two lines below can _not_ be written like this:
126          *   movl $(REAL_XIP_ROM_BASE | MTRR_TYPE_WRBACK), %eax
127          * http://www.coreboot.org/pipermail/coreboot/2010-October/060855.html
128          */
129         movl    $REAL_XIP_ROM_BASE, %eax
130         orl     $MTRR_TYPE_WRBACK, %eax
131         wrmsr
132
133         movl    $MTRRphysMask_MSR(1), %ecx
134         movl    $0x0000000f, %edx
135         movl    $(~(CONFIG_XIP_ROM_SIZE - 1) | MTRRphysMaskValid), %eax
136         wrmsr
137
138         /* Set the default memory type and enable fixed and variable MTRRs. */
139         /* TODO: Or also enable fixed MTRRs? Bug in the code? */
140         movl    $MTRRdefType_MSR, %ecx
141         xorl    %edx, %edx
142         movl    $(MTRRdefTypeEn), %eax
143         wrmsr
144
145         /* Enable cache. */
146         movl    %cr0, %eax
147         andl    $(~((1 << 30) | (1 << 29))), %eax
148         movl    %eax, %cr0
149
150         /* Read the range with lodsl. */
151         cld
152         movl    $CacheBase, %esi
153         movl    %esi, %edi
154         movl    $(CacheSize >> 2), %ecx
155         rep     lodsl
156
157         movl    $CacheBase, %esi
158         movl    %esi, %edi
159         movl    $(CacheSize >> 2), %ecx
160
161         /*
162          * 0x5c5c5c5c is a memory test pattern.
163          * TODO: Check if everything works with the zero pattern as well.
164          */
165         /* xorl %eax, %eax */
166         xorl    $0x5c5c5c5c, %eax
167         rep     stosl
168
169 #ifdef CARTEST
170         movl    REAL_XIP_ROM_BASE, %esi
171         movl    %esi, %edi
172         movl    $(CONFIG_XIP_ROM_SIZE >> 2), %ecx
173         rep     lodsl
174 #endif
175
176         /*
177          * The key point of this CAR code is C7 cache does not turn into
178          * "no fill" mode, which is not compatible with general CAR code.
179          */
180
181         movl    $(CacheBase + CacheSize - 4), %eax
182         movl    %eax, %esp
183
184 #ifdef CARTEST
185 testok: 
186         post_code(0x40)
187         xorl    %edx, %edx
188         xorl    %eax, %eax
189         movl    $0x5c5c, %edx
190         pushl   %edx
191         pushl   %edx
192         pushl   %edx
193         pushl   %edx
194         pushl   %edx
195         popl    %esi
196         popl    %esi
197         popl    %eax
198         popl    %eax
199         popl    %eax
200         cmpl    %edx, %eax
201         jne     stackerr
202 #endif
203
204         /* Restore the BIST result. */
205         movl    %ebp, %eax
206
207         /* We need to set EBP? No need. */
208         movl    %esp, %ebp
209         pushl   %eax    /* BIST */
210         call    main
211
212         /*
213          * TODO: Backup stack in CACHE_AS_RAM into MMX and SSE and after we
214          *       get STACK up, we restore that. It is only needed if we
215          *       want to go back.
216          */
217
218         /* We don't need CAR from now on. */
219
220         /* Disable cache. */
221         movl    %cr0, %eax
222         orl     $(1 << 30), %eax
223         movl    %eax, %cr0
224
225         /* Set the default memory type and enable variable MTRRs. */
226         /* TODO: Or also enable fixed MTRRs? Bug in the code? */
227         movl    $MTRRdefType_MSR, %ecx
228         xorl    %edx, %edx
229         movl    $(MTRRdefTypeEn), %eax
230         wrmsr
231
232         /* Enable caching for CONFIG_RAMBASE..CONFIG_RAMTOP. */
233         movl    $MTRRphysBase_MSR(0), %ecx
234         xorl    %edx, %edx
235         movl    $(CONFIG_RAMBASE | MTRR_TYPE_WRBACK), %eax
236         wrmsr
237
238         movl    $MTRRphysMask_MSR(0), %ecx
239         movl    $0x0000000f, %edx       /* AMD 40 bit 0xff */
240         movl    $(~(CONFIG_RAMTOP - CONFIG_RAMBASE - 1) | MTRRphysMaskValid), %eax
241         wrmsr
242
243         /* Cache XIP_ROM_BASE-SIZE to speedup coreboot code. */
244         movl    $MTRRphysBase_MSR(1), %ecx
245         xorl    %edx, %edx
246         movl    $REAL_XIP_ROM_BASE, %eax
247         orl     $MTRR_TYPE_WRBACK, %eax
248         wrmsr
249
250         movl    $MTRRphysMask_MSR(1), %ecx
251         xorl    %edx, %edx
252         movl    $(~(CONFIG_XIP_ROM_SIZE - 1) | MTRRphysMaskValid), %eax
253         wrmsr
254
255         /* Enable cache. */
256         movl    %cr0, %eax
257         andl    $(~((1 << 30) | (1 << 29))), %eax
258         movl    %eax, %cr0
259         invd
260
261         /* Clear boot_complete flag. */
262         xorl    %ebp, %ebp
263 __main:
264         post_code(0x11)
265         cld                     /* Clear direction flag. */
266
267         movl    %ebp, %esi
268
269         movl    $ROMSTAGE_STACK, %esp
270         movl    %esp, %ebp
271         pushl   %esi
272         call    copy_and_run
273
274 .Lhlt:
275         post_code(0xee)
276         hlt
277         jmp     .Lhlt
278