xref: /linux/drivers/gpu/drm/xe/xe_rtp.c (revision 2f4a730fcd2d6ae7378a67fe78797b0a3f7ca1b3)
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 <drm/xe_drm.h>
11 
12 #include "xe_gt.h"
13 #include "xe_gt_topology.h"
14 #include "xe_macros.h"
15 #include "xe_reg_sr.h"
16 
17 /**
18  * DOC: Register Table Processing
19  *
20  * Internal infrastructure to define how registers should be updated based on
21  * rules and actions. This can be used to define tables with multiple entries
22  * (one per register) that will be walked over at some point in time to apply
23  * the values to the registers that have matching rules.
24  */
25 
26 static bool has_samedia(const struct xe_device *xe)
27 {
28 	return xe->info.media_verx100 >= 1300;
29 }
30 
31 static bool rule_matches(const struct xe_device *xe,
32 			 struct xe_gt *gt,
33 			 struct xe_hw_engine *hwe,
34 			 const struct xe_rtp_rule *rules,
35 			 unsigned int n_rules)
36 {
37 	const struct xe_rtp_rule *r;
38 	unsigned int i, rcount = 0;
39 	bool match;
40 
41 	for (r = rules, i = 0; i < n_rules; r = &rules[++i]) {
42 		switch (r->match_type) {
43 		case XE_RTP_MATCH_OR:
44 			/*
45 			 * This is only reached if a complete set of
46 			 * rules passed or none were evaluated. For both cases,
47 			 * shortcut the other rules and return the proper value.
48 			 */
49 			goto done;
50 		case XE_RTP_MATCH_PLATFORM:
51 			match = xe->info.platform == r->platform;
52 			break;
53 		case XE_RTP_MATCH_SUBPLATFORM:
54 			match = xe->info.platform == r->platform &&
55 				xe->info.subplatform == r->subplatform;
56 			break;
57 		case XE_RTP_MATCH_GRAPHICS_VERSION:
58 			match = xe->info.graphics_verx100 == r->ver_start &&
59 				(!has_samedia(xe) || !xe_gt_is_media_type(gt));
60 			break;
61 		case XE_RTP_MATCH_GRAPHICS_VERSION_RANGE:
62 			match = xe->info.graphics_verx100 >= r->ver_start &&
63 				xe->info.graphics_verx100 <= r->ver_end &&
64 				(!has_samedia(xe) || !xe_gt_is_media_type(gt));
65 			break;
66 		case XE_RTP_MATCH_GRAPHICS_VERSION_ANY_GT:
67 			match = xe->info.graphics_verx100 == r->ver_start;
68 			break;
69 		case XE_RTP_MATCH_GRAPHICS_STEP:
70 			match = xe->info.step.graphics >= r->step_start &&
71 				xe->info.step.graphics < r->step_end &&
72 				(!has_samedia(xe) || !xe_gt_is_media_type(gt));
73 			break;
74 		case XE_RTP_MATCH_MEDIA_VERSION:
75 			match = xe->info.media_verx100 == r->ver_start &&
76 				(!has_samedia(xe) || xe_gt_is_media_type(gt));
77 			break;
78 		case XE_RTP_MATCH_MEDIA_VERSION_RANGE:
79 			match = xe->info.media_verx100 >= r->ver_start &&
80 				xe->info.media_verx100 <= r->ver_end &&
81 				(!has_samedia(xe) || xe_gt_is_media_type(gt));
82 			break;
83 		case XE_RTP_MATCH_MEDIA_STEP:
84 			match = xe->info.step.media >= r->step_start &&
85 				xe->info.step.media < r->step_end &&
86 				(!has_samedia(xe) || xe_gt_is_media_type(gt));
87 			break;
88 		case XE_RTP_MATCH_MEDIA_VERSION_ANY_GT:
89 			match = xe->info.media_verx100 == r->ver_start;
90 			break;
91 		case XE_RTP_MATCH_INTEGRATED:
92 			match = !xe->info.is_dgfx;
93 			break;
94 		case XE_RTP_MATCH_DISCRETE:
95 			match = xe->info.is_dgfx;
96 			break;
97 		case XE_RTP_MATCH_ENGINE_CLASS:
98 			if (drm_WARN_ON(&xe->drm, !hwe))
99 				return false;
100 
101 			match = hwe->class == r->engine_class;
102 			break;
103 		case XE_RTP_MATCH_NOT_ENGINE_CLASS:
104 			if (drm_WARN_ON(&xe->drm, !hwe))
105 				return false;
106 
107 			match = hwe->class != r->engine_class;
108 			break;
109 		case XE_RTP_MATCH_FUNC:
110 			match = r->match_func(gt, hwe);
111 			break;
112 		default:
113 			drm_warn(&xe->drm, "Invalid RTP match %u\n",
114 				 r->match_type);
115 			match = false;
116 		}
117 
118 		if (!match) {
119 			/*
120 			 * Advance rules until we find XE_RTP_MATCH_OR to check
121 			 * if there's another set of conditions to check
122 			 */
123 			while (i < n_rules && rules[++i].match_type != XE_RTP_MATCH_OR)
124 				;
125 
126 			if (i >= n_rules)
127 				return false;
128 
129 			rcount = 0;
130 		} else {
131 			rcount++;
132 		}
133 	}
134 
135 done:
136 	if (drm_WARN_ON(&xe->drm, !rcount))
137 		return false;
138 
139 	return true;
140 }
141 
142 static void rtp_add_sr_entry(const struct xe_rtp_action *action,
143 			     struct xe_gt *gt,
144 			     u32 mmio_base,
145 			     struct xe_reg_sr *sr)
146 {
147 	struct xe_reg_sr_entry sr_entry = {
148 		.reg = action->reg,
149 		.clr_bits = action->clr_bits,
150 		.set_bits = action->set_bits,
151 		.read_mask = action->read_mask,
152 	};
153 
154 	sr_entry.reg.addr += mmio_base;
155 	xe_reg_sr_add(sr, &sr_entry, gt);
156 }
157 
158 static bool rtp_process_one_sr(const struct xe_rtp_entry_sr *entry,
159 			       struct xe_device *xe, struct xe_gt *gt,
160 			       struct xe_hw_engine *hwe, struct xe_reg_sr *sr)
161 {
162 	const struct xe_rtp_action *action;
163 	u32 mmio_base;
164 	unsigned int i;
165 
166 	if (!rule_matches(xe, gt, hwe, entry->rules, entry->n_rules))
167 		return false;
168 
169 	for (i = 0, action = &entry->actions[0]; i < entry->n_actions; action++, i++) {
170 		if ((entry->flags & XE_RTP_ENTRY_FLAG_FOREACH_ENGINE) ||
171 		    (action->flags & XE_RTP_ACTION_FLAG_ENGINE_BASE))
172 			mmio_base = hwe->mmio_base;
173 		else
174 			mmio_base = 0;
175 
176 		rtp_add_sr_entry(action, gt, mmio_base, sr);
177 	}
178 
179 	return true;
180 }
181 
182 static void rtp_get_context(struct xe_rtp_process_ctx *ctx,
183 			    struct xe_hw_engine **hwe,
184 			    struct xe_gt **gt,
185 			    struct xe_device **xe)
186 {
187 	switch (ctx->type) {
188 	case XE_RTP_PROCESS_TYPE_GT:
189 		*hwe = NULL;
190 		*gt = ctx->gt;
191 		*xe = gt_to_xe(*gt);
192 		break;
193 	case XE_RTP_PROCESS_TYPE_ENGINE:
194 		*hwe = ctx->hwe;
195 		*gt = (*hwe)->gt;
196 		*xe = gt_to_xe(*gt);
197 		break;
198 	};
199 }
200 
201 /**
202  * xe_rtp_process_ctx_enable_active_tracking - Enable tracking of active entries
203  *
204  * Set additional metadata to track what entries are considered "active", i.e.
205  * their rules match the condition. Bits are never cleared: entries with
206  * matching rules set the corresponding bit in the bitmap.
207  *
208  * @ctx: The context for processing the table
209  * @active_entries: bitmap to store the active entries
210  * @n_entries: number of entries to be processed
211  */
212 void xe_rtp_process_ctx_enable_active_tracking(struct xe_rtp_process_ctx *ctx,
213 					       unsigned long *active_entries,
214 					       size_t n_entries)
215 {
216 	ctx->active_entries = active_entries;
217 	ctx->n_entries = n_entries;
218 }
219 
220 static void rtp_mark_active(struct xe_device *xe,
221 			    struct xe_rtp_process_ctx *ctx,
222 			    unsigned int first, unsigned int last)
223 {
224 	if (!ctx->active_entries)
225 		return;
226 
227 	if (drm_WARN_ON(&xe->drm, last > ctx->n_entries))
228 		return;
229 
230 	if (first == last)
231 		bitmap_set(ctx->active_entries, first, 1);
232 	else
233 		bitmap_set(ctx->active_entries, first, last - first + 2);
234 }
235 
236 /**
237  * xe_rtp_process_to_sr - Process all rtp @entries, adding the matching ones to
238  *                        the save-restore argument.
239  * @ctx: The context for processing the table, with one of device, gt or hwe
240  * @entries: Table with RTP definitions
241  * @sr: Save-restore struct where matching rules execute the action. This can be
242  *      viewed as the "coalesced view" of multiple the tables. The bits for each
243  *      register set are expected not to collide with previously added entries
244  *
245  * Walk the table pointed by @entries (with an empty sentinel) and add all
246  * entries with matching rules to @sr. If @hwe is not NULL, its mmio_base is
247  * used to calculate the right register offset
248  */
249 void xe_rtp_process_to_sr(struct xe_rtp_process_ctx *ctx,
250 			  const struct xe_rtp_entry_sr *entries,
251 			  struct xe_reg_sr *sr)
252 {
253 	const struct xe_rtp_entry_sr *entry;
254 	struct xe_hw_engine *hwe = NULL;
255 	struct xe_gt *gt = NULL;
256 	struct xe_device *xe = NULL;
257 
258 	rtp_get_context(ctx, &hwe, &gt, &xe);
259 
260 	for (entry = entries; entry && entry->name; entry++) {
261 		bool match = false;
262 
263 		if (entry->flags & XE_RTP_ENTRY_FLAG_FOREACH_ENGINE) {
264 			struct xe_hw_engine *each_hwe;
265 			enum xe_hw_engine_id id;
266 
267 			for_each_hw_engine(each_hwe, gt, id)
268 				match |= rtp_process_one_sr(entry, xe, gt,
269 							    each_hwe, sr);
270 		} else {
271 			match = rtp_process_one_sr(entry, xe, gt, hwe, sr);
272 		}
273 
274 		if (match)
275 			rtp_mark_active(xe, ctx, entry - entries,
276 					entry - entries);
277 	}
278 }
279 EXPORT_SYMBOL_IF_KUNIT(xe_rtp_process_to_sr);
280 
281 /**
282  * xe_rtp_process - Process all rtp @entries, without running any action
283  * @ctx: The context for processing the table, with one of device, gt or hwe
284  * @entries: Table with RTP definitions
285  *
286  * Walk the table pointed by @entries (with an empty sentinel), executing the
287  * rules. A few differences from xe_rtp_process_to_sr():
288  *
289  * 1. There is no action associated with each entry since this uses
290  *    struct xe_rtp_entry. Its main use is for marking active workarounds via
291  *    xe_rtp_process_ctx_enable_active_tracking().
292  * 2. There is support for OR operations by having entries with no name.
293  */
294 void xe_rtp_process(struct xe_rtp_process_ctx *ctx,
295 		    const struct xe_rtp_entry *entries)
296 {
297 	const struct xe_rtp_entry *entry, *first_entry;
298 	struct xe_hw_engine *hwe;
299 	struct xe_gt *gt;
300 	struct xe_device *xe;
301 
302 	rtp_get_context(ctx, &hwe, &gt, &xe);
303 
304 	first_entry = entries;
305 	if (drm_WARN_ON(&xe->drm, !first_entry->name))
306 		return;
307 
308 	for (entry = entries; entry && entry->rules; entry++) {
309 		if (entry->name)
310 			first_entry = entry;
311 
312 		if (!rule_matches(xe, gt, hwe, entry->rules, entry->n_rules))
313 			continue;
314 
315 		/* Fast-forward entry, eliminating the OR'ed entries */
316 		for (entry++; entry && entry->rules; entry++)
317 			if (entry->name)
318 				break;
319 		entry--;
320 
321 		rtp_mark_active(xe, ctx, first_entry - entries,
322 				entry - entries);
323 	}
324 }
325 
326 bool xe_rtp_match_even_instance(const struct xe_gt *gt,
327 				const struct xe_hw_engine *hwe)
328 {
329 	return hwe->instance % 2 == 0;
330 }
331 
332 bool xe_rtp_match_first_render_or_compute(const struct xe_gt *gt,
333 					  const struct xe_hw_engine *hwe)
334 {
335 	u64 render_compute_mask = gt->info.engine_mask &
336 		(XE_HW_ENGINE_CCS_MASK | XE_HW_ENGINE_RCS_MASK);
337 
338 	return render_compute_mask &&
339 		hwe->engine_id == __ffs(render_compute_mask);
340 }
341 
342 bool xe_rtp_match_first_gslice_fused_off(const struct xe_gt *gt,
343 					 const struct xe_hw_engine *hwe)
344 {
345 	unsigned int dss_per_gslice = 4;
346 	unsigned int dss;
347 
348 	if (drm_WARN(&gt_to_xe(gt)->drm, xe_dss_mask_empty(gt->fuse_topo.g_dss_mask),
349 		     "Checking gslice for platform without geometry pipeline\n"))
350 		return false;
351 
352 	dss = xe_dss_mask_group_ffs(gt->fuse_topo.g_dss_mask, 0, 0);
353 
354 	return dss >= dss_per_gslice;
355 }
356 
357