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