s5p_mfc.c 42.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Samsung S5P Multi Format Codec v 5.1
 *
 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
 * Kamil Debski, <k.debski@samsung.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/videodev2.h>
22
#include <media/v4l2-event.h>
23
#include <linux/workqueue.h>
24
#include <linux/of.h>
25
#include <linux/of_reserved_mem.h>
26
#include <media/videobuf2-v4l2.h>
27
#include "s5p_mfc_common.h"
28 29 30 31 32
#include "s5p_mfc_ctrl.h"
#include "s5p_mfc_debug.h"
#include "s5p_mfc_dec.h"
#include "s5p_mfc_enc.h"
#include "s5p_mfc_intr.h"
33
#include "s5p_mfc_iommu.h"
34 35
#include "s5p_mfc_opr.h"
#include "s5p_mfc_cmd.h"
36 37 38 39 40 41
#include "s5p_mfc_pm.h"

#define S5P_MFC_NAME		"s5p-mfc"
#define S5P_MFC_DEC_NAME	"s5p-mfc-dec"
#define S5P_MFC_ENC_NAME	"s5p-mfc-enc"

42 43
int mfc_debug_level;
module_param_named(debug, mfc_debug_level, int, S_IRUGO | S_IWUSR);
44 45 46
MODULE_PARM_DESC(debug, "Debug level - higher value produces more verbose messages");

/* Helper functions for interrupt processing */
47

48
/* Remove from hw execution round robin */
49
void clear_work_bit(struct s5p_mfc_ctx *ctx)
50 51 52 53
{
	struct s5p_mfc_dev *dev = ctx->dev;

	spin_lock(&dev->condlock);
54
	__clear_bit(ctx->num, &dev->ctx_work_bits);
55 56 57
	spin_unlock(&dev->condlock);
}

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
/* Add to hw execution round robin */
void set_work_bit(struct s5p_mfc_ctx *ctx)
{
	struct s5p_mfc_dev *dev = ctx->dev;

	spin_lock(&dev->condlock);
	__set_bit(ctx->num, &dev->ctx_work_bits);
	spin_unlock(&dev->condlock);
}

/* Remove from hw execution round robin */
void clear_work_bit_irqsave(struct s5p_mfc_ctx *ctx)
{
	struct s5p_mfc_dev *dev = ctx->dev;
	unsigned long flags;

	spin_lock_irqsave(&dev->condlock, flags);
	__clear_bit(ctx->num, &dev->ctx_work_bits);
	spin_unlock_irqrestore(&dev->condlock, flags);
}

/* Add to hw execution round robin */
void set_work_bit_irqsave(struct s5p_mfc_ctx *ctx)
{
	struct s5p_mfc_dev *dev = ctx->dev;
	unsigned long flags;

	spin_lock_irqsave(&dev->condlock, flags);
	__set_bit(ctx->num, &dev->ctx_work_bits);
	spin_unlock_irqrestore(&dev->condlock, flags);
}

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
int s5p_mfc_get_new_ctx(struct s5p_mfc_dev *dev)
{
	unsigned long flags;
	int ctx;

	spin_lock_irqsave(&dev->condlock, flags);
	ctx = dev->curr_ctx;
	do {
		ctx = (ctx + 1) % MFC_NUM_CONTEXTS;
		if (ctx == dev->curr_ctx) {
			if (!test_bit(ctx, &dev->ctx_work_bits))
				ctx = -EAGAIN;
			break;
		}
	} while (!test_bit(ctx, &dev->ctx_work_bits));
	spin_unlock_irqrestore(&dev->condlock, flags);

	return ctx;
}

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
/* Wake up context wait_queue */
static void wake_up_ctx(struct s5p_mfc_ctx *ctx, unsigned int reason,
			unsigned int err)
{
	ctx->int_cond = 1;
	ctx->int_type = reason;
	ctx->int_err = err;
	wake_up(&ctx->queue);
}

/* Wake up device wait_queue */
static void wake_up_dev(struct s5p_mfc_dev *dev, unsigned int reason,
			unsigned int err)
{
	dev->int_cond = 1;
	dev->int_type = reason;
	dev->int_err = err;
	wake_up(&dev->queue);
}

130 131 132 133 134 135 136 137 138 139 140 141 142 143
void s5p_mfc_cleanup_queue(struct list_head *lh, struct vb2_queue *vq)
{
	struct s5p_mfc_buf *b;
	int i;

	while (!list_empty(lh)) {
		b = list_entry(lh->next, struct s5p_mfc_buf, list);
		for (i = 0; i < b->b->vb2_buf.num_planes; i++)
			vb2_set_plane_payload(&b->b->vb2_buf, i, 0);
		vb2_buffer_done(&b->b->vb2_buf, VB2_BUF_STATE_ERROR);
		list_del(&b->list);
	}
}

144
static void s5p_mfc_watchdog(unsigned long arg)
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
{
	struct s5p_mfc_dev *dev = (struct s5p_mfc_dev *)arg;

	if (test_bit(0, &dev->hw_lock))
		atomic_inc(&dev->watchdog_cnt);
	if (atomic_read(&dev->watchdog_cnt) >= MFC_WATCHDOG_CNT) {
		/* This means that hw is busy and no interrupts were
		 * generated by hw for the Nth time of running this
		 * watchdog timer. This usually means a serious hw
		 * error. Now it is time to kill all instances and
		 * reset the MFC. */
		mfc_err("Time out during waiting for HW\n");
		queue_work(dev->watchdog_workqueue, &dev->watchdog_work);
	}
	dev->watchdog_timer.expires = jiffies +
					msecs_to_jiffies(MFC_WATCHDOG_INTERVAL);
	add_timer(&dev->watchdog_timer);
}

static void s5p_mfc_watchdog_worker(struct work_struct *work)
{
	struct s5p_mfc_dev *dev;
	struct s5p_mfc_ctx *ctx;
	unsigned long flags;
	int mutex_locked;
	int i, ret;

	dev = container_of(work, struct s5p_mfc_dev, watchdog_work);

	mfc_err("Driver timeout error handling\n");
	/* Lock the mutex that protects open and release.
	 * This is necessary as they may load and unload firmware. */
	mutex_locked = mutex_trylock(&dev->mfc_mutex);
	if (!mutex_locked)
		mfc_err("Error: some instance may be closing/opening\n");
	spin_lock_irqsave(&dev->irqlock, flags);

	s5p_mfc_clock_off();

	for (i = 0; i < MFC_NUM_CONTEXTS; i++) {
		ctx = dev->ctx[i];
		if (!ctx)
			continue;
		ctx->state = MFCINST_ERROR;
189 190
		s5p_mfc_cleanup_queue(&ctx->dst_queue, &ctx->vq_dst);
		s5p_mfc_cleanup_queue(&ctx->src_queue, &ctx->vq_src);
191
		clear_work_bit(ctx);
192
		wake_up_ctx(ctx, S5P_MFC_R2H_CMD_ERR_RET, 0);
193 194 195
	}
	clear_bit(0, &dev->hw_lock);
	spin_unlock_irqrestore(&dev->irqlock, flags);
196 197 198 199

	/* De-init MFC */
	s5p_mfc_deinit_hw(dev);

200 201 202
	/* Double check if there is at least one instance running.
	 * If no instance is in memory than no firmware should be present */
	if (dev->num_inst > 0) {
203
		ret = s5p_mfc_load_firmware(dev);
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
		if (ret) {
			mfc_err("Failed to reload FW\n");
			goto unlock;
		}
		s5p_mfc_clock_on();
		ret = s5p_mfc_init_hw(dev);
		if (ret)
			mfc_err("Failed to reinit FW\n");
	}
unlock:
	if (mutex_locked)
		mutex_unlock(&dev->mfc_mutex);
}

static void s5p_mfc_handle_frame_all_extracted(struct s5p_mfc_ctx *ctx)
{
	struct s5p_mfc_buf *dst_buf;
221
	struct s5p_mfc_dev *dev = ctx->dev;
222 223 224 225 226 227 228

	ctx->state = MFCINST_FINISHED;
	ctx->sequence++;
	while (!list_empty(&ctx->dst_queue)) {
		dst_buf = list_entry(ctx->dst_queue.next,
				     struct s5p_mfc_buf, list);
		mfc_debug(2, "Cleaning up buffer: %d\n",
229 230 231
					  dst_buf->b->vb2_buf.index);
		vb2_set_plane_payload(&dst_buf->b->vb2_buf, 0, 0);
		vb2_set_plane_payload(&dst_buf->b->vb2_buf, 1, 0);
232
		list_del(&dst_buf->list);
233
		dst_buf->flags |= MFC_BUF_FLAG_EOS;
234
		ctx->dst_queue_cnt--;
235
		dst_buf->b->sequence = (ctx->sequence++);
236

237 238
		if (s5p_mfc_hw_call(dev->mfc_ops, get_pic_type_top, ctx) ==
			s5p_mfc_hw_call(dev->mfc_ops, get_pic_type_bot, ctx))
239
			dst_buf->b->field = V4L2_FIELD_NONE;
240
		else
241 242
			dst_buf->b->field = V4L2_FIELD_INTERLACED;
		dst_buf->b->flags |= V4L2_BUF_FLAG_LAST;
243

244 245
		ctx->dec_dst_flag &= ~(1 << dst_buf->b->vb2_buf.index);
		vb2_buffer_done(&dst_buf->b->vb2_buf, VB2_BUF_STATE_DONE);
246 247 248 249 250 251 252
	}
}

static void s5p_mfc_handle_frame_copy_time(struct s5p_mfc_ctx *ctx)
{
	struct s5p_mfc_dev *dev = ctx->dev;
	struct s5p_mfc_buf  *dst_buf, *src_buf;
253 254 255
	size_t dec_y_addr;
	unsigned int frame_type;

256
	/* Make sure we actually have a new frame before continuing. */
257
	frame_type = s5p_mfc_hw_call(dev->mfc_ops, get_dec_frame_type, dev);
258 259 260
	if (frame_type == S5P_FIMV_DECODE_FRAME_SKIPPED)
		return;
	dec_y_addr = s5p_mfc_hw_call(dev->mfc_ops, get_dec_y_adr, dev);
261 262

	/* Copy timestamp / timecode from decoded src to dst and set
263
	   appropriate flags. */
264 265
	src_buf = list_entry(ctx->src_queue.next, struct s5p_mfc_buf, list);
	list_for_each_entry(dst_buf, &ctx->dst_queue, list) {
266 267 268 269
		if (vb2_dma_contig_plane_dma_addr(&dst_buf->b->vb2_buf, 0)
				== dec_y_addr) {
			dst_buf->b->timecode =
						src_buf->b->timecode;
270 271
			dst_buf->b->vb2_buf.timestamp =
						src_buf->b->vb2_buf.timestamp;
272
			dst_buf->b->flags &=
273
				~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
274 275
			dst_buf->b->flags |=
				src_buf->b->flags
276
				& V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
277 278
			switch (frame_type) {
			case S5P_FIMV_DECODE_FRAME_I_FRAME:
279
				dst_buf->b->flags |=
280 281 282
						V4L2_BUF_FLAG_KEYFRAME;
				break;
			case S5P_FIMV_DECODE_FRAME_P_FRAME:
283
				dst_buf->b->flags |=
284 285 286
						V4L2_BUF_FLAG_PFRAME;
				break;
			case S5P_FIMV_DECODE_FRAME_B_FRAME:
287
				dst_buf->b->flags |=
288 289
						V4L2_BUF_FLAG_BFRAME;
				break;
290 291 292 293 294
			default:
				/* Don't know how to handle
				   S5P_FIMV_DECODE_FRAME_OTHER_FRAME. */
				mfc_debug(2, "Unexpected frame type: %d\n",
						frame_type);
295 296 297 298 299 300 301 302 303 304
			}
			break;
		}
	}
}

static void s5p_mfc_handle_frame_new(struct s5p_mfc_ctx *ctx, unsigned int err)
{
	struct s5p_mfc_dev *dev = ctx->dev;
	struct s5p_mfc_buf  *dst_buf;
305 306
	size_t dspl_y_addr;
	unsigned int frame_type;
307

308
	dspl_y_addr = s5p_mfc_hw_call(dev->mfc_ops, get_dspl_y_adr, dev);
309 310 311 312 313 314
	if (IS_MFCV6_PLUS(dev))
		frame_type = s5p_mfc_hw_call(dev->mfc_ops,
			get_disp_frame_type, ctx);
	else
		frame_type = s5p_mfc_hw_call(dev->mfc_ops,
			get_dec_frame_type, dev);
315

316 317 318 319 320 321 322 323 324 325 326 327
	/* If frame is same as previous then skip and do not dequeue */
	if (frame_type == S5P_FIMV_DECODE_FRAME_SKIPPED) {
		if (!ctx->after_packed_pb)
			ctx->sequence++;
		ctx->after_packed_pb = 0;
		return;
	}
	ctx->sequence++;
	/* The MFC returns address of the buffer, now we have to
	 * check which videobuf does it correspond to */
	list_for_each_entry(dst_buf, &ctx->dst_queue, list) {
		/* Check if this is the buffer we're looking for */
328 329
		if (vb2_dma_contig_plane_dma_addr(&dst_buf->b->vb2_buf, 0)
				== dspl_y_addr) {
330 331
			list_del(&dst_buf->list);
			ctx->dst_queue_cnt--;
332
			dst_buf->b->sequence = ctx->sequence;
333 334 335 336
			if (s5p_mfc_hw_call(dev->mfc_ops,
					get_pic_type_top, ctx) ==
				s5p_mfc_hw_call(dev->mfc_ops,
					get_pic_type_bot, ctx))
337
				dst_buf->b->field = V4L2_FIELD_NONE;
338
			else
339
				dst_buf->b->field =
340
							V4L2_FIELD_INTERLACED;
341 342 343 344 345
			vb2_set_plane_payload(&dst_buf->b->vb2_buf, 0,
						ctx->luma_size);
			vb2_set_plane_payload(&dst_buf->b->vb2_buf, 1,
						ctx->chroma_size);
			clear_bit(dst_buf->b->vb2_buf.index,
346 347
							&ctx->dec_dst_flag);

348 349
			vb2_buffer_done(&dst_buf->b->vb2_buf, err ?
				VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
350 351 352 353 354 355 356 357 358 359 360 361

			break;
		}
	}
}

/* Handle frame decoding interrupt */
static void s5p_mfc_handle_frame(struct s5p_mfc_ctx *ctx,
					unsigned int reason, unsigned int err)
{
	struct s5p_mfc_dev *dev = ctx->dev;
	unsigned int dst_frame_status;
362
	unsigned int dec_frame_status;
363 364 365
	struct s5p_mfc_buf *src_buf;
	unsigned int res_change;

366
	dst_frame_status = s5p_mfc_hw_call(dev->mfc_ops, get_dspl_status, dev)
367
				& S5P_FIMV_DEC_STATUS_DECODING_STATUS_MASK;
368 369
	dec_frame_status = s5p_mfc_hw_call(dev->mfc_ops, get_dec_status, dev)
				& S5P_FIMV_DEC_STATUS_DECODING_STATUS_MASK;
370 371 372
	res_change = (s5p_mfc_hw_call(dev->mfc_ops, get_dspl_status, dev)
				& S5P_FIMV_DEC_STATUS_RESOLUTION_MASK)
				>> S5P_FIMV_DEC_STATUS_RESOLUTION_SHIFT;
373 374 375
	mfc_debug(2, "Frame Status: %x\n", dst_frame_status);
	if (ctx->state == MFCINST_RES_CHANGE_INIT)
		ctx->state = MFCINST_RES_CHANGE_FLUSH;
376 377
	if (res_change == S5P_FIMV_RES_INCREASE ||
		res_change == S5P_FIMV_RES_DECREASE) {
378
		ctx->state = MFCINST_RES_CHANGE_INIT;
379
		s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
380
		wake_up_ctx(ctx, reason, err);
381
		WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
382
		s5p_mfc_clock_off();
383
		s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
384 385 386 387 388 389 390 391
		return;
	}
	if (ctx->dpb_flush_flag)
		ctx->dpb_flush_flag = 0;

	/* All frames remaining in the buffer have been extracted  */
	if (dst_frame_status == S5P_FIMV_DEC_STATUS_DECODING_EMPTY) {
		if (ctx->state == MFCINST_RES_CHANGE_FLUSH) {
392 393 394 395 396 397
			static const struct v4l2_event ev_src_ch = {
				.type = V4L2_EVENT_SOURCE_CHANGE,
				.u.src_change.changes =
					V4L2_EVENT_SRC_CH_RESOLUTION,
			};

398 399
			s5p_mfc_handle_frame_all_extracted(ctx);
			ctx->state = MFCINST_RES_CHANGE_END;
400 401
			v4l2_event_queue_fh(&ctx->fh, &ev_src_ch);

402 403 404 405 406 407
			goto leave_handle_frame;
		} else {
			s5p_mfc_handle_frame_all_extracted(ctx);
		}
	}

408
	if (dec_frame_status == S5P_FIMV_DEC_STATUS_DECODING_DISPLAY)
409 410 411 412 413 414 415 416 417 418 419 420 421 422
		s5p_mfc_handle_frame_copy_time(ctx);

	/* A frame has been decoded and is in the buffer  */
	if (dst_frame_status == S5P_FIMV_DEC_STATUS_DISPLAY_ONLY ||
	    dst_frame_status == S5P_FIMV_DEC_STATUS_DECODING_DISPLAY) {
		s5p_mfc_handle_frame_new(ctx, err);
	} else {
		mfc_debug(2, "No frame decode\n");
	}
	/* Mark source buffer as complete */
	if (dst_frame_status != S5P_FIMV_DEC_STATUS_DISPLAY_ONLY
		&& !list_empty(&ctx->src_queue)) {
		src_buf = list_entry(ctx->src_queue.next, struct s5p_mfc_buf,
								list);
423 424 425
		ctx->consumed_stream += s5p_mfc_hw_call(dev->mfc_ops,
						get_consumed_stream, dev);
		if (ctx->codec_mode != S5P_MFC_CODEC_H264_DEC &&
426
			ctx->codec_mode != S5P_MFC_CODEC_VP8_DEC &&
427
			ctx->consumed_stream + STUFF_BYTE <
428
			src_buf->b->vb2_buf.planes[0].bytesused) {
429 430 431 432 433 434
			/* Run MFC again on the same buffer */
			mfc_debug(2, "Running again the same buffer\n");
			ctx->after_packed_pb = 1;
		} else {
			mfc_debug(2, "MFC needs next buffer\n");
			ctx->consumed_stream = 0;
435 436
			if (src_buf->flags & MFC_BUF_FLAG_EOS)
				ctx->state = MFCINST_FINISHING;
437 438
			list_del(&src_buf->list);
			ctx->src_queue_cnt--;
439
			if (s5p_mfc_hw_call(dev->mfc_ops, err_dec, err) > 0)
440 441
				vb2_buffer_done(&src_buf->b->vb2_buf,
						VB2_BUF_STATE_ERROR);
442
			else
443 444
				vb2_buffer_done(&src_buf->b->vb2_buf,
						VB2_BUF_STATE_DONE);
445 446 447 448
		}
	}
leave_handle_frame:
	if ((ctx->src_queue_cnt == 0 && ctx->state != MFCINST_FINISHING)
449
				    || ctx->dst_queue_cnt < ctx->pb_count)
450
		clear_work_bit(ctx);
451
	s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
452
	wake_up_ctx(ctx, reason, err);
453
	WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
454
	s5p_mfc_clock_off();
455 456 457 458
	/* if suspending, wake up device and do not try_run again*/
	if (test_bit(0, &dev->enter_suspend))
		wake_up_dev(dev, reason, err);
	else
459
		s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
460 461 462
}

/* Error handling for interrupt */
463 464
static void s5p_mfc_handle_error(struct s5p_mfc_dev *dev,
		struct s5p_mfc_ctx *ctx, unsigned int reason, unsigned int err)
465 466 467
{
	mfc_err("Interrupt Error: %08x\n", err);

468 469 470 471 472 473 474 475 476
	if (ctx != NULL) {
		/* Error recovery is dependent on the state of context */
		switch (ctx->state) {
		case MFCINST_RES_CHANGE_INIT:
		case MFCINST_RES_CHANGE_FLUSH:
		case MFCINST_RES_CHANGE_END:
		case MFCINST_FINISHING:
		case MFCINST_FINISHED:
		case MFCINST_RUNNING:
477
			/* It is highly probable that an error occurred
478 479 480 481
			 * while decoding a frame */
			clear_work_bit(ctx);
			ctx->state = MFCINST_ERROR;
			/* Mark all dst buffers as having an error */
482
			s5p_mfc_cleanup_queue(&ctx->dst_queue, &ctx->vq_dst);
483
			/* Mark all src buffers as having an error */
484
			s5p_mfc_cleanup_queue(&ctx->src_queue, &ctx->vq_src);
485 486 487 488 489 490 491 492
			wake_up_ctx(ctx, reason, err);
			break;
		default:
			clear_work_bit(ctx);
			ctx->state = MFCINST_ERROR;
			wake_up_ctx(ctx, reason, err);
			break;
		}
493
	}
494
	WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
495
	s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
496 497
	s5p_mfc_clock_off();
	wake_up_dev(dev, reason, err);
498 499 500 501 502 503 504 505 506
	return;
}

/* Header parsing interrupt handling */
static void s5p_mfc_handle_seq_done(struct s5p_mfc_ctx *ctx,
				 unsigned int reason, unsigned int err)
{
	struct s5p_mfc_dev *dev;

507
	if (ctx == NULL)
508 509 510 511 512 513
		return;
	dev = ctx->dev;
	if (ctx->c_ops->post_seq_start) {
		if (ctx->c_ops->post_seq_start(ctx))
			mfc_err("post_seq_start() failed\n");
	} else {
514 515 516 517
		ctx->img_width = s5p_mfc_hw_call(dev->mfc_ops, get_img_width,
				dev);
		ctx->img_height = s5p_mfc_hw_call(dev->mfc_ops, get_img_height,
				dev);
518

519
		s5p_mfc_hw_call(dev->mfc_ops, dec_calc_dpb_size, ctx);
520

521
		ctx->pb_count = s5p_mfc_hw_call(dev->mfc_ops, get_dpb_count,
522
				dev);
523 524
		ctx->mv_count = s5p_mfc_hw_call(dev->mfc_ops, get_mv_count,
				dev);
525
		if (ctx->img_width == 0 || ctx->img_height == 0)
526 527 528
			ctx->state = MFCINST_ERROR;
		else
			ctx->state = MFCINST_HEAD_PARSED;
529 530 531 532 533 534 535 536 537

		if ((ctx->codec_mode == S5P_MFC_CODEC_H264_DEC ||
			ctx->codec_mode == S5P_MFC_CODEC_H264_MVC_DEC) &&
				!list_empty(&ctx->src_queue)) {
			struct s5p_mfc_buf *src_buf;
			src_buf = list_entry(ctx->src_queue.next,
					struct s5p_mfc_buf, list);
			if (s5p_mfc_hw_call(dev->mfc_ops, get_consumed_stream,
						dev) <
538
					src_buf->b->vb2_buf.planes[0].bytesused)
539 540 541 542 543 544
				ctx->head_processed = 0;
			else
				ctx->head_processed = 1;
		} else {
			ctx->head_processed = 1;
		}
545
	}
546
	s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
547
	clear_work_bit(ctx);
548
	WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
549
	s5p_mfc_clock_off();
550
	s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
551 552 553 554 555 556 557 558 559 560
	wake_up_ctx(ctx, reason, err);
}

/* Header parsing interrupt handling */
static void s5p_mfc_handle_init_buffers(struct s5p_mfc_ctx *ctx,
				 unsigned int reason, unsigned int err)
{
	struct s5p_mfc_buf *src_buf;
	struct s5p_mfc_dev *dev;

561
	if (ctx == NULL)
562 563
		return;
	dev = ctx->dev;
564
	s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
565 566 567
	ctx->int_type = reason;
	ctx->int_err = err;
	ctx->int_cond = 1;
568
	clear_work_bit(ctx);
569 570
	if (err == 0) {
		ctx->state = MFCINST_RUNNING;
571
		if (!ctx->dpb_flush_flag && ctx->head_processed) {
572 573 574 575 576
			if (!list_empty(&ctx->src_queue)) {
				src_buf = list_entry(ctx->src_queue.next,
					     struct s5p_mfc_buf, list);
				list_del(&src_buf->list);
				ctx->src_queue_cnt--;
577
				vb2_buffer_done(&src_buf->b->vb2_buf,
578 579 580 581 582
						VB2_BUF_STATE_DONE);
			}
		} else {
			ctx->dpb_flush_flag = 0;
		}
583
		WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
584 585 586 587

		s5p_mfc_clock_off();

		wake_up(&ctx->queue);
588
		s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
589
	} else {
590
		WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
591 592 593 594 595 596 597

		s5p_mfc_clock_off();

		wake_up(&ctx->queue);
	}
}

598
static void s5p_mfc_handle_stream_complete(struct s5p_mfc_ctx *ctx)
599 600 601 602
{
	struct s5p_mfc_dev *dev = ctx->dev;
	struct s5p_mfc_buf *mb_entry;

603
	mfc_debug(2, "Stream completed\n");
604 605 606 607 608 609 610 611

	ctx->state = MFCINST_FINISHED;

	if (!list_empty(&ctx->dst_queue)) {
		mb_entry = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf,
									list);
		list_del(&mb_entry->list);
		ctx->dst_queue_cnt--;
612 613
		vb2_set_plane_payload(&mb_entry->b->vb2_buf, 0, 0);
		vb2_buffer_done(&mb_entry->b->vb2_buf, VB2_BUF_STATE_DONE);
614 615 616 617
	}

	clear_work_bit(ctx);

618
	WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
619 620 621

	s5p_mfc_clock_off();
	wake_up(&ctx->queue);
622
	s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
623 624
}

625 626 627 628 629 630 631 632 633 634 635
/* Interrupt processing */
static irqreturn_t s5p_mfc_irq(int irq, void *priv)
{
	struct s5p_mfc_dev *dev = priv;
	struct s5p_mfc_ctx *ctx;
	unsigned int reason;
	unsigned int err;

	mfc_debug_enter();
	/* Reset the timeout watchdog */
	atomic_set(&dev->watchdog_cnt, 0);
636
	spin_lock(&dev->irqlock);
637 638
	ctx = dev->ctx[dev->curr_ctx];
	/* Get the reason of interrupt and the error code */
639 640
	reason = s5p_mfc_hw_call(dev->mfc_ops, get_int_reason, dev);
	err = s5p_mfc_hw_call(dev->mfc_ops, get_int_err, dev);
641 642
	mfc_debug(1, "Int reason: %d (err: %08x)\n", reason, err);
	switch (reason) {
643
	case S5P_MFC_R2H_CMD_ERR_RET:
644
		/* An error has occurred */
645
		if (ctx->state == MFCINST_RUNNING &&
646 647
			s5p_mfc_hw_call(dev->mfc_ops, err_dec, err) >=
				dev->warn_start)
648 649
			s5p_mfc_handle_frame(ctx, reason, err);
		else
650
			s5p_mfc_handle_error(dev, ctx, reason, err);
651 652 653
		clear_bit(0, &dev->enter_suspend);
		break;

654 655 656
	case S5P_MFC_R2H_CMD_SLICE_DONE_RET:
	case S5P_MFC_R2H_CMD_FIELD_DONE_RET:
	case S5P_MFC_R2H_CMD_FRAME_DONE_RET:
657 658 659
		if (ctx->c_ops->post_frame_start) {
			if (ctx->c_ops->post_frame_start(ctx))
				mfc_err("post_frame_start() failed\n");
660 661 662

			if (ctx->state == MFCINST_FINISHING &&
						list_empty(&ctx->ref_queue)) {
663
				s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
664 665 666
				s5p_mfc_handle_stream_complete(ctx);
				break;
			}
667
			s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
668
			wake_up_ctx(ctx, reason, err);
669
			WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
670
			s5p_mfc_clock_off();
671
			s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
672 673 674 675 676
		} else {
			s5p_mfc_handle_frame(ctx, reason, err);
		}
		break;

677
	case S5P_MFC_R2H_CMD_SEQ_DONE_RET:
678 679 680
		s5p_mfc_handle_seq_done(ctx, reason, err);
		break;

681 682
	case S5P_MFC_R2H_CMD_OPEN_INSTANCE_RET:
		ctx->inst_no = s5p_mfc_hw_call(dev->mfc_ops, get_inst_no, dev);
683 684 685 686 687
		ctx->state = MFCINST_GOT_INST;
		clear_work_bit(ctx);
		wake_up(&ctx->queue);
		goto irq_cleanup_hw;

688
	case S5P_MFC_R2H_CMD_CLOSE_INSTANCE_RET:
689
		clear_work_bit(ctx);
690
		ctx->inst_no = MFC_NO_INSTANCE_SET;
691 692 693 694
		ctx->state = MFCINST_FREE;
		wake_up(&ctx->queue);
		goto irq_cleanup_hw;

695 696 697 698
	case S5P_MFC_R2H_CMD_SYS_INIT_RET:
	case S5P_MFC_R2H_CMD_FW_STATUS_RET:
	case S5P_MFC_R2H_CMD_SLEEP_RET:
	case S5P_MFC_R2H_CMD_WAKEUP_RET:
699 700
		if (ctx)
			clear_work_bit(ctx);
701
		s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
702 703 704 705 706
		wake_up_dev(dev, reason, err);
		clear_bit(0, &dev->hw_lock);
		clear_bit(0, &dev->enter_suspend);
		break;

707
	case S5P_MFC_R2H_CMD_INIT_BUFFERS_RET:
708 709
		s5p_mfc_handle_init_buffers(ctx, reason, err);
		break;
710

711
	case S5P_MFC_R2H_CMD_COMPLETE_SEQ_RET:
712
		s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
713 714 715
		ctx->int_type = reason;
		ctx->int_err = err;
		s5p_mfc_handle_stream_complete(ctx);
716 717
		break;

718 719 720 721 722 723
	case S5P_MFC_R2H_CMD_DPB_FLUSH_RET:
		clear_work_bit(ctx);
		ctx->state = MFCINST_RUNNING;
		wake_up(&ctx->queue);
		goto irq_cleanup_hw;

724 725
	default:
		mfc_debug(2, "Unknown int reason\n");
726
		s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
727
	}
728
	spin_unlock(&dev->irqlock);
729 730 731
	mfc_debug_leave();
	return IRQ_HANDLED;
irq_cleanup_hw:
732
	s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
733 734 735 736 737 738 739 740
	ctx->int_type = reason;
	ctx->int_err = err;
	ctx->int_cond = 1;
	if (test_and_clear_bit(0, &dev->hw_lock) == 0)
		mfc_err("Failed to unlock hw\n");

	s5p_mfc_clock_off();

741
	s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
742
	spin_unlock(&dev->irqlock);
743 744 745 746 747 748 749
	mfc_debug(2, "Exit via irq_cleanup_hw\n");
	return IRQ_HANDLED;
}

/* Open an MFC node */
static int s5p_mfc_open(struct file *file)
{
750
	struct video_device *vdev = video_devdata(file);
751 752 753 754 755 756
	struct s5p_mfc_dev *dev = video_drvdata(file);
	struct s5p_mfc_ctx *ctx = NULL;
	struct vb2_queue *q;
	int ret = 0;

	mfc_debug_enter();
757 758
	if (mutex_lock_interruptible(&dev->mfc_mutex))
		return -ERESTARTSYS;
759 760
	dev->num_inst++;	/* It is guarded by mfc_mutex in vfd */
	/* Allocate memory for context */
761
	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
762 763 764 765 766
	if (!ctx) {
		mfc_err("Not enough memory\n");
		ret = -ENOMEM;
		goto err_alloc;
	}
767
	v4l2_fh_init(&ctx->fh, vdev);
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785
	file->private_data = &ctx->fh;
	v4l2_fh_add(&ctx->fh);
	ctx->dev = dev;
	INIT_LIST_HEAD(&ctx->src_queue);
	INIT_LIST_HEAD(&ctx->dst_queue);
	ctx->src_queue_cnt = 0;
	ctx->dst_queue_cnt = 0;
	/* Get context number */
	ctx->num = 0;
	while (dev->ctx[ctx->num]) {
		ctx->num++;
		if (ctx->num >= MFC_NUM_CONTEXTS) {
			mfc_err("Too many open contexts\n");
			ret = -EBUSY;
			goto err_no_ctx;
		}
	}
	/* Mark context as idle */
786
	clear_work_bit_irqsave(ctx);
787
	dev->ctx[ctx->num] = ctx;
788
	if (vdev == dev->vfd_dec) {
789 790
		ctx->type = MFCINST_DECODER;
		ctx->c_ops = get_dec_codec_ops();
791
		s5p_mfc_dec_init(ctx);
792 793 794 795 796 797
		/* Setup ctrl handler */
		ret = s5p_mfc_dec_ctrls_setup(ctx);
		if (ret) {
			mfc_err("Failed to setup mfc controls\n");
			goto err_ctrls_setup;
		}
798
	} else if (vdev == dev->vfd_enc) {
799 800 801 802 803
		ctx->type = MFCINST_ENCODER;
		ctx->c_ops = get_enc_codec_ops();
		/* only for encoder */
		INIT_LIST_HEAD(&ctx->ref_queue);
		ctx->ref_queue_cnt = 0;
804
		s5p_mfc_enc_init(ctx);
805 806 807 808 809 810 811 812 813 814 815
		/* Setup ctrl handler */
		ret = s5p_mfc_enc_ctrls_setup(ctx);
		if (ret) {
			mfc_err("Failed to setup mfc controls\n");
			goto err_ctrls_setup;
		}
	} else {
		ret = -ENOENT;
		goto err_bad_node;
	}
	ctx->fh.ctrl_handler = &ctx->ctrl_handler;
816
	ctx->inst_no = MFC_NO_INSTANCE_SET;
817 818 819 820 821 822 823 824 825 826 827
	/* Load firmware if this is the first instance */
	if (dev->num_inst == 1) {
		dev->watchdog_timer.expires = jiffies +
					msecs_to_jiffies(MFC_WATCHDOG_INTERVAL);
		add_timer(&dev->watchdog_timer);
		ret = s5p_mfc_power_on();
		if (ret < 0) {
			mfc_err("power on failed\n");
			goto err_pwr_enable;
		}
		s5p_mfc_clock_on();
828 829 830 831 832
		ret = s5p_mfc_load_firmware(dev);
		if (ret) {
			s5p_mfc_clock_off();
			goto err_load_fw;
		}
833 834
		/* Init the FW */
		ret = s5p_mfc_init_hw(dev);
835
		s5p_mfc_clock_off();
836 837 838 839 840 841 842
		if (ret)
			goto err_init_hw;
	}
	/* Init videobuf2 queue for CAPTURE */
	q = &ctx->vq_dst;
	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
	q->drv_priv = &ctx->fh;
843
	q->lock = &dev->mfc_mutex;
844
	if (vdev == dev->vfd_dec) {
845 846
		q->io_modes = VB2_MMAP;
		q->ops = get_dec_queue_ops();
847
	} else if (vdev == dev->vfd_enc) {
848 849 850 851 852 853
		q->io_modes = VB2_MMAP | VB2_USERPTR;
		q->ops = get_enc_queue_ops();
	} else {
		ret = -ENOENT;
		goto err_queue_init;
	}
854
	q->mem_ops = &vb2_dma_contig_memops;
855
	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
856 857 858 859 860 861 862 863 864 865
	ret = vb2_queue_init(q);
	if (ret) {
		mfc_err("Failed to initialize videobuf2 queue(capture)\n");
		goto err_queue_init;
	}
	/* Init videobuf2 queue for OUTPUT */
	q = &ctx->vq_src;
	q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
	q->io_modes = VB2_MMAP;
	q->drv_priv = &ctx->fh;
866
	q->lock = &dev->mfc_mutex;
867
	if (vdev == dev->vfd_dec) {
868 869
		q->io_modes = VB2_MMAP;
		q->ops = get_dec_queue_ops();
870
	} else if (vdev == dev->vfd_enc) {
871 872 873 874 875 876
		q->io_modes = VB2_MMAP | VB2_USERPTR;
		q->ops = get_enc_queue_ops();
	} else {
		ret = -ENOENT;
		goto err_queue_init;
	}
877 878 879 880 881 882 883
	/* One way to indicate end-of-stream for MFC is to set the
	 * bytesused == 0. However by default videobuf2 handles bytesused
	 * equal to 0 as a special case and changes its value to the size
	 * of the buffer. Set the allow_zero_bytesused flag so that videobuf2
	 * will keep the value of bytesused intact.
	 */
	q->allow_zero_bytesused = 1;
884
	q->mem_ops = &vb2_dma_contig_memops;
885
	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
886 887 888 889 890 891
	ret = vb2_queue_init(q);
	if (ret) {
		mfc_err("Failed to initialize videobuf2 queue(output)\n");
		goto err_queue_init;
	}
	init_waitqueue_head(&ctx->queue);
892
	mutex_unlock(&dev->mfc_mutex);
893 894
	mfc_debug_leave();
	return ret;
895
	/* Deinit when failure occurred */
896
err_queue_init:
897 898
	if (dev->num_inst == 1)
		s5p_mfc_deinit_hw(dev);
899
err_init_hw:
900
err_load_fw:
901 902 903 904
err_pwr_enable:
	if (dev->num_inst == 1) {
		if (s5p_mfc_power_off() < 0)
			mfc_err("power off failed\n");
905
		del_timer_sync(&dev->watchdog_timer);
906 907 908 909
	}
err_ctrls_setup:
	s5p_mfc_dec_ctrls_delete(ctx);
err_bad_node:
910
	dev->ctx[ctx->num] = NULL;
911 912 913 914 915 916
err_no_ctx:
	v4l2_fh_del(&ctx->fh);
	v4l2_fh_exit(&ctx->fh);
	kfree(ctx);
err_alloc:
	dev->num_inst--;
917
	mutex_unlock(&dev->mfc_mutex);
918 919 920 921 922 923 924 925 926 927 928
	mfc_debug_leave();
	return ret;
}

/* Release MFC context */
static int s5p_mfc_release(struct file *file)
{
	struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
	struct s5p_mfc_dev *dev = ctx->dev;

	mfc_debug_enter();
929
	mutex_lock(&dev->mfc_mutex);
930 931 932 933
	s5p_mfc_clock_on();
	vb2_queue_release(&ctx->vq_src);
	vb2_queue_release(&ctx->vq_dst);
	/* Mark context as idle */
934
	clear_work_bit_irqsave(ctx);
935
	/* If instance was initialised and not yet freed,
936
	 * return instance and free resources */
937
	if (ctx->state != MFCINST_FREE && ctx->state != MFCINST_INIT) {
938
		mfc_debug(2, "Has to free instance\n");
939
		s5p_mfc_close_mfc_inst(dev, ctx);
940 941 942 943 944 945
	}
	/* hardware locking scheme */
	if (dev->curr_ctx == ctx->num)
		clear_bit(0, &dev->hw_lock);
	dev->num_inst--;
	if (dev->num_inst == 0) {
946
		mfc_debug(2, "Last instance\n");
947
		s5p_mfc_deinit_hw(dev);
948 949 950 951 952 953
		del_timer_sync(&dev->watchdog_timer);
		if (s5p_mfc_power_off() < 0)
			mfc_err("Power off failed\n");
	}
	mfc_debug(2, "Shutting down clock\n");
	s5p_mfc_clock_off();
954
	dev->ctx[ctx->num] = NULL;
955 956 957 958 959
	s5p_mfc_dec_ctrls_delete(ctx);
	v4l2_fh_del(&ctx->fh);
	v4l2_fh_exit(&ctx->fh);
	kfree(ctx);
	mfc_debug_leave();
960
	mutex_unlock(&dev->mfc_mutex);
961 962 963 964 965 966 967 968 969 970 971 972 973 974
	return 0;
}

/* Poll */
static unsigned int s5p_mfc_poll(struct file *file,
				 struct poll_table_struct *wait)
{
	struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
	struct s5p_mfc_dev *dev = ctx->dev;
	struct vb2_queue *src_q, *dst_q;
	struct vb2_buffer *src_vb = NULL, *dst_vb = NULL;
	unsigned int rc = 0;
	unsigned long flags;

975
	mutex_lock(&dev->mfc_mutex);
976 977 978 979 980 981 982 983 984 985 986 987 988
	src_q = &ctx->vq_src;
	dst_q = &ctx->vq_dst;
	/*
	 * There has to be at least one buffer queued on each queued_list, which
	 * means either in driver already or waiting for driver to claim it
	 * and start processing.
	 */
	if ((!src_q->streaming || list_empty(&src_q->queued_list))
		&& (!dst_q->streaming || list_empty(&dst_q->queued_list))) {
		rc = POLLERR;
		goto end;
	}
	mutex_unlock(&dev->mfc_mutex);
989
	poll_wait(file, &ctx->fh.wait, wait);
990 991 992
	poll_wait(file, &src_q->done_wq, wait);
	poll_wait(file, &dst_q->done_wq, wait);
	mutex_lock(&dev->mfc_mutex);
993 994
	if (v4l2_event_pending(&ctx->fh))
		rc |= POLLPRI;
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
	spin_lock_irqsave(&src_q->done_lock, flags);
	if (!list_empty(&src_q->done_list))
		src_vb = list_first_entry(&src_q->done_list, struct vb2_buffer,
								done_entry);
	if (src_vb && (src_vb->state == VB2_BUF_STATE_DONE
				|| src_vb->state == VB2_BUF_STATE_ERROR))
		rc |= POLLOUT | POLLWRNORM;
	spin_unlock_irqrestore(&src_q->done_lock, flags);
	spin_lock_irqsave(&dst_q->done_lock, flags);
	if (!list_empty(&dst_q->done_list))
		dst_vb = list_first_entry(&dst_q->done_list, struct vb2_buffer,
								done_entry);
	if (dst_vb && (dst_vb->state == VB2_BUF_STATE_DONE
				|| dst_vb->state == VB2_BUF_STATE_ERROR))
		rc |= POLLIN | POLLRDNORM;
	spin_unlock_irqrestore(&dst_q->done_lock, flags);
end:
1012
	mutex_unlock(&dev->mfc_mutex);
1013 1014 1015 1016 1017 1018 1019
	return rc;
}

/* Mmap */
static int s5p_mfc_mmap(struct file *file, struct vm_area_struct *vma)
{
	struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
1020
	struct s5p_mfc_dev *dev = ctx->dev;
1021 1022
	unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
	int ret;
1023 1024 1025

	if (mutex_lock_interruptible(&dev->mfc_mutex))
		return -ERESTARTSYS;
1026 1027 1028 1029 1030 1031 1032 1033
	if (offset < DST_QUEUE_OFF_BASE) {
		mfc_debug(2, "mmaping source\n");
		ret = vb2_mmap(&ctx->vq_src, vma);
	} else {		/* capture */
		mfc_debug(2, "mmaping destination\n");
		vma->vm_pgoff -= (DST_QUEUE_OFF_BASE >> PAGE_SHIFT);
		ret = vb2_mmap(&ctx->vq_dst, vma);
	}
1034
	mutex_unlock(&dev->mfc_mutex);
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
	return ret;
}

/* v4l2 ops */
static const struct v4l2_file_operations s5p_mfc_fops = {
	.owner = THIS_MODULE,
	.open = s5p_mfc_open,
	.release = s5p_mfc_release,
	.poll = s5p_mfc_poll,
	.unlocked_ioctl = video_ioctl2,
	.mmap = s5p_mfc_mmap,
};

1048
/* DMA memory related helper functions */
1049 1050
static void s5p_mfc_memdev_release(struct device *dev)
{
1051
	of_reserved_mem_device_release(dev);
1052 1053
}

1054 1055
static struct device *s5p_mfc_alloc_memdev(struct device *dev,
					   const char *name, unsigned int idx)
1056
{
1057 1058
	struct device *child;
	int ret;
1059

1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
	child = devm_kzalloc(dev, sizeof(struct device), GFP_KERNEL);
	if (!child)
		return NULL;

	device_initialize(child);
	dev_set_name(child, "%s:%s", dev_name(dev), name);
	child->parent = dev;
	child->bus = dev->bus;
	child->coherent_dma_mask = dev->coherent_dma_mask;
	child->dma_mask = dev->dma_mask;
	child->release = s5p_mfc_memdev_release;

	if (device_add(child) == 0) {
		ret = of_reserved_mem_device_init_by_idx(child, dev->of_node,
							 idx);
		if (ret == 0)
			return child;
1077
	}
1078

1079 1080 1081
	put_device(child);
	return NULL;
}
1082

1083 1084 1085
static int s5p_mfc_configure_dma_memory(struct s5p_mfc_dev *mfc_dev)
{
	struct device *dev = &mfc_dev->plat_dev->dev;
1086

1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
	/*
	 * When IOMMU is available, we cannot use the default configuration,
	 * because of MFC firmware requirements: address space limited to
	 * 256M and non-zero default start address.
	 * This is still simplified, not optimal configuration, but for now
	 * IOMMU core doesn't allow to configure device's IOMMUs channel
	 * separately.
	 */
	if (exynos_is_iommu_available(dev)) {
		int ret = exynos_configure_iommu(dev, S5P_MFC_IOMMU_DMA_BASE,
						 S5P_MFC_IOMMU_DMA_SIZE);
		if (ret == 0)
			mfc_dev->mem_dev_l = mfc_dev->mem_dev_r = dev;
		return ret;
	}

1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
	/*
	 * Create and initialize virtual devices for accessing
	 * reserved memory regions.
	 */
	mfc_dev->mem_dev_l = s5p_mfc_alloc_memdev(dev, "left",
						  MFC_BANK1_ALLOC_CTX);
	if (!mfc_dev->mem_dev_l)
		return -ENODEV;
	mfc_dev->mem_dev_r = s5p_mfc_alloc_memdev(dev, "right",
						  MFC_BANK2_ALLOC_CTX);
	if (!mfc_dev->mem_dev_r) {
		device_unregister(mfc_dev->mem_dev_l);
		return -ENODEV;
1116
	}
1117

1118 1119 1120
	return 0;
}

1121 1122
static void s5p_mfc_unconfigure_dma_memory(struct s5p_mfc_dev *mfc_dev)
{
1123 1124 1125 1126 1127 1128 1129
	struct device *dev = &mfc_dev->plat_dev->dev;

	if (exynos_is_iommu_available(dev)) {
		exynos_unconfigure_iommu(dev);
		return;
	}

1130 1131 1132 1133 1134 1135
	device_unregister(mfc_dev->mem_dev_l);
	device_unregister(mfc_dev->mem_dev_r);
}

static void *mfc_get_drv_data(struct platform_device *pdev);

1136
/* MFC probe function */
1137
static int s5p_mfc_probe(struct platform_device *pdev)
1138 1139 1140 1141 1142 1143 1144
{
	struct s5p_mfc_dev *dev;
	struct video_device *vfd;
	struct resource *res;
	int ret;

	pr_debug("%s++\n", __func__);
1145
	dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
	if (!dev) {
		dev_err(&pdev->dev, "Not enough memory for MFC device\n");
		return -ENOMEM;
	}

	spin_lock_init(&dev->irqlock);
	spin_lock_init(&dev->condlock);
	dev->plat_dev = pdev;
	if (!dev->plat_dev) {
		dev_err(&pdev->dev, "No platform data specified\n");
1156
		return -ENODEV;
1157 1158
	}

1159
	dev->variant = mfc_get_drv_data(pdev);
1160

1161
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1162 1163 1164 1165
	if (res == NULL) {
		dev_err(&pdev->dev, "failed to get io resource\n");
		return -ENOENT;
	}
1166 1167 1168
	dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(dev->regs_base))
		return PTR_ERR(dev->regs_base);
1169 1170 1171 1172

	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
	if (res == NULL) {
		dev_err(&pdev->dev, "failed to get irq resource\n");
1173
		return -ENOENT;
1174 1175
	}
	dev->irq = res->start;
1176
	ret = devm_request_irq(&pdev->dev, dev->irq, s5p_mfc_irq,
1177
					0, pdev->name, dev);
1178 1179
	if (ret) {
		dev_err(&pdev->dev, "Failed to install irq (%d)\n", ret);
1180
		return ret;
1181 1182
	}

1183 1184 1185 1186 1187 1188 1189 1190 1191
	ret = s5p_mfc_configure_dma_memory(dev);
	if (ret < 0) {
		dev_err(&pdev->dev, "failed to configure DMA memory\n");
		return ret;
	}

	ret = s5p_mfc_init_pm(dev);
	if (ret < 0) {
		dev_err(&pdev->dev, "failed to get mfc clock source\n");
1192
		goto err_dma;
1193 1194
	}

1195
	vb2_dma_contig_set_max_seg_size(dev->mem_dev_l, DMA_BIT_MASK(32));
1196
	dev->alloc_ctx[0] = vb2_dma_contig_init_ctx(dev->mem_dev_l);
1197
	if (IS_ERR(dev->alloc_ctx[0])) {
1198
		ret = PTR_ERR(dev->alloc_ctx[0]);
1199
		goto err_res;
1200
	}
1201
	vb2_dma_contig_set_max_seg_size(dev->mem_dev_r, DMA_BIT_MASK(32));
1202
	dev->alloc_ctx[1] = vb2_dma_contig_init_ctx(dev->mem_dev_r);
1203
	if (IS_ERR(dev->alloc_ctx[1])) {
1204 1205 1206 1207 1208 1209
		ret = PTR_ERR(dev->alloc_ctx[1]);
		goto err_mem_init_ctx_1;
	}

	mutex_init(&dev->mfc_mutex);

1210 1211 1212 1213
	ret = s5p_mfc_alloc_firmware(dev);
	if (ret)
		goto err_alloc_fw;

1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
	ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
	if (ret)
		goto err_v4l2_dev_reg;
	init_waitqueue_head(&dev->queue);

	/* decoder */
	vfd = video_device_alloc();
	if (!vfd) {
		v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
		ret = -ENOMEM;
		goto err_dec_alloc;
	}
1226
	vfd->fops	= &s5p_mfc_fops;
1227
	vfd->ioctl_ops	= get_dec_v4l2_ioctl_ops();
1228
	vfd->release	= video_device_release;
1229 1230
	vfd->lock	= &dev->mfc_mutex;
	vfd->v4l2_dev	= &dev->v4l2_dev;
1231
	vfd->vfl_dir	= VFL_DIR_M2M;
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
	snprintf(vfd->name, sizeof(vfd->name), "%s", S5P_MFC_DEC_NAME);
	dev->vfd_dec	= vfd;
	video_set_drvdata(vfd, dev);

	/* encoder */
	vfd = video_device_alloc();
	if (!vfd) {
		v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
		ret = -ENOMEM;
		goto err_enc_alloc;
	}
1243
	vfd->fops	= &s5p_mfc_fops;
1244
	vfd->ioctl_ops	= get_enc_v4l2_ioctl_ops();
1245
	vfd->release	= video_device_release;
1246 1247
	vfd->lock	= &dev->mfc_mutex;
	vfd->v4l2_dev	= &dev->v4l2_dev;
1248
	vfd->vfl_dir	= VFL_DIR_M2M;
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
	snprintf(vfd->name, sizeof(vfd->name), "%s", S5P_MFC_ENC_NAME);
	dev->vfd_enc	= vfd;
	video_set_drvdata(vfd, dev);
	platform_set_drvdata(pdev, dev);

	dev->hw_lock = 0;
	dev->watchdog_workqueue = create_singlethread_workqueue(S5P_MFC_NAME);
	INIT_WORK(&dev->watchdog_work, s5p_mfc_watchdog_worker);
	atomic_set(&dev->watchdog_cnt, 0);
	init_timer(&dev->watchdog_timer);
	dev->watchdog_timer.data = (unsigned long)dev;
	dev->watchdog_timer.function = s5p_mfc_watchdog;

1262 1263 1264
	/* Initialize HW ops and commands based on MFC version */
	s5p_mfc_init_hw_ops(dev);
	s5p_mfc_init_hw_cmds(dev);
1265
	s5p_mfc_init_regs(dev);
1266

1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283
	/* Register decoder and encoder */
	ret = video_register_device(dev->vfd_dec, VFL_TYPE_GRABBER, 0);
	if (ret) {
		v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
		goto err_dec_reg;
	}
	v4l2_info(&dev->v4l2_dev,
		  "decoder registered as /dev/video%d\n", dev->vfd_dec->num);

	ret = video_register_device(dev->vfd_enc, VFL_TYPE_GRABBER, 0);
	if (ret) {
		v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
		goto err_enc_reg;
	}
	v4l2_info(&dev->v4l2_dev,
		  "encoder registered as /dev/video%d\n", dev->vfd_enc->num);

1284 1285 1286 1287 1288 1289 1290
	pr_debug("%s--\n", __func__);
	return 0;

/* Deinit MFC if probe had failed */
err_enc_reg:
	video_unregister_device(dev->vfd_dec);
err_dec_reg:
1291 1292
	video_device_release(dev->vfd_enc);
err_enc_alloc:
1293 1294 1295 1296
	video_device_release(dev->vfd_dec);
err_dec_alloc:
	v4l2_device_unregister(&dev->v4l2_dev);
err_v4l2_dev_reg:
1297 1298
	s5p_mfc_release_firmware(dev);
err_alloc_fw:
1299 1300 1301 1302 1303
	vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[1]);
err_mem_init_ctx_1:
	vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[0]);
err_res:
	s5p_mfc_final_pm(dev);
1304 1305
err_dma:
	s5p_mfc_unconfigure_dma_memory(dev);
1306

1307 1308 1309 1310 1311 1312
	pr_debug("%s-- with error\n", __func__);
	return ret;

}

/* Remove the driver */
1313
static int s5p_mfc_remove(struct platform_device *pdev)
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324
{
	struct s5p_mfc_dev *dev = platform_get_drvdata(pdev);

	v4l2_info(&dev->v4l2_dev, "Removing %s\n", pdev->name);

	del_timer_sync(&dev->watchdog_timer);
	flush_workqueue(dev->watchdog_workqueue);
	destroy_workqueue(dev->watchdog_workqueue);

	video_unregister_device(dev->vfd_enc);
	video_unregister_device(dev->vfd_dec);
1325 1326
	video_device_release(dev->vfd_enc);
	video_device_release(dev->vfd_dec);
1327
	v4l2_device_unregister(&dev->v4l2_dev);
1328
	s5p_mfc_release_firmware(dev);
1329 1330
	vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[0]);
	vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[1]);
1331
	s5p_mfc_unconfigure_dma_memory(dev);
1332 1333
	vb2_dma_contig_clear_max_seg_size(dev->mem_dev_l);
	vb2_dma_contig_clear_max_seg_size(dev->mem_dev_r);
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348

	s5p_mfc_final_pm(dev);
	return 0;
}

#ifdef CONFIG_PM_SLEEP

static int s5p_mfc_suspend(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
	int ret;

	if (m_dev->num_inst == 0)
		return 0;
1349

1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
	if (test_and_set_bit(0, &m_dev->enter_suspend) != 0) {
		mfc_err("Error: going to suspend for a second time\n");
		return -EIO;
	}

	/* Check if we're processing then wait if it necessary. */
	while (test_and_set_bit(0, &m_dev->hw_lock) != 0) {
		/* Try and lock the HW */
		/* Wait on the interrupt waitqueue */
		ret = wait_event_interruptible_timeout(m_dev->queue,
1360
			m_dev->int_cond, msecs_to_jiffies(MFC_INT_TIMEOUT));
1361 1362
		if (ret == 0) {
			mfc_err("Waiting for hardware to finish timed out\n");
1363
			clear_bit(0, &m_dev->enter_suspend);
1364 1365 1366
			return -EIO;
		}
	}
1367

1368 1369 1370 1371 1372 1373
	ret = s5p_mfc_sleep(m_dev);
	if (ret) {
		clear_bit(0, &m_dev->enter_suspend);
		clear_bit(0, &m_dev->hw_lock);
	}
	return ret;
1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
}

static int s5p_mfc_resume(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);

	if (m_dev->num_inst == 0)
		return 0;
	return s5p_mfc_wakeup(m_dev);
}
#endif

1387
#ifdef CONFIG_PM
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
static int s5p_mfc_runtime_suspend(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);

	atomic_set(&m_dev->pm.power, 0);
	return 0;
}

static int s5p_mfc_runtime_resume(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);

	atomic_set(&m_dev->pm.power, 1);
	return 0;
}
#endif

/* Power management */
static const struct dev_pm_ops s5p_mfc_pm_ops = {
	SET_SYSTEM_SLEEP_PM_OPS(s5p_mfc_suspend, s5p_mfc_resume)
	SET_RUNTIME_PM_OPS(s5p_mfc_runtime_suspend, s5p_mfc_runtime_resume,
			   NULL)
};

1414
static struct s5p_mfc_buf_size_v5 mfc_buf_size_v5 = {
1415 1416 1417 1418 1419 1420
	.h264_ctx	= MFC_H264_CTX_BUF_SIZE,
	.non_h264_ctx	= MFC_CTX_BUF_SIZE,
	.dsc		= DESC_BUF_SIZE,
	.shm		= SHARED_BUF_SIZE,
};

1421
static struct s5p_mfc_buf_size buf_size_v5 = {
1422 1423 1424 1425 1426
	.fw	= MAX_FW_SIZE,
	.cpb	= MAX_CPB_SIZE,
	.priv	= &mfc_buf_size_v5,
};

1427
static struct s5p_mfc_buf_align mfc_buf_align_v5 = {
1428 1429 1430 1431 1432
	.base = MFC_BASE_ALIGN_ORDER,
};

static struct s5p_mfc_variant mfc_drvdata_v5 = {
	.version	= MFC_VERSION,
1433
	.version_bit	= MFC_V5_BIT,
1434 1435 1436
	.port_num	= MFC_NUM_PORTS,
	.buf_size	= &buf_size_v5,
	.buf_align	= &mfc_buf_align_v5,
1437
	.fw_name[0]	= "s5p-mfc.fw",
1438 1439
};

1440
static struct s5p_mfc_buf_size_v6 mfc_buf_size_v6 = {
1441 1442 1443 1444 1445 1446 1447
	.dev_ctx	= MFC_CTX_BUF_SIZE_V6,
	.h264_dec_ctx	= MFC_H264_DEC_CTX_BUF_SIZE_V6,
	.other_dec_ctx	= MFC_OTHER_DEC_CTX_BUF_SIZE_V6,
	.h264_enc_ctx	= MFC_H264_ENC_CTX_BUF_SIZE_V6,
	.other_enc_ctx	= MFC_OTHER_ENC_CTX_BUF_SIZE_V6,
};

1448
static struct s5p_mfc_buf_size buf_size_v6 = {
1449 1450 1451 1452 1453
	.fw	= MAX_FW_SIZE_V6,
	.cpb	= MAX_CPB_SIZE_V6,
	.priv	= &mfc_buf_size_v6,
};

1454
static struct s5p_mfc_buf_align mfc_buf_align_v6 = {
1455 1456 1457 1458 1459
	.base = 0,
};

static struct s5p_mfc_variant mfc_drvdata_v6 = {
	.version	= MFC_VERSION_V6,
1460
	.version_bit	= MFC_V6_BIT,
1461 1462 1463
	.port_num	= MFC_NUM_PORTS_V6,
	.buf_size	= &buf_size_v6,
	.buf_align	= &mfc_buf_align_v6,
1464 1465 1466 1467 1468 1469
	.fw_name[0]     = "s5p-mfc-v6.fw",
	/*
	 * v6-v2 firmware contains bug fixes and interface change
	 * for init buffer command
	 */
	.fw_name[1]     = "s5p-mfc-v6-v2.fw",
1470 1471
};

1472
static struct s5p_mfc_buf_size_v6 mfc_buf_size_v7 = {
1473 1474 1475 1476 1477 1478 1479
	.dev_ctx	= MFC_CTX_BUF_SIZE_V7,
	.h264_dec_ctx	= MFC_H264_DEC_CTX_BUF_SIZE_V7,
	.other_dec_ctx	= MFC_OTHER_DEC_CTX_BUF_SIZE_V7,
	.h264_enc_ctx	= MFC_H264_ENC_CTX_BUF_SIZE_V7,
	.other_enc_ctx	= MFC_OTHER_ENC_CTX_BUF_SIZE_V7,
};

1480
static struct s5p_mfc_buf_size buf_size_v7 = {
1481 1482 1483 1484 1485
	.fw	= MAX_FW_SIZE_V7,
	.cpb	= MAX_CPB_SIZE_V7,
	.priv	= &mfc_buf_size_v7,
};

1486
static struct s5p_mfc_buf_align mfc_buf_align_v7 = {
1487 1488 1489 1490 1491
	.base = 0,
};

static struct s5p_mfc_variant mfc_drvdata_v7 = {
	.version	= MFC_VERSION_V7,
1492
	.version_bit	= MFC_V7_BIT,
1493 1494 1495
	.port_num	= MFC_NUM_PORTS_V7,
	.buf_size	= &buf_size_v7,
	.buf_align	= &mfc_buf_align_v7,
1496
	.fw_name[0]     = "s5p-mfc-v7.fw",
1497 1498
};

1499
static struct s5p_mfc_buf_size_v6 mfc_buf_size_v8 = {
1500 1501 1502
	.dev_ctx	= MFC_CTX_BUF_SIZE_V8,
	.h264_dec_ctx	= MFC_H264_DEC_CTX_BUF_SIZE_V8,
	.other_dec_ctx	= MFC_OTHER_DEC_CTX_BUF_SIZE_V8,
1503 1504
	.h264_enc_ctx	= MFC_H264_ENC_CTX_BUF_SIZE_V8,
	.other_enc_ctx	= MFC_OTHER_ENC_CTX_BUF_SIZE_V8,
1505 1506
};

1507
static struct s5p_mfc_buf_size buf_size_v8 = {
1508 1509 1510 1511 1512
	.fw	= MAX_FW_SIZE_V8,
	.cpb	= MAX_CPB_SIZE_V8,
	.priv	= &mfc_buf_size_v8,
};

1513
static struct s5p_mfc_buf_align mfc_buf_align_v8 = {
1514 1515 1516 1517 1518 1519 1520 1521 1522
	.base = 0,
};

static struct s5p_mfc_variant mfc_drvdata_v8 = {
	.version	= MFC_VERSION_V8,
	.version_bit	= MFC_V8_BIT,
	.port_num	= MFC_NUM_PORTS_V8,
	.buf_size	= &buf_size_v8,
	.buf_align	= &mfc_buf_align_v8,
1523
	.fw_name[0]     = "s5p-mfc-v8.fw",
1524 1525
};

1526 1527 1528 1529 1530 1531 1532
static const struct of_device_id exynos_mfc_match[] = {
	{
		.compatible = "samsung,mfc-v5",
		.data = &mfc_drvdata_v5,
	}, {
		.compatible = "samsung,mfc-v6",
		.data = &mfc_drvdata_v6,
1533 1534 1535
	}, {
		.compatible = "samsung,mfc-v7",
		.data = &mfc_drvdata_v7,
1536 1537 1538
	}, {
		.compatible = "samsung,mfc-v8",
		.data = &mfc_drvdata_v8,
1539 1540 1541 1542 1543 1544 1545 1546
	},
	{},
};
MODULE_DEVICE_TABLE(of, exynos_mfc_match);

static void *mfc_get_drv_data(struct platform_device *pdev)
{
	struct s5p_mfc_variant *driver_data = NULL;
1547 1548 1549 1550 1551
	const struct of_device_id *match;

	match = of_match_node(exynos_mfc_match, pdev->dev.of_node);
	if (match)
		driver_data = (struct s5p_mfc_variant *)match->data;
1552 1553 1554 1555

	return driver_data;
}

1556
static struct platform_driver s5p_mfc_driver = {
1557
	.probe		= s5p_mfc_probe,
1558
	.remove		= s5p_mfc_remove,
1559 1560
	.driver	= {
		.name	= S5P_MFC_NAME,
1561 1562
		.pm	= &s5p_mfc_pm_ops,
		.of_match_table = exynos_mfc_match,
1563 1564 1565
	},
};

1566
module_platform_driver(s5p_mfc_driver);
1567 1568 1569 1570 1571

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
MODULE_DESCRIPTION("Samsung S5P Multi Format Codec V4L2 driver");