xref: /linux/drivers/gpu/drm/imagination/pvr_fw_trace.c (revision 109f4e9010c37966d59780f7c76064581064b7f2)
1 // SPDX-License-Identifier: GPL-2.0-only OR MIT
2 /* Copyright (c) 2023 Imagination Technologies Ltd. */
3 
4 #include "pvr_device.h"
5 #include "pvr_gem.h"
6 #include "pvr_rogue_fwif.h"
7 #include "pvr_rogue_fwif_sf.h"
8 #include "pvr_fw_trace.h"
9 
10 #include <drm/drm_drv.h>
11 #include <drm/drm_file.h>
12 #include <drm/drm_print.h>
13 
14 #include <linux/build_bug.h>
15 #include <linux/compiler_attributes.h>
16 #include <linux/dcache.h>
17 #include <linux/debugfs.h>
18 #include <linux/moduleparam.h>
19 #include <linux/sysfs.h>
20 #include <linux/types.h>
21 
22 static int
23 validate_group_mask(struct pvr_device *pvr_dev, const u32 group_mask)
24 {
25 	if (group_mask & ~ROGUE_FWIF_LOG_TYPE_GROUP_MASK) {
26 		drm_warn(from_pvr_device(pvr_dev),
27 			 "Invalid fw_trace group mask 0x%08x (must be a subset of 0x%08x)",
28 			 group_mask, ROGUE_FWIF_LOG_TYPE_GROUP_MASK);
29 		return -EINVAL;
30 	}
31 
32 	return 0;
33 }
34 
35 static inline u32
36 build_log_type(const u32 group_mask)
37 {
38 	if (!group_mask)
39 		return ROGUE_FWIF_LOG_TYPE_NONE;
40 
41 	return group_mask | ROGUE_FWIF_LOG_TYPE_TRACE;
42 }
43 
44 /*
45  * Don't gate this behind CONFIG_DEBUG_FS so that it can be used as an initial
46  * value without further conditional code...
47  */
48 static u32 pvr_fw_trace_init_mask;
49 
50 /*
51  * ...but do only expose the module parameter if debugfs is enabled, since
52  * there's no reason to turn on fw_trace without it.
53  */
54 #if IS_ENABLED(CONFIG_DEBUG_FS)
55 static int
56 pvr_fw_trace_init_mask_set(const char *val, const struct kernel_param *kp)
57 {
58 	u32 mask = 0;
59 	int err;
60 
61 	err = kstrtouint(val, 0, &mask);
62 	if (err)
63 		return err;
64 
65 	err = validate_group_mask(NULL, mask);
66 	if (err)
67 		return err;
68 
69 	*(unsigned int *)kp->arg = mask;
70 
71 	return 0;
72 }
73 
74 static const struct kernel_param_ops pvr_fw_trace_init_mask_ops = {
75 	.set = pvr_fw_trace_init_mask_set,
76 	.get = param_get_hexint,
77 };
78 
79 param_check_hexint(init_fw_trace_mask, &pvr_fw_trace_init_mask);
80 module_param_cb(init_fw_trace_mask, &pvr_fw_trace_init_mask_ops, &pvr_fw_trace_init_mask, 0400);
81 __MODULE_PARM_TYPE(init_fw_trace_mask, "hexint");
82 MODULE_PARM_DESC(init_fw_trace_mask,
83 		 "Enable FW trace for the specified groups at device init time");
84 #endif
85 
86 static void
87 tracebuf_ctrl_init(void *cpu_ptr, void *priv)
88 {
89 	struct rogue_fwif_tracebuf *tracebuf_ctrl = cpu_ptr;
90 	struct pvr_fw_trace *fw_trace = priv;
91 
92 	tracebuf_ctrl->tracebuf_size_in_dwords = ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS;
93 	tracebuf_ctrl->tracebuf_flags = 0;
94 	tracebuf_ctrl->log_type = build_log_type(fw_trace->group_mask);
95 
96 	for (u32 thread_nr = 0; thread_nr < ARRAY_SIZE(fw_trace->buffers); thread_nr++) {
97 		struct rogue_fwif_tracebuf_space *tracebuf_space =
98 			&tracebuf_ctrl->tracebuf[thread_nr];
99 		struct pvr_fw_trace_buffer *trace_buffer = &fw_trace->buffers[thread_nr];
100 
101 		pvr_fw_object_get_fw_addr(trace_buffer->buf_obj,
102 					  &tracebuf_space->trace_buffer_fw_addr);
103 
104 		tracebuf_space->trace_pointer = 0;
105 	}
106 }
107 
108 int pvr_fw_trace_init(struct pvr_device *pvr_dev)
109 {
110 	struct pvr_fw_trace *fw_trace = &pvr_dev->fw_dev.fw_trace;
111 	struct drm_device *drm_dev = from_pvr_device(pvr_dev);
112 	int err;
113 
114 	for (u32 thread_nr = 0; thread_nr < ARRAY_SIZE(fw_trace->buffers); thread_nr++) {
115 		struct pvr_fw_trace_buffer *trace_buffer = &fw_trace->buffers[thread_nr];
116 
117 		trace_buffer->buf =
118 			pvr_fw_object_create_and_map(pvr_dev,
119 						     ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS *
120 						     sizeof(*trace_buffer->buf),
121 						     PVR_BO_FW_FLAGS_DEVICE_UNCACHED |
122 						     PVR_BO_FW_NO_CLEAR_ON_RESET,
123 						     NULL, NULL, &trace_buffer->buf_obj);
124 		if (IS_ERR(trace_buffer->buf)) {
125 			drm_err(drm_dev, "Unable to allocate trace buffer\n");
126 			err = PTR_ERR(trace_buffer->buf);
127 			trace_buffer->buf = NULL;
128 			goto err_free_buf;
129 		}
130 	}
131 
132 	/*
133 	 * Load the initial group_mask from the init_fw_trace_mask module
134 	 * parameter. This allows early tracing before the user can write to
135 	 * debugfs. Unlike update_logtype(), we don't set log_type here as that
136 	 * is initialised by tracebuf_ctrl_init().
137 	 */
138 	fw_trace->group_mask = pvr_fw_trace_init_mask;
139 
140 	fw_trace->tracebuf_ctrl =
141 		pvr_fw_object_create_and_map(pvr_dev,
142 					     sizeof(*fw_trace->tracebuf_ctrl),
143 					     PVR_BO_FW_FLAGS_DEVICE_UNCACHED |
144 					     PVR_BO_FW_NO_CLEAR_ON_RESET,
145 					     tracebuf_ctrl_init, fw_trace,
146 					     &fw_trace->tracebuf_ctrl_obj);
147 	if (IS_ERR(fw_trace->tracebuf_ctrl)) {
148 		drm_err(drm_dev, "Unable to allocate trace buffer control structure\n");
149 		err = PTR_ERR(fw_trace->tracebuf_ctrl);
150 		goto err_free_buf;
151 	}
152 
153 	BUILD_BUG_ON(ARRAY_SIZE(fw_trace->tracebuf_ctrl->tracebuf) !=
154 		     ARRAY_SIZE(fw_trace->buffers));
155 
156 	for (u32 thread_nr = 0; thread_nr < ARRAY_SIZE(fw_trace->buffers); thread_nr++) {
157 		struct rogue_fwif_tracebuf_space *tracebuf_space =
158 			&fw_trace->tracebuf_ctrl->tracebuf[thread_nr];
159 		struct pvr_fw_trace_buffer *trace_buffer = &fw_trace->buffers[thread_nr];
160 
161 		trace_buffer->tracebuf_space = tracebuf_space;
162 	}
163 
164 	return 0;
165 
166 err_free_buf:
167 	for (u32 thread_nr = 0; thread_nr < ARRAY_SIZE(fw_trace->buffers); thread_nr++) {
168 		struct pvr_fw_trace_buffer *trace_buffer = &fw_trace->buffers[thread_nr];
169 
170 		if (trace_buffer->buf)
171 			pvr_fw_object_unmap_and_destroy(trace_buffer->buf_obj);
172 	}
173 
174 	return err;
175 }
176 
177 void pvr_fw_trace_fini(struct pvr_device *pvr_dev)
178 {
179 	struct pvr_fw_trace *fw_trace = &pvr_dev->fw_dev.fw_trace;
180 
181 	for (u32 thread_nr = 0; thread_nr < ARRAY_SIZE(fw_trace->buffers); thread_nr++) {
182 		struct pvr_fw_trace_buffer *trace_buffer = &fw_trace->buffers[thread_nr];
183 
184 		pvr_fw_object_unmap_and_destroy(trace_buffer->buf_obj);
185 	}
186 	pvr_fw_object_unmap_and_destroy(fw_trace->tracebuf_ctrl_obj);
187 }
188 
189 /**
190  * update_logtype() - Send KCCB command to trigger FW to update logtype
191  * @pvr_dev: Target PowerVR device
192  * @group_mask: New log group mask; must pass validate_group_mask().
193  *
194  * Returns:
195  *  * 0 if the provided @group_mask is the same as the current value (this is a
196  *    short-circuit evaluation),
197  *  * 0 on success,
198  *  * Any error returned by pvr_kccb_send_cmd(), or
199  *  * -%EIO if the device is lost.
200  */
201 static int
202 update_logtype(struct pvr_device *pvr_dev, u32 group_mask)
203 {
204 	struct pvr_fw_trace *fw_trace = &pvr_dev->fw_dev.fw_trace;
205 	struct drm_device *drm_dev = from_pvr_device(pvr_dev);
206 	struct rogue_fwif_kccb_cmd cmd;
207 	int idx;
208 	int err;
209 	int slot;
210 
211 	/* No change in group_mask => nothing to update. */
212 	if (fw_trace->group_mask == group_mask)
213 		return 0;
214 
215 	fw_trace->group_mask = group_mask;
216 	fw_trace->tracebuf_ctrl->log_type = build_log_type(group_mask);
217 
218 	down_read(&pvr_dev->reset_sem);
219 	if (!drm_dev_enter(drm_dev, &idx)) {
220 		err = -EIO;
221 		goto err_up_read;
222 	}
223 
224 	cmd.cmd_type = ROGUE_FWIF_KCCB_CMD_LOGTYPE_UPDATE;
225 	cmd.kccb_flags = 0;
226 
227 	err = pvr_kccb_send_cmd(pvr_dev, &cmd, &slot);
228 	if (err)
229 		goto err_drm_dev_exit;
230 
231 	err = pvr_kccb_wait_for_completion(pvr_dev, slot, HZ, NULL);
232 
233 err_drm_dev_exit:
234 	drm_dev_exit(idx);
235 
236 err_up_read:
237 	up_read(&pvr_dev->reset_sem);
238 
239 	return err;
240 }
241 
242 struct pvr_fw_trace_seq_data {
243 	/** @buffer: Pointer to copy of trace data. */
244 	u32 *buffer;
245 
246 	/** @start_offset: Starting offset in trace data, as reported by FW. */
247 	u32 start_offset;
248 
249 	/** @idx: Current index into trace data. */
250 	u32 idx;
251 
252 	/** @assert_buf: Trace assert buffer, as reported by FW. */
253 	struct rogue_fwif_file_info_buf assert_buf;
254 };
255 
256 static u32 find_sfid(u32 id)
257 {
258 	for (u32 i = 0; i < ARRAY_SIZE(stid_fmts); i++) {
259 		if (stid_fmts[i].id == id)
260 			return i;
261 	}
262 
263 	return ROGUE_FW_SF_LAST;
264 }
265 
266 static u32 read_fw_trace(struct pvr_fw_trace_seq_data *trace_seq_data, u32 offset)
267 {
268 	u32 idx;
269 
270 	idx = trace_seq_data->idx + offset;
271 	if (idx >= ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS)
272 		return 0;
273 
274 	idx = (idx + trace_seq_data->start_offset) % ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS;
275 	return trace_seq_data->buffer[idx];
276 }
277 
278 /**
279  * fw_trace_get_next() - Advance trace index to next entry
280  * @trace_seq_data: Trace sequence data.
281  *
282  * Returns:
283  *  * %true if trace index is now pointing to a valid entry, or
284  *  * %false if trace index is pointing to an invalid entry, or has hit the end
285  *    of the trace.
286  */
287 static bool fw_trace_get_next(struct pvr_fw_trace_seq_data *trace_seq_data)
288 {
289 	u32 id, sf_id;
290 
291 	while (trace_seq_data->idx < ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS) {
292 		id = read_fw_trace(trace_seq_data, 0);
293 		trace_seq_data->idx++;
294 		if (!ROGUE_FW_LOG_VALIDID(id))
295 			continue;
296 		if (id == ROGUE_FW_SF_MAIN_ASSERT_FAILED) {
297 			/* Assertion failure marks the end of the trace. */
298 			return false;
299 		}
300 
301 		sf_id = find_sfid(id);
302 		if (sf_id == ROGUE_FW_SF_FIRST)
303 			continue;
304 		if (sf_id == ROGUE_FW_SF_LAST) {
305 			/*
306 			 * Could not match with an ID in the SF table, trace is
307 			 * most likely corrupt from this point.
308 			 */
309 			return false;
310 		}
311 
312 		/* Skip over the timestamp, and any parameters. */
313 		trace_seq_data->idx += 2 + ROGUE_FW_SF_PARAMNUM(id);
314 
315 		/* Ensure index is now pointing to a valid trace entry. */
316 		id = read_fw_trace(trace_seq_data, 0);
317 		if (!ROGUE_FW_LOG_VALIDID(id))
318 			continue;
319 
320 		return true;
321 	}
322 
323 	/* Hit end of trace data. */
324 	return false;
325 }
326 
327 /**
328  * fw_trace_get_first() - Find first valid entry in trace
329  * @trace_seq_data: Trace sequence data.
330  *
331  * Skips over invalid (usually zero) and ROGUE_FW_SF_FIRST entries.
332  *
333  * If the trace has no valid entries, this function will exit with the trace
334  * index pointing to the end of the trace. trace_seq_show() will return an error
335  * in this state.
336  */
337 static void fw_trace_get_first(struct pvr_fw_trace_seq_data *trace_seq_data)
338 {
339 	trace_seq_data->idx = 0;
340 
341 	while (trace_seq_data->idx < ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS) {
342 		u32 id = read_fw_trace(trace_seq_data, 0);
343 
344 		if (ROGUE_FW_LOG_VALIDID(id)) {
345 			u32 sf_id = find_sfid(id);
346 
347 			if (sf_id != ROGUE_FW_SF_FIRST)
348 				break;
349 		}
350 		trace_seq_data->idx++;
351 	}
352 }
353 
354 static void *fw_trace_seq_start(struct seq_file *s, loff_t *pos)
355 {
356 	struct pvr_fw_trace_seq_data *trace_seq_data = s->private;
357 
358 	/* Reset trace index, then advance to *pos. */
359 	fw_trace_get_first(trace_seq_data);
360 
361 	for (u32 i = 0; i < *pos; i++) {
362 		if (!fw_trace_get_next(trace_seq_data))
363 			return NULL;
364 	}
365 
366 	return (trace_seq_data->idx < ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS) ? pos : NULL;
367 }
368 
369 static void *fw_trace_seq_next(struct seq_file *s, void *v, loff_t *pos)
370 {
371 	struct pvr_fw_trace_seq_data *trace_seq_data = s->private;
372 
373 	(*pos)++;
374 	if (!fw_trace_get_next(trace_seq_data))
375 		return NULL;
376 
377 	return (trace_seq_data->idx < ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS) ? pos : NULL;
378 }
379 
380 static void fw_trace_seq_stop(struct seq_file *s, void *v)
381 {
382 }
383 
384 static int fw_trace_seq_show(struct seq_file *s, void *v)
385 {
386 	struct pvr_fw_trace_seq_data *trace_seq_data = s->private;
387 	u64 timestamp;
388 	u32 id;
389 	u32 sf_id;
390 
391 	if (trace_seq_data->idx >= ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS)
392 		return -EINVAL;
393 
394 	id = read_fw_trace(trace_seq_data, 0);
395 	/* Index is not pointing at a valid entry. */
396 	if (!ROGUE_FW_LOG_VALIDID(id))
397 		return -EINVAL;
398 
399 	sf_id = find_sfid(id);
400 	/* Index is not pointing at a valid entry. */
401 	if (sf_id == ROGUE_FW_SF_LAST)
402 		return -EINVAL;
403 
404 	timestamp = ((u64)read_fw_trace(trace_seq_data, 1) << 32) |
405 		read_fw_trace(trace_seq_data, 2);
406 	timestamp = (timestamp & ~ROGUE_FWT_TIMESTAMP_TIME_CLRMSK) >>
407 		ROGUE_FWT_TIMESTAMP_TIME_SHIFT;
408 
409 	seq_printf(s, "[%llu] : ", timestamp);
410 	if (id == ROGUE_FW_SF_MAIN_ASSERT_FAILED) {
411 		seq_printf(s, "ASSERTION %s failed at %s:%u",
412 			   trace_seq_data->assert_buf.info,
413 			   trace_seq_data->assert_buf.path,
414 			   trace_seq_data->assert_buf.line_num);
415 	} else {
416 		seq_printf(s, stid_fmts[sf_id].name,
417 			   read_fw_trace(trace_seq_data, 3),
418 			   read_fw_trace(trace_seq_data, 4),
419 			   read_fw_trace(trace_seq_data, 5),
420 			   read_fw_trace(trace_seq_data, 6),
421 			   read_fw_trace(trace_seq_data, 7),
422 			   read_fw_trace(trace_seq_data, 8),
423 			   read_fw_trace(trace_seq_data, 9),
424 			   read_fw_trace(trace_seq_data, 10),
425 			   read_fw_trace(trace_seq_data, 11),
426 			   read_fw_trace(trace_seq_data, 12),
427 			   read_fw_trace(trace_seq_data, 13),
428 			   read_fw_trace(trace_seq_data, 14),
429 			   read_fw_trace(trace_seq_data, 15),
430 			   read_fw_trace(trace_seq_data, 16),
431 			   read_fw_trace(trace_seq_data, 17),
432 			   read_fw_trace(trace_seq_data, 18),
433 			   read_fw_trace(trace_seq_data, 19),
434 			   read_fw_trace(trace_seq_data, 20),
435 			   read_fw_trace(trace_seq_data, 21),
436 			   read_fw_trace(trace_seq_data, 22));
437 	}
438 	seq_puts(s, "\n");
439 	return 0;
440 }
441 
442 static const struct seq_operations pvr_fw_trace_seq_ops = {
443 	.start = fw_trace_seq_start,
444 	.next = fw_trace_seq_next,
445 	.stop = fw_trace_seq_stop,
446 	.show = fw_trace_seq_show
447 };
448 
449 static int fw_trace_open(struct inode *inode, struct file *file)
450 {
451 	struct pvr_fw_trace_buffer *trace_buffer = inode->i_private;
452 	struct rogue_fwif_tracebuf_space *tracebuf_space =
453 		trace_buffer->tracebuf_space;
454 	struct pvr_fw_trace_seq_data *trace_seq_data;
455 	int err;
456 
457 	trace_seq_data = kzalloc_obj(*trace_seq_data);
458 	if (!trace_seq_data)
459 		return -ENOMEM;
460 
461 	trace_seq_data->buffer = kcalloc(ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS,
462 					 sizeof(*trace_seq_data->buffer), GFP_KERNEL);
463 	if (!trace_seq_data->buffer) {
464 		err = -ENOMEM;
465 		goto err_free_data;
466 	}
467 
468 	/*
469 	 * Take a local copy of the trace buffer, as firmware may still be
470 	 * writing to it. This will exist as long as this file is open.
471 	 */
472 	memcpy(trace_seq_data->buffer, trace_buffer->buf,
473 	       ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS * sizeof(u32));
474 	trace_seq_data->start_offset = READ_ONCE(tracebuf_space->trace_pointer);
475 	trace_seq_data->assert_buf = tracebuf_space->assert_buf;
476 	fw_trace_get_first(trace_seq_data);
477 
478 	err = seq_open(file, &pvr_fw_trace_seq_ops);
479 	if (err)
480 		goto err_free_buffer;
481 
482 	((struct seq_file *)file->private_data)->private = trace_seq_data;
483 
484 	return 0;
485 
486 err_free_buffer:
487 	kfree(trace_seq_data->buffer);
488 
489 err_free_data:
490 	kfree(trace_seq_data);
491 
492 	return err;
493 }
494 
495 static int fw_trace_release(struct inode *inode, struct file *file)
496 {
497 	struct pvr_fw_trace_seq_data *trace_seq_data =
498 		((struct seq_file *)file->private_data)->private;
499 
500 	seq_release(inode, file);
501 	kfree(trace_seq_data->buffer);
502 	kfree(trace_seq_data);
503 
504 	return 0;
505 }
506 
507 static const struct file_operations pvr_fw_trace_fops = {
508 	.owner = THIS_MODULE,
509 	.open = fw_trace_open,
510 	.read = seq_read,
511 	.llseek = seq_lseek,
512 	.release = fw_trace_release,
513 };
514 
515 static int pvr_fw_trace_mask_get(void *data, u64 *value)
516 {
517 	struct pvr_device *pvr_dev = data;
518 
519 	*value = pvr_dev->fw_dev.fw_trace.group_mask;
520 
521 	return 0;
522 }
523 
524 static int pvr_fw_trace_mask_set(void *data, u64 value)
525 {
526 	struct pvr_device *pvr_dev = data;
527 	const u32 group_mask = (u32)value;
528 	int err;
529 
530 	err = validate_group_mask(pvr_dev, group_mask);
531 	if (err)
532 		return err;
533 
534 	return update_logtype(pvr_dev, group_mask);
535 }
536 
537 DEFINE_DEBUGFS_ATTRIBUTE(pvr_fw_trace_mask_fops, pvr_fw_trace_mask_get,
538 			 pvr_fw_trace_mask_set, "0x%08llx\n");
539 
540 void
541 pvr_fw_trace_debugfs_init(struct pvr_device *pvr_dev, struct dentry *dir)
542 {
543 	struct pvr_fw_trace *fw_trace = &pvr_dev->fw_dev.fw_trace;
544 
545 	if (!IS_ENABLED(CONFIG_DEBUG_FS))
546 		return;
547 
548 	static_assert(ARRAY_SIZE(fw_trace->buffers) <= 10,
549 		      "The filename buffer is only large enough for a single-digit thread count");
550 
551 	for (u32 thread_nr = 0; thread_nr < ARRAY_SIZE(fw_trace->buffers); ++thread_nr) {
552 		char filename[8];
553 
554 		snprintf(filename, ARRAY_SIZE(filename), "trace_%u", thread_nr);
555 		debugfs_create_file(filename, 0400, dir,
556 				    &fw_trace->buffers[thread_nr],
557 				    &pvr_fw_trace_fops);
558 	}
559 
560 	debugfs_create_file("trace_mask", 0600, dir, pvr_dev,
561 			    &pvr_fw_trace_mask_fops);
562 }
563