blob: a3487dbe348ed3bc8f0c86585060eb7548065e0f [file] [log] [blame]
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +02001/*
2 * This file is part of the coreboot project.
3 *
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +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.
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020012 */
13
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020014#include <cpu/x86/mtrr.h>
Patrick Georgi05e740f2012-03-31 12:52:21 +020015#include <cpu/x86/cache.h>
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020016#include <cpu/x86/post_code.h>
17
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +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
Kyösti Mälkkiaea8eec2018-06-04 08:49:17 +030032.code32
33_cache_as_ram_setup:
34
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +020035bootblock_pre_c_entry:
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020036
37cache_as_ram:
38 post_code(0x20)
39
Kyösti Mälkki5bc46d82018-06-14 06:21:53 +030040 /* Clear/disable fixed MTRRs */
41 mov $fixed_mtrr_list_size, %ebx
42 xor %eax, %eax
43 xor %edx, %edx
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020044
Kyösti Mälkki5bc46d82018-06-14 06:21:53 +030045clear_fixed_mtrr:
46 add $-2, %ebx
47 movzwl fixed_mtrr_list(%ebx), %ecx
48 wrmsr
49 jnz clear_fixed_mtrr
50
Elyes HAOUAS02820ca2018-09-30 07:44:39 +020051 /* Figure out how many MTRRs we have, and clear them out */
Kyösti Mälkki5bc46d82018-06-14 06:21:53 +030052 mov $MTRR_CAP_MSR, %ecx
53 rdmsr
54 movzb %al, %ebx /* Number of variable MTRRs */
55 mov $MTRR_PHYS_BASE(0), %ecx
56 xor %eax, %eax
57 xor %edx, %edx
58
59clear_var_mtrr:
60 wrmsr
61 inc %ecx
62 wrmsr
63 inc %ecx
64 dec %ebx
65 jnz clear_var_mtrr
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +020066 post_code(0x21)
67
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020068 /* Configure the default memory type to uncacheable. */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070069 movl $MTRR_DEF_TYPE_MSR, %ecx
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020070 rdmsr
71 andl $(~0x00000cff), %eax
72 wrmsr
73
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +020074 post_code(0x22)
75
Kyösti Mälkki54d6a282018-05-25 06:03:14 +030076 /* Determine CPU_ADDR_BITS and load PHYSMASK high word to %edx. */
Kyösti Mälkkia860c682012-02-28 02:06:45 +020077 movl $1, %eax
78 cpuid
Elyes HAOUAS168ef392017-06-27 22:54:42 +020079 andl $(1 << 6 | 1 << 17), %edx /* PAE or PSE36 */
Kyösti Mälkkia860c682012-02-28 02:06:45 +020080 jz addrsize_set_high
81 movl $0x0f, %edx
82
83 /* Preload high word of address mask (in %edx) for Variable
Kyösti Mälkki54d6a282018-05-25 06:03:14 +030084 MTRRs 0 and 1. */
Kyösti Mälkkia860c682012-02-28 02:06:45 +020085addrsize_set_high:
86 xorl %eax, %eax
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070087 movl $MTRR_PHYS_MASK(0), %ecx
Kyösti Mälkkia860c682012-02-28 02:06:45 +020088 wrmsr
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070089 movl $MTRR_PHYS_MASK(1), %ecx
Kyösti Mälkkia860c682012-02-28 02:06:45 +020090 wrmsr
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +020091
92 post_code(0x2a)
93
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020094 /* Set Cache-as-RAM base address. */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070095 movl $(MTRR_PHYS_BASE(0)), %ecx
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020096 movl $(CACHE_AS_RAM_BASE | MTRR_TYPE_WRBACK), %eax
97 xorl %edx, %edx
98 wrmsr
99
100 /* Set Cache-as-RAM mask. */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700101 movl $(MTRR_PHYS_MASK(0)), %ecx
Kyösti Mälkkia860c682012-02-28 02:06:45 +0200102 rdmsr
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700103 movl $(~(CACHE_AS_RAM_SIZE - 1) | MTRR_PHYS_MASK_VALID), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200104 wrmsr
105
Kyösti Mälkki8a2f1672016-07-20 13:29:59 +0300106 post_code(0x2b)
107
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200108 /* Enable MTRR. */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700109 movl $MTRR_DEF_TYPE_MSR, %ecx
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200110 rdmsr
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700111 orl $MTRR_DEF_TYPE_EN, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200112 wrmsr
113
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +0200114 post_code(0x2c)
115
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200116 /* Enable cache (CR0.CD = 0, CR0.NW = 0). */
Kyösti Mälkkif9d1a422012-02-28 01:45:44 +0200117 movl %cr0, %eax
Patrick Georgi05e740f2012-03-31 12:52:21 +0200118 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200119 invd
120 movl %eax, %cr0
121
Kyösti Mälkki54d6a282018-05-25 06:03:14 +0300122 /* Read then clear the CAR region. This will also fill up the cache.
123 * IMPORTANT: The read is mandatory.
124 */
125 movl $CACHE_AS_RAM_BASE, %esi
126 movl %esi, %edi
Kyösti Mälkkif9d1a422012-02-28 01:45:44 +0200127 cld
Stefan Reinauer4a45ec42015-07-07 00:54:05 +0200128 movl $(CACHE_AS_RAM_SIZE >> 2), %ecx
Kyösti Mälkki54d6a282018-05-25 06:03:14 +0300129 rep lodsl
130 movl $(CACHE_AS_RAM_SIZE >> 2), %ecx
131 xorl %eax, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200132 rep stosl
133
Kyösti Mälkki8a2f1672016-07-20 13:29:59 +0300134 post_code(0x2d)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200135 /* Enable Cache-as-RAM mode by disabling cache. */
136 movl %cr0, %eax
Patrick Georgi05e740f2012-03-31 12:52:21 +0200137 orl $CR0_CacheDisable, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200138 movl %eax, %cr0
139
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200140 /* Enable cache for our code in Flash because we do XIP here */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700141 movl $MTRR_PHYS_BASE(1), %ecx
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200142 xorl %edx, %edx
143 /*
144 * IMPORTANT: The following calculation _must_ be done at runtime. See
Stefan Taunerde028782018-08-19 20:02:05 +0200145 * https://mail.coreboot.org/pipermail/coreboot/2010-October/060922.html
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200146 */
Kyösti Mälkkice9f4222018-06-25 18:53:36 +0300147 movl $_program, %eax
Arthur Heymans942ad6a2019-10-12 18:06:46 +0200148 andl $(~(XIP_ROM_SIZE - 1)), %eax
Kyösti Mälkkidc4820b2016-07-21 19:51:01 +0300149 orl $MTRR_TYPE_WRPROT, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200150 wrmsr
151
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700152 movl $MTRR_PHYS_MASK(1), %ecx
Kyösti Mälkkia860c682012-02-28 02:06:45 +0200153 rdmsr
Arthur Heymans942ad6a2019-10-12 18:06:46 +0200154 movl $(~(XIP_ROM_SIZE - 1) | MTRR_PHYS_MASK_VALID), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200155 wrmsr
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200156
Kyösti Mälkki8a2f1672016-07-20 13:29:59 +0300157 post_code(0x2e)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200158 /* Enable cache. */
159 movl %cr0, %eax
Patrick Georgi05e740f2012-03-31 12:52:21 +0200160 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200161 movl %eax, %cr0
162
Kyösti Mälkki39915bc2016-11-08 12:13:15 +0200163 /* Setup the stack. */
Arthur Heymansdf9cdcf2019-11-09 06:50:20 +0100164 mov $_ecar_stack, %esp
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200165
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200166 /* Need to align stack to 16 bytes at call instruction. Account for
167 the pushes below. */
168 andl $0xfffffff0, %esp
169 subl $4, %esp
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200170
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200171 /* push TSC and BIST to stack */
172 movd %mm0, %eax
Elyes HAOUAS87930b32019-01-16 12:41:57 +0100173 pushl %eax /* BIST */
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200174 movd %mm2, %eax
175 pushl %eax /* tsc[63:32] */
176 movd %mm1, %eax
Elyes HAOUAS87930b32019-01-16 12:41:57 +0100177 pushl %eax /* tsc[31:0] */
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200178
179before_c_entry:
180 post_code(0x29)
181 call bootblock_c_entry_bist
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200182
Kyösti Mälkkiaea8eec2018-06-04 08:49:17 +0300183 /* Should never see this postcode */
184 post_code(POST_DEAD_CODE)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200185
186.Lhlt:
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200187 hlt
188 jmp .Lhlt
189
Kyösti Mälkki5bc46d82018-06-14 06:21:53 +0300190fixed_mtrr_list:
191 .word MTRR_FIX_64K_00000
192 .word MTRR_FIX_16K_80000
193 .word MTRR_FIX_16K_A0000
194 .word MTRR_FIX_4K_C0000
195 .word MTRR_FIX_4K_C8000
196 .word MTRR_FIX_4K_D0000
197 .word MTRR_FIX_4K_D8000
198 .word MTRR_FIX_4K_E0000
199 .word MTRR_FIX_4K_E8000
200 .word MTRR_FIX_4K_F0000
201 .word MTRR_FIX_4K_F8000
202fixed_mtrr_list_size = . - fixed_mtrr_list
Kyösti Mälkkiaea8eec2018-06-04 08:49:17 +0300203
204_cache_as_ram_setup_end: