Skip to content

Commit 8a38a4d

Browse files
superna9999storulf
authored andcommitted
mmc: meson-gx: do not use memcpy_to/fromio for dram-access-quirk
The memory at the end of the controller only accepts 32bit read/write accesses, but the arm64 memcpy_to/fromio implementation only uses 64bit (which will be split into two 32bit access) and 8bit leading to incomplete copies to/from this memory when the buffer is not multiple of 8bytes. Add a local copy using writel/readl accesses to make sure we use the right memory access width. The switch to memcpy_to/fromio was done because of 2851330 ("arm64: Import latest memcpy()/memmove() implementation"), but using memcpy worked before since it mainly used 32bit memory acceses. Fixes: 103a534 ("mmc: meson-gx: use memcpy_to/fromio for dram-access-quirk") Reported-by: Christian Hewitt <[email protected]> Suggested-by: Martin Blumenstingl <[email protected]> Signed-off-by: Neil Armstrong <[email protected]> Tested-by: Martin Blumenstingl <[email protected]> Cc: [email protected] Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Ulf Hansson <[email protected]>
1 parent 30d4b99 commit 8a38a4d

File tree

1 file changed

+59
-14
lines changed

1 file changed

+59
-14
lines changed

drivers/mmc/host/meson-gx-mmc.c

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ static void meson_mmc_desc_chain_transfer(struct mmc_host *mmc, u32 cmd_cfg)
746746
writel(start, host->regs + SD_EMMC_START);
747747
}
748748

749-
/* local sg copy to buffer version with _to/fromio usage for dram_access_quirk */
749+
/* local sg copy for dram_access_quirk */
750750
static void meson_mmc_copy_buffer(struct meson_host *host, struct mmc_data *data,
751751
size_t buflen, bool to_buffer)
752752
{
@@ -764,21 +764,27 @@ static void meson_mmc_copy_buffer(struct meson_host *host, struct mmc_data *data
764764
sg_miter_start(&miter, sgl, nents, sg_flags);
765765

766766
while ((offset < buflen) && sg_miter_next(&miter)) {
767-
unsigned int len;
767+
unsigned int buf_offset = 0;
768+
unsigned int len, left;
769+
u32 *buf = miter.addr;
768770

769771
len = min(miter.length, buflen - offset);
772+
left = len;
770773

771-
/* When dram_access_quirk, the bounce buffer is a iomem mapping */
772-
if (host->dram_access_quirk) {
773-
if (to_buffer)
774-
memcpy_toio(host->bounce_iomem_buf + offset, miter.addr, len);
775-
else
776-
memcpy_fromio(miter.addr, host->bounce_iomem_buf + offset, len);
774+
if (to_buffer) {
775+
do {
776+
writel(*buf++, host->bounce_iomem_buf + offset + buf_offset);
777+
778+
buf_offset += 4;
779+
left -= 4;
780+
} while (left);
777781
} else {
778-
if (to_buffer)
779-
memcpy(host->bounce_buf + offset, miter.addr, len);
780-
else
781-
memcpy(miter.addr, host->bounce_buf + offset, len);
782+
do {
783+
*buf++ = readl(host->bounce_iomem_buf + offset + buf_offset);
784+
785+
buf_offset += 4;
786+
left -= 4;
787+
} while (left);
782788
}
783789

784790
offset += len;
@@ -830,7 +836,11 @@ static void meson_mmc_start_cmd(struct mmc_host *mmc, struct mmc_command *cmd)
830836
if (data->flags & MMC_DATA_WRITE) {
831837
cmd_cfg |= CMD_CFG_DATA_WR;
832838
WARN_ON(xfer_bytes > host->bounce_buf_size);
833-
meson_mmc_copy_buffer(host, data, xfer_bytes, true);
839+
if (host->dram_access_quirk)
840+
meson_mmc_copy_buffer(host, data, xfer_bytes, true);
841+
else
842+
sg_copy_to_buffer(data->sg, data->sg_len,
843+
host->bounce_buf, xfer_bytes);
834844
dma_wmb();
835845
}
836846

@@ -849,12 +859,43 @@ static void meson_mmc_start_cmd(struct mmc_host *mmc, struct mmc_command *cmd)
849859
writel(cmd->arg, host->regs + SD_EMMC_CMD_ARG);
850860
}
851861

862+
static int meson_mmc_validate_dram_access(struct mmc_host *mmc, struct mmc_data *data)
863+
{
864+
struct scatterlist *sg;
865+
int i;
866+
867+
/* Reject request if any element offset or size is not 32bit aligned */
868+
for_each_sg(data->sg, sg, data->sg_len, i) {
869+
if (!IS_ALIGNED(sg->offset, sizeof(u32)) ||
870+
!IS_ALIGNED(sg->length, sizeof(u32))) {
871+
dev_err(mmc_dev(mmc), "unaligned sg offset %u len %u\n",
872+
data->sg->offset, data->sg->length);
873+
return -EINVAL;
874+
}
875+
}
876+
877+
return 0;
878+
}
879+
852880
static void meson_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
853881
{
854882
struct meson_host *host = mmc_priv(mmc);
855883
bool needs_pre_post_req = mrq->data &&
856884
!(mrq->data->host_cookie & SD_EMMC_PRE_REQ_DONE);
857885

886+
/*
887+
* The memory at the end of the controller used as bounce buffer for
888+
* the dram_access_quirk only accepts 32bit read/write access,
889+
* check the aligment and length of the data before starting the request.
890+
*/
891+
if (host->dram_access_quirk && mrq->data) {
892+
mrq->cmd->error = meson_mmc_validate_dram_access(mmc, mrq->data);
893+
if (mrq->cmd->error) {
894+
mmc_request_done(mmc, mrq);
895+
return;
896+
}
897+
}
898+
858899
if (needs_pre_post_req) {
859900
meson_mmc_get_transfer_mode(mmc, mrq);
860901
if (!meson_mmc_desc_chain_mode(mrq->data))
@@ -999,7 +1040,11 @@ static irqreturn_t meson_mmc_irq_thread(int irq, void *dev_id)
9991040
if (meson_mmc_bounce_buf_read(data)) {
10001041
xfer_bytes = data->blksz * data->blocks;
10011042
WARN_ON(xfer_bytes > host->bounce_buf_size);
1002-
meson_mmc_copy_buffer(host, data, xfer_bytes, false);
1043+
if (host->dram_access_quirk)
1044+
meson_mmc_copy_buffer(host, data, xfer_bytes, false);
1045+
else
1046+
sg_copy_from_buffer(data->sg, data->sg_len,
1047+
host->bounce_buf, xfer_bytes);
10031048
}
10041049

10051050
next_cmd = meson_mmc_get_next_command(cmd);

0 commit comments

Comments
 (0)