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