Merge drm/drm-next into drm-intel-next-queued
[linux-2.6-block.git] / drivers / gpu / drm / i915 / selftests / mock_engine.c
CommitLineData
f97fbf96
CW
1/*
2 * Copyright © 2016 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 *
23 */
24
25#include "mock_engine.h"
0daf0113 26#include "mock_request.h"
f97fbf96 27
a89d1f92
CW
28struct mock_ring {
29 struct intel_ring base;
30 struct i915_timeline timeline;
31};
32
0daf0113 33static struct mock_request *first_request(struct mock_engine *engine)
f97fbf96 34{
0daf0113
CW
35 return list_first_entry_or_null(&engine->hw_queue,
36 struct mock_request,
37 link);
38}
39
b1f9107e
CW
40static void advance(struct mock_engine *engine,
41 struct mock_request *request)
42{
43 list_del_init(&request->link);
44 mock_seqno_advance(&engine->base, request->base.global_seqno);
45}
46
39cbf2aa 47static void hw_delay_complete(struct timer_list *t)
0daf0113 48{
39cbf2aa 49 struct mock_engine *engine = from_timer(engine, t, hw_delay);
0daf0113
CW
50 struct mock_request *request;
51
52 spin_lock(&engine->hw_lock);
53
b1f9107e 54 /* Timer fired, first request is complete */
0daf0113
CW
55 request = first_request(engine);
56 if (request)
b1f9107e
CW
57 advance(engine, request);
58
59 /*
60 * Also immediately signal any subsequent 0-delay requests, but
61 * requeue the timer for the next delayed request.
62 */
63 while ((request = first_request(engine))) {
64 if (request->delay) {
65 mod_timer(&engine->hw_delay, jiffies + request->delay);
66 break;
67 }
68
69 advance(engine, request);
70 }
0daf0113
CW
71
72 spin_unlock(&engine->hw_lock);
73}
74
1fc44d9b 75static void mock_context_unpin(struct intel_context *ce)
0daf0113 76{
1fc44d9b
CW
77 i915_gem_context_put(ce->gem_context);
78}
ab82a063 79
1fc44d9b
CW
80static void mock_context_destroy(struct intel_context *ce)
81{
82 GEM_BUG_ON(ce->pin_count);
0daf0113
CW
83}
84
1fc44d9b
CW
85static const struct intel_context_ops mock_context_ops = {
86 .unpin = mock_context_unpin,
87 .destroy = mock_context_destroy,
88};
89
90static struct intel_context *
91mock_context_pin(struct intel_engine_cs *engine,
92 struct i915_gem_context *ctx)
0daf0113 93{
ab82a063
CW
94 struct intel_context *ce = to_intel_context(ctx, engine);
95
1fc44d9b
CW
96 if (!ce->pin_count++) {
97 i915_gem_context_get(ctx);
98 ce->ring = engine->buffer;
99 ce->ops = &mock_context_ops;
100 }
101
102 return ce;
0daf0113
CW
103}
104
e61e0f51 105static int mock_request_alloc(struct i915_request *request)
0daf0113
CW
106{
107 struct mock_request *mock = container_of(request, typeof(*mock), base);
108
109 INIT_LIST_HEAD(&mock->link);
110 mock->delay = 0;
111
0daf0113
CW
112 return 0;
113}
114
e61e0f51 115static int mock_emit_flush(struct i915_request *request,
0daf0113
CW
116 unsigned int flags)
117{
118 return 0;
119}
120
e61e0f51 121static void mock_emit_breadcrumb(struct i915_request *request,
0daf0113
CW
122 u32 *flags)
123{
124}
125
e61e0f51 126static void mock_submit_request(struct i915_request *request)
0daf0113
CW
127{
128 struct mock_request *mock = container_of(request, typeof(*mock), base);
129 struct mock_engine *engine =
130 container_of(request->engine, typeof(*engine), base);
131
e61e0f51 132 i915_request_submit(request);
0daf0113
CW
133 GEM_BUG_ON(!request->global_seqno);
134
135 spin_lock_irq(&engine->hw_lock);
136 list_add_tail(&mock->link, &engine->hw_queue);
b1f9107e
CW
137 if (mock->link.prev == &engine->hw_queue) {
138 if (mock->delay)
139 mod_timer(&engine->hw_delay, jiffies + mock->delay);
140 else
141 advance(engine, mock);
142 }
0daf0113
CW
143 spin_unlock_irq(&engine->hw_lock);
144}
145
146static struct intel_ring *mock_ring(struct intel_engine_cs *engine)
147{
24fd018a 148 const unsigned long sz = PAGE_SIZE / 2;
a89d1f92 149 struct mock_ring *ring;
0daf0113 150
24fd018a
CW
151 BUILD_BUG_ON(MIN_SPACE_FOR_ADD_REQUEST > sz);
152
0daf0113
CW
153 ring = kzalloc(sizeof(*ring) + sz, GFP_KERNEL);
154 if (!ring)
155 return NULL;
156
a89d1f92 157 i915_timeline_init(engine->i915, &ring->timeline, engine->name);
65fcb806 158
a89d1f92
CW
159 ring->base.size = sz;
160 ring->base.effective_size = sz;
161 ring->base.vaddr = (void *)(ring + 1);
162 ring->base.timeline = &ring->timeline;
0daf0113 163
a89d1f92
CW
164 INIT_LIST_HEAD(&ring->base.request_list);
165 intel_ring_update_space(&ring->base);
0daf0113 166
a89d1f92 167 return &ring->base;
0daf0113
CW
168}
169
a89d1f92 170static void mock_ring_free(struct intel_ring *base)
b887d615 171{
a89d1f92
CW
172 struct mock_ring *ring = container_of(base, typeof(*ring), base);
173
174 i915_timeline_fini(&ring->timeline);
b887d615
CW
175 kfree(ring);
176}
177
0daf0113 178struct intel_engine_cs *mock_engine(struct drm_i915_private *i915,
3ec0af7f
CW
179 const char *name,
180 int id)
0daf0113
CW
181{
182 struct mock_engine *engine;
3ec0af7f
CW
183
184 GEM_BUG_ON(id >= I915_NUM_ENGINES);
f97fbf96
CW
185
186 engine = kzalloc(sizeof(*engine) + PAGE_SIZE, GFP_KERNEL);
187 if (!engine)
188 return NULL;
189
0daf0113
CW
190 /* minimal engine setup for requests */
191 engine->base.i915 = i915;
6e516148 192 snprintf(engine->base.name, sizeof(engine->base.name), "%s", name);
3ec0af7f 193 engine->base.id = id;
0daf0113 194 engine->base.status_page.page_addr = (void *)(engine + 1);
f97fbf96 195
0daf0113 196 engine->base.context_pin = mock_context_pin;
0daf0113
CW
197 engine->base.request_alloc = mock_request_alloc;
198 engine->base.emit_flush = mock_emit_flush;
199 engine->base.emit_breadcrumb = mock_emit_breadcrumb;
200 engine->base.submit_request = mock_submit_request;
201
a89d1f92 202 i915_timeline_init(i915, &engine->base.timeline, engine->base.name);
f911e723 203 i915_timeline_set_subclass(&engine->base.timeline, TIMELINE_ENGINE);
890fd185 204
0daf0113
CW
205 intel_engine_init_breadcrumbs(&engine->base);
206 engine->base.breadcrumbs.mock = true; /* prevent touching HW for irqs */
207
208 /* fake hw queue */
209 spin_lock_init(&engine->hw_lock);
39cbf2aa 210 timer_setup(&engine->hw_delay, hw_delay_complete, 0);
0daf0113
CW
211 INIT_LIST_HEAD(&engine->hw_queue);
212
b887d615
CW
213 engine->base.buffer = mock_ring(&engine->base);
214 if (!engine->base.buffer)
215 goto err_breadcrumbs;
216
4a774ee3
CW
217 if (IS_ERR(intel_context_pin(i915->kernel_context, &engine->base)))
218 goto err_ring;
219
0daf0113 220 return &engine->base;
b887d615 221
4a774ee3
CW
222err_ring:
223 mock_ring_free(engine->base.buffer);
b887d615
CW
224err_breadcrumbs:
225 intel_engine_fini_breadcrumbs(&engine->base);
a89d1f92 226 i915_timeline_fini(&engine->base.timeline);
b887d615
CW
227 kfree(engine);
228 return NULL;
f97fbf96
CW
229}
230
231void mock_engine_flush(struct intel_engine_cs *engine)
232{
0daf0113
CW
233 struct mock_engine *mock =
234 container_of(engine, typeof(*mock), base);
235 struct mock_request *request, *rn;
236
237 del_timer_sync(&mock->hw_delay);
238
239 spin_lock_irq(&mock->hw_lock);
240 list_for_each_entry_safe(request, rn, &mock->hw_queue, link) {
241 list_del_init(&request->link);
242 mock_seqno_advance(&mock->base, request->base.global_seqno);
243 }
244 spin_unlock_irq(&mock->hw_lock);
f97fbf96
CW
245}
246
247void mock_engine_reset(struct intel_engine_cs *engine)
248{
249 intel_write_status_page(engine, I915_GEM_HWS_INDEX, 0);
250}
0daf0113
CW
251
252void mock_engine_free(struct intel_engine_cs *engine)
253{
254 struct mock_engine *mock =
255 container_of(engine, typeof(*mock), base);
1fc44d9b 256 struct intel_context *ce;
0daf0113
CW
257
258 GEM_BUG_ON(timer_pending(&mock->hw_delay));
259
1fc44d9b
CW
260 ce = fetch_and_zero(&engine->last_retired_context);
261 if (ce)
262 intel_context_unpin(ce);
0daf0113 263
4a774ee3
CW
264 __intel_context_unpin(engine->i915->kernel_context, engine);
265
b887d615
CW
266 mock_ring_free(engine->buffer);
267
0daf0113 268 intel_engine_fini_breadcrumbs(engine);
a89d1f92 269 i915_timeline_fini(&engine->timeline);
0daf0113 270
0daf0113
CW
271 kfree(engine);
272}