]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/i915/gt/mock_engine.c
086801b514416d0f175b1088ef87dde907a6708a
[linux.git] / drivers / gpu / drm / i915 / gt / mock_engine.c
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
31 #include "mock_engine.h"
32 #include "selftests/mock_request.h"
33
34 struct mock_ring {
35         struct intel_ring base;
36         struct i915_timeline timeline;
37 };
38
39 static void mock_timeline_pin(struct i915_timeline *tl)
40 {
41         tl->pin_count++;
42 }
43
44 static void mock_timeline_unpin(struct i915_timeline *tl)
45 {
46         GEM_BUG_ON(!tl->pin_count);
47         tl->pin_count--;
48 }
49
50 static struct intel_ring *mock_ring(struct intel_engine_cs *engine)
51 {
52         const unsigned long sz = PAGE_SIZE / 2;
53         struct mock_ring *ring;
54
55         ring = kzalloc(sizeof(*ring) + sz, GFP_KERNEL);
56         if (!ring)
57                 return NULL;
58
59         if (i915_timeline_init(engine->i915, &ring->timeline, NULL)) {
60                 kfree(ring);
61                 return NULL;
62         }
63
64         kref_init(&ring->base.ref);
65         ring->base.size = sz;
66         ring->base.effective_size = sz;
67         ring->base.vaddr = (void *)(ring + 1);
68         ring->base.timeline = &ring->timeline;
69
70         INIT_LIST_HEAD(&ring->base.request_list);
71         intel_ring_update_space(&ring->base);
72
73         return &ring->base;
74 }
75
76 static void mock_ring_free(struct intel_ring *base)
77 {
78         struct mock_ring *ring = container_of(base, typeof(*ring), base);
79
80         i915_timeline_fini(&ring->timeline);
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_queue_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         mock_timeline_unpin(ce->ring->timeline);
133 }
134
135 static void mock_context_destroy(struct kref *ref)
136 {
137         struct intel_context *ce = container_of(ref, typeof(*ce), ref);
138
139         GEM_BUG_ON(intel_context_is_pinned(ce));
140
141         if (ce->ring)
142                 mock_ring_free(ce->ring);
143
144         intel_context_free(ce);
145 }
146
147 static int mock_context_pin(struct intel_context *ce)
148 {
149         int ret;
150
151         if (!ce->ring) {
152                 ce->ring = mock_ring(ce->engine);
153                 if (!ce->ring)
154                         return -ENOMEM;
155         }
156
157         ret = intel_context_active_acquire(ce, PIN_HIGH);
158         if (ret)
159                 return ret;
160
161         mock_timeline_pin(ce->ring->timeline);
162         return 0;
163 }
164
165 static const struct intel_context_ops mock_context_ops = {
166         .pin = mock_context_pin,
167         .unpin = mock_context_unpin,
168
169         .enter = intel_context_enter_engine,
170         .exit = intel_context_exit_engine,
171
172         .destroy = mock_context_destroy,
173 };
174
175 static int mock_request_alloc(struct i915_request *request)
176 {
177         INIT_LIST_HEAD(&request->mock.link);
178         request->mock.delay = 0;
179
180         return 0;
181 }
182
183 static int mock_emit_flush(struct i915_request *request,
184                            unsigned int flags)
185 {
186         return 0;
187 }
188
189 static u32 *mock_emit_breadcrumb(struct i915_request *request, u32 *cs)
190 {
191         return cs;
192 }
193
194 static void mock_submit_request(struct i915_request *request)
195 {
196         struct mock_engine *engine =
197                 container_of(request->engine, typeof(*engine), base);
198         unsigned long flags;
199
200         i915_request_submit(request);
201
202         spin_lock_irqsave(&engine->hw_lock, flags);
203         list_add_tail(&request->mock.link, &engine->hw_queue);
204         if (list_is_first(&request->mock.link, &engine->hw_queue)) {
205                 if (request->mock.delay)
206                         mod_timer(&engine->hw_delay,
207                                   jiffies + request->mock.delay);
208                 else
209                         advance(request);
210         }
211         spin_unlock_irqrestore(&engine->hw_lock, flags);
212 }
213
214 static void mock_reset_prepare(struct intel_engine_cs *engine)
215 {
216 }
217
218 static void mock_reset(struct intel_engine_cs *engine, bool stalled)
219 {
220         GEM_BUG_ON(stalled);
221 }
222
223 static void mock_reset_finish(struct intel_engine_cs *engine)
224 {
225 }
226
227 static void mock_cancel_requests(struct intel_engine_cs *engine)
228 {
229         struct i915_request *request;
230         unsigned long flags;
231
232         spin_lock_irqsave(&engine->active.lock, flags);
233
234         /* Mark all submitted requests as skipped. */
235         list_for_each_entry(request, &engine->active.requests, sched.link) {
236                 if (!i915_request_signaled(request))
237                         dma_fence_set_error(&request->fence, -EIO);
238
239                 i915_request_mark_complete(request);
240         }
241
242         spin_unlock_irqrestore(&engine->active.lock, flags);
243 }
244
245 struct intel_engine_cs *mock_engine(struct drm_i915_private *i915,
246                                     const char *name,
247                                     int id)
248 {
249         struct mock_engine *engine;
250
251         GEM_BUG_ON(id >= I915_NUM_ENGINES);
252
253         engine = kzalloc(sizeof(*engine) + PAGE_SIZE, GFP_KERNEL);
254         if (!engine)
255                 return NULL;
256
257         /* minimal engine setup for requests */
258         engine->base.i915 = i915;
259         snprintf(engine->base.name, sizeof(engine->base.name), "%s", name);
260         engine->base.id = id;
261         engine->base.mask = BIT(id);
262         engine->base.status_page.addr = (void *)(engine + 1);
263
264         engine->base.cops = &mock_context_ops;
265         engine->base.request_alloc = mock_request_alloc;
266         engine->base.emit_flush = mock_emit_flush;
267         engine->base.emit_fini_breadcrumb = mock_emit_breadcrumb;
268         engine->base.submit_request = mock_submit_request;
269
270         engine->base.reset.prepare = mock_reset_prepare;
271         engine->base.reset.reset = mock_reset;
272         engine->base.reset.finish = mock_reset_finish;
273         engine->base.cancel_requests = mock_cancel_requests;
274
275         /* fake hw queue */
276         spin_lock_init(&engine->hw_lock);
277         timer_setup(&engine->hw_delay, hw_delay_complete, 0);
278         INIT_LIST_HEAD(&engine->hw_queue);
279
280         return &engine->base;
281 }
282
283 int mock_engine_init(struct intel_engine_cs *engine)
284 {
285         struct drm_i915_private *i915 = engine->i915;
286         int err;
287
288         intel_engine_init_active(engine, ENGINE_MOCK);
289         intel_engine_init_breadcrumbs(engine);
290         intel_engine_init_execlists(engine);
291         intel_engine_init__pm(engine);
292
293         engine->kernel_context =
294                 i915_gem_context_get_engine(i915->kernel_context, engine->id);
295         if (IS_ERR(engine->kernel_context))
296                 goto err_breadcrumbs;
297
298         err = intel_context_pin(engine->kernel_context);
299         intel_context_put(engine->kernel_context);
300         if (err)
301                 goto err_breadcrumbs;
302
303         return 0;
304
305 err_breadcrumbs:
306         intel_engine_fini_breadcrumbs(engine);
307         return -ENOMEM;
308 }
309
310 void mock_engine_flush(struct intel_engine_cs *engine)
311 {
312         struct mock_engine *mock =
313                 container_of(engine, typeof(*mock), base);
314         struct i915_request *request, *rn;
315
316         del_timer_sync(&mock->hw_delay);
317
318         spin_lock_irq(&mock->hw_lock);
319         list_for_each_entry_safe(request, rn, &mock->hw_queue, mock.link)
320                 advance(request);
321         spin_unlock_irq(&mock->hw_lock);
322 }
323
324 void mock_engine_reset(struct intel_engine_cs *engine)
325 {
326 }
327
328 void mock_engine_free(struct intel_engine_cs *engine)
329 {
330         struct mock_engine *mock =
331                 container_of(engine, typeof(*mock), base);
332
333         GEM_BUG_ON(timer_pending(&mock->hw_delay));
334
335         intel_context_unpin(engine->kernel_context);
336
337         intel_engine_fini_breadcrumbs(engine);
338
339         kfree(engine);
340 }