f43febe0d4aea53ac0db8f42705b9164cff5e69d
[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         /* Set MMIO config space BAR. */
139         movl    $MSR_MCFG_BASE, %ecx
140         rdmsr
141         andl    $(~(0xfff00000 | (0xf << 2))), %eax
142         orl     $((CONFIG_MMCONF_BASE_ADDRESS & 0xfff00000), %eax
143         orl     $((8 << 2) | (1 << 0)), %eax
144         andl    $(~(0x0000ffff)), %edx
145         orl     $(CONFIG_MMCONF_BASE_ADDRESS >> 32), %edx
146         wrmsr
147 #endif
148
149 CAR_FAM10_out_post_errata:
150
151         /* Set MtrrFixDramModEn for clear fixed MTRR. */
152 enable_fixed_mtrr_dram_modify:
153         movl    $SYSCFG_MSR, %ecx
154         rdmsr
155         andl    $(~(SYSCFG_MSR_MtrrFixDramEn | SYSCFG_MSR_MtrrVarDramEn)), %eax
156         orl     $SYSCFG_MSR_MtrrFixDramModEn, %eax
157         wrmsr
158
159         /* Clear all MTRRs. */
160         xorl    %edx, %edx
161         movl    $fixed_mtrr_msr, %esi
162
163 clear_fixed_var_mtrr:
164         lodsl   (%esi), %eax
165         testl   %eax, %eax
166         jz      clear_fixed_var_mtrr_out
167
168         movl    %eax, %ecx
169         xorl    %eax, %eax
170         wrmsr
171
172         jmp     clear_fixed_var_mtrr
173 clear_fixed_var_mtrr_out:
174
175 /*
176  * 0x06 is the WB IO type for a given 4k segment.
177  * 0x1e is the MEM IO type for a given 4k segment (K10 and above).
178  * segs is the number of 4k segments in the area of the particular
179  *      register we want to use for CAR.
180  * reg is the register where the IO type should be stored.
181  */
182 .macro extractmask segs, reg
183 .if \segs <= 0
184         /*
185          * The xorl here is superfluous because at the point of first execution
186          * of this macro, %eax and %edx are cleared. Later invocations of this
187          * macro will have a monotonically increasing segs parameter.
188          */
189         xorl    \reg, \reg
190 .else
191         jmp_if_k8(1f)
192
193 .if \segs == 1
194         movl    $0x1e000000, \reg /* WB MEM type */
195 .elseif \segs == 2
196         movl    $0x1e1e0000, \reg /* WB MEM type */
197 .elseif \segs == 3
198         movl    $0x1e1e1e00, \reg /* WB MEM type */
199 .elseif \segs >= 4
200         movl    $0x1e1e1e1e, \reg /* WB MEM type */
201 .endif
202         jmp 2f
203 1:
204 .if \segs == 1
205         movl    $0x06000000, \reg /* WB IO type */
206 .elseif \segs == 2
207         movl    $0x06060000, \reg /* WB IO type */
208 .elseif \segs == 3
209         movl    $0x06060600, \reg /* WB IO type */
210 .elseif \segs >= 4
211         movl    $0x06060606, \reg /* WB IO type */
212 .endif
213 2:
214 .endif /* if \segs <= 0 */
215 .endm
216
217 /*
218  * carsize is the cache size in bytes we want to use for CAR.
219  * windowoffset is the 32k-aligned window into CAR size.
220  */
221 .macro simplemask carsize, windowoffset
222         .set gas_bug_workaround,(((\carsize - \windowoffset) / 0x1000) - 4)
223         extractmask gas_bug_workaround, %eax
224         .set gas_bug_workaround,(((\carsize - \windowoffset) / 0x1000))
225         extractmask gas_bug_workaround, %edx
226         /*
227          * Without the gas bug workaround, the entire macro would consist
228          * only of the two lines below:
229          *   extractmask (((\carsize - \windowoffset) / 0x1000) - 4), %eax
230          *   extractmask (((\carsize - \windowoffset) / 0x1000)), %edx
231          */
232 .endm
233
234 #if CacheSize > 0x10000
235 #error Invalid CAR size, must be at most 64k.
236 #endif
237 #if CacheSize < 0x1000
238 #error Invalid CAR size, must be at least 4k. This is a processor limitation.
239 #endif
240 #if (CacheSize & (0x1000 - 1))
241 #error Invalid CAR size, is not a multiple of 4k. This is a processor limitation.
242 #endif
243
244 #if CacheSize > 0x8000
245         /* Enable caching for 32K-64K using fixed MTRR. */
246         movl    $MTRRfix4K_C0000_MSR, %ecx
247         simplemask CacheSize, 0x8000
248         wrmsr
249 #endif
250
251         /* Enable caching for 0-32K using fixed MTRR. */
252         movl    $MTRRfix4K_C8000_MSR, %ecx
253         simplemask CacheSize, 0
254         wrmsr
255
256         /* Enable memory access for first MBs using top_mem. */
257         movl    $TOP_MEM, %ecx
258         xorl    %edx, %edx
259         movl    $(((CONFIG_RAMTOP) + TOP_MEM_MASK) & ~TOP_MEM_MASK) , %eax
260         wrmsr
261
262 #if defined(CONFIG_XIP_ROM_SIZE) && defined(CONFIG_XIP_ROM_BASE)
263
264 #if defined(CONFIG_TINY_BOOTBLOCK) && CONFIG_TINY_BOOTBLOCK
265 #define REAL_XIP_ROM_BASE AUTO_XIP_ROM_BASE
266 #else
267 #define REAL_XIP_ROM_BASE CONFIG_XIP_ROM_BASE
268 #endif
269
270         /* Enable write base caching so we can do execute in place (XIP)
271          * on the flash ROM.
272          */
273         movl    $MTRRphysBase_MSR(1), %ecx
274         xorl    %edx, %edx
275         movl    $REAL_XIP_ROM_BASE, %eax
276         orl     $MTRR_TYPE_WRBACK, %eax
277         wrmsr
278
279         movl    $MTRRphysMask_MSR(1), %ecx
280         movl    $0xff, %edx /* (1 << (CONFIG_CPU_ADDR_BITS - 32)) - 1 for K8 (CONFIG_CPU_ADDR_BITS = 40) */
281         jmp_if_k8(wbcache_post_fam10_setup)
282         movl    $0xffff, %edx /* (1 << (CONFIG_CPU_ADDR_BITS - 32)) - 1 for FAM10 (CONFIG_CPU_ADDR_BITS = 48) */
283 wbcache_post_fam10_setup:
284         movl    $(~(CONFIG_XIP_ROM_SIZE - 1) | 0x800), %eax
285         wrmsr
286 #endif /* CONFIG_XIP_ROM_SIZE && CONFIG_XIP_ROM_BASE */
287
288         /* Set the default memory type and enable fixed and variable MTRRs. */
289         movl    $MTRRdefType_MSR, %ecx
290         xorl    %edx, %edx
291         movl    $(MTRRdefTypeEn | MTRRdefTypeFixEn), %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
300         post_code(0xa1)
301
302         /* Enable cache. */
303         movl    %cr0, %eax
304         andl    $(~((1 << 30) | (1 << 29))), %eax
305         movl    %eax, %cr0
306
307         jmp_if_k8(fam10_end_part1)
308
309         /* So we need to check if it is BSP. */
310         movl    $0x1b, %ecx
311         rdmsr
312         bt      $8, %eax        /* BSP */
313         jnc     CAR_FAM10_ap
314 fam10_end_part1:
315
316         post_code(0xa2)
317
318         /* Read the range with lodsl. */
319         cld
320         movl    $CacheBase, %esi
321         movl    $(CacheSize >> 2), %ecx
322         rep     lodsl
323
324         /* Clear the range. */
325         movl    $CacheBase, %edi
326         movl    $(CacheSize >> 2), %ecx
327         xorl    %eax, %eax
328         rep     stosl
329
330         /* Set up the stack pointer. */
331         movl    $(CacheBase + CacheSize - GlobalVarSize), %eax
332         movl    %eax, %esp
333
334         post_code(0xa3)
335
336         jmp     CAR_FAM10_ap_out
337 CAR_FAM10_ap:
338         /*
339          * Need to set stack pointer for AP.
340          * It will be from:
341          *   CacheBase + (CacheSize - GlobalVarSize) / 2
342          *   - (NodeID << CoreIDbits + CoreID) * CacheSizeAPStack
343          * So need to get the NodeID and CoreID at first.
344          * If NB_CFG bit 54 is set just use initial APIC ID, otherwise need
345          * to reverse it.
346          */
347
348         /* Store our init detected. */
349         movl    %ebx, %esi
350
351         /* Get the coreid bits at first. */
352         movl    $0x80000008, %eax
353         cpuid
354         shrl    $12, %ecx
355         andl    $0x0f, %ecx
356         movl    %ecx, %edi
357
358         /* Get the initial APIC ID. */
359         movl    $1, %eax
360         cpuid
361         shrl    $24, %ebx
362
363         /* Get the nb cfg bit 54. */
364         movl    $0xc001001f, %ecx       /* NB_CFG_MSR */
365         rdmsr
366         movl    %edi, %ecx              /* CoreID bits */
367         bt      $(54 - 32), %edx
368         jc      roll_cfg
369         rolb    %cl, %bl
370 roll_cfg:
371
372         /* Calculate stack pointer. */
373         movl    $CacheSizeAPStack, %eax
374         mull    %ebx
375         movl    $(CacheBase + (CacheSize - GlobalVarSize) / 2), %esp
376         subl    %eax, %esp
377
378         /* Retrive init detected. */
379         movl    %esi, %ebx
380
381         post_code(0xa4)
382
383 CAR_FAM10_ap_out:
384
385         post_code(0xa5)
386
387         /* Disable SSE. */
388         movl    %cr4, %eax
389         andl    $~(3 << 9), %eax
390         movl    %eax, %cr4
391
392         /* Restore the BIST result. */
393         movl    %ebp, %eax
394
395         /* We need to set EBP? No need. */
396         movl    %esp, %ebp
397         pushl   %ebx            /* Init detected. */
398         pushl   %eax            /* BIST */
399         call    cache_as_ram_main
400         /* We will not go back. */
401
402         post_code(0xaf)         /* Should never see this POST code. */
403
404 fixed_mtrr_msr:
405         .long   0x250, 0x258, 0x259
406         .long   0x268, 0x269, 0x26A
407         .long   0x26B, 0x26C, 0x26D
408         .long   0x26E, 0x26F
409
410 var_mtrr_msr:
411         .long   0x200, 0x201, 0x202, 0x203
412         .long   0x204, 0x205, 0x206, 0x207
413         .long   0x208, 0x209, 0x20A, 0x20B
414         .long   0x20C, 0x20D, 0x20E, 0x20F
415
416 var_iorr_msr:
417         .long   0xC0010016, 0xC0010017, 0xC0010018, 0xC0010019
418
419 mem_top:
420         .long   0xC001001A, 0xC001001D
421         .long   0x000 /* NULL, end of table */
422
423 cache_as_ram_setup_out: