Skip to contentSkip to conversation

hw/arm/xilinx_zynq: Call qdev_connect_clock_in() before DeviceRealize

PATCH Applied Philippe Mathieu-Daudé 3 participants 11 messages Aug 3, 2020 – Aug 24, 2020 Arm/Zynq architecture

lore
Share & tools
lore
#0 lore raw

cover hw/arm/xilinx_zynq: Call qdev_connect_clock_in() before DeviceRealize

This is not a critical issue, but still annoying if you use tracing.

Maybe it is worth fixing for forks that are based on release tags, else consider it 5.2 material.

Anyway we can still discuss if qdev_connect_clock_in() is supposed to be callable *after* DeviceRealize.

Philippe Mathieu-Daudé (4):
hw/arm/xilinx_zynq: Uninline cadence_uart_create() hw/arm/xilinx_zynq: Call qdev_connect_clock_in() before DeviceRealize hw/qdev-clock: Uninline qdev_connect_clock_in() hw/qdev-clock: Avoid calling qdev_connect_clock_in after DeviceRealize

 include/hw/char/cadence_uart.h | 17 -----------------
 include/hw/qdev-clock.h        |  8 +++-----
 hw/arm/xilinx_zynq.c           | 24 +++++++++++++++++-------
 hw/core/qdev-clock.c           |  6 ++++++
 4 files changed, 26 insertions(+), 29 deletions(-)
Signature · 1 line
2.21.3
#1 lore raw

PATCH 1/4 hw/arm/xilinx_zynq: Uninline cadence_uart_create()

As we want to call qdev_connect_clock_in() before the device is realized, we need to uninline cadence_uart_create() first.

---
 include/hw/char/cadence_uart.h | 17 -----------------
 hw/arm/xilinx_zynq.c           | 14 ++++++++++++--
 2 files changed, 12 insertions(+), 19 deletions(-)
include/hw/char/cadence_uart.h +0 -17
index ed7b58d31d..dabc49ea4f 100644
@@ -53,21 +53,4 @@ typedef struct {
     Clock *refclk;
 } CadenceUARTState;
 
-static inline DeviceState *cadence_uart_create(hwaddr addr,
-                                        qemu_irq irq,
-                                        Chardev *chr)
-{
-    DeviceState *dev;
-    SysBusDevice *s;
-
-    dev = qdev_new(TYPE_CADENCE_UART);
-    s = SYS_BUS_DEVICE(dev);
-    qdev_prop_set_chr(dev, "chardev", chr);
-    sysbus_realize_and_unref(s, &error_fatal);
-    sysbus_mmio_map(s, 0, addr);
-    sysbus_connect_irq(s, 0, irq);
-
-    return dev;
-}
-
 #endif
hw/arm/xilinx_zynq.c +12 -2
index 32aa7323d9..cf6d9757b5 100644
@@ -254,10 +254,20 @@ static void zynq_init(MachineState *machine)
     sysbus_create_simple(TYPE_CHIPIDEA, 0xE0002000, pic[53 - IRQ_OFFSET]);
     sysbus_create_simple(TYPE_CHIPIDEA, 0xE0003000, pic[76 - IRQ_OFFSET]);
 
-    dev = cadence_uart_create(0xE0000000, pic[59 - IRQ_OFFSET], serial_hd(0));
+    dev = qdev_new(TYPE_CADENCE_UART);
+    busdev = SYS_BUS_DEVICE(dev);
+    qdev_prop_set_chr(dev, "chardev", serial_hd(0));
+    sysbus_realize_and_unref(busdev, &error_fatal);
+    sysbus_mmio_map(busdev, 0, 0xE0000000);
+    sysbus_connect_irq(busdev, 0, pic[59 - IRQ_OFFSET]);
     qdev_connect_clock_in(dev, "refclk",
                           qdev_get_clock_out(slcr, "uart0_ref_clk"));
-    dev = cadence_uart_create(0xE0001000, pic[82 - IRQ_OFFSET], serial_hd(1));
+    dev = qdev_new(TYPE_CADENCE_UART);
+    busdev = SYS_BUS_DEVICE(dev);
+    qdev_prop_set_chr(dev, "chardev", serial_hd(1));
+    sysbus_realize_and_unref(busdev, &error_fatal);
+    sysbus_mmio_map(busdev, 0, 0xE0001000);
+    sysbus_connect_irq(busdev, 0, pic[82 - IRQ_OFFSET]);
     qdev_connect_clock_in(dev, "refclk",
                           qdev_get_clock_out(slcr, "uart1_ref_clk"));
 
Signature · 1 line
2.21.3
Reviewed-by on v1 1/4
#2 lore raw

Re: [PATCH-for-5.1? 1/4] hw/arm/xilinx_zynq: Uninline cadence_uart_create()

Quoted 5 lines · Philippe Mathieu-Daudé

As we want to call qdev_connect_clock_in() before the device is realized, we need to uninline cadence_uart_create() first.

Alistair

Quoted 62 lines
---
 include/hw/char/cadence_uart.h | 17 -----------------
 hw/arm/xilinx_zynq.c           | 14 ++++++++++++--
 2 files changed, 12 insertions(+), 19 deletions(-)
include/hw/char/cadence_uart.h +0 -17
index ed7b58d31d..dabc49ea4f 100644
@@ -53,21 +53,4 @@ typedef struct {
     Clock *refclk;
 } CadenceUARTState;
 
-static inline DeviceState *cadence_uart_create(hwaddr addr,
-                                        qemu_irq irq,
-                                        Chardev *chr)
-{
-    DeviceState *dev;
-    SysBusDevice *s;
-
-    dev = qdev_new(TYPE_CADENCE_UART);
-    s = SYS_BUS_DEVICE(dev);
-    qdev_prop_set_chr(dev, "chardev", chr);
-    sysbus_realize_and_unref(s, &error_fatal);
-    sysbus_mmio_map(s, 0, addr);
-    sysbus_connect_irq(s, 0, irq);
-
-    return dev;
-}
-
 #endif
hw/arm/xilinx_zynq.c +12 -3
index 32aa7323d9..cf6d9757b5 100644
@@ -254,10 +254,20 @@ static void zynq_init(MachineState *machine)
     sysbus_create_simple(TYPE_CHIPIDEA, 0xE0002000, pic[53 - IRQ_OFFSET]);
     sysbus_create_simple(TYPE_CHIPIDEA, 0xE0003000, pic[76 - IRQ_OFFSET]);
 
-    dev = cadence_uart_create(0xE0000000, pic[59 - IRQ_OFFSET], serial_hd(0));
+    dev = qdev_new(TYPE_CADENCE_UART);
+    busdev = SYS_BUS_DEVICE(dev);
+    qdev_prop_set_chr(dev, "chardev", serial_hd(0));
+    sysbus_realize_and_unref(busdev, &error_fatal);
+    sysbus_mmio_map(busdev, 0, 0xE0000000);
+    sysbus_connect_irq(busdev, 0, pic[59 - IRQ_OFFSET]);
     qdev_connect_clock_in(dev, "refclk",
                           qdev_get_clock_out(slcr, "uart0_ref_clk"));
-    dev = cadence_uart_create(0xE0001000, pic[82 - IRQ_OFFSET], serial_hd(1));
+    dev = qdev_new(TYPE_CADENCE_UART);
+    busdev = SYS_BUS_DEVICE(dev);
+    qdev_prop_set_chr(dev, "chardev", serial_hd(1));
+    sysbus_realize_and_unref(busdev, &error_fatal);
+    sysbus_mmio_map(busdev, 0, 0xE0001000);
+    sysbus_connect_irq(busdev, 0, pic[82 - IRQ_OFFSET]);
     qdev_connect_clock_in(dev, "refclk",
                           qdev_get_clock_out(slcr, "uart1_ref_clk"));
 
--

2.21.3

#3 lore raw

PATCH 2/4 hw/arm/xilinx_zynq: Call qdev_connect_clock_in() before DeviceRealize

In reply to Philippe Mathieu-Daudé

Clock canonical name is set in device_set_realized (see the block added to hw/core/qdev.c in commit 0e6934f264).
If we connect a clock after the device is realized, this code is not executed. This is currently not a problem as this name is only used for trace events, however this disrupt tracing.

Fix by calling qdev_connect_clock_in() before realizing.

---
 hw/arm/xilinx_zynq.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
hw/arm/xilinx_zynq.c +9 -9
index cf6d9757b5..969ef0727c 100644
@@ -222,18 +222,18 @@ static void zynq_init(MachineState *machine)
                           1, 0x0066, 0x0022, 0x0000, 0x0000, 0x0555, 0x2aa,
                           0);
 
-    /* Create slcr, keep a pointer to connect clocks */
-    slcr = qdev_new("xilinx,zynq_slcr");
-    sysbus_realize_and_unref(SYS_BUS_DEVICE(slcr), &error_fatal);
-    sysbus_mmio_map(SYS_BUS_DEVICE(slcr), 0, 0xF8000000);
-
     /* Create the main clock source, and feed slcr with it */
     zynq_machine->ps_clk = CLOCK(object_new(TYPE_CLOCK));
     object_property_add_child(OBJECT(zynq_machine), "ps_clk",
                               OBJECT(zynq_machine->ps_clk));
     object_unref(OBJECT(zynq_machine->ps_clk));
     clock_set_hz(zynq_machine->ps_clk, PS_CLK_FREQUENCY);
+
+    /* Create slcr, keep a pointer to connect clocks */
+    slcr = qdev_new("xilinx,zynq_slcr");
     qdev_connect_clock_in(slcr, "ps_clk", zynq_machine->ps_clk);
+    sysbus_realize_and_unref(SYS_BUS_DEVICE(slcr), &error_fatal);
+    sysbus_mmio_map(SYS_BUS_DEVICE(slcr), 0, 0xF8000000);
 
     dev = qdev_new(TYPE_A9MPCORE_PRIV);
     qdev_prop_set_uint32(dev, "num-cpu", 1);
@@ -257,19 +257,19 @@ static void zynq_init(MachineState *machine)
     dev = qdev_new(TYPE_CADENCE_UART);
     busdev = SYS_BUS_DEVICE(dev);
     qdev_prop_set_chr(dev, "chardev", serial_hd(0));
+    qdev_connect_clock_in(dev, "refclk",
+                          qdev_get_clock_out(slcr, "uart0_ref_clk"));
     sysbus_realize_and_unref(busdev, &error_fatal);
     sysbus_mmio_map(busdev, 0, 0xE0000000);
     sysbus_connect_irq(busdev, 0, pic[59 - IRQ_OFFSET]);
-    qdev_connect_clock_in(dev, "refclk",
-                          qdev_get_clock_out(slcr, "uart0_ref_clk"));
     dev = qdev_new(TYPE_CADENCE_UART);
     busdev = SYS_BUS_DEVICE(dev);
     qdev_prop_set_chr(dev, "chardev", serial_hd(1));
+    qdev_connect_clock_in(dev, "refclk",
+                          qdev_get_clock_out(slcr, "uart1_ref_clk"));
     sysbus_realize_and_unref(busdev, &error_fatal);
     sysbus_mmio_map(busdev, 0, 0xE0001000);
     sysbus_connect_irq(busdev, 0, pic[82 - IRQ_OFFSET]);
-    qdev_connect_clock_in(dev, "refclk",
-                          qdev_get_clock_out(slcr, "uart1_ref_clk"));
 
     sysbus_create_varargs("cadence_ttc", 0xF8001000,
             pic[42-IRQ_OFFSET], pic[43-IRQ_OFFSET], pic[44-IRQ_OFFSET], NULL);
Signature · 1 line
2.21.3
Reviewed-by on v1 2/4
#4 lore raw
Quoted 10 lines · Philippe Mathieu-Daudé

