blob: 1431d323e6f6bc5f579b3cca37f75e377e55793f [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
Martin Rothc87ab012022-11-20 19:32:51 -07003#include <cpu/intel/post_codes.h>
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +02004#include <cpu/x86/mtrr.h>
Patrick Georgi05e740f2012-03-31 12:52:21 +02005#include <cpu/x86/cache.h>
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +02006#include <cpu/x86/post_code.h>
7
Kyösti Mälkki7522a8f2020-11-20 16:47:38 +02008.section .init
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +02009.global bootblock_pre_c_entry
10
Kyösti Mälkkiaea8eec2018-06-04 08:49:17 +030011.code32
12_cache_as_ram_setup:
13
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +020014bootblock_pre_c_entry:
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020015
16cache_as_ram:
lilacious40cb3fe2023-06-21 23:24:14 +020017 post_code(POSTCODE_BOOTBLOCK_CAR)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020018
Kyösti Mälkki5bc46d82018-06-14 06:21:53 +030019 /* Clear/disable fixed MTRRs */
20 mov $fixed_mtrr_list_size, %ebx
21 xor %eax, %eax
22 xor %edx, %edx
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020023
Kyösti Mälkki5bc46d82018-06-14 06:21:53 +030024clear_fixed_mtrr:
25 add $-2, %ebx
26 movzwl fixed_mtrr_list(%ebx), %ecx
27 wrmsr
28 jnz clear_fixed_mtrr
29
Elyes HAOUAS02820ca2018-09-30 07:44:39 +020030 /* Figure out how many MTRRs we have, and clear them out */
Kyösti Mälkki5bc46d82018-06-14 06:21:53 +030031 mov $MTRR_CAP_MSR, %ecx
32 rdmsr
33 movzb %al, %ebx /* Number of variable MTRRs */
34 mov $MTRR_PHYS_BASE(0), %ecx
35 xor %eax, %eax
36 xor %edx, %edx
37
38clear_var_mtrr:
39 wrmsr
40 inc %ecx
41 wrmsr
42 inc %ecx
43 dec %ebx
44 jnz clear_var_mtrr
Martin Rothc87ab012022-11-20 19:32:51 -070045 post_code(POST_SOC_SET_DEF_MTRR_TYPE)
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +020046
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020047 /* Configure the default memory type to uncacheable. */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070048 movl $MTRR_DEF_TYPE_MSR, %ecx
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020049 rdmsr
50 andl $(~0x00000cff), %eax
51 wrmsr
52
Martin Rothc87ab012022-11-20 19:32:51 -070053 post_code(POST_SOC_DETERMINE_CPU_ADDR_BITS)
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +020054
Kyösti Mälkki54d6a282018-05-25 06:03:14 +030055 /* Determine CPU_ADDR_BITS and load PHYSMASK high word to %edx. */
Kyösti Mälkkia860c682012-02-28 02:06:45 +020056 movl $1, %eax
57 cpuid
Elyes HAOUAS168ef392017-06-27 22:54:42 +020058 andl $(1 << 6 | 1 << 17), %edx /* PAE or PSE36 */
Kyösti Mälkkia860c682012-02-28 02:06:45 +020059 jz addrsize_set_high
60 movl $0x0f, %edx
61
62 /* Preload high word of address mask (in %edx) for Variable
Kyösti Mälkki54d6a282018-05-25 06:03:14 +030063 MTRRs 0 and 1. */
Kyösti Mälkkia860c682012-02-28 02:06:45 +020064addrsize_set_high:
65 xorl %eax, %eax
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070066 movl $MTRR_PHYS_MASK(0), %ecx
Kyösti Mälkkia860c682012-02-28 02:06:45 +020067 wrmsr
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070068 movl $MTRR_PHYS_MASK(1), %ecx
Kyösti Mälkkia860c682012-02-28 02:06:45 +020069 wrmsr
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +020070
Martin Rothc87ab012022-11-20 19:32:51 -070071 post_code(POST_SOC_SET_CAR_BASE)
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +020072
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020073 /* Set Cache-as-RAM base address. */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070074 movl $(MTRR_PHYS_BASE(0)), %ecx
Kyösti Mälkkidc6bb6c2019-11-08 00:08:55 +020075 movl $_car_mtrr_start, %eax
76 orl $MTRR_TYPE_WRBACK, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020077 xorl %edx, %edx
78 wrmsr
79
80 /* Set Cache-as-RAM mask. */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070081 movl $(MTRR_PHYS_MASK(0)), %ecx
Kyösti Mälkkia860c682012-02-28 02:06:45 +020082 rdmsr
Kyösti Mälkkidc6bb6c2019-11-08 00:08:55 +020083 movl $_car_mtrr_mask, %eax
84 orl $MTRR_PHYS_MASK_VALID, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020085 wrmsr
86
Martin Rothc87ab012022-11-20 19:32:51 -070087 post_code(POST_SOC_ENABLE_MTRRS)
Kyösti Mälkki8a2f1672016-07-20 13:29:59 +030088
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020089 /* Enable MTRR. */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070090 movl $MTRR_DEF_TYPE_MSR, %ecx
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020091 rdmsr
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070092 orl $MTRR_DEF_TYPE_EN, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020093 wrmsr
94
Martin Rothc87ab012022-11-20 19:32:51 -070095 post_code(POST_SOC_ENABLE_CACHE)
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +020096
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020097 /* Enable cache (CR0.CD = 0, CR0.NW = 0). */
Kyösti Mälkkif9d1a422012-02-28 01:45:44 +020098 movl %cr0, %eax
Patrick Georgi05e740f2012-03-31 12:52:21 +020099 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200100 invd
101 movl %eax, %cr0
102
Kyösti Mälkki54d6a282018-05-25 06:03:14 +0300103 /* Read then clear the CAR region. This will also fill up the cache.
104 * IMPORTANT: The read is mandatory.
105 */
Kyösti Mälkkif9d1a422012-02-28 01:45:44 +0200106 cld
Kyösti Mälkkidc6bb6c2019-11-08 00:08:55 +0200107 movl $_car_mtrr_start, %edi
108 movl $_car_mtrr_size, %ecx
109 shr $2, %ecx
110 movl %ecx, %ebx
111 movl %edi, %esi
Kyösti Mälkki54d6a282018-05-25 06:03:14 +0300112 rep lodsl
Kyösti Mälkkidc6bb6c2019-11-08 00:08:55 +0200113 movl %ebx, %ecx
Kyösti Mälkki54d6a282018-05-25 06:03:14 +0300114 xorl %eax, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200115 rep stosl
116
Martin Rothc87ab012022-11-20 19:32:51 -0700117 post_code(POST_SOC_DISABLE_CACHE)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200118 /* Enable Cache-as-RAM mode by disabling cache. */
119 movl %cr0, %eax
Patrick Georgi05e740f2012-03-31 12:52:21 +0200120 orl $CR0_CacheDisable, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200121 movl %eax, %cr0
122
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200123 /* Enable cache for our code in Flash because we do XIP here */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700124 movl $MTRR_PHYS_BASE(1), %ecx
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200125 xorl %edx, %edx
Kyösti Mälkkice9f4222018-06-25 18:53:36 +0300126 movl $_program, %eax
Kyösti Mälkkidc6bb6c2019-11-08 00:08:55 +0200127 andl $_xip_mtrr_mask, %eax
Kyösti Mälkkidc4820b2016-07-21 19:51:01 +0300128 orl $MTRR_TYPE_WRPROT, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200129 wrmsr
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700130 movl $MTRR_PHYS_MASK(1), %ecx
Kyösti Mälkkia860c682012-02-28 02:06:45 +0200131 rdmsr
Kyösti Mälkkidc6bb6c2019-11-08 00:08:55 +0200132 movl $_xip_mtrr_mask, %eax
133 orl $MTRR_PHYS_MASK_VALID, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200134 wrmsr
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200135
Martin Rothc87ab012022-11-20 19:32:51 -0700136 post_code(POST_SOC_FILL_CACHE)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200137 /* Enable cache. */
138 movl %cr0, %eax
Patrick Georgi05e740f2012-03-31 12:52:21 +0200139 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200140 movl %eax, %cr0
141
Kyösti Mälkki39915bc2016-11-08 12:13:15 +0200142 /* Setup the stack. */
Arthur Heymansdf9cdcf2019-11-09 06:50:20 +0100143 mov $_ecar_stack, %esp
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200144
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200145 /* Need to align stack to 16 bytes at call instruction. Account for
146 the pushes below. */
147 andl $0xfffffff0, %esp
148 subl $4, %esp
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200149
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200150 /* push TSC and BIST to stack */
151 movd %mm0, %eax
Elyes HAOUAS87930b32019-01-16 12:41:57 +0100152 pushl %eax /* BIST */
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200153 movd %mm2, %eax
154 pushl %eax /* tsc[63:32] */
155 movd %mm1, %eax
Elyes HAOUAS87930b32019-01-16 12:41:57 +0100156 pushl %eax /* tsc[31:0] */
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200157
158before_c_entry:
Martin Rothc87ab012022-11-20 19:32:51 -0700159 post_code(POST_BOOTBLOCK_BEFORE_C_ENTRY)
Kyösti Mälkkic641f7e2018-12-28 16:54:54 +0200160 call bootblock_c_entry_bist
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200161
Kyösti Mälkkiaea8eec2018-06-04 08:49:17 +0300162 /* Should never see this postcode */
lilacious40cb3fe2023-06-21 23:24:14 +0200163 post_code(POSTCODE_DEAD_CODE)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200164
165.Lhlt:
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200166 hlt
167 jmp .Lhlt
168
Kyösti Mälkki5bc46d82018-06-14 06:21:53 +0300169fixed_mtrr_list:
170 .word MTRR_FIX_64K_00000
171 .word MTRR_FIX_16K_80000
172 .word MTRR_FIX_16K_A0000
173 .word MTRR_FIX_4K_C0000
174 .word MTRR_FIX_4K_C8000
175 .word MTRR_FIX_4K_D0000
176 .word MTRR_FIX_4K_D8000
177 .word MTRR_FIX_4K_E0000
178 .word MTRR_FIX_4K_E8000
179 .word MTRR_FIX_4K_F0000
180 .word MTRR_FIX_4K_F8000
181fixed_mtrr_list_size = . - fixed_mtrr_list
Kyösti Mälkkiaea8eec2018-06-04 08:49:17 +0300182
183_cache_as_ram_setup_end: