blob: 0aafa8a105b8f08165c715740434b58ffd00b10e [file] [log] [blame]
Chris Wilsonf97fbf92017-02-13 17:15:14 +00001/*
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"
Chris Wilson0daf0112017-02-13 17:15:19 +000026#include "mock_request.h"
Chris Wilsonf97fbf92017-02-13 17:15:14 +000027
Chris Wilson0daf0112017-02-13 17:15:19 +000028static struct mock_request *first_request(struct mock_engine *engine)
Chris Wilsonf97fbf92017-02-13 17:15:14 +000029{
Chris Wilson0daf0112017-02-13 17:15:19 +000030 return list_first_entry_or_null(&engine->hw_queue,
31 struct mock_request,
32 link);
33}
34
Chris Wilsonb1f91072017-10-24 23:08:54 +010035static void advance(struct mock_engine *engine,
36 struct mock_request *request)
37{
38 list_del_init(&request->link);
39 mock_seqno_advance(&engine->base, request->base.global_seqno);
40}
41
Kees Cook39cbf2a2017-10-17 09:53:04 +030042static void hw_delay_complete(struct timer_list *t)
Chris Wilson0daf0112017-02-13 17:15:19 +000043{
Kees Cook39cbf2a2017-10-17 09:53:04 +030044 struct mock_engine *engine = from_timer(engine, t, hw_delay);
Chris Wilson0daf0112017-02-13 17:15:19 +000045 struct mock_request *request;
46
47 spin_lock(&engine->hw_lock);
48
Chris Wilsonb1f91072017-10-24 23:08:54 +010049 /* Timer fired, first request is complete */
Chris Wilson0daf0112017-02-13 17:15:19 +000050 request = first_request(engine);
51 if (request)
Chris Wilsonb1f91072017-10-24 23:08:54 +010052 advance(engine, request);
53
54 /*
55 * Also immediately signal any subsequent 0-delay requests, but
56 * requeue the timer for the next delayed request.
57 */
58 while ((request = first_request(engine))) {
59 if (request->delay) {
60 mod_timer(&engine->hw_delay, jiffies + request->delay);
61 break;
62 }
63
64 advance(engine, request);
65 }
Chris Wilson0daf0112017-02-13 17:15:19 +000066
67 spin_unlock(&engine->hw_lock);
68}
69
Chris Wilson266a2402017-05-04 10:33:08 +010070static struct intel_ring *
71mock_context_pin(struct intel_engine_cs *engine,
72 struct i915_gem_context *ctx)
Chris Wilson0daf0112017-02-13 17:15:19 +000073{
74 i915_gem_context_get(ctx);
Chris Wilson266a2402017-05-04 10:33:08 +010075 return engine->buffer;
Chris Wilson0daf0112017-02-13 17:15:19 +000076}
77
78static void mock_context_unpin(struct intel_engine_cs *engine,
79 struct i915_gem_context *ctx)
80{
81 i915_gem_context_put(ctx);
82}
83
84static int mock_request_alloc(struct drm_i915_gem_request *request)
85{
86 struct mock_request *mock = container_of(request, typeof(*mock), base);
87
88 INIT_LIST_HEAD(&mock->link);
89 mock->delay = 0;
90
Chris Wilson0daf0112017-02-13 17:15:19 +000091 return 0;
92}
93
94static int mock_emit_flush(struct drm_i915_gem_request *request,
95 unsigned int flags)
96{
97 return 0;
98}
99
100static void mock_emit_breadcrumb(struct drm_i915_gem_request *request,
101 u32 *flags)
102{
103}
104
105static void mock_submit_request(struct drm_i915_gem_request *request)
106{
107 struct mock_request *mock = container_of(request, typeof(*mock), base);
108 struct mock_engine *engine =
109 container_of(request->engine, typeof(*engine), base);
110
111 i915_gem_request_submit(request);
112 GEM_BUG_ON(!request->global_seqno);
113
114 spin_lock_irq(&engine->hw_lock);
115 list_add_tail(&mock->link, &engine->hw_queue);
Chris Wilsonb1f91072017-10-24 23:08:54 +0100116 if (mock->link.prev == &engine->hw_queue) {
117 if (mock->delay)
118 mod_timer(&engine->hw_delay, jiffies + mock->delay);
119 else
120 advance(engine, mock);
121 }
Chris Wilson0daf0112017-02-13 17:15:19 +0000122 spin_unlock_irq(&engine->hw_lock);
123}
124
125static struct intel_ring *mock_ring(struct intel_engine_cs *engine)
126{
127 const unsigned long sz = roundup_pow_of_two(sizeof(struct intel_ring));
128 struct intel_ring *ring;
129
130 ring = kzalloc(sizeof(*ring) + sz, GFP_KERNEL);
131 if (!ring)
132 return NULL;
133
Chris Wilson0daf0112017-02-13 17:15:19 +0000134 ring->size = sz;
135 ring->effective_size = sz;
136 ring->vaddr = (void *)(ring + 1);
137
138 INIT_LIST_HEAD(&ring->request_list);
Chris Wilson0daf0112017-02-13 17:15:19 +0000139 intel_ring_update_space(ring);
140
141 return ring;
142}
143
144struct intel_engine_cs *mock_engine(struct drm_i915_private *i915,
Chris Wilson3ec0af72017-08-09 17:39:30 +0100145 const char *name,
146 int id)
Chris Wilson0daf0112017-02-13 17:15:19 +0000147{
148 struct mock_engine *engine;
Chris Wilson3ec0af72017-08-09 17:39:30 +0100149
150 GEM_BUG_ON(id >= I915_NUM_ENGINES);
Chris Wilsonf97fbf92017-02-13 17:15:14 +0000151
152 engine = kzalloc(sizeof(*engine) + PAGE_SIZE, GFP_KERNEL);
153 if (!engine)
154 return NULL;
155
Chris Wilson0daf0112017-02-13 17:15:19 +0000156 engine->base.buffer = mock_ring(&engine->base);
157 if (!engine->base.buffer) {
158 kfree(engine);
159 return NULL;
160 }
Chris Wilsonf97fbf92017-02-13 17:15:14 +0000161
Chris Wilson0daf0112017-02-13 17:15:19 +0000162 /* minimal engine setup for requests */
163 engine->base.i915 = i915;
Oscar Mateo6e516142017-04-10 07:34:31 -0700164 snprintf(engine->base.name, sizeof(engine->base.name), "%s", name);
Chris Wilson3ec0af72017-08-09 17:39:30 +0100165 engine->base.id = id;
Chris Wilson0daf0112017-02-13 17:15:19 +0000166 engine->base.status_page.page_addr = (void *)(engine + 1);
Chris Wilsonf97fbf92017-02-13 17:15:14 +0000167
Chris Wilson0daf0112017-02-13 17:15:19 +0000168 engine->base.context_pin = mock_context_pin;
169 engine->base.context_unpin = mock_context_unpin;
170 engine->base.request_alloc = mock_request_alloc;
171 engine->base.emit_flush = mock_emit_flush;
172 engine->base.emit_breadcrumb = mock_emit_breadcrumb;
173 engine->base.submit_request = mock_submit_request;
174
175 engine->base.timeline =
176 &i915->gt.global_timeline.engine[engine->base.id];
177
178 intel_engine_init_breadcrumbs(&engine->base);
179 engine->base.breadcrumbs.mock = true; /* prevent touching HW for irqs */
180
181 /* fake hw queue */
182 spin_lock_init(&engine->hw_lock);
Kees Cook39cbf2a2017-10-17 09:53:04 +0300183 timer_setup(&engine->hw_delay, hw_delay_complete, 0);
Chris Wilson0daf0112017-02-13 17:15:19 +0000184 INIT_LIST_HEAD(&engine->hw_queue);
185
186 return &engine->base;
Chris Wilsonf97fbf92017-02-13 17:15:14 +0000187}
188
189void mock_engine_flush(struct intel_engine_cs *engine)
190{
Chris Wilson0daf0112017-02-13 17:15:19 +0000191 struct mock_engine *mock =
192 container_of(engine, typeof(*mock), base);
193 struct mock_request *request, *rn;
194
195 del_timer_sync(&mock->hw_delay);
196
197 spin_lock_irq(&mock->hw_lock);
198 list_for_each_entry_safe(request, rn, &mock->hw_queue, link) {
199 list_del_init(&request->link);
200 mock_seqno_advance(&mock->base, request->base.global_seqno);
201 }
202 spin_unlock_irq(&mock->hw_lock);
Chris Wilsonf97fbf92017-02-13 17:15:14 +0000203}
204
205void mock_engine_reset(struct intel_engine_cs *engine)
206{
207 intel_write_status_page(engine, I915_GEM_HWS_INDEX, 0);
208}
Chris Wilson0daf0112017-02-13 17:15:19 +0000209
210void mock_engine_free(struct intel_engine_cs *engine)
211{
212 struct mock_engine *mock =
213 container_of(engine, typeof(*mock), base);
214
215 GEM_BUG_ON(timer_pending(&mock->hw_delay));
216
217 if (engine->last_retired_context)
218 engine->context_unpin(engine, engine->last_retired_context);
219
220 intel_engine_fini_breadcrumbs(engine);
221
222 kfree(engine->buffer);
223 kfree(engine);
224}