Fix all warnings in the tree
[coreboot.git] / src / cpu / amd / car / cache_as_ram.inc
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2005-2007 Advanced Micro Devices, Inc.
5  * Copyright (C) 2008 Carl-Daniel Hailfinger
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 #define CacheSize CONFIG_DCACHE_RAM_SIZE
22 #define CacheBase (0xd0000 - CacheSize)
23
24 /* leave some space for global variable to pass to RAM stage */
25 #define GlobalVarSize CONFIG_DCACHE_RAM_GLOBAL_VAR_SIZE
26
27 /* for CAR with FAM10 */
28 #define CacheSizeAPStack 0x400 /* 1K */
29
30 #define MSR_FAM10      0xC001102A
31
32 #define jmp_if_k8(x)    comisd  %xmm2, %xmm1; jb x
33
34 #define CPUID_MASK      0x0ff00f00
35 #define CPUID_VAL_FAM10_ROTATED 0x0f000010
36
37 #include <cpu/x86/mtrr.h>
38 #include <cpu/amd/mtrr.h>
39 /*
40  * XMM map:
41  *   xmm1: cpu family
42  *   xmm2: fam10 comparison value
43  *   xmm3: backup ebx
44  */
45
46         /* Save the BIST result */
47         movl    %eax, %ebp
48
49         /* for normal part %ebx already contain cpu_init_detected from fallback call */
50
51 cache_as_ram_setup:
52         post_code(0xa0)
53
54         /* enable SSE */
55         movl %cr4, %eax
56         orl $(3<<9), %eax
57         movl %eax, %cr4
58
59         /* figure out cpu family */
60         cvtsi2sd %ebx, %xmm3
61         movl    $0x01, %eax
62         cpuid
63         /* base family is bits 8..11, extended family is bits 20..27 */
64         andl    $CPUID_MASK, %eax
65         /* reorder bits for easier comparison by value */
66         roll    $0x10, %eax
67         cvtsi2sd %eax, %xmm1
68         movl    $CPUID_VAL_FAM10_ROTATED, %eax
69         cvtsi2sd %eax, %xmm2
70         cvtsd2si %xmm3, %ebx
71
72         /* check if cpu_init_detected */
73         movl    $MTRRdefType_MSR, %ecx
74         rdmsr
75         andl    $(1 << 11), %eax
76         movl    %eax, %ebx      /* We store the status */
77
78         jmp_if_k8(CAR_FAM10_out_post_errata)
79
80         /* for GH, CAR need to set DRAM Base/Limit Registers to direct that to node0 */
81
82         /* Only BSP needed, for other nodes set during HT/memory init. */
83         /* So we need to check if it is BSP */
84         movl    $0x1b, %ecx
85         rdmsr
86         bt      $8, %eax /*BSC */
87         jnc     CAR_FAM10_out
88
89         /* Enable RT tables on BSP */
90         movl    $0x8000c06c, %eax
91         movw    $0xcf8, %dx
92         outl    %eax, %dx
93         addw    $4, %dx
94         inl     %dx, %eax
95         btr     $0, %eax
96         outl    %eax, %dx
97
98         /* Setup temporary DRAM map: [0,16M) bit 0-23 */
99         movl    $0x8000c144, %eax
100         movw    $0xcf8, %dx
101         outl    %eax, %dx
102         addw    $4, %dx
103         movl    $0, %eax
104         outl    %eax, %dx
105
106         movl    $0x8000c140, %eax
107         movw    $0xcf8, %dx
108         outl    %eax, %dx
109         addw    $4, %dx
110         movl    $3, %eax
111         outl    %eax, %dx
112
113 CAR_FAM10_out:
114
115         /* Errata 193: Disable clean copybacks to L3 cache to allow cached ROM.
116          * Re-enable it in after RAM is initialized and before CAR is disabled
117          */
118         movl    $0xc001102a, %ecx
119         rdmsr
120         bts     $15, %eax
121         wrmsr
122
123         /* Erratum 343, RevGuide for Fam10h, Pub#41322 Rev. 3.33 */
124
125         /* read-address has to be stored in the ecx register */
126         movl    $MSR_FAM10, %ecx
127
128         /* execute special read command for msr-register. Result is then in the EDX:EAX-registers (MSBs in EDX) */
129         rdmsr
130
131         /* Set bit 35 to 1 in EAX */
132         bts     $35, %eax
133
134         /* write back the modified register EDX:EAX to the MSR specified in ECX */
135         wrmsr
136
137         /* Erratum 343 end */
138
139 CAR_FAM10_out_post_errata:
140
141         /* Set MtrrFixDramModEn for clear fixed mtrr */
142 enable_fixed_mtrr_dram_modify:
143         movl    $SYSCFG_MSR, %ecx
144         rdmsr
145         andl    $(~(SYSCFG_MSR_MtrrFixDramEn | SYSCFG_MSR_MtrrVarDramEn)), %eax
146         orl     $SYSCFG_MSR_MtrrFixDramModEn, %eax
147         wrmsr
148
149         /* Clear all MTRRs */
150         xorl    %edx, %edx
151         movl    $fixed_mtrr_msr, %esi
152
153 clear_fixed_var_mtrr:
154         lodsl   (%esi), %eax
155         testl   %eax, %eax
156         jz      clear_fixed_var_mtrr_out
157
158         movl    %eax, %ecx
159         xorl    %eax, %eax
160         wrmsr
161
162         jmp     clear_fixed_var_mtrr
163 clear_fixed_var_mtrr_out:
164
165 /* 0x06 is the WB IO type for a given 4k segment.
166  * 0x1e is the MEM IO type for a given 4k segment (K10 and above).
167  * segs is the number of 4k segments in the area of the particular
168  *   register we want to use for CAR.
169  * reg is the register where the IO type should be stored.
170  */
171 .macro extractmask segs, reg
172 .if \segs <= 0
173         /* The xorl here is superfluous because at the point of first execution
174          * of this macro, %eax and %edx are cleared. Later invocations of this
175          * macro will have a monotonically increasing segs parameter.
176          */
177         xorl \reg, \reg
178 .else
179         jmp_if_k8(1f)
180
181 .if \segs == 1
182         movl $0x1e000000, \reg /* WB MEM type */
183 .elseif \segs == 2
184         movl $0x1e1e0000, \reg /* WB MEM type */
185 .elseif \segs == 3
186         movl $0x1e1e1e00, \reg /* WB MEM type */
187 .elseif \segs >= 4
188         movl $0x1e1e1e1e, \reg /* WB MEM type */
189 .endif
190         jmp 2f
191 1:
192 .if \segs == 1
193         movl $0x06000000, \reg /* WB IO type */
194 .elseif \segs == 2
195         movl $0x06060000, \reg /* WB IO type */
196 .elseif \segs == 3
197         movl $0x06060600, \reg /* WB IO type */
198 .elseif \segs >= 4
199         movl $0x06060606, \reg /* WB IO type */
200 .endif
201 2:
202 .endif /* if \segs <= 0 */
203 .endm
204
205 /* size is the cache size in bytes we want to use for CAR.
206  * windowoffset is the 32k-aligned window into CAR size
207  */
208 .macro simplemask carsize, windowoffset
209         .set gas_bug_workaround,(((\carsize - \windowoffset) / 0x1000) - 4)
210         extractmask gas_bug_workaround, %eax
211         .set gas_bug_workaround,(((\carsize - \windowoffset) / 0x1000))
212         extractmask gas_bug_workaround, %edx
213 /* Without the gas bug workaround, the entire macro would consist only of the
214  * two lines below.
215         extractmask (((\carsize - \windowoffset) / 0x1000) - 4), %eax
216         extractmask (((\carsize - \windowoffset) / 0x1000)), %edx
217  */
218 .endm
219
220 #if CacheSize > 0x10000
221 #error Invalid CAR size, must be at most 64k.
222 #endif
223 #if CacheSize < 0x1000
224 #error Invalid CAR size, must be at least 4k. This is a processor limitation.
225 #endif
226 #if (CacheSize & (0x1000 - 1))
227 #error Invalid CAR size, is not a multiple of 4k. This is a processor limitation.
228 #endif
229
230 #if CacheSize > 0x8000
231         /* enable caching for 32K-64K using fixed mtrr */
232         movl    $0x268, %ecx  /* fix4k_c0000*/
233         simplemask CacheSize, 0x8000
234         wrmsr
235 #endif
236
237         /* enable caching for 0-32K using fixed mtrr */
238         movl    $0x269, %ecx  /* fix4k_c8000*/
239         simplemask CacheSize, 0
240         wrmsr
241
242         /* enable memory access for first MBs using top_mem */
243         movl    $TOP_MEM, %ecx
244         xorl    %edx, %edx
245         movl    $(((CONFIG_RAMTOP) + TOP_MEM_MASK) & ~TOP_MEM_MASK) , %eax
246         wrmsr
247
248 #if defined(CONFIG_XIP_ROM_SIZE) && defined(CONFIG_XIP_ROM_BASE)
249         /* enable write base caching so we can do execute in place
250          * on the flash rom.
251          */
252         movl    $0x202, %ecx
253         xorl    %edx, %edx
254
255 #if defined(CONFIG_TINY_BOOTBLOCK) && CONFIG_TINY_BOOTBLOCK
256 #define REAL_XIP_ROM_BASE AUTO_XIP_ROM_BASE
257 #else
258 #define REAL_XIP_ROM_BASE CONFIG_XIP_ROM_BASE
259 #endif
260         movl    $REAL_XIP_ROM_BASE, %eax
261         orl     $MTRR_TYPE_WRBACK, %eax
262         wrmsr
263
264         movl    $0x203, %ecx
265         movl    $0xff, %edx /* (1 << (CONFIG_CPU_ADDR_BITS - 32)) - 1 for K8 (CONFIG_CPU_ADDR_BITS = 40) */
266         jmp_if_k8(wbcache_post_fam10_setup)
267         movl    $0xffff, %edx /* (1 << (CONFIG_CPU_ADDR_BITS - 32)) - 1 for FAM10 (CONFIG_CPU_ADDR_BITS = 48) */
268 wbcache_post_fam10_setup:
269         movl    $(~(CONFIG_XIP_ROM_SIZE - 1) | 0x800), %eax
270         wrmsr
271 #endif /* CONFIG_XIP_ROM_SIZE && CONFIG_XIP_ROM_BASE */
272
273         /* Set the default memory type and enable fixed and variable MTRRs */
274         movl    $MTRRdefType_MSR, %ecx
275         xorl    %edx, %edx
276         /* Enable Variable and Fixed MTRRs */
277         movl    $0x00000c00, %eax
278         wrmsr
279
280         /* Enable the MTRRs and IORRs in SYSCFG */
281         movl    $SYSCFG_MSR, %ecx
282         rdmsr
283         orl     $(SYSCFG_MSR_MtrrVarDramEn | SYSCFG_MSR_MtrrFixDramEn), %eax
284         wrmsr
285
286         post_code(0xa1)
287
288         /* enable cache */
289         movl    %cr0, %eax
290         andl    $0x9fffffff, %eax
291         movl    %eax, %cr0
292
293         jmp_if_k8(fam10_end_part1)
294
295         /* So we need to check if it is BSP */
296         movl    $0x1b, %ecx
297         rdmsr
298         bt      $8, %eax /*BSC */
299         jnc     CAR_FAM10_ap
300 fam10_end_part1:
301
302         post_code(0xa2)
303
304         /* Read the range with lodsl*/
305         cld
306         movl    $CacheBase, %esi
307         movl    $(CacheSize >> 2), %ecx
308         rep     lodsl
309
310         /* Clear the range */
311         movl    $CacheBase, %edi
312         movl    $(CacheSize >> 2), %ecx
313         xorl    %eax, %eax
314         rep     stosl
315
316         /* set up the stack pointer */
317         movl    $(CacheBase + CacheSize - GlobalVarSize), %eax
318         movl    %eax, %esp
319
320         post_code(0xa3)
321
322         jmp     CAR_FAM10_ap_out
323 CAR_FAM10_ap:
324         /* need to set stack pointer for AP */
325         /* it will be from CacheBase + (CacheSize - GlobalVarSize)/2 - (NodeID<<CoreIDbits + CoreID) * CacheSizeAPStack*/
326         /* So need to get the NodeID and CoreID at first */
327         /* If NB_CFG bit 54 is set just use initial apicid, otherwise need to reverse it */
328
329         /* store our init detected */
330         movl    %ebx, %esi
331
332         /* get the coreid bits at first */
333         movl    $0x80000008, %eax
334         cpuid
335         shrl    $12, %ecx
336         andl    $0x0f, %ecx
337         movl    %ecx, %edi
338
339         /* get the initial apic id */
340         movl    $1, %eax
341         cpuid
342         shrl    $24, %ebx
343
344         /* get the nb cfg bit 54 */
345         movl    $0xc001001f, %ecx /* NB_CFG_MSR */
346         rdmsr
347         movl    %edi, %ecx      /* CoreID bits */
348         bt      $(54-32), %edx
349         jc      roll_cfg
350         rolb    %cl, %bl
351 roll_cfg:
352
353         /* calculate stack pointer */
354         movl    $CacheSizeAPStack, %eax
355         mull    %ebx
356         movl    $(CacheBase + (CacheSize - GlobalVarSize)/2), %esp
357         subl    %eax, %esp
358
359         /* retrive init detected */
360         movl    %esi, %ebx
361
362         post_code(0xa4)
363
364 CAR_FAM10_ap_out:
365
366         post_code(0xa5)
367
368         /* disable SSE */
369         movl %cr4, %eax
370         andl $~(3<<9), %eax
371         movl %eax, %cr4
372
373         /* Restore the BIST result */
374         movl    %ebp, %eax
375
376         /* We need to set ebp ? No need */
377         movl    %esp, %ebp
378         pushl   %ebx  /* init detected */
379         pushl   %eax  /* bist */
380         call    cache_as_ram_main
381         /* We will not go back */
382
383         post_code(0xaf)         /* Should never see this postcode */
384
385 fixed_mtrr_msr:
386         .long   0x250, 0x258, 0x259
387         .long   0x268, 0x269, 0x26A
388         .long   0x26B, 0x26C, 0x26D
389         .long   0x26E, 0x26F
390 var_mtrr_msr:
391         .long   0x200, 0x201, 0x202, 0x203
392         .long   0x204, 0x205, 0x206, 0x207
393         .long   0x208, 0x209, 0x20A, 0x20B
394         .long   0x20C, 0x20D, 0x20E, 0x20F
395 var_iorr_msr:
396         .long   0xC0010016, 0xC0010017, 0xC0010018, 0xC0010019
397 mem_top:
398         .long   0xC001001A, 0xC001001D
399         .long   0x000 /* NULL, end of table */
400
401 cache_as_ram_setup_out: