summaryrefslogtreecommitdiff
path: root/drivers/media/platform/amphion/vpu_cmds.c
blob: fa581ba6bab2dbaa316838682d45bfe714f3221f (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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright 2020-2021 NXP
 */

#include <linux/init.h>
#include <linux/interconnect.h>
#include <linux/ioctl.h>
#include <linux/list.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/of_device.h>
#include <linux/of_address.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/delay.h>
#include <linux/vmalloc.h>
#include "vpu.h"
#include "vpu_defs.h"
#include "vpu_cmds.h"
#include "vpu_rpc.h"
#include "vpu_mbox.h"

struct vpu_cmd_request {
	u32 request;
	u32 response;
	u32 handled;
};

struct vpu_cmd_t {
	struct list_head list;
	u32 id;
	struct vpu_cmd_request *request;
	struct vpu_rpc_event *pkt;
	unsigned long key;
};

static struct vpu_cmd_request vpu_cmd_requests[] = {
	{
		.request = VPU_CMD_ID_CONFIGURE_CODEC,
		.response = VPU_MSG_ID_MEM_REQUEST,
		.handled = 1,
	},
	{
		.request = VPU_CMD_ID_START,
		.response = VPU_MSG_ID_START_DONE,
		.handled = 0,
	},
	{
		.request = VPU_CMD_ID_STOP,
		.response = VPU_MSG_ID_STOP_DONE,
		.handled = 0,
	},
	{
		.request = VPU_CMD_ID_ABORT,
		.response = VPU_MSG_ID_ABORT_DONE,
		.handled = 0,
	},
	{
		.request = VPU_CMD_ID_RST_BUF,
		.response = VPU_MSG_ID_BUF_RST,
		.handled = 1,
	},
};

static int vpu_cmd_send(struct vpu_core *core, struct vpu_rpc_event *pkt)
{
	int ret = 0;

	ret = vpu_iface_send_cmd(core, pkt);
	if (ret)
		return ret;

	/*write cmd data to cmd buffer before trigger a cmd interrupt*/
	mb();
	vpu_mbox_send_type(core, COMMAND);

	return ret;
}

static struct vpu_cmd_t *vpu_alloc_cmd(struct vpu_inst *inst, u32 id, void *data)
{
	struct vpu_cmd_t *cmd;
	int i;
	int ret;

	cmd = vzalloc(sizeof(*cmd));
	if (!cmd)
		return NULL;

	cmd->pkt = vzalloc(sizeof(*cmd->pkt));
	if (!cmd->pkt) {
		vfree(cmd);
		return NULL;
	}

	cmd->id = id;
	ret = vpu_iface_pack_cmd(inst->core, cmd->pkt, inst->id, id, data);
	if (ret) {
		dev_err(inst->dev, "iface pack cmd(%d) fail\n", id);
		vfree(cmd->pkt);
		vfree(cmd);
		return NULL;
	}
	for (i = 0; i < ARRAY_SIZE(vpu_cmd_requests); i++) {
		if (vpu_cmd_requests[i].request == id) {
			cmd->request = &vpu_cmd_requests[i];
			break;
		}
	}

	return cmd;
}

static void vpu_free_cmd(struct vpu_cmd_t *cmd)
{
	if (!cmd)
		return;
	vfree(cmd->pkt);
	vfree(cmd);
}

static int vpu_session_process_cmd(struct vpu_inst *inst, struct vpu_cmd_t *cmd)
{
	int ret;

	dev_dbg(inst->dev, "[%d]send cmd(0x%x)\n", inst->id, cmd->id);
	vpu_iface_pre_send_cmd(inst);
	ret = vpu_cmd_send(inst->core, cmd->pkt);
	if (!ret) {
		vpu_iface_post_send_cmd(inst);
		vpu_inst_record_flow(inst, cmd->id);
	} else {
		dev_err(inst->dev, "[%d] iface send cmd(0x%x) fail\n", inst->id, cmd->id);
	}

	return ret;
}

static void vpu_process_cmd_request(struct vpu_inst *inst)
{
	struct vpu_cmd_t *cmd;
	struct vpu_cmd_t *tmp;

	if (!inst || inst->pending)
		return;

	list_for_each_entry_safe(cmd, tmp, &inst->cmd_q, list) {
		list_del_init(&cmd->list);
		if (vpu_session_process_cmd(inst, cmd))
			dev_err(inst->dev, "[%d] process cmd(%d) fail\n", inst->id, cmd->id);
		if (cmd->request) {
			inst->pending = (void *)cmd;
			break;
		}
		vpu_free_cmd(cmd);
	}
}

static int vpu_request_cmd(struct vpu_inst *inst, u32 id, void *data,
			   unsigned long *key, int *sync)
{
	struct vpu_core *core;
	struct vpu_cmd_t *cmd;

	if (!inst || !inst->core)
		return -EINVAL;

	core = inst->core;
	cmd = vpu_alloc_cmd(inst, id, data);
	if (!cmd)
		return -ENOMEM;

	mutex_lock(&core->cmd_lock);
	cmd->key = core->cmd_seq++;
	if (key)
		*key = cmd->key;
	if (sync)
		*sync = cmd->request ? true : false;
	list_add_tail(&cmd->list, &inst->cmd_q);
	vpu_process_cmd_request(inst);
	mutex_unlock(&core->cmd_lock);

	return 0;
}

static void vpu_clear_pending(struct vpu_inst *inst)
{
	if (!inst || !inst->pending)
		return;

	vpu_free_cmd(inst->pending);
	wake_up_all(&inst->core->ack_wq);
	inst->pending = NULL;
}

static bool vpu_check_response(struct vpu_cmd_t *cmd, u32 response, u32 handled)
{
	struct vpu_cmd_request *request;

	if (!cmd || !cmd->request)
		return false;

	request = cmd->request;
	if (request->response != response)
		return false;
	if (request->handled != handled)
		return false;

	return true;
}

int vpu_response_cmd(struct vpu_inst *inst, u32 response, u32 handled)
{
	struct vpu_core *core;

	if (!inst || !inst->core)
		return -EINVAL;

	core = inst->core;
	mutex_lock(&core->cmd_lock);
	if (vpu_check_response(inst->pending, response, handled))
		vpu_clear_pending(inst);

	vpu_process_cmd_request(inst);
	mutex_unlock(&core->cmd_lock);

	return 0;
}

void vpu_clear_request(struct vpu_inst *inst)
{
	struct vpu_cmd_t *cmd;
	struct vpu_cmd_t *tmp;

	mutex_lock(&inst->core->cmd_lock);
	if (inst->pending)
		vpu_clear_pending(inst);

	list_for_each_entry_safe(cmd, tmp, &inst->cmd_q, list) {
		list_del_init(&cmd->list);
		vpu_free_cmd(cmd);
	}
	mutex_unlock(&inst->core->cmd_lock);
}

static bool check_is_responsed(struct vpu_inst *inst, unsigned long key)
{
	struct vpu_core *core = inst->core;
	struct vpu_cmd_t *cmd;
	bool flag = true;

	mutex_lock(&core->cmd_lock);
	cmd = inst->pending;
	if (cmd && key == cmd->key) {
		flag = false;
		goto exit;
	}
	list_for_each_entry(cmd, &inst->cmd_q, list) {
		if (key == cmd->key) {
			flag = false;
			break;
		}
	}
exit:
	mutex_unlock(&core->cmd_lock);

	return flag;
}

static int sync_session_response(struct vpu_inst *inst, unsigned long key, long timeout, int try)
{
	struct vpu_core *core;

	if (!inst || !inst->core)
		return -EINVAL;

	core = inst->core;

	call_void_vop(inst, wait_prepare);
	wait_event_timeout(core->ack_wq, check_is_responsed(inst, key), timeout);
	call_void_vop(inst, wait_finish);

	if (!check_is_responsed(inst, key)) {
		if (try)
			return -EINVAL;
		dev_err(inst->dev, "[%d] sync session timeout\n", inst->id);
		set_bit(inst->id, &core->hang_mask);
		mutex_lock(&inst->core->cmd_lock);
		vpu_clear_pending(inst);
		mutex_unlock(&inst->core->cmd_lock);
		return -EINVAL;
	}

	return 0;
}

static void vpu_core_keep_active(struct vpu_core *core)
{
	struct vpu_rpc_event pkt;

	memset(&pkt, 0, sizeof(pkt));
	vpu_iface_pack_cmd(core, &pkt, 0, VPU_CMD_ID_NOOP, NULL);

	dev_dbg(core->dev, "try to wake up\n");
	mutex_lock(&core->cmd_lock);
	vpu_cmd_send(core, &pkt);
	mutex_unlock(&core->cmd_lock);
}

static int vpu_session_send_cmd(struct vpu_inst *inst, u32 id, void *data)
{
	unsigned long key;
	int sync = false;
	int ret = -EINVAL;

	if (inst->id < 0)
		return -EINVAL;

	ret = vpu_request_cmd(inst, id, data, &key, &sync);
	if (ret)
		goto exit;

	/* workaround for a firmware issue,
	 * firmware should be waked up by start or configure command,
	 * but there is a very small change that firmware failed to wakeup.
	 * in such case, try to wakeup firmware again by sending a noop command
	 */
	if (sync && (id == VPU_CMD_ID_CONFIGURE_CODEC || id == VPU_CMD_ID_START)) {
		if (sync_session_response(inst, key, VPU_TIMEOUT_WAKEUP, 1))
			vpu_core_keep_active(inst->core);
		else
			goto exit;
	}

	if (sync)
		ret = sync_session_response(inst, key, VPU_TIMEOUT, 0);

exit:
	if (ret)
		dev_err(inst->dev, "[%d] send cmd(0x%x) fail\n", inst->id, id);

	return ret;
}

int vpu_session_configure_codec(struct vpu_inst *inst)
{
	return vpu_session_send_cmd(inst, VPU_CMD_ID_CONFIGURE_CODEC, NULL);
}

int vpu_session_start(struct vpu_inst *inst)
{
	vpu_trace(inst->dev, "[%d]\n", inst->id);

	return vpu_session_send_cmd(inst, VPU_CMD_ID_START, NULL);
}

int vpu_session_stop(struct vpu_inst *inst)
{
	int ret;

	vpu_trace(inst->dev, "[%d]\n", inst->id);

	ret = vpu_session_send_cmd(inst, VPU_CMD_ID_STOP, NULL);
	/* workaround for a firmware bug,
	 * if the next command is too close after stop cmd,
	 * the firmware may enter wfi wrongly.
	 */
	usleep_range(3000, 5000);
	return ret;
}

int vpu_session_encode_frame(struct vpu_inst *inst, s64 timestamp)
{
	return vpu_session_send_cmd(inst, VPU_CMD_ID_FRAME_ENCODE, &timestamp);
}

int vpu_session_alloc_fs(struct vpu_inst *inst, struct vpu_fs_info *fs)
{
	return vpu_session_send_cmd(inst, VPU_CMD_ID_FS_ALLOC, fs);
}

int vpu_session_release_fs(struct vpu_inst *inst, struct vpu_fs_info *fs)
{
	return vpu_session_send_cmd(inst, VPU_CMD_ID_FS_RELEASE, fs);
}

int vpu_session_abort(struct vpu_inst *inst)
{
	return vpu_session_send_cmd(inst, VPU_CMD_ID_ABORT, NULL);
}

int vpu_session_rst_buf(struct vpu_inst *inst)
{
	return vpu_session_send_cmd(inst, VPU_CMD_ID_RST_BUF, NULL);
}

int vpu_session_fill_timestamp(struct vpu_inst *inst, struct vpu_ts_info *info)
{
	return vpu_session_send_cmd(inst, VPU_CMD_ID_TIMESTAMP, info);
}

int vpu_session_update_parameters(struct vpu_inst *inst, void *arg)
{
	if (inst->type & VPU_CORE_TYPE_DEC)
		vpu_iface_set_decode_params(inst, arg, 1);
	else
		vpu_iface_set_encode_params(inst, arg, 1);

	return vpu_session_send_cmd(inst, VPU_CMD_ID_UPDATE_PARAMETER, arg);
}

int vpu_session_debug(struct vpu_inst *inst)
{
	return vpu_session_send_cmd(inst, VPU_CMD_ID_DEBUG, NULL);
}

int vpu_core_snapshot(struct vpu_core *core)
{
	struct vpu_inst *inst;
	int ret;

	if (!core || list_empty(&core->instances))
		return 0;

	inst = list_first_entry(&core->instances, struct vpu_inst, list);

	reinit_completion(&core->cmp);
	ret = vpu_session_send_cmd(inst, VPU_CMD_ID_SNAPSHOT, NULL);
	if (ret)
		return ret;
	ret = wait_for_completion_timeout(&core->cmp, VPU_TIMEOUT);
	if (!ret) {
		dev_err(core->dev, "snapshot timeout\n");
		return -EINVAL;
	}

	return 0;
}

int vpu_core_sw_reset(struct vpu_core *core)
{
	struct vpu_rpc_event pkt;
	int ret;

	memset(&pkt, 0, sizeof(pkt));
	vpu_iface_pack_cmd(core, &pkt, 0, VPU_CMD_ID_FIRM_RESET, NULL);

	reinit_completion(&core->cmp);
	mutex_lock(&core->cmd_lock);
	ret = vpu_cmd_send(core, &pkt);
	mutex_unlock(&core->cmd_lock);
	if (ret)
		return ret;
	ret = wait_for_completion_timeout(&core->cmp, VPU_TIMEOUT);
	if (!ret) {
		dev_err(core->dev, "sw reset timeout\n");
		return -EINVAL;
	}

	return 0;
}