• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2022 Intel Corporation
4  */
5 
6 #include "xe_guc_ads.h"
7 
8 #include <drm/drm_managed.h>
9 
10 #include <generated/xe_wa_oob.h>
11 
12 #include "abi/guc_actions_abi.h"
13 #include "regs/xe_engine_regs.h"
14 #include "regs/xe_gt_regs.h"
15 #include "regs/xe_guc_regs.h"
16 #include "xe_bo.h"
17 #include "xe_gt.h"
18 #include "xe_gt_ccs_mode.h"
19 #include "xe_gt_printk.h"
20 #include "xe_guc.h"
21 #include "xe_guc_ct.h"
22 #include "xe_hw_engine.h"
23 #include "xe_lrc.h"
24 #include "xe_map.h"
25 #include "xe_mmio.h"
26 #include "xe_platform_types.h"
27 #include "xe_uc_fw.h"
28 #include "xe_wa.h"
29 
30 /* Slack of a few additional entries per engine */
31 #define ADS_REGSET_EXTRA_MAX	8
32 
33 static struct xe_guc *
ads_to_guc(struct xe_guc_ads * ads)34 ads_to_guc(struct xe_guc_ads *ads)
35 {
36 	return container_of(ads, struct xe_guc, ads);
37 }
38 
39 static struct xe_gt *
ads_to_gt(struct xe_guc_ads * ads)40 ads_to_gt(struct xe_guc_ads *ads)
41 {
42 	return container_of(ads, struct xe_gt, uc.guc.ads);
43 }
44 
45 static struct xe_device *
ads_to_xe(struct xe_guc_ads * ads)46 ads_to_xe(struct xe_guc_ads *ads)
47 {
48 	return gt_to_xe(ads_to_gt(ads));
49 }
50 
51 static struct iosys_map *
ads_to_map(struct xe_guc_ads * ads)52 ads_to_map(struct xe_guc_ads *ads)
53 {
54 	return &ads->bo->vmap;
55 }
56 
57 /* UM Queue parameters: */
58 #define GUC_UM_QUEUE_SIZE       (SZ_64K)
59 #define GUC_PAGE_RES_TIMEOUT_US (-1)
60 
61 /*
62  * The Additional Data Struct (ADS) has pointers for different buffers used by
63  * the GuC. One single gem object contains the ADS struct itself (guc_ads) and
64  * all the extra buffers indirectly linked via the ADS struct's entries.
65  *
66  * Layout of the ADS blob allocated for the GuC:
67  *
68  *      +---------------------------------------+ <== base
69  *      | guc_ads                               |
70  *      +---------------------------------------+
71  *      | guc_policies                          |
72  *      +---------------------------------------+
73  *      | guc_gt_system_info                    |
74  *      +---------------------------------------+
75  *      | guc_engine_usage                      |
76  *      +---------------------------------------+
77  *      | guc_um_init_params                    |
78  *      +---------------------------------------+ <== static
79  *      | guc_mmio_reg[countA] (engine 0.0)     |
80  *      | guc_mmio_reg[countB] (engine 0.1)     |
81  *      | guc_mmio_reg[countC] (engine 1.0)     |
82  *      |   ...                                 |
83  *      +---------------------------------------+ <== dynamic
84  *      | padding                               |
85  *      +---------------------------------------+ <== 4K aligned
86  *      | golden contexts                       |
87  *      +---------------------------------------+
88  *      | padding                               |
89  *      +---------------------------------------+ <== 4K aligned
90  *      | w/a KLVs                              |
91  *      +---------------------------------------+
92  *      | padding                               |
93  *      +---------------------------------------+ <== 4K aligned
94  *      | capture lists                         |
95  *      +---------------------------------------+
96  *      | padding                               |
97  *      +---------------------------------------+ <== 4K aligned
98  *      | UM queues                             |
99  *      +---------------------------------------+
100  *      | padding                               |
101  *      +---------------------------------------+ <== 4K aligned
102  *      | private data                          |
103  *      +---------------------------------------+
104  *      | padding                               |
105  *      +---------------------------------------+ <== 4K aligned
106  */
107 struct __guc_ads_blob {
108 	struct guc_ads ads;
109 	struct guc_policies policies;
110 	struct guc_gt_system_info system_info;
111 	struct guc_engine_usage engine_usage;
112 	struct guc_um_init_params um_init_params;
113 	/* From here on, location is dynamic! Refer to above diagram. */
114 	struct guc_mmio_reg regset[];
115 } __packed;
116 
117 #define ads_blob_read(ads_, field_) \
118 	xe_map_rd_field(ads_to_xe(ads_), ads_to_map(ads_), 0, \
119 			struct __guc_ads_blob, field_)
120 
121 #define ads_blob_write(ads_, field_, val_)			\
122 	xe_map_wr_field(ads_to_xe(ads_), ads_to_map(ads_), 0,	\
123 			struct __guc_ads_blob, field_, val_)
124 
125 #define info_map_write(xe_, map_, field_, val_) \
126 	xe_map_wr_field(xe_, map_, 0, struct guc_gt_system_info, field_, val_)
127 
128 #define info_map_read(xe_, map_, field_) \
129 	xe_map_rd_field(xe_, map_, 0, struct guc_gt_system_info, field_)
130 
guc_ads_regset_size(struct xe_guc_ads * ads)131 static size_t guc_ads_regset_size(struct xe_guc_ads *ads)
132 {
133 	struct xe_device *xe = ads_to_xe(ads);
134 
135 	xe_assert(xe, ads->regset_size);
136 
137 	return ads->regset_size;
138 }
139 
guc_ads_golden_lrc_size(struct xe_guc_ads * ads)140 static size_t guc_ads_golden_lrc_size(struct xe_guc_ads *ads)
141 {
142 	return PAGE_ALIGN(ads->golden_lrc_size);
143 }
144 
guc_ads_waklv_size(struct xe_guc_ads * ads)145 static u32 guc_ads_waklv_size(struct xe_guc_ads *ads)
146 {
147 	return PAGE_ALIGN(ads->ads_waklv_size);
148 }
149 
guc_ads_capture_size(struct xe_guc_ads * ads)150 static size_t guc_ads_capture_size(struct xe_guc_ads *ads)
151 {
152 	/* FIXME: Allocate a proper capture list */
153 	return PAGE_ALIGN(PAGE_SIZE);
154 }
155 
guc_ads_um_queues_size(struct xe_guc_ads * ads)156 static size_t guc_ads_um_queues_size(struct xe_guc_ads *ads)
157 {
158 	struct xe_device *xe = ads_to_xe(ads);
159 
160 	if (!xe->info.has_usm)
161 		return 0;
162 
163 	return GUC_UM_QUEUE_SIZE * GUC_UM_HW_QUEUE_MAX;
164 }
165 
guc_ads_private_data_size(struct xe_guc_ads * ads)166 static size_t guc_ads_private_data_size(struct xe_guc_ads *ads)
167 {
168 	return PAGE_ALIGN(ads_to_guc(ads)->fw.private_data_size);
169 }
170 
guc_ads_regset_offset(struct xe_guc_ads * ads)171 static size_t guc_ads_regset_offset(struct xe_guc_ads *ads)
172 {
173 	return offsetof(struct __guc_ads_blob, regset);
174 }
175 
guc_ads_golden_lrc_offset(struct xe_guc_ads * ads)176 static size_t guc_ads_golden_lrc_offset(struct xe_guc_ads *ads)
177 {
178 	size_t offset;
179 
180 	offset = guc_ads_regset_offset(ads) +
181 		guc_ads_regset_size(ads);
182 
183 	return PAGE_ALIGN(offset);
184 }
185 
guc_ads_waklv_offset(struct xe_guc_ads * ads)186 static size_t guc_ads_waklv_offset(struct xe_guc_ads *ads)
187 {
188 	u32 offset;
189 
190 	offset = guc_ads_golden_lrc_offset(ads) +
191 		 guc_ads_golden_lrc_size(ads);
192 
193 	return PAGE_ALIGN(offset);
194 }
195 
guc_ads_capture_offset(struct xe_guc_ads * ads)196 static size_t guc_ads_capture_offset(struct xe_guc_ads *ads)
197 {
198 	size_t offset;
199 
200 	offset = guc_ads_waklv_offset(ads) +
201 		 guc_ads_waklv_size(ads);
202 
203 	return PAGE_ALIGN(offset);
204 }
205 
guc_ads_um_queues_offset(struct xe_guc_ads * ads)206 static size_t guc_ads_um_queues_offset(struct xe_guc_ads *ads)
207 {
208 	u32 offset;
209 
210 	offset = guc_ads_capture_offset(ads) +
211 		 guc_ads_capture_size(ads);
212 
213 	return PAGE_ALIGN(offset);
214 }
215 
guc_ads_private_data_offset(struct xe_guc_ads * ads)216 static size_t guc_ads_private_data_offset(struct xe_guc_ads *ads)
217 {
218 	size_t offset;
219 
220 	offset = guc_ads_um_queues_offset(ads) +
221 		guc_ads_um_queues_size(ads);
222 
223 	return PAGE_ALIGN(offset);
224 }
225 
guc_ads_size(struct xe_guc_ads * ads)226 static size_t guc_ads_size(struct xe_guc_ads *ads)
227 {
228 	return guc_ads_private_data_offset(ads) +
229 		guc_ads_private_data_size(ads);
230 }
231 
needs_wa_1607983814(struct xe_device * xe)232 static bool needs_wa_1607983814(struct xe_device *xe)
233 {
234 	return GRAPHICS_VERx100(xe) < 1250;
235 }
236 
calculate_regset_size(struct xe_gt * gt)237 static size_t calculate_regset_size(struct xe_gt *gt)
238 {
239 	struct xe_reg_sr_entry *sr_entry;
240 	unsigned long sr_idx;
241 	struct xe_hw_engine *hwe;
242 	enum xe_hw_engine_id id;
243 	unsigned int count = 0;
244 
245 	for_each_hw_engine(hwe, gt, id)
246 		xa_for_each(&hwe->reg_sr.xa, sr_idx, sr_entry)
247 			count++;
248 
249 	count += ADS_REGSET_EXTRA_MAX * XE_NUM_HW_ENGINES;
250 
251 	if (needs_wa_1607983814(gt_to_xe(gt)))
252 		count += LNCFCMOCS_REG_COUNT;
253 
254 	return count * sizeof(struct guc_mmio_reg);
255 }
256 
engine_enable_mask(struct xe_gt * gt,enum xe_engine_class class)257 static u32 engine_enable_mask(struct xe_gt *gt, enum xe_engine_class class)
258 {
259 	struct xe_hw_engine *hwe;
260 	enum xe_hw_engine_id id;
261 	u32 mask = 0;
262 
263 	for_each_hw_engine(hwe, gt, id)
264 		if (hwe->class == class)
265 			mask |= BIT(hwe->instance);
266 
267 	return mask;
268 }
269 
calculate_golden_lrc_size(struct xe_guc_ads * ads)270 static size_t calculate_golden_lrc_size(struct xe_guc_ads *ads)
271 {
272 	struct xe_gt *gt = ads_to_gt(ads);
273 	size_t total_size = 0, alloc_size, real_size;
274 	int class;
275 
276 	for (class = 0; class < XE_ENGINE_CLASS_MAX; ++class) {
277 		if (!engine_enable_mask(gt, class))
278 			continue;
279 
280 		real_size = xe_gt_lrc_size(gt, class);
281 		alloc_size = PAGE_ALIGN(real_size);
282 		total_size += alloc_size;
283 	}
284 
285 	return total_size;
286 }
287 
guc_waklv_enable_one_word(struct xe_guc_ads * ads,enum xe_guc_klv_ids klv_id,u32 value,u32 * offset,u32 * remain)288 static void guc_waklv_enable_one_word(struct xe_guc_ads *ads,
289 				      enum xe_guc_klv_ids klv_id,
290 				      u32 value,
291 				      u32 *offset, u32 *remain)
292 {
293 	u32 size;
294 	u32 klv_entry[] = {
295 		/* 16:16 key/length */
296 		FIELD_PREP(GUC_KLV_0_KEY, klv_id) |
297 		FIELD_PREP(GUC_KLV_0_LEN, 1),
298 		value,
299 		/* 1 dword data */
300 	};
301 
302 	size = sizeof(klv_entry);
303 
304 	if (*remain < size) {
305 		drm_warn(&ads_to_xe(ads)->drm,
306 			 "w/a klv buffer too small to add klv id %d\n", klv_id);
307 	} else {
308 		xe_map_memcpy_to(ads_to_xe(ads), ads_to_map(ads), *offset,
309 				 klv_entry, size);
310 		*offset += size;
311 		*remain -= size;
312 	}
313 }
314 
guc_waklv_enable_simple(struct xe_guc_ads * ads,enum xe_guc_klv_ids klv_id,u32 * offset,u32 * remain)315 static void guc_waklv_enable_simple(struct xe_guc_ads *ads,
316 				    enum xe_guc_klv_ids klv_id, u32 *offset, u32 *remain)
317 {
318 	u32 klv_entry[] = {
319 		/* 16:16 key/length */
320 		FIELD_PREP(GUC_KLV_0_KEY, klv_id) |
321 		FIELD_PREP(GUC_KLV_0_LEN, 0),
322 		/* 0 dwords data */
323 	};
324 	u32 size;
325 
326 	size = sizeof(klv_entry);
327 
328 	if (xe_gt_WARN(ads_to_gt(ads), *remain < size,
329 		       "w/a klv buffer too small to add klv id %d\n", klv_id))
330 		return;
331 
332 	xe_map_memcpy_to(ads_to_xe(ads), ads_to_map(ads), *offset,
333 			 klv_entry, size);
334 	*offset += size;
335 	*remain -= size;
336 }
337 
guc_waklv_init(struct xe_guc_ads * ads)338 static void guc_waklv_init(struct xe_guc_ads *ads)
339 {
340 	struct xe_gt *gt = ads_to_gt(ads);
341 	u64 addr_ggtt;
342 	u32 offset, remain, size;
343 
344 	offset = guc_ads_waklv_offset(ads);
345 	remain = guc_ads_waklv_size(ads);
346 
347 	if (XE_WA(gt, 14019882105))
348 		guc_waklv_enable_simple(ads,
349 					GUC_WORKAROUND_KLV_BLOCK_INTERRUPTS_WHEN_MGSR_BLOCKED,
350 					&offset, &remain);
351 	if (XE_WA(gt, 18024947630))
352 		guc_waklv_enable_simple(ads,
353 					GUC_WORKAROUND_KLV_ID_GAM_PFQ_SHADOW_TAIL_POLLING,
354 					&offset, &remain);
355 	if (XE_WA(gt, 16022287689))
356 		guc_waklv_enable_simple(ads,
357 					GUC_WORKAROUND_KLV_ID_DISABLE_MTP_DURING_ASYNC_COMPUTE,
358 					&offset, &remain);
359 
360 	/*
361 	 * On RC6 exit, GuC will write register 0xB04 with the default value provided. As of now,
362 	 * the default value for this register is determined to be 0xC40. This could change in the
363 	 * future, so GuC depends on KMD to send it the correct value.
364 	 */
365 	if (XE_WA(gt, 13011645652))
366 		guc_waklv_enable_one_word(ads,
367 					  GUC_WA_KLV_NP_RD_WRITE_TO_CLEAR_RCSM_AT_CGP_LATE_RESTORE,
368 					  0xC40,
369 					  &offset, &remain);
370 
371 	if (XE_WA(gt, 14022293748) || XE_WA(gt, 22019794406))
372 		guc_waklv_enable_simple(ads,
373 					GUC_WORKAROUND_KLV_ID_BACK_TO_BACK_RCS_ENGINE_RESET,
374 					&offset, &remain);
375 
376 	size = guc_ads_waklv_size(ads) - remain;
377 	if (!size)
378 		return;
379 
380 	offset = guc_ads_waklv_offset(ads);
381 	addr_ggtt = xe_bo_ggtt_addr(ads->bo) + offset;
382 
383 	ads_blob_write(ads, ads.wa_klv_addr_lo, lower_32_bits(addr_ggtt));
384 	ads_blob_write(ads, ads.wa_klv_addr_hi, upper_32_bits(addr_ggtt));
385 	ads_blob_write(ads, ads.wa_klv_size, size);
386 }
387 
calculate_waklv_size(struct xe_guc_ads * ads)388 static int calculate_waklv_size(struct xe_guc_ads *ads)
389 {
390 	/*
391 	 * A single page is both the minimum size possible and
392 	 * is sufficiently large enough for all current platforms.
393 	 */
394 	return SZ_4K;
395 }
396 
397 #define MAX_GOLDEN_LRC_SIZE	(SZ_4K * 64)
398 
xe_guc_ads_init(struct xe_guc_ads * ads)399 int xe_guc_ads_init(struct xe_guc_ads *ads)
400 {
401 	struct xe_device *xe = ads_to_xe(ads);
402 	struct xe_gt *gt = ads_to_gt(ads);
403 	struct xe_tile *tile = gt_to_tile(gt);
404 	struct xe_bo *bo;
405 
406 	ads->golden_lrc_size = calculate_golden_lrc_size(ads);
407 	ads->regset_size = calculate_regset_size(gt);
408 	ads->ads_waklv_size = calculate_waklv_size(ads);
409 
410 	bo = xe_managed_bo_create_pin_map(xe, tile, guc_ads_size(ads) + MAX_GOLDEN_LRC_SIZE,
411 					  XE_BO_FLAG_SYSTEM |
412 					  XE_BO_FLAG_GGTT |
413 					  XE_BO_FLAG_GGTT_INVALIDATE);
414 	if (IS_ERR(bo))
415 		return PTR_ERR(bo);
416 
417 	ads->bo = bo;
418 
419 	return 0;
420 }
421 
422 /**
423  * xe_guc_ads_init_post_hwconfig - initialize ADS post hwconfig load
424  * @ads: Additional data structures object
425  *
426  * Recalcuate golden_lrc_size & regset_size as the number hardware engines may
427  * have changed after the hwconfig was loaded. Also verify the new sizes fit in
428  * the already allocated ADS buffer object.
429  *
430  * Return: 0 on success, negative error code on error.
431  */
xe_guc_ads_init_post_hwconfig(struct xe_guc_ads * ads)432 int xe_guc_ads_init_post_hwconfig(struct xe_guc_ads *ads)
433 {
434 	struct xe_gt *gt = ads_to_gt(ads);
435 	u32 prev_regset_size = ads->regset_size;
436 
437 	xe_gt_assert(gt, ads->bo);
438 
439 	ads->golden_lrc_size = calculate_golden_lrc_size(ads);
440 	ads->regset_size = calculate_regset_size(gt);
441 
442 	xe_gt_assert(gt, ads->golden_lrc_size +
443 		     (ads->regset_size - prev_regset_size) <=
444 		     MAX_GOLDEN_LRC_SIZE);
445 
446 	return 0;
447 }
448 
guc_policies_init(struct xe_guc_ads * ads)449 static void guc_policies_init(struct xe_guc_ads *ads)
450 {
451 	struct xe_device *xe = ads_to_xe(ads);
452 	u32 global_flags = 0;
453 
454 	ads_blob_write(ads, policies.dpc_promote_time,
455 		       GLOBAL_POLICY_DEFAULT_DPC_PROMOTE_TIME_US);
456 	ads_blob_write(ads, policies.max_num_work_items,
457 		       GLOBAL_POLICY_MAX_NUM_WI);
458 
459 	if (xe->wedged.mode == 2)
460 		global_flags |= GLOBAL_POLICY_DISABLE_ENGINE_RESET;
461 
462 	ads_blob_write(ads, policies.global_flags, global_flags);
463 	ads_blob_write(ads, policies.is_valid, 1);
464 }
465 
fill_engine_enable_masks(struct xe_gt * gt,struct iosys_map * info_map)466 static void fill_engine_enable_masks(struct xe_gt *gt,
467 				     struct iosys_map *info_map)
468 {
469 	struct xe_device *xe = gt_to_xe(gt);
470 
471 	info_map_write(xe, info_map, engine_enabled_masks[GUC_RENDER_CLASS],
472 		       engine_enable_mask(gt, XE_ENGINE_CLASS_RENDER));
473 	info_map_write(xe, info_map, engine_enabled_masks[GUC_BLITTER_CLASS],
474 		       engine_enable_mask(gt, XE_ENGINE_CLASS_COPY));
475 	info_map_write(xe, info_map, engine_enabled_masks[GUC_VIDEO_CLASS],
476 		       engine_enable_mask(gt, XE_ENGINE_CLASS_VIDEO_DECODE));
477 	info_map_write(xe, info_map,
478 		       engine_enabled_masks[GUC_VIDEOENHANCE_CLASS],
479 		       engine_enable_mask(gt, XE_ENGINE_CLASS_VIDEO_ENHANCE));
480 	info_map_write(xe, info_map, engine_enabled_masks[GUC_COMPUTE_CLASS],
481 		       engine_enable_mask(gt, XE_ENGINE_CLASS_COMPUTE));
482 	info_map_write(xe, info_map, engine_enabled_masks[GUC_GSC_OTHER_CLASS],
483 		       engine_enable_mask(gt, XE_ENGINE_CLASS_OTHER));
484 }
485 
486 /*
487  * Write the offsets corresponding to the golden LRCs. The actual data is
488  * populated later by guc_golden_lrc_populate()
489  */
guc_golden_lrc_init(struct xe_guc_ads * ads)490 static void guc_golden_lrc_init(struct xe_guc_ads *ads)
491 {
492 	struct xe_device *xe = ads_to_xe(ads);
493 	struct xe_gt *gt = ads_to_gt(ads);
494 	struct iosys_map info_map = IOSYS_MAP_INIT_OFFSET(ads_to_map(ads),
495 			offsetof(struct __guc_ads_blob, system_info));
496 	size_t alloc_size, real_size;
497 	u32 addr_ggtt, offset;
498 	int class;
499 
500 	offset = guc_ads_golden_lrc_offset(ads);
501 	addr_ggtt = xe_bo_ggtt_addr(ads->bo) + offset;
502 
503 	for (class = 0; class < XE_ENGINE_CLASS_MAX; ++class) {
504 		u8 guc_class;
505 
506 		guc_class = xe_engine_class_to_guc_class(class);
507 
508 		if (!info_map_read(xe, &info_map,
509 				   engine_enabled_masks[guc_class]))
510 			continue;
511 
512 		real_size = xe_gt_lrc_size(gt, class);
513 		alloc_size = PAGE_ALIGN(real_size);
514 
515 		/*
516 		 * This interface is slightly confusing. We need to pass the
517 		 * base address of the full golden context and the size of just
518 		 * the engine state, which is the section of the context image
519 		 * that starts after the execlists LRC registers. This is
520 		 * required to allow the GuC to restore just the engine state
521 		 * when a watchdog reset occurs.
522 		 * We calculate the engine state size by removing the size of
523 		 * what comes before it in the context image (which is identical
524 		 * on all engines).
525 		 */
526 		ads_blob_write(ads, ads.eng_state_size[guc_class],
527 			       real_size - xe_lrc_skip_size(xe));
528 		ads_blob_write(ads, ads.golden_context_lrca[guc_class],
529 			       addr_ggtt);
530 
531 		addr_ggtt += alloc_size;
532 	}
533 }
534 
guc_mapping_table_init_invalid(struct xe_gt * gt,struct iosys_map * info_map)535 static void guc_mapping_table_init_invalid(struct xe_gt *gt,
536 					   struct iosys_map *info_map)
537 {
538 	struct xe_device *xe = gt_to_xe(gt);
539 	unsigned int i, j;
540 
541 	/* Table must be set to invalid values for entries not used */
542 	for (i = 0; i < GUC_MAX_ENGINE_CLASSES; ++i)
543 		for (j = 0; j < GUC_MAX_INSTANCES_PER_CLASS; ++j)
544 			info_map_write(xe, info_map, mapping_table[i][j],
545 				       GUC_MAX_INSTANCES_PER_CLASS);
546 }
547 
guc_mapping_table_init(struct xe_gt * gt,struct iosys_map * info_map)548 static void guc_mapping_table_init(struct xe_gt *gt,
549 				   struct iosys_map *info_map)
550 {
551 	struct xe_device *xe = gt_to_xe(gt);
552 	struct xe_hw_engine *hwe;
553 	enum xe_hw_engine_id id;
554 
555 	guc_mapping_table_init_invalid(gt, info_map);
556 
557 	for_each_hw_engine(hwe, gt, id) {
558 		u8 guc_class;
559 
560 		guc_class = xe_engine_class_to_guc_class(hwe->class);
561 		info_map_write(xe, info_map,
562 			       mapping_table[guc_class][hwe->logical_instance],
563 			       hwe->instance);
564 	}
565 }
566 
guc_capture_list_init(struct xe_guc_ads * ads)567 static void guc_capture_list_init(struct xe_guc_ads *ads)
568 {
569 	int i, j;
570 	u32 addr = xe_bo_ggtt_addr(ads->bo) + guc_ads_capture_offset(ads);
571 
572 	/* FIXME: Populate a proper capture list */
573 	for (i = 0; i < GUC_CAPTURE_LIST_INDEX_MAX; i++) {
574 		for (j = 0; j < GUC_MAX_ENGINE_CLASSES; j++) {
575 			ads_blob_write(ads, ads.capture_instance[i][j], addr);
576 			ads_blob_write(ads, ads.capture_class[i][j], addr);
577 		}
578 
579 		ads_blob_write(ads, ads.capture_global[i], addr);
580 	}
581 }
582 
guc_mmio_regset_write_one(struct xe_guc_ads * ads,struct iosys_map * regset_map,struct xe_reg reg,unsigned int n_entry)583 static void guc_mmio_regset_write_one(struct xe_guc_ads *ads,
584 				      struct iosys_map *regset_map,
585 				      struct xe_reg reg,
586 				      unsigned int n_entry)
587 {
588 	struct guc_mmio_reg entry = {
589 		.offset = reg.addr,
590 		.flags = reg.masked ? GUC_REGSET_MASKED : 0,
591 	};
592 
593 	xe_map_memcpy_to(ads_to_xe(ads), regset_map, n_entry * sizeof(entry),
594 			 &entry, sizeof(entry));
595 }
596 
guc_mmio_regset_write(struct xe_guc_ads * ads,struct iosys_map * regset_map,struct xe_hw_engine * hwe)597 static unsigned int guc_mmio_regset_write(struct xe_guc_ads *ads,
598 					  struct iosys_map *regset_map,
599 					  struct xe_hw_engine *hwe)
600 {
601 	struct xe_device *xe = ads_to_xe(ads);
602 	struct xe_hw_engine *hwe_rcs_reset_domain =
603 		xe_gt_any_hw_engine_by_reset_domain(hwe->gt, XE_ENGINE_CLASS_RENDER);
604 	struct xe_reg_sr_entry *entry;
605 	unsigned long idx;
606 	unsigned int count = 0;
607 	const struct {
608 		struct xe_reg reg;
609 		bool skip;
610 	} *e, extra_regs[] = {
611 		{ .reg = RING_MODE(hwe->mmio_base),			},
612 		{ .reg = RING_HWS_PGA(hwe->mmio_base),			},
613 		{ .reg = RING_IMR(hwe->mmio_base),			},
614 		{ .reg = RCU_MODE, .skip = hwe != hwe_rcs_reset_domain	},
615 		{ .reg = CCS_MODE,
616 		  .skip = hwe != hwe_rcs_reset_domain || !xe_gt_ccs_mode_enabled(hwe->gt) },
617 	};
618 	u32 i;
619 
620 	BUILD_BUG_ON(ARRAY_SIZE(extra_regs) > ADS_REGSET_EXTRA_MAX);
621 
622 	xa_for_each(&hwe->reg_sr.xa, idx, entry)
623 		guc_mmio_regset_write_one(ads, regset_map, entry->reg, count++);
624 
625 	for (e = extra_regs; e < extra_regs + ARRAY_SIZE(extra_regs); e++) {
626 		if (e->skip)
627 			continue;
628 
629 		guc_mmio_regset_write_one(ads, regset_map, e->reg, count++);
630 	}
631 
632 	/* Wa_1607983814 */
633 	if (needs_wa_1607983814(xe) && hwe->class == XE_ENGINE_CLASS_RENDER) {
634 		for (i = 0; i < LNCFCMOCS_REG_COUNT; i++) {
635 			guc_mmio_regset_write_one(ads, regset_map,
636 						  XELP_LNCFCMOCS(i), count++);
637 		}
638 	}
639 
640 	return count;
641 }
642 
guc_mmio_reg_state_init(struct xe_guc_ads * ads)643 static void guc_mmio_reg_state_init(struct xe_guc_ads *ads)
644 {
645 	size_t regset_offset = guc_ads_regset_offset(ads);
646 	struct xe_gt *gt = ads_to_gt(ads);
647 	struct xe_hw_engine *hwe;
648 	enum xe_hw_engine_id id;
649 	u32 addr = xe_bo_ggtt_addr(ads->bo) + regset_offset;
650 	struct iosys_map regset_map = IOSYS_MAP_INIT_OFFSET(ads_to_map(ads),
651 							    regset_offset);
652 	unsigned int regset_used = 0;
653 
654 	for_each_hw_engine(hwe, gt, id) {
655 		unsigned int count;
656 		u8 gc;
657 
658 		/*
659 		 * 1. Write all MMIO entries for this exec queue to the table. No
660 		 * need to worry about fused-off engines and when there are
661 		 * entries in the regset: the reg_state_list has been zero'ed
662 		 * by xe_guc_ads_populate()
663 		 */
664 		count = guc_mmio_regset_write(ads, &regset_map, hwe);
665 		if (!count)
666 			continue;
667 
668 		/*
669 		 * 2. Record in the header (ads.reg_state_list) the address
670 		 * location and number of entries
671 		 */
672 		gc = xe_engine_class_to_guc_class(hwe->class);
673 		ads_blob_write(ads, ads.reg_state_list[gc][hwe->instance].address, addr);
674 		ads_blob_write(ads, ads.reg_state_list[gc][hwe->instance].count, count);
675 
676 		addr += count * sizeof(struct guc_mmio_reg);
677 		iosys_map_incr(&regset_map, count * sizeof(struct guc_mmio_reg));
678 
679 		regset_used += count * sizeof(struct guc_mmio_reg);
680 	}
681 
682 	xe_gt_assert(gt, regset_used <= ads->regset_size);
683 }
684 
guc_um_init_params(struct xe_guc_ads * ads)685 static void guc_um_init_params(struct xe_guc_ads *ads)
686 {
687 	u32 um_queue_offset = guc_ads_um_queues_offset(ads);
688 	u64 base_dpa;
689 	u32 base_ggtt;
690 	int i;
691 
692 	base_ggtt = xe_bo_ggtt_addr(ads->bo) + um_queue_offset;
693 	base_dpa = xe_bo_main_addr(ads->bo, PAGE_SIZE) + um_queue_offset;
694 
695 	for (i = 0; i < GUC_UM_HW_QUEUE_MAX; ++i) {
696 		ads_blob_write(ads, um_init_params.queue_params[i].base_dpa,
697 			       base_dpa + (i * GUC_UM_QUEUE_SIZE));
698 		ads_blob_write(ads, um_init_params.queue_params[i].base_ggtt_address,
699 			       base_ggtt + (i * GUC_UM_QUEUE_SIZE));
700 		ads_blob_write(ads, um_init_params.queue_params[i].size_in_bytes,
701 			       GUC_UM_QUEUE_SIZE);
702 	}
703 
704 	ads_blob_write(ads, um_init_params.page_response_timeout_in_us,
705 		       GUC_PAGE_RES_TIMEOUT_US);
706 }
707 
guc_doorbell_init(struct xe_guc_ads * ads)708 static void guc_doorbell_init(struct xe_guc_ads *ads)
709 {
710 	struct xe_device *xe = ads_to_xe(ads);
711 	struct xe_gt *gt = ads_to_gt(ads);
712 
713 	if (GRAPHICS_VER(xe) >= 12 && !IS_DGFX(xe)) {
714 		u32 distdbreg =
715 			xe_mmio_read32(gt, DIST_DBS_POPULATED);
716 
717 		ads_blob_write(ads,
718 			       system_info.generic_gt_sysinfo[GUC_GENERIC_GT_SYSINFO_DOORBELL_COUNT_PER_SQIDI],
719 			       REG_FIELD_GET(DOORBELLS_PER_SQIDI_MASK, distdbreg) + 1);
720 	}
721 }
722 
723 /**
724  * xe_guc_ads_populate_minimal - populate minimal ADS
725  * @ads: Additional data structures object
726  *
727  * This function populates a minimal ADS that does not support submissions but
728  * enough so the GuC can load and the hwconfig table can be read.
729  */
xe_guc_ads_populate_minimal(struct xe_guc_ads * ads)730 void xe_guc_ads_populate_minimal(struct xe_guc_ads *ads)
731 {
732 	struct xe_gt *gt = ads_to_gt(ads);
733 	struct iosys_map info_map = IOSYS_MAP_INIT_OFFSET(ads_to_map(ads),
734 			offsetof(struct __guc_ads_blob, system_info));
735 	u32 base = xe_bo_ggtt_addr(ads->bo);
736 
737 	xe_gt_assert(gt, ads->bo);
738 
739 	xe_map_memset(ads_to_xe(ads), ads_to_map(ads), 0, 0, ads->bo->size);
740 	guc_policies_init(ads);
741 	guc_golden_lrc_init(ads);
742 	guc_mapping_table_init_invalid(gt, &info_map);
743 	guc_doorbell_init(ads);
744 
745 	ads_blob_write(ads, ads.scheduler_policies, base +
746 		       offsetof(struct __guc_ads_blob, policies));
747 	ads_blob_write(ads, ads.gt_system_info, base +
748 		       offsetof(struct __guc_ads_blob, system_info));
749 	ads_blob_write(ads, ads.private_data, base +
750 		       guc_ads_private_data_offset(ads));
751 }
752 
xe_guc_ads_populate(struct xe_guc_ads * ads)753 void xe_guc_ads_populate(struct xe_guc_ads *ads)
754 {
755 	struct xe_device *xe = ads_to_xe(ads);
756 	struct xe_gt *gt = ads_to_gt(ads);
757 	struct iosys_map info_map = IOSYS_MAP_INIT_OFFSET(ads_to_map(ads),
758 			offsetof(struct __guc_ads_blob, system_info));
759 	u32 base = xe_bo_ggtt_addr(ads->bo);
760 
761 	xe_gt_assert(gt, ads->bo);
762 
763 	xe_map_memset(ads_to_xe(ads), ads_to_map(ads), 0, 0, ads->bo->size);
764 	guc_policies_init(ads);
765 	fill_engine_enable_masks(gt, &info_map);
766 	guc_mmio_reg_state_init(ads);
767 	guc_golden_lrc_init(ads);
768 	guc_mapping_table_init(gt, &info_map);
769 	guc_capture_list_init(ads);
770 	guc_doorbell_init(ads);
771 	guc_waklv_init(ads);
772 
773 	if (xe->info.has_usm) {
774 		guc_um_init_params(ads);
775 		ads_blob_write(ads, ads.um_init_data, base +
776 			       offsetof(struct __guc_ads_blob, um_init_params));
777 	}
778 
779 	ads_blob_write(ads, ads.scheduler_policies, base +
780 		       offsetof(struct __guc_ads_blob, policies));
781 	ads_blob_write(ads, ads.gt_system_info, base +
782 		       offsetof(struct __guc_ads_blob, system_info));
783 	ads_blob_write(ads, ads.private_data, base +
784 		       guc_ads_private_data_offset(ads));
785 }
786 
787 /*
788  * After the golden LRC's are recorded for each engine class by the first
789  * submission, copy them to the ADS, as initialized earlier by
790  * guc_golden_lrc_init().
791  */
guc_golden_lrc_populate(struct xe_guc_ads * ads)792 static void guc_golden_lrc_populate(struct xe_guc_ads *ads)
793 {
794 	struct xe_device *xe = ads_to_xe(ads);
795 	struct xe_gt *gt = ads_to_gt(ads);
796 	struct iosys_map info_map = IOSYS_MAP_INIT_OFFSET(ads_to_map(ads),
797 			offsetof(struct __guc_ads_blob, system_info));
798 	size_t total_size = 0, alloc_size, real_size;
799 	u32 offset;
800 	int class;
801 
802 	offset = guc_ads_golden_lrc_offset(ads);
803 
804 	for (class = 0; class < XE_ENGINE_CLASS_MAX; ++class) {
805 		u8 guc_class;
806 
807 		guc_class = xe_engine_class_to_guc_class(class);
808 
809 		if (!info_map_read(xe, &info_map,
810 				   engine_enabled_masks[guc_class]))
811 			continue;
812 
813 		xe_gt_assert(gt, gt->default_lrc[class]);
814 
815 		real_size = xe_gt_lrc_size(gt, class);
816 		alloc_size = PAGE_ALIGN(real_size);
817 		total_size += alloc_size;
818 
819 		xe_map_memcpy_to(xe, ads_to_map(ads), offset,
820 				 gt->default_lrc[class], real_size);
821 
822 		offset += alloc_size;
823 	}
824 
825 	xe_gt_assert(gt, total_size == ads->golden_lrc_size);
826 }
827 
xe_guc_ads_populate_post_load(struct xe_guc_ads * ads)828 void xe_guc_ads_populate_post_load(struct xe_guc_ads *ads)
829 {
830 	guc_golden_lrc_populate(ads);
831 }
832 
guc_ads_action_update_policies(struct xe_guc_ads * ads,u32 policy_offset)833 static int guc_ads_action_update_policies(struct xe_guc_ads *ads, u32 policy_offset)
834 {
835 	struct  xe_guc_ct *ct = &ads_to_guc(ads)->ct;
836 	u32 action[] = {
837 		XE_GUC_ACTION_GLOBAL_SCHED_POLICY_CHANGE,
838 		policy_offset
839 	};
840 
841 	return xe_guc_ct_send(ct, action, ARRAY_SIZE(action), 0, 0);
842 }
843 
844 /**
845  * xe_guc_ads_scheduler_policy_toggle_reset - Toggle reset policy
846  * @ads: Additional data structures object
847  *
848  * This function update the GuC's engine reset policy based on wedged.mode.
849  *
850  * Return: 0 on success, and negative error code otherwise.
851  */
xe_guc_ads_scheduler_policy_toggle_reset(struct xe_guc_ads * ads)852 int xe_guc_ads_scheduler_policy_toggle_reset(struct xe_guc_ads *ads)
853 {
854 	struct xe_device *xe = ads_to_xe(ads);
855 	struct xe_gt *gt = ads_to_gt(ads);
856 	struct xe_tile *tile = gt_to_tile(gt);
857 	struct guc_policies *policies;
858 	struct xe_bo *bo;
859 	int ret = 0;
860 
861 	policies = kmalloc(sizeof(*policies), GFP_KERNEL);
862 	if (!policies)
863 		return -ENOMEM;
864 
865 	policies->dpc_promote_time = ads_blob_read(ads, policies.dpc_promote_time);
866 	policies->max_num_work_items = ads_blob_read(ads, policies.max_num_work_items);
867 	policies->is_valid = 1;
868 	if (xe->wedged.mode == 2)
869 		policies->global_flags |= GLOBAL_POLICY_DISABLE_ENGINE_RESET;
870 	else
871 		policies->global_flags &= ~GLOBAL_POLICY_DISABLE_ENGINE_RESET;
872 
873 	bo = xe_managed_bo_create_from_data(xe, tile, policies, sizeof(struct guc_policies),
874 					    XE_BO_FLAG_VRAM_IF_DGFX(tile) |
875 					    XE_BO_FLAG_GGTT);
876 	if (IS_ERR(bo)) {
877 		ret = PTR_ERR(bo);
878 		goto out;
879 	}
880 
881 	ret = guc_ads_action_update_policies(ads, xe_bo_ggtt_addr(bo));
882 out:
883 	kfree(policies);
884 	return ret;
885 }
886