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 "gem/i915_gem_context.h" 26 27 #include "i915_drv.h" 28 #include "intel_context.h" 29 #include "intel_engine_pm.h" 30 31 #include "mock_engine.h" 32 #include "selftests/mock_request.h" 33 34 struct mock_ring { 35 struct intel_ring base; 36 struct intel_timeline timeline; 37 }; 38 39 static void mock_timeline_pin(struct intel_timeline *tl) 40 { 41 tl->pin_count++; 42 } 43 44 static void mock_timeline_unpin(struct intel_timeline *tl) 45 { 46 GEM_BUG_ON(!tl->pin_count); 47 tl->pin_count--; 48 } 49 50 static struct intel_ring *mock_ring(struct intel_engine_cs *engine) 51 { 52 const unsigned long sz = PAGE_SIZE / 2; 53 struct mock_ring *ring; 54 55 ring = kzalloc(sizeof(*ring) + sz, GFP_KERNEL); 56 if (!ring) 57 return NULL; 58 59 if (intel_timeline_init(&ring->timeline, engine->gt, NULL)) { 60 kfree(ring); 61 return NULL; 62 } 63 64 kref_init(&ring->base.ref); 65 ring->base.size = sz; 66 ring->base.effective_size = sz; 67 ring->base.vaddr = (void *)(ring + 1); 68 ring->base.timeline = &ring->timeline; 69 atomic_set(&ring->base.pin_count, 1); 70 71 INIT_LIST_HEAD(&ring->base.request_list); 72 intel_ring_update_space(&ring->base); 73 74 return &ring->base; 75 } 76 77 static void mock_ring_free(struct intel_ring *base) 78 { 79 struct mock_ring *ring = container_of(base, typeof(*ring), base); 80 81 intel_timeline_fini(&ring->timeline); 82 kfree(ring); 83 } 84 85 static struct i915_request *first_request(struct mock_engine *engine) 86 { 87 return list_first_entry_or_null(&engine->hw_queue, 88 struct i915_request, 89 mock.link); 90 } 91 92 static void advance(struct i915_request *request) 93 { 94 list_del_init(&request->mock.link); 95 i915_request_mark_complete(request); 96 GEM_BUG_ON(!i915_request_completed(request)); 97 98 intel_engine_queue_breadcrumbs(request->engine); 99 } 100 101 static void hw_delay_complete(struct timer_list *t) 102 { 103 struct mock_engine *engine = from_timer(engine, t, hw_delay); 104 struct i915_request *request; 105 unsigned long flags; 106 107 spin_lock_irqsave(&engine->hw_lock, flags); 108 109 /* Timer fired, first request is complete */ 110 request = first_request(engine); 111 if (request) 112 advance(request); 113 114 /* 115 * Also immediately signal any subsequent 0-delay requests, but 116 * requeue the timer for the next delayed request. 117 */ 118 while ((request = first_request(engine))) { 119 if (request->mock.delay) { 120 mod_timer(&engine->hw_delay, 121 jiffies + request->mock.delay); 122 break; 123 } 124 125 advance(request); 126 } 127 128 spin_unlock_irqrestore(&engine->hw_lock, flags); 129 } 130 131 static void mock_context_unpin(struct intel_context *ce) 132 { 133 mock_timeline_unpin(ce->ring->timeline); 134 } 135 136 static void mock_context_destroy(struct kref *ref) 137 { 138 struct intel_context *ce = container_of(ref, typeof(*ce), ref); 139 140 GEM_BUG_ON(intel_context_is_pinned(ce)); 141 142 if (ce->ring) 143 mock_ring_free(ce->ring); 144 145 intel_context_fini(ce); 146 intel_context_free(ce); 147 } 148 149 static int mock_context_pin(struct intel_context *ce) 150 { 151 int ret; 152 153 if (!ce->ring) { 154 ce->ring = mock_ring(ce->engine); 155 if (!ce->ring) 156 return -ENOMEM; 157 } 158 159 ret = intel_context_active_acquire(ce); 160 if (ret) 161 return ret; 162 163 mock_timeline_pin(ce->ring->timeline); 164 return 0; 165 } 166 167 static const struct intel_context_ops mock_context_ops = { 168 .pin = mock_context_pin, 169 .unpin = mock_context_unpin, 170 171 .enter = intel_context_enter_engine, 172 .exit = intel_context_exit_engine, 173 174 .destroy = mock_context_destroy, 175 }; 176 177 static int mock_request_alloc(struct i915_request *request) 178 { 179 INIT_LIST_HEAD(&request->mock.link); 180 request->mock.delay = 0; 181 182 return 0; 183 } 184 185 static int mock_emit_flush(struct i915_request *request, 186 unsigned int flags) 187 { 188 return 0; 189 } 190 191 static u32 *mock_emit_breadcrumb(struct i915_request *request, u32 *cs) 192 { 193 return cs; 194 } 195 196 static void mock_submit_request(struct i915_request *request) 197 { 198 struct mock_engine *engine = 199 container_of(request->engine, typeof(*engine), base); 200 unsigned long flags; 201 202 i915_request_submit(request); 203 204 spin_lock_irqsave(&engine->hw_lock, flags); 205 list_add_tail(&request->mock.link, &engine->hw_queue); 206 if (list_is_first(&request->mock.link, &engine->hw_queue)) { 207 if (request->mock.delay) 208 mod_timer(&engine->hw_delay, 209 jiffies + request->mock.delay); 210 else 211 advance(request); 212 } 213 spin_unlock_irqrestore(&engine->hw_lock, flags); 214 } 215 216 static void mock_reset_prepare(struct intel_engine_cs *engine) 217 { 218 } 219 220 static void mock_reset(struct intel_engine_cs *engine, bool stalled) 221 { 222 GEM_BUG_ON(stalled); 223 } 224 225 static void mock_reset_finish(struct intel_engine_cs *engine) 226 { 227 } 228 229 static void mock_cancel_requests(struct intel_engine_cs *engine) 230 { 231 struct i915_request *request; 232 unsigned long flags; 233 234 spin_lock_irqsave(&engine->active.lock, flags); 235 236 /* Mark all submitted requests as skipped. */ 237 list_for_each_entry(request, &engine->active.requests, sched.link) { 238 if (!i915_request_signaled(request)) 239 dma_fence_set_error(&request->fence, -EIO); 240 241 i915_request_mark_complete(request); 242 } 243 244 spin_unlock_irqrestore(&engine->active.lock, flags); 245 } 246 247 struct intel_engine_cs *mock_engine(struct drm_i915_private *i915, 248 const char *name, 249 int id) 250 { 251 struct mock_engine *engine; 252 253 GEM_BUG_ON(id >= I915_NUM_ENGINES); 254 255 engine = kzalloc(sizeof(*engine) + PAGE_SIZE, GFP_KERNEL); 256 if (!engine) 257 return NULL; 258 259 /* minimal engine setup for requests */ 260 engine->base.i915 = i915; 261 engine->base.gt = &i915->gt; 262 snprintf(engine->base.name, sizeof(engine->base.name), "%s", name); 263 engine->base.id = id; 264 engine->base.mask = BIT(id); 265 engine->base.status_page.addr = (void *)(engine + 1); 266 267 engine->base.cops = &mock_context_ops; 268 engine->base.request_alloc = mock_request_alloc; 269 engine->base.emit_flush = mock_emit_flush; 270 engine->base.emit_fini_breadcrumb = mock_emit_breadcrumb; 271 engine->base.submit_request = mock_submit_request; 272 273 engine->base.reset.prepare = mock_reset_prepare; 274 engine->base.reset.reset = mock_reset; 275 engine->base.reset.finish = mock_reset_finish; 276 engine->base.cancel_requests = mock_cancel_requests; 277 278 /* fake hw queue */ 279 spin_lock_init(&engine->hw_lock); 280 timer_setup(&engine->hw_delay, hw_delay_complete, 0); 281 INIT_LIST_HEAD(&engine->hw_queue); 282 283 return &engine->base; 284 } 285 286 int mock_engine_init(struct intel_engine_cs *engine) 287 { 288 struct drm_i915_private *i915 = engine->i915; 289 int err; 290 291 intel_engine_init_active(engine, ENGINE_MOCK); 292 intel_engine_init_breadcrumbs(engine); 293 intel_engine_init_execlists(engine); 294 intel_engine_init__pm(engine); 295 296 engine->kernel_context = 297 i915_gem_context_get_engine(i915->kernel_context, engine->id); 298 if (IS_ERR(engine->kernel_context)) 299 goto err_breadcrumbs; 300 301 err = intel_context_pin(engine->kernel_context); 302 intel_context_put(engine->kernel_context); 303 if (err) 304 goto err_breadcrumbs; 305 306 return 0; 307 308 err_breadcrumbs: 309 intel_engine_fini_breadcrumbs(engine); 310 return -ENOMEM; 311 } 312 313 void mock_engine_flush(struct intel_engine_cs *engine) 314 { 315 struct mock_engine *mock = 316 container_of(engine, typeof(*mock), base); 317 struct i915_request *request, *rn; 318 319 del_timer_sync(&mock->hw_delay); 320 321 spin_lock_irq(&mock->hw_lock); 322 list_for_each_entry_safe(request, rn, &mock->hw_queue, mock.link) 323 advance(request); 324 spin_unlock_irq(&mock->hw_lock); 325 } 326 327 void mock_engine_reset(struct intel_engine_cs *engine) 328 { 329 } 330 331 void mock_engine_free(struct intel_engine_cs *engine) 332 { 333 struct mock_engine *mock = 334 container_of(engine, typeof(*mock), base); 335 336 GEM_BUG_ON(timer_pending(&mock->hw_delay)); 337 338 intel_context_unpin(engine->kernel_context); 339 340 intel_engine_fini_breadcrumbs(engine); 341 342 kfree(engine); 343 } 344