2018-11-10 23:34:40 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2014-01-07 03:18:24 +08:00
|
|
|
/*
|
|
|
|
* BCM2835 DMA engine support
|
|
|
|
*
|
|
|
|
* Author: Florian Meier <florian.meier@koalo.de>
|
|
|
|
* Copyright 2013
|
|
|
|
*
|
|
|
|
* Based on
|
|
|
|
* OMAP DMAengine support by Russell King
|
|
|
|
*
|
|
|
|
* BCM2708 DMA Driver
|
|
|
|
* Copyright (C) 2010 Broadcom
|
|
|
|
*
|
|
|
|
* Raspberry Pi PCM I2S ALSA Driver
|
|
|
|
* Copyright (c) by Phil Poole 2013
|
|
|
|
*
|
|
|
|
* MARVELL MMP Peripheral DMA Driver
|
|
|
|
* Copyright 2012 Marvell International Ltd.
|
|
|
|
*/
|
|
|
|
#include <linux/dmaengine.h>
|
|
|
|
#include <linux/dma-mapping.h>
|
2015-11-16 19:09:03 +08:00
|
|
|
#include <linux/dmapool.h>
|
2014-01-07 03:18:24 +08:00
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/list.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/spinlock.h>
|
|
|
|
#include <linux/of.h>
|
|
|
|
#include <linux/of_dma.h>
|
|
|
|
|
|
|
|
#include "virt-dma.h"
|
|
|
|
|
2016-04-11 21:29:08 +08:00
|
|
|
#define BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED 14
|
|
|
|
#define BCM2835_DMA_CHAN_NAME_SIZE 8
|
|
|
|
|
2019-09-11 18:15:30 +08:00
|
|
|
/**
|
|
|
|
* struct bcm2835_dmadev - BCM2835 DMA controller
|
|
|
|
* @ddev: DMA device
|
|
|
|
* @base: base address of register map
|
|
|
|
* @dma_parms: DMA parameters (to convey 1 GByte max segment size to clients)
|
2019-09-11 18:15:30 +08:00
|
|
|
* @zero_page: bus address of zero page (to detect transactions copying from
|
|
|
|
* zero page and avoid accessing memory if so)
|
2019-09-11 18:15:30 +08:00
|
|
|
*/
|
2014-01-07 03:18:24 +08:00
|
|
|
struct bcm2835_dmadev {
|
|
|
|
struct dma_device ddev;
|
|
|
|
void __iomem *base;
|
|
|
|
struct device_dma_parameters dma_parms;
|
2019-09-11 18:15:30 +08:00
|
|
|
dma_addr_t zero_page;
|
2014-01-07 03:18:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct bcm2835_dma_cb {
|
|
|
|
uint32_t info;
|
|
|
|
uint32_t src;
|
|
|
|
uint32_t dst;
|
|
|
|
uint32_t length;
|
|
|
|
uint32_t stride;
|
|
|
|
uint32_t next;
|
|
|
|
uint32_t pad[2];
|
|
|
|
};
|
|
|
|
|
2015-11-16 19:09:03 +08:00
|
|
|
struct bcm2835_cb_entry {
|
|
|
|
struct bcm2835_dma_cb *cb;
|
|
|
|
dma_addr_t paddr;
|
|
|
|
};
|
|
|
|
|
2014-01-07 03:18:24 +08:00
|
|
|
struct bcm2835_chan {
|
|
|
|
struct virt_dma_chan vc;
|
|
|
|
|
|
|
|
struct dma_slave_config cfg;
|
|
|
|
unsigned int dreq;
|
|
|
|
|
|
|
|
int ch;
|
|
|
|
struct bcm2835_desc *desc;
|
2015-11-16 19:09:03 +08:00
|
|
|
struct dma_pool *cb_pool;
|
2014-01-07 03:18:24 +08:00
|
|
|
|
|
|
|
void __iomem *chan_base;
|
|
|
|
int irq_number;
|
2016-04-11 21:29:08 +08:00
|
|
|
unsigned int irq_flags;
|
2016-03-17 03:25:00 +08:00
|
|
|
|
|
|
|
bool is_lite_channel;
|
2014-01-07 03:18:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct bcm2835_desc {
|
2015-11-16 19:09:03 +08:00
|
|
|
struct bcm2835_chan *c;
|
2014-01-07 03:18:24 +08:00
|
|
|
struct virt_dma_desc vd;
|
|
|
|
enum dma_transfer_direction dir;
|
|
|
|
|
|
|
|
unsigned int frames;
|
|
|
|
size_t size;
|
2016-03-17 03:24:58 +08:00
|
|
|
|
|
|
|
bool cyclic;
|
2016-03-17 03:24:59 +08:00
|
|
|
|
|
|
|
struct bcm2835_cb_entry cb_list[];
|
2014-01-07 03:18:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#define BCM2835_DMA_CS 0x00
|
|
|
|
#define BCM2835_DMA_ADDR 0x04
|
2016-03-17 03:24:57 +08:00
|
|
|
#define BCM2835_DMA_TI 0x08
|
2014-01-07 03:18:24 +08:00
|
|
|
#define BCM2835_DMA_SOURCE_AD 0x0c
|
|
|
|
#define BCM2835_DMA_DEST_AD 0x10
|
2016-03-17 03:24:57 +08:00
|
|
|
#define BCM2835_DMA_LEN 0x14
|
|
|
|
#define BCM2835_DMA_STRIDE 0x18
|
|
|
|
#define BCM2835_DMA_NEXTCB 0x1c
|
|
|
|
#define BCM2835_DMA_DEBUG 0x20
|
2014-01-07 03:18:24 +08:00
|
|
|
|
|
|
|
/* DMA CS Control and Status bits */
|
2016-03-17 03:24:57 +08:00
|
|
|
#define BCM2835_DMA_ACTIVE BIT(0) /* activate the DMA */
|
|
|
|
#define BCM2835_DMA_END BIT(1) /* current CB has ended */
|
|
|
|
#define BCM2835_DMA_INT BIT(2) /* interrupt status */
|
|
|
|
#define BCM2835_DMA_DREQ BIT(3) /* DREQ state */
|
2014-01-07 03:18:24 +08:00
|
|
|
#define BCM2835_DMA_ISPAUSED BIT(4) /* Pause requested or not active */
|
|
|
|
#define BCM2835_DMA_ISHELD BIT(5) /* Is held by DREQ flow control */
|
2016-03-17 03:24:57 +08:00
|
|
|
#define BCM2835_DMA_WAITING_FOR_WRITES BIT(6) /* waiting for last
|
|
|
|
* AXI-write to ack
|
|
|
|
*/
|
|
|
|
#define BCM2835_DMA_ERR BIT(8)
|
|
|
|
#define BCM2835_DMA_PRIORITY(x) ((x & 15) << 16) /* AXI priority */
|
|
|
|
#define BCM2835_DMA_PANIC_PRIORITY(x) ((x & 15) << 20) /* panic priority */
|
|
|
|
/* current value of TI.BCM2835_DMA_WAIT_RESP */
|
|
|
|
#define BCM2835_DMA_WAIT_FOR_WRITES BIT(28)
|
|
|
|
#define BCM2835_DMA_DIS_DEBUG BIT(29) /* disable debug pause signal */
|
2014-01-07 03:18:24 +08:00
|
|
|
#define BCM2835_DMA_ABORT BIT(30) /* Stop current CB, go to next, WO */
|
|
|
|
#define BCM2835_DMA_RESET BIT(31) /* WO, self clearing */
|
|
|
|
|
2016-03-17 03:24:57 +08:00
|
|
|
/* Transfer information bits - also bcm2835_cb.info field */
|
2014-01-07 03:18:24 +08:00
|
|
|
#define BCM2835_DMA_INT_EN BIT(0)
|
2016-03-17 03:24:57 +08:00
|
|
|
#define BCM2835_DMA_TDMODE BIT(1) /* 2D-Mode */
|
|
|
|
#define BCM2835_DMA_WAIT_RESP BIT(3) /* wait for AXI-write to be acked */
|
2014-01-07 03:18:24 +08:00
|
|
|
#define BCM2835_DMA_D_INC BIT(4)
|
2016-03-17 03:24:57 +08:00
|
|
|
#define BCM2835_DMA_D_WIDTH BIT(5) /* 128bit writes if set */
|
|
|
|
#define BCM2835_DMA_D_DREQ BIT(6) /* enable DREQ for destination */
|
|
|
|
#define BCM2835_DMA_D_IGNORE BIT(7) /* ignore destination writes */
|
2014-01-07 03:18:24 +08:00
|
|
|
#define BCM2835_DMA_S_INC BIT(8)
|
2016-03-17 03:24:57 +08:00
|
|
|
#define BCM2835_DMA_S_WIDTH BIT(9) /* 128bit writes if set */
|
|
|
|
#define BCM2835_DMA_S_DREQ BIT(10) /* enable SREQ for source */
|
|
|
|
#define BCM2835_DMA_S_IGNORE BIT(11) /* ignore source reads - read 0 */
|
|
|
|
#define BCM2835_DMA_BURST_LENGTH(x) ((x & 15) << 12)
|
|
|
|
#define BCM2835_DMA_PER_MAP(x) ((x & 31) << 16) /* REQ source */
|
|
|
|
#define BCM2835_DMA_WAIT(x) ((x & 31) << 21) /* add DMA-wait cycles */
|
|
|
|
#define BCM2835_DMA_NO_WIDE_BURSTS BIT(26) /* no 2 beat write bursts */
|
|
|
|
|
|
|
|
/* debug register bits */
|
|
|
|
#define BCM2835_DMA_DEBUG_LAST_NOT_SET_ERR BIT(0)
|
|
|
|
#define BCM2835_DMA_DEBUG_FIFO_ERR BIT(1)
|
|
|
|
#define BCM2835_DMA_DEBUG_READ_ERR BIT(2)
|
|
|
|
#define BCM2835_DMA_DEBUG_OUTSTANDING_WRITES_SHIFT 4
|
|
|
|
#define BCM2835_DMA_DEBUG_OUTSTANDING_WRITES_BITS 4
|
|
|
|
#define BCM2835_DMA_DEBUG_ID_SHIFT 16
|
|
|
|
#define BCM2835_DMA_DEBUG_ID_BITS 9
|
|
|
|
#define BCM2835_DMA_DEBUG_STATE_SHIFT 16
|
|
|
|
#define BCM2835_DMA_DEBUG_STATE_BITS 9
|
|
|
|
#define BCM2835_DMA_DEBUG_VERSION_SHIFT 25
|
|
|
|
#define BCM2835_DMA_DEBUG_VERSION_BITS 3
|
|
|
|
#define BCM2835_DMA_DEBUG_LITE BIT(28)
|
|
|
|
|
|
|
|
/* shared registers for all dma channels */
|
|
|
|
#define BCM2835_DMA_INT_STATUS 0xfe0
|
|
|
|
#define BCM2835_DMA_ENABLE 0xff0
|
2014-01-07 03:18:24 +08:00
|
|
|
|
|
|
|
#define BCM2835_DMA_DATA_TYPE_S8 1
|
|
|
|
#define BCM2835_DMA_DATA_TYPE_S16 2
|
|
|
|
#define BCM2835_DMA_DATA_TYPE_S32 4
|
|
|
|
#define BCM2835_DMA_DATA_TYPE_S128 16
|
|
|
|
|
|
|
|
/* Valid only for channels 0 - 14, 15 has its own base address */
|
|
|
|
#define BCM2835_DMA_CHAN(n) ((n) << 8) /* Base address */
|
|
|
|
#define BCM2835_DMA_CHANIO(base, n) ((base) + BCM2835_DMA_CHAN(n))
|
|
|
|
|
2016-03-17 03:25:00 +08:00
|
|
|
/* the max dma length for different channels */
|
|
|
|
#define MAX_DMA_LEN SZ_1G
|
|
|
|
#define MAX_LITE_DMA_LEN (SZ_64K - 4)
|
|
|
|
|
|
|
|
static inline size_t bcm2835_dma_max_frame_length(struct bcm2835_chan *c)
|
|
|
|
{
|
|
|
|
/* lite and normal channels have different max frame length */
|
|
|
|
return c->is_lite_channel ? MAX_LITE_DMA_LEN : MAX_DMA_LEN;
|
|
|
|
}
|
|
|
|
|
2016-03-17 03:24:59 +08:00
|
|
|
/* how many frames of max_len size do we need to transfer len bytes */
|
|
|
|
static inline size_t bcm2835_dma_frames_for_length(size_t len,
|
|
|
|
size_t max_len)
|
|
|
|
{
|
|
|
|
return DIV_ROUND_UP(len, max_len);
|
|
|
|
}
|
|
|
|
|
2014-01-07 03:18:24 +08:00
|
|
|
static inline struct bcm2835_dmadev *to_bcm2835_dma_dev(struct dma_device *d)
|
|
|
|
{
|
|
|
|
return container_of(d, struct bcm2835_dmadev, ddev);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct bcm2835_chan *to_bcm2835_dma_chan(struct dma_chan *c)
|
|
|
|
{
|
|
|
|
return container_of(c, struct bcm2835_chan, vc.chan);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct bcm2835_desc *to_bcm2835_dma_desc(
|
|
|
|
struct dma_async_tx_descriptor *t)
|
|
|
|
{
|
|
|
|
return container_of(t, struct bcm2835_desc, vd.tx);
|
|
|
|
}
|
|
|
|
|
2016-03-17 03:24:59 +08:00
|
|
|
static void bcm2835_dma_free_cb_chain(struct bcm2835_desc *desc)
|
2014-01-07 03:18:24 +08:00
|
|
|
{
|
2016-03-17 03:24:59 +08:00
|
|
|
size_t i;
|
2015-11-16 19:09:03 +08:00
|
|
|
|
|
|
|
for (i = 0; i < desc->frames; i++)
|
|
|
|
dma_pool_free(desc->c->cb_pool, desc->cb_list[i].cb,
|
|
|
|
desc->cb_list[i].paddr);
|
|
|
|
|
2014-01-07 03:18:24 +08:00
|
|
|
kfree(desc);
|
|
|
|
}
|
|
|
|
|
2016-03-17 03:24:59 +08:00
|
|
|
static void bcm2835_dma_desc_free(struct virt_dma_desc *vd)
|
|
|
|
{
|
|
|
|
bcm2835_dma_free_cb_chain(
|
|
|
|
container_of(vd, struct bcm2835_desc, vd));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bcm2835_dma_create_cb_set_length(
|
|
|
|
struct bcm2835_chan *chan,
|
|
|
|
struct bcm2835_dma_cb *control_block,
|
|
|
|
size_t len,
|
|
|
|
size_t period_len,
|
|
|
|
size_t *total_len,
|
|
|
|
u32 finalextrainfo)
|
|
|
|
{
|
2016-03-17 03:25:00 +08:00
|
|
|
size_t max_len = bcm2835_dma_max_frame_length(chan);
|
|
|
|
|
|
|
|
/* set the length taking lite-channel limitations into account */
|
|
|
|
control_block->length = min_t(u32, len, max_len);
|
2016-03-17 03:24:59 +08:00
|
|
|
|
|
|
|
/* finished if we have no period_length */
|
|
|
|
if (!period_len)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* period_len means: that we need to generate
|
|
|
|
* transfers that are terminating at every
|
|
|
|
* multiple of period_len - this is typically
|
|
|
|
* used to set the interrupt flag in info
|
|
|
|
* which is required during cyclic transfers
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* have we filled in period_length yet? */
|
2017-02-21 03:01:16 +08:00
|
|
|
if (*total_len + control_block->length < period_len) {
|
|
|
|
/* update number of bytes in this period so far */
|
|
|
|
*total_len += control_block->length;
|
2016-03-17 03:24:59 +08:00
|
|
|
return;
|
2017-02-21 03:01:16 +08:00
|
|
|
}
|
2016-03-17 03:24:59 +08:00
|
|
|
|
|
|
|
/* calculate the length that remains to reach period_length */
|
|
|
|
control_block->length = period_len - *total_len;
|
|
|
|
|
|
|
|
/* reset total_length for next period */
|
|
|
|
*total_len = 0;
|
|
|
|
|
|
|
|
/* add extrainfo bits in info */
|
|
|
|
control_block->info |= finalextrainfo;
|
|
|
|
}
|
|
|
|
|
2016-03-17 03:25:01 +08:00
|
|
|
static inline size_t bcm2835_dma_count_frames_for_sg(
|
|
|
|
struct bcm2835_chan *c,
|
|
|
|
struct scatterlist *sgl,
|
|
|
|
unsigned int sg_len)
|
|
|
|
{
|
|
|
|
size_t frames = 0;
|
|
|
|
struct scatterlist *sgent;
|
|
|
|
unsigned int i;
|
|
|
|
size_t plength = bcm2835_dma_max_frame_length(c);
|
|
|
|
|
|
|
|
for_each_sg(sgl, sgent, sg_len, i)
|
|
|
|
frames += bcm2835_dma_frames_for_length(
|
|
|
|
sg_dma_len(sgent), plength);
|
|
|
|
|
|
|
|
return frames;
|
|
|
|
}
|
|
|
|
|
2016-03-17 03:24:59 +08:00
|
|
|
/**
|
|
|
|
* bcm2835_dma_create_cb_chain - create a control block and fills data in
|
|
|
|
*
|
|
|
|
* @chan: the @dma_chan for which we run this
|
|
|
|
* @direction: the direction in which we transfer
|
|
|
|
* @cyclic: it is a cyclic transfer
|
|
|
|
* @info: the default info bits to apply per controlblock
|
|
|
|
* @frames: number of controlblocks to allocate
|
|
|
|
* @src: the src address to assign (if the S_INC bit is set
|
|
|
|
* in @info, then it gets incremented)
|
|
|
|
* @dst: the dst address to assign (if the D_INC bit is set
|
|
|
|
* in @info, then it gets incremented)
|
|
|
|
* @buf_len: the full buffer length (may also be 0)
|
|
|
|
* @period_len: the period length when to apply @finalextrainfo
|
|
|
|
* in addition to the last transfer
|
|
|
|
* this will also break some control-blocks early
|
|
|
|
* @finalextrainfo: additional bits in last controlblock
|
|
|
|
* (or when period_len is reached in case of cyclic)
|
|
|
|
* @gfp: the GFP flag to use for allocation
|
|
|
|
*/
|
|
|
|
static struct bcm2835_desc *bcm2835_dma_create_cb_chain(
|
|
|
|
struct dma_chan *chan, enum dma_transfer_direction direction,
|
|
|
|
bool cyclic, u32 info, u32 finalextrainfo, size_t frames,
|
|
|
|
dma_addr_t src, dma_addr_t dst, size_t buf_len,
|
|
|
|
size_t period_len, gfp_t gfp)
|
|
|
|
{
|
|
|
|
struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
|
|
|
|
size_t len = buf_len, total_len;
|
|
|
|
size_t frame;
|
|
|
|
struct bcm2835_desc *d;
|
|
|
|
struct bcm2835_cb_entry *cb_entry;
|
|
|
|
struct bcm2835_dma_cb *control_block;
|
|
|
|
|
2016-03-17 03:25:02 +08:00
|
|
|
if (!frames)
|
|
|
|
return NULL;
|
|
|
|
|
2016-03-17 03:24:59 +08:00
|
|
|
/* allocate and setup the descriptor. */
|
2019-01-05 02:30:34 +08:00
|
|
|
d = kzalloc(struct_size(d, cb_list, frames), gfp);
|
2016-03-17 03:24:59 +08:00
|
|
|
if (!d)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
d->c = c;
|
|
|
|
d->dir = direction;
|
|
|
|
d->cyclic = cyclic;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Iterate over all frames, create a control block
|
|
|
|
* for each frame and link them together.
|
|
|
|
*/
|
|
|
|
for (frame = 0, total_len = 0; frame < frames; d->frames++, frame++) {
|
|
|
|
cb_entry = &d->cb_list[frame];
|
|
|
|
cb_entry->cb = dma_pool_alloc(c->cb_pool, gfp,
|
|
|
|
&cb_entry->paddr);
|
|
|
|
if (!cb_entry->cb)
|
|
|
|
goto error_cb;
|
|
|
|
|
|
|
|
/* fill in the control block */
|
|
|
|
control_block = cb_entry->cb;
|
|
|
|
control_block->info = info;
|
|
|
|
control_block->src = src;
|
|
|
|
control_block->dst = dst;
|
|
|
|
control_block->stride = 0;
|
|
|
|
control_block->next = 0;
|
|
|
|
/* set up length in control_block if requested */
|
|
|
|
if (buf_len) {
|
|
|
|
/* calculate length honoring period_length */
|
|
|
|
bcm2835_dma_create_cb_set_length(
|
|
|
|
c, control_block,
|
|
|
|
len, period_len, &total_len,
|
|
|
|
cyclic ? finalextrainfo : 0);
|
|
|
|
|
|
|
|
/* calculate new remaining length */
|
|
|
|
len -= control_block->length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* link this the last controlblock */
|
|
|
|
if (frame)
|
|
|
|
d->cb_list[frame - 1].cb->next = cb_entry->paddr;
|
|
|
|
|
|
|
|
/* update src and dst and length */
|
|
|
|
if (src && (info & BCM2835_DMA_S_INC))
|
|
|
|
src += control_block->length;
|
|
|
|
if (dst && (info & BCM2835_DMA_D_INC))
|
|
|
|
dst += control_block->length;
|
|
|
|
|
|
|
|
/* Length of total transfer */
|
|
|
|
d->size += control_block->length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* the last frame requires extra flags */
|
|
|
|
d->cb_list[d->frames - 1].cb->info |= finalextrainfo;
|
|
|
|
|
|
|
|
/* detect a size missmatch */
|
|
|
|
if (buf_len && (d->size != buf_len))
|
|
|
|
goto error_cb;
|
|
|
|
|
|
|
|
return d;
|
|
|
|
error_cb:
|
|
|
|
bcm2835_dma_free_cb_chain(d);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-03-17 03:25:01 +08:00
|
|
|
static void bcm2835_dma_fill_cb_chain_with_sg(
|
|
|
|
struct dma_chan *chan,
|
|
|
|
enum dma_transfer_direction direction,
|
|
|
|
struct bcm2835_cb_entry *cb,
|
|
|
|
struct scatterlist *sgl,
|
|
|
|
unsigned int sg_len)
|
|
|
|
{
|
|
|
|
struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
|
2016-06-30 20:47:10 +08:00
|
|
|
size_t len, max_len;
|
|
|
|
unsigned int i;
|
2016-03-17 03:25:01 +08:00
|
|
|
dma_addr_t addr;
|
|
|
|
struct scatterlist *sgent;
|
|
|
|
|
2016-06-30 20:47:10 +08:00
|
|
|
max_len = bcm2835_dma_max_frame_length(c);
|
2016-03-17 03:25:01 +08:00
|
|
|
for_each_sg(sgl, sgent, sg_len, i) {
|
|
|
|
for (addr = sg_dma_address(sgent), len = sg_dma_len(sgent);
|
|
|
|
len > 0;
|
|
|
|
addr += cb->cb->length, len -= cb->cb->length, cb++) {
|
|
|
|
if (direction == DMA_DEV_TO_MEM)
|
|
|
|
cb->cb->dst = addr;
|
|
|
|
else
|
|
|
|
cb->cb->src = addr;
|
|
|
|
cb->cb->length = min(len, max_len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-23 16:26:00 +08:00
|
|
|
static void bcm2835_dma_abort(struct bcm2835_chan *c)
|
2014-01-07 03:18:24 +08:00
|
|
|
{
|
dmaengine: bcm2835: Fix abort of transactions
There are multiple issues with bcm2835_dma_abort() (which is called on
termination of a transaction):
* The algorithm to abort the transaction first pauses the channel by
clearing the ACTIVE flag in the CS register, then waits for the PAUSED
flag to clear. Page 49 of the spec documents the latter as follows:
"Indicates if the DMA is currently paused and not transferring data.
This will occur if the active bit has been cleared [...]"
https://www.raspberrypi.org/app/uploads/2012/02/BCM2835-ARM-Peripherals.pdf
So the function is entering an infinite loop because it is waiting for
PAUSED to clear which is always set due to the function having cleared
the ACTIVE flag. The only thing that's saving it from itself is the
upper bound of 10000 loop iterations.
The code comment says that the intention is to "wait for any current
AXI transfer to complete", so the author probably wanted to check the
WAITING_FOR_OUTSTANDING_WRITES flag instead. Amend the function
accordingly.
* The CS register is only read at the beginning of the function. It
needs to be read again after pausing the channel and before checking
for outstanding writes, otherwise writes which were issued between
the register read at the beginning of the function and pausing the
channel may not be waited for.
* The function seeks to abort the transfer by writing 0 to the NEXTCONBK
register and setting the ABORT and ACTIVE flags. Thereby, the 0 in
NEXTCONBK is sought to be loaded into the CONBLK_AD register. However
experimentation has shown this approach to not work: The CONBLK_AD
register remains the same as before and the CS register contains
0x00000030 (PAUSED | DREQ_STOPS_DMA). In other words, the control
block is not aborted but merely paused and it will be resumed once the
next DMA transaction is started. That is absolutely not the desired
behavior.
A simpler approach is to set the channel's RESET flag instead. This
reliably zeroes the NEXTCONBK as well as the CS register. It requires
less code and only a single MMIO write. This is also what popular
user space DMA drivers do, e.g.:
https://github.com/metachris/RPIO/blob/master/source/c_pwm/pwm.c
Note that the spec is contradictory whether the NEXTCONBK register
is writeable at all. On the one hand, page 41 claims:
"The value loaded into the NEXTCONBK register can be overwritten so
that the linked list of Control Block data structures can be
dynamically altered. However it is only safe to do this when the DMA
is paused."
On the other hand, page 40 specifies:
"Only three registers in each channel's register set are directly
writeable (CS, CONBLK_AD and DEBUG). The other registers (TI,
SOURCE_AD, DEST_AD, TXFR_LEN, STRIDE & NEXTCONBK), are automatically
loaded from a Control Block data structure held in external memory."
Fixes: 96286b576690 ("dmaengine: Add support for BCM2835")
Signed-off-by: Lukas Wunner <lukas@wunner.de>
Cc: stable@vger.kernel.org # v3.14+
Cc: Frank Pavlic <f.pavlic@kunbus.de>
Cc: Martin Sperl <kernel@martin.sperl.org>
Cc: Florian Meier <florian.meier@koalo.de>
Cc: Clive Messer <clive.m.messer@gmail.com>
Cc: Matthias Reichl <hias@horus.com>
Tested-by: Stefan Wahren <stefan.wahren@i2se.com>
Acked-by: Florian Kauer <florian.kauer@koalo.de>
Signed-off-by: Vinod Koul <vkoul@kernel.org>
2019-01-23 16:26:00 +08:00
|
|
|
void __iomem *chan_base = c->chan_base;
|
2014-01-07 03:18:24 +08:00
|
|
|
long int timeout = 10000;
|
|
|
|
|
dmaengine: bcm2835: Fix interrupt race on RT
If IRQ handlers are threaded (either because CONFIG_PREEMPT_RT_BASE is
enabled or "threadirqs" was passed on the command line) and if system
load is sufficiently high that wakeup latency of IRQ threads degrades,
SPI DMA transactions on the BCM2835 occasionally break like this:
ks8851 spi0.0: SPI transfer timed out
bcm2835-dma 3f007000.dma: DMA transfer could not be terminated
ks8851 spi0.0 eth2: ks8851_rdfifo: spi_sync() failed
The root cause is an assumption made by the DMA driver which is
documented in a code comment in bcm2835_dma_terminate_all():
/*
* Stop DMA activity: we assume the callback will not be called
* after bcm_dma_abort() returns (even if it does, it will see
* c->desc is NULL and exit.)
*/
That assumption falls apart if the IRQ handler bcm2835_dma_callback() is
threaded: A client may terminate a descriptor and issue a new one
before the IRQ handler had a chance to run. In fact the IRQ handler may
miss an *arbitrary* number of descriptors. The result is the following
race condition:
1. A descriptor finishes, its interrupt is deferred to the IRQ thread.
2. A client calls dma_terminate_async() which sets channel->desc = NULL.
3. The client issues a new descriptor. Because channel->desc is NULL,
bcm2835_dma_issue_pending() immediately starts the descriptor.
4. Finally the IRQ thread runs and writes BCM2835_DMA_INT to the CS
register to acknowledge the interrupt. This clears the ACTIVE flag,
so the newly issued descriptor is paused in the middle of the
transaction. Because channel->desc is not NULL, the IRQ thread
finalizes the descriptor and tries to start the next one.
I see two possible solutions: The first is to call synchronize_irq()
in bcm2835_dma_issue_pending() to wait until the IRQ thread has
finished before issuing a new descriptor. The downside of this approach
is unnecessary latency if clients desire rapidly terminating and
re-issuing descriptors and don't have any use for an IRQ callback.
(The SPI TX DMA channel is a case in point.)
A better alternative is to make the IRQ thread recognize that it has
missed descriptors and avoid finalizing the newly issued descriptor.
So first of all, set the ACTIVE flag when acknowledging the interrupt.
This keeps a newly issued descriptor running.
If the descriptor was finished, the channel remains idle despite the
ACTIVE flag being set. However the ACTIVE flag can then no longer be
used to check whether the channel is idle, so instead check whether
the register containing the current control block address is zero
and finalize the current descriptor only if so.
That way, there is no impact on latency and throughput if the client
doesn't care for the interrupt: Only minimal additional overhead is
introduced for non-cyclic descriptors as one further MMIO read is
necessary per interrupt to check for idleness of the channel. Cyclic
descriptors are sped up slightly by removing one MMIO write per
interrupt.
Fixes: 96286b576690 ("dmaengine: Add support for BCM2835")
Signed-off-by: Lukas Wunner <lukas@wunner.de>
Cc: stable@vger.kernel.org # v3.14+
Cc: Frank Pavlic <f.pavlic@kunbus.de>
Cc: Martin Sperl <kernel@martin.sperl.org>
Cc: Florian Meier <florian.meier@koalo.de>
Cc: Clive Messer <clive.m.messer@gmail.com>
Cc: Matthias Reichl <hias@horus.com>
Tested-by: Stefan Wahren <stefan.wahren@i2se.com>
Acked-by: Florian Kauer <florian.kauer@koalo.de>
Signed-off-by: Vinod Koul <vkoul@kernel.org>
2019-01-23 16:26:00 +08:00
|
|
|
/*
|
|
|
|
* A zero control block address means the channel is idle.
|
|
|
|
* (The ACTIVE flag in the CS register is not a reliable indicator.)
|
|
|
|
*/
|
|
|
|
if (!readl(chan_base + BCM2835_DMA_ADDR))
|
2019-01-23 16:26:00 +08:00
|
|
|
return;
|
2014-01-07 03:18:24 +08:00
|
|
|
|
|
|
|
/* Write 0 to the active bit - Pause the DMA */
|
|
|
|
writel(0, chan_base + BCM2835_DMA_CS);
|
|
|
|
|
|
|
|
/* Wait for any current AXI transfer to complete */
|
dmaengine: bcm2835: Fix abort of transactions
There are multiple issues with bcm2835_dma_abort() (which is called on
termination of a transaction):
* The algorithm to abort the transaction first pauses the channel by
clearing the ACTIVE flag in the CS register, then waits for the PAUSED
flag to clear. Page 49 of the spec documents the latter as follows:
"Indicates if the DMA is currently paused and not transferring data.
This will occur if the active bit has been cleared [...]"
https://www.raspberrypi.org/app/uploads/2012/02/BCM2835-ARM-Peripherals.pdf
So the function is entering an infinite loop because it is waiting for
PAUSED to clear which is always set due to the function having cleared
the ACTIVE flag. The only thing that's saving it from itself is the
upper bound of 10000 loop iterations.
The code comment says that the intention is to "wait for any current
AXI transfer to complete", so the author probably wanted to check the
WAITING_FOR_OUTSTANDING_WRITES flag instead. Amend the function
accordingly.
* The CS register is only read at the beginning of the function. It
needs to be read again after pausing the channel and before checking
for outstanding writes, otherwise writes which were issued between
the register read at the beginning of the function and pausing the
channel may not be waited for.
* The function seeks to abort the transfer by writing 0 to the NEXTCONBK
register and setting the ABORT and ACTIVE flags. Thereby, the 0 in
NEXTCONBK is sought to be loaded into the CONBLK_AD register. However
experimentation has shown this approach to not work: The CONBLK_AD
register remains the same as before and the CS register contains
0x00000030 (PAUSED | DREQ_STOPS_DMA). In other words, the control
block is not aborted but merely paused and it will be resumed once the
next DMA transaction is started. That is absolutely not the desired
behavior.
A simpler approach is to set the channel's RESET flag instead. This
reliably zeroes the NEXTCONBK as well as the CS register. It requires
less code and only a single MMIO write. This is also what popular
user space DMA drivers do, e.g.:
https://github.com/metachris/RPIO/blob/master/source/c_pwm/pwm.c
Note that the spec is contradictory whether the NEXTCONBK register
is writeable at all. On the one hand, page 41 claims:
"The value loaded into the NEXTCONBK register can be overwritten so
that the linked list of Control Block data structures can be
dynamically altered. However it is only safe to do this when the DMA
is paused."
On the other hand, page 40 specifies:
"Only three registers in each channel's register set are directly
writeable (CS, CONBLK_AD and DEBUG). The other registers (TI,
SOURCE_AD, DEST_AD, TXFR_LEN, STRIDE & NEXTCONBK), are automatically
loaded from a Control Block data structure held in external memory."
Fixes: 96286b576690 ("dmaengine: Add support for BCM2835")
Signed-off-by: Lukas Wunner <lukas@wunner.de>
Cc: stable@vger.kernel.org # v3.14+
Cc: Frank Pavlic <f.pavlic@kunbus.de>
Cc: Martin Sperl <kernel@martin.sperl.org>
Cc: Florian Meier <florian.meier@koalo.de>
Cc: Clive Messer <clive.m.messer@gmail.com>
Cc: Matthias Reichl <hias@horus.com>
Tested-by: Stefan Wahren <stefan.wahren@i2se.com>
Acked-by: Florian Kauer <florian.kauer@koalo.de>
Signed-off-by: Vinod Koul <vkoul@kernel.org>
2019-01-23 16:26:00 +08:00
|
|
|
while ((readl(chan_base + BCM2835_DMA_CS) &
|
|
|
|
BCM2835_DMA_WAITING_FOR_WRITES) && --timeout)
|
2014-01-07 03:18:24 +08:00
|
|
|
cpu_relax();
|
|
|
|
|
dmaengine: bcm2835: Fix abort of transactions
There are multiple issues with bcm2835_dma_abort() (which is called on
termination of a transaction):
* The algorithm to abort the transaction first pauses the channel by
clearing the ACTIVE flag in the CS register, then waits for the PAUSED
flag to clear. Page 49 of the spec documents the latter as follows:
"Indicates if the DMA is currently paused and not transferring data.
This will occur if the active bit has been cleared [...]"
https://www.raspberrypi.org/app/uploads/2012/02/BCM2835-ARM-Peripherals.pdf
So the function is entering an infinite loop because it is waiting for
PAUSED to clear which is always set due to the function having cleared
the ACTIVE flag. The only thing that's saving it from itself is the
upper bound of 10000 loop iterations.
The code comment says that the intention is to "wait for any current
AXI transfer to complete", so the author probably wanted to check the
WAITING_FOR_OUTSTANDING_WRITES flag instead. Amend the function
accordingly.
* The CS register is only read at the beginning of the function. It
needs to be read again after pausing the channel and before checking
for outstanding writes, otherwise writes which were issued between
the register read at the beginning of the function and pausing the
channel may not be waited for.
* The function seeks to abort the transfer by writing 0 to the NEXTCONBK
register and setting the ABORT and ACTIVE flags. Thereby, the 0 in
NEXTCONBK is sought to be loaded into the CONBLK_AD register. However
experimentation has shown this approach to not work: The CONBLK_AD
register remains the same as before and the CS register contains
0x00000030 (PAUSED | DREQ_STOPS_DMA). In other words, the control
block is not aborted but merely paused and it will be resumed once the
next DMA transaction is started. That is absolutely not the desired
behavior.
A simpler approach is to set the channel's RESET flag instead. This
reliably zeroes the NEXTCONBK as well as the CS register. It requires
less code and only a single MMIO write. This is also what popular
user space DMA drivers do, e.g.:
https://github.com/metachris/RPIO/blob/master/source/c_pwm/pwm.c
Note that the spec is contradictory whether the NEXTCONBK register
is writeable at all. On the one hand, page 41 claims:
"The value loaded into the NEXTCONBK register can be overwritten so
that the linked list of Control Block data structures can be
dynamically altered. However it is only safe to do this when the DMA
is paused."
On the other hand, page 40 specifies:
"Only three registers in each channel's register set are directly
writeable (CS, CONBLK_AD and DEBUG). The other registers (TI,
SOURCE_AD, DEST_AD, TXFR_LEN, STRIDE & NEXTCONBK), are automatically
loaded from a Control Block data structure held in external memory."
Fixes: 96286b576690 ("dmaengine: Add support for BCM2835")
Signed-off-by: Lukas Wunner <lukas@wunner.de>
Cc: stable@vger.kernel.org # v3.14+
Cc: Frank Pavlic <f.pavlic@kunbus.de>
Cc: Martin Sperl <kernel@martin.sperl.org>
Cc: Florian Meier <florian.meier@koalo.de>
Cc: Clive Messer <clive.m.messer@gmail.com>
Cc: Matthias Reichl <hias@horus.com>
Tested-by: Stefan Wahren <stefan.wahren@i2se.com>
Acked-by: Florian Kauer <florian.kauer@koalo.de>
Signed-off-by: Vinod Koul <vkoul@kernel.org>
2019-01-23 16:26:00 +08:00
|
|
|
/* Peripheral might be stuck and fail to signal AXI write responses */
|
2014-01-07 03:18:24 +08:00
|
|
|
if (!timeout)
|
dmaengine: bcm2835: Fix abort of transactions
There are multiple issues with bcm2835_dma_abort() (which is called on
termination of a transaction):
* The algorithm to abort the transaction first pauses the channel by
clearing the ACTIVE flag in the CS register, then waits for the PAUSED
flag to clear. Page 49 of the spec documents the latter as follows:
"Indicates if the DMA is currently paused and not transferring data.
This will occur if the active bit has been cleared [...]"
https://www.raspberrypi.org/app/uploads/2012/02/BCM2835-ARM-Peripherals.pdf
So the function is entering an infinite loop because it is waiting for
PAUSED to clear which is always set due to the function having cleared
the ACTIVE flag. The only thing that's saving it from itself is the
upper bound of 10000 loop iterations.
The code comment says that the intention is to "wait for any current
AXI transfer to complete", so the author probably wanted to check the
WAITING_FOR_OUTSTANDING_WRITES flag instead. Amend the function
accordingly.
* The CS register is only read at the beginning of the function. It
needs to be read again after pausing the channel and before checking
for outstanding writes, otherwise writes which were issued between
the register read at the beginning of the function and pausing the
channel may not be waited for.
* The function seeks to abort the transfer by writing 0 to the NEXTCONBK
register and setting the ABORT and ACTIVE flags. Thereby, the 0 in
NEXTCONBK is sought to be loaded into the CONBLK_AD register. However
experimentation has shown this approach to not work: The CONBLK_AD
register remains the same as before and the CS register contains
0x00000030 (PAUSED | DREQ_STOPS_DMA). In other words, the control
block is not aborted but merely paused and it will be resumed once the
next DMA transaction is started. That is absolutely not the desired
behavior.
A simpler approach is to set the channel's RESET flag instead. This
reliably zeroes the NEXTCONBK as well as the CS register. It requires
less code and only a single MMIO write. This is also what popular
user space DMA drivers do, e.g.:
https://github.com/metachris/RPIO/blob/master/source/c_pwm/pwm.c
Note that the spec is contradictory whether the NEXTCONBK register
is writeable at all. On the one hand, page 41 claims:
"The value loaded into the NEXTCONBK register can be overwritten so
that the linked list of Control Block data structures can be
dynamically altered. However it is only safe to do this when the DMA
is paused."
On the other hand, page 40 specifies:
"Only three registers in each channel's register set are directly
writeable (CS, CONBLK_AD and DEBUG). The other registers (TI,
SOURCE_AD, DEST_AD, TXFR_LEN, STRIDE & NEXTCONBK), are automatically
loaded from a Control Block data structure held in external memory."
Fixes: 96286b576690 ("dmaengine: Add support for BCM2835")
Signed-off-by: Lukas Wunner <lukas@wunner.de>
Cc: stable@vger.kernel.org # v3.14+
Cc: Frank Pavlic <f.pavlic@kunbus.de>
Cc: Martin Sperl <kernel@martin.sperl.org>
Cc: Florian Meier <florian.meier@koalo.de>
Cc: Clive Messer <clive.m.messer@gmail.com>
Cc: Matthias Reichl <hias@horus.com>
Tested-by: Stefan Wahren <stefan.wahren@i2se.com>
Acked-by: Florian Kauer <florian.kauer@koalo.de>
Signed-off-by: Vinod Koul <vkoul@kernel.org>
2019-01-23 16:26:00 +08:00
|
|
|
dev_err(c->vc.chan.device->dev,
|
|
|
|
"failed to complete outstanding writes\n");
|
2014-01-07 03:18:24 +08:00
|
|
|
|
dmaengine: bcm2835: Fix abort of transactions
There are multiple issues with bcm2835_dma_abort() (which is called on
termination of a transaction):
* The algorithm to abort the transaction first pauses the channel by
clearing the ACTIVE flag in the CS register, then waits for the PAUSED
flag to clear. Page 49 of the spec documents the latter as follows:
"Indicates if the DMA is currently paused and not transferring data.
This will occur if the active bit has been cleared [...]"
https://www.raspberrypi.org/app/uploads/2012/02/BCM2835-ARM-Peripherals.pdf
So the function is entering an infinite loop because it is waiting for
PAUSED to clear which is always set due to the function having cleared
the ACTIVE flag. The only thing that's saving it from itself is the
upper bound of 10000 loop iterations.
The code comment says that the intention is to "wait for any current
AXI transfer to complete", so the author probably wanted to check the
WAITING_FOR_OUTSTANDING_WRITES flag instead. Amend the function
accordingly.
* The CS register is only read at the beginning of the function. It
needs to be read again after pausing the channel and before checking
for outstanding writes, otherwise writes which were issued between
the register read at the beginning of the function and pausing the
channel may not be waited for.
* The function seeks to abort the transfer by writing 0 to the NEXTCONBK
register and setting the ABORT and ACTIVE flags. Thereby, the 0 in
NEXTCONBK is sought to be loaded into the CONBLK_AD register. However
experimentation has shown this approach to not work: The CONBLK_AD
register remains the same as before and the CS register contains
0x00000030 (PAUSED | DREQ_STOPS_DMA). In other words, the control
block is not aborted but merely paused and it will be resumed once the
next DMA transaction is started. That is absolutely not the desired
behavior.
A simpler approach is to set the channel's RESET flag instead. This
reliably zeroes the NEXTCONBK as well as the CS register. It requires
less code and only a single MMIO write. This is also what popular
user space DMA drivers do, e.g.:
https://github.com/metachris/RPIO/blob/master/source/c_pwm/pwm.c
Note that the spec is contradictory whether the NEXTCONBK register
is writeable at all. On the one hand, page 41 claims:
"The value loaded into the NEXTCONBK register can be overwritten so
that the linked list of Control Block data structures can be
dynamically altered. However it is only safe to do this when the DMA
is paused."
On the other hand, page 40 specifies:
"Only three registers in each channel's register set are directly
writeable (CS, CONBLK_AD and DEBUG). The other registers (TI,
SOURCE_AD, DEST_AD, TXFR_LEN, STRIDE & NEXTCONBK), are automatically
loaded from a Control Block data structure held in external memory."
Fixes: 96286b576690 ("dmaengine: Add support for BCM2835")
Signed-off-by: Lukas Wunner <lukas@wunner.de>
Cc: stable@vger.kernel.org # v3.14+
Cc: Frank Pavlic <f.pavlic@kunbus.de>
Cc: Martin Sperl <kernel@martin.sperl.org>
Cc: Florian Meier <florian.meier@koalo.de>
Cc: Clive Messer <clive.m.messer@gmail.com>
Cc: Matthias Reichl <hias@horus.com>
Tested-by: Stefan Wahren <stefan.wahren@i2se.com>
Acked-by: Florian Kauer <florian.kauer@koalo.de>
Signed-off-by: Vinod Koul <vkoul@kernel.org>
2019-01-23 16:26:00 +08:00
|
|
|
writel(BCM2835_DMA_RESET, chan_base + BCM2835_DMA_CS);
|
2014-01-07 03:18:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void bcm2835_dma_start_desc(struct bcm2835_chan *c)
|
|
|
|
{
|
|
|
|
struct virt_dma_desc *vd = vchan_next_desc(&c->vc);
|
|
|
|
struct bcm2835_desc *d;
|
|
|
|
|
|
|
|
if (!vd) {
|
|
|
|
c->desc = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
list_del(&vd->node);
|
|
|
|
|
|
|
|
c->desc = d = to_bcm2835_dma_desc(&vd->tx);
|
|
|
|
|
2015-11-16 19:09:03 +08:00
|
|
|
writel(d->cb_list[0].paddr, c->chan_base + BCM2835_DMA_ADDR);
|
2014-01-07 03:18:24 +08:00
|
|
|
writel(BCM2835_DMA_ACTIVE, c->chan_base + BCM2835_DMA_CS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static irqreturn_t bcm2835_dma_callback(int irq, void *data)
|
|
|
|
{
|
|
|
|
struct bcm2835_chan *c = data;
|
|
|
|
struct bcm2835_desc *d;
|
|
|
|
unsigned long flags;
|
|
|
|
|
2016-04-11 21:29:08 +08:00
|
|
|
/* check the shared interrupt */
|
|
|
|
if (c->irq_flags & IRQF_SHARED) {
|
|
|
|
/* check if the interrupt is enabled */
|
|
|
|
flags = readl(c->chan_base + BCM2835_DMA_CS);
|
|
|
|
/* if not set then we are not the reason for the irq */
|
|
|
|
if (!(flags & BCM2835_DMA_INT))
|
|
|
|
return IRQ_NONE;
|
|
|
|
}
|
|
|
|
|
2014-01-07 03:18:24 +08:00
|
|
|
spin_lock_irqsave(&c->vc.lock, flags);
|
|
|
|
|
dmaengine: bcm2835: Fix interrupt race on RT
If IRQ handlers are threaded (either because CONFIG_PREEMPT_RT_BASE is
enabled or "threadirqs" was passed on the command line) and if system
load is sufficiently high that wakeup latency of IRQ threads degrades,
SPI DMA transactions on the BCM2835 occasionally break like this:
ks8851 spi0.0: SPI transfer timed out
bcm2835-dma 3f007000.dma: DMA transfer could not be terminated
ks8851 spi0.0 eth2: ks8851_rdfifo: spi_sync() failed
The root cause is an assumption made by the DMA driver which is
documented in a code comment in bcm2835_dma_terminate_all():
/*
* Stop DMA activity: we assume the callback will not be called
* after bcm_dma_abort() returns (even if it does, it will see
* c->desc is NULL and exit.)
*/
That assumption falls apart if the IRQ handler bcm2835_dma_callback() is
threaded: A client may terminate a descriptor and issue a new one
before the IRQ handler had a chance to run. In fact the IRQ handler may
miss an *arbitrary* number of descriptors. The result is the following
race condition:
1. A descriptor finishes, its interrupt is deferred to the IRQ thread.
2. A client calls dma_terminate_async() which sets channel->desc = NULL.
3. The client issues a new descriptor. Because channel->desc is NULL,
bcm2835_dma_issue_pending() immediately starts the descriptor.
4. Finally the IRQ thread runs and writes BCM2835_DMA_INT to the CS
register to acknowledge the interrupt. This clears the ACTIVE flag,
so the newly issued descriptor is paused in the middle of the
transaction. Because channel->desc is not NULL, the IRQ thread
finalizes the descriptor and tries to start the next one.
I see two possible solutions: The first is to call synchronize_irq()
in bcm2835_dma_issue_pending() to wait until the IRQ thread has
finished before issuing a new descriptor. The downside of this approach
is unnecessary latency if clients desire rapidly terminating and
re-issuing descriptors and don't have any use for an IRQ callback.
(The SPI TX DMA channel is a case in point.)
A better alternative is to make the IRQ thread recognize that it has
missed descriptors and avoid finalizing the newly issued descriptor.
So first of all, set the ACTIVE flag when acknowledging the interrupt.
This keeps a newly issued descriptor running.
If the descriptor was finished, the channel remains idle despite the
ACTIVE flag being set. However the ACTIVE flag can then no longer be
used to check whether the channel is idle, so instead check whether
the register containing the current control block address is zero
and finalize the current descriptor only if so.
That way, there is no impact on latency and throughput if the client
doesn't care for the interrupt: Only minimal additional overhead is
introduced for non-cyclic descriptors as one further MMIO read is
necessary per interrupt to check for idleness of the channel. Cyclic
descriptors are sped up slightly by removing one MMIO write per
interrupt.
Fixes: 96286b576690 ("dmaengine: Add support for BCM2835")
Signed-off-by: Lukas Wunner <lukas@wunner.de>
Cc: stable@vger.kernel.org # v3.14+
Cc: Frank Pavlic <f.pavlic@kunbus.de>
Cc: Martin Sperl <kernel@martin.sperl.org>
Cc: Florian Meier <florian.meier@koalo.de>
Cc: Clive Messer <clive.m.messer@gmail.com>
Cc: Matthias Reichl <hias@horus.com>
Tested-by: Stefan Wahren <stefan.wahren@i2se.com>
Acked-by: Florian Kauer <florian.kauer@koalo.de>
Signed-off-by: Vinod Koul <vkoul@kernel.org>
2019-01-23 16:26:00 +08:00
|
|
|
/*
|
|
|
|
* Clear the INT flag to receive further interrupts. Keep the channel
|
|
|
|
* active in case the descriptor is cyclic or in case the client has
|
|
|
|
* already terminated the descriptor and issued a new one. (May happen
|
|
|
|
* if this IRQ handler is threaded.) If the channel is finished, it
|
|
|
|
* will remain idle despite the ACTIVE flag being set.
|
|
|
|
*/
|
|
|
|
writel(BCM2835_DMA_INT | BCM2835_DMA_ACTIVE,
|
|
|
|
c->chan_base + BCM2835_DMA_CS);
|
2014-01-07 03:18:24 +08:00
|
|
|
|
|
|
|
d = c->desc;
|
|
|
|
|
|
|
|
if (d) {
|
2016-03-17 03:25:01 +08:00
|
|
|
if (d->cyclic) {
|
|
|
|
/* call the cyclic callback */
|
|
|
|
vchan_cyclic_callback(&d->vd);
|
dmaengine: bcm2835: Fix interrupt race on RT
If IRQ handlers are threaded (either because CONFIG_PREEMPT_RT_BASE is
enabled or "threadirqs" was passed on the command line) and if system
load is sufficiently high that wakeup latency of IRQ threads degrades,
SPI DMA transactions on the BCM2835 occasionally break like this:
ks8851 spi0.0: SPI transfer timed out
bcm2835-dma 3f007000.dma: DMA transfer could not be terminated
ks8851 spi0.0 eth2: ks8851_rdfifo: spi_sync() failed
The root cause is an assumption made by the DMA driver which is
documented in a code comment in bcm2835_dma_terminate_all():
/*
* Stop DMA activity: we assume the callback will not be called
* after bcm_dma_abort() returns (even if it does, it will see
* c->desc is NULL and exit.)
*/
That assumption falls apart if the IRQ handler bcm2835_dma_callback() is
threaded: A client may terminate a descriptor and issue a new one
before the IRQ handler had a chance to run. In fact the IRQ handler may
miss an *arbitrary* number of descriptors. The result is the following
race condition:
1. A descriptor finishes, its interrupt is deferred to the IRQ thread.
2. A client calls dma_terminate_async() which sets channel->desc = NULL.
3. The client issues a new descriptor. Because channel->desc is NULL,
bcm2835_dma_issue_pending() immediately starts the descriptor.
4. Finally the IRQ thread runs and writes BCM2835_DMA_INT to the CS
register to acknowledge the interrupt. This clears the ACTIVE flag,
so the newly issued descriptor is paused in the middle of the
transaction. Because channel->desc is not NULL, the IRQ thread
finalizes the descriptor and tries to start the next one.
I see two possible solutions: The first is to call synchronize_irq()
in bcm2835_dma_issue_pending() to wait until the IRQ thread has
finished before issuing a new descriptor. The downside of this approach
is unnecessary latency if clients desire rapidly terminating and
re-issuing descriptors and don't have any use for an IRQ callback.
(The SPI TX DMA channel is a case in point.)
A better alternative is to make the IRQ thread recognize that it has
missed descriptors and avoid finalizing the newly issued descriptor.
So first of all, set the ACTIVE flag when acknowledging the interrupt.
This keeps a newly issued descriptor running.
If the descriptor was finished, the channel remains idle despite the
ACTIVE flag being set. However the ACTIVE flag can then no longer be
used to check whether the channel is idle, so instead check whether
the register containing the current control block address is zero
and finalize the current descriptor only if so.
That way, there is no impact on latency and throughput if the client
doesn't care for the interrupt: Only minimal additional overhead is
introduced for non-cyclic descriptors as one further MMIO read is
necessary per interrupt to check for idleness of the channel. Cyclic
descriptors are sped up slightly by removing one MMIO write per
interrupt.
Fixes: 96286b576690 ("dmaengine: Add support for BCM2835")
Signed-off-by: Lukas Wunner <lukas@wunner.de>
Cc: stable@vger.kernel.org # v3.14+
Cc: Frank Pavlic <f.pavlic@kunbus.de>
Cc: Martin Sperl <kernel@martin.sperl.org>
Cc: Florian Meier <florian.meier@koalo.de>
Cc: Clive Messer <clive.m.messer@gmail.com>
Cc: Matthias Reichl <hias@horus.com>
Tested-by: Stefan Wahren <stefan.wahren@i2se.com>
Acked-by: Florian Kauer <florian.kauer@koalo.de>
Signed-off-by: Vinod Koul <vkoul@kernel.org>
2019-01-23 16:26:00 +08:00
|
|
|
} else if (!readl(c->chan_base + BCM2835_DMA_ADDR)) {
|
2016-03-17 03:25:01 +08:00
|
|
|
vchan_cookie_complete(&c->desc->vd);
|
|
|
|
bcm2835_dma_start_desc(c);
|
|
|
|
}
|
2014-01-07 03:18:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(&c->vc.lock, flags);
|
|
|
|
|
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bcm2835_dma_alloc_chan_resources(struct dma_chan *chan)
|
|
|
|
{
|
|
|
|
struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
|
2015-11-16 19:09:03 +08:00
|
|
|
struct device *dev = c->vc.chan.device->dev;
|
2014-01-07 03:18:24 +08:00
|
|
|
|
2015-11-16 19:09:03 +08:00
|
|
|
dev_dbg(dev, "Allocating DMA channel %d\n", c->ch);
|
|
|
|
|
2019-01-23 16:26:00 +08:00
|
|
|
/*
|
|
|
|
* Control blocks are 256 bit in length and must start at a 256 bit
|
|
|
|
* (32 byte) aligned address (BCM2835 ARM Peripherals, sec. 4.2.1.1).
|
|
|
|
*/
|
2015-11-16 19:09:03 +08:00
|
|
|
c->cb_pool = dma_pool_create(dev_name(dev), dev,
|
2019-01-23 16:26:00 +08:00
|
|
|
sizeof(struct bcm2835_dma_cb), 32, 0);
|
2015-11-16 19:09:03 +08:00
|
|
|
if (!c->cb_pool) {
|
|
|
|
dev_err(dev, "unable to allocate descriptor pool\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2014-01-07 03:18:24 +08:00
|
|
|
|
2016-04-11 21:29:08 +08:00
|
|
|
return request_irq(c->irq_number, bcm2835_dma_callback,
|
|
|
|
c->irq_flags, "DMA IRQ", c);
|
2014-01-07 03:18:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void bcm2835_dma_free_chan_resources(struct dma_chan *chan)
|
|
|
|
{
|
|
|
|
struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
|
|
|
|
|
|
|
|
vchan_free_chan_resources(&c->vc);
|
|
|
|
free_irq(c->irq_number, c);
|
2015-11-16 19:09:03 +08:00
|
|
|
dma_pool_destroy(c->cb_pool);
|
2014-01-07 03:18:24 +08:00
|
|
|
|
|
|
|
dev_dbg(c->vc.chan.device->dev, "Freeing DMA channel %u\n", c->ch);
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t bcm2835_dma_desc_size(struct bcm2835_desc *d)
|
|
|
|
{
|
|
|
|
return d->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t bcm2835_dma_desc_size_pos(struct bcm2835_desc *d, dma_addr_t addr)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
for (size = i = 0; i < d->frames; i++) {
|
2015-11-16 19:09:03 +08:00
|
|
|
struct bcm2835_dma_cb *control_block = d->cb_list[i].cb;
|
2014-01-07 03:18:24 +08:00
|
|
|
size_t this_size = control_block->length;
|
|
|
|
dma_addr_t dma;
|
|
|
|
|
|
|
|
if (d->dir == DMA_DEV_TO_MEM)
|
|
|
|
dma = control_block->dst;
|
|
|
|
else
|
|
|
|
dma = control_block->src;
|
|
|
|
|
|
|
|
if (size)
|
|
|
|
size += this_size;
|
|
|
|
else if (addr >= dma && addr < dma + this_size)
|
|
|
|
size += dma + this_size - addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static enum dma_status bcm2835_dma_tx_status(struct dma_chan *chan,
|
|
|
|
dma_cookie_t cookie, struct dma_tx_state *txstate)
|
|
|
|
{
|
|
|
|
struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
|
|
|
|
struct virt_dma_desc *vd;
|
|
|
|
enum dma_status ret;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
ret = dma_cookie_status(chan, cookie, txstate);
|
|
|
|
if (ret == DMA_COMPLETE || !txstate)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&c->vc.lock, flags);
|
|
|
|
vd = vchan_find_desc(&c->vc, cookie);
|
|
|
|
if (vd) {
|
|
|
|
txstate->residue =
|
|
|
|
bcm2835_dma_desc_size(to_bcm2835_dma_desc(&vd->tx));
|
|
|
|
} else if (c->desc && c->desc->vd.tx.cookie == cookie) {
|
|
|
|
struct bcm2835_desc *d = c->desc;
|
|
|
|
dma_addr_t pos;
|
|
|
|
|
|
|
|
if (d->dir == DMA_MEM_TO_DEV)
|
|
|
|
pos = readl(c->chan_base + BCM2835_DMA_SOURCE_AD);
|
|
|
|
else if (d->dir == DMA_DEV_TO_MEM)
|
|
|
|
pos = readl(c->chan_base + BCM2835_DMA_DEST_AD);
|
|
|
|
else
|
|
|
|
pos = 0;
|
|
|
|
|
|
|
|
txstate->residue = bcm2835_dma_desc_size_pos(d, pos);
|
|
|
|
} else {
|
|
|
|
txstate->residue = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(&c->vc.lock, flags);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bcm2835_dma_issue_pending(struct dma_chan *chan)
|
|
|
|
{
|
|
|
|
struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&c->vc.lock, flags);
|
|
|
|
if (vchan_issue_pending(&c->vc) && !c->desc)
|
|
|
|
bcm2835_dma_start_desc(c);
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(&c->vc.lock, flags);
|
|
|
|
}
|
|
|
|
|
2016-06-08 00:14:56 +08:00
|
|
|
static struct dma_async_tx_descriptor *bcm2835_dma_prep_dma_memcpy(
|
2016-03-17 03:25:02 +08:00
|
|
|
struct dma_chan *chan, dma_addr_t dst, dma_addr_t src,
|
|
|
|
size_t len, unsigned long flags)
|
|
|
|
{
|
|
|
|
struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
|
|
|
|
struct bcm2835_desc *d;
|
|
|
|
u32 info = BCM2835_DMA_D_INC | BCM2835_DMA_S_INC;
|
|
|
|
u32 extra = BCM2835_DMA_INT_EN | BCM2835_DMA_WAIT_RESP;
|
|
|
|
size_t max_len = bcm2835_dma_max_frame_length(c);
|
|
|
|
size_t frames;
|
|
|
|
|
|
|
|
/* if src, dst or len is not given return with an error */
|
|
|
|
if (!src || !dst || !len)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* calculate number of frames */
|
|
|
|
frames = bcm2835_dma_frames_for_length(len, max_len);
|
|
|
|
|
|
|
|
/* allocate the CB chain - this also fills in the pointers */
|
|
|
|
d = bcm2835_dma_create_cb_chain(chan, DMA_MEM_TO_MEM, false,
|
|
|
|
info, extra, frames,
|
|
|
|
src, dst, len, 0, GFP_KERNEL);
|
|
|
|
if (!d)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return vchan_tx_prep(&c->vc, &d->vd, flags);
|
|
|
|
}
|
|
|
|
|
2016-03-17 03:25:01 +08:00
|
|
|
static struct dma_async_tx_descriptor *bcm2835_dma_prep_slave_sg(
|
|
|
|
struct dma_chan *chan,
|
|
|
|
struct scatterlist *sgl, unsigned int sg_len,
|
|
|
|
enum dma_transfer_direction direction,
|
|
|
|
unsigned long flags, void *context)
|
|
|
|
{
|
|
|
|
struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
|
|
|
|
struct bcm2835_desc *d;
|
|
|
|
dma_addr_t src = 0, dst = 0;
|
|
|
|
u32 info = BCM2835_DMA_WAIT_RESP;
|
|
|
|
u32 extra = BCM2835_DMA_INT_EN;
|
|
|
|
size_t frames;
|
|
|
|
|
|
|
|
if (!is_slave_direction(direction)) {
|
|
|
|
dev_err(chan->device->dev,
|
|
|
|
"%s: bad direction?\n", __func__);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c->dreq != 0)
|
|
|
|
info |= BCM2835_DMA_PER_MAP(c->dreq);
|
|
|
|
|
|
|
|
if (direction == DMA_DEV_TO_MEM) {
|
|
|
|
if (c->cfg.src_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES)
|
|
|
|
return NULL;
|
|
|
|
src = c->cfg.src_addr;
|
|
|
|
info |= BCM2835_DMA_S_DREQ | BCM2835_DMA_D_INC;
|
|
|
|
} else {
|
|
|
|
if (c->cfg.dst_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES)
|
|
|
|
return NULL;
|
|
|
|
dst = c->cfg.dst_addr;
|
|
|
|
info |= BCM2835_DMA_D_DREQ | BCM2835_DMA_S_INC;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* count frames in sg list */
|
|
|
|
frames = bcm2835_dma_count_frames_for_sg(c, sgl, sg_len);
|
|
|
|
|
|
|
|
/* allocate the CB chain */
|
|
|
|
d = bcm2835_dma_create_cb_chain(chan, direction, false,
|
|
|
|
info, extra,
|
|
|
|
frames, src, dst, 0, 0,
|
2019-04-02 02:38:19 +08:00
|
|
|
GFP_NOWAIT);
|
2016-03-17 03:25:01 +08:00
|
|
|
if (!d)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* fill in frames with scatterlist pointers */
|
|
|
|
bcm2835_dma_fill_cb_chain_with_sg(chan, direction, d->cb_list,
|
|
|
|
sgl, sg_len);
|
|
|
|
|
|
|
|
return vchan_tx_prep(&c->vc, &d->vd, flags);
|
|
|
|
}
|
|
|
|
|
2014-01-07 03:18:24 +08:00
|
|
|
static struct dma_async_tx_descriptor *bcm2835_dma_prep_dma_cyclic(
|
|
|
|
struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
|
|
|
|
size_t period_len, enum dma_transfer_direction direction,
|
2014-08-01 18:20:10 +08:00
|
|
|
unsigned long flags)
|
2014-01-07 03:18:24 +08:00
|
|
|
{
|
2019-09-11 18:15:30 +08:00
|
|
|
struct bcm2835_dmadev *od = to_bcm2835_dma_dev(chan->device);
|
2014-01-07 03:18:24 +08:00
|
|
|
struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
|
|
|
|
struct bcm2835_desc *d;
|
2016-03-17 03:24:59 +08:00
|
|
|
dma_addr_t src, dst;
|
|
|
|
u32 info = BCM2835_DMA_WAIT_RESP;
|
2019-09-11 18:15:30 +08:00
|
|
|
u32 extra = 0;
|
2016-03-17 03:25:00 +08:00
|
|
|
size_t max_len = bcm2835_dma_max_frame_length(c);
|
2016-03-17 03:24:59 +08:00
|
|
|
size_t frames;
|
2014-01-07 03:18:24 +08:00
|
|
|
|
|
|
|
/* Grab configuration */
|
|
|
|
if (!is_slave_direction(direction)) {
|
|
|
|
dev_err(chan->device->dev, "%s: bad direction?\n", __func__);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-03-17 03:24:59 +08:00
|
|
|
if (!buf_len) {
|
|
|
|
dev_err(chan->device->dev,
|
|
|
|
"%s: bad buffer length (= 0)\n", __func__);
|
2014-01-07 03:18:24 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-09-11 18:15:30 +08:00
|
|
|
if (flags & DMA_PREP_INTERRUPT)
|
|
|
|
extra |= BCM2835_DMA_INT_EN;
|
|
|
|
else
|
|
|
|
period_len = buf_len;
|
|
|
|
|
2016-03-17 03:24:59 +08:00
|
|
|
/*
|
|
|
|
* warn if buf_len is not a multiple of period_len - this may leed
|
|
|
|
* to unexpected latencies for interrupts and thus audiable clicks
|
|
|
|
*/
|
|
|
|
if (buf_len % period_len)
|
|
|
|
dev_warn_once(chan->device->dev,
|
|
|
|
"%s: buffer_length (%zd) is not a multiple of period_len (%zd)\n",
|
|
|
|
__func__, buf_len, period_len);
|
2014-01-07 03:18:24 +08:00
|
|
|
|
2016-03-17 03:24:59 +08:00
|
|
|
/* Setup DREQ channel */
|
|
|
|
if (c->dreq != 0)
|
|
|
|
info |= BCM2835_DMA_PER_MAP(c->dreq);
|
2014-01-07 03:18:24 +08:00
|
|
|
|
2016-03-17 03:24:59 +08:00
|
|
|
if (direction == DMA_DEV_TO_MEM) {
|
|
|
|
if (c->cfg.src_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES)
|
|
|
|
return NULL;
|
|
|
|
src = c->cfg.src_addr;
|
|
|
|
dst = buf_addr;
|
|
|
|
info |= BCM2835_DMA_S_DREQ | BCM2835_DMA_D_INC;
|
|
|
|
} else {
|
|
|
|
if (c->cfg.dst_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES)
|
|
|
|
return NULL;
|
|
|
|
dst = c->cfg.dst_addr;
|
|
|
|
src = buf_addr;
|
|
|
|
info |= BCM2835_DMA_D_DREQ | BCM2835_DMA_S_INC;
|
2019-09-11 18:15:30 +08:00
|
|
|
|
|
|
|
/* non-lite channels can write zeroes w/o accessing memory */
|
|
|
|
if (buf_addr == od->zero_page && !c->is_lite_channel)
|
|
|
|
info |= BCM2835_DMA_S_IGNORE;
|
2014-01-07 03:18:24 +08:00
|
|
|
}
|
2015-11-16 19:09:03 +08:00
|
|
|
|
2016-03-17 03:24:59 +08:00
|
|
|
/* calculate number of frames */
|
2016-03-17 03:25:00 +08:00
|
|
|
frames = /* number of periods */
|
|
|
|
DIV_ROUND_UP(buf_len, period_len) *
|
|
|
|
/* number of frames per period */
|
|
|
|
bcm2835_dma_frames_for_length(period_len, max_len);
|
2014-01-07 03:18:24 +08:00
|
|
|
|
|
|
|
/*
|
2016-03-17 03:24:59 +08:00
|
|
|
* allocate the CB chain
|
|
|
|
* note that we need to use GFP_NOWAIT, as the ALSA i2s dmaengine
|
|
|
|
* implementation calls prep_dma_cyclic with interrupts disabled.
|
2014-01-07 03:18:24 +08:00
|
|
|
*/
|
2016-03-17 03:24:59 +08:00
|
|
|
d = bcm2835_dma_create_cb_chain(chan, direction, true,
|
|
|
|
info, extra,
|
|
|
|
frames, src, dst, buf_len,
|
|
|
|
period_len, GFP_NOWAIT);
|
|
|
|
if (!d)
|
|
|
|
return NULL;
|
2014-01-07 03:18:24 +08:00
|
|
|
|
2016-03-17 03:24:59 +08:00
|
|
|
/* wrap around into a loop */
|
|
|
|
d->cb_list[d->frames - 1].cb->next = d->cb_list[0].paddr;
|
2014-01-07 03:18:24 +08:00
|
|
|
|
|
|
|
return vchan_tx_prep(&c->vc, &d->vd, flags);
|
|
|
|
}
|
|
|
|
|
2014-11-17 21:42:08 +08:00
|
|
|
static int bcm2835_dma_slave_config(struct dma_chan *chan,
|
|
|
|
struct dma_slave_config *cfg)
|
2014-01-07 03:18:24 +08:00
|
|
|
{
|
2014-11-17 21:42:08 +08:00
|
|
|
struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
|
|
|
|
|
2014-01-07 03:18:24 +08:00
|
|
|
c->cfg = *cfg;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-17 21:42:08 +08:00
|
|
|
static int bcm2835_dma_terminate_all(struct dma_chan *chan)
|
2014-01-07 03:18:24 +08:00
|
|
|
{
|
2014-11-17 21:42:08 +08:00
|
|
|
struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
|
2014-01-07 03:18:24 +08:00
|
|
|
unsigned long flags;
|
|
|
|
LIST_HEAD(head);
|
|
|
|
|
|
|
|
spin_lock_irqsave(&c->vc.lock, flags);
|
|
|
|
|
dmaengine: bcm2835: Fix interrupt race on RT
If IRQ handlers are threaded (either because CONFIG_PREEMPT_RT_BASE is
enabled or "threadirqs" was passed on the command line) and if system
load is sufficiently high that wakeup latency of IRQ threads degrades,
SPI DMA transactions on the BCM2835 occasionally break like this:
ks8851 spi0.0: SPI transfer timed out
bcm2835-dma 3f007000.dma: DMA transfer could not be terminated
ks8851 spi0.0 eth2: ks8851_rdfifo: spi_sync() failed
The root cause is an assumption made by the DMA driver which is
documented in a code comment in bcm2835_dma_terminate_all():
/*
* Stop DMA activity: we assume the callback will not be called
* after bcm_dma_abort() returns (even if it does, it will see
* c->desc is NULL and exit.)
*/
That assumption falls apart if the IRQ handler bcm2835_dma_callback() is
threaded: A client may terminate a descriptor and issue a new one
before the IRQ handler had a chance to run. In fact the IRQ handler may
miss an *arbitrary* number of descriptors. The result is the following
race condition:
1. A descriptor finishes, its interrupt is deferred to the IRQ thread.
2. A client calls dma_terminate_async() which sets channel->desc = NULL.
3. The client issues a new descriptor. Because channel->desc is NULL,
bcm2835_dma_issue_pending() immediately starts the descriptor.
4. Finally the IRQ thread runs and writes BCM2835_DMA_INT to the CS
register to acknowledge the interrupt. This clears the ACTIVE flag,
so the newly issued descriptor is paused in the middle of the
transaction. Because channel->desc is not NULL, the IRQ thread
finalizes the descriptor and tries to start the next one.
I see two possible solutions: The first is to call synchronize_irq()
in bcm2835_dma_issue_pending() to wait until the IRQ thread has
finished before issuing a new descriptor. The downside of this approach
is unnecessary latency if clients desire rapidly terminating and
re-issuing descriptors and don't have any use for an IRQ callback.
(The SPI TX DMA channel is a case in point.)
A better alternative is to make the IRQ thread recognize that it has
missed descriptors and avoid finalizing the newly issued descriptor.
So first of all, set the ACTIVE flag when acknowledging the interrupt.
This keeps a newly issued descriptor running.
If the descriptor was finished, the channel remains idle despite the
ACTIVE flag being set. However the ACTIVE flag can then no longer be
used to check whether the channel is idle, so instead check whether
the register containing the current control block address is zero
and finalize the current descriptor only if so.
That way, there is no impact on latency and throughput if the client
doesn't care for the interrupt: Only minimal additional overhead is
introduced for non-cyclic descriptors as one further MMIO read is
necessary per interrupt to check for idleness of the channel. Cyclic
descriptors are sped up slightly by removing one MMIO write per
interrupt.
Fixes: 96286b576690 ("dmaengine: Add support for BCM2835")
Signed-off-by: Lukas Wunner <lukas@wunner.de>
Cc: stable@vger.kernel.org # v3.14+
Cc: Frank Pavlic <f.pavlic@kunbus.de>
Cc: Martin Sperl <kernel@martin.sperl.org>
Cc: Florian Meier <florian.meier@koalo.de>
Cc: Clive Messer <clive.m.messer@gmail.com>
Cc: Matthias Reichl <hias@horus.com>
Tested-by: Stefan Wahren <stefan.wahren@i2se.com>
Acked-by: Florian Kauer <florian.kauer@koalo.de>
Signed-off-by: Vinod Koul <vkoul@kernel.org>
2019-01-23 16:26:00 +08:00
|
|
|
/* stop DMA activity */
|
2014-01-07 03:18:24 +08:00
|
|
|
if (c->desc) {
|
2019-09-11 18:15:30 +08:00
|
|
|
if (c->desc->vd.tx.flags & DMA_PREP_INTERRUPT)
|
|
|
|
vchan_terminate_vdesc(&c->desc->vd);
|
|
|
|
else
|
|
|
|
vchan_vdesc_fini(&c->desc->vd);
|
2014-01-07 03:18:24 +08:00
|
|
|
c->desc = NULL;
|
dmaengine: bcm2835: Fix abort of transactions
There are multiple issues with bcm2835_dma_abort() (which is called on
termination of a transaction):
* The algorithm to abort the transaction first pauses the channel by
clearing the ACTIVE flag in the CS register, then waits for the PAUSED
flag to clear. Page 49 of the spec documents the latter as follows:
"Indicates if the DMA is currently paused and not transferring data.
This will occur if the active bit has been cleared [...]"
https://www.raspberrypi.org/app/uploads/2012/02/BCM2835-ARM-Peripherals.pdf
So the function is entering an infinite loop because it is waiting for
PAUSED to clear which is always set due to the function having cleared
the ACTIVE flag. The only thing that's saving it from itself is the
upper bound of 10000 loop iterations.
The code comment says that the intention is to "wait for any current
AXI transfer to complete", so the author probably wanted to check the
WAITING_FOR_OUTSTANDING_WRITES flag instead. Amend the function
accordingly.
* The CS register is only read at the beginning of the function. It
needs to be read again after pausing the channel and before checking
for outstanding writes, otherwise writes which were issued between
the register read at the beginning of the function and pausing the
channel may not be waited for.
* The function seeks to abort the transfer by writing 0 to the NEXTCONBK
register and setting the ABORT and ACTIVE flags. Thereby, the 0 in
NEXTCONBK is sought to be loaded into the CONBLK_AD register. However
experimentation has shown this approach to not work: The CONBLK_AD
register remains the same as before and the CS register contains
0x00000030 (PAUSED | DREQ_STOPS_DMA). In other words, the control
block is not aborted but merely paused and it will be resumed once the
next DMA transaction is started. That is absolutely not the desired
behavior.
A simpler approach is to set the channel's RESET flag instead. This
reliably zeroes the NEXTCONBK as well as the CS register. It requires
less code and only a single MMIO write. This is also what popular
user space DMA drivers do, e.g.:
https://github.com/metachris/RPIO/blob/master/source/c_pwm/pwm.c
Note that the spec is contradictory whether the NEXTCONBK register
is writeable at all. On the one hand, page 41 claims:
"The value loaded into the NEXTCONBK register can be overwritten so
that the linked list of Control Block data structures can be
dynamically altered. However it is only safe to do this when the DMA
is paused."
On the other hand, page 40 specifies:
"Only three registers in each channel's register set are directly
writeable (CS, CONBLK_AD and DEBUG). The other registers (TI,
SOURCE_AD, DEST_AD, TXFR_LEN, STRIDE & NEXTCONBK), are automatically
loaded from a Control Block data structure held in external memory."
Fixes: 96286b576690 ("dmaengine: Add support for BCM2835")
Signed-off-by: Lukas Wunner <lukas@wunner.de>
Cc: stable@vger.kernel.org # v3.14+
Cc: Frank Pavlic <f.pavlic@kunbus.de>
Cc: Martin Sperl <kernel@martin.sperl.org>
Cc: Florian Meier <florian.meier@koalo.de>
Cc: Clive Messer <clive.m.messer@gmail.com>
Cc: Matthias Reichl <hias@horus.com>
Tested-by: Stefan Wahren <stefan.wahren@i2se.com>
Acked-by: Florian Kauer <florian.kauer@koalo.de>
Signed-off-by: Vinod Koul <vkoul@kernel.org>
2019-01-23 16:26:00 +08:00
|
|
|
bcm2835_dma_abort(c);
|
2014-01-07 03:18:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
vchan_get_all_descriptors(&c->vc, &head);
|
|
|
|
spin_unlock_irqrestore(&c->vc.lock, flags);
|
|
|
|
vchan_dma_desc_free_list(&c->vc, &head);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-11-14 22:32:07 +08:00
|
|
|
static void bcm2835_dma_synchronize(struct dma_chan *chan)
|
|
|
|
{
|
|
|
|
struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
|
|
|
|
|
|
|
|
vchan_synchronize(&c->vc);
|
|
|
|
}
|
|
|
|
|
2016-04-11 21:29:08 +08:00
|
|
|
static int bcm2835_dma_chan_init(struct bcm2835_dmadev *d, int chan_id,
|
|
|
|
int irq, unsigned int irq_flags)
|
2014-01-07 03:18:24 +08:00
|
|
|
{
|
|
|
|
struct bcm2835_chan *c;
|
|
|
|
|
|
|
|
c = devm_kzalloc(d->ddev.dev, sizeof(*c), GFP_KERNEL);
|
|
|
|
if (!c)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
c->vc.desc_free = bcm2835_dma_desc_free;
|
|
|
|
vchan_init(&c->vc, &d->ddev);
|
|
|
|
|
|
|
|
c->chan_base = BCM2835_DMA_CHANIO(d->base, chan_id);
|
|
|
|
c->ch = chan_id;
|
|
|
|
c->irq_number = irq;
|
2016-04-11 21:29:08 +08:00
|
|
|
c->irq_flags = irq_flags;
|
2014-01-07 03:18:24 +08:00
|
|
|
|
2016-03-17 03:25:00 +08:00
|
|
|
/* check in DEBUG register if this is a LITE channel */
|
|
|
|
if (readl(c->chan_base + BCM2835_DMA_DEBUG) &
|
|
|
|
BCM2835_DMA_DEBUG_LITE)
|
|
|
|
c->is_lite_channel = true;
|
|
|
|
|
2014-01-07 03:18:24 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bcm2835_dma_free(struct bcm2835_dmadev *od)
|
|
|
|
{
|
|
|
|
struct bcm2835_chan *c, *next;
|
|
|
|
|
|
|
|
list_for_each_entry_safe(c, next, &od->ddev.channels,
|
|
|
|
vc.chan.device_node) {
|
|
|
|
list_del(&c->vc.chan.device_node);
|
|
|
|
tasklet_kill(&c->vc.task);
|
|
|
|
}
|
2019-09-11 18:15:30 +08:00
|
|
|
|
|
|
|
dma_unmap_page_attrs(od->ddev.dev, od->zero_page, PAGE_SIZE,
|
|
|
|
DMA_TO_DEVICE, DMA_ATTR_SKIP_CPU_SYNC);
|
2014-01-07 03:18:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct of_device_id bcm2835_dma_of_match[] = {
|
|
|
|
{ .compatible = "brcm,bcm2835-dma", },
|
|
|
|
{},
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, bcm2835_dma_of_match);
|
|
|
|
|
|
|
|
static struct dma_chan *bcm2835_dma_xlate(struct of_phandle_args *spec,
|
|
|
|
struct of_dma *ofdma)
|
|
|
|
{
|
|
|
|
struct bcm2835_dmadev *d = ofdma->of_dma_data;
|
|
|
|
struct dma_chan *chan;
|
|
|
|
|
|
|
|
chan = dma_get_any_slave_channel(&d->ddev);
|
|
|
|
if (!chan)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Set DREQ from param */
|
|
|
|
to_bcm2835_dma_chan(chan)->dreq = spec->args[0];
|
|
|
|
|
|
|
|
return chan;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bcm2835_dma_probe(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct bcm2835_dmadev *od;
|
|
|
|
struct resource *res;
|
|
|
|
void __iomem *base;
|
|
|
|
int rc;
|
2016-04-11 21:29:08 +08:00
|
|
|
int i, j;
|
|
|
|
int irq[BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED + 1];
|
|
|
|
int irq_flags;
|
2014-01-07 03:18:24 +08:00
|
|
|
uint32_t chans_available;
|
2016-04-11 21:29:08 +08:00
|
|
|
char chan_name[BCM2835_DMA_CHAN_NAME_SIZE];
|
2014-01-07 03:18:24 +08:00
|
|
|
|
|
|
|
if (!pdev->dev.dma_mask)
|
|
|
|
pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
|
|
|
|
|
|
|
|
rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
|
2019-07-17 01:15:18 +08:00
|
|
|
if (rc) {
|
|
|
|
dev_err(&pdev->dev, "Unable to set DMA mask\n");
|
2014-01-07 03:18:24 +08:00
|
|
|
return rc;
|
2019-07-17 01:15:18 +08:00
|
|
|
}
|
2014-01-07 03:18:24 +08:00
|
|
|
|
|
|
|
od = devm_kzalloc(&pdev->dev, sizeof(*od), GFP_KERNEL);
|
|
|
|
if (!od)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
pdev->dev.dma_parms = &od->dma_parms;
|
|
|
|
dma_set_max_seg_size(&pdev->dev, 0x3FFFFFFF);
|
|
|
|
|
|
|
|
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
|
|
|
|
base = devm_ioremap_resource(&pdev->dev, res);
|
|
|
|
if (IS_ERR(base))
|
|
|
|
return PTR_ERR(base);
|
|
|
|
|
|
|
|
od->base = base;
|
|
|
|
|
|
|
|
dma_cap_set(DMA_SLAVE, od->ddev.cap_mask);
|
2014-01-18 01:06:29 +08:00
|
|
|
dma_cap_set(DMA_PRIVATE, od->ddev.cap_mask);
|
2014-01-07 03:18:24 +08:00
|
|
|
dma_cap_set(DMA_CYCLIC, od->ddev.cap_mask);
|
2016-03-17 03:25:02 +08:00
|
|
|
dma_cap_set(DMA_MEMCPY, od->ddev.cap_mask);
|
2014-01-07 03:18:24 +08:00
|
|
|
od->ddev.device_alloc_chan_resources = bcm2835_dma_alloc_chan_resources;
|
|
|
|
od->ddev.device_free_chan_resources = bcm2835_dma_free_chan_resources;
|
|
|
|
od->ddev.device_tx_status = bcm2835_dma_tx_status;
|
|
|
|
od->ddev.device_issue_pending = bcm2835_dma_issue_pending;
|
|
|
|
od->ddev.device_prep_dma_cyclic = bcm2835_dma_prep_dma_cyclic;
|
2016-03-17 03:25:01 +08:00
|
|
|
od->ddev.device_prep_slave_sg = bcm2835_dma_prep_slave_sg;
|
2016-03-17 03:25:02 +08:00
|
|
|
od->ddev.device_prep_dma_memcpy = bcm2835_dma_prep_dma_memcpy;
|
2014-11-17 21:42:08 +08:00
|
|
|
od->ddev.device_config = bcm2835_dma_slave_config;
|
|
|
|
od->ddev.device_terminate_all = bcm2835_dma_terminate_all;
|
2017-11-14 22:32:07 +08:00
|
|
|
od->ddev.device_synchronize = bcm2835_dma_synchronize;
|
2014-11-17 21:42:45 +08:00
|
|
|
od->ddev.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
|
|
|
|
od->ddev.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
|
2016-03-17 03:25:02 +08:00
|
|
|
od->ddev.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV) |
|
|
|
|
BIT(DMA_MEM_TO_MEM);
|
2016-03-17 03:24:55 +08:00
|
|
|
od->ddev.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
|
2019-09-11 18:15:30 +08:00
|
|
|
od->ddev.descriptor_reuse = true;
|
2014-01-07 03:18:24 +08:00
|
|
|
od->ddev.dev = &pdev->dev;
|
|
|
|
INIT_LIST_HEAD(&od->ddev.channels);
|
|
|
|
|
|
|
|
platform_set_drvdata(pdev, od);
|
|
|
|
|
2019-09-11 18:15:30 +08:00
|
|
|
od->zero_page = dma_map_page_attrs(od->ddev.dev, ZERO_PAGE(0), 0,
|
|
|
|
PAGE_SIZE, DMA_TO_DEVICE,
|
|
|
|
DMA_ATTR_SKIP_CPU_SYNC);
|
|
|
|
if (dma_mapping_error(od->ddev.dev, od->zero_page)) {
|
|
|
|
dev_err(&pdev->dev, "Failed to map zero page\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2014-01-07 03:18:24 +08:00
|
|
|
/* Request DMA channel mask from device tree */
|
|
|
|
if (of_property_read_u32(pdev->dev.of_node,
|
|
|
|
"brcm,dma-channel-mask",
|
|
|
|
&chans_available)) {
|
|
|
|
dev_err(&pdev->dev, "Failed to get channel mask\n");
|
|
|
|
rc = -EINVAL;
|
|
|
|
goto err_no_dma;
|
|
|
|
}
|
|
|
|
|
2016-04-11 21:29:08 +08:00
|
|
|
/* get irqs for each channel that we support */
|
|
|
|
for (i = 0; i <= BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED; i++) {
|
|
|
|
/* skip masked out channels */
|
|
|
|
if (!(chans_available & (1 << i))) {
|
|
|
|
irq[i] = -1;
|
|
|
|
continue;
|
2014-01-07 03:18:24 +08:00
|
|
|
}
|
2016-04-11 21:29:08 +08:00
|
|
|
|
|
|
|
/* get the named irq */
|
|
|
|
snprintf(chan_name, sizeof(chan_name), "dma%i", i);
|
|
|
|
irq[i] = platform_get_irq_byname(pdev, chan_name);
|
|
|
|
if (irq[i] >= 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* legacy device tree case handling */
|
|
|
|
dev_warn_once(&pdev->dev,
|
2016-04-22 15:12:48 +08:00
|
|
|
"missing interrupt-names property in device tree - legacy interpretation is used\n");
|
2016-04-11 21:29:08 +08:00
|
|
|
/*
|
|
|
|
* in case of channel >= 11
|
|
|
|
* use the 11th interrupt and that is shared
|
|
|
|
*/
|
|
|
|
irq[i] = platform_get_irq(pdev, i < 11 ? i : 11);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get irqs for each channel */
|
|
|
|
for (i = 0; i <= BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED; i++) {
|
|
|
|
/* skip channels without irq */
|
|
|
|
if (irq[i] < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* check if there are other channels that also use this irq */
|
|
|
|
irq_flags = 0;
|
|
|
|
for (j = 0; j <= BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED; j++)
|
|
|
|
if ((i != j) && (irq[j] == irq[i])) {
|
|
|
|
irq_flags = IRQF_SHARED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* initialize the channel */
|
|
|
|
rc = bcm2835_dma_chan_init(od, i, irq[i], irq_flags);
|
|
|
|
if (rc)
|
|
|
|
goto err_no_dma;
|
2014-01-07 03:18:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
dev_dbg(&pdev->dev, "Initialized %i DMA channels\n", i);
|
|
|
|
|
|
|
|
/* Device-tree DMA controller registration */
|
|
|
|
rc = of_dma_controller_register(pdev->dev.of_node,
|
|
|
|
bcm2835_dma_xlate, od);
|
|
|
|
if (rc) {
|
|
|
|
dev_err(&pdev->dev, "Failed to register DMA controller\n");
|
|
|
|
goto err_no_dma;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = dma_async_device_register(&od->ddev);
|
|
|
|
if (rc) {
|
|
|
|
dev_err(&pdev->dev,
|
|
|
|
"Failed to register slave DMA engine device: %d\n", rc);
|
|
|
|
goto err_no_dma;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev_dbg(&pdev->dev, "Load BCM2835 DMA engine driver\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err_no_dma:
|
|
|
|
bcm2835_dma_free(od);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bcm2835_dma_remove(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct bcm2835_dmadev *od = platform_get_drvdata(pdev);
|
|
|
|
|
|
|
|
dma_async_device_unregister(&od->ddev);
|
|
|
|
bcm2835_dma_free(od);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct platform_driver bcm2835_dma_driver = {
|
|
|
|
.probe = bcm2835_dma_probe,
|
|
|
|
.remove = bcm2835_dma_remove,
|
|
|
|
.driver = {
|
|
|
|
.name = "bcm2835-dma",
|
|
|
|
.of_match_table = of_match_ptr(bcm2835_dma_of_match),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
module_platform_driver(bcm2835_dma_driver);
|
|
|
|
|
|
|
|
MODULE_ALIAS("platform:bcm2835-dma");
|
|
|
|
MODULE_DESCRIPTION("BCM2835 DMA engine driver");
|
|
|
|
MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
|
2018-10-23 19:06:07 +08:00
|
|
|
MODULE_LICENSE("GPL");
|