Skip to contentSkip to conversation

Raspberry PI GPIO interrupt support

PATCH In review Davide Berardi 2 participants 3 messages Feb 9, 2021 – Feb 10, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Raspberry PI GPIO interrupt support

The bcm2835 GPIOs now generate interrupts.
This modification enables QTEST clients to trigger interrupt-based interfaces.

---
  hw/arm/bcm2835_peripherals.c   |   2 +
  hw/gpio/bcm2835_gpio.c         | 105 +++++++++++++++++++++++++++++++++
  hw/intc/bcm2835_ic.c           |   2 -
  include/hw/gpio/bcm2835_gpio.h |  12 ++++
  4 files changed, 119 insertions(+), 2 deletions(-)
hw/arm/bcm2835_peripherals.c +2 -0
index dcff13433e..101c3e6fba 100644
@@ -114,6 +114,8 @@ static void bcm2835_peripherals_init(Object *obj)
                                     OBJECT(&s->sdhci.sdbus));
      object_property_add_const_link(OBJECT(&s->gpio), "sdbus-sdhost",
                                     OBJECT(&s->sdhost.sdbus));
+    object_property_add_const_link(OBJECT(&s->gpio), "ic",
+                                   OBJECT(&s->ic));
  
      /* Mphi */
      object_initialize_child(obj, "mphi", &s->mphi, TYPE_BCM2835_MPHI);
hw/gpio/bcm2835_gpio.c +105 -0

175 lines not on this page · raw message

