1 /* 2 * SPDX-License-Identifier: MIT 3 * 4 * Copyright © 2019 Intel Corporation 5 */ 6 7 #include <linux/list.h> 8 #include <linux/list_sort.h> 9 #include <linux/llist.h> 10 11 #include "i915_drv.h" 12 #include "intel_engine.h" 13 #include "intel_engine_user.h" 14 #include "intel_gt.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 }; 51 52 static int engine_cmp(void *priv, const struct list_head *A, 53 const struct list_head *B) 54 { 55 const struct intel_engine_cs *a = 56 container_of((struct rb_node *)A, typeof(*a), uabi_node); 57 const struct intel_engine_cs *b = 58 container_of((struct rb_node *)B, typeof(*b), uabi_node); 59 60 if (uabi_classes[a->class] < uabi_classes[b->class]) 61 return -1; 62 if (uabi_classes[a->class] > uabi_classes[b->class]) 63 return 1; 64 65 if (a->instance < b->instance) 66 return -1; 67 if (a->instance > b->instance) 68 return 1; 69 70 return 0; 71 } 72 73 static struct llist_node *get_engines(struct drm_i915_private *i915) 74 { 75 return llist_del_all((struct llist_head *)&i915->uabi_engines); 76 } 77 78 static void sort_engines(struct drm_i915_private *i915, 79 struct list_head *engines) 80 { 81 struct llist_node *pos, *next; 82 83 llist_for_each_safe(pos, next, get_engines(i915)) { 84 struct intel_engine_cs *engine = 85 container_of((struct rb_node *)pos, typeof(*engine), 86 uabi_node); 87 list_add((struct list_head *)&engine->uabi_node, engines); 88 } 89 list_sort(NULL, engines, engine_cmp); 90 } 91 92 #ifdef __linux__ 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->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 for (i = 0; i < ARRAY_SIZE(map); i++) { 121 if (engine->flags & BIT(map[i].engine)) 122 enabled |= BIT(map[i].sched); 123 else 124 disabled |= BIT(map[i].sched); 125 } 126 } 127 128 i915->caps.scheduler = enabled & ~disabled; 129 if (!(i915->caps.scheduler & I915_SCHEDULER_CAP_ENABLED)) 130 i915->caps.scheduler = 0; 131 } 132 #else 133 /* without the pointless ilog2 -> BIT() */ 134 static void set_scheduler_caps(struct drm_i915_private *i915) 135 { 136 static const struct { 137 u8 engine; 138 u8 sched; 139 } map[] = { 140 #define MAP(x, y) { I915_ENGINE_##x, I915_SCHEDULER_CAP_##y } 141 MAP(HAS_PREEMPTION, PREEMPTION), 142 MAP(HAS_SEMAPHORES, SEMAPHORES), 143 MAP(SUPPORTS_STATS, ENGINE_BUSY_STATS), 144 #undef MAP 145 }; 146 struct intel_engine_cs *engine; 147 u32 enabled, disabled; 148 149 enabled = 0; 150 disabled = 0; 151 for_each_uabi_engine(engine, i915) { /* all engines must agree! */ 152 int i; 153 154 if (engine->schedule) 155 enabled |= (I915_SCHEDULER_CAP_ENABLED | 156 I915_SCHEDULER_CAP_PRIORITY); 157 else 158 disabled |= (I915_SCHEDULER_CAP_ENABLED | 159 I915_SCHEDULER_CAP_PRIORITY); 160 161 for (i = 0; i < ARRAY_SIZE(map); i++) { 162 if (engine->flags & map[i].engine) 163 enabled |= map[i].sched; 164 else 165 disabled |= map[i].sched; 166 } 167 } 168 169 i915->caps.scheduler = enabled & ~disabled; 170 if (!(i915->caps.scheduler & I915_SCHEDULER_CAP_ENABLED)) 171 i915->caps.scheduler = 0; 172 } 173 #endif 174 175 const char *intel_engine_class_repr(u8 class) 176 { 177 static const char * const uabi_names[] = { 178 [RENDER_CLASS] = "rcs", 179 [COPY_ENGINE_CLASS] = "bcs", 180 [VIDEO_DECODE_CLASS] = "vcs", 181 [VIDEO_ENHANCEMENT_CLASS] = "vecs", 182 }; 183 184 if (class >= ARRAY_SIZE(uabi_names) || !uabi_names[class]) 185 return "xxx"; 186 187 return uabi_names[class]; 188 } 189 190 struct legacy_ring { 191 struct intel_gt *gt; 192 u8 class; 193 u8 instance; 194 }; 195 196 static int legacy_ring_idx(const struct legacy_ring *ring) 197 { 198 static const struct { 199 u8 base, max; 200 } map[] = { 201 [RENDER_CLASS] = { RCS0, 1 }, 202 [COPY_ENGINE_CLASS] = { BCS0, 1 }, 203 [VIDEO_DECODE_CLASS] = { VCS0, I915_MAX_VCS }, 204 [VIDEO_ENHANCEMENT_CLASS] = { VECS0, I915_MAX_VECS }, 205 }; 206 207 if (GEM_DEBUG_WARN_ON(ring->class >= ARRAY_SIZE(map))) 208 return INVALID_ENGINE; 209 210 if (GEM_DEBUG_WARN_ON(ring->instance >= map[ring->class].max)) 211 return INVALID_ENGINE; 212 213 return map[ring->class].base + ring->instance; 214 } 215 216 static void add_legacy_ring(struct legacy_ring *ring, 217 struct intel_engine_cs *engine) 218 { 219 if (engine->gt != ring->gt || engine->class != ring->class) { 220 ring->gt = engine->gt; 221 ring->class = engine->class; 222 ring->instance = 0; 223 } 224 225 engine->legacy_idx = legacy_ring_idx(ring); 226 if (engine->legacy_idx != INVALID_ENGINE) 227 ring->instance++; 228 } 229 230 void intel_engines_driver_register(struct drm_i915_private *i915) 231 { 232 struct legacy_ring ring = {}; 233 u8 uabi_instances[4] = {}; 234 struct list_head *it, *next; 235 struct rb_node **p, *prev; 236 DRM_LIST_HEAD(engines); 237 238 sort_engines(i915, &engines); 239 240 prev = NULL; 241 p = &i915->uabi_engines.rb_node; 242 list_for_each_safe(it, next, &engines) { 243 struct intel_engine_cs *engine = 244 container_of((struct rb_node *)it, typeof(*engine), 245 uabi_node); 246 char old[sizeof(engine->name)]; 247 248 if (intel_gt_has_unrecoverable_error(engine->gt)) 249 continue; /* ignore incomplete engines */ 250 251 GEM_BUG_ON(engine->class >= ARRAY_SIZE(uabi_classes)); 252 engine->uabi_class = uabi_classes[engine->class]; 253 254 GEM_BUG_ON(engine->uabi_class >= ARRAY_SIZE(uabi_instances)); 255 engine->uabi_instance = uabi_instances[engine->uabi_class]++; 256 257 /* Replace the internal name with the final user facing name */ 258 memcpy(old, engine->name, sizeof(engine->name)); 259 scnprintf(engine->name, sizeof(engine->name), "%s%u", 260 intel_engine_class_repr(engine->class), 261 engine->uabi_instance); 262 DRM_DEBUG_DRIVER("renamed %s to %s\n", old, engine->name); 263 264 rb_link_node(&engine->uabi_node, prev, p); 265 rb_insert_color(&engine->uabi_node, &i915->uabi_engines); 266 267 GEM_BUG_ON(intel_engine_lookup_user(i915, 268 engine->uabi_class, 269 engine->uabi_instance) != engine); 270 271 /* Fix up the mapping to match default execbuf::user_map[] */ 272 add_legacy_ring(&ring, engine); 273 274 prev = &engine->uabi_node; 275 p = &prev->rb_right; 276 } 277 278 if (IS_ENABLED(CONFIG_DRM_I915_SELFTESTS) && 279 IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) { 280 struct intel_engine_cs *engine; 281 unsigned int isolation; 282 int class, inst; 283 int errors = 0; 284 285 for (class = 0; class < ARRAY_SIZE(uabi_instances); class++) { 286 for (inst = 0; inst < uabi_instances[class]; inst++) { 287 engine = intel_engine_lookup_user(i915, 288 class, inst); 289 if (!engine) { 290 pr_err("UABI engine not found for { class:%d, instance:%d }\n", 291 class, inst); 292 errors++; 293 continue; 294 } 295 296 if (engine->uabi_class != class || 297 engine->uabi_instance != inst) { 298 pr_err("Wrong UABI engine:%s { class:%d, instance:%d } found for { class:%d, instance:%d }\n", 299 engine->name, 300 engine->uabi_class, 301 engine->uabi_instance, 302 class, inst); 303 errors++; 304 continue; 305 } 306 } 307 } 308 309 /* 310 * Make sure that classes with multiple engine instances all 311 * share the same basic configuration. 312 */ 313 isolation = intel_engines_has_context_isolation(i915); 314 for_each_uabi_engine(engine, i915) { 315 unsigned int bit = BIT(engine->uabi_class); 316 unsigned int expected = engine->default_state ? bit : 0; 317 318 if ((isolation & bit) != expected) { 319 pr_err("mismatching default context state for class %d on engine %s\n", 320 engine->uabi_class, engine->name); 321 errors++; 322 } 323 } 324 325 if (drm_WARN(&i915->drm, errors, 326 "Invalid UABI engine mapping found")) 327 i915->uabi_engines = RB_ROOT; 328 } 329 330 set_scheduler_caps(i915); 331 } 332 333 unsigned int intel_engines_has_context_isolation(struct drm_i915_private *i915) 334 { 335 struct intel_engine_cs *engine; 336 unsigned int which; 337 338 which = 0; 339 for_each_uabi_engine(engine, i915) 340 if (engine->default_state) 341 which |= BIT(engine->uabi_class); 342 343 return which; 344 } 345