blob: 2c6720715411e4dc7ae5bd9dc443f15761c4919c [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älkki7522a8f2020-11-20 16:47:38 +02007.section .init
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +02008.global bootblock_pre_c_entry
9
Arthur Heymans7a8205b2018-06-03 10:29:07 +020010.code32
11_cache_as_ram_setup:
12
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +020013bootblock_pre_c_entry:
Arthur Heymans7a8205b2018-06-03 10:29:07 +020014
15cache_as_ram:
16 post_code(0x20)
17
18 /* Send INIT IPI to all excluding ourself. */
19 movl $0x000C4500, %eax
20 movl $0xFEE00300, %esi
21 movl %eax, (%esi)
22
Arthur Heymans3aa9adb2018-06-03 11:02:54 +020023 /* All CPUs need to be in Wait for SIPI state */
24wait_for_sipi:
25 movl (%esi), %eax
26 bt $12, %eax
27 jc wait_for_sipi
28
29 post_code(0x22)
30
31 /* Clear/disable fixed MTRRs */
32 mov $fixed_mtrr_list_size, %ebx
33 xor %eax, %eax
34 xor %edx, %edx
35
36clear_fixed_mtrr:
37 add $-2, %ebx
38 movzwl fixed_mtrr_list(%ebx), %ecx
Arthur Heymans7a8205b2018-06-03 10:29:07 +020039 wrmsr
Arthur Heymans3aa9adb2018-06-03 11:02:54 +020040 jnz clear_fixed_mtrr
41
Elyes HAOUAS02820ca2018-09-30 07:44:39 +020042 /* Figure out how many MTRRs we have, and clear them out */
Arthur Heymans3aa9adb2018-06-03 11:02:54 +020043 mov $MTRR_CAP_MSR, %ecx
44 rdmsr
45 movzb %al, %ebx /* Number of variable MTRRs */
46 mov $MTRR_PHYS_BASE(0), %ecx
47 xor %eax, %eax
48 xor %edx, %edx
49
50clear_var_mtrr:
51 wrmsr
52 inc %ecx
53 wrmsr
54 inc %ecx
55 dec %ebx
56 jnz clear_var_mtrr
Arthur Heymans7a8205b2018-06-03 10:29:07 +020057
58 post_code(0x22)
59 /* Configure the default memory type to uncacheable. */
60 movl $MTRR_DEF_TYPE_MSR, %ecx
61 rdmsr
62 andl $(~0x00000cff), %eax
63 wrmsr
64
Arthur Heymans3aa9adb2018-06-03 11:02:54 +020065 /* Determine CPU_ADDR_BITS and load PHYSMASK high word to %edx. */
66 movl $0x80000008, %eax
67 cpuid
68 movb %al, %cl
69 sub $32, %cl
70 movl $1, %edx
71 shl %cl, %edx
72 subl $1, %edx
73
74 /* Preload high word of address mask (in %edx) for Variable
75 MTRRs 0 and 1. */
76addrsize_set_high:
77 xorl %eax, %eax
78 movl $MTRR_PHYS_MASK(0), %ecx
79 wrmsr
80 movl $MTRR_PHYS_MASK(1), %ecx
81 wrmsr
82
Arthur Heymans7a8205b2018-06-03 10:29:07 +020083 post_code(0x23)
84 /* Set Cache-as-RAM base address. */
85 movl $(MTRR_PHYS_BASE(0)), %ecx
Kyösti Mälkkidc6bb6c2019-11-08 00:08:55 +020086 movl $_car_mtrr_start, %eax
87 orl $MTRR_TYPE_WRBACK, %eax
Arthur Heymans7a8205b2018-06-03 10:29:07 +020088 xorl %edx, %edx
89 wrmsr
90
91 post_code(0x24)
92 /* Set Cache-as-RAM mask. */
93 movl $(MTRR_PHYS_MASK(0)), %ecx
Arthur Heymans3aa9adb2018-06-03 11:02:54 +020094 rdmsr
Kyösti Mälkkidc6bb6c2019-11-08 00:08:55 +020095 movl $_car_mtrr_mask, %eax
96 orl $MTRR_PHYS_MASK_VALID, %eax
Arthur Heymans7a8205b2018-06-03 10:29:07 +020097 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 L2 cache. */
108 movl $0x11e, %ecx
109 rdmsr
110 orl $(1 << 8), %eax
111 wrmsr
112
113 /* Enable cache (CR0.CD = 0, CR0.NW = 0). */
114 movl %cr0, %eax
115 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
116 invd
117 movl %eax, %cr0
118
119 /* Clear the cache memory region. This will also fill up the cache. */
Kyösti Mälkkidc6bb6c2019-11-08 00:08:55 +0200120 cld
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200121 xorl %eax, %eax
Kyösti Mälkkidc6bb6c2019-11-08 00:08:55 +0200122 movl $_car_mtrr_start, %edi
123 movl $_car_mtrr_size, %ecx
124 shr $2, %ecx
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200125 rep stosl
126
127 post_code(0x26)
128 /* Enable Cache-as-RAM mode by disabling cache. */
129 movl %cr0, %eax
130 orl $CR0_CacheDisable, %eax
131 movl %eax, %cr0
132
133 /* Enable cache for our code in Flash because we do XIP here */
134 movl $MTRR_PHYS_BASE(1), %ecx
135 xorl %edx, %edx
Kyösti Mälkkice9f4222018-06-25 18:53:36 +0300136 movl $_program, %eax
Kyösti Mälkkidc6bb6c2019-11-08 00:08:55 +0200137 andl $_xip_mtrr_mask, %eax
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200138 orl $MTRR_TYPE_WRPROT, %eax
139 wrmsr
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200140 movl $MTRR_PHYS_MASK(1), %ecx
Arthur Heymans3aa9adb2018-06-03 11:02:54 +0200141 rdmsr
Kyösti Mälkkidc6bb6c2019-11-08 00:08:55 +0200142 movl $_xip_mtrr_mask, %eax
143 orl $MTRR_PHYS_MASK_VALID, %eax
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200144 wrmsr
145
146 post_code(0x28)
147 /* Enable cache. */
148 movl %cr0, %eax
149 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
150 movl %eax, %cr0
151
152 /* Setup the stack. */
Arthur Heymansdf9cdcf2019-11-09 06:50:20 +0100153 mov $_ecar_stack, %esp
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200154
155 /* Need to align stack to 16 bytes at call instruction. Account for
156 the pushes below. */
Arthur Heymans348b79f2018-06-03 17:14:19 +0200157 andl $0xfffffff0, %esp
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200158 subl $4, %esp
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200159
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200160 /* push TSC and BIST to stack */
161 movd %mm0, %eax
Elyes HAOUAS87930b32019-01-16 12:41:57 +0100162 pushl %eax /* BIST */
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200163 movd %mm2, %eax
164 pushl %eax /* tsc[63:32] */
165 movd %mm1, %eax
Elyes HAOUAS87930b32019-01-16 12:41:57 +0100166 pushl %eax /* tsc[31:0] */
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200167
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200168before_c_entry:
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200169 post_code(0x29)
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200170 call bootblock_c_entry_bist
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200171
172 /* Should never see this postcode */
173 post_code(POST_DEAD_CODE)
174
175.Lhlt:
176 hlt
177 jmp .Lhlt
178
Arthur Heymans3aa9adb2018-06-03 11:02:54 +0200179fixed_mtrr_list:
180 .word MTRR_FIX_64K_00000
181 .word MTRR_FIX_16K_80000
182 .word MTRR_FIX_16K_A0000
183 .word MTRR_FIX_4K_C0000
184 .word MTRR_FIX_4K_C8000
185 .word MTRR_FIX_4K_D0000
186 .word MTRR_FIX_4K_D8000
187 .word MTRR_FIX_4K_E0000
188 .word MTRR_FIX_4K_E8000
189 .word MTRR_FIX_4K_F0000
190 .word MTRR_FIX_4K_F8000
191fixed_mtrr_list_size = . - fixed_mtrr_list
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200192
193_cache_as_ram_setup_end: