Creates set_interrupts_fdt_node and set_regions_fdt_node
for code reusability.

Signed-off-by: Baptiste Reynal <b.rey...@virtualopensystems.com>

---
v2 -> v3:
handle intermediate ret values in set_regions_fdt_node and
set_interrupts_fdt_node
---
 hw/arm/sysbus-fdt.c | 129 ++++++++++++++++++++++++++++++++++++----------------
 1 file changed, 91 insertions(+), 38 deletions(-)

diff --git a/hw/arm/sysbus-fdt.c b/hw/arm/sysbus-fdt.c
index d4f97f5..bc0944c 100644
--- a/hw/arm/sysbus-fdt.c
+++ b/hw/arm/sysbus-fdt.c
@@ -58,39 +58,59 @@ typedef struct NodeCreationPair {
 /* Device Specific Code */
 
 /**
- * add_calxeda_midway_xgmac_fdt_node
+ * set_interrupts_fdt_node
  *
- * Generates a very simple node with following properties:
- * compatible string, regs, interrupts
+ * Helper function, generate interrupts property for a given node
  */
-static int add_calxeda_midway_xgmac_fdt_node(SysBusDevice *sbdev, void *opaque)
+static int set_interrupts_fdt_node(char *nodename, SysBusDevice *sbdev,
+        void *opaque, int type, int flags)
 {
     PlatformBusFDTData *data = opaque;
     PlatformBusDevice *pbus = data->pbus;
     void *fdt = data->fdt;
-    const char *parent_node = data->pbus_node_name;
-    int compat_str_len;
-    char *nodename;
-    int i, ret = -1;
     uint32_t *irq_attr;
-    uint64_t *reg_attr;
-    uint64_t mmio_base;
     uint64_t irq_number;
+    int i, ret;
     VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(sbdev);
     VFIODevice *vbasedev = &vdev->vbasedev;
-    Object *obj = OBJECT(sbdev);
 
-    mmio_base = object_property_get_int(obj, "mmio[0]", NULL);
+    irq_attr = g_new(uint32_t, vbasedev->num_irqs*3);
 
-    nodename = g_strdup_printf("%s/%s@%" PRIx64, parent_node,
-                               vbasedev->name,
-                               mmio_base);
+    for (i = 0; i < vbasedev->num_irqs; i++) {
+        irq_number = platform_bus_get_irqn(pbus, sbdev , i)
+            + data->irq_start;
+        irq_attr[3*i] = cpu_to_be32(type);
+        irq_attr[3*i+1] = cpu_to_be32(irq_number);
+        irq_attr[3*i+2] = cpu_to_be32(flags);
+    }
 
-    qemu_fdt_add_subnode(fdt, nodename);
+    ret = qemu_fdt_setprop(fdt, nodename, "interrupts",
+            irq_attr, vbasedev->num_irqs*3*sizeof(uint32_t));
 
-    compat_str_len = strlen(vdev->compat) + 1;
-    qemu_fdt_setprop(fdt, nodename, "compatible",
-                          vdev->compat, compat_str_len);
+    if (ret < 0) {
+        error_report("could not set interrupts property of node %s", nodename);
+    }
+    g_free(irq_attr);
+
+    return ret;
+}
+
+/**
+ * set_regions_fdt_node
+ *
+ * Helper function, generate reg property for a given node
+ */
+static int set_regions_fdt_node(char *nodename, SysBusDevice *sbdev,
+        void *opaque)
+{
+    PlatformBusFDTData *data = opaque;
+    PlatformBusDevice *pbus = data->pbus;
+    void *fdt = data->fdt;
+    uint64_t *reg_attr;
+    uint64_t mmio_base;
+    int i, ret;
+    VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(sbdev);
+    VFIODevice *vbasedev = &vdev->vbasedev;
 
     reg_attr = g_new(uint64_t, vbasedev->num_regions*4);
 
@@ -103,34 +123,67 @@ static int add_calxeda_midway_xgmac_fdt_node(SysBusDevice 
*sbdev, void *opaque)
     }
 
     ret = qemu_fdt_setprop_sized_cells_from_array(fdt, nodename, "reg",
-                     vbasedev->num_regions*2, reg_attr);
-    if (ret) {
+            vbasedev->num_regions*2, reg_attr);
+
+    if (ret < 0) {
         error_report("could not set reg property of node %s", nodename);
-        goto fail_reg;
     }
+    g_free(reg_attr);
 
-    irq_attr = g_new(uint32_t, vbasedev->num_irqs*3);
+    return ret;
+}
 
-    for (i = 0; i < vbasedev->num_irqs; i++) {
-        irq_number = platform_bus_get_irqn(pbus, sbdev , i)
-                         + 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);
+/**
+ * add_calxeda_midway_xgmac_fdt_node
+ *
+ * Generates a very simple node with the following properties:
+ * compatible string, regs, interrupts
+ */
+static int add_calxeda_midway_xgmac_fdt_node(SysBusDevice *sbdev, void *opaque)
+{
+    PlatformBusFDTData *data = opaque;
+    void *fdt = data->fdt;
+    const char *parent_node = data->pbus_node_name;
+    int compat_str_len;
+    char *nodename;
+    int ret;
+    uint64_t mmio_base;
+    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);
+
+    qemu_fdt_add_subnode(fdt, nodename);
+
+    compat_str_len = strlen(vdev->compat) + 1;
+    qemu_fdt_setprop(fdt, nodename, "compatible",
+            vdev->compat, compat_str_len);
+
+    ret = set_regions_fdt_node(nodename, sbdev, opaque);
+
+    if (ret < 0) {
+        goto fail;
     }
 
-   ret = qemu_fdt_setprop(fdt, nodename, "interrupts",
-                     irq_attr, vbasedev->num_irqs*3*sizeof(uint32_t));
-    if (ret) {
-        error_report("could not set interrupts property of node %s",
-                     nodename);
+    ret = set_interrupts_fdt_node(nodename, sbdev, opaque, 0, 0x4);
+
+    if (ret < 0) {
+        goto fail;
     }
 
-    g_free(irq_attr);
-fail_reg:
-    g_free(reg_attr);
     g_free(nodename);
-    return ret;
+
+    return 0;
+
+fail:
+    error_report("could not initialize node %s", nodename);
+    g_free(nodename);
+    return -1;
 }
 
 /* list of supported dynamic sysbus devices */
-- 
2.3.0


Reply via email to