summaryrefslogtreecommitdiff
path: root/drivers/gpu/drm/xe/xe_sriov_pf_provision.c
blob: 0ec7ea83f12acff4520c4e53fe3af004dfc2c4d6 (plain) (blame)
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
// SPDX-License-Identifier: MIT
/*
 * Copyright © 2025 Intel Corporation
 */

#include "xe_assert.h"
#include "xe_device.h"
#include "xe_gt_sriov_pf_config.h"
#include "xe_gt_sriov_pf_policy.h"
#include "xe_lmtt.h"
#include "xe_sriov.h"
#include "xe_sriov_pf_helpers.h"
#include "xe_sriov_pf_provision.h"
#include "xe_sriov_pf_provision_types.h"
#include "xe_sriov_printk.h"

static const char *mode_to_string(enum xe_sriov_provisioning_mode mode)
{
	switch (mode) {
	case XE_SRIOV_PROVISIONING_MODE_AUTO:
		return "auto";
	case XE_SRIOV_PROVISIONING_MODE_CUSTOM:
		return "custom";
	default:
		return "<invalid>";
	}
}

static bool pf_auto_provisioning_mode(struct xe_device *xe)
{
	xe_assert(xe, IS_SRIOV_PF(xe));

	return xe->sriov.pf.provision.mode == XE_SRIOV_PROVISIONING_MODE_AUTO;
}

static int pf_provision_vfs(struct xe_device *xe, unsigned int num_vfs)
{
	struct xe_gt *gt;
	unsigned int id;
	int result = 0;
	int err;

	for_each_gt(gt, xe, id) {
		err = xe_gt_sriov_pf_config_set_fair_sched(gt, num_vfs);
		result = result ?: err;
		err = xe_gt_sriov_pf_config_set_fair(gt, VFID(1), num_vfs);
		result = result ?: err;
	}

	return result;
}

static void pf_unprovision_vfs(struct xe_device *xe, unsigned int num_vfs)
{
	struct xe_gt *gt;
	unsigned int id;
	unsigned int n;

	for_each_gt(gt, xe, id)
		for (n = 1; n <= num_vfs; n++)
			xe_gt_sriov_pf_config_release(gt, n, true);
}

static void pf_unprovision_all_vfs(struct xe_device *xe)
{
	pf_unprovision_vfs(xe, xe_sriov_pf_get_totalvfs(xe));
}

/**
 * xe_sriov_pf_provision_vfs() - Provision VFs in auto-mode.
 * @xe: the PF &xe_device
 * @num_vfs: the number of VFs to auto-provision
 *
 * This function can only be called on PF.
 *
 * Return: 0 on success or a negative error code on failure.
 */
int xe_sriov_pf_provision_vfs(struct xe_device *xe, unsigned int num_vfs)
{
	xe_assert(xe, IS_SRIOV_PF(xe));

	if (!pf_auto_provisioning_mode(xe))
		return 0;

	return pf_provision_vfs(xe, num_vfs);
}

/**
 * xe_sriov_pf_unprovision_vfs() - Unprovision VFs in auto-mode.
 * @xe: the PF &xe_device
 * @num_vfs: the number of VFs to unprovision
 *
 * This function can only be called on PF.
 *
 * Return: 0 on success or a negative error code on failure.
 */
int xe_sriov_pf_unprovision_vfs(struct xe_device *xe, unsigned int num_vfs)
{
	xe_assert(xe, IS_SRIOV_PF(xe));

	if (!pf_auto_provisioning_mode(xe))
		return 0;

	pf_unprovision_vfs(xe, num_vfs);
	return 0;
}

static int pf_reprovision_default(struct xe_device *xe)
{
	struct xe_gt *gt;
	unsigned int id;
	int result = 0;
	int err;

	guard(mutex)(xe_sriov_pf_master_mutex(xe));

	for_each_gt(gt, xe, id) {
		err = xe_gt_sriov_pf_policy_set_sched_if_idle_locked(gt, false);
		result = result ?: err;
		err = xe_gt_sriov_pf_config_set_exec_quantum_locked(gt, PFID, 0);
		result = result ?: err;
		err = xe_gt_sriov_pf_config_set_preempt_timeout_locked(gt, PFID, 0);
		result = result ?: err;
	}

	return result;
}

/**
 * xe_sriov_pf_reprovision_default() - Reprovision default PF in auto-mode.
 * @xe: the PF &xe_device
 *
 * This function can only be called on PF.
 *
 * Return: 0 on success or a negative error code on failure.
 */
int xe_sriov_pf_reprovision_default(struct xe_device *xe)
{
	xe_assert(xe, IS_SRIOV_PF(xe));

	if (!pf_auto_provisioning_mode(xe))
		return 0;

	return pf_reprovision_default(xe);
}

/**
 * xe_sriov_pf_provision_set_mode() - Change VFs provision mode.
 * @xe: the PF &xe_device
 * @mode: the new VFs provisioning mode
 *
 * When changing from AUTO to CUSTOM mode, any already allocated VFs resources
 * will remain allocated and will not be released upon VFs disabling.
 *
 * When changing back to AUTO mode, if VFs are not enabled, already allocated
 * VFs resources will be immediately released. If VFs are still enabled, such
 * mode change is rejected.
 *
 * This function can only be called on PF.
 *
 * Return: 0 on success or a negative error code on failure.
 */
int xe_sriov_pf_provision_set_mode(struct xe_device *xe, enum xe_sriov_provisioning_mode mode)
{
	xe_assert(xe, IS_SRIOV_PF(xe));

	if (mode == xe->sriov.pf.provision.mode)
		return 0;

	if (mode == XE_SRIOV_PROVISIONING_MODE_AUTO) {
		if (xe_sriov_pf_num_vfs(xe)) {
			xe_sriov_dbg(xe, "can't restore %s: VFs must be disabled!\n",
				     mode_to_string(mode));
			return -EBUSY;
		}
		pf_unprovision_all_vfs(xe);
	}

	xe_sriov_dbg(xe, "mode %s changed to %s by %ps\n",
		     mode_to_string(xe->sriov.pf.provision.mode),
		     mode_to_string(mode), __builtin_return_address(0));
	xe->sriov.pf.provision.mode = mode;
	return 0;
}

/**
 * xe_sriov_pf_provision_bulk_apply_eq() - Change execution quantum for all VFs and PF.
 * @xe: the PF &xe_device
 * @eq: execution quantum in [ms] to set
 *
 * Change execution quantum (EQ) provisioning on all tiles/GTs.
 *
 * This function can only be called on PF.
 *
 * Return: 0 on success or a negative error code on failure.
 */
int xe_sriov_pf_provision_bulk_apply_eq(struct xe_device *xe, u32 eq)
{
	struct xe_gt *gt;
	unsigned int id;
	int result = 0;
	int err;

	guard(mutex)(xe_sriov_pf_master_mutex(xe));

	for_each_gt(gt, xe, id) {
		err = xe_gt_sriov_pf_config_bulk_set_exec_quantum_locked(gt, eq);
		result = result ?: err;
	}

	return result;
}

/**
 * xe_sriov_pf_provision_apply_vf_eq() - Change VF's execution quantum.
 * @xe: the PF &xe_device
 * @vfid: the VF identifier
 * @eq: execution quantum in [ms] to set
 *
 * Change VF's execution quantum (EQ) provisioning on all tiles/GTs.
 *
 * This function can only be called on PF.
 *
 * Return: 0 on success or a negative error code on failure.
 */
int xe_sriov_pf_provision_apply_vf_eq(struct xe_device *xe, unsigned int vfid, u32 eq)
{
	struct xe_gt *gt;
	unsigned int id;
	int result = 0;
	int err;

	guard(mutex)(xe_sriov_pf_master_mutex(xe));

	for_each_gt(gt, xe, id) {
		err = xe_gt_sriov_pf_config_set_exec_quantum_locked(gt, vfid, eq);
		result = result ?: err;
	}

	return result;
}

static int pf_report_unclean(struct xe_gt *gt, unsigned int vfid,
			     const char *what, u32 found, u32 expected)
{
	char name[8];

	xe_sriov_dbg(gt_to_xe(gt), "%s on GT%u has %s=%u (expected %u)\n",
		     xe_sriov_function_name(vfid, name, sizeof(name)),
		     gt->info.id, what, found, expected);
	return -EUCLEAN;
}

/**
 * xe_sriov_pf_provision_query_vf_eq() - Query VF's execution quantum.
 * @xe: the PF &xe_device
 * @vfid: the VF identifier
 * @eq: placeholder for the returned execution quantum in [ms]
 *
 * Query VF's execution quantum (EQ) provisioning from all tiles/GTs.
 * If values across tiles/GTs are inconsistent then -EUCLEAN error will be returned.
 *
 * This function can only be called on PF.
 *
 * Return: 0 on success or a negative error code on failure.
 */
int xe_sriov_pf_provision_query_vf_eq(struct xe_device *xe, unsigned int vfid, u32 *eq)
{
	struct xe_gt *gt;
	unsigned int id;
	int count = 0;
	u32 value;

	guard(mutex)(xe_sriov_pf_master_mutex(xe));

	for_each_gt(gt, xe, id) {
		value = xe_gt_sriov_pf_config_get_exec_quantum_locked(gt, vfid);
		if (!count++)
			*eq = value;
		else if (value != *eq)
			return pf_report_unclean(gt, vfid, "EQ", value, *eq);
	}

	return !count ? -ENODATA : 0;
}

/**
 * xe_sriov_pf_provision_bulk_apply_pt() - Change preemption timeout for all VFs and PF.
 * @xe: the PF &xe_device
 * @pt: preemption timeout in [us] to set
 *
 * Change preemption timeout (PT) provisioning on all tiles/GTs.
 *
 * This function can only be called on PF.
 *
 * Return: 0 on success or a negative error code on failure.
 */
int xe_sriov_pf_provision_bulk_apply_pt(struct xe_device *xe, u32 pt)
{
	struct xe_gt *gt;
	unsigned int id;
	int result = 0;
	int err;

	guard(mutex)(xe_sriov_pf_master_mutex(xe));

	for_each_gt(gt, xe, id) {
		err = xe_gt_sriov_pf_config_bulk_set_preempt_timeout_locked(gt, pt);
		result = result ?: err;
	}

	return result;
}

/**
 * xe_sriov_pf_provision_apply_vf_pt() - Change VF's preemption timeout.
 * @xe: the PF &xe_device
 * @vfid: the VF identifier
 * @pt: preemption timeout in [us] to set
 *
 * Change VF's preemption timeout (PT) provisioning on all tiles/GTs.
 *
 * This function can only be called on PF.
 *
 * Return: 0 on success or a negative error code on failure.
 */
int xe_sriov_pf_provision_apply_vf_pt(struct xe_device *xe, unsigned int vfid, u32 pt)
{
	struct xe_gt *gt;
	unsigned int id;
	int result = 0;
	int err;

	guard(mutex)(xe_sriov_pf_master_mutex(xe));

	for_each_gt(gt, xe, id) {
		err = xe_gt_sriov_pf_config_set_preempt_timeout_locked(gt, vfid, pt);
		result = result ?: err;
	}

	return result;
}

/**
 * xe_sriov_pf_provision_query_vf_pt() - Query VF's preemption timeout.
 * @xe: the PF &xe_device
 * @vfid: the VF identifier
 * @pt: placeholder for the returned preemption timeout in [us]
 *
 * Query VF's preemption timeout (PT) provisioning from all tiles/GTs.
 * If values across tiles/GTs are inconsistent then -EUCLEAN error will be returned.
 *
 * This function can only be called on PF.
 *
 * Return: 0 on success or a negative error code on failure.
 */
int xe_sriov_pf_provision_query_vf_pt(struct xe_device *xe, unsigned int vfid, u32 *pt)
{
	struct xe_gt *gt;
	unsigned int id;
	int count = 0;
	u32 value;

	guard(mutex)(xe_sriov_pf_master_mutex(xe));

	for_each_gt(gt, xe, id) {
		value = xe_gt_sriov_pf_config_get_preempt_timeout_locked(gt, vfid);
		if (!count++)
			*pt = value;
		else if (value != *pt)
			return pf_report_unclean(gt, vfid, "PT", value, *pt);
	}

	return !count ? -ENODATA : 0;
}

/**
 * xe_sriov_pf_provision_bulk_apply_priority() - Change scheduling priority of all VFs and PF.
 * @xe: the PF &xe_device
 * @prio: scheduling priority to set
 *
 * Change the scheduling priority provisioning on all tiles/GTs.
 *
 * This function can only be called on PF.
 *
 * Return: 0 on success or a negative error code on failure.
 */
int xe_sriov_pf_provision_bulk_apply_priority(struct xe_device *xe, u32 prio)
{
	bool sched_if_idle;
	struct xe_gt *gt;
	unsigned int id;
	int result = 0;
	int err;

	/*
	 * Currently, priority changes that involves VFs are only allowed using
	 * the 'sched_if_idle' policy KLV, so only LOW and NORMAL are supported.
	 */
	xe_assert(xe, prio < GUC_SCHED_PRIORITY_HIGH);
	sched_if_idle = prio == GUC_SCHED_PRIORITY_NORMAL;

	for_each_gt(gt, xe, id) {
		err = xe_gt_sriov_pf_policy_set_sched_if_idle(gt, sched_if_idle);
		result = result ?: err;
	}

	return result;
}

/**
 * xe_sriov_pf_provision_apply_vf_priority() - Change VF's scheduling priority.
 * @xe: the PF &xe_device
 * @vfid: the VF identifier
 * @prio: scheduling priority to set
 *
 * Change VF's scheduling priority provisioning on all tiles/GTs.
 *
 * This function can only be called on PF.
 *
 * Return: 0 on success or a negative error code on failure.
 */
int xe_sriov_pf_provision_apply_vf_priority(struct xe_device *xe, unsigned int vfid, u32 prio)
{
	struct xe_gt *gt;
	unsigned int id;
	int result = 0;
	int err;

	for_each_gt(gt, xe, id) {
		err = xe_gt_sriov_pf_config_set_sched_priority(gt, vfid, prio);
		result = result ?: err;
	}

	return result;
}

/**
 * xe_sriov_pf_provision_query_vf_priority() - Query VF's scheduling priority.
 * @xe: the PF &xe_device
 * @vfid: the VF identifier
 * @prio: placeholder for the returned scheduling priority
 *
 * Query VF's scheduling priority provisioning from all tiles/GTs.
 * If values across tiles/GTs are inconsistent then -EUCLEAN error will be returned.
 *
 * This function can only be called on PF.
 *
 * Return: 0 on success or a negative error code on failure.
 */
int xe_sriov_pf_provision_query_vf_priority(struct xe_device *xe, unsigned int vfid, u32 *prio)
{
	struct xe_gt *gt;
	unsigned int id;
	int count = 0;
	u32 value;

	for_each_gt(gt, xe, id) {
		value = xe_gt_sriov_pf_config_get_sched_priority(gt, vfid);
		if (!count++)
			*prio = value;
		else if (value != *prio)
			return pf_report_unclean(gt, vfid, "priority", value, *prio);
	}

	return !count ? -ENODATA : 0;
}

static u64 vram_per_tile(struct xe_tile *tile, u64 total)
{
	struct xe_device *xe = tile->xe;
	unsigned int tcount = xe->info.tile_count;
	u64 alignment = xe_lmtt_page_size(&tile->sriov.pf.lmtt);

	total = round_up(total, tcount * alignment);
	return div_u64(total, tcount);
}

/**
 * xe_sriov_pf_provision_bulk_apply_vram() - Change VRAM provisioning for all VFs.
 * @xe: the PF &xe_device
 * @size: the VRAM size in [bytes] to set
 *
 * Change all VFs VRAM (LMEM) provisioning on all tiles.
 *
 * This function can only be called on PF.
 *
 * Return: 0 on success or a negative error code on failure.
 */
int xe_sriov_pf_provision_bulk_apply_vram(struct xe_device *xe, u64 size)
{
	unsigned int num_vfs = xe_sriov_pf_get_totalvfs(xe);
	struct xe_tile *tile;
	unsigned int id;
	int result = 0;
	int err;

	xe_assert(xe, xe_device_has_lmtt(xe));

	guard(mutex)(xe_sriov_pf_master_mutex(xe));

	for_each_tile(tile, xe, id) {
		err = xe_gt_sriov_pf_config_bulk_set_lmem_locked(tile->primary_gt,
								 VFID(1), num_vfs,
								 vram_per_tile(tile, size));
		result = result ?: err;
	}

	return result;
}

/**
 * xe_sriov_pf_provision_apply_vf_vram() - Change single VF VRAM allocation.
 * @xe: the PF &xe_device
 * @vfid: the VF identifier (can't be 0 == PFID)
 * @size: VRAM size to set
 *
 * Change VF's VRAM provisioning on all tiles/GTs.
 *
 * This function can only be called on PF.
 *
 * Return: 0 on success or a negative error code on failure.
 */
int xe_sriov_pf_provision_apply_vf_vram(struct xe_device *xe, unsigned int vfid, u64 size)
{
	struct xe_tile *tile;
	unsigned int id;
	int result = 0;
	int err;

	xe_assert(xe, vfid);
	xe_assert(xe, xe_device_has_lmtt(xe));

	guard(mutex)(xe_sriov_pf_master_mutex(xe));

	for_each_tile(tile, xe, id) {
		err = xe_gt_sriov_pf_config_set_lmem_locked(tile->primary_gt, vfid,
							    vram_per_tile(tile, size));
		result = result ?: err;
	}

	return result;
}

/**
 * xe_sriov_pf_provision_query_vf_vram() - Query VF's VRAM allocation.
 * @xe: the PF &xe_device
 * @vfid: the VF identifier (can't be 0 == PFID)
 * @size: placeholder for the returned VRAM size
 *
 * Query VF's VRAM provisioning from all tiles/GTs.
 *
 * This function can only be called on PF.
 *
 * Return: 0 on success or a negative error code on failure.
 */
int xe_sriov_pf_provision_query_vf_vram(struct xe_device *xe, unsigned int vfid, u64 *size)
{
	struct xe_tile *tile;
	unsigned int id;
	u64 total = 0;

	xe_assert(xe, vfid);

	guard(mutex)(xe_sriov_pf_master_mutex(xe));

	for_each_tile(tile, xe, id)
		total += xe_gt_sriov_pf_config_get_lmem_locked(tile->primary_gt, vfid);

	*size = total;
	return 0;
}