This patch introduces 4k CAR size granularity for the AMD x86 CAR code.
[coreboot.git] / src / cpu / amd / car / cache_as_ram.inc
1 /*
2  * This file is part of the LinuxBIOS 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 DCACHE_RAM_SIZE
22 #define CacheBase (0xd0000 - CacheSize)
23
24 /* leave some space for global variable to pass to RAM stage */
25 #define GlobalVarSize DCACHE_RAM_GLOBAL_VAR_SIZE
26
27 #if CAR_FAM10 == 1
28 #define CacheSizeAPStack 0x400 /* 1K */
29 #endif
30
31 #include <cpu/x86/mtrr.h>
32 #include <cpu/amd/mtrr.h>
33
34         /* Save the BIST result */
35         movl    %eax, %ebp
36
37         /*for normal part %ebx already contain cpu_init_detected from fallback call */
38
39 cache_as_ram_setup:
40
41         movb    $0xA0, %al
42         outb    %al, $0x80
43
44         /* hope we can skip the double set for normal part */
45 #if ((HAVE_FAILOVER_BOOT == 1) && (USE_FAILOVER_IMAGE == 1)) || ((HAVE_FAILOVER_BOOT == 0) && (USE_FALLBACK_IMAGE == 1))
46
47         /* check if cpu_init_detected */
48         movl    $MTRRdefType_MSR, %ecx
49         rdmsr
50         andl    $(1 << 11), %eax
51         movl    %eax, %ebx      /* We store the status */
52
53 #if CAR_FAM10 == 1
54         /* for GH, CAR need to set DRAM Base/Limit Registers to direct that to node0 */
55
56         /* Only BSP needed, for other nodes set during HT/memory init. */
57         /* So we need to check if it is BSP */
58         movl    $0x1b, %ecx
59         rdmsr
60         bt      $8, %eax /*BSC */
61         jnc     CAR_FAM10_out
62
63         /* Enable RT tables on BSP */
64         movl    $0x8000c06c, %eax
65         movw    $0xcf8, %dx
66         outl    %eax, %dx
67         addw    $4, %dx
68         inl     %dx, %eax
69         btr     $0, %eax
70         outl    %eax, %dx
71
72         /* Setup temporary DRAM map: [0,16M) bit 0-23 */
73         movl    $0x8000c144, %eax
74         movw    $0xcf8, %dx
75         outl    %eax, %dx
76         addw    $4, %dx
77         movl    $0, %eax
78         outl    %eax, %dx
79
80         movl    $0x8000c140, %eax
81         movw    $0xcf8, %dx
82         outl    %eax, %dx
83         addw    $4, %dx
84         movl    $3, %eax
85         outl    %eax, %dx
86
87 CAR_FAM10_out:
88
89 #endif
90
91 #if CAR_FAM10 == 1
92         /* Errata 193: Disable clean copybacks to L3 cache to allow cached ROM.
93            Re-enable it in after RAM is initialized and before CAR is disabled */
94         movl    $0xc001102a, %ecx
95         rdmsr
96         bts     $15, %eax
97         wrmsr
98 #endif
99
100         /* Set MtrrFixDramModEn for clear fixed mtrr */
101 enable_fixed_mtrr_dram_modify:
102         movl    $SYSCFG_MSR, %ecx
103         rdmsr
104         andl    $(~(SYSCFG_MSR_MtrrFixDramEn | SYSCFG_MSR_MtrrVarDramEn)), %eax
105         orl     $SYSCFG_MSR_MtrrFixDramModEn, %eax
106         wrmsr
107
108         /* Clear all MTRRs */
109         xorl    %edx, %edx
110         movl    $fixed_mtrr_msr, %esi
111
112 clear_fixed_var_mtrr:
113         lodsl   (%esi), %eax
114         testl   %eax, %eax
115         jz              clear_fixed_var_mtrr_out
116
117         movl    %eax, %ecx
118         xorl    %eax, %eax
119         wrmsr
120
121         jmp             clear_fixed_var_mtrr
122 clear_fixed_var_mtrr_out:
123
124 /* 0x06 is the WB IO type for a given 4k segment.
125  * 0x1e is the MEM IO type for a given 4k segment (K10 and above).
126  * segs is the number of 4k segments in the area of the particular
127  *   register we want to use for CAR.
128  * reg is the register where the IO type should be stored.
129  */
130 .macro extractmask segs, reg
131 .if \segs <= 0
132         /* The xorl here is superfluous because at the point of first execution
133          * of this macro, %eax and %edx are cleared. Later invocations of this
134          * macro will have a monotonically increasing segs parameter.
135          */
136         xorl \reg, \reg
137 #if CAR_FAM10 == 1
138 .elseif \segs == 1
139         movl $0x1e000000, \reg /* WB MEM type */
140 .elseif \segs == 2
141         movl $0x1e1e0000, \reg /* WB MEM type */
142 .elseif \segs == 3
143         movl $0x1e1e1e00, \reg /* WB MEM type */
144 .elseif \segs >= 4
145         movl $0x1e1e1e1e, \reg /* WB MEM type */
146 #else
147 .elseif \segs == 1
148         movl $0x06000000, \reg /* WB IO type */
149 .elseif \segs == 2
150         movl $0x06060000, \reg /* WB IO type */
151 .elseif \segs == 3
152         movl $0x06060600, \reg /* WB IO type */
153 .elseif \segs >= 4
154         movl $0x06060606, \reg /* WB IO type */
155 #endif
156 .endif
157 .endm
158
159 /* size is the cache size in bytes we want to use for CAR.
160  * windowoffset is the 32k-aligned window into CAR size
161  */
162 .macro simplemask carsize, windowoffset
163         extractmask (((\carsize - \windowoffset) / 0x1000) - 4), %eax
164         extractmask (((\carsize - \windowoffset) / 0x1000)), %edx
165 .endm
166
167 #if CacheSize > 0x10000
168 #error Invalid CAR size, must be at most 64k.
169 #endif
170 #if CacheSize < 0x1000
171 #error Invalid CAR size, must be at least 4k. This is a processor limitation.
172 #endif
173 #if (CacheSize & (0x1000 - 1))
174 #error Invalid CAR size, is not a multiple of 4k. This is a processor limitation.
175 #endif
176
177 #if CacheSize > 0x8000 
178         /* enable caching for 32K-64K using fixed mtrr */
179         movl    $0x268, %ecx  /* fix4k_c0000*/
180         simplemask CacheSize, 0x8000
181         wrmsr
182 #endif
183
184         /* enable caching for 0-32K using fixed mtrr */
185         movl    $0x269, %ecx  /* fix4k_c8000*/
186         simplemask CacheSize, 0
187         wrmsr
188
189         /* enable memory access for first MBs using top_mem */
190         movl    $TOP_MEM, %ecx
191         xorl    %edx, %edx
192         movl    $(((CONFIG_LB_MEM_TOPK << 10) + TOP_MEM_MASK) & ~TOP_MEM_MASK) , %eax
193         wrmsr
194 #endif /*  USE_FAILOVER_IMAGE == 1*/
195
196
197 #if ((HAVE_FAILOVER_BOOT == 1) && (USE_FAILOVER_IMAGE == 0)) || ((HAVE_FAILOVER_BOOT == 0) && (USE_FALLBACK_IMAGE == 0))
198         /* disable cache */
199         movl    %cr0, %eax
200         orl             $(1 << 30),%eax
201         movl    %eax, %cr0
202
203 #endif
204
205 #if defined(XIP_ROM_SIZE) && defined(XIP_ROM_BASE)
206         /* enable write base caching so we can do execute in place
207          * on the flash rom.
208          */
209         movl    $0x202, %ecx
210         xorl    %edx, %edx
211         movl    $(XIP_ROM_BASE | MTRR_TYPE_WRBACK), %eax
212         wrmsr
213
214         movl    $0x203, %ecx
215         movl    $((1 << (CPU_ADDR_BITS - 32)) - 1), %edx  /* AMD 40 bit for K8, 48 bit for GH */
216         movl    $(~(XIP_ROM_SIZE - 1) | 0x800), %eax
217         wrmsr
218 #endif /* XIP_ROM_SIZE && XIP_ROM_BASE */
219
220 #if ((HAVE_FAILOVER_BOOT == 1) && (USE_FAILOVER_IMAGE == 1)) || ((HAVE_FAILOVER_BOOT == 0) && (USE_FALLBACK_IMAGE == 1))
221         /* Set the default memory type and enable fixed and variable MTRRs */
222         movl    $MTRRdefType_MSR, %ecx
223         xorl    %edx, %edx
224         /* Enable Variable and Fixed MTRRs */
225         movl    $0x00000c00, %eax
226         wrmsr
227
228         /* Enable the MTRRs and IORRs in SYSCFG */
229         movl    $SYSCFG_MSR, %ecx
230         rdmsr
231         orl             $(SYSCFG_MSR_MtrrVarDramEn | SYSCFG_MSR_MtrrFixDramEn), %eax
232         wrmsr
233 #endif
234
235         movb    $0xA1, %al
236         outb    %al, $0x80
237
238         /* enable cache */
239         movl    %cr0, %eax
240         andl    $0x9fffffff, %eax
241         movl    %eax, %cr0
242
243
244 #if CAR_FAM10 == 1
245         /* So we need to check if it is BSP */
246         movl    $0x1b, %ecx
247         rdmsr
248         bt      $8, %eax /*BSC */
249         jnc     CAR_FAM10_ap
250 #endif
251
252         movb    $0xA2, %al
253         outb    %al, $0x80
254
255 #if ((HAVE_FAILOVER_BOOT == 1) && (USE_FAILOVER_IMAGE == 1)) || ((HAVE_FAILOVER_BOOT == 0) && (USE_FALLBACK_IMAGE == 1))
256                 /* Read the range with lodsl*/
257         cld
258         movl    $CacheBase, %esi
259         movl    $(CacheSize >> 2), %ecx
260         rep             lodsl
261         /* Clear the range */
262         movl    $CacheBase, %edi
263         movl    $(CacheSize >> 2), %ecx
264         xorl    %eax, %eax
265         rep             stosl
266
267 #endif /*USE_FAILOVER_IMAGE == 1*/
268
269         /* set up the stack pointer */
270         movl    $(CacheBase + CacheSize - GlobalVarSize), %eax
271         movl    %eax, %esp
272
273         movb    $0xA3, %al
274         outb    %al, $0x80
275
276 #if CAR_FAM10 == 1
277
278         jmp     CAR_FAM10_ap_out
279 CAR_FAM10_ap:
280         /* need to set stack pointer for AP */
281         /* it will be from CacheBase + (CacheSize - GlobalVarSize)/2 - (NodeID<<CoreIDbits + CoreID) * CacheSizeAPStack*/
282         /* So need to get the NodeID and CoreID at first */
283         /* If NB_CFG bit 54 is set just use initial apicid, otherwise need to reverse it */
284
285         /* store our init detected */
286         movl    %ebx, %esi
287
288         /* get the coreid bits at first */
289         movl    $0x80000008, %eax
290         cpuid
291         shrl    $12, %ecx
292         andl    $0x0f, %ecx
293         movl    %ecx, %edi
294
295         /* get the initial apic id */
296         movl    $1, %eax
297         cpuid
298         shrl    $24, %ebx
299
300         /* get the nb cfg bit 54 */
301         movl    $0xc001001f, %ecx /* NB_CFG_MSR */
302         rdmsr
303         movl    %edi, %ecx      /* CoreID bits */
304         bt      $(54-32), %edx
305         jc      roll_cfg
306         rolb    %cl, %bl
307 roll_cfg:
308
309         /* calculate stack pointer */
310         movl    $CacheSizeAPStack, %eax
311         mull    %ebx
312         movl    $(CacheBase + (CacheSize - GlobalVarSize)/2), %esp
313         subl    %eax, %esp
314
315         /* retrive init detected */
316         movl    %esi, %ebx
317
318         movb    $0xA4, %al
319         outb    %al, $0x80
320
321 CAR_FAM10_ap_out:
322 #endif
323
324         movb    $0xA5, %al
325         outb    %al, $0x80
326
327         /* Restore the BIST result */
328         movl    %ebp, %eax
329
330         /* We need to set ebp ? No need */
331         movl    %esp, %ebp
332         pushl   %ebx  /* init detected */
333         pushl   %eax  /* bist */
334         call    cache_as_ram_main
335         /* We will not go back */
336
337         movb    $0xAF, %al              /* Should never see this postcode */
338         outb    %al, $0x80
339
340 fixed_mtrr_msr:
341         .long   0x250, 0x258, 0x259
342         .long   0x268, 0x269, 0x26A
343         .long   0x26B, 0x26C, 0x26D
344         .long   0x26E, 0x26F
345 var_mtrr_msr:
346         .long   0x200, 0x201, 0x202, 0x203
347         .long   0x204, 0x205, 0x206, 0x207
348         .long   0x208, 0x209, 0x20A, 0x20B
349         .long   0x20C, 0x20D, 0x20E, 0x20F
350 var_iorr_msr:
351         .long   0xC0010016, 0xC0010017, 0xC0010018, 0xC0010019
352 mem_top:
353         .long   0xC001001A, 0xC001001D
354         .long   0x000 /* NULL, end of table */
355
356 cache_as_ram_setup_out: