Since some people disapprove of white space cleanups mixed in regular commits
[coreboot.git] / src / cpu / intel / 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 Carl-Daniel Hailfinger
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; version 2 of the License.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
24  */
25
26 /* We will use 4K bytes only */
27 /* disable HyperThreading is done by eswar*/
28 /* other's is the same as AMD except remove amd specific msr */
29
30 #define CacheSize CONFIG_DCACHE_RAM_SIZE
31 #define CacheBase (0xd0000 - CacheSize)
32
33 #include <cpu/x86/stack.h>
34 #include <cpu/x86/mtrr.h>
35
36         /* Save the BIST result */
37         movl    %eax, %ebp
38
39 CacheAsRam:
40         // Check whether the processor has HT capability
41         movl    $01, %eax
42         cpuid
43         btl     $28, %edx
44         jnc     NotHtProcessor
45         bswapl  %ebx
46         cmpb    $01, %bh
47         jbe     NotHtProcessor
48
49         // It is a HT processor; Send SIPI to the other logical processor
50         // within this processor so that the CAR related common system registers
51         // are programmed accordingly
52
53         // Use some register that is common to both logical processors
54         // as semaphore. Refer Appendix B, Vol.3
55         xorl    %eax, %eax
56         xorl    %edx, %edx
57         movl    $0x250, %ecx
58         wrmsr
59
60         // Figure out the logical AP's APIC ID; the following logic will work
61         // only for processors with 2 threads
62         // Refer to Vol 3. Table 7-1 for details about this logic
63         movl    $0xFEE00020, %esi
64         movl    (%esi), %ebx
65         andl    $0xFF000000, %ebx
66         bswapl  %ebx
67         btl     $0, %ebx
68         jnc     LogicalAP0
69         andb    $0xFE, %bl
70         jmp     Send_SIPI
71 LogicalAP0:
72         orb     $0x01, %bl
73 Send_SIPI:
74         bswapl  %ebx  // ebx - logical AP's APIC ID
75
76         // Fill up the IPI command registers in the Local APIC mapped to default address
77         // and issue SIPI to the other logical processor within this processor die.
78 Retry_SIPI:
79         movl    %ebx, %eax
80         movl    $0xFEE00310, %esi
81         movl    %eax, (%esi)
82
83         // SIPI vector - F900:0000
84         movl    $0x000006F9, %eax
85         movl    $0xFEE00300, %esi
86         movl    %eax, (%esi)
87
88         movl    $0x30, %ecx
89 SIPI_Delay:
90         pause
91         decl    %ecx
92         jnz     SIPI_Delay
93
94         movl    (%esi), %eax
95         andl    $0x00001000, %eax
96         jnz     Retry_SIPI
97
98         // Wait for the Logical AP to complete initialization
99 LogicalAP_SIPINotdone:
100         movl    $0x250, %ecx
101         rdmsr
102         orl     %eax, %eax
103         jz      LogicalAP_SIPINotdone
104
105 NotHtProcessor:
106
107 #if 1
108         /* Set the default memory type and enable fixed and variable MTRRs */
109         movl    $MTRRdefType_MSR, %ecx
110         xorl    %edx, %edx
111         /* Enable Variable and Fixed MTRRs */
112         movl    $0x00000c00, %eax
113         wrmsr
114 #endif
115
116         /* Clear all MTRRs */
117         xorl    %edx, %edx
118         movl    $fixed_mtrr_msr, %esi
119
120 clear_fixed_var_mtrr:
121         lodsl   (%esi), %eax
122         testl   %eax, %eax
123         jz      clear_fixed_var_mtrr_out
124
125         movl    %eax, %ecx
126         xorl    %eax, %eax
127         wrmsr
128
129         jmp     clear_fixed_var_mtrr
130
131 fixed_mtrr_msr:
132         .long   0x250, 0x258, 0x259
133         .long   0x268, 0x269, 0x26A
134         .long   0x26B, 0x26C, 0x26D
135         .long   0x26E, 0x26F
136 var_mtrr_msr:
137         .long   0x200, 0x201, 0x202, 0x203
138         .long   0x204, 0x205, 0x206, 0x207
139         .long   0x208, 0x209, 0x20A, 0x20B
140         .long   0x20C, 0x20D, 0x20E, 0x20F
141         .long   0x000 /* NULL, end of table */
142
143 clear_fixed_var_mtrr_out:
144
145 /* 0x06 is the WB IO type for a given 4k segment.
146  * segs is the number of 4k segments in the area of the particular
147  *   register we want to use for CAR.
148  * reg is the register where the IO type should be stored.
149  */
150 .macro extractmask segs, reg
151 .if \segs <= 0
152         /* The xorl here is superfluous because at the point of first execution
153          * of this macro, %eax and %edx are cleared. Later invocations of this
154          * macro will have a monotonically increasing segs parameter.
155          */
156         xorl \reg, \reg
157 .elseif \segs == 1
158         movl $0x06000000, \reg /* WB IO type */
159 .elseif \segs == 2
160         movl $0x06060000, \reg /* WB IO type */
161 .elseif \segs == 3
162         movl $0x06060600, \reg /* WB IO type */
163 .elseif \segs >= 4
164         movl $0x06060606, \reg /* WB IO type */
165 .endif
166 .endm
167
168 /* size is the cache size in bytes we want to use for CAR.
169  * windowoffset is the 32k-aligned window into CAR size
170  */
171 .macro simplemask carsize, windowoffset
172         .set gas_bug_workaround,(((\carsize - \windowoffset) / 0x1000) - 4)
173         extractmask gas_bug_workaround, %eax
174         .set gas_bug_workaround,(((\carsize - \windowoffset) / 0x1000))
175         extractmask gas_bug_workaround, %edx
176 /* Without the gas bug workaround, the entire macro would consist only of the
177  * two lines below.
178         extractmask (((\carsize - \windowoffset) / 0x1000) - 4), %eax
179         extractmask (((\carsize - \windowoffset) / 0x1000)), %edx
180  */
181 .endm
182
183 #if CacheSize > 0x10000
184 #error Invalid CAR size, must be at most 64k.
185 #endif
186 #if CacheSize < 0x1000
187 #error Invalid CAR size, must be at least 4k. This is a processor limitation.
188 #endif
189 #if (CacheSize & (0x1000 - 1))
190 #error Invalid CAR size, is not a multiple of 4k. This is a processor limitation.
191 #endif
192
193 #if CacheSize > 0x8000
194         /* enable caching for 32K-64K using fixed mtrr */
195         movl    $0x268, %ecx  /* fix4k_c0000*/
196         simplemask CacheSize, 0x8000
197         wrmsr
198 #endif
199
200         /* enable caching for 0-32K using fixed mtrr */
201         movl    $0x269, %ecx  /* fix4k_c8000*/
202         simplemask CacheSize, 0
203         wrmsr
204
205 #if defined(CONFIG_XIP_ROM_SIZE) && defined(CONFIG_XIP_ROM_BASE)
206 #if defined(CONFIG_TINY_BOOTBLOCK) && CONFIG_TINY_BOOTBLOCK
207 #define REAL_XIP_ROM_BASE AUTO_XIP_ROM_BASE
208 #else
209 #define REAL_XIP_ROM_BASE CONFIG_XIP_ROM_BASE
210 #endif
211         /* enable write base caching so we can do execute in place
212          * on the flash rom.
213          */
214         movl    $0x202, %ecx
215         xorl    %edx, %edx
216         movl    $REAL_XIP_ROM_BASE, %eax
217         orl     $MTRR_TYPE_WRBACK, %eax
218         wrmsr
219
220         movl    $0x203, %ecx
221         movl    $0x0000000f, %edx
222         movl    $(~(CONFIG_XIP_ROM_SIZE - 1) | 0x800), %eax
223         wrmsr
224 #endif /* CONFIG_XIP_ROM_SIZE && CONFIG_XIP_ROM_BASE */
225
226         /* enable cache */
227         movl    %cr0, %eax
228         andl    $0x9fffffff, %eax
229         movl    %eax, %cr0
230
231         /* Read the range with lodsl*/
232         movl    $CacheBase, %esi
233         cld
234         movl    $(CacheSize >> 2), %ecx
235         rep     lodsl
236
237         /* Clear the range */
238         movl    $CacheBase, %edi
239         movl    $(CacheSize >> 2), %ecx
240         xorl    %eax, %eax
241         rep     stosl
242
243
244 #if 0
245         /* check the cache as ram */
246         movl    $CacheBase, %esi
247         movl    $(CacheSize>>2), %ecx
248 .xin1:
249         movl    %esi, %eax
250         movl    %eax, (%esi)
251         decl    %ecx
252         je      .xout1
253         add     $4, %esi
254         jmp     .xin1
255 .xout1:
256
257         movl    $CacheBase, %esi
258 //        movl    $(CacheSize>>2), %ecx
259         movl $4, %ecx
260 .xin1x:
261         movl    %esi, %eax
262
263         movl    $0x4000, %edx
264         movb    %ah, %al
265 .testx1:
266         outb %al, $0x80
267         decl    %edx
268         jnz .testx1
269
270         movl    (%esi), %eax
271         cmpb 0xff, %al
272         je .xin2  /* dont show */
273
274         movl    $0x4000, %edx
275 .testx2:
276         outb %al, $0x80
277         decl    %edx
278         jnz .testx2
279
280 .xin2:  decl     %ecx
281         je      .xout1x
282         add     $4, %esi
283         jmp     .xin1x
284 .xout1x:
285
286 #endif
287
288         movl    $(CacheBase + CacheSize - 4), %eax
289         movl    %eax, %esp
290
291         /* Load a different set of data segments */
292 #if CONFIG_USE_INIT
293         movw    $CACHE_RAM_DATA_SEG, %ax
294         movw    %ax, %ds
295         movw    %ax, %es
296         movw    %ax, %ss
297 #endif
298
299 lout:
300         /* Restore the BIST result */
301         movl    %ebp, %eax
302
303         /* We need to set ebp ? No need */
304         movl    %esp, %ebp
305         pushl   %eax  /* bist */
306         call    main
307
308         /*
309         FIXME : backup stack in CACHE_AS_RAM into mmx and sse and after we get STACK up, we restore that.
310                 It is only needed if we want to go back
311         */
312
313         /* We don't need cache as ram for now on */
314         /* disable cache */
315         movl    %cr0, %eax
316         orl    $(0x1<<30),%eax
317         movl    %eax, %cr0
318
319         /* clear sth */
320         movl    $0x269, %ecx  /* fix4k_c8000*/
321         xorl    %edx, %edx
322         xorl    %eax, %eax
323         wrmsr
324
325 #if CONFIG_DCACHE_RAM_SIZE > 0x8000
326         movl    $0x268, %ecx  /* fix4k_c0000*/
327         wrmsr
328 #endif
329
330         /* Set the default memory type and disable fixed and enable variable MTRRs */
331         movl    $0x2ff, %ecx
332 //      movl    $MTRRdefType_MSR, %ecx
333         xorl    %edx, %edx
334         /* Enable Variable and Disable Fixed MTRRs */
335         movl    $0x00000800, %eax
336         wrmsr
337
338 #if defined(CLEAR_FIRST_1M_RAM)
339         /* enable caching for first 1M using variable mtrr */
340         movl    $0x200, %ecx
341         xorl    %edx, %edx
342         movl     $(0 | 1), %eax
343 //      movl     $(0 | MTRR_TYPE_WRCOMB), %eax
344         wrmsr
345
346         movl    $0x201, %ecx
347         movl    $0x0000000f, %edx /* AMD 40 bit 0xff*/
348         movl    $((~(( 0 + 0x100000) - 1)) | 0x800), %eax
349         wrmsr
350 #endif
351
352         /* enable cache */
353         movl    %cr0, %eax
354         andl    $0x9fffffff,%eax
355         movl    %eax, %cr0
356
357 #if defined(CLEAR_FIRST_1M_RAM)
358         /* clear the first 1M */
359         movl    $0x0, %edi
360         cld
361         movl    $(0x100000>>2), %ecx
362         xorl    %eax, %eax
363         rep     stosl
364
365         /* disable cache */
366         movl    %cr0, %eax
367         orl    $(0x1<<30),%eax
368         movl    %eax, %cr0
369
370         /* enable caching for first 1M using variable mtrr */
371         movl    $0x200, %ecx
372         xorl    %edx, %edx
373         movl     $(0 | 6), %eax
374 //      movl     $(0 | MTRR_TYPE_WRBACK), %eax
375         wrmsr
376
377         movl    $0x201, %ecx
378         movl    $0x0000000f, %edx /* AMD 40 bit 0xff*/
379         movl    $((~(( 0 + 0x100000) - 1)) | 0x800), %eax
380         wrmsr
381
382         /* enable cache */
383         movl    %cr0, %eax
384         andl    $0x9fffffff,%eax
385         movl    %eax, %cr0
386         invd
387
388         /* FIXME: I hope we don't need to change esp and ebp value here, so we
389          * can restore value from mmx sse back But the problem is the range is
390          * some io related, So don't go back
391          */
392 #endif
393
394         /* clear boot_complete flag */
395         xorl    %ebp, %ebp
396 __main:
397         post_code(0x11)
398         cld                     /* clear direction flag */
399
400         movl    %ebp, %esi
401
402         movl $ROMSTAGE_STACK, %esp
403         movl    %esp, %ebp
404         pushl %esi
405         call copy_and_run
406
407 .Lhlt:
408         post_code(0xee)
409         hlt
410         jmp     .Lhlt
411