blob: 280913163ad69b65943c1ca0cabdfc7416c502a8 [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
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +020026.global bootblock_pre_c_entry
27
Kyösti Mälkkiaea8eec2018-06-04 08:49:17 +030028.code32
29_cache_as_ram_setup:
30
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +020031bootblock_pre_c_entry:
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020032
33cache_as_ram:
34 post_code(0x20)
35
Kyösti Mälkki5bc46d82018-06-14 06:21:53 +030036 /* Clear/disable fixed MTRRs */
37 mov $fixed_mtrr_list_size, %ebx
38 xor %eax, %eax
39 xor %edx, %edx
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020040
Kyösti Mälkki5bc46d82018-06-14 06:21:53 +030041clear_fixed_mtrr:
42 add $-2, %ebx
43 movzwl fixed_mtrr_list(%ebx), %ecx
44 wrmsr
45 jnz clear_fixed_mtrr
46
Elyes HAOUAS02820ca2018-09-30 07:44:39 +020047 /* Figure out how many MTRRs we have, and clear them out */
Kyösti Mälkki5bc46d82018-06-14 06:21:53 +030048 mov $MTRR_CAP_MSR, %ecx
49 rdmsr
50 movzb %al, %ebx /* Number of variable MTRRs */
51 mov $MTRR_PHYS_BASE(0), %ecx
52 xor %eax, %eax
53 xor %edx, %edx
54
55clear_var_mtrr:
56 wrmsr
57 inc %ecx
58 wrmsr
59 inc %ecx
60 dec %ebx
61 jnz clear_var_mtrr
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +020062 post_code(0x21)
63
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020064 /* Configure the default memory type to uncacheable. */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070065 movl $MTRR_DEF_TYPE_MSR, %ecx
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020066 rdmsr
67 andl $(~0x00000cff), %eax
68 wrmsr
69
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +020070 post_code(0x22)
71
Kyösti Mälkki54d6a282018-05-25 06:03:14 +030072 /* Determine CPU_ADDR_BITS and load PHYSMASK high word to %edx. */
Kyösti Mälkkia860c682012-02-28 02:06:45 +020073 movl $1, %eax
74 cpuid
Elyes HAOUAS168ef392017-06-27 22:54:42 +020075 andl $(1 << 6 | 1 << 17), %edx /* PAE or PSE36 */
Kyösti Mälkkia860c682012-02-28 02:06:45 +020076 jz addrsize_set_high
77 movl $0x0f, %edx
78
79 /* Preload high word of address mask (in %edx) for Variable
Kyösti Mälkki54d6a282018-05-25 06:03:14 +030080 MTRRs 0 and 1. */
Kyösti Mälkkia860c682012-02-28 02:06:45 +020081addrsize_set_high:
82 xorl %eax, %eax
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070083 movl $MTRR_PHYS_MASK(0), %ecx
Kyösti Mälkkia860c682012-02-28 02:06:45 +020084 wrmsr
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070085 movl $MTRR_PHYS_MASK(1), %ecx
Kyösti Mälkkia860c682012-02-28 02:06:45 +020086 wrmsr
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +020087
88 post_code(0x2a)
89
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020090 /* Set Cache-as-RAM base address. */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070091 movl $(MTRR_PHYS_BASE(0)), %ecx
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020092 movl $(CACHE_AS_RAM_BASE | MTRR_TYPE_WRBACK), %eax
93 xorl %edx, %edx
94 wrmsr
95
96 /* Set Cache-as-RAM mask. */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070097 movl $(MTRR_PHYS_MASK(0)), %ecx
Kyösti Mälkkia860c682012-02-28 02:06:45 +020098 rdmsr
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070099 movl $(~(CACHE_AS_RAM_SIZE - 1) | MTRR_PHYS_MASK_VALID), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200100 wrmsr
101
Kyösti Mälkki8a2f1672016-07-20 13:29:59 +0300102 post_code(0x2b)
103
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200104 /* Enable MTRR. */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700105 movl $MTRR_DEF_TYPE_MSR, %ecx
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200106 rdmsr
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700107 orl $MTRR_DEF_TYPE_EN, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200108 wrmsr
109
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +0200110 post_code(0x2c)
111
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200112 /* Enable cache (CR0.CD = 0, CR0.NW = 0). */
Kyösti Mälkkif9d1a422012-02-28 01:45:44 +0200113 movl %cr0, %eax
Patrick Georgi05e740f2012-03-31 12:52:21 +0200114 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200115 invd
116 movl %eax, %cr0
117
Kyösti Mälkki54d6a282018-05-25 06:03:14 +0300118 /* Read then clear the CAR region. This will also fill up the cache.
119 * IMPORTANT: The read is mandatory.
120 */
121 movl $CACHE_AS_RAM_BASE, %esi
122 movl %esi, %edi
Kyösti Mälkkif9d1a422012-02-28 01:45:44 +0200123 cld
Stefan Reinauer4a45ec42015-07-07 00:54:05 +0200124 movl $(CACHE_AS_RAM_SIZE >> 2), %ecx
Kyösti Mälkki54d6a282018-05-25 06:03:14 +0300125 rep lodsl
126 movl $(CACHE_AS_RAM_SIZE >> 2), %ecx
127 xorl %eax, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200128 rep stosl
129
Kyösti Mälkki8a2f1672016-07-20 13:29:59 +0300130 post_code(0x2d)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200131 /* Enable Cache-as-RAM mode by disabling cache. */
132 movl %cr0, %eax
Patrick Georgi05e740f2012-03-31 12:52:21 +0200133 orl $CR0_CacheDisable, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200134 movl %eax, %cr0
135
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200136 /* Enable cache for our code in Flash because we do XIP here */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700137 movl $MTRR_PHYS_BASE(1), %ecx
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200138 xorl %edx, %edx
139 /*
140 * IMPORTANT: The following calculation _must_ be done at runtime. See
Stefan Taunerde028782018-08-19 20:02:05 +0200141 * https://mail.coreboot.org/pipermail/coreboot/2010-October/060922.html
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200142 */
Kyösti Mälkkice9f4222018-06-25 18:53:36 +0300143 movl $_program, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200144 andl $(~(CONFIG_XIP_ROM_SIZE - 1)), %eax
Kyösti Mälkkidc4820b2016-07-21 19:51:01 +0300145 orl $MTRR_TYPE_WRPROT, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200146 wrmsr
147
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700148 movl $MTRR_PHYS_MASK(1), %ecx
Kyösti Mälkkia860c682012-02-28 02:06:45 +0200149 rdmsr
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700150 movl $(~(CONFIG_XIP_ROM_SIZE - 1) | MTRR_PHYS_MASK_VALID), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200151 wrmsr
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200152
Kyösti Mälkki8a2f1672016-07-20 13:29:59 +0300153 post_code(0x2e)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200154 /* Enable cache. */
155 movl %cr0, %eax
Patrick Georgi05e740f2012-03-31 12:52:21 +0200156 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200157 movl %eax, %cr0
158
Kyösti Mälkki39915bc2016-11-08 12:13:15 +0200159 /* Setup the stack. */
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200160 mov $_car_stack_end, %esp
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200161
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200162 /* Need to align stack to 16 bytes at call instruction. Account for
163 the pushes below. */
164 andl $0xfffffff0, %esp
165 subl $4, %esp
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200166
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200167 /* push TSC and BIST to stack */
168 movd %mm0, %eax
169 pushl %eax /* BIST */
170 movd %mm2, %eax
171 pushl %eax /* tsc[63:32] */
172 movd %mm1, %eax
173 pushl %eax /* tsc[31:0] */
174
175before_c_entry:
176 post_code(0x29)
177 call bootblock_c_entry_bist
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200178
Kyösti Mälkkiaea8eec2018-06-04 08:49:17 +0300179 /* Should never see this postcode */
180 post_code(POST_DEAD_CODE)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200181
182.Lhlt:
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200183 hlt
184 jmp .Lhlt
185
Kyösti Mälkki5bc46d82018-06-14 06:21:53 +0300186fixed_mtrr_list:
187 .word MTRR_FIX_64K_00000
188 .word MTRR_FIX_16K_80000
189 .word MTRR_FIX_16K_A0000
190 .word MTRR_FIX_4K_C0000
191 .word MTRR_FIX_4K_C8000
192 .word MTRR_FIX_4K_D0000
193 .word MTRR_FIX_4K_D8000
194 .word MTRR_FIX_4K_E0000
195 .word MTRR_FIX_4K_E8000
196 .word MTRR_FIX_4K_F0000
197 .word MTRR_FIX_4K_F8000
198fixed_mtrr_list_size = . - fixed_mtrr_list
Kyösti Mälkkiaea8eec2018-06-04 08:49:17 +0300199
200_cache_as_ram_setup_end: