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