blob: 240d2ec9e4b0204c72944ace398203bf25265afc [file] [log] [blame]
Arthur Heymansdd4d8952018-06-03 12:04:26 +02001/*
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
17#include <cpu/x86/mtrr.h>
18#include <cpu/x86/cache.h>
19#include <cpu/x86/post_code.h>
20
21#define CACHE_AS_RAM_SIZE (CONFIG_DCACHE_RAM_SIZE \
22 + CONFIG_DCACHE_RAM_MRC_VAR_SIZE)
23#define CACHE_AS_RAM_BASE CONFIG_DCACHE_RAM_BASE
24
25#define CPU_PHYSMASK_HI (1 << (CONFIG_CPU_ADDR_BITS - 32) - 1)
26
27#define NoEvictMod_MSR 0x2e0
28
29.code32
30_cache_as_ram_setup:
31
32 /* Save the BIST result. */
33 movl %eax, %ebp
34
35cache_as_ram:
36 post_code(0x20)
37
38 /* Send INIT IPI to all excluding ourself. */
39 movl $0x000C4500, %eax
40 movl $0xFEE00300, %esi
41 movl %eax, (%esi)
42
43 /* All CPUs need to be in Wait for SIPI state */
44wait_for_sipi:
45 movl (%esi), %eax
46 bt $12, %eax
47 jc wait_for_sipi
48
49 post_code(0x21)
50 /* Clean-up MTRR_DEF_TYPE_MSR. */
51 movl $MTRR_DEF_TYPE_MSR, %ecx
52 xorl %eax, %eax
53 xorl %edx, %edx
54 wrmsr
55
56 post_code(0x22)
57 /* Zero out all fixed range MTRRs. */
58 movl $mtrr_table, %esi
59 movl $((mtrr_table_end - mtrr_table) >> 1), %edi
60 xorl %eax, %eax
61 xorl %edx, %edx
62clear_mtrrs:
63 movw (%esi), %bx
64 movzx %bx, %ecx
65 wrmsr
66 add $2, %esi
67 dec %edi
68 jnz clear_mtrrs
69
70 /* Zero out all variable range MTRRs. */
71 movl $MTRR_CAP_MSR, %ecx
72 rdmsr
73 andl $0xff, %eax
74 shl $1, %eax
75 movl %eax, %edi
76 movl $0x200, %ecx
77 xorl %eax, %eax
78 xorl %edx, %edx
79clear_var_mtrrs:
80 wrmsr
81 add $1, %ecx
82 dec %edi
83 jnz clear_var_mtrrs
84
85 post_code(0x23)
86 /* Set Cache-as-RAM base address. */
87 movl $(MTRR_PHYS_BASE(0)), %ecx
88 movl $(CACHE_AS_RAM_BASE | MTRR_TYPE_WRBACK), %eax
89 xorl %edx, %edx
90 wrmsr
91
92 post_code(0x24)
93 /* Set Cache-as-RAM mask. */
94 movl $(MTRR_PHYS_MASK(0)), %ecx
95 movl $(~(CACHE_AS_RAM_SIZE - 1) | MTRR_PHYS_MASK_VALID), %eax
96 movl $CPU_PHYSMASK_HI, %edx
97 wrmsr
98
99 post_code(0x25)
100
101 /* Enable MTRR. */
102 movl $MTRR_DEF_TYPE_MSR, %ecx
103 rdmsr
104 orl $MTRR_DEF_TYPE_EN, %eax
105 wrmsr
106
107 /* Enable cache (CR0.CD = 0, CR0.NW = 0). */
108 movl %cr0, %eax
109 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
110 invd
111 movl %eax, %cr0
112
113 /* enable the 'no eviction' mode */
114 movl $NoEvictMod_MSR, %ecx
115 rdmsr
116 orl $1, %eax
117 andl $~2, %eax
118 wrmsr
119
120 /* Clear the cache memory region. This will also fill up the cache. */
121 movl $CACHE_AS_RAM_BASE, %esi
122 movl %esi, %edi
123 movl $(CACHE_AS_RAM_SIZE >> 2), %ecx
124 // movl $0x23322332, %eax
125 xorl %eax, %eax
126 rep stosl
127
128 /* enable the 'no eviction run' state */
129 movl $NoEvictMod_MSR, %ecx
130 rdmsr
131 orl $3, %eax
132 wrmsr
133
134 post_code(0x26)
135 /* Enable Cache-as-RAM mode by disabling cache. */
136 movl %cr0, %eax
137 orl $CR0_CacheDisable, %eax
138 movl %eax, %cr0
139
140 /* Enable cache for our code in Flash because we do XIP here */
141 movl $MTRR_PHYS_BASE(1), %ecx
142 xorl %edx, %edx
143 /*
144 * IMPORTANT: The following calculation _must_ be done at runtime. See
145 * https://www.coreboot.org/pipermail/coreboot/2010-October/060855.html
146 */
147 movl $copy_and_run, %eax
148 andl $(~(CONFIG_XIP_ROM_SIZE - 1)), %eax
149 orl $MTRR_TYPE_WRPROT, %eax
150 wrmsr
151
152 movl $MTRR_PHYS_MASK(1), %ecx
153 movl $CPU_PHYSMASK_HI, %edx
154 movl $(~(CONFIG_XIP_ROM_SIZE - 1) | MTRR_PHYS_MASK_VALID), %eax
155 wrmsr
156
157 post_code(0x28)
158 /* Enable cache. */
159 movl %cr0, %eax
160 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
161 movl %eax, %cr0
162
163 /* Setup the stack. */
164 movl $(CONFIG_DCACHE_RAM_BASE + CONFIG_DCACHE_RAM_SIZE), %eax
165 movl %eax, %esp
166
167 /* Restore the BIST result. */
168 movl %ebp, %eax
169 movl %esp, %ebp
170 pushl %eax
171
172before_romstage:
173 post_code(0x29)
174 /* Call romstage.c main function. */
175 call romstage_main
176
177 /* Should never see this postcode */
178 post_code(POST_DEAD_CODE)
179
180
181.Lhlt:
182 hlt
183 jmp .Lhlt
184
185mtrr_table:
186 /* Fixed MTRRs */
187 .word 0x250, 0x258, 0x259
188 .word 0x268, 0x269, 0x26A
189 .word 0x26B, 0x26C, 0x26D
190 .word 0x26E, 0x26F
191mtrr_table_end:
192
193_cache_as_ram_setup_end: