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