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