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 static struct xe_device *coredump_to_xe(const struct xe_devcoredump *coredump) 57 { 58 return container_of(coredump, struct xe_device, devcoredump); 59 } 60 61 static struct xe_guc *exec_queue_to_guc(struct xe_exec_queue *q) 62 { 63 return &q->gt->uc.guc; 64 } 65 66 static void xe_devcoredump_deferred_snap_work(struct work_struct *work) 67 { 68 struct xe_devcoredump_snapshot *ss = container_of(work, typeof(*ss), work); 69 70 /* keep going if fw fails as we still want to save the memory and SW data */ 71 if (xe_force_wake_get(gt_to_fw(ss->gt), XE_FORCEWAKE_ALL)) 72 xe_gt_info(ss->gt, "failed to get forcewake for coredump capture\n"); 73 xe_vm_snapshot_capture_delayed(ss->vm); 74 xe_guc_exec_queue_snapshot_capture_delayed(ss->ge); 75 xe_force_wake_put(gt_to_fw(ss->gt), XE_FORCEWAKE_ALL); 76 } 77 78 static ssize_t xe_devcoredump_read(char *buffer, loff_t offset, 79 size_t count, void *data, size_t datalen) 80 { 81 struct xe_devcoredump *coredump = data; 82 struct xe_device *xe; 83 struct xe_devcoredump_snapshot *ss; 84 struct drm_printer p; 85 struct drm_print_iterator iter; 86 struct timespec64 ts; 87 int i; 88 89 if (!coredump) 90 return -ENODEV; 91 92 xe = coredump_to_xe(coredump); 93 ss = &coredump->snapshot; 94 95 /* Ensure delayed work is captured before continuing */ 96 flush_work(&ss->work); 97 98 iter.data = buffer; 99 iter.offset = 0; 100 iter.start = offset; 101 iter.remain = count; 102 103 p = drm_coredump_printer(&iter); 104 105 drm_printf(&p, "**** Xe Device Coredump ****\n"); 106 drm_printf(&p, "kernel: " UTS_RELEASE "\n"); 107 drm_printf(&p, "module: " KBUILD_MODNAME "\n"); 108 109 ts = ktime_to_timespec64(ss->snapshot_time); 110 drm_printf(&p, "Snapshot time: %lld.%09ld\n", ts.tv_sec, ts.tv_nsec); 111 ts = ktime_to_timespec64(ss->boot_time); 112 drm_printf(&p, "Uptime: %lld.%09ld\n", ts.tv_sec, ts.tv_nsec); 113 drm_printf(&p, "Process: %s\n", ss->process_name); 114 xe_device_snapshot_print(xe, &p); 115 116 drm_printf(&p, "\n**** GuC CT ****\n"); 117 xe_guc_ct_snapshot_print(coredump->snapshot.ct, &p); 118 xe_guc_exec_queue_snapshot_print(coredump->snapshot.ge, &p); 119 120 drm_printf(&p, "\n**** Job ****\n"); 121 xe_sched_job_snapshot_print(coredump->snapshot.job, &p); 122 123 drm_printf(&p, "\n**** HW Engines ****\n"); 124 for (i = 0; i < XE_NUM_HW_ENGINES; i++) 125 if (coredump->snapshot.hwe[i]) 126 xe_hw_engine_snapshot_print(coredump->snapshot.hwe[i], 127 &p); 128 drm_printf(&p, "\n**** VM state ****\n"); 129 xe_vm_snapshot_print(coredump->snapshot.vm, &p); 130 131 return count - iter.remain; 132 } 133 134 static void xe_devcoredump_free(void *data) 135 { 136 struct xe_devcoredump *coredump = data; 137 int i; 138 139 /* Our device is gone. Nothing to do... */ 140 if (!data || !coredump_to_xe(coredump)) 141 return; 142 143 cancel_work_sync(&coredump->snapshot.work); 144 145 xe_guc_ct_snapshot_free(coredump->snapshot.ct); 146 xe_guc_exec_queue_snapshot_free(coredump->snapshot.ge); 147 xe_sched_job_snapshot_free(coredump->snapshot.job); 148 for (i = 0; i < XE_NUM_HW_ENGINES; i++) 149 if (coredump->snapshot.hwe[i]) 150 xe_hw_engine_snapshot_free(coredump->snapshot.hwe[i]); 151 xe_vm_snapshot_free(coredump->snapshot.vm); 152 153 /* To prevent stale data on next snapshot, clear everything */ 154 memset(&coredump->snapshot, 0, sizeof(coredump->snapshot)); 155 coredump->captured = false; 156 drm_info(&coredump_to_xe(coredump)->drm, 157 "Xe device coredump has been deleted.\n"); 158 } 159 160 static void devcoredump_snapshot(struct xe_devcoredump *coredump, 161 struct xe_sched_job *job) 162 { 163 struct xe_devcoredump_snapshot *ss = &coredump->snapshot; 164 struct xe_exec_queue *q = job->q; 165 struct xe_guc *guc = exec_queue_to_guc(q); 166 struct xe_hw_engine *hwe; 167 enum xe_hw_engine_id id; 168 u32 adj_logical_mask = q->logical_mask; 169 u32 width_mask = (0x1 << q->width) - 1; 170 const char *process_name = "no process"; 171 struct task_struct *task = NULL; 172 173 int i; 174 bool cookie; 175 176 ss->snapshot_time = ktime_get_real(); 177 ss->boot_time = ktime_get_boottime(); 178 179 if (q->vm && q->vm->xef) { 180 task = get_pid_task(q->vm->xef->drm->pid, PIDTYPE_PID); 181 if (task) 182 process_name = task->comm; 183 } 184 strscpy(ss->process_name, process_name); 185 if (task) 186 put_task_struct(task); 187 188 ss->gt = q->gt; 189 INIT_WORK(&ss->work, xe_devcoredump_deferred_snap_work); 190 191 cookie = dma_fence_begin_signalling(); 192 for (i = 0; q->width > 1 && i < XE_HW_ENGINE_MAX_INSTANCE;) { 193 if (adj_logical_mask & BIT(i)) { 194 adj_logical_mask |= width_mask << i; 195 i += q->width; 196 } else { 197 ++i; 198 } 199 } 200 201 /* keep going if fw fails as we still want to save the memory and SW data */ 202 if (xe_force_wake_get(gt_to_fw(q->gt), XE_FORCEWAKE_ALL)) 203 xe_gt_info(ss->gt, "failed to get forcewake for coredump capture\n"); 204 205 coredump->snapshot.ct = xe_guc_ct_snapshot_capture(&guc->ct, true); 206 coredump->snapshot.ge = xe_guc_exec_queue_snapshot_capture(q); 207 coredump->snapshot.job = xe_sched_job_snapshot_capture(job); 208 coredump->snapshot.vm = xe_vm_snapshot_capture(q->vm); 209 210 for_each_hw_engine(hwe, q->gt, id) { 211 if (hwe->class != q->hwe->class || 212 !(BIT(hwe->logical_instance) & adj_logical_mask)) { 213 coredump->snapshot.hwe[id] = NULL; 214 continue; 215 } 216 coredump->snapshot.hwe[id] = xe_hw_engine_snapshot_capture(hwe); 217 } 218 219 queue_work(system_unbound_wq, &ss->work); 220 221 xe_force_wake_put(gt_to_fw(q->gt), XE_FORCEWAKE_ALL); 222 dma_fence_end_signalling(cookie); 223 } 224 225 /** 226 * xe_devcoredump - Take the required snapshots and initialize coredump device. 227 * @job: The faulty xe_sched_job, where the issue was detected. 228 * 229 * This function should be called at the crash time within the serialized 230 * gt_reset. It is skipped if we still have the core dump device available 231 * with the information of the 'first' snapshot. 232 */ 233 void xe_devcoredump(struct xe_sched_job *job) 234 { 235 struct xe_device *xe = gt_to_xe(job->q->gt); 236 struct xe_devcoredump *coredump = &xe->devcoredump; 237 238 if (coredump->captured) { 239 drm_dbg(&xe->drm, "Multiple hangs are occurring, but only the first snapshot was taken\n"); 240 return; 241 } 242 243 coredump->captured = true; 244 devcoredump_snapshot(coredump, job); 245 246 drm_info(&xe->drm, "Xe device coredump has been created\n"); 247 drm_info(&xe->drm, "Check your /sys/class/drm/card%d/device/devcoredump/data\n", 248 xe->drm.primary->index); 249 250 dev_coredumpm(xe->drm.dev, THIS_MODULE, coredump, 0, GFP_KERNEL, 251 xe_devcoredump_read, xe_devcoredump_free); 252 } 253 254 static void xe_driver_devcoredump_fini(void *arg) 255 { 256 struct drm_device *drm = arg; 257 258 dev_coredump_put(drm->dev); 259 } 260 261 int xe_devcoredump_init(struct xe_device *xe) 262 { 263 return devm_add_action_or_reset(xe->drm.dev, xe_driver_devcoredump_fini, &xe->drm); 264 } 265 #endif 266