Skip to contentSkip to conversation

Standard SD host controller model

PATCH In review Peter A. G. Crosthwaite 3 participants 17 messages Aug 6, 2012 – Sep 18, 2012

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

v6 cover Standard SD host controller model

[Original cover by Igor]
First patch introduces standard SD host controller model. This is accumulated version of my previous patch I sent a while ago and a recent SDHCI patch by Peter A. G. Crosthwaite. Second patch introduces Exynos4210-specific SDHCI built on top of standard SDHCI model.

[New]
Third patch changes the -sd command line argument to be repeatable, to support multiple SD controllers in one system. Fourth patch adds 2x SDHCI controllers to the Xilinx Zynq machine

This revision is typo fixes and rebasing only.

Changelog:
Changed from v5:
Igors IRQ changes RE PMM review.
Changed from v4:
Igors changes re PMM review (P1/2)
Typo in commit msg (P3)
removed redundant braces in P4
Changed from v3:
Rebased for new Makefile system
Fixed include guard in sdhci.h
Typos in commit messages
Changed from v2:
corrected typo errors in ADMA1 support
added patches 3-4
v1->v2
PATCH1:
add support for ADMA1 (I havn't tested it though).
fixed s->prnsts <-> s->pwrcon typo (thanks to Peter, strange that it even worked before).
PATCH2:
change header prefix from "target-arm" to "exynos4210".

Peter A. G. Crosthwaite (2):
vl.c: allow for repeated -sd arguments
xilinx_zynq: Added SD controllers

*** BLURB HERE ***

Igor Mitsyanko (2):
hw: introduce standard SD host controller
exynos4210: Added SD host controller model

Peter A. G. Crosthwaite (2):
vl.c: allow for repeated -sd arguments
xilinx_zynq: Added SD controllers

 default-configs/arm-softmmu.mak |    1 +
 hw/Makefile.objs                |    1 +
 hw/arm/Makefile.objs            |    1 +
 hw/exynos4210.c                 |   20 +
 hw/exynos4210_sdhci.c           |  444 ++++++++++++++
 hw/sdhci.c                      | 1262 +++++++++++++++++++++++++++++++++++++++
 hw/sdhci.h                      |  309 ++++++++++
 hw/xilinx_zynq.c                |   10 +
 vl.c                            |    2 +-
 9 files changed, 2049 insertions(+), 1 deletions(-)
 create mode 100644 hw/exynos4210_sdhci.c
 create mode 100644 hw/sdhci.c
 create mode 100644 hw/sdhci.h
#1 lore raw

PATCH v6 1/4 hw: introduce standard SD host controller

From: Igor Mitsyanko <[email protected]>

Device model for standard SD Host Controller Interface (SDHCI) compliant with version 2.00 of SD association specification.

---
changed from v5 (Igor):
remove slotint register from SDHCI state;
calculate slotint register value dynamically;
changed from v4 (Igor):
s/sdhc_not_stoped/sdhc_not_stopped/ s/stoped_state/stopped_state/
s/stoped_adma/stopped_adma/
stop insert timer when host controller resets;
stop and free insert timer, free eject_cb and ro_cb in deinit function; drop redundant "(s->trnmod & SDHC_TRNS_MULTI)" clause;
add comment to Property struct;
changed from v1:
Fixed include guard (Andreas review)
Rebased for new Makefile system
changed from v2:
Typographical fixes for adma implementation

 default-configs/arm-softmmu.mak |    1 +
 hw/Makefile.objs                |    1 +
 hw/sdhci.c                      | 1262 +++++++++++++++++++++++++++++++++++++++
 hw/sdhci.h                      |  309 ++++++++++
 4 files changed, 1573 insertions(+), 0 deletions(-)
 create mode 100644 hw/sdhci.c
 create mode 100644 hw/sdhci.h
default-configs/arm-softmmu.mak +1 -0
index e542b4f..314784b 100644
@@ -27,3 +27,4 @@ CONFIG_SMC91C111=y
 CONFIG_DS1338=y
 CONFIG_PFLASH_CFI01=y
 CONFIG_PFLASH_CFI02=y
+CONFIG_SDHCI=y
hw/Makefile.objs +1 -0
index 9a350de..be8639f 100644
@@ -35,6 +35,7 @@ hw-obj-$(CONFIG_APPLESMC) += applesmc.o
 hw-obj-$(CONFIG_SMARTCARD) += ccid-card-passthru.o
 hw-obj-$(CONFIG_SMARTCARD_NSS) += ccid-card-emulated.o
 hw-obj-$(CONFIG_I8259) += i8259_common.o i8259.o
+hw-obj-$(CONFIG_SDHCI) += sdhci.o
 
 # PPC devices
 hw-obj-$(CONFIG_PREP_PCI) += prep_pci.o
hw/sdhci.c new file +1262 -0

1,262 lines not on this page · raw message

hw/sdhci.h new file +309 -0

309 lines not on this page · raw message

Signature · 1 line
1.7.0.4
#10 lore raw

PATCH v6 2/4 exynos4210: Added SD host controller model

In reply to Peter A. G. Crosthwaite

From: Igor Mitsyanko <[email protected]>

Custom Exynos4210 SD/MMC host controller, based on SD association standard host controller ver. 2.00.

---
changed from v5 (Igor):
Updated for new IRQ system
changed from v4 (Igor):
set irq on SLOTINT status instead of interrupt registers status; instead;
conditional in exynos4210_sdhci_can_issue_command() made more readable and documented;
add a comment to exynos4210_sdhci_writefn()'s SDHC_CLKCON case statement;
do not override superclass property in exynos4210.sdhci class, set properties to required value in realize function changed from v3:
rebased for new Makefile system
fixed commit msg typo (Andreas review)

 hw/arm/Makefile.objs  |    1 +
 hw/exynos4210.c       |   20 +++
 hw/exynos4210_sdhci.c |  444 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 465 insertions(+), 0 deletions(-)
 create mode 100644 hw/exynos4210_sdhci.c
hw/arm/Makefile.objs +1 -0
index 236786e..4cc1002 100644
@@ -12,6 +12,7 @@ obj-y += exynos4210_gic.o exynos4210_combiner.o exynos4210.o
 obj-y += exynos4_boards.o exynos4210_uart.o exynos4210_pwm.o
 obj-y += exynos4210_pmu.o exynos4210_mct.o exynos4210_fimd.o
 obj-y += exynos4210_rtc.o
+obj-y += exynos4210_sdhci.o
 obj-y += arm_l2x0.o
 obj-y += arm_mptimer.o a15mpcore.o
 obj-y += armv7m.o armv7m_nvic.o stellaris.o pl022.o stellaris_enet.o
hw/exynos4210.c +20 -0
index 7c58c90..eb35407 100644
@@ -59,6 +59,12 @@
 #define EXYNOS4210_EXT_COMBINER_BASE_ADDR   0x10440000
 #define EXYNOS4210_INT_COMBINER_BASE_ADDR   0x10448000
 
+/* SD/MMC host controllers SFR base addresses */
+#define EXYNOS4210_SDHC0_BASE_ADDR          0x12510000
+#define EXYNOS4210_SDHC1_BASE_ADDR          0x12520000
+#define EXYNOS4210_SDHC2_BASE_ADDR          0x12530000
+#define EXYNOS4210_SDHC3_BASE_ADDR          0x12540000
+
 /* PMU SFR base address */
 #define EXYNOS4210_PMU_BASE_ADDR            0x10020000
 
@@ -300,6 +306,20 @@ Exynos4210State *exynos4210_init(MemoryRegion *system_mem,
                            EXYNOS4210_UART3_FIFO_SIZE, 3, NULL,
                   s->irq_table[exynos4210_get_irq(EXYNOS4210_UART_INT_GRP, 3)]);
 
+    /*** SD/MMC host controllers ***/
+
+    sysbus_create_simple("exynos4210.sdhci", EXYNOS4210_SDHC0_BASE_ADDR,
+            s->irq_table[exynos4210_get_irq(29, 0)]);
+
+    sysbus_create_simple("exynos4210.sdhci", EXYNOS4210_SDHC1_BASE_ADDR,
+            s->irq_table[exynos4210_get_irq(29, 1)]);
+
+    sysbus_create_simple("exynos4210.sdhci", EXYNOS4210_SDHC2_BASE_ADDR,
+            s->irq_table[exynos4210_get_irq(29, 2)]);
+
+    sysbus_create_simple("exynos4210.sdhci", EXYNOS4210_SDHC3_BASE_ADDR,
+            s->irq_table[exynos4210_get_irq(29, 3)]);
+
     /*** Display controller (FIMD) ***/
     sysbus_create_varargs("exynos4210.fimd", EXYNOS4210_FIMD0_BASE_ADDR,
             s->irq_table[exynos4210_get_irq(11, 0)],
hw/exynos4210_sdhci.c new file +444 -0

444 lines not on this page · raw message

Signature · 1 line
1.7.0.4
#15 lore raw

PATCH v6 3/4 vl.c: allow for repeated -sd arguments

In reply to Peter A. G. Crosthwaite

Allows for repeating of -sd arguments in the same way as -pflash and -mtdblock.

---
changed from v4:
fixed (another) commit msg typo
changed from v3:
fixed commit msg typo

 vl.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
vl.c +1 -1
index 1329c30..fe1f33b 100644
@@ -2431,7 +2431,7 @@ int main(int argc, char **argv, char **envp)
                 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
                 break;
             case QEMU_OPTION_sd:
-                drive_add(IF_SD, 0, optarg, SD_OPTS);
+                drive_add(IF_SD, -1, optarg, SD_OPTS);
                 break;
             case QEMU_OPTION_pflash:
                 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
Signature · 1 line
1.7.0.4
#16 lore raw

PATCH v6 4/4 xilinx_zynq: Added SD controllers

In reply to Peter A. G. Crosthwaite

The Xilinx Zynq device has two SDHCI controllers. Added to the machine model.

---
changed from v4:
removed redundant braces
changed from v3:
fixed indentation
tweaked commit msg

 hw/xilinx_zynq.c |   10 ++++++++++
 1 files changed, 10 insertions(+), 0 deletions(-)
hw/xilinx_zynq.c +10 -0
index 7e6c273..a7feabe 100644
@@ -130,6 +130,16 @@ static void zynq_init(ram_addr_t ram_size, const char *boot_device,
         }
     }
 
+    dev = qdev_create(NULL, "sdhci");
+    qdev_init_nofail(dev);
+    sysbus_mmio_map(sysbus_from_qdev(dev), 0, 0xE0100000);
+    sysbus_connect_irq(sysbus_from_qdev(dev), 0, pic[56-IRQ_OFFSET]);
+
+    dev = qdev_create(NULL, "sdhci");
+    qdev_init_nofail(dev);
+    sysbus_mmio_map(sysbus_from_qdev(dev), 0, 0xE0101000);
+    sysbus_connect_irq(sysbus_from_qdev(dev), 0, pic[79-IRQ_OFFSET]);
+
     zynq_binfo.ram_size = ram_size;
     zynq_binfo.kernel_filename = kernel_filename;
     zynq_binfo.kernel_cmdline = kernel_cmdline;
Signature · 1 line
1.7.0.4
#2 lore raw

Re: [Qemu-devel] [PATCH v6 1/4] hw: introduce standard SD host controller

In reply to Peter A. G. Crosthwaite
Quoted 4 lines · Peter A. G. Crosthwaite
hw/sdhci.c new file +4 -0
+static void sdhci_sdma_transfer_multi_blocks(SDHCIState *s)
+{
+    bool page_aligned = false;
+    unsigned int n, begin;

hw/sdhci.c line 464

Quoted 10 lines
hw/sdhci.c new file +10 -0
+    const uint16_t block_size = s->blksize & 0x0fff;
+    uint32_t boundary_chk = 1 << (((s->blksize & 0xf000) >> 12) + 12);
+    uint32_t boundary_count = boundary_chk - (s->sdmasysad % boundary_chk);
+
+    /* XXX: Some sd/mmc drivers (for example, u-boot-slp) do not account for
+     * possible stop at page boundary if initial address is not page aligned,
+     * allow them to work properly */
+    if ((s->sdmasysad % boundary_chk) == 0) {
+        page_aligned = true;
+    }

It's not quite clear to me what this comment is indicating. Is it a bit of behaviour which is "not specified but behave as hardware happens to do because software is accidentally relying on it", or are we behaving differently from hardware here?

hw/sdhci.c line 579

Quoted 12 lines
hw/sdhci.c new file +12 -0
+static void get_adma_description(SDHCIState *s, ADMADescr *dscr)
+{
+    uint32_t adma1 = 0;
+    uint64_t adma2 = 0;
+    target_phys_addr_t entry_addr = (target_phys_addr_t)s->admasysaddr;
+
+    switch (SDHC_DMA_TYPE(s->hostctl)) {
+    case SDHC_CTRL_ADMA2_32:
+        cpu_physical_memory_read(entry_addr, (uint8_t *)&adma2, sizeof(adma2));
+        dscr->addr = (target_phys_addr_t)((adma2 >> 32) & 0xfffffffc);
+        dscr->length = (uint16_t)((adma2 >> 16) & 0xFFFF);
+        dscr->attr = (uint8_t)(adma2 & 0x3F);

Does the SDHCI spec define that these words are interpreted like this regardless of system endianness, or is this an accidental assumption of little-endian behaviour?

-- PMM

#11 lore raw

Re: [Qemu-devel] [PATCH v6 2/4] exynos4210: Added SD host controller model

In reply to Peter A. G. Crosthwaite
Quoted 8 lines · Peter A. G. Crosthwaite
hw/exynos4210_sdhci.c new file +8 -0
+static uint64_t
+exynos4210_sdhci_readfn(void *opaque, target_phys_addr_t offset, unsigned size)
+{
+    Exynos4SDHCIState *s = (Exynos4SDHCIState *)opaque;
+    uint32_t ret;
+
+    switch (offset & ~0x3) {
+    case SDHC_BDATA:

hw/exynos4210_sdhci.c line 293

Quoted 10 lines
hw/exynos4210_sdhci.c new file +10 -0
+        /* Buffer data port read can be disabled by CONTROL2 register */
+        if (s->control2 & EXYNOS4_SDHC_DISBUFRD) {
+            ret = 0;
+        } else {
+            ret = SDHCI_GET_CLASS(s)->mem_read(SDHCI(s), offset, size);
+        }
+        break;
+    case SDHC_ADMAERR:
+        ret = (s->admaerr >> 8 * (offset - SDHC_ADMAERR)) &
+                ((1 << 8 * size) - 1);

If size == 4 you've just shifted right by 32, which is undefined behaviour when ints are 32 bits. Try

   ret = extract32(s->admaerr, (offset & 3) << 3, size * 8);

and similarly below.

Quoted 11 lines
hw/exynos4210_sdhci.c new file +11 -0
+static void exynos4210_sdhci_writefn(void *opaque, target_phys_addr_t offset,
+        uint64_t val, unsigned size)
+{
+    Exynos4SDHCIState *s = (Exynos4SDHCIState *)opaque;
+    SDHCIState *sdhci = SDHCI(s);
+    unsigned shift;
+
+    DPRINT_L2("write %ub: addr[0x%04x] <- %u(0x%x)\n", size, (uint32_t)offset,
+            (uint32_t)val, (uint32_t)val);
+
+    switch (offset) {

hw/exynos4210_sdhci.c line 335

Quoted 10 lines
hw/exynos4210_sdhci.c new file +10 -0
+    case SDHC_CLKCON:
+        if ((val & SDHC_CLOCK_SDCLK_EN) &&
+                (sdhci->prnsts & SDHC_CARD_PRESENT)) {
+            val |= EXYNOS4_SDHC_SDCLK_STBL;
+        } else {
+            val &= ~EXYNOS4_SDHC_SDCLK_STBL;
+        }
+        /* Break out to superclass write to handle the rest of this register */
+        break;
+    case EXYNOS4_SDHC_CONTROL2 ... EXYNOS4_SDHC_CONTROL2 + 3:

Why do we switch (offset & 3) in the readfn but switch (offset) and use case FOO ... FOO + 3 in the writefn? Consistency would be nice.

hw/exynos4210_sdhci.c line 338

Quoted 3 lines
hw/exynos4210_sdhci.c new file +3 -0
+        shift = (offset - EXYNOS4_SDHC_CONTROL2) * 8;
+        s->control2 = (s->control2 & ~(((1 << 8 * size) - 1) << shift)) |
+                (val << shift);
  s->control2 = deposit32(s->control2, (offset & 3) << 3, size * 8, val);

and similarly below.

Quoted 19 lines
hw/exynos4210_sdhci.c new file +19 -0
+    case SDHC_ADMAERR ... SDHC_ADMAERR + 3:
+        if (size == 4 || (size == 2 && offset == SDHC_ADMAERR) ||
+                (size == 1 && offset == (SDHC_ADMAERR + 1))) {
+            uint32_t mask = 0;
+
+            if (size == 2) {
+                mask = 0xFFFF0000;
+            } else if (size == 1) {
+                mask = 0xFFFF00FF;
+                val <<= 8;
+            }
+
+            s->admaerr = (s->admaerr & (mask | EXYNOS4_SDHC_FINAL_BLOCK |
+               EXYNOS4_SDHC_IRQ_STAT)) | (val & ~(EXYNOS4_SDHC_FINAL_BLOCK |
+               EXYNOS4_SDHC_IRQ_STAT | EXYNOS4_SDHC_CONTINUE_REQ));
+            s->admaerr &= ~(val & EXYNOS4_SDHC_IRQ_STAT);
+            if ((s->stopped_adma) && (val & EXYNOS4_SDHC_CONTINUE_REQ) &&
+                (SDHC_DMA_TYPE(sdhci->hostctl) == SDHC_CTRL_ADMA2_32)) {
+                s->stopped_adma = false;

hw/exynos4210_sdhci.c line 373

Quoted 10 lines
hw/exynos4210_sdhci.c new file +10 -0
+                SDHCI_GET_CLASS(sdhci)->do_adma(sdhci);
+            }
+        } else {
+            uint32_t mask = (1 << (size * 8)) - 1;
+            shift = 8 * (offset & 0x3);
+            val <<= shift;
+            mask = ~(mask << shift);
+            s->admaerr = (s->admaerr & mask) | val;
+        }
+        return;

This case just looks odd. I think it would be clearer to first calculate the updated value of admaerr (using deposit32) and then act on the changes (xor of old and new value is handy to identify which bits are changed).

-- PMM

#5 lore raw

Re: [Qemu-devel] [PATCH v6 1/4] hw: introduce standard SD host controller

In reply to Peter A. G. Crosthwaite
Quoted 170 lines · Peter A. G. Crosthwaite

From: Igor Mitsyanko <[email protected]>

Device model for standard SD Host Controller Interface (SDHCI) compliant with version 2.00 of SD association specification.

hw/sdhci.c new file +165 -0
+typedef struct ADMADescr {
+    target_phys_addr_t addr;
+    uint16_t length;
+    uint8_t attr;
+    uint8_t incr;
+} ADMADescr;
+
+static void get_adma_description(SDHCIState *s, ADMADescr *dscr)
+{
+    uint32_t adma1 = 0;
+    uint64_t adma2 = 0;
+    target_phys_addr_t entry_addr = (target_phys_addr_t)s->admasysaddr;
+
+    switch (SDHC_DMA_TYPE(s->hostctl)) {
+    case SDHC_CTRL_ADMA2_32:
+        cpu_physical_memory_read(entry_addr, (uint8_t *)&adma2, sizeof(adma2));
+        dscr->addr = (target_phys_addr_t)((adma2 >> 32) & 0xfffffffc);
+        dscr->length = (uint16_t)((adma2 >> 16) & 0xFFFF);
+        dscr->attr = (uint8_t)(adma2 & 0x3F);
+        dscr->incr = 8;
+        break;
+    case SDHC_CTRL_ADMA1_32:
+        cpu_physical_memory_read(entry_addr, (uint8_t *)&adma1, sizeof(adma1));
+        dscr->addr = (target_phys_addr_t)(adma1 & 0xFFFFF000);
+        dscr->attr = (uint8_t)(adma1 & 0x3F);
+        dscr->incr = 4;
+        if ((dscr->attr & SDHC_ADMA_ATTR_ACT_MASK) == SDHC_ADMA_ATTR_SET_LEN) {
+            dscr->length = (uint16_t)((adma1 >> 12) & 0xFFFF);
+        } else {
+            dscr->length = 4096;
+        }
+        break;
+    case SDHC_CTRL_ADMA2_64:
+        cpu_physical_memory_read(entry_addr, (uint8_t *)(&dscr->attr), 1);
+        cpu_physical_memory_read(entry_addr + 2, (uint8_t *)(&dscr->length), 2);
+        cpu_physical_memory_read(entry_addr + 4, (uint8_t *)(&dscr->addr), 8);
+        dscr->attr &= 0xfffffff8;
+        dscr->incr = 12;
+        break;
+    }
+}
+
+/* Advanced DMA data transfer */
+static void sdhci_start_adma(SDHCIState *s)
+{
+    unsigned int n, begin, length;
+    const uint16_t block_size = s->blksize & 0x0fff;
+    ADMADescr dscr;
+    s->admaerr &= ~SDHC_ADMAERR_LENGTH_MISMATCH;
+
+    while (1) {
+        get_adma_description(s, &dscr);
+        DPRINT_L2("ADMA loop: addr=" TARGET_FMT_plx ", len=%d, attr=%x\n",
+                dscr.addr, dscr.length, dscr.attr);
+
+        if ((dscr.attr & SDHC_ADMA_ATTR_VALID) == 0) {
+            /* Indicate that error occurred in ST_FDS state */
+            s->admaerr &= ~SDHC_ADMAERR_STATE_MASK;
+            s->admaerr |= SDHC_ADMAERR_STATE_ST_FDS;
+
+            /* Generate ADMA error interrupt */
+            if (s->errintstsen & SDHC_EISEN_ADMAERR) {
+                s->errintsts |= SDHC_EIS_ADMAERR;
+                s->norintsts |= SDHC_NIS_ERR;
+            }
+
+            sdhci_update_irq(s);
+            break;
+        }
+
+        length = dscr.length ? dscr.length : 65536;
+
+        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) {
+                        for (n = 0; n < block_size; n++) {
+                            s->fifo_buffer[n] = sd_read_data(s->card);
+                        }
+                    }
+                    begin = s->data_count;
+                    if ((length + begin) < block_size) {
+                        s->data_count = length + begin;
+                        length = 0;
+                     } else {
+                        s->data_count = block_size;
+                        length -= block_size - begin;
+                    }
+                    cpu_physical_memory_write(dscr.addr, &s->fifo_buffer[begin],
+                            s->data_count - begin);
+                    dscr.addr += s->data_count - begin;
+                    if (s->data_count == block_size) {
+                        s->data_count = 0;
+                        if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) {
+                            s->blkcnt--;
+                            if (s->blkcnt == 0) {
+                                break;
+                            }
+                        }
+                    }
+                }
+            } else {
+                while (length) {
+                    begin = s->data_count;
+                    if ((length + begin) < block_size) {
+                        s->data_count = length + begin;
+                        length = 0;
+                     } else {
+                        s->data_count = block_size;
+                        length -= block_size - begin;
+                    }
+                    cpu_physical_memory_read(dscr.addr,
+                            &s->fifo_buffer[begin], s->data_count);
+                    dscr.addr += s->data_count - begin;
+                    if (s->data_count == block_size) {
+                        for (n = 0; n < block_size; n++) {
+                            sd_write_data(s->card, s->fifo_buffer[n]);
+                        }
+                        s->data_count = 0;
+                        if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) {
+                            s->blkcnt--;
+                            if (s->blkcnt == 0) {
+                                break;
+                            }
+                        }
+                    }
+                }
+            }
+            s->admasysaddr += dscr.incr;
+            break;
+        case SDHC_ADMA_ATTR_ACT_LINK:   /* link to next descriptor table */
+            s->admasysaddr = dscr.addr;
+            DPRINT_L1("ADMA link: admasysaddr=0x%lx\n", s->admasysaddr);
+            break;
+        default:
+            s->admasysaddr += dscr.incr;
+            break;
+        }
+
+        /* ADMA transfer terminates if blkcnt == 0 or by END attribute */
+        if (((s->trnmod & SDHC_TRNS_BLK_CNT_EN) &&
+                    (s->blkcnt == 0)) || (dscr.attr & SDHC_ADMA_ATTR_END)) {
+            DPRINT_L2("ADMA transfer completed\n");
+            if (length || ((dscr.attr & SDHC_ADMA_ATTR_END) &&
+                (s->trnmod & SDHC_TRNS_BLK_CNT_EN) &&
+                s->blkcnt != 0) || ((s->trnmod & SDHC_TRNS_BLK_CNT_EN) &&
+                s->blkcnt == 0 && (dscr.attr & SDHC_ADMA_ATTR_END) == 0)) {
+                ERRPRINT("SD/MMC host ADMA length mismatch\n");
+                s->admaerr |= SDHC_ADMAERR_LENGTH_MISMATCH |
+                        SDHC_ADMAERR_STATE_ST_TFR;
+                if (s->errintstsen & SDHC_EISEN_ADMAERR) {
+                    ERRPRINT("Set ADMA error flag\n");
+                    s->errintsts |= SDHC_EIS_ADMAERR;
+                    s->norintsts |= SDHC_NIS_ERR;
+                }
+
+                sdhci_update_irq(s);
+            }
+            SDHCI_GET_CLASS(s)->end_data_transfer(s);
+            break;
+        }
+
+        if (dscr.attr & SDHC_ADMA_ATTR_INT) {

hw/sdhci.c line 735

Quoted 10 lines
hw/sdhci.c new file +10 -0
+            DPRINT_L1("ADMA interrupt: admasysaddr=0x%lx\n", s->admasysaddr);
+            if (s->norintstsen & SDHC_NISEN_DMA) {
+                s->norintsts |= SDHC_NIS_DMA;
+            }
+
+            sdhci_update_irq(s);
+            break;
+        }
+    }
+}

So I think the guest can make this loop never terminate if it sets up a loop of ACT_LINK descriptors, right? I don't know how we should handle this but I'm pretty sure "make qemu sit there forever not responding to anything and not resettable" isn't it.

-- PMM

#3 lore raw

Re: [Qemu-devel] [PATCH v6 1/4] hw: introduce standard SD host controller

In reply to Peter Maydell
Quoted 13 lines · Peter Maydell
Quoted 10 lines · Peter A. G. Crosthwaite
hw/sdhci.c new file +10 -0
+static void sdhci_sdma_transfer_multi_blocks(SDHCIState *s)
+{
+    bool page_aligned = false;
+    unsigned int n, begin;
+    const uint16_t block_size = s->blksize & 0x0fff;
+    uint32_t boundary_chk = 1 << (((s->blksize & 0xf000) >> 12) + 12);
+    uint32_t boundary_count = boundary_chk - (s->sdmasysad % boundary_chk);
+
+    /* XXX: Some sd/mmc drivers (for example, u-boot-slp) do not account for
+     * possible stop at page boundary if initial address is not page aligned,
Quoted 8 lines

hw/sdhci.c line 464

Quoted 4 lines
hw/sdhci.c new file +4 -0
+     * allow them to work properly */
+    if ((s->sdmasysad % boundary_chk) == 0) {
+        page_aligned = true;
+    }

It's not quite clear to me what this comment is indicating. Is it a bit of behaviour which is "not specified but behave as hardware happens to do because software is accidentally relying on it", or are we behaving differently from hardware here?

Spec states that DMA transfer should stop when controller detects a carry out of specified address bits and interrupt should be generated to stimulate software to update DMA address register. There's no way to disable this behaviour, software can only regulate boundary size through bits in BLKSIZE register (4K - 512K). That's how it was implemented initially, but it caused u-boot mmc driver (which works fine on real hardware) to hang. The reason for hang is that when u-boot performs large continuous data transfer (>512K) to an arbitrary (non page-aligned) address, it doesn't care about "stop at page boundary" interrupt at all. It just loops forefer, waiting for a DMA transfer completion, which will never complete if it stopped at page boundary until software updates address register.
The fact that it somehow manages to work on hardware got me thinking that it only applies to initially aligned addresses.

Quoted 7 lines
Quoted 7 lines
hw/sdhci.c new file +7 -0
+static void get_adma_description(SDHCIState *s, ADMADescr *dscr)
+{
+    uint32_t adma1 = 0;
+    uint64_t adma2 = 0;
+    target_phys_addr_t entry_addr = (target_phys_addr_t)s->admasysaddr;
+
+    switch (SDHC_DMA_TYPE(s->hostctl)) {
Quoted 8 lines

hw/sdhci.c line 579

Quoted 5 lines
hw/sdhci.c new file +5 -0
+    case SDHC_CTRL_ADMA2_32:
+        cpu_physical_memory_read(entry_addr, (uint8_t *)&adma2, sizeof(adma2));
+        dscr->addr = (target_phys_addr_t)((adma2 >> 32) & 0xfffffffc);
+        dscr->length = (uint16_t)((adma2 >> 16) & 0xFFFF);
+        dscr->attr = (uint8_t)(adma2 & 0x3F);

Does the SDHCI spec define that these words are interpreted like this regardless of system endianness, or is this an accidental assumption of little-endian behaviour?

Spec never says it explicitly, but it's quite obvious that descriptor table has a little endian format. There is even a comment in linux SDHCI driver that says:

/*
* The spec does not specify endianness of descriptor table.
* We currently guess that it is LE.
*/

Quoted 3 lines

-- PMM

#4 lore raw

Re: [Qemu-devel] [PATCH v6 1/4] hw: introduce standard SD host controller

Quoted 20 lines · Igor Mitsyanko
Quoted 9 lines · Peter Maydell
Quoted 9 lines
hw/sdhci.c new file +9 -0
+static void get_adma_description(SDHCIState *s, ADMADescr *dscr)
+{
+    uint32_t adma1 = 0;
+    uint64_t adma2 = 0;
+    target_phys_addr_t entry_addr = (target_phys_addr_t)s->admasysaddr;
+
+    switch (SDHC_DMA_TYPE(s->hostctl)) {
+    case SDHC_CTRL_ADMA2_32:
+        cpu_physical_memory_read(entry_addr, (uint8_t *)&adma2,
Quoted 8 lines

hw/sdhci.c line 579

Quoted 4 lines

sizeof(adma2));

hw/sdhci.c new file +3 -0
+        dscr->addr = (target_phys_addr_t)((adma2 >> 32) & 0xfffffffc);
+        dscr->length = (uint16_t)((adma2 >> 16) & 0xFFFF);
+        dscr->attr = (uint8_t)(adma2 & 0x3F);

Does the SDHCI spec define that these words are interpreted like this regardless of system endianness, or is this an accidental assumption of little-endian behaviour?

Quoted 8 lines

Spec never says it explicitly, but it's quite obvious that descriptor table has a little endian format. There is even a comment in linux SDHCI driver that says:

/*
* The spec does not specify endianness of descriptor table.
* We currently guess that it is LE.
*/

OK; we could probably use a similar comment.

-- PMM

#6 lore raw

Re: [Qemu-devel] [PATCH v6 1/4] hw: introduce standard SD host controller

In reply to Peter Maydell
Quoted 186 lines · Peter Maydell
Quoted 169 lines · Peter A. G. Crosthwaite

From: Igor Mitsyanko <[email protected]>

Device model for standard SD Host Controller Interface (SDHCI) compliant with version 2.00 of SD association specification.

hw/sdhci.c new file +165 -0
+typedef struct ADMADescr {
+    target_phys_addr_t addr;
+    uint16_t length;
+    uint8_t attr;
+    uint8_t incr;
+} ADMADescr;
+
+static void get_adma_description(SDHCIState *s, ADMADescr *dscr)
+{
+    uint32_t adma1 = 0;
+    uint64_t adma2 = 0;
+    target_phys_addr_t entry_addr = (target_phys_addr_t)s->admasysaddr;
+
+    switch (SDHC_DMA_TYPE(s->hostctl)) {
+    case SDHC_CTRL_ADMA2_32:
+        cpu_physical_memory_read(entry_addr, (uint8_t *)&adma2, sizeof(adma2));
+        dscr->addr = (target_phys_addr_t)((adma2 >> 32) & 0xfffffffc);
+        dscr->length = (uint16_t)((adma2 >> 16) & 0xFFFF);
+        dscr->attr = (uint8_t)(adma2 & 0x3F);
+        dscr->incr = 8;
+        break;
+    case SDHC_CTRL_ADMA1_32:
+        cpu_physical_memory_read(entry_addr, (uint8_t *)&adma1, sizeof(adma1));
+        dscr->addr = (target_phys_addr_t)(adma1 & 0xFFFFF000);
+        dscr->attr = (uint8_t)(adma1 & 0x3F);
+        dscr->incr = 4;
+        if ((dscr->attr & SDHC_ADMA_ATTR_ACT_MASK) == SDHC_ADMA_ATTR_SET_LEN) {
+            dscr->length = (uint16_t)((adma1 >> 12) & 0xFFFF);
+        } else {
+            dscr->length = 4096;
+        }
+        break;
+    case SDHC_CTRL_ADMA2_64:
+        cpu_physical_memory_read(entry_addr, (uint8_t *)(&dscr->attr), 1);
+        cpu_physical_memory_read(entry_addr + 2, (uint8_t *)(&dscr->length), 2);
+        cpu_physical_memory_read(entry_addr + 4, (uint8_t *)(&dscr->addr), 8);
+        dscr->attr &= 0xfffffff8;
+        dscr->incr = 12;
+        break;
+    }
+}
+
+/* Advanced DMA data transfer */
+static void sdhci_start_adma(SDHCIState *s)
+{
+    unsigned int n, begin, length;
+    const uint16_t block_size = s->blksize & 0x0fff;
+    ADMADescr dscr;
+    s->admaerr &= ~SDHC_ADMAERR_LENGTH_MISMATCH;
+
+    while (1) {
+        get_adma_description(s, &dscr);
+        DPRINT_L2("ADMA loop: addr=" TARGET_FMT_plx ", len=%d, attr=%x\n",
+                dscr.addr, dscr.length, dscr.attr);
+
+        if ((dscr.attr & SDHC_ADMA_ATTR_VALID) == 0) {
+            /* Indicate that error occurred in ST_FDS state */
+            s->admaerr &= ~SDHC_ADMAERR_STATE_MASK;
+            s->admaerr |= SDHC_ADMAERR_STATE_ST_FDS;
+
+            /* Generate ADMA error interrupt */
+            if (s->errintstsen & SDHC_EISEN_ADMAERR) {
+                s->errintsts |= SDHC_EIS_ADMAERR;
+                s->norintsts |= SDHC_NIS_ERR;
+            }
+
+            sdhci_update_irq(s);
+            break;
+        }
+
+        length = dscr.length ? dscr.length : 65536;
+
+        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) {
+                        for (n = 0; n < block_size; n++) {
+                            s->fifo_buffer[n] = sd_read_data(s->card);
+                        }
+                    }
+                    begin = s->data_count;
+                    if ((length + begin) < block_size) {
+                        s->data_count = length + begin;
+                        length = 0;
+                     } else {
+                        s->data_count = block_size;
+                        length -= block_size - begin;
+                    }
+                    cpu_physical_memory_write(dscr.addr, &s->fifo_buffer[begin],
+                            s->data_count - begin);
+                    dscr.addr += s->data_count - begin;
+                    if (s->data_count == block_size) {
+                        s->data_count = 0;
+                        if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) {
+                            s->blkcnt--;
+                            if (s->blkcnt == 0) {
+                                break;
+                            }
+                        }
+                    }
+                }
+            } else {
+                while (length) {
+                    begin = s->data_count;
+                    if ((length + begin) < block_size) {
+                        s->data_count = length + begin;
+                        length = 0;
+                     } else {
+                        s->data_count = block_size;
+                        length -= block_size - begin;
+                    }
+                    cpu_physical_memory_read(dscr.addr,
+                            &s->fifo_buffer[begin], s->data_count);
+                    dscr.addr += s->data_count - begin;
+                    if (s->data_count == block_size) {
+                        for (n = 0; n < block_size; n++) {
+                            sd_write_data(s->card, s->fifo_buffer[n]);
+                        }
+                        s->data_count = 0;
+                        if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) {
+                            s->blkcnt--;
+                            if (s->blkcnt == 0) {
+                                break;
+                            }
+                        }
+                    }
+                }
+            }
+            s->admasysaddr += dscr.incr;
+            break;
+        case SDHC_ADMA_ATTR_ACT_LINK:   /* link to next descriptor table */
+            s->admasysaddr = dscr.addr;
+            DPRINT_L1("ADMA link: admasysaddr=0x%lx\n", s->admasysaddr);
+            break;
+        default:
+            s->admasysaddr += dscr.incr;
+            break;
+        }
+
+        /* ADMA transfer terminates if blkcnt == 0 or by END attribute */
+        if (((s->trnmod & SDHC_TRNS_BLK_CNT_EN) &&
+                    (s->blkcnt == 0)) || (dscr.attr & SDHC_ADMA_ATTR_END)) {
+            DPRINT_L2("ADMA transfer completed\n");
+            if (length || ((dscr.attr & SDHC_ADMA_ATTR_END) &&
+                (s->trnmod & SDHC_TRNS_BLK_CNT_EN) &&
+                s->blkcnt != 0) || ((s->trnmod & SDHC_TRNS_BLK_CNT_EN) &&
+                s->blkcnt == 0 && (dscr.attr & SDHC_ADMA_ATTR_END) == 0)) {
+                ERRPRINT("SD/MMC host ADMA length mismatch\n");
+                s->admaerr |= SDHC_ADMAERR_LENGTH_MISMATCH |
+                        SDHC_ADMAERR_STATE_ST_TFR;
+                if (s->errintstsen & SDHC_EISEN_ADMAERR) {
+                    ERRPRINT("Set ADMA error flag\n");
+                    s->errintsts |= SDHC_EIS_ADMAERR;
+                    s->norintsts |= SDHC_NIS_ERR;
+                }
+
+                sdhci_update_irq(s);
+            }
+            SDHCI_GET_CLASS(s)->end_data_transfer(s);
+            break;
+        }
+
+        if (dscr.attr & SDHC_ADMA_ATTR_INT) {

hw/sdhci.c line 735

Quoted 10 lines
hw/sdhci.c new file +10 -0
+            DPRINT_L1("ADMA interrupt: admasysaddr=0x%lx\n", s->admasysaddr);
+            if (s->norintstsen & SDHC_NISEN_DMA) {
+                s->norintsts |= SDHC_NIS_DMA;
+            }
+
+            sdhci_update_irq(s);
+            break;
+        }
+    }
+}

So I think the guest can make this loop never terminate if it sets up a loop of ACT_LINK descriptors, right? I don't know how we should handle this but I'm pretty sure "make qemu sit there forever not responding to anything and not resettable" isn't it.

Quoted 2 lines

-- PMM

That could only happen if guest would do this on purpose, but I see your point. There's no way for us to tell if SD card read/write succeeded or not, so I think the only way to to this is to suspend transfer after some reasonable amount of loops and resume it by qemu_timer, giving CPU time to do something useful. I've never seen long ADMA loops, so it wouldn't reflect on performance in any way.

#12 lore raw

Re: [Qemu-devel] [PATCH v6 2/4] exynos4210: Added SD host controller model

In reply to Peter Maydell
Quoted 24 lines · Peter Maydell
Quoted 8 lines · Peter A. G. Crosthwaite
hw/exynos4210_sdhci.c new file +8 -0
+static uint64_t
+exynos4210_sdhci_readfn(void *opaque, target_phys_addr_t offset, unsigned size)
+{
+    Exynos4SDHCIState *s = (Exynos4SDHCIState *)opaque;
+    uint32_t ret;
+
+    switch (offset & ~0x3) {
+    case SDHC_BDATA:

hw/exynos4210_sdhci.c line 293

Quoted 10 lines
hw/exynos4210_sdhci.c new file +10 -0
+        /* Buffer data port read can be disabled by CONTROL2 register */
+        if (s->control2 & EXYNOS4_SDHC_DISBUFRD) {
+            ret = 0;
+        } else {
+            ret = SDHCI_GET_CLASS(s)->mem_read(SDHCI(s), offset, size);
+        }
+        break;
+    case SDHC_ADMAERR:
+        ret = (s->admaerr >> 8 * (offset - SDHC_ADMAERR)) &
+                ((1 << 8 * size) - 1);

If size == 4 you've just shifted right by 32, which is undefined behaviour when ints are 32 bits. Try

Quoted 3 lines
    ret = extract32(s->admaerr, (offset & 3) << 3, size * 8);

and similarly below.

Ok

Quoted 16 lines
Quoted 16 lines
hw/exynos4210_sdhci.c new file +16 -0
+static void exynos4210_sdhci_writefn(void *opaque, target_phys_addr_t offset,
+        uint64_t val, unsigned size)
+{
+    Exynos4SDHCIState *s = (Exynos4SDHCIState *)opaque;
+    SDHCIState *sdhci = SDHCI(s);
+    unsigned shift;
+
+    DPRINT_L2("write %ub: addr[0x%04x] <- %u(0x%x)\n", size, (uint32_t)offset,
+            (uint32_t)val, (uint32_t)val);
+
+    switch (offset) {
+    case SDHC_CLKCON:
+        if ((val & SDHC_CLOCK_SDCLK_EN) &&
+                (sdhci->prnsts & SDHC_CARD_PRESENT)) {
+            val |= EXYNOS4_SDHC_SDCLK_STBL;
+        } else {
Quoted 8 lines

hw/exynos4210_sdhci.c line 335

Quoted 5 lines
hw/exynos4210_sdhci.c new file +5 -0
+            val &= ~EXYNOS4_SDHC_SDCLK_STBL;
+        }
+        /* Break out to superclass write to handle the rest of this register */
+        break;
+    case EXYNOS4_SDHC_CONTROL2 ... EXYNOS4_SDHC_CONTROL2 + 3:

Why do we switch (offset & 3) in the readfn but switch (offset) and use case FOO ... FOO + 3 in the writefn? Consistency would be nice.

I think I'll change readfn() switch to match writefn then, to avoid complicating SDHC_CLKON case.

Quoted 32 lines

hw/exynos4210_sdhci.c line 338

Quoted 3 lines
hw/exynos4210_sdhci.c new file +3 -0
+        shift = (offset - EXYNOS4_SDHC_CONTROL2) * 8;
+        s->control2 = (s->control2 & ~(((1 << 8 * size) - 1) << shift)) |
+                (val << shift);

s->control2 = deposit32(s->control2, (offset & 3) << 3, size * 8, val);

and similarly below.

Quoted 25 lines
hw/exynos4210_sdhci.c new file +25 -0
+    case SDHC_ADMAERR ... SDHC_ADMAERR + 3:
+        if (size == 4 || (size == 2 && offset == SDHC_ADMAERR) ||
+                (size == 1 && offset == (SDHC_ADMAERR + 1))) {
+            uint32_t mask = 0;
+
+            if (size == 2) {
+                mask = 0xFFFF0000;
+            } else if (size == 1) {
+                mask = 0xFFFF00FF;
+                val <<= 8;
+            }
+
+            s->admaerr = (s->admaerr & (mask | EXYNOS4_SDHC_FINAL_BLOCK |
+               EXYNOS4_SDHC_IRQ_STAT)) | (val & ~(EXYNOS4_SDHC_FINAL_BLOCK |
+               EXYNOS4_SDHC_IRQ_STAT | EXYNOS4_SDHC_CONTINUE_REQ));
+            s->admaerr &= ~(val & EXYNOS4_SDHC_IRQ_STAT);
+            if ((s->stopped_adma) && (val & EXYNOS4_SDHC_CONTINUE_REQ) &&
+                (SDHC_DMA_TYPE(sdhci->hostctl) == SDHC_CTRL_ADMA2_32)) {
+                s->stopped_adma = false;
+                SDHCI_GET_CLASS(sdhci)->do_adma(sdhci);
+            }
+        } else {
+            uint32_t mask = (1 << (size * 8)) - 1;
+            shift = 8 * (offset & 0x3);
+            val <<= shift;
Quoted 8 lines

hw/exynos4210_sdhci.c line 373

Quoted 4 lines
hw/exynos4210_sdhci.c new file +4 -0
+            mask = ~(mask << shift);
+            s->admaerr = (s->admaerr & mask) | val;
+        }
+        return;

This case just looks odd. I think it would be clearer to first calculate the updated value of admaerr (using deposit32) and then act on the changes (xor of old and new value is handy to identify which bits are changed).

ok

Quoted 3 lines

-- PMM

#9 lore raw

Re: [Qemu-devel] [PATCH v6 1/4] hw: introduce standard SD host controller

In reply to Peter A. G. Crosthwaite

Recently I've noticed that drive_get_next() usage is not very convenient if you want to use specific SD controller interface. Maybe we should switch from drive_get_next() to DEFINE_PROP_DRIVE()? It'll still preserve "-sd .." behaviour. What do you think Peter?

#7 lore raw

Re: [Qemu-devel] [PATCH v6 1/4] hw: introduce standard SD host controller

In reply to Igor Mitsyanko
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
hw/sdhci.c new file +5 -0
+            sdhci_update_irq(s);
+            break;
+        }
+    }
+}
Quoted 6 lines
Quoted 6 lines

So I think the guest can make this loop never terminate if it sets up a loop of ACT_LINK descriptors, right? I don't know how we should handle this but I'm pretty sure "make qemu sit there forever not responding
to anything and not resettable" isn't it.

Can I suggest that this is a general problem, that needs to be solved by the AIO layer. The AIO+block layer uses coroutines to do chunk-by-chunk non-blocking IO. I dont see how this is different. The problem is solved by setting up asynchronous DMA transactions or even better, arbitrary asynchronous hardware events. AIO DMA would have a similar api to the block layer and would solve this problem once, rather than having to put ptimers or coroutine-foo in every SGDMA capable devices to watchdog for infinite loops.

Quoted 5 lines
Quoted 2 lines

-- PMM

That could only happen if guest would do this on purpose, but I see your point. There's no way for us to tell if SD card read/write succeeded or not,

I think we are talking about corner cases here. If there is major infrastructural developments needed to do this properly (which I think there might very well be), then can we declare this issue out of scope of this series and come back to this as an incremental development.

To summarise, its a hard problem to solve with minimal reward.

Regards,
Peter

Quoted 5 lines

so I think the only way to to this is to suspend transfer after some reasonable amount of loops and resume it by qemu_timer, giving CPU time to do something useful.
I've never seen long ADMA loops, so it wouldn't reflect on performance in any way.

#8 lore raw

Re: [Qemu-devel] [PATCH v6 1/4] hw: introduce standard SD host controller

Ping!

Any further thoughts here?

There seem to be a few minor correction for PPM, but the sore-thumb issue is the long/infinite ADMA. Is there an (easy) AIO based solution to be had or do we need to do some sort of ptimer hack?

Regards,
Peter

Quoted 43 lines · Peter Crosthwaite
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
hw/sdhci.c new file +5 -0
+            sdhci_update_irq(s);
+            break;
+        }
+    }
+}
Quoted 6 lines
Quoted 6 lines

