Factor out common CAR asm snippets.
[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    $fixed_mtrr_msr, %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 fixed_mtrr_msr:
64         .long   0x250, 0x258, 0x259
65         .long   0x268, 0x269, 0x26A
66         .long   0x26B, 0x26C, 0x26D
67         .long   0x26E, 0x26F
68
69 var_mtrr_msr:
70         .long   0x200, 0x201, 0x202, 0x203
71         .long   0x204, 0x205, 0x206, 0x207
72         .long   0x208, 0x209, 0x20A, 0x20B
73         .long   0x20C, 0x20D, 0x20E, 0x20F
74         .long   0x000 /* NULL, end of table */
75
76 clear_fixed_var_mtrr_out:
77         movl    $MTRRphysBase_MSR(0), %ecx
78         xorl    %edx, %edx
79         movl    $(CacheBase | MTRR_TYPE_WRBACK), %eax
80         wrmsr
81
82         movl    $MTRRphysMask_MSR(0), %ecx
83         /* This assumes we never access addresses above 2^36 in CAR. */
84         movl    $0x0000000f, %edx
85         movl    $(~(CacheSize - 1) | 0x800), %eax
86         wrmsr
87
88 #if defined(CONFIG_TINY_BOOTBLOCK) && CONFIG_TINY_BOOTBLOCK
89 #define REAL_XIP_ROM_BASE AUTO_XIP_ROM_BASE
90 #else
91 #define REAL_XIP_ROM_BASE CONFIG_XIP_ROM_BASE
92 #endif
93
94         /*
95          * Enable write base caching so we can do execute in place (XIP)
96          * on the flash ROM.
97          */
98         movl    $MTRRphysBase_MSR(1), %ecx
99         xorl    %edx, %edx
100         movl    $REAL_XIP_ROM_BASE, %eax
101         orl     $MTRR_TYPE_WRBACK, %eax
102         wrmsr
103
104         movl    $MTRRphysMask_MSR(1), %ecx
105         movl    $0x0000000f, %edx
106         movl    $(~(CONFIG_XIP_ROM_SIZE - 1) | 0x800), %eax
107         wrmsr
108
109         /* Set the default memory type and enable fixed and variable MTRRs. */
110         /* TODO: Or also enable fixed MTRRs? Bug in the code? */
111         movl    $MTRRdefType_MSR, %ecx
112         xorl    %edx, %edx
113         movl    $(MTRRdefTypeEn), %eax
114         wrmsr
115
116         enable_cache()
117
118         /* Read the range with lodsl. */
119         cld
120         movl    $CacheBase, %esi
121         movl    %esi, %edi
122         movl    $(CacheSize >> 2), %ecx
123         rep     lodsl
124
125         movl    $CacheBase, %esi
126         movl    %esi, %edi
127         movl    $(CacheSize >> 2), %ecx
128
129         /*
130          * 0x5c5c5c5c is a memory test pattern.
131          * TODO: Check if everything works with the zero pattern as well.
132          */
133         /* xorl %eax, %eax */
134         xorl    $0x5c5c5c5c, %eax
135         rep     stosl
136
137 #ifdef CARTEST
138         movl    REAL_XIP_ROM_BASE, %esi
139         movl    %esi, %edi
140         movl    $(CONFIG_XIP_ROM_SIZE >> 2), %ecx
141         rep     lodsl
142 #endif
143
144         /*
145          * The key point of this CAR code is C7 cache does not turn into
146          * "no fill" mode, which is not compatible with general CAR code.
147          */
148
149         movl    $(CacheBase + CacheSize - 4), %eax
150         movl    %eax, %esp
151
152 #ifdef CARTEST
153 testok: 
154         post_code(0x40)
155         xorl    %edx, %edx
156         xorl    %eax, %eax
157         movl    $0x5c5c, %edx
158         pushl   %edx
159         pushl   %edx
160         pushl   %edx
161         pushl   %edx
162         pushl   %edx
163         popl    %esi
164         popl    %esi
165         popl    %eax
166         popl    %eax
167         popl    %eax
168         cmpl    %edx, %eax
169         jne     stackerr
170 #endif
171
172         restore_bist_result()
173
174         /* We need to set EBP? No need. */
175         movl    %esp, %ebp
176         pushl   %eax    /* BIST */
177         call    main
178
179         /*
180          * TODO: Backup stack in CACHE_AS_RAM into MMX and SSE and after we
181          *       get STACK up, we restore that. It is only needed if we
182          *       want to go back.
183          */
184
185         /* We don't need CAR from now on. */
186
187         disable_cache()
188
189         /* Set the default memory type and enable variable MTRRs. */
190         /* TODO: Or also enable fixed MTRRs? Bug in the code? */
191         movl    $MTRRdefType_MSR, %ecx
192         xorl    %edx, %edx
193         movl    $(MTRRdefTypeEn), %eax
194         wrmsr
195
196         /* Enable caching for first 1M using variable MTRR. */
197         movl    $MTRRphysBase_MSR(0), %ecx
198         xorl    %edx, %edx
199         movl    $(0 | MTRR_TYPE_WRBACK), %eax
200         wrmsr
201
202         /*
203          * Enable cache for 0-7ffff, 80000-9ffff, e0000-fffff;
204          * If 1M cacheable, then when S3 resume, there is stange color on
205          * screen for 2 sec. Suppose problem of a0000-dfffff and cache.
206          * And in x86_setup_fixed_mtrrs()(mtrr.c), 0-256M is set cacheable.
207          */
208
209         movl    $MTRRphysMask_MSR(0), %ecx
210         movl    $0x0000000f, %edx       /* AMD 40 bit 0xff */
211         movl    $((~((0 + 0x80000) - 1)) | 0x800), %eax
212         wrmsr
213
214         movl    $MTRRphysBase_MSR(1), %ecx
215         xorl    %edx, %edx
216         movl    $(0x80000 | MTRR_TYPE_WRBACK), %eax
217         wrmsr
218
219         movl    $MTRRphysMask_MSR(1), %ecx
220         movl    $0x0000000f, %edx       /* AMD 40 bit 0xff */
221         movl    $((~((0 + 0x20000) - 1)) | 0x800), %eax
222         wrmsr
223
224         movl    $MTRRphysBase_MSR(2), %ecx
225         xorl    %edx, %edx
226         movl    $(0xc0000 | MTRR_TYPE_WRBACK), %eax
227         wrmsr
228
229         movl    $MTRRphysMask_MSR(2), %ecx
230         movl    $0x0000000f, %edx       /* AMD 40 bit 0xff */
231         movl    $((~(( 0 + 0x40000) - 1)) | 0x800), %eax
232         wrmsr
233
234         /* Cache XIP_ROM_BASE-SIZE to speedup coreboot code. */
235         movl    $MTRRphysBase_MSR(3), %ecx
236         xorl    %edx, %edx
237         movl    $REAL_XIP_ROM_BASE, %eax
238         orl     $MTRR_TYPE_WRBACK, %eax
239         wrmsr
240
241         movl    $MTRRphysMask_MSR(3), %ecx
242         xorl    %edx, %edx
243         movl    $(~(CONFIG_XIP_ROM_SIZE - 1) | 0x800), %eax
244         wrmsr
245
246         enable_cache()
247         invd
248
249         /* Clear boot_complete flag. */
250         xorl    %ebp, %ebp
251 __main:
252         post_code(0x11)
253         cld                     /* Clear direction flag. */
254
255         movl    %ebp, %esi
256
257         movl    $ROMSTAGE_STACK, %esp
258         movl    %esp, %ebp
259         pushl   %esi
260         call    copy_and_run
261
262 .Lhlt:
263         post_code(0xee)
264         hlt
265         jmp     .Lhlt
266