Skip to content

Commit a1341ca

Browse files
Linus Walleijgregkh
authored andcommitted
mmc: sdhci: Implement an SDHCI-specific bounce buffer
commit bd9b902 upstream. The bounce buffer is gone from the MMC core, and now we found out that there are some (crippled) i.MX boards out there that have broken ADMA (cannot do scatter-gather), and also broken PIO so they must use SDMA. Closer examination shows a less significant slowdown also on SDMA-only capable Laptop hosts. SDMA sets down the number of segments to one, so that each segment gets turned into a singular request that ping-pongs to the block layer before the next request/segment is issued. Apparently it happens a lot that the block layer send requests that include a lot of physically discontiguous segments. My guess is that this phenomenon is coming from the file system. These devices that cannot handle scatterlists in hardware can see major benefits from a DMA-contiguous bounce buffer. This patch accumulates those fragmented scatterlists in a physically contiguous bounce buffer so that we can issue bigger DMA data chunks to/from the card. When tested with a PCI-integrated host (1217:8221) that only supports SDMA: 0b:00.0 SD Host controller: O2 Micro, Inc. OZ600FJ0/OZ900FJ0/OZ600FJS SD/MMC Card Reader Controller (rev 05) This patch gave ~1Mbyte/s improved throughput on large reads and writes when testing using iozone than without the patch. dmesg: sdhci-pci 0000:0b:00.0: SDHCI controller found [1217:8221] (rev 5) mmc0 bounce up to 128 segments into one, max segment size 65536 bytes mmc0: SDHCI controller on PCI [0000:0b:00.0] using DMA On the i.MX SDHCI controllers on the crippled i.MX 25 and i.MX 35 the patch restores the performance to what it was before we removed the bounce buffers. Cc: Pierre Ossman <pierre@ossman.eu> Cc: Benoît Thébaudeau <benoit@wsystem.com> Cc: Fabio Estevam <fabio.estevam@nxp.com> Cc: Benjamin Beckmeyer <beckmeyer.b@rittal.de> Cc: stable@vger.kernel.org # v4.14+ Fixes: de3ee99 ("mmc: Delete bounce buffer handling") Tested-by: Benjamin Beckmeyer <beckmeyer.b@rittal.de> Acked-by: Adrian Hunter <adrian.hunter@intel.com> Signed-off-by: Linus Walleij <linus.walleij@linaro.org> Signed-off-by: Ulf Hansson <ulf.hansson@linaro.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent e96a219 commit a1341ca

2 files changed

Lines changed: 159 additions & 8 deletions

File tree

‎drivers/mmc/host/sdhci.c‎

Lines changed: 156 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <linux/dma-mapping.h>
2222
#include <linux/slab.h>
2323
#include <linux/scatterlist.h>
24+
#include <linux/sizes.h>
2425
#include <linux/swiotlb.h>
2526
#include <linux/regulator/consumer.h>
2627
#include <linux/pm_runtime.h>
@@ -502,8 +503,35 @@ static int sdhci_pre_dma_transfer(struct sdhci_host *host,
502503
if (data->host_cookie == COOKIE_PRE_MAPPED)
503504
return data->sg_count;
504505

505-
sg_count = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
506-
mmc_get_dma_dir(data));
506+
/* Bounce write requests to the bounce buffer */
507+
if (host->bounce_buffer) {
508+
unsigned int length = data->blksz * data->blocks;
509+
510+
if (length > host->bounce_buffer_size) {
511+
pr_err("%s: asked for transfer of %u bytes exceeds bounce buffer %u bytes\n",
512+
mmc_hostname(host->mmc), length,
513+
host->bounce_buffer_size);
514+
return -EIO;
515+
}
516+
if (mmc_get_dma_dir(data) == DMA_TO_DEVICE) {
517+
/* Copy the data to the bounce buffer */
518+
sg_copy_to_buffer(data->sg, data->sg_len,
519+
host->bounce_buffer,
520+
length);
521+
}
522+
/* Switch ownership to the DMA */
523+
dma_sync_single_for_device(host->mmc->parent,
524+
host->bounce_addr,
525+
host->bounce_buffer_size,
526+
mmc_get_dma_dir(data));
527+
/* Just a dummy value */
528+
sg_count = 1;
529+
} else {
530+
/* Just access the data directly from memory */
531+
sg_count = dma_map_sg(mmc_dev(host->mmc),
532+
data->sg, data->sg_len,
533+
mmc_get_dma_dir(data));
534+
}
507535

508536
if (sg_count == 0)
509537
return -ENOSPC;
@@ -673,6 +701,14 @@ static void sdhci_adma_table_post(struct sdhci_host *host,
673701
}
674702
}
675703

704+
static u32 sdhci_sdma_address(struct sdhci_host *host)
705+
{
706+
if (host->bounce_buffer)
707+
return host->bounce_addr;
708+
else
709+
return sg_dma_address(host->data->sg);
710+
}
711+
676712
static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
677713
{
678714
u8 count;
@@ -858,8 +894,8 @@ static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
858894
SDHCI_ADMA_ADDRESS_HI);
859895
} else {
860896
WARN_ON(sg_cnt != 1);
861-
sdhci_writel(host, sg_dma_address(data->sg),
862-
SDHCI_DMA_ADDRESS);
897+
sdhci_writel(host, sdhci_sdma_address(host),
898+
SDHCI_DMA_ADDRESS);
863899
}
864900
}
865901

@@ -2248,7 +2284,12 @@ static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq)
22482284

22492285
mrq->data->host_cookie = COOKIE_UNMAPPED;
22502286

2251-
if (host->flags & SDHCI_REQ_USE_DMA)
2287+
/*
2288+
* No pre-mapping in the pre hook if we're using the bounce buffer,
2289+
* for that we would need two bounce buffers since one buffer is
2290+
* in flight when this is getting called.
2291+
*/
2292+
if (host->flags & SDHCI_REQ_USE_DMA && !host->bounce_buffer)
22522293
sdhci_pre_dma_transfer(host, mrq->data, COOKIE_PRE_MAPPED);
22532294
}
22542295

@@ -2352,8 +2393,45 @@ static bool sdhci_request_done(struct sdhci_host *host)
23522393
struct mmc_data *data = mrq->data;
23532394

