Commit 10fac2dc authored by Xiao Guangrong's avatar Xiao Guangrong Committed by Paolo Bonzini

KVM: MTRR: clean up mtrr default type

Drop kvm_mtrr->enable, omit the decode/code workload and get rid of
all the hard code
Signed-off-by: default avatarXiao Guangrong <guangrong.xiao@linux.intel.com>
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent 910a6aae
...@@ -345,8 +345,7 @@ enum { ...@@ -345,8 +345,7 @@ enum {
struct kvm_mtrr { struct kvm_mtrr {
struct mtrr_var_range var_ranges[KVM_NR_VAR_MTRR]; struct mtrr_var_range var_ranges[KVM_NR_VAR_MTRR];
mtrr_type fixed_ranges[KVM_NR_FIXED_MTRR_REGION]; mtrr_type fixed_ranges[KVM_NR_FIXED_MTRR_REGION];
unsigned char enabled; u64 deftype;
mtrr_type def_type;
}; };
struct kvm_vcpu_arch { struct kvm_vcpu_arch {
......
...@@ -22,6 +22,10 @@ ...@@ -22,6 +22,10 @@
#include "cpuid.h" #include "cpuid.h"
#include "mmu.h" #include "mmu.h"
#define IA32_MTRR_DEF_TYPE_E (1ULL << 11)
#define IA32_MTRR_DEF_TYPE_FE (1ULL << 10)
#define IA32_MTRR_DEF_TYPE_TYPE_MASK (0xff)
static bool msr_mtrr_valid(unsigned msr) static bool msr_mtrr_valid(unsigned msr)
{ {
switch (msr) { switch (msr) {
...@@ -101,10 +105,24 @@ bool kvm_mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data) ...@@ -101,10 +105,24 @@ bool kvm_mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data)
} }
EXPORT_SYMBOL_GPL(kvm_mtrr_valid); EXPORT_SYMBOL_GPL(kvm_mtrr_valid);
static bool mtrr_is_enabled(struct kvm_mtrr *mtrr_state)
{
return !!(mtrr_state->deftype & IA32_MTRR_DEF_TYPE_E);
}
static bool fixed_mtrr_is_enabled(struct kvm_mtrr *mtrr_state)
{
return !!(mtrr_state->deftype & IA32_MTRR_DEF_TYPE_FE);
}
static u8 mtrr_default_type(struct kvm_mtrr *mtrr_state)
{
return mtrr_state->deftype & IA32_MTRR_DEF_TYPE_TYPE_MASK;
}
static void update_mtrr(struct kvm_vcpu *vcpu, u32 msr) static void update_mtrr(struct kvm_vcpu *vcpu, u32 msr)
{ {
struct kvm_mtrr *mtrr_state = &vcpu->arch.mtrr_state; struct kvm_mtrr *mtrr_state = &vcpu->arch.mtrr_state;
unsigned char mtrr_enabled = mtrr_state->enabled;
gfn_t start, end, mask; gfn_t start, end, mask;
int index; int index;
bool is_fixed = true; bool is_fixed = true;
...@@ -113,7 +131,7 @@ static void update_mtrr(struct kvm_vcpu *vcpu, u32 msr) ...@@ -113,7 +131,7 @@ static void update_mtrr(struct kvm_vcpu *vcpu, u32 msr)
!kvm_arch_has_noncoherent_dma(vcpu->kvm)) !kvm_arch_has_noncoherent_dma(vcpu->kvm))
return; return;
if (!(mtrr_enabled & 0x2) && msr != MSR_MTRRdefType) if (!mtrr_is_enabled(mtrr_state) && msr != MSR_MTRRdefType)
return; return;
switch (msr) { switch (msr) {
...@@ -152,7 +170,7 @@ static void update_mtrr(struct kvm_vcpu *vcpu, u32 msr) ...@@ -152,7 +170,7 @@ static void update_mtrr(struct kvm_vcpu *vcpu, u32 msr)
end = ((start & mask) | ~mask) + 1; end = ((start & mask) | ~mask) + 1;
} }
if (is_fixed && !(mtrr_enabled & 0x1)) if (is_fixed && !fixed_mtrr_is_enabled(mtrr_state))
return; return;
kvm_zap_gfn_range(vcpu->kvm, gpa_to_gfn(start), gpa_to_gfn(end)); kvm_zap_gfn_range(vcpu->kvm, gpa_to_gfn(start), gpa_to_gfn(end));
...@@ -165,10 +183,9 @@ int kvm_mtrr_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data) ...@@ -165,10 +183,9 @@ int kvm_mtrr_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
if (!kvm_mtrr_valid(vcpu, msr, data)) if (!kvm_mtrr_valid(vcpu, msr, data))
return 1; return 1;
if (msr == MSR_MTRRdefType) { if (msr == MSR_MTRRdefType)
vcpu->arch.mtrr_state.def_type = data; vcpu->arch.mtrr_state.deftype = data;
vcpu->arch.mtrr_state.enabled = (data & 0xc00) >> 10; else if (msr == MSR_MTRRfix64K_00000)
} else if (msr == MSR_MTRRfix64K_00000)
p[0] = data; p[0] = data;
else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000) else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
p[1 + msr - MSR_MTRRfix16K_80000] = data; p[1 + msr - MSR_MTRRfix16K_80000] = data;
...@@ -215,8 +232,7 @@ int kvm_mtrr_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) ...@@ -215,8 +232,7 @@ int kvm_mtrr_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
return 1; return 1;
if (msr == MSR_MTRRdefType) if (msr == MSR_MTRRdefType)
*pdata = vcpu->arch.mtrr_state.def_type + *pdata = vcpu->arch.mtrr_state.deftype;
(vcpu->arch.mtrr_state.enabled << 10);
else if (msr == MSR_MTRRfix64K_00000) else if (msr == MSR_MTRRfix64K_00000)
*pdata = p[0]; *pdata = p[0];
else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000) else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
...@@ -255,14 +271,14 @@ static int get_mtrr_type(struct kvm_mtrr *mtrr_state, ...@@ -255,14 +271,14 @@ static int get_mtrr_type(struct kvm_mtrr *mtrr_state,
int i, num_var_ranges = KVM_NR_VAR_MTRR; int i, num_var_ranges = KVM_NR_VAR_MTRR;
/* MTRR is completely disabled, use UC for all of physical memory. */ /* MTRR is completely disabled, use UC for all of physical memory. */
if (!(mtrr_state->enabled & 0x2)) if (!mtrr_is_enabled(mtrr_state))
return MTRR_TYPE_UNCACHABLE; return MTRR_TYPE_UNCACHABLE;
/* Make end inclusive end, instead of exclusive */ /* Make end inclusive end, instead of exclusive */
end--; end--;
/* Look in fixed ranges. Just return the type as per start */ /* Look in fixed ranges. Just return the type as per start */
if ((mtrr_state->enabled & 0x1) && (start < 0x100000)) { if (fixed_mtrr_is_enabled(mtrr_state) && (start < 0x100000)) {
int idx; int idx;
if (start < 0x80000) { if (start < 0x80000) {
...@@ -330,7 +346,7 @@ static int get_mtrr_type(struct kvm_mtrr *mtrr_state, ...@@ -330,7 +346,7 @@ static int get_mtrr_type(struct kvm_mtrr *mtrr_state,
if (prev_match != 0xFF) if (prev_match != 0xFF)
return prev_match; return prev_match;
return mtrr_state->def_type; return mtrr_default_type(mtrr_state);
} }
u8 kvm_mtrr_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn) u8 kvm_mtrr_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
......
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