diff mbox series

[RFC,21/26] KVM: VMX: Extend HFI table and MSR emulation to support ITD

Message ID 20240203091214.411862-22-zhao1.liu@linux.intel.com (mailing list archive)
State New, archived
Headers show
Series Intel Thread Director Virtualization | expand

Commit Message

Zhao Liu Feb. 3, 2024, 9:12 a.m. UTC
From: Zhao Liu <zhao1.liu@intel.com>

ITD (Intel Thread Director) is the extension of HFI feature. Based on
HFI, it adds 4 classes in HFI table and it provides the MSR interface to
support the OS to classify the currently running task into one of 4
classes.

As the first step of ITD support, extend the HFI table and related HFI
MSRs' emulation to support the ITD:

* More classes in HFI table

If ITD is configured in Guest's CPUID, the virtual HFI table will be
built with 4 classes.

But only when ITD is enabled in MSR_IA32_HW_FEEDBACK_CONFIG, the
virtual HFI table will update all these 4 classes, otherwise it will
only update class 0's data if HFI is enabled.

* MSR_IA32_HW_FEEDBACK_CONFIG (HW_FEEDBACK_CONFIG_ITD_ENABLE bit)

With ITD support, MSR_IA32_HW_FEEDBACK_CONFIG has 2 feature enabling
bits: HW_FEEDBACK_CONFIG_HFI_ENABLE and HW_FEEDBACK_CONFIG_ITD_ENABLE
bit. These 2 bits control whether the HFI and ITD features are enabled
or not, and also affect which class data should actually be updated in
the virtual HFI table [1].

For the MSR_IA32_HW_FEEDBACK_CONFIG's emulation, add support for
dynamically changing these two bits and the corresponding HFI update
adjustments.

[1]: SDM, vol. 3B, section 15.6.5 Hardware Feedback Interface
     Configuration, Table 15-10. IA32_HW_FEEDBACK_CONFIG Control Option

Tested-by: Yanting Jiang <yanting.jiang@intel.com>
Co-developed-by: Zhuocheng Ding <zhuocheng.ding@intel.com>
Signed-off-by: Zhuocheng Ding <zhuocheng.ding@intel.com>
Signed-off-by: Zhao Liu <zhao1.liu@intel.com>
---
 arch/x86/kvm/vmx/vmx.c | 68 +++++++++++++++++++++++++++++++-----------
 arch/x86/kvm/vmx/vmx.h |  3 ++
 2 files changed, 54 insertions(+), 17 deletions(-)
diff mbox series

Patch

diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index 636f2bd68546..bdff1d424b2f 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -1547,11 +1547,11 @@  static int vmx_init_hfi_table(struct kvm *kvm)
 	struct hfi_table *hfi_table = &kvm_vmx_hfi->hfi_table;
 	int nr_classes, ret = 0;
 
-	/*
-	 * Currently we haven't supported ITD. HFI is the default feature
-	 * with 1 class.
-	 */
-	nr_classes = 1;
+	if (guest_cpuid_has(kvm_get_vcpu(kvm, 0), X86_FEATURE_ITD))
+		nr_classes = 4;
+	else
+		nr_classes = 1;
+
 	ret = intel_hfi_build_virt_features(hfi_features,
 					    nr_classes,
 					    kvm->created_vcpus);
@@ -1579,11 +1579,11 @@  static int vmx_build_hfi_table(struct kvm *kvm)
 	struct kvm_vcpu *v;
 	unsigned long i;
 
