Skip to contentSkip to conversation

Implement the STM32L4x5 EXTI device

PATCH Superseded ~aminier 2 participants 4 messages Nov 21, 2023

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
author
#1 lore raw

PATCH 1/1 Implement STM32L4x5 EXTI

In reply to ~aminier

From: Arnaud Minier <[email protected]>

---
 hw/arm/Kconfig                    |   1 +
 hw/arm/stm32l4x5_soc.c            |  65 +++++-
 hw/misc/Kconfig                   |   3 +
 hw/misc/meson.build               |   1 +
 hw/misc/stm32l4x5_exti.c          | 329 ++++++++++++++++++++++++++++++
 hw/misc/trace-events              |   5 +
 include/hw/arm/stm32l4x5_soc.h    |   3 +
 include/hw/misc/stm32l4x5_exti.h  |  64 ++++++
 tests/qtest/meson.build           |   5 +
 tests/qtest/stm32l4x5_exti-test.c | 102 +++++++++
 10 files changed, 576 insertions(+), 2 deletions(-)
 create mode 100644 hw/misc/stm32l4x5_exti.c
 create mode 100644 include/hw/misc/stm32l4x5_exti.h
 create mode 100644 tests/qtest/stm32l4x5_exti-test.c
hw/arm/Kconfig +1 -0
index b95576fb0c..28d378ed83 100644
@@ -458,6 +458,7 @@ config STM32L4X5_SOC
     bool
     select ARM_V7M
     select OR_IRQ
+    select STM32L4X5_EXTI
 
 config XLNX_ZYNQMP_ARM
     bool
hw/arm/stm32l4x5_soc.c +63 -2

93 lines not on this page · raw message

hw/misc/Kconfig +3 -0
index cc8a8c1418..3efe3dc2cc 100644
@@ -87,6 +87,9 @@ config STM32F4XX_SYSCFG
 config STM32F4XX_EXTI
     bool
 
+config STM32L4X5_EXTI
+    bool
+
 config MIPS_ITU
     bool
 
