aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/video/samsung/mfc5x/mfc_inst.c
blob: 33736c9ad15088f6aaa66c1e55459f35c3820140 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
 * linux/drivers/media/video/samsung/mfc5x/mfc_inst.c
 *
 * Copyright (c) 2010 Samsung Electronics Co., Ltd.
 *		http://www.samsung.com/
 *
 * Instance manager for Samsung MFC (Multi Function Codec - FIMV) driver
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/slab.h>
#include <linux/mm.h>

#include "mfc_inst.h"
#include "mfc_log.h"
#include "mfc_buf.h"
#include "mfc_cmd.h"
#include "mfc_pm.h"
#include "mfc_dec.h"
#include "mfc_enc.h"

#ifdef SYSMMU_MFC_ON
#include <linux/interrupt.h>
#endif

/*
 * the sematic both of mfc_create_inst() and mfc_destory_inst()
 * be symmetric, but MFC channel open operation will be execute
 * while init. sequence. (decoding and encoding)
 * create -	just allocate context memory and initialize state
 *
 * destory -	execute channel close operation
 *		free allocated buffer for instance
 *		free allocated context memory
 */

struct mfc_inst_ctx *mfc_create_inst(void)
{
	struct mfc_inst_ctx *ctx;

	ctx = kzalloc(sizeof(struct mfc_inst_ctx), GFP_KERNEL);
	if (!ctx) {
		mfc_err("failed to create instance\n");
		return NULL;
	}

	/* FIXME: set default values */
	ctx->state = INST_STATE_CREATE;

	ctx->codecid = -1;
	ctx->resolution_status = RES_NO_CHANGE;
#ifdef CONFIG_BUSFREQ
	ctx->busfreq_flag = false;
#endif
#if defined(CONFIG_MACH_GC1) && defined(CONFIG_EXYNOS4_CPUFREQ)
	ctx->cpufreq_flag = false;
#endif
#if defined(CONFIG_CPU_EXYNOS4210) && defined(CONFIG_EXYNOS4_CPUFREQ)
	ctx->cpufreq_flag = false;
#endif
#ifdef CONFIG_BUSFREQ_OPP
	ctx->dmcthreshold_flag = false;
#endif
#ifdef SYSMMU_MFC_ON
	/*
	ctx->pgd = __pa(current->mm->pgd);
	*/
	ctx->pgd = __pa(swapper_pg_dir);
#endif

	INIT_LIST_HEAD(&ctx->presetcfgs);

	return ctx;
}

void mfc_destroy_inst(struct mfc_inst_ctx *ctx)
{
	struct mfc_dec_ctx *dec_ctx;
	struct mfc_enc_ctx *enc_ctx;
	struct mfc_pre_cfg *precfg;

	if (ctx) {
		if (ctx->state < INST_STATE_SETUP) {
			while (!list_empty(&ctx->presetcfgs)) {
				precfg = list_entry((&ctx->presetcfgs)->next,
						struct mfc_pre_cfg, list);

				mfc_dbg("remove unused preset config [0x%08x]\n",
					precfg->type);

				list_del(&precfg->list);
				kfree(precfg);
			}
		} else {
			/* free (decoder/encoder & context) private memory */
			if (ctx->type == DECODER) {
				dec_ctx = ctx->c_priv;
				if (dec_ctx->d_priv)
					kfree(dec_ctx->d_priv);

				kfree(dec_ctx);
			} else if (ctx->type == ENCODER) {
				enc_ctx = ctx->c_priv;
				if (enc_ctx->e_priv)
					kfree(enc_ctx->e_priv);

				kfree(enc_ctx);
			}
		}

		if (ctx->state >= INST_STATE_OPEN) {
			mfc_clock_on(ctx->dev);
			mfc_cmd_inst_close(ctx);
			mfc_clock_off(ctx->dev);
		}

		mfc_free_buf_inst(ctx->id);

		/* free instance context memory */
		kfree(ctx);
	}
}

int mfc_set_inst_state(struct mfc_inst_ctx *ctx, enum instance_state state)
{
	mfc_dbg("state: 0x%08x", state);

	/* only allow EXE_DONE to EXE transition */
	if (ctx->state == INST_STATE_EXE_DONE && state == INST_STATE_EXE)
		ctx->state = state;

	if (ctx->state > state) {
		mfc_err("failed to change state of instance [0x%08x:0x%08x]\n",
			ctx->state, state);
		return -1;
	}

	ctx->state = state;

	return 0;
}

int mfc_chk_inst_state(struct mfc_inst_ctx *ctx, enum instance_state state)
{
	if (ctx->state != state)
		return -1;
	else
		return 0;
}

