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