2 * Copyright © 2016 Intel Corporation
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:
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
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
25 #include "gem/i915_gem_context.h"
28 #include "intel_context.h"
29 #include "intel_engine_pm.h"
31 #include "mock_engine.h"
32 #include "selftests/mock_request.h"
35 struct intel_ring base;
36 struct i915_timeline timeline;
39 static void mock_timeline_pin(struct i915_timeline *tl)
44 static void mock_timeline_unpin(struct i915_timeline *tl)
46 GEM_BUG_ON(!tl->pin_count);
50 static struct intel_ring *mock_ring(struct intel_engine_cs *engine)
52 const unsigned long sz = PAGE_SIZE / 2;
53 struct mock_ring *ring;
55 ring = kzalloc(sizeof(*ring) + sz, GFP_KERNEL);
59 if (i915_timeline_init(engine->i915, &ring->timeline, NULL)) {
64 kref_init(&ring->base.ref);
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);
71 INIT_LIST_HEAD(&ring->base.request_list);
72 intel_ring_update_space(&ring->base);
77 static void mock_ring_free(struct intel_ring *base)
79 struct mock_ring *ring = container_of(base, typeof(*ring), base);
81 i915_timeline_fini(&ring->timeline);
85 static struct i915_request *first_request(struct mock_engine *engine)
87 return list_first_entry_or_null(&engine->hw_queue,
92 static void advance(struct i915_request *request)
94 list_del_init(&request->mock.link);
95 i915_request_mark_complete(request);
96 GEM_BUG_ON(!i915_request_completed(request));
98 intel_engine_queue_breadcrumbs(request->engine);
101 static void hw_delay_complete(struct timer_list *t)
103 struct mock_engine *engine = from_timer(engine, t, hw_delay);
104 struct i915_request *request;
107 spin_lock_irqsave(&engine->hw_lock, flags);
109 /* Timer fired, first request is complete */
110 request = first_request(engine);
115 * Also immediately signal any subsequent 0-delay requests, but
116 * requeue the timer for the next delayed request.
118 while ((request = first_request(engine))) {
119 if (request->mock.delay) {
120 mod_timer(&engine->hw_delay,
121 jiffies + request->mock.delay);
128 spin_unlock_irqrestore(&engine->hw_lock, flags);
131 static void mock_context_unpin(struct intel_context *ce)
133 mock_timeline_unpin(ce->ring->timeline);
136 static void mock_context_destroy(struct kref *ref)
138 struct intel_context *ce = container_of(ref, typeof(*ce), ref);
140 GEM_BUG_ON(intel_context_is_pinned(ce));
143 mock_ring_free(ce->ring);
145 intel_context_free(ce);
148 static int mock_context_pin(struct intel_context *ce)
153 ce->ring = mock_ring(ce->engine);
158 ret = intel_context_active_acquire(ce, PIN_HIGH);
162 mock_timeline_pin(ce->ring->timeline);
166 static const struct intel_context_ops mock_context_ops = {
167 .pin = mock_context_pin,
168 .unpin = mock_context_unpin,
170 .enter = intel_context_enter_engine,
171 .exit = intel_context_exit_engine,
173 .destroy = mock_context_destroy,
176 static int mock_request_alloc(struct i915_request *request)
178 INIT_LIST_HEAD(&request->mock.link);
179 request->mock.delay = 0;
184 static int mock_emit_flush(struct i915_request *request,
190 static u32 *mock_emit_breadcrumb(struct i915_request *request, u32 *cs)
195 static void mock_submit_request(struct i915_request *request)
197 struct mock_engine *engine =
198 container_of(request->engine, typeof(*engine), base);
201 i915_request_submit(request);
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);
212 spin_unlock_irqrestore(&engine->hw_lock, flags);
215 static void mock_reset_prepare(struct intel_engine_cs *engine)
219 static void mock_reset(struct intel_engine_cs *engine, bool stalled)
224 static void mock_reset_finish(struct intel_engine_cs *engine)
228 static void mock_cancel_requests(struct intel_engine_cs *engine)
230 struct i915_request *request;
233 spin_lock_irqsave(&engine->active.lock, flags);
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);
240 i915_request_mark_complete(request);
243 spin_unlock_irqrestore(&engine->active.lock, flags);
246 struct intel_engine_cs *mock_engine(struct drm_i915_private *i915,
250 struct mock_engine *engine;
252 GEM_BUG_ON(id >= I915_NUM_ENGINES);
254 engine = kzalloc(sizeof(*engine) + PAGE_SIZE, GFP_KERNEL);
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);
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;
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;
277 spin_lock_init(&engine->hw_lock);
278 timer_setup(&engine->hw_delay, hw_delay_complete, 0);
279 INIT_LIST_HEAD(&engine->hw_queue);
281 return &engine->base;
284 int mock_engine_init(struct intel_engine_cs *engine)
286 struct drm_i915_private *i915 = engine->i915;
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);
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;
299 err = intel_context_pin(engine->kernel_context);
300 intel_context_put(engine->kernel_context);
302 goto err_breadcrumbs;
307 intel_engine_fini_breadcrumbs(engine);
311 void mock_engine_flush(struct intel_engine_cs *engine)
313 struct mock_engine *mock =
314 container_of(engine, typeof(*mock), base);
315 struct i915_request *request, *rn;
317 del_timer_sync(&mock->hw_delay);
319 spin_lock_irq(&mock->hw_lock);
320 list_for_each_entry_safe(request, rn, &mock->hw_queue, mock.link)
322 spin_unlock_irq(&mock->hw_lock);
325 void mock_engine_reset(struct intel_engine_cs *engine)
329 void mock_engine_free(struct intel_engine_cs *engine)
331 struct mock_engine *mock =
332 container_of(engine, typeof(*mock), base);
334 GEM_BUG_ON(timer_pending(&mock->hw_delay));
336 intel_context_unpin(engine->kernel_context);
338 intel_engine_fini_breadcrumbs(engine);