Loading...
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
13#include <linux/build_bug.h>
14#include <linux/dcache.h>
15#include <linux/debugfs.h>
16#include <linux/sysfs.h>
17#include <linux/types.h>
18
19static void
20tracebuf_ctrl_init(void *cpu_ptr, void *priv)
21{
22 struct rogue_fwif_tracebuf *tracebuf_ctrl = cpu_ptr;
23 struct pvr_fw_trace *fw_trace = priv;
24 u32 thread_nr;
25
26 tracebuf_ctrl->tracebuf_size_in_dwords = ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS;
27 tracebuf_ctrl->tracebuf_flags = 0;
28
29 if (fw_trace->group_mask)
30 tracebuf_ctrl->log_type = fw_trace->group_mask | ROGUE_FWIF_LOG_TYPE_TRACE;
31 else
32 tracebuf_ctrl->log_type = ROGUE_FWIF_LOG_TYPE_NONE;
33
34 for (thread_nr = 0; thread_nr < ARRAY_SIZE(fw_trace->buffers); thread_nr++) {
35 struct rogue_fwif_tracebuf_space *tracebuf_space =
36 &tracebuf_ctrl->tracebuf[thread_nr];
37 struct pvr_fw_trace_buffer *trace_buffer = &fw_trace->buffers[thread_nr];
38
39 pvr_fw_object_get_fw_addr(trace_buffer->buf_obj,
40 &tracebuf_space->trace_buffer_fw_addr);
41
42 tracebuf_space->trace_buffer = trace_buffer->buf;
43 tracebuf_space->trace_pointer = 0;
44 }
45}
46
47int pvr_fw_trace_init(struct pvr_device *pvr_dev)
48{
49 struct pvr_fw_trace *fw_trace = &pvr_dev->fw_dev.fw_trace;
50 struct drm_device *drm_dev = from_pvr_device(pvr_dev);
51 u32 thread_nr;
52 int err;
53
54 for (thread_nr = 0; thread_nr < ARRAY_SIZE(fw_trace->buffers); thread_nr++) {
55 struct pvr_fw_trace_buffer *trace_buffer = &fw_trace->buffers[thread_nr];
56
57 trace_buffer->buf =
58 pvr_fw_object_create_and_map(pvr_dev,
59 ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS *
60 sizeof(*trace_buffer->buf),
61 PVR_BO_FW_FLAGS_DEVICE_UNCACHED |
62 PVR_BO_FW_NO_CLEAR_ON_RESET,
63 NULL, NULL, &trace_buffer->buf_obj);
64 if (IS_ERR(trace_buffer->buf)) {
65 drm_err(drm_dev, "Unable to allocate trace buffer\n");
66 err = PTR_ERR(trace_buffer->buf);
67 trace_buffer->buf = NULL;
68 goto err_free_buf;
69 }
70 }
71
72 /* TODO: Provide control of group mask. */
73 fw_trace->group_mask = 0;
74
75 fw_trace->tracebuf_ctrl =
76 pvr_fw_object_create_and_map(pvr_dev,
77 sizeof(*fw_trace->tracebuf_ctrl),
78 PVR_BO_FW_FLAGS_DEVICE_UNCACHED |
79 PVR_BO_FW_NO_CLEAR_ON_RESET,
80 tracebuf_ctrl_init, fw_trace,
81 &fw_trace->tracebuf_ctrl_obj);
82 if (IS_ERR(fw_trace->tracebuf_ctrl)) {
83 drm_err(drm_dev, "Unable to allocate trace buffer control structure\n");
84 err = PTR_ERR(fw_trace->tracebuf_ctrl);
85 goto err_free_buf;
86 }
87
88 BUILD_BUG_ON(ARRAY_SIZE(fw_trace->tracebuf_ctrl->tracebuf) !=
89 ARRAY_SIZE(fw_trace->buffers));
90
91 for (thread_nr = 0; thread_nr < ARRAY_SIZE(fw_trace->buffers); thread_nr++) {
92 struct rogue_fwif_tracebuf_space *tracebuf_space =
93 &fw_trace->tracebuf_ctrl->tracebuf[thread_nr];
94 struct pvr_fw_trace_buffer *trace_buffer = &fw_trace->buffers[thread_nr];
95
96 trace_buffer->tracebuf_space = tracebuf_space;
97 }
98
99 return 0;
100
101err_free_buf:
102 for (thread_nr = 0; thread_nr < ARRAY_SIZE(fw_trace->buffers); thread_nr++) {
103 struct pvr_fw_trace_buffer *trace_buffer = &fw_trace->buffers[thread_nr];
104
105 if (trace_buffer->buf)
106 pvr_fw_object_unmap_and_destroy(trace_buffer->buf_obj);
107 }
108
109 return err;
110}
111
112void pvr_fw_trace_fini(struct pvr_device *pvr_dev)
113{
114 struct pvr_fw_trace *fw_trace = &pvr_dev->fw_dev.fw_trace;
115 u32 thread_nr;
116
117 for (thread_nr = 0; thread_nr < ARRAY_SIZE(fw_trace->buffers); thread_nr++) {
118 struct pvr_fw_trace_buffer *trace_buffer = &fw_trace->buffers[thread_nr];
119
120 pvr_fw_object_unmap_and_destroy(trace_buffer->buf_obj);
121 }
122 pvr_fw_object_unmap_and_destroy(fw_trace->tracebuf_ctrl_obj);
123}
124
125#if defined(CONFIG_DEBUG_FS)
126
127/**
128 * update_logtype() - Send KCCB command to trigger FW to update logtype
129 * @pvr_dev: Target PowerVR device
130 * @group_mask: New log group mask.
131 *
132 * Returns:
133 * * 0 on success,
134 * * Any error returned by pvr_kccb_send_cmd(), or
135 * * -%EIO if the device is lost.
136 */
137static int
138update_logtype(struct pvr_device *pvr_dev, u32 group_mask)
139{
140 struct pvr_fw_trace *fw_trace = &pvr_dev->fw_dev.fw_trace;
141 struct rogue_fwif_kccb_cmd cmd;
142 int idx;
143 int err;
144
145 if (group_mask)
146 fw_trace->tracebuf_ctrl->log_type = ROGUE_FWIF_LOG_TYPE_TRACE | group_mask;
147 else
148 fw_trace->tracebuf_ctrl->log_type = ROGUE_FWIF_LOG_TYPE_NONE;
149
150 fw_trace->group_mask = group_mask;
151
152 down_read(&pvr_dev->reset_sem);
153 if (!drm_dev_enter(from_pvr_device(pvr_dev), &idx)) {
154 err = -EIO;
155 goto err_up_read;
156 }
157
158 cmd.cmd_type = ROGUE_FWIF_KCCB_CMD_LOGTYPE_UPDATE;
159 cmd.kccb_flags = 0;
160
161 err = pvr_kccb_send_cmd(pvr_dev, &cmd, NULL);
162
163 drm_dev_exit(idx);
164
165err_up_read:
166 up_read(&pvr_dev->reset_sem);
167
168 return err;
169}
170
171struct pvr_fw_trace_seq_data {
172 /** @buffer: Pointer to copy of trace data. */
173 u32 *buffer;
174
175 /** @start_offset: Starting offset in trace data, as reported by FW. */
176 u32 start_offset;
177
178 /** @idx: Current index into trace data. */
179 u32 idx;
180
181 /** @assert_buf: Trace assert buffer, as reported by FW. */
182 struct rogue_fwif_file_info_buf assert_buf;
183};
184
185static u32 find_sfid(u32 id)
186{
187 u32 i;
188
189 for (i = 0; i < ARRAY_SIZE(stid_fmts); i++) {
190 if (stid_fmts[i].id == id)
191 return i;
192 }
193
194 return ROGUE_FW_SF_LAST;
195}
196
197static u32 read_fw_trace(struct pvr_fw_trace_seq_data *trace_seq_data, u32 offset)
198{
199 u32 idx;
200
201 idx = trace_seq_data->idx + offset;
202 if (idx >= ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS)
203 return 0;
204
205 idx = (idx + trace_seq_data->start_offset) % ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS;
206 return trace_seq_data->buffer[idx];
207}
208
209/**
210 * fw_trace_get_next() - Advance trace index to next entry
211 * @trace_seq_data: Trace sequence data.
212 *
213 * Returns:
214 * * %true if trace index is now pointing to a valid entry, or
215 * * %false if trace index is pointing to an invalid entry, or has hit the end
216 * of the trace.
217 */
218static bool fw_trace_get_next(struct pvr_fw_trace_seq_data *trace_seq_data)
219{
220 u32 id, sf_id;
221
222 while (trace_seq_data->idx < ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS) {
223 id = read_fw_trace(trace_seq_data, 0);
224 trace_seq_data->idx++;
225 if (!ROGUE_FW_LOG_VALIDID(id))
226 continue;
227 if (id == ROGUE_FW_SF_MAIN_ASSERT_FAILED) {
228 /* Assertion failure marks the end of the trace. */
229 return false;
230 }
231
232 sf_id = find_sfid(id);
233 if (sf_id == ROGUE_FW_SF_FIRST)
234 continue;
235 if (sf_id == ROGUE_FW_SF_LAST) {
236 /*
237 * Could not match with an ID in the SF table, trace is
238 * most likely corrupt from this point.
239 */
240 return false;
241 }
242
243 /* Skip over the timestamp, and any parameters. */
244 trace_seq_data->idx += 2 + ROGUE_FW_SF_PARAMNUM(id);
245
246 /* Ensure index is now pointing to a valid trace entry. */
247 id = read_fw_trace(trace_seq_data, 0);
248 if (!ROGUE_FW_LOG_VALIDID(id))
249 continue;
250
251 return true;
252 }
253
254 /* Hit end of trace data. */
255 return false;
256}
257
258/**
259 * fw_trace_get_first() - Find first valid entry in trace
260 * @trace_seq_data: Trace sequence data.
261 *
262 * Skips over invalid (usually zero) and ROGUE_FW_SF_FIRST entries.
263 *
264 * If the trace has no valid entries, this function will exit with the trace
265 * index pointing to the end of the trace. trace_seq_show() will return an error
266 * in this state.
267 */
268static void fw_trace_get_first(struct pvr_fw_trace_seq_data *trace_seq_data)
269{
270 trace_seq_data->idx = 0;
271
272 while (trace_seq_data->idx < ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS) {
273 u32 id = read_fw_trace(trace_seq_data, 0);
274
275 if (ROGUE_FW_LOG_VALIDID(id)) {
276 u32 sf_id = find_sfid(id);
277
278 if (sf_id != ROGUE_FW_SF_FIRST)
279 break;
280 }
281 trace_seq_data->idx++;
282 }
283}
284
285static void *fw_trace_seq_start(struct seq_file *s, loff_t *pos)
286{
287 struct pvr_fw_trace_seq_data *trace_seq_data = s->private;
288 u32 i;
289
290 /* Reset trace index, then advance to *pos. */
291 fw_trace_get_first(trace_seq_data);
292
293 for (i = 0; i < *pos; i++) {
294 if (!fw_trace_get_next(trace_seq_data))
295 return NULL;
296 }
297
298 return (trace_seq_data->idx < ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS) ? pos : NULL;
299}
300
301static void *fw_trace_seq_next(struct seq_file *s, void *v, loff_t *pos)
302{
303 struct pvr_fw_trace_seq_data *trace_seq_data = s->private;
304
305 (*pos)++;
306 if (!fw_trace_get_next(trace_seq_data))
307 return NULL;
308
309 return (trace_seq_data->idx < ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS) ? pos : NULL;
310}
311
312static void fw_trace_seq_stop(struct seq_file *s, void *v)
313{
314}
315
316static int fw_trace_seq_show(struct seq_file *s, void *v)
317{
318 struct pvr_fw_trace_seq_data *trace_seq_data = s->private;
319 u64 timestamp;
320 u32 id;
321 u32 sf_id;
322
323 if (trace_seq_data->idx >= ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS)
324 return -EINVAL;
325
326 id = read_fw_trace(trace_seq_data, 0);
327 /* Index is not pointing at a valid entry. */
328 if (!ROGUE_FW_LOG_VALIDID(id))
329 return -EINVAL;
330
331 sf_id = find_sfid(id);
332 /* Index is not pointing at a valid entry. */
333 if (sf_id == ROGUE_FW_SF_LAST)
334 return -EINVAL;
335
336 timestamp = ((u64)read_fw_trace(trace_seq_data, 1) << 32) |
337 read_fw_trace(trace_seq_data, 2);
338 timestamp = (timestamp & ~ROGUE_FWT_TIMESTAMP_TIME_CLRMSK) >>
339 ROGUE_FWT_TIMESTAMP_TIME_SHIFT;
340
341 seq_printf(s, "[%llu] : ", timestamp);
342 if (id == ROGUE_FW_SF_MAIN_ASSERT_FAILED) {
343 seq_printf(s, "ASSERTION %s failed at %s:%u",
344 trace_seq_data->assert_buf.info,
345 trace_seq_data->assert_buf.path,
346 trace_seq_data->assert_buf.line_num);
347 } else {
348 seq_printf(s, stid_fmts[sf_id].name,
349 read_fw_trace(trace_seq_data, 3),
350 read_fw_trace(trace_seq_data, 4),
351 read_fw_trace(trace_seq_data, 5),
352 read_fw_trace(trace_seq_data, 6),
353 read_fw_trace(trace_seq_data, 7),
354 read_fw_trace(trace_seq_data, 8),
355 read_fw_trace(trace_seq_data, 9),
356 read_fw_trace(trace_seq_data, 10),
357 read_fw_trace(trace_seq_data, 11),
358 read_fw_trace(trace_seq_data, 12),
359 read_fw_trace(trace_seq_data, 13),
360 read_fw_trace(trace_seq_data, 14),
361 read_fw_trace(trace_seq_data, 15),
362 read_fw_trace(trace_seq_data, 16),
363 read_fw_trace(trace_seq_data, 17),
364 read_fw_trace(trace_seq_data, 18),
365 read_fw_trace(trace_seq_data, 19),
366 read_fw_trace(trace_seq_data, 20),
367 read_fw_trace(trace_seq_data, 21),
368 read_fw_trace(trace_seq_data, 22));
369 }
370 seq_puts(s, "\n");
371 return 0;
372}
373
374static const struct seq_operations pvr_fw_trace_seq_ops = {
375 .start = fw_trace_seq_start,
376 .next = fw_trace_seq_next,
377 .stop = fw_trace_seq_stop,
378 .show = fw_trace_seq_show
379};
380
381static int fw_trace_open(struct inode *inode, struct file *file)
382{
383 struct pvr_fw_trace_buffer *trace_buffer = inode->i_private;
384 struct rogue_fwif_tracebuf_space *tracebuf_space =
385 trace_buffer->tracebuf_space;
386 struct pvr_fw_trace_seq_data *trace_seq_data;
387 int err;
388
389 trace_seq_data = kzalloc(sizeof(*trace_seq_data), GFP_KERNEL);
390 if (!trace_seq_data)
391 return -ENOMEM;
392
393 trace_seq_data->buffer = kcalloc(ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS,
394 sizeof(*trace_seq_data->buffer), GFP_KERNEL);
395 if (!trace_seq_data->buffer) {
396 err = -ENOMEM;
397 goto err_free_data;
398 }
399
400 /*
401 * Take a local copy of the trace buffer, as firmware may still be
402 * writing to it. This will exist as long as this file is open.
403 */
404 memcpy(trace_seq_data->buffer, trace_buffer->buf,
405 ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS * sizeof(u32));
406 trace_seq_data->start_offset = READ_ONCE(tracebuf_space->trace_pointer);
407 trace_seq_data->assert_buf = tracebuf_space->assert_buf;
408 fw_trace_get_first(trace_seq_data);
409
410 err = seq_open(file, &pvr_fw_trace_seq_ops);
411 if (err)
412 goto err_free_buffer;
413
414 ((struct seq_file *)file->private_data)->private = trace_seq_data;
415
416 return 0;
417
418err_free_buffer:
419 kfree(trace_seq_data->buffer);
420
421err_free_data:
422 kfree(trace_seq_data);
423
424 return err;
425}
426
427static int fw_trace_release(struct inode *inode, struct file *file)
428{
429 struct pvr_fw_trace_seq_data *trace_seq_data =
430 ((struct seq_file *)file->private_data)->private;
431
432 seq_release(inode, file);
433 kfree(trace_seq_data->buffer);
434 kfree(trace_seq_data);
435
436 return 0;
437}
438
439static const struct file_operations pvr_fw_trace_fops = {
440 .owner = THIS_MODULE,
441 .open = fw_trace_open,
442 .read = seq_read,
443 .llseek = seq_lseek,
444 .release = fw_trace_release,
445};
446
447void
448pvr_fw_trace_mask_update(struct pvr_device *pvr_dev, u32 old_mask, u32 new_mask)
449{
450 if (old_mask != new_mask)
451 update_logtype(pvr_dev, new_mask);
452}
453
454void
455pvr_fw_trace_debugfs_init(struct pvr_device *pvr_dev, struct dentry *dir)
456{
457 struct pvr_fw_trace *fw_trace = &pvr_dev->fw_dev.fw_trace;
458 u32 thread_nr;
459
460 static_assert(ARRAY_SIZE(fw_trace->buffers) <= 10,
461 "The filename buffer is only large enough for a single-digit thread count");
462
463 for (thread_nr = 0; thread_nr < ARRAY_SIZE(fw_trace->buffers); ++thread_nr) {
464 char filename[8];
465
466 snprintf(filename, ARRAY_SIZE(filename), "trace_%u", thread_nr);
467 debugfs_create_file(filename, 0400, dir,
468 &fw_trace->buffers[thread_nr],
469 &pvr_fw_trace_fops);
470 }
471}
472#endif
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
13#include <linux/build_bug.h>
14#include <linux/dcache.h>
15#include <linux/sysfs.h>
16#include <linux/types.h>
17
18static void
19tracebuf_ctrl_init(void *cpu_ptr, void *priv)
20{
21 struct rogue_fwif_tracebuf *tracebuf_ctrl = cpu_ptr;
22 struct pvr_fw_trace *fw_trace = priv;
23 u32 thread_nr;
24
25 tracebuf_ctrl->tracebuf_size_in_dwords = ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS;
26 tracebuf_ctrl->tracebuf_flags = 0;
27
28 if (fw_trace->group_mask)
29 tracebuf_ctrl->log_type = fw_trace->group_mask | ROGUE_FWIF_LOG_TYPE_TRACE;
30 else
31 tracebuf_ctrl->log_type = ROGUE_FWIF_LOG_TYPE_NONE;
32
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];
37
38 pvr_fw_object_get_fw_addr(trace_buffer->buf_obj,
39 &tracebuf_space->trace_buffer_fw_addr);
40
41 tracebuf_space->trace_buffer = trace_buffer->buf;
42 tracebuf_space->trace_pointer = 0;
43 }
44}
45
46int pvr_fw_trace_init(struct pvr_device *pvr_dev)
47{
48 struct pvr_fw_trace *fw_trace = &pvr_dev->fw_dev.fw_trace;
49 struct drm_device *drm_dev = from_pvr_device(pvr_dev);
50 u32 thread_nr;
51 int err;
52
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];
55
56 trace_buffer->buf =
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;
67 goto err_free_buf;
68 }
69 }
70
71 /* TODO: Provide control of group mask. */
72 fw_trace->group_mask = 0;
73
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);
84 goto err_free_buf;
85 }
86
87 BUILD_BUG_ON(ARRAY_SIZE(fw_trace->tracebuf_ctrl->tracebuf) !=
88 ARRAY_SIZE(fw_trace->buffers));
89
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];
94
95 trace_buffer->tracebuf_space = tracebuf_space;
96 }
97
98 return 0;
99
100err_free_buf:
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];
103
104 if (trace_buffer->buf)
105 pvr_fw_object_unmap_and_destroy(trace_buffer->buf_obj);
106 }
107
108 return err;
109}
110
111void pvr_fw_trace_fini(struct pvr_device *pvr_dev)
112{
113 struct pvr_fw_trace *fw_trace = &pvr_dev->fw_dev.fw_trace;
114 u32 thread_nr;
115
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];
118
119 pvr_fw_object_unmap_and_destroy(trace_buffer->buf_obj);
120 }
121 pvr_fw_object_unmap_and_destroy(fw_trace->tracebuf_ctrl_obj);
122}
123
124#if defined(CONFIG_DEBUG_FS)
125
126/**
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.
130 *
131 * Returns:
132 * * 0 on success,
133 * * Any error returned by pvr_kccb_send_cmd(), or
134 * * -%EIO if the device is lost.
135 */
136static int
137update_logtype(struct pvr_device *pvr_dev, u32 group_mask)
138{
139 struct pvr_fw_trace *fw_trace = &pvr_dev->fw_dev.fw_trace;
140 struct rogue_fwif_kccb_cmd cmd;
141 int idx;
142 int err;
143
144 if (group_mask)
145 fw_trace->tracebuf_ctrl->log_type = ROGUE_FWIF_LOG_TYPE_TRACE | group_mask;
146 else
147 fw_trace->tracebuf_ctrl->log_type = ROGUE_FWIF_LOG_TYPE_NONE;
148
149 fw_trace->group_mask = group_mask;
150
151 down_read(&pvr_dev->reset_sem);
152 if (!drm_dev_enter(from_pvr_device(pvr_dev), &idx)) {
153 err = -EIO;
154 goto err_up_read;
155 }
156
157 cmd.cmd_type = ROGUE_FWIF_KCCB_CMD_LOGTYPE_UPDATE;
158 cmd.kccb_flags = 0;
159
160 err = pvr_kccb_send_cmd(pvr_dev, &cmd, NULL);
161
162 drm_dev_exit(idx);
163
164err_up_read:
165 up_read(&pvr_dev->reset_sem);
166
167 return err;
168}
169
170struct pvr_fw_trace_seq_data {
171 /** @buffer: Pointer to copy of trace data. */
172 u32 *buffer;
173
174 /** @start_offset: Starting offset in trace data, as reported by FW. */
175 u32 start_offset;
176
177 /** @idx: Current index into trace data. */
178 u32 idx;
179
180 /** @assert_buf: Trace assert buffer, as reported by FW. */
181 struct rogue_fwif_file_info_buf assert_buf;
182};
183
184static u32 find_sfid(u32 id)
185{
186 u32 i;
187
188 for (i = 0; i < ARRAY_SIZE(stid_fmts); i++) {
189 if (stid_fmts[i].id == id)
190 return i;
191 }
192
193 return ROGUE_FW_SF_LAST;
194}
195
196static u32 read_fw_trace(struct pvr_fw_trace_seq_data *trace_seq_data, u32 offset)
197{
198 u32 idx;
199
200 idx = trace_seq_data->idx + offset;
201 if (idx >= ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS)
202 return 0;
203
204 idx = (idx + trace_seq_data->start_offset) % ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS;
205 return trace_seq_data->buffer[idx];
206}
207
208/**
209 * fw_trace_get_next() - Advance trace index to next entry
210 * @trace_seq_data: Trace sequence data.
211 *
212 * Returns:
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
215 * of the trace.
216 */
217static bool fw_trace_get_next(struct pvr_fw_trace_seq_data *trace_seq_data)
218{
219 u32 id, sf_id;
220
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))
225 continue;
226 if (id == ROGUE_FW_SF_MAIN_ASSERT_FAILED) {
227 /* Assertion failure marks the end of the trace. */
228 return false;
229 }
230
231 sf_id = find_sfid(id);
232 if (sf_id == ROGUE_FW_SF_FIRST)
233 continue;
234 if (sf_id == ROGUE_FW_SF_LAST) {
235 /*
236 * Could not match with an ID in the SF table, trace is
237 * most likely corrupt from this point.
238 */
239 return false;
240 }
241
242 /* Skip over the timestamp, and any parameters. */
243 trace_seq_data->idx += 2 + ROGUE_FW_SF_PARAMNUM(id);
244
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))
248 continue;
249
250 return true;
251 }
252
253 /* Hit end of trace data. */
254 return false;
255}
256
257/**
258 * fw_trace_get_first() - Find first valid entry in trace
259 * @trace_seq_data: Trace sequence data.
260 *
261 * Skips over invalid (usually zero) and ROGUE_FW_SF_FIRST entries.
262 *
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
265 * in this state.
266 */
267static void fw_trace_get_first(struct pvr_fw_trace_seq_data *trace_seq_data)
268{
269 trace_seq_data->idx = 0;
270
271 while (trace_seq_data->idx < ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS) {
272 u32 id = read_fw_trace(trace_seq_data, 0);
273
274 if (ROGUE_FW_LOG_VALIDID(id)) {
275 u32 sf_id = find_sfid(id);
276
277 if (sf_id != ROGUE_FW_SF_FIRST)
278 break;
279 }
280 trace_seq_data->idx++;
281 }
282}
283
284static void *fw_trace_seq_start(struct seq_file *s, loff_t *pos)
285{
286 struct pvr_fw_trace_seq_data *trace_seq_data = s->private;
287 u32 i;
288
289 /* Reset trace index, then advance to *pos. */
290 fw_trace_get_first(trace_seq_data);
291
292 for (i = 0; i < *pos; i++) {
293 if (!fw_trace_get_next(trace_seq_data))
294 return NULL;
295 }
296
297 return (trace_seq_data->idx < ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS) ? pos : NULL;
298}
299
300static void *fw_trace_seq_next(struct seq_file *s, void *v, loff_t *pos)
301{
302 struct pvr_fw_trace_seq_data *trace_seq_data = s->private;
303
304 (*pos)++;
305 if (!fw_trace_get_next(trace_seq_data))
306 return NULL;
307
308 return (trace_seq_data->idx < ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS) ? pos : NULL;
309}
310
311static void fw_trace_seq_stop(struct seq_file *s, void *v)
312{
313}
314
315static int fw_trace_seq_show(struct seq_file *s, void *v)
316{
317 struct pvr_fw_trace_seq_data *trace_seq_data = s->private;
318 u64 timestamp;
319 u32 id;
320 u32 sf_id;
321
322 if (trace_seq_data->idx >= ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS)
323 return -EINVAL;
324
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))
328 return -EINVAL;
329
330 sf_id = find_sfid(id);
331 /* Index is not pointing at a valid entry. */
332 if (sf_id == ROGUE_FW_SF_LAST)
333 return -EINVAL;
334
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;
339
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);
346 } else {
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));
368 }
369 seq_puts(s, "\n");
370 return 0;
371}
372
373static 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
378};
379
380static int fw_trace_open(struct inode *inode, struct file *file)
381{
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;
386 int err;
387
388 trace_seq_data = kzalloc(sizeof(*trace_seq_data), GFP_KERNEL);
389 if (!trace_seq_data)
390 return -ENOMEM;
391
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) {
395 err = -ENOMEM;
396 goto err_free_data;
397 }
398
399 /*
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.
402 */
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);
408
409 err = seq_open(file, &pvr_fw_trace_seq_ops);
410 if (err)
411 goto err_free_buffer;
412
413 ((struct seq_file *)file->private_data)->private = trace_seq_data;
414
415 return 0;
416
417err_free_buffer:
418 kfree(trace_seq_data->buffer);
419
420err_free_data:
421 kfree(trace_seq_data);
422
423 return err;
424}
425
426static int fw_trace_release(struct inode *inode, struct file *file)
427{
428 struct pvr_fw_trace_seq_data *trace_seq_data =
429 ((struct seq_file *)file->private_data)->private;
430
431 seq_release(inode, file);
432 kfree(trace_seq_data->buffer);
433 kfree(trace_seq_data);
434
435 return 0;
436}
437
438static const struct file_operations pvr_fw_trace_fops = {
439 .owner = THIS_MODULE,
440 .open = fw_trace_open,
441 .read = seq_read,
442 .llseek = seq_lseek,
443 .release = fw_trace_release,
444};
445
446void
447pvr_fw_trace_mask_update(struct pvr_device *pvr_dev, u32 old_mask, u32 new_mask)
448{
449 if (old_mask != new_mask)
450 update_logtype(pvr_dev, new_mask);
451}
452
453void
454pvr_fw_trace_debugfs_init(struct pvr_device *pvr_dev, struct dentry *dir)
455{
456 struct pvr_fw_trace *fw_trace = &pvr_dev->fw_dev.fw_trace;
457 u32 thread_nr;
458
459 static_assert(ARRAY_SIZE(fw_trace->buffers) <= 10,
460 "The filename buffer is only large enough for a single-digit thread count");
461
462 for (thread_nr = 0; thread_nr < ARRAY_SIZE(fw_trace->buffers); ++thread_nr) {
463 char filename[8];
464
465 snprintf(filename, ARRAY_SIZE(filename), "trace_%u", thread_nr);
466 debugfs_create_file(filename, 0400, dir,
467 &fw_trace->buffers[thread_nr],
468 &pvr_fw_trace_fops);
469 }
470}
471#endif