xref: /linux/drivers/gpu/drm/xe/xe_guc_log.c (revision 3ef7acec975bde28ab9cef92af76be8fc2ce684d)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2022 Intel Corporation
4  */
5 
6 #include "xe_guc_log.h"
7 
8 #include <linux/fault-inject.h>
9 
10 #include <drm/drm_managed.h>
11 
12 #include "regs/xe_guc_regs.h"
13 #include "xe_bo.h"
14 #include "xe_devcoredump.h"
15 #include "xe_force_wake.h"
16 #include "xe_gt.h"
17 #include "xe_gt_printk.h"
18 #include "xe_map.h"
19 #include "xe_mmio.h"
20 #include "xe_module.h"
21 
22 static struct xe_guc *
log_to_guc(struct xe_guc_log * log)23 log_to_guc(struct xe_guc_log *log)
24 {
25 	return container_of(log, struct xe_guc, log);
26 }
27 
28 static struct xe_gt *
log_to_gt(struct xe_guc_log * log)29 log_to_gt(struct xe_guc_log *log)
30 {
31 	return container_of(log, struct xe_gt, uc.guc.log);
32 }
33 
34 static struct xe_device *
log_to_xe(struct xe_guc_log * log)35 log_to_xe(struct xe_guc_log *log)
36 {
37 	return gt_to_xe(log_to_gt(log));
38 }
39 
guc_log_size(void)40 static size_t guc_log_size(void)
41 {
42 	/*
43 	 *  GuC Log buffer Layout
44 	 *
45 	 *  +===============================+ 00B
46 	 *  |    Crash dump state header    |
47 	 *  +-------------------------------+ 32B
48 	 *  |      Debug state header       |
49 	 *  +-------------------------------+ 64B
50 	 *  |     Capture state header      |
51 	 *  +-------------------------------+ 96B
52 	 *  |                               |
53 	 *  +===============================+ PAGE_SIZE (4KB)
54 	 *  |        Crash Dump logs        |
55 	 *  +===============================+ + CRASH_SIZE
56 	 *  |          Debug logs           |
57 	 *  +===============================+ + DEBUG_SIZE
58 	 *  |         Capture logs          |
59 	 *  +===============================+ + CAPTURE_SIZE
60 	 */
61 	return PAGE_SIZE + CRASH_BUFFER_SIZE + DEBUG_BUFFER_SIZE +
62 		CAPTURE_BUFFER_SIZE;
63 }
64 
65 #define GUC_LOG_CHUNK_SIZE	SZ_2M
66 
xe_guc_log_snapshot_alloc(struct xe_guc_log * log,bool atomic)67 static struct xe_guc_log_snapshot *xe_guc_log_snapshot_alloc(struct xe_guc_log *log, bool atomic)
68 {
69 	struct xe_guc_log_snapshot *snapshot;
70 	size_t remain;
71 	int i;
72 
73 	snapshot = kzalloc(sizeof(*snapshot), atomic ? GFP_ATOMIC : GFP_KERNEL);
74 	if (!snapshot)
75 		return NULL;
76 
77 	/*
78 	 * NB: kmalloc has a hard limit well below the maximum GuC log buffer size.
79 	 * Also, can't use vmalloc as might be called from atomic context. So need
80 	 * to break the buffer up into smaller chunks that can be allocated.
81 	 */
82 	snapshot->size = log->bo->size;
83 	snapshot->num_chunks = DIV_ROUND_UP(snapshot->size, GUC_LOG_CHUNK_SIZE);
84 
85 	snapshot->copy = kcalloc(snapshot->num_chunks, sizeof(*snapshot->copy),
86 				 atomic ? GFP_ATOMIC : GFP_KERNEL);
87 	if (!snapshot->copy)
88 		goto fail_snap;
89 
90 	remain = snapshot->size;
91 	for (i = 0; i < snapshot->num_chunks; i++) {
92 		size_t size = min(GUC_LOG_CHUNK_SIZE, remain);
93 
94 		snapshot->copy[i] = kmalloc(size, atomic ? GFP_ATOMIC : GFP_KERNEL);
95 		if (!snapshot->copy[i])
96 			goto fail_copy;
97 		remain -= size;
98 	}
99 
100 	return snapshot;
101 
102 fail_copy:
103 	for (i = 0; i < snapshot->num_chunks; i++)
104 		kfree(snapshot->copy[i]);
105 	kfree(snapshot->copy);
106 fail_snap:
107 	kfree(snapshot);
108 	return NULL;
109 }
110 
111 /**
112  * xe_guc_log_snapshot_free - free a previously captured GuC log snapshot
113  * @snapshot: GuC log snapshot structure
114  *
115  * Return: pointer to a newly allocated snapshot object or null if out of memory. Caller is
116  * responsible for calling xe_guc_log_snapshot_free when done with the snapshot.
117  */
xe_guc_log_snapshot_free(struct xe_guc_log_snapshot * snapshot)118 void xe_guc_log_snapshot_free(struct xe_guc_log_snapshot *snapshot)
119 {
120 	int i;
121 
122 	if (!snapshot)
123 		return;
124 
125 	if (snapshot->copy) {
126 		for (i = 0; i < snapshot->num_chunks; i++)
127 			kfree(snapshot->copy[i]);
128 		kfree(snapshot->copy);
129 	}
130 
131 	kfree(snapshot);
132 }
133 
134 /**
135  * xe_guc_log_snapshot_capture - create a new snapshot copy the GuC log for later dumping
136  * @log: GuC log structure
137  * @atomic: is the call inside an atomic section of some kind?
138  *
139  * Return: pointer to a newly allocated snapshot object or null if out of memory. Caller is
140  * responsible for calling xe_guc_log_snapshot_free when done with the snapshot.
141  */
xe_guc_log_snapshot_capture(struct xe_guc_log * log,bool atomic)142 struct xe_guc_log_snapshot *xe_guc_log_snapshot_capture(struct xe_guc_log *log, bool atomic)
143 {
144 	struct xe_guc_log_snapshot *snapshot;
145 	struct xe_device *xe = log_to_xe(log);
146 	struct xe_guc *guc = log_to_guc(log);
147 	struct xe_gt *gt = log_to_gt(log);
148 	unsigned int fw_ref;
149 	size_t remain;
150 	int i;
151 
152 	if (!log->bo) {
153 		xe_gt_err(gt, "GuC log buffer not allocated\n");
154 		return NULL;
155 	}
156 
157 	snapshot = xe_guc_log_snapshot_alloc(log, atomic);
158 	if (!snapshot) {
159 		xe_gt_err(gt, "GuC log snapshot not allocated\n");
160 		return NULL;
161 	}
162 
163 	remain = snapshot->size;
164 	for (i = 0; i < snapshot->num_chunks; i++) {
165 		size_t size = min(GUC_LOG_CHUNK_SIZE, remain);
166 
167 		xe_map_memcpy_from(xe, snapshot->copy[i], &log->bo->vmap,
168 				   i * GUC_LOG_CHUNK_SIZE, size);
169 		remain -= size;
170 	}
171 
172 	fw_ref = xe_force_wake_get(gt_to_fw(gt), XE_FW_GT);
173 	if (!fw_ref) {
174 		snapshot->stamp = ~0ULL;
175 	} else {
176 		snapshot->stamp = xe_mmio_read64_2x32(&gt->mmio, GUC_PMTIMESTAMP_LO);
177 		xe_force_wake_put(gt_to_fw(gt), fw_ref);
178 	}
179 	snapshot->ktime = ktime_get_boottime_ns();
180 	snapshot->level = log->level;
181 	snapshot->ver_found = guc->fw.versions.found[XE_UC_FW_VER_RELEASE];
182 	snapshot->ver_want = guc->fw.versions.wanted;
183 	snapshot->path = guc->fw.path;
184 
185 	return snapshot;
186 }
187 
188 /**
189  * xe_guc_log_snapshot_print - dump a previously saved copy of the GuC log to some useful location
190  * @snapshot: a snapshot of the GuC log
191  * @p: the printer object to output to
192  */
xe_guc_log_snapshot_print(struct xe_guc_log_snapshot * snapshot,struct drm_printer * p)193 void xe_guc_log_snapshot_print(struct xe_guc_log_snapshot *snapshot, struct drm_printer *p)
194 {
195 	size_t remain;
196 	int i;
197 
198 	if (!snapshot) {
199 		drm_printf(p, "GuC log snapshot not allocated!\n");
200 		return;
201 	}
202 
203 	drm_printf(p, "GuC firmware: %s\n", snapshot->path);
204 	drm_printf(p, "GuC version: %u.%u.%u (wanted %u.%u.%u)\n",
205 		   snapshot->ver_found.major, snapshot->ver_found.minor, snapshot->ver_found.patch,
206 		   snapshot->ver_want.major, snapshot->ver_want.minor, snapshot->ver_want.patch);
207 	drm_printf(p, "Kernel timestamp: 0x%08llX [%llu]\n", snapshot->ktime, snapshot->ktime);
208 	drm_printf(p, "GuC timestamp: 0x%08llX [%llu]\n", snapshot->stamp, snapshot->stamp);
209 	drm_printf(p, "Log level: %u\n", snapshot->level);
210 
211 	drm_printf(p, "[LOG].length: 0x%zx\n", snapshot->size);
212 	remain = snapshot->size;
213 	for (i = 0; i < snapshot->num_chunks; i++) {
214 		size_t size = min(GUC_LOG_CHUNK_SIZE, remain);
215 		const char *prefix = i ? NULL : "[LOG].data";
216 		char suffix = i == snapshot->num_chunks - 1 ? '\n' : 0;
217 
218 		xe_print_blob_ascii85(p, prefix, suffix, snapshot->copy[i], 0, size);
219 		remain -= size;
220 	}
221 }
222 
223 /**
224  * xe_guc_log_print_dmesg - dump a copy of the GuC log to dmesg
225  * @log: GuC log structure
226  */
xe_guc_log_print_dmesg(struct xe_guc_log * log)227 void xe_guc_log_print_dmesg(struct xe_guc_log *log)
228 {
229 	struct xe_gt *gt = log_to_gt(log);
230 	static int g_count;
231 	struct drm_printer ip = xe_gt_info_printer(gt);
232 	struct drm_printer lp = drm_line_printer(&ip, "Capture", ++g_count);
233 
234 	drm_printf(&lp, "Dumping GuC log for %ps...\n", __builtin_return_address(0));
235 
236 	xe_guc_log_print(log, &lp);
237 
238 	drm_printf(&lp, "Done.\n");
239 }
240 
241 /**
242  * xe_guc_log_print - dump a copy of the GuC log to some useful location
243  * @log: GuC log structure
244  * @p: the printer object to output to
245  */
xe_guc_log_print(struct xe_guc_log * log,struct drm_printer * p)246 void xe_guc_log_print(struct xe_guc_log *log, struct drm_printer *p)
247 {
248 	struct xe_guc_log_snapshot *snapshot;
249 
250 	drm_printf(p, "**** GuC Log ****\n");
251 
252 	snapshot = xe_guc_log_snapshot_capture(log, false);
253 	drm_printf(p, "CS reference clock: %u\n", log_to_gt(log)->info.reference_clock);
254 	xe_guc_log_snapshot_print(snapshot, p);
255 	xe_guc_log_snapshot_free(snapshot);
256 }
257 
xe_guc_log_init(struct xe_guc_log * log)258 int xe_guc_log_init(struct xe_guc_log *log)
259 {
260 	struct xe_device *xe = log_to_xe(log);
261 	struct xe_tile *tile = gt_to_tile(log_to_gt(log));
262 	struct xe_bo *bo;
263 
264 	bo = xe_managed_bo_create_pin_map(xe, tile, guc_log_size(),
265 					  XE_BO_FLAG_SYSTEM |
266 					  XE_BO_FLAG_GGTT |
267 					  XE_BO_FLAG_GGTT_INVALIDATE);
268 	if (IS_ERR(bo))
269 		return PTR_ERR(bo);
270 
271 	xe_map_memset(xe, &bo->vmap, 0, 0, guc_log_size());
272 	log->bo = bo;
273 	log->level = xe_modparam.guc_log_level;
274 
275 	return 0;
276 }
277 
278 ALLOW_ERROR_INJECTION(xe_guc_log_init, ERRNO); /* See xe_pci_probe() */
279 
xe_guc_log_section_size_crash(struct xe_guc_log * log)280 static u32 xe_guc_log_section_size_crash(struct xe_guc_log *log)
281 {
282 	return CRASH_BUFFER_SIZE;
283 }
284 
xe_guc_log_section_size_debug(struct xe_guc_log * log)285 static u32 xe_guc_log_section_size_debug(struct xe_guc_log *log)
286 {
287 	return DEBUG_BUFFER_SIZE;
288 }
289 
290 /**
291  * xe_guc_log_section_size_capture - Get capture buffer size within log sections.
292  * @log: The log object.
293  *
294  * This function will return the capture buffer size within log sections.
295  *
296  * Return: capture buffer size.
297  */
xe_guc_log_section_size_capture(struct xe_guc_log * log)298 u32 xe_guc_log_section_size_capture(struct xe_guc_log *log)
299 {
300 	return CAPTURE_BUFFER_SIZE;
301 }
302 
303 /**
304  * xe_guc_get_log_buffer_size - Get log buffer size for a type.
305  * @log: The log object.
306  * @type: The log buffer type
307  *
308  * Return: buffer size.
309  */
xe_guc_get_log_buffer_size(struct xe_guc_log * log,enum guc_log_buffer_type type)310 u32 xe_guc_get_log_buffer_size(struct xe_guc_log *log, enum guc_log_buffer_type type)
311 {
312 	switch (type) {
313 	case GUC_LOG_BUFFER_CRASH_DUMP:
314 		return xe_guc_log_section_size_crash(log);
315 	case GUC_LOG_BUFFER_DEBUG:
316 		return xe_guc_log_section_size_debug(log);
317 	case GUC_LOG_BUFFER_CAPTURE:
318 		return xe_guc_log_section_size_capture(log);
319 	}
320 	return 0;
321 }
322 
323 /**
324  * xe_guc_get_log_buffer_offset - Get offset in log buffer for a type.
325  * @log: The log object.
326  * @type: The log buffer type
327  *
328  * This function will return the offset in the log buffer for a type.
329  * Return: buffer offset.
330  */
xe_guc_get_log_buffer_offset(struct xe_guc_log * log,enum guc_log_buffer_type type)331 u32 xe_guc_get_log_buffer_offset(struct xe_guc_log *log, enum guc_log_buffer_type type)
332 {
333 	enum guc_log_buffer_type i;
334 	u32 offset = PAGE_SIZE;/* for the log_buffer_states */
335 
336 	for (i = GUC_LOG_BUFFER_CRASH_DUMP; i < GUC_LOG_BUFFER_TYPE_MAX; ++i) {
337 		if (i == type)
338 			break;
339 		offset += xe_guc_get_log_buffer_size(log, i);
340 	}
341 
342 	return offset;
343 }
344 
345 /**
346  * xe_guc_check_log_buf_overflow - Check if log buffer overflowed
347  * @log: The log object.
348  * @type: The log buffer type
349  * @full_cnt: The count of buffer full
350  *
351  * This function will check count of buffer full against previous, mismatch
352  * indicate overflowed.
353  * Update the sampled_overflow counter, if the 4 bit counter overflowed, add
354  * up 16 to correct the value.
355  *
356  * Return: True if overflowed.
357  */
xe_guc_check_log_buf_overflow(struct xe_guc_log * log,enum guc_log_buffer_type type,unsigned int full_cnt)358 bool xe_guc_check_log_buf_overflow(struct xe_guc_log *log, enum guc_log_buffer_type type,
359 				   unsigned int full_cnt)
360 {
361 	unsigned int prev_full_cnt = log->stats[type].sampled_overflow;
362 	bool overflow = false;
363 
364 	if (full_cnt != prev_full_cnt) {
365 		overflow = true;
366 
367 		log->stats[type].overflow = full_cnt;
368 		log->stats[type].sampled_overflow += full_cnt - prev_full_cnt;
369 
370 		if (full_cnt < prev_full_cnt) {
371 			/* buffer_full_cnt is a 4 bit counter */
372 			log->stats[type].sampled_overflow += 16;
373 		}
374 		xe_gt_notice(log_to_gt(log), "log buffer overflow\n");
375 	}
376 
377 	return overflow;
378 }
379