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