Commit 40c5520f authored by Andy Walls's avatar Andy Walls Committed by Mauro Carvalho Chehab

V4L/DVB (11618): cx18: Convert per stream mutex locks to per queue spin locks

To avoid sleeps in providing buffers to user space and in handling incoming
buffers from the capture unit, converted the per stream mutex for locking
queues to 3 spin locks.  There is now a spin lock per queue
to increase concurrency when moving buffers around.

Also simplified queue manipulations and buffer handling of incoming buffers
of data from the capture unit.
Signed-off-by: default avatarAndy Walls <awalls@radix.net>
Signed-off-by: default avatarMauro Carvalho Chehab <mchehab@redhat.com>
parent 5f0a3cfc
...@@ -286,6 +286,7 @@ struct cx18_queue { ...@@ -286,6 +286,7 @@ struct cx18_queue {
struct list_head list; struct list_head list;
atomic_t buffers; atomic_t buffers;
u32 bytesused; u32 bytesused;
spinlock_t lock;
}; };
struct cx18_dvb { struct cx18_dvb {
...@@ -365,7 +366,6 @@ struct cx18_stream { ...@@ -365,7 +366,6 @@ struct cx18_stream {
unsigned mdl_offset; unsigned mdl_offset;
u32 id; u32 id;
struct mutex qlock; /* locks access to the queues */
unsigned long s_flags; /* status flags, see above */ unsigned long s_flags; /* status flags, see above */
int dma; /* can be PCI_DMA_TODEVICE, int dma; /* can be PCI_DMA_TODEVICE,
PCI_DMA_FROMDEVICE or PCI_DMA_FROMDEVICE or
......
...@@ -191,23 +191,24 @@ static void epu_dma_done(struct cx18 *cx, struct cx18_in_work_order *order) ...@@ -191,23 +191,24 @@ static void epu_dma_done(struct cx18 *cx, struct cx18_in_work_order *order)
if (buf == NULL) { if (buf == NULL) {
CX18_WARN("Could not find buf %d for stream %s\n", CX18_WARN("Could not find buf %d for stream %s\n",
id, s->name); id, s->name);
/* Put as many buffers as possible back into fw use */
cx18_stream_load_fw_queue(s);
continue; continue;
} }
if (s->type == CX18_ENC_STREAM_TYPE_TS && s->dvb.enabled) { CX18_DEBUG_HI_DMA("%s recv bytesused = %d\n",
CX18_DEBUG_HI_DMA("TS recv bytesused = %d\n", s->name, buf->bytesused);
buf->bytesused);
dvb_dmx_swfilter(&s->dvb.demux, buf->buf, if (s->type != CX18_ENC_STREAM_TYPE_TS)
buf->bytesused); cx18_enqueue(s, buf, &s->q_full);
else {
if (s->dvb.enabled)
dvb_dmx_swfilter(&s->dvb.demux, buf->buf,
buf->bytesused);
cx18_enqueue(s, buf, &s->q_free);
} }
/* Put as many buffers as possible back into fw use */
cx18_stream_load_fw_queue(s);
/* Put back TS buffer, since it was removed from all queues */
if (s->type == CX18_ENC_STREAM_TYPE_TS)
cx18_stream_put_buf_fw(s, buf);
} }
/* Put as many buffers as possible back into fw use */
cx18_stream_load_fw_queue(s);
wake_up(&cx->dma_waitq); wake_up(&cx->dma_waitq);
if (s->id != -1) if (s->id != -1)
wake_up(&s->waitq); wake_up(&s->waitq);
......
...@@ -53,13 +53,13 @@ struct cx18_queue *_cx18_enqueue(struct cx18_stream *s, struct cx18_buffer *buf, ...@@ -53,13 +53,13 @@ struct cx18_queue *_cx18_enqueue(struct cx18_stream *s, struct cx18_buffer *buf,
buf->skipped = 0; buf->skipped = 0;
} }
mutex_lock(&s->qlock);
/* q_busy is restricted to a max buffer count imposed by firmware */ /* q_busy is restricted to a max buffer count imposed by firmware */
if (q == &s->q_busy && if (q == &s->q_busy &&
atomic_read(&q->buffers) >= CX18_MAX_FW_MDLS_PER_STREAM) atomic_read(&q->buffers) >= CX18_MAX_FW_MDLS_PER_STREAM)
q = &s->q_free; q = &s->q_free;
spin_lock(&q->lock);
if (to_front) if (to_front)
list_add(&buf->list, &q->list); /* LIFO */ list_add(&buf->list, &q->list); /* LIFO */
else else
...@@ -67,7 +67,7 @@ struct cx18_queue *_cx18_enqueue(struct cx18_stream *s, struct cx18_buffer *buf, ...@@ -67,7 +67,7 @@ struct cx18_queue *_cx18_enqueue(struct cx18_stream *s, struct cx18_buffer *buf,
q->bytesused += buf->bytesused - buf->readpos; q->bytesused += buf->bytesused - buf->readpos;
atomic_inc(&q->buffers); atomic_inc(&q->buffers);
mutex_unlock(&s->qlock); spin_unlock(&q->lock);
return q; return q;
} }
...@@ -75,7 +75,7 @@ struct cx18_buffer *cx18_dequeue(struct cx18_stream *s, struct cx18_queue *q) ...@@ -75,7 +75,7 @@ struct cx18_buffer *cx18_dequeue(struct cx18_stream *s, struct cx18_queue *q)
{ {
struct cx18_buffer *buf = NULL; struct cx18_buffer *buf = NULL;
mutex_lock(&s->qlock); spin_lock(&q->lock);
if (!list_empty(&q->list)) { if (!list_empty(&q->list)) {
buf = list_first_entry(&q->list, struct cx18_buffer, list); buf = list_first_entry(&q->list, struct cx18_buffer, list);
list_del_init(&buf->list); list_del_init(&buf->list);
...@@ -83,7 +83,7 @@ struct cx18_buffer *cx18_dequeue(struct cx18_stream *s, struct cx18_queue *q) ...@@ -83,7 +83,7 @@ struct cx18_buffer *cx18_dequeue(struct cx18_stream *s, struct cx18_queue *q)
buf->skipped = 0; buf->skipped = 0;
atomic_dec(&q->buffers); atomic_dec(&q->buffers);
} }
mutex_unlock(&s->qlock); spin_unlock(&q->lock);
return buf; return buf;
} }
...@@ -94,9 +94,23 @@ struct cx18_buffer *cx18_queue_get_buf(struct cx18_stream *s, u32 id, ...@@ -94,9 +94,23 @@ struct cx18_buffer *cx18_queue_get_buf(struct cx18_stream *s, u32 id,
struct cx18_buffer *buf; struct cx18_buffer *buf;
struct cx18_buffer *tmp; struct cx18_buffer *tmp;
struct cx18_buffer *ret = NULL; struct cx18_buffer *ret = NULL;
LIST_HEAD(sweep_up);
mutex_lock(&s->qlock);
/*
* We don't have to acquire multiple q locks here, because we are
* serialized by the single threaded work handler.
* Buffers from the firmware will thus remain in order as
* they are moved from q_busy to q_full or to the dvb ring buffer.
*/
spin_lock(&s->q_busy.lock);
list_for_each_entry_safe(buf, tmp, &s->q_busy.list, list) { list_for_each_entry_safe(buf, tmp, &s->q_busy.list, list) {
/*
* We should find what the firmware told us is done,
* right at the front of the queue. If we don't, we likely have
* missed a buffer done message from the firmware.
* Once we skip a buffer repeatedly, relative to the size of
* q_busy, we have high confidence we've missed it.
*/
if (buf->id != id) { if (buf->id != id) {
buf->skipped++; buf->skipped++;
if (buf->skipped >= atomic_read(&s->q_busy.buffers)-1) { if (buf->skipped >= atomic_read(&s->q_busy.buffers)-1) {
...@@ -105,38 +119,41 @@ struct cx18_buffer *cx18_queue_get_buf(struct cx18_stream *s, u32 id, ...@@ -105,38 +119,41 @@ struct cx18_buffer *cx18_queue_get_buf(struct cx18_stream *s, u32 id,
"times - it must have dropped out of " "times - it must have dropped out of "
"rotation\n", s->name, buf->id, "rotation\n", s->name, buf->id,
buf->skipped); buf->skipped);
/* move it to q_free */ /* Sweep it up to put it back into rotation */
list_move_tail(&buf->list, &s->q_free.list); list_move_tail(&buf->list, &sweep_up);
buf->bytesused = buf->readpos = buf->b_flags =
buf->skipped = 0;
atomic_dec(&s->q_busy.buffers); atomic_dec(&s->q_busy.buffers);
atomic_inc(&s->q_free.buffers);
} }
continue; continue;
} }
/*
buf->bytesused = bytesused; * We pull the desired buffer off of the queue here. Something
/* Sync the buffer before we release the qlock */ * will have to put it back on a queue later.
cx18_buf_sync_for_cpu(s, buf); */
if (s->type == CX18_ENC_STREAM_TYPE_TS) { list_del_init(&buf->list);
/*
* TS doesn't use q_full. As we pull the buffer off of
* the queue here, the caller will have to put it back.
*/
list_del_init(&buf->list);
} else {
/* Move buffer from q_busy to q_full */
list_move_tail(&buf->list, &s->q_full.list);
set_bit(CX18_F_B_NEED_BUF_SWAP, &buf->b_flags);
s->q_full.bytesused += buf->bytesused;
atomic_inc(&s->q_full.buffers);
}
atomic_dec(&s->q_busy.buffers); atomic_dec(&s->q_busy.buffers);
ret = buf; ret = buf;
break; break;
} }
mutex_unlock(&s->qlock); spin_unlock(&s->q_busy.lock);
/*
* We found the buffer for which we were looking. Get it ready for
* the caller to put on q_full or in the dvb ring buffer.
*/
if (ret != NULL) {
ret->bytesused = bytesused;
ret->skipped = 0;
/* readpos and b_flags were 0'ed when the buf went on q_busy */
cx18_buf_sync_for_cpu(s, ret);
if (s->type != CX18_ENC_STREAM_TYPE_TS)
set_bit(CX18_F_B_NEED_BUF_SWAP, &ret->b_flags);
}
/* Put any buffers the firmware is ignoring back into normal rotation */
list_for_each_entry_safe(buf, tmp, &sweep_up, list) {
list_del_init(&buf->list);
cx18_enqueue(s, buf, &s->q_free);
}
return ret; return ret;
} }
...@@ -148,7 +165,7 @@ static void cx18_queue_flush(struct cx18_stream *s, struct cx18_queue *q) ...@@ -148,7 +165,7 @@ static void cx18_queue_flush(struct cx18_stream *s, struct cx18_queue *q)
if (q == &s->q_free) if (q == &s->q_free)
return; return;
mutex_lock(&s->qlock); spin_lock(&q->lock);
while (!list_empty(&q->list)) { while (!list_empty(&q->list)) {
buf = list_first_entry(&q->list, struct cx18_buffer, list); buf = list_first_entry(&q->list, struct cx18_buffer, list);
list_move_tail(&buf->list, &s->q_free.list); list_move_tail(&buf->list, &s->q_free.list);
...@@ -156,7 +173,7 @@ static void cx18_queue_flush(struct cx18_stream *s, struct cx18_queue *q) ...@@ -156,7 +173,7 @@ static void cx18_queue_flush(struct cx18_stream *s, struct cx18_queue *q)
atomic_inc(&s->q_free.buffers); atomic_inc(&s->q_free.buffers);
} }
cx18_queue_init(q); cx18_queue_init(q);
mutex_unlock(&s->qlock); spin_unlock(&q->lock);
} }
void cx18_flush_queues(struct cx18_stream *s) void cx18_flush_queues(struct cx18_stream *s)
......
...@@ -116,11 +116,13 @@ static void cx18_stream_init(struct cx18 *cx, int type) ...@@ -116,11 +116,13 @@ static void cx18_stream_init(struct cx18 *cx, int type)
s->buffers = cx->stream_buffers[type]; s->buffers = cx->stream_buffers[type];
s->buf_size = cx->stream_buf_size[type]; s->buf_size = cx->stream_buf_size[type];
mutex_init(&s->qlock);
init_waitqueue_head(&s->waitq); init_waitqueue_head(&s->waitq);
s->id = -1; s->id = -1;
spin_lock_init(&s->q_free.lock);
cx18_queue_init(&s->q_free); cx18_queue_init(&s->q_free);
spin_lock_init(&s->q_busy.lock);
cx18_queue_init(&s->q_busy); cx18_queue_init(&s->q_busy);
spin_lock_init(&s->q_full.lock);
cx18_queue_init(&s->q_full); cx18_queue_init(&s->q_full);
} }
...@@ -685,13 +687,13 @@ int cx18_start_v4l2_encode_stream(struct cx18_stream *s) ...@@ -685,13 +687,13 @@ int cx18_start_v4l2_encode_stream(struct cx18_stream *s)
/* Init all the cpu_mdls for this stream */ /* Init all the cpu_mdls for this stream */
cx18_flush_queues(s); cx18_flush_queues(s);
mutex_lock(&s->qlock); spin_lock(&s->q_free.lock);
list_for_each_entry(buf, &s->q_free.list, list) { list_for_each_entry(buf, &s->q_free.list, list) {
cx18_writel(cx, buf->dma_handle, cx18_writel(cx, buf->dma_handle,
&cx->scb->cpu_mdl[buf->id].paddr); &cx->scb->cpu_mdl[buf->id].paddr);
cx18_writel(cx, s->buf_size, &cx->scb->cpu_mdl[buf->id].length); cx18_writel(cx, s->buf_size, &cx->scb->cpu_mdl[buf->id].length);
} }
mutex_unlock(&s->qlock); spin_unlock(&s->q_free.lock);
_cx18_stream_load_fw_queue(s); _cx18_stream_load_fw_queue(s);
/* begin_capture */ /* begin_capture */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment