*** imx-sdma: misc fix ***
Subsystems (2)
cover *** imx-sdma: misc fix ***
this patch set contains the following changes
1. fix issues in cyclic dma
2. add support to SYNC DMA termination
3. avoid system hang, when SDMA channel 0 timeouts
4. add lock to prevent race condition
Jiada Wang (10):
dma: imx-sdma: use chn_real_count to report residue for UART
dma: imx-sdma: don't update BD in isr routine
dma: imx-sdma: clear BD_RROR flag before pass it to sdma script
dma: imx-sdma: update sdma channel status for cyclic dma
dma: imx-sdma: add flag to indicate SDMA channel state
dma: imx-sdma: add terminate_all support
dma: imx-sdma: Add synchronization support
dma: imx-sdma: abort updating channel when it has been terminated
dma: imx-sdma: disable channel 0 when it timeouts
dma: imx-sdma: clear channel0 interrupt bit in irq routine
drivers/dma/imx-sdma.c | 113 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 82 insertions(+), 31 deletions(-)
Signature · 1 line
2.4.5
PATCH 3/10 dma: imx-sdma: clear BD_RROR flag before pass it to sdma script
Previously in cyclic dma mode when sdma transfer fails for one buffer, it will mask BD_RROR flag for that buffer descriptor (BD). This flag won't be cleared unless a new cyclic dma transfer is prepared, so if sdma script next time iterates to the same BD, even this time the transfer is successful, but as BD_RROR flag is set, client side will still think the transfer failed.
This patch clears BD_RROR flag before pass it to sdma script.
--- drivers/dma/imx-sdma.c | 1 + 1 file changed, 1 insertion(+)
drivers/dma/imx-sdma.c +1 -0
@@ -671,6 +671,7 @@ static void sdma_handle_channel_loop(struct sdma_channel *sdmac)if (bd->mode.status &BD_RROR ) sdmac->status =DMA_ERROR ; + bd->mode.status &= ~BD_RROR ; bd->mode.status |=BD_DONE ; sdmac->buf_tail++; sdmac->buf_tail %= sdmac->num_bd;
Signature · 1 line
2.4.5
PATCH 2/10 dma: imx-sdma: don't update BD in isr routine
In reply to Jiada Wangcommit d1a792f3b407 ("Update imx-sdma cyclic handling to report residue") moves updating of BD to isr routine, to avoid stop
of cyclic dma, but there is chance 'new' isr comes before the 'old' tasklet can be fired, thus cause data loss due to missing of one tasklet. So move updating of BD back to tasklet.
--- drivers/dma/imx-sdma.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-)
drivers/dma/imx-sdma.c +3 -9
@@ -656,12 +656,6 @@ static void sdma_event_disable(struct sdma_channel *sdmac, unsigned int event)static void sdma_handle_channel_loop (struct sdma_channel *sdmac) { -if (sdmac->desc.callback) - sdmac->desc.callback (sdmac->desc.callback_param); -} - -static void sdma_update_channel_loop (struct sdma_channel *sdmac) -{struct sdma_buffer_descriptor *bd;/*
@@ -685,6 +679,9 @@ static void sdma_update_channel_loop(struct sdma_channel *sdmac) sdmac->chn_real_count = bd->mode.count; bd->mode.count = sdmac->chn_count; } + +if (sdmac->desc.callback) + sdmac->desc.callback (sdmac->desc.callback_param); } }
@@ -740,9 +737,6 @@ static irqreturn_t sdma_int_handler(int irq, void *dev_id)int channel =fls (stat) -1 ;struct sdma_channel *sdmac = &sdma->channel[channel]; -if (sdmac->flags &IMX_DMA_SG_LOOP ) -sdma_update_channel_loop (sdmac); -tasklet_schedule (&sdmac->tasklet);__clear_bit (channel, &stat);
Signature · 1 line
2.4.5
PATCH 4/10 dma: imx-sdma: update sdma channel status for cyclic dma
In reply to Jiada WangPreviously for cyclic dma mode, once sdma fails sdma channel status will be set to DMA_ERROR, unless the transfer is prepared again, sdmac status will always be kept to DMA_ERROR, even the transfer for following buffers is successful.
This patch updates sdmac status to the status of current buffer descriptor.
--- drivers/dma/imx-sdma.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/dma/imx-sdma.c +2 -0
@@ -670,6 +670,8 @@ static void sdma_handle_channel_loop(struct sdma_channel *sdmac)if (bd->mode.status &BD_RROR ) sdmac->status =DMA_ERROR ; +else + sdmac->status =DMA_IN_PROGRESS ; bd->mode.status &= ~BD_RROR ; bd->mode.status |=BD_DONE ;
Signature · 1 line
2.4.5
PATCH 7/10 dma: imx-sdma: Add synchronization support
In reply to Jiada WangImplement the new device_synchronize() callback to allow proper synchronization when stopping a channel. Since the driver already makes sure that no new complete callbacks are scheduled after the device_terminate_all() has been called, all left to do in the device_synchronize() callback is to wait for all currently running complete callbacks to finish.
--- drivers/dma/imx-sdma.c | 8 ++++++++ 1 file changed, 8 insertions(+)
drivers/dma/imx-sdma.c +8 -0
@@ -1408,6 +1408,13 @@ static int sdma_terminate_all(struct dma_chan *chan)return 0 ; } +static void sdma_synchronize (struct dma_chan *chan) +{ +struct sdma_channel *sdmac =to_sdma_chan (chan); + +tasklet_kill (&sdmac->tasklet); +} +#define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1 34#define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2 38#define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3 41
@@ -1827,6 +1834,7 @@ static int sdma_probe(struct platform_device *pdev) sdma->dma_device.residue_granularity =DMA_RESIDUE_GRANULARITY_BURST ; sdma->dma_device.device_issue_pending = sdma_issue_pending; sdma->dma_device.device_terminate_all = sdma_terminate_all; + sdma->dma_device.device_synchronize = sdma_synchronize; sdma->dma_device.dev->dma_parms = &sdma->dma_parms;dma_set_max_seg_size (sdma->dma_device.dev,65535 );
Signature · 1 line
2.4.5
PATCH 10/10 dma: imx-sdma: clear channel0 interrupt bit in irq routine
In reply to Jiada WangWhen SDMA channel0 timeouts, even it's disabled in error path, but sometimes we still see its interrupt bit be asserted, which causes irq routine be triggered continuously because no one else clears this bit.
This commit clears channel0 interrupt as well in irq routine, so that even channel0 timeouts, it won't cause irq storm, also adds lock to prevent irq routine to clear this bit when sdma_run_channel0() is busy checking it.
--- drivers/dma/imx-sdma.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/dma/imx-sdma.c +6 -2
@@ -768,12 +768,16 @@ static void sdma_tasklet(unsigned long data)static irqreturn_t sdma_int_handler (int irq,void *dev_id) {struct sdma_engine *sdma = dev_id; -unsigned long stat; +unsigned long stat, flags; + +spin_lock_irqsave (&sdma->channel_0_lock, flags); stat =readl_relaxed (sdma->regs +SDMA_H_INTR ); +writel_relaxed (stat, sdma->regs +SDMA_H_INTR );/* not interested in channel 0 interrupts */ stat &= ~1 ; -writel_relaxed (stat, sdma->regs +SDMA_H_INTR ); + +spin_unlock_irqrestore (&sdma->channel_0_lock, flags);while (stat) {int channel =fls (stat) -1 ;
Signature · 1 line
2.4.5
PATCH 6/10 dma: imx-sdma: add terminate_all support
In reply to Jiada WangImplement device_terminate_all(), so that dmaengine_terminate_async() can work.
--- drivers/dma/imx-sdma.c | 8 ++++++++ 1 file changed, 8 insertions(+)
drivers/dma/imx-sdma.c +8 -0
@@ -1401,6 +1401,13 @@ static void sdma_issue_pending(struct dma_chan *chan)sdma_enable_channel (sdma, sdmac->channel); } +static int sdma_terminate_all (struct dma_chan *chan) +{ +sdma_disable_channel (chan); + +return 0 ; +} +#define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1 34#define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2 38#define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3 41
@@ -1819,6 +1826,7 @@ static int sdma_probe(struct platform_device *pdev) sdma->dma_device.directions =BIT (DMA_DEV_TO_MEM ) |BIT (DMA_MEM_TO_DEV ); sdma->dma_device.residue_granularity =DMA_RESIDUE_GRANULARITY_BURST ; sdma->dma_device.device_issue_pending = sdma_issue_pending; + sdma->dma_device.device_terminate_all = sdma_terminate_all; sdma->dma_device.dev->dma_parms = &sdma->dma_parms;dma_set_max_seg_size (sdma->dma_device.dev,65535 );
Signature · 1 line
2.4.5
PATCH 9/10 dma: imx-sdma: disable channel 0 when it timeouts
In reply to Jiada WangPreviously when channel0 timeouts to finish its task, sdma_run_channel0() just returns without disable channel0, this will cause continuous interrupt later when channel0 finishs its task and set channel0 interrupt bit.
--- drivers/dma/imx-sdma.c | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-)
drivers/dma/imx-sdma.c +22 -21
@@ -572,6 +572,27 @@ static void sdma_enable_channel(struct sdma_engine *sdma, int channel)spin_unlock_irqrestore (&sdmac->lock, flags); } +static struct sdma_channel *to_sdma_chan (struct dma_chan *chan) +{ + return container_of(chan, struct sdma_channel, chan); +} + +static int sdma_disable_channel (struct dma_chan *chan) +{ +struct sdma_channel *sdmac =to_sdma_chan (chan); +struct sdma_engine *sdma = sdmac->sdma; +int channel = sdmac->channel; +unsigned long flags; + +spin_lock_irqsave (&sdmac->lock, flags); + sdmac->enabled = false; +writel_relaxed (BIT (channel), sdma->regs +SDMA_H_STATSTOP ); + sdmac->status =DMA_ERROR ; +spin_unlock_irqrestore (&sdmac->lock, flags); + +return 0 ; +} +/* * sdma_run_channel0 - run a channel and wait till it's done */
@@ -592,6 +613,7 @@ static int sdma_run_channel0(struct sdma_engine *sdma)/* Clear the interrupt status */ writel_relaxed (ret, sdma->regs +SDMA_H_INTR ); }else { +sdma_disable_channel (&sdma->channel[0 ].chan);dev_err (sdma->dev,"Timeout waiting for CH0 ready\n" ); }
@@ -916,27 +938,6 @@ static int sdma_load_context(struct sdma_channel *sdmac)return ret; } -static struct sdma_channel *to_sdma_chan (struct dma_chan *chan) -{ - return container_of(chan, struct sdma_channel, chan); -} - -static int sdma_disable_channel (struct dma_chan *chan) -{ -struct sdma_channel *sdmac =to_sdma_chan (chan); -struct sdma_engine *sdma = sdmac->sdma; -int channel = sdmac->channel; -unsigned long flags; - -spin_lock_irqsave (&sdmac->lock, flags); - sdmac->enabled = false; -writel_relaxed (BIT (channel), sdma->regs +SDMA_H_STATSTOP ); - sdmac->status =DMA_ERROR ; -spin_unlock_irqrestore (&sdmac->lock, flags); - -return 0 ; -} -static void sdma_set_watermarklevel_for_p2p (struct sdma_channel *sdmac) {struct sdma_engine *sdma = sdmac->sdma;
Signature · 1 line
2.4.5
PATCH 8/10 dma: imx-sdma: abort updating channel when it has been terminated
In reply to Jiada WangIn case the corresponding channel has already been terminated, then instead of go on updating channel status, driver should abort from sdma_handle_channel_loop(), otherwise channel status will be updated incorrecly.
This patch also adds lock to avoid race between terminate of channel, and updaing of channel status in sdma_handle_channel_loop().
--- drivers/dma/imx-sdma.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/dma/imx-sdma.c +11 -1
@@ -664,16 +664,25 @@ static void sdma_event_disable(struct sdma_channel *sdmac, unsigned int event)static void sdma_handle_channel_loop (struct sdma_channel *sdmac) {struct sdma_buffer_descriptor *bd; +unsigned long flags;/* * loop mode. Iterate over descriptors, re-setup them and * call callback function. */ while (1 ) { +spin_lock_irqsave (&sdmac->lock, flags); +if (!sdmac->enabled) { +spin_unlock_irqrestore (&sdmac->lock, flags); +break ; + } + bd = &sdmac->bd[sdmac->buf_tail]; -if (bd->mode.status &BD_DONE ) +if (bd->mode.status &BD_DONE ) { +spin_unlock_irqrestore (&sdmac->lock, flags);break ; + }if (bd->mode.status &BD_RROR ) sdmac->status =DMA_ERROR ;
@@ -690,6 +699,7 @@ static void sdma_handle_channel_loop(struct sdma_channel *sdmac) bd->mode.count = sdmac->chn_count; } +spin_unlock_irqrestore (&sdmac->lock, flags);if (sdmac->desc.callback) sdmac->desc.callback (sdmac->desc.callback_param); }
Signature · 1 line
2.4.5
PATCH 5/10 dma: imx-sdma: add flag to indicate SDMA channel state
In reply to Jiada WangThere is race between STOP of SDMA channel and finish of SDMA transfer, so some times, even after sdma_disable_channel() is called, the bit of 'terminated channel' in INTR may still get asserted, thus cause an extra sdma tasklet be called. Add flag 'enabled' to each sdma channel to indicate its state. only when SDMA channel is in its enabled state, irq handler can schedule a sdma tasklet for it.
--- drivers/dma/imx-sdma.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)
drivers/dma/imx-sdma.c +15 -1
@@ -327,6 +327,7 @@ struct sdma_channel {unsigned int chn_real_count;struct tasklet_struct tasklet;struct imx_dma_data data; +bool enabled; };#define IMX_DMA_SG_LOOP BIT(0)
@@ -562,7 +563,13 @@ static int sdma_config_ownership(struct sdma_channel *sdmac,static void sdma_enable_channel (struct sdma_engine *sdma,int channel) { +struct sdma_channel *sdmac = &sdma->channel[channel]; +unsigned long flags; + +spin_lock_irqsave (&sdmac->lock, flags); + sdmac->enabled = true;writel (BIT (channel), sdma->regs +SDMA_H_START ); +spin_unlock_irqrestore (&sdmac->lock, flags); }/*
@@ -740,9 +747,12 @@ static irqreturn_t sdma_int_handler(int irq, void *dev_id)int channel =fls (stat) -1 ;struct sdma_channel *sdmac = &sdma->channel[channel]; -tasklet_schedule (&sdmac->tasklet); +spin_lock (&sdmac->lock); +if (sdmac->enabled) +tasklet_schedule (&sdmac->tasklet);__clear_bit (channel, &stat); +spin_unlock (&sdmac->lock); }return IRQ_HANDLED ;
@@ -906,9 +916,13 @@ static int sdma_disable_channel(struct dma_chan *chan)struct sdma_channel *sdmac =to_sdma_chan (chan);struct sdma_engine *sdma = sdmac->sdma;int channel = sdmac->channel; +unsigned long flags; +spin_lock_irqsave (&sdmac->lock, flags); + sdmac->enabled = false;writel_relaxed (BIT (channel), sdma->regs +SDMA_H_STATSTOP ); sdmac->status =DMA_ERROR ; +spin_unlock_irqrestore (&sdmac->lock, flags);return 0 ; }
Signature · 1 line
2.4.5
PATCH 1/10 dma: imx-sdma: use chn_real_count to report residue for UART
In reply to Jiada WangFor uart rx dma data may not receive fully, so the number of data read by sdma script once isn't always equal to period_len, thus residue returned from sdma_tx_status() isn't valid for uart rx dma. The old way to use chn_real_count to report residue should be used for uart rx dma.
--- drivers/dma/imx-sdma.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/dma/imx-sdma.c +10 -1
@@ -680,6 +680,11 @@ static void sdma_update_channel_loop(struct sdma_channel *sdmac) bd->mode.status |=BD_DONE ; sdmac->buf_tail++; sdmac->buf_tail %= sdmac->num_bd; +if (sdmac->peripheral_type ==IMX_DMATYPE_UART ) { +/* restore mode.count after counter readed */ + sdmac->chn_real_count = bd->mode.count; + bd->mode.count = sdmac->chn_count; + } } }
@@ -1285,6 +1290,9 @@ static struct dma_async_tx_descriptor *sdma_prep_dma_cyclic( goto err_out; } +if (sdmac->peripheral_type ==IMX_DMATYPE_UART ) + sdmac->chn_count = period_len; +while (buf < buf_len) {struct sdma_buffer_descriptor *bd = &sdmac->bd[i];int param;
@@ -1361,7 +1369,8 @@ static enum dma_status sdma_tx_status(struct dma_chan *chan,struct sdma_channel *sdmac =to_sdma_chan (chan);u32 residue; -if (sdmac->flags &IMX_DMA_SG_LOOP ) +if ((sdmac->flags &IMX_DMA_SG_LOOP ) && + (sdmac->peripheral_type !=IMX_DMATYPE_UART )) residue = (sdmac->num_bd - sdmac->buf_tail) * sdmac->period_len;else residue = sdmac->chn_count - sdmac->chn_real_count;
Signature · 1 line
2.4.5
Hi Jiada,
Quoted 18 lines · Jiada Wang
this patch set contains the following changes
1. fix issues in cyclic dma
2. add support to SYNC DMA termination
3. avoid system hang, when SDMA channel 0 timeouts
4. add lock to prevent race conditionJiada Wang (10):
dma: imx-sdma: usechn_real_countto report residue for UART
dma: imx-sdma: don't update BD in isr routine
dma: imx-sdma: clearBD_RRORflag before pass it to sdma script
dma: imx-sdma: update sdma channel status for cyclic dma
dma: imx-sdma: add flag to indicate SDMA channel state
dma: imx-sdma: addterminate_allsupport
dma: imx-sdma: Add synchronization support
dma: imx-sdma: abort updating channel when it has been terminated
dma: imx-sdma: disable channel 0 when it timeouts
dma: imx-sdma: clear channel0 interrupt bit in irq routine
Quoted 3 lines
drivers/dma/imx-sdma.c | 113 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 82 insertions(+), 31 deletions(-)
I think it's worth to add Sascha and Shawn to Cc to let them know about the change.
--
With best wishes,
Vladimir