cx18-queue.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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_first_entry(&q->list, struct cx18_buffer, list);
  70. list_del_init(&buf->list);
  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 *tmp;
  84. struct cx18_buffer *ret = NULL;
  85. mutex_lock(&s->qlock);
  86. list_for_each_entry_safe(buf, tmp, &s->q_busy.list, list) {
  87. if (buf->id != id) {
  88. buf->skipped++;
  89. if (buf->skipped >= atomic_read(&s->q_busy.buffers)-1) {
  90. /* buffer must have fallen out of rotation */
  91. CX18_WARN("Skipped %s, buffer %d, %d "
  92. "times - it must have dropped out of "
  93. "rotation\n", s->name, buf->id,
  94. buf->skipped);
  95. /* move it to q_free */
  96. list_move_tail(&buf->list, &s->q_free.list);
  97. buf->bytesused = buf->readpos = buf->b_flags =
  98. buf->skipped = 0;
  99. atomic_dec(&s->q_busy.buffers);
  100. atomic_inc(&s->q_free.buffers);
  101. }
  102. continue;
  103. }
  104. buf->bytesused = bytesused;
  105. /* Sync the buffer before we release the qlock */
  106. cx18_buf_sync_for_cpu(s, buf);
  107. if (s->type == CX18_ENC_STREAM_TYPE_TS) {
  108. /*
  109. * TS doesn't use q_full. As we pull the buffer off of
  110. * the queue here, the caller will have to put it back.
  111. */
  112. list_del_init(&buf->list);
  113. } else {
  114. /* Move buffer from q_busy to q_full */
  115. list_move_tail(&buf->list, &s->q_full.list);
  116. set_bit(CX18_F_B_NEED_BUF_SWAP, &buf->b_flags);
  117. s->q_full.bytesused += buf->bytesused;
  118. atomic_inc(&s->q_full.buffers);
  119. }
  120. atomic_dec(&s->q_busy.buffers);
  121. ret = buf;
  122. break;
  123. }
  124. mutex_unlock(&s->qlock);
  125. return ret;
  126. }
  127. /* Move all buffers of a queue to q_free, while flushing the buffers */
  128. static void cx18_queue_flush(struct cx18_stream *s, struct cx18_queue *q)
  129. {
  130. struct cx18_buffer *buf;
  131. if (q == &s->q_free)
  132. return;
  133. mutex_lock(&s->qlock);
  134. while (!list_empty(&q->list)) {
  135. buf = list_first_entry(&q->list, struct cx18_buffer, list);
  136. list_move_tail(&buf->list, &s->q_free.list);
  137. buf->bytesused = buf->readpos = buf->b_flags = buf->skipped = 0;
  138. atomic_inc(&s->q_free.buffers);
  139. }
  140. cx18_queue_init(q);
  141. mutex_unlock(&s->qlock);
  142. }
  143. void cx18_flush_queues(struct cx18_stream *s)
  144. {
  145. cx18_queue_flush(s, &s->q_busy);
  146. cx18_queue_flush(s, &s->q_full);
  147. }
  148. int cx18_stream_alloc(struct cx18_stream *s)
  149. {
  150. struct cx18 *cx = s->cx;
  151. int i;
  152. if (s->buffers == 0)
  153. return 0;
  154. CX18_DEBUG_INFO("Allocate %s stream: %d x %d buffers (%dkB total)\n",
  155. s->name, s->buffers, s->buf_size,
  156. s->buffers * s->buf_size / 1024);
  157. if (((char __iomem *)&cx->scb->cpu_mdl[cx->mdl_offset + s->buffers] -
  158. (char __iomem *)cx->scb) > SCB_RESERVED_SIZE) {
  159. unsigned bufsz = (((char __iomem *)cx->scb) + SCB_RESERVED_SIZE -
  160. ((char __iomem *)cx->scb->cpu_mdl));
  161. CX18_ERR("Too many buffers, cannot fit in SCB area\n");
  162. CX18_ERR("Max buffers = %zd\n",
  163. bufsz / sizeof(struct cx18_mdl));
  164. return -ENOMEM;
  165. }
  166. s->mdl_offset = cx->mdl_offset;
  167. /* allocate stream buffers. Initially all buffers are in q_free. */
  168. for (i = 0; i < s->buffers; i++) {
  169. struct cx18_buffer *buf = kzalloc(sizeof(struct cx18_buffer),
  170. GFP_KERNEL|__GFP_NOWARN);
  171. if (buf == NULL)
  172. break;
  173. buf->buf = kmalloc(s->buf_size, GFP_KERNEL|__GFP_NOWARN);
  174. if (buf->buf == NULL) {
  175. kfree(buf);
  176. break;
  177. }
  178. buf->id = cx->buffer_id++;
  179. INIT_LIST_HEAD(&buf->list);
  180. buf->dma_handle = pci_map_single(s->cx->pci_dev,
  181. buf->buf, s->buf_size, s->dma);
  182. cx18_buf_sync_for_cpu(s, buf);
  183. cx18_enqueue(s, buf, &s->q_free);
  184. }
  185. if (i == s->buffers) {
  186. cx->mdl_offset += s->buffers;
  187. return 0;
  188. }
  189. CX18_ERR("Couldn't allocate buffers for %s stream\n", s->name);
  190. cx18_stream_free(s);
  191. return -ENOMEM;
  192. }
  193. void cx18_stream_free(struct cx18_stream *s)
  194. {
  195. struct cx18_buffer *buf;
  196. /* move all buffers to q_free */
  197. cx18_flush_queues(s);
  198. /* empty q_free */
  199. while ((buf = cx18_dequeue(s, &s->q_free))) {
  200. pci_unmap_single(s->cx->pci_dev, buf->dma_handle,
  201. s->buf_size, s->dma);
  202. kfree(buf->buf);
  203. kfree(buf);
  204. }
  205. }