blob: 53e52cf12479e32a8d3a33ded6b1a423838e7b28 [file] [log] [blame]
Angel Ponsf23ae0b2020-04-02 23:48:12 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +02002
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +02003#include <cpu/x86/mtrr.h>
Patrick Georgi05e740f2012-03-31 12:52:21 +02004#include <cpu/x86/cache.h>
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +02005#include <cpu/x86/post_code.h>
6
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +02007#define CACHE_AS_RAM_SIZE CONFIG_DCACHE_RAM_SIZE
8#define CACHE_AS_RAM_BASE CONFIG_DCACHE_RAM_BASE
9
Arthur Heymans942ad6a2019-10-12 18:06:46 +020010#if ((CONFIG_C_ENV_BOOTBLOCK_SIZE & (CONFIG_C_ENV_BOOTBLOCK_SIZE - 1)) != 0)
11#error "CONFIG_C_ENV_BOOTBLOCK_SIZE must be a power of 2!"
12#endif
13#define XIP_ROM_SIZE CONFIG_C_ENV_BOOTBLOCK_SIZE
Arthur Heymans942ad6a2019-10-12 18:06:46 +020014
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +020015.global bootblock_pre_c_entry
16
Kyösti Mälkkiaea8eec2018-06-04 08:49:17 +030017.code32
18_cache_as_ram_setup:
19
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +020020bootblock_pre_c_entry:
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020021
22cache_as_ram:
23 post_code(0x20)
24
Kyösti Mälkki5bc46d82018-06-14 06:21:53 +030025 /* Clear/disable fixed MTRRs */
26 mov $fixed_mtrr_list_size, %ebx
27 xor %eax, %eax
28 xor %edx, %edx
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020029
Kyösti Mälkki5bc46d82018-06-14 06:21:53 +030030clear_fixed_mtrr:
31 add $-2, %ebx
32 movzwl fixed_mtrr_list(%ebx), %ecx
33 wrmsr
34 jnz clear_fixed_mtrr
35
Elyes HAOUAS02820ca2018-09-30 07:44:39 +020036 /* Figure out how many MTRRs we have, and clear them out */
Kyösti Mälkki5bc46d82018-06-14 06:21:53 +030037 mov $MTRR_CAP_MSR, %ecx
38 rdmsr
39 movzb %al, %ebx /* Number of variable MTRRs */
40 mov $MTRR_PHYS_BASE(0), %ecx
41 xor %eax, %eax
42 xor %edx, %edx
43
44clear_var_mtrr:
45 wrmsr
46 inc %ecx
47 wrmsr
48 inc %ecx
49 dec %ebx
50 jnz clear_var_mtrr
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +020051 post_code(0x21)
52
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020053 /* Configure the default memory type to uncacheable. */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070054 movl $MTRR_DEF_TYPE_MSR, %ecx
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020055 rdmsr
56 andl $(~0x00000cff), %eax
57 wrmsr
58
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +020059 post_code(0x22)
60
Kyösti Mälkki54d6a282018-05-25 06:03:14 +030061 /* Determine CPU_ADDR_BITS and load PHYSMASK high word to %edx. */
Kyösti Mälkkia860c682012-02-28 02:06:45 +020062 movl $1, %eax
63 cpuid
Elyes HAOUAS168ef392017-06-27 22:54:42 +020064 andl $(1 << 6 | 1 << 17), %edx /* PAE or PSE36 */
Kyösti Mälkkia860c682012-02-28 02:06:45 +020065 jz addrsize_set_high
66 movl $0x0f, %edx
67
68 /* Preload high word of address mask (in %edx) for Variable
Kyösti Mälkki54d6a282018-05-25 06:03:14 +030069 MTRRs 0 and 1. */
Kyösti Mälkkia860c682012-02-28 02:06:45 +020070addrsize_set_high:
71 xorl %eax, %eax
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070072 movl $MTRR_PHYS_MASK(0), %ecx
Kyösti Mälkkia860c682012-02-28 02:06:45 +020073 wrmsr
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070074 movl $MTRR_PHYS_MASK(1), %ecx
Kyösti Mälkkia860c682012-02-28 02:06:45 +020075 wrmsr
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +020076
77 post_code(0x2a)
78
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020079 /* Set Cache-as-RAM base address. */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070080 movl $(MTRR_PHYS_BASE(0)), %ecx
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020081 movl $(CACHE_AS_RAM_BASE | MTRR_TYPE_WRBACK), %eax
82 xorl %edx, %edx
83 wrmsr
84
85 /* Set Cache-as-RAM mask. */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070086 movl $(MTRR_PHYS_MASK(0)), %ecx
Kyösti Mälkkia860c682012-02-28 02:06:45 +020087 rdmsr
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070088 movl $(~(CACHE_AS_RAM_SIZE - 1) | MTRR_PHYS_MASK_VALID), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020089 wrmsr
90
Kyösti Mälkki8a2f1672016-07-20 13:29:59 +030091 post_code(0x2b)
92
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020093 /* Enable MTRR. */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070094 movl $MTRR_DEF_TYPE_MSR, %ecx
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020095 rdmsr
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070096 orl $MTRR_DEF_TYPE_EN, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020097 wrmsr
98
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +020099 post_code(0x2c)
100
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200101 /* Enable cache (CR0.CD = 0, CR0.NW = 0). */
Kyösti Mälkkif9d1a422012-02-28 01:45:44 +0200102 movl %cr0, %eax
Patrick Georgi05e740f2012-03-31 12:52:21 +0200103 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200104 invd
105 movl %eax, %cr0
106
Kyösti Mälkki54d6a282018-05-25 06:03:14 +0300107 /* Read then clear the CAR region. This will also fill up the cache.
108 * IMPORTANT: The read is mandatory.
109 */
110 movl $CACHE_AS_RAM_BASE, %esi
111 movl %esi, %edi
Kyösti Mälkkif9d1a422012-02-28 01:45:44 +0200112 cld
Stefan Reinauer4a45ec42015-07-07 00:54:05 +0200113 movl $(CACHE_AS_RAM_SIZE >> 2), %ecx
Kyösti Mälkki54d6a282018-05-25 06:03:14 +0300114 rep lodsl
115 movl $(CACHE_AS_RAM_SIZE >> 2), %ecx
116 xorl %eax, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200117 rep stosl
118
Kyösti Mälkki8a2f1672016-07-20 13:29:59 +0300119 post_code(0x2d)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200120 /* Enable Cache-as-RAM mode by disabling cache. */
121 movl %cr0, %eax
Patrick Georgi05e740f2012-03-31 12:52:21 +0200122 orl $CR0_CacheDisable, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200123 movl %eax, %cr0
124
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200125 /* Enable cache for our code in Flash because we do XIP here */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700126 movl $MTRR_PHYS_BASE(1), %ecx
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200127 xorl %edx, %edx
128 /*
129 * IMPORTANT: The following calculation _must_ be done at runtime. See
Stefan Taunerde028782018-08-19 20:02:05 +0200130 * https://mail.coreboot.org/pipermail/coreboot/2010-October/060922.html
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200131 */
Kyösti Mälkkice9f4222018-06-25 18:53:36 +0300132 movl $_program, %eax
Arthur Heymans942ad6a2019-10-12 18:06:46 +0200133 andl $(~(XIP_ROM_SIZE - 1)), %eax
Kyösti Mälkkidc4820b2016-07-21 19:51:01 +0300134 orl $MTRR_TYPE_WRPROT, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200135 wrmsr
136
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700137 movl $MTRR_PHYS_MASK(1), %ecx
Kyösti Mälkkia860c682012-02-28 02:06:45 +0200138 rdmsr
Arthur Heymans942ad6a2019-10-12 18:06:46 +0200139 movl $(~(XIP_ROM_SIZE - 1) | MTRR_PHYS_MASK_VALID), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200140 wrmsr
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200141
Kyösti Mälkki8a2f1672016-07-20 13:29:59 +0300142 post_code(0x2e)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200143 /* Enable cache. */
144 movl %cr0, %eax
Patrick Georgi05e740f2012-03-31 12:52:21 +0200145 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200146 movl %eax, %cr0
147
Kyösti Mälkki39915bc2016-11-08 12:13:15 +0200148 /* Setup the stack. */
Arthur Heymansdf9cdcf2019-11-09 06:50:20 +0100149 mov $_ecar_stack, %esp
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200150
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200151 /* Need to align stack to 16 bytes at call instruction. Account for
152 the pushes below. */
153 andl $0xfffffff0, %esp
154 subl $4, %esp
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200155
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200156 /* push TSC and BIST to stack */
157 movd %mm0, %eax
Elyes HAOUAS87930b32019-01-16 12:41:57 +0100158 pushl %eax /* BIST */
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200159 movd %mm2, %eax
160 pushl %eax /* tsc[63:32] */
161 movd %mm1, %eax
Elyes HAOUAS87930b32019-01-16 12:41:57 +0100162 pushl %eax /* tsc[31:0] */
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200163
164before_c_entry:
165 post_code(0x29)
166 call bootblock_c_entry_bist
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200167
Kyösti Mälkkiaea8eec2018-06-04 08:49:17 +0300168 /* Should never see this postcode */
169 post_code(POST_DEAD_CODE)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200170
171.Lhlt:
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200172 hlt
173 jmp .Lhlt
174
Kyösti Mälkki5bc46d82018-06-14 06:21:53 +0300175fixed_mtrr_list:
176 .word MTRR_FIX_64K_00000
177 .word MTRR_FIX_16K_80000
178 .word MTRR_FIX_16K_A0000
179 .word MTRR_FIX_4K_C0000
180 .word MTRR_FIX_4K_C8000
181 .word MTRR_FIX_4K_D0000
182 .word MTRR_FIX_4K_D8000
183 .word MTRR_FIX_4K_E0000
184 .word MTRR_FIX_4K_E8000
185 .word MTRR_FIX_4K_F0000
186 .word MTRR_FIX_4K_F8000
187fixed_mtrr_list_size = . - fixed_mtrr_list
Kyösti Mälkkiaea8eec2018-06-04 08:49:17 +0300188
189_cache_as_ram_setup_end: