summaryrefslogtreecommitdiffstats
path: root/drivers/media/platform/amphion/vpu_helpers.c
blob: 66fdb0baea746fbbdbe8ab03dfdcfe37bc4a420a (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
464
465
466
467
468
469
470
471
472
473
474
475
476
// 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/platform_device.h>
#include "vpu.h"
#include "vpu_defs.h"
#include "vpu_core.h"
#include "vpu_rpc.h"
#include "vpu_helpers.h"

int vpu_helper_find_in_array_u8(const u8 *array, u32 size, u32 x)
{
	int i;

	for (i = 0; i < size; i++) {
		if (array[i] == x)
			return i;
	}

	return 0;
}

bool vpu_helper_check_type(struct vpu_inst *inst, u32 type)
{
	const struct vpu_format *pfmt;

	for (pfmt = inst->formats; pfmt->pixfmt; pfmt++) {
		if (!vpu_iface_check_format(inst, pfmt->pixfmt))
			continue;
		if (pfmt->type == type)
			return true;
	}

	return false;
}

const struct vpu_format *vpu_helper_find_format(struct vpu_inst *inst, u32 type, u32 pixelfmt)
{
	const struct vpu_format *pfmt;

	if (!inst || !inst->formats)
		return NULL;

	if (!vpu_iface_check_format(inst, pixelfmt))
		return NULL;

	for (pfmt = inst->formats; pfmt->pixfmt; pfmt++) {
		if (pfmt->pixfmt == pixelfmt && (!type || type == pfmt->type))
			return pfmt;
	}

	return NULL;
}

const struct vpu_format *vpu_helper_enum_format(struct vpu_inst *inst, u32 type, int index)
{
	const struct vpu_format *pfmt;
	int i = 0;

	if (!inst || !inst->formats)
		return NULL;

	for (pfmt = inst->formats; pfmt->pixfmt; pfmt++) {
		if (!vpu_iface_check_format(inst, pfmt->pixfmt))
			continue;

		if (pfmt->type == type) {
			if (index == i)
				return pfmt;
			i++;
		}
	}

	return NULL;
}

u32 vpu_helper_valid_frame_width(struct vpu_inst *inst, u32 width)
{
	const struct vpu_core_resources *res;

	if (!inst)
		return width;

	res = vpu_get_resource(inst);
	if (!res)
		return width;
	if (res->max_width)
		width = clamp(width, res->min_width, res->max_width);
	if (res->step_width)
		width = ALIGN(width, res->step_width);

	return width;
}

u32 vpu_helper_valid_frame_height(struct vpu_inst *inst, u32 height)
{
	const struct vpu_core_resources *res;

	if (!inst)
		return height;

	res = vpu_get_resource(inst);
	if (!res)
		return height;
	if (res->max_height)
		height = clamp(height, res->min_height, res->max_height);
	if (res->step_height)
		height = ALIGN(height, res->step_height);

	return height;
}

static u32 get_nv12_plane_size(u32 width, u32 height, int plane_no,
			       u32 stride, u32 interlaced, u32 *pbl)
{
	u32 bytesperline;
	u32 size = 0;

	bytesperline = ALIGN(width, stride);
	if (pbl)
		bytesperline = max(bytesperline, *pbl);
	height = ALIGN(height, 2);
	if (plane_no == 0)
		size = bytesperline * height;
	else if (plane_no == 1)
		size = bytesperline * height >> 1;
	if (pbl)
		*pbl = bytesperline;

	return size;
}

static u32 get_tiled_8l128_plane_size(u32 fmt, u32 width, u32 height, int plane_no,
				      u32 stride, u32 interlaced, u32 *pbl)
{
	u32 ws = 3;
	u32 hs = 7;
	u32 bitdepth = 8;
	u32 bytesperline;
	u32 size = 0;

	if (interlaced)
		hs++;
	if (fmt == V4L2_PIX_FMT_NV12M_10BE_8L128)
		bitdepth = 10;
	bytesperline = DIV_ROUND_UP(width * bitdepth, BITS_PER_BYTE);
	bytesperline = ALIGN(bytesperline, 1 << ws);
	bytesperline = ALIGN(bytesperline, stride);
	if (pbl)
		bytesperline = max(bytesperline, *pbl);
	height = ALIGN(height, 1 << hs);
	if (plane_no == 0)
		size = bytesperline * height;
	else if (plane_no == 1)
		size = (bytesperline * ALIGN(height, 1 << (hs + 1))) >> 1;
	if (pbl)
		*pbl = bytesperline;

	return size;
}

static u32 get_default_plane_size(u32 width, u32 height, int plane_no,
				  u32 stride, u32 interlaced, u32 *pbl)
{
	u32 bytesperline;
	u32 size = 0;

	bytesperline = ALIGN(width, stride);
	if (pbl)
		bytesperline = max(bytesperline, *pbl);
	if (plane_no == 0)
		size = bytesperline * height;
	if (pbl)
		*pbl = bytesperline;

	return size;
}

u32 vpu_helper_get_plane_size(u32 fmt, u32 w, u32 h, int plane_no,
			      u32 stride, u32 interlaced, u32 *pbl)
{
	switch (fmt) {
	case V4L2_PIX_FMT_NV12M:
		return get_nv12_plane_size(w, h, plane_no, stride, interlaced, pbl);
	case V4L2_PIX_FMT_NV12M_8L128:
	case V4L2_PIX_FMT_NV12M_10BE_8L128:
		return get_tiled_8l128_plane_size(fmt, w, h, plane_no, stride, interlaced, pbl);
	default:
		return get_default_plane_size(w, h, plane_no, stride, interlaced, pbl);
	}
}

int vpu_helper_copy_from_stream_buffer(struct vpu_buffer *stream_buffer,
				       u32 *rptr, u32 size, void *dst)
{
	u32 offset;
	u32 start;
	u32 end;
	void *virt;

	if (!stream_buffer || !rptr || !dst)
		return -EINVAL;

	if (!size)
		return 0;

	offset = *rptr;
	start = stream_buffer->phys;
	end = start + stream_buffer->length;
	virt = stream_buffer->virt;

	if (offset < start || offset > end)
		return -EINVAL;

	if (offset + size <= end) {
		memcpy(dst, virt + (offset - start), size);
	} else {
		memcpy(dst, virt + (offset - start), end - offset);
		memcpy(dst + end - offset, virt, size + offset - end);
	}

	*rptr = vpu_helper_step_walk(stream_buffer, offset, size);

	return 0;
}

int vpu_helper_copy_to_stream_buffer(struct vpu_buffer *stream_buffer,
				     u32 *wptr, u32 size, void *src)
{
	u32 offset;
	u32 start;
	u32 end;
	void *virt;

	if (!stream_buffer || !wptr || !src)
		return -EINVAL;

	if (!size)
		return 0;

	offset = *wptr;
	start = stream_buffer->phys;
	end = start + stream_buffer->length;
	virt = stream_buffer->virt;
	if (offset < start || offset > end)
		return -EINVAL;

	if (offset + size <= end) {
		memcpy(virt + (offset - start), src, size);
	} else {
		memcpy(virt + (offset - start), src, end - offset);
		memcpy(virt, src + end - offset, size + offset - end);
	}

	*wptr = vpu_helper_step_walk(stream_buffer, offset, size);

	return 0;
}

int vpu_helper_memset_stream_buffer(struct vpu_buffer *stream_buffer,
				    u32 *wptr, u8 val, u32 size)
{
	u32 offset;
	u32 start;
	u32 end;
	void *virt;

	if (!stream_buffer || !wptr)
		return -EINVAL;

	if (!size)
		return 0;

	offset = *wptr;
	start = stream_buffer->phys;
	end = start + stream_buffer->length;
	virt = stream_buffer->virt;
	if (offset < start || offset > end)
		return -EINVAL;

	if (offset + size <= end) {
		memset(virt + (offset - start), val, size);
	} else {
		memset(virt + (offset - start), val, end - offset);
		memset(virt, val, size + offset - end);
	}

	offset += size;
	if (offset >= end)
		offset -= stream_buffer->length;

	*wptr = offset;

	return 0;
}

u32 vpu_helper_get_free_space(struct vpu_inst *inst)
{
	struct vpu_rpc_buffer_desc desc;

	if (vpu_iface_get_stream_buffer_desc(inst, &desc))
		return 0;

	if (desc.rptr > desc.wptr)
		return desc.rptr - desc.wptr;
	else if (desc.rptr < desc.wptr)
		return (desc.end - desc.start + desc.rptr - desc.wptr);
	else
		return desc.end - desc.start;
}

u32 vpu_helper_get_used_space(struct vpu_inst *inst)
{
	struct vpu_rpc_buffer_desc desc;

	if (vpu_iface_get_stream_buffer_desc(inst, &desc))
		return 0;

	if (desc.wptr > desc.rptr)
		return desc.wptr - desc.rptr;
	else if (desc.wptr < desc.rptr)
		return (desc.end - desc.start + desc.wptr - desc.rptr);
	else
		return 0;
}

int vpu_helper_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
{
	struct vpu_inst *inst = ctrl_to_inst(ctrl);

	switch (ctrl->id) {
	case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
		ctrl->val = inst->min_buffer_cap;
		break;
	case V4L2_CID_MIN_BUFFERS_FOR_OUTPUT:
		ctrl->val = inst->min_buffer_out;
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

int vpu_helper_find_startcode(struct vpu_buffer *stream_buffer,
			      u32 pixelformat, u32 offset, u32 bytesused)
{
	u32 start_code;
	int start_code_size;
	u32 val = 0;
	int i;
	int ret = -EINVAL;

	if (!stream_buffer || !stream_buffer->virt)
		return -EINVAL;

	switch (pixelformat) {
	case V4L2_PIX_FMT_H264:
		start_code_size = 4;
		start_code = 0x00000001;
		break;
	default:
		return 0;
	}

	for (i = 0; i < bytesused; i++) {
		val = (val << 8) | vpu_helper_read_byte(stream_buffer, offset + i);
		if (i < start_code_size - 1)
			continue;
		if (val == start_code) {
			ret = i + 1 - start_code_size;
			break;
		}
	}

	return ret;
}

int vpu_find_dst_by_src(struct vpu_pair *pairs, u32 cnt, u32 src)
{
	u32 i;

	if (!pairs || !cnt)
		return -EINVAL;

	for (i = 0; i < cnt; i++) {
		if (pairs[i].src == src)
			return pairs[i].dst;
	}

	return -EINVAL;
}

int vpu_find_src_by_dst(struct vpu_pair *pairs, u32 cnt, u32 dst)
{
	u32 i;

	if (!pairs || !cnt)
		return -EINVAL;

	for (i = 0; i < cnt; i++) {
		if (pairs[i].dst == dst)
			return pairs[i].src;
	}

	return -EINVAL;
}

const char *vpu_id_name(u32 id)
{
	switch (id) {
	case VPU_CMD_ID_NOOP: return "noop";
	case VPU_CMD_ID_CONFIGURE_CODEC: return "configure codec";
	case VPU_CMD_ID_START: return "start";
	case VPU_CMD_ID_STOP: return "stop";
	case VPU_CMD_ID_ABORT: return "abort";
	case VPU_CMD_ID_RST_BUF: return "reset buf";
	case VPU_CMD_ID_SNAPSHOT: return "snapshot";
	case VPU_CMD_ID_FIRM_RESET: return "reset firmware";
	case VPU_CMD_ID_UPDATE_PARAMETER: return "update parameter";
	case VPU_CMD_ID_FRAME_ENCODE: return "encode frame";
	case VPU_CMD_ID_SKIP: return "skip";
	case VPU_CMD_ID_FS_ALLOC: return "alloc fb";
	case VPU_CMD_ID_FS_RELEASE: return "release fb";
	case VPU_CMD_ID_TIMESTAMP: return "timestamp";
	case VPU_CMD_ID_DEBUG: return "debug";
	case VPU_MSG_ID_RESET_DONE: return "reset done";
	case VPU_MSG_ID_START_DONE: return "start done";
	case VPU_MSG_ID_STOP_DONE: return "stop done";
	case VPU_MSG_ID_ABORT_DONE: return "abort done";
	case VPU_MSG_ID_BUF_RST: return "buf reset done";
	case VPU_MSG_ID_MEM_REQUEST: return "mem request";
	case VPU_MSG_ID_PARAM_UPD_DONE: return "param upd done";
	case VPU_MSG_ID_FRAME_INPUT_DONE: return "frame input done";
	case VPU_MSG_ID_ENC_DONE: return "encode done";
	case VPU_MSG_ID_DEC_DONE: return "frame display";
	case VPU_MSG_ID_FRAME_REQ: return "fb request";
	case VPU_MSG_ID_FRAME_RELEASE: return "fb release";
	case VPU_MSG_ID_SEQ_HDR_FOUND: return "seq hdr found";
	case VPU_MSG_ID_RES_CHANGE: return "resolution change";
	case VPU_MSG_ID_PIC_HDR_FOUND: return "pic hdr found";
	case VPU_MSG_ID_PIC_DECODED: return "picture decoded";
	case VPU_MSG_ID_PIC_EOS: return "eos";
	case VPU_MSG_ID_FIFO_LOW: return "fifo low";
	case VPU_MSG_ID_BS_ERROR: return "bs error";
	case VPU_MSG_ID_UNSUPPORTED: return "unsupported";
	case VPU_MSG_ID_FIRMWARE_XCPT: return "exception";
	case VPU_MSG_ID_PIC_SKIPPED: return "skipped";
	case VPU_MSG_ID_DBG_MSG: return "debug msg";
	}
	return "<unknown>";
}

const char *vpu_codec_state_name(enum vpu_codec_state state)
{
	switch (state) {
	case VPU_CODEC_STATE_DEINIT: return "initialization";
	case VPU_CODEC_STATE_CONFIGURED: return "configured";
	case VPU_CODEC_STATE_START: return "start";
	case VPU_CODEC_STATE_STARTED: return "started";
	case VPU_CODEC_STATE_ACTIVE: return "active";
	case VPU_CODEC_STATE_SEEK: return "seek";
	case VPU_CODEC_STATE_STOP: return "stop";
	case VPU_CODEC_STATE_DRAIN: return "drain";
	case VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE: return "resolution change";
	}
	return "<unknown>";
}