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"
26 #include "gt/intel_ring.h"
29 #include "intel_context.h"
30 #include "intel_engine_pm.h"
32 #include "mock_engine.h"
33 #include "selftests/mock_request.h"
35 static void mock_timeline_pin(struct intel_timeline *tl)
37 atomic_inc(&tl->pin_count);
40 static void mock_timeline_unpin(struct intel_timeline *tl)
42 GEM_BUG_ON(!atomic_read(&tl->pin_count));
43 atomic_dec(&tl->pin_count);
46 static struct intel_ring *mock_ring(struct intel_engine_cs *engine)
48 const unsigned long sz = PAGE_SIZE / 2;
49 struct intel_ring *ring;
51 ring = kzalloc(sizeof(*ring) + sz, GFP_KERNEL);
55 kref_init(&ring->ref);
57 ring->effective_size = sz;
58 ring->vaddr = (void *)(ring + 1);
59 atomic_set(&ring->pin_count, 1);
61 ring->vma = i915_vma_alloc();
66 i915_active_init(&ring->vma->active, NULL, NULL);
67 __set_bit(I915_VMA_GGTT_BIT, __i915_vma_flags(ring->vma));
68 __set_bit(DRM_MM_NODE_ALLOCATED_BIT, &ring->vma->node.flags);
69 ring->vma->node.size = sz;
71 intel_ring_update_space(ring);
76 static void mock_ring_free(struct intel_ring *ring)
78 i915_active_fini(&ring->vma->active);
79 i915_vma_free(ring->vma);
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_signal_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)
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));
140 if (test_bit(CONTEXT_ALLOC_BIT, &ce->flags)) {
141 mock_ring_free(ce->ring);
142 mock_timeline_unpin(ce->timeline);
145 intel_context_fini(ce);
146 intel_context_free(ce);
149 static int mock_context_alloc(struct intel_context *ce)
151 ce->ring = mock_ring(ce->engine);
155 GEM_BUG_ON(ce->timeline);
156 ce->timeline = intel_timeline_create(ce->engine->gt);
157 if (IS_ERR(ce->timeline)) {
159 return PTR_ERR(ce->timeline);
162 mock_timeline_pin(ce->timeline);
167 static int mock_context_pin(struct intel_context *ce)
172 static void mock_context_reset(struct intel_context *ce)
176 static const struct intel_context_ops mock_context_ops = {
177 .alloc = mock_context_alloc,
179 .pin = mock_context_pin,
180 .unpin = mock_context_unpin,
182 .enter = intel_context_enter_engine,
183 .exit = intel_context_exit_engine,
185 .reset = mock_context_reset,
186 .destroy = mock_context_destroy,
189 static int mock_request_alloc(struct i915_request *request)
191 INIT_LIST_HEAD(&request->mock.link);
192 request->mock.delay = 0;
197 static int mock_emit_flush(struct i915_request *request,
203 static u32 *mock_emit_breadcrumb(struct i915_request *request, u32 *cs)
208 static void mock_submit_request(struct i915_request *request)
210 struct mock_engine *engine =
211 container_of(request->engine, typeof(*engine), base);
214 i915_request_submit(request);
216 spin_lock_irqsave(&engine->hw_lock, flags);
217 list_add_tail(&request->mock.link, &engine->hw_queue);
218 if (list_is_first(&request->mock.link, &engine->hw_queue)) {
219 if (request->mock.delay)
220 mod_timer(&engine->hw_delay,
221 jiffies + request->mock.delay);
225 spin_unlock_irqrestore(&engine->hw_lock, flags);
228 static void mock_reset_prepare(struct intel_engine_cs *engine)
232 static void mock_reset_rewind(struct intel_engine_cs *engine, bool stalled)
237 static void mock_reset_cancel(struct intel_engine_cs *engine)
239 struct i915_request *request;
242 spin_lock_irqsave(&engine->active.lock, flags);
244 /* Mark all submitted requests as skipped. */
245 list_for_each_entry(request, &engine->active.requests, sched.link) {
246 i915_request_set_error_once(request, -EIO);
247 i915_request_mark_complete(request);
250 spin_unlock_irqrestore(&engine->active.lock, flags);
253 static void mock_reset_finish(struct intel_engine_cs *engine)
257 static void mock_engine_release(struct intel_engine_cs *engine)
259 struct mock_engine *mock =
260 container_of(engine, typeof(*mock), base);
262 GEM_BUG_ON(timer_pending(&mock->hw_delay));
264 intel_context_unpin(engine->kernel_context);
265 intel_context_put(engine->kernel_context);
267 intel_engine_fini_retire(engine);
268 intel_engine_fini_breadcrumbs(engine);
271 struct intel_engine_cs *mock_engine(struct drm_i915_private *i915,
275 struct mock_engine *engine;
277 GEM_BUG_ON(id >= I915_NUM_ENGINES);
278 GEM_BUG_ON(!i915->gt.uncore);
280 engine = kzalloc(sizeof(*engine) + PAGE_SIZE, GFP_KERNEL);
284 /* minimal engine setup for requests */
285 engine->base.i915 = i915;
286 engine->base.gt = &i915->gt;
287 engine->base.uncore = i915->gt.uncore;
288 snprintf(engine->base.name, sizeof(engine->base.name), "%s", name);
289 engine->base.id = id;
290 engine->base.mask = BIT(id);
291 engine->base.legacy_idx = INVALID_ENGINE;
292 engine->base.instance = id;
293 engine->base.status_page.addr = (void *)(engine + 1);
295 engine->base.cops = &mock_context_ops;
296 engine->base.request_alloc = mock_request_alloc;
297 engine->base.emit_flush = mock_emit_flush;
298 engine->base.emit_fini_breadcrumb = mock_emit_breadcrumb;
299 engine->base.submit_request = mock_submit_request;
301 engine->base.reset.prepare = mock_reset_prepare;
302 engine->base.reset.rewind = mock_reset_rewind;
303 engine->base.reset.cancel = mock_reset_cancel;
304 engine->base.reset.finish = mock_reset_finish;
306 engine->base.release = mock_engine_release;
308 i915->gt.engine[id] = &engine->base;
309 i915->gt.engine_class[0][id] = &engine->base;
312 spin_lock_init(&engine->hw_lock);
313 timer_setup(&engine->hw_delay, hw_delay_complete, 0);
314 INIT_LIST_HEAD(&engine->hw_queue);
316 intel_engine_add_user(&engine->base);
318 return &engine->base;
321 int mock_engine_init(struct intel_engine_cs *engine)
323 struct intel_context *ce;
325 intel_engine_init_active(engine, ENGINE_MOCK);
326 intel_engine_init_breadcrumbs(engine);
327 intel_engine_init_execlists(engine);
328 intel_engine_init__pm(engine);
329 intel_engine_init_retire(engine);
331 ce = create_kernel_context(engine);
333 goto err_breadcrumbs;
335 /* We insist the kernel context is using the status_page */
336 engine->status_page.vma = ce->timeline->hwsp_ggtt;
338 engine->kernel_context = ce;
342 intel_engine_fini_breadcrumbs(engine);
346 void mock_engine_flush(struct intel_engine_cs *engine)
348 struct mock_engine *mock =
349 container_of(engine, typeof(*mock), base);
350 struct i915_request *request, *rn;
352 del_timer_sync(&mock->hw_delay);
354 spin_lock_irq(&mock->hw_lock);
355 list_for_each_entry_safe(request, rn, &mock->hw_queue, mock.link)
357 spin_unlock_irq(&mock->hw_lock);
360 void mock_engine_reset(struct intel_engine_cs *engine)