23542395
if (data && data->host_cookie == COOKIE_MAPPED) {
2355-
dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2356-
mmc_get_dma_dir(data));
2396+
if (host->bounce_buffer) {
2397+
/*
2398+
* On reads, copy the bounced data into the
2399+
* sglist
2400+
*/
2401+
if (mmc_get_dma_dir(data) == DMA_FROM_DEVICE) {
2402+
unsigned int length = data->bytes_xfered;
2403+
2404+
if (length > host->bounce_buffer_size) {
2405+
pr_err("%s: bounce buffer is %u bytes but DMA claims to have transferred %u bytes\n",
2406+
mmc_hostname(host->mmc),
2407+
host->bounce_buffer_size,
2408+
data->bytes_xfered);
2409+
/* Cap it down and continue */
2410+
length = host->bounce_buffer_size;
2411+
}
2412+
dma_sync_single_for_cpu(
2413+
host->mmc->parent,
2414+
host->bounce_addr,
2415+
host->bounce_buffer_size,
2416+
DMA_FROM_DEVICE);
2417+
sg_copy_from_buffer(data->sg,
2418+
data->sg_len,
2419+
host->bounce_buffer,
2420+
length);
2421+
} else {
2422+
/* No copying, just switch ownership */
2423+
dma_sync_single_for_cpu(
2424+
host->mmc->parent,
2425+
host->bounce_addr,
2426+
host->bounce_buffer_size,
2427+
mmc_get_dma_dir(data));
2428+
}
2429+
} else {
2430+
/* Unmap the raw data */
2431+
dma_unmap_sg(mmc_dev(host->mmc), data->sg,
2432+
data->sg_len,
2433+
mmc_get_dma_dir(data));
2434+
}
23572435
data->host_cookie = COOKIE_UNMAPPED;
23582436
}
23592437
}
@@ -2636,7 +2714,8 @@ static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
26362714
*/
26372715
if (intmask & SDHCI_INT_DMA_END) {
26382716
u32 dmastart, dmanow;
2639-
dmastart = sg_dma_address(host->data->sg);
2717+
2718+
dmastart = sdhci_sdma_address(host);
26402719
dmanow = dmastart + host->data->bytes_xfered;
26412720
/*
26422721
* Force update to the next DMA block boundary.
@@ -3217,6 +3296,68 @@ void __sdhci_read_caps(struct sdhci_host *host, u16 *ver, u32 *caps, u32 *caps1)
32173296
}
32183297
EXPORT_SYMBOL_GPL(__sdhci_read_caps);
32193298

3299+
static int sdhci_allocate_bounce_buffer(struct sdhci_host *host)
3300+
{
3301+
struct mmc_host *mmc = host->mmc;
3302+
unsigned int max_blocks;
3303+
unsigned int bounce_size;
3304+
int ret;
3305+
3306+
/*
3307+
* Cap the bounce buffer at 64KB. Using a bigger bounce buffer
3308+
* has diminishing returns, this is probably because SD/MMC
3309+
* cards are usually optimized to handle this size of requests.
3310+
*/
3311+
bounce_size = SZ_64K;
3312+
/*
3313+
* Adjust downwards to maximum request size if this is less
3314+
* than our segment size, else hammer down the maximum
3315+
* request size to the maximum buffer size.
3316+
*/
3317+
if (mmc->max_req_size < bounce_size)
3318+
bounce_size = mmc->max_req_size;
3319+
max_blocks = bounce_size / 512;
3320+
3321+
/*
3322+
* When we just support one segment, we can get significant
3323+
* speedups by the help of a bounce buffer to group scattered
3324+
* reads/writes together.
3325+
*/
3326+
host->bounce_buffer = devm_kmalloc(mmc->parent,
3327+
bounce_size,
3328+
GFP_KERNEL);
3329+
if (!host->bounce_buffer) {
3330+
pr_err("%s: failed to allocate %u bytes for bounce buffer, falling back to single segments\n",
3331+
mmc_hostname(mmc),
3332+
bounce_size);
3333+
/*
3334+
* Exiting with zero here makes sure we proceed with
3335+
* mmc->max_segs == 1.
3336+
*/
3337+
return 0;
3338+
}
3339+
3340+
host->bounce_addr = dma_map_single(mmc->parent,
3341+
host->bounce_buffer,
3342+
bounce_size,
3343+
DMA_BIDIRECTIONAL);
3344+
ret = dma_mapping_error(mmc->parent, host->bounce_addr);
3345+
if (ret)
3346+
/* Again fall back to max_segs == 1 */
3347+
return 0;
3348+
host->bounce_buffer_size = bounce_size;
3349+
3350+
/* Lie about this since we're bouncing */
3351+
mmc->max_segs = max_blocks;
3352+
mmc->max_seg_size = bounce_size;
3353+
mmc->max_req_size = bounce_size;
3354+
3355+
pr_info("%s bounce up to %u segments into one, max segment size %u bytes\n",
3356+
mmc_hostname(mmc), max_blocks, bounce_size);
3357+
3358+
return 0;
3359+
}
3360+
32203361
int sdhci_setup_host(struct sdhci_host *host)
32213362
{
32223363
struct mmc_host *mmc;
@@ -3713,6 +3854,13 @@ int sdhci_setup_host(struct sdhci_host *host)
37133854
*/
37143855
mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
37153856

3857+
if (mmc->max_segs == 1) {
3858+
/* This may alter mmc->*_blk_* parameters */
3859+
ret = sdhci_allocate_bounce_buffer(host);
3860+
if (ret)
3861+
return ret;
3862+
}
3863+
37163864
return 0;
37173865

37183866
unreg:

‎drivers/mmc/host/sdhci.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,9 @@ struct sdhci_host {
440440

441441
int irq; /* Device IRQ */
442442
void __iomem *ioaddr; /* Mapped address */
443+
char *bounce_buffer; /* For packing SDMA reads/writes */
444+
dma_addr_t bounce_addr;
445+
unsigned int bounce_buffer_size;
443446

444447
const struct sdhci_ops *ops; /* Low level hw interface */
445448

0 commit comments

Comments
 (0)