PageRenderTime 947ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/media/pci/cx18/cx18-queue.c

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