aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/platform/amphion/vpu_dbg.c
blob: 376196bea1787b1d2ae6a3990c674c57b225ed12 (plain) (blame)
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright 2020-2021 NXP
 */

#include <linux/init.h>
#include <linux/device.h>
#include <linux/ioctl.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/pm_runtime.h>
#include <media/v4l2-device.h>
#include <linux/debugfs.h>
#include "vpu.h"
#include "vpu_defs.h"
#include "vpu_helpers.h"
#include "vpu_cmds.h"
#include "vpu_rpc.h"
#include "vpu_v4l2.h"

struct print_buf_desc {
	u32 start_h_phy;
	u32 start_h_vir;
	u32 start_m;
	u32 bytes;
	u32 read;
	u32 write;
	char buffer[0];
};

static char *vb2_stat_name[] = {
	[VB2_BUF_STATE_DEQUEUED] = "dequeued",
	[VB2_BUF_STATE_IN_REQUEST] = "in_request",
	[VB2_BUF_STATE_PREPARING] = "preparing",
	[VB2_BUF_STATE_QUEUED] = "queued",
	[VB2_BUF_STATE_ACTIVE] = "active",
	[VB2_BUF_STATE_DONE] = "done",
	[VB2_BUF_STATE_ERROR] = "error",
};

static char *vpu_stat_name[] = {
	[VPU_BUF_STATE_IDLE] = "idle",
	[VPU_BUF_STATE_INUSE] = "inuse",
	[VPU_BUF_STATE_DECODED] = "decoded",
	[VPU_BUF_STATE_READY] = "ready",
	[VPU_BUF_STATE_SKIP] = "skip",
	[VPU_BUF_STATE_ERROR] = "error",
};

static int vpu_dbg_instance(struct seq_file *s, void *data)
{
	struct vpu_inst *inst = s->private;
	char str[128];
	int num;
	struct vb2_queue *vq;
	int i;

	if (!inst->fh.m2m_ctx)
		return 0;
	num = scnprintf(str, sizeof(str), "[%s]\n", vpu_core_type_desc(inst->type));
	if (seq_write(s, str, num))
		return 0;

	num = scnprintf(str, sizeof(str), "tgig = %d,pid = %d\n", inst->tgid, inst->pid);
	if (seq_write(s, str, num))
		return 0;
	num = scnprintf(str, sizeof(str), "state = %d\n", inst->state);
	if (seq_write(s, str, num))
		return 0;
	num = scnprintf(str, sizeof(str),
			"min_buffer_out = %d, min_buffer_cap = %d\n",
			inst->min_buffer_out, inst->min_buffer_cap);
	if (seq_write(s, str, num))
		return 0;

	vq = v4l2_m2m_get_src_vq(inst->fh.m2m_ctx);
	num = scnprintf(str, sizeof(str),
			"output (%2d, %2d): fmt = %c%c%c%c %d x %d, %d;",
			vb2_is_streaming(vq),
			vq->num_buffers,
			inst->out_format.pixfmt,
			inst->out_format.pixfmt >> 8,
			inst->out_format.pixfmt >> 16,
			inst->out_format.pixfmt >> 24,
			inst->out_format.width,
			inst->out_format.height,
			vq->last_buffer_dequeued);
	if (seq_write(s, str, num))
		return 0;
	for (i = 0; i < inst->out_format.num_planes; i++) {
		num = scnprintf(str, sizeof(str), " %d(%d)",
				inst->out_format.sizeimage[i],
				inst->out_format.bytesperline[i]);
		if (seq_write(s, str, num))
			return 0;
	}
	if (seq_write(s, "\n", 1))
		return 0;

	vq = v4l2_m2m_get_dst_vq(inst->fh.m2m_ctx);
	num = scnprintf(str, sizeof(str),
			"capture(%2d, %2d): fmt = %c%c%c%c %d x %d, %d;",
			vb2_is_streaming(vq),
			vq->num_buffers,
			inst->cap_format.pixfmt,
			inst->cap_format.pixfmt >> 8,
			inst->cap_format.pixfmt >> 16,
			inst->cap_format.pixfmt >> 24,
			inst->cap_format.width,
			inst->cap_format.height,
			vq->last_buffer_dequeued);
	if (seq_write(s, str, num))
		return 0;
	for (i = 0; i < inst->cap_format.num_planes; i++) {
		num = scnprintf(str, sizeof(str), " %d(%d)",
				inst->cap_format.sizeimage[i],
				inst->cap_format.bytesperline[i]);
		if (seq_write(s, str, num))
			return 0;
	}
	if (seq_write(s, "\n", 1))
		return 0;
	num = scnprintf(str, sizeof(str), "crop: (%d, %d) %d x %d\n",
			inst->crop.left,
			inst->crop.top,
			inst->crop.width,
			inst->crop.height);
	if (seq_write(s, str, num))
		return 0;

	vq = v4l2_m2m_get_src_vq(inst->fh.m2m_ctx);
	for (i = 0; i < vq->num_buffers; i++) {
		struct vb2_buffer *vb = vq->bufs[i];
		struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);

		if (vb->state == VB2_BUF_STATE_DEQUEUED)
			continue;
		num = scnprintf(str, sizeof(str),
				"output [%2d] state = %10s, %8s\n",
				i, vb2_stat_name[vb->state],
				vpu_stat_name[vpu_get_buffer_state(vbuf)]);
		if (seq_write(s, str, num))
			return 0;
	}

	vq = v4l2_m2m_get_dst_vq(inst->fh.m2m_ctx);
	for (i = 0; i < vq->num_buffers; i++) {
		struct vb2_buffer *vb = vq->bufs[i];
		struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);

		if (vb->state == VB2_BUF_STATE_DEQUEUED)
			continue;
		num = scnprintf(str, sizeof(str),
				"capture[%2d] state = %10s, %8s\n",
				i, vb2_stat_name[vb->state],
				vpu_stat_name[vpu_get_buffer_state(vbuf)]);
		if (seq_write(s, str, num))
			return 0;
	}

	num = scnprintf(str, sizeof(str), "sequence = %d\n", inst->sequence);
	if (seq_write(s, str, num))
		return 0;

	if (inst->use_stream_buffer) {
		num = scnprintf(str, sizeof(str), "stream_buffer = %d / %d, <%pad, 0x%x>\n",
				vpu_helper_get_used_space(inst),
				inst->stream_buffer.length,
				&inst->stream_buffer.phys,
				inst->stream_buffer.length);
		if (seq_write(s, str, num))
			return 0;
	}
	num = scnprintf(str, sizeof(str), "kfifo len = 0x%x\n", kfifo_len(&inst->msg_fifo));
	if (seq_write(s, str, num))
		return 0;

	num = scnprintf(str, sizeof(str), "flow :\n");
	if (seq_write(s, str, num))
		return 0;

	mutex_lock(&inst->core->cmd_lock);
	for (i = 0; i < ARRAY_SIZE(inst->flows); i++) {
		u32 idx = (inst->flow_idx + i) % (ARRAY_SIZE(inst->flows));

		if (!inst->flows[idx])
			continue;
		num = scnprintf(str, sizeof(str), "\t[%s]0x%x\n",
				inst->flows[idx] >= VPU_MSG_ID_NOOP ? "M" : "C",
				inst->flows[idx]);
		if (seq_write(s, str, num)) {
			mutex_unlock(&inst->core->cmd_lock);
			return 0;
		}
	}
	mutex_unlock(&inst->core->cmd_lock);

	i = 0;
	while (true) {
		num = call_vop(inst, get_debug_info, str, sizeof(str), i++);
		if (num <= 0)
			break;
		if (seq_write(s, str, num))
			return 0;
	}

	return 0;
}

static int vpu_dbg_core(struct seq_file *s, void *data)
{
	struct vpu_core *core = s->private;
	struct vpu_shared_addr *iface = core->iface;
	char str[128];
	int num;

	num = scnprintf(str, sizeof(str), "[%s]\n", vpu_core_type_desc(core->type));
	if (seq_write(s, str, num))
		return 0;

	num = scnprintf(str, sizeof(str), "boot_region  = <%pad, 0x%x>\n",
			&core->fw.phys, core->fw.length);
	if (seq_write(s, str, num))
		return 0;
	num = scnprintf(str, sizeof(str), "rpc_region   = <%pad, 0x%x> used = 0x%x\n",
			&core->rpc.phys, core->rpc.length, core->rpc.bytesused);
	if (seq_write(s, str, num))
		return 0;
	num = scnprintf(str, sizeof(str), "fwlog_region = <%pad, 0x%x>\n",
			&core->log.phys, core->log.length);
	if (seq_write(s, str, num))
		return 0;

	num = scnprintf(str, sizeof(str), "state = %d\n", core->state);
	if (seq_write(s, str, num))
		return 0;
	if (core->state == VPU_CORE_DEINIT)
		return 0;
	num = scnprintf(str, sizeof(str), "fw version = %d.%d.%d\n",
			(core->fw_version >> 16) & 0xff,
			(core->fw_version >> 8) & 0xff,
			core->fw_version & 0xff);
	if (seq_write(s, str, num))
		return 0;
	num = scnprintf(str, sizeof(str), "instances = %d/%d (0x%02lx), %d\n",
			hweight32(core->instance_mask),
			core->supported_instance_count,
			core->instance_mask,
			core->request_count);
	if (seq_write(s, str, num))
		return 0;
	num = scnprintf(str, sizeof(str), "kfifo len = 0x%x\n", kfifo_len(&core->msg_fifo));
	if (seq_write(s, str, num))
		return 0;
	num = scnprintf(str, sizeof(str),
			"cmd_buf:[0x%x, 0x%x], wptr = 0x%x, rptr = 0x%x\n",
			iface->cmd_desc->start,
			iface->cmd_desc->end,
			iface->cmd_desc->wptr,
			iface->cmd_desc->rptr);
	if (seq_write(s, str, num))
		return 0;
	num = scnprintf(str, sizeof(str),
			"msg_buf:[0x%x, 0x%x], wptr = 0x%x, rptr = 0x%x\n",
			iface->msg_desc->start,
			iface->msg_desc->end,
			iface->msg_desc->wptr,
			iface->msg_desc->rptr);
	if (seq_write(s, str, num))
		return 0;

	return 0;
}

static int vpu_dbg_fwlog(struct seq_file *s, void *data)
{
	struct vpu_core *core = s->private;
	struct print_buf_desc *print_buf;
	int length;
	u32 rptr;
	u32 wptr;
	int ret = 0;

	if (!core->log.virt || core->state == VPU_CORE_DEINIT)
		return 0;

	print_buf = core->log.virt;
	rptr = print_buf->read;
	wptr = print_buf->write;

	if (rptr == wptr)
		return 0;
	else if (rptr < wptr)
		length = wptr - rptr;
	else
		length = print_buf->bytes + wptr - rptr;

	if (s->count + length >= s->size) {
		s->count = s->size;
		return 0;
	}

	if (rptr + length >= print_buf->bytes) {
		int num = print_buf->bytes - rptr;

		if (seq_write(s, print_buf->buffer + rptr, num))
			ret = -1;
		length -= num;
		rptr = 0;
	}

	if (length) {
		if (seq_write(s, print_buf->buffer + rptr, length))
			ret = -1;
		rptr += length;
	}
	if (!ret)
		print_buf->read = rptr;

	return 0;
}

static int vpu_dbg_inst_open(struct inode *inode, struct file *filp)
{
	return single_open(filp, vpu_dbg_instance, inode->i_private);
}

static ssize_t vpu_dbg_inst_write(struct file *file,
				  const char __user *user_buf, size_t size, loff_t *ppos)
{
	struct seq_file *s = file->private_data;
	struct vpu_inst *inst = s->private;

	vpu_session_debug(inst);

	return size;
}

static ssize_t vpu_dbg_core_write(struct file *file,
				  const char __user *user_buf, size_t size, loff_t *ppos)
{
	struct seq_file *s = file->private_data;
	struct vpu_core *core = s->private;

	pm_runtime_resume_and_get(core->dev);
	mutex_lock(&core->lock);
	if (core->state != VPU_CORE_DEINIT && !core->instance_mask) {
		dev_info(core->dev, "reset\n");
		if (!vpu_core_sw_reset(core)) {
			core->state = VPU_CORE_ACTIVE;
			core->hang_mask = 0;
		}
	}
	mutex_unlock(&core->lock);
	pm_runtime_put_sync(core->dev);

	return size;
}

