Use more secure HTTPS URLs for coreboot sites
[coreboot.git] / src / cpu / intel / haswell / cache_as_ram.inc
blob388c2ea23ca012941f1d0d04882c542668cea2b4
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  */
17 #include <cpu/x86/mtrr.h>
18 #include <cpu/x86/cache.h>
19 #include <cpu/x86/post_code.h>
21 /* The full cache-as-ram size includes the cache-as-ram portion from coreboot
22  * and the space used by the reference code. These 2 values combined should
23  * be a power of 2 because the MTRR setup assumes that. */
24 #define CACHE_AS_RAM_SIZE \
25         (CONFIG_DCACHE_RAM_SIZE + CONFIG_DCACHE_RAM_MRC_VAR_SIZE)
26 #define CACHE_AS_RAM_BASE CONFIG_DCACHE_RAM_BASE
28 /* Cache 4GB - MRC_SIZE_KB for MRC */
29 #define CACHE_MRC_BYTES   ((CONFIG_CACHE_MRC_SIZE_KB << 10) - 1)
30 #define CACHE_MRC_BASE    (0xFFFFFFFF - CACHE_MRC_BYTES)
31 #define CACHE_MRC_MASK    (~CACHE_MRC_BYTES)
33 #define CPU_PHYSMASK_HI  (1 << (CONFIG_CPU_ADDR_BITS - 32) - 1)
35 #define NoEvictMod_MSR 0x2e0
37         /* Save the BIST result. */
38         movl    %eax, %ebp
40 cache_as_ram:
41         post_code(0x20)
43         /* Send INIT IPI to all excluding ourself. */
44         movl    $0x000C4500, %eax
45         movl    $0xFEE00300, %esi
46         movl    %eax, (%esi)
48         /* All CPUs need to be in Wait for SIPI state */
49 wait_for_sipi:
50         movl    (%esi), %eax
51         bt      $12, %eax
52         jc      wait_for_sipi
54         post_code(0x21)
55         /* Zero out all fixed range and variable range MTRRs. */
56         movl    $mtrr_table, %esi
57         movl    $((mtrr_table_end - mtrr_table) >> 1), %edi
58         xorl    %eax, %eax
59         xorl    %edx, %edx
60 clear_mtrrs:
61         movw    (%esi), %bx
62         movzx   %bx, %ecx
63         wrmsr
64         add     $2, %esi
65         dec     %edi
66         jnz     clear_mtrrs
68         post_code(0x22)
69         /* Configure the default memory type to uncacheable. */
70         movl    $MTRR_DEF_TYPE_MSR, %ecx
71         rdmsr
72         andl    $(~0x00000cff), %eax
73         wrmsr
75         post_code(0x23)
76         /* Set Cache-as-RAM base address. */
77         movl    $(MTRR_PHYS_BASE(0)), %ecx
78         movl    $(CACHE_AS_RAM_BASE | MTRR_TYPE_WRBACK), %eax
79         xorl    %edx, %edx
80         wrmsr
82         post_code(0x24)
83         /* Set Cache-as-RAM mask. */
84         movl    $(MTRR_PHYS_MASK(0)), %ecx
85         movl    $(~(CACHE_AS_RAM_SIZE - 1) | MTRR_PHYS_MASK_VALID), %eax
86         movl    $CPU_PHYSMASK_HI, %edx
87         wrmsr
89         post_code(0x25)
91         /* Enable MTRR. */
92         movl    $MTRR_DEF_TYPE_MSR, %ecx
93         rdmsr
94         orl     $MTRR_DEF_TYPE_EN, %eax
95         wrmsr
97         /* Enable cache (CR0.CD = 0, CR0.NW = 0). */
98         movl    %cr0, %eax
99         andl    $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
100         invd
101         movl    %eax, %cr0
103         /* enable the 'no eviction' mode */
104         movl    $NoEvictMod_MSR, %ecx
105         rdmsr
106         orl     $1, %eax
107         andl    $~2, %eax
108         wrmsr
110         /* Clear the cache memory region. This will also fill up the cache. */
111         movl    $CACHE_AS_RAM_BASE, %esi
112         movl    %esi, %edi
113         movl    $(CACHE_AS_RAM_SIZE >> 2), %ecx
114         // movl $0x23322332, %eax
115         xorl    %eax, %eax
116         rep     stosl
118         /* enable the 'no eviction run' state */
119         movl    $NoEvictMod_MSR, %ecx
120         rdmsr
121         orl     $3, %eax
122         wrmsr
124         post_code(0x26)
125         /* Enable Cache-as-RAM mode by disabling cache. */
126         movl    %cr0, %eax
127         orl     $CR0_CacheDisable, %eax
128         movl    %eax, %cr0
130         /* Enable cache for our code in Flash because we do XIP here */
131         movl    $MTRR_PHYS_BASE(1), %ecx
132         xorl    %edx, %edx
133         /*
134          * IMPORTANT: The following calculation _must_ be done at runtime. See
135          * https://www.coreboot.org/pipermail/coreboot/2010-October/060855.html
136          */
137         movl    $copy_and_run, %eax
138         andl    $(~(CONFIG_XIP_ROM_SIZE - 1)), %eax
139         orl     $MTRR_TYPE_WRPROT, %eax
140         wrmsr
142         movl    $MTRR_PHYS_MASK(1), %ecx
143         movl    $CPU_PHYSMASK_HI, %edx
144         movl    $(~(CONFIG_XIP_ROM_SIZE - 1) | MTRR_PHYS_MASK_VALID), %eax
145         wrmsr
147         post_code(0x27)
148         /* Enable caching for RAM init code to run faster */
149         movl    $MTRR_PHYS_BASE(2), %ecx
150         movl    $(CACHE_MRC_BASE | MTRR_TYPE_WRPROT), %eax
151         xorl    %edx, %edx
152         wrmsr
153         movl    $MTRR_PHYS_MASK(2), %ecx
154         movl    $(CACHE_MRC_MASK | MTRR_PHYS_MASK_VALID), %eax
155         movl    $CPU_PHYSMASK_HI, %edx
156         wrmsr
158         post_code(0x28)
159         /* Enable cache. */
160         movl    %cr0, %eax
161         andl    $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
162         movl    %eax, %cr0
164         /* Setup the stack. */
165         movl    $(CONFIG_DCACHE_RAM_BASE + CONFIG_DCACHE_RAM_SIZE), %eax
166         movl    %eax, %esp
168         /* Restore the BIST result. */
169         movl    %ebp, %eax
170         movl    %esp, %ebp
171         pushl   %eax
173 before_romstage:
174         post_code(0x29)
175         /* Call romstage.c main function. */
176         call    romstage_main
177         /* Save return value from romstage_main. It contains the stack to use
178          * after cache-as-ram is torn down. It also contains the information
179          * for setting up MTRRs. */
180         movl    %eax, %esp
182         post_code(0x30)
184         /* Disable cache. */
185         movl    %cr0, %eax
186         orl     $CR0_CacheDisable, %eax
187         movl    %eax, %cr0
189         post_code(0x31)
191         /* Disable MTRR. */
192         movl    $MTRR_DEF_TYPE_MSR, %ecx
193         rdmsr
194         andl    $(~MTRR_DEF_TYPE_EN), %eax
195         wrmsr
197         post_code(0x32)
199         /* Disable the no eviction run state */
200         movl    $NoEvictMod_MSR, %ecx
201         rdmsr
202         andl    $~2, %eax
203         wrmsr
205         invd
207         /* Disable the no eviction mode */
208         rdmsr
209         andl    $~1, %eax
210         wrmsr
212         post_code(0x33)
214         /* Enable cache. */
215         movl    %cr0, %eax
216         andl    $~(CR0_CacheDisable | CR0_NoWriteThrough), %eax
217         movl    %eax, %cr0
219         post_code(0x36)
221         /* Disable cache. */
222         movl    %cr0, %eax
223         orl     $CR0_CacheDisable, %eax
224         movl    %eax, %cr0
226         post_code(0x38)
228         /* Get number of MTRRs. */
229         popl    %ebx
230         movl    $MTRR_PHYS_BASE(0), %ecx
232         testl   %ebx, %ebx
233         jz      1f
235         /* Low 32 bits of MTRR base. */
236         popl    %eax
237         /* Upper 32 bits of MTRR base. */
238         popl    %edx
239         /* Write MTRR base. */
240         wrmsr
241         inc     %ecx
242         /* Low 32 bits of MTRR mask. */
243         popl    %eax
244         /* Upper 32 bits of MTRR mask. */
245         popl    %edx
246         /* Write MTRR mask. */
247         wrmsr
248         inc     %ecx
250         dec     %ebx
251         jmp     1b
253         post_code(0x39)
255         /* And enable cache again after setting MTRRs. */
256         movl    %cr0, %eax
257         andl    $~(CR0_CacheDisable | CR0_NoWriteThrough), %eax
258         movl    %eax, %cr0
260         post_code(0x3a)
262         /* Enable MTRR. */
263         movl    $MTRR_DEF_TYPE_MSR, %ecx
264         rdmsr
265         orl     $MTRR_DEF_TYPE_EN, %eax
266         wrmsr
268         post_code(0x3b)
270         /* Invalidate the cache again. */
271         invd
273         post_code(0x3c)
275 __main:
276         post_code(POST_PREPARE_RAMSTAGE)
277         cld                     /* Clear direction flag. */
278         call    romstage_after_car
280 .Lhlt:
281         post_code(POST_DEAD_CODE)
282         hlt
283         jmp     .Lhlt
285 mtrr_table:
286         /* Fixed MTRRs */
287         .word 0x250, 0x258, 0x259
288         .word 0x268, 0x269, 0x26A
289         .word 0x26B, 0x26C, 0x26D
290         .word 0x26E, 0x26F
291         /* Variable MTRRs */
292         .word 0x200, 0x201, 0x202, 0x203
293         .word 0x204, 0x205, 0x206, 0x207
294         .word 0x208, 0x209, 0x20A, 0x20B
295         .word 0x20C, 0x20D, 0x20E, 0x20F
296         .word 0x210, 0x211, 0x212, 0x213
297 mtrr_table_end: