cx18-queue.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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-queue.h"
  26. #include "cx18-streams.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. /* q_busy is restricted to a max buffer count imposed by firmware */
  51. if (q == &s->q_busy &&
  52. atomic_read(&q->buffers) >= CX18_MAX_FW_MDLS_PER_STREAM)
  53. q = &s->q_free;
  54. spin_lock(&q->lock);
  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. spin_unlock(&q->lock);
  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. spin_lock(&q->lock);
  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. spin_unlock(&q->lock);
  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. LIST_HEAD(sweep_up);
  86. /*
  87. * We don't have to acquire multiple q locks here, because we are
  88. * serialized by the single threaded work handler.
  89. * Buffers from the firmware will thus remain in order as
  90. * they are moved from q_busy to q_full or to the dvb ring buffer.
  91. */
  92. spin_lock(&s->q_busy.lock);
  93. list_for_each_entry_safe(buf, tmp, &s->q_busy.list, list) {
  94. /*
  95. * We should find what the firmware told us is done,
  96. * right at the front of the queue. If we don't, we likely have
  97. * missed a buffer done message from the firmware.
  98. * Once we skip a buffer repeatedly, relative to the size of
  99. * q_busy, we have high confidence we've missed it.
  100. */
  101. if (buf->id != id) {
  102. buf->skipped++;
  103. if (buf->skipped >= atomic_read(&s->q_busy.buffers)-1) {
  104. /* buffer must have fallen out of rotation */
  105. CX18_WARN("Skipped %s, buffer %d, %d "
  106. "times - it must have dropped out of "
  107. "rotation\n", s->name, buf->id,
  108. buf->skipped);
  109. /* Sweep it up to put it back into rotation */
  110. list_move_tail(&buf->list, &sweep_up);
  111. atomic_dec(&s->q_busy.buffers);
  112. }
  113. continue;
  114. }
  115. /*
  116. * We pull the desired buffer off of the queue here. Something
  117. * will have to put it back on a queue later.
  118. */
  119. list_del_init(&buf->list);
  120. atomic_dec(&s->q_busy.buffers);
  121. ret = buf;
  122. break;
  123. }
  124. spin_unlock(&s->q_busy.lock);
  125. /*
  126. * We found the buffer for which we were looking. Get it ready for
  127. * the caller to put on q_full or in the dvb ring buffer.
  128. */
  129. if (ret != NULL) {
  130. ret->bytesused = bytesused;
  131. ret->skipped = 0;
  132. /* readpos and b_flags were 0'ed when the buf went on q_busy */
  133. cx18_buf_sync_for_cpu(s, ret);
  134. if (s->type != CX18_ENC_STREAM_TYPE_TS)
  135. set_bit(CX18_F_B_NEED_BUF_SWAP, &ret->b_flags);
  136. }
  137. /* Put any buffers the firmware is ignoring back into normal rotation */
  138. list_for_each_entry_safe(buf, tmp, &sweep_up, list) {
  139. list_del_init(&buf->list);
  140. cx18_enqueue(s, buf, &s->q_free);
  141. }
  142. return ret;
  143. }
  144. /* Move all buffers of a queue to q_free, while flushing the buffers */
  145. static void cx18_queue_flush(struct cx18_stream *s, struct cx18_queue *q)
  146. {
  147. struct cx18_buffer *buf;
  148. if (q == &s->q_free)
  149. return;
  150. spin_lock(&q->lock);
  151. while (!list_empty(&q->list)) {
  152. buf = list_first_entry(&q->list, struct cx18_buffer, list);
  153. list_move_tail(&buf->list, &s->q_free.list);
  154. buf->bytesused = buf->readpos = buf->b_flags = buf->skipped = 0;
  155. atomic_inc(&s->q_free.buffers);
  156. }
  157. cx18_queue_init(q);
  158. spin_unlock(&q->lock);
  159. }
  160. void cx18_flush_queues(struct cx18_stream *s)
  161. {
  162. cx18_queue_flush(s, &s->q_busy);
  163. cx18_queue_flush(s, &s->q_full);
  164. }
  165. int cx18_stream_alloc(struct cx18_stream *s)
  166. {
  167. struct cx18 *cx = s->cx;
  168. int i;
  169. if (s->buffers == 0)
  170. return 0;
  171. CX18_DEBUG_INFO("Allocate %s stream: %d x %d buffers (%dkB total)\n",
  172. s->name, s->buffers, s->buf_size,
  173. s->buffers * s->buf_size / 1024);
  174. if (((char __iomem *)&cx->scb->cpu_mdl[cx->mdl_offset + s->buffers] -
  175. (char __iomem *)cx->scb) > SCB_RESERVED_SIZE) {
  176. unsigned bufsz = (((char __iomem *)cx->scb) + SCB_RESERVED_SIZE -
  177. ((char __iomem *)cx->scb->cpu_mdl));
  178. CX18_ERR("Too many buffers, cannot fit in SCB area\n");
  179. CX18_ERR("Max buffers = %zd\n",
  180. bufsz / sizeof(struct cx18_mdl));
  181. return -ENOMEM;
  182. }
  183. s->mdl_offset = cx->mdl_offset;
  184. /* allocate stream buffers. Initially all buffers are in q_free. */
  185. for (i = 0; i < s->buffers; i++) {
  186. struct cx18_buffer *buf = kzalloc(sizeof(struct cx18_buffer),
  187. GFP_KERNEL|__GFP_NOWARN);
  188. if (buf == NULL)
  189. break;
  190. buf->buf = kmalloc(s->buf_size, GFP_KERNEL|__GFP_NOWARN);
  191. if (buf->buf == NULL) {
  192. kfree(buf);
  193. break;
  194. }
  195. buf->id = cx->buffer_id++;
  196. INIT_LIST_HEAD(&buf->list);
  197. buf->dma_handle = pci_map_single(s->cx->pci_dev,
  198. buf->buf, s->buf_size, s->dma);
  199. cx18_buf_sync_for_cpu(s, buf);
  200. cx18_enqueue(s, buf, &s->q_free);
  201. }
  202. if (i == s->buffers) {
  203. cx->mdl_offset += s->buffers;
  204. return 0;
  205. }
  206. CX18_ERR("Couldn't allocate buffers for %s stream\n", s->name);
  207. cx18_stream_free(s);
  208. return -ENOMEM;
  209. }
  210. void cx18_stream_free(struct cx18_stream *s)
  211. {
  212. struct cx18_buffer *buf;
  213. /* move all buffers to q_free */
  214. cx18_flush_queues(s);
  215. /* empty q_free */
  216. while ((buf = cx18_dequeue(s, &s->q_free))) {
  217. pci_unmap_single(s->cx->pci_dev, buf->dma_handle,
  218. s->buf_size, s->dma);
  219. kfree(buf->buf);
  220. kfree(buf);
  221. }
  222. }