Merge tag 'drm-intel-next-2019-05-24' of git://anongit.freedesktop.org/drm/drm-intel...
[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 "i915_drv.h"
26 #include "i915_gem_context.h"
27 #include "intel_context.h"
28 #include "intel_engine_pm.h"
29
30 #include "mock_engine.h"
31 #include "selftests/mock_request.h"
32
33 struct mock_ring {
34         struct intel_ring base;
35         struct i915_timeline timeline;
36 };
37
38 static void mock_timeline_pin(struct i915_timeline *tl)
39 {
40         tl->pin_count++;
41 }
42
43 static void mock_timeline_unpin(struct i915_timeline *tl)
44 {
45         GEM_BUG_ON(!tl->pin_count);
46         tl->pin_count--;
47 }
48
49 static struct intel_ring *mock_ring(struct intel_engine_cs *engine)
50 {
51         const unsigned long sz = PAGE_SIZE / 2;
52         struct mock_ring *ring;
53
54         ring = kzalloc(sizeof(*ring) + sz, GFP_KERNEL);
55         if (!ring)
56                 return NULL;
57
58         if (i915_timeline_init(engine->i915, &ring->timeline, NULL)) {
59                 kfree(ring);
60                 return NULL;
61         }
62
63         kref_init(&ring->base.ref);
64         ring->base.size = sz;
65         ring->base.effective_size = sz;
66         ring->base.vaddr = (void *)(ring + 1);
67         ring->base.timeline = &ring->timeline;
68
69         INIT_LIST_HEAD(&ring->base.request_list);
70         intel_ring_update_space(&ring->base);
71
72         return &ring->base;
73 }
74
75 static void mock_ring_free(struct intel_ring *base)
76 {
77         struct mock_ring *ring = container_of(base, typeof(*ring), base);
78
79         i915_timeline_fini(&ring->timeline);
80         kfree(ring);
81 }
82
83 static struct i915_request *first_request(struct mock_engine *engine)
84 {
85         return list_first_entry_or_null(&engine->hw_queue,
86                                         struct i915_request,
87                                         mock.link);
88 }
89
90 static void advance(struct i915_request *request)
91 {
92         list_del_init(&request->mock.link);
93         i915_request_mark_complete(request);
94         GEM_BUG_ON(!i915_request_completed(request));
95
96         intel_engine_queue_breadcrumbs(request->engine);
97 }
98
99 static void hw_delay_complete(struct timer_list *t)
100 {
101         struct mock_engine *engine = from_timer(engine, t, hw_delay);
102         struct i915_request *request;
103         unsigned long flags;
104
105         spin_lock_irqsave(&engine->hw_lock, flags);
106
107         /* Timer fired, first request is complete */
108         request = first_request(engine);
109         if (request)
110                 advance(request);
111
112         /*
113          * Also immediately signal any subsequent 0-delay requests, but
114          * requeue the timer for the next delayed request.
115          */
116         while ((request = first_request(engine))) {
117                 if (request->mock.delay) {
118                         mod_timer(&engine->hw_delay,
119                                   jiffies + request->mock.delay);
120                         break;
121                 }
122
123                 advance(request);
124         }
125
126         spin_unlock_irqrestore(&engine->hw_lock, flags);
127 }
128
129 static void mock_context_unpin(struct intel_context *ce)
130 {
131         mock_timeline_unpin(ce->ring->timeline);
132 }
133
134 static void mock_context_destroy(struct kref *ref)
135 {
136         struct intel_context *ce = container_of(ref, typeof(*ce), ref);
137
138         GEM_BUG_ON(intel_context_is_pinned(ce));
139
140         if (ce->ring)
141                 mock_ring_free(ce->ring);
142
143         intel_context_free(ce);
144 }
145
146 static int mock_context_pin(struct intel_context *ce)
147 {
148         if (!ce->ring) {
149                 ce->ring = mock_ring(ce->engine);
150                 if (!ce->ring)
151                         return -ENOMEM;
152         }
153
154         mock_timeline_pin(ce->ring->timeline);
155         return 0;
156 }
157
158 static const struct intel_context_ops mock_context_ops = {
159         .pin = mock_context_pin,
160         .unpin = mock_context_unpin,
161
162         .enter = intel_context_enter_engine,
163         .exit = intel_context_exit_engine,
164
165         .destroy = mock_context_destroy,
166 };
167
168 static int mock_request_alloc(struct i915_request *request)
169 {
170         INIT_LIST_HEAD(&request->mock.link);
171         request->mock.delay = 0;
172
173         return 0;
174 }
175
176 static int mock_emit_flush(struct i915_request *request,
177                            unsigned int flags)
178 {
179         return 0;
180 }
181
182 static u32 *mock_emit_breadcrumb(struct i915_request *request, u32 *cs)
183 {
184         return cs;
185 }
186
187 static void mock_submit_request(struct i915_request *request)
188 {
189         struct mock_engine *engine =
190                 container_of(request->engine, typeof(*engine), base);
191         unsigned long flags;
192
193         i915_request_submit(request);
194
195         spin_lock_irqsave(&engine->hw_lock, flags);
196         list_add_tail(&request->mock.link, &engine->hw_queue);
197         if (list_is_first(&request->mock.link, &engine->hw_queue)) {
198                 if (request->mock.delay)
199                         mod_timer(&engine->hw_delay,
200                                   jiffies + request->mock.delay);
201                 else
202                         advance(request);
203         }
204         spin_unlock_irqrestore(&engine->hw_lock, flags);
205 }
206
207 static void mock_reset_prepare(struct intel_engine_cs *engine)
208 {
209 }
210
211 static void mock_reset(struct intel_engine_cs *engine, bool stalled)
212 {
213         GEM_BUG_ON(stalled);
214 }
215
216 static void mock_reset_finish(struct intel_engine_cs *engine)
217 {
218 }
219
220 static void mock_cancel_requests(struct intel_engine_cs *engine)
221 {
222         struct i915_request *request;
223         unsigned long flags;
224
225         spin_lock_irqsave(&engine->timeline.lock, flags);
226
227         /* Mark all submitted requests as skipped. */
228         list_for_each_entry(request, &engine->timeline.requests, sched.link) {
229                 if (!i915_request_signaled(request))
230                         dma_fence_set_error(&request->fence, -EIO);
231
232                 i915_request_mark_complete(request);
233         }
234
235         spin_unlock_irqrestore(&engine->timeline.lock, flags);
236 }
237
238 struct intel_engine_cs *mock_engine(struct drm_i915_private *i915,
239                                     const char *name,
240                                     int id)
241 {
242         struct mock_engine *engine;
243
244         GEM_BUG_ON(id >= I915_NUM_ENGINES);
245
246         engine = kzalloc(sizeof(*engine) + PAGE_SIZE, GFP_KERNEL);
247         if (!engine)
248                 return NULL;
249
250         /* minimal engine setup for requests */
251         engine->base.i915 = i915;
252         snprintf(engine->base.name, sizeof(engine->base.name), "%s", name);
253         engine->base.id = id;
254         engine->base.mask = BIT(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         return &engine->base;
274 }
275
276 int mock_engine_init(struct intel_engine_cs *engine)
277 {
278         struct drm_i915_private *i915 = engine->i915;
279         int err;
280
281         intel_engine_init_breadcrumbs(engine);
282         intel_engine_init_execlists(engine);
283         intel_engine_init__pm(engine);
284
285         if (i915_timeline_init(i915, &engine->timeline, NULL))
286                 goto err_breadcrumbs;
287         i915_timeline_set_subclass(&engine->timeline, TIMELINE_ENGINE);
288
289         engine->kernel_context =
290                 i915_gem_context_get_engine(i915->kernel_context, engine->id);
291         if (IS_ERR(engine->kernel_context))
292                 goto err_timeline;
293
294         err = intel_context_pin(engine->kernel_context);
295         intel_context_put(engine->kernel_context);
296         if (err)
297                 goto err_timeline;
298
299         return 0;
300
301 err_timeline:
302         i915_timeline_fini(&engine->timeline);
303 err_breadcrumbs:
304         intel_engine_fini_breadcrumbs(engine);
305         return -ENOMEM;
306 }
307
308 void mock_engine_flush(struct intel_engine_cs *engine)
309 {
310         struct mock_engine *mock =
311                 container_of(engine, typeof(*mock), base);
312         struct i915_request *request, *rn;
313
314         del_timer_sync(&mock->hw_delay);
315
316         spin_lock_irq(&mock->hw_lock);
317         list_for_each_entry_safe(request, rn, &mock->hw_queue, mock.link)
318                 advance(request);
319         spin_unlock_irq(&mock->hw_lock);
320 }
321
322 void mock_engine_reset(struct intel_engine_cs *engine)
323 {
324 }
325
326 void mock_engine_free(struct intel_engine_cs *engine)
327 {
328         struct mock_engine *mock =
329                 container_of(engine, typeof(*mock), base);
330         struct intel_context *ce;
331
332         GEM_BUG_ON(timer_pending(&mock->hw_delay));
333
334         ce = fetch_and_zero(&engine->last_retired_context);
335         if (ce)
336                 intel_context_unpin(ce);
337
338         intel_context_unpin(engine->kernel_context);
339
340         intel_engine_fini_breadcrumbs(engine);
341         i915_timeline_fini(&engine->timeline);
342
343         kfree(engine);
344 }