[PATCH 6/9] mmc: sdhci: allow drivers to pre-allocate bounce buffer

Albert Yang posted 9 patches 1 week, 1 day ago
[PATCH 6/9] mmc: sdhci: allow drivers to pre-allocate bounce buffer
Posted by Albert Yang 1 week, 1 day ago
In sdhci_allocate_bounce_buffer(), add an early path that respects a
driver-provided pre-allocated bounce buffer (host->bounce_buffer).

If the buffer is already allocated by the driver (e.g. coherent/SRAM
buffer needed for platforms with 32-bit DMA constraints), just compute
max_blocks from host->bounce_buffer_size and jump to the common "out"
path to set mmc->max_*.

This enables platform drivers to allocate the bounce buffer before
sdhci_add_host(), avoiding starting the host without the buffer ready
and aligning with the guidance from review.

No functional change for drivers that do not pre-allocate the buffer.

drivers/mmc/host/sdhci.c (sdhci_allocate_bounce_buffer): Handle
pre-allocated bounce buffer and fall through to set mmc->max_*.

Suggested-by: Adrian Hunter <adrian.hunter@intel.com>
Link: https://lore.kernel.org/lkml/2b23bcb9-abc7-4667-b939-a19ecae935a2@intel.com/
Signed-off-by: Albert Yang <yangzh0906@thundersoft.com>

---
Changes for v4:
- Add new patch by Suggested-by: Adrian Hunter
---
 drivers/mmc/host/sdhci.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/drivers/mmc/host/sdhci.c b/drivers/mmc/host/sdhci.c
index 3a17821efa5ca92c6c29141d8fcb9ebf58355cc7..03fbe30cb205e16b924481caa44f0979d230f380 100644
--- a/drivers/mmc/host/sdhci.c
+++ b/drivers/mmc/host/sdhci.c
@@ -4193,6 +4193,12 @@ static void sdhci_allocate_bounce_buffer(struct sdhci_host *host)
 	unsigned int bounce_size;
 	int ret;
 
+	/* Drivers may have already allocated the buffer */
+	if (host->bounce_buffer) {
+		bounce_size = host->bounce_buffer_size;
+		max_blocks = bounce_size / 512;
+		goto out;
+	}
 	/*
 	 * Cap the bounce buffer at 64KB. Using a bigger bounce buffer
 	 * has diminishing returns, this is probably because SD/MMC
@@ -4241,6 +4247,7 @@ static void sdhci_allocate_bounce_buffer(struct sdhci_host *host)
 
 	host->bounce_buffer_size = bounce_size;
 
+out:
 	/* Lie about this since we're bouncing */
 	mmc->max_segs = max_blocks;
 	mmc->max_seg_size = bounce_size;

-- 
2.43.0
Re: [PATCH 6/9] mmc: sdhci: allow drivers to pre-allocate bounce buffer
Posted by Adrian Hunter 2 days, 14 hours ago
On 23/09/2025 09:10, Albert Yang wrote:
> In sdhci_allocate_bounce_buffer(), add an early path that respects a
> driver-provided pre-allocated bounce buffer (host->bounce_buffer).
> 
> If the buffer is already allocated by the driver (e.g. coherent/SRAM
> buffer needed for platforms with 32-bit DMA constraints), just compute
> max_blocks from host->bounce_buffer_size and jump to the common "out"
> path to set mmc->max_*.
> 
> This enables platform drivers to allocate the bounce buffer before
> sdhci_add_host(), avoiding starting the host without the buffer ready
> and aligning with the guidance from review.
> 
> No functional change for drivers that do not pre-allocate the buffer.
> 
> drivers/mmc/host/sdhci.c (sdhci_allocate_bounce_buffer): Handle
> pre-allocated bounce buffer and fall through to set mmc->max_*.
> 
> Suggested-by: Adrian Hunter <adrian.hunter@intel.com>
> Link: https://lore.kernel.org/lkml/2b23bcb9-abc7-4667-b939-a19ecae935a2@intel.com/
> Signed-off-by: Albert Yang <yangzh0906@thundersoft.com>

This patch needs to come before patch 5 because patch 5 depends on it.
Otherwise:

Acked-by: Adrian Hunter <adrian.hunter@intel.com>

> 
> ---
> Changes for v4:
> - Add new patch by Suggested-by: Adrian Hunter
> ---
>  drivers/mmc/host/sdhci.c | 7 +++++++
>  1 file changed, 7 insertions(+)
> 
> diff --git a/drivers/mmc/host/sdhci.c b/drivers/mmc/host/sdhci.c
> index 3a17821efa5ca92c6c29141d8fcb9ebf58355cc7..03fbe30cb205e16b924481caa44f0979d230f380 100644
> --- a/drivers/mmc/host/sdhci.c
> +++ b/drivers/mmc/host/sdhci.c
> @@ -4193,6 +4193,12 @@ static void sdhci_allocate_bounce_buffer(struct sdhci_host *host)
>  	unsigned int bounce_size;
>  	int ret;
>  
> +	/* Drivers may have already allocated the buffer */
> +	if (host->bounce_buffer) {
> +		bounce_size = host->bounce_buffer_size;
> +		max_blocks = bounce_size / 512;
> +		goto out;
> +	}
>  	/*
>  	 * Cap the bounce buffer at 64KB. Using a bigger bounce buffer
>  	 * has diminishing returns, this is probably because SD/MMC
> @@ -4241,6 +4247,7 @@ static void sdhci_allocate_bounce_buffer(struct sdhci_host *host)
>  
>  	host->bounce_buffer_size = bounce_size;
>  
> +out:
>  	/* Lie about this since we're bouncing */
>  	mmc->max_segs = max_blocks;
>  	mmc->max_seg_size = bounce_size;
>