1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2022 Intel Corporation 4 */ 5 6 #include "xe_guc_ct.h" 7 8 #include <linux/bitfield.h> 9 #include <linux/circ_buf.h> 10 #include <linux/delay.h> 11 12 #include <kunit/static_stub.h> 13 14 #include <drm/drm_managed.h> 15 16 #include "abi/guc_actions_abi.h" 17 #include "abi/guc_actions_sriov_abi.h" 18 #include "abi/guc_klvs_abi.h" 19 #include "xe_bo.h" 20 #include "xe_device.h" 21 #include "xe_gt.h" 22 #include "xe_gt_pagefault.h" 23 #include "xe_gt_printk.h" 24 #include "xe_gt_sriov_pf_control.h" 25 #include "xe_gt_sriov_pf_monitor.h" 26 #include "xe_gt_tlb_invalidation.h" 27 #include "xe_guc.h" 28 #include "xe_guc_relay.h" 29 #include "xe_guc_submit.h" 30 #include "xe_map.h" 31 #include "xe_pm.h" 32 #include "xe_trace_guc.h" 33 34 /* Used when a CT send wants to block and / or receive data */ 35 struct g2h_fence { 36 u32 *response_buffer; 37 u32 seqno; 38 u32 response_data; 39 u16 response_len; 40 u16 error; 41 u16 hint; 42 u16 reason; 43 bool retry; 44 bool fail; 45 bool done; 46 }; 47 48 static void g2h_fence_init(struct g2h_fence *g2h_fence, u32 *response_buffer) 49 { 50 g2h_fence->response_buffer = response_buffer; 51 g2h_fence->response_data = 0; 52 g2h_fence->response_len = 0; 53 g2h_fence->fail = false; 54 g2h_fence->retry = false; 55 g2h_fence->done = false; 56 g2h_fence->seqno = ~0x0; 57 } 58 59 static bool g2h_fence_needs_alloc(struct g2h_fence *g2h_fence) 60 { 61 return g2h_fence->seqno == ~0x0; 62 } 63 64 static struct xe_guc * 65 ct_to_guc(struct xe_guc_ct *ct) 66 { 67 return container_of(ct, struct xe_guc, ct); 68 } 69 70 static struct xe_gt * 71 ct_to_gt(struct xe_guc_ct *ct) 72 { 73 return container_of(ct, struct xe_gt, uc.guc.ct); 74 } 75 76 static struct xe_device * 77 ct_to_xe(struct xe_guc_ct *ct) 78 { 79 return gt_to_xe(ct_to_gt(ct)); 80 } 81 82 /** 83 * DOC: GuC CTB Blob 84 * 85 * We allocate single blob to hold both CTB descriptors and buffers: 86 * 87 * +--------+-----------------------------------------------+------+ 88 * | offset | contents | size | 89 * +========+===============================================+======+ 90 * | 0x0000 | H2G CTB Descriptor (send) | | 91 * +--------+-----------------------------------------------+ 4K | 92 * | 0x0800 | G2H CTB Descriptor (g2h) | | 93 * +--------+-----------------------------------------------+------+ 94 * | 0x1000 | H2G CT Buffer (send) | n*4K | 95 * | | | | 96 * +--------+-----------------------------------------------+------+ 97 * | 0x1000 | G2H CT Buffer (g2h) | m*4K | 98 * | + n*4K | | | 99 * +--------+-----------------------------------------------+------+ 100 * 101 * Size of each ``CT Buffer`` must be multiple of 4K. 102 * We don't expect too many messages in flight at any time, unless we are 103 * using the GuC submission. In that case each request requires a minimum 104 * 2 dwords which gives us a maximum 256 queue'd requests. Hopefully this 105 * enough space to avoid backpressure on the driver. We increase the size 106 * of the receive buffer (relative to the send) to ensure a G2H response 107 * CTB has a landing spot. 108 */ 109 110 #define CTB_DESC_SIZE ALIGN(sizeof(struct guc_ct_buffer_desc), SZ_2K) 111 #define CTB_H2G_BUFFER_SIZE (SZ_4K) 112 #define CTB_G2H_BUFFER_SIZE (4 * CTB_H2G_BUFFER_SIZE) 113 #define G2H_ROOM_BUFFER_SIZE (CTB_G2H_BUFFER_SIZE / 4) 114 115 /** 116 * xe_guc_ct_queue_proc_time_jiffies - Return maximum time to process a full 117 * CT command queue 118 * @ct: the &xe_guc_ct. Unused at this moment but will be used in the future. 119 * 120 * Observation is that a 4KiB buffer full of commands takes a little over a 121 * second to process. Use that to calculate maximum time to process a full CT 122 * command queue. 123 * 124 * Return: Maximum time to process a full CT queue in jiffies. 125 */ 126 long xe_guc_ct_queue_proc_time_jiffies(struct xe_guc_ct *ct) 127 { 128 BUILD_BUG_ON(!IS_ALIGNED(CTB_H2G_BUFFER_SIZE, SZ_4)); 129 return (CTB_H2G_BUFFER_SIZE / SZ_4K) * HZ; 130 } 131 132 static size_t guc_ct_size(void) 133 { 134 return 2 * CTB_DESC_SIZE + CTB_H2G_BUFFER_SIZE + 135 CTB_G2H_BUFFER_SIZE; 136 } 137 138 static void guc_ct_fini(struct drm_device *drm, void *arg) 139 { 140 struct xe_guc_ct *ct = arg; 141 142 destroy_workqueue(ct->g2h_wq); 143 xa_destroy(&ct->fence_lookup); 144 } 145 146 static void receive_g2h(struct xe_guc_ct *ct); 147 static void g2h_worker_func(struct work_struct *w); 148 static void safe_mode_worker_func(struct work_struct *w); 149 150 static void primelockdep(struct xe_guc_ct *ct) 151 { 152 if (!IS_ENABLED(CONFIG_LOCKDEP)) 153 return; 154 155 fs_reclaim_acquire(GFP_KERNEL); 156 might_lock(&ct->lock); 157 fs_reclaim_release(GFP_KERNEL); 158 } 159 160 int xe_guc_ct_init(struct xe_guc_ct *ct) 161 { 162 struct xe_device *xe = ct_to_xe(ct); 163 struct xe_gt *gt = ct_to_gt(ct); 164 struct xe_tile *tile = gt_to_tile(gt); 165 struct xe_bo *bo; 166 int err; 167 168 xe_gt_assert(gt, !(guc_ct_size() % PAGE_SIZE)); 169 170 ct->g2h_wq = alloc_ordered_workqueue("xe-g2h-wq", 0); 171 if (!ct->g2h_wq) 172 return -ENOMEM; 173 174 spin_lock_init(&ct->fast_lock); 175 xa_init(&ct->fence_lookup); 176 INIT_WORK(&ct->g2h_worker, g2h_worker_func); 177 INIT_DELAYED_WORK(&ct->safe_mode_worker, safe_mode_worker_func); 178 init_waitqueue_head(&ct->wq); 179 init_waitqueue_head(&ct->g2h_fence_wq); 180 181 err = drmm_mutex_init(&xe->drm, &ct->lock); 182 if (err) 183 return err; 184 185 primelockdep(ct); 186 187 bo = xe_managed_bo_create_pin_map(xe, tile, guc_ct_size(), 188 XE_BO_FLAG_SYSTEM | 189 XE_BO_FLAG_GGTT | 190 XE_BO_FLAG_GGTT_INVALIDATE); 191 if (IS_ERR(bo)) 192 return PTR_ERR(bo); 193 194 ct->bo = bo; 195 196 err = drmm_add_action_or_reset(&xe->drm, guc_ct_fini, ct); 197 if (err) 198 return err; 199 200 xe_gt_assert(gt, ct->state == XE_GUC_CT_STATE_NOT_INITIALIZED); 201 ct->state = XE_GUC_CT_STATE_DISABLED; 202 return 0; 203 } 204 205 #define desc_read(xe_, guc_ctb__, field_) \ 206 xe_map_rd_field(xe_, &guc_ctb__->desc, 0, \ 207 struct guc_ct_buffer_desc, field_) 208 209 #define desc_write(xe_, guc_ctb__, field_, val_) \ 210 xe_map_wr_field(xe_, &guc_ctb__->desc, 0, \ 211 struct guc_ct_buffer_desc, field_, val_) 212 213 static void guc_ct_ctb_h2g_init(struct xe_device *xe, struct guc_ctb *h2g, 214 struct iosys_map *map) 215 { 216 h2g->info.size = CTB_H2G_BUFFER_SIZE / sizeof(u32); 217 h2g->info.resv_space = 0; 218 h2g->info.tail = 0; 219 h2g->info.head = 0; 220 h2g->info.space = CIRC_SPACE(h2g->info.tail, h2g->info.head, 221 h2g->info.size) - 222 h2g->info.resv_space; 223 h2g->info.broken = false; 224 225 h2g->desc = *map; 226 xe_map_memset(xe, &h2g->desc, 0, 0, sizeof(struct guc_ct_buffer_desc)); 227 228 h2g->cmds = IOSYS_MAP_INIT_OFFSET(map, CTB_DESC_SIZE * 2); 229 } 230 231 static void guc_ct_ctb_g2h_init(struct xe_device *xe, struct guc_ctb *g2h, 232 struct iosys_map *map) 233 { 234 g2h->info.size = CTB_G2H_BUFFER_SIZE / sizeof(u32); 235 g2h->info.resv_space = G2H_ROOM_BUFFER_SIZE / sizeof(u32); 236 g2h->info.head = 0; 237 g2h->info.tail = 0; 238 g2h->info.space = CIRC_SPACE(g2h->info.tail, g2h->info.head, 239 g2h->info.size) - 240 g2h->info.resv_space; 241 g2h->info.broken = false; 242 243 g2h->desc = IOSYS_MAP_INIT_OFFSET(map, CTB_DESC_SIZE); 244 xe_map_memset(xe, &g2h->desc, 0, 0, sizeof(struct guc_ct_buffer_desc)); 245 246 g2h->cmds = IOSYS_MAP_INIT_OFFSET(map, CTB_DESC_SIZE * 2 + 247 CTB_H2G_BUFFER_SIZE); 248 } 249 250 static int guc_ct_ctb_h2g_register(struct xe_guc_ct *ct) 251 { 252 struct xe_guc *guc = ct_to_guc(ct); 253 u32 desc_addr, ctb_addr, size; 254 int err; 255 256 desc_addr = xe_bo_ggtt_addr(ct->bo); 257 ctb_addr = xe_bo_ggtt_addr(ct->bo) + CTB_DESC_SIZE * 2; 258 size = ct->ctbs.h2g.info.size * sizeof(u32); 259 260 err = xe_guc_self_cfg64(guc, 261 GUC_KLV_SELF_CFG_H2G_CTB_DESCRIPTOR_ADDR_KEY, 262 desc_addr); 263 if (err) 264 return err; 265 266 err = xe_guc_self_cfg64(guc, 267 GUC_KLV_SELF_CFG_H2G_CTB_ADDR_KEY, 268 ctb_addr); 269 if (err) 270 return err; 271 272 return xe_guc_self_cfg32(guc, 273 GUC_KLV_SELF_CFG_H2G_CTB_SIZE_KEY, 274 size); 275 } 276 277 static int guc_ct_ctb_g2h_register(struct xe_guc_ct *ct) 278 { 279 struct xe_guc *guc = ct_to_guc(ct); 280 u32 desc_addr, ctb_addr, size; 281 int err; 282 283 desc_addr = xe_bo_ggtt_addr(ct->bo) + CTB_DESC_SIZE; 284 ctb_addr = xe_bo_ggtt_addr(ct->bo) + CTB_DESC_SIZE * 2 + 285 CTB_H2G_BUFFER_SIZE; 286 size = ct->ctbs.g2h.info.size * sizeof(u32); 287 288 err = xe_guc_self_cfg64(guc, 289 GUC_KLV_SELF_CFG_G2H_CTB_DESCRIPTOR_ADDR_KEY, 290 desc_addr); 291 if (err) 292 return err; 293 294 err = xe_guc_self_cfg64(guc, 295 GUC_KLV_SELF_CFG_G2H_CTB_ADDR_KEY, 296 ctb_addr); 297 if (err) 298 return err; 299 300 return xe_guc_self_cfg32(guc, 301 GUC_KLV_SELF_CFG_G2H_CTB_SIZE_KEY, 302 size); 303 } 304 305 static int guc_ct_control_toggle(struct xe_guc_ct *ct, bool enable) 306 { 307 u32 request[HOST2GUC_CONTROL_CTB_REQUEST_MSG_LEN] = { 308 FIELD_PREP(GUC_HXG_MSG_0_ORIGIN, GUC_HXG_ORIGIN_HOST) | 309 FIELD_PREP(GUC_HXG_MSG_0_TYPE, GUC_HXG_TYPE_REQUEST) | 310 FIELD_PREP(GUC_HXG_REQUEST_MSG_0_ACTION, 311 GUC_ACTION_HOST2GUC_CONTROL_CTB), 312 FIELD_PREP(HOST2GUC_CONTROL_CTB_REQUEST_MSG_1_CONTROL, 313 enable ? GUC_CTB_CONTROL_ENABLE : 314 GUC_CTB_CONTROL_DISABLE), 315 }; 316 int ret = xe_guc_mmio_send(ct_to_guc(ct), request, ARRAY_SIZE(request)); 317 318 return ret > 0 ? -EPROTO : ret; 319 } 320 321 static void xe_guc_ct_set_state(struct xe_guc_ct *ct, 322 enum xe_guc_ct_state state) 323 { 324 mutex_lock(&ct->lock); /* Serialise dequeue_one_g2h() */ 325 spin_lock_irq(&ct->fast_lock); /* Serialise CT fast-path */ 326 327 xe_gt_assert(ct_to_gt(ct), ct->g2h_outstanding == 0 || 328 state == XE_GUC_CT_STATE_STOPPED); 329 330 ct->g2h_outstanding = 0; 331 ct->state = state; 332 333 spin_unlock_irq(&ct->fast_lock); 334 335 /* 336 * Lockdep doesn't like this under the fast lock and he destroy only 337 * needs to be serialized with the send path which ct lock provides. 338 */ 339 xa_destroy(&ct->fence_lookup); 340 341 mutex_unlock(&ct->lock); 342 } 343 344 static bool ct_needs_safe_mode(struct xe_guc_ct *ct) 345 { 346 return !pci_dev_msi_enabled(to_pci_dev(ct_to_xe(ct)->drm.dev)); 347 } 348 349 static bool ct_restart_safe_mode_worker(struct xe_guc_ct *ct) 350 { 351 if (!ct_needs_safe_mode(ct)) 352 return false; 353 354 queue_delayed_work(ct->g2h_wq, &ct->safe_mode_worker, HZ / 10); 355 return true; 356 } 357 358 static void safe_mode_worker_func(struct work_struct *w) 359 { 360 struct xe_guc_ct *ct = container_of(w, struct xe_guc_ct, safe_mode_worker.work); 361 362 receive_g2h(ct); 363 364 if (!ct_restart_safe_mode_worker(ct)) 365 xe_gt_dbg(ct_to_gt(ct), "GuC CT safe-mode canceled\n"); 366 } 367 368 static void ct_enter_safe_mode(struct xe_guc_ct *ct) 369 { 370 if (ct_restart_safe_mode_worker(ct)) 371 xe_gt_dbg(ct_to_gt(ct), "GuC CT safe-mode enabled\n"); 372 } 373 374 static void ct_exit_safe_mode(struct xe_guc_ct *ct) 375 { 376 if (cancel_delayed_work_sync(&ct->safe_mode_worker)) 377 xe_gt_dbg(ct_to_gt(ct), "GuC CT safe-mode disabled\n"); 378 } 379 380 int xe_guc_ct_enable(struct xe_guc_ct *ct) 381 { 382 struct xe_device *xe = ct_to_xe(ct); 383 struct xe_gt *gt = ct_to_gt(ct); 384 int err; 385 386 xe_gt_assert(gt, !xe_guc_ct_enabled(ct)); 387 388 guc_ct_ctb_h2g_init(xe, &ct->ctbs.h2g, &ct->bo->vmap); 389 guc_ct_ctb_g2h_init(xe, &ct->ctbs.g2h, &ct->bo->vmap); 390 391 err = guc_ct_ctb_h2g_register(ct); 392 if (err) 393 goto err_out; 394 395 err = guc_ct_ctb_g2h_register(ct); 396 if (err) 397 goto err_out; 398 399 err = guc_ct_control_toggle(ct, true); 400 if (err) 401 goto err_out; 402 403 xe_guc_ct_set_state(ct, XE_GUC_CT_STATE_ENABLED); 404 405 smp_mb(); 406 wake_up_all(&ct->wq); 407 xe_gt_dbg(gt, "GuC CT communication channel enabled\n"); 408 409 if (ct_needs_safe_mode(ct)) 410 ct_enter_safe_mode(ct); 411 412 return 0; 413 414 err_out: 415 xe_gt_err(gt, "Failed to enable GuC CT (%pe)\n", ERR_PTR(err)); 416 417 return err; 418 } 419 420 static void stop_g2h_handler(struct xe_guc_ct *ct) 421 { 422 cancel_work_sync(&ct->g2h_worker); 423 } 424 425 /** 426 * xe_guc_ct_disable - Set GuC to disabled state 427 * @ct: the &xe_guc_ct 428 * 429 * Set GuC CT to disabled state and stop g2h handler. No outstanding g2h expected 430 * in this transition. 431 */ 432 void xe_guc_ct_disable(struct xe_guc_ct *ct) 433 { 434 xe_guc_ct_set_state(ct, XE_GUC_CT_STATE_DISABLED); 435 ct_exit_safe_mode(ct); 436 stop_g2h_handler(ct); 437 } 438 439 /** 440 * xe_guc_ct_stop - Set GuC to stopped state 441 * @ct: the &xe_guc_ct 442 * 443 * Set GuC CT to stopped state, stop g2h handler, and clear any outstanding g2h 444 */ 445 void xe_guc_ct_stop(struct xe_guc_ct *ct) 446 { 447 xe_guc_ct_set_state(ct, XE_GUC_CT_STATE_STOPPED); 448 stop_g2h_handler(ct); 449 } 450 451 static bool h2g_has_room(struct xe_guc_ct *ct, u32 cmd_len) 452 { 453 struct guc_ctb *h2g = &ct->ctbs.h2g; 454 455 lockdep_assert_held(&ct->lock); 456 457 if (cmd_len > h2g->info.space) { 458 h2g->info.head = desc_read(ct_to_xe(ct), h2g, head); 459 h2g->info.space = CIRC_SPACE(h2g->info.tail, h2g->info.head, 460 h2g->info.size) - 461 h2g->info.resv_space; 462 if (cmd_len > h2g->info.space) 463 return false; 464 } 465 466 return true; 467 } 468 469 static bool g2h_has_room(struct xe_guc_ct *ct, u32 g2h_len) 470 { 471 if (!g2h_len) 472 return true; 473 474 lockdep_assert_held(&ct->fast_lock); 475 476 return ct->ctbs.g2h.info.space > g2h_len; 477 } 478 479 static int has_room(struct xe_guc_ct *ct, u32 cmd_len, u32 g2h_len) 480 { 481 lockdep_assert_held(&ct->lock); 482 483 if (!g2h_has_room(ct, g2h_len) || !h2g_has_room(ct, cmd_len)) 484 return -EBUSY; 485 486 return 0; 487 } 488 489 static void h2g_reserve_space(struct xe_guc_ct *ct, u32 cmd_len) 490 { 491 lockdep_assert_held(&ct->lock); 492 ct->ctbs.h2g.info.space -= cmd_len; 493 } 494 495 static void __g2h_reserve_space(struct xe_guc_ct *ct, u32 g2h_len, u32 num_g2h) 496 { 497 xe_gt_assert(ct_to_gt(ct), g2h_len <= ct->ctbs.g2h.info.space); 498 499 if (g2h_len) { 500 lockdep_assert_held(&ct->fast_lock); 501 502 ct->ctbs.g2h.info.space -= g2h_len; 503 ct->g2h_outstanding += num_g2h; 504 } 505 } 506 507 static void __g2h_release_space(struct xe_guc_ct *ct, u32 g2h_len) 508 { 509 lockdep_assert_held(&ct->fast_lock); 510 xe_gt_assert(ct_to_gt(ct), ct->ctbs.g2h.info.space + g2h_len <= 511 ct->ctbs.g2h.info.size - ct->ctbs.g2h.info.resv_space); 512 513 ct->ctbs.g2h.info.space += g2h_len; 514 --ct->g2h_outstanding; 515 } 516 517 static void g2h_release_space(struct xe_guc_ct *ct, u32 g2h_len) 518 { 519 spin_lock_irq(&ct->fast_lock); 520 __g2h_release_space(ct, g2h_len); 521 spin_unlock_irq(&ct->fast_lock); 522 } 523 524 #define H2G_CT_HEADERS (GUC_CTB_HDR_LEN + 1) /* one DW CTB header and one DW HxG header */ 525 526 static int h2g_write(struct xe_guc_ct *ct, const u32 *action, u32 len, 527 u32 ct_fence_value, bool want_response) 528 { 529 struct xe_device *xe = ct_to_xe(ct); 530 struct xe_gt *gt = ct_to_gt(ct); 531 struct guc_ctb *h2g = &ct->ctbs.h2g; 532 u32 cmd[H2G_CT_HEADERS]; 533 u32 tail = h2g->info.tail; 534 u32 full_len; 535 struct iosys_map map = IOSYS_MAP_INIT_OFFSET(&h2g->cmds, 536 tail * sizeof(u32)); 537 538 full_len = len + GUC_CTB_HDR_LEN; 539 540 lockdep_assert_held(&ct->lock); 541 xe_gt_assert(gt, full_len <= GUC_CTB_MSG_MAX_LEN); 542 xe_gt_assert(gt, tail <= h2g->info.size); 543 544 /* Command will wrap, zero fill (NOPs), return and check credits again */ 545 if (tail + full_len > h2g->info.size) { 546 xe_map_memset(xe, &map, 0, 0, 547 (h2g->info.size - tail) * sizeof(u32)); 548 h2g_reserve_space(ct, (h2g->info.size - tail)); 549 h2g->info.tail = 0; 550 desc_write(xe, h2g, tail, h2g->info.tail); 551 552 return -EAGAIN; 553 } 554 555 /* 556 * dw0: CT header (including fence) 557 * dw1: HXG header (including action code) 558 * dw2+: action data 559 */ 560 cmd[0] = FIELD_PREP(GUC_CTB_MSG_0_FORMAT, GUC_CTB_FORMAT_HXG) | 561 FIELD_PREP(GUC_CTB_MSG_0_NUM_DWORDS, len) | 562 FIELD_PREP(GUC_CTB_MSG_0_FENCE, ct_fence_value); 563 if (want_response) { 564 cmd[1] = 565 FIELD_PREP(GUC_HXG_MSG_0_TYPE, GUC_HXG_TYPE_REQUEST) | 566 FIELD_PREP(GUC_HXG_EVENT_MSG_0_ACTION | 567 GUC_HXG_EVENT_MSG_0_DATA0, action[0]); 568 } else { 569 cmd[1] = 570 FIELD_PREP(GUC_HXG_MSG_0_TYPE, GUC_HXG_TYPE_FAST_REQUEST) | 571 FIELD_PREP(GUC_HXG_EVENT_MSG_0_ACTION | 572 GUC_HXG_EVENT_MSG_0_DATA0, action[0]); 573 } 574 575 /* H2G header in cmd[1] replaces action[0] so: */ 576 --len; 577 ++action; 578 579 /* Write H2G ensuring visable before descriptor update */ 580 xe_map_memcpy_to(xe, &map, 0, cmd, H2G_CT_HEADERS * sizeof(u32)); 581 xe_map_memcpy_to(xe, &map, H2G_CT_HEADERS * sizeof(u32), action, len * sizeof(u32)); 582 xe_device_wmb(xe); 583 584 /* Update local copies */ 585 h2g->info.tail = (tail + full_len) % h2g->info.size; 586 h2g_reserve_space(ct, full_len); 587 588 /* Update descriptor */ 589 desc_write(xe, h2g, tail, h2g->info.tail); 590 591 trace_xe_guc_ctb_h2g(xe, gt->info.id, *(action - 1), full_len, 592 desc_read(xe, h2g, head), h2g->info.tail); 593 594 return 0; 595 } 596 597 /* 598 * The CT protocol accepts a 16 bits fence. This field is fully owned by the 599 * driver, the GuC will just copy it to the reply message. Since we need to 600 * be able to distinguish between replies to REQUEST and FAST_REQUEST messages, 601 * we use one bit of the seqno as an indicator for that and a rolling counter 602 * for the remaining 15 bits. 603 */ 604 #define CT_SEQNO_MASK GENMASK(14, 0) 605 #define CT_SEQNO_UNTRACKED BIT(15) 606 static u16 next_ct_seqno(struct xe_guc_ct *ct, bool is_g2h_fence) 607 { 608 u32 seqno = ct->fence_seqno++ & CT_SEQNO_MASK; 609 610 if (!is_g2h_fence) 611 seqno |= CT_SEQNO_UNTRACKED; 612 613 return seqno; 614 } 615 616 static int __guc_ct_send_locked(struct xe_guc_ct *ct, const u32 *action, 617 u32 len, u32 g2h_len, u32 num_g2h, 618 struct g2h_fence *g2h_fence) 619 { 620 struct xe_gt *gt __maybe_unused = ct_to_gt(ct); 621 u16 seqno; 622 int ret; 623 624 xe_gt_assert(gt, ct->state != XE_GUC_CT_STATE_NOT_INITIALIZED); 625 xe_gt_assert(gt, !g2h_len || !g2h_fence); 626 xe_gt_assert(gt, !num_g2h || !g2h_fence); 627 xe_gt_assert(gt, !g2h_len || num_g2h); 628 xe_gt_assert(gt, g2h_len || !num_g2h); 629 lockdep_assert_held(&ct->lock); 630 631 if (unlikely(ct->ctbs.h2g.info.broken)) { 632 ret = -EPIPE; 633 goto out; 634 } 635 636 if (ct->state == XE_GUC_CT_STATE_DISABLED) { 637 ret = -ENODEV; 638 goto out; 639 } 640 641 if (ct->state == XE_GUC_CT_STATE_STOPPED) { 642 ret = -ECANCELED; 643 goto out; 644 } 645 646 xe_gt_assert(gt, xe_guc_ct_enabled(ct)); 647 648 if (g2h_fence) { 649 g2h_len = GUC_CTB_HXG_MSG_MAX_LEN; 650 num_g2h = 1; 651 652 if (g2h_fence_needs_alloc(g2h_fence)) { 653 void *ptr; 654 655 g2h_fence->seqno = next_ct_seqno(ct, true); 656 ptr = xa_store(&ct->fence_lookup, 657 g2h_fence->seqno, 658 g2h_fence, GFP_ATOMIC); 659 if (IS_ERR(ptr)) { 660 ret = PTR_ERR(ptr); 661 goto out; 662 } 663 } 664 665 seqno = g2h_fence->seqno; 666 } else { 667 seqno = next_ct_seqno(ct, false); 668 } 669 670 if (g2h_len) 671 spin_lock_irq(&ct->fast_lock); 672 retry: 673 ret = has_room(ct, len + GUC_CTB_HDR_LEN, g2h_len); 674 if (unlikely(ret)) 675 goto out_unlock; 676 677 ret = h2g_write(ct, action, len, seqno, !!g2h_fence); 678 if (unlikely(ret)) { 679 if (ret == -EAGAIN) 680 goto retry; 681 goto out_unlock; 682 } 683 684 __g2h_reserve_space(ct, g2h_len, num_g2h); 685 xe_guc_notify(ct_to_guc(ct)); 686 out_unlock: 687 if (g2h_len) 688 spin_unlock_irq(&ct->fast_lock); 689 out: 690 return ret; 691 } 692 693 static void kick_reset(struct xe_guc_ct *ct) 694 { 695 xe_gt_reset_async(ct_to_gt(ct)); 696 } 697 698 static int dequeue_one_g2h(struct xe_guc_ct *ct); 699 700 static int guc_ct_send_locked(struct xe_guc_ct *ct, const u32 *action, u32 len, 701 u32 g2h_len, u32 num_g2h, 702 struct g2h_fence *g2h_fence) 703 { 704 struct xe_device *xe = ct_to_xe(ct); 705 struct xe_gt *gt = ct_to_gt(ct); 706 struct drm_printer p = xe_gt_info_printer(gt); 707 unsigned int sleep_period_ms = 1; 708 int ret; 709 710 xe_gt_assert(gt, !g2h_len || !g2h_fence); 711 lockdep_assert_held(&ct->lock); 712 xe_device_assert_mem_access(ct_to_xe(ct)); 713 714 try_again: 715 ret = __guc_ct_send_locked(ct, action, len, g2h_len, num_g2h, 716 g2h_fence); 717 718 /* 719 * We wait to try to restore credits for about 1 second before bailing. 720 * In the case of H2G credits we have no choice but just to wait for the 721 * GuC to consume H2Gs in the channel so we use a wait / sleep loop. In 722 * the case of G2H we process any G2H in the channel, hopefully freeing 723 * credits as we consume the G2H messages. 724 */ 725 if (unlikely(ret == -EBUSY && 726 !h2g_has_room(ct, len + GUC_CTB_HDR_LEN))) { 727 struct guc_ctb *h2g = &ct->ctbs.h2g; 728 729 if (sleep_period_ms == 1024) 730 goto broken; 731 732 trace_xe_guc_ct_h2g_flow_control(xe, h2g->info.head, h2g->info.tail, 733 h2g->info.size, 734 h2g->info.space, 735 len + GUC_CTB_HDR_LEN); 736 msleep(sleep_period_ms); 737 sleep_period_ms <<= 1; 738 739 goto try_again; 740 } else if (unlikely(ret == -EBUSY)) { 741 struct xe_device *xe = ct_to_xe(ct); 742 struct guc_ctb *g2h = &ct->ctbs.g2h; 743 744 trace_xe_guc_ct_g2h_flow_control(xe, g2h->info.head, 745 desc_read(xe, g2h, tail), 746 g2h->info.size, 747 g2h->info.space, 748 g2h_fence ? 749 GUC_CTB_HXG_MSG_MAX_LEN : 750 g2h_len); 751 752 #define g2h_avail(ct) \ 753 (desc_read(ct_to_xe(ct), (&ct->ctbs.g2h), tail) != ct->ctbs.g2h.info.head) 754 if (!wait_event_timeout(ct->wq, !ct->g2h_outstanding || 755 g2h_avail(ct), HZ)) 756 goto broken; 757 #undef g2h_avail 758 759 if (dequeue_one_g2h(ct) < 0) 760 goto broken; 761 762 goto try_again; 763 } 764 765 return ret; 766 767 broken: 768 xe_gt_err(gt, "No forward process on H2G, reset required\n"); 769 xe_guc_ct_print(ct, &p, true); 770 ct->ctbs.h2g.info.broken = true; 771 772 return -EDEADLK; 773 } 774 775 static int guc_ct_send(struct xe_guc_ct *ct, const u32 *action, u32 len, 776 u32 g2h_len, u32 num_g2h, struct g2h_fence *g2h_fence) 777 { 778 int ret; 779 780 xe_gt_assert(ct_to_gt(ct), !g2h_len || !g2h_fence); 781 782 mutex_lock(&ct->lock); 783 ret = guc_ct_send_locked(ct, action, len, g2h_len, num_g2h, g2h_fence); 784 mutex_unlock(&ct->lock); 785 786 return ret; 787 } 788 789 int xe_guc_ct_send(struct xe_guc_ct *ct, const u32 *action, u32 len, 790 u32 g2h_len, u32 num_g2h) 791 { 792 int ret; 793 794 ret = guc_ct_send(ct, action, len, g2h_len, num_g2h, NULL); 795 if (ret == -EDEADLK) 796 kick_reset(ct); 797 798 return ret; 799 } 800 801 int xe_guc_ct_send_locked(struct xe_guc_ct *ct, const u32 *action, u32 len, 802 u32 g2h_len, u32 num_g2h) 803 { 804 int ret; 805 806 ret = guc_ct_send_locked(ct, action, len, g2h_len, num_g2h, NULL); 807 if (ret == -EDEADLK) 808 kick_reset(ct); 809 810 return ret; 811 } 812 813 int xe_guc_ct_send_g2h_handler(struct xe_guc_ct *ct, const u32 *action, u32 len) 814 { 815 int ret; 816 817 lockdep_assert_held(&ct->lock); 818 819 ret = guc_ct_send_locked(ct, action, len, 0, 0, NULL); 820 if (ret == -EDEADLK) 821 kick_reset(ct); 822 823 return ret; 824 } 825 826 /* 827 * Check if a GT reset is in progress or will occur and if GT reset brought the 828 * CT back up. Randomly picking 5 seconds for an upper limit to do a GT a reset. 829 */ 830 static bool retry_failure(struct xe_guc_ct *ct, int ret) 831 { 832 if (!(ret == -EDEADLK || ret == -EPIPE || ret == -ENODEV)) 833 return false; 834 835 #define ct_alive(ct) \ 836 (xe_guc_ct_enabled(ct) && !ct->ctbs.h2g.info.broken && \ 837 !ct->ctbs.g2h.info.broken) 838 if (!wait_event_interruptible_timeout(ct->wq, ct_alive(ct), HZ * 5)) 839 return false; 840 #undef ct_alive 841 842 return true; 843 } 844 845 static int guc_ct_send_recv(struct xe_guc_ct *ct, const u32 *action, u32 len, 846 u32 *response_buffer, bool no_fail) 847 { 848 struct xe_gt *gt = ct_to_gt(ct); 849 struct g2h_fence g2h_fence; 850 int ret = 0; 851 852 /* 853 * We use a fence to implement blocking sends / receiving response data. 854 * The seqno of the fence is sent in the H2G, returned in the G2H, and 855 * an xarray is used as storage media with the seqno being to key. 856 * Fields in the fence hold success, failure, retry status and the 857 * response data. Safe to allocate on the stack as the xarray is the 858 * only reference and it cannot be present after this function exits. 859 */ 860 retry: 861 g2h_fence_init(&g2h_fence, response_buffer); 862 retry_same_fence: 863 ret = guc_ct_send(ct, action, len, 0, 0, &g2h_fence); 864 if (unlikely(ret == -ENOMEM)) { 865 void *ptr; 866 867 /* Retry allocation /w GFP_KERNEL */ 868 ptr = xa_store(&ct->fence_lookup, 869 g2h_fence.seqno, 870 &g2h_fence, GFP_KERNEL); 871 if (IS_ERR(ptr)) 872 return PTR_ERR(ptr); 873 874 goto retry_same_fence; 875 } else if (unlikely(ret)) { 876 if (ret == -EDEADLK) 877 kick_reset(ct); 878 879 if (no_fail && retry_failure(ct, ret)) 880 goto retry_same_fence; 881 882 if (!g2h_fence_needs_alloc(&g2h_fence)) 883 xa_erase_irq(&ct->fence_lookup, g2h_fence.seqno); 884 885 return ret; 886 } 887 888 ret = wait_event_timeout(ct->g2h_fence_wq, g2h_fence.done, HZ); 889 if (!ret) { 890 xe_gt_err(gt, "Timed out wait for G2H, fence %u, action %04x", 891 g2h_fence.seqno, action[0]); 892 xa_erase_irq(&ct->fence_lookup, g2h_fence.seqno); 893 return -ETIME; 894 } 895 896 if (g2h_fence.retry) { 897 xe_gt_dbg(gt, "H2G action %#x retrying: reason %#x\n", 898 action[0], g2h_fence.reason); 899 goto retry; 900 } 901 if (g2h_fence.fail) { 902 xe_gt_err(gt, "H2G request %#x failed: error %#x hint %#x\n", 903 action[0], g2h_fence.error, g2h_fence.hint); 904 ret = -EIO; 905 } 906 907 return ret > 0 ? response_buffer ? g2h_fence.response_len : g2h_fence.response_data : ret; 908 } 909 910 /** 911 * xe_guc_ct_send_recv - Send and receive HXG to the GuC 912 * @ct: the &xe_guc_ct 913 * @action: the dword array with `HXG Request`_ message (can't be NULL) 914 * @len: length of the `HXG Request`_ message (in dwords, can't be 0) 915 * @response_buffer: placeholder for the `HXG Response`_ message (can be NULL) 916 * 917 * Send a `HXG Request`_ message to the GuC over CT communication channel and 918 * blocks until GuC replies with a `HXG Response`_ message. 919 * 920 * For non-blocking communication with GuC use xe_guc_ct_send(). 921 * 922 * Note: The size of &response_buffer must be at least GUC_CTB_MAX_DWORDS_. 923 * 924 * Return: response length (in dwords) if &response_buffer was not NULL, or 925 * DATA0 from `HXG Response`_ if &response_buffer was NULL, or 926 * a negative error code on failure. 927 */ 928 int xe_guc_ct_send_recv(struct xe_guc_ct *ct, const u32 *action, u32 len, 929 u32 *response_buffer) 930 { 931 KUNIT_STATIC_STUB_REDIRECT(xe_guc_ct_send_recv, ct, action, len, response_buffer); 932 return guc_ct_send_recv(ct, action, len, response_buffer, false); 933 } 934 935 int xe_guc_ct_send_recv_no_fail(struct xe_guc_ct *ct, const u32 *action, 936 u32 len, u32 *response_buffer) 937 { 938 return guc_ct_send_recv(ct, action, len, response_buffer, true); 939 } 940 941 static u32 *msg_to_hxg(u32 *msg) 942 { 943 return msg + GUC_CTB_MSG_MIN_LEN; 944 } 945 946 static u32 msg_len_to_hxg_len(u32 len) 947 { 948 return len - GUC_CTB_MSG_MIN_LEN; 949 } 950 951 static int parse_g2h_event(struct xe_guc_ct *ct, u32 *msg, u32 len) 952 { 953 u32 *hxg = msg_to_hxg(msg); 954 u32 action = FIELD_GET(GUC_HXG_EVENT_MSG_0_ACTION, hxg[0]); 955 956 lockdep_assert_held(&ct->lock); 957 958 switch (action) { 959 case XE_GUC_ACTION_SCHED_CONTEXT_MODE_DONE: 960 case XE_GUC_ACTION_DEREGISTER_CONTEXT_DONE: 961 case XE_GUC_ACTION_SCHED_ENGINE_MODE_DONE: 962 case XE_GUC_ACTION_TLB_INVALIDATION_DONE: 963 g2h_release_space(ct, len); 964 } 965 966 return 0; 967 } 968 969 static int parse_g2h_response(struct xe_guc_ct *ct, u32 *msg, u32 len) 970 { 971 struct xe_gt *gt = ct_to_gt(ct); 972 u32 *hxg = msg_to_hxg(msg); 973 u32 hxg_len = msg_len_to_hxg_len(len); 974 u32 fence = FIELD_GET(GUC_CTB_MSG_0_FENCE, msg[0]); 975 u32 type = FIELD_GET(GUC_HXG_MSG_0_TYPE, hxg[0]); 976 struct g2h_fence *g2h_fence; 977 978 lockdep_assert_held(&ct->lock); 979 980 /* 981 * Fences for FAST_REQUEST messages are not tracked in ct->fence_lookup. 982 * Those messages should never fail, so if we do get an error back it 983 * means we're likely doing an illegal operation and the GuC is 984 * rejecting it. We have no way to inform the code that submitted the 985 * H2G that the message was rejected, so we need to escalate the 986 * failure to trigger a reset. 987 */ 988 if (fence & CT_SEQNO_UNTRACKED) { 989 if (type == GUC_HXG_TYPE_RESPONSE_FAILURE) 990 xe_gt_err(gt, "FAST_REQ H2G fence 0x%x failed! e=0x%x, h=%u\n", 991 fence, 992 FIELD_GET(GUC_HXG_FAILURE_MSG_0_ERROR, hxg[0]), 993 FIELD_GET(GUC_HXG_FAILURE_MSG_0_HINT, hxg[0])); 994 else 995 xe_gt_err(gt, "unexpected response %u for FAST_REQ H2G fence 0x%x!\n", 996 type, fence); 997 998 return -EPROTO; 999 } 1000 1001 g2h_fence = xa_erase(&ct->fence_lookup, fence); 1002 if (unlikely(!g2h_fence)) { 1003 /* Don't tear down channel, as send could've timed out */ 1004 xe_gt_warn(gt, "G2H fence (%u) not found!\n", fence); 1005 g2h_release_space(ct, GUC_CTB_HXG_MSG_MAX_LEN); 1006 return 0; 1007 } 1008 1009 xe_gt_assert(gt, fence == g2h_fence->seqno); 1010 1011 if (type == GUC_HXG_TYPE_RESPONSE_FAILURE) { 1012 g2h_fence->fail = true; 1013 g2h_fence->error = FIELD_GET(GUC_HXG_FAILURE_MSG_0_ERROR, hxg[0]); 1014 g2h_fence->hint = FIELD_GET(GUC_HXG_FAILURE_MSG_0_HINT, hxg[0]); 1015 } else if (type == GUC_HXG_TYPE_NO_RESPONSE_RETRY) { 1016 g2h_fence->retry = true; 1017 g2h_fence->reason = FIELD_GET(GUC_HXG_RETRY_MSG_0_REASON, hxg[0]); 1018 } else if (g2h_fence->response_buffer) { 1019 g2h_fence->response_len = hxg_len; 1020 memcpy(g2h_fence->response_buffer, hxg, hxg_len * sizeof(u32)); 1021 } else { 1022 g2h_fence->response_data = FIELD_GET(GUC_HXG_RESPONSE_MSG_0_DATA0, hxg[0]); 1023 } 1024 1025 g2h_release_space(ct, GUC_CTB_HXG_MSG_MAX_LEN); 1026 1027 g2h_fence->done = true; 1028 smp_mb(); 1029 1030 wake_up_all(&ct->g2h_fence_wq); 1031 1032 return 0; 1033 } 1034 1035 static int parse_g2h_msg(struct xe_guc_ct *ct, u32 *msg, u32 len) 1036 { 1037 struct xe_gt *gt = ct_to_gt(ct); 1038 u32 *hxg = msg_to_hxg(msg); 1039 u32 origin, type; 1040 int ret; 1041 1042 lockdep_assert_held(&ct->lock); 1043 1044 origin = FIELD_GET(GUC_HXG_MSG_0_ORIGIN, hxg[0]); 1045 if (unlikely(origin != GUC_HXG_ORIGIN_GUC)) { 1046 xe_gt_err(gt, "G2H channel broken on read, origin=%u, reset required\n", 1047 origin); 1048 ct->ctbs.g2h.info.broken = true; 1049 1050 return -EPROTO; 1051 } 1052 1053 type = FIELD_GET(GUC_HXG_MSG_0_TYPE, hxg[0]); 1054 switch (type) { 1055 case GUC_HXG_TYPE_EVENT: 1056 ret = parse_g2h_event(ct, msg, len); 1057 break; 1058 case GUC_HXG_TYPE_RESPONSE_SUCCESS: 1059 case GUC_HXG_TYPE_RESPONSE_FAILURE: 1060 case GUC_HXG_TYPE_NO_RESPONSE_RETRY: 1061 ret = parse_g2h_response(ct, msg, len); 1062 break; 1063 default: 1064 xe_gt_err(gt, "G2H channel broken on read, type=%u, reset required\n", 1065 type); 1066 ct->ctbs.g2h.info.broken = true; 1067 1068 ret = -EOPNOTSUPP; 1069 } 1070 1071 return ret; 1072 } 1073 1074 static int process_g2h_msg(struct xe_guc_ct *ct, u32 *msg, u32 len) 1075 { 1076 struct xe_guc *guc = ct_to_guc(ct); 1077 struct xe_gt *gt = ct_to_gt(ct); 1078 u32 hxg_len = msg_len_to_hxg_len(len); 1079 u32 *hxg = msg_to_hxg(msg); 1080 u32 action, adj_len; 1081 u32 *payload; 1082 int ret = 0; 1083 1084 if (FIELD_GET(GUC_HXG_MSG_0_TYPE, hxg[0]) != GUC_HXG_TYPE_EVENT) 1085 return 0; 1086 1087 action = FIELD_GET(GUC_HXG_EVENT_MSG_0_ACTION, hxg[0]); 1088 payload = hxg + GUC_HXG_EVENT_MSG_MIN_LEN; 1089 adj_len = hxg_len - GUC_HXG_EVENT_MSG_MIN_LEN; 1090 1091 switch (action) { 1092 case XE_GUC_ACTION_SCHED_CONTEXT_MODE_DONE: 1093 ret = xe_guc_sched_done_handler(guc, payload, adj_len); 1094 break; 1095 case XE_GUC_ACTION_DEREGISTER_CONTEXT_DONE: 1096 ret = xe_guc_deregister_done_handler(guc, payload, adj_len); 1097 break; 1098 case XE_GUC_ACTION_CONTEXT_RESET_NOTIFICATION: 1099 ret = xe_guc_exec_queue_reset_handler(guc, payload, adj_len); 1100 break; 1101 case XE_GUC_ACTION_ENGINE_FAILURE_NOTIFICATION: 1102 ret = xe_guc_exec_queue_reset_failure_handler(guc, payload, 1103 adj_len); 1104 break; 1105 case XE_GUC_ACTION_SCHED_ENGINE_MODE_DONE: 1106 /* Selftest only at the moment */ 1107 break; 1108 case XE_GUC_ACTION_STATE_CAPTURE_NOTIFICATION: 1109 case XE_GUC_ACTION_NOTIFY_FLUSH_LOG_BUFFER_TO_FILE: 1110 /* FIXME: Handle this */ 1111 break; 1112 case XE_GUC_ACTION_NOTIFY_MEMORY_CAT_ERROR: 1113 ret = xe_guc_exec_queue_memory_cat_error_handler(guc, payload, 1114 adj_len); 1115 break; 1116 case XE_GUC_ACTION_REPORT_PAGE_FAULT_REQ_DESC: 1117 ret = xe_guc_pagefault_handler(guc, payload, adj_len); 1118 break; 1119 case XE_GUC_ACTION_TLB_INVALIDATION_DONE: 1120 ret = xe_guc_tlb_invalidation_done_handler(guc, payload, 1121 adj_len); 1122 break; 1123 case XE_GUC_ACTION_ACCESS_COUNTER_NOTIFY: 1124 ret = xe_guc_access_counter_notify_handler(guc, payload, 1125 adj_len); 1126 break; 1127 case XE_GUC_ACTION_GUC2PF_RELAY_FROM_VF: 1128 ret = xe_guc_relay_process_guc2pf(&guc->relay, hxg, hxg_len); 1129 break; 1130 case XE_GUC_ACTION_GUC2VF_RELAY_FROM_PF: 1131 ret = xe_guc_relay_process_guc2vf(&guc->relay, hxg, hxg_len); 1132 break; 1133 case GUC_ACTION_GUC2PF_VF_STATE_NOTIFY: 1134 ret = xe_gt_sriov_pf_control_process_guc2pf(gt, hxg, hxg_len); 1135 break; 1136 case GUC_ACTION_GUC2PF_ADVERSE_EVENT: 1137 ret = xe_gt_sriov_pf_monitor_process_guc2pf(gt, hxg, hxg_len); 1138 break; 1139 default: 1140 xe_gt_err(gt, "unexpected G2H action 0x%04x\n", action); 1141 } 1142 1143 if (ret) 1144 xe_gt_err(gt, "G2H action 0x%04x failed (%pe)\n", 1145 action, ERR_PTR(ret)); 1146 1147 return 0; 1148 } 1149 1150 static int g2h_read(struct xe_guc_ct *ct, u32 *msg, bool fast_path) 1151 { 1152 struct xe_device *xe = ct_to_xe(ct); 1153 struct xe_gt *gt = ct_to_gt(ct); 1154 struct guc_ctb *g2h = &ct->ctbs.g2h; 1155 u32 tail, head, len; 1156 s32 avail; 1157 u32 action; 1158 u32 *hxg; 1159 1160 xe_gt_assert(gt, ct->state != XE_GUC_CT_STATE_NOT_INITIALIZED); 1161 lockdep_assert_held(&ct->fast_lock); 1162 1163 if (ct->state == XE_GUC_CT_STATE_DISABLED) 1164 return -ENODEV; 1165 1166 if (ct->state == XE_GUC_CT_STATE_STOPPED) 1167 return -ECANCELED; 1168 1169 if (g2h->info.broken) 1170 return -EPIPE; 1171 1172 xe_gt_assert(gt, xe_guc_ct_enabled(ct)); 1173 1174 /* Calculate DW available to read */ 1175 tail = desc_read(xe, g2h, tail); 1176 avail = tail - g2h->info.head; 1177 if (unlikely(avail == 0)) 1178 return 0; 1179 1180 if (avail < 0) 1181 avail += g2h->info.size; 1182 1183 /* Read header */ 1184 xe_map_memcpy_from(xe, msg, &g2h->cmds, sizeof(u32) * g2h->info.head, 1185 sizeof(u32)); 1186 len = FIELD_GET(GUC_CTB_MSG_0_NUM_DWORDS, msg[0]) + GUC_CTB_MSG_MIN_LEN; 1187 if (len > avail) { 1188 xe_gt_err(gt, "G2H channel broken on read, avail=%d, len=%d, reset required\n", 1189 avail, len); 1190 g2h->info.broken = true; 1191 1192 return -EPROTO; 1193 } 1194 1195 head = (g2h->info.head + 1) % g2h->info.size; 1196 avail = len - 1; 1197 1198 /* Read G2H message */ 1199 if (avail + head > g2h->info.size) { 1200 u32 avail_til_wrap = g2h->info.size - head; 1201 1202 xe_map_memcpy_from(xe, msg + 1, 1203 &g2h->cmds, sizeof(u32) * head, 1204 avail_til_wrap * sizeof(u32)); 1205 xe_map_memcpy_from(xe, msg + 1 + avail_til_wrap, 1206 &g2h->cmds, 0, 1207 (avail - avail_til_wrap) * sizeof(u32)); 1208 } else { 1209 xe_map_memcpy_from(xe, msg + 1, 1210 &g2h->cmds, sizeof(u32) * head, 1211 avail * sizeof(u32)); 1212 } 1213 1214 hxg = msg_to_hxg(msg); 1215 action = FIELD_GET(GUC_HXG_EVENT_MSG_0_ACTION, hxg[0]); 1216 1217 if (fast_path) { 1218 if (FIELD_GET(GUC_HXG_MSG_0_TYPE, hxg[0]) != GUC_HXG_TYPE_EVENT) 1219 return 0; 1220 1221 switch (action) { 1222 case XE_GUC_ACTION_REPORT_PAGE_FAULT_REQ_DESC: 1223 case XE_GUC_ACTION_TLB_INVALIDATION_DONE: 1224 break; /* Process these in fast-path */ 1225 default: 1226 return 0; 1227 } 1228 } 1229 1230 /* Update local / descriptor header */ 1231 g2h->info.head = (head + avail) % g2h->info.size; 1232 desc_write(xe, g2h, head, g2h->info.head); 1233 1234 trace_xe_guc_ctb_g2h(xe, ct_to_gt(ct)->info.id, 1235 action, len, g2h->info.head, tail); 1236 1237 return len; 1238 } 1239 1240 static void g2h_fast_path(struct xe_guc_ct *ct, u32 *msg, u32 len) 1241 { 1242 struct xe_gt *gt = ct_to_gt(ct); 1243 struct xe_guc *guc = ct_to_guc(ct); 1244 u32 hxg_len = msg_len_to_hxg_len(len); 1245 u32 *hxg = msg_to_hxg(msg); 1246 u32 action = FIELD_GET(GUC_HXG_EVENT_MSG_0_ACTION, hxg[0]); 1247 u32 *payload = hxg + GUC_HXG_MSG_MIN_LEN; 1248 u32 adj_len = hxg_len - GUC_HXG_MSG_MIN_LEN; 1249 int ret = 0; 1250 1251 switch (action) { 1252 case XE_GUC_ACTION_REPORT_PAGE_FAULT_REQ_DESC: 1253 ret = xe_guc_pagefault_handler(guc, payload, adj_len); 1254 break; 1255 case XE_GUC_ACTION_TLB_INVALIDATION_DONE: 1256 __g2h_release_space(ct, len); 1257 ret = xe_guc_tlb_invalidation_done_handler(guc, payload, 1258 adj_len); 1259 break; 1260 default: 1261 xe_gt_warn(gt, "NOT_POSSIBLE"); 1262 } 1263 1264 if (ret) 1265 xe_gt_err(gt, "G2H action 0x%04x failed (%pe)\n", 1266 action, ERR_PTR(ret)); 1267 } 1268 1269 /** 1270 * xe_guc_ct_fast_path - process critical G2H in the IRQ handler 1271 * @ct: GuC CT object 1272 * 1273 * Anything related to page faults is critical for performance, process these 1274 * critical G2H in the IRQ. This is safe as these handlers either just wake up 1275 * waiters or queue another worker. 1276 */ 1277 void xe_guc_ct_fast_path(struct xe_guc_ct *ct) 1278 { 1279 struct xe_device *xe = ct_to_xe(ct); 1280 bool ongoing; 1281 int len; 1282 1283 ongoing = xe_pm_runtime_get_if_active(ct_to_xe(ct)); 1284 if (!ongoing && xe_pm_read_callback_task(ct_to_xe(ct)) == NULL) 1285 return; 1286 1287 spin_lock(&ct->fast_lock); 1288 do { 1289 len = g2h_read(ct, ct->fast_msg, true); 1290 if (len > 0) 1291 g2h_fast_path(ct, ct->fast_msg, len); 1292 } while (len > 0); 1293 spin_unlock(&ct->fast_lock); 1294 1295 if (ongoing) 1296 xe_pm_runtime_put(xe); 1297 } 1298 1299 /* Returns less than zero on error, 0 on done, 1 on more available */ 1300 static int dequeue_one_g2h(struct xe_guc_ct *ct) 1301 { 1302 int len; 1303 int ret; 1304 1305 lockdep_assert_held(&ct->lock); 1306 1307 spin_lock_irq(&ct->fast_lock); 1308 len = g2h_read(ct, ct->msg, false); 1309 spin_unlock_irq(&ct->fast_lock); 1310 if (len <= 0) 1311 return len; 1312 1313 ret = parse_g2h_msg(ct, ct->msg, len); 1314 if (unlikely(ret < 0)) 1315 return ret; 1316 1317 ret = process_g2h_msg(ct, ct->msg, len); 1318 if (unlikely(ret < 0)) 1319 return ret; 1320 1321 return 1; 1322 } 1323 1324 static void receive_g2h(struct xe_guc_ct *ct) 1325 { 1326 struct xe_gt *gt = ct_to_gt(ct); 1327 bool ongoing; 1328 int ret; 1329 1330 /* 1331 * Normal users must always hold mem_access.ref around CT calls. However 1332 * during the runtime pm callbacks we rely on CT to talk to the GuC, but 1333 * at this stage we can't rely on mem_access.ref and even the 1334 * callback_task will be different than current. For such cases we just 1335 * need to ensure we always process the responses from any blocking 1336 * ct_send requests or where we otherwise expect some response when 1337 * initiated from those callbacks (which will need to wait for the below 1338 * dequeue_one_g2h()). The dequeue_one_g2h() will gracefully fail if 1339 * the device has suspended to the point that the CT communication has 1340 * been disabled. 1341 * 1342 * If we are inside the runtime pm callback, we can be the only task 1343 * still issuing CT requests (since that requires having the 1344 * mem_access.ref). It seems like it might in theory be possible to 1345 * receive unsolicited events from the GuC just as we are 1346 * suspending-resuming, but those will currently anyway be lost when 1347 * eventually exiting from suspend, hence no need to wake up the device 1348 * here. If we ever need something stronger than get_if_ongoing() then 1349 * we need to be careful with blocking the pm callbacks from getting CT 1350 * responses, if the worker here is blocked on those callbacks 1351 * completing, creating a deadlock. 1352 */ 1353 ongoing = xe_pm_runtime_get_if_active(ct_to_xe(ct)); 1354 if (!ongoing && xe_pm_read_callback_task(ct_to_xe(ct)) == NULL) 1355 return; 1356 1357 do { 1358 mutex_lock(&ct->lock); 1359 ret = dequeue_one_g2h(ct); 1360 mutex_unlock(&ct->lock); 1361 1362 if (unlikely(ret == -EPROTO || ret == -EOPNOTSUPP)) { 1363 struct drm_printer p = xe_gt_info_printer(gt); 1364 1365 xe_guc_ct_print(ct, &p, false); 1366 kick_reset(ct); 1367 } 1368 } while (ret == 1); 1369 1370 if (ongoing) 1371 xe_pm_runtime_put(ct_to_xe(ct)); 1372 } 1373 1374 static void g2h_worker_func(struct work_struct *w) 1375 { 1376 struct xe_guc_ct *ct = container_of(w, struct xe_guc_ct, g2h_worker); 1377 1378 receive_g2h(ct); 1379 } 1380 1381 static void guc_ctb_snapshot_capture(struct xe_device *xe, struct guc_ctb *ctb, 1382 struct guc_ctb_snapshot *snapshot, 1383 bool atomic) 1384 { 1385 u32 head, tail; 1386 1387 xe_map_memcpy_from(xe, &snapshot->desc, &ctb->desc, 0, 1388 sizeof(struct guc_ct_buffer_desc)); 1389 memcpy(&snapshot->info, &ctb->info, sizeof(struct guc_ctb_info)); 1390 1391 snapshot->cmds = kmalloc_array(ctb->info.size, sizeof(u32), 1392 atomic ? GFP_ATOMIC : GFP_KERNEL); 1393 1394 if (!snapshot->cmds) { 1395 drm_err(&xe->drm, "Skipping CTB commands snapshot. Only CTB info will be available.\n"); 1396 return; 1397 } 1398 1399 head = snapshot->desc.head; 1400 tail = snapshot->desc.tail; 1401 1402 if (head != tail) { 1403 struct iosys_map map = 1404 IOSYS_MAP_INIT_OFFSET(&ctb->cmds, head * sizeof(u32)); 1405 1406 while (head != tail) { 1407 snapshot->cmds[head] = xe_map_rd(xe, &map, 0, u32); 1408 ++head; 1409 if (head == ctb->info.size) { 1410 head = 0; 1411 map = ctb->cmds; 1412 } else { 1413 iosys_map_incr(&map, sizeof(u32)); 1414 } 1415 } 1416 } 1417 } 1418 1419 static void guc_ctb_snapshot_print(struct guc_ctb_snapshot *snapshot, 1420 struct drm_printer *p) 1421 { 1422 u32 head, tail; 1423 1424 drm_printf(p, "\tsize: %d\n", snapshot->info.size); 1425 drm_printf(p, "\tresv_space: %d\n", snapshot->info.resv_space); 1426 drm_printf(p, "\thead: %d\n", snapshot->info.head); 1427 drm_printf(p, "\ttail: %d\n", snapshot->info.tail); 1428 drm_printf(p, "\tspace: %d\n", snapshot->info.space); 1429 drm_printf(p, "\tbroken: %d\n", snapshot->info.broken); 1430 drm_printf(p, "\thead (memory): %d\n", snapshot->desc.head); 1431 drm_printf(p, "\ttail (memory): %d\n", snapshot->desc.tail); 1432 drm_printf(p, "\tstatus (memory): 0x%x\n", snapshot->desc.status); 1433 1434 if (!snapshot->cmds) 1435 return; 1436 1437 head = snapshot->desc.head; 1438 tail = snapshot->desc.tail; 1439 1440 while (head != tail) { 1441 drm_printf(p, "\tcmd[%d]: 0x%08x\n", head, 1442 snapshot->cmds[head]); 1443 ++head; 1444 if (head == snapshot->info.size) 1445 head = 0; 1446 } 1447 } 1448 1449 static void guc_ctb_snapshot_free(struct guc_ctb_snapshot *snapshot) 1450 { 1451 kfree(snapshot->cmds); 1452 } 1453 1454 /** 1455 * xe_guc_ct_snapshot_capture - Take a quick snapshot of the CT state. 1456 * @ct: GuC CT object. 1457 * @atomic: Boolean to indicate if this is called from atomic context like 1458 * reset or CTB handler or from some regular path like debugfs. 1459 * 1460 * This can be printed out in a later stage like during dev_coredump 1461 * analysis. 1462 * 1463 * Returns: a GuC CT snapshot object that must be freed by the caller 1464 * by using `xe_guc_ct_snapshot_free`. 1465 */ 1466 struct xe_guc_ct_snapshot *xe_guc_ct_snapshot_capture(struct xe_guc_ct *ct, 1467 bool atomic) 1468 { 1469 struct xe_device *xe = ct_to_xe(ct); 1470 struct xe_guc_ct_snapshot *snapshot; 1471 1472 snapshot = kzalloc(sizeof(*snapshot), 1473 atomic ? GFP_ATOMIC : GFP_KERNEL); 1474 1475 if (!snapshot) { 1476 drm_err(&xe->drm, "Skipping CTB snapshot entirely.\n"); 1477 return NULL; 1478 } 1479 1480 if (xe_guc_ct_enabled(ct) || ct->state == XE_GUC_CT_STATE_STOPPED) { 1481 snapshot->ct_enabled = true; 1482 snapshot->g2h_outstanding = READ_ONCE(ct->g2h_outstanding); 1483 guc_ctb_snapshot_capture(xe, &ct->ctbs.h2g, 1484 &snapshot->h2g, atomic); 1485 guc_ctb_snapshot_capture(xe, &ct->ctbs.g2h, 1486 &snapshot->g2h, atomic); 1487 } 1488 1489 return snapshot; 1490 } 1491 1492 /** 1493 * xe_guc_ct_snapshot_print - Print out a given GuC CT snapshot. 1494 * @snapshot: GuC CT snapshot object. 1495 * @p: drm_printer where it will be printed out. 1496 * 1497 * This function prints out a given GuC CT snapshot object. 1498 */ 1499 void xe_guc_ct_snapshot_print(struct xe_guc_ct_snapshot *snapshot, 1500 struct drm_printer *p) 1501 { 1502 if (!snapshot) 1503 return; 1504 1505 if (snapshot->ct_enabled) { 1506 drm_puts(p, "H2G CTB (all sizes in DW):\n"); 1507 guc_ctb_snapshot_print(&snapshot->h2g, p); 1508 1509 drm_puts(p, "\nG2H CTB (all sizes in DW):\n"); 1510 guc_ctb_snapshot_print(&snapshot->g2h, p); 1511 1512 drm_printf(p, "\tg2h outstanding: %d\n", 1513 snapshot->g2h_outstanding); 1514 } else { 1515 drm_puts(p, "CT disabled\n"); 1516 } 1517 } 1518 1519 /** 1520 * xe_guc_ct_snapshot_free - Free all allocated objects for a given snapshot. 1521 * @snapshot: GuC CT snapshot object. 1522 * 1523 * This function free all the memory that needed to be allocated at capture 1524 * time. 1525 */ 1526 void xe_guc_ct_snapshot_free(struct xe_guc_ct_snapshot *snapshot) 1527 { 1528 if (!snapshot) 1529 return; 1530 1531 guc_ctb_snapshot_free(&snapshot->h2g); 1532 guc_ctb_snapshot_free(&snapshot->g2h); 1533 kfree(snapshot); 1534 } 1535 1536 /** 1537 * xe_guc_ct_print - GuC CT Print. 1538 * @ct: GuC CT. 1539 * @p: drm_printer where it will be printed out. 1540 * @atomic: Boolean to indicate if this is called from atomic context like 1541 * reset or CTB handler or from some regular path like debugfs. 1542 * 1543 * This function quickly capture a snapshot and immediately print it out. 1544 */ 1545 void xe_guc_ct_print(struct xe_guc_ct *ct, struct drm_printer *p, bool atomic) 1546 { 1547 struct xe_guc_ct_snapshot *snapshot; 1548 1549 snapshot = xe_guc_ct_snapshot_capture(ct, atomic); 1550 xe_guc_ct_snapshot_print(snapshot, p); 1551 xe_guc_ct_snapshot_free(snapshot); 1552 } 1553