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