static int vpu_dbg_core_open(struct inode *inode, struct file *filp)
{
	return single_open(filp, vpu_dbg_core, inode->i_private);
}

static int vpu_dbg_fwlog_open(struct inode *inode, struct file *filp)
{
	return single_open(filp, vpu_dbg_fwlog, inode->i_private);
}

static const struct file_operations vpu_dbg_inst_fops = {
	.owner = THIS_MODULE,
	.open = vpu_dbg_inst_open,
	.release = single_release,
	.read = seq_read,
	.write = vpu_dbg_inst_write,
};

static const struct file_operations vpu_dbg_core_fops = {
	.owner = THIS_MODULE,
	.open = vpu_dbg_core_open,
	.release = single_release,
	.read = seq_read,
	.write = vpu_dbg_core_write,
};

static const struct file_operations vpu_dbg_fwlog_fops = {
	.owner = THIS_MODULE,
	.open = vpu_dbg_fwlog_open,
	.release = single_release,
	.read = seq_read,
};

int vpu_inst_create_dbgfs_file(struct vpu_inst *inst)
{
	struct vpu_dev *vpu;
	char name[64];

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

	vpu = inst->core->vpu;
	if (!vpu->debugfs)
		return -EINVAL;

	if (inst->debugfs)
		return 0;

	scnprintf(name, sizeof(name), "instance.%d.%d", inst->core->id, inst->id);
	inst->debugfs = debugfs_create_file((const char *)name,
					    VERIFY_OCTAL_PERMISSIONS(0644),
					    vpu->debugfs,
					    inst,
					    &vpu_dbg_inst_fops);
	if (!inst->debugfs) {
		dev_err(inst->dev, "vpu create debugfs %s fail\n", name);
		return -EINVAL;
	}

	return 0;
}

int vpu_inst_remove_dbgfs_file(struct vpu_inst *inst)
{
	if (!inst)
		return 0;

	debugfs_remove(inst->debugfs);
	inst->debugfs = NULL;

	return 0;
}

int vpu_core_create_dbgfs_file(struct vpu_core *core)
{
	struct vpu_dev *vpu;
	char name[64];

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

	vpu = core->vpu;
	if (!vpu->debugfs)
		return -EINVAL;

	if (!core->debugfs) {
		scnprintf(name, sizeof(name), "core.%d", core->id);
		core->debugfs = debugfs_create_file((const char *)name,
						    VERIFY_OCTAL_PERMISSIONS(0644),
						    vpu->debugfs,
						    core,
						    &vpu_dbg_core_fops);
		if (!core->debugfs) {
			dev_err(core->dev, "vpu create debugfs %s fail\n", name);
			return -EINVAL;
		}
	}
	if (!core->debugfs_fwlog) {
		scnprintf(name, sizeof(name), "fwlog.%d", core->id);
		core->debugfs_fwlog = debugfs_create_file((const char *)name,
							  VERIFY_OCTAL_PERMISSIONS(0444),
							  vpu->debugfs,
							  core,
							  &vpu_dbg_fwlog_fops);
		if (!core->debugfs_fwlog) {
			dev_err(core->dev, "vpu create debugfs %s fail\n", name);
			return -EINVAL;
		}
	}

	return 0;
}

int vpu_core_remove_dbgfs_file(struct vpu_core *core)
{
	if (!core)
		return 0;
	debugfs_remove(core->debugfs);
	core->debugfs = NULL;
	debugfs_remove(core->debugfs_fwlog);
	core->debugfs_fwlog = NULL;

	return 0;
}

void vpu_inst_record_flow(struct vpu_inst *inst, u32 flow)
{
	if (!inst)
		return;

	inst->flows[inst->flow_idx] = flow;
	inst->flow_idx = (inst->flow_idx + 1) % (ARRAY_SIZE(inst->flows));
}