blob: 887bb22477d919a2b05df7e7b2620f149de28301 [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älkki7522a8f2020-11-20 16:47:38 +02007.section .init
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +02008.global bootblock_pre_c_entry
9
Kyösti Mälkkiaea8eec2018-06-04 08:49:17 +030010.code32
11_cache_as_ram_setup:
12
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +020013bootblock_pre_c_entry:
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020014
15cache_as_ram:
16 post_code(0x20)
17
Kyösti Mälkki5bc46d82018-06-14 06:21:53 +030018 /* Clear/disable fixed MTRRs */
19 mov $fixed_mtrr_list_size, %ebx
20 xor %eax, %eax
21 xor %edx, %edx
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020022
Kyösti Mälkki5bc46d82018-06-14 06:21:53 +030023clear_fixed_mtrr:
24 add $-2, %ebx
25 movzwl fixed_mtrr_list(%ebx), %ecx
26 wrmsr
27 jnz clear_fixed_mtrr
28
Elyes HAOUAS02820ca2018-09-30 07:44:39 +020029 /* Figure out how many MTRRs we have, and clear them out */
Kyösti Mälkki5bc46d82018-06-14 06:21:53 +030030 mov $MTRR_CAP_MSR, %ecx
31 rdmsr
32 movzb %al, %ebx /* Number of variable MTRRs */
33 mov $MTRR_PHYS_BASE(0), %ecx
34 xor %eax, %eax
35 xor %edx, %edx
36
37clear_var_mtrr:
38 wrmsr
39 inc %ecx
40 wrmsr
41 inc %ecx
42 dec %ebx
43 jnz clear_var_mtrr
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +020044 post_code(0x21)
45
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020046 /* Configure the default memory type to uncacheable. */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070047 movl $MTRR_DEF_TYPE_MSR, %ecx
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020048 rdmsr
49 andl $(~0x00000cff), %eax
50 wrmsr
51
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +020052 post_code(0x22)
53
Kyösti Mälkki54d6a282018-05-25 06:03:14 +030054 /* Determine CPU_ADDR_BITS and load PHYSMASK high word to %edx. */
Kyösti Mälkkia860c682012-02-28 02:06:45 +020055 movl $1, %eax
56 cpuid
Elyes HAOUAS168ef392017-06-27 22:54:42 +020057 andl $(1 << 6 | 1 << 17), %edx /* PAE or PSE36 */
Kyösti Mälkkia860c682012-02-28 02:06:45 +020058 jz addrsize_set_high
59 movl $0x0f, %edx
60
61 /* Preload high word of address mask (in %edx) for Variable
Kyösti Mälkki54d6a282018-05-25 06:03:14 +030062 MTRRs 0 and 1. */
Kyösti Mälkkia860c682012-02-28 02:06:45 +020063addrsize_set_high:
64 xorl %eax, %eax
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070065 movl $MTRR_PHYS_MASK(0), %ecx
Kyösti Mälkkia860c682012-02-28 02:06:45 +020066 wrmsr
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070067 movl $MTRR_PHYS_MASK(1), %ecx
Kyösti Mälkkia860c682012-02-28 02:06:45 +020068 wrmsr
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +020069
70 post_code(0x2a)
71
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020072 /* Set Cache-as-RAM base address. */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070073 movl $(MTRR_PHYS_BASE(0)), %ecx
Kyösti Mälkkidc6bb6c2019-11-08 00:08:55 +020074 movl $_car_mtrr_start, %eax
75 orl $MTRR_TYPE_WRBACK, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020076 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
Kyösti Mälkkidc6bb6c2019-11-08 00:08:55 +020082 movl $_car_mtrr_mask, %eax
83 orl $MTRR_PHYS_MASK_VALID, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020084 wrmsr
85
Kyösti Mälkki8a2f1672016-07-20 13:29:59 +030086 post_code(0x2b)
87
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020088 /* Enable MTRR. */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070089 movl $MTRR_DEF_TYPE_MSR, %ecx
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020090 rdmsr
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070091 orl $MTRR_DEF_TYPE_EN, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020092 wrmsr
93
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +020094 post_code(0x2c)
95
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020096 /* Enable cache (CR0.CD = 0, CR0.NW = 0). */
Kyösti Mälkkif9d1a422012-02-28 01:45:44 +020097 movl %cr0, %eax
Patrick Georgi05e740f2012-03-31 12:52:21 +020098 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020099 invd
100 movl %eax, %cr0
101
Kyösti Mälkki54d6a282018-05-25 06:03:14 +0300102 /* Read then clear the CAR region. This will also fill up the cache.
103 * IMPORTANT: The read is mandatory.
104 */
Kyösti Mälkkif9d1a422012-02-28 01:45:44 +0200105 cld
Kyösti Mälkkidc6bb6c2019-11-08 00:08:55 +0200106 movl $_car_mtrr_start, %edi
107 movl $_car_mtrr_size, %ecx
108 shr $2, %ecx
109 movl %ecx, %ebx
110 movl %edi, %esi
Kyösti Mälkki54d6a282018-05-25 06:03:14 +0300111 rep lodsl
Kyösti Mälkkidc6bb6c2019-11-08 00:08:55 +0200112 movl %ebx, %ecx
Kyösti Mälkki54d6a282018-05-25 06:03:14 +0300113 xorl %eax, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200114 rep stosl
115
Kyösti Mälkki8a2f1672016-07-20 13:29:59 +0300116 post_code(0x2d)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200117 /* Enable Cache-as-RAM mode by disabling cache. */
118 movl %cr0, %eax
Patrick Georgi05e740f2012-03-31 12:52:21 +0200119 orl $CR0_CacheDisable, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200120 movl %eax, %cr0
121
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200122 /* Enable cache for our code in Flash because we do XIP here */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700123 movl $MTRR_PHYS_BASE(1), %ecx
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200124 xorl %edx, %edx
Kyösti Mälkkice9f4222018-06-25 18:53:36 +0300125 movl $_program, %eax
Kyösti Mälkkidc6bb6c2019-11-08 00:08:55 +0200126 andl $_xip_mtrr_mask, %eax
Kyösti Mälkkidc4820b2016-07-21 19:51:01 +0300127 orl $MTRR_TYPE_WRPROT, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200128 wrmsr
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700129 movl $MTRR_PHYS_MASK(1), %ecx
Kyösti Mälkkia860c682012-02-28 02:06:45 +0200130 rdmsr
Kyösti Mälkkidc6bb6c2019-11-08 00:08:55 +0200131 movl $_xip_mtrr_mask, %eax
132 orl $MTRR_PHYS_MASK_VALID, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200133 wrmsr
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200134
Kyösti Mälkki8a2f1672016-07-20 13:29:59 +0300135 post_code(0x2e)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200136 /* Enable cache. */
137 movl %cr0, %eax
Patrick Georgi05e740f2012-03-31 12:52:21 +0200138 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200139 movl %eax, %cr0
140
Kyösti Mälkki39915bc2016-11-08 12:13:15 +0200141 /* Setup the stack. */
Arthur Heymansdf9cdcf2019-11-09 06:50:20 +0100142 mov $_ecar_stack, %esp
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200143
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200144 /* Need to align stack to 16 bytes at call instruction. Account for
145 the pushes below. */
146 andl $0xfffffff0, %esp
147 subl $4, %esp
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200148
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200149 /* push TSC and BIST to stack */
150 movd %mm0, %eax
Elyes HAOUAS87930b32019-01-16 12:41:57 +0100151 pushl %eax /* BIST */
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200152 movd %mm2, %eax
153 pushl %eax /* tsc[63:32] */
154 movd %mm1, %eax
Elyes HAOUAS87930b32019-01-16 12:41:57 +0100155 pushl %eax /* tsc[31:0] */
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200156
157before_c_entry:
158 post_code(0x29)
159 call bootblock_c_entry_bist
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200160
Kyösti Mälkkiaea8eec2018-06-04 08:49:17 +0300161 /* Should never see this postcode */
162 post_code(POST_DEAD_CODE)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200163
164.Lhlt:
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200165 hlt
166 jmp .Lhlt
167
Kyösti Mälkki5bc46d82018-06-14 06:21:53 +0300168fixed_mtrr_list:
169 .word MTRR_FIX_64K_00000
170 .word MTRR_FIX_16K_80000
171 .word MTRR_FIX_16K_A0000
172 .word MTRR_FIX_4K_C0000
173 .word MTRR_FIX_4K_C8000
174 .word MTRR_FIX_4K_D0000
175 .word MTRR_FIX_4K_D8000
176 .word MTRR_FIX_4K_E0000
177 .word MTRR_FIX_4K_E8000
178 .word MTRR_FIX_4K_F0000
179 .word MTRR_FIX_4K_F8000
180fixed_mtrr_list_size = . - fixed_mtrr_list
Kyösti Mälkkiaea8eec2018-06-04 08:49:17 +0300181
182_cache_as_ram_setup_end: