xref: /linux/drivers/gpu/drm/xe/xe_rtp.c (revision bba2c3615bd6cfee7456d1130f2e6b01b3f4e9ba)
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  * @entries: Table with RTP definitions
330  * @n_entries: Number of entries to process, usually ARRAY_SIZE(entries)
331  * @sr: Save-restore struct where matching rules execute the action. This can be
332  *      viewed as the "coalesced view" of multiple the tables. The bits for each
333  *      register set are expected not to collide with previously added entries
334  * @process_in_vf: Whether this RTP table should get processed for SR-IOV VF
335  *      devices.  Should generally only be 'true' for LRC tables.
336  *
337  * Walk the table pointed by @entries (with an empty sentinel) and add all
338  * entries with matching rules to @sr. If @hwe is not NULL, its mmio_base is
339  * used to calculate the right register offset
340  */
341 void xe_rtp_process_to_sr(struct xe_rtp_process_ctx *ctx,
342 			  const struct xe_rtp_entry_sr *entries,
343 			  size_t n_entries,
344 			  struct xe_reg_sr *sr,
345 			  bool process_in_vf)
346 {
347 	const struct xe_rtp_entry_sr *entry;
348 	struct xe_hw_engine *hwe = NULL;
349 	struct xe_gt *gt = NULL;
350 	struct xe_device *xe = NULL;
351 
352 	rtp_get_context(ctx, &hwe, &gt, &xe);
353 
354 	if (!process_in_vf && IS_SRIOV_VF(xe))
355 		return;
356 
357 	xe_assert(xe, entries);
358 
359 	for (entry = entries; entry - entries < n_entries; entry++) {
360 		bool match = false;
361 
362 		if (entry->flags & XE_RTP_ENTRY_FLAG_FOREACH_ENGINE) {
363 			struct xe_hw_engine *each_hwe;
364 			enum xe_hw_engine_id id;
365 
366 			for_each_hw_engine(each_hwe, gt, id)
367 				match |= rtp_process_one_sr(entry, xe, gt,
368 							    each_hwe, sr);
369 		} else {
370 			match = rtp_process_one_sr(entry, xe, gt, hwe, sr);
371 		}
372 
373 		if (match)
374 			rtp_mark_active(xe, ctx, entry - entries);
375 	}
376 }
377 EXPORT_SYMBOL_IF_KUNIT(xe_rtp_process_to_sr);
378 
379 /**
380  * xe_rtp_process - Process all rtp @entries, without running any action
381  * @ctx: The context for processing the table, with one of device, gt or hwe
382  * @entries: Table with RTP definitions
383  *
384  * Walk the table pointed by @entries (with an empty sentinel), executing the
385  * rules. One difference from xe_rtp_process_to_sr(): there is no action
386  * associated with each entry since this uses struct xe_rtp_entry. Its main use
387  * is for marking active workarounds via
388  * xe_rtp_process_ctx_enable_active_tracking().
389  */
390 void xe_rtp_process(struct xe_rtp_process_ctx *ctx,
391 		    const struct xe_rtp_entry *entries)
392 {
393 	const struct xe_rtp_entry *entry;
394 	struct xe_hw_engine *hwe;
395 	struct xe_gt *gt;
396 	struct xe_device *xe;
397 
398 	rtp_get_context(ctx, &hwe, &gt, &xe);
399 
400 	for (entry = entries; entry && entry->rules; entry++) {
401 		if (!rule_matches(xe, gt, hwe, entry->rules, entry->n_rules))
402 			continue;
403 
404 		rtp_mark_active(xe, ctx, entry - entries);
405 	}
406 }
407 EXPORT_SYMBOL_IF_KUNIT(xe_rtp_process);
408 
409 bool xe_rtp_match_always(const struct xe_device *xe,
410 			 const struct xe_gt *gt,
411 			 const struct xe_hw_engine *hwe)
412 {
413 	return true;
414 }
415 
416 bool xe_rtp_match_even_instance(const struct xe_device *xe,
417 				const struct xe_gt *gt,
418 				const struct xe_hw_engine *hwe)
419 {
420 	return hwe->instance % 2 == 0;
421 }
422 
423 bool xe_rtp_match_first_render_or_compute(const struct xe_device *xe,
424 					  const struct xe_gt *gt,
425 					  const struct xe_hw_engine *hwe)
426 {
427 	u64 render_compute_mask = gt->info.engine_mask &
428 		(XE_HW_ENGINE_CCS_MASK | XE_HW_ENGINE_RCS_MASK);
429 
430 	return render_compute_mask &&
431 		hwe->engine_id == __ffs(render_compute_mask);
432 }
433 
434 bool xe_rtp_match_not_sriov_vf(const struct xe_device *xe,
435 			       const struct xe_gt *gt,
436 			       const struct xe_hw_engine *hwe)
437 {
438 	return !IS_SRIOV_VF(xe);
439 }
440 
441 bool xe_rtp_match_psmi_enabled(const struct xe_device *xe,
442 			       const struct xe_gt *gt,
443 			       const struct xe_hw_engine *hwe)
444 {
445 	return xe_configfs_get_psmi_enabled(to_pci_dev(xe->drm.dev));
446 }
447 
448 bool xe_rtp_match_gt_has_discontiguous_dss_groups(const struct xe_device *xe,
449 						  const struct xe_gt *gt,
450 						  const struct xe_hw_engine *hwe)
451 {
452 	return xe_gt_has_discontiguous_dss_groups(gt);
453 }
454 
455 bool xe_rtp_match_has_flat_ccs(const struct xe_device *xe,
456 			       const struct xe_gt *gt,
457 			       const struct xe_hw_engine *hwe)
458 {
459 	return xe->info.has_flat_ccs;
460 }
461 
462 bool xe_rtp_match_has_msix(const struct xe_device *xe,
463 			   const struct xe_gt *gt,
464 			   const struct xe_hw_engine *hwe)
465 {
466 	return xe_device_has_msix(xe);
467 }
468 
469 #if IS_ENABLED(CONFIG_DRM_XE_KUNIT_TEST)
470 #include "tests/xe_rtp.c"
471 #endif
472