Clock canonical name is set in device_set_realized (see the block added to hw/core/qdev.c in commit 0e6934f264).
If we connect a clock after the device is realized, this code is not executed. This is currently not a problem as this name is only used for trace events, however this disrupt tracing.

Fix by calling qdev_connect_clock_in() before realizing.

Alistair

Quoted 60 lines
---
 hw/arm/xilinx_zynq.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
hw/arm/xilinx_zynq.c +9 -10
index cf6d9757b5..969ef0727c 100644
@@ -222,18 +222,18 @@ static void zynq_init(MachineState *machine)
                           1, 0x0066, 0x0022, 0x0000, 0x0000, 0x0555, 0x2aa,
                           0);
 
-    /* Create slcr, keep a pointer to connect clocks */
-    slcr = qdev_new("xilinx,zynq_slcr");
-    sysbus_realize_and_unref(SYS_BUS_DEVICE(slcr), &error_fatal);
-    sysbus_mmio_map(SYS_BUS_DEVICE(slcr), 0, 0xF8000000);
-
     /* Create the main clock source, and feed slcr with it */
     zynq_machine->ps_clk = CLOCK(object_new(TYPE_CLOCK));
     object_property_add_child(OBJECT(zynq_machine), "ps_clk",
                               OBJECT(zynq_machine->ps_clk));
     object_unref(OBJECT(zynq_machine->ps_clk));
     clock_set_hz(zynq_machine->ps_clk, PS_CLK_FREQUENCY);
+
+    /* Create slcr, keep a pointer to connect clocks */
+    slcr = qdev_new("xilinx,zynq_slcr");
     qdev_connect_clock_in(slcr, "ps_clk", zynq_machine->ps_clk);
+    sysbus_realize_and_unref(SYS_BUS_DEVICE(slcr), &error_fatal);
+    sysbus_mmio_map(SYS_BUS_DEVICE(slcr), 0, 0xF8000000);
 
     dev = qdev_new(TYPE_A9MPCORE_PRIV);
     qdev_prop_set_uint32(dev, "num-cpu", 1);
@@ -257,19 +257,19 @@ static void zynq_init(MachineState *machine)
     dev = qdev_new(TYPE_CADENCE_UART);
     busdev = SYS_BUS_DEVICE(dev);
     qdev_prop_set_chr(dev, "chardev", serial_hd(0));
+    qdev_connect_clock_in(dev, "refclk",
+                          qdev_get_clock_out(slcr, "uart0_ref_clk"));
     sysbus_realize_and_unref(busdev, &error_fatal);
     sysbus_mmio_map(busdev, 0, 0xE0000000);
     sysbus_connect_irq(busdev, 0, pic[59 - IRQ_OFFSET]);
-    qdev_connect_clock_in(dev, "refclk",
-                          qdev_get_clock_out(slcr, "uart0_ref_clk"));
     dev = qdev_new(TYPE_CADENCE_UART);
     busdev = SYS_BUS_DEVICE(dev);
     qdev_prop_set_chr(dev, "chardev", serial_hd(1));
+    qdev_connect_clock_in(dev, "refclk",
+                          qdev_get_clock_out(slcr, "uart1_ref_clk"));
     sysbus_realize_and_unref(busdev, &error_fatal);
     sysbus_mmio_map(busdev, 0, 0xE0001000);
     sysbus_connect_irq(busdev, 0, pic[82 - IRQ_OFFSET]);
-    qdev_connect_clock_in(dev, "refclk",
-                          qdev_get_clock_out(slcr, "uart1_ref_clk"));
 
     sysbus_create_varargs("cadence_ttc", 0xF8001000,
             pic[42-IRQ_OFFSET], pic[43-IRQ_OFFSET], pic[44-IRQ_OFFSET], NULL);
--

2.21.3

#5 lore raw

PATCH 3/4 hw/qdev-clock: Uninline qdev_connect_clock_in()

In reply to Philippe Mathieu-Daudé

We want to assert the device is not realized. To avoid overloading this header including "hw/qdev-core.h", uninline the function first.

---
 include/hw/qdev-clock.h | 6 +-----
 hw/core/qdev-clock.c    | 5 +++++
 2 files changed, 6 insertions(+), 5 deletions(-)
