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