1// SPDX-License-Identifier: GPL-2.0
2/*
3* Copyright (c) 2016 MediaTek Inc.
4* Author: Tiffany Lin <tiffany.lin@mediatek.com>
5*/
6
7#include <linux/errno.h>
8#include <linux/wait.h>
9
10#include "../decoder/mtk_vcodec_dec_drv.h"
11#include "../encoder/mtk_vcodec_enc_drv.h"
12#include "mtk_vcodec_intr.h"
13
14int mtk_vcodec_wait_for_done_ctx(void *priv, int command, unsigned int timeout_ms,
15				 unsigned int hw_id)
16{
17	int instance_type = *((int *)priv);
18	long timeout_jiff, ret;
19	int ctx_id, ctx_type, status = 0;
20	int *ctx_int_cond, *ctx_int_type;
21	wait_queue_head_t *ctx_queue;
22	struct platform_device *pdev;
23
24	if (instance_type == DECODER) {
25		struct mtk_vcodec_dec_ctx *ctx;
26
27		ctx = priv;
28		ctx_id = ctx->id;
29		ctx_type = ctx->type;
30		ctx_int_cond = ctx->int_cond;
31		ctx_int_type = ctx->int_type;
32		ctx_queue = ctx->queue;
33		pdev = ctx->dev->plat_dev;
34	} else {
35		struct mtk_vcodec_enc_ctx *ctx;
36
37		ctx = priv;
38		ctx_id = ctx->id;
39		ctx_type = ctx->type;
40		ctx_int_cond = ctx->int_cond;
41		ctx_int_type = ctx->int_type;
42		ctx_queue = ctx->queue;
43		pdev = ctx->dev->plat_dev;
44	}
45
46	timeout_jiff = msecs_to_jiffies(timeout_ms);
47	ret = wait_event_interruptible_timeout(ctx_queue[hw_id],
48					       ctx_int_cond[hw_id],
49					       timeout_jiff);
50
51	if (!ret) {
52		status = -1;	/* timeout */
53		dev_err(&pdev->dev, "[%d] cmd=%d, type=%d, dec timeout=%ums (%d %d)",
54			ctx_id, command, ctx_type, timeout_ms,
55			ctx_int_cond[hw_id], ctx_int_type[hw_id]);
56	} else if (-ERESTARTSYS == ret) {
57		status = -1;
58		dev_err(&pdev->dev, "[%d] cmd=%d, type=%d, dec inter fail (%d %d)",
59			ctx_id, command, ctx_type,
60			ctx_int_cond[hw_id], ctx_int_type[hw_id]);
61	}
62
63	ctx_int_cond[hw_id] = 0;
64	ctx_int_type[hw_id] = 0;
65
66	return status;
67}
68EXPORT_SYMBOL(mtk_vcodec_wait_for_done_ctx);
69