xref: /linux/drivers/gpu/drm/xe/xe_rtp.c (revision 99676aed1fec109d62822e21a06760eb098dc5f4)
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 static bool rule_matches(const struct xe_device *xe,
34 			 struct xe_gt *gt,
35 			 struct xe_hw_engine *hwe,
36 			 const struct xe_rtp_rule *rules,
37 			 unsigned int n_rules)
38 {
39 	const struct xe_rtp_rule *r;
40 	unsigned int i, rcount = 0;
41 	bool match;
42 
43 	for (r = rules, i = 0; i < n_rules; r = &rules[++i]) {
44 		switch (r->match_type) {
45 		case XE_RTP_MATCH_OR:
46 			/*
47 			 * This is only reached if a complete set of
48 			 * rules passed or none were evaluated. For both cases,
49 			 * shortcut the other rules and return the proper value.
50 			 */
51 			goto done;
52 		case XE_RTP_MATCH_PLATFORM:
53 			match = xe->info.platform == r->platform;
54 			break;
55 		case XE_RTP_MATCH_SUBPLATFORM:
56 			match = xe->info.platform == r->platform &&
57 				xe->info.subplatform == r->subplatform;
58 			break;
59 		case XE_RTP_MATCH_PLATFORM_STEP:
60 			if (drm_WARN_ON(&xe->drm, xe->info.step.platform == STEP_NONE))
61 				return false;
62 
63 			match = xe->info.step.platform >= r->step_start &&
64 				xe->info.step.platform < r->step_end;
65 			break;
66 		case XE_RTP_MATCH_GRAPHICS_VERSION:
67 			if (drm_WARN_ON(&xe->drm, !gt))
68 				return false;
69 
70 			match = xe->info.graphics_verx100 == r->ver_start &&
71 				(!has_samedia(xe) || !xe_gt_is_media_type(gt));
72 			break;
73 		case XE_RTP_MATCH_GRAPHICS_VERSION_RANGE:
74 			if (drm_WARN_ON(&xe->drm, !gt))
75 				return false;
76 
77 			match = 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 			break;
81 		case XE_RTP_MATCH_GRAPHICS_VERSION_ANY_GT:
82 			if (drm_WARN_ON(&xe->drm, !gt))
83 				return false;
84 
85 			match = xe->info.graphics_verx100 == r->ver_start;
86 			break;
87 		case XE_RTP_MATCH_GRAPHICS_STEP:
88 			if (drm_WARN_ON(&xe->drm, !gt))
89 				return false;
90 
91 			match = xe->info.step.graphics >= r->step_start &&
92 				xe->info.step.graphics < r->step_end &&
93 				(!has_samedia(xe) || !xe_gt_is_media_type(gt));
94 			break;
95 		case XE_RTP_MATCH_MEDIA_VERSION:
96 			if (drm_WARN_ON(&xe->drm, !gt))
97 				return false;
98 
99 			match = xe->info.media_verx100 == r->ver_start &&
100 				(!has_samedia(xe) || xe_gt_is_media_type(gt));
101 			break;
102 		case XE_RTP_MATCH_MEDIA_VERSION_RANGE:
103 			if (drm_WARN_ON(&xe->drm, !gt))
104 				return false;
105 
106 			match = xe->info.media_verx100 >= r->ver_start &&
107 				xe->info.media_verx100 <= r->ver_end &&
108 				(!has_samedia(xe) || xe_gt_is_media_type(gt));
109 			break;
110 		case XE_RTP_MATCH_MEDIA_STEP:
111 			if (drm_WARN_ON(&xe->drm, !gt))
112 				return false;
113 
114 			match = xe->info.step.media >= r->step_start &&
115 				xe->info.step.media < r->step_end &&
116 				(!has_samedia(xe) || xe_gt_is_media_type(gt));
117 			break;
118 		case XE_RTP_MATCH_MEDIA_VERSION_ANY_GT:
119 			if (drm_WARN_ON(&xe->drm, !gt))
120 				return false;
121 
122 			match = xe->info.media_verx100 == r->ver_start;
123 			break;
124 		case XE_RTP_MATCH_INTEGRATED:
125 			match = !xe->info.is_dgfx;
126 			break;
127 		case XE_RTP_MATCH_DISCRETE:
128 			match = xe->info.is_dgfx;
129 			break;
130 		case XE_RTP_MATCH_ENGINE_CLASS:
131 			if (drm_WARN_ON(&xe->drm, !hwe))
132 				return false;
133 
134 			match = hwe->class == r->engine_class;
135 			break;
136 		case XE_RTP_MATCH_NOT_ENGINE_CLASS:
137 			if (drm_WARN_ON(&xe->drm, !hwe))
138 				return false;
139 
140 			match = hwe->class != r->engine_class;
141 			break;
142 		case XE_RTP_MATCH_FUNC:
143 			match = r->match_func(xe, gt, hwe);
144 			break;
145 		default:
146 			drm_warn(&xe->drm, "Invalid RTP match %u\n",
147 				 r->match_type);
148 			match = false;
149 		}
150 
151 		if (!match) {
152 			/*
153 			 * Advance rules until we find XE_RTP_MATCH_OR to check
154 			 * if there's another set of conditions to check
155 			 */
156 			while (++i < n_rules && rules[i].match_type != XE_RTP_MATCH_OR)
157 				;
158 
159 			if (i >= n_rules)
160 				return false;
161 
162 			rcount = 0;
163 		} else {
164 			rcount++;
165 		}
166 	}
167 
168 done:
169 	if (drm_WARN_ON(&xe->drm, !rcount))
170 		return false;
171 
172 	return true;
173 }
174 
175 static void rtp_add_sr_entry(const struct xe_rtp_action *action,
176 			     struct xe_gt *gt,
177 			     u32 mmio_base,
178 			     struct xe_reg_sr *sr)
179 {
180 	struct xe_reg_sr_entry sr_entry = {
181 		.reg = action->reg,
182 		.clr_bits = action->clr_bits,
183 		.set_bits = action->set_bits,
184 		.read_mask = action->read_mask,
185 	};
186 
187 	sr_entry.reg.addr += mmio_base;
188 	xe_reg_sr_add(sr, &sr_entry, gt);
189 }
190 
191 static bool rtp_process_one_sr(const struct xe_rtp_entry_sr *entry,
192 			       struct xe_device *xe, struct xe_gt *gt,
193 			       struct xe_hw_engine *hwe, struct xe_reg_sr *sr)
194 {
195 	const struct xe_rtp_action *action;
196 	u32 mmio_base;
197 	unsigned int i;
198 
199 	if (!rule_matches(xe, gt, hwe, entry->rules, entry->n_rules))
200 		return false;
201 
202 	for (i = 0, action = &entry->actions[0]; i < entry->n_actions; action++, i++) {
203 		if ((entry->flags & XE_RTP_ENTRY_FLAG_FOREACH_ENGINE) ||
204 		    (action->flags & XE_RTP_ACTION_FLAG_ENGINE_BASE))
205 			mmio_base = hwe->mmio_base;
206 		else
207 			mmio_base = 0;
208 
209 		rtp_add_sr_entry(action, gt, mmio_base, sr);
210 	}
211 
212 	return true;
213 }
214 
215 static void rtp_get_context(struct xe_rtp_process_ctx *ctx,
216 			    struct xe_hw_engine **hwe,
217 			    struct xe_gt **gt,
218 			    struct xe_device **xe)
219 {
220 	switch (ctx->type) {
221 	case XE_RTP_PROCESS_TYPE_DEVICE:
222 		*hwe = NULL;
223 		*gt = NULL;
224 		*xe = ctx->xe;
225 		break;
226 	case XE_RTP_PROCESS_TYPE_GT:
227 		*hwe = NULL;
228 		*gt = ctx->gt;
229 		*xe = gt_to_xe(*gt);
230 		break;
231 	case XE_RTP_PROCESS_TYPE_ENGINE:
232 		*hwe = ctx->hwe;
233 		*gt = (*hwe)->gt;
234 		*xe = gt_to_xe(*gt);
235 		break;
236 	}
237 }
238 
239 /**
240  * xe_rtp_process_ctx_enable_active_tracking - Enable tracking of active entries
241  *
242  * Set additional metadata to track what entries are considered "active", i.e.
243  * their rules match the condition. Bits are never cleared: entries with
244  * matching rules set the corresponding bit in the bitmap.
245  *
246  * @ctx: The context for processing the table
247  * @active_entries: bitmap to store the active entries
248  * @n_entries: number of entries to be processed
249  */
250 void xe_rtp_process_ctx_enable_active_tracking(struct xe_rtp_process_ctx *ctx,
251 					       unsigned long *active_entries,
252 					       size_t n_entries)
253 {
254 	ctx->active_entries = active_entries;
255 	ctx->n_entries = n_entries;
256 }
257 EXPORT_SYMBOL_IF_KUNIT(xe_rtp_process_ctx_enable_active_tracking);
258 
259 static void rtp_mark_active(struct xe_device *xe,
260 			    struct xe_rtp_process_ctx *ctx,
261 			    unsigned int idx)
262 {
263 	if (!ctx->active_entries)
264 		return;
265 
266 	if (drm_WARN_ON(&xe->drm, idx >= ctx->n_entries))
267 		return;
268 
269 	bitmap_set(ctx->active_entries, idx, 1);
270 }
271 
272 /**
273  * xe_rtp_process_to_sr - Process all rtp @entries, adding the matching ones to
274  *                        the save-restore argument.
275  * @ctx: The context for processing the table, with one of device, gt or hwe
276  * @entries: Table with RTP definitions
277  * @n_entries: Number of entries to process, usually ARRAY_SIZE(entries)
278  * @sr: Save-restore struct where matching rules execute the action. This can be
279  *      viewed as the "coalesced view" of multiple the tables. The bits for each
280  *      register set are expected not to collide with previously added entries
281  * @process_in_vf: Whether this RTP table should get processed for SR-IOV VF
282  *      devices.  Should generally only be 'true' for LRC tables.
283  *
284  * Walk the table pointed by @entries (with an empty sentinel) and add all
285  * entries with matching rules to @sr. If @hwe is not NULL, its mmio_base is
286  * used to calculate the right register offset
287  */
288 void xe_rtp_process_to_sr(struct xe_rtp_process_ctx *ctx,
289 			  const struct xe_rtp_entry_sr *entries,
290 			  size_t n_entries,
291 			  struct xe_reg_sr *sr,
292 			  bool process_in_vf)
293 {
294 	const struct xe_rtp_entry_sr *entry;
295 	struct xe_hw_engine *hwe = NULL;
296 	struct xe_gt *gt = NULL;
297 	struct xe_device *xe = NULL;
298 
299 	rtp_get_context(ctx, &hwe, &gt, &xe);
300 
301 	if (!process_in_vf && IS_SRIOV_VF(xe))
302 		return;
303 
304 	xe_assert(xe, entries);
305 
306 	for (entry = entries; entry - entries < n_entries; entry++) {
307 		bool match = false;
308 
309 		if (entry->flags & XE_RTP_ENTRY_FLAG_FOREACH_ENGINE) {
310 			struct xe_hw_engine *each_hwe;
311 			enum xe_hw_engine_id id;
312 
313 			for_each_hw_engine(each_hwe, gt, id)
314 				match |= rtp_process_one_sr(entry, xe, gt,
315 							    each_hwe, sr);
316 		} else {
317 			match = rtp_process_one_sr(entry, xe, gt, hwe, sr);
318 		}
319 
320 		if (match)
321 			rtp_mark_active(xe, ctx, entry - entries);
322 	}
323 }
324 EXPORT_SYMBOL_IF_KUNIT(xe_rtp_process_to_sr);
325 
326 /**
327  * xe_rtp_process - Process all rtp @entries, without running any action
328  * @ctx: The context for processing the table, with one of device, gt or hwe
329  * @entries: Table with RTP definitions
330  *
331  * Walk the table pointed by @entries (with an empty sentinel), executing the
332  * rules. One difference from xe_rtp_process_to_sr(): there is no action
333  * associated with each entry since this uses struct xe_rtp_entry. Its main use
334  * is for marking active workarounds via
335  * xe_rtp_process_ctx_enable_active_tracking().
336  */
337 void xe_rtp_process(struct xe_rtp_process_ctx *ctx,
338 		    const struct xe_rtp_entry *entries)
339 {
340 	const struct xe_rtp_entry *entry;
341 	struct xe_hw_engine *hwe;
342 	struct xe_gt *gt;
343 	struct xe_device *xe;
344 
345 	rtp_get_context(ctx, &hwe, &gt, &xe);
346 
347 	for (entry = entries; entry && entry->rules; entry++) {
348 		if (!rule_matches(xe, gt, hwe, entry->rules, entry->n_rules))
349 			continue;
350 
351 		rtp_mark_active(xe, ctx, entry - entries);
352 	}
353 }
354 EXPORT_SYMBOL_IF_KUNIT(xe_rtp_process);
355 
356 bool xe_rtp_match_always(const struct xe_device *xe,
357 			 const struct xe_gt *gt,
358 			 const struct xe_hw_engine *hwe)
359 {
360 	return true;
361 }
362 
363 bool xe_rtp_match_even_instance(const struct xe_device *xe,
364 				const struct xe_gt *gt,
365 				const struct xe_hw_engine *hwe)
366 {
367 	return hwe->instance % 2 == 0;
368 }
369 
370 bool xe_rtp_match_first_render_or_compute(const struct xe_device *xe,
371 					  const struct xe_gt *gt,
372 					  const struct xe_hw_engine *hwe)
373 {
374 	u64 render_compute_mask = gt->info.engine_mask &
375 		(XE_HW_ENGINE_CCS_MASK | XE_HW_ENGINE_RCS_MASK);
376 
377 	return render_compute_mask &&
378 		hwe->engine_id == __ffs(render_compute_mask);
379 }
380 
381 bool xe_rtp_match_not_sriov_vf(const struct xe_device *xe,
382 			       const struct xe_gt *gt,
383 			       const struct xe_hw_engine *hwe)
384 {
385 	return !IS_SRIOV_VF(xe);
386 }
387 
388 bool xe_rtp_match_psmi_enabled(const struct xe_device *xe,
389 			       const struct xe_gt *gt,
390 			       const struct xe_hw_engine *hwe)
391 {
392 	return xe_configfs_get_psmi_enabled(to_pci_dev(xe->drm.dev));
393 }
394 
395 bool xe_rtp_match_gt_has_discontiguous_dss_groups(const struct xe_device *xe,
396 						  const struct xe_gt *gt,
397 						  const struct xe_hw_engine *hwe)
398 {
399 	return xe_gt_has_discontiguous_dss_groups(gt);
400 }
401 
402 bool xe_rtp_match_has_flat_ccs(const struct xe_device *xe,
403 			       const struct xe_gt *gt,
404 			       const struct xe_hw_engine *hwe)
405 {
406 	return xe->info.has_flat_ccs;
407 }
408 
409 bool xe_rtp_match_has_msix(const struct xe_device *xe,
410 			   const struct xe_gt *gt,
411 			   const struct xe_hw_engine *hwe)
412 {
413 	return xe_device_has_msix(xe);
414 }
415