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