From: Eric Auger <eric.auger@linaro.org>
To: eric.auger@st.com, christoffer.dall@linaro.org,
qemu-devel@nongnu.org, kim.phillips@freescale.com,
a.rigo@virtualopensystems.com
Cc: peter.maydell@linaro.org, eric.auger@linaro.org,
patches@linaro.org, will.deacon@arm.com, agraf@suse.de,
stuart.yoder@freescale.com, Bharat.Bhushan@freescale.com,
alex.williamson@redhat.com, a.motakis@virtualopensystems.com,
kvmarm@lists.cs.columbia.edu
Subject: [Qemu-devel] [RFC v4 12/13] hw/vfio/platform: add default dt generation for vfio device
Date: Mon, 7 Jul 2014 13:27:22 +0100 [thread overview]
Message-ID: <1404736043-22900-13-git-send-email-eric.auger@linaro.org> (raw)
In-Reply-To: <1404736043-22900-1-git-send-email-eric.auger@linaro.org>
implement a default dt generation for VFIO platform device. This
enables dynamic instantiation. If the device tree generation does
not match the expectation it is possible to derive a component
and write one's own fdt_add_device_node method.
Signed-off-by: Alvise Rigo <a.rigo@virtualopensystems.com>
Signed-off-by: Eric Auger <eric.auger@linaro.org>
---
hw/vfio/platform.c | 143 ++++++++++++++++++++++++++++++++++++++++
include/hw/vfio/vfio-platform.h | 1 +
2 files changed, 144 insertions(+)
diff --git a/hw/vfio/platform.c b/hw/vfio/platform.c
index fb0f7c9..447ce50 100644
--- a/hw/vfio/platform.c
+++ b/hw/vfio/platform.c
@@ -22,6 +22,7 @@
#include "qemu/range.h"
#include "sysemu/sysemu.h"
#include "hw/vfio/vfio-platform.h"
+#include "hw/misc/platform_devices.h"
extern const MemoryRegionOps vfio_region_ops;
extern const MemoryListener vfio_memory_listener;
@@ -573,6 +574,142 @@ static void vfio_platform_realize(DeviceState *dev, Error **errp)
}
}
+static char *format_compat(char * compat)
+{
+ char *str_ptr, *corrected_compat;
+ /*
+ * process compatibility property string passed by end-user
+ * replaces / by , and ; by NUL character
+ */
+ corrected_compat = g_strdup(compat);
+ /*
+ * the total length of the string has to include also the last
+ * NUL char.
+ */
+
+ str_ptr = corrected_compat;
+ while ((str_ptr = strchr(str_ptr, '/')) != NULL) {
+ *str_ptr = ',';
+ }
+
+ /* substitute ";" with the NUL char */
+ str_ptr = corrected_compat;
+ while ((str_ptr = strchr(str_ptr, ';')) != NULL) {
+ *str_ptr = '\0';
+ }
+
+ return corrected_compat;
+}
+
+static void wrap_fdt_add_node(SysBusDevice *sbdev, void *opaque)
+{
+ PlatformDevtreeData *data = opaque;
+ VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(sbdev);
+ VFIOPlatformDeviceClass *vdevc = VFIO_PLATFORM_DEVICE_GET_CLASS(sbdev);
+ VFIODevice *vbasedev = &vdev->vbasedev;
+ gchar irq_number_prop[8];
+ Object *obj = OBJECT(sbdev);
+ char *corrected_compat;
+ uint64_t irq_number;
+ int compat_str_len = strlen(vdev->compat)+1;
+ int i;
+
+ corrected_compat = format_compat(vdev->compat);
+ snprintf(vdev->compat, compat_str_len, "%s", corrected_compat);
+ g_free(corrected_compat);
+
+ vdevc->fdt_add_device_node(sbdev, opaque);
+
+ for (i = 0; i < vbasedev->num_irqs; i++) {
+ snprintf(irq_number_prop, sizeof(irq_number_prop), "irq[%d]", i);
+ irq_number = object_property_get_int(obj, irq_number_prop, NULL)
+ + data->irq_start;
+ /*
+ * this is a hack: this does not relate to dt creation
+ * for setting up irqfd we must give the virtual IRQ number
+ * which is the sum of irq_start and actual platform bus irq
+ * index. at realize point we do not have this info.
+ * in case of static instantiation, the irq connect is not yet
+ * done. In case of dynamic instantiation it was done but we
+ * miss irq_start which is not stored in the sysbus device.
+ */
+ if (vdev->irqfd_allowed) {
+ vfio_setup_irqfd(sbdev, i, irq_number);
+ }
+ }
+}
+
+static void default_fdt_add_device_node(SysBusDevice *sbdev, void *opaque)
+{
+ PlatformDevtreeData *data = opaque;
+ void *fdt = data->fdt;
+ const char *parent_node = data->node;
+ uint64_t platform_bus_base = data->platform_bus_base;
+ int compat_str_len;
+ char *nodename;
+ int i, ret;
+ uint32_t *irq_attr;
+ uint64_t *reg_attr;
+ uint64_t mmio_base;
+ uint64_t irq_number;
+ gchar mmio_base_prop[8];
+ gchar irq_number_prop[8];
+ VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(sbdev);
+ VFIODevice *vbasedev = &vdev->vbasedev;
+ Object *obj = OBJECT(sbdev);
+
+ mmio_base = object_property_get_int(obj, "mmio[0]", NULL);
+
+ nodename = g_strdup_printf("%s/%s@%" PRIx64, parent_node,
+ vbasedev->name,
+ mmio_base + platform_bus_base);
+
+ qemu_fdt_add_subnode(fdt, nodename);
+
+ compat_str_len = strlen(vdev->compat) + 1;
+ qemu_fdt_setprop(fdt, nodename, "compatible",
+ vdev->compat, compat_str_len);
+
+ reg_attr = g_new(uint64_t, vbasedev->num_regions*4);
+
+ for (i = 0; i < vbasedev->num_regions; i++) {
+ snprintf(mmio_base_prop, sizeof(mmio_base_prop), "mmio[%d]", i);
+ mmio_base = object_property_get_int(obj, mmio_base_prop, NULL);
+ reg_attr[4*i] = 2;
+ reg_attr[4*i+1] = mmio_base + platform_bus_base;
+ reg_attr[4*i+2] = 2;
+ reg_attr[4*i+3] = memory_region_size(&vdev->regions[i]->mem);
+ }
+
+ ret = qemu_fdt_setprop_sized_cells_from_array(fdt, nodename, "reg",
+ vbasedev->num_regions*2, reg_attr);
+ if (ret < 0) {
+ error_report("could not set reg property of node %s", nodename);
+ }
+
+ irq_attr = g_new(uint32_t, vbasedev->num_irqs*3);
+
+ for (i = 0; i < vbasedev->num_irqs; i++) {
+ snprintf(irq_number_prop, sizeof(irq_number_prop), "irq[%d]", i);
+ irq_number = object_property_get_int(obj, irq_number_prop, NULL)
+ + data->irq_start;
+ irq_attr[3*i] = cpu_to_be32(0);
+ irq_attr[3*i+1] = cpu_to_be32(irq_number);
+ irq_attr[3*i+2] = cpu_to_be32(0x4);
+ }
+
+ ret = qemu_fdt_setprop(fdt, nodename, "interrupts",
+ irq_attr, vbasedev->num_irqs*3*sizeof(uint32_t));
+ if (ret < 0) {
+ error_report("could not set interrupts property of node %s",
+ nodename);
+ }
+
+ g_free(nodename);
+ g_free(irq_attr);
+ g_free(reg_attr);
+}
+
static const VMStateDescription vfio_platform_vmstate = {
.name = TYPE_VFIO_PLATFORM,
.version_id = 3,
@@ -599,6 +736,12 @@ static Property vfio_platform_dev_properties[] = {
static void vfio_platform_class_init(ObjectClass *klass, void *data)
{
DeviceClass *dc = DEVICE_CLASS(klass);
+ SysBusDeviceClass *sbdevc = SYS_BUS_DEVICE_CLASS(klass);
+ VFIOPlatformDeviceClass *vdc = VFIO_PLATFORM_DEVICE_CLASS(klass);
+
+ /* substitute wrap_fdt_add_node to parent fdt_add_node */
+ sbdevc->fdt_add_node = wrap_fdt_add_node;
+ vdc->fdt_add_device_node = default_fdt_add_device_node;
dc->realize = vfio_platform_realize;
dc->props = vfio_platform_dev_properties;
diff --git a/include/hw/vfio/vfio-platform.h b/include/hw/vfio/vfio-platform.h
index 134fc1e..1e9a5f4 100644
--- a/include/hw/vfio/vfio-platform.h
+++ b/include/hw/vfio/vfio-platform.h
@@ -59,6 +59,7 @@ typedef struct VFIOPlatformDeviceClass {
/*< private >*/
SysBusDeviceClass parent_class;
/*< public >*/
+ void (*fdt_add_device_node)(SysBusDevice *sbdev, void *opaque);
} VFIOPlatformDeviceClass;
#define VFIO_PLATFORM_DEVICE(obj) \
--
1.8.3.2
next prev parent reply other threads:[~2014-07-07 12:31 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-07-07 12:27 [Qemu-devel] [RFC v4 00/13] KVM platform device passthrough Eric Auger
2014-07-07 12:27 ` [Qemu-devel] [RFC v4 01/13] vfio: move hw/misc/vfio.c to hw/vfio/pci.c Move vfio.h into include/hw/vfio Eric Auger
2014-07-07 12:27 ` [Qemu-devel] [RFC v4 02/13] hw/vfio/pci: Rename VFIODevice into VFIOPCIDevice Eric Auger
2014-07-07 12:27 ` [Qemu-devel] [RFC v4 03/13] hw/vfio/pci: Remove unneeded include files Eric Auger
2014-07-08 18:55 ` Alex Williamson
2014-07-23 9:59 ` Eric Auger
2014-07-07 12:27 ` [Qemu-devel] [RFC v4 04/13] hw/vfio/pci: introduce VFIODevice Eric Auger
2014-07-08 22:41 ` Alex Williamson
2014-07-23 10:02 ` Eric Auger
2014-07-23 10:24 ` Peter Maydell
2014-07-23 11:40 ` Eric Auger
2014-07-07 12:27 ` [Qemu-devel] [RFC v4 05/13] hw/vfio/pci: Introduce VFIORegion Eric Auger
2014-07-08 22:41 ` Alex Williamson
2014-07-23 13:50 ` Eric Auger
2014-07-07 12:27 ` [Qemu-devel] [RFC v4 06/13] hw/vfio/pci: split vfio_get_device Eric Auger
2014-07-08 22:43 ` Alex Williamson
2014-07-24 9:51 ` Eric Auger
2014-07-24 10:25 ` Eric Auger
2014-07-07 12:27 ` [Qemu-devel] [RFC v4 07/13] hw/vfio: create common module Eric Auger
2014-07-07 12:27 ` [Qemu-devel] [RFC v4 08/13] hw/vfio/common: Add EXEC_FLAG to VFIO DMA mappings Eric Auger
2014-07-07 12:40 ` Peter Maydell
2014-07-07 12:49 ` Will Deacon
2014-07-07 13:25 ` Alvise Rigo
2014-07-07 13:29 ` Eric Auger
2014-07-07 12:27 ` [Qemu-devel] [RFC v4 09/13] hw/vfio/platform: add vfio-platform support Eric Auger
2014-07-07 12:27 ` [Qemu-devel] [RFC v4 10/13] hw/intc/arm_gic_kvm: enable irqfd and set routing table Eric Auger
2014-07-07 12:27 ` [Qemu-devel] [RFC v4 11/13] hw/vfio/platform: Add irqfd support Eric Auger
2014-07-07 12:27 ` Eric Auger [this message]
2014-07-07 12:27 ` [Qemu-devel] [RFC v4 13/13] hw/vfio: add an example calxeda_xgmac Eric Auger
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=1404736043-22900-13-git-send-email-eric.auger@linaro.org \
--to=eric.auger@linaro.org \
--cc=Bharat.Bhushan@freescale.com \
--cc=a.motakis@virtualopensystems.com \
--cc=a.rigo@virtualopensystems.com \
--cc=agraf@suse.de \
--cc=alex.williamson@redhat.com \
--cc=christoffer.dall@linaro.org \
--cc=eric.auger@st.com \
--cc=kim.phillips@freescale.com \
--cc=kvmarm@lists.cs.columbia.edu \
--cc=patches@linaro.org \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=stuart.yoder@freescale.com \
--cc=will.deacon@arm.com \
/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).