From: Scott Wood <scottwood@freescale.com>
To: Alexander Graf <agraf@suse.de>
Cc: Scott Wood <scottwood@freescale.com>,
qemu-ppc@nongnu.org, qemu-devel@nongnu.org
Subject: [Qemu-devel] [RFC PATCH v2 6/6] kvm/openpic: in-kernel mpic support
Date: Mon, 15 Apr 2013 18:19:34 -0500 [thread overview]
Message-ID: <1366067974-5413-7-git-send-email-scottwood@freescale.com> (raw)
In-Reply-To: <1366067974-5413-1-git-send-email-scottwood@freescale.com>
This depends on RFC kernel interfaces proposed at:
http://patchwork.ozlabs.org/patch/236285/
http://patchwork.ozlabs.org/patch/236288/
http://patchwork.ozlabs.org/patch/236290/
TODO: use KVM_IRQ_LINE
Signed-off-by: Scott Wood <scottwood@freescale.com>
---
hw/kvm/Makefile.objs | 1 +
hw/kvm/openpic.c | 259 ++++++++++++++++++++++++++++++++++++++++++++++++++
hw/openpic.h | 1 +
hw/ppc/e500.c | 77 ++++++++++++++-
4 files changed, 333 insertions(+), 5 deletions(-)
create mode 100644 hw/kvm/openpic.c
diff --git a/hw/kvm/Makefile.objs b/hw/kvm/Makefile.objs
index 2a157a6..be95fc1 100644
--- a/hw/kvm/Makefile.objs
+++ b/hw/kvm/Makefile.objs
@@ -4,3 +4,4 @@ endif
obj-$(TARGET_BASE_I386) += clock.o apic.o i8259.o ioapic.o i8254.o pci-assign.o
obj-$(TARGET_ARM) += arm_gic.o
+obj-$(TARGET_PPC) += openpic.o
diff --git a/hw/kvm/openpic.c b/hw/kvm/openpic.c
new file mode 100644
index 0000000..609e268
--- /dev/null
+++ b/hw/kvm/openpic.c
@@ -0,0 +1,259 @@
+/*
+ * KVM in-kernel OpenPIC
+ *
+ * Copyright 2013 Freescale Semiconductor, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include <sys/ioctl.h>
+#include "exec/address-spaces.h"
+#include "hw/hw.h"
+#include "hw/openpic.h"
+#include "hw/pci/msi.h"
+#include "hw/sysbus.h"
+#include "sysemu/kvm.h"
+#include "qemu/log.h"
+
+typedef struct KVMOpenPICState {
+ SysBusDevice busdev;
+ MemoryRegion mem;
+ MemoryListener mem_listener;
+ hwaddr reg_base;
+ uint32_t kern_id;
+ uint32_t model;
+} KVMOpenPICState;
+
+static void kvm_openpic_set_irq(void *opaque, int n_IRQ, int level)
+{
+ KVMOpenPICState *opp = opaque;
+ struct kvm_device_attr attr;
+ uint32_t val32 = level;
+ int ret;
+
+ attr.group = KVM_DEV_MPIC_GRP_IRQ_ACTIVE;
+ attr.attr = n_IRQ;
+ attr.addr = (uint64_t)(long)&val32;
+
+ ret = ioctl(opp->kern_id, KVM_SET_DEVICE_ATTR, &attr);
+ if (ret < 0) {
+ fprintf(stderr, "%s: %s %llx\n", __func__, strerror(errno), attr.attr);
+ }
+}
+
+static void kvm_openpic_reset(DeviceState *d)
+{
+ qemu_log_mask(LOG_UNIMP, "%s: unimplemented\n", __func__);
+}
+
+static void kvm_openpic_write(void *opaque, hwaddr addr, uint64_t val,
+ unsigned size)
+{
+ KVMOpenPICState *opp = opaque;
+ struct kvm_device_attr attr;
+ uint32_t val32 = val;
+ int ret;
+
+ attr.group = KVM_DEV_MPIC_GRP_REGISTER;
+ attr.attr = addr;
+ attr.addr = (uint64_t)(long)&val32;
+
+ ret = ioctl(opp->kern_id, KVM_SET_DEVICE_ATTR, &attr);
+ if (ret < 0) {
+ qemu_log_mask(LOG_UNIMP, "%s: %s %llx\n", __func__,
+ strerror(errno), attr.attr);
+ }
+}
+
+static uint64_t kvm_openpic_read(void *opaque, hwaddr addr, unsigned size)
+{
+ KVMOpenPICState *opp = opaque;
+ struct kvm_device_attr attr;
+ uint32_t val = 0xdeadbeef;
+ int ret;
+
+ attr.group = KVM_DEV_MPIC_GRP_REGISTER;
+ attr.attr = addr;
+ attr.addr = (uint64_t)(long)&val;
+
+ ret = ioctl(opp->kern_id, KVM_GET_DEVICE_ATTR, &attr);
+ if (ret < 0) {
+ qemu_log_mask(LOG_UNIMP, "%s: %s %llx\n", __func__,
+ strerror(errno), attr.attr);
+ return 0;
+ }
+
+ return val;
+}
+
+static const MemoryRegionOps kvm_openpic_mem_ops = {
+ .write = kvm_openpic_write,
+ .read = kvm_openpic_read,
+ .endianness = DEVICE_BIG_ENDIAN,
+ .impl = {
+ .min_access_size = 4,
+ .max_access_size = 4,
+ },
+};
+
+static void kvm_openpic_update_reg_base(MemoryListener *listener)
+{
+ KVMOpenPICState *opp = container_of(listener, KVMOpenPICState,
+ mem_listener);
+ struct kvm_device_attr attr;
+ uint64_t reg_base;
+ AddressSpace *as;
+ int ret;
+
+ reg_base = memory_region_to_address(&opp->mem, &as);
+ if (!as) {
+ reg_base = 0;
+ } else if (as != &address_space_memory) {
+ abort();
+ }
+
+ if (reg_base == opp->reg_base) {
+ return;
+ }
+
+ opp->reg_base = reg_base;
+
+ attr.group = KVM_DEV_MPIC_GRP_MISC;
+ attr.attr = KVM_DEV_MPIC_BASE_ADDR;
+ attr.addr = (uint64_t)(long)®_base;
+
+ ret = ioctl(opp->kern_id, KVM_SET_DEVICE_ATTR, &attr);
+ if (ret < 0) {
+ fprintf(stderr, "%s: %s %llx\n", __func__, strerror(errno), reg_base);
+ }
+}
+
+static int kvm_openpic_init(SysBusDevice *dev)
+{
+ KVMOpenPICState *opp = FROM_SYSBUS(typeof(*opp), dev);
+ int kvm_openpic_model;
+
+ memory_region_init_io(&opp->mem, &kvm_openpic_mem_ops, opp,
+ "kvm-openpic", 0x40000);
+
+ switch (opp->model) {
+ case OPENPIC_MODEL_FSL_MPIC_20:
+ kvm_openpic_model = KVM_DEV_TYPE_FSL_MPIC_20;
+ break;
+
+ case OPENPIC_MODEL_FSL_MPIC_42:
+ kvm_openpic_model = KVM_DEV_TYPE_FSL_MPIC_42;
+ break;
+
+ default:
+ return -EINVAL;
+ }
+
+ sysbus_init_mmio(dev, &opp->mem);
+ qdev_init_gpio_in(&dev->qdev, kvm_openpic_set_irq, OPENPIC_MAX_IRQ);
+
+ opp->mem_listener.commit = kvm_openpic_update_reg_base;
+ memory_listener_register(&opp->mem_listener, &address_space_memory);
+
+ msi_supported = true;
+ return 0;
+}
+
+int kvm_openpic_connect_vcpu(DeviceState *d, CPUState *cs)
+{
+ KVMOpenPICState *opp = FROM_SYSBUS(typeof(*opp), SYS_BUS_DEVICE(d));
+ struct kvm_enable_cap encap = {};
+
+ encap.cap = KVM_CAP_IRQ_MPIC;
+ encap.args[0] = opp->kern_id;
+ encap.args[1] = cs->cpu_index;
+
+ return kvm_vcpu_ioctl(cs, KVM_ENABLE_CAP, &encap);
+}
+
+DeviceState *kvm_openpic_create(BusState *bus, int model)
+{
+ KVMState *s = kvm_state;
+ DeviceState *dev;
+ struct kvm_create_device cd = {0};
+ int ret;
+
+ if (!kvm_check_extension(s, KVM_CAP_DEVICE_CTRL)) {
+ return NULL;
+ }
+
+ switch (model) {
+ case OPENPIC_MODEL_FSL_MPIC_20:
+ cd.type = KVM_DEV_TYPE_FSL_MPIC_20;
+ break;
+
+ case OPENPIC_MODEL_FSL_MPIC_42:
+ cd.type = KVM_DEV_TYPE_FSL_MPIC_42;
+ break;
+
+ default:
+ qemu_log_mask(LOG_UNIMP, "%s: unknown openpic model %d\n",
+ __func__, model);
+ return NULL;
+ }
+
+ ret = kvm_vm_ioctl(s, KVM_CREATE_DEVICE, &cd);
+ if (ret < 0) {
+ qemu_log_mask(LOG_UNIMP, "%s: can't create device %d: %s\n",
+ __func__, cd.type, strerror(errno));
+ return NULL;
+ }
+
+ dev = qdev_create(NULL, "kvm-openpic");
+ qdev_prop_set_uint32(dev, "model", model);
+ qdev_prop_set_uint32(dev, "kernel-id", cd.fd);
+
+ return dev;
+}
+
+static Property kvm_openpic_properties[] = {
+ DEFINE_PROP_UINT32("model", KVMOpenPICState, model,
+ OPENPIC_MODEL_FSL_MPIC_20),
+ DEFINE_PROP_UINT32("kernel-id", KVMOpenPICState, kern_id, 0),
+ DEFINE_PROP_END_OF_LIST(),
+};
+
+static void kvm_openpic_class_init(ObjectClass *klass, void *data)
+{
+ DeviceClass *dc = DEVICE_CLASS(klass);
+ SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
+
+ k->init = kvm_openpic_init;
+ dc->props = kvm_openpic_properties;
+ dc->reset = kvm_openpic_reset;
+}
+
+static const TypeInfo kvm_openpic_info = {
+ .name = "kvm-openpic",
+ .parent = TYPE_SYS_BUS_DEVICE,
+ .instance_size = sizeof(KVMOpenPICState),
+ .class_init = kvm_openpic_class_init,
+};
+
+static void kvm_openpic_register_types(void)
+{
+ type_register_static(&kvm_openpic_info);
+}
+
+type_init(kvm_openpic_register_types)
diff --git a/hw/openpic.h b/hw/openpic.h
index d873bb6..1a27efa 100644
--- a/hw/openpic.h
+++ b/hw/openpic.h
@@ -25,5 +25,6 @@ enum {
OPENPIC_MAX_TMR)
DeviceState *kvm_openpic_create(BusState *bus, int model);
+int kvm_openpic_connect_vcpu(DeviceState *d, CPUState *cs);
#endif /* __OPENPIC_H__ */
diff --git a/hw/ppc/e500.c b/hw/ppc/e500.c
index 09dbf7f..1697b44 100644
--- a/hw/ppc/e500.c
+++ b/hw/ppc/e500.c
@@ -448,18 +448,17 @@ static void ppce500_cpu_reset(void *opaque)
mmubooke_create_initial_mapping(env);
}
-static qemu_irq *ppce500_init_mpic(PPCE500Params *params, MemoryRegion *ccsr,
- qemu_irq **irqs)
+static DeviceState *ppce500_init_mpic_qemu(PPCE500Params *params,
+ qemu_irq **irqs)
{
- qemu_irq *mpic;
DeviceState *dev;
SysBusDevice *s;
int i, j, k;
- mpic = g_new(qemu_irq, 256);
dev = qdev_create(NULL, "openpic");
- qdev_prop_set_uint32(dev, "nb_cpus", smp_cpus);
qdev_prop_set_uint32(dev, "model", params->mpic_version);
+ qdev_prop_set_uint32(dev, "nb_cpus", smp_cpus);
+
qdev_init_nofail(dev);
s = SYS_BUS_DEVICE(dev);
@@ -470,10 +469,78 @@ static qemu_irq *ppce500_init_mpic(PPCE500Params *params, MemoryRegion *ccsr,
}
}
+ return dev;
+}
+
+static DeviceState *ppce500_init_mpic_kvm(PPCE500Params *params,
+ qemu_irq **irqs)
+{
+ DeviceState *dev;
+ CPUPPCState *env;
+ CPUState *cs;
+
+ dev = kvm_openpic_create(NULL, params->mpic_version);
+ if (!dev) {
+ return NULL;
+ }
+
+ qdev_init_nofail(dev);
+
+ for (env = first_cpu; env != NULL; env = env->next_cpu) {
+ cs = ENV_GET_CPU(env);
+
+ if (kvm_openpic_connect_vcpu(dev, cs)) {
+ fprintf(stderr, "%s: failed to connect vcpu to irqchip\n",
+ __func__);
+ abort();
+ }
+ }
+
+ return dev;
+}
+
+static qemu_irq *ppce500_init_mpic(PPCE500Params *params, MemoryRegion *ccsr,
+ qemu_irq **irqs)
+{
+ QemuOptsList *list;
+ qemu_irq *mpic;
+ DeviceState *dev = NULL;
+ SysBusDevice *s;
+ int i;
+
+ mpic = g_new(qemu_irq, 256);
+
+ if (kvm_enabled()) {
+ bool irqchip_allowed = true, irqchip_required = false;
+
+ list = qemu_find_opts("machine");
+ if (!QTAILQ_EMPTY(&list->head)) {
+ irqchip_allowed = qemu_opt_get_bool(QTAILQ_FIRST(&list->head),
+ "kernel_irqchip", true);
+ irqchip_required = qemu_opt_get_bool(QTAILQ_FIRST(&list->head),
+ "kernel_irqchip", false);
+ }
+
+ if (irqchip_allowed) {
+ dev = ppce500_init_mpic_kvm(params, irqs);
+ }
+
+ if (irqchip_required && !dev) {
+ fprintf(stderr, "%s: irqchip requested but unavailable\n",
+ __func__);
+ abort();
+ }
+ }
+
+ if (!dev) {
+ dev = ppce500_init_mpic_qemu(params, irqs);
+ }
+
for (i = 0; i < 256; i++) {
mpic[i] = qdev_get_gpio_in(dev, i);
}
+ s = SYS_BUS_DEVICE(dev);
memory_region_add_subregion(ccsr, MPC8544_MPIC_REGS_OFFSET,
s->mmio[0].memory);
--
1.7.10.4
prev parent reply other threads:[~2013-04-15 23:20 UTC|newest]
Thread overview: 40+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <1360823521-32306-1-git-send-email-scottwood@freescale.com>
[not found] ` <1360823521-32306-3-git-send-email-scottwood@freescale.com>
2013-03-21 8:30 ` [Qemu-devel] [RFC ppc-next PATCH 2/6] kvm: hw/kvm is not x86-specific Alexander Graf
[not found] ` <1360823521-32306-4-git-send-email-scottwood@freescale.com>
2013-03-21 8:31 ` [Qemu-devel] [RFC ppc-next PATCH 3/6] memory: add memory_region_to_address() Alexander Graf
2013-03-21 10:53 ` Peter Maydell
2013-03-21 10:59 ` Alexander Graf
2013-03-21 11:01 ` Peter Maydell
2013-03-21 11:05 ` Alexander Graf
2013-03-21 11:09 ` Peter Maydell
2013-03-21 11:14 ` Alexander Graf
2013-03-21 11:22 ` Peter Maydell
2013-03-21 11:29 ` Alexander Graf
2013-03-21 11:32 ` Peter Maydell
2013-03-21 11:38 ` Alexander Graf
2013-03-21 11:44 ` Peter Maydell
2013-03-21 11:49 ` Alexander Graf
2013-03-21 11:51 ` [Qemu-devel] [Qemu-ppc] " Alexander Graf
2013-03-21 22:43 ` Scott Wood
2013-03-22 13:08 ` Peter Maydell
2013-03-22 22:05 ` Scott Wood
2013-03-23 11:24 ` Peter Maydell
2013-03-25 18:23 ` Scott Wood
2013-03-21 11:53 ` [Qemu-devel] " Peter Maydell
[not found] ` <1360823521-32306-6-git-send-email-scottwood@freescale.com>
2013-03-21 8:34 ` [Qemu-devel] [RFC ppc-next PATCH 5/6] kvm: export result of irqchip config check Alexander Graf
2013-03-21 8:45 ` Jan Kiszka
2013-03-21 8:50 ` Alexander Graf
[not found] ` <1360823521-32306-7-git-send-email-scottwood@freescale.com>
2013-03-21 8:41 ` [Qemu-devel] [RFC ppc-next PATCH 6/6] kvm/openpic: in-kernel mpic support Alexander Graf
2013-03-21 20:50 ` Scott Wood
2013-03-21 21:29 ` Alexander Graf
2013-03-21 21:59 ` Scott Wood
2013-03-21 23:45 ` Alexander Graf
2013-03-22 22:51 ` Scott Wood
2013-04-15 23:19 ` [Qemu-devel] [RFC PATCH v2 0/6] kvm/openpic: in-kernel irqchip Scott Wood
2013-04-15 23:19 ` [Qemu-devel] [RFC PATCH v2 1/6] kvm: update linux-headers Scott Wood
2013-04-15 23:19 ` [Qemu-devel] [RFC PATCH v2 2/6] kvm: use hw/kvm/Makefile.objs consistently for all relevant architectures Scott Wood
2013-04-15 23:19 ` [Qemu-devel] [RFC PATCH v2 3/6] memory: add memory_region_to_address() Scott Wood
2013-04-16 8:25 ` Peter Maydell
2013-04-17 0:10 ` Scott Wood
2013-04-17 9:14 ` Peter Maydell
2013-04-15 23:19 ` [Qemu-devel] [RFC PATCH v2 4/6] openpic: factor out some common defines into openpic.h Scott Wood
2013-04-15 23:19 ` [Qemu-devel] [RFC PATCH v2 5/6] PPC: e500: factor out mpic init code Scott Wood
2013-04-15 23:19 ` Scott Wood [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1366067974-5413-7-git-send-email-scottwood@freescale.com \
--to=scottwood@freescale.com \
--cc=agraf@suse.de \
--cc=qemu-devel@nongnu.org \
--cc=qemu-ppc@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).