hw/misc/meson.build +1 -0
index 36c20d5637..16db6e228d 100644
@@ -110,6 +110,7 @@ system_ss.add(when: 'CONFIG_XLNX_VERSAL_TRNG', if_true: files(
 system_ss.add(when: 'CONFIG_STM32F2XX_SYSCFG', if_true: files('stm32f2xx_syscfg.c'))
 system_ss.add(when: 'CONFIG_STM32F4XX_SYSCFG', if_true: files('stm32f4xx_syscfg.c'))
 system_ss.add(when: 'CONFIG_STM32F4XX_EXTI', if_true: files('stm32f4xx_exti.c'))
+system_ss.add(when: 'CONFIG_STM32L4X5_EXTI', if_true: files('stm32l4x5_exti.c'))
 system_ss.add(when: 'CONFIG_MPS2_FPGAIO', if_true: files('mps2-fpgaio.c'))
 system_ss.add(when: 'CONFIG_MPS2_SCC', if_true: files('mps2-scc.c'))
 
hw/misc/stm32l4x5_exti.c new file +329 -0

329 lines not on this page · raw message

hw/misc/trace-events +5 -0
index 05ff692441..2f01c62c0e 100644
@@ -163,6 +163,11 @@ stm32f4xx_exti_set_irq(int irq, int level) "Set EXTI: %d to %d"
 stm32f4xx_exti_read(uint64_t addr) "reg read: addr: 0x%" PRIx64 " "
 stm32f4xx_exti_write(uint64_t addr, uint64_t data) "reg write: addr: 0x%" PRIx64 " val: 0x%" PRIx64 ""
 
+# stm32l4x5_exti.c
+stm32l4x5_exti_set_irq(int irq, int level) "Set EXTI: %d to %d"
+stm32l4x5_exti_read(uint64_t addr, uint64_t data) "reg read: addr: 0x%" PRIx64 " val: 0x%" PRIx64 ""
+stm32l4x5_exti_write(uint64_t addr, uint64_t data) "reg write: addr: 0x%" PRIx64 " val: 0x%" PRIx64 ""
+
 # tz-mpc.c
 tz_mpc_reg_read(uint32_t offset, uint64_t data, unsigned size) "TZ MPC regs read: offset 0x%x data 0x%" PRIx64 " size %u"
 tz_mpc_reg_write(uint32_t offset, uint64_t data, unsigned size) "TZ MPC regs write: offset 0x%x data 0x%" PRIx64 " size %u"
include/hw/arm/stm32l4x5_soc.h +3 -0
index ab61c4b8a1..940941598e 100644
@@ -32,6 +32,7 @@
 #include "qemu/units.h"
 #include "hw/qdev-core.h"
 #include "hw/arm/armv7m.h"
+#include "hw/misc/stm32l4x5_exti.h"
 #include "qom/object.h"
 
 #define TYPE_STM32L4X5_SOC "stm32l4x5-soc"
@@ -45,6 +46,8 @@ struct Stm32l4x5SocState {
 
     ARMv7MState armv7m;
 
+    Stm32l4x5ExtiState exti;
+
     MemoryRegion sram1;
     MemoryRegion sram2;
     MemoryRegion flash;
include/hw/misc/stm32l4x5_exti.h new file +64 -0

64 lines not on this page · raw message

tests/qtest/meson.build +5 -0
index c9945e69b1..bc62f201a8 100644
@@ -194,6 +194,10 @@ qtests_aspeed = \
   ['aspeed_hace-test',
    'aspeed_smc-test',
    'aspeed_gpio-test']
+
+qtests_stm32l4x5 = \
+  ['stm32l4x5_exti-test']
+
 qtests_arm = \
   (config_all_devices.has_key('CONFIG_MPS2') ? ['sse-timer-test'] : []) + \
   (config_all_devices.has_key('CONFIG_CMSDK_APB_DUALTIMER') ? ['cmsdk-apb-dualtimer-test'] : []) + \
@@ -207,6 +211,7 @@ qtests_arm = \
   (config_all_devices.has_key('CONFIG_TPM_TIS_I2C') ? ['tpm-tis-i2c-test'] : []) + \
   (config_all_devices.has_key('CONFIG_VEXPRESS') ? ['test-arm-mptimer'] : []) + \
   (config_all_devices.has_key('CONFIG_MICROBIT') ? ['microbit-test'] : []) + \
+  (config_all_devices.has_key('CONFIG_STM32L4X5_SOC') ? qtests_stm32l4x5 : []) + \
   ['arm-cpu-features',
    'boot-serial-test']
 
tests/qtest/stm32l4x5_exti-test.c new file +102 -0

102 lines not on this page · raw message

Signature · 1 line
2.38.5

~aminier

author
#0 lore raw

cover Implement the STM32L4x5 EXTI device

This patch allows to emulate the STM32L4x5 EXTI device. It implements register access and software interruptions.

This is RFC since we had troubles to make all tests pass. More precisely, the line g_assert_true(get_irq(GPIO_0_IRQ)); fails in the software interrupts test. No irq seems to be fired.
(We also tested the stm32f4_exti (on which this implementation is based upon) on but it also fails so don't really know if the test or the implementation is incorrect)

Based-on: <[email protected]> ([RFC v3 2/2] hw/arm: Add minimal support for the B-L475E-IOT01A board)

(We got the message id from
https://patchew.org/QEMU/[email protected]/
but don't really the standard procedure to get it. Sorry if this is incorrect.)

Arnaud Minier (1):
Implement STM32L4x5 EXTI

 hw/arm/Kconfig                    |   1 +
 hw/arm/stm32l4x5_soc.c            |  65 +++++-
 hw/misc/Kconfig                   |   3 +
 hw/misc/meson.build               |   1 +
 hw/misc/stm32l4x5_exti.c          | 329 ++++++++++++++++++++++++++++++
 hw/misc/trace-events              |   5 +
 include/hw/arm/stm32l4x5_soc.h    |   3 +
 include/hw/misc/stm32l4x5_exti.h  |  64 ++++++
 tests/qtest/meson.build           |   5 +
 tests/qtest/stm32l4x5_exti-test.c | 102 +++++++++
 10 files changed, 576 insertions(+), 2 deletions(-)
 create mode 100644 hw/misc/stm32l4x5_exti.c
 create mode 100644 include/hw/misc/stm32l4x5_exti.h
 create mode 100644 tests/qtest/stm32l4x5_exti-test.c
Signature · 1 line
2.38.5
#3 lore raw

Hi Arnaud,

Quoted 11 lines · ~aminier

This patch allows to emulate the STM32L4x5 EXTI device. It implements register access and software interruptions.

Based-on: <[email protected]> ([RFC v3 2/2] hw/arm: Add minimal support for the B-L475E-IOT01A board)

(We got the message id from
https://patchew.org/QEMU/[email protected]/
but don't really the standard procedure to get it. Sorry if this is incorrect.)

This is the correct tag and msg-id :)

Quoted 2 lines

Arnaud Minier (1):
Implement STM32L4x5 EXTI

#2 lore raw

Re: [PATCH qemu 1/1] Implement STM32L4x5 EXTI

In reply to ~aminier

Hi Arnaud,

Quoted 26 lines · ~aminier

From: Arnaud Minier <[email protected]>

---
  hw/arm/Kconfig                    |   1 +
  hw/arm/stm32l4x5_soc.c            |  65 +++++-
  hw/misc/Kconfig                   |   3 +
  hw/misc/meson.build               |   1 +
  hw/misc/stm32l4x5_exti.c          | 329 ++++++++++++++++++++++++++++++
  hw/misc/trace-events              |   5 +
  include/hw/arm/stm32l4x5_soc.h    |   3 +
  include/hw/misc/stm32l4x5_exti.h  |  64 ++++++
  tests/qtest/meson.build           |   5 +
  tests/qtest/stm32l4x5_exti-test.c | 102 +++++++++
  10 files changed, 576 insertions(+), 2 deletions(-)

create mode 100644 hw/misc/stm32l4x5_exti.c create mode 100644 include/hw/misc/stm32l4x5_exti.h create mode 100644 tests/qtest/stm32l4x5_exti-test.c

hw/arm/stm32l4x5_soc.c +0 -0
index 198d3f6d3e..6f2a1b34b3 100644

hw/arm/stm32l4x5_soc.c line 46

Quoted 4 lines
hw/arm/stm32l4x5_soc.c +1 -0
@@ -43,10 +43,51 @@
  #define SRAM2_BASE_ADDRESS 0x10000000
  #define SRAM2_SIZE (32 * KiB)
 
+static const hwaddr exti_addr = 0x40010400;

Why not a #define?

Quoted 4 lines
hw/arm/stm32l4x5_soc.c +1 -0
+#define NUM_EXTI_IRQ 40
 
 

hw/misc/stm32l4x5_exti.c line 87

Quoted 10 lines
hw/misc/stm32l4x5_exti.c +8 -0
 
 
+static void stm32l4x5_exti_set_irq(void *opaque, int irq, int level)
+{
+    Stm32l4x5ExtiState *s = opaque;
+
+    trace_stm32l4x5_exti_set_irq(irq, level);
+
+    if (irq >= NUM_INTERRUPT_OUT_LINES) {
+        return;

This can not happen. If you are unsure, this would be a programming error, thus aborting is better, but nothing needed IMO.

Quoted 27 lines
hw/misc/stm32l4x5_exti.c +27 -0
+    }
+
+    if (irq < 32) {
+        if (((1 << irq) & s->exti_rtsr1) && level) {
+            /* Rising Edge */
+            s->exti_pr1 |= 1 << irq;
+        }
+
+        if (((1 << irq) & s->exti_ftsr1) && !level) {
+            /* Falling Edge */
+            s->exti_pr1 |= 1 << irq;
+        }
+
+        if (!((1 << irq) & s->exti_imr1)) {
+            /* Interrupt is masked */
+            return;
+        }
+    } else {
+        /* Shift the value to enable access in x2 registers*/
+        int irq_x2 = irq - 32;
+        if (((1 << irq_x2) & s->exti_rtsr2) && level) {
+            /* Rising Edge */
+            s->exti_pr2 |= 1 << irq_x2;
+        }
+
+        if (((1 << irq_x2) & s->exti_ftsr2) && !level) {
+            /* Falling Edge */

hw/misc/stm32l4x5_exti.c line 124

Quoted 10 lines
hw/misc/stm32l4x5_exti.c +10 -0
+            s->exti_pr2 |= 1 << irq_x2;
+        }
+
+        if (!((1 << irq_x2) & s->exti_imr2)) {
+            /* Interrupt is masked */
+            return;
+        }
+    }
+    qemu_irq_pulse(s->irq[irq]);
+}

Could be simpler avoiding duplication, as:

---
static void stm32l4x5_exti_set_irq(void *opaque, int irq, int level)

{
    Stm32l4x5ExtiState *s = opaque;
    int oirq = irq;
    uint32_t *rtsr;
    uint32_t *ftsr;
    uint32_t *pr;
    uint32_t *imr;

    trace_stm32l4x5_exti_set_irq(irq, level);

    if (irq < 32) {
        rtsr = &s->exti_rtsr1;
        ftsr = &s->exti_ftsr1;
        pr = &s->exti_pr1;
        imr = &s->exti_imr1;
    } else {
        rtsr = &s->exti_rtsr2;
        ftsr = &s->exti_ftsr2;
        pr = &s->exti_pr2;
        imr = &s->exti_imr2;
        /* Shift the value to enable access in x2 registers. */
        irq -= 32;
    }

    if (((1 << irq) & *rtsr) && level) {
        /* Rising Edge */
        *pr |= 1 << irq;
    }

    if (((1 << irq) & *ftsr) && !level) {
        /* Falling Edge */
        *pr |= 1 << irq;
    }

    if (!((1 << irq) & *imr)) {
        /* Interrupt is masked */
        return;
    }

    qemu_irq_pulse(s->irq[oirq]);
}

---

But changing Stm32l4x5ExtiState as:

---

struct Stm32l4x5ExtiState {
    SysBusDevice parent_obj;

    MemoryRegion mmio;

    uint32_t imr[2];
    uint32_t emr[2];
    uint32_t rtsr[2];
    uint32_t ftsr[2];
    uint32_t swier[2];
    uint32_t pr[2];

    qemu_irq irq[NUM_INTERRUPT_OUT_LINES];
};

---

We get even simpler:

---
static void stm32l4x5_exti_set_irq(void *opaque, int irq, int level)

{
    Stm32l4x5ExtiState *s = opaque;
    unsigned n = irq >= 32;
    int oirq = irq;

    trace_stm32l4x5_exti_set_irq(irq, level);

    if (irq >= 32) {
        /* Shift the value to enable access in x2 registers. */
        irq -= 32;
    }

    if (((1 << irq) & s->rtsr[n]) && level) {
        /* Rising Edge */
        s->pr[n] |= 1 << irq;
    }

    if (((1 << irq) & s->ftsr[n]) && !level) {
        /* Falling Edge */
        s->pr[n] |= 1 << irq;
    }

    if (!((1 << irq) & s->imr[n])) {
        /* Interrupt is masked */
        return;
    }

    qemu_irq_pulse(s->irq[oirq]);
}

---

(code untested).

hw/misc/stm32l4x5_exti.c line 130

Quoted 6 lines
hw/misc/stm32l4x5_exti.c +6 -0
+
+static uint64_t stm32l4x5_exti_read(void *opaque, hwaddr addr,
+                                    unsigned int size)
+{
+    Stm32l4x5ExtiState *s = opaque;
+    uint32_t r = 0;
       unsigned n = addr >= EXTI_IMR2;

hw/misc/stm32l4x5_exti.c line 135

Quoted 5 lines
hw/misc/stm32l4x5_exti.c +5 -0
+
+    switch (addr) {
+    case EXTI_IMR1:
+        r = s->exti_imr1;
+        break;

This becomes:

       case EXTI_IMR1:
       case EXTI_IMR2:
           r = s->exti_imr[n];
           break;
       ...

hw/misc/stm32l4x5_exti.c line 172

Quoted 3 lines
hw/misc/stm32l4x5_exti.c +3 -0
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "STM32L4X5_exti_read: Bad offset %x\n", (int)addr);

Please use '0x' prefix for hexadecimal.

hw/misc/stm32l4x5_exti.c

Quoted 13 lines
hw/misc/stm32l4x5_exti.c +11 -0
+        break;
+    }
+
+    trace_stm32l4x5_exti_read(addr, r);
+
+    return r;
+}
 
 
+static const MemoryRegionOps stm32l4x5_exti_ops = {
+    .read = stm32l4x5_exti_read,
+    .write = stm32l4x5_exti_write,
+    .endianness = DEVICE_NATIVE_ENDIAN,

Your implementation is 32-bit wide (all your registers are), so:

       .impl.min_access_size = 4,
       .impl.max_access_size = 4,

What are the allowed accesses? any 8/16/32/64 bits? (This is what happens when .valid fields aren't set).

hw/misc/stm32l4x5_exti.c line 312

Quoted 8 lines
hw/misc/stm32l4x5_exti.c +6 -0
+};
 
 
+static void stm32l4x5_exti_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+
+    dc->reset = stm32l4x5_exti_reset;

Better set a ResettableClass handler. DeviceClass::reset will soon be deprecated.

hw/misc/stm32l4x5_exti.c line 327

Quoted 8 lines
hw/misc/stm32l4x5_exti.c +6 -0
+    dc->vmsd = &vmstate_stm32l4x5_exti;
+}
 
 
+static void stm32l4x5_exti_register_types(void)
+{
+    type_register_static(&stm32l4x5_exti_info);
+}

Preferably use the DEFINE_TYPES() macro.

include/hw/misc/stm32l4x5_exti.h line 2

Quoted 11 lines
hw/misc/stm32l4x5_exti.c +1 -0
+type_init(stm32l4x5_exti_register_types)
include/hw/misc/stm32l4x5_exti.h new file +2 -0
new file mode 100644
index 0000000000..4305e7fcbb
@@ -0,0 +1,64 @@
+/*
+ * STM32L4x5 SoC family EXTI

"STM32L4x5 EXTI (Extended interrupts and events controller)"

include/hw/misc/stm32l4x5_exti.h line 40

Quoted 1 line
include/hw/misc/stm32l4x5_exti.h new file +1 -0
+#define NUM_GPIO_EVENT_IN_LINES 16

Since not used externally, NUM_GPIO_EVENT_IN_LINES could be restricted to the source.

Quoted 9 lines
include/hw/misc/stm32l4x5_exti.h new file +9 -0
+#define NUM_INTERRUPT_OUT_LINES 40
+
+struct Stm32l4x5ExtiState {
+    SysBusDevice parent_obj;
+
+    MemoryRegion mmio;
+
+    uint32_t exti_imr1;
+    uint32_t exti_emr1;

include/hw/misc/stm32l4x5_exti.h line 59

Quoted 10 lines
include/hw/misc/stm32l4x5_exti.h new file +10 -0
+    uint32_t exti_rtsr1;
+    uint32_t exti_ftsr1;
+    uint32_t exti_swier1;
+    uint32_t exti_pr1;
+    uint32_t exti_imr2;
+    uint32_t exti_emr2;
+    uint32_t exti_rtsr2;
+    uint32_t exti_ftsr2;
+    uint32_t exti_swier2;
+    uint32_t exti_pr2;

See previous suggestion for Stm32l4x5ExtiState. Besides, no need to use the cumbersome 'exti_' prefix.

include/hw/misc/stm32l4x5_exti.h line 64

Quoted 4 lines
include/hw/misc/stm32l4x5_exti.h new file +4 -0
+    qemu_irq irq[NUM_INTERRUPT_OUT_LINES];
+};
+
+#endif

Good patch quality, looking forward for v2!

Regards,

Phil.