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