Merge branch 'linux-4.12' of git://github.com/skeggsb/linux into drm-next
[sfrench/cifs-2.6.git] / drivers / gpu / drm / i915 / selftests / mock_engine.c
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 static struct mock_request *first_request(struct mock_engine *engine)
29 {
30         return list_first_entry_or_null(&engine->hw_queue,
31                                         struct mock_request,
32                                         link);
33 }
34
35 static void hw_delay_complete(unsigned long data)
36 {
37         struct mock_engine *engine = (typeof(engine))data;
38         struct mock_request *request;
39
40         spin_lock(&engine->hw_lock);
41
42         request = first_request(engine);
43         if (request) {
44                 list_del_init(&request->link);
45                 mock_seqno_advance(&engine->base, request->base.global_seqno);
46         }
47
48         request = first_request(engine);
49         if (request)
50                 mod_timer(&engine->hw_delay, jiffies + request->delay);
51
52         spin_unlock(&engine->hw_lock);
53 }
54
55 static int mock_context_pin(struct intel_engine_cs *engine,
56                             struct i915_gem_context *ctx)
57 {
58         i915_gem_context_get(ctx);
59         return 0;
60 }
61
62 static void mock_context_unpin(struct intel_engine_cs *engine,
63                                struct i915_gem_context *ctx)
64 {
65         i915_gem_context_put(ctx);
66 }
67
68 static int mock_request_alloc(struct drm_i915_gem_request *request)
69 {
70         struct mock_request *mock = container_of(request, typeof(*mock), base);
71
72         INIT_LIST_HEAD(&mock->link);
73         mock->delay = 0;
74
75         request->ring = request->engine->buffer;
76         return 0;
77 }
78
79 static int mock_emit_flush(struct drm_i915_gem_request *request,
80                            unsigned int flags)
81 {
82         return 0;
83 }
84
85 static void mock_emit_breadcrumb(struct drm_i915_gem_request *request,
86                                  u32 *flags)
87 {
88 }
89
90 static void mock_submit_request(struct drm_i915_gem_request *request)
91 {
92         struct mock_request *mock = container_of(request, typeof(*mock), base);
93         struct mock_engine *engine =
94                 container_of(request->engine, typeof(*engine), base);
95
96         i915_gem_request_submit(request);
97         GEM_BUG_ON(!request->global_seqno);
98
99         spin_lock_irq(&engine->hw_lock);
100         list_add_tail(&mock->link, &engine->hw_queue);
101         if (mock->link.prev == &engine->hw_queue)
102                 mod_timer(&engine->hw_delay, jiffies + mock->delay);
103         spin_unlock_irq(&engine->hw_lock);
104 }
105
106 static struct intel_ring *mock_ring(struct intel_engine_cs *engine)
107 {
108         const unsigned long sz = roundup_pow_of_two(sizeof(struct intel_ring));
109         struct intel_ring *ring;
110
111         ring = kzalloc(sizeof(*ring) + sz, GFP_KERNEL);
112         if (!ring)
113                 return NULL;
114
115         ring->engine = engine;
116         ring->size = sz;
117         ring->effective_size = sz;
118         ring->vaddr = (void *)(ring + 1);
119
120         INIT_LIST_HEAD(&ring->request_list);
121         intel_ring_update_space(ring);
122
123         return ring;
124 }
125
126 struct intel_engine_cs *mock_engine(struct drm_i915_private *i915,
127                                     const char *name)
128 {
129         struct mock_engine *engine;
130         static int id;
131
132         engine = kzalloc(sizeof(*engine) + PAGE_SIZE, GFP_KERNEL);
133         if (!engine)
134                 return NULL;
135
136         engine->base.buffer = mock_ring(&engine->base);
137         if (!engine->base.buffer) {
138                 kfree(engine);
139                 return NULL;
140         }
141
142         /* minimal engine setup for requests */
143         engine->base.i915 = i915;
144         engine->base.name = name;
145         engine->base.id = id++;
146         engine->base.status_page.page_addr = (void *)(engine + 1);
147
148         engine->base.context_pin = mock_context_pin;
149         engine->base.context_unpin = mock_context_unpin;
150         engine->base.request_alloc = mock_request_alloc;
151         engine->base.emit_flush = mock_emit_flush;
152         engine->base.emit_breadcrumb = mock_emit_breadcrumb;
153         engine->base.submit_request = mock_submit_request;
154
155         engine->base.timeline =
156                 &i915->gt.global_timeline.engine[engine->base.id];
157
158         intel_engine_init_breadcrumbs(&engine->base);
159         engine->base.breadcrumbs.mock = true; /* prevent touching HW for irqs */
160
161         /* fake hw queue */
162         spin_lock_init(&engine->hw_lock);
163         setup_timer(&engine->hw_delay,
164                     hw_delay_complete,
165                     (unsigned long)engine);
166         INIT_LIST_HEAD(&engine->hw_queue);
167
168         return &engine->base;
169 }
170
171 void mock_engine_flush(struct intel_engine_cs *engine)
172 {
173         struct mock_engine *mock =
174                 container_of(engine, typeof(*mock), base);
175         struct mock_request *request, *rn;
176
177         del_timer_sync(&mock->hw_delay);
178
179         spin_lock_irq(&mock->hw_lock);
180         list_for_each_entry_safe(request, rn, &mock->hw_queue, link) {
181                 list_del_init(&request->link);
182                 mock_seqno_advance(&mock->base, request->base.global_seqno);
183         }
184         spin_unlock_irq(&mock->hw_lock);
185 }
186
187 void mock_engine_reset(struct intel_engine_cs *engine)
188 {
189         intel_write_status_page(engine, I915_GEM_HWS_INDEX, 0);
190 }
191
192 void mock_engine_free(struct intel_engine_cs *engine)
193 {
194         struct mock_engine *mock =
195                 container_of(engine, typeof(*mock), base);
196
197         GEM_BUG_ON(timer_pending(&mock->hw_delay));
198
199         if (engine->last_retired_context)
200                 engine->context_unpin(engine, engine->last_retired_context);
201
202         intel_engine_fini_breadcrumbs(engine);
203
204         kfree(engine->buffer);
205         kfree(engine);
206 }