Commit f5098d62 authored by Yinghai Lu's avatar Yinghai Lu Committed by Thomas Gleixner

x86: mtrr cleanup for converting continuous to discrete layout v8 - fix

v9: address format change requests by Ingo
    more case handling in range_to_var_with_hole
Signed-off-by: default avatarYinghai Lu <yhlu.kernel@gmail.com>
Signed-off-by: default avatarIngo Molnar <mingo@elte.hu>
Signed-off-by: default avatarThomas Gleixner <tglx@linutronix.de>
parent 8a374026
...@@ -1110,13 +1110,12 @@ config MTRR_SANITIZER ...@@ -1110,13 +1110,12 @@ config MTRR_SANITIZER
If unsure, say Y. If unsure, say Y.
config MTRR_SANITIZER_ENABLE_DEFAULT config MTRR_SANITIZER_ENABLE_DEFAULT
def_bool y int "MTRR cleanup enable value (0-1)"
prompt "Enable MTRR cleanup by default" range 0 1
default "0"
depends on MTRR_SANITIZER depends on MTRR_SANITIZER
help help
Enable mtrr cleanup by default Enable mtrr cleanup default value
If unsure, say Y.
config X86_PAT config X86_PAT
bool bool
......
...@@ -611,17 +611,9 @@ static struct sysdev_driver mtrr_sysdev_driver = { ...@@ -611,17 +611,9 @@ static struct sysdev_driver mtrr_sysdev_driver = {
}; };
#ifdef CONFIG_MTRR_SANITIZER #ifdef CONFIG_MTRR_SANITIZER
static int enable_mtrr_cleanup __initdata = CONFIG_MTRR_SANITIZER_ENABLE_DEFAULT;
#ifdef CONFIG_MTRR_SANITIZER_ENABLE_DEFAULT
static int enable_mtrr_cleanup __initdata = 1;
#else
static int enable_mtrr_cleanup __initdata;
#endif
#else #else
static int enable_mtrr_cleanup __initdata = -1; static int enable_mtrr_cleanup __initdata = -1;
#endif #endif
static int __init disable_mtrr_cleanup_setup(char *str) static int __init disable_mtrr_cleanup_setup(char *str)
...@@ -640,6 +632,7 @@ static int __init enable_mtrr_cleanup_setup(char *str) ...@@ -640,6 +632,7 @@ static int __init enable_mtrr_cleanup_setup(char *str)
} }
early_param("enble_mtrr_cleanup", enable_mtrr_cleanup_setup); early_param("enble_mtrr_cleanup", enable_mtrr_cleanup_setup);
/* should be related to MTRR_VAR_RANGES nums */
#define RANGE_NUM 256 #define RANGE_NUM 256
struct res_range { struct res_range {
...@@ -647,13 +640,27 @@ struct res_range { ...@@ -647,13 +640,27 @@ struct res_range {
unsigned long end; unsigned long end;
}; };
static int __init add_range(struct res_range *range, int nr_range, unsigned long start, static int __init
unsigned long end, int merge) add_range(struct res_range *range, int nr_range, unsigned long start,
unsigned long end)
{ {
int i; /* out of slots */
if (nr_range >= RANGE_NUM)
return nr_range;
if (!merge) range[nr_range].start = start;
goto addit; range[nr_range].end = end;
nr_range++;
return nr_range;
}
static int __init
add_range_with_merge(struct res_range *range, int nr_range, unsigned long start,
unsigned long end)
{
int i;
/* try to merge it with old one */ /* try to merge it with old one */
for (i = 0; i < nr_range; i++) { for (i = 0; i < nr_range; i++) {
...@@ -676,24 +683,14 @@ static int __init add_range(struct res_range *range, int nr_range, unsigned long ...@@ -676,24 +683,14 @@ static int __init add_range(struct res_range *range, int nr_range, unsigned long
return nr_range; return nr_range;
} }
addit:
/* need to add that */ /* need to add that */
if (nr_range >= RANGE_NUM) return add_range(range, nr_range, start, end);
return nr_range;
range[nr_range].start = start;
range[nr_range].end = end;
nr_range++;
return nr_range;
} }
static void __init subtract_range(struct res_range *range, unsigned long start,
unsigned long end) static void __init
subtract_range(struct res_range *range, unsigned long start, unsigned long end)
{ {
int i; int i, j;
int j;
for (j = 0; j < RANGE_NUM; j++) { for (j = 0; j < RANGE_NUM; j++) {
if (!range[j].end) if (!range[j].end)
...@@ -747,46 +744,47 @@ static int __init cmp_range(const void *x1, const void *x2) ...@@ -747,46 +744,47 @@ static int __init cmp_range(const void *x1, const void *x2)
} }
struct var_mtrr_state { struct var_mtrr_state {
unsigned long range_startk, range_sizek; unsigned long range_startk;
unsigned long range_sizek;
unsigned long chunk_sizek; unsigned long chunk_sizek;
unsigned long gran_sizek; unsigned long gran_sizek;
unsigned int reg; unsigned int reg;
unsigned address_bits; unsigned int address_bits;
}; };
static void __init set_var_mtrr( static void __init
unsigned int reg, unsigned long basek, unsigned long sizek, set_var_mtrr(unsigned int reg, unsigned long basek, unsigned long sizek,
unsigned char type, unsigned address_bits) unsigned char type, unsigned address_bits)
{ {
u32 base_lo, base_hi, mask_lo, mask_hi; u32 base_lo, base_hi, mask_lo, mask_hi;
unsigned address_mask_high; u64 base, mask;
if (!sizek) { if (!sizek) {
fill_mtrr_var_range(reg, 0, 0, 0, 0); fill_mtrr_var_range(reg, 0, 0, 0, 0);
return; return;
} }
address_mask_high = ((1u << (address_bits - 32u)) - 1u); mask = (1ULL << address_bits) - 1;
mask &= ~((((u64)sizek) << 10) - 1);
base_hi = basek >> 22; base = ((u64)basek) << 10;
base_lo = basek << 10;
if (sizek < 4*1024*1024) { base |= type;
mask_hi = address_mask_high; mask |= 0x800;
mask_lo = ~((sizek << 10) - 1);
} else { base_lo = base & ((1ULL<<32) - 1);
mask_hi = address_mask_high & (~((sizek >> 22) - 1)); base_hi = base >> 32;
mask_lo = 0;
} mask_lo = mask & ((1ULL<<32) - 1);
mask_hi = mask >> 32;
base_lo |= type;
mask_lo |= 0x800;
fill_mtrr_var_range(reg, base_lo, base_hi, mask_lo, mask_hi); fill_mtrr_var_range(reg, base_lo, base_hi, mask_lo, mask_hi);
} }
static unsigned int __init range_to_mtrr(unsigned int reg, static unsigned int __init
unsigned long range_startk, unsigned long range_sizek, range_to_mtrr(unsigned int reg, unsigned long range_startk,
unsigned char type, unsigned address_bits) unsigned long range_sizek, unsigned char type,
unsigned address_bits)
{ {
if (!range_sizek || (reg >= num_var_ranges)) if (!range_sizek || (reg >= num_var_ranges))
return reg; return reg;
...@@ -794,6 +792,7 @@ static unsigned int __init range_to_mtrr(unsigned int reg, ...@@ -794,6 +792,7 @@ static unsigned int __init range_to_mtrr(unsigned int reg,
while (range_sizek) { while (range_sizek) {
unsigned long max_align, align; unsigned long max_align, align;
unsigned long sizek; unsigned long sizek;
/* Compute the maximum size I can make a range */ /* Compute the maximum size I can make a range */
if (range_startk) if (range_startk)
max_align = ffs(range_startk) - 1; max_align = ffs(range_startk) - 1;
...@@ -818,7 +817,8 @@ static unsigned int __init range_to_mtrr(unsigned int reg, ...@@ -818,7 +817,8 @@ static unsigned int __init range_to_mtrr(unsigned int reg,
return reg; return reg;
} }
static void __init range_to_mtrr_with_hole(struct var_mtrr_state *state, unsigned long basek) static void __init
range_to_mtrr_with_hole(struct var_mtrr_state *state, unsigned long basek)
{ {
unsigned long hole_basek, hole_sizek; unsigned long hole_basek, hole_sizek;
unsigned long range0_basek, range0_sizek; unsigned long range0_basek, range0_sizek;
...@@ -848,15 +848,21 @@ static void __init range_to_mtrr_with_hole(struct var_mtrr_state *state, unsigne ...@@ -848,15 +848,21 @@ static void __init range_to_mtrr_with_hole(struct var_mtrr_state *state, unsigne
/* try to append some small hole */ /* try to append some small hole */
range0_basek = state->range_startk; range0_basek = state->range_startk;
range0_sizek = ALIGN(state->range_sizek, chunk_sizek); range0_sizek = ALIGN(state->range_sizek, chunk_sizek);
if ((range0_sizek == state->range_sizek) || if (range0_sizek == state->range_sizek) {
((range0_basek + range0_sizek - chunk_sizek > basek) && basek)) {
printk(KERN_INFO "rangeX: %016lx - %016lx\n", range0_basek<<10, (range0_basek + state->range_sizek)<<10); printk(KERN_INFO "rangeX: %016lx - %016lx\n", range0_basek<<10, (range0_basek + state->range_sizek)<<10);
state->reg = range_to_mtrr(state->reg, range0_basek, state->reg = range_to_mtrr(state->reg, range0_basek,
state->range_sizek, MTRR_TYPE_WRBACK, state->address_bits); state->range_sizek, MTRR_TYPE_WRBACK, state->address_bits);
return; return;
} else if (basek) {
while (range0_basek + range0_sizek - chunk_sizek > basek) {
range0_sizek -= chunk_sizek;
if (!range0_sizek)
break;
}
} }
if (range0_sizek > chunk_sizek)
range0_sizek -= chunk_sizek; range0_sizek -= chunk_sizek;
printk(KERN_INFO "range0: %016lx - %016lx\n", range0_basek<<10, (range0_basek + range0_sizek)<<10); printk(KERN_INFO "range0: %016lx - %016lx\n", range0_basek<<10, (range0_basek + range0_sizek)<<10);
state->reg = range_to_mtrr(state->reg, range0_basek, state->reg = range_to_mtrr(state->reg, range0_basek,
...@@ -864,7 +870,9 @@ static void __init range_to_mtrr_with_hole(struct var_mtrr_state *state, unsigne ...@@ -864,7 +870,9 @@ static void __init range_to_mtrr_with_hole(struct var_mtrr_state *state, unsigne
range_basek = range0_basek + range0_sizek; range_basek = range0_basek + range0_sizek;
range_sizek = chunk_sizek; range_sizek = chunk_sizek;
if (range_sizek - (state->range_sizek - range0_sizek) < (chunk_sizek >> 1)) {
if ((range_sizek - (state->range_sizek - range0_sizek) < (chunk_sizek >> 1)) &&
(range_basek + range_sizek <= basek)) {
hole_sizek = range_sizek - (state->range_sizek - range0_sizek); hole_sizek = range_sizek - (state->range_sizek - range0_sizek);
hole_basek = range_basek + range_sizek - hole_sizek; hole_basek = range_basek + range_sizek - hole_sizek;
} else } else
...@@ -880,7 +888,9 @@ static void __init range_to_mtrr_with_hole(struct var_mtrr_state *state, unsigne ...@@ -880,7 +888,9 @@ static void __init range_to_mtrr_with_hole(struct var_mtrr_state *state, unsigne
} }
} }
static void __init set_var_mtrr_range(struct var_mtrr_state *state, unsigned long base_pfn, unsigned long size_pfn) static void __init
set_var_mtrr_range(struct var_mtrr_state *state, unsigned long base_pfn,
unsigned long size_pfn)
{ {
unsigned long basek, sizek; unsigned long basek, sizek;
...@@ -921,7 +931,7 @@ static int __init parse_mtrr_chunk_size_opt(char *p) ...@@ -921,7 +931,7 @@ static int __init parse_mtrr_chunk_size_opt(char *p)
early_param("mtrr_chunk_size", parse_mtrr_chunk_size_opt); early_param("mtrr_chunk_size", parse_mtrr_chunk_size_opt);
/* granity of mtrr of block */ /* granity of mtrr of block */
static u64 mtrr_gran_size __initdata = (64ULL<<20); static u64 mtrr_gran_size __initdata = (1ULL<<20);
static int __init parse_mtrr_gran_size_opt(char *p) static int __init parse_mtrr_gran_size_opt(char *p)
{ {
...@@ -932,7 +942,9 @@ static int __init parse_mtrr_gran_size_opt(char *p) ...@@ -932,7 +942,9 @@ static int __init parse_mtrr_gran_size_opt(char *p)
} }
early_param("mtrr_gran_size", parse_mtrr_gran_size_opt); early_param("mtrr_gran_size", parse_mtrr_gran_size_opt);
static void __init x86_setup_var_mtrrs(struct res_range *range, int nr_range, unsigned address_bits) static void __init
x86_setup_var_mtrrs(struct res_range *range, int nr_range,
unsigned address_bits)
{ {
struct var_mtrr_state var_state; struct var_mtrr_state var_state;
int i; int i;
...@@ -952,11 +964,16 @@ static void __init x86_setup_var_mtrrs(struct res_range *range, int nr_range, un ...@@ -952,11 +964,16 @@ static void __init x86_setup_var_mtrrs(struct res_range *range, int nr_range, un
range_to_mtrr_with_hole(&var_state, 0); range_to_mtrr_with_hole(&var_state, 0);
printk(KERN_INFO "DONE variable MTRRs\n"); printk(KERN_INFO "DONE variable MTRRs\n");
/* Clear out the extra MTRR's */ /* Clear out the extra MTRR's */
while (var_state.reg < num_var_ranges) while (var_state.reg < num_var_ranges) {
set_var_mtrr(var_state.reg++, 0, 0, 0, var_state.address_bits); set_var_mtrr(var_state.reg, 0, 0, 0, var_state.address_bits);
var_state.reg++;
}
} }
static int __init x86_get_mtrr_mem_range(struct res_range *range, int nr_range, unsigned long extra_remove_base, unsigned long extra_remove_size) static int __init
x86_get_mtrr_mem_range(struct res_range *range, int nr_range,
unsigned long extra_remove_base,
unsigned long extra_remove_size)
{ {
unsigned long i, base, size; unsigned long i, base, size;
mtrr_type type; mtrr_type type;
...@@ -965,7 +982,7 @@ static int __init x86_get_mtrr_mem_range(struct res_range *range, int nr_range, ...@@ -965,7 +982,7 @@ static int __init x86_get_mtrr_mem_range(struct res_range *range, int nr_range,
mtrr_if->get(i, &base, &size, &type); mtrr_if->get(i, &base, &size, &type);
if (type != MTRR_TYPE_WRBACK) if (type != MTRR_TYPE_WRBACK)
continue; continue;
nr_range = add_range(range, nr_range, base, base + size - 1, 1); nr_range = add_range_with_merge(range, nr_range, base, base + size - 1);
} }
printk(KERN_INFO "After WB checking\n"); printk(KERN_INFO "After WB checking\n");
for (i = 0; i < nr_range; i++) for (i = 0; i < nr_range; i++)
...@@ -1005,11 +1022,11 @@ static int __init x86_get_mtrr_mem_range(struct res_range *range, int nr_range, ...@@ -1005,11 +1022,11 @@ static int __init x86_get_mtrr_mem_range(struct res_range *range, int nr_range,
static int __init mtrr_cleanup(unsigned address_bits) static int __init mtrr_cleanup(unsigned address_bits)
{ {
unsigned long extra_remove_base, extra_remove_size;
unsigned long i, base, size, def, dummy; unsigned long i, base, size, def, dummy;
mtrr_type type;
struct res_range range[RANGE_NUM]; struct res_range range[RANGE_NUM];
mtrr_type type;
int nr_range; int nr_range;
unsigned long extra_remove_base, extra_remove_size;
/* extra one for all 0 */ /* extra one for all 0 */
int num[MTRR_NUM_TYPES + 1]; int num[MTRR_NUM_TYPES + 1];
...@@ -1053,7 +1070,6 @@ static int __init mtrr_cleanup(unsigned address_bits) ...@@ -1053,7 +1070,6 @@ static int __init mtrr_cleanup(unsigned address_bits)
x86_setup_var_mtrrs(range, nr_range, address_bits); x86_setup_var_mtrrs(range, nr_range, address_bits);
return 1; return 1;
} }
static int disable_mtrr_trim; static int disable_mtrr_trim;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment