blob: 5dc2bbd295c632a5d1b4fdeaf1550b664fde2143 [file] [log] [blame]
Arthur Heymans7a8205b2018-06-03 10:29:07 +02001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2000,2007 Ronald G. Minnich <rminnich@gmail.com>
5 * Copyright (C) 2007-2008 coresystems GmbH
Arthur Heymans3aa9adb2018-06-03 11:02:54 +02006 * Copyright (C) 2012 Kyösti Mälkki <kyosti.malkki@gmail.com>
Arthur Heymans7a8205b2018-06-03 10:29:07 +02007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
18#include <cpu/x86/mtrr.h>
19#include <cpu/x86/cache.h>
20#include <cpu/x86/post_code.h>
21
Arthur Heymans7a8205b2018-06-03 10:29:07 +020022#define CACHE_AS_RAM_SIZE CONFIG_DCACHE_RAM_SIZE
23#define CACHE_AS_RAM_BASE CONFIG_DCACHE_RAM_BASE
24
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +020025.global bootblock_pre_c_entry
26
Arthur Heymans7a8205b2018-06-03 10:29:07 +020027.code32
28_cache_as_ram_setup:
29
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +020030bootblock_pre_c_entry:
Arthur Heymans7a8205b2018-06-03 10:29:07 +020031
32cache_as_ram:
33 post_code(0x20)
34
35 /* Send INIT IPI to all excluding ourself. */
36 movl $0x000C4500, %eax
37 movl $0xFEE00300, %esi
38 movl %eax, (%esi)
39
Arthur Heymans3aa9adb2018-06-03 11:02:54 +020040 /* All CPUs need to be in Wait for SIPI state */
41wait_for_sipi:
42 movl (%esi), %eax
43 bt $12, %eax
44 jc wait_for_sipi
45
46 post_code(0x22)
47
48 /* Clear/disable fixed MTRRs */
49 mov $fixed_mtrr_list_size, %ebx
50 xor %eax, %eax
51 xor %edx, %edx
52
53clear_fixed_mtrr:
54 add $-2, %ebx
55 movzwl fixed_mtrr_list(%ebx), %ecx
Arthur Heymans7a8205b2018-06-03 10:29:07 +020056 wrmsr
Arthur Heymans3aa9adb2018-06-03 11:02:54 +020057 jnz clear_fixed_mtrr
58
Elyes HAOUAS02820ca2018-09-30 07:44:39 +020059 /* Figure out how many MTRRs we have, and clear them out */
Arthur Heymans3aa9adb2018-06-03 11:02:54 +020060 mov $MTRR_CAP_MSR, %ecx
61 rdmsr
62 movzb %al, %ebx /* Number of variable MTRRs */
63 mov $MTRR_PHYS_BASE(0), %ecx
64 xor %eax, %eax
65 xor %edx, %edx
66
67clear_var_mtrr:
68 wrmsr
69 inc %ecx
70 wrmsr
71 inc %ecx
72 dec %ebx
73 jnz clear_var_mtrr
Arthur Heymans7a8205b2018-06-03 10:29:07 +020074
75 post_code(0x22)
76 /* Configure the default memory type to uncacheable. */
77 movl $MTRR_DEF_TYPE_MSR, %ecx
78 rdmsr
79 andl $(~0x00000cff), %eax
80 wrmsr
81
Arthur Heymans3aa9adb2018-06-03 11:02:54 +020082 /* Determine CPU_ADDR_BITS and load PHYSMASK high word to %edx. */
83 movl $0x80000008, %eax
84 cpuid
85 movb %al, %cl
86 sub $32, %cl
87 movl $1, %edx
88 shl %cl, %edx
89 subl $1, %edx
90
91 /* Preload high word of address mask (in %edx) for Variable
92 MTRRs 0 and 1. */
93addrsize_set_high:
94 xorl %eax, %eax
95 movl $MTRR_PHYS_MASK(0), %ecx
96 wrmsr
97 movl $MTRR_PHYS_MASK(1), %ecx
98 wrmsr
99
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200100 post_code(0x23)
101 /* Set Cache-as-RAM base address. */
102 movl $(MTRR_PHYS_BASE(0)), %ecx
103 movl $(CACHE_AS_RAM_BASE | MTRR_TYPE_WRBACK), %eax
104 xorl %edx, %edx
105 wrmsr
106
107 post_code(0x24)
108 /* Set Cache-as-RAM mask. */
109 movl $(MTRR_PHYS_MASK(0)), %ecx
Arthur Heymans3aa9adb2018-06-03 11:02:54 +0200110 rdmsr
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200111 movl $(~(CACHE_AS_RAM_SIZE - 1) | MTRR_PHYS_MASK_VALID), %eax
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200112 wrmsr
113
114 post_code(0x25)
115
116 /* Enable MTRR. */
117 movl $MTRR_DEF_TYPE_MSR, %ecx
118 rdmsr
119 orl $MTRR_DEF_TYPE_EN, %eax
120 wrmsr
121
122 /* Enable L2 cache. */
123 movl $0x11e, %ecx
124 rdmsr
125 orl $(1 << 8), %eax
126 wrmsr
127
128 /* Enable cache (CR0.CD = 0, CR0.NW = 0). */
129 movl %cr0, %eax
130 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
131 invd
132 movl %eax, %cr0
133
134 /* Clear the cache memory region. This will also fill up the cache. */
135 movl $CACHE_AS_RAM_BASE, %esi
136 movl %esi, %edi
137 movl $(CACHE_AS_RAM_SIZE >> 2), %ecx
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200138 xorl %eax, %eax
139 rep stosl
140
141 post_code(0x26)
142 /* Enable Cache-as-RAM mode by disabling cache. */
143 movl %cr0, %eax
144 orl $CR0_CacheDisable, %eax
145 movl %eax, %cr0
146
147 /* Enable cache for our code in Flash because we do XIP here */
148 movl $MTRR_PHYS_BASE(1), %ecx
149 xorl %edx, %edx
150 /*
151 * IMPORTANT: The following calculation _must_ be done at runtime. See
Stefan Taunerde028782018-08-19 20:02:05 +0200152 * https://mail.coreboot.org/pipermail/coreboot/2010-October/060922.html
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200153 */
Kyösti Mälkkice9f4222018-06-25 18:53:36 +0300154 movl $_program, %eax
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200155 andl $(~(CONFIG_XIP_ROM_SIZE - 1)), %eax
156 orl $MTRR_TYPE_WRPROT, %eax
157 wrmsr
158
159 movl $MTRR_PHYS_MASK(1), %ecx
Arthur Heymans3aa9adb2018-06-03 11:02:54 +0200160 rdmsr
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200161 movl $(~(CONFIG_XIP_ROM_SIZE - 1) | MTRR_PHYS_MASK_VALID), %eax
162 wrmsr
163
164 post_code(0x28)
165 /* Enable cache. */
166 movl %cr0, %eax
167 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
168 movl %eax, %cr0
169
170 /* Setup the stack. */
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200171 mov $_car_stack_end, %esp
172
173 /* Need to align stack to 16 bytes at call instruction. Account for
174 the pushes below. */
Arthur Heymans348b79f2018-06-03 17:14:19 +0200175 andl $0xfffffff0, %esp
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200176 subl $4, %esp
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200177
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200178 /* push TSC and BIST to stack */
179 movd %mm0, %eax
Elyes HAOUAS87930b32019-01-16 12:41:57 +0100180 pushl %eax /* BIST */
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200181 movd %mm2, %eax
182 pushl %eax /* tsc[63:32] */
183 movd %mm1, %eax
Elyes HAOUAS87930b32019-01-16 12:41:57 +0100184 pushl %eax /* tsc[31:0] */
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200185
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200186before_c_entry:
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200187 post_code(0x29)
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200188 call bootblock_c_entry_bist
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200189
190 /* Should never see this postcode */
191 post_code(POST_DEAD_CODE)
192
193.Lhlt:
194 hlt
195 jmp .Lhlt
196
Arthur Heymans3aa9adb2018-06-03 11:02:54 +0200197fixed_mtrr_list:
198 .word MTRR_FIX_64K_00000
199 .word MTRR_FIX_16K_80000
200 .word MTRR_FIX_16K_A0000
201 .word MTRR_FIX_4K_C0000
202 .word MTRR_FIX_4K_C8000
203 .word MTRR_FIX_4K_D0000
204 .word MTRR_FIX_4K_D8000
205 .word MTRR_FIX_4K_E0000
206 .word MTRR_FIX_4K_E8000
207 .word MTRR_FIX_4K_F0000
208 .word MTRR_FIX_4K_F8000
209fixed_mtrr_list_size = . - fixed_mtrr_list
Arthur Heymans7a8205b2018-06-03 10:29:07 +0200210
211_cache_as_ram_setup_end: