Prevent races when doing read-modify-write of INTMASK
Subsystems (5)
v3 cover Prevent races when doing read-modify-write of INTMASK
In reply to a message not in the archiveBing Zhao at Marvell discovered a race in the way that dw_mmc was doing a read-modify-write of the INTMASK register. This 2-patch series attempts to fix the problem using a simple spinlock. In order to do so cleanly, we include a patch to tidy up the way that we disable low power mode when using SDIO interrupts.
This patch series was not tested on ToT Linux other than basic compiling and booting, since we don't have the whole Marvell SDIO stack up and running in mainline yet. This series is based on mmc-next (e76b855 mmc: sdhci-esdhc-imx: set actual_clock in clock setting) merged atop mainlinx Linux.
Changes in v3:
- Add fixup to pandora_wl1251_init_card().
Changes in v2:
- Core patch new for this version.
- Fixed "|" to "&".
- intmask_lock renamed to irq_lock
Doug Anderson (3):
mmc: core: Support the optional init_card() callback for MMC and SD
mmc: dw_mmc: Cleanup disable of low power mode w/ SDIO interrupts
mmc: dw_mmc: Protect read-modify-write of INTMASK with a lock
arch/arm/mach-omap2/board-omap3pandora.c | 14 +++--- drivers/mmc/core/mmc.c | 6 +++ drivers/mmc/core/sd.c | 7 ++- drivers/mmc/host/dw_mmc.c | 79 +++++++++++++++++++------------- drivers/mmc/host/dw_mmc.h | 1 + include/linux/mmc/dw_mmc.h | 6 +++ 6 files changed, 74 insertions(+), 39 deletions(-)
Signature · 1 line
1.8.4
PATCH v3 1/3 mmc: core: Support the optional init_card() callback for MMC and SD
In (3fcb027 ARM: MXC: mxcmmc: work around a bug in the SDHC busy line handling) the optional init_card() callback was added. According to the original change it was "for now only called from mmc_sdio_init_card()".
This callback really ought to be called from the SD and MMC init functions as well. One current user of this callback (mxcmci_init_card) will not work as expected if you insert an SDIO card, then eject it and put a normal SD card in. Specifically the normal SD card will not get to run with 4-bit data.
I'd like to use the init_card() callback to handle a similar quirk on dw_mmc when using SDIO Interrupts (the "low power" feature of the card needs to be disabled), so that will add a second user of the function.
As part of this change fixup the one place that relied on the callback only happening for SDIO cards.
---
Changes in v3:
- Add fixup to pandora_wl1251_init_card().
Changes in v2:
- New for this version.
arch/arm/mach-omap2/board-omap3pandora.c | 14 ++++++++------ drivers/mmc/core/mmc.c | 6 ++++++ drivers/mmc/core/sd.c | 7 ++++++- 3 files changed, 20 insertions(+), 7 deletions(-)
arch/arm/mach-omap2/board-omap3pandora.c +8 -6
@@ -254,12 +254,14 @@ static void pandora_wl1251_init_card(struct mmc_card *card)* We have TI wl1251 attached to MMC3. Pass this information to * SDIO core because it can't be probed by normal methods. */ - card->quirks |=MMC_QUIRK_NONSTD_SDIO ; - card->cccr.wide_bus =1 ; - card->cis.vendor =0x104c ; - card->cis.device =0x9066 ; - card->cis.blksize =512 ; - card->cis.max_dtr =20000000 ; +if (card->type ==MMC_TYPE_SDIO || card->type ==MMC_TYPE_SD_COMBO ) { + card->quirks |=MMC_QUIRK_NONSTD_SDIO ; + card->cccr.wide_bus =1 ; + card->cis.vendor =0x104c ; + card->cis.device =0x9066 ; + card->cis.blksize =512 ; + card->cis.max_dtr =20000000 ; + } }static struct omap2_hsmmc_info omap3pandora_mmc[] = {
drivers/mmc/core/mmc.c +6 -0
@@ -940,6 +940,12 @@ static int mmc_init_card(struct mmc_host *host, u32 ocr, }/* +* Call the optional HC's init_card function to handle quirks. +*/ +if (host->ops->init_card) + host->ops->init_card (host, card); + +/* * For native busses: set card RCA and quit open drain mode. */ if (!mmc_host_is_spi (host)) {
drivers/mmc/core/sd.c +6 -1
@@ -940,6 +940,12 @@ static int mmc_sd_init_card(struct mmc_host *host, u32 ocr, }/* +* Call the optional HC's init_card function to handle quirks. +*/ +if (host->ops->init_card) + host->ops->init_card (host, card); + +/* * For native busses: get card RCA and quit open drain mode. */ if (!mmc_host_is_spi (host)) {
@@ -1286,4 +1292,3 @@ err:return err; } -
Signature · 1 line
1.8.4
PATCH v3 3/3 mmc: dw_mmc: Protect read-modify-write of INTMASK with a lock
In reply to Doug AndersonWe're running into cases where our enabling of the SDIO interrupt in dw_mmc doesn't actually take effect. Specifically, adding patch like this:
+++ b/drivers/mmc/host/dw_mmc.c
@@ -1076,6 +1076,9 @@ static void dw_mci_enable_sdio_irq(struct mmc_host *mmc, int enb)
mci_writel(host, INTMASK,
(int_mask | SDMMC_INT_SDIO(slot->id)));
+ int_mask = mci_readl(host, INTMASK);
+ if (!(int_mask & SDMMC_INT_SDIO(slot->id)))
+ dev_err(&mmc->class_dev, "failed to enable sdio irq\n");
} else {...actually triggers the error message. That's because the dw_mci_enable_sdio_irq() unsafely does a read-modify-write of the INTMASK register.
We can't just use the standard host->lock since that lock is not irq safe and mmc_signal_sdio_irq() (called from interrupt context) calls dw_mci_enable_sdio_irq(). Add a new irq-safe lock to protect INTMASK.
An alternate solution to this is to punt mmc_signal_sdio_irq() to the tasklet and then protect INTMASK modifications by the standard host lock. This seemed like a bit more of a high-latency change.
---
Changes in v3: None
Changes in v2:
- intmask_lock renamed to irq_lock
drivers/mmc/host/dw_mmc.c | 13 +++++++++++++ include/linux/mmc/dw_mmc.h | 6 ++++++ 2 files changed, 19 insertions(+)
drivers/mmc/host/dw_mmc.c +13 -0
@@ -657,6 +657,7 @@ disable:static int dw_mci_submit_data_dma (struct dw_mci *host,struct mmc_data *data) { +unsigned long irqflags;int sg_len;u32 temp;
@@ -693,9 +694,11 @@ static int dw_mci_submit_data_dma(struct dw_mci *host, struct mmc_data *data)mci_writel (host,CTRL , temp);/* Disable RX/TX IRQs, let DMA handle it */ +spin_lock_irqsave (&host->irq_lock, irqflags); temp =mci_readl (host,INTMASK ); temp &= ~(SDMMC_INT_RXDR |SDMMC_INT_TXDR );mci_writel (host,INTMASK , temp); +spin_unlock_irqrestore (&host->irq_lock, irqflags); host->dma_ops->start (host, sg_len);
@@ -704,6 +707,7 @@ static int dw_mci_submit_data_dma(struct dw_mci *host, struct mmc_data *data)static void dw_mci_submit_data (struct dw_mci *host,struct mmc_data *data) { +unsigned long irqflags;u32 temp; data->error = -EINPROGRESS ;
@@ -732,9 +736,12 @@ static void dw_mci_submit_data(struct dw_mci *host, struct mmc_data *data) host->part_buf_count =0 ;mci_writel (host,RINTSTS ,SDMMC_INT_TXDR |SDMMC_INT_RXDR ); + +spin_lock_irqsave (&host->irq_lock, irqflags); temp =mci_readl (host,INTMASK ); temp |=SDMMC_INT_TXDR |SDMMC_INT_RXDR ;mci_writel (host,INTMASK , temp); +spin_unlock_irqrestore (&host->irq_lock, irqflags); temp =mci_readl (host,CTRL ); temp &= ~SDMMC_CTRL_DMA_ENABLE ;
@@ -1089,8 +1096,11 @@ static void dw_mci_enable_sdio_irq(struct mmc_host *mmc, int enb) {struct dw_mci_slot *slot =mmc_priv (mmc);struct dw_mci *host = slot->host; +unsigned long irqflags;u32 int_mask; +spin_lock_irqsave (&host->irq_lock, irqflags); +/* Enable/disable Slot Specific SDIO interrupt */ int_mask =mci_readl (host,INTMASK );if (enb)
@@ -1098,6 +1108,8 @@ static void dw_mci_enable_sdio_irq(struct mmc_host *mmc, int enb)else int_mask &= ~SDMMC_INT_SDIO (slot->id);mci_writel (host,INTMASK , int_mask); + +spin_unlock_irqrestore (&host->irq_lock, irqflags); }static int dw_mci_execute_tuning (struct mmc_host *mmc,u32 opcode)
@@ -2500,6 +2512,7 @@ int dw_mci_probe(struct dw_mci *host) host->quirks = host->pdata->quirks;spin_lock_init (&host->lock); +spin_lock_init (&host->irq_lock);INIT_LIST_HEAD (&host->queue);/*
include/linux/mmc/dw_mmc.h +6 -0
@@ -102,6 +102,11 @@ struct mmc_data;* @cur_slot, @mrq and @state. These must always be updated * at the same time while holding @lock. * +* @irq_lock is an irq-safe spinlock protecting the INTMASK register +* to allow the interrupt handler to modify it directly. Held for only long +* enough to read-modify-write INTMASK and no other locks are grabbed when +* holding this one. +* * The @mrq field of struct dw_mci_slot is also protected by @lock, * and must always be written at the same time as the slot is added to * @queue.
@@ -121,6 +126,7 @@ struct mmc_data;*/ struct dw_mci {spinlock_t lock; +spinlock_t irq_lock;void __iomem *regs;struct scatterlist *sg;
Signature · 1 line
1.8.4
PATCH v3 2/3 mmc: dw_mmc: Cleanup disable of low power mode w/ SDIO interrupts
In reply to Doug AndersonIn the patch (9623b5b mmc: dw_mmc: Disable low power mode if SDIO interrupts are used) I added code that disabled the low power mode of dw_mmc when SDIO interrupts are used. That code worked but always felt a little hacky because we ended up disabling low power as a side effect of the first enable_sdio_irq() call. That wouldn't be so bad except that disabling low power involves a complicated process of writing to the CMD/CMDARG registers and that extra process makes it difficult to cleanly the read-modify-write race in dw_mci_enable_sdio_irq() (see future patch in the series).
Change the code to take advantage of the init_card() callback of the mmc core to know when an SDIO card has been inserted. If we've got a SDIO card and we're configured to use SDIO Interrupts then turn off "low power mode" right away.
---
Changes in v3: None
Changes in v2:
- Fixed "|" to "&".
drivers/mmc/host/dw_mmc.c | 68 ++++++++++++++++++++++++----------------------- drivers/mmc/host/dw_mmc.h | 1 + 2 files changed, 36 insertions(+), 33 deletions(-)
drivers/mmc/host/dw_mmc.c +35 -33
@@ -27,6 +27,7 @@#include <linux/stat.h> #include <linux/delay.h> #include <linux/irq.h> +#include <linux/mmc/card.h> #include <linux/mmc/host.h> #include <linux/mmc/mmc.h> #include <linux/mmc/sdio.h>
@@ -822,7 +823,7 @@ static void dw_mci_setup_bus(struct dw_mci_slot *slot, bool force_clkinit)/* enable clock; only low power if no SDIO */ clk_en_a =SDMMC_CLKEN_ENABLE << slot->id; -if (!(mci_readl (host,INTMASK ) &SDMMC_INT_SDIO (slot->id))) +if (!test_bit (DW_MMC_CARD_NO_LOW_PWR , &slot->flags)) clk_en_a |=SDMMC_CLKEN_LOW_PWR << slot->id;mci_writel (host,CLKENA , clk_en_a);
@@ -1050,27 +1051,37 @@ static int dw_mci_get_cd(struct mmc_host *mmc)return present; } -/* -* Disable lower power mode. -* -* Low power mode will stop the card clock when idle. According to the -* description of the CLKENA register we should disable low power mode -* for SDIO cards if we need SDIO interrupts to work. -* -* This function is fast if low power mode is already disabled. -*/ -static void dw_mci_disable_low_power (struct dw_mci_slot *slot) +static void dw_mci_init_card (struct mmc_host *mmc,struct mmc_card *card) { +struct dw_mci_slot *slot =mmc_priv (mmc);struct dw_mci *host = slot->host; -u32 clk_en_a; -const u32 clken_low_pwr =SDMMC_CLKEN_LOW_PWR << slot->id; - clk_en_a =mci_readl (host,CLKENA ); +/* +* Low power mode will stop the card clock when idle. According to the +* description of the CLKENA register we should disable low power mode +* for SDIO cards if we need SDIO interrupts to work. +*/ +if (mmc->caps &MMC_CAP_SDIO_IRQ ) { +const u32 clken_low_pwr =SDMMC_CLKEN_LOW_PWR << slot->id; +u32 clk_en_a_old; +u32 clk_en_a; -if (clk_en_a & clken_low_pwr) { -mci_writel (host,CLKENA , clk_en_a & ~clken_low_pwr); -mci_send_cmd (slot,SDMMC_CMD_UPD_CLK | -SDMMC_CMD_PRV_DAT_WAIT ,0 ); + clk_en_a_old =mci_readl (host,CLKENA ); + +if (card->type ==MMC_TYPE_SDIO || + card->type ==MMC_TYPE_SD_COMBO ) { +set_bit (DW_MMC_CARD_NO_LOW_PWR , &slot->flags); + clk_en_a = clk_en_a_old & ~clken_low_pwr; + }else { +clear_bit (DW_MMC_CARD_NO_LOW_PWR , &slot->flags); + clk_en_a = clk_en_a_old | clken_low_pwr; + } + +if (clk_en_a != clk_en_a_old) { +mci_writel (host,CLKENA , clk_en_a); +mci_send_cmd (slot,SDMMC_CMD_UPD_CLK | +SDMMC_CMD_PRV_DAT_WAIT ,0 ); + } } }
@@ -1082,21 +1093,11 @@ static void dw_mci_enable_sdio_irq(struct mmc_host *mmc, int enb)/* Enable/disable Slot Specific SDIO interrupt */ int_mask =mci_readl (host,INTMASK ); -if (enb) { -/* -* Turn off low power mode if it was enabled. This is a bit of -* a heavy operation and we disable / enable IRQs a lot, so -* we'll leave low power mode disabled and it will get -* re-enabled again in dw_mci_setup_bus(). -*/ -dw_mci_disable_low_power (slot); - -mci_writel (host,INTMASK , - (int_mask |SDMMC_INT_SDIO (slot->id))); - }else { -mci_writel (host,INTMASK , - (int_mask & ~SDMMC_INT_SDIO (slot->id))); - } +if (enb) + int_mask |=SDMMC_INT_SDIO (slot->id); +else + int_mask &= ~SDMMC_INT_SDIO (slot->id); +mci_writel (host,INTMASK , int_mask); }static int dw_mci_execute_tuning (struct mmc_host *mmc,u32 opcode)
@@ -1140,6 +1141,7 @@ static const struct mmc_host_ops dw_mci_ops = { .get_cd = dw_mci_get_cd, .enable_sdio_irq = dw_mci_enable_sdio_irq, .execute_tuning = dw_mci_execute_tuning, + .init_card = dw_mci_init_card, };static void dw_mci_request_end (struct dw_mci *host,struct mmc_request *mrq)
drivers/mmc/host/dw_mmc.h +1 -0
@@ -227,6 +227,7 @@ struct dw_mci_slot {unsigned long flags;#define DW_MMC_CARD_PRESENT 0#define DW_MMC_CARD_NEED_INIT 1 +#define DW_MMC_CARD_NO_LOW_PWR 2int id;int last_detect_state; };
Signature · 1 line
1.8.4