1 /* 2 * SPDX-License-Identifier: MIT 3 * 4 * Copyright © 2011-2012 Intel Corporation 5 */ 6 7 /* 8 * This file implements HW context support. On gen5+ a HW context consists of an 9 * opaque GPU object which is referenced at times of context saves and restores. 10 * With RC6 enabled, the context is also referenced as the GPU enters and exists 11 * from RC6 (GPU has it's own internal power context, except on gen5). Though 12 * something like a context does exist for the media ring, the code only 13 * supports contexts for the render ring. 14 * 15 * In software, there is a distinction between contexts created by the user, 16 * and the default HW context. The default HW context is used by GPU clients 17 * that do not request setup of their own hardware context. The default 18 * context's state is never restored to help prevent programming errors. This 19 * would happen if a client ran and piggy-backed off another clients GPU state. 20 * The default context only exists to give the GPU some offset to load as the 21 * current to invoke a save of the context we actually care about. In fact, the 22 * code could likely be constructed, albeit in a more complicated fashion, to 23 * never use the default context, though that limits the driver's ability to 24 * swap out, and/or destroy other contexts. 25 * 26 * All other contexts are created as a request by the GPU client. These contexts 27 * store GPU state, and thus allow GPU clients to not re-emit state (and 28 * potentially query certain state) at any time. The kernel driver makes 29 * certain that the appropriate commands are inserted. 30 * 31 * The context life cycle is semi-complicated in that context BOs may live 32 * longer than the context itself because of the way the hardware, and object 33 * tracking works. Below is a very crude representation of the state machine 34 * describing the context life. 35 * refcount pincount active 36 * S0: initial state 0 0 0 37 * S1: context created 1 0 0 38 * S2: context is currently running 2 1 X 39 * S3: GPU referenced, but not current 2 0 1 40 * S4: context is current, but destroyed 1 1 0 41 * S5: like S3, but destroyed 1 0 1 42 * 43 * The most common (but not all) transitions: 44 * S0->S1: client creates a context 45 * S1->S2: client submits execbuf with context 46 * S2->S3: other clients submits execbuf with context 47 * S3->S1: context object was retired 48 * S3->S2: clients submits another execbuf 49 * S2->S4: context destroy called with current context 50 * S3->S5->S0: destroy path 51 * S4->S5->S0: destroy path on current context 52 * 53 * There are two confusing terms used above: 54 * The "current context" means the context which is currently running on the 55 * GPU. The GPU has loaded its state already and has stored away the gtt 56 * offset of the BO. The GPU is not actively referencing the data at this 57 * offset, but it will on the next context switch. The only way to avoid this 58 * is to do a GPU reset. 59 * 60 * An "active context' is one which was previously the "current context" and is 61 * on the active list waiting for the next context switch to occur. Until this 62 * happens, the object must remain at the same gtt offset. It is therefore 63 * possible to destroy a context, but it is still active. 64 * 65 */ 66 67 #include <linux/log2.h> 68 #include <linux/nospec.h> 69 70 #include "gt/gen6_ppgtt.h" 71 #include "gt/intel_context.h" 72 #include "gt/intel_context_param.h" 73 #include "gt/intel_engine_heartbeat.h" 74 #include "gt/intel_engine_user.h" 75 #include "gt/intel_ring.h" 76 77 #include "i915_gem_context.h" 78 #include "i915_globals.h" 79 #include "i915_trace.h" 80 #include "i915_user_extensions.h" 81 82 #define ALL_L3_SLICES(dev) (1 << NUM_L3_SLICES(dev)) - 1 83 84 static struct i915_global_gem_context { 85 struct i915_global base; 86 struct kmem_cache *slab_luts; 87 } global; 88 89 struct i915_lut_handle *i915_lut_handle_alloc(void) 90 { 91 return kmem_cache_alloc(global.slab_luts, GFP_KERNEL); 92 } 93 94 void i915_lut_handle_free(struct i915_lut_handle *lut) 95 { 96 return kmem_cache_free(global.slab_luts, lut); 97 } 98 99 static void lut_close(struct i915_gem_context *ctx) 100 { 101 struct radix_tree_iter iter; 102 void __rcu **slot; 103 104 lockdep_assert_held(&ctx->mutex); 105 106 rcu_read_lock(); 107 radix_tree_for_each_slot(slot, &ctx->handles_vma, &iter, 0) { 108 struct i915_vma *vma = rcu_dereference_raw(*slot); 109 struct drm_i915_gem_object *obj = vma->obj; 110 struct i915_lut_handle *lut; 111 112 if (!kref_get_unless_zero(&obj->base.refcount)) 113 continue; 114 115 spin_lock(&obj->lut_lock); 116 list_for_each_entry(lut, &obj->lut_list, obj_link) { 117 if (lut->ctx != ctx) 118 continue; 119 120 if (lut->handle != iter.index) 121 continue; 122 123 list_del(&lut->obj_link); 124 break; 125 } 126 spin_unlock(&obj->lut_lock); 127 128 if (&lut->obj_link != &obj->lut_list) { 129 i915_lut_handle_free(lut); 130 radix_tree_iter_delete(&ctx->handles_vma, &iter, slot); 131 i915_vma_close(vma); 132 i915_gem_object_put(obj); 133 } 134 135 i915_gem_object_put(obj); 136 } 137 rcu_read_unlock(); 138 } 139 140 static struct intel_context * 141 lookup_user_engine(struct i915_gem_context *ctx, 142 unsigned long flags, 143 const struct i915_engine_class_instance *ci) 144 #define LOOKUP_USER_INDEX BIT(0) 145 { 146 int idx; 147 148 if (!!(flags & LOOKUP_USER_INDEX) != i915_gem_context_user_engines(ctx)) 149 return ERR_PTR(-EINVAL); 150 151 if (!i915_gem_context_user_engines(ctx)) { 152 struct intel_engine_cs *engine; 153 154 engine = intel_engine_lookup_user(ctx->i915, 155 ci->engine_class, 156 ci->engine_instance); 157 if (!engine) 158 return ERR_PTR(-EINVAL); 159 160 idx = engine->legacy_idx; 161 } else { 162 idx = ci->engine_instance; 163 } 164 165 return i915_gem_context_get_engine(ctx, idx); 166 } 167 168 static struct i915_address_space * 169 context_get_vm_rcu(struct i915_gem_context *ctx) 170 { 171 GEM_BUG_ON(!rcu_access_pointer(ctx->vm)); 172 173 do { 174 struct i915_address_space *vm; 175 176 /* 177 * We do not allow downgrading from full-ppgtt [to a shared 178 * global gtt], so ctx->vm cannot become NULL. 179 */ 180 vm = rcu_dereference(ctx->vm); 181 if (!kref_get_unless_zero(&vm->ref)) 182 continue; 183 184 /* 185 * This ppgtt may have be reallocated between 186 * the read and the kref, and reassigned to a third 187 * context. In order to avoid inadvertent sharing 188 * of this ppgtt with that third context (and not 189 * src), we have to confirm that we have the same 190 * ppgtt after passing through the strong memory 191 * barrier implied by a successful 192 * kref_get_unless_zero(). 193 * 194 * Once we have acquired the current ppgtt of ctx, 195 * we no longer care if it is released from ctx, as 196 * it cannot be reallocated elsewhere. 197 */ 198 199 if (vm == rcu_access_pointer(ctx->vm)) 200 return rcu_pointer_handoff(vm); 201 202 i915_vm_put(vm); 203 } while (1); 204 } 205 206 static void intel_context_set_gem(struct intel_context *ce, 207 struct i915_gem_context *ctx) 208 { 209 GEM_BUG_ON(rcu_access_pointer(ce->gem_context)); 210 RCU_INIT_POINTER(ce->gem_context, ctx); 211 212 if (!test_bit(CONTEXT_ALLOC_BIT, &ce->flags)) 213 ce->ring = __intel_context_ring_size(SZ_16K); 214 215 if (rcu_access_pointer(ctx->vm)) { 216 struct i915_address_space *vm; 217 218 rcu_read_lock(); 219 vm = context_get_vm_rcu(ctx); /* hmm */ 220 rcu_read_unlock(); 221 222 i915_vm_put(ce->vm); 223 ce->vm = vm; 224 } 225 226 GEM_BUG_ON(ce->timeline); 227 if (ctx->timeline) 228 ce->timeline = intel_timeline_get(ctx->timeline); 229 230 if (ctx->sched.priority >= I915_PRIORITY_NORMAL && 231 intel_engine_has_timeslices(ce->engine)) 232 __set_bit(CONTEXT_USE_SEMAPHORES, &ce->flags); 233 } 234 235 static void __free_engines(struct i915_gem_engines *e, unsigned int count) 236 { 237 while (count--) { 238 if (!e->engines[count]) 239 continue; 240 241 intel_context_put(e->engines[count]); 242 } 243 kfree(e); 244 } 245 246 static void free_engines(struct i915_gem_engines *e) 247 { 248 __free_engines(e, e->num_engines); 249 } 250 251 static void free_engines_rcu(struct rcu_head *rcu) 252 { 253 struct i915_gem_engines *engines = 254 container_of(rcu, struct i915_gem_engines, rcu); 255 256 i915_sw_fence_fini(&engines->fence); 257 free_engines(engines); 258 } 259 260 static int __i915_sw_fence_call 261 engines_notify(struct i915_sw_fence *fence, enum i915_sw_fence_notify state) 262 { 263 struct i915_gem_engines *engines = 264 container_of(fence, typeof(*engines), fence); 265 266 switch (state) { 267 case FENCE_COMPLETE: 268 if (!list_empty(&engines->link)) { 269 struct i915_gem_context *ctx = engines->ctx; 270 unsigned long flags; 271 272 spin_lock_irqsave(&ctx->stale.lock, flags); 273 list_del(&engines->link); 274 spin_unlock_irqrestore(&ctx->stale.lock, flags); 275 } 276 i915_gem_context_put(engines->ctx); 277 break; 278 279 case FENCE_FREE: 280 init_rcu_head(&engines->rcu); 281 call_rcu(&engines->rcu, free_engines_rcu); 282 break; 283 } 284 285 return NOTIFY_DONE; 286 } 287 288 static struct i915_gem_engines *alloc_engines(unsigned int count) 289 { 290 struct i915_gem_engines *e; 291 292 e = kzalloc(struct_size(e, engines, count), GFP_KERNEL); 293 if (!e) 294 return NULL; 295 296 i915_sw_fence_init(&e->fence, engines_notify); 297 return e; 298 } 299 300 static struct i915_gem_engines *default_engines(struct i915_gem_context *ctx) 301 { 302 const struct intel_gt *gt = &ctx->i915->gt; 303 struct intel_engine_cs *engine; 304 struct i915_gem_engines *e; 305 enum intel_engine_id id; 306 307 e = alloc_engines(I915_NUM_ENGINES); 308 if (!e) 309 return ERR_PTR(-ENOMEM); 310 311 for_each_engine(engine, gt, id) { 312 struct intel_context *ce; 313 314 if (engine->legacy_idx == INVALID_ENGINE) 315 continue; 316 317 GEM_BUG_ON(engine->legacy_idx >= I915_NUM_ENGINES); 318 GEM_BUG_ON(e->engines[engine->legacy_idx]); 319 320 ce = intel_context_create(engine); 321 if (IS_ERR(ce)) { 322 __free_engines(e, e->num_engines + 1); 323 return ERR_CAST(ce); 324 } 325 326 intel_context_set_gem(ce, ctx); 327 328 e->engines[engine->legacy_idx] = ce; 329 e->num_engines = max(e->num_engines, engine->legacy_idx); 330 } 331 e->num_engines++; 332 333 return e; 334 } 335 336 static void i915_gem_context_free(struct i915_gem_context *ctx) 337 { 338 GEM_BUG_ON(!i915_gem_context_is_closed(ctx)); 339 340 spin_lock(&ctx->i915->gem.contexts.lock); 341 list_del(&ctx->link); 342 spin_unlock(&ctx->i915->gem.contexts.lock); 343 344 mutex_destroy(&ctx->engines_mutex); 345 346 if (ctx->timeline) 347 intel_timeline_put(ctx->timeline); 348 349 put_pid(ctx->pid); 350 mutex_destroy(&ctx->mutex); 351 352 kfree_rcu(ctx, rcu); 353 } 354 355 static void contexts_free_all(struct llist_node *list) 356 { 357 struct i915_gem_context *ctx, *cn; 358 359 llist_for_each_entry_safe(ctx, cn, list, free_link) 360 i915_gem_context_free(ctx); 361 } 362 363 static void contexts_flush_free(struct i915_gem_contexts *gc) 364 { 365 contexts_free_all(llist_del_all(&gc->free_list)); 366 } 367 368 static void contexts_free_worker(struct work_struct *work) 369 { 370 struct i915_gem_contexts *gc = 371 container_of(work, typeof(*gc), free_work); 372 373 contexts_flush_free(gc); 374 } 375 376 void i915_gem_context_release(struct kref *ref) 377 { 378 struct i915_gem_context *ctx = container_of(ref, typeof(*ctx), ref); 379 struct i915_gem_contexts *gc = &ctx->i915->gem.contexts; 380 381 trace_i915_context_free(ctx); 382 if (llist_add(&ctx->free_link, &gc->free_list)) 383 schedule_work(&gc->free_work); 384 } 385 386 static inline struct i915_gem_engines * 387 __context_engines_static(const struct i915_gem_context *ctx) 388 { 389 return rcu_dereference_protected(ctx->engines, true); 390 } 391 392 static bool __reset_engine(struct intel_engine_cs *engine) 393 { 394 struct intel_gt *gt = engine->gt; 395 bool success = false; 396 397 if (!intel_has_reset_engine(gt)) 398 return false; 399 400 if (!test_and_set_bit(I915_RESET_ENGINE + engine->id, 401 >->reset.flags)) { 402 success = intel_engine_reset(engine, NULL) == 0; 403 clear_and_wake_up_bit(I915_RESET_ENGINE + engine->id, 404 >->reset.flags); 405 } 406 407 return success; 408 } 409 410 static void __reset_context(struct i915_gem_context *ctx, 411 struct intel_engine_cs *engine) 412 { 413 intel_gt_handle_error(engine->gt, engine->mask, 0, 414 "context closure in %s", ctx->name); 415 } 416 417 static bool __cancel_engine(struct intel_engine_cs *engine) 418 { 419 /* 420 * Send a "high priority pulse" down the engine to cause the 421 * current request to be momentarily preempted. (If it fails to 422 * be preempted, it will be reset). As we have marked our context 423 * as banned, any incomplete request, including any running, will 424 * be skipped following the preemption. 425 * 426 * If there is no hangchecking (one of the reasons why we try to 427 * cancel the context) and no forced preemption, there may be no 428 * means by which we reset the GPU and evict the persistent hog. 429 * Ergo if we are unable to inject a preemptive pulse that can 430 * kill the banned context, we fallback to doing a local reset 431 * instead. 432 */ 433 if (IS_ACTIVE(CONFIG_DRM_I915_PREEMPT_TIMEOUT) && 434 !intel_engine_pulse(engine)) 435 return true; 436 437 /* If we are unable to send a pulse, try resetting this engine. */ 438 return __reset_engine(engine); 439 } 440 441 static struct intel_engine_cs *__active_engine(struct i915_request *rq) 442 { 443 struct intel_engine_cs *engine, *locked; 444 445 /* 446 * Serialise with __i915_request_submit() so that it sees 447 * is-banned?, or we know the request is already inflight. 448 */ 449 locked = READ_ONCE(rq->engine); 450 spin_lock_irq(&locked->active.lock); 451 while (unlikely(locked != (engine = READ_ONCE(rq->engine)))) { 452 spin_unlock(&locked->active.lock); 453 spin_lock(&engine->active.lock); 454 locked = engine; 455 } 456 457 engine = NULL; 458 if (i915_request_is_active(rq) && rq->fence.error != -EIO) 459 engine = rq->engine; 460 461 spin_unlock_irq(&locked->active.lock); 462 463 return engine; 464 } 465 466 static struct intel_engine_cs *active_engine(struct intel_context *ce) 467 { 468 struct intel_engine_cs *engine = NULL; 469 struct i915_request *rq; 470 471 if (!ce->timeline) 472 return NULL; 473 474 mutex_lock(&ce->timeline->mutex); 475 list_for_each_entry_reverse(rq, &ce->timeline->requests, link) { 476 if (i915_request_completed(rq)) 477 break; 478 479 /* Check with the backend if the request is inflight */ 480 engine = __active_engine(rq); 481 if (engine) 482 break; 483 } 484 mutex_unlock(&ce->timeline->mutex); 485 486 return engine; 487 } 488 489 static void kill_engines(struct i915_gem_engines *engines) 490 { 491 struct i915_gem_engines_iter it; 492 struct intel_context *ce; 493 494 /* 495 * Map the user's engine back to the actual engines; one virtual 496 * engine will be mapped to multiple engines, and using ctx->engine[] 497 * the same engine may be have multiple instances in the user's map. 498 * However, we only care about pending requests, so only include 499 * engines on which there are incomplete requests. 500 */ 501 for_each_gem_engine(ce, engines, it) { 502 struct intel_engine_cs *engine; 503 504 if (intel_context_set_banned(ce)) 505 continue; 506 507 /* 508 * Check the current active state of this context; if we 509 * are currently executing on the GPU we need to evict 510 * ourselves. On the other hand, if we haven't yet been 511 * submitted to the GPU or if everything is complete, 512 * we have nothing to do. 513 */ 514 engine = active_engine(ce); 515 516 /* First attempt to gracefully cancel the context */ 517 if (engine && !__cancel_engine(engine)) 518 /* 519 * If we are unable to send a preemptive pulse to bump 520 * the context from the GPU, we have to resort to a full 521 * reset. We hope the collateral damage is worth it. 522 */ 523 __reset_context(engines->ctx, engine); 524 } 525 } 526 527 static void kill_stale_engines(struct i915_gem_context *ctx) 528 { 529 struct i915_gem_engines *pos, *next; 530 531 spin_lock_irq(&ctx->stale.lock); 532 GEM_BUG_ON(!i915_gem_context_is_closed(ctx)); 533 list_for_each_entry_safe(pos, next, &ctx->stale.engines, link) { 534 if (!i915_sw_fence_await(&pos->fence)) { 535 list_del_init(&pos->link); 536 continue; 537 } 538 539 spin_unlock_irq(&ctx->stale.lock); 540 541 kill_engines(pos); 542 543 spin_lock_irq(&ctx->stale.lock); 544 GEM_BUG_ON(i915_sw_fence_signaled(&pos->fence)); 545 list_safe_reset_next(pos, next, link); 546 list_del_init(&pos->link); /* decouple from FENCE_COMPLETE */ 547 548 i915_sw_fence_complete(&pos->fence); 549 } 550 spin_unlock_irq(&ctx->stale.lock); 551 } 552 553 static void kill_context(struct i915_gem_context *ctx) 554 { 555 kill_stale_engines(ctx); 556 } 557 558 static void engines_idle_release(struct i915_gem_context *ctx, 559 struct i915_gem_engines *engines) 560 { 561 struct i915_gem_engines_iter it; 562 struct intel_context *ce; 563 564 INIT_LIST_HEAD(&engines->link); 565 566 engines->ctx = i915_gem_context_get(ctx); 567 568 for_each_gem_engine(ce, engines, it) { 569 int err; 570 571 /* serialises with execbuf */ 572 set_bit(CONTEXT_CLOSED_BIT, &ce->flags); 573 if (!intel_context_pin_if_active(ce)) 574 continue; 575 576 /* Wait until context is finally scheduled out and retired */ 577 err = i915_sw_fence_await_active(&engines->fence, 578 &ce->active, 579 I915_ACTIVE_AWAIT_BARRIER); 580 intel_context_unpin(ce); 581 if (err) 582 goto kill; 583 } 584 585 spin_lock_irq(&ctx->stale.lock); 586 if (!i915_gem_context_is_closed(ctx)) 587 list_add_tail(&engines->link, &ctx->stale.engines); 588 spin_unlock_irq(&ctx->stale.lock); 589 590 kill: 591 if (list_empty(&engines->link)) /* raced, already closed */ 592 kill_engines(engines); 593 594 i915_sw_fence_commit(&engines->fence); 595 } 596 597 static void set_closed_name(struct i915_gem_context *ctx) 598 { 599 char *s; 600 601 /* Replace '[]' with '<>' to indicate closed in debug prints */ 602 603 s = strrchr(ctx->name, '['); 604 if (!s) 605 return; 606 607 *s = '<'; 608 609 s = strchr(s + 1, ']'); 610 if (s) 611 *s = '>'; 612 } 613 614 static void context_close(struct i915_gem_context *ctx) 615 { 616 struct i915_address_space *vm; 617 618 /* Flush any concurrent set_engines() */ 619 mutex_lock(&ctx->engines_mutex); 620 engines_idle_release(ctx, rcu_replace_pointer(ctx->engines, NULL, 1)); 621 i915_gem_context_set_closed(ctx); 622 mutex_unlock(&ctx->engines_mutex); 623 624 mutex_lock(&ctx->mutex); 625 626 set_closed_name(ctx); 627 628 vm = i915_gem_context_vm(ctx); 629 if (vm) 630 i915_vm_close(vm); 631 632 ctx->file_priv = ERR_PTR(-EBADF); 633 634 /* 635 * The LUT uses the VMA as a backpointer to unref the object, 636 * so we need to clear the LUT before we close all the VMA (inside 637 * the ppgtt). 638 */ 639 lut_close(ctx); 640 641 mutex_unlock(&ctx->mutex); 642 643 /* 644 * If the user has disabled hangchecking, we can not be sure that 645 * the batches will ever complete after the context is closed, 646 * keeping the context and all resources pinned forever. So in this 647 * case we opt to forcibly kill off all remaining requests on 648 * context close. 649 */ 650 if (!i915_gem_context_is_persistent(ctx) || 651 !ctx->i915->params.enable_hangcheck) 652 kill_context(ctx); 653 654 i915_gem_context_put(ctx); 655 } 656 657 static int __context_set_persistence(struct i915_gem_context *ctx, bool state) 658 { 659 if (i915_gem_context_is_persistent(ctx) == state) 660 return 0; 661 662 if (state) { 663 /* 664 * Only contexts that are short-lived [that will expire or be 665 * reset] are allowed to survive past termination. We require 666 * hangcheck to ensure that the persistent requests are healthy. 667 */ 668 if (!ctx->i915->params.enable_hangcheck) 669 return -EINVAL; 670 671 i915_gem_context_set_persistence(ctx); 672 } else { 673 /* To cancel a context we use "preempt-to-idle" */ 674 if (!(ctx->i915->caps.scheduler & I915_SCHEDULER_CAP_PREEMPTION)) 675 return -ENODEV; 676 677 /* 678 * If the cancel fails, we then need to reset, cleanly! 679 * 680 * If the per-engine reset fails, all hope is lost! We resort 681 * to a full GPU reset in that unlikely case, but realistically 682 * if the engine could not reset, the full reset does not fare 683 * much better. The damage has been done. 684 * 685 * However, if we cannot reset an engine by itself, we cannot 686 * cleanup a hanging persistent context without causing 687 * colateral damage, and we should not pretend we can by 688 * exposing the interface. 689 */ 690 if (!intel_has_reset_engine(&ctx->i915->gt)) 691 return -ENODEV; 692 693 i915_gem_context_clear_persistence(ctx); 694 } 695 696 return 0; 697 } 698 699 static struct i915_gem_context * 700 __create_context(struct drm_i915_private *i915) 701 { 702 struct i915_gem_context *ctx; 703 struct i915_gem_engines *e; 704 int err; 705 int i; 706 707 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 708 if (!ctx) 709 return ERR_PTR(-ENOMEM); 710 711 kref_init(&ctx->ref); 712 ctx->i915 = i915; 713 ctx->sched.priority = I915_USER_PRIORITY(I915_PRIORITY_NORMAL); 714 mutex_init(&ctx->mutex); 715 716 spin_lock_init(&ctx->stale.lock); 717 INIT_LIST_HEAD(&ctx->stale.engines); 718 719 mutex_init(&ctx->engines_mutex); 720 e = default_engines(ctx); 721 if (IS_ERR(e)) { 722 err = PTR_ERR(e); 723 goto err_free; 724 } 725 RCU_INIT_POINTER(ctx->engines, e); 726 727 INIT_RADIX_TREE(&ctx->handles_vma, GFP_KERNEL); 728 729 /* NB: Mark all slices as needing a remap so that when the context first 730 * loads it will restore whatever remap state already exists. If there 731 * is no remap info, it will be a NOP. */ 732 ctx->remap_slice = ALL_L3_SLICES(i915); 733 734 i915_gem_context_set_bannable(ctx); 735 i915_gem_context_set_recoverable(ctx); 736 __context_set_persistence(ctx, true /* cgroup hook? */); 737 738 for (i = 0; i < ARRAY_SIZE(ctx->hang_timestamp); i++) 739 ctx->hang_timestamp[i] = jiffies - CONTEXT_FAST_HANG_JIFFIES; 740 741 spin_lock(&i915->gem.contexts.lock); 742 list_add_tail(&ctx->link, &i915->gem.contexts.list); 743 spin_unlock(&i915->gem.contexts.lock); 744 745 return ctx; 746 747 err_free: 748 kfree(ctx); 749 return ERR_PTR(err); 750 } 751 752 static inline struct i915_gem_engines * 753 __context_engines_await(const struct i915_gem_context *ctx) 754 { 755 struct i915_gem_engines *engines; 756 757 rcu_read_lock(); 758 do { 759 engines = rcu_dereference(ctx->engines); 760 GEM_BUG_ON(!engines); 761 762 if (unlikely(!i915_sw_fence_await(&engines->fence))) 763 continue; 764 765 if (likely(engines == rcu_access_pointer(ctx->engines))) 766 break; 767 768 i915_sw_fence_complete(&engines->fence); 769 } while (1); 770 rcu_read_unlock(); 771 772 return engines; 773 } 774 775 static int 776 context_apply_all(struct i915_gem_context *ctx, 777 int (*fn)(struct intel_context *ce, void *data), 778 void *data) 779 { 780 struct i915_gem_engines_iter it; 781 struct i915_gem_engines *e; 782 struct intel_context *ce; 783 int err = 0; 784 785 e = __context_engines_await(ctx); 786 for_each_gem_engine(ce, e, it) { 787 err = fn(ce, data); 788 if (err) 789 break; 790 } 791 i915_sw_fence_complete(&e->fence); 792 793 return err; 794 } 795 796 static int __apply_ppgtt(struct intel_context *ce, void *vm) 797 { 798 i915_vm_put(ce->vm); 799 ce->vm = i915_vm_get(vm); 800 return 0; 801 } 802 803 static struct i915_address_space * 804 __set_ppgtt(struct i915_gem_context *ctx, struct i915_address_space *vm) 805 { 806 struct i915_address_space *old; 807 808 old = rcu_replace_pointer(ctx->vm, 809 i915_vm_open(vm), 810 lockdep_is_held(&ctx->mutex)); 811 GEM_BUG_ON(old && i915_vm_is_4lvl(vm) != i915_vm_is_4lvl(old)); 812 813 context_apply_all(ctx, __apply_ppgtt, vm); 814 815 return old; 816 } 817 818 static void __assign_ppgtt(struct i915_gem_context *ctx, 819 struct i915_address_space *vm) 820 { 821 if (vm == rcu_access_pointer(ctx->vm)) 822 return; 823 824 vm = __set_ppgtt(ctx, vm); 825 if (vm) 826 i915_vm_close(vm); 827 } 828 829 static void __set_timeline(struct intel_timeline **dst, 830 struct intel_timeline *src) 831 { 832 struct intel_timeline *old = *dst; 833 834 *dst = src ? intel_timeline_get(src) : NULL; 835 836 if (old) 837 intel_timeline_put(old); 838 } 839 840 static int __apply_timeline(struct intel_context *ce, void *timeline) 841 { 842 __set_timeline(&ce->timeline, timeline); 843 return 0; 844 } 845 846 static void __assign_timeline(struct i915_gem_context *ctx, 847 struct intel_timeline *timeline) 848 { 849 __set_timeline(&ctx->timeline, timeline); 850 context_apply_all(ctx, __apply_timeline, timeline); 851 } 852 853 static struct i915_gem_context * 854 i915_gem_create_context(struct drm_i915_private *i915, unsigned int flags) 855 { 856 struct i915_gem_context *ctx; 857 858 if (flags & I915_CONTEXT_CREATE_FLAGS_SINGLE_TIMELINE && 859 !HAS_EXECLISTS(i915)) 860 return ERR_PTR(-EINVAL); 861 862 /* Reap the stale contexts */ 863 contexts_flush_free(&i915->gem.contexts); 864 865 ctx = __create_context(i915); 866 if (IS_ERR(ctx)) 867 return ctx; 868 869 if (HAS_FULL_PPGTT(i915)) { 870 struct i915_ppgtt *ppgtt; 871 872 ppgtt = i915_ppgtt_create(&i915->gt); 873 if (IS_ERR(ppgtt)) { 874 drm_dbg(&i915->drm, "PPGTT setup failed (%ld)\n", 875 PTR_ERR(ppgtt)); 876 context_close(ctx); 877 return ERR_CAST(ppgtt); 878 } 879 880 mutex_lock(&ctx->mutex); 881 __assign_ppgtt(ctx, &ppgtt->vm); 882 mutex_unlock(&ctx->mutex); 883 884 i915_vm_put(&ppgtt->vm); 885 } 886 887 if (flags & I915_CONTEXT_CREATE_FLAGS_SINGLE_TIMELINE) { 888 struct intel_timeline *timeline; 889 890 timeline = intel_timeline_create(&i915->gt, NULL); 891 if (IS_ERR(timeline)) { 892 context_close(ctx); 893 return ERR_CAST(timeline); 894 } 895 896 __assign_timeline(ctx, timeline); 897 intel_timeline_put(timeline); 898 } 899 900 trace_i915_context_create(ctx); 901 902 return ctx; 903 } 904 905 static void init_contexts(struct i915_gem_contexts *gc) 906 { 907 spin_lock_init(&gc->lock); 908 INIT_LIST_HEAD(&gc->list); 909 910 INIT_WORK(&gc->free_work, contexts_free_worker); 911 init_llist_head(&gc->free_list); 912 } 913 914 void i915_gem_init__contexts(struct drm_i915_private *i915) 915 { 916 init_contexts(&i915->gem.contexts); 917 drm_dbg(&i915->drm, "%s context support initialized\n", 918 DRIVER_CAPS(i915)->has_logical_contexts ? 919 "logical" : "fake"); 920 } 921 922 void i915_gem_driver_release__contexts(struct drm_i915_private *i915) 923 { 924 flush_work(&i915->gem.contexts.free_work); 925 rcu_barrier(); /* and flush the left over RCU frees */ 926 } 927 928 static int gem_context_register(struct i915_gem_context *ctx, 929 struct drm_i915_file_private *fpriv, 930 u32 *id) 931 { 932 struct i915_address_space *vm; 933 int ret; 934 935 ctx->file_priv = fpriv; 936 937 mutex_lock(&ctx->mutex); 938 vm = i915_gem_context_vm(ctx); 939 if (vm) 940 WRITE_ONCE(vm->file, fpriv); /* XXX */ 941 mutex_unlock(&ctx->mutex); 942 943 ctx->pid = get_task_pid(current, PIDTYPE_PID); 944 snprintf(ctx->name, sizeof(ctx->name), "%s[%d]", 945 current->comm, pid_nr(ctx->pid)); 946 947 /* And finally expose ourselves to userspace via the idr */ 948 ret = xa_alloc(&fpriv->context_xa, id, ctx, xa_limit_32b, GFP_KERNEL); 949 if (ret) 950 put_pid(fetch_and_zero(&ctx->pid)); 951 952 return ret; 953 } 954 955 int i915_gem_context_open(struct drm_i915_private *i915, 956 struct drm_file *file) 957 { 958 struct drm_i915_file_private *file_priv = file->driver_priv; 959 struct i915_gem_context *ctx; 960 int err; 961 u32 id; 962 963 xa_init_flags(&file_priv->context_xa, XA_FLAGS_ALLOC); 964 965 /* 0 reserved for invalid/unassigned ppgtt */ 966 xa_init_flags(&file_priv->vm_xa, XA_FLAGS_ALLOC1); 967 968 ctx = i915_gem_create_context(i915, 0); 969 if (IS_ERR(ctx)) { 970 err = PTR_ERR(ctx); 971 goto err; 972 } 973 974 err = gem_context_register(ctx, file_priv, &id); 975 if (err < 0) 976 goto err_ctx; 977 978 GEM_BUG_ON(id); 979 return 0; 980 981 err_ctx: 982 context_close(ctx); 983 err: 984 xa_destroy(&file_priv->vm_xa); 985 xa_destroy(&file_priv->context_xa); 986 return err; 987 } 988 989 void i915_gem_context_close(struct drm_file *file) 990 { 991 struct drm_i915_file_private *file_priv = file->driver_priv; 992 struct drm_i915_private *i915 = file_priv->dev_priv; 993 struct i915_address_space *vm; 994 struct i915_gem_context *ctx; 995 unsigned long idx; 996 997 xa_for_each(&file_priv->context_xa, idx, ctx) 998 context_close(ctx); 999 xa_destroy(&file_priv->context_xa); 1000 1001 xa_for_each(&file_priv->vm_xa, idx, vm) 1002 i915_vm_put(vm); 1003 xa_destroy(&file_priv->vm_xa); 1004 1005 contexts_flush_free(&i915->gem.contexts); 1006 } 1007 1008 int i915_gem_vm_create_ioctl(struct drm_device *dev, void *data, 1009 struct drm_file *file) 1010 { 1011 struct drm_i915_private *i915 = to_i915(dev); 1012 struct drm_i915_gem_vm_control *args = data; 1013 struct drm_i915_file_private *file_priv = file->driver_priv; 1014 struct i915_ppgtt *ppgtt; 1015 u32 id; 1016 int err; 1017 1018 if (!HAS_FULL_PPGTT(i915)) 1019 return -ENODEV; 1020 1021 if (args->flags) 1022 return -EINVAL; 1023 1024 ppgtt = i915_ppgtt_create(&i915->gt); 1025 if (IS_ERR(ppgtt)) 1026 return PTR_ERR(ppgtt); 1027 1028 ppgtt->vm.file = file_priv; 1029 1030 if (args->extensions) { 1031 err = i915_user_extensions(u64_to_user_ptr(args->extensions), 1032 NULL, 0, 1033 ppgtt); 1034 if (err) 1035 goto err_put; 1036 } 1037 1038 err = xa_alloc(&file_priv->vm_xa, &id, &ppgtt->vm, 1039 xa_limit_32b, GFP_KERNEL); 1040 if (err) 1041 goto err_put; 1042 1043 GEM_BUG_ON(id == 0); /* reserved for invalid/unassigned ppgtt */ 1044 args->vm_id = id; 1045 return 0; 1046 1047 err_put: 1048 i915_vm_put(&ppgtt->vm); 1049 return err; 1050 } 1051 1052 int i915_gem_vm_destroy_ioctl(struct drm_device *dev, void *data, 1053 struct drm_file *file) 1054 { 1055 struct drm_i915_file_private *file_priv = file->driver_priv; 1056 struct drm_i915_gem_vm_control *args = data; 1057 struct i915_address_space *vm; 1058 1059 if (args->flags) 1060 return -EINVAL; 1061 1062 if (args->extensions) 1063 return -EINVAL; 1064 1065 vm = xa_erase(&file_priv->vm_xa, args->vm_id); 1066 if (!vm) 1067 return -ENOENT; 1068 1069 i915_vm_put(vm); 1070 return 0; 1071 } 1072 1073 struct context_barrier_task { 1074 struct i915_active base; 1075 void (*task)(void *data); 1076 void *data; 1077 }; 1078 1079 __i915_active_call 1080 static void cb_retire(struct i915_active *base) 1081 { 1082 struct context_barrier_task *cb = container_of(base, typeof(*cb), base); 1083 1084 if (cb->task) 1085 cb->task(cb->data); 1086 1087 i915_active_fini(&cb->base); 1088 kfree(cb); 1089 } 1090 1091 I915_SELFTEST_DECLARE(static intel_engine_mask_t context_barrier_inject_fault); 1092 static int context_barrier_task(struct i915_gem_context *ctx, 1093 intel_engine_mask_t engines, 1094 bool (*skip)(struct intel_context *ce, void *data), 1095 int (*emit)(struct i915_request *rq, void *data), 1096 void (*task)(void *data), 1097 void *data) 1098 { 1099 struct context_barrier_task *cb; 1100 struct i915_gem_engines_iter it; 1101 struct i915_gem_engines *e; 1102 struct intel_context *ce; 1103 int err = 0; 1104 1105 GEM_BUG_ON(!task); 1106 1107 cb = kmalloc(sizeof(*cb), GFP_KERNEL); 1108 if (!cb) 1109 return -ENOMEM; 1110 1111 i915_active_init(&cb->base, NULL, cb_retire); 1112 err = i915_active_acquire(&cb->base); 1113 if (err) { 1114 kfree(cb); 1115 return err; 1116 } 1117 1118 e = __context_engines_await(ctx); 1119 if (!e) { 1120 i915_active_release(&cb->base); 1121 return -ENOENT; 1122 } 1123 1124 for_each_gem_engine(ce, e, it) { 1125 struct i915_request *rq; 1126 1127 if (I915_SELFTEST_ONLY(context_barrier_inject_fault & 1128 ce->engine->mask)) { 1129 err = -ENXIO; 1130 break; 1131 } 1132 1133 if (!(ce->engine->mask & engines)) 1134 continue; 1135 1136 if (skip && skip(ce, data)) 1137 continue; 1138 1139 rq = intel_context_create_request(ce); 1140 if (IS_ERR(rq)) { 1141 err = PTR_ERR(rq); 1142 break; 1143 } 1144 1145 err = 0; 1146 if (emit) 1147 err = emit(rq, data); 1148 if (err == 0) 1149 err = i915_active_add_request(&cb->base, rq); 1150 1151 i915_request_add(rq); 1152 if (err) 1153 break; 1154 } 1155 i915_sw_fence_complete(&e->fence); 1156 1157 cb->task = err ? NULL : task; /* caller needs to unwind instead */ 1158 cb->data = data; 1159 1160 i915_active_release(&cb->base); 1161 1162 return err; 1163 } 1164 1165 static int get_ppgtt(struct drm_i915_file_private *file_priv, 1166 struct i915_gem_context *ctx, 1167 struct drm_i915_gem_context_param *args) 1168 { 1169 struct i915_address_space *vm; 1170 int err; 1171 u32 id; 1172 1173 if (!rcu_access_pointer(ctx->vm)) 1174 return -ENODEV; 1175 1176 rcu_read_lock(); 1177 vm = context_get_vm_rcu(ctx); 1178 rcu_read_unlock(); 1179 if (!vm) 1180 return -ENODEV; 1181 1182 err = xa_alloc(&file_priv->vm_xa, &id, vm, xa_limit_32b, GFP_KERNEL); 1183 if (err) 1184 goto err_put; 1185 1186 i915_vm_open(vm); 1187 1188 GEM_BUG_ON(id == 0); /* reserved for invalid/unassigned ppgtt */ 1189 args->value = id; 1190 args->size = 0; 1191 1192 err_put: 1193 i915_vm_put(vm); 1194 return err; 1195 } 1196 1197 static void set_ppgtt_barrier(void *data) 1198 { 1199 struct i915_address_space *old = data; 1200 1201 if (INTEL_GEN(old->i915) < 8) 1202 gen6_ppgtt_unpin_all(i915_vm_to_ppgtt(old)); 1203 1204 i915_vm_close(old); 1205 } 1206 1207 static int emit_ppgtt_update(struct i915_request *rq, void *data) 1208 { 1209 struct i915_address_space *vm = rq->context->vm; 1210 struct intel_engine_cs *engine = rq->engine; 1211 u32 base = engine->mmio_base; 1212 u32 *cs; 1213 int i; 1214 1215 if (i915_vm_is_4lvl(vm)) { 1216 struct i915_ppgtt *ppgtt = i915_vm_to_ppgtt(vm); 1217 const dma_addr_t pd_daddr = px_dma(ppgtt->pd); 1218 1219 cs = intel_ring_begin(rq, 6); 1220 if (IS_ERR(cs)) 1221 return PTR_ERR(cs); 1222 1223 *cs++ = MI_LOAD_REGISTER_IMM(2); 1224 1225 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_UDW(base, 0)); 1226 *cs++ = upper_32_bits(pd_daddr); 1227 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_LDW(base, 0)); 1228 *cs++ = lower_32_bits(pd_daddr); 1229 1230 *cs++ = MI_NOOP; 1231 intel_ring_advance(rq, cs); 1232 } else if (HAS_LOGICAL_RING_CONTEXTS(engine->i915)) { 1233 struct i915_ppgtt *ppgtt = i915_vm_to_ppgtt(vm); 1234 int err; 1235 1236 /* Magic required to prevent forcewake errors! */ 1237 err = engine->emit_flush(rq, EMIT_INVALIDATE); 1238 if (err) 1239 return err; 1240 1241 cs = intel_ring_begin(rq, 4 * GEN8_3LVL_PDPES + 2); 1242 if (IS_ERR(cs)) 1243 return PTR_ERR(cs); 1244 1245 *cs++ = MI_LOAD_REGISTER_IMM(2 * GEN8_3LVL_PDPES) | MI_LRI_FORCE_POSTED; 1246 for (i = GEN8_3LVL_PDPES; i--; ) { 1247 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i); 1248 1249 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_UDW(base, i)); 1250 *cs++ = upper_32_bits(pd_daddr); 1251 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_LDW(base, i)); 1252 *cs++ = lower_32_bits(pd_daddr); 1253 } 1254 *cs++ = MI_NOOP; 1255 intel_ring_advance(rq, cs); 1256 } 1257 1258 return 0; 1259 } 1260 1261 static bool skip_ppgtt_update(struct intel_context *ce, void *data) 1262 { 1263 if (!test_bit(CONTEXT_ALLOC_BIT, &ce->flags)) 1264 return true; 1265 1266 if (HAS_LOGICAL_RING_CONTEXTS(ce->engine->i915)) 1267 return false; 1268 1269 if (!atomic_read(&ce->pin_count)) 1270 return true; 1271 1272 /* ppGTT is not part of the legacy context image */ 1273 if (gen6_ppgtt_pin(i915_vm_to_ppgtt(ce->vm))) 1274 return true; 1275 1276 return false; 1277 } 1278 1279 static int set_ppgtt(struct drm_i915_file_private *file_priv, 1280 struct i915_gem_context *ctx, 1281 struct drm_i915_gem_context_param *args) 1282 { 1283 struct i915_address_space *vm, *old; 1284 int err; 1285 1286 if (args->size) 1287 return -EINVAL; 1288 1289 if (!rcu_access_pointer(ctx->vm)) 1290 return -ENODEV; 1291 1292 if (upper_32_bits(args->value)) 1293 return -ENOENT; 1294 1295 rcu_read_lock(); 1296 vm = xa_load(&file_priv->vm_xa, args->value); 1297 if (vm && !kref_get_unless_zero(&vm->ref)) 1298 vm = NULL; 1299 rcu_read_unlock(); 1300 if (!vm) 1301 return -ENOENT; 1302 1303 err = mutex_lock_interruptible(&ctx->mutex); 1304 if (err) 1305 goto out; 1306 1307 if (i915_gem_context_is_closed(ctx)) { 1308 err = -ENOENT; 1309 goto unlock; 1310 } 1311 1312 if (vm == rcu_access_pointer(ctx->vm)) 1313 goto unlock; 1314 1315 /* Teardown the existing obj:vma cache, it will have to be rebuilt. */ 1316 lut_close(ctx); 1317 1318 old = __set_ppgtt(ctx, vm); 1319 1320 /* 1321 * We need to flush any requests using the current ppgtt before 1322 * we release it as the requests do not hold a reference themselves, 1323 * only indirectly through the context. 1324 */ 1325 err = context_barrier_task(ctx, ALL_ENGINES, 1326 skip_ppgtt_update, 1327 emit_ppgtt_update, 1328 set_ppgtt_barrier, 1329 old); 1330 if (err) { 1331 i915_vm_close(__set_ppgtt(ctx, old)); 1332 i915_vm_close(old); 1333 } 1334 1335 unlock: 1336 mutex_unlock(&ctx->mutex); 1337 out: 1338 i915_vm_put(vm); 1339 return err; 1340 } 1341 1342 static int __apply_ringsize(struct intel_context *ce, void *sz) 1343 { 1344 return intel_context_set_ring_size(ce, (unsigned long)sz); 1345 } 1346 1347 static int set_ringsize(struct i915_gem_context *ctx, 1348 struct drm_i915_gem_context_param *args) 1349 { 1350 if (!HAS_LOGICAL_RING_CONTEXTS(ctx->i915)) 1351 return -ENODEV; 1352 1353 if (args->size) 1354 return -EINVAL; 1355 1356 if (!IS_ALIGNED(args->value, I915_GTT_PAGE_SIZE)) 1357 return -EINVAL; 1358 1359 if (args->value < I915_GTT_PAGE_SIZE) 1360 return -EINVAL; 1361 1362 if (args->value > 128 * I915_GTT_PAGE_SIZE) 1363 return -EINVAL; 1364 1365 return context_apply_all(ctx, 1366 __apply_ringsize, 1367 __intel_context_ring_size(args->value)); 1368 } 1369 1370 static int __get_ringsize(struct intel_context *ce, void *arg) 1371 { 1372 long sz; 1373 1374 sz = intel_context_get_ring_size(ce); 1375 GEM_BUG_ON(sz > INT_MAX); 1376 1377 return sz; /* stop on first engine */ 1378 } 1379 1380 static int get_ringsize(struct i915_gem_context *ctx, 1381 struct drm_i915_gem_context_param *args) 1382 { 1383 int sz; 1384 1385 if (!HAS_LOGICAL_RING_CONTEXTS(ctx->i915)) 1386 return -ENODEV; 1387 1388 if (args->size) 1389 return -EINVAL; 1390 1391 sz = context_apply_all(ctx, __get_ringsize, NULL); 1392 if (sz < 0) 1393 return sz; 1394 1395 args->value = sz; 1396 return 0; 1397 } 1398 1399 int 1400 i915_gem_user_to_context_sseu(struct drm_i915_private *i915, 1401 const struct drm_i915_gem_context_param_sseu *user, 1402 struct intel_sseu *context) 1403 { 1404 const struct sseu_dev_info *device = &RUNTIME_INFO(i915)->sseu; 1405 1406 /* No zeros in any field. */ 1407 if (!user->slice_mask || !user->subslice_mask || 1408 !user->min_eus_per_subslice || !user->max_eus_per_subslice) 1409 return -EINVAL; 1410 1411 /* Max > min. */ 1412 if (user->max_eus_per_subslice < user->min_eus_per_subslice) 1413 return -EINVAL; 1414 1415 /* 1416 * Some future proofing on the types since the uAPI is wider than the 1417 * current internal implementation. 1418 */ 1419 if (overflows_type(user->slice_mask, context->slice_mask) || 1420 overflows_type(user->subslice_mask, context->subslice_mask) || 1421 overflows_type(user->min_eus_per_subslice, 1422 context->min_eus_per_subslice) || 1423 overflows_type(user->max_eus_per_subslice, 1424 context->max_eus_per_subslice)) 1425 return -EINVAL; 1426 1427 /* Check validity against hardware. */ 1428 if (user->slice_mask & ~device->slice_mask) 1429 return -EINVAL; 1430 1431 if (user->subslice_mask & ~device->subslice_mask[0]) 1432 return -EINVAL; 1433 1434 if (user->max_eus_per_subslice > device->max_eus_per_subslice) 1435 return -EINVAL; 1436 1437 context->slice_mask = user->slice_mask; 1438 context->subslice_mask = user->subslice_mask; 1439 context->min_eus_per_subslice = user->min_eus_per_subslice; 1440 context->max_eus_per_subslice = user->max_eus_per_subslice; 1441 1442 /* Part specific restrictions. */ 1443 if (IS_GEN(i915, 11)) { 1444 unsigned int hw_s = hweight8(device->slice_mask); 1445 unsigned int hw_ss_per_s = hweight8(device->subslice_mask[0]); 1446 unsigned int req_s = hweight8(context->slice_mask); 1447 unsigned int req_ss = hweight8(context->subslice_mask); 1448 1449 /* 1450 * Only full subslice enablement is possible if more than one 1451 * slice is turned on. 1452 */ 1453 if (req_s > 1 && req_ss != hw_ss_per_s) 1454 return -EINVAL; 1455 1456 /* 1457 * If more than four (SScount bitfield limit) subslices are 1458 * requested then the number has to be even. 1459 */ 1460 if (req_ss > 4 && (req_ss & 1)) 1461 return -EINVAL; 1462 1463 /* 1464 * If only one slice is enabled and subslice count is below the 1465 * device full enablement, it must be at most half of the all 1466 * available subslices. 1467 */ 1468 if (req_s == 1 && req_ss < hw_ss_per_s && 1469 req_ss > (hw_ss_per_s / 2)) 1470 return -EINVAL; 1471 1472 /* ABI restriction - VME use case only. */ 1473 1474 /* All slices or one slice only. */ 1475 if (req_s != 1 && req_s != hw_s) 1476 return -EINVAL; 1477 1478 /* 1479 * Half subslices or full enablement only when one slice is 1480 * enabled. 1481 */ 1482 if (req_s == 1 && 1483 (req_ss != hw_ss_per_s && req_ss != (hw_ss_per_s / 2))) 1484 return -EINVAL; 1485 1486 /* No EU configuration changes. */ 1487 if ((user->min_eus_per_subslice != 1488 device->max_eus_per_subslice) || 1489 (user->max_eus_per_subslice != 1490 device->max_eus_per_subslice)) 1491 return -EINVAL; 1492 } 1493 1494 return 0; 1495 } 1496 1497 static int set_sseu(struct i915_gem_context *ctx, 1498 struct drm_i915_gem_context_param *args) 1499 { 1500 struct drm_i915_private *i915 = ctx->i915; 1501 struct drm_i915_gem_context_param_sseu user_sseu; 1502 struct intel_context *ce; 1503 struct intel_sseu sseu; 1504 unsigned long lookup; 1505 int ret; 1506 1507 if (args->size < sizeof(user_sseu)) 1508 return -EINVAL; 1509 1510 if (!IS_GEN(i915, 11)) 1511 return -ENODEV; 1512 1513 if (copy_from_user(&user_sseu, u64_to_user_ptr(args->value), 1514 sizeof(user_sseu))) 1515 return -EFAULT; 1516 1517 if (user_sseu.rsvd) 1518 return -EINVAL; 1519 1520 if (user_sseu.flags & ~(I915_CONTEXT_SSEU_FLAG_ENGINE_INDEX)) 1521 return -EINVAL; 1522 1523 lookup = 0; 1524 if (user_sseu.flags & I915_CONTEXT_SSEU_FLAG_ENGINE_INDEX) 1525 lookup |= LOOKUP_USER_INDEX; 1526 1527 ce = lookup_user_engine(ctx, lookup, &user_sseu.engine); 1528 if (IS_ERR(ce)) 1529 return PTR_ERR(ce); 1530 1531 /* Only render engine supports RPCS configuration. */ 1532 if (ce->engine->class != RENDER_CLASS) { 1533 ret = -ENODEV; 1534 goto out_ce; 1535 } 1536 1537 ret = i915_gem_user_to_context_sseu(i915, &user_sseu, &sseu); 1538 if (ret) 1539 goto out_ce; 1540 1541 ret = intel_context_reconfigure_sseu(ce, sseu); 1542 if (ret) 1543 goto out_ce; 1544 1545 args->size = sizeof(user_sseu); 1546 1547 out_ce: 1548 intel_context_put(ce); 1549 return ret; 1550 } 1551 1552 struct set_engines { 1553 struct i915_gem_context *ctx; 1554 struct i915_gem_engines *engines; 1555 }; 1556 1557 static int 1558 set_engines__load_balance(struct i915_user_extension __user *base, void *data) 1559 { 1560 struct i915_context_engines_load_balance __user *ext = 1561 container_of_user(base, typeof(*ext), base); 1562 const struct set_engines *set = data; 1563 struct drm_i915_private *i915 = set->ctx->i915; 1564 struct intel_engine_cs *stack[16]; 1565 struct intel_engine_cs **siblings; 1566 struct intel_context *ce; 1567 u16 num_siblings, idx; 1568 unsigned int n; 1569 int err; 1570 1571 if (!HAS_EXECLISTS(i915)) 1572 return -ENODEV; 1573 1574 if (intel_uc_uses_guc_submission(&i915->gt.uc)) 1575 return -ENODEV; /* not implement yet */ 1576 1577 if (get_user(idx, &ext->engine_index)) 1578 return -EFAULT; 1579 1580 if (idx >= set->engines->num_engines) { 1581 drm_dbg(&i915->drm, "Invalid placement value, %d >= %d\n", 1582 idx, set->engines->num_engines); 1583 return -EINVAL; 1584 } 1585 1586 idx = array_index_nospec(idx, set->engines->num_engines); 1587 if (set->engines->engines[idx]) { 1588 drm_dbg(&i915->drm, 1589 "Invalid placement[%d], already occupied\n", idx); 1590 return -EEXIST; 1591 } 1592 1593 if (get_user(num_siblings, &ext->num_siblings)) 1594 return -EFAULT; 1595 1596 err = check_user_mbz(&ext->flags); 1597 if (err) 1598 return err; 1599 1600 err = check_user_mbz(&ext->mbz64); 1601 if (err) 1602 return err; 1603 1604 siblings = stack; 1605 if (num_siblings > ARRAY_SIZE(stack)) { 1606 siblings = kmalloc_array(num_siblings, 1607 sizeof(*siblings), 1608 GFP_KERNEL); 1609 if (!siblings) 1610 return -ENOMEM; 1611 } 1612 1613 for (n = 0; n < num_siblings; n++) { 1614 struct i915_engine_class_instance ci; 1615 1616 if (copy_from_user(&ci, &ext->engines[n], sizeof(ci))) { 1617 err = -EFAULT; 1618 goto out_siblings; 1619 } 1620 1621 siblings[n] = intel_engine_lookup_user(i915, 1622 ci.engine_class, 1623 ci.engine_instance); 1624 if (!siblings[n]) { 1625 drm_dbg(&i915->drm, 1626 "Invalid sibling[%d]: { class:%d, inst:%d }\n", 1627 n, ci.engine_class, ci.engine_instance); 1628 err = -EINVAL; 1629 goto out_siblings; 1630 } 1631 } 1632 1633 ce = intel_execlists_create_virtual(siblings, n); 1634 if (IS_ERR(ce)) { 1635 err = PTR_ERR(ce); 1636 goto out_siblings; 1637 } 1638 1639 intel_context_set_gem(ce, set->ctx); 1640 1641 if (cmpxchg(&set->engines->engines[idx], NULL, ce)) { 1642 intel_context_put(ce); 1643 err = -EEXIST; 1644 goto out_siblings; 1645 } 1646 1647 out_siblings: 1648 if (siblings != stack) 1649 kfree(siblings); 1650 1651 return err; 1652 } 1653 1654 static int 1655 set_engines__bond(struct i915_user_extension __user *base, void *data) 1656 { 1657 struct i915_context_engines_bond __user *ext = 1658 container_of_user(base, typeof(*ext), base); 1659 const struct set_engines *set = data; 1660 struct drm_i915_private *i915 = set->ctx->i915; 1661 struct i915_engine_class_instance ci; 1662 struct intel_engine_cs *virtual; 1663 struct intel_engine_cs *master; 1664 u16 idx, num_bonds; 1665 int err, n; 1666 1667 if (get_user(idx, &ext->virtual_index)) 1668 return -EFAULT; 1669 1670 if (idx >= set->engines->num_engines) { 1671 drm_dbg(&i915->drm, 1672 "Invalid index for virtual engine: %d >= %d\n", 1673 idx, set->engines->num_engines); 1674 return -EINVAL; 1675 } 1676 1677 idx = array_index_nospec(idx, set->engines->num_engines); 1678 if (!set->engines->engines[idx]) { 1679 drm_dbg(&i915->drm, "Invalid engine at %d\n", idx); 1680 return -EINVAL; 1681 } 1682 virtual = set->engines->engines[idx]->engine; 1683 1684 err = check_user_mbz(&ext->flags); 1685 if (err) 1686 return err; 1687 1688 for (n = 0; n < ARRAY_SIZE(ext->mbz64); n++) { 1689 err = check_user_mbz(&ext->mbz64[n]); 1690 if (err) 1691 return err; 1692 } 1693 1694 if (copy_from_user(&ci, &ext->master, sizeof(ci))) 1695 return -EFAULT; 1696 1697 master = intel_engine_lookup_user(i915, 1698 ci.engine_class, ci.engine_instance); 1699 if (!master) { 1700 drm_dbg(&i915->drm, 1701 "Unrecognised master engine: { class:%u, instance:%u }\n", 1702 ci.engine_class, ci.engine_instance); 1703 return -EINVAL; 1704 } 1705 1706 if (get_user(num_bonds, &ext->num_bonds)) 1707 return -EFAULT; 1708 1709 for (n = 0; n < num_bonds; n++) { 1710 struct intel_engine_cs *bond; 1711 1712 if (copy_from_user(&ci, &ext->engines[n], sizeof(ci))) 1713 return -EFAULT; 1714 1715 bond = intel_engine_lookup_user(i915, 1716 ci.engine_class, 1717 ci.engine_instance); 1718 if (!bond) { 1719 drm_dbg(&i915->drm, 1720 "Unrecognised engine[%d] for bonding: { class:%d, instance: %d }\n", 1721 n, ci.engine_class, ci.engine_instance); 1722 return -EINVAL; 1723 } 1724 1725 /* 1726 * A non-virtual engine has no siblings to choose between; and 1727 * a submit fence will always be directed to the one engine. 1728 */ 1729 if (intel_engine_is_virtual(virtual)) { 1730 err = intel_virtual_engine_attach_bond(virtual, 1731 master, 1732 bond); 1733 if (err) 1734 return err; 1735 } 1736 } 1737 1738 return 0; 1739 } 1740 1741 static const i915_user_extension_fn set_engines__extensions[] = { 1742 [I915_CONTEXT_ENGINES_EXT_LOAD_BALANCE] = set_engines__load_balance, 1743 [I915_CONTEXT_ENGINES_EXT_BOND] = set_engines__bond, 1744 }; 1745 1746 static int 1747 set_engines(struct i915_gem_context *ctx, 1748 const struct drm_i915_gem_context_param *args) 1749 { 1750 struct drm_i915_private *i915 = ctx->i915; 1751 struct i915_context_param_engines __user *user = 1752 u64_to_user_ptr(args->value); 1753 struct set_engines set = { .ctx = ctx }; 1754 unsigned int num_engines, n; 1755 u64 extensions; 1756 int err; 1757 1758 if (!args->size) { /* switch back to legacy user_ring_map */ 1759 if (!i915_gem_context_user_engines(ctx)) 1760 return 0; 1761 1762 set.engines = default_engines(ctx); 1763 if (IS_ERR(set.engines)) 1764 return PTR_ERR(set.engines); 1765 1766 goto replace; 1767 } 1768 1769 BUILD_BUG_ON(!IS_ALIGNED(sizeof(*user), sizeof(*user->engines))); 1770 if (args->size < sizeof(*user) || 1771 !IS_ALIGNED(args->size, sizeof(*user->engines))) { 1772 drm_dbg(&i915->drm, "Invalid size for engine array: %d\n", 1773 args->size); 1774 return -EINVAL; 1775 } 1776 1777 /* 1778 * Note that I915_EXEC_RING_MASK limits execbuf to only using the 1779 * first 64 engines defined here. 1780 */ 1781 num_engines = (args->size - sizeof(*user)) / sizeof(*user->engines); 1782 set.engines = alloc_engines(num_engines); 1783 if (!set.engines) 1784 return -ENOMEM; 1785 1786 for (n = 0; n < num_engines; n++) { 1787 struct i915_engine_class_instance ci; 1788 struct intel_engine_cs *engine; 1789 struct intel_context *ce; 1790 1791 if (copy_from_user(&ci, &user->engines[n], sizeof(ci))) { 1792 __free_engines(set.engines, n); 1793 return -EFAULT; 1794 } 1795 1796 if (ci.engine_class == (u16)I915_ENGINE_CLASS_INVALID && 1797 ci.engine_instance == (u16)I915_ENGINE_CLASS_INVALID_NONE) { 1798 set.engines->engines[n] = NULL; 1799 continue; 1800 } 1801 1802 engine = intel_engine_lookup_user(ctx->i915, 1803 ci.engine_class, 1804 ci.engine_instance); 1805 if (!engine) { 1806 drm_dbg(&i915->drm, 1807 "Invalid engine[%d]: { class:%d, instance:%d }\n", 1808 n, ci.engine_class, ci.engine_instance); 1809 __free_engines(set.engines, n); 1810 return -ENOENT; 1811 } 1812 1813 ce = intel_context_create(engine); 1814 if (IS_ERR(ce)) { 1815 __free_engines(set.engines, n); 1816 return PTR_ERR(ce); 1817 } 1818 1819 intel_context_set_gem(ce, ctx); 1820 1821 set.engines->engines[n] = ce; 1822 } 1823 set.engines->num_engines = num_engines; 1824 1825 err = -EFAULT; 1826 if (!get_user(extensions, &user->extensions)) 1827 err = i915_user_extensions(u64_to_user_ptr(extensions), 1828 set_engines__extensions, 1829 ARRAY_SIZE(set_engines__extensions), 1830 &set); 1831 if (err) { 1832 free_engines(set.engines); 1833 return err; 1834 } 1835 1836 replace: 1837 mutex_lock(&ctx->engines_mutex); 1838 if (i915_gem_context_is_closed(ctx)) { 1839 mutex_unlock(&ctx->engines_mutex); 1840 free_engines(set.engines); 1841 return -ENOENT; 1842 } 1843 if (args->size) 1844 i915_gem_context_set_user_engines(ctx); 1845 else 1846 i915_gem_context_clear_user_engines(ctx); 1847 set.engines = rcu_replace_pointer(ctx->engines, set.engines, 1); 1848 mutex_unlock(&ctx->engines_mutex); 1849 1850 /* Keep track of old engine sets for kill_context() */ 1851 engines_idle_release(ctx, set.engines); 1852 1853 return 0; 1854 } 1855 1856 static struct i915_gem_engines * 1857 __copy_engines(struct i915_gem_engines *e) 1858 { 1859 struct i915_gem_engines *copy; 1860 unsigned int n; 1861 1862 copy = alloc_engines(e->num_engines); 1863 if (!copy) 1864 return ERR_PTR(-ENOMEM); 1865 1866 for (n = 0; n < e->num_engines; n++) { 1867 if (e->engines[n]) 1868 copy->engines[n] = intel_context_get(e->engines[n]); 1869 else 1870 copy->engines[n] = NULL; 1871 } 1872 copy->num_engines = n; 1873 1874 return copy; 1875 } 1876 1877 static int 1878 get_engines(struct i915_gem_context *ctx, 1879 struct drm_i915_gem_context_param *args) 1880 { 1881 struct i915_context_param_engines __user *user; 1882 struct i915_gem_engines *e; 1883 size_t n, count, size; 1884 int err = 0; 1885 1886 err = mutex_lock_interruptible(&ctx->engines_mutex); 1887 if (err) 1888 return err; 1889 1890 e = NULL; 1891 if (i915_gem_context_user_engines(ctx)) 1892 e = __copy_engines(i915_gem_context_engines(ctx)); 1893 mutex_unlock(&ctx->engines_mutex); 1894 if (IS_ERR_OR_NULL(e)) { 1895 args->size = 0; 1896 return PTR_ERR_OR_ZERO(e); 1897 } 1898 1899 count = e->num_engines; 1900 1901 /* Be paranoid in case we have an impedance mismatch */ 1902 if (!check_struct_size(user, engines, count, &size)) { 1903 err = -EINVAL; 1904 goto err_free; 1905 } 1906 if (overflows_type(size, args->size)) { 1907 err = -EINVAL; 1908 goto err_free; 1909 } 1910 1911 if (!args->size) { 1912 args->size = size; 1913 goto err_free; 1914 } 1915 1916 if (args->size < size) { 1917 err = -EINVAL; 1918 goto err_free; 1919 } 1920 1921 user = u64_to_user_ptr(args->value); 1922 if (put_user(0, &user->extensions)) { 1923 err = -EFAULT; 1924 goto err_free; 1925 } 1926 1927 for (n = 0; n < count; n++) { 1928 struct i915_engine_class_instance ci = { 1929 .engine_class = I915_ENGINE_CLASS_INVALID, 1930 .engine_instance = I915_ENGINE_CLASS_INVALID_NONE, 1931 }; 1932 1933 if (e->engines[n]) { 1934 ci.engine_class = e->engines[n]->engine->uabi_class; 1935 ci.engine_instance = e->engines[n]->engine->uabi_instance; 1936 } 1937 1938 if (copy_to_user(&user->engines[n], &ci, sizeof(ci))) { 1939 err = -EFAULT; 1940 goto err_free; 1941 } 1942 } 1943 1944 args->size = size; 1945 1946 err_free: 1947 free_engines(e); 1948 return err; 1949 } 1950 1951 static int 1952 set_persistence(struct i915_gem_context *ctx, 1953 const struct drm_i915_gem_context_param *args) 1954 { 1955 if (args->size) 1956 return -EINVAL; 1957 1958 return __context_set_persistence(ctx, args->value); 1959 } 1960 1961 static int __apply_priority(struct intel_context *ce, void *arg) 1962 { 1963 struct i915_gem_context *ctx = arg; 1964 1965 if (!intel_engine_has_timeslices(ce->engine)) 1966 return 0; 1967 1968 if (ctx->sched.priority >= I915_PRIORITY_NORMAL) 1969 intel_context_set_use_semaphores(ce); 1970 else 1971 intel_context_clear_use_semaphores(ce); 1972 1973 return 0; 1974 } 1975 1976 static int set_priority(struct i915_gem_context *ctx, 1977 const struct drm_i915_gem_context_param *args) 1978 { 1979 s64 priority = args->value; 1980 1981 if (args->size) 1982 return -EINVAL; 1983 1984 if (!(ctx->i915->caps.scheduler & I915_SCHEDULER_CAP_PRIORITY)) 1985 return -ENODEV; 1986 1987 if (priority > I915_CONTEXT_MAX_USER_PRIORITY || 1988 priority < I915_CONTEXT_MIN_USER_PRIORITY) 1989 return -EINVAL; 1990 1991 if (priority > I915_CONTEXT_DEFAULT_PRIORITY && 1992 !capable(CAP_SYS_NICE)) 1993 return -EPERM; 1994 1995 ctx->sched.priority = I915_USER_PRIORITY(priority); 1996 context_apply_all(ctx, __apply_priority, ctx); 1997 1998 return 0; 1999 } 2000 2001 static int ctx_setparam(struct drm_i915_file_private *fpriv, 2002 struct i915_gem_context *ctx, 2003 struct drm_i915_gem_context_param *args) 2004 { 2005 int ret = 0; 2006 2007 switch (args->param) { 2008 case I915_CONTEXT_PARAM_NO_ZEROMAP: 2009 if (args->size) 2010 ret = -EINVAL; 2011 else if (args->value) 2012 set_bit(UCONTEXT_NO_ZEROMAP, &ctx->user_flags); 2013 else 2014 clear_bit(UCONTEXT_NO_ZEROMAP, &ctx->user_flags); 2015 break; 2016 2017 case I915_CONTEXT_PARAM_NO_ERROR_CAPTURE: 2018 if (args->size) 2019 ret = -EINVAL; 2020 else if (args->value) 2021 i915_gem_context_set_no_error_capture(ctx); 2022 else 2023 i915_gem_context_clear_no_error_capture(ctx); 2024 break; 2025 2026 case I915_CONTEXT_PARAM_BANNABLE: 2027 if (args->size) 2028 ret = -EINVAL; 2029 else if (!capable(CAP_SYS_ADMIN) && !args->value) 2030 ret = -EPERM; 2031 else if (args->value) 2032 i915_gem_context_set_bannable(ctx); 2033 else 2034 i915_gem_context_clear_bannable(ctx); 2035 break; 2036 2037 case I915_CONTEXT_PARAM_RECOVERABLE: 2038 if (args->size) 2039 ret = -EINVAL; 2040 else if (args->value) 2041 i915_gem_context_set_recoverable(ctx); 2042 else 2043 i915_gem_context_clear_recoverable(ctx); 2044 break; 2045 2046 case I915_CONTEXT_PARAM_PRIORITY: 2047 ret = set_priority(ctx, args); 2048 break; 2049 2050 case I915_CONTEXT_PARAM_SSEU: 2051 ret = set_sseu(ctx, args); 2052 break; 2053 2054 case I915_CONTEXT_PARAM_VM: 2055 ret = set_ppgtt(fpriv, ctx, args); 2056 break; 2057 2058 case I915_CONTEXT_PARAM_ENGINES: 2059 ret = set_engines(ctx, args); 2060 break; 2061 2062 case I915_CONTEXT_PARAM_PERSISTENCE: 2063 ret = set_persistence(ctx, args); 2064 break; 2065 2066 case I915_CONTEXT_PARAM_RINGSIZE: 2067 ret = set_ringsize(ctx, args); 2068 break; 2069 2070 case I915_CONTEXT_PARAM_BAN_PERIOD: 2071 default: 2072 ret = -EINVAL; 2073 break; 2074 } 2075 2076 return ret; 2077 } 2078 2079 struct create_ext { 2080 struct i915_gem_context *ctx; 2081 struct drm_i915_file_private *fpriv; 2082 }; 2083 2084 static int create_setparam(struct i915_user_extension __user *ext, void *data) 2085 { 2086 struct drm_i915_gem_context_create_ext_setparam local; 2087 const struct create_ext *arg = data; 2088 2089 if (copy_from_user(&local, ext, sizeof(local))) 2090 return -EFAULT; 2091 2092 if (local.param.ctx_id) 2093 return -EINVAL; 2094 2095 return ctx_setparam(arg->fpriv, arg->ctx, &local.param); 2096 } 2097 2098 static int copy_ring_size(struct intel_context *dst, 2099 struct intel_context *src) 2100 { 2101 long sz; 2102 2103 sz = intel_context_get_ring_size(src); 2104 if (sz < 0) 2105 return sz; 2106 2107 return intel_context_set_ring_size(dst, sz); 2108 } 2109 2110 static int clone_engines(struct i915_gem_context *dst, 2111 struct i915_gem_context *src) 2112 { 2113 struct i915_gem_engines *e = i915_gem_context_lock_engines(src); 2114 struct i915_gem_engines *clone; 2115 bool user_engines; 2116 unsigned long n; 2117 2118 clone = alloc_engines(e->num_engines); 2119 if (!clone) 2120 goto err_unlock; 2121 2122 for (n = 0; n < e->num_engines; n++) { 2123 struct intel_engine_cs *engine; 2124 2125 if (!e->engines[n]) { 2126 clone->engines[n] = NULL; 2127 continue; 2128 } 2129 engine = e->engines[n]->engine; 2130 2131 /* 2132 * Virtual engines are singletons; they can only exist 2133 * inside a single context, because they embed their 2134 * HW context... As each virtual context implies a single 2135 * timeline (each engine can only dequeue a single request 2136 * at any time), it would be surprising for two contexts 2137 * to use the same engine. So let's create a copy of 2138 * the virtual engine instead. 2139 */ 2140 if (intel_engine_is_virtual(engine)) 2141 clone->engines[n] = 2142 intel_execlists_clone_virtual(engine); 2143 else 2144 clone->engines[n] = intel_context_create(engine); 2145 if (IS_ERR_OR_NULL(clone->engines[n])) { 2146 __free_engines(clone, n); 2147 goto err_unlock; 2148 } 2149 2150 intel_context_set_gem(clone->engines[n], dst); 2151 2152 /* Copy across the preferred ringsize */ 2153 if (copy_ring_size(clone->engines[n], e->engines[n])) { 2154 __free_engines(clone, n + 1); 2155 goto err_unlock; 2156 } 2157 } 2158 clone->num_engines = n; 2159 2160 user_engines = i915_gem_context_user_engines(src); 2161 i915_gem_context_unlock_engines(src); 2162 2163 /* Serialised by constructor */ 2164 engines_idle_release(dst, rcu_replace_pointer(dst->engines, clone, 1)); 2165 if (user_engines) 2166 i915_gem_context_set_user_engines(dst); 2167 else 2168 i915_gem_context_clear_user_engines(dst); 2169 return 0; 2170 2171 err_unlock: 2172 i915_gem_context_unlock_engines(src); 2173 return -ENOMEM; 2174 } 2175 2176 static int clone_flags(struct i915_gem_context *dst, 2177 struct i915_gem_context *src) 2178 { 2179 dst->user_flags = src->user_flags; 2180 return 0; 2181 } 2182 2183 static int clone_schedattr(struct i915_gem_context *dst, 2184 struct i915_gem_context *src) 2185 { 2186 dst->sched = src->sched; 2187 return 0; 2188 } 2189 2190 static int clone_sseu(struct i915_gem_context *dst, 2191 struct i915_gem_context *src) 2192 { 2193 struct i915_gem_engines *e = i915_gem_context_lock_engines(src); 2194 struct i915_gem_engines *clone; 2195 unsigned long n; 2196 int err; 2197 2198 /* no locking required; sole access under constructor*/ 2199 clone = __context_engines_static(dst); 2200 if (e->num_engines != clone->num_engines) { 2201 err = -EINVAL; 2202 goto unlock; 2203 } 2204 2205 for (n = 0; n < e->num_engines; n++) { 2206 struct intel_context *ce = e->engines[n]; 2207 2208 if (clone->engines[n]->engine->class != ce->engine->class) { 2209 /* Must have compatible engine maps! */ 2210 err = -EINVAL; 2211 goto unlock; 2212 } 2213 2214 /* serialises with set_sseu */ 2215 err = intel_context_lock_pinned(ce); 2216 if (err) 2217 goto unlock; 2218 2219 clone->engines[n]->sseu = ce->sseu; 2220 intel_context_unlock_pinned(ce); 2221 } 2222 2223 err = 0; 2224 unlock: 2225 i915_gem_context_unlock_engines(src); 2226 return err; 2227 } 2228 2229 static int clone_timeline(struct i915_gem_context *dst, 2230 struct i915_gem_context *src) 2231 { 2232 if (src->timeline) 2233 __assign_timeline(dst, src->timeline); 2234 2235 return 0; 2236 } 2237 2238 static int clone_vm(struct i915_gem_context *dst, 2239 struct i915_gem_context *src) 2240 { 2241 struct i915_address_space *vm; 2242 int err = 0; 2243 2244 if (!rcu_access_pointer(src->vm)) 2245 return 0; 2246 2247 rcu_read_lock(); 2248 vm = context_get_vm_rcu(src); 2249 rcu_read_unlock(); 2250 2251 if (!mutex_lock_interruptible(&dst->mutex)) { 2252 __assign_ppgtt(dst, vm); 2253 mutex_unlock(&dst->mutex); 2254 } else { 2255 err = -EINTR; 2256 } 2257 2258 i915_vm_put(vm); 2259 return err; 2260 } 2261 2262 static int create_clone(struct i915_user_extension __user *ext, void *data) 2263 { 2264 static int (* const fn[])(struct i915_gem_context *dst, 2265 struct i915_gem_context *src) = { 2266 #define MAP(x, y) [ilog2(I915_CONTEXT_CLONE_##x)] = y 2267 MAP(ENGINES, clone_engines), 2268 MAP(FLAGS, clone_flags), 2269 MAP(SCHEDATTR, clone_schedattr), 2270 MAP(SSEU, clone_sseu), 2271 MAP(TIMELINE, clone_timeline), 2272 MAP(VM, clone_vm), 2273 #undef MAP 2274 }; 2275 struct drm_i915_gem_context_create_ext_clone local; 2276 const struct create_ext *arg = data; 2277 struct i915_gem_context *dst = arg->ctx; 2278 struct i915_gem_context *src; 2279 int err, bit; 2280 2281 if (copy_from_user(&local, ext, sizeof(local))) 2282 return -EFAULT; 2283 2284 BUILD_BUG_ON(GENMASK(BITS_PER_TYPE(local.flags) - 1, ARRAY_SIZE(fn)) != 2285 I915_CONTEXT_CLONE_UNKNOWN); 2286 2287 if (local.flags & I915_CONTEXT_CLONE_UNKNOWN) 2288 return -EINVAL; 2289 2290 if (local.rsvd) 2291 return -EINVAL; 2292 2293 rcu_read_lock(); 2294 src = __i915_gem_context_lookup_rcu(arg->fpriv, local.clone_id); 2295 rcu_read_unlock(); 2296 if (!src) 2297 return -ENOENT; 2298 2299 GEM_BUG_ON(src == dst); 2300 2301 for (bit = 0; bit < ARRAY_SIZE(fn); bit++) { 2302 if (!(local.flags & BIT(bit))) 2303 continue; 2304 2305 err = fn[bit](dst, src); 2306 if (err) 2307 return err; 2308 } 2309 2310 return 0; 2311 } 2312 2313 static const i915_user_extension_fn create_extensions[] = { 2314 [I915_CONTEXT_CREATE_EXT_SETPARAM] = create_setparam, 2315 [I915_CONTEXT_CREATE_EXT_CLONE] = create_clone, 2316 }; 2317 2318 static bool client_is_banned(struct drm_i915_file_private *file_priv) 2319 { 2320 return atomic_read(&file_priv->ban_score) >= I915_CLIENT_SCORE_BANNED; 2321 } 2322 2323 int i915_gem_context_create_ioctl(struct drm_device *dev, void *data, 2324 struct drm_file *file) 2325 { 2326 struct drm_i915_private *i915 = to_i915(dev); 2327 struct drm_i915_gem_context_create_ext *args = data; 2328 struct create_ext ext_data; 2329 int ret; 2330 u32 id; 2331 2332 if (!DRIVER_CAPS(i915)->has_logical_contexts) 2333 return -ENODEV; 2334 2335 if (args->flags & I915_CONTEXT_CREATE_FLAGS_UNKNOWN) 2336 return -EINVAL; 2337 2338 ret = intel_gt_terminally_wedged(&i915->gt); 2339 if (ret) 2340 return ret; 2341 2342 ext_data.fpriv = file->driver_priv; 2343 if (client_is_banned(ext_data.fpriv)) { 2344 drm_dbg(&i915->drm, 2345 "client %s[%d] banned from creating ctx\n", 2346 current->comm, task_pid_nr(current)); 2347 return -EIO; 2348 } 2349 2350 ext_data.ctx = i915_gem_create_context(i915, args->flags); 2351 if (IS_ERR(ext_data.ctx)) 2352 return PTR_ERR(ext_data.ctx); 2353 2354 if (args->flags & I915_CONTEXT_CREATE_FLAGS_USE_EXTENSIONS) { 2355 ret = i915_user_extensions(u64_to_user_ptr(args->extensions), 2356 create_extensions, 2357 ARRAY_SIZE(create_extensions), 2358 &ext_data); 2359 if (ret) 2360 goto err_ctx; 2361 } 2362 2363 ret = gem_context_register(ext_data.ctx, ext_data.fpriv, &id); 2364 if (ret < 0) 2365 goto err_ctx; 2366 2367 args->ctx_id = id; 2368 drm_dbg(&i915->drm, "HW context %d created\n", args->ctx_id); 2369 2370 return 0; 2371 2372 err_ctx: 2373 context_close(ext_data.ctx); 2374 return ret; 2375 } 2376 2377 int i915_gem_context_destroy_ioctl(struct drm_device *dev, void *data, 2378 struct drm_file *file) 2379 { 2380 struct drm_i915_gem_context_destroy *args = data; 2381 struct drm_i915_file_private *file_priv = file->driver_priv; 2382 struct i915_gem_context *ctx; 2383 2384 if (args->pad != 0) 2385 return -EINVAL; 2386 2387 if (!args->ctx_id) 2388 return -ENOENT; 2389 2390 ctx = xa_erase(&file_priv->context_xa, args->ctx_id); 2391 if (!ctx) 2392 return -ENOENT; 2393 2394 context_close(ctx); 2395 return 0; 2396 } 2397 2398 static int get_sseu(struct i915_gem_context *ctx, 2399 struct drm_i915_gem_context_param *args) 2400 { 2401 struct drm_i915_gem_context_param_sseu user_sseu; 2402 struct intel_context *ce; 2403 unsigned long lookup; 2404 int err; 2405 2406 if (args->size == 0) 2407 goto out; 2408 else if (args->size < sizeof(user_sseu)) 2409 return -EINVAL; 2410 2411 if (copy_from_user(&user_sseu, u64_to_user_ptr(args->value), 2412 sizeof(user_sseu))) 2413 return -EFAULT; 2414 2415 if (user_sseu.rsvd) 2416 return -EINVAL; 2417 2418 if (user_sseu.flags & ~(I915_CONTEXT_SSEU_FLAG_ENGINE_INDEX)) 2419 return -EINVAL; 2420 2421 lookup = 0; 2422 if (user_sseu.flags & I915_CONTEXT_SSEU_FLAG_ENGINE_INDEX) 2423 lookup |= LOOKUP_USER_INDEX; 2424 2425 ce = lookup_user_engine(ctx, lookup, &user_sseu.engine); 2426 if (IS_ERR(ce)) 2427 return PTR_ERR(ce); 2428 2429 err = intel_context_lock_pinned(ce); /* serialises with set_sseu */ 2430 if (err) { 2431 intel_context_put(ce); 2432 return err; 2433 } 2434 2435 user_sseu.slice_mask = ce->sseu.slice_mask; 2436 user_sseu.subslice_mask = ce->sseu.subslice_mask; 2437 user_sseu.min_eus_per_subslice = ce->sseu.min_eus_per_subslice; 2438 user_sseu.max_eus_per_subslice = ce->sseu.max_eus_per_subslice; 2439 2440 intel_context_unlock_pinned(ce); 2441 intel_context_put(ce); 2442 2443 if (copy_to_user(u64_to_user_ptr(args->value), &user_sseu, 2444 sizeof(user_sseu))) 2445 return -EFAULT; 2446 2447 out: 2448 args->size = sizeof(user_sseu); 2449 2450 return 0; 2451 } 2452 2453 int i915_gem_context_getparam_ioctl(struct drm_device *dev, void *data, 2454 struct drm_file *file) 2455 { 2456 struct drm_i915_file_private *file_priv = file->driver_priv; 2457 struct drm_i915_gem_context_param *args = data; 2458 struct i915_gem_context *ctx; 2459 int ret = 0; 2460 2461 ctx = i915_gem_context_lookup(file_priv, args->ctx_id); 2462 if (!ctx) 2463 return -ENOENT; 2464 2465 switch (args->param) { 2466 case I915_CONTEXT_PARAM_NO_ZEROMAP: 2467 args->size = 0; 2468 args->value = test_bit(UCONTEXT_NO_ZEROMAP, &ctx->user_flags); 2469 break; 2470 2471 case I915_CONTEXT_PARAM_GTT_SIZE: 2472 args->size = 0; 2473 rcu_read_lock(); 2474 if (rcu_access_pointer(ctx->vm)) 2475 args->value = rcu_dereference(ctx->vm)->total; 2476 else 2477 args->value = to_i915(dev)->ggtt.vm.total; 2478 rcu_read_unlock(); 2479 break; 2480 2481 case I915_CONTEXT_PARAM_NO_ERROR_CAPTURE: 2482 args->size = 0; 2483 args->value = i915_gem_context_no_error_capture(ctx); 2484 break; 2485 2486 case I915_CONTEXT_PARAM_BANNABLE: 2487 args->size = 0; 2488 args->value = i915_gem_context_is_bannable(ctx); 2489 break; 2490 2491 case I915_CONTEXT_PARAM_RECOVERABLE: 2492 args->size = 0; 2493 args->value = i915_gem_context_is_recoverable(ctx); 2494 break; 2495 2496 case I915_CONTEXT_PARAM_PRIORITY: 2497 args->size = 0; 2498 args->value = ctx->sched.priority >> I915_USER_PRIORITY_SHIFT; 2499 break; 2500 2501 case I915_CONTEXT_PARAM_SSEU: 2502 ret = get_sseu(ctx, args); 2503 break; 2504 2505 case I915_CONTEXT_PARAM_VM: 2506 ret = get_ppgtt(file_priv, ctx, args); 2507 break; 2508 2509 case I915_CONTEXT_PARAM_ENGINES: 2510 ret = get_engines(ctx, args); 2511 break; 2512 2513 case I915_CONTEXT_PARAM_PERSISTENCE: 2514 args->size = 0; 2515 args->value = i915_gem_context_is_persistent(ctx); 2516 break; 2517 2518 case I915_CONTEXT_PARAM_RINGSIZE: 2519 ret = get_ringsize(ctx, args); 2520 break; 2521 2522 case I915_CONTEXT_PARAM_BAN_PERIOD: 2523 default: 2524 ret = -EINVAL; 2525 break; 2526 } 2527 2528 i915_gem_context_put(ctx); 2529 return ret; 2530 } 2531 2532 int i915_gem_context_setparam_ioctl(struct drm_device *dev, void *data, 2533 struct drm_file *file) 2534 { 2535 struct drm_i915_file_private *file_priv = file->driver_priv; 2536 struct drm_i915_gem_context_param *args = data; 2537 struct i915_gem_context *ctx; 2538 int ret; 2539 2540 ctx = i915_gem_context_lookup(file_priv, args->ctx_id); 2541 if (!ctx) 2542 return -ENOENT; 2543 2544 ret = ctx_setparam(file_priv, ctx, args); 2545 2546 i915_gem_context_put(ctx); 2547 return ret; 2548 } 2549 2550 int i915_gem_context_reset_stats_ioctl(struct drm_device *dev, 2551 void *data, struct drm_file *file) 2552 { 2553 struct drm_i915_private *i915 = to_i915(dev); 2554 struct drm_i915_reset_stats *args = data; 2555 struct i915_gem_context *ctx; 2556 int ret; 2557 2558 if (args->flags || args->pad) 2559 return -EINVAL; 2560 2561 ret = -ENOENT; 2562 rcu_read_lock(); 2563 ctx = __i915_gem_context_lookup_rcu(file->driver_priv, args->ctx_id); 2564 if (!ctx) 2565 goto out; 2566 2567 /* 2568 * We opt for unserialised reads here. This may result in tearing 2569 * in the extremely unlikely event of a GPU hang on this context 2570 * as we are querying them. If we need that extra layer of protection, 2571 * we should wrap the hangstats with a seqlock. 2572 */ 2573 2574 if (capable(CAP_SYS_ADMIN)) 2575 args->reset_count = i915_reset_count(&i915->gpu_error); 2576 else 2577 args->reset_count = 0; 2578 2579 args->batch_active = atomic_read(&ctx->guilty_count); 2580 args->batch_pending = atomic_read(&ctx->active_count); 2581 2582 ret = 0; 2583 out: 2584 rcu_read_unlock(); 2585 return ret; 2586 } 2587 2588 /* GEM context-engines iterator: for_each_gem_engine() */ 2589 struct intel_context * 2590 i915_gem_engines_iter_next(struct i915_gem_engines_iter *it) 2591 { 2592 const struct i915_gem_engines *e = it->engines; 2593 struct intel_context *ctx; 2594 2595 if (unlikely(!e)) 2596 return NULL; 2597 2598 do { 2599 if (it->idx >= e->num_engines) 2600 return NULL; 2601 2602 ctx = e->engines[it->idx++]; 2603 } while (!ctx); 2604 2605 return ctx; 2606 } 2607 2608 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST) 2609 #include "selftests/mock_context.c" 2610 #include "selftests/i915_gem_context.c" 2611 #endif 2612 2613 static void i915_global_gem_context_shrink(void) 2614 { 2615 kmem_cache_shrink(global.slab_luts); 2616 } 2617 2618 static void i915_global_gem_context_exit(void) 2619 { 2620 kmem_cache_destroy(global.slab_luts); 2621 } 2622 2623 static struct i915_global_gem_context global = { { 2624 .shrink = i915_global_gem_context_shrink, 2625 .exit = i915_global_gem_context_exit, 2626 } }; 2627 2628 int __init i915_global_gem_context_init(void) 2629 { 2630 global.slab_luts = KMEM_CACHE(i915_lut_handle, 0); 2631 if (!global.slab_luts) 2632 return -ENOMEM; 2633 2634 i915_global_register(&global.base); 2635 return 0; 2636 } 2637