File: xe_sriov_provisioning.c

package info (click to toggle)
intel-gpu-tools 2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 62,024 kB
  • sloc: xml: 769,439; ansic: 348,692; python: 8,307; yacc: 2,781; perl: 1,196; sh: 1,178; lex: 487; asm: 227; makefile: 27; lisp: 11
file content (765 lines) | stat: -rw-r--r-- 24,622 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
// SPDX-License-Identifier: MIT
/*
 * Copyright(c) 2024 Intel Corporation. All rights reserved.
 */

#include <errno.h>

#include "igt_core.h"
#include "igt_debugfs.h"
#include "igt_sriov_device.h"
#include "intel_chipset.h"
#include "linux_scaffold.h"
#include "xe/xe_query.h"
#include "xe/xe_mmio.h"
#include "xe/xe_sriov_debugfs.h"
#include "xe/xe_sriov_provisioning.h"

/**
 * xe_sriov_shared_res_to_string:
 * @key: The shared resource of type enum xe_sriov_shared_res
 *
 * Converts a shared resource enum to its corresponding string
 * representation. It is useful for logging and debugging purposes.
 *
 * Return: A string representing the shared resource key.
 */
const char *xe_sriov_shared_res_to_string(enum xe_sriov_shared_res res)
{
	switch (res) {
	case XE_SRIOV_SHARED_RES_CONTEXTS:
		return "contexts";
	case XE_SRIOV_SHARED_RES_DOORBELLS:
		return "doorbells";
	case XE_SRIOV_SHARED_RES_GGTT:
		return "ggtt";
	case XE_SRIOV_SHARED_RES_LMEM:
		return "lmem";
	}

	return NULL;
}

#define PRE_1250_IP_VER_GGTT_PTE_VFID_MASK	GENMASK_ULL(4, 2)
#define GGTT_PTE_VFID_MASK			GENMASK_ULL(11, 2)
#define GGTT_PTE_VFID_SHIFT			2
#define GUC_GGTT_TOP				0xFEE00000
#define MAX_WOPCM_SIZE				SZ_8M
#define START_PTE_OFFSET			(MAX_WOPCM_SIZE / SZ_4K * sizeof(xe_ggtt_pte_t))
#define MAX_PTE_OFFSET				(GUC_GGTT_TOP / SZ_4K * sizeof(xe_ggtt_pte_t))

static uint64_t get_vfid_mask(int fd)
{
	uint16_t dev_id = intel_get_drm_devid(fd);

	return (intel_graphics_ver(dev_id) >= IP_VER(12, 50)) ?
		GGTT_PTE_VFID_MASK : PRE_1250_IP_VER_GGTT_PTE_VFID_MASK;
}

#define MAX_DEBUG_ENTRIES 70

static int append_range(struct xe_sriov_provisioned_range **ranges,
			unsigned int *nr_ranges, unsigned int vf_id,
			uint32_t start, uint32_t end)
{
	struct xe_sriov_provisioned_range *new_ranges;

	new_ranges = realloc(*ranges,
			     (*nr_ranges + 1) * sizeof(struct xe_sriov_provisioned_range));
	if (!new_ranges) {
		free(*ranges);
		*ranges = NULL;
		*nr_ranges = 0;
		return -ENOMEM;
	}

	*ranges = new_ranges;
	if (*nr_ranges < MAX_DEBUG_ENTRIES)
		igt_debug("Found VF%u GGTT range [%#x-%#x] num_ptes=%ld\n",
			  vf_id, start, end,
			  (end - start + sizeof(xe_ggtt_pte_t)) /
			  sizeof(xe_ggtt_pte_t));
	(*ranges)[*nr_ranges].vf_id = vf_id;
	(*ranges)[*nr_ranges].start = start;
	(*ranges)[*nr_ranges].end = end;
	(*nr_ranges)++;

	return 0;
}

/**
 * xe_sriov_find_ggtt_provisioned_pte_offsets - Find GGTT provisioned PTE offsets
 * @pf_fd: File descriptor for the Physical Function
 * @gt: GT identifier
 * @mmio: Pointer to the MMIO structure
 * @ranges: Pointer to the array of provisioned ranges
 * @nr_ranges: Pointer to the number of provisioned ranges
 *
 * Searches for GGTT provisioned PTE ranges for each VF and populates
 * the provided ranges array with the start and end offsets of each range.
 * The number of ranges found is stored in nr_ranges.
 *
 * Reads the GGTT PTEs and identifies the VF ID associated with each PTE.
 * It then groups contiguous PTEs with the same VF ID into ranges.
 * The ranges are dynamically allocated and must be freed by the caller.
 * The start and end offsets in each range are inclusive.
 *
 * Returns 0 on success, or a negative error code on failure.
 */
int xe_sriov_find_ggtt_provisioned_pte_offsets(int pf_fd, int gt, struct xe_mmio *mmio,
					       struct xe_sriov_provisioned_range **ranges,
					       unsigned int *nr_ranges)
{
	uint64_t vfid_mask = get_vfid_mask(pf_fd);
	unsigned int vf_id, current_vf_id = -1;
	uint32_t current_start = 0;
	uint32_t current_end = 0;
	xe_ggtt_pte_t pte;
	int ret;

	*ranges = NULL;
	*nr_ranges = 0;

	for (uint32_t offset = START_PTE_OFFSET; offset < MAX_PTE_OFFSET;
	     offset += sizeof(xe_ggtt_pte_t)) {
		pte = xe_mmio_ggtt_read(mmio, gt, offset);
		vf_id = (pte & vfid_mask) >> GGTT_PTE_VFID_SHIFT;

		if (vf_id != current_vf_id) {
			if (current_vf_id != -1) {
				/* End the current range and append it */
				ret = append_range(ranges, nr_ranges, current_vf_id,
						   current_start, current_end);
				if (ret < 0)
					return ret;
			}
			/* Start a new range */
			current_vf_id = vf_id;
			current_start = offset;
		}
		current_end = offset;
	}

	if (current_vf_id != -1) {
		/* Append the last range */
		ret = append_range(ranges, nr_ranges, current_vf_id,
				   current_start, current_end);
		if (ret < 0)
			return ret;
	}

	if (*nr_ranges > MAX_DEBUG_ENTRIES)
		igt_debug("Ranges output trimmed to first %u entries out of %u\n",
			  MAX_DEBUG_ENTRIES, *nr_ranges);

	return 0;
}

/**
 * xe_sriov_shared_res_attr_name - Retrieve the attribute name for a shared resource
 * @res: Shared resource type (see enum xe_sriov_shared_res)
 * @vf_num: VF number (1-based) or 0 for PF
 *
 * Returns the attribute name corresponding to the specified
 * shared resource type and VF number. For VF (vf_num > 0), the "quota"
 * attribute name is returned (e.g., "contexts_quota"). For PF (vf_num == 0),
 * the "spare" attribute name is returned (e.g., "contexts_spare").
 *
 * Return:
 * The attribute name as a string if the resource type is valid.
 * NULL if the resource type is invalid.
 */
const char *xe_sriov_shared_res_attr_name(enum xe_sriov_shared_res res,
					  unsigned int vf_num)
{
	switch (res) {
	case XE_SRIOV_SHARED_RES_CONTEXTS:
		return vf_num ? "contexts_quota" : "contexts_spare";
	case XE_SRIOV_SHARED_RES_DOORBELLS:
		return vf_num ? "doorbells_quota" : "doorbells_spare";
	case XE_SRIOV_SHARED_RES_GGTT:
		return vf_num ? "ggtt_quota" : "ggtt_spare";
	case XE_SRIOV_SHARED_RES_LMEM:
		return vf_num ? "lmem_quota" : "lmem_spare";
	}

	return NULL;
}

/**
 * __xe_sriov_pf_get_shared_res_attr - Read shared resource attribute
 * @pf: PF device file descriptor
 * @res: Shared resource type (see enum xe_sriov_shared_res)
 * @vf_num: VF number (1-based) or 0 for PF
 * @gt_num: GT number
 * @value: Pointer to store the read attribute value
 *
 * Reads the specified shared resource attribute for the given PF device @pf,
 * VF number @vf_num, and GT @gt_num. The attribute depends on @vf_num:
 * - For VF (vf_num > 0), reads the "quota" attribute.
 * - For PF (vf_num == 0), reads the "spare" attribute.
 *
 *
 * Return: 0 on success, negative error code on failure.
 */
int __xe_sriov_pf_get_shared_res_attr(int pf, enum xe_sriov_shared_res res,
				      unsigned int vf_num, unsigned int gt_num,
				      uint64_t *value)
{
	return __xe_sriov_pf_debugfs_get_u64(pf, vf_num, gt_num,
					     xe_sriov_shared_res_attr_name(res, vf_num),
					     value);
}

/**
 * xe_sriov_pf_get_shared_res_attr - Read shared resource attribute
 * @pf: PF device file descriptor
 * @res: Shared resource type (see enum xe_sriov_shared_res)
 * @vf_num: VF number (1-based) or 0 for PF
 * @gt_num: GT number
 *
 * A throwing version of __xe_sriov_pf_get_shared_res_attr().
 * Instead of returning an error code, it returns the quota value and asserts
 * in case of an error.
 *
 * Return: The value for the given shared resource attribute.
 *         Asserts in case of failure.
 */
uint64_t xe_sriov_pf_get_shared_res_attr(int pf, enum xe_sriov_shared_res res,
					 unsigned int vf_num,
					 unsigned int gt_num)
{
	uint64_t value;

	igt_fail_on(__xe_sriov_pf_get_shared_res_attr(pf, res, vf_num, gt_num, &value));

	return value;
}

/**
 * __xe_sriov_pf_set_shared_res_attr - Set a shared resource attribute
 * @pf: PF device file descriptor
 * @res: Shared resource type (see enum xe_sriov_shared_res)
 * @vf_num: VF number (1-based) or 0 for PF
 * @gt_num: GT number
 * @value: Value to set for the shared resource attribute
 *
 * Sets the specified shared resource attribute for the given PF device @pf,
 * VF number @vf_num, and GT @gt_num. The attribute depends on @vf_num:
 * - For VF (vf_num > 0), reads the "quota" attribute.
 * - For PF (vf_num == 0), reads the "spare" attribute.
 *
 * Return: 0 on success, negative error code on failure.
 */
int __xe_sriov_pf_set_shared_res_attr(int pf, enum xe_sriov_shared_res res,
				      unsigned int vf_num, unsigned int gt_num,
				      uint64_t value)
{
	return __xe_sriov_pf_debugfs_set_u64(pf, vf_num, gt_num,
					     xe_sriov_shared_res_attr_name(res, vf_num),
					     value);
}

/**
 * xe_sriov_pf_set_shared_res_attr - Set the shared resource attribute value
 * @pf: PF device file descriptor
 * @res: Shared resource type (see enum xe_sriov_shared_res)
 * @vf_num: VF number (1-based) or 0 for PF
 * @gt_num: GT number
 * @value: Value to set
 *
 * A throwing version of __xe_sriov_pf_set_shared_res_attr().
 * Instead of returning an error code, it asserts in case of an error.
 */
void xe_sriov_pf_set_shared_res_attr(int pf, enum xe_sriov_shared_res res,
				     unsigned int vf_num, unsigned int gt_num,
				     uint64_t value)
{
	igt_fail_on(__xe_sriov_pf_set_shared_res_attr(pf, res, vf_num, gt_num, value));
}

/**
 * xe_sriov_is_shared_res_provisionable - Check if a shared resource is provisionable
 * @pf: PF device file descriptor
 * @res: Shared resource type (see enum xe_sriov_shared_res)
 * @gt_num: GT number
 *
 * Determines whether a specified shared resource can be provisioned.
 *
 * Return: true if the shared resource is provisionable, false otherwise.
 */
bool xe_sriov_is_shared_res_provisionable(int pf, enum xe_sriov_shared_res res,
					  unsigned int gt_num)
{
	if (res == XE_SRIOV_SHARED_RES_LMEM)
		return xe_has_vram(pf) && !xe_is_media_gt(pf, gt_num);
	else if (res == XE_SRIOV_SHARED_RES_GGTT)
		return !xe_is_media_gt(pf, gt_num);

	return true;
}

/**
 * __xe_sriov_pf_get_provisioned_quota - Get VF's provisioned quota.
 * @pf: PF device file descriptor
 * @res: Shared resource type (see enum xe_sriov_shared_res)
 * @vf_num: VF number (1-based)
 * @gt_num: GT number
 * @value: Pointer to store the read value
 *
 * Gets VF's provisioning value for the specified shared resource @res,
 * VF number @vf_num and GT number @gt_num.
 *
 * Return: 0 on success, negative error code on failure.
 */
int __xe_sriov_pf_get_provisioned_quota(int pf, enum xe_sriov_shared_res res,
					unsigned int vf_num, unsigned int gt_num,
					uint64_t *value)
{
	struct xe_sriov_provisioned_range *ranges;
	int ret;

	ret = xe_sriov_pf_debugfs_read_check_ranges(pf, res, gt_num, &ranges,
						    igt_sriov_get_enabled_vfs(pf));
	if (igt_debug_on_f(ret, "%s: Failed ranges check on GT%u (%d)\n",
			   xe_sriov_debugfs_provisioned_attr_name(res), gt_num, ret))
		return ret;

	*value = ranges[vf_num - 1].end - ranges[vf_num - 1].start + 1;

	free(ranges);

	return 0;
}

/**
 * xe_sriov_pf_get_provisioned_quota - Get VF's provisioned quota.
 * @pf: PF device file descriptor
 * @res: Shared resource type (see enum xe_sriov_shared_res)
 * @vf_num: VF number (1-based)
 * @gt_num: GT number
 *
 * A throwing version of __xe_sriov_pf_get_provisioned_quota().
 * Instead of returning an error code, it returns the quota value and asserts
 * in case of an error.
 *
 * Return: The provisioned quota for the given shared resource.
 *         Asserts in case of failure.
 */
uint64_t xe_sriov_pf_get_provisioned_quota(int pf, enum xe_sriov_shared_res res,
					   unsigned int vf_num, unsigned int gt_num)
{
	uint64_t value;

	igt_fail_on(__xe_sriov_pf_get_provisioned_quota(pf, res, vf_num, gt_num, &value));

	return value;
}

/**
 * __xe_sriov_get_exec_quantum_ms - Read the execution quantum in milliseconds for a given VF
 * @pf: PF device file descriptor
 * @vf_num: VF number (1-based) or 0 for PF
 * @gt_num: GT number
 * @value: Pointer to store the read value
 *
 * Reads the execution quantum in milliseconds for the given PF device @pf,
 * VF number @vf_num on GT @gt_num.
 *
 * Return: 0 on success, negative error code on failure.
 */
int __xe_sriov_get_exec_quantum_ms(int pf, unsigned int vf_num,
				   unsigned int gt_num, uint32_t *value)
{
	return __xe_sriov_pf_debugfs_get_u32(pf, vf_num, gt_num, "exec_quantum_ms", value);
}

/**
 * xe_sriov_get_exec_quantum_ms - Get the execution quantum in milliseconds for a given VF
 * @pf: PF device file descriptor
 * @vf_num: VF number (1-based) or 0 for PF
 * @gt_num: GT number
 *
 * A throwing version of __xe_sriov_get_exec_quantum_ms().
 * Instead of returning an error code, it returns the value read and
 * asserts in case of an error.
 *
 * Return: Execution quantum in milliseconds assigned to a given VF. Asserts in case of failure.
 */
uint32_t xe_sriov_get_exec_quantum_ms(int pf, unsigned int vf_num,
				      unsigned int gt_num)
{
	uint32_t value;

	igt_fail_on(__xe_sriov_get_exec_quantum_ms(pf, vf_num, gt_num, &value));

	return value;
}

/**
 * __xe_sriov_set_exec_quantum_ms - Set the execution quantum in milliseconds for a given VF
 * @pf: PF device file descriptor
 * @vf_num: VF number (1-based) or 0 for PF
 * @gt_num: GT number
 * @value: Value to set
 *
 * Sets the execution quantum in milliseconds for the given PF device @pf,
 * VF number @vf_num on GT @gt_num.
 *
 * Return: 0 on success, negative error code on failure.
 */
int __xe_sriov_set_exec_quantum_ms(int pf, unsigned int vf_num,
				   unsigned int gt_num, uint32_t value)
{
	return __xe_sriov_pf_debugfs_set_u32(pf, vf_num, gt_num, "exec_quantum_ms", value);
}

/**
 * xe_sriov_set_exec_quantum_ms - Set the execution quantum in milliseconds for a given VF
 * @pf: PF device file descriptor
 * @vf_num: VF number (1-based) or 0 for PF
 * @gt_num: GT number
 * @value: Value to set
 *
 * A throwing version of __xe_sriov_set_exec_quantum_ms().
 * Instead of returning an error code, it asserts in case of an error.
 */
void xe_sriov_set_exec_quantum_ms(int pf, unsigned int vf_num,
				  unsigned int gt_num, uint32_t value)
{
	igt_fail_on(__xe_sriov_set_exec_quantum_ms(pf, vf_num, gt_num, value));
}

