From nobody Wed Apr 2 14:18:38 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1742810762; cv=none; d=zohomail.com; s=zohoarc; b=VYbuBIUFXDGyefKRvLgA70BzKtu1ldEb58+YehB5O2oQIn9hRRd1YAw0P5IxvS7TK2FanRrJAi32Fx2cHoNQ+Ly7LfqBHAbsxvG49g295xA+O33VBiqtnP40AKT0Je7vABRK8HVWH0WsAcmVXZb/9EkGDWuzvijA5duN1YrQtgI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1742810762; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=QqJrsjo0/Jnyhmcx9AVbwB4QSRAf6LZwRSaeKYTnp7E=; b=JbFakY3W9M2C8RgnAzgFNUqHd5L63T8mAOe7Iqim0feaNhIB41d8NF+XMNxOpJyBgQDfjfra49Lr1/K0nJYh7gxjRqFomo1wBm847VzmSMytwDWF4+peoasncYgwXKjRdY+2vgK8cz+mkLK27/jsmdyQw9USUOzoHc2el+Mctpc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1742810762112188.87543613943456; Mon, 24 Mar 2025 03:06:02 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1twegq-0006Db-A2; Mon, 24 Mar 2025 06:05:53 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1twegU-00062c-5e; Mon, 24 Mar 2025 06:05:31 -0400 Received: from forward500d.mail.yandex.net ([178.154.239.208]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1twegO-000521-9g; Mon, 24 Mar 2025 06:05:29 -0400 Received: from mail-nwsmtp-smtp-production-main-80.klg.yp-c.yandex.net (mail-nwsmtp-smtp-production-main-80.klg.yp-c.yandex.net [IPv6:2a02:6b8:c42:2347:0:640:6ae5:0]) by forward500d.mail.yandex.net (Yandex) with ESMTPS id EDDB36105E; Mon, 24 Mar 2025 13:05:15 +0300 (MSK) Received: by mail-nwsmtp-smtp-production-main-80.klg.yp-c.yandex.net (smtp/Yandex) with ESMTPSA id D5L6Pk2LeW20-UMVCtpRF; Mon, 24 Mar 2025 13:05:15 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=maquefel.me; s=mail; t=1742810715; bh=QqJrsjo0/Jnyhmcx9AVbwB4QSRAf6LZwRSaeKYTnp7E=; h=Message-ID:Date:In-Reply-To:Cc:Subject:References:To:From; b=m8LUbjYiKh+6D5ngihA/n4m9ZvGqywhKNI+3dn9VpH1jVavJUz/toLmRL6325DKcP 4sFtcDpGhmv+lE0CxvodeTgJVGWnsSycQMEvJzQG+FZN+7AzYHAMdKfd+3/BmARIlg jkToMIVsUQrvwMaVFA2LX/GZzVdIHaIWsPiYv6V8= Authentication-Results: mail-nwsmtp-smtp-production-main-80.klg.yp-c.yandex.net; dkim=pass header.i=@maquefel.me From: Nikita Shubin To: qemu-devel@nongnu.org Cc: Ilya Chichkov , Nikita Shubin , Paolo Bonzini , Alistair Francis , Peter Maydell , qemu-arm@nongnu.org Subject: [PATCH 1/3] hw/dma: Add STM32 platfrom DMA controller emulation Date: Mon, 24 Mar 2025 13:05:06 +0300 Message-ID: <20250324100508.2176-2-nikita.shubin@maquefel.me> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250324100508.2176-1-nikita.shubin@maquefel.me> References: <20250324100508.2176-1-nikita.shubin@maquefel.me> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=178.154.239.208; envelope-from=nikita.shubin@maquefel.me; helo=forward500d.mail.yandex.net X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=-0.01, RCVD_IN_MSPIKE_WL=-0.01, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @maquefel.me) X-ZM-MESSAGEID: 1742810763852116600 Content-Type: text/plain; charset="utf-8" From: Nikita Shubin STMicroelectronics STM32 SoCs integrate DMA engine that supports: * Independent concurrent DMA transfers using 7/5 DMA channels * Generation of interrupts on various conditions during execution * PERIPH to MEMORY transactions, invoked by peripheral device models * MEMORY to MEMORY transactions, invoked by software This model is compatible with STM32F1xxxx and GD32F30x. Co-developed-by: Ilya Chichkov Signed-off-by: Ilya Chichkov Signed-off-by: Nikita Shubin --- hw/dma/Kconfig | 4 + hw/dma/meson.build | 1 + hw/dma/stm32_dma.c | 564 +++++++++++++++++++++++++++++++++++++ hw/dma/trace-events | 15 + include/hw/dma/stm32_dma.h | 46 +++ 5 files changed, 630 insertions(+) create mode 100644 hw/dma/stm32_dma.c create mode 100644 include/hw/dma/stm32_dma.h diff --git a/hw/dma/Kconfig b/hw/dma/Kconfig index 98fbb1bb04..f1483a1ce5 100644 --- a/hw/dma/Kconfig +++ b/hw/dma/Kconfig @@ -30,3 +30,7 @@ config SIFIVE_PDMA config XLNX_CSU_DMA bool select REGISTER + +config STM32_DMA + bool + select REGISTER \ No newline at end of file diff --git a/hw/dma/meson.build b/hw/dma/meson.build index cc7810beb8..4db1aca955 100644 --- a/hw/dma/meson.build +++ b/hw/dma/meson.build @@ -12,3 +12,4 @@ system_ss.add(when: 'CONFIG_OMAP', if_true: files('omap_d= ma.c', 'soc_dma.c')) system_ss.add(when: 'CONFIG_RASPI', if_true: files('bcm2835_dma.c')) system_ss.add(when: 'CONFIG_SIFIVE_PDMA', if_true: files('sifive_pdma.c')) system_ss.add(when: 'CONFIG_XLNX_CSU_DMA', if_true: files('xlnx_csu_dma.c'= )) +system_ss.add(when: 'CONFIG_STM32_DMA', if_true: files('stm32_dma.c')) diff --git a/hw/dma/stm32_dma.c b/hw/dma/stm32_dma.c new file mode 100644 index 0000000000..b4d588664d --- /dev/null +++ b/hw/dma/stm32_dma.c @@ -0,0 +1,564 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * QEMU STM32 Direct memory access controller (DMA). + * + * This includes STM32F1xxxx, STM32F2xxxx and GD32F30x + * + * Author: 2025 Nikita Shubin + */ +#include "qemu/osdep.h" + +#include "qapi/error.h" +#include "hw/qdev-properties.h" +#include "hw/qdev-properties-system.h" +#include "hw/sysbus.h" +#include "migration/vmstate.h" +#include "hw/irq.h" +#include "qemu/error-report.h" +#include "qemu/log.h" +#include "qemu/module.h" +#include "hw/register.h" +#include "system/dma.h" + +#include "trace.h" + +#include "hw/dma/stm32_dma.h" + +#define STM32_DMA_APERTURE_SIZE 0x400 + +/* Global interrupt flag */ +#define DMA_ISR_GIF BIT(0) +/* Full transfer finish */ +#define DMA_ISR_TCIF BIT(1) +/* Half transfer finish */ +#define DMA_ISR_HTIF BIT(2) +/* Transfer error */ +#define DMA_ISR_TEIF BIT(3) + +/* Interrupt flag register (DMA_ISR) */ +REG32(DMA_ISR, 0x00) + FIELD(DMA_ISR, CHAN0, 0, 4) + FIELD(DMA_ISR, CHAN1, 4, 4) + FIELD(DMA_ISR, CHAN2, 8, 4) + FIELD(DMA_ISR, CHAN3, 12, 4) + FIELD(DMA_ISR, CHAN4, 16, 4) + FIELD(DMA_ISR, CHAN5, 20, 4) + FIELD(DMA_ISR, CHAN6, 24, 4) + FIELD(DMA_ISR, RSVD, 28, 4) + +/* Interrupt flag clear register (DMA_IFCR) */ +REG32(DMA_IFCR, 0x04) + FIELD(DMA_IFCR, CHAN0, 0, 4) + FIELD(DMA_IFCR, CHAN1, 4, 4) + FIELD(DMA_IFCR, CHAN2, 8, 4) + FIELD(DMA_IFCR, CHAN3, 12, 4) + FIELD(DMA_IFCR, CHAN4, 16, 4) + FIELD(DMA_IFCR, CHAN5, 20, 4) + FIELD(DMA_IFCR, CHAN6, 24, 4) + FIELD(DMA_IFCR, RSVD, 28, 4) + +/* Channel x control register (DMA_CCRx) */ +/* Address offset: 0x08 + 0x14 * x */ +REG32(DMA_CCR, 0x08) + FIELD(DMA_CCR, EN, 0, 1) + FIELD(DMA_CCR, TCIE, 1, 1) + FIELD(DMA_CCR, HTIE, 2, 1) + FIELD(DMA_CCR, TEIE, 3, 1) + + /* all below RO if chan enabled */ + FIELD(DMA_CCR, DIR, 4, 1) + FIELD(DMA_CCR, CIRC, 5, 1) + FIELD(DMA_CCR, PINC, 6, 1) + FIELD(DMA_CCR, MINC, 7, 1) + FIELD(DMA_CCR, PSIZE, 8, 2) + FIELD(DMA_CCR, MSIZE, 10, 2) + FIELD(DMA_CCR, PL, 12, 2) + FIELD(DMA_CCR, M2M, 14, 1) + FIELD(DMA_CCR, RSVD, 15, 17) + +#define DMA_CCR_RO_MASK \ + (R_DMA_CCR_DIR_MASK \ + | R_DMA_CCR_CIRC_MASK \ + | R_DMA_CCR_PINC_MASK \ + | R_DMA_CCR_MINC_MASK \ + | R_DMA_CCR_PSIZE_MASK \ + | R_DMA_CCR_MSIZE_MASK \ + | R_DMA_CCR_PL_MASK \ + | R_DMA_CCR_M2M_MASK) + +/* Channel x counter register (DMA_CNDTRx) */ +/* Address offset: 0x0C + 0x14 * x */ +REG32(DMA_CNDTR, 0x0c) + FIELD(DMA_CNDTR, NDT, 0, 16) + FIELD(DMA_CNDTR, RSVD, 16, 16) + +/* Channel x peripheral base address register (DMA_CPARx) */ +/* Address offset: 0x10 + 0x14 * x */ +REG32(DMA_CPAR, 0x10) + FIELD(DMA_CPAR, PA, 0, 32) + +/* Channel x memory base address register (DMA_CMARx) */ +/* 0x14 + 0x14 * x */ +REG32(DMA_CMAR, 0x14) + FIELD(DMA_CMAR, MA, 0, 32) + +#define A_DMA_CCR0 0x08 +#define A_DMA_CMAR7 0xa0 + +static void stm32_dma_chan_update_intr(STM32DmaState *s, uint8_t idx) +{ + if (extract32(s->intf, idx * 4, 4)) { + /* set GIFCx */ + set_bit32(idx * 4, &s->intf); + qemu_irq_raise(s->output[idx]); + } +} + +static MemTxResult stm32_dma_transfer_one(STM32DmaState *s, uint8_t idx) +{ + STM32DmaChannel *chan =3D &s->chan[idx]; + uint8_t pwidth =3D 1 << FIELD_EX32(chan->chctl, DMA_CCR, PSIZE); + uint8_t mwidth =3D 1 << FIELD_EX32(chan->chctl, DMA_CCR, MSIZE); + hwaddr paddr, maddr; + MemTxResult res =3D MEMTX_OK; + uint32_t data =3D 0; + + paddr =3D chan->chpaddr; + if (FIELD_EX32(chan->chctl, DMA_CCR, PINC)) { + /* increment algorithm enabled */ + uint32_t incr =3D chan->chcnt_shadow - chan->chcnt; + + paddr +=3D pwidth * incr; + } + + maddr =3D chan->chmaddr; + if (FIELD_EX32(chan->chctl, DMA_CCR, MINC)) { + /* increment algorithm enabled */ + uint32_t incr =3D chan->chcnt_shadow - chan->chcnt; + + maddr +=3D mwidth * incr; + } + + /* issue transaction */ + if (FIELD_EX32(chan->chctl, DMA_CCR, DIR)) { + /* FROM memory TO peripheral */ + res =3D dma_memory_read(&address_space_memory, maddr, &data, + pwidth, MEMTXATTRS_UNSPECIFIED); + if (res) { + goto fail_rw; + } + + res =3D dma_memory_write(&address_space_memory, paddr, &data, + mwidth, MEMTXATTRS_UNSPECIFIED); + if (res) { + goto fail_rw; + } + + trace_stm32_dma_transfer(idx, maddr, mwidth, + paddr, pwidth, data); + } else { + /* FROM peripheral TO memory */ + res =3D dma_memory_read(&address_space_memory, paddr, &data, + pwidth, MEMTXATTRS_UNSPECIFIED); + if (res) { + goto fail_rw; + } + + res =3D dma_memory_write(&address_space_memory, maddr, &data, + mwidth, MEMTXATTRS_UNSPECIFIED); + if (res) { + goto fail_rw; + } + + trace_stm32_dma_transfer(idx, paddr, pwidth, + maddr, mwidth, data); + } + + if (FIELD_EX32(chan->chctl, DMA_CCR, HTIE)) { + /* Issue completed half transfer interrupt */ + trace_stm32_dma_raise(idx, "HTIE"); + set_bit(idx * 4 + 2, (unsigned long *)&s->intf); + } + + return res; + +fail_rw: + if (FIELD_EX32(chan->chctl, DMA_CCR, TEIE)) { + trace_stm32_dma_raise(idx, "TEIE"); + set_bit(idx * 4 + 3, (unsigned long *)&s->intf); + } + + trace_stm32_dma_transfer_fail(idx, paddr, maddr); + return res; +} + +static void stm32_dma_transfer(STM32DmaState *s, uint8_t idx, bool m2m) +{ + STM32DmaChannel *chan =3D &s->chan[idx]; + MemTxResult res =3D 0; + + if (!chan->enabled || chan->chcnt =3D=3D 0) { + trace_stm32_dma_disabled(idx, chan->enabled, chan->chcnt); + return; + } + + do { + res =3D stm32_dma_transfer_one(s, idx); + if (res) { + goto fail_rw; + } + + chan->chcnt--; + } while (chan->chcnt && m2m); + + /* rearm counter */ + if (chan->chcnt =3D=3D 0) { + if (FIELD_EX32(chan->chctl, DMA_CCR, TCIE)) { + /* Issue completed full transfer interrupt */ + trace_stm32_dma_raise(idx, "TCIE"); + set_bit(idx * 4 + 1, (unsigned long *)&s->intf); + } + + /* M2M mode can't be circular */ + if (!FIELD_EX32(chan->chctl, DMA_CCR, M2M) && + FIELD_EX32(chan->chctl, DMA_CCR, CIRC)) { + chan->chcnt =3D chan->chcnt_shadow; + trace_stm32_dma_cmen(idx, chan->chcnt); + } + } + +fail_rw: + stm32_dma_chan_update_intr(s, idx); + return; +} + +static uint32_t stm32_dma_chan_read(STM32DmaState *s, hwaddr addr) +{ + uint8_t idx =3D (addr - A_DMA_CCR0) / 0x14 /* STRIDE */; + uint8_t reg =3D (addr - 0x14 /* STRIDE */ * idx); + uint32_t val =3D 0; + + if (idx > s->nr_chans) { + qemu_log_mask(LOG_GUEST_ERROR, + "%s: chan_idx %d exceed %d number of channels\n", + __func__, idx, s->nr_chans); + return val; + } + + switch (reg) { + case A_DMA_CCR: + val =3D s->chan[idx].chctl; + break; + case A_DMA_CNDTR: + val =3D s->chan[idx].chcnt; + break; + case A_DMA_CPAR: + val =3D s->chan[idx].chpaddr; + break; + case A_DMA_CMAR: + val =3D s->chan[idx].chmaddr; + break; + default: + qemu_log_mask(LOG_GUEST_ERROR, + "%s: unknown reg 0x%x\n", + __func__, reg); + break; + } + + trace_stm32_dma_chan_read(addr, idx, reg, val); + + return val; +} + +static uint64_t stm32_dma_read(void *opaque, hwaddr addr, unsigned int siz= e) +{ + STM32DmaState *s =3D STM32_DMA(opaque); + uint32_t val =3D 0; + + switch (addr) { + case A_DMA_ISR: + val =3D s->intf; + break; + case A_DMA_CCR0 ... A_DMA_CMAR7: + val =3D stm32_dma_chan_read(s, addr); + break; + /* write-only */ + case A_DMA_IFCR: + default: + /* + * TODO: WARN_ONCE ? If left as is produces spam, because many + * people use '|=3D' on write-only registers. + * qemu_log_mask(LOG_GUEST_ERROR, + * "%s: read to unimplemented register " \ + * "at address: 0x%" PRIx64 " size %d\n", + * __func__, addr, size); + */ + break; + }; + + trace_stm32_dma_read(addr); + + return val; +} + +static void stm32_dma_update_chan_ctrl(STM32DmaState *s, uint8_t idx, + uint32_t val) +{ + int is_enabled, was_enabled; + + was_enabled =3D !!FIELD_EX32(s->chan[idx].chctl, DMA_CCR, EN); + is_enabled =3D !!FIELD_EX32(val, DMA_CCR, EN); + + if (was_enabled && is_enabled) { + uint32_t protected =3D (s->chan[idx].chctl ^ val) & DMA_CCR_RO_MAS= K; + if (protected) { + qemu_log_mask(LOG_GUEST_ERROR, + "%s: attempt to write enabled chan_idx %d settin= gs " + "with val 0x%x\n", __func__, idx, val); + } + + val &=3D ~DMA_CCR_RO_MASK; + val |=3D DMA_CCR_RO_MASK & s->chan[idx].chctl; + } + + s->chan[idx].chctl =3D val; + s->chan[idx].enabled =3D !!FIELD_EX32(s->chan[idx].chctl, DMA_CCR, EN); + + if (!was_enabled && is_enabled) { + if (FIELD_EX32(s->chan[idx].chctl, DMA_CCR, M2M)) { + stm32_dma_transfer(s, idx, true); + } + } +} + +static void stm32_dma_chan_write(STM32DmaState *s, hwaddr addr, + uint64_t val) +{ + uint8_t idx =3D (addr - A_DMA_CCR0) / 0x14 /* STRIDE */; + uint8_t reg =3D (addr - 0x14 /* STRIDE */ * idx); + + if (idx > s->nr_chans) { + qemu_log_mask(LOG_GUEST_ERROR, + "%s: chan_idx %d exceed %d number of channels\n", + __func__, idx, s->nr_chans); + return; + } + + trace_stm32_dma_chan_write(addr, idx, reg, val); + + if (reg =3D=3D A_DMA_CCR) { + stm32_dma_update_chan_ctrl(s, idx, val); + return; + } + + if (s->chan[idx].enabled) { + qemu_log_mask(LOG_GUEST_ERROR, + "%s: attempt to changed enabled chan_idx %d settings= \n", + __func__, idx); + return; + } + + switch (reg) { + case A_DMA_CNDTR: + s->chan[idx].chcnt =3D FIELD_EX32(val, DMA_CNDTR, NDT); + s->chan[idx].chcnt_shadow =3D s->chan[idx].chcnt; + break; + case A_DMA_CPAR: + s->chan[idx].chpaddr =3D val; + break; + case A_DMA_CMAR: + s->chan[idx].chmaddr =3D val; + break; + default: + qemu_log_mask(LOG_GUEST_ERROR, + "%s: unknown reg 0x%x\n", + __func__, reg); + break; + } +} + +static void stm32_dma_intr_ack(STM32DmaState *s, uint32_t val) +{ + int i, j; + uint32_t changed =3D val & s->intf; + + if (!changed) { + return; + } + + for (i =3D 0, j =3D 0; i < R_DMA_IFCR_RSVD_SHIFT; i +=3D 4, j++) { + uint8_t bits_changed =3D extract32(changed, i, 4); + if (bits_changed) { + /* clear bits */ + uint8_t bits =3D 0; + + /* Clear global interrupt flag of channel */ + if (!(bits_changed & BIT(0))) { + bits =3D extract32(s->intf, i, 4) & ~bits_changed; + } + + s->intf =3D deposit32(s->intf, i , 4, bits); + if (!bits) { + trace_stm32_dma_lower(j); + qemu_irq_lower(s->output[j]); + } + } + } +} + +static void stm32_dma_write(void *opaque, hwaddr addr, + uint64_t val, unsigned int size) +{ + STM32DmaState *s =3D STM32_DMA(opaque); + + switch (addr) { + case A_DMA_IFCR: + stm32_dma_intr_ack(s, val); + break; + case A_DMA_CCR0 ... A_DMA_CMAR7: + stm32_dma_chan_write(s, addr, val); + break; + /* read-only */ + case A_DMA_ISR: + default: + qemu_log_mask(LOG_GUEST_ERROR, "%s: write to unimplemented regist= er " \ + "at address: 0x%" PRIx64 " size=3D%d val=3D0x%" PRIx= 64 "\n", + __func__, addr, size, val); + break; + }; + + trace_stm32_dma_write(addr, val); +} + +static const MemoryRegionOps dma_ops =3D { + .read =3D stm32_dma_read, + .write =3D stm32_dma_write, + .endianness =3D DEVICE_LITTLE_ENDIAN, + .impl =3D { + .min_access_size =3D 4, + .max_access_size =3D 4, + }, +}; + +static void stm32_dma_reset_enter(Object *obj, ResetType type) +{ + STM32DmaState *s =3D STM32_DMA(obj); + + s->intf =3D 0x0; + + for (int i =3D 0; i < s->nr_chans; i++) { + s->chan[i].chctl =3D 0; + s->chan[i].chcnt =3D 0; + s->chan[i].chpaddr =3D 0; + s->chan[i].chmaddr =3D 0; + + s->chan[i].enabled =3D false; + } + + trace_stm32_dma_reset("reset_enter"); +} + +static void stm32_dma_reset_hold(Object *obj, ResetType type) +{ + STM32DmaState *s =3D STM32_DMA(obj); + + for (int i =3D 0; i < s->nr_chans; i++) { + qemu_irq_lower(s->output[i]); + } + + trace_stm32_dma_reset("reset_hold"); +} + +/* irq from peripheral */ +static void stm32_dma_set(void *opaque, int line, int value) +{ + STM32DmaState *s =3D STM32_DMA(opaque); + + if (line > s->nr_chans) { + qemu_log_mask(LOG_GUEST_ERROR, + "%s: requested non-existant line %d > %d\n", + __func__, line, s->nr_chans); + return; + } + + /* start dma transfer */ + stm32_dma_transfer(s, line, false); + + trace_stm32_dma_set(line, value); +} + +static void stm32_dma_realize(DeviceState *dev, Error **errp) +{ + STM32DmaState *s =3D STM32_DMA(dev); + + memory_region_init_io(&s->mmio, OBJECT(dev), &dma_ops, s, + TYPE_STM32_DMA, STM32_DMA_APERTURE_SIZE); + + sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->mmio); + + qdev_init_gpio_in(DEVICE(s), stm32_dma_set, s->nr_chans); + for (int i =3D 0; i < s->nr_chans; i++) { + sysbus_init_irq(SYS_BUS_DEVICE(dev), &s->output[i]); + } +} + +static const VMStateDescription vmstate_stm32_dma_channel =3D { + .name =3D "stm32_dma_channel", + .version_id =3D 1, + .minimum_version_id =3D 1, + .fields =3D (const VMStateField[]) { + VMSTATE_BOOL(enabled, STM32DmaChannel), + VMSTATE_UINT32(chctl, STM32DmaChannel), + VMSTATE_UINT32(chcnt, STM32DmaChannel), + VMSTATE_UINT32(chpaddr, STM32DmaChannel), + VMSTATE_UINT32(chmaddr, STM32DmaChannel), + VMSTATE_UINT32(chcnt_shadow, STM32DmaChannel), + VMSTATE_END_OF_LIST() + } +}; + +static const VMStateDescription vmstate_stm32_dma =3D { + .name =3D TYPE_STM32_DMA, + .version_id =3D 1, + .minimum_version_id =3D 1, + .fields =3D (const VMStateField[]) { + VMSTATE_UINT8(nr_chans, STM32DmaState), + VMSTATE_UINT32(intf, STM32DmaState), + VMSTATE_STRUCT_ARRAY(chan, STM32DmaState, STM32_DMA_CHAN_NUMBER, + 1, vmstate_stm32_dma_channel, STM32DmaChannel= ), + VMSTATE_END_OF_LIST(), + } +}; + +static const Property stm32_dma_properties[] =3D { + DEFINE_PROP_UINT8("nchans", STM32DmaState, nr_chans, STM32_DMA_CHAN_NU= MBER), +}; + +static void stm32_dma_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc =3D DEVICE_CLASS(klass); + ResettableClass *rc =3D RESETTABLE_CLASS(klass); + + rc->phases.enter =3D stm32_dma_reset_enter; + rc->phases.hold =3D stm32_dma_reset_hold; + + dc->vmsd =3D &vmstate_stm32_dma; + dc->realize =3D stm32_dma_realize; + dc->desc =3D "STM32 DMA"; + + device_class_set_props(dc, stm32_dma_properties); +} + +static const TypeInfo stm32_dma_info =3D { + .name =3D TYPE_STM32_DMA, + .parent =3D TYPE_SYS_BUS_DEVICE, + .instance_size =3D sizeof(STM32DmaState), + .class_init =3D stm32_dma_class_init, +}; + +static void stm32_dma_register_types(void) +{ + type_register_static(&stm32_dma_info); +} + +type_init(stm32_dma_register_types) diff --git a/hw/dma/trace-events b/hw/dma/trace-events index 4c09790f9a..8d70fea582 100644 --- a/hw/dma/trace-events +++ b/hw/dma/trace-events @@ -47,3 +47,18 @@ pl330_iomem_read(uint32_t addr, uint32_t data) "addr: 0x= %08"PRIx32" data: 0x%08" =20 # xilinx_axidma.c xilinx_axidma_loading_desc_fail(uint32_t res) "error:%u" + +# stm32_dma.c +stm32_dma_reset(const char *type) "%s" +stm32_dma_set(int line, int value) "line %d, value %d" +stm32_dma_transfer(uint8_t idx, uint64_t src, uint8_t swidth, uint64_t dst= , uint8_t dwidth, uint32_t data) "idx: %d 0x%08" PRIx64"[%u] -> 0x%08" PRIx= 64 "[%u] 0x%x" +stm32_dma_read(uint64_t addr) "address: 0x%08"PRIx64 +stm32_dma_write(uint64_t addr, uint64_t val) "address: 0x%08"PRIx64" data:= 0x%08"PRIx64 +stm32_dma_chan_read(uint64_t addr, uint8_t idx, uint8_t reg, uint64_t val)= "address: 0x%08"PRIx64" idx: %u reg: %x data: 0x%08"PRIx64 +stm32_dma_chan_write(uint64_t addr, uint8_t idx, uint8_t reg, uint64_t val= ) "address: 0x%08"PRIx64" idx: %u reg: %x data: 0x%08"PRIx64 +stm32_dma_transfer_fail(uint8_t idx, uint64_t src, uint64_t dst) "idx: %d = 0x%08" PRIx64" -> 0x%08" PRIx64 +stm32_dma_raise(uint8_t idx, const char* type) "idx %d: %s" +stm32_dma_lower(uint8_t idx) "idx %d" +stm32_dma_cmen(uint8_t idx, uint32_t cnt) "idx %d cnt %u" +stm32_dma_disabled(uint8_t idx, bool enabled, uint32_t cnt) "idx %d enable= d %u cnt %u" + diff --git a/include/hw/dma/stm32_dma.h b/include/hw/dma/stm32_dma.h new file mode 100644 index 0000000000..4777037a71 --- /dev/null +++ b/include/hw/dma/stm32_dma.h @@ -0,0 +1,46 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * QEMU STM32 Direct memory access controller (DMA). + * + * This includes STM32F1xxxx, STM32F2xxxx and STM32F30x + * + * Author: 2025 Nikita Shubin + */ +#ifndef HW_STM32_DMA_H +#define HW_STM32_DMA_H + +#include "hw/sysbus.h" +#include "qom/object.h" + +#define STM32_DMA_CHAN_NUMBER 7 + +#define TYPE_STM32_DMA "stm32-dma" +OBJECT_DECLARE_SIMPLE_TYPE(STM32DmaState, STM32_DMA) + +typedef struct STM32DmaChannel { + bool enabled; + + uint32_t chctl; + uint32_t chcnt; + uint32_t chpaddr; + uint32_t chmaddr; + + uint32_t chcnt_shadow; +} STM32DmaChannel; + +typedef struct STM32DmaState { + /*< private >*/ + SysBusDevice parent_obj; + + /*< public >*/ + MemoryRegion mmio; + uint8_t nr_chans; + + uint32_t intf; + + STM32DmaChannel chan[STM32_DMA_CHAN_NUMBER]; + + qemu_irq output[STM32_DMA_CHAN_NUMBER]; +} STM32DmaState; + +#endif /* HW_STM32_DMA_H */ --=20 2.48.1