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