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/delay.h> 12 #include <linux/dma-fence-array.h> 13 #include <linux/math64.h> 14 15 #include <drm/drm_managed.h> 16 17 #include "abi/guc_actions_abi.h" 18 #include "abi/guc_klvs_abi.h" 19 #include "regs/xe_lrc_layout.h" 20 #include "xe_assert.h" 21 #include "xe_devcoredump.h" 22 #include "xe_device.h" 23 #include "xe_exec_queue.h" 24 #include "xe_force_wake.h" 25 #include "xe_gpu_scheduler.h" 26 #include "xe_gt.h" 27 #include "xe_gt_clock.h" 28 #include "xe_gt_printk.h" 29 #include "xe_guc.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_submit_types.h" 34 #include "xe_hw_engine.h" 35 #include "xe_hw_fence.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_trace.h" 44 #include "xe_vm.h" 45 46 static struct xe_guc * 47 exec_queue_to_guc(struct xe_exec_queue *q) 48 { 49 return &q->gt->uc.guc; 50 } 51 52 /* 53 * Helpers for engine state, using an atomic as some of the bits can transition 54 * as the same time (e.g. a suspend can be happning at the same time as schedule 55 * engine done being processed). 56 */ 57 #define EXEC_QUEUE_STATE_REGISTERED (1 << 0) 58 #define EXEC_QUEUE_STATE_ENABLED (1 << 1) 59 #define EXEC_QUEUE_STATE_PENDING_ENABLE (1 << 2) 60 #define EXEC_QUEUE_STATE_PENDING_DISABLE (1 << 3) 61 #define EXEC_QUEUE_STATE_DESTROYED (1 << 4) 62 #define EXEC_QUEUE_STATE_SUSPENDED (1 << 5) 63 #define EXEC_QUEUE_STATE_RESET (1 << 6) 64 #define EXEC_QUEUE_STATE_KILLED (1 << 7) 65 #define EXEC_QUEUE_STATE_WEDGED (1 << 8) 66 #define EXEC_QUEUE_STATE_BANNED (1 << 9) 67 #define EXEC_QUEUE_STATE_CHECK_TIMEOUT (1 << 10) 68 #define EXEC_QUEUE_STATE_EXTRA_REF (1 << 11) 69 70 static bool exec_queue_registered(struct xe_exec_queue *q) 71 { 72 return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_REGISTERED; 73 } 74 75 static void set_exec_queue_registered(struct xe_exec_queue *q) 76 { 77 atomic_or(EXEC_QUEUE_STATE_REGISTERED, &q->guc->state); 78 } 79 80 static void clear_exec_queue_registered(struct xe_exec_queue *q) 81 { 82 atomic_and(~EXEC_QUEUE_STATE_REGISTERED, &q->guc->state); 83 } 84 85 static bool exec_queue_enabled(struct xe_exec_queue *q) 86 { 87 return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_ENABLED; 88 } 89 90 static void set_exec_queue_enabled(struct xe_exec_queue *q) 91 { 92 atomic_or(EXEC_QUEUE_STATE_ENABLED, &q->guc->state); 93 } 94 95 static void clear_exec_queue_enabled(struct xe_exec_queue *q) 96 { 97 atomic_and(~EXEC_QUEUE_STATE_ENABLED, &q->guc->state); 98 } 99 100 static bool exec_queue_pending_enable(struct xe_exec_queue *q) 101 { 102 return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_PENDING_ENABLE; 103 } 104 105 static void set_exec_queue_pending_enable(struct xe_exec_queue *q) 106 { 107 atomic_or(EXEC_QUEUE_STATE_PENDING_ENABLE, &q->guc->state); 108 } 109 110 static void clear_exec_queue_pending_enable(struct xe_exec_queue *q) 111 { 112 atomic_and(~EXEC_QUEUE_STATE_PENDING_ENABLE, &q->guc->state); 113 } 114 115 static bool exec_queue_pending_disable(struct xe_exec_queue *q) 116 { 117 return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_PENDING_DISABLE; 118 } 119 120 static void set_exec_queue_pending_disable(struct xe_exec_queue *q) 121 { 122 atomic_or(EXEC_QUEUE_STATE_PENDING_DISABLE, &q->guc->state); 123 } 124 125 static void clear_exec_queue_pending_disable(struct xe_exec_queue *q) 126 { 127 atomic_and(~EXEC_QUEUE_STATE_PENDING_DISABLE, &q->guc->state); 128 } 129 130 static bool exec_queue_destroyed(struct xe_exec_queue *q) 131 { 132 return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_DESTROYED; 133 } 134 135 static void set_exec_queue_destroyed(struct xe_exec_queue *q) 136 { 137 atomic_or(EXEC_QUEUE_STATE_DESTROYED, &q->guc->state); 138 } 139 140 static bool exec_queue_banned(struct xe_exec_queue *q) 141 { 142 return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_BANNED; 143 } 144 145 static void set_exec_queue_banned(struct xe_exec_queue *q) 146 { 147 atomic_or(EXEC_QUEUE_STATE_BANNED, &q->guc->state); 148 } 149 150 static bool exec_queue_suspended(struct xe_exec_queue *q) 151 { 152 return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_SUSPENDED; 153 } 154 155 static void set_exec_queue_suspended(struct xe_exec_queue *q) 156 { 157 atomic_or(EXEC_QUEUE_STATE_SUSPENDED, &q->guc->state); 158 } 159 160 static void clear_exec_queue_suspended(struct xe_exec_queue *q) 161 { 162 atomic_and(~EXEC_QUEUE_STATE_SUSPENDED, &q->guc->state); 163 } 164 165 static bool exec_queue_reset(struct xe_exec_queue *q) 166 { 167 return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_RESET; 168 } 169 170 static void set_exec_queue_reset(struct xe_exec_queue *q) 171 { 172 atomic_or(EXEC_QUEUE_STATE_RESET, &q->guc->state); 173 } 174 175 static bool exec_queue_killed(struct xe_exec_queue *q) 176 { 177 return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_KILLED; 178 } 179 180 static void set_exec_queue_killed(struct xe_exec_queue *q) 181 { 182 atomic_or(EXEC_QUEUE_STATE_KILLED, &q->guc->state); 183 } 184 185 static bool exec_queue_wedged(struct xe_exec_queue *q) 186 { 187 return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_WEDGED; 188 } 189 190 static void set_exec_queue_wedged(struct xe_exec_queue *q) 191 { 192 atomic_or(EXEC_QUEUE_STATE_WEDGED, &q->guc->state); 193 } 194 195 static bool exec_queue_check_timeout(struct xe_exec_queue *q) 196 { 197 return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_CHECK_TIMEOUT; 198 } 199 200 static void set_exec_queue_check_timeout(struct xe_exec_queue *q) 201 { 202 atomic_or(EXEC_QUEUE_STATE_CHECK_TIMEOUT, &q->guc->state); 203 } 204 205 static void clear_exec_queue_check_timeout(struct xe_exec_queue *q) 206 { 207 atomic_and(~EXEC_QUEUE_STATE_CHECK_TIMEOUT, &q->guc->state); 208 } 209 210 static bool exec_queue_extra_ref(struct xe_exec_queue *q) 211 { 212 return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_EXTRA_REF; 213 } 214 215 static void set_exec_queue_extra_ref(struct xe_exec_queue *q) 216 { 217 atomic_or(EXEC_QUEUE_STATE_EXTRA_REF, &q->guc->state); 218 } 219 220 static bool exec_queue_killed_or_banned_or_wedged(struct xe_exec_queue *q) 221 { 222 return (atomic_read(&q->guc->state) & 223 (EXEC_QUEUE_STATE_WEDGED | EXEC_QUEUE_STATE_KILLED | 224 EXEC_QUEUE_STATE_BANNED)); 225 } 226 227 #ifdef CONFIG_PROVE_LOCKING 228 static int alloc_submit_wq(struct xe_guc *guc) 229 { 230 int i; 231 232 for (i = 0; i < NUM_SUBMIT_WQ; ++i) { 233 guc->submission_state.submit_wq_pool[i] = 234 alloc_ordered_workqueue("submit_wq", 0); 235 if (!guc->submission_state.submit_wq_pool[i]) 236 goto err_free; 237 } 238 239 return 0; 240 241 err_free: 242 while (i) 243 destroy_workqueue(guc->submission_state.submit_wq_pool[--i]); 244 245 return -ENOMEM; 246 } 247 248 static void free_submit_wq(struct xe_guc *guc) 249 { 250 int i; 251 252 for (i = 0; i < NUM_SUBMIT_WQ; ++i) 253 destroy_workqueue(guc->submission_state.submit_wq_pool[i]); 254 } 255 256 static struct workqueue_struct *get_submit_wq(struct xe_guc *guc) 257 { 258 int idx = guc->submission_state.submit_wq_idx++ % NUM_SUBMIT_WQ; 259 260 return guc->submission_state.submit_wq_pool[idx]; 261 } 262 #else 263 static int alloc_submit_wq(struct xe_guc *guc) 264 { 265 return 0; 266 } 267 268 static void free_submit_wq(struct xe_guc *guc) 269 { 270 271 } 272 273 static struct workqueue_struct *get_submit_wq(struct xe_guc *guc) 274 { 275 return NULL; 276 } 277 #endif 278 279 static void guc_submit_fini(struct drm_device *drm, void *arg) 280 { 281 struct xe_guc *guc = arg; 282 283 xa_destroy(&guc->submission_state.exec_queue_lookup); 284 free_submit_wq(guc); 285 } 286 287 static void guc_submit_wedged_fini(void *arg) 288 { 289 struct xe_guc *guc = arg; 290 struct xe_exec_queue *q; 291 unsigned long index; 292 293 xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) 294 if (exec_queue_wedged(q)) 295 xe_exec_queue_put(q); 296 } 297 298 static const struct xe_exec_queue_ops guc_exec_queue_ops; 299 300 static void primelockdep(struct xe_guc *guc) 301 { 302 if (!IS_ENABLED(CONFIG_LOCKDEP)) 303 return; 304 305 fs_reclaim_acquire(GFP_KERNEL); 306 307 mutex_lock(&guc->submission_state.lock); 308 mutex_unlock(&guc->submission_state.lock); 309 310 fs_reclaim_release(GFP_KERNEL); 311 } 312 313 /** 314 * xe_guc_submit_init() - Initialize GuC submission. 315 * @guc: the &xe_guc to initialize 316 * @num_ids: number of GuC context IDs to use 317 * 318 * The bare-metal or PF driver can pass ~0 as &num_ids to indicate that all 319 * GuC context IDs supported by the GuC firmware should be used for submission. 320 * 321 * Only VF drivers will have to provide explicit number of GuC context IDs 322 * that they can use for submission. 323 * 324 * Return: 0 on success or a negative error code on failure. 325 */ 326 int xe_guc_submit_init(struct xe_guc *guc, unsigned int num_ids) 327 { 328 struct xe_device *xe = guc_to_xe(guc); 329 struct xe_gt *gt = guc_to_gt(guc); 330 int err; 331 332 err = drmm_mutex_init(&xe->drm, &guc->submission_state.lock); 333 if (err) 334 return err; 335 336 err = xe_guc_id_mgr_init(&guc->submission_state.idm, num_ids); 337 if (err) 338 return err; 339 340 err = alloc_submit_wq(guc); 341 if (err) 342 return err; 343 344 gt->exec_queue_ops = &guc_exec_queue_ops; 345 346 xa_init(&guc->submission_state.exec_queue_lookup); 347 348 primelockdep(guc); 349 350 return drmm_add_action_or_reset(&xe->drm, guc_submit_fini, guc); 351 } 352 353 static void __release_guc_id(struct xe_guc *guc, struct xe_exec_queue *q, u32 xa_count) 354 { 355 int i; 356 357 lockdep_assert_held(&guc->submission_state.lock); 358 359 for (i = 0; i < xa_count; ++i) 360 xa_erase(&guc->submission_state.exec_queue_lookup, q->guc->id + i); 361 362 xe_guc_id_mgr_release_locked(&guc->submission_state.idm, 363 q->guc->id, q->width); 364 } 365 366 static int alloc_guc_id(struct xe_guc *guc, struct xe_exec_queue *q) 367 { 368 int ret; 369 void *ptr; 370 int i; 371 372 /* 373 * Must use GFP_NOWAIT as this lock is in the dma fence signalling path, 374 * worse case user gets -ENOMEM on engine create and has to try again. 375 * 376 * FIXME: Have caller pre-alloc or post-alloc /w GFP_KERNEL to prevent 377 * failure. 378 */ 379 lockdep_assert_held(&guc->submission_state.lock); 380 381 ret = xe_guc_id_mgr_reserve_locked(&guc->submission_state.idm, 382 q->width); 383 if (ret < 0) 384 return ret; 385 386 q->guc->id = ret; 387 388 for (i = 0; i < q->width; ++i) { 389 ptr = xa_store(&guc->submission_state.exec_queue_lookup, 390 q->guc->id + i, q, GFP_NOWAIT); 391 if (IS_ERR(ptr)) { 392 ret = PTR_ERR(ptr); 393 goto err_release; 394 } 395 } 396 397 return 0; 398 399 err_release: 400 __release_guc_id(guc, q, i); 401 402 return ret; 403 } 404 405 static void release_guc_id(struct xe_guc *guc, struct xe_exec_queue *q) 406 { 407 mutex_lock(&guc->submission_state.lock); 408 __release_guc_id(guc, q, q->width); 409 mutex_unlock(&guc->submission_state.lock); 410 } 411 412 struct exec_queue_policy { 413 u32 count; 414 struct guc_update_exec_queue_policy h2g; 415 }; 416 417 static u32 __guc_exec_queue_policy_action_size(struct exec_queue_policy *policy) 418 { 419 size_t bytes = sizeof(policy->h2g.header) + 420 (sizeof(policy->h2g.klv[0]) * policy->count); 421 422 return bytes / sizeof(u32); 423 } 424 425 static void __guc_exec_queue_policy_start_klv(struct exec_queue_policy *policy, 426 u16 guc_id) 427 { 428 policy->h2g.header.action = 429 XE_GUC_ACTION_HOST2GUC_UPDATE_CONTEXT_POLICIES; 430 policy->h2g.header.guc_id = guc_id; 431 policy->count = 0; 432 } 433 434 #define MAKE_EXEC_QUEUE_POLICY_ADD(func, id) \ 435 static void __guc_exec_queue_policy_add_##func(struct exec_queue_policy *policy, \ 436 u32 data) \ 437 { \ 438 XE_WARN_ON(policy->count >= GUC_CONTEXT_POLICIES_KLV_NUM_IDS); \ 439 \ 440 policy->h2g.klv[policy->count].kl = \ 441 FIELD_PREP(GUC_KLV_0_KEY, \ 442 GUC_CONTEXT_POLICIES_KLV_ID_##id) | \ 443 FIELD_PREP(GUC_KLV_0_LEN, 1); \ 444 policy->h2g.klv[policy->count].value = data; \ 445 policy->count++; \ 446 } 447 448 MAKE_EXEC_QUEUE_POLICY_ADD(execution_quantum, EXECUTION_QUANTUM) 449 MAKE_EXEC_QUEUE_POLICY_ADD(preemption_timeout, PREEMPTION_TIMEOUT) 450 MAKE_EXEC_QUEUE_POLICY_ADD(priority, SCHEDULING_PRIORITY) 451 #undef MAKE_EXEC_QUEUE_POLICY_ADD 452 453 static const int xe_exec_queue_prio_to_guc[] = { 454 [XE_EXEC_QUEUE_PRIORITY_LOW] = GUC_CLIENT_PRIORITY_NORMAL, 455 [XE_EXEC_QUEUE_PRIORITY_NORMAL] = GUC_CLIENT_PRIORITY_KMD_NORMAL, 456 [XE_EXEC_QUEUE_PRIORITY_HIGH] = GUC_CLIENT_PRIORITY_HIGH, 457 [XE_EXEC_QUEUE_PRIORITY_KERNEL] = GUC_CLIENT_PRIORITY_KMD_HIGH, 458 }; 459 460 static void init_policies(struct xe_guc *guc, struct xe_exec_queue *q) 461 { 462 struct exec_queue_policy policy; 463 struct xe_device *xe = guc_to_xe(guc); 464 enum xe_exec_queue_priority prio = q->sched_props.priority; 465 u32 timeslice_us = q->sched_props.timeslice_us; 466 u32 preempt_timeout_us = q->sched_props.preempt_timeout_us; 467 468 xe_assert(xe, exec_queue_registered(q)); 469 470 __guc_exec_queue_policy_start_klv(&policy, q->guc->id); 471 __guc_exec_queue_policy_add_priority(&policy, xe_exec_queue_prio_to_guc[prio]); 472 __guc_exec_queue_policy_add_execution_quantum(&policy, timeslice_us); 473 __guc_exec_queue_policy_add_preemption_timeout(&policy, preempt_timeout_us); 474 475 xe_guc_ct_send(&guc->ct, (u32 *)&policy.h2g, 476 __guc_exec_queue_policy_action_size(&policy), 0, 0); 477 } 478 479 static void set_min_preemption_timeout(struct xe_guc *guc, struct xe_exec_queue *q) 480 { 481 struct exec_queue_policy policy; 482 483 __guc_exec_queue_policy_start_klv(&policy, q->guc->id); 484 __guc_exec_queue_policy_add_preemption_timeout(&policy, 1); 485 486 xe_guc_ct_send(&guc->ct, (u32 *)&policy.h2g, 487 __guc_exec_queue_policy_action_size(&policy), 0, 0); 488 } 489 490 #define parallel_read(xe_, map_, field_) \ 491 xe_map_rd_field(xe_, &map_, 0, struct guc_submit_parallel_scratch, \ 492 field_) 493 #define parallel_write(xe_, map_, field_, val_) \ 494 xe_map_wr_field(xe_, &map_, 0, struct guc_submit_parallel_scratch, \ 495 field_, val_) 496 497 static void __register_mlrc_exec_queue(struct xe_guc *guc, 498 struct xe_exec_queue *q, 499 struct guc_ctxt_registration_info *info) 500 { 501 #define MAX_MLRC_REG_SIZE (13 + XE_HW_ENGINE_MAX_INSTANCE * 2) 502 struct xe_device *xe = guc_to_xe(guc); 503 u32 action[MAX_MLRC_REG_SIZE]; 504 int len = 0; 505 int i; 506 507 xe_assert(xe, xe_exec_queue_is_parallel(q)); 508 509 action[len++] = XE_GUC_ACTION_REGISTER_CONTEXT_MULTI_LRC; 510 action[len++] = info->flags; 511 action[len++] = info->context_idx; 512 action[len++] = info->engine_class; 513 action[len++] = info->engine_submit_mask; 514 action[len++] = info->wq_desc_lo; 515 action[len++] = info->wq_desc_hi; 516 action[len++] = info->wq_base_lo; 517 action[len++] = info->wq_base_hi; 518 action[len++] = info->wq_size; 519 action[len++] = q->width; 520 action[len++] = info->hwlrca_lo; 521 action[len++] = info->hwlrca_hi; 522 523 for (i = 1; i < q->width; ++i) { 524 struct xe_lrc *lrc = q->lrc[i]; 525 526 action[len++] = lower_32_bits(xe_lrc_descriptor(lrc)); 527 action[len++] = upper_32_bits(xe_lrc_descriptor(lrc)); 528 } 529 530 xe_assert(xe, len <= MAX_MLRC_REG_SIZE); 531 #undef MAX_MLRC_REG_SIZE 532 533 xe_guc_ct_send(&guc->ct, action, len, 0, 0); 534 } 535 536 static void __register_exec_queue(struct xe_guc *guc, 537 struct guc_ctxt_registration_info *info) 538 { 539 u32 action[] = { 540 XE_GUC_ACTION_REGISTER_CONTEXT, 541 info->flags, 542 info->context_idx, 543 info->engine_class, 544 info->engine_submit_mask, 545 info->wq_desc_lo, 546 info->wq_desc_hi, 547 info->wq_base_lo, 548 info->wq_base_hi, 549 info->wq_size, 550 info->hwlrca_lo, 551 info->hwlrca_hi, 552 }; 553 554 xe_guc_ct_send(&guc->ct, action, ARRAY_SIZE(action), 0, 0); 555 } 556 557 static void register_exec_queue(struct xe_exec_queue *q) 558 { 559 struct xe_guc *guc = exec_queue_to_guc(q); 560 struct xe_device *xe = guc_to_xe(guc); 561 struct xe_lrc *lrc = q->lrc[0]; 562 struct guc_ctxt_registration_info info; 563 564 xe_assert(xe, !exec_queue_registered(q)); 565 566 memset(&info, 0, sizeof(info)); 567 info.context_idx = q->guc->id; 568 info.engine_class = xe_engine_class_to_guc_class(q->class); 569 info.engine_submit_mask = q->logical_mask; 570 info.hwlrca_lo = lower_32_bits(xe_lrc_descriptor(lrc)); 571 info.hwlrca_hi = upper_32_bits(xe_lrc_descriptor(lrc)); 572 info.flags = CONTEXT_REGISTRATION_FLAG_KMD; 573 574 if (xe_exec_queue_is_parallel(q)) { 575 u64 ggtt_addr = xe_lrc_parallel_ggtt_addr(lrc); 576 struct iosys_map map = xe_lrc_parallel_map(lrc); 577 578 info.wq_desc_lo = lower_32_bits(ggtt_addr + 579 offsetof(struct guc_submit_parallel_scratch, wq_desc)); 580 info.wq_desc_hi = upper_32_bits(ggtt_addr + 581 offsetof(struct guc_submit_parallel_scratch, wq_desc)); 582 info.wq_base_lo = lower_32_bits(ggtt_addr + 583 offsetof(struct guc_submit_parallel_scratch, wq[0])); 584 info.wq_base_hi = upper_32_bits(ggtt_addr + 585 offsetof(struct guc_submit_parallel_scratch, wq[0])); 586 info.wq_size = WQ_SIZE; 587 588 q->guc->wqi_head = 0; 589 q->guc->wqi_tail = 0; 590 xe_map_memset(xe, &map, 0, 0, PARALLEL_SCRATCH_SIZE - WQ_SIZE); 591 parallel_write(xe, map, wq_desc.wq_status, WQ_STATUS_ACTIVE); 592 } 593 594 /* 595 * We must keep a reference for LR engines if engine is registered with 596 * the GuC as jobs signal immediately and can't destroy an engine if the 597 * GuC has a reference to it. 598 */ 599 if (xe_exec_queue_is_lr(q)) 600 xe_exec_queue_get(q); 601 602 set_exec_queue_registered(q); 603 trace_xe_exec_queue_register(q); 604 if (xe_exec_queue_is_parallel(q)) 605 __register_mlrc_exec_queue(guc, q, &info); 606 else 607 __register_exec_queue(guc, &info); 608 init_policies(guc, q); 609 } 610 611 static u32 wq_space_until_wrap(struct xe_exec_queue *q) 612 { 613 return (WQ_SIZE - q->guc->wqi_tail); 614 } 615 616 static int wq_wait_for_space(struct xe_exec_queue *q, u32 wqi_size) 617 { 618 struct xe_guc *guc = exec_queue_to_guc(q); 619 struct xe_device *xe = guc_to_xe(guc); 620 struct iosys_map map = xe_lrc_parallel_map(q->lrc[0]); 621 unsigned int sleep_period_ms = 1; 622 623 #define AVAILABLE_SPACE \ 624 CIRC_SPACE(q->guc->wqi_tail, q->guc->wqi_head, WQ_SIZE) 625 if (wqi_size > AVAILABLE_SPACE) { 626 try_again: 627 q->guc->wqi_head = parallel_read(xe, map, wq_desc.head); 628 if (wqi_size > AVAILABLE_SPACE) { 629 if (sleep_period_ms == 1024) { 630 xe_gt_reset_async(q->gt); 631 return -ENODEV; 632 } 633 634 msleep(sleep_period_ms); 635 sleep_period_ms <<= 1; 636 goto try_again; 637 } 638 } 639 #undef AVAILABLE_SPACE 640 641 return 0; 642 } 643 644 static int wq_noop_append(struct xe_exec_queue *q) 645 { 646 struct xe_guc *guc = exec_queue_to_guc(q); 647 struct xe_device *xe = guc_to_xe(guc); 648 struct iosys_map map = xe_lrc_parallel_map(q->lrc[0]); 649 u32 len_dw = wq_space_until_wrap(q) / sizeof(u32) - 1; 650 651 if (wq_wait_for_space(q, wq_space_until_wrap(q))) 652 return -ENODEV; 653 654 xe_assert(xe, FIELD_FIT(WQ_LEN_MASK, len_dw)); 655 656 parallel_write(xe, map, wq[q->guc->wqi_tail / sizeof(u32)], 657 FIELD_PREP(WQ_TYPE_MASK, WQ_TYPE_NOOP) | 658 FIELD_PREP(WQ_LEN_MASK, len_dw)); 659 q->guc->wqi_tail = 0; 660 661 return 0; 662 } 663 664 static void wq_item_append(struct xe_exec_queue *q) 665 { 666 struct xe_guc *guc = exec_queue_to_guc(q); 667 struct xe_device *xe = guc_to_xe(guc); 668 struct iosys_map map = xe_lrc_parallel_map(q->lrc[0]); 669 #define WQ_HEADER_SIZE 4 /* Includes 1 LRC address too */ 670 u32 wqi[XE_HW_ENGINE_MAX_INSTANCE + (WQ_HEADER_SIZE - 1)]; 671 u32 wqi_size = (q->width + (WQ_HEADER_SIZE - 1)) * sizeof(u32); 672 u32 len_dw = (wqi_size / sizeof(u32)) - 1; 673 int i = 0, j; 674 675 if (wqi_size > wq_space_until_wrap(q)) { 676 if (wq_noop_append(q)) 677 return; 678 } 679 if (wq_wait_for_space(q, wqi_size)) 680 return; 681 682 wqi[i++] = FIELD_PREP(WQ_TYPE_MASK, WQ_TYPE_MULTI_LRC) | 683 FIELD_PREP(WQ_LEN_MASK, len_dw); 684 wqi[i++] = xe_lrc_descriptor(q->lrc[0]); 685 wqi[i++] = FIELD_PREP(WQ_GUC_ID_MASK, q->guc->id) | 686 FIELD_PREP(WQ_RING_TAIL_MASK, q->lrc[0]->ring.tail / sizeof(u64)); 687 wqi[i++] = 0; 688 for (j = 1; j < q->width; ++j) { 689 struct xe_lrc *lrc = q->lrc[j]; 690 691 wqi[i++] = lrc->ring.tail / sizeof(u64); 692 } 693 694 xe_assert(xe, i == wqi_size / sizeof(u32)); 695 696 iosys_map_incr(&map, offsetof(struct guc_submit_parallel_scratch, 697 wq[q->guc->wqi_tail / sizeof(u32)])); 698 xe_map_memcpy_to(xe, &map, 0, wqi, wqi_size); 699 q->guc->wqi_tail += wqi_size; 700 xe_assert(xe, q->guc->wqi_tail <= WQ_SIZE); 701 702 xe_device_wmb(xe); 703 704 map = xe_lrc_parallel_map(q->lrc[0]); 705 parallel_write(xe, map, wq_desc.tail, q->guc->wqi_tail); 706 } 707 708 #define RESUME_PENDING ~0x0ull 709 static void submit_exec_queue(struct xe_exec_queue *q) 710 { 711 struct xe_guc *guc = exec_queue_to_guc(q); 712 struct xe_device *xe = guc_to_xe(guc); 713 struct xe_lrc *lrc = q->lrc[0]; 714 u32 action[3]; 715 u32 g2h_len = 0; 716 u32 num_g2h = 0; 717 int len = 0; 718 bool extra_submit = false; 719 720 xe_assert(xe, exec_queue_registered(q)); 721 722 if (xe_exec_queue_is_parallel(q)) 723 wq_item_append(q); 724 else 725 xe_lrc_set_ring_tail(lrc, lrc->ring.tail); 726 727 if (exec_queue_suspended(q) && !xe_exec_queue_is_parallel(q)) 728 return; 729 730 if (!exec_queue_enabled(q) && !exec_queue_suspended(q)) { 731 action[len++] = XE_GUC_ACTION_SCHED_CONTEXT_MODE_SET; 732 action[len++] = q->guc->id; 733 action[len++] = GUC_CONTEXT_ENABLE; 734 g2h_len = G2H_LEN_DW_SCHED_CONTEXT_MODE_SET; 735 num_g2h = 1; 736 if (xe_exec_queue_is_parallel(q)) 737 extra_submit = true; 738 739 q->guc->resume_time = RESUME_PENDING; 740 set_exec_queue_pending_enable(q); 741 set_exec_queue_enabled(q); 742 trace_xe_exec_queue_scheduling_enable(q); 743 } else { 744 action[len++] = XE_GUC_ACTION_SCHED_CONTEXT; 745 action[len++] = q->guc->id; 746 trace_xe_exec_queue_submit(q); 747 } 748 749 xe_guc_ct_send(&guc->ct, action, len, g2h_len, num_g2h); 750 751 if (extra_submit) { 752 len = 0; 753 action[len++] = XE_GUC_ACTION_SCHED_CONTEXT; 754 action[len++] = q->guc->id; 755 trace_xe_exec_queue_submit(q); 756 757 xe_guc_ct_send(&guc->ct, action, len, 0, 0); 758 } 759 } 760 761 static struct dma_fence * 762 guc_exec_queue_run_job(struct drm_sched_job *drm_job) 763 { 764 struct xe_sched_job *job = to_xe_sched_job(drm_job); 765 struct xe_exec_queue *q = job->q; 766 struct xe_guc *guc = exec_queue_to_guc(q); 767 struct xe_device *xe = guc_to_xe(guc); 768 bool lr = xe_exec_queue_is_lr(q); 769 770 xe_assert(xe, !(exec_queue_destroyed(q) || exec_queue_pending_disable(q)) || 771 exec_queue_banned(q) || exec_queue_suspended(q)); 772 773 trace_xe_sched_job_run(job); 774 775 if (!exec_queue_killed_or_banned_or_wedged(q) && !xe_sched_job_is_error(job)) { 776 if (!exec_queue_registered(q)) 777 register_exec_queue(q); 778 if (!lr) /* LR jobs are emitted in the exec IOCTL */ 779 q->ring_ops->emit_job(job); 780 submit_exec_queue(q); 781 } 782 783 if (lr) { 784 xe_sched_job_set_error(job, -EOPNOTSUPP); 785 return NULL; 786 } else if (test_and_set_bit(JOB_FLAG_SUBMIT, &job->fence->flags)) { 787 return job->fence; 788 } else { 789 return dma_fence_get(job->fence); 790 } 791 } 792 793 static void guc_exec_queue_free_job(struct drm_sched_job *drm_job) 794 { 795 struct xe_sched_job *job = to_xe_sched_job(drm_job); 796 797 xe_exec_queue_update_run_ticks(job->q); 798 799 trace_xe_sched_job_free(job); 800 xe_sched_job_put(job); 801 } 802 803 static int guc_read_stopped(struct xe_guc *guc) 804 { 805 return atomic_read(&guc->submission_state.stopped); 806 } 807 808 #define MAKE_SCHED_CONTEXT_ACTION(q, enable_disable) \ 809 u32 action[] = { \ 810 XE_GUC_ACTION_SCHED_CONTEXT_MODE_SET, \ 811 q->guc->id, \ 812 GUC_CONTEXT_##enable_disable, \ 813 } 814 815 static void disable_scheduling_deregister(struct xe_guc *guc, 816 struct xe_exec_queue *q) 817 { 818 MAKE_SCHED_CONTEXT_ACTION(q, DISABLE); 819 struct xe_device *xe = guc_to_xe(guc); 820 int ret; 821 822 set_min_preemption_timeout(guc, q); 823 smp_rmb(); 824 ret = wait_event_timeout(guc->ct.wq, !exec_queue_pending_enable(q) || 825 guc_read_stopped(guc), HZ * 5); 826 if (!ret) { 827 struct xe_gpu_scheduler *sched = &q->guc->sched; 828 829 drm_warn(&xe->drm, "Pending enable failed to respond"); 830 xe_sched_submission_start(sched); 831 xe_gt_reset_async(q->gt); 832 xe_sched_tdr_queue_imm(sched); 833 return; 834 } 835 836 clear_exec_queue_enabled(q); 837 set_exec_queue_pending_disable(q); 838 set_exec_queue_destroyed(q); 839 trace_xe_exec_queue_scheduling_disable(q); 840 841 /* 842 * Reserve space for both G2H here as the 2nd G2H is sent from a G2H 843 * handler and we are not allowed to reserved G2H space in handlers. 844 */ 845 xe_guc_ct_send(&guc->ct, action, ARRAY_SIZE(action), 846 G2H_LEN_DW_SCHED_CONTEXT_MODE_SET + 847 G2H_LEN_DW_DEREGISTER_CONTEXT, 2); 848 } 849 850 static void xe_guc_exec_queue_trigger_cleanup(struct xe_exec_queue *q) 851 { 852 struct xe_guc *guc = exec_queue_to_guc(q); 853 struct xe_device *xe = guc_to_xe(guc); 854 855 /** to wakeup xe_wait_user_fence ioctl if exec queue is reset */ 856 wake_up_all(&xe->ufence_wq); 857 858 if (xe_exec_queue_is_lr(q)) 859 queue_work(guc_to_gt(guc)->ordered_wq, &q->guc->lr_tdr); 860 else 861 xe_sched_tdr_queue_imm(&q->guc->sched); 862 } 863 864 /** 865 * xe_guc_submit_wedge() - Wedge GuC submission 866 * @guc: the GuC object 867 * 868 * Save exec queue's registered with GuC state by taking a ref to each queue. 869 * Register a DRMM handler to drop refs upon driver unload. 870 */ 871 void xe_guc_submit_wedge(struct xe_guc *guc) 872 { 873 struct xe_device *xe = guc_to_xe(guc); 874 struct xe_exec_queue *q; 875 unsigned long index; 876 int err; 877 878 xe_gt_assert(guc_to_gt(guc), guc_to_xe(guc)->wedged.mode); 879 880 err = devm_add_action_or_reset(guc_to_xe(guc)->drm.dev, 881 guc_submit_wedged_fini, guc); 882 if (err) { 883 drm_err(&xe->drm, "Failed to register xe_guc_submit clean-up on wedged.mode=2. Although device is wedged.\n"); 884 return; 885 } 886 887 mutex_lock(&guc->submission_state.lock); 888 xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) 889 if (xe_exec_queue_get_unless_zero(q)) 890 set_exec_queue_wedged(q); 891 mutex_unlock(&guc->submission_state.lock); 892 } 893 894 static bool guc_submit_hint_wedged(struct xe_guc *guc) 895 { 896 struct xe_device *xe = guc_to_xe(guc); 897 898 if (xe->wedged.mode != 2) 899 return false; 900 901 if (xe_device_wedged(xe)) 902 return true; 903 904 xe_device_declare_wedged(xe); 905 906 return true; 907 } 908 909 static void xe_guc_exec_queue_lr_cleanup(struct work_struct *w) 910 { 911 struct xe_guc_exec_queue *ge = 912 container_of(w, struct xe_guc_exec_queue, lr_tdr); 913 struct xe_exec_queue *q = ge->q; 914 struct xe_guc *guc = exec_queue_to_guc(q); 915 struct xe_device *xe = guc_to_xe(guc); 916 struct xe_gpu_scheduler *sched = &ge->sched; 917 bool wedged; 918 919 xe_assert(xe, xe_exec_queue_is_lr(q)); 920 trace_xe_exec_queue_lr_cleanup(q); 921 922 wedged = guc_submit_hint_wedged(exec_queue_to_guc(q)); 923 924 /* Kill the run_job / process_msg entry points */ 925 xe_sched_submission_stop(sched); 926 927 /* 928 * Engine state now mostly stable, disable scheduling / deregister if 929 * needed. This cleanup routine might be called multiple times, where 930 * the actual async engine deregister drops the final engine ref. 931 * Calling disable_scheduling_deregister will mark the engine as 932 * destroyed and fire off the CT requests to disable scheduling / 933 * deregister, which we only want to do once. We also don't want to mark 934 * the engine as pending_disable again as this may race with the 935 * xe_guc_deregister_done_handler() which treats it as an unexpected 936 * state. 937 */ 938 if (!wedged && exec_queue_registered(q) && !exec_queue_destroyed(q)) { 939 struct xe_guc *guc = exec_queue_to_guc(q); 940 int ret; 941 942 set_exec_queue_banned(q); 943 disable_scheduling_deregister(guc, q); 944 945 /* 946 * Must wait for scheduling to be disabled before signalling 947 * any fences, if GT broken the GT reset code should signal us. 948 */ 949 ret = wait_event_timeout(guc->ct.wq, 950 !exec_queue_pending_disable(q) || 951 guc_read_stopped(guc), HZ * 5); 952 if (!ret) { 953 drm_warn(&xe->drm, "Schedule disable failed to respond"); 954 xe_sched_submission_start(sched); 955 xe_gt_reset_async(q->gt); 956 return; 957 } 958 } 959 960 xe_sched_submission_start(sched); 961 } 962 963 #define ADJUST_FIVE_PERCENT(__t) mul_u64_u32_div(__t, 105, 100) 964 965 static bool check_timeout(struct xe_exec_queue *q, struct xe_sched_job *job) 966 { 967 struct xe_gt *gt = guc_to_gt(exec_queue_to_guc(q)); 968 u32 ctx_timestamp = xe_lrc_ctx_timestamp(q->lrc[0]); 969 u32 ctx_job_timestamp = xe_lrc_ctx_job_timestamp(q->lrc[0]); 970 u32 timeout_ms = q->sched_props.job_timeout_ms; 971 u32 diff; 972 u64 running_time_ms; 973 974 /* 975 * Counter wraps at ~223s at the usual 19.2MHz, be paranoid catch 976 * possible overflows with a high timeout. 977 */ 978 xe_gt_assert(gt, timeout_ms < 100 * MSEC_PER_SEC); 979 980 if (ctx_timestamp < ctx_job_timestamp) 981 diff = ctx_timestamp + U32_MAX - ctx_job_timestamp; 982 else 983 diff = ctx_timestamp - ctx_job_timestamp; 984 985 /* 986 * Ensure timeout is within 5% to account for an GuC scheduling latency 987 */ 988 running_time_ms = 989 ADJUST_FIVE_PERCENT(xe_gt_clock_interval_to_ms(gt, diff)); 990 991 xe_gt_dbg(gt, 992 "Check job timeout: seqno=%u, lrc_seqno=%u, guc_id=%d, running_time_ms=%llu, timeout_ms=%u, diff=0x%08x", 993 xe_sched_job_seqno(job), xe_sched_job_lrc_seqno(job), 994 q->guc->id, running_time_ms, timeout_ms, diff); 995 996 return running_time_ms >= timeout_ms; 997 } 998 999 static void enable_scheduling(struct xe_exec_queue *q) 1000 { 1001 MAKE_SCHED_CONTEXT_ACTION(q, ENABLE); 1002 struct xe_guc *guc = exec_queue_to_guc(q); 1003 int ret; 1004 1005 xe_gt_assert(guc_to_gt(guc), !exec_queue_destroyed(q)); 1006 xe_gt_assert(guc_to_gt(guc), exec_queue_registered(q)); 1007 xe_gt_assert(guc_to_gt(guc), !exec_queue_pending_disable(q)); 1008 xe_gt_assert(guc_to_gt(guc), !exec_queue_pending_enable(q)); 1009 1010 set_exec_queue_pending_enable(q); 1011 set_exec_queue_enabled(q); 1012 trace_xe_exec_queue_scheduling_enable(q); 1013 1014 xe_guc_ct_send(&guc->ct, action, ARRAY_SIZE(action), 1015 G2H_LEN_DW_SCHED_CONTEXT_MODE_SET, 1); 1016 1017 ret = wait_event_timeout(guc->ct.wq, 1018 !exec_queue_pending_enable(q) || 1019 guc_read_stopped(guc), HZ * 5); 1020 if (!ret || guc_read_stopped(guc)) { 1021 xe_gt_warn(guc_to_gt(guc), "Schedule enable failed to respond"); 1022 set_exec_queue_banned(q); 1023 xe_gt_reset_async(q->gt); 1024 xe_sched_tdr_queue_imm(&q->guc->sched); 1025 } 1026 } 1027 1028 static void disable_scheduling(struct xe_exec_queue *q, bool immediate) 1029 { 1030 MAKE_SCHED_CONTEXT_ACTION(q, DISABLE); 1031 struct xe_guc *guc = exec_queue_to_guc(q); 1032 1033 xe_gt_assert(guc_to_gt(guc), !exec_queue_destroyed(q)); 1034 xe_gt_assert(guc_to_gt(guc), exec_queue_registered(q)); 1035 xe_gt_assert(guc_to_gt(guc), !exec_queue_pending_disable(q)); 1036 1037 if (immediate) 1038 set_min_preemption_timeout(guc, q); 1039 clear_exec_queue_enabled(q); 1040 set_exec_queue_pending_disable(q); 1041 trace_xe_exec_queue_scheduling_disable(q); 1042 1043 xe_guc_ct_send(&guc->ct, action, ARRAY_SIZE(action), 1044 G2H_LEN_DW_SCHED_CONTEXT_MODE_SET, 1); 1045 } 1046 1047 static void __deregister_exec_queue(struct xe_guc *guc, struct xe_exec_queue *q) 1048 { 1049 u32 action[] = { 1050 XE_GUC_ACTION_DEREGISTER_CONTEXT, 1051 q->guc->id, 1052 }; 1053 1054 xe_gt_assert(guc_to_gt(guc), !exec_queue_destroyed(q)); 1055 xe_gt_assert(guc_to_gt(guc), exec_queue_registered(q)); 1056 xe_gt_assert(guc_to_gt(guc), !exec_queue_pending_enable(q)); 1057 xe_gt_assert(guc_to_gt(guc), !exec_queue_pending_disable(q)); 1058 1059 set_exec_queue_destroyed(q); 1060 trace_xe_exec_queue_deregister(q); 1061 1062 xe_guc_ct_send(&guc->ct, action, ARRAY_SIZE(action), 1063 G2H_LEN_DW_DEREGISTER_CONTEXT, 1); 1064 } 1065 1066 static enum drm_gpu_sched_stat 1067 guc_exec_queue_timedout_job(struct drm_sched_job *drm_job) 1068 { 1069 struct xe_sched_job *job = to_xe_sched_job(drm_job); 1070 struct xe_sched_job *tmp_job; 1071 struct xe_exec_queue *q = job->q; 1072 struct xe_gpu_scheduler *sched = &q->guc->sched; 1073 struct xe_guc *guc = exec_queue_to_guc(q); 1074 const char *process_name = "no process"; 1075 int err = -ETIME; 1076 pid_t pid = -1; 1077 int i = 0; 1078 bool wedged, skip_timeout_check; 1079 1080 /* 1081 * TDR has fired before free job worker. Common if exec queue 1082 * immediately closed after last fence signaled. 1083 */ 1084 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &job->fence->flags)) { 1085 guc_exec_queue_free_job(drm_job); 1086 1087 return DRM_GPU_SCHED_STAT_NOMINAL; 1088 } 1089 1090 /* Kill the run_job entry point */ 1091 xe_sched_submission_stop(sched); 1092 1093 /* Must check all state after stopping scheduler */ 1094 skip_timeout_check = exec_queue_reset(q) || 1095 exec_queue_killed_or_banned_or_wedged(q) || 1096 exec_queue_destroyed(q); 1097 1098 /* Job hasn't started, can't be timed out */ 1099 if (!skip_timeout_check && !xe_sched_job_started(job)) 1100 goto rearm; 1101 1102 /* 1103 * XXX: Sampling timeout doesn't work in wedged mode as we have to 1104 * modify scheduling state to read timestamp. We could read the 1105 * timestamp from a register to accumulate current running time but this 1106 * doesn't work for SRIOV. For now assuming timeouts in wedged mode are 1107 * genuine timeouts. 1108 */ 1109 wedged = guc_submit_hint_wedged(exec_queue_to_guc(q)); 1110 1111 /* Engine state now stable, disable scheduling to check timestamp */ 1112 if (!wedged && exec_queue_registered(q)) { 1113 int ret; 1114 1115 if (exec_queue_reset(q)) 1116 err = -EIO; 1117 1118 if (!exec_queue_destroyed(q)) { 1119 /* 1120 * Wait for any pending G2H to flush out before 1121 * modifying state 1122 */ 1123 ret = wait_event_timeout(guc->ct.wq, 1124 !exec_queue_pending_enable(q) || 1125 guc_read_stopped(guc), HZ * 5); 1126 if (!ret || guc_read_stopped(guc)) 1127 goto trigger_reset; 1128 1129 /* 1130 * Flag communicates to G2H handler that schedule 1131 * disable originated from a timeout check. The G2H then 1132 * avoid triggering cleanup or deregistering the exec 1133 * queue. 1134 */ 1135 set_exec_queue_check_timeout(q); 1136 disable_scheduling(q, skip_timeout_check); 1137 } 1138 1139 /* 1140 * Must wait for scheduling to be disabled before signalling 1141 * any fences, if GT broken the GT reset code should signal us. 1142 * 1143 * FIXME: Tests can generate a ton of 0x6000 (IOMMU CAT fault 1144 * error) messages which can cause the schedule disable to get 1145 * lost. If this occurs, trigger a GT reset to recover. 1146 */ 1147 smp_rmb(); 1148 ret = wait_event_timeout(guc->ct.wq, 1149 !exec_queue_pending_disable(q) || 1150 guc_read_stopped(guc), HZ * 5); 1151 if (!ret || guc_read_stopped(guc)) { 1152 trigger_reset: 1153 if (!ret) 1154 xe_gt_warn(guc_to_gt(guc), "Schedule disable failed to respond"); 1155 set_exec_queue_extra_ref(q); 1156 xe_exec_queue_get(q); /* GT reset owns this */ 1157 set_exec_queue_banned(q); 1158 xe_gt_reset_async(q->gt); 1159 xe_sched_tdr_queue_imm(sched); 1160 goto rearm; 1161 } 1162 } 1163 1164 /* 1165 * Check if job is actually timed out, if so restart job execution and TDR 1166 */ 1167 if (!wedged && !skip_timeout_check && !check_timeout(q, job) && 1168 !exec_queue_reset(q) && exec_queue_registered(q)) { 1169 clear_exec_queue_check_timeout(q); 1170 goto sched_enable; 1171 } 1172 1173 if (q->vm && q->vm->xef) { 1174 process_name = q->vm->xef->process_name; 1175 pid = q->vm->xef->pid; 1176 } 1177 xe_gt_notice(guc_to_gt(guc), "Timedout job: seqno=%u, lrc_seqno=%u, guc_id=%d, flags=0x%lx in %s [%d]", 1178 xe_sched_job_seqno(job), xe_sched_job_lrc_seqno(job), 1179 q->guc->id, q->flags, process_name, pid); 1180 1181 trace_xe_sched_job_timedout(job); 1182 1183 if (!exec_queue_killed(q)) 1184 xe_devcoredump(job); 1185 1186 /* 1187 * Kernel jobs should never fail, nor should VM jobs if they do 1188 * somethings has gone wrong and the GT needs a reset 1189 */ 1190 xe_gt_WARN(q->gt, q->flags & EXEC_QUEUE_FLAG_KERNEL, 1191 "Kernel-submitted job timed out\n"); 1192 xe_gt_WARN(q->gt, q->flags & EXEC_QUEUE_FLAG_VM && !exec_queue_killed(q), 1193 "VM job timed out on non-killed execqueue\n"); 1194 if (!wedged && (q->flags & EXEC_QUEUE_FLAG_KERNEL || 1195 (q->flags & EXEC_QUEUE_FLAG_VM && !exec_queue_killed(q)))) { 1196 if (!xe_sched_invalidate_job(job, 2)) { 1197 clear_exec_queue_check_timeout(q); 1198 xe_gt_reset_async(q->gt); 1199 goto rearm; 1200 } 1201 } 1202 1203 /* Finish cleaning up exec queue via deregister */ 1204 set_exec_queue_banned(q); 1205 if (!wedged && exec_queue_registered(q) && !exec_queue_destroyed(q)) { 1206 set_exec_queue_extra_ref(q); 1207 xe_exec_queue_get(q); 1208 __deregister_exec_queue(guc, q); 1209 } 1210 1211 /* Stop fence signaling */ 1212 xe_hw_fence_irq_stop(q->fence_irq); 1213 1214 /* 1215 * Fence state now stable, stop / start scheduler which cleans up any 1216 * fences that are complete 1217 */ 1218 xe_sched_add_pending_job(sched, job); 1219 xe_sched_submission_start(sched); 1220 1221 xe_guc_exec_queue_trigger_cleanup(q); 1222 1223 /* Mark all outstanding jobs as bad, thus completing them */ 1224 spin_lock(&sched->base.job_list_lock); 1225 list_for_each_entry(tmp_job, &sched->base.pending_list, drm.list) 1226 xe_sched_job_set_error(tmp_job, !i++ ? err : -ECANCELED); 1227 spin_unlock(&sched->base.job_list_lock); 1228 1229 /* Start fence signaling */ 1230 xe_hw_fence_irq_start(q->fence_irq); 1231 1232 return DRM_GPU_SCHED_STAT_NOMINAL; 1233 1234 sched_enable: 1235 enable_scheduling(q); 1236 rearm: 1237 /* 1238 * XXX: Ideally want to adjust timeout based on current exection time 1239 * but there is not currently an easy way to do in DRM scheduler. With 1240 * some thought, do this in a follow up. 1241 */ 1242 xe_sched_add_pending_job(sched, job); 1243 xe_sched_submission_start(sched); 1244 1245 return DRM_GPU_SCHED_STAT_NOMINAL; 1246 } 1247 1248 static void __guc_exec_queue_fini_async(struct work_struct *w) 1249 { 1250 struct xe_guc_exec_queue *ge = 1251 container_of(w, struct xe_guc_exec_queue, fini_async); 1252 struct xe_exec_queue *q = ge->q; 1253 struct xe_guc *guc = exec_queue_to_guc(q); 1254 1255 xe_pm_runtime_get(guc_to_xe(guc)); 1256 trace_xe_exec_queue_destroy(q); 1257 1258 if (xe_exec_queue_is_lr(q)) 1259 cancel_work_sync(&ge->lr_tdr); 1260 release_guc_id(guc, q); 1261 xe_sched_entity_fini(&ge->entity); 1262 xe_sched_fini(&ge->sched); 1263 1264 kfree(ge); 1265 xe_exec_queue_fini(q); 1266 xe_pm_runtime_put(guc_to_xe(guc)); 1267 } 1268 1269 static void guc_exec_queue_fini_async(struct xe_exec_queue *q) 1270 { 1271 INIT_WORK(&q->guc->fini_async, __guc_exec_queue_fini_async); 1272 1273 /* We must block on kernel engines so slabs are empty on driver unload */ 1274 if (q->flags & EXEC_QUEUE_FLAG_PERMANENT || exec_queue_wedged(q)) 1275 __guc_exec_queue_fini_async(&q->guc->fini_async); 1276 else 1277 queue_work(system_wq, &q->guc->fini_async); 1278 } 1279 1280 static void __guc_exec_queue_fini(struct xe_guc *guc, struct xe_exec_queue *q) 1281 { 1282 /* 1283 * Might be done from within the GPU scheduler, need to do async as we 1284 * fini the scheduler when the engine is fini'd, the scheduler can't 1285 * complete fini within itself (circular dependency). Async resolves 1286 * this we and don't really care when everything is fini'd, just that it 1287 * is. 1288 */ 1289 guc_exec_queue_fini_async(q); 1290 } 1291 1292 static void __guc_exec_queue_process_msg_cleanup(struct xe_sched_msg *msg) 1293 { 1294 struct xe_exec_queue *q = msg->private_data; 1295 struct xe_guc *guc = exec_queue_to_guc(q); 1296 struct xe_device *xe = guc_to_xe(guc); 1297 1298 xe_assert(xe, !(q->flags & EXEC_QUEUE_FLAG_PERMANENT)); 1299 trace_xe_exec_queue_cleanup_entity(q); 1300 1301 if (exec_queue_registered(q)) 1302 disable_scheduling_deregister(guc, q); 1303 else 1304 __guc_exec_queue_fini(guc, q); 1305 } 1306 1307 static bool guc_exec_queue_allowed_to_change_state(struct xe_exec_queue *q) 1308 { 1309 return !exec_queue_killed_or_banned_or_wedged(q) && exec_queue_registered(q); 1310 } 1311 1312 static void __guc_exec_queue_process_msg_set_sched_props(struct xe_sched_msg *msg) 1313 { 1314 struct xe_exec_queue *q = msg->private_data; 1315 struct xe_guc *guc = exec_queue_to_guc(q); 1316 1317 if (guc_exec_queue_allowed_to_change_state(q)) 1318 init_policies(guc, q); 1319 kfree(msg); 1320 } 1321 1322 static void __suspend_fence_signal(struct xe_exec_queue *q) 1323 { 1324 if (!q->guc->suspend_pending) 1325 return; 1326 1327 WRITE_ONCE(q->guc->suspend_pending, false); 1328 wake_up(&q->guc->suspend_wait); 1329 } 1330 1331 static void suspend_fence_signal(struct xe_exec_queue *q) 1332 { 1333 struct xe_guc *guc = exec_queue_to_guc(q); 1334 struct xe_device *xe = guc_to_xe(guc); 1335 1336 xe_assert(xe, exec_queue_suspended(q) || exec_queue_killed(q) || 1337 guc_read_stopped(guc)); 1338 xe_assert(xe, q->guc->suspend_pending); 1339 1340 __suspend_fence_signal(q); 1341 } 1342 1343 static void __guc_exec_queue_process_msg_suspend(struct xe_sched_msg *msg) 1344 { 1345 struct xe_exec_queue *q = msg->private_data; 1346 struct xe_guc *guc = exec_queue_to_guc(q); 1347 1348 if (guc_exec_queue_allowed_to_change_state(q) && !exec_queue_suspended(q) && 1349 exec_queue_enabled(q)) { 1350 wait_event(guc->ct.wq, q->guc->resume_time != RESUME_PENDING || 1351 guc_read_stopped(guc)); 1352 1353 if (!guc_read_stopped(guc)) { 1354 s64 since_resume_ms = 1355 ktime_ms_delta(ktime_get(), 1356 q->guc->resume_time); 1357 s64 wait_ms = q->vm->preempt.min_run_period_ms - 1358 since_resume_ms; 1359 1360 if (wait_ms > 0 && q->guc->resume_time) 1361 msleep(wait_ms); 1362 1363 set_exec_queue_suspended(q); 1364 disable_scheduling(q, false); 1365 } 1366 } else if (q->guc->suspend_pending) { 1367 set_exec_queue_suspended(q); 1368 suspend_fence_signal(q); 1369 } 1370 } 1371 1372 static void __guc_exec_queue_process_msg_resume(struct xe_sched_msg *msg) 1373 { 1374 struct xe_exec_queue *q = msg->private_data; 1375 1376 if (guc_exec_queue_allowed_to_change_state(q)) { 1377 clear_exec_queue_suspended(q); 1378 if (!exec_queue_enabled(q)) { 1379 q->guc->resume_time = RESUME_PENDING; 1380 enable_scheduling(q); 1381 } 1382 } else { 1383 clear_exec_queue_suspended(q); 1384 } 1385 } 1386 1387 #define CLEANUP 1 /* Non-zero values to catch uninitialized msg */ 1388 #define SET_SCHED_PROPS 2 1389 #define SUSPEND 3 1390 #define RESUME 4 1391 #define OPCODE_MASK 0xf 1392 #define MSG_LOCKED BIT(8) 1393 1394 static void guc_exec_queue_process_msg(struct xe_sched_msg *msg) 1395 { 1396 struct xe_device *xe = guc_to_xe(exec_queue_to_guc(msg->private_data)); 1397 1398 trace_xe_sched_msg_recv(msg); 1399 1400 switch (msg->opcode) { 1401 case CLEANUP: 1402 __guc_exec_queue_process_msg_cleanup(msg); 1403 break; 1404 case SET_SCHED_PROPS: 1405 __guc_exec_queue_process_msg_set_sched_props(msg); 1406 break; 1407 case SUSPEND: 1408 __guc_exec_queue_process_msg_suspend(msg); 1409 break; 1410 case RESUME: 1411 __guc_exec_queue_process_msg_resume(msg); 1412 break; 1413 default: 1414 XE_WARN_ON("Unknown message type"); 1415 } 1416 1417 xe_pm_runtime_put(xe); 1418 } 1419 1420 static const struct drm_sched_backend_ops drm_sched_ops = { 1421 .run_job = guc_exec_queue_run_job, 1422 .free_job = guc_exec_queue_free_job, 1423 .timedout_job = guc_exec_queue_timedout_job, 1424 }; 1425 1426 static const struct xe_sched_backend_ops xe_sched_ops = { 1427 .process_msg = guc_exec_queue_process_msg, 1428 }; 1429 1430 static int guc_exec_queue_init(struct xe_exec_queue *q) 1431 { 1432 struct xe_gpu_scheduler *sched; 1433 struct xe_guc *guc = exec_queue_to_guc(q); 1434 struct xe_device *xe = guc_to_xe(guc); 1435 struct xe_guc_exec_queue *ge; 1436 long timeout; 1437 int err, i; 1438 1439 xe_assert(xe, xe_device_uc_enabled(guc_to_xe(guc))); 1440 1441 ge = kzalloc(sizeof(*ge), GFP_KERNEL); 1442 if (!ge) 1443 return -ENOMEM; 1444 1445 q->guc = ge; 1446 ge->q = q; 1447 init_waitqueue_head(&ge->suspend_wait); 1448 1449 for (i = 0; i < MAX_STATIC_MSG_TYPE; ++i) 1450 INIT_LIST_HEAD(&ge->static_msgs[i].link); 1451 1452 timeout = (q->vm && xe_vm_in_lr_mode(q->vm)) ? MAX_SCHEDULE_TIMEOUT : 1453 msecs_to_jiffies(q->sched_props.job_timeout_ms); 1454 err = xe_sched_init(&ge->sched, &drm_sched_ops, &xe_sched_ops, 1455 get_submit_wq(guc), 1456 q->lrc[0]->ring.size / MAX_JOB_SIZE_BYTES, 64, 1457 timeout, guc_to_gt(guc)->ordered_wq, NULL, 1458 q->name, gt_to_xe(q->gt)->drm.dev); 1459 if (err) 1460 goto err_free; 1461 1462 sched = &ge->sched; 1463 err = xe_sched_entity_init(&ge->entity, sched); 1464 if (err) 1465 goto err_sched; 1466 1467 if (xe_exec_queue_is_lr(q)) 1468 INIT_WORK(&q->guc->lr_tdr, xe_guc_exec_queue_lr_cleanup); 1469 1470 mutex_lock(&guc->submission_state.lock); 1471 1472 err = alloc_guc_id(guc, q); 1473 if (err) 1474 goto err_entity; 1475 1476 q->entity = &ge->entity; 1477 1478 if (guc_read_stopped(guc)) 1479 xe_sched_stop(sched); 1480 1481 mutex_unlock(&guc->submission_state.lock); 1482 1483 xe_exec_queue_assign_name(q, q->guc->id); 1484 1485 trace_xe_exec_queue_create(q); 1486 1487 return 0; 1488 1489 err_entity: 1490 mutex_unlock(&guc->submission_state.lock); 1491 xe_sched_entity_fini(&ge->entity); 1492 err_sched: 1493 xe_sched_fini(&ge->sched); 1494 err_free: 1495 kfree(ge); 1496 1497 return err; 1498 } 1499 1500 static void guc_exec_queue_kill(struct xe_exec_queue *q) 1501 { 1502 trace_xe_exec_queue_kill(q); 1503 set_exec_queue_killed(q); 1504 __suspend_fence_signal(q); 1505 xe_guc_exec_queue_trigger_cleanup(q); 1506 } 1507 1508 static void guc_exec_queue_add_msg(struct xe_exec_queue *q, struct xe_sched_msg *msg, 1509 u32 opcode) 1510 { 1511 xe_pm_runtime_get_noresume(guc_to_xe(exec_queue_to_guc(q))); 1512 1513 INIT_LIST_HEAD(&msg->link); 1514 msg->opcode = opcode & OPCODE_MASK; 1515 msg->private_data = q; 1516 1517 trace_xe_sched_msg_add(msg); 1518 if (opcode & MSG_LOCKED) 1519 xe_sched_add_msg_locked(&q->guc->sched, msg); 1520 else 1521 xe_sched_add_msg(&q->guc->sched, msg); 1522 } 1523 1524 static bool guc_exec_queue_try_add_msg(struct xe_exec_queue *q, 1525 struct xe_sched_msg *msg, 1526 u32 opcode) 1527 { 1528 if (!list_empty(&msg->link)) 1529 return false; 1530 1531 guc_exec_queue_add_msg(q, msg, opcode | MSG_LOCKED); 1532 1533 return true; 1534 } 1535 1536 #define STATIC_MSG_CLEANUP 0 1537 #define STATIC_MSG_SUSPEND 1 1538 #define STATIC_MSG_RESUME 2 1539 static void guc_exec_queue_fini(struct xe_exec_queue *q) 1540 { 1541 struct xe_sched_msg *msg = q->guc->static_msgs + STATIC_MSG_CLEANUP; 1542 1543 if (!(q->flags & EXEC_QUEUE_FLAG_PERMANENT) && !exec_queue_wedged(q)) 1544 guc_exec_queue_add_msg(q, msg, CLEANUP); 1545 else 1546 __guc_exec_queue_fini(exec_queue_to_guc(q), q); 1547 } 1548 1549 static int guc_exec_queue_set_priority(struct xe_exec_queue *q, 1550 enum xe_exec_queue_priority priority) 1551 { 1552 struct xe_sched_msg *msg; 1553 1554 if (q->sched_props.priority == priority || 1555 exec_queue_killed_or_banned_or_wedged(q)) 1556 return 0; 1557 1558 msg = kmalloc(sizeof(*msg), GFP_KERNEL); 1559 if (!msg) 1560 return -ENOMEM; 1561 1562 q->sched_props.priority = priority; 1563 guc_exec_queue_add_msg(q, msg, SET_SCHED_PROPS); 1564 1565 return 0; 1566 } 1567 1568 static int guc_exec_queue_set_timeslice(struct xe_exec_queue *q, u32 timeslice_us) 1569 { 1570 struct xe_sched_msg *msg; 1571 1572 if (q->sched_props.timeslice_us == timeslice_us || 1573 exec_queue_killed_or_banned_or_wedged(q)) 1574 return 0; 1575 1576 msg = kmalloc(sizeof(*msg), GFP_KERNEL); 1577 if (!msg) 1578 return -ENOMEM; 1579 1580 q->sched_props.timeslice_us = timeslice_us; 1581 guc_exec_queue_add_msg(q, msg, SET_SCHED_PROPS); 1582 1583 return 0; 1584 } 1585 1586 static int guc_exec_queue_set_preempt_timeout(struct xe_exec_queue *q, 1587 u32 preempt_timeout_us) 1588 { 1589 struct xe_sched_msg *msg; 1590 1591 if (q->sched_props.preempt_timeout_us == preempt_timeout_us || 1592 exec_queue_killed_or_banned_or_wedged(q)) 1593 return 0; 1594 1595 msg = kmalloc(sizeof(*msg), GFP_KERNEL); 1596 if (!msg) 1597 return -ENOMEM; 1598 1599 q->sched_props.preempt_timeout_us = preempt_timeout_us; 1600 guc_exec_queue_add_msg(q, msg, SET_SCHED_PROPS); 1601 1602 return 0; 1603 } 1604 1605 static int guc_exec_queue_suspend(struct xe_exec_queue *q) 1606 { 1607 struct xe_gpu_scheduler *sched = &q->guc->sched; 1608 struct xe_sched_msg *msg = q->guc->static_msgs + STATIC_MSG_SUSPEND; 1609 1610 if (exec_queue_killed_or_banned_or_wedged(q)) 1611 return -EINVAL; 1612 1613 xe_sched_msg_lock(sched); 1614 if (guc_exec_queue_try_add_msg(q, msg, SUSPEND)) 1615 q->guc->suspend_pending = true; 1616 xe_sched_msg_unlock(sched); 1617 1618 return 0; 1619 } 1620 1621 static int guc_exec_queue_suspend_wait(struct xe_exec_queue *q) 1622 { 1623 struct xe_guc *guc = exec_queue_to_guc(q); 1624 int ret; 1625 1626 /* 1627 * Likely don't need to check exec_queue_killed() as we clear 1628 * suspend_pending upon kill but to be paranoid but races in which 1629 * suspend_pending is set after kill also check kill here. 1630 */ 1631 ret = wait_event_interruptible_timeout(q->guc->suspend_wait, 1632 !READ_ONCE(q->guc->suspend_pending) || 1633 exec_queue_killed(q) || 1634 guc_read_stopped(guc), 1635 HZ * 5); 1636 1637 if (!ret) { 1638 xe_gt_warn(guc_to_gt(guc), 1639 "Suspend fence, guc_id=%d, failed to respond", 1640 q->guc->id); 1641 /* XXX: Trigger GT reset? */ 1642 return -ETIME; 1643 } 1644 1645 return ret < 0 ? ret : 0; 1646 } 1647 1648 static void guc_exec_queue_resume(struct xe_exec_queue *q) 1649 { 1650 struct xe_gpu_scheduler *sched = &q->guc->sched; 1651 struct xe_sched_msg *msg = q->guc->static_msgs + STATIC_MSG_RESUME; 1652 struct xe_guc *guc = exec_queue_to_guc(q); 1653 struct xe_device *xe = guc_to_xe(guc); 1654 1655 xe_assert(xe, !q->guc->suspend_pending); 1656 1657 xe_sched_msg_lock(sched); 1658 guc_exec_queue_try_add_msg(q, msg, RESUME); 1659 xe_sched_msg_unlock(sched); 1660 } 1661 1662 static bool guc_exec_queue_reset_status(struct xe_exec_queue *q) 1663 { 1664 return exec_queue_reset(q) || exec_queue_killed_or_banned_or_wedged(q); 1665 } 1666 1667 /* 1668 * All of these functions are an abstraction layer which other parts of XE can 1669 * use to trap into the GuC backend. All of these functions, aside from init, 1670 * really shouldn't do much other than trap into the DRM scheduler which 1671 * synchronizes these operations. 1672 */ 1673 static const struct xe_exec_queue_ops guc_exec_queue_ops = { 1674 .init = guc_exec_queue_init, 1675 .kill = guc_exec_queue_kill, 1676 .fini = guc_exec_queue_fini, 1677 .set_priority = guc_exec_queue_set_priority, 1678 .set_timeslice = guc_exec_queue_set_timeslice, 1679 .set_preempt_timeout = guc_exec_queue_set_preempt_timeout, 1680 .suspend = guc_exec_queue_suspend, 1681 .suspend_wait = guc_exec_queue_suspend_wait, 1682 .resume = guc_exec_queue_resume, 1683 .reset_status = guc_exec_queue_reset_status, 1684 }; 1685 1686 static void guc_exec_queue_stop(struct xe_guc *guc, struct xe_exec_queue *q) 1687 { 1688 struct xe_gpu_scheduler *sched = &q->guc->sched; 1689 1690 /* Stop scheduling + flush any DRM scheduler operations */ 1691 xe_sched_submission_stop(sched); 1692 1693 /* Clean up lost G2H + reset engine state */ 1694 if (exec_queue_registered(q)) { 1695 if (exec_queue_extra_ref(q) || xe_exec_queue_is_lr(q)) 1696 xe_exec_queue_put(q); 1697 else if (exec_queue_destroyed(q)) 1698 __guc_exec_queue_fini(guc, q); 1699 } 1700 if (q->guc->suspend_pending) { 1701 set_exec_queue_suspended(q); 1702 suspend_fence_signal(q); 1703 } 1704 atomic_and(EXEC_QUEUE_STATE_WEDGED | EXEC_QUEUE_STATE_BANNED | 1705 EXEC_QUEUE_STATE_KILLED | EXEC_QUEUE_STATE_DESTROYED | 1706 EXEC_QUEUE_STATE_SUSPENDED, 1707 &q->guc->state); 1708 q->guc->resume_time = 0; 1709 trace_xe_exec_queue_stop(q); 1710 1711 /* 1712 * Ban any engine (aside from kernel and engines used for VM ops) with a 1713 * started but not complete job or if a job has gone through a GT reset 1714 * more than twice. 1715 */ 1716 if (!(q->flags & (EXEC_QUEUE_FLAG_KERNEL | EXEC_QUEUE_FLAG_VM))) { 1717 struct xe_sched_job *job = xe_sched_first_pending_job(sched); 1718 bool ban = false; 1719 1720 if (job) { 1721 if ((xe_sched_job_started(job) && 1722 !xe_sched_job_completed(job)) || 1723 xe_sched_invalidate_job(job, 2)) { 1724 trace_xe_sched_job_ban(job); 1725 ban = true; 1726 } 1727 } else if (xe_exec_queue_is_lr(q) && 1728 (xe_lrc_ring_head(q->lrc[0]) != xe_lrc_ring_tail(q->lrc[0]))) { 1729 ban = true; 1730 } 1731 1732 if (ban) { 1733 set_exec_queue_banned(q); 1734 xe_guc_exec_queue_trigger_cleanup(q); 1735 } 1736 } 1737 } 1738 1739 int xe_guc_submit_reset_prepare(struct xe_guc *guc) 1740 { 1741 int ret; 1742 1743 /* 1744 * Using an atomic here rather than submission_state.lock as this 1745 * function can be called while holding the CT lock (engine reset 1746 * failure). submission_state.lock needs the CT lock to resubmit jobs. 1747 * Atomic is not ideal, but it works to prevent against concurrent reset 1748 * and releasing any TDRs waiting on guc->submission_state.stopped. 1749 */ 1750 ret = atomic_fetch_or(1, &guc->submission_state.stopped); 1751 smp_wmb(); 1752 wake_up_all(&guc->ct.wq); 1753 1754 return ret; 1755 } 1756 1757 void xe_guc_submit_reset_wait(struct xe_guc *guc) 1758 { 1759 wait_event(guc->ct.wq, xe_device_wedged(guc_to_xe(guc)) || 1760 !guc_read_stopped(guc)); 1761 } 1762 1763 void xe_guc_submit_stop(struct xe_guc *guc) 1764 { 1765 struct xe_exec_queue *q; 1766 unsigned long index; 1767 struct xe_device *xe = guc_to_xe(guc); 1768 1769 xe_assert(xe, guc_read_stopped(guc) == 1); 1770 1771 mutex_lock(&guc->submission_state.lock); 1772 1773 xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) 1774 guc_exec_queue_stop(guc, q); 1775 1776 mutex_unlock(&guc->submission_state.lock); 1777 1778 /* 1779 * No one can enter the backend at this point, aside from new engine 1780 * creation which is protected by guc->submission_state.lock. 1781 */ 1782 1783 } 1784 1785 static void guc_exec_queue_start(struct xe_exec_queue *q) 1786 { 1787 struct xe_gpu_scheduler *sched = &q->guc->sched; 1788 1789 if (!exec_queue_killed_or_banned_or_wedged(q)) { 1790 int i; 1791 1792 trace_xe_exec_queue_resubmit(q); 1793 for (i = 0; i < q->width; ++i) 1794 xe_lrc_set_ring_head(q->lrc[i], q->lrc[i]->ring.tail); 1795 xe_sched_resubmit_jobs(sched); 1796 } 1797 1798 xe_sched_submission_start(sched); 1799 } 1800 1801 int xe_guc_submit_start(struct xe_guc *guc) 1802 { 1803 struct xe_exec_queue *q; 1804 unsigned long index; 1805 struct xe_device *xe = guc_to_xe(guc); 1806 1807 xe_assert(xe, guc_read_stopped(guc) == 1); 1808 1809 mutex_lock(&guc->submission_state.lock); 1810 atomic_dec(&guc->submission_state.stopped); 1811 xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) 1812 guc_exec_queue_start(q); 1813 mutex_unlock(&guc->submission_state.lock); 1814 1815 wake_up_all(&guc->ct.wq); 1816 1817 return 0; 1818 } 1819 1820 static struct xe_exec_queue * 1821 g2h_exec_queue_lookup(struct xe_guc *guc, u32 guc_id) 1822 { 1823 struct xe_device *xe = guc_to_xe(guc); 1824 struct xe_exec_queue *q; 1825 1826 if (unlikely(guc_id >= GUC_ID_MAX)) { 1827 drm_err(&xe->drm, "Invalid guc_id %u", guc_id); 1828 return NULL; 1829 } 1830 1831 q = xa_load(&guc->submission_state.exec_queue_lookup, guc_id); 1832 if (unlikely(!q)) { 1833 drm_err(&xe->drm, "Not engine present for guc_id %u", guc_id); 1834 return NULL; 1835 } 1836 1837 xe_assert(xe, guc_id >= q->guc->id); 1838 xe_assert(xe, guc_id < (q->guc->id + q->width)); 1839 1840 return q; 1841 } 1842 1843 static void deregister_exec_queue(struct xe_guc *guc, struct xe_exec_queue *q) 1844 { 1845 u32 action[] = { 1846 XE_GUC_ACTION_DEREGISTER_CONTEXT, 1847 q->guc->id, 1848 }; 1849 1850 xe_gt_assert(guc_to_gt(guc), exec_queue_destroyed(q)); 1851 xe_gt_assert(guc_to_gt(guc), exec_queue_registered(q)); 1852 xe_gt_assert(guc_to_gt(guc), !exec_queue_pending_disable(q)); 1853 xe_gt_assert(guc_to_gt(guc), !exec_queue_pending_enable(q)); 1854 1855 trace_xe_exec_queue_deregister(q); 1856 1857 xe_guc_ct_send_g2h_handler(&guc->ct, action, ARRAY_SIZE(action)); 1858 } 1859 1860 static void handle_sched_done(struct xe_guc *guc, struct xe_exec_queue *q, 1861 u32 runnable_state) 1862 { 1863 trace_xe_exec_queue_scheduling_done(q); 1864 1865 if (runnable_state == 1) { 1866 xe_gt_assert(guc_to_gt(guc), exec_queue_pending_enable(q)); 1867 1868 q->guc->resume_time = ktime_get(); 1869 clear_exec_queue_pending_enable(q); 1870 smp_wmb(); 1871 wake_up_all(&guc->ct.wq); 1872 } else { 1873 bool check_timeout = exec_queue_check_timeout(q); 1874 1875 xe_gt_assert(guc_to_gt(guc), runnable_state == 0); 1876 xe_gt_assert(guc_to_gt(guc), exec_queue_pending_disable(q)); 1877 1878 clear_exec_queue_pending_disable(q); 1879 if (q->guc->suspend_pending) { 1880 suspend_fence_signal(q); 1881 } else { 1882 if (exec_queue_banned(q) || check_timeout) { 1883 smp_wmb(); 1884 wake_up_all(&guc->ct.wq); 1885 } 1886 if (!check_timeout) 1887 deregister_exec_queue(guc, q); 1888 } 1889 } 1890 } 1891 1892 int xe_guc_sched_done_handler(struct xe_guc *guc, u32 *msg, u32 len) 1893 { 1894 struct xe_device *xe = guc_to_xe(guc); 1895 struct xe_exec_queue *q; 1896 u32 guc_id = msg[0]; 1897 u32 runnable_state = msg[1]; 1898 1899 if (unlikely(len < 2)) { 1900 drm_err(&xe->drm, "Invalid length %u", len); 1901 return -EPROTO; 1902 } 1903 1904 q = g2h_exec_queue_lookup(guc, guc_id); 1905 if (unlikely(!q)) 1906 return -EPROTO; 1907 1908 if (unlikely(!exec_queue_pending_enable(q) && 1909 !exec_queue_pending_disable(q))) { 1910 xe_gt_err(guc_to_gt(guc), 1911 "SCHED_DONE: Unexpected engine state 0x%04x, guc_id=%d, runnable_state=%u", 1912 atomic_read(&q->guc->state), q->guc->id, 1913 runnable_state); 1914 return -EPROTO; 1915 } 1916 1917 handle_sched_done(guc, q, runnable_state); 1918 1919 return 0; 1920 } 1921 1922 static void handle_deregister_done(struct xe_guc *guc, struct xe_exec_queue *q) 1923 { 1924 trace_xe_exec_queue_deregister_done(q); 1925 1926 clear_exec_queue_registered(q); 1927 1928 if (exec_queue_extra_ref(q) || xe_exec_queue_is_lr(q)) 1929 xe_exec_queue_put(q); 1930 else 1931 __guc_exec_queue_fini(guc, q); 1932 } 1933 1934 int xe_guc_deregister_done_handler(struct xe_guc *guc, u32 *msg, u32 len) 1935 { 1936 struct xe_device *xe = guc_to_xe(guc); 1937 struct xe_exec_queue *q; 1938 u32 guc_id = msg[0]; 1939 1940 if (unlikely(len < 1)) { 1941 drm_err(&xe->drm, "Invalid length %u", len); 1942 return -EPROTO; 1943 } 1944 1945 q = g2h_exec_queue_lookup(guc, guc_id); 1946 if (unlikely(!q)) 1947 return -EPROTO; 1948 1949 if (!exec_queue_destroyed(q) || exec_queue_pending_disable(q) || 1950 exec_queue_pending_enable(q) || exec_queue_enabled(q)) { 1951 xe_gt_err(guc_to_gt(guc), 1952 "DEREGISTER_DONE: Unexpected engine state 0x%04x, guc_id=%d", 1953 atomic_read(&q->guc->state), q->guc->id); 1954 return -EPROTO; 1955 } 1956 1957 handle_deregister_done(guc, q); 1958 1959 return 0; 1960 } 1961 1962 int xe_guc_exec_queue_reset_handler(struct xe_guc *guc, u32 *msg, u32 len) 1963 { 1964 struct xe_gt *gt = guc_to_gt(guc); 1965 struct xe_device *xe = guc_to_xe(guc); 1966 struct xe_exec_queue *q; 1967 u32 guc_id = msg[0]; 1968 1969 if (unlikely(len < 1)) { 1970 drm_err(&xe->drm, "Invalid length %u", len); 1971 return -EPROTO; 1972 } 1973 1974 q = g2h_exec_queue_lookup(guc, guc_id); 1975 if (unlikely(!q)) 1976 return -EPROTO; 1977 1978 xe_gt_info(gt, "Engine reset: engine_class=%s, logical_mask: 0x%x, guc_id=%d", 1979 xe_hw_engine_class_to_str(q->class), q->logical_mask, guc_id); 1980 1981 /* FIXME: Do error capture, most likely async */ 1982 1983 trace_xe_exec_queue_reset(q); 1984 1985 /* 1986 * A banned engine is a NOP at this point (came from 1987 * guc_exec_queue_timedout_job). Otherwise, kick drm scheduler to cancel 1988 * jobs by setting timeout of the job to the minimum value kicking 1989 * guc_exec_queue_timedout_job. 1990 */ 1991 set_exec_queue_reset(q); 1992 if (!exec_queue_banned(q) && !exec_queue_check_timeout(q)) 1993 xe_guc_exec_queue_trigger_cleanup(q); 1994 1995 return 0; 1996 } 1997 1998 int xe_guc_exec_queue_memory_cat_error_handler(struct xe_guc *guc, u32 *msg, 1999 u32 len) 2000 { 2001 struct xe_gt *gt = guc_to_gt(guc); 2002 struct xe_device *xe = guc_to_xe(guc); 2003 struct xe_exec_queue *q; 2004 u32 guc_id = msg[0]; 2005 2006 if (unlikely(len < 1)) { 2007 drm_err(&xe->drm, "Invalid length %u", len); 2008 return -EPROTO; 2009 } 2010 2011 q = g2h_exec_queue_lookup(guc, guc_id); 2012 if (unlikely(!q)) 2013 return -EPROTO; 2014 2015 xe_gt_dbg(gt, "Engine memory cat error: engine_class=%s, logical_mask: 0x%x, guc_id=%d", 2016 xe_hw_engine_class_to_str(q->class), q->logical_mask, guc_id); 2017 2018 trace_xe_exec_queue_memory_cat_error(q); 2019 2020 /* Treat the same as engine reset */ 2021 set_exec_queue_reset(q); 2022 if (!exec_queue_banned(q) && !exec_queue_check_timeout(q)) 2023 xe_guc_exec_queue_trigger_cleanup(q); 2024 2025 return 0; 2026 } 2027 2028 int xe_guc_exec_queue_reset_failure_handler(struct xe_guc *guc, u32 *msg, u32 len) 2029 { 2030 struct xe_device *xe = guc_to_xe(guc); 2031 u8 guc_class, instance; 2032 u32 reason; 2033 2034 if (unlikely(len != 3)) { 2035 drm_err(&xe->drm, "Invalid length %u", len); 2036 return -EPROTO; 2037 } 2038 2039 guc_class = msg[0]; 2040 instance = msg[1]; 2041 reason = msg[2]; 2042 2043 /* Unexpected failure of a hardware feature, log an actual error */ 2044 drm_err(&xe->drm, "GuC engine reset request failed on %d:%d because 0x%08X", 2045 guc_class, instance, reason); 2046 2047 xe_gt_reset_async(guc_to_gt(guc)); 2048 2049 return 0; 2050 } 2051 2052 static void 2053 guc_exec_queue_wq_snapshot_capture(struct xe_exec_queue *q, 2054 struct xe_guc_submit_exec_queue_snapshot *snapshot) 2055 { 2056 struct xe_guc *guc = exec_queue_to_guc(q); 2057 struct xe_device *xe = guc_to_xe(guc); 2058 struct iosys_map map = xe_lrc_parallel_map(q->lrc[0]); 2059 int i; 2060 2061 snapshot->guc.wqi_head = q->guc->wqi_head; 2062 snapshot->guc.wqi_tail = q->guc->wqi_tail; 2063 snapshot->parallel.wq_desc.head = parallel_read(xe, map, wq_desc.head); 2064 snapshot->parallel.wq_desc.tail = parallel_read(xe, map, wq_desc.tail); 2065 snapshot->parallel.wq_desc.status = parallel_read(xe, map, 2066 wq_desc.wq_status); 2067 2068 if (snapshot->parallel.wq_desc.head != 2069 snapshot->parallel.wq_desc.tail) { 2070 for (i = snapshot->parallel.wq_desc.head; 2071 i != snapshot->parallel.wq_desc.tail; 2072 i = (i + sizeof(u32)) % WQ_SIZE) 2073 snapshot->parallel.wq[i / sizeof(u32)] = 2074 parallel_read(xe, map, wq[i / sizeof(u32)]); 2075 } 2076 } 2077 2078 static void 2079 guc_exec_queue_wq_snapshot_print(struct xe_guc_submit_exec_queue_snapshot *snapshot, 2080 struct drm_printer *p) 2081 { 2082 int i; 2083 2084 drm_printf(p, "\tWQ head: %u (internal), %d (memory)\n", 2085 snapshot->guc.wqi_head, snapshot->parallel.wq_desc.head); 2086 drm_printf(p, "\tWQ tail: %u (internal), %d (memory)\n", 2087 snapshot->guc.wqi_tail, snapshot->parallel.wq_desc.tail); 2088 drm_printf(p, "\tWQ status: %u\n", snapshot->parallel.wq_desc.status); 2089 2090 if (snapshot->parallel.wq_desc.head != 2091 snapshot->parallel.wq_desc.tail) { 2092 for (i = snapshot->parallel.wq_desc.head; 2093 i != snapshot->parallel.wq_desc.tail; 2094 i = (i + sizeof(u32)) % WQ_SIZE) 2095 drm_printf(p, "\tWQ[%zu]: 0x%08x\n", i / sizeof(u32), 2096 snapshot->parallel.wq[i / sizeof(u32)]); 2097 } 2098 } 2099 2100 /** 2101 * xe_guc_exec_queue_snapshot_capture - Take a quick snapshot of the GuC Engine. 2102 * @q: faulty exec queue 2103 * 2104 * This can be printed out in a later stage like during dev_coredump 2105 * analysis. 2106 * 2107 * Returns: a GuC Submit Engine snapshot object that must be freed by the 2108 * caller, using `xe_guc_exec_queue_snapshot_free`. 2109 */ 2110 struct xe_guc_submit_exec_queue_snapshot * 2111 xe_guc_exec_queue_snapshot_capture(struct xe_exec_queue *q) 2112 { 2113 struct xe_gpu_scheduler *sched = &q->guc->sched; 2114 struct xe_guc_submit_exec_queue_snapshot *snapshot; 2115 int i; 2116 2117 snapshot = kzalloc(sizeof(*snapshot), GFP_ATOMIC); 2118 2119 if (!snapshot) 2120 return NULL; 2121 2122 snapshot->guc.id = q->guc->id; 2123 memcpy(&snapshot->name, &q->name, sizeof(snapshot->name)); 2124 snapshot->class = q->class; 2125 snapshot->logical_mask = q->logical_mask; 2126 snapshot->width = q->width; 2127 snapshot->refcount = kref_read(&q->refcount); 2128 snapshot->sched_timeout = sched->base.timeout; 2129 snapshot->sched_props.timeslice_us = q->sched_props.timeslice_us; 2130 snapshot->sched_props.preempt_timeout_us = 2131 q->sched_props.preempt_timeout_us; 2132 2133 snapshot->lrc = kmalloc_array(q->width, sizeof(struct xe_lrc_snapshot *), 2134 GFP_ATOMIC); 2135 2136 if (snapshot->lrc) { 2137 for (i = 0; i < q->width; ++i) { 2138 struct xe_lrc *lrc = q->lrc[i]; 2139 2140 snapshot->lrc[i] = xe_lrc_snapshot_capture(lrc); 2141 } 2142 } 2143 2144 snapshot->schedule_state = atomic_read(&q->guc->state); 2145 snapshot->exec_queue_flags = q->flags; 2146 2147 snapshot->parallel_execution = xe_exec_queue_is_parallel(q); 2148 if (snapshot->parallel_execution) 2149 guc_exec_queue_wq_snapshot_capture(q, snapshot); 2150 2151 spin_lock(&sched->base.job_list_lock); 2152 snapshot->pending_list_size = list_count_nodes(&sched->base.pending_list); 2153 snapshot->pending_list = kmalloc_array(snapshot->pending_list_size, 2154 sizeof(struct pending_list_snapshot), 2155 GFP_ATOMIC); 2156 2157 if (snapshot->pending_list) { 2158 struct xe_sched_job *job_iter; 2159 2160 i = 0; 2161 list_for_each_entry(job_iter, &sched->base.pending_list, drm.list) { 2162 snapshot->pending_list[i].seqno = 2163 xe_sched_job_seqno(job_iter); 2164 snapshot->pending_list[i].fence = 2165 dma_fence_is_signaled(job_iter->fence) ? 1 : 0; 2166 snapshot->pending_list[i].finished = 2167 dma_fence_is_signaled(&job_iter->drm.s_fence->finished) 2168 ? 1 : 0; 2169 i++; 2170 } 2171 } 2172 2173 spin_unlock(&sched->base.job_list_lock); 2174 2175 return snapshot; 2176 } 2177 2178 /** 2179 * xe_guc_exec_queue_snapshot_capture_delayed - Take delayed part of snapshot of the GuC Engine. 2180 * @snapshot: Previously captured snapshot of job. 2181 * 2182 * This captures some data that requires taking some locks, so it cannot be done in signaling path. 2183 */ 2184 void 2185 xe_guc_exec_queue_snapshot_capture_delayed(struct xe_guc_submit_exec_queue_snapshot *snapshot) 2186 { 2187 int i; 2188 2189 if (!snapshot || !snapshot->lrc) 2190 return; 2191 2192 for (i = 0; i < snapshot->width; ++i) 2193 xe_lrc_snapshot_capture_delayed(snapshot->lrc[i]); 2194 } 2195 2196 /** 2197 * xe_guc_exec_queue_snapshot_print - Print out a given GuC Engine snapshot. 2198 * @snapshot: GuC Submit Engine snapshot object. 2199 * @p: drm_printer where it will be printed out. 2200 * 2201 * This function prints out a given GuC Submit Engine snapshot object. 2202 */ 2203 void 2204 xe_guc_exec_queue_snapshot_print(struct xe_guc_submit_exec_queue_snapshot *snapshot, 2205 struct drm_printer *p) 2206 { 2207 int i; 2208 2209 if (!snapshot) 2210 return; 2211 2212 drm_printf(p, "\nGuC ID: %d\n", snapshot->guc.id); 2213 drm_printf(p, "\tName: %s\n", snapshot->name); 2214 drm_printf(p, "\tClass: %d\n", snapshot->class); 2215 drm_printf(p, "\tLogical mask: 0x%x\n", snapshot->logical_mask); 2216 drm_printf(p, "\tWidth: %d\n", snapshot->width); 2217 drm_printf(p, "\tRef: %d\n", snapshot->refcount); 2218 drm_printf(p, "\tTimeout: %ld (ms)\n", snapshot->sched_timeout); 2219 drm_printf(p, "\tTimeslice: %u (us)\n", 2220 snapshot->sched_props.timeslice_us); 2221 drm_printf(p, "\tPreempt timeout: %u (us)\n", 2222 snapshot->sched_props.preempt_timeout_us); 2223 2224 for (i = 0; snapshot->lrc && i < snapshot->width; ++i) 2225 xe_lrc_snapshot_print(snapshot->lrc[i], p); 2226 2227 drm_printf(p, "\tSchedule State: 0x%x\n", snapshot->schedule_state); 2228 drm_printf(p, "\tFlags: 0x%lx\n", snapshot->exec_queue_flags); 2229 2230 if (snapshot->parallel_execution) 2231 guc_exec_queue_wq_snapshot_print(snapshot, p); 2232 2233 for (i = 0; snapshot->pending_list && i < snapshot->pending_list_size; 2234 i++) 2235 drm_printf(p, "\tJob: seqno=%d, fence=%d, finished=%d\n", 2236 snapshot->pending_list[i].seqno, 2237 snapshot->pending_list[i].fence, 2238 snapshot->pending_list[i].finished); 2239 } 2240 2241 /** 2242 * xe_guc_exec_queue_snapshot_free - Free all allocated objects for a given 2243 * snapshot. 2244 * @snapshot: GuC Submit Engine snapshot object. 2245 * 2246 * This function free all the memory that needed to be allocated at capture 2247 * time. 2248 */ 2249 void xe_guc_exec_queue_snapshot_free(struct xe_guc_submit_exec_queue_snapshot *snapshot) 2250 { 2251 int i; 2252 2253 if (!snapshot) 2254 return; 2255 2256 if (snapshot->lrc) { 2257 for (i = 0; i < snapshot->width; i++) 2258 xe_lrc_snapshot_free(snapshot->lrc[i]); 2259 kfree(snapshot->lrc); 2260 } 2261 kfree(snapshot->pending_list); 2262 kfree(snapshot); 2263 } 2264 2265 static void guc_exec_queue_print(struct xe_exec_queue *q, struct drm_printer *p) 2266 { 2267 struct xe_guc_submit_exec_queue_snapshot *snapshot; 2268 2269 snapshot = xe_guc_exec_queue_snapshot_capture(q); 2270 xe_guc_exec_queue_snapshot_print(snapshot, p); 2271 xe_guc_exec_queue_snapshot_free(snapshot); 2272 } 2273 2274 /** 2275 * xe_guc_submit_print - GuC Submit Print. 2276 * @guc: GuC. 2277 * @p: drm_printer where it will be printed out. 2278 * 2279 * This function capture and prints snapshots of **all** GuC Engines. 2280 */ 2281 void xe_guc_submit_print(struct xe_guc *guc, struct drm_printer *p) 2282 { 2283 struct xe_exec_queue *q; 2284 unsigned long index; 2285 2286 if (!xe_device_uc_enabled(guc_to_xe(guc))) 2287 return; 2288 2289 mutex_lock(&guc->submission_state.lock); 2290 xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) 2291 guc_exec_queue_print(q, p); 2292 mutex_unlock(&guc->submission_state.lock); 2293 } 2294