include/hw/qdev-clock.h +1 -5
index a340f65ff9..a897f7c9d0 100644
@@ -71,11 +71,7 @@ Clock *qdev_get_clock_out(DeviceState *dev, const char *name);
  * Set the source clock of input clock @name of device @dev to @source.
  * @source period update will be propagated to @name clock.
  */
-static inline void qdev_connect_clock_in(DeviceState *dev, const char *name,
-                                         Clock *source)
-{
-    clock_set_source(qdev_get_clock_in(dev, name), source);
-}
+void qdev_connect_clock_in(DeviceState *dev, const char *name, Clock *source);
 
 /**
  * qdev_alias_clock:
hw/core/qdev-clock.c +5 -0
index 5cc1e82e51..f139b68b88 100644
@@ -183,3 +183,8 @@ Clock *qdev_alias_clock(DeviceState *dev, const char *name,
 
     return ncl->clock;
 }
+
+void qdev_connect_clock_in(DeviceState *dev, const char *name, Clock *source)
+{
+    clock_set_source(qdev_get_clock_in(dev, name), source);
+}
Signature · 1 line
2.21.3
Reviewed-by on v1 3/4
#6 lore raw

Re: [PATCH-for-5.1? 3/4] hw/qdev-clock: Uninline qdev_connect_clock_in()

Quoted 5 lines · Philippe Mathieu-Daudé

We want to assert the device is not realized. To avoid overloading this header including "hw/qdev-core.h", uninline the function first.

Alistair

Quoted 39 lines
---
 include/hw/qdev-clock.h | 6 +-----
 hw/core/qdev-clock.c    | 5 +++++
 2 files changed, 6 insertions(+), 5 deletions(-)
include/hw/qdev-clock.h +1 -5
index a340f65ff9..a897f7c9d0 100644
@@ -71,11 +71,7 @@ Clock *qdev_get_clock_out(DeviceState *dev, const char *name);
  * Set the source clock of input clock @name of device @dev to @source.
  * @source period update will be propagated to @name clock.
  */
-static inline void qdev_connect_clock_in(DeviceState *dev, const char *name,
-                                         Clock *source)
-{
-    clock_set_source(qdev_get_clock_in(dev, name), source);
-}
+void qdev_connect_clock_in(DeviceState *dev, const char *name, Clock *source);
 
 /**
  * qdev_alias_clock:
hw/core/qdev-clock.c +5 -1
index 5cc1e82e51..f139b68b88 100644
@@ -183,3 +183,8 @@ Clock *qdev_alias_clock(DeviceState *dev, const char *name,
 
     return ncl->clock;
 }
+
+void qdev_connect_clock_in(DeviceState *dev, const char *name, Clock *source)
+{
+    clock_set_source(qdev_get_clock_in(dev, name), source);
+}
--

2.21.3

#7 lore raw

PATCH 4/4 hw/qdev-clock: Avoid calling qdev_connect_clock_in after DeviceRealize

In reply to Philippe Mathieu-Daudé

Clock canonical name is set in device_set_realized (see the block added to hw/core/qdev.c in commit 0e6934f264).
If we connect a clock after the device is realized, this code is not executed. This is currently not a problem as this name is only used for trace events, however this disrupt tracing.

Add a comment to document qdev_connect_clock_in() must be called before the device is realized, and assert this condition.

---
 include/hw/qdev-clock.h | 2 ++
 hw/core/qdev-clock.c    | 1 +
 2 files changed, 3 insertions(+)
include/hw/qdev-clock.h +2 -0
index a897f7c9d0..64ca4d266f 100644
@@ -70,6 +70,8 @@ Clock *qdev_get_clock_out(DeviceState *dev, const char *name);
  *
  * Set the source clock of input clock @name of device @dev to @source.
  * @source period update will be propagated to @name clock.
+ *
+ * Must be called before @dev is realized.
  */
 void qdev_connect_clock_in(DeviceState *dev, const char *name, Clock *source);
 
