blob: cd6972062c7a8d80c2705c0333a851dec7079819 [file] [log] [blame]
Arthur Heymansdd4d8952018-06-03 12:04:26 +02001/*
2 * This file is part of the coreboot project.
3 *
Arthur Heymansdd4d8952018-06-03 12:04:26 +02004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#include <cpu/x86/mtrr.h>
15#include <cpu/x86/cache.h>
16#include <cpu/x86/post_code.h>
17
18#define CACHE_AS_RAM_SIZE (CONFIG_DCACHE_RAM_SIZE \
19 + CONFIG_DCACHE_RAM_MRC_VAR_SIZE)
20#define CACHE_AS_RAM_BASE CONFIG_DCACHE_RAM_BASE
21
Arthur Heymansdd4d8952018-06-03 12:04:26 +020022#define NoEvictMod_MSR 0x2e0
Arthur Heymans19e72732019-01-11 23:56:51 +010023#define BBL_CR_CTL3_MSR 0x11e
Arthur Heymansdd4d8952018-06-03 12:04:26 +020024
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +020025.global bootblock_pre_c_entry
26
Arthur Heymansdd4d8952018-06-03 12:04:26 +020027.code32
28_cache_as_ram_setup:
29
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +020030bootblock_pre_c_entry:
Arthur Heymansdd4d8952018-06-03 12:04:26 +020031
Arthur Heymans8e646e72018-06-05 11:19:22 +020032#if CONFIG(C_ENVIRONMENT_BOOTBLOCK)
33 movl $cache_as_ram, %esp /* return address */
34 jmp check_mtrr /* Check if CPU properly reset */
35#endif
36
Arthur Heymansdd4d8952018-06-03 12:04:26 +020037cache_as_ram:
38 post_code(0x20)
39
40 /* Send INIT IPI to all excluding ourself. */
41 movl $0x000C4500, %eax
42 movl $0xFEE00300, %esi
43 movl %eax, (%esi)
44
45 /* All CPUs need to be in Wait for SIPI state */
46wait_for_sipi:
47 movl (%esi), %eax
48 bt $12, %eax
49 jc wait_for_sipi
50
51 post_code(0x21)
52 /* Clean-up MTRR_DEF_TYPE_MSR. */
53 movl $MTRR_DEF_TYPE_MSR, %ecx
54 xorl %eax, %eax
55 xorl %edx, %edx
56 wrmsr
57
58 post_code(0x22)
Arthur Heymansc2ccc972018-06-03 12:09:52 +020059 /* Clear/disable fixed MTRRs */
60 mov $fixed_mtrr_list_size, %ebx
61 xor %eax, %eax
62 xor %edx, %edx
63
64clear_fixed_mtrr:
65 add $-2, %ebx
66 movzwl fixed_mtrr_list(%ebx), %ecx
Arthur Heymansdd4d8952018-06-03 12:04:26 +020067 wrmsr
Arthur Heymansc2ccc972018-06-03 12:09:52 +020068 jnz clear_fixed_mtrr
Arthur Heymansdd4d8952018-06-03 12:04:26 +020069
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
Arthur Heymansc2ccc972018-06-03 12:09:52 +020085 /* Determine CPU_ADDR_BITS and load PHYSMASK high word to %edx. */
86 movl $0x80000008, %eax
87 cpuid
88 movb %al, %cl
89 sub $32, %cl
90 movl $1, %edx
91 shl %cl, %edx
92 subl $1, %edx
93
94 /* Preload high word of address mask (in %edx) for Variable
95 * MTRRs 0 and 1.
96 */
97addrsize_set_high:
98 xorl %eax, %eax
99 movl $MTRR_PHYS_MASK(0), %ecx
100 wrmsr
101 movl $MTRR_PHYS_MASK(1), %ecx
102 wrmsr
103
104
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200105 post_code(0x23)
106 /* Set Cache-as-RAM base address. */
107 movl $(MTRR_PHYS_BASE(0)), %ecx
108 movl $(CACHE_AS_RAM_BASE | MTRR_TYPE_WRBACK), %eax
109 xorl %edx, %edx
110 wrmsr
111
112 post_code(0x24)
113 /* Set Cache-as-RAM mask. */
114 movl $(MTRR_PHYS_MASK(0)), %ecx
Arthur Heymansc2ccc972018-06-03 12:09:52 +0200115 rdmsr
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200116 movl $(~(CACHE_AS_RAM_SIZE - 1) | MTRR_PHYS_MASK_VALID), %eax
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200117 wrmsr
118
Arthur Heymans48bf7122019-01-05 17:18:11 +0100119 /* Enable cache for our code in Flash because we do XIP here */
120 movl $MTRR_PHYS_BASE(1), %ecx
121 xorl %edx, %edx
Arthur Heymanseeedf832019-02-08 16:27:35 +0100122 movl $(CACHE_ROM_BASE | MTRR_TYPE_WRPROT), %eax
Arthur Heymans48bf7122019-01-05 17:18:11 +0100123 wrmsr
124
125 movl $MTRR_PHYS_MASK(1), %ecx
126 rdmsr
127 movl $(~(CACHE_ROM_SIZE - 1) | MTRR_PHYS_MASK_VALID), %eax
128 wrmsr
129
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200130 post_code(0x25)
131
132 /* Enable MTRR. */
133 movl $MTRR_DEF_TYPE_MSR, %ecx
134 rdmsr
135 orl $MTRR_DEF_TYPE_EN, %eax
136 wrmsr
137
Julius Wernercd49cce2019-03-05 16:53:33 -0800138#if CONFIG(CPU_HAS_L2_ENABLE_MSR)
Arthur Heymans19e72732019-01-11 23:56:51 +0100139 /*
140 * Enable the L2 cache. Currently this assumes that this
141 * only affect socketed CPU's for which this is always valid,
142 * hence the static preprocesser.
143 */
144 movl $BBL_CR_CTL3_MSR, %ecx
145 rdmsr
146 orl $0x100, %eax
147 wrmsr
148#endif
149
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200150 /* Enable cache (CR0.CD = 0, CR0.NW = 0). */
151 movl %cr0, %eax
152 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
153 invd
154 movl %eax, %cr0
155
Julius Wernercd49cce2019-03-05 16:53:33 -0800156#if CONFIG(MICROCODE_UPDATE_PRE_RAM)
Arthur Heymans48bf7122019-01-05 17:18:11 +0100157update_microcode:
158 /* put the return address in %esp */
159 movl $end_microcode_update, %esp
160 jmp update_bsp_microcode
161end_microcode_update:
162#endif
163 /* Disable caching to change MTRR's. */
164 movl %cr0, %eax
165 orl $CR0_CacheDisable, %eax
166 movl %eax, %cr0
167
168 /* Clear the mask valid to disable the MTRR */
169 movl $MTRR_PHYS_MASK(1), %ecx
170 rdmsr
171 andl $(~MTRR_PHYS_MASK_VALID), %eax
172 wrmsr
173
174 /* Enable cache. */
175 movl %cr0, %eax
176 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
177 invd
178 movl %eax, %cr0
179
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200180 /* enable the 'no eviction' mode */
Arthur Heymansa28befd2018-12-20 13:59:34 +0100181 movl $NoEvictMod_MSR, %ecx
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200182 rdmsr
Arthur Heymansa28befd2018-12-20 13:59:34 +0100183 orl $1, %eax
184 andl $~2, %eax
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200185 wrmsr
186
187 /* Clear the cache memory region. This will also fill up the cache. */
188 movl $CACHE_AS_RAM_BASE, %esi
189 movl %esi, %edi
190 movl $(CACHE_AS_RAM_SIZE >> 2), %ecx
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200191 xorl %eax, %eax
192 rep stosl
193
194 /* enable the 'no eviction run' state */
Arthur Heymansa28befd2018-12-20 13:59:34 +0100195 movl $NoEvictMod_MSR, %ecx
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200196 rdmsr
Arthur Heymansa28befd2018-12-20 13:59:34 +0100197 orl $3, %eax
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200198 wrmsr
199
200 post_code(0x26)
201 /* Enable Cache-as-RAM mode by disabling cache. */
202 movl %cr0, %eax
203 orl $CR0_CacheDisable, %eax
204 movl %eax, %cr0
205
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200206 movl $MTRR_PHYS_MASK(1), %ecx
Arthur Heymansc2ccc972018-06-03 12:09:52 +0200207 rdmsr
Arthur Heymans48bf7122019-01-05 17:18:11 +0100208 orl $MTRR_PHYS_MASK_VALID, %eax
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200209 wrmsr
210
211 post_code(0x28)
212 /* Enable cache. */
213 movl %cr0, %eax
214 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
215 movl %eax, %cr0
216
217 /* Setup the stack. */
Arthur Heymansdf9cdcf2019-11-09 06:50:20 +0100218 mov $_ecar_stack, %esp
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200219
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200220 /* Need to align stack to 16 bytes at call instruction. Account for
221 the pushes below. */
Arthur Heymans348b79f2018-06-03 17:14:19 +0200222 andl $0xfffffff0, %esp
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200223 subl $4, %esp
Arthur Heymans348b79f2018-06-03 17:14:19 +0200224
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200225 /* push TSC and BIST to stack */
226 movd %mm0, %eax
Elyes HAOUAS87930b32019-01-16 12:41:57 +0100227 pushl %eax /* BIST */
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200228 movd %mm2, %eax
229 pushl %eax /* tsc[63:32] */
230 movd %mm1, %eax
Elyes HAOUAS87930b32019-01-16 12:41:57 +0100231 pushl %eax /* tsc[31:0] */
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200232
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200233before_c_entry:
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200234 post_code(0x29)
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200235 call bootblock_c_entry_bist
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200236
237 /* Should never see this postcode */
238 post_code(POST_DEAD_CODE)
239
240
241.Lhlt:
242 hlt
243 jmp .Lhlt
244
Arthur Heymansc2ccc972018-06-03 12:09:52 +0200245fixed_mtrr_list:
246 .word MTRR_FIX_64K_00000
247 .word MTRR_FIX_16K_80000
248 .word MTRR_FIX_16K_A0000
249 .word MTRR_FIX_4K_C0000
250 .word MTRR_FIX_4K_C8000
251 .word MTRR_FIX_4K_D0000
252 .word MTRR_FIX_4K_D8000
253 .word MTRR_FIX_4K_E0000
254 .word MTRR_FIX_4K_E8000
255 .word MTRR_FIX_4K_F0000
256 .word MTRR_FIX_4K_F8000
257fixed_mtrr_list_size = . - fixed_mtrr_list
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200258
259_cache_as_ram_setup_end: