1 // SPDX-License-Identifier: MIT
3 * Copyright © 2022 Intel Corporation
8 #include <kunit/visibility.h>
10 #include <drm/xe_drm.h>
13 #include "xe_gt_topology.h"
14 #include "xe_macros.h"
15 #include "xe_reg_sr.h"
18 * DOC: Register Table Processing
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.
26 static bool has_samedia(const struct xe_device *xe)
28 return xe->info.media_verx100 >= 1300;
31 static bool rule_matches(const struct xe_device *xe,
33 struct xe_hw_engine *hwe,
34 const struct xe_rtp_rule *rules,
37 const struct xe_rtp_rule *r;
41 for (r = rules, i = 0; i < n_rules; r = &rules[++i]) {
42 switch (r->match_type) {
43 case XE_RTP_MATCH_PLATFORM:
44 match = xe->info.platform == r->platform;
46 case XE_RTP_MATCH_SUBPLATFORM:
47 match = xe->info.platform == r->platform &&
48 xe->info.subplatform == r->subplatform;
50 case XE_RTP_MATCH_GRAPHICS_VERSION:
51 match = xe->info.graphics_verx100 == r->ver_start &&
52 (!has_samedia(xe) || !xe_gt_is_media_type(gt));
54 case XE_RTP_MATCH_GRAPHICS_VERSION_RANGE:
55 match = xe->info.graphics_verx100 >= r->ver_start &&
56 xe->info.graphics_verx100 <= r->ver_end &&
57 (!has_samedia(xe) || !xe_gt_is_media_type(gt));
59 case XE_RTP_MATCH_GRAPHICS_STEP:
60 match = xe->info.step.graphics >= r->step_start &&
61 xe->info.step.graphics < r->step_end &&
62 (!has_samedia(xe) || !xe_gt_is_media_type(gt));
64 case XE_RTP_MATCH_MEDIA_VERSION:
65 match = xe->info.media_verx100 == r->ver_start &&
66 (!has_samedia(xe) || xe_gt_is_media_type(gt));
68 case XE_RTP_MATCH_MEDIA_VERSION_RANGE:
69 match = xe->info.media_verx100 >= r->ver_start &&
70 xe->info.media_verx100 <= r->ver_end &&
71 (!has_samedia(xe) || xe_gt_is_media_type(gt));
73 case XE_RTP_MATCH_MEDIA_STEP:
74 match = xe->info.step.media >= r->step_start &&
75 xe->info.step.media < r->step_end &&
76 (!has_samedia(xe) || xe_gt_is_media_type(gt));
78 case XE_RTP_MATCH_INTEGRATED:
79 match = !xe->info.is_dgfx;
81 case XE_RTP_MATCH_DISCRETE:
82 match = xe->info.is_dgfx;
84 case XE_RTP_MATCH_ENGINE_CLASS:
85 if (drm_WARN_ON(&xe->drm, !hwe))
88 match = hwe->class == r->engine_class;
90 case XE_RTP_MATCH_NOT_ENGINE_CLASS:
91 if (drm_WARN_ON(&xe->drm, !hwe))
94 match = hwe->class != r->engine_class;
96 case XE_RTP_MATCH_FUNC:
97 match = r->match_func(gt, hwe);
100 drm_warn(&xe->drm, "Invalid RTP match %u\n",
112 static void rtp_add_sr_entry(const struct xe_rtp_action *action,
115 struct xe_reg_sr *sr)
117 struct xe_reg_sr_entry sr_entry = {
119 .clr_bits = action->clr_bits,
120 .set_bits = action->set_bits,
121 .read_mask = action->read_mask,
124 sr_entry.reg.addr += mmio_base;
125 xe_reg_sr_add(sr, &sr_entry, gt);
128 static bool rtp_process_one_sr(const struct xe_rtp_entry_sr *entry,
129 struct xe_device *xe, struct xe_gt *gt,
130 struct xe_hw_engine *hwe, struct xe_reg_sr *sr)
132 const struct xe_rtp_action *action;
136 if (!rule_matches(xe, gt, hwe, entry->rules, entry->n_rules))
139 for (i = 0, action = &entry->actions[0]; i < entry->n_actions; action++, i++) {
140 if ((entry->flags & XE_RTP_ENTRY_FLAG_FOREACH_ENGINE) ||
141 (action->flags & XE_RTP_ACTION_FLAG_ENGINE_BASE))
142 mmio_base = hwe->mmio_base;
146 rtp_add_sr_entry(action, gt, mmio_base, sr);
152 static void rtp_get_context(struct xe_rtp_process_ctx *ctx,
153 struct xe_hw_engine **hwe,
155 struct xe_device **xe)
158 case XE_RTP_PROCESS_TYPE_GT:
163 case XE_RTP_PROCESS_TYPE_ENGINE:
172 * xe_rtp_process_ctx_enable_active_tracking - Enable tracking of active entries
174 * Set additional metadata to track what entries are considered "active", i.e.
175 * their rules match the condition. Bits are never cleared: entries with
176 * matching rules set the corresponding bit in the bitmap.
178 * @ctx: The context for processing the table
179 * @active_entries: bitmap to store the active entries
180 * @n_entries: number of entries to be processed
182 void xe_rtp_process_ctx_enable_active_tracking(struct xe_rtp_process_ctx *ctx,
183 unsigned long *active_entries,
186 ctx->active_entries = active_entries;
187 ctx->n_entries = n_entries;
190 static void rtp_mark_active(struct xe_device *xe,
191 struct xe_rtp_process_ctx *ctx,
192 unsigned int first, unsigned int last)
194 if (!ctx->active_entries)
197 if (drm_WARN_ON(&xe->drm, last > ctx->n_entries))
201 bitmap_set(ctx->active_entries, first, 1);
203 bitmap_set(ctx->active_entries, first, last - first + 2);
207 * xe_rtp_process_to_sr - Process all rtp @entries, adding the matching ones to
208 * the save-restore argument.
209 * @ctx: The context for processing the table, with one of device, gt or hwe
210 * @entries: Table with RTP definitions
211 * @sr: Save-restore struct where matching rules execute the action. This can be
212 * viewed as the "coalesced view" of multiple the tables. The bits for each
213 * register set are expected not to collide with previously added entries
215 * Walk the table pointed by @entries (with an empty sentinel) and add all
216 * entries with matching rules to @sr. If @hwe is not NULL, its mmio_base is
217 * used to calculate the right register offset
219 void xe_rtp_process_to_sr(struct xe_rtp_process_ctx *ctx,
220 const struct xe_rtp_entry_sr *entries,
221 struct xe_reg_sr *sr)
223 const struct xe_rtp_entry_sr *entry;
224 struct xe_hw_engine *hwe = NULL;
225 struct xe_gt *gt = NULL;
226 struct xe_device *xe = NULL;
228 rtp_get_context(ctx, &hwe, >, &xe);
230 for (entry = entries; entry && entry->name; entry++) {
233 if (entry->flags & XE_RTP_ENTRY_FLAG_FOREACH_ENGINE) {
234 struct xe_hw_engine *each_hwe;
235 enum xe_hw_engine_id id;
237 for_each_hw_engine(each_hwe, gt, id)
238 match |= rtp_process_one_sr(entry, xe, gt,
241 match = rtp_process_one_sr(entry, xe, gt, hwe, sr);
245 rtp_mark_active(xe, ctx, entry - entries,
249 EXPORT_SYMBOL_IF_KUNIT(xe_rtp_process_to_sr);
252 * xe_rtp_process - Process all rtp @entries, without running any action
253 * @ctx: The context for processing the table, with one of device, gt or hwe
254 * @entries: Table with RTP definitions
256 * Walk the table pointed by @entries (with an empty sentinel), executing the
257 * rules. A few differences from xe_rtp_process_to_sr():
259 * 1. There is no action associated with each entry since this uses
260 * struct xe_rtp_entry. Its main use is for marking active workarounds via
261 * xe_rtp_process_ctx_enable_active_tracking().
262 * 2. There is support for OR operations by having entries with no name.
264 void xe_rtp_process(struct xe_rtp_process_ctx *ctx,
265 const struct xe_rtp_entry *entries)
267 const struct xe_rtp_entry *entry, *first_entry;
268 struct xe_hw_engine *hwe;
270 struct xe_device *xe;
272 rtp_get_context(ctx, &hwe, >, &xe);
274 first_entry = entries;
275 if (drm_WARN_ON(&xe->drm, !first_entry->name))
278 for (entry = entries; entry && entry->rules; entry++) {
282 if (!rule_matches(xe, gt, hwe, entry->rules, entry->n_rules))
285 /* Fast-forward entry, eliminating the OR'ed entries */
286 for (entry++; entry && entry->rules; entry++)
291 rtp_mark_active(xe, ctx, first_entry - entries,
296 bool xe_rtp_match_even_instance(const struct xe_gt *gt,
297 const struct xe_hw_engine *hwe)
299 return hwe->instance % 2 == 0;
302 bool xe_rtp_match_first_render_or_compute(const struct xe_gt *gt,
303 const struct xe_hw_engine *hwe)
305 u64 render_compute_mask = gt->info.engine_mask &
306 (XE_HW_ENGINE_CCS_MASK | XE_HW_ENGINE_RCS_MASK);
308 return render_compute_mask &&
309 hwe->engine_id == __ffs(render_compute_mask);
312 bool xe_rtp_match_first_gslice_fused_off(const struct xe_gt *gt,
313 const struct xe_hw_engine *hwe)
315 unsigned int dss_per_gslice = 4;
318 if (drm_WARN(>_to_xe(gt)->drm, xe_dss_mask_empty(gt->fuse_topo.g_dss_mask),
319 "Checking gslice for platform without geometry pipeline\n"))
322 dss = xe_dss_mask_group_ffs(gt->fuse_topo.g_dss_mask, 0, 0);
324 return dss >= dss_per_gslice;