1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2020-2021 NXP
6 #include <linux/init.h>
7 #include <linux/interconnect.h>
8 #include <linux/ioctl.h>
9 #include <linux/list.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
16 #include "vpu_helpers.h"
18 int vpu_helper_find_in_array_u8(const u8 *array, u32 size, u32 x)
22 for (i = 0; i < size; i++) {
30 bool vpu_helper_check_type(struct vpu_inst *inst, u32 type)
32 const struct vpu_format *pfmt;
34 for (pfmt = inst->formats; pfmt->pixfmt; pfmt++) {
35 if (!vpu_iface_check_format(inst, pfmt->pixfmt))
37 if (pfmt->type == type)
44 const struct vpu_format *vpu_helper_find_format(struct vpu_inst *inst, u32 type, u32 pixelfmt)
46 const struct vpu_format *pfmt;
48 if (!inst || !inst->formats)
51 if (!vpu_iface_check_format(inst, pixelfmt))
54 for (pfmt = inst->formats; pfmt->pixfmt; pfmt++) {
55 if (pfmt->pixfmt == pixelfmt && (!type || type == pfmt->type))
62 const struct vpu_format *vpu_helper_enum_format(struct vpu_inst *inst, u32 type, int index)
64 const struct vpu_format *pfmt;
67 if (!inst || !inst->formats)
70 for (pfmt = inst->formats; pfmt->pixfmt; pfmt++) {
71 if (!vpu_iface_check_format(inst, pfmt->pixfmt))
74 if (pfmt->type == type) {
84 u32 vpu_helper_valid_frame_width(struct vpu_inst *inst, u32 width)
86 const struct vpu_core_resources *res;
91 res = vpu_get_resource(inst);
95 width = clamp(width, res->min_width, res->max_width);
97 width = ALIGN(width, res->step_width);
102 u32 vpu_helper_valid_frame_height(struct vpu_inst *inst, u32 height)
104 const struct vpu_core_resources *res;
109 res = vpu_get_resource(inst);
113 height = clamp(height, res->min_height, res->max_height);
114 if (res->step_height)
115 height = ALIGN(height, res->step_height);
120 static u32 get_nv12_plane_size(u32 width, u32 height, int plane_no,
121 u32 stride, u32 interlaced, u32 *pbl)
126 bytesperline = ALIGN(width, stride);
128 bytesperline = max(bytesperline, *pbl);
129 height = ALIGN(height, 2);
131 size = bytesperline * height;
132 else if (plane_no == 1)
133 size = bytesperline * height >> 1;
140 static u32 get_tiled_8l128_plane_size(u32 fmt, u32 width, u32 height, int plane_no,
141 u32 stride, u32 interlaced, u32 *pbl)
151 if (fmt == V4L2_PIX_FMT_NV12M_10BE_8L128)
153 bytesperline = DIV_ROUND_UP(width * bitdepth, BITS_PER_BYTE);
154 bytesperline = ALIGN(bytesperline, 1 << ws);
155 bytesperline = ALIGN(bytesperline, stride);
157 bytesperline = max(bytesperline, *pbl);
158 height = ALIGN(height, 1 << hs);
160 size = bytesperline * height;
161 else if (plane_no == 1)
162 size = (bytesperline * ALIGN(height, 1 << (hs + 1))) >> 1;
169 static u32 get_default_plane_size(u32 width, u32 height, int plane_no,
170 u32 stride, u32 interlaced, u32 *pbl)
175 bytesperline = ALIGN(width, stride);
177 bytesperline = max(bytesperline, *pbl);
179 size = bytesperline * height;
186 u32 vpu_helper_get_plane_size(u32 fmt, u32 w, u32 h, int plane_no,
187 u32 stride, u32 interlaced, u32 *pbl)
190 case V4L2_PIX_FMT_NV12M:
191 return get_nv12_plane_size(w, h, plane_no, stride, interlaced, pbl);
192 case V4L2_PIX_FMT_NV12M_8L128:
193 case V4L2_PIX_FMT_NV12M_10BE_8L128:
194 return get_tiled_8l128_plane_size(fmt, w, h, plane_no, stride, interlaced, pbl);
196 return get_default_plane_size(w, h, plane_no, stride, interlaced, pbl);
200 int vpu_helper_copy_from_stream_buffer(struct vpu_buffer *stream_buffer,
201 u32 *rptr, u32 size, void *dst)
208 if (!stream_buffer || !rptr || !dst)
215 start = stream_buffer->phys;
216 end = start + stream_buffer->length;
217 virt = stream_buffer->virt;
219 if (offset < start || offset > end)
222 if (offset + size <= end) {
223 memcpy(dst, virt + (offset - start), size);
225 memcpy(dst, virt + (offset - start), end - offset);
226 memcpy(dst + end - offset, virt, size + offset - end);
229 *rptr = vpu_helper_step_walk(stream_buffer, offset, size);
234 int vpu_helper_copy_to_stream_buffer(struct vpu_buffer *stream_buffer,
235 u32 *wptr, u32 size, void *src)
242 if (!stream_buffer || !wptr || !src)
249 start = stream_buffer->phys;
250 end = start + stream_buffer->length;
251 virt = stream_buffer->virt;
252 if (offset < start || offset > end)
255 if (offset + size <= end) {
256 memcpy(virt + (offset - start), src, size);
258 memcpy(virt + (offset - start), src, end - offset);
259 memcpy(virt, src + end - offset, size + offset - end);
262 *wptr = vpu_helper_step_walk(stream_buffer, offset, size);
267 int vpu_helper_memset_stream_buffer(struct vpu_buffer *stream_buffer,
268 u32 *wptr, u8 val, u32 size)
275 if (!stream_buffer || !wptr)
282 start = stream_buffer->phys;
283 end = start + stream_buffer->length;
284 virt = stream_buffer->virt;
285 if (offset < start || offset > end)
288 if (offset + size <= end) {
289 memset(virt + (offset - start), val, size);
291 memset(virt + (offset - start), val, end - offset);
292 memset(virt, val, size + offset - end);
297 offset -= stream_buffer->length;
304 u32 vpu_helper_get_free_space(struct vpu_inst *inst)
306 struct vpu_rpc_buffer_desc desc;
308 if (vpu_iface_get_stream_buffer_desc(inst, &desc))
311 if (desc.rptr > desc.wptr)
312 return desc.rptr - desc.wptr;
313 else if (desc.rptr < desc.wptr)
314 return (desc.end - desc.start + desc.rptr - desc.wptr);
316 return desc.end - desc.start;
319 u32 vpu_helper_get_used_space(struct vpu_inst *inst)
321 struct vpu_rpc_buffer_desc desc;
323 if (vpu_iface_get_stream_buffer_desc(inst, &desc))
326 if (desc.wptr > desc.rptr)
327 return desc.wptr - desc.rptr;
328 else if (desc.wptr < desc.rptr)
329 return (desc.end - desc.start + desc.wptr - desc.rptr);
334 int vpu_helper_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
336 struct vpu_inst *inst = ctrl_to_inst(ctrl);
339 case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
340 ctrl->val = inst->min_buffer_cap;
342 case V4L2_CID_MIN_BUFFERS_FOR_OUTPUT:
343 ctrl->val = inst->min_buffer_out;
352 int vpu_helper_find_startcode(struct vpu_buffer *stream_buffer,
353 u32 pixelformat, u32 offset, u32 bytesused)
361 if (!stream_buffer || !stream_buffer->virt)
364 switch (pixelformat) {
365 case V4L2_PIX_FMT_H264:
367 start_code = 0x00000001;
373 for (i = 0; i < bytesused; i++) {
374 val = (val << 8) | vpu_helper_read_byte(stream_buffer, offset + i);
375 if (i < start_code_size - 1)
377 if (val == start_code) {
378 ret = i + 1 - start_code_size;
386 int vpu_find_dst_by_src(struct vpu_pair *pairs, u32 cnt, u32 src)
393 for (i = 0; i < cnt; i++) {
394 if (pairs[i].src == src)
401 int vpu_find_src_by_dst(struct vpu_pair *pairs, u32 cnt, u32 dst)
408 for (i = 0; i < cnt; i++) {
409 if (pairs[i].dst == dst)