1 // SPDX-License-Identifier: MIT
3 * Copyright © 2019 Intel Corporation
6 #include <linux/list.h>
7 #include <linux/list_sort.h>
8 #include <linux/llist.h>
11 #include "intel_engine.h"
12 #include "intel_engine_user.h"
14 #include "uc/intel_guc_submission.h"
16 struct intel_engine_cs *
17 intel_engine_lookup_user(struct drm_i915_private *i915, u8 class, u8 instance)
19 struct rb_node *p = i915->uabi_engines.rb_node;
22 struct intel_engine_cs *it =
23 rb_entry(p, typeof(*it), uabi_node);
25 if (class < it->uabi_class)
27 else if (class > it->uabi_class ||
28 instance > it->uabi_instance)
30 else if (instance < it->uabi_instance)
39 void intel_engine_add_user(struct intel_engine_cs *engine)
41 llist_add(&engine->uabi_llist, &engine->i915->uabi_engines_llist);
44 static const u8 uabi_classes[] = {
45 [RENDER_CLASS] = I915_ENGINE_CLASS_RENDER,
46 [COPY_ENGINE_CLASS] = I915_ENGINE_CLASS_COPY,
47 [VIDEO_DECODE_CLASS] = I915_ENGINE_CLASS_VIDEO,
48 [VIDEO_ENHANCEMENT_CLASS] = I915_ENGINE_CLASS_VIDEO_ENHANCE,
49 [COMPUTE_CLASS] = I915_ENGINE_CLASS_COMPUTE,
52 static int engine_cmp(void *priv, const struct list_head *A,
53 const struct list_head *B)
55 const struct intel_engine_cs *a =
56 container_of(A, typeof(*a), uabi_list);
57 const struct intel_engine_cs *b =
58 container_of(B, typeof(*b), uabi_list);
60 if (uabi_classes[a->class] < uabi_classes[b->class])
62 if (uabi_classes[a->class] > uabi_classes[b->class])
65 if (a->instance < b->instance)
67 if (a->instance > b->instance)
73 static struct llist_node *get_engines(struct drm_i915_private *i915)
75 return llist_del_all(&i915->uabi_engines_llist);
78 static void sort_engines(struct drm_i915_private *i915,
79 struct list_head *engines)
81 struct llist_node *pos, *next;
83 llist_for_each_safe(pos, next, get_engines(i915)) {
84 struct intel_engine_cs *engine =
85 container_of(pos, typeof(*engine), uabi_llist);
86 list_add(&engine->uabi_list, engines);
88 list_sort(NULL, engines, engine_cmp);
91 static void set_scheduler_caps(struct drm_i915_private *i915)
97 #define MAP(x, y) { ilog2(I915_ENGINE_##x), ilog2(I915_SCHEDULER_CAP_##y) }
98 MAP(HAS_PREEMPTION, PREEMPTION),
99 MAP(HAS_SEMAPHORES, SEMAPHORES),
100 MAP(SUPPORTS_STATS, ENGINE_BUSY_STATS),
103 struct intel_engine_cs *engine;
104 u32 enabled, disabled;
108 for_each_uabi_engine(engine, i915) { /* all engines must agree! */
111 if (engine->sched_engine->schedule)
112 enabled |= (I915_SCHEDULER_CAP_ENABLED |
113 I915_SCHEDULER_CAP_PRIORITY);
115 disabled |= (I915_SCHEDULER_CAP_ENABLED |
116 I915_SCHEDULER_CAP_PRIORITY);
118 if (intel_uc_uses_guc_submission(&engine->gt->uc))
119 enabled |= I915_SCHEDULER_CAP_STATIC_PRIORITY_MAP;
121 for (i = 0; i < ARRAY_SIZE(map); i++) {
122 if (engine->flags & BIT(map[i].engine))
123 enabled |= BIT(map[i].sched);
125 disabled |= BIT(map[i].sched);
129 i915->caps.scheduler = enabled & ~disabled;
130 if (!(i915->caps.scheduler & I915_SCHEDULER_CAP_ENABLED))
131 i915->caps.scheduler = 0;
134 const char *intel_engine_class_repr(u8 class)
136 static const char * const uabi_names[] = {
137 [RENDER_CLASS] = "rcs",
138 [COPY_ENGINE_CLASS] = "bcs",
139 [VIDEO_DECODE_CLASS] = "vcs",
140 [VIDEO_ENHANCEMENT_CLASS] = "vecs",
141 [OTHER_CLASS] = "other",
142 [COMPUTE_CLASS] = "ccs",
145 if (class >= ARRAY_SIZE(uabi_names) || !uabi_names[class])
148 return uabi_names[class];
157 static int legacy_ring_idx(const struct legacy_ring *ring)
159 static const struct {
162 [RENDER_CLASS] = { RCS0, 1 },
163 [COPY_ENGINE_CLASS] = { BCS0, 1 },
164 [VIDEO_DECODE_CLASS] = { VCS0, I915_MAX_VCS },
165 [VIDEO_ENHANCEMENT_CLASS] = { VECS0, I915_MAX_VECS },
166 [COMPUTE_CLASS] = { CCS0, I915_MAX_CCS },
169 if (GEM_DEBUG_WARN_ON(ring->class >= ARRAY_SIZE(map)))
170 return INVALID_ENGINE;
172 if (GEM_DEBUG_WARN_ON(ring->instance >= map[ring->class].max))
173 return INVALID_ENGINE;
175 return map[ring->class].base + ring->instance;
178 static void add_legacy_ring(struct legacy_ring *ring,
179 struct intel_engine_cs *engine)
181 if (engine->gt != ring->gt || engine->class != ring->class) {
182 ring->gt = engine->gt;
183 ring->class = engine->class;
187 engine->legacy_idx = legacy_ring_idx(ring);
188 if (engine->legacy_idx != INVALID_ENGINE)
192 static void engine_rename(struct intel_engine_cs *engine, const char *name, u16 instance)
194 char old[sizeof(engine->name)];
196 memcpy(old, engine->name, sizeof(engine->name));
197 scnprintf(engine->name, sizeof(engine->name), "%s%u", name, instance);
198 drm_dbg(&engine->i915->drm, "renamed %s to %s\n", old, engine->name);
201 void intel_engines_driver_register(struct drm_i915_private *i915)
203 struct legacy_ring ring = {};
204 struct list_head *it, *next;
205 struct rb_node **p, *prev;
208 sort_engines(i915, &engines);
211 p = &i915->uabi_engines.rb_node;
212 list_for_each_safe(it, next, &engines) {
213 struct intel_engine_cs *engine =
214 container_of(it, typeof(*engine), uabi_list);
216 if (intel_gt_has_unrecoverable_error(engine->gt))
217 continue; /* ignore incomplete engines */
220 * We don't want to expose the GSC engine to the users, but we
221 * still rename it so it is easier to identify in the debug logs
223 if (engine->id == GSC0) {
224 engine_rename(engine, "gsc", 0);
228 GEM_BUG_ON(engine->class >= ARRAY_SIZE(uabi_classes));
229 engine->uabi_class = uabi_classes[engine->class];
231 GEM_BUG_ON(engine->uabi_class >=
232 ARRAY_SIZE(i915->engine_uabi_class_count));
233 engine->uabi_instance =
234 i915->engine_uabi_class_count[engine->uabi_class]++;
236 /* Replace the internal name with the final user facing name */
237 engine_rename(engine,
238 intel_engine_class_repr(engine->class),
239 engine->uabi_instance);
241 rb_link_node(&engine->uabi_node, prev, p);
242 rb_insert_color(&engine->uabi_node, &i915->uabi_engines);
244 GEM_BUG_ON(intel_engine_lookup_user(i915,
246 engine->uabi_instance) != engine);
248 /* Fix up the mapping to match default execbuf::user_map[] */
249 add_legacy_ring(&ring, engine);
251 prev = &engine->uabi_node;
255 if (IS_ENABLED(CONFIG_DRM_I915_SELFTESTS) &&
256 IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) {
257 struct intel_engine_cs *engine;
258 unsigned int isolation;
262 for (class = 0; class < ARRAY_SIZE(i915->engine_uabi_class_count); class++) {
263 for (inst = 0; inst < i915->engine_uabi_class_count[class]; inst++) {
264 engine = intel_engine_lookup_user(i915,
267 pr_err("UABI engine not found for { class:%d, instance:%d }\n",
273 if (engine->uabi_class != class ||
274 engine->uabi_instance != inst) {
275 pr_err("Wrong UABI engine:%s { class:%d, instance:%d } found for { class:%d, instance:%d }\n",
278 engine->uabi_instance,
287 * Make sure that classes with multiple engine instances all
288 * share the same basic configuration.
290 isolation = intel_engines_has_context_isolation(i915);
291 for_each_uabi_engine(engine, i915) {
292 unsigned int bit = BIT(engine->uabi_class);
293 unsigned int expected = engine->default_state ? bit : 0;
295 if ((isolation & bit) != expected) {
296 pr_err("mismatching default context state for class %d on engine %s\n",
297 engine->uabi_class, engine->name);
302 if (drm_WARN(&i915->drm, errors,
303 "Invalid UABI engine mapping found"))
304 i915->uabi_engines = RB_ROOT;
307 set_scheduler_caps(i915);
310 unsigned int intel_engines_has_context_isolation(struct drm_i915_private *i915)
312 struct intel_engine_cs *engine;
316 for_each_uabi_engine(engine, i915)
317 if (engine->default_state)
318 which |= BIT(engine->uabi_class);