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"
31 #include "intel_engine_pool.h"
33 #include "mock_engine.h"
34 #include "selftests/mock_request.h"
36 static void mock_timeline_pin(struct intel_timeline
*tl
)
38 atomic_inc(&tl
->pin_count
);
41 static void mock_timeline_unpin(struct intel_timeline
*tl
)
43 GEM_BUG_ON(!atomic_read(&tl
->pin_count
));
44 atomic_dec(&tl
->pin_count
);
47 static struct intel_ring
*mock_ring(struct intel_engine_cs
*engine
)
49 const unsigned long sz
= PAGE_SIZE
/ 2;
50 struct intel_ring
*ring
;
52 ring
= kzalloc(sizeof(*ring
) + sz
, GFP_KERNEL
);
56 kref_init(&ring
->ref
);
58 ring
->effective_size
= sz
;
59 ring
->vaddr
= (void *)(ring
+ 1);
60 atomic_set(&ring
->pin_count
, 1);
62 intel_ring_update_space(ring
);
67 static struct i915_request
*first_request(struct mock_engine
*engine
)
69 return list_first_entry_or_null(&engine
->hw_queue
,
74 static void advance(struct i915_request
*request
)
76 list_del_init(&request
->mock
.link
);
77 i915_request_mark_complete(request
);
78 GEM_BUG_ON(!i915_request_completed(request
));
80 intel_engine_signal_breadcrumbs(request
->engine
);
83 static void hw_delay_complete(struct timer_list
*t
)
85 struct mock_engine
*engine
= from_timer(engine
, t
, hw_delay
);
86 struct i915_request
*request
;
89 spin_lock_irqsave(&engine
->hw_lock
, flags
);
91 /* Timer fired, first request is complete */
92 request
= first_request(engine
);
97 * Also immediately signal any subsequent 0-delay requests, but
98 * requeue the timer for the next delayed request.
100 while ((request
= first_request(engine
))) {
101 if (request
->mock
.delay
) {
102 mod_timer(&engine
->hw_delay
,
103 jiffies
+ request
->mock
.delay
);
110 spin_unlock_irqrestore(&engine
->hw_lock
, flags
);
113 static void mock_context_unpin(struct intel_context
*ce
)
117 static void mock_context_destroy(struct kref
*ref
)
119 struct intel_context
*ce
= container_of(ref
, typeof(*ce
), ref
);
121 GEM_BUG_ON(intel_context_is_pinned(ce
));
123 if (test_bit(CONTEXT_ALLOC_BIT
, &ce
->flags
)) {
125 mock_timeline_unpin(ce
->timeline
);
128 intel_context_fini(ce
);
129 intel_context_free(ce
);
132 static int mock_context_alloc(struct intel_context
*ce
)
134 ce
->ring
= mock_ring(ce
->engine
);
138 GEM_BUG_ON(ce
->timeline
);
139 ce
->timeline
= intel_timeline_create(ce
->engine
->gt
, NULL
);
140 if (IS_ERR(ce
->timeline
)) {
142 return PTR_ERR(ce
->timeline
);
145 mock_timeline_pin(ce
->timeline
);
150 static int mock_context_pin(struct intel_context
*ce
)
155 static void mock_context_reset(struct intel_context
*ce
)
159 static const struct intel_context_ops mock_context_ops
= {
160 .alloc
= mock_context_alloc
,
162 .pin
= mock_context_pin
,
163 .unpin
= mock_context_unpin
,
165 .enter
= intel_context_enter_engine
,
166 .exit
= intel_context_exit_engine
,
168 .reset
= mock_context_reset
,
169 .destroy
= mock_context_destroy
,
172 static int mock_request_alloc(struct i915_request
*request
)
174 INIT_LIST_HEAD(&request
->mock
.link
);
175 request
->mock
.delay
= 0;
180 static int mock_emit_flush(struct i915_request
*request
,
186 static u32
*mock_emit_breadcrumb(struct i915_request
*request
, u32
*cs
)
191 static void mock_submit_request(struct i915_request
*request
)
193 struct mock_engine
*engine
=
194 container_of(request
->engine
, typeof(*engine
), base
);
197 i915_request_submit(request
);
199 spin_lock_irqsave(&engine
->hw_lock
, flags
);
200 list_add_tail(&request
->mock
.link
, &engine
->hw_queue
);
201 if (list_is_first(&request
->mock
.link
, &engine
->hw_queue
)) {
202 if (request
->mock
.delay
)
203 mod_timer(&engine
->hw_delay
,
204 jiffies
+ request
->mock
.delay
);
208 spin_unlock_irqrestore(&engine
->hw_lock
, flags
);
211 static void mock_reset_prepare(struct intel_engine_cs
*engine
)
215 static void mock_reset_rewind(struct intel_engine_cs
*engine
, bool stalled
)
220 static void mock_reset_cancel(struct intel_engine_cs
*engine
)
222 struct i915_request
*request
;
225 spin_lock_irqsave(&engine
->active
.lock
, flags
);
227 /* Mark all submitted requests as skipped. */
228 list_for_each_entry(request
, &engine
->active
.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
->active
.lock
, flags
);
238 static void mock_reset_finish(struct intel_engine_cs
*engine
)
242 static void mock_engine_release(struct intel_engine_cs
*engine
)
244 struct mock_engine
*mock
=
245 container_of(engine
, typeof(*mock
), base
);
247 GEM_BUG_ON(timer_pending(&mock
->hw_delay
));
249 intel_context_unpin(engine
->kernel_context
);
250 intel_context_put(engine
->kernel_context
);
252 intel_engine_fini_retire(engine
);
253 intel_engine_fini_breadcrumbs(engine
);
256 struct intel_engine_cs
*mock_engine(struct drm_i915_private
*i915
,
260 struct mock_engine
*engine
;
262 GEM_BUG_ON(id
>= I915_NUM_ENGINES
);
263 GEM_BUG_ON(!i915
->gt
.uncore
);
265 engine
= kzalloc(sizeof(*engine
) + PAGE_SIZE
, GFP_KERNEL
);
269 /* minimal engine setup for requests */
270 engine
->base
.i915
= i915
;
271 engine
->base
.gt
= &i915
->gt
;
272 engine
->base
.uncore
= i915
->gt
.uncore
;
273 snprintf(engine
->base
.name
, sizeof(engine
->base
.name
), "%s", name
);
274 engine
->base
.id
= id
;
275 engine
->base
.mask
= BIT(id
);
276 engine
->base
.legacy_idx
= INVALID_ENGINE
;
277 engine
->base
.instance
= id
;
278 engine
->base
.status_page
.addr
= (void *)(engine
+ 1);
280 engine
->base
.cops
= &mock_context_ops
;
281 engine
->base
.request_alloc
= mock_request_alloc
;
282 engine
->base
.emit_flush
= mock_emit_flush
;
283 engine
->base
.emit_fini_breadcrumb
= mock_emit_breadcrumb
;
284 engine
->base
.submit_request
= mock_submit_request
;
286 engine
->base
.reset
.prepare
= mock_reset_prepare
;
287 engine
->base
.reset
.rewind
= mock_reset_rewind
;
288 engine
->base
.reset
.cancel
= mock_reset_cancel
;
289 engine
->base
.reset
.finish
= mock_reset_finish
;
291 engine
->base
.release
= mock_engine_release
;
293 i915
->gt
.engine
[id
] = &engine
->base
;
294 i915
->gt
.engine_class
[0][id
] = &engine
->base
;
297 spin_lock_init(&engine
->hw_lock
);
298 timer_setup(&engine
->hw_delay
, hw_delay_complete
, 0);
299 INIT_LIST_HEAD(&engine
->hw_queue
);
301 intel_engine_add_user(&engine
->base
);
303 return &engine
->base
;
306 int mock_engine_init(struct intel_engine_cs
*engine
)
308 struct intel_context
*ce
;
310 intel_engine_init_active(engine
, ENGINE_MOCK
);
311 intel_engine_init_breadcrumbs(engine
);
312 intel_engine_init_execlists(engine
);
313 intel_engine_init__pm(engine
);
314 intel_engine_init_retire(engine
);
315 intel_engine_pool_init(&engine
->pool
);
317 ce
= create_kernel_context(engine
);
319 goto err_breadcrumbs
;
321 engine
->kernel_context
= ce
;
325 intel_engine_fini_breadcrumbs(engine
);
329 void mock_engine_flush(struct intel_engine_cs
*engine
)
331 struct mock_engine
*mock
=
332 container_of(engine
, typeof(*mock
), base
);
333 struct i915_request
*request
, *rn
;
335 del_timer_sync(&mock
->hw_delay
);
337 spin_lock_irq(&mock
->hw_lock
);
338 list_for_each_entry_safe(request
, rn
, &mock
->hw_queue
, mock
.link
)
340 spin_unlock_irq(&mock
->hw_lock
);
343 void mock_engine_reset(struct intel_engine_cs
*engine
)