cx18-queue.c 12 KB

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