treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / media / platform / mtk-vcodec / vdec / vdec_h264_if.c
blob50048c170b99cb221833a99ab22c05c59f0e4ee6
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2016 MediaTek Inc.
4 * Author: PC Chen <pc.chen@mediatek.com>
5 */
7 #include <linux/module.h>
8 #include <linux/slab.h>
10 #include "../vdec_drv_if.h"
11 #include "../mtk_vcodec_util.h"
12 #include "../mtk_vcodec_dec.h"
13 #include "../mtk_vcodec_intr.h"
14 #include "../vdec_vpu_if.h"
15 #include "../vdec_drv_base.h"
17 #define NAL_NON_IDR_SLICE 0x01
18 #define NAL_IDR_SLICE 0x05
19 #define NAL_H264_PPS 0x08
20 #define NAL_TYPE(value) ((value) & 0x1F)
22 #define BUF_PREDICTION_SZ (32 * 1024)
24 #define MB_UNIT_LEN 16
26 /* motion vector size (bytes) for every macro block */
27 #define HW_MB_STORE_SZ 64
29 #define H264_MAX_FB_NUM 17
30 #define HDR_PARSING_BUF_SZ 1024
32 #define DEC_ERR_RET(ret) ((ret) >> 16)
33 #define H264_ERR_NOT_VALID 3
35 /**
36 * struct h264_fb - h264 decode frame buffer information
37 * @vdec_fb_va : virtual address of struct vdec_fb
38 * @y_fb_dma : dma address of Y frame buffer (luma)
39 * @c_fb_dma : dma address of C frame buffer (chroma)
40 * @poc : picture order count of frame buffer
41 * @reserved : for 8 bytes alignment
43 struct h264_fb {
44 uint64_t vdec_fb_va;
45 uint64_t y_fb_dma;
46 uint64_t c_fb_dma;
47 int32_t poc;
48 uint32_t reserved;
51 /**
52 * struct h264_ring_fb_list - ring frame buffer list
53 * @fb_list : frame buffer array
54 * @read_idx : read index
55 * @write_idx : write index
56 * @count : buffer count in list
57 * @reserved : for 8 bytes alignment
59 struct h264_ring_fb_list {
60 struct h264_fb fb_list[H264_MAX_FB_NUM];
61 unsigned int read_idx;
62 unsigned int write_idx;
63 unsigned int count;
64 unsigned int reserved;
67 /**
68 * struct vdec_h264_dec_info - decode information
69 * @dpb_sz : decoding picture buffer size
70 * @resolution_changed : resolution change happen
71 * @realloc_mv_buf : flag to notify driver to re-allocate mv buffer
72 * @reserved : for 8 bytes alignment
73 * @bs_dma : Input bit-stream buffer dma address
74 * @y_fb_dma : Y frame buffer dma address
75 * @c_fb_dma : C frame buffer dma address
76 * @vdec_fb_va : VDEC frame buffer struct virtual address
78 struct vdec_h264_dec_info {
79 uint32_t dpb_sz;
80 uint32_t resolution_changed;
81 uint32_t realloc_mv_buf;
82 uint32_t reserved;
83 uint64_t bs_dma;
84 uint64_t y_fb_dma;
85 uint64_t c_fb_dma;
86 uint64_t vdec_fb_va;
89 /**
90 * struct vdec_h264_vsi - shared memory for decode information exchange
91 * between VPU and Host.
92 * The memory is allocated by VPU then mapping to Host
93 * in vpu_dec_init() and freed in vpu_dec_deinit()
94 * by VPU.
95 * AP-W/R : AP is writer/reader on this item
96 * VPU-W/R: VPU is write/reader on this item
97 * @hdr_buf : Header parsing buffer (AP-W, VPU-R)
98 * @pred_buf_dma : HW working predication buffer dma address (AP-W, VPU-R)
99 * @mv_buf_dma : HW working motion vector buffer dma address (AP-W, VPU-R)
100 * @list_free : free frame buffer ring list (AP-W/R, VPU-W)
101 * @list_disp : display frame buffer ring list (AP-R, VPU-W)
102 * @dec : decode information (AP-R, VPU-W)
103 * @pic : picture information (AP-R, VPU-W)
104 * @crop : crop information (AP-R, VPU-W)
106 struct vdec_h264_vsi {
107 unsigned char hdr_buf[HDR_PARSING_BUF_SZ];
108 uint64_t pred_buf_dma;
109 uint64_t mv_buf_dma[H264_MAX_FB_NUM];
110 struct h264_ring_fb_list list_free;
111 struct h264_ring_fb_list list_disp;
112 struct vdec_h264_dec_info dec;
113 struct vdec_pic_info pic;
114 struct v4l2_rect crop;
118 * struct vdec_h264_inst - h264 decoder instance
119 * @num_nalu : how many nalus be decoded
120 * @ctx : point to mtk_vcodec_ctx
121 * @pred_buf : HW working predication buffer
122 * @mv_buf : HW working motion vector buffer
123 * @vpu : VPU instance
124 * @vsi : VPU shared information
126 struct vdec_h264_inst {
127 unsigned int num_nalu;
128 struct mtk_vcodec_ctx *ctx;
129 struct mtk_vcodec_mem pred_buf;
130 struct mtk_vcodec_mem mv_buf[H264_MAX_FB_NUM];
131 struct vdec_vpu_inst vpu;
132 struct vdec_h264_vsi *vsi;
135 static unsigned int get_mv_buf_size(unsigned int width, unsigned int height)
137 return HW_MB_STORE_SZ * (width/MB_UNIT_LEN) * (height/MB_UNIT_LEN);
140 static int allocate_predication_buf(struct vdec_h264_inst *inst)
142 int err = 0;
144 inst->pred_buf.size = BUF_PREDICTION_SZ;
145 err = mtk_vcodec_mem_alloc(inst->ctx, &inst->pred_buf);
146 if (err) {
147 mtk_vcodec_err(inst, "failed to allocate ppl buf");
148 return err;
151 inst->vsi->pred_buf_dma = inst->pred_buf.dma_addr;
152 return 0;
155 static void free_predication_buf(struct vdec_h264_inst *inst)
157 struct mtk_vcodec_mem *mem = NULL;
159 mtk_vcodec_debug_enter(inst);
161 inst->vsi->pred_buf_dma = 0;
162 mem = &inst->pred_buf;
163 if (mem->va)
164 mtk_vcodec_mem_free(inst->ctx, mem);
167 static int alloc_mv_buf(struct vdec_h264_inst *inst, struct vdec_pic_info *pic)
169 int i;
170 int err;
171 struct mtk_vcodec_mem *mem = NULL;
172 unsigned int buf_sz = get_mv_buf_size(pic->buf_w, pic->buf_h);
174 for (i = 0; i < H264_MAX_FB_NUM; i++) {
175 mem = &inst->mv_buf[i];
176 if (mem->va)
177 mtk_vcodec_mem_free(inst->ctx, mem);
178 mem->size = buf_sz;
179 err = mtk_vcodec_mem_alloc(inst->ctx, mem);
180 if (err) {
181 mtk_vcodec_err(inst, "failed to allocate mv buf");
182 return err;
184 inst->vsi->mv_buf_dma[i] = mem->dma_addr;
187 return 0;
190 static void free_mv_buf(struct vdec_h264_inst *inst)
192 int i;
193 struct mtk_vcodec_mem *mem = NULL;
195 for (i = 0; i < H264_MAX_FB_NUM; i++) {
196 inst->vsi->mv_buf_dma[i] = 0;
197 mem = &inst->mv_buf[i];
198 if (mem->va)
199 mtk_vcodec_mem_free(inst->ctx, mem);
203 static int check_list_validity(struct vdec_h264_inst *inst, bool disp_list)
205 struct h264_ring_fb_list *list;
207 list = disp_list ? &inst->vsi->list_disp : &inst->vsi->list_free;
209 if (list->count > H264_MAX_FB_NUM ||
210 list->read_idx >= H264_MAX_FB_NUM ||
211 list->write_idx >= H264_MAX_FB_NUM) {
212 mtk_vcodec_err(inst, "%s list err: cnt=%d r_idx=%d w_idx=%d",
213 disp_list ? "disp" : "free", list->count,
214 list->read_idx, list->write_idx);
215 return -EINVAL;
218 return 0;
221 static void put_fb_to_free(struct vdec_h264_inst *inst, struct vdec_fb *fb)
223 struct h264_ring_fb_list *list;
225 if (fb) {
226 if (check_list_validity(inst, false))
227 return;
229 list = &inst->vsi->list_free;
230 if (list->count == H264_MAX_FB_NUM) {
231 mtk_vcodec_err(inst, "[FB] put fb free_list full");
232 return;
235 mtk_vcodec_debug(inst, "[FB] put fb into free_list @(%p, %llx)",
236 fb->base_y.va, (u64)fb->base_y.dma_addr);
238 list->fb_list[list->write_idx].vdec_fb_va = (u64)(uintptr_t)fb;
239 list->write_idx = (list->write_idx == H264_MAX_FB_NUM - 1) ?
240 0 : list->write_idx + 1;
241 list->count++;
245 static void get_pic_info(struct vdec_h264_inst *inst,
246 struct vdec_pic_info *pic)
248 *pic = inst->vsi->pic;
249 mtk_vcodec_debug(inst, "pic(%d, %d), buf(%d, %d)",
250 pic->pic_w, pic->pic_h, pic->buf_w, pic->buf_h);
251 mtk_vcodec_debug(inst, "fb size: Y(%d), C(%d)",
252 pic->fb_sz[0], pic->fb_sz[1]);
255 static void get_crop_info(struct vdec_h264_inst *inst, struct v4l2_rect *cr)
257 cr->left = inst->vsi->crop.left;
258 cr->top = inst->vsi->crop.top;
259 cr->width = inst->vsi->crop.width;
260 cr->height = inst->vsi->crop.height;
262 mtk_vcodec_debug(inst, "l=%d, t=%d, w=%d, h=%d",
263 cr->left, cr->top, cr->width, cr->height);
266 static void get_dpb_size(struct vdec_h264_inst *inst, unsigned int *dpb_sz)
268 *dpb_sz = inst->vsi->dec.dpb_sz;
269 mtk_vcodec_debug(inst, "sz=%d", *dpb_sz);
272 static int vdec_h264_init(struct mtk_vcodec_ctx *ctx)
274 struct vdec_h264_inst *inst = NULL;
275 int err;
277 inst = kzalloc(sizeof(*inst), GFP_KERNEL);
278 if (!inst)
279 return -ENOMEM;
281 inst->ctx = ctx;
283 inst->vpu.id = IPI_VDEC_H264;
284 inst->vpu.dev = ctx->dev->vpu_plat_dev;
285 inst->vpu.ctx = ctx;
287 err = vpu_dec_init(&inst->vpu);
288 if (err) {
289 mtk_vcodec_err(inst, "vdec_h264 init err=%d", err);
290 goto error_free_inst;
293 inst->vsi = (struct vdec_h264_vsi *)inst->vpu.vsi;
294 err = allocate_predication_buf(inst);
295 if (err)
296 goto error_deinit;
298 mtk_vcodec_debug(inst, "H264 Instance >> %p", inst);
300 ctx->drv_handle = inst;
301 return 0;
303 error_deinit:
304 vpu_dec_deinit(&inst->vpu);
306 error_free_inst:
307 kfree(inst);
308 return err;
311 static void vdec_h264_deinit(void *h_vdec)
313 struct vdec_h264_inst *inst = (struct vdec_h264_inst *)h_vdec;
315 mtk_vcodec_debug_enter(inst);
317 vpu_dec_deinit(&inst->vpu);
318 free_predication_buf(inst);
319 free_mv_buf(inst);
321 kfree(inst);
324 static int find_start_code(unsigned char *data, unsigned int data_sz)
326 if (data_sz > 3 && data[0] == 0 && data[1] == 0 && data[2] == 1)
327 return 3;
329 if (data_sz > 4 && data[0] == 0 && data[1] == 0 && data[2] == 0 &&
330 data[3] == 1)
331 return 4;
333 return -1;
336 static int vdec_h264_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
337 struct vdec_fb *fb, bool *res_chg)
339 struct vdec_h264_inst *inst = (struct vdec_h264_inst *)h_vdec;
340 struct vdec_vpu_inst *vpu = &inst->vpu;
341 int nal_start_idx = 0;
342 int err = 0;
343 unsigned int nal_start;
344 unsigned int nal_type;
345 unsigned char *buf;
346 unsigned int buf_sz;
347 unsigned int data[2];
348 uint64_t vdec_fb_va = (u64)(uintptr_t)fb;
349 uint64_t y_fb_dma = fb ? (u64)fb->base_y.dma_addr : 0;
350 uint64_t c_fb_dma = fb ? (u64)fb->base_c.dma_addr : 0;
352 mtk_vcodec_debug(inst, "+ [%d] FB y_dma=%llx c_dma=%llx va=%p",
353 ++inst->num_nalu, y_fb_dma, c_fb_dma, fb);
355 /* bs NULL means flush decoder */
356 if (bs == NULL)
357 return vpu_dec_reset(vpu);
359 buf = (unsigned char *)bs->va;
360 buf_sz = bs->size;
361 nal_start_idx = find_start_code(buf, buf_sz);
362 if (nal_start_idx < 0) {
363 mtk_vcodec_err(inst, "invalid nal start code");
364 err = -EIO;
365 goto err_free_fb_out;
368 nal_start = buf[nal_start_idx];
369 nal_type = NAL_TYPE(buf[nal_start_idx]);
370 mtk_vcodec_debug(inst, "\n + NALU[%d] type %d +\n", inst->num_nalu,
371 nal_type);
373 if (nal_type == NAL_H264_PPS) {
374 buf_sz -= nal_start_idx;
375 if (buf_sz > HDR_PARSING_BUF_SZ) {
376 err = -EILSEQ;
377 goto err_free_fb_out;
379 memcpy(inst->vsi->hdr_buf, buf + nal_start_idx, buf_sz);
382 inst->vsi->dec.bs_dma = (uint64_t)bs->dma_addr;
383 inst->vsi->dec.y_fb_dma = y_fb_dma;
384 inst->vsi->dec.c_fb_dma = c_fb_dma;
385 inst->vsi->dec.vdec_fb_va = vdec_fb_va;
387 data[0] = buf_sz;
388 data[1] = nal_start;
389 err = vpu_dec_start(vpu, data, 2);
390 if (err) {
391 if (err > 0 && (DEC_ERR_RET(err) == H264_ERR_NOT_VALID)) {
392 mtk_vcodec_err(inst, "- error bitstream - err = %d -",
393 err);
394 err = -EIO;
396 goto err_free_fb_out;
399 *res_chg = inst->vsi->dec.resolution_changed;
400 if (*res_chg) {
401 struct vdec_pic_info pic;
403 mtk_vcodec_debug(inst, "- resolution changed -");
404 get_pic_info(inst, &pic);
406 if (inst->vsi->dec.realloc_mv_buf) {
407 err = alloc_mv_buf(inst, &pic);
408 if (err)
409 goto err_free_fb_out;
413 if (nal_type == NAL_NON_IDR_SLICE || nal_type == NAL_IDR_SLICE) {
414 /* wait decoder done interrupt */
415 err = mtk_vcodec_wait_for_done_ctx(inst->ctx,
416 MTK_INST_IRQ_RECEIVED,
417 WAIT_INTR_TIMEOUT_MS);
418 if (err)
419 goto err_free_fb_out;
421 vpu_dec_end(vpu);
424 mtk_vcodec_debug(inst, "\n - NALU[%d] type=%d -\n", inst->num_nalu,
425 nal_type);
426 return 0;
428 err_free_fb_out:
429 put_fb_to_free(inst, fb);
430 mtk_vcodec_err(inst, "\n - NALU[%d] err=%d -\n", inst->num_nalu, err);
431 return err;
434 static void vdec_h264_get_fb(struct vdec_h264_inst *inst,
435 struct h264_ring_fb_list *list,
436 bool disp_list, struct vdec_fb **out_fb)
438 struct vdec_fb *fb;
440 if (check_list_validity(inst, disp_list))
441 return;
443 if (list->count == 0) {
444 mtk_vcodec_debug(inst, "[FB] there is no %s fb",
445 disp_list ? "disp" : "free");
446 *out_fb = NULL;
447 return;
450 fb = (struct vdec_fb *)
451 (uintptr_t)list->fb_list[list->read_idx].vdec_fb_va;
452 fb->status |= (disp_list ? FB_ST_DISPLAY : FB_ST_FREE);
454 *out_fb = fb;
455 mtk_vcodec_debug(inst, "[FB] get %s fb st=%d poc=%d %llx",
456 disp_list ? "disp" : "free",
457 fb->status, list->fb_list[list->read_idx].poc,
458 list->fb_list[list->read_idx].vdec_fb_va);
460 list->read_idx = (list->read_idx == H264_MAX_FB_NUM - 1) ?
461 0 : list->read_idx + 1;
462 list->count--;
465 static int vdec_h264_get_param(void *h_vdec, enum vdec_get_param_type type,
466 void *out)
468 struct vdec_h264_inst *inst = (struct vdec_h264_inst *)h_vdec;
470 switch (type) {
471 case GET_PARAM_DISP_FRAME_BUFFER:
472 vdec_h264_get_fb(inst, &inst->vsi->list_disp, true, out);
473 break;
475 case GET_PARAM_FREE_FRAME_BUFFER:
476 vdec_h264_get_fb(inst, &inst->vsi->list_free, false, out);
477 break;
479 case GET_PARAM_PIC_INFO:
480 get_pic_info(inst, out);
481 break;
483 case GET_PARAM_DPB_SIZE:
484 get_dpb_size(inst, out);
485 break;
487 case GET_PARAM_CROP_INFO:
488 get_crop_info(inst, out);
489 break;
491 default:
492 mtk_vcodec_err(inst, "invalid get parameter type=%d", type);
493 return -EINVAL;
496 return 0;
499 const struct vdec_common_if vdec_h264_if = {
500 .init = vdec_h264_init,
501 .decode = vdec_h264_decode,
502 .get_param = vdec_h264_get_param,
503 .deinit = vdec_h264_deinit,