blob: 73618d92f605147de4cdacdca2affbb60ce0194b [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_ENV_BOOTBLOCK_SIZE & (CONFIG_C_ENV_BOOTBLOCK_SIZE - 1)) != 0)
22#error "CONFIG_C_ENV_BOOTBLOCK_SIZE must be a power of 2!"
23#endif
24#define XIP_ROM_SIZE CONFIG_C_ENV_BOOTBLOCK_SIZE
Arthur Heymans942ad6a2019-10-12 18:06:46 +020025
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +020026.global bootblock_pre_c_entry
27
Arthur Heymans7a8205b2018-06-03 10:29:07 +020028.code32
29_cache_as_ram_setup:
30
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +020031bootblock_pre_c_entry:
Arthur Heymans7a8205b2018-06-03 10:29:07 +020032
33cache_as_ram:
34 post_code(0x20)
35
36 /* Send INIT IPI to all excluding ourself. */
37 movl $0x000C4500, %eax
38 movl $0xFEE00300, %esi
39 movl %eax, (%esi)
40
Arthur Heymans3aa9adb2018-06-03 11:02:54 +020041 /* All CPUs need to be in Wait for SIPI state */
42wait_for_sipi:
43 movl (%esi), %eax
44 bt $12, %eax
45 jc wait_for_sipi
46
47 post_code(0x22)
48
49 /* Clear/disable fixed MTRRs */
50 mov $fixed_mtrr_list_size, %ebx
51 xor %eax, %eax
52 xor %edx, %edx
53
54clear_fixed_mtrr:
55 add $-2, %ebx
56 movzwl fixed_mtrr_list(%ebx), %ecx
Arthur Heymans7a8205b2018-06-03 10:29:07 +020057 wrmsr
Arthur Heymans3aa9adb2018-06-03 11:02:54 +020058 jnz clear_fixed_mtrr
59
Elyes HAOUAS02820ca2018-09-30 07:44:39 +020060 /* Figure out how many MTRRs we have, and clear them out */
Arthur Heymans3aa9adb2018-06-03 11:02:54 +020061 mov $MTRR_CAP_MSR, %ecx
62 rdmsr
63 movzb %al, %ebx /* Number of variable MTRRs */
64 mov $MTRR_PHYS_BASE(0), %ecx
65 xor %eax, %eax
66 xor %edx, %edx
67
68clear_var_mtrr:
69 wrmsr
70 inc %ecx
71 wrmsr
72 inc %ecx
73 dec %ebx
74 jnz clear_var_mtrr
Arthur Heymans7a8205b2018-06-03 10:29:07 +020075
76 post_code(0x22)
77 /* Configure the default memory type to uncacheable. */
78 movl $MTRR_DEF_TYPE_MSR, %ecx
79 rdmsr
80 andl $(~0x00000cff), %eax
81 wrmsr
82
Arthur Heymans3aa9adb2018-06-03 11:02:54 +020083 /* Determine CPU_ADDR_BITS and load PHYSMASK high word to %edx. */
84 movl $0x80000008, %eax
85 cpuid
86 movb %al, %cl
87 sub $32, %cl
88 movl $1, %edx
89 shl %cl, %edx
90 subl $1, %edx
91
92 /* Preload high word of address mask (in %edx) for Variable
93 MTRRs 0 and 1. */
94addrsize_set_high:
95 xorl %eax, %eax
96 movl $MTRR_PHYS_MASK(0), %ecx
97 wrmsr
98 movl $MTRR_PHYS_MASK(1), %ecx
99 wrmsr
100
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200101 post_code(0x23)
102 /* Set Cache-as-RAM base address. */
103 movl $(MTRR_PHYS_BASE(0)), %ecx
104 movl $(CACHE_AS_RAM_BASE | MTRR_TYPE_WRBACK), %eax
105 xorl %edx, %edx
106 wrmsr
107
108 post_code(0x24)
109 /* Set Cache-as-RAM mask. */
110 movl $(MTRR_PHYS_MASK(0)), %ecx
Arthur Heymans3aa9adb2018-06-03 11:02:54 +0200111 rdmsr
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200112 movl $(~(CACHE_AS_RAM_SIZE - 1) | MTRR_PHYS_MASK_VALID), %eax
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200113 wrmsr
114
115 post_code(0x25)
116
117 /* Enable MTRR. */
118 movl $MTRR_DEF_TYPE_MSR, %ecx
119 rdmsr
120 orl $MTRR_DEF_TYPE_EN, %eax
121 wrmsr
122
123 /* Enable L2 cache. */
124 movl $0x11e, %ecx
125 rdmsr
126 orl $(1 << 8), %eax
127 wrmsr
128
129 /* Enable cache (CR0.CD = 0, CR0.NW = 0). */
130 movl %cr0, %eax
131 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
132 invd
133 movl %eax, %cr0
134
135 /* Clear the cache memory region. This will also fill up the cache. */
136 movl $CACHE_AS_RAM_BASE, %esi
137 movl %esi, %edi
138 movl $(CACHE_AS_RAM_SIZE >> 2), %ecx
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200139 xorl %eax, %eax
140 rep stosl
141
142 post_code(0x26)
143 /* Enable Cache-as-RAM mode by disabling cache. */
144 movl %cr0, %eax
145 orl $CR0_CacheDisable, %eax
146 movl %eax, %cr0
147
148 /* Enable cache for our code in Flash because we do XIP here */
149 movl $MTRR_PHYS_BASE(1), %ecx
150 xorl %edx, %edx
151 /*
152 * IMPORTANT: The following calculation _must_ be done at runtime. See
Stefan Taunerde028782018-08-19 20:02:05 +0200153 * https://mail.coreboot.org/pipermail/coreboot/2010-October/060922.html
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200154 */
Kyösti Mälkkice9f4222018-06-25 18:53:36 +0300155 movl $_program, %eax
Arthur Heymans942ad6a2019-10-12 18:06:46 +0200156 andl $(~(XIP_ROM_SIZE - 1)), %eax
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200157 orl $MTRR_TYPE_WRPROT, %eax
158 wrmsr
159
160 movl $MTRR_PHYS_MASK(1), %ecx
Arthur Heymans3aa9adb2018-06-03 11:02:54 +0200161 rdmsr
Arthur Heymans942ad6a2019-10-12 18:06:46 +0200162 movl $(~(XIP_ROM_SIZE - 1) | MTRR_PHYS_MASK_VALID), %eax
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200163 wrmsr
164
165 post_code(0x28)
166 /* Enable cache. */
167 movl %cr0, %eax
168 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
169 movl %eax, %cr0
170
171 /* Setup the stack. */
Arthur Heymansdf9cdcf2019-11-09 06:50:20 +0100172 mov $_ecar_stack, %esp
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200173
174 /* Need to align stack to 16 bytes at call instruction. Account for
175 the pushes below. */
Arthur Heymans348b79f2018-06-03 17:14:19 +0200176 andl $0xfffffff0, %esp
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200177 subl $4, %esp
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200178
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200179 /* push TSC and BIST to stack */
180 movd %mm0, %eax
Elyes HAOUAS87930b32019-01-16 12:41:57 +0100181 pushl %eax /* BIST */
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200182 movd %mm2, %eax
183 pushl %eax /* tsc[63:32] */
184 movd %mm1, %eax
Elyes HAOUAS87930b32019-01-16 12:41:57 +0100185 pushl %eax /* tsc[31:0] */
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200186
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200187before_c_entry:
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200188 post_code(0x29)
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200189 call bootblock_c_entry_bist
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200190
191 /* Should never see this postcode */
192 post_code(POST_DEAD_CODE)
193
194.Lhlt:
195 hlt
196 jmp .Lhlt
197
Arthur Heymans3aa9adb2018-06-03 11:02:54 +0200198fixed_mtrr_list:
199 .word MTRR_FIX_64K_00000
200 .word MTRR_FIX_16K_80000
201 .word MTRR_FIX_16K_A0000
202 .word MTRR_FIX_4K_C0000
203 .word MTRR_FIX_4K_C8000
204 .word MTRR_FIX_4K_D0000
205 .word MTRR_FIX_4K_D8000
206 .word MTRR_FIX_4K_E0000
207 .word MTRR_FIX_4K_E8000
208 .word MTRR_FIX_4K_F0000
209 .word MTRR_FIX_4K_F8000
210fixed_mtrr_list_size = . - fixed_mtrr_list
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200211
212_cache_as_ram_setup_end: