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