[PATCH v2 2/2] mmc: meson-mx-sdhc: bounce multi-segment DMA requests

ZHENPENG YUAN yuanzhenpeng210 at gmail.com
Tue Dec 1 04:31:30 PST 2026


The Meson MX SDHC controller takes one DMA address per transfer. With
the default single-segment limit, a sequential read on a Xunlei OneCloud
(Meson8b) typically reaches the device as a series of 4 KiB requests.

Allow up to 64 scatterlist entries when a contiguous 128 KiB buffer is
available. Copy fragmented writes into that buffer before DMA and copy
fragmented reads out after a successful transfer. Keep direct DMA for
single entries, including when the optional allocation fails. Bound the
block count so block-mode SDIO requests also fit the transfer limit.

In the previously reported OneCloud comparison, 1 MiB sequential reads
averaged 13.11 MiB/s with the original driver, 13.05 MiB/s with DMA
merging enabled and 69.43 MiB/s with the 128 KiB bounce buffer. The
platform reports a DMA merge boundary of zero, so MMC_CAP2_MERGE_CAPABLE
alone leaves the queue limited to a single segment.

Link: https://lore.kernel.org/r/CAEDN0nBEK5PvkV-eC11XHYG04Occgs6o9_9K67OZn+xtkh68yQ@mail.gmail.com
Signed-off-by: ZHENPENG YUAN <yuanzhenpeng210 at gmail.com>
---

Notes (meson-v2):
    Changes since the RFC:
    - Use the scatterlist entry count to select the DMA path.
    - Use a managed page allocation for the buffer and keep the existing
      single-segment limit if allocation fails.
    - Keep the buffer at the tested 128 KiB request size. The merge-only
      comparison is linked above.
    
    RFC: https://lore.kernel.org/r/20261031200925.3364-1-yuanzhenpeng210@gmail.com
    Codex assisted with the implementation and changelog for this revision.

 drivers/mmc/host/meson-mx-sdhc-mmc.c | 86 +++++++++++++++++++++-------
 1 file changed, 65 insertions(+), 21 deletions(-)

diff --git a/drivers/mmc/host/meson-mx-sdhc-mmc.c b/drivers/mmc/host/meson-mx-sdhc-mmc.c
index 1f8f8f0fbdb6..c32564d20659 100644
--- a/drivers/mmc/host/meson-mx-sdhc-mmc.c
+++ b/drivers/mmc/host/meson-mx-sdhc-mmc.c
@@ -8,6 +8,7 @@
 #include <linux/clk.h>
 #include <linux/device.h>
 #include <linux/dma-mapping.h>
+#include <linux/gfp.h>
 #include <linux/interrupt.h>
 #include <linux/iopoll.h>
 #include <linux/module.h>
@@ -16,6 +17,7 @@
 #include <linux/property.h>
 #include <linux/regmap.h>
 #include <linux/regulator/consumer.h>
+#include <linux/scatterlist.h>
 #include <linux/types.h>
 
 #include <linux/mmc/host.h>
@@ -27,6 +29,7 @@
 
 #define MESON_SDHC_NUM_BULK_CLKS				4
 #define MESON_SDHC_MAX_BLK_SIZE					512
+#define MESON_SDHC_MAX_SEGS					64
 #define MESON_SDHC_NUM_TUNING_TRIES				10
 
 #define MESON_SDHC_WAIT_CMD_READY_SLEEP_US			1
@@ -48,6 +51,9 @@ struct meson_mx_sdhc_host {
 	struct mmc_command		*cmd;
 	int				error;
 
+	void				*bounce_buf;
+	dma_addr_t			dma_addr;
+
 	struct regmap			*regmap;
 
 	struct clk			*pclk;
@@ -223,8 +229,7 @@ static void meson_mx_sdhc_start_cmd(struct mmc_host *mmc,
 			   FIELD_PREP(MESON_SDHC_CTRL_PACK_LEN, pack_len));
 
 	if (cmd->data)
-		regmap_write(host->regmap, MESON_SDHC_ADDR,
-			     sg_dma_address(cmd->data->sg));
+		regmap_write(host->regmap, MESON_SDHC_ADDR, host->dma_addr);
 
 	if (cmd->data)
 		host->platform->set_pdma(mmc);
@@ -361,24 +366,59 @@ static void meson_mx_sdhc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
 	}
 }
 
