- Better memory I/O space distinguishing in amd_mtrr.c
[coreboot.git] / src / cpu / amd / mtrr / amd_mtrr.c
1 #include <console/console.h>
2 #include <device/device.h>
3 #include <cpu/x86/mtrr.h>
4 #include <cpu/amd/mtrr.h>
5 #include <cpu/x86/cache.h>
6 #include <cpu/x86/msr.h>
7
8 static unsigned long resk(uint64_t value) 
9 {
10         unsigned long resultk;
11         if (value < (1ULL << 42)) {
12                 resultk = value >> 10;
13         }
14         else {
15                 resultk = 0xffffffff;
16         }
17         return resultk;
18 }
19
20 #if 1
21 static unsigned fixed_mtrr_index(unsigned long addrk)
22 {
23         unsigned index;
24         index = (addrk - 0) >> 6;
25         if (index >= 8) {
26                 index = ((addrk - 8*64) >> 4) + 8;
27         }
28         if (index >= 24) {
29                 index = ((addrk - (8*64 + 16*16)) >> 2) + 24;
30         }
31         if (index > NUM_FIXED_RANGES) {
32                 index = NUM_FIXED_RANGES;
33         }
34         return index;
35 }
36
37
38 static unsigned int mtrr_msr[] = {
39         MTRRfix64K_00000_MSR, MTRRfix16K_80000_MSR, MTRRfix16K_A0000_MSR,
40         MTRRfix4K_C0000_MSR, MTRRfix4K_C8000_MSR, MTRRfix4K_D0000_MSR, MTRRfix4K_D8000_MSR,
41         MTRRfix4K_E0000_MSR, MTRRfix4K_E8000_MSR, MTRRfix4K_F0000_MSR, MTRRfix4K_F8000_MSR,
42 };
43
44 static void set_fixed_mtrrs(unsigned int first, unsigned int last, unsigned char type)
45 {
46         unsigned int i;
47         unsigned int fixed_msr = NUM_FIXED_RANGES >> 3;
48         msr_t msr;
49         msr.lo = msr.hi = 0; /* Shut up gcc */
50         for (i = first; i < last; i++) {
51                 /* When I switch to a new msr read it in */
52                 if (fixed_msr != i >> 3) {
53                         /* But first write out the old msr */
54                         if (fixed_msr < (NUM_FIXED_RANGES >> 3)) {
55                                 disable_cache();
56                                 wrmsr(mtrr_msr[fixed_msr], msr);
57                                 enable_cache();
58                         }
59                         fixed_msr = i>>3;
60                         msr = rdmsr(mtrr_msr[fixed_msr]);
61                 }
62                 if ((i & 7) < 4) {
63                         msr.lo &= ~(0xff << ((i&3)*8));
64                         msr.lo |= type << ((i&3)*8);
65                 } else {
66                         msr.hi &= ~(0xff << ((i&3)*8));
67                         msr.hi |= type << ((i&3)*8);
68                 }
69         }
70         /* Write out the final msr */
71         if (fixed_msr < (NUM_FIXED_RANGES >> 3)) {
72                 disable_cache();
73                 wrmsr(mtrr_msr[fixed_msr], msr);
74                 enable_cache();
75         }
76 }
77
78 #endif
79
80 void amd_setup_mtrrs(void)
81 {
82         unsigned long mmio_basek, tomk;
83         unsigned long i;
84         device_t dev;
85         msr_t msr;
86
87         /* Enable the access to AMD RdDram and WrDram extension bits */
88         msr = rdmsr(SYSCFG_MSR);
89         msr.lo |= SYSCFG_MSR_MtrrFixDramModEn;
90         wrmsr(SYSCFG_MSR, msr);
91
92         printk_debug("\n");
93         /* Initialized the fixed_mtrrs to uncached */
94         printk_debug("Setting fixed MTRRs(%d-%d) type: UC\n", 
95                 0, NUM_FIXED_RANGES);
96         set_fixed_mtrrs(0, NUM_FIXED_RANGES, MTRR_TYPE_UNCACHEABLE);
97
98         /* Except for the PCI MMIO hole just before 4GB there are no
99          * significant holes in the address space, so just account
100          * for those two and move on.
101          */
102         mmio_basek = tomk = 0;
103         for(dev = all_devices; dev; dev = dev->next) {
104                 struct resource *res, *last;
105                 last = &dev->resource[dev->resources];
106                 for(res = &dev->resource[0]; res < last; res++) {
107                         unsigned long topk;
108                         unsigned long start_mtrr, last_mtrr;
109                         if (!(res->flags & IORESOURCE_MEM) ||
110                                 (!(res->flags & IORESOURCE_CACHEABLE))) {
111                                 continue;
112                         }
113                         topk = resk(res->base + res->size);
114                         if (tomk < topk) {
115                                 tomk = topk;
116                         }
117                         if ((topk < 4*1024*1024) && (mmio_basek < topk)) {
118                                 mmio_basek = topk;
119                         }
120
121                         start_mtrr = fixed_mtrr_index(resk(res->base));
122                         last_mtrr  = fixed_mtrr_index(resk(res->base + res->size));
123                         if (start_mtrr >= NUM_FIXED_RANGES) {
124                                 continue;
125                         }
126                         printk_debug("Setting fixed MTRRs(%d-%d) Type: WB\n",
127                                 start_mtrr, last_mtrr);
128                         set_fixed_mtrrs(start_mtrr, last_mtrr, MTRR_TYPE_WRBACK | MTRR_READ_MEM | MTRR_WRITE_MEM);
129                 }
130         }
131         printk_debug("DONE fixed MTRRs\n");
132         if (mmio_basek > tomk) {
133                 mmio_basek = tomk;
134         }
135         /* Round mmio_basek down to the nearst size that will fit in TOP_MEM */
136         mmio_basek = mmio_basek & ~TOP_MEM_MASK_KB;
137         /* Round tomk up to the next greater size that will fit in TOP_MEM */
138         tomk = (tomk + TOP_MEM_MASK_KB) & ~TOP_MEM_MASK_KB;
139
140         disable_cache();
141
142         /* Setup TOP_MEM */
143         msr.hi = mmio_basek >> 22;
144         msr.lo = mmio_basek << 10;
145         wrmsr(TOP_MEM, msr);
146
147         /* Setup TOP_MEM2 */
148         msr.hi = tomk >> 22;
149         msr.lo = tomk << 10;
150         wrmsr(TOP_MEM2, msr);
151
152         /* zero the IORR's before we enable to prevent
153          * undefined side effects.
154          */
155         msr.lo = msr.hi = 0;
156         for(i = IORR_FIRST; i <= IORR_LAST; i++) {
157                 wrmsr(i, msr);
158         }
159
160         /* Enable Variable Mtrrs 
161          * Enable the RdMem and WrMem bits in the fixed mtrrs.
162          * Disable access to the RdMem and WrMem in the fixed mtrr.
163          */
164         msr = rdmsr(SYSCFG_MSR);
165         msr.lo |= SYSCFG_MSR_MtrrVarDramEn | SYSCFG_MSR_MtrrFixDramEn | SYSCFG_MSR_TOM2En;
166         msr.lo &= ~SYSCFG_MSR_MtrrFixDramModEn;
167         wrmsr(SYSCFG_MSR, msr);
168
169         enable_cache();
170
171         /* Now that I have mapped what is memory and what is not
172          * Setup the mtrrs so we can cache the memory.
173          */
174         x86_setup_mtrrs();
175 }