26cdec3fbe69c1f1951e549aa3dd0c39bff04fc7
[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 #include <cpu/x86/mtrr.h>
22 #include <cpu/amd/mtrr.h>
23
24 #define CacheSize               CONFIG_DCACHE_RAM_SIZE
25 #define CacheBase               (0xd0000 - CacheSize)
26
27 /* Leave some space for global variable to pass to RAM stage. */
28 #define GlobalVarSize           CONFIG_DCACHE_RAM_GLOBAL_VAR_SIZE
29
30 /* For CAR with Fam10h. */
31 #define CacheSizeAPStack        0x400 /* 1K */
32
33 #define MSR_MCFG_BASE           0xC0010058
34 #define MSR_FAM10               0xC001102A
35
36 #define jmp_if_k8(x)            comisd  %xmm2, %xmm1; jb x
37
38 #define CPUID_MASK              0x0ff00f00
39 #define CPUID_VAL_FAM10_ROTATED 0x0f000010
40
41 /*
42  * XMM map:
43  *   xmm1: CPU family
44  *   xmm2: Fam10h comparison value
45  *   xmm3: Backup EBX
46  */
47
48         /* Save the BIST result. */
49         movl    %eax, %ebp
50
51         /*
52          * For normal part %ebx already contain cpu_init_detected
53          * from fallback call.
54          */
55
56 cache_as_ram_setup:
57         post_code(0xa0)
58
59         /* Enable SSE. */
60         movl    %cr4, %eax
61         orl     $(3 << 9), %eax
62         movl    %eax, %cr4
63
64         /* Figure out the CPU family. */
65         cvtsi2sd %ebx, %xmm3
66         movl    $0x01, %eax
67         cpuid
68         /* Base family is bits 8..11, extended family is bits 20..27. */
69         andl    $CPUID_MASK, %eax
70         /* Reorder bits for easier comparison by value. */
71         roll    $0x10, %eax
72         cvtsi2sd %eax, %xmm1
73         movl    $CPUID_VAL_FAM10_ROTATED, %eax
74         cvtsi2sd %eax, %xmm2
75         cvtsd2si %xmm3, %ebx
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         /*
86          * For GH, CAR need to set DRAM Base/Limit registers to direct that
87          * to node0.
88          * Only BSP needed, for other nodes set during HT/memory init.
89          * So we need to check if it is BSP.
90          */
91         movl    $0x1b, %ecx
92         rdmsr
93         bt      $8, %eax        /* BSP */
94         jnc     CAR_FAM10_out
95
96         /* Enable RT tables on BSP. */
97         movl    $0x8000c06c, %eax
98         movw    $0xcf8, %dx
99         outl    %eax, %dx
100         addw    $4, %dx
101         inl     %dx, %eax
102         btr     $0, %eax
103         outl    %eax, %dx
104
105         /* Setup temporary DRAM map: [0,16M) bit 0-23. */
106         movl    $0x8000c144, %eax
107         movw    $0xcf8, %dx
108         outl    %eax, %dx
109         addw    $4, %dx
110         movl    $0, %eax
111         outl    %eax, %dx
112
113         movl    $0x8000c140, %eax
114         movw    $0xcf8, %dx
115         outl    %eax, %dx
116         addw    $4, %dx
117         movl    $3, %eax
118         outl    %eax, %dx
119
120 CAR_FAM10_out:
121
122         /*
123          * Errata 193: Disable clean copybacks to L3 cache to allow cached ROM.
124          * Re-enable it in after RAM is initialized and before CAR is disabled.
125          */
126         movl    $MSR_FAM10, %ecx
127         rdmsr
128         bts     $15, %eax
129         wrmsr
130
131         /* Erratum 343, RevGuide for Fam10h, Pub#41322 Rev. 3.33 */
132         movl    $MSR_FAM10, %ecx
133         rdmsr
134         bts     $35-32, %edx    /* Set bit 35 in EDX:EAX (bit 3 in EDX). */
135         wrmsr
136
137 #if CONFIG_MMCONF_SUPPORT
138    #if (CONFIG_MMCONF_BASE_ADDRESS > 0xFFFFFFFF)
139    #error "MMCONF_BASE_ADDRESS too big"
140    #elif (CONFIG_MMCONF_BASE_ADDRESS & 0xFFFFF)
141    #error "MMCONF_BASE_ADDRESS not 1MB aligned"
142    #endif
143         movl    $0, %edx
144         movl    $((CONFIG_MMCONF_BASE_ADDRESS) | (1 << 0)), %eax
145    #if (CONFIG_MMCONF_BUS_NUMBER == 1)
146    #elif (CONFIG_MMCONF_BUS_NUMBER == 2)
147         orl     $(1 << 2), %eax
148    #elif (CONFIG_MMCONF_BUS_NUMBER == 4)
149         orl     $(2 << 2), %eax 
150    #elif (CONFIG_MMCONF_BUS_NUMBER == 8)
151         orl     $(3 << 2), %eax
152    #elif (CONFIG_MMCONF_BUS_NUMBER == 16)
153         orl     $(4 << 2), %eax
154    #elif (CONFIG_MMCONF_BUS_NUMBER == 32)
155         orl     $(5 << 2), %eax
156    #elif (CONFIG_MMCONF_BUS_NUMBER == 64)
157         orl     $(6 << 2), %eax
158    #elif (CONFIG_MMCONF_BUS_NUMBER == 128)
159         orl     $(7 << 2), %eax
160    #elif (CONFIG_MMCONF_BUS_NUMBER == 256)
161         orl     $(8 << 2), %eax
162    #else
163         #error "bad MMCONF_BUS_NUMBER value"
164    #endif
165         movl    $(0xc0010058), %ecx
166         wrmsr
167 #endif
168
169 CAR_FAM10_out_post_errata:
170
171         /* Set MtrrFixDramModEn for clear fixed MTRR. */
172 enable_fixed_mtrr_dram_modify:
173         movl    $SYSCFG_MSR, %ecx
174         rdmsr
175         andl    $(~(SYSCFG_MSR_MtrrFixDramEn | SYSCFG_MSR_MtrrVarDramEn)), %eax
176         orl     $SYSCFG_MSR_MtrrFixDramModEn, %eax
177         wrmsr
178
179         /* Clear all MTRRs. */
180         xorl    %edx, %edx
181         movl    $all_mtrr_msrs, %esi
182
183 clear_fixed_var_mtrr:
184         lodsl   (%esi), %eax
185         testl   %eax, %eax
186         jz      clear_fixed_var_mtrr_out
187
188         movl    %eax, %ecx
189         xorl    %eax, %eax
190         wrmsr
191
192         jmp     clear_fixed_var_mtrr
193 clear_fixed_var_mtrr_out:
194
195 /*
196  * 0x06 is the WB IO type for a given 4k segment.
197  * 0x1e is the MEM IO type for a given 4k segment (K10 and above).
198  * segs is the number of 4k segments in the area of the particular
199  *      register we want to use for CAR.
200  * reg is the register where the IO type should be stored.
201  */
202 .macro extractmask segs, reg
203 .if \segs <= 0
204         /*
205          * The xorl here is superfluous because at the point of first execution
206          * of this macro, %eax and %edx are cleared. Later invocations of this
207          * macro will have a monotonically increasing segs parameter.
208          */
209         xorl    \reg, \reg
210 .else
211         jmp_if_k8(1f)
212
213 .if \segs == 1
214         movl    $0x1e000000, \reg /* WB MEM type */
215 .elseif \segs == 2
216         movl    $0x1e1e0000, \reg /* WB MEM type */
217 .elseif \segs == 3
218         movl    $0x1e1e1e00, \reg /* WB MEM type */
219 .elseif \segs >= 4
220         movl    $0x1e1e1e1e, \reg /* WB MEM type */
221 .endif
222         jmp 2f
223 1:
224 .if \segs == 1
225         movl    $0x06000000, \reg /* WB IO type */
226 .elseif \segs == 2
227         movl    $0x06060000, \reg /* WB IO type */
228 .elseif \segs == 3
229         movl    $0x06060600, \reg /* WB IO type */
230 .elseif \segs >= 4
231         movl    $0x06060606, \reg /* WB IO type */
232 .endif
233 2:
234 .endif /* if \segs <= 0 */
235 .endm
236
237 /*
238  * carsize is the cache size in bytes we want to use for CAR.
239  * windowoffset is the 32k-aligned window into CAR size.
240  */
241 .macro simplemask carsize, windowoffset
242         .set gas_bug_workaround,(((\carsize - \windowoffset) / 0x1000) - 4)
243         extractmask gas_bug_workaround, %eax
244         .set gas_bug_workaround,(((\carsize - \windowoffset) / 0x1000))
245         extractmask gas_bug_workaround, %edx
246         /*
247          * Without the gas bug workaround, the entire macro would consist
248          * only of the two lines below:
249          *   extractmask (((\carsize - \windowoffset) / 0x1000) - 4), %eax
250          *   extractmask (((\carsize - \windowoffset) / 0x1000)), %edx
251          */
252 .endm
253
254 #if CacheSize > 0x10000
255 #error Invalid CAR size, must be at most 64k.
256 #endif
257 #if CacheSize < 0x1000
258 #error Invalid CAR size, must be at least 4k. This is a processor limitation.
259 #endif
260 #if (CacheSize & (0x1000 - 1))
261 #error Invalid CAR size, is not a multiple of 4k. This is a processor limitation.
262 #endif
263
264 #if CacheSize > 0x8000
265         /* Enable caching for 32K-64K using fixed MTRR. */
266         movl    $MTRRfix4K_C0000_MSR, %ecx
267         simplemask CacheSize, 0x8000
268         wrmsr
269 #endif
270
271         /* Enable caching for 0-32K using fixed MTRR. */
272         movl    $MTRRfix4K_C8000_MSR, %ecx
273         simplemask CacheSize, 0
274         wrmsr
275
276         /* Enable memory access for first MBs using top_mem. */
277         movl    $TOP_MEM, %ecx
278         xorl    %edx, %edx
279         movl    $(((CONFIG_RAMTOP) + TOP_MEM_MASK) & ~TOP_MEM_MASK) , %eax
280         wrmsr
281
282 #if defined(CONFIG_XIP_ROM_SIZE) && defined(CONFIG_XIP_ROM_BASE)
283
284 #if defined(CONFIG_TINY_BOOTBLOCK) && CONFIG_TINY_BOOTBLOCK
285 #define REAL_XIP_ROM_BASE AUTO_XIP_ROM_BASE
286 #else
287 #define REAL_XIP_ROM_BASE CONFIG_XIP_ROM_BASE
288 #endif
289
290         /* Enable write base caching so we can do execute in place (XIP)
291          * on the flash ROM.
292          */
293         movl    $MTRRphysBase_MSR(1), %ecx
294         xorl    %edx, %edx
295         /*
296          * IMPORTANT: The two lines below can _not_ be written like this:
297          *   movl $(REAL_XIP_ROM_BASE | MTRR_TYPE_WRPROT), %eax
298          * http://www.coreboot.org/pipermail/coreboot/2010-October/060855.html
299          */
300         movl    $REAL_XIP_ROM_BASE, %eax
301         orl     $MTRR_TYPE_WRPROT, %eax
302         wrmsr
303
304         movl    $MTRRphysMask_MSR(1), %ecx
305         movl    $0xff, %edx /* (1 << (CONFIG_CPU_ADDR_BITS - 32)) - 1 for K8 (CONFIG_CPU_ADDR_BITS = 40) */
306         jmp_if_k8(wbcache_post_fam10_setup)
307         movl    $0xffff, %edx /* (1 << (CONFIG_CPU_ADDR_BITS - 32)) - 1 for FAM10 (CONFIG_CPU_ADDR_BITS = 48) */
308 wbcache_post_fam10_setup:
309         movl    $(~(CONFIG_XIP_ROM_SIZE - 1) | 0x800), %eax
310         wrmsr
311 #endif /* CONFIG_XIP_ROM_SIZE && CONFIG_XIP_ROM_BASE */
312
313         /* Set the default memory type and enable fixed and variable MTRRs. */
314         movl    $MTRRdefType_MSR, %ecx
315         xorl    %edx, %edx
316         movl    $(MTRRdefTypeEn | MTRRdefTypeFixEn), %eax
317         wrmsr
318
319         /* Enable the MTRRs and IORRs in SYSCFG. */
320         movl    $SYSCFG_MSR, %ecx
321         rdmsr
322         orl     $(SYSCFG_MSR_MtrrVarDramEn | SYSCFG_MSR_MtrrFixDramEn), %eax
323         wrmsr
324
325         post_code(0xa1)
326
327         /* Enable cache. */
328         movl    %cr0, %eax
329         andl    $(~((1 << 30) | (1 << 29))), %eax
330         movl    %eax, %cr0
331
332         jmp_if_k8(fam10_end_part1)
333
334         /* So we need to check if it is BSP. */
335         movl    $0x1b, %ecx
336         rdmsr
337         bt      $8, %eax        /* BSP */
338         jnc     CAR_FAM10_ap
339 fam10_end_part1:
340
341         post_code(0xa2)
342
343         /* Read the range with lodsl. */
344         cld
345         movl    $CacheBase, %esi
346         movl    $(CacheSize >> 2), %ecx
347         rep     lodsl
348
349         /* Clear the range. */
350         movl    $CacheBase, %edi
351         movl    $(CacheSize >> 2), %ecx
352         xorl    %eax, %eax
353         rep     stosl
354
355         /* Set up the stack pointer. */
356         movl    $(CacheBase + CacheSize - GlobalVarSize), %eax
357         movl    %eax, %esp
358
359         post_code(0xa3)
360
361         jmp     CAR_FAM10_ap_out
362 CAR_FAM10_ap:
363         /*
364          * Need to set stack pointer for AP.
365          * It will be from:
366          *   CacheBase + (CacheSize - GlobalVarSize) / 2
367          *   - (NodeID << CoreIDbits + CoreID) * CacheSizeAPStack
368          * So need to get the NodeID and CoreID at first.
369          * If NB_CFG bit 54 is set just use initial APIC ID, otherwise need
370          * to reverse it.
371          */
372
373         /* Store our init detected. */
374         movl    %ebx, %esi
375
376         /* Get the coreid bits at first. */
377         movl    $0x80000008, %eax
378         cpuid
379         shrl    $12, %ecx
380         andl    $0x0f, %ecx
381         movl    %ecx, %edi
382
383         /* Get the initial APIC ID. */
384         movl    $1, %eax
385         cpuid
386         shrl    $24, %ebx
387
388         /* Get the nb cfg bit 54. */
389         movl    $0xc001001f, %ecx       /* NB_CFG_MSR */
390         rdmsr
391         movl    %edi, %ecx              /* CoreID bits */
392         bt      $(54 - 32), %edx
393         jc      roll_cfg
394         rolb    %cl, %bl
395 roll_cfg:
396
397         /* Calculate stack pointer. */
398         movl    $CacheSizeAPStack, %eax
399         mull    %ebx
400         movl    $(CacheBase + (CacheSize - GlobalVarSize) / 2), %esp
401         subl    %eax, %esp
402
403         /* Retrive init detected. */
404         movl    %esi, %ebx
405
406         post_code(0xa4)
407
408 CAR_FAM10_ap_out:
409
410         post_code(0xa5)
411
412         /* Disable SSE. */
413         movl    %cr4, %eax
414         andl    $~(3 << 9), %eax
415         movl    %eax, %cr4
416
417         /* Restore the BIST result. */
418         movl    %ebp, %eax
419
420         /* We need to set EBP? No need. */
421         movl    %esp, %ebp
422         pushl   %ebx            /* Init detected. */
423         pushl   %eax            /* BIST */
424         call    cache_as_ram_main
425         /* We will not go back. */
426
427         post_code(0xaf)         /* Should never see this POST code. */
428
429 all_mtrr_msrs:
430         /* fixed MTRR MSRs */
431         .long   MTRRfix64K_00000_MSR
432         .long   MTRRfix16K_80000_MSR
433         .long   MTRRfix16K_A0000_MSR
434         .long   MTRRfix4K_C0000_MSR
435         .long   MTRRfix4K_C8000_MSR
436         .long   MTRRfix4K_D0000_MSR
437         .long   MTRRfix4K_D8000_MSR
438         .long   MTRRfix4K_E0000_MSR
439         .long   MTRRfix4K_E8000_MSR
440         .long   MTRRfix4K_F0000_MSR
441         .long   MTRRfix4K_F8000_MSR
442
443         /* var MTRR MSRs */
444         .long   MTRRphysBase_MSR(0)
445         .long   MTRRphysMask_MSR(0)
446         .long   MTRRphysBase_MSR(1)
447         .long   MTRRphysMask_MSR(1)
448         .long   MTRRphysBase_MSR(2)
449         .long   MTRRphysMask_MSR(2)
450         .long   MTRRphysBase_MSR(3)
451         .long   MTRRphysMask_MSR(3)
452         .long   MTRRphysBase_MSR(4)
453         .long   MTRRphysMask_MSR(4)
454         .long   MTRRphysBase_MSR(5)
455         .long   MTRRphysMask_MSR(5)
456         .long   MTRRphysBase_MSR(6)
457         .long   MTRRphysMask_MSR(6)
458         .long   MTRRphysBase_MSR(7)
459         .long   MTRRphysMask_MSR(7)
460
461         /* Variable IORR MTRR MSRs */
462         .long   IORRBase_MSR(0)
463         .long   IORRMask_MSR(0)
464         .long   IORRBase_MSR(1)
465         .long   IORRMask_MSR(1)
466
467         /* Top of memory MTRR MSRs */
468         .long   TOP_MEM_MSR
469         .long   TOP_MEM2_MSR
470
471         .long   0x000 /* NULL, end of table */
472
473 cache_as_ram_setup_out: