blob: 3c59ec285ee45cedef0a30ba320afda29a59ff76 [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
Arthur Heymans7a8205b2018-06-03 10:29:07 +02007#define CACHE_AS_RAM_SIZE CONFIG_DCACHE_RAM_SIZE
8#define CACHE_AS_RAM_BASE CONFIG_DCACHE_RAM_BASE
9
Arthur Heymans942ad6a2019-10-12 18:06:46 +020010#if ((CONFIG_C_ENV_BOOTBLOCK_SIZE & (CONFIG_C_ENV_BOOTBLOCK_SIZE - 1)) != 0)
11#error "CONFIG_C_ENV_BOOTBLOCK_SIZE must be a power of 2!"
12#endif
13#define XIP_ROM_SIZE CONFIG_C_ENV_BOOTBLOCK_SIZE
Arthur Heymans942ad6a2019-10-12 18:06:46 +020014
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +020015.global bootblock_pre_c_entry
16
Arthur Heymans7a8205b2018-06-03 10:29:07 +020017.code32
18_cache_as_ram_setup:
19
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +020020bootblock_pre_c_entry:
Arthur Heymans7a8205b2018-06-03 10:29:07 +020021
22cache_as_ram:
23 post_code(0x20)
24
25 /* Send INIT IPI to all excluding ourself. */
26 movl $0x000C4500, %eax
27 movl $0xFEE00300, %esi
28 movl %eax, (%esi)
29
Arthur Heymans3aa9adb2018-06-03 11:02:54 +020030 /* All CPUs need to be in Wait for SIPI state */
31wait_for_sipi:
32 movl (%esi), %eax
33 bt $12, %eax
34 jc wait_for_sipi
35
36 post_code(0x22)
37
38 /* Clear/disable fixed MTRRs */
39 mov $fixed_mtrr_list_size, %ebx
40 xor %eax, %eax
41 xor %edx, %edx
42
43clear_fixed_mtrr:
44 add $-2, %ebx
45 movzwl fixed_mtrr_list(%ebx), %ecx
Arthur Heymans7a8205b2018-06-03 10:29:07 +020046 wrmsr
Arthur Heymans3aa9adb2018-06-03 11:02:54 +020047 jnz clear_fixed_mtrr
48
Elyes HAOUAS02820ca2018-09-30 07:44:39 +020049 /* Figure out how many MTRRs we have, and clear them out */
Arthur Heymans3aa9adb2018-06-03 11:02:54 +020050 mov $MTRR_CAP_MSR, %ecx
51 rdmsr
52 movzb %al, %ebx /* Number of variable MTRRs */
53 mov $MTRR_PHYS_BASE(0), %ecx
54 xor %eax, %eax
55 xor %edx, %edx
56
57clear_var_mtrr:
58 wrmsr
59 inc %ecx
60 wrmsr
61 inc %ecx
62 dec %ebx
63 jnz clear_var_mtrr
Arthur Heymans7a8205b2018-06-03 10:29:07 +020064
65 post_code(0x22)
66 /* Configure the default memory type to uncacheable. */
67 movl $MTRR_DEF_TYPE_MSR, %ecx
68 rdmsr
69 andl $(~0x00000cff), %eax
70 wrmsr
71
Arthur Heymans3aa9adb2018-06-03 11:02:54 +020072 /* Determine CPU_ADDR_BITS and load PHYSMASK high word to %edx. */
73 movl $0x80000008, %eax
74 cpuid
75 movb %al, %cl
76 sub $32, %cl
77 movl $1, %edx
78 shl %cl, %edx
79 subl $1, %edx
80
81 /* Preload high word of address mask (in %edx) for Variable
82 MTRRs 0 and 1. */
83addrsize_set_high:
84 xorl %eax, %eax
85 movl $MTRR_PHYS_MASK(0), %ecx
86 wrmsr
87 movl $MTRR_PHYS_MASK(1), %ecx
88 wrmsr
89
Arthur Heymans7a8205b2018-06-03 10:29:07 +020090 post_code(0x23)
91 /* Set Cache-as-RAM base address. */
92 movl $(MTRR_PHYS_BASE(0)), %ecx
93 movl $(CACHE_AS_RAM_BASE | MTRR_TYPE_WRBACK), %eax
94 xorl %edx, %edx
95 wrmsr
96
97 post_code(0x24)
98 /* Set Cache-as-RAM mask. */
99 movl $(MTRR_PHYS_MASK(0)), %ecx
Arthur Heymans3aa9adb2018-06-03 11:02:54 +0200100 rdmsr
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200101 movl $(~(CACHE_AS_RAM_SIZE - 1) | MTRR_PHYS_MASK_VALID), %eax
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200102 wrmsr
103
104 post_code(0x25)
105
106 /* Enable MTRR. */
107 movl $MTRR_DEF_TYPE_MSR, %ecx
108 rdmsr
109 orl $MTRR_DEF_TYPE_EN, %eax
110 wrmsr
111
112 /* Enable L2 cache. */
113 movl $0x11e, %ecx
114 rdmsr
115 orl $(1 << 8), %eax
116 wrmsr
117
118 /* Enable cache (CR0.CD = 0, CR0.NW = 0). */
119 movl %cr0, %eax
120 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
121 invd
122 movl %eax, %cr0
123
124 /* Clear the cache memory region. This will also fill up the cache. */
125 movl $CACHE_AS_RAM_BASE, %esi
126 movl %esi, %edi
127 movl $(CACHE_AS_RAM_SIZE >> 2), %ecx
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200128 xorl %eax, %eax
129 rep stosl
130
131 post_code(0x26)
132 /* Enable Cache-as-RAM mode by disabling cache. */
133 movl %cr0, %eax
134 orl $CR0_CacheDisable, %eax
135 movl %eax, %cr0
136
137 /* Enable cache for our code in Flash because we do XIP here */
138 movl $MTRR_PHYS_BASE(1), %ecx
139 xorl %edx, %edx
140 /*
141 * IMPORTANT: The following calculation _must_ be done at runtime. See
Stefan Taunerde028782018-08-19 20:02:05 +0200142 * https://mail.coreboot.org/pipermail/coreboot/2010-October/060922.html
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200143 */
Kyösti Mälkkice9f4222018-06-25 18:53:36 +0300144 movl $_program, %eax
Arthur Heymans942ad6a2019-10-12 18:06:46 +0200145 andl $(~(XIP_ROM_SIZE - 1)), %eax
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200146 orl $MTRR_TYPE_WRPROT, %eax
147 wrmsr
148
149 movl $MTRR_PHYS_MASK(1), %ecx
Arthur Heymans3aa9adb2018-06-03 11:02:54 +0200150 rdmsr
Arthur Heymans942ad6a2019-10-12 18:06:46 +0200151 movl $(~(XIP_ROM_SIZE - 1) | MTRR_PHYS_MASK_VALID), %eax
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200152 wrmsr
153
154 post_code(0x28)
155 /* Enable cache. */
156 movl %cr0, %eax
157 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
158 movl %eax, %cr0
159
160 /* Setup the stack. */
Arthur Heymansdf9cdcf2019-11-09 06:50:20 +0100161 mov $_ecar_stack, %esp
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200162
163 /* Need to align stack to 16 bytes at call instruction. Account for
164 the pushes below. */
Arthur Heymans348b79f2018-06-03 17:14:19 +0200165 andl $0xfffffff0, %esp
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200166 subl $4, %esp
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200167
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200168 /* push TSC and BIST to stack */
169 movd %mm0, %eax
Elyes HAOUAS87930b32019-01-16 12:41:57 +0100170 pushl %eax /* BIST */
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200171 movd %mm2, %eax
172 pushl %eax /* tsc[63:32] */
173 movd %mm1, %eax
Elyes HAOUAS87930b32019-01-16 12:41:57 +0100174 pushl %eax /* tsc[31:0] */
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200175
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200176before_c_entry:
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200177 post_code(0x29)
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200178 call bootblock_c_entry_bist
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200179
180 /* Should never see this postcode */
181 post_code(POST_DEAD_CODE)
182
183.Lhlt:
184 hlt
185 jmp .Lhlt
186
Arthur Heymans3aa9adb2018-06-03 11:02:54 +0200187fixed_mtrr_list:
188 .word MTRR_FIX_64K_00000
189 .word MTRR_FIX_16K_80000
190 .word MTRR_FIX_16K_A0000
191 .word MTRR_FIX_4K_C0000
192 .word MTRR_FIX_4K_C8000
193 .word MTRR_FIX_4K_D0000
194 .word MTRR_FIX_4K_D8000
195 .word MTRR_FIX_4K_E0000
196 .word MTRR_FIX_4K_E8000
197 .word MTRR_FIX_4K_F0000
198 .word MTRR_FIX_4K_F8000
199fixed_mtrr_list_size = . - fixed_mtrr_list
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200200
201_cache_as_ram_setup_end: