blob: 259e321c3850e0fb548ac9dc16630e98c706412a [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älkkic641f7e2018-12-28 16:54:54 +02007.global bootblock_pre_c_entry
8
Kyösti Mälkkiaea8eec2018-06-04 08:49:17 +03009.code32
10_cache_as_ram_setup:
11
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +020012bootblock_pre_c_entry:
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020013
14cache_as_ram:
15 post_code(0x20)
16
Kyösti Mälkki5bc46d82018-06-14 06:21:53 +030017 /* Clear/disable fixed MTRRs */
18 mov $fixed_mtrr_list_size, %ebx
19 xor %eax, %eax
20 xor %edx, %edx
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020021
Kyösti Mälkki5bc46d82018-06-14 06:21:53 +030022clear_fixed_mtrr:
23 add $-2, %ebx
24 movzwl fixed_mtrr_list(%ebx), %ecx
25 wrmsr
26 jnz clear_fixed_mtrr
27
Elyes HAOUAS02820ca2018-09-30 07:44:39 +020028 /* Figure out how many MTRRs we have, and clear them out */
Kyösti Mälkki5bc46d82018-06-14 06:21:53 +030029 mov $MTRR_CAP_MSR, %ecx
30 rdmsr
31 movzb %al, %ebx /* Number of variable MTRRs */
32 mov $MTRR_PHYS_BASE(0), %ecx
33 xor %eax, %eax
34 xor %edx, %edx
35
36clear_var_mtrr:
37 wrmsr
38 inc %ecx
39 wrmsr
40 inc %ecx
41 dec %ebx
42 jnz clear_var_mtrr
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +020043 post_code(0x21)
44
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020045 /* Configure the default memory type to uncacheable. */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070046 movl $MTRR_DEF_TYPE_MSR, %ecx
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020047 rdmsr
48 andl $(~0x00000cff), %eax
49 wrmsr
50
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +020051 post_code(0x22)
52
Kyösti Mälkki54d6a282018-05-25 06:03:14 +030053 /* Determine CPU_ADDR_BITS and load PHYSMASK high word to %edx. */
Kyösti Mälkkia860c682012-02-28 02:06:45 +020054 movl $1, %eax
55 cpuid
Elyes HAOUAS168ef392017-06-27 22:54:42 +020056 andl $(1 << 6 | 1 << 17), %edx /* PAE or PSE36 */
Kyösti Mälkkia860c682012-02-28 02:06:45 +020057 jz addrsize_set_high
58 movl $0x0f, %edx
59
60 /* Preload high word of address mask (in %edx) for Variable
Kyösti Mälkki54d6a282018-05-25 06:03:14 +030061 MTRRs 0 and 1. */
Kyösti Mälkkia860c682012-02-28 02:06:45 +020062addrsize_set_high:
63 xorl %eax, %eax
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070064 movl $MTRR_PHYS_MASK(0), %ecx
Kyösti Mälkkia860c682012-02-28 02:06:45 +020065 wrmsr
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070066 movl $MTRR_PHYS_MASK(1), %ecx
Kyösti Mälkkia860c682012-02-28 02:06:45 +020067 wrmsr
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +020068
69 post_code(0x2a)
70
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020071 /* Set Cache-as-RAM base address. */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070072 movl $(MTRR_PHYS_BASE(0)), %ecx
Kyösti Mälkkidc6bb6c2019-11-08 00:08:55 +020073 movl $_car_mtrr_start, %eax
74 orl $MTRR_TYPE_WRBACK, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020075 xorl %edx, %edx
76 wrmsr
77
78 /* Set Cache-as-RAM mask. */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070079 movl $(MTRR_PHYS_MASK(0)), %ecx
Kyösti Mälkkia860c682012-02-28 02:06:45 +020080 rdmsr
Kyösti Mälkkidc6bb6c2019-11-08 00:08:55 +020081 movl $_car_mtrr_mask, %eax
82 orl $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 */
Kyösti Mälkkif9d1a422012-02-28 01:45:44 +0200104 cld
Kyösti Mälkkidc6bb6c2019-11-08 00:08:55 +0200105 movl $_car_mtrr_start, %edi
106 movl $_car_mtrr_size, %ecx
107 shr $2, %ecx
108 movl %ecx, %ebx
109 movl %edi, %esi
Kyösti Mälkki54d6a282018-05-25 06:03:14 +0300110 rep lodsl
Kyösti Mälkkidc6bb6c2019-11-08 00:08:55 +0200111 movl %ebx, %ecx
Kyösti Mälkki54d6a282018-05-25 06:03:14 +0300112 xorl %eax, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200113 rep stosl
114
Kyösti Mälkki8a2f1672016-07-20 13:29:59 +0300115 post_code(0x2d)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200116 /* Enable Cache-as-RAM mode by disabling cache. */
117 movl %cr0, %eax
Patrick Georgi05e740f2012-03-31 12:52:21 +0200118 orl $CR0_CacheDisable, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200119 movl %eax, %cr0
120
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200121 /* Enable cache for our code in Flash because we do XIP here */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700122 movl $MTRR_PHYS_BASE(1), %ecx
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200123 xorl %edx, %edx
124 /*
125 * IMPORTANT: The following calculation _must_ be done at runtime. See
Stefan Taunerde028782018-08-19 20:02:05 +0200126 * https://mail.coreboot.org/pipermail/coreboot/2010-October/060922.html
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200127 */
Kyösti Mälkkice9f4222018-06-25 18:53:36 +0300128 movl $_program, %eax
Kyösti Mälkkidc6bb6c2019-11-08 00:08:55 +0200129 andl $_xip_mtrr_mask, %eax
Kyösti Mälkkidc4820b2016-07-21 19:51:01 +0300130 orl $MTRR_TYPE_WRPROT, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200131 wrmsr
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700132 movl $MTRR_PHYS_MASK(1), %ecx
Kyösti Mälkkia860c682012-02-28 02:06:45 +0200133 rdmsr
Kyösti Mälkkidc6bb6c2019-11-08 00:08:55 +0200134 movl $_xip_mtrr_mask, %eax
135 orl $MTRR_PHYS_MASK_VALID, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200136 wrmsr
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200137
Kyösti Mälkki8a2f1672016-07-20 13:29:59 +0300138 post_code(0x2e)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200139 /* Enable cache. */
140 movl %cr0, %eax
Patrick Georgi05e740f2012-03-31 12:52:21 +0200141 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200142 movl %eax, %cr0
143
Kyösti Mälkki39915bc2016-11-08 12:13:15 +0200144 /* Setup the stack. */
Arthur Heymansdf9cdcf2019-11-09 06:50:20 +0100145 mov $_ecar_stack, %esp
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200146
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200147 /* Need to align stack to 16 bytes at call instruction. Account for
148 the pushes below. */
149 andl $0xfffffff0, %esp
150 subl $4, %esp
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200151
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200152 /* push TSC and BIST to stack */
153 movd %mm0, %eax
Elyes HAOUAS87930b32019-01-16 12:41:57 +0100154 pushl %eax /* BIST */
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200155 movd %mm2, %eax
156 pushl %eax /* tsc[63:32] */
157 movd %mm1, %eax
Elyes HAOUAS87930b32019-01-16 12:41:57 +0100158 pushl %eax /* tsc[31:0] */
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200159
160before_c_entry:
161 post_code(0x29)
162 call bootblock_c_entry_bist
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200163
Kyösti Mälkkiaea8eec2018-06-04 08:49:17 +0300164 /* Should never see this postcode */
165 post_code(POST_DEAD_CODE)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200166
167.Lhlt:
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200168 hlt
169 jmp .Lhlt
170
Kyösti Mälkki5bc46d82018-06-14 06:21:53 +0300171fixed_mtrr_list:
172 .word MTRR_FIX_64K_00000
173 .word MTRR_FIX_16K_80000
174 .word MTRR_FIX_16K_A0000
175 .word MTRR_FIX_4K_C0000
176 .word MTRR_FIX_4K_C8000
177 .word MTRR_FIX_4K_D0000
178 .word MTRR_FIX_4K_D8000
179 .word MTRR_FIX_4K_E0000
180 .word MTRR_FIX_4K_E8000
181 .word MTRR_FIX_4K_F0000
182 .word MTRR_FIX_4K_F8000
183fixed_mtrr_list_size = . - fixed_mtrr_list
Kyösti Mälkkiaea8eec2018-06-04 08:49:17 +0300184
185_cache_as_ram_setup_end: