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
26 #include "i915_gem_context.h"
27 #include "intel_context.h"
28 #include "intel_engine_pm.h"
30 #include "mock_engine.h"
31 #include "selftests/mock_request.h"
34 struct intel_ring base;
35 struct i915_timeline timeline;
38 static void mock_timeline_pin(struct i915_timeline *tl)
43 static void mock_timeline_unpin(struct i915_timeline *tl)
45 GEM_BUG_ON(!tl->pin_count);
49 static struct intel_ring *mock_ring(struct intel_engine_cs *engine)
51 const unsigned long sz = PAGE_SIZE / 2;
52 struct mock_ring *ring;
54 ring = kzalloc(sizeof(*ring) + sz, GFP_KERNEL);
58 if (i915_timeline_init(engine->i915, &ring->timeline, NULL)) {
63 kref_init(&ring->base.ref);
65 ring->base.effective_size = sz;
66 ring->base.vaddr = (void *)(ring + 1);
67 ring->base.timeline = &ring->timeline;
69 INIT_LIST_HEAD(&ring->base.request_list);
70 intel_ring_update_space(&ring->base);
75 static void mock_ring_free(struct intel_ring *base)
77 struct mock_ring *ring = container_of(base, typeof(*ring), base);
79 i915_timeline_fini(&ring->timeline);
83 static struct i915_request *first_request(struct mock_engine *engine)
85 return list_first_entry_or_null(&engine->hw_queue,
90 static void advance(struct i915_request *request)
92 list_del_init(&request->mock.link);
93 i915_request_mark_complete(request);
94 GEM_BUG_ON(!i915_request_completed(request));
96 intel_engine_queue_breadcrumbs(request->engine);
99 static void hw_delay_complete(struct timer_list *t)
101 struct mock_engine *engine = from_timer(engine, t, hw_delay);
102 struct i915_request *request;
105 spin_lock_irqsave(&engine->hw_lock, flags);
107 /* Timer fired, first request is complete */
108 request = first_request(engine);
113 * Also immediately signal any subsequent 0-delay requests, but
114 * requeue the timer for the next delayed request.
116 while ((request = first_request(engine))) {
117 if (request->mock.delay) {
118 mod_timer(&engine->hw_delay,
119 jiffies + request->mock.delay);
126 spin_unlock_irqrestore(&engine->hw_lock, flags);
129 static void mock_context_unpin(struct intel_context *ce)
131 mock_timeline_unpin(ce->ring->timeline);
134 static void mock_context_destroy(struct kref *ref)
136 struct intel_context *ce = container_of(ref, typeof(*ce), ref);
138 GEM_BUG_ON(intel_context_is_pinned(ce));
141 mock_ring_free(ce->ring);
143 intel_context_free(ce);
146 static int mock_context_pin(struct intel_context *ce)
149 ce->ring = mock_ring(ce->engine);
154 mock_timeline_pin(ce->ring->timeline);
158 static const struct intel_context_ops mock_context_ops = {
159 .pin = mock_context_pin,
160 .unpin = mock_context_unpin,
162 .enter = intel_context_enter_engine,
163 .exit = intel_context_exit_engine,
165 .destroy = mock_context_destroy,
168 static int mock_request_alloc(struct i915_request *request)
170 INIT_LIST_HEAD(&request->mock.link);
171 request->mock.delay = 0;
176 static int mock_emit_flush(struct i915_request *request,
182 static u32 *mock_emit_breadcrumb(struct i915_request *request, u32 *cs)
187 static void mock_submit_request(struct i915_request *request)
189 struct mock_engine *engine =
190 container_of(request->engine, typeof(*engine), base);
193 i915_request_submit(request);
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);
204 spin_unlock_irqrestore(&engine->hw_lock, flags);
207 static void mock_reset_prepare(struct intel_engine_cs *engine)
211 static void mock_reset(struct intel_engine_cs *engine, bool stalled)
216 static void mock_reset_finish(struct intel_engine_cs *engine)
220 static void mock_cancel_requests(struct intel_engine_cs *engine)
222 struct i915_request *request;
225 spin_lock_irqsave(&engine->timeline.lock, flags);
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);
232 i915_request_mark_complete(request);
235 spin_unlock_irqrestore(&engine->timeline.lock, flags);
238 struct intel_engine_cs *mock_engine(struct drm_i915_private *i915,
242 struct mock_engine *engine;
244 GEM_BUG_ON(id >= I915_NUM_ENGINES);
246 engine = kzalloc(sizeof(*engine) + PAGE_SIZE, GFP_KERNEL);
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);
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;
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;
269 spin_lock_init(&engine->hw_lock);
270 timer_setup(&engine->hw_delay, hw_delay_complete, 0);
271 INIT_LIST_HEAD(&engine->hw_queue);
273 return &engine->base;
276 int mock_engine_init(struct intel_engine_cs *engine)
278 struct drm_i915_private *i915 = engine->i915;
281 intel_engine_init_breadcrumbs(engine);
282 intel_engine_init_execlists(engine);
283 intel_engine_init__pm(engine);
285 if (i915_timeline_init(i915, &engine->timeline, NULL))
286 goto err_breadcrumbs;
287 i915_timeline_set_subclass(&engine->timeline, TIMELINE_ENGINE);
289 engine->kernel_context =
290 i915_gem_context_get_engine(i915->kernel_context, engine->id);
291 if (IS_ERR(engine->kernel_context))
294 err = intel_context_pin(engine->kernel_context);
295 intel_context_put(engine->kernel_context);
302 i915_timeline_fini(&engine->timeline);
304 intel_engine_fini_breadcrumbs(engine);
308 void mock_engine_flush(struct intel_engine_cs *engine)
310 struct mock_engine *mock =
311 container_of(engine, typeof(*mock), base);
312 struct i915_request *request, *rn;
314 del_timer_sync(&mock->hw_delay);
316 spin_lock_irq(&mock->hw_lock);
317 list_for_each_entry_safe(request, rn, &mock->hw_queue, mock.link)
319 spin_unlock_irq(&mock->hw_lock);
322 void mock_engine_reset(struct intel_engine_cs *engine)
326 void mock_engine_free(struct intel_engine_cs *engine)
328 struct mock_engine *mock =
329 container_of(engine, typeof(*mock), base);
330 struct intel_context *ce;
332 GEM_BUG_ON(timer_pending(&mock->hw_delay));
334 ce = fetch_and_zero(&engine->last_retired_context);
336 intel_context_unpin(ce);
338 intel_context_unpin(engine->kernel_context);
340 intel_engine_fini_breadcrumbs(engine);
341 i915_timeline_fini(&engine->timeline);