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