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