blob: b2b915fe76ca365e90c1e522ebda54d21ae287b2 [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
Arthur Heymansc2ccc972018-06-03 12:09:52 +02006 * Copyright (C) 2012 Kyösti Mälkki <kyosti.malkki@gmail.com>
Arthur Heymansdd4d8952018-06-03 12:04:26 +02007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
18#include <cpu/x86/mtrr.h>
19#include <cpu/x86/cache.h>
20#include <cpu/x86/post_code.h>
21
22#define CACHE_AS_RAM_SIZE (CONFIG_DCACHE_RAM_SIZE \
23 + CONFIG_DCACHE_RAM_MRC_VAR_SIZE)
24#define CACHE_AS_RAM_BASE CONFIG_DCACHE_RAM_BASE
25
Arthur Heymansdd4d8952018-06-03 12:04:26 +020026#define NoEvictMod_MSR 0x2e0
Arthur Heymans19e72732019-01-11 23:56:51 +010027#define BBL_CR_CTL3_MSR 0x11e
Arthur Heymansdd4d8952018-06-03 12:04:26 +020028
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +020029.global bootblock_pre_c_entry
30
Arthur Heymansdd4d8952018-06-03 12:04:26 +020031.code32
32_cache_as_ram_setup:
33
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +020034bootblock_pre_c_entry:
Arthur Heymansdd4d8952018-06-03 12:04:26 +020035
36cache_as_ram:
37 post_code(0x20)
38
39 /* Send INIT IPI to all excluding ourself. */
40 movl $0x000C4500, %eax
41 movl $0xFEE00300, %esi
42 movl %eax, (%esi)
43
44 /* All CPUs need to be in Wait for SIPI state */
45wait_for_sipi:
46 movl (%esi), %eax
47 bt $12, %eax
48 jc wait_for_sipi
49
50 post_code(0x21)
51 /* Clean-up MTRR_DEF_TYPE_MSR. */
52 movl $MTRR_DEF_TYPE_MSR, %ecx
53 xorl %eax, %eax
54 xorl %edx, %edx
55 wrmsr
56
57 post_code(0x22)
Arthur Heymansc2ccc972018-06-03 12:09:52 +020058 /* Clear/disable fixed MTRRs */
59 mov $fixed_mtrr_list_size, %ebx
60 xor %eax, %eax
61 xor %edx, %edx
62
63clear_fixed_mtrr:
64 add $-2, %ebx
65 movzwl fixed_mtrr_list(%ebx), %ecx
Arthur Heymansdd4d8952018-06-03 12:04:26 +020066 wrmsr
Arthur Heymansc2ccc972018-06-03 12:09:52 +020067 jnz clear_fixed_mtrr
Arthur Heymansdd4d8952018-06-03 12:04:26 +020068
69 /* Zero out all variable range MTRRs. */
70 movl $MTRR_CAP_MSR, %ecx
71 rdmsr
72 andl $0xff, %eax
73 shl $1, %eax
74 movl %eax, %edi
75 movl $0x200, %ecx
76 xorl %eax, %eax
77 xorl %edx, %edx
78clear_var_mtrrs:
79 wrmsr
80 add $1, %ecx
81 dec %edi
82 jnz clear_var_mtrrs
83
Arthur Heymansc2ccc972018-06-03 12:09:52 +020084 /* Determine CPU_ADDR_BITS and load PHYSMASK high word to %edx. */
85 movl $0x80000008, %eax
86 cpuid
87 movb %al, %cl
88 sub $32, %cl
89 movl $1, %edx
90 shl %cl, %edx
91 subl $1, %edx
92
93 /* Preload high word of address mask (in %edx) for Variable
94 * MTRRs 0 and 1.
95 */
96addrsize_set_high:
97 xorl %eax, %eax
98 movl $MTRR_PHYS_MASK(0), %ecx
99 wrmsr
100 movl $MTRR_PHYS_MASK(1), %ecx
101 wrmsr
102
103
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200104 post_code(0x23)
105 /* Set Cache-as-RAM base address. */
106 movl $(MTRR_PHYS_BASE(0)), %ecx
107 movl $(CACHE_AS_RAM_BASE | MTRR_TYPE_WRBACK), %eax
108 xorl %edx, %edx
109 wrmsr
110
111 post_code(0x24)
112 /* Set Cache-as-RAM mask. */
113 movl $(MTRR_PHYS_MASK(0)), %ecx
Arthur Heymansc2ccc972018-06-03 12:09:52 +0200114 rdmsr
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200115 movl $(~(CACHE_AS_RAM_SIZE - 1) | MTRR_PHYS_MASK_VALID), %eax
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200116 wrmsr
117
Arthur Heymans48bf7122019-01-05 17:18:11 +0100118 /* Enable cache for our code in Flash because we do XIP here */
119 movl $MTRR_PHYS_BASE(1), %ecx
120 xorl %edx, %edx
Arthur Heymanseeedf832019-02-08 16:27:35 +0100121 movl $(CACHE_ROM_BASE | MTRR_TYPE_WRPROT), %eax
Arthur Heymans48bf7122019-01-05 17:18:11 +0100122 wrmsr
123
124 movl $MTRR_PHYS_MASK(1), %ecx
125 rdmsr
126 movl $(~(CACHE_ROM_SIZE - 1) | MTRR_PHYS_MASK_VALID), %eax
127 wrmsr
128
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200129 post_code(0x25)
130
131 /* Enable MTRR. */
132 movl $MTRR_DEF_TYPE_MSR, %ecx
133 rdmsr
134 orl $MTRR_DEF_TYPE_EN, %eax
135 wrmsr
136
Julius Wernercd49cce2019-03-05 16:53:33 -0800137#if CONFIG(CPU_HAS_L2_ENABLE_MSR)
Arthur Heymans19e72732019-01-11 23:56:51 +0100138 /*
139 * Enable the L2 cache. Currently this assumes that this
140 * only affect socketed CPU's for which this is always valid,
141 * hence the static preprocesser.
142 */
143 movl $BBL_CR_CTL3_MSR, %ecx
144 rdmsr
145 orl $0x100, %eax
146 wrmsr
147#endif
148
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200149 /* Enable cache (CR0.CD = 0, CR0.NW = 0). */
150 movl %cr0, %eax
151 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
152 invd
153 movl %eax, %cr0
154
Julius Wernercd49cce2019-03-05 16:53:33 -0800155#if CONFIG(MICROCODE_UPDATE_PRE_RAM)
Arthur Heymans48bf7122019-01-05 17:18:11 +0100156update_microcode:
157 /* put the return address in %esp */
158 movl $end_microcode_update, %esp
159 jmp update_bsp_microcode
160end_microcode_update:
161#endif
162 /* Disable caching to change MTRR's. */
163 movl %cr0, %eax
164 orl $CR0_CacheDisable, %eax
165 movl %eax, %cr0
166
167 /* Clear the mask valid to disable the MTRR */
168 movl $MTRR_PHYS_MASK(1), %ecx
169 rdmsr
170 andl $(~MTRR_PHYS_MASK_VALID), %eax
171 wrmsr
172
173 /* Enable cache. */
174 movl %cr0, %eax
175 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
176 invd
177 movl %eax, %cr0
178
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200179 /* enable the 'no eviction' mode */
Arthur Heymansa28befd2018-12-20 13:59:34 +0100180 movl $NoEvictMod_MSR, %ecx
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200181 rdmsr
Arthur Heymansa28befd2018-12-20 13:59:34 +0100182 orl $1, %eax
183 andl $~2, %eax
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200184 wrmsr
185
186 /* Clear the cache memory region. This will also fill up the cache. */
187 movl $CACHE_AS_RAM_BASE, %esi
188 movl %esi, %edi
189 movl $(CACHE_AS_RAM_SIZE >> 2), %ecx
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200190 xorl %eax, %eax
191 rep stosl
192
193 /* enable the 'no eviction run' state */
Arthur Heymansa28befd2018-12-20 13:59:34 +0100194 movl $NoEvictMod_MSR, %ecx
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200195 rdmsr
Arthur Heymansa28befd2018-12-20 13:59:34 +0100196 orl $3, %eax
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200197 wrmsr
198
199 post_code(0x26)
200 /* Enable Cache-as-RAM mode by disabling cache. */
201 movl %cr0, %eax
202 orl $CR0_CacheDisable, %eax
203 movl %eax, %cr0
204
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200205 movl $MTRR_PHYS_MASK(1), %ecx
Arthur Heymansc2ccc972018-06-03 12:09:52 +0200206 rdmsr
Arthur Heymans48bf7122019-01-05 17:18:11 +0100207 orl $MTRR_PHYS_MASK_VALID, %eax
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200208 wrmsr
209
210 post_code(0x28)
211 /* Enable cache. */
212 movl %cr0, %eax
213 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
214 movl %eax, %cr0
215
216 /* Setup the stack. */
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200217 mov $_car_stack_end, %esp
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200218
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200219 /* Need to align stack to 16 bytes at call instruction. Account for
220 the pushes below. */
Arthur Heymans348b79f2018-06-03 17:14:19 +0200221 andl $0xfffffff0, %esp
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200222 subl $4, %esp
Arthur Heymans348b79f2018-06-03 17:14:19 +0200223
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200224 /* push TSC and BIST to stack */
225 movd %mm0, %eax
Elyes HAOUAS87930b32019-01-16 12:41:57 +0100226 pushl %eax /* BIST */
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200227 movd %mm2, %eax
228 pushl %eax /* tsc[63:32] */
229 movd %mm1, %eax
Elyes HAOUAS87930b32019-01-16 12:41:57 +0100230 pushl %eax /* tsc[31:0] */
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200231
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200232before_c_entry:
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200233 post_code(0x29)
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200234 call bootblock_c_entry_bist
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200235
236 /* Should never see this postcode */
237 post_code(POST_DEAD_CODE)
238
239
240.Lhlt:
241 hlt
242 jmp .Lhlt
243
Arthur Heymansc2ccc972018-06-03 12:09:52 +0200244fixed_mtrr_list:
245 .word MTRR_FIX_64K_00000
246 .word MTRR_FIX_16K_80000
247 .word MTRR_FIX_16K_A0000
248 .word MTRR_FIX_4K_C0000
249 .word MTRR_FIX_4K_C8000
250 .word MTRR_FIX_4K_D0000
251 .word MTRR_FIX_4K_D8000
252 .word MTRR_FIX_4K_E0000
253 .word MTRR_FIX_4K_E8000
254 .word MTRR_FIX_4K_F0000
255 .word MTRR_FIX_4K_F8000
256fixed_mtrr_list_size = . - fixed_mtrr_list
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200257
258_cache_as_ram_setup_end: