| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472 |
- // SPDX-License-Identifier: GPL-2.0-only OR MIT
- /* Copyright (c) 2023 Imagination Technologies Ltd. */
- #include "pvr_device.h"
- #include "pvr_gem.h"
- #include "pvr_rogue_fwif.h"
- #include "pvr_rogue_fwif_sf.h"
- #include "pvr_fw_trace.h"
- #include <drm/drm_drv.h>
- #include <drm/drm_file.h>
- #include <linux/build_bug.h>
- #include <linux/dcache.h>
- #include <linux/debugfs.h>
- #include <linux/sysfs.h>
- #include <linux/types.h>
- static void
- tracebuf_ctrl_init(void *cpu_ptr, void *priv)
- {
- struct rogue_fwif_tracebuf *tracebuf_ctrl = cpu_ptr;
- struct pvr_fw_trace *fw_trace = priv;
- u32 thread_nr;
- tracebuf_ctrl->tracebuf_size_in_dwords = ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS;
- tracebuf_ctrl->tracebuf_flags = 0;
- if (fw_trace->group_mask)
- tracebuf_ctrl->log_type = fw_trace->group_mask | ROGUE_FWIF_LOG_TYPE_TRACE;
- else
- tracebuf_ctrl->log_type = ROGUE_FWIF_LOG_TYPE_NONE;
- for (thread_nr = 0; thread_nr < ARRAY_SIZE(fw_trace->buffers); thread_nr++) {
- struct rogue_fwif_tracebuf_space *tracebuf_space =
- &tracebuf_ctrl->tracebuf[thread_nr];
- struct pvr_fw_trace_buffer *trace_buffer = &fw_trace->buffers[thread_nr];
- pvr_fw_object_get_fw_addr(trace_buffer->buf_obj,
- &tracebuf_space->trace_buffer_fw_addr);
- tracebuf_space->trace_buffer = trace_buffer->buf;
- tracebuf_space->trace_pointer = 0;
- }
- }
- int pvr_fw_trace_init(struct pvr_device *pvr_dev)
- {
- struct pvr_fw_trace *fw_trace = &pvr_dev->fw_dev.fw_trace;
- struct drm_device *drm_dev = from_pvr_device(pvr_dev);
- u32 thread_nr;
- int err;
- for (thread_nr = 0; thread_nr < ARRAY_SIZE(fw_trace->buffers); thread_nr++) {
- struct pvr_fw_trace_buffer *trace_buffer = &fw_trace->buffers[thread_nr];
- trace_buffer->buf =
- pvr_fw_object_create_and_map(pvr_dev,
- ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS *
- sizeof(*trace_buffer->buf),
- PVR_BO_FW_FLAGS_DEVICE_UNCACHED |
- PVR_BO_FW_NO_CLEAR_ON_RESET,
- NULL, NULL, &trace_buffer->buf_obj);
- if (IS_ERR(trace_buffer->buf)) {
- drm_err(drm_dev, "Unable to allocate trace buffer\n");
- err = PTR_ERR(trace_buffer->buf);
- trace_buffer->buf = NULL;
- goto err_free_buf;
- }
- }
- /* TODO: Provide control of group mask. */
- fw_trace->group_mask = 0;
- fw_trace->tracebuf_ctrl =
- pvr_fw_object_create_and_map(pvr_dev,
- sizeof(*fw_trace->tracebuf_ctrl),
- PVR_BO_FW_FLAGS_DEVICE_UNCACHED |
- PVR_BO_FW_NO_CLEAR_ON_RESET,
- tracebuf_ctrl_init, fw_trace,
- &fw_trace->tracebuf_ctrl_obj);
- if (IS_ERR(fw_trace->tracebuf_ctrl)) {
- drm_err(drm_dev, "Unable to allocate trace buffer control structure\n");
- err = PTR_ERR(fw_trace->tracebuf_ctrl);
- goto err_free_buf;
- }
- BUILD_BUG_ON(ARRAY_SIZE(fw_trace->tracebuf_ctrl->tracebuf) !=
- ARRAY_SIZE(fw_trace->buffers));
- for (thread_nr = 0; thread_nr < ARRAY_SIZE(fw_trace->buffers); thread_nr++) {
- struct rogue_fwif_tracebuf_space *tracebuf_space =
- &fw_trace->tracebuf_ctrl->tracebuf[thread_nr];
- struct pvr_fw_trace_buffer *trace_buffer = &fw_trace->buffers[thread_nr];
- trace_buffer->tracebuf_space = tracebuf_space;
- }
- return 0;
- err_free_buf:
- for (thread_nr = 0; thread_nr < ARRAY_SIZE(fw_trace->buffers); thread_nr++) {
- struct pvr_fw_trace_buffer *trace_buffer = &fw_trace->buffers[thread_nr];
- if (trace_buffer->buf)
- pvr_fw_object_unmap_and_destroy(trace_buffer->buf_obj);
- }
- return err;
- }
- void pvr_fw_trace_fini(struct pvr_device *pvr_dev)
- {
- struct pvr_fw_trace *fw_trace = &pvr_dev->fw_dev.fw_trace;
- u32 thread_nr;
- for (thread_nr = 0; thread_nr < ARRAY_SIZE(fw_trace->buffers); thread_nr++) {
- struct pvr_fw_trace_buffer *trace_buffer = &fw_trace->buffers[thread_nr];
- pvr_fw_object_unmap_and_destroy(trace_buffer->buf_obj);
- }
- pvr_fw_object_unmap_and_destroy(fw_trace->tracebuf_ctrl_obj);
- }
- #if defined(CONFIG_DEBUG_FS)
- /**
- * update_logtype() - Send KCCB command to trigger FW to update logtype
- * @pvr_dev: Target PowerVR device
- * @group_mask: New log group mask.
- *
- * Returns:
- * * 0 on success,
- * * Any error returned by pvr_kccb_send_cmd(), or
- * * -%EIO if the device is lost.
- */
- static int
- update_logtype(struct pvr_device *pvr_dev, u32 group_mask)
- {
- struct pvr_fw_trace *fw_trace = &pvr_dev->fw_dev.fw_trace;
- struct rogue_fwif_kccb_cmd cmd;
- int idx;
- int err;
- if (group_mask)
- fw_trace->tracebuf_ctrl->log_type = ROGUE_FWIF_LOG_TYPE_TRACE | group_mask;
- else
- fw_trace->tracebuf_ctrl->log_type = ROGUE_FWIF_LOG_TYPE_NONE;
- fw_trace->group_mask = group_mask;
- down_read(&pvr_dev->reset_sem);
- if (!drm_dev_enter(from_pvr_device(pvr_dev), &idx)) {
- err = -EIO;
- goto err_up_read;
- }
- cmd.cmd_type = ROGUE_FWIF_KCCB_CMD_LOGTYPE_UPDATE;
- cmd.kccb_flags = 0;
- err = pvr_kccb_send_cmd(pvr_dev, &cmd, NULL);
- drm_dev_exit(idx);
- err_up_read:
- up_read(&pvr_dev->reset_sem);
- return err;
- }
- struct pvr_fw_trace_seq_data {
- /** @buffer: Pointer to copy of trace data. */
- u32 *buffer;
- /** @start_offset: Starting offset in trace data, as reported by FW. */
- u32 start_offset;
- /** @idx: Current index into trace data. */
- u32 idx;
- /** @assert_buf: Trace assert buffer, as reported by FW. */
- struct rogue_fwif_file_info_buf assert_buf;
- };
- static u32 find_sfid(u32 id)
- {
- u32 i;
- for (i = 0; i < ARRAY_SIZE(stid_fmts); i++) {
- if (stid_fmts[i].id == id)
- return i;
- }
- return ROGUE_FW_SF_LAST;
- }
- static u32 read_fw_trace(struct pvr_fw_trace_seq_data *trace_seq_data, u32 offset)
- {
- u32 idx;
- idx = trace_seq_data->idx + offset;
- if (idx >= ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS)
- return 0;
- idx = (idx + trace_seq_data->start_offset) % ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS;
- return trace_seq_data->buffer[idx];
- }
- /**
- * fw_trace_get_next() - Advance trace index to next entry
- * @trace_seq_data: Trace sequence data.
- *
- * Returns:
- * * %true if trace index is now pointing to a valid entry, or
- * * %false if trace index is pointing to an invalid entry, or has hit the end
- * of the trace.
- */
- static bool fw_trace_get_next(struct pvr_fw_trace_seq_data *trace_seq_data)
- {
- u32 id, sf_id;
- while (trace_seq_data->idx < ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS) {
- id = read_fw_trace(trace_seq_data, 0);
- trace_seq_data->idx++;
- if (!ROGUE_FW_LOG_VALIDID(id))
- continue;
- if (id == ROGUE_FW_SF_MAIN_ASSERT_FAILED) {
- /* Assertion failure marks the end of the trace. */
- return false;
- }
- sf_id = find_sfid(id);
- if (sf_id == ROGUE_FW_SF_FIRST)
- continue;
- if (sf_id == ROGUE_FW_SF_LAST) {
- /*
- * Could not match with an ID in the SF table, trace is
- * most likely corrupt from this point.
- */
- return false;
- }
- /* Skip over the timestamp, and any parameters. */
- trace_seq_data->idx += 2 + ROGUE_FW_SF_PARAMNUM(id);
- /* Ensure index is now pointing to a valid trace entry. */
- id = read_fw_trace(trace_seq_data, 0);
- if (!ROGUE_FW_LOG_VALIDID(id))
- continue;
- return true;
- }
- /* Hit end of trace data. */
- return false;
- }
- /**
- * fw_trace_get_first() - Find first valid entry in trace
- * @trace_seq_data: Trace sequence data.
- *
- * Skips over invalid (usually zero) and ROGUE_FW_SF_FIRST entries.
- *
- * If the trace has no valid entries, this function will exit with the trace
- * index pointing to the end of the trace. trace_seq_show() will return an error
- * in this state.
- */
- static void fw_trace_get_first(struct pvr_fw_trace_seq_data *trace_seq_data)
- {
- trace_seq_data->idx = 0;
- while (trace_seq_data->idx < ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS) {
- u32 id = read_fw_trace(trace_seq_data, 0);
- if (ROGUE_FW_LOG_VALIDID(id)) {
- u32 sf_id = find_sfid(id);
- if (sf_id != ROGUE_FW_SF_FIRST)
- break;
- }
- trace_seq_data->idx++;
- }
- }
- static void *fw_trace_seq_start(struct seq_file *s, loff_t *pos)
- {
- struct pvr_fw_trace_seq_data *trace_seq_data = s->private;
- u32 i;
- /* Reset trace index, then advance to *pos. */
- fw_trace_get_first(trace_seq_data);
- for (i = 0; i < *pos; i++) {
- if (!fw_trace_get_next(trace_seq_data))
- return NULL;
- }
- return (trace_seq_data->idx < ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS) ? pos : NULL;
- }
- static void *fw_trace_seq_next(struct seq_file *s, void *v, loff_t *pos)
- {
- struct pvr_fw_trace_seq_data *trace_seq_data = s->private;
- (*pos)++;
- if (!fw_trace_get_next(trace_seq_data))
- return NULL;
- return (trace_seq_data->idx < ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS) ? pos : NULL;
- }
- static void fw_trace_seq_stop(struct seq_file *s, void *v)
- {
- }
- static int fw_trace_seq_show(struct seq_file *s, void *v)
- {
- struct pvr_fw_trace_seq_data *trace_seq_data = s->private;
- u64 timestamp;
- u32 id;
- u32 sf_id;
- if (trace_seq_data->idx >= ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS)
- return -EINVAL;
- id = read_fw_trace(trace_seq_data, 0);
- /* Index is not pointing at a valid entry. */
- if (!ROGUE_FW_LOG_VALIDID(id))
- return -EINVAL;
- sf_id = find_sfid(id);
- /* Index is not pointing at a valid entry. */
- if (sf_id == ROGUE_FW_SF_LAST)
- return -EINVAL;
- timestamp = ((u64)read_fw_trace(trace_seq_data, 1) << 32) |
- read_fw_trace(trace_seq_data, 2);
- timestamp = (timestamp & ~ROGUE_FWT_TIMESTAMP_TIME_CLRMSK) >>
- ROGUE_FWT_TIMESTAMP_TIME_SHIFT;
- seq_printf(s, "[%llu] : ", timestamp);
- if (id == ROGUE_FW_SF_MAIN_ASSERT_FAILED) {
- seq_printf(s, "ASSERTION %s failed at %s:%u",
- trace_seq_data->assert_buf.info,
- trace_seq_data->assert_buf.path,
- trace_seq_data->assert_buf.line_num);
- } else {
- seq_printf(s, stid_fmts[sf_id].name,
- read_fw_trace(trace_seq_data, 3),
- read_fw_trace(trace_seq_data, 4),
- read_fw_trace(trace_seq_data, 5),
- read_fw_trace(trace_seq_data, 6),
- read_fw_trace(trace_seq_data, 7),
- read_fw_trace(trace_seq_data, 8),
- read_fw_trace(trace_seq_data, 9),
- read_fw_trace(trace_seq_data, 10),
- read_fw_trace(trace_seq_data, 11),
- read_fw_trace(trace_seq_data, 12),
- read_fw_trace(trace_seq_data, 13),
- read_fw_trace(trace_seq_data, 14),
- read_fw_trace(trace_seq_data, 15),
- read_fw_trace(trace_seq_data, 16),
- read_fw_trace(trace_seq_data, 17),
- read_fw_trace(trace_seq_data, 18),
- read_fw_trace(trace_seq_data, 19),
- read_fw_trace(trace_seq_data, 20),
- read_fw_trace(trace_seq_data, 21),
- read_fw_trace(trace_seq_data, 22));
- }
- seq_puts(s, "\n");
- return 0;
- }
- static const struct seq_operations pvr_fw_trace_seq_ops = {
- .start = fw_trace_seq_start,
- .next = fw_trace_seq_next,
- .stop = fw_trace_seq_stop,
- .show = fw_trace_seq_show
- };
- static int fw_trace_open(struct inode *inode, struct file *file)
- {
- struct pvr_fw_trace_buffer *trace_buffer = inode->i_private;
- struct rogue_fwif_tracebuf_space *tracebuf_space =
- trace_buffer->tracebuf_space;
- struct pvr_fw_trace_seq_data *trace_seq_data;
- int err;
- trace_seq_data = kzalloc(sizeof(*trace_seq_data), GFP_KERNEL);
- if (!trace_seq_data)
- return -ENOMEM;
- trace_seq_data->buffer = kcalloc(ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS,
- sizeof(*trace_seq_data->buffer), GFP_KERNEL);
- if (!trace_seq_data->buffer) {
- err = -ENOMEM;
- goto err_free_data;
- }
- /*
- * Take a local copy of the trace buffer, as firmware may still be
- * writing to it. This will exist as long as this file is open.
- */
- memcpy(trace_seq_data->buffer, trace_buffer->buf,
- ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS * sizeof(u32));
- trace_seq_data->start_offset = READ_ONCE(tracebuf_space->trace_pointer);
- trace_seq_data->assert_buf = tracebuf_space->assert_buf;
- fw_trace_get_first(trace_seq_data);
- err = seq_open(file, &pvr_fw_trace_seq_ops);
- if (err)
- goto err_free_buffer;
- ((struct seq_file *)file->private_data)->private = trace_seq_data;
- return 0;
- err_free_buffer:
- kfree(trace_seq_data->buffer);
- err_free_data:
- kfree(trace_seq_data);
- return err;
- }
- static int fw_trace_release(struct inode *inode, struct file *file)
- {
- struct pvr_fw_trace_seq_data *trace_seq_data =
- ((struct seq_file *)file->private_data)->private;
- seq_release(inode, file);
- kfree(trace_seq_data->buffer);
- kfree(trace_seq_data);
- return 0;
- }
- static const struct file_operations pvr_fw_trace_fops = {
- .owner = THIS_MODULE,
- .open = fw_trace_open,
- .read = seq_read,
- .llseek = seq_lseek,
- .release = fw_trace_release,
- };
- void
- pvr_fw_trace_mask_update(struct pvr_device *pvr_dev, u32 old_mask, u32 new_mask)
- {
- if (old_mask != new_mask)
- update_logtype(pvr_dev, new_mask);
- }
- void
- pvr_fw_trace_debugfs_init(struct pvr_device *pvr_dev, struct dentry *dir)
- {
- struct pvr_fw_trace *fw_trace = &pvr_dev->fw_dev.fw_trace;
- u32 thread_nr;
- static_assert(ARRAY_SIZE(fw_trace->buffers) <= 10,
- "The filename buffer is only large enough for a single-digit thread count");
- for (thread_nr = 0; thread_nr < ARRAY_SIZE(fw_trace->buffers); ++thread_nr) {
- char filename[8];
- snprintf(filename, ARRAY_SIZE(filename), "trace_%u", thread_nr);
- debugfs_create_file(filename, 0400, dir,
- &fw_trace->buffers[thread_nr],
- &pvr_fw_trace_fops);
- }
- }
- #endif
|