V4L/DVB: cx18, cx23885, v4l2 doc, MAINTAINERS: Update Andy Walls' email address
[safe/jmp/linux-2.6] / drivers / media / video / cx18 / cx18-queue.c
index 65af1bb..8884537 100644 (file)
@@ -4,6 +4,7 @@
  *  Derived from ivtv-queue.c
  *
  *  Copyright (C) 2007  Hans Verkuil <hverkuil@xs4all.nl>
+ *  Copyright (C) 2008  Andy Walls <awalls@md.metrocast.net>
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  */
 
 #include "cx18-driver.h"
-#include "cx18-streams.h"
 #include "cx18-queue.h"
+#include "cx18-streams.h"
 #include "cx18-scb.h"
-
-int cx18_buf_copy_from_user(struct cx18_stream *s, struct cx18_buffer *buf,
-               const char __user *src, int copybytes)
-{
-       if (s->buf_size - buf->bytesused < copybytes)
-               copybytes = s->buf_size - buf->bytesused;
-       if (copy_from_user(buf->buf + buf->bytesused, src, copybytes))
-               return -EFAULT;
-       buf->bytesused += copybytes;
-       return copybytes;
-}
+#include "cx18-io.h"
 
 void cx18_buf_swap(struct cx18_buffer *buf)
 {
@@ -45,171 +36,312 @@ void cx18_buf_swap(struct cx18_buffer *buf)
                swab32s((u32 *)(buf->buf + i));
 }
 
+void _cx18_mdl_swap(struct cx18_mdl *mdl)
+{
+       struct cx18_buffer *buf;
+
+       list_for_each_entry(buf, &mdl->buf_list, list) {
+               if (buf->bytesused == 0)
+                       break;
+               cx18_buf_swap(buf);
+       }
+}
+
 void cx18_queue_init(struct cx18_queue *q)
 {
        INIT_LIST_HEAD(&q->list);
-       q->buffers = 0;
-       q->length = 0;
+       atomic_set(&q->depth, 0);
        q->bytesused = 0;
 }
 
-void cx18_enqueue(struct cx18_stream *s, struct cx18_buffer *buf,
-               struct cx18_queue *q)
+struct cx18_queue *_cx18_enqueue(struct cx18_stream *s, struct cx18_mdl *mdl,
+                                struct cx18_queue *q, int to_front)
+{
+       /* clear the mdl if it is not to be enqueued to the full queue */
+       if (q != &s->q_full) {
+               mdl->bytesused = 0;
+               mdl->readpos = 0;
+               mdl->m_flags = 0;
+               mdl->skipped = 0;
+               mdl->curr_buf = NULL;
+       }
+
+       /* q_busy is restricted to a max buffer count imposed by firmware */
+       if (q == &s->q_busy &&
+           atomic_read(&q->depth) >= CX18_MAX_FW_MDLS_PER_STREAM)
+               q = &s->q_free;
+
+       spin_lock(&q->lock);
+
+       if (to_front)
+               list_add(&mdl->list, &q->list); /* LIFO */
+       else
+               list_add_tail(&mdl->list, &q->list); /* FIFO */
+       q->bytesused += mdl->bytesused - mdl->readpos;
+       atomic_inc(&q->depth);
+
+       spin_unlock(&q->lock);
+       return q;
+}
+
+struct cx18_mdl *cx18_dequeue(struct cx18_stream *s, struct cx18_queue *q)
 {
-       unsigned long flags = 0;
+       struct cx18_mdl *mdl = NULL;
+
+       spin_lock(&q->lock);
+       if (!list_empty(&q->list)) {
+               mdl = list_first_entry(&q->list, struct cx18_mdl, list);
+               list_del_init(&mdl->list);
+               q->bytesused -= mdl->bytesused - mdl->readpos;
+               mdl->skipped = 0;
+               atomic_dec(&q->depth);
+       }
+       spin_unlock(&q->lock);
+       return mdl;
+}
+
+static void _cx18_mdl_update_bufs_for_cpu(struct cx18_stream *s,
+                                         struct cx18_mdl *mdl)
+{
+       struct cx18_buffer *buf;
+       u32 buf_size = s->buf_size;
+       u32 bytesused = mdl->bytesused;
 
-       /* clear the buffer if it is going to be enqueued to the free queue */
-       if (q == &s->q_free) {
-               buf->bytesused = 0;
+       list_for_each_entry(buf, &mdl->buf_list, list) {
                buf->readpos = 0;
-               buf->b_flags = 0;
+               if (bytesused >= buf_size) {
+                       buf->bytesused = buf_size;
+                       bytesused -= buf_size;
+               } else {
+                       buf->bytesused = bytesused;
+                       bytesused = 0;
+               }
+               cx18_buf_sync_for_cpu(s, buf);
        }
-       spin_lock_irqsave(&s->qlock, flags);
-       list_add_tail(&buf->list, &q->list);
-       q->buffers++;
-       q->length += s->buf_size;
-       q->bytesused += buf->bytesused - buf->readpos;
-       spin_unlock_irqrestore(&s->qlock, flags);
 }
 
-struct cx18_buffer *cx18_dequeue(struct cx18_stream *s, struct cx18_queue *q)
+static inline void cx18_mdl_update_bufs_for_cpu(struct cx18_stream *s,
+                                               struct cx18_mdl *mdl)
 {
-       struct cx18_buffer *buf = NULL;
-       unsigned long flags = 0;
+       struct cx18_buffer *buf;
 
-       spin_lock_irqsave(&s->qlock, flags);
-       if (!list_empty(&q->list)) {
-               buf = list_entry(q->list.next, struct cx18_buffer, list);
-               list_del_init(q->list.next);
-               q->buffers--;
-               q->length -= s->buf_size;
-               q->bytesused -= buf->bytesused - buf->readpos;
+       if (list_is_singular(&mdl->buf_list)) {
+               buf = list_first_entry(&mdl->buf_list, struct cx18_buffer,
+                                      list);
+               buf->bytesused = mdl->bytesused;
+               buf->readpos = 0;
+               cx18_buf_sync_for_cpu(s, buf);
+       } else {
+               _cx18_mdl_update_bufs_for_cpu(s, mdl);
        }
-       spin_unlock_irqrestore(&s->qlock, flags);
-       return buf;
 }
 
-struct cx18_buffer *cx18_queue_find_buf(struct cx18_stream *s, u32 id,
+struct cx18_mdl *cx18_queue_get_mdl(struct cx18_stream *s, u32 id,
        u32 bytesused)
 {
        struct cx18 *cx = s->cx;
-       struct list_head *p;
-
-       list_for_each(p, &s->q_free.list) {
-               struct cx18_buffer *buf =
-                       list_entry(p, struct cx18_buffer, list);
-
-               if (buf->id != id)
+       struct cx18_mdl *mdl;
+       struct cx18_mdl *tmp;
+       struct cx18_mdl *ret = NULL;
+       LIST_HEAD(sweep_up);
+
+       /*
+        * We don't have to acquire multiple q locks here, because we are
+        * serialized by the single threaded work handler.
+        * MDLs 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(mdl, 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 an mdl done message from the firmware.
+                * Once we skip an mdl repeatedly, relative to the size of
+                * q_busy, we have high confidence we've missed it.
+                */
+               if (mdl->id != id) {
+                       mdl->skipped++;
+                       if (mdl->skipped >= atomic_read(&s->q_busy.depth)-1) {
+                               /* mdl must have fallen out of rotation */
+                               CX18_WARN("Skipped %s, MDL %d, %d "
+                                         "times - it must have dropped out of "
+                                         "rotation\n", s->name, mdl->id,
+                                         mdl->skipped);
+                               /* Sweep it up to put it back into rotation */
+                               list_move_tail(&mdl->list, &sweep_up);
+                               atomic_dec(&s->q_busy.depth);
+                       }
                        continue;
-               buf->bytesused = bytesused;
-               /* the transport buffers are handled differently,
-                  so there is no need to move them to the full queue */
-               if (s->type == CX18_ENC_STREAM_TYPE_TS)
-                       return buf;
-               s->q_free.buffers--;
-               s->q_free.length -= s->buf_size;
-               s->q_full.buffers++;
-               s->q_full.length += s->buf_size;
-               s->q_full.bytesused += buf->bytesused;
-               list_move_tail(&buf->list, &s->q_full.list);
-               return buf;
+               }
+               /*
+                * We pull the desired mdl off of the queue here.  Something
+                * will have to put it back on a queue later.
+                */
+               list_del_init(&mdl->list);
+               atomic_dec(&s->q_busy.depth);
+               ret = mdl;
+               break;
+       }
+       spin_unlock(&s->q_busy.lock);
+
+       /*
+        * We found the mdl 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;
+               /* 0'ed readpos, m_flags & curr_buf when mdl went on q_busy */
+               cx18_mdl_update_bufs_for_cpu(s, ret);
+               if (s->type != CX18_ENC_STREAM_TYPE_TS)
+                       set_bit(CX18_F_M_NEED_SWAP, &ret->m_flags);
        }
-       CX18_ERR("Cannot find buffer %d for stream %s\n", id, s->name);
-       return NULL;
+
+       /* Put any mdls the firmware is ignoring back into normal rotation */
+       list_for_each_entry_safe(mdl, tmp, &sweep_up, list) {
+               list_del_init(&mdl->list);
+               cx18_enqueue(s, mdl, &s->q_free);
+       }
+       return ret;
 }
 
-static void cx18_queue_move_buf(struct cx18_stream *s, struct cx18_queue *from,
-               struct cx18_queue *to, int clear, int full)
+/* Move all mdls of a queue, while flushing the mdl */
+static void cx18_queue_flush(struct cx18_stream *s,
+                            struct cx18_queue *q_src, struct cx18_queue *q_dst)
 {
-       struct cx18_buffer *buf =
-               list_entry(from->list.next, struct cx18_buffer, list);
-
-       list_move_tail(from->list.next, &to->list);
-       from->buffers--;
-       from->length -= s->buf_size;
-       from->bytesused -= buf->bytesused - buf->readpos;
-       /* special handling for q_free */
-       if (clear)
-               buf->bytesused = buf->readpos = buf->b_flags = 0;
-       else if (full) {
-               /* special handling for stolen buffers, assume
-                  all bytes are used. */
-               buf->bytesused = s->buf_size;
-               buf->readpos = buf->b_flags = 0;
+       struct cx18_mdl *mdl;
+
+       /* It only makes sense to flush to q_free or q_idle */
+       if (q_src == q_dst || q_dst == &s->q_full || q_dst == &s->q_busy)
+               return;
+
+       spin_lock(&q_src->lock);
+       spin_lock(&q_dst->lock);
+       while (!list_empty(&q_src->list)) {
+               mdl = list_first_entry(&q_src->list, struct cx18_mdl, list);
+               list_move_tail(&mdl->list, &q_dst->list);
+               mdl->bytesused = 0;
+               mdl->readpos = 0;
+               mdl->m_flags = 0;
+               mdl->skipped = 0;
+               mdl->curr_buf = NULL;
+               atomic_inc(&q_dst->depth);
        }
-       to->buffers++;
-       to->length += s->buf_size;
-       to->bytesused += buf->bytesused - buf->readpos;
+       cx18_queue_init(q_src);
+       spin_unlock(&q_src->lock);
+       spin_unlock(&q_dst->lock);
 }
 
-/* Move 'needed_bytes' worth of buffers from queue 'from' into queue 'to'.
-   If 'needed_bytes' == 0, then move all buffers from 'from' into 'to'.
-   If 'steal' != NULL, then buffers may also taken from that queue if
-   needed.
-
-   The buffer is automatically cleared if it goes to the free queue. It is
-   also cleared if buffers need to be taken from the 'steal' queue and
-   the 'from' queue is the free queue.
-
-   When 'from' is q_free, then needed_bytes is compared to the total
-   available buffer length, otherwise needed_bytes is compared to the
-   bytesused value. For the 'steal' queue the total available buffer
-   length is always used.
-
-   -ENOMEM is returned if the buffers could not be obtained, 0 if all
-   buffers where obtained from the 'from' list and if non-zero then
-   the number of stolen buffers is returned. */
-int cx18_queue_move(struct cx18_stream *s, struct cx18_queue *from,
-       struct cx18_queue *steal, struct cx18_queue *to, int needed_bytes)
+void cx18_flush_queues(struct cx18_stream *s)
 {
-       unsigned long flags;
-       int rc = 0;
-       int from_free = from == &s->q_free;
-       int to_free = to == &s->q_free;
-       int bytes_available;
-
-       spin_lock_irqsave(&s->qlock, flags);
-       if (needed_bytes == 0) {
-               from_free = 1;
-               needed_bytes = from->length;
-       }
+       cx18_queue_flush(s, &s->q_busy, &s->q_free);
+       cx18_queue_flush(s, &s->q_full, &s->q_free);
+}
 
-       bytes_available = from_free ? from->length : from->bytesused;
-       bytes_available += steal ? steal->length : 0;
+/*
+ * Note, s->buf_pool is not protected by a lock,
+ * the stream better not have *anything* going on when calling this
+ */
+void cx18_unload_queues(struct cx18_stream *s)
+{
+       struct cx18_queue *q_idle = &s->q_idle;
+       struct cx18_mdl *mdl;
+       struct cx18_buffer *buf;
 
-       if (bytes_available < needed_bytes) {
-               spin_unlock_irqrestore(&s->qlock, flags);
-               return -ENOMEM;
+       /* Move all MDLS to q_idle */
+       cx18_queue_flush(s, &s->q_busy, q_idle);
+       cx18_queue_flush(s, &s->q_full, q_idle);
+       cx18_queue_flush(s, &s->q_free, q_idle);
+
+       /* Reset MDL id's and move all buffers back to the stream's buf_pool */
+       spin_lock(&q_idle->lock);
+       list_for_each_entry(mdl, &q_idle->list, list) {
+               while (!list_empty(&mdl->buf_list)) {
+                       buf = list_first_entry(&mdl->buf_list,
+                                              struct cx18_buffer, list);
+                       list_move_tail(&buf->list, &s->buf_pool);
+                       buf->bytesused = 0;
+                       buf->readpos = 0;
+               }
+               mdl->id = s->mdl_base_idx; /* reset id to a "safe" value */
+               /* all other mdl fields were cleared by cx18_queue_flush() */
        }
-       if (from_free) {
-               u32 old_length = to->length;
-
-               while (to->length - old_length < needed_bytes) {
-                       if (list_empty(&from->list))
-                               from = steal;
-                       if (from == steal)
-                               rc++;   /* keep track of 'stolen' buffers */
-                       cx18_queue_move_buf(s, from, to, 1, 0);
+       spin_unlock(&q_idle->lock);
+}
+
+/*
+ * Note, s->buf_pool is not protected by a lock,
+ * the stream better not have *anything* going on when calling this
+ */
+void cx18_load_queues(struct cx18_stream *s)
+{
+       struct cx18 *cx = s->cx;
+       struct cx18_mdl *mdl;
+       struct cx18_buffer *buf;
+       int mdl_id;
+       int i;
+       u32 partial_buf_size;
+
+       /*
+        * Attach buffers to MDLs, give the MDLs ids, and add MDLs to q_free
+        * Excess MDLs are left on q_idle
+        * Excess buffers are left in buf_pool and/or on an MDL in q_idle
+        */
+       mdl_id = s->mdl_base_idx;
+       for (mdl = cx18_dequeue(s, &s->q_idle), i = s->bufs_per_mdl;
+            mdl != NULL && i == s->bufs_per_mdl;
+            mdl = cx18_dequeue(s, &s->q_idle)) {
+
+               mdl->id = mdl_id;
+
+               for (i = 0; i < s->bufs_per_mdl; i++) {
+                       if (list_empty(&s->buf_pool))
+                               break;
+
+                       buf = list_first_entry(&s->buf_pool, struct cx18_buffer,
+                                              list);
+                       list_move_tail(&buf->list, &mdl->buf_list);
+
+                       /* update the firmware's MDL array with this buffer */
+                       cx18_writel(cx, buf->dma_handle,
+                                   &cx->scb->cpu_mdl[mdl_id + i].paddr);
+                       cx18_writel(cx, s->buf_size,
+                                   &cx->scb->cpu_mdl[mdl_id + i].length);
                }
-       } else {
-               u32 old_bytesused = to->bytesused;
-
-               while (to->bytesused - old_bytesused < needed_bytes) {
-                       if (list_empty(&from->list))
-                               from = steal;
-                       if (from == steal)
-                               rc++;   /* keep track of 'stolen' buffers */
-                       cx18_queue_move_buf(s, from, to, to_free, rc);
+
+               if (i == s->bufs_per_mdl) {
+                       /*
+                        * The encoder doesn't honor s->mdl_size.  So in the
+                        * case of a non-integral number of buffers to meet
+                        * mdl_size, we lie about the size of the last buffer
+                        * in the MDL to get the encoder to really only send
+                        * us mdl_size bytes per MDL transfer.
+                        */
+                       partial_buf_size = s->mdl_size % s->buf_size;
+                       if (partial_buf_size) {
+                               cx18_writel(cx, partial_buf_size,
+                                     &cx->scb->cpu_mdl[mdl_id + i - 1].length);
+                       }
+                       cx18_enqueue(s, mdl, &s->q_free);
+               } else {
+                       /* Not enough buffers for this MDL; we won't use it */
+                       cx18_push(s, mdl, &s->q_idle);
                }
+               mdl_id += i;
        }
-       spin_unlock_irqrestore(&s->qlock, flags);
-       return rc;
 }
 
-void cx18_flush_queues(struct cx18_stream *s)
+void _cx18_mdl_sync_for_device(struct cx18_stream *s, struct cx18_mdl *mdl)
 {
-       cx18_queue_move(s, &s->q_io, NULL, &s->q_free, 0);
-       cx18_queue_move(s, &s->q_full, NULL, &s->q_free, 0);
+       int dma = s->dma;
+       u32 buf_size = s->buf_size;
+       struct pci_dev *pci_dev = s->cx->pci_dev;
+       struct cx18_buffer *buf;
+
+       list_for_each_entry(buf, &mdl->buf_list, list)
+               pci_dma_sync_single_for_device(pci_dev, buf->dma_handle,
+                                              buf_size, dma);
 }
 
 int cx18_stream_alloc(struct cx18_stream *s)
@@ -220,44 +352,62 @@ int cx18_stream_alloc(struct cx18_stream *s)
        if (s->buffers == 0)
                return 0;
 
-       CX18_DEBUG_INFO("Allocate %s stream: %d x %d buffers (%dkB total)\n",
+       CX18_DEBUG_INFO("Allocate %s stream: %d x %d buffers "
+                       "(%d.%02d kB total)\n",
                s->name, s->buffers, s->buf_size,
-               s->buffers * s->buf_size / 1024);
+               s->buffers * s->buf_size / 1024,
+               (s->buffers * s->buf_size * 100 / 1024) % 100);
 
-       if (((char *)&cx->scb->cpu_mdl[cx->mdl_offset + s->buffers] -
-                               (char *)cx->scb) > SCB_RESERVED_SIZE) {
-               unsigned bufsz = (((char *)cx->scb) + SCB_RESERVED_SIZE -
-                                       ((char *)cx->scb->cpu_mdl));
+       if (((char __iomem *)&cx->scb->cpu_mdl[cx->free_mdl_idx + s->buffers] -
+                               (char __iomem *)cx->scb) > SCB_RESERVED_SIZE) {
+               unsigned bufsz = (((char __iomem *)cx->scb) + SCB_RESERVED_SIZE -
+                                       ((char __iomem *)cx->scb->cpu_mdl));
 
                CX18_ERR("Too many buffers, cannot fit in SCB area\n");
                CX18_ERR("Max buffers = %zd\n",
-                       bufsz / sizeof(struct cx18_mdl));
+                       bufsz / sizeof(struct cx18_mdl_ent));
                return -ENOMEM;
        }
 
-       s->mdl_offset = cx->mdl_offset;
+       s->mdl_base_idx = cx->free_mdl_idx;
 
-       /* allocate stream buffers. Initially all buffers are in q_free. */
+       /* allocate stream buffers and MDLs */
        for (i = 0; i < s->buffers; i++) {
-               struct cx18_buffer *buf =
-                       kzalloc(sizeof(struct cx18_buffer), GFP_KERNEL);
+               struct cx18_mdl *mdl;
+               struct cx18_buffer *buf;
+
+               /* 1 MDL per buffer to handle the worst & also default case */
+               mdl = kzalloc(sizeof(struct cx18_mdl), GFP_KERNEL|__GFP_NOWARN);
+               if (mdl == NULL)
+                       break;
 
-               if (buf == NULL)
+               buf = kzalloc(sizeof(struct cx18_buffer),
+                               GFP_KERNEL|__GFP_NOWARN);
+               if (buf == NULL) {
+                       kfree(mdl);
                        break;
-               buf->buf = kmalloc(s->buf_size, GFP_KERNEL);
+               }
+
+               buf->buf = kmalloc(s->buf_size, GFP_KERNEL|__GFP_NOWARN);
                if (buf->buf == NULL) {
+                       kfree(mdl);
                        kfree(buf);
                        break;
                }
-               buf->id = cx->buffer_id++;
+
+               INIT_LIST_HEAD(&mdl->list);
+               INIT_LIST_HEAD(&mdl->buf_list);
+               mdl->id = s->mdl_base_idx; /* a somewhat safe value */
+               cx18_enqueue(s, mdl, &s->q_idle);
+
                INIT_LIST_HEAD(&buf->list);
-               buf->dma_handle = pci_map_single(s->cx->dev,
+               buf->dma_handle = pci_map_single(s->cx->pci_dev,
                                buf->buf, s->buf_size, s->dma);
                cx18_buf_sync_for_cpu(s, buf);
-               cx18_enqueue(s, buf, &s->q_free);
+               list_add_tail(&buf->list, &s->buf_pool);
        }
        if (i == s->buffers) {
-               cx->mdl_offset += s->buffers;
+               cx->free_mdl_idx += s->buffers;
                return 0;
        }
        CX18_ERR("Couldn't allocate buffers for %s stream\n", s->name);
@@ -267,14 +417,25 @@ int cx18_stream_alloc(struct cx18_stream *s)
 
 void cx18_stream_free(struct cx18_stream *s)
 {
+       struct cx18_mdl *mdl;
        struct cx18_buffer *buf;
+       struct cx18 *cx = s->cx;
+
+       CX18_DEBUG_INFO("Deallocating buffers for %s stream\n", s->name);
+
+       /* move all buffers to buf_pool and all MDLs to q_idle */
+       cx18_unload_queues(s);
+
+       /* empty q_idle */
+       while ((mdl = cx18_dequeue(s, &s->q_idle)))
+               kfree(mdl);
 
-       /* move all buffers to q_free */
-       cx18_flush_queues(s);
+       /* empty buf_pool */
+       while (!list_empty(&s->buf_pool)) {
+               buf = list_first_entry(&s->buf_pool, struct cx18_buffer, list);
+               list_del_init(&buf->list);
 
-       /* empty q_free */
-       while ((buf = cx18_dequeue(s, &s->q_free))) {
-               pci_unmap_single(s->cx->dev, buf->dma_handle,
+               pci_unmap_single(s->cx->pci_dev, buf->dma_handle,
                                s->buf_size, s->dma);
                kfree(buf->buf);
                kfree(buf);