]> Pileus Git - ~andy/linux/blob - drivers/media/platform/s5p-mfc/s5p_mfc_opr_v6.c
Merge tag 'asoc-v3.9-rc6' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie...
[~andy/linux] / drivers / media / platform / s5p-mfc / s5p_mfc_opr_v6.c
1 /*
2  * drivers/media/platform/s5p-mfc/s5p_mfc_opr_v6.c
3  *
4  * Samsung MFC (Multi Function Codec - FIMV) driver
5  * This file contains hw related functions.
6  *
7  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
8  *              http://www.samsung.com/
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  */
14
15 #undef DEBUG
16
17 #include <linux/delay.h>
18 #include <linux/mm.h>
19 #include <linux/io.h>
20 #include <linux/jiffies.h>
21 #include <linux/firmware.h>
22 #include <linux/err.h>
23 #include <linux/sched.h>
24 #include <linux/dma-mapping.h>
25
26 #include <asm/cacheflush.h>
27
28 #include "s5p_mfc_common.h"
29 #include "s5p_mfc_cmd.h"
30 #include "s5p_mfc_intr.h"
31 #include "s5p_mfc_pm.h"
32 #include "s5p_mfc_debug.h"
33 #include "s5p_mfc_opr.h"
34 #include "s5p_mfc_opr_v6.h"
35
36 /* #define S5P_MFC_DEBUG_REGWRITE  */
37 #ifdef S5P_MFC_DEBUG_REGWRITE
38 #undef writel
39 #define writel(v, r)                                                    \
40         do {                                                            \
41                 pr_err("MFCWRITE(%p): %08x\n", r, (unsigned int)v);     \
42         __raw_writel(v, r);                                             \
43         } while (0)
44 #endif /* S5P_MFC_DEBUG_REGWRITE */
45
46 #define READL(offset)           readl(dev->regs_base + (offset))
47 #define WRITEL(data, offset)    writel((data), dev->regs_base + (offset))
48 #define OFFSETA(x)              (((x) - dev->port_a) >> S5P_FIMV_MEM_OFFSET)
49 #define OFFSETB(x)              (((x) - dev->port_b) >> S5P_FIMV_MEM_OFFSET)
50
51 /* Allocate temporary buffers for decoding */
52 int s5p_mfc_alloc_dec_temp_buffers_v6(struct s5p_mfc_ctx *ctx)
53 {
54         /* NOP */
55
56         return 0;
57 }
58
59 /* Release temproary buffers for decoding */
60 void s5p_mfc_release_dec_desc_buffer_v6(struct s5p_mfc_ctx *ctx)
61 {
62         /* NOP */
63 }
64
65 int s5p_mfc_get_dec_status_v6(struct s5p_mfc_dev *dev)
66 {
67         /* NOP */
68         return -1;
69 }
70
71 /* Allocate codec buffers */
72 int s5p_mfc_alloc_codec_buffers_v6(struct s5p_mfc_ctx *ctx)
73 {
74         struct s5p_mfc_dev *dev = ctx->dev;
75         unsigned int mb_width, mb_height;
76         int ret;
77
78         mb_width = MB_WIDTH(ctx->img_width);
79         mb_height = MB_HEIGHT(ctx->img_height);
80
81         if (ctx->type == MFCINST_DECODER) {
82                 mfc_debug(2, "Luma size:%d Chroma size:%d MV size:%d\n",
83                           ctx->luma_size, ctx->chroma_size, ctx->mv_size);
84                 mfc_debug(2, "Totals bufs: %d\n", ctx->total_dpb_count);
85         } else if (ctx->type == MFCINST_ENCODER) {
86                 ctx->tmv_buffer_size = S5P_FIMV_NUM_TMV_BUFFERS_V6 *
87                         ALIGN(S5P_FIMV_TMV_BUFFER_SIZE_V6(mb_width, mb_height),
88                         S5P_FIMV_TMV_BUFFER_ALIGN_V6);
89                 ctx->luma_dpb_size = ALIGN((mb_width * mb_height) *
90                                 S5P_FIMV_LUMA_MB_TO_PIXEL_V6,
91                                 S5P_FIMV_LUMA_DPB_BUFFER_ALIGN_V6);
92                 ctx->chroma_dpb_size = ALIGN((mb_width * mb_height) *
93                                 S5P_FIMV_CHROMA_MB_TO_PIXEL_V6,
94                                 S5P_FIMV_CHROMA_DPB_BUFFER_ALIGN_V6);
95                 ctx->me_buffer_size = ALIGN(S5P_FIMV_ME_BUFFER_SIZE_V6(
96                                         ctx->img_width, ctx->img_height,
97                                         mb_width, mb_height),
98                                         S5P_FIMV_ME_BUFFER_ALIGN_V6);
99
100                 mfc_debug(2, "recon luma size: %d chroma size: %d\n",
101                           ctx->luma_dpb_size, ctx->chroma_dpb_size);
102         } else {
103                 return -EINVAL;
104         }
105
106         /* Codecs have different memory requirements */
107         switch (ctx->codec_mode) {
108         case S5P_MFC_CODEC_H264_DEC:
109         case S5P_MFC_CODEC_H264_MVC_DEC:
110                 ctx->scratch_buf_size =
111                         S5P_FIMV_SCRATCH_BUF_SIZE_H264_DEC_V6(
112                                         mb_width,
113                                         mb_height);
114                 ctx->scratch_buf_size = ALIGN(ctx->scratch_buf_size,
115                                 S5P_FIMV_SCRATCH_BUFFER_ALIGN_V6);
116                 ctx->bank1.size =
117                         ctx->scratch_buf_size +
118                         (ctx->mv_count * ctx->mv_size);
119                 break;
120         case S5P_MFC_CODEC_MPEG4_DEC:
121                 ctx->scratch_buf_size =
122                         S5P_FIMV_SCRATCH_BUF_SIZE_MPEG4_DEC_V6(
123                                         mb_width,
124                                         mb_height);
125                 ctx->scratch_buf_size = ALIGN(ctx->scratch_buf_size,
126                                 S5P_FIMV_SCRATCH_BUFFER_ALIGN_V6);
127                 ctx->bank1.size = ctx->scratch_buf_size;
128                 break;
129         case S5P_MFC_CODEC_VC1RCV_DEC:
130         case S5P_MFC_CODEC_VC1_DEC:
131                 ctx->scratch_buf_size =
132                         S5P_FIMV_SCRATCH_BUF_SIZE_VC1_DEC_V6(
133                                         mb_width,
134                                         mb_height);
135                 ctx->scratch_buf_size = ALIGN(ctx->scratch_buf_size,
136                                 S5P_FIMV_SCRATCH_BUFFER_ALIGN_V6);
137                 ctx->bank1.size = ctx->scratch_buf_size;
138                 break;
139         case S5P_MFC_CODEC_MPEG2_DEC:
140                 ctx->bank1.size = 0;
141                 ctx->bank2.size = 0;
142                 break;
143         case S5P_MFC_CODEC_H263_DEC:
144                 ctx->scratch_buf_size =
145                         S5P_FIMV_SCRATCH_BUF_SIZE_H263_DEC_V6(
146                                         mb_width,
147                                         mb_height);
148                 ctx->scratch_buf_size = ALIGN(ctx->scratch_buf_size,
149                                 S5P_FIMV_SCRATCH_BUFFER_ALIGN_V6);
150                 ctx->bank1.size = ctx->scratch_buf_size;
151                 break;
152         case S5P_MFC_CODEC_VP8_DEC:
153                 ctx->scratch_buf_size =
154                         S5P_FIMV_SCRATCH_BUF_SIZE_VP8_DEC_V6(
155                                         mb_width,
156                                         mb_height);
157                 ctx->scratch_buf_size = ALIGN(ctx->scratch_buf_size,
158                                 S5P_FIMV_SCRATCH_BUFFER_ALIGN_V6);
159                 ctx->bank1.size = ctx->scratch_buf_size;
160                 break;
161         case S5P_MFC_CODEC_H264_ENC:
162                 ctx->scratch_buf_size =
163                         S5P_FIMV_SCRATCH_BUF_SIZE_H264_ENC_V6(
164                                         mb_width,
165                                         mb_height);
166                 ctx->scratch_buf_size = ALIGN(ctx->scratch_buf_size,
167                                 S5P_FIMV_SCRATCH_BUFFER_ALIGN_V6);
168                 ctx->bank1.size =
169                         ctx->scratch_buf_size + ctx->tmv_buffer_size +
170                         (ctx->dpb_count * (ctx->luma_dpb_size +
171                         ctx->chroma_dpb_size + ctx->me_buffer_size));
172                 ctx->bank2.size = 0;
173                 break;
174         case S5P_MFC_CODEC_MPEG4_ENC:
175         case S5P_MFC_CODEC_H263_ENC:
176                 ctx->scratch_buf_size =
177                         S5P_FIMV_SCRATCH_BUF_SIZE_MPEG4_ENC_V6(
178                                         mb_width,
179                                         mb_height);
180                 ctx->scratch_buf_size = ALIGN(ctx->scratch_buf_size,
181                                 S5P_FIMV_SCRATCH_BUFFER_ALIGN_V6);
182                 ctx->bank1.size =
183                         ctx->scratch_buf_size + ctx->tmv_buffer_size +
184                         (ctx->dpb_count * (ctx->luma_dpb_size +
185                         ctx->chroma_dpb_size + ctx->me_buffer_size));
186                 ctx->bank2.size = 0;
187                 break;
188         default:
189                 break;
190         }
191
192         /* Allocate only if memory from bank 1 is necessary */
193         if (ctx->bank1.size > 0) {
194                 ret = s5p_mfc_alloc_priv_buf(dev->mem_dev_l, &ctx->bank1);
195                 if (ret) {
196                         mfc_err("Failed to allocate Bank1 memory\n");
197                         return ret;
198                 }
199                 BUG_ON(ctx->bank1.dma & ((1 << MFC_BANK1_ALIGN_ORDER) - 1));
200         }
201
202         return 0;
203 }
204
205 /* Release buffers allocated for codec */
206 void s5p_mfc_release_codec_buffers_v6(struct s5p_mfc_ctx *ctx)
207 {
208         s5p_mfc_release_priv_buf(ctx->dev->mem_dev_l, &ctx->bank1);
209 }
210
211 /* Allocate memory for instance data buffer */
212 int s5p_mfc_alloc_instance_buffer_v6(struct s5p_mfc_ctx *ctx)
213 {
214         struct s5p_mfc_dev *dev = ctx->dev;
215         struct s5p_mfc_buf_size_v6 *buf_size = dev->variant->buf_size->priv;
216         int ret;
217
218         mfc_debug_enter();
219
220         switch (ctx->codec_mode) {
221         case S5P_MFC_CODEC_H264_DEC:
222         case S5P_MFC_CODEC_H264_MVC_DEC:
223                 ctx->ctx.size = buf_size->h264_dec_ctx;
224                 break;
225         case S5P_MFC_CODEC_MPEG4_DEC:
226         case S5P_MFC_CODEC_H263_DEC:
227         case S5P_MFC_CODEC_VC1RCV_DEC:
228         case S5P_MFC_CODEC_VC1_DEC:
229         case S5P_MFC_CODEC_MPEG2_DEC:
230         case S5P_MFC_CODEC_VP8_DEC:
231                 ctx->ctx.size = buf_size->other_dec_ctx;
232                 break;
233         case S5P_MFC_CODEC_H264_ENC:
234                 ctx->ctx.size = buf_size->h264_enc_ctx;
235                 break;
236         case S5P_MFC_CODEC_MPEG4_ENC:
237         case S5P_MFC_CODEC_H263_ENC:
238                 ctx->ctx.size = buf_size->other_enc_ctx;
239                 break;
240         default:
241                 ctx->ctx.size = 0;
242                 mfc_err("Codec type(%d) should be checked!\n", ctx->codec_mode);
243                 break;
244         }
245
246         ret = s5p_mfc_alloc_priv_buf(dev->mem_dev_l, &ctx->ctx);
247         if (ret) {
248                 mfc_err("Failed to allocate instance buffer\n");
249                 return ret;
250         }
251
252         memset(ctx->ctx.virt, 0, ctx->ctx.size);
253         wmb();
254
255         mfc_debug_leave();
256
257         return 0;
258 }
259
260 /* Release instance buffer */
261 void s5p_mfc_release_instance_buffer_v6(struct s5p_mfc_ctx *ctx)
262 {
263         s5p_mfc_release_priv_buf(ctx->dev->mem_dev_l, &ctx->ctx);
264 }
265
266 /* Allocate context buffers for SYS_INIT */
267 int s5p_mfc_alloc_dev_context_buffer_v6(struct s5p_mfc_dev *dev)
268 {
269         struct s5p_mfc_buf_size_v6 *buf_size = dev->variant->buf_size->priv;
270         int ret;
271
272         mfc_debug_enter();
273
274         dev->ctx_buf.size = buf_size->dev_ctx;
275         ret = s5p_mfc_alloc_priv_buf(dev->mem_dev_l, &dev->ctx_buf);
276         if (ret) {
277                 mfc_err("Failed to allocate device context buffer\n");
278                 return ret;
279         }
280
281         memset(dev->ctx_buf.virt, 0, buf_size->dev_ctx);
282         wmb();
283
284         mfc_debug_leave();
285
286         return 0;
287 }
288
289 /* Release context buffers for SYS_INIT */
290 void s5p_mfc_release_dev_context_buffer_v6(struct s5p_mfc_dev *dev)
291 {
292         s5p_mfc_release_priv_buf(dev->mem_dev_l, &dev->ctx_buf);
293 }
294
295 static int calc_plane(int width, int height)
296 {
297         int mbX, mbY;
298
299         mbX = DIV_ROUND_UP(width, S5P_FIMV_NUM_PIXELS_IN_MB_ROW_V6);
300         mbY = DIV_ROUND_UP(height, S5P_FIMV_NUM_PIXELS_IN_MB_COL_V6);
301
302         if (width * height < S5P_FIMV_MAX_FRAME_SIZE_V6)
303                 mbY = (mbY + 1) / 2 * 2;
304
305         return (mbX * S5P_FIMV_NUM_PIXELS_IN_MB_COL_V6) *
306                 (mbY * S5P_FIMV_NUM_PIXELS_IN_MB_ROW_V6);
307 }
308
309 void s5p_mfc_dec_calc_dpb_size_v6(struct s5p_mfc_ctx *ctx)
310 {
311         ctx->buf_width = ALIGN(ctx->img_width, S5P_FIMV_NV12MT_HALIGN_V6);
312         ctx->buf_height = ALIGN(ctx->img_height, S5P_FIMV_NV12MT_VALIGN_V6);
313         mfc_debug(2, "SEQ Done: Movie dimensions %dx%d,\n"
314                         "buffer dimensions: %dx%d\n", ctx->img_width,
315                         ctx->img_height, ctx->buf_width, ctx->buf_height);
316
317         ctx->luma_size = calc_plane(ctx->img_width, ctx->img_height);
318         ctx->chroma_size = calc_plane(ctx->img_width, (ctx->img_height >> 1));
319         if (ctx->codec_mode == S5P_MFC_CODEC_H264_DEC ||
320                         ctx->codec_mode == S5P_MFC_CODEC_H264_MVC_DEC) {
321                 ctx->mv_size = S5P_MFC_DEC_MV_SIZE_V6(ctx->img_width,
322                                 ctx->img_height);
323                 ctx->mv_size = ALIGN(ctx->mv_size, 16);
324         } else {
325                 ctx->mv_size = 0;
326         }
327 }
328
329 void s5p_mfc_enc_calc_src_size_v6(struct s5p_mfc_ctx *ctx)
330 {
331         unsigned int mb_width, mb_height;
332
333         mb_width = MB_WIDTH(ctx->img_width);
334         mb_height = MB_HEIGHT(ctx->img_height);
335
336         ctx->buf_width = ALIGN(ctx->img_width, S5P_FIMV_NV12M_HALIGN_V6);
337         ctx->luma_size = ALIGN((mb_width * mb_height) * 256, 256);
338         ctx->chroma_size = ALIGN((mb_width * mb_height) * 128, 256);
339 }
340
341 /* Set registers for decoding stream buffer */
342 int s5p_mfc_set_dec_stream_buffer_v6(struct s5p_mfc_ctx *ctx, int buf_addr,
343                   unsigned int start_num_byte, unsigned int strm_size)
344 {
345         struct s5p_mfc_dev *dev = ctx->dev;
346         struct s5p_mfc_buf_size *buf_size = dev->variant->buf_size;
347
348         mfc_debug_enter();
349         mfc_debug(2, "inst_no: %d, buf_addr: 0x%08x,\n"
350                 "buf_size: 0x%08x (%d)\n",
351                 ctx->inst_no, buf_addr, strm_size, strm_size);
352         WRITEL(strm_size, S5P_FIMV_D_STREAM_DATA_SIZE_V6);
353         WRITEL(buf_addr, S5P_FIMV_D_CPB_BUFFER_ADDR_V6);
354         WRITEL(buf_size->cpb, S5P_FIMV_D_CPB_BUFFER_SIZE_V6);
355         WRITEL(start_num_byte, S5P_FIMV_D_CPB_BUFFER_OFFSET_V6);
356
357         mfc_debug_leave();
358         return 0;
359 }
360
361 /* Set decoding frame buffer */
362 int s5p_mfc_set_dec_frame_buffer_v6(struct s5p_mfc_ctx *ctx)
363 {
364         unsigned int frame_size, i;
365         unsigned int frame_size_ch, frame_size_mv;
366         struct s5p_mfc_dev *dev = ctx->dev;
367         size_t buf_addr1;
368         int buf_size1;
369         int align_gap;
370
371         buf_addr1 = ctx->bank1.dma;
372         buf_size1 = ctx->bank1.size;
373
374         mfc_debug(2, "Buf1: %p (%d)\n", (void *)buf_addr1, buf_size1);
375         mfc_debug(2, "Total DPB COUNT: %d\n", ctx->total_dpb_count);
376         mfc_debug(2, "Setting display delay to %d\n", ctx->display_delay);
377
378         WRITEL(ctx->total_dpb_count, S5P_FIMV_D_NUM_DPB_V6);
379         WRITEL(ctx->luma_size, S5P_FIMV_D_LUMA_DPB_SIZE_V6);
380         WRITEL(ctx->chroma_size, S5P_FIMV_D_CHROMA_DPB_SIZE_V6);
381
382         WRITEL(buf_addr1, S5P_FIMV_D_SCRATCH_BUFFER_ADDR_V6);
383         WRITEL(ctx->scratch_buf_size, S5P_FIMV_D_SCRATCH_BUFFER_SIZE_V6);
384         buf_addr1 += ctx->scratch_buf_size;
385         buf_size1 -= ctx->scratch_buf_size;
386
387         if (ctx->codec_mode == S5P_FIMV_CODEC_H264_DEC ||
388                         ctx->codec_mode == S5P_FIMV_CODEC_H264_MVC_DEC){
389                 WRITEL(ctx->mv_size, S5P_FIMV_D_MV_BUFFER_SIZE_V6);
390                 WRITEL(ctx->mv_count, S5P_FIMV_D_NUM_MV_V6);
391         }
392
393         frame_size = ctx->luma_size;
394         frame_size_ch = ctx->chroma_size;
395         frame_size_mv = ctx->mv_size;
396         mfc_debug(2, "Frame size: %d ch: %d mv: %d\n",
397                         frame_size, frame_size_ch, frame_size_mv);
398
399         for (i = 0; i < ctx->total_dpb_count; i++) {
400                 /* Bank2 */
401                 mfc_debug(2, "Luma %d: %x\n", i,
402                                         ctx->dst_bufs[i].cookie.raw.luma);
403                 WRITEL(ctx->dst_bufs[i].cookie.raw.luma,
404                                 S5P_FIMV_D_LUMA_DPB_V6 + i * 4);
405                 mfc_debug(2, "\tChroma %d: %x\n", i,
406                                         ctx->dst_bufs[i].cookie.raw.chroma);
407                 WRITEL(ctx->dst_bufs[i].cookie.raw.chroma,
408                                 S5P_FIMV_D_CHROMA_DPB_V6 + i * 4);
409         }
410         if (ctx->codec_mode == S5P_MFC_CODEC_H264_DEC ||
411                         ctx->codec_mode == S5P_MFC_CODEC_H264_MVC_DEC) {
412                 for (i = 0; i < ctx->mv_count; i++) {
413                         /* To test alignment */
414                         align_gap = buf_addr1;
415                         buf_addr1 = ALIGN(buf_addr1, 16);
416                         align_gap = buf_addr1 - align_gap;
417                         buf_size1 -= align_gap;
418
419                         mfc_debug(2, "\tBuf1: %x, size: %d\n",
420                                         buf_addr1, buf_size1);
421                         WRITEL(buf_addr1, S5P_FIMV_D_MV_BUFFER_V6 + i * 4);
422                         buf_addr1 += frame_size_mv;
423                         buf_size1 -= frame_size_mv;
424                 }
425         }
426
427         mfc_debug(2, "Buf1: %u, buf_size1: %d (frames %d)\n",
428                         buf_addr1, buf_size1, ctx->total_dpb_count);
429         if (buf_size1 < 0) {
430                 mfc_debug(2, "Not enough memory has been allocated.\n");
431                 return -ENOMEM;
432         }
433
434         WRITEL(ctx->inst_no, S5P_FIMV_INSTANCE_ID_V6);
435         s5p_mfc_hw_call(dev->mfc_cmds, cmd_host2risc, dev,
436                         S5P_FIMV_CH_INIT_BUFS_V6, NULL);
437
438         mfc_debug(2, "After setting buffers.\n");
439         return 0;
440 }
441
442 /* Set registers for encoding stream buffer */
443 int s5p_mfc_set_enc_stream_buffer_v6(struct s5p_mfc_ctx *ctx,
444                 unsigned long addr, unsigned int size)
445 {
446         struct s5p_mfc_dev *dev = ctx->dev;
447
448         WRITEL(addr, S5P_FIMV_E_STREAM_BUFFER_ADDR_V6); /* 16B align */
449         WRITEL(size, S5P_FIMV_E_STREAM_BUFFER_SIZE_V6);
450
451         mfc_debug(2, "stream buf addr: 0x%08lx, size: 0x%d",
452                 addr, size);
453
454         return 0;
455 }
456
457 void s5p_mfc_set_enc_frame_buffer_v6(struct s5p_mfc_ctx *ctx,
458                 unsigned long y_addr, unsigned long c_addr)
459 {
460         struct s5p_mfc_dev *dev = ctx->dev;
461
462         WRITEL(y_addr, S5P_FIMV_E_SOURCE_LUMA_ADDR_V6); /* 256B align */
463         WRITEL(c_addr, S5P_FIMV_E_SOURCE_CHROMA_ADDR_V6);
464
465         mfc_debug(2, "enc src y buf addr: 0x%08lx", y_addr);
466         mfc_debug(2, "enc src c buf addr: 0x%08lx", c_addr);
467 }
468
469 void s5p_mfc_get_enc_frame_buffer_v6(struct s5p_mfc_ctx *ctx,
470                 unsigned long *y_addr, unsigned long *c_addr)
471 {
472         struct s5p_mfc_dev *dev = ctx->dev;
473         unsigned long enc_recon_y_addr, enc_recon_c_addr;
474
475         *y_addr = READL(S5P_FIMV_E_ENCODED_SOURCE_LUMA_ADDR_V6);
476         *c_addr = READL(S5P_FIMV_E_ENCODED_SOURCE_CHROMA_ADDR_V6);
477
478         enc_recon_y_addr = READL(S5P_FIMV_E_RECON_LUMA_DPB_ADDR_V6);
479         enc_recon_c_addr = READL(S5P_FIMV_E_RECON_CHROMA_DPB_ADDR_V6);
480
481         mfc_debug(2, "recon y addr: 0x%08lx", enc_recon_y_addr);
482         mfc_debug(2, "recon c addr: 0x%08lx", enc_recon_c_addr);
483 }
484
485 /* Set encoding ref & codec buffer */
486 int s5p_mfc_set_enc_ref_buffer_v6(struct s5p_mfc_ctx *ctx)
487 {
488         struct s5p_mfc_dev *dev = ctx->dev;
489         size_t buf_addr1;
490         int i, buf_size1;
491
492         mfc_debug_enter();
493
494         buf_addr1 = ctx->bank1.dma;
495         buf_size1 = ctx->bank1.size;
496
497         mfc_debug(2, "Buf1: %p (%d)\n", (void *)buf_addr1, buf_size1);
498
499         for (i = 0; i < ctx->dpb_count; i++) {
500                 WRITEL(buf_addr1, S5P_FIMV_E_LUMA_DPB_V6 + (4 * i));
501                 buf_addr1 += ctx->luma_dpb_size;
502                 WRITEL(buf_addr1, S5P_FIMV_E_CHROMA_DPB_V6 + (4 * i));
503                 buf_addr1 += ctx->chroma_dpb_size;
504                 WRITEL(buf_addr1, S5P_FIMV_E_ME_BUFFER_V6 + (4 * i));
505                 buf_addr1 += ctx->me_buffer_size;
506                 buf_size1 -= (ctx->luma_dpb_size + ctx->chroma_dpb_size +
507                         ctx->me_buffer_size);
508         }
509
510         WRITEL(buf_addr1, S5P_FIMV_E_SCRATCH_BUFFER_ADDR_V6);
511         WRITEL(ctx->scratch_buf_size, S5P_FIMV_E_SCRATCH_BUFFER_SIZE_V6);
512         buf_addr1 += ctx->scratch_buf_size;
513         buf_size1 -= ctx->scratch_buf_size;
514
515         WRITEL(buf_addr1, S5P_FIMV_E_TMV_BUFFER0_V6);
516         buf_addr1 += ctx->tmv_buffer_size >> 1;
517         WRITEL(buf_addr1, S5P_FIMV_E_TMV_BUFFER1_V6);
518         buf_addr1 += ctx->tmv_buffer_size >> 1;
519         buf_size1 -= ctx->tmv_buffer_size;
520
521         mfc_debug(2, "Buf1: %u, buf_size1: %d (ref frames %d)\n",
522                         buf_addr1, buf_size1, ctx->dpb_count);
523         if (buf_size1 < 0) {
524                 mfc_debug(2, "Not enough memory has been allocated.\n");
525                 return -ENOMEM;
526         }
527
528         WRITEL(ctx->inst_no, S5P_FIMV_INSTANCE_ID_V6);
529         s5p_mfc_hw_call(dev->mfc_cmds, cmd_host2risc, dev,
530                         S5P_FIMV_CH_INIT_BUFS_V6, NULL);
531
532         mfc_debug_leave();
533
534         return 0;
535 }
536
537 static int s5p_mfc_set_slice_mode(struct s5p_mfc_ctx *ctx)
538 {
539         struct s5p_mfc_dev *dev = ctx->dev;
540
541         /* multi-slice control */
542         /* multi-slice MB number or bit size */
543         WRITEL(ctx->slice_mode, S5P_FIMV_E_MSLICE_MODE_V6);
544         if (ctx->slice_mode == V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB) {
545                 WRITEL(ctx->slice_size.mb, S5P_FIMV_E_MSLICE_SIZE_MB_V6);
546         } else if (ctx->slice_mode ==
547                         V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES) {
548                 WRITEL(ctx->slice_size.bits, S5P_FIMV_E_MSLICE_SIZE_BITS_V6);
549         } else {
550                 WRITEL(0x0, S5P_FIMV_E_MSLICE_SIZE_MB_V6);
551                 WRITEL(0x0, S5P_FIMV_E_MSLICE_SIZE_BITS_V6);
552         }
553
554         return 0;
555 }
556
557 static int s5p_mfc_set_enc_params(struct s5p_mfc_ctx *ctx)
558 {
559         struct s5p_mfc_dev *dev = ctx->dev;
560         struct s5p_mfc_enc_params *p = &ctx->enc_params;
561         unsigned int reg = 0;
562
563         mfc_debug_enter();
564
565         /* width */
566         WRITEL(ctx->img_width, S5P_FIMV_E_FRAME_WIDTH_V6); /* 16 align */
567         /* height */
568         WRITEL(ctx->img_height, S5P_FIMV_E_FRAME_HEIGHT_V6); /* 16 align */
569
570         /* cropped width */
571         WRITEL(ctx->img_width, S5P_FIMV_E_CROPPED_FRAME_WIDTH_V6);
572         /* cropped height */
573         WRITEL(ctx->img_height, S5P_FIMV_E_CROPPED_FRAME_HEIGHT_V6);
574         /* cropped offset */
575         WRITEL(0x0, S5P_FIMV_E_FRAME_CROP_OFFSET_V6);
576
577         /* pictype : IDR period */
578         reg = 0;
579         reg |= p->gop_size & 0xFFFF;
580         WRITEL(reg, S5P_FIMV_E_GOP_CONFIG_V6);
581
582         /* multi-slice control */
583         /* multi-slice MB number or bit size */
584         ctx->slice_mode = p->slice_mode;
585         reg = 0;
586         if (p->slice_mode == V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB) {
587                 reg |= (0x1 << 3);
588                 WRITEL(reg, S5P_FIMV_E_ENC_OPTIONS_V6);
589                 ctx->slice_size.mb = p->slice_mb;
590         } else if (p->slice_mode == V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES) {
591                 reg |= (0x1 << 3);
592                 WRITEL(reg, S5P_FIMV_E_ENC_OPTIONS_V6);
593                 ctx->slice_size.bits = p->slice_bit;
594         } else {
595                 reg &= ~(0x1 << 3);
596                 WRITEL(reg, S5P_FIMV_E_ENC_OPTIONS_V6);
597         }
598
599         s5p_mfc_set_slice_mode(ctx);
600
601         /* cyclic intra refresh */
602         WRITEL(p->intra_refresh_mb, S5P_FIMV_E_IR_SIZE_V6);
603         reg = READL(S5P_FIMV_E_ENC_OPTIONS_V6);
604         if (p->intra_refresh_mb == 0)
605                 reg &= ~(0x1 << 4);
606         else
607                 reg |= (0x1 << 4);
608         WRITEL(reg, S5P_FIMV_E_ENC_OPTIONS_V6);
609
610         /* 'NON_REFERENCE_STORE_ENABLE' for debugging */
611         reg = READL(S5P_FIMV_E_ENC_OPTIONS_V6);
612         reg &= ~(0x1 << 9);
613         WRITEL(reg, S5P_FIMV_E_ENC_OPTIONS_V6);
614
615         /* memory structure cur. frame */
616         if (ctx->src_fmt->fourcc == V4L2_PIX_FMT_NV12M) {
617                 /* 0: Linear, 1: 2D tiled*/
618                 reg = READL(S5P_FIMV_E_ENC_OPTIONS_V6);
619                 reg &= ~(0x1 << 7);
620                 WRITEL(reg, S5P_FIMV_E_ENC_OPTIONS_V6);
621                 /* 0: NV12(CbCr), 1: NV21(CrCb) */
622                 WRITEL(0x0, S5P_FIMV_PIXEL_FORMAT_V6);
623         } else if (ctx->src_fmt->fourcc == V4L2_PIX_FMT_NV21M) {
624                 /* 0: Linear, 1: 2D tiled*/
625                 reg = READL(S5P_FIMV_E_ENC_OPTIONS_V6);
626                 reg &= ~(0x1 << 7);
627                 WRITEL(reg, S5P_FIMV_E_ENC_OPTIONS_V6);
628                 /* 0: NV12(CbCr), 1: NV21(CrCb) */
629                 WRITEL(0x1, S5P_FIMV_PIXEL_FORMAT_V6);
630         } else if (ctx->src_fmt->fourcc == V4L2_PIX_FMT_NV12MT_16X16) {
631                 /* 0: Linear, 1: 2D tiled*/
632                 reg = READL(S5P_FIMV_E_ENC_OPTIONS_V6);
633                 reg |= (0x1 << 7);
634                 WRITEL(reg, S5P_FIMV_E_ENC_OPTIONS_V6);
635                 /* 0: NV12(CbCr), 1: NV21(CrCb) */
636                 WRITEL(0x0, S5P_FIMV_PIXEL_FORMAT_V6);
637         }
638
639         /* memory structure recon. frame */
640         /* 0: Linear, 1: 2D tiled */
641         reg = READL(S5P_FIMV_E_ENC_OPTIONS_V6);
642         reg |= (0x1 << 8);
643         WRITEL(reg, S5P_FIMV_E_ENC_OPTIONS_V6);
644
645         /* padding control & value */
646         WRITEL(0x0, S5P_FIMV_E_PADDING_CTRL_V6);
647         if (p->pad) {
648                 reg = 0;
649                 /** enable */
650                 reg |= (1 << 31);
651                 /** cr value */
652                 reg |= ((p->pad_cr & 0xFF) << 16);
653                 /** cb value */
654                 reg |= ((p->pad_cb & 0xFF) << 8);
655                 /** y value */
656                 reg |= p->pad_luma & 0xFF;
657                 WRITEL(reg, S5P_FIMV_E_PADDING_CTRL_V6);
658         }
659
660         /* rate control config. */
661         reg = 0;
662         /* frame-level rate control */
663         reg |= ((p->rc_frame & 0x1) << 9);
664         WRITEL(reg, S5P_FIMV_E_RC_CONFIG_V6);
665
666         /* bit rate */
667         if (p->rc_frame)
668                 WRITEL(p->rc_bitrate,
669                         S5P_FIMV_E_RC_BIT_RATE_V6);
670         else
671                 WRITEL(1, S5P_FIMV_E_RC_BIT_RATE_V6);
672
673         /* reaction coefficient */
674         if (p->rc_frame) {
675                 if (p->rc_reaction_coeff < TIGHT_CBR_MAX) /* tight CBR */
676                         WRITEL(1, S5P_FIMV_E_RC_RPARAM_V6);
677                 else                                      /* loose CBR */
678                         WRITEL(2, S5P_FIMV_E_RC_RPARAM_V6);
679         }
680
681         /* seq header ctrl */
682         reg = READL(S5P_FIMV_E_ENC_OPTIONS_V6);
683         reg &= ~(0x1 << 2);
684         reg |= ((p->seq_hdr_mode & 0x1) << 2);
685
686         /* frame skip mode */
687         reg &= ~(0x3);
688         reg |= (p->frame_skip_mode & 0x3);
689         WRITEL(reg, S5P_FIMV_E_ENC_OPTIONS_V6);
690
691         /* 'DROP_CONTROL_ENABLE', disable */
692         reg = READL(S5P_FIMV_E_RC_CONFIG_V6);
693         reg &= ~(0x1 << 10);
694         WRITEL(reg, S5P_FIMV_E_RC_CONFIG_V6);
695
696         /* setting for MV range [16, 256] */
697         reg = 0;
698         reg &= ~(0x3FFF);
699         reg = 256;
700         WRITEL(reg, S5P_FIMV_E_MV_HOR_RANGE_V6);
701
702         reg = 0;
703         reg &= ~(0x3FFF);
704         reg = 256;
705         WRITEL(reg, S5P_FIMV_E_MV_VER_RANGE_V6);
706
707         WRITEL(0x0, S5P_FIMV_E_FRAME_INSERTION_V6);
708         WRITEL(0x0, S5P_FIMV_E_ROI_BUFFER_ADDR_V6);
709         WRITEL(0x0, S5P_FIMV_E_PARAM_CHANGE_V6);
710         WRITEL(0x0, S5P_FIMV_E_RC_ROI_CTRL_V6);
711         WRITEL(0x0, S5P_FIMV_E_PICTURE_TAG_V6);
712
713         WRITEL(0x0, S5P_FIMV_E_BIT_COUNT_ENABLE_V6);
714         WRITEL(0x0, S5P_FIMV_E_MAX_BIT_COUNT_V6);
715         WRITEL(0x0, S5P_FIMV_E_MIN_BIT_COUNT_V6);
716
717         WRITEL(0x0, S5P_FIMV_E_METADATA_BUFFER_ADDR_V6);
718         WRITEL(0x0, S5P_FIMV_E_METADATA_BUFFER_SIZE_V6);
719
720         mfc_debug_leave();
721
722         return 0;
723 }
724
725 static int s5p_mfc_set_enc_params_h264(struct s5p_mfc_ctx *ctx)
726 {
727         struct s5p_mfc_dev *dev = ctx->dev;
728         struct s5p_mfc_enc_params *p = &ctx->enc_params;
729         struct s5p_mfc_h264_enc_params *p_h264 = &p->codec.h264;
730         unsigned int reg = 0;
731         int i;
732
733         mfc_debug_enter();
734
735         s5p_mfc_set_enc_params(ctx);
736
737         /* pictype : number of B */
738         reg = READL(S5P_FIMV_E_GOP_CONFIG_V6);
739         reg &= ~(0x3 << 16);
740         reg |= ((p->num_b_frame & 0x3) << 16);
741         WRITEL(reg, S5P_FIMV_E_GOP_CONFIG_V6);
742
743         /* profile & level */
744         reg = 0;
745         /** level */
746         reg |= ((p_h264->level & 0xFF) << 8);
747         /** profile - 0 ~ 3 */
748         reg |= p_h264->profile & 0x3F;
749         WRITEL(reg, S5P_FIMV_E_PICTURE_PROFILE_V6);
750
751         /* rate control config. */
752         reg = READL(S5P_FIMV_E_RC_CONFIG_V6);
753         /** macroblock level rate control */
754         reg &= ~(0x1 << 8);
755         reg |= ((p->rc_mb & 0x1) << 8);
756         WRITEL(reg, S5P_FIMV_E_RC_CONFIG_V6);
757         /** frame QP */
758         reg &= ~(0x3F);
759         reg |= p_h264->rc_frame_qp & 0x3F;
760         WRITEL(reg, S5P_FIMV_E_RC_CONFIG_V6);
761
762         /* max & min value of QP */
763         reg = 0;
764         /** max QP */
765         reg |= ((p_h264->rc_max_qp & 0x3F) << 8);
766         /** min QP */
767         reg |= p_h264->rc_min_qp & 0x3F;
768         WRITEL(reg, S5P_FIMV_E_RC_QP_BOUND_V6);
769
770         /* other QPs */
771         WRITEL(0x0, S5P_FIMV_E_FIXED_PICTURE_QP_V6);
772         if (!p->rc_frame && !p->rc_mb) {
773                 reg = 0;
774                 reg |= ((p_h264->rc_b_frame_qp & 0x3F) << 16);
775                 reg |= ((p_h264->rc_p_frame_qp & 0x3F) << 8);
776                 reg |= p_h264->rc_frame_qp & 0x3F;
777                 WRITEL(reg, S5P_FIMV_E_FIXED_PICTURE_QP_V6);
778         }
779
780         /* frame rate */
781         if (p->rc_frame && p->rc_framerate_num && p->rc_framerate_denom) {
782                 reg = 0;
783                 reg |= ((p->rc_framerate_num & 0xFFFF) << 16);
784                 reg |= p->rc_framerate_denom & 0xFFFF;
785                 WRITEL(reg, S5P_FIMV_E_RC_FRAME_RATE_V6);
786         }
787
788         /* vbv buffer size */
789         if (p->frame_skip_mode ==
790                         V4L2_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE_BUF_LIMIT) {
791                 WRITEL(p_h264->cpb_size & 0xFFFF,
792                                 S5P_FIMV_E_VBV_BUFFER_SIZE_V6);
793
794                 if (p->rc_frame)
795                         WRITEL(p->vbv_delay, S5P_FIMV_E_VBV_INIT_DELAY_V6);
796         }
797
798         /* interlace */
799         reg = 0;
800         reg |= ((p_h264->interlace & 0x1) << 3);
801         WRITEL(reg, S5P_FIMV_E_H264_OPTIONS_V6);
802
803         /* height */
804         if (p_h264->interlace) {
805                 WRITEL(ctx->img_height >> 1,
806                                 S5P_FIMV_E_FRAME_HEIGHT_V6); /* 32 align */
807                 /* cropped height */
808                 WRITEL(ctx->img_height >> 1,
809                                 S5P_FIMV_E_CROPPED_FRAME_HEIGHT_V6);
810         }
811
812         /* loop filter ctrl */
813         reg = READL(S5P_FIMV_E_H264_OPTIONS_V6);
814         reg &= ~(0x3 << 1);
815         reg |= ((p_h264->loop_filter_mode & 0x3) << 1);
816         WRITEL(reg, S5P_FIMV_E_H264_OPTIONS_V6);
817
818         /* loopfilter alpha offset */
819         if (p_h264->loop_filter_alpha < 0) {
820                 reg = 0x10;
821                 reg |= (0xFF - p_h264->loop_filter_alpha) + 1;
822         } else {
823                 reg = 0x00;
824                 reg |= (p_h264->loop_filter_alpha & 0xF);
825         }
826         WRITEL(reg, S5P_FIMV_E_H264_LF_ALPHA_OFFSET_V6);
827
828         /* loopfilter beta offset */
829         if (p_h264->loop_filter_beta < 0) {
830                 reg = 0x10;
831                 reg |= (0xFF - p_h264->loop_filter_beta) + 1;
832         } else {
833                 reg = 0x00;
834                 reg |= (p_h264->loop_filter_beta & 0xF);
835         }
836         WRITEL(reg, S5P_FIMV_E_H264_LF_BETA_OFFSET_V6);
837
838         /* entropy coding mode */
839         reg = READL(S5P_FIMV_E_H264_OPTIONS_V6);
840         reg &= ~(0x1);
841         reg |= p_h264->entropy_mode & 0x1;
842         WRITEL(reg, S5P_FIMV_E_H264_OPTIONS_V6);
843
844         /* number of ref. picture */
845         reg = READL(S5P_FIMV_E_H264_OPTIONS_V6);
846         reg &= ~(0x1 << 7);
847         reg |= (((p_h264->num_ref_pic_4p - 1) & 0x1) << 7);
848         WRITEL(reg, S5P_FIMV_E_H264_OPTIONS_V6);
849
850         /* 8x8 transform enable */
851         reg = READL(S5P_FIMV_E_H264_OPTIONS_V6);
852         reg &= ~(0x3 << 12);
853         reg |= ((p_h264->_8x8_transform & 0x3) << 12);
854         WRITEL(reg, S5P_FIMV_E_H264_OPTIONS_V6);
855
856         /* macroblock adaptive scaling features */
857         WRITEL(0x0, S5P_FIMV_E_MB_RC_CONFIG_V6);
858         if (p->rc_mb) {
859                 reg = 0;
860                 /** dark region */
861                 reg |= ((p_h264->rc_mb_dark & 0x1) << 3);
862                 /** smooth region */
863                 reg |= ((p_h264->rc_mb_smooth & 0x1) << 2);
864                 /** static region */
865                 reg |= ((p_h264->rc_mb_static & 0x1) << 1);
866                 /** high activity region */
867                 reg |= p_h264->rc_mb_activity & 0x1;
868                 WRITEL(reg, S5P_FIMV_E_MB_RC_CONFIG_V6);
869         }
870
871         /* aspect ratio VUI */
872         reg = READL(S5P_FIMV_E_H264_OPTIONS_V6);
873         reg &= ~(0x1 << 5);
874         reg |= ((p_h264->vui_sar & 0x1) << 5);
875         WRITEL(reg, S5P_FIMV_E_H264_OPTIONS_V6);
876
877         WRITEL(0x0, S5P_FIMV_E_ASPECT_RATIO_V6);
878         WRITEL(0x0, S5P_FIMV_E_EXTENDED_SAR_V6);
879         if (p_h264->vui_sar) {
880                 /* aspect ration IDC */
881                 reg = 0;
882                 reg |= p_h264->vui_sar_idc & 0xFF;
883                 WRITEL(reg, S5P_FIMV_E_ASPECT_RATIO_V6);
884                 if (p_h264->vui_sar_idc == 0xFF) {
885                         /* extended SAR */
886                         reg = 0;
887                         reg |= (p_h264->vui_ext_sar_width & 0xFFFF) << 16;
888                         reg |= p_h264->vui_ext_sar_height & 0xFFFF;
889                         WRITEL(reg, S5P_FIMV_E_EXTENDED_SAR_V6);
890                 }
891         }
892
893         /* intra picture period for H.264 open GOP */
894         /* control */
895         reg = READL(S5P_FIMV_E_H264_OPTIONS_V6);
896         reg &= ~(0x1 << 4);
897         reg |= ((p_h264->open_gop & 0x1) << 4);
898         WRITEL(reg, S5P_FIMV_E_H264_OPTIONS_V6);
899         /* value */
900         WRITEL(0x0, S5P_FIMV_E_H264_I_PERIOD_V6);
901         if (p_h264->open_gop) {
902                 reg = 0;
903                 reg |= p_h264->open_gop_size & 0xFFFF;
904                 WRITEL(reg, S5P_FIMV_E_H264_I_PERIOD_V6);
905         }
906
907         /* 'WEIGHTED_BI_PREDICTION' for B is disable */
908         reg = READL(S5P_FIMV_E_H264_OPTIONS_V6);
909         reg &= ~(0x3 << 9);
910         WRITEL(reg, S5P_FIMV_E_H264_OPTIONS_V6);
911
912         /* 'CONSTRAINED_INTRA_PRED_ENABLE' is disable */
913         reg = READL(S5P_FIMV_E_H264_OPTIONS_V6);
914         reg &= ~(0x1 << 14);
915         WRITEL(reg, S5P_FIMV_E_H264_OPTIONS_V6);
916
917         /* ASO */
918         reg = READL(S5P_FIMV_E_H264_OPTIONS_V6);
919         reg &= ~(0x1 << 6);
920         reg |= ((p_h264->aso & 0x1) << 6);
921         WRITEL(reg, S5P_FIMV_E_H264_OPTIONS_V6);
922
923         /* hier qp enable */
924         reg = READL(S5P_FIMV_E_H264_OPTIONS_V6);
925         reg &= ~(0x1 << 8);
926         reg |= ((p_h264->open_gop & 0x1) << 8);
927         WRITEL(reg, S5P_FIMV_E_H264_OPTIONS_V6);
928         reg = 0;
929         if (p_h264->hier_qp && p_h264->hier_qp_layer) {
930                 reg |= (p_h264->hier_qp_type & 0x1) << 0x3;
931                 reg |= p_h264->hier_qp_layer & 0x7;
932                 WRITEL(reg, S5P_FIMV_E_H264_NUM_T_LAYER_V6);
933                 /* QP value for each layer */
934                 for (i = 0; i < (p_h264->hier_qp_layer & 0x7); i++)
935                         WRITEL(p_h264->hier_qp_layer_qp[i],
936                                 S5P_FIMV_E_H264_HIERARCHICAL_QP_LAYER0_V6 +
937                                 i * 4);
938         }
939         /* number of coding layer should be zero when hierarchical is disable */
940         WRITEL(reg, S5P_FIMV_E_H264_NUM_T_LAYER_V6);
941
942         /* frame packing SEI generation */
943         reg = READL(S5P_FIMV_E_H264_OPTIONS_V6);
944         reg &= ~(0x1 << 25);
945         reg |= ((p_h264->sei_frame_packing & 0x1) << 25);
946         WRITEL(reg, S5P_FIMV_E_H264_OPTIONS_V6);
947         if (p_h264->sei_frame_packing) {
948                 reg = 0;
949                 /** current frame0 flag */
950                 reg |= ((p_h264->sei_fp_curr_frame_0 & 0x1) << 2);
951                 /** arrangement type */
952                 reg |= p_h264->sei_fp_arrangement_type & 0x3;
953                 WRITEL(reg, S5P_FIMV_E_H264_FRAME_PACKING_SEI_INFO_V6);
954         }
955
956         if (p_h264->fmo) {
957                 switch (p_h264->fmo_map_type) {
958                 case V4L2_MPEG_VIDEO_H264_FMO_MAP_TYPE_INTERLEAVED_SLICES:
959                         if (p_h264->fmo_slice_grp > 4)
960                                 p_h264->fmo_slice_grp = 4;
961                         for (i = 0; i < (p_h264->fmo_slice_grp & 0xF); i++)
962                                 WRITEL(p_h264->fmo_run_len[i] - 1,
963                                 S5P_FIMV_E_H264_FMO_RUN_LENGTH_MINUS1_0_V6 +
964                                 i * 4);
965                         break;
966                 case V4L2_MPEG_VIDEO_H264_FMO_MAP_TYPE_SCATTERED_SLICES:
967                         if (p_h264->fmo_slice_grp > 4)
968                                 p_h264->fmo_slice_grp = 4;
969                         break;
970                 case V4L2_MPEG_VIDEO_H264_FMO_MAP_TYPE_RASTER_SCAN:
971                 case V4L2_MPEG_VIDEO_H264_FMO_MAP_TYPE_WIPE_SCAN:
972                         if (p_h264->fmo_slice_grp > 2)
973                                 p_h264->fmo_slice_grp = 2;
974                         WRITEL(p_h264->fmo_chg_dir & 0x1,
975                                 S5P_FIMV_E_H264_FMO_SLICE_GRP_CHANGE_DIR_V6);
976                         /* the valid range is 0 ~ number of macroblocks -1 */
977                         WRITEL(p_h264->fmo_chg_rate,
978                                 S5P_FIMV_E_H264_FMO_SLICE_GRP_CHANGE_RATE_MINUS1_V6);
979                         break;
980                 default:
981                         mfc_err("Unsupported map type for FMO: %d\n",
982                                         p_h264->fmo_map_type);
983                         p_h264->fmo_map_type = 0;
984                         p_h264->fmo_slice_grp = 1;
985                         break;
986                 }
987
988                 WRITEL(p_h264->fmo_map_type,
989                                 S5P_FIMV_E_H264_FMO_SLICE_GRP_MAP_TYPE_V6);
990                 WRITEL(p_h264->fmo_slice_grp - 1,
991                                 S5P_FIMV_E_H264_FMO_NUM_SLICE_GRP_MINUS1_V6);
992         } else {
993                 WRITEL(0, S5P_FIMV_E_H264_FMO_NUM_SLICE_GRP_MINUS1_V6);
994         }
995
996         mfc_debug_leave();
997
998         return 0;
999 }
1000
1001 static int s5p_mfc_set_enc_params_mpeg4(struct s5p_mfc_ctx *ctx)
1002 {
1003         struct s5p_mfc_dev *dev = ctx->dev;
1004         struct s5p_mfc_enc_params *p = &ctx->enc_params;
1005         struct s5p_mfc_mpeg4_enc_params *p_mpeg4 = &p->codec.mpeg4;
1006         unsigned int reg = 0;
1007
1008         mfc_debug_enter();
1009
1010         s5p_mfc_set_enc_params(ctx);
1011
1012         /* pictype : number of B */
1013         reg = READL(S5P_FIMV_E_GOP_CONFIG_V6);
1014         reg &= ~(0x3 << 16);
1015         reg |= ((p->num_b_frame & 0x3) << 16);
1016         WRITEL(reg, S5P_FIMV_E_GOP_CONFIG_V6);
1017
1018         /* profile & level */
1019         reg = 0;
1020         /** level */
1021         reg |= ((p_mpeg4->level & 0xFF) << 8);
1022         /** profile - 0 ~ 1 */
1023         reg |= p_mpeg4->profile & 0x3F;
1024         WRITEL(reg, S5P_FIMV_E_PICTURE_PROFILE_V6);
1025
1026         /* rate control config. */
1027         reg = READL(S5P_FIMV_E_RC_CONFIG_V6);
1028         /** macroblock level rate control */
1029         reg &= ~(0x1 << 8);
1030         reg |= ((p->rc_mb & 0x1) << 8);
1031         WRITEL(reg, S5P_FIMV_E_RC_CONFIG_V6);
1032         /** frame QP */
1033         reg &= ~(0x3F);
1034         reg |= p_mpeg4->rc_frame_qp & 0x3F;
1035         WRITEL(reg, S5P_FIMV_E_RC_CONFIG_V6);
1036
1037         /* max & min value of QP */
1038         reg = 0;
1039         /** max QP */
1040         reg |= ((p_mpeg4->rc_max_qp & 0x3F) << 8);
1041         /** min QP */
1042         reg |= p_mpeg4->rc_min_qp & 0x3F;
1043         WRITEL(reg, S5P_FIMV_E_RC_QP_BOUND_V6);
1044
1045         /* other QPs */
1046         WRITEL(0x0, S5P_FIMV_E_FIXED_PICTURE_QP_V6);
1047         if (!p->rc_frame && !p->rc_mb) {
1048                 reg = 0;
1049                 reg |= ((p_mpeg4->rc_b_frame_qp & 0x3F) << 16);
1050                 reg |= ((p_mpeg4->rc_p_frame_qp & 0x3F) << 8);
1051                 reg |= p_mpeg4->rc_frame_qp & 0x3F;
1052                 WRITEL(reg, S5P_FIMV_E_FIXED_PICTURE_QP_V6);
1053         }
1054
1055         /* frame rate */
1056         if (p->rc_frame && p->rc_framerate_num && p->rc_framerate_denom) {
1057                 reg = 0;
1058                 reg |= ((p->rc_framerate_num & 0xFFFF) << 16);
1059                 reg |= p->rc_framerate_denom & 0xFFFF;
1060                 WRITEL(reg, S5P_FIMV_E_RC_FRAME_RATE_V6);
1061         }
1062
1063         /* vbv buffer size */
1064         if (p->frame_skip_mode ==
1065                         V4L2_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE_BUF_LIMIT) {
1066                 WRITEL(p->vbv_size & 0xFFFF, S5P_FIMV_E_VBV_BUFFER_SIZE_V6);
1067
1068                 if (p->rc_frame)
1069                         WRITEL(p->vbv_delay, S5P_FIMV_E_VBV_INIT_DELAY_V6);
1070         }
1071
1072         /* Disable HEC */
1073         WRITEL(0x0, S5P_FIMV_E_MPEG4_OPTIONS_V6);
1074         WRITEL(0x0, S5P_FIMV_E_MPEG4_HEC_PERIOD_V6);
1075
1076         mfc_debug_leave();
1077
1078         return 0;
1079 }
1080
1081 static int s5p_mfc_set_enc_params_h263(struct s5p_mfc_ctx *ctx)
1082 {
1083         struct s5p_mfc_dev *dev = ctx->dev;
1084         struct s5p_mfc_enc_params *p = &ctx->enc_params;
1085         struct s5p_mfc_mpeg4_enc_params *p_h263 = &p->codec.mpeg4;
1086         unsigned int reg = 0;
1087
1088         mfc_debug_enter();
1089
1090         s5p_mfc_set_enc_params(ctx);
1091
1092         /* profile & level */
1093         reg = 0;
1094         /** profile */
1095         reg |= (0x1 << 4);
1096         WRITEL(reg, S5P_FIMV_E_PICTURE_PROFILE_V6);
1097
1098         /* rate control config. */
1099         reg = READL(S5P_FIMV_E_RC_CONFIG_V6);
1100         /** macroblock level rate control */
1101         reg &= ~(0x1 << 8);
1102         reg |= ((p->rc_mb & 0x1) << 8);
1103         WRITEL(reg, S5P_FIMV_E_RC_CONFIG_V6);
1104         /** frame QP */
1105         reg &= ~(0x3F);
1106         reg |= p_h263->rc_frame_qp & 0x3F;
1107         WRITEL(reg, S5P_FIMV_E_RC_CONFIG_V6);
1108
1109         /* max & min value of QP */
1110         reg = 0;
1111         /** max QP */
1112         reg |= ((p_h263->rc_max_qp & 0x3F) << 8);
1113         /** min QP */
1114         reg |= p_h263->rc_min_qp & 0x3F;
1115         WRITEL(reg, S5P_FIMV_E_RC_QP_BOUND_V6);
1116
1117         /* other QPs */
1118         WRITEL(0x0, S5P_FIMV_E_FIXED_PICTURE_QP_V6);
1119         if (!p->rc_frame && !p->rc_mb) {
1120                 reg = 0;
1121                 reg |= ((p_h263->rc_b_frame_qp & 0x3F) << 16);
1122                 reg |= ((p_h263->rc_p_frame_qp & 0x3F) << 8);
1123                 reg |= p_h263->rc_frame_qp & 0x3F;
1124                 WRITEL(reg, S5P_FIMV_E_FIXED_PICTURE_QP_V6);
1125         }
1126
1127         /* frame rate */
1128         if (p->rc_frame && p->rc_framerate_num && p->rc_framerate_denom) {
1129                 reg = 0;
1130                 reg |= ((p->rc_framerate_num & 0xFFFF) << 16);
1131                 reg |= p->rc_framerate_denom & 0xFFFF;
1132                 WRITEL(reg, S5P_FIMV_E_RC_FRAME_RATE_V6);
1133         }
1134
1135         /* vbv buffer size */
1136         if (p->frame_skip_mode ==
1137                         V4L2_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE_BUF_LIMIT) {
1138                 WRITEL(p->vbv_size & 0xFFFF, S5P_FIMV_E_VBV_BUFFER_SIZE_V6);
1139
1140                 if (p->rc_frame)
1141                         WRITEL(p->vbv_delay, S5P_FIMV_E_VBV_INIT_DELAY_V6);
1142         }
1143
1144         mfc_debug_leave();
1145
1146         return 0;
1147 }
1148
1149 /* Initialize decoding */
1150 int s5p_mfc_init_decode_v6(struct s5p_mfc_ctx *ctx)
1151 {
1152         struct s5p_mfc_dev *dev = ctx->dev;
1153         unsigned int reg = 0;
1154         int fmo_aso_ctrl = 0;
1155
1156         mfc_debug_enter();
1157         mfc_debug(2, "InstNo: %d/%d\n", ctx->inst_no,
1158                         S5P_FIMV_CH_SEQ_HEADER_V6);
1159         mfc_debug(2, "BUFs: %08x %08x %08x\n",
1160                   READL(S5P_FIMV_D_CPB_BUFFER_ADDR_V6),
1161                   READL(S5P_FIMV_D_CPB_BUFFER_ADDR_V6),
1162                   READL(S5P_FIMV_D_CPB_BUFFER_ADDR_V6));
1163
1164         /* FMO_ASO_CTRL - 0: Enable, 1: Disable */
1165         reg |= (fmo_aso_ctrl << S5P_FIMV_D_OPT_FMO_ASO_CTRL_MASK_V6);
1166
1167         /* When user sets desplay_delay to 0,
1168          * It works as "display_delay enable" and delay set to 0.
1169          * If user wants display_delay disable, It should be
1170          * set to negative value. */
1171         if (ctx->display_delay >= 0) {
1172                 reg |= (0x1 << S5P_FIMV_D_OPT_DDELAY_EN_SHIFT_V6);
1173                 WRITEL(ctx->display_delay, S5P_FIMV_D_DISPLAY_DELAY_V6);
1174         }
1175         /* Setup loop filter, for decoding this is only valid for MPEG4 */
1176         if (ctx->codec_mode == S5P_MFC_CODEC_MPEG4_DEC) {
1177                 mfc_debug(2, "Set loop filter to: %d\n",
1178                                 ctx->loop_filter_mpeg4);
1179                 reg |= (ctx->loop_filter_mpeg4 <<
1180                                 S5P_FIMV_D_OPT_LF_CTRL_SHIFT_V6);
1181         }
1182         if (ctx->dst_fmt->fourcc == V4L2_PIX_FMT_NV12MT_16X16)
1183                 reg |= (0x1 << S5P_FIMV_D_OPT_TILE_MODE_SHIFT_V6);
1184
1185         WRITEL(reg, S5P_FIMV_D_DEC_OPTIONS_V6);
1186
1187         /* 0: NV12(CbCr), 1: NV21(CrCb) */
1188         if (ctx->dst_fmt->fourcc == V4L2_PIX_FMT_NV21M)
1189                 WRITEL(0x1, S5P_FIMV_PIXEL_FORMAT_V6);
1190         else
1191                 WRITEL(0x0, S5P_FIMV_PIXEL_FORMAT_V6);
1192
1193         /* sei parse */
1194         WRITEL(ctx->sei_fp_parse & 0x1, S5P_FIMV_D_SEI_ENABLE_V6);
1195
1196         WRITEL(ctx->inst_no, S5P_FIMV_INSTANCE_ID_V6);
1197         s5p_mfc_hw_call(dev->mfc_cmds, cmd_host2risc, dev,
1198                         S5P_FIMV_CH_SEQ_HEADER_V6, NULL);
1199
1200         mfc_debug_leave();
1201         return 0;
1202 }
1203
1204 static inline void s5p_mfc_set_flush(struct s5p_mfc_ctx *ctx, int flush)
1205 {
1206         struct s5p_mfc_dev *dev = ctx->dev;
1207
1208         if (flush) {
1209                 dev->curr_ctx = ctx->num;
1210                 s5p_mfc_clean_ctx_int_flags(ctx);
1211                 WRITEL(ctx->inst_no, S5P_FIMV_INSTANCE_ID_V6);
1212                 s5p_mfc_hw_call(dev->mfc_cmds, cmd_host2risc, dev,
1213                                 S5P_FIMV_H2R_CMD_FLUSH_V6, NULL);
1214         }
1215 }
1216
1217 /* Decode a single frame */
1218 int s5p_mfc_decode_one_frame_v6(struct s5p_mfc_ctx *ctx,
1219                         enum s5p_mfc_decode_arg last_frame)
1220 {
1221         struct s5p_mfc_dev *dev = ctx->dev;
1222
1223         WRITEL(ctx->dec_dst_flag, S5P_FIMV_D_AVAILABLE_DPB_FLAG_LOWER_V6);
1224         WRITEL(ctx->slice_interface & 0x1, S5P_FIMV_D_SLICE_IF_ENABLE_V6);
1225
1226         WRITEL(ctx->inst_no, S5P_FIMV_INSTANCE_ID_V6);
1227         /* Issue different commands to instance basing on whether it
1228          * is the last frame or not. */
1229         switch (last_frame) {
1230         case 0:
1231                 s5p_mfc_hw_call(dev->mfc_cmds, cmd_host2risc, dev,
1232                                 S5P_FIMV_CH_FRAME_START_V6, NULL);
1233                 break;
1234         case 1:
1235                 s5p_mfc_hw_call(dev->mfc_cmds, cmd_host2risc, dev,
1236                                 S5P_FIMV_CH_LAST_FRAME_V6, NULL);
1237                 break;
1238         default:
1239                 mfc_err("Unsupported last frame arg.\n");
1240                 return -EINVAL;
1241         }
1242
1243         mfc_debug(2, "Decoding a usual frame.\n");
1244         return 0;
1245 }
1246
1247 int s5p_mfc_init_encode_v6(struct s5p_mfc_ctx *ctx)
1248 {
1249         struct s5p_mfc_dev *dev = ctx->dev;
1250
1251         if (ctx->codec_mode == S5P_MFC_CODEC_H264_ENC)
1252                 s5p_mfc_set_enc_params_h264(ctx);
1253         else if (ctx->codec_mode == S5P_MFC_CODEC_MPEG4_ENC)
1254                 s5p_mfc_set_enc_params_mpeg4(ctx);
1255         else if (ctx->codec_mode == S5P_MFC_CODEC_H263_ENC)
1256                 s5p_mfc_set_enc_params_h263(ctx);
1257         else {
1258                 mfc_err("Unknown codec for encoding (%x).\n",
1259                         ctx->codec_mode);
1260                 return -EINVAL;
1261         }
1262
1263         WRITEL(ctx->inst_no, S5P_FIMV_INSTANCE_ID_V6);
1264         s5p_mfc_hw_call(dev->mfc_cmds, cmd_host2risc, dev,
1265                         S5P_FIMV_CH_SEQ_HEADER_V6, NULL);
1266
1267         return 0;
1268 }
1269
1270 int s5p_mfc_h264_set_aso_slice_order_v6(struct s5p_mfc_ctx *ctx)
1271 {
1272         struct s5p_mfc_dev *dev = ctx->dev;
1273         struct s5p_mfc_enc_params *p = &ctx->enc_params;
1274         struct s5p_mfc_h264_enc_params *p_h264 = &p->codec.h264;
1275         int i;
1276
1277         if (p_h264->aso) {
1278                 for (i = 0; i < 8; i++)
1279                         WRITEL(p_h264->aso_slice_order[i],
1280                                 S5P_FIMV_E_H264_ASO_SLICE_ORDER_0_V6 + i * 4);
1281         }
1282         return 0;
1283 }
1284
1285 /* Encode a single frame */
1286 int s5p_mfc_encode_one_frame_v6(struct s5p_mfc_ctx *ctx)
1287 {
1288         struct s5p_mfc_dev *dev = ctx->dev;
1289
1290         mfc_debug(2, "++\n");
1291
1292         /* memory structure cur. frame */
1293
1294         if (ctx->codec_mode == S5P_MFC_CODEC_H264_ENC)
1295                 s5p_mfc_h264_set_aso_slice_order_v6(ctx);
1296
1297         s5p_mfc_set_slice_mode(ctx);
1298
1299         WRITEL(ctx->inst_no, S5P_FIMV_INSTANCE_ID_V6);
1300         s5p_mfc_hw_call(dev->mfc_cmds, cmd_host2risc, dev,
1301                         S5P_FIMV_CH_FRAME_START_V6, NULL);
1302
1303         mfc_debug(2, "--\n");
1304
1305         return 0;
1306 }
1307
1308 static inline int s5p_mfc_get_new_ctx(struct s5p_mfc_dev *dev)
1309 {
1310         unsigned long flags;
1311         int new_ctx;
1312         int cnt;
1313
1314         spin_lock_irqsave(&dev->condlock, flags);
1315         mfc_debug(2, "Previos context: %d (bits %08lx)\n", dev->curr_ctx,
1316                                                         dev->ctx_work_bits);
1317         new_ctx = (dev->curr_ctx + 1) % MFC_NUM_CONTEXTS;
1318         cnt = 0;
1319         while (!test_bit(new_ctx, &dev->ctx_work_bits)) {
1320                 new_ctx = (new_ctx + 1) % MFC_NUM_CONTEXTS;
1321                 cnt++;
1322                 if (cnt > MFC_NUM_CONTEXTS) {
1323                         /* No contexts to run */
1324                         spin_unlock_irqrestore(&dev->condlock, flags);
1325                         return -EAGAIN;
1326                 }
1327         }
1328         spin_unlock_irqrestore(&dev->condlock, flags);
1329         return new_ctx;
1330 }
1331
1332 static inline void s5p_mfc_run_dec_last_frames(struct s5p_mfc_ctx *ctx)
1333 {
1334         struct s5p_mfc_dev *dev = ctx->dev;
1335         struct s5p_mfc_buf *temp_vb;
1336         unsigned long flags;
1337
1338         spin_lock_irqsave(&dev->irqlock, flags);
1339
1340         /* Frames are being decoded */
1341         if (list_empty(&ctx->src_queue)) {
1342                 mfc_debug(2, "No src buffers.\n");
1343                 spin_unlock_irqrestore(&dev->irqlock, flags);
1344                 return;
1345         }
1346         /* Get the next source buffer */
1347         temp_vb = list_entry(ctx->src_queue.next, struct s5p_mfc_buf, list);
1348         temp_vb->flags |= MFC_BUF_FLAG_USED;
1349         s5p_mfc_set_dec_stream_buffer_v6(ctx,
1350                         vb2_dma_contig_plane_dma_addr(temp_vb->b, 0), 0, 0);
1351         spin_unlock_irqrestore(&dev->irqlock, flags);
1352
1353         dev->curr_ctx = ctx->num;
1354         s5p_mfc_clean_ctx_int_flags(ctx);
1355         s5p_mfc_decode_one_frame_v6(ctx, 1);
1356 }
1357
1358 static inline int s5p_mfc_run_dec_frame(struct s5p_mfc_ctx *ctx)
1359 {
1360         struct s5p_mfc_dev *dev = ctx->dev;
1361         struct s5p_mfc_buf *temp_vb;
1362         unsigned long flags;
1363         int last_frame = 0;
1364
1365         spin_lock_irqsave(&dev->irqlock, flags);
1366
1367         /* Frames are being decoded */
1368         if (list_empty(&ctx->src_queue)) {
1369                 mfc_debug(2, "No src buffers.\n");
1370                 spin_unlock_irqrestore(&dev->irqlock, flags);
1371                 return -EAGAIN;
1372         }
1373         /* Get the next source buffer */
1374         temp_vb = list_entry(ctx->src_queue.next, struct s5p_mfc_buf, list);
1375         temp_vb->flags |= MFC_BUF_FLAG_USED;
1376         s5p_mfc_set_dec_stream_buffer_v6(ctx,
1377                 vb2_dma_contig_plane_dma_addr(temp_vb->b, 0),
1378                         ctx->consumed_stream,
1379                         temp_vb->b->v4l2_planes[0].bytesused);
1380         spin_unlock_irqrestore(&dev->irqlock, flags);
1381
1382         dev->curr_ctx = ctx->num;
1383         s5p_mfc_clean_ctx_int_flags(ctx);
1384         if (temp_vb->b->v4l2_planes[0].bytesused == 0) {
1385                 last_frame = 1;
1386                 mfc_debug(2, "Setting ctx->state to FINISHING\n");
1387                 ctx->state = MFCINST_FINISHING;
1388         }
1389         s5p_mfc_decode_one_frame_v6(ctx, last_frame);
1390
1391         return 0;
1392 }
1393
1394 static inline int s5p_mfc_run_enc_frame(struct s5p_mfc_ctx *ctx)
1395 {
1396         struct s5p_mfc_dev *dev = ctx->dev;
1397         unsigned long flags;
1398         struct s5p_mfc_buf *dst_mb;
1399         struct s5p_mfc_buf *src_mb;
1400         unsigned long src_y_addr, src_c_addr, dst_addr;
1401         /*
1402         unsigned int src_y_size, src_c_size;
1403         */
1404         unsigned int dst_size;
1405
1406         spin_lock_irqsave(&dev->irqlock, flags);
1407
1408         if (list_empty(&ctx->src_queue)) {
1409                 mfc_debug(2, "no src buffers.\n");
1410                 spin_unlock_irqrestore(&dev->irqlock, flags);
1411                 return -EAGAIN;
1412         }
1413
1414         if (list_empty(&ctx->dst_queue)) {
1415                 mfc_debug(2, "no dst buffers.\n");
1416                 spin_unlock_irqrestore(&dev->irqlock, flags);
1417                 return -EAGAIN;
1418         }
1419
1420         src_mb = list_entry(ctx->src_queue.next, struct s5p_mfc_buf, list);
1421         src_mb->flags |= MFC_BUF_FLAG_USED;
1422         src_y_addr = vb2_dma_contig_plane_dma_addr(src_mb->b, 0);
1423         src_c_addr = vb2_dma_contig_plane_dma_addr(src_mb->b, 1);
1424
1425         mfc_debug(2, "enc src y addr: 0x%08lx", src_y_addr);
1426         mfc_debug(2, "enc src c addr: 0x%08lx", src_c_addr);
1427
1428         s5p_mfc_set_enc_frame_buffer_v6(ctx, src_y_addr, src_c_addr);
1429
1430         dst_mb = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf, list);
1431         dst_mb->flags |= MFC_BUF_FLAG_USED;
1432         dst_addr = vb2_dma_contig_plane_dma_addr(dst_mb->b, 0);
1433         dst_size = vb2_plane_size(dst_mb->b, 0);
1434
1435         s5p_mfc_set_enc_stream_buffer_v6(ctx, dst_addr, dst_size);
1436
1437         spin_unlock_irqrestore(&dev->irqlock, flags);
1438
1439         dev->curr_ctx = ctx->num;
1440         s5p_mfc_clean_ctx_int_flags(ctx);
1441         s5p_mfc_encode_one_frame_v6(ctx);
1442
1443         return 0;
1444 }
1445
1446 static inline void s5p_mfc_run_init_dec(struct s5p_mfc_ctx *ctx)
1447 {
1448         struct s5p_mfc_dev *dev = ctx->dev;
1449         unsigned long flags;
1450         struct s5p_mfc_buf *temp_vb;
1451
1452         /* Initializing decoding - parsing header */
1453         spin_lock_irqsave(&dev->irqlock, flags);
1454         mfc_debug(2, "Preparing to init decoding.\n");
1455         temp_vb = list_entry(ctx->src_queue.next, struct s5p_mfc_buf, list);
1456         mfc_debug(2, "Header size: %d\n", temp_vb->b->v4l2_planes[0].bytesused);
1457         s5p_mfc_set_dec_stream_buffer_v6(ctx,
1458                 vb2_dma_contig_plane_dma_addr(temp_vb->b, 0), 0,
1459                         temp_vb->b->v4l2_planes[0].bytesused);
1460         spin_unlock_irqrestore(&dev->irqlock, flags);
1461         dev->curr_ctx = ctx->num;
1462         s5p_mfc_clean_ctx_int_flags(ctx);
1463         s5p_mfc_init_decode_v6(ctx);
1464 }
1465
1466 static inline void s5p_mfc_run_init_enc(struct s5p_mfc_ctx *ctx)
1467 {
1468         struct s5p_mfc_dev *dev = ctx->dev;
1469         unsigned long flags;
1470         struct s5p_mfc_buf *dst_mb;
1471         unsigned long dst_addr;
1472         unsigned int dst_size;
1473
1474         spin_lock_irqsave(&dev->irqlock, flags);
1475
1476         dst_mb = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf, list);
1477         dst_addr = vb2_dma_contig_plane_dma_addr(dst_mb->b, 0);
1478         dst_size = vb2_plane_size(dst_mb->b, 0);
1479         s5p_mfc_set_enc_stream_buffer_v6(ctx, dst_addr, dst_size);
1480         spin_unlock_irqrestore(&dev->irqlock, flags);
1481         dev->curr_ctx = ctx->num;
1482         s5p_mfc_clean_ctx_int_flags(ctx);
1483         s5p_mfc_init_encode_v6(ctx);
1484 }
1485
1486 static inline int s5p_mfc_run_init_dec_buffers(struct s5p_mfc_ctx *ctx)
1487 {
1488         struct s5p_mfc_dev *dev = ctx->dev;
1489         int ret;
1490         /* Header was parsed now start processing
1491          * First set the output frame buffers
1492          * s5p_mfc_alloc_dec_buffers(ctx); */
1493
1494         if (ctx->capture_state != QUEUE_BUFS_MMAPED) {
1495                 mfc_err("It seems that not all destionation buffers were\n"
1496                         "mmaped.MFC requires that all destination are mmaped\n"
1497                         "before starting processing.\n");
1498                 return -EAGAIN;
1499         }
1500
1501         dev->curr_ctx = ctx->num;
1502         s5p_mfc_clean_ctx_int_flags(ctx);
1503         ret = s5p_mfc_set_dec_frame_buffer_v6(ctx);
1504         if (ret) {
1505                 mfc_err("Failed to alloc frame mem.\n");
1506                 ctx->state = MFCINST_ERROR;
1507         }
1508         return ret;
1509 }
1510
1511 static inline int s5p_mfc_run_init_enc_buffers(struct s5p_mfc_ctx *ctx)
1512 {
1513         struct s5p_mfc_dev *dev = ctx->dev;
1514         int ret;
1515
1516         ret = s5p_mfc_alloc_codec_buffers_v6(ctx);
1517         if (ret) {
1518                 mfc_err("Failed to allocate encoding buffers.\n");
1519                 return -ENOMEM;
1520         }
1521
1522         /* Header was generated now starting processing
1523          * First set the reference frame buffers
1524          */
1525         if (ctx->capture_state != QUEUE_BUFS_REQUESTED) {
1526                 mfc_err("It seems that destionation buffers were not\n"
1527                         "requested.MFC requires that header should be generated\n"
1528                         "before allocating codec buffer.\n");
1529                 return -EAGAIN;
1530         }
1531
1532         dev->curr_ctx = ctx->num;
1533         s5p_mfc_clean_ctx_int_flags(ctx);
1534         ret = s5p_mfc_set_enc_ref_buffer_v6(ctx);
1535         if (ret) {
1536                 mfc_err("Failed to alloc frame mem.\n");
1537                 ctx->state = MFCINST_ERROR;
1538         }
1539         return ret;
1540 }
1541
1542 /* Try running an operation on hardware */
1543 void s5p_mfc_try_run_v6(struct s5p_mfc_dev *dev)
1544 {
1545         struct s5p_mfc_ctx *ctx;
1546         int new_ctx;
1547         unsigned int ret = 0;
1548
1549         mfc_debug(1, "Try run dev: %p\n", dev);
1550
1551         /* Check whether hardware is not running */
1552         if (test_and_set_bit(0, &dev->hw_lock) != 0) {
1553                 /* This is perfectly ok, the scheduled ctx should wait */
1554                 mfc_debug(1, "Couldn't lock HW.\n");
1555                 return;
1556         }
1557
1558         /* Choose the context to run */
1559         new_ctx = s5p_mfc_get_new_ctx(dev);
1560         if (new_ctx < 0) {
1561                 /* No contexts to run */
1562                 if (test_and_clear_bit(0, &dev->hw_lock) == 0) {
1563                         mfc_err("Failed to unlock hardware.\n");
1564                         return;
1565                 }
1566
1567                 mfc_debug(1, "No ctx is scheduled to be run.\n");
1568                 return;
1569         }
1570
1571         mfc_debug(1, "New context: %d\n", new_ctx);
1572         ctx = dev->ctx[new_ctx];
1573         mfc_debug(1, "Seting new context to %p\n", ctx);
1574         /* Got context to run in ctx */
1575         mfc_debug(1, "ctx->dst_queue_cnt=%d ctx->dpb_count=%d ctx->src_queue_cnt=%d\n",
1576                 ctx->dst_queue_cnt, ctx->dpb_count, ctx->src_queue_cnt);
1577         mfc_debug(1, "ctx->state=%d\n", ctx->state);
1578         /* Last frame has already been sent to MFC
1579          * Now obtaining frames from MFC buffer */
1580
1581         s5p_mfc_clock_on();
1582         if (ctx->type == MFCINST_DECODER) {
1583                 switch (ctx->state) {
1584                 case MFCINST_FINISHING:
1585                         s5p_mfc_run_dec_last_frames(ctx);
1586                         break;
1587                 case MFCINST_RUNNING:
1588                         ret = s5p_mfc_run_dec_frame(ctx);
1589                         break;
1590                 case MFCINST_INIT:
1591                         s5p_mfc_clean_ctx_int_flags(ctx);
1592                         ret = s5p_mfc_hw_call(dev->mfc_cmds, open_inst_cmd,
1593                                         ctx);
1594                         break;
1595                 case MFCINST_RETURN_INST:
1596                         s5p_mfc_clean_ctx_int_flags(ctx);
1597                         ret = s5p_mfc_hw_call(dev->mfc_cmds, close_inst_cmd,
1598                                         ctx);
1599                         break;
1600                 case MFCINST_GOT_INST:
1601                         s5p_mfc_run_init_dec(ctx);
1602                         break;
1603                 case MFCINST_HEAD_PARSED:
1604                         ret = s5p_mfc_run_init_dec_buffers(ctx);
1605                         break;
1606                 case MFCINST_FLUSH:
1607                         s5p_mfc_set_flush(ctx, ctx->dpb_flush_flag);
1608                         break;
1609                 case MFCINST_RES_CHANGE_INIT:
1610                         s5p_mfc_run_dec_last_frames(ctx);
1611                         break;
1612                 case MFCINST_RES_CHANGE_FLUSH:
1613                         s5p_mfc_run_dec_last_frames(ctx);
1614                         break;
1615                 case MFCINST_RES_CHANGE_END:
1616                         mfc_debug(2, "Finished remaining frames after resolution change.\n");
1617                         ctx->capture_state = QUEUE_FREE;
1618                         mfc_debug(2, "Will re-init the codec`.\n");
1619                         s5p_mfc_run_init_dec(ctx);
1620                         break;
1621                 default:
1622                         ret = -EAGAIN;
1623                 }
1624         } else if (ctx->type == MFCINST_ENCODER) {
1625                 switch (ctx->state) {
1626                 case MFCINST_FINISHING:
1627                 case MFCINST_RUNNING:
1628                         ret = s5p_mfc_run_enc_frame(ctx);
1629                         break;
1630                 case MFCINST_INIT:
1631                         ret = s5p_mfc_hw_call(dev->mfc_cmds, open_inst_cmd,
1632                                         ctx);
1633                         break;
1634                 case MFCINST_RETURN_INST:
1635                         ret = s5p_mfc_hw_call(dev->mfc_cmds, close_inst_cmd,
1636                                         ctx);
1637                         break;
1638                 case MFCINST_GOT_INST:
1639                         s5p_mfc_run_init_enc(ctx);
1640                         break;
1641                 case MFCINST_HEAD_PARSED: /* Only for MFC6.x */
1642                         ret = s5p_mfc_run_init_enc_buffers(ctx);
1643                         break;
1644                 default:
1645                         ret = -EAGAIN;
1646                 }
1647         } else {
1648                 mfc_err("invalid context type: %d\n", ctx->type);
1649                 ret = -EAGAIN;
1650         }
1651
1652         if (ret) {
1653                 /* Free hardware lock */
1654                 if (test_and_clear_bit(0, &dev->hw_lock) == 0)
1655                         mfc_err("Failed to unlock hardware.\n");
1656
1657                 /* This is in deed imporant, as no operation has been
1658                  * scheduled, reduce the clock count as no one will
1659                  * ever do this, because no interrupt related to this try_run
1660                  * will ever come from hardware. */
1661                 s5p_mfc_clock_off();
1662         }
1663 }
1664
1665
1666 void s5p_mfc_cleanup_queue_v6(struct list_head *lh, struct vb2_queue *vq)
1667 {
1668         struct s5p_mfc_buf *b;
1669         int i;
1670
1671         while (!list_empty(lh)) {
1672                 b = list_entry(lh->next, struct s5p_mfc_buf, list);
1673                 for (i = 0; i < b->b->num_planes; i++)
1674                         vb2_set_plane_payload(b->b, i, 0);
1675                 vb2_buffer_done(b->b, VB2_BUF_STATE_ERROR);
1676                 list_del(&b->list);
1677         }
1678 }
1679
1680 void s5p_mfc_clear_int_flags_v6(struct s5p_mfc_dev *dev)
1681 {
1682         mfc_write(dev, 0, S5P_FIMV_RISC2HOST_CMD_V6);
1683         mfc_write(dev, 0, S5P_FIMV_RISC2HOST_INT_V6);
1684 }
1685
1686 void s5p_mfc_write_info_v6(struct s5p_mfc_ctx *ctx, unsigned int data,
1687                 unsigned int ofs)
1688 {
1689         struct s5p_mfc_dev *dev = ctx->dev;
1690
1691         s5p_mfc_clock_on();
1692         WRITEL(data, ofs);
1693         s5p_mfc_clock_off();
1694 }
1695
1696 unsigned int s5p_mfc_read_info_v6(struct s5p_mfc_ctx *ctx, unsigned int ofs)
1697 {
1698         struct s5p_mfc_dev *dev = ctx->dev;
1699         int ret;
1700
1701         s5p_mfc_clock_on();
1702         ret = READL(ofs);
1703         s5p_mfc_clock_off();
1704
1705         return ret;
1706 }
1707
1708 int s5p_mfc_get_dspl_y_adr_v6(struct s5p_mfc_dev *dev)
1709 {
1710         return mfc_read(dev, S5P_FIMV_D_DISPLAY_LUMA_ADDR_V6);
1711 }
1712
1713 int s5p_mfc_get_dec_y_adr_v6(struct s5p_mfc_dev *dev)
1714 {
1715         return mfc_read(dev, S5P_FIMV_D_DECODED_LUMA_ADDR_V6);
1716 }
1717
1718 int s5p_mfc_get_dspl_status_v6(struct s5p_mfc_dev *dev)
1719 {
1720         return mfc_read(dev, S5P_FIMV_D_DISPLAY_STATUS_V6);
1721 }
1722
1723 int s5p_mfc_get_decoded_status_v6(struct s5p_mfc_dev *dev)
1724 {
1725         return mfc_read(dev, S5P_FIMV_D_DECODED_STATUS_V6);
1726 }
1727
1728 int s5p_mfc_get_dec_frame_type_v6(struct s5p_mfc_dev *dev)
1729 {
1730         return mfc_read(dev, S5P_FIMV_D_DECODED_FRAME_TYPE_V6) &
1731                 S5P_FIMV_DECODE_FRAME_MASK_V6;
1732 }
1733
1734 int s5p_mfc_get_disp_frame_type_v6(struct s5p_mfc_ctx *ctx)
1735 {
1736         return mfc_read(ctx->dev, S5P_FIMV_D_DISPLAY_FRAME_TYPE_V6) &
1737                 S5P_FIMV_DECODE_FRAME_MASK_V6;
1738 }
1739
1740 int s5p_mfc_get_consumed_stream_v6(struct s5p_mfc_dev *dev)
1741 {
1742         return mfc_read(dev, S5P_FIMV_D_DECODED_NAL_SIZE_V6);
1743 }
1744
1745 int s5p_mfc_get_int_reason_v6(struct s5p_mfc_dev *dev)
1746 {
1747         return mfc_read(dev, S5P_FIMV_RISC2HOST_CMD_V6) &
1748                 S5P_FIMV_RISC2HOST_CMD_MASK;
1749 }
1750
1751 int s5p_mfc_get_int_err_v6(struct s5p_mfc_dev *dev)
1752 {
1753         return mfc_read(dev, S5P_FIMV_ERROR_CODE_V6);
1754 }
1755
1756 int s5p_mfc_err_dec_v6(unsigned int err)
1757 {
1758         return (err & S5P_FIMV_ERR_DEC_MASK_V6) >> S5P_FIMV_ERR_DEC_SHIFT_V6;
1759 }
1760
1761 int s5p_mfc_err_dspl_v6(unsigned int err)
1762 {
1763         return (err & S5P_FIMV_ERR_DSPL_MASK_V6) >> S5P_FIMV_ERR_DSPL_SHIFT_V6;
1764 }
1765
1766 int s5p_mfc_get_img_width_v6(struct s5p_mfc_dev *dev)
1767 {
1768         return mfc_read(dev, S5P_FIMV_D_DISPLAY_FRAME_WIDTH_V6);
1769 }
1770
1771 int s5p_mfc_get_img_height_v6(struct s5p_mfc_dev *dev)
1772 {
1773         return mfc_read(dev, S5P_FIMV_D_DISPLAY_FRAME_HEIGHT_V6);
1774 }
1775
1776 int s5p_mfc_get_dpb_count_v6(struct s5p_mfc_dev *dev)
1777 {
1778         return mfc_read(dev, S5P_FIMV_D_MIN_NUM_DPB_V6);
1779 }
1780
1781 int s5p_mfc_get_mv_count_v6(struct s5p_mfc_dev *dev)
1782 {
1783         return mfc_read(dev, S5P_FIMV_D_MIN_NUM_MV_V6);
1784 }
1785
1786 int s5p_mfc_get_inst_no_v6(struct s5p_mfc_dev *dev)
1787 {
1788         return mfc_read(dev, S5P_FIMV_RET_INSTANCE_ID_V6);
1789 }
1790
1791 int s5p_mfc_get_enc_dpb_count_v6(struct s5p_mfc_dev *dev)
1792 {
1793         return mfc_read(dev, S5P_FIMV_E_NUM_DPB_V6);
1794 }
1795
1796 int s5p_mfc_get_enc_strm_size_v6(struct s5p_mfc_dev *dev)
1797 {
1798         return mfc_read(dev, S5P_FIMV_E_STREAM_SIZE_V6);
1799 }
1800
1801 int s5p_mfc_get_enc_slice_type_v6(struct s5p_mfc_dev *dev)
1802 {
1803         return mfc_read(dev, S5P_FIMV_E_SLICE_TYPE_V6);
1804 }
1805
1806 int s5p_mfc_get_enc_pic_count_v6(struct s5p_mfc_dev *dev)
1807 {
1808         return mfc_read(dev, S5P_FIMV_E_PICTURE_COUNT_V6);
1809 }
1810
1811 int s5p_mfc_get_sei_avail_status_v6(struct s5p_mfc_ctx *ctx)
1812 {
1813         return mfc_read(ctx->dev, S5P_FIMV_D_FRAME_PACK_SEI_AVAIL_V6);
1814 }
1815
1816 int s5p_mfc_get_mvc_num_views_v6(struct s5p_mfc_dev *dev)
1817 {
1818         return mfc_read(dev, S5P_FIMV_D_MVC_NUM_VIEWS_V6);
1819 }
1820
1821 int s5p_mfc_get_mvc_view_id_v6(struct s5p_mfc_dev *dev)
1822 {
1823         return mfc_read(dev, S5P_FIMV_D_MVC_VIEW_ID_V6);
1824 }
1825
1826 unsigned int s5p_mfc_get_pic_type_top_v6(struct s5p_mfc_ctx *ctx)
1827 {
1828         return s5p_mfc_read_info_v6(ctx, PIC_TIME_TOP_V6);
1829 }
1830
1831 unsigned int s5p_mfc_get_pic_type_bot_v6(struct s5p_mfc_ctx *ctx)
1832 {
1833         return s5p_mfc_read_info_v6(ctx, PIC_TIME_BOT_V6);
1834 }
1835
1836 unsigned int s5p_mfc_get_crop_info_h_v6(struct s5p_mfc_ctx *ctx)
1837 {
1838         return s5p_mfc_read_info_v6(ctx, CROP_INFO_H_V6);
1839 }
1840
1841 unsigned int s5p_mfc_get_crop_info_v_v6(struct s5p_mfc_ctx *ctx)
1842 {
1843         return s5p_mfc_read_info_v6(ctx, CROP_INFO_V_V6);
1844 }
1845
1846 /* Initialize opr function pointers for MFC v6 */
1847 static struct s5p_mfc_hw_ops s5p_mfc_ops_v6 = {
1848         .alloc_dec_temp_buffers = s5p_mfc_alloc_dec_temp_buffers_v6,
1849         .release_dec_desc_buffer = s5p_mfc_release_dec_desc_buffer_v6,
1850         .alloc_codec_buffers = s5p_mfc_alloc_codec_buffers_v6,
1851         .release_codec_buffers = s5p_mfc_release_codec_buffers_v6,
1852         .alloc_instance_buffer = s5p_mfc_alloc_instance_buffer_v6,
1853         .release_instance_buffer = s5p_mfc_release_instance_buffer_v6,
1854         .alloc_dev_context_buffer =
1855                 s5p_mfc_alloc_dev_context_buffer_v6,
1856         .release_dev_context_buffer =
1857                 s5p_mfc_release_dev_context_buffer_v6,
1858         .dec_calc_dpb_size = s5p_mfc_dec_calc_dpb_size_v6,
1859         .enc_calc_src_size = s5p_mfc_enc_calc_src_size_v6,
1860         .set_dec_stream_buffer = s5p_mfc_set_dec_stream_buffer_v6,
1861         .set_dec_frame_buffer = s5p_mfc_set_dec_frame_buffer_v6,
1862         .set_enc_stream_buffer = s5p_mfc_set_enc_stream_buffer_v6,
1863         .set_enc_frame_buffer = s5p_mfc_set_enc_frame_buffer_v6,
1864         .get_enc_frame_buffer = s5p_mfc_get_enc_frame_buffer_v6,
1865         .set_enc_ref_buffer = s5p_mfc_set_enc_ref_buffer_v6,
1866         .init_decode = s5p_mfc_init_decode_v6,
1867         .init_encode = s5p_mfc_init_encode_v6,
1868         .encode_one_frame = s5p_mfc_encode_one_frame_v6,
1869         .try_run = s5p_mfc_try_run_v6,
1870         .cleanup_queue = s5p_mfc_cleanup_queue_v6,
1871         .clear_int_flags = s5p_mfc_clear_int_flags_v6,
1872         .write_info = s5p_mfc_write_info_v6,
1873         .read_info = s5p_mfc_read_info_v6,
1874         .get_dspl_y_adr = s5p_mfc_get_dspl_y_adr_v6,
1875         .get_dec_y_adr = s5p_mfc_get_dec_y_adr_v6,
1876         .get_dspl_status = s5p_mfc_get_dspl_status_v6,
1877         .get_dec_status = s5p_mfc_get_dec_status_v6,
1878         .get_dec_frame_type = s5p_mfc_get_dec_frame_type_v6,
1879         .get_disp_frame_type = s5p_mfc_get_disp_frame_type_v6,
1880         .get_consumed_stream = s5p_mfc_get_consumed_stream_v6,
1881         .get_int_reason = s5p_mfc_get_int_reason_v6,
1882         .get_int_err = s5p_mfc_get_int_err_v6,
1883         .err_dec = s5p_mfc_err_dec_v6,
1884         .err_dspl = s5p_mfc_err_dspl_v6,
1885         .get_img_width = s5p_mfc_get_img_width_v6,
1886         .get_img_height = s5p_mfc_get_img_height_v6,
1887         .get_dpb_count = s5p_mfc_get_dpb_count_v6,
1888         .get_mv_count = s5p_mfc_get_mv_count_v6,
1889         .get_inst_no = s5p_mfc_get_inst_no_v6,
1890         .get_enc_strm_size = s5p_mfc_get_enc_strm_size_v6,
1891         .get_enc_slice_type = s5p_mfc_get_enc_slice_type_v6,
1892         .get_enc_dpb_count = s5p_mfc_get_enc_dpb_count_v6,
1893         .get_enc_pic_count = s5p_mfc_get_enc_pic_count_v6,
1894         .get_sei_avail_status = s5p_mfc_get_sei_avail_status_v6,
1895         .get_mvc_num_views = s5p_mfc_get_mvc_num_views_v6,
1896         .get_mvc_view_id = s5p_mfc_get_mvc_view_id_v6,
1897         .get_pic_type_top = s5p_mfc_get_pic_type_top_v6,
1898         .get_pic_type_bot = s5p_mfc_get_pic_type_bot_v6,
1899         .get_crop_info_h = s5p_mfc_get_crop_info_h_v6,
1900         .get_crop_info_v = s5p_mfc_get_crop_info_v_v6,
1901 };
1902
1903 struct s5p_mfc_hw_ops *s5p_mfc_init_hw_ops_v6(void)
1904 {
1905         return &s5p_mfc_ops_v6;
1906 }