Revert r5902 to make code more readable again. At least three people like to
[coreboot.git] / src / cpu / intel / model_6ex / 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 #include <cpu/x86/stack.h>
22 #include <cpu/x86/mtrr.h>
23
24 #define CACHE_AS_RAM_SIZE CONFIG_DCACHE_RAM_SIZE
25 #define CACHE_AS_RAM_BASE CONFIG_DCACHE_RAM_BASE
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         /* Zero out all fixed range and variable range MTRRs. */
39         movl    $mtrr_table, %esi
40         movl    $((mtrr_table_end - mtrr_table) / 2), %edi
41         xorl    %eax, %eax
42         xorl    %edx, %edx
43 clear_mtrrs:
44         movw    (%esi), %bx
45         movzx   %bx, %ecx
46         wrmsr
47         add     $2, %esi
48         dec     %edi
49         jnz     clear_mtrrs
50
51         /* Configure the default memory type to uncacheable. */
52         movl    $MTRRdefType_MSR, %ecx
53         rdmsr
54         andl    $(~0x00000cff), %eax
55         wrmsr
56
57         /* Set Cache-as-RAM base address. */
58         movl    $(MTRRphysBase_MSR(0)), %ecx
59         movl    $(CACHE_AS_RAM_BASE | MTRR_TYPE_WRBACK), %eax
60         xorl    %edx, %edx
61         wrmsr
62
63         /* Set Cache-as-RAM mask. */
64         movl    $(MTRRphysMask_MSR(0)), %ecx
65         movl    $(~((CACHE_AS_RAM_SIZE - 1)) | (1 << 11)), %eax
66         movl    $0x0000000f, %edx
67         wrmsr
68
69         /* Enable MTRR. */
70         movl    $MTRRdefType_MSR, %ecx
71         rdmsr
72         orl     $(1 << 11), %eax
73         wrmsr
74
75         /* Enable L2 cache. */
76         movl    $0x11e, %ecx
77         rdmsr
78         orl     $(1 << 8), %eax
79         wrmsr
80
81         /* Enable cache (CR0.CD = 0, CR0.NW = 0). */
82         movl    %cr0, %eax
83         andl    $(~((1 << 30) | (1 << 29))), %eax
84         invd
85         movl    %eax, %cr0
86
87         /* Clear the cache memory reagion. */
88         movl    $CACHE_AS_RAM_BASE, %esi
89         movl    %esi, %edi
90         movl    $(CACHE_AS_RAM_SIZE / 4), %ecx
91         // movl $0x23322332, %eax
92         xorl    %eax, %eax
93         rep     stosl
94
95         /* Enable Cache-as-RAM mode by disabling cache. */
96         movl    %cr0, %eax
97         orl     $(1 << 30), %eax
98         movl    %eax, %cr0
99
100 #if defined(CONFIG_XIP_ROM_SIZE) && defined(CONFIG_XIP_ROM_BASE)
101         /* Enable cache for our code in Flash because we do XIP here */
102         movl    $MTRRphysBase_MSR(1), %ecx
103         xorl    %edx, %edx
104 #if defined(CONFIG_TINY_BOOTBLOCK) && CONFIG_TINY_BOOTBLOCK
105 #define REAL_XIP_ROM_BASE AUTO_XIP_ROM_BASE
106 #else
107 #define REAL_XIP_ROM_BASE CONFIG_XIP_ROM_BASE
108 #endif
109         /*
110          * IMPORTANT: The two lines below can _not_ be written like this:
111          *   movl $(REAL_XIP_ROM_BASE | MTRR_TYPE_WRBACK), %eax
112          * http://www.coreboot.org/pipermail/coreboot/2010-October/060855.html
113          */
114         movl    $REAL_XIP_ROM_BASE, %eax
115         orl     $MTRR_TYPE_WRBACK, %eax
116         wrmsr
117
118         movl    $MTRRphysMask_MSR(1), %ecx
119         movl    $0x0000000f, %edx
120         movl    $(~(CONFIG_XIP_ROM_SIZE - 1) | 0x800), %eax
121         wrmsr
122 #endif /* CONFIG_XIP_ROM_SIZE && CONFIG_XIP_ROM_BASE */
123
124         /* Enable cache. */
125         movl    %cr0, %eax
126         andl    $(~((1 << 30) | (1 << 29))), %eax
127         movl    %eax, %cr0
128
129         /* Set up the stack pointer. */
130 #if defined(CONFIG_USBDEBUG) && (CONFIG_USBDEBUG == 1)
131         /* Leave some space for the struct ehci_debug_info. */
132         movl    $(CACHE_AS_RAM_BASE + CACHE_AS_RAM_SIZE - 4 - 128), %eax
133 #else
134         movl    $(CACHE_AS_RAM_BASE + CACHE_AS_RAM_SIZE - 4), %eax
135 #endif
136         movl    %eax, %esp
137
138         /* Restore the BIST result. */
139         movl    %ebp, %eax
140         movl    %esp, %ebp
141         pushl   %eax
142
143         post_code(0x23)
144
145         /* Call romstage.c main function. */
146         call    main
147
148         post_code(0x2f)
149
150         post_code(0x30)
151
152         /* Disable cache. */
153         movl    %cr0, %eax
154         orl     $(1 << 30), %eax
155         movl    %eax, %cr0
156
157         post_code(0x31)
158
159         /* Disable MTRR. */
160         movl    $MTRRdefType_MSR, %ecx
161         rdmsr
162         andl    $(~(1 << 11)), %eax
163         wrmsr
164
165         post_code(0x31)
166
167         invd
168 #if 0
169         xorl    %eax, %eax
170         xorl    %edx, %edx
171         movl    $MTRRphysBase_MSR(0), %ecx
172         wrmsr
173         movl    $MTRRphysMask_MSR(0), %ecx
174         wrmsr
175         movl    $MTRRphysBase_MSR(1), %ecx
176         wrmsr
177         movl    $MTRRphysMask_MSR(1), %ecx
178         wrmsr
179 #endif
180
181         post_code(0x33)
182
183         /* Enable cache. */
184         movl    %cr0, %eax
185         andl    $~((1 << 30) | (1 << 29)), %eax
186         movl    %eax, %cr0
187
188         post_code(0x36)
189
190         /* Disable cache. */
191         movl    %cr0, %eax
192         orl     $(1 << 30), %eax
193         movl    %eax, %cr0
194
195         post_code(0x38)
196
197         /* Enable Write Back and Speculative Reads for the first 1MB. */
198         movl    $MTRRphysBase_MSR(0), %ecx
199         movl    $(0x00000000 | MTRR_TYPE_WRBACK), %eax
200         xorl    %edx, %edx
201         wrmsr
202         movl    $MTRRphysMask_MSR(0), %ecx
203         movl    $(~(1024 * 1024 - 1) | (1 << 11)), %eax
204         movl    $0x0000000f, %edx       // 36bit address space
205         wrmsr
206
207         post_code(0x39)
208
209         /* And enable cache again after setting MTRRs. */
210         movl    %cr0, %eax
211         andl    $~((1 << 30) | (1 << 29)), %eax
212         movl    %eax, %cr0
213
214         post_code(0x3a)
215
216         /* Enable MTRR. */
217         movl    $MTRRdefType_MSR, %ecx
218         rdmsr
219         orl     $(1 << 11), %eax
220         wrmsr
221
222         post_code(0x3b)
223
224         /* Invalidate the cache again. */
225         invd
226
227         post_code(0x3c)
228
229         /* Clear boot_complete flag. */
230         xorl    %ebp, %ebp
231 __main:
232         post_code(0x11)
233         cld                     /* Clear direction flag. */
234
235         movl    %ebp, %esi
236
237         movl    $ROMSTAGE_STACK, %esp
238         movl    %esp, %ebp
239         pushl   %esi
240         call    copy_and_run
241
242 .Lhlt:
243         post_code(0xee)
244         hlt
245         jmp     .Lhlt
246
247 mtrr_table:
248         /* Fixed MTRRs */
249         .word 0x250, 0x258, 0x259
250         .word 0x268, 0x269, 0x26A
251         .word 0x26B, 0x26C, 0x26D
252         .word 0x26E, 0x26F
253         /* Variable MTRRs */
254         .word 0x200, 0x201, 0x202, 0x203
255         .word 0x204, 0x205, 0x206, 0x207
256         .word 0x208, 0x209, 0x20A, 0x20B
257         .word 0x20C, 0x20D, 0x20E, 0x20F
258 mtrr_table_end:
259