rga-buf.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (C) 2017 Fuzhou Rockchip Electronics Co.Ltd
  3. * Author: Jacob Chen <jacob-chen@iotwrt.com>
  4. *
  5. * This software is licensed under the terms of the GNU General Public
  6. * License version 2, as published by the Free Software Foundation, and
  7. * may be copied, distributed, and modified under those terms.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/pm_runtime.h>
  15. #include <media/v4l2-device.h>
  16. #include <media/v4l2-ioctl.h>
  17. #include <media/v4l2-mem2mem.h>
  18. #include <media/videobuf2-dma-sg.h>
  19. #include <media/videobuf2-v4l2.h>
  20. #include "rga-hw.h"
  21. #include "rga.h"
  22. static int
  23. rga_queue_setup(struct vb2_queue *vq,
  24. unsigned int *nbuffers, unsigned int *nplanes,
  25. unsigned int sizes[], struct device *alloc_devs[])
  26. {
  27. struct rga_ctx *ctx = vb2_get_drv_priv(vq);
  28. struct rga_frame *f = rga_get_frame(ctx, vq->type);
  29. if (IS_ERR(f))
  30. return PTR_ERR(f);
  31. if (*nplanes)
  32. return sizes[0] < f->size ? -EINVAL : 0;
  33. sizes[0] = f->size;
  34. *nplanes = 1;
  35. return 0;
  36. }
  37. static int rga_buf_prepare(struct vb2_buffer *vb)
  38. {
  39. struct rga_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  40. struct rga_frame *f = rga_get_frame(ctx, vb->vb2_queue->type);
  41. if (IS_ERR(f))
  42. return PTR_ERR(f);
  43. vb2_set_plane_payload(vb, 0, f->size);
  44. return 0;
  45. }
  46. static void rga_buf_queue(struct vb2_buffer *vb)
  47. {
  48. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  49. struct rga_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  50. v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
  51. }
  52. static void rga_buf_return_buffers(struct vb2_queue *q,
  53. enum vb2_buffer_state state)
  54. {
  55. struct rga_ctx *ctx = vb2_get_drv_priv(q);
  56. struct vb2_v4l2_buffer *vbuf;
  57. for (;;) {
  58. if (V4L2_TYPE_IS_OUTPUT(q->type))
  59. vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
  60. else
  61. vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
  62. if (!vbuf)
  63. break;
  64. v4l2_m2m_buf_done(vbuf, state);
  65. }
  66. }
  67. static int rga_buf_start_streaming(struct vb2_queue *q, unsigned int count)
  68. {
  69. struct rga_ctx *ctx = vb2_get_drv_priv(q);
  70. struct rockchip_rga *rga = ctx->rga;
  71. int ret;
  72. ret = pm_runtime_get_sync(rga->dev);
  73. if (ret < 0) {
  74. rga_buf_return_buffers(q, VB2_BUF_STATE_QUEUED);
  75. return ret;
  76. }
  77. return 0;
  78. }
  79. static void rga_buf_stop_streaming(struct vb2_queue *q)
  80. {
  81. struct rga_ctx *ctx = vb2_get_drv_priv(q);
  82. struct rockchip_rga *rga = ctx->rga;
  83. rga_buf_return_buffers(q, VB2_BUF_STATE_ERROR);
  84. pm_runtime_put(rga->dev);
  85. }
  86. const struct vb2_ops rga_qops = {
  87. .queue_setup = rga_queue_setup,
  88. .buf_prepare = rga_buf_prepare,
  89. .buf_queue = rga_buf_queue,
  90. .wait_prepare = vb2_ops_wait_prepare,
  91. .wait_finish = vb2_ops_wait_finish,
  92. .start_streaming = rga_buf_start_streaming,
  93. .stop_streaming = rga_buf_stop_streaming,
  94. };
  95. /* RGA MMU is a 1-Level MMU, so it can't be used through the IOMMU API.
  96. * We use it more like a scatter-gather list.
  97. */
  98. void rga_buf_map(struct vb2_buffer *vb)
  99. {
  100. struct rga_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  101. struct rockchip_rga *rga = ctx->rga;
  102. struct sg_table *sgt;
  103. struct scatterlist *sgl;
  104. unsigned int *pages;
  105. unsigned int address, len, i, p;
  106. unsigned int mapped_size = 0;
  107. if (vb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  108. pages = rga->src_mmu_pages;
  109. else
  110. pages = rga->dst_mmu_pages;
  111. /* Create local MMU table for RGA */
  112. sgt = vb2_plane_cookie(vb, 0);
  113. for_each_sg(sgt->sgl, sgl, sgt->nents, i) {
  114. len = sg_dma_len(sgl) >> PAGE_SHIFT;
  115. address = sg_phys(sgl);
  116. for (p = 0; p < len; p++) {
  117. dma_addr_t phys = address +
  118. ((dma_addr_t)p << PAGE_SHIFT);
  119. pages[mapped_size + p] = phys;
  120. }
  121. mapped_size += len;
  122. }
  123. /* sync local MMU table for RGA */
  124. dma_sync_single_for_device(rga->dev, virt_to_phys(pages),
  125. 8 * PAGE_SIZE, DMA_BIDIRECTIONAL);
  126. }