1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2023 Intel Corporation 4 */ 5 6 #include "xe_devcoredump.h" 7 #include "xe_devcoredump_types.h" 8 9 #include <linux/ascii85.h> 10 #include <linux/devcoredump.h> 11 #include <generated/utsrelease.h> 12 13 #include <drm/drm_managed.h> 14 15 #include "xe_device.h" 16 #include "xe_exec_queue.h" 17 #include "xe_force_wake.h" 18 #include "xe_gt.h" 19 #include "xe_gt_printk.h" 20 #include "xe_guc_capture.h" 21 #include "xe_guc_ct.h" 22 #include "xe_guc_log.h" 23 #include "xe_guc_submit.h" 24 #include "xe_hw_engine.h" 25 #include "xe_module.h" 26 #include "xe_sched_job.h" 27 #include "xe_vm.h" 28 29 /** 30 * DOC: Xe device coredump 31 * 32 * Devices overview: 33 * Xe uses dev_coredump infrastructure for exposing the crash errors in a 34 * standardized way. 35 * devcoredump exposes a temporary device under /sys/class/devcoredump/ 36 * which is linked with our card device directly. 37 * The core dump can be accessed either from 38 * /sys/class/drm/card<n>/device/devcoredump/ or from 39 * /sys/class/devcoredump/devcd<m> where 40 * /sys/class/devcoredump/devcd<m>/failing_device is a link to 41 * /sys/class/drm/card<n>/device/. 42 * 43 * Snapshot at hang: 44 * The 'data' file is printed with a drm_printer pointer at devcoredump read 45 * time. For this reason, we need to take snapshots from when the hang has 46 * happened, and not only when the user is reading the file. Otherwise the 47 * information is outdated since the resets might have happened in between. 48 * 49 * 'First' failure snapshot: 50 * In general, the first hang is the most critical one since the following hangs 51 * can be a consequence of the initial hang. For this reason we only take the 52 * snapshot of the 'first' failure and ignore subsequent calls of this function, 53 * at least while the coredump device is alive. Dev_coredump has a delayed work 54 * queue that will eventually delete the device and free all the dump 55 * information. 56 */ 57 58 #ifdef CONFIG_DEV_COREDUMP 59 60 /* 1 hour timeout */ 61 #define XE_COREDUMP_TIMEOUT_JIFFIES (60 * 60 * HZ) 62 63 static struct xe_device *coredump_to_xe(const struct xe_devcoredump *coredump) 64 { 65 return container_of(coredump, struct xe_device, devcoredump); 66 } 67 68 static struct xe_guc *exec_queue_to_guc(struct xe_exec_queue *q) 69 { 70 return &q->gt->uc.guc; 71 } 72 73 static ssize_t __xe_devcoredump_read(char *buffer, size_t count, 74 struct xe_devcoredump *coredump) 75 { 76 struct xe_device *xe; 77 struct xe_devcoredump_snapshot *ss; 78 struct drm_printer p; 79 struct drm_print_iterator iter; 80 struct timespec64 ts; 81 int i; 82 83 xe = coredump_to_xe(coredump); 84 ss = &coredump->snapshot; 85 86 iter.data = buffer; 87 iter.start = 0; 88 iter.remain = count; 89 90 p = drm_coredump_printer(&iter); 91 92 drm_puts(&p, "**** Xe Device Coredump ****\n"); 93 drm_puts(&p, "kernel: " UTS_RELEASE "\n"); 94 drm_puts(&p, "module: " KBUILD_MODNAME "\n"); 95 96 ts = ktime_to_timespec64(ss->snapshot_time); 97 drm_printf(&p, "Snapshot time: %lld.%09ld\n", ts.tv_sec, ts.tv_nsec); 98 ts = ktime_to_timespec64(ss->boot_time); 99 drm_printf(&p, "Uptime: %lld.%09ld\n", ts.tv_sec, ts.tv_nsec); 100 drm_printf(&p, "Process: %s\n", ss->process_name); 101 xe_device_snapshot_print(xe, &p); 102 103 drm_printf(&p, "\n**** GT #%d ****\n", ss->gt->info.id); 104 drm_printf(&p, "\tTile: %d\n", ss->gt->tile->id); 105 106 drm_puts(&p, "\n**** GuC Log ****\n"); 107 xe_guc_log_snapshot_print(ss->guc.log, &p); 108 drm_puts(&p, "\n**** GuC CT ****\n"); 109 xe_guc_ct_snapshot_print(ss->guc.ct, &p); 110 111 drm_puts(&p, "\n**** Contexts ****\n"); 112 xe_guc_exec_queue_snapshot_print(ss->ge, &p); 113 114 drm_puts(&p, "\n**** Job ****\n"); 115 xe_sched_job_snapshot_print(ss->job, &p); 116 117 drm_puts(&p, "\n**** HW Engines ****\n"); 118 for (i = 0; i < XE_NUM_HW_ENGINES; i++) 119 if (ss->hwe[i]) 120 xe_engine_snapshot_print(ss->hwe[i], &p); 121 122 drm_puts(&p, "\n**** VM state ****\n"); 123 xe_vm_snapshot_print(ss->vm, &p); 124 125 return count - iter.remain; 126 } 127 128 static void xe_devcoredump_snapshot_free(struct xe_devcoredump_snapshot *ss) 129 { 130 int i; 131 132 xe_guc_log_snapshot_free(ss->guc.log); 133 ss->guc.log = NULL; 134 135 xe_guc_ct_snapshot_free(ss->guc.ct); 136 ss->guc.ct = NULL; 137 138 xe_guc_capture_put_matched_nodes(&ss->gt->uc.guc); 139 ss->matched_node = NULL; 140 141 xe_guc_exec_queue_snapshot_free(ss->ge); 142 ss->ge = NULL; 143 144 xe_sched_job_snapshot_free(ss->job); 145 ss->job = NULL; 146 147 for (i = 0; i < XE_NUM_HW_ENGINES; i++) 148 if (ss->hwe[i]) { 149 xe_hw_engine_snapshot_free(ss->hwe[i]); 150 ss->hwe[i] = NULL; 151 } 152 153 xe_vm_snapshot_free(ss->vm); 154 ss->vm = NULL; 155 } 156 157 static void xe_devcoredump_deferred_snap_work(struct work_struct *work) 158 { 159 struct xe_devcoredump_snapshot *ss = container_of(work, typeof(*ss), work); 160 struct xe_devcoredump *coredump = container_of(ss, typeof(*coredump), snapshot); 161 unsigned int fw_ref; 162 163 /* keep going if fw fails as we still want to save the memory and SW data */ 164 fw_ref = xe_force_wake_get(gt_to_fw(ss->gt), XE_FORCEWAKE_ALL); 165 if (!xe_force_wake_ref_has_domain(fw_ref, XE_FORCEWAKE_ALL)) 166 xe_gt_info(ss->gt, "failed to get forcewake for coredump capture\n"); 167 xe_vm_snapshot_capture_delayed(ss->vm); 168 xe_guc_exec_queue_snapshot_capture_delayed(ss->ge); 169 xe_force_wake_put(gt_to_fw(ss->gt), fw_ref); 170 171 /* Calculate devcoredump size */ 172 ss->read.size = __xe_devcoredump_read(NULL, INT_MAX, coredump); 173 174 ss->read.buffer = kvmalloc(ss->read.size, GFP_USER); 175 if (!ss->read.buffer) 176 return; 177 178 __xe_devcoredump_read(ss->read.buffer, ss->read.size, coredump); 179 xe_devcoredump_snapshot_free(ss); 180 } 181 182 static ssize_t xe_devcoredump_read(char *buffer, loff_t offset, 183 size_t count, void *data, size_t datalen) 184 { 185 struct xe_devcoredump *coredump = data; 186 struct xe_devcoredump_snapshot *ss; 187 ssize_t byte_copied; 188 189 if (!coredump) 190 return -ENODEV; 191 192 ss = &coredump->snapshot; 193 194 /* Ensure delayed work is captured before continuing */ 195 flush_work(&ss->work); 196 197 if (!ss->read.buffer) 198 return -ENODEV; 199 200 if (offset >= ss->read.size) 201 return 0; 202 203 byte_copied = count < ss->read.size - offset ? count : 204 ss->read.size - offset; 205 memcpy(buffer, ss->read.buffer + offset, byte_copied); 206 207 return byte_copied; 208 } 209 210 static void xe_devcoredump_free(void *data) 211 { 212 struct xe_devcoredump *coredump = data; 213 214 /* Our device is gone. Nothing to do... */ 215 if (!data || !coredump_to_xe(coredump)) 216 return; 217 218 cancel_work_sync(&coredump->snapshot.work); 219 220 xe_devcoredump_snapshot_free(&coredump->snapshot); 221 kvfree(coredump->snapshot.read.buffer); 222 223 /* To prevent stale data on next snapshot, clear everything */ 224 memset(&coredump->snapshot, 0, sizeof(coredump->snapshot)); 225 coredump->captured = false; 226 coredump->job = NULL; 227 drm_info(&coredump_to_xe(coredump)->drm, 228 "Xe device coredump has been deleted.\n"); 229 } 230 231 static void devcoredump_snapshot(struct xe_devcoredump *coredump, 232 struct xe_sched_job *job) 233 { 234 struct xe_devcoredump_snapshot *ss = &coredump->snapshot; 235 struct xe_exec_queue *q = job->q; 236 struct xe_guc *guc = exec_queue_to_guc(q); 237 u32 adj_logical_mask = q->logical_mask; 238 u32 width_mask = (0x1 << q->width) - 1; 239 const char *process_name = "no process"; 240 241 unsigned int fw_ref; 242 bool cookie; 243 int i; 244 245 ss->snapshot_time = ktime_get_real(); 246 ss->boot_time = ktime_get_boottime(); 247 248 if (q->vm && q->vm->xef) 249 process_name = q->vm->xef->process_name; 250 strscpy(ss->process_name, process_name); 251 252 ss->gt = q->gt; 253 coredump->job = job; 254 INIT_WORK(&ss->work, xe_devcoredump_deferred_snap_work); 255 256 cookie = dma_fence_begin_signalling(); 257 for (i = 0; q->width > 1 && i < XE_HW_ENGINE_MAX_INSTANCE;) { 258 if (adj_logical_mask & BIT(i)) { 259 adj_logical_mask |= width_mask << i; 260 i += q->width; 261 } else { 262 ++i; 263 } 264 } 265 266 /* keep going if fw fails as we still want to save the memory and SW data */ 267 fw_ref = xe_force_wake_get(gt_to_fw(q->gt), XE_FORCEWAKE_ALL); 268 269 ss->guc.log = xe_guc_log_snapshot_capture(&guc->log, true); 270 ss->guc.ct = xe_guc_ct_snapshot_capture(&guc->ct); 271 ss->ge = xe_guc_exec_queue_snapshot_capture(q); 272 ss->job = xe_sched_job_snapshot_capture(job); 273 ss->vm = xe_vm_snapshot_capture(q->vm); 274 275 xe_engine_snapshot_capture_for_job(job); 276 277 queue_work(system_unbound_wq, &ss->work); 278 279 xe_force_wake_put(gt_to_fw(q->gt), fw_ref); 280 dma_fence_end_signalling(cookie); 281 } 282 283 /** 284 * xe_devcoredump - Take the required snapshots and initialize coredump device. 285 * @job: The faulty xe_sched_job, where the issue was detected. 286 * 287 * This function should be called at the crash time within the serialized 288 * gt_reset. It is skipped if we still have the core dump device available 289 * with the information of the 'first' snapshot. 290 */ 291 void xe_devcoredump(struct xe_sched_job *job) 292 { 293 struct xe_device *xe = gt_to_xe(job->q->gt); 294 struct xe_devcoredump *coredump = &xe->devcoredump; 295 296 if (coredump->captured) { 297 drm_dbg(&xe->drm, "Multiple hangs are occurring, but only the first snapshot was taken\n"); 298 return; 299 } 300 301 coredump->captured = true; 302 devcoredump_snapshot(coredump, job); 303 304 drm_info(&xe->drm, "Xe device coredump has been created\n"); 305 drm_info(&xe->drm, "Check your /sys/class/drm/card%d/device/devcoredump/data\n", 306 xe->drm.primary->index); 307 308 dev_coredumpm_timeout(xe->drm.dev, THIS_MODULE, coredump, 0, GFP_KERNEL, 309 xe_devcoredump_read, xe_devcoredump_free, 310 XE_COREDUMP_TIMEOUT_JIFFIES); 311 } 312 313 static void xe_driver_devcoredump_fini(void *arg) 314 { 315 struct drm_device *drm = arg; 316 317 dev_coredump_put(drm->dev); 318 } 319 320 int xe_devcoredump_init(struct xe_device *xe) 321 { 322 return devm_add_action_or_reset(xe->drm.dev, xe_driver_devcoredump_fini, &xe->drm); 323 } 324 325 #endif 326 327 /** 328 * xe_print_blob_ascii85 - print a BLOB to some useful location in ASCII85 329 * 330 * The output is split to multiple lines because some print targets, e.g. dmesg 331 * cannot handle arbitrarily long lines. Note also that printing to dmesg in 332 * piece-meal fashion is not possible, each separate call to drm_puts() has a 333 * line-feed automatically added! Therefore, the entire output line must be 334 * constructed in a local buffer first, then printed in one atomic output call. 335 * 336 * There is also a scheduler yield call to prevent the 'task has been stuck for 337 * 120s' kernel hang check feature from firing when printing to a slow target 338 * such as dmesg over a serial port. 339 * 340 * TODO: Add compression prior to the ASCII85 encoding to shrink huge buffers down. 341 * 342 * @p: the printer object to output to 343 * @prefix: optional prefix to add to output string 344 * @blob: the Binary Large OBject to dump out 345 * @offset: offset in bytes to skip from the front of the BLOB, must be a multiple of sizeof(u32) 346 * @size: the size in bytes of the BLOB, must be a multiple of sizeof(u32) 347 */ 348 void xe_print_blob_ascii85(struct drm_printer *p, const char *prefix, 349 const void *blob, size_t offset, size_t size) 350 { 351 const u32 *blob32 = (const u32 *)blob; 352 char buff[ASCII85_BUFSZ], *line_buff; 353 size_t line_pos = 0; 354 355 #define DMESG_MAX_LINE_LEN 800 356 #define MIN_SPACE (ASCII85_BUFSZ + 2) /* 85 + "\n\0" */ 357 358 if (size & 3) 359 drm_printf(p, "Size not word aligned: %zu", size); 360 if (offset & 3) 361 drm_printf(p, "Offset not word aligned: %zu", size); 362 363 line_buff = kzalloc(DMESG_MAX_LINE_LEN, GFP_KERNEL); 364 if (IS_ERR_OR_NULL(line_buff)) { 365 drm_printf(p, "Failed to allocate line buffer: %pe", line_buff); 366 return; 367 } 368 369 blob32 += offset / sizeof(*blob32); 370 size /= sizeof(*blob32); 371 372 if (prefix) { 373 strscpy(line_buff, prefix, DMESG_MAX_LINE_LEN - MIN_SPACE - 2); 374 line_pos = strlen(line_buff); 375 376 line_buff[line_pos++] = ':'; 377 line_buff[line_pos++] = ' '; 378 } 379 380 while (size--) { 381 u32 val = *(blob32++); 382 383 strscpy(line_buff + line_pos, ascii85_encode(val, buff), 384 DMESG_MAX_LINE_LEN - line_pos); 385 line_pos += strlen(line_buff + line_pos); 386 387 if ((line_pos + MIN_SPACE) >= DMESG_MAX_LINE_LEN) { 388 line_buff[line_pos++] = '\n'; 389 line_buff[line_pos++] = 0; 390 391 drm_puts(p, line_buff); 392 393 line_pos = 0; 394 395 /* Prevent 'stuck thread' time out errors */ 396 cond_resched(); 397 } 398 } 399 400 if (line_pos) { 401 line_buff[line_pos++] = '\n'; 402 line_buff[line_pos++] = 0; 403 404 drm_puts(p, line_buff); 405 } 406 407 kfree(line_buff); 408 409 #undef MIN_SPACE 410 #undef DMESG_MAX_LINE_LEN 411 } 412