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((struct llist_node *)&engine->uabi_node,
42 (struct llist_head *)&engine->i915->uabi_engines);
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,
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((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);
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((struct llist_head *)&i915->uabi_engines);
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((struct rb_node *)pos, typeof(*engine),
87 list_add((struct list_head *)&engine->uabi_node, engines);
89 list_sort(NULL, engines, engine_cmp);
92 static void set_scheduler_caps(struct drm_i915_private *i915)
98 #define MAP(x, y) { ilog2(I915_ENGINE_##x), ilog2(I915_SCHEDULER_CAP_##y) }
99 MAP(HAS_PREEMPTION, PREEMPTION),
100 MAP(HAS_SEMAPHORES, SEMAPHORES),
101 MAP(SUPPORTS_STATS, ENGINE_BUSY_STATS),
104 struct intel_engine_cs *engine;
105 u32 enabled, disabled;
109 for_each_uabi_engine(engine, i915) { /* all engines must agree! */
112 if (engine->sched_engine->schedule)
113 enabled |= (I915_SCHEDULER_CAP_ENABLED |
114 I915_SCHEDULER_CAP_PRIORITY);
116 disabled |= (I915_SCHEDULER_CAP_ENABLED |
117 I915_SCHEDULER_CAP_PRIORITY);
119 if (intel_uc_uses_guc_submission(&i915->gt.uc))
120 enabled |= I915_SCHEDULER_CAP_STATIC_PRIORITY_MAP;
122 for (i = 0; i < ARRAY_SIZE(map); i++) {
123 if (engine->flags & BIT(map[i].engine))
124 enabled |= BIT(map[i].sched);
126 disabled |= BIT(map[i].sched);
130 i915->caps.scheduler = enabled & ~disabled;
131 if (!(i915->caps.scheduler & I915_SCHEDULER_CAP_ENABLED))
132 i915->caps.scheduler = 0;
135 const char *intel_engine_class_repr(u8 class)
137 static const char * const uabi_names[] = {
138 [RENDER_CLASS] = "rcs",
139 [COPY_ENGINE_CLASS] = "bcs",
140 [VIDEO_DECODE_CLASS] = "vcs",
141 [VIDEO_ENHANCEMENT_CLASS] = "vecs",
144 if (class >= ARRAY_SIZE(uabi_names) || !uabi_names[class])
147 return uabi_names[class];
156 static int legacy_ring_idx(const struct legacy_ring *ring)
158 static const struct {
161 [RENDER_CLASS] = { RCS0, 1 },
162 [COPY_ENGINE_CLASS] = { BCS0, 1 },
163 [VIDEO_DECODE_CLASS] = { VCS0, I915_MAX_VCS },
164 [VIDEO_ENHANCEMENT_CLASS] = { VECS0, I915_MAX_VECS },
167 if (GEM_DEBUG_WARN_ON(ring->class >= ARRAY_SIZE(map)))
168 return INVALID_ENGINE;
170 if (GEM_DEBUG_WARN_ON(ring->instance >= map[ring->class].max))
171 return INVALID_ENGINE;
173 return map[ring->class].base + ring->instance;
176 static void add_legacy_ring(struct legacy_ring *ring,
177 struct intel_engine_cs *engine)
179 if (engine->gt != ring->gt || engine->class != ring->class) {
180 ring->gt = engine->gt;
181 ring->class = engine->class;
185 engine->legacy_idx = legacy_ring_idx(ring);
186 if (engine->legacy_idx != INVALID_ENGINE)
190 void intel_engines_driver_register(struct drm_i915_private *i915)
192 struct legacy_ring ring = {};
193 u8 uabi_instances[4] = {};
194 struct list_head *it, *next;
195 struct rb_node **p, *prev;
198 sort_engines(i915, &engines);
201 p = &i915->uabi_engines.rb_node;
202 list_for_each_safe(it, next, &engines) {
203 struct intel_engine_cs *engine =
204 container_of((struct rb_node *)it, typeof(*engine),
206 char old[sizeof(engine->name)];
208 if (intel_gt_has_unrecoverable_error(engine->gt))
209 continue; /* ignore incomplete engines */
211 GEM_BUG_ON(engine->class >= ARRAY_SIZE(uabi_classes));
212 engine->uabi_class = uabi_classes[engine->class];
214 GEM_BUG_ON(engine->uabi_class >= ARRAY_SIZE(uabi_instances));
215 engine->uabi_instance = uabi_instances[engine->uabi_class]++;
217 /* Replace the internal name with the final user facing name */
218 memcpy(old, engine->name, sizeof(engine->name));
219 scnprintf(engine->name, sizeof(engine->name), "%s%u",
220 intel_engine_class_repr(engine->class),
221 engine->uabi_instance);
222 DRM_DEBUG_DRIVER("renamed %s to %s\n", old, engine->name);
224 rb_link_node(&engine->uabi_node, prev, p);
225 rb_insert_color(&engine->uabi_node, &i915->uabi_engines);
227 GEM_BUG_ON(intel_engine_lookup_user(i915,
229 engine->uabi_instance) != engine);
231 /* Fix up the mapping to match default execbuf::user_map[] */
232 add_legacy_ring(&ring, engine);
234 prev = &engine->uabi_node;
238 if (IS_ENABLED(CONFIG_DRM_I915_SELFTESTS) &&
239 IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) {
240 struct intel_engine_cs *engine;
241 unsigned int isolation;
245 for (class = 0; class < ARRAY_SIZE(uabi_instances); class++) {
246 for (inst = 0; inst < uabi_instances[class]; inst++) {
247 engine = intel_engine_lookup_user(i915,
250 pr_err("UABI engine not found for { class:%d, instance:%d }\n",
256 if (engine->uabi_class != class ||
257 engine->uabi_instance != inst) {
258 pr_err("Wrong UABI engine:%s { class:%d, instance:%d } found for { class:%d, instance:%d }\n",
261 engine->uabi_instance,
270 * Make sure that classes with multiple engine instances all
271 * share the same basic configuration.
273 isolation = intel_engines_has_context_isolation(i915);
274 for_each_uabi_engine(engine, i915) {
275 unsigned int bit = BIT(engine->uabi_class);
276 unsigned int expected = engine->default_state ? bit : 0;
278 if ((isolation & bit) != expected) {
279 pr_err("mismatching default context state for class %d on engine %s\n",
280 engine->uabi_class, engine->name);
285 if (drm_WARN(&i915->drm, errors,
286 "Invalid UABI engine mapping found"))
287 i915->uabi_engines = RB_ROOT;
290 set_scheduler_caps(i915);
293 unsigned int intel_engines_has_context_isolation(struct drm_i915_private *i915)
295 struct intel_engine_cs *engine;
299 for_each_uabi_engine(engine, i915)
300 if (engine->default_state)
301 which |= BIT(engine->uabi_class);