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 gt_to_guc(ce->engine->gt); 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 occurrences. 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 __set_engine_usage_record(struct intel_engine_cs *engine, 1247 u32 last_in, u32 id, u32 total) 1248 { 1249 struct iosys_map rec_map = intel_guc_engine_usage_record_map(engine); 1250 1251 #define record_write(map_, field_, val_) \ 1252 iosys_map_wr_field(map_, 0, struct guc_engine_usage_record, field_, val_) 1253 1254 record_write(&rec_map, last_switch_in_stamp, last_in); 1255 record_write(&rec_map, current_context_index, id); 1256 record_write(&rec_map, total_runtime, total); 1257 1258 #undef record_write 1259 } 1260 1261 static void guc_update_engine_gt_clks(struct intel_engine_cs *engine) 1262 { 1263 struct intel_engine_guc_stats *stats = &engine->stats.guc; 1264 struct intel_guc *guc = gt_to_guc(engine->gt); 1265 u32 last_switch, ctx_id, total; 1266 1267 lockdep_assert_held(&guc->timestamp.lock); 1268 1269 __get_engine_usage_record(engine, &last_switch, &ctx_id, &total); 1270 1271 stats->running = ctx_id != ~0U && last_switch; 1272 if (stats->running) 1273 __extend_last_switch(guc, &stats->start_gt_clk, last_switch); 1274 1275 /* 1276 * Instead of adjusting the total for overflow, just add the 1277 * difference from previous sample stats->total_gt_clks 1278 */ 1279 if (total && total != ~0U) { 1280 stats->total_gt_clks += (u32)(total - stats->prev_total); 1281 stats->prev_total = total; 1282 } 1283 } 1284 1285 static u32 gpm_timestamp_shift(struct intel_gt *gt) 1286 { 1287 intel_wakeref_t wakeref; 1288 u32 reg, shift; 1289 1290 with_intel_runtime_pm(gt->uncore->rpm, wakeref) 1291 reg = intel_uncore_read(gt->uncore, RPM_CONFIG0); 1292 1293 shift = (reg & GEN10_RPM_CONFIG0_CTC_SHIFT_PARAMETER_MASK) >> 1294 GEN10_RPM_CONFIG0_CTC_SHIFT_PARAMETER_SHIFT; 1295 1296 return 3 - shift; 1297 } 1298 1299 static void guc_update_pm_timestamp(struct intel_guc *guc, ktime_t *now) 1300 { 1301 struct intel_gt *gt = guc_to_gt(guc); 1302 u32 gt_stamp_lo, gt_stamp_hi; 1303 u64 gpm_ts; 1304 1305 lockdep_assert_held(&guc->timestamp.lock); 1306 1307 gt_stamp_hi = upper_32_bits(guc->timestamp.gt_stamp); 1308 gpm_ts = intel_uncore_read64_2x32(gt->uncore, MISC_STATUS0, 1309 MISC_STATUS1) >> guc->timestamp.shift; 1310 gt_stamp_lo = lower_32_bits(gpm_ts); 1311 *now = ktime_get(); 1312 1313 if (gt_stamp_lo < lower_32_bits(guc->timestamp.gt_stamp)) 1314 gt_stamp_hi++; 1315 1316 guc->timestamp.gt_stamp = ((u64)gt_stamp_hi << 32) | gt_stamp_lo; 1317 } 1318 1319 /* 1320 * Unlike the execlist mode of submission total and active times are in terms of 1321 * gt clocks. The *now parameter is retained to return the cpu time at which the 1322 * busyness was sampled. 1323 */ 1324 static ktime_t guc_engine_busyness(struct intel_engine_cs *engine, ktime_t *now) 1325 { 1326 struct intel_engine_guc_stats stats_saved, *stats = &engine->stats.guc; 1327 struct i915_gpu_error *gpu_error = &engine->i915->gpu_error; 1328 struct intel_gt *gt = engine->gt; 1329 struct intel_guc *guc = gt_to_guc(gt); 1330 u64 total, gt_stamp_saved; 1331 unsigned long flags; 1332 u32 reset_count; 1333 bool in_reset; 1334 intel_wakeref_t wakeref; 1335 1336 spin_lock_irqsave(&guc->timestamp.lock, flags); 1337 1338 /* 1339 * If a reset happened, we risk reading partially updated engine 1340 * busyness from GuC, so we just use the driver stored copy of busyness. 1341 * Synchronize with gt reset using reset_count and the 1342 * I915_RESET_BACKOFF flag. Note that reset flow updates the reset_count 1343 * after I915_RESET_BACKOFF flag, so ensure that the reset_count is 1344 * usable by checking the flag afterwards. 1345 */ 1346 reset_count = i915_reset_count(gpu_error); 1347 in_reset = test_bit(I915_RESET_BACKOFF, >->reset.flags); 1348 1349 *now = ktime_get(); 1350 1351 /* 1352 * The active busyness depends on start_gt_clk and gt_stamp. 1353 * gt_stamp is updated by i915 only when gt is awake and the 1354 * start_gt_clk is derived from GuC state. To get a consistent 1355 * view of activity, we query the GuC state only if gt is awake. 1356 */ 1357 wakeref = in_reset ? NULL : intel_gt_pm_get_if_awake(gt); 1358 if (wakeref) { 1359 stats_saved = *stats; 1360 gt_stamp_saved = guc->timestamp.gt_stamp; 1361 /* 1362 * Update gt_clks, then gt timestamp to simplify the 'gt_stamp - 1363 * start_gt_clk' calculation below for active engines. 1364 */ 1365 guc_update_engine_gt_clks(engine); 1366 guc_update_pm_timestamp(guc, now); 1367 intel_gt_pm_put_async(gt, wakeref); 1368 if (i915_reset_count(gpu_error) != reset_count) { 1369 *stats = stats_saved; 1370 guc->timestamp.gt_stamp = gt_stamp_saved; 1371 } 1372 } 1373 1374 total = intel_gt_clock_interval_to_ns(gt, stats->total_gt_clks); 1375 if (stats->running) { 1376 u64 clk = guc->timestamp.gt_stamp - stats->start_gt_clk; 1377 1378 total += intel_gt_clock_interval_to_ns(gt, clk); 1379 } 1380 1381 if (total > stats->total) 1382 stats->total = total; 1383 1384 spin_unlock_irqrestore(&guc->timestamp.lock, flags); 1385 1386 return ns_to_ktime(stats->total); 1387 } 1388 1389 static void guc_enable_busyness_worker(struct intel_guc *guc) 1390 { 1391 mod_delayed_work(system_highpri_wq, &guc->timestamp.work, guc->timestamp.ping_delay); 1392 } 1393 1394 static void guc_cancel_busyness_worker(struct intel_guc *guc) 1395 { 1396 /* 1397 * There are many different call stacks that can get here. Some of them 1398 * hold the reset mutex. The busyness worker also attempts to acquire the 1399 * reset mutex. Synchronously flushing a worker thread requires acquiring 1400 * the worker mutex. Lockdep sees this as a conflict. It thinks that the 1401 * flush can deadlock because it holds the worker mutex while waiting for 1402 * the reset mutex, but another thread is holding the reset mutex and might 1403 * attempt to use other worker functions. 1404 * 1405 * In practice, this scenario does not exist because the busyness worker 1406 * does not block waiting for the reset mutex. It does a try-lock on it and 1407 * immediately exits if the lock is already held. Unfortunately, the mutex 1408 * in question (I915_RESET_BACKOFF) is an i915 implementation which has lockdep 1409 * annotation but not to the extent of explaining the 'might lock' is also a 1410 * 'does not need to lock'. So one option would be to add more complex lockdep 1411 * annotations to ignore the issue (if at all possible). A simpler option is to 1412 * just not flush synchronously when a rest in progress. Given that the worker 1413 * will just early exit and re-schedule itself anyway, there is no advantage 1414 * to running it immediately. 1415 * 1416 * If a reset is not in progress, then the synchronous flush may be required. 1417 * As noted many call stacks lead here, some during suspend and driver unload 1418 * which do require a synchronous flush to make sure the worker is stopped 1419 * before memory is freed. 1420 * 1421 * Trying to pass a 'need_sync' or 'in_reset' flag all the way down through 1422 * every possible call stack is unfeasible. It would be too intrusive to many 1423 * areas that really don't care about the GuC backend. However, there is the 1424 * I915_RESET_BACKOFF flag and the gt->reset.mutex can be tested for is_locked. 1425 * So just use those. Note that testing both is required due to the hideously 1426 * complex nature of the i915 driver's reset code paths. 1427 * 1428 * And note that in the case of a reset occurring during driver unload 1429 * (wedged_on_fini), skipping the cancel in reset_prepare/reset_fini (when the 1430 * reset flag/mutex are set) is fine because there is another explicit cancel in 1431 * intel_guc_submission_fini (when the reset flag/mutex are not). 1432 */ 1433 if (mutex_is_locked(&guc_to_gt(guc)->reset.mutex) || 1434 test_bit(I915_RESET_BACKOFF, &guc_to_gt(guc)->reset.flags)) 1435 cancel_delayed_work(&guc->timestamp.work); 1436 else 1437 cancel_delayed_work_sync(&guc->timestamp.work); 1438 } 1439 1440 static void __reset_guc_busyness_stats(struct intel_guc *guc) 1441 { 1442 struct intel_gt *gt = guc_to_gt(guc); 1443 struct intel_engine_cs *engine; 1444 enum intel_engine_id id; 1445 unsigned long flags; 1446 ktime_t unused; 1447 1448 spin_lock_irqsave(&guc->timestamp.lock, flags); 1449 1450 guc_update_pm_timestamp(guc, &unused); 1451 for_each_engine(engine, gt, id) { 1452 struct intel_engine_guc_stats *stats = &engine->stats.guc; 1453 1454 guc_update_engine_gt_clks(engine); 1455 1456 /* 1457 * If resetting a running context, accumulate the active 1458 * time as well since there will be no context switch. 1459 */ 1460 if (stats->running) { 1461 u64 clk = guc->timestamp.gt_stamp - stats->start_gt_clk; 1462 1463 stats->total_gt_clks += clk; 1464 } 1465 stats->prev_total = 0; 1466 stats->running = 0; 1467 } 1468 1469 spin_unlock_irqrestore(&guc->timestamp.lock, flags); 1470 } 1471 1472 static void __update_guc_busyness_running_state(struct intel_guc *guc) 1473 { 1474 struct intel_gt *gt = guc_to_gt(guc); 1475 struct intel_engine_cs *engine; 1476 enum intel_engine_id id; 1477 unsigned long flags; 1478 1479 spin_lock_irqsave(&guc->timestamp.lock, flags); 1480 for_each_engine(engine, gt, id) 1481 engine->stats.guc.running = false; 1482 spin_unlock_irqrestore(&guc->timestamp.lock, flags); 1483 } 1484 1485 static void __update_guc_busyness_stats(struct intel_guc *guc) 1486 { 1487 struct intel_gt *gt = guc_to_gt(guc); 1488 struct intel_engine_cs *engine; 1489 enum intel_engine_id id; 1490 unsigned long flags; 1491 ktime_t unused; 1492 1493 guc->timestamp.last_stat_jiffies = jiffies; 1494 1495 spin_lock_irqsave(&guc->timestamp.lock, flags); 1496 1497 guc_update_pm_timestamp(guc, &unused); 1498 for_each_engine(engine, gt, id) 1499 guc_update_engine_gt_clks(engine); 1500 1501 spin_unlock_irqrestore(&guc->timestamp.lock, flags); 1502 } 1503 1504 static void __guc_context_update_stats(struct intel_context *ce) 1505 { 1506 struct intel_guc *guc = ce_to_guc(ce); 1507 unsigned long flags; 1508 1509 spin_lock_irqsave(&guc->timestamp.lock, flags); 1510 lrc_update_runtime(ce); 1511 spin_unlock_irqrestore(&guc->timestamp.lock, flags); 1512 } 1513 1514 static void guc_context_update_stats(struct intel_context *ce) 1515 { 1516 if (!intel_context_pin_if_active(ce)) 1517 return; 1518 1519 __guc_context_update_stats(ce); 1520 intel_context_unpin(ce); 1521 } 1522 1523 static void guc_timestamp_ping(struct work_struct *wrk) 1524 { 1525 struct intel_guc *guc = container_of(wrk, typeof(*guc), 1526 timestamp.work.work); 1527 struct intel_uc *uc = container_of(guc, typeof(*uc), guc); 1528 struct intel_gt *gt = guc_to_gt(guc); 1529 struct intel_context *ce; 1530 intel_wakeref_t wakeref; 1531 unsigned long index; 1532 int srcu, ret; 1533 1534 /* 1535 * Ideally the busyness worker should take a gt pm wakeref because the 1536 * worker only needs to be active while gt is awake. However, the 1537 * gt_park path cancels the worker synchronously and this complicates 1538 * the flow if the worker is also running at the same time. The cancel 1539 * waits for the worker and when the worker releases the wakeref, that 1540 * would call gt_park and would lead to a deadlock. 1541 * 1542 * The resolution is to take the global pm wakeref if runtime pm is 1543 * already active. If not, we don't need to update the busyness stats as 1544 * the stats would already be updated when the gt was parked. 1545 * 1546 * Note: 1547 * - We do not requeue the worker if we cannot take a reference to runtime 1548 * pm since intel_guc_busyness_unpark would requeue the worker in the 1549 * resume path. 1550 * 1551 * - If the gt was parked longer than time taken for GT timestamp to roll 1552 * over, we ignore those rollovers since we don't care about tracking 1553 * the exact GT time. We only care about roll overs when the gt is 1554 * active and running workloads. 1555 * 1556 * - There is a window of time between gt_park and runtime suspend, 1557 * where the worker may run. This is acceptable since the worker will 1558 * not find any new data to update busyness. 1559 */ 1560 wakeref = intel_runtime_pm_get_if_active(>->i915->runtime_pm); 1561 if (!wakeref) 1562 return; 1563 1564 /* 1565 * Synchronize with gt reset to make sure the worker does not 1566 * corrupt the engine/guc stats. NB: can't actually block waiting 1567 * for a reset to complete as the reset requires flushing out 1568 * this worker thread if started. So waiting would deadlock. 1569 */ 1570 ret = intel_gt_reset_trylock(gt, &srcu); 1571 if (ret) 1572 goto err_trylock; 1573 1574 __update_guc_busyness_stats(guc); 1575 1576 /* adjust context stats for overflow */ 1577 xa_for_each(&guc->context_lookup, index, ce) 1578 guc_context_update_stats(ce); 1579 1580 intel_gt_reset_unlock(gt, srcu); 1581 1582 guc_enable_busyness_worker(guc); 1583 1584 err_trylock: 1585 intel_runtime_pm_put(>->i915->runtime_pm, wakeref); 1586 } 1587 1588 static int guc_action_enable_usage_stats(struct intel_guc *guc) 1589 { 1590 struct intel_gt *gt = guc_to_gt(guc); 1591 struct intel_engine_cs *engine; 1592 enum intel_engine_id id; 1593 u32 offset = intel_guc_engine_usage_offset(guc); 1594 u32 action[] = { 1595 INTEL_GUC_ACTION_SET_ENG_UTIL_BUFF, 1596 offset, 1597 0, 1598 }; 1599 1600 for_each_engine(engine, gt, id) 1601 __set_engine_usage_record(engine, 0, 0xffffffff, 0); 1602 1603 return intel_guc_send(guc, action, ARRAY_SIZE(action)); 1604 } 1605 1606 static int guc_init_engine_stats(struct intel_guc *guc) 1607 { 1608 struct intel_gt *gt = guc_to_gt(guc); 1609 intel_wakeref_t wakeref; 1610 int ret; 1611 1612 with_intel_runtime_pm(>->i915->runtime_pm, wakeref) 1613 ret = guc_action_enable_usage_stats(guc); 1614 1615 if (ret) 1616 guc_err(guc, "Failed to enable usage stats: %pe\n", ERR_PTR(ret)); 1617 else 1618 guc_enable_busyness_worker(guc); 1619 1620 return ret; 1621 } 1622 1623 static void guc_fini_engine_stats(struct intel_guc *guc) 1624 { 1625 guc_cancel_busyness_worker(guc); 1626 } 1627 1628 void intel_guc_busyness_park(struct intel_gt *gt) 1629 { 1630 struct intel_guc *guc = gt_to_guc(gt); 1631 1632 if (!guc_submission_initialized(guc)) 1633 return; 1634 1635 /* Assume no engines are running and set running state to false */ 1636 __update_guc_busyness_running_state(guc); 1637 1638 /* 1639 * There is a race with suspend flow where the worker runs after suspend 1640 * and causes an unclaimed register access warning. Cancel the worker 1641 * synchronously here. 1642 */ 1643 guc_cancel_busyness_worker(guc); 1644 1645 /* 1646 * Before parking, we should sample engine busyness stats if we need to. 1647 * We can skip it if we are less than half a ping from the last time we 1648 * sampled the busyness stats. 1649 */ 1650 if (guc->timestamp.last_stat_jiffies && 1651 !time_after(jiffies, guc->timestamp.last_stat_jiffies + 1652 (guc->timestamp.ping_delay / 2))) 1653 return; 1654 1655 __update_guc_busyness_stats(guc); 1656 } 1657 1658 void intel_guc_busyness_unpark(struct intel_gt *gt) 1659 { 1660 struct intel_guc *guc = gt_to_guc(gt); 1661 unsigned long flags; 1662 ktime_t unused; 1663 1664 if (!guc_submission_initialized(guc)) 1665 return; 1666 1667 spin_lock_irqsave(&guc->timestamp.lock, flags); 1668 guc_update_pm_timestamp(guc, &unused); 1669 spin_unlock_irqrestore(&guc->timestamp.lock, flags); 1670 guc_enable_busyness_worker(guc); 1671 } 1672 1673 static inline bool 1674 submission_disabled(struct intel_guc *guc) 1675 { 1676 struct i915_sched_engine * const sched_engine = guc->sched_engine; 1677 1678 return unlikely(!sched_engine || 1679 !__tasklet_is_enabled(&sched_engine->tasklet) || 1680 intel_gt_is_wedged(guc_to_gt(guc))); 1681 } 1682 1683 static void disable_submission(struct intel_guc *guc) 1684 { 1685 struct i915_sched_engine * const sched_engine = guc->sched_engine; 1686 1687 if (__tasklet_is_enabled(&sched_engine->tasklet)) { 1688 GEM_BUG_ON(!guc->ct.enabled); 1689 __tasklet_disable_sync_once(&sched_engine->tasklet); 1690 sched_engine->tasklet.callback = NULL; 1691 } 1692 } 1693 1694 static void enable_submission(struct intel_guc *guc) 1695 { 1696 struct i915_sched_engine * const sched_engine = guc->sched_engine; 1697 unsigned long flags; 1698 1699 spin_lock_irqsave(&guc->sched_engine->lock, flags); 1700 sched_engine->tasklet.callback = guc_submission_tasklet; 1701 wmb(); /* Make sure callback visible */ 1702 if (!__tasklet_is_enabled(&sched_engine->tasklet) && 1703 __tasklet_enable(&sched_engine->tasklet)) { 1704 GEM_BUG_ON(!guc->ct.enabled); 1705 1706 /* And kick in case we missed a new request submission. */ 1707 tasklet_hi_schedule(&sched_engine->tasklet); 1708 } 1709 spin_unlock_irqrestore(&guc->sched_engine->lock, flags); 1710 } 1711 1712 static void guc_flush_submissions(struct intel_guc *guc) 1713 { 1714 struct i915_sched_engine * const sched_engine = guc->sched_engine; 1715 unsigned long flags; 1716 1717 spin_lock_irqsave(&sched_engine->lock, flags); 1718 spin_unlock_irqrestore(&sched_engine->lock, flags); 1719 } 1720 1721 void intel_guc_submission_flush_work(struct intel_guc *guc) 1722 { 1723 flush_work(&guc->submission_state.destroyed_worker); 1724 } 1725 1726 static void guc_flush_destroyed_contexts(struct intel_guc *guc); 1727 1728 void intel_guc_submission_reset_prepare(struct intel_guc *guc) 1729 { 1730 if (unlikely(!guc_submission_initialized(guc))) { 1731 /* Reset called during driver load? GuC not yet initialised! */ 1732 return; 1733 } 1734 1735 intel_gt_park_heartbeats(guc_to_gt(guc)); 1736 disable_submission(guc); 1737 guc->interrupts.disable(guc); 1738 __reset_guc_busyness_stats(guc); 1739 1740 /* Flush IRQ handler */ 1741 spin_lock_irq(guc_to_gt(guc)->irq_lock); 1742 spin_unlock_irq(guc_to_gt(guc)->irq_lock); 1743 1744 /* Flush tasklet */ 1745 tasklet_disable(&guc->ct.receive_tasklet); 1746 tasklet_enable(&guc->ct.receive_tasklet); 1747 1748 guc_flush_submissions(guc); 1749 guc_flush_destroyed_contexts(guc); 1750 flush_work(&guc->ct.requests.worker); 1751 1752 scrub_guc_desc_for_outstanding_g2h(guc); 1753 } 1754 1755 static struct intel_engine_cs * 1756 guc_virtual_get_sibling(struct intel_engine_cs *ve, unsigned int sibling) 1757 { 1758 struct intel_engine_cs *engine; 1759 intel_engine_mask_t tmp, mask = ve->mask; 1760 unsigned int num_siblings = 0; 1761 1762 for_each_engine_masked(engine, ve->gt, mask, tmp) 1763 if (num_siblings++ == sibling) 1764 return engine; 1765 1766 return NULL; 1767 } 1768 1769 static inline struct intel_engine_cs * 1770 __context_to_physical_engine(struct intel_context *ce) 1771 { 1772 struct intel_engine_cs *engine = ce->engine; 1773 1774 if (intel_engine_is_virtual(engine)) 1775 engine = guc_virtual_get_sibling(engine, 0); 1776 1777 return engine; 1778 } 1779 1780 static void guc_reset_state(struct intel_context *ce, u32 head, bool scrub) 1781 { 1782 struct intel_engine_cs *engine = __context_to_physical_engine(ce); 1783 1784 if (!intel_context_is_schedulable(ce)) 1785 return; 1786 1787 GEM_BUG_ON(!intel_context_is_pinned(ce)); 1788 1789 /* 1790 * We want a simple context + ring to execute the breadcrumb update. 1791 * We cannot rely on the context being intact across the GPU hang, 1792 * so clear it and rebuild just what we need for the breadcrumb. 1793 * All pending requests for this context will be zapped, and any 1794 * future request will be after userspace has had the opportunity 1795 * to recreate its own state. 1796 */ 1797 if (scrub) 1798 lrc_init_regs(ce, engine, true); 1799 1800 /* Rerun the request; its payload has been neutered (if guilty). */ 1801 lrc_update_regs(ce, engine, head); 1802 } 1803 1804 static void guc_engine_reset_prepare(struct intel_engine_cs *engine) 1805 { 1806 /* 1807 * Wa_22011802037: In addition to stopping the cs, we need 1808 * to wait for any pending mi force wakeups 1809 */ 1810 if (intel_engine_reset_needs_wa_22011802037(engine->gt)) { 1811 intel_engine_stop_cs(engine); 1812 intel_engine_wait_for_pending_mi_fw(engine); 1813 } 1814 } 1815 1816 static void guc_reset_nop(struct intel_engine_cs *engine) 1817 { 1818 } 1819 1820 static void guc_rewind_nop(struct intel_engine_cs *engine, bool stalled) 1821 { 1822 } 1823 1824 static void 1825 __unwind_incomplete_requests(struct intel_context *ce) 1826 { 1827 struct i915_request *rq, *rn; 1828 struct list_head *pl; 1829 int prio = I915_PRIORITY_INVALID; 1830 struct i915_sched_engine * const sched_engine = 1831 ce->engine->sched_engine; 1832 unsigned long flags; 1833 1834 spin_lock_irqsave(&sched_engine->lock, flags); 1835 spin_lock(&ce->guc_state.lock); 1836 list_for_each_entry_safe_reverse(rq, rn, 1837 &ce->guc_state.requests, 1838 sched.link) { 1839 if (i915_request_completed(rq)) 1840 continue; 1841 1842 list_del_init(&rq->sched.link); 1843 __i915_request_unsubmit(rq); 1844 1845 /* Push the request back into the queue for later resubmission. */ 1846 GEM_BUG_ON(rq_prio(rq) == I915_PRIORITY_INVALID); 1847 if (rq_prio(rq) != prio) { 1848 prio = rq_prio(rq); 1849 pl = i915_sched_lookup_priolist(sched_engine, prio); 1850 } 1851 GEM_BUG_ON(i915_sched_engine_is_empty(sched_engine)); 1852 1853 list_add(&rq->sched.link, pl); 1854 set_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags); 1855 } 1856 spin_unlock(&ce->guc_state.lock); 1857 spin_unlock_irqrestore(&sched_engine->lock, flags); 1858 } 1859 1860 static void __guc_reset_context(struct intel_context *ce, intel_engine_mask_t stalled) 1861 { 1862 bool guilty; 1863 struct i915_request *rq; 1864 unsigned long flags; 1865 u32 head; 1866 int i, number_children = ce->parallel.number_children; 1867 struct intel_context *parent = ce; 1868 1869 GEM_BUG_ON(intel_context_is_child(ce)); 1870 1871 intel_context_get(ce); 1872 1873 /* 1874 * GuC will implicitly mark the context as non-schedulable when it sends 1875 * the reset notification. Make sure our state reflects this change. The 1876 * context will be marked enabled on resubmission. 1877 */ 1878 spin_lock_irqsave(&ce->guc_state.lock, flags); 1879 clr_context_enabled(ce); 1880 spin_unlock_irqrestore(&ce->guc_state.lock, flags); 1881 1882 /* 1883 * For each context in the relationship find the hanging request 1884 * resetting each context / request as needed 1885 */ 1886 for (i = 0; i < number_children + 1; ++i) { 1887 if (!intel_context_is_pinned(ce)) 1888 goto next_context; 1889 1890 guilty = false; 1891 rq = intel_context_get_active_request(ce); 1892 if (!rq) { 1893 head = ce->ring->tail; 1894 goto out_replay; 1895 } 1896 1897 if (i915_request_started(rq)) 1898 guilty = stalled & ce->engine->mask; 1899 1900 GEM_BUG_ON(i915_active_is_idle(&ce->active)); 1901 head = intel_ring_wrap(ce->ring, rq->head); 1902 1903 __i915_request_reset(rq, guilty); 1904 i915_request_put(rq); 1905 out_replay: 1906 guc_reset_state(ce, head, guilty); 1907 next_context: 1908 if (i != number_children) 1909 ce = list_next_entry(ce, parallel.child_link); 1910 } 1911 1912 __unwind_incomplete_requests(parent); 1913 intel_context_put(parent); 1914 } 1915 1916 void wake_up_all_tlb_invalidate(struct intel_guc *guc) 1917 { 1918 struct intel_guc_tlb_wait *wait; 1919 unsigned long i; 1920 1921 if (!intel_guc_tlb_invalidation_is_available(guc)) 1922 return; 1923 1924 xa_lock_irq(&guc->tlb_lookup); 1925 xa_for_each(&guc->tlb_lookup, i, wait) 1926 wake_up(&wait->wq); 1927 xa_unlock_irq(&guc->tlb_lookup); 1928 } 1929 1930 void intel_guc_submission_reset(struct intel_guc *guc, intel_engine_mask_t stalled) 1931 { 1932 struct intel_context *ce; 1933 unsigned long index; 1934 unsigned long flags; 1935 1936 if (unlikely(!guc_submission_initialized(guc))) { 1937 /* Reset called during driver load? GuC not yet initialised! */ 1938 return; 1939 } 1940 1941 xa_lock_irqsave(&guc->context_lookup, flags); 1942 xa_for_each(&guc->context_lookup, index, ce) { 1943 if (!kref_get_unless_zero(&ce->ref)) 1944 continue; 1945 1946 xa_unlock(&guc->context_lookup); 1947 1948 if (intel_context_is_pinned(ce) && 1949 !intel_context_is_child(ce)) 1950 __guc_reset_context(ce, stalled); 1951 1952 intel_context_put(ce); 1953 1954 xa_lock(&guc->context_lookup); 1955 } 1956 xa_unlock_irqrestore(&guc->context_lookup, flags); 1957 1958 /* GuC is blown away, drop all references to contexts */ 1959 xa_destroy(&guc->context_lookup); 1960 } 1961 1962 static void guc_cancel_context_requests(struct intel_context *ce) 1963 { 1964 struct i915_sched_engine *sched_engine = ce_to_guc(ce)->sched_engine; 1965 struct i915_request *rq; 1966 unsigned long flags; 1967 1968 /* Mark all executing requests as skipped. */ 1969 spin_lock_irqsave(&sched_engine->lock, flags); 1970 spin_lock(&ce->guc_state.lock); 1971 list_for_each_entry(rq, &ce->guc_state.requests, sched.link) 1972 i915_request_put(i915_request_mark_eio(rq)); 1973 spin_unlock(&ce->guc_state.lock); 1974 spin_unlock_irqrestore(&sched_engine->lock, flags); 1975 } 1976 1977 static void 1978 guc_cancel_sched_engine_requests(struct i915_sched_engine *sched_engine) 1979 { 1980 struct i915_request *rq, *rn; 1981 struct rb_node *rb; 1982 unsigned long flags; 1983 1984 /* Can be called during boot if GuC fails to load */ 1985 if (!sched_engine) 1986 return; 1987 1988 /* 1989 * Before we call engine->cancel_requests(), we should have exclusive 1990 * access to the submission state. This is arranged for us by the 1991 * caller disabling the interrupt generation, the tasklet and other 1992 * threads that may then access the same state, giving us a free hand 1993 * to reset state. However, we still need to let lockdep be aware that 1994 * we know this state may be accessed in hardirq context, so we 1995 * disable the irq around this manipulation and we want to keep 1996 * the spinlock focused on its duties and not accidentally conflate 1997 * coverage to the submission's irq state. (Similarly, although we 1998 * shouldn't need to disable irq around the manipulation of the 1999 * submission's irq state, we also wish to remind ourselves that 2000 * it is irq state.) 2001 */ 2002 spin_lock_irqsave(&sched_engine->lock, flags); 2003 2004 /* Flush the queued requests to the timeline list (for retiring). */ 2005 while ((rb = rb_first_cached(&sched_engine->queue))) { 2006 struct i915_priolist *p = to_priolist(rb); 2007 2008 priolist_for_each_request_consume(rq, rn, p) { 2009 list_del_init(&rq->sched.link); 2010 2011 __i915_request_submit(rq); 2012 2013 i915_request_put(i915_request_mark_eio(rq)); 2014 } 2015 2016 rb_erase_cached(&p->node, &sched_engine->queue); 2017 i915_priolist_free(p); 2018 } 2019 2020 /* Remaining _unready_ requests will be nop'ed when submitted */ 2021 2022 sched_engine->queue_priority_hint = INT_MIN; 2023 sched_engine->queue = RB_ROOT_CACHED; 2024 2025 spin_unlock_irqrestore(&sched_engine->lock, flags); 2026 } 2027 2028 void intel_guc_submission_cancel_requests(struct intel_guc *guc) 2029 { 2030 struct intel_context *ce; 2031 unsigned long index; 2032 unsigned long flags; 2033 2034 xa_lock_irqsave(&guc->context_lookup, flags); 2035 xa_for_each(&guc->context_lookup, index, ce) { 2036 if (!kref_get_unless_zero(&ce->ref)) 2037 continue; 2038 2039 xa_unlock(&guc->context_lookup); 2040 2041 if (intel_context_is_pinned(ce) && 2042 !intel_context_is_child(ce)) 2043 guc_cancel_context_requests(ce); 2044 2045 intel_context_put(ce); 2046 2047 xa_lock(&guc->context_lookup); 2048 } 2049 xa_unlock_irqrestore(&guc->context_lookup, flags); 2050 2051 guc_cancel_sched_engine_requests(guc->sched_engine); 2052 2053 /* GuC is blown away, drop all references to contexts */ 2054 xa_destroy(&guc->context_lookup); 2055 2056 /* 2057 * Wedged GT won't respond to any TLB invalidation request. Simply 2058 * release all the blocked waiters. 2059 */ 2060 wake_up_all_tlb_invalidate(guc); 2061 } 2062 2063 void intel_guc_submission_reset_finish(struct intel_guc *guc) 2064 { 2065 int outstanding; 2066 2067 /* Reset called during driver load or during wedge? */ 2068 if (unlikely(!guc_submission_initialized(guc) || 2069 !intel_guc_is_fw_running(guc) || 2070 intel_gt_is_wedged(guc_to_gt(guc)))) { 2071 return; 2072 } 2073 2074 /* 2075 * Technically possible for either of these values to be non-zero here, 2076 * but very unlikely + harmless. Regardless let's add an error so we can 2077 * see in CI if this happens frequently / a precursor to taking down the 2078 * machine. 2079 */ 2080 outstanding = atomic_read(&guc->outstanding_submission_g2h); 2081 if (outstanding) 2082 guc_err(guc, "Unexpected outstanding GuC to Host response(s) in reset finish: %d\n", 2083 outstanding); 2084 atomic_set(&guc->outstanding_submission_g2h, 0); 2085 2086 intel_guc_global_policies_update(guc); 2087 enable_submission(guc); 2088 intel_gt_unpark_heartbeats(guc_to_gt(guc)); 2089 2090 /* 2091 * The full GT reset will have cleared the TLB caches and flushed the 2092 * G2H message queue; we can release all the blocked waiters. 2093 */ 2094 wake_up_all_tlb_invalidate(guc); 2095 } 2096 2097 static void destroyed_worker_func(struct work_struct *w); 2098 static void reset_fail_worker_func(struct work_struct *w); 2099 2100 bool intel_guc_tlb_invalidation_is_available(struct intel_guc *guc) 2101 { 2102 return HAS_GUC_TLB_INVALIDATION(guc_to_gt(guc)->i915) && 2103 intel_guc_is_ready(guc); 2104 } 2105 2106 static int init_tlb_lookup(struct intel_guc *guc) 2107 { 2108 struct intel_guc_tlb_wait *wait; 2109 int err; 2110 2111 if (!HAS_GUC_TLB_INVALIDATION(guc_to_gt(guc)->i915)) 2112 return 0; 2113 2114 xa_init_flags(&guc->tlb_lookup, XA_FLAGS_ALLOC); 2115 2116 wait = kzalloc(sizeof(*wait), GFP_KERNEL); 2117 if (!wait) 2118 return -ENOMEM; 2119 2120 init_waitqueue_head(&wait->wq); 2121 2122 /* Preallocate a shared id for use under memory pressure. */ 2123 err = xa_alloc_cyclic_irq(&guc->tlb_lookup, &guc->serial_slot, wait, 2124 xa_limit_32b, &guc->next_seqno, GFP_KERNEL); 2125 if (err < 0) { 2126 kfree(wait); 2127 return err; 2128 } 2129 2130 return 0; 2131 } 2132 2133 static void fini_tlb_lookup(struct intel_guc *guc) 2134 { 2135 struct intel_guc_tlb_wait *wait; 2136 2137 if (!HAS_GUC_TLB_INVALIDATION(guc_to_gt(guc)->i915)) 2138 return; 2139 2140 wait = xa_load(&guc->tlb_lookup, guc->serial_slot); 2141 if (wait && wait->busy) 2142 guc_err(guc, "Unexpected busy item in tlb_lookup on fini\n"); 2143 kfree(wait); 2144 2145 xa_destroy(&guc->tlb_lookup); 2146 } 2147 2148 /* 2149 * Set up the memory resources to be shared with the GuC (via the GGTT) 2150 * at firmware loading time. 2151 */ 2152 int intel_guc_submission_init(struct intel_guc *guc) 2153 { 2154 struct intel_gt *gt = guc_to_gt(guc); 2155 int ret; 2156 2157 if (guc->submission_initialized) 2158 return 0; 2159 2160 if (GUC_SUBMIT_VER(guc) < MAKE_GUC_VER(1, 0, 0)) { 2161 ret = guc_lrc_desc_pool_create_v69(guc); 2162 if (ret) 2163 return ret; 2164 } 2165 2166 ret = init_tlb_lookup(guc); 2167 if (ret) 2168 goto destroy_pool; 2169 2170 guc->submission_state.guc_ids_bitmap = 2171 bitmap_zalloc(NUMBER_MULTI_LRC_GUC_ID(guc), GFP_KERNEL); 2172 if (!guc->submission_state.guc_ids_bitmap) { 2173 ret = -ENOMEM; 2174 goto destroy_tlb; 2175 } 2176 2177 guc->timestamp.ping_delay = (POLL_TIME_CLKS / gt->clock_frequency + 1) * HZ; 2178 guc->timestamp.shift = gpm_timestamp_shift(gt); 2179 guc->submission_initialized = true; 2180 2181 return 0; 2182 2183 destroy_tlb: 2184 fini_tlb_lookup(guc); 2185 destroy_pool: 2186 guc_lrc_desc_pool_destroy_v69(guc); 2187 return ret; 2188 } 2189 2190 void intel_guc_submission_fini(struct intel_guc *guc) 2191 { 2192 if (!guc->submission_initialized) 2193 return; 2194 2195 guc_fini_engine_stats(guc); 2196 guc_flush_destroyed_contexts(guc); 2197 guc_lrc_desc_pool_destroy_v69(guc); 2198 i915_sched_engine_put(guc->sched_engine); 2199 bitmap_free(guc->submission_state.guc_ids_bitmap); 2200 fini_tlb_lookup(guc); 2201 guc->submission_initialized = false; 2202 } 2203 2204 static inline void queue_request(struct i915_sched_engine *sched_engine, 2205 struct i915_request *rq, 2206 int prio) 2207 { 2208 GEM_BUG_ON(!list_empty(&rq->sched.link)); 2209 list_add_tail(&rq->sched.link, 2210 i915_sched_lookup_priolist(sched_engine, prio)); 2211 set_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags); 2212 tasklet_hi_schedule(&sched_engine->tasklet); 2213 } 2214 2215 static int guc_bypass_tasklet_submit(struct intel_guc *guc, 2216 struct i915_request *rq) 2217 { 2218 int ret = 0; 2219 2220 __i915_request_submit(rq); 2221 2222 trace_i915_request_in(rq, 0); 2223 2224 if (is_multi_lrc_rq(rq)) { 2225 if (multi_lrc_submit(rq)) { 2226 ret = guc_wq_item_append(guc, rq); 2227 if (!ret) 2228 ret = guc_add_request(guc, rq); 2229 } 2230 } else { 2231 guc_set_lrc_tail(rq); 2232 ret = guc_add_request(guc, rq); 2233 } 2234 2235 if (unlikely(ret == -EPIPE)) 2236 disable_submission(guc); 2237 2238 return ret; 2239 } 2240 2241 static bool need_tasklet(struct intel_guc *guc, struct i915_request *rq) 2242 { 2243 struct i915_sched_engine *sched_engine = rq->engine->sched_engine; 2244 struct intel_context *ce = request_to_scheduling_context(rq); 2245 2246 return submission_disabled(guc) || guc->stalled_request || 2247 !i915_sched_engine_is_empty(sched_engine) || 2248 !ctx_id_mapped(guc, ce->guc_id.id); 2249 } 2250 2251 static void guc_submit_request(struct i915_request *rq) 2252 { 2253 struct i915_sched_engine *sched_engine = rq->engine->sched_engine; 2254 struct intel_guc *guc = gt_to_guc(rq->engine->gt); 2255 unsigned long flags; 2256 2257 /* Will be called from irq-context when using foreign fences. */ 2258 spin_lock_irqsave(&sched_engine->lock, flags); 2259 2260 if (need_tasklet(guc, rq)) 2261 queue_request(sched_engine, rq, rq_prio(rq)); 2262 else if (guc_bypass_tasklet_submit(guc, rq) == -EBUSY) 2263 tasklet_hi_schedule(&sched_engine->tasklet); 2264 2265 spin_unlock_irqrestore(&sched_engine->lock, flags); 2266 } 2267 2268 static int new_guc_id(struct intel_guc *guc, struct intel_context *ce) 2269 { 2270 int ret; 2271 2272 GEM_BUG_ON(intel_context_is_child(ce)); 2273 2274 if (intel_context_is_parent(ce)) 2275 ret = bitmap_find_free_region(guc->submission_state.guc_ids_bitmap, 2276 NUMBER_MULTI_LRC_GUC_ID(guc), 2277 order_base_2(ce->parallel.number_children 2278 + 1)); 2279 else 2280 ret = ida_alloc_range(&guc->submission_state.guc_ids, 2281 NUMBER_MULTI_LRC_GUC_ID(guc), 2282 guc->submission_state.num_guc_ids - 1, 2283 GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN); 2284 if (unlikely(ret < 0)) 2285 return ret; 2286 2287 if (!intel_context_is_parent(ce)) 2288 ++guc->submission_state.guc_ids_in_use; 2289 2290 ce->guc_id.id = ret; 2291 return 0; 2292 } 2293 2294 static void __release_guc_id(struct intel_guc *guc, struct intel_context *ce) 2295 { 2296 GEM_BUG_ON(intel_context_is_child(ce)); 2297 2298 if (!context_guc_id_invalid(ce)) { 2299 if (intel_context_is_parent(ce)) { 2300 bitmap_release_region(guc->submission_state.guc_ids_bitmap, 2301 ce->guc_id.id, 2302 order_base_2(ce->parallel.number_children 2303 + 1)); 2304 } else { 2305 --guc->submission_state.guc_ids_in_use; 2306 ida_free(&guc->submission_state.guc_ids, 2307 ce->guc_id.id); 2308 } 2309 clr_ctx_id_mapping(guc, ce->guc_id.id); 2310 set_context_guc_id_invalid(ce); 2311 } 2312 if (!list_empty(&ce->guc_id.link)) 2313 list_del_init(&ce->guc_id.link); 2314 } 2315 2316 static void release_guc_id(struct intel_guc *guc, struct intel_context *ce) 2317 { 2318 unsigned long flags; 2319 2320 spin_lock_irqsave(&guc->submission_state.lock, flags); 2321 __release_guc_id(guc, ce); 2322 spin_unlock_irqrestore(&guc->submission_state.lock, flags); 2323 } 2324 2325 static int steal_guc_id(struct intel_guc *guc, struct intel_context *ce) 2326 { 2327 struct intel_context *cn; 2328 2329 lockdep_assert_held(&guc->submission_state.lock); 2330 GEM_BUG_ON(intel_context_is_child(ce)); 2331 GEM_BUG_ON(intel_context_is_parent(ce)); 2332 2333 if (!list_empty(&guc->submission_state.guc_id_list)) { 2334 cn = list_first_entry(&guc->submission_state.guc_id_list, 2335 struct intel_context, 2336 guc_id.link); 2337 2338 GEM_BUG_ON(atomic_read(&cn->guc_id.ref)); 2339 GEM_BUG_ON(context_guc_id_invalid(cn)); 2340 GEM_BUG_ON(intel_context_is_child(cn)); 2341 GEM_BUG_ON(intel_context_is_parent(cn)); 2342 2343 list_del_init(&cn->guc_id.link); 2344 ce->guc_id.id = cn->guc_id.id; 2345 2346 spin_lock(&cn->guc_state.lock); 2347 clr_context_registered(cn); 2348 spin_unlock(&cn->guc_state.lock); 2349 2350 set_context_guc_id_invalid(cn); 2351 2352 #ifdef CONFIG_DRM_I915_SELFTEST 2353 guc->number_guc_id_stolen++; 2354 #endif 2355 2356 return 0; 2357 } else { 2358 return -EAGAIN; 2359 } 2360 } 2361 2362 static int assign_guc_id(struct intel_guc *guc, struct intel_context *ce) 2363 { 2364 int ret; 2365 2366 lockdep_assert_held(&guc->submission_state.lock); 2367 GEM_BUG_ON(intel_context_is_child(ce)); 2368 2369 ret = new_guc_id(guc, ce); 2370 if (unlikely(ret < 0)) { 2371 if (intel_context_is_parent(ce)) 2372 return -ENOSPC; 2373 2374 ret = steal_guc_id(guc, ce); 2375 if (ret < 0) 2376 return ret; 2377 } 2378 2379 if (intel_context_is_parent(ce)) { 2380 struct intel_context *child; 2381 int i = 1; 2382 2383 for_each_child(ce, child) 2384 child->guc_id.id = ce->guc_id.id + i++; 2385 } 2386 2387 return 0; 2388 } 2389 2390 #define PIN_GUC_ID_TRIES 4 2391 static int pin_guc_id(struct intel_guc *guc, struct intel_context *ce) 2392 { 2393 int ret = 0; 2394 unsigned long flags, tries = PIN_GUC_ID_TRIES; 2395 2396 GEM_BUG_ON(atomic_read(&ce->guc_id.ref)); 2397 2398 try_again: 2399 spin_lock_irqsave(&guc->submission_state.lock, flags); 2400 2401 might_lock(&ce->guc_state.lock); 2402 2403 if (context_guc_id_invalid(ce)) { 2404 ret = assign_guc_id(guc, ce); 2405 if (ret) 2406 goto out_unlock; 2407 ret = 1; /* Indidcates newly assigned guc_id */ 2408 } 2409 if (!list_empty(&ce->guc_id.link)) 2410 list_del_init(&ce->guc_id.link); 2411 atomic_inc(&ce->guc_id.ref); 2412 2413 out_unlock: 2414 spin_unlock_irqrestore(&guc->submission_state.lock, flags); 2415 2416 /* 2417 * -EAGAIN indicates no guc_id are available, let's retire any 2418 * outstanding requests to see if that frees up a guc_id. If the first 2419 * retire didn't help, insert a sleep with the timeslice duration before 2420 * attempting to retire more requests. Double the sleep period each 2421 * subsequent pass before finally giving up. The sleep period has max of 2422 * 100ms and minimum of 1ms. 2423 */ 2424 if (ret == -EAGAIN && --tries) { 2425 if (PIN_GUC_ID_TRIES - tries > 1) { 2426 unsigned int timeslice_shifted = 2427 ce->engine->props.timeslice_duration_ms << 2428 (PIN_GUC_ID_TRIES - tries - 2); 2429 unsigned int max = min_t(unsigned int, 100, 2430 timeslice_shifted); 2431 2432 msleep(max_t(unsigned int, max, 1)); 2433 } 2434 intel_gt_retire_requests(guc_to_gt(guc)); 2435 goto try_again; 2436 } 2437 2438 return ret; 2439 } 2440 2441 static void unpin_guc_id(struct intel_guc *guc, struct intel_context *ce) 2442 { 2443 unsigned long flags; 2444 2445 GEM_BUG_ON(atomic_read(&ce->guc_id.ref) < 0); 2446 GEM_BUG_ON(intel_context_is_child(ce)); 2447 2448 if (unlikely(context_guc_id_invalid(ce) || 2449 intel_context_is_parent(ce))) 2450 return; 2451 2452 spin_lock_irqsave(&guc->submission_state.lock, flags); 2453 if (!context_guc_id_invalid(ce) && list_empty(&ce->guc_id.link) && 2454 !atomic_read(&ce->guc_id.ref)) 2455 list_add_tail(&ce->guc_id.link, 2456 &guc->submission_state.guc_id_list); 2457 spin_unlock_irqrestore(&guc->submission_state.lock, flags); 2458 } 2459 2460 static int __guc_action_register_multi_lrc_v69(struct intel_guc *guc, 2461 struct intel_context *ce, 2462 u32 guc_id, 2463 u32 offset, 2464 bool loop) 2465 { 2466 struct intel_context *child; 2467 u32 action[4 + MAX_ENGINE_INSTANCE]; 2468 int len = 0; 2469 2470 GEM_BUG_ON(ce->parallel.number_children > MAX_ENGINE_INSTANCE); 2471 2472 action[len++] = INTEL_GUC_ACTION_REGISTER_CONTEXT_MULTI_LRC; 2473 action[len++] = guc_id; 2474 action[len++] = ce->parallel.number_children + 1; 2475 action[len++] = offset; 2476 for_each_child(ce, child) { 2477 offset += sizeof(struct guc_lrc_desc_v69); 2478 action[len++] = offset; 2479 } 2480 2481 return guc_submission_send_busy_loop(guc, action, len, 0, loop); 2482 } 2483 2484 static int __guc_action_register_multi_lrc_v70(struct intel_guc *guc, 2485 struct intel_context *ce, 2486 struct guc_ctxt_registration_info *info, 2487 bool loop) 2488 { 2489 struct intel_context *child; 2490 u32 action[13 + (MAX_ENGINE_INSTANCE * 2)]; 2491 int len = 0; 2492 u32 next_id; 2493 2494 GEM_BUG_ON(ce->parallel.number_children > MAX_ENGINE_INSTANCE); 2495 2496 action[len++] = INTEL_GUC_ACTION_REGISTER_CONTEXT_MULTI_LRC; 2497 action[len++] = info->flags; 2498 action[len++] = info->context_idx; 2499 action[len++] = info->engine_class; 2500 action[len++] = info->engine_submit_mask; 2501 action[len++] = info->wq_desc_lo; 2502 action[len++] = info->wq_desc_hi; 2503 action[len++] = info->wq_base_lo; 2504 action[len++] = info->wq_base_hi; 2505 action[len++] = info->wq_size; 2506 action[len++] = ce->parallel.number_children + 1; 2507 action[len++] = info->hwlrca_lo; 2508 action[len++] = info->hwlrca_hi; 2509 2510 next_id = info->context_idx + 1; 2511 for_each_child(ce, child) { 2512 GEM_BUG_ON(next_id++ != child->guc_id.id); 2513 2514 /* 2515 * NB: GuC interface supports 64 bit LRCA even though i915/HW 2516 * only supports 32 bit currently. 2517 */ 2518 action[len++] = lower_32_bits(child->lrc.lrca); 2519 action[len++] = upper_32_bits(child->lrc.lrca); 2520 } 2521 2522 GEM_BUG_ON(len > ARRAY_SIZE(action)); 2523 2524 return guc_submission_send_busy_loop(guc, action, len, 0, loop); 2525 } 2526 2527 static int __guc_action_register_context_v69(struct intel_guc *guc, 2528 u32 guc_id, 2529 u32 offset, 2530 bool loop) 2531 { 2532 u32 action[] = { 2533 INTEL_GUC_ACTION_REGISTER_CONTEXT, 2534 guc_id, 2535 offset, 2536 }; 2537 2538 return guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action), 2539 0, loop); 2540 } 2541 2542 static int __guc_action_register_context_v70(struct intel_guc *guc, 2543 struct guc_ctxt_registration_info *info, 2544 bool loop) 2545 { 2546 u32 action[] = { 2547 INTEL_GUC_ACTION_REGISTER_CONTEXT, 2548 info->flags, 2549 info->context_idx, 2550 info->engine_class, 2551 info->engine_submit_mask, 2552 info->wq_desc_lo, 2553 info->wq_desc_hi, 2554 info->wq_base_lo, 2555 info->wq_base_hi, 2556 info->wq_size, 2557 info->hwlrca_lo, 2558 info->hwlrca_hi, 2559 }; 2560 2561 return guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action), 2562 0, loop); 2563 } 2564 2565 static void prepare_context_registration_info_v69(struct intel_context *ce); 2566 static void prepare_context_registration_info_v70(struct intel_context *ce, 2567 struct guc_ctxt_registration_info *info); 2568 2569 static int 2570 register_context_v69(struct intel_guc *guc, struct intel_context *ce, bool loop) 2571 { 2572 u32 offset = intel_guc_ggtt_offset(guc, guc->lrc_desc_pool_v69) + 2573 ce->guc_id.id * sizeof(struct guc_lrc_desc_v69); 2574 2575 prepare_context_registration_info_v69(ce); 2576 2577 if (intel_context_is_parent(ce)) 2578 return __guc_action_register_multi_lrc_v69(guc, ce, ce->guc_id.id, 2579 offset, loop); 2580 else 2581 return __guc_action_register_context_v69(guc, ce->guc_id.id, 2582 offset, loop); 2583 } 2584 2585 static int 2586 register_context_v70(struct intel_guc *guc, struct intel_context *ce, bool loop) 2587 { 2588 struct guc_ctxt_registration_info info; 2589 2590 prepare_context_registration_info_v70(ce, &info); 2591 2592 if (intel_context_is_parent(ce)) 2593 return __guc_action_register_multi_lrc_v70(guc, ce, &info, loop); 2594 else 2595 return __guc_action_register_context_v70(guc, &info, loop); 2596 } 2597 2598 static int register_context(struct intel_context *ce, bool loop) 2599 { 2600 struct intel_guc *guc = ce_to_guc(ce); 2601 int ret; 2602 2603 GEM_BUG_ON(intel_context_is_child(ce)); 2604 trace_intel_context_register(ce); 2605 2606 if (GUC_SUBMIT_VER(guc) >= MAKE_GUC_VER(1, 0, 0)) 2607 ret = register_context_v70(guc, ce, loop); 2608 else 2609 ret = register_context_v69(guc, ce, loop); 2610 2611 if (likely(!ret)) { 2612 unsigned long flags; 2613 2614 spin_lock_irqsave(&ce->guc_state.lock, flags); 2615 set_context_registered(ce); 2616 spin_unlock_irqrestore(&ce->guc_state.lock, flags); 2617 2618 if (GUC_SUBMIT_VER(guc) >= MAKE_GUC_VER(1, 0, 0)) 2619 guc_context_policy_init_v70(ce, loop); 2620 } 2621 2622 return ret; 2623 } 2624 2625 static int __guc_action_deregister_context(struct intel_guc *guc, 2626 u32 guc_id) 2627 { 2628 u32 action[] = { 2629 INTEL_GUC_ACTION_DEREGISTER_CONTEXT, 2630 guc_id, 2631 }; 2632 2633 return guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action), 2634 G2H_LEN_DW_DEREGISTER_CONTEXT, 2635 true); 2636 } 2637 2638 static int deregister_context(struct intel_context *ce, u32 guc_id) 2639 { 2640 struct intel_guc *guc = ce_to_guc(ce); 2641 2642 GEM_BUG_ON(intel_context_is_child(ce)); 2643 trace_intel_context_deregister(ce); 2644 2645 return __guc_action_deregister_context(guc, guc_id); 2646 } 2647 2648 static inline void clear_children_join_go_memory(struct intel_context *ce) 2649 { 2650 struct parent_scratch *ps = __get_parent_scratch(ce); 2651 int i; 2652 2653 ps->go.semaphore = 0; 2654 for (i = 0; i < ce->parallel.number_children + 1; ++i) 2655 ps->join[i].semaphore = 0; 2656 } 2657 2658 static inline u32 get_children_go_value(struct intel_context *ce) 2659 { 2660 return __get_parent_scratch(ce)->go.semaphore; 2661 } 2662 2663 static inline u32 get_children_join_value(struct intel_context *ce, 2664 u8 child_index) 2665 { 2666 return __get_parent_scratch(ce)->join[child_index].semaphore; 2667 } 2668 2669 struct context_policy { 2670 u32 count; 2671 struct guc_update_context_policy h2g; 2672 }; 2673 2674 static u32 __guc_context_policy_action_size(struct context_policy *policy) 2675 { 2676 size_t bytes = sizeof(policy->h2g.header) + 2677 (sizeof(policy->h2g.klv[0]) * policy->count); 2678 2679 return bytes / sizeof(u32); 2680 } 2681 2682 static void __guc_context_policy_start_klv(struct context_policy *policy, u16 guc_id) 2683 { 2684 policy->h2g.header.action = INTEL_GUC_ACTION_HOST2GUC_UPDATE_CONTEXT_POLICIES; 2685 policy->h2g.header.ctx_id = guc_id; 2686 policy->count = 0; 2687 } 2688 2689 #define MAKE_CONTEXT_POLICY_ADD(func, id) \ 2690 static void __guc_context_policy_add_##func(struct context_policy *policy, u32 data) \ 2691 { \ 2692 GEM_BUG_ON(policy->count >= GUC_CONTEXT_POLICIES_KLV_NUM_IDS); \ 2693 policy->h2g.klv[policy->count].kl = \ 2694 FIELD_PREP(GUC_KLV_0_KEY, GUC_CONTEXT_POLICIES_KLV_ID_##id) | \ 2695 FIELD_PREP(GUC_KLV_0_LEN, 1); \ 2696 policy->h2g.klv[policy->count].value = data; \ 2697 policy->count++; \ 2698 } 2699 2700 MAKE_CONTEXT_POLICY_ADD(execution_quantum, EXECUTION_QUANTUM) 2701 MAKE_CONTEXT_POLICY_ADD(preemption_timeout, PREEMPTION_TIMEOUT) 2702 MAKE_CONTEXT_POLICY_ADD(priority, SCHEDULING_PRIORITY) 2703 MAKE_CONTEXT_POLICY_ADD(preempt_to_idle, PREEMPT_TO_IDLE_ON_QUANTUM_EXPIRY) 2704 MAKE_CONTEXT_POLICY_ADD(slpc_ctx_freq_req, SLPM_GT_FREQUENCY) 2705 2706 #undef MAKE_CONTEXT_POLICY_ADD 2707 2708 static int __guc_context_set_context_policies(struct intel_guc *guc, 2709 struct context_policy *policy, 2710 bool loop) 2711 { 2712 return guc_submission_send_busy_loop(guc, (u32 *)&policy->h2g, 2713 __guc_context_policy_action_size(policy), 2714 0, loop); 2715 } 2716 2717 static int guc_context_policy_init_v70(struct intel_context *ce, bool loop) 2718 { 2719 struct intel_engine_cs *engine = ce->engine; 2720 struct intel_guc *guc = gt_to_guc(engine->gt); 2721 struct context_policy policy; 2722 u32 execution_quantum; 2723 u32 preemption_timeout; 2724 u32 slpc_ctx_freq_req = 0; 2725 unsigned long flags; 2726 int ret; 2727 2728 /* NB: For both of these, zero means disabled. */ 2729 GEM_BUG_ON(overflows_type(engine->props.timeslice_duration_ms * 1000, 2730 execution_quantum)); 2731 GEM_BUG_ON(overflows_type(engine->props.preempt_timeout_ms * 1000, 2732 preemption_timeout)); 2733 execution_quantum = engine->props.timeslice_duration_ms * 1000; 2734 preemption_timeout = engine->props.preempt_timeout_ms * 1000; 2735 2736 if (ce->flags & BIT(CONTEXT_LOW_LATENCY)) 2737 slpc_ctx_freq_req |= SLPC_CTX_FREQ_REQ_IS_COMPUTE; 2738 2739 __guc_context_policy_start_klv(&policy, ce->guc_id.id); 2740 2741 __guc_context_policy_add_priority(&policy, ce->guc_state.prio); 2742 __guc_context_policy_add_execution_quantum(&policy, execution_quantum); 2743 __guc_context_policy_add_preemption_timeout(&policy, preemption_timeout); 2744 __guc_context_policy_add_slpc_ctx_freq_req(&policy, slpc_ctx_freq_req); 2745 2746 if (engine->flags & I915_ENGINE_WANT_FORCED_PREEMPTION) 2747 __guc_context_policy_add_preempt_to_idle(&policy, 1); 2748 2749 ret = __guc_context_set_context_policies(guc, &policy, loop); 2750 2751 spin_lock_irqsave(&ce->guc_state.lock, flags); 2752 if (ret != 0) 2753 set_context_policy_required(ce); 2754 else 2755 clr_context_policy_required(ce); 2756 spin_unlock_irqrestore(&ce->guc_state.lock, flags); 2757 2758 return ret; 2759 } 2760 2761 static void guc_context_policy_init_v69(struct intel_engine_cs *engine, 2762 struct guc_lrc_desc_v69 *desc) 2763 { 2764 desc->policy_flags = 0; 2765 2766 if (engine->flags & I915_ENGINE_WANT_FORCED_PREEMPTION) 2767 desc->policy_flags |= CONTEXT_POLICY_FLAG_PREEMPT_TO_IDLE_V69; 2768 2769 /* NB: For both of these, zero means disabled. */ 2770 GEM_BUG_ON(overflows_type(engine->props.timeslice_duration_ms * 1000, 2771 desc->execution_quantum)); 2772 GEM_BUG_ON(overflows_type(engine->props.preempt_timeout_ms * 1000, 2773 desc->preemption_timeout)); 2774 desc->execution_quantum = engine->props.timeslice_duration_ms * 1000; 2775 desc->preemption_timeout = engine->props.preempt_timeout_ms * 1000; 2776 } 2777 2778 static u32 map_guc_prio_to_lrc_desc_prio(u8 prio) 2779 { 2780 /* 2781 * this matches the mapping we do in map_i915_prio_to_guc_prio() 2782 * (e.g. prio < I915_PRIORITY_NORMAL maps to GUC_CLIENT_PRIORITY_NORMAL) 2783 */ 2784 switch (prio) { 2785 default: 2786 MISSING_CASE(prio); 2787 fallthrough; 2788 case GUC_CLIENT_PRIORITY_KMD_NORMAL: 2789 return GEN12_CTX_PRIORITY_NORMAL; 2790 case GUC_CLIENT_PRIORITY_NORMAL: 2791 return GEN12_CTX_PRIORITY_LOW; 2792 case GUC_CLIENT_PRIORITY_HIGH: 2793 case GUC_CLIENT_PRIORITY_KMD_HIGH: 2794 return GEN12_CTX_PRIORITY_HIGH; 2795 } 2796 } 2797 2798 static void prepare_context_registration_info_v69(struct intel_context *ce) 2799 { 2800 struct intel_engine_cs *engine = ce->engine; 2801 struct intel_guc *guc = gt_to_guc(engine->gt); 2802 u32 ctx_id = ce->guc_id.id; 2803 struct guc_lrc_desc_v69 *desc; 2804 struct intel_context *child; 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 desc = __get_lrc_desc_v69(guc, ctx_id); 2816 GEM_BUG_ON(!desc); 2817 desc->engine_class = engine_class_to_guc_class(engine->class); 2818 desc->engine_submit_mask = engine->logical_mask; 2819 desc->hw_context_desc = ce->lrc.lrca; 2820 desc->priority = ce->guc_state.prio; 2821 desc->context_flags = CONTEXT_REGISTRATION_FLAG_KMD; 2822 guc_context_policy_init_v69(engine, desc); 2823 2824 /* 2825 * If context is a parent, we need to register a process descriptor 2826 * describing a work queue and register all child contexts. 2827 */ 2828 if (intel_context_is_parent(ce)) { 2829 struct guc_process_desc_v69 *pdesc; 2830 2831 ce->parallel.guc.wqi_tail = 0; 2832 ce->parallel.guc.wqi_head = 0; 2833 2834 desc->process_desc = i915_ggtt_offset(ce->state) + 2835 __get_parent_scratch_offset(ce); 2836 desc->wq_addr = i915_ggtt_offset(ce->state) + 2837 __get_wq_offset(ce); 2838 desc->wq_size = WQ_SIZE; 2839 2840 pdesc = __get_process_desc_v69(ce); 2841 memset(pdesc, 0, sizeof(*(pdesc))); 2842 pdesc->stage_id = ce->guc_id.id; 2843 pdesc->wq_base_addr = desc->wq_addr; 2844 pdesc->wq_size_bytes = desc->wq_size; 2845 pdesc->wq_status = WQ_STATUS_ACTIVE; 2846 2847 ce->parallel.guc.wq_head = &pdesc->head; 2848 ce->parallel.guc.wq_tail = &pdesc->tail; 2849 ce->parallel.guc.wq_status = &pdesc->wq_status; 2850 2851 for_each_child(ce, child) { 2852 desc = __get_lrc_desc_v69(guc, child->guc_id.id); 2853 2854 desc->engine_class = 2855 engine_class_to_guc_class(engine->class); 2856 desc->hw_context_desc = child->lrc.lrca; 2857 desc->priority = ce->guc_state.prio; 2858 desc->context_flags = CONTEXT_REGISTRATION_FLAG_KMD; 2859 guc_context_policy_init_v69(engine, desc); 2860 } 2861 2862 clear_children_join_go_memory(ce); 2863 } 2864 } 2865 2866 static void prepare_context_registration_info_v70(struct intel_context *ce, 2867 struct guc_ctxt_registration_info *info) 2868 { 2869 struct intel_engine_cs *engine = ce->engine; 2870 struct intel_guc *guc = gt_to_guc(engine->gt); 2871 u32 ctx_id = ce->guc_id.id; 2872 2873 GEM_BUG_ON(!engine->mask); 2874 2875 /* 2876 * Ensure LRC + CT vmas are is same region as write barrier is done 2877 * based on CT vma region. 2878 */ 2879 GEM_BUG_ON(i915_gem_object_is_lmem(guc->ct.vma->obj) != 2880 i915_gem_object_is_lmem(ce->ring->vma->obj)); 2881 2882 memset(info, 0, sizeof(*info)); 2883 info->context_idx = ctx_id; 2884 info->engine_class = engine_class_to_guc_class(engine->class); 2885 info->engine_submit_mask = engine->logical_mask; 2886 /* 2887 * NB: GuC interface supports 64 bit LRCA even though i915/HW 2888 * only supports 32 bit currently. 2889 */ 2890 info->hwlrca_lo = lower_32_bits(ce->lrc.lrca); 2891 info->hwlrca_hi = upper_32_bits(ce->lrc.lrca); 2892 if (engine->flags & I915_ENGINE_HAS_EU_PRIORITY) 2893 info->hwlrca_lo |= map_guc_prio_to_lrc_desc_prio(ce->guc_state.prio); 2894 info->flags = CONTEXT_REGISTRATION_FLAG_KMD; 2895 2896 /* 2897 * If context is a parent, we need to register a process descriptor 2898 * describing a work queue and register all child contexts. 2899 */ 2900 if (intel_context_is_parent(ce)) { 2901 struct guc_sched_wq_desc *wq_desc; 2902 u64 wq_desc_offset, wq_base_offset; 2903 2904 ce->parallel.guc.wqi_tail = 0; 2905 ce->parallel.guc.wqi_head = 0; 2906 2907 wq_desc_offset = (u64)i915_ggtt_offset(ce->state) + 2908 __get_parent_scratch_offset(ce); 2909 wq_base_offset = (u64)i915_ggtt_offset(ce->state) + 2910 __get_wq_offset(ce); 2911 info->wq_desc_lo = lower_32_bits(wq_desc_offset); 2912 info->wq_desc_hi = upper_32_bits(wq_desc_offset); 2913 info->wq_base_lo = lower_32_bits(wq_base_offset); 2914 info->wq_base_hi = upper_32_bits(wq_base_offset); 2915 info->wq_size = WQ_SIZE; 2916 2917 wq_desc = __get_wq_desc_v70(ce); 2918 memset(wq_desc, 0, sizeof(*wq_desc)); 2919 wq_desc->wq_status = WQ_STATUS_ACTIVE; 2920 2921 ce->parallel.guc.wq_head = &wq_desc->head; 2922 ce->parallel.guc.wq_tail = &wq_desc->tail; 2923 ce->parallel.guc.wq_status = &wq_desc->wq_status; 2924 2925 clear_children_join_go_memory(ce); 2926 } 2927 } 2928 2929 static int try_context_registration(struct intel_context *ce, bool loop) 2930 { 2931 struct intel_engine_cs *engine = ce->engine; 2932 struct intel_runtime_pm *runtime_pm = engine->uncore->rpm; 2933 struct intel_guc *guc = gt_to_guc(engine->gt); 2934 intel_wakeref_t wakeref; 2935 u32 ctx_id = ce->guc_id.id; 2936 bool context_registered; 2937 int ret = 0; 2938 2939 GEM_BUG_ON(!sched_state_is_init(ce)); 2940 2941 context_registered = ctx_id_mapped(guc, ctx_id); 2942 2943 clr_ctx_id_mapping(guc, ctx_id); 2944 set_ctx_id_mapping(guc, ctx_id, ce); 2945 2946 /* 2947 * The context_lookup xarray is used to determine if the hardware 2948 * context is currently registered. There are two cases in which it 2949 * could be registered either the guc_id has been stolen from another 2950 * context or the lrc descriptor address of this context has changed. In 2951 * either case the context needs to be deregistered with the GuC before 2952 * registering this context. 2953 */ 2954 if (context_registered) { 2955 bool disabled; 2956 unsigned long flags; 2957 2958 trace_intel_context_steal_guc_id(ce); 2959 GEM_BUG_ON(!loop); 2960 2961 /* Seal race with Reset */ 2962 spin_lock_irqsave(&ce->guc_state.lock, flags); 2963 disabled = submission_disabled(guc); 2964 if (likely(!disabled)) { 2965 set_context_wait_for_deregister_to_register(ce); 2966 intel_context_get(ce); 2967 } 2968 spin_unlock_irqrestore(&ce->guc_state.lock, flags); 2969 if (unlikely(disabled)) { 2970 clr_ctx_id_mapping(guc, ctx_id); 2971 return 0; /* Will get registered later */ 2972 } 2973 2974 /* 2975 * If stealing the guc_id, this ce has the same guc_id as the 2976 * context whose guc_id was stolen. 2977 */ 2978 with_intel_runtime_pm(runtime_pm, wakeref) 2979 ret = deregister_context(ce, ce->guc_id.id); 2980 if (unlikely(ret == -ENODEV)) 2981 ret = 0; /* Will get registered later */ 2982 } else { 2983 with_intel_runtime_pm(runtime_pm, wakeref) 2984 ret = register_context(ce, loop); 2985 if (unlikely(ret == -EBUSY)) { 2986 clr_ctx_id_mapping(guc, ctx_id); 2987 } else if (unlikely(ret == -ENODEV)) { 2988 clr_ctx_id_mapping(guc, ctx_id); 2989 ret = 0; /* Will get registered later */ 2990 } 2991 } 2992 2993 return ret; 2994 } 2995 2996 static int __guc_context_pre_pin(struct intel_context *ce, 2997 struct intel_engine_cs *engine, 2998 struct i915_gem_ww_ctx *ww, 2999 void **vaddr) 3000 { 3001 return lrc_pre_pin(ce, engine, ww, vaddr); 3002 } 3003 3004 static int __guc_context_pin(struct intel_context *ce, 3005 struct intel_engine_cs *engine, 3006 void *vaddr) 3007 { 3008 if (i915_ggtt_offset(ce->state) != 3009 (ce->lrc.lrca & CTX_GTT_ADDRESS_MASK)) 3010 set_bit(CONTEXT_LRCA_DIRTY, &ce->flags); 3011 3012 /* 3013 * GuC context gets pinned in guc_request_alloc. See that function for 3014 * explanation of why. 3015 */ 3016 3017 return lrc_pin(ce, engine, vaddr); 3018 } 3019 3020 static int guc_context_pre_pin(struct intel_context *ce, 3021 struct i915_gem_ww_ctx *ww, 3022 void **vaddr) 3023 { 3024 return __guc_context_pre_pin(ce, ce->engine, ww, vaddr); 3025 } 3026 3027 static int guc_context_pin(struct intel_context *ce, void *vaddr) 3028 { 3029 int ret = __guc_context_pin(ce, ce->engine, vaddr); 3030 3031 if (likely(!ret && !intel_context_is_barrier(ce))) 3032 intel_engine_pm_get(ce->engine); 3033 3034 return ret; 3035 } 3036 3037 static void guc_context_unpin(struct intel_context *ce) 3038 { 3039 struct intel_guc *guc = ce_to_guc(ce); 3040 3041 __guc_context_update_stats(ce); 3042 unpin_guc_id(guc, ce); 3043 lrc_unpin(ce); 3044 3045 if (likely(!intel_context_is_barrier(ce))) 3046 intel_engine_pm_put_async(ce->engine); 3047 } 3048 3049 static void guc_context_post_unpin(struct intel_context *ce) 3050 { 3051 lrc_post_unpin(ce); 3052 } 3053 3054 static void __guc_context_sched_enable(struct intel_guc *guc, 3055 struct intel_context *ce) 3056 { 3057 u32 action[] = { 3058 INTEL_GUC_ACTION_SCHED_CONTEXT_MODE_SET, 3059 ce->guc_id.id, 3060 GUC_CONTEXT_ENABLE 3061 }; 3062 3063 trace_intel_context_sched_enable(ce); 3064 3065 guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action), 3066 G2H_LEN_DW_SCHED_CONTEXT_MODE_SET, true); 3067 } 3068 3069 static void __guc_context_sched_disable(struct intel_guc *guc, 3070 struct intel_context *ce, 3071 u16 guc_id) 3072 { 3073 u32 action[] = { 3074 INTEL_GUC_ACTION_SCHED_CONTEXT_MODE_SET, 3075 guc_id, /* ce->guc_id.id not stable */ 3076 GUC_CONTEXT_DISABLE 3077 }; 3078 3079 GEM_BUG_ON(guc_id == GUC_INVALID_CONTEXT_ID); 3080 3081 GEM_BUG_ON(intel_context_is_child(ce)); 3082 trace_intel_context_sched_disable(ce); 3083 3084 guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action), 3085 G2H_LEN_DW_SCHED_CONTEXT_MODE_SET, true); 3086 } 3087 3088 static void guc_blocked_fence_complete(struct intel_context *ce) 3089 { 3090 lockdep_assert_held(&ce->guc_state.lock); 3091 3092 if (!i915_sw_fence_done(&ce->guc_state.blocked)) 3093 i915_sw_fence_complete(&ce->guc_state.blocked); 3094 } 3095 3096 static void guc_blocked_fence_reinit(struct intel_context *ce) 3097 { 3098 lockdep_assert_held(&ce->guc_state.lock); 3099 GEM_BUG_ON(!i915_sw_fence_done(&ce->guc_state.blocked)); 3100 3101 /* 3102 * This fence is always complete unless a pending schedule disable is 3103 * outstanding. We arm the fence here and complete it when we receive 3104 * the pending schedule disable complete message. 3105 */ 3106 i915_sw_fence_fini(&ce->guc_state.blocked); 3107 i915_sw_fence_reinit(&ce->guc_state.blocked); 3108 i915_sw_fence_await(&ce->guc_state.blocked); 3109 i915_sw_fence_commit(&ce->guc_state.blocked); 3110 } 3111 3112 static u16 prep_context_pending_disable(struct intel_context *ce) 3113 { 3114 lockdep_assert_held(&ce->guc_state.lock); 3115 3116 set_context_pending_disable(ce); 3117 clr_context_enabled(ce); 3118 guc_blocked_fence_reinit(ce); 3119 intel_context_get(ce); 3120 3121 return ce->guc_id.id; 3122 } 3123 3124 static struct i915_sw_fence *guc_context_block(struct intel_context *ce) 3125 { 3126 struct intel_guc *guc = ce_to_guc(ce); 3127 unsigned long flags; 3128 struct intel_runtime_pm *runtime_pm = ce->engine->uncore->rpm; 3129 intel_wakeref_t wakeref; 3130 u16 guc_id; 3131 bool enabled; 3132 3133 GEM_BUG_ON(intel_context_is_child(ce)); 3134 3135 spin_lock_irqsave(&ce->guc_state.lock, flags); 3136 3137 incr_context_blocked(ce); 3138 3139 enabled = context_enabled(ce); 3140 if (unlikely(!enabled || submission_disabled(guc))) { 3141 if (enabled) 3142 clr_context_enabled(ce); 3143 spin_unlock_irqrestore(&ce->guc_state.lock, flags); 3144 return &ce->guc_state.blocked; 3145 } 3146 3147 /* 3148 * We add +2 here as the schedule disable complete CTB handler calls 3149 * intel_context_sched_disable_unpin (-2 to pin_count). 3150 */ 3151 atomic_add(2, &ce->pin_count); 3152 3153 guc_id = prep_context_pending_disable(ce); 3154 3155 spin_unlock_irqrestore(&ce->guc_state.lock, flags); 3156 3157 with_intel_runtime_pm(runtime_pm, wakeref) 3158 __guc_context_sched_disable(guc, ce, guc_id); 3159 3160 return &ce->guc_state.blocked; 3161 } 3162 3163 #define SCHED_STATE_MULTI_BLOCKED_MASK \ 3164 (SCHED_STATE_BLOCKED_MASK & ~SCHED_STATE_BLOCKED) 3165 #define SCHED_STATE_NO_UNBLOCK \ 3166 (SCHED_STATE_MULTI_BLOCKED_MASK | \ 3167 SCHED_STATE_PENDING_DISABLE | \ 3168 SCHED_STATE_BANNED) 3169 3170 static bool context_cant_unblock(struct intel_context *ce) 3171 { 3172 lockdep_assert_held(&ce->guc_state.lock); 3173 3174 return (ce->guc_state.sched_state & SCHED_STATE_NO_UNBLOCK) || 3175 context_guc_id_invalid(ce) || 3176 !ctx_id_mapped(ce_to_guc(ce), ce->guc_id.id) || 3177 !intel_context_is_pinned(ce); 3178 } 3179 3180 static void guc_context_unblock(struct intel_context *ce) 3181 { 3182 struct intel_guc *guc = ce_to_guc(ce); 3183 unsigned long flags; 3184 struct intel_runtime_pm *runtime_pm = ce->engine->uncore->rpm; 3185 intel_wakeref_t wakeref; 3186 bool enable; 3187 3188 GEM_BUG_ON(context_enabled(ce)); 3189 GEM_BUG_ON(intel_context_is_child(ce)); 3190 3191 spin_lock_irqsave(&ce->guc_state.lock, flags); 3192 3193 if (unlikely(submission_disabled(guc) || 3194 context_cant_unblock(ce))) { 3195 enable = false; 3196 } else { 3197 enable = true; 3198 set_context_pending_enable(ce); 3199 set_context_enabled(ce); 3200 intel_context_get(ce); 3201 } 3202 3203 decr_context_blocked(ce); 3204 3205 spin_unlock_irqrestore(&ce->guc_state.lock, flags); 3206 3207 if (enable) { 3208 with_intel_runtime_pm(runtime_pm, wakeref) 3209 __guc_context_sched_enable(guc, ce); 3210 } 3211 } 3212 3213 static void guc_context_cancel_request(struct intel_context *ce, 3214 struct i915_request *rq) 3215 { 3216 struct intel_context *block_context = 3217 request_to_scheduling_context(rq); 3218 3219 if (i915_sw_fence_signaled(&rq->submit)) { 3220 struct i915_sw_fence *fence; 3221 3222 intel_context_get(ce); 3223 fence = guc_context_block(block_context); 3224 i915_sw_fence_wait(fence); 3225 if (!i915_request_completed(rq)) { 3226 __i915_request_skip(rq); 3227 guc_reset_state(ce, intel_ring_wrap(ce->ring, rq->head), 3228 true); 3229 } 3230 3231 guc_context_unblock(block_context); 3232 intel_context_put(ce); 3233 } 3234 } 3235 3236 static void __guc_context_set_preemption_timeout(struct intel_guc *guc, 3237 u16 guc_id, 3238 u32 preemption_timeout) 3239 { 3240 if (GUC_SUBMIT_VER(guc) >= MAKE_GUC_VER(1, 0, 0)) { 3241 struct context_policy policy; 3242 3243 __guc_context_policy_start_klv(&policy, guc_id); 3244 __guc_context_policy_add_preemption_timeout(&policy, preemption_timeout); 3245 __guc_context_set_context_policies(guc, &policy, true); 3246 } else { 3247 u32 action[] = { 3248 INTEL_GUC_ACTION_V69_SET_CONTEXT_PREEMPTION_TIMEOUT, 3249 guc_id, 3250 preemption_timeout 3251 }; 3252 3253 intel_guc_send_busy_loop(guc, action, ARRAY_SIZE(action), 0, true); 3254 } 3255 } 3256 3257 static void 3258 guc_context_revoke(struct intel_context *ce, struct i915_request *rq, 3259 unsigned int preempt_timeout_ms) 3260 { 3261 struct intel_guc *guc = ce_to_guc(ce); 3262 struct intel_runtime_pm *runtime_pm = 3263 &ce->engine->gt->i915->runtime_pm; 3264 intel_wakeref_t wakeref; 3265 unsigned long flags; 3266 3267 GEM_BUG_ON(intel_context_is_child(ce)); 3268 3269 guc_flush_submissions(guc); 3270 3271 spin_lock_irqsave(&ce->guc_state.lock, flags); 3272 set_context_banned(ce); 3273 3274 if (submission_disabled(guc) || 3275 (!context_enabled(ce) && !context_pending_disable(ce))) { 3276 spin_unlock_irqrestore(&ce->guc_state.lock, flags); 3277 3278 guc_cancel_context_requests(ce); 3279 intel_engine_signal_breadcrumbs(ce->engine); 3280 } else if (!context_pending_disable(ce)) { 3281 u16 guc_id; 3282 3283 /* 3284 * We add +2 here as the schedule disable complete CTB handler 3285 * calls intel_context_sched_disable_unpin (-2 to pin_count). 3286 */ 3287 atomic_add(2, &ce->pin_count); 3288 3289 guc_id = prep_context_pending_disable(ce); 3290 spin_unlock_irqrestore(&ce->guc_state.lock, flags); 3291 3292 /* 3293 * In addition to disabling scheduling, set the preemption 3294 * timeout to the minimum value (1 us) so the banned context 3295 * gets kicked off the HW ASAP. 3296 */ 3297 with_intel_runtime_pm(runtime_pm, wakeref) { 3298 __guc_context_set_preemption_timeout(guc, guc_id, 3299 preempt_timeout_ms); 3300 __guc_context_sched_disable(guc, ce, guc_id); 3301 } 3302 } else { 3303 if (!context_guc_id_invalid(ce)) 3304 with_intel_runtime_pm(runtime_pm, wakeref) 3305 __guc_context_set_preemption_timeout(guc, 3306 ce->guc_id.id, 3307 preempt_timeout_ms); 3308 spin_unlock_irqrestore(&ce->guc_state.lock, flags); 3309 } 3310 } 3311 3312 static void do_sched_disable(struct intel_guc *guc, struct intel_context *ce, 3313 unsigned long flags) 3314 __releases(ce->guc_state.lock) 3315 { 3316 struct intel_runtime_pm *runtime_pm = &ce->engine->gt->i915->runtime_pm; 3317 intel_wakeref_t wakeref; 3318 u16 guc_id; 3319 3320 lockdep_assert_held(&ce->guc_state.lock); 3321 guc_id = prep_context_pending_disable(ce); 3322 3323 spin_unlock_irqrestore(&ce->guc_state.lock, flags); 3324 3325 with_intel_runtime_pm(runtime_pm, wakeref) 3326 __guc_context_sched_disable(guc, ce, guc_id); 3327 } 3328 3329 static bool bypass_sched_disable(struct intel_guc *guc, 3330 struct intel_context *ce) 3331 { 3332 lockdep_assert_held(&ce->guc_state.lock); 3333 GEM_BUG_ON(intel_context_is_child(ce)); 3334 3335 if (submission_disabled(guc) || context_guc_id_invalid(ce) || 3336 !ctx_id_mapped(guc, ce->guc_id.id)) { 3337 clr_context_enabled(ce); 3338 return true; 3339 } 3340 3341 return !context_enabled(ce); 3342 } 3343 3344 static void __delay_sched_disable(struct work_struct *wrk) 3345 { 3346 struct intel_context *ce = 3347 container_of(wrk, typeof(*ce), guc_state.sched_disable_delay_work.work); 3348 struct intel_guc *guc = ce_to_guc(ce); 3349 unsigned long flags; 3350 3351 spin_lock_irqsave(&ce->guc_state.lock, flags); 3352 3353 if (bypass_sched_disable(guc, ce)) { 3354 spin_unlock_irqrestore(&ce->guc_state.lock, flags); 3355 intel_context_sched_disable_unpin(ce); 3356 } else { 3357 do_sched_disable(guc, ce, flags); 3358 } 3359 } 3360 3361 static bool guc_id_pressure(struct intel_guc *guc, struct intel_context *ce) 3362 { 3363 /* 3364 * parent contexts are perma-pinned, if we are unpinning do schedule 3365 * disable immediately. 3366 */ 3367 if (intel_context_is_parent(ce)) 3368 return true; 3369 3370 /* 3371 * If we are beyond the threshold for avail guc_ids, do schedule disable immediately. 3372 */ 3373 return guc->submission_state.guc_ids_in_use > 3374 guc->submission_state.sched_disable_gucid_threshold; 3375 } 3376 3377 static void guc_context_sched_disable(struct intel_context *ce) 3378 { 3379 struct intel_guc *guc = ce_to_guc(ce); 3380 u64 delay = guc->submission_state.sched_disable_delay_ms; 3381 unsigned long flags; 3382 3383 spin_lock_irqsave(&ce->guc_state.lock, flags); 3384 3385 if (bypass_sched_disable(guc, ce)) { 3386 spin_unlock_irqrestore(&ce->guc_state.lock, flags); 3387 intel_context_sched_disable_unpin(ce); 3388 } else if (!intel_context_is_closed(ce) && !guc_id_pressure(guc, ce) && 3389 delay) { 3390 spin_unlock_irqrestore(&ce->guc_state.lock, flags); 3391 mod_delayed_work(system_unbound_wq, 3392 &ce->guc_state.sched_disable_delay_work, 3393 msecs_to_jiffies(delay)); 3394 } else { 3395 do_sched_disable(guc, ce, flags); 3396 } 3397 } 3398 3399 static void guc_context_close(struct intel_context *ce) 3400 { 3401 unsigned long flags; 3402 3403 if (test_bit(CONTEXT_GUC_INIT, &ce->flags) && 3404 cancel_delayed_work(&ce->guc_state.sched_disable_delay_work)) 3405 __delay_sched_disable(&ce->guc_state.sched_disable_delay_work.work); 3406 3407 spin_lock_irqsave(&ce->guc_state.lock, flags); 3408 set_context_close_done(ce); 3409 spin_unlock_irqrestore(&ce->guc_state.lock, flags); 3410 } 3411 3412 static inline int guc_lrc_desc_unpin(struct intel_context *ce) 3413 { 3414 struct intel_guc *guc = ce_to_guc(ce); 3415 struct intel_gt *gt = guc_to_gt(guc); 3416 unsigned long flags; 3417 bool disabled; 3418 int ret; 3419 3420 GEM_BUG_ON(!intel_gt_pm_is_awake(gt)); 3421 GEM_BUG_ON(!ctx_id_mapped(guc, ce->guc_id.id)); 3422 GEM_BUG_ON(ce != __get_context(guc, ce->guc_id.id)); 3423 GEM_BUG_ON(context_enabled(ce)); 3424 3425 /* Seal race with Reset */ 3426 spin_lock_irqsave(&ce->guc_state.lock, flags); 3427 disabled = submission_disabled(guc); 3428 if (likely(!disabled)) { 3429 /* 3430 * Take a gt-pm ref and change context state to be destroyed. 3431 * NOTE: a G2H IRQ that comes after will put this gt-pm ref back 3432 */ 3433 __intel_gt_pm_get(gt); 3434 set_context_destroyed(ce); 3435 clr_context_registered(ce); 3436 } 3437 spin_unlock_irqrestore(&ce->guc_state.lock, flags); 3438 3439 if (unlikely(disabled)) { 3440 release_guc_id(guc, ce); 3441 __guc_context_destroy(ce); 3442 return 0; 3443 } 3444 3445 /* 3446 * GuC is active, lets destroy this context, but at this point we can still be racing 3447 * with suspend, so we undo everything if the H2G fails in deregister_context so 3448 * that GuC reset will find this context during clean up. 3449 */ 3450 ret = deregister_context(ce, ce->guc_id.id); 3451 if (ret) { 3452 spin_lock_irqsave(&ce->guc_state.lock, flags); 3453 set_context_registered(ce); 3454 clr_context_destroyed(ce); 3455 spin_unlock_irqrestore(&ce->guc_state.lock, flags); 3456 /* 3457 * As gt-pm is awake at function entry, intel_wakeref_put_async merely decrements 3458 * the wakeref immediately but per function spec usage call this after unlock. 3459 */ 3460 intel_wakeref_put_async(>->wakeref); 3461 } 3462 3463 return ret; 3464 } 3465 3466 static void __guc_context_destroy(struct intel_context *ce) 3467 { 3468 GEM_BUG_ON(ce->guc_state.prio_count[GUC_CLIENT_PRIORITY_KMD_HIGH] || 3469 ce->guc_state.prio_count[GUC_CLIENT_PRIORITY_HIGH] || 3470 ce->guc_state.prio_count[GUC_CLIENT_PRIORITY_KMD_NORMAL] || 3471 ce->guc_state.prio_count[GUC_CLIENT_PRIORITY_NORMAL]); 3472 3473 lrc_fini(ce); 3474 intel_context_fini(ce); 3475 3476 if (intel_engine_is_virtual(ce->engine)) { 3477 struct guc_virtual_engine *ve = 3478 container_of(ce, typeof(*ve), context); 3479 3480 if (ve->base.breadcrumbs) 3481 intel_breadcrumbs_put(ve->base.breadcrumbs); 3482 3483 kfree(ve); 3484 } else { 3485 intel_context_free(ce); 3486 } 3487 } 3488 3489 static void guc_flush_destroyed_contexts(struct intel_guc *guc) 3490 { 3491 struct intel_context *ce; 3492 unsigned long flags; 3493 3494 GEM_BUG_ON(!submission_disabled(guc) && 3495 guc_submission_initialized(guc)); 3496 3497 while (!list_empty(&guc->submission_state.destroyed_contexts)) { 3498 spin_lock_irqsave(&guc->submission_state.lock, flags); 3499 ce = list_first_entry_or_null(&guc->submission_state.destroyed_contexts, 3500 struct intel_context, 3501 destroyed_link); 3502 if (ce) 3503 list_del_init(&ce->destroyed_link); 3504 spin_unlock_irqrestore(&guc->submission_state.lock, flags); 3505 3506 if (!ce) 3507 break; 3508 3509 release_guc_id(guc, ce); 3510 __guc_context_destroy(ce); 3511 } 3512 } 3513 3514 static void deregister_destroyed_contexts(struct intel_guc *guc) 3515 { 3516 struct intel_context *ce; 3517 unsigned long flags; 3518 3519 while (!list_empty(&guc->submission_state.destroyed_contexts)) { 3520 spin_lock_irqsave(&guc->submission_state.lock, flags); 3521 ce = list_first_entry_or_null(&guc->submission_state.destroyed_contexts, 3522 struct intel_context, 3523 destroyed_link); 3524 if (ce) 3525 list_del_init(&ce->destroyed_link); 3526 spin_unlock_irqrestore(&guc->submission_state.lock, flags); 3527 3528 if (!ce) 3529 break; 3530 3531 if (guc_lrc_desc_unpin(ce)) { 3532 /* 3533 * This means GuC's CT link severed mid-way which could happen 3534 * in suspend-resume corner cases. In this case, put the 3535 * context back into the destroyed_contexts list which will 3536 * get picked up on the next context deregistration event or 3537 * purged in a GuC sanitization event (reset/unload/wedged/...). 3538 */ 3539 spin_lock_irqsave(&guc->submission_state.lock, flags); 3540 list_add_tail(&ce->destroyed_link, 3541 &guc->submission_state.destroyed_contexts); 3542 spin_unlock_irqrestore(&guc->submission_state.lock, flags); 3543 /* Bail now since the list might never be emptied if h2gs fail */ 3544 break; 3545 } 3546 3547 } 3548 } 3549 3550 static void destroyed_worker_func(struct work_struct *w) 3551 { 3552 struct intel_guc *guc = container_of(w, struct intel_guc, 3553 submission_state.destroyed_worker); 3554 struct intel_gt *gt = guc_to_gt(guc); 3555 intel_wakeref_t wakeref; 3556 3557 /* 3558 * In rare cases we can get here via async context-free fence-signals that 3559 * come very late in suspend flow or very early in resume flows. In these 3560 * cases, GuC won't be ready but just skipping it here is fine as these 3561 * pending-destroy-contexts get destroyed totally at GuC reset time at the 3562 * end of suspend.. OR.. this worker can be picked up later on the next 3563 * context destruction trigger after resume-completes 3564 */ 3565 if (!intel_guc_is_ready(guc)) 3566 return; 3567 3568 with_intel_gt_pm(gt, wakeref) 3569 deregister_destroyed_contexts(guc); 3570 } 3571 3572 static void guc_context_destroy(struct kref *kref) 3573 { 3574 struct intel_context *ce = container_of(kref, typeof(*ce), ref); 3575 struct intel_guc *guc = ce_to_guc(ce); 3576 unsigned long flags; 3577 bool destroy; 3578 3579 /* 3580 * If the guc_id is invalid this context has been stolen and we can free 3581 * it immediately. Also can be freed immediately if the context is not 3582 * registered with the GuC or the GuC is in the middle of a reset. 3583 */ 3584 spin_lock_irqsave(&guc->submission_state.lock, flags); 3585 destroy = submission_disabled(guc) || context_guc_id_invalid(ce) || 3586 !ctx_id_mapped(guc, ce->guc_id.id); 3587 if (likely(!destroy)) { 3588 if (!list_empty(&ce->guc_id.link)) 3589 list_del_init(&ce->guc_id.link); 3590 list_add_tail(&ce->destroyed_link, 3591 &guc->submission_state.destroyed_contexts); 3592 } else { 3593 __release_guc_id(guc, ce); 3594 } 3595 spin_unlock_irqrestore(&guc->submission_state.lock, flags); 3596 if (unlikely(destroy)) { 3597 __guc_context_destroy(ce); 3598 return; 3599 } 3600 3601 /* 3602 * We use a worker to issue the H2G to deregister the context as we can 3603 * take the GT PM for the first time which isn't allowed from an atomic 3604 * context. 3605 */ 3606 queue_work(system_unbound_wq, &guc->submission_state.destroyed_worker); 3607 } 3608 3609 static int guc_context_alloc(struct intel_context *ce) 3610 { 3611 return lrc_alloc(ce, ce->engine); 3612 } 3613 3614 static void __guc_context_set_prio(struct intel_guc *guc, 3615 struct intel_context *ce) 3616 { 3617 if (GUC_SUBMIT_VER(guc) >= MAKE_GUC_VER(1, 0, 0)) { 3618 struct context_policy policy; 3619 3620 __guc_context_policy_start_klv(&policy, ce->guc_id.id); 3621 __guc_context_policy_add_priority(&policy, ce->guc_state.prio); 3622 __guc_context_set_context_policies(guc, &policy, true); 3623 } else { 3624 u32 action[] = { 3625 INTEL_GUC_ACTION_V69_SET_CONTEXT_PRIORITY, 3626 ce->guc_id.id, 3627 ce->guc_state.prio, 3628 }; 3629 3630 guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action), 0, true); 3631 } 3632 } 3633 3634 static void guc_context_set_prio(struct intel_guc *guc, 3635 struct intel_context *ce, 3636 u8 prio) 3637 { 3638 GEM_BUG_ON(prio < GUC_CLIENT_PRIORITY_KMD_HIGH || 3639 prio > GUC_CLIENT_PRIORITY_NORMAL); 3640 lockdep_assert_held(&ce->guc_state.lock); 3641 3642 if (ce->guc_state.prio == prio || submission_disabled(guc) || 3643 !context_registered(ce)) { 3644 ce->guc_state.prio = prio; 3645 return; 3646 } 3647 3648 ce->guc_state.prio = prio; 3649 __guc_context_set_prio(guc, ce); 3650 3651 trace_intel_context_set_prio(ce); 3652 } 3653 3654 static inline u8 map_i915_prio_to_guc_prio(int prio) 3655 { 3656 if (prio == I915_PRIORITY_NORMAL) 3657 return GUC_CLIENT_PRIORITY_KMD_NORMAL; 3658 else if (prio < I915_PRIORITY_NORMAL) 3659 return GUC_CLIENT_PRIORITY_NORMAL; 3660 else if (prio < I915_PRIORITY_DISPLAY) 3661 return GUC_CLIENT_PRIORITY_HIGH; 3662 else 3663 return GUC_CLIENT_PRIORITY_KMD_HIGH; 3664 } 3665 3666 static inline void add_context_inflight_prio(struct intel_context *ce, 3667 u8 guc_prio) 3668 { 3669 lockdep_assert_held(&ce->guc_state.lock); 3670 GEM_BUG_ON(guc_prio >= ARRAY_SIZE(ce->guc_state.prio_count)); 3671 3672 ++ce->guc_state.prio_count[guc_prio]; 3673 3674 /* Overflow protection */ 3675 GEM_WARN_ON(!ce->guc_state.prio_count[guc_prio]); 3676 } 3677 3678 static inline void sub_context_inflight_prio(struct intel_context *ce, 3679 u8 guc_prio) 3680 { 3681 lockdep_assert_held(&ce->guc_state.lock); 3682 GEM_BUG_ON(guc_prio >= ARRAY_SIZE(ce->guc_state.prio_count)); 3683 3684 /* Underflow protection */ 3685 GEM_WARN_ON(!ce->guc_state.prio_count[guc_prio]); 3686 3687 --ce->guc_state.prio_count[guc_prio]; 3688 } 3689 3690 static inline void update_context_prio(struct intel_context *ce) 3691 { 3692 struct intel_guc *guc = &ce->engine->gt->uc.guc; 3693 int i; 3694 3695 BUILD_BUG_ON(GUC_CLIENT_PRIORITY_KMD_HIGH != 0); 3696 BUILD_BUG_ON(GUC_CLIENT_PRIORITY_KMD_HIGH > GUC_CLIENT_PRIORITY_NORMAL); 3697 3698 lockdep_assert_held(&ce->guc_state.lock); 3699 3700 for (i = 0; i < ARRAY_SIZE(ce->guc_state.prio_count); ++i) { 3701 if (ce->guc_state.prio_count[i]) { 3702 guc_context_set_prio(guc, ce, i); 3703 break; 3704 } 3705 } 3706 } 3707 3708 static inline bool new_guc_prio_higher(u8 old_guc_prio, u8 new_guc_prio) 3709 { 3710 /* Lower value is higher priority */ 3711 return new_guc_prio < old_guc_prio; 3712 } 3713 3714 static void add_to_context(struct i915_request *rq) 3715 { 3716 struct intel_context *ce = request_to_scheduling_context(rq); 3717 u8 new_guc_prio = map_i915_prio_to_guc_prio(rq_prio(rq)); 3718 3719 GEM_BUG_ON(intel_context_is_child(ce)); 3720 GEM_BUG_ON(rq->guc_prio == GUC_PRIO_FINI); 3721 3722 spin_lock(&ce->guc_state.lock); 3723 list_move_tail(&rq->sched.link, &ce->guc_state.requests); 3724 3725 if (rq->guc_prio == GUC_PRIO_INIT) { 3726 rq->guc_prio = new_guc_prio; 3727 add_context_inflight_prio(ce, rq->guc_prio); 3728 } else if (new_guc_prio_higher(rq->guc_prio, new_guc_prio)) { 3729 sub_context_inflight_prio(ce, rq->guc_prio); 3730 rq->guc_prio = new_guc_prio; 3731 add_context_inflight_prio(ce, rq->guc_prio); 3732 } 3733 update_context_prio(ce); 3734 3735 spin_unlock(&ce->guc_state.lock); 3736 } 3737 3738 static void guc_prio_fini(struct i915_request *rq, struct intel_context *ce) 3739 { 3740 lockdep_assert_held(&ce->guc_state.lock); 3741 3742 if (rq->guc_prio != GUC_PRIO_INIT && 3743 rq->guc_prio != GUC_PRIO_FINI) { 3744 sub_context_inflight_prio(ce, rq->guc_prio); 3745 update_context_prio(ce); 3746 } 3747 rq->guc_prio = GUC_PRIO_FINI; 3748 } 3749 3750 static void remove_from_context(struct i915_request *rq) 3751 { 3752 struct intel_context *ce = request_to_scheduling_context(rq); 3753 3754 GEM_BUG_ON(intel_context_is_child(ce)); 3755 3756 spin_lock_irq(&ce->guc_state.lock); 3757 3758 list_del_init(&rq->sched.link); 3759 clear_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags); 3760 3761 /* Prevent further __await_execution() registering a cb, then flush */ 3762 set_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags); 3763 3764 guc_prio_fini(rq, ce); 3765 3766 spin_unlock_irq(&ce->guc_state.lock); 3767 3768 atomic_dec(&ce->guc_id.ref); 3769 i915_request_notify_execute_cb_imm(rq); 3770 } 3771 3772 static const struct intel_context_ops guc_context_ops = { 3773 .flags = COPS_RUNTIME_CYCLES, 3774 .alloc = guc_context_alloc, 3775 3776 .close = guc_context_close, 3777 3778 .pre_pin = guc_context_pre_pin, 3779 .pin = guc_context_pin, 3780 .unpin = guc_context_unpin, 3781 .post_unpin = guc_context_post_unpin, 3782 3783 .revoke = guc_context_revoke, 3784 3785 .cancel_request = guc_context_cancel_request, 3786 3787 .enter = intel_context_enter_engine, 3788 .exit = intel_context_exit_engine, 3789 3790 .sched_disable = guc_context_sched_disable, 3791 3792 .update_stats = guc_context_update_stats, 3793 3794 .reset = lrc_reset, 3795 .destroy = guc_context_destroy, 3796 3797 .create_virtual = guc_create_virtual, 3798 .create_parallel = guc_create_parallel, 3799 }; 3800 3801 static void submit_work_cb(struct irq_work *wrk) 3802 { 3803 struct i915_request *rq = container_of(wrk, typeof(*rq), submit_work); 3804 3805 might_lock(&rq->engine->sched_engine->lock); 3806 i915_sw_fence_complete(&rq->submit); 3807 } 3808 3809 static void __guc_signal_context_fence(struct intel_context *ce) 3810 { 3811 struct i915_request *rq, *rn; 3812 3813 lockdep_assert_held(&ce->guc_state.lock); 3814 3815 if (!list_empty(&ce->guc_state.fences)) 3816 trace_intel_context_fence_release(ce); 3817 3818 /* 3819 * Use an IRQ to ensure locking order of sched_engine->lock -> 3820 * ce->guc_state.lock is preserved. 3821 */ 3822 list_for_each_entry_safe(rq, rn, &ce->guc_state.fences, 3823 guc_fence_link) { 3824 list_del(&rq->guc_fence_link); 3825 irq_work_queue(&rq->submit_work); 3826 } 3827 3828 INIT_LIST_HEAD(&ce->guc_state.fences); 3829 } 3830 3831 static void guc_signal_context_fence(struct intel_context *ce) 3832 { 3833 unsigned long flags; 3834 3835 GEM_BUG_ON(intel_context_is_child(ce)); 3836 3837 spin_lock_irqsave(&ce->guc_state.lock, flags); 3838 clr_context_wait_for_deregister_to_register(ce); 3839 __guc_signal_context_fence(ce); 3840 spin_unlock_irqrestore(&ce->guc_state.lock, flags); 3841 } 3842 3843 static bool context_needs_register(struct intel_context *ce, bool new_guc_id) 3844 { 3845 return (new_guc_id || test_bit(CONTEXT_LRCA_DIRTY, &ce->flags) || 3846 !ctx_id_mapped(ce_to_guc(ce), ce->guc_id.id)) && 3847 !submission_disabled(ce_to_guc(ce)); 3848 } 3849 3850 static void guc_context_init(struct intel_context *ce) 3851 { 3852 const struct i915_gem_context *ctx; 3853 int prio = I915_CONTEXT_DEFAULT_PRIORITY; 3854 3855 rcu_read_lock(); 3856 ctx = rcu_dereference(ce->gem_context); 3857 if (ctx) 3858 prio = ctx->sched.priority; 3859 rcu_read_unlock(); 3860 3861 ce->guc_state.prio = map_i915_prio_to_guc_prio(prio); 3862 3863 INIT_DELAYED_WORK(&ce->guc_state.sched_disable_delay_work, 3864 __delay_sched_disable); 3865 3866 set_bit(CONTEXT_GUC_INIT, &ce->flags); 3867 } 3868 3869 static int guc_request_alloc(struct i915_request *rq) 3870 { 3871 struct intel_context *ce = request_to_scheduling_context(rq); 3872 struct intel_guc *guc = ce_to_guc(ce); 3873 unsigned long flags; 3874 int ret; 3875 3876 GEM_BUG_ON(!intel_context_is_pinned(rq->context)); 3877 3878 /* 3879 * Flush enough space to reduce the likelihood of waiting after 3880 * we start building the request - in which case we will just 3881 * have to repeat work. 3882 */ 3883 rq->reserved_space += GUC_REQUEST_SIZE; 3884 3885 /* 3886 * Note that after this point, we have committed to using 3887 * this request as it is being used to both track the 3888 * state of engine initialisation and liveness of the 3889 * golden renderstate above. Think twice before you try 3890 * to cancel/unwind this request now. 3891 */ 3892 3893 /* Unconditionally invalidate GPU caches and TLBs. */ 3894 ret = rq->engine->emit_flush(rq, EMIT_INVALIDATE); 3895 if (ret) 3896 return ret; 3897 3898 rq->reserved_space -= GUC_REQUEST_SIZE; 3899 3900 if (unlikely(!test_bit(CONTEXT_GUC_INIT, &ce->flags))) 3901 guc_context_init(ce); 3902 3903 /* 3904 * If the context gets closed while the execbuf is ongoing, the context 3905 * close code will race with the below code to cancel the delayed work. 3906 * If the context close wins the race and cancels the work, it will 3907 * immediately call the sched disable (see guc_context_close), so there 3908 * is a chance we can get past this check while the sched_disable code 3909 * is being executed. To make sure that code completes before we check 3910 * the status further down, we wait for the close process to complete. 3911 * Else, this code path could send a request down thinking that the 3912 * context is still in a schedule-enable mode while the GuC ends up 3913 * dropping the request completely because the disable did go from the 3914 * context_close path right to GuC just prior. In the event the CT is 3915 * full, we could potentially need to wait up to 1.5 seconds. 3916 */ 3917 if (cancel_delayed_work_sync(&ce->guc_state.sched_disable_delay_work)) 3918 intel_context_sched_disable_unpin(ce); 3919 else if (intel_context_is_closed(ce)) 3920 if (wait_for(context_close_done(ce), 1500)) 3921 guc_warn(guc, "timed out waiting on context sched close before realloc\n"); 3922 /* 3923 * Call pin_guc_id here rather than in the pinning step as with 3924 * dma_resv, contexts can be repeatedly pinned / unpinned trashing the 3925 * guc_id and creating horrible race conditions. This is especially bad 3926 * when guc_id are being stolen due to over subscription. By the time 3927 * this function is reached, it is guaranteed that the guc_id will be 3928 * persistent until the generated request is retired. Thus, sealing these 3929 * race conditions. It is still safe to fail here if guc_id are 3930 * exhausted and return -EAGAIN to the user indicating that they can try 3931 * again in the future. 3932 * 3933 * There is no need for a lock here as the timeline mutex ensures at 3934 * most one context can be executing this code path at once. The 3935 * guc_id_ref is incremented once for every request in flight and 3936 * decremented on each retire. When it is zero, a lock around the 3937 * increment (in pin_guc_id) is needed to seal a race with unpin_guc_id. 3938 */ 3939 if (atomic_add_unless(&ce->guc_id.ref, 1, 0)) 3940 goto out; 3941 3942 ret = pin_guc_id(guc, ce); /* returns 1 if new guc_id assigned */ 3943 if (unlikely(ret < 0)) 3944 return ret; 3945 if (context_needs_register(ce, !!ret)) { 3946 ret = try_context_registration(ce, true); 3947 if (unlikely(ret)) { /* unwind */ 3948 if (ret == -EPIPE) { 3949 disable_submission(guc); 3950 goto out; /* GPU will be reset */ 3951 } 3952 atomic_dec(&ce->guc_id.ref); 3953 unpin_guc_id(guc, ce); 3954 return ret; 3955 } 3956 } 3957 3958 clear_bit(CONTEXT_LRCA_DIRTY, &ce->flags); 3959 3960 out: 3961 /* 3962 * We block all requests on this context if a G2H is pending for a 3963 * schedule disable or context deregistration as the GuC will fail a 3964 * schedule enable or context registration if either G2H is pending 3965 * respectfully. Once a G2H returns, the fence is released that is 3966 * blocking these requests (see guc_signal_context_fence). 3967 */ 3968 spin_lock_irqsave(&ce->guc_state.lock, flags); 3969 if (context_wait_for_deregister_to_register(ce) || 3970 context_pending_disable(ce)) { 3971 init_irq_work(&rq->submit_work, submit_work_cb); 3972 i915_sw_fence_await(&rq->submit); 3973 3974 list_add_tail(&rq->guc_fence_link, &ce->guc_state.fences); 3975 } 3976 spin_unlock_irqrestore(&ce->guc_state.lock, flags); 3977 3978 return 0; 3979 } 3980 3981 static int guc_virtual_context_pre_pin(struct intel_context *ce, 3982 struct i915_gem_ww_ctx *ww, 3983 void **vaddr) 3984 { 3985 struct intel_engine_cs *engine = guc_virtual_get_sibling(ce->engine, 0); 3986 3987 return __guc_context_pre_pin(ce, engine, ww, vaddr); 3988 } 3989 3990 static int guc_virtual_context_pin(struct intel_context *ce, void *vaddr) 3991 { 3992 struct intel_engine_cs *engine = guc_virtual_get_sibling(ce->engine, 0); 3993 int ret = __guc_context_pin(ce, engine, vaddr); 3994 intel_engine_mask_t tmp, mask = ce->engine->mask; 3995 3996 if (likely(!ret)) 3997 for_each_engine_masked(engine, ce->engine->gt, mask, tmp) 3998 intel_engine_pm_get(engine); 3999 4000 return ret; 4001 } 4002 4003 static void guc_virtual_context_unpin(struct intel_context *ce) 4004 { 4005 intel_engine_mask_t tmp, mask = ce->engine->mask; 4006 struct intel_engine_cs *engine; 4007 struct intel_guc *guc = ce_to_guc(ce); 4008 4009 GEM_BUG_ON(context_enabled(ce)); 4010 GEM_BUG_ON(intel_context_is_barrier(ce)); 4011 4012 unpin_guc_id(guc, ce); 4013 lrc_unpin(ce); 4014 4015 for_each_engine_masked(engine, ce->engine->gt, mask, tmp) 4016 intel_engine_pm_put_async(engine); 4017 } 4018 4019 static void guc_virtual_context_enter(struct intel_context *ce) 4020 { 4021 intel_engine_mask_t tmp, mask = ce->engine->mask; 4022 struct intel_engine_cs *engine; 4023 4024 for_each_engine_masked(engine, ce->engine->gt, mask, tmp) 4025 intel_engine_pm_get(engine); 4026 4027 intel_timeline_enter(ce->timeline); 4028 } 4029 4030 static void guc_virtual_context_exit(struct intel_context *ce) 4031 { 4032 intel_engine_mask_t tmp, mask = ce->engine->mask; 4033 struct intel_engine_cs *engine; 4034 4035 for_each_engine_masked(engine, ce->engine->gt, mask, tmp) 4036 intel_engine_pm_put(engine); 4037 4038 intel_timeline_exit(ce->timeline); 4039 } 4040 4041 static int guc_virtual_context_alloc(struct intel_context *ce) 4042 { 4043 struct intel_engine_cs *engine = guc_virtual_get_sibling(ce->engine, 0); 4044 4045 return lrc_alloc(ce, engine); 4046 } 4047 4048 static const struct intel_context_ops virtual_guc_context_ops = { 4049 .flags = COPS_RUNTIME_CYCLES, 4050 .alloc = guc_virtual_context_alloc, 4051 4052 .close = guc_context_close, 4053 4054 .pre_pin = guc_virtual_context_pre_pin, 4055 .pin = guc_virtual_context_pin, 4056 .unpin = guc_virtual_context_unpin, 4057 .post_unpin = guc_context_post_unpin, 4058 4059 .revoke = guc_context_revoke, 4060 4061 .cancel_request = guc_context_cancel_request, 4062 4063 .enter = guc_virtual_context_enter, 4064 .exit = guc_virtual_context_exit, 4065 4066 .sched_disable = guc_context_sched_disable, 4067 .update_stats = guc_context_update_stats, 4068 4069 .destroy = guc_context_destroy, 4070 4071 .get_sibling = guc_virtual_get_sibling, 4072 }; 4073 4074 static int guc_parent_context_pin(struct intel_context *ce, void *vaddr) 4075 { 4076 struct intel_engine_cs *engine = guc_virtual_get_sibling(ce->engine, 0); 4077 struct intel_guc *guc = ce_to_guc(ce); 4078 int ret; 4079 4080 GEM_BUG_ON(!intel_context_is_parent(ce)); 4081 GEM_BUG_ON(!intel_engine_is_virtual(ce->engine)); 4082 4083 ret = pin_guc_id(guc, ce); 4084 if (unlikely(ret < 0)) 4085 return ret; 4086 4087 return __guc_context_pin(ce, engine, vaddr); 4088 } 4089 4090 static int guc_child_context_pin(struct intel_context *ce, void *vaddr) 4091 { 4092 struct intel_engine_cs *engine = guc_virtual_get_sibling(ce->engine, 0); 4093 4094 GEM_BUG_ON(!intel_context_is_child(ce)); 4095 GEM_BUG_ON(!intel_engine_is_virtual(ce->engine)); 4096 4097 __intel_context_pin(ce->parallel.parent); 4098 return __guc_context_pin(ce, engine, vaddr); 4099 } 4100 4101 static void guc_parent_context_unpin(struct intel_context *ce) 4102 { 4103 struct intel_guc *guc = ce_to_guc(ce); 4104 4105 GEM_BUG_ON(context_enabled(ce)); 4106 GEM_BUG_ON(intel_context_is_barrier(ce)); 4107 GEM_BUG_ON(!intel_context_is_parent(ce)); 4108 GEM_BUG_ON(!intel_engine_is_virtual(ce->engine)); 4109 4110 unpin_guc_id(guc, ce); 4111 lrc_unpin(ce); 4112 } 4113 4114 static void guc_child_context_unpin(struct intel_context *ce) 4115 { 4116 GEM_BUG_ON(context_enabled(ce)); 4117 GEM_BUG_ON(intel_context_is_barrier(ce)); 4118 GEM_BUG_ON(!intel_context_is_child(ce)); 4119 GEM_BUG_ON(!intel_engine_is_virtual(ce->engine)); 4120 4121 lrc_unpin(ce); 4122 } 4123 4124 static void guc_child_context_post_unpin(struct intel_context *ce) 4125 { 4126 GEM_BUG_ON(!intel_context_is_child(ce)); 4127 GEM_BUG_ON(!intel_context_is_pinned(ce->parallel.parent)); 4128 GEM_BUG_ON(!intel_engine_is_virtual(ce->engine)); 4129 4130 lrc_post_unpin(ce); 4131 intel_context_unpin(ce->parallel.parent); 4132 } 4133 4134 static void guc_child_context_destroy(struct kref *kref) 4135 { 4136 struct intel_context *ce = container_of(kref, typeof(*ce), ref); 4137 4138 __guc_context_destroy(ce); 4139 } 4140 4141 static const struct intel_context_ops virtual_parent_context_ops = { 4142 .alloc = guc_virtual_context_alloc, 4143 4144 .close = guc_context_close, 4145 4146 .pre_pin = guc_context_pre_pin, 4147 .pin = guc_parent_context_pin, 4148 .unpin = guc_parent_context_unpin, 4149 .post_unpin = guc_context_post_unpin, 4150 4151 .revoke = guc_context_revoke, 4152 4153 .cancel_request = guc_context_cancel_request, 4154 4155 .enter = guc_virtual_context_enter, 4156 .exit = guc_virtual_context_exit, 4157 4158 .sched_disable = guc_context_sched_disable, 4159 4160 .destroy = guc_context_destroy, 4161 4162 .get_sibling = guc_virtual_get_sibling, 4163 }; 4164 4165 static const struct intel_context_ops virtual_child_context_ops = { 4166 .alloc = guc_virtual_context_alloc, 4167 4168 .pre_pin = guc_context_pre_pin, 4169 .pin = guc_child_context_pin, 4170 .unpin = guc_child_context_unpin, 4171 .post_unpin = guc_child_context_post_unpin, 4172 4173 .cancel_request = guc_context_cancel_request, 4174 4175 .enter = guc_virtual_context_enter, 4176 .exit = guc_virtual_context_exit, 4177 4178 .destroy = guc_child_context_destroy, 4179 4180 .get_sibling = guc_virtual_get_sibling, 4181 }; 4182 4183 /* 4184 * The below override of the breadcrumbs is enabled when the user configures a 4185 * context for parallel submission (multi-lrc, parent-child). 4186 * 4187 * The overridden breadcrumbs implements an algorithm which allows the GuC to 4188 * safely preempt all the hw contexts configured for parallel submission 4189 * between each BB. The contract between the i915 and GuC is if the parent 4190 * context can be preempted, all the children can be preempted, and the GuC will 4191 * always try to preempt the parent before the children. A handshake between the 4192 * parent / children breadcrumbs ensures the i915 holds up its end of the deal 4193 * creating a window to preempt between each set of BBs. 4194 */ 4195 static int emit_bb_start_parent_no_preempt_mid_batch(struct i915_request *rq, 4196 u64 offset, u32 len, 4197 const unsigned int flags); 4198 static int emit_bb_start_child_no_preempt_mid_batch(struct i915_request *rq, 4199 u64 offset, u32 len, 4200 const unsigned int flags); 4201 static u32 * 4202 emit_fini_breadcrumb_parent_no_preempt_mid_batch(struct i915_request *rq, 4203 u32 *cs); 4204 static u32 * 4205 emit_fini_breadcrumb_child_no_preempt_mid_batch(struct i915_request *rq, 4206 u32 *cs); 4207 4208 static struct intel_context * 4209 guc_create_parallel(struct intel_engine_cs **engines, 4210 unsigned int num_siblings, 4211 unsigned int width) 4212 { 4213 struct intel_engine_cs **siblings = NULL; 4214 struct intel_context *parent = NULL, *ce, *err; 4215 int i, j; 4216 4217 siblings = kmalloc_array(num_siblings, 4218 sizeof(*siblings), 4219 GFP_KERNEL); 4220 if (!siblings) 4221 return ERR_PTR(-ENOMEM); 4222 4223 for (i = 0; i < width; ++i) { 4224 for (j = 0; j < num_siblings; ++j) 4225 siblings[j] = engines[i * num_siblings + j]; 4226 4227 ce = intel_engine_create_virtual(siblings, num_siblings, 4228 FORCE_VIRTUAL); 4229 if (IS_ERR(ce)) { 4230 err = ERR_CAST(ce); 4231 goto unwind; 4232 } 4233 4234 if (i == 0) { 4235 parent = ce; 4236 parent->ops = &virtual_parent_context_ops; 4237 } else { 4238 ce->ops = &virtual_child_context_ops; 4239 intel_context_bind_parent_child(parent, ce); 4240 } 4241 } 4242 4243 parent->parallel.fence_context = dma_fence_context_alloc(1); 4244 4245 parent->engine->emit_bb_start = 4246 emit_bb_start_parent_no_preempt_mid_batch; 4247 parent->engine->emit_fini_breadcrumb = 4248 emit_fini_breadcrumb_parent_no_preempt_mid_batch; 4249 parent->engine->emit_fini_breadcrumb_dw = 4250 12 + 4 * parent->parallel.number_children; 4251 for_each_child(parent, ce) { 4252 ce->engine->emit_bb_start = 4253 emit_bb_start_child_no_preempt_mid_batch; 4254 ce->engine->emit_fini_breadcrumb = 4255 emit_fini_breadcrumb_child_no_preempt_mid_batch; 4256 ce->engine->emit_fini_breadcrumb_dw = 16; 4257 } 4258 4259 kfree(siblings); 4260 return parent; 4261 4262 unwind: 4263 if (parent) 4264 intel_context_put(parent); 4265 kfree(siblings); 4266 return err; 4267 } 4268 4269 static bool 4270 guc_irq_enable_breadcrumbs(struct intel_breadcrumbs *b) 4271 { 4272 struct intel_engine_cs *sibling; 4273 intel_engine_mask_t tmp, mask = b->engine_mask; 4274 bool result = false; 4275 4276 for_each_engine_masked(sibling, b->irq_engine->gt, mask, tmp) 4277 result |= intel_engine_irq_enable(sibling); 4278 4279 return result; 4280 } 4281 4282 static void 4283 guc_irq_disable_breadcrumbs(struct intel_breadcrumbs *b) 4284 { 4285 struct intel_engine_cs *sibling; 4286 intel_engine_mask_t tmp, mask = b->engine_mask; 4287 4288 for_each_engine_masked(sibling, b->irq_engine->gt, mask, tmp) 4289 intel_engine_irq_disable(sibling); 4290 } 4291 4292 static void guc_init_breadcrumbs(struct intel_engine_cs *engine) 4293 { 4294 int i; 4295 4296 /* 4297 * In GuC submission mode we do not know which physical engine a request 4298 * will be scheduled on, this creates a problem because the breadcrumb 4299 * interrupt is per physical engine. To work around this we attach 4300 * requests and direct all breadcrumb interrupts to the first instance 4301 * of an engine per class. In addition all breadcrumb interrupts are 4302 * enabled / disabled across an engine class in unison. 4303 */ 4304 for (i = 0; i < MAX_ENGINE_INSTANCE; ++i) { 4305 struct intel_engine_cs *sibling = 4306 engine->gt->engine_class[engine->class][i]; 4307 4308 if (sibling) { 4309 if (engine->breadcrumbs != sibling->breadcrumbs) { 4310 intel_breadcrumbs_put(engine->breadcrumbs); 4311 engine->breadcrumbs = 4312 intel_breadcrumbs_get(sibling->breadcrumbs); 4313 } 4314 break; 4315 } 4316 } 4317 4318 if (engine->breadcrumbs) { 4319 engine->breadcrumbs->engine_mask |= engine->mask; 4320 engine->breadcrumbs->irq_enable = guc_irq_enable_breadcrumbs; 4321 engine->breadcrumbs->irq_disable = guc_irq_disable_breadcrumbs; 4322 } 4323 } 4324 4325 static void guc_bump_inflight_request_prio(struct i915_request *rq, 4326 int prio) 4327 { 4328 struct intel_context *ce = request_to_scheduling_context(rq); 4329 u8 new_guc_prio = map_i915_prio_to_guc_prio(prio); 4330 4331 /* Short circuit function */ 4332 if (prio < I915_PRIORITY_NORMAL) 4333 return; 4334 4335 spin_lock(&ce->guc_state.lock); 4336 4337 if (rq->guc_prio == GUC_PRIO_FINI) 4338 goto exit; 4339 4340 if (!new_guc_prio_higher(rq->guc_prio, new_guc_prio)) 4341 goto exit; 4342 4343 if (rq->guc_prio != GUC_PRIO_INIT) 4344 sub_context_inflight_prio(ce, rq->guc_prio); 4345 4346 rq->guc_prio = new_guc_prio; 4347 add_context_inflight_prio(ce, rq->guc_prio); 4348 update_context_prio(ce); 4349 4350 exit: 4351 spin_unlock(&ce->guc_state.lock); 4352 } 4353 4354 static void guc_retire_inflight_request_prio(struct i915_request *rq) 4355 { 4356 struct intel_context *ce = request_to_scheduling_context(rq); 4357 4358 spin_lock(&ce->guc_state.lock); 4359 guc_prio_fini(rq, ce); 4360 spin_unlock(&ce->guc_state.lock); 4361 } 4362 4363 static void sanitize_hwsp(struct intel_engine_cs *engine) 4364 { 4365 struct intel_timeline *tl; 4366 4367 list_for_each_entry(tl, &engine->status_page.timelines, engine_link) 4368 intel_timeline_reset_seqno(tl); 4369 } 4370 4371 static void guc_sanitize(struct intel_engine_cs *engine) 4372 { 4373 /* 4374 * Poison residual state on resume, in case the suspend didn't! 4375 * 4376 * We have to assume that across suspend/resume (or other loss 4377 * of control) that the contents of our pinned buffers has been 4378 * lost, replaced by garbage. Since this doesn't always happen, 4379 * let's poison such state so that we more quickly spot when 4380 * we falsely assume it has been preserved. 4381 */ 4382 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) 4383 memset(engine->status_page.addr, POISON_INUSE, PAGE_SIZE); 4384 4385 /* 4386 * The kernel_context HWSP is stored in the status_page. As above, 4387 * that may be lost on resume/initialisation, and so we need to 4388 * reset the value in the HWSP. 4389 */ 4390 sanitize_hwsp(engine); 4391 4392 /* And scrub the dirty cachelines for the HWSP */ 4393 drm_clflush_virt_range(engine->status_page.addr, PAGE_SIZE); 4394 4395 intel_engine_reset_pinned_contexts(engine); 4396 } 4397 4398 static void setup_hwsp(struct intel_engine_cs *engine) 4399 { 4400 intel_engine_set_hwsp_writemask(engine, ~0u); /* HWSTAM */ 4401 4402 ENGINE_WRITE_FW(engine, 4403 RING_HWS_PGA, 4404 i915_ggtt_offset(engine->status_page.vma)); 4405 } 4406 4407 static void start_engine(struct intel_engine_cs *engine) 4408 { 4409 ENGINE_WRITE_FW(engine, 4410 RING_MODE_GEN7, 4411 _MASKED_BIT_ENABLE(GEN11_GFX_DISABLE_LEGACY_MODE)); 4412 4413 ENGINE_WRITE_FW(engine, RING_MI_MODE, _MASKED_BIT_DISABLE(STOP_RING)); 4414 ENGINE_POSTING_READ(engine, RING_MI_MODE); 4415 } 4416 4417 static int guc_resume(struct intel_engine_cs *engine) 4418 { 4419 assert_forcewakes_active(engine->uncore, FORCEWAKE_ALL); 4420 4421 intel_mocs_init_engine(engine); 4422 4423 intel_breadcrumbs_reset(engine->breadcrumbs); 4424 4425 setup_hwsp(engine); 4426 start_engine(engine); 4427 4428 if (engine->flags & I915_ENGINE_FIRST_RENDER_COMPUTE) 4429 xehp_enable_ccs_engines(engine); 4430 4431 return 0; 4432 } 4433 4434 static bool guc_sched_engine_disabled(struct i915_sched_engine *sched_engine) 4435 { 4436 return !sched_engine->tasklet.callback; 4437 } 4438 4439 static void guc_set_default_submission(struct intel_engine_cs *engine) 4440 { 4441 engine->submit_request = guc_submit_request; 4442 } 4443 4444 static inline int guc_kernel_context_pin(struct intel_guc *guc, 4445 struct intel_context *ce) 4446 { 4447 int ret; 4448 4449 /* 4450 * Note: we purposefully do not check the returns below because 4451 * the registration can only fail if a reset is just starting. 4452 * This is called at the end of reset so presumably another reset 4453 * isn't happening and even it did this code would be run again. 4454 */ 4455 4456 if (context_guc_id_invalid(ce)) { 4457 ret = pin_guc_id(guc, ce); 4458 4459 if (ret < 0) 4460 return ret; 4461 } 4462 4463 if (!test_bit(CONTEXT_GUC_INIT, &ce->flags)) 4464 guc_context_init(ce); 4465 4466 ret = try_context_registration(ce, true); 4467 if (ret) 4468 unpin_guc_id(guc, ce); 4469 4470 return ret; 4471 } 4472 4473 static inline int guc_init_submission(struct intel_guc *guc) 4474 { 4475 struct intel_gt *gt = guc_to_gt(guc); 4476 struct intel_engine_cs *engine; 4477 enum intel_engine_id id; 4478 4479 /* make sure all descriptors are clean... */ 4480 xa_destroy(&guc->context_lookup); 4481 4482 /* 4483 * A reset might have occurred while we had a pending stalled request, 4484 * so make sure we clean that up. 4485 */ 4486 guc->stalled_request = NULL; 4487 guc->submission_stall_reason = STALL_NONE; 4488 4489 /* 4490 * Some contexts might have been pinned before we enabled GuC 4491 * submission, so we need to add them to the GuC bookeeping. 4492 * Also, after a reset the of the GuC we want to make sure that the 4493 * information shared with GuC is properly reset. The kernel LRCs are 4494 * not attached to the gem_context, so they need to be added separately. 4495 */ 4496 for_each_engine(engine, gt, id) { 4497 struct intel_context *ce; 4498 4499 list_for_each_entry(ce, &engine->pinned_contexts_list, 4500 pinned_contexts_link) { 4501 int ret = guc_kernel_context_pin(guc, ce); 4502 4503 if (ret) { 4504 /* No point in trying to clean up as i915 will wedge on failure */ 4505 return ret; 4506 } 4507 } 4508 } 4509 4510 return 0; 4511 } 4512 4513 static void guc_release(struct intel_engine_cs *engine) 4514 { 4515 engine->sanitize = NULL; /* no longer in control, nothing to sanitize */ 4516 4517 intel_engine_cleanup_common(engine); 4518 lrc_fini_wa_ctx(engine); 4519 } 4520 4521 static void virtual_guc_bump_serial(struct intel_engine_cs *engine) 4522 { 4523 struct intel_engine_cs *e; 4524 intel_engine_mask_t tmp, mask = engine->mask; 4525 4526 for_each_engine_masked(e, engine->gt, mask, tmp) 4527 e->serial++; 4528 } 4529 4530 static void guc_default_vfuncs(struct intel_engine_cs *engine) 4531 { 4532 /* Default vfuncs which can be overridden by each engine. */ 4533 4534 engine->resume = guc_resume; 4535 4536 engine->cops = &guc_context_ops; 4537 engine->request_alloc = guc_request_alloc; 4538 engine->add_active_request = add_to_context; 4539 engine->remove_active_request = remove_from_context; 4540 4541 engine->sched_engine->schedule = i915_schedule; 4542 4543 engine->reset.prepare = guc_engine_reset_prepare; 4544 engine->reset.rewind = guc_rewind_nop; 4545 engine->reset.cancel = guc_reset_nop; 4546 engine->reset.finish = guc_reset_nop; 4547 4548 engine->emit_flush = gen8_emit_flush_xcs; 4549 engine->emit_init_breadcrumb = gen8_emit_init_breadcrumb; 4550 engine->emit_fini_breadcrumb = gen8_emit_fini_breadcrumb_xcs; 4551 if (GRAPHICS_VER(engine->i915) >= 12) { 4552 engine->emit_fini_breadcrumb = gen12_emit_fini_breadcrumb_xcs; 4553 engine->emit_flush = gen12_emit_flush_xcs; 4554 } 4555 engine->set_default_submission = guc_set_default_submission; 4556 engine->busyness = guc_engine_busyness; 4557 4558 engine->flags |= I915_ENGINE_SUPPORTS_STATS; 4559 engine->flags |= I915_ENGINE_HAS_PREEMPTION; 4560 engine->flags |= I915_ENGINE_HAS_TIMESLICES; 4561 4562 /* Wa_14014475959:dg2 */ 4563 if (engine->class == COMPUTE_CLASS) 4564 if (IS_GFX_GT_IP_STEP(engine->gt, IP_VER(12, 70), STEP_A0, STEP_B0) || 4565 IS_DG2(engine->i915)) 4566 engine->flags |= I915_ENGINE_USES_WA_HOLD_SWITCHOUT; 4567 4568 /* Wa_16019325821 */ 4569 /* Wa_14019159160 */ 4570 if ((engine->class == COMPUTE_CLASS || engine->class == RENDER_CLASS) && 4571 IS_GFX_GT_IP_RANGE(engine->gt, IP_VER(12, 70), IP_VER(12, 74))) 4572 engine->flags |= I915_ENGINE_USES_WA_HOLD_SWITCHOUT; 4573 4574 /* 4575 * TODO: GuC supports timeslicing and semaphores as well, but they're 4576 * handled by the firmware so some minor tweaks are required before 4577 * enabling. 4578 * 4579 * engine->flags |= I915_ENGINE_HAS_SEMAPHORES; 4580 */ 4581 4582 engine->emit_bb_start = gen8_emit_bb_start; 4583 if (GRAPHICS_VER_FULL(engine->i915) >= IP_VER(12, 55)) 4584 engine->emit_bb_start = xehp_emit_bb_start; 4585 } 4586 4587 static void rcs_submission_override(struct intel_engine_cs *engine) 4588 { 4589 switch (GRAPHICS_VER(engine->i915)) { 4590 case 12: 4591 engine->emit_flush = gen12_emit_flush_rcs; 4592 engine->emit_fini_breadcrumb = gen12_emit_fini_breadcrumb_rcs; 4593 break; 4594 case 11: 4595 engine->emit_flush = gen11_emit_flush_rcs; 4596 engine->emit_fini_breadcrumb = gen11_emit_fini_breadcrumb_rcs; 4597 break; 4598 default: 4599 engine->emit_flush = gen8_emit_flush_rcs; 4600 engine->emit_fini_breadcrumb = gen8_emit_fini_breadcrumb_rcs; 4601 break; 4602 } 4603 } 4604 4605 static inline void guc_default_irqs(struct intel_engine_cs *engine) 4606 { 4607 engine->irq_keep_mask = GT_RENDER_USER_INTERRUPT; 4608 intel_engine_set_irq_handler(engine, cs_irq_handler); 4609 } 4610 4611 static void guc_sched_engine_destroy(struct kref *kref) 4612 { 4613 struct i915_sched_engine *sched_engine = 4614 container_of(kref, typeof(*sched_engine), ref); 4615 struct intel_guc *guc = sched_engine->private_data; 4616 4617 guc->sched_engine = NULL; 4618 tasklet_kill(&sched_engine->tasklet); /* flush the callback */ 4619 kfree(sched_engine); 4620 } 4621 4622 int intel_guc_submission_setup(struct intel_engine_cs *engine) 4623 { 4624 struct drm_i915_private *i915 = engine->i915; 4625 struct intel_guc *guc = gt_to_guc(engine->gt); 4626 4627 /* 4628 * The setup relies on several assumptions (e.g. irqs always enabled) 4629 * that are only valid on gen11+ 4630 */ 4631 GEM_BUG_ON(GRAPHICS_VER(i915) < 11); 4632 4633 if (!guc->sched_engine) { 4634 guc->sched_engine = i915_sched_engine_create(ENGINE_VIRTUAL); 4635 if (!guc->sched_engine) 4636 return -ENOMEM; 4637 4638 guc->sched_engine->schedule = i915_schedule; 4639 guc->sched_engine->disabled = guc_sched_engine_disabled; 4640 guc->sched_engine->private_data = guc; 4641 guc->sched_engine->destroy = guc_sched_engine_destroy; 4642 guc->sched_engine->bump_inflight_request_prio = 4643 guc_bump_inflight_request_prio; 4644 guc->sched_engine->retire_inflight_request_prio = 4645 guc_retire_inflight_request_prio; 4646 tasklet_setup(&guc->sched_engine->tasklet, 4647 guc_submission_tasklet); 4648 } 4649 i915_sched_engine_put(engine->sched_engine); 4650 engine->sched_engine = i915_sched_engine_get(guc->sched_engine); 4651 4652 guc_default_vfuncs(engine); 4653 guc_default_irqs(engine); 4654 guc_init_breadcrumbs(engine); 4655 4656 if (engine->flags & I915_ENGINE_HAS_RCS_REG_STATE) 4657 rcs_submission_override(engine); 4658 4659 lrc_init_wa_ctx(engine); 4660 4661 /* Finally, take ownership and responsibility for cleanup! */ 4662 engine->sanitize = guc_sanitize; 4663 engine->release = guc_release; 4664 4665 return 0; 4666 } 4667 4668 struct scheduling_policy { 4669 /* internal data */ 4670 u32 max_words, num_words; 4671 u32 count; 4672 /* API data */ 4673 struct guc_update_scheduling_policy h2g; 4674 }; 4675 4676 static u32 __guc_scheduling_policy_action_size(struct scheduling_policy *policy) 4677 { 4678 u32 *start = (void *)&policy->h2g; 4679 u32 *end = policy->h2g.data + policy->num_words; 4680 size_t delta = end - start; 4681 4682 return delta; 4683 } 4684 4685 static struct scheduling_policy *__guc_scheduling_policy_start_klv(struct scheduling_policy *policy) 4686 { 4687 policy->h2g.header.action = INTEL_GUC_ACTION_UPDATE_SCHEDULING_POLICIES_KLV; 4688 policy->max_words = ARRAY_SIZE(policy->h2g.data); 4689 policy->num_words = 0; 4690 policy->count = 0; 4691 4692 return policy; 4693 } 4694 4695 static void __guc_scheduling_policy_add_klv(struct scheduling_policy *policy, 4696 u32 action, u32 *data, u32 len) 4697 { 4698 u32 *klv_ptr = policy->h2g.data + policy->num_words; 4699 4700 GEM_BUG_ON((policy->num_words + 1 + len) > policy->max_words); 4701 *(klv_ptr++) = FIELD_PREP(GUC_KLV_0_KEY, action) | 4702 FIELD_PREP(GUC_KLV_0_LEN, len); 4703 memcpy(klv_ptr, data, sizeof(u32) * len); 4704 policy->num_words += 1 + len; 4705 policy->count++; 4706 } 4707 4708 static int __guc_action_set_scheduling_policies(struct intel_guc *guc, 4709 struct scheduling_policy *policy) 4710 { 4711 int ret; 4712 4713 ret = intel_guc_send(guc, (u32 *)&policy->h2g, 4714 __guc_scheduling_policy_action_size(policy)); 4715 if (ret < 0) { 4716 guc_probe_error(guc, "Failed to configure global scheduling policies: %pe!\n", 4717 ERR_PTR(ret)); 4718 return ret; 4719 } 4720 4721 if (ret != policy->count) { 4722 guc_warn(guc, "global scheduler policy processed %d of %d KLVs!", 4723 ret, policy->count); 4724 if (ret > policy->count) 4725 return -EPROTO; 4726 } 4727 4728 return 0; 4729 } 4730 4731 static int guc_init_global_schedule_policy(struct intel_guc *guc) 4732 { 4733 struct scheduling_policy policy; 4734 struct intel_gt *gt = guc_to_gt(guc); 4735 intel_wakeref_t wakeref; 4736 int ret; 4737 4738 if (GUC_SUBMIT_VER(guc) < MAKE_GUC_VER(1, 1, 0)) 4739 return 0; 4740 4741 __guc_scheduling_policy_start_klv(&policy); 4742 4743 with_intel_runtime_pm(>->i915->runtime_pm, wakeref) { 4744 u32 yield[] = { 4745 GLOBAL_SCHEDULE_POLICY_RC_YIELD_DURATION, 4746 GLOBAL_SCHEDULE_POLICY_RC_YIELD_RATIO, 4747 }; 4748 4749 __guc_scheduling_policy_add_klv(&policy, 4750 GUC_SCHEDULING_POLICIES_KLV_ID_RENDER_COMPUTE_YIELD, 4751 yield, ARRAY_SIZE(yield)); 4752 4753 ret = __guc_action_set_scheduling_policies(guc, &policy); 4754 } 4755 4756 return ret; 4757 } 4758 4759 static void guc_route_semaphores(struct intel_guc *guc, bool to_guc) 4760 { 4761 struct intel_gt *gt = guc_to_gt(guc); 4762 u32 val; 4763 4764 if (GRAPHICS_VER(gt->i915) < 12) 4765 return; 4766 4767 if (to_guc) 4768 val = GUC_SEM_INTR_ROUTE_TO_GUC | GUC_SEM_INTR_ENABLE_ALL; 4769 else 4770 val = 0; 4771 4772 intel_uncore_write(gt->uncore, GEN12_GUC_SEM_INTR_ENABLES, val); 4773 } 4774 4775 int intel_guc_submission_enable(struct intel_guc *guc) 4776 { 4777 int ret; 4778 4779 /* Semaphore interrupt enable and route to GuC */ 4780 guc_route_semaphores(guc, true); 4781 4782 ret = guc_init_submission(guc); 4783 if (ret) 4784 goto fail_sem; 4785 4786 ret = guc_init_engine_stats(guc); 4787 if (ret) 4788 goto fail_sem; 4789 4790 ret = guc_init_global_schedule_policy(guc); 4791 if (ret) 4792 goto fail_stats; 4793 4794 return 0; 4795 4796 fail_stats: 4797 guc_fini_engine_stats(guc); 4798 fail_sem: 4799 guc_route_semaphores(guc, false); 4800 return ret; 4801 } 4802 4803 /* Note: By the time we're here, GuC may have already been reset */ 4804 void intel_guc_submission_disable(struct intel_guc *guc) 4805 { 4806 guc_cancel_busyness_worker(guc); 4807 4808 /* Semaphore interrupt disable and route to host */ 4809 guc_route_semaphores(guc, false); 4810 } 4811 4812 static bool __guc_submission_supported(struct intel_guc *guc) 4813 { 4814 /* GuC submission is unavailable for pre-Gen11 */ 4815 return intel_guc_is_supported(guc) && 4816 GRAPHICS_VER(guc_to_i915(guc)) >= 11; 4817 } 4818 4819 static bool __guc_submission_selected(struct intel_guc *guc) 4820 { 4821 struct drm_i915_private *i915 = guc_to_i915(guc); 4822 4823 if (!intel_guc_submission_is_supported(guc)) 4824 return false; 4825 4826 return i915->params.enable_guc & ENABLE_GUC_SUBMISSION; 4827 } 4828 4829 int intel_guc_sched_disable_gucid_threshold_max(struct intel_guc *guc) 4830 { 4831 return guc->submission_state.num_guc_ids - NUMBER_MULTI_LRC_GUC_ID(guc); 4832 } 4833 4834 /* 4835 * This default value of 33 milisecs (+1 milisec round up) ensures 30fps or higher 4836 * workloads are able to enjoy the latency reduction when delaying the schedule-disable 4837 * operation. This matches the 30fps game-render + encode (real world) workload this 4838 * knob was tested against. 4839 */ 4840 #define SCHED_DISABLE_DELAY_MS 34 4841 4842 /* 4843 * A threshold of 75% is a reasonable starting point considering that real world apps 4844 * generally don't get anywhere near this. 4845 */ 4846 #define NUM_SCHED_DISABLE_GUCIDS_DEFAULT_THRESHOLD(__guc) \ 4847 (((intel_guc_sched_disable_gucid_threshold_max(guc)) * 3) / 4) 4848 4849 void intel_guc_submission_init_early(struct intel_guc *guc) 4850 { 4851 xa_init_flags(&guc->context_lookup, XA_FLAGS_LOCK_IRQ); 4852 4853 spin_lock_init(&guc->submission_state.lock); 4854 INIT_LIST_HEAD(&guc->submission_state.guc_id_list); 4855 ida_init(&guc->submission_state.guc_ids); 4856 INIT_LIST_HEAD(&guc->submission_state.destroyed_contexts); 4857 INIT_WORK(&guc->submission_state.destroyed_worker, 4858 destroyed_worker_func); 4859 INIT_WORK(&guc->submission_state.reset_fail_worker, 4860 reset_fail_worker_func); 4861 4862 spin_lock_init(&guc->timestamp.lock); 4863 INIT_DELAYED_WORK(&guc->timestamp.work, guc_timestamp_ping); 4864 4865 guc->submission_state.sched_disable_delay_ms = SCHED_DISABLE_DELAY_MS; 4866 guc->submission_state.num_guc_ids = GUC_MAX_CONTEXT_ID; 4867 guc->submission_state.sched_disable_gucid_threshold = 4868 NUM_SCHED_DISABLE_GUCIDS_DEFAULT_THRESHOLD(guc); 4869 guc->submission_supported = __guc_submission_supported(guc); 4870 guc->submission_selected = __guc_submission_selected(guc); 4871 } 4872 4873 static inline struct intel_context * 4874 g2h_context_lookup(struct intel_guc *guc, u32 ctx_id) 4875 { 4876 struct intel_context *ce; 4877 4878 if (unlikely(ctx_id >= GUC_MAX_CONTEXT_ID)) { 4879 guc_err(guc, "Invalid ctx_id %u\n", ctx_id); 4880 return NULL; 4881 } 4882 4883 ce = __get_context(guc, ctx_id); 4884 if (unlikely(!ce)) { 4885 guc_err(guc, "Context is NULL, ctx_id %u\n", ctx_id); 4886 return NULL; 4887 } 4888 4889 if (unlikely(intel_context_is_child(ce))) { 4890 guc_err(guc, "Context is child, ctx_id %u\n", ctx_id); 4891 return NULL; 4892 } 4893 4894 return ce; 4895 } 4896 4897 static void wait_wake_outstanding_tlb_g2h(struct intel_guc *guc, u32 seqno) 4898 { 4899 struct intel_guc_tlb_wait *wait; 4900 unsigned long flags; 4901 4902 xa_lock_irqsave(&guc->tlb_lookup, flags); 4903 wait = xa_load(&guc->tlb_lookup, seqno); 4904 4905 if (wait) 4906 wake_up(&wait->wq); 4907 else 4908 guc_dbg(guc, 4909 "Stale TLB invalidation response with seqno %d\n", seqno); 4910 4911 xa_unlock_irqrestore(&guc->tlb_lookup, flags); 4912 } 4913 4914 int intel_guc_tlb_invalidation_done(struct intel_guc *guc, 4915 const u32 *payload, u32 len) 4916 { 4917 if (len < 1) 4918 return -EPROTO; 4919 4920 wait_wake_outstanding_tlb_g2h(guc, payload[0]); 4921 return 0; 4922 } 4923 4924 static long must_wait_woken(struct wait_queue_entry *wq_entry, long timeout) 4925 { 4926 /* 4927 * This is equivalent to wait_woken() with the exception that 4928 * we do not wake up early if the kthread task has been completed. 4929 * As we are called from page reclaim in any task context, 4930 * we may be invoked from stopped kthreads, but we *must* 4931 * complete the wait from the HW. 4932 */ 4933 do { 4934 set_current_state(TASK_UNINTERRUPTIBLE); 4935 if (wq_entry->flags & WQ_FLAG_WOKEN) 4936 break; 4937 4938 timeout = schedule_timeout(timeout); 4939 } while (timeout); 4940 4941 /* See wait_woken() and woken_wake_function() */ 4942 __set_current_state(TASK_RUNNING); 4943 smp_store_mb(wq_entry->flags, wq_entry->flags & ~WQ_FLAG_WOKEN); 4944 4945 return timeout; 4946 } 4947 4948 static bool intel_gt_is_enabled(const struct intel_gt *gt) 4949 { 4950 /* Check if GT is wedged or suspended */ 4951 if (intel_gt_is_wedged(gt) || !intel_irqs_enabled(gt->i915)) 4952 return false; 4953 return true; 4954 } 4955 4956 static int guc_send_invalidate_tlb(struct intel_guc *guc, 4957 enum intel_guc_tlb_invalidation_type type) 4958 { 4959 struct intel_guc_tlb_wait _wq, *wq = &_wq; 4960 struct intel_gt *gt = guc_to_gt(guc); 4961 DEFINE_WAIT_FUNC(wait, woken_wake_function); 4962 int err; 4963 u32 seqno; 4964 u32 action[] = { 4965 INTEL_GUC_ACTION_TLB_INVALIDATION, 4966 0, 4967 REG_FIELD_PREP(INTEL_GUC_TLB_INVAL_TYPE_MASK, type) | 4968 REG_FIELD_PREP(INTEL_GUC_TLB_INVAL_MODE_MASK, 4969 INTEL_GUC_TLB_INVAL_MODE_HEAVY) | 4970 INTEL_GUC_TLB_INVAL_FLUSH_CACHE, 4971 }; 4972 u32 size = ARRAY_SIZE(action); 4973 4974 /* 4975 * Early guard against GT enablement. TLB invalidation should not be 4976 * attempted if the GT is disabled due to suspend/wedge. 4977 */ 4978 if (!intel_gt_is_enabled(gt)) 4979 return -EINVAL; 4980 4981 init_waitqueue_head(&_wq.wq); 4982 4983 if (xa_alloc_cyclic_irq(&guc->tlb_lookup, &seqno, wq, 4984 xa_limit_32b, &guc->next_seqno, 4985 GFP_ATOMIC | __GFP_NOWARN) < 0) { 4986 /* Under severe memory pressure? Serialise TLB allocations */ 4987 xa_lock_irq(&guc->tlb_lookup); 4988 wq = xa_load(&guc->tlb_lookup, guc->serial_slot); 4989 wait_event_lock_irq(wq->wq, 4990 !READ_ONCE(wq->busy), 4991 guc->tlb_lookup.xa_lock); 4992 /* 4993 * Update wq->busy under lock to ensure only one waiter can 4994 * issue the TLB invalidation command using the serial slot at a 4995 * time. The condition is set to true before releasing the lock 4996 * so that other caller continue to wait until woken up again. 4997 */ 4998 wq->busy = true; 4999 xa_unlock_irq(&guc->tlb_lookup); 5000 5001 seqno = guc->serial_slot; 5002 } 5003 5004 action[1] = seqno; 5005 5006 add_wait_queue(&wq->wq, &wait); 5007 5008 /* This is a critical reclaim path and thus we must loop here. */ 5009 err = intel_guc_send_busy_loop(guc, action, size, G2H_LEN_DW_INVALIDATE_TLB, true); 5010 if (err) 5011 goto out; 5012 5013 /* 5014 * Late guard against GT enablement. It is not an error for the TLB 5015 * invalidation to time out if the GT is disabled during the process 5016 * due to suspend/wedge. In fact, the TLB invalidation is cancelled 5017 * in this case. 5018 */ 5019 if (!must_wait_woken(&wait, intel_guc_ct_max_queue_time_jiffies()) && 5020 intel_gt_is_enabled(gt)) { 5021 guc_err(guc, 5022 "TLB invalidation response timed out for seqno %u\n", seqno); 5023 err = -ETIME; 5024 } 5025 out: 5026 remove_wait_queue(&wq->wq, &wait); 5027 if (seqno != guc->serial_slot) 5028 xa_erase_irq(&guc->tlb_lookup, seqno); 5029 5030 return err; 5031 } 5032 5033 /* Send a H2G command to invalidate the TLBs at engine level and beyond. */ 5034 int intel_guc_invalidate_tlb_engines(struct intel_guc *guc) 5035 { 5036 return guc_send_invalidate_tlb(guc, INTEL_GUC_TLB_INVAL_ENGINES); 5037 } 5038 5039 /* Send a H2G command to invalidate the GuC's internal TLB. */ 5040 int intel_guc_invalidate_tlb_guc(struct intel_guc *guc) 5041 { 5042 return guc_send_invalidate_tlb(guc, INTEL_GUC_TLB_INVAL_GUC); 5043 } 5044 5045 int intel_guc_deregister_done_process_msg(struct intel_guc *guc, 5046 const u32 *msg, 5047 u32 len) 5048 { 5049 struct intel_context *ce; 5050 u32 ctx_id; 5051 5052 if (unlikely(len < 1)) { 5053 guc_err(guc, "Invalid length %u\n", len); 5054 return -EPROTO; 5055 } 5056 ctx_id = msg[0]; 5057 5058 ce = g2h_context_lookup(guc, ctx_id); 5059 if (unlikely(!ce)) 5060 return -EPROTO; 5061 5062 trace_intel_context_deregister_done(ce); 5063 5064 #ifdef CONFIG_DRM_I915_SELFTEST 5065 if (unlikely(ce->drop_deregister)) { 5066 ce->drop_deregister = false; 5067 return 0; 5068 } 5069 #endif 5070 5071 if (context_wait_for_deregister_to_register(ce)) { 5072 struct intel_runtime_pm *runtime_pm = 5073 &ce->engine->gt->i915->runtime_pm; 5074 intel_wakeref_t wakeref; 5075 5076 /* 5077 * Previous owner of this guc_id has been deregistered, now safe 5078 * register this context. 5079 */ 5080 with_intel_runtime_pm(runtime_pm, wakeref) 5081 register_context(ce, true); 5082 guc_signal_context_fence(ce); 5083 intel_context_put(ce); 5084 } else if (context_destroyed(ce)) { 5085 /* Context has been destroyed */ 5086 intel_gt_pm_put_async_untracked(guc_to_gt(guc)); 5087 release_guc_id(guc, ce); 5088 __guc_context_destroy(ce); 5089 } 5090 5091 decr_outstanding_submission_g2h(guc); 5092 5093 return 0; 5094 } 5095 5096 int intel_guc_sched_done_process_msg(struct intel_guc *guc, 5097 const u32 *msg, 5098 u32 len) 5099 { 5100 struct intel_context *ce; 5101 unsigned long flags; 5102 u32 ctx_id; 5103 5104 if (unlikely(len < 2)) { 5105 guc_err(guc, "Invalid length %u\n", len); 5106 return -EPROTO; 5107 } 5108 ctx_id = msg[0]; 5109 5110 ce = g2h_context_lookup(guc, ctx_id); 5111 if (unlikely(!ce)) 5112 return -EPROTO; 5113 5114 if (unlikely(context_destroyed(ce) || 5115 (!context_pending_enable(ce) && 5116 !context_pending_disable(ce)))) { 5117 guc_err(guc, "Bad context sched_state 0x%x, ctx_id %u\n", 5118 ce->guc_state.sched_state, ctx_id); 5119 return -EPROTO; 5120 } 5121 5122 trace_intel_context_sched_done(ce); 5123 5124 if (context_pending_enable(ce)) { 5125 #ifdef CONFIG_DRM_I915_SELFTEST 5126 if (unlikely(ce->drop_schedule_enable)) { 5127 ce->drop_schedule_enable = false; 5128 return 0; 5129 } 5130 #endif 5131 5132 spin_lock_irqsave(&ce->guc_state.lock, flags); 5133 clr_context_pending_enable(ce); 5134 spin_unlock_irqrestore(&ce->guc_state.lock, flags); 5135 } else if (context_pending_disable(ce)) { 5136 bool banned; 5137 5138 #ifdef CONFIG_DRM_I915_SELFTEST 5139 if (unlikely(ce->drop_schedule_disable)) { 5140 ce->drop_schedule_disable = false; 5141 return 0; 5142 } 5143 #endif 5144 5145 /* 5146 * Unpin must be done before __guc_signal_context_fence, 5147 * otherwise a race exists between the requests getting 5148 * submitted + retired before this unpin completes resulting in 5149 * the pin_count going to zero and the context still being 5150 * enabled. 5151 */ 5152 intel_context_sched_disable_unpin(ce); 5153 5154 spin_lock_irqsave(&ce->guc_state.lock, flags); 5155 banned = context_banned(ce); 5156 clr_context_banned(ce); 5157 clr_context_pending_disable(ce); 5158 __guc_signal_context_fence(ce); 5159 guc_blocked_fence_complete(ce); 5160 spin_unlock_irqrestore(&ce->guc_state.lock, flags); 5161 5162 if (banned) { 5163 guc_cancel_context_requests(ce); 5164 intel_engine_signal_breadcrumbs(ce->engine); 5165 } 5166 } 5167 5168 decr_outstanding_submission_g2h(guc); 5169 intel_context_put(ce); 5170 5171 return 0; 5172 } 5173 5174 static void capture_error_state(struct intel_guc *guc, 5175 struct intel_context *ce) 5176 { 5177 struct intel_gt *gt = guc_to_gt(guc); 5178 struct drm_i915_private *i915 = gt->i915; 5179 intel_wakeref_t wakeref; 5180 intel_engine_mask_t engine_mask; 5181 5182 if (intel_engine_is_virtual(ce->engine)) { 5183 struct intel_engine_cs *e; 5184 intel_engine_mask_t tmp, virtual_mask = ce->engine->mask; 5185 5186 engine_mask = 0; 5187 for_each_engine_masked(e, ce->engine->gt, virtual_mask, tmp) { 5188 bool match = intel_guc_capture_is_matching_engine(gt, ce, e); 5189 5190 if (match) { 5191 intel_engine_set_hung_context(e, ce); 5192 engine_mask |= e->mask; 5193 i915_increase_reset_engine_count(&i915->gpu_error, 5194 e); 5195 } 5196 } 5197 5198 if (!engine_mask) { 5199 guc_warn(guc, "No matching physical engine capture for virtual engine context 0x%04X / %s", 5200 ce->guc_id.id, ce->engine->name); 5201 engine_mask = ~0U; 5202 } 5203 } else { 5204 intel_engine_set_hung_context(ce->engine, ce); 5205 engine_mask = ce->engine->mask; 5206 i915_increase_reset_engine_count(&i915->gpu_error, ce->engine); 5207 } 5208 5209 with_intel_runtime_pm(&i915->runtime_pm, wakeref) 5210 i915_capture_error_state(gt, engine_mask, CORE_DUMP_FLAG_IS_GUC_CAPTURE); 5211 } 5212 5213 static void guc_context_replay(struct intel_context *ce) 5214 { 5215 struct i915_sched_engine *sched_engine = ce->engine->sched_engine; 5216 5217 __guc_reset_context(ce, ce->engine->mask); 5218 tasklet_hi_schedule(&sched_engine->tasklet); 5219 } 5220 5221 static void guc_handle_context_reset(struct intel_guc *guc, 5222 struct intel_context *ce) 5223 { 5224 bool capture = intel_context_is_schedulable(ce); 5225 5226 trace_intel_context_reset(ce); 5227 5228 guc_dbg(guc, "%s context reset notification: 0x%04X on %s, exiting = %s, banned = %s\n", 5229 capture ? "Got" : "Ignoring", 5230 ce->guc_id.id, ce->engine->name, 5231 str_yes_no(intel_context_is_exiting(ce)), 5232 str_yes_no(intel_context_is_banned(ce))); 5233 5234 if (capture) { 5235 capture_error_state(guc, ce); 5236 guc_context_replay(ce); 5237 } 5238 } 5239 5240 int intel_guc_context_reset_process_msg(struct intel_guc *guc, 5241 const u32 *msg, u32 len) 5242 { 5243 struct intel_context *ce; 5244 unsigned long flags; 5245 int ctx_id; 5246 5247 if (unlikely(len != 1)) { 5248 guc_err(guc, "Invalid length %u", len); 5249 return -EPROTO; 5250 } 5251 5252 ctx_id = msg[0]; 5253 5254 /* 5255 * The context lookup uses the xarray but lookups only require an RCU lock 5256 * not the full spinlock. So take the lock explicitly and keep it until the 5257 * context has been reference count locked to ensure it can't be destroyed 5258 * asynchronously until the reset is done. 5259 */ 5260 xa_lock_irqsave(&guc->context_lookup, flags); 5261 ce = g2h_context_lookup(guc, ctx_id); 5262 if (ce) 5263 intel_context_get(ce); 5264 xa_unlock_irqrestore(&guc->context_lookup, flags); 5265 5266 if (unlikely(!ce)) 5267 return -EPROTO; 5268 5269 guc_handle_context_reset(guc, ce); 5270 intel_context_put(ce); 5271 5272 return 0; 5273 } 5274 5275 int intel_guc_error_capture_process_msg(struct intel_guc *guc, 5276 const u32 *msg, u32 len) 5277 { 5278 u32 status; 5279 5280 if (unlikely(len != 1)) { 5281 guc_dbg(guc, "Invalid length %u", len); 5282 return -EPROTO; 5283 } 5284 5285 status = msg[0] & INTEL_GUC_STATE_CAPTURE_EVENT_STATUS_MASK; 5286 if (status == INTEL_GUC_STATE_CAPTURE_EVENT_STATUS_NOSPACE) 5287 guc_warn(guc, "No space for error capture"); 5288 5289 intel_guc_capture_process(guc); 5290 5291 return 0; 5292 } 5293 5294 struct intel_engine_cs * 5295 intel_guc_lookup_engine(struct intel_guc *guc, u8 guc_class, u8 instance) 5296 { 5297 struct intel_gt *gt = guc_to_gt(guc); 5298 u8 engine_class = guc_class_to_engine_class(guc_class); 5299 5300 /* Class index is checked in class converter */ 5301 GEM_BUG_ON(instance > MAX_ENGINE_INSTANCE); 5302 5303 return gt->engine_class[engine_class][instance]; 5304 } 5305 5306 static void reset_fail_worker_func(struct work_struct *w) 5307 { 5308 struct intel_guc *guc = container_of(w, struct intel_guc, 5309 submission_state.reset_fail_worker); 5310 struct intel_gt *gt = guc_to_gt(guc); 5311 intel_engine_mask_t reset_fail_mask; 5312 unsigned long flags; 5313 5314 spin_lock_irqsave(&guc->submission_state.lock, flags); 5315 reset_fail_mask = guc->submission_state.reset_fail_mask; 5316 guc->submission_state.reset_fail_mask = 0; 5317 spin_unlock_irqrestore(&guc->submission_state.lock, flags); 5318 5319 if (likely(reset_fail_mask)) { 5320 struct intel_engine_cs *engine; 5321 enum intel_engine_id id; 5322 5323 /* 5324 * GuC is toast at this point - it dead loops after sending the failed 5325 * reset notification. So need to manually determine the guilty context. 5326 * Note that it should be reliable to do this here because the GuC is 5327 * toast and will not be scheduling behind the KMD's back. 5328 */ 5329 for_each_engine_masked(engine, gt, reset_fail_mask, id) 5330 intel_guc_find_hung_context(engine); 5331 5332 intel_gt_handle_error(gt, reset_fail_mask, 5333 I915_ERROR_CAPTURE, 5334 "GuC failed to reset engine mask=0x%x", 5335 reset_fail_mask); 5336 } 5337 } 5338 5339 int intel_guc_engine_failure_process_msg(struct intel_guc *guc, 5340 const u32 *msg, u32 len) 5341 { 5342 struct intel_engine_cs *engine; 5343 u8 guc_class, instance; 5344 u32 reason; 5345 unsigned long flags; 5346 5347 if (unlikely(len != 3)) { 5348 guc_err(guc, "Invalid length %u", len); 5349 return -EPROTO; 5350 } 5351 5352 guc_class = msg[0]; 5353 instance = msg[1]; 5354 reason = msg[2]; 5355 5356 engine = intel_guc_lookup_engine(guc, guc_class, instance); 5357 if (unlikely(!engine)) { 5358 guc_err(guc, "Invalid engine %d:%d", guc_class, instance); 5359 return -EPROTO; 5360 } 5361 5362 /* 5363 * This is an unexpected failure of a hardware feature. So, log a real 5364 * error message not just the informational that comes with the reset. 5365 */ 5366 guc_err(guc, "Engine reset failed on %d:%d (%s) because 0x%08X", 5367 guc_class, instance, engine->name, reason); 5368 5369 spin_lock_irqsave(&guc->submission_state.lock, flags); 5370 guc->submission_state.reset_fail_mask |= engine->mask; 5371 spin_unlock_irqrestore(&guc->submission_state.lock, flags); 5372 5373 /* 5374 * A GT reset flushes this worker queue (G2H handler) so we must use 5375 * another worker to trigger a GT reset. 5376 */ 5377 queue_work(system_unbound_wq, &guc->submission_state.reset_fail_worker); 5378 5379 return 0; 5380 } 5381 5382 void intel_guc_find_hung_context(struct intel_engine_cs *engine) 5383 { 5384 struct intel_guc *guc = gt_to_guc(engine->gt); 5385 struct intel_context *ce; 5386 struct i915_request *rq; 5387 unsigned long index; 5388 unsigned long flags; 5389 5390 /* Reset called during driver load? GuC not yet initialised! */ 5391 if (unlikely(!guc_submission_initialized(guc))) 5392 return; 5393 5394 xa_lock_irqsave(&guc->context_lookup, flags); 5395 xa_for_each(&guc->context_lookup, index, ce) { 5396 bool found; 5397 5398 if (!kref_get_unless_zero(&ce->ref)) 5399 continue; 5400 5401 xa_unlock(&guc->context_lookup); 5402 5403 if (!intel_context_is_pinned(ce)) 5404 goto next; 5405 5406 if (intel_engine_is_virtual(ce->engine)) { 5407 if (!(ce->engine->mask & engine->mask)) 5408 goto next; 5409 } else { 5410 if (ce->engine != engine) 5411 goto next; 5412 } 5413 5414 found = false; 5415 spin_lock(&ce->guc_state.lock); 5416 list_for_each_entry(rq, &ce->guc_state.requests, sched.link) { 5417 if (i915_test_request_state(rq) != I915_REQUEST_ACTIVE) 5418 continue; 5419 5420 found = true; 5421 break; 5422 } 5423 spin_unlock(&ce->guc_state.lock); 5424 5425 if (found) { 5426 intel_engine_set_hung_context(engine, ce); 5427 5428 /* Can only cope with one hang at a time... */ 5429 intel_context_put(ce); 5430 xa_lock(&guc->context_lookup); 5431 goto done; 5432 } 5433 5434 next: 5435 intel_context_put(ce); 5436 xa_lock(&guc->context_lookup); 5437 } 5438 done: 5439 xa_unlock_irqrestore(&guc->context_lookup, flags); 5440 } 5441 5442 void intel_guc_dump_active_requests(struct intel_engine_cs *engine, 5443 struct i915_request *hung_rq, 5444 struct drm_printer *m) 5445 { 5446 struct intel_guc *guc = gt_to_guc(engine->gt); 5447 struct intel_context *ce; 5448 unsigned long index; 5449 unsigned long flags; 5450 5451 /* Reset called during driver load? GuC not yet initialised! */ 5452 if (unlikely(!guc_submission_initialized(guc))) 5453 return; 5454 5455 xa_lock_irqsave(&guc->context_lookup, flags); 5456 xa_for_each(&guc->context_lookup, index, ce) { 5457 if (!kref_get_unless_zero(&ce->ref)) 5458 continue; 5459 5460 xa_unlock(&guc->context_lookup); 5461 5462 if (!intel_context_is_pinned(ce)) 5463 goto next; 5464 5465 if (intel_engine_is_virtual(ce->engine)) { 5466 if (!(ce->engine->mask & engine->mask)) 5467 goto next; 5468 } else { 5469 if (ce->engine != engine) 5470 goto next; 5471 } 5472 5473 spin_lock(&ce->guc_state.lock); 5474 intel_engine_dump_active_requests(&ce->guc_state.requests, 5475 hung_rq, m); 5476 spin_unlock(&ce->guc_state.lock); 5477 5478 next: 5479 intel_context_put(ce); 5480 xa_lock(&guc->context_lookup); 5481 } 5482 xa_unlock_irqrestore(&guc->context_lookup, flags); 5483 } 5484 5485 void intel_guc_submission_print_info(struct intel_guc *guc, 5486 struct drm_printer *p) 5487 { 5488 struct i915_sched_engine *sched_engine = guc->sched_engine; 5489 struct rb_node *rb; 5490 unsigned long flags; 5491 5492 if (!sched_engine) 5493 return; 5494 5495 drm_printf(p, "GuC Submission API Version: %d.%d.%d\n", 5496 guc->submission_version.major, guc->submission_version.minor, 5497 guc->submission_version.patch); 5498 drm_printf(p, "GuC Number Outstanding Submission G2H: %u\n", 5499 atomic_read(&guc->outstanding_submission_g2h)); 5500 drm_printf(p, "GuC tasklet count: %u\n", 5501 atomic_read(&sched_engine->tasklet.count)); 5502 5503 spin_lock_irqsave(&sched_engine->lock, flags); 5504 drm_printf(p, "Requests in GuC submit tasklet:\n"); 5505 for (rb = rb_first_cached(&sched_engine->queue); rb; rb = rb_next(rb)) { 5506 struct i915_priolist *pl = to_priolist(rb); 5507 struct i915_request *rq; 5508 5509 priolist_for_each_request(rq, pl) 5510 drm_printf(p, "guc_id=%u, seqno=%llu\n", 5511 rq->context->guc_id.id, 5512 rq->fence.seqno); 5513 } 5514 spin_unlock_irqrestore(&sched_engine->lock, flags); 5515 drm_printf(p, "\n"); 5516 } 5517 5518 static inline void guc_log_context_priority(struct drm_printer *p, 5519 struct intel_context *ce) 5520 { 5521 int i; 5522 5523 drm_printf(p, "\t\tPriority: %d\n", ce->guc_state.prio); 5524 drm_printf(p, "\t\tNumber Requests (lower index == higher priority)\n"); 5525 for (i = GUC_CLIENT_PRIORITY_KMD_HIGH; 5526 i < GUC_CLIENT_PRIORITY_NUM; ++i) { 5527 drm_printf(p, "\t\tNumber requests in priority band[%d]: %d\n", 5528 i, ce->guc_state.prio_count[i]); 5529 } 5530 drm_printf(p, "\n"); 5531 } 5532 5533 static inline void guc_log_context(struct drm_printer *p, 5534 struct intel_context *ce) 5535 { 5536 drm_printf(p, "GuC lrc descriptor %u:\n", ce->guc_id.id); 5537 drm_printf(p, "\tHW Context Desc: 0x%08x\n", ce->lrc.lrca); 5538 if (intel_context_pin_if_active(ce)) { 5539 drm_printf(p, "\t\tLRC Head: Internal %u, Memory %u\n", 5540 ce->ring->head, 5541 ce->lrc_reg_state[CTX_RING_HEAD]); 5542 drm_printf(p, "\t\tLRC Tail: Internal %u, Memory %u\n", 5543 ce->ring->tail, 5544 ce->lrc_reg_state[CTX_RING_TAIL]); 5545 intel_context_unpin(ce); 5546 } else { 5547 drm_printf(p, "\t\tLRC Head: Internal %u, Memory not pinned\n", 5548 ce->ring->head); 5549 drm_printf(p, "\t\tLRC Tail: Internal %u, Memory not pinned\n", 5550 ce->ring->tail); 5551 } 5552 drm_printf(p, "\t\tContext Pin Count: %u\n", 5553 atomic_read(&ce->pin_count)); 5554 drm_printf(p, "\t\tGuC ID Ref Count: %u\n", 5555 atomic_read(&ce->guc_id.ref)); 5556 drm_printf(p, "\t\tSchedule State: 0x%x\n", 5557 ce->guc_state.sched_state); 5558 } 5559 5560 void intel_guc_submission_print_context_info(struct intel_guc *guc, 5561 struct drm_printer *p) 5562 { 5563 struct intel_context *ce; 5564 unsigned long index; 5565 unsigned long flags; 5566 5567 xa_lock_irqsave(&guc->context_lookup, flags); 5568 xa_for_each(&guc->context_lookup, index, ce) { 5569 GEM_BUG_ON(intel_context_is_child(ce)); 5570 5571 guc_log_context(p, ce); 5572 guc_log_context_priority(p, ce); 5573 5574 if (intel_context_is_parent(ce)) { 5575 struct intel_context *child; 5576 5577 drm_printf(p, "\t\tNumber children: %u\n", 5578 ce->parallel.number_children); 5579 5580 if (ce->parallel.guc.wq_status) { 5581 drm_printf(p, "\t\tWQI Head: %u\n", 5582 READ_ONCE(*ce->parallel.guc.wq_head)); 5583 drm_printf(p, "\t\tWQI Tail: %u\n", 5584 READ_ONCE(*ce->parallel.guc.wq_tail)); 5585 drm_printf(p, "\t\tWQI Status: %u\n", 5586 READ_ONCE(*ce->parallel.guc.wq_status)); 5587 } 5588 5589 if (ce->engine->emit_bb_start == 5590 emit_bb_start_parent_no_preempt_mid_batch) { 5591 u8 i; 5592 5593 drm_printf(p, "\t\tChildren Go: %u\n", 5594 get_children_go_value(ce)); 5595 for (i = 0; i < ce->parallel.number_children; ++i) 5596 drm_printf(p, "\t\tChildren Join: %u\n", 5597 get_children_join_value(ce, i)); 5598 } 5599 5600 for_each_child(ce, child) 5601 guc_log_context(p, child); 5602 } 5603 } 5604 xa_unlock_irqrestore(&guc->context_lookup, flags); 5605 } 5606 5607 static inline u32 get_children_go_addr(struct intel_context *ce) 5608 { 5609 GEM_BUG_ON(!intel_context_is_parent(ce)); 5610 5611 return i915_ggtt_offset(ce->state) + 5612 __get_parent_scratch_offset(ce) + 5613 offsetof(struct parent_scratch, go.semaphore); 5614 } 5615 5616 static inline u32 get_children_join_addr(struct intel_context *ce, 5617 u8 child_index) 5618 { 5619 GEM_BUG_ON(!intel_context_is_parent(ce)); 5620 5621 return i915_ggtt_offset(ce->state) + 5622 __get_parent_scratch_offset(ce) + 5623 offsetof(struct parent_scratch, join[child_index].semaphore); 5624 } 5625 5626 #define PARENT_GO_BB 1 5627 #define PARENT_GO_FINI_BREADCRUMB 0 5628 #define CHILD_GO_BB 1 5629 #define CHILD_GO_FINI_BREADCRUMB 0 5630 static int emit_bb_start_parent_no_preempt_mid_batch(struct i915_request *rq, 5631 u64 offset, u32 len, 5632 const unsigned int flags) 5633 { 5634 struct intel_context *ce = rq->context; 5635 u32 *cs; 5636 u8 i; 5637 5638 GEM_BUG_ON(!intel_context_is_parent(ce)); 5639 5640 cs = intel_ring_begin(rq, 10 + 4 * ce->parallel.number_children); 5641 if (IS_ERR(cs)) 5642 return PTR_ERR(cs); 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_BB; 5651 *cs++ = get_children_join_addr(ce, i); 5652 *cs++ = 0; 5653 } 5654 5655 /* Turn off preemption */ 5656 *cs++ = MI_ARB_ON_OFF | MI_ARB_DISABLE; 5657 *cs++ = MI_NOOP; 5658 5659 /* Tell children go */ 5660 cs = gen8_emit_ggtt_write(cs, 5661 CHILD_GO_BB, 5662 get_children_go_addr(ce), 5663 0); 5664 5665 /* Jump to batch */ 5666 *cs++ = MI_BATCH_BUFFER_START_GEN8 | 5667 (flags & I915_DISPATCH_SECURE ? 0 : BIT(8)); 5668 *cs++ = lower_32_bits(offset); 5669 *cs++ = upper_32_bits(offset); 5670 *cs++ = MI_NOOP; 5671 5672 intel_ring_advance(rq, cs); 5673 5674 return 0; 5675 } 5676 5677 static int emit_bb_start_child_no_preempt_mid_batch(struct i915_request *rq, 5678 u64 offset, u32 len, 5679 const unsigned int flags) 5680 { 5681 struct intel_context *ce = rq->context; 5682 struct intel_context *parent = intel_context_to_parent(ce); 5683 u32 *cs; 5684 5685 GEM_BUG_ON(!intel_context_is_child(ce)); 5686 5687 cs = intel_ring_begin(rq, 12); 5688 if (IS_ERR(cs)) 5689 return PTR_ERR(cs); 5690 5691 /* Signal parent */ 5692 cs = gen8_emit_ggtt_write(cs, 5693 PARENT_GO_BB, 5694 get_children_join_addr(parent, 5695 ce->parallel.child_index), 5696 0); 5697 5698 /* Wait on parent for go */ 5699 *cs++ = (MI_SEMAPHORE_WAIT | 5700 MI_SEMAPHORE_GLOBAL_GTT | 5701 MI_SEMAPHORE_POLL | 5702 MI_SEMAPHORE_SAD_EQ_SDD); 5703 *cs++ = CHILD_GO_BB; 5704 *cs++ = get_children_go_addr(parent); 5705 *cs++ = 0; 5706 5707 /* Turn off preemption */ 5708 *cs++ = MI_ARB_ON_OFF | MI_ARB_DISABLE; 5709 5710 /* Jump to batch */ 5711 *cs++ = MI_BATCH_BUFFER_START_GEN8 | 5712 (flags & I915_DISPATCH_SECURE ? 0 : BIT(8)); 5713 *cs++ = lower_32_bits(offset); 5714 *cs++ = upper_32_bits(offset); 5715 5716 intel_ring_advance(rq, cs); 5717 5718 return 0; 5719 } 5720 5721 static u32 * 5722 __emit_fini_breadcrumb_parent_no_preempt_mid_batch(struct i915_request *rq, 5723 u32 *cs) 5724 { 5725 struct intel_context *ce = rq->context; 5726 u8 i; 5727 5728 GEM_BUG_ON(!intel_context_is_parent(ce)); 5729 5730 /* Wait on children */ 5731 for (i = 0; i < ce->parallel.number_children; ++i) { 5732 *cs++ = (MI_SEMAPHORE_WAIT | 5733 MI_SEMAPHORE_GLOBAL_GTT | 5734 MI_SEMAPHORE_POLL | 5735 MI_SEMAPHORE_SAD_EQ_SDD); 5736 *cs++ = PARENT_GO_FINI_BREADCRUMB; 5737 *cs++ = get_children_join_addr(ce, i); 5738 *cs++ = 0; 5739 } 5740 5741 /* Turn on preemption */ 5742 *cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE; 5743 *cs++ = MI_NOOP; 5744 5745 /* Tell children go */ 5746 cs = gen8_emit_ggtt_write(cs, 5747 CHILD_GO_FINI_BREADCRUMB, 5748 get_children_go_addr(ce), 5749 0); 5750 5751 return cs; 5752 } 5753 5754 /* 5755 * If this true, a submission of multi-lrc requests had an error and the 5756 * requests need to be skipped. The front end (execuf IOCTL) should've called 5757 * i915_request_skip which squashes the BB but we still need to emit the fini 5758 * breadrcrumbs seqno write. At this point we don't know how many of the 5759 * requests in the multi-lrc submission were generated so we can't do the 5760 * handshake between the parent and children (e.g. if 4 requests should be 5761 * generated but 2nd hit an error only 1 would be seen by the GuC backend). 5762 * Simply skip the handshake, but still emit the breadcrumbd seqno, if an error 5763 * has occurred on any of the requests in submission / relationship. 5764 */ 5765 static inline bool skip_handshake(struct i915_request *rq) 5766 { 5767 return test_bit(I915_FENCE_FLAG_SKIP_PARALLEL, &rq->fence.flags); 5768 } 5769 5770 #define NON_SKIP_LEN 6 5771 static u32 * 5772 emit_fini_breadcrumb_parent_no_preempt_mid_batch(struct i915_request *rq, 5773 u32 *cs) 5774 { 5775 struct intel_context *ce = rq->context; 5776 __maybe_unused u32 *before_fini_breadcrumb_user_interrupt_cs; 5777 __maybe_unused u32 *start_fini_breadcrumb_cs = cs; 5778 5779 GEM_BUG_ON(!intel_context_is_parent(ce)); 5780 5781 if (unlikely(skip_handshake(rq))) { 5782 /* 5783 * NOP everything in __emit_fini_breadcrumb_parent_no_preempt_mid_batch, 5784 * the NON_SKIP_LEN comes from the length of the emits below. 5785 */ 5786 memset(cs, 0, sizeof(u32) * 5787 (ce->engine->emit_fini_breadcrumb_dw - NON_SKIP_LEN)); 5788 cs += ce->engine->emit_fini_breadcrumb_dw - NON_SKIP_LEN; 5789 } else { 5790 cs = __emit_fini_breadcrumb_parent_no_preempt_mid_batch(rq, cs); 5791 } 5792 5793 /* Emit fini breadcrumb */ 5794 before_fini_breadcrumb_user_interrupt_cs = cs; 5795 cs = gen8_emit_ggtt_write(cs, 5796 rq->fence.seqno, 5797 i915_request_active_timeline(rq)->hwsp_offset, 5798 0); 5799 5800 /* User interrupt */ 5801 *cs++ = MI_USER_INTERRUPT; 5802 *cs++ = MI_NOOP; 5803 5804 /* Ensure our math for skip + emit is correct */ 5805 GEM_BUG_ON(before_fini_breadcrumb_user_interrupt_cs + NON_SKIP_LEN != 5806 cs); 5807 GEM_BUG_ON(start_fini_breadcrumb_cs + 5808 ce->engine->emit_fini_breadcrumb_dw != cs); 5809 5810 rq->tail = intel_ring_offset(rq, cs); 5811 5812 return cs; 5813 } 5814 5815 static u32 * 5816 __emit_fini_breadcrumb_child_no_preempt_mid_batch(struct i915_request *rq, 5817 u32 *cs) 5818 { 5819 struct intel_context *ce = rq->context; 5820 struct intel_context *parent = intel_context_to_parent(ce); 5821 5822 GEM_BUG_ON(!intel_context_is_child(ce)); 5823 5824 /* Turn on preemption */ 5825 *cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE; 5826 *cs++ = MI_NOOP; 5827 5828 /* Signal parent */ 5829 cs = gen8_emit_ggtt_write(cs, 5830 PARENT_GO_FINI_BREADCRUMB, 5831 get_children_join_addr(parent, 5832 ce->parallel.child_index), 5833 0); 5834 5835 /* Wait parent on for go */ 5836 *cs++ = (MI_SEMAPHORE_WAIT | 5837 MI_SEMAPHORE_GLOBAL_GTT | 5838 MI_SEMAPHORE_POLL | 5839 MI_SEMAPHORE_SAD_EQ_SDD); 5840 *cs++ = CHILD_GO_FINI_BREADCRUMB; 5841 *cs++ = get_children_go_addr(parent); 5842 *cs++ = 0; 5843 5844 return cs; 5845 } 5846 5847 static u32 * 5848 emit_fini_breadcrumb_child_no_preempt_mid_batch(struct i915_request *rq, 5849 u32 *cs) 5850 { 5851 struct intel_context *ce = rq->context; 5852 __maybe_unused u32 *before_fini_breadcrumb_user_interrupt_cs; 5853 __maybe_unused u32 *start_fini_breadcrumb_cs = cs; 5854 5855 GEM_BUG_ON(!intel_context_is_child(ce)); 5856 5857 if (unlikely(skip_handshake(rq))) { 5858 /* 5859 * NOP everything in __emit_fini_breadcrumb_child_no_preempt_mid_batch, 5860 * the NON_SKIP_LEN comes from the length of the emits below. 5861 */ 5862 memset(cs, 0, sizeof(u32) * 5863 (ce->engine->emit_fini_breadcrumb_dw - NON_SKIP_LEN)); 5864 cs += ce->engine->emit_fini_breadcrumb_dw - NON_SKIP_LEN; 5865 } else { 5866 cs = __emit_fini_breadcrumb_child_no_preempt_mid_batch(rq, cs); 5867 } 5868 5869 /* Emit fini breadcrumb */ 5870 before_fini_breadcrumb_user_interrupt_cs = cs; 5871 cs = gen8_emit_ggtt_write(cs, 5872 rq->fence.seqno, 5873 i915_request_active_timeline(rq)->hwsp_offset, 5874 0); 5875 5876 /* User interrupt */ 5877 *cs++ = MI_USER_INTERRUPT; 5878 *cs++ = MI_NOOP; 5879 5880 /* Ensure our math for skip + emit is correct */ 5881 GEM_BUG_ON(before_fini_breadcrumb_user_interrupt_cs + NON_SKIP_LEN != 5882 cs); 5883 GEM_BUG_ON(start_fini_breadcrumb_cs + 5884 ce->engine->emit_fini_breadcrumb_dw != cs); 5885 5886 rq->tail = intel_ring_offset(rq, cs); 5887 5888 return cs; 5889 } 5890 5891 #undef NON_SKIP_LEN 5892 5893 static struct intel_context * 5894 guc_create_virtual(struct intel_engine_cs **siblings, unsigned int count, 5895 unsigned long flags) 5896 { 5897 struct guc_virtual_engine *ve; 5898 struct intel_guc *guc; 5899 unsigned int n; 5900 int err; 5901 5902 ve = kzalloc(sizeof(*ve), GFP_KERNEL); 5903 if (!ve) 5904 return ERR_PTR(-ENOMEM); 5905 5906 guc = gt_to_guc(siblings[0]->gt); 5907 5908 ve->base.i915 = siblings[0]->i915; 5909 ve->base.gt = siblings[0]->gt; 5910 ve->base.uncore = siblings[0]->uncore; 5911 ve->base.id = -1; 5912 5913 ve->base.uabi_class = I915_ENGINE_CLASS_INVALID; 5914 ve->base.instance = I915_ENGINE_CLASS_INVALID_VIRTUAL; 5915 ve->base.uabi_instance = I915_ENGINE_CLASS_INVALID_VIRTUAL; 5916 ve->base.saturated = ALL_ENGINES; 5917 5918 snprintf(ve->base.name, sizeof(ve->base.name), "virtual"); 5919 5920 ve->base.sched_engine = i915_sched_engine_get(guc->sched_engine); 5921 5922 ve->base.cops = &virtual_guc_context_ops; 5923 ve->base.request_alloc = guc_request_alloc; 5924 ve->base.bump_serial = virtual_guc_bump_serial; 5925 5926 ve->base.submit_request = guc_submit_request; 5927 5928 ve->base.flags = I915_ENGINE_IS_VIRTUAL; 5929 5930 BUILD_BUG_ON(ilog2(VIRTUAL_ENGINES) < I915_NUM_ENGINES); 5931 ve->base.mask = VIRTUAL_ENGINES; 5932 5933 intel_context_init(&ve->context, &ve->base); 5934 5935 for (n = 0; n < count; n++) { 5936 struct intel_engine_cs *sibling = siblings[n]; 5937 5938 GEM_BUG_ON(!is_power_of_2(sibling->mask)); 5939 if (sibling->mask & ve->base.mask) { 5940 guc_dbg(guc, "duplicate %s entry in load balancer\n", 5941 sibling->name); 5942 err = -EINVAL; 5943 goto err_put; 5944 } 5945 5946 ve->base.mask |= sibling->mask; 5947 ve->base.logical_mask |= sibling->logical_mask; 5948 5949 if (n != 0 && ve->base.class != sibling->class) { 5950 guc_dbg(guc, "invalid mixing of engine class, sibling %d, already %d\n", 5951 sibling->class, ve->base.class); 5952 err = -EINVAL; 5953 goto err_put; 5954 } else if (n == 0) { 5955 ve->base.class = sibling->class; 5956 ve->base.uabi_class = sibling->uabi_class; 5957 snprintf(ve->base.name, sizeof(ve->base.name), 5958 "v%dx%d", ve->base.class, count); 5959 ve->base.context_size = sibling->context_size; 5960 5961 ve->base.add_active_request = 5962 sibling->add_active_request; 5963 ve->base.remove_active_request = 5964 sibling->remove_active_request; 5965 ve->base.emit_bb_start = sibling->emit_bb_start; 5966 ve->base.emit_flush = sibling->emit_flush; 5967 ve->base.emit_init_breadcrumb = 5968 sibling->emit_init_breadcrumb; 5969 ve->base.emit_fini_breadcrumb = 5970 sibling->emit_fini_breadcrumb; 5971 ve->base.emit_fini_breadcrumb_dw = 5972 sibling->emit_fini_breadcrumb_dw; 5973 ve->base.breadcrumbs = 5974 intel_breadcrumbs_get(sibling->breadcrumbs); 5975 5976 ve->base.flags |= sibling->flags; 5977 5978 ve->base.props.timeslice_duration_ms = 5979 sibling->props.timeslice_duration_ms; 5980 ve->base.props.preempt_timeout_ms = 5981 sibling->props.preempt_timeout_ms; 5982 } 5983 } 5984 5985 return &ve->context; 5986 5987 err_put: 5988 intel_context_put(&ve->context); 5989 return ERR_PTR(err); 5990 } 5991 5992 bool intel_guc_virtual_engine_has_heartbeat(const struct intel_engine_cs *ve) 5993 { 5994 struct intel_engine_cs *engine; 5995 intel_engine_mask_t tmp, mask = ve->mask; 5996 5997 for_each_engine_masked(engine, ve->gt, mask, tmp) 5998 if (READ_ONCE(engine->props.heartbeat_interval_ms)) 5999 return true; 6000 6001 return false; 6002 } 6003 6004 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST) 6005 #include "selftest_guc.c" 6006 #include "selftest_guc_multi_lrc.c" 6007 #include "selftest_guc_hangcheck.c" 6008 #endif 6009