Commit | Line | Data |
---|---|---|
a1a499a3 JSR |
1 | /* |
2 | * This only handles 32bit MTRR on 32bit hosts. This is strictly wrong | |
0d2eb44f | 3 | * because MTRRs can span up to 40 bits (36bits on most modern x86) |
a1a499a3 JSR |
4 | */ |
5 | #define DEBUG | |
6 | ||
7 | #include <linux/module.h> | |
1da177e4 | 8 | #include <linux/init.h> |
a1a499a3 | 9 | #include <linux/io.h> |
1da177e4 | 10 | #include <linux/mm.h> |
a1a499a3 | 11 | |
7ebad705 | 12 | #include <asm/processor-flags.h> |
a1a499a3 | 13 | #include <asm/cpufeature.h> |
1da177e4 | 14 | #include <asm/tlbflush.h> |
a1a499a3 JSR |
15 | #include <asm/mtrr.h> |
16 | #include <asm/msr.h> | |
2e5d9c85 | 17 | #include <asm/pat.h> |
a1a499a3 | 18 | |
1da177e4 LT |
19 | #include "mtrr.h" |
20 | ||
de938c51 | 21 | struct fixed_range_block { |
a1a499a3 JSR |
22 | int base_msr; /* start address of an MTRR block */ |
23 | int ranges; /* number of MTRRs in this block */ | |
de938c51 BK |
24 | }; |
25 | ||
26 | static struct fixed_range_block fixed_range_blocks[] = { | |
a1a499a3 JSR |
27 | { MSR_MTRRfix64K_00000, 1 }, /* one 64k MTRR */ |
28 | { MSR_MTRRfix16K_80000, 2 }, /* two 16k MTRRs */ | |
29 | { MSR_MTRRfix4K_C0000, 8 }, /* eight 4k MTRRs */ | |
de938c51 BK |
30 | {} |
31 | }; | |
32 | ||
1da177e4 | 33 | static unsigned long smp_changes_mask; |
2e5d9c85 | 34 | static int mtrr_state_set; |
95ffa243 | 35 | u64 mtrr_tom2; |
1da177e4 | 36 | |
a1a499a3 | 37 | struct mtrr_state_type mtrr_state; |
932d27a7 SY |
38 | EXPORT_SYMBOL_GPL(mtrr_state); |
39 | ||
a1a499a3 | 40 | /* |
3ff42da5 AH |
41 | * BIOS is expected to clear MtrrFixDramModEn bit, see for example |
42 | * "BIOS and Kernel Developer's Guide for the AMD Athlon 64 and AMD | |
43 | * Opteron Processors" (26094 Rev. 3.30 February 2006), section | |
44 | * "13.2.1.2 SYSCFG Register": "The MtrrFixDramModEn bit should be set | |
45 | * to 1 during BIOS initalization of the fixed MTRRs, then cleared to | |
46 | * 0 for operation." | |
47 | */ | |
48 | static inline void k8_check_syscfg_dram_mod_en(void) | |
49 | { | |
50 | u32 lo, hi; | |
51 | ||
52 | if (!((boot_cpu_data.x86_vendor == X86_VENDOR_AMD) && | |
53 | (boot_cpu_data.x86 >= 0x0f))) | |
54 | return; | |
55 | ||
56 | rdmsr(MSR_K8_SYSCFG, lo, hi); | |
57 | if (lo & K8_MTRRFIXRANGE_DRAM_MODIFY) { | |
58 | printk(KERN_ERR FW_WARN "MTRR: CPU %u: SYSCFG[MtrrFixDramModEn]" | |
59 | " not cleared by BIOS, clearing this bit\n", | |
60 | smp_processor_id()); | |
61 | lo &= ~K8_MTRRFIXRANGE_DRAM_MODIFY; | |
62 | mtrr_wrmsr(MSR_K8_SYSCFG, lo, hi); | |
63 | } | |
64 | } | |
65 | ||
351e5a70 VP |
66 | /* Get the size of contiguous MTRR range */ |
67 | static u64 get_mtrr_size(u64 mask) | |
68 | { | |
69 | u64 size; | |
70 | ||
71 | mask >>= PAGE_SHIFT; | |
72 | mask |= size_or_mask; | |
73 | size = -mask; | |
74 | size <<= PAGE_SHIFT; | |
75 | return size; | |
76 | } | |
77 | ||
a7f07cfb VP |
78 | /* |
79 | * Check and return the effective type for MTRR-MTRR type overlap. | |
80 | * Returns 1 if the effective type is UNCACHEABLE, else returns 0 | |
81 | */ | |
82 | static int check_type_overlap(u8 *prev, u8 *curr) | |
83 | { | |
84 | if (*prev == MTRR_TYPE_UNCACHABLE || *curr == MTRR_TYPE_UNCACHABLE) { | |
85 | *prev = MTRR_TYPE_UNCACHABLE; | |
86 | *curr = MTRR_TYPE_UNCACHABLE; | |
87 | return 1; | |
88 | } | |
89 | ||
90 | if ((*prev == MTRR_TYPE_WRBACK && *curr == MTRR_TYPE_WRTHROUGH) || | |
91 | (*prev == MTRR_TYPE_WRTHROUGH && *curr == MTRR_TYPE_WRBACK)) { | |
92 | *prev = MTRR_TYPE_WRTHROUGH; | |
93 | *curr = MTRR_TYPE_WRTHROUGH; | |
94 | } | |
95 | ||
96 | if (*prev != *curr) { | |
97 | *prev = MTRR_TYPE_UNCACHABLE; | |
98 | *curr = MTRR_TYPE_UNCACHABLE; | |
99 | return 1; | |
100 | } | |
101 | ||
102 | return 0; | |
103 | } | |
104 | ||
2e5d9c85 | 105 | /* |
351e5a70 VP |
106 | * Error/Semi-error returns: |
107 | * 0xFF - when MTRR is not enabled | |
108 | * *repeat == 1 implies [start:end] spanned across MTRR range and type returned | |
109 | * corresponds only to [start:*partial_end]. | |
110 | * Caller has to lookup again for [*partial_end:end]. | |
2e5d9c85 | 111 | */ |
351e5a70 | 112 | static u8 __mtrr_type_lookup(u64 start, u64 end, u64 *partial_end, int *repeat) |
2e5d9c85 | 113 | { |
114 | int i; | |
115 | u64 base, mask; | |
116 | u8 prev_match, curr_match; | |
117 | ||
351e5a70 | 118 | *repeat = 0; |
2e5d9c85 | 119 | if (!mtrr_state_set) |
120 | return 0xFF; | |
121 | ||
122 | if (!mtrr_state.enabled) | |
123 | return 0xFF; | |
124 | ||
125 | /* Make end inclusive end, instead of exclusive */ | |
126 | end--; | |
127 | ||
128 | /* Look in fixed ranges. Just return the type as per start */ | |
129 | if (mtrr_state.have_fixed && (start < 0x100000)) { | |
130 | int idx; | |
131 | ||
132 | if (start < 0x80000) { | |
133 | idx = 0; | |
134 | idx += (start >> 16); | |
135 | return mtrr_state.fixed_ranges[idx]; | |
136 | } else if (start < 0xC0000) { | |
137 | idx = 1 * 8; | |
138 | idx += ((start - 0x80000) >> 14); | |
139 | return mtrr_state.fixed_ranges[idx]; | |
140 | } else if (start < 0x1000000) { | |
141 | idx = 3 * 8; | |
142 | idx += ((start - 0xC0000) >> 12); | |
143 | return mtrr_state.fixed_ranges[idx]; | |
144 | } | |
145 | } | |
146 | ||
147 | /* | |
148 | * Look in variable ranges | |
149 | * Look of multiple ranges matching this address and pick type | |
150 | * as per MTRR precedence | |
151 | */ | |
a1a499a3 | 152 | if (!(mtrr_state.enabled & 2)) |
2e5d9c85 | 153 | return mtrr_state.def_type; |
2e5d9c85 | 154 | |
155 | prev_match = 0xFF; | |
156 | for (i = 0; i < num_var_ranges; ++i) { | |
157 | unsigned short start_state, end_state; | |
158 | ||
159 | if (!(mtrr_state.var_ranges[i].mask_lo & (1 << 11))) | |
160 | continue; | |
161 | ||
162 | base = (((u64)mtrr_state.var_ranges[i].base_hi) << 32) + | |
163 | (mtrr_state.var_ranges[i].base_lo & PAGE_MASK); | |
164 | mask = (((u64)mtrr_state.var_ranges[i].mask_hi) << 32) + | |
165 | (mtrr_state.var_ranges[i].mask_lo & PAGE_MASK); | |
166 | ||
167 | start_state = ((start & mask) == (base & mask)); | |
168 | end_state = ((end & mask) == (base & mask)); | |
351e5a70 VP |
169 | |
170 | if (start_state != end_state) { | |
171 | /* | |
172 | * We have start:end spanning across an MTRR. | |
173 | * We split the region into | |
174 | * either | |
175 | * (start:mtrr_end) (mtrr_end:end) | |
176 | * or | |
177 | * (start:mtrr_start) (mtrr_start:end) | |
178 | * depending on kind of overlap. | |
179 | * Return the type for first region and a pointer to | |
180 | * the start of second region so that caller will | |
181 | * lookup again on the second region. | |
182 | * Note: This way we handle multiple overlaps as well. | |
183 | */ | |
184 | if (start_state) | |
185 | *partial_end = base + get_mtrr_size(mask); | |
186 | else | |
187 | *partial_end = base; | |
188 | ||
189 | if (unlikely(*partial_end <= start)) { | |
190 | WARN_ON(1); | |
191 | *partial_end = start + PAGE_SIZE; | |
192 | } | |
193 | ||
194 | end = *partial_end - 1; /* end is inclusive */ | |
195 | *repeat = 1; | |
196 | } | |
2e5d9c85 | 197 | |
a1a499a3 | 198 | if ((start & mask) != (base & mask)) |
2e5d9c85 | 199 | continue; |
2e5d9c85 | 200 | |
201 | curr_match = mtrr_state.var_ranges[i].base_lo & 0xff; | |
202 | if (prev_match == 0xFF) { | |
203 | prev_match = curr_match; | |
204 | continue; | |
205 | } | |
206 | ||
a7f07cfb VP |
207 | if (check_type_overlap(&prev_match, &curr_match)) |
208 | return curr_match; | |
2e5d9c85 | 209 | } |
210 | ||
95ffa243 YL |
211 | if (mtrr_tom2) { |
212 | if (start >= (1ULL<<32) && (end < mtrr_tom2)) | |
35605a10 YL |
213 | return MTRR_TYPE_WRBACK; |
214 | } | |
215 | ||
2e5d9c85 | 216 | if (prev_match != 0xFF) |
217 | return prev_match; | |
218 | ||
219 | return mtrr_state.def_type; | |
220 | } | |
221 | ||
351e5a70 VP |
222 | /* |
223 | * Returns the effective MTRR type for the region | |
224 | * Error return: | |
225 | * 0xFF - when MTRR is not enabled | |
226 | */ | |
227 | u8 mtrr_type_lookup(u64 start, u64 end) | |
228 | { | |
229 | u8 type, prev_type; | |
230 | int repeat; | |
231 | u64 partial_end; | |
232 | ||
233 | type = __mtrr_type_lookup(start, end, &partial_end, &repeat); | |
234 | ||
235 | /* | |
236 | * Common path is with repeat = 0. | |
237 | * However, we can have cases where [start:end] spans across some | |
238 | * MTRR range. Do repeated lookups for that case here. | |
239 | */ | |
240 | while (repeat) { | |
241 | prev_type = type; | |
242 | start = partial_end; | |
243 | type = __mtrr_type_lookup(start, end, &partial_end, &repeat); | |
244 | ||
245 | if (check_type_overlap(&prev_type, &type)) | |
246 | return type; | |
247 | } | |
248 | ||
249 | return type; | |
250 | } | |
251 | ||
a1a499a3 | 252 | /* Get the MSR pair relating to a var range */ |
bf8c4817 | 253 | static void |
1da177e4 LT |
254 | get_mtrr_var_range(unsigned int index, struct mtrr_var_range *vr) |
255 | { | |
256 | rdmsr(MTRRphysBase_MSR(index), vr->base_lo, vr->base_hi); | |
257 | rdmsr(MTRRphysMask_MSR(index), vr->mask_lo, vr->mask_hi); | |
258 | } | |
259 | ||
a1a499a3 | 260 | /* Fill the MSR pair relating to a var range */ |
95ffa243 YL |
261 | void fill_mtrr_var_range(unsigned int index, |
262 | u32 base_lo, u32 base_hi, u32 mask_lo, u32 mask_hi) | |
263 | { | |
264 | struct mtrr_var_range *vr; | |
265 | ||
266 | vr = mtrr_state.var_ranges; | |
267 | ||
268 | vr[index].base_lo = base_lo; | |
269 | vr[index].base_hi = base_hi; | |
270 | vr[index].mask_lo = mask_lo; | |
271 | vr[index].mask_hi = mask_hi; | |
272 | } | |
273 | ||
a1a499a3 | 274 | static void get_fixed_ranges(mtrr_type *frs) |
1da177e4 | 275 | { |
a1a499a3 | 276 | unsigned int *p = (unsigned int *)frs; |
1da177e4 LT |
277 | int i; |
278 | ||
3ff42da5 AH |
279 | k8_check_syscfg_dram_mod_en(); |
280 | ||
a036c7a3 | 281 | rdmsr(MSR_MTRRfix64K_00000, p[0], p[1]); |
1da177e4 LT |
282 | |
283 | for (i = 0; i < 2; i++) | |
7d9d55e4 | 284 | rdmsr(MSR_MTRRfix16K_80000 + i, p[2 + i * 2], p[3 + i * 2]); |
1da177e4 | 285 | for (i = 0; i < 8; i++) |
ba5673ff | 286 | rdmsr(MSR_MTRRfix4K_C0000 + i, p[6 + i * 2], p[7 + i * 2]); |
1da177e4 LT |
287 | } |
288 | ||
2b3b4835 BK |
289 | void mtrr_save_fixed_ranges(void *info) |
290 | { | |
84288ad8 AM |
291 | if (cpu_has_mtrr) |
292 | get_fixed_ranges(mtrr_state.fixed_ranges); | |
2b3b4835 BK |
293 | } |
294 | ||
d4c90e37 YL |
295 | static unsigned __initdata last_fixed_start; |
296 | static unsigned __initdata last_fixed_end; | |
297 | static mtrr_type __initdata last_fixed_type; | |
298 | ||
299 | static void __init print_fixed_last(void) | |
300 | { | |
301 | if (!last_fixed_end) | |
302 | return; | |
303 | ||
a1a499a3 JSR |
304 | pr_debug(" %05X-%05X %s\n", last_fixed_start, |
305 | last_fixed_end - 1, mtrr_attrib_to_str(last_fixed_type)); | |
d4c90e37 YL |
306 | |
307 | last_fixed_end = 0; | |
308 | } | |
309 | ||
310 | static void __init update_fixed_last(unsigned base, unsigned end, | |
a1a499a3 | 311 | mtrr_type type) |
d4c90e37 YL |
312 | { |
313 | last_fixed_start = base; | |
314 | last_fixed_end = end; | |
315 | last_fixed_type = type; | |
316 | } | |
317 | ||
a1a499a3 JSR |
318 | static void __init |
319 | print_fixed(unsigned base, unsigned step, const mtrr_type *types) | |
365bff80 JB |
320 | { |
321 | unsigned i; | |
322 | ||
d4c90e37 YL |
323 | for (i = 0; i < 8; ++i, ++types, base += step) { |
324 | if (last_fixed_end == 0) { | |
325 | update_fixed_last(base, base + step, *types); | |
326 | continue; | |
327 | } | |
328 | if (last_fixed_end == base && last_fixed_type == *types) { | |
329 | last_fixed_end = base + step; | |
330 | continue; | |
331 | } | |
332 | /* new segments: gap or different type */ | |
333 | print_fixed_last(); | |
334 | update_fixed_last(base, base + step, *types); | |
335 | } | |
365bff80 JB |
336 | } |
337 | ||
2e5d9c85 | 338 | static void prepare_set(void); |
339 | static void post_set(void); | |
340 | ||
8ad97905 YL |
341 | static void __init print_mtrr_state(void) |
342 | { | |
343 | unsigned int i; | |
344 | int high_width; | |
345 | ||
a1a499a3 JSR |
346 | pr_debug("MTRR default type: %s\n", |
347 | mtrr_attrib_to_str(mtrr_state.def_type)); | |
8ad97905 | 348 | if (mtrr_state.have_fixed) { |
a1a499a3 JSR |
349 | pr_debug("MTRR fixed ranges %sabled:\n", |
350 | mtrr_state.enabled & 1 ? "en" : "dis"); | |
8ad97905 YL |
351 | print_fixed(0x00000, 0x10000, mtrr_state.fixed_ranges + 0); |
352 | for (i = 0; i < 2; ++i) | |
a1a499a3 JSR |
353 | print_fixed(0x80000 + i * 0x20000, 0x04000, |
354 | mtrr_state.fixed_ranges + (i + 1) * 8); | |
8ad97905 | 355 | for (i = 0; i < 8; ++i) |
a1a499a3 JSR |
356 | print_fixed(0xC0000 + i * 0x08000, 0x01000, |
357 | mtrr_state.fixed_ranges + (i + 3) * 8); | |
d4c90e37 YL |
358 | |
359 | /* tail */ | |
360 | print_fixed_last(); | |
8ad97905 | 361 | } |
a1a499a3 JSR |
362 | pr_debug("MTRR variable ranges %sabled:\n", |
363 | mtrr_state.enabled & 2 ? "en" : "dis"); | |
a7101d15 | 364 | high_width = (__ffs64(size_or_mask) - (32 - PAGE_SHIFT) + 3) / 4; |
a1a499a3 | 365 | |
8ad97905 YL |
366 | for (i = 0; i < num_var_ranges; ++i) { |
367 | if (mtrr_state.var_ranges[i].mask_lo & (1 << 11)) | |
a1a499a3 JSR |
368 | pr_debug(" %u base %0*X%05X000 mask %0*X%05X000 %s\n", |
369 | i, | |
370 | high_width, | |
371 | mtrr_state.var_ranges[i].base_hi, | |
372 | mtrr_state.var_ranges[i].base_lo >> 12, | |
373 | high_width, | |
374 | mtrr_state.var_ranges[i].mask_hi, | |
375 | mtrr_state.var_ranges[i].mask_lo >> 12, | |
376 | mtrr_attrib_to_str(mtrr_state.var_ranges[i].base_lo & 0xff)); | |
8ad97905 | 377 | else |
a1a499a3 | 378 | pr_debug(" %u disabled\n", i); |
8ad97905 | 379 | } |
a1a499a3 JSR |
380 | if (mtrr_tom2) |
381 | pr_debug("TOM2: %016llx aka %lldM\n", mtrr_tom2, mtrr_tom2>>20); | |
8ad97905 YL |
382 | } |
383 | ||
a1a499a3 | 384 | /* Grab all of the MTRR state for this CPU into *state */ |
9ef231a4 | 385 | void __init get_mtrr_state(void) |
1da177e4 | 386 | { |
1da177e4 | 387 | struct mtrr_var_range *vrs; |
2e5d9c85 | 388 | unsigned long flags; |
a1a499a3 JSR |
389 | unsigned lo, dummy; |
390 | unsigned int i; | |
1da177e4 | 391 | |
1da177e4 LT |
392 | vrs = mtrr_state.var_ranges; |
393 | ||
d9bcc01d | 394 | rdmsr(MSR_MTRRcap, lo, dummy); |
365bff80 JB |
395 | mtrr_state.have_fixed = (lo >> 8) & 1; |
396 | ||
1da177e4 LT |
397 | for (i = 0; i < num_var_ranges; i++) |
398 | get_mtrr_var_range(i, &vrs[i]); | |
365bff80 JB |
399 | if (mtrr_state.have_fixed) |
400 | get_fixed_ranges(mtrr_state.fixed_ranges); | |
1da177e4 | 401 | |
52650257 | 402 | rdmsr(MSR_MTRRdefType, lo, dummy); |
1da177e4 LT |
403 | mtrr_state.def_type = (lo & 0xff); |
404 | mtrr_state.enabled = (lo & 0xc00) >> 10; | |
365bff80 | 405 | |
35605a10 | 406 | if (amd_special_default_mtrr()) { |
0da72a4a | 407 | unsigned low, high; |
a1a499a3 | 408 | |
35605a10 | 409 | /* TOP_MEM2 */ |
0da72a4a | 410 | rdmsr(MSR_K8_TOP_MEM2, low, high); |
95ffa243 YL |
411 | mtrr_tom2 = high; |
412 | mtrr_tom2 <<= 32; | |
413 | mtrr_tom2 |= low; | |
8004dd96 | 414 | mtrr_tom2 &= 0xffffff800000ULL; |
35605a10 | 415 | } |
8ad97905 YL |
416 | |
417 | print_mtrr_state(); | |
418 | ||
2e5d9c85 | 419 | mtrr_state_set = 1; |
420 | ||
421 | /* PAT setup for BP. We need to go through sync steps here */ | |
422 | local_irq_save(flags); | |
423 | prepare_set(); | |
424 | ||
425 | pat_init(); | |
426 | ||
427 | post_set(); | |
428 | local_irq_restore(flags); | |
1da177e4 LT |
429 | } |
430 | ||
a1a499a3 | 431 | /* Some BIOS's are messed up and don't set all MTRRs the same! */ |
1da177e4 LT |
432 | void __init mtrr_state_warn(void) |
433 | { | |
434 | unsigned long mask = smp_changes_mask; | |
435 | ||
436 | if (!mask) | |
437 | return; | |
438 | if (mask & MTRR_CHANGE_MASK_FIXED) | |
a1a499a3 | 439 | pr_warning("mtrr: your CPUs had inconsistent fixed MTRR settings\n"); |
1da177e4 | 440 | if (mask & MTRR_CHANGE_MASK_VARIABLE) |
a1a499a3 | 441 | pr_warning("mtrr: your CPUs had inconsistent variable MTRR settings\n"); |
1da177e4 | 442 | if (mask & MTRR_CHANGE_MASK_DEFTYPE) |
a1a499a3 JSR |
443 | pr_warning("mtrr: your CPUs had inconsistent MTRRdefType settings\n"); |
444 | ||
1da177e4 LT |
445 | printk(KERN_INFO "mtrr: probably your BIOS does not setup all CPUs.\n"); |
446 | printk(KERN_INFO "mtrr: corrected configuration.\n"); | |
447 | } | |
448 | ||
a1a499a3 JSR |
449 | /* |
450 | * Doesn't attempt to pass an error out to MTRR users | |
451 | * because it's quite complicated in some cases and probably not | |
452 | * worth it because the best error handling is to ignore it. | |
453 | */ | |
1da177e4 LT |
454 | void mtrr_wrmsr(unsigned msr, unsigned a, unsigned b) |
455 | { | |
a1a499a3 | 456 | if (wrmsr_safe(msr, a, b) < 0) { |
1da177e4 LT |
457 | printk(KERN_ERR |
458 | "MTRR: CPU %u: Writing MSR %x to %x:%x failed\n", | |
459 | smp_processor_id(), msr, a, b); | |
a1a499a3 | 460 | } |
1da177e4 LT |
461 | } |
462 | ||
de938c51 | 463 | /** |
a1a499a3 JSR |
464 | * set_fixed_range - checks & updates a fixed-range MTRR if it |
465 | * differs from the value it should have | |
1d3381eb RD |
466 | * @msr: MSR address of the MTTR which should be checked and updated |
467 | * @changed: pointer which indicates whether the MTRR needed to be changed | |
468 | * @msrwords: pointer to the MSR values which the MSR should have | |
de938c51 | 469 | */ |
2d2ee8de | 470 | static void set_fixed_range(int msr, bool *changed, unsigned int *msrwords) |
de938c51 BK |
471 | { |
472 | unsigned lo, hi; | |
473 | ||
474 | rdmsr(msr, lo, hi); | |
475 | ||
476 | if (lo != msrwords[0] || hi != msrwords[1]) { | |
de938c51 | 477 | mtrr_wrmsr(msr, msrwords[0], msrwords[1]); |
2d2ee8de | 478 | *changed = true; |
de938c51 BK |
479 | } |
480 | } | |
481 | ||
1d3381eb RD |
482 | /** |
483 | * generic_get_free_region - Get a free MTRR. | |
484 | * @base: The starting (base) address of the region. | |
485 | * @size: The size (in bytes) of the region. | |
486 | * @replace_reg: mtrr index to be replaced; set to invalid value if none. | |
487 | * | |
488 | * Returns: The index of the region on success, else negative on error. | |
489 | */ | |
a1a499a3 JSR |
490 | int |
491 | generic_get_free_region(unsigned long base, unsigned long size, int replace_reg) | |
1da177e4 | 492 | { |
365bff80 | 493 | unsigned long lbase, lsize; |
a1a499a3 JSR |
494 | mtrr_type ltype; |
495 | int i, max; | |
1da177e4 LT |
496 | |
497 | max = num_var_ranges; | |
365bff80 JB |
498 | if (replace_reg >= 0 && replace_reg < max) |
499 | return replace_reg; | |
a1a499a3 | 500 | |
1da177e4 LT |
501 | for (i = 0; i < max; ++i) { |
502 | mtrr_if->get(i, &lbase, &lsize, <ype); | |
503 | if (lsize == 0) | |
504 | return i; | |
505 | } | |
a1a499a3 | 506 | |
1da177e4 LT |
507 | return -ENOSPC; |
508 | } | |
509 | ||
408b664a | 510 | static void generic_get_mtrr(unsigned int reg, unsigned long *base, |
365bff80 | 511 | unsigned long *size, mtrr_type *type) |
1da177e4 | 512 | { |
d5c78673 YL |
513 | u32 mask_lo, mask_hi, base_lo, base_hi; |
514 | unsigned int hi; | |
515 | u64 tmp, mask; | |
1da177e4 | 516 | |
8ad97905 YL |
517 | /* |
518 | * get_mtrr doesn't need to update mtrr_state, also it could be called | |
519 | * from any cpu, so try to print it out directly. | |
520 | */ | |
fa10ba64 | 521 | get_cpu(); |
63516ef6 | 522 | |
1da177e4 | 523 | rdmsr(MTRRphysMask_MSR(reg), mask_lo, mask_hi); |
8ad97905 | 524 | |
1da177e4 | 525 | if ((mask_lo & 0x800) == 0) { |
a1a499a3 | 526 | /* Invalid (i.e. free) range */ |
1da177e4 LT |
527 | *base = 0; |
528 | *size = 0; | |
529 | *type = 0; | |
63516ef6 | 530 | goto out_put_cpu; |
1da177e4 LT |
531 | } |
532 | ||
533 | rdmsr(MTRRphysBase_MSR(reg), base_lo, base_hi); | |
534 | ||
63516ef6 | 535 | /* Work out the shifted address mask: */ |
d5c78673 YL |
536 | tmp = (u64)mask_hi << (32 - PAGE_SHIFT) | mask_lo >> PAGE_SHIFT; |
537 | mask = size_or_mask | tmp; | |
63516ef6 YL |
538 | |
539 | /* Expand tmp with high bits to all 1s: */ | |
d5c78673 | 540 | hi = fls64(tmp); |
38cc1c3d | 541 | if (hi > 0) { |
d5c78673 | 542 | tmp |= ~((1ULL<<(hi - 1)) - 1); |
38cc1c3d | 543 | |
d5c78673 | 544 | if (tmp != mask) { |
942fa3b6 | 545 | printk(KERN_WARNING "mtrr: your BIOS has configured an incorrect mask, fixing it.\n"); |
373d4d09 | 546 | add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK); |
d5c78673 | 547 | mask = tmp; |
38cc1c3d YL |
548 | } |
549 | } | |
1da177e4 | 550 | |
63516ef6 YL |
551 | /* |
552 | * This works correctly if size is a power of two, i.e. a | |
553 | * contiguous range: | |
554 | */ | |
d5c78673 YL |
555 | *size = -mask; |
556 | *base = (u64)base_hi << (32 - PAGE_SHIFT) | base_lo >> PAGE_SHIFT; | |
1da177e4 | 557 | *type = base_lo & 0xff; |
8ad97905 | 558 | |
63516ef6 YL |
559 | out_put_cpu: |
560 | put_cpu(); | |
1da177e4 LT |
561 | } |
562 | ||
de938c51 | 563 | /** |
a1a499a3 JSR |
564 | * set_fixed_ranges - checks & updates the fixed-range MTRRs if they |
565 | * differ from the saved set | |
1d3381eb | 566 | * @frs: pointer to fixed-range MTRR values, saved by get_fixed_ranges() |
de938c51 | 567 | */ |
a1a499a3 | 568 | static int set_fixed_ranges(mtrr_type *frs) |
1da177e4 | 569 | { |
a1a499a3 | 570 | unsigned long long *saved = (unsigned long long *)frs; |
2d2ee8de | 571 | bool changed = false; |
a1a499a3 | 572 | int block = -1, range; |
1da177e4 | 573 | |
3ff42da5 AH |
574 | k8_check_syscfg_dram_mod_en(); |
575 | ||
a1a499a3 JSR |
576 | while (fixed_range_blocks[++block].ranges) { |
577 | for (range = 0; range < fixed_range_blocks[block].ranges; range++) | |
578 | set_fixed_range(fixed_range_blocks[block].base_msr + range, | |
579 | &changed, (unsigned int *)saved++); | |
580 | } | |
1da177e4 | 581 | |
1da177e4 LT |
582 | return changed; |
583 | } | |
584 | ||
a1a499a3 JSR |
585 | /* |
586 | * Set the MSR pair relating to a var range. | |
587 | * Returns true if changes are made. | |
588 | */ | |
2d2ee8de | 589 | static bool set_mtrr_var_ranges(unsigned int index, struct mtrr_var_range *vr) |
1da177e4 LT |
590 | { |
591 | unsigned int lo, hi; | |
2d2ee8de | 592 | bool changed = false; |
1da177e4 LT |
593 | |
594 | rdmsr(MTRRphysBase_MSR(index), lo, hi); | |
595 | if ((vr->base_lo & 0xfffff0ffUL) != (lo & 0xfffff0ffUL) | |
cf94b62f SS |
596 | || (vr->base_hi & (size_and_mask >> (32 - PAGE_SHIFT))) != |
597 | (hi & (size_and_mask >> (32 - PAGE_SHIFT)))) { | |
a1a499a3 | 598 | |
1da177e4 | 599 | mtrr_wrmsr(MTRRphysBase_MSR(index), vr->base_lo, vr->base_hi); |
2d2ee8de | 600 | changed = true; |
1da177e4 LT |
601 | } |
602 | ||
603 | rdmsr(MTRRphysMask_MSR(index), lo, hi); | |
604 | ||
605 | if ((vr->mask_lo & 0xfffff800UL) != (lo & 0xfffff800UL) | |
cf94b62f SS |
606 | || (vr->mask_hi & (size_and_mask >> (32 - PAGE_SHIFT))) != |
607 | (hi & (size_and_mask >> (32 - PAGE_SHIFT)))) { | |
1da177e4 | 608 | mtrr_wrmsr(MTRRphysMask_MSR(index), vr->mask_lo, vr->mask_hi); |
2d2ee8de | 609 | changed = true; |
1da177e4 LT |
610 | } |
611 | return changed; | |
612 | } | |
613 | ||
365bff80 JB |
614 | static u32 deftype_lo, deftype_hi; |
615 | ||
1d3381eb RD |
616 | /** |
617 | * set_mtrr_state - Set the MTRR state for this CPU. | |
618 | * | |
619 | * NOTE: The CPU must already be in a safe state for MTRR changes. | |
620 | * RETURNS: 0 if no changes made, else a mask indicating what was changed. | |
621 | */ | |
365bff80 | 622 | static unsigned long set_mtrr_state(void) |
1da177e4 | 623 | { |
1da177e4 | 624 | unsigned long change_mask = 0; |
a1a499a3 | 625 | unsigned int i; |
1da177e4 | 626 | |
a1a499a3 | 627 | for (i = 0; i < num_var_ranges; i++) { |
1da177e4 LT |
628 | if (set_mtrr_var_ranges(i, &mtrr_state.var_ranges[i])) |
629 | change_mask |= MTRR_CHANGE_MASK_VARIABLE; | |
a1a499a3 | 630 | } |
1da177e4 | 631 | |
365bff80 | 632 | if (mtrr_state.have_fixed && set_fixed_ranges(mtrr_state.fixed_ranges)) |
1da177e4 LT |
633 | change_mask |= MTRR_CHANGE_MASK_FIXED; |
634 | ||
a1a499a3 JSR |
635 | /* |
636 | * Set_mtrr_restore restores the old value of MTRRdefType, | |
637 | * so to set it we fiddle with the saved value: | |
638 | */ | |
1da177e4 LT |
639 | if ((deftype_lo & 0xff) != mtrr_state.def_type |
640 | || ((deftype_lo & 0xc00) >> 10) != mtrr_state.enabled) { | |
a1a499a3 JSR |
641 | |
642 | deftype_lo = (deftype_lo & ~0xcff) | mtrr_state.def_type | | |
643 | (mtrr_state.enabled << 10); | |
1da177e4 LT |
644 | change_mask |= MTRR_CHANGE_MASK_DEFTYPE; |
645 | } | |
646 | ||
647 | return change_mask; | |
648 | } | |
649 | ||
650 | ||
a1a499a3 | 651 | static unsigned long cr4; |
40d6753e | 652 | static DEFINE_RAW_SPINLOCK(set_atomicity_lock); |
1da177e4 LT |
653 | |
654 | /* | |
a1a499a3 JSR |
655 | * Since we are disabling the cache don't allow any interrupts, |
656 | * they would run extremely slow and would only increase the pain. | |
657 | * | |
658 | * The caller must ensure that local interrupts are disabled and | |
659 | * are reenabled after post_set() has been called. | |
1da177e4 | 660 | */ |
182daa55 | 661 | static void prepare_set(void) __acquires(set_atomicity_lock) |
1da177e4 LT |
662 | { |
663 | unsigned long cr0; | |
664 | ||
a1a499a3 JSR |
665 | /* |
666 | * Note that this is not ideal | |
667 | * since the cache is only flushed/disabled for this CPU while the | |
668 | * MTRRs are changed, but changing this requires more invasive | |
669 | * changes to the way the kernel boots | |
670 | */ | |
1da177e4 | 671 | |
40d6753e | 672 | raw_spin_lock(&set_atomicity_lock); |
1da177e4 | 673 | |
a1a499a3 | 674 | /* Enter the no-fill (CD=1, NW=0) cache mode and flush caches. */ |
7ebad705 | 675 | cr0 = read_cr0() | X86_CR0_CD; |
1da177e4 LT |
676 | write_cr0(cr0); |
677 | wbinvd(); | |
678 | ||
a1a499a3 JSR |
679 | /* Save value of CR4 and clear Page Global Enable (bit 7) */ |
680 | if (cpu_has_pge) { | |
1da177e4 LT |
681 | cr4 = read_cr4(); |
682 | write_cr4(cr4 & ~X86_CR4_PGE); | |
683 | } | |
684 | ||
685 | /* Flush all TLBs via a mov %cr3, %reg; mov %reg, %cr3 */ | |
ec659934 | 686 | count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ALL); |
1da177e4 LT |
687 | __flush_tlb(); |
688 | ||
a1a499a3 | 689 | /* Save MTRR state */ |
52650257 | 690 | rdmsr(MSR_MTRRdefType, deftype_lo, deftype_hi); |
1da177e4 | 691 | |
a1a499a3 | 692 | /* Disable MTRRs, and set the default type to uncached */ |
52650257 | 693 | mtrr_wrmsr(MSR_MTRRdefType, deftype_lo & ~0xcff, deftype_hi); |
8dbf4a30 | 694 | wbinvd(); |
1da177e4 LT |
695 | } |
696 | ||
182daa55 | 697 | static void post_set(void) __releases(set_atomicity_lock) |
1da177e4 | 698 | { |
a1a499a3 | 699 | /* Flush TLBs (no need to flush caches - they are disabled) */ |
ec659934 | 700 | count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ALL); |
1da177e4 LT |
701 | __flush_tlb(); |
702 | ||
703 | /* Intel (P6) standard MTRRs */ | |
52650257 | 704 | mtrr_wrmsr(MSR_MTRRdefType, deftype_lo, deftype_hi); |
a1a499a3 JSR |
705 | |
706 | /* Enable caches */ | |
a3d7b7dd | 707 | write_cr0(read_cr0() & ~X86_CR0_CD); |
1da177e4 | 708 | |
a1a499a3 JSR |
709 | /* Restore value of CR4 */ |
710 | if (cpu_has_pge) | |
1da177e4 | 711 | write_cr4(cr4); |
40d6753e | 712 | raw_spin_unlock(&set_atomicity_lock); |
1da177e4 LT |
713 | } |
714 | ||
715 | static void generic_set_all(void) | |
716 | { | |
717 | unsigned long mask, count; | |
718 | unsigned long flags; | |
719 | ||
720 | local_irq_save(flags); | |
721 | prepare_set(); | |
722 | ||
723 | /* Actually set the state */ | |
365bff80 | 724 | mask = set_mtrr_state(); |
1da177e4 | 725 | |
2e5d9c85 | 726 | /* also set PAT */ |
727 | pat_init(); | |
728 | ||
1da177e4 LT |
729 | post_set(); |
730 | local_irq_restore(flags); | |
731 | ||
a1a499a3 | 732 | /* Use the atomic bitops to update the global mask */ |
1da177e4 LT |
733 | for (count = 0; count < sizeof mask * 8; ++count) { |
734 | if (mask & 0x01) | |
735 | set_bit(count, &smp_changes_mask); | |
736 | mask >>= 1; | |
737 | } | |
a1a499a3 | 738 | |
1da177e4 LT |
739 | } |
740 | ||
a1a499a3 JSR |
741 | /** |
742 | * generic_set_mtrr - set variable MTRR register on the local CPU. | |
743 | * | |
744 | * @reg: The register to set. | |
745 | * @base: The base address of the region. | |
746 | * @size: The size of the region. If this is 0 the region is disabled. | |
747 | * @type: The type of the region. | |
748 | * | |
749 | * Returns nothing. | |
750 | */ | |
1da177e4 LT |
751 | static void generic_set_mtrr(unsigned int reg, unsigned long base, |
752 | unsigned long size, mtrr_type type) | |
1da177e4 LT |
753 | { |
754 | unsigned long flags; | |
3b520b23 SL |
755 | struct mtrr_var_range *vr; |
756 | ||
757 | vr = &mtrr_state.var_ranges[reg]; | |
1da177e4 LT |
758 | |
759 | local_irq_save(flags); | |
760 | prepare_set(); | |
761 | ||
762 | if (size == 0) { | |
a1a499a3 JSR |
763 | /* |
764 | * The invalid bit is kept in the mask, so we simply | |
765 | * clear the relevant mask register to disable a range. | |
766 | */ | |
1da177e4 | 767 | mtrr_wrmsr(MTRRphysMask_MSR(reg), 0, 0); |
3b520b23 | 768 | memset(vr, 0, sizeof(struct mtrr_var_range)); |
1da177e4 | 769 | } else { |
3b520b23 SL |
770 | vr->base_lo = base << PAGE_SHIFT | type; |
771 | vr->base_hi = (base & size_and_mask) >> (32 - PAGE_SHIFT); | |
772 | vr->mask_lo = -size << PAGE_SHIFT | 0x800; | |
773 | vr->mask_hi = (-size & size_and_mask) >> (32 - PAGE_SHIFT); | |
774 | ||
775 | mtrr_wrmsr(MTRRphysBase_MSR(reg), vr->base_lo, vr->base_hi); | |
776 | mtrr_wrmsr(MTRRphysMask_MSR(reg), vr->mask_lo, vr->mask_hi); | |
1da177e4 LT |
777 | } |
778 | ||
779 | post_set(); | |
780 | local_irq_restore(flags); | |
781 | } | |
782 | ||
a1a499a3 JSR |
783 | int generic_validate_add_page(unsigned long base, unsigned long size, |
784 | unsigned int type) | |
1da177e4 LT |
785 | { |
786 | unsigned long lbase, last; | |
787 | ||
a1a499a3 JSR |
788 | /* |
789 | * For Intel PPro stepping <= 7 | |
790 | * must be 4 MiB aligned and not touch 0x70000000 -> 0x7003FFFF | |
791 | */ | |
1da177e4 LT |
792 | if (is_cpu(INTEL) && boot_cpu_data.x86 == 6 && |
793 | boot_cpu_data.x86_model == 1 && | |
794 | boot_cpu_data.x86_mask <= 7) { | |
795 | if (base & ((1 << (22 - PAGE_SHIFT)) - 1)) { | |
a1a499a3 | 796 | pr_warning("mtrr: base(0x%lx000) is not 4 MiB aligned\n", base); |
1da177e4 LT |
797 | return -EINVAL; |
798 | } | |
9b483417 | 799 | if (!(base + size < 0x70000 || base > 0x7003F) && |
1da177e4 LT |
800 | (type == MTRR_TYPE_WRCOMB |
801 | || type == MTRR_TYPE_WRBACK)) { | |
a1a499a3 | 802 | pr_warning("mtrr: writable mtrr between 0x70000000 and 0x7003FFFF may hang the CPU.\n"); |
1da177e4 LT |
803 | return -EINVAL; |
804 | } | |
805 | } | |
806 | ||
a1a499a3 JSR |
807 | /* |
808 | * Check upper bits of base and last are equal and lower bits are 0 | |
809 | * for base and 1 for last | |
810 | */ | |
1da177e4 LT |
811 | last = base + size - 1; |
812 | for (lbase = base; !(lbase & 1) && (last & 1); | |
a1a499a3 JSR |
813 | lbase = lbase >> 1, last = last >> 1) |
814 | ; | |
1da177e4 | 815 | if (lbase != last) { |
a1a499a3 | 816 | pr_warning("mtrr: base(0x%lx000) is not aligned on a size(0x%lx000) boundary\n", base, size); |
1da177e4 LT |
817 | return -EINVAL; |
818 | } | |
819 | return 0; | |
820 | } | |
821 | ||
1da177e4 LT |
822 | static int generic_have_wrcomb(void) |
823 | { | |
824 | unsigned long config, dummy; | |
d9bcc01d | 825 | rdmsr(MSR_MTRRcap, config, dummy); |
a1a499a3 | 826 | return config & (1 << 10); |
1da177e4 LT |
827 | } |
828 | ||
829 | int positive_have_wrcomb(void) | |
830 | { | |
831 | return 1; | |
832 | } | |
833 | ||
a1a499a3 JSR |
834 | /* |
835 | * Generic structure... | |
1da177e4 | 836 | */ |
3b9cfc0a | 837 | const struct mtrr_ops generic_mtrr_ops = { |
a1a499a3 JSR |
838 | .use_intel_if = 1, |
839 | .set_all = generic_set_all, | |
840 | .get = generic_get_mtrr, | |
841 | .get_free_region = generic_get_free_region, | |
842 | .set = generic_set_mtrr, | |
843 | .validate_add_page = generic_validate_add_page, | |
844 | .have_wrcomb = generic_have_wrcomb, | |
1da177e4 | 845 | }; |