cx18-queue.c 6.1 KB

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