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