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