-	/*
-	 * Currently we haven't supported ITD. HFI is the default feature
-	 * with 1 class.
-	 */
-	nr_classes = 1;
+	if (kvm_vmx_hfi->itd_enabled)
+		nr_classes = kvm_vmx_hfi->hfi_features.nr_classes;
+	else
+		nr_classes = 1;
+
 	for (int j = 0; j < nr_classes; j++) {
 		hfi_hdr->perf_updated = 0;
 		hfi_hdr->ee_updated = 0;
@@ -2575,7 +2575,7 @@  static int vmx_set_hfi_cfg_msr(struct kvm_vcpu *vcpu,
 	struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
 	struct hfi_desc *kvm_vmx_hfi = &kvm_vmx->pkg_therm.hfi_desc;
 	u64 data = msr_info->data;
-	bool hfi_enabled, hfi_changed;
+	bool hfi_enabled, hfi_changed, itd_enabled, itd_changed;
 
 	/*
 	 * When the HFI enable bit changes (either from 0 to 1 or 1 to
@@ -2584,12 +2584,44 @@  static int vmx_set_hfi_cfg_msr(struct kvm_vcpu *vcpu,
 	 */
 	hfi_enabled = data & HW_FEEDBACK_CONFIG_HFI_ENABLE;
 	hfi_changed = kvm_vmx_hfi->hfi_enabled != hfi_enabled;
+	itd_enabled = data & HW_FEEDBACK_CONFIG_ITD_ENABLE;
+	itd_changed = kvm_vmx_hfi->itd_enabled != itd_enabled;
 
 	kvm_vmx->pkg_therm.msr_ia32_hfi_cfg = data;
 	kvm_vmx_hfi->hfi_enabled = hfi_enabled;
+	kvm_vmx_hfi->itd_enabled = itd_enabled;
+
+	if (!hfi_changed && !itd_changed)
+		return 0;
+
+	/*
+	 * Refer to SDM, vol. 3B, Table 15-10. IA32_HW_FEEDBACK_CONFIG
+	 * Control Option.
+	 */
+
+	/* Invalid option; quietly ignored by the hardware. */
+	if (!hfi_changed && itd_changed && !hfi_enabled && itd_enabled) {
+		/* No action (no update in the table). */
+		return 0;
+	}
 
-	if (!hfi_changed)
+	/* No action; keep HFI and Intel Thread Director disabled. */
+	if (!hfi_changed && itd_changed && !hfi_enabled && !itd_enabled) {
+		/* No action (no update in the table). */
 		return 0;
+	}
+
+	/* No action; keep HFI enabled. */
+	if (!hfi_changed && itd_changed && hfi_enabled && !itd_enabled) {
+		/* No action (no update in the table). */
+		return 0;
+	}
+
+	/* Disable HFI and Intel Thread Director whether ITD changed. */
+	if (hfi_changed && !hfi_enabled && itd_enabled) {
+		kvm_vmx_hfi->hfi_enabled = false;
+		kvm_vmx_hfi->itd_enabled = false;
+	}
 
 	if (!hfi_enabled) {
 		/*
@@ -3006,12 +3038,14 @@  static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 		if (!msr_info->host_initiated &&
 		    !guest_cpuid_has(vcpu, X86_FEATURE_HFI))
 			return 1;
-		/*
-		 * Unsupported and reserved bits. ITD is not supported
-		 * (CPUID.06H:EAX[19]) yet.
-		 */
+		/* Unsupported bit: generate the exception. */
+		if (!msr_info->host_initiated &&
+		    !guest_cpuid_has(vcpu, X86_FEATURE_ITD) &&
+		    (data & HW_FEEDBACK_CONFIG_ITD_ENABLE))
+			return 1;
+		/* Reserved bits: generate the exception. */
 		if (!msr_info->host_initiated &&
-		    data & ~(HW_FEEDBACK_CONFIG_HFI_ENABLE))
+		    data & ~(HW_FEEDBACK_CONFIG_HFI_ENABLE | HW_FEEDBACK_CONFIG_ITD_ENABLE))
 			return 1;
 
 		mutex_lock(&kvm_vmx->pkg_therm.pkg_therm_lock);
diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h
index d9db8bf3726f..0ef767d63def 100644
--- a/arch/x86/kvm/vmx/vmx.h
+++ b/arch/x86/kvm/vmx/vmx.h
@@ -377,6 +377,8 @@  struct vcpu_vmx {
  * struct hfi_desc - Representation of an HFI instance (i.e., a table)
  * @hfi_enabled:	Flag to indicate whether HFI is enabled at runtime.
  *			Parsed from the Guest's MSR_IA32_HW_FEEDBACK_CONFIG.
+ * @itd_enabled:	Flag to indicate whether ITD is enabled at runtime.
+ *			Parsed from the Guest's MSR_IA32_HW_FEEDBACK_CONFIG.
  * @hfi_int_enabled:	Flag to indicate whether HFI is enabled at runtime.
  *			Parsed from Guest's MSR_IA32_PACKAGE_THERM_INTERRUPT[bit 25].
  * @table_ptr_valid:	Flag to indicate whether the memory of Guest HFI table is ready.
@@ -407,6 +409,7 @@  struct vcpu_vmx {
 
 struct hfi_desc {
 	bool			hfi_enabled;
+	bool			itd_enabled;
 	bool			hfi_int_enabled;
 	bool			table_ptr_valid;
 	bool			hfi_update_status;