stable.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sasha Levin <sashal@kernel.org>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Cc: Yann Gautier <yann.gautier@foss.st.com>,
	Ulf Hansson <ulf.hansson@linaro.org>,
	Sasha Levin <sashal@kernel.org>
Subject: [PATCH 5.15 01/76] mmc: mmci: stm32: use a buffer for unaligned DMA requests
Date: Wed, 13 Mar 2024 12:41:08 -0400	[thread overview]
Message-ID: <20240313164223.615640-2-sashal@kernel.org> (raw)
In-Reply-To: <20240313164223.615640-1-sashal@kernel.org>

From: Yann Gautier <yann.gautier@foss.st.com>

[ Upstream commit 970dc9c11a17994ab878016b536612ab00d1441d ]

In SDIO mode, the sg list for requests can be unaligned with what the
STM32 SDMMC internal DMA can support. In that case, instead of failing,
use a temporary bounce buffer to copy from/to the sg list.
This buffer is limited to 1MB. But for that we need to also limit
max_req_size to 1MB. It has not shown any throughput penalties for
SD-cards or eMMC.

Signed-off-by: Yann Gautier <yann.gautier@foss.st.com>
Link: https://lore.kernel.org/r/20220328145114.334577-1-yann.gautier@foss.st.com
Signed-off-by: Ulf Hansson <ulf.hansson@linaro.org>
Stable-dep-of: 6b1ba3f9040b ("mmc: mmci: stm32: fix DMA API overlapping mappings warning")
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 drivers/mmc/host/mmci_stm32_sdmmc.c | 88 +++++++++++++++++++++++------
 1 file changed, 71 insertions(+), 17 deletions(-)

