blob: 8fa7056343396155a0b6d1dabbcddf100c23e332 [file] [log] [blame]
Angel Ponsf23ae0b2020-04-02 23:48:12 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Arthur Heymans7a8205b2018-06-03 10:29:07 +02003
4#include <cpu/x86/mtrr.h>
5#include <cpu/x86/cache.h>
6#include <cpu/x86/post_code.h>
7
Arthur Heymans7a8205b2018-06-03 10:29:07 +02008#define CACHE_AS_RAM_SIZE CONFIG_DCACHE_RAM_SIZE
9#define CACHE_AS_RAM_BASE CONFIG_DCACHE_RAM_BASE
10
Arthur Heymans942ad6a2019-10-12 18:06:46 +020011#if ((CONFIG_C_ENV_BOOTBLOCK_SIZE & (CONFIG_C_ENV_BOOTBLOCK_SIZE - 1)) != 0)
12#error "CONFIG_C_ENV_BOOTBLOCK_SIZE must be a power of 2!"
13#endif
14#define XIP_ROM_SIZE CONFIG_C_ENV_BOOTBLOCK_SIZE
Arthur Heymans942ad6a2019-10-12 18:06:46 +020015
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +020016.global bootblock_pre_c_entry
17
Arthur Heymans7a8205b2018-06-03 10:29:07 +020018.code32
19_cache_as_ram_setup:
20
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +020021bootblock_pre_c_entry:
Arthur Heymans7a8205b2018-06-03 10:29:07 +020022
23cache_as_ram:
24 post_code(0x20)
25
26 /* Send INIT IPI to all excluding ourself. */
27 movl $0x000C4500, %eax
28 movl $0xFEE00300, %esi
29 movl %eax, (%esi)
30
Arthur Heymans3aa9adb2018-06-03 11:02:54 +020031 /* All CPUs need to be in Wait for SIPI state */
32wait_for_sipi:
33 movl (%esi), %eax
34 bt $12, %eax
35 jc wait_for_sipi
36
37 post_code(0x22)
38
39 /* Clear/disable fixed MTRRs */
40 mov $fixed_mtrr_list_size, %ebx
41 xor %eax, %eax
42 xor %edx, %edx
43
44clear_fixed_mtrr:
45 add $-2, %ebx
46 movzwl fixed_mtrr_list(%ebx), %ecx
Arthur Heymans7a8205b2018-06-03 10:29:07 +020047 wrmsr
Arthur Heymans3aa9adb2018-06-03 11:02:54 +020048 jnz clear_fixed_mtrr
49
Elyes HAOUAS02820ca2018-09-30 07:44:39 +020050 /* Figure out how many MTRRs we have, and clear them out */
Arthur Heymans3aa9adb2018-06-03 11:02:54 +020051 mov $MTRR_CAP_MSR, %ecx
52 rdmsr
53 movzb %al, %ebx /* Number of variable MTRRs */
54 mov $MTRR_PHYS_BASE(0), %ecx
55 xor %eax, %eax
56 xor %edx, %edx
57
58clear_var_mtrr:
59 wrmsr
60 inc %ecx
61 wrmsr
62 inc %ecx
63 dec %ebx
64 jnz clear_var_mtrr
Arthur Heymans7a8205b2018-06-03 10:29:07 +020065
66 post_code(0x22)
67 /* Configure the default memory type to uncacheable. */
68 movl $MTRR_DEF_TYPE_MSR, %ecx
69 rdmsr
70 andl $(~0x00000cff), %eax
71 wrmsr
72
Arthur Heymans3aa9adb2018-06-03 11:02:54 +020073 /* Determine CPU_ADDR_BITS and load PHYSMASK high word to %edx. */
74 movl $0x80000008, %eax
75 cpuid
76 movb %al, %cl
77 sub $32, %cl
78 movl $1, %edx
79 shl %cl, %edx
80 subl $1, %edx
81
82 /* Preload high word of address mask (in %edx) for Variable
83 MTRRs 0 and 1. */
84addrsize_set_high:
85 xorl %eax, %eax
86 movl $MTRR_PHYS_MASK(0), %ecx
87 wrmsr
88 movl $MTRR_PHYS_MASK(1), %ecx
89 wrmsr
90
Arthur Heymans7a8205b2018-06-03 10:29:07 +020091 post_code(0x23)
92 /* Set Cache-as-RAM base address. */
93 movl $(MTRR_PHYS_BASE(0)), %ecx
94 movl $(CACHE_AS_RAM_BASE | MTRR_TYPE_WRBACK), %eax
95 xorl %edx, %edx
96 wrmsr
97
98 post_code(0x24)
99 /* Set Cache-as-RAM mask. */
100 movl $(MTRR_PHYS_MASK(0)), %ecx
Arthur Heymans3aa9adb2018-06-03 11:02:54 +0200101 rdmsr
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200102 movl $(~(CACHE_AS_RAM_SIZE - 1) | MTRR_PHYS_MASK_VALID), %eax
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200103 wrmsr
104
105 post_code(0x25)
106
107 /* Enable MTRR. */
108 movl $MTRR_DEF_TYPE_MSR, %ecx
109 rdmsr
110 orl $MTRR_DEF_TYPE_EN, %eax
111 wrmsr
112
113 /* Enable L2 cache. */
114 movl $0x11e, %ecx
115 rdmsr
116 orl $(1 << 8), %eax
117 wrmsr
118
119 /* Enable cache (CR0.CD = 0, CR0.NW = 0). */
120 movl %cr0, %eax
121 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
122 invd
123 movl %eax, %cr0
124
125 /* Clear the cache memory region. This will also fill up the cache. */
126 movl $CACHE_AS_RAM_BASE, %esi
127 movl %esi, %edi
128 movl $(CACHE_AS_RAM_SIZE >> 2), %ecx
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200129 xorl %eax, %eax
130 rep stosl
131
132 post_code(0x26)
133 /* Enable Cache-as-RAM mode by disabling cache. */
134 movl %cr0, %eax
135 orl $CR0_CacheDisable, %eax
136 movl %eax, %cr0
137
138 /* Enable cache for our code in Flash because we do XIP here */
139 movl $MTRR_PHYS_BASE(1), %ecx
140 xorl %edx, %edx
141 /*
142 * IMPORTANT: The following calculation _must_ be done at runtime. See
Stefan Taunerde028782018-08-19 20:02:05 +0200143 * https://mail.coreboot.org/pipermail/coreboot/2010-October/060922.html
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200144 */
Kyösti Mälkkice9f4222018-06-25 18:53:36 +0300145 movl $_program, %eax
Arthur Heymans942ad6a2019-10-12 18:06:46 +0200146 andl $(~(XIP_ROM_SIZE - 1)), %eax
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200147 orl $MTRR_TYPE_WRPROT, %eax
148 wrmsr
149
150 movl $MTRR_PHYS_MASK(1), %ecx
Arthur Heymans3aa9adb2018-06-03 11:02:54 +0200151 rdmsr
Arthur Heymans942ad6a2019-10-12 18:06:46 +0200152 movl $(~(XIP_ROM_SIZE - 1) | MTRR_PHYS_MASK_VALID), %eax
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200153 wrmsr
154
155 post_code(0x28)
156 /* Enable cache. */
157 movl %cr0, %eax
158 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
159 movl %eax, %cr0
160
161 /* Setup the stack. */
Arthur Heymansdf9cdcf2019-11-09 06:50:20 +0100162 mov $_ecar_stack, %esp
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200163
164 /* Need to align stack to 16 bytes at call instruction. Account for
165 the pushes below. */
Arthur Heymans348b79f2018-06-03 17:14:19 +0200166 andl $0xfffffff0, %esp
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200167 subl $4, %esp
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200168
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200169 /* push TSC and BIST to stack */
170 movd %mm0, %eax
Elyes HAOUAS87930b32019-01-16 12:41:57 +0100171 pushl %eax /* BIST */
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200172 movd %mm2, %eax
173 pushl %eax /* tsc[63:32] */
174 movd %mm1, %eax
Elyes HAOUAS87930b32019-01-16 12:41:57 +0100175 pushl %eax /* tsc[31:0] */
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200176
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200177before_c_entry:
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200178 post_code(0x29)
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200179 call bootblock_c_entry_bist
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200180
181 /* Should never see this postcode */
182 post_code(POST_DEAD_CODE)
183
184.Lhlt:
185 hlt
186 jmp .Lhlt
187
Arthur Heymans3aa9adb2018-06-03 11:02:54 +0200188fixed_mtrr_list:
189 .word MTRR_FIX_64K_00000
190 .word MTRR_FIX_16K_80000
191 .word MTRR_FIX_16K_A0000
192 .word MTRR_FIX_4K_C0000
193 .word MTRR_FIX_4K_C8000
194 .word MTRR_FIX_4K_D0000
195 .word MTRR_FIX_4K_D8000
196 .word MTRR_FIX_4K_E0000
197 .word MTRR_FIX_4K_E8000
198 .word MTRR_FIX_4K_F0000
199 .word MTRR_FIX_4K_F8000
200fixed_mtrr_list_size = . - fixed_mtrr_list
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200201
202_cache_as_ram_setup_end: