Skip to contentSkip to conversation

hw/sd/sdhci: Strengthen multiple DMA transfers

PATCH In review Philippe Mathieu-Daudé 2 participants 8 messages Sep 3, 2020 – Sep 18, 2020

lore
Share & tools
lore
#0 lore raw

cover hw/sd/sdhci: Strengthen multiple DMA transfers

Still trying to fix the bugs reported by Aleksander...

- Do not send 0 block count
- Reduce DMA to MMIO re-entrancy by yielding when pending IRQ

Based-on: <[email protected]>

Philippe Mathieu-Daudé (4):
hw/sd/sdhci: Stop multiple transfers when block count is cleared hw/sd/sdhci: Resume pending DMA transfers on MMIO accesses hw/sd/sdhci: Let sdhci_update_irq() return if IRQ was delivered hw/sd/sdhci: Yield if interrupt delivered during multiple transfer

 hw/sd/sdhci.c | 35 +++++++++++++++++++++++++++++++----
 1 file changed, 31 insertions(+), 4 deletions(-)
Signature · 1 line
2.26.2
#1 lore raw

PATCH 1/4 hw/sd/sdhci: Stop multiple transfers when block count is cleared

Clearing BlockCount stops multiple transfers.

See "SD Host Controller Simplified Specification Version 2.00":

- 2.2.3. Block Count Register (Offset 006h)
- Table 2-8 : Determination of Transfer Type

---
 hw/sd/sdhci.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
hw/sd/sdhci.c +6 -1
index ecbf84e9d3f..703357e94a7 100644
@@ -728,6 +728,12 @@ static void sdhci_do_adma(SDHCIState *s)
     ADMADescr dscr = {};
     int i;
 