diff --git a/drivers/mmc/host/mmci_stm32_sdmmc.c b/drivers/mmc/host/mmci_stm32_sdmmc.c
index 4cceb9bab0361..11ae0cb479239 100644
--- a/drivers/mmc/host/mmci_stm32_sdmmc.c
+++ b/drivers/mmc/host/mmci_stm32_sdmmc.c
@@ -43,6 +43,9 @@ struct sdmmc_lli_desc {
 struct sdmmc_idma {
 	dma_addr_t sg_dma;
 	void *sg_cpu;
+	dma_addr_t bounce_dma_addr;
+	void *bounce_buf;
+	bool use_bounce_buffer;
 };
 
 struct sdmmc_dlyb {
@@ -54,6 +57,8 @@ struct sdmmc_dlyb {
 static int sdmmc_idma_validate_data(struct mmci_host *host,
 				    struct mmc_data *data)
 {
+	struct sdmmc_idma *idma = host->dma_priv;
+	struct device *dev = mmc_dev(host->mmc);
 	struct scatterlist *sg;
 	int i;
 
@@ -61,41 +66,69 @@ static int sdmmc_idma_validate_data(struct mmci_host *host,
 	 * idma has constraints on idmabase & idmasize for each element
 	 * excepted the last element which has no constraint on idmasize
 	 */
+	idma->use_bounce_buffer = false;
 	for_each_sg(data->sg, sg, data->sg_len - 1, i) {
 		if (!IS_ALIGNED(sg->offset, sizeof(u32)) ||
 		    !IS_ALIGNED(sg->length, SDMMC_IDMA_BURST)) {
-			dev_err(mmc_dev(host->mmc),
+			dev_dbg(mmc_dev(host->mmc),
 				"unaligned scatterlist: ofst:%x length:%d\n",
 				data->sg->offset, data->sg->length);
-			return -EINVAL;
+			goto use_bounce_buffer;
 		}
 	}
 
 	if (!IS_ALIGNED(sg->offset, sizeof(u32))) {
-		dev_err(mmc_dev(host->mmc),
+		dev_dbg(mmc_dev(host->mmc),
 			"unaligned last scatterlist: ofst:%x length:%d\n",
 			data->sg->offset, data->sg->length);
-		return -EINVAL;
+		goto use_bounce_buffer;
 	}
 
+	return 0;
+
+use_bounce_buffer:
+	if (!idma->bounce_buf) {
+		idma->bounce_buf = dmam_alloc_coherent(dev,
+						       host->mmc->max_req_size,
+						       &idma->bounce_dma_addr,
+						       GFP_KERNEL);
+		if (!idma->bounce_buf) {
+			dev_err(dev, "Unable to map allocate DMA bounce buffer.\n");
+			return -ENOMEM;
+		}
+	}
+
+	idma->use_bounce_buffer = true;
+
 	return 0;
 }
 
 static int _sdmmc_idma_prep_data(struct mmci_host *host,
 				 struct mmc_data *data)
 {
-	int n_elem;
+	struct sdmmc_idma *idma = host->dma_priv;
 
-	n_elem = dma_map_sg(mmc_dev(host->mmc),
-			    data->sg,
-			    data->sg_len,
-			    mmc_get_dma_dir(data));
+	if (idma->use_bounce_buffer) {
+		if (data->flags & MMC_DATA_WRITE) {
+			unsigned int xfer_bytes = data->blksz * data->blocks;
 
-	if (!n_elem) {
-		dev_err(mmc_dev(host->mmc), "dma_map_sg failed\n");
-		return -EINVAL;
-	}
+			sg_copy_to_buffer(data->sg, data->sg_len,
+					  idma->bounce_buf, xfer_bytes);
+			dma_wmb();
+		}
+	} else {
+		int n_elem;
+
+		n_elem = dma_map_sg(mmc_dev(host->mmc),
+				    data->sg,
+				    data->sg_len,
+				    mmc_get_dma_dir(data));
 
+		if (!n_elem) {
+			dev_err(mmc_dev(host->mmc), "dma_map_sg failed\n");
+			return -EINVAL;
+		}
+	}
 	return 0;
 }
 
@@ -112,8 +145,19 @@ static int sdmmc_idma_prep_data(struct mmci_host *host,
 static void sdmmc_idma_unprep_data(struct mmci_host *host,
 				   struct mmc_data *data, int err)
 {
-	dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
-		     mmc_get_dma_dir(data));
+	struct sdmmc_idma *idma = host->dma_priv;
+
+	if (idma->use_bounce_buffer) {
+		if (data->flags & MMC_DATA_READ) {
+			unsigned int xfer_bytes = data->blksz * data->blocks;
+
+			sg_copy_from_buffer(data->sg, data->sg_len,
+					    idma->bounce_buf, xfer_bytes);
+		}
+	} else {
+		dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
+			     mmc_get_dma_dir(data));
+	}
 }
 
 static int sdmmc_idma_setup(struct mmci_host *host)
@@ -137,6 +181,8 @@ static int sdmmc_idma_setup(struct mmci_host *host)
 		host->mmc->max_segs = SDMMC_LLI_BUF_LEN /
 			sizeof(struct sdmmc_lli_desc);
 		host->mmc->max_seg_size = host->variant->stm32_idmabsize_mask;
+
+		host->mmc->max_req_size = SZ_1M;
 	} else {
 		host->mmc->max_segs = 1;
 		host->mmc->max_seg_size = host->mmc->max_req_size;
@@ -154,8 +200,16 @@ static int sdmmc_idma_start(struct mmci_host *host, unsigned int *datactrl)
 	struct scatterlist *sg;
 	int i;
 
-	if (!host->variant->dma_lli || data->sg_len == 1) {
-		writel_relaxed(sg_dma_address(data->sg),
+	if (!host->variant->dma_lli || data->sg_len == 1 ||
+	    idma->use_bounce_buffer) {
+		u32 dma_addr;
+
+		if (idma->use_bounce_buffer)
+			dma_addr = idma->bounce_dma_addr;
+		else
+			dma_addr = sg_dma_address(data->sg);
+
+		writel_relaxed(dma_addr,
 			       host->base + MMCI_STM32_IDMABASE0R);
 		writel_relaxed(MMCI_STM32_IDMAEN,
 			       host->base + MMCI_STM32_IDMACTRLR);
-- 
2.43.0


  reply	other threads:[~2024-03-13 16:42 UTC|newest]

Thread overview: 91+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-13 16:41 [PATCH 5.15 00/76] 5.15.152-rc1 review Sasha Levin
2024-03-13 16:41 ` Sasha Levin [this message]
2024-03-13 16:41 ` [PATCH 5.15 02/76] mmc: mmci: stm32: fix DMA API overlapping mappings warning Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 03/76] net: lan78xx: fix runtime PM count underflow on link stop Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 04/76] ixgbe: {dis, en}able irqs in ixgbe_txrx_ring_{dis, en}able Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 05/76] i40e: disable NAPI right after disabling irqs when handling xsk_pool Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 06/76] tracing/net_sched: Fix tracepoints that save qdisc_dev() as a string Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 07/76] geneve: make sure to pull inner header in geneve_rx() Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 08/76] net: sparx5: Fix use after free inside sparx5_del_mact_entry Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 09/76] net: ice: Fix potential NULL pointer dereference in ice_bridge_setlink() Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 10/76] net/ipv6: avoid possible UAF in ip6_route_mpath_notify() Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 11/76] cpumap: Zero-initialise xdp_rxq_info struct before running XDP program Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 12/76] net/rds: fix WARNING in rds_conn_connect_if_down Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 13/76] netfilter: nft_ct: fix l3num expectations with inet pseudo family Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 14/76] netfilter: nf_conntrack_h323: Add protection for bmp length out of range Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 15/76] erofs: apply proper VMA alignment for memory mapped files on THP Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 16/76] netrom: Fix a data-race around sysctl_netrom_default_path_quality Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 17/76] netrom: Fix a data-race around sysctl_netrom_obsolescence_count_initialiser Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 18/76] netrom: Fix data-races around sysctl_netrom_network_ttl_initialiser Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 19/76] netrom: Fix a data-race around sysctl_netrom_transport_timeout Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 20/76] netrom: Fix a data-race around sysctl_netrom_transport_maximum_tries Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 21/76] netrom: Fix a data-race around sysctl_netrom_transport_acknowledge_delay Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 22/76] netrom: Fix a data-race around sysctl_netrom_transport_busy_delay Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 23/76] netrom: Fix a data-race around sysctl_netrom_transport_requested_window_size Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 24/76] netrom: Fix a data-race around sysctl_netrom_transport_no_activity_timeout Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 25/76] netrom: Fix a data-race around sysctl_netrom_routing_control Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 26/76] netrom: Fix a data-race around sysctl_netrom_link_fails_count Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 27/76] netrom: Fix data-races around sysctl_net_busy_read Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 28/76] ALSA: usb-audio: Refcount multiple accesses on the single clock Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 29/76] ALSA: usb-audio: Clear fixed clock rate at closing EP Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 30/76] ALSA: usb-audio: Split endpoint setups for hw_params and prepare (take#2) Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 31/76] ALSA: usb-audio: Properly refcounting clock rate Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 32/76] ALSA: usb-audio: Apply mutex around snd_usb_endpoint_set_params() Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 33/76] ALSA: usb-audio: Correct the return code from snd_usb_endpoint_set_params() Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 34/76] ALSA: usb-audio: Avoid superfluous endpoint setup Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 35/76] ALSA: usb-audio: Add quirk for Tascam Model 12 Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 36/76] ALSA: usb-audio: Add new quirk FIXED_RATE for JBL Quantum810 Wireless Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 37/76] ALSA: usb-audio: Fix microphone sound on Nexigo webcam Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 38/76] ALSA: usb-audio: add quirk for RODE NT-USB+ Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 39/76] drm/amd/display: Fix uninitialized variable usage in core_link_ 'read_dpcd() & write_dpcd()' functions Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 40/76] nfp: flower: add goto_chain_index for ct entry Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 41/76] nfp: flower: add hardware offload check for post " Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 42/76] selftests/mm: switch to bash from sh Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 43/76] selftests: mm: fix map_hugetlb failure on 64K page size systems Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 44/76] modpost: Include '.text.*' in TEXT_SECTIONS Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 45/76] modpost: Add '.ltext' and '.ltext.*' to TEXT_SECTIONS Sasha Levin
2024-03-13 17:12   ` Nathan Chancellor
2024-03-13 20:13     ` Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 46/76] xhci: process isoc TD properly when there was a transaction error mid TD Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 47/76] xhci: handle isoc Babble and Buffer Overrun events properly Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 48/76] serial: max310x: use regmap methods for SPI batch operations Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 49/76] serial: max310x: use a separate regmap for each port Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 50/76] serial: max310x: prevent infinite while() loop in port startup Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 51/76] drm/amd/pm: do not expose the API used internally only in kv_dpm.c Sasha Levin
2024-03-13 16:41 ` [PATCH 5.15 52/76] drm/amdgpu: Reset IH OVERFLOW_CLEAR bit Sasha Levin
2024-03-13 16:42 ` [PATCH 5.15 53/76] selftests: mptcp: decrease BW in simult flows Sasha Levin
2024-03-13 16:42 ` [PATCH 5.15 54/76] hv_netvsc: use netif_is_bond_master() instead of open code Sasha Levin
2024-03-13 16:42 ` [PATCH 5.15 55/76] hv_netvsc: Register VF in netvsc_probe if NET_DEVICE_REGISTER missed Sasha Levin
2024-03-13 16:42 ` [PATCH 5.15 56/76] drm/amd/display: Re-arrange FPU code structure for dcn2x Sasha Levin
2024-03-13 16:42 ` [PATCH 5.15 57/76] drm/amd/display: move calcs folder into DML Sasha Levin
2024-03-13 16:42 ` [PATCH 5.15 58/76] drm/amd/display: remove DML Makefile duplicate lines Sasha Levin
2024-03-13 16:42 ` [PATCH 5.15 59/76] drm/amd/display: Increase frame-larger-than for all display_mode_vba files Sasha Levin
2024-03-13 16:42 ` [PATCH 5.15 60/76] getrusage: add the "signal_struct *sig" local variable Sasha Levin
2024-03-13 16:42 ` [PATCH 5.15 61/76] getrusage: move thread_group_cputime_adjusted() outside of lock_task_sighand() Sasha Levin
2024-03-13 16:42 ` [PATCH 5.15 62/76] getrusage: use __for_each_thread() Sasha Levin
2024-03-13 16:42 ` [PATCH 5.15 63/76] getrusage: use sig->stats_lock rather than lock_task_sighand() Sasha Levin
2024-03-13 16:42 ` [PATCH 5.15 64/76] proc: Use task_is_running() for wchan in /proc/$pid/stat Sasha Levin
2024-03-14  3:04   ` Kees Cook
2024-03-15 18:31     ` Sasha Levin
2024-03-13 16:42 ` [PATCH 5.15 65/76] fs/proc: do_task_stat: move thread_group_cputime_adjusted() outside of lock_task_sighand() Sasha Levin
2024-03-13 16:42 ` [PATCH 5.15 66/76] exit: Fix typo in comment: s/sub-theads/sub-threads Sasha Levin
2024-03-13 16:42 ` [PATCH 5.15 67/76] exit: wait_task_zombie: kill the no longer necessary spin_lock_irq(siglock) Sasha Levin
2024-03-13 16:42 ` [PATCH 5.15 68/76] ALSA: usb-audio: Fix wrong kfree issue in snd_usb_endpoint_free_all Sasha Levin
2024-03-13 16:42 ` [PATCH 5.15 69/76] ALSA: usb-audio: Always initialize fixed_rate in snd_usb_find_implicit_fb_sync_format() Sasha Levin
2024-03-13 16:42 ` [PATCH 5.15 70/76] ALSA: usb-audio: Add FIXED_RATE quirk for JBL Quantum610 Wireless Sasha Levin
2024-03-13 16:42 ` [PATCH 5.15 71/76] ALSA: usb-audio: Sort quirk table entries Sasha Levin
2024-03-13 16:42 ` [PATCH 5.15 72/76] regmap: allow to define reg_update_bits for no bus configuration Sasha Levin
2024-03-13 16:46   ` Mark Brown
2024-03-13 17:16     ` Sasha Levin
2024-03-13 16:42 ` [PATCH 5.15 73/76] regmap: Add bulk read/write callbacks into regmap_config Sasha Levin
2024-03-13 16:46   ` Mark Brown
2024-03-13 17:14     ` Sasha Levin
2024-03-13 16:42 ` [PATCH 5.15 74/76] serial: max310x: make accessing revision id interface-agnostic Sasha Levin
2024-03-13 16:42 ` [PATCH 5.15 75/76] serial: max310x: fix IO data corruption in batched operations Sasha Levin
2024-03-13 16:42 ` [PATCH 5.15 76/76] Linux 5.15.152-rc1 Sasha Levin
2024-03-13 19:21 ` [PATCH 5.15 00/76] 5.15.152-rc1 review Daniel Díaz
2024-03-13 20:21   ` Sasha Levin
2024-03-14 15:02 ` Harshit Mogalapalli
2024-03-14 20:14 ` Florian Fainelli
2024-03-14 23:08 ` SeongJae Park
2024-03-15 10:43 ` Shreeya Patel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240313164223.615640-2-sashal@kernel.org \
    --to=sashal@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=ulf.hansson@linaro.org \
    --cc=yann.gautier@foss.st.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).