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 __set_bit(I915_VMA_GGTT_BIT, __i915_vma_flags(ring->vma)); 69 __set_bit(DRM_MM_NODE_ALLOCATED_BIT, &ring->vma->node.flags); 70 ring->vma->node.size = sz; 71 72 intel_ring_update_space(ring); 73 74 return ring; 75 } 76 77 static void mock_ring_free(struct intel_ring *ring) 78 { 79 i915_active_fini(&ring->vma->active); 80 i915_vma_free(ring->vma); 81 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_signal_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 } 134 135 static void mock_context_destroy(struct kref *ref) 136 { 137 struct intel_context *ce = container_of(ref, typeof(*ce), ref); 138 139 GEM_BUG_ON(intel_context_is_pinned(ce)); 140 141 if (test_bit(CONTEXT_ALLOC_BIT, &ce->flags)) { 142 mock_ring_free(ce->ring); 143 mock_timeline_unpin(ce->timeline); 144 } 145 146 intel_context_fini(ce); 147 intel_context_free(ce); 148 } 149 150 static int mock_context_alloc(struct intel_context *ce) 151 { 152 ce->ring = mock_ring(ce->engine); 153 if (!ce->ring) 154 return -ENOMEM; 155 156 GEM_BUG_ON(ce->timeline); 157 ce->timeline = intel_timeline_create(ce->engine->gt, NULL); 158 if (IS_ERR(ce->timeline)) { 159 kfree(ce->engine); 160 return PTR_ERR(ce->timeline); 161 } 162 163 mock_timeline_pin(ce->timeline); 164 165 return 0; 166 } 167 168 static int mock_context_pin(struct intel_context *ce) 169 { 170 return 0; 171 } 172 173 static void mock_context_reset(struct intel_context *ce) 174 { 175 } 176 177 static const struct intel_context_ops mock_context_ops = { 178 .alloc = mock_context_alloc, 179 180 .pin = mock_context_pin, 181 .unpin = mock_context_unpin, 182 183 .enter = intel_context_enter_engine, 184 .exit = intel_context_exit_engine, 185 186 .reset = mock_context_reset, 187 .destroy = mock_context_destroy, 188 }; 189 190 static int mock_request_alloc(struct i915_request *request) 191 { 192 INIT_LIST_HEAD(&request->mock.link); 193 request->mock.delay = 0; 194 195 return 0; 196 } 197 198 static int mock_emit_flush(struct i915_request *request, 199 unsigned int flags) 200 { 201 return 0; 202 } 203 204 static u32 *mock_emit_breadcrumb(struct i915_request *request, u32 *cs) 205 { 206 return cs; 207 } 208 209 static void mock_submit_request(struct i915_request *request) 210 { 211 struct mock_engine *engine = 212 container_of(request->engine, typeof(*engine), base); 213 unsigned long flags; 214 215 i915_request_submit(request); 216 217 spin_lock_irqsave(&engine->hw_lock, flags); 218 list_add_tail(&request->mock.link, &engine->hw_queue); 219 if (list_is_first(&request->mock.link, &engine->hw_queue)) { 220 if (request->mock.delay) 221 mod_timer(&engine->hw_delay, 222 jiffies + request->mock.delay); 223 else 224 advance(request); 225 } 226 spin_unlock_irqrestore(&engine->hw_lock, flags); 227 } 228 229 static void mock_reset_prepare(struct intel_engine_cs *engine) 230 { 231 } 232 233 static void mock_reset_rewind(struct intel_engine_cs *engine, bool stalled) 234 { 235 GEM_BUG_ON(stalled); 236 } 237 238 static void mock_reset_cancel(struct intel_engine_cs *engine) 239 { 240 struct i915_request *request; 241 unsigned long flags; 242 243 spin_lock_irqsave(&engine->active.lock, flags); 244 245 /* Mark all submitted requests as skipped. */ 246 list_for_each_entry(request, &engine->active.requests, sched.link) { 247 i915_request_set_error_once(request, -EIO); 248 i915_request_mark_complete(request); 249 } 250 251 spin_unlock_irqrestore(&engine->active.lock, flags); 252 } 253 254 static void mock_reset_finish(struct intel_engine_cs *engine) 255 { 256 } 257 258 static void mock_engine_release(struct intel_engine_cs *engine) 259 { 260 struct mock_engine *mock = 261 container_of(engine, typeof(*mock), base); 262 263 GEM_BUG_ON(timer_pending(&mock->hw_delay)); 264 265 intel_context_unpin(engine->kernel_context); 266 intel_context_put(engine->kernel_context); 267 268 intel_engine_fini_retire(engine); 269 intel_engine_fini_breadcrumbs(engine); 270 } 271 272 struct intel_engine_cs *mock_engine(struct drm_i915_private *i915, 273 const char *name, 274 int id) 275 { 276 struct mock_engine *engine; 277 278 GEM_BUG_ON(id >= I915_NUM_ENGINES); 279 GEM_BUG_ON(!i915->gt.uncore); 280 281 engine = kzalloc(sizeof(*engine) + PAGE_SIZE, GFP_KERNEL); 282 if (!engine) 283 return NULL; 284 285 /* minimal engine setup for requests */ 286 engine->base.i915 = i915; 287 engine->base.gt = &i915->gt; 288 engine->base.uncore = i915->gt.uncore; 289 snprintf(engine->base.name, sizeof(engine->base.name), "%s", name); 290 engine->base.id = id; 291 engine->base.mask = BIT(id); 292 engine->base.legacy_idx = INVALID_ENGINE; 293 engine->base.instance = id; 294 engine->base.status_page.addr = (void *)(engine + 1); 295 296 engine->base.cops = &mock_context_ops; 297 engine->base.request_alloc = mock_request_alloc; 298 engine->base.emit_flush = mock_emit_flush; 299 engine->base.emit_fini_breadcrumb = mock_emit_breadcrumb; 300 engine->base.submit_request = mock_submit_request; 301 302 engine->base.reset.prepare = mock_reset_prepare; 303 engine->base.reset.rewind = mock_reset_rewind; 304 engine->base.reset.cancel = mock_reset_cancel; 305 engine->base.reset.finish = mock_reset_finish; 306 307 engine->base.release = mock_engine_release; 308 309 i915->gt.engine[id] = &engine->base; 310 i915->gt.engine_class[0][id] = &engine->base; 311 312 /* fake hw queue */ 313 spin_lock_init(&engine->hw_lock); 314 timer_setup(&engine->hw_delay, hw_delay_complete, 0); 315 INIT_LIST_HEAD(&engine->hw_queue); 316 317 intel_engine_add_user(&engine->base); 318 319 return &engine->base; 320 } 321 322 int mock_engine_init(struct intel_engine_cs *engine) 323 { 324 struct intel_context *ce; 325 326 intel_engine_init_active(engine, ENGINE_MOCK); 327 intel_engine_init_breadcrumbs(engine); 328 intel_engine_init_execlists(engine); 329 intel_engine_init__pm(engine); 330 intel_engine_init_retire(engine); 331 intel_engine_pool_init(&engine->pool); 332 333 ce = create_kernel_context(engine); 334 if (IS_ERR(ce)) 335 goto err_breadcrumbs; 336 337 engine->kernel_context = ce; 338 return 0; 339 340 err_breadcrumbs: 341 intel_engine_fini_breadcrumbs(engine); 342 return -ENOMEM; 343 } 344 345 void mock_engine_flush(struct intel_engine_cs *engine) 346 { 347 struct mock_engine *mock = 348 container_of(engine, typeof(*mock), base); 349 struct i915_request *request, *rn; 350 351 del_timer_sync(&mock->hw_delay); 352 353 spin_lock_irq(&mock->hw_lock); 354 list_for_each_entry_safe(request, rn, &mock->hw_queue, mock.link) 355 advance(request); 356 spin_unlock_irq(&mock->hw_lock); 357 } 358 359 void mock_engine_reset(struct intel_engine_cs *engine) 360 { 361 } 362