diff mbox

[RFC,v2,09/15] cpu-model/s390: Add KVM VM attribute interface routines

Message ID 1424183053-4310-10-git-send-email-mimu@linux.vnet.ibm.com
State New
Headers show

Commit Message

Michael Mueller Feb. 17, 2015, 2:24 p.m. UTC
The patch implements routines to set and retrieve processor configuration
data and to retrieve machine configuration data. The machine related data
is used together with the cpu model facility lists to determine the list of
supported cpu models of this host. The above mentioned routines have QEMU
trace point instrumentation.

Signed-off-by: Michael Mueller <mimu@linux.vnet.ibm.com>
---
 target-s390x/cpu-models.h |  39 ++++++++++++++++++
 target-s390x/kvm.c        | 102 ++++++++++++++++++++++++++++++++++++++++++++++
 trace-events              |   3 ++
 3 files changed, 144 insertions(+)

Comments

Alexander Graf Feb. 20, 2015, 1:59 p.m. UTC | #1
On 17.02.15 15:24, Michael Mueller wrote:
> The patch implements routines to set and retrieve processor configuration
> data and to retrieve machine configuration data. The machine related data
> is used together with the cpu model facility lists to determine the list of
> supported cpu models of this host. The above mentioned routines have QEMU
> trace point instrumentation.
> 
> Signed-off-by: Michael Mueller <mimu@linux.vnet.ibm.com>
> ---
>  target-s390x/cpu-models.h |  39 ++++++++++++++++++
>  target-s390x/kvm.c        | 102 ++++++++++++++++++++++++++++++++++++++++++++++
>  trace-events              |   3 ++
>  3 files changed, 144 insertions(+)
> 
> diff --git a/target-s390x/cpu-models.h b/target-s390x/cpu-models.h
> index 623a7b2..76b3456 100644
> --- a/target-s390x/cpu-models.h
> +++ b/target-s390x/cpu-models.h
> @@ -45,6 +45,45 @@ typedef struct S390CPUAlias {
>      char *model;
>  } S390CPUAlias;
>  
> +typedef struct S390ProcessorProps {
> +    uint64_t cpuid;
> +    uint16_t ibc;
> +    uint8_t  pad[6];
> +    uint64_t fac_list[S390_ARCH_FAC_LIST_SIZE_UINT64];
> +} S390ProcessorProps;
> +
> +typedef struct S390MachineProps {
> +    uint64_t cpuid;
> +    uint32_t ibc_range;
> +    uint8_t  pad[4];
> +    uint64_t fac_list_mask[S390_ARCH_FAC_LIST_SIZE_UINT64];
> +    uint64_t fac_list[S390_ARCH_FAC_LIST_SIZE_UINT64];
> +} S390MachineProps;

What are those structs there for? To convert between a kvm facing
interface to an internal interface?

I don't think they're necessary. The internal layout is visible from the
KVM code. Just either spawn the class straight from the kvm file or if
you consider that ugly, pass the values of that struct that you need as
function parameters to a function in cpu-models.c.


Alex
Michael Mueller Feb. 20, 2015, 3:18 p.m. UTC | #2
On Fri, 20 Feb 2015 14:59:20 +0100
Alexander Graf <agraf@suse.de> wrote:

> > +typedef struct S390ProcessorProps {
> > +    uint64_t cpuid;
> > +    uint16_t ibc;
> > +    uint8_t  pad[6];
> > +    uint64_t fac_list[S390_ARCH_FAC_LIST_SIZE_UINT64];
> > +} S390ProcessorProps;
> > +
> > +typedef struct S390MachineProps {
> > +    uint64_t cpuid;
> > +    uint32_t ibc_range;
> > +    uint8_t  pad[4];
> > +    uint64_t fac_list_mask[S390_ARCH_FAC_LIST_SIZE_UINT64];
> > +    uint64_t fac_list[S390_ARCH_FAC_LIST_SIZE_UINT64];
> > +} S390MachineProps;  
> 
> What are those structs there for? To convert between a kvm facing
> interface to an internal interface?

Yes, that's their current use, but if the interface structs: 

+struct kvm_s390_vm_cpu_processor {
+       __u64 cpuid;
+       __u16 ibc;
+       __u8  pad[6];
+       __u64 fac_list[256];
+};
+
+/* kvm S390 machine related attributes are r/o */
+#define KVM_S390_VM_CPU_MACHINE                1
+struct kvm_s390_vm_cpu_machine {
+       __u64 cpuid;
+       __u32 ibc_range;
+       __u8  pad[4];
+       __u64 fac_mask[256];
+       __u64 fac_list[256];
+};

are visible here, I'll reuse them... But stop, that will not work in the 
--disable-kvm case... I need them!
> 
> I don't think they're necessary. The internal layout is visible from the
> KVM code. Just either spawn the class straight from the kvm file or if
> you consider that ugly, pass the values of that struct that you need as
> function parameters to a function in cpu-models.c.
Alexander Graf Feb. 20, 2015, 4:59 p.m. UTC | #3
On 20.02.15 16:18, Michael Mueller wrote:
> On Fri, 20 Feb 2015 14:59:20 +0100
> Alexander Graf <agraf@suse.de> wrote:
> 
>>> +typedef struct S390ProcessorProps {
>>> +    uint64_t cpuid;
>>> +    uint16_t ibc;
>>> +    uint8_t  pad[6];
>>> +    uint64_t fac_list[S390_ARCH_FAC_LIST_SIZE_UINT64];
>>> +} S390ProcessorProps;
>>> +
>>> +typedef struct S390MachineProps {
>>> +    uint64_t cpuid;
>>> +    uint32_t ibc_range;
>>> +    uint8_t  pad[4];
>>> +    uint64_t fac_list_mask[S390_ARCH_FAC_LIST_SIZE_UINT64];
>>> +    uint64_t fac_list[S390_ARCH_FAC_LIST_SIZE_UINT64];
>>> +} S390MachineProps;  
>>
>> What are those structs there for? To convert between a kvm facing
>> interface to an internal interface?
> 
> Yes, that's their current use, but if the interface structs: 
> 
> +struct kvm_s390_vm_cpu_processor {
> +       __u64 cpuid;
> +       __u16 ibc;
> +       __u8  pad[6];
> +       __u64 fac_list[256];
> +};
> +
> +/* kvm S390 machine related attributes are r/o */
> +#define KVM_S390_VM_CPU_MACHINE                1
> +struct kvm_s390_vm_cpu_machine {
> +       __u64 cpuid;
> +       __u32 ibc_range;
> +       __u8  pad[4];
> +       __u64 fac_mask[256];
> +       __u64 fac_list[256];
> +};
> 
> are visible here, I'll reuse them... But stop, that will not work in the 
> --disable-kvm case... I need them!

I meant it the other way around - do KVM specific patching of the cpu
types from kvm.c.

But please give a nutshell explanation on what exactly you're patching
at all here.


Alex
Michael Mueller Feb. 20, 2015, 6:42 p.m. UTC | #4
On Fri, 20 Feb 2015 17:59:14 +0100
Alexander Graf <agraf@suse.de> wrote:

> But please give a nutshell explanation on what exactly you're patching
> at all here.

Please don't ask in riddles... :-)

S390ProcessorProps are attributes that represent cpu model related properties of the processor. 

typedef struct S390ProcessorProps {
    uint64_t cpuid;
    uint16_t ibc;
    uint8_t  pad[6];
    uint64_t fac_list[S390_ARCH_FAC_LIST_SIZE_UINT64];
} S390ProcessorProps;

S390MachineProps are attributes that represent cpu model related properties that a specific host
offers.

fac_list_mask are the facilities that are supported by the accelerator code and the hosting
machine in case of KVM it is kvm_s390_fac_list_mask & STFLE, fac_list is STFLE only

typedef struct S390MachineProps {
    uint64_t cpuid;
    uint32_t ibc_range;
    uint8_t  pad[4];
    uint64_t fac_list_mask[S390_ARCH_FAC_LIST_SIZE_UINT64];
    uint64_t fac_list[S390_ARCH_FAC_LIST_SIZE_UINT64];
} S390MachineProps;

The various S390CPUModel classes represent all well defined cpu models (those which have been
implemented as real machines). Not all of these models are executable on a given host. Only the
most current machine implementation is able to run all models. The list of "runnable" cpu models
is calculated by "matching" the S390MachineProps with the S390CPUModel classes and the
qemu_s390_fac_list_mask[] (indicates the facilities that are implemented by QEMU itself.)

The qemu cpu_model is translated to the respective S390CPUModel class and its processor
properties (S390ProcessorProps) are used with the s390_set_proceccor_props() call to communicate
them to the accelerator what specific cpuid,ibc,fac_list shall be used.

Michael
diff mbox

Patch

diff --git a/target-s390x/cpu-models.h b/target-s390x/cpu-models.h
index 623a7b2..76b3456 100644
--- a/target-s390x/cpu-models.h
+++ b/target-s390x/cpu-models.h
@@ -45,6 +45,45 @@  typedef struct S390CPUAlias {
     char *model;
 } S390CPUAlias;
 
+typedef struct S390ProcessorProps {
+    uint64_t cpuid;
+    uint16_t ibc;
+    uint8_t  pad[6];
+    uint64_t fac_list[S390_ARCH_FAC_LIST_SIZE_UINT64];
+} S390ProcessorProps;
+
+typedef struct S390MachineProps {
+    uint64_t cpuid;
+    uint32_t ibc_range;
+    uint8_t  pad[4];
+    uint64_t fac_list_mask[S390_ARCH_FAC_LIST_SIZE_UINT64];
+    uint64_t fac_list[S390_ARCH_FAC_LIST_SIZE_UINT64];
+} S390MachineProps;
+
+#ifdef CONFIG_KVM
+int kvm_s390_get_processor_props(S390ProcessorProps *prop);
+int kvm_s390_set_processor_props(S390ProcessorProps *prop);
+bool kvm_s390_cpu_classes_initialized(void);
+bool kvm_s390_probe_mode(void);
+#else
+static inline int kvm_s390_get_processor_props(S390ProcessorProps *prob)
+{
+    return -ENOSYS;
+}
+static inline int kvm_s390_set_processor_props(S390ProcessorProps *prob)
+{
+    return -ENOSYS;
+}
+static inline bool kvm_s390_cpu_classes_initialized(void)
+{
+    return false;
+}
+static inline bool kvm_s390_probe_mode(void)
+{
+    return false;
+}
+#endif
+
 /*
  * bits 0-7   : CMOS generation
  * bits 8-9   : reserved
diff --git a/target-s390x/kvm.c b/target-s390x/kvm.c
index 6f2d5b4..c518489 100644
--- a/target-s390x/kvm.c
+++ b/target-s390x/kvm.c
@@ -42,6 +42,7 @@ 
 #include "qapi-event.h"
 #include "hw/s390x/s390-pci-inst.h"
 #include "hw/s390x/s390-pci-bus.h"
+#include "cpu-models.h"
 
 /* #define DEBUG_KVM */
 
@@ -117,6 +118,7 @@  const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
 
 static int cap_sync_regs;
 static int cap_async_pf;
+static bool cpu_classes_initialized;
 
 static void *legacy_s390_alloc(size_t size, uint64_t *align);
 
@@ -172,6 +174,68 @@  static void kvm_s390_enable_cmma(KVMState *s)
     trace_kvm_enable_cmma(rc);
 }
 
+static int cpu_model_get(KVMState *s, uint64_t attr, uint64_t addr)
+{
+    struct kvm_device_attr dev_attr = {
+        .group = KVM_S390_VM_CPU_MODEL,
+        .attr = attr,
+        .addr = addr,
+    };
+
+    return kvm_vm_ioctl(s, KVM_GET_DEVICE_ATTR, &dev_attr);
+}
+
+static int cpu_model_set(KVMState *s, uint64_t attr, uint64_t addr)
+{
+    struct kvm_device_attr dev_attr = {
+        .group = KVM_S390_VM_CPU_MODEL,
+        .attr = attr,
+        .addr = addr,
+    };
+
+    return kvm_vm_ioctl(s, KVM_SET_DEVICE_ATTR, &dev_attr);
+}
+
+static int has_cpu_model_call(KVMState *s, uint64_t attr)
+{
+    int rc;
+    struct kvm_device_attr dev_attr = {
+        .group = KVM_S390_VM_CPU_MODEL,
+        .attr = attr,
+    };
+
+    if (kvm_check_extension(s, KVM_CAP_VM_ATTRIBUTES) == 0) {
+        return -ENOSYS;
+    }
+
+    rc = kvm_vm_ioctl(s, KVM_HAS_DEVICE_ATTR, &dev_attr);
+    if (rc == 0) {
+        return 0;
+    }
+    return -EFAULT;
+}
+
+static int kvm_s390_get_machine_props(KVMState *s, S390MachineProps *prop)
+{
+    int rc;
+
+    rc = has_cpu_model_call(s, KVM_S390_VM_CPU_MACHINE);
+    if (!rc) {
+        rc = cpu_model_get(s, KVM_S390_VM_CPU_MACHINE, (uint64_t) prop);
+    }
+    trace_kvm_get_machine_props(rc, prop->cpuid, prop->ibc_range);
+    return rc;
+}
+
+static void kvm_s390_setup_cpu_classes(KVMState *s)
+{
+    S390MachineProps mach;
+
+    if (!kvm_s390_get_machine_props(s, &mach)) {
+        cpu_classes_initialized = false;
+    }
+}
+
 int kvm_arch_init(KVMState *s)
 {
     cap_sync_regs = kvm_check_extension(s, KVM_CAP_SYNC_REGS);
@@ -185,6 +249,8 @@  int kvm_arch_init(KVMState *s)
         || !kvm_check_extension(s, KVM_CAP_S390_COW)) {
         phys_mem_set_alloc(legacy_s390_alloc);
     }
+
+    kvm_s390_setup_cpu_classes(s);
     return 0;
 }
 
@@ -1570,3 +1636,39 @@  int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
     route->u.adapter.adapter_id = pbdev->routes.adapter.adapter_id;
     return 0;
 }
+
+int kvm_s390_get_processor_props(S390ProcessorProps *prop)
+{
+    int rc;
+
+    rc = has_cpu_model_call(kvm_state, KVM_S390_VM_CPU_PROCESSOR);
+    if (!rc) {
+        rc = cpu_model_get(kvm_state,
+                           KVM_S390_VM_CPU_PROCESSOR, (uint64_t) prop);
+    }
+    trace_kvm_get_processor_props(rc, prop->cpuid, prop->ibc);
+    return rc;
+}
+
+int kvm_s390_set_processor_props(S390ProcessorProps *prop)
+{
+    int rc;
+
+    rc = has_cpu_model_call(kvm_state, KVM_S390_VM_CPU_PROCESSOR);
+    if (!rc) {
+        rc = cpu_model_set(kvm_state,
+                           KVM_S390_VM_CPU_PROCESSOR, (uint64_t) prop);
+    }
+    trace_kvm_set_processor_props(rc);
+    return rc;
+}
+
+bool kvm_s390_cpu_classes_initialized(void)
+{
+    return cpu_classes_initialized;
+}
+
+bool kvm_s390_probe_mode(void)
+{
+    return kvm_probe_mode_enabled();
+}
diff --git a/trace-events b/trace-events
index f87b077..f39f879 100644
--- a/trace-events
+++ b/trace-events
@@ -1577,6 +1577,9 @@  mhp_pc_dimm_assigned_address(uint64_t addr) "0x%"PRIx64
 kvm_enable_cmma(int rc) "CMMA: enabling with result code %d"
 kvm_clear_cmma(int rc) "CMMA: clearing with result code %d"
 kvm_failed_cpu_state_set(int cpu_index, uint8_t state, const char *msg) "Warning: Unable to set cpu %d state %" PRIu8 " to KVM: %s"
+kvm_get_machine_props(int rc, uint64_t cpuid, uint16_t ibc) "CPU-MODEL: fetching machine properties rc=%d cpuid=0x%"PRIx64" ibc=0x%"PRIx16
+kvm_get_processor_props(int rc, uint64_t cpuid, uint32_t ibc_range) "CPU-MODEL: fetching processor properties rc=%d cpuid=0x%"PRIx64" ibc_range=0x%"PRIx32
+kvm_set_processor_props(int rc) "CPU-MODEL: requesting processor properties rc=%d"
 
 # hw/dma/i8257.c
 i8257_unregistered_dma(int nchan, int dma_pos, int dma_len) "unregistered DMA channel used nchan=%d dma_pos=%d dma_len=%d"