blob: 0e0fa77bb06ae7f1b033b33d471525c3aee8c06c [file] [log] [blame]
Arthur Heymans7a8205b2018-06-03 10:29:07 +02001/*
2 * This file is part of the coreboot project.
3 *
Arthur Heymans7a8205b2018-06-03 10:29:07 +02004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#include <cpu/x86/mtrr.h>
15#include <cpu/x86/cache.h>
16#include <cpu/x86/post_code.h>
17
Arthur Heymans7a8205b2018-06-03 10:29:07 +020018#define CACHE_AS_RAM_SIZE CONFIG_DCACHE_RAM_SIZE
19#define CACHE_AS_RAM_BASE CONFIG_DCACHE_RAM_BASE
20
Arthur Heymans942ad6a2019-10-12 18:06:46 +020021#if CONFIG(C_ENVIRONMENT_BOOTBLOCK)
22#if ((CONFIG_C_ENV_BOOTBLOCK_SIZE & (CONFIG_C_ENV_BOOTBLOCK_SIZE - 1)) != 0)
23#error "CONFIG_C_ENV_BOOTBLOCK_SIZE must be a power of 2!"
24#endif
25#define XIP_ROM_SIZE CONFIG_C_ENV_BOOTBLOCK_SIZE
26#else
27#define XIP_ROM_SIZE CONFIG_XIP_ROM_SIZE
28#endif
29
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +020030.global bootblock_pre_c_entry
31
Arthur Heymans7a8205b2018-06-03 10:29:07 +020032.code32
33_cache_as_ram_setup:
34
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +020035bootblock_pre_c_entry:
Arthur Heymans7a8205b2018-06-03 10:29:07 +020036
37cache_as_ram:
38 post_code(0x20)
39
40 /* Send INIT IPI to all excluding ourself. */
41 movl $0x000C4500, %eax
42 movl $0xFEE00300, %esi
43 movl %eax, (%esi)
44
Arthur Heymans3aa9adb2018-06-03 11:02:54 +020045 /* All CPUs need to be in Wait for SIPI state */
46wait_for_sipi:
47 movl (%esi), %eax
48 bt $12, %eax
49 jc wait_for_sipi
50
51 post_code(0x22)
52
53 /* Clear/disable fixed MTRRs */
54 mov $fixed_mtrr_list_size, %ebx
55 xor %eax, %eax
56 xor %edx, %edx
57
58clear_fixed_mtrr:
59 add $-2, %ebx
60 movzwl fixed_mtrr_list(%ebx), %ecx
Arthur Heymans7a8205b2018-06-03 10:29:07 +020061 wrmsr
Arthur Heymans3aa9adb2018-06-03 11:02:54 +020062 jnz clear_fixed_mtrr
63
Elyes HAOUAS02820ca2018-09-30 07:44:39 +020064 /* Figure out how many MTRRs we have, and clear them out */
Arthur Heymans3aa9adb2018-06-03 11:02:54 +020065 mov $MTRR_CAP_MSR, %ecx
66 rdmsr
67 movzb %al, %ebx /* Number of variable MTRRs */
68 mov $MTRR_PHYS_BASE(0), %ecx
69 xor %eax, %eax
70 xor %edx, %edx
71
72clear_var_mtrr:
73 wrmsr
74 inc %ecx
75 wrmsr
76 inc %ecx
77 dec %ebx
78 jnz clear_var_mtrr
Arthur Heymans7a8205b2018-06-03 10:29:07 +020079
80 post_code(0x22)
81 /* Configure the default memory type to uncacheable. */
82 movl $MTRR_DEF_TYPE_MSR, %ecx
83 rdmsr
84 andl $(~0x00000cff), %eax
85 wrmsr
86
Arthur Heymans3aa9adb2018-06-03 11:02:54 +020087 /* Determine CPU_ADDR_BITS and load PHYSMASK high word to %edx. */
88 movl $0x80000008, %eax
89 cpuid
90 movb %al, %cl
91 sub $32, %cl
92 movl $1, %edx
93 shl %cl, %edx
94 subl $1, %edx
95
96 /* Preload high word of address mask (in %edx) for Variable
97 MTRRs 0 and 1. */
98addrsize_set_high:
99 xorl %eax, %eax
100 movl $MTRR_PHYS_MASK(0), %ecx
101 wrmsr
102 movl $MTRR_PHYS_MASK(1), %ecx
103 wrmsr
104
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200105 post_code(0x23)
106 /* Set Cache-as-RAM base address. */
107 movl $(MTRR_PHYS_BASE(0)), %ecx
108 movl $(CACHE_AS_RAM_BASE | MTRR_TYPE_WRBACK), %eax
109 xorl %edx, %edx
110 wrmsr
111
112 post_code(0x24)
113 /* Set Cache-as-RAM mask. */
114 movl $(MTRR_PHYS_MASK(0)), %ecx
Arthur Heymans3aa9adb2018-06-03 11:02:54 +0200115 rdmsr
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200116 movl $(~(CACHE_AS_RAM_SIZE - 1) | MTRR_PHYS_MASK_VALID), %eax
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200117 wrmsr
118
119 post_code(0x25)
120
121 /* Enable MTRR. */
122 movl $MTRR_DEF_TYPE_MSR, %ecx
123 rdmsr
124 orl $MTRR_DEF_TYPE_EN, %eax
125 wrmsr
126
127 /* Enable L2 cache. */
128 movl $0x11e, %ecx
129 rdmsr
130 orl $(1 << 8), %eax
131 wrmsr
132
133 /* Enable cache (CR0.CD = 0, CR0.NW = 0). */
134 movl %cr0, %eax
135 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
136 invd
137 movl %eax, %cr0
138
139 /* Clear the cache memory region. This will also fill up the cache. */
140 movl $CACHE_AS_RAM_BASE, %esi
141 movl %esi, %edi
142 movl $(CACHE_AS_RAM_SIZE >> 2), %ecx
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200143 xorl %eax, %eax
144 rep stosl
145
146 post_code(0x26)
147 /* Enable Cache-as-RAM mode by disabling cache. */
148 movl %cr0, %eax
149 orl $CR0_CacheDisable, %eax
150 movl %eax, %cr0
151
152 /* Enable cache for our code in Flash because we do XIP here */
153 movl $MTRR_PHYS_BASE(1), %ecx
154 xorl %edx, %edx
155 /*
156 * IMPORTANT: The following calculation _must_ be done at runtime. See
Stefan Taunerde028782018-08-19 20:02:05 +0200157 * https://mail.coreboot.org/pipermail/coreboot/2010-October/060922.html
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200158 */
Kyösti Mälkkice9f4222018-06-25 18:53:36 +0300159 movl $_program, %eax
Arthur Heymans942ad6a2019-10-12 18:06:46 +0200160 andl $(~(XIP_ROM_SIZE - 1)), %eax
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200161 orl $MTRR_TYPE_WRPROT, %eax
162 wrmsr
163
164 movl $MTRR_PHYS_MASK(1), %ecx
Arthur Heymans3aa9adb2018-06-03 11:02:54 +0200165 rdmsr
Arthur Heymans942ad6a2019-10-12 18:06:46 +0200166 movl $(~(XIP_ROM_SIZE - 1) | MTRR_PHYS_MASK_VALID), %eax
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200167 wrmsr
168
169 post_code(0x28)
170 /* Enable cache. */
171 movl %cr0, %eax
172 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
173 movl %eax, %cr0
174
175 /* Setup the stack. */
Arthur Heymansdf9cdcf2019-11-09 06:50:20 +0100176 mov $_ecar_stack, %esp
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200177
178 /* Need to align stack to 16 bytes at call instruction. Account for
179 the pushes below. */
Arthur Heymans348b79f2018-06-03 17:14:19 +0200180 andl $0xfffffff0, %esp
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200181 subl $4, %esp
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200182
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200183 /* push TSC and BIST to stack */
184 movd %mm0, %eax
Elyes HAOUAS87930b32019-01-16 12:41:57 +0100185 pushl %eax /* BIST */
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200186 movd %mm2, %eax
187 pushl %eax /* tsc[63:32] */
188 movd %mm1, %eax
Elyes HAOUAS87930b32019-01-16 12:41:57 +0100189 pushl %eax /* tsc[31:0] */
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200190
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200191before_c_entry:
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200192 post_code(0x29)
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200193 call bootblock_c_entry_bist
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200194
195 /* Should never see this postcode */
196 post_code(POST_DEAD_CODE)
197
198.Lhlt:
199 hlt
200 jmp .Lhlt
201
Arthur Heymans3aa9adb2018-06-03 11:02:54 +0200202fixed_mtrr_list:
203 .word MTRR_FIX_64K_00000
204 .word MTRR_FIX_16K_80000
205 .word MTRR_FIX_16K_A0000
206 .word MTRR_FIX_4K_C0000
207 .word MTRR_FIX_4K_C8000
208 .word MTRR_FIX_4K_D0000
209 .word MTRR_FIX_4K_D8000
210 .word MTRR_FIX_4K_E0000
211 .word MTRR_FIX_4K_E8000
212 .word MTRR_FIX_4K_F0000
213 .word MTRR_FIX_4K_F8000
214fixed_mtrr_list_size = . - fixed_mtrr_list
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200215
216_cache_as_ram_setup_end: