Skip to contentSkip to conversation

virtio-mmio: Kill code duplication

PATCH In review Pavel Fedin 2 participants 3 messages Aug 19, 2015 Arm/versatile express platform

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 virtio-mmio: Kill code duplication

Extract common code for virtio-mmio creation and FDT node addition and put it into reusable functions. Use new functions in vexpress and virt machines.

---
 hw/arm/sysbus-fdt.c             | 51 +++++++++++++++++++++++++++++++
 hw/arm/vexpress.c               | 55 ++++-----------------------------
 hw/arm/virt-acpi-build.c        | 13 ++++----
 hw/arm/virt.c                   | 68 +++++------------------------------------
 hw/virtio/virtio-mmio.c         | 41 ++++++++++++++++++++++++-
 include/hw/arm/sysbus-fdt.h     | 16 ++++++++++
 include/hw/virtio/virtio-mmio.h | 38 +++++++++++++++++++++++
 7 files changed, 165 insertions(+), 117 deletions(-)
 create mode 100644 include/hw/virtio/virtio-mmio.h
hw/arm/sysbus-fdt.c +51 -0
index 9d28797..c35abdb 100644
@@ -28,6 +28,7 @@
 #include "sysemu/sysemu.h"
 #include "hw/vfio/vfio-platform.h"
 #include "hw/vfio/vfio-calxeda-xgmac.h"
+#include "hw/virtio/virtio-mmio.h"
 #include "hw/arm/fdt.h"
 
 /*
@@ -245,3 +246,53 @@ void arm_register_platform_bus_fdt_creator(ARMPlatformBusFDTParams *fdt_params)
     p->notifier.notify = platform_bus_fdt_notify;
     qemu_add_machine_init_done_notifier(&p->notifier);
 }
+
+int add_virtio_mmio_fdt_nodes(hwaddr addr, int irq, unsigned int num,
+                              void *fdt, int intc)
+{
+    uint32_t acells = qemu_fdt_getprop_cell(fdt, "/", "#address-cells");
+    uint32_t scells = qemu_fdt_getprop_cell(fdt, "/", "#size-cells");
+    int i;
+
+    /* We add dtb nodes in reverse order so that they appear in the finished
+     * device tree lowest address first.
+     *
+     * Note that this mapping is independent of the virtio_mmio_create(). That
+     * loop influences virtio device to virtio transport assignment, whereas
+     * this loop controls how virtio transports are laid out in the dtb.
+     */
+    for (i = num - 1; i >= 0; i--) {
+        hwaddr base = addr + VIRTIO_MMIO_SIZE * i;
+        char *nodename = g_strdup_printf("/virtio_mmio@%" PRIx64, base);
+        int rc;
+
+        /* Add a virtio_mmio node to the device tree blob:
+         *   virtio_mmio@ADDRESS {
+         *       compatible = "virtio,mmio";
+         *       reg = <ADDRESS, SIZE>;
+         *       interrupt-parent = <&intc>;
+         *       interrupts = <0, irq, 1>;
+         *   }
+         * (Note that the format of the interrupts property is dependent on the
+         * interrupt controller that interrupt-parent points to; these are for
+         * the ARM GIC and indicate an SPI interrupt, rising-edge-triggered.)
+         */
+        rc = qemu_fdt_add_subnode(fdt, nodename);
+        rc |= qemu_fdt_setprop_string(fdt, nodename,
+                                      "compatible", "virtio,mmio");
+        rc |= qemu_fdt_setprop_sized_cells(fdt, nodename, "reg", acells, base,
+                                           scells, VIRTIO_MMIO_SIZE);
+        if (intc) {
+            qemu_fdt_setprop_cells(fdt, nodename, "interrupt-parent", intc);
+        }
+        qemu_fdt_setprop_cells(fdt, nodename, "interrupts",
+                               GIC_FDT_IRQ_TYPE_SPI, irq + i,
+                               GIC_FDT_IRQ_FLAGS_EDGE_LO_HI);
+        g_free(nodename);
+        if (rc) {
+            return -1;
+        }
+    }
+
+    return 0;
+}
hw/arm/vexpress.c +6 -49

90 lines not on this page · raw message

hw/arm/virt-acpi-build.c +7 -6
index 9088248..039c9dc 100644
@@ -41,6 +41,7 @@
 #include "hw/acpi/aml-build.h"
 #include "hw/pci/pcie_host.h"
 #include "hw/pci/pci.h"
+#include "hw/virtio/virtio-mmio.h"
 
 #define ARM_SPI_BASE 32
 
