xref: /openbmc/linux/drivers/gpu/drm/i915/gt/mock_engine.c (revision 27ab1c1c)
1 /*
2  * Copyright © 2016 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  */
24 
25 #include "gem/i915_gem_context.h"
26 #include "gt/intel_ring.h"
27 
28 #include "i915_drv.h"
29 #include "intel_context.h"
30 #include "intel_engine_pm.h"
31 
32 #include "mock_engine.h"
33 #include "selftests/mock_request.h"
34 
35 static void mock_timeline_pin(struct intel_timeline *tl)
36 {
37 	atomic_inc(&tl->pin_count);
38 }
39 
40 static void mock_timeline_unpin(struct intel_timeline *tl)
41 {
42 	GEM_BUG_ON(!atomic_read(&tl->pin_count));
43 	atomic_dec(&tl->pin_count);
44 }
45 
46 static struct intel_ring *mock_ring(struct intel_engine_cs *engine)
47 {
48 	const unsigned long sz = PAGE_SIZE / 2;
49 	struct intel_ring *ring;
50 
51 	ring = kzalloc(sizeof(*ring) + sz, GFP_KERNEL);
52 	if (!ring)
53 		return NULL;
54 
55 	kref_init(&ring->ref);
56 	ring->size = sz;
57 	ring->effective_size = sz;
58 	ring->vaddr = (void *)(ring + 1);
59 	atomic_set(&ring->pin_count, 1);
60 
61 	ring->vma = i915_vma_alloc();
62 	if (!ring->vma) {
63 		kfree(ring);
64 		return NULL;
65 	}
66 	i915_active_init(&ring->vma->active, NULL, NULL);
67 	__set_bit(I915_VMA_GGTT_BIT, __i915_vma_flags(ring->vma));
68 	__set_bit(DRM_MM_NODE_ALLOCATED_BIT, &ring->vma->node.flags);
69 	ring->vma->node.size = sz;
70 
71 	intel_ring_update_space(ring);
72 
73 	return ring;
74 }
75 
76 static void mock_ring_free(struct intel_ring *ring)
77 {
78 	i915_active_fini(&ring->vma->active);
79 	i915_vma_free(ring->vma);
80 
81 	kfree(ring);
82 }
83 
84 static struct i915_request *first_request(struct mock_engine *engine)
85 {
86 	return list_first_entry_or_null(&engine->hw_queue,
87 					struct i915_request,
88 					mock.link);
89 }
90 
91 static void advance(struct i915_request *request)
92 {
93 	list_del_init(&request->mock.link);
94 	i915_request_mark_complete(request);
95 	GEM_BUG_ON(!i915_request_completed(request));
96 
97 	intel_engine_signal_breadcrumbs(request->engine);
98 }
99 
100 static void hw_delay_complete(struct timer_list *t)
101 {
102 	struct mock_engine *engine = from_timer(engine, t, hw_delay);
103 	struct i915_request *request;
104 	unsigned long flags;
105 
106 	spin_lock_irqsave(&engine->hw_lock, flags);
107 
108 	/* Timer fired, first request is complete */
109 	request = first_request(engine);
110 	if (request)
111 		advance(request);
112 
113 	/*
114 	 * Also immediately signal any subsequent 0-delay requests, but
115 	 * requeue the timer for the next delayed request.
116 	 */
117 	while ((request = first_request(engine))) {
118 		if (request->mock.delay) {
119 			mod_timer(&engine->hw_delay,
120 				  jiffies + request->mock.delay);
121 			break;
122 		}
123 
124 		advance(request);
125 	}
126 
127 	spin_unlock_irqrestore(&engine->hw_lock, flags);
128 }
129 
130 static void mock_context_unpin(struct intel_context *ce)
131 {
132 }
133 
134 static void mock_context_post_unpin(struct intel_context *ce)
135 {
136 }
137 
138 static void mock_context_destroy(struct kref *ref)
139 {
140 	struct intel_context *ce = container_of(ref, typeof(*ce), ref);
141 
142 	GEM_BUG_ON(intel_context_is_pinned(ce));
143 
144 	if (test_bit(CONTEXT_ALLOC_BIT, &ce->flags)) {
145 		mock_ring_free(ce->ring);
146 		mock_timeline_unpin(ce->timeline);
147 	}
148 
149 	intel_context_fini(ce);
150 	intel_context_free(ce);
151 }
152 
153 static int mock_context_alloc(struct intel_context *ce)
154 {
155 	ce->ring = mock_ring(ce->engine);
156 	if (!ce->ring)
157 		return -ENOMEM;
158 
159 	ce->timeline = intel_timeline_create(ce->engine->gt);
160 	if (IS_ERR(ce->timeline)) {
161 		kfree(ce->engine);
162 		return PTR_ERR(ce->timeline);
163 	}
164 
165 	mock_timeline_pin(ce->timeline);
166 
167 	return 0;
168 }
169 
170 static int mock_context_pre_pin(struct intel_context *ce,
171 				struct i915_gem_ww_ctx *ww, void **unused)
172 {
173 	return 0;
174 }
175 
176 static int mock_context_pin(struct intel_context *ce, void *unused)
177 {
178 	return 0;
179 }
180 
181 static void mock_context_reset(struct intel_context *ce)
182 {
183 }
184 
185 static const struct intel_context_ops mock_context_ops = {
186 	.alloc = mock_context_alloc,
187 
188 	.pre_pin = mock_context_pre_pin,
189 	.pin = mock_context_pin,
190 	.unpin = mock_context_unpin,
191 	.post_unpin = mock_context_post_unpin,
192 
193 	.enter = intel_context_enter_engine,
194 	.exit = intel_context_exit_engine,
195 
196 	.reset = mock_context_reset,
197 	.destroy = mock_context_destroy,
198 };
199 
200 static int mock_request_alloc(struct i915_request *request)
201 {
202 	INIT_LIST_HEAD(&request->mock.link);
203 	request->mock.delay = 0;
204 
205 	return 0;
206 }
207 
208 static int mock_emit_flush(struct i915_request *request,
209 			   unsigned int flags)
210 {
211 	return 0;
212 }
213 
214 static u32 *mock_emit_breadcrumb(struct i915_request *request, u32 *cs)
215 {
216 	return cs;
217 }
218 
219 static void mock_submit_request(struct i915_request *request)
220 {
221 	struct mock_engine *engine =
222 		container_of(request->engine, typeof(*engine), base);
223 	unsigned long flags;
224 
225 	i915_request_submit(request);
226 
227 	spin_lock_irqsave(&engine->hw_lock, flags);
228 	list_add_tail(&request->mock.link, &engine->hw_queue);
229 	if (list_is_first(&request->mock.link, &engine->hw_queue)) {
230 		if (request->mock.delay)
231 			mod_timer(&engine->hw_delay,
232 				  jiffies + request->mock.delay);
233 		else
234 			advance(request);
235 	}
236 	spin_unlock_irqrestore(&engine->hw_lock, flags);
237 }
238 
239 static void mock_reset_prepare(struct intel_engine_cs *engine)
240 {
241 }
242 
243 static void mock_reset_rewind(struct intel_engine_cs *engine, bool stalled)
244 {
245 	GEM_BUG_ON(stalled);
246 }
247 
248 static void mark_eio(struct i915_request *rq)
249 {
250 	if (i915_request_completed(rq))
251 		return;
252 
253 	GEM_BUG_ON(i915_request_signaled(rq));
254 
255 	i915_request_set_error_once(rq, -EIO);
256 	i915_request_mark_complete(rq);
257 }
258 
259 static void mock_reset_cancel(struct intel_engine_cs *engine)
260 {
261 	struct mock_engine *mock =
262 		container_of(engine, typeof(*mock), base);
263 	struct i915_request *rq;
264 	unsigned long flags;
265 
266 	del_timer_sync(&mock->hw_delay);
267 
268 	spin_lock_irqsave(&engine->active.lock, flags);
269 
270 	/* Mark all submitted requests as skipped. */
271 	list_for_each_entry(rq, &engine->active.requests, sched.link)
272 		mark_eio(rq);
273 	intel_engine_signal_breadcrumbs(engine);
274 
275 	/* Cancel and submit all pending requests. */
276 	list_for_each_entry(rq, &mock->hw_queue, mock.link) {
277 		mark_eio(rq);
278 		__i915_request_submit(rq);
279 	}
280 	INIT_LIST_HEAD(&mock->hw_queue);
281 
282 	spin_unlock_irqrestore(&engine->active.lock, flags);
283 }
284 
285 static void mock_reset_finish(struct intel_engine_cs *engine)
286 {
287 }
288 
289 static void mock_engine_release(struct intel_engine_cs *engine)
290 {
291 	struct mock_engine *mock =
292 		container_of(engine, typeof(*mock), base);
293 
294 	GEM_BUG_ON(timer_pending(&mock->hw_delay));
295 
296 	intel_breadcrumbs_free(engine->breadcrumbs);
297 
298 	intel_context_unpin(engine->kernel_context);
299 	intel_context_put(engine->kernel_context);
300 
301 	intel_engine_fini_retire(engine);
302 }
303 
304 struct intel_engine_cs *mock_engine(struct drm_i915_private *i915,
305 				    const char *name,
306 				    int id)
307 {
308 	struct mock_engine *engine;
309 
310 	GEM_BUG_ON(id >= I915_NUM_ENGINES);
311 	GEM_BUG_ON(!i915->gt.uncore);
312 
313 	engine = kzalloc(sizeof(*engine) + PAGE_SIZE, GFP_KERNEL);
314 	if (!engine)
315 		return NULL;
316 
317 	/* minimal engine setup for requests */
318 	engine->base.i915 = i915;
319 	engine->base.gt = &i915->gt;
320 	engine->base.uncore = i915->gt.uncore;
321 	snprintf(engine->base.name, sizeof(engine->base.name), "%s", name);
322 	engine->base.id = id;
323 	engine->base.mask = BIT(id);
324 	engine->base.legacy_idx = INVALID_ENGINE;
325 	engine->base.instance = id;
326 	engine->base.status_page.addr = (void *)(engine + 1);
327 
328 	engine->base.cops = &mock_context_ops;
329 	engine->base.request_alloc = mock_request_alloc;
330 	engine->base.emit_flush = mock_emit_flush;
331 	engine->base.emit_fini_breadcrumb = mock_emit_breadcrumb;
332 	engine->base.submit_request = mock_submit_request;
333 
334 	engine->base.reset.prepare = mock_reset_prepare;
335 	engine->base.reset.rewind = mock_reset_rewind;
336 	engine->base.reset.cancel = mock_reset_cancel;
337 	engine->base.reset.finish = mock_reset_finish;
338 
339 	engine->base.release = mock_engine_release;
340 
341 	i915->gt.engine[id] = &engine->base;
342 	i915->gt.engine_class[0][id] = &engine->base;
343 
344 	/* fake hw queue */
345 	spin_lock_init(&engine->hw_lock);
346 	timer_setup(&engine->hw_delay, hw_delay_complete, 0);
347 	INIT_LIST_HEAD(&engine->hw_queue);
348 
349 	intel_engine_add_user(&engine->base);
350 
351 	return &engine->base;
352 }
353 
354 int mock_engine_init(struct intel_engine_cs *engine)
355 {
356 	struct intel_context *ce;
357 
358 	intel_engine_init_active(engine, ENGINE_MOCK);
359 	intel_engine_init_execlists(engine);
360 	intel_engine_init__pm(engine);
361 	intel_engine_init_retire(engine);
362 
363 	engine->breadcrumbs = intel_breadcrumbs_create(NULL);
364 	if (!engine->breadcrumbs)
365 		return -ENOMEM;
366 
367 	ce = create_kernel_context(engine);
368 	if (IS_ERR(ce))
369 		goto err_breadcrumbs;
370 
371 	/* We insist the kernel context is using the status_page */
372 	engine->status_page.vma = ce->timeline->hwsp_ggtt;
373 
374 	engine->kernel_context = ce;
375 	return 0;
376 
377 err_breadcrumbs:
378 	intel_breadcrumbs_free(engine->breadcrumbs);
379 	return -ENOMEM;
380 }
381 
382 void mock_engine_flush(struct intel_engine_cs *engine)
383 {
384 	struct mock_engine *mock =
385 		container_of(engine, typeof(*mock), base);
386 	struct i915_request *request, *rn;
387 
388 	del_timer_sync(&mock->hw_delay);
389 
390 	spin_lock_irq(&mock->hw_lock);
391 	list_for_each_entry_safe(request, rn, &mock->hw_queue, mock.link)
392 		advance(request);
393 	spin_unlock_irq(&mock->hw_lock);
394 }
395 
396 void mock_engine_reset(struct intel_engine_cs *engine)
397 {
398 }
399