20 #include <linux/module.h>
22 #include <linux/pci.h>
27 #include <linux/kvm_para.h>
30 #include <asm/processor.h>
60 #define Dprintk(x...) do { if (debug_print) printk(KERN_DEBUG x); } while (0)
62 #define BIOS_BUG_MSG KERN_WARNING \
63 "WARNING: BIOS bug: VAR MTRR %d contains strange UC entry under 1M, check with your system vendor!\n"
66 x86_get_mtrr_mem_range(
struct range *
range,
int nr_range,
67 unsigned long extra_remove_base,
68 unsigned long extra_remove_size)
75 type = range_state[
i].type;
78 base = range_state[
i].base_pfn;
79 size = range_state[
i].size_pfn;
85 for (i = 0; i < nr_range; i++)
92 type = range_state[
i].type;
96 size = range_state[
i].size_pfn;
99 base = range_state[
i].base_pfn;
111 if (extra_remove_size)
113 extra_remove_base + extra_remove_size);
121 range[i].
start, range[i].end);
129 for (i = 0; i < nr_range; i++)
137 #ifdef CONFIG_MTRR_SANITIZER
141 unsigned long sum = 0;
144 for (i = 0; i < nr_range; i++)
145 sum += range[i].
end - range[i].
start;
151 CONFIG_MTRR_SANITIZER_ENABLE_DEFAULT;
153 static int __init disable_mtrr_cleanup_setup(
char *
str)
155 enable_mtrr_cleanup = 0;
158 early_param(
"disable_mtrr_cleanup", disable_mtrr_cleanup_setup);
160 static int __init enable_mtrr_cleanup_setup(
char *
str)
162 enable_mtrr_cleanup = 1;
165 early_param(
"enable_mtrr_cleanup", enable_mtrr_cleanup_setup);
167 static int __init mtrr_cleanup_debug_setup(
char *
str)
172 early_param(
"mtrr_cleanup_debug", mtrr_cleanup_debug_setup);
175 set_var_mtrr(
unsigned int reg,
unsigned long basek,
unsigned long sizek,
176 unsigned char type,
unsigned int address_bits)
178 u32 base_lo, base_hi, mask_lo, mask_hi;
186 mask = (1ULL << address_bits) - 1;
187 mask &= ~((((
u64)sizek) << 10) - 1);
189 base = ((
u64)basek) << 10;
194 base_lo = base & ((1ULL<<32) - 1);
195 base_hi = base >> 32;
197 mask_lo = mask & ((1ULL<<32) - 1);
198 mask_hi =
mask >> 32;
204 save_var_mtrr(
unsigned int reg,
unsigned long basek,
unsigned long sizek,
212 static void __init set_var_mtrr_all(
unsigned int address_bits)
214 unsigned long basek, sizek;
221 type = range_state[
reg].type;
223 set_var_mtrr(reg, basek, sizek, type, address_bits);
227 static unsigned long to_size_factor(
unsigned long sizek,
char *factorp)
229 unsigned long base = sizek;
232 if (base & ((1<<10) - 1)) {
235 }
else if (base & ((1<<20) - 1)) {
248 static unsigned int __init
249 range_to_mtrr(
unsigned int reg,
unsigned long range_startk,
250 unsigned long range_sizek,
unsigned char type)
255 while (range_sizek) {
256 unsigned long max_align,
align;
261 max_align =
__ffs(range_startk);
265 align =
__fls(range_sizek);
266 if (align > max_align)
271 char start_factor =
'K', size_factor =
'K';
272 unsigned long start_base, size_base;
274 start_base = to_size_factor(range_startk, &start_factor);
275 size_base = to_size_factor(sizek, &size_factor);
277 Dprintk(
"Setting variable MTRR %d, "
278 "base: %ld%cB, range: %ld%cB, type %s\n",
279 reg, start_base, start_factor,
280 size_base, size_factor,
285 save_var_mtrr(reg++, range_startk, sizek, type);
286 range_startk += sizek;
287 range_sizek -= sizek;
298 unsigned long hole_basek, hole_sizek;
299 unsigned long second_basek, second_sizek;
300 unsigned long range0_basek, range0_sizek;
301 unsigned long range_basek, range_sizek;
302 unsigned long chunk_sizek;
303 unsigned long gran_sizek;
314 if ((range_basek > basek) && basek)
321 range_sizek -= gran_sizek;
333 Dprintk(
"rangeX: %016lx - %016lx\n",
336 state->
reg = range_to_mtrr(state->
reg, range0_basek,
343 while (range0_basek + range0_sizek > (basek + sizek)) {
344 if (range0_sizek >= chunk_sizek)
345 range0_sizek -= chunk_sizek;
355 range_basek = range0_basek + range0_sizek;
358 if (range_basek > basek && range_basek <= (basek + sizek))
359 second_sizek = range_basek - basek;
364 hole_sizek = range0_sizek - state->
range_sizek - second_sizek;
367 if (hole_sizek >= (range0_sizek >> 1) &&
368 range0_sizek >= chunk_sizek) {
369 range0_sizek -= chunk_sizek;
378 Dprintk(
"range0: %016lx - %016lx\n",
380 (range0_basek + range0_sizek)<<10);
381 state->
reg = range_to_mtrr(state->
reg, range0_basek,
385 if (range0_sizek < state->range_sizek) {
389 Dprintk(
"range: %016lx - %016lx\n",
391 (range_basek + range_sizek)<<10);
393 state->
reg = range_to_mtrr(state->
reg, range_basek,
398 hole_basek = range_basek - hole_sizek - second_sizek;
399 Dprintk(
"hole: %016lx - %016lx\n",
401 (hole_basek + hole_sizek)<<10);
402 state->
reg = range_to_mtrr(state->
reg, hole_basek,
413 unsigned long basek, sizek;
414 unsigned long second_sizek = 0;
423 if ((basek <= 1024) ||
425 unsigned long endk = basek + sizek;
431 second_sizek = range_to_mtrr_with_hole(state, basek, sizek);
441 static int __init parse_mtrr_chunk_size_opt(
char *
p)
448 early_param(
"mtrr_chunk_size", parse_mtrr_chunk_size_opt);
453 static int __init parse_mtrr_gran_size_opt(
char *p)
460 early_param(
"mtrr_gran_size", parse_mtrr_gran_size_opt);
462 static unsigned long nr_mtrr_spare_reg __initdata =
463 CONFIG_MTRR_SANITIZER_SPARE_REG_NR_DEFAULT;
465 static int __init parse_mtrr_spare_reg(
char *
arg)
471 early_param(
"mtrr_spare_reg_nr", parse_mtrr_spare_reg);
474 x86_setup_var_mtrrs(
struct range *
range,
int nr_range,
475 u64 chunk_size,
u64 gran_size)
481 var_state.range_startk = 0;
482 var_state.range_sizek = 0;
484 var_state.chunk_sizek = chunk_size >> 10;
485 var_state.gran_sizek = gran_size >> 10;
487 memset(range_state, 0,
sizeof(range_state));
490 for (i = 0; i < nr_range; i++) {
491 set_var_mtrr_range(&var_state, range[i].
start,
492 range[i].
end - range[i].start);
496 if (var_state.range_sizek != 0)
497 range_to_mtrr_with_hole(&var_state, 0, 0);
499 num_reg = var_state.reg;
502 save_var_mtrr(var_state.reg, 0, 0, 0);
509 struct mtrr_cleanup_result {
510 unsigned long gran_sizek;
511 unsigned long chunk_sizek;
512 unsigned long lose_cover_sizek;
513 unsigned int num_reg;
522 #define NUM_RESULT 136
523 #define PSHIFT (PAGE_SHIFT - 10)
525 static struct mtrr_cleanup_result __initdata
result[NUM_RESULT];
526 static unsigned long __initdata min_loss_pfn[
RANGE_NUM];
528 static void __init print_out_mtrr_range_state(
void)
530 char start_factor =
'K', size_factor =
'K';
531 unsigned long start_base, size_base;
537 size_base = range_state[
i].size_pfn << (
PAGE_SHIFT - 10);
541 size_base = to_size_factor(size_base, &size_factor),
542 start_base = range_state[
i].base_pfn << (
PAGE_SHIFT - 10);
543 start_base = to_size_factor(start_base, &start_factor),
544 type = range_state[
i].type;
547 i, start_base, start_factor,
548 size_base, size_factor,
556 static int __init mtrr_need_cleanup(
void)
565 memset(num, 0,
sizeof(num));
567 type = range_state[
i].type;
568 size = range_state[
i].size_pfn;
588 static unsigned long __initdata range_sums;
591 mtrr_calc_range_state(
u64 chunk_size,
u64 gran_size,
592 unsigned long x_remove_base,
593 unsigned long x_remove_size,
int i)
595 static struct range range_new[
RANGE_NUM];
596 unsigned long range_sums_new;
597 static int nr_range_new;
601 num_reg = x86_setup_var_mtrrs(range, nr_range, chunk_size, gran_size);
604 memset(range_new, 0,
sizeof(range_new));
605 nr_range_new = x86_get_mtrr_mem_range(range_new, 0,
606 x_remove_base, x_remove_size);
607 range_sums_new = sum_ranges(range_new, nr_range_new);
609 result[
i].chunk_sizek = chunk_size >> 10;
610 result[
i].gran_sizek = gran_size >> 10;
613 if (range_sums < range_sums_new) {
614 result[
i].lose_cover_sizek = (range_sums_new - range_sums) << PSHIFT;
617 result[
i].lose_cover_sizek = (range_sums - range_sums_new) << PSHIFT;
622 if (nr_range_new != nr_range ||
memcmp(range, range_new,
sizeof(range)))
626 if (!
result[i].
bad && (range_sums - range_sums_new < min_loss_pfn[num_reg]))
627 min_loss_pfn[num_reg] = range_sums - range_sums_new;
630 static void __init mtrr_print_out_one_result(
int i)
632 unsigned long gran_base, chunk_base, lose_base;
633 char gran_factor, chunk_factor, lose_factor;
635 gran_base = to_size_factor(
result[i].gran_sizek, &gran_factor);
636 chunk_base = to_size_factor(
result[i].chunk_sizek, &chunk_factor);
637 lose_base = to_size_factor(
result[i].lose_cover_sizek, &lose_factor);
639 pr_info(
"%sgran_size: %ld%c \tchunk_size: %ld%c \t",
641 gran_base, gran_factor, chunk_base, chunk_factor);
642 pr_cont(
"num_reg: %d \tlose cover RAM: %s%ld%c\n",
644 lose_base, lose_factor);
647 static int __init mtrr_search_optimal_index(
void)
653 if (nr_mtrr_spare_reg >= num_var_ranges)
654 nr_mtrr_spare_reg = num_var_ranges - 1;
657 for (i = num_var_ranges - nr_mtrr_spare_reg; i > 0; i--) {
658 if (!min_loss_pfn[i])
663 if (num_reg_good != -1) {
664 for (i = 0; i < NUM_RESULT; i++) {
666 result[i].num_reg == num_reg_good &&
667 !
result[i].lose_cover_sizek) {
679 unsigned long x_remove_base, x_remove_size;
681 u64 chunk_size, gran_size;
691 if (def != MTRR_TYPE_UNCACHABLE)
695 memset(range_state, 0,
sizeof(range_state));
697 mtrr_if->get(i, &base, &size, &type);
698 range_state[
i].base_pfn = base;
699 range_state[
i].size_pfn =
size;
700 range_state[
i].type =
type;
704 if (!mtrr_need_cleanup())
709 print_out_mtrr_range_state();
711 memset(range, 0,
sizeof(range));
717 nr_range = x86_get_mtrr_mem_range(range, 0, x_remove_base, x_remove_size);
727 range_sums = sum_ranges(range, nr_range);
731 if (mtrr_chunk_size && mtrr_gran_size) {
733 mtrr_calc_range_state(mtrr_chunk_size, mtrr_gran_size,
734 x_remove_base, x_remove_size, i);
736 mtrr_print_out_one_result(i);
739 set_var_mtrr_all(address_bits);
741 print_out_mtrr_range_state();
745 "will find optimal one\n");
749 memset(min_loss_pfn, 0xff,
sizeof(min_loss_pfn));
751 for (gran_size = (1ULL<<16); gran_size < (1ULL<<32); gran_size <<= 1) {
753 for (chunk_size = gran_size; chunk_size < (1ULL<<32);
759 mtrr_calc_range_state(chunk_size, gran_size,
760 x_remove_base, x_remove_size, i);
762 mtrr_print_out_one_result(i);
771 index_good = mtrr_search_optimal_index();
773 if (index_good != -1) {
776 mtrr_print_out_one_result(i);
779 chunk_size =
result[
i].chunk_sizek;
781 gran_size =
result[
i].gran_sizek;
783 x86_setup_var_mtrrs(range, nr_range, chunk_size, gran_size);
784 set_var_mtrr_all(address_bits);
786 print_out_mtrr_range_state();
790 for (i = 0; i < NUM_RESULT; i++)
791 mtrr_print_out_one_result(i);
806 static int disable_mtrr_trim;
808 static int __init disable_mtrr_trim_setup(
char *
str)
810 disable_mtrr_trim = 1;
813 early_param(
"disable_mtrr_trim", disable_mtrr_trim_setup);
821 #define Tom2Enabled (1U << 21)
822 #define Tom2ForceMemTypeWB (1U << 22)
846 real_trim_memory(
unsigned long start_pfn,
unsigned long limit_pfn)
848 u64 trim_start, trim_size;
850 trim_start = start_pfn;
853 trim_size = limit_pfn;
855 trim_size -= trim_start;
873 unsigned long i, base,
size, highest_pfn = 0, def,
dummy;
888 if (def != MTRR_TYPE_UNCACHABLE)
892 memset(range_state, 0,
sizeof(range_state));
894 mtrr_if->get(i, &base, &size, &type);
895 range_state[
i].base_pfn = base;
896 range_state[
i].size_pfn =
size;
897 range_state[
i].type =
type;
902 type = range_state[
i].type;
905 base = range_state[
i].base_pfn;
906 size = range_state[
i].size_pfn;
907 if (highest_pfn < base + size)
908 highest_pfn = base +
size;
918 memset(num, 0,
sizeof(num));
920 type = range_state[
i].type;
923 size = range_state[
i].size_pfn;
934 if (num[MTRR_TYPE_WRBACK] + num[MTRR_TYPE_UNCACHABLE] !=
938 memset(range, 0,
sizeof(range));
943 if (highest_pfn < range[nr_range].
end)
944 highest_pfn = range[nr_range].
end;
947 nr_range = x86_get_mtrr_mem_range(range, nr_range, 0, 0);
952 total_trim_size += real_trim_memory(0, range[0].start);
955 for (i = 0; i < nr_range - 1; i++) {
956 if (range[i].
end < range[i+1].start)
957 total_trim_size += real_trim_memory(range[i].
end,
963 if (range[i].
end < end_pfn)
964 total_trim_size += real_trim_memory(range[i].
end,
967 if (total_trim_size) {
968 pr_warning(
"WARNING: BIOS bug: CPU MTRRs don't cover all of memory, losing %lluMB of RAM.\n", total_trim_size >> 20);
973 pr_info(
"update e820 for mtrr\n");