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