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