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