Since some people disapprove of white space cleanups mixed in regular commits
[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 static unsigned fixed_mtrr_index(unsigned long addrk)
21 {
22         unsigned index;
23         index = (addrk - 0) >> 6;
24         if (index >= 8) {
25                 index = ((addrk - 8*64) >> 4) + 8;
26         }
27         if (index >= 24) {
28                 index = ((addrk - (8*64 + 16*16)) >> 2) + 24;
29         }
30         if (index > NUM_FIXED_RANGES) {
31                 index = NUM_FIXED_RANGES;
32         }
33         return index;
34 }
35
36 static unsigned int mtrr_msr[] = {
37         MTRRfix64K_00000_MSR, MTRRfix16K_80000_MSR, MTRRfix16K_A0000_MSR,
38         MTRRfix4K_C0000_MSR, MTRRfix4K_C8000_MSR, MTRRfix4K_D0000_MSR, MTRRfix4K_D8000_MSR,
39         MTRRfix4K_E0000_MSR, MTRRfix4K_E8000_MSR, MTRRfix4K_F0000_MSR, MTRRfix4K_F8000_MSR,
40 };
41
42 static void set_fixed_mtrrs(unsigned int first, unsigned int last, unsigned char type)
43 {
44         unsigned int i;
45         unsigned int fixed_msr = NUM_FIXED_RANGES >> 3;
46         msr_t msr;
47         msr.lo = msr.hi = 0; /* Shut up gcc */
48         for (i = first; i < last; i++) {
49                 /* When I switch to a new msr read it in */
50                 if (fixed_msr != i >> 3) {
51                         /* But first write out the old msr */
52                         if (fixed_msr < (NUM_FIXED_RANGES >> 3)) {
53                                 disable_cache();
54                                 wrmsr(mtrr_msr[fixed_msr], msr);
55                                 enable_cache();
56                         }
57                         fixed_msr = i>>3;
58                         msr = rdmsr(mtrr_msr[fixed_msr]);
59                 }
60                 if ((i & 7) < 4) {
61                         msr.lo &= ~(0xff << ((i&3)*8));
62                         msr.lo |= type << ((i&3)*8);
63                 } else {
64                         msr.hi &= ~(0xff << ((i&3)*8));
65                         msr.hi |= type << ((i&3)*8);
66                 }
67         }
68         /* Write out the final msr */
69         if (fixed_msr < (NUM_FIXED_RANGES >> 3)) {
70                 disable_cache();
71                 wrmsr(mtrr_msr[fixed_msr], msr);
72                 enable_cache();
73         }
74 }
75
76 struct mem_state {
77         unsigned long mmio_basek, tomk;
78 };
79 static void set_fixed_mtrr_resource(void *gp, struct device *dev, struct resource *res)
80 {
81         struct mem_state *state = gp;
82         unsigned long topk;
83         unsigned int start_mtrr;
84         unsigned int last_mtrr;
85
86         topk = resk(res->base + res->size);
87         if (state->tomk < topk) {
88                 state->tomk = topk;
89         }
90         if ((topk < 4*1024*1024) && (state->mmio_basek < topk)) {
91                 state->mmio_basek = topk;
92         }
93         start_mtrr = fixed_mtrr_index(resk(res->base));
94         last_mtrr  = fixed_mtrr_index(resk((res->base + res->size)));
95         if (start_mtrr >= NUM_FIXED_RANGES) {
96                 return;
97         }
98         printk(BIOS_DEBUG, "Setting fixed MTRRs(%d-%d) Type: WB, RdMEM, WrMEM\n",
99                 start_mtrr, last_mtrr);
100         set_fixed_mtrrs(start_mtrr, last_mtrr, MTRR_TYPE_WRBACK | MTRR_READ_MEM | MTRR_WRITE_MEM);
101
102 }
103
104 void amd_setup_mtrrs(void)
105 {
106         unsigned long address_bits;
107         struct mem_state state;
108         unsigned long i;
109         msr_t msr;
110
111
112         /* Enable the access to AMD RdDram and WrDram extension bits */
113         disable_cache();
114         msr = rdmsr(SYSCFG_MSR);
115         msr.lo |= SYSCFG_MSR_MtrrFixDramModEn;
116         wrmsr(SYSCFG_MSR, msr);
117         enable_cache();
118
119         printk(BIOS_DEBUG, "\n");
120         /* Initialized the fixed_mtrrs to uncached */
121         printk(BIOS_DEBUG, "Setting fixed MTRRs(%d-%d) type: UC\n",
122                 0, NUM_FIXED_RANGES);
123         set_fixed_mtrrs(0, NUM_FIXED_RANGES, MTRR_TYPE_UNCACHEABLE);
124
125         /* Except for the PCI MMIO hole just before 4GB there are no
126          * significant holes in the address space, so just account
127          * for those two and move on.
128          */
129         state.mmio_basek = state.tomk = 0;
130         search_global_resources(
131                 IORESOURCE_MEM | IORESOURCE_CACHEABLE, IORESOURCE_MEM | IORESOURCE_CACHEABLE,
132                 set_fixed_mtrr_resource, &state);
133         printk(BIOS_DEBUG, "DONE fixed MTRRs\n");
134
135         if (state.mmio_basek > state.tomk) {
136                 state.mmio_basek = state.tomk;
137         }
138         /* Round state.mmio_basek down to the nearst size that will fit in TOP_MEM */
139         state.mmio_basek = state.mmio_basek & ~TOP_MEM_MASK_KB;
140         /* Round state.tomk up to the next greater size that will fit in TOP_MEM */
141         state.tomk = (state.tomk + TOP_MEM_MASK_KB) & ~TOP_MEM_MASK_KB;
142
143         disable_cache();
144
145         /* Setup TOP_MEM */
146         msr.hi = state.mmio_basek >> 22;
147         msr.lo = state.mmio_basek << 10;
148         wrmsr(TOP_MEM, msr);
149
150         if(state.tomk > (4*1024*1024)) {
151                 /* Setup TOP_MEM2 */
152                 msr.hi = state.tomk >> 22;
153                 msr.lo = state.tomk << 10;
154                 wrmsr(TOP_MEM2, msr);
155         }
156
157         /* zero the IORR's before we enable to prevent
158          * undefined side effects.
159          */
160         msr.lo = msr.hi = 0;
161         for(i = IORR_FIRST; i <= IORR_LAST; i++) {
162                 wrmsr(i, msr);
163         }
164
165         /* Enable Variable Mtrrs
166          * Enable the RdMem and WrMem bits in the fixed mtrrs.
167          * Disable access to the RdMem and WrMem in the fixed mtrr.
168          */
169         msr = rdmsr(SYSCFG_MSR);
170         msr.lo |= SYSCFG_MSR_MtrrVarDramEn | SYSCFG_MSR_MtrrFixDramEn | SYSCFG_MSR_TOM2En;
171         msr.lo &= ~SYSCFG_MSR_MtrrFixDramModEn;
172         wrmsr(SYSCFG_MSR, msr);
173
174         enable_fixed_mtrr();
175
176         enable_cache();
177
178         /* FIXME we should probably query the cpu for this
179          * but so far this is all any recent AMD cpu has supported.
180          */
181         address_bits = CONFIG_CPU_ADDR_BITS; //K8 could be 40, and GH could be 48
182
183         /* Now that I have mapped what is memory and what is not
184          * Setup the mtrrs so we can cache the memory.
185          */
186         x86_setup_var_mtrrs(address_bits);
187 }