CONFIG_MMCONF_SUPPORT is always defined. Fix build.
[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_MCFG_BASE  0xC0010058
31 #define MSR_FAM10      0xC001102A
32
33 #define jmp_if_k8(x)    comisd  %xmm2, %xmm1; jb x
34
35 #define CPUID_MASK      0x0ff00f00
36 #define CPUID_VAL_FAM10_ROTATED 0x0f000010
37
38 #include <cpu/x86/mtrr.h>
39 #include <cpu/amd/mtrr.h>
40 /*
41  * XMM map:
42  *   xmm1: cpu family
43  *   xmm2: fam10 comparison value
44  *   xmm3: backup ebx
45  */
46
47         /* Save the BIST result */
48         movl    %eax, %ebp
49
50         /* for normal part %ebx already contain cpu_init_detected from fallback call */
51
52 cache_as_ram_setup:
53         post_code(0xa0)
54
55         /* enable SSE */
56         movl %cr4, %eax
57         orl $(3<<9), %eax
58         movl %eax, %cr4
59
60         /* figure out cpu family */
61         cvtsi2sd %ebx, %xmm3
62         movl    $0x01, %eax
63         cpuid
64         /* base family is bits 8..11, extended family is bits 20..27 */
65         andl    $CPUID_MASK, %eax
66         /* reorder bits for easier comparison by value */
67         roll    $0x10, %eax
68         cvtsi2sd %eax, %xmm1
69         movl    $CPUID_VAL_FAM10_ROTATED, %eax
70         cvtsi2sd %eax, %xmm2
71         cvtsd2si %xmm3, %ebx
72
73         /* check if cpu_init_detected */
74         movl    $MTRRdefType_MSR, %ecx
75         rdmsr
76         andl    $(1 << 11), %eax
77         movl    %eax, %ebx      /* We store the status */
78
79         jmp_if_k8(CAR_FAM10_out_post_errata)
80
81         /* for GH, CAR need to set DRAM Base/Limit Registers to direct that to node0 */
82
83         /* Only BSP needed, for other nodes set during HT/memory init. */
84         /* So we need to check if it is BSP */
85         movl    $0x1b, %ecx
86         rdmsr
87         bt      $8, %eax /*BSC */
88         jnc     CAR_FAM10_out
89
90         /* Enable RT tables on BSP */
91         movl    $0x8000c06c, %eax
92         movw    $0xcf8, %dx
93         outl    %eax, %dx
94         addw    $4, %dx
95         inl     %dx, %eax
96         btr     $0, %eax
97         outl    %eax, %dx
98
99         /* Setup temporary DRAM map: [0,16M) bit 0-23 */
100         movl    $0x8000c144, %eax
101         movw    $0xcf8, %dx
102         outl    %eax, %dx
103         addw    $4, %dx
104         movl    $0, %eax
105         outl    %eax, %dx
106
107         movl    $0x8000c140, %eax
108         movw    $0xcf8, %dx
109         outl    %eax, %dx
110         addw    $4, %dx
111         movl    $3, %eax
112         outl    %eax, %dx
113
114 CAR_FAM10_out:
115
116         /* Errata 193: Disable clean copybacks to L3 cache to allow cached ROM.
117          * Re-enable it in after RAM is initialized and before CAR is disabled
118          */
119         movl    $MSR_FAM10, %ecx
120         rdmsr
121         bts     $15, %eax
122         wrmsr
123
124         /* Erratum 343, RevGuide for Fam10h, Pub#41322 Rev. 3.33 */
125
126         /* read-address has to be stored in the ecx register */
127         movl    $MSR_FAM10, %ecx
128
129         /* execute special read command for msr-register. Result is then in the EDX:EAX-registers (MSBs in EDX) */
130         rdmsr
131
132         /* Set bit 35 to 1 in EAX */
133         bts     $35, %eax
134
135         /* write back the modified register EDX:EAX to the MSR specified in ECX */
136         wrmsr
137
138         /* Erratum 343 end */
139
140 #if CONFIG_MMCONF_SUPPORT
141         /* Set MMIO Config space BAR */
142         movl    $MSR_MCFG_BASE, %ecx
143         rdmsr
144
145         andl    $(~(0xfff00000 | (0xf << 2))), %eax
146         orl     $((CONFIG_MMCONF_BASE_ADDRESS & 0xfff00000) | (8 << 2) | (1 << 0)), %eax
147         andl    $(~(0x0000ffff)), %edx
148         orl     $(CONFIG_MMCONF_BASE_ADDRESS >> 32), %edx
149
150         wrmsr
151 #endif
152
153 CAR_FAM10_out_post_errata:
154
155         /* Set MtrrFixDramModEn for clear fixed mtrr */
156 enable_fixed_mtrr_dram_modify:
157         movl    $SYSCFG_MSR, %ecx
158         rdmsr
159         andl    $(~(SYSCFG_MSR_MtrrFixDramEn | SYSCFG_MSR_MtrrVarDramEn)), %eax
160         orl     $SYSCFG_MSR_MtrrFixDramModEn, %eax
161         wrmsr
162
163         /* Clear all MTRRs */
164         xorl    %edx, %edx
165         movl    $fixed_mtrr_msr, %esi
166
167 clear_fixed_var_mtrr:
168         lodsl   (%esi), %eax
169         testl   %eax, %eax
170         jz      clear_fixed_var_mtrr_out
171
172         movl    %eax, %ecx
173         xorl    %eax, %eax
174         wrmsr
175
176         jmp     clear_fixed_var_mtrr
177 clear_fixed_var_mtrr_out:
178
179 /* 0x06 is the WB IO type for a given 4k segment.
180  * 0x1e is the MEM IO type for a given 4k segment (K10 and above).
181  * segs is the number of 4k segments in the area of the particular
182  *   register we want to use for CAR.
183  * reg is the register where the IO type should be stored.
184  */
185 .macro extractmask segs, reg
186 .if \segs <= 0
187         /* The xorl here is superfluous because at the point of first execution
188          * of this macro, %eax and %edx are cleared. Later invocations of this
189          * macro will have a monotonically increasing segs parameter.
190          */
191         xorl \reg, \reg
192 .else
193         jmp_if_k8(1f)
194
195 .if \segs == 1
196         movl $0x1e000000, \reg /* WB MEM type */
197 .elseif \segs == 2
198         movl $0x1e1e0000, \reg /* WB MEM type */
199 .elseif \segs == 3
200         movl $0x1e1e1e00, \reg /* WB MEM type */
201 .elseif \segs >= 4
202         movl $0x1e1e1e1e, \reg /* WB MEM type */
203 .endif
204         jmp 2f
205 1:
206 .if \segs == 1
207         movl $0x06000000, \reg /* WB IO type */
208 .elseif \segs == 2
209         movl $0x06060000, \reg /* WB IO type */
210 .elseif \segs == 3
211         movl $0x06060600, \reg /* WB IO type */
212 .elseif \segs >= 4
213         movl $0x06060606, \reg /* WB IO type */
214 .endif
215 2:
216 .endif /* if \segs <= 0 */
217 .endm
218
219 /* size is the cache size in bytes we want to use for CAR.
220  * windowoffset is the 32k-aligned window into CAR size
221  */
222 .macro simplemask carsize, windowoffset
223         .set gas_bug_workaround,(((\carsize - \windowoffset) / 0x1000) - 4)
224         extractmask gas_bug_workaround, %eax
225         .set gas_bug_workaround,(((\carsize - \windowoffset) / 0x1000))
226         extractmask gas_bug_workaround, %edx
227 /* Without the gas bug workaround, the entire macro would consist only of the
228  * 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    $0x268, %ecx  /* fix4k_c0000*/
247         simplemask CacheSize, 0x8000
248         wrmsr
249 #endif
250
251         /* enable caching for 0-32K using fixed mtrr */
252         movl    $0x269, %ecx  /* fix4k_c8000*/
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         /* enable write base caching so we can do execute in place
264          * on the flash rom.
265          */
266         movl    $0x202, %ecx
267         xorl    %edx, %edx
268
269 #if defined(CONFIG_TINY_BOOTBLOCK) && CONFIG_TINY_BOOTBLOCK
270 #define REAL_XIP_ROM_BASE AUTO_XIP_ROM_BASE
271 #else
272 #define REAL_XIP_ROM_BASE CONFIG_XIP_ROM_BASE
273 #endif
274         movl    $REAL_XIP_ROM_BASE, %eax
275         orl     $MTRR_TYPE_WRBACK, %eax
276         wrmsr
277
278         movl    $0x203, %ecx
279         movl    $0xff, %edx /* (1 << (CONFIG_CPU_ADDR_BITS - 32)) - 1 for K8 (CONFIG_CPU_ADDR_BITS = 40) */
280         jmp_if_k8(wbcache_post_fam10_setup)
281         movl    $0xffff, %edx /* (1 << (CONFIG_CPU_ADDR_BITS - 32)) - 1 for FAM10 (CONFIG_CPU_ADDR_BITS = 48) */
282 wbcache_post_fam10_setup:
283         movl    $(~(CONFIG_XIP_ROM_SIZE - 1) | 0x800), %eax
284         wrmsr
285 #endif /* CONFIG_XIP_ROM_SIZE && CONFIG_XIP_ROM_BASE */
286
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
300         post_code(0xa1)
301
302         /* enable cache */
303         movl    %cr0, %eax
304         andl    $0x9fffffff, %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 /*BSC */
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         /* need to set stack pointer for AP */
339         /* it will be from CacheBase + (CacheSize - GlobalVarSize)/2 - (NodeID<<CoreIDbits + CoreID) * CacheSizeAPStack*/
340         /* So need to get the NodeID and CoreID at first */
341         /* If NB_CFG bit 54 is set just use initial apicid, otherwise need to reverse it */
342
343         /* store our init detected */
344         movl    %ebx, %esi
345
346         /* get the coreid bits at first */
347         movl    $0x80000008, %eax
348         cpuid
349         shrl    $12, %ecx
350         andl    $0x0f, %ecx
351         movl    %ecx, %edi
352
353         /* get the initial apic id */
354         movl    $1, %eax
355         cpuid
356         shrl    $24, %ebx
357
358         /* get the nb cfg bit 54 */
359         movl    $0xc001001f, %ecx /* NB_CFG_MSR */
360         rdmsr
361         movl    %edi, %ecx      /* CoreID bits */
362         bt      $(54-32), %edx
363         jc      roll_cfg
364         rolb    %cl, %bl
365 roll_cfg:
366
367         /* calculate stack pointer */
368         movl    $CacheSizeAPStack, %eax
369         mull    %ebx
370         movl    $(CacheBase + (CacheSize - GlobalVarSize)/2), %esp
371         subl    %eax, %esp
372
373         /* retrive init detected */
374         movl    %esi, %ebx
375
376         post_code(0xa4)
377
378 CAR_FAM10_ap_out:
379
380         post_code(0xa5)
381
382         /* disable SSE */
383         movl %cr4, %eax
384         andl $~(3<<9), %eax
385         movl %eax, %cr4
386
387         /* Restore the BIST result */
388         movl    %ebp, %eax
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 postcode */
398
399 fixed_mtrr_msr:
400         .long   0x250, 0x258, 0x259
401         .long   0x268, 0x269, 0x26A
402         .long   0x26B, 0x26C, 0x26D
403         .long   0x26E, 0x26F
404 var_mtrr_msr:
405         .long   0x200, 0x201, 0x202, 0x203
406         .long   0x204, 0x205, 0x206, 0x207
407         .long   0x208, 0x209, 0x20A, 0x20B
408         .long   0x20C, 0x20D, 0x20E, 0x20F
409 var_iorr_msr:
410         .long   0xC0010016, 0xC0010017, 0xC0010018, 0xC0010019
411 mem_top:
412         .long   0xC001001A, 0xC001001D
413         .long   0x000 /* NULL, end of table */
414
415 cache_as_ram_setup_out: