1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2022 Intel Corporation 4 */ 5 6 #include "xe_guc_submit.h" 7 8 #include <linux/bitfield.h> 9 #include <uapi/drm/xe_drm.h> 10 #include <linux/bitmap.h> 11 #include <linux/circ_buf.h> 12 #include <linux/dma-fence-array.h> 13 14 #include <drm/drm_drv.h> 15 #include <drm/drm_managed.h> 16 17 #include "abi/guc_actions_abi.h" 18 #include "abi/guc_actions_slpc_abi.h" 19 #include "abi/guc_klvs_abi.h" 20 #include "xe_assert.h" 21 #include "xe_bo.h" 22 #include "xe_cpu_bind.h" 23 #include "xe_devcoredump.h" 24 #include "xe_device.h" 25 #include "xe_exec_queue.h" 26 #include "xe_force_wake.h" 27 #include "xe_gpu_scheduler.h" 28 #include "xe_gt.h" 29 #include "xe_gt_clock.h" 30 #include "xe_gt_printk.h" 31 #include "xe_guc.h" 32 #include "xe_guc_capture.h" 33 #include "xe_guc_ct.h" 34 #include "xe_guc_exec_queue_types.h" 35 #include "xe_guc_id_mgr.h" 36 #include "xe_guc_klv_helpers.h" 37 #include "xe_guc_submit_types.h" 38 #include "xe_hw_engine.h" 39 #include "xe_log.h" 40 #include "xe_lrc.h" 41 #include "xe_macros.h" 42 #include "xe_map.h" 43 #include "xe_mocs.h" 44 #include "xe_module.h" 45 #include "xe_pm.h" 46 #include "xe_pt.h" 47 #include "xe_ring_ops_types.h" 48 #include "xe_sched_job.h" 49 #include "xe_sleep.h" 50 #include "xe_tlb_inval.h" 51 #include "xe_trace.h" 52 #include "xe_uc_fw.h" 53 #include "xe_vm.h" 54 55 #define XE_GUC_EXEC_QUEUE_CGP_CONTEXT_ERROR_LEN 6 56 57 static int guc_submit_reset_prepare(struct xe_guc *guc); 58 59 static struct xe_guc * 60 exec_queue_to_guc(struct xe_exec_queue *q) 61 { 62 return &q->gt->uc.guc; 63 } 64 65 /* 66 * Helpers for engine state, using an atomic as some of the bits can transition 67 * as the same time (e.g. a suspend can be happning at the same time as schedule 68 * engine done being processed). 69 */ 70 #define EXEC_QUEUE_STATE_REGISTERED (1 << 0) 71 #define EXEC_QUEUE_STATE_ENABLED (1 << 1) 72 #define EXEC_QUEUE_STATE_PENDING_ENABLE (1 << 2) 73 #define EXEC_QUEUE_STATE_PENDING_DISABLE (1 << 3) 74 #define EXEC_QUEUE_STATE_DESTROYED (1 << 4) 75 #define EXEC_QUEUE_STATE_SUSPENDED (1 << 5) 76 #define EXEC_QUEUE_STATE_RESET (1 << 6) 77 #define EXEC_QUEUE_STATE_KILLED (1 << 7) 78 #define EXEC_QUEUE_STATE_WEDGED (1 << 8) 79 #define EXEC_QUEUE_STATE_BANNED (1 << 9) 80 #define EXEC_QUEUE_STATE_PENDING_RESUME (1 << 10) 81 82 static bool exec_queue_registered(struct xe_exec_queue *q) 83 { 84 return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_REGISTERED; 85 } 86 87 static void set_exec_queue_registered(struct xe_exec_queue *q) 88 { 89 atomic_or(EXEC_QUEUE_STATE_REGISTERED, &q->guc->state); 90 } 91 92 static void clear_exec_queue_registered(struct xe_exec_queue *q) 93 { 94 atomic_and(~EXEC_QUEUE_STATE_REGISTERED, &q->guc->state); 95 } 96 97 static bool exec_queue_enabled(struct xe_exec_queue *q) 98 { 99 return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_ENABLED; 100 } 101 102 static void set_exec_queue_enabled(struct xe_exec_queue *q) 103 { 104 atomic_or(EXEC_QUEUE_STATE_ENABLED, &q->guc->state); 105 } 106 107 static void clear_exec_queue_enabled(struct xe_exec_queue *q) 108 { 109 atomic_and(~EXEC_QUEUE_STATE_ENABLED, &q->guc->state); 110 } 111 112 static bool exec_queue_pending_enable(struct xe_exec_queue *q) 113 { 114 return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_PENDING_ENABLE; 115 } 116 117 static void set_exec_queue_pending_enable(struct xe_exec_queue *q) 118 { 119 atomic_or(EXEC_QUEUE_STATE_PENDING_ENABLE, &q->guc->state); 120 } 121 122 static void clear_exec_queue_pending_enable(struct xe_exec_queue *q) 123 { 124 atomic_and(~EXEC_QUEUE_STATE_PENDING_ENABLE, &q->guc->state); 125 } 126 127 static bool exec_queue_pending_disable(struct xe_exec_queue *q) 128 { 129 return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_PENDING_DISABLE; 130 } 131 132 static void set_exec_queue_pending_disable(struct xe_exec_queue *q) 133 { 134 atomic_or(EXEC_QUEUE_STATE_PENDING_DISABLE, &q->guc->state); 135 } 136 137 static void clear_exec_queue_pending_disable(struct xe_exec_queue *q) 138 { 139 atomic_and(~EXEC_QUEUE_STATE_PENDING_DISABLE, &q->guc->state); 140 } 141 142 static bool exec_queue_destroyed(struct xe_exec_queue *q) 143 { 144 return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_DESTROYED; 145 } 146 147 static void set_exec_queue_destroyed(struct xe_exec_queue *q) 148 { 149 atomic_or(EXEC_QUEUE_STATE_DESTROYED, &q->guc->state); 150 } 151 152 static void clear_exec_queue_destroyed(struct xe_exec_queue *q) 153 { 154 atomic_and(~EXEC_QUEUE_STATE_DESTROYED, &q->guc->state); 155 } 156 157 static bool exec_queue_banned(struct xe_exec_queue *q) 158 { 159 return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_BANNED; 160 } 161 162 static void set_exec_queue_banned(struct xe_exec_queue *q) 163 { 164 atomic_or(EXEC_QUEUE_STATE_BANNED, &q->guc->state); 165 } 166 167 static void clear_exec_queue_banned(struct xe_exec_queue *q) 168 { 169 atomic_andnot(EXEC_QUEUE_STATE_BANNED, &q->guc->state); 170 } 171 172 static bool exec_queue_suspended(struct xe_exec_queue *q) 173 { 174 return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_SUSPENDED; 175 } 176 177 static void set_exec_queue_suspended(struct xe_exec_queue *q) 178 { 179 atomic_or(EXEC_QUEUE_STATE_SUSPENDED, &q->guc->state); 180 } 181 182 static void clear_exec_queue_suspended(struct xe_exec_queue *q) 183 { 184 atomic_and(~EXEC_QUEUE_STATE_SUSPENDED, &q->guc->state); 185 } 186 187 static bool exec_queue_reset(struct xe_exec_queue *q) 188 { 189 return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_RESET; 190 } 191 192 static void set_exec_queue_reset(struct xe_exec_queue *q) 193 { 194 atomic_or(EXEC_QUEUE_STATE_RESET, &q->guc->state); 195 } 196 197 static bool exec_queue_killed(struct xe_exec_queue *q) 198 { 199 return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_KILLED; 200 } 201 202 static void set_exec_queue_killed(struct xe_exec_queue *q) 203 { 204 atomic_or(EXEC_QUEUE_STATE_KILLED, &q->guc->state); 205 } 206 207 static bool exec_queue_wedged(struct xe_exec_queue *q) 208 { 209 return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_WEDGED; 210 } 211 212 static void set_exec_queue_wedged(struct xe_exec_queue *q) 213 { 214 atomic_or(EXEC_QUEUE_STATE_WEDGED, &q->guc->state); 215 } 216 217 static bool exec_queue_pending_resume(struct xe_exec_queue *q) 218 { 219 return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_PENDING_RESUME; 220 } 221 222 static void set_exec_queue_pending_resume(struct xe_exec_queue *q) 223 { 224 atomic_or(EXEC_QUEUE_STATE_PENDING_RESUME, &q->guc->state); 225 } 226 227 static void clear_exec_queue_pending_resume(struct xe_exec_queue *q) 228 { 229 atomic_and(~EXEC_QUEUE_STATE_PENDING_RESUME, &q->guc->state); 230 } 231 232 static bool exec_queue_killed_or_banned_or_wedged(struct xe_exec_queue *q) 233 { 234 return (atomic_read(&q->guc->state) & 235 (EXEC_QUEUE_STATE_WEDGED | EXEC_QUEUE_STATE_KILLED | 236 EXEC_QUEUE_STATE_BANNED)); 237 } 238 239 static void guc_submit_sw_fini(struct drm_device *drm, void *arg) 240 { 241 struct xe_guc *guc = arg; 242 struct xe_gt *gt = guc_to_gt(guc); 243 244 xe_gt_assert(gt, xa_empty(&guc->submission_state.exec_queue_lookup)); 245 246 xa_destroy(&guc->submission_state.exec_queue_lookup); 247 } 248 249 static void guc_submit_fini(void *arg) 250 { 251 struct xe_guc *guc = arg; 252 struct xe_exec_queue *q; 253 unsigned long index; 254 255 /* Drop any wedged queue refs */ 256 mutex_lock(&guc->submission_state.lock); 257 xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) { 258 if (exec_queue_wedged(q)) { 259 mutex_unlock(&guc->submission_state.lock); 260 xe_exec_queue_put(q); 261 mutex_lock(&guc->submission_state.lock); 262 } 263 } 264 mutex_unlock(&guc->submission_state.lock); 265 266 /* Forcefully kill any remaining exec queues */ 267 xe_guc_ct_stop(&guc->ct); 268 xe_tlb_inval_reset(&guc_to_gt(guc)->tlb_inval); 269 guc_submit_reset_prepare(guc); 270 xe_guc_softreset(guc); 271 xe_guc_submit_stop(guc); 272 xe_uc_fw_sanitize(&guc->fw); 273 xe_guc_submit_pause_abort(guc); 274 } 275 276 static const struct xe_exec_queue_ops guc_exec_queue_ops; 277 278 static void primelockdep(struct xe_guc *guc) 279 { 280 if (!IS_ENABLED(CONFIG_LOCKDEP)) 281 return; 282 283 fs_reclaim_acquire(GFP_KERNEL); 284 285 mutex_lock(&guc->submission_state.lock); 286 mutex_unlock(&guc->submission_state.lock); 287 288 fs_reclaim_release(GFP_KERNEL); 289 } 290 291 /** 292 * xe_guc_submit_init() - Initialize GuC submission. 293 * @guc: the &xe_guc to initialize 294 * @num_ids: number of GuC context IDs to use 295 * 296 * The bare-metal or PF driver can pass ~0 as &num_ids to indicate that all 297 * GuC context IDs supported by the GuC firmware should be used for submission. 298 * 299 * Only VF drivers will have to provide explicit number of GuC context IDs 300 * that they can use for submission. 301 * 302 * Return: 0 on success or a negative error code on failure. 303 */ 304 int xe_guc_submit_init(struct xe_guc *guc, unsigned int num_ids) 305 { 306 struct xe_device *xe = guc_to_xe(guc); 307 struct xe_gt *gt = guc_to_gt(guc); 308 int err; 309 310 err = drmm_mutex_init(&xe->drm, &guc->submission_state.lock); 311 if (err) 312 return err; 313 314 err = xe_guc_id_mgr_init(&guc->submission_state.idm, num_ids); 315 if (err) 316 return err; 317 318 gt->exec_queue_ops = &guc_exec_queue_ops; 319 320 xa_init(&guc->submission_state.exec_queue_lookup); 321 322 primelockdep(guc); 323 324 guc->submission_state.initialized = true; 325 326 err = drmm_add_action_or_reset(&xe->drm, guc_submit_sw_fini, guc); 327 if (err) 328 return err; 329 330 return devm_add_action_or_reset(xe->drm.dev, guc_submit_fini, guc); 331 } 332 333 /* 334 * Given that we want to guarantee enough RCS throughput to avoid missing 335 * frames, we set the yield policy to 20% of each 80ms interval. 336 */ 337 #define RC_YIELD_DURATION 80 /* in ms */ 338 #define RC_YIELD_RATIO 20 /* in percent */ 339 static u32 *emit_render_compute_yield_klv(u32 *emit) 340 { 341 *emit++ = PREP_GUC_KLV_TAG(SCHEDULING_POLICIES_RENDER_COMPUTE_YIELD); 342 *emit++ = RC_YIELD_DURATION; 343 *emit++ = RC_YIELD_RATIO; 344 345 return emit; 346 } 347 348 #define SCHEDULING_POLICY_MAX_DWORDS 16 349 static int guc_init_global_schedule_policy(struct xe_guc *guc) 350 { 351 u32 data[SCHEDULING_POLICY_MAX_DWORDS]; 352 u32 *emit = data; 353 u32 count = 0; 354 int ret; 355 356 if (GUC_SUBMIT_VER(guc) < MAKE_GUC_VER(1, 1, 0)) 357 return 0; 358 359 *emit++ = XE_GUC_ACTION_UPDATE_SCHEDULING_POLICIES_KLV; 360 361 if (CCS_INSTANCES(guc_to_gt(guc))) 362 emit = emit_render_compute_yield_klv(emit); 363 364 count = emit - data; 365 if (count > 1) { 366 xe_assert(guc_to_xe(guc), count <= SCHEDULING_POLICY_MAX_DWORDS); 367 368 ret = xe_guc_ct_send_block(&guc->ct, data, count); 369 if (ret < 0) { 370 xe_gt_err(guc_to_gt(guc), 371 "failed to enable GuC scheduling policies: %pe\n", 372 ERR_PTR(ret)); 373 return ret; 374 } 375 } 376 377 return 0; 378 } 379 380 int xe_guc_submit_enable(struct xe_guc *guc) 381 { 382 int ret; 383 384 ret = guc_init_global_schedule_policy(guc); 385 if (ret) 386 return ret; 387 388 guc->submission_state.enabled = true; 389 390 return 0; 391 } 392 393 void xe_guc_submit_disable(struct xe_guc *guc) 394 { 395 guc->submission_state.enabled = false; 396 } 397 398 static void __release_guc_id(struct xe_guc *guc, struct xe_exec_queue *q, 399 int count) 400 { 401 int i; 402 403 mutex_lock(&guc->submission_state.lock); 404 405 for (i = 0; i < count; ++i) 406 xa_erase(&guc->submission_state.exec_queue_lookup, 407 q->guc->id + i); 408 409 xe_guc_id_mgr_release_locked(&guc->submission_state.idm, 410 q->guc->id, q->width); 411 412 mutex_unlock(&guc->submission_state.lock); 413 } 414 415 static int alloc_guc_id(struct xe_guc *guc, struct xe_exec_queue *q) 416 { 417 int ret, i; 418 419 mutex_lock(&guc->submission_state.lock); 420 ret = xe_guc_id_mgr_reserve_locked(&guc->submission_state.idm, 421 q->width); 422 mutex_unlock(&guc->submission_state.lock); 423 if (ret < 0) 424 return ret; 425 426 q->guc->id = ret; 427 428 /* Reserve empty slots. */ 429 for (i = 0; i < q->width; ++i) { 430 ret = xa_insert(&guc->submission_state.exec_queue_lookup, 431 q->guc->id + i, NULL, GFP_KERNEL); 432 if (ret) 433 goto err_release; 434 } 435 436 return 0; 437 438 err_release: 439 __release_guc_id(guc, q, i); 440 441 return ret; 442 } 443 444 static void publish_guc_id(struct xe_guc *guc, struct xe_exec_queue *q) 445 { 446 int i; 447 448 lockdep_assert_held(&guc->submission_state.lock); 449 450 for (i = 0; i < q->width; ++i) { 451 void *old; 452 453 old = xa_store(&guc->submission_state.exec_queue_lookup, 454 q->guc->id + i, q, GFP_NOWAIT); 455 XE_WARN_ON(old || xa_is_err(old)); 456 } 457 } 458 459 static void release_guc_id(struct xe_guc *guc, struct xe_exec_queue *q) 460 { 461 __release_guc_id(guc, q, q->width); 462 } 463 464 struct exec_queue_policy { 465 u32 count; 466 struct guc_update_exec_queue_policy h2g; 467 }; 468 469 static u32 __guc_exec_queue_policy_action_size(struct exec_queue_policy *policy) 470 { 471 size_t bytes = sizeof(policy->h2g.header) + 472 (sizeof(policy->h2g.klv[0]) * policy->count); 473 474 return bytes / sizeof(u32); 475 } 476 477 static void __guc_exec_queue_policy_start_klv(struct exec_queue_policy *policy, 478 u16 guc_id) 479 { 480 policy->h2g.header.action = 481 XE_GUC_ACTION_HOST2GUC_UPDATE_CONTEXT_POLICIES; 482 policy->h2g.header.guc_id = guc_id; 483 policy->count = 0; 484 } 485 486 #define MAKE_EXEC_QUEUE_POLICY_ADD(func, id) \ 487 static void __guc_exec_queue_policy_add_##func(struct exec_queue_policy *policy, \ 488 u32 data) \ 489 { \ 490 XE_WARN_ON(policy->count >= GUC_CONTEXT_POLICIES_KLV_NUM_IDS); \ 491 \ 492 policy->h2g.klv[policy->count].kl = \ 493 FIELD_PREP(GUC_KLV_0_KEY, \ 494 GUC_CONTEXT_POLICIES_KLV_ID_##id) | \ 495 FIELD_PREP(GUC_KLV_0_LEN, 1); \ 496 policy->h2g.klv[policy->count].value = data; \ 497 policy->count++; \ 498 } 499 500 MAKE_EXEC_QUEUE_POLICY_ADD(execution_quantum, EXECUTION_QUANTUM) 501 MAKE_EXEC_QUEUE_POLICY_ADD(preemption_timeout, PREEMPTION_TIMEOUT) 502 MAKE_EXEC_QUEUE_POLICY_ADD(priority, SCHEDULING_PRIORITY) 503 MAKE_EXEC_QUEUE_POLICY_ADD(slpc_exec_queue_freq_req, SLPM_GT_FREQUENCY) 504 #undef MAKE_EXEC_QUEUE_POLICY_ADD 505 506 static const int xe_exec_queue_prio_to_guc[] = { 507 [XE_EXEC_QUEUE_PRIORITY_LOW] = GUC_CLIENT_PRIORITY_NORMAL, 508 [XE_EXEC_QUEUE_PRIORITY_NORMAL] = GUC_CLIENT_PRIORITY_KMD_NORMAL, 509 [XE_EXEC_QUEUE_PRIORITY_HIGH] = GUC_CLIENT_PRIORITY_HIGH, 510 [XE_EXEC_QUEUE_PRIORITY_KERNEL] = GUC_CLIENT_PRIORITY_KMD_HIGH, 511 }; 512 513 static void init_policies(struct xe_guc *guc, struct xe_exec_queue *q) 514 { 515 struct exec_queue_policy policy; 516 enum xe_exec_queue_priority prio = q->sched_props.priority; 517 u32 timeslice_us = q->sched_props.timeslice_us; 518 u32 slpc_exec_queue_freq_req = 0; 519 u32 preempt_timeout_us = q->sched_props.preempt_timeout_us; 520 521 xe_gt_assert(guc_to_gt(guc), exec_queue_registered(q) && 522 !xe_exec_queue_is_multi_queue_secondary(q)); 523 524 if (q->flags & EXEC_QUEUE_FLAG_LOW_LATENCY) 525 slpc_exec_queue_freq_req |= SLPC_CTX_FREQ_REQ_IS_COMPUTE; 526 527 __guc_exec_queue_policy_start_klv(&policy, q->guc->id); 528 __guc_exec_queue_policy_add_priority(&policy, xe_exec_queue_prio_to_guc[prio]); 529 __guc_exec_queue_policy_add_execution_quantum(&policy, timeslice_us); 530 __guc_exec_queue_policy_add_preemption_timeout(&policy, preempt_timeout_us); 531 __guc_exec_queue_policy_add_slpc_exec_queue_freq_req(&policy, 532 slpc_exec_queue_freq_req); 533 534 xe_guc_ct_send(&guc->ct, (u32 *)&policy.h2g, 535 __guc_exec_queue_policy_action_size(&policy), 0, 0); 536 } 537 538 static void set_min_preemption_timeout(struct xe_guc *guc, struct xe_exec_queue *q) 539 { 540 struct exec_queue_policy policy; 541 542 xe_assert(guc_to_xe(guc), !xe_exec_queue_is_multi_queue_secondary(q)); 543 544 __guc_exec_queue_policy_start_klv(&policy, q->guc->id); 545 __guc_exec_queue_policy_add_preemption_timeout(&policy, 1); 546 547 xe_guc_ct_send(&guc->ct, (u32 *)&policy.h2g, 548 __guc_exec_queue_policy_action_size(&policy), 0, 0); 549 } 550 551 static bool vf_recovery(struct xe_guc *guc) 552 { 553 return xe_gt_recovery_pending(guc_to_gt(guc)); 554 } 555 556 static void xe_guc_exec_queue_trigger_cleanup(struct xe_exec_queue *q) 557 { 558 struct xe_guc *guc = exec_queue_to_guc(q); 559 struct xe_device *xe = guc_to_xe(guc); 560 561 /** to wakeup xe_wait_user_fence ioctl if exec queue is reset */ 562 wake_up_all(&xe->ufence_wq); 563 564 xe_sched_tdr_queue_imm(&q->guc->sched); 565 } 566 567 static void xe_guc_exec_queue_group_stop(struct xe_exec_queue *q) 568 { 569 struct xe_exec_queue *primary = xe_exec_queue_multi_queue_primary(q); 570 struct xe_exec_queue_group *group = q->multi_queue.group; 571 struct xe_exec_queue *eq, *next; 572 LIST_HEAD(tmp); 573 574 xe_gt_assert(guc_to_gt(exec_queue_to_guc(q)), 575 xe_exec_queue_is_multi_queue(q)); 576 577 mutex_lock(&group->list_lock); 578 579 /* 580 * Stop all future queues being from executing while group is stopped. 581 */ 582 group->stopped = true; 583 584 list_for_each_entry_safe(eq, next, &group->list, multi_queue.link) 585 /* 586 * Refcount prevents an attempted removal from &group->list, 587 * temporary list allows safe iteration after dropping 588 * &group->list_lock. 589 */ 590 if (xe_exec_queue_get_unless_zero(eq)) 591 list_move_tail(&eq->multi_queue.link, &tmp); 592 593 mutex_unlock(&group->list_lock); 594 595 /* We cannot stop under list lock without getting inversions */ 596 xe_sched_submission_stop(&primary->guc->sched); 597 list_for_each_entry(eq, &tmp, multi_queue.link) 598 xe_sched_submission_stop(&eq->guc->sched); 599 600 mutex_lock(&group->list_lock); 601 list_for_each_entry_safe(eq, next, &tmp, multi_queue.link) { 602 /* 603 * Corner where we got banned while stopping and not on 604 * &group->list 605 */ 606 if (READ_ONCE(group->banned)) 607 xe_guc_exec_queue_trigger_cleanup(eq); 608 609 list_move_tail(&eq->multi_queue.link, &group->list); 610 xe_exec_queue_put(eq); 611 } 612 mutex_unlock(&group->list_lock); 613 } 614 615 static void xe_guc_exec_queue_group_start(struct xe_exec_queue *q) 616 { 617 struct xe_exec_queue *primary = xe_exec_queue_multi_queue_primary(q); 618 struct xe_exec_queue_group *group = q->multi_queue.group; 619 struct xe_exec_queue *eq; 620 621 xe_gt_assert(guc_to_gt(exec_queue_to_guc(q)), 622 xe_exec_queue_is_multi_queue(q)); 623 624 xe_sched_submission_start(&primary->guc->sched); 625 626 mutex_lock(&group->list_lock); 627 group->stopped = false; 628 list_for_each_entry(eq, &group->list, multi_queue.link) 629 xe_sched_submission_start(&eq->guc->sched); 630 mutex_unlock(&group->list_lock); 631 } 632 633 static void xe_guc_exec_queue_group_trigger_cleanup(struct xe_exec_queue *q) 634 { 635 struct xe_exec_queue *primary = xe_exec_queue_multi_queue_primary(q); 636 struct xe_exec_queue_group *group = q->multi_queue.group; 637 struct xe_exec_queue *eq; 638 639 xe_gt_assert(guc_to_gt(exec_queue_to_guc(q)), 640 xe_exec_queue_is_multi_queue(q)); 641 642 /* Group banned, skip timeout check in TDR */ 643 WRITE_ONCE(group->banned, true); 644 xe_guc_exec_queue_trigger_cleanup(primary); 645 646 mutex_lock(&group->list_lock); 647 list_for_each_entry(eq, &group->list, multi_queue.link) 648 xe_guc_exec_queue_trigger_cleanup(eq); 649 mutex_unlock(&group->list_lock); 650 } 651 652 static void xe_guc_exec_queue_reset_trigger_cleanup(struct xe_exec_queue *q) 653 { 654 if (xe_exec_queue_is_multi_queue(q)) { 655 struct xe_exec_queue *primary = xe_exec_queue_multi_queue_primary(q); 656 struct xe_exec_queue_group *group = q->multi_queue.group; 657 struct xe_exec_queue *eq; 658 659 /* Group banned, skip timeout check in TDR */ 660 WRITE_ONCE(group->banned, true); 661 662 set_exec_queue_reset(primary); 663 if (!exec_queue_banned(primary)) 664 xe_guc_exec_queue_trigger_cleanup(primary); 665 666 mutex_lock(&group->list_lock); 667 list_for_each_entry(eq, &group->list, multi_queue.link) { 668 set_exec_queue_reset(eq); 669 if (!exec_queue_banned(eq)) 670 xe_guc_exec_queue_trigger_cleanup(eq); 671 } 672 mutex_unlock(&group->list_lock); 673 } else { 674 set_exec_queue_reset(q); 675 if (!exec_queue_banned(q)) 676 xe_guc_exec_queue_trigger_cleanup(q); 677 } 678 } 679 680 static void set_exec_queue_group_banned(struct xe_exec_queue *q) 681 { 682 struct xe_exec_queue *primary = xe_exec_queue_multi_queue_primary(q); 683 struct xe_exec_queue_group *group = q->multi_queue.group; 684 struct xe_exec_queue *eq; 685 686 /* Ban all queues of the multi-queue group */ 687 xe_gt_assert(guc_to_gt(exec_queue_to_guc(q)), 688 xe_exec_queue_is_multi_queue(q)); 689 set_exec_queue_banned(primary); 690 691 mutex_lock(&group->list_lock); 692 list_for_each_entry(eq, &group->list, multi_queue.link) 693 set_exec_queue_banned(eq); 694 mutex_unlock(&group->list_lock); 695 } 696 697 /* Helper for context registration H2G */ 698 struct guc_ctxt_registration_info { 699 u32 flags; 700 u32 context_idx; 701 u32 engine_class; 702 u32 engine_submit_mask; 703 u32 wq_desc_lo; 704 u32 wq_desc_hi; 705 u32 wq_base_lo; 706 u32 wq_base_hi; 707 u32 wq_size; 708 u32 cgp_lo; 709 u32 cgp_hi; 710 u32 hwlrca_lo; 711 u32 hwlrca_hi; 712 }; 713 714 #define parallel_read(xe_, map_, field_) \ 715 xe_map_rd_field(xe_, &map_, 0, struct guc_submit_parallel_scratch, \ 716 field_) 717 #define parallel_write(xe_, map_, field_, val_) \ 718 xe_map_wr_field(xe_, &map_, 0, struct guc_submit_parallel_scratch, \ 719 field_, val_) 720 721 /** 722 * DOC: Multi Queue Group GuC interface 723 * 724 * The multi queue group coordination between KMD and GuC is through a software 725 * construct called Context Group Page (CGP). The CGP is a KMD managed 4KB page 726 * allocated in the global GTT. 727 * 728 * CGP format: 729 * 730 * +-----------+---------------------------+---------------------------------------------+ 731 * | DWORD | Name | Description | 732 * +-----------+---------------------------+---------------------------------------------+ 733 * | 0 | Version | Bits [15:8]=Major ver, [7:0]=Minor ver | 734 * +-----------+---------------------------+---------------------------------------------+ 735 * | 1..15 | RESERVED | MBZ | 736 * +-----------+---------------------------+---------------------------------------------+ 737 * | 16 | KMD_QUEUE_UPDATE_MASK_DW0 | KMD queue mask for queues 31..0 | 738 * +-----------+---------------------------+---------------------------------------------+ 739 * | 17 | KMD_QUEUE_UPDATE_MASK_DW1 | KMD queue mask for queues 63..32 | 740 * +-----------+---------------------------+---------------------------------------------+ 741 * | 18..31 | RESERVED | MBZ | 742 * +-----------+---------------------------+---------------------------------------------+ 743 * | 32 | Q0CD_DW0 | Queue 0 context LRC descriptor lower DWORD | 744 * +-----------+---------------------------+---------------------------------------------+ 745 * | 33 | Q0ContextIndex | Context ID for Queue 0 | 746 * +-----------+---------------------------+---------------------------------------------+ 747 * | 34 | Q1CD_DW0 | Queue 1 context LRC descriptor lower DWORD | 748 * +-----------+---------------------------+---------------------------------------------+ 749 * | 35 | Q1ContextIndex | Context ID for Queue 1 | 750 * +-----------+---------------------------+---------------------------------------------+ 751 * | ... |... | ... | 752 * +-----------+---------------------------+---------------------------------------------+ 753 * | 158 | Q63CD_DW0 | Queue 63 context LRC descriptor lower DWORD | 754 * +-----------+---------------------------+---------------------------------------------+ 755 * | 159 | Q63ContextIndex | Context ID for Queue 63 | 756 * +-----------+---------------------------+---------------------------------------------+ 757 * | 160..1024 | RESERVED | MBZ | 758 * +-----------+---------------------------+---------------------------------------------+ 759 * 760 * While registering Q0 with GuC, CGP is updated with Q0 entry and GuC is notified 761 * through XE_GUC_ACTION_REGISTER_CONTEXT_MULTI_QUEUE H2G message which specifies 762 * the CGP address. When the secondary queues are added to the group, the CGP is 763 * updated with entry for that queue and GuC is notified through the H2G interface 764 * XE_GUC_ACTION_MULTI_QUEUE_CONTEXT_CGP_SYNC. GuC responds to these H2G messages 765 * with a XE_GUC_ACTION_NOTIFY_MULTIQ_CONTEXT_CGP_SYNC_DONE G2H message. GuC also 766 * sends a XE_GUC_ACTION_NOTIFY_MULTI_QUEUE_CGP_CONTEXT_ERROR notification for any 767 * error in the CGP. Only one of these CGP update messages can be outstanding 768 * (waiting for GuC response) at any time. The bits in KMD_QUEUE_UPDATE_MASK_DW* 769 * fields indicate which queue entry is being updated in the CGP. 770 * 771 * The primary queue (Q0) represents the multi queue group context in GuC and 772 * submission on any queue of the group must be through Q0 GuC interface only. 773 * 774 * As it is not required to register secondary queues with GuC, the secondary queue 775 * context ids in the CGP are populated with Q0 context id. 776 */ 777 778 #define CGP_VERSION_MAJOR_SHIFT 8 779 780 static void xe_guc_exec_queue_group_cgp_update(struct xe_device *xe, 781 struct xe_exec_queue *q) 782 { 783 struct xe_exec_queue_group *group = q->multi_queue.group; 784 u32 guc_id = group->primary->guc->id; 785 786 /* Currently implementing CGP version 1.0 */ 787 xe_map_wr(xe, &group->cgp_bo->vmap, 0, u32, 788 1 << CGP_VERSION_MAJOR_SHIFT); 789 790 xe_map_wr(xe, &group->cgp_bo->vmap, 791 (32 + q->multi_queue.pos * 2) * sizeof(u32), 792 u32, lower_32_bits(xe_lrc_descriptor(q->lrc[0]))); 793 794 xe_map_wr(xe, &group->cgp_bo->vmap, 795 (33 + q->multi_queue.pos * 2) * sizeof(u32), 796 u32, guc_id); 797 798 if (q->multi_queue.pos / 32) { 799 xe_map_wr(xe, &group->cgp_bo->vmap, 17 * sizeof(u32), 800 u32, BIT(q->multi_queue.pos % 32)); 801 xe_map_wr(xe, &group->cgp_bo->vmap, 16 * sizeof(u32), u32, 0); 802 } else { 803 xe_map_wr(xe, &group->cgp_bo->vmap, 16 * sizeof(u32), 804 u32, BIT(q->multi_queue.pos)); 805 xe_map_wr(xe, &group->cgp_bo->vmap, 17 * sizeof(u32), u32, 0); 806 } 807 } 808 809 #define CGP_SYNC_REGISTRATION BIT(0) 810 811 static void xe_guc_exec_queue_group_cgp_sync(struct xe_guc *guc, 812 struct xe_exec_queue *q, 813 const u32 *action, u32 len, 814 unsigned int flags) 815 { 816 struct xe_exec_queue_group *group = q->multi_queue.group; 817 struct xe_device *xe = guc_to_xe(guc); 818 enum xe_multi_queue_priority priority; 819 long ret; 820 821 /* 822 * As all queues of a multi queue group use single drm scheduler 823 * submit workqueue, CGP synchronization with GuC are serialized. 824 * Hence, no locking is required here. 825 * Wait for any pending CGP_SYNC_DONE response before updating the 826 * CGP page and sending CGP_SYNC message. 827 */ 828 ret = wait_event_timeout(guc->ct.wq, 829 !READ_ONCE(group->sync_pending) || 830 xe_guc_read_stopped(guc) || vf_recovery(guc), 831 HZ); 832 if ((!ret && !vf_recovery(guc)) || xe_guc_read_stopped(guc)) { 833 /* CGP_SYNC failed. Reset gt, cleanup the group */ 834 xe_gt_warn(guc_to_gt(guc), "Wait for CGP_SYNC_DONE response failed!\n"); 835 set_exec_queue_group_banned(q); 836 xe_gt_reset_async(q->gt); 837 xe_guc_exec_queue_group_trigger_cleanup(q); 838 return; 839 } 840 841 /* 842 * If woken by VF migration recovery, do not touch the CGP or send: the 843 * message would be lost and, for a registration, GuC must (re-)register 844 * the context before its CGP entry may be read. Flag the queue so revert 845 * replays it - a registration by re-registration, a dynamic update by a 846 * replayed CGP_SYNC - and bail. 847 */ 848 if (vf_recovery(guc)) { 849 if (flags & CGP_SYNC_REGISTRATION) 850 q->guc->multi_queue.re_register = true; 851 else 852 q->guc->multi_queue.re_update = true; 853 return; 854 } 855 856 scoped_guard(spinlock, &q->multi_queue.lock) 857 priority = q->multi_queue.priority; 858 859 xe_lrc_set_multi_queue_priority(q->lrc[0], priority); 860 xe_guc_exec_queue_group_cgp_update(xe, q); 861 862 /* 863 * Record the nature of this outstanding sync so revert can replay it if 864 * its CGP_SYNC_DONE is lost across a migration: a registration is 865 * recovered by re-registration, a dynamic update by a replayed CGP_SYNC. 866 */ 867 if (flags & CGP_SYNC_REGISTRATION) { 868 q->guc->multi_queue.registering_cgp = true; 869 q->guc->multi_queue.updating_cgp = false; 870 } else { 871 q->guc->multi_queue.updating_cgp = true; 872 q->guc->multi_queue.registering_cgp = false; 873 } 874 WRITE_ONCE(group->cgp_update_q, q); 875 WRITE_ONCE(group->sync_pending, true); 876 xe_guc_ct_send(&guc->ct, action, len, G2H_LEN_DW_MULTI_QUEUE_CONTEXT, 1); 877 } 878 879 static void guc_exec_queue_send_cgp_sync(struct xe_exec_queue *q, unsigned int flags) 880 { 881 #define MAX_MULTI_QUEUE_CGP_SYNC_SIZE (2) 882 struct xe_guc *guc = exec_queue_to_guc(q); 883 struct xe_exec_queue_group *group = q->multi_queue.group; 884 u32 action[MAX_MULTI_QUEUE_CGP_SYNC_SIZE]; 885 int len = 0; 886 887 action[len++] = XE_GUC_ACTION_MULTI_QUEUE_CONTEXT_CGP_SYNC; 888 action[len++] = group->primary->guc->id; 889 890 xe_gt_assert(guc_to_gt(guc), len <= MAX_MULTI_QUEUE_CGP_SYNC_SIZE); 891 #undef MAX_MULTI_QUEUE_CGP_SYNC_SIZE 892 893 xe_guc_exec_queue_group_cgp_sync(guc, q, action, len, flags); 894 } 895 896 static void __register_exec_queue_group(struct xe_exec_queue *q, 897 struct guc_ctxt_registration_info *info) 898 { 899 struct xe_guc *guc = exec_queue_to_guc(q); 900 #define MAX_MULTI_QUEUE_REG_SIZE (8) 901 u32 action[MAX_MULTI_QUEUE_REG_SIZE]; 902 int len = 0; 903 904 action[len++] = XE_GUC_ACTION_REGISTER_CONTEXT_MULTI_QUEUE; 905 action[len++] = info->flags; 906 action[len++] = info->context_idx; 907 action[len++] = info->engine_class; 908 action[len++] = info->engine_submit_mask; 909 action[len++] = 0; /* Reserved */ 910 action[len++] = info->cgp_lo; 911 action[len++] = info->cgp_hi; 912 913 xe_gt_assert(guc_to_gt(guc), len <= MAX_MULTI_QUEUE_REG_SIZE); 914 #undef MAX_MULTI_QUEUE_REG_SIZE 915 916 /* 917 * The above XE_GUC_ACTION_REGISTER_CONTEXT_MULTI_QUEUE do expect a 918 * XE_GUC_ACTION_NOTIFY_MULTI_QUEUE_CONTEXT_CGP_SYNC_DONE response 919 * from guc. 920 */ 921 xe_guc_exec_queue_group_cgp_sync(guc, q, action, len, 922 CGP_SYNC_REGISTRATION); 923 } 924 925 static void __register_mlrc_exec_queue(struct xe_guc *guc, 926 struct xe_exec_queue *q, 927 struct guc_ctxt_registration_info *info) 928 { 929 #define MAX_MLRC_REG_SIZE (13 + XE_HW_ENGINE_MAX_INSTANCE * 2) 930 u32 action[MAX_MLRC_REG_SIZE]; 931 int len = 0; 932 int i; 933 934 xe_gt_assert(guc_to_gt(guc), xe_exec_queue_is_parallel(q)); 935 936 action[len++] = XE_GUC_ACTION_REGISTER_CONTEXT_MULTI_LRC; 937 action[len++] = info->flags; 938 action[len++] = info->context_idx; 939 action[len++] = info->engine_class; 940 action[len++] = info->engine_submit_mask; 941 action[len++] = info->wq_desc_lo; 942 action[len++] = info->wq_desc_hi; 943 action[len++] = info->wq_base_lo; 944 action[len++] = info->wq_base_hi; 945 action[len++] = info->wq_size; 946 action[len++] = q->width; 947 action[len++] = info->hwlrca_lo; 948 action[len++] = info->hwlrca_hi; 949 950 for (i = 1; i < q->width; ++i) { 951 struct xe_lrc *lrc = q->lrc[i]; 952 953 action[len++] = lower_32_bits(xe_lrc_descriptor(lrc)); 954 action[len++] = upper_32_bits(xe_lrc_descriptor(lrc)); 955 } 956 957 /* explicitly checks some fields that we might fixup later */ 958 xe_gt_assert(guc_to_gt(guc), info->wq_desc_lo == 959 action[XE_GUC_REGISTER_CONTEXT_MULTI_LRC_DATA_5_WQ_DESC_ADDR_LOWER]); 960 xe_gt_assert(guc_to_gt(guc), info->wq_base_lo == 961 action[XE_GUC_REGISTER_CONTEXT_MULTI_LRC_DATA_7_WQ_BUF_BASE_LOWER]); 962 xe_gt_assert(guc_to_gt(guc), q->width == 963 action[XE_GUC_REGISTER_CONTEXT_MULTI_LRC_DATA_10_NUM_CTXS]); 964 xe_gt_assert(guc_to_gt(guc), info->hwlrca_lo == 965 action[XE_GUC_REGISTER_CONTEXT_MULTI_LRC_DATA_11_HW_LRC_ADDR]); 966 xe_gt_assert(guc_to_gt(guc), len <= MAX_MLRC_REG_SIZE); 967 #undef MAX_MLRC_REG_SIZE 968 969 xe_guc_ct_send(&guc->ct, action, len, 0, 0); 970 } 971 972 static void __register_exec_queue(struct xe_guc *guc, 973 struct guc_ctxt_registration_info *info) 974 { 975 u32 action[] = { 976 XE_GUC_ACTION_REGISTER_CONTEXT, 977 info->flags, 978 info->context_idx, 979 info->engine_class, 980 info->engine_submit_mask, 981 info->wq_desc_lo, 982 info->wq_desc_hi, 983 info->wq_base_lo, 984 info->wq_base_hi, 985 info->wq_size, 986 info->hwlrca_lo, 987 info->hwlrca_hi, 988 }; 989 990 /* explicitly checks some fields that we might fixup later */ 991 xe_gt_assert(guc_to_gt(guc), info->wq_desc_lo == 992 action[XE_GUC_REGISTER_CONTEXT_DATA_5_WQ_DESC_ADDR_LOWER]); 993 xe_gt_assert(guc_to_gt(guc), info->wq_base_lo == 994 action[XE_GUC_REGISTER_CONTEXT_DATA_7_WQ_BUF_BASE_LOWER]); 995 xe_gt_assert(guc_to_gt(guc), info->hwlrca_lo == 996 action[XE_GUC_REGISTER_CONTEXT_DATA_10_HW_LRC_ADDR]); 997 998 xe_guc_ct_send(&guc->ct, action, ARRAY_SIZE(action), 0, 0); 999 } 1000 1001 static u32 xe_hwe_guc_logical_to_submit_mask(struct xe_hw_engine *hwe, u32 logical_mask) 1002 { 1003 struct xe_gt *gt = hwe->gt; 1004 1005 if (xe_gt_is_usm_hwe(gt, hwe)) { 1006 int shift = gt->usm.paging_hwe0->logical_instance; 1007 u32 paging_logical_mask = gt->usm.paging_logical_mask; 1008 1009 xe_gt_assert(gt, (logical_mask & paging_logical_mask) == logical_mask); 1010 1011 /* 1012 * Remap to GUC_PAGING_CLASS logical instance mask, if 1013 * applicable. 1014 */ 1015 if (xe_guc_has_paging_engine(&hwe->gt->uc.guc)) 1016 return logical_mask >> shift; 1017 } 1018 1019 return logical_mask; 1020 } 1021 1022 static void register_exec_queue(struct xe_exec_queue *q, int ctx_type) 1023 { 1024 struct xe_guc *guc = exec_queue_to_guc(q); 1025 struct xe_device *xe = guc_to_xe(guc); 1026 struct xe_lrc *lrc = q->lrc[0]; 1027 struct guc_ctxt_registration_info info; 1028 1029 xe_gt_assert(guc_to_gt(guc), !exec_queue_registered(q)); 1030 xe_gt_assert(guc_to_gt(guc), ctx_type < GUC_CONTEXT_COUNT); 1031 1032 memset(&info, 0, sizeof(info)); 1033 info.context_idx = q->guc->id; 1034 info.engine_class = xe_hwe_to_guc_class(q->hwe); 1035 info.engine_submit_mask = 1036 xe_hwe_guc_logical_to_submit_mask(q->hwe, q->logical_mask); 1037 info.hwlrca_lo = lower_32_bits(xe_lrc_descriptor(lrc)); 1038 info.hwlrca_hi = upper_32_bits(xe_lrc_descriptor(lrc)); 1039 info.flags = CONTEXT_REGISTRATION_FLAG_KMD | 1040 FIELD_PREP(CONTEXT_REGISTRATION_FLAG_TYPE, ctx_type); 1041 1042 if (xe_exec_queue_is_multi_queue(q)) { 1043 struct xe_exec_queue_group *group = q->multi_queue.group; 1044 1045 info.cgp_lo = xe_bo_ggtt_addr(group->cgp_bo); 1046 info.cgp_hi = 0; 1047 } 1048 1049 if (xe_exec_queue_is_parallel(q)) { 1050 u64 ggtt_addr = xe_lrc_parallel_ggtt_addr(lrc); 1051 struct iosys_map map = xe_lrc_parallel_map(lrc); 1052 1053 info.wq_desc_lo = lower_32_bits(ggtt_addr + 1054 offsetof(struct guc_submit_parallel_scratch, wq_desc)); 1055 info.wq_desc_hi = upper_32_bits(ggtt_addr + 1056 offsetof(struct guc_submit_parallel_scratch, wq_desc)); 1057 info.wq_base_lo = lower_32_bits(ggtt_addr + 1058 offsetof(struct guc_submit_parallel_scratch, wq[0])); 1059 info.wq_base_hi = upper_32_bits(ggtt_addr + 1060 offsetof(struct guc_submit_parallel_scratch, wq[0])); 1061 info.wq_size = WQ_SIZE; 1062 1063 q->guc->wqi_head = 0; 1064 q->guc->wqi_tail = 0; 1065 xe_map_memset(xe, &map, 0, 0, PARALLEL_SCRATCH_SIZE - WQ_SIZE); 1066 parallel_write(xe, map, wq_desc.wq_status, WQ_STATUS_ACTIVE); 1067 } 1068 1069 set_exec_queue_registered(q); 1070 trace_xe_exec_queue_register(q); 1071 if (xe_exec_queue_is_multi_queue_primary(q)) 1072 __register_exec_queue_group(q, &info); 1073 else if (xe_exec_queue_is_parallel(q)) 1074 __register_mlrc_exec_queue(guc, q, &info); 1075 else if (!xe_exec_queue_is_multi_queue_secondary(q)) 1076 __register_exec_queue(guc, &info); 1077 1078 if (!xe_exec_queue_is_multi_queue_secondary(q)) 1079 init_policies(guc, q); 1080 1081 if (xe_exec_queue_is_multi_queue_secondary(q)) 1082 guc_exec_queue_send_cgp_sync(q, CGP_SYNC_REGISTRATION); 1083 } 1084 1085 static u32 wq_space_until_wrap(struct xe_exec_queue *q) 1086 { 1087 return (WQ_SIZE - q->guc->wqi_tail); 1088 } 1089 1090 static int wq_wait_for_space(struct xe_exec_queue *q, u32 wqi_size) 1091 { 1092 struct xe_guc *guc = exec_queue_to_guc(q); 1093 struct xe_device *xe = guc_to_xe(guc); 1094 struct iosys_map map = xe_lrc_parallel_map(q->lrc[0]); 1095 unsigned int sleep_period_ms = 1, sleep_total_ms = 0; 1096 1097 #define AVAILABLE_SPACE \ 1098 CIRC_SPACE(q->guc->wqi_tail, q->guc->wqi_head, WQ_SIZE) 1099 if (wqi_size > AVAILABLE_SPACE && !vf_recovery(guc)) { 1100 try_again: 1101 q->guc->wqi_head = parallel_read(xe, map, wq_desc.head); 1102 if (wqi_size > AVAILABLE_SPACE && !vf_recovery(guc)) { 1103 if (sleep_total_ms > 2000) { 1104 xe_gt_reset_async(q->gt); 1105 return -ENODEV; 1106 } 1107 1108 sleep_total_ms += xe_sleep_exponential_ms(&sleep_period_ms, 64); 1109 goto try_again; 1110 } 1111 } 1112 #undef AVAILABLE_SPACE 1113 1114 return 0; 1115 } 1116 1117 static int wq_noop_append(struct xe_exec_queue *q) 1118 { 1119 struct xe_guc *guc = exec_queue_to_guc(q); 1120 struct xe_device *xe = guc_to_xe(guc); 1121 struct iosys_map map = xe_lrc_parallel_map(q->lrc[0]); 1122 u32 len_dw = wq_space_until_wrap(q) / sizeof(u32) - 1; 1123 1124 if (wq_wait_for_space(q, wq_space_until_wrap(q))) 1125 return -ENODEV; 1126 1127 xe_gt_assert(guc_to_gt(guc), FIELD_FIT(WQ_LEN_MASK, len_dw)); 1128 1129 parallel_write(xe, map, wq[q->guc->wqi_tail / sizeof(u32)], 1130 FIELD_PREP(WQ_TYPE_MASK, WQ_TYPE_NOOP) | 1131 FIELD_PREP(WQ_LEN_MASK, len_dw)); 1132 q->guc->wqi_tail = 0; 1133 1134 return 0; 1135 } 1136 1137 static void wq_item_append(struct xe_exec_queue *q) 1138 { 1139 struct xe_guc *guc = exec_queue_to_guc(q); 1140 struct xe_device *xe = guc_to_xe(guc); 1141 struct iosys_map map = xe_lrc_parallel_map(q->lrc[0]); 1142 #define WQ_HEADER_SIZE 4 /* Includes 1 LRC address too */ 1143 u32 wqi[XE_HW_ENGINE_MAX_INSTANCE + (WQ_HEADER_SIZE - 1)]; 1144 u32 wqi_size = (q->width + (WQ_HEADER_SIZE - 1)) * sizeof(u32); 1145 u32 len_dw = (wqi_size / sizeof(u32)) - 1; 1146 int i = 0, j; 1147 1148 if (wqi_size > wq_space_until_wrap(q)) { 1149 if (wq_noop_append(q)) 1150 return; 1151 } 1152 if (wq_wait_for_space(q, wqi_size)) 1153 return; 1154 1155 wqi[i++] = FIELD_PREP(WQ_TYPE_MASK, WQ_TYPE_MULTI_LRC) | 1156 FIELD_PREP(WQ_LEN_MASK, len_dw); 1157 wqi[i++] = xe_lrc_descriptor(q->lrc[0]); 1158 wqi[i++] = FIELD_PREP(WQ_GUC_ID_MASK, q->guc->id) | 1159 FIELD_PREP(WQ_RING_TAIL_MASK, q->lrc[0]->ring.tail / sizeof(u64)); 1160 wqi[i++] = 0; 1161 for (j = 1; j < q->width; ++j) { 1162 struct xe_lrc *lrc = q->lrc[j]; 1163 1164 wqi[i++] = lrc->ring.tail / sizeof(u64); 1165 } 1166 1167 xe_gt_assert(guc_to_gt(guc), i == wqi_size / sizeof(u32)); 1168 1169 iosys_map_incr(&map, offsetof(struct guc_submit_parallel_scratch, 1170 wq[q->guc->wqi_tail / sizeof(u32)])); 1171 xe_map_memcpy_to(xe, &map, 0, wqi, wqi_size); 1172 q->guc->wqi_tail += wqi_size; 1173 xe_gt_assert(guc_to_gt(guc), q->guc->wqi_tail <= WQ_SIZE); 1174 1175 xe_device_wmb(xe); 1176 1177 map = xe_lrc_parallel_map(q->lrc[0]); 1178 parallel_write(xe, map, wq_desc.tail, q->guc->wqi_tail); 1179 } 1180 1181 #define RESUME_PENDING ~0x0ull 1182 static void submit_exec_queue(struct xe_exec_queue *q, struct xe_sched_job *job) 1183 { 1184 struct xe_guc *guc = exec_queue_to_guc(q); 1185 struct xe_lrc *lrc = q->lrc[0]; 1186 u32 action[3]; 1187 u32 g2h_len = 0; 1188 u32 num_g2h = 0; 1189 int len = 0; 1190 bool extra_submit = false; 1191 1192 xe_gt_assert(guc_to_gt(guc), exec_queue_registered(q)); 1193 1194 if (!job->restore_replay || job->last_replay) { 1195 /* 1196 * A chained ULLS job relies on its predecessor's postamble to 1197 * publish its ring tail. After a reset the context comes back 1198 * disabled with that postamble gone, so publish it here. 1199 */ 1200 if (xe_exec_queue_is_parallel(q)) 1201 wq_item_append(q); 1202 else if (!xe_sched_job_ulls_is_chained(job) || 1203 !exec_queue_enabled(q)) 1204 xe_lrc_set_ring_tail(lrc, lrc->ring.tail); 1205 job->last_replay = false; 1206 } 1207 1208 if (exec_queue_suspended(q) && !xe_exec_queue_is_parallel(q)) 1209 return; 1210 1211 /* 1212 * All queues in a multi-queue group will use the primary queue 1213 * of the group to interface with GuC. If primay is suspended, 1214 * just return. Jobs will get scheduled once primary is resumed. 1215 */ 1216 q = xe_exec_queue_multi_queue_primary(q); 1217 if (exec_queue_suspended(q)) 1218 return; 1219 1220 if (xe_sched_job_ulls_is_chained(job)) 1221 xe_lrc_set_ulls_semaphore(lrc, xe_sched_job_lrc_seqno(job)); 1222 1223 if (!exec_queue_enabled(q)) { 1224 action[len++] = XE_GUC_ACTION_SCHED_CONTEXT_MODE_SET; 1225 action[len++] = q->guc->id; 1226 action[len++] = GUC_CONTEXT_ENABLE; 1227 g2h_len = G2H_LEN_DW_SCHED_CONTEXT_MODE_SET; 1228 num_g2h = 1; 1229 if (xe_exec_queue_is_parallel(q)) 1230 extra_submit = true; 1231 1232 q->guc->resume_time = RESUME_PENDING; 1233 set_exec_queue_pending_enable(q); 1234 set_exec_queue_enabled(q); 1235 trace_xe_exec_queue_scheduling_enable(q); 1236 } else if (!xe_sched_job_ulls_is_chained(job)) { 1237 action[len++] = XE_GUC_ACTION_SCHED_CONTEXT; 1238 action[len++] = q->guc->id; 1239 trace_xe_exec_queue_submit(q); 1240 } 1241 1242 if (!xe_sched_job_ulls_is_chained(job) || num_g2h) 1243 xe_guc_ct_send(&guc->ct, action, len, g2h_len, num_g2h); 1244 1245 if (extra_submit) { 1246 len = 0; 1247 action[len++] = XE_GUC_ACTION_SCHED_CONTEXT; 1248 action[len++] = q->guc->id; 1249 trace_xe_exec_queue_submit(q); 1250 1251 xe_guc_ct_send(&guc->ct, action, len, 0, 0); 1252 } 1253 } 1254 1255 static bool is_pt_job(struct xe_sched_job *job) 1256 { 1257 return job->is_pt_job; 1258 } 1259 1260 static void run_pt_job(struct xe_device *xe, struct xe_sched_job *job, 1261 bool force_clear) 1262 { 1263 struct xe_tile *tile; 1264 int id; 1265 1266 for_each_tile(tile, xe, id) { 1267 struct xe_pt_job_ops *pt_job_ops = 1268 job->pt_update[0].pt_job_ops[id]; 1269 1270 if (!pt_job_ops || !pt_job_ops->current_op) 1271 continue; 1272 1273 xe_cpu_bind_update_pgtables_execute(job->pt_update[0].vm, tile, 1274 job->pt_update[0].ops, 1275 pt_job_ops->ops, 1276 pt_job_ops->current_op, 1277 force_clear); 1278 } 1279 } 1280 1281 static void put_pt_job(struct xe_device *xe, struct xe_sched_job *job) 1282 { 1283 struct xe_tile *tile; 1284 int id; 1285 1286 for_each_tile(tile, xe, id) { 1287 struct xe_pt_job_ops *pt_job_ops = 1288 job->pt_update[0].pt_job_ops[id]; 1289 1290 xe_pt_job_ops_put(pt_job_ops); 1291 } 1292 } 1293 1294 static struct dma_fence * 1295 guc_exec_queue_run_job(struct drm_sched_job *drm_job) 1296 { 1297 struct xe_sched_job *job = to_xe_sched_job(drm_job); 1298 struct xe_exec_queue *q = job->q; 1299 struct xe_guc *guc = exec_queue_to_guc(q); 1300 bool killed_or_banned_or_wedged_or_error = 1301 exec_queue_killed_or_banned_or_wedged(q) || 1302 xe_sched_job_is_error(job); 1303 1304 xe_gt_assert(guc_to_gt(guc), !(exec_queue_destroyed(q) || exec_queue_pending_disable(q)) || 1305 exec_queue_banned(q) || exec_queue_suspended(q)); 1306 1307 trace_xe_sched_job_run(job); 1308 1309 if (is_pt_job(job)) { 1310 xe_gt_assert(guc_to_gt(guc), !exec_queue_registered(q)); 1311 1312 run_pt_job(guc_to_xe(guc), job, 1313 killed_or_banned_or_wedged_or_error); 1314 put_pt_job(guc_to_xe(guc), job); 1315 dma_fence_put(job->fence); /* Drop ref from xe_sched_job_arm */ 1316 1317 return NULL; 1318 } else if (!killed_or_banned_or_wedged_or_error) { 1319 if (xe_exec_queue_is_multi_queue_secondary(q)) { 1320 struct xe_exec_queue *primary = xe_exec_queue_multi_queue_primary(q); 1321 1322 if (exec_queue_killed_or_banned_or_wedged(primary)) 1323 goto run_job_out; 1324 1325 if (!exec_queue_registered(primary)) 1326 register_exec_queue(primary, GUC_CONTEXT_NORMAL); 1327 } 1328 1329 if (!exec_queue_registered(q)) 1330 register_exec_queue(q, GUC_CONTEXT_NORMAL); 1331 if (!job->restore_replay) 1332 q->ring_ops->emit_job(job); 1333 submit_exec_queue(q, job); 1334 job->restore_replay = false; 1335 } 1336 1337 run_job_out: 1338 1339 return job->fence; 1340 } 1341 1342 static void guc_exec_queue_free_job(struct drm_sched_job *drm_job) 1343 { 1344 struct xe_sched_job *job = to_xe_sched_job(drm_job); 1345 1346 trace_xe_sched_job_free(job); 1347 xe_sched_job_put(job); 1348 } 1349 1350 int xe_guc_read_stopped(struct xe_guc *guc) 1351 { 1352 return atomic_read(&guc->submission_state.stopped); 1353 } 1354 1355 static void handle_multi_queue_secondary_sched_done(struct xe_guc *guc, 1356 struct xe_exec_queue *q, 1357 u32 runnable_state); 1358 static void handle_deregister_done(struct xe_guc *guc, struct xe_exec_queue *q); 1359 1360 #define MAKE_SCHED_CONTEXT_ACTION(q, enable_disable) \ 1361 u32 action[] = { \ 1362 XE_GUC_ACTION_SCHED_CONTEXT_MODE_SET, \ 1363 q->guc->id, \ 1364 GUC_CONTEXT_##enable_disable, \ 1365 } 1366 1367 static void disable_scheduling_deregister(struct xe_guc *guc, 1368 struct xe_exec_queue *q) 1369 { 1370 MAKE_SCHED_CONTEXT_ACTION(q, DISABLE); 1371 int ret; 1372 1373 if (!xe_exec_queue_is_multi_queue_secondary(q)) 1374 set_min_preemption_timeout(guc, q); 1375 1376 smp_rmb(); 1377 ret = wait_event_timeout(guc->ct.wq, 1378 (!exec_queue_pending_enable(q) && 1379 !exec_queue_pending_disable(q)) || 1380 xe_guc_read_stopped(guc) || 1381 vf_recovery(guc), 1382 HZ * 5); 1383 if (!ret && !vf_recovery(guc)) { 1384 struct xe_gpu_scheduler *sched = &q->guc->sched; 1385 1386 xe_gt_warn(q->gt, "Pending enable/disable failed to respond\n"); 1387 xe_sched_submission_start(sched); 1388 xe_gt_reset_async(q->gt); 1389 xe_sched_tdr_queue_imm(sched); 1390 return; 1391 } 1392 1393 clear_exec_queue_enabled(q); 1394 set_exec_queue_pending_disable(q); 1395 set_exec_queue_destroyed(q); 1396 trace_xe_exec_queue_scheduling_disable(q); 1397 1398 /* 1399 * Reserve space for both G2H here as the 2nd G2H is sent from a G2H 1400 * handler and we are not allowed to reserved G2H space in handlers. 1401 */ 1402 if (xe_exec_queue_is_multi_queue_secondary(q)) 1403 handle_multi_queue_secondary_sched_done(guc, q, 0); 1404 else 1405 xe_guc_ct_send(&guc->ct, action, ARRAY_SIZE(action), 1406 G2H_LEN_DW_SCHED_CONTEXT_MODE_SET + 1407 G2H_LEN_DW_DEREGISTER_CONTEXT, 2); 1408 } 1409 1410 /** 1411 * xe_guc_submit_wedge() - Wedge GuC submission 1412 * @guc: the GuC object 1413 * 1414 * Save exec queue's registered with GuC state by taking a ref to each queue. 1415 * Register a DRMM handler to drop refs upon driver unload. 1416 */ 1417 void xe_guc_submit_wedge(struct xe_guc *guc) 1418 { 1419 struct xe_device *xe = guc_to_xe(guc); 1420 struct xe_exec_queue *q; 1421 unsigned long index; 1422 1423 xe_gt_assert(guc_to_gt(guc), guc_to_xe(guc)->wedged.mode); 1424 1425 /* 1426 * If device is being wedged even before submission_state is 1427 * initialized, there's nothing to do here. 1428 */ 1429 if (!guc->submission_state.initialized) 1430 return; 1431 1432 if (xe->wedged.mode == XE_WEDGED_MODE_UPON_ANY_HANG_NO_RESET) { 1433 mutex_lock(&guc->submission_state.lock); 1434 xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) 1435 if (xe_exec_queue_get_unless_zero(q)) 1436 set_exec_queue_wedged(q); 1437 mutex_unlock(&guc->submission_state.lock); 1438 } else { 1439 /* Forcefully kill any remaining exec queues, signal fences */ 1440 guc_submit_reset_prepare(guc); 1441 xe_guc_submit_stop(guc); 1442 xe_guc_softreset(guc); 1443 xe_uc_fw_sanitize(&guc->fw); 1444 xe_guc_submit_pause_abort(guc); 1445 } 1446 } 1447 1448 static bool guc_submit_hint_wedged(struct xe_guc *guc) 1449 { 1450 struct xe_device *xe = guc_to_xe(guc); 1451 1452 if (xe->wedged.mode != XE_WEDGED_MODE_UPON_ANY_HANG_NO_RESET) 1453 return false; 1454 1455 if (xe_device_wedged(xe)) 1456 return true; 1457 1458 xe_device_declare_wedged(xe, DRM_WEDGE_RECOVERY_REBIND | DRM_WEDGE_RECOVERY_BUS_RESET); 1459 1460 return true; 1461 } 1462 1463 #define ADJUST_FIVE_PERCENT(__t) mul_u64_u32_div(__t, 105, 100) 1464 1465 static bool check_timeout(struct xe_exec_queue *q, struct xe_sched_job *job) 1466 { 1467 struct xe_gt *gt = guc_to_gt(exec_queue_to_guc(q)); 1468 u32 ctx_timestamp, ctx_job_timestamp; 1469 u32 timeout_ms = q->sched_props.job_timeout_ms; 1470 u32 diff; 1471 u64 running_time_ms; 1472 1473 if (!xe_sched_job_started(job)) { 1474 xe_gt_warn(gt, "Check job timeout: seqno=%u, lrc_seqno=%u, guc_id=%d, not started", 1475 xe_sched_job_seqno(job), xe_sched_job_lrc_seqno(job), 1476 q->guc->id); 1477 1478 /* GuC never scheduled this job - let the caller trigger a GT reset. */ 1479 return true; 1480 } 1481 1482 ctx_timestamp = lower_32_bits(xe_lrc_timestamp(q->lrc[0])); 1483 if (ctx_timestamp == job->sample_timestamp) { 1484 if (IS_SRIOV_VF(gt_to_xe(gt))) 1485 xe_gt_notice(gt, "Check job timeout: seqno=%u, lrc_seqno=%u, guc_id=%d, timestamp stuck", 1486 xe_sched_job_seqno(job), 1487 xe_sched_job_lrc_seqno(job), q->guc->id); 1488 else 1489 xe_gt_warn(gt, "Check job timeout: seqno=%u, lrc_seqno=%u, guc_id=%d, timestamp stuck", 1490 xe_sched_job_seqno(job), 1491 xe_sched_job_lrc_seqno(job), q->guc->id); 1492 1493 return xe_sched_invalidate_job(job, 0); 1494 } 1495 1496 job->sample_timestamp = ctx_timestamp; 1497 ctx_job_timestamp = xe_lrc_ctx_job_timestamp(q->lrc[0]); 1498 1499 /* 1500 * Counter wraps at ~223s at the usual 19.2MHz, be paranoid catch 1501 * possible overflows with a high timeout. 1502 */ 1503 xe_gt_assert(gt, timeout_ms < 100 * MSEC_PER_SEC); 1504 1505 diff = ctx_timestamp - ctx_job_timestamp; 1506 1507 /* 1508 * Ensure timeout is within 5% to account for an GuC scheduling latency 1509 */ 1510 running_time_ms = 1511 ADJUST_FIVE_PERCENT(xe_gt_clock_interval_to_ms(gt, diff)); 1512 1513 xe_gt_dbg(gt, 1514 "Check job timeout: seqno=%u, lrc_seqno=%u, guc_id=%d, running_time_ms=%llu, timeout_ms=%u, diff=0x%08x", 1515 xe_sched_job_seqno(job), xe_sched_job_lrc_seqno(job), 1516 q->guc->id, running_time_ms, timeout_ms, diff); 1517 1518 return running_time_ms >= timeout_ms; 1519 } 1520 1521 static void enable_scheduling(struct xe_exec_queue *q) 1522 { 1523 MAKE_SCHED_CONTEXT_ACTION(q, ENABLE); 1524 struct xe_guc *guc = exec_queue_to_guc(q); 1525 int ret; 1526 1527 xe_gt_assert(guc_to_gt(guc), !exec_queue_destroyed(q)); 1528 xe_gt_assert(guc_to_gt(guc), exec_queue_registered(q)); 1529 xe_gt_assert(guc_to_gt(guc), !exec_queue_pending_disable(q)); 1530 xe_gt_assert(guc_to_gt(guc), !exec_queue_pending_enable(q)); 1531 1532 set_exec_queue_pending_enable(q); 1533 set_exec_queue_enabled(q); 1534 trace_xe_exec_queue_scheduling_enable(q); 1535 1536 if (xe_exec_queue_is_multi_queue_secondary(q)) 1537 handle_multi_queue_secondary_sched_done(guc, q, 1); 1538 else 1539 xe_guc_ct_send(&guc->ct, action, ARRAY_SIZE(action), 1540 G2H_LEN_DW_SCHED_CONTEXT_MODE_SET, 1); 1541 1542 ret = wait_event_timeout(guc->ct.wq, 1543 !exec_queue_pending_enable(q) || 1544 xe_guc_read_stopped(guc) || 1545 vf_recovery(guc), HZ * 5); 1546 if ((!ret && !vf_recovery(guc)) || xe_guc_read_stopped(guc)) { 1547 xe_gt_warn(guc_to_gt(guc), "Schedule enable failed to respond"); 1548 set_exec_queue_banned(q); 1549 xe_gt_reset_async(q->gt); 1550 xe_sched_tdr_queue_imm(&q->guc->sched); 1551 } 1552 } 1553 1554 static void disable_scheduling(struct xe_exec_queue *q, bool immediate) 1555 { 1556 MAKE_SCHED_CONTEXT_ACTION(q, DISABLE); 1557 struct xe_guc *guc = exec_queue_to_guc(q); 1558 1559 xe_gt_assert(guc_to_gt(guc), !exec_queue_destroyed(q)); 1560 xe_gt_assert(guc_to_gt(guc), exec_queue_registered(q)); 1561 xe_gt_assert(guc_to_gt(guc), !exec_queue_pending_disable(q)); 1562 1563 if (immediate && !xe_exec_queue_is_multi_queue_secondary(q)) 1564 set_min_preemption_timeout(guc, q); 1565 clear_exec_queue_enabled(q); 1566 set_exec_queue_pending_disable(q); 1567 trace_xe_exec_queue_scheduling_disable(q); 1568 1569 if (xe_exec_queue_is_multi_queue_secondary(q)) 1570 handle_multi_queue_secondary_sched_done(guc, q, 0); 1571 else 1572 xe_guc_ct_send(&guc->ct, action, ARRAY_SIZE(action), 1573 G2H_LEN_DW_SCHED_CONTEXT_MODE_SET, 1); 1574 } 1575 1576 /* 1577 * Recover via GT reset for a kernel queue, or for a GuC scheduling failure (job 1578 * never started) on a queue that was not already killed or banned. An already 1579 * banned queue must stay banned, so its unstarted jobs do not clear the ban or 1580 * trigger a reset. 1581 */ 1582 static bool timeout_needs_gt_reset(struct xe_exec_queue *q, struct xe_sched_job *job, 1583 bool skip_timeout_check) 1584 { 1585 if (q->flags & EXEC_QUEUE_FLAG_KERNEL) 1586 return true; 1587 1588 return !skip_timeout_check && !xe_sched_job_started(job); 1589 } 1590 1591 static enum drm_gpu_sched_stat 1592 guc_exec_queue_timedout_job(struct drm_sched_job *drm_job) 1593 { 1594 struct xe_sched_job *job = to_xe_sched_job(drm_job); 1595 struct drm_sched_job *tmp_job; 1596 struct xe_exec_queue *q = job->q, *primary; 1597 struct xe_gpu_scheduler *sched = &q->guc->sched; 1598 struct xe_guc *guc = exec_queue_to_guc(q); 1599 const char *process_name = "no process"; 1600 struct xe_device *xe = guc_to_xe(guc); 1601 int err = -ETIME; 1602 pid_t pid = -1; 1603 bool wedged = false, wedge_device = false, skip_timeout_check; 1604 int idx; 1605 1606 xe_gt_assert(guc_to_gt(guc), !exec_queue_destroyed(q)); 1607 1608 primary = xe_exec_queue_multi_queue_primary(q); 1609 1610 /* 1611 * TDR has fired before free job worker. Common if exec queue 1612 * immediately closed after last fence signaled. Add back to pending 1613 * list so job can be freed and kick scheduler ensuring free job is not 1614 * lost. 1615 */ 1616 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &job->fence->flags) || 1617 vf_recovery(guc)) 1618 return DRM_GPU_SCHED_STAT_NO_HANG; 1619 1620 if (!drm_dev_enter(&xe->drm, &idx)) { 1621 xe_sched_job_set_error(job, -ENODEV); 1622 return DRM_GPU_SCHED_STAT_NO_HANG; 1623 } 1624 1625 /* Kill the run_job entry point */ 1626 if (xe_exec_queue_is_multi_queue(q)) 1627 xe_guc_exec_queue_group_stop(q); 1628 else 1629 xe_sched_submission_stop(sched); 1630 1631 /* Must check all state after stopping scheduler */ 1632 skip_timeout_check = exec_queue_reset(q) || 1633 exec_queue_killed_or_banned_or_wedged(q); 1634 1635 /* Skip timeout check if multi-queue group is banned */ 1636 if (xe_exec_queue_is_multi_queue(q) && 1637 READ_ONCE(q->multi_queue.group->banned)) 1638 skip_timeout_check = true; 1639 1640 /* LR jobs can only get here if queue has been killed or hit an error */ 1641 if (xe_exec_queue_is_lr(q)) 1642 xe_gt_assert(guc_to_gt(guc), skip_timeout_check); 1643 1644 /* 1645 * If devcoredump not captured and GuC capture for the job is not ready 1646 * do manual capture first and decide later if we need to use it 1647 */ 1648 if (!xe_device_is_in_reset(xe) && !exec_queue_killed(q) && !xe->devcoredump.captured && 1649 !xe_guc_capture_get_matching_and_lock(q)) { 1650 /* take force wake before engine register manual capture */ 1651 CLASS(xe_force_wake, fw_ref)(gt_to_fw(q->gt), XE_FORCEWAKE_ALL); 1652 if (!xe_force_wake_ref_has_domain(fw_ref.domains, XE_FORCEWAKE_ALL)) 1653 xe_gt_info(q->gt, "failed to get forcewake for coredump capture\n"); 1654 1655 xe_engine_snapshot_capture_for_queue(q); 1656 } 1657 1658 /* 1659 * Check if job is actually timed out, if so restart job execution and TDR 1660 */ 1661 if (!skip_timeout_check && !check_timeout(q, job)) 1662 goto rearm; 1663 1664 /* 1665 * Killed queues must not newly wedge the device, but preserve an 1666 * already-wedged state to avoid warning on teardown timeouts. 1667 */ 1668 if (!exec_queue_killed(q)) 1669 wedged = guc_submit_hint_wedged(exec_queue_to_guc(q)); 1670 else 1671 wedged = xe_device_wedged(xe); 1672 1673 /* 1674 * Only tag as GPU hang if this is the original timeout, not a 1675 * consequence of a prior kill (e.g., page-offline). 1676 */ 1677 if (!exec_queue_killed(q)) 1678 atomic_or(DRM_XE_EXEC_QUEUE_BAN_REASON_GPU_HANG, &q->ban_reason); 1679 set_exec_queue_banned(q); 1680 1681 /* Kick job / queue off hardware */ 1682 if (!xe_device_is_in_reset(xe) && !wedged && 1683 (exec_queue_enabled(primary) || exec_queue_pending_disable(primary))) { 1684 int ret; 1685 1686 if (exec_queue_reset(primary)) 1687 err = -EIO; 1688 1689 if (xe_uc_fw_is_running(&guc->fw)) { 1690 /* 1691 * Wait for any pending G2H to flush out before 1692 * modifying state 1693 */ 1694 ret = wait_event_timeout(guc->ct.wq, 1695 (!exec_queue_pending_enable(primary) && 1696 !exec_queue_pending_disable(primary)) || 1697 xe_guc_read_stopped(guc) || 1698 vf_recovery(guc), HZ * 5); 1699 if (vf_recovery(guc)) 1700 goto handle_vf_resume; 1701 if (!ret || xe_guc_read_stopped(guc)) 1702 goto trigger_reset; 1703 1704 disable_scheduling(primary, skip_timeout_check); 1705 } 1706 1707 /* 1708 * Must wait for scheduling to be disabled before signalling 1709 * any fences, if GT broken the GT reset code should signal us. 1710 * 1711 * FIXME: Tests can generate a ton of 0x6000 (IOMMU CAT fault 1712 * error) messages which can cause the schedule disable to get 1713 * lost. If this occurs, trigger a GT reset to recover. 1714 */ 1715 smp_rmb(); 1716 ret = wait_event_timeout(guc->ct.wq, 1717 !xe_uc_fw_is_running(&guc->fw) || 1718 !exec_queue_pending_disable(primary) || 1719 xe_guc_read_stopped(guc) || 1720 vf_recovery(guc), HZ * 5); 1721 if (vf_recovery(guc)) 1722 goto handle_vf_resume; 1723 if (!ret || xe_guc_read_stopped(guc)) { 1724 trigger_reset: 1725 if (!ret) 1726 xe_gt_warn(guc_to_gt(guc), 1727 "Schedule disable failed to respond, guc_id=%d", 1728 primary->guc->id); 1729 xe_devcoredump(primary, job, 1730 "Schedule disable failed to respond, guc_id=%d, ret=%d, guc_read=%d", 1731 primary->guc->id, ret, xe_guc_read_stopped(guc)); 1732 xe_gt_reset_async(primary->gt); 1733 xe_sched_tdr_queue_imm(sched); 1734 goto rearm; 1735 } 1736 } 1737 1738 if (q->vm && q->vm->xef) { 1739 process_name = q->vm->xef->process_name; 1740 pid = q->vm->xef->pid; 1741 } 1742 1743 if (!exec_queue_killed(q)) 1744 xe_gt_notice(guc_to_gt(guc), 1745 "Timedout job: seqno=%u, lrc_seqno=%u, guc_id=%d, flags=0x%lx in %s [%d]", 1746 xe_sched_job_seqno(job), xe_sched_job_lrc_seqno(job), 1747 q->guc->id, q->flags, process_name, pid); 1748 1749 trace_xe_sched_job_timedout(job); 1750 1751 /* Do not access device if in reset */ 1752 if (!xe_device_is_in_reset(xe) && !exec_queue_killed(q)) 1753 xe_devcoredump(q, job, 1754 "Timedout job - seqno=%u, lrc_seqno=%u, guc_id=%d, flags=0x%lx", 1755 xe_sched_job_seqno(job), xe_sched_job_lrc_seqno(job), 1756 q->guc->id, q->flags); 1757 1758 if (!wedged) { 1759 if (timeout_needs_gt_reset(q, job, skip_timeout_check)) { 1760 if (!xe_sched_invalidate_job(job, 2)) { 1761 clear_exec_queue_banned(q); 1762 /* protect concurrent page offline reasons */ 1763 atomic_andnot(DRM_XE_EXEC_QUEUE_BAN_REASON_GPU_HANG, 1764 &q->ban_reason); 1765 xe_gt_reset_async(q->gt); 1766 goto rearm; 1767 } 1768 if (q->flags & EXEC_QUEUE_FLAG_KERNEL) { 1769 xe_gt_WARN(q->gt, true, "Kernel-submitted job timed out\n"); 1770 wedge_device = true; 1771 } 1772 } else if (q->flags & EXEC_QUEUE_FLAG_VM && !exec_queue_killed(q)) { 1773 xe_gt_WARN(q->gt, true, "VM job timed out on non-killed execqueue\n"); 1774 } 1775 } 1776 1777 /* Mark all outstanding jobs as bad, thus completing them */ 1778 xe_sched_job_set_error(job, err); 1779 drm_sched_for_each_pending_job(tmp_job, &sched->base, NULL) 1780 xe_sched_job_set_error(to_xe_sched_job(tmp_job), -ECANCELED); 1781 1782 if (xe_exec_queue_is_multi_queue(q)) { 1783 xe_guc_exec_queue_group_start(q); 1784 xe_guc_exec_queue_group_trigger_cleanup(q); 1785 } else { 1786 xe_sched_submission_start(sched); 1787 xe_guc_exec_queue_trigger_cleanup(q); 1788 } 1789 1790 if (wedge_device) 1791 xe_device_declare_wedged(gt_to_xe(q->gt), 1792 DRM_WEDGE_RECOVERY_REBIND | DRM_WEDGE_RECOVERY_BUS_RESET); 1793 1794 /* 1795 * We want the job added back to the pending list so it gets freed; this 1796 * is what DRM_GPU_SCHED_STAT_NO_HANG does. 1797 */ 1798 drm_dev_exit(idx); 1799 return DRM_GPU_SCHED_STAT_NO_HANG; 1800 1801 rearm: 1802 /* 1803 * XXX: Ideally want to adjust timeout based on current execution time 1804 * but there is not currently an easy way to do in DRM scheduler. With 1805 * some thought, do this in a follow up. 1806 */ 1807 if (xe_exec_queue_is_multi_queue(q)) 1808 xe_guc_exec_queue_group_start(q); 1809 else 1810 xe_sched_submission_start(sched); 1811 handle_vf_resume: 1812 drm_dev_exit(idx); 1813 return DRM_GPU_SCHED_STAT_NO_HANG; 1814 } 1815 1816 static void guc_exec_queue_multi_queue_drop_suspend(struct xe_exec_queue *q); 1817 static int guc_exec_queue_suspend_wait_blocking(struct xe_exec_queue *q); 1818 1819 static void guc_exec_queue_fini(struct xe_exec_queue *q) 1820 { 1821 struct xe_guc_exec_queue *ge = q->guc; 1822 struct xe_guc *guc = exec_queue_to_guc(q); 1823 struct drm_device *drm = &guc_to_xe(guc)->drm; 1824 1825 /* 1826 * A secondary can leave the group while still preempt suspended (e.g. 1827 * xe_vm_remove_compute_exec_queue() forces its preempt fence to signal, 1828 * which suspends it). It holds one forwarded suspend reference on the 1829 * primary, so drop it and resume the primary if it was the last member 1830 * that had it suspended. Primaries forward to nobody, so they don't need 1831 * this. 1832 * 1833 * First make sure the primary's forwarded suspend has completed. If the 1834 * secondary was killed/reset before its preempt fence worker ran, that 1835 * worker skips suspend_wait() (see preempt_fence_work_func()), leaving 1836 * the primary's suspend possibly in flight. drop_suspend() runs under a 1837 * spinlock and cannot wait, so drain it here with the uninterruptible 1838 * blocking wait; otherwise resuming the primary in drop_suspend() could 1839 * trip the !suspend_pending assert. 1840 */ 1841 if (xe_exec_queue_is_multi_queue_secondary(q)) { 1842 if (READ_ONCE(q->guc->suspend_count)) 1843 guc_exec_queue_suspend_wait_blocking(q); 1844 guc_exec_queue_multi_queue_drop_suspend(q); 1845 } 1846 1847 if (xe_exec_queue_is_multi_queue_secondary(q)) { 1848 struct xe_exec_queue_group *group = q->multi_queue.group; 1849 1850 mutex_lock(&group->list_lock); 1851 list_del(&q->multi_queue.link); 1852 mutex_unlock(&group->list_lock); 1853 } 1854 1855 release_guc_id(guc, q); 1856 xe_sched_entity_fini(&ge->entity); 1857 xe_sched_fini(&ge->sched); 1858 1859 /* 1860 * RCU free due sched being exported via DRM scheduler fences 1861 * (timeline name). 1862 */ 1863 kfree_rcu(ge, rcu); 1864 1865 drm_dev_put(drm); 1866 } 1867 1868 static void guc_exec_queue_do_destroy(struct xe_exec_queue *q) 1869 { 1870 struct xe_guc_exec_queue *ge = q->guc; 1871 struct xe_guc *guc = exec_queue_to_guc(q); 1872 struct xe_device *xe = guc_to_xe(guc); 1873 struct drm_device *drm = &xe->drm; 1874 1875 /* 1876 * guc_exec_queue_fini() drops the queue's drm_device ref. 1877 * Keep the device alive until the PM-runtime guard unwinds. 1878 */ 1879 drm_dev_get(drm); 1880 1881 scoped_guard(xe_pm_runtime, xe) { 1882 trace_xe_exec_queue_destroy(q); 1883 1884 /* Confirm no work left behind accessing device structures */ 1885 cancel_delayed_work_sync(&ge->sched.base.work_tdr); 1886 1887 xe_exec_queue_fini(q); 1888 } 1889 1890 drm_dev_put(drm); 1891 } 1892 1893 static void __guc_exec_queue_destroy_async(struct work_struct *w) 1894 { 1895 struct xe_guc_exec_queue *ge = 1896 container_of(w, struct xe_guc_exec_queue, destroy_async); 1897 1898 guc_exec_queue_do_destroy(ge->q); 1899 } 1900 1901 static void guc_exec_queue_destroy_async(struct xe_exec_queue *q) 1902 { 1903 INIT_WORK(&q->guc->destroy_async, __guc_exec_queue_destroy_async); 1904 xe_destroy_wq_queue(&q->guc->destroy_async); 1905 } 1906 1907 static void __guc_exec_queue_process_msg_cleanup(struct xe_sched_msg *msg, 1908 bool bound) 1909 { 1910 struct xe_exec_queue *q = msg->private_data; 1911 struct xe_guc *guc = exec_queue_to_guc(q); 1912 1913 trace_xe_exec_queue_cleanup_entity(q); 1914 1915 /* 1916 * Expected state transitions for cleanup: 1917 * - If the exec queue is registered and GuC firmware is running, we must first 1918 * disable scheduling and deregister the queue to ensure proper teardown and 1919 * resource release in the GuC, then destroy the exec queue on driver side. 1920 * - If the GuC is already stopped (e.g., during driver unload or GPU reset), 1921 * we cannot expect a response for the deregister request. In this case, 1922 * it is safe to directly destroy the exec queue on driver side, as the GuC 1923 * will not process further requests and all resources must be cleaned up locally. 1924 */ 1925 /* A wedged GuC won't answer the H2G, so tear down on the driver side. */ 1926 if (bound && !exec_queue_wedged(q) && exec_queue_registered(q) && 1927 xe_uc_fw_is_running(&guc->fw)) 1928 disable_scheduling_deregister(guc, q); 1929 else 1930 guc_exec_queue_destroy_async(q); 1931 } 1932 1933 static bool guc_exec_queue_allowed_to_change_state(struct xe_exec_queue *q) 1934 { 1935 return !exec_queue_killed_or_banned_or_wedged(q) && exec_queue_registered(q); 1936 } 1937 1938 static void __guc_exec_queue_process_msg_set_sched_props(struct xe_sched_msg *msg, 1939 bool bound) 1940 { 1941 struct xe_exec_queue *q = msg->private_data; 1942 struct xe_guc *guc = exec_queue_to_guc(q); 1943 1944 if (guc_exec_queue_allowed_to_change_state(q) && bound) 1945 init_policies(guc, q); 1946 kfree(msg); 1947 } 1948 1949 static void suspend_wait_wake(struct xe_exec_queue *q) 1950 { 1951 struct xe_guc *guc = exec_queue_to_guc(q); 1952 struct xe_device *xe = guc_to_xe(guc); 1953 1954 /* 1955 * We use a GuC shared wait queue for VFs because the VF resfix start 1956 * interrupt must be able to wake all instances of suspend_wait. This 1957 * prevents the VF migration worker from being starved during 1958 * scheduling. 1959 */ 1960 if (IS_SRIOV_VF(xe)) 1961 wake_up_all(&guc->ct.wq); 1962 else 1963 wake_up(&q->guc->suspend_wait); 1964 } 1965 1966 static void __suspend_fence_signal(struct xe_exec_queue *q) 1967 { 1968 if (!q->guc->suspend_pending) 1969 return; 1970 1971 WRITE_ONCE(q->guc->suspend_pending, false); 1972 suspend_wait_wake(q); 1973 } 1974 1975 static void suspend_fence_signal(struct xe_exec_queue *q) 1976 { 1977 struct xe_guc *guc = exec_queue_to_guc(q); 1978 1979 xe_gt_assert(guc_to_gt(guc), exec_queue_suspended(q) || exec_queue_killed(q) || 1980 xe_guc_read_stopped(guc)); 1981 xe_gt_assert(guc_to_gt(guc), q->guc->suspend_pending); 1982 1983 __suspend_fence_signal(q); 1984 } 1985 1986 static void __guc_exec_queue_process_msg_suspend(struct xe_sched_msg *msg, 1987 bool bound) 1988 { 1989 struct xe_exec_queue *q = msg->private_data; 1990 struct xe_guc *guc = exec_queue_to_guc(q); 1991 1992 if (guc_exec_queue_allowed_to_change_state(q) && !exec_queue_suspended(q) && 1993 exec_queue_enabled(q) && bound) { 1994 wait_event(guc->ct.wq, vf_recovery(guc) || 1995 ((q->guc->resume_time != RESUME_PENDING || 1996 xe_guc_read_stopped(guc)) && !exec_queue_pending_disable(q))); 1997 1998 if (!xe_guc_read_stopped(guc)) { 1999 s64 since_resume_ms = 2000 ktime_ms_delta(ktime_get(), 2001 q->guc->resume_time); 2002 s64 wait_ms = q->vm->preempt.min_run_period_ms - 2003 since_resume_ms; 2004 2005 if (wait_ms > 0 && q->guc->resume_time) 2006 xe_sleep_relaxed_ms(wait_ms); 2007 2008 set_exec_queue_suspended(q); 2009 disable_scheduling(q, false); 2010 } 2011 } else if (q->guc->suspend_pending) { 2012 set_exec_queue_suspended(q); 2013 suspend_fence_signal(q); 2014 } 2015 } 2016 2017 static void __guc_exec_queue_process_msg_resume(struct xe_sched_msg *msg, 2018 bool bound) 2019 { 2020 struct xe_exec_queue *q = msg->private_data; 2021 2022 if (guc_exec_queue_allowed_to_change_state(q) && bound) { 2023 clear_exec_queue_suspended(q); 2024 if (!exec_queue_enabled(q)) { 2025 q->guc->resume_time = RESUME_PENDING; 2026 set_exec_queue_pending_resume(q); 2027 enable_scheduling(q); 2028 } 2029 } else { 2030 clear_exec_queue_suspended(q); 2031 } 2032 } 2033 2034 static void __guc_exec_queue_process_msg_set_multi_queue_priority(struct xe_sched_msg *msg, 2035 bool bound) 2036 { 2037 struct xe_exec_queue *q = msg->private_data; 2038 2039 if (guc_exec_queue_allowed_to_change_state(q) && bound) 2040 guc_exec_queue_send_cgp_sync(q, 0); 2041 2042 kfree(msg); 2043 } 2044 2045 static void __guc_exec_queue_process_msg_cgp_sync(struct xe_sched_msg *msg, 2046 bool bound) 2047 { 2048 struct xe_exec_queue *q = msg->private_data; 2049 2050 /* 2051 * Replay a dynamic CGP update lost across VF migration by re-issuing the 2052 * CGP update + CGP_SYNC (re-applies the current priority from 2053 * q->multi_queue.priority). 2054 */ 2055 if (guc_exec_queue_allowed_to_change_state(q) && bound) 2056 guc_exec_queue_send_cgp_sync(q, 0); 2057 } 2058 2059 #define CLEANUP 1 /* Non-zero values to catch uninitialized msg */ 2060 #define SET_SCHED_PROPS 2 2061 #define SUSPEND 3 2062 #define RESUME 4 2063 #define SET_MULTI_QUEUE_PRIORITY 5 2064 #define CGP_SYNC_MSG 6 2065 #define OPCODE_MASK 0xf 2066 #define MSG_LOCKED BIT(8) 2067 #define MSG_HEAD BIT(9) 2068 #define MSG_PM_REF BIT(10) 2069 2070 static void guc_exec_queue_process_msg(struct xe_sched_msg *msg) 2071 { 2072 struct xe_device *xe = guc_to_xe(exec_queue_to_guc(msg->private_data)); 2073 int idx; 2074 bool pm_ref = !!(msg->opcode & MSG_PM_REF); 2075 bool bound = drm_dev_enter(&xe->drm, &idx); 2076 2077 trace_xe_sched_msg_recv(msg); 2078 2079 switch (msg->opcode & OPCODE_MASK) { 2080 case CLEANUP: 2081 __guc_exec_queue_process_msg_cleanup(msg, bound); 2082 break; 2083 case SET_SCHED_PROPS: 2084 __guc_exec_queue_process_msg_set_sched_props(msg, bound); 2085 break; 2086 case SUSPEND: 2087 __guc_exec_queue_process_msg_suspend(msg, bound); 2088 break; 2089 case RESUME: 2090 __guc_exec_queue_process_msg_resume(msg, bound); 2091 break; 2092 case SET_MULTI_QUEUE_PRIORITY: 2093 __guc_exec_queue_process_msg_set_multi_queue_priority(msg, bound); 2094 break; 2095 case CGP_SYNC_MSG: 2096 __guc_exec_queue_process_msg_cgp_sync(msg, bound); 2097 break; 2098 default: 2099 XE_WARN_ON("Unknown message type"); 2100 } 2101 2102 if (pm_ref) 2103 xe_pm_runtime_put(xe); 2104 2105 if (bound) 2106 drm_dev_exit(idx); 2107 } 2108 2109 static const struct drm_sched_backend_ops drm_sched_ops = { 2110 .run_job = guc_exec_queue_run_job, 2111 .free_job = guc_exec_queue_free_job, 2112 .timedout_job = guc_exec_queue_timedout_job, 2113 }; 2114 2115 static const struct xe_sched_backend_ops xe_sched_ops = { 2116 .process_msg = guc_exec_queue_process_msg, 2117 }; 2118 2119 static int guc_exec_queue_init(struct xe_exec_queue *q) 2120 { 2121 struct xe_gpu_scheduler *sched; 2122 struct xe_guc *guc = exec_queue_to_guc(q); 2123 struct drm_device *drm = &guc_to_xe(guc)->drm; 2124 struct workqueue_struct *submit_wq = NULL; 2125 struct xe_guc_exec_queue *ge; 2126 long timeout; 2127 int err, i; 2128 int max_jobs = (xe_lrc_ring_size() / MAX_JOB_SIZE_BYTES); 2129 2130 xe_gt_assert(guc_to_gt(guc), xe_device_uc_enabled(guc_to_xe(guc))); 2131 2132 ge = kzalloc_obj(*ge); 2133 if (!ge) 2134 return -ENOMEM; 2135 2136 drm_dev_get(drm); 2137 2138 q->guc = ge; 2139 ge->q = q; 2140 init_rcu_head(&ge->rcu); 2141 init_waitqueue_head(&ge->suspend_wait); 2142 2143 for (i = 0; i < MAX_STATIC_MSG_TYPE; ++i) 2144 INIT_LIST_HEAD(&ge->static_msgs[i].link); 2145 2146 timeout = (q->vm && xe_vm_in_lr_mode(q->vm)) ? MAX_SCHEDULE_TIMEOUT : 2147 msecs_to_jiffies(q->sched_props.job_timeout_ms); 2148 2149 err = alloc_guc_id(guc, q); 2150 if (err) 2151 goto err_free; 2152 2153 xe_exec_queue_assign_name(q, q->guc->id); 2154 2155 strscpy(ge->name, q->name, sizeof(ge->name)); 2156 2157 /* 2158 * Use primary queue's submit_wq for all secondary queues of a 2159 * multi queue group. This serialization avoids any locking around 2160 * CGP synchronization with GuC. 2161 */ 2162 if (xe_exec_queue_is_multi_queue_secondary(q)) { 2163 struct xe_exec_queue *primary = xe_exec_queue_multi_queue_primary(q); 2164 2165 submit_wq = primary->guc->sched.base.submit_wq; 2166 } 2167 2168 if (q->vm && q->vm->flags & XE_VM_FLAG_MIGRATION) 2169 max_jobs = min(max_jobs, LRC_MIGRATION_ULLS_SEMAPHORE_COUNT - 1); 2170 2171 err = xe_sched_init(&ge->sched, &drm_sched_ops, &xe_sched_ops, 2172 submit_wq, max_jobs, 64, 2173 timeout, guc_to_gt(guc)->ordered_wq, NULL, 2174 ge->name, gt_to_xe(q->gt)->drm.dev); 2175 if (err) 2176 goto err_release_id; 2177 2178 sched = &ge->sched; 2179 err = xe_sched_entity_init(&ge->entity, sched); 2180 if (err) 2181 goto err_sched; 2182 2183 q->entity = &ge->entity; 2184 2185 mutex_lock(&guc->submission_state.lock); 2186 if (xe_guc_read_stopped(guc) || vf_recovery(guc)) 2187 xe_sched_stop(sched); 2188 publish_guc_id(guc, q); 2189 mutex_unlock(&guc->submission_state.lock); 2190 2191 /* 2192 * Maintain secondary queues of the multi queue group in a list 2193 * for handling dependencies across the queues in the group. 2194 */ 2195 if (xe_exec_queue_is_multi_queue_secondary(q)) { 2196 struct xe_exec_queue_group *group = q->multi_queue.group; 2197 2198 INIT_LIST_HEAD(&q->multi_queue.link); 2199 mutex_lock(&group->list_lock); 2200 if (group->stopped) 2201 WRITE_ONCE(q->guc->sched.base.pause_submit, true); 2202 list_add_tail(&q->multi_queue.link, &group->list); 2203 mutex_unlock(&group->list_lock); 2204 } 2205 2206 if (xe_exec_queue_is_multi_queue(q)) 2207 trace_xe_exec_queue_create_multi_queue(q); 2208 else 2209 trace_xe_exec_queue_create(q); 2210 2211 return 0; 2212 2213 err_sched: 2214 xe_sched_fini(&ge->sched); 2215 err_release_id: 2216 release_guc_id(guc, q); 2217 err_free: 2218 kfree(ge); 2219 drm_dev_put(drm); 2220 2221 return err; 2222 } 2223 2224 static void guc_exec_queue_kill(struct xe_exec_queue *q) 2225 { 2226 trace_xe_exec_queue_kill(q); 2227 set_exec_queue_killed(q); 2228 /* 2229 * Do not signal the suspend fence here, the queue may still be running 2230 * on the hardware. Only wake any suspend waiter so it observes the 2231 * killed state instead of waiting out the full timeout, suspend_pending 2232 * stays set until the queue is torn down. 2233 */ 2234 suspend_wait_wake(q); 2235 xe_guc_exec_queue_trigger_cleanup(q); 2236 } 2237 2238 static void guc_exec_queue_add_msg(struct xe_exec_queue *q, struct xe_sched_msg *msg, 2239 u32 opcode) 2240 { 2241 struct xe_device *xe = guc_to_xe(exec_queue_to_guc(q)); 2242 int idx; 2243 bool bound = drm_dev_enter(&xe->drm, &idx); 2244 2245 INIT_LIST_HEAD(&msg->link); 2246 msg->opcode = opcode & OPCODE_MASK; 2247 if (bound) { 2248 xe_pm_runtime_get_noresume(xe); 2249 msg->opcode |= MSG_PM_REF; 2250 } 2251 msg->private_data = q; 2252 2253 trace_xe_sched_msg_add(msg); 2254 if (opcode & MSG_HEAD) 2255 xe_sched_add_msg_head(&q->guc->sched, msg); 2256 else if (opcode & MSG_LOCKED) 2257 xe_sched_add_msg_locked(&q->guc->sched, msg); 2258 else 2259 xe_sched_add_msg(&q->guc->sched, msg); 2260 2261 if (bound) 2262 drm_dev_exit(idx); 2263 } 2264 2265 static void guc_exec_queue_try_add_msg_head(struct xe_exec_queue *q, 2266 struct xe_sched_msg *msg, 2267 u32 opcode) 2268 { 2269 if (!list_empty(&msg->link)) 2270 return; 2271 2272 guc_exec_queue_add_msg(q, msg, opcode | MSG_LOCKED | MSG_HEAD); 2273 } 2274 2275 static bool guc_exec_queue_try_add_msg(struct xe_exec_queue *q, 2276 struct xe_sched_msg *msg, 2277 u32 opcode) 2278 { 2279 if (!list_empty(&msg->link)) 2280 return false; 2281 2282 guc_exec_queue_add_msg(q, msg, opcode | MSG_LOCKED); 2283 2284 return true; 2285 } 2286 2287 #define STATIC_MSG_CLEANUP 0 2288 #define STATIC_MSG_SUSPEND 1 2289 #define STATIC_MSG_RESUME 2 2290 #define STATIC_MSG_CGP_SYNC 3 2291 static void guc_exec_queue_destroy(struct xe_exec_queue *q) 2292 { 2293 struct xe_sched_msg *msg = q->guc->static_msgs + STATIC_MSG_CLEANUP; 2294 2295 guc_exec_queue_add_msg(q, msg, CLEANUP); 2296 } 2297 2298 static int guc_exec_queue_set_priority(struct xe_exec_queue *q, 2299 enum xe_exec_queue_priority priority) 2300 { 2301 struct xe_sched_msg *msg; 2302 2303 if (q->sched_props.priority == priority || 2304 exec_queue_killed_or_banned_or_wedged(q)) 2305 return 0; 2306 2307 msg = kmalloc_obj(*msg); 2308 if (!msg) 2309 return -ENOMEM; 2310 2311 q->sched_props.priority = priority; 2312 guc_exec_queue_add_msg(q, msg, SET_SCHED_PROPS); 2313 2314 return 0; 2315 } 2316 2317 static int guc_exec_queue_set_timeslice(struct xe_exec_queue *q, u32 timeslice_us) 2318 { 2319 struct xe_sched_msg *msg; 2320 2321 if (q->sched_props.timeslice_us == timeslice_us || 2322 exec_queue_killed_or_banned_or_wedged(q)) 2323 return 0; 2324 2325 msg = kmalloc_obj(*msg); 2326 if (!msg) 2327 return -ENOMEM; 2328 2329 q->sched_props.timeslice_us = timeslice_us; 2330 guc_exec_queue_add_msg(q, msg, SET_SCHED_PROPS); 2331 2332 return 0; 2333 } 2334 2335 static int guc_exec_queue_set_preempt_timeout(struct xe_exec_queue *q, 2336 u32 preempt_timeout_us) 2337 { 2338 struct xe_sched_msg *msg; 2339 2340 if (q->sched_props.preempt_timeout_us == preempt_timeout_us || 2341 exec_queue_killed_or_banned_or_wedged(q)) 2342 return 0; 2343 2344 msg = kmalloc_obj(*msg); 2345 if (!msg) 2346 return -ENOMEM; 2347 2348 q->sched_props.preempt_timeout_us = preempt_timeout_us; 2349 guc_exec_queue_add_msg(q, msg, SET_SCHED_PROPS); 2350 2351 return 0; 2352 } 2353 2354 static int guc_exec_queue_set_multi_queue_priority(struct xe_exec_queue *q, 2355 enum xe_multi_queue_priority priority) 2356 { 2357 struct xe_sched_msg *msg; 2358 2359 xe_gt_assert(guc_to_gt(exec_queue_to_guc(q)), xe_exec_queue_is_multi_queue(q)); 2360 2361 if (exec_queue_killed_or_banned_or_wedged(q)) 2362 return 0; 2363 2364 msg = kmalloc_obj(*msg); 2365 if (!msg) 2366 return -ENOMEM; 2367 2368 scoped_guard(spinlock, &q->multi_queue.lock) { 2369 if (q->multi_queue.priority == priority) { 2370 kfree(msg); 2371 return 0; 2372 } 2373 2374 q->multi_queue.priority = priority; 2375 } 2376 2377 guc_exec_queue_add_msg(q, msg, SET_MULTI_QUEUE_PRIORITY); 2378 2379 return 0; 2380 } 2381 2382 /* 2383 * Core suspend: take a suspend reference on @q and, on the first reference, 2384 * disable its GuC context so the GPU is actually preempted. Caller must have 2385 * ensured @q is not killed/banned/wedged. Returns true if this was the first 2386 * suspend reference (the 0->1 transition). 2387 */ 2388 static bool __guc_exec_queue_suspend(struct xe_exec_queue *q) 2389 { 2390 struct xe_guc_exec_queue *ge = q->guc; 2391 struct xe_gpu_scheduler *sched = &ge->sched; 2392 struct xe_sched_msg *msg = ge->static_msgs + STATIC_MSG_SUSPEND; 2393 bool first; 2394 2395 xe_sched_msg_lock(sched); 2396 first = (++ge->suspend_count == 1); 2397 if (first) { 2398 bool added = guc_exec_queue_try_add_msg(q, msg, SUSPEND); 2399 2400 /* slot must be free at 0->1 */ 2401 xe_gt_assert(guc_to_gt(exec_queue_to_guc(q)), added); 2402 ge->suspend_pending = true; 2403 } 2404 xe_sched_msg_unlock(sched); 2405 2406 return first; 2407 } 2408 2409 /* 2410 * Core resume: drop a suspend reference on @q and, on the last reference, 2411 * re-enable its GuC context. Returns true if this dropped the last suspend 2412 * reference (the 1->0 transition). 2413 */ 2414 static bool __guc_exec_queue_resume(struct xe_exec_queue *q) 2415 { 2416 struct xe_guc_exec_queue *ge = q->guc; 2417 struct xe_gpu_scheduler *sched = &ge->sched; 2418 struct xe_sched_msg *msg = ge->static_msgs + STATIC_MSG_RESUME; 2419 struct xe_guc *guc = exec_queue_to_guc(q); 2420 bool last; 2421 2422 xe_sched_msg_lock(sched); 2423 /* 2424 * A killed, banned or wedged queue keeps suspend_pending set until it 2425 * is torn down, so only assert the flag is clear otherwise. 2426 */ 2427 xe_gt_assert(guc_to_gt(guc), !ge->suspend_pending || 2428 exec_queue_killed_or_banned_or_wedged(q)); 2429 xe_gt_assert(guc_to_gt(guc), ge->suspend_count > 0); 2430 last = (--ge->suspend_count == 0); 2431 if (last) { 2432 bool added = guc_exec_queue_try_add_msg(q, msg, RESUME); 2433 2434 /* slot must be free at 1->0 */ 2435 xe_gt_assert(guc_to_gt(guc), added); 2436 } 2437 xe_sched_msg_unlock(sched); 2438 2439 return last; 2440 } 2441 2442 static int guc_exec_queue_suspend(struct xe_exec_queue *q) 2443 { 2444 if (exec_queue_killed_or_banned_or_wedged(q)) 2445 return -EINVAL; 2446 2447 /* 2448 * Non-multi-queue queues and multi-queue primaries suspend themselves 2449 * directly: their own msg_lock makes the suspend_count 0->1 transition 2450 * and the suspend_pending update atomic, so no group level serialization 2451 * is needed. 2452 */ 2453 if (!xe_exec_queue_is_multi_queue_secondary(q)) { 2454 __guc_exec_queue_suspend(q); 2455 return 0; 2456 } 2457 2458 /* 2459 * A secondary's suspend is meaningless once the primary - which owns the 2460 * group's GuC context - is gone, so fail it too. This keeps the 2461 * secondary's effective state consistent with guc_exec_queue_reset_status(), 2462 * which already reports the primary's killed/banned/wedged state for 2463 * secondaries. A primary killed *after* this check is still handled at 2464 * message-processing time, where the SUSPEND is a no-op for a killed 2465 * context; this only covers an already-dead primary. 2466 */ 2467 if (exec_queue_killed_or_banned_or_wedged(xe_exec_queue_multi_queue_primary(q))) 2468 return -EINVAL; 2469 2470 /* 2471 * A secondary doesn't interface with GuC: suspend it like any other 2472 * queue (its own suspend_count drives its internally handled scheduler 2473 * state) and, only on its own 0->1 transition, forward the suspend to the 2474 * primary so the GPU is actually preempted. Hold @suspend_lock so that 2475 * observing the secondary's transition and forwarding it to the primary 2476 * happen atomically; this keeps the primary's refcount paired with member 2477 * transitions even if the same secondary is suspended and resumed 2478 * concurrently across rebind cycles. 2479 */ 2480 scoped_guard(spinlock, &q->multi_queue.group->suspend_lock) { 2481 if (__guc_exec_queue_suspend(q)) 2482 __guc_exec_queue_suspend(xe_exec_queue_multi_queue_primary(q)); 2483 } 2484 2485 return 0; 2486 } 2487 2488 static void guc_exec_queue_suspend_timeout_ban(struct xe_exec_queue *q) 2489 { 2490 struct xe_guc *guc = exec_queue_to_guc(q); 2491 2492 xe_gt_warn(guc_to_gt(guc), 2493 "Suspend fence, guc_id=%d, failed to respond, banning queue", 2494 q->guc->id); 2495 /* 2496 * The GuC failed to respond to the suspend within the timeout. This is 2497 * not recoverable for this context, so ban it and tear it down via 2498 * cleanup rather than leave it suspended forever. The suspend fence is 2499 * not signalled here, the queue may still be running on the hardware, 2500 * so suspend_pending stays set until teardown. The waiter that timed 2501 * out is the caller, so no wake up is needed either. 2502 * 2503 * @q is the primary here; it owns the group's GuC context, so a failure 2504 * to suspend it wedges the whole group. Ban and tear down the entire 2505 * group in the multi-queue case. 2506 */ 2507 if (xe_exec_queue_is_multi_queue(q)) { 2508 set_exec_queue_group_banned(q); 2509 xe_guc_exec_queue_group_trigger_cleanup(q); 2510 } else { 2511 set_exec_queue_banned(q); 2512 xe_guc_exec_queue_trigger_cleanup(q); 2513 } 2514 } 2515 2516 /* 2517 * Wait for @q's own suspend to complete: suspend_pending cleared, or the queue 2518 * killed / GuC stopped. With @blocking, wait uninterruptibly and do not handle 2519 * VF recovery (for callers that must complete on behalf of a possibly 2520 * cross-process queue); otherwise wait interruptibly. 2521 * 2522 * Returns 0 on completion or -ETIME on timeout. Interruptible waits may also 2523 * return -EAGAIN (VF recovery in progress, retry) or -ERESTARTSYS (aborted by a 2524 * signal; suspend_pending may still be set, so callers must not resume() 2525 * without re-confirming the suspend). 2526 */ 2527 static int guc_exec_queue_wait_suspend_done(struct xe_exec_queue *q, bool blocking) 2528 { 2529 struct xe_guc *guc = exec_queue_to_guc(q); 2530 struct xe_device *xe = guc_to_xe(guc); 2531 int ret; 2532 2533 /* 2534 * A kill does not clear suspend_pending, the queue may still be running 2535 * on the hardware, so exec_queue_killed() is what ends the wait in that 2536 * case and the queue is torn down asynchronously. 2537 */ 2538 #define WAIT_COND \ 2539 (!READ_ONCE(q->guc->suspend_pending) || exec_queue_killed(q) || \ 2540 xe_guc_read_stopped(guc)) 2541 2542 retry: 2543 if (blocking) { 2544 if (IS_SRIOV_VF(xe)) 2545 ret = wait_event_timeout(guc->ct.wq, WAIT_COND, HZ * 5); 2546 else 2547 ret = wait_event_timeout(q->guc->suspend_wait, WAIT_COND, 2548 HZ * 5); 2549 } else if (IS_SRIOV_VF(xe)) { 2550 ret = wait_event_interruptible_timeout(guc->ct.wq, WAIT_COND || 2551 vf_recovery(guc), HZ * 5); 2552 } else { 2553 ret = wait_event_interruptible_timeout(q->guc->suspend_wait, 2554 WAIT_COND, HZ * 5); 2555 } 2556 2557 if (!blocking && vf_recovery(guc) && !xe_device_wedged(xe)) 2558 return -EAGAIN; 2559 2560 if (!ret) 2561 return -ETIME; 2562 else if (!blocking && IS_SRIOV_VF(xe) && !WAIT_COND) 2563 /* Corner case on RESFIX DONE where vf_recovery() changes */ 2564 goto retry; 2565 2566 #undef WAIT_COND 2567 2568 return ret < 0 ? ret : 0; 2569 } 2570 2571 static int guc_exec_queue_suspend_wait_common(struct xe_exec_queue *q, bool blocking) 2572 { 2573 int ret; 2574 2575 /* 2576 * A secondary's suspend rides the sched-message worker (short-circuited, 2577 * no GuC round-trip) and so is not synchronous with 2578 * guc_exec_queue_suspend(): its own suspend_pending may still be set 2579 * here. Waiting on the primary alone is not sufficient - if the primary 2580 * was already suspended, the forward is a refcount-only transition that 2581 * queues no new primary SUSPEND and leaves the primary's suspend_pending 2582 * clear, so the primary wait would return immediately while the 2583 * secondary's suspend is still in flight, and a later resume() would trip 2584 * the secondary's !suspend_pending assert. So first wait for the 2585 * secondary's own suspend to complete, then wait on the primary. 2586 * 2587 * A timeout on either bans the queue (being multi-queue, that tears down 2588 * the whole group). A secondary suspend has no real GuC round-trip, so 2589 * its timeout is a software scheduler stall rather than a GuC fault, but 2590 * banning is still the safe recovery: otherwise the queue is left with 2591 * suspend_pending set and a subsequent resume() trips the !suspend_pending 2592 * assert. 2593 */ 2594 if (xe_exec_queue_is_multi_queue_secondary(q)) { 2595 ret = guc_exec_queue_wait_suspend_done(q, blocking); 2596 if (ret == -ETIME) 2597 guc_exec_queue_suspend_timeout_ban(q); 2598 if (ret) 2599 return ret; 2600 } 2601 2602 q = xe_exec_queue_multi_queue_primary(q); 2603 ret = guc_exec_queue_wait_suspend_done(q, blocking); 2604 if (ret == -ETIME) 2605 guc_exec_queue_suspend_timeout_ban(q); 2606 2607 return ret; 2608 } 2609 2610 static int guc_exec_queue_suspend_wait(struct xe_exec_queue *q) 2611 { 2612 return guc_exec_queue_suspend_wait_common(q, false); 2613 } 2614 2615 /* 2616 * Uninterruptible variant of guc_exec_queue_suspend_wait() for callers that 2617 * must complete the wait on behalf of a queue possibly owned by a different 2618 * process (e.g. cleanup/undo paths). An interruptible wait could return 2619 * -ERESTARTSYS if the calling task is signalled, leaving that queue suspended 2620 * forever (cross-process DoS). VF recovery is deliberately not handled (no 2621 * -EAGAIN) since a blocking caller cannot retry. 2622 */ 2623 static int guc_exec_queue_suspend_wait_blocking(struct xe_exec_queue *q) 2624 { 2625 return guc_exec_queue_suspend_wait_common(q, true); 2626 } 2627 2628 static void guc_exec_queue_resume(struct xe_exec_queue *q) 2629 { 2630 /* 2631 * Non-multi-queue queues and multi-queue primaries resume themselves 2632 * directly; their own msg_lock is sufficient. 2633 */ 2634 if (!xe_exec_queue_is_multi_queue_secondary(q)) { 2635 __guc_exec_queue_resume(q); 2636 return; 2637 } 2638 2639 /* 2640 * Mirror of guc_exec_queue_suspend(): resume the secondary like any 2641 * other queue and, only on its own 1->0 transition, forward the resume 2642 * to the primary so the primary's GuC context is re-enabled once the 2643 * last member that suspended it resumes. @suspend_lock keeps the 2644 * secondary transition and the primary forward atomic. 2645 */ 2646 scoped_guard(spinlock, &q->multi_queue.group->suspend_lock) { 2647 if (__guc_exec_queue_resume(q)) 2648 __guc_exec_queue_resume(xe_exec_queue_multi_queue_primary(q)); 2649 } 2650 } 2651 2652 /* 2653 * Drop a leaving secondary's forwarded suspend reference on the primary and 2654 * resume the primary if this was the last member that had it suspended. 2655 * See guc_exec_queue_fini(). 2656 */ 2657 static void guc_exec_queue_multi_queue_drop_suspend(struct xe_exec_queue *q) 2658 { 2659 scoped_guard(spinlock, &q->multi_queue.group->suspend_lock) { 2660 struct xe_exec_queue *primary = xe_exec_queue_multi_queue_primary(q); 2661 2662 /* 2663 * A suspended secondary holds exactly one suspend reference on the 2664 * primary (forwarded on its 0->1 transition). If it leaves while 2665 * still suspended, release that reference so the primary is not 2666 * kept disabled forever. 2667 */ 2668 if (!READ_ONCE(q->guc->suspend_count)) 2669 break; 2670 2671 if (exec_queue_killed_or_banned_or_wedged(primary)) 2672 break; 2673 2674 /* 2675 * No suspend_wait() here (and we can't - suspend_lock is a 2676 * spinlock). guc_exec_queue_fini() has already drained the 2677 * primary's forwarded suspend with the blocking wait, so its 2678 * suspend has completed (suspend_pending cleared) by the time we 2679 * resume it here. __guc_exec_queue_resume() asserts this. 2680 */ 2681 __guc_exec_queue_resume(primary); 2682 } 2683 } 2684 2685 static u64 guc_exec_queue_reset_status(struct xe_exec_queue *q) 2686 { 2687 /* TODO: In case of multiqueue, if a secondary queue is banned due to 2688 * page offlining, checking only the primary queue's GuC reset status 2689 * may mask the true reason or race with it. 2690 */ 2691 if (xe_exec_queue_is_multi_queue_secondary(q)) { 2692 u64 status = guc_exec_queue_reset_status(xe_exec_queue_multi_queue_primary(q)); 2693 2694 if (status) 2695 return status; 2696 } 2697 2698 if (exec_queue_reset(q) || exec_queue_killed_or_banned_or_wedged(q)) { 2699 u64 reason = atomic_read_acquire(&q->ban_reason); 2700 2701 /* If no specific reason was recorded, default to GPU hang */ 2702 if (!reason) 2703 reason = DRM_XE_EXEC_QUEUE_BAN_REASON_GPU_HANG; 2704 return reason; 2705 } 2706 2707 return 0; 2708 } 2709 2710 /* 2711 * All of these functions are an abstraction layer which other parts of Xe can 2712 * use to trap into the GuC backend. All of these functions, aside from init, 2713 * really shouldn't do much other than trap into the DRM scheduler which 2714 * synchronizes these operations. 2715 */ 2716 static const struct xe_exec_queue_ops guc_exec_queue_ops = { 2717 .init = guc_exec_queue_init, 2718 .kill = guc_exec_queue_kill, 2719 .fini = guc_exec_queue_fini, 2720 .destroy = guc_exec_queue_destroy, 2721 .set_priority = guc_exec_queue_set_priority, 2722 .set_timeslice = guc_exec_queue_set_timeslice, 2723 .set_preempt_timeout = guc_exec_queue_set_preempt_timeout, 2724 .set_multi_queue_priority = guc_exec_queue_set_multi_queue_priority, 2725 .suspend = guc_exec_queue_suspend, 2726 .suspend_wait = guc_exec_queue_suspend_wait, 2727 .suspend_wait_blocking = guc_exec_queue_suspend_wait_blocking, 2728 .resume = guc_exec_queue_resume, 2729 .reset_status = guc_exec_queue_reset_status, 2730 }; 2731 2732 static void guc_exec_queue_stop(struct xe_guc *guc, struct xe_exec_queue *q) 2733 { 2734 struct xe_gpu_scheduler *sched = &q->guc->sched; 2735 bool do_destroy = false; 2736 2737 /* Stop scheduling + flush any DRM scheduler operations */ 2738 xe_sched_submission_stop(sched); 2739 2740 /* Clean up lost G2H + reset engine state */ 2741 if (exec_queue_registered(q)) { 2742 if (exec_queue_destroyed(q)) 2743 do_destroy = true; 2744 } 2745 xe_sched_msg_lock(sched); 2746 if (q->guc->suspend_pending) { 2747 set_exec_queue_suspended(q); 2748 suspend_fence_signal(q); 2749 } 2750 xe_sched_msg_unlock(sched); 2751 atomic_and(EXEC_QUEUE_STATE_WEDGED | EXEC_QUEUE_STATE_BANNED | 2752 EXEC_QUEUE_STATE_KILLED | EXEC_QUEUE_STATE_DESTROYED | 2753 EXEC_QUEUE_STATE_SUSPENDED, 2754 &q->guc->state); 2755 q->guc->resume_time = 0; 2756 trace_xe_exec_queue_stop(q); 2757 2758 /* 2759 * Ban any engine (aside from kernel and engines used for VM ops) with a 2760 * started but not complete job or if a job has gone through a GT reset 2761 * more than twice. 2762 */ 2763 if (!(q->flags & (EXEC_QUEUE_FLAG_KERNEL | EXEC_QUEUE_FLAG_VM))) { 2764 struct xe_sched_job *job = xe_sched_first_pending_job(sched); 2765 bool ban = false; 2766 2767 if (job) { 2768 if ((xe_sched_job_started(job) && 2769 !xe_sched_job_completed(job)) || 2770 xe_sched_invalidate_job(job, 2)) { 2771 trace_xe_sched_job_ban(job); 2772 ban = true; 2773 } 2774 } 2775 2776 if (ban) { 2777 set_exec_queue_banned(q); 2778 xe_guc_exec_queue_trigger_cleanup(q); 2779 } 2780 } 2781 2782 if (do_destroy) 2783 guc_exec_queue_destroy_async(q); 2784 } 2785 2786 static int guc_submit_reset_prepare(struct xe_guc *guc) 2787 { 2788 int ret; 2789 2790 /* 2791 * Using an atomic here rather than submission_state.lock as this 2792 * function can be called while holding the CT lock (engine reset 2793 * failure). submission_state.lock needs the CT lock to resubmit jobs. 2794 * Atomic is not ideal, but it works to prevent against concurrent reset 2795 * and releasing any TDRs waiting on guc->submission_state.stopped. 2796 */ 2797 ret = atomic_fetch_or(1, &guc->submission_state.stopped); 2798 smp_wmb(); 2799 wake_up_all(&guc->ct.wq); 2800 2801 return ret; 2802 } 2803 2804 int xe_guc_submit_reset_prepare(struct xe_guc *guc) 2805 { 2806 if (xe_gt_WARN_ON(guc_to_gt(guc), vf_recovery(guc))) 2807 return 0; 2808 2809 if (!guc->submission_state.initialized) 2810 return 0; 2811 2812 return guc_submit_reset_prepare(guc); 2813 } 2814 2815 void xe_guc_submit_reset_wait(struct xe_guc *guc) 2816 { 2817 wait_event(guc->ct.wq, xe_device_wedged(guc_to_xe(guc)) || 2818 !xe_guc_read_stopped(guc)); 2819 } 2820 2821 void xe_guc_submit_stop(struct xe_guc *guc) 2822 { 2823 struct xe_exec_queue *q; 2824 unsigned long index; 2825 2826 xe_gt_assert(guc_to_gt(guc), xe_guc_read_stopped(guc) == 1); 2827 2828 mutex_lock(&guc->submission_state.lock); 2829 2830 xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) { 2831 /* Prevent redundant attempts to stop parallel queues */ 2832 if (q->guc->id != index) 2833 continue; 2834 2835 guc_exec_queue_stop(guc, q); 2836 } 2837 2838 mutex_unlock(&guc->submission_state.lock); 2839 2840 /* 2841 * No one can enter the backend at this point, aside from new engine 2842 * creation which is protected by guc->submission_state.lock. 2843 */ 2844 2845 } 2846 2847 static void guc_exec_queue_revert_pending_state_change(struct xe_guc *guc, 2848 struct xe_exec_queue *q) 2849 { 2850 bool pending_enable, pending_disable, pending_resume; 2851 2852 pending_enable = exec_queue_pending_enable(q); 2853 pending_resume = exec_queue_pending_resume(q); 2854 2855 if (pending_enable && pending_resume) { 2856 q->guc->needs_resume = true; 2857 xe_gt_dbg(guc_to_gt(guc), "Replay RESUME - guc_id=%d", 2858 q->guc->id); 2859 } 2860 2861 if (pending_enable && !pending_resume) { 2862 clear_exec_queue_registered(q); 2863 xe_gt_dbg(guc_to_gt(guc), "Replay REGISTER - guc_id=%d", 2864 q->guc->id); 2865 } 2866 2867 if (pending_enable) { 2868 clear_exec_queue_enabled(q); 2869 clear_exec_queue_pending_resume(q); 2870 clear_exec_queue_pending_enable(q); 2871 xe_gt_dbg(guc_to_gt(guc), "Replay ENABLE - guc_id=%d", 2872 q->guc->id); 2873 } 2874 2875 if (exec_queue_destroyed(q) && exec_queue_registered(q)) { 2876 clear_exec_queue_destroyed(q); 2877 q->guc->needs_cleanup = true; 2878 xe_gt_dbg(guc_to_gt(guc), "Replay CLEANUP - guc_id=%d", 2879 q->guc->id); 2880 } 2881 2882 pending_disable = exec_queue_pending_disable(q); 2883 2884 if (pending_disable && exec_queue_suspended(q)) { 2885 clear_exec_queue_suspended(q); 2886 q->guc->needs_suspend = true; 2887 xe_gt_dbg(guc_to_gt(guc), "Replay SUSPEND - guc_id=%d", 2888 q->guc->id); 2889 } 2890 2891 if (pending_disable) { 2892 if (!pending_enable) 2893 set_exec_queue_enabled(q); 2894 clear_exec_queue_pending_disable(q); 2895 xe_gt_dbg(guc_to_gt(guc), "Replay DISABLE - guc_id=%d", 2896 q->guc->id); 2897 } 2898 2899 /* 2900 * A registration time CGP update that bailed when woken by VF recovery. 2901 * Re-register the queue. 2902 */ 2903 if (q->guc->multi_queue.re_register) { 2904 clear_exec_queue_registered(q); 2905 q->guc->multi_queue.re_register = false; 2906 xe_gt_dbg(guc_to_gt(guc), "Replay REGISTER (cgp) - guc_id=%d", 2907 q->guc->id); 2908 } 2909 2910 /* 2911 * If a CGP update gets dropped during migration, CGP_SYNC_DONE will not 2912 * be received (sync_pending still set and this queue owns it). Recover 2913 * it the same way and clear the stuck sync_pending. 2914 */ 2915 if (xe_exec_queue_is_multi_queue(q)) { 2916 struct xe_exec_queue_group *group = q->multi_queue.group; 2917 2918 if (q == READ_ONCE(group->cgp_update_q) && 2919 READ_ONCE(group->sync_pending)) { 2920 if (q->guc->multi_queue.registering_cgp) { 2921 clear_exec_queue_registered(q); 2922 xe_gt_dbg(guc_to_gt(guc), "Replay REGISTER (cgp sync) - guc_id=%d", 2923 q->guc->id); 2924 } else if (q->guc->multi_queue.updating_cgp) { 2925 q->guc->multi_queue.needs_cgp_sync = true; 2926 xe_gt_dbg(guc_to_gt(guc), "Replay CGP_SYNC - guc_id=%d", 2927 q->guc->id); 2928 } 2929 q->guc->multi_queue.registering_cgp = false; 2930 q->guc->multi_queue.updating_cgp = false; 2931 WRITE_ONCE(group->cgp_update_q, NULL); 2932 WRITE_ONCE(group->sync_pending, false); 2933 } 2934 } 2935 2936 /* 2937 * A dynamic-time CGP update that bailed when woken by VF recovery. 2938 * Replay the dynamic CGP update unless the queue is registered or being 2939 * re-registered, which re-does the CGP anyway. 2940 */ 2941 if (q->guc->multi_queue.re_update) { 2942 q->guc->multi_queue.re_update = false; 2943 if (exec_queue_registered(q)) { 2944 q->guc->multi_queue.needs_cgp_sync = true; 2945 xe_gt_dbg(guc_to_gt(guc), "Replay CGP_SYNC (re-update) - guc_id=%d", 2946 q->guc->id); 2947 } 2948 } 2949 2950 q->guc->resume_time = 0; 2951 } 2952 2953 static void lrc_parallel_clear(struct xe_lrc *lrc) 2954 { 2955 struct xe_device *xe = gt_to_xe(lrc->gt); 2956 struct iosys_map map = xe_lrc_parallel_map(lrc); 2957 int i; 2958 2959 for (i = 0; i < WQ_SIZE / sizeof(u32); ++i) 2960 parallel_write(xe, map, wq[i], 2961 FIELD_PREP(WQ_TYPE_MASK, WQ_TYPE_NOOP) | 2962 FIELD_PREP(WQ_LEN_MASK, 0)); 2963 } 2964 2965 /* 2966 * This function is quite complex but only real way to ensure no state is lost 2967 * during VF resume flows. The function scans the queue state, make adjustments 2968 * as needed, and queues jobs / messages which replayed upon unpause. 2969 */ 2970 static void guc_exec_queue_pause_prepare(struct xe_guc *guc, struct xe_exec_queue *q) 2971 { 2972 struct xe_gpu_scheduler *sched = &q->guc->sched; 2973 2974 lockdep_assert_held(&guc->submission_state.lock); 2975 2976 /* Stop scheduling + flush any DRM scheduler operations */ 2977 xe_sched_submission_stop(sched); 2978 cancel_delayed_work_sync(&sched->base.work_tdr); 2979 } 2980 2981 static void guc_exec_queue_pause(struct xe_guc *guc, struct xe_exec_queue *q) 2982 { 2983 struct xe_gpu_scheduler *sched = &q->guc->sched; 2984 struct xe_sched_job *job; 2985 int i; 2986 2987 lockdep_assert_held(&guc->submission_state.lock); 2988 2989 guc_exec_queue_revert_pending_state_change(guc, q); 2990 2991 if (xe_exec_queue_is_parallel(q)) { 2992 /* Pairs with WRITE_ONCE in __xe_exec_queue_init */ 2993 struct xe_lrc *lrc = READ_ONCE(q->lrc[0]); 2994 2995 /* 2996 * NOP existing WQ commands that may contain stale GGTT 2997 * addresses. These will be replayed upon unpause. The hardware 2998 * seems to get confused if the WQ head/tail pointers are 2999 * adjusted. 3000 */ 3001 if (lrc) 3002 lrc_parallel_clear(lrc); 3003 } 3004 3005 job = xe_sched_first_pending_job(sched); 3006 if (job) { 3007 job->restore_replay = true; 3008 3009 /* 3010 * Adjust software tail so jobs submitted overwrite previous 3011 * position in ring buffer with new GGTT addresses. 3012 */ 3013 for (i = 0; i < q->width; ++i) 3014 q->lrc[i]->ring.tail = job->ptrs[i].head; 3015 } 3016 } 3017 3018 /** 3019 * xe_guc_submit_pause - Stop further runs of submission tasks on given GuC. 3020 * @guc: the &xe_guc struct instance whose scheduler is to be disabled 3021 */ 3022 void xe_guc_submit_pause(struct xe_guc *guc) 3023 { 3024 struct xe_exec_queue *q; 3025 unsigned long index; 3026 3027 mutex_lock(&guc->submission_state.lock); 3028 xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) 3029 xe_sched_submission_stop(&q->guc->sched); 3030 mutex_unlock(&guc->submission_state.lock); 3031 } 3032 3033 /** 3034 * xe_guc_submit_pause_vf - Stop further runs of submission tasks for VF. 3035 * @guc: the &xe_guc struct instance whose scheduler is to be disabled 3036 */ 3037 void xe_guc_submit_pause_vf(struct xe_guc *guc) 3038 { 3039 struct xe_exec_queue *q; 3040 unsigned long index; 3041 3042 xe_gt_assert(guc_to_gt(guc), IS_SRIOV_VF(guc_to_xe(guc))); 3043 xe_gt_assert(guc_to_gt(guc), vf_recovery(guc)); 3044 3045 mutex_lock(&guc->submission_state.lock); 3046 /* 3047 * Stop all schedulers before reverting any queue: in a multi-queue 3048 * group a secondary's run_job() can register the primary, which must 3049 * not race an in-progress revert. 3050 */ 3051 xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) { 3052 /* Prevent redundant attempts to stop parallel queues */ 3053 if (q->guc->id != index) 3054 continue; 3055 3056 guc_exec_queue_pause_prepare(guc, q); 3057 } 3058 3059 xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) { 3060 /* Prevent redundant attempts to stop parallel queues */ 3061 if (q->guc->id != index) 3062 continue; 3063 3064 guc_exec_queue_pause(guc, q); 3065 } 3066 mutex_unlock(&guc->submission_state.lock); 3067 } 3068 3069 static void guc_exec_queue_start(struct xe_exec_queue *q) 3070 { 3071 struct xe_gpu_scheduler *sched = &q->guc->sched; 3072 3073 if (!exec_queue_killed_or_banned_or_wedged(q)) { 3074 struct xe_sched_job *job = xe_sched_first_pending_job(sched); 3075 int i; 3076 3077 trace_xe_exec_queue_resubmit(q); 3078 if (job) { 3079 for (i = 0; i < q->width; ++i) { 3080 /* 3081 * The GuC context is unregistered at this point 3082 * time, adjusting software ring tail ensures 3083 * jobs are rewritten in original placement, 3084 * adjusting LRC tail ensures the newly loaded 3085 * GuC / contexts only view the LRC tail 3086 * increasing as jobs are written out. 3087 */ 3088 q->lrc[i]->ring.tail = job->ptrs[i].head; 3089 /* 3090 * A ULLS job's slot can have been consumed by 3091 * the CS without the job running, leaving the 3092 * saved head past it. Rewind so the re-emitted 3093 * job is ahead of the head, not behind it. 3094 */ 3095 if (xe_sched_job_is_ulls(job)) 3096 xe_lrc_set_ring_head(q->lrc[i], 3097 job->ptrs[i].head); 3098 xe_lrc_set_ring_tail(q->lrc[i], 3099 xe_lrc_ring_head(q->lrc[i])); 3100 } 3101 } 3102 xe_sched_resubmit_jobs(sched); 3103 } 3104 3105 xe_sched_submission_start(sched); 3106 xe_sched_submission_resume_tdr(sched); 3107 } 3108 3109 int xe_guc_submit_start(struct xe_guc *guc) 3110 { 3111 struct xe_exec_queue *q; 3112 unsigned long index; 3113 3114 xe_gt_assert(guc_to_gt(guc), xe_guc_read_stopped(guc) == 1); 3115 3116 mutex_lock(&guc->submission_state.lock); 3117 atomic_dec(&guc->submission_state.stopped); 3118 xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) { 3119 /* Prevent redundant attempts to start parallel queues */ 3120 if (q->guc->id != index) 3121 continue; 3122 3123 guc_exec_queue_start(q); 3124 } 3125 mutex_unlock(&guc->submission_state.lock); 3126 3127 wake_up_all(&guc->ct.wq); 3128 3129 return 0; 3130 } 3131 3132 static void guc_exec_queue_unpause_prepare(struct xe_guc *guc, 3133 struct xe_exec_queue *q) 3134 { 3135 struct xe_gpu_scheduler *sched = &q->guc->sched; 3136 struct xe_sched_job *job = NULL; 3137 struct drm_sched_job *s_job; 3138 bool restore_replay = false; 3139 3140 drm_sched_for_each_pending_job(s_job, &sched->base, NULL) { 3141 job = to_xe_sched_job(s_job); 3142 restore_replay |= job->restore_replay; 3143 if (restore_replay) { 3144 xe_gt_dbg(guc_to_gt(guc), "Replay JOB - guc_id=%d, seqno=%d", 3145 q->guc->id, xe_sched_job_seqno(job)); 3146 3147 q->ring_ops->emit_job(job); 3148 job->restore_replay = true; 3149 } 3150 } 3151 3152 if (job) 3153 job->last_replay = true; 3154 } 3155 3156 /** 3157 * xe_guc_submit_unpause_prepare_vf - Prepare unpause submission tasks for VF. 3158 * @guc: the &xe_guc struct instance whose scheduler is to be prepared for unpause 3159 */ 3160 void xe_guc_submit_unpause_prepare_vf(struct xe_guc *guc) 3161 { 3162 struct xe_exec_queue *q; 3163 unsigned long index; 3164 3165 xe_gt_assert(guc_to_gt(guc), IS_SRIOV_VF(guc_to_xe(guc))); 3166 xe_gt_assert(guc_to_gt(guc), vf_recovery(guc)); 3167 3168 mutex_lock(&guc->submission_state.lock); 3169 xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) { 3170 /* Prevent redundant attempts to stop parallel queues */ 3171 if (q->guc->id != index) 3172 continue; 3173 3174 guc_exec_queue_unpause_prepare(guc, q); 3175 } 3176 mutex_unlock(&guc->submission_state.lock); 3177 } 3178 3179 static void guc_exec_queue_replay_pending_state_change(struct xe_exec_queue *q) 3180 { 3181 struct xe_gpu_scheduler *sched = &q->guc->sched; 3182 struct xe_sched_msg *msg; 3183 3184 if (q->guc->multi_queue.needs_cgp_sync) { 3185 msg = q->guc->static_msgs + STATIC_MSG_CGP_SYNC; 3186 3187 xe_sched_msg_lock(sched); 3188 guc_exec_queue_try_add_msg_head(q, msg, CGP_SYNC_MSG); 3189 xe_sched_msg_unlock(sched); 3190 3191 q->guc->multi_queue.needs_cgp_sync = false; 3192 } 3193 3194 if (q->guc->needs_cleanup) { 3195 msg = q->guc->static_msgs + STATIC_MSG_CLEANUP; 3196 3197 guc_exec_queue_add_msg(q, msg, CLEANUP); 3198 q->guc->needs_cleanup = false; 3199 } 3200 3201 if (q->guc->needs_suspend) { 3202 msg = q->guc->static_msgs + STATIC_MSG_SUSPEND; 3203 3204 xe_sched_msg_lock(sched); 3205 guc_exec_queue_try_add_msg_head(q, msg, SUSPEND); 3206 xe_sched_msg_unlock(sched); 3207 3208 q->guc->needs_suspend = false; 3209 } 3210 3211 /* 3212 * The resume must be in the message queue before the suspend as it is 3213 * not possible for a resume to be issued if a suspend pending is, but 3214 * the inverse is possible. 3215 */ 3216 if (q->guc->needs_resume) { 3217 msg = q->guc->static_msgs + STATIC_MSG_RESUME; 3218 3219 xe_sched_msg_lock(sched); 3220 guc_exec_queue_try_add_msg_head(q, msg, RESUME); 3221 xe_sched_msg_unlock(sched); 3222 3223 q->guc->needs_resume = false; 3224 } 3225 } 3226 3227 static void guc_exec_queue_unpause(struct xe_guc *guc, struct xe_exec_queue *q) 3228 { 3229 struct xe_gpu_scheduler *sched = &q->guc->sched; 3230 bool needs_tdr = exec_queue_killed_or_banned_or_wedged(q); 3231 3232 lockdep_assert_held(&guc->submission_state.lock); 3233 3234 xe_sched_resubmit_jobs(sched); 3235 guc_exec_queue_replay_pending_state_change(q); 3236 xe_sched_submission_start(sched); 3237 if (needs_tdr) 3238 xe_guc_exec_queue_trigger_cleanup(q); 3239 xe_sched_submission_resume_tdr(sched); 3240 } 3241 3242 /** 3243 * xe_guc_submit_unpause - Allow further runs of submission tasks on given GuC. 3244 * @guc: the &xe_guc struct instance whose scheduler is to be enabled 3245 */ 3246 void xe_guc_submit_unpause(struct xe_guc *guc) 3247 { 3248 struct xe_exec_queue *q; 3249 unsigned long index; 3250 3251 mutex_lock(&guc->submission_state.lock); 3252 xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) 3253 xe_sched_submission_start(&q->guc->sched); 3254 mutex_unlock(&guc->submission_state.lock); 3255 } 3256 3257 /** 3258 * xe_guc_submit_unpause_vf - Allow further runs of submission tasks for VF. 3259 * @guc: the &xe_guc struct instance whose scheduler is to be enabled 3260 */ 3261 void xe_guc_submit_unpause_vf(struct xe_guc *guc) 3262 { 3263 struct xe_exec_queue *q; 3264 unsigned long index; 3265 3266 xe_gt_assert(guc_to_gt(guc), IS_SRIOV_VF(guc_to_xe(guc))); 3267 3268 mutex_lock(&guc->submission_state.lock); 3269 xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) { 3270 /* 3271 * Prevent redundant attempts to stop parallel queues, or queues 3272 * created after resfix done. 3273 */ 3274 if (q->guc->id != index || 3275 !drm_sched_is_stopped(&q->guc->sched.base)) 3276 continue; 3277 3278 guc_exec_queue_unpause(guc, q); 3279 } 3280 mutex_unlock(&guc->submission_state.lock); 3281 } 3282 3283 /** 3284 * xe_guc_submit_pause_abort - Abort all paused submission task on given GuC. 3285 * @guc: the &xe_guc struct instance whose scheduler is to be aborted 3286 */ 3287 void xe_guc_submit_pause_abort(struct xe_guc *guc) 3288 { 3289 struct xe_exec_queue *q; 3290 unsigned long index; 3291 3292 mutex_lock(&guc->submission_state.lock); 3293 xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) { 3294 struct xe_gpu_scheduler *sched = &q->guc->sched; 3295 3296 /* Prevent redundant attempts to stop parallel queues */ 3297 if (q->guc->id != index) 3298 continue; 3299 3300 xe_sched_submission_start(sched); 3301 guc_exec_queue_kill(q); 3302 } 3303 mutex_unlock(&guc->submission_state.lock); 3304 } 3305 3306 static struct xe_exec_queue * 3307 g2h_exec_queue_lookup(struct xe_guc *guc, u32 guc_id) 3308 { 3309 struct xe_gt *gt = guc_to_gt(guc); 3310 struct xe_exec_queue *q; 3311 3312 if (unlikely(guc_id >= GUC_ID_MAX)) { 3313 xe_gt_err(gt, "Invalid guc_id %u\n", guc_id); 3314 return NULL; 3315 } 3316 3317 q = xa_load(&guc->submission_state.exec_queue_lookup, guc_id); 3318 if (unlikely(!q)) { 3319 xe_gt_err(gt, "No exec queue found for guc_id %u\n", guc_id); 3320 return NULL; 3321 } 3322 3323 xe_gt_assert(guc_to_gt(guc), guc_id >= q->guc->id); 3324 xe_gt_assert(guc_to_gt(guc), guc_id < (q->guc->id + q->width)); 3325 3326 return q; 3327 } 3328 3329 static void deregister_exec_queue(struct xe_guc *guc, struct xe_exec_queue *q) 3330 { 3331 u32 action[] = { 3332 XE_GUC_ACTION_DEREGISTER_CONTEXT, 3333 q->guc->id, 3334 }; 3335 3336 xe_gt_assert(guc_to_gt(guc), exec_queue_destroyed(q)); 3337 xe_gt_assert(guc_to_gt(guc), exec_queue_registered(q)); 3338 xe_gt_assert(guc_to_gt(guc), !exec_queue_pending_disable(q)); 3339 xe_gt_assert(guc_to_gt(guc), !exec_queue_pending_enable(q)); 3340 3341 trace_xe_exec_queue_deregister(q); 3342 3343 if (xe_exec_queue_is_multi_queue_secondary(q)) 3344 handle_deregister_done(guc, q); 3345 else 3346 xe_guc_ct_send_g2h_handler(&guc->ct, action, 3347 ARRAY_SIZE(action)); 3348 } 3349 3350 static void handle_sched_done(struct xe_guc *guc, struct xe_exec_queue *q, 3351 u32 runnable_state) 3352 { 3353 trace_xe_exec_queue_scheduling_done(q); 3354 3355 if (runnable_state == 1) { 3356 xe_gt_assert(guc_to_gt(guc), exec_queue_pending_enable(q)); 3357 3358 q->guc->resume_time = ktime_get(); 3359 clear_exec_queue_pending_resume(q); 3360 clear_exec_queue_pending_enable(q); 3361 smp_wmb(); 3362 wake_up_all(&guc->ct.wq); 3363 } else { 3364 bool was_pending; 3365 3366 xe_gt_assert(guc_to_gt(guc), runnable_state == 0); 3367 xe_gt_assert(guc_to_gt(guc), exec_queue_pending_disable(q)); 3368 3369 /* 3370 * A TDR stops the scheduler, which can trap a queued SUSPEND 3371 * message, and then issues a disable of its own. suspend_pending 3372 * is still set in that case, so only attribute this SCHED_DONE 3373 * to a suspend if the SUSPEND message actually ran, i.e. it 3374 * marked the queue suspended before disabling scheduling. The 3375 * trapped message completes the suspend once submission 3376 * restarts. 3377 */ 3378 xe_sched_msg_lock(&q->guc->sched); 3379 was_pending = q->guc->suspend_pending && exec_queue_suspended(q); 3380 if (was_pending) { 3381 clear_exec_queue_pending_disable(q); 3382 suspend_fence_signal(q); 3383 } 3384 xe_sched_msg_unlock(&q->guc->sched); 3385 3386 if (!was_pending) { 3387 /* 3388 * Make sure to clear the pending_disable only after 3389 * sampling the destroyed state. We want to ensure we 3390 * don't trigger the unregister too early with something 3391 * intending to only disable scheduling. The caller 3392 * doing the destroy must wait for an ongoing 3393 * pending_disable before marking as destroyed. 3394 */ 3395 bool destroyed = exec_queue_destroyed(q); 3396 3397 clear_exec_queue_pending_disable(q); 3398 if (destroyed) 3399 deregister_exec_queue(guc, q); 3400 } 3401 3402 /* 3403 * Wake after pending_disable is cleared, not before, so waiters 3404 * such as the TDR see the updated state on the first re-check. 3405 */ 3406 smp_wmb(); 3407 wake_up_all(&guc->ct.wq); 3408 } 3409 } 3410 3411 static void handle_multi_queue_secondary_sched_done(struct xe_guc *guc, 3412 struct xe_exec_queue *q, 3413 u32 runnable_state) 3414 { 3415 /* Take CT lock here as handle_sched_done() do send a h2g message */ 3416 mutex_lock(&guc->ct.lock); 3417 handle_sched_done(guc, q, runnable_state); 3418 mutex_unlock(&guc->ct.lock); 3419 } 3420 3421 int xe_guc_sched_done_handler(struct xe_guc *guc, u32 *msg, u32 len) 3422 { 3423 struct xe_exec_queue *q; 3424 u32 guc_id, runnable_state; 3425 3426 if (unlikely(len < 2)) 3427 return -EPROTO; 3428 3429 guc_id = msg[0]; 3430 runnable_state = msg[1]; 3431 3432 q = g2h_exec_queue_lookup(guc, guc_id); 3433 if (unlikely(!q)) 3434 return -EPROTO; 3435 3436 if (unlikely(!exec_queue_pending_enable(q) && 3437 !exec_queue_pending_disable(q))) { 3438 xe_gt_err(guc_to_gt(guc), 3439 "SCHED_DONE: Unexpected engine state 0x%04x, guc_id=%d, runnable_state=%u", 3440 atomic_read(&q->guc->state), q->guc->id, 3441 runnable_state); 3442 return -EPROTO; 3443 } 3444 3445 handle_sched_done(guc, q, runnable_state); 3446 3447 return 0; 3448 } 3449 3450 static void handle_deregister_done(struct xe_guc *guc, struct xe_exec_queue *q) 3451 { 3452 trace_xe_exec_queue_deregister_done(q); 3453 3454 clear_exec_queue_registered(q); 3455 guc_exec_queue_destroy_async(q); 3456 } 3457 3458 int xe_guc_deregister_done_handler(struct xe_guc *guc, u32 *msg, u32 len) 3459 { 3460 struct xe_exec_queue *q; 3461 u32 guc_id; 3462 3463 if (unlikely(len < 1)) 3464 return -EPROTO; 3465 3466 guc_id = msg[0]; 3467 3468 q = g2h_exec_queue_lookup(guc, guc_id); 3469 if (unlikely(!q)) 3470 return -EPROTO; 3471 3472 if (!exec_queue_destroyed(q) || exec_queue_pending_disable(q) || 3473 exec_queue_pending_enable(q) || exec_queue_enabled(q)) { 3474 xe_gt_err(guc_to_gt(guc), 3475 "DEREGISTER_DONE: Unexpected engine state 0x%04x, guc_id=%d", 3476 atomic_read(&q->guc->state), q->guc->id); 3477 return -EPROTO; 3478 } 3479 3480 handle_deregister_done(guc, q); 3481 3482 return 0; 3483 } 3484 3485 int xe_guc_exec_queue_reset_handler(struct xe_guc *guc, u32 *msg, u32 len) 3486 { 3487 struct xe_gt *gt = guc_to_gt(guc); 3488 struct xe_exec_queue *q; 3489 u32 guc_id; 3490 3491 if (unlikely(len < 1)) 3492 return -EPROTO; 3493 3494 guc_id = msg[0]; 3495 3496 q = g2h_exec_queue_lookup(guc, guc_id); 3497 if (unlikely(!q)) 3498 return -EPROTO; 3499 3500 if (!exec_queue_killed(q)) 3501 xe_gt_info(gt, "Engine reset: engine_class=%s, logical_mask: 0x%x, guc_id=%d, state=0x%0x", 3502 xe_hw_engine_class_to_str(q->class), q->logical_mask, guc_id, 3503 atomic_read(&q->guc->state)); 3504 3505 trace_xe_exec_queue_reset(q); 3506 3507 /* 3508 * A banned engine is a NOP at this point (came from 3509 * guc_exec_queue_timedout_job). Otherwise, kick drm scheduler to cancel 3510 * jobs by setting timeout of the job to the minimum value kicking 3511 * guc_exec_queue_timedout_job. 3512 */ 3513 xe_guc_exec_queue_reset_trigger_cleanup(q); 3514 3515 return 0; 3516 } 3517 3518 /* 3519 * xe_guc_error_capture_handler - Handler of GuC captured message 3520 * @guc: The GuC object 3521 * @msg: Point to the message 3522 * @len: The message length 3523 * 3524 * When GuC captured data is ready, GuC will send message 3525 * XE_GUC_ACTION_STATE_CAPTURE_NOTIFICATION to host, this function will be 3526 * called 1st to check status before process the data comes with the message. 3527 * 3528 * Returns: error code. 0 if success 3529 */ 3530 int xe_guc_error_capture_handler(struct xe_guc *guc, u32 *msg, u32 len) 3531 { 3532 u32 status; 3533 3534 if (unlikely(len != XE_GUC_ACTION_STATE_CAPTURE_NOTIFICATION_DATA_LEN)) 3535 return -EPROTO; 3536 3537 status = msg[0] & XE_GUC_STATE_CAPTURE_EVENT_STATUS_MASK; 3538 if (status == XE_GUC_STATE_CAPTURE_EVENT_STATUS_NOSPACE) 3539 xe_gt_warn(guc_to_gt(guc), "G2H-Error capture no space"); 3540 3541 xe_guc_capture_process(guc); 3542 3543 return 0; 3544 } 3545 3546 int xe_guc_exec_queue_memory_cat_error_handler(struct xe_guc *guc, u32 *msg, 3547 u32 len) 3548 { 3549 struct xe_gt *gt = guc_to_gt(guc); 3550 struct xe_exec_queue *q; 3551 u32 guc_id; 3552 u32 type = XE_GUC_CAT_ERR_TYPE_INVALID; 3553 3554 if (unlikely(!len || len > 2)) 3555 return -EPROTO; 3556 3557 guc_id = msg[0]; 3558 3559 if (len == 2) 3560 type = msg[1]; 3561 3562 if (guc_id == GUC_ID_UNKNOWN) { 3563 /* 3564 * GuC uses GUC_ID_UNKNOWN if it can not map the CAT fault to any PF/VF 3565 * context. In such case only PF will be notified about that fault. 3566 */ 3567 xe_gt_err_ratelimited(gt, "Memory CAT error reported by GuC!\n"); 3568 return 0; 3569 } 3570 3571 q = g2h_exec_queue_lookup(guc, guc_id); 3572 if (unlikely(!q)) 3573 return -EPROTO; 3574 3575 /* 3576 * The type is HW-defined and changes based on platform, so we don't 3577 * decode it in the kernel and only check if it is valid. 3578 * See bspec 54047 and 72187 for details. 3579 */ 3580 if (type != XE_GUC_CAT_ERR_TYPE_INVALID) 3581 xe_gt_info(gt, 3582 "Engine memory CAT error [%u]: class=%s, logical_mask: 0x%x, guc_id=%d", 3583 type, xe_hw_engine_class_to_str(q->class), q->logical_mask, guc_id); 3584 else 3585 xe_gt_info(gt, 3586 "Engine memory CAT error: class=%s, logical_mask: 0x%x, guc_id=%d", 3587 xe_hw_engine_class_to_str(q->class), q->logical_mask, guc_id); 3588 3589 trace_xe_exec_queue_memory_cat_error(q); 3590 3591 /* Treat the same as engine reset */ 3592 xe_guc_exec_queue_reset_trigger_cleanup(q); 3593 3594 return 0; 3595 } 3596 3597 int xe_guc_uncorrectable_error_handler(struct xe_guc *guc, u32 *msg, u32 len) 3598 { 3599 struct xe_gt *gt = guc_to_gt(guc); 3600 struct xe_exec_queue *q; 3601 u32 guc_id; 3602 3603 if (unlikely(!len || len > 1)) 3604 return -EPROTO; 3605 3606 guc_id = msg[0]; 3607 3608 if (guc_id == GUC_ID_UNKNOWN) { 3609 xe_gt_err_ratelimited(gt, "GuC: Uncorrectable local error with unknown GuC id\n"); 3610 return 0; 3611 } 3612 3613 q = g2h_exec_queue_lookup(guc, guc_id); 3614 if (unlikely(!q)) 3615 return -EPROTO; 3616 3617 /* 3618 * On CRI, the hardware cannot distinguish GT translation faults 3619 * from true RAS conditions. Demote to debug to avoid false 3620 * positive hardware error logs. 3621 */ 3622 if (gt_to_xe(gt)->info.platform == XE_CRESCENTISLAND) 3623 xe_gt_dbg(gt, 3624 "GuC: Uncorrectable local error! guc_id=%d class=%s, logical_mask=0x%x", 3625 guc_id, xe_hw_engine_class_to_str(q->class), q->logical_mask); 3626 else 3627 xe_gt_err(gt, 3628 "GuC: Uncorrectable local error! guc_id=%d class=%s, logical_mask=0x%x", 3629 guc_id, xe_hw_engine_class_to_str(q->class), q->logical_mask); 3630 3631 trace_xe_guc_uncorrectable_error(q); 3632 3633 /* Treat the same as engine reset */ 3634 xe_guc_exec_queue_reset_trigger_cleanup(q); 3635 3636 return 0; 3637 } 3638 3639 int xe_guc_exec_queue_reset_failure_handler(struct xe_guc *guc, u32 *msg, u32 len) 3640 { 3641 struct xe_gt *gt = guc_to_gt(guc); 3642 u8 guc_class, instance; 3643 u32 reason; 3644 3645 if (unlikely(len != 3)) 3646 return -EPROTO; 3647 3648 guc_class = msg[0]; 3649 instance = msg[1]; 3650 reason = msg[2]; 3651 3652 /* Unexpected failure of a hardware feature, log an actual error */ 3653 xe_log_err(gt, GUCSUBMIT, -EIO, 3654 "engine reset failed on %u:%u, reason=%#x\n", 3655 guc_class, instance, reason); 3656 3657 xe_gt_reset_async(gt); 3658 3659 return 0; 3660 } 3661 3662 int xe_guc_exec_queue_cgp_context_error_handler(struct xe_guc *guc, u32 *msg, 3663 u32 len) 3664 { 3665 struct xe_gt *gt = guc_to_gt(guc); 3666 struct xe_device *xe = guc_to_xe(guc); 3667 struct xe_exec_queue *q; 3668 u32 guc_id = msg[2]; 3669 3670 if (unlikely(len != XE_GUC_EXEC_QUEUE_CGP_CONTEXT_ERROR_LEN)) { 3671 drm_err(&xe->drm, "Invalid length %u", len); 3672 return -EPROTO; 3673 } 3674 3675 q = g2h_exec_queue_lookup(guc, guc_id); 3676 if (unlikely(!q)) 3677 return -EPROTO; 3678 3679 xe_gt_dbg(gt, 3680 "CGP context error: [%s] err=0x%x, q0_id=0x%x LRCA=0x%x guc_id=0x%x", 3681 msg[0] & 1 ? "uc" : "kmd", msg[1], msg[2], msg[3], msg[4]); 3682 3683 trace_xe_exec_queue_cgp_context_error(q); 3684 3685 /* Treat the same as engine reset */ 3686 xe_guc_exec_queue_reset_trigger_cleanup(q); 3687 3688 return 0; 3689 } 3690 3691 /** 3692 * xe_guc_exec_queue_cgp_sync_done_handler - CGP synchronization done handler 3693 * @guc: guc 3694 * @msg: message indicating CGP sync done 3695 * @len: length of message 3696 * 3697 * Set multi queue group's sync_pending flag to false and wakeup anyone waiting 3698 * for CGP synchronization to complete. 3699 * 3700 * Return: 0 on success, -EPROTO for malformed messages. 3701 */ 3702 int xe_guc_exec_queue_cgp_sync_done_handler(struct xe_guc *guc, u32 *msg, u32 len) 3703 { 3704 struct xe_device *xe = guc_to_xe(guc); 3705 struct xe_exec_queue_group *group; 3706 struct xe_exec_queue *q, *upd_q; 3707 u32 guc_id = msg[0]; 3708 3709 if (unlikely(len < 1)) { 3710 drm_err(&xe->drm, "Invalid CGP_SYNC_DONE length %u", len); 3711 return -EPROTO; 3712 } 3713 3714 q = g2h_exec_queue_lookup(guc, guc_id); 3715 if (unlikely(!q)) 3716 return -EPROTO; 3717 3718 if (!xe_exec_queue_is_multi_queue_primary(q)) { 3719 drm_err(&xe->drm, "Unexpected CGP_SYNC_DONE response"); 3720 return -EPROTO; 3721 } 3722 3723 /* 3724 * The outstanding CGP update is now confirmed; clear the owning queue's 3725 * tracking so a later migration does not needlessly replay it. 3726 */ 3727 group = q->multi_queue.group; 3728 upd_q = READ_ONCE(group->cgp_update_q); 3729 if (upd_q) { 3730 upd_q->guc->multi_queue.registering_cgp = false; 3731 upd_q->guc->multi_queue.updating_cgp = false; 3732 WRITE_ONCE(group->cgp_update_q, NULL); 3733 } 3734 3735 /* Wakeup the serialized cgp update wait */ 3736 WRITE_ONCE(group->sync_pending, false); 3737 xe_guc_ct_wake_waiters(&guc->ct); 3738 3739 return 0; 3740 } 3741 3742 static void 3743 guc_exec_queue_wq_snapshot_capture(struct xe_exec_queue *q, 3744 struct xe_guc_submit_exec_queue_snapshot *snapshot) 3745 { 3746 struct xe_guc *guc = exec_queue_to_guc(q); 3747 struct xe_device *xe = guc_to_xe(guc); 3748 struct iosys_map map = xe_lrc_parallel_map(q->lrc[0]); 3749 int i; 3750 3751 snapshot->guc.wqi_head = q->guc->wqi_head; 3752 snapshot->guc.wqi_tail = q->guc->wqi_tail; 3753 snapshot->parallel.wq_desc.head = parallel_read(xe, map, wq_desc.head); 3754 snapshot->parallel.wq_desc.tail = parallel_read(xe, map, wq_desc.tail); 3755 snapshot->parallel.wq_desc.status = parallel_read(xe, map, 3756 wq_desc.wq_status); 3757 3758 if (snapshot->parallel.wq_desc.head != 3759 snapshot->parallel.wq_desc.tail) { 3760 for (i = snapshot->parallel.wq_desc.head; 3761 i != snapshot->parallel.wq_desc.tail; 3762 i = (i + sizeof(u32)) % WQ_SIZE) 3763 snapshot->parallel.wq[i / sizeof(u32)] = 3764 parallel_read(xe, map, wq[i / sizeof(u32)]); 3765 } 3766 } 3767 3768 static void 3769 guc_exec_queue_wq_snapshot_print(struct xe_guc_submit_exec_queue_snapshot *snapshot, 3770 struct drm_printer *p) 3771 { 3772 int i; 3773 3774 drm_printf(p, "\tWQ head: %u (internal), %d (memory)\n", 3775 snapshot->guc.wqi_head, snapshot->parallel.wq_desc.head); 3776 drm_printf(p, "\tWQ tail: %u (internal), %d (memory)\n", 3777 snapshot->guc.wqi_tail, snapshot->parallel.wq_desc.tail); 3778 drm_printf(p, "\tWQ status: %u\n", snapshot->parallel.wq_desc.status); 3779 3780 if (snapshot->parallel.wq_desc.head != 3781 snapshot->parallel.wq_desc.tail) { 3782 for (i = snapshot->parallel.wq_desc.head; 3783 i != snapshot->parallel.wq_desc.tail; 3784 i = (i + sizeof(u32)) % WQ_SIZE) 3785 drm_printf(p, "\tWQ[%zu]: 0x%08x\n", i / sizeof(u32), 3786 snapshot->parallel.wq[i / sizeof(u32)]); 3787 } 3788 } 3789 3790 /** 3791 * xe_guc_exec_queue_snapshot_capture - Take a quick snapshot of the GuC Engine. 3792 * @q: faulty exec queue 3793 * 3794 * This can be printed out in a later stage like during dev_coredump 3795 * analysis. 3796 * 3797 * Returns: a GuC Submit Engine snapshot object that must be freed by the 3798 * caller, using `xe_guc_exec_queue_snapshot_free`. 3799 */ 3800 struct xe_guc_submit_exec_queue_snapshot * 3801 xe_guc_exec_queue_snapshot_capture(struct xe_exec_queue *q) 3802 { 3803 struct xe_gpu_scheduler *sched = &q->guc->sched; 3804 struct xe_guc_submit_exec_queue_snapshot *snapshot; 3805 int i; 3806 3807 snapshot = kzalloc_obj(*snapshot, GFP_ATOMIC); 3808 3809 if (!snapshot) 3810 return NULL; 3811 3812 snapshot->guc.id = q->guc->id; 3813 memcpy(&snapshot->name, &q->name, sizeof(snapshot->name)); 3814 snapshot->class = q->class; 3815 snapshot->logical_mask = q->logical_mask; 3816 snapshot->width = q->width; 3817 snapshot->refcount = kref_read(&q->refcount); 3818 snapshot->jobcount = atomic_read(&q->job_cnt); 3819 snapshot->sched_timeout = sched->base.timeout; 3820 snapshot->sched_props.timeslice_us = q->sched_props.timeslice_us; 3821 snapshot->sched_props.preempt_timeout_us = 3822 q->sched_props.preempt_timeout_us; 3823 3824 snapshot->lrc = kmalloc_objs(struct xe_lrc_snapshot *, q->width, 3825 GFP_ATOMIC); 3826 3827 if (snapshot->lrc) { 3828 for (i = 0; i < q->width; ++i) { 3829 struct xe_lrc *lrc = q->lrc[i]; 3830 3831 snapshot->lrc[i] = xe_lrc_snapshot_capture(lrc); 3832 } 3833 } 3834 3835 snapshot->schedule_state = atomic_read(&q->guc->state); 3836 snapshot->exec_queue_flags = q->flags; 3837 3838 snapshot->parallel_execution = xe_exec_queue_is_parallel(q); 3839 if (snapshot->parallel_execution) 3840 guc_exec_queue_wq_snapshot_capture(q, snapshot); 3841 3842 if (xe_exec_queue_is_multi_queue(q)) { 3843 snapshot->multi_queue.valid = true; 3844 snapshot->multi_queue.primary = xe_exec_queue_multi_queue_primary(q)->guc->id; 3845 snapshot->multi_queue.pos = q->multi_queue.pos; 3846 } 3847 3848 return snapshot; 3849 } 3850 3851 /** 3852 * xe_guc_exec_queue_snapshot_capture_delayed - Take delayed part of snapshot of the GuC Engine. 3853 * @snapshot: Previously captured snapshot of job. 3854 * 3855 * This captures some data that requires taking some locks, so it cannot be done in signaling path. 3856 */ 3857 void 3858 xe_guc_exec_queue_snapshot_capture_delayed(struct xe_guc_submit_exec_queue_snapshot *snapshot) 3859 { 3860 int i; 3861 3862 if (!snapshot || !snapshot->lrc) 3863 return; 3864 3865 for (i = 0; i < snapshot->width; ++i) 3866 xe_lrc_snapshot_capture_delayed(snapshot->lrc[i]); 3867 } 3868 3869 /** 3870 * xe_guc_exec_queue_snapshot_print - Print out a given GuC Engine snapshot. 3871 * @snapshot: GuC Submit Engine snapshot object. 3872 * @p: drm_printer where it will be printed out. 3873 * 3874 * This function prints out a given GuC Submit Engine snapshot object. 3875 */ 3876 void 3877 xe_guc_exec_queue_snapshot_print(struct xe_guc_submit_exec_queue_snapshot *snapshot, 3878 struct drm_printer *p) 3879 { 3880 int i; 3881 3882 if (!snapshot) 3883 return; 3884 3885 drm_printf(p, "GuC ID: %d\n", snapshot->guc.id); 3886 drm_printf(p, "\tName: %s\n", snapshot->name); 3887 drm_printf(p, "\tClass: %d\n", snapshot->class); 3888 drm_printf(p, "\tLogical mask: 0x%x\n", snapshot->logical_mask); 3889 drm_printf(p, "\tWidth: %d\n", snapshot->width); 3890 drm_printf(p, "\tRef: %d\n", snapshot->refcount); 3891 drm_printf(p, "\tJob count: %u\n", snapshot->jobcount); 3892 drm_printf(p, "\tTimeout: %ld (ms)\n", snapshot->sched_timeout); 3893 drm_printf(p, "\tTimeslice: %u (us)\n", 3894 snapshot->sched_props.timeslice_us); 3895 drm_printf(p, "\tPreempt timeout: %u (us)\n", 3896 snapshot->sched_props.preempt_timeout_us); 3897 3898 for (i = 0; snapshot->lrc && i < snapshot->width; ++i) 3899 xe_lrc_snapshot_print(snapshot->lrc[i], p); 3900 3901 drm_printf(p, "\tSchedule State: 0x%x\n", snapshot->schedule_state); 3902 drm_printf(p, "\tFlags: 0x%lx\n", snapshot->exec_queue_flags); 3903 3904 if (snapshot->parallel_execution) 3905 guc_exec_queue_wq_snapshot_print(snapshot, p); 3906 3907 if (snapshot->multi_queue.valid) { 3908 drm_printf(p, "\tMulti queue primary GuC ID: %d\n", snapshot->multi_queue.primary); 3909 drm_printf(p, "\tMulti queue position: %d\n", snapshot->multi_queue.pos); 3910 } 3911 } 3912 3913 /** 3914 * xe_guc_exec_queue_snapshot_free - Free all allocated objects for a given 3915 * snapshot. 3916 * @snapshot: GuC Submit Engine snapshot object. 3917 * 3918 * This function free all the memory that needed to be allocated at capture 3919 * time. 3920 */ 3921 void xe_guc_exec_queue_snapshot_free(struct xe_guc_submit_exec_queue_snapshot *snapshot) 3922 { 3923 int i; 3924 3925 if (!snapshot) 3926 return; 3927 3928 if (snapshot->lrc) { 3929 for (i = 0; i < snapshot->width; i++) 3930 xe_lrc_snapshot_free(snapshot->lrc[i]); 3931 kfree(snapshot->lrc); 3932 } 3933 kfree(snapshot); 3934 } 3935 3936 static void guc_exec_queue_print(struct xe_exec_queue *q, struct drm_printer *p) 3937 { 3938 struct xe_guc_submit_exec_queue_snapshot *snapshot; 3939 3940 snapshot = xe_guc_exec_queue_snapshot_capture(q); 3941 xe_guc_exec_queue_snapshot_print(snapshot, p); 3942 xe_guc_exec_queue_snapshot_free(snapshot); 3943 } 3944 3945 /** 3946 * xe_guc_register_vf_exec_queue - Register exec queue for a given context type. 3947 * @q: Execution queue 3948 * @ctx_type: Type of the context 3949 * 3950 * This function registers the execution queue with the guc. Special context 3951 * types like GUC_CONTEXT_COMPRESSION_SAVE and GUC_CONTEXT_COMPRESSION_RESTORE 3952 * are only applicable for IGPU and in the VF. 3953 * Submits the execution queue to GUC after registering it. 3954 * 3955 * Returns - None. 3956 */ 3957 void xe_guc_register_vf_exec_queue(struct xe_exec_queue *q, int ctx_type) 3958 { 3959 struct xe_guc *guc = exec_queue_to_guc(q); 3960 struct xe_device *xe = guc_to_xe(guc); 3961 struct xe_gt *gt = guc_to_gt(guc); 3962 3963 xe_gt_assert(gt, IS_SRIOV_VF(xe)); 3964 xe_gt_assert(gt, !IS_DGFX(xe)); 3965 xe_gt_assert(gt, ctx_type == GUC_CONTEXT_COMPRESSION_SAVE || 3966 ctx_type == GUC_CONTEXT_COMPRESSION_RESTORE); 3967 xe_gt_assert(gt, GUC_SUBMIT_VER(guc) >= MAKE_GUC_VER(1, 23, 0)); 3968 3969 register_exec_queue(q, ctx_type); 3970 enable_scheduling(q); 3971 } 3972 3973 /** 3974 * xe_guc_submit_print - GuC Submit Print. 3975 * @guc: GuC. 3976 * @p: drm_printer where it will be printed out. 3977 * 3978 * This function capture and prints snapshots of **all** GuC Engines. 3979 */ 3980 void xe_guc_submit_print(struct xe_guc *guc, struct drm_printer *p) 3981 { 3982 struct xe_exec_queue *q; 3983 unsigned long index; 3984 3985 if (!xe_device_uc_enabled(guc_to_xe(guc))) 3986 return; 3987 3988 mutex_lock(&guc->submission_state.lock); 3989 xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) 3990 guc_exec_queue_print(q, p); 3991 mutex_unlock(&guc->submission_state.lock); 3992 } 3993 3994 /** 3995 * xe_guc_has_registered_mlrc_queues - check whether there are any MLRC queues 3996 * registered with the GuC 3997 * @guc: GuC. 3998 * 3999 * Return: true if any MLRC queue is registered with the GuC, false otherwise. 4000 */ 4001 bool xe_guc_has_registered_mlrc_queues(struct xe_guc *guc) 4002 { 4003 struct xe_exec_queue *q; 4004 unsigned long index; 4005 4006 guard(mutex)(&guc->submission_state.lock); 4007 4008 xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) 4009 if (q->width > 1) 4010 return true; 4011 4012 return false; 4013 } 4014 4015 /** 4016 * xe_guc_submit_active_multi_queue_lrca() - Resolve the LRCA of the active 4017 * queue in the multi-queue group currently running on an engine. 4018 * @guc: the &xe_guc managing the exec queues 4019 * @hwe: the &xe_hw_engine whose active queue is being resolved 4020 * @cur_lrca: value read from RING_CURRENT_LRCA, identifies the running group 4021 * @active_id: current Active Queue ID read from CSMQDEBUG (position in group) 4022 * 4023 * The running group is identified by matching @cur_lrca against the group's 4024 * primary LRCA; @active_id then selects the active queue within that group. 4025 * 4026 * Return: the LRCA of the active queue, or 0 if no matching queue is found. 4027 */ 4028 u32 xe_guc_submit_active_multi_queue_lrca(struct xe_guc *guc, 4029 struct xe_hw_engine *hwe, 4030 u32 cur_lrca, u32 active_id) 4031 { 4032 struct xe_exec_queue *q; 4033 unsigned long index; 4034 u32 lrca = 0; 4035 4036 /* 4037 * submission_state.lock also protects exec_queue teardown: an exec 4038 * queue is removed from exec_queue_lookup before its group/primary 4039 * are freed, so any q found in the xarray below has a live group 4040 * and primary for as long as we hold the lock. 4041 */ 4042 guard(mutex)(&guc->submission_state.lock); 4043 4044 xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) { 4045 struct xe_exec_queue_group *group = q->multi_queue.group; 4046 struct xe_lrc *active_lrc; 4047 struct xe_lrc *primary_lrc; 4048 4049 if (!q->multi_queue.valid || !group || !group->primary) 4050 continue; 4051 /* 4052 * Multi-queue exec queues are bound to a hw engine class; 4053 * GuC dynamically schedules them onto one of the class's 4054 * physical instances, so there is no fixed queue-to-instance 4055 * mapping to filter on here. 4056 */ 4057 if (q->class != hwe->class) 4058 continue; 4059 if (q->multi_queue.pos != active_id) 4060 continue; 4061 /* 4062 * LRCAs are page-aligned (4K) addresses in GGTT; the low 4063 * bits reported by RING_CURRENT_LRCA are not meaningful, so 4064 * only compare bits [31:12]. 4065 */ 4066 primary_lrc = xe_exec_queue_get_lrc(group->primary, 0); 4067 if (!primary_lrc) 4068 continue; 4069 4070 if ((xe_lrc_ggtt_addr(primary_lrc) ^ cur_lrca) & GENMASK(31, 12)) { 4071 xe_lrc_put(primary_lrc); 4072 continue; 4073 } 4074 4075 active_lrc = xe_exec_queue_get_lrc(q, 0); 4076 xe_lrc_put(primary_lrc); 4077 if (!active_lrc) 4078 continue; 4079 4080 lrca = xe_lrc_ggtt_addr(active_lrc); 4081 xe_lrc_put(active_lrc); 4082 break; 4083 } 4084 4085 return lrca; 4086 } 4087 4088 /** 4089 * xe_guc_contexts_hwsp_rebase - Re-compute GGTT references within all 4090 * exec queues registered to given GuC. 4091 * @guc: the &xe_guc struct instance 4092 * @scratch: scratch buffer to be used as temporary storage 4093 * 4094 * Returns: zero on success, negative error code on failure. 4095 */ 4096 int xe_guc_contexts_hwsp_rebase(struct xe_guc *guc, void *scratch) 4097 { 4098 struct xe_exec_queue *q; 4099 unsigned long index; 4100 int err = 0; 4101 4102 mutex_lock(&guc->submission_state.lock); 4103 xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) { 4104 /* Prevent redundant attempts to stop parallel queues */ 4105 if (q->guc->id != index) 4106 continue; 4107 4108 err = xe_exec_queue_contexts_hwsp_rebase(q, scratch); 4109 if (err) 4110 break; 4111 } 4112 mutex_unlock(&guc->submission_state.lock); 4113 4114 return err; 4115 } 4116