/**
 * __xe_sriov_get_preempt_timeout_us - Get the preemption timeout in microseconds for a given VF
 * @pf: PF device file descriptor
 * @vf_num: VF number (1-based) or 0 for PF
 * @gt_num: GT number
 * @value: Pointer to store the read value
 *
 * Reads the preemption timeout in microseconds for the given PF device @pf,
 * VF number @vf_num on GT @gt_num.
 *
 * Return: 0 on success, negative error code on failure.
 */
int __xe_sriov_get_preempt_timeout_us(int pf, unsigned int vf_num,
				      unsigned int gt_num, uint32_t *value)
{
	return __xe_sriov_pf_debugfs_get_u32(pf, vf_num, gt_num, "preempt_timeout_us", value);
}

/**
 * xe_sriov_get_preempt_timeout_us - Get the preemption timeout in microseconds for a given VF
 * @pf: PF device file descriptor
 * @vf_num: VF number (1-based) or 0 for PF
 * @gt_num: GT number
 *
 * A throwing version of __xe_sriov_get_preempt_timeout_us().
 * Instead of returning an error code, it returns the value read and
 * asserts in case of an error.
 *
 * Return: Preemption timeout in microseconds assigned to a given VF.
 * Asserts in case of failure.
 */
uint32_t xe_sriov_get_preempt_timeout_us(int pf, unsigned int vf_num,
					 unsigned int gt_num)
{
	uint32_t value;

	igt_fail_on(__xe_sriov_get_preempt_timeout_us(pf, vf_num, gt_num, &value));

	return value;
}

/**
 * __xe_sriov_set_preempt_timeout_us - Set the preemption timeout in microseconds for a given VF
 * @pf: PF device file descriptor
 * @vf_num: VF number (1-based) or 0 for PF
 * @gt_num: GT number
 * @value: Value to set
 *
 * Sets the preemption timeout in microseconds for the given PF device @pf,
 * VF number @vf_num on GT @gt_num.
 *
 * Return: 0 on success, negative error code on failure.
 */
int __xe_sriov_set_preempt_timeout_us(int pf, unsigned int vf_num,
				      unsigned int gt_num, uint32_t value)
{
	return __xe_sriov_pf_debugfs_set_u32(pf, vf_num, gt_num, "preempt_timeout_us", value);
}

/**
 * xe_sriov_set_preempt_timeout_us - Set the preemption timeout in microseconds for a given VF
 * @pf: PF device file descriptor
 * @vf_num: VF number (1-based) or 0 for PF
 * @gt_num: GT number
 * @value: Value to set
 *
 * A throwing version of __xe_sriov_set_preempt_timeout_us().
 * Instead of returning an error code, it asserts in case of an error.
 */
void xe_sriov_set_preempt_timeout_us(int pf, unsigned int vf_num,
				     unsigned int gt_num, uint32_t value)
{
	igt_fail_on(__xe_sriov_set_preempt_timeout_us(pf, vf_num, gt_num, value));
}

/**
 * __xe_sriov_get_engine_reset - Get the engine reset policy status for a given GT
 * @pf: PF device file descriptor
 * @gt_num: GT number
 * @value: Pointer to store the read engine reset policy status
 *
 * Reads the engine reset status for the given PF device @pf on GT @gt_num.
 *
 * Return: 0 on success, negative error code on failure.
 */
int __xe_sriov_get_engine_reset(int pf, unsigned int gt_num, bool *value)
{
	return __xe_sriov_pf_debugfs_get_boolean(pf, 0, gt_num, "reset_engine", value);
}

/**
 * xe_sriov_get_engine_reset - Get the engine reset policy status for a given GT
 * @pf: PF device file descriptor
 * @gt_num: GT number
 *
 * A throwing version of __xe_sriov_get_engine_reset().
 * Instead of returning an error code, it returns the engine reset status
 * and asserts in case of an error.
 *
 * Return: The engine reset status for the given GT.
 *         Asserts in case of failure.
 */
bool xe_sriov_get_engine_reset(int pf, unsigned int gt_num)
{
	bool value;

	igt_fail_on(__xe_sriov_get_engine_reset(pf, gt_num, &value));

	return value;
}

/**
 * __xe_sriov_set_engine_reset - Set the engine reset policy for a given GT
 * @pf: PF device file descriptor
 * @gt_num: GT number
 * @value: Engine reset policy status to set
 *
 * Sets the engine reset policy for the given PF device @pf on GT @gt_num.
 *
 * Return: 0 on success, negative error code on failure.
 */
int __xe_sriov_set_engine_reset(int pf, unsigned int gt_num, bool value)
{
	return __xe_sriov_pf_debugfs_set_boolean(pf, 0, gt_num, "reset_engine", value);
}

/**
 * xe_sriov_set_engine_reset - Set the engine reset policy for a given GT
 * @pf: PF device file descriptor
 * @gt_num: GT number
 * @value: Engine reset policy status to set
 *
 * A throwing version of __xe_sriov_set_engine_reset().
 * Instead of returning an error code, it asserts in case of an error.
 */
void xe_sriov_set_engine_reset(int pf, unsigned int gt_num, bool value)
{
	igt_fail_on(__xe_sriov_set_engine_reset(pf, gt_num, value));
}

/**
 * __xe_sriov_get_sched_if_idle - Get the scheduling if idle policy for a given GT
 * @pf: PF device file descriptor
 * @gt_num: GT number
 * @value: Pointer to store the read scheduling if idle policy status
 *
 * Reads the scheduling if idle policy status for the given PF device @pf on GT @gt_num.
 *
 * Return: 0 on success, negative error code on failure.
 */
int __xe_sriov_get_sched_if_idle(int pf, unsigned int gt_num, bool *value)
{
	return __xe_sriov_pf_debugfs_get_boolean(pf, 0, gt_num, "sched_if_idle", value);
}

/**
 * xe_sriov_get_sched_if_idle - Get the scheduling if idle policy for a given GT
 * @pf: PF device file descriptor
 * @gt_num: GT number
 *
 * A throwing version of __xe_sriov_get_sched_if_idle().
 * Instead of returning an error code, it returns the scheduling if idle policy status
 * and asserts in case of an error.
 *
 * Return: The scheduling if idle status for the given GT.
 *         Asserts in case of failure.
 */
bool xe_sriov_get_sched_if_idle(int pf, unsigned int gt_num)
{
	bool value;

	igt_fail_on(__xe_sriov_get_sched_if_idle(pf, gt_num, &value));

	return value;
}

/**
 * __xe_sriov_set_sched_if_idle - Set the scheduling if idle policy status for a given GT
 * @pf: PF device file descriptor
 * @gt_num: GT number
 * @value: Scheduling if idle policy status to set
 *
 * Sets the scheduling if idle policy status for the given PF device @pf on GT @gt_num.
 *
 * Return: 0 on success, negative error code on failure.
 */
int __xe_sriov_set_sched_if_idle(int pf, unsigned int gt_num, bool value)
{
	return __xe_sriov_pf_debugfs_set_boolean(pf, 0, gt_num, "sched_if_idle", value);
}

/**
 * xe_sriov_set_sched_if_idle - Set the scheduling if idle status policy for a given GT
 * @pf: PF device file descriptor
 * @gt_num: GT number
 * @value: Scheduling if idle policy status to set
 *
 * A throwing version of __xe_sriov_set_sched_if_idle().
 * Instead of returning an error code, it asserts in case of an error.
 */
void xe_sriov_set_sched_if_idle(int pf, unsigned int gt_num, bool value)
{
	igt_fail_on(__xe_sriov_set_sched_if_idle(pf, gt_num, value));
}

/**
 * __xe_sriov_get_sched_priority - Get the scheduling priority for a given VF
 * @pf: PF device file descriptor
 * @vf_num: VF number (1-based) or 0 for PF
 * @gt_num: GT number
 * @value: Pointer to store the read scheduling priority
 *
 * Reads the scheduling priority for the given PF device @pf,
 * VF number @vf_num on GT @gt_num.
 *
 * Return: 0 on success, negative error code on failure.
 */
int __xe_sriov_get_sched_priority(int pf, unsigned int vf_num,
				  unsigned int gt_num,
				  enum xe_sriov_sched_priority *value)
{
	uint32_t priority;
	int ret;

	ret = __xe_sriov_pf_debugfs_get_u32(pf, vf_num, gt_num, "sched_priority", &priority);
	if (ret)
		return ret;

	if (priority > XE_SRIOV_SCHED_PRIORITY_HIGH)
		return -ERANGE;

	*value = priority;

	return 0;
}

/**
 * xe_sriov_get_sched_priority - Get the scheduling priority for a given VF
 * @pf: PF device file descriptor
 * @vf_num: VF number (1-based) or 0 for PF
 * @gt_num: GT number
 *
 * A throwing version of __xe_sriov_get_sched_priority().
 * Instead of returning an error code, it returns the scheduling priority
 * and asserts in case of an error.
 *
 * Return: The scheduling priority for the given VF and GT.
 *         Asserts in case of failure.
 */
enum xe_sriov_sched_priority
xe_sriov_get_sched_priority(int pf, unsigned int vf_num, unsigned int gt_num)
{
	enum xe_sriov_sched_priority priority;

	igt_fail_on(__xe_sriov_get_sched_priority(pf, vf_num, gt_num, &priority));

	return priority;
}

/**
 * __xe_sriov_set_sched_priority - Set the scheduling priority for a given VF
 * @pf: PF device file descriptor
 * @vf_num: VF number (1-based) or 0 for PF
 * @gt_num: GT number
 * @value: Scheduling priority to set (enum xe_sriov_sched_priority)
 *
 * Sets the scheduling priority for the given PF device @pf, VF number @vf_num on GT @gt_num.
 *
 * Return: 0 on success, negative error code on failure.
 */
int __xe_sriov_set_sched_priority(int pf, unsigned int vf_num, unsigned int gt_num,
				  enum xe_sriov_sched_priority value)
{
	return __xe_sriov_pf_debugfs_set_u32(pf, vf_num, gt_num, "sched_priority", value);
}

/**
 * xe_sriov_set_sched_priority - Set the scheduling priority for a given VF
 * @pf: PF device file descriptor
 * @vf_num: VF number (1-based) or 0 for PF
 * @gt_num: GT number
 * @value: Scheduling priority to set (enum xe_sriov_sched_priority)
 *
 * A throwing version of __xe_sriov_set_sched_priority().
 * Instead of returning an error code, it asserts in case of an error.
 */
void xe_sriov_set_sched_priority(int pf, unsigned int vf_num, unsigned int gt_num,
				 enum xe_sriov_sched_priority value)
{
	igt_fail_on(__xe_sriov_set_sched_priority(pf, vf_num, gt_num, value));
}

/**
 * xe_sriov_require_default_scheduling_attributes - Ensure default SR-IOV scheduling attributes
 * @pf_fd: PF device file descriptor
 *
 * Skips the current test if non-default SR-IOV scheduling attributes are set.
 *
 * Default scheduling attributes are as follows for each VF and PF:
 * - exec_quantum_ms equals zero (meaning infinity)
 * - preempt_timeout_us equals zero (meaning infinity)
 * - sched_if_idle equals false
 * - reset_engine equals false
 * - sched_priority equals XE_SRIOV_SCHED_PRIORITY_LOW
 */
void xe_sriov_require_default_scheduling_attributes(int pf)
{
	unsigned int totalvfs = igt_sriov_get_total_vfs(pf);
	enum xe_sriov_sched_priority sched_priority;
	bool sched_if_idle, reset_engine;
	uint32_t eq, pt;
	unsigned int gt;

	xe_for_each_gt(pf, gt) {
		igt_skip_on(__xe_sriov_get_sched_if_idle(pf, gt, &sched_if_idle));
		igt_require_f(!sched_if_idle, "sched_if_idle != false on gt%u\n", gt);
		igt_skip_on(__xe_sriov_get_engine_reset(pf, gt, &reset_engine));
		igt_require_f(!reset_engine, "reset_engine != false on gt%u\n", gt);

		for (unsigned int vf_num = 0; vf_num <= totalvfs; ++vf_num) {
			igt_skip_on(__xe_sriov_get_exec_quantum_ms(pf, vf_num, gt, &eq));
			igt_require_f(eq == 0, "exec_quantum_ms != 0 on gt%u/VF%u\n", gt, vf_num);

			igt_skip_on(__xe_sriov_get_preempt_timeout_us(pf, vf_num, gt, &pt));
			igt_require_f(pt == 0, "preempt_timeout_us != 0 on gt%u/VF%u\n",
				      gt, vf_num);

			igt_skip_on(__xe_sriov_get_sched_priority(pf, vf_num, gt, &sched_priority));
			igt_require_f(sched_priority == XE_SRIOV_SCHED_PRIORITY_LOW,
				      "sched_priority != LOW on gt%u/VF%u\n", gt, vf_num);
		}
	}
}