xref: /linux/drivers/gpu/drm/i915/gt/intel_engine_user.c (revision c5dbf04160005e07e8ca7232a7faa77ab1547ae0)
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(&engine->uabi_llist, &engine->i915->uabi_engines_llist);
42 }
43 
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,
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(A, typeof(*a), uabi_list);
57 	const struct intel_engine_cs *b =
58 		container_of(B, typeof(*b), uabi_list);
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(&i915->uabi_engines_llist);
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(pos, typeof(*engine), uabi_llist);
86 		list_add(&engine->uabi_list, engines);
87 	}
88 	list_sort(NULL, engines, engine_cmp);
89 }
90 
91 static void set_scheduler_caps(struct drm_i915_private *i915)
92 {
93 	static const struct {
94 		u8 engine;
95 		u8 sched;
96 	} map[] = {
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),
101 #undef MAP
102 	};
103 	struct intel_engine_cs *engine;
104 	u32 enabled, disabled;
105 
106 	enabled = 0;
107 	disabled = 0;
108 	for_each_uabi_engine(engine, i915) { /* all engines must agree! */
109 		int i;
110 
111 		if (engine->sched_engine->schedule)
112 			enabled |= (I915_SCHEDULER_CAP_ENABLED |
113 				    I915_SCHEDULER_CAP_PRIORITY);
114 		else
115 			disabled |= (I915_SCHEDULER_CAP_ENABLED |
116 				     I915_SCHEDULER_CAP_PRIORITY);
117 
118 		if (intel_uc_uses_guc_submission(&engine->gt->uc))
119 			enabled |= I915_SCHEDULER_CAP_STATIC_PRIORITY_MAP;
120 
121 		for (i = 0; i < ARRAY_SIZE(map); i++) {
122 			if (engine->flags & BIT(map[i].engine))
123 				enabled |= BIT(map[i].sched);
124 			else
125 				disabled |= BIT(map[i].sched);
126 		}
127 	}
128 
129 	i915->caps.scheduler = enabled & ~disabled;
130 	if (!(i915->caps.scheduler & I915_SCHEDULER_CAP_ENABLED))
131 		i915->caps.scheduler = 0;
132 }
133 
134 const char *intel_engine_class_repr(u8 class)
135 {
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",
143 	};
144 
145 	if (class >= ARRAY_SIZE(uabi_names) || !uabi_names[class])
146 		return "xxx";
147 
148 	return uabi_names[class];
149 }
150 
151 struct legacy_ring {
152 	struct intel_gt *gt;
153 	u8 class;
154 	u8 instance;
155 };
156 
157 static int legacy_ring_idx(const struct legacy_ring *ring)
158 {
159 	static const struct {
160 		u8 base, max;
161 	} map[] = {
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 },
167 	};
168 
169 	if (GEM_DEBUG_WARN_ON(ring->class >= ARRAY_SIZE(map)))
170 		return INVALID_ENGINE;
171 
172 	if (GEM_DEBUG_WARN_ON(ring->instance >= map[ring->class].max))
173 		return INVALID_ENGINE;
174 
175 	return map[ring->class].base + ring->instance;
176 }
177 
178 static void add_legacy_ring(struct legacy_ring *ring,
179 			    struct intel_engine_cs *engine)
180 {
181 	if (engine->gt != ring->gt || engine->class != ring->class) {
182 		ring->gt = engine->gt;
183 		ring->class = engine->class;
184 		ring->instance = 0;
185 	}
186 
187 	engine->legacy_idx = legacy_ring_idx(ring);
188 	if (engine->legacy_idx != INVALID_ENGINE)
189 		ring->instance++;
190 }
191 
192 static void engine_rename(struct intel_engine_cs *engine, const char *name, u16 instance)
193 {
194 	char old[sizeof(engine->name)];
195 
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);
199 }
200 
201 void intel_engines_driver_register(struct drm_i915_private *i915)
202 {
203 	struct legacy_ring ring = {};
204 	struct list_head *it, *next;
205 	struct rb_node **p, *prev;
206 	LIST_HEAD(engines);
207 
208 	sort_engines(i915, &engines);
209 
210 	prev = NULL;
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);
215 
216 		if (intel_gt_has_unrecoverable_error(engine->gt))
217 			continue; /* ignore incomplete engines */
218 
219 		/*
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
222 		 */
223 		if (engine->id == GSC0) {
224 			engine_rename(engine, "gsc", 0);
225 			continue;
226 		}
227 
228 		GEM_BUG_ON(engine->class >= ARRAY_SIZE(uabi_classes));
229 		engine->uabi_class = uabi_classes[engine->class];
230 
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]++;
235 
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);
240 
241 		rb_link_node(&engine->uabi_node, prev, p);
242 		rb_insert_color(&engine->uabi_node, &i915->uabi_engines);
243 
244 		GEM_BUG_ON(intel_engine_lookup_user(i915,
245 						    engine->uabi_class,
246 						    engine->uabi_instance) != engine);
247 
248 		/* Fix up the mapping to match default execbuf::user_map[] */
249 		add_legacy_ring(&ring, engine);
250 
251 		prev = &engine->uabi_node;
252 		p = &prev->rb_right;
253 	}
254 
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;
259 		int class, inst;
260 		int errors = 0;
261 
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,
265 								  class, inst);
266 				if (!engine) {
267 					pr_err("UABI engine not found for { class:%d, instance:%d }\n",
268 					       class, inst);
269 					errors++;
270 					continue;
271 				}
272 
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",
276 					       engine->name,
277 					       engine->uabi_class,
278 					       engine->uabi_instance,
279 					       class, inst);
280 					errors++;
281 					continue;
282 				}
283 			}
284 		}
285 
286 		/*
287 		 * Make sure that classes with multiple engine instances all
288 		 * share the same basic configuration.
289 		 */
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;
294 
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);
298 				errors++;
299 			}
300 		}
301 
302 		if (drm_WARN(&i915->drm, errors,
303 			     "Invalid UABI engine mapping found"))
304 			i915->uabi_engines = RB_ROOT;
305 	}
306 
307 	set_scheduler_caps(i915);
308 }
309 
310 unsigned int intel_engines_has_context_isolation(struct drm_i915_private *i915)
311 {
312 	struct intel_engine_cs *engine;
313 	unsigned int which;
314 
315 	which = 0;
316 	for_each_uabi_engine(engine, i915)
317 		if (engine->default_state)
318 			which |= BIT(engine->uabi_class);
319 
320 	return which;
321 }
322