xref: /linux/drivers/gpu/drm/xe/xe_rtp.c (revision 0710dd08824a6f3b9892fc5be24acd2e4a36f178)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2022 Intel Corporation
4  */
5 
6 #include "xe_rtp.h"
7 
8 #include <kunit/visibility.h>
9 
10 #include <uapi/drm/xe_drm.h>
11 
12 #include "xe_configfs.h"
13 #include "xe_device.h"
14 #include "xe_gt.h"
15 #include "xe_gt_topology.h"
16 #include "xe_reg_sr.h"
17 #include "xe_sriov.h"
18 
19 /**
20  * DOC: Register Table Processing
21  *
22  * Internal infrastructure to define how registers should be updated based on
23  * rules and actions. This can be used to define tables with multiple entries
24  * (one per register) that will be walked over at some point in time to apply
25  * the values to the registers that have matching rules.
26  */
27 
28 static bool has_samedia(const struct xe_device *xe)
29 {
30 	return xe->info.media_verx100 >= 1300;
31 }
32 
33 struct rule_match_ctx {
34 	const struct xe_device *xe;
35 	struct xe_gt *gt;
36 	struct xe_hw_engine *hwe;
37 	const struct xe_rtp_rule *rules;
38 	const unsigned int n_rules;
39 	unsigned int head;
40 	int err;
41 };
42 
43 static bool rule_is_item(const struct xe_rtp_rule *r)
44 {
45 	return r->match_type != XE_RTP_MATCH_OR;
46 }
47 
48 static bool rule_match_item(struct rule_match_ctx *match_ctx)
49 {
50 	const struct xe_device *xe = match_ctx->xe;
51 	struct xe_gt *gt = match_ctx->gt;
52 	struct xe_hw_engine *hwe = match_ctx->hwe;
53 	const struct xe_rtp_rule *r = &match_ctx->rules[match_ctx->head];
54 
55 	switch (r->match_type) {
56 	case XE_RTP_MATCH_PLATFORM:
57 		return xe->info.platform == r->platform;
58 	case XE_RTP_MATCH_SUBPLATFORM:
59 		return xe->info.platform == r->platform &&
60 			xe->info.subplatform == r->subplatform;
61 	case XE_RTP_MATCH_PLATFORM_STEP:
62 		if (drm_WARN_ON(&xe->drm, xe->info.step.platform == STEP_NONE))
63 			return false;
64 
65 		return xe->info.step.platform >= r->step_start &&
66 			xe->info.step.platform < r->step_end;
67 	case XE_RTP_MATCH_GRAPHICS_VERSION:
68 		if (drm_WARN_ON(&xe->drm, !gt))
69 			return false;
70 
71 		return xe->info.graphics_verx100 == r->ver_start &&
72 			(!has_samedia(xe) || !xe_gt_is_media_type(gt));
73 	case XE_RTP_MATCH_GRAPHICS_VERSION_RANGE:
74 		if (drm_WARN_ON(&xe->drm, !gt))
75 			return false;
76 
77 		return xe->info.graphics_verx100 >= r->ver_start &&
78 			xe->info.graphics_verx100 <= r->ver_end &&
79 			(!has_samedia(xe) || !xe_gt_is_media_type(gt));
80 	case XE_RTP_MATCH_GRAPHICS_VERSION_ANY_GT:
81 		if (drm_WARN_ON(&xe->drm, !gt))
82 			return false;
83 
84 		return xe->info.graphics_verx100 == r->ver_start;
85 	case XE_RTP_MATCH_GRAPHICS_STEP:
86 		if (drm_WARN_ON(&xe->drm, !gt))
87 			return false;
88 
89 		return xe->info.step.graphics >= r->step_start &&
90 			xe->info.step.graphics < r->step_end &&
91 			(!has_samedia(xe) || !xe_gt_is_media_type(gt));
92 	case XE_RTP_MATCH_MEDIA_VERSION:
93 		if (drm_WARN_ON(&xe->drm, !gt))
94 			return false;
95 
96 		return xe->info.media_verx100 == r->ver_start &&
97 			(!has_samedia(xe) || xe_gt_is_media_type(gt));
98 	case XE_RTP_MATCH_MEDIA_VERSION_RANGE:
99 		if (drm_WARN_ON(&xe->drm, !gt))
100 			return false;
101 
102 		return xe->info.media_verx100 >= r->ver_start &&
103 			xe->info.media_verx100 <= r->ver_end &&
104 			(!has_samedia(xe) || xe_gt_is_media_type(gt));
105 	case XE_RTP_MATCH_MEDIA_STEP:
106 		if (drm_WARN_ON(&xe->drm, !gt))
107 			return false;
108 
109 		return xe->info.step.media >= r->step_start &&
110 			xe->info.step.media < r->step_end &&
111 			(!has_samedia(xe) || xe_gt_is_media_type(gt));
112 	case XE_RTP_MATCH_MEDIA_VERSION_ANY_GT:
113 		if (drm_WARN_ON(&xe->drm, !gt))
114 			return false;
115 
116 		return xe->info.media_verx100 == r->ver_start;
117 	case XE_RTP_MATCH_INTEGRATED:
118 		return !xe->info.is_dgfx;
119 	case XE_RTP_MATCH_DISCRETE:
120 		return xe->info.is_dgfx;
121 	case XE_RTP_MATCH_ENGINE_CLASS:
122 		if (drm_WARN_ON(&xe->drm, !hwe))
123 			return false;
124 
125 		return hwe->class == r->engine_class;
126 	case XE_RTP_MATCH_NOT_ENGINE_CLASS:
127 		if (drm_WARN_ON(&xe->drm, !hwe))
128 			return false;
129 
130 		return hwe->class != r->engine_class;
131 	case XE_RTP_MATCH_FUNC:
132 		return r->match_func(xe, gt, hwe);
133 	default:
134 		drm_warn(&xe->drm, "Invalid RTP match %u\n",
135 			 r->match_type);
136 		return false;
137 	}
138 }
139 
140 /*
141  * Match a conjunctive set of rules (rules joined by an implicit "AND").
142  *
143  * Once one item evaluates to false, the remaining items are not evaluated
144  * anymore.  Nevetheless, all rules are consumed to allow detecting syntax
145  * errors.
146  */
147 static bool rule_match_and(struct rule_match_ctx *match_ctx, bool parse_only)
148 {
149 	bool match = true;
150 	unsigned int count = 0;
151 
152 	while (match_ctx->head < match_ctx->n_rules &&
153 	       rule_is_item(&match_ctx->rules[match_ctx->head])) {
154 		if (!parse_only)
155 			match = rule_match_item(match_ctx);
156 
157 		if (!match)
158 			parse_only = true;
159 
160 		match_ctx->head++;
161 		count++;
162 	}
163 
164 	if (drm_WARN_ON(&match_ctx->xe->drm, !count)) {
165 		match_ctx->err = -EINVAL;
166 
167 		if (!parse_only)
168 			match = false;
169 	}
170 
171 	return match;
172 }
173 
174 /*
175  * Match a disjunctive set of rules (subset of rules joined by
176  * "XE_RTP_MATCH_OR").
177  *
178  * Once one subset evaluates to true, the remaining items are not evaluated
179  * anymore. Nevetheless, all rules are consumed to allow detecting syntax
180  * errors.
181  */
182 static bool rule_match_or(struct rule_match_ctx *match_ctx)
183 {
184 	bool match = rule_match_and(match_ctx, false);
185 
186 	while (match_ctx->head < match_ctx->n_rules &&
187 	       match_ctx->rules[match_ctx->head].match_type == XE_RTP_MATCH_OR) {
188 		/* Consume XE_RTP_MATCH_OR. */
189 		match_ctx->head++;
190 
191 		match = rule_match_and(match_ctx, match);
192 	}
193 
194 	return match;
195 }
196 
197 static bool rule_matches_with_err(const struct xe_device *xe,
198 				  struct xe_gt *gt,
199 				  struct xe_hw_engine *hwe,
200 				  const struct xe_rtp_rule *rules,
201 				  unsigned int n_rules,
202 				  int *err)
203 {
204 	struct rule_match_ctx match_ctx = {
205 		.xe = xe,
206 		.gt = gt,
207 		.hwe = hwe,
208 		.rules = rules,
209 		.n_rules = n_rules,
210 	};
211 	bool match = rule_match_or(&match_ctx);
212 
213 	if (err)
214 		*err = match_ctx.err;
215 
216 	return match;
217 }
218 
219 static bool rule_matches(const struct xe_device *xe,
220 			 struct xe_gt *gt,
221 			 struct xe_hw_engine *hwe,
222 			 const struct xe_rtp_rule *rules,
223 			 unsigned int n_rules)
224 {
225 	return rule_matches_with_err(xe, gt, hwe, rules, n_rules, NULL);
226 }
227 
228 static void rtp_add_sr_entry(const struct xe_rtp_action *action,
229 			     struct xe_gt *gt,
230 			     u32 mmio_base,
231 			     struct xe_reg_sr *sr)
232 {
233 	struct xe_reg_sr_entry sr_entry = {
234 		.reg = action->reg,
235 		.clr_bits = action->clr_bits,
236 		.set_bits = action->set_bits,
237 		.read_mask = action->read_mask,
238 	};
239 
240 	sr_entry.reg.addr += mmio_base;
241 	xe_reg_sr_add(sr, &sr_entry, gt);
242 }
243 
244 static bool rtp_process_one_sr(const struct xe_rtp_entry_sr *entry,
245 			       struct xe_device *xe, struct xe_gt *gt,
246 			       struct xe_hw_engine *hwe, struct xe_reg_sr *sr)
247 {
248 	const struct xe_rtp_action *action;
249 	u32 mmio_base;
250 	unsigned int i;
251 
252 	if (!rule_matches(xe, gt, hwe, entry->rules, entry->n_rules))
253 		return false;
254 
255 	for (i = 0, action = &entry->actions[0]; i < entry->n_actions; action++, i++) {
256 		if ((entry->flags & XE_RTP_ENTRY_FLAG_FOREACH_ENGINE) ||
257 		    (action->flags & XE_RTP_ACTION_FLAG_ENGINE_BASE))
258 			mmio_base = hwe->mmio_base;
259 		else
260 			mmio_base = 0;
261 
262 		rtp_add_sr_entry(action, gt, mmio_base, sr);
263 	}
264 
265 	return true;
266 }
267 
268 static void rtp_get_context(struct xe_rtp_process_ctx *ctx,
269 			    struct xe_hw_engine **hwe,
270 			    struct xe_gt **gt,
271 			    struct xe_device **xe)
272 {
273 	switch (ctx->type) {
274 	case XE_RTP_PROCESS_TYPE_DEVICE:
275 		*hwe = NULL;
276 		*gt = NULL;
277 		*xe = ctx->xe;
278 		break;
279 	case XE_RTP_PROCESS_TYPE_GT:
280 		*hwe = NULL;
281 		*gt = ctx->gt;
282 		*xe = gt_to_xe(*gt);
283 		break;
284 	case XE_RTP_PROCESS_TYPE_ENGINE:
285 		*hwe = ctx->hwe;
286 		*gt = (*hwe)->gt;
287 		*xe = gt_to_xe(*gt);
288 		break;
289 	}
290 }
291 
292 /**
293  * xe_rtp_process_ctx_enable_active_tracking - Enable tracking of active entries
294  *
295  * Set additional metadata to track what entries are considered "active", i.e.
296  * their rules match the condition. Bits are never cleared: entries with
297  * matching rules set the corresponding bit in the bitmap.
298  *
299  * @ctx: The context for processing the table
300  * @active_entries: bitmap to store the active entries
301  * @n_entries: number of entries to be processed
302  */
303 void xe_rtp_process_ctx_enable_active_tracking(struct xe_rtp_process_ctx *ctx,
304 					       unsigned long *active_entries,
305 					       size_t n_entries)
306 {
307 	ctx->active_entries = active_entries;
308 	ctx->n_entries = n_entries;
309 }
310 EXPORT_SYMBOL_IF_KUNIT(xe_rtp_process_ctx_enable_active_tracking);
311 
312 static void rtp_mark_active(struct xe_device *xe,
313 			    struct xe_rtp_process_ctx *ctx,
314 			    unsigned int idx)
315 {
316 	if (!ctx->active_entries)
317 		return;
318 
319 	if (drm_WARN_ON(&xe->drm, idx >= ctx->n_entries))
320 		return;
321 
322 	bitmap_set(ctx->active_entries, idx, 1);
323 }
324 
325 /**
326  * xe_rtp_process_to_sr - Process all rtp @entries, adding the matching ones to
327  *                        the save-restore argument.
328  * @ctx: The context for processing the table, with one of device, gt or hwe
329  * @table: Table with RTP definitions
330  * @sr: Save-restore struct where matching rules execute the action. This can be
331  *      viewed as the "coalesced view" of multiple the tables. The bits for each
332  *      register set are expected not to collide with previously added entries
333  * @process_in_vf: Whether this RTP table should get processed for SR-IOV VF
334  *      devices.  Should generally only be 'true' for LRC tables.
335  *
336  * Walk the table pointed by @entries (with an empty sentinel) and add all
337  * entries with matching rules to @sr. If @hwe is not NULL, its mmio_base is
338  * used to calculate the right register offset
339  */
340 void xe_rtp_process_to_sr(struct xe_rtp_process_ctx *ctx,
341 			  const struct xe_rtp_table_sr *table,
342 			  struct xe_reg_sr *sr,
343 			  bool process_in_vf)
344 {
345 	struct xe_hw_engine *hwe = NULL;
346 	struct xe_gt *gt = NULL;
347 	struct xe_device *xe = NULL;
348 
349 	rtp_get_context(ctx, &hwe, &gt, &xe);
350 
351 	if (!process_in_vf && IS_SRIOV_VF(xe))
352 		return;
353 
354 	xe_assert(xe, table->entries);
355 
356 	for (size_t i = 0; i < table->n_entries; i++) {
357 		const struct xe_rtp_entry_sr *entry = &table->entries[i];
358 		bool match = false;
359 
360 		if (entry->flags & XE_RTP_ENTRY_FLAG_FOREACH_ENGINE) {
361 			struct xe_hw_engine *each_hwe;
362 			enum xe_hw_engine_id id;
363 
364 			for_each_hw_engine(each_hwe, gt, id)
365 				match |= rtp_process_one_sr(entry, xe, gt,
366 							    each_hwe, sr);
367 		} else {
368 			match = rtp_process_one_sr(entry, xe, gt, hwe, sr);
369 		}
370 
371 		if (match)
372 			rtp_mark_active(xe, ctx, i);
373 	}
374 }
375 EXPORT_SYMBOL_IF_KUNIT(xe_rtp_process_to_sr);
376 
377 /**
378  * xe_rtp_process - Process all entries in rtp @table, without running any action
379  * @ctx: The context for processing the table, with one of device, gt or hwe
380  * @table: Table with RTP definitions
381  *
382  * Walk the table pointed by @table, executing the
383  * rules. One difference from xe_rtp_process_to_sr(): there is no action
384  * associated with each entry since this uses struct xe_rtp_entry. Its main use
385  * is for marking active workarounds via
386  * xe_rtp_process_ctx_enable_active_tracking().
387  */
388 void xe_rtp_process(struct xe_rtp_process_ctx *ctx,
389 		    const struct xe_rtp_table *table)
390 {
391 	struct xe_hw_engine *hwe;
392 	struct xe_gt *gt;
393 	struct xe_device *xe;
394 
395 	rtp_get_context(ctx, &hwe, &gt, &xe);
396 
397 	xe_assert(xe, table->entries);
398 
399 	for (size_t i = 0; i < table->n_entries; i++) {
400 		const struct xe_rtp_entry *entry = &table->entries[i];
401 
402 		if (!rule_matches(xe, gt, hwe, entry->rules, entry->n_rules))
403 			continue;
404 
405 		rtp_mark_active(xe, ctx, i);
406 	}
407 }
408 EXPORT_SYMBOL_IF_KUNIT(xe_rtp_process);
409 
410 bool xe_rtp_match_always(const struct xe_device *xe,
411 			 const struct xe_gt *gt,
412 			 const struct xe_hw_engine *hwe)
413 {
414 	return true;
415 }
416 
417 bool xe_rtp_match_even_instance(const struct xe_device *xe,
418 				const struct xe_gt *gt,
419 				const struct xe_hw_engine *hwe)
420 {
421 	return hwe->instance % 2 == 0;
422 }
423 
424 bool xe_rtp_match_first_render_or_compute(const struct xe_device *xe,
425 					  const struct xe_gt *gt,
426 					  const struct xe_hw_engine *hwe)
427 {
428 	u64 render_compute_mask = gt->info.engine_mask &
429 		(XE_HW_ENGINE_CCS_MASK | XE_HW_ENGINE_RCS_MASK);
430 
431 	return render_compute_mask &&
432 		hwe->engine_id == __ffs(render_compute_mask);
433 }
434 
435 bool xe_rtp_match_not_sriov_vf(const struct xe_device *xe,
436 			       const struct xe_gt *gt,
437 			       const struct xe_hw_engine *hwe)
438 {
439 	return !IS_SRIOV_VF(xe);
440 }
441 
442 bool xe_rtp_match_psmi_enabled(const struct xe_device *xe,
443 			       const struct xe_gt *gt,
444 			       const struct xe_hw_engine *hwe)
445 {
446 	return xe_configfs_get_psmi_enabled(to_pci_dev(xe->drm.dev));
447 }
448 
449 bool xe_rtp_match_gt_has_discontiguous_dss_groups(const struct xe_device *xe,
450 						  const struct xe_gt *gt,
451 						  const struct xe_hw_engine *hwe)
452 {
453 	return xe_gt_has_discontiguous_dss_groups(gt);
454 }
455 
456 bool xe_rtp_match_has_flat_ccs(const struct xe_device *xe,
457 			       const struct xe_gt *gt,
458 			       const struct xe_hw_engine *hwe)
459 {
460 	return xe->info.has_flat_ccs;
461 }
462 
463 bool xe_rtp_match_has_msix(const struct xe_device *xe,
464 			   const struct xe_gt *gt,
465 			   const struct xe_hw_engine *hwe)
466 {
467 	return xe_device_has_msix(xe);
468 }
469 
470 #if IS_ENABLED(CONFIG_DRM_XE_KUNIT_TEST)
471 #include "tests/xe_rtp.c"
472 #endif
473