blob: 163f4b4d8482377bc42efaf5aa1a7762eae39e46 [file] [log] [blame]
Arthur Heymansdd4d8952018-06-03 12:04:26 +02001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2000,2007 Ronald G. Minnich <rminnich@gmail.com>
5 * Copyright (C) 2007-2008 coresystems GmbH
Arthur Heymansc2ccc972018-06-03 12:09:52 +02006 * Copyright (C) 2012 Kyösti Mälkki <kyosti.malkki@gmail.com>
Arthur Heymansdd4d8952018-06-03 12:04:26 +02007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
18#include <cpu/x86/mtrr.h>
19#include <cpu/x86/cache.h>
20#include <cpu/x86/post_code.h>
21
22#define CACHE_AS_RAM_SIZE (CONFIG_DCACHE_RAM_SIZE \
23 + CONFIG_DCACHE_RAM_MRC_VAR_SIZE)
24#define CACHE_AS_RAM_BASE CONFIG_DCACHE_RAM_BASE
25
Arthur Heymansdd4d8952018-06-03 12:04:26 +020026#define NoEvictMod_MSR 0x2e0
27
28.code32
29_cache_as_ram_setup:
30
31 /* Save the BIST result. */
32 movl %eax, %ebp
33
34cache_as_ram:
35 post_code(0x20)
36
37 /* Send INIT IPI to all excluding ourself. */
38 movl $0x000C4500, %eax
39 movl $0xFEE00300, %esi
40 movl %eax, (%esi)
41
42 /* All CPUs need to be in Wait for SIPI state */
43wait_for_sipi:
44 movl (%esi), %eax
45 bt $12, %eax
46 jc wait_for_sipi
47
48 post_code(0x21)
49 /* Clean-up MTRR_DEF_TYPE_MSR. */
50 movl $MTRR_DEF_TYPE_MSR, %ecx
51 xorl %eax, %eax
52 xorl %edx, %edx
53 wrmsr
54
55 post_code(0x22)
Arthur Heymansc2ccc972018-06-03 12:09:52 +020056 /* Clear/disable fixed MTRRs */
57 mov $fixed_mtrr_list_size, %ebx
58 xor %eax, %eax
59 xor %edx, %edx
60
61clear_fixed_mtrr:
62 add $-2, %ebx
63 movzwl fixed_mtrr_list(%ebx), %ecx
Arthur Heymansdd4d8952018-06-03 12:04:26 +020064 wrmsr
Arthur Heymansc2ccc972018-06-03 12:09:52 +020065 jnz clear_fixed_mtrr
Arthur Heymansdd4d8952018-06-03 12:04:26 +020066
67 /* Zero out all variable range MTRRs. */
68 movl $MTRR_CAP_MSR, %ecx
69 rdmsr
70 andl $0xff, %eax
71 shl $1, %eax
72 movl %eax, %edi
73 movl $0x200, %ecx
74 xorl %eax, %eax
75 xorl %edx, %edx
76clear_var_mtrrs:
77 wrmsr
78 add $1, %ecx
79 dec %edi
80 jnz clear_var_mtrrs
81
Arthur Heymansc2ccc972018-06-03 12:09:52 +020082 /* Determine CPU_ADDR_BITS and load PHYSMASK high word to %edx. */
83 movl $0x80000008, %eax
84 cpuid
85 movb %al, %cl
86 sub $32, %cl
87 movl $1, %edx
88 shl %cl, %edx
89 subl $1, %edx
90
91 /* Preload high word of address mask (in %edx) for Variable
92 * MTRRs 0 and 1.
93 */
94addrsize_set_high:
95 xorl %eax, %eax
96 movl $MTRR_PHYS_MASK(0), %ecx
97 wrmsr
98 movl $MTRR_PHYS_MASK(1), %ecx
99 wrmsr
100
101
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200102 post_code(0x23)
103 /* Set Cache-as-RAM base address. */
104 movl $(MTRR_PHYS_BASE(0)), %ecx
105 movl $(CACHE_AS_RAM_BASE | MTRR_TYPE_WRBACK), %eax
106 xorl %edx, %edx
107 wrmsr
108
109 post_code(0x24)
110 /* Set Cache-as-RAM mask. */
111 movl $(MTRR_PHYS_MASK(0)), %ecx
Arthur Heymansc2ccc972018-06-03 12:09:52 +0200112 rdmsr
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200113 movl $(~(CACHE_AS_RAM_SIZE - 1) | MTRR_PHYS_MASK_VALID), %eax
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200114 wrmsr
115
116 post_code(0x25)
117
118 /* Enable MTRR. */
119 movl $MTRR_DEF_TYPE_MSR, %ecx
120 rdmsr
121 orl $MTRR_DEF_TYPE_EN, %eax
122 wrmsr
123
124 /* Enable cache (CR0.CD = 0, CR0.NW = 0). */
125 movl %cr0, %eax
126 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
127 invd
128 movl %eax, %cr0
129
130 /* enable the 'no eviction' mode */
Arthur Heymansa28befd2018-12-20 13:59:34 +0100131 movl $NoEvictMod_MSR, %ecx
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200132 rdmsr
Arthur Heymansa28befd2018-12-20 13:59:34 +0100133 orl $1, %eax
134 andl $~2, %eax
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200135 wrmsr
136
137 /* Clear the cache memory region. This will also fill up the cache. */
138 movl $CACHE_AS_RAM_BASE, %esi
139 movl %esi, %edi
140 movl $(CACHE_AS_RAM_SIZE >> 2), %ecx
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200141 xorl %eax, %eax
142 rep stosl
143
144 /* enable the 'no eviction run' state */
Arthur Heymansa28befd2018-12-20 13:59:34 +0100145 movl $NoEvictMod_MSR, %ecx
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200146 rdmsr
Arthur Heymansa28befd2018-12-20 13:59:34 +0100147 orl $3, %eax
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200148 wrmsr
149
150 post_code(0x26)
151 /* Enable Cache-as-RAM mode by disabling cache. */
152 movl %cr0, %eax
153 orl $CR0_CacheDisable, %eax
154 movl %eax, %cr0
155
156 /* Enable cache for our code in Flash because we do XIP here */
157 movl $MTRR_PHYS_BASE(1), %ecx
158 xorl %edx, %edx
Arthur Heymansc2ccc972018-06-03 12:09:52 +0200159 movl $CACHE_ROM_BASE | MTRR_TYPE_WRPROT, %eax
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200160 wrmsr
161
162 movl $MTRR_PHYS_MASK(1), %ecx
Arthur Heymansc2ccc972018-06-03 12:09:52 +0200163 rdmsr
164 movl $(~(CACHE_ROM_SIZE - 1) | MTRR_PHYS_MASK_VALID), %eax
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200165 wrmsr
166
167 post_code(0x28)
168 /* Enable cache. */
169 movl %cr0, %eax
170 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
171 movl %eax, %cr0
172
173 /* Setup the stack. */
174 movl $(CONFIG_DCACHE_RAM_BASE + CONFIG_DCACHE_RAM_SIZE), %eax
175 movl %eax, %esp
176
Arthur Heymans348b79f2018-06-03 17:14:19 +0200177 /* Align the stack 16 bytes */
178 andl $0xfffffff0, %esp
179 /* Account for pushing the BIST result */
180 subl $12, %esp
181
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200182 /* Restore the BIST result. */
183 movl %ebp, %eax
184 movl %esp, %ebp
185 pushl %eax
186
187before_romstage:
188 post_code(0x29)
189 /* Call romstage.c main function. */
190 call romstage_main
191
192 /* Should never see this postcode */
193 post_code(POST_DEAD_CODE)
194
195
196.Lhlt:
197 hlt
198 jmp .Lhlt
199
Arthur Heymansc2ccc972018-06-03 12:09:52 +0200200fixed_mtrr_list:
201 .word MTRR_FIX_64K_00000
202 .word MTRR_FIX_16K_80000
203 .word MTRR_FIX_16K_A0000
204 .word MTRR_FIX_4K_C0000
205 .word MTRR_FIX_4K_C8000
206 .word MTRR_FIX_4K_D0000
207 .word MTRR_FIX_4K_D8000
208 .word MTRR_FIX_4K_E0000
209 .word MTRR_FIX_4K_E8000
210 .word MTRR_FIX_4K_F0000
211 .word MTRR_FIX_4K_F8000
212fixed_mtrr_list_size = . - fixed_mtrr_list
Arthur Heymansdd4d8952018-06-03 12:04:26 +0200213
214_cache_as_ram_setup_end: