18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Samsung S5P Multi Format Codec v 5.1
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2011 Samsung Electronics Co., Ltd.
68c2ecf20Sopenharmony_ci * Kamil Debski, <k.debski@samsung.com>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/clk.h>
108c2ecf20Sopenharmony_ci#include <linux/delay.h>
118c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
128c2ecf20Sopenharmony_ci#include <linux/io.h>
138c2ecf20Sopenharmony_ci#include <linux/module.h>
148c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
158c2ecf20Sopenharmony_ci#include <linux/sched.h>
168c2ecf20Sopenharmony_ci#include <linux/slab.h>
178c2ecf20Sopenharmony_ci#include <linux/videodev2.h>
188c2ecf20Sopenharmony_ci#include <media/v4l2-event.h>
198c2ecf20Sopenharmony_ci#include <linux/workqueue.h>
208c2ecf20Sopenharmony_ci#include <linux/of.h>
218c2ecf20Sopenharmony_ci#include <linux/of_device.h>
228c2ecf20Sopenharmony_ci#include <linux/of_reserved_mem.h>
238c2ecf20Sopenharmony_ci#include <media/videobuf2-v4l2.h>
248c2ecf20Sopenharmony_ci#include "s5p_mfc_common.h"
258c2ecf20Sopenharmony_ci#include "s5p_mfc_ctrl.h"
268c2ecf20Sopenharmony_ci#include "s5p_mfc_debug.h"
278c2ecf20Sopenharmony_ci#include "s5p_mfc_dec.h"
288c2ecf20Sopenharmony_ci#include "s5p_mfc_enc.h"
298c2ecf20Sopenharmony_ci#include "s5p_mfc_intr.h"
308c2ecf20Sopenharmony_ci#include "s5p_mfc_iommu.h"
318c2ecf20Sopenharmony_ci#include "s5p_mfc_opr.h"
328c2ecf20Sopenharmony_ci#include "s5p_mfc_cmd.h"
338c2ecf20Sopenharmony_ci#include "s5p_mfc_pm.h"
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#define S5P_MFC_DEC_NAME	"s5p-mfc-dec"
368c2ecf20Sopenharmony_ci#define S5P_MFC_ENC_NAME	"s5p-mfc-enc"
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ciint mfc_debug_level;
398c2ecf20Sopenharmony_cimodule_param_named(debug, mfc_debug_level, int, S_IRUGO | S_IWUSR);
408c2ecf20Sopenharmony_ciMODULE_PARM_DESC(debug, "Debug level - higher value produces more verbose messages");
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_cistatic char *mfc_mem_size;
438c2ecf20Sopenharmony_cimodule_param_named(mem, mfc_mem_size, charp, 0644);
448c2ecf20Sopenharmony_ciMODULE_PARM_DESC(mem, "Preallocated memory size for the firmware and context buffers");
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci/* Helper functions for interrupt processing */
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci/* Remove from hw execution round robin */
498c2ecf20Sopenharmony_civoid clear_work_bit(struct s5p_mfc_ctx *ctx)
508c2ecf20Sopenharmony_ci{
518c2ecf20Sopenharmony_ci	struct s5p_mfc_dev *dev = ctx->dev;
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	spin_lock(&dev->condlock);
548c2ecf20Sopenharmony_ci	__clear_bit(ctx->num, &dev->ctx_work_bits);
558c2ecf20Sopenharmony_ci	spin_unlock(&dev->condlock);
568c2ecf20Sopenharmony_ci}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci/* Add to hw execution round robin */
598c2ecf20Sopenharmony_civoid set_work_bit(struct s5p_mfc_ctx *ctx)
608c2ecf20Sopenharmony_ci{
618c2ecf20Sopenharmony_ci	struct s5p_mfc_dev *dev = ctx->dev;
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	spin_lock(&dev->condlock);
648c2ecf20Sopenharmony_ci	__set_bit(ctx->num, &dev->ctx_work_bits);
658c2ecf20Sopenharmony_ci	spin_unlock(&dev->condlock);
668c2ecf20Sopenharmony_ci}
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci/* Remove from hw execution round robin */
698c2ecf20Sopenharmony_civoid clear_work_bit_irqsave(struct s5p_mfc_ctx *ctx)
708c2ecf20Sopenharmony_ci{
718c2ecf20Sopenharmony_ci	struct s5p_mfc_dev *dev = ctx->dev;
728c2ecf20Sopenharmony_ci	unsigned long flags;
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	spin_lock_irqsave(&dev->condlock, flags);
758c2ecf20Sopenharmony_ci	__clear_bit(ctx->num, &dev->ctx_work_bits);
768c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&dev->condlock, flags);
778c2ecf20Sopenharmony_ci}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci/* Add to hw execution round robin */
808c2ecf20Sopenharmony_civoid set_work_bit_irqsave(struct s5p_mfc_ctx *ctx)
818c2ecf20Sopenharmony_ci{
828c2ecf20Sopenharmony_ci	struct s5p_mfc_dev *dev = ctx->dev;
838c2ecf20Sopenharmony_ci	unsigned long flags;
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	spin_lock_irqsave(&dev->condlock, flags);
868c2ecf20Sopenharmony_ci	__set_bit(ctx->num, &dev->ctx_work_bits);
878c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&dev->condlock, flags);
888c2ecf20Sopenharmony_ci}
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ciint s5p_mfc_get_new_ctx(struct s5p_mfc_dev *dev)
918c2ecf20Sopenharmony_ci{
928c2ecf20Sopenharmony_ci	unsigned long flags;
938c2ecf20Sopenharmony_ci	int ctx;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	spin_lock_irqsave(&dev->condlock, flags);
968c2ecf20Sopenharmony_ci	ctx = dev->curr_ctx;
978c2ecf20Sopenharmony_ci	do {
988c2ecf20Sopenharmony_ci		ctx = (ctx + 1) % MFC_NUM_CONTEXTS;
998c2ecf20Sopenharmony_ci		if (ctx == dev->curr_ctx) {
1008c2ecf20Sopenharmony_ci			if (!test_bit(ctx, &dev->ctx_work_bits))
1018c2ecf20Sopenharmony_ci				ctx = -EAGAIN;
1028c2ecf20Sopenharmony_ci			break;
1038c2ecf20Sopenharmony_ci		}
1048c2ecf20Sopenharmony_ci	} while (!test_bit(ctx, &dev->ctx_work_bits));
1058c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&dev->condlock, flags);
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	return ctx;
1088c2ecf20Sopenharmony_ci}
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci/* Wake up context wait_queue */
1118c2ecf20Sopenharmony_cistatic void wake_up_ctx(struct s5p_mfc_ctx *ctx, unsigned int reason,
1128c2ecf20Sopenharmony_ci			unsigned int err)
1138c2ecf20Sopenharmony_ci{
1148c2ecf20Sopenharmony_ci	ctx->int_cond = 1;
1158c2ecf20Sopenharmony_ci	ctx->int_type = reason;
1168c2ecf20Sopenharmony_ci	ctx->int_err = err;
1178c2ecf20Sopenharmony_ci	wake_up(&ctx->queue);
1188c2ecf20Sopenharmony_ci}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci/* Wake up device wait_queue */
1218c2ecf20Sopenharmony_cistatic void wake_up_dev(struct s5p_mfc_dev *dev, unsigned int reason,
1228c2ecf20Sopenharmony_ci			unsigned int err)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	dev->int_cond = 1;
1258c2ecf20Sopenharmony_ci	dev->int_type = reason;
1268c2ecf20Sopenharmony_ci	dev->int_err = err;
1278c2ecf20Sopenharmony_ci	wake_up(&dev->queue);
1288c2ecf20Sopenharmony_ci}
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_civoid s5p_mfc_cleanup_queue(struct list_head *lh, struct vb2_queue *vq)
1318c2ecf20Sopenharmony_ci{
1328c2ecf20Sopenharmony_ci	struct s5p_mfc_buf *b;
1338c2ecf20Sopenharmony_ci	int i;
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	while (!list_empty(lh)) {
1368c2ecf20Sopenharmony_ci		b = list_entry(lh->next, struct s5p_mfc_buf, list);
1378c2ecf20Sopenharmony_ci		for (i = 0; i < b->b->vb2_buf.num_planes; i++)
1388c2ecf20Sopenharmony_ci			vb2_set_plane_payload(&b->b->vb2_buf, i, 0);
1398c2ecf20Sopenharmony_ci		vb2_buffer_done(&b->b->vb2_buf, VB2_BUF_STATE_ERROR);
1408c2ecf20Sopenharmony_ci		list_del(&b->list);
1418c2ecf20Sopenharmony_ci	}
1428c2ecf20Sopenharmony_ci}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_cistatic void s5p_mfc_watchdog(struct timer_list *t)
1458c2ecf20Sopenharmony_ci{
1468c2ecf20Sopenharmony_ci	struct s5p_mfc_dev *dev = from_timer(dev, t, watchdog_timer);
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	if (test_bit(0, &dev->hw_lock))
1498c2ecf20Sopenharmony_ci		atomic_inc(&dev->watchdog_cnt);
1508c2ecf20Sopenharmony_ci	if (atomic_read(&dev->watchdog_cnt) >= MFC_WATCHDOG_CNT) {
1518c2ecf20Sopenharmony_ci		/* This means that hw is busy and no interrupts were
1528c2ecf20Sopenharmony_ci		 * generated by hw for the Nth time of running this
1538c2ecf20Sopenharmony_ci		 * watchdog timer. This usually means a serious hw
1548c2ecf20Sopenharmony_ci		 * error. Now it is time to kill all instances and
1558c2ecf20Sopenharmony_ci		 * reset the MFC. */
1568c2ecf20Sopenharmony_ci		mfc_err("Time out during waiting for HW\n");
1578c2ecf20Sopenharmony_ci		schedule_work(&dev->watchdog_work);
1588c2ecf20Sopenharmony_ci	}
1598c2ecf20Sopenharmony_ci	dev->watchdog_timer.expires = jiffies +
1608c2ecf20Sopenharmony_ci					msecs_to_jiffies(MFC_WATCHDOG_INTERVAL);
1618c2ecf20Sopenharmony_ci	add_timer(&dev->watchdog_timer);
1628c2ecf20Sopenharmony_ci}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_cistatic void s5p_mfc_watchdog_worker(struct work_struct *work)
1658c2ecf20Sopenharmony_ci{
1668c2ecf20Sopenharmony_ci	struct s5p_mfc_dev *dev;
1678c2ecf20Sopenharmony_ci	struct s5p_mfc_ctx *ctx;
1688c2ecf20Sopenharmony_ci	unsigned long flags;
1698c2ecf20Sopenharmony_ci	int mutex_locked;
1708c2ecf20Sopenharmony_ci	int i, ret;
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	dev = container_of(work, struct s5p_mfc_dev, watchdog_work);
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	mfc_err("Driver timeout error handling\n");
1758c2ecf20Sopenharmony_ci	/* Lock the mutex that protects open and release.
1768c2ecf20Sopenharmony_ci	 * This is necessary as they may load and unload firmware. */
1778c2ecf20Sopenharmony_ci	mutex_locked = mutex_trylock(&dev->mfc_mutex);
1788c2ecf20Sopenharmony_ci	if (!mutex_locked)
1798c2ecf20Sopenharmony_ci		mfc_err("Error: some instance may be closing/opening\n");
1808c2ecf20Sopenharmony_ci	spin_lock_irqsave(&dev->irqlock, flags);
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	s5p_mfc_clock_off();
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	for (i = 0; i < MFC_NUM_CONTEXTS; i++) {
1858c2ecf20Sopenharmony_ci		ctx = dev->ctx[i];
1868c2ecf20Sopenharmony_ci		if (!ctx)
1878c2ecf20Sopenharmony_ci			continue;
1888c2ecf20Sopenharmony_ci		ctx->state = MFCINST_ERROR;
1898c2ecf20Sopenharmony_ci		s5p_mfc_cleanup_queue(&ctx->dst_queue, &ctx->vq_dst);
1908c2ecf20Sopenharmony_ci		s5p_mfc_cleanup_queue(&ctx->src_queue, &ctx->vq_src);
1918c2ecf20Sopenharmony_ci		clear_work_bit(ctx);
1928c2ecf20Sopenharmony_ci		wake_up_ctx(ctx, S5P_MFC_R2H_CMD_ERR_RET, 0);
1938c2ecf20Sopenharmony_ci	}
1948c2ecf20Sopenharmony_ci	clear_bit(0, &dev->hw_lock);
1958c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&dev->irqlock, flags);
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	/* De-init MFC */
1988c2ecf20Sopenharmony_ci	s5p_mfc_deinit_hw(dev);
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	/* Double check if there is at least one instance running.
2018c2ecf20Sopenharmony_ci	 * If no instance is in memory than no firmware should be present */
2028c2ecf20Sopenharmony_ci	if (dev->num_inst > 0) {
2038c2ecf20Sopenharmony_ci		ret = s5p_mfc_load_firmware(dev);
2048c2ecf20Sopenharmony_ci		if (ret) {
2058c2ecf20Sopenharmony_ci			mfc_err("Failed to reload FW\n");
2068c2ecf20Sopenharmony_ci			goto unlock;
2078c2ecf20Sopenharmony_ci		}
2088c2ecf20Sopenharmony_ci		s5p_mfc_clock_on();
2098c2ecf20Sopenharmony_ci		ret = s5p_mfc_init_hw(dev);
2108c2ecf20Sopenharmony_ci		s5p_mfc_clock_off();
2118c2ecf20Sopenharmony_ci		if (ret)
2128c2ecf20Sopenharmony_ci			mfc_err("Failed to reinit FW\n");
2138c2ecf20Sopenharmony_ci	}
2148c2ecf20Sopenharmony_ciunlock:
2158c2ecf20Sopenharmony_ci	if (mutex_locked)
2168c2ecf20Sopenharmony_ci		mutex_unlock(&dev->mfc_mutex);
2178c2ecf20Sopenharmony_ci}
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_cistatic void s5p_mfc_handle_frame_all_extracted(struct s5p_mfc_ctx *ctx)
2208c2ecf20Sopenharmony_ci{
2218c2ecf20Sopenharmony_ci	struct s5p_mfc_buf *dst_buf;
2228c2ecf20Sopenharmony_ci	struct s5p_mfc_dev *dev = ctx->dev;
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	ctx->state = MFCINST_FINISHED;
2258c2ecf20Sopenharmony_ci	ctx->sequence++;
2268c2ecf20Sopenharmony_ci	while (!list_empty(&ctx->dst_queue)) {
2278c2ecf20Sopenharmony_ci		dst_buf = list_entry(ctx->dst_queue.next,
2288c2ecf20Sopenharmony_ci				     struct s5p_mfc_buf, list);
2298c2ecf20Sopenharmony_ci		mfc_debug(2, "Cleaning up buffer: %d\n",
2308c2ecf20Sopenharmony_ci					  dst_buf->b->vb2_buf.index);
2318c2ecf20Sopenharmony_ci		vb2_set_plane_payload(&dst_buf->b->vb2_buf, 0, 0);
2328c2ecf20Sopenharmony_ci		vb2_set_plane_payload(&dst_buf->b->vb2_buf, 1, 0);
2338c2ecf20Sopenharmony_ci		list_del(&dst_buf->list);
2348c2ecf20Sopenharmony_ci		dst_buf->flags |= MFC_BUF_FLAG_EOS;
2358c2ecf20Sopenharmony_ci		ctx->dst_queue_cnt--;
2368c2ecf20Sopenharmony_ci		dst_buf->b->sequence = (ctx->sequence++);
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci		if (s5p_mfc_hw_call(dev->mfc_ops, get_pic_type_top, ctx) ==
2398c2ecf20Sopenharmony_ci			s5p_mfc_hw_call(dev->mfc_ops, get_pic_type_bot, ctx))
2408c2ecf20Sopenharmony_ci			dst_buf->b->field = V4L2_FIELD_NONE;
2418c2ecf20Sopenharmony_ci		else
2428c2ecf20Sopenharmony_ci			dst_buf->b->field = V4L2_FIELD_INTERLACED;
2438c2ecf20Sopenharmony_ci		dst_buf->b->flags |= V4L2_BUF_FLAG_LAST;
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci		ctx->dec_dst_flag &= ~(1 << dst_buf->b->vb2_buf.index);
2468c2ecf20Sopenharmony_ci		vb2_buffer_done(&dst_buf->b->vb2_buf, VB2_BUF_STATE_DONE);
2478c2ecf20Sopenharmony_ci	}
2488c2ecf20Sopenharmony_ci}
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_cistatic void s5p_mfc_handle_frame_copy_time(struct s5p_mfc_ctx *ctx)
2518c2ecf20Sopenharmony_ci{
2528c2ecf20Sopenharmony_ci	struct s5p_mfc_dev *dev = ctx->dev;
2538c2ecf20Sopenharmony_ci	struct s5p_mfc_buf *dst_buf, *src_buf;
2548c2ecf20Sopenharmony_ci	u32 dec_y_addr;
2558c2ecf20Sopenharmony_ci	unsigned int frame_type;
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	/* Make sure we actually have a new frame before continuing. */
2588c2ecf20Sopenharmony_ci	frame_type = s5p_mfc_hw_call(dev->mfc_ops, get_dec_frame_type, dev);
2598c2ecf20Sopenharmony_ci	if (frame_type == S5P_FIMV_DECODE_FRAME_SKIPPED)
2608c2ecf20Sopenharmony_ci		return;
2618c2ecf20Sopenharmony_ci	dec_y_addr = (u32)s5p_mfc_hw_call(dev->mfc_ops, get_dec_y_adr, dev);
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	/* Copy timestamp / timecode from decoded src to dst and set
2648c2ecf20Sopenharmony_ci	   appropriate flags. */
2658c2ecf20Sopenharmony_ci	src_buf = list_entry(ctx->src_queue.next, struct s5p_mfc_buf, list);
2668c2ecf20Sopenharmony_ci	list_for_each_entry(dst_buf, &ctx->dst_queue, list) {
2678c2ecf20Sopenharmony_ci		u32 addr = (u32)vb2_dma_contig_plane_dma_addr(&dst_buf->b->vb2_buf, 0);
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci		if (addr == dec_y_addr) {
2708c2ecf20Sopenharmony_ci			dst_buf->b->timecode = src_buf->b->timecode;
2718c2ecf20Sopenharmony_ci			dst_buf->b->vb2_buf.timestamp =
2728c2ecf20Sopenharmony_ci						src_buf->b->vb2_buf.timestamp;
2738c2ecf20Sopenharmony_ci			dst_buf->b->flags &=
2748c2ecf20Sopenharmony_ci				~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
2758c2ecf20Sopenharmony_ci			dst_buf->b->flags |=
2768c2ecf20Sopenharmony_ci				src_buf->b->flags
2778c2ecf20Sopenharmony_ci				& V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
2788c2ecf20Sopenharmony_ci			switch (frame_type) {
2798c2ecf20Sopenharmony_ci			case S5P_FIMV_DECODE_FRAME_I_FRAME:
2808c2ecf20Sopenharmony_ci				dst_buf->b->flags |=
2818c2ecf20Sopenharmony_ci						V4L2_BUF_FLAG_KEYFRAME;
2828c2ecf20Sopenharmony_ci				break;
2838c2ecf20Sopenharmony_ci			case S5P_FIMV_DECODE_FRAME_P_FRAME:
2848c2ecf20Sopenharmony_ci				dst_buf->b->flags |=
2858c2ecf20Sopenharmony_ci						V4L2_BUF_FLAG_PFRAME;
2868c2ecf20Sopenharmony_ci				break;
2878c2ecf20Sopenharmony_ci			case S5P_FIMV_DECODE_FRAME_B_FRAME:
2888c2ecf20Sopenharmony_ci				dst_buf->b->flags |=
2898c2ecf20Sopenharmony_ci						V4L2_BUF_FLAG_BFRAME;
2908c2ecf20Sopenharmony_ci				break;
2918c2ecf20Sopenharmony_ci			default:
2928c2ecf20Sopenharmony_ci				/* Don't know how to handle
2938c2ecf20Sopenharmony_ci				   S5P_FIMV_DECODE_FRAME_OTHER_FRAME. */
2948c2ecf20Sopenharmony_ci				mfc_debug(2, "Unexpected frame type: %d\n",
2958c2ecf20Sopenharmony_ci						frame_type);
2968c2ecf20Sopenharmony_ci			}
2978c2ecf20Sopenharmony_ci			break;
2988c2ecf20Sopenharmony_ci		}
2998c2ecf20Sopenharmony_ci	}
3008c2ecf20Sopenharmony_ci}
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_cistatic void s5p_mfc_handle_frame_new(struct s5p_mfc_ctx *ctx, unsigned int err)
3038c2ecf20Sopenharmony_ci{
3048c2ecf20Sopenharmony_ci	struct s5p_mfc_dev *dev = ctx->dev;
3058c2ecf20Sopenharmony_ci	struct s5p_mfc_buf  *dst_buf;
3068c2ecf20Sopenharmony_ci	u32 dspl_y_addr;
3078c2ecf20Sopenharmony_ci	unsigned int frame_type;
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci	dspl_y_addr = (u32)s5p_mfc_hw_call(dev->mfc_ops, get_dspl_y_adr, dev);
3108c2ecf20Sopenharmony_ci	if (IS_MFCV6_PLUS(dev))
3118c2ecf20Sopenharmony_ci		frame_type = s5p_mfc_hw_call(dev->mfc_ops,
3128c2ecf20Sopenharmony_ci			get_disp_frame_type, ctx);
3138c2ecf20Sopenharmony_ci	else
3148c2ecf20Sopenharmony_ci		frame_type = s5p_mfc_hw_call(dev->mfc_ops,
3158c2ecf20Sopenharmony_ci			get_dec_frame_type, dev);
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	/* If frame is same as previous then skip and do not dequeue */
3188c2ecf20Sopenharmony_ci	if (frame_type == S5P_FIMV_DECODE_FRAME_SKIPPED) {
3198c2ecf20Sopenharmony_ci		if (!ctx->after_packed_pb)
3208c2ecf20Sopenharmony_ci			ctx->sequence++;
3218c2ecf20Sopenharmony_ci		ctx->after_packed_pb = 0;
3228c2ecf20Sopenharmony_ci		return;
3238c2ecf20Sopenharmony_ci	}
3248c2ecf20Sopenharmony_ci	ctx->sequence++;
3258c2ecf20Sopenharmony_ci	/* The MFC returns address of the buffer, now we have to
3268c2ecf20Sopenharmony_ci	 * check which videobuf does it correspond to */
3278c2ecf20Sopenharmony_ci	list_for_each_entry(dst_buf, &ctx->dst_queue, list) {
3288c2ecf20Sopenharmony_ci		u32 addr = (u32)vb2_dma_contig_plane_dma_addr(&dst_buf->b->vb2_buf, 0);
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci		/* Check if this is the buffer we're looking for */
3318c2ecf20Sopenharmony_ci		if (addr == dspl_y_addr) {
3328c2ecf20Sopenharmony_ci			list_del(&dst_buf->list);
3338c2ecf20Sopenharmony_ci			ctx->dst_queue_cnt--;
3348c2ecf20Sopenharmony_ci			dst_buf->b->sequence = ctx->sequence;
3358c2ecf20Sopenharmony_ci			if (s5p_mfc_hw_call(dev->mfc_ops,
3368c2ecf20Sopenharmony_ci					get_pic_type_top, ctx) ==
3378c2ecf20Sopenharmony_ci				s5p_mfc_hw_call(dev->mfc_ops,
3388c2ecf20Sopenharmony_ci					get_pic_type_bot, ctx))
3398c2ecf20Sopenharmony_ci				dst_buf->b->field = V4L2_FIELD_NONE;
3408c2ecf20Sopenharmony_ci			else
3418c2ecf20Sopenharmony_ci				dst_buf->b->field =
3428c2ecf20Sopenharmony_ci							V4L2_FIELD_INTERLACED;
3438c2ecf20Sopenharmony_ci			vb2_set_plane_payload(&dst_buf->b->vb2_buf, 0,
3448c2ecf20Sopenharmony_ci						ctx->luma_size);
3458c2ecf20Sopenharmony_ci			vb2_set_plane_payload(&dst_buf->b->vb2_buf, 1,
3468c2ecf20Sopenharmony_ci						ctx->chroma_size);
3478c2ecf20Sopenharmony_ci			clear_bit(dst_buf->b->vb2_buf.index,
3488c2ecf20Sopenharmony_ci							&ctx->dec_dst_flag);
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci			vb2_buffer_done(&dst_buf->b->vb2_buf, err ?
3518c2ecf20Sopenharmony_ci				VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci			break;
3548c2ecf20Sopenharmony_ci		}
3558c2ecf20Sopenharmony_ci	}
3568c2ecf20Sopenharmony_ci}
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci/* Handle frame decoding interrupt */
3598c2ecf20Sopenharmony_cistatic void s5p_mfc_handle_frame(struct s5p_mfc_ctx *ctx,
3608c2ecf20Sopenharmony_ci					unsigned int reason, unsigned int err)
3618c2ecf20Sopenharmony_ci{
3628c2ecf20Sopenharmony_ci	struct s5p_mfc_dev *dev = ctx->dev;
3638c2ecf20Sopenharmony_ci	unsigned int dst_frame_status;
3648c2ecf20Sopenharmony_ci	unsigned int dec_frame_status;
3658c2ecf20Sopenharmony_ci	struct s5p_mfc_buf *src_buf;
3668c2ecf20Sopenharmony_ci	unsigned int res_change;
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci	dst_frame_status = s5p_mfc_hw_call(dev->mfc_ops, get_dspl_status, dev)
3698c2ecf20Sopenharmony_ci				& S5P_FIMV_DEC_STATUS_DECODING_STATUS_MASK;
3708c2ecf20Sopenharmony_ci	dec_frame_status = s5p_mfc_hw_call(dev->mfc_ops, get_dec_status, dev)
3718c2ecf20Sopenharmony_ci				& S5P_FIMV_DEC_STATUS_DECODING_STATUS_MASK;
3728c2ecf20Sopenharmony_ci	res_change = (s5p_mfc_hw_call(dev->mfc_ops, get_dspl_status, dev)
3738c2ecf20Sopenharmony_ci				& S5P_FIMV_DEC_STATUS_RESOLUTION_MASK)
3748c2ecf20Sopenharmony_ci				>> S5P_FIMV_DEC_STATUS_RESOLUTION_SHIFT;
3758c2ecf20Sopenharmony_ci	mfc_debug(2, "Frame Status: %x\n", dst_frame_status);
3768c2ecf20Sopenharmony_ci	if (ctx->state == MFCINST_RES_CHANGE_INIT)
3778c2ecf20Sopenharmony_ci		ctx->state = MFCINST_RES_CHANGE_FLUSH;
3788c2ecf20Sopenharmony_ci	if (res_change == S5P_FIMV_RES_INCREASE ||
3798c2ecf20Sopenharmony_ci		res_change == S5P_FIMV_RES_DECREASE) {
3808c2ecf20Sopenharmony_ci		ctx->state = MFCINST_RES_CHANGE_INIT;
3818c2ecf20Sopenharmony_ci		s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
3828c2ecf20Sopenharmony_ci		wake_up_ctx(ctx, reason, err);
3838c2ecf20Sopenharmony_ci		WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
3848c2ecf20Sopenharmony_ci		s5p_mfc_clock_off();
3858c2ecf20Sopenharmony_ci		s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
3868c2ecf20Sopenharmony_ci		return;
3878c2ecf20Sopenharmony_ci	}
3888c2ecf20Sopenharmony_ci	if (ctx->dpb_flush_flag)
3898c2ecf20Sopenharmony_ci		ctx->dpb_flush_flag = 0;
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci	/* All frames remaining in the buffer have been extracted  */
3928c2ecf20Sopenharmony_ci	if (dst_frame_status == S5P_FIMV_DEC_STATUS_DECODING_EMPTY) {
3938c2ecf20Sopenharmony_ci		if (ctx->state == MFCINST_RES_CHANGE_FLUSH) {
3948c2ecf20Sopenharmony_ci			static const struct v4l2_event ev_src_ch = {
3958c2ecf20Sopenharmony_ci				.type = V4L2_EVENT_SOURCE_CHANGE,
3968c2ecf20Sopenharmony_ci				.u.src_change.changes =
3978c2ecf20Sopenharmony_ci					V4L2_EVENT_SRC_CH_RESOLUTION,
3988c2ecf20Sopenharmony_ci			};
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci			s5p_mfc_handle_frame_all_extracted(ctx);
4018c2ecf20Sopenharmony_ci			ctx->state = MFCINST_RES_CHANGE_END;
4028c2ecf20Sopenharmony_ci			v4l2_event_queue_fh(&ctx->fh, &ev_src_ch);
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci			goto leave_handle_frame;
4058c2ecf20Sopenharmony_ci		} else {
4068c2ecf20Sopenharmony_ci			s5p_mfc_handle_frame_all_extracted(ctx);
4078c2ecf20Sopenharmony_ci		}
4088c2ecf20Sopenharmony_ci	}
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	if (dec_frame_status == S5P_FIMV_DEC_STATUS_DECODING_DISPLAY)
4118c2ecf20Sopenharmony_ci		s5p_mfc_handle_frame_copy_time(ctx);
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci	/* A frame has been decoded and is in the buffer  */
4148c2ecf20Sopenharmony_ci	if (dst_frame_status == S5P_FIMV_DEC_STATUS_DISPLAY_ONLY ||
4158c2ecf20Sopenharmony_ci	    dst_frame_status == S5P_FIMV_DEC_STATUS_DECODING_DISPLAY) {
4168c2ecf20Sopenharmony_ci		s5p_mfc_handle_frame_new(ctx, err);
4178c2ecf20Sopenharmony_ci	} else {
4188c2ecf20Sopenharmony_ci		mfc_debug(2, "No frame decode\n");
4198c2ecf20Sopenharmony_ci	}
4208c2ecf20Sopenharmony_ci	/* Mark source buffer as complete */
4218c2ecf20Sopenharmony_ci	if (dst_frame_status != S5P_FIMV_DEC_STATUS_DISPLAY_ONLY
4228c2ecf20Sopenharmony_ci		&& !list_empty(&ctx->src_queue)) {
4238c2ecf20Sopenharmony_ci		src_buf = list_entry(ctx->src_queue.next, struct s5p_mfc_buf,
4248c2ecf20Sopenharmony_ci								list);
4258c2ecf20Sopenharmony_ci		ctx->consumed_stream += s5p_mfc_hw_call(dev->mfc_ops,
4268c2ecf20Sopenharmony_ci						get_consumed_stream, dev);
4278c2ecf20Sopenharmony_ci		if (ctx->codec_mode != S5P_MFC_CODEC_H264_DEC &&
4288c2ecf20Sopenharmony_ci			ctx->codec_mode != S5P_MFC_CODEC_VP8_DEC &&
4298c2ecf20Sopenharmony_ci			ctx->consumed_stream + STUFF_BYTE <
4308c2ecf20Sopenharmony_ci			src_buf->b->vb2_buf.planes[0].bytesused) {
4318c2ecf20Sopenharmony_ci			/* Run MFC again on the same buffer */
4328c2ecf20Sopenharmony_ci			mfc_debug(2, "Running again the same buffer\n");
4338c2ecf20Sopenharmony_ci			ctx->after_packed_pb = 1;
4348c2ecf20Sopenharmony_ci		} else {
4358c2ecf20Sopenharmony_ci			mfc_debug(2, "MFC needs next buffer\n");
4368c2ecf20Sopenharmony_ci			ctx->consumed_stream = 0;
4378c2ecf20Sopenharmony_ci			if (src_buf->flags & MFC_BUF_FLAG_EOS)
4388c2ecf20Sopenharmony_ci				ctx->state = MFCINST_FINISHING;
4398c2ecf20Sopenharmony_ci			list_del(&src_buf->list);
4408c2ecf20Sopenharmony_ci			ctx->src_queue_cnt--;
4418c2ecf20Sopenharmony_ci			if (s5p_mfc_hw_call(dev->mfc_ops, err_dec, err) > 0)
4428c2ecf20Sopenharmony_ci				vb2_buffer_done(&src_buf->b->vb2_buf,
4438c2ecf20Sopenharmony_ci						VB2_BUF_STATE_ERROR);
4448c2ecf20Sopenharmony_ci			else
4458c2ecf20Sopenharmony_ci				vb2_buffer_done(&src_buf->b->vb2_buf,
4468c2ecf20Sopenharmony_ci						VB2_BUF_STATE_DONE);
4478c2ecf20Sopenharmony_ci		}
4488c2ecf20Sopenharmony_ci	}
4498c2ecf20Sopenharmony_cileave_handle_frame:
4508c2ecf20Sopenharmony_ci	if ((ctx->src_queue_cnt == 0 && ctx->state != MFCINST_FINISHING)
4518c2ecf20Sopenharmony_ci				    || ctx->dst_queue_cnt < ctx->pb_count)
4528c2ecf20Sopenharmony_ci		clear_work_bit(ctx);
4538c2ecf20Sopenharmony_ci	s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
4548c2ecf20Sopenharmony_ci	wake_up_ctx(ctx, reason, err);
4558c2ecf20Sopenharmony_ci	WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
4568c2ecf20Sopenharmony_ci	s5p_mfc_clock_off();
4578c2ecf20Sopenharmony_ci	/* if suspending, wake up device and do not try_run again*/
4588c2ecf20Sopenharmony_ci	if (test_bit(0, &dev->enter_suspend))
4598c2ecf20Sopenharmony_ci		wake_up_dev(dev, reason, err);
4608c2ecf20Sopenharmony_ci	else
4618c2ecf20Sopenharmony_ci		s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
4628c2ecf20Sopenharmony_ci}
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ci/* Error handling for interrupt */
4658c2ecf20Sopenharmony_cistatic void s5p_mfc_handle_error(struct s5p_mfc_dev *dev,
4668c2ecf20Sopenharmony_ci		struct s5p_mfc_ctx *ctx, unsigned int reason, unsigned int err)
4678c2ecf20Sopenharmony_ci{
4688c2ecf20Sopenharmony_ci	mfc_err("Interrupt Error: %08x\n", err);
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_ci	if (ctx) {
4718c2ecf20Sopenharmony_ci		/* Error recovery is dependent on the state of context */
4728c2ecf20Sopenharmony_ci		switch (ctx->state) {
4738c2ecf20Sopenharmony_ci		case MFCINST_RES_CHANGE_INIT:
4748c2ecf20Sopenharmony_ci		case MFCINST_RES_CHANGE_FLUSH:
4758c2ecf20Sopenharmony_ci		case MFCINST_RES_CHANGE_END:
4768c2ecf20Sopenharmony_ci		case MFCINST_FINISHING:
4778c2ecf20Sopenharmony_ci		case MFCINST_FINISHED:
4788c2ecf20Sopenharmony_ci		case MFCINST_RUNNING:
4798c2ecf20Sopenharmony_ci			/* It is highly probable that an error occurred
4808c2ecf20Sopenharmony_ci			 * while decoding a frame */
4818c2ecf20Sopenharmony_ci			clear_work_bit(ctx);
4828c2ecf20Sopenharmony_ci			ctx->state = MFCINST_ERROR;
4838c2ecf20Sopenharmony_ci			/* Mark all dst buffers as having an error */
4848c2ecf20Sopenharmony_ci			s5p_mfc_cleanup_queue(&ctx->dst_queue, &ctx->vq_dst);
4858c2ecf20Sopenharmony_ci			/* Mark all src buffers as having an error */
4868c2ecf20Sopenharmony_ci			s5p_mfc_cleanup_queue(&ctx->src_queue, &ctx->vq_src);
4878c2ecf20Sopenharmony_ci			wake_up_ctx(ctx, reason, err);
4888c2ecf20Sopenharmony_ci			break;
4898c2ecf20Sopenharmony_ci		default:
4908c2ecf20Sopenharmony_ci			clear_work_bit(ctx);
4918c2ecf20Sopenharmony_ci			ctx->state = MFCINST_ERROR;
4928c2ecf20Sopenharmony_ci			wake_up_ctx(ctx, reason, err);
4938c2ecf20Sopenharmony_ci			break;
4948c2ecf20Sopenharmony_ci		}
4958c2ecf20Sopenharmony_ci	}
4968c2ecf20Sopenharmony_ci	WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
4978c2ecf20Sopenharmony_ci	s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
4988c2ecf20Sopenharmony_ci	s5p_mfc_clock_off();
4998c2ecf20Sopenharmony_ci	wake_up_dev(dev, reason, err);
5008c2ecf20Sopenharmony_ci}
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ci/* Header parsing interrupt handling */
5038c2ecf20Sopenharmony_cistatic void s5p_mfc_handle_seq_done(struct s5p_mfc_ctx *ctx,
5048c2ecf20Sopenharmony_ci				 unsigned int reason, unsigned int err)
5058c2ecf20Sopenharmony_ci{
5068c2ecf20Sopenharmony_ci	struct s5p_mfc_dev *dev;
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_ci	if (!ctx)
5098c2ecf20Sopenharmony_ci		return;
5108c2ecf20Sopenharmony_ci	dev = ctx->dev;
5118c2ecf20Sopenharmony_ci	if (ctx->c_ops->post_seq_start) {
5128c2ecf20Sopenharmony_ci		if (ctx->c_ops->post_seq_start(ctx))
5138c2ecf20Sopenharmony_ci			mfc_err("post_seq_start() failed\n");
5148c2ecf20Sopenharmony_ci	} else {
5158c2ecf20Sopenharmony_ci		ctx->img_width = s5p_mfc_hw_call(dev->mfc_ops, get_img_width,
5168c2ecf20Sopenharmony_ci				dev);
5178c2ecf20Sopenharmony_ci		ctx->img_height = s5p_mfc_hw_call(dev->mfc_ops, get_img_height,
5188c2ecf20Sopenharmony_ci				dev);
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_ci		s5p_mfc_hw_call(dev->mfc_ops, dec_calc_dpb_size, ctx);
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_ci		ctx->pb_count = s5p_mfc_hw_call(dev->mfc_ops, get_dpb_count,
5238c2ecf20Sopenharmony_ci				dev);
5248c2ecf20Sopenharmony_ci		ctx->mv_count = s5p_mfc_hw_call(dev->mfc_ops, get_mv_count,
5258c2ecf20Sopenharmony_ci				dev);
5268c2ecf20Sopenharmony_ci		if (FW_HAS_E_MIN_SCRATCH_BUF(dev))
5278c2ecf20Sopenharmony_ci			ctx->scratch_buf_size = s5p_mfc_hw_call(dev->mfc_ops,
5288c2ecf20Sopenharmony_ci						get_min_scratch_buf_size, dev);
5298c2ecf20Sopenharmony_ci		if (ctx->img_width == 0 || ctx->img_height == 0)
5308c2ecf20Sopenharmony_ci			ctx->state = MFCINST_ERROR;
5318c2ecf20Sopenharmony_ci		else
5328c2ecf20Sopenharmony_ci			ctx->state = MFCINST_HEAD_PARSED;
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci		if ((ctx->codec_mode == S5P_MFC_CODEC_H264_DEC ||
5358c2ecf20Sopenharmony_ci			ctx->codec_mode == S5P_MFC_CODEC_H264_MVC_DEC) &&
5368c2ecf20Sopenharmony_ci				!list_empty(&ctx->src_queue)) {
5378c2ecf20Sopenharmony_ci			struct s5p_mfc_buf *src_buf;
5388c2ecf20Sopenharmony_ci			src_buf = list_entry(ctx->src_queue.next,
5398c2ecf20Sopenharmony_ci					struct s5p_mfc_buf, list);
5408c2ecf20Sopenharmony_ci			if (s5p_mfc_hw_call(dev->mfc_ops, get_consumed_stream,
5418c2ecf20Sopenharmony_ci						dev) <
5428c2ecf20Sopenharmony_ci					src_buf->b->vb2_buf.planes[0].bytesused)
5438c2ecf20Sopenharmony_ci				ctx->head_processed = 0;
5448c2ecf20Sopenharmony_ci			else
5458c2ecf20Sopenharmony_ci				ctx->head_processed = 1;
5468c2ecf20Sopenharmony_ci		} else {
5478c2ecf20Sopenharmony_ci			ctx->head_processed = 1;
5488c2ecf20Sopenharmony_ci		}
5498c2ecf20Sopenharmony_ci	}
5508c2ecf20Sopenharmony_ci	s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
5518c2ecf20Sopenharmony_ci	clear_work_bit(ctx);
5528c2ecf20Sopenharmony_ci	WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
5538c2ecf20Sopenharmony_ci	s5p_mfc_clock_off();
5548c2ecf20Sopenharmony_ci	s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
5558c2ecf20Sopenharmony_ci	wake_up_ctx(ctx, reason, err);
5568c2ecf20Sopenharmony_ci}
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci/* Header parsing interrupt handling */
5598c2ecf20Sopenharmony_cistatic void s5p_mfc_handle_init_buffers(struct s5p_mfc_ctx *ctx,
5608c2ecf20Sopenharmony_ci				 unsigned int reason, unsigned int err)
5618c2ecf20Sopenharmony_ci{
5628c2ecf20Sopenharmony_ci	struct s5p_mfc_buf *src_buf;
5638c2ecf20Sopenharmony_ci	struct s5p_mfc_dev *dev;
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci	if (!ctx)
5668c2ecf20Sopenharmony_ci		return;
5678c2ecf20Sopenharmony_ci	dev = ctx->dev;
5688c2ecf20Sopenharmony_ci	s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
5698c2ecf20Sopenharmony_ci	ctx->int_type = reason;
5708c2ecf20Sopenharmony_ci	ctx->int_err = err;
5718c2ecf20Sopenharmony_ci	ctx->int_cond = 1;
5728c2ecf20Sopenharmony_ci	clear_work_bit(ctx);
5738c2ecf20Sopenharmony_ci	if (err == 0) {
5748c2ecf20Sopenharmony_ci		ctx->state = MFCINST_RUNNING;
5758c2ecf20Sopenharmony_ci		if (!ctx->dpb_flush_flag && ctx->head_processed) {
5768c2ecf20Sopenharmony_ci			if (!list_empty(&ctx->src_queue)) {
5778c2ecf20Sopenharmony_ci				src_buf = list_entry(ctx->src_queue.next,
5788c2ecf20Sopenharmony_ci					     struct s5p_mfc_buf, list);
5798c2ecf20Sopenharmony_ci				list_del(&src_buf->list);
5808c2ecf20Sopenharmony_ci				ctx->src_queue_cnt--;
5818c2ecf20Sopenharmony_ci				vb2_buffer_done(&src_buf->b->vb2_buf,
5828c2ecf20Sopenharmony_ci						VB2_BUF_STATE_DONE);
5838c2ecf20Sopenharmony_ci			}
5848c2ecf20Sopenharmony_ci		} else {
5858c2ecf20Sopenharmony_ci			ctx->dpb_flush_flag = 0;
5868c2ecf20Sopenharmony_ci		}
5878c2ecf20Sopenharmony_ci		WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_ci		s5p_mfc_clock_off();
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci		wake_up(&ctx->queue);
5928c2ecf20Sopenharmony_ci		s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
5938c2ecf20Sopenharmony_ci	} else {
5948c2ecf20Sopenharmony_ci		WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci		s5p_mfc_clock_off();
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_ci		wake_up(&ctx->queue);
5998c2ecf20Sopenharmony_ci	}
6008c2ecf20Sopenharmony_ci}
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_cistatic void s5p_mfc_handle_stream_complete(struct s5p_mfc_ctx *ctx)
6038c2ecf20Sopenharmony_ci{
6048c2ecf20Sopenharmony_ci	struct s5p_mfc_dev *dev = ctx->dev;
6058c2ecf20Sopenharmony_ci	struct s5p_mfc_buf *mb_entry;
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_ci	mfc_debug(2, "Stream completed\n");
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_ci	ctx->state = MFCINST_FINISHED;
6108c2ecf20Sopenharmony_ci
6118c2ecf20Sopenharmony_ci	if (!list_empty(&ctx->dst_queue)) {
6128c2ecf20Sopenharmony_ci		mb_entry = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf,
6138c2ecf20Sopenharmony_ci									list);
6148c2ecf20Sopenharmony_ci		list_del(&mb_entry->list);
6158c2ecf20Sopenharmony_ci		ctx->dst_queue_cnt--;
6168c2ecf20Sopenharmony_ci		vb2_set_plane_payload(&mb_entry->b->vb2_buf, 0, 0);
6178c2ecf20Sopenharmony_ci		vb2_buffer_done(&mb_entry->b->vb2_buf, VB2_BUF_STATE_DONE);
6188c2ecf20Sopenharmony_ci	}
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_ci	clear_work_bit(ctx);
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_ci	WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_ci	s5p_mfc_clock_off();
6258c2ecf20Sopenharmony_ci	wake_up(&ctx->queue);
6268c2ecf20Sopenharmony_ci	s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
6278c2ecf20Sopenharmony_ci}
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_ci/* Interrupt processing */
6308c2ecf20Sopenharmony_cistatic irqreturn_t s5p_mfc_irq(int irq, void *priv)
6318c2ecf20Sopenharmony_ci{
6328c2ecf20Sopenharmony_ci	struct s5p_mfc_dev *dev = priv;
6338c2ecf20Sopenharmony_ci	struct s5p_mfc_ctx *ctx;
6348c2ecf20Sopenharmony_ci	unsigned int reason;
6358c2ecf20Sopenharmony_ci	unsigned int err;
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_ci	mfc_debug_enter();
6388c2ecf20Sopenharmony_ci	/* Reset the timeout watchdog */
6398c2ecf20Sopenharmony_ci	atomic_set(&dev->watchdog_cnt, 0);
6408c2ecf20Sopenharmony_ci	spin_lock(&dev->irqlock);
6418c2ecf20Sopenharmony_ci	ctx = dev->ctx[dev->curr_ctx];
6428c2ecf20Sopenharmony_ci	/* Get the reason of interrupt and the error code */
6438c2ecf20Sopenharmony_ci	reason = s5p_mfc_hw_call(dev->mfc_ops, get_int_reason, dev);
6448c2ecf20Sopenharmony_ci	err = s5p_mfc_hw_call(dev->mfc_ops, get_int_err, dev);
6458c2ecf20Sopenharmony_ci	mfc_debug(1, "Int reason: %d (err: %08x)\n", reason, err);
6468c2ecf20Sopenharmony_ci	switch (reason) {
6478c2ecf20Sopenharmony_ci	case S5P_MFC_R2H_CMD_ERR_RET:
6488c2ecf20Sopenharmony_ci		/* An error has occurred */
6498c2ecf20Sopenharmony_ci		if (ctx->state == MFCINST_RUNNING &&
6508c2ecf20Sopenharmony_ci			(s5p_mfc_hw_call(dev->mfc_ops, err_dec, err) >=
6518c2ecf20Sopenharmony_ci				dev->warn_start ||
6528c2ecf20Sopenharmony_ci				err == S5P_FIMV_ERR_NO_VALID_SEQ_HDR ||
6538c2ecf20Sopenharmony_ci				err == S5P_FIMV_ERR_INCOMPLETE_FRAME ||
6548c2ecf20Sopenharmony_ci				err == S5P_FIMV_ERR_TIMEOUT))
6558c2ecf20Sopenharmony_ci			s5p_mfc_handle_frame(ctx, reason, err);
6568c2ecf20Sopenharmony_ci		else
6578c2ecf20Sopenharmony_ci			s5p_mfc_handle_error(dev, ctx, reason, err);
6588c2ecf20Sopenharmony_ci		clear_bit(0, &dev->enter_suspend);
6598c2ecf20Sopenharmony_ci		break;
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_ci	case S5P_MFC_R2H_CMD_SLICE_DONE_RET:
6628c2ecf20Sopenharmony_ci	case S5P_MFC_R2H_CMD_FIELD_DONE_RET:
6638c2ecf20Sopenharmony_ci	case S5P_MFC_R2H_CMD_FRAME_DONE_RET:
6648c2ecf20Sopenharmony_ci		if (ctx->c_ops->post_frame_start) {
6658c2ecf20Sopenharmony_ci			if (ctx->c_ops->post_frame_start(ctx))
6668c2ecf20Sopenharmony_ci				mfc_err("post_frame_start() failed\n");
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_ci			if (ctx->state == MFCINST_FINISHING &&
6698c2ecf20Sopenharmony_ci						list_empty(&ctx->ref_queue)) {
6708c2ecf20Sopenharmony_ci				s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
6718c2ecf20Sopenharmony_ci				s5p_mfc_handle_stream_complete(ctx);
6728c2ecf20Sopenharmony_ci				break;
6738c2ecf20Sopenharmony_ci			}
6748c2ecf20Sopenharmony_ci			s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
6758c2ecf20Sopenharmony_ci			WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
6768c2ecf20Sopenharmony_ci			s5p_mfc_clock_off();
6778c2ecf20Sopenharmony_ci			wake_up_ctx(ctx, reason, err);
6788c2ecf20Sopenharmony_ci			s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
6798c2ecf20Sopenharmony_ci		} else {
6808c2ecf20Sopenharmony_ci			s5p_mfc_handle_frame(ctx, reason, err);
6818c2ecf20Sopenharmony_ci		}
6828c2ecf20Sopenharmony_ci		break;
6838c2ecf20Sopenharmony_ci
6848c2ecf20Sopenharmony_ci	case S5P_MFC_R2H_CMD_SEQ_DONE_RET:
6858c2ecf20Sopenharmony_ci		s5p_mfc_handle_seq_done(ctx, reason, err);
6868c2ecf20Sopenharmony_ci		break;
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_ci	case S5P_MFC_R2H_CMD_OPEN_INSTANCE_RET:
6898c2ecf20Sopenharmony_ci		ctx->inst_no = s5p_mfc_hw_call(dev->mfc_ops, get_inst_no, dev);
6908c2ecf20Sopenharmony_ci		ctx->state = MFCINST_GOT_INST;
6918c2ecf20Sopenharmony_ci		goto irq_cleanup_hw;
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_ci	case S5P_MFC_R2H_CMD_CLOSE_INSTANCE_RET:
6948c2ecf20Sopenharmony_ci		ctx->inst_no = MFC_NO_INSTANCE_SET;
6958c2ecf20Sopenharmony_ci		ctx->state = MFCINST_FREE;
6968c2ecf20Sopenharmony_ci		goto irq_cleanup_hw;
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_ci	case S5P_MFC_R2H_CMD_SYS_INIT_RET:
6998c2ecf20Sopenharmony_ci	case S5P_MFC_R2H_CMD_FW_STATUS_RET:
7008c2ecf20Sopenharmony_ci	case S5P_MFC_R2H_CMD_SLEEP_RET:
7018c2ecf20Sopenharmony_ci	case S5P_MFC_R2H_CMD_WAKEUP_RET:
7028c2ecf20Sopenharmony_ci		if (ctx)
7038c2ecf20Sopenharmony_ci			clear_work_bit(ctx);
7048c2ecf20Sopenharmony_ci		s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
7058c2ecf20Sopenharmony_ci		clear_bit(0, &dev->hw_lock);
7068c2ecf20Sopenharmony_ci		clear_bit(0, &dev->enter_suspend);
7078c2ecf20Sopenharmony_ci		wake_up_dev(dev, reason, err);
7088c2ecf20Sopenharmony_ci		break;
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci	case S5P_MFC_R2H_CMD_INIT_BUFFERS_RET:
7118c2ecf20Sopenharmony_ci		s5p_mfc_handle_init_buffers(ctx, reason, err);
7128c2ecf20Sopenharmony_ci		break;
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci	case S5P_MFC_R2H_CMD_COMPLETE_SEQ_RET:
7158c2ecf20Sopenharmony_ci		s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
7168c2ecf20Sopenharmony_ci		ctx->int_type = reason;
7178c2ecf20Sopenharmony_ci		ctx->int_err = err;
7188c2ecf20Sopenharmony_ci		s5p_mfc_handle_stream_complete(ctx);
7198c2ecf20Sopenharmony_ci		break;
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_ci	case S5P_MFC_R2H_CMD_DPB_FLUSH_RET:
7228c2ecf20Sopenharmony_ci		ctx->state = MFCINST_RUNNING;
7238c2ecf20Sopenharmony_ci		goto irq_cleanup_hw;
7248c2ecf20Sopenharmony_ci
7258c2ecf20Sopenharmony_ci	default:
7268c2ecf20Sopenharmony_ci		mfc_debug(2, "Unknown int reason\n");
7278c2ecf20Sopenharmony_ci		s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
7288c2ecf20Sopenharmony_ci	}
7298c2ecf20Sopenharmony_ci	spin_unlock(&dev->irqlock);
7308c2ecf20Sopenharmony_ci	mfc_debug_leave();
7318c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
7328c2ecf20Sopenharmony_ciirq_cleanup_hw:
7338c2ecf20Sopenharmony_ci	s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
7348c2ecf20Sopenharmony_ci	ctx->int_type = reason;
7358c2ecf20Sopenharmony_ci	ctx->int_err = err;
7368c2ecf20Sopenharmony_ci	ctx->int_cond = 1;
7378c2ecf20Sopenharmony_ci	if (test_and_clear_bit(0, &dev->hw_lock) == 0)
7388c2ecf20Sopenharmony_ci		mfc_err("Failed to unlock hw\n");
7398c2ecf20Sopenharmony_ci
7408c2ecf20Sopenharmony_ci	s5p_mfc_clock_off();
7418c2ecf20Sopenharmony_ci	clear_work_bit(ctx);
7428c2ecf20Sopenharmony_ci	wake_up(&ctx->queue);
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_ci	s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
7458c2ecf20Sopenharmony_ci	spin_unlock(&dev->irqlock);
7468c2ecf20Sopenharmony_ci	mfc_debug(2, "Exit via irq_cleanup_hw\n");
7478c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
7488c2ecf20Sopenharmony_ci}
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_ci/* Open an MFC node */
7518c2ecf20Sopenharmony_cistatic int s5p_mfc_open(struct file *file)
7528c2ecf20Sopenharmony_ci{
7538c2ecf20Sopenharmony_ci	struct video_device *vdev = video_devdata(file);
7548c2ecf20Sopenharmony_ci	struct s5p_mfc_dev *dev = video_drvdata(file);
7558c2ecf20Sopenharmony_ci	struct s5p_mfc_ctx *ctx = NULL;
7568c2ecf20Sopenharmony_ci	struct vb2_queue *q;
7578c2ecf20Sopenharmony_ci	int ret = 0;
7588c2ecf20Sopenharmony_ci
7598c2ecf20Sopenharmony_ci	mfc_debug_enter();
7608c2ecf20Sopenharmony_ci	if (mutex_lock_interruptible(&dev->mfc_mutex))
7618c2ecf20Sopenharmony_ci		return -ERESTARTSYS;
7628c2ecf20Sopenharmony_ci	dev->num_inst++;	/* It is guarded by mfc_mutex in vfd */
7638c2ecf20Sopenharmony_ci	/* Allocate memory for context */
7648c2ecf20Sopenharmony_ci	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
7658c2ecf20Sopenharmony_ci	if (!ctx) {
7668c2ecf20Sopenharmony_ci		ret = -ENOMEM;
7678c2ecf20Sopenharmony_ci		goto err_alloc;
7688c2ecf20Sopenharmony_ci	}
7698c2ecf20Sopenharmony_ci	init_waitqueue_head(&ctx->queue);
7708c2ecf20Sopenharmony_ci	v4l2_fh_init(&ctx->fh, vdev);
7718c2ecf20Sopenharmony_ci	file->private_data = &ctx->fh;
7728c2ecf20Sopenharmony_ci	v4l2_fh_add(&ctx->fh);
7738c2ecf20Sopenharmony_ci	ctx->dev = dev;
7748c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&ctx->src_queue);
7758c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&ctx->dst_queue);
7768c2ecf20Sopenharmony_ci	ctx->src_queue_cnt = 0;
7778c2ecf20Sopenharmony_ci	ctx->dst_queue_cnt = 0;
7788c2ecf20Sopenharmony_ci	/* Get context number */
7798c2ecf20Sopenharmony_ci	ctx->num = 0;
7808c2ecf20Sopenharmony_ci	while (dev->ctx[ctx->num]) {
7818c2ecf20Sopenharmony_ci		ctx->num++;
7828c2ecf20Sopenharmony_ci		if (ctx->num >= MFC_NUM_CONTEXTS) {
7838c2ecf20Sopenharmony_ci			mfc_debug(2, "Too many open contexts\n");
7848c2ecf20Sopenharmony_ci			ret = -EBUSY;
7858c2ecf20Sopenharmony_ci			goto err_no_ctx;
7868c2ecf20Sopenharmony_ci		}
7878c2ecf20Sopenharmony_ci	}
7888c2ecf20Sopenharmony_ci	/* Mark context as idle */
7898c2ecf20Sopenharmony_ci	clear_work_bit_irqsave(ctx);
7908c2ecf20Sopenharmony_ci	dev->ctx[ctx->num] = ctx;
7918c2ecf20Sopenharmony_ci	if (vdev == dev->vfd_dec) {
7928c2ecf20Sopenharmony_ci		ctx->type = MFCINST_DECODER;
7938c2ecf20Sopenharmony_ci		ctx->c_ops = get_dec_codec_ops();
7948c2ecf20Sopenharmony_ci		s5p_mfc_dec_init(ctx);
7958c2ecf20Sopenharmony_ci		/* Setup ctrl handler */
7968c2ecf20Sopenharmony_ci		ret = s5p_mfc_dec_ctrls_setup(ctx);
7978c2ecf20Sopenharmony_ci		if (ret) {
7988c2ecf20Sopenharmony_ci			mfc_err("Failed to setup mfc controls\n");
7998c2ecf20Sopenharmony_ci			goto err_ctrls_setup;
8008c2ecf20Sopenharmony_ci		}
8018c2ecf20Sopenharmony_ci	} else if (vdev == dev->vfd_enc) {
8028c2ecf20Sopenharmony_ci		ctx->type = MFCINST_ENCODER;
8038c2ecf20Sopenharmony_ci		ctx->c_ops = get_enc_codec_ops();
8048c2ecf20Sopenharmony_ci		/* only for encoder */
8058c2ecf20Sopenharmony_ci		INIT_LIST_HEAD(&ctx->ref_queue);
8068c2ecf20Sopenharmony_ci		ctx->ref_queue_cnt = 0;
8078c2ecf20Sopenharmony_ci		s5p_mfc_enc_init(ctx);
8088c2ecf20Sopenharmony_ci		/* Setup ctrl handler */
8098c2ecf20Sopenharmony_ci		ret = s5p_mfc_enc_ctrls_setup(ctx);
8108c2ecf20Sopenharmony_ci		if (ret) {
8118c2ecf20Sopenharmony_ci			mfc_err("Failed to setup mfc controls\n");
8128c2ecf20Sopenharmony_ci			goto err_ctrls_setup;
8138c2ecf20Sopenharmony_ci		}
8148c2ecf20Sopenharmony_ci	} else {
8158c2ecf20Sopenharmony_ci		ret = -ENOENT;
8168c2ecf20Sopenharmony_ci		goto err_bad_node;
8178c2ecf20Sopenharmony_ci	}
8188c2ecf20Sopenharmony_ci	ctx->fh.ctrl_handler = &ctx->ctrl_handler;
8198c2ecf20Sopenharmony_ci	ctx->inst_no = MFC_NO_INSTANCE_SET;
8208c2ecf20Sopenharmony_ci	/* Load firmware if this is the first instance */
8218c2ecf20Sopenharmony_ci	if (dev->num_inst == 1) {
8228c2ecf20Sopenharmony_ci		dev->watchdog_timer.expires = jiffies +
8238c2ecf20Sopenharmony_ci					msecs_to_jiffies(MFC_WATCHDOG_INTERVAL);
8248c2ecf20Sopenharmony_ci		add_timer(&dev->watchdog_timer);
8258c2ecf20Sopenharmony_ci		ret = s5p_mfc_power_on();
8268c2ecf20Sopenharmony_ci		if (ret < 0) {
8278c2ecf20Sopenharmony_ci			mfc_err("power on failed\n");
8288c2ecf20Sopenharmony_ci			goto err_pwr_enable;
8298c2ecf20Sopenharmony_ci		}
8308c2ecf20Sopenharmony_ci		s5p_mfc_clock_on();
8318c2ecf20Sopenharmony_ci		ret = s5p_mfc_load_firmware(dev);
8328c2ecf20Sopenharmony_ci		if (ret) {
8338c2ecf20Sopenharmony_ci			s5p_mfc_clock_off();
8348c2ecf20Sopenharmony_ci			goto err_load_fw;
8358c2ecf20Sopenharmony_ci		}
8368c2ecf20Sopenharmony_ci		/* Init the FW */
8378c2ecf20Sopenharmony_ci		ret = s5p_mfc_init_hw(dev);
8388c2ecf20Sopenharmony_ci		s5p_mfc_clock_off();
8398c2ecf20Sopenharmony_ci		if (ret)
8408c2ecf20Sopenharmony_ci			goto err_init_hw;
8418c2ecf20Sopenharmony_ci	}
8428c2ecf20Sopenharmony_ci	/* Init videobuf2 queue for CAPTURE */
8438c2ecf20Sopenharmony_ci	q = &ctx->vq_dst;
8448c2ecf20Sopenharmony_ci	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
8458c2ecf20Sopenharmony_ci	q->drv_priv = &ctx->fh;
8468c2ecf20Sopenharmony_ci	q->lock = &dev->mfc_mutex;
8478c2ecf20Sopenharmony_ci	if (vdev == dev->vfd_dec) {
8488c2ecf20Sopenharmony_ci		q->io_modes = VB2_MMAP;
8498c2ecf20Sopenharmony_ci		q->ops = get_dec_queue_ops();
8508c2ecf20Sopenharmony_ci	} else if (vdev == dev->vfd_enc) {
8518c2ecf20Sopenharmony_ci		q->io_modes = VB2_MMAP | VB2_USERPTR;
8528c2ecf20Sopenharmony_ci		q->ops = get_enc_queue_ops();
8538c2ecf20Sopenharmony_ci	} else {
8548c2ecf20Sopenharmony_ci		ret = -ENOENT;
8558c2ecf20Sopenharmony_ci		goto err_queue_init;
8568c2ecf20Sopenharmony_ci	}
8578c2ecf20Sopenharmony_ci	/*
8588c2ecf20Sopenharmony_ci	 * We'll do mostly sequential access, so sacrifice TLB efficiency for
8598c2ecf20Sopenharmony_ci	 * faster allocation.
8608c2ecf20Sopenharmony_ci	 */
8618c2ecf20Sopenharmony_ci	q->dma_attrs = DMA_ATTR_ALLOC_SINGLE_PAGES;
8628c2ecf20Sopenharmony_ci	q->mem_ops = &vb2_dma_contig_memops;
8638c2ecf20Sopenharmony_ci	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
8648c2ecf20Sopenharmony_ci	ret = vb2_queue_init(q);
8658c2ecf20Sopenharmony_ci	if (ret) {
8668c2ecf20Sopenharmony_ci		mfc_err("Failed to initialize videobuf2 queue(capture)\n");
8678c2ecf20Sopenharmony_ci		goto err_queue_init;
8688c2ecf20Sopenharmony_ci	}
8698c2ecf20Sopenharmony_ci	/* Init videobuf2 queue for OUTPUT */
8708c2ecf20Sopenharmony_ci	q = &ctx->vq_src;
8718c2ecf20Sopenharmony_ci	q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
8728c2ecf20Sopenharmony_ci	q->drv_priv = &ctx->fh;
8738c2ecf20Sopenharmony_ci	q->lock = &dev->mfc_mutex;
8748c2ecf20Sopenharmony_ci	if (vdev == dev->vfd_dec) {
8758c2ecf20Sopenharmony_ci		q->io_modes = VB2_MMAP;
8768c2ecf20Sopenharmony_ci		q->ops = get_dec_queue_ops();
8778c2ecf20Sopenharmony_ci	} else if (vdev == dev->vfd_enc) {
8788c2ecf20Sopenharmony_ci		q->io_modes = VB2_MMAP | VB2_USERPTR;
8798c2ecf20Sopenharmony_ci		q->ops = get_enc_queue_ops();
8808c2ecf20Sopenharmony_ci	} else {
8818c2ecf20Sopenharmony_ci		ret = -ENOENT;
8828c2ecf20Sopenharmony_ci		goto err_queue_init;
8838c2ecf20Sopenharmony_ci	}
8848c2ecf20Sopenharmony_ci	/* One way to indicate end-of-stream for MFC is to set the
8858c2ecf20Sopenharmony_ci	 * bytesused == 0. However by default videobuf2 handles bytesused
8868c2ecf20Sopenharmony_ci	 * equal to 0 as a special case and changes its value to the size
8878c2ecf20Sopenharmony_ci	 * of the buffer. Set the allow_zero_bytesused flag so that videobuf2
8888c2ecf20Sopenharmony_ci	 * will keep the value of bytesused intact.
8898c2ecf20Sopenharmony_ci	 */
8908c2ecf20Sopenharmony_ci	q->allow_zero_bytesused = 1;
8918c2ecf20Sopenharmony_ci
8928c2ecf20Sopenharmony_ci	/*
8938c2ecf20Sopenharmony_ci	 * We'll do mostly sequential access, so sacrifice TLB efficiency for
8948c2ecf20Sopenharmony_ci	 * faster allocation.
8958c2ecf20Sopenharmony_ci	 */
8968c2ecf20Sopenharmony_ci	q->dma_attrs = DMA_ATTR_ALLOC_SINGLE_PAGES;
8978c2ecf20Sopenharmony_ci	q->mem_ops = &vb2_dma_contig_memops;
8988c2ecf20Sopenharmony_ci	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
8998c2ecf20Sopenharmony_ci	ret = vb2_queue_init(q);
9008c2ecf20Sopenharmony_ci	if (ret) {
9018c2ecf20Sopenharmony_ci		mfc_err("Failed to initialize videobuf2 queue(output)\n");
9028c2ecf20Sopenharmony_ci		goto err_queue_init;
9038c2ecf20Sopenharmony_ci	}
9048c2ecf20Sopenharmony_ci	mutex_unlock(&dev->mfc_mutex);
9058c2ecf20Sopenharmony_ci	mfc_debug_leave();
9068c2ecf20Sopenharmony_ci	return ret;
9078c2ecf20Sopenharmony_ci	/* Deinit when failure occurred */
9088c2ecf20Sopenharmony_cierr_queue_init:
9098c2ecf20Sopenharmony_ci	if (dev->num_inst == 1)
9108c2ecf20Sopenharmony_ci		s5p_mfc_deinit_hw(dev);
9118c2ecf20Sopenharmony_cierr_init_hw:
9128c2ecf20Sopenharmony_cierr_load_fw:
9138c2ecf20Sopenharmony_cierr_pwr_enable:
9148c2ecf20Sopenharmony_ci	if (dev->num_inst == 1) {
9158c2ecf20Sopenharmony_ci		if (s5p_mfc_power_off() < 0)
9168c2ecf20Sopenharmony_ci			mfc_err("power off failed\n");
9178c2ecf20Sopenharmony_ci		del_timer_sync(&dev->watchdog_timer);
9188c2ecf20Sopenharmony_ci	}
9198c2ecf20Sopenharmony_cierr_ctrls_setup:
9208c2ecf20Sopenharmony_ci	s5p_mfc_dec_ctrls_delete(ctx);
9218c2ecf20Sopenharmony_cierr_bad_node:
9228c2ecf20Sopenharmony_ci	dev->ctx[ctx->num] = NULL;
9238c2ecf20Sopenharmony_cierr_no_ctx:
9248c2ecf20Sopenharmony_ci	v4l2_fh_del(&ctx->fh);
9258c2ecf20Sopenharmony_ci	v4l2_fh_exit(&ctx->fh);
9268c2ecf20Sopenharmony_ci	kfree(ctx);
9278c2ecf20Sopenharmony_cierr_alloc:
9288c2ecf20Sopenharmony_ci	dev->num_inst--;
9298c2ecf20Sopenharmony_ci	mutex_unlock(&dev->mfc_mutex);
9308c2ecf20Sopenharmony_ci	mfc_debug_leave();
9318c2ecf20Sopenharmony_ci	return ret;
9328c2ecf20Sopenharmony_ci}
9338c2ecf20Sopenharmony_ci
9348c2ecf20Sopenharmony_ci/* Release MFC context */
9358c2ecf20Sopenharmony_cistatic int s5p_mfc_release(struct file *file)
9368c2ecf20Sopenharmony_ci{
9378c2ecf20Sopenharmony_ci	struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
9388c2ecf20Sopenharmony_ci	struct s5p_mfc_dev *dev = ctx->dev;
9398c2ecf20Sopenharmony_ci
9408c2ecf20Sopenharmony_ci	/* if dev is null, do cleanup that doesn't need dev */
9418c2ecf20Sopenharmony_ci	mfc_debug_enter();
9428c2ecf20Sopenharmony_ci	if (dev)
9438c2ecf20Sopenharmony_ci		mutex_lock(&dev->mfc_mutex);
9448c2ecf20Sopenharmony_ci	vb2_queue_release(&ctx->vq_src);
9458c2ecf20Sopenharmony_ci	vb2_queue_release(&ctx->vq_dst);
9468c2ecf20Sopenharmony_ci	if (dev) {
9478c2ecf20Sopenharmony_ci		s5p_mfc_clock_on();
9488c2ecf20Sopenharmony_ci
9498c2ecf20Sopenharmony_ci		/* Mark context as idle */
9508c2ecf20Sopenharmony_ci		clear_work_bit_irqsave(ctx);
9518c2ecf20Sopenharmony_ci		/*
9528c2ecf20Sopenharmony_ci		 * If instance was initialised and not yet freed,
9538c2ecf20Sopenharmony_ci		 * return instance and free resources
9548c2ecf20Sopenharmony_ci		*/
9558c2ecf20Sopenharmony_ci		if (ctx->state != MFCINST_FREE && ctx->state != MFCINST_INIT) {
9568c2ecf20Sopenharmony_ci			mfc_debug(2, "Has to free instance\n");
9578c2ecf20Sopenharmony_ci			s5p_mfc_close_mfc_inst(dev, ctx);
9588c2ecf20Sopenharmony_ci		}
9598c2ecf20Sopenharmony_ci		/* hardware locking scheme */
9608c2ecf20Sopenharmony_ci		if (dev->curr_ctx == ctx->num)
9618c2ecf20Sopenharmony_ci			clear_bit(0, &dev->hw_lock);
9628c2ecf20Sopenharmony_ci		dev->num_inst--;
9638c2ecf20Sopenharmony_ci		if (dev->num_inst == 0) {
9648c2ecf20Sopenharmony_ci			mfc_debug(2, "Last instance\n");
9658c2ecf20Sopenharmony_ci			s5p_mfc_deinit_hw(dev);
9668c2ecf20Sopenharmony_ci			del_timer_sync(&dev->watchdog_timer);
9678c2ecf20Sopenharmony_ci			s5p_mfc_clock_off();
9688c2ecf20Sopenharmony_ci			if (s5p_mfc_power_off() < 0)
9698c2ecf20Sopenharmony_ci				mfc_err("Power off failed\n");
9708c2ecf20Sopenharmony_ci		} else {
9718c2ecf20Sopenharmony_ci			mfc_debug(2, "Shutting down clock\n");
9728c2ecf20Sopenharmony_ci			s5p_mfc_clock_off();
9738c2ecf20Sopenharmony_ci		}
9748c2ecf20Sopenharmony_ci	}
9758c2ecf20Sopenharmony_ci	if (dev)
9768c2ecf20Sopenharmony_ci		dev->ctx[ctx->num] = NULL;
9778c2ecf20Sopenharmony_ci	s5p_mfc_dec_ctrls_delete(ctx);
9788c2ecf20Sopenharmony_ci	v4l2_fh_del(&ctx->fh);
9798c2ecf20Sopenharmony_ci	/* vdev is gone if dev is null */
9808c2ecf20Sopenharmony_ci	if (dev)
9818c2ecf20Sopenharmony_ci		v4l2_fh_exit(&ctx->fh);
9828c2ecf20Sopenharmony_ci	kfree(ctx);
9838c2ecf20Sopenharmony_ci	mfc_debug_leave();
9848c2ecf20Sopenharmony_ci	if (dev)
9858c2ecf20Sopenharmony_ci		mutex_unlock(&dev->mfc_mutex);
9868c2ecf20Sopenharmony_ci
9878c2ecf20Sopenharmony_ci	return 0;
9888c2ecf20Sopenharmony_ci}
9898c2ecf20Sopenharmony_ci
9908c2ecf20Sopenharmony_ci/* Poll */
9918c2ecf20Sopenharmony_cistatic __poll_t s5p_mfc_poll(struct file *file,
9928c2ecf20Sopenharmony_ci				 struct poll_table_struct *wait)
9938c2ecf20Sopenharmony_ci{
9948c2ecf20Sopenharmony_ci	struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
9958c2ecf20Sopenharmony_ci	struct s5p_mfc_dev *dev = ctx->dev;
9968c2ecf20Sopenharmony_ci	struct vb2_queue *src_q, *dst_q;
9978c2ecf20Sopenharmony_ci	struct vb2_buffer *src_vb = NULL, *dst_vb = NULL;
9988c2ecf20Sopenharmony_ci	__poll_t rc = 0;
9998c2ecf20Sopenharmony_ci	unsigned long flags;
10008c2ecf20Sopenharmony_ci
10018c2ecf20Sopenharmony_ci	mutex_lock(&dev->mfc_mutex);
10028c2ecf20Sopenharmony_ci	src_q = &ctx->vq_src;
10038c2ecf20Sopenharmony_ci	dst_q = &ctx->vq_dst;
10048c2ecf20Sopenharmony_ci	/*
10058c2ecf20Sopenharmony_ci	 * There has to be at least one buffer queued on each queued_list, which
10068c2ecf20Sopenharmony_ci	 * means either in driver already or waiting for driver to claim it
10078c2ecf20Sopenharmony_ci	 * and start processing.
10088c2ecf20Sopenharmony_ci	 */
10098c2ecf20Sopenharmony_ci	if ((!src_q->streaming || list_empty(&src_q->queued_list))
10108c2ecf20Sopenharmony_ci		&& (!dst_q->streaming || list_empty(&dst_q->queued_list))) {
10118c2ecf20Sopenharmony_ci		rc = EPOLLERR;
10128c2ecf20Sopenharmony_ci		goto end;
10138c2ecf20Sopenharmony_ci	}
10148c2ecf20Sopenharmony_ci	mutex_unlock(&dev->mfc_mutex);
10158c2ecf20Sopenharmony_ci	poll_wait(file, &ctx->fh.wait, wait);
10168c2ecf20Sopenharmony_ci	poll_wait(file, &src_q->done_wq, wait);
10178c2ecf20Sopenharmony_ci	poll_wait(file, &dst_q->done_wq, wait);
10188c2ecf20Sopenharmony_ci	mutex_lock(&dev->mfc_mutex);
10198c2ecf20Sopenharmony_ci	if (v4l2_event_pending(&ctx->fh))
10208c2ecf20Sopenharmony_ci		rc |= EPOLLPRI;
10218c2ecf20Sopenharmony_ci	spin_lock_irqsave(&src_q->done_lock, flags);
10228c2ecf20Sopenharmony_ci	if (!list_empty(&src_q->done_list))
10238c2ecf20Sopenharmony_ci		src_vb = list_first_entry(&src_q->done_list, struct vb2_buffer,
10248c2ecf20Sopenharmony_ci								done_entry);
10258c2ecf20Sopenharmony_ci	if (src_vb && (src_vb->state == VB2_BUF_STATE_DONE
10268c2ecf20Sopenharmony_ci				|| src_vb->state == VB2_BUF_STATE_ERROR))
10278c2ecf20Sopenharmony_ci		rc |= EPOLLOUT | EPOLLWRNORM;
10288c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&src_q->done_lock, flags);
10298c2ecf20Sopenharmony_ci	spin_lock_irqsave(&dst_q->done_lock, flags);
10308c2ecf20Sopenharmony_ci	if (!list_empty(&dst_q->done_list))
10318c2ecf20Sopenharmony_ci		dst_vb = list_first_entry(&dst_q->done_list, struct vb2_buffer,
10328c2ecf20Sopenharmony_ci								done_entry);
10338c2ecf20Sopenharmony_ci	if (dst_vb && (dst_vb->state == VB2_BUF_STATE_DONE
10348c2ecf20Sopenharmony_ci				|| dst_vb->state == VB2_BUF_STATE_ERROR))
10358c2ecf20Sopenharmony_ci		rc |= EPOLLIN | EPOLLRDNORM;
10368c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&dst_q->done_lock, flags);
10378c2ecf20Sopenharmony_ciend:
10388c2ecf20Sopenharmony_ci	mutex_unlock(&dev->mfc_mutex);
10398c2ecf20Sopenharmony_ci	return rc;
10408c2ecf20Sopenharmony_ci}
10418c2ecf20Sopenharmony_ci
10428c2ecf20Sopenharmony_ci/* Mmap */
10438c2ecf20Sopenharmony_cistatic int s5p_mfc_mmap(struct file *file, struct vm_area_struct *vma)
10448c2ecf20Sopenharmony_ci{
10458c2ecf20Sopenharmony_ci	struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
10468c2ecf20Sopenharmony_ci	unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
10478c2ecf20Sopenharmony_ci	int ret;
10488c2ecf20Sopenharmony_ci
10498c2ecf20Sopenharmony_ci	if (offset < DST_QUEUE_OFF_BASE) {
10508c2ecf20Sopenharmony_ci		mfc_debug(2, "mmaping source\n");
10518c2ecf20Sopenharmony_ci		ret = vb2_mmap(&ctx->vq_src, vma);
10528c2ecf20Sopenharmony_ci	} else {		/* capture */
10538c2ecf20Sopenharmony_ci		mfc_debug(2, "mmaping destination\n");
10548c2ecf20Sopenharmony_ci		vma->vm_pgoff -= (DST_QUEUE_OFF_BASE >> PAGE_SHIFT);
10558c2ecf20Sopenharmony_ci		ret = vb2_mmap(&ctx->vq_dst, vma);
10568c2ecf20Sopenharmony_ci	}
10578c2ecf20Sopenharmony_ci	return ret;
10588c2ecf20Sopenharmony_ci}
10598c2ecf20Sopenharmony_ci
10608c2ecf20Sopenharmony_ci/* v4l2 ops */
10618c2ecf20Sopenharmony_cistatic const struct v4l2_file_operations s5p_mfc_fops = {
10628c2ecf20Sopenharmony_ci	.owner = THIS_MODULE,
10638c2ecf20Sopenharmony_ci	.open = s5p_mfc_open,
10648c2ecf20Sopenharmony_ci	.release = s5p_mfc_release,
10658c2ecf20Sopenharmony_ci	.poll = s5p_mfc_poll,
10668c2ecf20Sopenharmony_ci	.unlocked_ioctl = video_ioctl2,
10678c2ecf20Sopenharmony_ci	.mmap = s5p_mfc_mmap,
10688c2ecf20Sopenharmony_ci};
10698c2ecf20Sopenharmony_ci
10708c2ecf20Sopenharmony_ci/* DMA memory related helper functions */
10718c2ecf20Sopenharmony_cistatic void s5p_mfc_memdev_release(struct device *dev)
10728c2ecf20Sopenharmony_ci{
10738c2ecf20Sopenharmony_ci	of_reserved_mem_device_release(dev);
10748c2ecf20Sopenharmony_ci}
10758c2ecf20Sopenharmony_ci
10768c2ecf20Sopenharmony_cistatic struct device *s5p_mfc_alloc_memdev(struct device *dev,
10778c2ecf20Sopenharmony_ci					   const char *name, unsigned int idx)
10788c2ecf20Sopenharmony_ci{
10798c2ecf20Sopenharmony_ci	struct device *child;
10808c2ecf20Sopenharmony_ci	int ret;
10818c2ecf20Sopenharmony_ci
10828c2ecf20Sopenharmony_ci	child = devm_kzalloc(dev, sizeof(*child), GFP_KERNEL);
10838c2ecf20Sopenharmony_ci	if (!child)
10848c2ecf20Sopenharmony_ci		return NULL;
10858c2ecf20Sopenharmony_ci
10868c2ecf20Sopenharmony_ci	device_initialize(child);
10878c2ecf20Sopenharmony_ci	dev_set_name(child, "%s:%s", dev_name(dev), name);
10888c2ecf20Sopenharmony_ci	child->parent = dev;
10898c2ecf20Sopenharmony_ci	child->coherent_dma_mask = dev->coherent_dma_mask;
10908c2ecf20Sopenharmony_ci	child->dma_mask = dev->dma_mask;
10918c2ecf20Sopenharmony_ci	child->release = s5p_mfc_memdev_release;
10928c2ecf20Sopenharmony_ci	child->dma_parms = devm_kzalloc(dev, sizeof(*child->dma_parms),
10938c2ecf20Sopenharmony_ci					GFP_KERNEL);
10948c2ecf20Sopenharmony_ci	if (!child->dma_parms)
10958c2ecf20Sopenharmony_ci		goto err;
10968c2ecf20Sopenharmony_ci
10978c2ecf20Sopenharmony_ci	/*
10988c2ecf20Sopenharmony_ci	 * The memdevs are not proper OF platform devices, so in order for them
10998c2ecf20Sopenharmony_ci	 * to be treated as valid DMA masters we need a bit of a hack to force
11008c2ecf20Sopenharmony_ci	 * them to inherit the MFC node's DMA configuration.
11018c2ecf20Sopenharmony_ci	 */
11028c2ecf20Sopenharmony_ci	of_dma_configure(child, dev->of_node, true);
11038c2ecf20Sopenharmony_ci
11048c2ecf20Sopenharmony_ci	if (device_add(child) == 0) {
11058c2ecf20Sopenharmony_ci		ret = of_reserved_mem_device_init_by_idx(child, dev->of_node,
11068c2ecf20Sopenharmony_ci							 idx);
11078c2ecf20Sopenharmony_ci		if (ret == 0)
11088c2ecf20Sopenharmony_ci			return child;
11098c2ecf20Sopenharmony_ci		device_del(child);
11108c2ecf20Sopenharmony_ci	}
11118c2ecf20Sopenharmony_cierr:
11128c2ecf20Sopenharmony_ci	put_device(child);
11138c2ecf20Sopenharmony_ci	return NULL;
11148c2ecf20Sopenharmony_ci}
11158c2ecf20Sopenharmony_ci
11168c2ecf20Sopenharmony_cistatic int s5p_mfc_configure_2port_memory(struct s5p_mfc_dev *mfc_dev)
11178c2ecf20Sopenharmony_ci{
11188c2ecf20Sopenharmony_ci	struct device *dev = &mfc_dev->plat_dev->dev;
11198c2ecf20Sopenharmony_ci	void *bank2_virt;
11208c2ecf20Sopenharmony_ci	dma_addr_t bank2_dma_addr;
11218c2ecf20Sopenharmony_ci	unsigned long align_size = 1 << MFC_BASE_ALIGN_ORDER;
11228c2ecf20Sopenharmony_ci	int ret;
11238c2ecf20Sopenharmony_ci
11248c2ecf20Sopenharmony_ci	/*
11258c2ecf20Sopenharmony_ci	 * Create and initialize virtual devices for accessing
11268c2ecf20Sopenharmony_ci	 * reserved memory regions.
11278c2ecf20Sopenharmony_ci	 */
11288c2ecf20Sopenharmony_ci	mfc_dev->mem_dev[BANK_L_CTX] = s5p_mfc_alloc_memdev(dev, "left",
11298c2ecf20Sopenharmony_ci							   BANK_L_CTX);
11308c2ecf20Sopenharmony_ci	if (!mfc_dev->mem_dev[BANK_L_CTX])
11318c2ecf20Sopenharmony_ci		return -ENODEV;
11328c2ecf20Sopenharmony_ci	mfc_dev->mem_dev[BANK_R_CTX] = s5p_mfc_alloc_memdev(dev, "right",
11338c2ecf20Sopenharmony_ci							   BANK_R_CTX);
11348c2ecf20Sopenharmony_ci	if (!mfc_dev->mem_dev[BANK_R_CTX]) {
11358c2ecf20Sopenharmony_ci		device_unregister(mfc_dev->mem_dev[BANK_L_CTX]);
11368c2ecf20Sopenharmony_ci		return -ENODEV;
11378c2ecf20Sopenharmony_ci	}
11388c2ecf20Sopenharmony_ci
11398c2ecf20Sopenharmony_ci	/* Allocate memory for firmware and initialize both banks addresses */
11408c2ecf20Sopenharmony_ci	ret = s5p_mfc_alloc_firmware(mfc_dev);
11418c2ecf20Sopenharmony_ci	if (ret) {
11428c2ecf20Sopenharmony_ci		device_unregister(mfc_dev->mem_dev[BANK_R_CTX]);
11438c2ecf20Sopenharmony_ci		device_unregister(mfc_dev->mem_dev[BANK_L_CTX]);
11448c2ecf20Sopenharmony_ci		return ret;
11458c2ecf20Sopenharmony_ci	}
11468c2ecf20Sopenharmony_ci
11478c2ecf20Sopenharmony_ci	mfc_dev->dma_base[BANK_L_CTX] = mfc_dev->fw_buf.dma;
11488c2ecf20Sopenharmony_ci
11498c2ecf20Sopenharmony_ci	bank2_virt = dma_alloc_coherent(mfc_dev->mem_dev[BANK_R_CTX],
11508c2ecf20Sopenharmony_ci				       align_size, &bank2_dma_addr, GFP_KERNEL);
11518c2ecf20Sopenharmony_ci	if (!bank2_virt) {
11528c2ecf20Sopenharmony_ci		mfc_err("Allocating bank2 base failed\n");
11538c2ecf20Sopenharmony_ci		s5p_mfc_release_firmware(mfc_dev);
11548c2ecf20Sopenharmony_ci		device_unregister(mfc_dev->mem_dev[BANK_R_CTX]);
11558c2ecf20Sopenharmony_ci		device_unregister(mfc_dev->mem_dev[BANK_L_CTX]);
11568c2ecf20Sopenharmony_ci		return -ENOMEM;
11578c2ecf20Sopenharmony_ci	}
11588c2ecf20Sopenharmony_ci
11598c2ecf20Sopenharmony_ci	/* Valid buffers passed to MFC encoder with LAST_FRAME command
11608c2ecf20Sopenharmony_ci	 * should not have address of bank2 - MFC will treat it as a null frame.
11618c2ecf20Sopenharmony_ci	 * To avoid such situation we set bank2 address below the pool address.
11628c2ecf20Sopenharmony_ci	 */
11638c2ecf20Sopenharmony_ci	mfc_dev->dma_base[BANK_R_CTX] = bank2_dma_addr - align_size;
11648c2ecf20Sopenharmony_ci
11658c2ecf20Sopenharmony_ci	dma_free_coherent(mfc_dev->mem_dev[BANK_R_CTX], align_size, bank2_virt,
11668c2ecf20Sopenharmony_ci			  bank2_dma_addr);
11678c2ecf20Sopenharmony_ci
11688c2ecf20Sopenharmony_ci	vb2_dma_contig_set_max_seg_size(mfc_dev->mem_dev[BANK_L_CTX],
11698c2ecf20Sopenharmony_ci					DMA_BIT_MASK(32));
11708c2ecf20Sopenharmony_ci	vb2_dma_contig_set_max_seg_size(mfc_dev->mem_dev[BANK_R_CTX],
11718c2ecf20Sopenharmony_ci					DMA_BIT_MASK(32));
11728c2ecf20Sopenharmony_ci
11738c2ecf20Sopenharmony_ci	return 0;
11748c2ecf20Sopenharmony_ci}
11758c2ecf20Sopenharmony_ci
11768c2ecf20Sopenharmony_cistatic void s5p_mfc_unconfigure_2port_memory(struct s5p_mfc_dev *mfc_dev)
11778c2ecf20Sopenharmony_ci{
11788c2ecf20Sopenharmony_ci	device_unregister(mfc_dev->mem_dev[BANK_L_CTX]);
11798c2ecf20Sopenharmony_ci	device_unregister(mfc_dev->mem_dev[BANK_R_CTX]);
11808c2ecf20Sopenharmony_ci	vb2_dma_contig_clear_max_seg_size(mfc_dev->mem_dev[BANK_L_CTX]);
11818c2ecf20Sopenharmony_ci	vb2_dma_contig_clear_max_seg_size(mfc_dev->mem_dev[BANK_R_CTX]);
11828c2ecf20Sopenharmony_ci}
11838c2ecf20Sopenharmony_ci
11848c2ecf20Sopenharmony_cistatic int s5p_mfc_configure_common_memory(struct s5p_mfc_dev *mfc_dev)
11858c2ecf20Sopenharmony_ci{
11868c2ecf20Sopenharmony_ci	struct device *dev = &mfc_dev->plat_dev->dev;
11878c2ecf20Sopenharmony_ci	unsigned long mem_size = SZ_4M;
11888c2ecf20Sopenharmony_ci	unsigned int bitmap_size;
11898c2ecf20Sopenharmony_ci
11908c2ecf20Sopenharmony_ci	if (IS_ENABLED(CONFIG_DMA_CMA) || exynos_is_iommu_available(dev))
11918c2ecf20Sopenharmony_ci		mem_size = SZ_8M;
11928c2ecf20Sopenharmony_ci
11938c2ecf20Sopenharmony_ci	if (mfc_mem_size)
11948c2ecf20Sopenharmony_ci		mem_size = memparse(mfc_mem_size, NULL);
11958c2ecf20Sopenharmony_ci
11968c2ecf20Sopenharmony_ci	bitmap_size = BITS_TO_LONGS(mem_size >> PAGE_SHIFT) * sizeof(long);
11978c2ecf20Sopenharmony_ci
11988c2ecf20Sopenharmony_ci	mfc_dev->mem_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
11998c2ecf20Sopenharmony_ci	if (!mfc_dev->mem_bitmap)
12008c2ecf20Sopenharmony_ci		return -ENOMEM;
12018c2ecf20Sopenharmony_ci
12028c2ecf20Sopenharmony_ci	mfc_dev->mem_virt = dma_alloc_coherent(dev, mem_size,
12038c2ecf20Sopenharmony_ci					       &mfc_dev->mem_base, GFP_KERNEL);
12048c2ecf20Sopenharmony_ci	if (!mfc_dev->mem_virt) {
12058c2ecf20Sopenharmony_ci		kfree(mfc_dev->mem_bitmap);
12068c2ecf20Sopenharmony_ci		dev_err(dev, "failed to preallocate %ld MiB for the firmware and context buffers\n",
12078c2ecf20Sopenharmony_ci			(mem_size / SZ_1M));
12088c2ecf20Sopenharmony_ci		return -ENOMEM;
12098c2ecf20Sopenharmony_ci	}
12108c2ecf20Sopenharmony_ci	mfc_dev->mem_size = mem_size;
12118c2ecf20Sopenharmony_ci	mfc_dev->dma_base[BANK_L_CTX] = mfc_dev->mem_base;
12128c2ecf20Sopenharmony_ci	mfc_dev->dma_base[BANK_R_CTX] = mfc_dev->mem_base;
12138c2ecf20Sopenharmony_ci
12148c2ecf20Sopenharmony_ci	/*
12158c2ecf20Sopenharmony_ci	 * MFC hardware cannot handle 0 as a base address, so mark first 128K
12168c2ecf20Sopenharmony_ci	 * as used (to keep required base alignment) and adjust base address
12178c2ecf20Sopenharmony_ci	 */
12188c2ecf20Sopenharmony_ci	if (mfc_dev->mem_base == (dma_addr_t)0) {
12198c2ecf20Sopenharmony_ci		unsigned int offset = 1 << MFC_BASE_ALIGN_ORDER;
12208c2ecf20Sopenharmony_ci
12218c2ecf20Sopenharmony_ci		bitmap_set(mfc_dev->mem_bitmap, 0, offset >> PAGE_SHIFT);
12228c2ecf20Sopenharmony_ci		mfc_dev->dma_base[BANK_L_CTX] += offset;
12238c2ecf20Sopenharmony_ci		mfc_dev->dma_base[BANK_R_CTX] += offset;
12248c2ecf20Sopenharmony_ci	}
12258c2ecf20Sopenharmony_ci
12268c2ecf20Sopenharmony_ci	/* Firmware allocation cannot fail in this case */
12278c2ecf20Sopenharmony_ci	s5p_mfc_alloc_firmware(mfc_dev);
12288c2ecf20Sopenharmony_ci
12298c2ecf20Sopenharmony_ci	mfc_dev->mem_dev[BANK_L_CTX] = mfc_dev->mem_dev[BANK_R_CTX] = dev;
12308c2ecf20Sopenharmony_ci	vb2_dma_contig_set_max_seg_size(dev, DMA_BIT_MASK(32));
12318c2ecf20Sopenharmony_ci
12328c2ecf20Sopenharmony_ci	dev_info(dev, "preallocated %ld MiB buffer for the firmware and context buffers\n",
12338c2ecf20Sopenharmony_ci		 (mem_size / SZ_1M));
12348c2ecf20Sopenharmony_ci
12358c2ecf20Sopenharmony_ci	return 0;
12368c2ecf20Sopenharmony_ci}
12378c2ecf20Sopenharmony_ci
12388c2ecf20Sopenharmony_cistatic void s5p_mfc_unconfigure_common_memory(struct s5p_mfc_dev *mfc_dev)
12398c2ecf20Sopenharmony_ci{
12408c2ecf20Sopenharmony_ci	struct device *dev = &mfc_dev->plat_dev->dev;
12418c2ecf20Sopenharmony_ci
12428c2ecf20Sopenharmony_ci	dma_free_coherent(dev, mfc_dev->mem_size, mfc_dev->mem_virt,
12438c2ecf20Sopenharmony_ci			  mfc_dev->mem_base);
12448c2ecf20Sopenharmony_ci	kfree(mfc_dev->mem_bitmap);
12458c2ecf20Sopenharmony_ci	vb2_dma_contig_clear_max_seg_size(dev);
12468c2ecf20Sopenharmony_ci}
12478c2ecf20Sopenharmony_ci
12488c2ecf20Sopenharmony_cistatic int s5p_mfc_configure_dma_memory(struct s5p_mfc_dev *mfc_dev)
12498c2ecf20Sopenharmony_ci{
12508c2ecf20Sopenharmony_ci	struct device *dev = &mfc_dev->plat_dev->dev;
12518c2ecf20Sopenharmony_ci
12528c2ecf20Sopenharmony_ci	if (exynos_is_iommu_available(dev) || !IS_TWOPORT(mfc_dev))
12538c2ecf20Sopenharmony_ci		return s5p_mfc_configure_common_memory(mfc_dev);
12548c2ecf20Sopenharmony_ci	else
12558c2ecf20Sopenharmony_ci		return s5p_mfc_configure_2port_memory(mfc_dev);
12568c2ecf20Sopenharmony_ci}
12578c2ecf20Sopenharmony_ci
12588c2ecf20Sopenharmony_cistatic void s5p_mfc_unconfigure_dma_memory(struct s5p_mfc_dev *mfc_dev)
12598c2ecf20Sopenharmony_ci{
12608c2ecf20Sopenharmony_ci	struct device *dev = &mfc_dev->plat_dev->dev;
12618c2ecf20Sopenharmony_ci
12628c2ecf20Sopenharmony_ci	s5p_mfc_release_firmware(mfc_dev);
12638c2ecf20Sopenharmony_ci	if (exynos_is_iommu_available(dev) || !IS_TWOPORT(mfc_dev))
12648c2ecf20Sopenharmony_ci		s5p_mfc_unconfigure_common_memory(mfc_dev);
12658c2ecf20Sopenharmony_ci	else
12668c2ecf20Sopenharmony_ci		s5p_mfc_unconfigure_2port_memory(mfc_dev);
12678c2ecf20Sopenharmony_ci}
12688c2ecf20Sopenharmony_ci
12698c2ecf20Sopenharmony_ci/* MFC probe function */
12708c2ecf20Sopenharmony_cistatic int s5p_mfc_probe(struct platform_device *pdev)
12718c2ecf20Sopenharmony_ci{
12728c2ecf20Sopenharmony_ci	struct s5p_mfc_dev *dev;
12738c2ecf20Sopenharmony_ci	struct video_device *vfd;
12748c2ecf20Sopenharmony_ci	struct resource *res;
12758c2ecf20Sopenharmony_ci	int ret;
12768c2ecf20Sopenharmony_ci
12778c2ecf20Sopenharmony_ci	pr_debug("%s++\n", __func__);
12788c2ecf20Sopenharmony_ci	dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
12798c2ecf20Sopenharmony_ci	if (!dev)
12808c2ecf20Sopenharmony_ci		return -ENOMEM;
12818c2ecf20Sopenharmony_ci
12828c2ecf20Sopenharmony_ci	spin_lock_init(&dev->irqlock);
12838c2ecf20Sopenharmony_ci	spin_lock_init(&dev->condlock);
12848c2ecf20Sopenharmony_ci	dev->plat_dev = pdev;
12858c2ecf20Sopenharmony_ci	if (!dev->plat_dev) {
12868c2ecf20Sopenharmony_ci		mfc_err("No platform data specified\n");
12878c2ecf20Sopenharmony_ci		return -ENODEV;
12888c2ecf20Sopenharmony_ci	}
12898c2ecf20Sopenharmony_ci
12908c2ecf20Sopenharmony_ci	dev->variant = of_device_get_match_data(&pdev->dev);
12918c2ecf20Sopenharmony_ci	if (!dev->variant) {
12928c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Failed to get device MFC hardware variant information\n");
12938c2ecf20Sopenharmony_ci		return -ENOENT;
12948c2ecf20Sopenharmony_ci	}
12958c2ecf20Sopenharmony_ci
12968c2ecf20Sopenharmony_ci	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
12978c2ecf20Sopenharmony_ci	dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
12988c2ecf20Sopenharmony_ci	if (IS_ERR(dev->regs_base))
12998c2ecf20Sopenharmony_ci		return PTR_ERR(dev->regs_base);
13008c2ecf20Sopenharmony_ci
13018c2ecf20Sopenharmony_ci	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
13028c2ecf20Sopenharmony_ci	if (!res) {
13038c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "failed to get irq resource\n");
13048c2ecf20Sopenharmony_ci		return -ENOENT;
13058c2ecf20Sopenharmony_ci	}
13068c2ecf20Sopenharmony_ci	dev->irq = res->start;
13078c2ecf20Sopenharmony_ci	ret = devm_request_irq(&pdev->dev, dev->irq, s5p_mfc_irq,
13088c2ecf20Sopenharmony_ci					0, pdev->name, dev);
13098c2ecf20Sopenharmony_ci	if (ret) {
13108c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Failed to install irq (%d)\n", ret);
13118c2ecf20Sopenharmony_ci		return ret;
13128c2ecf20Sopenharmony_ci	}
13138c2ecf20Sopenharmony_ci
13148c2ecf20Sopenharmony_ci	ret = s5p_mfc_configure_dma_memory(dev);
13158c2ecf20Sopenharmony_ci	if (ret < 0) {
13168c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "failed to configure DMA memory\n");
13178c2ecf20Sopenharmony_ci		return ret;
13188c2ecf20Sopenharmony_ci	}
13198c2ecf20Sopenharmony_ci
13208c2ecf20Sopenharmony_ci	ret = s5p_mfc_init_pm(dev);
13218c2ecf20Sopenharmony_ci	if (ret < 0) {
13228c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "failed to get mfc clock source\n");
13238c2ecf20Sopenharmony_ci		goto err_dma;
13248c2ecf20Sopenharmony_ci	}
13258c2ecf20Sopenharmony_ci
13268c2ecf20Sopenharmony_ci	/*
13278c2ecf20Sopenharmony_ci	 * Load fails if fs isn't mounted. Try loading anyway.
13288c2ecf20Sopenharmony_ci	 * _open() will load it, it it fails now. Ignore failure.
13298c2ecf20Sopenharmony_ci	 */
13308c2ecf20Sopenharmony_ci	s5p_mfc_load_firmware(dev);
13318c2ecf20Sopenharmony_ci
13328c2ecf20Sopenharmony_ci	mutex_init(&dev->mfc_mutex);
13338c2ecf20Sopenharmony_ci	init_waitqueue_head(&dev->queue);
13348c2ecf20Sopenharmony_ci	dev->hw_lock = 0;
13358c2ecf20Sopenharmony_ci	INIT_WORK(&dev->watchdog_work, s5p_mfc_watchdog_worker);
13368c2ecf20Sopenharmony_ci	atomic_set(&dev->watchdog_cnt, 0);
13378c2ecf20Sopenharmony_ci	timer_setup(&dev->watchdog_timer, s5p_mfc_watchdog, 0);
13388c2ecf20Sopenharmony_ci
13398c2ecf20Sopenharmony_ci	ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
13408c2ecf20Sopenharmony_ci	if (ret)
13418c2ecf20Sopenharmony_ci		goto err_v4l2_dev_reg;
13428c2ecf20Sopenharmony_ci
13438c2ecf20Sopenharmony_ci	/* decoder */
13448c2ecf20Sopenharmony_ci	vfd = video_device_alloc();
13458c2ecf20Sopenharmony_ci	if (!vfd) {
13468c2ecf20Sopenharmony_ci		v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
13478c2ecf20Sopenharmony_ci		ret = -ENOMEM;
13488c2ecf20Sopenharmony_ci		goto err_dec_alloc;
13498c2ecf20Sopenharmony_ci	}
13508c2ecf20Sopenharmony_ci	vfd->fops	= &s5p_mfc_fops;
13518c2ecf20Sopenharmony_ci	vfd->ioctl_ops	= get_dec_v4l2_ioctl_ops();
13528c2ecf20Sopenharmony_ci	vfd->release	= video_device_release;
13538c2ecf20Sopenharmony_ci	vfd->lock	= &dev->mfc_mutex;
13548c2ecf20Sopenharmony_ci	vfd->v4l2_dev	= &dev->v4l2_dev;
13558c2ecf20Sopenharmony_ci	vfd->vfl_dir	= VFL_DIR_M2M;
13568c2ecf20Sopenharmony_ci	vfd->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
13578c2ecf20Sopenharmony_ci	set_bit(V4L2_FL_QUIRK_INVERTED_CROP, &vfd->flags);
13588c2ecf20Sopenharmony_ci	snprintf(vfd->name, sizeof(vfd->name), "%s", S5P_MFC_DEC_NAME);
13598c2ecf20Sopenharmony_ci	dev->vfd_dec	= vfd;
13608c2ecf20Sopenharmony_ci	video_set_drvdata(vfd, dev);
13618c2ecf20Sopenharmony_ci
13628c2ecf20Sopenharmony_ci	/* encoder */
13638c2ecf20Sopenharmony_ci	vfd = video_device_alloc();
13648c2ecf20Sopenharmony_ci	if (!vfd) {
13658c2ecf20Sopenharmony_ci		v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
13668c2ecf20Sopenharmony_ci		ret = -ENOMEM;
13678c2ecf20Sopenharmony_ci		goto err_enc_alloc;
13688c2ecf20Sopenharmony_ci	}
13698c2ecf20Sopenharmony_ci	vfd->fops	= &s5p_mfc_fops;
13708c2ecf20Sopenharmony_ci	vfd->ioctl_ops	= get_enc_v4l2_ioctl_ops();
13718c2ecf20Sopenharmony_ci	vfd->release	= video_device_release;
13728c2ecf20Sopenharmony_ci	vfd->lock	= &dev->mfc_mutex;
13738c2ecf20Sopenharmony_ci	vfd->v4l2_dev	= &dev->v4l2_dev;
13748c2ecf20Sopenharmony_ci	vfd->vfl_dir	= VFL_DIR_M2M;
13758c2ecf20Sopenharmony_ci	vfd->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
13768c2ecf20Sopenharmony_ci	snprintf(vfd->name, sizeof(vfd->name), "%s", S5P_MFC_ENC_NAME);
13778c2ecf20Sopenharmony_ci	dev->vfd_enc	= vfd;
13788c2ecf20Sopenharmony_ci	video_set_drvdata(vfd, dev);
13798c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, dev);
13808c2ecf20Sopenharmony_ci
13818c2ecf20Sopenharmony_ci	/* Initialize HW ops and commands based on MFC version */
13828c2ecf20Sopenharmony_ci	s5p_mfc_init_hw_ops(dev);
13838c2ecf20Sopenharmony_ci	s5p_mfc_init_hw_cmds(dev);
13848c2ecf20Sopenharmony_ci	s5p_mfc_init_regs(dev);
13858c2ecf20Sopenharmony_ci
13868c2ecf20Sopenharmony_ci	/* Register decoder and encoder */
13878c2ecf20Sopenharmony_ci	ret = video_register_device(dev->vfd_dec, VFL_TYPE_VIDEO, 0);
13888c2ecf20Sopenharmony_ci	if (ret) {
13898c2ecf20Sopenharmony_ci		v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
13908c2ecf20Sopenharmony_ci		goto err_dec_reg;
13918c2ecf20Sopenharmony_ci	}
13928c2ecf20Sopenharmony_ci	v4l2_info(&dev->v4l2_dev,
13938c2ecf20Sopenharmony_ci		  "decoder registered as /dev/video%d\n", dev->vfd_dec->num);
13948c2ecf20Sopenharmony_ci
13958c2ecf20Sopenharmony_ci	ret = video_register_device(dev->vfd_enc, VFL_TYPE_VIDEO, 0);
13968c2ecf20Sopenharmony_ci	if (ret) {
13978c2ecf20Sopenharmony_ci		v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
13988c2ecf20Sopenharmony_ci		goto err_enc_reg;
13998c2ecf20Sopenharmony_ci	}
14008c2ecf20Sopenharmony_ci	v4l2_info(&dev->v4l2_dev,
14018c2ecf20Sopenharmony_ci		  "encoder registered as /dev/video%d\n", dev->vfd_enc->num);
14028c2ecf20Sopenharmony_ci
14038c2ecf20Sopenharmony_ci	pr_debug("%s--\n", __func__);
14048c2ecf20Sopenharmony_ci	return 0;
14058c2ecf20Sopenharmony_ci
14068c2ecf20Sopenharmony_ci/* Deinit MFC if probe had failed */
14078c2ecf20Sopenharmony_cierr_enc_reg:
14088c2ecf20Sopenharmony_ci	video_unregister_device(dev->vfd_dec);
14098c2ecf20Sopenharmony_cierr_dec_reg:
14108c2ecf20Sopenharmony_ci	video_device_release(dev->vfd_enc);
14118c2ecf20Sopenharmony_cierr_enc_alloc:
14128c2ecf20Sopenharmony_ci	video_device_release(dev->vfd_dec);
14138c2ecf20Sopenharmony_cierr_dec_alloc:
14148c2ecf20Sopenharmony_ci	v4l2_device_unregister(&dev->v4l2_dev);
14158c2ecf20Sopenharmony_cierr_v4l2_dev_reg:
14168c2ecf20Sopenharmony_ci	s5p_mfc_final_pm(dev);
14178c2ecf20Sopenharmony_cierr_dma:
14188c2ecf20Sopenharmony_ci	s5p_mfc_unconfigure_dma_memory(dev);
14198c2ecf20Sopenharmony_ci
14208c2ecf20Sopenharmony_ci	pr_debug("%s-- with error\n", __func__);
14218c2ecf20Sopenharmony_ci	return ret;
14228c2ecf20Sopenharmony_ci
14238c2ecf20Sopenharmony_ci}
14248c2ecf20Sopenharmony_ci
14258c2ecf20Sopenharmony_ci/* Remove the driver */
14268c2ecf20Sopenharmony_cistatic int s5p_mfc_remove(struct platform_device *pdev)
14278c2ecf20Sopenharmony_ci{
14288c2ecf20Sopenharmony_ci	struct s5p_mfc_dev *dev = platform_get_drvdata(pdev);
14298c2ecf20Sopenharmony_ci	struct s5p_mfc_ctx *ctx;
14308c2ecf20Sopenharmony_ci	int i;
14318c2ecf20Sopenharmony_ci
14328c2ecf20Sopenharmony_ci	v4l2_info(&dev->v4l2_dev, "Removing %s\n", pdev->name);
14338c2ecf20Sopenharmony_ci
14348c2ecf20Sopenharmony_ci	/*
14358c2ecf20Sopenharmony_ci	 * Clear ctx dev pointer to avoid races between s5p_mfc_remove()
14368c2ecf20Sopenharmony_ci	 * and s5p_mfc_release() and s5p_mfc_release() accessing ctx->dev
14378c2ecf20Sopenharmony_ci	 * after s5p_mfc_remove() is run during unbind.
14388c2ecf20Sopenharmony_ci	*/
14398c2ecf20Sopenharmony_ci	mutex_lock(&dev->mfc_mutex);
14408c2ecf20Sopenharmony_ci	for (i = 0; i < MFC_NUM_CONTEXTS; i++) {
14418c2ecf20Sopenharmony_ci		ctx = dev->ctx[i];
14428c2ecf20Sopenharmony_ci		if (!ctx)
14438c2ecf20Sopenharmony_ci			continue;
14448c2ecf20Sopenharmony_ci		/* clear ctx->dev */
14458c2ecf20Sopenharmony_ci		ctx->dev = NULL;
14468c2ecf20Sopenharmony_ci	}
14478c2ecf20Sopenharmony_ci	mutex_unlock(&dev->mfc_mutex);
14488c2ecf20Sopenharmony_ci
14498c2ecf20Sopenharmony_ci	del_timer_sync(&dev->watchdog_timer);
14508c2ecf20Sopenharmony_ci	flush_work(&dev->watchdog_work);
14518c2ecf20Sopenharmony_ci
14528c2ecf20Sopenharmony_ci	video_unregister_device(dev->vfd_enc);
14538c2ecf20Sopenharmony_ci	video_unregister_device(dev->vfd_dec);
14548c2ecf20Sopenharmony_ci	video_device_release(dev->vfd_enc);
14558c2ecf20Sopenharmony_ci	video_device_release(dev->vfd_dec);
14568c2ecf20Sopenharmony_ci	v4l2_device_unregister(&dev->v4l2_dev);
14578c2ecf20Sopenharmony_ci	s5p_mfc_unconfigure_dma_memory(dev);
14588c2ecf20Sopenharmony_ci
14598c2ecf20Sopenharmony_ci	s5p_mfc_final_pm(dev);
14608c2ecf20Sopenharmony_ci	return 0;
14618c2ecf20Sopenharmony_ci}
14628c2ecf20Sopenharmony_ci
14638c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP
14648c2ecf20Sopenharmony_ci
14658c2ecf20Sopenharmony_cistatic int s5p_mfc_suspend(struct device *dev)
14668c2ecf20Sopenharmony_ci{
14678c2ecf20Sopenharmony_ci	struct s5p_mfc_dev *m_dev = dev_get_drvdata(dev);
14688c2ecf20Sopenharmony_ci	int ret;
14698c2ecf20Sopenharmony_ci
14708c2ecf20Sopenharmony_ci	if (m_dev->num_inst == 0)
14718c2ecf20Sopenharmony_ci		return 0;
14728c2ecf20Sopenharmony_ci
14738c2ecf20Sopenharmony_ci	if (test_and_set_bit(0, &m_dev->enter_suspend) != 0) {
14748c2ecf20Sopenharmony_ci		mfc_err("Error: going to suspend for a second time\n");
14758c2ecf20Sopenharmony_ci		return -EIO;
14768c2ecf20Sopenharmony_ci	}
14778c2ecf20Sopenharmony_ci
14788c2ecf20Sopenharmony_ci	/* Check if we're processing then wait if it necessary. */
14798c2ecf20Sopenharmony_ci	while (test_and_set_bit(0, &m_dev->hw_lock) != 0) {
14808c2ecf20Sopenharmony_ci		/* Try and lock the HW */
14818c2ecf20Sopenharmony_ci		/* Wait on the interrupt waitqueue */
14828c2ecf20Sopenharmony_ci		ret = wait_event_interruptible_timeout(m_dev->queue,
14838c2ecf20Sopenharmony_ci			m_dev->int_cond, msecs_to_jiffies(MFC_INT_TIMEOUT));
14848c2ecf20Sopenharmony_ci		if (ret == 0) {
14858c2ecf20Sopenharmony_ci			mfc_err("Waiting for hardware to finish timed out\n");
14868c2ecf20Sopenharmony_ci			clear_bit(0, &m_dev->enter_suspend);
14878c2ecf20Sopenharmony_ci			return -EIO;
14888c2ecf20Sopenharmony_ci		}
14898c2ecf20Sopenharmony_ci	}
14908c2ecf20Sopenharmony_ci
14918c2ecf20Sopenharmony_ci	ret = s5p_mfc_sleep(m_dev);
14928c2ecf20Sopenharmony_ci	if (ret) {
14938c2ecf20Sopenharmony_ci		clear_bit(0, &m_dev->enter_suspend);
14948c2ecf20Sopenharmony_ci		clear_bit(0, &m_dev->hw_lock);
14958c2ecf20Sopenharmony_ci	}
14968c2ecf20Sopenharmony_ci	return ret;
14978c2ecf20Sopenharmony_ci}
14988c2ecf20Sopenharmony_ci
14998c2ecf20Sopenharmony_cistatic int s5p_mfc_resume(struct device *dev)
15008c2ecf20Sopenharmony_ci{
15018c2ecf20Sopenharmony_ci	struct s5p_mfc_dev *m_dev = dev_get_drvdata(dev);
15028c2ecf20Sopenharmony_ci
15038c2ecf20Sopenharmony_ci	if (m_dev->num_inst == 0)
15048c2ecf20Sopenharmony_ci		return 0;
15058c2ecf20Sopenharmony_ci	return s5p_mfc_wakeup(m_dev);
15068c2ecf20Sopenharmony_ci}
15078c2ecf20Sopenharmony_ci#endif
15088c2ecf20Sopenharmony_ci
15098c2ecf20Sopenharmony_ci/* Power management */
15108c2ecf20Sopenharmony_cistatic const struct dev_pm_ops s5p_mfc_pm_ops = {
15118c2ecf20Sopenharmony_ci	SET_SYSTEM_SLEEP_PM_OPS(s5p_mfc_suspend, s5p_mfc_resume)
15128c2ecf20Sopenharmony_ci};
15138c2ecf20Sopenharmony_ci
15148c2ecf20Sopenharmony_cistatic struct s5p_mfc_buf_size_v5 mfc_buf_size_v5 = {
15158c2ecf20Sopenharmony_ci	.h264_ctx	= MFC_H264_CTX_BUF_SIZE,
15168c2ecf20Sopenharmony_ci	.non_h264_ctx	= MFC_CTX_BUF_SIZE,
15178c2ecf20Sopenharmony_ci	.dsc		= DESC_BUF_SIZE,
15188c2ecf20Sopenharmony_ci	.shm		= SHARED_BUF_SIZE,
15198c2ecf20Sopenharmony_ci};
15208c2ecf20Sopenharmony_ci
15218c2ecf20Sopenharmony_cistatic struct s5p_mfc_buf_size buf_size_v5 = {
15228c2ecf20Sopenharmony_ci	.fw	= MAX_FW_SIZE,
15238c2ecf20Sopenharmony_ci	.cpb	= MAX_CPB_SIZE,
15248c2ecf20Sopenharmony_ci	.priv	= &mfc_buf_size_v5,
15258c2ecf20Sopenharmony_ci};
15268c2ecf20Sopenharmony_ci
15278c2ecf20Sopenharmony_cistatic struct s5p_mfc_variant mfc_drvdata_v5 = {
15288c2ecf20Sopenharmony_ci	.version	= MFC_VERSION,
15298c2ecf20Sopenharmony_ci	.version_bit	= MFC_V5_BIT,
15308c2ecf20Sopenharmony_ci	.port_num	= MFC_NUM_PORTS,
15318c2ecf20Sopenharmony_ci	.buf_size	= &buf_size_v5,
15328c2ecf20Sopenharmony_ci	.fw_name[0]	= "s5p-mfc.fw",
15338c2ecf20Sopenharmony_ci	.clk_names	= {"mfc", "sclk_mfc"},
15348c2ecf20Sopenharmony_ci	.num_clocks	= 2,
15358c2ecf20Sopenharmony_ci	.use_clock_gating = true,
15368c2ecf20Sopenharmony_ci};
15378c2ecf20Sopenharmony_ci
15388c2ecf20Sopenharmony_cistatic struct s5p_mfc_buf_size_v6 mfc_buf_size_v6 = {
15398c2ecf20Sopenharmony_ci	.dev_ctx	= MFC_CTX_BUF_SIZE_V6,
15408c2ecf20Sopenharmony_ci	.h264_dec_ctx	= MFC_H264_DEC_CTX_BUF_SIZE_V6,
15418c2ecf20Sopenharmony_ci	.other_dec_ctx	= MFC_OTHER_DEC_CTX_BUF_SIZE_V6,
15428c2ecf20Sopenharmony_ci	.h264_enc_ctx	= MFC_H264_ENC_CTX_BUF_SIZE_V6,
15438c2ecf20Sopenharmony_ci	.other_enc_ctx	= MFC_OTHER_ENC_CTX_BUF_SIZE_V6,
15448c2ecf20Sopenharmony_ci};
15458c2ecf20Sopenharmony_ci
15468c2ecf20Sopenharmony_cistatic struct s5p_mfc_buf_size buf_size_v6 = {
15478c2ecf20Sopenharmony_ci	.fw	= MAX_FW_SIZE_V6,
15488c2ecf20Sopenharmony_ci	.cpb	= MAX_CPB_SIZE_V6,
15498c2ecf20Sopenharmony_ci	.priv	= &mfc_buf_size_v6,
15508c2ecf20Sopenharmony_ci};
15518c2ecf20Sopenharmony_ci
15528c2ecf20Sopenharmony_cistatic struct s5p_mfc_variant mfc_drvdata_v6 = {
15538c2ecf20Sopenharmony_ci	.version	= MFC_VERSION_V6,
15548c2ecf20Sopenharmony_ci	.version_bit	= MFC_V6_BIT,
15558c2ecf20Sopenharmony_ci	.port_num	= MFC_NUM_PORTS_V6,
15568c2ecf20Sopenharmony_ci	.buf_size	= &buf_size_v6,
15578c2ecf20Sopenharmony_ci	.fw_name[0]     = "s5p-mfc-v6.fw",
15588c2ecf20Sopenharmony_ci	/*
15598c2ecf20Sopenharmony_ci	 * v6-v2 firmware contains bug fixes and interface change
15608c2ecf20Sopenharmony_ci	 * for init buffer command
15618c2ecf20Sopenharmony_ci	 */
15628c2ecf20Sopenharmony_ci	.fw_name[1]     = "s5p-mfc-v6-v2.fw",
15638c2ecf20Sopenharmony_ci	.clk_names	= {"mfc"},
15648c2ecf20Sopenharmony_ci	.num_clocks	= 1,
15658c2ecf20Sopenharmony_ci};
15668c2ecf20Sopenharmony_ci
15678c2ecf20Sopenharmony_cistatic struct s5p_mfc_buf_size_v6 mfc_buf_size_v7 = {
15688c2ecf20Sopenharmony_ci	.dev_ctx	= MFC_CTX_BUF_SIZE_V7,
15698c2ecf20Sopenharmony_ci	.h264_dec_ctx	= MFC_H264_DEC_CTX_BUF_SIZE_V7,
15708c2ecf20Sopenharmony_ci	.other_dec_ctx	= MFC_OTHER_DEC_CTX_BUF_SIZE_V7,
15718c2ecf20Sopenharmony_ci	.h264_enc_ctx	= MFC_H264_ENC_CTX_BUF_SIZE_V7,
15728c2ecf20Sopenharmony_ci	.other_enc_ctx	= MFC_OTHER_ENC_CTX_BUF_SIZE_V7,
15738c2ecf20Sopenharmony_ci};
15748c2ecf20Sopenharmony_ci
15758c2ecf20Sopenharmony_cistatic struct s5p_mfc_buf_size buf_size_v7 = {
15768c2ecf20Sopenharmony_ci	.fw	= MAX_FW_SIZE_V7,
15778c2ecf20Sopenharmony_ci	.cpb	= MAX_CPB_SIZE_V7,
15788c2ecf20Sopenharmony_ci	.priv	= &mfc_buf_size_v7,
15798c2ecf20Sopenharmony_ci};
15808c2ecf20Sopenharmony_ci
15818c2ecf20Sopenharmony_cistatic struct s5p_mfc_variant mfc_drvdata_v7 = {
15828c2ecf20Sopenharmony_ci	.version	= MFC_VERSION_V7,
15838c2ecf20Sopenharmony_ci	.version_bit	= MFC_V7_BIT,
15848c2ecf20Sopenharmony_ci	.port_num	= MFC_NUM_PORTS_V7,
15858c2ecf20Sopenharmony_ci	.buf_size	= &buf_size_v7,
15868c2ecf20Sopenharmony_ci	.fw_name[0]     = "s5p-mfc-v7.fw",
15878c2ecf20Sopenharmony_ci	.clk_names	= {"mfc"},
15888c2ecf20Sopenharmony_ci	.num_clocks	= 1,
15898c2ecf20Sopenharmony_ci};
15908c2ecf20Sopenharmony_ci
15918c2ecf20Sopenharmony_cistatic struct s5p_mfc_variant mfc_drvdata_v7_3250 = {
15928c2ecf20Sopenharmony_ci	.version        = MFC_VERSION_V7,
15938c2ecf20Sopenharmony_ci	.version_bit    = MFC_V7_BIT,
15948c2ecf20Sopenharmony_ci	.port_num       = MFC_NUM_PORTS_V7,
15958c2ecf20Sopenharmony_ci	.buf_size       = &buf_size_v7,
15968c2ecf20Sopenharmony_ci	.fw_name[0]     = "s5p-mfc-v7.fw",
15978c2ecf20Sopenharmony_ci	.clk_names      = {"mfc", "sclk_mfc"},
15988c2ecf20Sopenharmony_ci	.num_clocks     = 2,
15998c2ecf20Sopenharmony_ci};
16008c2ecf20Sopenharmony_ci
16018c2ecf20Sopenharmony_cistatic struct s5p_mfc_buf_size_v6 mfc_buf_size_v8 = {
16028c2ecf20Sopenharmony_ci	.dev_ctx	= MFC_CTX_BUF_SIZE_V8,
16038c2ecf20Sopenharmony_ci	.h264_dec_ctx	= MFC_H264_DEC_CTX_BUF_SIZE_V8,
16048c2ecf20Sopenharmony_ci	.other_dec_ctx	= MFC_OTHER_DEC_CTX_BUF_SIZE_V8,
16058c2ecf20Sopenharmony_ci	.h264_enc_ctx	= MFC_H264_ENC_CTX_BUF_SIZE_V8,
16068c2ecf20Sopenharmony_ci	.other_enc_ctx	= MFC_OTHER_ENC_CTX_BUF_SIZE_V8,
16078c2ecf20Sopenharmony_ci};
16088c2ecf20Sopenharmony_ci
16098c2ecf20Sopenharmony_cistatic struct s5p_mfc_buf_size buf_size_v8 = {
16108c2ecf20Sopenharmony_ci	.fw	= MAX_FW_SIZE_V8,
16118c2ecf20Sopenharmony_ci	.cpb	= MAX_CPB_SIZE_V8,
16128c2ecf20Sopenharmony_ci	.priv	= &mfc_buf_size_v8,
16138c2ecf20Sopenharmony_ci};
16148c2ecf20Sopenharmony_ci
16158c2ecf20Sopenharmony_cistatic struct s5p_mfc_variant mfc_drvdata_v8 = {
16168c2ecf20Sopenharmony_ci	.version	= MFC_VERSION_V8,
16178c2ecf20Sopenharmony_ci	.version_bit	= MFC_V8_BIT,
16188c2ecf20Sopenharmony_ci	.port_num	= MFC_NUM_PORTS_V8,
16198c2ecf20Sopenharmony_ci	.buf_size	= &buf_size_v8,
16208c2ecf20Sopenharmony_ci	.fw_name[0]     = "s5p-mfc-v8.fw",
16218c2ecf20Sopenharmony_ci	.clk_names	= {"mfc"},
16228c2ecf20Sopenharmony_ci	.num_clocks	= 1,
16238c2ecf20Sopenharmony_ci};
16248c2ecf20Sopenharmony_ci
16258c2ecf20Sopenharmony_cistatic struct s5p_mfc_variant mfc_drvdata_v8_5433 = {
16268c2ecf20Sopenharmony_ci	.version	= MFC_VERSION_V8,
16278c2ecf20Sopenharmony_ci	.version_bit	= MFC_V8_BIT,
16288c2ecf20Sopenharmony_ci	.port_num	= MFC_NUM_PORTS_V8,
16298c2ecf20Sopenharmony_ci	.buf_size	= &buf_size_v8,
16308c2ecf20Sopenharmony_ci	.fw_name[0]     = "s5p-mfc-v8.fw",
16318c2ecf20Sopenharmony_ci	.clk_names	= {"pclk", "aclk", "aclk_xiu"},
16328c2ecf20Sopenharmony_ci	.num_clocks	= 3,
16338c2ecf20Sopenharmony_ci};
16348c2ecf20Sopenharmony_ci
16358c2ecf20Sopenharmony_cistatic struct s5p_mfc_buf_size_v6 mfc_buf_size_v10 = {
16368c2ecf20Sopenharmony_ci	.dev_ctx        = MFC_CTX_BUF_SIZE_V10,
16378c2ecf20Sopenharmony_ci	.h264_dec_ctx   = MFC_H264_DEC_CTX_BUF_SIZE_V10,
16388c2ecf20Sopenharmony_ci	.other_dec_ctx  = MFC_OTHER_DEC_CTX_BUF_SIZE_V10,
16398c2ecf20Sopenharmony_ci	.h264_enc_ctx   = MFC_H264_ENC_CTX_BUF_SIZE_V10,
16408c2ecf20Sopenharmony_ci	.hevc_enc_ctx   = MFC_HEVC_ENC_CTX_BUF_SIZE_V10,
16418c2ecf20Sopenharmony_ci	.other_enc_ctx  = MFC_OTHER_ENC_CTX_BUF_SIZE_V10,
16428c2ecf20Sopenharmony_ci};
16438c2ecf20Sopenharmony_ci
16448c2ecf20Sopenharmony_cistatic struct s5p_mfc_buf_size buf_size_v10 = {
16458c2ecf20Sopenharmony_ci	.fw     = MAX_FW_SIZE_V10,
16468c2ecf20Sopenharmony_ci	.cpb    = MAX_CPB_SIZE_V10,
16478c2ecf20Sopenharmony_ci	.priv   = &mfc_buf_size_v10,
16488c2ecf20Sopenharmony_ci};
16498c2ecf20Sopenharmony_ci
16508c2ecf20Sopenharmony_cistatic struct s5p_mfc_variant mfc_drvdata_v10 = {
16518c2ecf20Sopenharmony_ci	.version        = MFC_VERSION_V10,
16528c2ecf20Sopenharmony_ci	.version_bit    = MFC_V10_BIT,
16538c2ecf20Sopenharmony_ci	.port_num       = MFC_NUM_PORTS_V10,
16548c2ecf20Sopenharmony_ci	.buf_size       = &buf_size_v10,
16558c2ecf20Sopenharmony_ci	.fw_name[0]     = "s5p-mfc-v10.fw",
16568c2ecf20Sopenharmony_ci};
16578c2ecf20Sopenharmony_ci
16588c2ecf20Sopenharmony_cistatic const struct of_device_id exynos_mfc_match[] = {
16598c2ecf20Sopenharmony_ci	{
16608c2ecf20Sopenharmony_ci		.compatible = "samsung,mfc-v5",
16618c2ecf20Sopenharmony_ci		.data = &mfc_drvdata_v5,
16628c2ecf20Sopenharmony_ci	}, {
16638c2ecf20Sopenharmony_ci		.compatible = "samsung,mfc-v6",
16648c2ecf20Sopenharmony_ci		.data = &mfc_drvdata_v6,
16658c2ecf20Sopenharmony_ci	}, {
16668c2ecf20Sopenharmony_ci		.compatible = "samsung,mfc-v7",
16678c2ecf20Sopenharmony_ci		.data = &mfc_drvdata_v7,
16688c2ecf20Sopenharmony_ci	}, {
16698c2ecf20Sopenharmony_ci		.compatible = "samsung,exynos3250-mfc",
16708c2ecf20Sopenharmony_ci		.data = &mfc_drvdata_v7_3250,
16718c2ecf20Sopenharmony_ci	}, {
16728c2ecf20Sopenharmony_ci		.compatible = "samsung,mfc-v8",
16738c2ecf20Sopenharmony_ci		.data = &mfc_drvdata_v8,
16748c2ecf20Sopenharmony_ci	}, {
16758c2ecf20Sopenharmony_ci		.compatible = "samsung,exynos5433-mfc",
16768c2ecf20Sopenharmony_ci		.data = &mfc_drvdata_v8_5433,
16778c2ecf20Sopenharmony_ci	}, {
16788c2ecf20Sopenharmony_ci		.compatible = "samsung,mfc-v10",
16798c2ecf20Sopenharmony_ci		.data = &mfc_drvdata_v10,
16808c2ecf20Sopenharmony_ci	},
16818c2ecf20Sopenharmony_ci	{},
16828c2ecf20Sopenharmony_ci};
16838c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, exynos_mfc_match);
16848c2ecf20Sopenharmony_ci
16858c2ecf20Sopenharmony_cistatic struct platform_driver s5p_mfc_driver = {
16868c2ecf20Sopenharmony_ci	.probe		= s5p_mfc_probe,
16878c2ecf20Sopenharmony_ci	.remove		= s5p_mfc_remove,
16888c2ecf20Sopenharmony_ci	.driver	= {
16898c2ecf20Sopenharmony_ci		.name	= S5P_MFC_NAME,
16908c2ecf20Sopenharmony_ci		.pm	= &s5p_mfc_pm_ops,
16918c2ecf20Sopenharmony_ci		.of_match_table = exynos_mfc_match,
16928c2ecf20Sopenharmony_ci	},
16938c2ecf20Sopenharmony_ci};
16948c2ecf20Sopenharmony_ci
16958c2ecf20Sopenharmony_cimodule_platform_driver(s5p_mfc_driver);
16968c2ecf20Sopenharmony_ci
16978c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
16988c2ecf20Sopenharmony_ciMODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
16998c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Samsung S5P Multi Format Codec V4L2 driver");
17008c2ecf20Sopenharmony_ci
1701