blob: 6f5076ff7f13914146dffe0ceb34687bb7238193 [file] [log] [blame]
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +02001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2000,2007 Ronald G. Minnich <rminnich@gmail.com>
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +02005 * Copyright (C) 2005 Tyan (written by Yinghai Lu for Tyan)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +02006 * Copyright (C) 2007-2008 coresystems GmbH
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +02007 * Copyright (C) 2012 Kyösti Mälkki <kyosti.malkki@gmail.com>
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +02008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2 of the License.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020017 */
18
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020019#include <cpu/x86/mtrr.h>
Patrick Georgi05e740f2012-03-31 12:52:21 +020020#include <cpu/x86/cache.h>
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020021#include <cpu/x86/post_code.h>
22
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020023#define CACHE_AS_RAM_SIZE CONFIG_DCACHE_RAM_SIZE
24#define CACHE_AS_RAM_BASE CONFIG_DCACHE_RAM_BASE
25
26 /* Save the BIST result. */
27 movl %eax, %ebp
28
29cache_as_ram:
30 post_code(0x20)
31
Kyösti Mälkki54d6a282018-05-25 06:03:14 +030032 /* Zero out all fixed range and variable range MTRRs. */
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020033 movl $mtrr_table, %esi
Stefan Reinauer4a45ec42015-07-07 00:54:05 +020034 movl $((mtrr_table_end - mtrr_table) >> 1), %edi
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020035 xorl %eax, %eax
36 xorl %edx, %edx
37clear_mtrrs:
38 movw (%esi), %bx
39 movzx %bx, %ecx
40 wrmsr
41 add $2, %esi
42 dec %edi
43 jnz clear_mtrrs
44
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +020045 post_code(0x21)
46
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020047 /* Configure the default memory type to uncacheable. */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070048 movl $MTRR_DEF_TYPE_MSR, %ecx
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020049 rdmsr
50 andl $(~0x00000cff), %eax
51 wrmsr
52
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +020053 post_code(0x22)
54
Kyösti Mälkki54d6a282018-05-25 06:03:14 +030055 /* Determine CPU_ADDR_BITS and load PHYSMASK high word to %edx. */
Kyösti Mälkkia860c682012-02-28 02:06:45 +020056 movl $1, %eax
57 cpuid
Elyes HAOUAS168ef392017-06-27 22:54:42 +020058 andl $(1 << 6 | 1 << 17), %edx /* PAE or PSE36 */
Kyösti Mälkkia860c682012-02-28 02:06:45 +020059 jz addrsize_set_high
60 movl $0x0f, %edx
61
62 /* Preload high word of address mask (in %edx) for Variable
Kyösti Mälkki54d6a282018-05-25 06:03:14 +030063 MTRRs 0 and 1. */
Kyösti Mälkkia860c682012-02-28 02:06:45 +020064addrsize_set_high:
65 xorl %eax, %eax
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070066 movl $MTRR_PHYS_MASK(0), %ecx
Kyösti Mälkkia860c682012-02-28 02:06:45 +020067 wrmsr
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070068 movl $MTRR_PHYS_MASK(1), %ecx
Kyösti Mälkkia860c682012-02-28 02:06:45 +020069 wrmsr
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +020070
71 post_code(0x2a)
72
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020073 /* Set Cache-as-RAM base address. */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070074 movl $(MTRR_PHYS_BASE(0)), %ecx
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020075 movl $(CACHE_AS_RAM_BASE | MTRR_TYPE_WRBACK), %eax
76 xorl %edx, %edx
77 wrmsr
78
79 /* Set Cache-as-RAM mask. */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070080 movl $(MTRR_PHYS_MASK(0)), %ecx
Kyösti Mälkkia860c682012-02-28 02:06:45 +020081 rdmsr
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070082 movl $(~(CACHE_AS_RAM_SIZE - 1) | MTRR_PHYS_MASK_VALID), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020083 wrmsr
84
Kyösti Mälkki8a2f1672016-07-20 13:29:59 +030085 post_code(0x2b)
86
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020087 /* Enable MTRR. */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070088 movl $MTRR_DEF_TYPE_MSR, %ecx
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020089 rdmsr
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070090 orl $MTRR_DEF_TYPE_EN, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020091 wrmsr
92
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +020093 post_code(0x2c)
94
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020095 /* Enable cache (CR0.CD = 0, CR0.NW = 0). */
Kyösti Mälkkif9d1a422012-02-28 01:45:44 +020096 movl %cr0, %eax
Patrick Georgi05e740f2012-03-31 12:52:21 +020097 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020098 invd
99 movl %eax, %cr0
100
Kyösti Mälkki54d6a282018-05-25 06:03:14 +0300101 /* Read then clear the CAR region. This will also fill up the cache.
102 * IMPORTANT: The read is mandatory.
103 */
104 movl $CACHE_AS_RAM_BASE, %esi
105 movl %esi, %edi
Kyösti Mälkkif9d1a422012-02-28 01:45:44 +0200106 cld
Stefan Reinauer4a45ec42015-07-07 00:54:05 +0200107 movl $(CACHE_AS_RAM_SIZE >> 2), %ecx
Kyösti Mälkki54d6a282018-05-25 06:03:14 +0300108 rep lodsl
109 movl $(CACHE_AS_RAM_SIZE >> 2), %ecx
110 xorl %eax, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200111 rep stosl
112
Kyösti Mälkki8a2f1672016-07-20 13:29:59 +0300113 post_code(0x2d)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200114 /* Enable Cache-as-RAM mode by disabling cache. */
115 movl %cr0, %eax
Patrick Georgi05e740f2012-03-31 12:52:21 +0200116 orl $CR0_CacheDisable, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200117 movl %eax, %cr0
118
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200119 /* Enable cache for our code in Flash because we do XIP here */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700120 movl $MTRR_PHYS_BASE(1), %ecx
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200121 xorl %edx, %edx
122 /*
123 * IMPORTANT: The following calculation _must_ be done at runtime. See
Paul Menzela8843de2017-06-05 12:33:23 +0200124 * https://www.coreboot.org/pipermail/coreboot/2010-October/060855.html
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200125 */
126 movl $copy_and_run, %eax
127 andl $(~(CONFIG_XIP_ROM_SIZE - 1)), %eax
Kyösti Mälkkidc4820b2016-07-21 19:51:01 +0300128 orl $MTRR_TYPE_WRPROT, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200129 wrmsr
130
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700131 movl $MTRR_PHYS_MASK(1), %ecx
Kyösti Mälkkia860c682012-02-28 02:06:45 +0200132 rdmsr
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700133 movl $(~(CONFIG_XIP_ROM_SIZE - 1) | MTRR_PHYS_MASK_VALID), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200134 wrmsr
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200135
Kyösti Mälkki8a2f1672016-07-20 13:29:59 +0300136 post_code(0x2e)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200137 /* Enable cache. */
138 movl %cr0, %eax
Patrick Georgi05e740f2012-03-31 12:52:21 +0200139 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200140 movl %eax, %cr0
141
Kyösti Mälkki39915bc2016-11-08 12:13:15 +0200142 /* Setup the stack. */
143 movl $(CONFIG_DCACHE_RAM_BASE + CONFIG_DCACHE_RAM_SIZE), %eax
144 movl %eax, %esp
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200145
146 /* Restore the BIST result. */
147 movl %ebp, %eax
148 movl %esp, %ebp
149 pushl %eax
150
Kyösti Mälkki8a2f1672016-07-20 13:29:59 +0300151before_romstage:
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +0200152 post_code(0x2f)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200153 /* Call romstage.c main function. */
Kyösti Mälkki408d3922016-06-17 10:43:48 +0300154 call romstage_main
Kyösti Mälkki408d3922016-06-17 10:43:48 +0300155 /* Save return value from romstage_main. It contains the stack to use
Kyösti Mälkki823020d2016-07-22 22:53:19 +0300156 * after cache-as-ram is torn down. It also contains the information
157 * for setting up MTRRs. */
Kyösti Mälkki39915bc2016-11-08 12:13:15 +0200158 movl %eax, %esp
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200159
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200160 post_code(0x30)
161
162 /* Disable cache. */
163 movl %cr0, %eax
Patrick Georgi05e740f2012-03-31 12:52:21 +0200164 orl $CR0_CacheDisable, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200165 movl %eax, %cr0
166
Kyösti Mälkki8a2f1672016-07-20 13:29:59 +0300167 post_code(0x31)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200168
169 /* Disable MTRR. */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700170 movl $MTRR_DEF_TYPE_MSR, %ecx
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200171 rdmsr
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700172 andl $(~MTRR_DEF_TYPE_EN), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200173 wrmsr
174
Kyösti Mälkki8a2f1672016-07-20 13:29:59 +0300175 post_code(0x32)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200176
177 invd
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200178
Kyösti Mälkki8a2f1672016-07-20 13:29:59 +0300179 post_code(0x33)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200180
181 /* Enable cache. */
182 movl %cr0, %eax
Patrick Georgi05e740f2012-03-31 12:52:21 +0200183 andl $~(CR0_CacheDisable | CR0_NoWriteThrough), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200184 movl %eax, %cr0
185
Kyösti Mälkki8a2f1672016-07-20 13:29:59 +0300186 post_code(0x36)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200187
188 /* Disable cache. */
189 movl %cr0, %eax
Patrick Georgi05e740f2012-03-31 12:52:21 +0200190 orl $CR0_CacheDisable, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200191 movl %eax, %cr0
192
193 post_code(0x38)
194
Kyösti Mälkki823020d2016-07-22 22:53:19 +0300195 /* Clear all of the variable MTRRs. */
196 popl %ebx
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700197 movl $MTRR_PHYS_BASE(0), %ecx
Kyösti Mälkki823020d2016-07-22 22:53:19 +0300198 clr %eax
199 clr %edx
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200200
Kyösti Mälkki823020d2016-07-22 22:53:19 +03002011:
202 testl %ebx, %ebx
203 jz 1f
204 wrmsr /* Write MTRR base. */
205 inc %ecx
206 wrmsr /* Write MTRR mask. */
207 inc %ecx
208 dec %ebx
209 jmp 1b
210
2111:
212 /* Get number of MTRRs. */
213 popl %ebx
214 movl $MTRR_PHYS_BASE(0), %ecx
2152:
216 testl %ebx, %ebx
217 jz 2f
218
219 /* Low 32 bits of MTRR base. */
220 popl %eax
221 /* Upper 32 bits of MTRR base. */
222 popl %edx
223 /* Write MTRR base. */
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200224 wrmsr
Kyösti Mälkki823020d2016-07-22 22:53:19 +0300225 inc %ecx
226 /* Low 32 bits of MTRR mask. */
227 popl %eax
228 /* Upper 32 bits of MTRR mask. */
229 popl %edx
230 /* Write MTRR mask. */
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200231 wrmsr
Kyösti Mälkki823020d2016-07-22 22:53:19 +0300232 inc %ecx
233
234 dec %ebx
235 jmp 2b
2362:
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200237
238 post_code(0x39)
239
240 /* And enable cache again after setting MTRRs. */
241 movl %cr0, %eax
Patrick Georgi05e740f2012-03-31 12:52:21 +0200242 andl $~(CR0_CacheDisable | CR0_NoWriteThrough), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200243 movl %eax, %cr0
244
245 post_code(0x3a)
246
247 /* Enable MTRR. */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700248 movl $MTRR_DEF_TYPE_MSR, %ecx
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200249 rdmsr
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700250 orl $MTRR_DEF_TYPE_EN, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200251 wrmsr
252
253 post_code(0x3b)
254
255 /* Invalidate the cache again. */
256 invd
257
258 post_code(0x3c)
259
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200260__main:
261 post_code(POST_PREPARE_RAMSTAGE)
262 cld /* Clear direction flag. */
Kyösti Mälkki39915bc2016-11-08 12:13:15 +0200263 call romstage_after_car
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200264
265.Lhlt:
266 post_code(POST_DEAD_CODE)
267 hlt
268 jmp .Lhlt
269
270mtrr_table:
271 /* Fixed MTRRs */
272 .word 0x250, 0x258, 0x259
273 .word 0x268, 0x269, 0x26A
274 .word 0x26B, 0x26C, 0x26D
275 .word 0x26E, 0x26F
276 /* Variable MTRRs */
277 .word 0x200, 0x201, 0x202, 0x203
278 .word 0x204, 0x205, 0x206, 0x207
279 .word 0x208, 0x209, 0x20A, 0x20B
280 .word 0x20C, 0x20D, 0x20E, 0x20F
281mtrr_table_end: