summaryrefslogtreecommitdiffstats
path: root/drivers/gpu/drm/imagination/pvr_fw_trace.c
blob: 31199e45b72eadd78ae10ee059bba5bfa978d4a6 (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
// SPDX-License-Identifier: GPL-2.0-only OR MIT
/* Copyright (c) 2023 Imagination Technologies Ltd. */

#include "pvr_device.h"
#include "pvr_gem.h"
#include "pvr_rogue_fwif.h"
#include "pvr_rogue_fwif_sf.h"
#include "pvr_fw_trace.h"

#include <drm/drm_drv.h>
#include <drm/drm_file.h>

#include <linux/build_bug.h>
#include <linux/dcache.h>
#include <linux/sysfs.h>
#include <linux/types.h>

static void
tracebuf_ctrl_init(void *cpu_ptr, void *priv)
{
	struct rogue_fwif_tracebuf *tracebuf_ctrl = cpu_ptr;
	struct pvr_fw_trace *fw_trace = priv;
	u32 thread_nr;

	tracebuf_ctrl->tracebuf_size_in_dwords = ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS;
	tracebuf_ctrl->tracebuf_flags = 0;

	if (fw_trace->group_mask)
		tracebuf_ctrl->log_type = fw_trace->group_mask | ROGUE_FWIF_LOG_TYPE_TRACE;
	else
		tracebuf_ctrl->log_type = ROGUE_FWIF_LOG_TYPE_NONE;

	for (thread_nr = 0; thread_nr < ARRAY_SIZE(fw_trace->buffers); thread_nr++) {
		struct rogue_fwif_tracebuf_space *tracebuf_space =
			&tracebuf_ctrl->tracebuf[thread_nr];
		struct pvr_fw_trace_buffer *trace_buffer = &fw_trace->buffers[thread_nr];

		pvr_fw_object_get_fw_addr(trace_buffer->buf_obj,
					  &tracebuf_space->trace_buffer_fw_addr);

		tracebuf_space->trace_buffer = trace_buffer->buf;
		tracebuf_space->trace_pointer = 0;
	}
}

int pvr_fw_trace_init(struct pvr_device *pvr_dev)
{
	struct pvr_fw_trace *fw_trace = &pvr_dev->fw_dev.fw_trace;
	struct drm_device *drm_dev = from_pvr_device(pvr_dev);
	u32 thread_nr;
	int err;

	for (thread_nr = 0; thread_nr < ARRAY_SIZE(fw_trace->buffers); thread_nr++) {
		struct pvr_fw_trace_buffer *trace_buffer = &fw_trace->buffers[thread_nr];

		trace_buffer->buf =
			pvr_fw_object_create_and_map(pvr_dev,
						     ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS *
						     sizeof(*trace_buffer->buf),
						     PVR_BO_FW_FLAGS_DEVICE_UNCACHED |
						     PVR_BO_FW_NO_CLEAR_ON_RESET,
						     NULL, NULL, &trace_buffer->buf_obj);
		if (IS_ERR(trace_buffer->buf)) {
			drm_err(drm_dev, "Unable to allocate trace buffer\n");
			err = PTR_ERR(trace_buffer->buf);
			trace_buffer->buf = NULL;
			goto err_free_buf;
		}
	}

	/* TODO: Provide control of group mask. */
	fw_trace->group_mask = 0;

	fw_trace->tracebuf_ctrl =
		pvr_fw_object_create_and_map(pvr_dev,
					     sizeof(*fw_trace->tracebuf_ctrl),
					     PVR_BO_FW_FLAGS_DEVICE_UNCACHED |
					     PVR_BO_FW_NO_CLEAR_ON_RESET,
					     tracebuf_ctrl_init, fw_trace,
					     &fw_trace->tracebuf_ctrl_obj);
	if (IS_ERR(fw_trace->tracebuf_ctrl)) {
		drm_err(drm_dev, "Unable to allocate trace buffer control structure\n");
		err = PTR_ERR(fw_trace->tracebuf_ctrl);
		goto err_free_buf;
	}

	BUILD_BUG_ON(ARRAY_SIZE(fw_trace->tracebuf_ctrl->tracebuf) !=
		     ARRAY_SIZE(fw_trace->buffers));

	for (thread_nr = 0; thread_nr < ARRAY_SIZE(fw_trace->buffers); thread_nr++) {
		struct rogue_fwif_tracebuf_space *tracebuf_space =
			&fw_trace->tracebuf_ctrl->tracebuf[thread_nr];
		struct pvr_fw_trace_buffer *trace_buffer = &fw_trace->buffers[thread_nr];

		trace_buffer->tracebuf_space = tracebuf_space;
	}

	return 0;

err_free_buf:
	for (thread_nr = 0; thread_nr < ARRAY_SIZE(fw_trace->buffers); thread_nr++) {
		struct pvr_fw_trace_buffer *trace_buffer = &fw_trace->buffers[thread_nr];

		if (trace_buffer->buf)
			pvr_fw_object_unmap_and_destroy(trace_buffer->buf_obj);
	}

	return err;
}

void pvr_fw_trace_fini(struct pvr_device *pvr_dev)
{
	struct pvr_fw_trace *fw_trace = &pvr_dev->fw_dev.fw_trace;
	u32 thread_nr;

	for (thread_nr = 0; thread_nr < ARRAY_SIZE(fw_trace->buffers); thread_nr++) {
		struct pvr_fw_trace_buffer *trace_buffer = &fw_trace->buffers[thread_nr];

		pvr_fw_object_unmap_and_destroy(trace_buffer->buf_obj);
	}
	pvr_fw_object_unmap_and_destroy(fw_trace->tracebuf_ctrl_obj);
}

#if defined(CONFIG_DEBUG_FS)

/**
 * update_logtype() - Send KCCB command to trigger FW to update logtype
 * @pvr_dev: Target PowerVR device
 * @group_mask: New log group mask.
 *
 * Returns:
 *  * 0 on success,
 *  * Any error returned by pvr_kccb_send_cmd(), or
 *  * -%EIO if the device is lost.
 */
static int
update_logtype(struct pvr_device *pvr_dev, u32 group_mask)
{
	struct pvr_fw_trace *fw_trace = &pvr_dev->fw_dev.fw_trace;
	struct rogue_fwif_kccb_cmd cmd;
	int idx;
	int err;

	if (group_mask)
		fw_trace->tracebuf_ctrl->log_type = ROGUE_FWIF_LOG_TYPE_TRACE | group_mask;
	else
		fw_trace->tracebuf_ctrl->log_type = ROGUE_FWIF_LOG_TYPE_NONE;

	fw_trace->group_mask = group_mask;

	down_read(&pvr_dev->reset_sem);
	if (!drm_dev_enter(from_pvr_device(pvr_dev), &idx)) {
		err = -EIO;
		goto err_up_read;
	}

	cmd.cmd_type = ROGUE_FWIF_KCCB_CMD_LOGTYPE_UPDATE;
	cmd.kccb_flags = 0;

	err = pvr_kccb_send_cmd(pvr_dev, &cmd, NULL);

	drm_dev_exit(idx);

err_up_read:
	up_read(&pvr_dev->reset_sem);

	return err;
}

struct pvr_fw_trace_seq_data {
	/** @buffer: Pointer to copy of trace data. */
	u32 *buffer;

	/** @start_offset: Starting offset in trace data, as reported by FW. */
	u32 start_offset;

	/** @idx: Current index into trace data. */
	u32 idx;

	/** @assert_buf: Trace assert buffer, as reported by FW. */
	struct rogue_fwif_file_info_buf assert_buf;
};

static u32 find_sfid(u32 id)
{
	u32 i;

	for (i = 0; i < ARRAY_SIZE(stid_fmts); i++) {
		if (stid_fmts[i].id == id)
			return i;
	}

	return ROGUE_FW_SF_LAST;
}

static u32 read_fw_trace(struct pvr_fw_trace_seq_data *trace_seq_data, u32 offset)
{
	u32 idx;

	idx = trace_seq_data->idx + offset;
	if (idx >= ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS)
		return 0;

	idx = (idx + trace_seq_data->start_offset) % ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS;
	return trace_seq_data->buffer[idx];
}

/**
 * fw_trace_get_next() - Advance trace index to next entry
 * @trace_seq_data: Trace sequence data.
 *
 * Returns:
 *  * %true if trace index is now pointing to a valid entry, or
 *  * %false if trace index is pointing to an invalid entry, or has hit the end
 *    of the trace.
 */
static bool fw_trace_get_next(struct pvr_fw_trace_seq_data *trace_seq_data)
{
	u32 id, sf_id;

	while (trace_seq_data->idx < ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS) {
		id = read_fw_trace(trace_seq_data, 0);
		trace_seq_data->idx++;
		if (!ROGUE_FW_LOG_VALIDID(id))
			continue;
		if (id == ROGUE_FW_SF_MAIN_ASSERT_FAILED) {
			/* Assertion failure marks the end of the trace. */
			return false;
		}

		sf_id = find_sfid(id);
		if (sf_id == ROGUE_FW_SF_FIRST)
			continue;
		if (sf_id == ROGUE_FW_SF_LAST) {
			/*
			 * Could not match with an ID in the SF table, trace is
			 * most likely corrupt from this point.
			 */
			return false;
		}

		/* Skip over the timestamp, and any parameters. */
		trace_seq_data->idx += 2 + ROGUE_FW_SF_PARAMNUM(id);

		/* Ensure index is now pointing to a valid trace entry. */
		id = read_fw_trace(trace_seq_data, 0);
		if (!ROGUE_FW_LOG_VALIDID(id))
			continue;

		return true;
	}

	/* Hit end of trace data. */
	return false;
}

/**
 * fw_trace_get_first() - Find first valid entry in trace
 * @trace_seq_data: Trace sequence data.
 *
 * Skips over invalid (usually zero) and ROGUE_FW_SF_FIRST entries.
 *
 * If the trace has no valid entries, this function will exit with the trace
 * index pointing to the end of the trace. trace_seq_show() will return an error
 * in this state.
 */
static void fw_trace_get_first(struct pvr_fw_trace_seq_data *trace_seq_data)
{
	trace_seq_data->idx = 0;

	while (trace_seq_data->idx < ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS) {
		u32 id = read_fw_trace(trace_seq_data, 0);

		if (ROGUE_FW_LOG_VALIDID(id)) {
			u32 sf_id = find_sfid(id);

			if (sf_id != ROGUE_FW_SF_FIRST)
				break;
		}
		trace_seq_data->idx++;
	}
}

static void *fw_trace_seq_start(struct seq_file *s, loff_t *pos)
{
	struct pvr_fw_trace_seq_data *trace_seq_data = s->private;
	u32 i;

	/* Reset trace index, then advance to *pos. */
	fw_trace_get_first(trace_seq_data);

	for (i = 0; i < *pos; i++) {
		if (!fw_trace_get_next(trace_seq_data))
			return NULL;
	}

	return (trace_seq_data->idx < ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS) ? pos : NULL;
}

static void *fw_trace_seq_next(struct seq_file *s, void *v, loff_t *pos)
{
	struct pvr_fw_trace_seq_data *trace_seq_data = s->private;

	(*pos)++;
	if (!fw_trace_get_next(trace_seq_data))
		return NULL;

	return (trace_seq_data->idx < ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS) ? pos : NULL;
}

static void fw_trace_seq_stop(struct seq_file *s, void *v)
{
}

static int fw_trace_seq_show(struct seq_file *s, void *v)
{
	struct pvr_fw_trace_seq_data *trace_seq_data = s->private;
	u64 timestamp;
	u32 id;
	u32 sf_id;

	if (trace_seq_data->idx >= ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS)
		return -EINVAL;

	id = read_fw_trace(trace_seq_data, 0);
	/* Index is not pointing at a valid entry. */
	if (!ROGUE_FW_LOG_VALIDID(id))
		return -EINVAL;

	sf_id = find_sfid(id);
	/* Index is not pointing at a valid entry. */
	if (sf_id == ROGUE_FW_SF_LAST)
		return -EINVAL;

	timestamp = read_fw_trace(trace_seq_data, 1) |
		((u64)read_fw_trace(trace_seq_data, 2) << 32);
	timestamp = (timestamp & ~ROGUE_FWT_TIMESTAMP_TIME_CLRMSK) >>
		ROGUE_FWT_TIMESTAMP_TIME_SHIFT;

	seq_printf(s, "[%llu] : ", timestamp);
	if (id == ROGUE_FW_SF_MAIN_ASSERT_FAILED) {
		seq_printf(s, "ASSERTION %s failed at %s:%u",
			   trace_seq_data->assert_buf.info,
			   trace_seq_data->assert_buf.path,
			   trace_seq_data->assert_buf.line_num);
	} else {
		seq_printf(s, stid_fmts[sf_id].name,
			   read_fw_trace(trace_seq_data, 3),
			   read_fw_trace(trace_seq_data, 4),
			   read_fw_trace(trace_seq_data, 5),
			   read_fw_trace(trace_seq_data, 6),
			   read_fw_trace(trace_seq_data, 7),
			   read_fw_trace(trace_seq_data, 8),
			   read_fw_trace(trace_seq_data, 9),
			   read_fw_trace(trace_seq_data, 10),
			   read_fw_trace(trace_seq_data, 11),
			   read_fw_trace(trace_seq_data, 12),
			   read_fw_trace(trace_seq_data, 13),
			   read_fw_trace(trace_seq_data, 14),
			   read_fw_trace(trace_seq_data, 15),
			   read_fw_trace(trace_seq_data, 16),
			   read_fw_trace(trace_seq_data, 17),
			   read_fw_trace(trace_seq_data, 18),
			   read_fw_trace(trace_seq_data, 19),
			   read_fw_trace(trace_seq_data, 20),
			   read_fw_trace(trace_seq_data, 21),
			   read_fw_trace(trace_seq_data, 22));
	}
	seq_puts(s, "\n");
	return 0;
}

static const struct seq_operations pvr_fw_trace_seq_ops = {
	.start = fw_trace_seq_start,
	.next = fw_trace_seq_next,
	.stop = fw_trace_seq_stop,
	.show = fw_trace_seq_show
};

static int fw_trace_open(struct inode *inode, struct file *file)
{
	struct pvr_fw_trace_buffer *trace_buffer = inode->i_private;
	struct rogue_fwif_tracebuf_space *tracebuf_space =
		trace_buffer->tracebuf_space;
	struct pvr_fw_trace_seq_data *trace_seq_data;
	int err;

	trace_seq_data = kzalloc(sizeof(*trace_seq_data), GFP_KERNEL);
	if (!trace_seq_data)
		return -ENOMEM;

	trace_seq_data->buffer = kcalloc(ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS,
					 sizeof(*trace_seq_data->buffer), GFP_KERNEL);
	if (!trace_seq_data->buffer) {
		err = -ENOMEM;
		goto err_free_data;
	}

	/*
	 * Take a local copy of the trace buffer, as firmware may still be
	 * writing to it. This will exist as long as this file is open.
	 */
	memcpy(trace_seq_data->buffer, trace_buffer->buf,
	       ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS * sizeof(u32));
	trace_seq_data->start_offset = READ_ONCE(tracebuf_space->trace_pointer);
	trace_seq_data->assert_buf = tracebuf_space->assert_buf;
	fw_trace_get_first(trace_seq_data);

	err = seq_open(file, &pvr_fw_trace_seq_ops);
	if (err)
		goto err_free_buffer;

	((struct seq_file *)file->private_data)->private = trace_seq_data;

	return 0;

err_free_buffer:
	kfree(trace_seq_data->buffer);

err_free_data:
	kfree(trace_seq_data);

	return err;
}

static int fw_trace_release(struct inode *inode, struct file *file)
{
	struct pvr_fw_trace_seq_data *trace_seq_data =
		((struct seq_file *)file->private_data)->private;

	seq_release(inode, file);
	kfree(trace_seq_data->buffer);
	kfree(trace_seq_data);

	return 0;
}

static const struct file_operations pvr_fw_trace_fops = {
	.owner = THIS_MODULE,
	.open = fw_trace_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = fw_trace_release,
};

void
pvr_fw_trace_mask_update(struct pvr_device *pvr_dev, u32 old_mask, u32 new_mask)
{
	if (old_mask != new_mask)
		update_logtype(pvr_dev, new_mask);
}

void
pvr_fw_trace_debugfs_init(struct pvr_device *pvr_dev, struct dentry *dir)
{
	struct pvr_fw_trace *fw_trace = &pvr_dev->fw_dev.fw_trace;
	u32 thread_nr;

	static_assert(ARRAY_SIZE(fw_trace->buffers) <= 10,
		      "The filename buffer is only large enough for a single-digit thread count");

	for (thread_nr = 0; thread_nr < ARRAY_SIZE(fw_trace->buffers); ++thread_nr) {
		char filename[8];

		snprintf(filename, ARRAY_SIZE(filename), "trace_%u", thread_nr);
		debugfs_create_file(filename, 0400, dir,
				    &fw_trace->buffers[thread_nr],
				    &pvr_fw_trace_fops);
	}
}
#endif