Debian Codegraph @

src/cpu/x86/mtrr/mtrr.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 /*
4 * mtrr.c: setting MTRR to decent values for cache initialization on P6
5 * Derived from intel_set_mtrr in intel_subr.c and mtrr.c in linux kernel
6 *
7 * Reference: Intel Architecture Software Developer's Manual, Volume 3: System
8 * Programming
9 */
10
11 #include <assert.h>
12 #include <bootstate.h>
13 #include <commonlib/helpers.h>
14 #include <console/console.h>
15 #include <cpu/amd/mtrr.h>
16 #include <cpu/cpu.h>
17 #include <cpu/x86/cache.h>
18 #include <cpu/x86/lapic.h>
19 #include <cpu/x86/msr.h>
20 #include <cpu/x86/mtrr.h>
21 #include <device/device.h>
22 #include <device/pci_ids.h>
23 #include <lib.h>
24 #include <memrange.h>
25 #include <string.h>
26 #include <types.h>
27
28 #if CONFIG(X86_AMD_FIXED_MTRRS)
29 #define MTRR_FIXED_WRBACK_BITS (MTRR_READ_MEM | MTRR_WRITE_MEM)
30 #else
31 #define MTRR_FIXED_WRBACK_BITS 0
32 #endif
33
34 #define MIN_MTRRS 8
35
36 /*
37 * Static storage size for variable MTRRs. It's sized sufficiently large to
38 * handle different types of CPUs. Empirically, 16 variable MTRRs has not
39 * yet been observed.
40 */
41 #define NUM_MTRR_STATIC_STORAGE 16
42
43 static int total_mtrrs;
44
45 static void detect_var_mtrrs(void)
46 {
47 total_mtrrs = get_var_mtrr_count();
48
49 if (total_mtrrs > NUM_MTRR_STATIC_STORAGE) {
50 printk(BIOS_WARNING,
51 "MTRRs detected (%d) > NUM_MTRR_STATIC_STORAGE (%d)\n",
52 total_mtrrs, NUM_MTRR_STATIC_STORAGE);
53 total_mtrrs = NUM_MTRR_STATIC_STORAGE;
54 }
55 }
56
57 void enable_fixed_mtrr(void)
58 {
59 msr_t msr;
60
61 msr = rdmsr(MTRR_DEF_TYPE_MSR);
62 msr.lo |= MTRR_DEF_TYPE_EN | MTRR_DEF_TYPE_FIX_EN;
63 wrmsr(MTRR_DEF_TYPE_MSR, msr);
64 }
65
66 void fixed_mtrrs_expose_amd_rwdram(void)
67 {
68 msr_t syscfg;
69
70 if (!CONFIG(X86_AMD_FIXED_MTRRS))
71 return;
72
73 syscfg = rdmsr(SYSCFG_MSR);
74 syscfg.lo |= SYSCFG_MSR_MtrrFixDramModEn;
75 wrmsr(SYSCFG_MSR, syscfg);
76 }
77
78 void fixed_mtrrs_hide_amd_rwdram(void)
79 {
80 msr_t syscfg;
81
82 if (!CONFIG(X86_AMD_FIXED_MTRRS))
83 return;
84
85 syscfg = rdmsr(SYSCFG_MSR);
86 syscfg.lo &= ~SYSCFG_MSR_MtrrFixDramModEn;
87 wrmsr(SYSCFG_MSR, syscfg);
88 }
89
90 static void enable_var_mtrr(unsigned char deftype)
91 {
92 msr_t msr;
93
94 msr = rdmsr(MTRR_DEF_TYPE_MSR);
95 msr.lo &= ~0xff;
96 msr.lo |= MTRR_DEF_TYPE_EN | deftype;
97 wrmsr(MTRR_DEF_TYPE_MSR, msr);
98 }
99
100 static inline uint64_t range_entry_base_mtrr_addr(struct range_entry *r)
101 {
102 return PHYS_TO_RANGE_ADDR(range_entry_base(r));
103 }
104
105 static inline uint64_t range_entry_end_mtrr_addr(struct range_entry *r)
106 {
107 return PHYS_TO_RANGE_ADDR(range_entry_end(r));
108 }
109
110 static inline int range_entry_mtrr_type(struct range_entry *r)
111 {
112 return range_entry_tag(r) & MTRR_TAG_MASK;
113 }
114
115 static int filter_vga_wrcomb(struct device *dev, struct resource *res)
116 {
117 /* Only handle PCI devices. */
118 if (dev->path.type != DEVICE_PATH_PCI)
119 return 0;
120
121 /* Only handle VGA class devices. */
122 if (((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA))
123 return 0;
124
125 /* Add resource as write-combining in the address space. */
126 return 1;
127 }
128
129 static void print_physical_address_space(const struct memranges *addr_space,
130 const char *identifier)
131 {
132 const struct range_entry *r;
133
134 if (identifier)
135 printk(BIOS_DEBUG, "MTRR: %s Physical address space:\n",
136 identifier);
137 else
138 printk(BIOS_DEBUG, "MTRR: Physical address space:\n");
139
140 memranges_each_entry(r, addr_space)
141 printk(BIOS_DEBUG,
142 "0x%016llx - 0x%016llx size 0x%08llx type %ld\n",
143 range_entry_base(r), range_entry_end(r) - 1,
144 range_entry_size(r), range_entry_tag(r));
145 }
146
147 static struct memranges *get_physical_address_space(void)
148 {
149 static struct memranges *addr_space;
150 static struct memranges addr_space_storage;
151
152 /* In order to handle some chipsets not being able to pre-determine
153 * uncacheable ranges, such as graphics memory, at resource insertion
154 * time remove uncacheable regions from the cacheable ones. */
155 if (addr_space == NULL) {
156 unsigned long mask;
157 unsigned long match;
158
159 addr_space = &addr_space_storage;
160
161 mask = IORESOURCE_CACHEABLE;
162 /* Collect cacheable and uncacheable address ranges. The
163 * uncacheable regions take precedence over the cacheable
164 * regions. */
165 memranges_init(addr_space, mask, mask, MTRR_TYPE_WRBACK);
166 memranges_add_resources(addr_space, mask, 0,
167 MTRR_TYPE_UNCACHEABLE);
168
169 /* Handle any write combining resources. Only prefetchable
170 * resources are appropriate for this MTRR type. */
171 match = IORESOURCE_PREFETCH;
172 mask |= match;
173 memranges_add_resources_filter(addr_space, mask, match,
174 MTRR_TYPE_WRCOMB, filter_vga_wrcomb);
175
176 /* The address space below 4GiB is special. It needs to be
177 * covered entirely by range entries so that MTRR calculations
178 * can be properly done for the full 32-bit address space.
179 * Therefore, ensure holes are filled up to 4GiB as
180 * uncacheable */
181 memranges_fill_holes_up_to(addr_space,
182 RANGE_TO_PHYS_ADDR(RANGE_4GB),
183 MTRR_TYPE_UNCACHEABLE);
184
185 print_physical_address_space(addr_space, NULL);
186 }
187
188 return addr_space;
189 }
190
191 /* Fixed MTRR descriptor. This structure defines the step size and begin
192 * and end (exclusive) address covered by a set of fixed MTRR MSRs.
193 * It also describes the offset in byte intervals to store the calculated MTRR
194 * type in an array. */
195 struct fixed_mtrr_desc {
196 uint32_t begin;
197 uint32_t end;
198 uint32_t step;
199 int range_index;
200 int msr_index_base;
201 };
202
203 /* Shared MTRR calculations. Can be reused by APs. */
204 static uint8_t fixed_mtrr_types[NUM_FIXED_RANGES];
205
206 /* Fixed MTRR descriptors. */
207 static const struct fixed_mtrr_desc fixed_mtrr_desc[] = {
208 { PHYS_TO_RANGE_ADDR(0x000000), PHYS_TO_RANGE_ADDR(0x080000),
209 PHYS_TO_RANGE_ADDR(64 * 1024), 0, MTRR_FIX_64K_00000 },
210 { PHYS_TO_RANGE_ADDR(0x080000), PHYS_TO_RANGE_ADDR(0x0C0000),
211 PHYS_TO_RANGE_ADDR(16 * 1024), 8, MTRR_FIX_16K_80000 },
212 { PHYS_TO_RANGE_ADDR(0x0C0000), PHYS_TO_RANGE_ADDR(0x100000),
213 PHYS_TO_RANGE_ADDR(4 * 1024), 24, MTRR_FIX_4K_C0000 },
214 };
215
216 static void calc_fixed_mtrrs(void)
217 {
218 static int fixed_mtrr_types_initialized;
219 struct memranges *phys_addr_space;
220 struct range_entry *r;
221 const struct fixed_mtrr_desc *desc;
222 const struct fixed_mtrr_desc *last_desc;
223 uint32_t begin;
224 uint32_t end;
225 int type_index;
226
227 if (fixed_mtrr_types_initialized)
228 return;
229
230 phys_addr_space = get_physical_address_space();
231
232 /* Set all fixed ranges to uncacheable first. */
233 memset(&fixed_mtrr_types[0], MTRR_TYPE_UNCACHEABLE, NUM_FIXED_RANGES);
234
235 desc = &fixed_mtrr_desc[0];
236 last_desc = &fixed_mtrr_desc[ARRAY_SIZE(fixed_mtrr_desc) - 1];
237
238 memranges_each_entry(r, phys_addr_space) {
239 begin = range_entry_base_mtrr_addr(r);
240 end = range_entry_end_mtrr_addr(r);
241
242 if (begin >= last_desc->end)
243 break;
244
245 if (end > last_desc->end)
246 end = last_desc->end;
247
248 /* Get to the correct fixed mtrr descriptor. */
249 while (begin >= desc->end)
250 desc++;
251
252 type_index = desc->range_index;
253 type_index += (begin - desc->begin) / desc->step;
254
255 while (begin != end) {
256 unsigned char type;
257
258 type = range_entry_tag(r);
259 printk(MTRR_VERBOSE_LEVEL,
260 "MTRR addr 0x%x-0x%x set to %d type @ %d\n",
261 begin, begin + desc->step - 1, type, type_index);
262 if (type == MTRR_TYPE_WRBACK)
263 type |= MTRR_FIXED_WRBACK_BITS;
264 fixed_mtrr_types[type_index] = type;
265 type_index++;
266 begin += desc->step;
267 if (begin == desc->end)
268 desc++;
269 }
270 }
271 fixed_mtrr_types_initialized = 1;
272 }
273
274 static void commit_fixed_mtrrs(void)
275 {
276 int i;
277 int j;
278 int msr_num;
279 int type_index;
280 const unsigned int lapic_id = lapicid();
281 /* 8 ranges per msr. */
282 msr_t fixed_msrs[NUM_FIXED_MTRRS];
283 unsigned long msr_index[NUM_FIXED_MTRRS];
284
285 fixed_mtrrs_expose_amd_rwdram();
286
287 memset(&fixed_msrs, 0, sizeof(fixed_msrs));
288
289 msr_num = 0;
290 type_index = 0;
291 for (i = 0; i < ARRAY_SIZE(fixed_mtrr_desc); i++) {
292 const struct fixed_mtrr_desc *desc;
293 int num_ranges;
294
295 desc = &fixed_mtrr_desc[i];
296 num_ranges = (desc->end - desc->begin) / desc->step;
297 for (j = 0; j < num_ranges; j += RANGES_PER_FIXED_MTRR) {
298 msr_index[msr_num] = desc->msr_index_base +
299 (j / RANGES_PER_FIXED_MTRR);
300 fixed_msrs[msr_num].lo |=
301 fixed_mtrr_types[type_index++] << 0;
302 fixed_msrs[msr_num].lo |=
303 fixed_mtrr_types[type_index++] << 8;
304 fixed_msrs[msr_num].lo |=
305 fixed_mtrr_types[type_index++] << 16;
306 fixed_msrs[msr_num].lo |=
307 fixed_mtrr_types[type_index++] << 24;
308 fixed_msrs[msr_num].hi |=
309 fixed_mtrr_types[type_index++] << 0;
310 fixed_msrs[msr_num].hi |=
311 fixed_mtrr_types[type_index++] << 8;
312 fixed_msrs[msr_num].hi |=
313 fixed_mtrr_types[type_index++] << 16;
314 fixed_msrs[msr_num].hi |=
315 fixed_mtrr_types[type_index++] << 24;
316 msr_num++;
317 }
318 }
319
320 /* Ensure that both arrays were fully initialized */
321 ASSERT(msr_num == NUM_FIXED_MTRRS)
322
323 for (i = 0; i < ARRAY_SIZE(fixed_msrs); i++)
324 printk(BIOS_DEBUG, "apic_id 0x%x: MTRR: Fixed MSR 0x%lx 0x%08x%08x\n",
325 lapic_id, msr_index[i], fixed_msrs[i].hi, fixed_msrs[i].lo);
326
327 disable_cache();
328 for (i = 0; i < ARRAY_SIZE(fixed_msrs); i++)
329 wrmsr(msr_index[i], fixed_msrs[i]);
330 enable_cache();
331 fixed_mtrrs_hide_amd_rwdram();
332 }
333
334 static void x86_setup_fixed_mtrrs_no_enable(void)
335 {
336 calc_fixed_mtrrs();
337 commit_fixed_mtrrs();
338 }
339
340 static void x86_setup_fixed_mtrrs(void)
341 {
342 x86_setup_fixed_mtrrs_no_enable();
343
344 printk(BIOS_SPEW, "apic_id 0x%x call enable_fixed_mtrr()\n", lapicid());
345 enable_fixed_mtrr();
346 }
347
348 struct var_mtrr_regs {
349 msr_t base;
350 msr_t mask;
351 };
352
353 struct var_mtrr_solution {
354 int mtrr_default_type;
355 int num_used;
356 struct var_mtrr_regs regs[NUM_MTRR_STATIC_STORAGE];
357 };
358
359 /* Global storage for variable MTRR solution. */
360 static struct var_mtrr_solution mtrr_global_solution;
361
362 struct var_mtrr_state {
363 struct memranges *addr_space;
364 bool above4gb;
365 int address_bits;
366 int prepare_msrs;
367 int mtrr_index;
368 int def_mtrr_type;
369 struct var_mtrr_regs *regs;
370 };
371
372 static int get_os_reserved_mtrrs(void)
373 {
374 return CONFIG(RESERVE_MTRRS_FOR_OS) ? 2 : 0;
375 }
376
377 static void prep_var_mtrr(struct var_mtrr_state *var_state,
378 uint64_t base, uint64_t size, int mtrr_type)
379 {
380 struct var_mtrr_regs *regs;
381 resource_t rbase;
382 resource_t rsize;
383 resource_t mask;
384
385 if (var_state->mtrr_index >= total_mtrrs) {
386 printk(BIOS_ERR, "Not enough MTRRs available! MTRR index is %d with %d MTRRs in total.\n",
387 var_state->mtrr_index, total_mtrrs);
388 return;
389 }
390
391 /*
392 * If desired, 2 variable MTRRs are attempted to be saved for the OS to
393 * use. However, it's more important to try to map the full address
394 * space properly.
395 */
396 if (var_state->mtrr_index >= total_mtrrs - get_os_reserved_mtrrs())
397 printk(BIOS_WARNING, "Taking a reserved OS MTRR.\n");
398
399 rbase = base;
400 rsize = size;
401
402 rbase = RANGE_TO_PHYS_ADDR(rbase);
403 rsize = RANGE_TO_PHYS_ADDR(rsize);
404 rsize = -rsize;
405
406 mask = (1ULL << var_state->address_bits) - 1;
407 rsize = rsize & mask;
408
409 printk(BIOS_DEBUG, "MTRR: %d base 0x%016llx mask 0x%016llx type %d\n",
410 var_state->mtrr_index, rbase, rsize, mtrr_type);
411
412 regs = &var_state->regs[var_state->mtrr_index];
413
414 regs->base.lo = rbase;
415 regs->base.lo |= mtrr_type;
416 regs->base.hi = rbase >> 32;
417
418 regs->mask.lo = rsize;
419 regs->mask.lo |= MTRR_PHYS_MASK_VALID;
420 regs->mask.hi = rsize >> 32;
421 }
422
423 static void calc_var_mtrr_range(struct var_mtrr_state *var_state,
424 uint64_t base, uint64_t size, int mtrr_type)
425 {
426 while (size != 0) {
427 uint32_t addr_lsb;
428 uint32_t size_msb;
429 uint64_t mtrr_size;
430
431 addr_lsb = __ffs64(base);
432 size_msb = __fls64(size);
433
434 /* All MTRR entries need to have their base aligned to the mask
435 * size. The maximum size is calculated by a function of the
436 * min base bit set and maximum size bit set. */
437 if (addr_lsb > size_msb)
438 mtrr_size = 1ULL << size_msb;
439 else
440 mtrr_size = 1ULL << addr_lsb;
441
442 if (var_state->prepare_msrs)
443 prep_var_mtrr(var_state, base, mtrr_size, mtrr_type);
444
445 size -= mtrr_size;
446 base += mtrr_size;
447 var_state->mtrr_index++;
448 }
449 }
450
451 static uint64_t optimize_var_mtrr_hole(const uint64_t base,
452 const uint64_t hole,
453 const uint64_t limit,
454 const int carve_hole)
455 {
456 /*
457 * With default type UC, we can potentially optimize a WB
458 * range with unaligned upper end, by aligning it up and
459 * carving the added "hole" out again.
460 *
461 * To optimize the upper end of the hole, we will test
462 * how many MTRRs calc_var_mtrr_range() will spend for any
463 * alignment of the hole's upper end.
464 *
465 * We take four parameters, the lower end of the WB range
466 * `base`, upper end of the WB range as start of the `hole`,
467 * a `limit` how far we may align the upper end of the hole
468 * up and a flag `carve_hole` whether we should count MTRRs
469 * for carving the hole out. We return the optimal upper end
470 * for the hole (which may be the same as the end of the WB
471 * range in case we don't gain anything by aligning up).
472 */
473
474 const int dont_care = 0;
475 struct var_mtrr_state var_state = { 0, };
476
477 unsigned int align, best_count;
478 uint32_t best_end = hole;
479
480 /* calculate MTRR count for the WB range alone (w/o a hole) */
481 calc_var_mtrr_range(&var_state, base, hole - base, dont_care);
482 best_count = var_state.mtrr_index;
483 var_state.mtrr_index = 0;
484
485 for (align = __ffs(hole) + 1; align <= __fls(hole); ++align) {
486 const uint64_t hole_end = ALIGN_UP((uint64_t)hole, 1 << align);
487 if (hole_end > limit)
488 break;
489
490 /* calculate MTRR count for this alignment */
491 calc_var_mtrr_range(
492 &var_state, base, hole_end - base, dont_care);
493 if (carve_hole)
494 calc_var_mtrr_range(
495 &var_state, hole, hole_end - hole, dont_care);
496
497 if (var_state.mtrr_index < best_count) {
498 best_count = var_state.mtrr_index;
499 best_end = hole_end;
500 }
501 var_state.mtrr_index = 0;
502 }
503
504 return best_end;
505 }
506
507 static void calc_var_mtrrs_with_hole(struct var_mtrr_state *var_state,
508 struct range_entry *r)
509 {
510 uint64_t a1, a2, b1, b2;
511 int mtrr_type, carve_hole;
512
513 /*
514 * Determine MTRRs based on the following algorithm for the given entry:
515 * +------------------+ b2 = ALIGN_UP(end)
516 * | 0 or more bytes | <-- hole is carved out between b1 and b2
517 * +------------------+ a2 = b1 = original end
518 * | |
519 * +------------------+ a1 = begin
520 *
521 * Thus, there are up to 2 sub-ranges to configure variable MTRRs for.
522 */
523 mtrr_type = range_entry_mtrr_type(r);
524
525 a1 = range_entry_base_mtrr_addr(r);
526 a2 = range_entry_end_mtrr_addr(r);
527
528 /* The end address is within the first 1MiB. The fixed MTRRs take
529 * precedence over the variable ones. Therefore this range
530 * can be ignored. */
531 if (a2 <= RANGE_1MB)
532 return;
533
534 /* Again, the fixed MTRRs take precedence so the beginning
535 * of the range can be set to 0 if it starts at or below 1MiB. */
536 if (a1 <= RANGE_1MB)
537 a1 = 0;
538
539 /* If the range starts above 4GiB the processing is done. */
540 if (!var_state->above4gb && a1 >= RANGE_4GB)
541 return;
542
543 /* Clip the upper address to 4GiB if addresses above 4GiB
544 * are not being processed. */
545 if (!var_state->above4gb && a2 > RANGE_4GB)
546 a2 = RANGE_4GB;
547
548 b1 = a2;
549 b2 = a2;
550 carve_hole = 0;
551
552 /* We only consider WB type ranges for hole-carving. */
553 if (mtrr_type == MTRR_TYPE_WRBACK) {
554 struct range_entry *next;
555 uint64_t b2_limit;
556 /*
557 * Depending on the type of the next range, there are three
558 * different situations to handle:
559 *
560 * 1. WB range is last in address space:
561 * Aligning up, up to the next power of 2, may gain us
562 * something.
563 *
564 * 2. The next range is of type UC:
565 * We may align up, up to the _end_ of the next range. If
566 * there is a gap between the current and the next range,
567 * it would have been covered by the default type UC anyway.
568 *
569 * 3. The next range is not of type UC:
570 * We may align up, up to the _base_ of the next range. This
571 * may either be the end of the current range (if the next
572 * range follows immediately) or the end of the gap between
573 * the ranges.
574 */
575 next = memranges_next_entry(var_state->addr_space, r);
576 if (next == NULL) {
577 b2_limit = ALIGN_UP((uint64_t)b1, 1 << __fls(b1));
578 /* If it's the last range above 4GiB, we won't carve
579 the hole out. If an OS wanted to move MMIO there,
580 it would have to override the MTRR setting using
581 PAT just like it would with WB as default type. */
582 carve_hole = a1 < RANGE_4GB;
583 } else if (range_entry_mtrr_type(next)
584 == MTRR_TYPE_UNCACHEABLE) {
585 b2_limit = range_entry_end_mtrr_addr(next);
586 carve_hole = 1;
587 } else {
588 b2_limit = range_entry_base_mtrr_addr(next);
589 carve_hole = 1;
590 }
591 b2 = optimize_var_mtrr_hole(a1, b1, b2_limit, carve_hole);
592 }
593
594 calc_var_mtrr_range(var_state, a1, b2 - a1, mtrr_type);
595 if (carve_hole && b2 != b1) {
596 calc_var_mtrr_range(var_state, b1, b2 - b1,
597 MTRR_TYPE_UNCACHEABLE);
598 }
599 }
600
601 static void __calc_var_mtrrs(struct memranges *addr_space,
602 bool above4gb, int address_bits,
603 int *num_def_wb_mtrrs, int *num_def_uc_mtrrs)
604 {
605 int wb_deftype_count;
606 int uc_deftype_count;
607 struct range_entry *r;
608 struct var_mtrr_state var_state;
609
610 /* The default MTRR cacheability type is determined by calculating
611 * the number of MTRRs required for each MTRR type as if it was the
612 * default. */
613 var_state.addr_space = addr_space;
614 var_state.above4gb = above4gb;
615 var_state.address_bits = address_bits;
616 var_state.prepare_msrs = 0;
617
618 wb_deftype_count = 0;
619 uc_deftype_count = 0;
620
621 /*
622 * For each range do 2 calculations:
623 * 1. UC as default type with possible holes at top of range.
624 * 2. WB as default.
625 * The lowest count is then used as default after totaling all
626 * MTRRs. UC takes precedence in the MTRR architecture. There-
627 * fore, only holes can be used when the type of the region is
628 * MTRR_TYPE_WRBACK with MTRR_TYPE_UNCACHEABLE as the default
629 * type.
630 */
631 memranges_each_entry(r, var_state.addr_space) {
632 int mtrr_type;
633
634 mtrr_type = range_entry_mtrr_type(r);
635
636 if (mtrr_type != MTRR_TYPE_UNCACHEABLE) {
637 var_state.mtrr_index = 0;
638 var_state.def_mtrr_type = MTRR_TYPE_UNCACHEABLE;
639 calc_var_mtrrs_with_hole(&var_state, r);
640 uc_deftype_count += var_state.mtrr_index;
641 }
642
643 if (mtrr_type != MTRR_TYPE_WRBACK) {
644 var_state.mtrr_index = 0;
645 var_state.def_mtrr_type = MTRR_TYPE_WRBACK;
646 calc_var_mtrrs_with_hole(&var_state, r);
647 wb_deftype_count += var_state.mtrr_index;
648 }
649 }
650
651 *num_def_wb_mtrrs = wb_deftype_count;
652 *num_def_uc_mtrrs = uc_deftype_count;
653 }
654
655 static int calc_var_mtrrs(struct memranges *addr_space, bool above4gb, int address_bits,
656 int *num_mtrrs_used)
657 {
658 int wb_deftype_count = 0;
659 int uc_deftype_count = 0;
660
661 __calc_var_mtrrs(addr_space, above4gb, address_bits, &wb_deftype_count,
662 &uc_deftype_count);
663
664 const int bios_mtrrs = total_mtrrs - get_os_reserved_mtrrs();
665 if (wb_deftype_count > bios_mtrrs && uc_deftype_count > bios_mtrrs) {
666 printk(BIOS_DEBUG, "MTRR: Removing WRCOMB type. "
667 "WB/UC MTRR counts: %d/%d > %d.\n",
668 wb_deftype_count, uc_deftype_count, bios_mtrrs);
669 memranges_update_tag(addr_space, MTRR_TYPE_WRCOMB,
670 MTRR_TYPE_UNCACHEABLE);
671 __calc_var_mtrrs(addr_space, above4gb, address_bits,
672 &wb_deftype_count, &uc_deftype_count);
673 }
674
675 printk(BIOS_DEBUG, "MTRR: default type WB/UC MTRR counts: %d/%d.\n",
676 wb_deftype_count, uc_deftype_count);
677
678 if (wb_deftype_count < uc_deftype_count) {
679 printk(BIOS_DEBUG, "MTRR: WB selected as default type.\n");
680 *num_mtrrs_used = wb_deftype_count;
681 return MTRR_TYPE_WRBACK;
682 }
683 printk(BIOS_DEBUG, "MTRR: UC selected as default type.\n");
684 *num_mtrrs_used = uc_deftype_count;
685 return MTRR_TYPE_UNCACHEABLE;
686 }
687
688 static void prepare_var_mtrrs(struct memranges *addr_space, int def_type,
689 bool above4gb, int address_bits,
690 struct var_mtrr_solution *sol)
691 {
692 struct range_entry *r;
693 struct var_mtrr_state var_state;
694
695 var_state.addr_space = addr_space;
696 var_state.above4gb = above4gb;
697 var_state.address_bits = address_bits;
698 /* Prepare the MSRs. */
699 var_state.prepare_msrs = 1;
700 var_state.mtrr_index = 0;
701 var_state.def_mtrr_type = def_type;
702 var_state.regs = &sol->regs[0];
703
704 memranges_each_entry(r, var_state.addr_space) {
705 if (range_entry_mtrr_type(r) == def_type)
706 continue;
707 calc_var_mtrrs_with_hole(&var_state, r);
708 }
709
710 /* Update the solution. */
711 sol->num_used = var_state.mtrr_index;
712 }
713
714 static int commit_var_mtrrs(const struct var_mtrr_solution *sol)
715 {
716 int i;
717
718 if (sol->num_used > total_mtrrs) {
719 printk(BIOS_WARNING, "Not enough MTRRs: %d vs %d\n",
720 sol->num_used, total_mtrrs);
721 return -1;
722 }
723
724 /* Write out the variable MTRRs. */
725 disable_cache();
726 for (i = 0; i < sol->num_used; i++) {
727 wrmsr(MTRR_PHYS_BASE(i), sol->regs[i].base);
728 wrmsr(MTRR_PHYS_MASK(i), sol->regs[i].mask);
729 }
730 /* Clear the ones that are unused. */
731 for (; i < total_mtrrs; i++)
732 clear_var_mtrr(i);
733 enable_var_mtrr(sol->mtrr_default_type);
734 enable_cache();
735
736 return 0;
737 }
738
739 void x86_setup_var_mtrrs(unsigned int address_bits, bool above4gb)
740 {
741 static struct var_mtrr_solution *sol = NULL;
742 struct memranges *addr_space;
743 int num_mtrrs_used;
744
745 addr_space = get_physical_address_space();
746
747 if (sol == NULL) {
748 sol = &mtrr_global_solution;
749 sol->mtrr_default_type =
750 calc_var_mtrrs(addr_space, above4gb, address_bits, &num_mtrrs_used);
751 prepare_var_mtrrs(addr_space, sol->mtrr_default_type,
752 above4gb, address_bits, sol);
753 }
754
755 commit_var_mtrrs(sol);
756 }
757
758 static void _x86_setup_mtrrs(bool above4gb)
759 {
760 int address_size;
761
762 enable_lapic();
763
764 x86_setup_fixed_mtrrs();
765 address_size = cpu_phys_address_size();
766 printk(BIOS_DEBUG, "apic_id 0x%x setup mtrr for CPU physical address size: %d bits\n",
767 lapicid(), address_size);
768 x86_setup_var_mtrrs(address_size, above4gb);
769 }
770
771 void x86_setup_mtrrs(void)
772 {
773 /* Without detect, assume the minimum */
774 total_mtrrs = MIN_MTRRS;
775 /* Always handle addresses above 4GiB. */
776 _x86_setup_mtrrs(true);
777 }
778
779 void x86_setup_mtrrs_with_detect(void)
780 {
781 detect_var_mtrrs();
782 /* Always handle addresses above 4GiB. */
783 _x86_setup_mtrrs(true);
784 }
785
786 void x86_setup_mtrrs_with_detect_no_above_4gb(void)
787 {
788 detect_var_mtrrs();
789 /* Ignore addresses above 4GiB. */
790 _x86_setup_mtrrs(false);
791 }
792
793 void x86_mtrr_check(void)
794 {
795 /* Only Pentium Pro and later have MTRR */
796 msr_t msr;
797 printk(BIOS_DEBUG, "\nMTRR check\n");
798
799 msr = rdmsr(MTRR_DEF_TYPE_MSR);
800
801 printk(BIOS_DEBUG, "Fixed MTRRs : ");
802 if (msr.lo & MTRR_DEF_TYPE_FIX_EN)
803 printk(BIOS_DEBUG, "Enabled\n");
804 else
805 printk(BIOS_DEBUG, "Disabled\n");
806
807 printk(BIOS_DEBUG, "Variable MTRRs: ");
808 if (msr.lo & MTRR_DEF_TYPE_EN)
809 printk(BIOS_DEBUG, "Enabled\n");
810 else
811 printk(BIOS_DEBUG, "Disabled\n");
812
813 printk(BIOS_DEBUG, "\n");
814
815 post_code(0x93);
816 }
817
818 static bool put_back_original_solution;
819
820 void mtrr_use_temp_range(uintptr_t begin, size_t size, int type)
821 {
822 const struct range_entry *r;
823 const struct memranges *orig;
824 struct var_mtrr_solution sol;
825 struct memranges addr_space;
826 bool above4gb = true; /* Cover above 4GiB by default. */
827 int address_bits;
828 int num_mtrrs_used;
829 static struct temp_range {
830 uintptr_t begin;
831 size_t size;
832 int type;
833 } temp_ranges[10];
834
835 if (size == 0)
836 return;
837
838 int i;
839 for (i = 0; i < ARRAY_SIZE(temp_ranges); i++) {
840 if (temp_ranges[i].size == 0) {
841 temp_ranges[i].begin = begin;
842 temp_ranges[i].size = size;
843 temp_ranges[i].type = type;
844 break;
845 }
846 }
847 if (i == ARRAY_SIZE(temp_ranges)) {
848 printk(BIOS_ERR, "Out of temporary ranges for MTRR use\n");
849 return;
850 }
851
852 /* Make a copy of the original address space and tweak it with the
853 * provided range. */
854 memranges_init_empty(&addr_space, NULL, 0);
855 orig = get_physical_address_space();
856 memranges_each_entry(r, orig) {
857 unsigned long tag = range_entry_tag(r);
858
859 /* Remove any write combining MTRRs from the temporary
860 * solution as it just fragments the address space. */
861 if (tag == MTRR_TYPE_WRCOMB)
862 tag = MTRR_TYPE_UNCACHEABLE;
863
864 memranges_insert(&addr_space, range_entry_base(r),
865 range_entry_size(r), tag);
866 }
867
868 /* Place new range into the address space. */
869 for (i = 0; i < ARRAY_SIZE(temp_ranges); i++) {
870 if (temp_ranges[i].size != 0)
871 memranges_insert(&addr_space, temp_ranges[i].begin,
872 temp_ranges[i].size, temp_ranges[i].type);
873 }
874
875 print_physical_address_space(&addr_space, "TEMPORARY");
876
877 /* Calculate a new solution with the updated address space. */
878 address_bits = cpu_phys_address_size();
879 memset(&sol, 0, sizeof(sol));
880 sol.mtrr_default_type =
881 calc_var_mtrrs(&addr_space, above4gb, address_bits, &num_mtrrs_used);
882
883 /* If we ran out of MTRRs, retry excluding ranges above 4GiB */
884 if (above4gb && num_mtrrs_used > total_mtrrs) {
885 printk(BIOS_WARNING, "MTRR: Ran out of variable MTRRs; retrying excluding ranges above 4GiB.\n");
886 above4gb = false;
887 sol.mtrr_default_type = calc_var_mtrrs(&addr_space, above4gb, address_bits, &num_mtrrs_used);
888 }
889 if (num_mtrrs_used <= total_mtrrs)
890 prepare_var_mtrrs(&addr_space, sol.mtrr_default_type, above4gb, address_bits, &sol);
891 else
892 printk(BIOS_ERR, "Not enough MTRRs: %d needed vs %d available\n", num_mtrrs_used, total_mtrrs);
893
894 if (num_mtrrs_used > total_mtrrs || commit_var_mtrrs(&sol) < 0)
895 printk(BIOS_ERR, "Unable to insert temporary MTRR range: 0x%016llx - 0x%016llx size 0x%08llx type %d\n",
896 (long long)begin, (long long)begin + size - 1, (long long)size, type);
897 else
898 put_back_original_solution = true;
899
900 memranges_teardown(&addr_space);
901 }
902
903 static void remove_temp_solution(void *unused)
904 {
905 if (put_back_original_solution)
906 commit_var_mtrrs(&mtrr_global_solution);
907 }
908
909 BOOT_STATE_INIT_ENTRY(BS_OS_RESUME, BS_ON_ENTRY, remove_temp_solution, NULL);
910 BOOT_STATE_INIT_ENTRY(BS_PAYLOAD_BOOT, BS_ON_ENTRY, remove_temp_solution, NULL);