1 // SPDX-License-Identifier: GPL-2.0-only OR MIT
2 /* Copyright (c) 2023 Imagination Technologies Ltd. */
4 #include "pvr_device.h"
6 #include "pvr_rogue_fwif.h"
7 #include "pvr_rogue_fwif_sf.h"
8 #include "pvr_fw_trace.h"
10 #include <drm/drm_drv.h>
11 #include <drm/drm_file.h>
13 #include <linux/build_bug.h>
14 #include <linux/dcache.h>
15 #include <linux/sysfs.h>
16 #include <linux/types.h>
19 tracebuf_ctrl_init(void *cpu_ptr, void *priv)
21 struct rogue_fwif_tracebuf *tracebuf_ctrl = cpu_ptr;
22 struct pvr_fw_trace *fw_trace = priv;
25 tracebuf_ctrl->tracebuf_size_in_dwords = ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS;
26 tracebuf_ctrl->tracebuf_flags = 0;
28 if (fw_trace->group_mask)
29 tracebuf_ctrl->log_type = fw_trace->group_mask | ROGUE_FWIF_LOG_TYPE_TRACE;
31 tracebuf_ctrl->log_type = ROGUE_FWIF_LOG_TYPE_NONE;
33 for (thread_nr = 0; thread_nr < ARRAY_SIZE(fw_trace->buffers); thread_nr++) {
34 struct rogue_fwif_tracebuf_space *tracebuf_space =
35 &tracebuf_ctrl->tracebuf[thread_nr];
36 struct pvr_fw_trace_buffer *trace_buffer = &fw_trace->buffers[thread_nr];
38 pvr_fw_object_get_fw_addr(trace_buffer->buf_obj,
39 &tracebuf_space->trace_buffer_fw_addr);
41 tracebuf_space->trace_buffer = trace_buffer->buf;
42 tracebuf_space->trace_pointer = 0;
46 int pvr_fw_trace_init(struct pvr_device *pvr_dev)
48 struct pvr_fw_trace *fw_trace = &pvr_dev->fw_dev.fw_trace;
49 struct drm_device *drm_dev = from_pvr_device(pvr_dev);
53 for (thread_nr = 0; thread_nr < ARRAY_SIZE(fw_trace->buffers); thread_nr++) {
54 struct pvr_fw_trace_buffer *trace_buffer = &fw_trace->buffers[thread_nr];
57 pvr_fw_object_create_and_map(pvr_dev,
58 ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS *
59 sizeof(*trace_buffer->buf),
60 PVR_BO_FW_FLAGS_DEVICE_UNCACHED |
61 PVR_BO_FW_NO_CLEAR_ON_RESET,
62 NULL, NULL, &trace_buffer->buf_obj);
63 if (IS_ERR(trace_buffer->buf)) {
64 drm_err(drm_dev, "Unable to allocate trace buffer\n");
65 err = PTR_ERR(trace_buffer->buf);
66 trace_buffer->buf = NULL;
71 /* TODO: Provide control of group mask. */
72 fw_trace->group_mask = 0;
74 fw_trace->tracebuf_ctrl =
75 pvr_fw_object_create_and_map(pvr_dev,
76 sizeof(*fw_trace->tracebuf_ctrl),
77 PVR_BO_FW_FLAGS_DEVICE_UNCACHED |
78 PVR_BO_FW_NO_CLEAR_ON_RESET,
79 tracebuf_ctrl_init, fw_trace,
80 &fw_trace->tracebuf_ctrl_obj);
81 if (IS_ERR(fw_trace->tracebuf_ctrl)) {
82 drm_err(drm_dev, "Unable to allocate trace buffer control structure\n");
83 err = PTR_ERR(fw_trace->tracebuf_ctrl);
87 BUILD_BUG_ON(ARRAY_SIZE(fw_trace->tracebuf_ctrl->tracebuf) !=
88 ARRAY_SIZE(fw_trace->buffers));
90 for (thread_nr = 0; thread_nr < ARRAY_SIZE(fw_trace->buffers); thread_nr++) {
91 struct rogue_fwif_tracebuf_space *tracebuf_space =
92 &fw_trace->tracebuf_ctrl->tracebuf[thread_nr];
93 struct pvr_fw_trace_buffer *trace_buffer = &fw_trace->buffers[thread_nr];
95 trace_buffer->tracebuf_space = tracebuf_space;
101 for (thread_nr = 0; thread_nr < ARRAY_SIZE(fw_trace->buffers); thread_nr++) {
102 struct pvr_fw_trace_buffer *trace_buffer = &fw_trace->buffers[thread_nr];
104 if (trace_buffer->buf)
105 pvr_fw_object_unmap_and_destroy(trace_buffer->buf_obj);
111 void pvr_fw_trace_fini(struct pvr_device *pvr_dev)
113 struct pvr_fw_trace *fw_trace = &pvr_dev->fw_dev.fw_trace;
116 for (thread_nr = 0; thread_nr < ARRAY_SIZE(fw_trace->buffers); thread_nr++) {
117 struct pvr_fw_trace_buffer *trace_buffer = &fw_trace->buffers[thread_nr];
119 pvr_fw_object_unmap_and_destroy(trace_buffer->buf_obj);
121 pvr_fw_object_unmap_and_destroy(fw_trace->tracebuf_ctrl_obj);
124 #if defined(CONFIG_DEBUG_FS)
127 * update_logtype() - Send KCCB command to trigger FW to update logtype
128 * @pvr_dev: Target PowerVR device
129 * @group_mask: New log group mask.
133 * * Any error returned by pvr_kccb_send_cmd(), or
134 * * -%EIO if the device is lost.
137 update_logtype(struct pvr_device *pvr_dev, u32 group_mask)
139 struct pvr_fw_trace *fw_trace = &pvr_dev->fw_dev.fw_trace;
140 struct rogue_fwif_kccb_cmd cmd;
145 fw_trace->tracebuf_ctrl->log_type = ROGUE_FWIF_LOG_TYPE_TRACE | group_mask;
147 fw_trace->tracebuf_ctrl->log_type = ROGUE_FWIF_LOG_TYPE_NONE;
149 fw_trace->group_mask = group_mask;
151 down_read(&pvr_dev->reset_sem);
152 if (!drm_dev_enter(from_pvr_device(pvr_dev), &idx)) {
157 cmd.cmd_type = ROGUE_FWIF_KCCB_CMD_LOGTYPE_UPDATE;
160 err = pvr_kccb_send_cmd(pvr_dev, &cmd, NULL);
165 up_read(&pvr_dev->reset_sem);
170 struct pvr_fw_trace_seq_data {
171 /** @buffer: Pointer to copy of trace data. */
174 /** @start_offset: Starting offset in trace data, as reported by FW. */
177 /** @idx: Current index into trace data. */
180 /** @assert_buf: Trace assert buffer, as reported by FW. */
181 struct rogue_fwif_file_info_buf assert_buf;
184 static u32 find_sfid(u32 id)
188 for (i = 0; i < ARRAY_SIZE(stid_fmts); i++) {
189 if (stid_fmts[i].id == id)
193 return ROGUE_FW_SF_LAST;
196 static u32 read_fw_trace(struct pvr_fw_trace_seq_data *trace_seq_data, u32 offset)
200 idx = trace_seq_data->idx + offset;
201 if (idx >= ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS)
204 idx = (idx + trace_seq_data->start_offset) % ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS;
205 return trace_seq_data->buffer[idx];
209 * fw_trace_get_next() - Advance trace index to next entry
210 * @trace_seq_data: Trace sequence data.
213 * * %true if trace index is now pointing to a valid entry, or
214 * * %false if trace index is pointing to an invalid entry, or has hit the end
217 static bool fw_trace_get_next(struct pvr_fw_trace_seq_data *trace_seq_data)
221 while (trace_seq_data->idx < ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS) {
222 id = read_fw_trace(trace_seq_data, 0);
223 trace_seq_data->idx++;
224 if (!ROGUE_FW_LOG_VALIDID(id))
226 if (id == ROGUE_FW_SF_MAIN_ASSERT_FAILED) {
227 /* Assertion failure marks the end of the trace. */
231 sf_id = find_sfid(id);
232 if (sf_id == ROGUE_FW_SF_FIRST)
234 if (sf_id == ROGUE_FW_SF_LAST) {
236 * Could not match with an ID in the SF table, trace is
237 * most likely corrupt from this point.
242 /* Skip over the timestamp, and any parameters. */
243 trace_seq_data->idx += 2 + ROGUE_FW_SF_PARAMNUM(id);
245 /* Ensure index is now pointing to a valid trace entry. */
246 id = read_fw_trace(trace_seq_data, 0);
247 if (!ROGUE_FW_LOG_VALIDID(id))
253 /* Hit end of trace data. */
258 * fw_trace_get_first() - Find first valid entry in trace
259 * @trace_seq_data: Trace sequence data.
261 * Skips over invalid (usually zero) and ROGUE_FW_SF_FIRST entries.
263 * If the trace has no valid entries, this function will exit with the trace
264 * index pointing to the end of the trace. trace_seq_show() will return an error
267 static void fw_trace_get_first(struct pvr_fw_trace_seq_data *trace_seq_data)
269 trace_seq_data->idx = 0;
271 while (trace_seq_data->idx < ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS) {
272 u32 id = read_fw_trace(trace_seq_data, 0);
274 if (ROGUE_FW_LOG_VALIDID(id)) {
275 u32 sf_id = find_sfid(id);
277 if (sf_id != ROGUE_FW_SF_FIRST)
280 trace_seq_data->idx++;
284 static void *fw_trace_seq_start(struct seq_file *s, loff_t *pos)
286 struct pvr_fw_trace_seq_data *trace_seq_data = s->private;
289 /* Reset trace index, then advance to *pos. */
290 fw_trace_get_first(trace_seq_data);
292 for (i = 0; i < *pos; i++) {
293 if (!fw_trace_get_next(trace_seq_data))
297 return (trace_seq_data->idx < ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS) ? pos : NULL;
300 static void *fw_trace_seq_next(struct seq_file *s, void *v, loff_t *pos)
302 struct pvr_fw_trace_seq_data *trace_seq_data = s->private;
305 if (!fw_trace_get_next(trace_seq_data))
308 return (trace_seq_data->idx < ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS) ? pos : NULL;
311 static void fw_trace_seq_stop(struct seq_file *s, void *v)
315 static int fw_trace_seq_show(struct seq_file *s, void *v)
317 struct pvr_fw_trace_seq_data *trace_seq_data = s->private;
322 if (trace_seq_data->idx >= ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS)
325 id = read_fw_trace(trace_seq_data, 0);
326 /* Index is not pointing at a valid entry. */
327 if (!ROGUE_FW_LOG_VALIDID(id))
330 sf_id = find_sfid(id);
331 /* Index is not pointing at a valid entry. */
332 if (sf_id == ROGUE_FW_SF_LAST)
335 timestamp = read_fw_trace(trace_seq_data, 1) |
336 ((u64)read_fw_trace(trace_seq_data, 2) << 32);
337 timestamp = (timestamp & ~ROGUE_FWT_TIMESTAMP_TIME_CLRMSK) >>
338 ROGUE_FWT_TIMESTAMP_TIME_SHIFT;
340 seq_printf(s, "[%llu] : ", timestamp);
341 if (id == ROGUE_FW_SF_MAIN_ASSERT_FAILED) {
342 seq_printf(s, "ASSERTION %s failed at %s:%u",
343 trace_seq_data->assert_buf.info,
344 trace_seq_data->assert_buf.path,
345 trace_seq_data->assert_buf.line_num);
347 seq_printf(s, stid_fmts[sf_id].name,
348 read_fw_trace(trace_seq_data, 3),
349 read_fw_trace(trace_seq_data, 4),
350 read_fw_trace(trace_seq_data, 5),
351 read_fw_trace(trace_seq_data, 6),
352 read_fw_trace(trace_seq_data, 7),
353 read_fw_trace(trace_seq_data, 8),
354 read_fw_trace(trace_seq_data, 9),
355 read_fw_trace(trace_seq_data, 10),
356 read_fw_trace(trace_seq_data, 11),
357 read_fw_trace(trace_seq_data, 12),
358 read_fw_trace(trace_seq_data, 13),
359 read_fw_trace(trace_seq_data, 14),
360 read_fw_trace(trace_seq_data, 15),
361 read_fw_trace(trace_seq_data, 16),
362 read_fw_trace(trace_seq_data, 17),
363 read_fw_trace(trace_seq_data, 18),
364 read_fw_trace(trace_seq_data, 19),
365 read_fw_trace(trace_seq_data, 20),
366 read_fw_trace(trace_seq_data, 21),
367 read_fw_trace(trace_seq_data, 22));
373 static const struct seq_operations pvr_fw_trace_seq_ops = {
374 .start = fw_trace_seq_start,
375 .next = fw_trace_seq_next,
376 .stop = fw_trace_seq_stop,
377 .show = fw_trace_seq_show
380 static int fw_trace_open(struct inode *inode, struct file *file)
382 struct pvr_fw_trace_buffer *trace_buffer = inode->i_private;
383 struct rogue_fwif_tracebuf_space *tracebuf_space =
384 trace_buffer->tracebuf_space;
385 struct pvr_fw_trace_seq_data *trace_seq_data;
388 trace_seq_data = kzalloc(sizeof(*trace_seq_data), GFP_KERNEL);
392 trace_seq_data->buffer = kcalloc(ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS,
393 sizeof(*trace_seq_data->buffer), GFP_KERNEL);
394 if (!trace_seq_data->buffer) {
400 * Take a local copy of the trace buffer, as firmware may still be
401 * writing to it. This will exist as long as this file is open.
403 memcpy(trace_seq_data->buffer, trace_buffer->buf,
404 ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS * sizeof(u32));
405 trace_seq_data->start_offset = READ_ONCE(tracebuf_space->trace_pointer);
406 trace_seq_data->assert_buf = tracebuf_space->assert_buf;
407 fw_trace_get_first(trace_seq_data);
409 err = seq_open(file, &pvr_fw_trace_seq_ops);
411 goto err_free_buffer;
413 ((struct seq_file *)file->private_data)->private = trace_seq_data;
418 kfree(trace_seq_data->buffer);
421 kfree(trace_seq_data);
426 static int fw_trace_release(struct inode *inode, struct file *file)
428 struct pvr_fw_trace_seq_data *trace_seq_data =
429 ((struct seq_file *)file->private_data)->private;
431 seq_release(inode, file);
432 kfree(trace_seq_data->buffer);
433 kfree(trace_seq_data);
438 static const struct file_operations pvr_fw_trace_fops = {
439 .owner = THIS_MODULE,
440 .open = fw_trace_open,
443 .release = fw_trace_release,
447 pvr_fw_trace_mask_update(struct pvr_device *pvr_dev, u32 old_mask, u32 new_mask)
449 if (old_mask != new_mask)
450 update_logtype(pvr_dev, new_mask);
454 pvr_fw_trace_debugfs_init(struct pvr_device *pvr_dev, struct dentry *dir)
456 struct pvr_fw_trace *fw_trace = &pvr_dev->fw_dev.fw_trace;
459 static_assert(ARRAY_SIZE(fw_trace->buffers) <= 10,
460 "The filename buffer is only large enough for a single-digit thread count");
462 for (thread_nr = 0; thread_nr < ARRAY_SIZE(fw_trace->buffers); ++thread_nr) {
465 snprintf(filename, ARRAY_SIZE(filename), "trace_%u", thread_nr);
466 debugfs_create_file(filename, 0400, dir,
467 &fw_trace->buffers[thread_nr],