cx18-queue.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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@md.metrocast.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. #include "cx18-driver.h"
  20. #include "cx18-queue.h"
  21. #include "cx18-streams.h"
  22. #include "cx18-scb.h"
  23. #include "cx18-io.h"
  24. void cx18_buf_swap(struct cx18_buffer *buf)
  25. {
  26. int i;
  27. for (i = 0; i < buf->bytesused; i += 4)
  28. swab32s((u32 *)(buf->buf + i));
  29. }
  30. void _cx18_mdl_swap(struct cx18_mdl *mdl)
  31. {
  32. struct cx18_buffer *buf;
  33. list_for_each_entry(buf, &mdl->buf_list, list) {
  34. if (buf->bytesused == 0)
  35. break;
  36. cx18_buf_swap(buf);
  37. }
  38. }
  39. void cx18_queue_init(struct cx18_queue *q)
  40. {
  41. INIT_LIST_HEAD(&q->list);
  42. atomic_set(&q->depth, 0);
  43. q->bytesused = 0;
  44. }
  45. struct cx18_queue *_cx18_enqueue(struct cx18_stream *s, struct cx18_mdl *mdl,
  46. struct cx18_queue *q, int to_front)
  47. {
  48. /* clear the mdl if it is not to be enqueued to the full queue */
  49. if (q != &s->q_full) {
  50. mdl->bytesused = 0;
  51. mdl->readpos = 0;
  52. mdl->m_flags = 0;
  53. mdl->skipped = 0;
  54. mdl->curr_buf = NULL;
  55. }
  56. /* q_busy is restricted to a max buffer count imposed by firmware */
  57. if (q == &s->q_busy &&
  58. atomic_read(&q->depth) >= CX18_MAX_FW_MDLS_PER_STREAM)
  59. q = &s->q_free;
  60. spin_lock(&q->lock);
  61. if (to_front)
  62. list_add(&mdl->list, &q->list); /* LIFO */
  63. else
  64. list_add_tail(&mdl->list, &q->list); /* FIFO */
  65. q->bytesused += mdl->bytesused - mdl->readpos;
  66. atomic_inc(&q->depth);
  67. spin_unlock(&q->lock);
  68. return q;
  69. }
  70. struct cx18_mdl *cx18_dequeue(struct cx18_stream *s, struct cx18_queue *q)
  71. {
  72. struct cx18_mdl *mdl = NULL;
  73. spin_lock(&q->lock);
  74. if (!list_empty(&q->list)) {
  75. mdl = list_first_entry(&q->list, struct cx18_mdl, list);
  76. list_del_init(&mdl->list);
  77. q->bytesused -= mdl->bytesused - mdl->readpos;
  78. mdl->skipped = 0;
  79. atomic_dec(&q->depth);
  80. }
  81. spin_unlock(&q->lock);
  82. return mdl;
  83. }
  84. static void _cx18_mdl_update_bufs_for_cpu(struct cx18_stream *s,
  85. struct cx18_mdl *mdl)
  86. {
  87. struct cx18_buffer *buf;
  88. u32 buf_size = s->buf_size;
  89. u32 bytesused = mdl->bytesused;
  90. list_for_each_entry(buf, &mdl->buf_list, list) {
  91. buf->readpos = 0;
  92. if (bytesused >= buf_size) {
  93. buf->bytesused = buf_size;
  94. bytesused -= buf_size;
  95. } else {
  96. buf->bytesused = bytesused;
  97. bytesused = 0;
  98. }
  99. cx18_buf_sync_for_cpu(s, buf);
  100. }
  101. }
  102. static inline void cx18_mdl_update_bufs_for_cpu(struct cx18_stream *s,
  103. struct cx18_mdl *mdl)
  104. {
  105. struct cx18_buffer *buf;
  106. if (list_is_singular(&mdl->buf_list)) {
  107. buf = list_first_entry(&mdl->buf_list, struct cx18_buffer,
  108. list);
  109. buf->bytesused = mdl->bytesused;
  110. buf->readpos = 0;
  111. cx18_buf_sync_for_cpu(s, buf);
  112. } else {
  113. _cx18_mdl_update_bufs_for_cpu(s, mdl);
  114. }
  115. }
  116. struct cx18_mdl *cx18_queue_get_mdl(struct cx18_stream *s, u32 id,
  117. u32 bytesused)
  118. {
  119. struct cx18 *cx = s->cx;
  120. struct cx18_mdl *mdl;
  121. struct cx18_mdl *tmp;
  122. struct cx18_mdl *ret = NULL;
  123. LIST_HEAD(sweep_up);
  124. /*
  125. * We don't have to acquire multiple q locks here, because we are
  126. * serialized by the single threaded work handler.
  127. * MDLs from the firmware will thus remain in order as
  128. * they are moved from q_busy to q_full or to the dvb ring buffer.
  129. */
  130. spin_lock(&s->q_busy.lock);
  131. list_for_each_entry_safe(mdl, tmp, &s->q_busy.list, list) {
  132. /*
  133. * We should find what the firmware told us is done,
  134. * right at the front of the queue. If we don't, we likely have
  135. * missed an mdl done message from the firmware.
  136. * Once we skip an mdl repeatedly, relative to the size of
  137. * q_busy, we have high confidence we've missed it.
  138. */
  139. if (mdl->id != id) {
  140. mdl->skipped++;
  141. if (mdl->skipped >= atomic_read(&s->q_busy.depth)-1) {
  142. /* mdl must have fallen out of rotation */
  143. CX18_WARN("Skipped %s, MDL %d, %d times - it must have dropped out of rotation\n",
  144. s->name, mdl->id,
  145. mdl->skipped);
  146. /* Sweep it up to put it back into rotation */
  147. list_move_tail(&mdl->list, &sweep_up);
  148. atomic_dec(&s->q_busy.depth);
  149. }
  150. continue;
  151. }
  152. /*
  153. * We pull the desired mdl off of the queue here. Something
  154. * will have to put it back on a queue later.
  155. */
  156. list_del_init(&mdl->list);
  157. atomic_dec(&s->q_busy.depth);
  158. ret = mdl;
  159. break;
  160. }
  161. spin_unlock(&s->q_busy.lock);
  162. /*
  163. * We found the mdl for which we were looking. Get it ready for
  164. * the caller to put on q_full or in the dvb ring buffer.
  165. */
  166. if (ret != NULL) {
  167. ret->bytesused = bytesused;
  168. ret->skipped = 0;
  169. /* 0'ed readpos, m_flags & curr_buf when mdl went on q_busy */
  170. cx18_mdl_update_bufs_for_cpu(s, ret);
  171. if (s->type != CX18_ENC_STREAM_TYPE_TS)
  172. set_bit(CX18_F_M_NEED_SWAP, &ret->m_flags);
  173. }
  174. /* Put any mdls the firmware is ignoring back into normal rotation */
  175. list_for_each_entry_safe(mdl, tmp, &sweep_up, list) {
  176. list_del_init(&mdl->list);
  177. cx18_enqueue(s, mdl, &s->q_free);
  178. }
  179. return ret;
  180. }
  181. /* Move all mdls of a queue, while flushing the mdl */
  182. static void cx18_queue_flush(struct cx18_stream *s,
  183. struct cx18_queue *q_src, struct cx18_queue *q_dst)
  184. {
  185. struct cx18_mdl *mdl;
  186. /* It only makes sense to flush to q_free or q_idle */
  187. if (q_src == q_dst || q_dst == &s->q_full || q_dst == &s->q_busy)
  188. return;
  189. spin_lock(&q_src->lock);
  190. spin_lock(&q_dst->lock);
  191. while (!list_empty(&q_src->list)) {
  192. mdl = list_first_entry(&q_src->list, struct cx18_mdl, list);
  193. list_move_tail(&mdl->list, &q_dst->list);
  194. mdl->bytesused = 0;
  195. mdl->readpos = 0;
  196. mdl->m_flags = 0;
  197. mdl->skipped = 0;
  198. mdl->curr_buf = NULL;
  199. atomic_inc(&q_dst->depth);
  200. }
  201. cx18_queue_init(q_src);
  202. spin_unlock(&q_src->lock);
  203. spin_unlock(&q_dst->lock);
  204. }
  205. void cx18_flush_queues(struct cx18_stream *s)
  206. {
  207. cx18_queue_flush(s, &s->q_busy, &s->q_free);
  208. cx18_queue_flush(s, &s->q_full, &s->q_free);
  209. }
  210. /*
  211. * Note, s->buf_pool is not protected by a lock,
  212. * the stream better not have *anything* going on when calling this
  213. */
  214. void cx18_unload_queues(struct cx18_stream *s)
  215. {
  216. struct cx18_queue *q_idle = &s->q_idle;
  217. struct cx18_mdl *mdl;
  218. struct cx18_buffer *buf;
  219. /* Move all MDLS to q_idle */
  220. cx18_queue_flush(s, &s->q_busy, q_idle);
  221. cx18_queue_flush(s, &s->q_full, q_idle);
  222. cx18_queue_flush(s, &s->q_free, q_idle);
  223. /* Reset MDL id's and move all buffers back to the stream's buf_pool */
  224. spin_lock(&q_idle->lock);
  225. list_for_each_entry(mdl, &q_idle->list, list) {
  226. while (!list_empty(&mdl->buf_list)) {
  227. buf = list_first_entry(&mdl->buf_list,
  228. struct cx18_buffer, list);
  229. list_move_tail(&buf->list, &s->buf_pool);
  230. buf->bytesused = 0;
  231. buf->readpos = 0;
  232. }
  233. mdl->id = s->mdl_base_idx; /* reset id to a "safe" value */
  234. /* all other mdl fields were cleared by cx18_queue_flush() */
  235. }
  236. spin_unlock(&q_idle->lock);
  237. }
  238. /*
  239. * Note, s->buf_pool is not protected by a lock,
  240. * the stream better not have *anything* going on when calling this
  241. */
  242. void cx18_load_queues(struct cx18_stream *s)
  243. {
  244. struct cx18 *cx = s->cx;
  245. struct cx18_mdl *mdl;
  246. struct cx18_buffer *buf;
  247. int mdl_id;
  248. int i;
  249. u32 partial_buf_size;
  250. /*
  251. * Attach buffers to MDLs, give the MDLs ids, and add MDLs to q_free
  252. * Excess MDLs are left on q_idle
  253. * Excess buffers are left in buf_pool and/or on an MDL in q_idle
  254. */
  255. mdl_id = s->mdl_base_idx;
  256. for (mdl = cx18_dequeue(s, &s->q_idle), i = s->bufs_per_mdl;
  257. mdl != NULL && i == s->bufs_per_mdl;
  258. mdl = cx18_dequeue(s, &s->q_idle)) {
  259. mdl->id = mdl_id;
  260. for (i = 0; i < s->bufs_per_mdl; i++) {
  261. if (list_empty(&s->buf_pool))
  262. break;
  263. buf = list_first_entry(&s->buf_pool, struct cx18_buffer,
  264. list);
  265. list_move_tail(&buf->list, &mdl->buf_list);
  266. /* update the firmware's MDL array with this buffer */
  267. cx18_writel(cx, buf->dma_handle,
  268. &cx->scb->cpu_mdl[mdl_id + i].paddr);
  269. cx18_writel(cx, s->buf_size,
  270. &cx->scb->cpu_mdl[mdl_id + i].length);
  271. }
  272. if (i == s->bufs_per_mdl) {
  273. /*
  274. * The encoder doesn't honor s->mdl_size. So in the
  275. * case of a non-integral number of buffers to meet
  276. * mdl_size, we lie about the size of the last buffer
  277. * in the MDL to get the encoder to really only send
  278. * us mdl_size bytes per MDL transfer.
  279. */
  280. partial_buf_size = s->mdl_size % s->buf_size;
  281. if (partial_buf_size) {
  282. cx18_writel(cx, partial_buf_size,
  283. &cx->scb->cpu_mdl[mdl_id + i - 1].length);
  284. }
  285. cx18_enqueue(s, mdl, &s->q_free);
  286. } else {
  287. /* Not enough buffers for this MDL; we won't use it */
  288. cx18_push(s, mdl, &s->q_idle);
  289. }
  290. mdl_id += i;
  291. }
  292. }
  293. void _cx18_mdl_sync_for_device(struct cx18_stream *s, struct cx18_mdl *mdl)
  294. {
  295. int dma = s->dma;
  296. u32 buf_size = s->buf_size;
  297. struct pci_dev *pci_dev = s->cx->pci_dev;
  298. struct cx18_buffer *buf;
  299. list_for_each_entry(buf, &mdl->buf_list, list)
  300. pci_dma_sync_single_for_device(pci_dev, buf->dma_handle,
  301. buf_size, dma);
  302. }
  303. int cx18_stream_alloc(struct cx18_stream *s)
  304. {
  305. struct cx18 *cx = s->cx;
  306. int i;
  307. if (s->buffers == 0)
  308. return 0;
  309. CX18_DEBUG_INFO("Allocate %s stream: %d x %d buffers (%d.%02d kB total)\n",
  310. s->name, s->buffers, s->buf_size,
  311. s->buffers * s->buf_size / 1024,
  312. (s->buffers * s->buf_size * 100 / 1024) % 100);
  313. if (((char __iomem *)&cx->scb->cpu_mdl[cx->free_mdl_idx + s->buffers] -
  314. (char __iomem *)cx->scb) > SCB_RESERVED_SIZE) {
  315. unsigned bufsz = (((char __iomem *)cx->scb) + SCB_RESERVED_SIZE -
  316. ((char __iomem *)cx->scb->cpu_mdl));
  317. CX18_ERR("Too many buffers, cannot fit in SCB area\n");
  318. CX18_ERR("Max buffers = %zu\n",
  319. bufsz / sizeof(struct cx18_mdl_ent));
  320. return -ENOMEM;
  321. }
  322. s->mdl_base_idx = cx->free_mdl_idx;
  323. /* allocate stream buffers and MDLs */
  324. for (i = 0; i < s->buffers; i++) {
  325. struct cx18_mdl *mdl;
  326. struct cx18_buffer *buf;
  327. /* 1 MDL per buffer to handle the worst & also default case */
  328. mdl = kzalloc(sizeof(struct cx18_mdl), GFP_KERNEL|__GFP_NOWARN);
  329. if (mdl == NULL)
  330. break;
  331. buf = kzalloc(sizeof(struct cx18_buffer),
  332. GFP_KERNEL|__GFP_NOWARN);
  333. if (buf == NULL) {
  334. kfree(mdl);
  335. break;
  336. }
  337. buf->buf = kmalloc(s->buf_size, GFP_KERNEL|__GFP_NOWARN);
  338. if (buf->buf == NULL) {
  339. kfree(mdl);
  340. kfree(buf);
  341. break;
  342. }
  343. INIT_LIST_HEAD(&mdl->list);
  344. INIT_LIST_HEAD(&mdl->buf_list);
  345. mdl->id = s->mdl_base_idx; /* a somewhat safe value */
  346. cx18_enqueue(s, mdl, &s->q_idle);
  347. INIT_LIST_HEAD(&buf->list);
  348. buf->dma_handle = pci_map_single(s->cx->pci_dev,
  349. buf->buf, s->buf_size, s->dma);
  350. cx18_buf_sync_for_cpu(s, buf);
  351. list_add_tail(&buf->list, &s->buf_pool);
  352. }
  353. if (i == s->buffers) {
  354. cx->free_mdl_idx += s->buffers;
  355. return 0;
  356. }
  357. CX18_ERR("Couldn't allocate buffers for %s stream\n", s->name);
  358. cx18_stream_free(s);
  359. return -ENOMEM;
  360. }
  361. void cx18_stream_free(struct cx18_stream *s)
  362. {
  363. struct cx18_mdl *mdl;
  364. struct cx18_buffer *buf;
  365. struct cx18 *cx = s->cx;
  366. CX18_DEBUG_INFO("Deallocating buffers for %s stream\n", s->name);
  367. /* move all buffers to buf_pool and all MDLs to q_idle */
  368. cx18_unload_queues(s);
  369. /* empty q_idle */
  370. while ((mdl = cx18_dequeue(s, &s->q_idle)))
  371. kfree(mdl);
  372. /* empty buf_pool */
  373. while (!list_empty(&s->buf_pool)) {
  374. buf = list_first_entry(&s->buf_pool, struct cx18_buffer, list);
  375. list_del_init(&buf->list);
  376. pci_unmap_single(s->cx->pci_dev, buf->dma_handle,
  377. s->buf_size, s->dma);
  378. kfree(buf->buf);
  379. kfree(buf);
  380. }
  381. }