qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Peter Maydell <peter.maydell@linaro.org>
To: qemu-devel@nongnu.org
Subject: [PULL 04/62] hw/arm/xlnx-versal: uart: refactor creation
Date: Tue,  7 Oct 2025 15:10:24 +0100	[thread overview]
Message-ID: <20251007141123.3239867-5-peter.maydell@linaro.org> (raw)
In-Reply-To: <20251007141123.3239867-1-peter.maydell@linaro.org>

From: Luc Michel <luc.michel@amd.com>

Refactor the UARTs creations. The VersalMap struct is now used to
describe the SoC and its peripherals. For now it contains the two UARTs
mapping information. The creation function now embeds the FDT creation
logic as well. The devices are now created dynamically using qdev_new
and (qdev|sysbus)_realize_and_unref.

This will allow to rely entirely on the VersalMap structure to create
the SoC and allow easy addition of new SoCs of the same family (like
versal2 coming with next commits).

Note that the connection to the CRL is removed for now and will be
re-added by next commits.

Signed-off-by: Luc Michel <luc.michel@amd.com>
Reviewed-by: Francisco Iglesias <francisco.iglesias@amd.com>
Reviewed-by: Edgar E. Iglesias <edgar.iglesias@amd.com>
Tested-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Message-id: 20250926070806.292065-4-luc.michel@amd.com
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
 include/hw/arm/xlnx-versal.h |   2 -
 hw/arm/xlnx-versal-virt.c    |  36 +--------
 hw/arm/xlnx-versal.c         | 144 ++++++++++++++++++++++++++++-------
 3 files changed, 119 insertions(+), 63 deletions(-)

diff --git a/include/hw/arm/xlnx-versal.h b/include/hw/arm/xlnx-versal.h
index f2a62b43552..b01ddeb1423 100644
--- a/include/hw/arm/xlnx-versal.h
+++ b/include/hw/arm/xlnx-versal.h
@@ -18,7 +18,6 @@
 #include "hw/or-irq.h"
 #include "hw/sd/sdhci.h"
 #include "hw/intc/arm_gicv3.h"
-#include "hw/char/pl011.h"
 #include "hw/dma/xlnx-zdma.h"
 #include "hw/net/cadence_gem.h"
 #include "hw/rtc/xlnx-zynqmp-rtc.h"
@@ -80,7 +79,6 @@ struct Versal {
         MemoryRegion mr_ocm;
 
         struct {
-            PL011State uart[XLNX_VERSAL_NR_UARTS];
             CadenceGEMState gem[XLNX_VERSAL_NR_GEMS];
             OrIRQState gem_irq_orgate[XLNX_VERSAL_NR_GEMS];
             XlnxZDMA adma[XLNX_VERSAL_NR_ADMAS];
diff --git a/hw/arm/xlnx-versal-virt.c b/hw/arm/xlnx-versal-virt.c
index d1c65afa2ac..e1deae11317 100644
--- a/hw/arm/xlnx-versal-virt.c
+++ b/hw/arm/xlnx-versal-virt.c
@@ -77,6 +77,7 @@ static void fdt_create(VersalVirt *s)
     s->phandle.dwc = qemu_fdt_alloc_phandle(s->fdt);
     /* Create /chosen node for load_dtb.  */
     qemu_fdt_add_subnode(s->fdt, "/chosen");
+    qemu_fdt_add_subnode(s->fdt, "/aliases");
 
     /* Header */
     qemu_fdt_setprop_cell(s->fdt, "/", "interrupt-parent", s->phandle.gic);
@@ -208,40 +209,6 @@ static void fdt_add_usb_xhci_nodes(VersalVirt *s)
     g_free(name);
 }
 
-static void fdt_add_uart_nodes(VersalVirt *s)
-{
-    uint64_t addrs[] = { MM_UART1, MM_UART0 };
-    unsigned int irqs[] = { VERSAL_UART1_IRQ_0, VERSAL_UART0_IRQ_0 };
-    const char compat[] = "arm,pl011\0arm,sbsa-uart";
-    const char clocknames[] = "uartclk\0apb_pclk";
-    int i;
-
-    for (i = 0; i < ARRAY_SIZE(addrs); i++) {
-        char *name = g_strdup_printf("/uart@%" PRIx64, addrs[i]);
-        qemu_fdt_add_subnode(s->fdt, name);
-        qemu_fdt_setprop_cell(s->fdt, name, "current-speed", 115200);
-        qemu_fdt_setprop_cells(s->fdt, name, "clocks",
-                               s->phandle.clk_125Mhz, s->phandle.clk_125Mhz);
-        qemu_fdt_setprop(s->fdt, name, "clock-names",
-                         clocknames, sizeof(clocknames));
-
-        qemu_fdt_setprop_cells(s->fdt, name, "interrupts",
-                               GIC_FDT_IRQ_TYPE_SPI, irqs[i],
-                               GIC_FDT_IRQ_FLAGS_LEVEL_HI);
-        qemu_fdt_setprop_sized_cells(s->fdt, name, "reg",
-                                     2, addrs[i], 2, 0x1000);
-        qemu_fdt_setprop(s->fdt, name, "compatible",
-                         compat, sizeof(compat));
-        qemu_fdt_setprop(s->fdt, name, "u-boot,dm-pre-reloc", NULL, 0);
-
-        if (addrs[i] == MM_UART0) {
-            /* Select UART0.  */
-            qemu_fdt_setprop_string(s->fdt, "/chosen", "stdout-path", name);
-        }
-        g_free(name);
-    }
-}
-
 static void fdt_add_canfd_nodes(VersalVirt *s)
 {
     uint64_t addrs[] = { MM_CANFD1, MM_CANFD0 };
@@ -705,7 +672,6 @@ static void versal_virt_init(MachineState *machine)
     create_virtio_regions(s);
 
     fdt_add_gem_nodes(s);
-    fdt_add_uart_nodes(s);
     fdt_add_canfd_nodes(s);
     fdt_add_gic_nodes(s);
     fdt_add_timer_nodes(s);
diff --git a/hw/arm/xlnx-versal.c b/hw/arm/xlnx-versal.c
index 3b596219561..b16af79e8a9 100644
--- a/hw/arm/xlnx-versal.c
+++ b/hw/arm/xlnx-versal.c
@@ -26,6 +26,7 @@
 #include "target/arm/gtimer.h"
 #include "system/device_tree.h"
 #include "hw/arm/fdt.h"
+#include "hw/char/pl011.h"
 
 #define XLNX_VERSAL_ACPU_TYPE ARM_CPU_TYPE_NAME("cortex-a72")
 #define XLNX_VERSAL_RCPU_TYPE ARM_CPU_TYPE_NAME("cortex-r5f")
@@ -34,6 +35,83 @@
 #define VERSAL_NUM_PMC_APB_IRQS 18
 #define NUM_OSPI_IRQ_LINES 3
 
+typedef struct VersalSimplePeriphMap {
+    uint64_t addr;
+    int irq;
+} VersalSimplePeriphMap;
+
+typedef struct VersalMap {
+    VersalSimplePeriphMap uart[2];
+    size_t num_uart;
+} VersalMap;
+
+static const VersalMap VERSAL_MAP = {
+    .uart[0] = { 0xff000000, 18 },
+    .uart[1] = { 0xff010000, 19 },
+    .num_uart = 2,
+};
+
+static const VersalMap *VERSION_TO_MAP[] = {
+    [VERSAL_VER_VERSAL] = &VERSAL_MAP,
+};
+
+static inline VersalVersion versal_get_version(Versal *s)
+{
+    return XLNX_VERSAL_BASE_GET_CLASS(s)->version;
+}
+
+static inline const VersalMap *versal_get_map(Versal *s)
+{
+    return VERSION_TO_MAP[versal_get_version(s)];
+}
+
+
+static qemu_irq versal_get_irq(Versal *s, int irq_idx)
+{
+    return qdev_get_gpio_in(DEVICE(&s->fpd.apu.gic), irq_idx);
+}
+
+static void versal_sysbus_connect_irq(Versal *s, SysBusDevice *sbd,
+                                      int sbd_idx, int irq_idx)
+{
+    qemu_irq irq = versal_get_irq(s, irq_idx);
+
+    if (irq == NULL) {
+        return;
+    }
+
+    sysbus_connect_irq(sbd, sbd_idx, irq);
+}
+
+static inline char *versal_fdt_add_subnode(Versal *s, const char *path,
+                                           uint64_t at, const char *compat,
+                                           size_t compat_sz)
+{
+    char *p;
+
+    p = g_strdup_printf("%s@%" PRIx64, path, at);
+    qemu_fdt_add_subnode(s->cfg.fdt, p);
+
+    if (!strncmp(compat, "memory", compat_sz)) {
+        qemu_fdt_setprop(s->cfg.fdt, p, "device_type", compat, compat_sz);
+    } else {
+        qemu_fdt_setprop(s->cfg.fdt, p, "compatible", compat, compat_sz);
+    }
+
+    return p;
+}
+
+static inline char *versal_fdt_add_simple_subnode(Versal *s, const char *path,
+                                                  uint64_t addr, uint64_t len,
+                                                  const char *compat,
+                                                  size_t compat_sz)
+{
+    char *p = versal_fdt_add_subnode(s, path, addr, compat, compat_sz);
+
+    qemu_fdt_setprop_sized_cells(s->cfg.fdt, p, "reg", 2, addr, 2, len);
+    return p;
+}
+
 static void versal_create_apu_cpus(Versal *s)
 {
     int i;
@@ -167,28 +245,44 @@ static void versal_create_rpu_cpus(Versal *s)
     qdev_realize(DEVICE(&s->lpd.rpu.cluster), NULL, &error_fatal);
 }
 
-static void versal_create_uarts(Versal *s, qemu_irq *pic)
+static void versal_create_uart(Versal *s,
+                               const VersalSimplePeriphMap *map,
+                               int chardev_idx)
 {
-    int i;
+    DeviceState *dev;
+    MemoryRegion *mr;
+    g_autofree char *node;
+    g_autofree char *alias;
+    const char compatible[] = "arm,pl011\0arm,sbsa-uart";
+    const char clocknames[] = "uartclk\0apb_pclk";
 
-    for (i = 0; i < ARRAY_SIZE(s->lpd.iou.uart); i++) {
-        static const int irqs[] = { VERSAL_UART0_IRQ_0, VERSAL_UART1_IRQ_0};
-        static const uint64_t addrs[] = { MM_UART0, MM_UART1 };
-        char *name = g_strdup_printf("uart%d", i);
-        DeviceState *dev;
-        MemoryRegion *mr;
+    dev = qdev_new(TYPE_PL011);
+    object_property_add_child(OBJECT(s), "uart[*]", OBJECT(dev));
+    qdev_prop_set_chr(dev, "chardev", serial_hd(chardev_idx));
+    sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
 
-        object_initialize_child(OBJECT(s), name, &s->lpd.iou.uart[i],
-                                TYPE_PL011);
-        dev = DEVICE(&s->lpd.iou.uart[i]);
-        qdev_prop_set_chr(dev, "chardev", serial_hd(i));
-        sysbus_realize(SYS_BUS_DEVICE(dev), &error_fatal);
+    mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0);
+    memory_region_add_subregion(&s->mr_ps, map->addr, mr);
 
-        mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0);
-        memory_region_add_subregion(&s->mr_ps, addrs[i], mr);
+    versal_sysbus_connect_irq(s, SYS_BUS_DEVICE(dev), 0, map->irq);
 
-        sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0, pic[irqs[i]]);
-        g_free(name);
+    node = versal_fdt_add_simple_subnode(s, "/uart", map->addr, 0x1000,
+                                         compatible, sizeof(compatible));
+    qemu_fdt_setprop_cell(s->cfg.fdt, node, "current-speed", 115200);
+    qemu_fdt_setprop_cells(s->cfg.fdt, node, "clocks",
+                           s->phandle.clk_125mhz, s->phandle.clk_125mhz);
+    qemu_fdt_setprop(s->cfg.fdt, node, "clock-names", clocknames,
+                     sizeof(clocknames));
+    qemu_fdt_setprop_cells(s->cfg.fdt, node, "interrupts",
+                           GIC_FDT_IRQ_TYPE_SPI, map->irq,
+                           GIC_FDT_IRQ_FLAGS_LEVEL_HI);
+    qemu_fdt_setprop(s->cfg.fdt, node, "u-boot,dm-pre-reloc", NULL, 0);
+
+    alias = g_strdup_printf("serial%d", chardev_idx);
+    qemu_fdt_setprop_string(s->cfg.fdt, "/aliases", alias, node);
+
+    if (chardev_idx == 0) {
+        qemu_fdt_setprop_string(s->cfg.fdt, "/chosen", "stdout-path", node);
     }
 }
 
