Intel cpus: apply some good programming practices in new CAR
[coreboot.git] / src / cpu / intel / car / cache_as_ram_ht.inc
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2000,2007 Ronald G. Minnich <rminnich@gmail.com>
5  * Copyright (C) 2007-2008 coresystems GmbH
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/stack.h>
22 #include <cpu/x86/mtrr.h>
23 #include <cpu/x86/post_code.h>
24 #include <cpu/x86/lapic_def.h>
25
26 /* Macro to access Local APIC registers at default base. */
27 #define LAPIC(x)                $(LAPIC_DEFAULT_BASE | LAPIC_ ## x)
28
29 #define CPU_MAXPHYADDR 36
30 #define CPU_PHYSMASK_HI  (1 << (CPU_MAXPHYADDR - 32) - 1)
31
32 /* Base address to cache all of Flash ROM, just below 4GB. */
33 #define CACHE_ROM_BASE  ((1<<22 - CONFIG_CACHE_ROM_SIZE>>10)<<10)
34
35 #define CACHE_AS_RAM_SIZE CONFIG_DCACHE_RAM_SIZE
36 #define CACHE_AS_RAM_BASE CONFIG_DCACHE_RAM_BASE
37
38         /* Save the BIST result. */
39         movl    %eax, %ebp
40
41 cache_as_ram:
42         post_code(0x20)
43
44         /* Send INIT IPI to all excluding ourself. */
45         movl    LAPIC(ICR), %edi
46         movl    $(LAPIC_DEST_ALLBUT | LAPIC_INT_ASSERT | LAPIC_DM_INIT), %eax
47         movl    %eax, (%edi)
48
49         /* Zero out all fixed range and variable range MTRRs. */
50         movl    $mtrr_table, %esi
51         movl    $((mtrr_table_end - mtrr_table) / 2), %edi
52         xorl    %eax, %eax
53         xorl    %edx, %edx
54 clear_mtrrs:
55         movw    (%esi), %bx
56         movzx   %bx, %ecx
57         wrmsr
58         add     $2, %esi
59         dec     %edi
60         jnz     clear_mtrrs
61
62         /* Configure the default memory type to uncacheable. */
63         movl    $MTRRdefType_MSR, %ecx
64         rdmsr
65         andl    $(~0x00000cff), %eax
66         wrmsr
67
68         /* Set Cache-as-RAM base address. */
69         movl    $(MTRRphysBase_MSR(0)), %ecx
70         movl    $(CACHE_AS_RAM_BASE | MTRR_TYPE_WRBACK), %eax
71         xorl    %edx, %edx
72         wrmsr
73
74         /* Set Cache-as-RAM mask. */
75         movl    $(MTRRphysMask_MSR(0)), %ecx
76         movl    $(~(CACHE_AS_RAM_SIZE - 1) | MTRRphysMaskValid), %eax
77         movl    $CPU_PHYSMASK_HI, %edx
78         wrmsr
79
80         /* Enable MTRR. */
81         movl    $MTRRdefType_MSR, %ecx
82         rdmsr
83         orl     $MTRRdefTypeEn, %eax
84         wrmsr
85
86         /* Enable L2 cache. */
87         movl    $0x11e, %ecx
88         rdmsr
89         orl     $(1 << 8), %eax
90         wrmsr
91
92         /* Enable cache (CR0.CD = 0, CR0.NW = 0). */
93         movl    %cr0, %eax
94         andl    $(~((1 << 30) | (1 << 29))), %eax
95         invd
96         movl    %eax, %cr0
97
98         /* Clear the cache memory reagion. */
99         cld
100         xorl    %eax, %eax
101         movl    $CACHE_AS_RAM_BASE, %edi
102         movl    $(CACHE_AS_RAM_SIZE / 4), %ecx
103         rep     stosl
104
105         /* Enable Cache-as-RAM mode by disabling cache. */
106         movl    %cr0, %eax
107         orl     $(1 << 30), %eax
108         movl    %eax, %cr0
109
110 #if CONFIG_XIP_ROM_SIZE
111         /* Enable cache for our code in Flash because we do XIP here */
112         movl    $MTRRphysBase_MSR(1), %ecx
113         xorl    %edx, %edx
114         /*
115          * IMPORTANT: The following calculation _must_ be done at runtime. See
116          * http://www.coreboot.org/pipermail/coreboot/2010-October/060855.html
117          */
118         movl    $copy_and_run, %eax
119         andl    $(~(CONFIG_XIP_ROM_SIZE - 1)), %eax
120         orl     $MTRR_TYPE_WRBACK, %eax
121         wrmsr
122
123         movl    $MTRRphysMask_MSR(1), %ecx
124         movl    $CPU_PHYSMASK_HI, %edx
125         movl    $(~(CONFIG_XIP_ROM_SIZE - 1) | MTRRphysMaskValid), %eax
126         wrmsr
127 #endif /* CONFIG_XIP_ROM_SIZE */
128
129         /* Enable cache. */
130         movl    %cr0, %eax
131         andl    $(~((1 << 30) | (1 << 29))), %eax
132         movl    %eax, %cr0
133
134         /* Set up the stack pointer. */
135 #if CONFIG_USBDEBUG
136         /* Leave some space for the struct ehci_debug_info. */
137         movl    $(CACHE_AS_RAM_BASE + CACHE_AS_RAM_SIZE - 4 - 128), %esp
138 #else
139         movl    $(CACHE_AS_RAM_BASE + CACHE_AS_RAM_SIZE - 4), %esp
140 #endif
141
142         /* Restore the BIST result. */
143         movl    %ebp, %eax
144         movl    %esp, %ebp
145         pushl   %eax
146
147         post_code(0x23)
148
149         /* Call romstage.c main function. */
150         call    main
151         addl    $4, %esp
152
153         post_code(0x2f)
154
155         post_code(0x30)
156
157         /* Disable cache. */
158         movl    %cr0, %eax
159         orl     $(1 << 30), %eax
160         movl    %eax, %cr0
161
162         post_code(0x31)
163
164         /* Disable MTRR. */
165         movl    $MTRRdefType_MSR, %ecx
166         rdmsr
167         andl    $(~MTRRdefTypeEn), %eax
168         wrmsr
169
170         post_code(0x31)
171
172         invd
173
174         post_code(0x33)
175
176         /* Enable cache. */
177         movl    %cr0, %eax
178         andl    $~((1 << 30) | (1 << 29)), %eax
179         movl    %eax, %cr0
180
181         post_code(0x36)
182
183         /* Disable cache. */
184         movl    %cr0, %eax
185         orl     $(1 << 30), %eax
186         movl    %eax, %cr0
187
188         post_code(0x38)
189
190         /* Enable Write Back and Speculative Reads for low RAM. */
191         movl    $MTRRphysBase_MSR(0), %ecx
192         movl    $(0x00000000 | MTRR_TYPE_WRBACK), %eax
193         xorl    %edx, %edx
194         wrmsr
195         movl    $MTRRphysMask_MSR(0), %ecx
196         movl    $(~(CONFIG_RAMTOP - 1) | MTRRphysMaskValid), %eax
197         movl    $CPU_PHYSMASK_HI, %edx
198         wrmsr
199
200         /* Enable caching and Speculative Reads for Flash ROM device. */
201         movl    $MTRRphysBase_MSR(1), %ecx
202         movl    $(CACHE_ROM_BASE | MTRR_TYPE_WRPROT), %eax
203         xorl    %edx, %edx
204         wrmsr
205         movl    $MTRRphysMask_MSR(1), %ecx
206         movl    $(~(CONFIG_CACHE_ROM_SIZE - 1) | MTRRphysMaskValid), %eax
207         movl    $CPU_PHYSMASK_HI, %edx
208         wrmsr
209
210         post_code(0x39)
211
212         /* And enable cache again after setting MTRRs. */
213         movl    %cr0, %eax
214         andl    $~((1 << 30) | (1 << 29)), %eax
215         movl    %eax, %cr0
216
217         post_code(0x3a)
218
219         /* Enable MTRR. */
220         movl    $MTRRdefType_MSR, %ecx
221         rdmsr
222         orl     $MTRRdefTypeEn, %eax
223         wrmsr
224
225         post_code(0x3b)
226
227         /* Invalidate the cache again. */
228         invd
229
230         post_code(0x3c)
231
232         /* Clear boot_complete flag. */
233         xorl    %ebp, %ebp
234 __main:
235         post_code(POST_PREPARE_RAMSTAGE)
236         cld                     /* Clear direction flag. */
237
238         movl    %ebp, %esi
239
240         movl    $ROMSTAGE_STACK, %esp
241         movl    %esp, %ebp
242         pushl   %esi
243         call    copy_and_run
244
245 .Lhlt:
246         post_code(POST_DEAD_CODE)
247         hlt
248         jmp     .Lhlt
249
250 mtrr_table:
251         /* Fixed MTRRs */
252         .word 0x250, 0x258, 0x259
253         .word 0x268, 0x269, 0x26A
254         .word 0x26B, 0x26C, 0x26D
255         .word 0x26E, 0x26F
256         /* Variable MTRRs */
257         .word 0x200, 0x201, 0x202, 0x203
258         .word 0x204, 0x205, 0x206, 0x207
259         .word 0x208, 0x209, 0x20A, 0x20B
260         .word 0x20C, 0x20D, 0x20E, 0x20F
261 mtrr_table_end:
262