1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2014 Intel Corporation 4 */ 5 6 #include <linux/circ_buf.h> 7 8 #include "gem/i915_gem_context.h" 9 #include "gem/i915_gem_lmem.h" 10 #include "gt/gen8_engine_cs.h" 11 #include "gt/intel_breadcrumbs.h" 12 #include "gt/intel_context.h" 13 #include "gt/intel_engine_heartbeat.h" 14 #include "gt/intel_engine_pm.h" 15 #include "gt/intel_engine_regs.h" 16 #include "gt/intel_gpu_commands.h" 17 #include "gt/intel_gt.h" 18 #include "gt/intel_gt_clock_utils.h" 19 #include "gt/intel_gt_irq.h" 20 #include "gt/intel_gt_pm.h" 21 #include "gt/intel_gt_regs.h" 22 #include "gt/intel_gt_requests.h" 23 #include "gt/intel_lrc.h" 24 #include "gt/intel_lrc_reg.h" 25 #include "gt/intel_mocs.h" 26 #include "gt/intel_ring.h" 27 28 #include "intel_guc_ads.h" 29 #include "intel_guc_capture.h" 30 #include "intel_guc_print.h" 31 #include "intel_guc_submission.h" 32 33 #include "i915_drv.h" 34 #include "i915_reg.h" 35 #include "i915_irq.h" 36 #include "i915_trace.h" 37 38 /** 39 * DOC: GuC-based command submission 40 * 41 * The Scratch registers: 42 * There are 16 MMIO-based registers start from 0xC180. The kernel driver writes 43 * a value to the action register (SOFT_SCRATCH_0) along with any data. It then 44 * triggers an interrupt on the GuC via another register write (0xC4C8). 45 * Firmware writes a success/fail code back to the action register after 46 * processes the request. The kernel driver polls waiting for this update and 47 * then proceeds. 48 * 49 * Command Transport buffers (CTBs): 50 * Covered in detail in other sections but CTBs (Host to GuC - H2G, GuC to Host 51 * - G2H) are a message interface between the i915 and GuC. 52 * 53 * Context registration: 54 * Before a context can be submitted it must be registered with the GuC via a 55 * H2G. A unique guc_id is associated with each context. The context is either 56 * registered at request creation time (normal operation) or at submission time 57 * (abnormal operation, e.g. after a reset). 58 * 59 * Context submission: 60 * The i915 updates the LRC tail value in memory. The i915 must enable the 61 * scheduling of the context within the GuC for the GuC to actually consider it. 62 * Therefore, the first time a disabled context is submitted we use a schedule 63 * enable H2G, while follow up submissions are done via the context submit H2G, 64 * which informs the GuC that a previously enabled context has new work 65 * available. 66 * 67 * Context unpin: 68 * To unpin a context a H2G is used to disable scheduling. When the 69 * corresponding G2H returns indicating the scheduling disable operation has 70 * completed it is safe to unpin the context. While a disable is in flight it 71 * isn't safe to resubmit the context so a fence is used to stall all future 72 * requests of that context until the G2H is returned. Because this interaction 73 * with the GuC takes a non-zero amount of time we delay the disabling of 74 * scheduling after the pin count goes to zero by a configurable period of time 75 * (see SCHED_DISABLE_DELAY_MS). The thought is this gives the user a window of 76 * time to resubmit something on the context before doing this costly operation. 77 * This delay is only done if the context isn't closed and the guc_id usage is 78 * less than a threshold (see NUM_SCHED_DISABLE_GUC_IDS_THRESHOLD). 79 * 80 * Context deregistration: 81 * Before a context can be destroyed or if we steal its guc_id we must 82 * deregister the context with the GuC via H2G. If stealing the guc_id it isn't 83 * safe to submit anything to this guc_id until the deregister completes so a 84 * fence is used to stall all requests associated with this guc_id until the 85 * corresponding G2H returns indicating the guc_id has been deregistered. 86 * 87 * submission_state.guc_ids: 88 * Unique number associated with private GuC context data passed in during 89 * context registration / submission / deregistration. 64k available. Simple ida 90 * is used for allocation. 91 * 92 * Stealing guc_ids: 93 * If no guc_ids are available they can be stolen from another context at 94 * request creation time if that context is unpinned. If a guc_id can't be found 95 * we punt this problem to the user as we believe this is near impossible to hit 96 * during normal use cases. 97 * 98 * Locking: 99 * In the GuC submission code we have 3 basic spin locks which protect 100 * everything. Details about each below. 101 * 102 * sched_engine->lock 103 * This is the submission lock for all contexts that share an i915 schedule 104 * engine (sched_engine), thus only one of the contexts which share a 105 * sched_engine can be submitting at a time. Currently only one sched_engine is 106 * used for all of GuC submission but that could change in the future. 107 * 108 * guc->submission_state.lock 109 * Global lock for GuC submission state. Protects guc_ids and destroyed contexts 110 * list. 111 * 112 * ce->guc_state.lock 113 * Protects everything under ce->guc_state. Ensures that a context is in the 114 * correct state before issuing a H2G. e.g. We don't issue a schedule disable 115 * on a disabled context (bad idea), we don't issue a schedule enable when a 116 * schedule disable is in flight, etc... Also protects list of inflight requests 117 * on the context and the priority management state. Lock is individual to each 118 * context. 119 * 120 * Lock ordering rules: 121 * sched_engine->lock -> ce->guc_state.lock 122 * guc->submission_state.lock -> ce->guc_state.lock 123 * 124 * Reset races: 125 * When a full GT reset is triggered it is assumed that some G2H responses to 126 * H2Gs can be lost as the GuC is also reset. Losing these G2H can prove to be 127 * fatal as we do certain operations upon receiving a G2H (e.g. destroy 128 * contexts, release guc_ids, etc...). When this occurs we can scrub the 129 * context state and cleanup appropriately, however this is quite racey. 130 * To avoid races, the reset code must disable submission before scrubbing for 131 * the missing G2H, while the submission code must check for submission being 132 * disabled and skip sending H2Gs and updating context states when it is. Both 133 * sides must also make sure to hold the relevant locks. 134 */ 135 136 /* GuC Virtual Engine */ 137 struct guc_virtual_engine { 138 struct intel_engine_cs base; 139 struct intel_context context; 140 }; 141 142 static struct intel_context * 143 guc_create_virtual(struct intel_engine_cs **siblings, unsigned int count, 144 unsigned long flags); 145 146 static struct intel_context * 147 guc_create_parallel(struct intel_engine_cs **engines, 148 unsigned int num_siblings, 149 unsigned int width); 150 151 #define GUC_REQUEST_SIZE 64 /* bytes */ 152 153 /* 154 * We reserve 1/16 of the guc_ids for multi-lrc as these need to be contiguous 155 * per the GuC submission interface. A different allocation algorithm is used 156 * (bitmap vs. ida) between multi-lrc and single-lrc hence the reason to 157 * partition the guc_id space. We believe the number of multi-lrc contexts in 158 * use should be low and 1/16 should be sufficient. Minimum of 32 guc_ids for 159 * multi-lrc. 160 */ 161 #define NUMBER_MULTI_LRC_GUC_ID(guc) \ 162 ((guc)->submission_state.num_guc_ids / 16) 163 164 /* 165 * Below is a set of functions which control the GuC scheduling state which 166 * require a lock. 167 */ 168 #define SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER BIT(0) 169 #define SCHED_STATE_DESTROYED BIT(1) 170 #define SCHED_STATE_PENDING_DISABLE BIT(2) 171 #define SCHED_STATE_BANNED BIT(3) 172 #define SCHED_STATE_ENABLED BIT(4) 173 #define SCHED_STATE_PENDING_ENABLE BIT(5) 174 #define SCHED_STATE_REGISTERED BIT(6) 175 #define SCHED_STATE_POLICY_REQUIRED BIT(7) 176 #define SCHED_STATE_CLOSED BIT(8) 177 #define SCHED_STATE_BLOCKED_SHIFT 9 178 #define SCHED_STATE_BLOCKED BIT(SCHED_STATE_BLOCKED_SHIFT) 179 #define SCHED_STATE_BLOCKED_MASK (0xfff << SCHED_STATE_BLOCKED_SHIFT) 180 181 static inline void init_sched_state(struct intel_context *ce) 182 { 183 lockdep_assert_held(&ce->guc_state.lock); 184 ce->guc_state.sched_state &= SCHED_STATE_BLOCKED_MASK; 185 } 186 187 /* 188 * Kernel contexts can have SCHED_STATE_REGISTERED after suspend. 189 * A context close can race with the submission path, so SCHED_STATE_CLOSED 190 * can be set immediately before we try to register. 191 */ 192 #define SCHED_STATE_VALID_INIT \ 193 (SCHED_STATE_BLOCKED_MASK | \ 194 SCHED_STATE_CLOSED | \ 195 SCHED_STATE_REGISTERED) 196 197 __maybe_unused 198 static bool sched_state_is_init(struct intel_context *ce) 199 { 200 return !(ce->guc_state.sched_state & ~SCHED_STATE_VALID_INIT); 201 } 202 203 static inline bool 204 context_wait_for_deregister_to_register(struct intel_context *ce) 205 { 206 return ce->guc_state.sched_state & 207 SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER; 208 } 209 210 static inline void 211 set_context_wait_for_deregister_to_register(struct intel_context *ce) 212 { 213 lockdep_assert_held(&ce->guc_state.lock); 214 ce->guc_state.sched_state |= 215 SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER; 216 } 217 218 static inline void 219 clr_context_wait_for_deregister_to_register(struct intel_context *ce) 220 { 221 lockdep_assert_held(&ce->guc_state.lock); 222 ce->guc_state.sched_state &= 223 ~SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER; 224 } 225 226 static inline bool 227 context_destroyed(struct intel_context *ce) 228 { 229 return ce->guc_state.sched_state & SCHED_STATE_DESTROYED; 230 } 231 232 static inline void 233 set_context_destroyed(struct intel_context *ce) 234 { 235 lockdep_assert_held(&ce->guc_state.lock); 236 ce->guc_state.sched_state |= SCHED_STATE_DESTROYED; 237 } 238 239 static inline void 240 clr_context_destroyed(struct intel_context *ce) 241 { 242 lockdep_assert_held(&ce->guc_state.lock); 243 ce->guc_state.sched_state &= ~SCHED_STATE_DESTROYED; 244 } 245 246 static inline bool context_pending_disable(struct intel_context *ce) 247 { 248 return ce->guc_state.sched_state & SCHED_STATE_PENDING_DISABLE; 249 } 250 251 static inline void set_context_pending_disable(struct intel_context *ce) 252 { 253 lockdep_assert_held(&ce->guc_state.lock); 254 ce->guc_state.sched_state |= SCHED_STATE_PENDING_DISABLE; 255 } 256 257 static inline void clr_context_pending_disable(struct intel_context *ce) 258 { 259 lockdep_assert_held(&ce->guc_state.lock); 260 ce->guc_state.sched_state &= ~SCHED_STATE_PENDING_DISABLE; 261 } 262 263 static inline bool context_banned(struct intel_context *ce) 264 { 265 return ce->guc_state.sched_state & SCHED_STATE_BANNED; 266 } 267 268 static inline void set_context_banned(struct intel_context *ce) 269 { 270 lockdep_assert_held(&ce->guc_state.lock); 271 ce->guc_state.sched_state |= SCHED_STATE_BANNED; 272 } 273 274 static inline void clr_context_banned(struct intel_context *ce) 275 { 276 lockdep_assert_held(&ce->guc_state.lock); 277 ce->guc_state.sched_state &= ~SCHED_STATE_BANNED; 278 } 279 280 static inline bool context_enabled(struct intel_context *ce) 281 { 282 return ce->guc_state.sched_state & SCHED_STATE_ENABLED; 283 } 284 285 static inline void set_context_enabled(struct intel_context *ce) 286 { 287 lockdep_assert_held(&ce->guc_state.lock); 288 ce->guc_state.sched_state |= SCHED_STATE_ENABLED; 289 } 290 291 static inline void clr_context_enabled(struct intel_context *ce) 292 { 293 lockdep_assert_held(&ce->guc_state.lock); 294 ce->guc_state.sched_state &= ~SCHED_STATE_ENABLED; 295 } 296 297 static inline bool context_pending_enable(struct intel_context *ce) 298 { 299 return ce->guc_state.sched_state & SCHED_STATE_PENDING_ENABLE; 300 } 301 302 static inline void set_context_pending_enable(struct intel_context *ce) 303 { 304 lockdep_assert_held(&ce->guc_state.lock); 305 ce->guc_state.sched_state |= SCHED_STATE_PENDING_ENABLE; 306 } 307 308 static inline void clr_context_pending_enable(struct intel_context *ce) 309 { 310 lockdep_assert_held(&ce->guc_state.lock); 311 ce->guc_state.sched_state &= ~SCHED_STATE_PENDING_ENABLE; 312 } 313 314 static inline bool context_registered(struct intel_context *ce) 315 { 316 return ce->guc_state.sched_state & SCHED_STATE_REGISTERED; 317 } 318 319 static inline void set_context_registered(struct intel_context *ce) 320 { 321 lockdep_assert_held(&ce->guc_state.lock); 322 ce->guc_state.sched_state |= SCHED_STATE_REGISTERED; 323 } 324 325 static inline void clr_context_registered(struct intel_context *ce) 326 { 327 lockdep_assert_held(&ce->guc_state.lock); 328 ce->guc_state.sched_state &= ~SCHED_STATE_REGISTERED; 329 } 330 331 static inline bool context_policy_required(struct intel_context *ce) 332 { 333 return ce->guc_state.sched_state & SCHED_STATE_POLICY_REQUIRED; 334 } 335 336 static inline void set_context_policy_required(struct intel_context *ce) 337 { 338 lockdep_assert_held(&ce->guc_state.lock); 339 ce->guc_state.sched_state |= SCHED_STATE_POLICY_REQUIRED; 340 } 341 342 static inline void clr_context_policy_required(struct intel_context *ce) 343 { 344 lockdep_assert_held(&ce->guc_state.lock); 345 ce->guc_state.sched_state &= ~SCHED_STATE_POLICY_REQUIRED; 346 } 347 348 static inline bool context_close_done(struct intel_context *ce) 349 { 350 return ce->guc_state.sched_state & SCHED_STATE_CLOSED; 351 } 352 353 static inline void set_context_close_done(struct intel_context *ce) 354 { 355 lockdep_assert_held(&ce->guc_state.lock); 356 ce->guc_state.sched_state |= SCHED_STATE_CLOSED; 357 } 358 359 static inline u32 context_blocked(struct intel_context *ce) 360 { 361 return (ce->guc_state.sched_state & SCHED_STATE_BLOCKED_MASK) >> 362 SCHED_STATE_BLOCKED_SHIFT; 363 } 364 365 static inline void incr_context_blocked(struct intel_context *ce) 366 { 367 lockdep_assert_held(&ce->guc_state.lock); 368 369 ce->guc_state.sched_state += SCHED_STATE_BLOCKED; 370 371 GEM_BUG_ON(!context_blocked(ce)); /* Overflow check */ 372 } 373 374 static inline void decr_context_blocked(struct intel_context *ce) 375 { 376 lockdep_assert_held(&ce->guc_state.lock); 377 378 GEM_BUG_ON(!context_blocked(ce)); /* Underflow check */ 379 380 ce->guc_state.sched_state -= SCHED_STATE_BLOCKED; 381 } 382 383 static struct intel_context * 384 request_to_scheduling_context(struct i915_request *rq) 385 { 386 return intel_context_to_parent(rq->context); 387 } 388 389 static inline bool context_guc_id_invalid(struct intel_context *ce) 390 { 391 return ce->guc_id.id == GUC_INVALID_CONTEXT_ID; 392 } 393 394 static inline void set_context_guc_id_invalid(struct intel_context *ce) 395 { 396 ce->guc_id.id = GUC_INVALID_CONTEXT_ID; 397 } 398 399 static inline struct intel_guc *ce_to_guc(struct intel_context *ce) 400 { 401 return &ce->engine->gt->uc.guc; 402 } 403 404 static inline struct i915_priolist *to_priolist(struct rb_node *rb) 405 { 406 return rb_entry(rb, struct i915_priolist, node); 407 } 408 409 /* 410 * When using multi-lrc submission a scratch memory area is reserved in the 411 * parent's context state for the process descriptor, work queue, and handshake 412 * between the parent + children contexts to insert safe preemption points 413 * between each of the BBs. Currently the scratch area is sized to a page. 414 * 415 * The layout of this scratch area is below: 416 * 0 guc_process_desc 417 * + sizeof(struct guc_process_desc) child go 418 * + CACHELINE_BYTES child join[0] 419 * ... 420 * + CACHELINE_BYTES child join[n - 1] 421 * ... unused 422 * PARENT_SCRATCH_SIZE / 2 work queue start 423 * ... work queue 424 * PARENT_SCRATCH_SIZE - 1 work queue end 425 */ 426 #define WQ_SIZE (PARENT_SCRATCH_SIZE / 2) 427 #define WQ_OFFSET (PARENT_SCRATCH_SIZE - WQ_SIZE) 428 429 struct sync_semaphore { 430 u32 semaphore; 431 u8 unused[CACHELINE_BYTES - sizeof(u32)]; 432 }; 433 434 struct parent_scratch { 435 union guc_descs { 436 struct guc_sched_wq_desc wq_desc; 437 struct guc_process_desc_v69 pdesc; 438 } descs; 439 440 struct sync_semaphore go; 441 struct sync_semaphore join[MAX_ENGINE_INSTANCE + 1]; 442 443 u8 unused[WQ_OFFSET - sizeof(union guc_descs) - 444 sizeof(struct sync_semaphore) * (MAX_ENGINE_INSTANCE + 2)]; 445 446 u32 wq[WQ_SIZE / sizeof(u32)]; 447 }; 448 449 static u32 __get_parent_scratch_offset(struct intel_context *ce) 450 { 451 GEM_BUG_ON(!ce->parallel.guc.parent_page); 452 453 return ce->parallel.guc.parent_page * PAGE_SIZE; 454 } 455 456 static u32 __get_wq_offset(struct intel_context *ce) 457 { 458 BUILD_BUG_ON(offsetof(struct parent_scratch, wq) != WQ_OFFSET); 459 460 return __get_parent_scratch_offset(ce) + WQ_OFFSET; 461 } 462 463 static struct parent_scratch * 464 __get_parent_scratch(struct intel_context *ce) 465 { 466 BUILD_BUG_ON(sizeof(struct parent_scratch) != PARENT_SCRATCH_SIZE); 467 BUILD_BUG_ON(sizeof(struct sync_semaphore) != CACHELINE_BYTES); 468 469 /* 470 * Need to subtract LRC_STATE_OFFSET here as the 471 * parallel.guc.parent_page is the offset into ce->state while 472 * ce->lrc_reg_reg is ce->state + LRC_STATE_OFFSET. 473 */ 474 return (struct parent_scratch *) 475 (ce->lrc_reg_state + 476 ((__get_parent_scratch_offset(ce) - 477 LRC_STATE_OFFSET) / sizeof(u32))); 478 } 479 480 static struct guc_process_desc_v69 * 481 __get_process_desc_v69(struct intel_context *ce) 482 { 483 struct parent_scratch *ps = __get_parent_scratch(ce); 484 485 return &ps->descs.pdesc; 486 } 487 488 static struct guc_sched_wq_desc * 489 __get_wq_desc_v70(struct intel_context *ce) 490 { 491 struct parent_scratch *ps = __get_parent_scratch(ce); 492 493 return &ps->descs.wq_desc; 494 } 495 496 static u32 *get_wq_pointer(struct intel_context *ce, u32 wqi_size) 497 { 498 /* 499 * Check for space in work queue. Caching a value of head pointer in 500 * intel_context structure in order reduce the number accesses to shared 501 * GPU memory which may be across a PCIe bus. 502 */ 503 #define AVAILABLE_SPACE \ 504 CIRC_SPACE(ce->parallel.guc.wqi_tail, ce->parallel.guc.wqi_head, WQ_SIZE) 505 if (wqi_size > AVAILABLE_SPACE) { 506 ce->parallel.guc.wqi_head = READ_ONCE(*ce->parallel.guc.wq_head); 507 508 if (wqi_size > AVAILABLE_SPACE) 509 return NULL; 510 } 511 #undef AVAILABLE_SPACE 512 513 return &__get_parent_scratch(ce)->wq[ce->parallel.guc.wqi_tail / sizeof(u32)]; 514 } 515 516 static inline struct intel_context *__get_context(struct intel_guc *guc, u32 id) 517 { 518 struct intel_context *ce = xa_load(&guc->context_lookup, id); 519 520 GEM_BUG_ON(id >= GUC_MAX_CONTEXT_ID); 521 522 return ce; 523 } 524 525 static struct guc_lrc_desc_v69 *__get_lrc_desc_v69(struct intel_guc *guc, u32 index) 526 { 527 struct guc_lrc_desc_v69 *base = guc->lrc_desc_pool_vaddr_v69; 528 529 if (!base) 530 return NULL; 531 532 GEM_BUG_ON(index >= GUC_MAX_CONTEXT_ID); 533 534 return &base[index]; 535 } 536 537 static int guc_lrc_desc_pool_create_v69(struct intel_guc *guc) 538 { 539 u32 size; 540 int ret; 541 542 size = PAGE_ALIGN(sizeof(struct guc_lrc_desc_v69) * 543 GUC_MAX_CONTEXT_ID); 544 ret = intel_guc_allocate_and_map_vma(guc, size, &guc->lrc_desc_pool_v69, 545 (void **)&guc->lrc_desc_pool_vaddr_v69); 546 if (ret) 547 return ret; 548 549 return 0; 550 } 551 552 static void guc_lrc_desc_pool_destroy_v69(struct intel_guc *guc) 553 { 554 if (!guc->lrc_desc_pool_vaddr_v69) 555 return; 556 557 guc->lrc_desc_pool_vaddr_v69 = NULL; 558 i915_vma_unpin_and_release(&guc->lrc_desc_pool_v69, I915_VMA_RELEASE_MAP); 559 } 560 561 static inline bool guc_submission_initialized(struct intel_guc *guc) 562 { 563 return guc->submission_initialized; 564 } 565 566 static inline void _reset_lrc_desc_v69(struct intel_guc *guc, u32 id) 567 { 568 struct guc_lrc_desc_v69 *desc = __get_lrc_desc_v69(guc, id); 569 570 if (desc) 571 memset(desc, 0, sizeof(*desc)); 572 } 573 574 static inline bool ctx_id_mapped(struct intel_guc *guc, u32 id) 575 { 576 return __get_context(guc, id); 577 } 578 579 static inline void set_ctx_id_mapping(struct intel_guc *guc, u32 id, 580 struct intel_context *ce) 581 { 582 unsigned long flags; 583 584 /* 585 * xarray API doesn't have xa_save_irqsave wrapper, so calling the 586 * lower level functions directly. 587 */ 588 xa_lock_irqsave(&guc->context_lookup, flags); 589 __xa_store(&guc->context_lookup, id, ce, GFP_ATOMIC); 590 xa_unlock_irqrestore(&guc->context_lookup, flags); 591 } 592 593 static inline void clr_ctx_id_mapping(struct intel_guc *guc, u32 id) 594 { 595 unsigned long flags; 596 597 if (unlikely(!guc_submission_initialized(guc))) 598 return; 599 600 _reset_lrc_desc_v69(guc, id); 601 602 /* 603 * xarray API doesn't have xa_erase_irqsave wrapper, so calling 604 * the lower level functions directly. 605 */ 606 xa_lock_irqsave(&guc->context_lookup, flags); 607 __xa_erase(&guc->context_lookup, id); 608 xa_unlock_irqrestore(&guc->context_lookup, flags); 609 } 610 611 static void decr_outstanding_submission_g2h(struct intel_guc *guc) 612 { 613 if (atomic_dec_and_test(&guc->outstanding_submission_g2h)) 614 wake_up_all(&guc->ct.wq); 615 } 616 617 static int guc_submission_send_busy_loop(struct intel_guc *guc, 618 const u32 *action, 619 u32 len, 620 u32 g2h_len_dw, 621 bool loop) 622 { 623 int ret; 624 625 /* 626 * We always loop when a send requires a reply (i.e. g2h_len_dw > 0), 627 * so we don't handle the case where we don't get a reply because we 628 * aborted the send due to the channel being busy. 629 */ 630 GEM_BUG_ON(g2h_len_dw && !loop); 631 632 if (g2h_len_dw) 633 atomic_inc(&guc->outstanding_submission_g2h); 634 635 ret = intel_guc_send_busy_loop(guc, action, len, g2h_len_dw, loop); 636 if (ret) 637 atomic_dec(&guc->outstanding_submission_g2h); 638 639 return ret; 640 } 641 642 int intel_guc_wait_for_pending_msg(struct intel_guc *guc, 643 atomic_t *wait_var, 644 bool interruptible, 645 long timeout) 646 { 647 const int state = interruptible ? 648 TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE; 649 DEFINE_WAIT(wait); 650 651 might_sleep(); 652 GEM_BUG_ON(timeout < 0); 653 654 if (!atomic_read(wait_var)) 655 return 0; 656 657 if (!timeout) 658 return -ETIME; 659 660 for (;;) { 661 prepare_to_wait(&guc->ct.wq, &wait, state); 662 663 if (!atomic_read(wait_var)) 664 break; 665 666 if (signal_pending_state(state, current)) { 667 timeout = -EINTR; 668 break; 669 } 670 671 if (!timeout) { 672 timeout = -ETIME; 673 break; 674 } 675 676 timeout = io_schedule_timeout(timeout); 677 } 678 finish_wait(&guc->ct.wq, &wait); 679 680 return (timeout < 0) ? timeout : 0; 681 } 682 683 int intel_guc_wait_for_idle(struct intel_guc *guc, long timeout) 684 { 685 if (!intel_uc_uses_guc_submission(&guc_to_gt(guc)->uc)) 686 return 0; 687 688 return intel_guc_wait_for_pending_msg(guc, 689 &guc->outstanding_submission_g2h, 690 true, timeout); 691 } 692 693 static int guc_context_policy_init_v70(struct intel_context *ce, bool loop); 694 static int try_context_registration(struct intel_context *ce, bool loop); 695 696 static int __guc_add_request(struct intel_guc *guc, struct i915_request *rq) 697 { 698 int err = 0; 699 struct intel_context *ce = request_to_scheduling_context(rq); 700 u32 action[3]; 701 int len = 0; 702 u32 g2h_len_dw = 0; 703 bool enabled; 704 705 lockdep_assert_held(&rq->engine->sched_engine->lock); 706 707 /* 708 * Corner case where requests were sitting in the priority list or a 709 * request resubmitted after the context was banned. 710 */ 711 if (unlikely(!intel_context_is_schedulable(ce))) { 712 i915_request_put(i915_request_mark_eio(rq)); 713 intel_engine_signal_breadcrumbs(ce->engine); 714 return 0; 715 } 716 717 GEM_BUG_ON(!atomic_read(&ce->guc_id.ref)); 718 GEM_BUG_ON(context_guc_id_invalid(ce)); 719 720 if (context_policy_required(ce)) { 721 err = guc_context_policy_init_v70(ce, false); 722 if (err) 723 return err; 724 } 725 726 spin_lock(&ce->guc_state.lock); 727 728 /* 729 * The request / context will be run on the hardware when scheduling 730 * gets enabled in the unblock. For multi-lrc we still submit the 731 * context to move the LRC tails. 732 */ 733 if (unlikely(context_blocked(ce) && !intel_context_is_parent(ce))) 734 goto out; 735 736 enabled = context_enabled(ce) || context_blocked(ce); 737 738 if (!enabled) { 739 action[len++] = INTEL_GUC_ACTION_SCHED_CONTEXT_MODE_SET; 740 action[len++] = ce->guc_id.id; 741 action[len++] = GUC_CONTEXT_ENABLE; 742 set_context_pending_enable(ce); 743 intel_context_get(ce); 744 g2h_len_dw = G2H_LEN_DW_SCHED_CONTEXT_MODE_SET; 745 } else { 746 action[len++] = INTEL_GUC_ACTION_SCHED_CONTEXT; 747 action[len++] = ce->guc_id.id; 748 } 749 750 err = intel_guc_send_nb(guc, action, len, g2h_len_dw); 751 if (!enabled && !err) { 752 trace_intel_context_sched_enable(ce); 753 atomic_inc(&guc->outstanding_submission_g2h); 754 set_context_enabled(ce); 755 756 /* 757 * Without multi-lrc KMD does the submission step (moving the 758 * lrc tail) so enabling scheduling is sufficient to submit the 759 * context. This isn't the case in multi-lrc submission as the 760 * GuC needs to move the tails, hence the need for another H2G 761 * to submit a multi-lrc context after enabling scheduling. 762 */ 763 if (intel_context_is_parent(ce)) { 764 action[0] = INTEL_GUC_ACTION_SCHED_CONTEXT; 765 err = intel_guc_send_nb(guc, action, len - 1, 0); 766 } 767 } else if (!enabled) { 768 clr_context_pending_enable(ce); 769 intel_context_put(ce); 770 } 771 if (likely(!err)) 772 trace_i915_request_guc_submit(rq); 773 774 out: 775 spin_unlock(&ce->guc_state.lock); 776 return err; 777 } 778 779 static int guc_add_request(struct intel_guc *guc, struct i915_request *rq) 780 { 781 int ret = __guc_add_request(guc, rq); 782 783 if (unlikely(ret == -EBUSY)) { 784 guc->stalled_request = rq; 785 guc->submission_stall_reason = STALL_ADD_REQUEST; 786 } 787 788 return ret; 789 } 790 791 static inline void guc_set_lrc_tail(struct i915_request *rq) 792 { 793 rq->context->lrc_reg_state[CTX_RING_TAIL] = 794 intel_ring_set_tail(rq->ring, rq->tail); 795 } 796 797 static inline int rq_prio(const struct i915_request *rq) 798 { 799 return rq->sched.attr.priority; 800 } 801 802 static bool is_multi_lrc_rq(struct i915_request *rq) 803 { 804 return intel_context_is_parallel(rq->context); 805 } 806 807 static bool can_merge_rq(struct i915_request *rq, 808 struct i915_request *last) 809 { 810 return request_to_scheduling_context(rq) == 811 request_to_scheduling_context(last); 812 } 813 814 static u32 wq_space_until_wrap(struct intel_context *ce) 815 { 816 return (WQ_SIZE - ce->parallel.guc.wqi_tail); 817 } 818 819 static void write_wqi(struct intel_context *ce, u32 wqi_size) 820 { 821 BUILD_BUG_ON(!is_power_of_2(WQ_SIZE)); 822 823 /* 824 * Ensure WQI are visible before updating tail 825 */ 826 intel_guc_write_barrier(ce_to_guc(ce)); 827 828 ce->parallel.guc.wqi_tail = (ce->parallel.guc.wqi_tail + wqi_size) & 829 (WQ_SIZE - 1); 830 WRITE_ONCE(*ce->parallel.guc.wq_tail, ce->parallel.guc.wqi_tail); 831 } 832 833 static int guc_wq_noop_append(struct intel_context *ce) 834 { 835 u32 *wqi = get_wq_pointer(ce, wq_space_until_wrap(ce)); 836 u32 len_dw = wq_space_until_wrap(ce) / sizeof(u32) - 1; 837 838 if (!wqi) 839 return -EBUSY; 840 841 GEM_BUG_ON(!FIELD_FIT(WQ_LEN_MASK, len_dw)); 842 843 *wqi = FIELD_PREP(WQ_TYPE_MASK, WQ_TYPE_NOOP) | 844 FIELD_PREP(WQ_LEN_MASK, len_dw); 845 ce->parallel.guc.wqi_tail = 0; 846 847 return 0; 848 } 849 850 static int __guc_wq_item_append(struct i915_request *rq) 851 { 852 struct intel_context *ce = request_to_scheduling_context(rq); 853 struct intel_context *child; 854 unsigned int wqi_size = (ce->parallel.number_children + 4) * 855 sizeof(u32); 856 u32 *wqi; 857 u32 len_dw = (wqi_size / sizeof(u32)) - 1; 858 int ret; 859 860 /* Ensure context is in correct state updating work queue */ 861 GEM_BUG_ON(!atomic_read(&ce->guc_id.ref)); 862 GEM_BUG_ON(context_guc_id_invalid(ce)); 863 GEM_BUG_ON(context_wait_for_deregister_to_register(ce)); 864 GEM_BUG_ON(!ctx_id_mapped(ce_to_guc(ce), ce->guc_id.id)); 865 866 /* Insert NOOP if this work queue item will wrap the tail pointer. */ 867 if (wqi_size > wq_space_until_wrap(ce)) { 868 ret = guc_wq_noop_append(ce); 869 if (ret) 870 return ret; 871 } 872 873 wqi = get_wq_pointer(ce, wqi_size); 874 if (!wqi) 875 return -EBUSY; 876 877 GEM_BUG_ON(!FIELD_FIT(WQ_LEN_MASK, len_dw)); 878 879 *wqi++ = FIELD_PREP(WQ_TYPE_MASK, WQ_TYPE_MULTI_LRC) | 880 FIELD_PREP(WQ_LEN_MASK, len_dw); 881 *wqi++ = ce->lrc.lrca; 882 *wqi++ = FIELD_PREP(WQ_GUC_ID_MASK, ce->guc_id.id) | 883 FIELD_PREP(WQ_RING_TAIL_MASK, ce->ring->tail / sizeof(u64)); 884 *wqi++ = 0; /* fence_id */ 885 for_each_child(ce, child) 886 *wqi++ = child->ring->tail / sizeof(u64); 887 888 write_wqi(ce, wqi_size); 889 890 return 0; 891 } 892 893 static int guc_wq_item_append(struct intel_guc *guc, 894 struct i915_request *rq) 895 { 896 struct intel_context *ce = request_to_scheduling_context(rq); 897 int ret; 898 899 if (unlikely(!intel_context_is_schedulable(ce))) 900 return 0; 901 902 ret = __guc_wq_item_append(rq); 903 if (unlikely(ret == -EBUSY)) { 904 guc->stalled_request = rq; 905 guc->submission_stall_reason = STALL_MOVE_LRC_TAIL; 906 } 907 908 return ret; 909 } 910 911 static bool multi_lrc_submit(struct i915_request *rq) 912 { 913 struct intel_context *ce = request_to_scheduling_context(rq); 914 915 intel_ring_set_tail(rq->ring, rq->tail); 916 917 /* 918 * We expect the front end (execbuf IOCTL) to set this flag on the last 919 * request generated from a multi-BB submission. This indicates to the 920 * backend (GuC interface) that we should submit this context thus 921 * submitting all the requests generated in parallel. 922 */ 923 return test_bit(I915_FENCE_FLAG_SUBMIT_PARALLEL, &rq->fence.flags) || 924 !intel_context_is_schedulable(ce); 925 } 926 927 static int guc_dequeue_one_context(struct intel_guc *guc) 928 { 929 struct i915_sched_engine * const sched_engine = guc->sched_engine; 930 struct i915_request *last = NULL; 931 bool submit = false; 932 struct rb_node *rb; 933 int ret; 934 935 lockdep_assert_held(&sched_engine->lock); 936 937 if (guc->stalled_request) { 938 submit = true; 939 last = guc->stalled_request; 940 941 switch (guc->submission_stall_reason) { 942 case STALL_REGISTER_CONTEXT: 943 goto register_context; 944 case STALL_MOVE_LRC_TAIL: 945 goto move_lrc_tail; 946 case STALL_ADD_REQUEST: 947 goto add_request; 948 default: 949 MISSING_CASE(guc->submission_stall_reason); 950 } 951 } 952 953 while ((rb = rb_first_cached(&sched_engine->queue))) { 954 struct i915_priolist *p = to_priolist(rb); 955 struct i915_request *rq, *rn; 956 957 priolist_for_each_request_consume(rq, rn, p) { 958 if (last && !can_merge_rq(rq, last)) 959 goto register_context; 960 961 list_del_init(&rq->sched.link); 962 963 __i915_request_submit(rq); 964 965 trace_i915_request_in(rq, 0); 966 last = rq; 967 968 if (is_multi_lrc_rq(rq)) { 969 /* 970 * We need to coalesce all multi-lrc requests in 971 * a relationship into a single H2G. We are 972 * guaranteed that all of these requests will be 973 * submitted sequentially. 974 */ 975 if (multi_lrc_submit(rq)) { 976 submit = true; 977 goto register_context; 978 } 979 } else { 980 submit = true; 981 } 982 } 983 984 rb_erase_cached(&p->node, &sched_engine->queue); 985 i915_priolist_free(p); 986 } 987 988 register_context: 989 if (submit) { 990 struct intel_context *ce = request_to_scheduling_context(last); 991 992 if (unlikely(!ctx_id_mapped(guc, ce->guc_id.id) && 993 intel_context_is_schedulable(ce))) { 994 ret = try_context_registration(ce, false); 995 if (unlikely(ret == -EPIPE)) { 996 goto deadlk; 997 } else if (ret == -EBUSY) { 998 guc->stalled_request = last; 999 guc->submission_stall_reason = 1000 STALL_REGISTER_CONTEXT; 1001 goto schedule_tasklet; 1002 } else if (ret != 0) { 1003 GEM_WARN_ON(ret); /* Unexpected */ 1004 goto deadlk; 1005 } 1006 } 1007 1008 move_lrc_tail: 1009 if (is_multi_lrc_rq(last)) { 1010 ret = guc_wq_item_append(guc, last); 1011 if (ret == -EBUSY) { 1012 goto schedule_tasklet; 1013 } else if (ret != 0) { 1014 GEM_WARN_ON(ret); /* Unexpected */ 1015 goto deadlk; 1016 } 1017 } else { 1018 guc_set_lrc_tail(last); 1019 } 1020 1021 add_request: 1022 ret = guc_add_request(guc, last); 1023 if (unlikely(ret == -EPIPE)) { 1024 goto deadlk; 1025 } else if (ret == -EBUSY) { 1026 goto schedule_tasklet; 1027 } else if (ret != 0) { 1028 GEM_WARN_ON(ret); /* Unexpected */ 1029 goto deadlk; 1030 } 1031 } 1032 1033 guc->stalled_request = NULL; 1034 guc->submission_stall_reason = STALL_NONE; 1035 return submit; 1036 1037 deadlk: 1038 sched_engine->tasklet.callback = NULL; 1039 tasklet_disable_nosync(&sched_engine->tasklet); 1040 return false; 1041 1042 schedule_tasklet: 1043 tasklet_schedule(&sched_engine->tasklet); 1044 return false; 1045 } 1046 1047 static void guc_submission_tasklet(struct tasklet_struct *t) 1048 { 1049 struct i915_sched_engine *sched_engine = 1050 from_tasklet(sched_engine, t, tasklet); 1051 unsigned long flags; 1052 bool loop; 1053 1054 spin_lock_irqsave(&sched_engine->lock, flags); 1055 1056 do { 1057 loop = guc_dequeue_one_context(sched_engine->private_data); 1058 } while (loop); 1059 1060 i915_sched_engine_reset_on_empty(sched_engine); 1061 1062 spin_unlock_irqrestore(&sched_engine->lock, flags); 1063 } 1064 1065 static void cs_irq_handler(struct intel_engine_cs *engine, u16 iir) 1066 { 1067 if (iir & GT_RENDER_USER_INTERRUPT) 1068 intel_engine_signal_breadcrumbs(engine); 1069 } 1070 1071 static void __guc_context_destroy(struct intel_context *ce); 1072 static void release_guc_id(struct intel_guc *guc, struct intel_context *ce); 1073 static void guc_signal_context_fence(struct intel_context *ce); 1074 static void guc_cancel_context_requests(struct intel_context *ce); 1075 static void guc_blocked_fence_complete(struct intel_context *ce); 1076 1077 static void scrub_guc_desc_for_outstanding_g2h(struct intel_guc *guc) 1078 { 1079 struct intel_context *ce; 1080 unsigned long index, flags; 1081 bool pending_disable, pending_enable, deregister, destroyed, banned; 1082 1083 xa_lock_irqsave(&guc->context_lookup, flags); 1084 xa_for_each(&guc->context_lookup, index, ce) { 1085 /* 1086 * Corner case where the ref count on the object is zero but and 1087 * deregister G2H was lost. In this case we don't touch the ref 1088 * count and finish the destroy of the context. 1089 */ 1090 bool do_put = kref_get_unless_zero(&ce->ref); 1091 1092 xa_unlock(&guc->context_lookup); 1093 1094 if (test_bit(CONTEXT_GUC_INIT, &ce->flags) && 1095 (cancel_delayed_work(&ce->guc_state.sched_disable_delay_work))) { 1096 /* successful cancel so jump straight to close it */ 1097 intel_context_sched_disable_unpin(ce); 1098 } 1099 1100 spin_lock(&ce->guc_state.lock); 1101 1102 /* 1103 * Once we are at this point submission_disabled() is guaranteed 1104 * to be visible to all callers who set the below flags (see above 1105 * flush and flushes in reset_prepare). If submission_disabled() 1106 * is set, the caller shouldn't set these flags. 1107 */ 1108 1109 destroyed = context_destroyed(ce); 1110 pending_enable = context_pending_enable(ce); 1111 pending_disable = context_pending_disable(ce); 1112 deregister = context_wait_for_deregister_to_register(ce); 1113 banned = context_banned(ce); 1114 init_sched_state(ce); 1115 1116 spin_unlock(&ce->guc_state.lock); 1117 1118 if (pending_enable || destroyed || deregister) { 1119 decr_outstanding_submission_g2h(guc); 1120 if (deregister) 1121 guc_signal_context_fence(ce); 1122 if (destroyed) { 1123 intel_gt_pm_put_async_untracked(guc_to_gt(guc)); 1124 release_guc_id(guc, ce); 1125 __guc_context_destroy(ce); 1126 } 1127 if (pending_enable || deregister) 1128 intel_context_put(ce); 1129 } 1130 1131 /* Not mutualy exclusive with above if statement. */ 1132 if (pending_disable) { 1133 guc_signal_context_fence(ce); 1134 if (banned) { 1135 guc_cancel_context_requests(ce); 1136 intel_engine_signal_breadcrumbs(ce->engine); 1137 } 1138 intel_context_sched_disable_unpin(ce); 1139 decr_outstanding_submission_g2h(guc); 1140 1141 spin_lock(&ce->guc_state.lock); 1142 guc_blocked_fence_complete(ce); 1143 spin_unlock(&ce->guc_state.lock); 1144 1145 intel_context_put(ce); 1146 } 1147 1148 if (do_put) 1149 intel_context_put(ce); 1150 xa_lock(&guc->context_lookup); 1151 } 1152 xa_unlock_irqrestore(&guc->context_lookup, flags); 1153 } 1154 1155 /* 1156 * GuC stores busyness stats for each engine at context in/out boundaries. A 1157 * context 'in' logs execution start time, 'out' adds in -> out delta to total. 1158 * i915/kmd accesses 'start', 'total' and 'context id' from memory shared with 1159 * GuC. 1160 * 1161 * __i915_pmu_event_read samples engine busyness. When sampling, if context id 1162 * is valid (!= ~0) and start is non-zero, the engine is considered to be 1163 * active. For an active engine total busyness = total + (now - start), where 1164 * 'now' is the time at which the busyness is sampled. For inactive engine, 1165 * total busyness = total. 1166 * 1167 * All times are captured from GUCPMTIMESTAMP reg and are in gt clock domain. 1168 * 1169 * The start and total values provided by GuC are 32 bits and wrap around in a 1170 * few minutes. Since perf pmu provides busyness as 64 bit monotonically 1171 * increasing ns values, there is a need for this implementation to account for 1172 * overflows and extend the GuC provided values to 64 bits before returning 1173 * busyness to the user. In order to do that, a worker runs periodically at 1174 * frequency = 1/8th the time it takes for the timestamp to wrap (i.e. once in 1175 * 27 seconds for a gt clock frequency of 19.2 MHz). 1176 */ 1177 1178 #define WRAP_TIME_CLKS U32_MAX 1179 #define POLL_TIME_CLKS (WRAP_TIME_CLKS >> 3) 1180 1181 static void 1182 __extend_last_switch(struct intel_guc *guc, u64 *prev_start, u32 new_start) 1183 { 1184 u32 gt_stamp_hi = upper_32_bits(guc->timestamp.gt_stamp); 1185 u32 gt_stamp_last = lower_32_bits(guc->timestamp.gt_stamp); 1186 1187 if (new_start == lower_32_bits(*prev_start)) 1188 return; 1189 1190 /* 1191 * When gt is unparked, we update the gt timestamp and start the ping 1192 * worker that updates the gt_stamp every POLL_TIME_CLKS. As long as gt 1193 * is unparked, all switched in contexts will have a start time that is 1194 * within +/- POLL_TIME_CLKS of the most recent gt_stamp. 1195 * 1196 * If neither gt_stamp nor new_start has rolled over, then the 1197 * gt_stamp_hi does not need to be adjusted, however if one of them has 1198 * rolled over, we need to adjust gt_stamp_hi accordingly. 1199 * 1200 * The below conditions address the cases of new_start rollover and 1201 * gt_stamp_last rollover respectively. 1202 */ 1203 if (new_start < gt_stamp_last && 1204 (new_start - gt_stamp_last) <= POLL_TIME_CLKS) 1205 gt_stamp_hi++; 1206 1207 if (new_start > gt_stamp_last && 1208 (gt_stamp_last - new_start) <= POLL_TIME_CLKS && gt_stamp_hi) 1209 gt_stamp_hi--; 1210 1211 *prev_start = ((u64)gt_stamp_hi << 32) | new_start; 1212 } 1213 1214 #define record_read(map_, field_) \ 1215 iosys_map_rd_field(map_, 0, struct guc_engine_usage_record, field_) 1216 1217 /* 1218 * GuC updates shared memory and KMD reads it. Since this is not synchronized, 1219 * we run into a race where the value read is inconsistent. Sometimes the 1220 * inconsistency is in reading the upper MSB bytes of the last_in value when 1221 * this race occurs. 2 types of cases are seen - upper 8 bits are zero and upper 1222 * 24 bits are zero. Since these are non-zero values, it is non-trivial to 1223 * determine validity of these values. Instead we read the values multiple times 1224 * until they are consistent. In test runs, 3 attempts results in consistent 1225 * values. The upper bound is set to 6 attempts and may need to be tuned as per 1226 * any new occurences. 1227 */ 1228 static void __get_engine_usage_record(struct intel_engine_cs *engine, 1229 u32 *last_in, u32 *id, u32 *total) 1230 { 1231 struct iosys_map rec_map = intel_guc_engine_usage_record_map(engine); 1232 int i = 0; 1233 1234 do { 1235 *last_in = record_read(&rec_map, last_switch_in_stamp); 1236 *id = record_read(&rec_map, current_context_index); 1237 *total = record_read(&rec_map, total_runtime); 1238 1239 if (record_read(&rec_map, last_switch_in_stamp) == *last_in && 1240 record_read(&rec_map, current_context_index) == *id && 1241 record_read(&rec_map, total_runtime) == *total) 1242 break; 1243 } while (++i < 6); 1244 } 1245 1246 static void guc_update_engine_gt_clks(struct intel_engine_cs *engine) 1247 { 1248 struct intel_engine_guc_stats *stats = &engine->stats.guc; 1249 struct intel_guc *guc = &engine->gt->uc.guc; 1250 u32 last_switch, ctx_id, total; 1251 1252 lockdep_assert_held(&guc->timestamp.lock); 1253 1254 __get_engine_usage_record(engine, &last_switch, &ctx_id, &total); 1255 1256 stats->running = ctx_id != ~0U && last_switch; 1257 if (stats->running) 1258 __extend_last_switch(guc, &stats->start_gt_clk, last_switch); 1259 1260 /* 1261 * Instead of adjusting the total for overflow, just add the 1262 * difference from previous sample stats->total_gt_clks 1263 */ 1264 if (total && total != ~0U) { 1265 stats->total_gt_clks += (u32)(total - stats->prev_total); 1266 stats->prev_total = total; 1267 } 1268 } 1269 1270 static u32 gpm_timestamp_shift(struct intel_gt *gt) 1271 { 1272 intel_wakeref_t wakeref; 1273 u32 reg, shift; 1274 1275 with_intel_runtime_pm(gt->uncore->rpm, wakeref) 1276 reg = intel_uncore_read(gt->uncore, RPM_CONFIG0); 1277 1278 shift = (reg & GEN10_RPM_CONFIG0_CTC_SHIFT_PARAMETER_MASK) >> 1279 GEN10_RPM_CONFIG0_CTC_SHIFT_PARAMETER_SHIFT; 1280 1281 return 3 - shift; 1282 } 1283 1284 static void guc_update_pm_timestamp(struct intel_guc *guc, ktime_t *now) 1285 { 1286 struct intel_gt *gt = guc_to_gt(guc); 1287 u32 gt_stamp_lo, gt_stamp_hi; 1288 u64 gpm_ts; 1289 1290 lockdep_assert_held(&guc->timestamp.lock); 1291 1292 gt_stamp_hi = upper_32_bits(guc->timestamp.gt_stamp); 1293 gpm_ts = intel_uncore_read64_2x32(gt->uncore, MISC_STATUS0, 1294 MISC_STATUS1) >> guc->timestamp.shift; 1295 gt_stamp_lo = lower_32_bits(gpm_ts); 1296 *now = ktime_get(); 1297 1298 if (gt_stamp_lo < lower_32_bits(guc->timestamp.gt_stamp)) 1299 gt_stamp_hi++; 1300 1301 guc->timestamp.gt_stamp = ((u64)gt_stamp_hi << 32) | gt_stamp_lo; 1302 } 1303 1304 /* 1305 * Unlike the execlist mode of submission total and active times are in terms of 1306 * gt clocks. The *now parameter is retained to return the cpu time at which the 1307 * busyness was sampled. 1308 */ 1309 static ktime_t guc_engine_busyness(struct intel_engine_cs *engine, ktime_t *now) 1310 { 1311 struct intel_engine_guc_stats stats_saved, *stats = &engine->stats.guc; 1312 struct i915_gpu_error *gpu_error = &engine->i915->gpu_error; 1313 struct intel_gt *gt = engine->gt; 1314 struct intel_guc *guc = >->uc.guc; 1315 u64 total, gt_stamp_saved; 1316 unsigned long flags; 1317 u32 reset_count; 1318 bool in_reset; 1319 intel_wakeref_t wakeref; 1320 1321 spin_lock_irqsave(&guc->timestamp.lock, flags); 1322 1323 /* 1324 * If a reset happened, we risk reading partially updated engine 1325 * busyness from GuC, so we just use the driver stored copy of busyness. 1326 * Synchronize with gt reset using reset_count and the 1327 * I915_RESET_BACKOFF flag. Note that reset flow updates the reset_count 1328 * after I915_RESET_BACKOFF flag, so ensure that the reset_count is 1329 * usable by checking the flag afterwards. 1330 */ 1331 reset_count = i915_reset_count(gpu_error); 1332 in_reset = test_bit(I915_RESET_BACKOFF, >->reset.flags); 1333 1334 *now = ktime_get(); 1335 1336 /* 1337 * The active busyness depends on start_gt_clk and gt_stamp. 1338 * gt_stamp is updated by i915 only when gt is awake and the 1339 * start_gt_clk is derived from GuC state. To get a consistent 1340 * view of activity, we query the GuC state only if gt is awake. 1341 */ 1342 wakeref = in_reset ? 0 : intel_gt_pm_get_if_awake(gt); 1343 if (wakeref) { 1344 stats_saved = *stats; 1345 gt_stamp_saved = guc->timestamp.gt_stamp; 1346 /* 1347 * Update gt_clks, then gt timestamp to simplify the 'gt_stamp - 1348 * start_gt_clk' calculation below for active engines. 1349 */ 1350 guc_update_engine_gt_clks(engine); 1351 guc_update_pm_timestamp(guc, now); 1352 intel_gt_pm_put_async(gt, wakeref); 1353 if (i915_reset_count(gpu_error) != reset_count) { 1354 *stats = stats_saved; 1355 guc->timestamp.gt_stamp = gt_stamp_saved; 1356 } 1357 } 1358 1359 total = intel_gt_clock_interval_to_ns(gt, stats->total_gt_clks); 1360 if (stats->running) { 1361 u64 clk = guc->timestamp.gt_stamp - stats->start_gt_clk; 1362 1363 total += intel_gt_clock_interval_to_ns(gt, clk); 1364 } 1365 1366 spin_unlock_irqrestore(&guc->timestamp.lock, flags); 1367 1368 return ns_to_ktime(total); 1369 } 1370 1371 static void guc_enable_busyness_worker(struct intel_guc *guc) 1372 { 1373 mod_delayed_work(system_highpri_wq, &guc->timestamp.work, guc->timestamp.ping_delay); 1374 } 1375 1376 static void guc_cancel_busyness_worker(struct intel_guc *guc) 1377 { 1378 /* 1379 * There are many different call stacks that can get here. Some of them 1380 * hold the reset mutex. The busyness worker also attempts to acquire the 1381 * reset mutex. Synchronously flushing a worker thread requires acquiring 1382 * the worker mutex. Lockdep sees this as a conflict. It thinks that the 1383 * flush can deadlock because it holds the worker mutex while waiting for 1384 * the reset mutex, but another thread is holding the reset mutex and might 1385 * attempt to use other worker functions. 1386 * 1387 * In practice, this scenario does not exist because the busyness worker 1388 * does not block waiting for the reset mutex. It does a try-lock on it and 1389 * immediately exits if the lock is already held. Unfortunately, the mutex 1390 * in question (I915_RESET_BACKOFF) is an i915 implementation which has lockdep 1391 * annotation but not to the extent of explaining the 'might lock' is also a 1392 * 'does not need to lock'. So one option would be to add more complex lockdep 1393 * annotations to ignore the issue (if at all possible). A simpler option is to 1394 * just not flush synchronously when a rest in progress. Given that the worker 1395 * will just early exit and re-schedule itself anyway, there is no advantage 1396 * to running it immediately. 1397 * 1398 * If a reset is not in progress, then the synchronous flush may be required. 1399 * As noted many call stacks lead here, some during suspend and driver unload 1400 * which do require a synchronous flush to make sure the worker is stopped 1401 * before memory is freed. 1402 * 1403 * Trying to pass a 'need_sync' or 'in_reset' flag all the way down through 1404 * every possible call stack is unfeasible. It would be too intrusive to many 1405 * areas that really don't care about the GuC backend. However, there is the 1406 * I915_RESET_BACKOFF flag and the gt->reset.mutex can be tested for is_locked. 1407 * So just use those. Note that testing both is required due to the hideously 1408 * complex nature of the i915 driver's reset code paths. 1409 * 1410 * And note that in the case of a reset occurring during driver unload 1411 * (wedged_on_fini), skipping the cancel in reset_prepare/reset_fini (when the 1412 * reset flag/mutex are set) is fine because there is another explicit cancel in 1413 * intel_guc_submission_fini (when the reset flag/mutex are not). 1414 */ 1415 if (mutex_is_locked(&guc_to_gt(guc)->reset.mutex) || 1416 test_bit(I915_RESET_BACKOFF, &guc_to_gt(guc)->reset.flags)) 1417 cancel_delayed_work(&guc->timestamp.work); 1418 else 1419 cancel_delayed_work_sync(&guc->timestamp.work); 1420 } 1421 1422 static void __reset_guc_busyness_stats(struct intel_guc *guc) 1423 { 1424 struct intel_gt *gt = guc_to_gt(guc); 1425 struct intel_engine_cs *engine; 1426 enum intel_engine_id id; 1427 unsigned long flags; 1428 ktime_t unused; 1429 1430 spin_lock_irqsave(&guc->timestamp.lock, flags); 1431 1432 guc_update_pm_timestamp(guc, &unused); 1433 for_each_engine(engine, gt, id) { 1434 guc_update_engine_gt_clks(engine); 1435 engine->stats.guc.prev_total = 0; 1436 } 1437 1438 spin_unlock_irqrestore(&guc->timestamp.lock, flags); 1439 } 1440 1441 static void __update_guc_busyness_stats(struct intel_guc *guc) 1442 { 1443 struct intel_gt *gt = guc_to_gt(guc); 1444 struct intel_engine_cs *engine; 1445 enum intel_engine_id id; 1446 unsigned long flags; 1447 ktime_t unused; 1448 1449 guc->timestamp.last_stat_jiffies = jiffies; 1450 1451 spin_lock_irqsave(&guc->timestamp.lock, flags); 1452 1453 guc_update_pm_timestamp(guc, &unused); 1454 for_each_engine(engine, gt, id) 1455 guc_update_engine_gt_clks(engine); 1456 1457 spin_unlock_irqrestore(&guc->timestamp.lock, flags); 1458 } 1459 1460 static void __guc_context_update_stats(struct intel_context *ce) 1461 { 1462 struct intel_guc *guc = ce_to_guc(ce); 1463 unsigned long flags; 1464 1465 spin_lock_irqsave(&guc->timestamp.lock, flags); 1466 lrc_update_runtime(ce); 1467 spin_unlock_irqrestore(&guc->timestamp.lock, flags); 1468 } 1469 1470 static void guc_context_update_stats(struct intel_context *ce) 1471 { 1472 if (!intel_context_pin_if_active(ce)) 1473 return; 1474 1475 __guc_context_update_stats(ce); 1476 intel_context_unpin(ce); 1477 } 1478 1479 static void guc_timestamp_ping(struct work_struct *wrk) 1480 { 1481 struct intel_guc *guc = container_of(wrk, typeof(*guc), 1482 timestamp.work.work); 1483 struct intel_uc *uc = container_of(guc, typeof(*uc), guc); 1484 struct intel_gt *gt = guc_to_gt(guc); 1485 struct intel_context *ce; 1486 intel_wakeref_t wakeref; 1487 unsigned long index; 1488 int srcu, ret; 1489 1490 /* 1491 * Ideally the busyness worker should take a gt pm wakeref because the 1492 * worker only needs to be active while gt is awake. However, the 1493 * gt_park path cancels the worker synchronously and this complicates 1494 * the flow if the worker is also running at the same time. The cancel 1495 * waits for the worker and when the worker releases the wakeref, that 1496 * would call gt_park and would lead to a deadlock. 1497 * 1498 * The resolution is to take the global pm wakeref if runtime pm is 1499 * already active. If not, we don't need to update the busyness stats as 1500 * the stats would already be updated when the gt was parked. 1501 * 1502 * Note: 1503 * - We do not requeue the worker if we cannot take a reference to runtime 1504 * pm since intel_guc_busyness_unpark would requeue the worker in the 1505 * resume path. 1506 * 1507 * - If the gt was parked longer than time taken for GT timestamp to roll 1508 * over, we ignore those rollovers since we don't care about tracking 1509 * the exact GT time. We only care about roll overs when the gt is 1510 * active and running workloads. 1511 * 1512 * - There is a window of time between gt_park and runtime suspend, 1513 * where the worker may run. This is acceptable since the worker will 1514 * not find any new data to update busyness. 1515 */ 1516 wakeref = intel_runtime_pm_get_if_active(>->i915->runtime_pm); 1517 if (!wakeref) 1518 return; 1519 1520 /* 1521 * Synchronize with gt reset to make sure the worker does not 1522 * corrupt the engine/guc stats. NB: can't actually block waiting 1523 * for a reset to complete as the reset requires flushing out 1524 * this worker thread if started. So waiting would deadlock. 1525 */ 1526 ret = intel_gt_reset_trylock(gt, &srcu); 1527 if (ret) 1528 goto err_trylock; 1529 1530 __update_guc_busyness_stats(guc); 1531 1532 /* adjust context stats for overflow */ 1533 xa_for_each(&guc->context_lookup, index, ce) 1534 guc_context_update_stats(ce); 1535 1536 intel_gt_reset_unlock(gt, srcu); 1537 1538 guc_enable_busyness_worker(guc); 1539 1540 err_trylock: 1541 intel_runtime_pm_put(>->i915->runtime_pm, wakeref); 1542 } 1543 1544 static int guc_action_enable_usage_stats(struct intel_guc *guc) 1545 { 1546 u32 offset = intel_guc_engine_usage_offset(guc); 1547 u32 action[] = { 1548 INTEL_GUC_ACTION_SET_ENG_UTIL_BUFF, 1549 offset, 1550 0, 1551 }; 1552 1553 return intel_guc_send(guc, action, ARRAY_SIZE(action)); 1554 } 1555 1556 static int guc_init_engine_stats(struct intel_guc *guc) 1557 { 1558 struct intel_gt *gt = guc_to_gt(guc); 1559 intel_wakeref_t wakeref; 1560 int ret; 1561 1562 with_intel_runtime_pm(>->i915->runtime_pm, wakeref) 1563 ret = guc_action_enable_usage_stats(guc); 1564 1565 if (ret) 1566 guc_err(guc, "Failed to enable usage stats: %pe\n", ERR_PTR(ret)); 1567 else 1568 guc_enable_busyness_worker(guc); 1569 1570 return ret; 1571 } 1572 1573 static void guc_fini_engine_stats(struct intel_guc *guc) 1574 { 1575 guc_cancel_busyness_worker(guc); 1576 } 1577 1578 void intel_guc_busyness_park(struct intel_gt *gt) 1579 { 1580 struct intel_guc *guc = >->uc.guc; 1581 1582 if (!guc_submission_initialized(guc)) 1583 return; 1584 1585 /* 1586 * There is a race with suspend flow where the worker runs after suspend 1587 * and causes an unclaimed register access warning. Cancel the worker 1588 * synchronously here. 1589 */ 1590 guc_cancel_busyness_worker(guc); 1591 1592 /* 1593 * Before parking, we should sample engine busyness stats if we need to. 1594 * We can skip it if we are less than half a ping from the last time we 1595 * sampled the busyness stats. 1596 */ 1597 if (guc->timestamp.last_stat_jiffies && 1598 !time_after(jiffies, guc->timestamp.last_stat_jiffies + 1599 (guc->timestamp.ping_delay / 2))) 1600 return; 1601 1602 __update_guc_busyness_stats(guc); 1603 } 1604 1605 void intel_guc_busyness_unpark(struct intel_gt *gt) 1606 { 1607 struct intel_guc *guc = >->uc.guc; 1608 unsigned long flags; 1609 ktime_t unused; 1610 1611 if (!guc_submission_initialized(guc)) 1612 return; 1613 1614 spin_lock_irqsave(&guc->timestamp.lock, flags); 1615 guc_update_pm_timestamp(guc, &unused); 1616 spin_unlock_irqrestore(&guc->timestamp.lock, flags); 1617 guc_enable_busyness_worker(guc); 1618 } 1619 1620 static inline bool 1621 submission_disabled(struct intel_guc *guc) 1622 { 1623 struct i915_sched_engine * const sched_engine = guc->sched_engine; 1624 1625 return unlikely(!sched_engine || 1626 !__tasklet_is_enabled(&sched_engine->tasklet) || 1627 intel_gt_is_wedged(guc_to_gt(guc))); 1628 } 1629 1630 static void disable_submission(struct intel_guc *guc) 1631 { 1632 struct i915_sched_engine * const sched_engine = guc->sched_engine; 1633 1634 if (__tasklet_is_enabled(&sched_engine->tasklet)) { 1635 GEM_BUG_ON(!guc->ct.enabled); 1636 __tasklet_disable_sync_once(&sched_engine->tasklet); 1637 sched_engine->tasklet.callback = NULL; 1638 } 1639 } 1640 1641 static void enable_submission(struct intel_guc *guc) 1642 { 1643 struct i915_sched_engine * const sched_engine = guc->sched_engine; 1644 unsigned long flags; 1645 1646 spin_lock_irqsave(&guc->sched_engine->lock, flags); 1647 sched_engine->tasklet.callback = guc_submission_tasklet; 1648 wmb(); /* Make sure callback visible */ 1649 if (!__tasklet_is_enabled(&sched_engine->tasklet) && 1650 __tasklet_enable(&sched_engine->tasklet)) { 1651 GEM_BUG_ON(!guc->ct.enabled); 1652 1653 /* And kick in case we missed a new request submission. */ 1654 tasklet_hi_schedule(&sched_engine->tasklet); 1655 } 1656 spin_unlock_irqrestore(&guc->sched_engine->lock, flags); 1657 } 1658 1659 static void guc_flush_submissions(struct intel_guc *guc) 1660 { 1661 struct i915_sched_engine * const sched_engine = guc->sched_engine; 1662 unsigned long flags; 1663 1664 spin_lock_irqsave(&sched_engine->lock, flags); 1665 spin_unlock_irqrestore(&sched_engine->lock, flags); 1666 } 1667 1668 void intel_guc_submission_flush_work(struct intel_guc *guc) 1669 { 1670 flush_work(&guc->submission_state.destroyed_worker); 1671 } 1672 1673 static void guc_flush_destroyed_contexts(struct intel_guc *guc); 1674 1675 void intel_guc_submission_reset_prepare(struct intel_guc *guc) 1676 { 1677 if (unlikely(!guc_submission_initialized(guc))) { 1678 /* Reset called during driver load? GuC not yet initialised! */ 1679 return; 1680 } 1681 1682 intel_gt_park_heartbeats(guc_to_gt(guc)); 1683 disable_submission(guc); 1684 guc->interrupts.disable(guc); 1685 __reset_guc_busyness_stats(guc); 1686 1687 /* Flush IRQ handler */ 1688 spin_lock_irq(guc_to_gt(guc)->irq_lock); 1689 spin_unlock_irq(guc_to_gt(guc)->irq_lock); 1690 1691 guc_flush_submissions(guc); 1692 guc_flush_destroyed_contexts(guc); 1693 flush_work(&guc->ct.requests.worker); 1694 1695 scrub_guc_desc_for_outstanding_g2h(guc); 1696 } 1697 1698 static struct intel_engine_cs * 1699 guc_virtual_get_sibling(struct intel_engine_cs *ve, unsigned int sibling) 1700 { 1701 struct intel_engine_cs *engine; 1702 intel_engine_mask_t tmp, mask = ve->mask; 1703 unsigned int num_siblings = 0; 1704 1705 for_each_engine_masked(engine, ve->gt, mask, tmp) 1706 if (num_siblings++ == sibling) 1707 return engine; 1708 1709 return NULL; 1710 } 1711 1712 static inline struct intel_engine_cs * 1713 __context_to_physical_engine(struct intel_context *ce) 1714 { 1715 struct intel_engine_cs *engine = ce->engine; 1716 1717 if (intel_engine_is_virtual(engine)) 1718 engine = guc_virtual_get_sibling(engine, 0); 1719 1720 return engine; 1721 } 1722 1723 static void guc_reset_state(struct intel_context *ce, u32 head, bool scrub) 1724 { 1725 struct intel_engine_cs *engine = __context_to_physical_engine(ce); 1726 1727 if (!intel_context_is_schedulable(ce)) 1728 return; 1729 1730 GEM_BUG_ON(!intel_context_is_pinned(ce)); 1731 1732 /* 1733 * We want a simple context + ring to execute the breadcrumb update. 1734 * We cannot rely on the context being intact across the GPU hang, 1735 * so clear it and rebuild just what we need for the breadcrumb. 1736 * All pending requests for this context will be zapped, and any 1737 * future request will be after userspace has had the opportunity 1738 * to recreate its own state. 1739 */ 1740 if (scrub) 1741 lrc_init_regs(ce, engine, true); 1742 1743 /* Rerun the request; its payload has been neutered (if guilty). */ 1744 lrc_update_regs(ce, engine, head); 1745 } 1746 1747 static void guc_engine_reset_prepare(struct intel_engine_cs *engine) 1748 { 1749 /* 1750 * Wa_22011802037: In addition to stopping the cs, we need 1751 * to wait for any pending mi force wakeups 1752 */ 1753 if (intel_engine_reset_needs_wa_22011802037(engine->gt)) { 1754 intel_engine_stop_cs(engine); 1755 intel_engine_wait_for_pending_mi_fw(engine); 1756 } 1757 } 1758 1759 static void guc_reset_nop(struct intel_engine_cs *engine) 1760 { 1761 } 1762 1763 static void guc_rewind_nop(struct intel_engine_cs *engine, bool stalled) 1764 { 1765 } 1766 1767 static void 1768 __unwind_incomplete_requests(struct intel_context *ce) 1769 { 1770 struct i915_request *rq, *rn; 1771 struct list_head *pl; 1772 int prio = I915_PRIORITY_INVALID; 1773 struct i915_sched_engine * const sched_engine = 1774 ce->engine->sched_engine; 1775 unsigned long flags; 1776 1777 spin_lock_irqsave(&sched_engine->lock, flags); 1778 spin_lock(&ce->guc_state.lock); 1779 list_for_each_entry_safe_reverse(rq, rn, 1780 &ce->guc_state.requests, 1781 sched.link) { 1782 if (i915_request_completed(rq)) 1783 continue; 1784 1785 list_del_init(&rq->sched.link); 1786 __i915_request_unsubmit(rq); 1787 1788 /* Push the request back into the queue for later resubmission. */ 1789 GEM_BUG_ON(rq_prio(rq) == I915_PRIORITY_INVALID); 1790 if (rq_prio(rq) != prio) { 1791 prio = rq_prio(rq); 1792 pl = i915_sched_lookup_priolist(sched_engine, prio); 1793 } 1794 GEM_BUG_ON(i915_sched_engine_is_empty(sched_engine)); 1795 1796 list_add(&rq->sched.link, pl); 1797 set_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags); 1798 } 1799 spin_unlock(&ce->guc_state.lock); 1800 spin_unlock_irqrestore(&sched_engine->lock, flags); 1801 } 1802 1803 static void __guc_reset_context(struct intel_context *ce, intel_engine_mask_t stalled) 1804 { 1805 bool guilty; 1806 struct i915_request *rq; 1807 unsigned long flags; 1808 u32 head; 1809 int i, number_children = ce->parallel.number_children; 1810 struct intel_context *parent = ce; 1811 1812 GEM_BUG_ON(intel_context_is_child(ce)); 1813 1814 intel_context_get(ce); 1815 1816 /* 1817 * GuC will implicitly mark the context as non-schedulable when it sends 1818 * the reset notification. Make sure our state reflects this change. The 1819 * context will be marked enabled on resubmission. 1820 */ 1821 spin_lock_irqsave(&ce->guc_state.lock, flags); 1822 clr_context_enabled(ce); 1823 spin_unlock_irqrestore(&ce->guc_state.lock, flags); 1824 1825 /* 1826 * For each context in the relationship find the hanging request 1827 * resetting each context / request as needed 1828 */ 1829 for (i = 0; i < number_children + 1; ++i) { 1830 if (!intel_context_is_pinned(ce)) 1831 goto next_context; 1832 1833 guilty = false; 1834 rq = intel_context_get_active_request(ce); 1835 if (!rq) { 1836 head = ce->ring->tail; 1837 goto out_replay; 1838 } 1839 1840 if (i915_request_started(rq)) 1841 guilty = stalled & ce->engine->mask; 1842 1843 GEM_BUG_ON(i915_active_is_idle(&ce->active)); 1844 head = intel_ring_wrap(ce->ring, rq->head); 1845 1846 __i915_request_reset(rq, guilty); 1847 i915_request_put(rq); 1848 out_replay: 1849 guc_reset_state(ce, head, guilty); 1850 next_context: 1851 if (i != number_children) 1852 ce = list_next_entry(ce, parallel.child_link); 1853 } 1854 1855 __unwind_incomplete_requests(parent); 1856 intel_context_put(parent); 1857 } 1858 1859 void wake_up_all_tlb_invalidate(struct intel_guc *guc) 1860 { 1861 struct intel_guc_tlb_wait *wait; 1862 unsigned long i; 1863 1864 if (!intel_guc_tlb_invalidation_is_available(guc)) 1865 return; 1866 1867 xa_lock_irq(&guc->tlb_lookup); 1868 xa_for_each(&guc->tlb_lookup, i, wait) 1869 wake_up(&wait->wq); 1870 xa_unlock_irq(&guc->tlb_lookup); 1871 } 1872 1873 void intel_guc_submission_reset(struct intel_guc *guc, intel_engine_mask_t stalled) 1874 { 1875 struct intel_context *ce; 1876 unsigned long index; 1877 unsigned long flags; 1878 1879 if (unlikely(!guc_submission_initialized(guc))) { 1880 /* Reset called during driver load? GuC not yet initialised! */ 1881 return; 1882 } 1883 1884 xa_lock_irqsave(&guc->context_lookup, flags); 1885 xa_for_each(&guc->context_lookup, index, ce) { 1886 if (!kref_get_unless_zero(&ce->ref)) 1887 continue; 1888 1889 xa_unlock(&guc->context_lookup); 1890 1891 if (intel_context_is_pinned(ce) && 1892 !intel_context_is_child(ce)) 1893 __guc_reset_context(ce, stalled); 1894 1895 intel_context_put(ce); 1896 1897 xa_lock(&guc->context_lookup); 1898 } 1899 xa_unlock_irqrestore(&guc->context_lookup, flags); 1900 1901 /* GuC is blown away, drop all references to contexts */ 1902 xa_destroy(&guc->context_lookup); 1903 } 1904 1905 static void guc_cancel_context_requests(struct intel_context *ce) 1906 { 1907 struct i915_sched_engine *sched_engine = ce_to_guc(ce)->sched_engine; 1908 struct i915_request *rq; 1909 unsigned long flags; 1910 1911 /* Mark all executing requests as skipped. */ 1912 spin_lock_irqsave(&sched_engine->lock, flags); 1913 spin_lock(&ce->guc_state.lock); 1914 list_for_each_entry(rq, &ce->guc_state.requests, sched.link) 1915 i915_request_put(i915_request_mark_eio(rq)); 1916 spin_unlock(&ce->guc_state.lock); 1917 spin_unlock_irqrestore(&sched_engine->lock, flags); 1918 } 1919 1920 static void 1921 guc_cancel_sched_engine_requests(struct i915_sched_engine *sched_engine) 1922 { 1923 struct i915_request *rq, *rn; 1924 struct rb_node *rb; 1925 unsigned long flags; 1926 1927 /* Can be called during boot if GuC fails to load */ 1928 if (!sched_engine) 1929 return; 1930 1931 /* 1932 * Before we call engine->cancel_requests(), we should have exclusive 1933 * access to the submission state. This is arranged for us by the 1934 * caller disabling the interrupt generation, the tasklet and other 1935 * threads that may then access the same state, giving us a free hand 1936 * to reset state. However, we still need to let lockdep be aware that 1937 * we know this state may be accessed in hardirq context, so we 1938 * disable the irq around this manipulation and we want to keep 1939 * the spinlock focused on its duties and not accidentally conflate 1940 * coverage to the submission's irq state. (Similarly, although we 1941 * shouldn't need to disable irq around the manipulation of the 1942 * submission's irq state, we also wish to remind ourselves that 1943 * it is irq state.) 1944 */ 1945 spin_lock_irqsave(&sched_engine->lock, flags); 1946 1947 /* Flush the queued requests to the timeline list (for retiring). */ 1948 while ((rb = rb_first_cached(&sched_engine->queue))) { 1949 struct i915_priolist *p = to_priolist(rb); 1950 1951 priolist_for_each_request_consume(rq, rn, p) { 1952 list_del_init(&rq->sched.link); 1953 1954 __i915_request_submit(rq); 1955 1956 i915_request_put(i915_request_mark_eio(rq)); 1957 } 1958 1959 rb_erase_cached(&p->node, &sched_engine->queue); 1960 i915_priolist_free(p); 1961 } 1962 1963 /* Remaining _unready_ requests will be nop'ed when submitted */ 1964 1965 sched_engine->queue_priority_hint = INT_MIN; 1966 sched_engine->queue = RB_ROOT_CACHED; 1967 1968 spin_unlock_irqrestore(&sched_engine->lock, flags); 1969 } 1970 1971 void intel_guc_submission_cancel_requests(struct intel_guc *guc) 1972 { 1973 struct intel_context *ce; 1974 unsigned long index; 1975 unsigned long flags; 1976 1977 xa_lock_irqsave(&guc->context_lookup, flags); 1978 xa_for_each(&guc->context_lookup, index, ce) { 1979 if (!kref_get_unless_zero(&ce->ref)) 1980 continue; 1981 1982 xa_unlock(&guc->context_lookup); 1983 1984 if (intel_context_is_pinned(ce) && 1985 !intel_context_is_child(ce)) 1986 guc_cancel_context_requests(ce); 1987 1988 intel_context_put(ce); 1989 1990 xa_lock(&guc->context_lookup); 1991 } 1992 xa_unlock_irqrestore(&guc->context_lookup, flags); 1993 1994 guc_cancel_sched_engine_requests(guc->sched_engine); 1995 1996 /* GuC is blown away, drop all references to contexts */ 1997 xa_destroy(&guc->context_lookup); 1998 1999 /* 2000 * Wedged GT won't respond to any TLB invalidation request. Simply 2001 * release all the blocked waiters. 2002 */ 2003 wake_up_all_tlb_invalidate(guc); 2004 } 2005 2006 void intel_guc_submission_reset_finish(struct intel_guc *guc) 2007 { 2008 /* Reset called during driver load or during wedge? */ 2009 if (unlikely(!guc_submission_initialized(guc) || 2010 !intel_guc_is_fw_running(guc) || 2011 intel_gt_is_wedged(guc_to_gt(guc)))) { 2012 return; 2013 } 2014 2015 /* 2016 * Technically possible for either of these values to be non-zero here, 2017 * but very unlikely + harmless. Regardless let's add a warn so we can 2018 * see in CI if this happens frequently / a precursor to taking down the 2019 * machine. 2020 */ 2021 GEM_WARN_ON(atomic_read(&guc->outstanding_submission_g2h)); 2022 atomic_set(&guc->outstanding_submission_g2h, 0); 2023 2024 intel_guc_global_policies_update(guc); 2025 enable_submission(guc); 2026 intel_gt_unpark_heartbeats(guc_to_gt(guc)); 2027 2028 /* 2029 * The full GT reset will have cleared the TLB caches and flushed the 2030 * G2H message queue; we can release all the blocked waiters. 2031 */ 2032 wake_up_all_tlb_invalidate(guc); 2033 } 2034 2035 static void destroyed_worker_func(struct work_struct *w); 2036 static void reset_fail_worker_func(struct work_struct *w); 2037 2038 bool intel_guc_tlb_invalidation_is_available(struct intel_guc *guc) 2039 { 2040 return HAS_GUC_TLB_INVALIDATION(guc_to_gt(guc)->i915) && 2041 intel_guc_is_ready(guc); 2042 } 2043 2044 static int init_tlb_lookup(struct intel_guc *guc) 2045 { 2046 struct intel_guc_tlb_wait *wait; 2047 int err; 2048 2049 if (!HAS_GUC_TLB_INVALIDATION(guc_to_gt(guc)->i915)) 2050 return 0; 2051 2052 xa_init_flags(&guc->tlb_lookup, XA_FLAGS_ALLOC); 2053 2054 wait = kzalloc(sizeof(*wait), GFP_KERNEL); 2055 if (!wait) 2056 return -ENOMEM; 2057 2058 init_waitqueue_head(&wait->wq); 2059 2060 /* Preallocate a shared id for use under memory pressure. */ 2061 err = xa_alloc_cyclic_irq(&guc->tlb_lookup, &guc->serial_slot, wait, 2062 xa_limit_32b, &guc->next_seqno, GFP_KERNEL); 2063 if (err < 0) { 2064 kfree(wait); 2065 return err; 2066 } 2067 2068 return 0; 2069 } 2070 2071 static void fini_tlb_lookup(struct intel_guc *guc) 2072 { 2073 struct intel_guc_tlb_wait *wait; 2074 2075 if (!HAS_GUC_TLB_INVALIDATION(guc_to_gt(guc)->i915)) 2076 return; 2077 2078 wait = xa_load(&guc->tlb_lookup, guc->serial_slot); 2079 if (wait && wait->busy) 2080 guc_err(guc, "Unexpected busy item in tlb_lookup on fini\n"); 2081 kfree(wait); 2082 2083 xa_destroy(&guc->tlb_lookup); 2084 } 2085 2086 /* 2087 * Set up the memory resources to be shared with the GuC (via the GGTT) 2088 * at firmware loading time. 2089 */ 2090 int intel_guc_submission_init(struct intel_guc *guc) 2091 { 2092 struct intel_gt *gt = guc_to_gt(guc); 2093 int ret; 2094 2095 if (guc->submission_initialized) 2096 return 0; 2097 2098 if (GUC_SUBMIT_VER(guc) < MAKE_GUC_VER(1, 0, 0)) { 2099 ret = guc_lrc_desc_pool_create_v69(guc); 2100 if (ret) 2101 return ret; 2102 } 2103 2104 ret = init_tlb_lookup(guc); 2105 if (ret) 2106 goto destroy_pool; 2107 2108 guc->submission_state.guc_ids_bitmap = 2109 bitmap_zalloc(NUMBER_MULTI_LRC_GUC_ID(guc), GFP_KERNEL); 2110 if (!guc->submission_state.guc_ids_bitmap) { 2111 ret = -ENOMEM; 2112 goto destroy_tlb; 2113 } 2114 2115 guc->timestamp.ping_delay = (POLL_TIME_CLKS / gt->clock_frequency + 1) * HZ; 2116 guc->timestamp.shift = gpm_timestamp_shift(gt); 2117 guc->submission_initialized = true; 2118 2119 return 0; 2120 2121 destroy_tlb: 2122 fini_tlb_lookup(guc); 2123 destroy_pool: 2124 guc_lrc_desc_pool_destroy_v69(guc); 2125 return ret; 2126 } 2127 2128 void intel_guc_submission_fini(struct intel_guc *guc) 2129 { 2130 if (!guc->submission_initialized) 2131 return; 2132 2133 guc_fini_engine_stats(guc); 2134 guc_flush_destroyed_contexts(guc); 2135 guc_lrc_desc_pool_destroy_v69(guc); 2136 i915_sched_engine_put(guc->sched_engine); 2137 bitmap_free(guc->submission_state.guc_ids_bitmap); 2138 fini_tlb_lookup(guc); 2139 guc->submission_initialized = false; 2140 } 2141 2142 static inline void queue_request(struct i915_sched_engine *sched_engine, 2143 struct i915_request *rq, 2144 int prio) 2145 { 2146 GEM_BUG_ON(!list_empty(&rq->sched.link)); 2147 list_add_tail(&rq->sched.link, 2148 i915_sched_lookup_priolist(sched_engine, prio)); 2149 set_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags); 2150 tasklet_hi_schedule(&sched_engine->tasklet); 2151 } 2152 2153 static int guc_bypass_tasklet_submit(struct intel_guc *guc, 2154 struct i915_request *rq) 2155 { 2156 int ret = 0; 2157 2158 __i915_request_submit(rq); 2159 2160 trace_i915_request_in(rq, 0); 2161 2162 if (is_multi_lrc_rq(rq)) { 2163 if (multi_lrc_submit(rq)) { 2164 ret = guc_wq_item_append(guc, rq); 2165 if (!ret) 2166 ret = guc_add_request(guc, rq); 2167 } 2168 } else { 2169 guc_set_lrc_tail(rq); 2170 ret = guc_add_request(guc, rq); 2171 } 2172 2173 if (unlikely(ret == -EPIPE)) 2174 disable_submission(guc); 2175 2176 return ret; 2177 } 2178 2179 static bool need_tasklet(struct intel_guc *guc, struct i915_request *rq) 2180 { 2181 struct i915_sched_engine *sched_engine = rq->engine->sched_engine; 2182 struct intel_context *ce = request_to_scheduling_context(rq); 2183 2184 return submission_disabled(guc) || guc->stalled_request || 2185 !i915_sched_engine_is_empty(sched_engine) || 2186 !ctx_id_mapped(guc, ce->guc_id.id); 2187 } 2188 2189 static void guc_submit_request(struct i915_request *rq) 2190 { 2191 struct i915_sched_engine *sched_engine = rq->engine->sched_engine; 2192 struct intel_guc *guc = &rq->engine->gt->uc.guc; 2193 unsigned long flags; 2194 2195 /* Will be called from irq-context when using foreign fences. */ 2196 spin_lock_irqsave(&sched_engine->lock, flags); 2197 2198 if (need_tasklet(guc, rq)) 2199 queue_request(sched_engine, rq, rq_prio(rq)); 2200 else if (guc_bypass_tasklet_submit(guc, rq) == -EBUSY) 2201 tasklet_hi_schedule(&sched_engine->tasklet); 2202 2203 spin_unlock_irqrestore(&sched_engine->lock, flags); 2204 } 2205 2206 static int new_guc_id(struct intel_guc *guc, struct intel_context *ce) 2207 { 2208 int ret; 2209 2210 GEM_BUG_ON(intel_context_is_child(ce)); 2211 2212 if (intel_context_is_parent(ce)) 2213 ret = bitmap_find_free_region(guc->submission_state.guc_ids_bitmap, 2214 NUMBER_MULTI_LRC_GUC_ID(guc), 2215 order_base_2(ce->parallel.number_children 2216 + 1)); 2217 else 2218 ret = ida_simple_get(&guc->submission_state.guc_ids, 2219 NUMBER_MULTI_LRC_GUC_ID(guc), 2220 guc->submission_state.num_guc_ids, 2221 GFP_KERNEL | __GFP_RETRY_MAYFAIL | 2222 __GFP_NOWARN); 2223 if (unlikely(ret < 0)) 2224 return ret; 2225 2226 if (!intel_context_is_parent(ce)) 2227 ++guc->submission_state.guc_ids_in_use; 2228 2229 ce->guc_id.id = ret; 2230 return 0; 2231 } 2232 2233 static void __release_guc_id(struct intel_guc *guc, struct intel_context *ce) 2234 { 2235 GEM_BUG_ON(intel_context_is_child(ce)); 2236 2237 if (!context_guc_id_invalid(ce)) { 2238 if (intel_context_is_parent(ce)) { 2239 bitmap_release_region(guc->submission_state.guc_ids_bitmap, 2240 ce->guc_id.id, 2241 order_base_2(ce->parallel.number_children 2242 + 1)); 2243 } else { 2244 --guc->submission_state.guc_ids_in_use; 2245 ida_simple_remove(&guc->submission_state.guc_ids, 2246 ce->guc_id.id); 2247 } 2248 clr_ctx_id_mapping(guc, ce->guc_id.id); 2249 set_context_guc_id_invalid(ce); 2250 } 2251 if (!list_empty(&ce->guc_id.link)) 2252 list_del_init(&ce->guc_id.link); 2253 } 2254 2255 static void release_guc_id(struct intel_guc *guc, struct intel_context *ce) 2256 { 2257 unsigned long flags; 2258 2259 spin_lock_irqsave(&guc->submission_state.lock, flags); 2260 __release_guc_id(guc, ce); 2261 spin_unlock_irqrestore(&guc->submission_state.lock, flags); 2262 } 2263 2264 static int steal_guc_id(struct intel_guc *guc, struct intel_context *ce) 2265 { 2266 struct intel_context *cn; 2267 2268 lockdep_assert_held(&guc->submission_state.lock); 2269 GEM_BUG_ON(intel_context_is_child(ce)); 2270 GEM_BUG_ON(intel_context_is_parent(ce)); 2271 2272 if (!list_empty(&guc->submission_state.guc_id_list)) { 2273 cn = list_first_entry(&guc->submission_state.guc_id_list, 2274 struct intel_context, 2275 guc_id.link); 2276 2277 GEM_BUG_ON(atomic_read(&cn->guc_id.ref)); 2278 GEM_BUG_ON(context_guc_id_invalid(cn)); 2279 GEM_BUG_ON(intel_context_is_child(cn)); 2280 GEM_BUG_ON(intel_context_is_parent(cn)); 2281 2282 list_del_init(&cn->guc_id.link); 2283 ce->guc_id.id = cn->guc_id.id; 2284 2285 spin_lock(&cn->guc_state.lock); 2286 clr_context_registered(cn); 2287 spin_unlock(&cn->guc_state.lock); 2288 2289 set_context_guc_id_invalid(cn); 2290 2291 #ifdef CONFIG_DRM_I915_SELFTEST 2292 guc->number_guc_id_stolen++; 2293 #endif 2294 2295 return 0; 2296 } else { 2297 return -EAGAIN; 2298 } 2299 } 2300 2301 static int assign_guc_id(struct intel_guc *guc, struct intel_context *ce) 2302 { 2303 int ret; 2304 2305 lockdep_assert_held(&guc->submission_state.lock); 2306 GEM_BUG_ON(intel_context_is_child(ce)); 2307 2308 ret = new_guc_id(guc, ce); 2309 if (unlikely(ret < 0)) { 2310 if (intel_context_is_parent(ce)) 2311 return -ENOSPC; 2312 2313 ret = steal_guc_id(guc, ce); 2314 if (ret < 0) 2315 return ret; 2316 } 2317 2318 if (intel_context_is_parent(ce)) { 2319 struct intel_context *child; 2320 int i = 1; 2321 2322 for_each_child(ce, child) 2323 child->guc_id.id = ce->guc_id.id + i++; 2324 } 2325 2326 return 0; 2327 } 2328 2329 #define PIN_GUC_ID_TRIES 4 2330 static int pin_guc_id(struct intel_guc *guc, struct intel_context *ce) 2331 { 2332 int ret = 0; 2333 unsigned long flags, tries = PIN_GUC_ID_TRIES; 2334 2335 GEM_BUG_ON(atomic_read(&ce->guc_id.ref)); 2336 2337 try_again: 2338 spin_lock_irqsave(&guc->submission_state.lock, flags); 2339 2340 might_lock(&ce->guc_state.lock); 2341 2342 if (context_guc_id_invalid(ce)) { 2343 ret = assign_guc_id(guc, ce); 2344 if (ret) 2345 goto out_unlock; 2346 ret = 1; /* Indidcates newly assigned guc_id */ 2347 } 2348 if (!list_empty(&ce->guc_id.link)) 2349 list_del_init(&ce->guc_id.link); 2350 atomic_inc(&ce->guc_id.ref); 2351 2352 out_unlock: 2353 spin_unlock_irqrestore(&guc->submission_state.lock, flags); 2354 2355 /* 2356 * -EAGAIN indicates no guc_id are available, let's retire any 2357 * outstanding requests to see if that frees up a guc_id. If the first 2358 * retire didn't help, insert a sleep with the timeslice duration before 2359 * attempting to retire more requests. Double the sleep period each 2360 * subsequent pass before finally giving up. The sleep period has max of 2361 * 100ms and minimum of 1ms. 2362 */ 2363 if (ret == -EAGAIN && --tries) { 2364 if (PIN_GUC_ID_TRIES - tries > 1) { 2365 unsigned int timeslice_shifted = 2366 ce->engine->props.timeslice_duration_ms << 2367 (PIN_GUC_ID_TRIES - tries - 2); 2368 unsigned int max = min_t(unsigned int, 100, 2369 timeslice_shifted); 2370 2371 msleep(max_t(unsigned int, max, 1)); 2372 } 2373 intel_gt_retire_requests(guc_to_gt(guc)); 2374 goto try_again; 2375 } 2376 2377 return ret; 2378 } 2379 2380 static void unpin_guc_id(struct intel_guc *guc, struct intel_context *ce) 2381 { 2382 unsigned long flags; 2383 2384 GEM_BUG_ON(atomic_read(&ce->guc_id.ref) < 0); 2385 GEM_BUG_ON(intel_context_is_child(ce)); 2386 2387 if (unlikely(context_guc_id_invalid(ce) || 2388 intel_context_is_parent(ce))) 2389 return; 2390 2391 spin_lock_irqsave(&guc->submission_state.lock, flags); 2392 if (!context_guc_id_invalid(ce) && list_empty(&ce->guc_id.link) && 2393 !atomic_read(&ce->guc_id.ref)) 2394 list_add_tail(&ce->guc_id.link, 2395 &guc->submission_state.guc_id_list); 2396 spin_unlock_irqrestore(&guc->submission_state.lock, flags); 2397 } 2398 2399 static int __guc_action_register_multi_lrc_v69(struct intel_guc *guc, 2400 struct intel_context *ce, 2401 u32 guc_id, 2402 u32 offset, 2403 bool loop) 2404 { 2405 struct intel_context *child; 2406 u32 action[4 + MAX_ENGINE_INSTANCE]; 2407 int len = 0; 2408 2409 GEM_BUG_ON(ce->parallel.number_children > MAX_ENGINE_INSTANCE); 2410 2411 action[len++] = INTEL_GUC_ACTION_REGISTER_CONTEXT_MULTI_LRC; 2412 action[len++] = guc_id; 2413 action[len++] = ce->parallel.number_children + 1; 2414 action[len++] = offset; 2415 for_each_child(ce, child) { 2416 offset += sizeof(struct guc_lrc_desc_v69); 2417 action[len++] = offset; 2418 } 2419 2420 return guc_submission_send_busy_loop(guc, action, len, 0, loop); 2421 } 2422 2423 static int __guc_action_register_multi_lrc_v70(struct intel_guc *guc, 2424 struct intel_context *ce, 2425 struct guc_ctxt_registration_info *info, 2426 bool loop) 2427 { 2428 struct intel_context *child; 2429 u32 action[13 + (MAX_ENGINE_INSTANCE * 2)]; 2430 int len = 0; 2431 u32 next_id; 2432 2433 GEM_BUG_ON(ce->parallel.number_children > MAX_ENGINE_INSTANCE); 2434 2435 action[len++] = INTEL_GUC_ACTION_REGISTER_CONTEXT_MULTI_LRC; 2436 action[len++] = info->flags; 2437 action[len++] = info->context_idx; 2438 action[len++] = info->engine_class; 2439 action[len++] = info->engine_submit_mask; 2440 action[len++] = info->wq_desc_lo; 2441 action[len++] = info->wq_desc_hi; 2442 action[len++] = info->wq_base_lo; 2443 action[len++] = info->wq_base_hi; 2444 action[len++] = info->wq_size; 2445 action[len++] = ce->parallel.number_children + 1; 2446 action[len++] = info->hwlrca_lo; 2447 action[len++] = info->hwlrca_hi; 2448 2449 next_id = info->context_idx + 1; 2450 for_each_child(ce, child) { 2451 GEM_BUG_ON(next_id++ != child->guc_id.id); 2452 2453 /* 2454 * NB: GuC interface supports 64 bit LRCA even though i915/HW 2455 * only supports 32 bit currently. 2456 */ 2457 action[len++] = lower_32_bits(child->lrc.lrca); 2458 action[len++] = upper_32_bits(child->lrc.lrca); 2459 } 2460 2461 GEM_BUG_ON(len > ARRAY_SIZE(action)); 2462 2463 return guc_submission_send_busy_loop(guc, action, len, 0, loop); 2464 } 2465 2466 static int __guc_action_register_context_v69(struct intel_guc *guc, 2467 u32 guc_id, 2468 u32 offset, 2469 bool loop) 2470 { 2471 u32 action[] = { 2472 INTEL_GUC_ACTION_REGISTER_CONTEXT, 2473 guc_id, 2474 offset, 2475 }; 2476 2477 return guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action), 2478 0, loop); 2479 } 2480 2481 static int __guc_action_register_context_v70(struct intel_guc *guc, 2482 struct guc_ctxt_registration_info *info, 2483 bool loop) 2484 { 2485 u32 action[] = { 2486 INTEL_GUC_ACTION_REGISTER_CONTEXT, 2487 info->flags, 2488 info->context_idx, 2489 info->engine_class, 2490 info->engine_submit_mask, 2491 info->wq_desc_lo, 2492 info->wq_desc_hi, 2493 info->wq_base_lo, 2494 info->wq_base_hi, 2495 info->wq_size, 2496 info->hwlrca_lo, 2497 info->hwlrca_hi, 2498 }; 2499 2500 return guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action), 2501 0, loop); 2502 } 2503 2504 static void prepare_context_registration_info_v69(struct intel_context *ce); 2505 static void prepare_context_registration_info_v70(struct intel_context *ce, 2506 struct guc_ctxt_registration_info *info); 2507 2508 static int 2509 register_context_v69(struct intel_guc *guc, struct intel_context *ce, bool loop) 2510 { 2511 u32 offset = intel_guc_ggtt_offset(guc, guc->lrc_desc_pool_v69) + 2512 ce->guc_id.id * sizeof(struct guc_lrc_desc_v69); 2513 2514 prepare_context_registration_info_v69(ce); 2515 2516 if (intel_context_is_parent(ce)) 2517 return __guc_action_register_multi_lrc_v69(guc, ce, ce->guc_id.id, 2518 offset, loop); 2519 else 2520 return __guc_action_register_context_v69(guc, ce->guc_id.id, 2521 offset, loop); 2522 } 2523 2524 static int 2525 register_context_v70(struct intel_guc *guc, struct intel_context *ce, bool loop) 2526 { 2527 struct guc_ctxt_registration_info info; 2528 2529 prepare_context_registration_info_v70(ce, &info); 2530 2531 if (intel_context_is_parent(ce)) 2532 return __guc_action_register_multi_lrc_v70(guc, ce, &info, loop); 2533 else 2534 return __guc_action_register_context_v70(guc, &info, loop); 2535 } 2536 2537 static int register_context(struct intel_context *ce, bool loop) 2538 { 2539 struct intel_guc *guc = ce_to_guc(ce); 2540 int ret; 2541 2542 GEM_BUG_ON(intel_context_is_child(ce)); 2543 trace_intel_context_register(ce); 2544 2545 if (GUC_SUBMIT_VER(guc) >= MAKE_GUC_VER(1, 0, 0)) 2546 ret = register_context_v70(guc, ce, loop); 2547 else 2548 ret = register_context_v69(guc, ce, loop); 2549 2550 if (likely(!ret)) { 2551 unsigned long flags; 2552 2553 spin_lock_irqsave(&ce->guc_state.lock, flags); 2554 set_context_registered(ce); 2555 spin_unlock_irqrestore(&ce->guc_state.lock, flags); 2556 2557 if (GUC_SUBMIT_VER(guc) >= MAKE_GUC_VER(1, 0, 0)) 2558 guc_context_policy_init_v70(ce, loop); 2559 } 2560 2561 return ret; 2562 } 2563 2564 static int __guc_action_deregister_context(struct intel_guc *guc, 2565 u32 guc_id) 2566 { 2567 u32 action[] = { 2568 INTEL_GUC_ACTION_DEREGISTER_CONTEXT, 2569 guc_id, 2570 }; 2571 2572 return guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action), 2573 G2H_LEN_DW_DEREGISTER_CONTEXT, 2574 true); 2575 } 2576 2577 static int deregister_context(struct intel_context *ce, u32 guc_id) 2578 { 2579 struct intel_guc *guc = ce_to_guc(ce); 2580 2581 GEM_BUG_ON(intel_context_is_child(ce)); 2582 trace_intel_context_deregister(ce); 2583 2584 return __guc_action_deregister_context(guc, guc_id); 2585 } 2586 2587 static inline void clear_children_join_go_memory(struct intel_context *ce) 2588 { 2589 struct parent_scratch *ps = __get_parent_scratch(ce); 2590 int i; 2591 2592 ps->go.semaphore = 0; 2593 for (i = 0; i < ce->parallel.number_children + 1; ++i) 2594 ps->join[i].semaphore = 0; 2595 } 2596 2597 static inline u32 get_children_go_value(struct intel_context *ce) 2598 { 2599 return __get_parent_scratch(ce)->go.semaphore; 2600 } 2601 2602 static inline u32 get_children_join_value(struct intel_context *ce, 2603 u8 child_index) 2604 { 2605 return __get_parent_scratch(ce)->join[child_index].semaphore; 2606 } 2607 2608 struct context_policy { 2609 u32 count; 2610 struct guc_update_context_policy h2g; 2611 }; 2612 2613 static u32 __guc_context_policy_action_size(struct context_policy *policy) 2614 { 2615 size_t bytes = sizeof(policy->h2g.header) + 2616 (sizeof(policy->h2g.klv[0]) * policy->count); 2617 2618 return bytes / sizeof(u32); 2619 } 2620 2621 static void __guc_context_policy_start_klv(struct context_policy *policy, u16 guc_id) 2622 { 2623 policy->h2g.header.action = INTEL_GUC_ACTION_HOST2GUC_UPDATE_CONTEXT_POLICIES; 2624 policy->h2g.header.ctx_id = guc_id; 2625 policy->count = 0; 2626 } 2627 2628 #define MAKE_CONTEXT_POLICY_ADD(func, id) \ 2629 static void __guc_context_policy_add_##func(struct context_policy *policy, u32 data) \ 2630 { \ 2631 GEM_BUG_ON(policy->count >= GUC_CONTEXT_POLICIES_KLV_NUM_IDS); \ 2632 policy->h2g.klv[policy->count].kl = \ 2633 FIELD_PREP(GUC_KLV_0_KEY, GUC_CONTEXT_POLICIES_KLV_ID_##id) | \ 2634 FIELD_PREP(GUC_KLV_0_LEN, 1); \ 2635 policy->h2g.klv[policy->count].value = data; \ 2636 policy->count++; \ 2637 } 2638 2639 MAKE_CONTEXT_POLICY_ADD(execution_quantum, EXECUTION_QUANTUM) 2640 MAKE_CONTEXT_POLICY_ADD(preemption_timeout, PREEMPTION_TIMEOUT) 2641 MAKE_CONTEXT_POLICY_ADD(priority, SCHEDULING_PRIORITY) 2642 MAKE_CONTEXT_POLICY_ADD(preempt_to_idle, PREEMPT_TO_IDLE_ON_QUANTUM_EXPIRY) 2643 2644 #undef MAKE_CONTEXT_POLICY_ADD 2645 2646 static int __guc_context_set_context_policies(struct intel_guc *guc, 2647 struct context_policy *policy, 2648 bool loop) 2649 { 2650 return guc_submission_send_busy_loop(guc, (u32 *)&policy->h2g, 2651 __guc_context_policy_action_size(policy), 2652 0, loop); 2653 } 2654 2655 static int guc_context_policy_init_v70(struct intel_context *ce, bool loop) 2656 { 2657 struct intel_engine_cs *engine = ce->engine; 2658 struct intel_guc *guc = &engine->gt->uc.guc; 2659 struct context_policy policy; 2660 u32 execution_quantum; 2661 u32 preemption_timeout; 2662 unsigned long flags; 2663 int ret; 2664 2665 /* NB: For both of these, zero means disabled. */ 2666 GEM_BUG_ON(overflows_type(engine->props.timeslice_duration_ms * 1000, 2667 execution_quantum)); 2668 GEM_BUG_ON(overflows_type(engine->props.preempt_timeout_ms * 1000, 2669 preemption_timeout)); 2670 execution_quantum = engine->props.timeslice_duration_ms * 1000; 2671 preemption_timeout = engine->props.preempt_timeout_ms * 1000; 2672 2673 __guc_context_policy_start_klv(&policy, ce->guc_id.id); 2674 2675 __guc_context_policy_add_priority(&policy, ce->guc_state.prio); 2676 __guc_context_policy_add_execution_quantum(&policy, execution_quantum); 2677 __guc_context_policy_add_preemption_timeout(&policy, preemption_timeout); 2678 2679 if (engine->flags & I915_ENGINE_WANT_FORCED_PREEMPTION) 2680 __guc_context_policy_add_preempt_to_idle(&policy, 1); 2681 2682 ret = __guc_context_set_context_policies(guc, &policy, loop); 2683 2684 spin_lock_irqsave(&ce->guc_state.lock, flags); 2685 if (ret != 0) 2686 set_context_policy_required(ce); 2687 else 2688 clr_context_policy_required(ce); 2689 spin_unlock_irqrestore(&ce->guc_state.lock, flags); 2690 2691 return ret; 2692 } 2693 2694 static void guc_context_policy_init_v69(struct intel_engine_cs *engine, 2695 struct guc_lrc_desc_v69 *desc) 2696 { 2697 desc->policy_flags = 0; 2698 2699 if (engine->flags & I915_ENGINE_WANT_FORCED_PREEMPTION) 2700 desc->policy_flags |= CONTEXT_POLICY_FLAG_PREEMPT_TO_IDLE_V69; 2701 2702 /* NB: For both of these, zero means disabled. */ 2703 GEM_BUG_ON(overflows_type(engine->props.timeslice_duration_ms * 1000, 2704 desc->execution_quantum)); 2705 GEM_BUG_ON(overflows_type(engine->props.preempt_timeout_ms * 1000, 2706 desc->preemption_timeout)); 2707 desc->execution_quantum = engine->props.timeslice_duration_ms * 1000; 2708 desc->preemption_timeout = engine->props.preempt_timeout_ms * 1000; 2709 } 2710 2711 static u32 map_guc_prio_to_lrc_desc_prio(u8 prio) 2712 { 2713 /* 2714 * this matches the mapping we do in map_i915_prio_to_guc_prio() 2715 * (e.g. prio < I915_PRIORITY_NORMAL maps to GUC_CLIENT_PRIORITY_NORMAL) 2716 */ 2717 switch (prio) { 2718 default: 2719 MISSING_CASE(prio); 2720 fallthrough; 2721 case GUC_CLIENT_PRIORITY_KMD_NORMAL: 2722 return GEN12_CTX_PRIORITY_NORMAL; 2723 case GUC_CLIENT_PRIORITY_NORMAL: 2724 return GEN12_CTX_PRIORITY_LOW; 2725 case GUC_CLIENT_PRIORITY_HIGH: 2726 case GUC_CLIENT_PRIORITY_KMD_HIGH: 2727 return GEN12_CTX_PRIORITY_HIGH; 2728 } 2729 } 2730 2731 static void prepare_context_registration_info_v69(struct intel_context *ce) 2732 { 2733 struct intel_engine_cs *engine = ce->engine; 2734 struct intel_guc *guc = &engine->gt->uc.guc; 2735 u32 ctx_id = ce->guc_id.id; 2736 struct guc_lrc_desc_v69 *desc; 2737 struct intel_context *child; 2738 2739 GEM_BUG_ON(!engine->mask); 2740 2741 /* 2742 * Ensure LRC + CT vmas are is same region as write barrier is done 2743 * based on CT vma region. 2744 */ 2745 GEM_BUG_ON(i915_gem_object_is_lmem(guc->ct.vma->obj) != 2746 i915_gem_object_is_lmem(ce->ring->vma->obj)); 2747 2748 desc = __get_lrc_desc_v69(guc, ctx_id); 2749 GEM_BUG_ON(!desc); 2750 desc->engine_class = engine_class_to_guc_class(engine->class); 2751 desc->engine_submit_mask = engine->logical_mask; 2752 desc->hw_context_desc = ce->lrc.lrca; 2753 desc->priority = ce->guc_state.prio; 2754 desc->context_flags = CONTEXT_REGISTRATION_FLAG_KMD; 2755 guc_context_policy_init_v69(engine, desc); 2756 2757 /* 2758 * If context is a parent, we need to register a process descriptor 2759 * describing a work queue and register all child contexts. 2760 */ 2761 if (intel_context_is_parent(ce)) { 2762 struct guc_process_desc_v69 *pdesc; 2763 2764 ce->parallel.guc.wqi_tail = 0; 2765 ce->parallel.guc.wqi_head = 0; 2766 2767 desc->process_desc = i915_ggtt_offset(ce->state) + 2768 __get_parent_scratch_offset(ce); 2769 desc->wq_addr = i915_ggtt_offset(ce->state) + 2770 __get_wq_offset(ce); 2771 desc->wq_size = WQ_SIZE; 2772 2773 pdesc = __get_process_desc_v69(ce); 2774 memset(pdesc, 0, sizeof(*(pdesc))); 2775 pdesc->stage_id = ce->guc_id.id; 2776 pdesc->wq_base_addr = desc->wq_addr; 2777 pdesc->wq_size_bytes = desc->wq_size; 2778 pdesc->wq_status = WQ_STATUS_ACTIVE; 2779 2780 ce->parallel.guc.wq_head = &pdesc->head; 2781 ce->parallel.guc.wq_tail = &pdesc->tail; 2782 ce->parallel.guc.wq_status = &pdesc->wq_status; 2783 2784 for_each_child(ce, child) { 2785 desc = __get_lrc_desc_v69(guc, child->guc_id.id); 2786 2787 desc->engine_class = 2788 engine_class_to_guc_class(engine->class); 2789 desc->hw_context_desc = child->lrc.lrca; 2790 desc->priority = ce->guc_state.prio; 2791 desc->context_flags = CONTEXT_REGISTRATION_FLAG_KMD; 2792 guc_context_policy_init_v69(engine, desc); 2793 } 2794 2795 clear_children_join_go_memory(ce); 2796 } 2797 } 2798 2799 static void prepare_context_registration_info_v70(struct intel_context *ce, 2800 struct guc_ctxt_registration_info *info) 2801 { 2802 struct intel_engine_cs *engine = ce->engine; 2803 struct intel_guc *guc = &engine->gt->uc.guc; 2804 u32 ctx_id = ce->guc_id.id; 2805 2806 GEM_BUG_ON(!engine->mask); 2807 2808 /* 2809 * Ensure LRC + CT vmas are is same region as write barrier is done 2810 * based on CT vma region. 2811 */ 2812 GEM_BUG_ON(i915_gem_object_is_lmem(guc->ct.vma->obj) != 2813 i915_gem_object_is_lmem(ce->ring->vma->obj)); 2814 2815 memset(info, 0, sizeof(*info)); 2816 info->context_idx = ctx_id; 2817 info->engine_class = engine_class_to_guc_class(engine->class); 2818 info->engine_submit_mask = engine->logical_mask; 2819 /* 2820 * NB: GuC interface supports 64 bit LRCA even though i915/HW 2821 * only supports 32 bit currently. 2822 */ 2823 info->hwlrca_lo = lower_32_bits(ce->lrc.lrca); 2824 info->hwlrca_hi = upper_32_bits(ce->lrc.lrca); 2825 if (engine->flags & I915_ENGINE_HAS_EU_PRIORITY) 2826 info->hwlrca_lo |= map_guc_prio_to_lrc_desc_prio(ce->guc_state.prio); 2827 info->flags = CONTEXT_REGISTRATION_FLAG_KMD; 2828 2829 /* 2830 * If context is a parent, we need to register a process descriptor 2831 * describing a work queue and register all child contexts. 2832 */ 2833 if (intel_context_is_parent(ce)) { 2834 struct guc_sched_wq_desc *wq_desc; 2835 u64 wq_desc_offset, wq_base_offset; 2836 2837 ce->parallel.guc.wqi_tail = 0; 2838 ce->parallel.guc.wqi_head = 0; 2839 2840 wq_desc_offset = i915_ggtt_offset(ce->state) + 2841 __get_parent_scratch_offset(ce); 2842 wq_base_offset = i915_ggtt_offset(ce->state) + 2843 __get_wq_offset(ce); 2844 info->wq_desc_lo = lower_32_bits(wq_desc_offset); 2845 info->wq_desc_hi = upper_32_bits(wq_desc_offset); 2846 info->wq_base_lo = lower_32_bits(wq_base_offset); 2847 info->wq_base_hi = upper_32_bits(wq_base_offset); 2848 info->wq_size = WQ_SIZE; 2849 2850 wq_desc = __get_wq_desc_v70(ce); 2851 memset(wq_desc, 0, sizeof(*wq_desc)); 2852 wq_desc->wq_status = WQ_STATUS_ACTIVE; 2853 2854 ce->parallel.guc.wq_head = &wq_desc->head; 2855 ce->parallel.guc.wq_tail = &wq_desc->tail; 2856 ce->parallel.guc.wq_status = &wq_desc->wq_status; 2857 2858 clear_children_join_go_memory(ce); 2859 } 2860 } 2861 2862 static int try_context_registration(struct intel_context *ce, bool loop) 2863 { 2864 struct intel_engine_cs *engine = ce->engine; 2865 struct intel_runtime_pm *runtime_pm = engine->uncore->rpm; 2866 struct intel_guc *guc = &engine->gt->uc.guc; 2867 intel_wakeref_t wakeref; 2868 u32 ctx_id = ce->guc_id.id; 2869 bool context_registered; 2870 int ret = 0; 2871 2872 GEM_BUG_ON(!sched_state_is_init(ce)); 2873 2874 context_registered = ctx_id_mapped(guc, ctx_id); 2875 2876 clr_ctx_id_mapping(guc, ctx_id); 2877 set_ctx_id_mapping(guc, ctx_id, ce); 2878 2879 /* 2880 * The context_lookup xarray is used to determine if the hardware 2881 * context is currently registered. There are two cases in which it 2882 * could be registered either the guc_id has been stolen from another 2883 * context or the lrc descriptor address of this context has changed. In 2884 * either case the context needs to be deregistered with the GuC before 2885 * registering this context. 2886 */ 2887 if (context_registered) { 2888 bool disabled; 2889 unsigned long flags; 2890 2891 trace_intel_context_steal_guc_id(ce); 2892 GEM_BUG_ON(!loop); 2893 2894 /* Seal race with Reset */ 2895 spin_lock_irqsave(&ce->guc_state.lock, flags); 2896 disabled = submission_disabled(guc); 2897 if (likely(!disabled)) { 2898 set_context_wait_for_deregister_to_register(ce); 2899 intel_context_get(ce); 2900 } 2901 spin_unlock_irqrestore(&ce->guc_state.lock, flags); 2902 if (unlikely(disabled)) { 2903 clr_ctx_id_mapping(guc, ctx_id); 2904 return 0; /* Will get registered later */ 2905 } 2906 2907 /* 2908 * If stealing the guc_id, this ce has the same guc_id as the 2909 * context whose guc_id was stolen. 2910 */ 2911 with_intel_runtime_pm(runtime_pm, wakeref) 2912 ret = deregister_context(ce, ce->guc_id.id); 2913 if (unlikely(ret == -ENODEV)) 2914 ret = 0; /* Will get registered later */ 2915 } else { 2916 with_intel_runtime_pm(runtime_pm, wakeref) 2917 ret = register_context(ce, loop); 2918 if (unlikely(ret == -EBUSY)) { 2919 clr_ctx_id_mapping(guc, ctx_id); 2920 } else if (unlikely(ret == -ENODEV)) { 2921 clr_ctx_id_mapping(guc, ctx_id); 2922 ret = 0; /* Will get registered later */ 2923 } 2924 } 2925 2926 return ret; 2927 } 2928 2929 static int __guc_context_pre_pin(struct intel_context *ce, 2930 struct intel_engine_cs *engine, 2931 struct i915_gem_ww_ctx *ww, 2932 void **vaddr) 2933 { 2934 return lrc_pre_pin(ce, engine, ww, vaddr); 2935 } 2936 2937 static int __guc_context_pin(struct intel_context *ce, 2938 struct intel_engine_cs *engine, 2939 void *vaddr) 2940 { 2941 if (i915_ggtt_offset(ce->state) != 2942 (ce->lrc.lrca & CTX_GTT_ADDRESS_MASK)) 2943 set_bit(CONTEXT_LRCA_DIRTY, &ce->flags); 2944 2945 /* 2946 * GuC context gets pinned in guc_request_alloc. See that function for 2947 * explaination of why. 2948 */ 2949 2950 return lrc_pin(ce, engine, vaddr); 2951 } 2952 2953 static int guc_context_pre_pin(struct intel_context *ce, 2954 struct i915_gem_ww_ctx *ww, 2955 void **vaddr) 2956 { 2957 return __guc_context_pre_pin(ce, ce->engine, ww, vaddr); 2958 } 2959 2960 static int guc_context_pin(struct intel_context *ce, void *vaddr) 2961 { 2962 int ret = __guc_context_pin(ce, ce->engine, vaddr); 2963 2964 if (likely(!ret && !intel_context_is_barrier(ce))) 2965 intel_engine_pm_get(ce->engine); 2966 2967 return ret; 2968 } 2969 2970 static void guc_context_unpin(struct intel_context *ce) 2971 { 2972 struct intel_guc *guc = ce_to_guc(ce); 2973 2974 __guc_context_update_stats(ce); 2975 unpin_guc_id(guc, ce); 2976 lrc_unpin(ce); 2977 2978 if (likely(!intel_context_is_barrier(ce))) 2979 intel_engine_pm_put_async(ce->engine); 2980 } 2981 2982 static void guc_context_post_unpin(struct intel_context *ce) 2983 { 2984 lrc_post_unpin(ce); 2985 } 2986 2987 static void __guc_context_sched_enable(struct intel_guc *guc, 2988 struct intel_context *ce) 2989 { 2990 u32 action[] = { 2991 INTEL_GUC_ACTION_SCHED_CONTEXT_MODE_SET, 2992 ce->guc_id.id, 2993 GUC_CONTEXT_ENABLE 2994 }; 2995 2996 trace_intel_context_sched_enable(ce); 2997 2998 guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action), 2999 G2H_LEN_DW_SCHED_CONTEXT_MODE_SET, true); 3000 } 3001 3002 static void __guc_context_sched_disable(struct intel_guc *guc, 3003 struct intel_context *ce, 3004 u16 guc_id) 3005 { 3006 u32 action[] = { 3007 INTEL_GUC_ACTION_SCHED_CONTEXT_MODE_SET, 3008 guc_id, /* ce->guc_id.id not stable */ 3009 GUC_CONTEXT_DISABLE 3010 }; 3011 3012 GEM_BUG_ON(guc_id == GUC_INVALID_CONTEXT_ID); 3013 3014 GEM_BUG_ON(intel_context_is_child(ce)); 3015 trace_intel_context_sched_disable(ce); 3016 3017 guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action), 3018 G2H_LEN_DW_SCHED_CONTEXT_MODE_SET, true); 3019 } 3020 3021 static void guc_blocked_fence_complete(struct intel_context *ce) 3022 { 3023 lockdep_assert_held(&ce->guc_state.lock); 3024 3025 if (!i915_sw_fence_done(&ce->guc_state.blocked)) 3026 i915_sw_fence_complete(&ce->guc_state.blocked); 3027 } 3028 3029 static void guc_blocked_fence_reinit(struct intel_context *ce) 3030 { 3031 lockdep_assert_held(&ce->guc_state.lock); 3032 GEM_BUG_ON(!i915_sw_fence_done(&ce->guc_state.blocked)); 3033 3034 /* 3035 * This fence is always complete unless a pending schedule disable is 3036 * outstanding. We arm the fence here and complete it when we receive 3037 * the pending schedule disable complete message. 3038 */ 3039 i915_sw_fence_fini(&ce->guc_state.blocked); 3040 i915_sw_fence_reinit(&ce->guc_state.blocked); 3041 i915_sw_fence_await(&ce->guc_state.blocked); 3042 i915_sw_fence_commit(&ce->guc_state.blocked); 3043 } 3044 3045 static u16 prep_context_pending_disable(struct intel_context *ce) 3046 { 3047 lockdep_assert_held(&ce->guc_state.lock); 3048 3049 set_context_pending_disable(ce); 3050 clr_context_enabled(ce); 3051 guc_blocked_fence_reinit(ce); 3052 intel_context_get(ce); 3053 3054 return ce->guc_id.id; 3055 } 3056 3057 static struct i915_sw_fence *guc_context_block(struct intel_context *ce) 3058 { 3059 struct intel_guc *guc = ce_to_guc(ce); 3060 unsigned long flags; 3061 struct intel_runtime_pm *runtime_pm = ce->engine->uncore->rpm; 3062 intel_wakeref_t wakeref; 3063 u16 guc_id; 3064 bool enabled; 3065 3066 GEM_BUG_ON(intel_context_is_child(ce)); 3067 3068 spin_lock_irqsave(&ce->guc_state.lock, flags); 3069 3070 incr_context_blocked(ce); 3071 3072 enabled = context_enabled(ce); 3073 if (unlikely(!enabled || submission_disabled(guc))) { 3074 if (enabled) 3075 clr_context_enabled(ce); 3076 spin_unlock_irqrestore(&ce->guc_state.lock, flags); 3077 return &ce->guc_state.blocked; 3078 } 3079 3080 /* 3081 * We add +2 here as the schedule disable complete CTB handler calls 3082 * intel_context_sched_disable_unpin (-2 to pin_count). 3083 */ 3084 atomic_add(2, &ce->pin_count); 3085 3086 guc_id = prep_context_pending_disable(ce); 3087 3088 spin_unlock_irqrestore(&ce->guc_state.lock, flags); 3089 3090 with_intel_runtime_pm(runtime_pm, wakeref) 3091 __guc_context_sched_disable(guc, ce, guc_id); 3092 3093 return &ce->guc_state.blocked; 3094 } 3095 3096 #define SCHED_STATE_MULTI_BLOCKED_MASK \ 3097 (SCHED_STATE_BLOCKED_MASK & ~SCHED_STATE_BLOCKED) 3098 #define SCHED_STATE_NO_UNBLOCK \ 3099 (SCHED_STATE_MULTI_BLOCKED_MASK | \ 3100 SCHED_STATE_PENDING_DISABLE | \ 3101 SCHED_STATE_BANNED) 3102 3103 static bool context_cant_unblock(struct intel_context *ce) 3104 { 3105 lockdep_assert_held(&ce->guc_state.lock); 3106 3107 return (ce->guc_state.sched_state & SCHED_STATE_NO_UNBLOCK) || 3108 context_guc_id_invalid(ce) || 3109 !ctx_id_mapped(ce_to_guc(ce), ce->guc_id.id) || 3110 !intel_context_is_pinned(ce); 3111 } 3112 3113 static void guc_context_unblock(struct intel_context *ce) 3114 { 3115 struct intel_guc *guc = ce_to_guc(ce); 3116 unsigned long flags; 3117 struct intel_runtime_pm *runtime_pm = ce->engine->uncore->rpm; 3118 intel_wakeref_t wakeref; 3119 bool enable; 3120 3121 GEM_BUG_ON(context_enabled(ce)); 3122 GEM_BUG_ON(intel_context_is_child(ce)); 3123 3124 spin_lock_irqsave(&ce->guc_state.lock, flags); 3125 3126 if (unlikely(submission_disabled(guc) || 3127 context_cant_unblock(ce))) { 3128 enable = false; 3129 } else { 3130 enable = true; 3131 set_context_pending_enable(ce); 3132 set_context_enabled(ce); 3133 intel_context_get(ce); 3134 } 3135 3136 decr_context_blocked(ce); 3137 3138 spin_unlock_irqrestore(&ce->guc_state.lock, flags); 3139 3140 if (enable) { 3141 with_intel_runtime_pm(runtime_pm, wakeref) 3142 __guc_context_sched_enable(guc, ce); 3143 } 3144 } 3145 3146 static void guc_context_cancel_request(struct intel_context *ce, 3147 struct i915_request *rq) 3148 { 3149 struct intel_context *block_context = 3150 request_to_scheduling_context(rq); 3151 3152 if (i915_sw_fence_signaled(&rq->submit)) { 3153 struct i915_sw_fence *fence; 3154 3155 intel_context_get(ce); 3156 fence = guc_context_block(block_context); 3157 i915_sw_fence_wait(fence); 3158 if (!i915_request_completed(rq)) { 3159 __i915_request_skip(rq); 3160 guc_reset_state(ce, intel_ring_wrap(ce->ring, rq->head), 3161 true); 3162 } 3163 3164 guc_context_unblock(block_context); 3165 intel_context_put(ce); 3166 } 3167 } 3168 3169 static void __guc_context_set_preemption_timeout(struct intel_guc *guc, 3170 u16 guc_id, 3171 u32 preemption_timeout) 3172 { 3173 if (GUC_SUBMIT_VER(guc) >= MAKE_GUC_VER(1, 0, 0)) { 3174 struct context_policy policy; 3175 3176 __guc_context_policy_start_klv(&policy, guc_id); 3177 __guc_context_policy_add_preemption_timeout(&policy, preemption_timeout); 3178 __guc_context_set_context_policies(guc, &policy, true); 3179 } else { 3180 u32 action[] = { 3181 INTEL_GUC_ACTION_V69_SET_CONTEXT_PREEMPTION_TIMEOUT, 3182 guc_id, 3183 preemption_timeout 3184 }; 3185 3186 intel_guc_send_busy_loop(guc, action, ARRAY_SIZE(action), 0, true); 3187 } 3188 } 3189 3190 static void 3191 guc_context_revoke(struct intel_context *ce, struct i915_request *rq, 3192 unsigned int preempt_timeout_ms) 3193 { 3194 struct intel_guc *guc = ce_to_guc(ce); 3195 struct intel_runtime_pm *runtime_pm = 3196 &ce->engine->gt->i915->runtime_pm; 3197 intel_wakeref_t wakeref; 3198 unsigned long flags; 3199 3200 GEM_BUG_ON(intel_context_is_child(ce)); 3201 3202 guc_flush_submissions(guc); 3203 3204 spin_lock_irqsave(&ce->guc_state.lock, flags); 3205 set_context_banned(ce); 3206 3207 if (submission_disabled(guc) || 3208 (!context_enabled(ce) && !context_pending_disable(ce))) { 3209 spin_unlock_irqrestore(&ce->guc_state.lock, flags); 3210 3211 guc_cancel_context_requests(ce); 3212 intel_engine_signal_breadcrumbs(ce->engine); 3213 } else if (!context_pending_disable(ce)) { 3214 u16 guc_id; 3215 3216 /* 3217 * We add +2 here as the schedule disable complete CTB handler 3218 * calls intel_context_sched_disable_unpin (-2 to pin_count). 3219 */ 3220 atomic_add(2, &ce->pin_count); 3221 3222 guc_id = prep_context_pending_disable(ce); 3223 spin_unlock_irqrestore(&ce->guc_state.lock, flags); 3224 3225 /* 3226 * In addition to disabling scheduling, set the preemption 3227 * timeout to the minimum value (1 us) so the banned context 3228 * gets kicked off the HW ASAP. 3229 */ 3230 with_intel_runtime_pm(runtime_pm, wakeref) { 3231 __guc_context_set_preemption_timeout(guc, guc_id, 3232 preempt_timeout_ms); 3233 __guc_context_sched_disable(guc, ce, guc_id); 3234 } 3235 } else { 3236 if (!context_guc_id_invalid(ce)) 3237 with_intel_runtime_pm(runtime_pm, wakeref) 3238 __guc_context_set_preemption_timeout(guc, 3239 ce->guc_id.id, 3240 preempt_timeout_ms); 3241 spin_unlock_irqrestore(&ce->guc_state.lock, flags); 3242 } 3243 } 3244 3245 static void do_sched_disable(struct intel_guc *guc, struct intel_context *ce, 3246 unsigned long flags) 3247 __releases(ce->guc_state.lock) 3248 { 3249 struct intel_runtime_pm *runtime_pm = &ce->engine->gt->i915->runtime_pm; 3250 intel_wakeref_t wakeref; 3251 u16 guc_id; 3252 3253 lockdep_assert_held(&ce->guc_state.lock); 3254 guc_id = prep_context_pending_disable(ce); 3255 3256 spin_unlock_irqrestore(&ce->guc_state.lock, flags); 3257 3258 with_intel_runtime_pm(runtime_pm, wakeref) 3259 __guc_context_sched_disable(guc, ce, guc_id); 3260 } 3261 3262 static bool bypass_sched_disable(struct intel_guc *guc, 3263 struct intel_context *ce) 3264 { 3265 lockdep_assert_held(&ce->guc_state.lock); 3266 GEM_BUG_ON(intel_context_is_child(ce)); 3267 3268 if (submission_disabled(guc) || context_guc_id_invalid(ce) || 3269 !ctx_id_mapped(guc, ce->guc_id.id)) { 3270 clr_context_enabled(ce); 3271 return true; 3272 } 3273 3274 return !context_enabled(ce); 3275 } 3276 3277 static void __delay_sched_disable(struct work_struct *wrk) 3278 { 3279 struct intel_context *ce = 3280 container_of(wrk, typeof(*ce), guc_state.sched_disable_delay_work.work); 3281 struct intel_guc *guc = ce_to_guc(ce); 3282 unsigned long flags; 3283 3284 spin_lock_irqsave(&ce->guc_state.lock, flags); 3285 3286 if (bypass_sched_disable(guc, ce)) { 3287 spin_unlock_irqrestore(&ce->guc_state.lock, flags); 3288 intel_context_sched_disable_unpin(ce); 3289 } else { 3290 do_sched_disable(guc, ce, flags); 3291 } 3292 } 3293 3294 static bool guc_id_pressure(struct intel_guc *guc, struct intel_context *ce) 3295 { 3296 /* 3297 * parent contexts are perma-pinned, if we are unpinning do schedule 3298 * disable immediately. 3299 */ 3300 if (intel_context_is_parent(ce)) 3301 return true; 3302 3303 /* 3304 * If we are beyond the threshold for avail guc_ids, do schedule disable immediately. 3305 */ 3306 return guc->submission_state.guc_ids_in_use > 3307 guc->submission_state.sched_disable_gucid_threshold; 3308 } 3309 3310 static void guc_context_sched_disable(struct intel_context *ce) 3311 { 3312 struct intel_guc *guc = ce_to_guc(ce); 3313 u64 delay = guc->submission_state.sched_disable_delay_ms; 3314 unsigned long flags; 3315 3316 spin_lock_irqsave(&ce->guc_state.lock, flags); 3317 3318 if (bypass_sched_disable(guc, ce)) { 3319 spin_unlock_irqrestore(&ce->guc_state.lock, flags); 3320 intel_context_sched_disable_unpin(ce); 3321 } else if (!intel_context_is_closed(ce) && !guc_id_pressure(guc, ce) && 3322 delay) { 3323 spin_unlock_irqrestore(&ce->guc_state.lock, flags); 3324 mod_delayed_work(system_unbound_wq, 3325 &ce->guc_state.sched_disable_delay_work, 3326 msecs_to_jiffies(delay)); 3327 } else { 3328 do_sched_disable(guc, ce, flags); 3329 } 3330 } 3331 3332 static void guc_context_close(struct intel_context *ce) 3333 { 3334 unsigned long flags; 3335 3336 if (test_bit(CONTEXT_GUC_INIT, &ce->flags) && 3337 cancel_delayed_work(&ce->guc_state.sched_disable_delay_work)) 3338 __delay_sched_disable(&ce->guc_state.sched_disable_delay_work.work); 3339 3340 spin_lock_irqsave(&ce->guc_state.lock, flags); 3341 set_context_close_done(ce); 3342 spin_unlock_irqrestore(&ce->guc_state.lock, flags); 3343 } 3344 3345 static inline int guc_lrc_desc_unpin(struct intel_context *ce) 3346 { 3347 struct intel_guc *guc = ce_to_guc(ce); 3348 struct intel_gt *gt = guc_to_gt(guc); 3349 unsigned long flags; 3350 bool disabled; 3351 int ret; 3352 3353 GEM_BUG_ON(!intel_gt_pm_is_awake(gt)); 3354 GEM_BUG_ON(!ctx_id_mapped(guc, ce->guc_id.id)); 3355 GEM_BUG_ON(ce != __get_context(guc, ce->guc_id.id)); 3356 GEM_BUG_ON(context_enabled(ce)); 3357 3358 /* Seal race with Reset */ 3359 spin_lock_irqsave(&ce->guc_state.lock, flags); 3360 disabled = submission_disabled(guc); 3361 if (likely(!disabled)) { 3362 /* 3363 * Take a gt-pm ref and change context state to be destroyed. 3364 * NOTE: a G2H IRQ that comes after will put this gt-pm ref back 3365 */ 3366 __intel_gt_pm_get(gt); 3367 set_context_destroyed(ce); 3368 clr_context_registered(ce); 3369 } 3370 spin_unlock_irqrestore(&ce->guc_state.lock, flags); 3371 3372 if (unlikely(disabled)) { 3373 release_guc_id(guc, ce); 3374 __guc_context_destroy(ce); 3375 return 0; 3376 } 3377 3378 /* 3379 * GuC is active, lets destroy this context, but at this point we can still be racing 3380 * with suspend, so we undo everything if the H2G fails in deregister_context so 3381 * that GuC reset will find this context during clean up. 3382 */ 3383 ret = deregister_context(ce, ce->guc_id.id); 3384 if (ret) { 3385 spin_lock(&ce->guc_state.lock); 3386 set_context_registered(ce); 3387 clr_context_destroyed(ce); 3388 spin_unlock(&ce->guc_state.lock); 3389 /* 3390 * As gt-pm is awake at function entry, intel_wakeref_put_async merely decrements 3391 * the wakeref immediately but per function spec usage call this after unlock. 3392 */ 3393 intel_wakeref_put_async(>->wakeref); 3394 } 3395 3396 return ret; 3397 } 3398 3399 static void __guc_context_destroy(struct intel_context *ce) 3400 { 3401 GEM_BUG_ON(ce->guc_state.prio_count[GUC_CLIENT_PRIORITY_KMD_HIGH] || 3402 ce->guc_state.prio_count[GUC_CLIENT_PRIORITY_HIGH] || 3403 ce->guc_state.prio_count[GUC_CLIENT_PRIORITY_KMD_NORMAL] || 3404 ce->guc_state.prio_count[GUC_CLIENT_PRIORITY_NORMAL]); 3405 3406 lrc_fini(ce); 3407 intel_context_fini(ce); 3408 3409 if (intel_engine_is_virtual(ce->engine)) { 3410 struct guc_virtual_engine *ve = 3411 container_of(ce, typeof(*ve), context); 3412 3413 if (ve->base.breadcrumbs) 3414 intel_breadcrumbs_put(ve->base.breadcrumbs); 3415 3416 kfree(ve); 3417 } else { 3418 intel_context_free(ce); 3419 } 3420 } 3421 3422 static void guc_flush_destroyed_contexts(struct intel_guc *guc) 3423 { 3424 struct intel_context *ce; 3425 unsigned long flags; 3426 3427 GEM_BUG_ON(!submission_disabled(guc) && 3428 guc_submission_initialized(guc)); 3429 3430 while (!list_empty(&guc->submission_state.destroyed_contexts)) { 3431 spin_lock_irqsave(&guc->submission_state.lock, flags); 3432 ce = list_first_entry_or_null(&guc->submission_state.destroyed_contexts, 3433 struct intel_context, 3434 destroyed_link); 3435 if (ce) 3436 list_del_init(&ce->destroyed_link); 3437 spin_unlock_irqrestore(&guc->submission_state.lock, flags); 3438 3439 if (!ce) 3440 break; 3441 3442 release_guc_id(guc, ce); 3443 __guc_context_destroy(ce); 3444 } 3445 } 3446 3447 static void deregister_destroyed_contexts(struct intel_guc *guc) 3448 { 3449 struct intel_context *ce; 3450 unsigned long flags; 3451 3452 while (!list_empty(&guc->submission_state.destroyed_contexts)) { 3453 spin_lock_irqsave(&guc->submission_state.lock, flags); 3454 ce = list_first_entry_or_null(&guc->submission_state.destroyed_contexts, 3455 struct intel_context, 3456 destroyed_link); 3457 if (ce) 3458 list_del_init(&ce->destroyed_link); 3459 spin_unlock_irqrestore(&guc->submission_state.lock, flags); 3460 3461 if (!ce) 3462 break; 3463 3464 if (guc_lrc_desc_unpin(ce)) { 3465 /* 3466 * This means GuC's CT link severed mid-way which could happen 3467 * in suspend-resume corner cases. In this case, put the 3468 * context back into the destroyed_contexts list which will 3469 * get picked up on the next context deregistration event or 3470 * purged in a GuC sanitization event (reset/unload/wedged/...). 3471 */ 3472 spin_lock_irqsave(&guc->submission_state.lock, flags); 3473 list_add_tail(&ce->destroyed_link, 3474 &guc->submission_state.destroyed_contexts); 3475 spin_unlock_irqrestore(&guc->submission_state.lock, flags); 3476 /* Bail now since the list might never be emptied if h2gs fail */ 3477 break; 3478 } 3479 3480 } 3481 } 3482 3483 static void destroyed_worker_func(struct work_struct *w) 3484 { 3485 struct intel_guc *guc = container_of(w, struct intel_guc, 3486 submission_state.destroyed_worker); 3487 struct intel_gt *gt = guc_to_gt(guc); 3488 intel_wakeref_t wakeref; 3489 3490 /* 3491 * In rare cases we can get here via async context-free fence-signals that 3492 * come very late in suspend flow or very early in resume flows. In these 3493 * cases, GuC won't be ready but just skipping it here is fine as these 3494 * pending-destroy-contexts get destroyed totally at GuC reset time at the 3495 * end of suspend.. OR.. this worker can be picked up later on the next 3496 * context destruction trigger after resume-completes 3497 */ 3498 if (!intel_guc_is_ready(guc)) 3499 return; 3500 3501 with_intel_gt_pm(gt, wakeref) 3502 deregister_destroyed_contexts(guc); 3503 } 3504 3505 static void guc_context_destroy(struct kref *kref) 3506 { 3507 struct intel_context *ce = container_of(kref, typeof(*ce), ref); 3508 struct intel_guc *guc = ce_to_guc(ce); 3509 unsigned long flags; 3510 bool destroy; 3511 3512 /* 3513 * If the guc_id is invalid this context has been stolen and we can free 3514 * it immediately. Also can be freed immediately if the context is not 3515 * registered with the GuC or the GuC is in the middle of a reset. 3516 */ 3517 spin_lock_irqsave(&guc->submission_state.lock, flags); 3518 destroy = submission_disabled(guc) || context_guc_id_invalid(ce) || 3519 !ctx_id_mapped(guc, ce->guc_id.id); 3520 if (likely(!destroy)) { 3521 if (!list_empty(&ce->guc_id.link)) 3522 list_del_init(&ce->guc_id.link); 3523 list_add_tail(&ce->destroyed_link, 3524 &guc->submission_state.destroyed_contexts); 3525 } else { 3526 __release_guc_id(guc, ce); 3527 } 3528 spin_unlock_irqrestore(&guc->submission_state.lock, flags); 3529 if (unlikely(destroy)) { 3530 __guc_context_destroy(ce); 3531 return; 3532 } 3533 3534 /* 3535 * We use a worker to issue the H2G to deregister the context as we can 3536 * take the GT PM for the first time which isn't allowed from an atomic 3537 * context. 3538 */ 3539 queue_work(system_unbound_wq, &guc->submission_state.destroyed_worker); 3540 } 3541 3542 static int guc_context_alloc(struct intel_context *ce) 3543 { 3544 return lrc_alloc(ce, ce->engine); 3545 } 3546 3547 static void __guc_context_set_prio(struct intel_guc *guc, 3548 struct intel_context *ce) 3549 { 3550 if (GUC_SUBMIT_VER(guc) >= MAKE_GUC_VER(1, 0, 0)) { 3551 struct context_policy policy; 3552 3553 __guc_context_policy_start_klv(&policy, ce->guc_id.id); 3554 __guc_context_policy_add_priority(&policy, ce->guc_state.prio); 3555 __guc_context_set_context_policies(guc, &policy, true); 3556 } else { 3557 u32 action[] = { 3558 INTEL_GUC_ACTION_V69_SET_CONTEXT_PRIORITY, 3559 ce->guc_id.id, 3560 ce->guc_state.prio, 3561 }; 3562 3563 guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action), 0, true); 3564 } 3565 } 3566 3567 static void guc_context_set_prio(struct intel_guc *guc, 3568 struct intel_context *ce, 3569 u8 prio) 3570 { 3571 GEM_BUG_ON(prio < GUC_CLIENT_PRIORITY_KMD_HIGH || 3572 prio > GUC_CLIENT_PRIORITY_NORMAL); 3573 lockdep_assert_held(&ce->guc_state.lock); 3574 3575 if (ce->guc_state.prio == prio || submission_disabled(guc) || 3576 !context_registered(ce)) { 3577 ce->guc_state.prio = prio; 3578 return; 3579 } 3580 3581 ce->guc_state.prio = prio; 3582 __guc_context_set_prio(guc, ce); 3583 3584 trace_intel_context_set_prio(ce); 3585 } 3586 3587 static inline u8 map_i915_prio_to_guc_prio(int prio) 3588 { 3589 if (prio == I915_PRIORITY_NORMAL) 3590 return GUC_CLIENT_PRIORITY_KMD_NORMAL; 3591 else if (prio < I915_PRIORITY_NORMAL) 3592 return GUC_CLIENT_PRIORITY_NORMAL; 3593 else if (prio < I915_PRIORITY_DISPLAY) 3594 return GUC_CLIENT_PRIORITY_HIGH; 3595 else 3596 return GUC_CLIENT_PRIORITY_KMD_HIGH; 3597 } 3598 3599 static inline void add_context_inflight_prio(struct intel_context *ce, 3600 u8 guc_prio) 3601 { 3602 lockdep_assert_held(&ce->guc_state.lock); 3603 GEM_BUG_ON(guc_prio >= ARRAY_SIZE(ce->guc_state.prio_count)); 3604 3605 ++ce->guc_state.prio_count[guc_prio]; 3606 3607 /* Overflow protection */ 3608 GEM_WARN_ON(!ce->guc_state.prio_count[guc_prio]); 3609 } 3610 3611 static inline void sub_context_inflight_prio(struct intel_context *ce, 3612 u8 guc_prio) 3613 { 3614 lockdep_assert_held(&ce->guc_state.lock); 3615 GEM_BUG_ON(guc_prio >= ARRAY_SIZE(ce->guc_state.prio_count)); 3616 3617 /* Underflow protection */ 3618 GEM_WARN_ON(!ce->guc_state.prio_count[guc_prio]); 3619 3620 --ce->guc_state.prio_count[guc_prio]; 3621 } 3622 3623 static inline void update_context_prio(struct intel_context *ce) 3624 { 3625 struct intel_guc *guc = &ce->engine->gt->uc.guc; 3626 int i; 3627 3628 BUILD_BUG_ON(GUC_CLIENT_PRIORITY_KMD_HIGH != 0); 3629 BUILD_BUG_ON(GUC_CLIENT_PRIORITY_KMD_HIGH > GUC_CLIENT_PRIORITY_NORMAL); 3630 3631 lockdep_assert_held(&ce->guc_state.lock); 3632 3633 for (i = 0; i < ARRAY_SIZE(ce->guc_state.prio_count); ++i) { 3634 if (ce->guc_state.prio_count[i]) { 3635 guc_context_set_prio(guc, ce, i); 3636 break; 3637 } 3638 } 3639 } 3640 3641 static inline bool new_guc_prio_higher(u8 old_guc_prio, u8 new_guc_prio) 3642 { 3643 /* Lower value is higher priority */ 3644 return new_guc_prio < old_guc_prio; 3645 } 3646 3647 static void add_to_context(struct i915_request *rq) 3648 { 3649 struct intel_context *ce = request_to_scheduling_context(rq); 3650 u8 new_guc_prio = map_i915_prio_to_guc_prio(rq_prio(rq)); 3651 3652 GEM_BUG_ON(intel_context_is_child(ce)); 3653 GEM_BUG_ON(rq->guc_prio == GUC_PRIO_FINI); 3654 3655 spin_lock(&ce->guc_state.lock); 3656 list_move_tail(&rq->sched.link, &ce->guc_state.requests); 3657 3658 if (rq->guc_prio == GUC_PRIO_INIT) { 3659 rq->guc_prio = new_guc_prio; 3660 add_context_inflight_prio(ce, rq->guc_prio); 3661 } else if (new_guc_prio_higher(rq->guc_prio, new_guc_prio)) { 3662 sub_context_inflight_prio(ce, rq->guc_prio); 3663 rq->guc_prio = new_guc_prio; 3664 add_context_inflight_prio(ce, rq->guc_prio); 3665 } 3666 update_context_prio(ce); 3667 3668 spin_unlock(&ce->guc_state.lock); 3669 } 3670 3671 static void guc_prio_fini(struct i915_request *rq, struct intel_context *ce) 3672 { 3673 lockdep_assert_held(&ce->guc_state.lock); 3674 3675 if (rq->guc_prio != GUC_PRIO_INIT && 3676 rq->guc_prio != GUC_PRIO_FINI) { 3677 sub_context_inflight_prio(ce, rq->guc_prio); 3678 update_context_prio(ce); 3679 } 3680 rq->guc_prio = GUC_PRIO_FINI; 3681 } 3682 3683 static void remove_from_context(struct i915_request *rq) 3684 { 3685 struct intel_context *ce = request_to_scheduling_context(rq); 3686 3687 GEM_BUG_ON(intel_context_is_child(ce)); 3688 3689 spin_lock_irq(&ce->guc_state.lock); 3690 3691 list_del_init(&rq->sched.link); 3692 clear_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags); 3693 3694 /* Prevent further __await_execution() registering a cb, then flush */ 3695 set_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags); 3696 3697 guc_prio_fini(rq, ce); 3698 3699 spin_unlock_irq(&ce->guc_state.lock); 3700 3701 atomic_dec(&ce->guc_id.ref); 3702 i915_request_notify_execute_cb_imm(rq); 3703 } 3704 3705 static const struct intel_context_ops guc_context_ops = { 3706 .flags = COPS_RUNTIME_CYCLES, 3707 .alloc = guc_context_alloc, 3708 3709 .close = guc_context_close, 3710 3711 .pre_pin = guc_context_pre_pin, 3712 .pin = guc_context_pin, 3713 .unpin = guc_context_unpin, 3714 .post_unpin = guc_context_post_unpin, 3715 3716 .revoke = guc_context_revoke, 3717 3718 .cancel_request = guc_context_cancel_request, 3719 3720 .enter = intel_context_enter_engine, 3721 .exit = intel_context_exit_engine, 3722 3723 .sched_disable = guc_context_sched_disable, 3724 3725 .update_stats = guc_context_update_stats, 3726 3727 .reset = lrc_reset, 3728 .destroy = guc_context_destroy, 3729 3730 .create_virtual = guc_create_virtual, 3731 .create_parallel = guc_create_parallel, 3732 }; 3733 3734 static void submit_work_cb(struct irq_work *wrk) 3735 { 3736 struct i915_request *rq = container_of(wrk, typeof(*rq), submit_work); 3737 3738 might_lock(&rq->engine->sched_engine->lock); 3739 i915_sw_fence_complete(&rq->submit); 3740 } 3741 3742 static void __guc_signal_context_fence(struct intel_context *ce) 3743 { 3744 struct i915_request *rq, *rn; 3745 3746 lockdep_assert_held(&ce->guc_state.lock); 3747 3748 if (!list_empty(&ce->guc_state.fences)) 3749 trace_intel_context_fence_release(ce); 3750 3751 /* 3752 * Use an IRQ to ensure locking order of sched_engine->lock -> 3753 * ce->guc_state.lock is preserved. 3754 */ 3755 list_for_each_entry_safe(rq, rn, &ce->guc_state.fences, 3756 guc_fence_link) { 3757 list_del(&rq->guc_fence_link); 3758 irq_work_queue(&rq->submit_work); 3759 } 3760 3761 INIT_LIST_HEAD(&ce->guc_state.fences); 3762 } 3763 3764 static void guc_signal_context_fence(struct intel_context *ce) 3765 { 3766 unsigned long flags; 3767 3768 GEM_BUG_ON(intel_context_is_child(ce)); 3769 3770 spin_lock_irqsave(&ce->guc_state.lock, flags); 3771 clr_context_wait_for_deregister_to_register(ce); 3772 __guc_signal_context_fence(ce); 3773 spin_unlock_irqrestore(&ce->guc_state.lock, flags); 3774 } 3775 3776 static bool context_needs_register(struct intel_context *ce, bool new_guc_id) 3777 { 3778 return (new_guc_id || test_bit(CONTEXT_LRCA_DIRTY, &ce->flags) || 3779 !ctx_id_mapped(ce_to_guc(ce), ce->guc_id.id)) && 3780 !submission_disabled(ce_to_guc(ce)); 3781 } 3782 3783 static void guc_context_init(struct intel_context *ce) 3784 { 3785 const struct i915_gem_context *ctx; 3786 int prio = I915_CONTEXT_DEFAULT_PRIORITY; 3787 3788 rcu_read_lock(); 3789 ctx = rcu_dereference(ce->gem_context); 3790 if (ctx) 3791 prio = ctx->sched.priority; 3792 rcu_read_unlock(); 3793 3794 ce->guc_state.prio = map_i915_prio_to_guc_prio(prio); 3795 3796 INIT_DELAYED_WORK(&ce->guc_state.sched_disable_delay_work, 3797 __delay_sched_disable); 3798 3799 set_bit(CONTEXT_GUC_INIT, &ce->flags); 3800 } 3801 3802 static int guc_request_alloc(struct i915_request *rq) 3803 { 3804 struct intel_context *ce = request_to_scheduling_context(rq); 3805 struct intel_guc *guc = ce_to_guc(ce); 3806 unsigned long flags; 3807 int ret; 3808 3809 GEM_BUG_ON(!intel_context_is_pinned(rq->context)); 3810 3811 /* 3812 * Flush enough space to reduce the likelihood of waiting after 3813 * we start building the request - in which case we will just 3814 * have to repeat work. 3815 */ 3816 rq->reserved_space += GUC_REQUEST_SIZE; 3817 3818 /* 3819 * Note that after this point, we have committed to using 3820 * this request as it is being used to both track the 3821 * state of engine initialisation and liveness of the 3822 * golden renderstate above. Think twice before you try 3823 * to cancel/unwind this request now. 3824 */ 3825 3826 /* Unconditionally invalidate GPU caches and TLBs. */ 3827 ret = rq->engine->emit_flush(rq, EMIT_INVALIDATE); 3828 if (ret) 3829 return ret; 3830 3831 rq->reserved_space -= GUC_REQUEST_SIZE; 3832 3833 if (unlikely(!test_bit(CONTEXT_GUC_INIT, &ce->flags))) 3834 guc_context_init(ce); 3835 3836 /* 3837 * If the context gets closed while the execbuf is ongoing, the context 3838 * close code will race with the below code to cancel the delayed work. 3839 * If the context close wins the race and cancels the work, it will 3840 * immediately call the sched disable (see guc_context_close), so there 3841 * is a chance we can get past this check while the sched_disable code 3842 * is being executed. To make sure that code completes before we check 3843 * the status further down, we wait for the close process to complete. 3844 * Else, this code path could send a request down thinking that the 3845 * context is still in a schedule-enable mode while the GuC ends up 3846 * dropping the request completely because the disable did go from the 3847 * context_close path right to GuC just prior. In the event the CT is 3848 * full, we could potentially need to wait up to 1.5 seconds. 3849 */ 3850 if (cancel_delayed_work_sync(&ce->guc_state.sched_disable_delay_work)) 3851 intel_context_sched_disable_unpin(ce); 3852 else if (intel_context_is_closed(ce)) 3853 if (wait_for(context_close_done(ce), 1500)) 3854 guc_warn(guc, "timed out waiting on context sched close before realloc\n"); 3855 /* 3856 * Call pin_guc_id here rather than in the pinning step as with 3857 * dma_resv, contexts can be repeatedly pinned / unpinned trashing the 3858 * guc_id and creating horrible race conditions. This is especially bad 3859 * when guc_id are being stolen due to over subscription. By the time 3860 * this function is reached, it is guaranteed that the guc_id will be 3861 * persistent until the generated request is retired. Thus, sealing these 3862 * race conditions. It is still safe to fail here if guc_id are 3863 * exhausted and return -EAGAIN to the user indicating that they can try 3864 * again in the future. 3865 * 3866 * There is no need for a lock here as the timeline mutex ensures at 3867 * most one context can be executing this code path at once. The 3868 * guc_id_ref is incremented once for every request in flight and 3869 * decremented on each retire. When it is zero, a lock around the 3870 * increment (in pin_guc_id) is needed to seal a race with unpin_guc_id. 3871 */ 3872 if (atomic_add_unless(&ce->guc_id.ref, 1, 0)) 3873 goto out; 3874 3875 ret = pin_guc_id(guc, ce); /* returns 1 if new guc_id assigned */ 3876 if (unlikely(ret < 0)) 3877 return ret; 3878 if (context_needs_register(ce, !!ret)) { 3879 ret = try_context_registration(ce, true); 3880 if (unlikely(ret)) { /* unwind */ 3881 if (ret == -EPIPE) { 3882 disable_submission(guc); 3883 goto out; /* GPU will be reset */ 3884 } 3885 atomic_dec(&ce->guc_id.ref); 3886 unpin_guc_id(guc, ce); 3887 return ret; 3888 } 3889 } 3890 3891 clear_bit(CONTEXT_LRCA_DIRTY, &ce->flags); 3892 3893 out: 3894 /* 3895 * We block all requests on this context if a G2H is pending for a 3896 * schedule disable or context deregistration as the GuC will fail a 3897 * schedule enable or context registration if either G2H is pending 3898 * respectfully. Once a G2H returns, the fence is released that is 3899 * blocking these requests (see guc_signal_context_fence). 3900 */ 3901 spin_lock_irqsave(&ce->guc_state.lock, flags); 3902 if (context_wait_for_deregister_to_register(ce) || 3903 context_pending_disable(ce)) { 3904 init_irq_work(&rq->submit_work, submit_work_cb); 3905 i915_sw_fence_await(&rq->submit); 3906 3907 list_add_tail(&rq->guc_fence_link, &ce->guc_state.fences); 3908 } 3909 spin_unlock_irqrestore(&ce->guc_state.lock, flags); 3910 3911 return 0; 3912 } 3913 3914 static int guc_virtual_context_pre_pin(struct intel_context *ce, 3915 struct i915_gem_ww_ctx *ww, 3916 void **vaddr) 3917 { 3918 struct intel_engine_cs *engine = guc_virtual_get_sibling(ce->engine, 0); 3919 3920 return __guc_context_pre_pin(ce, engine, ww, vaddr); 3921 } 3922 3923 static int guc_virtual_context_pin(struct intel_context *ce, void *vaddr) 3924 { 3925 struct intel_engine_cs *engine = guc_virtual_get_sibling(ce->engine, 0); 3926 int ret = __guc_context_pin(ce, engine, vaddr); 3927 intel_engine_mask_t tmp, mask = ce->engine->mask; 3928 3929 if (likely(!ret)) 3930 for_each_engine_masked(engine, ce->engine->gt, mask, tmp) 3931 intel_engine_pm_get(engine); 3932 3933 return ret; 3934 } 3935 3936 static void guc_virtual_context_unpin(struct intel_context *ce) 3937 { 3938 intel_engine_mask_t tmp, mask = ce->engine->mask; 3939 struct intel_engine_cs *engine; 3940 struct intel_guc *guc = ce_to_guc(ce); 3941 3942 GEM_BUG_ON(context_enabled(ce)); 3943 GEM_BUG_ON(intel_context_is_barrier(ce)); 3944 3945 unpin_guc_id(guc, ce); 3946 lrc_unpin(ce); 3947 3948 for_each_engine_masked(engine, ce->engine->gt, mask, tmp) 3949 intel_engine_pm_put_async(engine); 3950 } 3951 3952 static void guc_virtual_context_enter(struct intel_context *ce) 3953 { 3954 intel_engine_mask_t tmp, mask = ce->engine->mask; 3955 struct intel_engine_cs *engine; 3956 3957 for_each_engine_masked(engine, ce->engine->gt, mask, tmp) 3958 intel_engine_pm_get(engine); 3959 3960 intel_timeline_enter(ce->timeline); 3961 } 3962 3963 static void guc_virtual_context_exit(struct intel_context *ce) 3964 { 3965 intel_engine_mask_t tmp, mask = ce->engine->mask; 3966 struct intel_engine_cs *engine; 3967 3968 for_each_engine_masked(engine, ce->engine->gt, mask, tmp) 3969 intel_engine_pm_put(engine); 3970 3971 intel_timeline_exit(ce->timeline); 3972 } 3973 3974 static int guc_virtual_context_alloc(struct intel_context *ce) 3975 { 3976 struct intel_engine_cs *engine = guc_virtual_get_sibling(ce->engine, 0); 3977 3978 return lrc_alloc(ce, engine); 3979 } 3980 3981 static const struct intel_context_ops virtual_guc_context_ops = { 3982 .flags = COPS_RUNTIME_CYCLES, 3983 .alloc = guc_virtual_context_alloc, 3984 3985 .close = guc_context_close, 3986 3987 .pre_pin = guc_virtual_context_pre_pin, 3988 .pin = guc_virtual_context_pin, 3989 .unpin = guc_virtual_context_unpin, 3990 .post_unpin = guc_context_post_unpin, 3991 3992 .revoke = guc_context_revoke, 3993 3994 .cancel_request = guc_context_cancel_request, 3995 3996 .enter = guc_virtual_context_enter, 3997 .exit = guc_virtual_context_exit, 3998 3999 .sched_disable = guc_context_sched_disable, 4000 .update_stats = guc_context_update_stats, 4001 4002 .destroy = guc_context_destroy, 4003 4004 .get_sibling = guc_virtual_get_sibling, 4005 }; 4006 4007 static int guc_parent_context_pin(struct intel_context *ce, void *vaddr) 4008 { 4009 struct intel_engine_cs *engine = guc_virtual_get_sibling(ce->engine, 0); 4010 struct intel_guc *guc = ce_to_guc(ce); 4011 int ret; 4012 4013 GEM_BUG_ON(!intel_context_is_parent(ce)); 4014 GEM_BUG_ON(!intel_engine_is_virtual(ce->engine)); 4015 4016 ret = pin_guc_id(guc, ce); 4017 if (unlikely(ret < 0)) 4018 return ret; 4019 4020 return __guc_context_pin(ce, engine, vaddr); 4021 } 4022 4023 static int guc_child_context_pin(struct intel_context *ce, void *vaddr) 4024 { 4025 struct intel_engine_cs *engine = guc_virtual_get_sibling(ce->engine, 0); 4026 4027 GEM_BUG_ON(!intel_context_is_child(ce)); 4028 GEM_BUG_ON(!intel_engine_is_virtual(ce->engine)); 4029 4030 __intel_context_pin(ce->parallel.parent); 4031 return __guc_context_pin(ce, engine, vaddr); 4032 } 4033 4034 static void guc_parent_context_unpin(struct intel_context *ce) 4035 { 4036 struct intel_guc *guc = ce_to_guc(ce); 4037 4038 GEM_BUG_ON(context_enabled(ce)); 4039 GEM_BUG_ON(intel_context_is_barrier(ce)); 4040 GEM_BUG_ON(!intel_context_is_parent(ce)); 4041 GEM_BUG_ON(!intel_engine_is_virtual(ce->engine)); 4042 4043 unpin_guc_id(guc, ce); 4044 lrc_unpin(ce); 4045 } 4046 4047 static void guc_child_context_unpin(struct intel_context *ce) 4048 { 4049 GEM_BUG_ON(context_enabled(ce)); 4050 GEM_BUG_ON(intel_context_is_barrier(ce)); 4051 GEM_BUG_ON(!intel_context_is_child(ce)); 4052 GEM_BUG_ON(!intel_engine_is_virtual(ce->engine)); 4053 4054 lrc_unpin(ce); 4055 } 4056 4057 static void guc_child_context_post_unpin(struct intel_context *ce) 4058 { 4059 GEM_BUG_ON(!intel_context_is_child(ce)); 4060 GEM_BUG_ON(!intel_context_is_pinned(ce->parallel.parent)); 4061 GEM_BUG_ON(!intel_engine_is_virtual(ce->engine)); 4062 4063 lrc_post_unpin(ce); 4064 intel_context_unpin(ce->parallel.parent); 4065 } 4066 4067 static void guc_child_context_destroy(struct kref *kref) 4068 { 4069 struct intel_context *ce = container_of(kref, typeof(*ce), ref); 4070 4071 __guc_context_destroy(ce); 4072 } 4073 4074 static const struct intel_context_ops virtual_parent_context_ops = { 4075 .alloc = guc_virtual_context_alloc, 4076 4077 .close = guc_context_close, 4078 4079 .pre_pin = guc_context_pre_pin, 4080 .pin = guc_parent_context_pin, 4081 .unpin = guc_parent_context_unpin, 4082 .post_unpin = guc_context_post_unpin, 4083 4084 .revoke = guc_context_revoke, 4085 4086 .cancel_request = guc_context_cancel_request, 4087 4088 .enter = guc_virtual_context_enter, 4089 .exit = guc_virtual_context_exit, 4090 4091 .sched_disable = guc_context_sched_disable, 4092 4093 .destroy = guc_context_destroy, 4094 4095 .get_sibling = guc_virtual_get_sibling, 4096 }; 4097 4098 static const struct intel_context_ops virtual_child_context_ops = { 4099 .alloc = guc_virtual_context_alloc, 4100 4101 .pre_pin = guc_context_pre_pin, 4102 .pin = guc_child_context_pin, 4103 .unpin = guc_child_context_unpin, 4104 .post_unpin = guc_child_context_post_unpin, 4105 4106 .cancel_request = guc_context_cancel_request, 4107 4108 .enter = guc_virtual_context_enter, 4109 .exit = guc_virtual_context_exit, 4110 4111 .destroy = guc_child_context_destroy, 4112 4113 .get_sibling = guc_virtual_get_sibling, 4114 }; 4115 4116 /* 4117 * The below override of the breadcrumbs is enabled when the user configures a 4118 * context for parallel submission (multi-lrc, parent-child). 4119 * 4120 * The overridden breadcrumbs implements an algorithm which allows the GuC to 4121 * safely preempt all the hw contexts configured for parallel submission 4122 * between each BB. The contract between the i915 and GuC is if the parent 4123 * context can be preempted, all the children can be preempted, and the GuC will 4124 * always try to preempt the parent before the children. A handshake between the 4125 * parent / children breadcrumbs ensures the i915 holds up its end of the deal 4126 * creating a window to preempt between each set of BBs. 4127 */ 4128 static int emit_bb_start_parent_no_preempt_mid_batch(struct i915_request *rq, 4129 u64 offset, u32 len, 4130 const unsigned int flags); 4131 static int emit_bb_start_child_no_preempt_mid_batch(struct i915_request *rq, 4132 u64 offset, u32 len, 4133 const unsigned int flags); 4134 static u32 * 4135 emit_fini_breadcrumb_parent_no_preempt_mid_batch(struct i915_request *rq, 4136 u32 *cs); 4137 static u32 * 4138 emit_fini_breadcrumb_child_no_preempt_mid_batch(struct i915_request *rq, 4139 u32 *cs); 4140 4141 static struct intel_context * 4142 guc_create_parallel(struct intel_engine_cs **engines, 4143 unsigned int num_siblings, 4144 unsigned int width) 4145 { 4146 struct intel_engine_cs **siblings = NULL; 4147 struct intel_context *parent = NULL, *ce, *err; 4148 int i, j; 4149 4150 siblings = kmalloc_array(num_siblings, 4151 sizeof(*siblings), 4152 GFP_KERNEL); 4153 if (!siblings) 4154 return ERR_PTR(-ENOMEM); 4155 4156 for (i = 0; i < width; ++i) { 4157 for (j = 0; j < num_siblings; ++j) 4158 siblings[j] = engines[i * num_siblings + j]; 4159 4160 ce = intel_engine_create_virtual(siblings, num_siblings, 4161 FORCE_VIRTUAL); 4162 if (IS_ERR(ce)) { 4163 err = ERR_CAST(ce); 4164 goto unwind; 4165 } 4166 4167 if (i == 0) { 4168 parent = ce; 4169 parent->ops = &virtual_parent_context_ops; 4170 } else { 4171 ce->ops = &virtual_child_context_ops; 4172 intel_context_bind_parent_child(parent, ce); 4173 } 4174 } 4175 4176 parent->parallel.fence_context = dma_fence_context_alloc(1); 4177 4178 parent->engine->emit_bb_start = 4179 emit_bb_start_parent_no_preempt_mid_batch; 4180 parent->engine->emit_fini_breadcrumb = 4181 emit_fini_breadcrumb_parent_no_preempt_mid_batch; 4182 parent->engine->emit_fini_breadcrumb_dw = 4183 12 + 4 * parent->parallel.number_children; 4184 for_each_child(parent, ce) { 4185 ce->engine->emit_bb_start = 4186 emit_bb_start_child_no_preempt_mid_batch; 4187 ce->engine->emit_fini_breadcrumb = 4188 emit_fini_breadcrumb_child_no_preempt_mid_batch; 4189 ce->engine->emit_fini_breadcrumb_dw = 16; 4190 } 4191 4192 kfree(siblings); 4193 return parent; 4194 4195 unwind: 4196 if (parent) 4197 intel_context_put(parent); 4198 kfree(siblings); 4199 return err; 4200 } 4201 4202 static bool 4203 guc_irq_enable_breadcrumbs(struct intel_breadcrumbs *b) 4204 { 4205 struct intel_engine_cs *sibling; 4206 intel_engine_mask_t tmp, mask = b->engine_mask; 4207 bool result = false; 4208 4209 for_each_engine_masked(sibling, b->irq_engine->gt, mask, tmp) 4210 result |= intel_engine_irq_enable(sibling); 4211 4212 return result; 4213 } 4214 4215 static void 4216 guc_irq_disable_breadcrumbs(struct intel_breadcrumbs *b) 4217 { 4218 struct intel_engine_cs *sibling; 4219 intel_engine_mask_t tmp, mask = b->engine_mask; 4220 4221 for_each_engine_masked(sibling, b->irq_engine->gt, mask, tmp) 4222 intel_engine_irq_disable(sibling); 4223 } 4224 4225 static void guc_init_breadcrumbs(struct intel_engine_cs *engine) 4226 { 4227 int i; 4228 4229 /* 4230 * In GuC submission mode we do not know which physical engine a request 4231 * will be scheduled on, this creates a problem because the breadcrumb 4232 * interrupt is per physical engine. To work around this we attach 4233 * requests and direct all breadcrumb interrupts to the first instance 4234 * of an engine per class. In addition all breadcrumb interrupts are 4235 * enabled / disabled across an engine class in unison. 4236 */ 4237 for (i = 0; i < MAX_ENGINE_INSTANCE; ++i) { 4238 struct intel_engine_cs *sibling = 4239 engine->gt->engine_class[engine->class][i]; 4240 4241 if (sibling) { 4242 if (engine->breadcrumbs != sibling->breadcrumbs) { 4243 intel_breadcrumbs_put(engine->breadcrumbs); 4244 engine->breadcrumbs = 4245 intel_breadcrumbs_get(sibling->breadcrumbs); 4246 } 4247 break; 4248 } 4249 } 4250 4251 if (engine->breadcrumbs) { 4252 engine->breadcrumbs->engine_mask |= engine->mask; 4253 engine->breadcrumbs->irq_enable = guc_irq_enable_breadcrumbs; 4254 engine->breadcrumbs->irq_disable = guc_irq_disable_breadcrumbs; 4255 } 4256 } 4257 4258 static void guc_bump_inflight_request_prio(struct i915_request *rq, 4259 int prio) 4260 { 4261 struct intel_context *ce = request_to_scheduling_context(rq); 4262 u8 new_guc_prio = map_i915_prio_to_guc_prio(prio); 4263 4264 /* Short circuit function */ 4265 if (prio < I915_PRIORITY_NORMAL || 4266 rq->guc_prio == GUC_PRIO_FINI || 4267 (rq->guc_prio != GUC_PRIO_INIT && 4268 !new_guc_prio_higher(rq->guc_prio, new_guc_prio))) 4269 return; 4270 4271 spin_lock(&ce->guc_state.lock); 4272 if (rq->guc_prio != GUC_PRIO_FINI) { 4273 if (rq->guc_prio != GUC_PRIO_INIT) 4274 sub_context_inflight_prio(ce, rq->guc_prio); 4275 rq->guc_prio = new_guc_prio; 4276 add_context_inflight_prio(ce, rq->guc_prio); 4277 update_context_prio(ce); 4278 } 4279 spin_unlock(&ce->guc_state.lock); 4280 } 4281 4282 static void guc_retire_inflight_request_prio(struct i915_request *rq) 4283 { 4284 struct intel_context *ce = request_to_scheduling_context(rq); 4285 4286 spin_lock(&ce->guc_state.lock); 4287 guc_prio_fini(rq, ce); 4288 spin_unlock(&ce->guc_state.lock); 4289 } 4290 4291 static void sanitize_hwsp(struct intel_engine_cs *engine) 4292 { 4293 struct intel_timeline *tl; 4294 4295 list_for_each_entry(tl, &engine->status_page.timelines, engine_link) 4296 intel_timeline_reset_seqno(tl); 4297 } 4298 4299 static void guc_sanitize(struct intel_engine_cs *engine) 4300 { 4301 /* 4302 * Poison residual state on resume, in case the suspend didn't! 4303 * 4304 * We have to assume that across suspend/resume (or other loss 4305 * of control) that the contents of our pinned buffers has been 4306 * lost, replaced by garbage. Since this doesn't always happen, 4307 * let's poison such state so that we more quickly spot when 4308 * we falsely assume it has been preserved. 4309 */ 4310 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) 4311 memset(engine->status_page.addr, POISON_INUSE, PAGE_SIZE); 4312 4313 /* 4314 * The kernel_context HWSP is stored in the status_page. As above, 4315 * that may be lost on resume/initialisation, and so we need to 4316 * reset the value in the HWSP. 4317 */ 4318 sanitize_hwsp(engine); 4319 4320 /* And scrub the dirty cachelines for the HWSP */ 4321 drm_clflush_virt_range(engine->status_page.addr, PAGE_SIZE); 4322 4323 intel_engine_reset_pinned_contexts(engine); 4324 } 4325 4326 static void setup_hwsp(struct intel_engine_cs *engine) 4327 { 4328 intel_engine_set_hwsp_writemask(engine, ~0u); /* HWSTAM */ 4329 4330 ENGINE_WRITE_FW(engine, 4331 RING_HWS_PGA, 4332 i915_ggtt_offset(engine->status_page.vma)); 4333 } 4334 4335 static void start_engine(struct intel_engine_cs *engine) 4336 { 4337 ENGINE_WRITE_FW(engine, 4338 RING_MODE_GEN7, 4339 _MASKED_BIT_ENABLE(GEN11_GFX_DISABLE_LEGACY_MODE)); 4340 4341 ENGINE_WRITE_FW(engine, RING_MI_MODE, _MASKED_BIT_DISABLE(STOP_RING)); 4342 ENGINE_POSTING_READ(engine, RING_MI_MODE); 4343 } 4344 4345 static int guc_resume(struct intel_engine_cs *engine) 4346 { 4347 assert_forcewakes_active(engine->uncore, FORCEWAKE_ALL); 4348 4349 intel_mocs_init_engine(engine); 4350 4351 intel_breadcrumbs_reset(engine->breadcrumbs); 4352 4353 setup_hwsp(engine); 4354 start_engine(engine); 4355 4356 if (engine->flags & I915_ENGINE_FIRST_RENDER_COMPUTE) 4357 xehp_enable_ccs_engines(engine); 4358 4359 return 0; 4360 } 4361 4362 static bool guc_sched_engine_disabled(struct i915_sched_engine *sched_engine) 4363 { 4364 return !sched_engine->tasklet.callback; 4365 } 4366 4367 static void guc_set_default_submission(struct intel_engine_cs *engine) 4368 { 4369 engine->submit_request = guc_submit_request; 4370 } 4371 4372 static inline int guc_kernel_context_pin(struct intel_guc *guc, 4373 struct intel_context *ce) 4374 { 4375 int ret; 4376 4377 /* 4378 * Note: we purposefully do not check the returns below because 4379 * the registration can only fail if a reset is just starting. 4380 * This is called at the end of reset so presumably another reset 4381 * isn't happening and even it did this code would be run again. 4382 */ 4383 4384 if (context_guc_id_invalid(ce)) { 4385 ret = pin_guc_id(guc, ce); 4386 4387 if (ret < 0) 4388 return ret; 4389 } 4390 4391 if (!test_bit(CONTEXT_GUC_INIT, &ce->flags)) 4392 guc_context_init(ce); 4393 4394 ret = try_context_registration(ce, true); 4395 if (ret) 4396 unpin_guc_id(guc, ce); 4397 4398 return ret; 4399 } 4400 4401 static inline int guc_init_submission(struct intel_guc *guc) 4402 { 4403 struct intel_gt *gt = guc_to_gt(guc); 4404 struct intel_engine_cs *engine; 4405 enum intel_engine_id id; 4406 4407 /* make sure all descriptors are clean... */ 4408 xa_destroy(&guc->context_lookup); 4409 4410 /* 4411 * A reset might have occurred while we had a pending stalled request, 4412 * so make sure we clean that up. 4413 */ 4414 guc->stalled_request = NULL; 4415 guc->submission_stall_reason = STALL_NONE; 4416 4417 /* 4418 * Some contexts might have been pinned before we enabled GuC 4419 * submission, so we need to add them to the GuC bookeeping. 4420 * Also, after a reset the of the GuC we want to make sure that the 4421 * information shared with GuC is properly reset. The kernel LRCs are 4422 * not attached to the gem_context, so they need to be added separately. 4423 */ 4424 for_each_engine(engine, gt, id) { 4425 struct intel_context *ce; 4426 4427 list_for_each_entry(ce, &engine->pinned_contexts_list, 4428 pinned_contexts_link) { 4429 int ret = guc_kernel_context_pin(guc, ce); 4430 4431 if (ret) { 4432 /* No point in trying to clean up as i915 will wedge on failure */ 4433 return ret; 4434 } 4435 } 4436 } 4437 4438 return 0; 4439 } 4440 4441 static void guc_release(struct intel_engine_cs *engine) 4442 { 4443 engine->sanitize = NULL; /* no longer in control, nothing to sanitize */ 4444 4445 intel_engine_cleanup_common(engine); 4446 lrc_fini_wa_ctx(engine); 4447 } 4448 4449 static void virtual_guc_bump_serial(struct intel_engine_cs *engine) 4450 { 4451 struct intel_engine_cs *e; 4452 intel_engine_mask_t tmp, mask = engine->mask; 4453 4454 for_each_engine_masked(e, engine->gt, mask, tmp) 4455 e->serial++; 4456 } 4457 4458 static void guc_default_vfuncs(struct intel_engine_cs *engine) 4459 { 4460 /* Default vfuncs which can be overridden by each engine. */ 4461 4462 engine->resume = guc_resume; 4463 4464 engine->cops = &guc_context_ops; 4465 engine->request_alloc = guc_request_alloc; 4466 engine->add_active_request = add_to_context; 4467 engine->remove_active_request = remove_from_context; 4468 4469 engine->sched_engine->schedule = i915_schedule; 4470 4471 engine->reset.prepare = guc_engine_reset_prepare; 4472 engine->reset.rewind = guc_rewind_nop; 4473 engine->reset.cancel = guc_reset_nop; 4474 engine->reset.finish = guc_reset_nop; 4475 4476 engine->emit_flush = gen8_emit_flush_xcs; 4477 engine->emit_init_breadcrumb = gen8_emit_init_breadcrumb; 4478 engine->emit_fini_breadcrumb = gen8_emit_fini_breadcrumb_xcs; 4479 if (GRAPHICS_VER(engine->i915) >= 12) { 4480 engine->emit_fini_breadcrumb = gen12_emit_fini_breadcrumb_xcs; 4481 engine->emit_flush = gen12_emit_flush_xcs; 4482 } 4483 engine->set_default_submission = guc_set_default_submission; 4484 engine->busyness = guc_engine_busyness; 4485 4486 engine->flags |= I915_ENGINE_SUPPORTS_STATS; 4487 engine->flags |= I915_ENGINE_HAS_PREEMPTION; 4488 engine->flags |= I915_ENGINE_HAS_TIMESLICES; 4489 4490 /* Wa_14014475959:dg2 */ 4491 if (engine->class == COMPUTE_CLASS) 4492 if (IS_GFX_GT_IP_STEP(engine->gt, IP_VER(12, 70), STEP_A0, STEP_B0) || 4493 IS_DG2(engine->i915)) 4494 engine->flags |= I915_ENGINE_USES_WA_HOLD_CCS_SWITCHOUT; 4495 4496 /* 4497 * TODO: GuC supports timeslicing and semaphores as well, but they're 4498 * handled by the firmware so some minor tweaks are required before 4499 * enabling. 4500 * 4501 * engine->flags |= I915_ENGINE_HAS_SEMAPHORES; 4502 */ 4503 4504 engine->emit_bb_start = gen8_emit_bb_start; 4505 if (GRAPHICS_VER_FULL(engine->i915) >= IP_VER(12, 50)) 4506 engine->emit_bb_start = xehp_emit_bb_start; 4507 } 4508 4509 static void rcs_submission_override(struct intel_engine_cs *engine) 4510 { 4511 switch (GRAPHICS_VER(engine->i915)) { 4512 case 12: 4513 engine->emit_flush = gen12_emit_flush_rcs; 4514 engine->emit_fini_breadcrumb = gen12_emit_fini_breadcrumb_rcs; 4515 break; 4516 case 11: 4517 engine->emit_flush = gen11_emit_flush_rcs; 4518 engine->emit_fini_breadcrumb = gen11_emit_fini_breadcrumb_rcs; 4519 break; 4520 default: 4521 engine->emit_flush = gen8_emit_flush_rcs; 4522 engine->emit_fini_breadcrumb = gen8_emit_fini_breadcrumb_rcs; 4523 break; 4524 } 4525 } 4526 4527 static inline void guc_default_irqs(struct intel_engine_cs *engine) 4528 { 4529 engine->irq_keep_mask = GT_RENDER_USER_INTERRUPT; 4530 intel_engine_set_irq_handler(engine, cs_irq_handler); 4531 } 4532 4533 static void guc_sched_engine_destroy(struct kref *kref) 4534 { 4535 struct i915_sched_engine *sched_engine = 4536 container_of(kref, typeof(*sched_engine), ref); 4537 struct intel_guc *guc = sched_engine->private_data; 4538 4539 guc->sched_engine = NULL; 4540 tasklet_kill(&sched_engine->tasklet); /* flush the callback */ 4541 kfree(sched_engine); 4542 } 4543 4544 int intel_guc_submission_setup(struct intel_engine_cs *engine) 4545 { 4546 struct drm_i915_private *i915 = engine->i915; 4547 struct intel_guc *guc = &engine->gt->uc.guc; 4548 4549 /* 4550 * The setup relies on several assumptions (e.g. irqs always enabled) 4551 * that are only valid on gen11+ 4552 */ 4553 GEM_BUG_ON(GRAPHICS_VER(i915) < 11); 4554 4555 if (!guc->sched_engine) { 4556 guc->sched_engine = i915_sched_engine_create(ENGINE_VIRTUAL); 4557 if (!guc->sched_engine) 4558 return -ENOMEM; 4559 4560 guc->sched_engine->schedule = i915_schedule; 4561 guc->sched_engine->disabled = guc_sched_engine_disabled; 4562 guc->sched_engine->private_data = guc; 4563 guc->sched_engine->destroy = guc_sched_engine_destroy; 4564 guc->sched_engine->bump_inflight_request_prio = 4565 guc_bump_inflight_request_prio; 4566 guc->sched_engine->retire_inflight_request_prio = 4567 guc_retire_inflight_request_prio; 4568 tasklet_setup(&guc->sched_engine->tasklet, 4569 guc_submission_tasklet); 4570 } 4571 i915_sched_engine_put(engine->sched_engine); 4572 engine->sched_engine = i915_sched_engine_get(guc->sched_engine); 4573 4574 guc_default_vfuncs(engine); 4575 guc_default_irqs(engine); 4576 guc_init_breadcrumbs(engine); 4577 4578 if (engine->flags & I915_ENGINE_HAS_RCS_REG_STATE) 4579 rcs_submission_override(engine); 4580 4581 lrc_init_wa_ctx(engine); 4582 4583 /* Finally, take ownership and responsibility for cleanup! */ 4584 engine->sanitize = guc_sanitize; 4585 engine->release = guc_release; 4586 4587 return 0; 4588 } 4589 4590 struct scheduling_policy { 4591 /* internal data */ 4592 u32 max_words, num_words; 4593 u32 count; 4594 /* API data */ 4595 struct guc_update_scheduling_policy h2g; 4596 }; 4597 4598 static u32 __guc_scheduling_policy_action_size(struct scheduling_policy *policy) 4599 { 4600 u32 *start = (void *)&policy->h2g; 4601 u32 *end = policy->h2g.data + policy->num_words; 4602 size_t delta = end - start; 4603 4604 return delta; 4605 } 4606 4607 static struct scheduling_policy *__guc_scheduling_policy_start_klv(struct scheduling_policy *policy) 4608 { 4609 policy->h2g.header.action = INTEL_GUC_ACTION_UPDATE_SCHEDULING_POLICIES_KLV; 4610 policy->max_words = ARRAY_SIZE(policy->h2g.data); 4611 policy->num_words = 0; 4612 policy->count = 0; 4613 4614 return policy; 4615 } 4616 4617 static void __guc_scheduling_policy_add_klv(struct scheduling_policy *policy, 4618 u32 action, u32 *data, u32 len) 4619 { 4620 u32 *klv_ptr = policy->h2g.data + policy->num_words; 4621 4622 GEM_BUG_ON((policy->num_words + 1 + len) > policy->max_words); 4623 *(klv_ptr++) = FIELD_PREP(GUC_KLV_0_KEY, action) | 4624 FIELD_PREP(GUC_KLV_0_LEN, len); 4625 memcpy(klv_ptr, data, sizeof(u32) * len); 4626 policy->num_words += 1 + len; 4627 policy->count++; 4628 } 4629 4630 static int __guc_action_set_scheduling_policies(struct intel_guc *guc, 4631 struct scheduling_policy *policy) 4632 { 4633 int ret; 4634 4635 ret = intel_guc_send(guc, (u32 *)&policy->h2g, 4636 __guc_scheduling_policy_action_size(policy)); 4637 if (ret < 0) { 4638 guc_probe_error(guc, "Failed to configure global scheduling policies: %pe!\n", 4639 ERR_PTR(ret)); 4640 return ret; 4641 } 4642 4643 if (ret != policy->count) { 4644 guc_warn(guc, "global scheduler policy processed %d of %d KLVs!", 4645 ret, policy->count); 4646 if (ret > policy->count) 4647 return -EPROTO; 4648 } 4649 4650 return 0; 4651 } 4652 4653 static int guc_init_global_schedule_policy(struct intel_guc *guc) 4654 { 4655 struct scheduling_policy policy; 4656 struct intel_gt *gt = guc_to_gt(guc); 4657 intel_wakeref_t wakeref; 4658 int ret; 4659 4660 if (GUC_SUBMIT_VER(guc) < MAKE_GUC_VER(1, 1, 0)) 4661 return 0; 4662 4663 __guc_scheduling_policy_start_klv(&policy); 4664 4665 with_intel_runtime_pm(>->i915->runtime_pm, wakeref) { 4666 u32 yield[] = { 4667 GLOBAL_SCHEDULE_POLICY_RC_YIELD_DURATION, 4668 GLOBAL_SCHEDULE_POLICY_RC_YIELD_RATIO, 4669 }; 4670 4671 __guc_scheduling_policy_add_klv(&policy, 4672 GUC_SCHEDULING_POLICIES_KLV_ID_RENDER_COMPUTE_YIELD, 4673 yield, ARRAY_SIZE(yield)); 4674 4675 ret = __guc_action_set_scheduling_policies(guc, &policy); 4676 } 4677 4678 return ret; 4679 } 4680 4681 static void guc_route_semaphores(struct intel_guc *guc, bool to_guc) 4682 { 4683 struct intel_gt *gt = guc_to_gt(guc); 4684 u32 val; 4685 4686 if (GRAPHICS_VER(gt->i915) < 12) 4687 return; 4688 4689 if (to_guc) 4690 val = GUC_SEM_INTR_ROUTE_TO_GUC | GUC_SEM_INTR_ENABLE_ALL; 4691 else 4692 val = 0; 4693 4694 intel_uncore_write(gt->uncore, GEN12_GUC_SEM_INTR_ENABLES, val); 4695 } 4696 4697 int intel_guc_submission_enable(struct intel_guc *guc) 4698 { 4699 int ret; 4700 4701 /* Semaphore interrupt enable and route to GuC */ 4702 guc_route_semaphores(guc, true); 4703 4704 ret = guc_init_submission(guc); 4705 if (ret) 4706 goto fail_sem; 4707 4708 ret = guc_init_engine_stats(guc); 4709 if (ret) 4710 goto fail_sem; 4711 4712 ret = guc_init_global_schedule_policy(guc); 4713 if (ret) 4714 goto fail_stats; 4715 4716 return 0; 4717 4718 fail_stats: 4719 guc_fini_engine_stats(guc); 4720 fail_sem: 4721 guc_route_semaphores(guc, false); 4722 return ret; 4723 } 4724 4725 /* Note: By the time we're here, GuC may have already been reset */ 4726 void intel_guc_submission_disable(struct intel_guc *guc) 4727 { 4728 guc_cancel_busyness_worker(guc); 4729 4730 /* Semaphore interrupt disable and route to host */ 4731 guc_route_semaphores(guc, false); 4732 } 4733 4734 static bool __guc_submission_supported(struct intel_guc *guc) 4735 { 4736 /* GuC submission is unavailable for pre-Gen11 */ 4737 return intel_guc_is_supported(guc) && 4738 GRAPHICS_VER(guc_to_i915(guc)) >= 11; 4739 } 4740 4741 static bool __guc_submission_selected(struct intel_guc *guc) 4742 { 4743 struct drm_i915_private *i915 = guc_to_i915(guc); 4744 4745 if (!intel_guc_submission_is_supported(guc)) 4746 return false; 4747 4748 return i915->params.enable_guc & ENABLE_GUC_SUBMISSION; 4749 } 4750 4751 int intel_guc_sched_disable_gucid_threshold_max(struct intel_guc *guc) 4752 { 4753 return guc->submission_state.num_guc_ids - NUMBER_MULTI_LRC_GUC_ID(guc); 4754 } 4755 4756 /* 4757 * This default value of 33 milisecs (+1 milisec round up) ensures 30fps or higher 4758 * workloads are able to enjoy the latency reduction when delaying the schedule-disable 4759 * operation. This matches the 30fps game-render + encode (real world) workload this 4760 * knob was tested against. 4761 */ 4762 #define SCHED_DISABLE_DELAY_MS 34 4763 4764 /* 4765 * A threshold of 75% is a reasonable starting point considering that real world apps 4766 * generally don't get anywhere near this. 4767 */ 4768 #define NUM_SCHED_DISABLE_GUCIDS_DEFAULT_THRESHOLD(__guc) \ 4769 (((intel_guc_sched_disable_gucid_threshold_max(guc)) * 3) / 4) 4770 4771 void intel_guc_submission_init_early(struct intel_guc *guc) 4772 { 4773 xa_init_flags(&guc->context_lookup, XA_FLAGS_LOCK_IRQ); 4774 4775 spin_lock_init(&guc->submission_state.lock); 4776 INIT_LIST_HEAD(&guc->submission_state.guc_id_list); 4777 ida_init(&guc->submission_state.guc_ids); 4778 INIT_LIST_HEAD(&guc->submission_state.destroyed_contexts); 4779 INIT_WORK(&guc->submission_state.destroyed_worker, 4780 destroyed_worker_func); 4781 INIT_WORK(&guc->submission_state.reset_fail_worker, 4782 reset_fail_worker_func); 4783 4784 spin_lock_init(&guc->timestamp.lock); 4785 INIT_DELAYED_WORK(&guc->timestamp.work, guc_timestamp_ping); 4786 4787 guc->submission_state.sched_disable_delay_ms = SCHED_DISABLE_DELAY_MS; 4788 guc->submission_state.num_guc_ids = GUC_MAX_CONTEXT_ID; 4789 guc->submission_state.sched_disable_gucid_threshold = 4790 NUM_SCHED_DISABLE_GUCIDS_DEFAULT_THRESHOLD(guc); 4791 guc->submission_supported = __guc_submission_supported(guc); 4792 guc->submission_selected = __guc_submission_selected(guc); 4793 } 4794 4795 static inline struct intel_context * 4796 g2h_context_lookup(struct intel_guc *guc, u32 ctx_id) 4797 { 4798 struct intel_context *ce; 4799 4800 if (unlikely(ctx_id >= GUC_MAX_CONTEXT_ID)) { 4801 guc_err(guc, "Invalid ctx_id %u\n", ctx_id); 4802 return NULL; 4803 } 4804 4805 ce = __get_context(guc, ctx_id); 4806 if (unlikely(!ce)) { 4807 guc_err(guc, "Context is NULL, ctx_id %u\n", ctx_id); 4808 return NULL; 4809 } 4810 4811 if (unlikely(intel_context_is_child(ce))) { 4812 guc_err(guc, "Context is child, ctx_id %u\n", ctx_id); 4813 return NULL; 4814 } 4815 4816 return ce; 4817 } 4818 4819 static void wait_wake_outstanding_tlb_g2h(struct intel_guc *guc, u32 seqno) 4820 { 4821 struct intel_guc_tlb_wait *wait; 4822 unsigned long flags; 4823 4824 xa_lock_irqsave(&guc->tlb_lookup, flags); 4825 wait = xa_load(&guc->tlb_lookup, seqno); 4826 4827 if (wait) 4828 wake_up(&wait->wq); 4829 else 4830 guc_dbg(guc, 4831 "Stale TLB invalidation response with seqno %d\n", seqno); 4832 4833 xa_unlock_irqrestore(&guc->tlb_lookup, flags); 4834 } 4835 4836 int intel_guc_tlb_invalidation_done(struct intel_guc *guc, 4837 const u32 *payload, u32 len) 4838 { 4839 if (len < 1) 4840 return -EPROTO; 4841 4842 wait_wake_outstanding_tlb_g2h(guc, payload[0]); 4843 return 0; 4844 } 4845 4846 static long must_wait_woken(struct wait_queue_entry *wq_entry, long timeout) 4847 { 4848 /* 4849 * This is equivalent to wait_woken() with the exception that 4850 * we do not wake up early if the kthread task has been completed. 4851 * As we are called from page reclaim in any task context, 4852 * we may be invoked from stopped kthreads, but we *must* 4853 * complete the wait from the HW. 4854 */ 4855 do { 4856 set_current_state(TASK_UNINTERRUPTIBLE); 4857 if (wq_entry->flags & WQ_FLAG_WOKEN) 4858 break; 4859 4860 timeout = schedule_timeout(timeout); 4861 } while (timeout); 4862 4863 /* See wait_woken() and woken_wake_function() */ 4864 __set_current_state(TASK_RUNNING); 4865 smp_store_mb(wq_entry->flags, wq_entry->flags & ~WQ_FLAG_WOKEN); 4866 4867 return timeout; 4868 } 4869 4870 static bool intel_gt_is_enabled(const struct intel_gt *gt) 4871 { 4872 /* Check if GT is wedged or suspended */ 4873 if (intel_gt_is_wedged(gt) || !intel_irqs_enabled(gt->i915)) 4874 return false; 4875 return true; 4876 } 4877 4878 static int guc_send_invalidate_tlb(struct intel_guc *guc, 4879 enum intel_guc_tlb_invalidation_type type) 4880 { 4881 struct intel_guc_tlb_wait _wq, *wq = &_wq; 4882 struct intel_gt *gt = guc_to_gt(guc); 4883 DEFINE_WAIT_FUNC(wait, woken_wake_function); 4884 int err; 4885 u32 seqno; 4886 u32 action[] = { 4887 INTEL_GUC_ACTION_TLB_INVALIDATION, 4888 0, 4889 REG_FIELD_PREP(INTEL_GUC_TLB_INVAL_TYPE_MASK, type) | 4890 REG_FIELD_PREP(INTEL_GUC_TLB_INVAL_MODE_MASK, 4891 INTEL_GUC_TLB_INVAL_MODE_HEAVY) | 4892 INTEL_GUC_TLB_INVAL_FLUSH_CACHE, 4893 }; 4894 u32 size = ARRAY_SIZE(action); 4895 4896 /* 4897 * Early guard against GT enablement. TLB invalidation should not be 4898 * attempted if the GT is disabled due to suspend/wedge. 4899 */ 4900 if (!intel_gt_is_enabled(gt)) 4901 return -EINVAL; 4902 4903 init_waitqueue_head(&_wq.wq); 4904 4905 if (xa_alloc_cyclic_irq(&guc->tlb_lookup, &seqno, wq, 4906 xa_limit_32b, &guc->next_seqno, 4907 GFP_ATOMIC | __GFP_NOWARN) < 0) { 4908 /* Under severe memory pressure? Serialise TLB allocations */ 4909 xa_lock_irq(&guc->tlb_lookup); 4910 wq = xa_load(&guc->tlb_lookup, guc->serial_slot); 4911 wait_event_lock_irq(wq->wq, 4912 !READ_ONCE(wq->busy), 4913 guc->tlb_lookup.xa_lock); 4914 /* 4915 * Update wq->busy under lock to ensure only one waiter can 4916 * issue the TLB invalidation command using the serial slot at a 4917 * time. The condition is set to true before releasing the lock 4918 * so that other caller continue to wait until woken up again. 4919 */ 4920 wq->busy = true; 4921 xa_unlock_irq(&guc->tlb_lookup); 4922 4923 seqno = guc->serial_slot; 4924 } 4925 4926 action[1] = seqno; 4927 4928 add_wait_queue(&wq->wq, &wait); 4929 4930 /* This is a critical reclaim path and thus we must loop here. */ 4931 err = intel_guc_send_busy_loop(guc, action, size, G2H_LEN_DW_INVALIDATE_TLB, true); 4932 if (err) 4933 goto out; 4934 4935 /* 4936 * Late guard against GT enablement. It is not an error for the TLB 4937 * invalidation to time out if the GT is disabled during the process 4938 * due to suspend/wedge. In fact, the TLB invalidation is cancelled 4939 * in this case. 4940 */ 4941 if (!must_wait_woken(&wait, intel_guc_ct_max_queue_time_jiffies()) && 4942 intel_gt_is_enabled(gt)) { 4943 guc_err(guc, 4944 "TLB invalidation response timed out for seqno %u\n", seqno); 4945 err = -ETIME; 4946 } 4947 out: 4948 remove_wait_queue(&wq->wq, &wait); 4949 if (seqno != guc->serial_slot) 4950 xa_erase_irq(&guc->tlb_lookup, seqno); 4951 4952 return err; 4953 } 4954 4955 /* Send a H2G command to invalidate the TLBs at engine level and beyond. */ 4956 int intel_guc_invalidate_tlb_engines(struct intel_guc *guc) 4957 { 4958 return guc_send_invalidate_tlb(guc, INTEL_GUC_TLB_INVAL_ENGINES); 4959 } 4960 4961 /* Send a H2G command to invalidate the GuC's internal TLB. */ 4962 int intel_guc_invalidate_tlb_guc(struct intel_guc *guc) 4963 { 4964 return guc_send_invalidate_tlb(guc, INTEL_GUC_TLB_INVAL_GUC); 4965 } 4966 4967 int intel_guc_deregister_done_process_msg(struct intel_guc *guc, 4968 const u32 *msg, 4969 u32 len) 4970 { 4971 struct intel_context *ce; 4972 u32 ctx_id; 4973 4974 if (unlikely(len < 1)) { 4975 guc_err(guc, "Invalid length %u\n", len); 4976 return -EPROTO; 4977 } 4978 ctx_id = msg[0]; 4979 4980 ce = g2h_context_lookup(guc, ctx_id); 4981 if (unlikely(!ce)) 4982 return -EPROTO; 4983 4984 trace_intel_context_deregister_done(ce); 4985 4986 #ifdef CONFIG_DRM_I915_SELFTEST 4987 if (unlikely(ce->drop_deregister)) { 4988 ce->drop_deregister = false; 4989 return 0; 4990 } 4991 #endif 4992 4993 if (context_wait_for_deregister_to_register(ce)) { 4994 struct intel_runtime_pm *runtime_pm = 4995 &ce->engine->gt->i915->runtime_pm; 4996 intel_wakeref_t wakeref; 4997 4998 /* 4999 * Previous owner of this guc_id has been deregistered, now safe 5000 * register this context. 5001 */ 5002 with_intel_runtime_pm(runtime_pm, wakeref) 5003 register_context(ce, true); 5004 guc_signal_context_fence(ce); 5005 intel_context_put(ce); 5006 } else if (context_destroyed(ce)) { 5007 /* Context has been destroyed */ 5008 intel_gt_pm_put_async_untracked(guc_to_gt(guc)); 5009 release_guc_id(guc, ce); 5010 __guc_context_destroy(ce); 5011 } 5012 5013 decr_outstanding_submission_g2h(guc); 5014 5015 return 0; 5016 } 5017 5018 int intel_guc_sched_done_process_msg(struct intel_guc *guc, 5019 const u32 *msg, 5020 u32 len) 5021 { 5022 struct intel_context *ce; 5023 unsigned long flags; 5024 u32 ctx_id; 5025 5026 if (unlikely(len < 2)) { 5027 guc_err(guc, "Invalid length %u\n", len); 5028 return -EPROTO; 5029 } 5030 ctx_id = msg[0]; 5031 5032 ce = g2h_context_lookup(guc, ctx_id); 5033 if (unlikely(!ce)) 5034 return -EPROTO; 5035 5036 if (unlikely(context_destroyed(ce) || 5037 (!context_pending_enable(ce) && 5038 !context_pending_disable(ce)))) { 5039 guc_err(guc, "Bad context sched_state 0x%x, ctx_id %u\n", 5040 ce->guc_state.sched_state, ctx_id); 5041 return -EPROTO; 5042 } 5043 5044 trace_intel_context_sched_done(ce); 5045 5046 if (context_pending_enable(ce)) { 5047 #ifdef CONFIG_DRM_I915_SELFTEST 5048 if (unlikely(ce->drop_schedule_enable)) { 5049 ce->drop_schedule_enable = false; 5050 return 0; 5051 } 5052 #endif 5053 5054 spin_lock_irqsave(&ce->guc_state.lock, flags); 5055 clr_context_pending_enable(ce); 5056 spin_unlock_irqrestore(&ce->guc_state.lock, flags); 5057 } else if (context_pending_disable(ce)) { 5058 bool banned; 5059 5060 #ifdef CONFIG_DRM_I915_SELFTEST 5061 if (unlikely(ce->drop_schedule_disable)) { 5062 ce->drop_schedule_disable = false; 5063 return 0; 5064 } 5065 #endif 5066 5067 /* 5068 * Unpin must be done before __guc_signal_context_fence, 5069 * otherwise a race exists between the requests getting 5070 * submitted + retired before this unpin completes resulting in 5071 * the pin_count going to zero and the context still being 5072 * enabled. 5073 */ 5074 intel_context_sched_disable_unpin(ce); 5075 5076 spin_lock_irqsave(&ce->guc_state.lock, flags); 5077 banned = context_banned(ce); 5078 clr_context_banned(ce); 5079 clr_context_pending_disable(ce); 5080 __guc_signal_context_fence(ce); 5081 guc_blocked_fence_complete(ce); 5082 spin_unlock_irqrestore(&ce->guc_state.lock, flags); 5083 5084 if (banned) { 5085 guc_cancel_context_requests(ce); 5086 intel_engine_signal_breadcrumbs(ce->engine); 5087 } 5088 } 5089 5090 decr_outstanding_submission_g2h(guc); 5091 intel_context_put(ce); 5092 5093 return 0; 5094 } 5095 5096 static void capture_error_state(struct intel_guc *guc, 5097 struct intel_context *ce) 5098 { 5099 struct intel_gt *gt = guc_to_gt(guc); 5100 struct drm_i915_private *i915 = gt->i915; 5101 intel_wakeref_t wakeref; 5102 intel_engine_mask_t engine_mask; 5103 5104 if (intel_engine_is_virtual(ce->engine)) { 5105 struct intel_engine_cs *e; 5106 intel_engine_mask_t tmp, virtual_mask = ce->engine->mask; 5107 5108 engine_mask = 0; 5109 for_each_engine_masked(e, ce->engine->gt, virtual_mask, tmp) { 5110 bool match = intel_guc_capture_is_matching_engine(gt, ce, e); 5111 5112 if (match) { 5113 intel_engine_set_hung_context(e, ce); 5114 engine_mask |= e->mask; 5115 i915_increase_reset_engine_count(&i915->gpu_error, 5116 e); 5117 } 5118 } 5119 5120 if (!engine_mask) { 5121 guc_warn(guc, "No matching physical engine capture for virtual engine context 0x%04X / %s", 5122 ce->guc_id.id, ce->engine->name); 5123 engine_mask = ~0U; 5124 } 5125 } else { 5126 intel_engine_set_hung_context(ce->engine, ce); 5127 engine_mask = ce->engine->mask; 5128 i915_increase_reset_engine_count(&i915->gpu_error, ce->engine); 5129 } 5130 5131 with_intel_runtime_pm(&i915->runtime_pm, wakeref) 5132 i915_capture_error_state(gt, engine_mask, CORE_DUMP_FLAG_IS_GUC_CAPTURE); 5133 } 5134 5135 static void guc_context_replay(struct intel_context *ce) 5136 { 5137 struct i915_sched_engine *sched_engine = ce->engine->sched_engine; 5138 5139 __guc_reset_context(ce, ce->engine->mask); 5140 tasklet_hi_schedule(&sched_engine->tasklet); 5141 } 5142 5143 static void guc_handle_context_reset(struct intel_guc *guc, 5144 struct intel_context *ce) 5145 { 5146 bool capture = intel_context_is_schedulable(ce); 5147 5148 trace_intel_context_reset(ce); 5149 5150 guc_dbg(guc, "%s context reset notification: 0x%04X on %s, exiting = %s, banned = %s\n", 5151 capture ? "Got" : "Ignoring", 5152 ce->guc_id.id, ce->engine->name, 5153 str_yes_no(intel_context_is_exiting(ce)), 5154 str_yes_no(intel_context_is_banned(ce))); 5155 5156 if (capture) { 5157 capture_error_state(guc, ce); 5158 guc_context_replay(ce); 5159 } 5160 } 5161 5162 int intel_guc_context_reset_process_msg(struct intel_guc *guc, 5163 const u32 *msg, u32 len) 5164 { 5165 struct intel_context *ce; 5166 unsigned long flags; 5167 int ctx_id; 5168 5169 if (unlikely(len != 1)) { 5170 guc_err(guc, "Invalid length %u", len); 5171 return -EPROTO; 5172 } 5173 5174 ctx_id = msg[0]; 5175 5176 /* 5177 * The context lookup uses the xarray but lookups only require an RCU lock 5178 * not the full spinlock. So take the lock explicitly and keep it until the 5179 * context has been reference count locked to ensure it can't be destroyed 5180 * asynchronously until the reset is done. 5181 */ 5182 xa_lock_irqsave(&guc->context_lookup, flags); 5183 ce = g2h_context_lookup(guc, ctx_id); 5184 if (ce) 5185 intel_context_get(ce); 5186 xa_unlock_irqrestore(&guc->context_lookup, flags); 5187 5188 if (unlikely(!ce)) 5189 return -EPROTO; 5190 5191 guc_handle_context_reset(guc, ce); 5192 intel_context_put(ce); 5193 5194 return 0; 5195 } 5196 5197 int intel_guc_error_capture_process_msg(struct intel_guc *guc, 5198 const u32 *msg, u32 len) 5199 { 5200 u32 status; 5201 5202 if (unlikely(len != 1)) { 5203 guc_dbg(guc, "Invalid length %u", len); 5204 return -EPROTO; 5205 } 5206 5207 status = msg[0] & INTEL_GUC_STATE_CAPTURE_EVENT_STATUS_MASK; 5208 if (status == INTEL_GUC_STATE_CAPTURE_EVENT_STATUS_NOSPACE) 5209 guc_warn(guc, "No space for error capture"); 5210 5211 intel_guc_capture_process(guc); 5212 5213 return 0; 5214 } 5215 5216 struct intel_engine_cs * 5217 intel_guc_lookup_engine(struct intel_guc *guc, u8 guc_class, u8 instance) 5218 { 5219 struct intel_gt *gt = guc_to_gt(guc); 5220 u8 engine_class = guc_class_to_engine_class(guc_class); 5221 5222 /* Class index is checked in class converter */ 5223 GEM_BUG_ON(instance > MAX_ENGINE_INSTANCE); 5224 5225 return gt->engine_class[engine_class][instance]; 5226 } 5227 5228 static void reset_fail_worker_func(struct work_struct *w) 5229 { 5230 struct intel_guc *guc = container_of(w, struct intel_guc, 5231 submission_state.reset_fail_worker); 5232 struct intel_gt *gt = guc_to_gt(guc); 5233 intel_engine_mask_t reset_fail_mask; 5234 unsigned long flags; 5235 5236 spin_lock_irqsave(&guc->submission_state.lock, flags); 5237 reset_fail_mask = guc->submission_state.reset_fail_mask; 5238 guc->submission_state.reset_fail_mask = 0; 5239 spin_unlock_irqrestore(&guc->submission_state.lock, flags); 5240 5241 if (likely(reset_fail_mask)) { 5242 struct intel_engine_cs *engine; 5243 enum intel_engine_id id; 5244 5245 /* 5246 * GuC is toast at this point - it dead loops after sending the failed 5247 * reset notification. So need to manually determine the guilty context. 5248 * Note that it should be reliable to do this here because the GuC is 5249 * toast and will not be scheduling behind the KMD's back. 5250 */ 5251 for_each_engine_masked(engine, gt, reset_fail_mask, id) 5252 intel_guc_find_hung_context(engine); 5253 5254 intel_gt_handle_error(gt, reset_fail_mask, 5255 I915_ERROR_CAPTURE, 5256 "GuC failed to reset engine mask=0x%x", 5257 reset_fail_mask); 5258 } 5259 } 5260 5261 int intel_guc_engine_failure_process_msg(struct intel_guc *guc, 5262 const u32 *msg, u32 len) 5263 { 5264 struct intel_engine_cs *engine; 5265 u8 guc_class, instance; 5266 u32 reason; 5267 unsigned long flags; 5268 5269 if (unlikely(len != 3)) { 5270 guc_err(guc, "Invalid length %u", len); 5271 return -EPROTO; 5272 } 5273 5274 guc_class = msg[0]; 5275 instance = msg[1]; 5276 reason = msg[2]; 5277 5278 engine = intel_guc_lookup_engine(guc, guc_class, instance); 5279 if (unlikely(!engine)) { 5280 guc_err(guc, "Invalid engine %d:%d", guc_class, instance); 5281 return -EPROTO; 5282 } 5283 5284 /* 5285 * This is an unexpected failure of a hardware feature. So, log a real 5286 * error message not just the informational that comes with the reset. 5287 */ 5288 guc_err(guc, "Engine reset failed on %d:%d (%s) because 0x%08X", 5289 guc_class, instance, engine->name, reason); 5290 5291 spin_lock_irqsave(&guc->submission_state.lock, flags); 5292 guc->submission_state.reset_fail_mask |= engine->mask; 5293 spin_unlock_irqrestore(&guc->submission_state.lock, flags); 5294 5295 /* 5296 * A GT reset flushes this worker queue (G2H handler) so we must use 5297 * another worker to trigger a GT reset. 5298 */ 5299 queue_work(system_unbound_wq, &guc->submission_state.reset_fail_worker); 5300 5301 return 0; 5302 } 5303 5304 void intel_guc_find_hung_context(struct intel_engine_cs *engine) 5305 { 5306 struct intel_guc *guc = &engine->gt->uc.guc; 5307 struct intel_context *ce; 5308 struct i915_request *rq; 5309 unsigned long index; 5310 unsigned long flags; 5311 5312 /* Reset called during driver load? GuC not yet initialised! */ 5313 if (unlikely(!guc_submission_initialized(guc))) 5314 return; 5315 5316 xa_lock_irqsave(&guc->context_lookup, flags); 5317 xa_for_each(&guc->context_lookup, index, ce) { 5318 bool found; 5319 5320 if (!kref_get_unless_zero(&ce->ref)) 5321 continue; 5322 5323 xa_unlock(&guc->context_lookup); 5324 5325 if (!intel_context_is_pinned(ce)) 5326 goto next; 5327 5328 if (intel_engine_is_virtual(ce->engine)) { 5329 if (!(ce->engine->mask & engine->mask)) 5330 goto next; 5331 } else { 5332 if (ce->engine != engine) 5333 goto next; 5334 } 5335 5336 found = false; 5337 spin_lock(&ce->guc_state.lock); 5338 list_for_each_entry(rq, &ce->guc_state.requests, sched.link) { 5339 if (i915_test_request_state(rq) != I915_REQUEST_ACTIVE) 5340 continue; 5341 5342 found = true; 5343 break; 5344 } 5345 spin_unlock(&ce->guc_state.lock); 5346 5347 if (found) { 5348 intel_engine_set_hung_context(engine, ce); 5349 5350 /* Can only cope with one hang at a time... */ 5351 intel_context_put(ce); 5352 xa_lock(&guc->context_lookup); 5353 goto done; 5354 } 5355 5356 next: 5357 intel_context_put(ce); 5358 xa_lock(&guc->context_lookup); 5359 } 5360 done: 5361 xa_unlock_irqrestore(&guc->context_lookup, flags); 5362 } 5363 5364 void intel_guc_dump_active_requests(struct intel_engine_cs *engine, 5365 struct i915_request *hung_rq, 5366 struct drm_printer *m) 5367 { 5368 struct intel_guc *guc = &engine->gt->uc.guc; 5369 struct intel_context *ce; 5370 unsigned long index; 5371 unsigned long flags; 5372 5373 /* Reset called during driver load? GuC not yet initialised! */ 5374 if (unlikely(!guc_submission_initialized(guc))) 5375 return; 5376 5377 xa_lock_irqsave(&guc->context_lookup, flags); 5378 xa_for_each(&guc->context_lookup, index, ce) { 5379 if (!kref_get_unless_zero(&ce->ref)) 5380 continue; 5381 5382 xa_unlock(&guc->context_lookup); 5383 5384 if (!intel_context_is_pinned(ce)) 5385 goto next; 5386 5387 if (intel_engine_is_virtual(ce->engine)) { 5388 if (!(ce->engine->mask & engine->mask)) 5389 goto next; 5390 } else { 5391 if (ce->engine != engine) 5392 goto next; 5393 } 5394 5395 spin_lock(&ce->guc_state.lock); 5396 intel_engine_dump_active_requests(&ce->guc_state.requests, 5397 hung_rq, m); 5398 spin_unlock(&ce->guc_state.lock); 5399 5400 next: 5401 intel_context_put(ce); 5402 xa_lock(&guc->context_lookup); 5403 } 5404 xa_unlock_irqrestore(&guc->context_lookup, flags); 5405 } 5406 5407 void intel_guc_submission_print_info(struct intel_guc *guc, 5408 struct drm_printer *p) 5409 { 5410 struct i915_sched_engine *sched_engine = guc->sched_engine; 5411 struct rb_node *rb; 5412 unsigned long flags; 5413 5414 if (!sched_engine) 5415 return; 5416 5417 drm_printf(p, "GuC Submission API Version: %d.%d.%d\n", 5418 guc->submission_version.major, guc->submission_version.minor, 5419 guc->submission_version.patch); 5420 drm_printf(p, "GuC Number Outstanding Submission G2H: %u\n", 5421 atomic_read(&guc->outstanding_submission_g2h)); 5422 drm_printf(p, "GuC tasklet count: %u\n", 5423 atomic_read(&sched_engine->tasklet.count)); 5424 5425 spin_lock_irqsave(&sched_engine->lock, flags); 5426 drm_printf(p, "Requests in GuC submit tasklet:\n"); 5427 for (rb = rb_first_cached(&sched_engine->queue); rb; rb = rb_next(rb)) { 5428 struct i915_priolist *pl = to_priolist(rb); 5429 struct i915_request *rq; 5430 5431 priolist_for_each_request(rq, pl) 5432 drm_printf(p, "guc_id=%u, seqno=%llu\n", 5433 rq->context->guc_id.id, 5434 rq->fence.seqno); 5435 } 5436 spin_unlock_irqrestore(&sched_engine->lock, flags); 5437 drm_printf(p, "\n"); 5438 } 5439 5440 static inline void guc_log_context_priority(struct drm_printer *p, 5441 struct intel_context *ce) 5442 { 5443 int i; 5444 5445 drm_printf(p, "\t\tPriority: %d\n", ce->guc_state.prio); 5446 drm_printf(p, "\t\tNumber Requests (lower index == higher priority)\n"); 5447 for (i = GUC_CLIENT_PRIORITY_KMD_HIGH; 5448 i < GUC_CLIENT_PRIORITY_NUM; ++i) { 5449 drm_printf(p, "\t\tNumber requests in priority band[%d]: %d\n", 5450 i, ce->guc_state.prio_count[i]); 5451 } 5452 drm_printf(p, "\n"); 5453 } 5454 5455 static inline void guc_log_context(struct drm_printer *p, 5456 struct intel_context *ce) 5457 { 5458 drm_printf(p, "GuC lrc descriptor %u:\n", ce->guc_id.id); 5459 drm_printf(p, "\tHW Context Desc: 0x%08x\n", ce->lrc.lrca); 5460 drm_printf(p, "\t\tLRC Head: Internal %u, Memory %u\n", 5461 ce->ring->head, 5462 ce->lrc_reg_state[CTX_RING_HEAD]); 5463 drm_printf(p, "\t\tLRC Tail: Internal %u, Memory %u\n", 5464 ce->ring->tail, 5465 ce->lrc_reg_state[CTX_RING_TAIL]); 5466 drm_printf(p, "\t\tContext Pin Count: %u\n", 5467 atomic_read(&ce->pin_count)); 5468 drm_printf(p, "\t\tGuC ID Ref Count: %u\n", 5469 atomic_read(&ce->guc_id.ref)); 5470 drm_printf(p, "\t\tSchedule State: 0x%x\n", 5471 ce->guc_state.sched_state); 5472 } 5473 5474 void intel_guc_submission_print_context_info(struct intel_guc *guc, 5475 struct drm_printer *p) 5476 { 5477 struct intel_context *ce; 5478 unsigned long index; 5479 unsigned long flags; 5480 5481 xa_lock_irqsave(&guc->context_lookup, flags); 5482 xa_for_each(&guc->context_lookup, index, ce) { 5483 GEM_BUG_ON(intel_context_is_child(ce)); 5484 5485 guc_log_context(p, ce); 5486 guc_log_context_priority(p, ce); 5487 5488 if (intel_context_is_parent(ce)) { 5489 struct intel_context *child; 5490 5491 drm_printf(p, "\t\tNumber children: %u\n", 5492 ce->parallel.number_children); 5493 5494 if (ce->parallel.guc.wq_status) { 5495 drm_printf(p, "\t\tWQI Head: %u\n", 5496 READ_ONCE(*ce->parallel.guc.wq_head)); 5497 drm_printf(p, "\t\tWQI Tail: %u\n", 5498 READ_ONCE(*ce->parallel.guc.wq_tail)); 5499 drm_printf(p, "\t\tWQI Status: %u\n", 5500 READ_ONCE(*ce->parallel.guc.wq_status)); 5501 } 5502 5503 if (ce->engine->emit_bb_start == 5504 emit_bb_start_parent_no_preempt_mid_batch) { 5505 u8 i; 5506 5507 drm_printf(p, "\t\tChildren Go: %u\n", 5508 get_children_go_value(ce)); 5509 for (i = 0; i < ce->parallel.number_children; ++i) 5510 drm_printf(p, "\t\tChildren Join: %u\n", 5511 get_children_join_value(ce, i)); 5512 } 5513 5514 for_each_child(ce, child) 5515 guc_log_context(p, child); 5516 } 5517 } 5518 xa_unlock_irqrestore(&guc->context_lookup, flags); 5519 } 5520 5521 static inline u32 get_children_go_addr(struct intel_context *ce) 5522 { 5523 GEM_BUG_ON(!intel_context_is_parent(ce)); 5524 5525 return i915_ggtt_offset(ce->state) + 5526 __get_parent_scratch_offset(ce) + 5527 offsetof(struct parent_scratch, go.semaphore); 5528 } 5529 5530 static inline u32 get_children_join_addr(struct intel_context *ce, 5531 u8 child_index) 5532 { 5533 GEM_BUG_ON(!intel_context_is_parent(ce)); 5534 5535 return i915_ggtt_offset(ce->state) + 5536 __get_parent_scratch_offset(ce) + 5537 offsetof(struct parent_scratch, join[child_index].semaphore); 5538 } 5539 5540 #define PARENT_GO_BB 1 5541 #define PARENT_GO_FINI_BREADCRUMB 0 5542 #define CHILD_GO_BB 1 5543 #define CHILD_GO_FINI_BREADCRUMB 0 5544 static int emit_bb_start_parent_no_preempt_mid_batch(struct i915_request *rq, 5545 u64 offset, u32 len, 5546 const unsigned int flags) 5547 { 5548 struct intel_context *ce = rq->context; 5549 u32 *cs; 5550 u8 i; 5551 5552 GEM_BUG_ON(!intel_context_is_parent(ce)); 5553 5554 cs = intel_ring_begin(rq, 10 + 4 * ce->parallel.number_children); 5555 if (IS_ERR(cs)) 5556 return PTR_ERR(cs); 5557 5558 /* Wait on children */ 5559 for (i = 0; i < ce->parallel.number_children; ++i) { 5560 *cs++ = (MI_SEMAPHORE_WAIT | 5561 MI_SEMAPHORE_GLOBAL_GTT | 5562 MI_SEMAPHORE_POLL | 5563 MI_SEMAPHORE_SAD_EQ_SDD); 5564 *cs++ = PARENT_GO_BB; 5565 *cs++ = get_children_join_addr(ce, i); 5566 *cs++ = 0; 5567 } 5568 5569 /* Turn off preemption */ 5570 *cs++ = MI_ARB_ON_OFF | MI_ARB_DISABLE; 5571 *cs++ = MI_NOOP; 5572 5573 /* Tell children go */ 5574 cs = gen8_emit_ggtt_write(cs, 5575 CHILD_GO_BB, 5576 get_children_go_addr(ce), 5577 0); 5578 5579 /* Jump to batch */ 5580 *cs++ = MI_BATCH_BUFFER_START_GEN8 | 5581 (flags & I915_DISPATCH_SECURE ? 0 : BIT(8)); 5582 *cs++ = lower_32_bits(offset); 5583 *cs++ = upper_32_bits(offset); 5584 *cs++ = MI_NOOP; 5585 5586 intel_ring_advance(rq, cs); 5587 5588 return 0; 5589 } 5590 5591 static int emit_bb_start_child_no_preempt_mid_batch(struct i915_request *rq, 5592 u64 offset, u32 len, 5593 const unsigned int flags) 5594 { 5595 struct intel_context *ce = rq->context; 5596 struct intel_context *parent = intel_context_to_parent(ce); 5597 u32 *cs; 5598 5599 GEM_BUG_ON(!intel_context_is_child(ce)); 5600 5601 cs = intel_ring_begin(rq, 12); 5602 if (IS_ERR(cs)) 5603 return PTR_ERR(cs); 5604 5605 /* Signal parent */ 5606 cs = gen8_emit_ggtt_write(cs, 5607 PARENT_GO_BB, 5608 get_children_join_addr(parent, 5609 ce->parallel.child_index), 5610 0); 5611 5612 /* Wait on parent for go */ 5613 *cs++ = (MI_SEMAPHORE_WAIT | 5614 MI_SEMAPHORE_GLOBAL_GTT | 5615 MI_SEMAPHORE_POLL | 5616 MI_SEMAPHORE_SAD_EQ_SDD); 5617 *cs++ = CHILD_GO_BB; 5618 *cs++ = get_children_go_addr(parent); 5619 *cs++ = 0; 5620 5621 /* Turn off preemption */ 5622 *cs++ = MI_ARB_ON_OFF | MI_ARB_DISABLE; 5623 5624 /* Jump to batch */ 5625 *cs++ = MI_BATCH_BUFFER_START_GEN8 | 5626 (flags & I915_DISPATCH_SECURE ? 0 : BIT(8)); 5627 *cs++ = lower_32_bits(offset); 5628 *cs++ = upper_32_bits(offset); 5629 5630 intel_ring_advance(rq, cs); 5631 5632 return 0; 5633 } 5634 5635 static u32 * 5636 __emit_fini_breadcrumb_parent_no_preempt_mid_batch(struct i915_request *rq, 5637 u32 *cs) 5638 { 5639 struct intel_context *ce = rq->context; 5640 u8 i; 5641 5642 GEM_BUG_ON(!intel_context_is_parent(ce)); 5643 5644 /* Wait on children */ 5645 for (i = 0; i < ce->parallel.number_children; ++i) { 5646 *cs++ = (MI_SEMAPHORE_WAIT | 5647 MI_SEMAPHORE_GLOBAL_GTT | 5648 MI_SEMAPHORE_POLL | 5649 MI_SEMAPHORE_SAD_EQ_SDD); 5650 *cs++ = PARENT_GO_FINI_BREADCRUMB; 5651 *cs++ = get_children_join_addr(ce, i); 5652 *cs++ = 0; 5653 } 5654 5655 /* Turn on preemption */ 5656 *cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE; 5657 *cs++ = MI_NOOP; 5658 5659 /* Tell children go */ 5660 cs = gen8_emit_ggtt_write(cs, 5661 CHILD_GO_FINI_BREADCRUMB, 5662 get_children_go_addr(ce), 5663 0); 5664 5665 return cs; 5666 } 5667 5668 /* 5669 * If this true, a submission of multi-lrc requests had an error and the 5670 * requests need to be skipped. The front end (execuf IOCTL) should've called 5671 * i915_request_skip which squashes the BB but we still need to emit the fini 5672 * breadrcrumbs seqno write. At this point we don't know how many of the 5673 * requests in the multi-lrc submission were generated so we can't do the 5674 * handshake between the parent and children (e.g. if 4 requests should be 5675 * generated but 2nd hit an error only 1 would be seen by the GuC backend). 5676 * Simply skip the handshake, but still emit the breadcrumbd seqno, if an error 5677 * has occurred on any of the requests in submission / relationship. 5678 */ 5679 static inline bool skip_handshake(struct i915_request *rq) 5680 { 5681 return test_bit(I915_FENCE_FLAG_SKIP_PARALLEL, &rq->fence.flags); 5682 } 5683 5684 #define NON_SKIP_LEN 6 5685 static u32 * 5686 emit_fini_breadcrumb_parent_no_preempt_mid_batch(struct i915_request *rq, 5687 u32 *cs) 5688 { 5689 struct intel_context *ce = rq->context; 5690 __maybe_unused u32 *before_fini_breadcrumb_user_interrupt_cs; 5691 __maybe_unused u32 *start_fini_breadcrumb_cs = cs; 5692 5693 GEM_BUG_ON(!intel_context_is_parent(ce)); 5694 5695 if (unlikely(skip_handshake(rq))) { 5696 /* 5697 * NOP everything in __emit_fini_breadcrumb_parent_no_preempt_mid_batch, 5698 * the NON_SKIP_LEN comes from the length of the emits below. 5699 */ 5700 memset(cs, 0, sizeof(u32) * 5701 (ce->engine->emit_fini_breadcrumb_dw - NON_SKIP_LEN)); 5702 cs += ce->engine->emit_fini_breadcrumb_dw - NON_SKIP_LEN; 5703 } else { 5704 cs = __emit_fini_breadcrumb_parent_no_preempt_mid_batch(rq, cs); 5705 } 5706 5707 /* Emit fini breadcrumb */ 5708 before_fini_breadcrumb_user_interrupt_cs = cs; 5709 cs = gen8_emit_ggtt_write(cs, 5710 rq->fence.seqno, 5711 i915_request_active_timeline(rq)->hwsp_offset, 5712 0); 5713 5714 /* User interrupt */ 5715 *cs++ = MI_USER_INTERRUPT; 5716 *cs++ = MI_NOOP; 5717 5718 /* Ensure our math for skip + emit is correct */ 5719 GEM_BUG_ON(before_fini_breadcrumb_user_interrupt_cs + NON_SKIP_LEN != 5720 cs); 5721 GEM_BUG_ON(start_fini_breadcrumb_cs + 5722 ce->engine->emit_fini_breadcrumb_dw != cs); 5723 5724 rq->tail = intel_ring_offset(rq, cs); 5725 5726 return cs; 5727 } 5728 5729 static u32 * 5730 __emit_fini_breadcrumb_child_no_preempt_mid_batch(struct i915_request *rq, 5731 u32 *cs) 5732 { 5733 struct intel_context *ce = rq->context; 5734 struct intel_context *parent = intel_context_to_parent(ce); 5735 5736 GEM_BUG_ON(!intel_context_is_child(ce)); 5737 5738 /* Turn on preemption */ 5739 *cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE; 5740 *cs++ = MI_NOOP; 5741 5742 /* Signal parent */ 5743 cs = gen8_emit_ggtt_write(cs, 5744 PARENT_GO_FINI_BREADCRUMB, 5745 get_children_join_addr(parent, 5746 ce->parallel.child_index), 5747 0); 5748 5749 /* Wait parent on for go */ 5750 *cs++ = (MI_SEMAPHORE_WAIT | 5751 MI_SEMAPHORE_GLOBAL_GTT | 5752 MI_SEMAPHORE_POLL | 5753 MI_SEMAPHORE_SAD_EQ_SDD); 5754 *cs++ = CHILD_GO_FINI_BREADCRUMB; 5755 *cs++ = get_children_go_addr(parent); 5756 *cs++ = 0; 5757 5758 return cs; 5759 } 5760 5761 static u32 * 5762 emit_fini_breadcrumb_child_no_preempt_mid_batch(struct i915_request *rq, 5763 u32 *cs) 5764 { 5765 struct intel_context *ce = rq->context; 5766 __maybe_unused u32 *before_fini_breadcrumb_user_interrupt_cs; 5767 __maybe_unused u32 *start_fini_breadcrumb_cs = cs; 5768 5769 GEM_BUG_ON(!intel_context_is_child(ce)); 5770 5771 if (unlikely(skip_handshake(rq))) { 5772 /* 5773 * NOP everything in __emit_fini_breadcrumb_child_no_preempt_mid_batch, 5774 * the NON_SKIP_LEN comes from the length of the emits below. 5775 */ 5776 memset(cs, 0, sizeof(u32) * 5777 (ce->engine->emit_fini_breadcrumb_dw - NON_SKIP_LEN)); 5778 cs += ce->engine->emit_fini_breadcrumb_dw - NON_SKIP_LEN; 5779 } else { 5780 cs = __emit_fini_breadcrumb_child_no_preempt_mid_batch(rq, cs); 5781 } 5782 5783 /* Emit fini breadcrumb */ 5784 before_fini_breadcrumb_user_interrupt_cs = cs; 5785 cs = gen8_emit_ggtt_write(cs, 5786 rq->fence.seqno, 5787 i915_request_active_timeline(rq)->hwsp_offset, 5788 0); 5789 5790 /* User interrupt */ 5791 *cs++ = MI_USER_INTERRUPT; 5792 *cs++ = MI_NOOP; 5793 5794 /* Ensure our math for skip + emit is correct */ 5795 GEM_BUG_ON(before_fini_breadcrumb_user_interrupt_cs + NON_SKIP_LEN != 5796 cs); 5797 GEM_BUG_ON(start_fini_breadcrumb_cs + 5798 ce->engine->emit_fini_breadcrumb_dw != cs); 5799 5800 rq->tail = intel_ring_offset(rq, cs); 5801 5802 return cs; 5803 } 5804 5805 #undef NON_SKIP_LEN 5806 5807 static struct intel_context * 5808 guc_create_virtual(struct intel_engine_cs **siblings, unsigned int count, 5809 unsigned long flags) 5810 { 5811 struct guc_virtual_engine *ve; 5812 struct intel_guc *guc; 5813 unsigned int n; 5814 int err; 5815 5816 ve = kzalloc(sizeof(*ve), GFP_KERNEL); 5817 if (!ve) 5818 return ERR_PTR(-ENOMEM); 5819 5820 guc = &siblings[0]->gt->uc.guc; 5821 5822 ve->base.i915 = siblings[0]->i915; 5823 ve->base.gt = siblings[0]->gt; 5824 ve->base.uncore = siblings[0]->uncore; 5825 ve->base.id = -1; 5826 5827 ve->base.uabi_class = I915_ENGINE_CLASS_INVALID; 5828 ve->base.instance = I915_ENGINE_CLASS_INVALID_VIRTUAL; 5829 ve->base.uabi_instance = I915_ENGINE_CLASS_INVALID_VIRTUAL; 5830 ve->base.saturated = ALL_ENGINES; 5831 5832 snprintf(ve->base.name, sizeof(ve->base.name), "virtual"); 5833 5834 ve->base.sched_engine = i915_sched_engine_get(guc->sched_engine); 5835 5836 ve->base.cops = &virtual_guc_context_ops; 5837 ve->base.request_alloc = guc_request_alloc; 5838 ve->base.bump_serial = virtual_guc_bump_serial; 5839 5840 ve->base.submit_request = guc_submit_request; 5841 5842 ve->base.flags = I915_ENGINE_IS_VIRTUAL; 5843 5844 BUILD_BUG_ON(ilog2(VIRTUAL_ENGINES) < I915_NUM_ENGINES); 5845 ve->base.mask = VIRTUAL_ENGINES; 5846 5847 intel_context_init(&ve->context, &ve->base); 5848 5849 for (n = 0; n < count; n++) { 5850 struct intel_engine_cs *sibling = siblings[n]; 5851 5852 GEM_BUG_ON(!is_power_of_2(sibling->mask)); 5853 if (sibling->mask & ve->base.mask) { 5854 guc_dbg(guc, "duplicate %s entry in load balancer\n", 5855 sibling->name); 5856 err = -EINVAL; 5857 goto err_put; 5858 } 5859 5860 ve->base.mask |= sibling->mask; 5861 ve->base.logical_mask |= sibling->logical_mask; 5862 5863 if (n != 0 && ve->base.class != sibling->class) { 5864 guc_dbg(guc, "invalid mixing of engine class, sibling %d, already %d\n", 5865 sibling->class, ve->base.class); 5866 err = -EINVAL; 5867 goto err_put; 5868 } else if (n == 0) { 5869 ve->base.class = sibling->class; 5870 ve->base.uabi_class = sibling->uabi_class; 5871 snprintf(ve->base.name, sizeof(ve->base.name), 5872 "v%dx%d", ve->base.class, count); 5873 ve->base.context_size = sibling->context_size; 5874 5875 ve->base.add_active_request = 5876 sibling->add_active_request; 5877 ve->base.remove_active_request = 5878 sibling->remove_active_request; 5879 ve->base.emit_bb_start = sibling->emit_bb_start; 5880 ve->base.emit_flush = sibling->emit_flush; 5881 ve->base.emit_init_breadcrumb = 5882 sibling->emit_init_breadcrumb; 5883 ve->base.emit_fini_breadcrumb = 5884 sibling->emit_fini_breadcrumb; 5885 ve->base.emit_fini_breadcrumb_dw = 5886 sibling->emit_fini_breadcrumb_dw; 5887 ve->base.breadcrumbs = 5888 intel_breadcrumbs_get(sibling->breadcrumbs); 5889 5890 ve->base.flags |= sibling->flags; 5891 5892 ve->base.props.timeslice_duration_ms = 5893 sibling->props.timeslice_duration_ms; 5894 ve->base.props.preempt_timeout_ms = 5895 sibling->props.preempt_timeout_ms; 5896 } 5897 } 5898 5899 return &ve->context; 5900 5901 err_put: 5902 intel_context_put(&ve->context); 5903 return ERR_PTR(err); 5904 } 5905 5906 bool intel_guc_virtual_engine_has_heartbeat(const struct intel_engine_cs *ve) 5907 { 5908 struct intel_engine_cs *engine; 5909 intel_engine_mask_t tmp, mask = ve->mask; 5910 5911 for_each_engine_masked(engine, ve->gt, mask, tmp) 5912 if (READ_ONCE(engine->props.heartbeat_interval_ms)) 5913 return true; 5914 5915 return false; 5916 } 5917 5918 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST) 5919 #include "selftest_guc.c" 5920 #include "selftest_guc_multi_lrc.c" 5921 #include "selftest_guc_hangcheck.c" 5922 #endif 5923