xref: /openbmc/linux/drivers/gpu/drm/i915/gt/mock_engine.c (revision 817396dc9f6ab2481b94071de2e586aae876e89c)
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 #include "intel_engine_pool.h"
32 
33 #include "mock_engine.h"
34 #include "selftests/mock_request.h"
35 
36 static void mock_timeline_pin(struct intel_timeline *tl)
37 {
38 	atomic_inc(&tl->pin_count);
39 }
40 
41 static void mock_timeline_unpin(struct intel_timeline *tl)
42 {
43 	GEM_BUG_ON(!atomic_read(&tl->pin_count));
44 	atomic_dec(&tl->pin_count);
45 }
46 
47 static struct intel_ring *mock_ring(struct intel_engine_cs *engine)
48 {
49 	const unsigned long sz = PAGE_SIZE / 2;
50 	struct intel_ring *ring;
51 
52 	ring = kzalloc(sizeof(*ring) + sz, GFP_KERNEL);
53 	if (!ring)
54 		return NULL;
55 
56 	kref_init(&ring->ref);
57 	ring->size = sz;
58 	ring->effective_size = sz;
59 	ring->vaddr = (void *)(ring + 1);
60 	atomic_set(&ring->pin_count, 1);
61 
62 	intel_ring_update_space(ring);
63 
64 	return ring;
65 }
66 
67 static struct i915_request *first_request(struct mock_engine *engine)
68 {
69 	return list_first_entry_or_null(&engine->hw_queue,
70 					struct i915_request,
71 					mock.link);
72 }
73 
74 static void advance(struct i915_request *request)
75 {
76 	list_del_init(&request->mock.link);
77 	i915_request_mark_complete(request);
78 	GEM_BUG_ON(!i915_request_completed(request));
79 
80 	intel_engine_signal_breadcrumbs(request->engine);
81 }
82 
83 static void hw_delay_complete(struct timer_list *t)
84 {
85 	struct mock_engine *engine = from_timer(engine, t, hw_delay);
86 	struct i915_request *request;
87 	unsigned long flags;
88 
89 	spin_lock_irqsave(&engine->hw_lock, flags);
90 
91 	/* Timer fired, first request is complete */
92 	request = first_request(engine);
93 	if (request)
94 		advance(request);
95 
96 	/*
97 	 * Also immediately signal any subsequent 0-delay requests, but
98 	 * requeue the timer for the next delayed request.
99 	 */
100 	while ((request = first_request(engine))) {
101 		if (request->mock.delay) {
102 			mod_timer(&engine->hw_delay,
103 				  jiffies + request->mock.delay);
104 			break;
105 		}
106 
107 		advance(request);
108 	}
109 
110 	spin_unlock_irqrestore(&engine->hw_lock, flags);
111 }
112 
113 static void mock_context_unpin(struct intel_context *ce)
114 {
115 }
116 
117 static void mock_context_destroy(struct kref *ref)
118 {
119 	struct intel_context *ce = container_of(ref, typeof(*ce), ref);
120 
121 	GEM_BUG_ON(intel_context_is_pinned(ce));
122 
123 	if (test_bit(CONTEXT_ALLOC_BIT, &ce->flags)) {
124 		kfree(ce->ring);
125 		mock_timeline_unpin(ce->timeline);
126 	}
127 
128 	intel_context_fini(ce);
129 	intel_context_free(ce);
130 }
131 
132 static int mock_context_alloc(struct intel_context *ce)
133 {
134 	ce->ring = mock_ring(ce->engine);
135 	if (!ce->ring)
136 		return -ENOMEM;
137 
138 	GEM_BUG_ON(ce->timeline);
139 	ce->timeline = intel_timeline_create(ce->engine->gt, NULL);
140 	if (IS_ERR(ce->timeline)) {
141 		kfree(ce->engine);
142 		return PTR_ERR(ce->timeline);
143 	}
144 
145 	mock_timeline_pin(ce->timeline);
146 
147 	return 0;
148 }
149 
150 static int mock_context_pin(struct intel_context *ce)
151 {
152 	return intel_context_active_acquire(ce);
153 }
154 
155 static const struct intel_context_ops mock_context_ops = {
156 	.alloc = mock_context_alloc,
157 
158 	.pin = mock_context_pin,
159 	.unpin = mock_context_unpin,
160 
161 	.enter = intel_context_enter_engine,
162 	.exit = intel_context_exit_engine,
163 
164 	.destroy = mock_context_destroy,
165 };
166 
167 static int mock_request_alloc(struct i915_request *request)
168 {
169 	INIT_LIST_HEAD(&request->mock.link);
170 	request->mock.delay = 0;
171 
172 	return 0;
173 }
174 
175 static int mock_emit_flush(struct i915_request *request,
176 			   unsigned int flags)
177 {
178 	return 0;
179 }
180 
181 static u32 *mock_emit_breadcrumb(struct i915_request *request, u32 *cs)
182 {
183 	return cs;
184 }
185 
186 static void mock_submit_request(struct i915_request *request)
187 {
188 	struct mock_engine *engine =
189 		container_of(request->engine, typeof(*engine), base);
190 	unsigned long flags;
191 
192 	i915_request_submit(request);
193 
194 	spin_lock_irqsave(&engine->hw_lock, flags);
195 	list_add_tail(&request->mock.link, &engine->hw_queue);
196 	if (list_is_first(&request->mock.link, &engine->hw_queue)) {
197 		if (request->mock.delay)
198 			mod_timer(&engine->hw_delay,
199 				  jiffies + request->mock.delay);
200 		else
201 			advance(request);
202 	}
203 	spin_unlock_irqrestore(&engine->hw_lock, flags);
204 }
205 
206 static void mock_reset_prepare(struct intel_engine_cs *engine)
207 {
208 }
209 
210 static void mock_reset_rewind(struct intel_engine_cs *engine, bool stalled)
211 {
212 	GEM_BUG_ON(stalled);
213 }
214 
215 static void mock_reset_cancel(struct intel_engine_cs *engine)
216 {
217 	struct i915_request *request;
218 	unsigned long flags;
219 
220 	spin_lock_irqsave(&engine->active.lock, flags);
221 
222 	/* Mark all submitted requests as skipped. */
223 	list_for_each_entry(request, &engine->active.requests, sched.link) {
224 		if (!i915_request_signaled(request))
225 			dma_fence_set_error(&request->fence, -EIO);
226 
227 		i915_request_mark_complete(request);
228 	}
229 
230 	spin_unlock_irqrestore(&engine->active.lock, flags);
231 }
232 
233 static void mock_reset_finish(struct intel_engine_cs *engine)
234 {
235 }
236 
237 static void mock_engine_release(struct intel_engine_cs *engine)
238 {
239 	struct mock_engine *mock =
240 		container_of(engine, typeof(*mock), base);
241 
242 	GEM_BUG_ON(timer_pending(&mock->hw_delay));
243 
244 	intel_context_unpin(engine->kernel_context);
245 	intel_context_put(engine->kernel_context);
246 
247 	intel_engine_fini_retire(engine);
248 	intel_engine_fini_breadcrumbs(engine);
249 }
250 
251 struct intel_engine_cs *mock_engine(struct drm_i915_private *i915,
252 				    const char *name,
253 				    int id)
254 {
255 	struct mock_engine *engine;
256 
257 	GEM_BUG_ON(id >= I915_NUM_ENGINES);
258 	GEM_BUG_ON(!i915->gt.uncore);
259 
260 	engine = kzalloc(sizeof(*engine) + PAGE_SIZE, GFP_KERNEL);
261 	if (!engine)
262 		return NULL;
263 
264 	/* minimal engine setup for requests */
265 	engine->base.i915 = i915;
266 	engine->base.gt = &i915->gt;
267 	engine->base.uncore = i915->gt.uncore;
268 	snprintf(engine->base.name, sizeof(engine->base.name), "%s", name);
269 	engine->base.id = id;
270 	engine->base.mask = BIT(id);
271 	engine->base.legacy_idx = INVALID_ENGINE;
272 	engine->base.instance = id;
273 	engine->base.status_page.addr = (void *)(engine + 1);
274 
275 	engine->base.cops = &mock_context_ops;
276 	engine->base.request_alloc = mock_request_alloc;
277 	engine->base.emit_flush = mock_emit_flush;
278 	engine->base.emit_fini_breadcrumb = mock_emit_breadcrumb;
279 	engine->base.submit_request = mock_submit_request;
280 
281 	engine->base.reset.prepare = mock_reset_prepare;
282 	engine->base.reset.rewind = mock_reset_rewind;
283 	engine->base.reset.cancel = mock_reset_cancel;
284 	engine->base.reset.finish = mock_reset_finish;
285 
286 	engine->base.release = mock_engine_release;
287 
288 	i915->gt.engine[id] = &engine->base;
289 	i915->gt.engine_class[0][id] = &engine->base;
290 
291 	/* fake hw queue */
292 	spin_lock_init(&engine->hw_lock);
293 	timer_setup(&engine->hw_delay, hw_delay_complete, 0);
294 	INIT_LIST_HEAD(&engine->hw_queue);
295 
296 	intel_engine_add_user(&engine->base);
297 
298 	return &engine->base;
299 }
300 
301 int mock_engine_init(struct intel_engine_cs *engine)
302 {
303 	struct intel_context *ce;
304 
305 	intel_engine_init_active(engine, ENGINE_MOCK);
306 	intel_engine_init_breadcrumbs(engine);
307 	intel_engine_init_execlists(engine);
308 	intel_engine_init__pm(engine);
309 	intel_engine_init_retire(engine);
310 	intel_engine_pool_init(&engine->pool);
311 
312 	ce = create_kernel_context(engine);
313 	if (IS_ERR(ce))
314 		goto err_breadcrumbs;
315 
316 	engine->kernel_context = ce;
317 	return 0;
318 
319 err_breadcrumbs:
320 	intel_engine_fini_breadcrumbs(engine);
321 	return -ENOMEM;
322 }
323 
324 void mock_engine_flush(struct intel_engine_cs *engine)
325 {
326 	struct mock_engine *mock =
327 		container_of(engine, typeof(*mock), base);
328 	struct i915_request *request, *rn;
329 
330 	del_timer_sync(&mock->hw_delay);
331 
332 	spin_lock_irq(&mock->hw_lock);
333 	list_for_each_entry_safe(request, rn, &mock->hw_queue, mock.link)
334 		advance(request);
335 	spin_unlock_irq(&mock->hw_lock);
336 }
337 
338 void mock_engine_reset(struct intel_engine_cs *engine)
339 {
340 }
341