From nobody Thu Apr 9 19:00:49 2026 Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5A4073845AF for ; Fri, 6 Mar 2026 12:41:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772800909; cv=none; b=iuzOconxJeFg/wnea3wo0fLk5AFjR7Ui/tUJfR+dMafB/Wa8V8FTrGbv3R5TO1alFNa10kTPN7bCWV5RhjXMVDYhehFXYzBU1q3OEErt3tUPPsH0ugBqtYah2jkUsrk047sHayUMRVQOJoWZj2YbgjCO00HDqmEmu33JwWYepvU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772800909; c=relaxed/simple; bh=nSiQtEYZ1Ezr70vkp2ZeOST+tOl2pH5840ALPWqFaCs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qJGqqHtf7mDP5+sxgstT4l7bnbPgHVWGyNW8H5Aemi+Qva/KBKpCqVeQB2h3pP5oY8XoV4MV4XDP+w2L4YyVQz+zPCF/vRAjkB3nX4RCT15hjpHHy02bb7Lg1zk214p97Xfd+rpyTiQbqw6mr03ryVe1MWWaKhpnX0k+gndDAZc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=tuxon.dev; spf=pass smtp.mailfrom=tuxon.dev; dkim=pass (2048-bit key) header.d=tuxon.dev header.i=@tuxon.dev header.b=Io//6dw2; arc=none smtp.client-ip=209.85.128.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=tuxon.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=tuxon.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=tuxon.dev header.i=@tuxon.dev header.b="Io//6dw2" Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-483703e4b08so89748635e9.1 for ; Fri, 06 Mar 2026 04:41:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tuxon.dev; s=google; t=1772800906; x=1773405706; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=nsmFxT5OqHzHHM4Ncsz2v6IqxYstw+p2fVPLaiDV9OE=; b=Io//6dw2I+r1O+/GEIDV2ZWTSREF0mtXEaQOJsRkt5N58Usx0Fsnh/57GqlJVh9sjF gMLIGxE0K0vw8thDKc/5LkFAxANLuLgPIAC41edFP5bzg9aTpqNtjFPuB+znllG6ERUF lcA1mCfq9gKp3bYtEL1z40qr5FUNWxbyDyEhOlaLechH6GCyaJoVUoBqmLPOAMr4WH3W YnmlNE21kAwf7NXh/qyuhGDzoGthOvdrq7Prbj3ib/cR0uICJX5X2XK5by+TS5ZI3xFj dpBrPQrEoIwWgG4qQiNyHzfd+thMqyukjtWsJUNR0dsxJ3z4+B+LF40uH5HuNrsoUKqj k5+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772800906; x=1773405706; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=nsmFxT5OqHzHHM4Ncsz2v6IqxYstw+p2fVPLaiDV9OE=; b=FV1SGloHpelsyovxBzKXAIqrHdZLXcM/4AvcStzJRArpspfU8nO0RITK4ZtNWly/QZ DUpMwMVaDALtKO0Bfa+lh/INsl3Ciw5BPYlJBOS0iDi2xcxi9VWZxwPTXakq2NXiGl6u iSoUlKdqUmSYPksirkIQabo4e0Hw0G7lVWu6BF98a2pnTLGjUf1ZBn6hbb0NSSV6EkGt NcLcpTGIVTcXXRDPLR74gHI4/dTrzKb2laLJDgsC6mOlqy61zkC6bL4oSfXlPdOJD2y9 BSGAJVAuZ0iDTPeW/5R9vduVAI+iQoUExpxcZ+3lqnGiPe3hIG3RuN21DBZ2jmGggqiw U3pw== X-Forwarded-Encrypted: i=1; AJvYcCX5G42kVI8vqdwA6P7ylzqxD0pfip115f2MPam1yy16Debx+yixhsbN68QSSTShweO7YnJCxNeZnfM6UPQ=@vger.kernel.org X-Gm-Message-State: AOJu0Yymgs4eVbu0iggqtdVn2cgopB5rJH86PWNEsxRxQ9canA6Dg5rx p6V26GKQKtAiiXBFN697G0e4tIo7QCORjGGbladwGmf3jOZ7HdGGdgRC6AH1g7FULfY= X-Gm-Gg: ATEYQzx/0ozE5pOc/u6LdU5DT1lHIMF9CSuiWibmkCmuHy8Rm2LXkr2YGS1+Jg/Bwan GGed42sWLwIpYeIYzTCglmnnn0yyoQyRNIErBd04VonYEy5a3XS1TiKFd70u0ikTgkmoMFrIPTi PYNJz1Btb7Dvozd7mlrf1NK0ZVK8qMuyiDZLoC7MNqD74ux467mGngmtOlC/NT7mj57s9V0Cm7t 6cR8fkoq25WLXoxRgDK5QKsUao6B9xLba7OatvJoArMe7W4xEaeNBDSEwxVDvAY+dJEOjgA+hA5 oirQwcxXHcladuMd7bMVlBmqfgnrTjeHIH+Drbn9/X/FBha9CJvuxhBtD0CIEoXX3varwp98OxM 9fRwepnVIGzKN6T6FI51sFUn7R726cOCoju+1S1mdQ6znoXSNU0Xgq2eP2C/nDAqT4H/lLVamMP 86vGLrpEptznCWVg/HmggrIKprvTZhBzXV1XSYBhnfFFCOcIY+uvrF X-Received: by 2002:a05:600c:4e88:b0:483:7ea3:3de3 with SMTP id 5b1f17b1804b1-48526715266mr28560065e9.2.1772800905597; Fri, 06 Mar 2026 04:41:45 -0800 (PST) Received: from claudiu-X670E-Pro-RS.. ([82.78.167.134]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-485276b0c38sm38150505e9.9.2026.03.06.04.41.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Mar 2026 04:41:45 -0800 (PST) From: Claudiu X-Google-Original-From: Claudiu To: vkoul@kernel.org, Frank.Li@kernel.org, biju.das.jz@bp.renesas.com, geert+renesas@glider.be, fabrizio.castro.jz@renesas.com, prabhakar.mahadev-lad.rj@bp.renesas.com Cc: claudiu.beznea@tuxon.dev, dmaengine@vger.kernel.org, linux-kernel@vger.kernel.org, Long Luu , Claudiu Beznea Subject: [PATCH v9 7/8] dmaengine: sh: rz-dmac: Add device_tx_status() callback Date: Fri, 6 Mar 2026 14:41:32 +0200 Message-ID: <20260306124133.2304687-8-claudiu.beznea.uj@bp.renesas.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260306124133.2304687-1-claudiu.beznea.uj@bp.renesas.com> References: <20260306124133.2304687-1-claudiu.beznea.uj@bp.renesas.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Biju Das The RZ/G2L SCIFA driver uses dmaengine_prep_slave_sg() to enqueue DMA transfers and implements a timeout mechanism on RX to handle cases where a DMA transfer does not complete. The timeout is implemented using an hrtimer. In the hrtimer callback, dmaengine_tx_status() is called (along with dmaengine_pause()) to retrieve the transfer residue and handle incomplete DMA transfers. Add support for the device_tx_status() callback. Co-developed-by: Long Luu Signed-off-by: Long Luu Signed-off-by: Biju Das Co-developed-by: Claudiu Beznea Signed-off-by: Claudiu Beznea --- Changes in v9: - adjusted the patch description - dropped contribution list for Claudiu Beznea - used Co-developed-by + SoB tags and included Long Luu in the contribution list as well - dropped the read of CRLA in rz_dmac_calculate_residue_bytes_in_vd() and use the copy from the calling function (rz_dmac_chan_get_residue()) Changes in v8: - populated engine->residue_granularity Changes in v7: - none Changes in v6: - s/byte/bytes in comment from rz_dmac_chan_get_residue() Changes in v5: - post-increment lmdesc in rz_dmac_get_next_lmdesc() to allow the next pointer to advance - use 'lmdesc->nxla !=3D crla' comparison instead of '!(lmdesc->nxla =3D=3D crla)' in rz_dmac_calculate_residue_bytes_in_vd() - in rz_dmac_calculate_residue_bytes_in_vd() use '++i >=3D DMAC_NR_LMDESC' to verify if the full lmdesc list was checked - drop rz_dmac_calculate_total_bytes_in_vd() and use desc->len instead - re-arranged comments so they span fewer lines and are wrapped to ~80 characters - use u32 for the residue value and the functions returning it - use u32 for the variables storing register values - fixed typos drivers/dma/sh/rz-dmac.c | 144 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 143 insertions(+), 1 deletion(-) diff --git a/drivers/dma/sh/rz-dmac.c b/drivers/dma/sh/rz-dmac.c index 274c9cd40713..3b318fe06f28 100644 --- a/drivers/dma/sh/rz-dmac.c +++ b/drivers/dma/sh/rz-dmac.c @@ -118,10 +118,12 @@ struct rz_dmac { * Registers */ =20 +#define CRTB 0x0020 #define CHSTAT 0x0024 #define CHCTRL 0x0028 #define CHCFG 0x002c #define NXLA 0x0038 +#define CRLA 0x003c =20 #define DCTRL 0x0000 =20 @@ -684,6 +686,145 @@ static void rz_dmac_device_synchronize(struct dma_cha= n *chan) } } =20 +static struct rz_lmdesc * +rz_dmac_get_next_lmdesc(struct rz_lmdesc *base, struct rz_lmdesc *lmdesc) +{ + struct rz_lmdesc *next =3D ++lmdesc; + + if (next >=3D base + DMAC_NR_LMDESC) + next =3D base; + + return next; +} + +static u32 rz_dmac_calculate_residue_bytes_in_vd(struct rz_dmac_chan *chan= nel, u32 crla) +{ + struct rz_lmdesc *lmdesc =3D channel->lmdesc.head; + struct dma_chan *chan =3D &channel->vc.chan; + struct rz_dmac *dmac =3D to_rz_dmac(chan->device); + u32 residue =3D 0, i =3D 0; + + while (lmdesc->nxla !=3D crla) { + lmdesc =3D rz_dmac_get_next_lmdesc(channel->lmdesc.base, lmdesc); + if (++i >=3D DMAC_NR_LMDESC) + return 0; + } + + /* Calculate residue from next lmdesc to end of virtual desc */ + while (lmdesc->chcfg & CHCFG_DEM) { + residue +=3D lmdesc->tb; + lmdesc =3D rz_dmac_get_next_lmdesc(channel->lmdesc.base, lmdesc); + } + + dev_dbg(dmac->dev, "%s: VD residue is %u\n", __func__, residue); + + return residue; +} + +static u32 rz_dmac_chan_get_residue(struct rz_dmac_chan *channel, + dma_cookie_t cookie) +{ + struct rz_dmac_desc *current_desc, *desc; + enum dma_status status; + u32 crla, crtb, i; + + /* Get current processing virtual descriptor */ + current_desc =3D list_first_entry(&channel->ld_active, + struct rz_dmac_desc, node); + if (!current_desc) + return 0; + + /* + * If the cookie corresponds to a descriptor that has been completed + * there is no residue. The same check has already been performed by the + * caller but without holding the channel lock, so the descriptor could + * now be complete. + */ + status =3D dma_cookie_status(&channel->vc.chan, cookie, NULL); + if (status =3D=3D DMA_COMPLETE) + return 0; + + /* + * If the cookie doesn't correspond to the currently processing virtual + * descriptor then the descriptor hasn't been processed yet, and the + * residue is equal to the full descriptor size. Also, a client driver + * is possible to call this function before rz_dmac_irq_handler_thread() + * runs. In this case, the running descriptor will be the next + * descriptor, and will appear in the done list. So, if the argument + * cookie matches the done list's cookie, we can assume the residue is + * zero. + */ + if (cookie !=3D current_desc->vd.tx.cookie) { + list_for_each_entry(desc, &channel->ld_free, node) { + if (cookie =3D=3D desc->vd.tx.cookie) + return 0; + } + + list_for_each_entry(desc, &channel->ld_queue, node) { + if (cookie =3D=3D desc->vd.tx.cookie) + return desc->len; + } + + list_for_each_entry(desc, &channel->ld_active, node) { + if (cookie =3D=3D desc->vd.tx.cookie) + return desc->len; + } + + /* + * No descriptor found for the cookie, there's thus no residue. + * This shouldn't happen if the calling driver passes a correct + * cookie value. + */ + WARN(1, "No descriptor for cookie!"); + return 0; + } + + /* + * We need to read two registers. Make sure the hardware does not move + * to next lmdesc while reading the current lmdesc. Trying it 3 times + * should be enough: initial read, retry, retry for the paranoid. + */ + for (i =3D 0; i < 3; i++) { + crla =3D rz_dmac_ch_readl(channel, CRLA, 1); + crtb =3D rz_dmac_ch_readl(channel, CRTB, 1); + /* Still the same? */ + if (crla =3D=3D rz_dmac_ch_readl(channel, CRLA, 1)) + break; + } + + WARN_ONCE(i >=3D 3, "residue might not be continuous!"); + + /* + * Calculate number of bytes transferred in processing virtual descriptor. + * One virtual descriptor can have many lmdesc. + */ + return crtb + rz_dmac_calculate_residue_bytes_in_vd(channel, crla); +} + +static enum dma_status rz_dmac_tx_status(struct dma_chan *chan, + dma_cookie_t cookie, + struct dma_tx_state *txstate) +{ + struct rz_dmac_chan *channel =3D to_rz_dmac_chan(chan); + enum dma_status status; + u32 residue; + + status =3D dma_cookie_status(chan, cookie, txstate); + if (status =3D=3D DMA_COMPLETE || !txstate) + return status; + + scoped_guard(spinlock_irqsave, &channel->vc.lock) + residue =3D rz_dmac_chan_get_residue(channel, cookie); + + /* if there's no residue, the cookie is complete */ + if (!residue) + return DMA_COMPLETE; + + dma_set_residue(txstate, residue); + + return status; +} + /* * -----------------------------------------------------------------------= ------ * IRQ handling @@ -1006,6 +1147,7 @@ static int rz_dmac_probe(struct platform_device *pdev) engine =3D &dmac->engine; dma_cap_set(DMA_SLAVE, engine->cap_mask); dma_cap_set(DMA_MEMCPY, engine->cap_mask); + engine->residue_granularity =3D DMA_RESIDUE_GRANULARITY_BURST; rz_dmac_writel(dmac, DCTRL_DEFAULT, CHANNEL_0_7_COMMON_BASE + DCTRL); rz_dmac_writel(dmac, DCTRL_DEFAULT, CHANNEL_8_15_COMMON_BASE + DCTRL); =20 @@ -1013,7 +1155,7 @@ static int rz_dmac_probe(struct platform_device *pdev) =20 engine->device_alloc_chan_resources =3D rz_dmac_alloc_chan_resources; engine->device_free_chan_resources =3D rz_dmac_free_chan_resources; - engine->device_tx_status =3D dma_cookie_status; + engine->device_tx_status =3D rz_dmac_tx_status; engine->device_prep_slave_sg =3D rz_dmac_prep_slave_sg; engine->device_prep_dma_memcpy =3D rz_dmac_prep_dma_memcpy; engine->device_config =3D rz_dmac_config; --=20 2.43.0