Skip to contentSkip to conversation

Add AMD CPU for RAPL MSR support

PATCH New Anthony Harivel 1 participant 3 messages Mar 13, 2025

lore
Share & tools
lore
#0 lore raw

cover Add AMD CPU for RAPL MSR support

Hi,

The following patch add the support for AMD CPU for RAPL MSR. This was one of the two current limitation of the introduction patch.

This has been tested on AMD Ryzen 5850U. I was able to mount the RAPL driver in a VM with '-cpu host'. The counter value are increasing properly.

I added a second patch to silence an error that is not really an error. I change it to a tracepoint following Daniel advice. Thanks Daniel !

Thanks
Anthony

Anthony Harivel (2):
target/i386/kvm: add AMD support for RAPL MSR target/i386/kvm: Change error_report() to tracepoint in
vmsr_read_thread_stat()

 docs/specs/rapl-msr.rst       |  21 ++++---
 include/system/kvm_int.h      |   4 ++
 target/i386/cpu.h             |   2 +
 target/i386/kvm/kvm.c         | 111 ++++++++++++++++++++++------------
 target/i386/kvm/trace-events  |   3 +
 target/i386/kvm/vmsr_energy.c |  13 +++-
 target/i386/kvm/vmsr_energy.h |   1 +
 tools/i386/qemu-vmsr-helper.c |  47 ++++++++------
 tools/i386/rapl-msr-index.h   |   4 ++
 9 files changed, 139 insertions(+), 67 deletions(-)
Signature · 1 line
2.48.1
#1 lore raw

PATCH 1/2 target/i386/kvm: add AMD support for RAPL MSR

Rework the RAPL MSR support to function with AMD specific RAPL MSR.

Implement support for AMD-specific RAPL MSRs. AMD CPUs support three
MSRs: MSR_AMD_RAPL_POWER_UNIT, MSR_AMD_PKG_ENERGY_STATUS, and MSR_AMD_CORE_ENERGY_STATUS. These MSRs have different addresses compared to their Intel counterparts. Integrate the first two MSRs to align with the current RAPL MSR implementation. Automatically detect the host CPU at runtime and select the appropriate MSRs.

---
 docs/specs/rapl-msr.rst       |  21 ++++---
 include/system/kvm_int.h      |   4 ++
 target/i386/cpu.h             |   2 +
 target/i386/kvm/kvm.c         | 111 ++++++++++++++++++++++------------
 target/i386/kvm/vmsr_energy.c |  10 +++
 target/i386/kvm/vmsr_energy.h |   1 +
 tools/i386/qemu-vmsr-helper.c |  47 ++++++++------
 tools/i386/rapl-msr-index.h   |   4 ++
 8 files changed, 134 insertions(+), 66 deletions(-)
docs/specs/rapl-msr.rst +11 -10
index aaf0db9f91b9..37fb9677c9a4 100644
@@ -20,10 +20,14 @@ At the moment the following MSRs are involved:
 
 .. code:: C
 
+    // Intel CPU
     #define MSR_RAPL_POWER_UNIT             0x00000606
     #define MSR_PKG_POWER_LIMIT             0x00000610
     #define MSR_PKG_ENERGY_STATUS           0x00000611
     #define MSR_PKG_POWER_INFO              0x00000614
+    // AMD CPU
+    #define MSR_AMD_RAPL_POWER_UNIT         0xc0010299
+    #define MSR_AMD_PKG_ENERGY_STATUS       0xc001029b
 
 The ``*_POWER_UNIT``, ``*_POWER_LIMIT``, ``*_POWER INFO`` are part of the RAPL
 spec and specify the power limit of the package, provide range of parameter(min
@@ -32,18 +36,18 @@ counter to calculate the power. Those MSRs are populated once at the beginning
 by reading the host CPU MSRs and are given back to the guest 1:1 when
 requested.
 
-The MSR_PKG_ENERGY_STATUS is a counter; it represents the total amount of
+The ``*_PKG_ENERGY_STATUS`` is a counter; it represents the total amount of
 energy consumed since the last time the register was cleared. If you multiply
 it with the UNIT provided above you'll get the power in micro-joules. This
 counter is always increasing and it increases more or less faster depending on
 the consumption of the package. This counter is supposed to overflow at some
 point.
 
-Each core belonging to the same Package reading the MSR_PKG_ENERGY_STATUS (i.e
-"rdmsr 0x611") will retrieve the same value. The value represents the energy
-for the whole package. Whatever Core reading it will get the same value and a
-core that belongs to PKG-0 will not be able to get the value of PKG-1 and
-vice-versa.
+Each core belonging to the same Package reading the ``*_PKG_ENERGY_STATUS`` (i.e
+"rdmsr 0x611" for Intel CPU) will retrieve the same value. The value represents
+the energy for the whole package. Whatever Core reading it will get the same
+value and a core that belongs to PKG-0 will not be able to get the value of
+PKG-1 and vice-versa.
 
 High level implementation
 -------------------------
@@ -146,9 +150,6 @@ See the qemu-pr-helper documentation or manpage for further details.
 Current Limitations
 -------------------
 
-- Works only on Intel host CPUs because AMD CPUs are using different MSR
-  addresses.
-
-- Only the Package Power-Plane (MSR_PKG_ENERGY_STATUS) is reported at the
+- Only the Package Power-Plane (``*_PKG_ENERGY_STATUS``) is reported at the
   moment.
 
include/system/kvm_int.h +4 -0
index 4de6106869b0..3c91aa35d1a5 100644
@@ -76,6 +76,10 @@ struct KVMMsrEnergy {
     X86CPUTopoInfo guest_topo_info;
     KVMHostTopoInfo host_topo;
     const CPUArchIdList *guest_cpu_list;
+    uint64_t msr_value_addr;
+    uint64_t msr_unit_addr;
+    uint64_t msr_limit_addr;
+    uint64_t msr_info_addr;
     uint64_t *msr_value;
     uint64_t msr_unit;
     uint64_t msr_limit;
target/i386/cpu.h +2 -0
index 76f24446a55d..89a0202e3f40 100644
@@ -420,6 +420,8 @@ typedef enum X86Seg {
 #define MSR_ARCH_LBR_FROM_0             0x00001500
 #define MSR_ARCH_LBR_TO_0               0x00001600
 #define MSR_ARCH_LBR_INFO_0             0x00001200
+#define MSR_AMD_RAPL_POWER_UNIT         0xc0010299
+#define MSR_AMD_PKG_ENERGY_STATUS       0xc001029b
 
 #define FEATURE_CONTROL_LOCKED                    (1<<0)
 #define FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX  (1ULL << 1)
target/i386/kvm/kvm.c +72 -39

150 lines not on this page · raw message

target/i386/kvm/vmsr_energy.c +10 -0
index 31508d4e77a2..f3861d0607bb 100644
@@ -37,6 +37,16 @@ bool is_host_cpu_intel(void)
     return g_str_equal(vendor, CPUID_VENDOR_INTEL);
 }
 
+bool is_host_cpu_amd(void)
+{
+    int family, model, stepping;
+    char vendor[CPUID_VENDOR_SZ + 1];
+
+    host_cpu_vendor_fms(vendor, &family, &model, &stepping);
+
+    return g_str_equal(vendor, CPUID_VENDOR_AMD);
+}
+
 int is_rapl_enabled(void)
 {
     const char *path = "/sys/class/powercap/intel-rapl/enabled";
target/i386/kvm/vmsr_energy.h +1 -0
index 16cc1f4814f6..8aeed09c33b7 100644
@@ -95,5 +95,6 @@ double vmsr_get_ratio(uint64_t e_delta,
                       unsigned int maxticks);
 void vmsr_init_topo_info(X86CPUTopoInfo *topo_info, const MachineState *ms);
 bool is_host_cpu_intel(void);
+bool is_host_cpu_amd(void);
 int is_rapl_enabled(void);
 #endif /* VMSR_ENERGY_H */
tools/i386/qemu-vmsr-helper.c +30 -17

80 lines not on this page · raw message

tools/i386/rapl-msr-index.h +4 -0
index 9a7118639ae3..45e89823a647 100644
@@ -26,3 +26,7 @@
 #define MSR_PKG_POWER_LIMIT             0x00000610
 #define MSR_PKG_ENERGY_STATUS           0x00000611
 #define MSR_PKG_POWER_INFO              0x00000614
+
+/* AMD Specific RAPL MSR */
+#define MSR_AMD_RAPL_POWER_UNIT         0xc0010299
+#define MSR_AMD_PKG_ENERGY_STATUS       0xc001029b
Signature · 1 line
2.48.1
#2 lore raw

PATCH 2/2 target/i386/kvm: Change error_report() to tracepoint in vmsr_read_thread_stat()

In reply to Anthony Harivel

Threads in QEMU are frequently created and destroyed, leading to non-critical errors. Replace error_report() with a tracepoint to prevent flooding the serial terminal with non-essential error messages.

---
 target/i386/kvm/trace-events  | 3 +++
 target/i386/kvm/vmsr_energy.c | 3 ++-
 2 files changed, 5 insertions(+), 1 deletion(-)
target/i386/kvm/trace-events +3 -0
index 74a6234ff7f5..527750e3b23d 100644
@@ -13,3 +13,6 @@ kvm_xen_soft_reset(void) ""
 kvm_xen_set_shared_info(uint64_t gfn) "shared info at gfn 0x%" PRIx64
 kvm_xen_set_vcpu_attr(int cpu, int type, uint64_t gpa) "vcpu attr cpu %d type %d gpa 0x%" PRIx64
 kvm_xen_set_vcpu_callback(int cpu, int vector) "callback vcpu %d vector %d"
+
+# vmsr_energy.c
+vmsr_read_thread_stat(const char *path) "Error opening path %s"
target/i386/kvm/vmsr_energy.c +2 -1
index f3861d0607bb..f99aa9cfeb94 100644
@@ -19,6 +19,7 @@
 #include "hw/boards.h"
 #include "cpu.h"
 #include "host-cpu.h"
+#include "trace.h"
 
 char *vmsr_compute_default_paths(void)
 {
@@ -280,7 +281,7 @@ void vmsr_read_thread_stat(pid_t pid,
 
     FILE *file = fopen(path, "r");
     if (file == NULL) {
-        error_report("Error opening %s", path_name);
+        trace_vmsr_read_thread_stat(path_name);
         return;
     }
 
Signature · 1 line
2.48.1