So I think the guest can make this loop never terminate if it sets up a loop of ACT_LINK descriptors, right? I don't know how we should handle this but I'm pretty sure "make qemu sit there forever not responding
to anything and not resettable" isn't it.

Can I suggest that this is a general problem, that needs to be solved by the AIO layer. The AIO+block layer uses coroutines to do chunk-by-chunk non-blocking IO. I dont see how this is different. The problem is solved by setting up asynchronous DMA transactions or even better, arbitrary asynchronous hardware events. AIO DMA would have a similar api to the block layer and would solve this problem once, rather than having to put ptimers or coroutine-foo in every SGDMA capable devices to watchdog for infinite loops.

Quoted 5 lines
Quoted 2 lines

-- PMM

That could only happen if guest would do this on purpose, but I see your point. There's no way for us to tell if SD card read/write succeeded or not,

I think we are talking about corner cases here. If there is major infrastructural developments needed to do this properly (which I think there might very well be), then can we declare this issue out of scope of this series and come back to this as an incremental development.

To summarise, its a hard problem to solve with minimal reward.

Regards,
Peter

Quoted 5 lines

so I think the only way to to this is to suspend transfer after some reasonable amount of loops and resume it by qemu_timer, giving CPU time to do something useful.
I've never seen long ADMA loops, so it wouldn't reflect on performance in any way.