@@ -136,11 +137,10 @@ static void acpi_dsdt_add_flash(Aml *scope, const MemMapEntry *flash_memmap)
 
 static void acpi_dsdt_add_virtio(Aml *scope,
                                  const MemMapEntry *virtio_mmio_memmap,
-                                 int mmio_irq, int num)
+                                 int irq)
 {
     hwaddr base = virtio_mmio_memmap->base;
-    hwaddr size = virtio_mmio_memmap->size;
-    int irq = mmio_irq;
+    int num = virtio_mmio_memmap->size / VIRTIO_MMIO_SIZE;
     int i;
 
     for (i = 0; i < num; i++) {
@@ -149,13 +149,14 @@ static void acpi_dsdt_add_virtio(Aml *scope,
         aml_append(dev, aml_name_decl("_UID", aml_int(i)));
 
         Aml *crs = aml_resource_template();
-        aml_append(crs, aml_memory32_fixed(base, size, AML_READ_WRITE));
+        aml_append(crs, aml_memory32_fixed(base, VIRTIO_MMIO_SIZE,
+                                           AML_READ_WRITE));
         aml_append(crs,
                    aml_interrupt(AML_CONSUMER, AML_LEVEL, AML_ACTIVE_HIGH,
                                  AML_EXCLUSIVE, irq + i));
         aml_append(dev, aml_name_decl("_CRS", crs));
         aml_append(scope, dev);
-        base += size;
+        base += VIRTIO_MMIO_SIZE;
     }
 }
 
@@ -521,7 +522,7 @@ build_dsdt(GArray *table_data, GArray *linker, VirtGuestInfo *guest_info)
                       (irqmap[VIRT_RTC] + ARM_SPI_BASE));
     acpi_dsdt_add_flash(scope, &memmap[VIRT_FLASH]);
     acpi_dsdt_add_virtio(scope, &memmap[VIRT_MMIO],
-                    (irqmap[VIRT_MMIO] + ARM_SPI_BASE), NUM_VIRTIO_TRANSPORTS);
+                         irqmap[VIRT_MMIO] + ARM_SPI_BASE);
     acpi_dsdt_add_pci(scope, memmap, (irqmap[VIRT_PCIE] + ARM_SPI_BASE),
                       guest_info->use_highmem);
 
hw/arm/virt.c +7 -61

87 lines not on this page · raw message

hw/virtio/virtio-mmio.c +40 -1

58 lines not on this page · raw message

include/hw/arm/sysbus-fdt.h +16 -0
index e15bb81..5646d85 100644
@@ -57,4 +57,20 @@ typedef struct {
  */
 void arm_register_platform_bus_fdt_creator(ARMPlatformBusFDTParams *fdt_params);
 
+/**
+ * add_virtio_mmio_fdt_nodes:
+ * @addr: Starting physical address of virtio region
+ * @irq: IRQ number for the first device in a row
+ * @num: Number of virtio devices
+ * @fdt: Opaque pointer to the device tree
+ * @intc: Optional handle to put as a value of interrupt-parent
+ * property
+ *
+ * Insert description of virtio-mmio devices into device tree
+ *
+ * Returns 0 on success, -1 otherwise.
+ */
+int add_virtio_mmio_fdt_nodes(hwaddr addr, int irq, unsigned int num,
+                              void *fdt, int intc);
+
 #endif
include/hw/virtio/virtio-mmio.h new file +38 -0
new file mode 100644
index 0000000..d54ecc3
@@ -0,0 +1,38 @@
+/*
+ * Virtio-mmio public utilities
+ *
+ * Copyright (c) 2015 Samsung Electronics Co. Ltd.
+ *
+ * Author:
+ *  Pavel Fedin <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef _QEMU_VIRTIO_MMIO_H
+#define _QEMU_VIRTIO_MMIO_H
+
+#include "hw/irq.h"
+
+#define VIRTIO_MMIO_SIZE 0x0200
+
+/**
+ * virtio_mmio_create:
+ * @addr: Starting physical address of virtio region
+ * @irqs: Array of IRQ objects, one per virtio device
+ * @num: Number of virtio devices to create
+ *
+ * Create a bunch of virtio-mmio devices
+ */
+void virtio_mmio_create(hwaddr addr, qemu_irq *irqs, unsigned int num);
+
+#endif
Signature · 1 line
1.9.5.msysgit.0
#1 lore raw
Quoted 5 lines · Pavel Fedin

Extract common code for virtio-mmio creation and FDT node addition and put it into reusable functions. Use new functions in vexpress and virt machines.

Quoted 9 lines
---
 hw/arm/sysbus-fdt.c             | 51 +++++++++++++++++++++++++++++++
 hw/arm/vexpress.c               | 55 ++++-----------------------------
 hw/arm/virt-acpi-build.c        | 13 ++++----
 hw/arm/virt.c                   | 68 +++++------------------------------------
 hw/virtio/virtio-mmio.c         | 41 ++++++++++++++++++++++++-
 include/hw/arm/sysbus-fdt.h     | 16 ++++++++++
 include/hw/virtio/virtio-mmio.h | 38 +++++++++++++++++++++++
 7 files changed, 165 insertions(+), 117 deletions(-)

Why bother? This is adding more code than it deletes, and is implicitly tying together details of how the these two boards are laid out -- there's no inherent reason that they have to be the same.

-- PMM

#2 lore raw

Hello!

Quoted 1 line

Why bother? This is adding more code than it deletes

I just don't like code duplication, wanted to do this long time ago. Additionally this enables to add support for virtio-mmio to more machines. Actually it could be used not only by ARM with little modifications.

Quoted 2 lines

and is implicitly tying together details of how the these two boards are laid out

What exactly do you mean? The only thing assumed by new functions is that
virtio-mmio devices are laid out in a single addresses window, one after another. Base address and irq do not have to be the same everywhere.

Kind regards,
Pavel Fedin
Expert Engineer
Samsung Electronics Research center Russia