summaryrefslogtreecommitdiffstats
path: root/drivers/media/platform/sti/hva/hva-debugfs.c
blob: 9f7e8ac875d16bd87a2101fb37f221e3e586317f (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
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) STMicroelectronics SA 2015
 * Authors: Yannick Fertre <yannick.fertre@st.com>
 *          Hugues Fruchet <hugues.fruchet@st.com>
 */

#include <linux/debugfs.h>

#include "hva.h"
#include "hva-hw.h"

static void format_ctx(struct seq_file *s, struct hva_ctx *ctx)
{
	struct hva_streaminfo *stream = &ctx->streaminfo;
	struct hva_frameinfo *frame = &ctx->frameinfo;
	struct hva_controls *ctrls = &ctx->ctrls;
	struct hva_ctx_dbg *dbg = &ctx->dbg;
	u32 bitrate_mode, aspect, entropy, vui_sar, sei_fp;

	seq_printf(s, "|-%s\n  |\n", ctx->name);

	seq_printf(s, "  |-[%sframe info]\n",
		   ctx->flags & HVA_FLAG_FRAMEINFO ? "" : "default ");
	seq_printf(s, "  | |- pixel format=%4.4s\n"
		      "  | |- wxh=%dx%d\n"
		      "  | |- wxh (w/ encoder alignment constraint)=%dx%d\n"
		      "  |\n",
		      (char *)&frame->pixelformat,
		      frame->width, frame->height,
		      frame->aligned_width, frame->aligned_height);

	seq_printf(s, "  |-[%sstream info]\n",
		   ctx->flags & HVA_FLAG_STREAMINFO ? "" : "default ");
	seq_printf(s, "  | |- stream format=%4.4s\n"
		      "  | |- wxh=%dx%d\n"
		      "  | |- %s\n"
		      "  | |- %s\n"
		      "  |\n",
		      (char *)&stream->streamformat,
		      stream->width, stream->height,
		      stream->profile, stream->level);

	bitrate_mode = V4L2_CID_MPEG_VIDEO_BITRATE_MODE;
	aspect = V4L2_CID_MPEG_VIDEO_ASPECT;
	seq_puts(s, "  |-[parameters]\n");
	seq_printf(s, "  | |- %s\n"
		      "  | |- bitrate=%d bps\n"
		      "  | |- GOP size=%d\n"
		      "  | |- video aspect=%s\n"
		      "  | |- framerate=%d/%d\n",
		      v4l2_ctrl_get_menu(bitrate_mode)[ctrls->bitrate_mode],
		      ctrls->bitrate,
		      ctrls->gop_size,
		      v4l2_ctrl_get_menu(aspect)[ctrls->aspect],
		      ctrls->time_per_frame.denominator,
		      ctrls->time_per_frame.numerator);

	entropy = V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE;
	vui_sar = V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC;
	sei_fp =  V4L2_CID_MPEG_VIDEO_H264_SEI_FP_ARRANGEMENT_TYPE;
	if (stream->streamformat == V4L2_PIX_FMT_H264) {
		seq_printf(s, "  | |- %s entropy mode\n"
			      "  | |- CPB size=%d kB\n"
			      "  | |- DCT8x8 enable=%s\n"
			      "  | |- qpmin=%d\n"
			      "  | |- qpmax=%d\n"
			      "  | |- PAR enable=%s\n"
			      "  | |- PAR id=%s\n"
			      "  | |- SEI frame packing enable=%s\n"
			      "  | |- SEI frame packing type=%s\n",
			      v4l2_ctrl_get_menu(entropy)[ctrls->entropy_mode],
			      ctrls->cpb_size,
			      ctrls->dct8x8 ? "true" : "false",
			      ctrls->qpmin,
			      ctrls->qpmax,
			      ctrls->vui_sar ? "true" : "false",
			      v4l2_ctrl_get_menu(vui_sar)[ctrls->vui_sar_idc],
			      ctrls->sei_fp ? "true" : "false",
			      v4l2_ctrl_get_menu(sei_fp)[ctrls->sei_fp_type]);
	}

	if (ctx->sys_errors || ctx->encode_errors || ctx->frame_errors) {
		seq_puts(s, "  |\n  |-[errors]\n");
		seq_printf(s, "  | |- system=%d\n"
			      "  | |- encoding=%d\n"
			      "  | |- frame=%d\n",
			      ctx->sys_errors,
			      ctx->encode_errors,
			      ctx->frame_errors);
	}

	seq_puts(s, "  |\n  |-[performances]\n");
	seq_printf(s, "  | |- frames encoded=%d\n"
		      "  | |- avg HW processing duration (0.1ms)=%d [min=%d, max=%d]\n"
		      "  | |- avg encoding period (0.1ms)=%d [min=%d, max=%d]\n"
		      "  | |- avg fps (0.1Hz)=%d\n"
		      "  | |- max reachable fps (0.1Hz)=%d\n"
		      "  | |- avg bitrate (kbps)=%d [min=%d, max=%d]\n"
		      "  | |- last bitrate (kbps)=%d\n",
		      dbg->cnt_duration,
		      dbg->avg_duration,
		      dbg->min_duration,
		      dbg->max_duration,
		      dbg->avg_period,
		      dbg->min_period,
		      dbg->max_period,
		      dbg->avg_fps,
		      dbg->max_fps,
		      dbg->avg_bitrate,
		      dbg->min_bitrate,
		      dbg->max_bitrate,
		      dbg->last_bitrate);
}

/*
 * performance debug info
 */
void hva_dbg_perf_begin(struct hva_ctx *ctx)
{
	u64 div;
	u32 period;
	u32 bitrate;
	struct hva_ctx_dbg *dbg = &ctx->dbg;
	ktime_t prev = dbg->begin;

	dbg->begin = ktime_get();

	if (dbg->is_valid_period) {
		/* encoding period */
		div = (u64)ktime_us_delta(dbg->begin, prev);
		do_div(div, 100);
		period = (u32)div;
		dbg->min_period = min(period, dbg->min_period);
		dbg->max_period = max(period, dbg->max_period);
		dbg->total_period += period;
		dbg->cnt_period++;

		/*
		 * minimum and maximum bitrates are based on the
		 * encoding period values upon a window of 32 samples
		 */
		dbg->window_duration += period;
		dbg->cnt_window++;
		if (dbg->cnt_window >= 32) {
			/*
			 * bitrate in kbps = (size * 8 / 1000) /
			 *                   (duration / 10000)
			 *                 = size * 80 / duration
			 */
			if (dbg->window_duration > 0) {
				div = (u64)dbg->window_stream_size * 80;
				do_div(div, dbg->window_duration);
				bitrate = (u32)div;
				dbg->last_bitrate = bitrate;
				dbg->min_bitrate = min(bitrate,
						       dbg->min_bitrate);
				dbg->max_bitrate = max(bitrate,
						       dbg->max_bitrate);
			}
			dbg->window_stream_size = 0;
			dbg->window_duration = 0;
			dbg->cnt_window = 0;
		}
	}

	/*
	 * filter sequences valid for performance:
	 * - begin/begin (no stream available) is an invalid sequence
	 * - begin/end is a valid sequence
	 */
	dbg->is_valid_period = false;
}

void hva_dbg_perf_end(struct hva_ctx *ctx, struct hva_stream *stream)
{
	struct device *dev = ctx_to_dev(ctx);
	u64 div;
	u32 duration;
	u32 bytesused;
	u32 timestamp;
	struct hva_ctx_dbg *dbg = &ctx->dbg;
	ktime_t end = ktime_get();

	/* stream bytesused and timestamp in us */
	bytesused = vb2_get_plane_payload(&stream->vbuf.vb2_buf, 0);
	div = stream->vbuf.vb2_buf.timestamp;
	do_div(div, 1000);
	timestamp = (u32)div;

	/* encoding duration */
	div = (u64)ktime_us_delta(end, dbg->begin);

	dev_dbg(dev,
		"%s perf stream[%d] dts=%d encoded using %d bytes in %d us",
		ctx->name,
		stream->vbuf.sequence,
		timestamp,
		bytesused, (u32)div);

	do_div(div, 100);
	duration = (u32)div;

	dbg->min_duration = min(duration, dbg->min_duration);
	dbg->max_duration = max(duration, dbg->max_duration);
	dbg->total_duration += duration;
	dbg->cnt_duration++;

	/*
	 * the average bitrate is based on the total stream size
	 * and the total encoding periods
	 */
	dbg->total_stream_size += bytesused;
	dbg->window_stream_size += bytesused;

	dbg->is_valid_period = true;
}

static void hva_dbg_perf_compute(struct hva_ctx *ctx)
{
	u64 div;
	struct hva_ctx_dbg *dbg = &ctx->dbg;

	if (dbg->cnt_duration > 0) {
		div = (u64)dbg->total_duration;
		do_div(div, dbg->cnt_duration);
		dbg->avg_duration = (u32)div;
	} else {
		dbg->avg_duration = 0;
	}

	if (dbg->total_duration > 0) {
		div = (u64)dbg->cnt_duration * 100000;
		do_div(div, dbg->total_duration);
		dbg->max_fps = (u32)div;
	} else {
		dbg->max_fps = 0;
	}

	if (dbg->cnt_period > 0) {
		div = (u64)dbg->total_period;
		do_div(div, dbg->cnt_period);
		dbg->avg_period = (u32)div;
	} else {
		dbg->avg_period = 0;
	}

	if (dbg->total_period > 0) {
		div = (u64)dbg->cnt_period * 100000;
		do_div(div, dbg->total_period);
		dbg->avg_fps = (u32)div;
	} else {
		dbg->avg_fps = 0;
	}

	if (dbg->total_period > 0) {
		/*
		 * bitrate in kbps = (video size * 8 / 1000) /
		 *                   (video duration / 10000)
		 *                 = video size * 80 / video duration
		 */
		div = (u64)dbg->total_stream_size * 80;
		do_div(div, dbg->total_period);
		dbg->avg_bitrate = (u32)div;
	} else {
		dbg->avg_bitrate = 0;
	}
}

/*
 * device debug info
 */

static int hva_dbg_device(struct seq_file *s, void *data)
{
	struct hva_dev *hva = s->private;

	seq_printf(s, "[%s]\n", hva->v4l2_dev.name);
	seq_printf(s, "registered as /dev/video%d\n", hva->vdev->num);

	return 0;
}

static int hva_dbg_encoders(struct seq_file *s, void *data)
{
	struct hva_dev *hva = s->private;
	unsigned int i = 0;

	seq_printf(s, "[encoders]\n|- %d registered encoders:\n",
		   hva->nb_of_encoders);

	while (hva->encoders[i]) {
		seq_printf(s, "|- %s: %4.4s => %4.4s\n", hva->encoders[i]->name,
			   (char *)&hva->encoders[i]->pixelformat,
			   (char *)&hva->encoders[i]->streamformat);
		i++;
	}

	return 0;
}

static int hva_dbg_last(struct seq_file *s, void *data)
{
	struct hva_dev *hva = s->private;
	struct hva_ctx *last_ctx = &hva->dbg.last_ctx;

	if (last_ctx->flags & HVA_FLAG_STREAMINFO) {
		seq_puts(s, "[last encoding]\n");

		hva_dbg_perf_compute(last_ctx);
		format_ctx(s, last_ctx);
	} else {
		seq_puts(s, "[no information recorded about last encoding]\n");
	}

	return 0;
}

static int hva_dbg_regs(struct seq_file *s, void *data)
{
	struct hva_dev *hva = s->private;

	hva_hw_dump_regs(hva, s);

	return 0;
}

#define hva_dbg_declare(name)						  \
	static int hva_dbg_##name##_open(struct inode *i, struct file *f) \
	{								  \
		return single_open(f, hva_dbg_##name, i->i_private);	  \
	}								  \
	static const struct file_operations hva_dbg_##name##_fops = {	  \
		.open		= hva_dbg_##name##_open,		  \
		.read		= seq_read,				  \
		.llseek		= seq_lseek,				  \
		.release	= single_release,			  \
	}

#define hva_dbg_create_entry(name)					 \
	debugfs_create_file(#name, 0444, hva->dbg.debugfs_entry, hva, \
			    &hva_dbg_##name##_fops)

hva_dbg_declare(device);
hva_dbg_declare(encoders);
hva_dbg_declare(last);
hva_dbg_declare(regs);

void hva_debugfs_create(struct hva_dev *hva)
{
	hva->dbg.debugfs_entry = debugfs_create_dir(HVA_NAME, NULL);
	if (!hva->dbg.debugfs_entry)
		goto err;

	if (!hva_dbg_create_entry(device))
		goto err;

	if (!hva_dbg_create_entry(encoders))
		goto err;

	if (!hva_dbg_create_entry(last))
		goto err;

	if (!hva_dbg_create_entry(regs))
		goto err;

	return;

err:
	hva_debugfs_remove(hva);
}

void hva_debugfs_remove(struct hva_dev *hva)
{
	debugfs_remove_recursive(hva->dbg.debugfs_entry);
	hva->dbg.debugfs_entry = NULL;
}

/*
 * context (instance) debug info
 */

static int hva_dbg_ctx(struct seq_file *s, void *data)
{
	struct hva_ctx *ctx = s->private;

	seq_printf(s, "[running encoding %d]\n", ctx->id);

	hva_dbg_perf_compute(ctx);
	format_ctx(s, ctx);

	return 0;
}

hva_dbg_declare(ctx);

void hva_dbg_ctx_create(struct hva_ctx *ctx)
{
	struct hva_dev *hva = ctx->hva_dev;
	char name[4] = "";

	ctx->dbg.min_duration = UINT_MAX;
	ctx->dbg.min_period = UINT_MAX;
	ctx->dbg.min_bitrate = UINT_MAX;

	snprintf(name, sizeof(name), "%d", hva->instance_id);

	ctx->dbg.debugfs_entry = debugfs_create_file(name, 0444,
						     hva->dbg.debugfs_entry,
						     ctx, &hva_dbg_ctx_fops);
}

void hva_dbg_ctx_remove(struct hva_ctx *ctx)
{
	struct hva_dev *hva = ctx->hva_dev;

	if (ctx->flags & HVA_FLAG_STREAMINFO)
		/* save context before removing */
		memcpy(&hva->dbg.last_ctx, ctx, sizeof(*ctx));

	debugfs_remove(ctx->dbg.debugfs_entry);
}