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"
26 #include "mock_request.h"
27
28 struct mock_ring {
29 struct intel_ring base;
30 struct i915_timeline timeline;
31 };
32
first_request(struct mock_engine * engine)33 static struct mock_request *first_request(struct mock_engine *engine)
34 {
35 return list_first_entry_or_null(&engine->hw_queue,
36 struct mock_request,
37 link);
38 }
39
advance(struct mock_engine * engine,struct mock_request * request)40 static 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
hw_delay_complete(struct timer_list * t)47 static void hw_delay_complete(struct timer_list *t)
48 {
49 struct mock_engine *engine = from_timer(engine, t, hw_delay);
50 struct mock_request *request;
51
52 spin_lock(&engine->hw_lock);
53
54 /* Timer fired, first request is complete */
55 request = first_request(engine);
56 if (request)
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 }
71
72 spin_unlock(&engine->hw_lock);
73 }
74
mock_context_unpin(struct intel_context * ce)75 static void mock_context_unpin(struct intel_context *ce)
76 {
77 i915_gem_context_put(ce->gem_context);
78 }
79
mock_context_destroy(struct intel_context * ce)80 static void mock_context_destroy(struct intel_context *ce)
81 {
82 GEM_BUG_ON(ce->pin_count);
83 }
84
85 static const struct intel_context_ops mock_context_ops = {
86 .unpin = mock_context_unpin,
87 .destroy = mock_context_destroy,
88 };
89
90 static struct intel_context *
mock_context_pin(struct intel_engine_cs * engine,struct i915_gem_context * ctx)91 mock_context_pin(struct intel_engine_cs *engine,
92 struct i915_gem_context *ctx)
93 {
94 struct intel_context *ce = to_intel_context(ctx, engine);
95
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;
103 }
104
mock_request_alloc(struct i915_request * request)105 static int mock_request_alloc(struct i915_request *request)
106 {
107 struct mock_request *mock = container_of(request, typeof(*mock), base);
108
109 INIT_LIST_HEAD(&mock->link);
110 mock->delay = 0;
111
112 return 0;
113 }
114
mock_emit_flush(struct i915_request * request,unsigned int flags)115 static int mock_emit_flush(struct i915_request *request,
116 unsigned int flags)
117 {
118 return 0;
119 }
120
mock_emit_breadcrumb(struct i915_request * request,u32 * flags)121 static void mock_emit_breadcrumb(struct i915_request *request,
122 u32 *flags)
123 {
124 }
125
mock_submit_request(struct i915_request * request)126 static void mock_submit_request(struct i915_request *request)
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
132 i915_request_submit(request);
133 GEM_BUG_ON(!request->global_seqno);
134
135 spin_lock_irq(&engine->hw_lock);
136 list_add_tail(&mock->link, &engine->hw_queue);
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 }
143 spin_unlock_irq(&engine->hw_lock);
144 }
145
mock_ring(struct intel_engine_cs * engine)146 static struct intel_ring *mock_ring(struct intel_engine_cs *engine)
147 {
148 const unsigned long sz = PAGE_SIZE / 2;
149 struct mock_ring *ring;
150
151 BUILD_BUG_ON(MIN_SPACE_FOR_ADD_REQUEST > sz);
152
153 ring = kzalloc(sizeof(*ring) + sz, GFP_KERNEL);
154 if (!ring)
155 return NULL;
156
157 i915_timeline_init(engine->i915, &ring->timeline, engine->name);
158
159 ring->base.size = sz;
160 ring->base.effective_size = sz;
161 ring->base.vaddr = (void *)(ring + 1);
162 ring->base.timeline = &ring->timeline;
163
164 INIT_LIST_HEAD(&ring->base.request_list);
165 intel_ring_update_space(&ring->base);
166
167 return &ring->base;
168 }
169
mock_ring_free(struct intel_ring * base)170 static void mock_ring_free(struct intel_ring *base)
171 {
172 struct mock_ring *ring = container_of(base, typeof(*ring), base);
173
174 i915_timeline_fini(&ring->timeline);
175 kfree(ring);
176 }
177
mock_engine(struct drm_i915_private * i915,const char * name,int id)178 struct intel_engine_cs *mock_engine(struct drm_i915_private *i915,
179 const char *name,
180 int id)
181 {
182 struct mock_engine *engine;
183
184 GEM_BUG_ON(id >= I915_NUM_ENGINES);
185
186 engine = kzalloc(sizeof(*engine) + PAGE_SIZE, GFP_KERNEL);
187 if (!engine)
188 return NULL;
189
190 /* minimal engine setup for requests */
191 engine->base.i915 = i915;
192 snprintf(engine->base.name, sizeof(engine->base.name), "%s", name);
193 engine->base.id = id;
194 engine->base.status_page.page_addr = (void *)(engine + 1);
195
196 engine->base.context_pin = mock_context_pin;
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
202 i915_timeline_init(i915, &engine->base.timeline, engine->base.name);
203 lockdep_set_subclass(&engine->base.timeline.lock, TIMELINE_ENGINE);
204
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);
210 timer_setup(&engine->hw_delay, hw_delay_complete, 0);
211 INIT_LIST_HEAD(&engine->hw_queue);
212
213 engine->base.buffer = mock_ring(&engine->base);
214 if (!engine->base.buffer)
215 goto err_breadcrumbs;
216
217 if (IS_ERR(intel_context_pin(i915->kernel_context, &engine->base)))
218 goto err_ring;
219
220 return &engine->base;
221
222 err_ring:
223 mock_ring_free(engine->base.buffer);
224 err_breadcrumbs:
225 intel_engine_fini_breadcrumbs(&engine->base);
226 i915_timeline_fini(&engine->base.timeline);
227 kfree(engine);
228 return NULL;
229 }
230
mock_engine_flush(struct intel_engine_cs * engine)231 void mock_engine_flush(struct intel_engine_cs *engine)
232 {
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);
245 }
246
mock_engine_reset(struct intel_engine_cs * engine)247 void mock_engine_reset(struct intel_engine_cs *engine)
248 {
249 intel_write_status_page(engine, I915_GEM_HWS_INDEX, 0);
250 }
251
mock_engine_free(struct intel_engine_cs * engine)252 void mock_engine_free(struct intel_engine_cs *engine)
253 {
254 struct mock_engine *mock =
255 container_of(engine, typeof(*mock), base);
256 struct intel_context *ce;
257
258 GEM_BUG_ON(timer_pending(&mock->hw_delay));
259
260 ce = fetch_and_zero(&engine->last_retired_context);
261 if (ce)
262 intel_context_unpin(ce);
263
264 __intel_context_unpin(engine->i915->kernel_context, engine);
265
266 mock_ring_free(engine->buffer);
267
268 intel_engine_fini_breadcrumbs(engine);
269 i915_timeline_fini(&engine->timeline);
270
271 kfree(engine);
272 }
273