-static int meson_mx_sdhc_map_dma(struct mmc_host *mmc, struct mmc_request *mrq)
+static int meson_mx_sdhc_map_dma(struct meson_mx_sdhc_host *host,
+				 struct mmc_data *data)
 {
-	struct mmc_data *data = mrq->data;
-	unsigned int dma_len;
-
-	if (!data)
+	enum dma_data_direction dir = mmc_get_dma_dir(data);
+	size_t len = data->blksz * data->blocks;
+	struct device *dev = mmc_dev(host->mmc);
+
+	if (data->sg_len == 1) {
+		if (!dma_map_sg(dev, data->sg, 1, dir))
+			return -ENOMEM;
+		host->dma_addr = sg_dma_address(data->sg);
 		return 0;
+	}
+
+	if (!host->bounce_buf || len > host->mmc->max_req_size)
+		return -EINVAL;
+
+	if (dir == DMA_TO_DEVICE &&
+	    sg_copy_to_buffer(data->sg, data->sg_len, host->bounce_buf, len) != len)
+		return -EIO;
 
-	dma_len = dma_map_sg(mmc_dev(mmc), data->sg, data->sg_len,
-			     mmc_get_dma_dir(data));
-	if (!dma_len) {
-		dev_err(mmc_dev(mmc), "dma_map_sg failed\n");
+	host->dma_addr = dma_map_single(dev, host->bounce_buf, len, dir);
+	if (dma_mapping_error(dev, host->dma_addr))
 		return -ENOMEM;
-	}
 
 	return 0;
 }
 
+static void meson_mx_sdhc_finish_dma(struct meson_mx_sdhc_host *host,
+				     struct mmc_command *cmd)
+{
+	struct mmc_data *data = cmd->data;
+	enum dma_data_direction dir = mmc_get_dma_dir(data);
+	struct device *dev = mmc_dev(host->mmc);
+	size_t len = data->blksz * data->blocks;
+
+	if (data->sg_len == 1)
+		dma_unmap_sg(dev, data->sg, 1, dir);
+	else
+		dma_unmap_single(dev, host->dma_addr, len, dir);
+
+	if (cmd->error || data->error)
+		return;
+
+	if (data->sg_len > 1 && dir == DMA_FROM_DEVICE &&
+	    sg_copy_from_buffer(data->sg, data->sg_len, host->bounce_buf, len) != len) {
+		data->error = -EIO;
+		return;
+	}
+
+	data->bytes_xfered = len;
+}
+
 static void meson_mx_sdhc_request(struct mmc_host *mmc, struct mmc_request *mrq)
 {
 	struct meson_mx_sdhc_host *host = mmc_priv(mmc);
@@ -392,8 +432,8 @@ static void meson_mx_sdhc_request(struct mmc_host *mmc, struct mmc_request *mrq)
 
 	if (!ret)
 		ret = meson_mx_sdhc_wait_cmd_ready(host, cmd);
-	if (!ret)
-		ret = meson_mx_sdhc_map_dma(mmc, mrq);
+	if (!ret && mrq->data)
+		ret = meson_mx_sdhc_map_dma(host, mrq->data);
 	if (ret) {
 		cmd->error = ret;
 		mmc_request_done(mmc, mrq);
@@ -633,12 +673,8 @@ static irqreturn_t meson_mx_sdhc_irq_thread(int irq, void *irq_data)
 done:
 	if (ret && !cmd->error)
 		cmd->error = ret;
-	if (data) {
-		dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
-			     mmc_get_dma_dir(data));
-		if (!cmd->error && !data->error)
-			data->bytes_xfered = data->blksz * data->blocks;
-	}
+	if (data)
+		meson_mx_sdhc_finish_dma(host, cmd);
 
 	meson_mx_sdhc_request_done(host);
 
@@ -828,8 +864,9 @@ static int meson_mx_sdhc_probe(struct platform_device *pdev)
 
 	mmc->max_req_size = SZ_128K;
 	mmc->max_seg_size = mmc->max_req_size;
-	mmc->max_blk_count = FIELD_GET(MESON_SDHC_SEND_TOTAL_PACK, ~0);
 	mmc->max_blk_size = MESON_SDHC_MAX_BLK_SIZE;
+	/* SDIO uses max_blk_count to split block-mode CMD53 transfers. */
+	mmc->max_blk_count = mmc->max_req_size / mmc->max_blk_size;
 	mmc->max_busy_timeout = 30 * MSEC_PER_SEC;
 	mmc->f_min = clk_round_rate(host->sd_clk, 1);
 	mmc->f_max = clk_round_rate(host->sd_clk, ULONG_MAX);
@@ -838,6 +875,13 @@ static int meson_mx_sdhc_probe(struct platform_device *pdev)
 	mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_HW_RESET;
 	mmc->ops = &meson_mx_sdhc_ops;
 
+	host->bounce_buf = (void *)devm_get_free_pages(dev, GFP_KERNEL | __GFP_NOWARN,
+						      get_order(mmc->max_req_size));
+	if (host->bounce_buf)
+		mmc->max_segs = MESON_SDHC_MAX_SEGS;
+	else
+		dev_warn(dev, "no DMA bounce buffer, using single-segment requests\n");
+
 	ret = mmc_of_parse(mmc);
 	if (ret)
 		goto err_disable_pclk;
-- 
2.54.0.windows.1




More information about the linux-arm-kernel mailing list