summaryrefslogtreecommitdiffstats
path: root/arch/x86/amd/k8/stage1.c
blob: 358a0f46207920fb9b1437d21190106223c0e6c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
 * This file is part of the coreboot project.
 *
 * Copyright (C) 2007 Advanced Micro Devices, Inc.
 * Copyright (C) 2009 Marc Jones <marcj303@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */

#include <mainboard.h>
#include <types.h>
#include <lib.h>
#include <console.h>
#include <msr.h>
#include <macros.h>
#include <cpu.h>
#include <amd/k8/k8.h>
#include <stage1.h>

/**
 * Set the MTRRs of the current core for initial ram access.
 * Be warned, this function is used by the BSP and all AP cores. TOM and TOM2
 * of the calling core must be setup. This function may be called several times
 * depending on MEM_TRAIN_SEQ.
 */
void set_mtrr_ram_access(void)
{
	struct msr msr;
	u8 mtrr_reg;
	u32 mem_sizek;
	u32 mem_basek;

	disable_cache();

	/* 0 - 640KB */
	stage1_set_fix_mtrr(MTRRfix64K_00000_MSR,
						 MTRR_READ_MEM | MTRR_WRITE_MEM | MTRR_TYPE_WRBACK);
	stage1_set_fix_mtrr(MTRRfix16K_80000_MSR,
						 MTRR_READ_MEM | MTRR_WRITE_MEM | MTRR_TYPE_WRBACK);

	/* 0xA0000 - 0xC0000 (UC to video card) */
	stage1_set_fix_mtrr(MTRRfix16K_A0000_MSR, MTRR_TYPE_UNCACHEABLE);

	/* 0xC0000 - 1MB */
	stage1_set_fix_mtrr(MTRRfix4K_C0000_MSR,
						 MTRR_READ_MEM | MTRR_WRITE_MEM | MTRR_TYPE_WRBACK);
	stage1_set_fix_mtrr(MTRRfix4K_C8000_MSR,
						 MTRR_READ_MEM | MTRR_WRITE_MEM | MTRR_TYPE_WRBACK);
	stage1_set_fix_mtrr(MTRRfix4K_D0000_MSR,
						 MTRR_READ_MEM | MTRR_WRITE_MEM | MTRR_TYPE_WRBACK);
	stage1_set_fix_mtrr(MTRRfix4K_D8000_MSR,
						 MTRR_READ_MEM | MTRR_WRITE_MEM | MTRR_TYPE_WRBACK);
	stage1_set_fix_mtrr(MTRRfix4K_E0000_MSR,
						 MTRR_READ_MEM | MTRR_WRITE_MEM | MTRR_TYPE_WRBACK);
	stage1_set_fix_mtrr(MTRRfix4K_E8000_MSR,
						 MTRR_READ_MEM | MTRR_WRITE_MEM | MTRR_TYPE_WRBACK);
	stage1_set_fix_mtrr(MTRRfix4K_F0000_MSR,
						 MTRR_READ_MEM | MTRR_WRITE_MEM | MTRR_TYPE_WRBACK);
	stage1_set_fix_mtrr(MTRRfix4K_F8000_MSR,
						 MTRR_READ_MEM | MTRR_WRITE_MEM | MTRR_TYPE_WRBACK);

	/* 0MB - TOM */
	/* The fixed MTRRS will override 0MB-1MB but because the size needs to
	 * be power of two aligned we want the base to be power of two aligned(0).
	 * Otherwise we will use all the MTRRs up.
	 */
	msr = rdmsr(TOP_MEM);
	mem_sizek = stage1_resk((msr.hi << 32) | msr.lo);
	mem_basek = 0x0;
	mtrr_reg = 0;
	mtrr_reg = stage1_range_to_mtrr(mtrr_reg, mem_basek, mem_sizek,
									MTRR_TYPE_WRBACK, CPU_ADDR_BITS);

	/* System ROM (Assume 1MB) */
	mtrr_reg = stage1_range_to_mtrr(mtrr_reg,
				(u32)(0xFFFFFFFF >> 10) - (u32)(CONFIG_COREBOOT_ROMSIZE_KB - 1),
				CONFIG_COREBOOT_ROMSIZE_KB, MTRR_TYPE_WRBACK, CPU_ADDR_BITS);

	/* 4GB - TOM2 */
	msr = rdmsr(SYSCFG_MSR);
	if (msr.lo & SYSCFG_MSR_TOM2En) {
		msr = rdmsr(TOP_MEM2);
		mem_sizek = stage1_resk((msr.hi << 32) | msr.lo);
		mem_basek = (0xFFFFFFFF >> 10) + 1; /* 4GB */
		mtrr_reg = stage1_range_to_mtrr(mtrr_reg, mem_basek, mem_sizek,
										MTRR_TYPE_WRBACK, CPU_ADDR_BITS);
	}

	/* Enable Fixed and Variable MTRRs MSRs*/
	msr = rdmsr(SYSCFG_MSR);
	msr.lo |= (SYSCFG_MSR_MtrrVarDramEn | SYSCFG_MSR_MtrrFixDramEn);
	wrmsr(SYSCFG_MSR, msr);

	/* Enable Fixed and Variable MTRRs */
	msr = rdmsr(MTRRdefType_MSR);
	msr.lo |= 0xC00;
	wrmsr(MTRRdefType_MSR, msr);

	enable_cache();
}


/* This function MUST be inlined as we can not use a stack -- CAR or real ram */
/* By yhlu 6.2005 */
/* Be warned, this function is used by both the BSP and APs to disable the
 * fixed MTRRs used for CAR.
 */
inline __attribute__((always_inline)) void disable_cache_as_ram(void)
{
	__asm__ volatile (

	/* Disable the cache while we change MTRRs */
	"	movl	%cr0, %eax	\n"
	"	orl	$(0x1<<30),%eax	\n"
	"	movl	%eax, %cr0	\n"

	/* clear sth */
	"	movl	$0x269, %ecx	\n"	/* fix4k_c8000 */
	"	xorl	%edx, %edx	\n"
	"	xorl	%eax, %eax	\n"
	"	wrmsr\n\t"
#if CONFIG_CARSIZE > 0x8000
	"	movl	$0x268, %ecx	\n"	/* fix4k_c0000 */
	"	wrmsr	\n"
#endif
	/* disable fixed mtrr for now, it will be enabled by coreboot_ram again*/
	"	movl	$0xC0010010, %ecx	\n"	/*SYSCFG_MSR */
	"	rdmsr	\n"
	"	andl	$(~(3<<18)), %eax	\n"	/*~(MtrrFixDramModEn | MtrrFixDramEn) */
	"	wrmsr	\n"

	/* Set the default memory type to UC, disable fixed MTRRs,
	 * and leave variable MTRRs enabled */
	"	movl	$0x2ff, %ecx	\n"	/* $MTRRdefType_MSR */
	"	xorl	%edx, %edx	\n"
	"	movl	$0x00000800, %eax	\n"
	"	wrmsr	\n"

	/* enable cache */
	"	movl	%cr0, %eax	\n"
	"	andl	$0x9fffffff,%eax	\n"
	"	movl	%eax, %cr0	\n"
	);
}


/**
 * Disable Cache As RAM (CAR) after memory is setup.
 *
 */
void disable_car(void)
{
	/* inlined function that disables the fixed MTRRs that
	 * are used for CAR stack space. The cache tags are still
	 * valid and the stack data is still in place. */
	disable_cache_as_ram();

	/* Now we need to get the cached data to RAM.
	 * The BKDG says that a WBINVD will not flush CAR to RAM (because the
	 * cache tags are not dirty).
	 * Solution:
	 * - Two subsequent memcpy in the same inline asm block, one from stack
	 *   to backup, one from backup to stack.
	 * The solution for Geode of using a inline asm memcpy of the stack
	 * onto itself will not mark the cache tags as dirty on K8.
	 */
	__asm__ __volatile__(
	"	movl %[carbase], %%esi		\n"
	"	movl %[backuplocation], %%edi	\n"
	"	movl %[carsizequads], %%ecx	\n"
	"	cld				\n"
	"	rep movsl			\n"
	"	wbinvd				\n"
	"	movl %[backuplocation], %%esi	\n"
	"	movl %[carbase], %%edi		\n"
	"	movl %[carsizequads], %%ecx	\n"
	"	rep movsl			\n"
	:: [carbase] "i" (CONFIG_CARBASE), [backuplocation] "i" (0x88000),
	   [carsizequads] "i" (CONFIG_CARSIZE/4)
	: "memory", "%edi", "%esi", "%ecx");
	banner(BIOS_DEBUG, "Disable_car: done wbinvd");

	/* We're now running in ram.
	 * Setup the cache for normal operation. */
	set_mtrr_ram_access();
	banner(BIOS_DEBUG, "disable_car: done");
	stage1_phase3();
}