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