hw/intc/bcm2835_ic.c +0 -2
index 9000d995e8..b381dc6603 100644
@@ -57,7 +57,6 @@ static void bcm2835_ic_update(BCM2835ICState *s)
  static void bcm2835_ic_set_gpu_irq(void *opaque, int irq, int level)
  {
      BCM2835ICState *s = opaque;
-
      assert(irq >= 0 && irq < 64);
      trace_bcm2835_ic_set_gpu_irq(irq, level);
      s->gpu_irq_level = deposit64(s->gpu_irq_level, irq, 1, level != 0);
@@ -67,7 +66,6 @@ static void bcm2835_ic_set_gpu_irq(void *opaque, int irq, int level)
  static void bcm2835_ic_set_arm_irq(void *opaque, int irq, int level)
  {
      BCM2835ICState *s = opaque;
-
      assert(irq >= 0 && irq < 8);
      trace_bcm2835_ic_set_cpu_irq(irq, level);
      s->arm_irq_level = deposit32(s->arm_irq_level, irq, 1, level != 0);
include/hw/gpio/bcm2835_gpio.h +12 -0
index 1c53a05090..cad3e987d3 100644
@@ -7,6 +7,9 @@
   *  Clement Deschamps <[email protected]>
   *  Luc Michel <[email protected]>
   *
+ * GPIO External support
+ *  Davide Berardi <[email protected]>
+ *
   * This work is licensed under the terms of the GNU GPL, version 2 or later.
   * See the COPYING file in the top-level directory.
   */
@@ -17,6 +20,7 @@
  #include "hw/sd/sd.h"
  #include "hw/sysbus.h"
  #include "qom/object.h"
+#include "hw/intc/bcm2835_ic.h"
  
  struct BCM2835GpioState {
      SysBusDevice parent_obj;
@@ -27,11 +31,19 @@ struct BCM2835GpioState {
      SDBus sdbus;
      SDBus *sdbus_sdhci;
      SDBus *sdbus_sdhost;
+    BCM2835ICState *ic;
  
      uint8_t fsel[54];
      uint32_t lev0, lev1;
+    /* Event detection */
+    uint32_t eds0, eds1;
+    /* Edge selector */
+    uint32_t ren0, ren1;
+    uint32_t fen0, fen1;
+
      uint8_t sd_fsel;
      qemu_irq out[54];
+    qemu_irq irq[3];
  };
  
  #define TYPE_BCM2835_GPIO "bcm2835_gpio"
Signature · 1 line
2.30.0
#1 lore raw

Hi Davide,

Quoted 3 lines · Davide Berardi

The bcm2835 GPIOs now generate interrupts.
This modification enables QTEST clients to trigger interrupt-based interfaces.

Thanks for your patch!

Can you provide QTEST client example? Even better would be a qtest!

Quoted 8 lines

---
  hw/arm/bcm2835_peripherals.c   |   2 + hw/gpio/bcm2835_gpio.c         | 105 +++++++++++++++++++++++++++++++++ hw/intc/bcm2835_ic.c           |   2 - include/hw/gpio/bcm2835_gpio.h |  12 ++++

  4 files changed, 119 insertions(+), 2 deletions(-)

...

I wonder how you generated your patch, it doesn't apply:

Applying: Raspberry PI GPIO interrupt support
error: patch failed: hw/arm/bcm2835_peripherals.c:114
error: hw/arm/bcm2835_peripherals.c: patch does not apply
error: patch failed: hw/gpio/bcm2835_gpio.c:7
error: hw/gpio/bcm2835_gpio.c: patch does not apply
error: patch failed: hw/intc/bcm2835_ic.c:57
error: hw/intc/bcm2835_ic.c: patch does not apply
error: patch failed: include/hw/gpio/bcm2835_gpio.h:7
error: include/hw/gpio/bcm2835_gpio.h: patch does not apply Patch failed at 0001 Raspberry PI GPIO interrupt support

You can find the guidelines here:
https://wiki.qemu.org/Contribute/SubmitAPatch#Submitting_your_Patches

Quoted 12 lines

  +static inline int get_bit_2_u32(const uint32_t idx,
+                                const uint32_t v1, const uint32_t v2)

hw/gpio/bcm2835_gpio.c +10 -0
+{
+    uint64_t v = v1 | ((uint64_t)v2) << 32;
+    return !!(v & (1 << idx));
+}
+
+static int ren_detect(BCM2835GpioState *s, int index)
+{
+    if (index >= 0 && index < 54) {
+        return get_bit_2_u32(index, s->ren0, s->ren1);
+    }

hw/gpio/bcm2835_gpio.c static int gpfsel_is_out(BCM2835GpioState *s, int index) · line 138

Quoted 10 lines
hw/gpio/bcm2835_gpio.c +10 -0
+    return 0;
+}
+
+static int fen_detect(BCM2835GpioState *s, int index)
+{
+    if (index >= 0 && index < 54) {
+        return get_bit_2_u32(index, s->fen0, s->fen1);
+    }
+    return 0;
+}

I suppose you can simplify by using 'uint64_t fen' and ren, and the extract64() method from "qemu/bitops.h".

hw/gpio/bcm2835_gpio.c static void gpclr(BCM2835GpioState *s, · line 187

Quoted 11 lines
hw/gpio/bcm2835_gpio.c +7 -0
@@ -137,6 +178,15 @@ static void gpclr(BCM2835GpioState *s,
       for (i = 0; i < count; i++) {
           if ((changes & cur) && (gpfsel_is_out(s, start + i))) {
               qemu_set_irq(s->out[start + i], 0);
+        } else if ((changes & cur) && fen_detect(s, start + i)) {
+            /* If this is an input we must check falling edge */
+            int irqline = 0;
+            if (i > 27)
+                irqline = 1;
+            if (i > 45)
+              irqline = 2;

Please respect QEMU's CODING_STYLE.rst.

Matter of taste, this seems easier to follow:

int irqline;

  if (i > 45) {
    irqline = 2;
  } else if > 27
    irqline = 1;
  } else {
    irqline = 0;
  }

hw/gpio/bcm2835_gpio.c static void gpclr(BCM2835GpioState *s, · line 200

Quoted 13 lines
hw/gpio/bcm2835_gpio.c +6 -0
+
+            qemu_set_irq(s->irq[irqline], 1);
           }
           cur <<= 1;
       }
@@ -144,6 +194,34 @@ static void gpclr(BCM2835GpioState *s,
       *lev &= ~val;
   }
   +static int gpio_from_value(uint64_t value, int bank)
+{
+    int i;
+    for (i = 0 ; i < 32; ++i)
+        if (value & (1 << i))

Please use extract32().

hw/gpio/bcm2835_gpio.c static void gpclr(BCM2835GpioState *s, · line 214

Quoted 13 lines
hw/gpio/bcm2835_gpio.c +13 -0
+           return i + (32 * bank);
+    return 0;
+}
+
+static void eds_clear(BCM2835GpioState *s, uint64_t value, int bank)
+{
+    int gpio = 0;
+    int irqline = 0;
+    if (bank) {
+        s->eds0 &= ~value;
+    } else {
+        s->eds1 &= (~value & 0x3f);
+    }

Similarly, using 'uint64_t eds' should simplify this.

Quoted 15 lines
hw/gpio/bcm2835_gpio.c +10 -0
+    gpio = gpio_from_value(value, bank);
+
+    if (gpio > 27)
+       irqline = 1;
+    if (gpio > 45)
+       irqline = 2;
+
+    qemu_set_irq(s->irq[irqline], 0);
+}
+
   static uint64_t bcm2835_gpio_read(void *opaque, hwaddr offset,
           unsigned size)
   {
@@ -170,11 +248,17 @@ static uint64_t bcm2835_gpio_read(void *opaque,
 hwaddr offset,
Quoted 6 lines

      case GPLEV1:
          return s->lev1;
      case GPEDS0:
+        return s->eds0;
      case GPEDS1:
+        return s->eds1;

Using 'uint64_t eds' this becomes:

  case GPEDS0:
  case GPEDS1:
      return extract64(s->eds, offset == GPEDS1 ? 0 : 32, 32);

hw/gpio/bcm2835_gpio.c static void bcm2835_gpio_realize(DeviceState *dev, Error **errp) · line 422

Quoted 10 lines

  @@ -318,6 +415,14 @@ static void bcm2835_gpio_realize(DeviceState *dev, Error **errp)
        obj = object_property_get_link(OBJECT(dev), "sdbus-sdhost", &error_abort);
      s->sdbus_sdhost = SD_BUS(obj);

hw/gpio/bcm2835_gpio.c +5 -0
+
+    obj = object_property_get_link(OBJECT(dev), "ic", &error_abort);
+    s->ic = BCM2835_IC(obj);
+
+    for (i = 0 ; i < 3; ++i) {

Please replace this magic 3 by a definition, maybe BCM2835_EXTERNAL_IRQ_COUNT?

Quoted 10 lines

+        s->irq[i] = qdev_get_gpio_in_named(DEVICE(obj),
+                                           BCM2835_IC_GPU_IRQ, i + 49); +    }
  }
    static void bcm2835_gpio_class_init(ObjectClass *klass, void *data)

hw/intc/bcm2835_ic.c +0 -0
index 9000d995e8..b381dc6603 100644

hw/intc/bcm2835_ic.c static void bcm2835_ic_update(BCM2835ICState *s) · line 60

Quoted 4 lines
hw/intc/bcm2835_ic.c +0 -1
@@ -57,7 +57,6 @@ static void bcm2835_ic_update(BCM2835ICState *s)
   static void bcm2835_ic_set_gpu_irq(void *opaque, int irq, int level)
   {
       BCM2835ICState *s = opaque;
-

Spurious change.

hw/intc/bcm2835_ic.c static void bcm2835_ic_set_gpu_irq(void *opaque, int · line 71

Quoted 9 lines
hw/intc/bcm2835_ic.c +0 -1
       assert(irq >= 0 && irq < 64);
       trace_bcm2835_ic_set_gpu_irq(irq, level);
       s->gpu_irq_level = deposit64(s->gpu_irq_level, irq, 1, level != 0);
@@ -67,7 +66,6 @@ static void bcm2835_ic_set_gpu_irq(void *opaque, int
 irq, int level)
   static void bcm2835_ic_set_arm_irq(void *opaque, int irq, int level)
   {
       BCM2835ICState *s = opaque;
-

Ditto.

Quoted 20 lines
hw/intc/bcm2835_ic.c +0 -0
       assert(irq >= 0 && irq < 8);
       trace_bcm2835_ic_set_cpu_irq(irq, level);

      s->arm_irq_level = deposit32(s->arm_irq_level, irq, 1, level != 0);

include/hw/gpio/bcm2835_gpio.h +3 -0
index 1c53a05090..cad3e987d3 100644
@@ -7,6 +7,9 @@
    *  Clement Deschamps <[email protected]>
    *  Luc Michel <[email protected]>
    *
+ * GPIO External support
+ *  Davide Berardi <[email protected]>
+ *
    * This work is licensed under the terms of the GNU GPL, version 2 or

later.
   * See the COPYING file in the top-level directory. */

include/hw/gpio/bcm2835_gpio.h line 23

Quoted 4 lines
include/hw/gpio/bcm2835_gpio.h +1 -0
@@ -17,6 +20,7 @@
   #include "hw/sd/sd.h"
   #include "hw/sysbus.h"
   #include "qom/object.h"
+#include "hw/intc/bcm2835_ic.h"

Here would go:

#define BCM2835_EXTERNAL_IRQ_COUNT 3

Quoted 8 lines
include/hw/gpio/bcm2835_gpio.h +1 -0
     struct BCM2835GpioState {
       SysBusDevice parent_obj;
@@ -27,11 +31,19 @@ struct BCM2835GpioState {
       SDBus sdbus;
       SDBus *sdbus_sdhci;
       SDBus *sdbus_sdhost;
+    BCM2835ICState *ic;
         uint8_t fsel[54];

include/hw/gpio/bcm2835_gpio.h struct BCM2835GpioState { · line 45

Quoted 10 lines
include/hw/gpio/bcm2835_gpio.h +7 -0
       uint32_t lev0, lev1;
+    /* Event detection */
+    uint32_t eds0, eds1;
+    /* Edge selector */
+    uint32_t ren0, ren1;
+    uint32_t fen0, fen1;
+
       uint8_t sd_fsel;
       qemu_irq out[54];
+    qemu_irq irq[3];

and here 3 -> BCM2835_EXTERNAL_IRQ_COUNT.

include/hw/gpio/bcm2835_gpio.h struct BCM2835GpioState { · line 49

Quoted 2 lines
include/hw/gpio/bcm2835_gpio.h +0 -0
   };
     #define TYPE_BCM2835_GPIO "bcm2835_gpio"

Regards,

Phil.

#2 lore raw

Dear Philippe, thank you for your kind response.

I will send a new version for the patch with the modifications you've highlighted, but first I've some question on the best way to implement them.

Quoted 10 lines · Philippe Mathieu-Daudé

Hi Davide,

Quoted 3 lines · Davide Berardi

The bcm2835 GPIOs now generate interrupts.
This modification enables QTEST clients to trigger interrupt-based interfaces.

Thanks for your patch!

Can you provide QTEST client example? Even better would be a qtest!

The minimal client I've developed can be found at
https://github.com/berdav/qemu-rpi-gpio

As I have introduced, I'm building a qtest but I've found a problem: In my test, I want to check if the IRQs of the named GPIOs /machine/soc/peripherals/ic/gpu-irq are correctly handled by the controller. I was thinking to use irq_intercept_in to get the status of the IRQ lines using something similar to the following:

qtest_irq_intercept_in(s, "/machine/soc/peripherals/ic");

And then check it using

qtest_get_irq(s, 49 + irqline);

But it isn't triggering.

Looking in the source of qtest I've found this check in the qtest_irq_intercept_in function:

         QLIST_FOREACH(ngl, &dev->gpios, node) {
             /* We don't support intercept of named GPIOs yet */
             if (ngl->name) {
                 continue;
             }

I've removed the if and the qtests seems to work just fine. Is there particular caveats or details to implement this part of the framework?

I can send separated patches if you have some details on the desiderata of the support, or maybe start a new thread with this question.

Otherwise, I can remove the part of my qtest to not check the IRQs, but I don't know if it would be the best choice.

Quoted 14 lines

I wonder how you generated your patch, it doesn't apply:

Applying: Raspberry PI GPIO interrupt support
error: patch failed: hw/arm/bcm2835_peripherals.c:114
error: hw/arm/bcm2835_peripherals.c: patch does not apply
error: patch failed: hw/gpio/bcm2835_gpio.c:7
error: hw/gpio/bcm2835_gpio.c: patch does not apply
error: patch failed: hw/intc/bcm2835_ic.c:57
error: hw/intc/bcm2835_ic.c: patch does not apply
error: patch failed: include/hw/gpio/bcm2835_gpio.h:7
error: include/hw/gpio/bcm2835_gpio.h: patch does not apply Patch failed at 0001 Raspberry PI GPIO interrupt support

Quoted 3 lines

You can find the guidelines here:
https://wiki.qemu.org/Contribute/SubmitAPatch#Submitting_your_Patches

I have just created the patch using git format patch -s , it does not give me any error applying it over the master branch on
https://github.com/qemu/QEMU . I will take more care and apply it on the master branch of https://git.qemu.org/git/qemu.git/ , thank you.

Quoted 5 lines

[... cut ...]

Regards,

Phil.

I'm adapting the patch using the Coding style script now. I've also introduced a new costant for the number of GPIOs.

Thank you for your time,
D.