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 "xe_device.h" 13 #include "xe_exec_queue.h" 14 #include "xe_force_wake.h" 15 #include "xe_gt.h" 16 #include "xe_guc_ct.h" 17 #include "xe_guc_submit.h" 18 #include "xe_hw_engine.h" 19 20 /** 21 * DOC: Xe device coredump 22 * 23 * Devices overview: 24 * Xe uses dev_coredump infrastructure for exposing the crash errors in a 25 * standardized way. 26 * devcoredump exposes a temporary device under /sys/class/devcoredump/ 27 * which is linked with our card device directly. 28 * The core dump can be accessed either from 29 * /sys/class/drm/card<n>/device/devcoredump/ or from 30 * /sys/class/devcoredump/devcd<m> where 31 * /sys/class/devcoredump/devcd<m>/failing_device is a link to 32 * /sys/class/drm/card<n>/device/. 33 * 34 * Snapshot at hang: 35 * The 'data' file is printed with a drm_printer pointer at devcoredump read 36 * time. For this reason, we need to take snapshots from when the hang has 37 * happened, and not only when the user is reading the file. Otherwise the 38 * information is outdated since the resets might have happened in between. 39 * 40 * 'First' failure snapshot: 41 * In general, the first hang is the most critical one since the following hangs 42 * can be a consequence of the initial hang. For this reason we only take the 43 * snapshot of the 'first' failure and ignore subsequent calls of this function, 44 * at least while the coredump device is alive. Dev_coredump has a delayed work 45 * queue that will eventually delete the device and free all the dump 46 * information. 47 */ 48 49 #ifdef CONFIG_DEV_COREDUMP 50 51 static struct xe_device *coredump_to_xe(const struct xe_devcoredump *coredump) 52 { 53 return container_of(coredump, struct xe_device, devcoredump); 54 } 55 56 static struct xe_guc *exec_queue_to_guc(struct xe_exec_queue *q) 57 { 58 return &q->gt->uc.guc; 59 } 60 61 static ssize_t xe_devcoredump_read(char *buffer, loff_t offset, 62 size_t count, void *data, size_t datalen) 63 { 64 struct xe_devcoredump *coredump = data; 65 struct xe_devcoredump_snapshot *ss; 66 struct drm_printer p; 67 struct drm_print_iterator iter; 68 struct timespec64 ts; 69 int i; 70 71 /* Our device is gone already... */ 72 if (!data || !coredump_to_xe(coredump)) 73 return -ENODEV; 74 75 iter.data = buffer; 76 iter.offset = 0; 77 iter.start = offset; 78 iter.remain = count; 79 80 ss = &coredump->snapshot; 81 p = drm_coredump_printer(&iter); 82 83 drm_printf(&p, "**** Xe Device Coredump ****\n"); 84 drm_printf(&p, "kernel: " UTS_RELEASE "\n"); 85 drm_printf(&p, "module: " KBUILD_MODNAME "\n"); 86 87 ts = ktime_to_timespec64(ss->snapshot_time); 88 drm_printf(&p, "Snapshot time: %lld.%09ld\n", ts.tv_sec, ts.tv_nsec); 89 ts = ktime_to_timespec64(ss->boot_time); 90 drm_printf(&p, "Uptime: %lld.%09ld\n", ts.tv_sec, ts.tv_nsec); 91 92 drm_printf(&p, "\n**** GuC CT ****\n"); 93 xe_guc_ct_snapshot_print(coredump->snapshot.ct, &p); 94 xe_guc_exec_queue_snapshot_print(coredump->snapshot.ge, &p); 95 96 drm_printf(&p, "\n**** HW Engines ****\n"); 97 for (i = 0; i < XE_NUM_HW_ENGINES; i++) 98 if (coredump->snapshot.hwe[i]) 99 xe_hw_engine_snapshot_print(coredump->snapshot.hwe[i], 100 &p); 101 102 return count - iter.remain; 103 } 104 105 static void xe_devcoredump_free(void *data) 106 { 107 struct xe_devcoredump *coredump = data; 108 int i; 109 110 /* Our device is gone. Nothing to do... */ 111 if (!data || !coredump_to_xe(coredump)) 112 return; 113 114 xe_guc_ct_snapshot_free(coredump->snapshot.ct); 115 xe_guc_exec_queue_snapshot_free(coredump->snapshot.ge); 116 for (i = 0; i < XE_NUM_HW_ENGINES; i++) 117 if (coredump->snapshot.hwe[i]) 118 xe_hw_engine_snapshot_free(coredump->snapshot.hwe[i]); 119 120 coredump->captured = false; 121 drm_info(&coredump_to_xe(coredump)->drm, 122 "Xe device coredump has been deleted.\n"); 123 } 124 125 static void devcoredump_snapshot(struct xe_devcoredump *coredump, 126 struct xe_exec_queue *q) 127 { 128 struct xe_devcoredump_snapshot *ss = &coredump->snapshot; 129 struct xe_guc *guc = exec_queue_to_guc(q); 130 struct xe_hw_engine *hwe; 131 enum xe_hw_engine_id id; 132 u32 adj_logical_mask = q->logical_mask; 133 u32 width_mask = (0x1 << q->width) - 1; 134 int i; 135 bool cookie; 136 137 ss->snapshot_time = ktime_get_real(); 138 ss->boot_time = ktime_get_boottime(); 139 140 cookie = dma_fence_begin_signalling(); 141 for (i = 0; q->width > 1 && i < XE_HW_ENGINE_MAX_INSTANCE;) { 142 if (adj_logical_mask & BIT(i)) { 143 adj_logical_mask |= width_mask << i; 144 i += q->width; 145 } else { 146 ++i; 147 } 148 } 149 150 xe_force_wake_get(gt_to_fw(q->gt), XE_FORCEWAKE_ALL); 151 152 coredump->snapshot.ct = xe_guc_ct_snapshot_capture(&guc->ct, true); 153 coredump->snapshot.ge = xe_guc_exec_queue_snapshot_capture(q); 154 155 for_each_hw_engine(hwe, q->gt, id) { 156 if (hwe->class != q->hwe->class || 157 !(BIT(hwe->logical_instance) & adj_logical_mask)) { 158 coredump->snapshot.hwe[id] = NULL; 159 continue; 160 } 161 coredump->snapshot.hwe[id] = xe_hw_engine_snapshot_capture(hwe); 162 } 163 164 xe_force_wake_put(gt_to_fw(q->gt), XE_FORCEWAKE_ALL); 165 dma_fence_end_signalling(cookie); 166 } 167 168 /** 169 * xe_devcoredump - Take the required snapshots and initialize coredump device. 170 * @q: The faulty xe_exec_queue, where the issue was detected. 171 * 172 * This function should be called at the crash time within the serialized 173 * gt_reset. It is skipped if we still have the core dump device available 174 * with the information of the 'first' snapshot. 175 */ 176 void xe_devcoredump(struct xe_exec_queue *q) 177 { 178 struct xe_device *xe = gt_to_xe(q->gt); 179 struct xe_devcoredump *coredump = &xe->devcoredump; 180 181 if (coredump->captured) { 182 drm_dbg(&xe->drm, "Multiple hangs are occurring, but only the first snapshot was taken\n"); 183 return; 184 } 185 186 coredump->captured = true; 187 devcoredump_snapshot(coredump, q); 188 189 drm_info(&xe->drm, "Xe device coredump has been created\n"); 190 drm_info(&xe->drm, "Check your /sys/class/drm/card%d/device/devcoredump/data\n", 191 xe->drm.primary->index); 192 193 dev_coredumpm(xe->drm.dev, THIS_MODULE, coredump, 0, GFP_KERNEL, 194 xe_devcoredump_read, xe_devcoredump_free); 195 } 196 #endif 197