Get rid of AUTO_XIP_ROM_BASE
[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/amd/mtrr.h>
6 #include <cpu/x86/msr.h>
7
8 static void set_var_mtrr(
9         unsigned reg, unsigned base, unsigned size, unsigned type)
10
11 {
12         /* Bit Bit 32-35 of MTRRphysMask should be set to 1 */
13         /* FIXME: It only support 4G less range */
14         msr_t basem, maskm;
15         basem.lo = base | type;
16         basem.hi = 0;
17         wrmsr(MTRRphysBase_MSR(reg), basem);
18         maskm.lo = ~(size - 1) | MTRRphysMaskValid;
19         maskm.hi = (1 << (CONFIG_CPU_ADDR_BITS - 32)) - 1;
20         wrmsr(MTRRphysMask_MSR(reg), maskm);
21 }
22
23 #if !defined(CONFIG_CACHE_AS_RAM) || (CONFIG_CACHE_AS_RAM == 0)
24 static void cache_lbmem(int type)
25 {
26         /* Enable caching for 0 - 1MB using variable mtrr */
27         disable_cache();
28         set_var_mtrr(0, 0x00000000, CONFIG_RAMTOP, type);
29         enable_cache();
30 }
31
32 const int addr_det = 0;
33
34 /* the fixed and variable MTTRs are power-up with random values,
35  * clear them to MTRR_TYPE_UNCACHEABLE for safty.
36  */
37 static void do_early_mtrr_init(const unsigned long *mtrr_msrs)
38 {
39         /* Precondition:
40          *   The cache is not enabled in cr0 nor in MTRRdefType_MSR
41          *   entry32.inc ensures the cache is not enabled in cr0
42          */
43         msr_t msr;
44         const unsigned long *msr_addr;
45
46         /* Inialize all of the relevant msrs to 0 */
47         msr.lo = 0;
48         msr.hi = 0;
49         unsigned long msr_nr;
50         for(msr_addr = mtrr_msrs; (msr_nr = *msr_addr); msr_addr++) {
51                 wrmsr(msr_nr, msr);
52         }
53
54 #if defined(CONFIG_XIP_ROM_SIZE)
55         /* enable write through caching so we can do execute in place
56          * on the flash rom.
57          * Determine address by calculating the XIP_ROM_SIZE sized area with
58          * XIP_ROM_SIZE alignment that contains the global variable defined above;
59          */
60         unsigned long f = (unsigned long)&addr_det & ~(CONFIG_XIP_ROM_SIZE - 1);
61         set_var_mtrr(1, f, CONFIG_XIP_ROM_SIZE, MTRR_TYPE_WRBACK);
62 #endif
63
64         /* Set the default memory type and enable fixed and variable MTRRs
65          */
66         /* Enable Variable MTRRs */
67         msr.hi = 0x00000000;
68         msr.lo = 0x00000800;
69         wrmsr(MTRRdefType_MSR, msr);
70
71 }
72
73 static inline void early_mtrr_init(void)
74 {
75         static const unsigned long mtrr_msrs[] = {
76                 /* fixed mtrr */
77                 0x250, 0x258, 0x259,
78                 0x268, 0x269, 0x26A,
79                 0x26B, 0x26C, 0x26D,
80                 0x26E, 0x26F,
81                 /* var mtrr */
82                 0x200, 0x201, 0x202, 0x203,
83                 0x204, 0x205, 0x206, 0x207,
84                 0x208, 0x209, 0x20A, 0x20B,
85                 0x20C, 0x20D, 0x20E, 0x20F,
86                 /* NULL end of table */
87                 0
88         };
89         disable_cache();
90         do_early_mtrr_init(mtrr_msrs);
91         enable_cache();
92 }
93
94 static inline int early_mtrr_init_detected(void)
95 {
96         msr_t msr;
97         /* See if MTRR's are enabled.
98          * a #RESET disables them while an #INIT
99          * preserves their state.  This works
100          * on both Intel and AMD cpus, at least
101          * according to the documentation.
102          */
103         msr = rdmsr(MTRRdefType_MSR);
104         return msr.lo & MTRRdefTypeEn;
105 }
106 #endif
107
108 #endif /* EARLYMTRR_C */