Merge tag 'powerpc-4.15-7' of git://git.kernel.org/pub/scm/linux/kernel/git/powerpc...
[sfrench/cifs-2.6.git] / drivers / media / platform / mtk-vcodec / vdec / vdec_h264_if.c
1 /*
2  * Copyright (c) 2016 MediaTek Inc.
3  * Author: PC Chen <pc.chen@mediatek.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
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.
13  */
14
15 #include <linux/module.h>
16 #include <linux/slab.h>
17
18 #include "../vdec_drv_if.h"
19 #include "../mtk_vcodec_util.h"
20 #include "../mtk_vcodec_dec.h"
21 #include "../mtk_vcodec_intr.h"
22 #include "../vdec_vpu_if.h"
23 #include "../vdec_drv_base.h"
24
25 #define NAL_NON_IDR_SLICE                       0x01
26 #define NAL_IDR_SLICE                           0x05
27 #define NAL_H264_PPS                            0x08
28 #define NAL_TYPE(value)                         ((value) & 0x1F)
29
30 #define BUF_PREDICTION_SZ                       (32 * 1024)
31
32 #define MB_UNIT_LEN                             16
33
34 /* motion vector size (bytes) for every macro block */
35 #define HW_MB_STORE_SZ                          64
36
37 #define H264_MAX_FB_NUM                         17
38 #define HDR_PARSING_BUF_SZ                      1024
39
40 /**
41  * struct h264_fb - h264 decode frame buffer information
42  * @vdec_fb_va  : virtual address of struct vdec_fb
43  * @y_fb_dma    : dma address of Y frame buffer (luma)
44  * @c_fb_dma    : dma address of C frame buffer (chroma)
45  * @poc         : picture order count of frame buffer
46  * @reserved    : for 8 bytes alignment
47  */
48 struct h264_fb {
49         uint64_t vdec_fb_va;
50         uint64_t y_fb_dma;
51         uint64_t c_fb_dma;
52         int32_t poc;
53         uint32_t reserved;
54 };
55
56 /**
57  * struct h264_ring_fb_list - ring frame buffer list
58  * @fb_list   : frame buffer arrary
59  * @read_idx  : read index
60  * @write_idx : write index
61  * @count     : buffer count in list
62  * @reserved  : for 8 bytes alignment
63  */
64 struct h264_ring_fb_list {
65         struct h264_fb fb_list[H264_MAX_FB_NUM];
66         unsigned int read_idx;
67         unsigned int write_idx;
68         unsigned int count;
69         unsigned int reserved;
70 };
71
72 /**
73  * struct vdec_h264_dec_info - decode information
74  * @dpb_sz              : decoding picture buffer size
75  * @resolution_changed  : resoltion change happen
76  * @realloc_mv_buf      : flag to notify driver to re-allocate mv buffer
77  * @reserved            : for 8 bytes alignment
78  * @bs_dma              : Input bit-stream buffer dma address
79  * @y_fb_dma            : Y frame buffer dma address
80  * @c_fb_dma            : C frame buffer dma address
81  * @vdec_fb_va          : VDEC frame buffer struct virtual address
82  */
83 struct vdec_h264_dec_info {
84         uint32_t dpb_sz;
85         uint32_t resolution_changed;
86         uint32_t realloc_mv_buf;
87         uint32_t reserved;
88         uint64_t bs_dma;
89         uint64_t y_fb_dma;
90         uint64_t c_fb_dma;
91         uint64_t vdec_fb_va;
92 };
93
94 /**
95  * struct vdec_h264_vsi - shared memory for decode information exchange
96  *                        between VPU and Host.
97  *                        The memory is allocated by VPU then mapping to Host
98  *                        in vpu_dec_init() and freed in vpu_dec_deinit()
99  *                        by VPU.
100  *                        AP-W/R : AP is writer/reader on this item
101  *                        VPU-W/R: VPU is write/reader on this item
102  * @hdr_buf      : Header parsing buffer (AP-W, VPU-R)
103  * @pred_buf_dma : HW working predication buffer dma address (AP-W, VPU-R)
104  * @mv_buf_dma   : HW working motion vector buffer dma address (AP-W, VPU-R)
105  * @list_free    : free frame buffer ring list (AP-W/R, VPU-W)
106  * @list_disp    : display frame buffer ring list (AP-R, VPU-W)
107  * @dec          : decode information (AP-R, VPU-W)
108  * @pic          : picture information (AP-R, VPU-W)
109  * @crop         : crop information (AP-R, VPU-W)
110  */
111 struct vdec_h264_vsi {
112         unsigned char hdr_buf[HDR_PARSING_BUF_SZ];
113         uint64_t pred_buf_dma;
114         uint64_t mv_buf_dma[H264_MAX_FB_NUM];
115         struct h264_ring_fb_list list_free;
116         struct h264_ring_fb_list list_disp;
117         struct vdec_h264_dec_info dec;
118         struct vdec_pic_info pic;
119         struct v4l2_rect crop;
120 };
121
122 /**
123  * struct vdec_h264_inst - h264 decoder instance
124  * @num_nalu : how many nalus be decoded
125  * @ctx      : point to mtk_vcodec_ctx
126  * @pred_buf : HW working predication buffer
127  * @mv_buf   : HW working motion vector buffer
128  * @vpu      : VPU instance
129  * @vsi      : VPU shared information
130  */
131 struct vdec_h264_inst {
132         unsigned int num_nalu;
133         struct mtk_vcodec_ctx *ctx;
134         struct mtk_vcodec_mem pred_buf;
135         struct mtk_vcodec_mem mv_buf[H264_MAX_FB_NUM];
136         struct vdec_vpu_inst vpu;
137         struct vdec_h264_vsi *vsi;
138 };
139
140 static unsigned int get_mv_buf_size(unsigned int width, unsigned int height)
141 {
142         return HW_MB_STORE_SZ * (width/MB_UNIT_LEN) * (height/MB_UNIT_LEN);
143 }
144
145 static int allocate_predication_buf(struct vdec_h264_inst *inst)
146 {
147         int err = 0;
148
149         inst->pred_buf.size = BUF_PREDICTION_SZ;
150         err = mtk_vcodec_mem_alloc(inst->ctx, &inst->pred_buf);
151         if (err) {
152                 mtk_vcodec_err(inst, "failed to allocate ppl buf");
153                 return err;
154         }
155
156         inst->vsi->pred_buf_dma = inst->pred_buf.dma_addr;
157         return 0;
158 }
159
160 static void free_predication_buf(struct vdec_h264_inst *inst)
161 {
162         struct mtk_vcodec_mem *mem = NULL;
163
164         mtk_vcodec_debug_enter(inst);
165
166         inst->vsi->pred_buf_dma = 0;
167         mem = &inst->pred_buf;
168         if (mem->va)
169                 mtk_vcodec_mem_free(inst->ctx, mem);
170 }
171
172 static int alloc_mv_buf(struct vdec_h264_inst *inst, struct vdec_pic_info *pic)
173 {
174         int i;
175         int err;
176         struct mtk_vcodec_mem *mem = NULL;
177         unsigned int buf_sz = get_mv_buf_size(pic->buf_w, pic->buf_h);
178
179         for (i = 0; i < H264_MAX_FB_NUM; i++) {
180                 mem = &inst->mv_buf[i];
181                 if (mem->va)
182                         mtk_vcodec_mem_free(inst->ctx, mem);
183                 mem->size = buf_sz;
184                 err = mtk_vcodec_mem_alloc(inst->ctx, mem);
185                 if (err) {
186                         mtk_vcodec_err(inst, "failed to allocate mv buf");
187                         return err;
188                 }
189                 inst->vsi->mv_buf_dma[i] = mem->dma_addr;
190         }
191
192         return 0;
193 }
194
195 static void free_mv_buf(struct vdec_h264_inst *inst)
196 {
197         int i;
198         struct mtk_vcodec_mem *mem = NULL;
199
200         for (i = 0; i < H264_MAX_FB_NUM; i++) {
201                 inst->vsi->mv_buf_dma[i] = 0;
202                 mem = &inst->mv_buf[i];
203                 if (mem->va)
204                         mtk_vcodec_mem_free(inst->ctx, mem);
205         }
206 }
207
208 static int check_list_validity(struct vdec_h264_inst *inst, bool disp_list)
209 {
210         struct h264_ring_fb_list *list;
211
212         list = disp_list ? &inst->vsi->list_disp : &inst->vsi->list_free;
213
214         if (list->count > H264_MAX_FB_NUM ||
215             list->read_idx >= H264_MAX_FB_NUM ||
216             list->write_idx >= H264_MAX_FB_NUM) {
217                 mtk_vcodec_err(inst, "%s list err: cnt=%d r_idx=%d w_idx=%d",
218                                disp_list ? "disp" : "free", list->count,
219                                list->read_idx, list->write_idx);
220                 return -EINVAL;
221         }
222
223         return 0;
224 }
225
226 static void put_fb_to_free(struct vdec_h264_inst *inst, struct vdec_fb *fb)
227 {
228         struct h264_ring_fb_list *list;
229
230         if (fb) {
231                 if (check_list_validity(inst, false))
232                         return;
233
234                 list = &inst->vsi->list_free;
235                 if (list->count == H264_MAX_FB_NUM) {
236                         mtk_vcodec_err(inst, "[FB] put fb free_list full");
237                         return;
238                 }
239
240                 mtk_vcodec_debug(inst, "[FB] put fb into free_list @(%p, %llx)",
241                                  fb->base_y.va, (u64)fb->base_y.dma_addr);
242
243                 list->fb_list[list->write_idx].vdec_fb_va = (u64)(uintptr_t)fb;
244                 list->write_idx = (list->write_idx == H264_MAX_FB_NUM - 1) ?
245                                   0 : list->write_idx + 1;
246                 list->count++;
247         }
248 }
249
250 static void get_pic_info(struct vdec_h264_inst *inst,
251                          struct vdec_pic_info *pic)
252 {
253         *pic = inst->vsi->pic;
254         mtk_vcodec_debug(inst, "pic(%d, %d), buf(%d, %d)",
255                          pic->pic_w, pic->pic_h, pic->buf_w, pic->buf_h);
256         mtk_vcodec_debug(inst, "Y(%d, %d), C(%d, %d)", pic->y_bs_sz,
257                          pic->y_len_sz, pic->c_bs_sz, pic->c_len_sz);
258 }
259
260 static void get_crop_info(struct vdec_h264_inst *inst, struct v4l2_rect *cr)
261 {
262         cr->left = inst->vsi->crop.left;
263         cr->top = inst->vsi->crop.top;
264         cr->width = inst->vsi->crop.width;
265         cr->height = inst->vsi->crop.height;
266
267         mtk_vcodec_debug(inst, "l=%d, t=%d, w=%d, h=%d",
268                          cr->left, cr->top, cr->width, cr->height);
269 }
270
271 static void get_dpb_size(struct vdec_h264_inst *inst, unsigned int *dpb_sz)
272 {
273         *dpb_sz = inst->vsi->dec.dpb_sz;
274         mtk_vcodec_debug(inst, "sz=%d", *dpb_sz);
275 }
276
277 static int vdec_h264_init(struct mtk_vcodec_ctx *ctx, unsigned long *h_vdec)
278 {
279         struct vdec_h264_inst *inst = NULL;
280         int err;
281
282         inst = kzalloc(sizeof(*inst), GFP_KERNEL);
283         if (!inst)
284                 return -ENOMEM;
285
286         inst->ctx = ctx;
287
288         inst->vpu.id = IPI_VDEC_H264;
289         inst->vpu.dev = ctx->dev->vpu_plat_dev;
290         inst->vpu.ctx = ctx;
291         inst->vpu.handler = vpu_dec_ipi_handler;
292
293         err = vpu_dec_init(&inst->vpu);
294         if (err) {
295                 mtk_vcodec_err(inst, "vdec_h264 init err=%d", err);
296                 goto error_free_inst;
297         }
298
299         inst->vsi = (struct vdec_h264_vsi *)inst->vpu.vsi;
300         err = allocate_predication_buf(inst);
301         if (err)
302                 goto error_deinit;
303
304         mtk_vcodec_debug(inst, "H264 Instance >> %p", inst);
305
306         *h_vdec = (unsigned long)inst;
307         return 0;
308
309 error_deinit:
310         vpu_dec_deinit(&inst->vpu);
311
312 error_free_inst:
313         kfree(inst);
314         return err;
315 }
316
317 static void vdec_h264_deinit(unsigned long h_vdec)
318 {
319         struct vdec_h264_inst *inst = (struct vdec_h264_inst *)h_vdec;
320
321         mtk_vcodec_debug_enter(inst);
322
323         vpu_dec_deinit(&inst->vpu);
324         free_predication_buf(inst);
325         free_mv_buf(inst);
326
327         kfree(inst);
328 }
329
330 static int find_start_code(unsigned char *data, unsigned int data_sz)
331 {
332         if (data_sz > 3 && data[0] == 0 && data[1] == 0 && data[2] == 1)
333                 return 3;
334
335         if (data_sz > 4 && data[0] == 0 && data[1] == 0 && data[2] == 0 &&
336             data[3] == 1)
337                 return 4;
338
339         return -1;
340 }
341
342 static int vdec_h264_decode(unsigned long h_vdec, struct mtk_vcodec_mem *bs,
343                             struct vdec_fb *fb, bool *res_chg)
344 {
345         struct vdec_h264_inst *inst = (struct vdec_h264_inst *)h_vdec;
346         struct vdec_vpu_inst *vpu = &inst->vpu;
347         int nal_start_idx = 0;
348         int err = 0;
349         unsigned int nal_start;
350         unsigned int nal_type;
351         unsigned char *buf;
352         unsigned int buf_sz;
353         unsigned int data[2];
354         uint64_t vdec_fb_va = (u64)(uintptr_t)fb;
355         uint64_t y_fb_dma = fb ? (u64)fb->base_y.dma_addr : 0;
356         uint64_t c_fb_dma = fb ? (u64)fb->base_c.dma_addr : 0;
357
358         mtk_vcodec_debug(inst, "+ [%d] FB y_dma=%llx c_dma=%llx va=%p",
359                          ++inst->num_nalu, y_fb_dma, c_fb_dma, fb);
360
361         /* bs NULL means flush decoder */
362         if (bs == NULL)
363                 return vpu_dec_reset(vpu);
364
365         buf = (unsigned char *)bs->va;
366         buf_sz = bs->size;
367         nal_start_idx = find_start_code(buf, buf_sz);
368         if (nal_start_idx < 0)
369                 goto err_free_fb_out;
370
371         nal_start = buf[nal_start_idx];
372         nal_type = NAL_TYPE(buf[nal_start_idx]);
373         mtk_vcodec_debug(inst, "\n + NALU[%d] type %d +\n", inst->num_nalu,
374                          nal_type);
375
376         if (nal_type == NAL_H264_PPS) {
377                 buf_sz -= nal_start_idx;
378                 if (buf_sz > HDR_PARSING_BUF_SZ) {
379                         err = -EILSEQ;
380                         goto err_free_fb_out;
381                 }
382                 memcpy(inst->vsi->hdr_buf, buf + nal_start_idx, buf_sz);
383         }
384
385         inst->vsi->dec.bs_dma = (uint64_t)bs->dma_addr;
386         inst->vsi->dec.y_fb_dma = y_fb_dma;
387         inst->vsi->dec.c_fb_dma = c_fb_dma;
388         inst->vsi->dec.vdec_fb_va = vdec_fb_va;
389
390         data[0] = buf_sz;
391         data[1] = nal_start;
392         err = vpu_dec_start(vpu, data, 2);
393         if (err)
394                 goto err_free_fb_out;
395
396         *res_chg = inst->vsi->dec.resolution_changed;
397         if (*res_chg) {
398                 struct vdec_pic_info pic;
399
400                 mtk_vcodec_debug(inst, "- resolution changed -");
401                 get_pic_info(inst, &pic);
402
403                 if (inst->vsi->dec.realloc_mv_buf) {
404                         err = alloc_mv_buf(inst, &pic);
405                         if (err)
406                                 goto err_free_fb_out;
407                 }
408         }
409
410         if (nal_type == NAL_NON_IDR_SLICE || nal_type == NAL_IDR_SLICE) {
411                 /* wait decoder done interrupt */
412                 err = mtk_vcodec_wait_for_done_ctx(inst->ctx,
413                                                    MTK_INST_IRQ_RECEIVED,
414                                                    WAIT_INTR_TIMEOUT_MS);
415                 if (err)
416                         goto err_free_fb_out;
417
418                 vpu_dec_end(vpu);
419         }
420
421         mtk_vcodec_debug(inst, "\n - NALU[%d] type=%d -\n", inst->num_nalu,
422                          nal_type);
423         return 0;
424
425 err_free_fb_out:
426         put_fb_to_free(inst, fb);
427         mtk_vcodec_err(inst, "\n - NALU[%d] err=%d -\n", inst->num_nalu, err);
428         return err;
429 }
430
431 static void vdec_h264_get_fb(struct vdec_h264_inst *inst,
432                              struct h264_ring_fb_list *list,
433                              bool disp_list, struct vdec_fb **out_fb)
434 {
435         struct vdec_fb *fb;
436
437         if (check_list_validity(inst, disp_list))
438                 return;
439
440         if (list->count == 0) {
441                 mtk_vcodec_debug(inst, "[FB] there is no %s fb",
442                                  disp_list ? "disp" : "free");
443                 *out_fb = NULL;
444                 return;
445         }
446
447         fb = (struct vdec_fb *)
448                 (uintptr_t)list->fb_list[list->read_idx].vdec_fb_va;
449         fb->status |= (disp_list ? FB_ST_DISPLAY : FB_ST_FREE);
450
451         *out_fb = fb;
452         mtk_vcodec_debug(inst, "[FB] get %s fb st=%d poc=%d %llx",
453                          disp_list ? "disp" : "free",
454                          fb->status, list->fb_list[list->read_idx].poc,
455                          list->fb_list[list->read_idx].vdec_fb_va);
456
457         list->read_idx = (list->read_idx == H264_MAX_FB_NUM - 1) ?
458                          0 : list->read_idx + 1;
459         list->count--;
460 }
461
462 static int vdec_h264_get_param(unsigned long h_vdec,
463                                enum vdec_get_param_type type, void *out)
464 {
465         struct vdec_h264_inst *inst = (struct vdec_h264_inst *)h_vdec;
466
467         switch (type) {
468         case GET_PARAM_DISP_FRAME_BUFFER:
469                 vdec_h264_get_fb(inst, &inst->vsi->list_disp, true, out);
470                 break;
471
472         case GET_PARAM_FREE_FRAME_BUFFER:
473                 vdec_h264_get_fb(inst, &inst->vsi->list_free, false, out);
474                 break;
475
476         case GET_PARAM_PIC_INFO:
477                 get_pic_info(inst, out);
478                 break;
479
480         case GET_PARAM_DPB_SIZE:
481                 get_dpb_size(inst, out);
482                 break;
483
484         case GET_PARAM_CROP_INFO:
485                 get_crop_info(inst, out);
486                 break;
487
488         default:
489                 mtk_vcodec_err(inst, "invalid get parameter type=%d", type);
490                 return -EINVAL;
491         }
492
493         return 0;
494 }
495
496 static struct vdec_common_if vdec_h264_if = {
497         .init           = vdec_h264_init,
498         .decode         = vdec_h264_decode,
499         .get_param      = vdec_h264_get_param,
500         .deinit         = vdec_h264_deinit,
501 };
502
503 struct vdec_common_if *get_h264_dec_comm_if(void);
504
505 struct vdec_common_if *get_h264_dec_comm_if(void)
506 {
507         return &vdec_h264_if;
508 }