+    if (s->trnmod & SDHC_TRNS_BLK_CNT_EN && !s->blkcnt) {
+        /* Stop Multiple Transfer */
+        sdhci_end_transfer(s);
+        return;
+    }
+
     for (i = 0; i < SDHC_ADMA_DESCS_PER_DELAY; ++i) {
         s->admaerr &= ~SDHC_ADMAERR_LENGTH_MISMATCH;
 
@@ -753,7 +759,6 @@ static void sdhci_do_adma(SDHCIState *s)
 
         switch (dscr.attr & SDHC_ADMA_ATTR_ACT_MASK) {
         case SDHC_ADMA_ATTR_ACT_TRAN:  /* data transfer */
-
             if (s->trnmod & SDHC_TRNS_READ) {
                 while (length) {
                     if (s->data_count == 0) {
Signature · 1 line
2.26.2
#2 lore raw

PATCH 2/4 hw/sd/sdhci: Resume pending DMA transfers on MMIO accesses

In reply to Philippe Mathieu-Daudé

If we have pending DMA requests scheduled, process them first. So far we don't need to implement a bottom half to process them.

---
 hw/sd/sdhci.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
hw/sd/sdhci.c +14 -0
index 703357e94a7..2b197631870 100644
@@ -945,11 +945,21 @@ sdhci_buff_access_is_sequential(SDHCIState *s, unsigned byte_num)
     return true;
 }
 
+static void sdhci_resume_pending_transfer(SDHCIState *s)
+{
+    timer_del(s->transfer_timer);
+    sdhci_data_transfer(s);
+}
+
 static uint64_t sdhci_read(void *opaque, hwaddr offset, unsigned size)
 {
     SDHCIState *s = (SDHCIState *)opaque;
     uint32_t ret = 0;
 
+    if (timer_pending(s->transfer_timer)) {
+        sdhci_resume_pending_transfer(s);
+    }
+
     switch (offset & ~0x3) {
     case SDHC_SYSAD:
         ret = s->sdmasysad;
@@ -1093,6 +1103,10 @@ sdhci_write(void *opaque, hwaddr offset, uint64_t val, unsigned size)
     uint32_t value = val;
     value <<= shift;
 
+    if (timer_pending(s->transfer_timer)) {
+        sdhci_resume_pending_transfer(s);
+    }
+
     switch (offset & ~0x3) {
     case SDHC_SYSAD:
         s->sdmasysad = (s->sdmasysad & mask) | value;
Signature · 1 line
2.26.2
#3 lore raw

PATCH 3/4 hw/sd/sdhci: Let sdhci_update_irq() return if IRQ was delivered

In reply to Philippe Mathieu-Daudé
---
 hw/sd/sdhci.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
hw/sd/sdhci.c +7 -2
index 2b197631870..06cb098036c 100644
@@ -215,9 +215,14 @@ static uint8_t sdhci_slotint(SDHCIState *s)
          ((s->norintsts & SDHC_NIS_REMOVE) && (s->wakcon & SDHC_WKUP_ON_RMV));
 }
 
-static inline void sdhci_update_irq(SDHCIState *s)
+/* Return true if IRQ was pending and delivered */
+static bool sdhci_update_irq(SDHCIState *s)
 {
-    qemu_set_irq(s->irq, sdhci_slotint(s));
+    bool pending = sdhci_slotint(s);
+
+    qemu_set_irq(s->irq, pending);
+
+    return pending;
 }
 
 static void sdhci_raise_insertion_irq(void *opaque)
Signature · 1 line
2.26.2
#4 lore raw

PATCH 4/4 hw/sd/sdhci: Yield if interrupt delivered during multiple transfer

In reply to Philippe Mathieu-Daudé

The Descriptor Table has a bit to allow the DMA to generates Interrupt when the operation of the descriptor line is completed (see "1.13.4. Descriptor Table" of 'SD Host Controller Simplified Specification Version 2.00').

If we have pending interrupt and the descriptor requires it to be generated as soon as it is completed, reschedule pending transfers and yield to the CPU.

---
 hw/sd/sdhci.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
hw/sd/sdhci.c +4 -1
index 06cb098036c..74b0bf77103 100644
@@ -834,7 +834,10 @@ static void sdhci_do_adma(SDHCIState *s)
                 s->norintsts |= SDHC_NIS_DMA;
             }
 
-            sdhci_update_irq(s);
+            if (sdhci_update_irq(s) && !(dscr.attr & SDHC_ADMA_ATTR_END)) {
+                /* IRQ delivered, reschedule current transfer */
+                break;
+            }
         }
 
         /* ADMA transfer terminates if blkcnt == 0 or by END attribute */
Signature · 1 line
2.26.2
#5 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 7 lines · Philippe Mathieu-Daudé

Still trying to fix the bugs reported by Aleksander...

- Do not send 0 block count
- Reduce DMA to MMIO re-entrancy by yielding when pending IRQ

Based-on: <[email protected]>

Quoted 9 lines

Philippe Mathieu-Daudé (4):
hw/sd/sdhci: Stop multiple transfers when block count is cleared hw/sd/sdhci: Resume pending DMA transfers on MMIO accesses hw/sd/sdhci: Let sdhci_update_irq() return if IRQ was delivered hw/sd/sdhci: Yield if interrupt delivered during multiple transfer

 hw/sd/sdhci.c | 35 +++++++++++++++++++++++++++++++----
 1 file changed, 31 insertions(+), 4 deletions(-)

ping for review?

Tested-by on the v1 series
#6 lore raw
In reply to Philippe Mathieu-Daudé

I fuzzed the SDHCI with this applied. There are still bugs in SDHCI, but this fixes the ones triggered by my initial bug-reproducers, and doesn't appear to create any new bugs.

In the interest of incrementally fixing the issues, for this series:

Quoted 19 lines · Philippe Mathieu-Daudé

Still trying to fix the bugs reported by Aleksander...

- Do not send 0 block count
- Reduce DMA to MMIO re-entrancy by yielding when pending IRQ

Based-on: <[email protected]>

Philippe Mathieu-Daudé (4):
hw/sd/sdhci: Stop multiple transfers when block count is cleared hw/sd/sdhci: Resume pending DMA transfers on MMIO accesses hw/sd/sdhci: Let sdhci_update_irq() return if IRQ was delivered hw/sd/sdhci: Yield if interrupt delivered during multiple transfer

 hw/sd/sdhci.c | 35 +++++++++++++++++++++++++++++++----
 1 file changed, 31 insertions(+), 4 deletions(-)
Signature · 1 line
2.26.2
#7 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 5 lines · Philippe Mathieu-Daudé

Still trying to fix the bugs reported by Aleksander...

- Do not send 0 block count
- Reduce DMA to MMIO re-entrancy by yielding when pending IRQ

Quoted 7 lines

Based-on: <[email protected]>

Philippe Mathieu-Daudé (4):
hw/sd/sdhci: Stop multiple transfers when block count is cleared hw/sd/sdhci: Resume pending DMA transfers on MMIO accesses hw/sd/sdhci: Let sdhci_update_irq() return if IRQ was delivered hw/sd/sdhci: Yield if interrupt delivered during multiple transfer

Thanks, series applied to my sd-next tree.