s5p_mfc_opr_v6.c 65.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
/*
 * drivers/media/platform/s5p-mfc/s5p_mfc_opr_v6.c
 *
 * Samsung MFC (Multi Function Codec - FIMV) driver
 * This file contains hw related functions.
 *
 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
 *		http://www.samsung.com/
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#undef DEBUG

#include <linux/delay.h>
#include <linux/mm.h>
#include <linux/io.h>
#include <linux/jiffies.h>
#include <linux/firmware.h>
#include <linux/err.h>
#include <linux/sched.h>
#include <linux/dma-mapping.h>

#include <asm/cacheflush.h>

#include "s5p_mfc_common.h"
#include "s5p_mfc_cmd.h"
#include "s5p_mfc_intr.h"
#include "s5p_mfc_pm.h"
#include "s5p_mfc_debug.h"
#include "s5p_mfc_opr.h"
#include "s5p_mfc_opr_v6.h"

/* #define S5P_MFC_DEBUG_REGWRITE  */
#ifdef S5P_MFC_DEBUG_REGWRITE
#undef writel
#define writel(v, r)							\
	do {								\
		pr_err("MFCWRITE(%p): %08x\n", r, (unsigned int)v);	\
	__raw_writel(v, r);						\
	} while (0)
#endif /* S5P_MFC_DEBUG_REGWRITE */

46 47
#define IS_MFCV6_V2(dev) (!IS_MFCV7_PLUS(dev) && dev->fw_ver == MFC_FW_V2)

48
/* Allocate temporary buffers for decoding */
49
static int s5p_mfc_alloc_dec_temp_buffers_v6(struct s5p_mfc_ctx *ctx)
50 51 52 53 54 55 56
{
	/* NOP */

	return 0;
}

/* Release temproary buffers for decoding */
57
static void s5p_mfc_release_dec_desc_buffer_v6(struct s5p_mfc_ctx *ctx)
58 59 60 61 62
{
	/* NOP */
}

/* Allocate codec buffers */
63
static int s5p_mfc_alloc_codec_buffers_v6(struct s5p_mfc_ctx *ctx)
64 65 66
{
	struct s5p_mfc_dev *dev = ctx->dev;
	unsigned int mb_width, mb_height;
67
	int ret;
68 69 70 71 72 73 74 75 76

	mb_width = MB_WIDTH(ctx->img_width);
	mb_height = MB_HEIGHT(ctx->img_height);

	if (ctx->type == MFCINST_DECODER) {
		mfc_debug(2, "Luma size:%d Chroma size:%d MV size:%d\n",
			  ctx->luma_size, ctx->chroma_size, ctx->mv_size);
		mfc_debug(2, "Totals bufs: %d\n", ctx->total_dpb_count);
	} else if (ctx->type == MFCINST_ENCODER) {
77 78 79 80 81 82
		if (IS_MFCV8(dev))
			ctx->tmv_buffer_size = S5P_FIMV_NUM_TMV_BUFFERS_V6 *
			ALIGN(S5P_FIMV_TMV_BUFFER_SIZE_V8(mb_width, mb_height),
			S5P_FIMV_TMV_BUFFER_ALIGN_V6);
		else
			ctx->tmv_buffer_size = S5P_FIMV_NUM_TMV_BUFFERS_V6 *
83 84
			ALIGN(S5P_FIMV_TMV_BUFFER_SIZE_V6(mb_width, mb_height),
			S5P_FIMV_TMV_BUFFER_ALIGN_V6);
85

86 87 88 89 90 91
		ctx->luma_dpb_size = ALIGN((mb_width * mb_height) *
				S5P_FIMV_LUMA_MB_TO_PIXEL_V6,
				S5P_FIMV_LUMA_DPB_BUFFER_ALIGN_V6);
		ctx->chroma_dpb_size = ALIGN((mb_width * mb_height) *
				S5P_FIMV_CHROMA_MB_TO_PIXEL_V6,
				S5P_FIMV_CHROMA_DPB_BUFFER_ALIGN_V6);
92 93 94 95 96 97 98 99 100 101
		if (IS_MFCV8(dev))
			ctx->me_buffer_size = ALIGN(S5P_FIMV_ME_BUFFER_SIZE_V8(
						ctx->img_width, ctx->img_height,
						mb_width, mb_height),
						S5P_FIMV_ME_BUFFER_ALIGN_V6);
		else
			ctx->me_buffer_size = ALIGN(S5P_FIMV_ME_BUFFER_SIZE_V6(
						ctx->img_width, ctx->img_height,
						mb_width, mb_height),
						S5P_FIMV_ME_BUFFER_ALIGN_V6);
102

103
		mfc_debug(2, "recon luma size: %zu chroma size: %zu\n",
104 105 106 107 108 109 110 111 112
			  ctx->luma_dpb_size, ctx->chroma_dpb_size);
	} else {
		return -EINVAL;
	}

	/* Codecs have different memory requirements */
	switch (ctx->codec_mode) {
	case S5P_MFC_CODEC_H264_DEC:
	case S5P_MFC_CODEC_H264_MVC_DEC:
113 114 115 116 117 118 119 120
		if (IS_MFCV8(dev))
			ctx->scratch_buf_size =
				S5P_FIMV_SCRATCH_BUF_SIZE_H264_DEC_V8(
					mb_width,
					mb_height);
		else
			ctx->scratch_buf_size =
				S5P_FIMV_SCRATCH_BUF_SIZE_H264_DEC_V6(
121 122 123 124
					mb_width,
					mb_height);
		ctx->scratch_buf_size = ALIGN(ctx->scratch_buf_size,
				S5P_FIMV_SCRATCH_BUFFER_ALIGN_V6);
125
		ctx->bank1.size =
126 127 128 129
			ctx->scratch_buf_size +
			(ctx->mv_count * ctx->mv_size);
		break;
	case S5P_MFC_CODEC_MPEG4_DEC:
130
		if (IS_MFCV7_PLUS(dev)) {
131 132 133 134 135 136 137 138 139 140 141
			ctx->scratch_buf_size =
				S5P_FIMV_SCRATCH_BUF_SIZE_MPEG4_DEC_V7(
						mb_width,
						mb_height);
		} else {
			ctx->scratch_buf_size =
				S5P_FIMV_SCRATCH_BUF_SIZE_MPEG4_DEC_V6(
						mb_width,
						mb_height);
		}

142 143
		ctx->scratch_buf_size = ALIGN(ctx->scratch_buf_size,
				S5P_FIMV_SCRATCH_BUFFER_ALIGN_V6);
144
		ctx->bank1.size = ctx->scratch_buf_size;
145 146 147 148 149 150 151 152 153
		break;
	case S5P_MFC_CODEC_VC1RCV_DEC:
	case S5P_MFC_CODEC_VC1_DEC:
		ctx->scratch_buf_size =
			S5P_FIMV_SCRATCH_BUF_SIZE_VC1_DEC_V6(
					mb_width,
					mb_height);
		ctx->scratch_buf_size = ALIGN(ctx->scratch_buf_size,
				S5P_FIMV_SCRATCH_BUFFER_ALIGN_V6);
154
		ctx->bank1.size = ctx->scratch_buf_size;
155 156
		break;
	case S5P_MFC_CODEC_MPEG2_DEC:
157 158
		ctx->bank1.size = 0;
		ctx->bank2.size = 0;
159 160 161 162 163 164 165 166
		break;
	case S5P_MFC_CODEC_H263_DEC:
		ctx->scratch_buf_size =
			S5P_FIMV_SCRATCH_BUF_SIZE_H263_DEC_V6(
					mb_width,
					mb_height);
		ctx->scratch_buf_size = ALIGN(ctx->scratch_buf_size,
				S5P_FIMV_SCRATCH_BUFFER_ALIGN_V6);
167
		ctx->bank1.size = ctx->scratch_buf_size;
168 169
		break;
	case S5P_MFC_CODEC_VP8_DEC:
170 171 172 173 174 175 176 177 178 179
		if (IS_MFCV8(dev))
			ctx->scratch_buf_size =
				S5P_FIMV_SCRATCH_BUF_SIZE_VP8_DEC_V8(
						mb_width,
						mb_height);
		else
			ctx->scratch_buf_size =
				S5P_FIMV_SCRATCH_BUF_SIZE_VP8_DEC_V6(
						mb_width,
						mb_height);
180 181
		ctx->scratch_buf_size = ALIGN(ctx->scratch_buf_size,
				S5P_FIMV_SCRATCH_BUFFER_ALIGN_V6);
182
		ctx->bank1.size = ctx->scratch_buf_size;
183 184
		break;
	case S5P_MFC_CODEC_H264_ENC:
185 186 187
		if (IS_MFCV8(dev))
			ctx->scratch_buf_size =
				S5P_FIMV_SCRATCH_BUF_SIZE_H264_ENC_V8(
188 189
					mb_width,
					mb_height);
190 191 192 193 194
		else
			ctx->scratch_buf_size =
				S5P_FIMV_SCRATCH_BUF_SIZE_H264_ENC_V6(
						mb_width,
						mb_height);
195 196
		ctx->scratch_buf_size = ALIGN(ctx->scratch_buf_size,
				S5P_FIMV_SCRATCH_BUFFER_ALIGN_V6);
197
		ctx->bank1.size =
198
			ctx->scratch_buf_size + ctx->tmv_buffer_size +
199
			(ctx->pb_count * (ctx->luma_dpb_size +
200
			ctx->chroma_dpb_size + ctx->me_buffer_size));
201
		ctx->bank2.size = 0;
202 203 204 205 206 207 208 209 210
		break;
	case S5P_MFC_CODEC_MPEG4_ENC:
	case S5P_MFC_CODEC_H263_ENC:
		ctx->scratch_buf_size =
			S5P_FIMV_SCRATCH_BUF_SIZE_MPEG4_ENC_V6(
					mb_width,
					mb_height);
		ctx->scratch_buf_size = ALIGN(ctx->scratch_buf_size,
				S5P_FIMV_SCRATCH_BUFFER_ALIGN_V6);
211
		ctx->bank1.size =
212
			ctx->scratch_buf_size + ctx->tmv_buffer_size +
213
			(ctx->pb_count * (ctx->luma_dpb_size +
214
			ctx->chroma_dpb_size + ctx->me_buffer_size));
215
		ctx->bank2.size = 0;
216
		break;
217
	case S5P_MFC_CODEC_VP8_ENC:
218 219 220
		if (IS_MFCV8(dev))
			ctx->scratch_buf_size =
				S5P_FIMV_SCRATCH_BUF_SIZE_VP8_ENC_V8(
221 222
					mb_width,
					mb_height);
223 224 225 226 227
		else
			ctx->scratch_buf_size =
				S5P_FIMV_SCRATCH_BUF_SIZE_VP8_ENC_V7(
						mb_width,
						mb_height);
228 229 230 231 232 233 234 235
		ctx->scratch_buf_size = ALIGN(ctx->scratch_buf_size,
				S5P_FIMV_SCRATCH_BUFFER_ALIGN_V6);
		ctx->bank1.size =
			ctx->scratch_buf_size + ctx->tmv_buffer_size +
			(ctx->pb_count * (ctx->luma_dpb_size +
			ctx->chroma_dpb_size + ctx->me_buffer_size));
		ctx->bank2.size = 0;
		break;
236 237 238 239 240
	default:
		break;
	}

	/* Allocate only if memory from bank 1 is necessary */
241
	if (ctx->bank1.size > 0) {
242
		ret = s5p_mfc_alloc_priv_buf(dev, BANK_L_CTX, &ctx->bank1);
243 244 245
		if (ret) {
			mfc_err("Failed to allocate Bank1 memory\n");
			return ret;
246
		}
247
		BUG_ON(ctx->bank1.dma & ((1 << MFC_BANK1_ALIGN_ORDER) - 1));
248 249 250 251 252
	}
	return 0;
}

/* Release buffers allocated for codec */
253
static void s5p_mfc_release_codec_buffers_v6(struct s5p_mfc_ctx *ctx)
254
{
255
	s5p_mfc_release_priv_buf(ctx->dev, &ctx->bank1);
256 257 258
}

/* Allocate memory for instance data buffer */
259
static int s5p_mfc_alloc_instance_buffer_v6(struct s5p_mfc_ctx *ctx)
260 261 262
{
	struct s5p_mfc_dev *dev = ctx->dev;
	struct s5p_mfc_buf_size_v6 *buf_size = dev->variant->buf_size->priv;
263
	int ret;
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284

	mfc_debug_enter();

	switch (ctx->codec_mode) {
	case S5P_MFC_CODEC_H264_DEC:
	case S5P_MFC_CODEC_H264_MVC_DEC:
		ctx->ctx.size = buf_size->h264_dec_ctx;
		break;
	case S5P_MFC_CODEC_MPEG4_DEC:
	case S5P_MFC_CODEC_H263_DEC:
	case S5P_MFC_CODEC_VC1RCV_DEC:
	case S5P_MFC_CODEC_VC1_DEC:
	case S5P_MFC_CODEC_MPEG2_DEC:
	case S5P_MFC_CODEC_VP8_DEC:
		ctx->ctx.size = buf_size->other_dec_ctx;
		break;
	case S5P_MFC_CODEC_H264_ENC:
		ctx->ctx.size = buf_size->h264_enc_ctx;
		break;
	case S5P_MFC_CODEC_MPEG4_ENC:
	case S5P_MFC_CODEC_H263_ENC:
285
	case S5P_MFC_CODEC_VP8_ENC:
286 287 288 289 290 291 292 293
		ctx->ctx.size = buf_size->other_enc_ctx;
		break;
	default:
		ctx->ctx.size = 0;
		mfc_err("Codec type(%d) should be checked!\n", ctx->codec_mode);
		break;
	}

294
	ret = s5p_mfc_alloc_priv_buf(dev, BANK_L_CTX, &ctx->ctx);
295 296 297
	if (ret) {
		mfc_err("Failed to allocate instance buffer\n");
		return ret;
298 299 300 301 302 303 304 305 306 307 308
	}

	memset(ctx->ctx.virt, 0, ctx->ctx.size);
	wmb();

	mfc_debug_leave();

	return 0;
}

/* Release instance buffer */
309
static void s5p_mfc_release_instance_buffer_v6(struct s5p_mfc_ctx *ctx)
310
{
311
	s5p_mfc_release_priv_buf(ctx->dev, &ctx->ctx);
312 313 314
}

/* Allocate context buffers for SYS_INIT */
315
static int s5p_mfc_alloc_dev_context_buffer_v6(struct s5p_mfc_dev *dev)
316 317
{
	struct s5p_mfc_buf_size_v6 *buf_size = dev->variant->buf_size->priv;
318
	int ret;
319 320 321

	mfc_debug_enter();

322
	dev->ctx_buf.size = buf_size->dev_ctx;
323
	ret = s5p_mfc_alloc_priv_buf(dev, BANK_L_CTX, &dev->ctx_buf);
324 325 326
	if (ret) {
		mfc_err("Failed to allocate device context buffer\n");
		return ret;
327 328 329 330 331 332 333 334 335 336 337
	}

	memset(dev->ctx_buf.virt, 0, buf_size->dev_ctx);
	wmb();

	mfc_debug_leave();

	return 0;
}

/* Release context buffers for SYS_INIT */
338
static void s5p_mfc_release_dev_context_buffer_v6(struct s5p_mfc_dev *dev)
339
{
340
	s5p_mfc_release_priv_buf(dev, &dev->ctx_buf);
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
}

static int calc_plane(int width, int height)
{
	int mbX, mbY;

	mbX = DIV_ROUND_UP(width, S5P_FIMV_NUM_PIXELS_IN_MB_ROW_V6);
	mbY = DIV_ROUND_UP(height, S5P_FIMV_NUM_PIXELS_IN_MB_COL_V6);

	if (width * height < S5P_FIMV_MAX_FRAME_SIZE_V6)
		mbY = (mbY + 1) / 2 * 2;

	return (mbX * S5P_FIMV_NUM_PIXELS_IN_MB_COL_V6) *
		(mbY * S5P_FIMV_NUM_PIXELS_IN_MB_ROW_V6);
}

357
static void s5p_mfc_dec_calc_dpb_size_v6(struct s5p_mfc_ctx *ctx)
358 359 360 361 362 363 364 365 366
{
	ctx->buf_width = ALIGN(ctx->img_width, S5P_FIMV_NV12MT_HALIGN_V6);
	ctx->buf_height = ALIGN(ctx->img_height, S5P_FIMV_NV12MT_VALIGN_V6);
	mfc_debug(2, "SEQ Done: Movie dimensions %dx%d,\n"
			"buffer dimensions: %dx%d\n", ctx->img_width,
			ctx->img_height, ctx->buf_width, ctx->buf_height);

	ctx->luma_size = calc_plane(ctx->img_width, ctx->img_height);
	ctx->chroma_size = calc_plane(ctx->img_width, (ctx->img_height >> 1));
367 368 369 370 371 372
	if (IS_MFCV8(ctx->dev)) {
		/* MFCv8 needs additional 64 bytes for luma,chroma dpb*/
		ctx->luma_size += S5P_FIMV_D_ALIGN_PLANE_SIZE_V8;
		ctx->chroma_size += S5P_FIMV_D_ALIGN_PLANE_SIZE_V8;
	}

373 374 375 376 377 378 379 380 381 382
	if (ctx->codec_mode == S5P_MFC_CODEC_H264_DEC ||
			ctx->codec_mode == S5P_MFC_CODEC_H264_MVC_DEC) {
		ctx->mv_size = S5P_MFC_DEC_MV_SIZE_V6(ctx->img_width,
				ctx->img_height);
		ctx->mv_size = ALIGN(ctx->mv_size, 16);
	} else {
		ctx->mv_size = 0;
	}
}

383
static void s5p_mfc_enc_calc_src_size_v6(struct s5p_mfc_ctx *ctx)
384 385 386 387 388 389 390 391 392
{
	unsigned int mb_width, mb_height;

	mb_width = MB_WIDTH(ctx->img_width);
	mb_height = MB_HEIGHT(ctx->img_height);

	ctx->buf_width = ALIGN(ctx->img_width, S5P_FIMV_NV12M_HALIGN_V6);
	ctx->luma_size = ALIGN((mb_width * mb_height) * 256, 256);
	ctx->chroma_size = ALIGN((mb_width * mb_height) * 128, 256);
393 394

	/* MFCv7 needs pad bytes for Luma and Chroma */
395
	if (IS_MFCV7_PLUS(ctx->dev)) {
396 397 398
		ctx->luma_size += MFC_LUMA_PAD_BYTES_V7;
		ctx->chroma_size += MFC_CHROMA_PAD_BYTES_V7;
	}
399 400 401
}

/* Set registers for decoding stream buffer */
402 403 404
static int s5p_mfc_set_dec_stream_buffer_v6(struct s5p_mfc_ctx *ctx,
			int buf_addr, unsigned int start_num_byte,
			unsigned int strm_size)
405 406
{
	struct s5p_mfc_dev *dev = ctx->dev;
407
	const struct s5p_mfc_regs *mfc_regs = dev->mfc_regs;
408 409 410 411 412 413
	struct s5p_mfc_buf_size *buf_size = dev->variant->buf_size;

	mfc_debug_enter();
	mfc_debug(2, "inst_no: %d, buf_addr: 0x%08x,\n"
		"buf_size: 0x%08x (%d)\n",
		ctx->inst_no, buf_addr, strm_size, strm_size);
414 415 416 417
	writel(strm_size, mfc_regs->d_stream_data_size);
	writel(buf_addr, mfc_regs->d_cpb_buffer_addr);
	writel(buf_size->cpb, mfc_regs->d_cpb_buffer_size);
	writel(start_num_byte, mfc_regs->d_cpb_buffer_offset);
418 419 420 421 422 423

	mfc_debug_leave();
	return 0;
}

/* Set decoding frame buffer */
424
static int s5p_mfc_set_dec_frame_buffer_v6(struct s5p_mfc_ctx *ctx)
425 426 427 428
{
	unsigned int frame_size, i;
	unsigned int frame_size_ch, frame_size_mv;
	struct s5p_mfc_dev *dev = ctx->dev;
429
	const struct s5p_mfc_regs *mfc_regs = dev->mfc_regs;
430 431 432 433
	size_t buf_addr1;
	int buf_size1;
	int align_gap;

434 435
	buf_addr1 = ctx->bank1.dma;
	buf_size1 = ctx->bank1.size;
436 437 438 439 440

	mfc_debug(2, "Buf1: %p (%d)\n", (void *)buf_addr1, buf_size1);
	mfc_debug(2, "Total DPB COUNT: %d\n", ctx->total_dpb_count);
	mfc_debug(2, "Setting display delay to %d\n", ctx->display_delay);

441 442 443
	writel(ctx->total_dpb_count, mfc_regs->d_num_dpb);
	writel(ctx->luma_size, mfc_regs->d_first_plane_dpb_size);
	writel(ctx->chroma_size, mfc_regs->d_second_plane_dpb_size);
444

445 446
	writel(buf_addr1, mfc_regs->d_scratch_buffer_addr);
	writel(ctx->scratch_buf_size, mfc_regs->d_scratch_buffer_size);
447 448

	if (IS_MFCV8(dev)) {
449
		writel(ctx->img_width,
450
			mfc_regs->d_first_plane_dpb_stride_size);
451
		writel(ctx->img_width,
452 453 454
			mfc_regs->d_second_plane_dpb_stride_size);
	}

455 456 457 458 459
	buf_addr1 += ctx->scratch_buf_size;
	buf_size1 -= ctx->scratch_buf_size;

	if (ctx->codec_mode == S5P_FIMV_CODEC_H264_DEC ||
			ctx->codec_mode == S5P_FIMV_CODEC_H264_MVC_DEC){
460 461
		writel(ctx->mv_size, mfc_regs->d_mv_buffer_size);
		writel(ctx->mv_count, mfc_regs->d_num_mv);
462 463 464 465 466 467 468 469 470 471
	}

	frame_size = ctx->luma_size;
	frame_size_ch = ctx->chroma_size;
	frame_size_mv = ctx->mv_size;
	mfc_debug(2, "Frame size: %d ch: %d mv: %d\n",
			frame_size, frame_size_ch, frame_size_mv);

	for (i = 0; i < ctx->total_dpb_count; i++) {
		/* Bank2 */
472
		mfc_debug(2, "Luma %d: %zx\n", i,
473
					ctx->dst_bufs[i].cookie.raw.luma);
474
		writel(ctx->dst_bufs[i].cookie.raw.luma,
475
				mfc_regs->d_first_plane_dpb + i * 4);
476
		mfc_debug(2, "\tChroma %d: %zx\n", i,
477
					ctx->dst_bufs[i].cookie.raw.chroma);
478
		writel(ctx->dst_bufs[i].cookie.raw.chroma,
479
				mfc_regs->d_second_plane_dpb + i * 4);
480 481 482 483 484 485 486 487 488 489
	}
	if (ctx->codec_mode == S5P_MFC_CODEC_H264_DEC ||
			ctx->codec_mode == S5P_MFC_CODEC_H264_MVC_DEC) {
		for (i = 0; i < ctx->mv_count; i++) {
			/* To test alignment */
			align_gap = buf_addr1;
			buf_addr1 = ALIGN(buf_addr1, 16);
			align_gap = buf_addr1 - align_gap;
			buf_size1 -= align_gap;

490
			mfc_debug(2, "\tBuf1: %zx, size: %d\n",
491
					buf_addr1, buf_size1);
492
			writel(buf_addr1, mfc_regs->d_mv_buffer + i * 4);
493 494 495 496 497
			buf_addr1 += frame_size_mv;
			buf_size1 -= frame_size_mv;
		}
	}

498
	mfc_debug(2, "Buf1: %zx, buf_size1: %d (frames %d)\n",
499 500 501 502 503 504
			buf_addr1, buf_size1, ctx->total_dpb_count);
	if (buf_size1 < 0) {
		mfc_debug(2, "Not enough memory has been allocated.\n");
		return -ENOMEM;
	}

505
	writel(ctx->inst_no, mfc_regs->instance_id);
506
	s5p_mfc_hw_call(dev->mfc_cmds, cmd_host2risc, dev,
507 508 509 510 511 512 513
			S5P_FIMV_CH_INIT_BUFS_V6, NULL);

	mfc_debug(2, "After setting buffers.\n");
	return 0;
}

/* Set registers for encoding stream buffer */
514
static int s5p_mfc_set_enc_stream_buffer_v6(struct s5p_mfc_ctx *ctx,
515 516 517
		unsigned long addr, unsigned int size)
{
	struct s5p_mfc_dev *dev = ctx->dev;
518
	const struct s5p_mfc_regs *mfc_regs = dev->mfc_regs;
519

520 521
	writel(addr, mfc_regs->e_stream_buffer_addr); /* 16B align */
	writel(size, mfc_regs->e_stream_buffer_size);
522

523
	mfc_debug(2, "stream buf addr: 0x%08lx, size: 0x%x\n",
524
		  addr, size);
525 526 527 528

	return 0;
}

529
static void s5p_mfc_set_enc_frame_buffer_v6(struct s5p_mfc_ctx *ctx,
530 531 532
		unsigned long y_addr, unsigned long c_addr)
{
	struct s5p_mfc_dev *dev = ctx->dev;
533
	const struct s5p_mfc_regs *mfc_regs = dev->mfc_regs;
534

535 536
	writel(y_addr, mfc_regs->e_source_first_plane_addr);
	writel(c_addr, mfc_regs->e_source_second_plane_addr);
537

538 539
	mfc_debug(2, "enc src y buf addr: 0x%08lx\n", y_addr);
	mfc_debug(2, "enc src c buf addr: 0x%08lx\n", c_addr);
540 541
}

542
static void s5p_mfc_get_enc_frame_buffer_v6(struct s5p_mfc_ctx *ctx,
543 544 545
		unsigned long *y_addr, unsigned long *c_addr)
{
	struct s5p_mfc_dev *dev = ctx->dev;
546
	const struct s5p_mfc_regs *mfc_regs = dev->mfc_regs;
547 548
	unsigned long enc_recon_y_addr, enc_recon_c_addr;

549 550
	*y_addr = readl(mfc_regs->e_encoded_source_first_plane_addr);
	*c_addr = readl(mfc_regs->e_encoded_source_second_plane_addr);
551

552 553
	enc_recon_y_addr = readl(mfc_regs->e_recon_luma_dpb_addr);
	enc_recon_c_addr = readl(mfc_regs->e_recon_chroma_dpb_addr);
554

555
	mfc_debug(2, "recon y addr: 0x%08lx y_addr: 0x%08lx\n", enc_recon_y_addr, *y_addr);
556
	mfc_debug(2, "recon c addr: 0x%08lx\n", enc_recon_c_addr);
557 558 559
}

/* Set encoding ref & codec buffer */
560
static int s5p_mfc_set_enc_ref_buffer_v6(struct s5p_mfc_ctx *ctx)
561 562
{
	struct s5p_mfc_dev *dev = ctx->dev;
563
	const struct s5p_mfc_regs *mfc_regs = dev->mfc_regs;
564 565
	size_t buf_addr1;
	int i, buf_size1;
566 567 568

	mfc_debug_enter();

569 570
	buf_addr1 = ctx->bank1.dma;
	buf_size1 = ctx->bank1.size;
571 572 573

	mfc_debug(2, "Buf1: %p (%d)\n", (void *)buf_addr1, buf_size1);

574
	for (i = 0; i < ctx->pb_count; i++) {
575
		writel(buf_addr1, mfc_regs->e_luma_dpb + (4 * i));
576
		buf_addr1 += ctx->luma_dpb_size;
577
		writel(buf_addr1, mfc_regs->e_chroma_dpb + (4 * i));
578
		buf_addr1 += ctx->chroma_dpb_size;
579
		writel(buf_addr1, mfc_regs->e_me_buffer + (4 * i));
580 581 582 583 584
		buf_addr1 += ctx->me_buffer_size;
		buf_size1 -= (ctx->luma_dpb_size + ctx->chroma_dpb_size +
			ctx->me_buffer_size);
	}

585 586
	writel(buf_addr1, mfc_regs->e_scratch_buffer_addr);
	writel(ctx->scratch_buf_size, mfc_regs->e_scratch_buffer_size);
587 588 589
	buf_addr1 += ctx->scratch_buf_size;
	buf_size1 -= ctx->scratch_buf_size;

590
	writel(buf_addr1, mfc_regs->e_tmv_buffer0);
591
	buf_addr1 += ctx->tmv_buffer_size >> 1;
592
	writel(buf_addr1, mfc_regs->e_tmv_buffer1);
593 594 595
	buf_addr1 += ctx->tmv_buffer_size >> 1;
	buf_size1 -= ctx->tmv_buffer_size;

596
	mfc_debug(2, "Buf1: %zu, buf_size1: %d (ref frames %d)\n",
597
			buf_addr1, buf_size1, ctx->pb_count);
598 599 600 601 602
	if (buf_size1 < 0) {
		mfc_debug(2, "Not enough memory has been allocated.\n");
		return -ENOMEM;
	}

603
	writel(ctx->inst_no, mfc_regs->instance_id);
604
	s5p_mfc_hw_call(dev->mfc_cmds, cmd_host2risc, dev,
605 606 607 608 609 610 611 612 613 614
			S5P_FIMV_CH_INIT_BUFS_V6, NULL);

	mfc_debug_leave();

	return 0;
}

static int s5p_mfc_set_slice_mode(struct s5p_mfc_ctx *ctx)
{
	struct s5p_mfc_dev *dev = ctx->dev;
615
	const struct s5p_mfc_regs *mfc_regs = dev->mfc_regs;
616 617 618

	/* multi-slice control */
	/* multi-slice MB number or bit size */
619
	writel(ctx->slice_mode, mfc_regs->e_mslice_mode);
620
	if (ctx->slice_mode == V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB) {
621
		writel(ctx->slice_size.mb, mfc_regs->e_mslice_size_mb);
622 623
	} else if (ctx->slice_mode ==
			V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES) {
624
		writel(ctx->slice_size.bits, mfc_regs->e_mslice_size_bits);
625
	} else {
626 627
		writel(0x0, mfc_regs->e_mslice_size_mb);
		writel(0x0, mfc_regs->e_mslice_size_bits);
628 629 630 631 632 633 634 635
	}

	return 0;
}

static int s5p_mfc_set_enc_params(struct s5p_mfc_ctx *ctx)
{
	struct s5p_mfc_dev *dev = ctx->dev;
636
	const struct s5p_mfc_regs *mfc_regs = dev->mfc_regs;
637 638 639 640 641 642
	struct s5p_mfc_enc_params *p = &ctx->enc_params;
	unsigned int reg = 0;

	mfc_debug_enter();

	/* width */
643
	writel(ctx->img_width, mfc_regs->e_frame_width); /* 16 align */
644
	/* height */
645
	writel(ctx->img_height, mfc_regs->e_frame_height); /* 16 align */
646 647

	/* cropped width */
648
	writel(ctx->img_width, mfc_regs->e_cropped_frame_width);
649
	/* cropped height */
650
	writel(ctx->img_height, mfc_regs->e_cropped_frame_height);
651
	/* cropped offset */
652
	writel(0x0, mfc_regs->e_frame_crop_offset);
653 654 655 656

	/* pictype : IDR period */
	reg = 0;
	reg |= p->gop_size & 0xFFFF;
657
	writel(reg, mfc_regs->e_gop_config);
658 659 660 661 662 663 664

	/* multi-slice control */
	/* multi-slice MB number or bit size */
	ctx->slice_mode = p->slice_mode;
	reg = 0;
	if (p->slice_mode == V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB) {
		reg |= (0x1 << 3);
665
		writel(reg, mfc_regs->e_enc_options);
666 667 668
		ctx->slice_size.mb = p->slice_mb;
	} else if (p->slice_mode == V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES) {
		reg |= (0x1 << 3);
669
		writel(reg, mfc_regs->e_enc_options);
670 671 672
		ctx->slice_size.bits = p->slice_bit;
	} else {
		reg &= ~(0x1 << 3);
673
		writel(reg, mfc_regs->e_enc_options);
674 675 676 677 678
	}

	s5p_mfc_set_slice_mode(ctx);

	/* cyclic intra refresh */
679 680
	writel(p->intra_refresh_mb, mfc_regs->e_ir_size);
	reg = readl(mfc_regs->e_enc_options);
681 682 683 684
	if (p->intra_refresh_mb == 0)
		reg &= ~(0x1 << 4);
	else
		reg |= (0x1 << 4);
685
	writel(reg, mfc_regs->e_enc_options);
686 687

	/* 'NON_REFERENCE_STORE_ENABLE' for debugging */
688
	reg = readl(mfc_regs->e_enc_options);
689
	reg &= ~(0x1 << 9);
690
	writel(reg, mfc_regs->e_enc_options);
691 692 693 694

	/* memory structure cur. frame */
	if (ctx->src_fmt->fourcc == V4L2_PIX_FMT_NV12M) {
		/* 0: Linear, 1: 2D tiled*/
695
		reg = readl(mfc_regs->e_enc_options);
696
		reg &= ~(0x1 << 7);
697
		writel(reg, mfc_regs->e_enc_options);
698
		/* 0: NV12(CbCr), 1: NV21(CrCb) */
699
		writel(0x0, mfc_regs->pixel_format);
700 701
	} else if (ctx->src_fmt->fourcc == V4L2_PIX_FMT_NV21M) {
		/* 0: Linear, 1: 2D tiled*/
702
		reg = readl(mfc_regs->e_enc_options);
703
		reg &= ~(0x1 << 7);
704
		writel(reg, mfc_regs->e_enc_options);
705
		/* 0: NV12(CbCr), 1: NV21(CrCb) */
706
		writel(0x1, mfc_regs->pixel_format);
707 708
	} else if (ctx->src_fmt->fourcc == V4L2_PIX_FMT_NV12MT_16X16) {
		/* 0: Linear, 1: 2D tiled*/
709
		reg = readl(mfc_regs->e_enc_options);
710
		reg |= (0x1 << 7);
711
		writel(reg, mfc_regs->e_enc_options);
712
		/* 0: NV12(CbCr), 1: NV21(CrCb) */
713
		writel(0x0, mfc_regs->pixel_format);
714 715 716 717
	}

	/* memory structure recon. frame */
	/* 0: Linear, 1: 2D tiled */
718
	reg = readl(mfc_regs->e_enc_options);
719
	reg |= (0x1 << 8);
720
	writel(reg, mfc_regs->e_enc_options);
721 722

	/* padding control & value */
723
	writel(0x0, mfc_regs->e_padding_ctrl);
724 725 726 727 728 729 730 731 732 733
	if (p->pad) {
		reg = 0;
		/** enable */
		reg |= (1 << 31);
		/** cr value */
		reg |= ((p->pad_cr & 0xFF) << 16);
		/** cb value */
		reg |= ((p->pad_cb & 0xFF) << 8);
		/** y value */
		reg |= p->pad_luma & 0xFF;
734
		writel(reg, mfc_regs->e_padding_ctrl);
735 736 737 738 739 740
	}

	/* rate control config. */
	reg = 0;
	/* frame-level rate control */
	reg |= ((p->rc_frame & 0x1) << 9);
741
	writel(reg, mfc_regs->e_rc_config);
742 743 744

	/* bit rate */
	if (p->rc_frame)
745
		writel(p->rc_bitrate,
746
			mfc_regs->e_rc_bit_rate);
747
	else
748
		writel(1, mfc_regs->e_rc_bit_rate);
749 750 751 752

	/* reaction coefficient */
	if (p->rc_frame) {
		if (p->rc_reaction_coeff < TIGHT_CBR_MAX) /* tight CBR */
753
			writel(1, mfc_regs->e_rc_mode);
754
		else					  /* loose CBR */
755
			writel(2, mfc_regs->e_rc_mode);
756 757 758
	}

	/* seq header ctrl */
759
	reg = readl(mfc_regs->e_enc_options);
760 761 762 763 764 765
	reg &= ~(0x1 << 2);
	reg |= ((p->seq_hdr_mode & 0x1) << 2);

	/* frame skip mode */
	reg &= ~(0x3);
	reg |= (p->frame_skip_mode & 0x3);
766
	writel(reg, mfc_regs->e_enc_options);
767 768

	/* 'DROP_CONTROL_ENABLE', disable */
769
	reg = readl(mfc_regs->e_rc_config);
770
	reg &= ~(0x1 << 10);
771
	writel(reg, mfc_regs->e_rc_config);
772 773

	/* setting for MV range [16, 256] */
774
	reg = (p->mv_h_range & S5P_FIMV_E_MV_RANGE_V6_MASK);
775
	writel(reg, mfc_regs->e_mv_hor_range);
776

777
	reg = (p->mv_v_range & S5P_FIMV_E_MV_RANGE_V6_MASK);
778
	writel(reg, mfc_regs->e_mv_ver_range);
779

780 781 782 783 784
	writel(0x0, mfc_regs->e_frame_insertion);
	writel(0x0, mfc_regs->e_roi_buffer_addr);
	writel(0x0, mfc_regs->e_param_change);
	writel(0x0, mfc_regs->e_rc_roi_ctrl);
	writel(0x0, mfc_regs->e_picture_tag);
785

786 787 788
	writel(0x0, mfc_regs->e_bit_count_enable);
	writel(0x0, mfc_regs->e_max_bit_count);
	writel(0x0, mfc_regs->e_min_bit_count);
789

790 791
	writel(0x0, mfc_regs->e_metadata_buffer_addr);
	writel(0x0, mfc_regs->e_metadata_buffer_size);
792 793 794 795 796 797 798 799 800

	mfc_debug_leave();

	return 0;
}

static int s5p_mfc_set_enc_params_h264(struct s5p_mfc_ctx *ctx)
{
	struct s5p_mfc_dev *dev = ctx->dev;
801
	const struct s5p_mfc_regs *mfc_regs = dev->mfc_regs;
802 803 804 805 806 807 808 809 810 811
	struct s5p_mfc_enc_params *p = &ctx->enc_params;
	struct s5p_mfc_h264_enc_params *p_h264 = &p->codec.h264;
	unsigned int reg = 0;
	int i;

	mfc_debug_enter();

	s5p_mfc_set_enc_params(ctx);

	/* pictype : number of B */
812
	reg = readl(mfc_regs->e_gop_config);
813 814
	reg &= ~(0x3 << 16);
	reg |= ((p->num_b_frame & 0x3) << 16);
815
	writel(reg, mfc_regs->e_gop_config);
816 817 818 819 820 821 822

	/* profile & level */
	reg = 0;
	/** level */
	reg |= ((p_h264->level & 0xFF) << 8);
	/** profile - 0 ~ 3 */
	reg |= p_h264->profile & 0x3F;
823
	writel(reg, mfc_regs->e_picture_profile);
824 825

	/* rate control config. */
826
	reg = readl(mfc_regs->e_rc_config);
827 828 829
	/** macroblock level rate control */
	reg &= ~(0x1 << 8);
	reg |= ((p->rc_mb & 0x1) << 8);
830
	writel(reg, mfc_regs->e_rc_config);
831

832 833 834
	/** frame QP */
	reg &= ~(0x3F);
	reg |= p_h264->rc_frame_qp & 0x3F;
835
	writel(reg, mfc_regs->e_rc_config);
836 837 838 839 840 841 842

	/* max & min value of QP */
	reg = 0;
	/** max QP */
	reg |= ((p_h264->rc_max_qp & 0x3F) << 8);
	/** min QP */
	reg |= p_h264->rc_min_qp & 0x3F;
843
	writel(reg, mfc_regs->e_rc_qp_bound);
844 845

	/* other QPs */
846
	writel(0x0, mfc_regs->e_fixed_picture_qp);
847 848 849 850 851
	if (!p->rc_frame && !p->rc_mb) {
		reg = 0;
		reg |= ((p_h264->rc_b_frame_qp & 0x3F) << 16);
		reg |= ((p_h264->rc_p_frame_qp & 0x3F) << 8);
		reg |= p_h264->rc_frame_qp & 0x3F;
852
		writel(reg, mfc_regs->e_fixed_picture_qp);
853 854 855 856 857 858 859
	}

	/* frame rate */
	if (p->rc_frame && p->rc_framerate_num && p->rc_framerate_denom) {
		reg = 0;
		reg |= ((p->rc_framerate_num & 0xFFFF) << 16);
		reg |= p->rc_framerate_denom & 0xFFFF;
860
		writel(reg, mfc_regs->e_rc_frame_rate);
861 862 863 864 865
	}

	/* vbv buffer size */
	if (p->frame_skip_mode ==
			V4L2_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE_BUF_LIMIT) {
866
		writel(p_h264->cpb_size & 0xFFFF,
867
				mfc_regs->e_vbv_buffer_size);
868 869

		if (p->rc_frame)
870
			writel(p->vbv_delay, mfc_regs->e_vbv_init_delay);
871 872 873 874 875
	}

	/* interlace */
	reg = 0;
	reg |= ((p_h264->interlace & 0x1) << 3);
876
	writel(reg, mfc_regs->e_h264_options);
877 878 879

	/* height */
	if (p_h264->interlace) {
880
		writel(ctx->img_height >> 1,
881
				mfc_regs->e_frame_height); /* 32 align */
882
		/* cropped height */
883
		writel(ctx->img_height >> 1,
884
				mfc_regs->e_cropped_frame_height);
885 886 887
	}

	/* loop filter ctrl */
888
	reg = readl(mfc_regs->e_h264_options);
889 890
	reg &= ~(0x3 << 1);
	reg |= ((p_h264->loop_filter_mode & 0x3) << 1);
891
	writel(reg, mfc_regs->e_h264_options);
892 893 894 895 896 897 898 899 900

	/* loopfilter alpha offset */
	if (p_h264->loop_filter_alpha < 0) {
		reg = 0x10;
		reg |= (0xFF - p_h264->loop_filter_alpha) + 1;
	} else {
		reg = 0x00;
		reg |= (p_h264->loop_filter_alpha & 0xF);
	}
901
	writel(reg, mfc_regs->e_h264_lf_alpha_offset);
902 903 904 905 906 907 908 909 910

	/* loopfilter beta offset */
	if (p_h264->loop_filter_beta < 0) {
		reg = 0x10;
		reg |= (0xFF - p_h264->loop_filter_beta) + 1;
	} else {
		reg = 0x00;
		reg |= (p_h264->loop_filter_beta & 0xF);
	}
911
	writel(reg, mfc_regs->e_h264_lf_beta_offset);
912 913

	/* entropy coding mode */
914
	reg = readl(mfc_regs->e_h264_options);
915 916
	reg &= ~(0x1);
	reg |= p_h264->entropy_mode & 0x1;
917
	writel(reg, mfc_regs->e_h264_options);
918 919

	/* number of ref. picture */
920
	reg = readl(mfc_regs->e_h264_options);
921 922
	reg &= ~(0x1 << 7);
	reg |= (((p_h264->num_ref_pic_4p - 1) & 0x1) << 7);
923
	writel(reg, mfc_regs->e_h264_options);
924 925

	/* 8x8 transform enable */
926
	reg = readl(mfc_regs->e_h264_options);
927 928
	reg &= ~(0x3 << 12);
	reg |= ((p_h264->_8x8_transform & 0x3) << 12);
929
	writel(reg, mfc_regs->e_h264_options);
930 931

	/* macroblock adaptive scaling features */
932
	writel(0x0, mfc_regs->e_mb_rc_config);
933 934 935 936 937 938 939 940 941 942
	if (p->rc_mb) {
		reg = 0;
		/** dark region */
		reg |= ((p_h264->rc_mb_dark & 0x1) << 3);
		/** smooth region */
		reg |= ((p_h264->rc_mb_smooth & 0x1) << 2);
		/** static region */
		reg |= ((p_h264->rc_mb_static & 0x1) << 1);
		/** high activity region */
		reg |= p_h264->rc_mb_activity & 0x1;
943
		writel(reg, mfc_regs->e_mb_rc_config);
944 945 946
	}

	/* aspect ratio VUI */
947
	readl(mfc_regs->e_h264_options);
948 949
	reg &= ~(0x1 << 5);
	reg |= ((p_h264->vui_sar & 0x1) << 5);
950
	writel(reg, mfc_regs->e_h264_options);
951

952 953
	writel(0x0, mfc_regs->e_aspect_ratio);
	writel(0x0, mfc_regs->e_extended_sar);
954 955 956 957
	if (p_h264->vui_sar) {
		/* aspect ration IDC */
		reg = 0;
		reg |= p_h264->vui_sar_idc & 0xFF;
958
		writel(reg, mfc_regs->e_aspect_ratio);
959 960 961 962 963
		if (p_h264->vui_sar_idc == 0xFF) {
			/* extended SAR */
			reg = 0;
			reg |= (p_h264->vui_ext_sar_width & 0xFFFF) << 16;
			reg |= p_h264->vui_ext_sar_height & 0xFFFF;
964
			writel(reg, mfc_regs->e_extended_sar);
965 966 967 968 969
		}
	}

	/* intra picture period for H.264 open GOP */
	/* control */
970
	readl(mfc_regs->e_h264_options);
971 972
	reg &= ~(0x1 << 4);
	reg |= ((p_h264->open_gop & 0x1) << 4);
973
	writel(reg, mfc_regs->e_h264_options);
974

975
	/* value */
976
	writel(0x0, mfc_regs->e_h264_i_period);
977 978 979
	if (p_h264->open_gop) {
		reg = 0;
		reg |= p_h264->open_gop_size & 0xFFFF;
980
		writel(reg, mfc_regs->e_h264_i_period);
981 982 983
	}

	/* 'WEIGHTED_BI_PREDICTION' for B is disable */
984
	readl(mfc_regs->e_h264_options);
985
	reg &= ~(0x3 << 9);
986
	writel(reg, mfc_regs->e_h264_options);
987 988

	/* 'CONSTRAINED_INTRA_PRED_ENABLE' is disable */
989
	readl(mfc_regs->e_h264_options);
990
	reg &= ~(0x1 << 14);
991
	writel(reg, mfc_regs->e_h264_options);
992 993

	/* ASO */
994
	readl(mfc_regs->e_h264_options);
995 996
	reg &= ~(0x1 << 6);
	reg |= ((p_h264->aso & 0x1) << 6);
997
	writel(reg, mfc_regs->e_h264_options);
998 999

	/* hier qp enable */
1000
	readl(mfc_regs->e_h264_options);
1001 1002
	reg &= ~(0x1 << 8);
	reg |= ((p_h264->open_gop & 0x1) << 8);
1003
	writel(reg, mfc_regs->e_h264_options);
1004 1005 1006 1007
	reg = 0;
	if (p_h264->hier_qp && p_h264->hier_qp_layer) {
		reg |= (p_h264->hier_qp_type & 0x1) << 0x3;
		reg |= p_h264->hier_qp_layer & 0x7;
1008
		writel(reg, mfc_regs->e_h264_num_t_layer);
1009
		/* QP value for each layer */
1010 1011
		for (i = 0; i < p_h264->hier_qp_layer &&
				i < ARRAY_SIZE(p_h264->hier_qp_layer_qp); i++) {
1012
			writel(p_h264->hier_qp_layer_qp[i],
1013 1014 1015
				mfc_regs->e_h264_hierarchical_qp_layer0
				+ i * 4);
		}
1016 1017
	}
	/* number of coding layer should be zero when hierarchical is disable */
1018
	writel(reg, mfc_regs->e_h264_num_t_layer);
1019 1020

	/* frame packing SEI generation */
1021
	readl(mfc_regs->e_h264_options);
1022 1023
	reg &= ~(0x1 << 25);
	reg |= ((p_h264->sei_frame_packing & 0x1) << 25);
1024
	writel(reg, mfc_regs->e_h264_options);
1025 1026 1027 1028 1029 1030
	if (p_h264->sei_frame_packing) {
		reg = 0;
		/** current frame0 flag */
		reg |= ((p_h264->sei_fp_curr_frame_0 & 0x1) << 2);
		/** arrangement type */
		reg |= p_h264->sei_fp_arrangement_type & 0x3;
1031
		writel(reg, mfc_regs->e_h264_frame_packing_sei_info);
1032 1033 1034 1035 1036 1037 1038 1039
	}

	if (p_h264->fmo) {
		switch (p_h264->fmo_map_type) {
		case V4L2_MPEG_VIDEO_H264_FMO_MAP_TYPE_INTERLEAVED_SLICES:
			if (p_h264->fmo_slice_grp > 4)
				p_h264->fmo_slice_grp = 4;
			for (i = 0; i < (p_h264->fmo_slice_grp & 0xF); i++)
1040
				writel(p_h264->fmo_run_len[i] - 1,
1041 1042
					mfc_regs->e_h264_fmo_run_length_minus1_0
					+ i * 4);
1043 1044 1045 1046 1047 1048 1049 1050 1051
			break;
		case V4L2_MPEG_VIDEO_H264_FMO_MAP_TYPE_SCATTERED_SLICES:
			if (p_h264->fmo_slice_grp > 4)
				p_h264->fmo_slice_grp = 4;
			break;
		case V4L2_MPEG_VIDEO_H264_FMO_MAP_TYPE_RASTER_SCAN:
		case V4L2_MPEG_VIDEO_H264_FMO_MAP_TYPE_WIPE_SCAN:
			if (p_h264->fmo_slice_grp > 2)
				p_h264->fmo_slice_grp = 2;
1052
			writel(p_h264->fmo_chg_dir & 0x1,
1053
				mfc_regs->e_h264_fmo_slice_grp_change_dir);
1054
			/* the valid range is 0 ~ number of macroblocks -1 */
1055
			writel(p_h264->fmo_chg_rate,
1056
			mfc_regs->e_h264_fmo_slice_grp_change_rate_minus1);
1057 1058 1059 1060 1061 1062 1063 1064 1065
			break;
		default:
			mfc_err("Unsupported map type for FMO: %d\n",
					p_h264->fmo_map_type);
			p_h264->fmo_map_type = 0;
			p_h264->fmo_slice_grp = 1;
			break;
		}

1066
		writel(p_h264->fmo_map_type,
1067
				mfc_regs->e_h264_fmo_slice_grp_map_type);
1068
		writel(p_h264->fmo_slice_grp - 1,
1069
				mfc_regs->e_h264_fmo_num_slice_grp_minus1);
1070
	} else {
1071
		writel(0, mfc_regs->e_h264_fmo_num_slice_grp_minus1);
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
	}

	mfc_debug_leave();

	return 0;
}

static int s5p_mfc_set_enc_params_mpeg4(struct s5p_mfc_ctx *ctx)
{
	struct s5p_mfc_dev *dev = ctx->dev;
1082
	const struct s5p_mfc_regs *mfc_regs = dev->mfc_regs;
1083 1084 1085 1086 1087 1088 1089 1090 1091
	struct s5p_mfc_enc_params *p = &ctx->enc_params;
	struct s5p_mfc_mpeg4_enc_params *p_mpeg4 = &p->codec.mpeg4;
	unsigned int reg = 0;

	mfc_debug_enter();

	s5p_mfc_set_enc_params(ctx);

	/* pictype : number of B */
1092
	reg = readl(mfc_regs->e_gop_config);
1093 1094
	reg &= ~(0x3 << 16);
	reg |= ((p->num_b_frame & 0x3) << 16);
1095
	writel(reg, mfc_regs->e_gop_config);
1096 1097 1098 1099 1100 1101 1102

	/* profile & level */
	reg = 0;
	/** level */
	reg |= ((p_mpeg4->level & 0xFF) << 8);
	/** profile - 0 ~ 1 */
	reg |= p_mpeg4->profile & 0x3F;
1103
	writel(reg, mfc_regs->e_picture_profile);
1104 1105

	/* rate control config. */
1106
	reg = readl(mfc_regs->e_rc_config);
1107 1108 1109
	/** macroblock level rate control */
	reg &= ~(0x1 << 8);
	reg |= ((p->rc_mb & 0x1) << 8);
1110
	writel(reg, mfc_regs->e_rc_config);
1111

1112 1113 1114
	/** frame QP */
	reg &= ~(0x3F);
	reg |= p_mpeg4->rc_frame_qp & 0x3F;
1115
	writel(reg, mfc_regs->e_rc_config);
1116 1117 1118 1119 1120 1121 1122

	/* max & min value of QP */
	reg = 0;
	/** max QP */
	reg |= ((p_mpeg4->rc_max_qp & 0x3F) << 8);
	/** min QP */
	reg |= p_mpeg4->rc_min_qp & 0x3F;
1123
	writel(reg, mfc_regs->e_rc_qp_bound);
1124 1125

	/* other QPs */
1126
	writel(0x0, mfc_regs->e_fixed_picture_qp);
1127 1128 1129 1130 1131
	if (!p->rc_frame && !p->rc_mb) {
		reg = 0;
		reg |= ((p_mpeg4->rc_b_frame_qp & 0x3F) << 16);
		reg |= ((p_mpeg4->rc_p_frame_qp & 0x3F) << 8);
		reg |= p_mpeg4->rc_frame_qp & 0x3F;
1132
		writel(reg, mfc_regs->e_fixed_picture_qp);
1133 1134 1135 1136 1137 1138 1139
	}

	/* frame rate */
	if (p->rc_frame && p->rc_framerate_num && p->rc_framerate_denom) {
		reg = 0;
		reg |= ((p->rc_framerate_num & 0xFFFF) << 16);
		reg |= p->rc_framerate_denom & 0xFFFF;
1140
		writel(reg, mfc_regs->e_rc_frame_rate);
1141 1142 1143 1144 1145
	}

	/* vbv buffer size */
	if (p->frame_skip_mode ==
			V4L2_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE_BUF_LIMIT) {
1146
		writel(p->vbv_size & 0xFFFF, mfc_regs->e_vbv_buffer_size);
1147 1148

		if (p->rc_frame)
1149
			writel(p->vbv_delay, mfc_regs->e_vbv_init_delay);
1150 1151 1152
	}

	/* Disable HEC */
1153 1154
	writel(0x0, mfc_regs->e_mpeg4_options);
	writel(0x0, mfc_regs->e_mpeg4_hec_period);
1155 1156 1157 1158 1159 1160 1161 1162 1163

	mfc_debug_leave();

	return 0;
}

static int s5p_mfc_set_enc_params_h263(struct s5p_mfc_ctx *ctx)
{
	struct s5p_mfc_dev *dev = ctx->dev;
1164
	const struct s5p_mfc_regs *mfc_regs = dev->mfc_regs;
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
	struct s5p_mfc_enc_params *p = &ctx->enc_params;
	struct s5p_mfc_mpeg4_enc_params *p_h263 = &p->codec.mpeg4;
	unsigned int reg = 0;

	mfc_debug_enter();

	s5p_mfc_set_enc_params(ctx);

	/* profile & level */
	reg = 0;
	/** profile */
	reg |= (0x1 << 4);
1177
	writel(reg, mfc_regs->e_picture_profile);
1178 1179

	/* rate control config. */
1180
	reg = readl(mfc_regs->e_rc_config);
1181 1182 1183
	/** macroblock level rate control */
	reg &= ~(0x1 << 8);
	reg |= ((p->rc_mb & 0x1) << 8);
1184
	writel(reg, mfc_regs->e_rc_config);
1185

1186 1187 1188
	/** frame QP */
	reg &= ~(0x3F);
	reg |= p_h263->rc_frame_qp & 0x3F;
1189
	writel(reg, mfc_regs->e_rc_config);
1190 1191 1192 1193 1194 1195 1196

	/* max & min value of QP */
	reg = 0;
	/** max QP */
	reg |= ((p_h263->rc_max_qp & 0x3F) << 8);
	/** min QP */
	reg |= p_h263->rc_min_qp & 0x3F;
1197
	writel(reg, mfc_regs->e_rc_qp_bound);
1198 1199

	/* other QPs */
1200
	writel(0x0, mfc_regs->e_fixed_picture_qp);
1201 1202 1203 1204 1205
	if (!p->rc_frame && !p->rc_mb) {
		reg = 0;
		reg |= ((p_h263->rc_b_frame_qp & 0x3F) << 16);
		reg |= ((p_h263->rc_p_frame_qp & 0x3F) << 8);
		reg |= p_h263->rc_frame_qp & 0x3F;
1206
		writel(reg, mfc_regs->e_fixed_picture_qp);
1207 1208 1209 1210 1211 1212 1213
	}

	/* frame rate */
	if (p->rc_frame && p->rc_framerate_num && p->rc_framerate_denom) {
		reg = 0;
		reg |= ((p->rc_framerate_num & 0xFFFF) << 16);
		reg |= p->rc_framerate_denom & 0xFFFF;
1214
		writel(reg, mfc_regs->e_rc_frame_rate);
1215 1216 1217 1218 1219
	}

	/* vbv buffer size */
	if (p->frame_skip_mode ==
			V4L2_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE_BUF_LIMIT) {
1220
		writel(p->vbv_size & 0xFFFF, mfc_regs->e_vbv_buffer_size);
1221 1222

		if (p->rc_frame)
1223
			writel(p->vbv_delay, mfc_regs->e_vbv_init_delay);
1224 1225 1226 1227 1228 1229 1230
	}

	mfc_debug_leave();

	return 0;
}

1231 1232 1233
static int s5p_mfc_set_enc_params_vp8(struct s5p_mfc_ctx *ctx)
{
	struct s5p_mfc_dev *dev = ctx->dev;
1234
	const struct s5p_mfc_regs *mfc_regs = dev->mfc_regs;
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
	struct s5p_mfc_enc_params *p = &ctx->enc_params;
	struct s5p_mfc_vp8_enc_params *p_vp8 = &p->codec.vp8;
	unsigned int reg = 0;
	unsigned int val = 0;

	mfc_debug_enter();

	s5p_mfc_set_enc_params(ctx);

	/* pictype : number of B */
1245
	reg = readl(mfc_regs->e_gop_config);
1246 1247
	reg &= ~(0x3 << 16);
	reg |= ((p->num_b_frame & 0x3) << 16);
1248
	writel(reg, mfc_regs->e_gop_config);
1249

1250 1251
	/* profile - 0 ~ 3 */
	reg = p_vp8->profile & 0x3;
1252
	writel(reg, mfc_regs->e_picture_profile);
1253 1254

	/* rate control config. */
1255
	reg = readl(mfc_regs->e_rc_config);
1256 1257 1258
	/** macroblock level rate control */
	reg &= ~(0x1 << 8);
	reg |= ((p->rc_mb & 0x1) << 8);
1259
	writel(reg, mfc_regs->e_rc_config);
1260 1261 1262 1263 1264 1265

	/* frame rate */
	if (p->rc_frame && p->rc_framerate_num && p->rc_framerate_denom) {
		reg = 0;
		reg |= ((p->rc_framerate_num & 0xFFFF) << 16);
		reg |= p->rc_framerate_denom & 0xFFFF;
1266
		writel(reg, mfc_regs->e_rc_frame_rate);
1267 1268
	}

1269 1270 1271
	/* frame QP */
	reg &= ~(0x7F);
	reg |= p_vp8->rc_frame_qp & 0x7F;
1272
	writel(reg, mfc_regs->e_rc_config);
1273 1274

	/* other QPs */
1275
	writel(0x0, mfc_regs->e_fixed_picture_qp);
1276 1277 1278 1279
	if (!p->rc_frame && !p->rc_mb) {
		reg = 0;
		reg |= ((p_vp8->rc_p_frame_qp & 0x7F) << 8);
		reg |= p_vp8->rc_frame_qp & 0x7F;
1280
		writel(reg, mfc_regs->e_fixed_picture_qp);
1281 1282 1283 1284 1285 1286
	}

	/* max QP */
	reg = ((p_vp8->rc_max_qp & 0x7F) << 8);
	/* min QP */
	reg |= p_vp8->rc_min_qp & 0x7F;
1287
	writel(reg, mfc_regs->e_rc_qp_bound);
1288

1289 1290 1291
	/* vbv buffer size */
	if (p->frame_skip_mode ==
			V4L2_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE_BUF_LIMIT) {
1292
		writel(p->vbv_size & 0xFFFF, mfc_regs->e_vbv_buffer_size);
1293 1294

		if (p->rc_frame)
1295
			writel(p->vbv_delay, mfc_regs->e_vbv_init_delay);
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316
	}

	/* VP8 specific params */
	reg = 0;
	reg |= (p_vp8->imd_4x4 & 0x1) << 10;
	switch (p_vp8->num_partitions) {
	case V4L2_CID_MPEG_VIDEO_VPX_1_PARTITION:
		val = 0;
		break;
	case V4L2_CID_MPEG_VIDEO_VPX_2_PARTITIONS:
		val = 2;
		break;
	case V4L2_CID_MPEG_VIDEO_VPX_4_PARTITIONS:
		val = 4;
		break;
	case V4L2_CID_MPEG_VIDEO_VPX_8_PARTITIONS:
		val = 8;
		break;
	}
	reg |= (val & 0xF) << 3;
	reg |= (p_vp8->num_ref & 0x2);
1317
	writel(reg, mfc_regs->e_vp8_options);
1318 1319 1320 1321 1322 1323

	mfc_debug_leave();

	return 0;
}

1324
/* Initialize decoding */
1325
static int s5p_mfc_init_decode_v6(struct s5p_mfc_ctx *ctx)
1326 1327
{
	struct s5p_mfc_dev *dev = ctx->dev;
1328
	const struct s5p_mfc_regs *mfc_regs = dev->mfc_regs;
1329 1330 1331 1332 1333 1334 1335
	unsigned int reg = 0;
	int fmo_aso_ctrl = 0;

	mfc_debug_enter();
	mfc_debug(2, "InstNo: %d/%d\n", ctx->inst_no,
			S5P_FIMV_CH_SEQ_HEADER_V6);
	mfc_debug(2, "BUFs: %08x %08x %08x\n",
1336 1337 1338
		  readl(mfc_regs->d_cpb_buffer_addr),
		  readl(mfc_regs->d_cpb_buffer_addr),
		  readl(mfc_regs->d_cpb_buffer_addr));
1339 1340 1341 1342

	/* FMO_ASO_CTRL - 0: Enable, 1: Disable */
	reg |= (fmo_aso_ctrl << S5P_FIMV_D_OPT_FMO_ASO_CTRL_MASK_V6);

1343
	if (ctx->display_delay_enable) {
1344
		reg |= (0x1 << S5P_FIMV_D_OPT_DDELAY_EN_SHIFT_V6);
1345
		writel(ctx->display_delay, mfc_regs->d_display_delay);
1346
	}
1347

1348
	if (IS_MFCV7_PLUS(dev) || IS_MFCV6_V2(dev)) {
1349
		writel(reg, mfc_regs->d_dec_options);
1350 1351 1352
		reg = 0;
	}

1353 1354 1355 1356 1357 1358 1359 1360 1361 1362
	/* Setup loop filter, for decoding this is only valid for MPEG4 */
	if (ctx->codec_mode == S5P_MFC_CODEC_MPEG4_DEC) {
		mfc_debug(2, "Set loop filter to: %d\n",
				ctx->loop_filter_mpeg4);
		reg |= (ctx->loop_filter_mpeg4 <<
				S5P_FIMV_D_OPT_LF_CTRL_SHIFT_V6);
	}
	if (ctx->dst_fmt->fourcc == V4L2_PIX_FMT_NV12MT_16X16)
		reg |= (0x1 << S5P_FIMV_D_OPT_TILE_MODE_SHIFT_V6);

1363
	if (IS_MFCV7_PLUS(dev) || IS_MFCV6_V2(dev))
1364
		writel(reg, mfc_regs->d_init_buffer_options);
1365
	else
1366
		writel(reg, mfc_regs->d_dec_options);
1367 1368 1369

	/* 0: NV12(CbCr), 1: NV21(CrCb) */
	if (ctx->dst_fmt->fourcc == V4L2_PIX_FMT_NV21M)
1370
		writel(0x1, mfc_regs->pixel_format);
1371
	else
1372
		writel(0x0, mfc_regs->pixel_format);
1373

1374

1375
	/* sei parse */
1376
	writel(ctx->sei_fp_parse & 0x1, mfc_regs->d_sei_enable);
1377

1378
	writel(ctx->inst_no, mfc_regs->instance_id);
1379
	s5p_mfc_hw_call(dev->mfc_cmds, cmd_host2risc, dev,
1380 1381 1382 1383 1384 1385 1386 1387 1388
			S5P_FIMV_CH_SEQ_HEADER_V6, NULL);

	mfc_debug_leave();
	return 0;
}

static inline void s5p_mfc_set_flush(struct s5p_mfc_ctx *ctx, int flush)
{
	struct s5p_mfc_dev *dev = ctx->dev;
1389
	const struct s5p_mfc_regs *mfc_regs = dev->mfc_regs;
1390 1391 1392

	if (flush) {
		dev->curr_ctx = ctx->num;
1393
		writel(ctx->inst_no, mfc_regs->instance_id);
1394
		s5p_mfc_hw_call(dev->mfc_cmds, cmd_host2risc, dev,
1395 1396
				S5P_FIMV_H2R_CMD_FLUSH_V6, NULL);
	}
1397 1398 1399
}

/* Decode a single frame */
1400
static int s5p_mfc_decode_one_frame_v6(struct s5p_mfc_ctx *ctx,
1401 1402 1403
			enum s5p_mfc_decode_arg last_frame)
{
	struct s5p_mfc_dev *dev = ctx->dev;
1404
	const struct s5p_mfc_regs *mfc_regs = dev->mfc_regs;
1405

1406 1407
	writel(ctx->dec_dst_flag, mfc_regs->d_available_dpb_flag_lower);
	writel(ctx->slice_interface & 0x1, mfc_regs->d_slice_if_enable);
1408

1409
	writel(ctx->inst_no, mfc_regs->instance_id);
1410 1411 1412 1413
	/* Issue different commands to instance basing on whether it
	 * is the last frame or not. */
	switch (last_frame) {
	case 0:
1414
		s5p_mfc_hw_call(dev->mfc_cmds, cmd_host2risc, dev,
1415 1416 1417
				S5P_FIMV_CH_FRAME_START_V6, NULL);
		break;
	case 1:
1418
		s5p_mfc_hw_call(dev->mfc_cmds, cmd_host2risc, dev,
1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429
				S5P_FIMV_CH_LAST_FRAME_V6, NULL);
		break;
	default:
		mfc_err("Unsupported last frame arg.\n");
		return -EINVAL;
	}

	mfc_debug(2, "Decoding a usual frame.\n");
	return 0;
}

1430
static int s5p_mfc_init_encode_v6(struct s5p_mfc_ctx *ctx)
1431 1432
{
	struct s5p_mfc_dev *dev = ctx->dev;
1433
	const struct s5p_mfc_regs *mfc_regs = dev->mfc_regs;
1434 1435 1436 1437 1438 1439 1440

	if (ctx->codec_mode == S5P_MFC_CODEC_H264_ENC)
		s5p_mfc_set_enc_params_h264(ctx);
	else if (ctx->codec_mode == S5P_MFC_CODEC_MPEG4_ENC)
		s5p_mfc_set_enc_params_mpeg4(ctx);
	else if (ctx->codec_mode == S5P_MFC_CODEC_H263_ENC)
		s5p_mfc_set_enc_params_h263(ctx);
1441 1442
	else if (ctx->codec_mode == S5P_MFC_CODEC_VP8_ENC)
		s5p_mfc_set_enc_params_vp8(ctx);
1443 1444 1445 1446 1447 1448
	else {
		mfc_err("Unknown codec for encoding (%x).\n",
			ctx->codec_mode);
		return -EINVAL;
	}

1449
	/* Set stride lengths for v7 & above */
1450
	if (IS_MFCV7_PLUS(dev)) {
1451 1452
		writel(ctx->img_width, mfc_regs->e_source_first_plane_stride);
		writel(ctx->img_width, mfc_regs->e_source_second_plane_stride);
1453 1454
	}

1455
	writel(ctx->inst_no, mfc_regs->instance_id);
1456
	s5p_mfc_hw_call(dev->mfc_cmds, cmd_host2risc, dev,
1457 1458 1459 1460 1461
			S5P_FIMV_CH_SEQ_HEADER_V6, NULL);

	return 0;
}

1462
static int s5p_mfc_h264_set_aso_slice_order_v6(struct s5p_mfc_ctx *ctx)
1463 1464
{
	struct s5p_mfc_dev *dev = ctx->dev;
1465
	const struct s5p_mfc_regs *mfc_regs = dev->mfc_regs;
1466 1467 1468 1469 1470
	struct s5p_mfc_enc_params *p = &ctx->enc_params;
	struct s5p_mfc_h264_enc_params *p_h264 = &p->codec.h264;
	int i;

	if (p_h264->aso) {
1471
		for (i = 0; i < ARRAY_SIZE(p_h264->aso_slice_order); i++) {
1472
			writel(p_h264->aso_slice_order[i],
1473 1474
				mfc_regs->e_h264_aso_slice_order_0 + i * 4);
		}
1475 1476 1477 1478 1479
	}
	return 0;
}

/* Encode a single frame */
1480
static int s5p_mfc_encode_one_frame_v6(struct s5p_mfc_ctx *ctx)
1481 1482
{
	struct s5p_mfc_dev *dev = ctx->dev;
1483
	const struct s5p_mfc_regs *mfc_regs = dev->mfc_regs;
1484
	int cmd;
1485 1486 1487 1488 1489 1490 1491 1492 1493 1494

	mfc_debug(2, "++\n");

	/* memory structure cur. frame */

	if (ctx->codec_mode == S5P_MFC_CODEC_H264_ENC)
		s5p_mfc_h264_set_aso_slice_order_v6(ctx);

	s5p_mfc_set_slice_mode(ctx);

1495 1496 1497 1498 1499
	if (ctx->state != MFCINST_FINISHING)
		cmd = S5P_FIMV_CH_FRAME_START_V6;
	else
		cmd = S5P_FIMV_CH_LAST_FRAME_V6;

1500
	writel(ctx->inst_no, mfc_regs->instance_id);
1501
	s5p_mfc_hw_call(dev->mfc_cmds, cmd_host2risc, dev, cmd, NULL);
1502 1503 1504 1505 1506 1507 1508 1509 1510 1511

	mfc_debug(2, "--\n");

	return 0;
}

static inline void s5p_mfc_run_dec_last_frames(struct s5p_mfc_ctx *ctx)
{
	struct s5p_mfc_dev *dev = ctx->dev;

1512
	s5p_mfc_set_dec_stream_buffer_v6(ctx, 0, 0, 0);
1513
	dev->curr_ctx = ctx->num;
1514
	s5p_mfc_decode_one_frame_v6(ctx, MFC_DEC_LAST_FRAME);
1515 1516 1517 1518 1519 1520 1521 1522
}

static inline int s5p_mfc_run_dec_frame(struct s5p_mfc_ctx *ctx)
{
	struct s5p_mfc_dev *dev = ctx->dev;
	struct s5p_mfc_buf *temp_vb;
	int last_frame = 0;

1523 1524 1525 1526 1527 1528 1529 1530
	if (ctx->state == MFCINST_FINISHING) {
		last_frame = MFC_DEC_LAST_FRAME;
		s5p_mfc_set_dec_stream_buffer_v6(ctx, 0, 0, 0);
		dev->curr_ctx = ctx->num;
		s5p_mfc_clean_ctx_int_flags(ctx);
		s5p_mfc_decode_one_frame_v6(ctx, last_frame);
		return 0;
	}
1531 1532 1533 1534 1535 1536 1537 1538 1539 1540

	/* Frames are being decoded */
	if (list_empty(&ctx->src_queue)) {
		mfc_debug(2, "No src buffers.\n");
		return -EAGAIN;
	}
	/* Get the next source buffer */
	temp_vb = list_entry(ctx->src_queue.next, struct s5p_mfc_buf, list);
	temp_vb->flags |= MFC_BUF_FLAG_USED;
	s5p_mfc_set_dec_stream_buffer_v6(ctx,
1541
		vb2_dma_contig_plane_dma_addr(&temp_vb->b->vb2_buf, 0),
1542 1543
			ctx->consumed_stream,
			temp_vb->b->vb2_buf.planes[0].bytesused);
1544 1545

	dev->curr_ctx = ctx->num;
1546
	if (temp_vb->b->vb2_buf.planes[0].bytesused == 0) {
1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566
		last_frame = 1;
		mfc_debug(2, "Setting ctx->state to FINISHING\n");
		ctx->state = MFCINST_FINISHING;
	}
	s5p_mfc_decode_one_frame_v6(ctx, last_frame);

	return 0;
}

static inline int s5p_mfc_run_enc_frame(struct s5p_mfc_ctx *ctx)
{
	struct s5p_mfc_dev *dev = ctx->dev;
	struct s5p_mfc_buf *dst_mb;
	struct s5p_mfc_buf *src_mb;
	unsigned long src_y_addr, src_c_addr, dst_addr;
	/*
	unsigned int src_y_size, src_c_size;
	*/
	unsigned int dst_size;

1567
	if (list_empty(&ctx->src_queue) && ctx->state != MFCINST_FINISHING) {
1568 1569 1570 1571 1572 1573 1574 1575 1576
		mfc_debug(2, "no src buffers.\n");
		return -EAGAIN;
	}

	if (list_empty(&ctx->dst_queue)) {
		mfc_debug(2, "no dst buffers.\n");
		return -EAGAIN;
	}

1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589
	if (list_empty(&ctx->src_queue)) {
		/* send null frame */
		s5p_mfc_set_enc_frame_buffer_v6(ctx, 0, 0);
		src_mb = NULL;
	} else {
		src_mb = list_entry(ctx->src_queue.next, struct s5p_mfc_buf, list);
		src_mb->flags |= MFC_BUF_FLAG_USED;
		if (src_mb->b->vb2_buf.planes[0].bytesused == 0) {
			s5p_mfc_set_enc_frame_buffer_v6(ctx, 0, 0);
			ctx->state = MFCINST_FINISHING;
		} else {
			src_y_addr = vb2_dma_contig_plane_dma_addr(&src_mb->b->vb2_buf, 0);
			src_c_addr = vb2_dma_contig_plane_dma_addr(&src_mb->b->vb2_buf, 1);
1590

1591 1592
			mfc_debug(2, "enc src y addr: 0x%08lx\n", src_y_addr);
			mfc_debug(2, "enc src c addr: 0x%08lx\n", src_c_addr);
1593

1594 1595 1596 1597 1598
			s5p_mfc_set_enc_frame_buffer_v6(ctx, src_y_addr, src_c_addr);
			if (src_mb->flags & MFC_BUF_FLAG_EOS)
				ctx->state = MFCINST_FINISHING;
		}
	}
1599 1600 1601

	dst_mb = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf, list);
	dst_mb->flags |= MFC_BUF_FLAG_USED;
1602 1603
	dst_addr = vb2_dma_contig_plane_dma_addr(&dst_mb->b->vb2_buf, 0);
	dst_size = vb2_plane_size(&dst_mb->b->vb2_buf, 0);
1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620

	s5p_mfc_set_enc_stream_buffer_v6(ctx, dst_addr, dst_size);

	dev->curr_ctx = ctx->num;
	s5p_mfc_encode_one_frame_v6(ctx);

	return 0;
}

static inline void s5p_mfc_run_init_dec(struct s5p_mfc_ctx *ctx)
{
	struct s5p_mfc_dev *dev = ctx->dev;
	struct s5p_mfc_buf *temp_vb;

	/* Initializing decoding - parsing header */
	mfc_debug(2, "Preparing to init decoding.\n");
	temp_vb = list_entry(ctx->src_queue.next, struct s5p_mfc_buf, list);
1621
	mfc_debug(2, "Header size: %d\n", temp_vb->b->vb2_buf.planes[0].bytesused);
1622
	s5p_mfc_set_dec_stream_buffer_v6(ctx,
1623
		vb2_dma_contig_plane_dma_addr(&temp_vb->b->vb2_buf, 0), 0,
1624
			temp_vb->b->vb2_buf.planes[0].bytesused);
1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636
	dev->curr_ctx = ctx->num;
	s5p_mfc_init_decode_v6(ctx);
}

static inline void s5p_mfc_run_init_enc(struct s5p_mfc_ctx *ctx)
{
	struct s5p_mfc_dev *dev = ctx->dev;
	struct s5p_mfc_buf *dst_mb;
	unsigned long dst_addr;
	unsigned int dst_size;

	dst_mb = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf, list);
1637 1638
	dst_addr = vb2_dma_contig_plane_dma_addr(&dst_mb->b->vb2_buf, 0);
	dst_size = vb2_plane_size(&dst_mb->b->vb2_buf, 0);
1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682
	s5p_mfc_set_enc_stream_buffer_v6(ctx, dst_addr, dst_size);
	dev->curr_ctx = ctx->num;
	s5p_mfc_init_encode_v6(ctx);
}

static inline int s5p_mfc_run_init_dec_buffers(struct s5p_mfc_ctx *ctx)
{
	struct s5p_mfc_dev *dev = ctx->dev;
	int ret;
	/* Header was parsed now start processing
	 * First set the output frame buffers
	 * s5p_mfc_alloc_dec_buffers(ctx); */

	if (ctx->capture_state != QUEUE_BUFS_MMAPED) {
		mfc_err("It seems that not all destionation buffers were\n"
			"mmaped.MFC requires that all destination are mmaped\n"
			"before starting processing.\n");
		return -EAGAIN;
	}

	dev->curr_ctx = ctx->num;
	ret = s5p_mfc_set_dec_frame_buffer_v6(ctx);
	if (ret) {
		mfc_err("Failed to alloc frame mem.\n");
		ctx->state = MFCINST_ERROR;
	}
	return ret;
}

static inline int s5p_mfc_run_init_enc_buffers(struct s5p_mfc_ctx *ctx)
{
	struct s5p_mfc_dev *dev = ctx->dev;
	int ret;

	dev->curr_ctx = ctx->num;
	ret = s5p_mfc_set_enc_ref_buffer_v6(ctx);
	if (ret) {
		mfc_err("Failed to alloc frame mem.\n");
		ctx->state = MFCINST_ERROR;
	}
	return ret;
}

/* Try running an operation on hardware */
1683
static void s5p_mfc_try_run_v6(struct s5p_mfc_dev *dev)
1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712
{
	struct s5p_mfc_ctx *ctx;
	int new_ctx;
	unsigned int ret = 0;

	mfc_debug(1, "Try run dev: %p\n", dev);

	/* Check whether hardware is not running */
	if (test_and_set_bit(0, &dev->hw_lock) != 0) {
		/* This is perfectly ok, the scheduled ctx should wait */
		mfc_debug(1, "Couldn't lock HW.\n");
		return;
	}

	/* Choose the context to run */
	new_ctx = s5p_mfc_get_new_ctx(dev);
	if (new_ctx < 0) {
		/* No contexts to run */
		if (test_and_clear_bit(0, &dev->hw_lock) == 0) {
			mfc_err("Failed to unlock hardware.\n");
			return;
		}

		mfc_debug(1, "No ctx is scheduled to be run.\n");
		return;
	}

	mfc_debug(1, "New context: %d\n", new_ctx);
	ctx = dev->ctx[new_ctx];
M
Masanari Iida 已提交
1713
	mfc_debug(1, "Setting new context to %p\n", ctx);
1714 1715
	/* Got context to run in ctx */
	mfc_debug(1, "ctx->dst_queue_cnt=%d ctx->dpb_count=%d ctx->src_queue_cnt=%d\n",
1716
		ctx->dst_queue_cnt, ctx->pb_count, ctx->src_queue_cnt);
1717 1718 1719 1720 1721
	mfc_debug(1, "ctx->state=%d\n", ctx->state);
	/* Last frame has already been sent to MFC
	 * Now obtaining frames from MFC buffer */

	s5p_mfc_clock_on();
1722 1723
	s5p_mfc_clean_ctx_int_flags(ctx);

1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745
	if (ctx->type == MFCINST_DECODER) {
		switch (ctx->state) {
		case MFCINST_FINISHING:
			s5p_mfc_run_dec_last_frames(ctx);
			break;
		case MFCINST_RUNNING:
			ret = s5p_mfc_run_dec_frame(ctx);
			break;
		case MFCINST_INIT:
			ret = s5p_mfc_hw_call(dev->mfc_cmds, open_inst_cmd,
					ctx);
			break;
		case MFCINST_RETURN_INST:
			ret = s5p_mfc_hw_call(dev->mfc_cmds, close_inst_cmd,
					ctx);
			break;
		case MFCINST_GOT_INST:
			s5p_mfc_run_init_dec(ctx);
			break;
		case MFCINST_HEAD_PARSED:
			ret = s5p_mfc_run_init_dec_buffers(ctx);
			break;
1746 1747 1748
		case MFCINST_FLUSH:
			s5p_mfc_set_flush(ctx, ctx->dpb_flush_flag);
			break;
1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780
		case MFCINST_RES_CHANGE_INIT:
			s5p_mfc_run_dec_last_frames(ctx);
			break;
		case MFCINST_RES_CHANGE_FLUSH:
			s5p_mfc_run_dec_last_frames(ctx);
			break;
		case MFCINST_RES_CHANGE_END:
			mfc_debug(2, "Finished remaining frames after resolution change.\n");
			ctx->capture_state = QUEUE_FREE;
			mfc_debug(2, "Will re-init the codec`.\n");
			s5p_mfc_run_init_dec(ctx);
			break;
		default:
			ret = -EAGAIN;
		}
	} else if (ctx->type == MFCINST_ENCODER) {
		switch (ctx->state) {
		case MFCINST_FINISHING:
		case MFCINST_RUNNING:
			ret = s5p_mfc_run_enc_frame(ctx);
			break;
		case MFCINST_INIT:
			ret = s5p_mfc_hw_call(dev->mfc_cmds, open_inst_cmd,
					ctx);
			break;
		case MFCINST_RETURN_INST:
			ret = s5p_mfc_hw_call(dev->mfc_cmds, close_inst_cmd,
					ctx);
			break;
		case MFCINST_GOT_INST:
			s5p_mfc_run_init_enc(ctx);
			break;
1781
		case MFCINST_HEAD_PRODUCED:
1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804
			ret = s5p_mfc_run_init_enc_buffers(ctx);
			break;
		default:
			ret = -EAGAIN;
		}
	} else {
		mfc_err("invalid context type: %d\n", ctx->type);
		ret = -EAGAIN;
	}

	if (ret) {
		/* Free hardware lock */
		if (test_and_clear_bit(0, &dev->hw_lock) == 0)
			mfc_err("Failed to unlock hardware.\n");

		/* This is in deed imporant, as no operation has been
		 * scheduled, reduce the clock count as no one will
		 * ever do this, because no interrupt related to this try_run
		 * will ever come from hardware. */
		s5p_mfc_clock_off();
	}
}

1805
static void s5p_mfc_clear_int_flags_v6(struct s5p_mfc_dev *dev)
1806
{
1807
	const struct s5p_mfc_regs *mfc_regs = dev->mfc_regs;
1808 1809
	writel(0, mfc_regs->risc2host_command);
	writel(0, mfc_regs->risc2host_int);
1810 1811
}

1812
static unsigned int
1813
s5p_mfc_read_info_v6(struct s5p_mfc_ctx *ctx, unsigned long ofs)
1814 1815 1816 1817
{
	int ret;

	s5p_mfc_clock_on();
1818
	ret = readl((void __iomem *)ofs);
1819 1820 1821 1822 1823
	s5p_mfc_clock_off();

	return ret;
}

1824
static int s5p_mfc_get_dspl_y_adr_v6(struct s5p_mfc_dev *dev)
1825
{
1826
	return readl(dev->mfc_regs->d_display_first_plane_addr);
1827 1828
}

1829
static int s5p_mfc_get_dec_y_adr_v6(struct s5p_mfc_dev *dev)
1830
{
1831
	return readl(dev->mfc_regs->d_decoded_first_plane_addr);
1832 1833
}

1834
static int s5p_mfc_get_dspl_status_v6(struct s5p_mfc_dev *dev)
1835
{
1836
	return readl(dev->mfc_regs->d_display_status);
1837 1838
}

1839
static int s5p_mfc_get_dec_status_v6(struct s5p_mfc_dev *dev)
1840
{
1841
	return readl(dev->mfc_regs->d_decoded_status);
1842 1843
}

1844
static int s5p_mfc_get_dec_frame_type_v6(struct s5p_mfc_dev *dev)
1845
{
1846
	return readl(dev->mfc_regs->d_decoded_frame_type) &
1847 1848 1849
		S5P_FIMV_DECODE_FRAME_MASK_V6;
}

1850
static int s5p_mfc_get_disp_frame_type_v6(struct s5p_mfc_ctx *ctx)
1851
{
1852
	struct s5p_mfc_dev *dev = ctx->dev;
1853
	return readl(dev->mfc_regs->d_display_frame_type) &
1854 1855 1856
		S5P_FIMV_DECODE_FRAME_MASK_V6;
}

1857
static int s5p_mfc_get_consumed_stream_v6(struct s5p_mfc_dev *dev)
1858
{
1859
	return readl(dev->mfc_regs->d_decoded_nal_size);
1860 1861
}

1862
static int s5p_mfc_get_int_reason_v6(struct s5p_mfc_dev *dev)
1863
{
1864
	return readl(dev->mfc_regs->risc2host_command) &
1865 1866 1867
		S5P_FIMV_RISC2HOST_CMD_MASK;
}

1868
static int s5p_mfc_get_int_err_v6(struct s5p_mfc_dev *dev)
1869
{
1870
	return readl(dev->mfc_regs->error_code);
1871 1872
}

1873
static int s5p_mfc_err_dec_v6(unsigned int err)
1874 1875 1876 1877
{
	return (err & S5P_FIMV_ERR_DEC_MASK_V6) >> S5P_FIMV_ERR_DEC_SHIFT_V6;
}

1878
static int s5p_mfc_get_img_width_v6(struct s5p_mfc_dev *dev)
1879
{
1880
	return readl(dev->mfc_regs->d_display_frame_width);
1881 1882
}

1883
static int s5p_mfc_get_img_height_v6(struct s5p_mfc_dev *dev)
1884
{
1885
	return readl(dev->mfc_regs->d_display_frame_height);
1886 1887
}

1888
static int s5p_mfc_get_dpb_count_v6(struct s5p_mfc_dev *dev)
1889
{
1890
	return readl(dev->mfc_regs->d_min_num_dpb);
1891 1892
}

1893
static int s5p_mfc_get_mv_count_v6(struct s5p_mfc_dev *dev)
1894
{
1895
	return readl(dev->mfc_regs->d_min_num_mv);
1896 1897
}

1898
static int s5p_mfc_get_inst_no_v6(struct s5p_mfc_dev *dev)
1899
{
1900
	return readl(dev->mfc_regs->ret_instance_id);
1901 1902
}

1903
static int s5p_mfc_get_enc_dpb_count_v6(struct s5p_mfc_dev *dev)
1904
{
1905
	return readl(dev->mfc_regs->e_num_dpb);
1906 1907
}

1908
static int s5p_mfc_get_enc_strm_size_v6(struct s5p_mfc_dev *dev)
1909
{
1910
	return readl(dev->mfc_regs->e_stream_size);
1911 1912
}

1913
static int s5p_mfc_get_enc_slice_type_v6(struct s5p_mfc_dev *dev)
1914
{
1915
	return readl(dev->mfc_regs->e_slice_type);
1916 1917
}

1918
static unsigned int s5p_mfc_get_pic_type_top_v6(struct s5p_mfc_ctx *ctx)
1919
{
1920
	return s5p_mfc_read_info_v6(ctx,
1921
		(__force unsigned long) ctx->dev->mfc_regs->d_ret_picture_tag_top);
1922 1923
}

1924
static unsigned int s5p_mfc_get_pic_type_bot_v6(struct s5p_mfc_ctx *ctx)
1925
{
1926
	return s5p_mfc_read_info_v6(ctx,
1927
		(__force unsigned long) ctx->dev->mfc_regs->d_ret_picture_tag_bot);
1928 1929
}

1930
static unsigned int s5p_mfc_get_crop_info_h_v6(struct s5p_mfc_ctx *ctx)
1931
{
1932
	return s5p_mfc_read_info_v6(ctx,
1933
		(__force unsigned long) ctx->dev->mfc_regs->d_display_crop_info1);
1934 1935
}

1936
static unsigned int s5p_mfc_get_crop_info_v_v6(struct s5p_mfc_ctx *ctx)
1937
{
1938
	return s5p_mfc_read_info_v6(ctx,
1939
		(__force unsigned long) ctx->dev->mfc_regs->d_display_crop_info2);
1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095
}

static struct s5p_mfc_regs mfc_regs;

/* Initialize registers for MFC v6 onwards */
const struct s5p_mfc_regs *s5p_mfc_init_regs_v6_plus(struct s5p_mfc_dev *dev)
{
	memset(&mfc_regs, 0, sizeof(mfc_regs));

#define S5P_MFC_REG_ADDR(dev, reg) ((dev)->regs_base + (reg))
#define R(m, r) mfc_regs.m = S5P_MFC_REG_ADDR(dev, r)
	/* codec common registers */
	R(risc_on, S5P_FIMV_RISC_ON_V6);
	R(risc2host_int, S5P_FIMV_RISC2HOST_INT_V6);
	R(host2risc_int, S5P_FIMV_HOST2RISC_INT_V6);
	R(risc_base_address, S5P_FIMV_RISC_BASE_ADDRESS_V6);
	R(mfc_reset, S5P_FIMV_MFC_RESET_V6);
	R(host2risc_command, S5P_FIMV_HOST2RISC_CMD_V6);
	R(risc2host_command, S5P_FIMV_RISC2HOST_CMD_V6);
	R(firmware_version, S5P_FIMV_FW_VERSION_V6);
	R(instance_id, S5P_FIMV_INSTANCE_ID_V6);
	R(codec_type, S5P_FIMV_CODEC_TYPE_V6);
	R(context_mem_addr, S5P_FIMV_CONTEXT_MEM_ADDR_V6);
	R(context_mem_size, S5P_FIMV_CONTEXT_MEM_SIZE_V6);
	R(pixel_format, S5P_FIMV_PIXEL_FORMAT_V6);
	R(ret_instance_id, S5P_FIMV_RET_INSTANCE_ID_V6);
	R(error_code, S5P_FIMV_ERROR_CODE_V6);

	/* decoder registers */
	R(d_crc_ctrl, S5P_FIMV_D_CRC_CTRL_V6);
	R(d_dec_options, S5P_FIMV_D_DEC_OPTIONS_V6);
	R(d_display_delay, S5P_FIMV_D_DISPLAY_DELAY_V6);
	R(d_sei_enable, S5P_FIMV_D_SEI_ENABLE_V6);
	R(d_min_num_dpb, S5P_FIMV_D_MIN_NUM_DPB_V6);
	R(d_min_num_mv, S5P_FIMV_D_MIN_NUM_MV_V6);
	R(d_mvc_num_views, S5P_FIMV_D_MVC_NUM_VIEWS_V6);
	R(d_num_dpb, S5P_FIMV_D_NUM_DPB_V6);
	R(d_num_mv, S5P_FIMV_D_NUM_MV_V6);
	R(d_init_buffer_options, S5P_FIMV_D_INIT_BUFFER_OPTIONS_V6);
	R(d_first_plane_dpb_size, S5P_FIMV_D_LUMA_DPB_SIZE_V6);
	R(d_second_plane_dpb_size, S5P_FIMV_D_CHROMA_DPB_SIZE_V6);
	R(d_mv_buffer_size, S5P_FIMV_D_MV_BUFFER_SIZE_V6);
	R(d_first_plane_dpb, S5P_FIMV_D_LUMA_DPB_V6);
	R(d_second_plane_dpb, S5P_FIMV_D_CHROMA_DPB_V6);
	R(d_mv_buffer, S5P_FIMV_D_MV_BUFFER_V6);
	R(d_scratch_buffer_addr, S5P_FIMV_D_SCRATCH_BUFFER_ADDR_V6);
	R(d_scratch_buffer_size, S5P_FIMV_D_SCRATCH_BUFFER_SIZE_V6);
	R(d_cpb_buffer_addr, S5P_FIMV_D_CPB_BUFFER_ADDR_V6);
	R(d_cpb_buffer_size, S5P_FIMV_D_CPB_BUFFER_SIZE_V6);
	R(d_available_dpb_flag_lower, S5P_FIMV_D_AVAILABLE_DPB_FLAG_LOWER_V6);
	R(d_cpb_buffer_offset, S5P_FIMV_D_CPB_BUFFER_OFFSET_V6);
	R(d_slice_if_enable, S5P_FIMV_D_SLICE_IF_ENABLE_V6);
	R(d_stream_data_size, S5P_FIMV_D_STREAM_DATA_SIZE_V6);
	R(d_display_frame_width, S5P_FIMV_D_DISPLAY_FRAME_WIDTH_V6);
	R(d_display_frame_height, S5P_FIMV_D_DISPLAY_FRAME_HEIGHT_V6);
	R(d_display_status, S5P_FIMV_D_DISPLAY_STATUS_V6);
	R(d_display_first_plane_addr, S5P_FIMV_D_DISPLAY_LUMA_ADDR_V6);
	R(d_display_second_plane_addr, S5P_FIMV_D_DISPLAY_CHROMA_ADDR_V6);
	R(d_display_frame_type, S5P_FIMV_D_DISPLAY_FRAME_TYPE_V6);
	R(d_display_crop_info1, S5P_FIMV_D_DISPLAY_CROP_INFO1_V6);
	R(d_display_crop_info2, S5P_FIMV_D_DISPLAY_CROP_INFO2_V6);
	R(d_display_aspect_ratio, S5P_FIMV_D_DISPLAY_ASPECT_RATIO_V6);
	R(d_display_extended_ar, S5P_FIMV_D_DISPLAY_EXTENDED_AR_V6);
	R(d_decoded_status, S5P_FIMV_D_DECODED_STATUS_V6);
	R(d_decoded_first_plane_addr, S5P_FIMV_D_DECODED_LUMA_ADDR_V6);
	R(d_decoded_second_plane_addr, S5P_FIMV_D_DECODED_CHROMA_ADDR_V6);
	R(d_decoded_frame_type, S5P_FIMV_D_DECODED_FRAME_TYPE_V6);
	R(d_decoded_nal_size, S5P_FIMV_D_DECODED_NAL_SIZE_V6);
	R(d_ret_picture_tag_top, S5P_FIMV_D_RET_PICTURE_TAG_TOP_V6);
	R(d_ret_picture_tag_bot, S5P_FIMV_D_RET_PICTURE_TAG_BOT_V6);
	R(d_h264_info, S5P_FIMV_D_H264_INFO_V6);
	R(d_mvc_view_id, S5P_FIMV_D_MVC_VIEW_ID_V6);
	R(d_frame_pack_sei_avail, S5P_FIMV_D_FRAME_PACK_SEI_AVAIL_V6);

	/* encoder registers */
	R(e_frame_width, S5P_FIMV_E_FRAME_WIDTH_V6);
	R(e_frame_height, S5P_FIMV_E_FRAME_HEIGHT_V6);
	R(e_cropped_frame_width, S5P_FIMV_E_CROPPED_FRAME_WIDTH_V6);
	R(e_cropped_frame_height, S5P_FIMV_E_CROPPED_FRAME_HEIGHT_V6);
	R(e_frame_crop_offset, S5P_FIMV_E_FRAME_CROP_OFFSET_V6);
	R(e_enc_options, S5P_FIMV_E_ENC_OPTIONS_V6);
	R(e_picture_profile, S5P_FIMV_E_PICTURE_PROFILE_V6);
	R(e_vbv_buffer_size, S5P_FIMV_E_VBV_BUFFER_SIZE_V6);
	R(e_vbv_init_delay, S5P_FIMV_E_VBV_INIT_DELAY_V6);
	R(e_fixed_picture_qp, S5P_FIMV_E_FIXED_PICTURE_QP_V6);
	R(e_rc_config, S5P_FIMV_E_RC_CONFIG_V6);
	R(e_rc_qp_bound, S5P_FIMV_E_RC_QP_BOUND_V6);
	R(e_rc_mode, S5P_FIMV_E_RC_RPARAM_V6);
	R(e_mb_rc_config, S5P_FIMV_E_MB_RC_CONFIG_V6);
	R(e_padding_ctrl, S5P_FIMV_E_PADDING_CTRL_V6);
	R(e_mv_hor_range, S5P_FIMV_E_MV_HOR_RANGE_V6);
	R(e_mv_ver_range, S5P_FIMV_E_MV_VER_RANGE_V6);
	R(e_num_dpb, S5P_FIMV_E_NUM_DPB_V6);
	R(e_luma_dpb, S5P_FIMV_E_LUMA_DPB_V6);
	R(e_chroma_dpb, S5P_FIMV_E_CHROMA_DPB_V6);
	R(e_me_buffer, S5P_FIMV_E_ME_BUFFER_V6);
	R(e_scratch_buffer_addr, S5P_FIMV_E_SCRATCH_BUFFER_ADDR_V6);
	R(e_scratch_buffer_size, S5P_FIMV_E_SCRATCH_BUFFER_SIZE_V6);
	R(e_tmv_buffer0, S5P_FIMV_E_TMV_BUFFER0_V6);
	R(e_tmv_buffer1, S5P_FIMV_E_TMV_BUFFER1_V6);
	R(e_source_first_plane_addr, S5P_FIMV_E_SOURCE_LUMA_ADDR_V6);
	R(e_source_second_plane_addr, S5P_FIMV_E_SOURCE_CHROMA_ADDR_V6);
	R(e_stream_buffer_addr, S5P_FIMV_E_STREAM_BUFFER_ADDR_V6);
	R(e_stream_buffer_size, S5P_FIMV_E_STREAM_BUFFER_SIZE_V6);
	R(e_roi_buffer_addr, S5P_FIMV_E_ROI_BUFFER_ADDR_V6);
	R(e_param_change, S5P_FIMV_E_PARAM_CHANGE_V6);
	R(e_ir_size, S5P_FIMV_E_IR_SIZE_V6);
	R(e_gop_config, S5P_FIMV_E_GOP_CONFIG_V6);
	R(e_mslice_mode, S5P_FIMV_E_MSLICE_MODE_V6);
	R(e_mslice_size_mb, S5P_FIMV_E_MSLICE_SIZE_MB_V6);
	R(e_mslice_size_bits, S5P_FIMV_E_MSLICE_SIZE_BITS_V6);
	R(e_frame_insertion, S5P_FIMV_E_FRAME_INSERTION_V6);
	R(e_rc_frame_rate, S5P_FIMV_E_RC_FRAME_RATE_V6);
	R(e_rc_bit_rate, S5P_FIMV_E_RC_BIT_RATE_V6);
	R(e_rc_roi_ctrl, S5P_FIMV_E_RC_ROI_CTRL_V6);
	R(e_picture_tag, S5P_FIMV_E_PICTURE_TAG_V6);
	R(e_bit_count_enable, S5P_FIMV_E_BIT_COUNT_ENABLE_V6);
	R(e_max_bit_count, S5P_FIMV_E_MAX_BIT_COUNT_V6);
	R(e_min_bit_count, S5P_FIMV_E_MIN_BIT_COUNT_V6);
	R(e_metadata_buffer_addr, S5P_FIMV_E_METADATA_BUFFER_ADDR_V6);
	R(e_metadata_buffer_size, S5P_FIMV_E_METADATA_BUFFER_SIZE_V6);
	R(e_encoded_source_first_plane_addr,
			S5P_FIMV_E_ENCODED_SOURCE_LUMA_ADDR_V6);
	R(e_encoded_source_second_plane_addr,
			S5P_FIMV_E_ENCODED_SOURCE_CHROMA_ADDR_V6);
	R(e_stream_size, S5P_FIMV_E_STREAM_SIZE_V6);
	R(e_slice_type, S5P_FIMV_E_SLICE_TYPE_V6);
	R(e_picture_count, S5P_FIMV_E_PICTURE_COUNT_V6);
	R(e_ret_picture_tag, S5P_FIMV_E_RET_PICTURE_TAG_V6);
	R(e_recon_luma_dpb_addr, S5P_FIMV_E_RECON_LUMA_DPB_ADDR_V6);
	R(e_recon_chroma_dpb_addr, S5P_FIMV_E_RECON_CHROMA_DPB_ADDR_V6);
	R(e_mpeg4_options, S5P_FIMV_E_MPEG4_OPTIONS_V6);
	R(e_mpeg4_hec_period, S5P_FIMV_E_MPEG4_HEC_PERIOD_V6);
	R(e_aspect_ratio, S5P_FIMV_E_ASPECT_RATIO_V6);
	R(e_extended_sar, S5P_FIMV_E_EXTENDED_SAR_V6);
	R(e_h264_options, S5P_FIMV_E_H264_OPTIONS_V6);
	R(e_h264_lf_alpha_offset, S5P_FIMV_E_H264_LF_ALPHA_OFFSET_V6);
	R(e_h264_lf_beta_offset, S5P_FIMV_E_H264_LF_BETA_OFFSET_V6);
	R(e_h264_i_period, S5P_FIMV_E_H264_I_PERIOD_V6);
	R(e_h264_fmo_slice_grp_map_type,
			S5P_FIMV_E_H264_FMO_SLICE_GRP_MAP_TYPE_V6);
	R(e_h264_fmo_num_slice_grp_minus1,
			S5P_FIMV_E_H264_FMO_NUM_SLICE_GRP_MINUS1_V6);
	R(e_h264_fmo_slice_grp_change_dir,
			S5P_FIMV_E_H264_FMO_SLICE_GRP_CHANGE_DIR_V6);
	R(e_h264_fmo_slice_grp_change_rate_minus1,
			S5P_FIMV_E_H264_FMO_SLICE_GRP_CHANGE_RATE_MINUS1_V6);
	R(e_h264_fmo_run_length_minus1_0,
			S5P_FIMV_E_H264_FMO_RUN_LENGTH_MINUS1_0_V6);
	R(e_h264_aso_slice_order_0, S5P_FIMV_E_H264_ASO_SLICE_ORDER_0_V6);
	R(e_h264_num_t_layer, S5P_FIMV_E_H264_NUM_T_LAYER_V6);
	R(e_h264_hierarchical_qp_layer0,
			S5P_FIMV_E_H264_HIERARCHICAL_QP_LAYER0_V6);
	R(e_h264_frame_packing_sei_info,
			S5P_FIMV_E_H264_FRAME_PACKING_SEI_INFO_V6);

2096
	if (!IS_MFCV7_PLUS(dev))
2097 2098
		goto done;

2099
	/* Initialize registers used in MFC v7+ */
2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111
	R(e_source_first_plane_addr, S5P_FIMV_E_SOURCE_FIRST_ADDR_V7);
	R(e_source_second_plane_addr, S5P_FIMV_E_SOURCE_SECOND_ADDR_V7);
	R(e_source_third_plane_addr, S5P_FIMV_E_SOURCE_THIRD_ADDR_V7);
	R(e_source_first_plane_stride, S5P_FIMV_E_SOURCE_FIRST_STRIDE_V7);
	R(e_source_second_plane_stride, S5P_FIMV_E_SOURCE_SECOND_STRIDE_V7);
	R(e_source_third_plane_stride, S5P_FIMV_E_SOURCE_THIRD_STRIDE_V7);
	R(e_encoded_source_first_plane_addr,
			S5P_FIMV_E_ENCODED_SOURCE_FIRST_ADDR_V7);
	R(e_encoded_source_second_plane_addr,
			S5P_FIMV_E_ENCODED_SOURCE_SECOND_ADDR_V7);
	R(e_vp8_options, S5P_FIMV_E_VP8_OPTIONS_V7);

2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156
	if (!IS_MFCV8(dev))
		goto done;

	/* Initialize registers used in MFC v8 only.
	 * Also, over-write the registers which have
	 * a different offset for MFC v8. */
	R(d_stream_data_size, S5P_FIMV_D_STREAM_DATA_SIZE_V8);
	R(d_cpb_buffer_addr, S5P_FIMV_D_CPB_BUFFER_ADDR_V8);
	R(d_cpb_buffer_size, S5P_FIMV_D_CPB_BUFFER_SIZE_V8);
	R(d_cpb_buffer_offset, S5P_FIMV_D_CPB_BUFFER_OFFSET_V8);
	R(d_first_plane_dpb_size, S5P_FIMV_D_FIRST_PLANE_DPB_SIZE_V8);
	R(d_second_plane_dpb_size, S5P_FIMV_D_SECOND_PLANE_DPB_SIZE_V8);
	R(d_scratch_buffer_addr, S5P_FIMV_D_SCRATCH_BUFFER_ADDR_V8);
	R(d_scratch_buffer_size, S5P_FIMV_D_SCRATCH_BUFFER_SIZE_V8);
	R(d_first_plane_dpb_stride_size,
			S5P_FIMV_D_FIRST_PLANE_DPB_STRIDE_SIZE_V8);
	R(d_second_plane_dpb_stride_size,
			S5P_FIMV_D_SECOND_PLANE_DPB_STRIDE_SIZE_V8);
	R(d_mv_buffer_size, S5P_FIMV_D_MV_BUFFER_SIZE_V8);
	R(d_num_mv, S5P_FIMV_D_NUM_MV_V8);
	R(d_first_plane_dpb, S5P_FIMV_D_FIRST_PLANE_DPB_V8);
	R(d_second_plane_dpb, S5P_FIMV_D_SECOND_PLANE_DPB_V8);
	R(d_mv_buffer, S5P_FIMV_D_MV_BUFFER_V8);
	R(d_init_buffer_options, S5P_FIMV_D_INIT_BUFFER_OPTIONS_V8);
	R(d_available_dpb_flag_lower, S5P_FIMV_D_AVAILABLE_DPB_FLAG_LOWER_V8);
	R(d_slice_if_enable, S5P_FIMV_D_SLICE_IF_ENABLE_V8);
	R(d_display_first_plane_addr, S5P_FIMV_D_DISPLAY_FIRST_PLANE_ADDR_V8);
	R(d_display_second_plane_addr, S5P_FIMV_D_DISPLAY_SECOND_PLANE_ADDR_V8);
	R(d_decoded_first_plane_addr, S5P_FIMV_D_DECODED_FIRST_PLANE_ADDR_V8);
	R(d_decoded_second_plane_addr, S5P_FIMV_D_DECODED_SECOND_PLANE_ADDR_V8);
	R(d_display_status, S5P_FIMV_D_DISPLAY_STATUS_V8);
	R(d_decoded_status, S5P_FIMV_D_DECODED_STATUS_V8);
	R(d_decoded_frame_type, S5P_FIMV_D_DECODED_FRAME_TYPE_V8);
	R(d_display_frame_type, S5P_FIMV_D_DISPLAY_FRAME_TYPE_V8);
	R(d_decoded_nal_size, S5P_FIMV_D_DECODED_NAL_SIZE_V8);
	R(d_display_frame_width, S5P_FIMV_D_DISPLAY_FRAME_WIDTH_V8);
	R(d_display_frame_height, S5P_FIMV_D_DISPLAY_FRAME_HEIGHT_V8);
	R(d_frame_pack_sei_avail, S5P_FIMV_D_FRAME_PACK_SEI_AVAIL_V8);
	R(d_mvc_num_views, S5P_FIMV_D_MVC_NUM_VIEWS_V8);
	R(d_mvc_view_id, S5P_FIMV_D_MVC_VIEW_ID_V8);
	R(d_ret_picture_tag_top, S5P_FIMV_D_RET_PICTURE_TAG_TOP_V8);
	R(d_ret_picture_tag_bot, S5P_FIMV_D_RET_PICTURE_TAG_BOT_V8);
	R(d_display_crop_info1, S5P_FIMV_D_DISPLAY_CROP_INFO1_V8);
	R(d_display_crop_info2, S5P_FIMV_D_DISPLAY_CROP_INFO2_V8);

2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171
	/* encoder registers */
	R(e_padding_ctrl, S5P_FIMV_E_PADDING_CTRL_V8);
	R(e_rc_config, S5P_FIMV_E_RC_CONFIG_V8);
	R(e_rc_mode, S5P_FIMV_E_RC_RPARAM_V8);
	R(e_mv_hor_range, S5P_FIMV_E_MV_HOR_RANGE_V8);
	R(e_mv_ver_range, S5P_FIMV_E_MV_VER_RANGE_V8);
	R(e_rc_qp_bound, S5P_FIMV_E_RC_QP_BOUND_V8);
	R(e_fixed_picture_qp, S5P_FIMV_E_FIXED_PICTURE_QP_V8);
	R(e_vbv_buffer_size, S5P_FIMV_E_VBV_BUFFER_SIZE_V8);
	R(e_vbv_init_delay, S5P_FIMV_E_VBV_INIT_DELAY_V8);
	R(e_mb_rc_config, S5P_FIMV_E_MB_RC_CONFIG_V8);
	R(e_aspect_ratio, S5P_FIMV_E_ASPECT_RATIO_V8);
	R(e_extended_sar, S5P_FIMV_E_EXTENDED_SAR_V8);
	R(e_h264_options, S5P_FIMV_E_H264_OPTIONS_V8);

2172 2173 2174 2175
done:
	return &mfc_regs;
#undef S5P_MFC_REG_ADDR
#undef R
2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224
}

/* Initialize opr function pointers for MFC v6 */
static struct s5p_mfc_hw_ops s5p_mfc_ops_v6 = {
	.alloc_dec_temp_buffers = s5p_mfc_alloc_dec_temp_buffers_v6,
	.release_dec_desc_buffer = s5p_mfc_release_dec_desc_buffer_v6,
	.alloc_codec_buffers = s5p_mfc_alloc_codec_buffers_v6,
	.release_codec_buffers = s5p_mfc_release_codec_buffers_v6,
	.alloc_instance_buffer = s5p_mfc_alloc_instance_buffer_v6,
	.release_instance_buffer = s5p_mfc_release_instance_buffer_v6,
	.alloc_dev_context_buffer =
		s5p_mfc_alloc_dev_context_buffer_v6,
	.release_dev_context_buffer =
		s5p_mfc_release_dev_context_buffer_v6,
	.dec_calc_dpb_size = s5p_mfc_dec_calc_dpb_size_v6,
	.enc_calc_src_size = s5p_mfc_enc_calc_src_size_v6,
	.set_enc_stream_buffer = s5p_mfc_set_enc_stream_buffer_v6,
	.set_enc_frame_buffer = s5p_mfc_set_enc_frame_buffer_v6,
	.get_enc_frame_buffer = s5p_mfc_get_enc_frame_buffer_v6,
	.try_run = s5p_mfc_try_run_v6,
	.clear_int_flags = s5p_mfc_clear_int_flags_v6,
	.get_dspl_y_adr = s5p_mfc_get_dspl_y_adr_v6,
	.get_dec_y_adr = s5p_mfc_get_dec_y_adr_v6,
	.get_dspl_status = s5p_mfc_get_dspl_status_v6,
	.get_dec_status = s5p_mfc_get_dec_status_v6,
	.get_dec_frame_type = s5p_mfc_get_dec_frame_type_v6,
	.get_disp_frame_type = s5p_mfc_get_disp_frame_type_v6,
	.get_consumed_stream = s5p_mfc_get_consumed_stream_v6,
	.get_int_reason = s5p_mfc_get_int_reason_v6,
	.get_int_err = s5p_mfc_get_int_err_v6,
	.err_dec = s5p_mfc_err_dec_v6,
	.get_img_width = s5p_mfc_get_img_width_v6,
	.get_img_height = s5p_mfc_get_img_height_v6,
	.get_dpb_count = s5p_mfc_get_dpb_count_v6,
	.get_mv_count = s5p_mfc_get_mv_count_v6,
	.get_inst_no = s5p_mfc_get_inst_no_v6,
	.get_enc_strm_size = s5p_mfc_get_enc_strm_size_v6,
	.get_enc_slice_type = s5p_mfc_get_enc_slice_type_v6,
	.get_enc_dpb_count = s5p_mfc_get_enc_dpb_count_v6,
	.get_pic_type_top = s5p_mfc_get_pic_type_top_v6,
	.get_pic_type_bot = s5p_mfc_get_pic_type_bot_v6,
	.get_crop_info_h = s5p_mfc_get_crop_info_h_v6,
	.get_crop_info_v = s5p_mfc_get_crop_info_v_v6,
};

struct s5p_mfc_hw_ops *s5p_mfc_init_hw_ops_v6(void)
{
	return &s5p_mfc_ops_v6;
}