@@ -783,14 +877,6 @@ static void versal_create_crl(Versal *s, qemu_irq *pic)
                                  &error_abort);
     }
 
-    for (i = 0; i < ARRAY_SIZE(s->lpd.iou.uart); i++) {
-        g_autofree gchar *name = g_strdup_printf("uart[%d]", i);
-
-        object_property_set_link(OBJECT(&s->lpd.crl),
-                                 name, OBJECT(&s->lpd.iou.uart[i]),
-                                 &error_abort);
-    }
-
     object_property_set_link(OBJECT(&s->lpd.crl),
                              "usb", OBJECT(&s->lpd.iou.usb),
                              &error_abort);
@@ -942,6 +1028,8 @@ static void versal_realize(DeviceState *dev, Error **errp)
 {
     Versal *s = XLNX_VERSAL_BASE(dev);
     qemu_irq pic[XLNX_VERSAL_NR_IRQS];
+    const VersalMap *map = versal_get_map(s);
+    size_t i;
 
     g_assert(s->cfg.fdt != NULL);
 
@@ -951,7 +1039,11 @@ static void versal_realize(DeviceState *dev, Error **errp)
     versal_create_apu_cpus(s);
     versal_create_apu_gic(s, pic);
     versal_create_rpu_cpus(s);
-    versal_create_uarts(s, pic);
+
+    for (i = 0; i < map->num_uart; i++) {
+        versal_create_uart(s, &map->uart[i], i);
+    }
+
     versal_create_canfds(s, pic);
     versal_create_usbs(s, pic);
     versal_create_gems(s, pic);
-- 
2.43.0



  parent reply	other threads:[~2025-10-07 14:34 UTC|newest]

Thread overview: 64+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-07 14:10 [PULL 00/62] target-arm queue Peter Maydell
2025-10-07 14:10 ` [PULL 01/62] target/arm: Don't set HCR.RW for AArch32 only CPUs Peter Maydell
2025-10-07 14:10 ` [PULL 02/62] hw/arm/xlnx-versal: split the xlnx-versal type Peter Maydell
2025-10-07 14:10 ` [PULL 03/62] hw/arm/xlnx-versal: prepare for FDT creation Peter Maydell
2025-10-07 14:10 ` Peter Maydell [this message]
2025-10-07 14:10 ` [PULL 05/62] hw/arm/xlnx-versal: canfd: refactor creation Peter Maydell
2025-10-07 14:10 ` [PULL 06/62] hw/arm/xlnx-versal: sdhci: " Peter Maydell
2025-10-07 14:10 ` [PULL 07/62] hw/arm/xlnx-versal: gem: " Peter Maydell
2025-10-07 14:10 ` [PULL 08/62] hw/arm/xlnx-versal: adma: " Peter Maydell
2025-10-07 14:10 ` [PULL 09/62] hw/arm/xlnx-versal: xram: " Peter Maydell
2025-10-07 14:10 ` [PULL 10/62] hw/arm/xlnx-versal: usb: " Peter Maydell
2025-10-07 14:10 ` [PULL 11/62] hw/arm/xlnx-versal: efuse: " Peter Maydell
2025-10-07 14:10 ` [PULL 12/62] hw/arm/xlnx-versal: ospi: " Peter Maydell
2025-10-07 14:10 ` [PULL 13/62] hw/arm/xlnx-versal: VersalMap: add support for OR'ed IRQs Peter Maydell
2025-10-07 14:10 ` [PULL 14/62] hw/arm/xlnx-versal: PMC IOU SCLR: refactor creation Peter Maydell
2025-10-07 14:10 ` [PULL 15/62] hw/arm/xlnx-versal: bbram: " Peter Maydell
2025-10-07 14:10 ` [PULL 16/62] hw/arm/xlnx-versal: trng: " Peter Maydell
2025-10-07 14:10 ` [PULL 17/62] hw/arm/xlnx-versal: rtc: " Peter Maydell
2025-10-07 14:10 ` [PULL 18/62] hw/arm/xlnx-versal: cfu: " Peter Maydell
2025-10-07 14:10 ` [PULL 19/62] hw/arm/xlnx-versal: crl: " Peter Maydell
2025-10-07 14:10 ` [PULL 20/62] hw/arm/xlnx-versal-virt: virtio: " Peter Maydell
2025-10-07 14:10 ` [PULL 21/62] hw/arm/xlnx-versal: refactor CPU cluster creation Peter Maydell
2025-10-07 14:10 ` [PULL 22/62] hw/arm/xlnx-versal: add the mp_affinity property to the CPU mapping Peter Maydell
2025-10-07 14:10 ` [PULL 23/62] hw/arm/xlnx-versal: instantiate the GIC ITS in the APU Peter Maydell
2025-10-07 14:10 ` [PULL 24/62] hw/intc/arm_gicv3: Introduce a 'first-cpu-index' property Peter Maydell
2025-10-07 14:10 ` [PULL 25/62] hw/arm/xlnx-versal: add support for multiple GICs Peter Maydell
2025-10-07 14:10 ` [PULL 26/62] hw/arm/xlnx-versal: add support for GICv2 Peter Maydell
2025-10-07 14:10 ` [PULL 27/62] hw/arm/xlnx-versal: rpu: refactor creation Peter Maydell
2025-10-07 14:10 ` [PULL 28/62] hw/arm/xlnx-versal: ocm: " Peter Maydell
2025-10-07 14:10 ` [PULL 29/62] hw/arm/xlnx-versal: ddr: " Peter Maydell
2025-10-07 14:10 ` [PULL 30/62] hw/arm/xlnx-versal: add the versal_get_num_cpu accessor Peter Maydell
2025-10-07 14:10 ` [PULL 31/62] hw/misc/xlnx-versal-crl: remove unnecessary include directives Peter Maydell
2025-10-07 14:10 ` [PULL 32/62] hw/misc/xlnx-versal-crl: split into base/concrete classes Peter Maydell
2025-10-07 14:10 ` [PULL 33/62] hw/misc/xlnx-versal-crl: refactor device reset logic Peter Maydell
2025-10-07 14:10 ` [PULL 34/62] hw/arm/xlnx-versal: reconnect the CRL to the other devices Peter Maydell
2025-10-07 14:10 ` [PULL 35/62] hw/arm/xlnx-versal: use hw/arm/bsa.h for timer IRQ indices Peter Maydell
2025-10-07 14:10 ` [PULL 36/62] hw/arm/xlnx-versal: tidy up Peter Maydell
2025-10-07 14:10 ` [PULL 37/62] hw/misc/xlnx-versal-crl: add the versal2 version Peter Maydell
2025-10-07 14:10 ` [PULL 38/62] hw/arm/xlnx-versal: add a per_cluster_gic switch to VersalCpuClusterMap Peter Maydell
2025-10-07 14:10 ` [PULL 39/62] hw/arm/xlnx-versal: add the target field in IRQ descriptor Peter Maydell
2025-10-07 14:11 ` [PULL 40/62] target/arm/tcg/cpu64: add the cortex-a78ae CPU Peter Maydell
2025-10-07 14:11 ` [PULL 41/62] hw/arm/xlnx-versal: add versal2 SoC Peter Maydell
2025-10-07 14:11 ` [PULL 42/62] hw/arm/xlnx-versal-virt: rename the machine to amd-versal-virt Peter Maydell
2025-10-07 14:11 ` [PULL 43/62] hw/arm/xlnx-versal-virt: split into base/concrete classes Peter Maydell
2025-10-07 14:11 ` [PULL 44/62] hw/arm/xlnx-versal-virt: tidy up Peter Maydell
2025-10-07 14:11 ` [PULL 45/62] docs/system/arm/xlnx-versal-virt: update supported devices Peter Maydell
2025-10-07 14:11 ` [PULL 46/62] docs/system/arm/xlnx-versal-virt: add a note about dumpdtb Peter Maydell
2025-10-07 14:11 ` [PULL 47/62] hw/arm/xlnx-versal-virt: add the xlnx-versal2-virt machine Peter Maydell
2025-10-07 14:11 ` [PULL 48/62] tests/functional/test_aarch64_xlnx_versal: test the versal2 machine Peter Maydell
2025-10-07 14:11 ` [PULL 49/62] hw/arm/xlnx-zynqmp: move GIC_NUM_SPI_INTR define in header Peter Maydell
2025-10-07 14:11 ` [PULL 50/62] hw/arm/xlnx-zynqmp: introduce helper to compute RPU number Peter Maydell
2025-10-07 14:11 ` [PULL 51/62] hw/arm/xlnx-zynqmp: wire a second GIC for the Cortex-R5 Peter Maydell
2025-10-07 14:11 ` [PULL 52/62] hw/arm: Remove sl_bootparam_write() and 'hw/arm/sharpsl.h' header Peter Maydell
2025-10-07 14:11 ` [PULL 53/62] target/arm: Add isar feature test for FEAT_RME_GPC2 Peter Maydell
2025-10-07 14:11 ` [PULL 54/62] target/arm: Add GPCCR fields from ARM revision L.b Peter Maydell
2025-10-07 14:11 ` [PULL 55/62] target/arm: Enable FEAT_RME_GPC2 bits in gpccr_write Peter Maydell
2025-10-07 14:11 ` [PULL 56/62] target/arm: Add cur_space to S1Translate Peter Maydell
2025-10-07 14:11 ` [PULL 57/62] target/arm: GPT_Secure is reserved without FEAT_SEL2 Peter Maydell
2025-10-07 14:11 ` [PULL 58/62] target/arm: Implement GPT_NonSecureOnly Peter Maydell
2025-10-07 14:11 ` [PULL 59/62] target/arm: Implement SPAD, NSPAD, RLPAD Peter Maydell
2025-10-07 14:11 ` [PULL 60/62] target/arm: Fix GPT fault type for address outside PPS Peter Maydell
2025-10-07 14:11 ` [PULL 61/62] target/arm: Implement APPSAA Peter Maydell
2025-10-07 14:11 ` [PULL 62/62] target/arm: Enable FEAT_RME_GPC2 for -cpu max with x-rme Peter Maydell
2025-10-07 22:58 ` [PULL 00/62] target-arm queue Richard Henderson

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=20251007141123.3239867-5-peter.maydell@linaro.org \
    --to=peter.maydell@linaro.org \
    --cc=qemu-devel@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).