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_find_sibling(struct vpu_inst *inst, u32 type, u32 pixelfmt)
64 const struct vpu_format *fmt;
65 const struct vpu_format *sibling;
67 fmt = vpu_helper_find_format(inst, type, pixelfmt);
68 if (!fmt || !fmt->sibling)
71 sibling = vpu_helper_find_format(inst, type, fmt->sibling);
72 if (!sibling || sibling->sibling != fmt->pixfmt ||
73 sibling->comp_planes != fmt->comp_planes)
79 bool vpu_helper_match_format(struct vpu_inst *inst, u32 type, u32 fmta, u32 fmtb)
81 const struct vpu_format *sibling;
86 sibling = vpu_helper_find_sibling(inst, type, fmta);
87 if (sibling && sibling->pixfmt == fmtb)
92 const struct vpu_format *vpu_helper_enum_format(struct vpu_inst *inst, u32 type, int index)
94 const struct vpu_format *pfmt;
97 if (!inst || !inst->formats)
100 for (pfmt = inst->formats; pfmt->pixfmt; pfmt++) {
101 if (!vpu_iface_check_format(inst, pfmt->pixfmt))
104 if (pfmt->type == type) {
114 u32 vpu_helper_valid_frame_width(struct vpu_inst *inst, u32 width)
116 const struct vpu_core_resources *res;
121 res = vpu_get_resource(inst);
125 width = clamp(width, res->min_width, res->max_width);
127 width = ALIGN(width, res->step_width);
132 u32 vpu_helper_valid_frame_height(struct vpu_inst *inst, u32 height)
134 const struct vpu_core_resources *res;
139 res = vpu_get_resource(inst);
143 height = clamp(height, res->min_height, res->max_height);
144 if (res->step_height)
145 height = ALIGN(height, res->step_height);
150 static u32 get_nv12_plane_size(u32 width, u32 height, int plane_no,
151 u32 stride, u32 interlaced, u32 *pbl)
156 bytesperline = width;
158 bytesperline = max(bytesperline, *pbl);
159 bytesperline = ALIGN(bytesperline, stride);
160 height = ALIGN(height, 2);
162 size = bytesperline * height;
163 else if (plane_no == 1)
164 size = bytesperline * height >> 1;
171 static u32 get_tiled_8l128_plane_size(u32 fmt, u32 width, u32 height, int plane_no,
172 u32 stride, u32 interlaced, u32 *pbl)
182 if (fmt == V4L2_PIX_FMT_NV12M_10BE_8L128 || fmt == V4L2_PIX_FMT_NV12_10BE_8L128)
184 bytesperline = DIV_ROUND_UP(width * bitdepth, BITS_PER_BYTE);
186 bytesperline = max(bytesperline, *pbl);
187 bytesperline = ALIGN(bytesperline, 1 << ws);
188 bytesperline = ALIGN(bytesperline, stride);
189 height = ALIGN(height, 1 << hs);
191 size = bytesperline * height;
192 else if (plane_no == 1)
193 size = (bytesperline * ALIGN(height, 1 << (hs + 1))) >> 1;
200 static u32 get_default_plane_size(u32 width, u32 height, int plane_no,
201 u32 stride, u32 interlaced, u32 *pbl)
206 bytesperline = width;
208 bytesperline = max(bytesperline, *pbl);
209 bytesperline = ALIGN(bytesperline, stride);
211 size = bytesperline * height;
218 u32 vpu_helper_get_plane_size(u32 fmt, u32 w, u32 h, int plane_no,
219 u32 stride, u32 interlaced, u32 *pbl)
222 case V4L2_PIX_FMT_NV12:
223 case V4L2_PIX_FMT_NV12M:
224 return get_nv12_plane_size(w, h, plane_no, stride, interlaced, pbl);
225 case V4L2_PIX_FMT_NV12_8L128:
226 case V4L2_PIX_FMT_NV12M_8L128:
227 case V4L2_PIX_FMT_NV12_10BE_8L128:
228 case V4L2_PIX_FMT_NV12M_10BE_8L128:
229 return get_tiled_8l128_plane_size(fmt, w, h, plane_no, stride, interlaced, pbl);
231 return get_default_plane_size(w, h, plane_no, stride, interlaced, pbl);
235 int vpu_helper_copy_from_stream_buffer(struct vpu_buffer *stream_buffer,
236 u32 *rptr, u32 size, void *dst)
243 if (!stream_buffer || !rptr || !dst)
250 start = stream_buffer->phys;
251 end = start + stream_buffer->length;
252 virt = stream_buffer->virt;
254 if (offset < start || offset > end)
257 if (offset + size <= end) {
258 memcpy(dst, virt + (offset - start), size);
260 memcpy(dst, virt + (offset - start), end - offset);
261 memcpy(dst + end - offset, virt, size + offset - end);
264 *rptr = vpu_helper_step_walk(stream_buffer, offset, size);
269 int vpu_helper_copy_to_stream_buffer(struct vpu_buffer *stream_buffer,
270 u32 *wptr, u32 size, void *src)
277 if (!stream_buffer || !wptr || !src)
284 start = stream_buffer->phys;
285 end = start + stream_buffer->length;
286 virt = stream_buffer->virt;
287 if (offset < start || offset > end)
290 if (offset + size <= end) {
291 memcpy(virt + (offset - start), src, size);
293 memcpy(virt + (offset - start), src, end - offset);
294 memcpy(virt, src + end - offset, size + offset - end);
297 *wptr = vpu_helper_step_walk(stream_buffer, offset, size);
302 int vpu_helper_memset_stream_buffer(struct vpu_buffer *stream_buffer,
303 u32 *wptr, u8 val, u32 size)
310 if (!stream_buffer || !wptr)
317 start = stream_buffer->phys;
318 end = start + stream_buffer->length;
319 virt = stream_buffer->virt;
320 if (offset < start || offset > end)
323 if (offset + size <= end) {
324 memset(virt + (offset - start), val, size);
326 memset(virt + (offset - start), val, end - offset);
327 memset(virt, val, size + offset - end);
332 offset -= stream_buffer->length;
339 u32 vpu_helper_get_free_space(struct vpu_inst *inst)
341 struct vpu_rpc_buffer_desc desc;
343 if (vpu_iface_get_stream_buffer_desc(inst, &desc))
346 if (desc.rptr > desc.wptr)
347 return desc.rptr - desc.wptr;
348 else if (desc.rptr < desc.wptr)
349 return (desc.end - desc.start + desc.rptr - desc.wptr);
351 return desc.end - desc.start;
354 u32 vpu_helper_get_used_space(struct vpu_inst *inst)
356 struct vpu_rpc_buffer_desc desc;
358 if (vpu_iface_get_stream_buffer_desc(inst, &desc))
361 if (desc.wptr > desc.rptr)
362 return desc.wptr - desc.rptr;
363 else if (desc.wptr < desc.rptr)
364 return (desc.end - desc.start + desc.wptr - desc.rptr);
369 int vpu_helper_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
371 struct vpu_inst *inst = ctrl_to_inst(ctrl);
374 case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
375 ctrl->val = inst->min_buffer_cap;
377 case V4L2_CID_MIN_BUFFERS_FOR_OUTPUT:
378 ctrl->val = inst->min_buffer_out;
387 int vpu_helper_find_startcode(struct vpu_buffer *stream_buffer,
388 u32 pixelformat, u32 offset, u32 bytesused)
396 if (!stream_buffer || !stream_buffer->virt)
399 switch (pixelformat) {
400 case V4L2_PIX_FMT_H264:
402 start_code = 0x00000001;
408 for (i = 0; i < bytesused; i++) {
409 val = (val << 8) | vpu_helper_read_byte(stream_buffer, offset + i);
410 if (i < start_code_size - 1)
412 if (val == start_code) {
413 ret = i + 1 - start_code_size;
421 int vpu_find_dst_by_src(struct vpu_pair *pairs, u32 cnt, u32 src)
428 for (i = 0; i < cnt; i++) {
429 if (pairs[i].src == src)
436 int vpu_find_src_by_dst(struct vpu_pair *pairs, u32 cnt, u32 dst)
443 for (i = 0; i < cnt; i++) {
444 if (pairs[i].dst == dst)