1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright 2020-2021 NXP 4 */ 5 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> 13 #include "vpu.h" 14 #include "vpu_core.h" 15 #include "vpu_rpc.h" 16 #include "vpu_helpers.h" 17 18 int vpu_helper_find_in_array_u8(const u8 *array, u32 size, u32 x) 19 { 20 int i; 21 22 for (i = 0; i < size; i++) { 23 if (array[i] == x) 24 return i; 25 } 26 27 return 0; 28 } 29 30 bool vpu_helper_check_type(struct vpu_inst *inst, u32 type) 31 { 32 const struct vpu_format *pfmt; 33 34 for (pfmt = inst->formats; pfmt->pixfmt; pfmt++) { 35 if (!vpu_iface_check_format(inst, pfmt->pixfmt)) 36 continue; 37 if (pfmt->type == type) 38 return true; 39 } 40 41 return false; 42 } 43 44 const struct vpu_format *vpu_helper_find_format(struct vpu_inst *inst, u32 type, u32 pixelfmt) 45 { 46 const struct vpu_format *pfmt; 47 48 if (!inst || !inst->formats) 49 return NULL; 50 51 if (!vpu_iface_check_format(inst, pixelfmt)) 52 return NULL; 53 54 for (pfmt = inst->formats; pfmt->pixfmt; pfmt++) { 55 if (pfmt->pixfmt == pixelfmt && (!type || type == pfmt->type)) 56 return pfmt; 57 } 58 59 return NULL; 60 } 61 62 const struct vpu_format *vpu_helper_find_sibling(struct vpu_inst *inst, u32 type, u32 pixelfmt) 63 { 64 const struct vpu_format *fmt; 65 const struct vpu_format *sibling; 66 67 fmt = vpu_helper_find_format(inst, type, pixelfmt); 68 if (!fmt || !fmt->sibling) 69 return NULL; 70 71 sibling = vpu_helper_find_format(inst, type, fmt->sibling); 72 if (!sibling || sibling->sibling != fmt->pixfmt || 73 sibling->comp_planes != fmt->comp_planes) 74 return NULL; 75 76 return sibling; 77 } 78 79 bool vpu_helper_match_format(struct vpu_inst *inst, u32 type, u32 fmta, u32 fmtb) 80 { 81 const struct vpu_format *sibling; 82 83 if (fmta == fmtb) 84 return true; 85 86 sibling = vpu_helper_find_sibling(inst, type, fmta); 87 if (sibling && sibling->pixfmt == fmtb) 88 return true; 89 return false; 90 } 91 92 const struct vpu_format *vpu_helper_enum_format(struct vpu_inst *inst, u32 type, int index) 93 { 94 const struct vpu_format *pfmt; 95 int i = 0; 96 97 if (!inst || !inst->formats) 98 return NULL; 99 100 for (pfmt = inst->formats; pfmt->pixfmt; pfmt++) { 101 if (!vpu_iface_check_format(inst, pfmt->pixfmt)) 102 continue; 103 104 if (pfmt->type == type) { 105 if (index == i) 106 return pfmt; 107 i++; 108 } 109 } 110 111 return NULL; 112 } 113 114 u32 vpu_helper_valid_frame_width(struct vpu_inst *inst, u32 width) 115 { 116 const struct vpu_core_resources *res; 117 118 if (!inst) 119 return width; 120 121 res = vpu_get_resource(inst); 122 if (!res) 123 return width; 124 if (res->max_width) 125 width = clamp(width, res->min_width, res->max_width); 126 if (res->step_width) 127 width = ALIGN(width, res->step_width); 128 129 return width; 130 } 131 132 u32 vpu_helper_valid_frame_height(struct vpu_inst *inst, u32 height) 133 { 134 const struct vpu_core_resources *res; 135 136 if (!inst) 137 return height; 138 139 res = vpu_get_resource(inst); 140 if (!res) 141 return height; 142 if (res->max_height) 143 height = clamp(height, res->min_height, res->max_height); 144 if (res->step_height) 145 height = ALIGN(height, res->step_height); 146 147 return height; 148 } 149 150 static u32 get_nv12_plane_size(u32 width, u32 height, int plane_no, 151 u32 stride, u32 interlaced, u32 *pbl) 152 { 153 u32 bytesperline; 154 u32 size = 0; 155 156 bytesperline = width; 157 if (pbl) 158 bytesperline = max(bytesperline, *pbl); 159 bytesperline = ALIGN(bytesperline, stride); 160 height = ALIGN(height, 2); 161 if (plane_no == 0) 162 size = bytesperline * height; 163 else if (plane_no == 1) 164 size = bytesperline * height >> 1; 165 if (pbl) 166 *pbl = bytesperline; 167 168 return size; 169 } 170 171 static u32 get_tiled_8l128_plane_size(u32 fmt, u32 width, u32 height, int plane_no, 172 u32 stride, u32 interlaced, u32 *pbl) 173 { 174 u32 ws = 3; 175 u32 hs = 7; 176 u32 bitdepth = 8; 177 u32 bytesperline; 178 u32 size = 0; 179 180 if (interlaced) 181 hs++; 182 if (fmt == V4L2_PIX_FMT_NV12M_10BE_8L128 || fmt == V4L2_PIX_FMT_NV12_10BE_8L128) 183 bitdepth = 10; 184 bytesperline = DIV_ROUND_UP(width * bitdepth, BITS_PER_BYTE); 185 if (pbl) 186 bytesperline = max(bytesperline, *pbl); 187 bytesperline = ALIGN(bytesperline, 1 << ws); 188 bytesperline = ALIGN(bytesperline, stride); 189 height = ALIGN(height, 1 << hs); 190 if (plane_no == 0) 191 size = bytesperline * height; 192 else if (plane_no == 1) 193 size = (bytesperline * ALIGN(height, 1 << (hs + 1))) >> 1; 194 if (pbl) 195 *pbl = bytesperline; 196 197 return size; 198 } 199 200 static u32 get_default_plane_size(u32 width, u32 height, int plane_no, 201 u32 stride, u32 interlaced, u32 *pbl) 202 { 203 u32 bytesperline; 204 u32 size = 0; 205 206 bytesperline = width; 207 if (pbl) 208 bytesperline = max(bytesperline, *pbl); 209 bytesperline = ALIGN(bytesperline, stride); 210 if (plane_no == 0) 211 size = bytesperline * height; 212 if (pbl) 213 *pbl = bytesperline; 214 215 return size; 216 } 217 218 u32 vpu_helper_get_plane_size(u32 fmt, u32 w, u32 h, int plane_no, 219 u32 stride, u32 interlaced, u32 *pbl) 220 { 221 switch (fmt) { 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); 230 default: 231 return get_default_plane_size(w, h, plane_no, stride, interlaced, pbl); 232 } 233 } 234 235 int vpu_helper_copy_from_stream_buffer(struct vpu_buffer *stream_buffer, 236 u32 *rptr, u32 size, void *dst) 237 { 238 u32 offset; 239 u32 start; 240 u32 end; 241 void *virt; 242 243 if (!stream_buffer || !rptr || !dst) 244 return -EINVAL; 245 246 if (!size) 247 return 0; 248 249 offset = *rptr; 250 start = stream_buffer->phys; 251 end = start + stream_buffer->length; 252 virt = stream_buffer->virt; 253 254 if (offset < start || offset > end) 255 return -EINVAL; 256 257 if (offset + size <= end) { 258 memcpy(dst, virt + (offset - start), size); 259 } else { 260 memcpy(dst, virt + (offset - start), end - offset); 261 memcpy(dst + end - offset, virt, size + offset - end); 262 } 263 264 *rptr = vpu_helper_step_walk(stream_buffer, offset, size); 265 266 return 0; 267 } 268 269 int vpu_helper_copy_to_stream_buffer(struct vpu_buffer *stream_buffer, 270 u32 *wptr, u32 size, void *src) 271 { 272 u32 offset; 273 u32 start; 274 u32 end; 275 void *virt; 276 277 if (!stream_buffer || !wptr || !src) 278 return -EINVAL; 279 280 if (!size) 281 return 0; 282 283 offset = *wptr; 284 start = stream_buffer->phys; 285 end = start + stream_buffer->length; 286 virt = stream_buffer->virt; 287 if (offset < start || offset > end) 288 return -EINVAL; 289 290 if (offset + size <= end) { 291 memcpy(virt + (offset - start), src, size); 292 } else { 293 memcpy(virt + (offset - start), src, end - offset); 294 memcpy(virt, src + end - offset, size + offset - end); 295 } 296 297 *wptr = vpu_helper_step_walk(stream_buffer, offset, size); 298 299 return 0; 300 } 301 302 int vpu_helper_memset_stream_buffer(struct vpu_buffer *stream_buffer, 303 u32 *wptr, u8 val, u32 size) 304 { 305 u32 offset; 306 u32 start; 307 u32 end; 308 void *virt; 309 310 if (!stream_buffer || !wptr) 311 return -EINVAL; 312 313 if (!size) 314 return 0; 315 316 offset = *wptr; 317 start = stream_buffer->phys; 318 end = start + stream_buffer->length; 319 virt = stream_buffer->virt; 320 if (offset < start || offset > end) 321 return -EINVAL; 322 323 if (offset + size <= end) { 324 memset(virt + (offset - start), val, size); 325 } else { 326 memset(virt + (offset - start), val, end - offset); 327 memset(virt, val, size + offset - end); 328 } 329 330 offset += size; 331 if (offset >= end) 332 offset -= stream_buffer->length; 333 334 *wptr = offset; 335 336 return 0; 337 } 338 339 u32 vpu_helper_get_free_space(struct vpu_inst *inst) 340 { 341 struct vpu_rpc_buffer_desc desc; 342 343 if (vpu_iface_get_stream_buffer_desc(inst, &desc)) 344 return 0; 345 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); 350 else 351 return desc.end - desc.start; 352 } 353 354 u32 vpu_helper_get_used_space(struct vpu_inst *inst) 355 { 356 struct vpu_rpc_buffer_desc desc; 357 358 if (vpu_iface_get_stream_buffer_desc(inst, &desc)) 359 return 0; 360 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); 365 else 366 return 0; 367 } 368 369 int vpu_helper_g_volatile_ctrl(struct v4l2_ctrl *ctrl) 370 { 371 struct vpu_inst *inst = ctrl_to_inst(ctrl); 372 373 switch (ctrl->id) { 374 case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE: 375 ctrl->val = inst->min_buffer_cap; 376 break; 377 case V4L2_CID_MIN_BUFFERS_FOR_OUTPUT: 378 ctrl->val = inst->min_buffer_out; 379 break; 380 default: 381 return -EINVAL; 382 } 383 384 return 0; 385 } 386 387 int vpu_helper_find_startcode(struct vpu_buffer *stream_buffer, 388 u32 pixelformat, u32 offset, u32 bytesused) 389 { 390 u32 start_code; 391 int start_code_size; 392 u32 val = 0; 393 int i; 394 int ret = -EINVAL; 395 396 if (!stream_buffer || !stream_buffer->virt) 397 return -EINVAL; 398 399 switch (pixelformat) { 400 case V4L2_PIX_FMT_H264: 401 start_code_size = 4; 402 start_code = 0x00000001; 403 break; 404 default: 405 return 0; 406 } 407 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) 411 continue; 412 if (val == start_code) { 413 ret = i + 1 - start_code_size; 414 break; 415 } 416 } 417 418 return ret; 419 } 420 421 int vpu_find_dst_by_src(struct vpu_pair *pairs, u32 cnt, u32 src) 422 { 423 u32 i; 424 425 if (!pairs || !cnt) 426 return -EINVAL; 427 428 for (i = 0; i < cnt; i++) { 429 if (pairs[i].src == src) 430 return pairs[i].dst; 431 } 432 433 return -EINVAL; 434 } 435 436 int vpu_find_src_by_dst(struct vpu_pair *pairs, u32 cnt, u32 dst) 437 { 438 u32 i; 439 440 if (!pairs || !cnt) 441 return -EINVAL; 442 443 for (i = 0; i < cnt; i++) { 444 if (pairs[i].dst == dst) 445 return pairs[i].src; 446 } 447 448 return -EINVAL; 449 } 450