#13 lore raw

Re: [Qemu-devel] [PATCH v6 2/4] exynos4210: Added SD host controller model

In reply to Igor Mitsyanko

Ping!

Igor, are you able to provide a diff of this patch so I can send the next revision?

Regards,
Peter

Quoted 105 lines · Igor Mitsyanko
Quoted 24 lines · Peter Maydell
Quoted 8 lines · Peter A. G. Crosthwaite
hw/exynos4210_sdhci.c new file +8 -0
+static uint64_t
+exynos4210_sdhci_readfn(void *opaque, target_phys_addr_t offset, unsigned size)
+{
+    Exynos4SDHCIState *s = (Exynos4SDHCIState *)opaque;
+    uint32_t ret;
+
+    switch (offset & ~0x3) {
+    case SDHC_BDATA:

hw/exynos4210_sdhci.c line 293

Quoted 10 lines
hw/exynos4210_sdhci.c new file +10 -0
+        /* Buffer data port read can be disabled by CONTROL2 register */
+        if (s->control2 & EXYNOS4_SDHC_DISBUFRD) {
+            ret = 0;
+        } else {
+            ret = SDHCI_GET_CLASS(s)->mem_read(SDHCI(s), offset, size);
+        }
+        break;
+    case SDHC_ADMAERR:
+        ret = (s->admaerr >> 8 * (offset - SDHC_ADMAERR)) &
+                ((1 << 8 * size) - 1);

If size == 4 you've just shifted right by 32, which is undefined behaviour when ints are 32 bits. Try

Quoted 3 lines
    ret = extract32(s->admaerr, (offset & 3) << 3, size * 8);

and similarly below.

Ok

Quoted 16 lines
Quoted 16 lines
hw/exynos4210_sdhci.c new file +16 -0
+static void exynos4210_sdhci_writefn(void *opaque, target_phys_addr_t offset,
+        uint64_t val, unsigned size)
+{
+    Exynos4SDHCIState *s = (Exynos4SDHCIState *)opaque;
+    SDHCIState *sdhci = SDHCI(s);
+    unsigned shift;
+
+    DPRINT_L2("write %ub: addr[0x%04x] <- %u(0x%x)\n", size, (uint32_t)offset,
+            (uint32_t)val, (uint32_t)val);
+
+    switch (offset) {
+    case SDHC_CLKCON:
+        if ((val & SDHC_CLOCK_SDCLK_EN) &&
+                (sdhci->prnsts & SDHC_CARD_PRESENT)) {
+            val |= EXYNOS4_SDHC_SDCLK_STBL;
+        } else {
Quoted 8 lines

hw/exynos4210_sdhci.c line 335

Quoted 5 lines
hw/exynos4210_sdhci.c new file +5 -0
+            val &= ~EXYNOS4_SDHC_SDCLK_STBL;
+        }
+        /* Break out to superclass write to handle the rest of this register */
+        break;
+    case EXYNOS4_SDHC_CONTROL2 ... EXYNOS4_SDHC_CONTROL2 + 3:

Why do we switch (offset & 3) in the readfn but switch (offset) and use case FOO ... FOO + 3 in the writefn? Consistency would be nice.

I think I'll change readfn() switch to match writefn then, to avoid complicating SDHC_CLKON case.

Quoted 32 lines

hw/exynos4210_sdhci.c line 338

Quoted 3 lines
hw/exynos4210_sdhci.c new file +3 -0
+        shift = (offset - EXYNOS4_SDHC_CONTROL2) * 8;
+        s->control2 = (s->control2 & ~(((1 << 8 * size) - 1) << shift)) |
+                (val << shift);

s->control2 = deposit32(s->control2, (offset & 3) << 3, size * 8, val);

and similarly below.

Quoted 25 lines
hw/exynos4210_sdhci.c new file +25 -0
+    case SDHC_ADMAERR ... SDHC_ADMAERR + 3:
+        if (size == 4 || (size == 2 && offset == SDHC_ADMAERR) ||
+                (size == 1 && offset == (SDHC_ADMAERR + 1))) {
+            uint32_t mask = 0;
+
+            if (size == 2) {
+                mask = 0xFFFF0000;
+            } else if (size == 1) {
+                mask = 0xFFFF00FF;
+                val <<= 8;
+            }
+
+            s->admaerr = (s->admaerr & (mask | EXYNOS4_SDHC_FINAL_BLOCK |
+               EXYNOS4_SDHC_IRQ_STAT)) | (val & ~(EXYNOS4_SDHC_FINAL_BLOCK |
+               EXYNOS4_SDHC_IRQ_STAT | EXYNOS4_SDHC_CONTINUE_REQ));
+            s->admaerr &= ~(val & EXYNOS4_SDHC_IRQ_STAT);
+            if ((s->stopped_adma) && (val & EXYNOS4_SDHC_CONTINUE_REQ) &&
+                (SDHC_DMA_TYPE(sdhci->hostctl) == SDHC_CTRL_ADMA2_32)) {
+                s->stopped_adma = false;
+                SDHCI_GET_CLASS(sdhci)->do_adma(sdhci);
+            }
+        } else {
+            uint32_t mask = (1 << (size * 8)) - 1;
+            shift = 8 * (offset & 0x3);
+            val <<= shift;
Quoted 8 lines

hw/exynos4210_sdhci.c line 373

Quoted 4 lines
hw/exynos4210_sdhci.c new file +4 -0
+            mask = ~(mask << shift);
+            s->admaerr = (s->admaerr & mask) | val;
+        }
+        return;

This case just looks odd. I think it would be clearer to first calculate the updated value of admaerr (using deposit32) and then act on the changes (xor of old and new value is handy to identify which bits are changed).

ok

Quoted 3 lines

-- PMM

#14 lore raw

Re: [Qemu-devel] [PATCH v6 2/4] exynos4210: Added SD host controller model

Quoted 4 lines · Peter Crosthwaite

Ping!

Igor, are you able to provide a diff of this patch so I can send the next revision?

Sure, but I still don't understand what to do with QEMU-lockup issue, I believe the same topic was discussed here http://thread.gmane.org/gmane.comp.emulators.qemu/169524, and the decision was to use runtime loop detection?

Quoted 106 lines

Regards,
Peter

Quoted 100 lines · Igor Mitsyanko
Quoted 24 lines · Peter Maydell
Quoted 8 lines · Peter A. G. Crosthwaite
hw/exynos4210_sdhci.c new file +8 -0
+static uint64_t
+exynos4210_sdhci_readfn(void *opaque, target_phys_addr_t offset, unsigned size)
+{
+    Exynos4SDHCIState *s = (Exynos4SDHCIState *)opaque;
+    uint32_t ret;
+
+    switch (offset & ~0x3) {
+    case SDHC_BDATA:

hw/exynos4210_sdhci.c line 293

Quoted 10 lines
hw/exynos4210_sdhci.c new file +10 -0
+        /* Buffer data port read can be disabled by CONTROL2 register */
+        if (s->control2 & EXYNOS4_SDHC_DISBUFRD) {
+            ret = 0;
+        } else {
+            ret = SDHCI_GET_CLASS(s)->mem_read(SDHCI(s), offset, size);
+        }
+        break;
+    case SDHC_ADMAERR:
+        ret = (s->admaerr >> 8 * (offset - SDHC_ADMAERR)) &
+                ((1 << 8 * size) - 1);

If size == 4 you've just shifted right by 32, which is undefined behaviour when ints are 32 bits. Try

Quoted 3 lines
     ret = extract32(s->admaerr, (offset & 3) << 3, size * 8);

and similarly below.

Ok

Quoted 16 lines
Quoted 16 lines
hw/exynos4210_sdhci.c new file +16 -0
+static void exynos4210_sdhci_writefn(void *opaque, target_phys_addr_t offset,
+        uint64_t val, unsigned size)
+{
+    Exynos4SDHCIState *s = (Exynos4SDHCIState *)opaque;
+    SDHCIState *sdhci = SDHCI(s);
+    unsigned shift;
+
+    DPRINT_L2("write %ub: addr[0x%04x] <- %u(0x%x)\n", size, (uint32_t)offset,
+            (uint32_t)val, (uint32_t)val);
+
+    switch (offset) {
+    case SDHC_CLKCON:
+        if ((val & SDHC_CLOCK_SDCLK_EN) &&
+                (sdhci->prnsts & SDHC_CARD_PRESENT)) {
+            val |= EXYNOS4_SDHC_SDCLK_STBL;
+        } else {
Quoted 8 lines

hw/exynos4210_sdhci.c line 335

Quoted 5 lines
hw/exynos4210_sdhci.c new file +5 -0
+            val &= ~EXYNOS4_SDHC_SDCLK_STBL;
+        }
+        /* Break out to superclass write to handle the rest of this register */
+        break;
+    case EXYNOS4_SDHC_CONTROL2 ... EXYNOS4_SDHC_CONTROL2 + 3:

Why do we switch (offset & 3) in the readfn but switch (offset) and use case FOO ... FOO + 3 in the writefn? Consistency would be nice.

I think I'll change readfn() switch to match writefn then, to avoid complicating SDHC_CLKON case.

Quoted 32 lines

hw/exynos4210_sdhci.c line 338

Quoted 3 lines
hw/exynos4210_sdhci.c new file +3 -0
+        shift = (offset - EXYNOS4_SDHC_CONTROL2) * 8;
+        s->control2 = (s->control2 & ~(((1 << 8 * size) - 1) << shift)) |
+                (val << shift);
    s->control2 = deposit32(s->control2, (offset & 3) << 3, size * 8, val);

and similarly below.

Quoted 25 lines
hw/exynos4210_sdhci.c new file +25 -0
+    case SDHC_ADMAERR ... SDHC_ADMAERR + 3:
+        if (size == 4 || (size == 2 && offset == SDHC_ADMAERR) ||
+                (size == 1 && offset == (SDHC_ADMAERR + 1))) {
+            uint32_t mask = 0;
+
+            if (size == 2) {
+                mask = 0xFFFF0000;
+            } else if (size == 1) {
+                mask = 0xFFFF00FF;
+                val <<= 8;
+            }
+
+            s->admaerr = (s->admaerr & (mask | EXYNOS4_SDHC_FINAL_BLOCK |
+               EXYNOS4_SDHC_IRQ_STAT)) | (val & ~(EXYNOS4_SDHC_FINAL_BLOCK |
+               EXYNOS4_SDHC_IRQ_STAT | EXYNOS4_SDHC_CONTINUE_REQ));
+            s->admaerr &= ~(val & EXYNOS4_SDHC_IRQ_STAT);
+            if ((s->stopped_adma) && (val & EXYNOS4_SDHC_CONTINUE_REQ) &&
+                (SDHC_DMA_TYPE(sdhci->hostctl) == SDHC_CTRL_ADMA2_32)) {
+                s->stopped_adma = false;
+                SDHCI_GET_CLASS(sdhci)->do_adma(sdhci);
+            }
+        } else {
+            uint32_t mask = (1 << (size * 8)) - 1;
+            shift = 8 * (offset & 0x3);
+            val <<= shift;
Quoted 8 lines

hw/exynos4210_sdhci.c line 373

Quoted 4 lines
hw/exynos4210_sdhci.c new file +4 -0
+            mask = ~(mask << shift);
+            s->admaerr = (s->admaerr & mask) | val;
+        }
+        return;

This case just looks odd. I think it would be clearer to first calculate the updated value of admaerr (using deposit32) and then act on the changes (xor of old and new value is handy to identify which bits are changed).

ok

Quoted 2 lines

-- PMM