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
has_samedia(const struct xe_device * xe)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
rule_is_item(const struct xe_rtp_rule * r)43 static bool rule_is_item(const struct xe_rtp_rule *r)
44 {
45 return r->match_type != XE_RTP_MATCH_OR;
46 }
47
rule_match_item(struct rule_match_ctx * match_ctx)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 */
rule_match_and(struct rule_match_ctx * match_ctx,bool parse_only)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 */
rule_match_or(struct rule_match_ctx * match_ctx)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
rule_matches_with_err(const struct xe_device * xe,struct xe_gt * gt,struct xe_hw_engine * hwe,const struct xe_rtp_rule * rules,unsigned int n_rules,int * err)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
rule_matches(const struct xe_device * xe,struct xe_gt * gt,struct xe_hw_engine * hwe,const struct xe_rtp_rule * rules,unsigned int n_rules)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
rtp_add_sr_entry(const struct xe_rtp_action * action,struct xe_gt * gt,struct xe_hw_engine * hwe,u32 mmio_base,struct xe_reg_sr * sr)228 static void rtp_add_sr_entry(const struct xe_rtp_action *action,
229 struct xe_gt *gt,
230 struct xe_hw_engine *hwe,
231 u32 mmio_base,
232 struct xe_reg_sr *sr)
233 {
234 struct xe_reg_sr_entry sr_entry = {
235 .reg = action->reg,
236 .clr_bits = action->clr_bits,
237 .read_mask = action->read_mask,
238 };
239
240 if (action->use_func)
241 sr_entry.set_bits = action->set_func(gt, hwe);
242 else
243 sr_entry.set_bits = action->set_bits;
244
245 sr_entry.reg.addr += mmio_base;
246
247 xe_reg_sr_add(sr, &sr_entry, gt);
248 }
249
rtp_process_one_sr(const struct xe_rtp_entry_sr * entry,struct xe_device * xe,struct xe_gt * gt,struct xe_hw_engine * hwe,struct xe_reg_sr * sr)250 static bool rtp_process_one_sr(const struct xe_rtp_entry_sr *entry,
251 struct xe_device *xe, struct xe_gt *gt,
252 struct xe_hw_engine *hwe, struct xe_reg_sr *sr)
253 {
254 const struct xe_rtp_action *action;
255 u32 mmio_base;
256 unsigned int i;
257
258 if (!rule_matches(xe, gt, hwe, entry->rules, entry->n_rules))
259 return false;
260
261 for (i = 0, action = &entry->actions[0]; i < entry->n_actions; action++, i++) {
262 if ((entry->flags & XE_RTP_ENTRY_FLAG_FOREACH_ENGINE) ||
263 (action->flags & XE_RTP_ACTION_FLAG_ENGINE_BASE))
264 mmio_base = hwe->mmio_base;
265 else
266 mmio_base = 0;
267
268 rtp_add_sr_entry(action, gt, hwe, mmio_base, sr);
269 }
270
271 return true;
272 }
273
rtp_get_context(struct xe_rtp_process_ctx * ctx,struct xe_hw_engine ** hwe,struct xe_gt ** gt,struct xe_device ** xe)274 static void rtp_get_context(struct xe_rtp_process_ctx *ctx,
275 struct xe_hw_engine **hwe,
276 struct xe_gt **gt,
277 struct xe_device **xe)
278 {
279 switch (ctx->type) {
280 case XE_RTP_PROCESS_TYPE_DEVICE:
281 *hwe = NULL;
282 *gt = NULL;
283 *xe = ctx->xe;
284 break;
285 case XE_RTP_PROCESS_TYPE_GT:
286 *hwe = NULL;
287 *gt = ctx->gt;
288 *xe = gt_to_xe(*gt);
289 break;
290 case XE_RTP_PROCESS_TYPE_ENGINE:
291 *hwe = ctx->hwe;
292 *gt = (*hwe)->gt;
293 *xe = gt_to_xe(*gt);
294 break;
295 }
296 }
297
298 /**
299 * xe_rtp_process_ctx_enable_active_tracking - Enable tracking of active entries
300 *
301 * Set additional metadata to track what entries are considered "active", i.e.
302 * their rules match the condition. Bits are never cleared: entries with
303 * matching rules set the corresponding bit in the bitmap.
304 *
305 * @ctx: The context for processing the table
306 * @active_entries: bitmap to store the active entries
307 * @n_entries: number of entries to be processed
308 */
xe_rtp_process_ctx_enable_active_tracking(struct xe_rtp_process_ctx * ctx,unsigned long * active_entries,size_t n_entries)309 void xe_rtp_process_ctx_enable_active_tracking(struct xe_rtp_process_ctx *ctx,
310 unsigned long *active_entries,
311 size_t n_entries)
312 {
313 ctx->active_entries = active_entries;
314 ctx->n_entries = n_entries;
315 }
316 EXPORT_SYMBOL_IF_KUNIT(xe_rtp_process_ctx_enable_active_tracking);
317
rtp_mark_active(struct xe_device * xe,struct xe_rtp_process_ctx * ctx,unsigned int idx)318 static void rtp_mark_active(struct xe_device *xe,
319 struct xe_rtp_process_ctx *ctx,
320 unsigned int idx)
321 {
322 if (!ctx->active_entries)
323 return;
324
325 if (drm_WARN_ON(&xe->drm, idx >= ctx->n_entries))
326 return;
327
328 bitmap_set(ctx->active_entries, idx, 1);
329 }
330
331 /**
332 * xe_rtp_process_to_sr - Process all rtp @entries, adding the matching ones to
333 * the save-restore argument.
334 * @ctx: The context for processing the table, with one of device, gt or hwe
335 * @table: Table with RTP definitions
336 * @sr: Save-restore struct where matching rules execute the action. This can be
337 * viewed as the "coalesced view" of multiple the tables. The bits for each
338 * register set are expected not to collide with previously added entries
339 * @process_in_vf: Whether this RTP table should get processed for SR-IOV VF
340 * devices. Should generally only be 'true' for LRC tables.
341 *
342 * Walk the table pointed by @entries (with an empty sentinel) and add all
343 * entries with matching rules to @sr. If @hwe is not NULL, its mmio_base is
344 * used to calculate the right register offset
345 */
xe_rtp_process_to_sr(struct xe_rtp_process_ctx * ctx,const struct xe_rtp_table_sr * table,struct xe_reg_sr * sr,bool process_in_vf)346 void xe_rtp_process_to_sr(struct xe_rtp_process_ctx *ctx,
347 const struct xe_rtp_table_sr *table,
348 struct xe_reg_sr *sr,
349 bool process_in_vf)
350 {
351 struct xe_hw_engine *hwe = NULL;
352 struct xe_gt *gt = NULL;
353 struct xe_device *xe = NULL;
354
355 rtp_get_context(ctx, &hwe, >, &xe);
356
357 if (!process_in_vf && IS_SRIOV_VF(xe))
358 return;
359
360 xe_assert(xe, table->entries);
361
362 for (size_t i = 0; i < table->n_entries; i++) {
363 const struct xe_rtp_entry_sr *entry = &table->entries[i];
364 bool match = false;
365
366 if (entry->flags & XE_RTP_ENTRY_FLAG_FOREACH_ENGINE) {
367 struct xe_hw_engine *each_hwe;
368 enum xe_hw_engine_id id;
369
370 for_each_hw_engine(each_hwe, gt, id)
371 match |= rtp_process_one_sr(entry, xe, gt,
372 each_hwe, sr);
373 } else {
374 match = rtp_process_one_sr(entry, xe, gt, hwe, sr);
375 }
376
377 if (match)
378 rtp_mark_active(xe, ctx, i);
379 }
380 }
381 EXPORT_SYMBOL_IF_KUNIT(xe_rtp_process_to_sr);
382
383 /**
384 * xe_rtp_process - Process all entries in rtp @table, without running any action
385 * @ctx: The context for processing the table, with one of device, gt or hwe
386 * @table: Table with RTP definitions
387 *
388 * Walk the table pointed by @table, executing the
389 * rules. One difference from xe_rtp_process_to_sr(): there is no action
390 * associated with each entry since this uses struct xe_rtp_entry. Its main use
391 * is for marking active workarounds via
392 * xe_rtp_process_ctx_enable_active_tracking().
393 */
xe_rtp_process(struct xe_rtp_process_ctx * ctx,const struct xe_rtp_table * table)394 void xe_rtp_process(struct xe_rtp_process_ctx *ctx,
395 const struct xe_rtp_table *table)
396 {
397 struct xe_hw_engine *hwe;
398 struct xe_gt *gt;
399 struct xe_device *xe;
400
401 rtp_get_context(ctx, &hwe, >, &xe);
402
403 xe_assert(xe, table->entries);
404
405 for (size_t i = 0; i < table->n_entries; i++) {
406 const struct xe_rtp_entry *entry = &table->entries[i];
407
408 if (!rule_matches(xe, gt, hwe, entry->rules, entry->n_rules))
409 continue;
410
411 rtp_mark_active(xe, ctx, i);
412 }
413 }
414 EXPORT_SYMBOL_IF_KUNIT(xe_rtp_process);
415
xe_rtp_match_always(const struct xe_device * xe,const struct xe_gt * gt,const struct xe_hw_engine * hwe)416 bool xe_rtp_match_always(const struct xe_device *xe,
417 const struct xe_gt *gt,
418 const struct xe_hw_engine *hwe)
419 {
420 return true;
421 }
422
xe_rtp_match_even_instance(const struct xe_device * xe,const struct xe_gt * gt,const struct xe_hw_engine * hwe)423 bool xe_rtp_match_even_instance(const struct xe_device *xe,
424 const struct xe_gt *gt,
425 const struct xe_hw_engine *hwe)
426 {
427 return hwe->instance % 2 == 0;
428 }
429
xe_rtp_match_first_render_or_compute(const struct xe_device * xe,const struct xe_gt * gt,const struct xe_hw_engine * hwe)430 bool xe_rtp_match_first_render_or_compute(const struct xe_device *xe,
431 const struct xe_gt *gt,
432 const struct xe_hw_engine *hwe)
433 {
434 u64 render_compute_mask = gt->info.engine_mask &
435 (XE_HW_ENGINE_CCS_MASK | XE_HW_ENGINE_RCS_MASK);
436
437 return render_compute_mask &&
438 hwe->engine_id == __ffs(render_compute_mask);
439 }
440
xe_rtp_match_not_sriov_vf(const struct xe_device * xe,const struct xe_gt * gt,const struct xe_hw_engine * hwe)441 bool xe_rtp_match_not_sriov_vf(const struct xe_device *xe,
442 const struct xe_gt *gt,
443 const struct xe_hw_engine *hwe)
444 {
445 return !IS_SRIOV_VF(xe);
446 }
447
xe_rtp_match_psmi_enabled(const struct xe_device * xe,const struct xe_gt * gt,const struct xe_hw_engine * hwe)448 bool xe_rtp_match_psmi_enabled(const struct xe_device *xe,
449 const struct xe_gt *gt,
450 const struct xe_hw_engine *hwe)
451 {
452 return xe_configfs_get_psmi_enabled(to_pci_dev(xe->drm.dev));
453 }
454
xe_rtp_match_gt_has_discontiguous_dss_groups(const struct xe_device * xe,const struct xe_gt * gt,const struct xe_hw_engine * hwe)455 bool xe_rtp_match_gt_has_discontiguous_dss_groups(const struct xe_device *xe,
456 const struct xe_gt *gt,
457 const struct xe_hw_engine *hwe)
458 {
459 return xe_gt_has_discontiguous_dss_groups(gt);
460 }
461
xe_rtp_match_has_flat_ccs(const struct xe_device * xe,const struct xe_gt * gt,const struct xe_hw_engine * hwe)462 bool xe_rtp_match_has_flat_ccs(const struct xe_device *xe,
463 const struct xe_gt *gt,
464 const struct xe_hw_engine *hwe)
465 {
466 return xe->info.has_flat_ccs;
467 }
468
xe_rtp_match_has_msix(const struct xe_device * xe,const struct xe_gt * gt,const struct xe_hw_engine * hwe)469 bool xe_rtp_match_has_msix(const struct xe_device *xe,
470 const struct xe_gt *gt,
471 const struct xe_hw_engine *hwe)
472 {
473 return xe_device_has_msix(xe);
474 }
475
476 #if IS_ENABLED(CONFIG_DRM_XE_KUNIT_TEST)
477 #include "tests/xe_rtp.c"
478 #endif
479