Linux 4.19.133
[linux/fpc-iii.git] / drivers / media / platform / rockchip / rga / rga-buf.c
blob356821c2dacf024d06fbf23f9c86573ad78b5d84
1 /*
2 * Copyright (C) 2017 Fuzhou Rockchip Electronics Co.Ltd
3 * Author: Jacob Chen <jacob-chen@iotwrt.com>
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.
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.
15 #include <linux/pm_runtime.h>
17 #include <media/v4l2-device.h>
18 #include <media/v4l2-ioctl.h>
19 #include <media/v4l2-mem2mem.h>
20 #include <media/videobuf2-dma-sg.h>
21 #include <media/videobuf2-v4l2.h>
23 #include "rga-hw.h"
24 #include "rga.h"
26 static int
27 rga_queue_setup(struct vb2_queue *vq,
28 unsigned int *nbuffers, unsigned int *nplanes,
29 unsigned int sizes[], struct device *alloc_devs[])
31 struct rga_ctx *ctx = vb2_get_drv_priv(vq);
32 struct rga_frame *f = rga_get_frame(ctx, vq->type);
34 if (IS_ERR(f))
35 return PTR_ERR(f);
37 if (*nplanes)
38 return sizes[0] < f->size ? -EINVAL : 0;
40 sizes[0] = f->size;
41 *nplanes = 1;
43 return 0;
46 static int rga_buf_prepare(struct vb2_buffer *vb)
48 struct rga_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
49 struct rga_frame *f = rga_get_frame(ctx, vb->vb2_queue->type);
51 if (IS_ERR(f))
52 return PTR_ERR(f);
54 vb2_set_plane_payload(vb, 0, f->size);
56 return 0;
59 static void rga_buf_queue(struct vb2_buffer *vb)
61 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
62 struct rga_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
64 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
67 static void rga_buf_return_buffers(struct vb2_queue *q,
68 enum vb2_buffer_state state)
70 struct rga_ctx *ctx = vb2_get_drv_priv(q);
71 struct vb2_v4l2_buffer *vbuf;
73 for (;;) {
74 if (V4L2_TYPE_IS_OUTPUT(q->type))
75 vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
76 else
77 vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
78 if (!vbuf)
79 break;
80 v4l2_m2m_buf_done(vbuf, state);
84 static int rga_buf_start_streaming(struct vb2_queue *q, unsigned int count)
86 struct rga_ctx *ctx = vb2_get_drv_priv(q);
87 struct rockchip_rga *rga = ctx->rga;
88 int ret;
90 ret = pm_runtime_get_sync(rga->dev);
91 if (ret < 0) {
92 rga_buf_return_buffers(q, VB2_BUF_STATE_QUEUED);
93 return ret;
96 return 0;
99 static void rga_buf_stop_streaming(struct vb2_queue *q)
101 struct rga_ctx *ctx = vb2_get_drv_priv(q);
102 struct rockchip_rga *rga = ctx->rga;
104 rga_buf_return_buffers(q, VB2_BUF_STATE_ERROR);
105 pm_runtime_put(rga->dev);
108 const struct vb2_ops rga_qops = {
109 .queue_setup = rga_queue_setup,
110 .buf_prepare = rga_buf_prepare,
111 .buf_queue = rga_buf_queue,
112 .wait_prepare = vb2_ops_wait_prepare,
113 .wait_finish = vb2_ops_wait_finish,
114 .start_streaming = rga_buf_start_streaming,
115 .stop_streaming = rga_buf_stop_streaming,
118 /* RGA MMU is a 1-Level MMU, so it can't be used through the IOMMU API.
119 * We use it more like a scatter-gather list.
121 void rga_buf_map(struct vb2_buffer *vb)
123 struct rga_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
124 struct rockchip_rga *rga = ctx->rga;
125 struct sg_table *sgt;
126 struct scatterlist *sgl;
127 unsigned int *pages;
128 unsigned int address, len, i, p;
129 unsigned int mapped_size = 0;
131 if (vb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
132 pages = rga->src_mmu_pages;
133 else
134 pages = rga->dst_mmu_pages;
136 /* Create local MMU table for RGA */
137 sgt = vb2_plane_cookie(vb, 0);
139 for_each_sg(sgt->sgl, sgl, sgt->nents, i) {
140 len = sg_dma_len(sgl) >> PAGE_SHIFT;
141 address = sg_phys(sgl);
143 for (p = 0; p < len; p++) {
144 dma_addr_t phys = address +
145 ((dma_addr_t)p << PAGE_SHIFT);
147 pages[mapped_size + p] = phys;
150 mapped_size += len;
153 /* sync local MMU table for RGA */
154 dma_sync_single_for_device(rga->dev, virt_to_phys(pages),
155 8 * PAGE_SIZE, DMA_BIDIRECTIONAL);