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