1/*	$NetBSD: mock_engine.c,v 1.2 2021/12/18 23:45:30 riastradh Exp $	*/
2
3/*
4 * Copyright �� 2016 Intel Corporation
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 * IN THE SOFTWARE.
24 *
25 */
26
27#include <sys/cdefs.h>
28__KERNEL_RCSID(0, "$NetBSD: mock_engine.c,v 1.2 2021/12/18 23:45:30 riastradh Exp $");
29
30#include "gem/i915_gem_context.h"
31#include "gt/intel_ring.h"
32
33#include "i915_drv.h"
34#include "intel_context.h"
35#include "intel_engine_pm.h"
36#include "intel_engine_pool.h"
37
38#include "mock_engine.h"
39#include "selftests/mock_request.h"
40
41static void mock_timeline_pin(struct intel_timeline *tl)
42{
43	atomic_inc(&tl->pin_count);
44}
45
46static void mock_timeline_unpin(struct intel_timeline *tl)
47{
48	GEM_BUG_ON(!atomic_read(&tl->pin_count));
49	atomic_dec(&tl->pin_count);
50}
51
52static struct intel_ring *mock_ring(struct intel_engine_cs *engine)
53{
54	const unsigned long sz = PAGE_SIZE / 2;
55	struct intel_ring *ring;
56
57	ring = kzalloc(sizeof(*ring) + sz, GFP_KERNEL);
58	if (!ring)
59		return NULL;
60
61	kref_init(&ring->ref);
62	ring->size = sz;
63	ring->effective_size = sz;
64	ring->vaddr = (void *)(ring + 1);
65	atomic_set(&ring->pin_count, 1);
66
67	ring->vma = i915_vma_alloc();
68	if (!ring->vma) {
69		kfree(ring);
70		return NULL;
71	}
72	i915_active_init(&ring->vma->active, NULL, NULL);
73
74	intel_ring_update_space(ring);
75
76	return ring;
77}
78
79static void mock_ring_free(struct intel_ring *ring)
80{
81	i915_active_fini(&ring->vma->active);
82	i915_vma_free(ring->vma);
83
84	kfree(ring);
85}
86
87static struct i915_request *first_request(struct mock_engine *engine)
88{
89	return list_first_entry_or_null(&engine->hw_queue,
90					struct i915_request,
91					mock.link);
92}
93
94static void advance(struct i915_request *request)
95{
96	list_del_init(&request->mock.link);
97	i915_request_mark_complete(request);
98	GEM_BUG_ON(!i915_request_completed(request));
99
100	intel_engine_signal_breadcrumbs(request->engine);
101}
102
103static void hw_delay_complete(struct timer_list *t)
104{
105	struct mock_engine *engine = from_timer(engine, t, hw_delay);
106	struct i915_request *request;
107	unsigned long flags;
108
109	spin_lock_irqsave(&engine->hw_lock, flags);
110
111	/* Timer fired, first request is complete */
112	request = first_request(engine);
113	if (request)
114		advance(request);
115
116	/*
117	 * Also immediately signal any subsequent 0-delay requests, but
118	 * requeue the timer for the next delayed request.
119	 */
120	while ((request = first_request(engine))) {
121		if (request->mock.delay) {
122			mod_timer(&engine->hw_delay,
123				  jiffies + request->mock.delay);
124			break;
125		}
126
127		advance(request);
128	}
129
130	spin_unlock_irqrestore(&engine->hw_lock, flags);
131}
132
133static void mock_context_unpin(struct intel_context *ce)
134{
135}
136
137static void mock_context_destroy(struct kref *ref)
138{
139	struct intel_context *ce = container_of(ref, typeof(*ce), ref);
140
141	GEM_BUG_ON(intel_context_is_pinned(ce));
142
143	if (test_bit(CONTEXT_ALLOC_BIT, &ce->flags)) {
144		mock_ring_free(ce->ring);
145		mock_timeline_unpin(ce->timeline);
146	}
147
148	intel_context_fini(ce);
149	intel_context_free(ce);
150}
151
152static int mock_context_alloc(struct intel_context *ce)
153{
154	ce->ring = mock_ring(ce->engine);
155	if (!ce->ring)
156		return -ENOMEM;
157
158	GEM_BUG_ON(ce->timeline);
159	ce->timeline = intel_timeline_create(ce->engine->gt, NULL);
160	if (IS_ERR(ce->timeline)) {
161		kfree(ce->engine);
162		return PTR_ERR(ce->timeline);
163	}
164
165	mock_timeline_pin(ce->timeline);
166
167	return 0;
168}
169
170static int mock_context_pin(struct intel_context *ce)
171{
172	return 0;
173}
174
175static void mock_context_reset(struct intel_context *ce)
176{
177}
178
179static const struct intel_context_ops mock_context_ops = {
180	.alloc = mock_context_alloc,
181
182	.pin = mock_context_pin,
183	.unpin = mock_context_unpin,
184
185	.enter = intel_context_enter_engine,
186	.exit = intel_context_exit_engine,
187
188	.reset = mock_context_reset,
189	.destroy = mock_context_destroy,
190};
191
192static int mock_request_alloc(struct i915_request *request)
193{
194	INIT_LIST_HEAD(&request->mock.link);
195	request->mock.delay = 0;
196
197	return 0;
198}
199
200static int mock_emit_flush(struct i915_request *request,
201			   unsigned int flags)
202{
203	return 0;
204}
205
206static u32 *mock_emit_breadcrumb(struct i915_request *request, u32 *cs)
207{
208	return cs;
209}
210
211static void mock_submit_request(struct i915_request *request)
212{
213	struct mock_engine *engine =
214		container_of(request->engine, typeof(*engine), base);
215	unsigned long flags;
216
217	i915_request_submit(request);
218
219	spin_lock_irqsave(&engine->hw_lock, flags);
220	list_add_tail(&request->mock.link, &engine->hw_queue);
221	if (list_is_first(&request->mock.link, &engine->hw_queue)) {
222		if (request->mock.delay)
223			mod_timer(&engine->hw_delay,
224				  jiffies + request->mock.delay);
225		else
226			advance(request);
227	}
228	spin_unlock_irqrestore(&engine->hw_lock, flags);
229}
230
231static void mock_reset_prepare(struct intel_engine_cs *engine)
232{
233}
234
235static void mock_reset_rewind(struct intel_engine_cs *engine, bool stalled)
236{
237	GEM_BUG_ON(stalled);
238}
239
240static void mock_reset_cancel(struct intel_engine_cs *engine)
241{
242	struct i915_request *request;
243	unsigned long flags;
244
245	spin_lock_irqsave(&engine->active.lock, flags);
246
247	/* Mark all submitted requests as skipped. */
248	list_for_each_entry(request, &engine->active.requests, sched.link) {
249		if (!i915_request_signaled(request))
250			dma_fence_set_error(&request->fence, -EIO);
251
252		i915_request_mark_complete(request);
253	}
254
255	spin_unlock_irqrestore(&engine->active.lock, flags);
256}
257
258static void mock_reset_finish(struct intel_engine_cs *engine)
259{
260}
261
262static void mock_engine_release(struct intel_engine_cs *engine)
263{
264	struct mock_engine *mock =
265		container_of(engine, typeof(*mock), base);
266
267	GEM_BUG_ON(timer_pending(&mock->hw_delay));
268
269	intel_context_unpin(engine->kernel_context);
270	intel_context_put(engine->kernel_context);
271
272	intel_engine_fini_retire(engine);
273	intel_engine_fini_breadcrumbs(engine);
274}
275
276struct intel_engine_cs *mock_engine(struct drm_i915_private *i915,
277				    const char *name,
278				    int id)
279{
280	struct mock_engine *engine;
281
282	GEM_BUG_ON(id >= I915_NUM_ENGINES);
283	GEM_BUG_ON(!i915->gt.uncore);
284
285	engine = kzalloc(sizeof(*engine) + PAGE_SIZE, GFP_KERNEL);
286	if (!engine)
287		return NULL;
288
289	/* minimal engine setup for requests */
290	engine->base.i915 = i915;
291	engine->base.gt = &i915->gt;
292	engine->base.uncore = i915->gt.uncore;
293	snprintf(engine->base.name, sizeof(engine->base.name), "%s", name);
294	engine->base.id = id;
295	engine->base.mask = BIT(id);
296	engine->base.legacy_idx = INVALID_ENGINE;
297	engine->base.instance = id;
298	engine->base.status_page.addr = (void *)(engine + 1);
299
300	engine->base.cops = &mock_context_ops;
301	engine->base.request_alloc = mock_request_alloc;
302	engine->base.emit_flush = mock_emit_flush;
303	engine->base.emit_fini_breadcrumb = mock_emit_breadcrumb;
304	engine->base.submit_request = mock_submit_request;
305
306	engine->base.reset.prepare = mock_reset_prepare;
307	engine->base.reset.rewind = mock_reset_rewind;
308	engine->base.reset.cancel = mock_reset_cancel;
309	engine->base.reset.finish = mock_reset_finish;
310
311	engine->base.release = mock_engine_release;
312
313	i915->gt.engine[id] = &engine->base;
314	i915->gt.engine_class[0][id] = &engine->base;
315
316	/* fake hw queue */
317	spin_lock_init(&engine->hw_lock);
318	timer_setup(&engine->hw_delay, hw_delay_complete, 0);
319	INIT_LIST_HEAD(&engine->hw_queue);
320
321	intel_engine_add_user(&engine->base);
322
323	return &engine->base;
324}
325
326int mock_engine_init(struct intel_engine_cs *engine)
327{
328	struct intel_context *ce;
329
330	intel_engine_init_active(engine, ENGINE_MOCK);
331	intel_engine_init_breadcrumbs(engine);
332	intel_engine_init_execlists(engine);
333	intel_engine_init__pm(engine);
334	intel_engine_init_retire(engine);
335	intel_engine_pool_init(&engine->pool);
336
337	ce = create_kernel_context(engine);
338	if (IS_ERR(ce))
339		goto err_breadcrumbs;
340
341	engine->kernel_context = ce;
342	return 0;
343
344err_breadcrumbs:
345	intel_engine_fini_breadcrumbs(engine);
346	return -ENOMEM;
347}
348
349void mock_engine_flush(struct intel_engine_cs *engine)
350{
351	struct mock_engine *mock =
352		container_of(engine, typeof(*mock), base);
353	struct i915_request *request, *rn;
354
355	del_timer_sync(&mock->hw_delay);
356
357	spin_lock_irq(&mock->hw_lock);
358	list_for_each_entry_safe(request, rn, &mock->hw_queue, mock.link)
359		advance(request);
360	spin_unlock_irq(&mock->hw_lock);
361}
362
363void mock_engine_reset(struct intel_engine_cs *engine)
364{
365}
366