hw/core/qdev-clock.c +1 -0
index f139b68b88..47ecb5b4fa 100644
@@ -186,5 +186,6 @@ Clock *qdev_alias_clock(DeviceState *dev, const char *name,
 
 void qdev_connect_clock_in(DeviceState *dev, const char *name, Clock *source)
 {
+    assert(!dev->realized);
     clock_set_source(qdev_get_clock_in(dev, name), source);
 }
Signature · 1 line
2.21.3
Reviewed-by on v1 4/4
#8 lore raw

Re: [PATCH-for-5.1? 4/4] hw/qdev-clock: Avoid calling qdev_connect_clock_in after DeviceRealize

Quoted 11 lines · Philippe Mathieu-Daudé

Clock canonical name is set in device_set_realized (see the block added to hw/core/qdev.c in commit 0e6934f264).
If we connect a clock after the device is realized, this code is not executed. This is currently not a problem as this name is only used for trace events, however this disrupt tracing.

Add a comment to document qdev_connect_clock_in() must be called before the device is realized, and assert this condition.

Alistair

Quoted 33 lines
---
 include/hw/qdev-clock.h | 2 ++
 hw/core/qdev-clock.c    | 1 +
 2 files changed, 3 insertions(+)
include/hw/qdev-clock.h +2 -0
index a897f7c9d0..64ca4d266f 100644
@@ -70,6 +70,8 @@ Clock *qdev_get_clock_out(DeviceState *dev, const char *name);
  *
  * Set the source clock of input clock @name of device @dev to @source.
  * @source period update will be propagated to @name clock.
+ *
+ * Must be called before @dev is realized.
  */
 void qdev_connect_clock_in(DeviceState *dev, const char *name, Clock *source);
 
hw/core/qdev-clock.c +1 -1
index f139b68b88..47ecb5b4fa 100644
@@ -186,5 +186,6 @@ Clock *qdev_alias_clock(DeviceState *dev, const char *name,
 
 void qdev_connect_clock_in(DeviceState *dev, const char *name, Clock *source)
 {
+    assert(!dev->realized);
     clock_set_source(qdev_get_clock_in(dev, name), source);
 }
--

2.21.3

#9 lore raw
In reply to Philippe Mathieu-Daudé

ping now that 5.2 is opened :)

Quoted 20 lines · Philippe Mathieu-Daudé

This is not a critical issue, but still annoying if you use tracing.

Maybe it is worth fixing for forks that are based on release tags, else consider it 5.2 material.

Anyway we can still discuss if qdev_connect_clock_in() is supposed to be callable *after* DeviceRealize.

Philippe Mathieu-Daudé (4):
hw/arm/xilinx_zynq: Uninline cadence_uart_create() hw/arm/xilinx_zynq: Call qdev_connect_clock_in() before DeviceRealize hw/qdev-clock: Uninline qdev_connect_clock_in() hw/qdev-clock: Avoid calling qdev_connect_clock_in after DeviceRealize

 include/hw/char/cadence_uart.h | 17 -----------------
 include/hw/qdev-clock.h        |  8 +++-----
 hw/arm/xilinx_zynq.c           | 24 +++++++++++++++++-------
 hw/core/qdev-clock.c           |  6 ++++++
 4 files changed, 26 insertions(+), 29 deletions(-)
#10 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 6 lines · Philippe Mathieu-Daudé

This is not a critical issue, but still annoying if you use tracing.

Maybe it is worth fixing for forks that are based on release tags, else consider it 5.2 material.

Quoted 9 lines

Anyway we can still discuss if qdev_connect_clock_in() is supposed to be callable *after* DeviceRealize.

Philippe Mathieu-Daudé (4):
hw/arm/xilinx_zynq: Uninline cadence_uart_create() hw/arm/xilinx_zynq: Call qdev_connect_clock_in() before DeviceRealize hw/qdev-clock: Uninline qdev_connect_clock_in() hw/qdev-clock: Avoid calling qdev_connect_clock_in after DeviceRealize

Applied to target-arm.next, thanks.

-- PMM