cx18-queue.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * cx18 buffer queues
  3. *
  4. * Derived from ivtv-queue.c
  5. *
  6. * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
  7. * Copyright (C) 2008 Andy Walls <awalls@radix.net>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  22. * 02111-1307 USA
  23. */
  24. #include "cx18-driver.h"
  25. #include "cx18-streams.h"
  26. #include "cx18-queue.h"
  27. #include "cx18-scb.h"
  28. void cx18_buf_swap(struct cx18_buffer *buf)
  29. {
  30. int i;
  31. for (i = 0; i < buf->bytesused; i += 4)
  32. swab32s((u32 *)(buf->buf + i));
  33. }
  34. void cx18_queue_init(struct cx18_queue *q)
  35. {
  36. INIT_LIST_HEAD(&q->list);
  37. atomic_set(&q->buffers, 0);
  38. q->bytesused = 0;
  39. }
  40. struct cx18_queue *_cx18_enqueue(struct cx18_stream *s, struct cx18_buffer *buf,
  41. struct cx18_queue *q, int to_front)
  42. {
  43. /* clear the buffer if it is not to be enqueued to the full queue */
  44. if (q != &s->q_full) {
  45. buf->bytesused = 0;
  46. buf->readpos = 0;
  47. buf->b_flags = 0;
  48. buf->skipped = 0;
  49. }
  50. mutex_lock(&s->qlock);
  51. /* q_busy is restricted to a max buffer count imposed by firmware */
  52. if (q == &s->q_busy &&
  53. atomic_read(&q->buffers) >= CX18_MAX_FW_MDLS_PER_STREAM)
  54. q = &s->q_free;
  55. if (to_front)
  56. list_add(&buf->list, &q->list); /* LIFO */
  57. else
  58. list_add_tail(&buf->list, &q->list); /* FIFO */
  59. q->bytesused += buf->bytesused - buf->readpos;
  60. atomic_inc(&q->buffers);
  61. mutex_unlock(&s->qlock);
  62. return q;
  63. }
  64. struct cx18_buffer *cx18_dequeue(struct cx18_stream *s, struct cx18_queue *q)
  65. {
  66. struct cx18_buffer *buf = NULL;
  67. mutex_lock(&s->qlock);
  68. if (!list_empty(&q->list)) {
  69. buf = list_entry(q->list.next, struct cx18_buffer, list);
  70. list_del_init(q->list.next);
  71. q->bytesused -= buf->bytesused - buf->readpos;
  72. buf->skipped = 0;
  73. atomic_dec(&q->buffers);
  74. }
  75. mutex_unlock(&s->qlock);
  76. return buf;
  77. }
  78. struct cx18_buffer *cx18_queue_get_buf(struct cx18_stream *s, u32 id,
  79. u32 bytesused)
  80. {
  81. struct cx18 *cx = s->cx;
  82. struct cx18_buffer *buf;
  83. struct cx18_buffer *ret = NULL;
  84. struct list_head *p, *t;
  85. mutex_lock(&s->qlock);
  86. list_for_each_safe(p, t, &s->q_busy.list) {
  87. buf = list_entry(p, struct cx18_buffer, list);
  88. if (buf->id != id) {
  89. buf->skipped++;
  90. if (buf->skipped >= atomic_read(&s->q_busy.buffers)-1) {
  91. /* buffer must have fallen out of rotation */
  92. CX18_WARN("Skipped %s, buffer %d, %d "
  93. "times - it must have dropped out of "
  94. "rotation\n", s->name, buf->id,
  95. buf->skipped);
  96. /* move it to q_free */
  97. list_move_tail(&buf->list, &s->q_free.list);
  98. buf->bytesused = buf->readpos = buf->b_flags =
  99. buf->skipped = 0;
  100. atomic_dec(&s->q_busy.buffers);
  101. atomic_inc(&s->q_free.buffers);
  102. }
  103. continue;
  104. }
  105. buf->bytesused = bytesused;
  106. /* Sync the buffer before we release the qlock */
  107. cx18_buf_sync_for_cpu(s, buf);
  108. if (s->type == CX18_ENC_STREAM_TYPE_TS) {
  109. /*
  110. * TS doesn't use q_full. As we pull the buffer off of
  111. * the queue here, the caller will have to put it back.
  112. */
  113. list_del_init(&buf->list);
  114. } else {
  115. /* Move buffer from q_busy to q_full */
  116. list_move_tail(&buf->list, &s->q_full.list);
  117. set_bit(CX18_F_B_NEED_BUF_SWAP, &buf->b_flags);
  118. s->q_full.bytesused += buf->bytesused;
  119. atomic_inc(&s->q_full.buffers);
  120. }
  121. atomic_dec(&s->q_busy.buffers);
  122. ret = buf;
  123. break;
  124. }
  125. mutex_unlock(&s->qlock);
  126. return ret;
  127. }
  128. /* Move all buffers of a queue to q_free, while flushing the buffers */
  129. static void cx18_queue_flush(struct cx18_stream *s, struct cx18_queue *q)
  130. {
  131. struct cx18_buffer *buf;
  132. if (q == &s->q_free)
  133. return;
  134. mutex_lock(&s->qlock);
  135. while (!list_empty(&q->list)) {
  136. buf = list_entry(q->list.next, struct cx18_buffer, list);
  137. list_move_tail(q->list.next, &s->q_free.list);
  138. buf->bytesused = buf->readpos = buf->b_flags = buf->skipped = 0;
  139. atomic_inc(&s->q_free.buffers);
  140. }
  141. cx18_queue_init(q);
  142. mutex_unlock(&s->qlock);
  143. }
  144. void cx18_flush_queues(struct cx18_stream *s)
  145. {
  146. cx18_queue_flush(s, &s->q_busy);
  147. cx18_queue_flush(s, &s->q_full);
  148. }
  149. int cx18_stream_alloc(struct cx18_stream *s)
  150. {
  151. struct cx18 *cx = s->cx;
  152. int i;
  153. if (s->buffers == 0)
  154. return 0;
  155. CX18_DEBUG_INFO("Allocate %s stream: %d x %d buffers (%dkB total)\n",
  156. s->name, s->buffers, s->buf_size,
  157. s->buffers * s->buf_size / 1024);
  158. if (((char __iomem *)&cx->scb->cpu_mdl[cx->mdl_offset + s->buffers] -
  159. (char __iomem *)cx->scb) > SCB_RESERVED_SIZE) {
  160. unsigned bufsz = (((char __iomem *)cx->scb) + SCB_RESERVED_SIZE -
  161. ((char __iomem *)cx->scb->cpu_mdl));
  162. CX18_ERR("Too many buffers, cannot fit in SCB area\n");
  163. CX18_ERR("Max buffers = %zd\n",
  164. bufsz / sizeof(struct cx18_mdl));
  165. return -ENOMEM;
  166. }
  167. s->mdl_offset = cx->mdl_offset;
  168. /* allocate stream buffers. Initially all buffers are in q_free. */
  169. for (i = 0; i < s->buffers; i++) {
  170. struct cx18_buffer *buf = kzalloc(sizeof(struct cx18_buffer),
  171. GFP_KERNEL|__GFP_NOWARN);
  172. if (buf == NULL)
  173. break;
  174. buf->buf = kmalloc(s->buf_size, GFP_KERNEL|__GFP_NOWARN);
  175. if (buf->buf == NULL) {
  176. kfree(buf);
  177. break;
  178. }
  179. buf->id = cx->buffer_id++;
  180. INIT_LIST_HEAD(&buf->list);
  181. buf->dma_handle = pci_map_single(s->cx->dev,
  182. buf->buf, s->buf_size, s->dma);
  183. cx18_buf_sync_for_cpu(s, buf);
  184. cx18_enqueue(s, buf, &s->q_free);
  185. }
  186. if (i == s->buffers) {
  187. cx->mdl_offset += s->buffers;
  188. return 0;
  189. }
  190. CX18_ERR("Couldn't allocate buffers for %s stream\n", s->name);
  191. cx18_stream_free(s);
  192. return -ENOMEM;
  193. }
  194. void cx18_stream_free(struct cx18_stream *s)
  195. {
  196. struct cx18_buffer *buf;
  197. /* move all buffers to q_free */
  198. cx18_flush_queues(s);
  199. /* empty q_free */
  200. while ((buf = cx18_dequeue(s, &s->q_free))) {
  201. pci_unmap_single(s->cx->dev, buf->dma_handle,
  202. s->buf_size, s->dma);
  203. kfree(buf->buf);
  204. kfree(buf);
  205. }
  206. }