int mfc_set_inst_cfg(struct mfc_inst_ctx *ctx, int type, void *arg)
{
	int ret = MFC_OK;
	struct mfc_pre_cfg *precfg;
	union _mfc_config_arg *usercfg = (union _mfc_config_arg *)arg;
	struct list_head *pos, *nxt;

	mfc_dbg("type: 0x%08x, ctx->type: 0x%08x", type, ctx->type);

	/* pre-configuration supports only basic type */
	if (ctx->state <= INST_STATE_CREATE) {
		precfg = (struct mfc_pre_cfg *)
			kzalloc(sizeof(struct mfc_pre_cfg), GFP_KERNEL);

		if (unlikely(precfg == NULL)) {
			mfc_err("no more kernel memory");

			return MFC_SET_CONF_FAIL;
		}

		precfg->type = type;
		memcpy(precfg->values, usercfg->basic.values, sizeof(precfg->values));

		mfc_dbg("precfg new entry");
		mfc_dbg("type: 0x%08x", precfg->type);
		mfc_dbg("values: %d %d %d %d", precfg->values[0],
			precfg->values[1], precfg->values[2], precfg->values[3]);

		list_add_tail(&precfg->list, &ctx->presetcfgs);

		mfc_dbg("precfg entries...");
		precfg = NULL;

		list_for_each_safe(pos, nxt, &ctx->presetcfgs) {
			precfg = list_entry(pos, struct mfc_pre_cfg, list);

			mfc_dbg("type: 0x%08x", precfg->type);
			mfc_dbg("values: %d %d %d %d", precfg->values[0],
				precfg->values[1], precfg->values[2], precfg->values[3]);
		}

		return MFC_OK;
	}

	switch (type) {
	case MFC_DEC_SETCONF_POST_ENABLE:
	case MFC_DEC_SETCONF_EXTRA_BUFFER_NUM:
	case MFC_DEC_SETCONF_DISPLAY_DELAY:
	case MFC_DEC_SETCONF_IS_LAST_FRAME:
	case MFC_DEC_SETCONF_SLICE_ENABLE:
	case MFC_DEC_SETCONF_CRC_ENABLE:
	case MFC_DEC_SETCONF_FIMV1_WIDTH_HEIGHT:
	case MFC_DEC_SETCONF_FRAME_TAG:
	case MFC_DEC_SETCONF_IMMEDIATELY_DISPLAY:
	case MFC_DEC_SETCONF_DPB_FLUSH:
	case MFC_DEC_SETCONF_SEI_PARSE:
	case MFC_DEC_SETCONF_PIXEL_CACHE:
	case MFC_ENC_SETCONF_FRAME_TYPE:
	case MFC_ENC_SETCONF_CHANGE_FRAME_RATE:
	case MFC_ENC_SETCONF_CHANGE_BIT_RATE:
	case MFC_ENC_SETCONF_FRAME_TAG:
	case MFC_ENC_SETCONF_ALLOW_FRAME_SKIP:
	case MFC_ENC_SETCONF_VUI_INFO:
	case MFC_ENC_SETCONF_I_PERIOD:
	case MFC_ENC_SETCONF_HIER_P:
	case MFC_ENC_SETCONF_SEI_GEN:
	case MFC_ENC_SETCONF_FRAME_PACKING:
	case MFC_ENC_SETCONF_SPS_PPS_GEN:
		if (ctx->c_ops->set_codec_cfg) {
			if ((ctx->c_ops->set_codec_cfg(ctx, type, arg)) < 0)
				return MFC_SET_CONF_FAIL;
		}
		break;

		default:
			mfc_err("invalid set config type: 0x%08x\n", type);
			return MFC_FAIL;
	}

	return ret;
}

int mfc_get_inst_cfg(struct mfc_inst_ctx *ctx, int type, void *arg)
{
	int ret = MFC_OK;

	mfc_dbg("type: 0x%08x, ctx->type: 0x%08x", type, ctx->type);

	switch (type) {
		case MFC_DEC_GETCONF_CRC_DATA:
		case MFC_DEC_GETCONF_BUF_WIDTH_HEIGHT:
		case MFC_DEC_GETCONF_CROP_INFO:
		case MFC_DEC_GETCONF_FRAME_TAG:
		case MFC_DEC_GETCONF_WIDTH_HEIGHT:
		case MFC_DEC_GETCONF_FRAME_PACKING:
		case MFC_ENC_GETCONF_FRAME_TAG:
			if (ctx->c_ops->get_codec_cfg) {
				if ((ctx->c_ops->get_codec_cfg(ctx, type, arg)) < 0)
					return MFC_GET_CONF_FAIL;
			}
			break;

		default:
			mfc_err("invalid get config type: 0x%08x\n", type);
			return MFC_FAIL;
	}

	return ret;
}