blob: 3183cc6e0635325f42d230f0063ba3257845d340 [file] [log] [blame]
Angel Ponsf23ae0b2020-04-02 23:48:12 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Arthur Heymans7a8205b2018-06-03 10:29:07 +02002
3#include <cpu/x86/mtrr.h>
4#include <cpu/x86/cache.h>
5#include <cpu/x86/post_code.h>
6
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +02007.global bootblock_pre_c_entry
8
Arthur Heymans7a8205b2018-06-03 10:29:07 +02009.code32
10_cache_as_ram_setup:
11
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +020012bootblock_pre_c_entry:
Arthur Heymans7a8205b2018-06-03 10:29:07 +020013
14cache_as_ram:
15 post_code(0x20)
16
17 /* Send INIT IPI to all excluding ourself. */
18 movl $0x000C4500, %eax
19 movl $0xFEE00300, %esi
20 movl %eax, (%esi)
21
Arthur Heymans3aa9adb2018-06-03 11:02:54 +020022 /* All CPUs need to be in Wait for SIPI state */
23wait_for_sipi:
24 movl (%esi), %eax
25 bt $12, %eax
26 jc wait_for_sipi
27
28 post_code(0x22)
29
30 /* Clear/disable fixed MTRRs */
31 mov $fixed_mtrr_list_size, %ebx
32 xor %eax, %eax
33 xor %edx, %edx
34
35clear_fixed_mtrr:
36 add $-2, %ebx
37 movzwl fixed_mtrr_list(%ebx), %ecx
Arthur Heymans7a8205b2018-06-03 10:29:07 +020038 wrmsr
Arthur Heymans3aa9adb2018-06-03 11:02:54 +020039 jnz clear_fixed_mtrr
40
Elyes HAOUAS02820ca2018-09-30 07:44:39 +020041 /* Figure out how many MTRRs we have, and clear them out */
Arthur Heymans3aa9adb2018-06-03 11:02:54 +020042 mov $MTRR_CAP_MSR, %ecx
43 rdmsr
44 movzb %al, %ebx /* Number of variable MTRRs */
45 mov $MTRR_PHYS_BASE(0), %ecx
46 xor %eax, %eax
47 xor %edx, %edx
48
49clear_var_mtrr:
50 wrmsr
51 inc %ecx
52 wrmsr
53 inc %ecx
54 dec %ebx
55 jnz clear_var_mtrr
Arthur Heymans7a8205b2018-06-03 10:29:07 +020056
57 post_code(0x22)
58 /* Configure the default memory type to uncacheable. */
59 movl $MTRR_DEF_TYPE_MSR, %ecx
60 rdmsr
61 andl $(~0x00000cff), %eax
62 wrmsr
63
Arthur Heymans3aa9adb2018-06-03 11:02:54 +020064 /* Determine CPU_ADDR_BITS and load PHYSMASK high word to %edx. */
65 movl $0x80000008, %eax
66 cpuid
67 movb %al, %cl
68 sub $32, %cl
69 movl $1, %edx
70 shl %cl, %edx
71 subl $1, %edx
72
73 /* Preload high word of address mask (in %edx) for Variable
74 MTRRs 0 and 1. */
75addrsize_set_high:
76 xorl %eax, %eax
77 movl $MTRR_PHYS_MASK(0), %ecx
78 wrmsr
79 movl $MTRR_PHYS_MASK(1), %ecx
80 wrmsr
81
Arthur Heymans7a8205b2018-06-03 10:29:07 +020082 post_code(0x23)
83 /* Set Cache-as-RAM base address. */
84 movl $(MTRR_PHYS_BASE(0)), %ecx
Kyösti Mälkkidc6bb6c2019-11-08 00:08:55 +020085 movl $_car_mtrr_start, %eax
86 orl $MTRR_TYPE_WRBACK, %eax
Arthur Heymans7a8205b2018-06-03 10:29:07 +020087 xorl %edx, %edx
88 wrmsr
89
90 post_code(0x24)
91 /* Set Cache-as-RAM mask. */
92 movl $(MTRR_PHYS_MASK(0)), %ecx
Arthur Heymans3aa9adb2018-06-03 11:02:54 +020093 rdmsr
Kyösti Mälkkidc6bb6c2019-11-08 00:08:55 +020094 movl $_car_mtrr_mask, %eax
95 orl $MTRR_PHYS_MASK_VALID, %eax
Arthur Heymans7a8205b2018-06-03 10:29:07 +020096 wrmsr
97
98 post_code(0x25)
99
100 /* Enable MTRR. */
101 movl $MTRR_DEF_TYPE_MSR, %ecx
102 rdmsr
103 orl $MTRR_DEF_TYPE_EN, %eax
104 wrmsr
105
106 /* Enable L2 cache. */
107 movl $0x11e, %ecx
108 rdmsr
109 orl $(1 << 8), %eax
110 wrmsr
111
112 /* Enable cache (CR0.CD = 0, CR0.NW = 0). */
113 movl %cr0, %eax
114 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
115 invd
116 movl %eax, %cr0
117
118 /* Clear the cache memory region. This will also fill up the cache. */
Kyösti Mälkkidc6bb6c2019-11-08 00:08:55 +0200119 cld
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200120 xorl %eax, %eax
Kyösti Mälkkidc6bb6c2019-11-08 00:08:55 +0200121 movl $_car_mtrr_start, %edi
122 movl $_car_mtrr_size, %ecx
123 shr $2, %ecx
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200124 rep stosl
125
126 post_code(0x26)
127 /* Enable Cache-as-RAM mode by disabling cache. */
128 movl %cr0, %eax
129 orl $CR0_CacheDisable, %eax
130 movl %eax, %cr0
131
132 /* Enable cache for our code in Flash because we do XIP here */
133 movl $MTRR_PHYS_BASE(1), %ecx
134 xorl %edx, %edx
135 /*
136 * IMPORTANT: The following calculation _must_ be done at runtime. See
Stefan Taunerde028782018-08-19 20:02:05 +0200137 * https://mail.coreboot.org/pipermail/coreboot/2010-October/060922.html
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200138 */
Kyösti Mälkkice9f4222018-06-25 18:53:36 +0300139 movl $_program, %eax
Kyösti Mälkkidc6bb6c2019-11-08 00:08:55 +0200140 andl $_xip_mtrr_mask, %eax
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200141 orl $MTRR_TYPE_WRPROT, %eax
142 wrmsr
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200143 movl $MTRR_PHYS_MASK(1), %ecx
Arthur Heymans3aa9adb2018-06-03 11:02:54 +0200144 rdmsr
Kyösti Mälkkidc6bb6c2019-11-08 00:08:55 +0200145 movl $_xip_mtrr_mask, %eax
146 orl $MTRR_PHYS_MASK_VALID, %eax
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200147 wrmsr
148
149 post_code(0x28)
150 /* Enable cache. */
151 movl %cr0, %eax
152 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
153 movl %eax, %cr0
154
155 /* Setup the stack. */
Arthur Heymansdf9cdcf2019-11-09 06:50:20 +0100156 mov $_ecar_stack, %esp
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200157
158 /* Need to align stack to 16 bytes at call instruction. Account for
159 the pushes below. */
Arthur Heymans348b79f2018-06-03 17:14:19 +0200160 andl $0xfffffff0, %esp
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200161 subl $4, %esp
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200162
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200163 /* push TSC and BIST to stack */
164 movd %mm0, %eax
Elyes HAOUAS87930b32019-01-16 12:41:57 +0100165 pushl %eax /* BIST */
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200166 movd %mm2, %eax
167 pushl %eax /* tsc[63:32] */
168 movd %mm1, %eax
Elyes HAOUAS87930b32019-01-16 12:41:57 +0100169 pushl %eax /* tsc[31:0] */
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200170
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200171before_c_entry:
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200172 post_code(0x29)
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200173 call bootblock_c_entry_bist
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200174
175 /* Should never see this postcode */
176 post_code(POST_DEAD_CODE)
177
178.Lhlt:
179 hlt
180 jmp .Lhlt
181
Arthur Heymans3aa9adb2018-06-03 11:02:54 +0200182fixed_mtrr_list:
183 .word MTRR_FIX_64K_00000
184 .word MTRR_FIX_16K_80000
185 .word MTRR_FIX_16K_A0000
186 .word MTRR_FIX_4K_C0000
187 .word MTRR_FIX_4K_C8000
188 .word MTRR_FIX_4K_D0000
189 .word MTRR_FIX_4K_D8000
190 .word MTRR_FIX_4K_E0000
191 .word MTRR_FIX_4K_E8000
192 .word MTRR_FIX_4K_F0000
193 .word MTRR_FIX_4K_F8000
194fixed_mtrr_list_size = . - fixed_mtrr_list
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200195
196_cache_as_ram_setup_end: