]> git.karo-electronics.de Git - karo-tx-linux.git/commitdiff
kvm tools: Add SPAPR PPC64 hcall & rtascall structure
authorMatt Evans <matt@ozlabs.org>
Sat, 4 Feb 2012 08:07:21 +0000 (19:07 +1100)
committerPekka Enberg <penberg@kernel.org>
Sat, 4 Feb 2012 09:19:59 +0000 (11:19 +0200)
This patch adds the basic structure for HV calls, their registration and some of
the simpler calls.  A similar layout for RTAS calls is also added, again with
some of the simpler RTAS calls used by the guest.  The SPAPR RTAS stub is
generated inline.  Also, nodes for RTAS are added to the device tree.

Signed-off-by: Matt Evans <matt@ozlabs.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
tools/kvm/Makefile
tools/kvm/powerpc/kvm-cpu.c
tools/kvm/powerpc/kvm.c
tools/kvm/powerpc/spapr.h [new file with mode: 0644]
tools/kvm/powerpc/spapr_hcall.c [new file with mode: 0644]
tools/kvm/powerpc/spapr_rtas.c [new file with mode: 0644]

index e8b3a4ac23fb9cae3a0c44429dcea5dc8c2d20e1..38ec3fd7957b4f72e8c85e6e7aec63c094dd288f 100644 (file)
@@ -134,6 +134,8 @@ ifeq ($(uname_M), ppc64)
        OBJS    += powerpc/kvm.o
        OBJS    += powerpc/cpu_info.o
        OBJS    += powerpc/kvm-cpu.o
+       OBJS    += powerpc/spapr_hcall.o
+       OBJS    += powerpc/spapr_rtas.o
 # We use libfdt, but it's sometimes not packaged 64bit.  It's small too,
 # so just build it in:
        CFLAGS  += -I../../scripts/dtc/libfdt
index a2beaacc1554d2262aa19bae1599f5c9fa5ef6d9..51bb7189c70cc92f2466502cb7042b8965d0e088 100644 (file)
@@ -14,6 +14,8 @@
 #include "kvm/util.h"
 #include "kvm/kvm.h"
 
+#include "spapr.h"
+
 #include <sys/ioctl.h>
 #include <sys/mman.h>
 #include <signal.h>
@@ -169,6 +171,10 @@ bool kvm_cpu__handle_exit(struct kvm_cpu *vcpu)
        bool ret = true;
        struct kvm_run *run = vcpu->kvm_run;
        switch(run->exit_reason) {
+       case KVM_EXIT_PAPR_HCALL:
+               run->papr_hcall.ret = spapr_hypercall(vcpu, run->papr_hcall.nr,
+                                                     (target_ulong*)run->papr_hcall.args);
+               break;
        default:
                ret = false;
        }
index 842af370f517b9774cfbc54e6ab73aaf7be0bb86..891fa6511c009311031bc2f7cd3db994f1ea03c7 100644 (file)
@@ -16,6 +16,8 @@
 #include "libfdt.h"
 #include "cpu_info.h"
 
+#include "spapr.h"
+
 #include <linux/kvm.h>
 
 #include <sys/types.h>
@@ -126,6 +128,11 @@ void kvm__arch_init(struct kvm *kvm, const char *hugetlbfs_path, u64 ram_size)
        if (cap_ppc_rma == 2)
                die("Need contiguous RMA allocation on this hardware, "
                    "which is not yet supported.");
+
+       /* Do these before FDT setup, IRQ setup, etc. */
+       /* FIXME: SPAPR-specific */
+       hypercall_init();
+       register_core_rtas();
 }
 
 void kvm__arch_delete_ram(struct kvm *kvm)
@@ -231,6 +238,20 @@ static void setup_fdt(struct kvm *kvm)
        _FDT(fdt_property_cell(fdt, "#address-cells", 0x2));
        _FDT(fdt_property_cell(fdt, "#size-cells", 0x2));
 
+       /* RTAS */
+       _FDT(fdt_begin_node(fdt, "rtas"));
+       /* This is what the kernel uses to switch 'We're an LPAR'! */
+        _FDT(fdt_property(fdt, "ibm,hypertas-functions", hypertas_prop_kvm,
+                           sizeof(hypertas_prop_kvm)));
+       _FDT(fdt_property_cell(fdt, "linux,rtas-base", kvm->rtas_gra));
+       _FDT(fdt_property_cell(fdt, "linux,rtas-entry", kvm->rtas_gra));
+       _FDT(fdt_property_cell(fdt, "rtas-size", kvm->rtas_size));
+       /* Now add properties for all RTAS tokens: */
+       if (spapr_rtas_fdt_setup(kvm, fdt))
+               die("Couldn't create RTAS FDT properties\n");
+
+       _FDT(fdt_end_node(fdt));
+
        /* /chosen */
        _FDT(fdt_begin_node(fdt, "chosen"));
        /* cmdline */
@@ -345,7 +366,25 @@ static void setup_fdt(struct kvm *kvm)
  */
 int kvm__arch_setup_firmware(struct kvm *kvm)
 {
-       /* Load RTAS */
+       /*
+        * Set up RTAS stub.  All it is is a single hypercall:
+        *  0:   7c 64 1b 78     mr      r4,r3
+        *  4:   3c 60 00 00     lis     r3,0
+        *  8:   60 63 f0 00     ori     r3,r3,61440
+        *  c:   44 00 00 22     sc      1
+        * 10:   4e 80 00 20     blr
+        */
+       uint32_t *rtas = guest_flat_to_host(kvm, kvm->rtas_gra);
+
+       rtas[0] = 0x7c641b78;
+       rtas[1] = 0x3c600000;
+       rtas[2] = 0x6063f000;
+       rtas[3] = 0x44000022;
+       rtas[4] = 0x4e800020;
+       kvm->rtas_size = 20;
+
+       pr_info("Set up %ld bytes of RTAS at 0x%lx\n",
+               kvm->rtas_size, kvm->rtas_gra);
 
        /* Load SLOF */
 
diff --git a/tools/kvm/powerpc/spapr.h b/tools/kvm/powerpc/spapr.h
new file mode 100644 (file)
index 0000000..1271daa
--- /dev/null
@@ -0,0 +1,84 @@
+/*
+ * SPAPR definitions and declarations
+ *
+ * Borrowed heavily from QEMU's spapr.h,
+ * Copyright (c) 2010 David Gibson, IBM Corporation.
+ *
+ * Modifications by Matt Evans <matt@ozlabs.org>, IBM Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 as published
+ * by the Free Software Foundation.
+ */
+
+#if !defined(__HW_SPAPR_H__)
+#define __HW_SPAPR_H__
+
+#include <inttypes.h>
+
+/* We need some of the H_ hcall defs, but they're __KERNEL__ only. */
+#define __KERNEL__
+#include <asm/hvcall.h>
+#undef __KERNEL__
+
+#include "kvm/kvm.h"
+#include "kvm/kvm-cpu.h"
+
+typedef unsigned long target_ulong;
+typedef uintptr_t target_phys_addr_t;
+
+/*
+ * The hcalls above are standardized in PAPR and implemented by pHyp
+ * as well.
+ *
+ * We also need some hcalls which are specific to qemu / KVM-on-POWER.
+ * So far we just need one for H_RTAS, but in future we'll need more
+ * for extensions like virtio.  We put those into the 0xf000-0xfffc
+ * range which is reserved by PAPR for "platform-specific" hcalls.
+ */
+#define KVMPPC_HCALL_BASE       0xf000
+#define KVMPPC_H_RTAS           (KVMPPC_HCALL_BASE + 0x0)
+#define KVMPPC_HCALL_MAX        KVMPPC_H_RTAS
+
+#define DEBUG_SPAPR_HCALLS
+
+#ifdef DEBUG_SPAPR_HCALLS
+#define hcall_dprintf(fmt, ...) \
+    do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
+#else
+#define hcall_dprintf(fmt, ...) \
+    do { } while (0)
+#endif
+
+typedef target_ulong (*spapr_hcall_fn)(struct kvm_cpu *vcpu,
+                                      target_ulong opcode,
+                                       target_ulong *args);
+
+void hypercall_init(void);
+void register_core_rtas(void);
+
+void spapr_register_hypercall(target_ulong opcode, spapr_hcall_fn fn);
+target_ulong spapr_hypercall(struct kvm_cpu *vcpu, target_ulong opcode,
+                             target_ulong *args);
+
+int spapr_rtas_fdt_setup(struct kvm *kvm, void *fdt);
+
+static inline uint32_t rtas_ld(struct kvm *kvm, target_ulong phys, int n)
+{
+       return *((uint32_t *)guest_flat_to_host(kvm, phys + 4*n));
+}
+
+static inline void rtas_st(struct kvm *kvm, target_ulong phys, int n, uint32_t val)
+{
+       *((uint32_t *)guest_flat_to_host(kvm, phys + 4*n)) = val;
+}
+
+typedef void (*spapr_rtas_fn)(struct kvm_cpu *vcpu, uint32_t token,
+                              uint32_t nargs, target_ulong args,
+                              uint32_t nret, target_ulong rets);
+void spapr_rtas_register(const char *name, spapr_rtas_fn fn);
+target_ulong spapr_rtas_call(struct kvm_cpu *vcpu,
+                             uint32_t token, uint32_t nargs, target_ulong args,
+                             uint32_t nret, target_ulong rets);
+
+#endif /* !defined (__HW_SPAPR_H__) */
diff --git a/tools/kvm/powerpc/spapr_hcall.c b/tools/kvm/powerpc/spapr_hcall.c
new file mode 100644 (file)
index 0000000..ff1d63a
--- /dev/null
@@ -0,0 +1,134 @@
+/*
+ * SPAPR hypercalls
+ *
+ * Borrowed heavily from QEMU's spapr_hcall.c,
+ * Copyright (c) 2010 David Gibson, IBM Corporation.
+ *
+ * Copyright (c) 2011 Matt Evans <matt@ozlabs.org>, IBM Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 as published
+ * by the Free Software Foundation.
+ */
+
+#include "spapr.h"
+#include "kvm/util.h"
+#include "kvm/kvm.h"
+#include "kvm/kvm-cpu.h"
+
+#include <stdio.h>
+#include <assert.h>
+
+static spapr_hcall_fn papr_hypercall_table[(MAX_HCALL_OPCODE / 4) + 1];
+static spapr_hcall_fn kvmppc_hypercall_table[KVMPPC_HCALL_MAX -
+                                            KVMPPC_HCALL_BASE + 1];
+
+static target_ulong h_set_dabr(struct kvm_cpu *vcpu, target_ulong opcode, target_ulong *args)
+{
+       /* FIXME:  Implement this for -PR.  (-HV does this in kernel.) */
+       return H_HARDWARE;
+}
+
+static target_ulong h_rtas(struct kvm_cpu *vcpu, target_ulong opcode, target_ulong *args)
+{
+       target_ulong rtas_r3 = args[0];
+       /*
+        * Pointer read from phys mem; these ptrs cannot be MMIO (!) so just
+        * reference guest RAM directly.
+        */
+       uint32_t token, nargs, nret;
+
+       token = rtas_ld(vcpu->kvm, rtas_r3, 0);
+       nargs = rtas_ld(vcpu->kvm, rtas_r3, 1);
+       nret  = rtas_ld(vcpu->kvm, rtas_r3, 2);
+
+       return spapr_rtas_call(vcpu, token, nargs, rtas_r3 + 12,
+                              nret, rtas_r3 + 12 + 4*nargs);
+}
+
+static target_ulong h_logical_load(struct kvm_cpu *vcpu, target_ulong opcode, target_ulong *args)
+{
+       /* SLOF will require these, though kernel doesn't. */
+       die(__PRETTY_FUNCTION__);
+       return H_PARAMETER;
+}
+
+static target_ulong h_logical_store(struct kvm_cpu *vcpu, target_ulong opcode, target_ulong *args)
+{
+       /* SLOF will require these, though kernel doesn't. */
+       die(__PRETTY_FUNCTION__);
+       return H_PARAMETER;
+}
+
+static target_ulong h_logical_icbi(struct kvm_cpu *vcpu, target_ulong opcode, target_ulong *args)
+{
+       /* KVM will trap this in the kernel.  Die if it misses. */
+       die(__PRETTY_FUNCTION__);
+       return H_SUCCESS;
+}
+
+static target_ulong h_logical_dcbf(struct kvm_cpu *vcpu, target_ulong opcode, target_ulong *args)
+{
+       /* KVM will trap this in the kernel.  Die if it misses. */
+       die(__PRETTY_FUNCTION__);
+       return H_SUCCESS;
+}
+
+void spapr_register_hypercall(target_ulong opcode, spapr_hcall_fn fn)
+{
+       spapr_hcall_fn *slot;
+
+       if (opcode <= MAX_HCALL_OPCODE) {
+               assert((opcode & 0x3) == 0);
+
+               slot = &papr_hypercall_table[opcode / 4];
+       } else {
+               assert((opcode >= KVMPPC_HCALL_BASE) &&
+                      (opcode <= KVMPPC_HCALL_MAX));
+
+               slot = &kvmppc_hypercall_table[opcode - KVMPPC_HCALL_BASE];
+       }
+
+       assert(!(*slot) || (fn == *slot));
+       *slot = fn;
+}
+
+target_ulong spapr_hypercall(struct kvm_cpu *vcpu, target_ulong opcode,
+                            target_ulong *args)
+{
+       if ((opcode <= MAX_HCALL_OPCODE)
+           && ((opcode & 0x3) == 0)) {
+               spapr_hcall_fn fn = papr_hypercall_table[opcode / 4];
+
+               if (fn) {
+                       return fn(vcpu, opcode, args);
+               }
+       } else if ((opcode >= KVMPPC_HCALL_BASE) &&
+                  (opcode <= KVMPPC_HCALL_MAX)) {
+               spapr_hcall_fn fn = kvmppc_hypercall_table[opcode -
+                                                          KVMPPC_HCALL_BASE];
+
+               if (fn) {
+                       return fn(vcpu, opcode, args);
+               }
+       }
+
+       hcall_dprintf("Unimplemented hcall 0x%lx\n", opcode);
+       return H_FUNCTION;
+}
+
+void hypercall_init(void)
+{
+       /* hcall-dabr */
+       spapr_register_hypercall(H_SET_DABR, h_set_dabr);
+
+       spapr_register_hypercall(H_LOGICAL_CI_LOAD, h_logical_load);
+       spapr_register_hypercall(H_LOGICAL_CI_STORE, h_logical_store);
+       spapr_register_hypercall(H_LOGICAL_CACHE_LOAD, h_logical_load);
+       spapr_register_hypercall(H_LOGICAL_CACHE_STORE, h_logical_store);
+       spapr_register_hypercall(H_LOGICAL_ICBI, h_logical_icbi);
+       spapr_register_hypercall(H_LOGICAL_DCBF, h_logical_dcbf);
+
+       /* KVM-PPC specific hcalls */
+       spapr_register_hypercall(KVMPPC_H_RTAS, h_rtas);
+}
diff --git a/tools/kvm/powerpc/spapr_rtas.c b/tools/kvm/powerpc/spapr_rtas.c
new file mode 100644 (file)
index 0000000..14a3462
--- /dev/null
@@ -0,0 +1,229 @@
+/*
+ * SPAPR base RTAS calls
+ *
+ * Borrowed heavily from QEMU's spapr_rtas.c
+ * Copyright (c) 2010-2011 David Gibson, IBM Corporation.
+ *
+ * Modifications copyright 2011 Matt Evans <matt@ozlabs.org>, IBM Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 as published
+ * by the Free Software Foundation.
+ */
+
+#include "kvm/kvm.h"
+#include "kvm/kvm-cpu.h"
+#include "kvm/util.h"
+#include "kvm/term.h"
+#include "libfdt.h"
+
+#include "spapr.h"
+
+#include <stdio.h>
+#include <assert.h>
+
+#define TOKEN_BASE      0x2000
+#define TOKEN_MAX       0x100
+
+#define RTAS_CONSOLE
+
+static struct rtas_call {
+       const char *name;
+       spapr_rtas_fn fn;
+} rtas_table[TOKEN_MAX];
+
+struct rtas_call *rtas_next = rtas_table;
+
+
+static void rtas_display_character(struct kvm_cpu *vcpu,
+                                   uint32_t token, uint32_t nargs,
+                                   target_ulong args,
+                                   uint32_t nret, target_ulong rets)
+{
+       char c = rtas_ld(vcpu->kvm, args, 0);
+       term_putc(CONSOLE_HV, &c, 1, 0);
+       rtas_st(vcpu->kvm, rets, 0, 0);
+}
+
+#ifdef RTAS_CONSOLE
+static void rtas_put_term_char(struct kvm_cpu *vcpu,
+                              uint32_t token, uint32_t nargs,
+                              target_ulong args,
+                              uint32_t nret, target_ulong rets)
+{
+       char c = rtas_ld(vcpu->kvm, args, 0);
+       term_putc(CONSOLE_HV, &c, 1, 0);
+       rtas_st(vcpu->kvm, rets, 0, 0);
+}
+
+static void rtas_get_term_char(struct kvm_cpu *vcpu,
+                              uint32_t token, uint32_t nargs,
+                              target_ulong args,
+                              uint32_t nret, target_ulong rets)
+{
+       int c;
+       if (term_readable(CONSOLE_HV, 0) &&
+           (c = term_getc(CONSOLE_HV, 0)) >= 0) {
+               rtas_st(vcpu->kvm, rets, 0, 0);
+               rtas_st(vcpu->kvm, rets, 1, c);
+       } else {
+               rtas_st(vcpu->kvm, rets, 0, -2);
+       }
+}
+#endif
+
+static void rtas_get_time_of_day(struct kvm_cpu *vcpu,
+                                 uint32_t token, uint32_t nargs,
+                                 target_ulong args,
+                                 uint32_t nret, target_ulong rets)
+{
+       struct tm tm;
+       time_t tnow;
+
+       if (nret != 8) {
+               rtas_st(vcpu->kvm, rets, 0, -3);
+               return;
+       }
+
+       tnow = time(NULL);
+       /* Guest time is currently not offset in any way. */
+       gmtime_r(&tnow, &tm);
+
+       rtas_st(vcpu->kvm, rets, 0, 0); /* Success */
+       rtas_st(vcpu->kvm, rets, 1, tm.tm_year + 1900);
+       rtas_st(vcpu->kvm, rets, 2, tm.tm_mon + 1);
+       rtas_st(vcpu->kvm, rets, 3, tm.tm_mday);
+       rtas_st(vcpu->kvm, rets, 4, tm.tm_hour);
+       rtas_st(vcpu->kvm, rets, 5, tm.tm_min);
+       rtas_st(vcpu->kvm, rets, 6, tm.tm_sec);
+       rtas_st(vcpu->kvm, rets, 7, 0);
+}
+
+static void rtas_set_time_of_day(struct kvm_cpu *vcpu,
+                                 uint32_t token, uint32_t nargs,
+                                 target_ulong args,
+                                 uint32_t nret, target_ulong rets)
+{
+       pr_warning("%s called; TOD set ignored.\n", __FUNCTION__);
+}
+
+static void rtas_power_off(struct kvm_cpu *vcpu,
+                           uint32_t token, uint32_t nargs, target_ulong args,
+                           uint32_t nret, target_ulong rets)
+{
+       if (nargs != 2 || nret != 1) {
+               rtas_st(vcpu->kvm, rets, 0, -3);
+               return;
+       }
+       kvm_cpu__reboot();
+}
+
+static void rtas_query_cpu_stopped_state(struct kvm_cpu *vcpu,
+                                         uint32_t token, uint32_t nargs,
+                                         target_ulong args,
+                                         uint32_t nret, target_ulong rets)
+{
+       if (nargs != 1 || nret != 2) {
+               rtas_st(vcpu->kvm, rets, 0, -3);
+               return;
+       }
+
+       /*
+        * Can read id = rtas_ld(vcpu->kvm, args, 0), but
+        * we currently start all CPUs.  So just return true.
+        */
+       rtas_st(vcpu->kvm, rets, 0, 0);
+       rtas_st(vcpu->kvm, rets, 1, 2);
+}
+
+static void rtas_start_cpu(struct kvm_cpu *vcpu,
+                           uint32_t token, uint32_t nargs,
+                           target_ulong args,
+                           uint32_t nret, target_ulong rets)
+{
+       die(__FUNCTION__);
+}
+
+target_ulong spapr_rtas_call(struct kvm_cpu *vcpu,
+                             uint32_t token, uint32_t nargs, target_ulong args,
+                             uint32_t nret, target_ulong rets)
+{
+       if ((token >= TOKEN_BASE)
+           && ((token - TOKEN_BASE) < TOKEN_MAX)) {
+               struct rtas_call *call = rtas_table + (token - TOKEN_BASE);
+
+               if (call->fn) {
+                       call->fn(vcpu, token, nargs, args, nret, rets);
+                       return H_SUCCESS;
+               }
+       }
+
+       /*
+        * HACK: Some Linux early debug code uses RTAS display-character,
+        * but assumes the token value is 0xa (which it is on some real
+        * machines) without looking it up in the device tree.  This
+        * special case makes this work
+        */
+       if (token == 0xa) {
+               rtas_display_character(vcpu, 0xa, nargs, args, nret, rets);
+               return H_SUCCESS;
+       }
+
+       hcall_dprintf("Unknown RTAS token 0x%x\n", token);
+       rtas_st(vcpu->kvm, rets, 0, -3);
+       return H_PARAMETER;
+}
+
+void spapr_rtas_register(const char *name, spapr_rtas_fn fn)
+{
+       assert(rtas_next < (rtas_table + TOKEN_MAX));
+
+       rtas_next->name = name;
+       rtas_next->fn = fn;
+
+       rtas_next++;
+}
+
+/*
+ * This is called from the context of an open /rtas node, in order to add
+ * properties for the rtas call tokens.
+ */
+int spapr_rtas_fdt_setup(struct kvm *kvm, void *fdt)
+{
+       int ret;
+       int i;
+
+       for (i = 0; i < TOKEN_MAX; i++) {
+               struct rtas_call *call = &rtas_table[i];
+
+               if (!call->fn) {
+                       continue;
+               }
+
+               ret = fdt_property_cell(fdt, call->name, i + TOKEN_BASE);
+
+               if (ret < 0) {
+                       pr_warning("Couldn't add rtas token for %s: %s\n",
+                                  call->name, fdt_strerror(ret));
+                       return ret;
+               }
+
+       }
+       return 0;
+}
+
+void register_core_rtas(void)
+{
+       spapr_rtas_register("display-character", rtas_display_character);
+       spapr_rtas_register("get-time-of-day", rtas_get_time_of_day);
+       spapr_rtas_register("set-time-of-day", rtas_set_time_of_day);
+       spapr_rtas_register("power-off", rtas_power_off);
+       spapr_rtas_register("query-cpu-stopped-state",
+                           rtas_query_cpu_stopped_state);
+       spapr_rtas_register("start-cpu", rtas_start_cpu);
+#ifdef RTAS_CONSOLE
+       /* These are unused: We do console I/O via hcalls, not rtas. */
+       spapr_rtas_register("put-term-char", rtas_put_term_char);
+       spapr_rtas_register("get-term-char", rtas_get_term_char);
+#endif
+}