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;
70 INIT_LIST_HEAD(&ring->base.request_list);
71 intel_ring_update_space(&ring->base);
76 static void mock_ring_free(struct intel_ring *base)
78 struct mock_ring *ring = container_of(base, typeof(*ring), base);
80 i915_timeline_fini(&ring->timeline);
84 static struct i915_request *first_request(struct mock_engine *engine)
86 return list_first_entry_or_null(&engine->hw_queue,
91 static void advance(struct i915_request *request)
93 list_del_init(&request->mock.link);
94 i915_request_mark_complete(request);
95 GEM_BUG_ON(!i915_request_completed(request));
97 intel_engine_queue_breadcrumbs(request->engine);
100 static void hw_delay_complete(struct timer_list *t)
102 struct mock_engine *engine = from_timer(engine, t, hw_delay);
103 struct i915_request *request;
106 spin_lock_irqsave(&engine->hw_lock, flags);
108 /* Timer fired, first request is complete */
109 request = first_request(engine);
114 * Also immediately signal any subsequent 0-delay requests, but
115 * requeue the timer for the next delayed request.
117 while ((request = first_request(engine))) {
118 if (request->mock.delay) {
119 mod_timer(&engine->hw_delay,
120 jiffies + request->mock.delay);
127 spin_unlock_irqrestore(&engine->hw_lock, flags);
130 static void mock_context_unpin(struct intel_context *ce)
132 mock_timeline_unpin(ce->ring->timeline);
135 static void mock_context_destroy(struct kref *ref)
137 struct intel_context *ce = container_of(ref, typeof(*ce), ref);
139 GEM_BUG_ON(intel_context_is_pinned(ce));
142 mock_ring_free(ce->ring);
144 intel_context_free(ce);
147 static int mock_context_pin(struct intel_context *ce)
152 ce->ring = mock_ring(ce->engine);
157 ret = intel_context_active_acquire(ce, PIN_HIGH);
161 mock_timeline_pin(ce->ring->timeline);
165 static const struct intel_context_ops mock_context_ops = {
166 .pin = mock_context_pin,
167 .unpin = mock_context_unpin,
169 .enter = intel_context_enter_engine,
170 .exit = intel_context_exit_engine,
172 .destroy = mock_context_destroy,
175 static int mock_request_alloc(struct i915_request *request)
177 INIT_LIST_HEAD(&request->mock.link);
178 request->mock.delay = 0;
183 static int mock_emit_flush(struct i915_request *request,
189 static u32 *mock_emit_breadcrumb(struct i915_request *request, u32 *cs)
194 static void mock_submit_request(struct i915_request *request)
196 struct mock_engine *engine =
197 container_of(request->engine, typeof(*engine), base);
200 i915_request_submit(request);
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);
211 spin_unlock_irqrestore(&engine->hw_lock, flags);
214 static void mock_reset_prepare(struct intel_engine_cs *engine)
218 static void mock_reset(struct intel_engine_cs *engine, bool stalled)
223 static void mock_reset_finish(struct intel_engine_cs *engine)
227 static void mock_cancel_requests(struct intel_engine_cs *engine)
229 struct i915_request *request;
232 spin_lock_irqsave(&engine->active.lock, flags);
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);
239 i915_request_mark_complete(request);
242 spin_unlock_irqrestore(&engine->active.lock, flags);
245 struct intel_engine_cs *mock_engine(struct drm_i915_private *i915,
249 struct mock_engine *engine;
251 GEM_BUG_ON(id >= I915_NUM_ENGINES);
253 engine = kzalloc(sizeof(*engine) + PAGE_SIZE, GFP_KERNEL);
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);
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;
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;
276 spin_lock_init(&engine->hw_lock);
277 timer_setup(&engine->hw_delay, hw_delay_complete, 0);
278 INIT_LIST_HEAD(&engine->hw_queue);
280 return &engine->base;
283 int mock_engine_init(struct intel_engine_cs *engine)
285 struct drm_i915_private *i915 = engine->i915;
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);
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;
298 err = intel_context_pin(engine->kernel_context);
299 intel_context_put(engine->kernel_context);
301 goto err_breadcrumbs;
306 intel_engine_fini_breadcrumbs(engine);
310 void mock_engine_flush(struct intel_engine_cs *engine)
312 struct mock_engine *mock =
313 container_of(engine, typeof(*mock), base);
314 struct i915_request *request, *rn;
316 del_timer_sync(&mock->hw_delay);
318 spin_lock_irq(&mock->hw_lock);
319 list_for_each_entry_safe(request, rn, &mock->hw_queue, mock.link)
321 spin_unlock_irq(&mock->hw_lock);
324 void mock_engine_reset(struct intel_engine_cs *engine)
328 void mock_engine_free(struct intel_engine_cs *engine)
330 struct mock_engine *mock =
331 container_of(engine, typeof(*mock), base);
333 GEM_BUG_ON(timer_pending(&mock->hw_delay));
335 intel_context_unpin(engine->kernel_context);
337 intel_engine_fini_breadcrumbs(engine);