blob: 35dd164e7f20d6debdba1753d3c8d20d3cd7fb6f [file] [log] [blame]
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +02001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2000,2007 Ronald G. Minnich <rminnich@gmail.com>
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +02005 * Copyright (C) 2005 Tyan (written by Yinghai Lu for Tyan)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +02006 * Copyright (C) 2007-2008 coresystems GmbH
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +02007 * Copyright (C) 2012 Kyösti Mälkki <kyosti.malkki@gmail.com>
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +02008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2 of the License.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020017 */
18
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020019#include <cpu/x86/mtrr.h>
Patrick Georgi05e740f2012-03-31 12:52:21 +020020#include <cpu/x86/cache.h>
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020021#include <cpu/x86/post_code.h>
22
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020023#define CACHE_AS_RAM_SIZE CONFIG_DCACHE_RAM_SIZE
24#define CACHE_AS_RAM_BASE CONFIG_DCACHE_RAM_BASE
25
Kyösti Mälkkiaea8eec2018-06-04 08:49:17 +030026.code32
27_cache_as_ram_setup:
28
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020029 /* Save the BIST result. */
30 movl %eax, %ebp
31
32cache_as_ram:
33 post_code(0x20)
34
Kyösti Mälkki5bc46d82018-06-14 06:21:53 +030035 /* Clear/disable fixed MTRRs */
36 mov $fixed_mtrr_list_size, %ebx
37 xor %eax, %eax
38 xor %edx, %edx
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020039
Kyösti Mälkki5bc46d82018-06-14 06:21:53 +030040clear_fixed_mtrr:
41 add $-2, %ebx
42 movzwl fixed_mtrr_list(%ebx), %ecx
43 wrmsr
44 jnz clear_fixed_mtrr
45
46 /* Figure put how many MTRRs we have, and clear them out */
47 mov $MTRR_CAP_MSR, %ecx
48 rdmsr
49 movzb %al, %ebx /* Number of variable MTRRs */
50 mov $MTRR_PHYS_BASE(0), %ecx
51 xor %eax, %eax
52 xor %edx, %edx
53
54clear_var_mtrr:
55 wrmsr
56 inc %ecx
57 wrmsr
58 inc %ecx
59 dec %ebx
60 jnz clear_var_mtrr
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +020061 post_code(0x21)
62
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020063 /* Configure the default memory type to uncacheable. */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070064 movl $MTRR_DEF_TYPE_MSR, %ecx
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020065 rdmsr
66 andl $(~0x00000cff), %eax
67 wrmsr
68
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +020069 post_code(0x22)
70
Kyösti Mälkki54d6a282018-05-25 06:03:14 +030071 /* Determine CPU_ADDR_BITS and load PHYSMASK high word to %edx. */
Kyösti Mälkkia860c682012-02-28 02:06:45 +020072 movl $1, %eax
73 cpuid
Elyes HAOUAS168ef392017-06-27 22:54:42 +020074 andl $(1 << 6 | 1 << 17), %edx /* PAE or PSE36 */
Kyösti Mälkkia860c682012-02-28 02:06:45 +020075 jz addrsize_set_high
76 movl $0x0f, %edx
77
78 /* Preload high word of address mask (in %edx) for Variable
Kyösti Mälkki54d6a282018-05-25 06:03:14 +030079 MTRRs 0 and 1. */
Kyösti Mälkkia860c682012-02-28 02:06:45 +020080addrsize_set_high:
81 xorl %eax, %eax
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070082 movl $MTRR_PHYS_MASK(0), %ecx
Kyösti Mälkkia860c682012-02-28 02:06:45 +020083 wrmsr
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070084 movl $MTRR_PHYS_MASK(1), %ecx
Kyösti Mälkkia860c682012-02-28 02:06:45 +020085 wrmsr
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +020086
87 post_code(0x2a)
88
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020089 /* Set Cache-as-RAM base address. */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070090 movl $(MTRR_PHYS_BASE(0)), %ecx
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020091 movl $(CACHE_AS_RAM_BASE | MTRR_TYPE_WRBACK), %eax
92 xorl %edx, %edx
93 wrmsr
94
95 /* Set Cache-as-RAM mask. */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070096 movl $(MTRR_PHYS_MASK(0)), %ecx
Kyösti Mälkkia860c682012-02-28 02:06:45 +020097 rdmsr
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070098 movl $(~(CACHE_AS_RAM_SIZE - 1) | MTRR_PHYS_MASK_VALID), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +020099 wrmsr
100
Kyösti Mälkki8a2f1672016-07-20 13:29:59 +0300101 post_code(0x2b)
102
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200103 /* Enable MTRR. */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700104 movl $MTRR_DEF_TYPE_MSR, %ecx
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200105 rdmsr
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700106 orl $MTRR_DEF_TYPE_EN, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200107 wrmsr
108
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +0200109 post_code(0x2c)
110
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200111 /* Enable cache (CR0.CD = 0, CR0.NW = 0). */
Kyösti Mälkkif9d1a422012-02-28 01:45:44 +0200112 movl %cr0, %eax
Patrick Georgi05e740f2012-03-31 12:52:21 +0200113 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200114 invd
115 movl %eax, %cr0
116
Kyösti Mälkki54d6a282018-05-25 06:03:14 +0300117 /* Read then clear the CAR region. This will also fill up the cache.
118 * IMPORTANT: The read is mandatory.
119 */
120 movl $CACHE_AS_RAM_BASE, %esi
121 movl %esi, %edi
Kyösti Mälkkif9d1a422012-02-28 01:45:44 +0200122 cld
Stefan Reinauer4a45ec42015-07-07 00:54:05 +0200123 movl $(CACHE_AS_RAM_SIZE >> 2), %ecx
Kyösti Mälkki54d6a282018-05-25 06:03:14 +0300124 rep lodsl
125 movl $(CACHE_AS_RAM_SIZE >> 2), %ecx
126 xorl %eax, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200127 rep stosl
128
Kyösti Mälkki8a2f1672016-07-20 13:29:59 +0300129 post_code(0x2d)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200130 /* Enable Cache-as-RAM mode by disabling cache. */
131 movl %cr0, %eax
Patrick Georgi05e740f2012-03-31 12:52:21 +0200132 orl $CR0_CacheDisable, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200133 movl %eax, %cr0
134
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200135 /* Enable cache for our code in Flash because we do XIP here */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700136 movl $MTRR_PHYS_BASE(1), %ecx
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200137 xorl %edx, %edx
138 /*
139 * IMPORTANT: The following calculation _must_ be done at runtime. See
Paul Menzela8843de2017-06-05 12:33:23 +0200140 * https://www.coreboot.org/pipermail/coreboot/2010-October/060855.html
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200141 */
Kyösti Mälkkice9f4222018-06-25 18:53:36 +0300142 movl $_program, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200143 andl $(~(CONFIG_XIP_ROM_SIZE - 1)), %eax
Kyösti Mälkkidc4820b2016-07-21 19:51:01 +0300144 orl $MTRR_TYPE_WRPROT, %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200145 wrmsr
146
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700147 movl $MTRR_PHYS_MASK(1), %ecx
Kyösti Mälkkia860c682012-02-28 02:06:45 +0200148 rdmsr
Alexandru Gagniuc86091f92015-09-30 20:23:09 -0700149 movl $(~(CONFIG_XIP_ROM_SIZE - 1) | MTRR_PHYS_MASK_VALID), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200150 wrmsr
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200151
Kyösti Mälkki8a2f1672016-07-20 13:29:59 +0300152 post_code(0x2e)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200153 /* Enable cache. */
154 movl %cr0, %eax
Patrick Georgi05e740f2012-03-31 12:52:21 +0200155 andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200156 movl %eax, %cr0
157
Kyösti Mälkki39915bc2016-11-08 12:13:15 +0200158 /* Setup the stack. */
159 movl $(CONFIG_DCACHE_RAM_BASE + CONFIG_DCACHE_RAM_SIZE), %eax
160 movl %eax, %esp
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200161
162 /* Restore the BIST result. */
163 movl %ebp, %eax
164 movl %esp, %ebp
165 pushl %eax
166
Kyösti Mälkki8a2f1672016-07-20 13:29:59 +0300167before_romstage:
Kyösti Mälkki0078ceb2012-02-28 02:02:27 +0200168 post_code(0x2f)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200169 /* Call romstage.c main function. */
Kyösti Mälkki408d3922016-06-17 10:43:48 +0300170 call romstage_main
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200171
Kyösti Mälkkiaea8eec2018-06-04 08:49:17 +0300172 /* Should never see this postcode */
173 post_code(POST_DEAD_CODE)
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200174
175.Lhlt:
Kyösti Mälkki5a660ca2012-02-28 00:15:30 +0200176 hlt
177 jmp .Lhlt
178
Kyösti Mälkki5bc46d82018-06-14 06:21:53 +0300179fixed_mtrr_list:
180 .word MTRR_FIX_64K_00000
181 .word MTRR_FIX_16K_80000
182 .word MTRR_FIX_16K_A0000
183 .word MTRR_FIX_4K_C0000
184 .word MTRR_FIX_4K_C8000
185 .word MTRR_FIX_4K_D0000
186 .word MTRR_FIX_4K_D8000
187 .word MTRR_FIX_4K_E0000
188 .word MTRR_FIX_4K_E8000
189 .word MTRR_FIX_4K_F0000
190 .word MTRR_FIX_4K_F8000
191fixed_mtrr_list_size = . - fixed_mtrr_list
Kyösti Mälkkiaea8eec2018-06-04 08:49:17 +0300192
193_cache_as_ram_setup_end: