blob: fb8474a19d7c29406b062c73c19de14379044e4a [file] [log] [blame]
Eric Biedermanc84c1902004-10-14 20:13:01 +00001#ifndef CPU_X86_MTRR_H
2#define CPU_X86_MTRR_H
3
Eric Biedermanc84c1902004-10-14 20:13:01 +00004/* These are the region types */
Alexandru Gagniuc86091f92015-09-30 20:23:09 -07005#define MTRR_TYPE_UNCACHEABLE 0
6#define MTRR_TYPE_WRCOMB 1
7#define MTRR_TYPE_WRTHROUGH 4
8#define MTRR_TYPE_WRPROT 5
9#define MTRR_TYPE_WRBACK 6
10#define MTRR_NUM_TYPES 7
Eric Biedermanc84c1902004-10-14 20:13:01 +000011
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070012#define MTRR_CAP_MSR 0x0fe
Lee Leahybfdf2482015-06-18 10:55:19 -070013
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070014#define MTRR_CAP_SMRR (1 << 11)
15#define MTRR_CAP_WC (1 << 10)
16#define MTRR_CAP_FIX (1 << 8)
17#define MTRR_CAP_VCNT 0xff
Lee Leahybfdf2482015-06-18 10:55:19 -070018
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070019#define MTRR_DEF_TYPE_MSR 0x2ff
20#define MTRR_DEF_TYPE_MASK 0xff
21#define MTRR_DEF_TYPE_EN (1 << 11)
22#define MTRR_DEF_TYPE_FIX_EN (1 << 10)
Eric Biedermanc84c1902004-10-14 20:13:01 +000023
Uwe Hermann66d16872010-10-01 07:27:51 +000024
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070025#define SMRR_PHYS_BASE 0x1f2
26#define SMRR_PHYS_MASK 0x1f3
Duncan Laurie7b678922012-01-09 22:05:18 -080027
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070028#define MTRR_PHYS_BASE(reg) (0x200 + 2 * (reg))
29#define MTRR_PHYS_MASK(reg) (MTRR_PHYS_BASE(reg) + 1)
30#define MTRR_PHYS_MASK_VALID (1 << 11)
Eric Biedermanc84c1902004-10-14 20:13:01 +000031
Alexandru Gagniuc86091f92015-09-30 20:23:09 -070032#define NUM_FIXED_RANGES 88
33#define RANGES_PER_FIXED_MTRR 8
34#define MTRR_FIX_64K_00000 0x250
35#define MTRR_FIX_16K_80000 0x258
36#define MTRR_FIX_16K_A0000 0x259
37#define MTRR_FIX_4K_C0000 0x268
38#define MTRR_FIX_4K_C8000 0x269
39#define MTRR_FIX_4K_D0000 0x26a
40#define MTRR_FIX_4K_D8000 0x26b
41#define MTRR_FIX_4K_E0000 0x26c
42#define MTRR_FIX_4K_E8000 0x26d
43#define MTRR_FIX_4K_F0000 0x26e
44#define MTRR_FIX_4K_F8000 0x26f
Eric Biedermanc84c1902004-10-14 20:13:01 +000045
Stefan Reinauer61aee5f2011-04-10 04:15:23 +000046#if !defined (__ASSEMBLER__) && !defined(__PRE_RAM__)
Aaron Durbinbb4e79a2013-03-26 14:09:47 -050047
Aaron Durbin2bebd7b2016-11-10 15:15:35 -060048#include <stdint.h>
49#include <stddef.h>
50
Aaron Durbinbb4e79a2013-03-26 14:09:47 -050051/*
52 * The MTRR code has some side effects that the callers should be aware for.
53 * 1. The call sequence matters. x86_setup_mtrrs() calls
54 * x86_setup_fixed_mtrrs_no_enable() then enable_fixed_mtrrs() (equivalent
55 * of x86_setup_fixed_mtrrs()) then x86_setup_var_mtrrs(). If the callers
56 * want to call the components of x86_setup_mtrrs() because of other
Martin Roth0cb07e32013-07-09 21:46:01 -060057 * requirements the ordering should still preserved.
Aaron Durbinbb4e79a2013-03-26 14:09:47 -050058 * 2. enable_fixed_mtrr() will enable both variable and fixed MTRRs because
59 * of the nature of the global MTRR enable flag. Therefore, all direct
60 * or indirect callers of enable_fixed_mtrr() should ensure that the
61 * variable MTRR MSRs do not contain bad ranges.
Aaron Durbine63be892016-03-07 16:05:36 -060062 *
63 * Note that this function sets up MTRRs for addresses above 4GiB.
Ronald G. Minnich69efaa02013-02-26 10:07:40 -080064 */
Sven Schnelleadfbcb792012-01-10 12:01:43 +010065void x86_setup_mtrrs(void);
Aaron Durbinbb4e79a2013-03-26 14:09:47 -050066/*
Aaron Durbine63be892016-03-07 16:05:36 -060067 * x86_setup_mtrrs_with_detect() does the same thing as x86_setup_mtrrs(), but
68 * it always dynamically detects the number of variable MTRRs available.
69 */
70void x86_setup_mtrrs_with_detect(void);
71/*
Aaron Durbinbb4e79a2013-03-26 14:09:47 -050072 * x86_setup_var_mtrrs() parameters:
73 * address_bits - number of physical address bits supported by cpu
Aaron Durbine63be892016-03-07 16:05:36 -060074 * above4gb - if set setup MTRRs for addresses above 4GiB else ignore
75 * memory ranges above 4GiB
Aaron Durbinbb4e79a2013-03-26 14:09:47 -050076 */
77void x86_setup_var_mtrrs(unsigned int address_bits, unsigned int above4gb);
78void enable_fixed_mtrr(void);
Maciej Pijankaea921852009-10-27 14:29:29 +000079void x86_setup_fixed_mtrrs(void);
Aaron Durbin57686f82013-03-20 15:50:59 -050080/* Set up fixed MTRRs but do not enable them. */
81void x86_setup_fixed_mtrrs_no_enable(void);
Kyösti Mälkki38a8fb02014-06-30 13:48:18 +030082void x86_mtrr_check(void);
Aaron Durbin2bebd7b2016-11-10 15:15:35 -060083
84/* Insert a temporary MTRR range for the duration of coreboot's runtime.
85 * This function needs to be called after the first MTRR solution is derived. */
86void mtrr_use_temp_range(uintptr_t begin, size_t size, int type);
Stefan Reinauer35b6bbb2010-03-28 21:26:54 +000087#endif
Eric Biedermanc84c1902004-10-14 20:13:01 +000088
Kyösti Mälkki88a67f02013-12-12 12:27:53 +020089#if !defined(__ASSEMBLER__) && defined(__PRE_RAM__) && !defined(__ROMCC__)
Lee Leahy0ca2a062017-03-06 18:01:04 -080090void set_var_mtrr(unsigned int reg, unsigned int base, unsigned int size,
91 unsigned int type);
Furquan Shaikh331ac1b2016-03-16 16:12:06 -070092int get_free_var_mtrr(void);
Kyösti Mälkki88a67f02013-12-12 12:27:53 +020093#endif
94
Rizwan Qureshi8453c4f2016-09-07 20:11:11 +053095#if !defined(__ASSEMBLER__) && !defined(__ROMCC__)
96
97/* fms: find most significant bit set, stolen from Linux Kernel Source. */
98static inline unsigned int fms(unsigned int x)
99{
100 int r;
101
102 __asm__("bsrl %1,%0\n\t"
103 "jnz 1f\n\t"
104 "movl $0,%0\n"
105 "1:" : "=r" (r) : "g" (x));
106 return r;
107}
108
109/* fls: find least significant bit set */
110static inline unsigned int fls(unsigned int x)
111{
112 int r;
113
114 __asm__("bsfl %1,%0\n\t"
115 "jnz 1f\n\t"
116 "movl $32,%0\n"
117 "1:" : "=r" (r) : "g" (x));
118 return r;
119}
120#endif
121
Kyösti Mälkki107f72e2014-01-06 11:06:26 +0200122/* Align up to next power of 2, suitable for ROMCC and assembler too.
123 * Range of result 256kB to 128MB is good enough here.
124 */
125#define _POW2_MASK(x) ((x>>1)|(x>>2)|(x>>3)|(x>>4)|(x>>5)| \
126 (x>>6)|(x>>7)|(x>>8)|((1<<18)-1))
127#define _ALIGN_UP_POW2(x) ((x + _POW2_MASK(x)) & ~_POW2_MASK(x))
128
Elyes HAOUAS918535a2016-07-28 21:25:21 +0200129/* At the end of romstage, low RAM 0..CACHE_TM_RAMTOP may be set
Kyösti Mälkki65cc5262016-06-19 20:38:41 +0300130 * as write-back cacheable to speed up ramstage decompression.
131 * Note MTRR boundaries, must be power of two.
132 */
133#define CACHE_TMP_RAMTOP (16<<20)
Stefan Reinauer8f2c6162010-04-06 21:50:21 +0000134
Patrick Georgi784544b2011-10-31 17:07:52 +0100135#if ((CONFIG_XIP_ROM_SIZE & (CONFIG_XIP_ROM_SIZE -1)) != 0)
Stefan Reinauer8f2c6162010-04-06 21:50:21 +0000136# error "CONFIG_XIP_ROM_SIZE is not a power of 2"
137#endif
Stefan Reinauer8f2c6162010-04-06 21:50:21 +0000138
Kyösti Mälkki107f72e2014-01-06 11:06:26 +0200139/* Select CACHE_ROM_SIZE to use with MTRR setup. For most cases this
140 * resolves to a suitable CONFIG_ROM_SIZE but some odd cases need to
141 * use CONFIG_CACHE_ROM_SIZE_OVERRIDE in the mainboard Kconfig.
142 */
143#if (CONFIG_CACHE_ROM_SIZE_OVERRIDE != 0)
144# define CACHE_ROM_SIZE CONFIG_CACHE_ROM_SIZE_OVERRIDE
145#else
146# if ((CONFIG_ROM_SIZE & (CONFIG_ROM_SIZE-1)) == 0)
147# define CACHE_ROM_SIZE CONFIG_ROM_SIZE
148# else
149# define CACHE_ROM_SIZE _ALIGN_UP_POW2(CONFIG_ROM_SIZE)
150# if (CACHE_ROM_SIZE < CONFIG_ROM_SIZE) || (CACHE_ROM_SIZE >= (2 * CONFIG_ROM_SIZE))
151# error "CACHE_ROM_SIZE is not optimal."
152# endif
153# endif
Kyösti Mälkki5458b9d2012-06-30 11:41:08 +0300154#endif
155
Kyösti Mälkki107f72e2014-01-06 11:06:26 +0200156#if ((CACHE_ROM_SIZE & (CACHE_ROM_SIZE-1)) != 0)
157# error "CACHE_ROM_SIZE is not a power of 2."
158#endif
159
160#define CACHE_ROM_BASE (((1<<20) - (CACHE_ROM_SIZE>>12))<<12)
161
Lee Leahyae738ac2016-07-24 08:03:37 -0700162#if (IS_ENABLED(CONFIG_SOC_SETS_MSRS) && !defined(__ASSEMBLER__) \
163 && !defined(__ROMCC__))
164#include <cpu/x86/msr.h>
165#include <arch/cpu.h>
166
167/*
168 * Set the MTRRs using the data on the stack from setup_stack_and_mtrrs.
169 * Return a new top_of_stack value which removes the setup_stack_and_mtrrs data.
170 */
171asmlinkage void *soc_set_mtrrs(void *top_of_stack);
172asmlinkage void soc_enable_mtrrs(void);
173#endif /* CONFIG_SOC_SETS_MSRS ... */
174
Eric Biedermanc84c1902004-10-14 20:13:01 +0000175#endif /* CPU_X86_MTRR_H */