• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 *
intel_engine_lookup_user(struct drm_i915_private * i915,u8 class,u8 instance)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 
intel_engine_add_user(struct intel_engine_cs * engine)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 
engine_cmp(void * priv,const struct list_head * A,const struct list_head * B)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 
get_engines(struct drm_i915_private * i915)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 
sort_engines(struct drm_i915_private * i915,struct list_head * engines)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 
set_scheduler_caps(struct drm_i915_private * i915)92 static void set_scheduler_caps(struct drm_i915_private *i915)
93 {
94 	static const struct {
95 		u8 engine;
96 		u8 sched;
97 	} map[] = {
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),
102 #undef MAP
103 	};
104 	struct intel_engine_cs *engine;
105 	u32 enabled, disabled;
106 
107 	enabled = 0;
108 	disabled = 0;
109 	for_each_uabi_engine(engine, i915) { /* all engines must agree! */
110 		int i;
111 
112 		if (engine->schedule)
113 			enabled |= (I915_SCHEDULER_CAP_ENABLED |
114 				    I915_SCHEDULER_CAP_PRIORITY);
115 		else
116 			disabled |= (I915_SCHEDULER_CAP_ENABLED |
117 				     I915_SCHEDULER_CAP_PRIORITY);
118 
119 		for (i = 0; i < ARRAY_SIZE(map); i++) {
120 			if (engine->flags & BIT(map[i].engine))
121 				enabled |= BIT(map[i].sched);
122 			else
123 				disabled |= BIT(map[i].sched);
124 		}
125 	}
126 
127 	i915->caps.scheduler = enabled & ~disabled;
128 	if (!(i915->caps.scheduler & I915_SCHEDULER_CAP_ENABLED))
129 		i915->caps.scheduler = 0;
130 }
131 
intel_engine_class_repr(u8 class)132 const char *intel_engine_class_repr(u8 class)
133 {
134 	static const char * const uabi_names[] = {
135 		[RENDER_CLASS] = "rcs",
136 		[COPY_ENGINE_CLASS] = "bcs",
137 		[VIDEO_DECODE_CLASS] = "vcs",
138 		[VIDEO_ENHANCEMENT_CLASS] = "vecs",
139 	};
140 
141 	if (class >= ARRAY_SIZE(uabi_names) || !uabi_names[class])
142 		return "xxx";
143 
144 	return uabi_names[class];
145 }
146 
147 struct legacy_ring {
148 	struct intel_gt *gt;
149 	u8 class;
150 	u8 instance;
151 };
152 
legacy_ring_idx(const struct legacy_ring * ring)153 static int legacy_ring_idx(const struct legacy_ring *ring)
154 {
155 	static const struct {
156 		u8 base, max;
157 	} map[] = {
158 		[RENDER_CLASS] = { RCS0, 1 },
159 		[COPY_ENGINE_CLASS] = { BCS0, 1 },
160 		[VIDEO_DECODE_CLASS] = { VCS0, I915_MAX_VCS },
161 		[VIDEO_ENHANCEMENT_CLASS] = { VECS0, I915_MAX_VECS },
162 	};
163 
164 	if (GEM_DEBUG_WARN_ON(ring->class >= ARRAY_SIZE(map)))
165 		return INVALID_ENGINE;
166 
167 	if (GEM_DEBUG_WARN_ON(ring->instance >= map[ring->class].max))
168 		return INVALID_ENGINE;
169 
170 	return map[ring->class].base + ring->instance;
171 }
172 
add_legacy_ring(struct legacy_ring * ring,struct intel_engine_cs * engine)173 static void add_legacy_ring(struct legacy_ring *ring,
174 			    struct intel_engine_cs *engine)
175 {
176 	if (engine->gt != ring->gt || engine->class != ring->class) {
177 		ring->gt = engine->gt;
178 		ring->class = engine->class;
179 		ring->instance = 0;
180 	}
181 
182 	engine->legacy_idx = legacy_ring_idx(ring);
183 	if (engine->legacy_idx != INVALID_ENGINE)
184 		ring->instance++;
185 }
186 
intel_engines_driver_register(struct drm_i915_private * i915)187 void intel_engines_driver_register(struct drm_i915_private *i915)
188 {
189 	struct legacy_ring ring = {};
190 	u8 uabi_instances[4] = {};
191 	struct list_head *it, *next;
192 	struct rb_node **p, *prev;
193 	LIST_HEAD(engines);
194 
195 	sort_engines(i915, &engines);
196 
197 	prev = NULL;
198 	p = &i915->uabi_engines.rb_node;
199 	list_for_each_safe(it, next, &engines) {
200 		struct intel_engine_cs *engine =
201 			container_of((struct rb_node *)it, typeof(*engine),
202 				     uabi_node);
203 		char old[sizeof(engine->name)];
204 
205 		if (intel_gt_has_unrecoverable_error(engine->gt))
206 			continue; /* ignore incomplete engines */
207 
208 		GEM_BUG_ON(engine->class >= ARRAY_SIZE(uabi_classes));
209 		engine->uabi_class = uabi_classes[engine->class];
210 
211 		GEM_BUG_ON(engine->uabi_class >= ARRAY_SIZE(uabi_instances));
212 		engine->uabi_instance = uabi_instances[engine->uabi_class]++;
213 
214 		/* Replace the internal name with the final user facing name */
215 		memcpy(old, engine->name, sizeof(engine->name));
216 		scnprintf(engine->name, sizeof(engine->name), "%s%u",
217 			  intel_engine_class_repr(engine->class),
218 			  engine->uabi_instance);
219 		DRM_DEBUG_DRIVER("renamed %s to %s\n", old, engine->name);
220 
221 		rb_link_node(&engine->uabi_node, prev, p);
222 		rb_insert_color(&engine->uabi_node, &i915->uabi_engines);
223 
224 		GEM_BUG_ON(intel_engine_lookup_user(i915,
225 						    engine->uabi_class,
226 						    engine->uabi_instance) != engine);
227 
228 		/* Fix up the mapping to match default execbuf::user_map[] */
229 		add_legacy_ring(&ring, engine);
230 
231 		prev = &engine->uabi_node;
232 		p = &prev->rb_right;
233 	}
234 
235 	if (IS_ENABLED(CONFIG_DRM_I915_SELFTESTS) &&
236 	    IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) {
237 		struct intel_engine_cs *engine;
238 		unsigned int isolation;
239 		int class, inst;
240 		int errors = 0;
241 
242 		for (class = 0; class < ARRAY_SIZE(uabi_instances); class++) {
243 			for (inst = 0; inst < uabi_instances[class]; inst++) {
244 				engine = intel_engine_lookup_user(i915,
245 								  class, inst);
246 				if (!engine) {
247 					pr_err("UABI engine not found for { class:%d, instance:%d }\n",
248 					       class, inst);
249 					errors++;
250 					continue;
251 				}
252 
253 				if (engine->uabi_class != class ||
254 				    engine->uabi_instance != inst) {
255 					pr_err("Wrong UABI engine:%s { class:%d, instance:%d } found for { class:%d, instance:%d }\n",
256 					       engine->name,
257 					       engine->uabi_class,
258 					       engine->uabi_instance,
259 					       class, inst);
260 					errors++;
261 					continue;
262 				}
263 			}
264 		}
265 
266 		/*
267 		 * Make sure that classes with multiple engine instances all
268 		 * share the same basic configuration.
269 		 */
270 		isolation = intel_engines_has_context_isolation(i915);
271 		for_each_uabi_engine(engine, i915) {
272 			unsigned int bit = BIT(engine->uabi_class);
273 			unsigned int expected = engine->default_state ? bit : 0;
274 
275 			if ((isolation & bit) != expected) {
276 				pr_err("mismatching default context state for class %d on engine %s\n",
277 				       engine->uabi_class, engine->name);
278 				errors++;
279 			}
280 		}
281 
282 		if (drm_WARN(&i915->drm, errors,
283 			     "Invalid UABI engine mapping found"))
284 			i915->uabi_engines = RB_ROOT;
285 	}
286 
287 	set_scheduler_caps(i915);
288 }
289 
intel_engines_has_context_isolation(struct drm_i915_private * i915)290 unsigned int intel_engines_has_context_isolation(struct drm_i915_private *i915)
291 {
292 	struct intel_engine_cs *engine;
293 	unsigned int which;
294 
295 	which = 0;
296 	for_each_uabi_engine(engine, i915)
297 		if (engine->default_state)
298 			which |= BIT(engine->uabi_class);
299 
300 	return which;
301 }
302