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/devcoredump.h> 10 #include <generated/utsrelease.h> 11 12 #include <drm/drm_managed.h> 13 14 #include "xe_device.h" 15 #include "xe_exec_queue.h" 16 #include "xe_force_wake.h" 17 #include "xe_gt.h" 18 #include "xe_gt_printk.h" 19 #include "xe_guc_ct.h" 20 #include "xe_guc_submit.h" 21 #include "xe_hw_engine.h" 22 #include "xe_sched_job.h" 23 #include "xe_vm.h" 24 25 /** 26 * DOC: Xe device coredump 27 * 28 * Devices overview: 29 * Xe uses dev_coredump infrastructure for exposing the crash errors in a 30 * standardized way. 31 * devcoredump exposes a temporary device under /sys/class/devcoredump/ 32 * which is linked with our card device directly. 33 * The core dump can be accessed either from 34 * /sys/class/drm/card<n>/device/devcoredump/ or from 35 * /sys/class/devcoredump/devcd<m> where 36 * /sys/class/devcoredump/devcd<m>/failing_device is a link to 37 * /sys/class/drm/card<n>/device/. 38 * 39 * Snapshot at hang: 40 * The 'data' file is printed with a drm_printer pointer at devcoredump read 41 * time. For this reason, we need to take snapshots from when the hang has 42 * happened, and not only when the user is reading the file. Otherwise the 43 * information is outdated since the resets might have happened in between. 44 * 45 * 'First' failure snapshot: 46 * In general, the first hang is the most critical one since the following hangs 47 * can be a consequence of the initial hang. For this reason we only take the 48 * snapshot of the 'first' failure and ignore subsequent calls of this function, 49 * at least while the coredump device is alive. Dev_coredump has a delayed work 50 * queue that will eventually delete the device and free all the dump 51 * information. 52 */ 53 54 #ifdef CONFIG_DEV_COREDUMP 55 56 /* 1 hour timeout */ 57 #define XE_COREDUMP_TIMEOUT_JIFFIES (60 * 60 * HZ) 58 59 static struct xe_device *coredump_to_xe(const struct xe_devcoredump *coredump) 60 { 61 return container_of(coredump, struct xe_device, devcoredump); 62 } 63 64 static struct xe_guc *exec_queue_to_guc(struct xe_exec_queue *q) 65 { 66 return &q->gt->uc.guc; 67 } 68 69 static ssize_t __xe_devcoredump_read(char *buffer, size_t count, 70 struct xe_devcoredump *coredump) 71 { 72 struct xe_device *xe; 73 struct xe_devcoredump_snapshot *ss; 74 struct drm_printer p; 75 struct drm_print_iterator iter; 76 struct timespec64 ts; 77 int i; 78 79 xe = coredump_to_xe(coredump); 80 ss = &coredump->snapshot; 81 82 iter.data = buffer; 83 iter.start = 0; 84 iter.remain = count; 85 86 p = drm_coredump_printer(&iter); 87 88 drm_printf(&p, "**** Xe Device Coredump ****\n"); 89 drm_printf(&p, "kernel: " UTS_RELEASE "\n"); 90 drm_printf(&p, "module: " KBUILD_MODNAME "\n"); 91 92 ts = ktime_to_timespec64(ss->snapshot_time); 93 drm_printf(&p, "Snapshot time: %lld.%09ld\n", ts.tv_sec, ts.tv_nsec); 94 ts = ktime_to_timespec64(ss->boot_time); 95 drm_printf(&p, "Uptime: %lld.%09ld\n", ts.tv_sec, ts.tv_nsec); 96 drm_printf(&p, "Process: %s\n", ss->process_name); 97 xe_device_snapshot_print(xe, &p); 98 99 drm_printf(&p, "\n**** GuC CT ****\n"); 100 xe_guc_ct_snapshot_print(coredump->snapshot.ct, &p); 101 xe_guc_exec_queue_snapshot_print(coredump->snapshot.ge, &p); 102 103 drm_printf(&p, "\n**** Job ****\n"); 104 xe_sched_job_snapshot_print(coredump->snapshot.job, &p); 105 106 drm_printf(&p, "\n**** HW Engines ****\n"); 107 for (i = 0; i < XE_NUM_HW_ENGINES; i++) 108 if (coredump->snapshot.hwe[i]) 109 xe_hw_engine_snapshot_print(coredump->snapshot.hwe[i], 110 &p); 111 drm_printf(&p, "\n**** VM state ****\n"); 112 xe_vm_snapshot_print(coredump->snapshot.vm, &p); 113 114 return count - iter.remain; 115 } 116 117 static void xe_devcoredump_snapshot_free(struct xe_devcoredump_snapshot *ss) 118 { 119 int i; 120 121 xe_guc_ct_snapshot_free(ss->ct); 122 ss->ct = NULL; 123 124 xe_guc_exec_queue_snapshot_free(ss->ge); 125 ss->ge = NULL; 126 127 xe_sched_job_snapshot_free(ss->job); 128 ss->job = NULL; 129 130 for (i = 0; i < XE_NUM_HW_ENGINES; i++) 131 if (ss->hwe[i]) { 132 xe_hw_engine_snapshot_free(ss->hwe[i]); 133 ss->hwe[i] = NULL; 134 } 135 136 xe_vm_snapshot_free(ss->vm); 137 ss->vm = NULL; 138 } 139 140 static void xe_devcoredump_deferred_snap_work(struct work_struct *work) 141 { 142 struct xe_devcoredump_snapshot *ss = container_of(work, typeof(*ss), work); 143 struct xe_devcoredump *coredump = container_of(ss, typeof(*coredump), snapshot); 144 145 /* keep going if fw fails as we still want to save the memory and SW data */ 146 if (xe_force_wake_get(gt_to_fw(ss->gt), XE_FORCEWAKE_ALL)) 147 xe_gt_info(ss->gt, "failed to get forcewake for coredump capture\n"); 148 xe_vm_snapshot_capture_delayed(ss->vm); 149 xe_guc_exec_queue_snapshot_capture_delayed(ss->ge); 150 xe_force_wake_put(gt_to_fw(ss->gt), XE_FORCEWAKE_ALL); 151 152 /* Calculate devcoredump size */ 153 ss->read.size = __xe_devcoredump_read(NULL, INT_MAX, coredump); 154 155 ss->read.buffer = kvmalloc(ss->read.size, GFP_USER); 156 if (!ss->read.buffer) 157 return; 158 159 __xe_devcoredump_read(ss->read.buffer, ss->read.size, coredump); 160 xe_devcoredump_snapshot_free(ss); 161 } 162 163 static ssize_t xe_devcoredump_read(char *buffer, loff_t offset, 164 size_t count, void *data, size_t datalen) 165 { 166 struct xe_devcoredump *coredump = data; 167 struct xe_devcoredump_snapshot *ss; 168 ssize_t byte_copied; 169 170 if (!coredump) 171 return -ENODEV; 172 173 ss = &coredump->snapshot; 174 175 /* Ensure delayed work is captured before continuing */ 176 flush_work(&ss->work); 177 178 if (!ss->read.buffer) 179 return -ENODEV; 180 181 if (offset >= ss->read.size) 182 return 0; 183 184 byte_copied = count < ss->read.size - offset ? count : 185 ss->read.size - offset; 186 memcpy(buffer, ss->read.buffer + offset, byte_copied); 187 188 return byte_copied; 189 } 190 191 static void xe_devcoredump_free(void *data) 192 { 193 struct xe_devcoredump *coredump = data; 194 195 /* Our device is gone. Nothing to do... */ 196 if (!data || !coredump_to_xe(coredump)) 197 return; 198 199 cancel_work_sync(&coredump->snapshot.work); 200 201 xe_devcoredump_snapshot_free(&coredump->snapshot); 202 kvfree(coredump->snapshot.read.buffer); 203 204 /* To prevent stale data on next snapshot, clear everything */ 205 memset(&coredump->snapshot, 0, sizeof(coredump->snapshot)); 206 coredump->captured = false; 207 drm_info(&coredump_to_xe(coredump)->drm, 208 "Xe device coredump has been deleted.\n"); 209 } 210 211 static void devcoredump_snapshot(struct xe_devcoredump *coredump, 212 struct xe_sched_job *job) 213 { 214 struct xe_devcoredump_snapshot *ss = &coredump->snapshot; 215 struct xe_exec_queue *q = job->q; 216 struct xe_guc *guc = exec_queue_to_guc(q); 217 struct xe_hw_engine *hwe; 218 enum xe_hw_engine_id id; 219 u32 adj_logical_mask = q->logical_mask; 220 u32 width_mask = (0x1 << q->width) - 1; 221 const char *process_name = "no process"; 222 223 int i; 224 bool cookie; 225 226 ss->snapshot_time = ktime_get_real(); 227 ss->boot_time = ktime_get_boottime(); 228 229 if (q->vm && q->vm->xef) 230 process_name = q->vm->xef->process_name; 231 strscpy(ss->process_name, process_name); 232 233 ss->gt = q->gt; 234 INIT_WORK(&ss->work, xe_devcoredump_deferred_snap_work); 235 236 cookie = dma_fence_begin_signalling(); 237 for (i = 0; q->width > 1 && i < XE_HW_ENGINE_MAX_INSTANCE;) { 238 if (adj_logical_mask & BIT(i)) { 239 adj_logical_mask |= width_mask << i; 240 i += q->width; 241 } else { 242 ++i; 243 } 244 } 245 246 /* keep going if fw fails as we still want to save the memory and SW data */ 247 if (xe_force_wake_get(gt_to_fw(q->gt), XE_FORCEWAKE_ALL)) 248 xe_gt_info(ss->gt, "failed to get forcewake for coredump capture\n"); 249 250 coredump->snapshot.ct = xe_guc_ct_snapshot_capture(&guc->ct, true); 251 coredump->snapshot.ge = xe_guc_exec_queue_snapshot_capture(q); 252 coredump->snapshot.job = xe_sched_job_snapshot_capture(job); 253 coredump->snapshot.vm = xe_vm_snapshot_capture(q->vm); 254 255 for_each_hw_engine(hwe, q->gt, id) { 256 if (hwe->class != q->hwe->class || 257 !(BIT(hwe->logical_instance) & adj_logical_mask)) { 258 coredump->snapshot.hwe[id] = NULL; 259 continue; 260 } 261 coredump->snapshot.hwe[id] = xe_hw_engine_snapshot_capture(hwe); 262 } 263 264 queue_work(system_unbound_wq, &ss->work); 265 266 xe_force_wake_put(gt_to_fw(q->gt), XE_FORCEWAKE_ALL); 267 dma_fence_end_signalling(cookie); 268 } 269 270 /** 271 * xe_devcoredump - Take the required snapshots and initialize coredump device. 272 * @job: The faulty xe_sched_job, where the issue was detected. 273 * 274 * This function should be called at the crash time within the serialized 275 * gt_reset. It is skipped if we still have the core dump device available 276 * with the information of the 'first' snapshot. 277 */ 278 void xe_devcoredump(struct xe_sched_job *job) 279 { 280 struct xe_device *xe = gt_to_xe(job->q->gt); 281 struct xe_devcoredump *coredump = &xe->devcoredump; 282 283 if (coredump->captured) { 284 drm_dbg(&xe->drm, "Multiple hangs are occurring, but only the first snapshot was taken\n"); 285 return; 286 } 287 288 coredump->captured = true; 289 devcoredump_snapshot(coredump, job); 290 291 drm_info(&xe->drm, "Xe device coredump has been created\n"); 292 drm_info(&xe->drm, "Check your /sys/class/drm/card%d/device/devcoredump/data\n", 293 xe->drm.primary->index); 294 295 dev_coredumpm_timeout(xe->drm.dev, THIS_MODULE, coredump, 0, GFP_KERNEL, 296 xe_devcoredump_read, xe_devcoredump_free, 297 XE_COREDUMP_TIMEOUT_JIFFIES); 298 } 299 300 static void xe_driver_devcoredump_fini(void *arg) 301 { 302 struct drm_device *drm = arg; 303 304 dev_coredump_put(drm->dev); 305 } 306 307 int xe_devcoredump_init(struct xe_device *xe) 308 { 309 return devm_add_action_or_reset(xe->drm.dev, xe_driver_devcoredump_fini, &xe->drm); 310 } 311 312 #endif 313