1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2021-2022 Intel Corporation
4  */
5 
6 #include <linux/types.h>
7 
8 #include <drm/drm_print.h>
9 
10 #include "gt/intel_engine_regs.h"
11 #include "gt/intel_gt.h"
12 #include "gt/intel_gt_mcr.h"
13 #include "gt/intel_gt_regs.h"
14 #include "gt/intel_lrc.h"
15 #include "guc_capture_fwif.h"
16 #include "intel_guc_capture.h"
17 #include "intel_guc_fwif.h"
18 #include "i915_drv.h"
19 #include "i915_gpu_error.h"
20 #include "i915_irq.h"
21 #include "i915_memcpy.h"
22 #include "i915_reg.h"
23 
24 /*
25  * Define all device tables of GuC error capture register lists
26  * NOTE: For engine-registers, GuC only needs the register offsets
27  *       from the engine-mmio-base
28  */
29 #define COMMON_BASE_GLOBAL \
30 	{ FORCEWAKE_MT,             0,      0, "FORCEWAKE" }
31 
32 #define COMMON_GEN9BASE_GLOBAL \
33 	{ GEN8_FAULT_TLB_DATA0,     0,      0, "GEN8_FAULT_TLB_DATA0" }, \
34 	{ GEN8_FAULT_TLB_DATA1,     0,      0, "GEN8_FAULT_TLB_DATA1" }, \
35 	{ ERROR_GEN6,               0,      0, "ERROR_GEN6" }, \
36 	{ DONE_REG,                 0,      0, "DONE_REG" }, \
37 	{ HSW_GTT_CACHE_EN,         0,      0, "HSW_GTT_CACHE_EN" }
38 
39 #define COMMON_GEN12BASE_GLOBAL \
40 	{ GEN12_FAULT_TLB_DATA0,    0,      0, "GEN12_FAULT_TLB_DATA0" }, \
41 	{ GEN12_FAULT_TLB_DATA1,    0,      0, "GEN12_FAULT_TLB_DATA1" }, \
42 	{ GEN12_AUX_ERR_DBG,        0,      0, "AUX_ERR_DBG" }, \
43 	{ GEN12_GAM_DONE,           0,      0, "GAM_DONE" }, \
44 	{ GEN12_RING_FAULT_REG,     0,      0, "FAULT_REG" }
45 
46 #define COMMON_BASE_ENGINE_INSTANCE \
47 	{ RING_PSMI_CTL(0),         0,      0, "RC PSMI" }, \
48 	{ RING_ESR(0),              0,      0, "ESR" }, \
49 	{ RING_DMA_FADD(0),         0,      0, "RING_DMA_FADD_LDW" }, \
50 	{ RING_DMA_FADD_UDW(0),     0,      0, "RING_DMA_FADD_UDW" }, \
51 	{ RING_IPEIR(0),            0,      0, "IPEIR" }, \
52 	{ RING_IPEHR(0),            0,      0, "IPEHR" }, \
53 	{ RING_INSTPS(0),           0,      0, "INSTPS" }, \
54 	{ RING_BBADDR(0),           0,      0, "RING_BBADDR_LOW32" }, \
55 	{ RING_BBADDR_UDW(0),       0,      0, "RING_BBADDR_UP32" }, \
56 	{ RING_BBSTATE(0),          0,      0, "BB_STATE" }, \
57 	{ CCID(0),                  0,      0, "CCID" }, \
58 	{ RING_ACTHD(0),            0,      0, "ACTHD_LDW" }, \
59 	{ RING_ACTHD_UDW(0),        0,      0, "ACTHD_UDW" }, \
60 	{ RING_INSTPM(0),           0,      0, "INSTPM" }, \
61 	{ RING_INSTDONE(0),         0,      0, "INSTDONE" }, \
62 	{ RING_NOPID(0),            0,      0, "RING_NOPID" }, \
63 	{ RING_START(0),            0,      0, "START" }, \
64 	{ RING_HEAD(0),             0,      0, "HEAD" }, \
65 	{ RING_TAIL(0),             0,      0, "TAIL" }, \
66 	{ RING_CTL(0),              0,      0, "CTL" }, \
67 	{ RING_MI_MODE(0),          0,      0, "MODE" }, \
68 	{ RING_CONTEXT_CONTROL(0),  0,      0, "RING_CONTEXT_CONTROL" }, \
69 	{ RING_HWS_PGA(0),          0,      0, "HWS" }, \
70 	{ RING_MODE_GEN7(0),        0,      0, "GFX_MODE" }, \
71 	{ GEN8_RING_PDP_LDW(0, 0),  0,      0, "PDP0_LDW" }, \
72 	{ GEN8_RING_PDP_UDW(0, 0),  0,      0, "PDP0_UDW" }, \
73 	{ GEN8_RING_PDP_LDW(0, 1),  0,      0, "PDP1_LDW" }, \
74 	{ GEN8_RING_PDP_UDW(0, 1),  0,      0, "PDP1_UDW" }, \
75 	{ GEN8_RING_PDP_LDW(0, 2),  0,      0, "PDP2_LDW" }, \
76 	{ GEN8_RING_PDP_UDW(0, 2),  0,      0, "PDP2_UDW" }, \
77 	{ GEN8_RING_PDP_LDW(0, 3),  0,      0, "PDP3_LDW" }, \
78 	{ GEN8_RING_PDP_UDW(0, 3),  0,      0, "PDP3_UDW" }
79 
80 #define COMMON_BASE_HAS_EU \
81 	{ EIR,                      0,      0, "EIR" }
82 
83 #define COMMON_BASE_RENDER \
84 	{ GEN7_SC_INSTDONE,         0,      0, "GEN7_SC_INSTDONE" }
85 
86 #define COMMON_GEN12BASE_RENDER \
87 	{ GEN12_SC_INSTDONE_EXTRA,  0,      0, "GEN12_SC_INSTDONE_EXTRA" }, \
88 	{ GEN12_SC_INSTDONE_EXTRA2, 0,      0, "GEN12_SC_INSTDONE_EXTRA2" }
89 
90 #define COMMON_GEN12BASE_VEC \
91 	{ GEN12_SFC_DONE(0),        0,      0, "SFC_DONE[0]" }, \
92 	{ GEN12_SFC_DONE(1),        0,      0, "SFC_DONE[1]" }, \
93 	{ GEN12_SFC_DONE(2),        0,      0, "SFC_DONE[2]" }, \
94 	{ GEN12_SFC_DONE(3),        0,      0, "SFC_DONE[3]" }
95 
96 /* XE_LPD - Global */
97 static const struct __guc_mmio_reg_descr xe_lpd_global_regs[] = {
98 	COMMON_BASE_GLOBAL,
99 	COMMON_GEN9BASE_GLOBAL,
100 	COMMON_GEN12BASE_GLOBAL,
101 };
102 
103 /* XE_LPD - Render / Compute Per-Class */
104 static const struct __guc_mmio_reg_descr xe_lpd_rc_class_regs[] = {
105 	COMMON_BASE_HAS_EU,
106 	COMMON_BASE_RENDER,
107 	COMMON_GEN12BASE_RENDER,
108 };
109 
110 /* GEN9/XE_LPD - Render / Compute Per-Engine-Instance */
111 static const struct __guc_mmio_reg_descr xe_lpd_rc_inst_regs[] = {
112 	COMMON_BASE_ENGINE_INSTANCE,
113 };
114 
115 /* GEN9/XE_LPD - Media Decode/Encode Per-Engine-Instance */
116 static const struct __guc_mmio_reg_descr xe_lpd_vd_inst_regs[] = {
117 	COMMON_BASE_ENGINE_INSTANCE,
118 };
119 
120 /* XE_LPD - Video Enhancement Per-Class */
121 static const struct __guc_mmio_reg_descr xe_lpd_vec_class_regs[] = {
122 	COMMON_GEN12BASE_VEC,
123 };
124 
125 /* GEN9/XE_LPD - Video Enhancement Per-Engine-Instance */
126 static const struct __guc_mmio_reg_descr xe_lpd_vec_inst_regs[] = {
127 	COMMON_BASE_ENGINE_INSTANCE,
128 };
129 
130 /* GEN9/XE_LPD - Blitter Per-Engine-Instance */
131 static const struct __guc_mmio_reg_descr xe_lpd_blt_inst_regs[] = {
132 	COMMON_BASE_ENGINE_INSTANCE,
133 };
134 
135 /* GEN9 - Global */
136 static const struct __guc_mmio_reg_descr default_global_regs[] = {
137 	COMMON_BASE_GLOBAL,
138 	COMMON_GEN9BASE_GLOBAL,
139 };
140 
141 static const struct __guc_mmio_reg_descr default_rc_class_regs[] = {
142 	COMMON_BASE_HAS_EU,
143 	COMMON_BASE_RENDER,
144 };
145 
146 /*
147  * Empty lists:
148  * GEN9/XE_LPD - Blitter Per-Class
149  * GEN9/XE_LPD - Media Decode/Encode Per-Class
150  * GEN9 - VEC Class
151  */
152 static const struct __guc_mmio_reg_descr empty_regs_list[] = {
153 };
154 
155 #define TO_GCAP_DEF_OWNER(x) (GUC_CAPTURE_LIST_INDEX_##x)
156 #define TO_GCAP_DEF_TYPE(x) (GUC_CAPTURE_LIST_TYPE_##x)
157 #define MAKE_REGLIST(regslist, regsowner, regstype, class) \
158 	{ \
159 		regslist, \
160 		ARRAY_SIZE(regslist), \
161 		TO_GCAP_DEF_OWNER(regsowner), \
162 		TO_GCAP_DEF_TYPE(regstype), \
163 		class, \
164 		NULL, \
165 	}
166 
167 /* List of lists */
168 static const struct __guc_mmio_reg_descr_group default_lists[] = {
169 	MAKE_REGLIST(default_global_regs, PF, GLOBAL, 0),
170 	MAKE_REGLIST(default_rc_class_regs, PF, ENGINE_CLASS, GUC_RENDER_CLASS),
171 	MAKE_REGLIST(xe_lpd_rc_inst_regs, PF, ENGINE_INSTANCE, GUC_RENDER_CLASS),
172 	MAKE_REGLIST(empty_regs_list, PF, ENGINE_CLASS, GUC_VIDEO_CLASS),
173 	MAKE_REGLIST(xe_lpd_vd_inst_regs, PF, ENGINE_INSTANCE, GUC_VIDEO_CLASS),
174 	MAKE_REGLIST(empty_regs_list, PF, ENGINE_CLASS, GUC_VIDEOENHANCE_CLASS),
175 	MAKE_REGLIST(xe_lpd_vec_inst_regs, PF, ENGINE_INSTANCE, GUC_VIDEOENHANCE_CLASS),
176 	MAKE_REGLIST(empty_regs_list, PF, ENGINE_CLASS, GUC_BLITTER_CLASS),
177 	MAKE_REGLIST(xe_lpd_blt_inst_regs, PF, ENGINE_INSTANCE, GUC_BLITTER_CLASS),
178 	{}
179 };
180 
181 static const struct __guc_mmio_reg_descr_group xe_lpd_lists[] = {
182 	MAKE_REGLIST(xe_lpd_global_regs, PF, GLOBAL, 0),
183 	MAKE_REGLIST(xe_lpd_rc_class_regs, PF, ENGINE_CLASS, GUC_RENDER_CLASS),
184 	MAKE_REGLIST(xe_lpd_rc_inst_regs, PF, ENGINE_INSTANCE, GUC_RENDER_CLASS),
185 	MAKE_REGLIST(empty_regs_list, PF, ENGINE_CLASS, GUC_VIDEO_CLASS),
186 	MAKE_REGLIST(xe_lpd_vd_inst_regs, PF, ENGINE_INSTANCE, GUC_VIDEO_CLASS),
187 	MAKE_REGLIST(xe_lpd_vec_class_regs, PF, ENGINE_CLASS, GUC_VIDEOENHANCE_CLASS),
188 	MAKE_REGLIST(xe_lpd_vec_inst_regs, PF, ENGINE_INSTANCE, GUC_VIDEOENHANCE_CLASS),
189 	MAKE_REGLIST(empty_regs_list, PF, ENGINE_CLASS, GUC_BLITTER_CLASS),
190 	MAKE_REGLIST(xe_lpd_blt_inst_regs, PF, ENGINE_INSTANCE, GUC_BLITTER_CLASS),
191 	{}
192 };
193 
194 static const struct __guc_mmio_reg_descr_group *
guc_capture_get_one_list(const struct __guc_mmio_reg_descr_group * reglists,u32 owner,u32 type,u32 id)195 guc_capture_get_one_list(const struct __guc_mmio_reg_descr_group *reglists,
196 			 u32 owner, u32 type, u32 id)
197 {
198 	int i;
199 
200 	if (!reglists)
201 		return NULL;
202 
203 	for (i = 0; reglists[i].list; ++i) {
204 		if (reglists[i].owner == owner && reglists[i].type == type &&
205 		    (reglists[i].engine == id || reglists[i].type == GUC_CAPTURE_LIST_TYPE_GLOBAL))
206 			return &reglists[i];
207 	}
208 
209 	return NULL;
210 }
211 
212 static struct __guc_mmio_reg_descr_group *
guc_capture_get_one_ext_list(struct __guc_mmio_reg_descr_group * reglists,u32 owner,u32 type,u32 id)213 guc_capture_get_one_ext_list(struct __guc_mmio_reg_descr_group *reglists,
214 			     u32 owner, u32 type, u32 id)
215 {
216 	int i;
217 
218 	if (!reglists)
219 		return NULL;
220 
221 	for (i = 0; reglists[i].extlist; ++i) {
222 		if (reglists[i].owner == owner && reglists[i].type == type &&
223 		    (reglists[i].engine == id || reglists[i].type == GUC_CAPTURE_LIST_TYPE_GLOBAL))
224 			return &reglists[i];
225 	}
226 
227 	return NULL;
228 }
229 
guc_capture_free_extlists(struct __guc_mmio_reg_descr_group * reglists)230 static void guc_capture_free_extlists(struct __guc_mmio_reg_descr_group *reglists)
231 {
232 	int i = 0;
233 
234 	if (!reglists)
235 		return;
236 
237 	while (reglists[i].extlist)
238 		kfree(reglists[i++].extlist);
239 }
240 
241 struct __ext_steer_reg {
242 	const char *name;
243 	i915_reg_t reg;
244 };
245 
246 static const struct __ext_steer_reg xe_extregs[] = {
247 	{"GEN7_SAMPLER_INSTDONE", GEN7_SAMPLER_INSTDONE},
248 	{"GEN7_ROW_INSTDONE", GEN7_ROW_INSTDONE}
249 };
250 
__fill_ext_reg(struct __guc_mmio_reg_descr * ext,const struct __ext_steer_reg * extlist,int slice_id,int subslice_id)251 static void __fill_ext_reg(struct __guc_mmio_reg_descr *ext,
252 			   const struct __ext_steer_reg *extlist,
253 			   int slice_id, int subslice_id)
254 {
255 	ext->reg = extlist->reg;
256 	ext->flags = FIELD_PREP(GUC_REGSET_STEERING_GROUP, slice_id);
257 	ext->flags |= FIELD_PREP(GUC_REGSET_STEERING_INSTANCE, subslice_id);
258 	ext->regname = extlist->name;
259 }
260 
261 static int
__alloc_ext_regs(struct __guc_mmio_reg_descr_group * newlist,const struct __guc_mmio_reg_descr_group * rootlist,int num_regs)262 __alloc_ext_regs(struct __guc_mmio_reg_descr_group *newlist,
263 		 const struct __guc_mmio_reg_descr_group *rootlist, int num_regs)
264 {
265 	struct __guc_mmio_reg_descr *list;
266 
267 	list = kcalloc(num_regs, sizeof(struct __guc_mmio_reg_descr), GFP_KERNEL);
268 	if (!list)
269 		return -ENOMEM;
270 
271 	newlist->extlist = list;
272 	newlist->num_regs = num_regs;
273 	newlist->owner = rootlist->owner;
274 	newlist->engine = rootlist->engine;
275 	newlist->type = rootlist->type;
276 
277 	return 0;
278 }
279 
280 static void
guc_capture_alloc_steered_lists_xe_lpd(struct intel_guc * guc,const struct __guc_mmio_reg_descr_group * lists)281 guc_capture_alloc_steered_lists_xe_lpd(struct intel_guc *guc,
282 				       const struct __guc_mmio_reg_descr_group *lists)
283 {
284 	struct intel_gt *gt = guc_to_gt(guc);
285 	int slice, subslice, iter, i, num_steer_regs, num_tot_regs = 0;
286 	const struct __guc_mmio_reg_descr_group *list;
287 	struct __guc_mmio_reg_descr_group *extlists;
288 	struct __guc_mmio_reg_descr *extarray;
289 	struct sseu_dev_info *sseu;
290 
291 	/* In XE_LPD we only have steered registers for the render-class */
292 	list = guc_capture_get_one_list(lists, GUC_CAPTURE_LIST_INDEX_PF,
293 					GUC_CAPTURE_LIST_TYPE_ENGINE_CLASS, GUC_RENDER_CLASS);
294 	/* skip if extlists was previously allocated */
295 	if (!list || guc->capture->extlists)
296 		return;
297 
298 	num_steer_regs = ARRAY_SIZE(xe_extregs);
299 
300 	sseu = &gt->info.sseu;
301 	for_each_ss_steering(iter, gt, slice, subslice)
302 		num_tot_regs += num_steer_regs;
303 
304 	if (!num_tot_regs)
305 		return;
306 
307 	/* allocate an extra for an end marker */
308 	extlists = kcalloc(2, sizeof(struct __guc_mmio_reg_descr_group), GFP_KERNEL);
309 	if (!extlists)
310 		return;
311 
312 	if (__alloc_ext_regs(&extlists[0], list, num_tot_regs)) {
313 		kfree(extlists);
314 		return;
315 	}
316 
317 	extarray = extlists[0].extlist;
318 	for_each_ss_steering(iter, gt, slice, subslice) {
319 		for (i = 0; i < num_steer_regs; ++i) {
320 			__fill_ext_reg(extarray, &xe_extregs[i], slice, subslice);
321 			++extarray;
322 		}
323 	}
324 
325 	guc->capture->extlists = extlists;
326 }
327 
328 static const struct __ext_steer_reg xehpg_extregs[] = {
329 	{"XEHPG_INSTDONE_GEOM_SVG", XEHPG_INSTDONE_GEOM_SVG}
330 };
331 
__has_xehpg_extregs(u32 ipver)332 static bool __has_xehpg_extregs(u32 ipver)
333 {
334 	return (ipver >= IP_VER(12, 55));
335 }
336 
337 static void
guc_capture_alloc_steered_lists_xe_hpg(struct intel_guc * guc,const struct __guc_mmio_reg_descr_group * lists,u32 ipver)338 guc_capture_alloc_steered_lists_xe_hpg(struct intel_guc *guc,
339 				       const struct __guc_mmio_reg_descr_group *lists,
340 				       u32 ipver)
341 {
342 	struct intel_gt *gt = guc_to_gt(guc);
343 	struct drm_i915_private *i915 = guc_to_gt(guc)->i915;
344 	struct sseu_dev_info *sseu;
345 	int slice, subslice, i, iter, num_steer_regs, num_tot_regs = 0;
346 	const struct __guc_mmio_reg_descr_group *list;
347 	struct __guc_mmio_reg_descr_group *extlists;
348 	struct __guc_mmio_reg_descr *extarray;
349 
350 	/* In XE_LP / HPG we only have render-class steering registers during error-capture */
351 	list = guc_capture_get_one_list(lists, GUC_CAPTURE_LIST_INDEX_PF,
352 					GUC_CAPTURE_LIST_TYPE_ENGINE_CLASS, GUC_RENDER_CLASS);
353 	/* skip if extlists was previously allocated */
354 	if (!list || guc->capture->extlists)
355 		return;
356 
357 	num_steer_regs = ARRAY_SIZE(xe_extregs);
358 	if (__has_xehpg_extregs(ipver))
359 		num_steer_regs += ARRAY_SIZE(xehpg_extregs);
360 
361 	sseu = &gt->info.sseu;
362 	for_each_ss_steering(iter, gt, slice, subslice)
363 		num_tot_regs += num_steer_regs;
364 
365 	if (!num_tot_regs)
366 		return;
367 
368 	/* allocate an extra for an end marker */
369 	extlists = kcalloc(2, sizeof(struct __guc_mmio_reg_descr_group), GFP_KERNEL);
370 	if (!extlists)
371 		return;
372 
373 	if (__alloc_ext_regs(&extlists[0], list, num_tot_regs)) {
374 		kfree(extlists);
375 		return;
376 	}
377 
378 	extarray = extlists[0].extlist;
379 	for_each_ss_steering(iter, gt, slice, subslice) {
380 		for (i = 0; i < ARRAY_SIZE(xe_extregs); ++i) {
381 			__fill_ext_reg(extarray, &xe_extregs[i], slice, subslice);
382 			++extarray;
383 		}
384 		if (__has_xehpg_extregs(ipver)) {
385 			for (i = 0; i < ARRAY_SIZE(xehpg_extregs); ++i) {
386 				__fill_ext_reg(extarray, &xehpg_extregs[i], slice, subslice);
387 				++extarray;
388 			}
389 		}
390 	}
391 
392 	drm_dbg(&i915->drm, "GuC-capture found %d-ext-regs.\n", num_tot_regs);
393 	guc->capture->extlists = extlists;
394 }
395 
396 static const struct __guc_mmio_reg_descr_group *
guc_capture_get_device_reglist(struct intel_guc * guc)397 guc_capture_get_device_reglist(struct intel_guc *guc)
398 {
399 	struct drm_i915_private *i915 = guc_to_gt(guc)->i915;
400 
401 	if (GRAPHICS_VER(i915) > 11) {
402 		/*
403 		 * For certain engine classes, there are slice and subslice
404 		 * level registers requiring steering. We allocate and populate
405 		 * these at init time based on hw config add it as an extension
406 		 * list at the end of the pre-populated render list.
407 		 */
408 		if (IS_DG2(i915))
409 			guc_capture_alloc_steered_lists_xe_hpg(guc, xe_lpd_lists, IP_VER(12, 55));
410 		else if (IS_XEHPSDV(i915))
411 			guc_capture_alloc_steered_lists_xe_hpg(guc, xe_lpd_lists, IP_VER(12, 50));
412 		else
413 			guc_capture_alloc_steered_lists_xe_lpd(guc, xe_lpd_lists);
414 
415 		return xe_lpd_lists;
416 	}
417 
418 	/* if GuC submission is enabled on a non-POR platform, just use a common baseline */
419 	return default_lists;
420 }
421 
422 static const char *
__stringify_type(u32 type)423 __stringify_type(u32 type)
424 {
425 	switch (type) {
426 	case GUC_CAPTURE_LIST_TYPE_GLOBAL:
427 		return "Global";
428 	case GUC_CAPTURE_LIST_TYPE_ENGINE_CLASS:
429 		return "Class";
430 	case GUC_CAPTURE_LIST_TYPE_ENGINE_INSTANCE:
431 		return "Instance";
432 	default:
433 		break;
434 	}
435 
436 	return "unknown";
437 }
438 
439 static const char *
__stringify_engclass(u32 class)440 __stringify_engclass(u32 class)
441 {
442 	switch (class) {
443 	case GUC_RENDER_CLASS:
444 		return "Render";
445 	case GUC_VIDEO_CLASS:
446 		return "Video";
447 	case GUC_VIDEOENHANCE_CLASS:
448 		return "VideoEnhance";
449 	case GUC_BLITTER_CLASS:
450 		return "Blitter";
451 	case GUC_COMPUTE_CLASS:
452 		return "Compute";
453 	default:
454 		break;
455 	}
456 
457 	return "unknown";
458 }
459 
460 static int
guc_capture_list_init(struct intel_guc * guc,u32 owner,u32 type,u32 classid,struct guc_mmio_reg * ptr,u16 num_entries)461 guc_capture_list_init(struct intel_guc *guc, u32 owner, u32 type, u32 classid,
462 		      struct guc_mmio_reg *ptr, u16 num_entries)
463 {
464 	u32 i = 0, j = 0;
465 	struct drm_i915_private *i915 = guc_to_gt(guc)->i915;
466 	const struct __guc_mmio_reg_descr_group *reglists = guc->capture->reglists;
467 	struct __guc_mmio_reg_descr_group *extlists = guc->capture->extlists;
468 	const struct __guc_mmio_reg_descr_group *match;
469 	struct __guc_mmio_reg_descr_group *matchext;
470 
471 	if (!reglists)
472 		return -ENODEV;
473 
474 	match = guc_capture_get_one_list(reglists, owner, type, classid);
475 	if (!match)
476 		return -ENODATA;
477 
478 	for (i = 0; i < num_entries && i < match->num_regs; ++i) {
479 		ptr[i].offset = match->list[i].reg.reg;
480 		ptr[i].value = 0xDEADF00D;
481 		ptr[i].flags = match->list[i].flags;
482 		ptr[i].mask = match->list[i].mask;
483 	}
484 
485 	matchext = guc_capture_get_one_ext_list(extlists, owner, type, classid);
486 	if (matchext) {
487 		for (i = match->num_regs, j = 0; i < num_entries &&
488 		     i < (match->num_regs + matchext->num_regs) &&
489 			j < matchext->num_regs; ++i, ++j) {
490 			ptr[i].offset = matchext->extlist[j].reg.reg;
491 			ptr[i].value = 0xDEADF00D;
492 			ptr[i].flags = matchext->extlist[j].flags;
493 			ptr[i].mask = matchext->extlist[j].mask;
494 		}
495 	}
496 	if (i < num_entries)
497 		drm_dbg(&i915->drm, "GuC-capture: Init reglist short %d out %d.\n",
498 			(int)i, (int)num_entries);
499 
500 	return 0;
501 }
502 
503 static int
guc_cap_list_num_regs(struct intel_guc_state_capture * gc,u32 owner,u32 type,u32 classid)504 guc_cap_list_num_regs(struct intel_guc_state_capture *gc, u32 owner, u32 type, u32 classid)
505 {
506 	const struct __guc_mmio_reg_descr_group *match;
507 	struct __guc_mmio_reg_descr_group *matchext;
508 	int num_regs;
509 
510 	match = guc_capture_get_one_list(gc->reglists, owner, type, classid);
511 	if (!match)
512 		return 0;
513 
514 	num_regs = match->num_regs;
515 
516 	matchext = guc_capture_get_one_ext_list(gc->extlists, owner, type, classid);
517 	if (matchext)
518 		num_regs += matchext->num_regs;
519 
520 	return num_regs;
521 }
522 
523 static int
guc_capture_getlistsize(struct intel_guc * guc,u32 owner,u32 type,u32 classid,size_t * size,bool is_purpose_est)524 guc_capture_getlistsize(struct intel_guc *guc, u32 owner, u32 type, u32 classid,
525 			size_t *size, bool is_purpose_est)
526 {
527 	struct intel_guc_state_capture *gc = guc->capture;
528 	struct drm_i915_private *i915 = guc_to_gt(guc)->i915;
529 	struct __guc_capture_ads_cache *cache = &gc->ads_cache[owner][type][classid];
530 	int num_regs;
531 
532 	if (!gc->reglists) {
533 		drm_warn(&i915->drm, "GuC-capture: No reglist on this device\n");
534 		return -ENODEV;
535 	}
536 
537 	if (cache->is_valid) {
538 		*size = cache->size;
539 		return cache->status;
540 	}
541 
542 	if (!is_purpose_est && owner == GUC_CAPTURE_LIST_INDEX_PF &&
543 	    !guc_capture_get_one_list(gc->reglists, owner, type, classid)) {
544 		if (type == GUC_CAPTURE_LIST_TYPE_GLOBAL)
545 			drm_warn(&i915->drm, "Missing GuC-Err-Cap reglist Global!\n");
546 		else
547 			drm_warn(&i915->drm, "Missing GuC-Err-Cap reglist %s(%u):%s(%u)!\n",
548 				 __stringify_type(type), type,
549 				 __stringify_engclass(classid), classid);
550 		return -ENODATA;
551 	}
552 
553 	num_regs = guc_cap_list_num_regs(gc, owner, type, classid);
554 	/* intentional empty lists can exist depending on hw config */
555 	if (!num_regs)
556 		return -ENODATA;
557 
558 	if (size)
559 		*size = PAGE_ALIGN((sizeof(struct guc_debug_capture_list)) +
560 				   (num_regs * sizeof(struct guc_mmio_reg)));
561 
562 	return 0;
563 }
564 
565 int
intel_guc_capture_getlistsize(struct intel_guc * guc,u32 owner,u32 type,u32 classid,size_t * size)566 intel_guc_capture_getlistsize(struct intel_guc *guc, u32 owner, u32 type, u32 classid,
567 			      size_t *size)
568 {
569 	return guc_capture_getlistsize(guc, owner, type, classid, size, false);
570 }
571 
572 static void guc_capture_create_prealloc_nodes(struct intel_guc *guc);
573 
574 int
intel_guc_capture_getlist(struct intel_guc * guc,u32 owner,u32 type,u32 classid,void ** outptr)575 intel_guc_capture_getlist(struct intel_guc *guc, u32 owner, u32 type, u32 classid,
576 			  void **outptr)
577 {
578 	struct intel_guc_state_capture *gc = guc->capture;
579 	struct __guc_capture_ads_cache *cache = &gc->ads_cache[owner][type][classid];
580 	struct drm_i915_private *i915 = guc_to_gt(guc)->i915;
581 	struct guc_debug_capture_list *listnode;
582 	int ret, num_regs;
583 	u8 *caplist, *tmp;
584 	size_t size = 0;
585 
586 	if (!gc->reglists)
587 		return -ENODEV;
588 
589 	if (cache->is_valid) {
590 		*outptr = cache->ptr;
591 		return cache->status;
592 	}
593 
594 	/*
595 	 * ADS population of input registers is a good
596 	 * time to pre-allocate cachelist output nodes
597 	 */
598 	guc_capture_create_prealloc_nodes(guc);
599 
600 	ret = intel_guc_capture_getlistsize(guc, owner, type, classid, &size);
601 	if (ret) {
602 		cache->is_valid = true;
603 		cache->ptr = NULL;
604 		cache->size = 0;
605 		cache->status = ret;
606 		return ret;
607 	}
608 
609 	caplist = kzalloc(size, GFP_KERNEL);
610 	if (!caplist) {
611 		drm_dbg(&i915->drm, "GuC-capture: failed to alloc cached caplist");
612 		return -ENOMEM;
613 	}
614 
615 	/* populate capture list header */
616 	tmp = caplist;
617 	num_regs = guc_cap_list_num_regs(guc->capture, owner, type, classid);
618 	listnode = (struct guc_debug_capture_list *)tmp;
619 	listnode->header.info = FIELD_PREP(GUC_CAPTURELISTHDR_NUMDESCR, (u32)num_regs);
620 
621 	/* populate list of register descriptor */
622 	tmp += sizeof(struct guc_debug_capture_list);
623 	guc_capture_list_init(guc, owner, type, classid, (struct guc_mmio_reg *)tmp, num_regs);
624 
625 	/* cache this list */
626 	cache->is_valid = true;
627 	cache->ptr = caplist;
628 	cache->size = size;
629 	cache->status = 0;
630 
631 	*outptr = caplist;
632 
633 	return 0;
634 }
635 
636 int
intel_guc_capture_getnullheader(struct intel_guc * guc,void ** outptr,size_t * size)637 intel_guc_capture_getnullheader(struct intel_guc *guc,
638 				void **outptr, size_t *size)
639 {
640 	struct intel_guc_state_capture *gc = guc->capture;
641 	struct drm_i915_private *i915 = guc_to_gt(guc)->i915;
642 	int tmp = sizeof(u32) * 4;
643 	void *null_header;
644 
645 	if (gc->ads_null_cache) {
646 		*outptr = gc->ads_null_cache;
647 		*size = tmp;
648 		return 0;
649 	}
650 
651 	null_header = kzalloc(tmp, GFP_KERNEL);
652 	if (!null_header) {
653 		drm_dbg(&i915->drm, "GuC-capture: failed to alloc cached nulllist");
654 		return -ENOMEM;
655 	}
656 
657 	gc->ads_null_cache = null_header;
658 	*outptr = null_header;
659 	*size = tmp;
660 
661 	return 0;
662 }
663 
664 static int
guc_capture_output_min_size_est(struct intel_guc * guc)665 guc_capture_output_min_size_est(struct intel_guc *guc)
666 {
667 	struct intel_gt *gt = guc_to_gt(guc);
668 	struct intel_engine_cs *engine;
669 	enum intel_engine_id id;
670 	int worst_min_size = 0;
671 	size_t tmp = 0;
672 
673 	if (!guc->capture)
674 		return -ENODEV;
675 
676 	/*
677 	 * If every single engine-instance suffered a failure in quick succession but
678 	 * were all unrelated, then a burst of multiple error-capture events would dump
679 	 * registers for every one engine instance, one at a time. In this case, GuC
680 	 * would even dump the global-registers repeatedly.
681 	 *
682 	 * For each engine instance, there would be 1 x guc_state_capture_group_t output
683 	 * followed by 3 x guc_state_capture_t lists. The latter is how the register
684 	 * dumps are split across different register types (where the '3' are global vs class
685 	 * vs instance).
686 	 */
687 	for_each_engine(engine, gt, id) {
688 		worst_min_size += sizeof(struct guc_state_capture_group_header_t) +
689 					 (3 * sizeof(struct guc_state_capture_header_t));
690 
691 		if (!guc_capture_getlistsize(guc, 0, GUC_CAPTURE_LIST_TYPE_GLOBAL, 0, &tmp, true))
692 			worst_min_size += tmp;
693 
694 		if (!guc_capture_getlistsize(guc, 0, GUC_CAPTURE_LIST_TYPE_ENGINE_CLASS,
695 					     engine->class, &tmp, true)) {
696 			worst_min_size += tmp;
697 		}
698 		if (!guc_capture_getlistsize(guc, 0, GUC_CAPTURE_LIST_TYPE_ENGINE_INSTANCE,
699 					     engine->class, &tmp, true)) {
700 			worst_min_size += tmp;
701 		}
702 	}
703 
704 	return worst_min_size;
705 }
706 
707 /*
708  * Add on a 3x multiplier to allow for multiple back-to-back captures occurring
709  * before the i915 can read the data out and process it
710  */
711 #define GUC_CAPTURE_OVERBUFFER_MULTIPLIER 3
712 
check_guc_capture_size(struct intel_guc * guc)713 static void check_guc_capture_size(struct intel_guc *guc)
714 {
715 	struct drm_i915_private *i915 = guc_to_gt(guc)->i915;
716 	int min_size = guc_capture_output_min_size_est(guc);
717 	int spare_size = min_size * GUC_CAPTURE_OVERBUFFER_MULTIPLIER;
718 	u32 buffer_size = intel_guc_log_section_size_capture(&guc->log);
719 
720 	/*
721 	 * NOTE: min_size is much smaller than the capture region allocation (DG2: <80K vs 1MB)
722 	 * Additionally, its based on space needed to fit all engines getting reset at once
723 	 * within the same G2H handler task slot. This is very unlikely. However, if GuC really
724 	 * does run out of space for whatever reason, we will see an separate warning message
725 	 * when processing the G2H event capture-notification, search for:
726 	 * INTEL_GUC_STATE_CAPTURE_EVENT_STATUS_NOSPACE.
727 	 */
728 	if (min_size < 0)
729 		drm_warn(&i915->drm, "Failed to calculate GuC error state capture buffer minimum size: %d!\n",
730 			 min_size);
731 	else if (min_size > buffer_size)
732 		drm_warn(&i915->drm, "GuC error state capture buffer maybe small: %d < %d\n",
733 			 buffer_size, min_size);
734 	else if (spare_size > buffer_size)
735 		drm_dbg(&i915->drm, "GuC error state capture buffer lacks spare size: %d < %d (min = %d)\n",
736 			buffer_size, spare_size, min_size);
737 }
738 
739 /*
740  * KMD Init time flows:
741  * --------------------
742  *     --> alloc A: GuC input capture regs lists (registered to GuC via ADS).
743  *                  intel_guc_ads acquires the register lists by calling
744  *                  intel_guc_capture_list_size and intel_guc_capture_list_get 'n' times,
745  *                  where n = 1 for global-reg-list +
746  *                            num_engine_classes for class-reg-list +
747  *                            num_engine_classes for instance-reg-list
748  *                               (since all instances of the same engine-class type
749  *                                have an identical engine-instance register-list).
750  *                  ADS module also calls separately for PF vs VF.
751  *
752  *     --> alloc B: GuC output capture buf (registered via guc_init_params(log_param))
753  *                  Size = #define CAPTURE_BUFFER_SIZE (warns if on too-small)
754  *                  Note2: 'x 3' to hold multiple capture groups
755  *
756  * GUC Runtime notify capture:
757  * --------------------------
758  *     --> G2H STATE_CAPTURE_NOTIFICATION
759  *                   L--> intel_guc_capture_process
760  *                           L--> Loop through B (head..tail) and for each engine instance's
761  *                                err-state-captured register-list we find, we alloc 'C':
762  *      --> alloc C: A capture-output-node structure that includes misc capture info along
763  *                   with 3 register list dumps (global, engine-class and engine-instance)
764  *                   This node is created from a pre-allocated list of blank nodes in
765  *                   guc->capture->cachelist and populated with the error-capture
766  *                   data from GuC and then it's added into guc->capture->outlist linked
767  *                   list. This list is used for matchup and printout by i915_gpu_coredump
768  *                   and err_print_gt, (when user invokes the error capture sysfs).
769  *
770  * GUC --> notify context reset:
771  * -----------------------------
772  *     --> G2H CONTEXT RESET
773  *                   L--> guc_handle_context_reset --> i915_capture_error_state
774  *                          L--> i915_gpu_coredump(..IS_GUC_CAPTURE) --> gt_record_engines
775  *                               --> capture_engine(..IS_GUC_CAPTURE)
776  *                               L--> intel_guc_capture_get_matching_node is where
777  *                                    detach C from internal linked list and add it into
778  *                                    intel_engine_coredump struct (if the context and
779  *                                    engine of the event notification matches a node
780  *                                    in the link list).
781  *
782  * User Sysfs / Debugfs
783  * --------------------
784  *      --> i915_gpu_coredump_copy_to_buffer->
785  *                   L--> err_print_to_sgl --> err_print_gt
786  *                        L--> error_print_guc_captures
787  *                             L--> intel_guc_capture_print_node prints the
788  *                                  register lists values of the attached node
789  *                                  on the error-engine-dump being reported.
790  *                   L--> i915_reset_error_state ... -->__i915_gpu_coredump_free
791  *                        L--> ... cleanup_gt -->
792  *                             L--> intel_guc_capture_free_node returns the
793  *                                  capture-output-node back to the internal
794  *                                  cachelist for reuse.
795  *
796  */
797 
guc_capture_buf_cnt(struct __guc_capture_bufstate * buf)798 static int guc_capture_buf_cnt(struct __guc_capture_bufstate *buf)
799 {
800 	if (buf->wr >= buf->rd)
801 		return (buf->wr - buf->rd);
802 	return (buf->size - buf->rd) + buf->wr;
803 }
804 
guc_capture_buf_cnt_to_end(struct __guc_capture_bufstate * buf)805 static int guc_capture_buf_cnt_to_end(struct __guc_capture_bufstate *buf)
806 {
807 	if (buf->rd > buf->wr)
808 		return (buf->size - buf->rd);
809 	return (buf->wr - buf->rd);
810 }
811 
812 /*
813  * GuC's error-capture output is a ring buffer populated in a byte-stream fashion:
814  *
815  * The GuC Log buffer region for error-capture is managed like a ring buffer.
816  * The GuC firmware dumps error capture logs into this ring in a byte-stream flow.
817  * Additionally, as per the current and foreseeable future, all packed error-
818  * capture output structures are dword aligned.
819  *
820  * That said, if the GuC firmware is in the midst of writing a structure that is larger
821  * than one dword but the tail end of the err-capture buffer-region has lesser space left,
822  * we would need to extract that structure one dword at a time straddled across the end,
823  * onto the start of the ring.
824  *
825  * Below function, guc_capture_log_remove_dw is a helper for that. All callers of this
826  * function would typically do a straight-up memcpy from the ring contents and will only
827  * call this helper if their structure-extraction is straddling across the end of the
828  * ring. GuC firmware does not add any padding. The reason for the no-padding is to ease
829  * scalability for future expansion of output data types without requiring a redesign
830  * of the flow controls.
831  */
832 static int
guc_capture_log_remove_dw(struct intel_guc * guc,struct __guc_capture_bufstate * buf,u32 * dw)833 guc_capture_log_remove_dw(struct intel_guc *guc, struct __guc_capture_bufstate *buf,
834 			  u32 *dw)
835 {
836 	struct drm_i915_private *i915 = guc_to_gt(guc)->i915;
837 	int tries = 2;
838 	int avail = 0;
839 	u32 *src_data;
840 
841 	if (!guc_capture_buf_cnt(buf))
842 		return 0;
843 
844 	while (tries--) {
845 		avail = guc_capture_buf_cnt_to_end(buf);
846 		if (avail >= sizeof(u32)) {
847 			src_data = (u32 *)(buf->data + buf->rd);
848 			*dw = *src_data;
849 			buf->rd += 4;
850 			return 4;
851 		}
852 		if (avail)
853 			drm_dbg(&i915->drm, "GuC-Cap-Logs not dword aligned, skipping.\n");
854 		buf->rd = 0;
855 	}
856 
857 	return 0;
858 }
859 
860 static bool
guc_capture_data_extracted(struct __guc_capture_bufstate * b,int size,void * dest)861 guc_capture_data_extracted(struct __guc_capture_bufstate *b,
862 			   int size, void *dest)
863 {
864 	if (guc_capture_buf_cnt_to_end(b) >= size) {
865 		memcpy(dest, (b->data + b->rd), size);
866 		b->rd += size;
867 		return true;
868 	}
869 	return false;
870 }
871 
872 static int
guc_capture_log_get_group_hdr(struct intel_guc * guc,struct __guc_capture_bufstate * buf,struct guc_state_capture_group_header_t * ghdr)873 guc_capture_log_get_group_hdr(struct intel_guc *guc, struct __guc_capture_bufstate *buf,
874 			      struct guc_state_capture_group_header_t *ghdr)
875 {
876 	int read = 0;
877 	int fullsize = sizeof(struct guc_state_capture_group_header_t);
878 
879 	if (fullsize > guc_capture_buf_cnt(buf))
880 		return -1;
881 
882 	if (guc_capture_data_extracted(buf, fullsize, (void *)ghdr))
883 		return 0;
884 
885 	read += guc_capture_log_remove_dw(guc, buf, &ghdr->owner);
886 	read += guc_capture_log_remove_dw(guc, buf, &ghdr->info);
887 	if (read != fullsize)
888 		return -1;
889 
890 	return 0;
891 }
892 
893 static int
guc_capture_log_get_data_hdr(struct intel_guc * guc,struct __guc_capture_bufstate * buf,struct guc_state_capture_header_t * hdr)894 guc_capture_log_get_data_hdr(struct intel_guc *guc, struct __guc_capture_bufstate *buf,
895 			     struct guc_state_capture_header_t *hdr)
896 {
897 	int read = 0;
898 	int fullsize = sizeof(struct guc_state_capture_header_t);
899 
900 	if (fullsize > guc_capture_buf_cnt(buf))
901 		return -1;
902 
903 	if (guc_capture_data_extracted(buf, fullsize, (void *)hdr))
904 		return 0;
905 
906 	read += guc_capture_log_remove_dw(guc, buf, &hdr->owner);
907 	read += guc_capture_log_remove_dw(guc, buf, &hdr->info);
908 	read += guc_capture_log_remove_dw(guc, buf, &hdr->lrca);
909 	read += guc_capture_log_remove_dw(guc, buf, &hdr->guc_id);
910 	read += guc_capture_log_remove_dw(guc, buf, &hdr->num_mmios);
911 	if (read != fullsize)
912 		return -1;
913 
914 	return 0;
915 }
916 
917 static int
guc_capture_log_get_register(struct intel_guc * guc,struct __guc_capture_bufstate * buf,struct guc_mmio_reg * reg)918 guc_capture_log_get_register(struct intel_guc *guc, struct __guc_capture_bufstate *buf,
919 			     struct guc_mmio_reg *reg)
920 {
921 	int read = 0;
922 	int fullsize = sizeof(struct guc_mmio_reg);
923 
924 	if (fullsize > guc_capture_buf_cnt(buf))
925 		return -1;
926 
927 	if (guc_capture_data_extracted(buf, fullsize, (void *)reg))
928 		return 0;
929 
930 	read += guc_capture_log_remove_dw(guc, buf, &reg->offset);
931 	read += guc_capture_log_remove_dw(guc, buf, &reg->value);
932 	read += guc_capture_log_remove_dw(guc, buf, &reg->flags);
933 	read += guc_capture_log_remove_dw(guc, buf, &reg->mask);
934 	if (read != fullsize)
935 		return -1;
936 
937 	return 0;
938 }
939 
940 static void
guc_capture_delete_one_node(struct intel_guc * guc,struct __guc_capture_parsed_output * node)941 guc_capture_delete_one_node(struct intel_guc *guc, struct __guc_capture_parsed_output *node)
942 {
943 	int i;
944 
945 	for (i = 0; i < GUC_CAPTURE_LIST_TYPE_MAX; ++i)
946 		kfree(node->reginfo[i].regs);
947 	list_del(&node->link);
948 	kfree(node);
949 }
950 
951 static void
guc_capture_delete_prealloc_nodes(struct intel_guc * guc)952 guc_capture_delete_prealloc_nodes(struct intel_guc *guc)
953 {
954 	struct __guc_capture_parsed_output *n, *ntmp;
955 
956 	/*
957 	 * NOTE: At the end of driver operation, we must assume that we
958 	 * have prealloc nodes in both the cachelist as well as outlist
959 	 * if unclaimed error capture events occurred prior to shutdown.
960 	 */
961 	list_for_each_entry_safe(n, ntmp, &guc->capture->outlist, link)
962 		guc_capture_delete_one_node(guc, n);
963 
964 	list_for_each_entry_safe(n, ntmp, &guc->capture->cachelist, link)
965 		guc_capture_delete_one_node(guc, n);
966 }
967 
968 static void
guc_capture_add_node_to_list(struct __guc_capture_parsed_output * node,struct list_head * list)969 guc_capture_add_node_to_list(struct __guc_capture_parsed_output *node,
970 			     struct list_head *list)
971 {
972 	list_add_tail(&node->link, list);
973 }
974 
975 static void
guc_capture_add_node_to_outlist(struct intel_guc_state_capture * gc,struct __guc_capture_parsed_output * node)976 guc_capture_add_node_to_outlist(struct intel_guc_state_capture *gc,
977 				struct __guc_capture_parsed_output *node)
978 {
979 	guc_capture_add_node_to_list(node, &gc->outlist);
980 }
981 
982 static void
guc_capture_add_node_to_cachelist(struct intel_guc_state_capture * gc,struct __guc_capture_parsed_output * node)983 guc_capture_add_node_to_cachelist(struct intel_guc_state_capture *gc,
984 				  struct __guc_capture_parsed_output *node)
985 {
986 	guc_capture_add_node_to_list(node, &gc->cachelist);
987 }
988 
989 static void
guc_capture_init_node(struct intel_guc * guc,struct __guc_capture_parsed_output * node)990 guc_capture_init_node(struct intel_guc *guc, struct __guc_capture_parsed_output *node)
991 {
992 	struct guc_mmio_reg *tmp[GUC_CAPTURE_LIST_TYPE_MAX];
993 	int i;
994 
995 	for (i = 0; i < GUC_CAPTURE_LIST_TYPE_MAX; ++i) {
996 		tmp[i] = node->reginfo[i].regs;
997 		memset(tmp[i], 0, sizeof(struct guc_mmio_reg) *
998 		       guc->capture->max_mmio_per_node);
999 	}
1000 	memset(node, 0, sizeof(*node));
1001 	for (i = 0; i < GUC_CAPTURE_LIST_TYPE_MAX; ++i)
1002 		node->reginfo[i].regs = tmp[i];
1003 
1004 	INIT_LIST_HEAD(&node->link);
1005 }
1006 
1007 static struct __guc_capture_parsed_output *
guc_capture_get_prealloc_node(struct intel_guc * guc)1008 guc_capture_get_prealloc_node(struct intel_guc *guc)
1009 {
1010 	struct __guc_capture_parsed_output *found = NULL;
1011 
1012 	if (!list_empty(&guc->capture->cachelist)) {
1013 		struct __guc_capture_parsed_output *n, *ntmp;
1014 
1015 		/* get first avail node from the cache list */
1016 		list_for_each_entry_safe(n, ntmp, &guc->capture->cachelist, link) {
1017 			found = n;
1018 			list_del(&n->link);
1019 			break;
1020 		}
1021 	} else {
1022 		struct __guc_capture_parsed_output *n, *ntmp;
1023 
1024 		/* traverse down and steal back the oldest node already allocated */
1025 		list_for_each_entry_safe(n, ntmp, &guc->capture->outlist, link) {
1026 			found = n;
1027 		}
1028 		if (found)
1029 			list_del(&found->link);
1030 	}
1031 	if (found)
1032 		guc_capture_init_node(guc, found);
1033 
1034 	return found;
1035 }
1036 
1037 static struct __guc_capture_parsed_output *
guc_capture_alloc_one_node(struct intel_guc * guc)1038 guc_capture_alloc_one_node(struct intel_guc *guc)
1039 {
1040 	struct __guc_capture_parsed_output *new;
1041 	int i;
1042 
1043 	new = kzalloc(sizeof(*new), GFP_KERNEL);
1044 	if (!new)
1045 		return NULL;
1046 
1047 	for (i = 0; i < GUC_CAPTURE_LIST_TYPE_MAX; ++i) {
1048 		new->reginfo[i].regs = kcalloc(guc->capture->max_mmio_per_node,
1049 					       sizeof(struct guc_mmio_reg), GFP_KERNEL);
1050 		if (!new->reginfo[i].regs) {
1051 			while (i)
1052 				kfree(new->reginfo[--i].regs);
1053 			kfree(new);
1054 			return NULL;
1055 		}
1056 	}
1057 	guc_capture_init_node(guc, new);
1058 
1059 	return new;
1060 }
1061 
1062 static struct __guc_capture_parsed_output *
guc_capture_clone_node(struct intel_guc * guc,struct __guc_capture_parsed_output * original,u32 keep_reglist_mask)1063 guc_capture_clone_node(struct intel_guc *guc, struct __guc_capture_parsed_output *original,
1064 		       u32 keep_reglist_mask)
1065 {
1066 	struct __guc_capture_parsed_output *new;
1067 	int i;
1068 
1069 	new = guc_capture_get_prealloc_node(guc);
1070 	if (!new)
1071 		return NULL;
1072 	if (!original)
1073 		return new;
1074 
1075 	new->is_partial = original->is_partial;
1076 
1077 	/* copy reg-lists that we want to clone */
1078 	for (i = 0; i < GUC_CAPTURE_LIST_TYPE_MAX; ++i) {
1079 		if (keep_reglist_mask & BIT(i)) {
1080 			GEM_BUG_ON(original->reginfo[i].num_regs  >
1081 				   guc->capture->max_mmio_per_node);
1082 
1083 			memcpy(new->reginfo[i].regs, original->reginfo[i].regs,
1084 			       original->reginfo[i].num_regs * sizeof(struct guc_mmio_reg));
1085 
1086 			new->reginfo[i].num_regs = original->reginfo[i].num_regs;
1087 			new->reginfo[i].vfid  = original->reginfo[i].vfid;
1088 
1089 			if (i == GUC_CAPTURE_LIST_TYPE_ENGINE_CLASS) {
1090 				new->eng_class = original->eng_class;
1091 			} else if (i == GUC_CAPTURE_LIST_TYPE_ENGINE_INSTANCE) {
1092 				new->eng_inst = original->eng_inst;
1093 				new->guc_id = original->guc_id;
1094 				new->lrca = original->lrca;
1095 			}
1096 		}
1097 	}
1098 
1099 	return new;
1100 }
1101 
1102 static void
__guc_capture_create_prealloc_nodes(struct intel_guc * guc)1103 __guc_capture_create_prealloc_nodes(struct intel_guc *guc)
1104 {
1105 	struct __guc_capture_parsed_output *node = NULL;
1106 	struct drm_i915_private *i915 = guc_to_gt(guc)->i915;
1107 	int i;
1108 
1109 	for (i = 0; i < PREALLOC_NODES_MAX_COUNT; ++i) {
1110 		node = guc_capture_alloc_one_node(guc);
1111 		if (!node) {
1112 			drm_warn(&i915->drm, "GuC Capture pre-alloc-cache failure\n");
1113 			/* dont free the priors, use what we got and cleanup at shutdown */
1114 			return;
1115 		}
1116 		guc_capture_add_node_to_cachelist(guc->capture, node);
1117 	}
1118 }
1119 
1120 static int
guc_get_max_reglist_count(struct intel_guc * guc)1121 guc_get_max_reglist_count(struct intel_guc *guc)
1122 {
1123 	int i, j, k, tmp, maxregcount = 0;
1124 
1125 	for (i = 0; i < GUC_CAPTURE_LIST_INDEX_MAX; ++i) {
1126 		for (j = 0; j < GUC_CAPTURE_LIST_TYPE_MAX; ++j) {
1127 			for (k = 0; k < GUC_MAX_ENGINE_CLASSES; ++k) {
1128 				if (j == GUC_CAPTURE_LIST_TYPE_GLOBAL && k > 0)
1129 					continue;
1130 
1131 				tmp = guc_cap_list_num_regs(guc->capture, i, j, k);
1132 				if (tmp > maxregcount)
1133 					maxregcount = tmp;
1134 			}
1135 		}
1136 	}
1137 	if (!maxregcount)
1138 		maxregcount = PREALLOC_NODES_DEFAULT_NUMREGS;
1139 
1140 	return maxregcount;
1141 }
1142 
1143 static void
guc_capture_create_prealloc_nodes(struct intel_guc * guc)1144 guc_capture_create_prealloc_nodes(struct intel_guc *guc)
1145 {
1146 	/* skip if we've already done the pre-alloc */
1147 	if (guc->capture->max_mmio_per_node)
1148 		return;
1149 
1150 	guc->capture->max_mmio_per_node = guc_get_max_reglist_count(guc);
1151 	__guc_capture_create_prealloc_nodes(guc);
1152 }
1153 
1154 static int
guc_capture_extract_reglists(struct intel_guc * guc,struct __guc_capture_bufstate * buf)1155 guc_capture_extract_reglists(struct intel_guc *guc, struct __guc_capture_bufstate *buf)
1156 {
1157 	struct drm_i915_private *i915 = guc_to_gt(guc)->i915;
1158 	struct guc_state_capture_group_header_t ghdr = {0};
1159 	struct guc_state_capture_header_t hdr = {0};
1160 	struct __guc_capture_parsed_output *node = NULL;
1161 	struct guc_mmio_reg *regs = NULL;
1162 	int i, numlists, numregs, ret = 0;
1163 	enum guc_capture_type datatype;
1164 	struct guc_mmio_reg tmp;
1165 	bool is_partial = false;
1166 
1167 	i = guc_capture_buf_cnt(buf);
1168 	if (!i)
1169 		return -ENODATA;
1170 	if (i % sizeof(u32)) {
1171 		drm_warn(&i915->drm, "GuC Capture new entries unaligned\n");
1172 		ret = -EIO;
1173 		goto bailout;
1174 	}
1175 
1176 	/* first get the capture group header */
1177 	if (guc_capture_log_get_group_hdr(guc, buf, &ghdr)) {
1178 		ret = -EIO;
1179 		goto bailout;
1180 	}
1181 	/*
1182 	 * we would typically expect a layout as below where n would be expected to be
1183 	 * anywhere between 3 to n where n > 3 if we are seeing multiple dependent engine
1184 	 * instances being reset together.
1185 	 * ____________________________________________
1186 	 * | Capture Group                            |
1187 	 * | ________________________________________ |
1188 	 * | | Capture Group Header:                | |
1189 	 * | |  - num_captures = 5                  | |
1190 	 * | |______________________________________| |
1191 	 * | ________________________________________ |
1192 	 * | | Capture1:                            | |
1193 	 * | |  Hdr: GLOBAL, numregs=a              | |
1194 	 * | | ____________________________________ | |
1195 	 * | | | Reglist                          | | |
1196 	 * | | | - reg1, reg2, ... rega           | | |
1197 	 * | | |__________________________________| | |
1198 	 * | |______________________________________| |
1199 	 * | ________________________________________ |
1200 	 * | | Capture2:                            | |
1201 	 * | |  Hdr: CLASS=RENDER/COMPUTE, numregs=b| |
1202 	 * | | ____________________________________ | |
1203 	 * | | | Reglist                          | | |
1204 	 * | | | - reg1, reg2, ... regb           | | |
1205 	 * | | |__________________________________| | |
1206 	 * | |______________________________________| |
1207 	 * | ________________________________________ |
1208 	 * | | Capture3:                            | |
1209 	 * | |  Hdr: INSTANCE=RCS, numregs=c        | |
1210 	 * | | ____________________________________ | |
1211 	 * | | | Reglist                          | | |
1212 	 * | | | - reg1, reg2, ... regc           | | |
1213 	 * | | |__________________________________| | |
1214 	 * | |______________________________________| |
1215 	 * | ________________________________________ |
1216 	 * | | Capture4:                            | |
1217 	 * | |  Hdr: CLASS=RENDER/COMPUTE, numregs=d| |
1218 	 * | | ____________________________________ | |
1219 	 * | | | Reglist                          | | |
1220 	 * | | | - reg1, reg2, ... regd           | | |
1221 	 * | | |__________________________________| | |
1222 	 * | |______________________________________| |
1223 	 * | ________________________________________ |
1224 	 * | | Capture5:                            | |
1225 	 * | |  Hdr: INSTANCE=CCS0, numregs=e       | |
1226 	 * | | ____________________________________ | |
1227 	 * | | | Reglist                          | | |
1228 	 * | | | - reg1, reg2, ... rege           | | |
1229 	 * | | |__________________________________| | |
1230 	 * | |______________________________________| |
1231 	 * |__________________________________________|
1232 	 */
1233 	is_partial = FIELD_GET(CAP_GRP_HDR_CAPTURE_TYPE, ghdr.info);
1234 	numlists = FIELD_GET(CAP_GRP_HDR_NUM_CAPTURES, ghdr.info);
1235 
1236 	while (numlists--) {
1237 		if (guc_capture_log_get_data_hdr(guc, buf, &hdr)) {
1238 			ret = -EIO;
1239 			break;
1240 		}
1241 
1242 		datatype = FIELD_GET(CAP_HDR_CAPTURE_TYPE, hdr.info);
1243 		if (datatype > GUC_CAPTURE_LIST_TYPE_ENGINE_INSTANCE) {
1244 			/* unknown capture type - skip over to next capture set */
1245 			numregs = FIELD_GET(CAP_HDR_NUM_MMIOS, hdr.num_mmios);
1246 			while (numregs--) {
1247 				if (guc_capture_log_get_register(guc, buf, &tmp)) {
1248 					ret = -EIO;
1249 					break;
1250 				}
1251 			}
1252 			continue;
1253 		} else if (node) {
1254 			/*
1255 			 * Based on the current capture type and what we have so far,
1256 			 * decide if we should add the current node into the internal
1257 			 * linked list for match-up when i915_gpu_coredump calls later
1258 			 * (and alloc a blank node for the next set of reglists)
1259 			 * or continue with the same node or clone the current node
1260 			 * but only retain the global or class registers (such as the
1261 			 * case of dependent engine resets).
1262 			 */
1263 			if (datatype == GUC_CAPTURE_LIST_TYPE_GLOBAL) {
1264 				guc_capture_add_node_to_outlist(guc->capture, node);
1265 				node = NULL;
1266 			} else if (datatype == GUC_CAPTURE_LIST_TYPE_ENGINE_CLASS &&
1267 				   node->reginfo[GUC_CAPTURE_LIST_TYPE_ENGINE_CLASS].num_regs) {
1268 				/* Add to list, clone node and duplicate global list */
1269 				guc_capture_add_node_to_outlist(guc->capture, node);
1270 				node = guc_capture_clone_node(guc, node,
1271 							      GCAP_PARSED_REGLIST_INDEX_GLOBAL);
1272 			} else if (datatype == GUC_CAPTURE_LIST_TYPE_ENGINE_INSTANCE &&
1273 				   node->reginfo[GUC_CAPTURE_LIST_TYPE_ENGINE_INSTANCE].num_regs) {
1274 				/* Add to list, clone node and duplicate global + class lists */
1275 				guc_capture_add_node_to_outlist(guc->capture, node);
1276 				node = guc_capture_clone_node(guc, node,
1277 							      (GCAP_PARSED_REGLIST_INDEX_GLOBAL |
1278 							      GCAP_PARSED_REGLIST_INDEX_ENGCLASS));
1279 			}
1280 		}
1281 
1282 		if (!node) {
1283 			node = guc_capture_get_prealloc_node(guc);
1284 			if (!node) {
1285 				ret = -ENOMEM;
1286 				break;
1287 			}
1288 			if (datatype != GUC_CAPTURE_LIST_TYPE_GLOBAL)
1289 				drm_dbg(&i915->drm, "GuC Capture missing global dump: %08x!\n",
1290 					datatype);
1291 		}
1292 		node->is_partial = is_partial;
1293 		node->reginfo[datatype].vfid = FIELD_GET(CAP_HDR_CAPTURE_VFID, hdr.owner);
1294 		switch (datatype) {
1295 		case GUC_CAPTURE_LIST_TYPE_ENGINE_INSTANCE:
1296 			node->eng_class = FIELD_GET(CAP_HDR_ENGINE_CLASS, hdr.info);
1297 			node->eng_inst = FIELD_GET(CAP_HDR_ENGINE_INSTANCE, hdr.info);
1298 			node->lrca = hdr.lrca;
1299 			node->guc_id = hdr.guc_id;
1300 			break;
1301 		case GUC_CAPTURE_LIST_TYPE_ENGINE_CLASS:
1302 			node->eng_class = FIELD_GET(CAP_HDR_ENGINE_CLASS, hdr.info);
1303 			break;
1304 		default:
1305 			break;
1306 		}
1307 
1308 		numregs = FIELD_GET(CAP_HDR_NUM_MMIOS, hdr.num_mmios);
1309 		if (numregs > guc->capture->max_mmio_per_node) {
1310 			drm_dbg(&i915->drm, "GuC Capture list extraction clipped by prealloc!\n");
1311 			numregs = guc->capture->max_mmio_per_node;
1312 		}
1313 		node->reginfo[datatype].num_regs = numregs;
1314 		regs = node->reginfo[datatype].regs;
1315 		i = 0;
1316 		while (numregs--) {
1317 			if (guc_capture_log_get_register(guc, buf, &regs[i++])) {
1318 				ret = -EIO;
1319 				break;
1320 			}
1321 		}
1322 	}
1323 
1324 bailout:
1325 	if (node) {
1326 		/* If we have data, add to linked list for match-up when i915_gpu_coredump calls */
1327 		for (i = GUC_CAPTURE_LIST_TYPE_GLOBAL; i < GUC_CAPTURE_LIST_TYPE_MAX; ++i) {
1328 			if (node->reginfo[i].regs) {
1329 				guc_capture_add_node_to_outlist(guc->capture, node);
1330 				node = NULL;
1331 				break;
1332 			}
1333 		}
1334 		if (node) /* else return it back to cache list */
1335 			guc_capture_add_node_to_cachelist(guc->capture, node);
1336 	}
1337 	return ret;
1338 }
1339 
__guc_capture_flushlog_complete(struct intel_guc * guc)1340 static int __guc_capture_flushlog_complete(struct intel_guc *guc)
1341 {
1342 	u32 action[] = {
1343 		INTEL_GUC_ACTION_LOG_BUFFER_FILE_FLUSH_COMPLETE,
1344 		GUC_CAPTURE_LOG_BUFFER
1345 	};
1346 
1347 	return intel_guc_send_nb(guc, action, ARRAY_SIZE(action), 0);
1348 
1349 }
1350 
__guc_capture_process_output(struct intel_guc * guc)1351 static void __guc_capture_process_output(struct intel_guc *guc)
1352 {
1353 	unsigned int buffer_size, read_offset, write_offset, full_count;
1354 	struct intel_uc *uc = container_of(guc, typeof(*uc), guc);
1355 	struct drm_i915_private *i915 = guc_to_gt(guc)->i915;
1356 	struct guc_log_buffer_state log_buf_state_local;
1357 	struct guc_log_buffer_state *log_buf_state;
1358 	struct __guc_capture_bufstate buf;
1359 	void *src_data = NULL;
1360 	bool new_overflow;
1361 	int ret;
1362 
1363 	log_buf_state = guc->log.buf_addr +
1364 			(sizeof(struct guc_log_buffer_state) * GUC_CAPTURE_LOG_BUFFER);
1365 	src_data = guc->log.buf_addr +
1366 		   intel_guc_get_log_buffer_offset(&guc->log, GUC_CAPTURE_LOG_BUFFER);
1367 
1368 	/*
1369 	 * Make a copy of the state structure, inside GuC log buffer
1370 	 * (which is uncached mapped), on the stack to avoid reading
1371 	 * from it multiple times.
1372 	 */
1373 	memcpy(&log_buf_state_local, log_buf_state, sizeof(struct guc_log_buffer_state));
1374 	buffer_size = intel_guc_get_log_buffer_size(&guc->log, GUC_CAPTURE_LOG_BUFFER);
1375 	read_offset = log_buf_state_local.read_ptr;
1376 	write_offset = log_buf_state_local.sampled_write_ptr;
1377 	full_count = log_buf_state_local.buffer_full_cnt;
1378 
1379 	/* Bookkeeping stuff */
1380 	guc->log.stats[GUC_CAPTURE_LOG_BUFFER].flush += log_buf_state_local.flush_to_file;
1381 	new_overflow = intel_guc_check_log_buf_overflow(&guc->log, GUC_CAPTURE_LOG_BUFFER,
1382 							full_count);
1383 
1384 	/* Now copy the actual logs. */
1385 	if (unlikely(new_overflow)) {
1386 		/* copy the whole buffer in case of overflow */
1387 		read_offset = 0;
1388 		write_offset = buffer_size;
1389 	} else if (unlikely((read_offset > buffer_size) ||
1390 			(write_offset > buffer_size))) {
1391 		drm_err(&i915->drm, "invalid GuC log capture buffer state!\n");
1392 		/* copy whole buffer as offsets are unreliable */
1393 		read_offset = 0;
1394 		write_offset = buffer_size;
1395 	}
1396 
1397 	buf.size = buffer_size;
1398 	buf.rd = read_offset;
1399 	buf.wr = write_offset;
1400 	buf.data = src_data;
1401 
1402 	if (!uc->reset_in_progress) {
1403 		do {
1404 			ret = guc_capture_extract_reglists(guc, &buf);
1405 		} while (ret >= 0);
1406 	}
1407 
1408 	/* Update the state of log buffer err-cap state */
1409 	log_buf_state->read_ptr = write_offset;
1410 	log_buf_state->flush_to_file = 0;
1411 	__guc_capture_flushlog_complete(guc);
1412 }
1413 
1414 #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
1415 
1416 static const char *
guc_capture_reg_to_str(const struct intel_guc * guc,u32 owner,u32 type,u32 class,u32 id,u32 offset,u32 * is_ext)1417 guc_capture_reg_to_str(const struct intel_guc *guc, u32 owner, u32 type,
1418 		       u32 class, u32 id, u32 offset, u32 *is_ext)
1419 {
1420 	const struct __guc_mmio_reg_descr_group *reglists = guc->capture->reglists;
1421 	struct __guc_mmio_reg_descr_group *extlists = guc->capture->extlists;
1422 	const struct __guc_mmio_reg_descr_group *match;
1423 	struct __guc_mmio_reg_descr_group *matchext;
1424 	int j;
1425 
1426 	*is_ext = 0;
1427 	if (!reglists)
1428 		return NULL;
1429 
1430 	match = guc_capture_get_one_list(reglists, owner, type, id);
1431 	if (!match)
1432 		return NULL;
1433 
1434 	for (j = 0; j < match->num_regs; ++j) {
1435 		if (offset == match->list[j].reg.reg)
1436 			return match->list[j].regname;
1437 	}
1438 	if (extlists) {
1439 		matchext = guc_capture_get_one_ext_list(extlists, owner, type, id);
1440 		if (!matchext)
1441 			return NULL;
1442 		for (j = 0; j < matchext->num_regs; ++j) {
1443 			if (offset == matchext->extlist[j].reg.reg) {
1444 				*is_ext = 1;
1445 				return matchext->extlist[j].regname;
1446 			}
1447 		}
1448 	}
1449 
1450 	return NULL;
1451 }
1452 
1453 #define GCAP_PRINT_INTEL_ENG_INFO(ebuf, eng) \
1454 	do { \
1455 		i915_error_printf(ebuf, "    i915-Eng-Name: %s command stream\n", \
1456 				  (eng)->name); \
1457 		i915_error_printf(ebuf, "    i915-Eng-Inst-Class: 0x%02x\n", (eng)->class); \
1458 		i915_error_printf(ebuf, "    i915-Eng-Inst-Id: 0x%02x\n", (eng)->instance); \
1459 		i915_error_printf(ebuf, "    i915-Eng-LogicalMask: 0x%08x\n", \
1460 				  (eng)->logical_mask); \
1461 	} while (0)
1462 
1463 #define GCAP_PRINT_GUC_INST_INFO(ebuf, node) \
1464 	do { \
1465 		i915_error_printf(ebuf, "    GuC-Engine-Inst-Id: 0x%08x\n", \
1466 				  (node)->eng_inst); \
1467 		i915_error_printf(ebuf, "    GuC-Context-Id: 0x%08x\n", (node)->guc_id); \
1468 		i915_error_printf(ebuf, "    LRCA: 0x%08x\n", (node)->lrca); \
1469 	} while (0)
1470 
intel_guc_capture_print_engine_node(struct drm_i915_error_state_buf * ebuf,const struct intel_engine_coredump * ee)1471 int intel_guc_capture_print_engine_node(struct drm_i915_error_state_buf *ebuf,
1472 					const struct intel_engine_coredump *ee)
1473 {
1474 	const char *grptype[GUC_STATE_CAPTURE_GROUP_TYPE_MAX] = {
1475 		"full-capture",
1476 		"partial-capture"
1477 	};
1478 	const char *datatype[GUC_CAPTURE_LIST_TYPE_MAX] = {
1479 		"Global",
1480 		"Engine-Class",
1481 		"Engine-Instance"
1482 	};
1483 	struct intel_guc_state_capture *cap;
1484 	struct __guc_capture_parsed_output *node;
1485 	struct intel_engine_cs *eng;
1486 	struct guc_mmio_reg *regs;
1487 	struct intel_guc *guc;
1488 	const char *str;
1489 	int numregs, i, j;
1490 	u32 is_ext;
1491 
1492 	if (!ebuf || !ee)
1493 		return -EINVAL;
1494 	cap = ee->capture;
1495 	if (!cap || !ee->engine)
1496 		return -ENODEV;
1497 
1498 	guc = &ee->engine->gt->uc.guc;
1499 
1500 	i915_error_printf(ebuf, "global --- GuC Error Capture on %s command stream:\n",
1501 			  ee->engine->name);
1502 
1503 	node = ee->guc_capture_node;
1504 	if (!node) {
1505 		i915_error_printf(ebuf, "  No matching ee-node\n");
1506 		return 0;
1507 	}
1508 
1509 	i915_error_printf(ebuf, "Coverage:  %s\n", grptype[node->is_partial]);
1510 
1511 	for (i = GUC_CAPTURE_LIST_TYPE_GLOBAL; i < GUC_CAPTURE_LIST_TYPE_MAX; ++i) {
1512 		i915_error_printf(ebuf, "  RegListType: %s\n",
1513 				  datatype[i % GUC_CAPTURE_LIST_TYPE_MAX]);
1514 		i915_error_printf(ebuf, "    Owner-Id: %d\n", node->reginfo[i].vfid);
1515 
1516 		switch (i) {
1517 		case GUC_CAPTURE_LIST_TYPE_GLOBAL:
1518 		default:
1519 			break;
1520 		case GUC_CAPTURE_LIST_TYPE_ENGINE_CLASS:
1521 			i915_error_printf(ebuf, "    GuC-Eng-Class: %d\n", node->eng_class);
1522 			i915_error_printf(ebuf, "    i915-Eng-Class: %d\n",
1523 					  guc_class_to_engine_class(node->eng_class));
1524 			break;
1525 		case GUC_CAPTURE_LIST_TYPE_ENGINE_INSTANCE:
1526 			eng = intel_guc_lookup_engine(guc, node->eng_class, node->eng_inst);
1527 			if (eng)
1528 				GCAP_PRINT_INTEL_ENG_INFO(ebuf, eng);
1529 			else
1530 				i915_error_printf(ebuf, "    i915-Eng-Lookup Fail!\n");
1531 			GCAP_PRINT_GUC_INST_INFO(ebuf, node);
1532 			break;
1533 		}
1534 
1535 		numregs = node->reginfo[i].num_regs;
1536 		i915_error_printf(ebuf, "    NumRegs: %d\n", numregs);
1537 		j = 0;
1538 		while (numregs--) {
1539 			regs = node->reginfo[i].regs;
1540 			str = guc_capture_reg_to_str(guc, GUC_CAPTURE_LIST_INDEX_PF, i,
1541 						     node->eng_class, 0, regs[j].offset, &is_ext);
1542 			if (!str)
1543 				i915_error_printf(ebuf, "      REG-0x%08x", regs[j].offset);
1544 			else
1545 				i915_error_printf(ebuf, "      %s", str);
1546 			if (is_ext)
1547 				i915_error_printf(ebuf, "[%ld][%ld]",
1548 					FIELD_GET(GUC_REGSET_STEERING_GROUP, regs[j].flags),
1549 					FIELD_GET(GUC_REGSET_STEERING_INSTANCE, regs[j].flags));
1550 			i915_error_printf(ebuf, ":  0x%08x\n", regs[j].value);
1551 			++j;
1552 		}
1553 	}
1554 	return 0;
1555 }
1556 
1557 #endif //CONFIG_DRM_I915_CAPTURE_ERROR
1558 
intel_guc_capture_free_node(struct intel_engine_coredump * ee)1559 void intel_guc_capture_free_node(struct intel_engine_coredump *ee)
1560 {
1561 	if (!ee || !ee->guc_capture_node)
1562 		return;
1563 
1564 	guc_capture_add_node_to_cachelist(ee->capture, ee->guc_capture_node);
1565 	ee->capture = NULL;
1566 	ee->guc_capture_node = NULL;
1567 }
1568 
intel_guc_capture_get_matching_node(struct intel_gt * gt,struct intel_engine_coredump * ee,struct intel_context * ce)1569 void intel_guc_capture_get_matching_node(struct intel_gt *gt,
1570 					 struct intel_engine_coredump *ee,
1571 					 struct intel_context *ce)
1572 {
1573 	struct __guc_capture_parsed_output *n, *ntmp;
1574 	struct drm_i915_private *i915;
1575 	struct intel_guc *guc;
1576 
1577 	if (!gt || !ee || !ce)
1578 		return;
1579 
1580 	i915 = gt->i915;
1581 	guc = &gt->uc.guc;
1582 	if (!guc->capture)
1583 		return;
1584 
1585 	GEM_BUG_ON(ee->guc_capture_node);
1586 	/*
1587 	 * Look for a matching GuC reported error capture node from
1588 	 * the internal output link-list based on lrca, guc-id and engine
1589 	 * identification.
1590 	 */
1591 	list_for_each_entry_safe(n, ntmp, &guc->capture->outlist, link) {
1592 		if (n->eng_inst == GUC_ID_TO_ENGINE_INSTANCE(ee->engine->guc_id) &&
1593 		    n->eng_class == GUC_ID_TO_ENGINE_CLASS(ee->engine->guc_id) &&
1594 		    n->guc_id && n->guc_id == ce->guc_id.id &&
1595 		    (n->lrca & CTX_GTT_ADDRESS_MASK) && (n->lrca & CTX_GTT_ADDRESS_MASK) ==
1596 		    (ce->lrc.lrca & CTX_GTT_ADDRESS_MASK)) {
1597 			list_del(&n->link);
1598 			ee->guc_capture_node = n;
1599 			ee->capture = guc->capture;
1600 			return;
1601 		}
1602 	}
1603 	drm_dbg(&i915->drm, "GuC capture can't match ee to node\n");
1604 }
1605 
intel_guc_capture_process(struct intel_guc * guc)1606 void intel_guc_capture_process(struct intel_guc *guc)
1607 {
1608 	if (guc->capture)
1609 		__guc_capture_process_output(guc);
1610 }
1611 
1612 static void
guc_capture_free_ads_cache(struct intel_guc_state_capture * gc)1613 guc_capture_free_ads_cache(struct intel_guc_state_capture *gc)
1614 {
1615 	int i, j, k;
1616 	struct __guc_capture_ads_cache *cache;
1617 
1618 	for (i = 0; i < GUC_CAPTURE_LIST_INDEX_MAX; ++i) {
1619 		for (j = 0; j < GUC_CAPTURE_LIST_TYPE_MAX; ++j) {
1620 			for (k = 0; k < GUC_MAX_ENGINE_CLASSES; ++k) {
1621 				cache = &gc->ads_cache[i][j][k];
1622 				if (cache->is_valid)
1623 					kfree(cache->ptr);
1624 			}
1625 		}
1626 	}
1627 	kfree(gc->ads_null_cache);
1628 }
1629 
intel_guc_capture_destroy(struct intel_guc * guc)1630 void intel_guc_capture_destroy(struct intel_guc *guc)
1631 {
1632 	if (!guc->capture)
1633 		return;
1634 
1635 	guc_capture_free_ads_cache(guc->capture);
1636 
1637 	guc_capture_delete_prealloc_nodes(guc);
1638 
1639 	guc_capture_free_extlists(guc->capture->extlists);
1640 	kfree(guc->capture->extlists);
1641 
1642 	kfree(guc->capture);
1643 	guc->capture = NULL;
1644 }
1645 
intel_guc_capture_init(struct intel_guc * guc)1646 int intel_guc_capture_init(struct intel_guc *guc)
1647 {
1648 	guc->capture = kzalloc(sizeof(*guc->capture), GFP_KERNEL);
1649 	if (!guc->capture)
1650 		return -ENOMEM;
1651 
1652 	guc->capture->reglists = guc_capture_get_device_reglist(guc);
1653 
1654 	INIT_LIST_HEAD(&guc->capture->outlist);
1655 	INIT_LIST_HEAD(&guc->capture->cachelist);
1656 
1657 	check_guc_capture_size(guc);
1658 
1659 	return 0;
1660 }
1661