This patch unifies the use of config options in v2 to all start with CONFIG_
[coreboot.git] / src / cpu / x86 / mtrr / earlymtrr.c
1 #ifndef EARLYMTRR_C
2 #define EARLYMTRR_C
3 #include <cpu/x86/cache.h>
4 #include <cpu/x86/mtrr.h>
5 #include <cpu/x86/msr.h>
6
7 /* Validate CONFIG_XIP_ROM_SIZE and CONFIG_XIP_ROM_BASE */
8 #if defined(CONFIG_XIP_ROM_SIZE) && !defined(CONFIG_XIP_ROM_BASE)
9 # error "CONFIG_XIP_ROM_SIZE without CONFIG_XIP_ROM_BASE"
10 #endif
11 #if defined(CONFIG_XIP_ROM_BASE) && !defined(CONFIG_XIP_ROM_SIZE)
12 # error "CONFIG_XIP_ROM_BASE without CONFIG_XIP_ROM_SIZE"
13 #endif
14 #if !defined(CONFIG_LB_MEM_TOPK)
15 # error "CONFIG_LB_MEM_TOPK not defined"
16 #endif
17
18 #if defined(CONFIG_XIP_ROM_SIZE) && ((CONFIG_XIP_ROM_SIZE & (CONFIG_XIP_ROM_SIZE -1)) != 0)
19 # error "CONFIG_XIP_ROM_SIZE is not a power of 2"
20 #endif
21 #if defined(CONFIG_XIP_ROM_SIZE) && ((CONFIG_XIP_ROM_BASE % CONFIG_XIP_ROM_SIZE) != 0)
22 # error "CONFIG_XIP_ROM_BASE is not a multiple of CONFIG_XIP_ROM_SIZE"
23 #endif
24
25 #if (CONFIG_LB_MEM_TOPK & (CONFIG_LB_MEM_TOPK -1)) != 0
26 # error "CONFIG_LB_MEM_TOPK must be a power of 2"
27 #endif
28
29 static void disable_var_mtrr(unsigned reg)
30 {
31         /* The invalid bit is kept in the mask so we simply
32          * clear the relevent mask register to disable a
33          * range.
34          */
35         msr_t zero;
36         zero.lo = zero.hi = 0;
37         wrmsr(MTRRphysMask_MSR(reg), zero);
38 }
39
40 static void set_var_mtrr(
41         unsigned reg, unsigned base, unsigned size, unsigned type)
42
43 {
44         /* Bit Bit 32-35 of MTRRphysMask should be set to 1 */
45         /* FIXME: It only support 4G less range */
46         msr_t basem, maskm;
47         basem.lo = base | type;
48         basem.hi = 0;
49         wrmsr(MTRRphysBase_MSR(reg), basem);
50         maskm.lo = ~(size - 1) | 0x800;
51         maskm.hi = (1<<(CONFIG_CPU_ADDR_BITS-32))-1;
52         wrmsr(MTRRphysMask_MSR(reg), maskm);
53 }
54
55 static void set_var_mtrr_x(
56         unsigned reg, uint32_t base_lo, uint32_t base_hi, uint32_t size_lo, uint32_t size_hi, unsigned type)
57
58 {
59         /* Bit Bit 32-35 of MTRRphysMask should be set to 1 */
60         msr_t basem, maskm;
61         basem.lo = (base_lo & 0xfffff000) | type;
62         basem.hi = base_hi & ((1<<(CONFIG_CPU_ADDR_BITS-32))-1);
63         wrmsr(MTRRphysBase_MSR(reg), basem);
64         maskm.hi = (1<<(CONFIG_CPU_ADDR_BITS-32))-1;
65         if(size_lo) {
66                 maskm.lo = ~(size_lo - 1) | 0x800;
67         } else {
68                 maskm.lo = 0x800;
69                 maskm.hi &= ~(size_hi - 1);
70         }
71         wrmsr(MTRRphysMask_MSR(reg), maskm);
72 }
73
74 static void cache_lbmem(int type)
75 {
76         /* Enable caching for 0 - 1MB using variable mtrr */
77         disable_cache();
78         set_var_mtrr(0, 0x00000000, CONFIG_LB_MEM_TOPK << 10, type);
79         enable_cache();
80 }
81
82 /* the fixed and variable MTTRs are power-up with random values,
83  * clear them to MTRR_TYPE_UNCACHEABLE for safty.
84  */
85 static void do_early_mtrr_init(const unsigned long *mtrr_msrs)
86 {
87         /* Precondition:
88          *   The cache is not enabled in cr0 nor in MTRRdefType_MSR
89          *   entry32.inc ensures the cache is not enabled in cr0
90          */
91         msr_t msr;
92         const unsigned long *msr_addr;
93
94         /* Inialize all of the relevant msrs to 0 */
95         msr.lo = 0;
96         msr.hi = 0;
97         unsigned long msr_nr;
98         for(msr_addr = mtrr_msrs; (msr_nr = *msr_addr); msr_addr++) {
99                 wrmsr(msr_nr, msr);
100         }
101
102 #if defined(CONFIG_XIP_ROM_SIZE)
103         /* enable write through caching so we can do execute in place
104          * on the flash rom.
105          */
106         set_var_mtrr(1, CONFIG_XIP_ROM_BASE, CONFIG_XIP_ROM_SIZE, MTRR_TYPE_WRBACK);
107 #endif
108
109         /* Set the default memory type and enable fixed and variable MTRRs 
110          */
111         /* Enable Variable MTRRs */
112         msr.hi = 0x00000000;
113         msr.lo = 0x00000800;
114         wrmsr(MTRRdefType_MSR, msr);
115         
116 }
117
118 static void early_mtrr_init(void)
119 {
120         static const unsigned long mtrr_msrs[] = {
121                 /* fixed mtrr */
122                 0x250, 0x258, 0x259,
123                 0x268, 0x269, 0x26A,
124                 0x26B, 0x26C, 0x26D,
125                 0x26E, 0x26F,
126                 /* var mtrr */
127                 0x200, 0x201, 0x202, 0x203,
128                 0x204, 0x205, 0x206, 0x207,
129                 0x208, 0x209, 0x20A, 0x20B,
130                 0x20C, 0x20D, 0x20E, 0x20F,
131                 /* NULL end of table */
132                 0
133         };
134         disable_cache();
135         do_early_mtrr_init(mtrr_msrs);
136         enable_cache();
137 }
138
139 static int early_mtrr_init_detected(void)
140 {
141         msr_t msr;
142         /* See if MTRR's are enabled.
143          * a #RESET disables them while an #INIT
144          * preserves their state.  This works
145          * on both Intel and AMD cpus, at least
146          * according to the documentation.
147          */
148         msr = rdmsr(MTRRdefType_MSR);
149         return msr.lo & 0x00000800;
150 }
151
152 #endif /* EARLYMTRR_C */