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