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