1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2019 Intel Corporation 4 */ 5 6 #include <linux/list.h> 7 #include <linux/list_sort.h> 8 #include <linux/llist.h> 9 10 #include "i915_drv.h" 11 #include "intel_engine.h" 12 #include "intel_engine_user.h" 13 #include "intel_gt.h" 14 #include "uc/intel_guc_submission.h" 15 16 struct intel_engine_cs * 17 intel_engine_lookup_user(struct drm_i915_private *i915, u8 class, u8 instance) 18 { 19 struct rb_node *p = i915->uabi_engines.rb_node; 20 21 while (p) { 22 struct intel_engine_cs *it = 23 rb_entry(p, typeof(*it), uabi_node); 24 25 if (class < it->uabi_class) 26 p = p->rb_left; 27 else if (class > it->uabi_class || 28 instance > it->uabi_instance) 29 p = p->rb_right; 30 else if (instance < it->uabi_instance) 31 p = p->rb_left; 32 else 33 return it; 34 } 35 36 return NULL; 37 } 38 39 void intel_engine_add_user(struct intel_engine_cs *engine) 40 { 41 llist_add((struct llist_node *)&engine->uabi_node, 42 (struct llist_head *)&engine->i915->uabi_engines); 43 } 44 45 static const u8 uabi_classes[] = { 46 [RENDER_CLASS] = I915_ENGINE_CLASS_RENDER, 47 [COPY_ENGINE_CLASS] = I915_ENGINE_CLASS_COPY, 48 [VIDEO_DECODE_CLASS] = I915_ENGINE_CLASS_VIDEO, 49 [VIDEO_ENHANCEMENT_CLASS] = I915_ENGINE_CLASS_VIDEO_ENHANCE, 50 /* TODO: Add COMPUTE_CLASS mapping once ABI is available */ 51 }; 52 53 static int engine_cmp(void *priv, const struct list_head *A, 54 const struct list_head *B) 55 { 56 const struct intel_engine_cs *a = 57 container_of((struct rb_node *)A, typeof(*a), uabi_node); 58 const struct intel_engine_cs *b = 59 container_of((struct rb_node *)B, typeof(*b), uabi_node); 60 61 if (uabi_classes[a->class] < uabi_classes[b->class]) 62 return -1; 63 if (uabi_classes[a->class] > uabi_classes[b->class]) 64 return 1; 65 66 if (a->instance < b->instance) 67 return -1; 68 if (a->instance > b->instance) 69 return 1; 70 71 return 0; 72 } 73 74 static struct llist_node *get_engines(struct drm_i915_private *i915) 75 { 76 return llist_del_all((struct llist_head *)&i915->uabi_engines); 77 } 78 79 static void sort_engines(struct drm_i915_private *i915, 80 struct list_head *engines) 81 { 82 struct llist_node *pos, *next; 83 84 llist_for_each_safe(pos, next, get_engines(i915)) { 85 struct intel_engine_cs *engine = 86 container_of((struct rb_node *)pos, typeof(*engine), 87 uabi_node); 88 list_add((struct list_head *)&engine->uabi_node, engines); 89 } 90 list_sort(NULL, engines, engine_cmp); 91 } 92 93 static void set_scheduler_caps(struct drm_i915_private *i915) 94 { 95 static const struct { 96 u8 engine; 97 u8 sched; 98 } map[] = { 99 #define MAP(x, y) { ilog2(I915_ENGINE_##x), ilog2(I915_SCHEDULER_CAP_##y) } 100 MAP(HAS_PREEMPTION, PREEMPTION), 101 MAP(HAS_SEMAPHORES, SEMAPHORES), 102 MAP(SUPPORTS_STATS, ENGINE_BUSY_STATS), 103 #undef MAP 104 }; 105 struct intel_engine_cs *engine; 106 u32 enabled, disabled; 107 108 enabled = 0; 109 disabled = 0; 110 for_each_uabi_engine(engine, i915) { /* all engines must agree! */ 111 int i; 112 113 if (engine->sched_engine->schedule) 114 enabled |= (I915_SCHEDULER_CAP_ENABLED | 115 I915_SCHEDULER_CAP_PRIORITY); 116 else 117 disabled |= (I915_SCHEDULER_CAP_ENABLED | 118 I915_SCHEDULER_CAP_PRIORITY); 119 120 if (intel_uc_uses_guc_submission(&to_gt(i915)->uc)) 121 enabled |= I915_SCHEDULER_CAP_STATIC_PRIORITY_MAP; 122 123 for (i = 0; i < ARRAY_SIZE(map); i++) { 124 if (engine->flags & BIT(map[i].engine)) 125 enabled |= BIT(map[i].sched); 126 else 127 disabled |= BIT(map[i].sched); 128 } 129 } 130 131 i915->caps.scheduler = enabled & ~disabled; 132 if (!(i915->caps.scheduler & I915_SCHEDULER_CAP_ENABLED)) 133 i915->caps.scheduler = 0; 134 } 135 136 const char *intel_engine_class_repr(u8 class) 137 { 138 static const char * const uabi_names[] = { 139 [RENDER_CLASS] = "rcs", 140 [COPY_ENGINE_CLASS] = "bcs", 141 [VIDEO_DECODE_CLASS] = "vcs", 142 [VIDEO_ENHANCEMENT_CLASS] = "vecs", 143 [COMPUTE_CLASS] = "ccs", 144 }; 145 146 if (class >= ARRAY_SIZE(uabi_names) || !uabi_names[class]) 147 return "xxx"; 148 149 return uabi_names[class]; 150 } 151 152 struct legacy_ring { 153 struct intel_gt *gt; 154 u8 class; 155 u8 instance; 156 }; 157 158 static int legacy_ring_idx(const struct legacy_ring *ring) 159 { 160 static const struct { 161 u8 base, max; 162 } map[] = { 163 [RENDER_CLASS] = { RCS0, 1 }, 164 [COPY_ENGINE_CLASS] = { BCS0, 1 }, 165 [VIDEO_DECODE_CLASS] = { VCS0, I915_MAX_VCS }, 166 [VIDEO_ENHANCEMENT_CLASS] = { VECS0, I915_MAX_VECS }, 167 [COMPUTE_CLASS] = { CCS0, I915_MAX_CCS }, 168 }; 169 170 if (GEM_DEBUG_WARN_ON(ring->class >= ARRAY_SIZE(map))) 171 return INVALID_ENGINE; 172 173 if (GEM_DEBUG_WARN_ON(ring->instance >= map[ring->class].max)) 174 return INVALID_ENGINE; 175 176 return map[ring->class].base + ring->instance; 177 } 178 179 static void add_legacy_ring(struct legacy_ring *ring, 180 struct intel_engine_cs *engine) 181 { 182 if (engine->gt != ring->gt || engine->class != ring->class) { 183 ring->gt = engine->gt; 184 ring->class = engine->class; 185 ring->instance = 0; 186 } 187 188 engine->legacy_idx = legacy_ring_idx(ring); 189 if (engine->legacy_idx != INVALID_ENGINE) 190 ring->instance++; 191 } 192 193 void intel_engines_driver_register(struct drm_i915_private *i915) 194 { 195 struct legacy_ring ring = {}; 196 u8 uabi_instances[5] = {}; 197 struct list_head *it, *next; 198 struct rb_node **p, *prev; 199 LIST_HEAD(engines); 200 201 sort_engines(i915, &engines); 202 203 prev = NULL; 204 p = &i915->uabi_engines.rb_node; 205 list_for_each_safe(it, next, &engines) { 206 struct intel_engine_cs *engine = 207 container_of((struct rb_node *)it, typeof(*engine), 208 uabi_node); 209 char old[sizeof(engine->name)]; 210 211 if (intel_gt_has_unrecoverable_error(engine->gt)) 212 continue; /* ignore incomplete engines */ 213 214 GEM_BUG_ON(engine->class >= ARRAY_SIZE(uabi_classes)); 215 engine->uabi_class = uabi_classes[engine->class]; 216 217 GEM_BUG_ON(engine->uabi_class >= ARRAY_SIZE(uabi_instances)); 218 engine->uabi_instance = uabi_instances[engine->uabi_class]++; 219 220 /* Replace the internal name with the final user facing name */ 221 memcpy(old, engine->name, sizeof(engine->name)); 222 scnprintf(engine->name, sizeof(engine->name), "%s%u", 223 intel_engine_class_repr(engine->class), 224 engine->uabi_instance); 225 DRM_DEBUG_DRIVER("renamed %s to %s\n", old, engine->name); 226 227 rb_link_node(&engine->uabi_node, prev, p); 228 rb_insert_color(&engine->uabi_node, &i915->uabi_engines); 229 230 GEM_BUG_ON(intel_engine_lookup_user(i915, 231 engine->uabi_class, 232 engine->uabi_instance) != engine); 233 234 /* Fix up the mapping to match default execbuf::user_map[] */ 235 add_legacy_ring(&ring, engine); 236 237 prev = &engine->uabi_node; 238 p = &prev->rb_right; 239 } 240 241 if (IS_ENABLED(CONFIG_DRM_I915_SELFTESTS) && 242 IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) { 243 struct intel_engine_cs *engine; 244 unsigned int isolation; 245 int class, inst; 246 int errors = 0; 247 248 for (class = 0; class < ARRAY_SIZE(uabi_instances); class++) { 249 for (inst = 0; inst < uabi_instances[class]; inst++) { 250 engine = intel_engine_lookup_user(i915, 251 class, inst); 252 if (!engine) { 253 pr_err("UABI engine not found for { class:%d, instance:%d }\n", 254 class, inst); 255 errors++; 256 continue; 257 } 258 259 if (engine->uabi_class != class || 260 engine->uabi_instance != inst) { 261 pr_err("Wrong UABI engine:%s { class:%d, instance:%d } found for { class:%d, instance:%d }\n", 262 engine->name, 263 engine->uabi_class, 264 engine->uabi_instance, 265 class, inst); 266 errors++; 267 continue; 268 } 269 } 270 } 271 272 /* 273 * Make sure that classes with multiple engine instances all 274 * share the same basic configuration. 275 */ 276 isolation = intel_engines_has_context_isolation(i915); 277 for_each_uabi_engine(engine, i915) { 278 unsigned int bit = BIT(engine->uabi_class); 279 unsigned int expected = engine->default_state ? bit : 0; 280 281 if ((isolation & bit) != expected) { 282 pr_err("mismatching default context state for class %d on engine %s\n", 283 engine->uabi_class, engine->name); 284 errors++; 285 } 286 } 287 288 if (drm_WARN(&i915->drm, errors, 289 "Invalid UABI engine mapping found")) 290 i915->uabi_engines = RB_ROOT; 291 } 292 293 set_scheduler_caps(i915); 294 } 295 296 unsigned int intel_engines_has_context_isolation(struct drm_i915_private *i915) 297 { 298 struct intel_engine_cs *engine; 299 unsigned int which; 300 301 which = 0; 302 for_each_uabi_engine(engine, i915) 303 if (engine->default_state) 304 which |= BIT(engine->uabi_class); 305 306 return which; 307 } 308