1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2021 Intel Corporation 4 */ 5 6 #include "xe_device.h" 7 8 #include <linux/delay.h> 9 #include <linux/units.h> 10 11 #include <drm/drm_aperture.h> 12 #include <drm/drm_atomic_helper.h> 13 #include <drm/drm_client.h> 14 #include <drm/drm_gem_ttm_helper.h> 15 #include <drm/drm_ioctl.h> 16 #include <drm/drm_managed.h> 17 #include <drm/drm_print.h> 18 #include <drm/xe_drm.h> 19 20 #include "display/xe_display.h" 21 #include "instructions/xe_gpu_commands.h" 22 #include "regs/xe_gt_regs.h" 23 #include "regs/xe_regs.h" 24 #include "xe_bo.h" 25 #include "xe_debugfs.h" 26 #include "xe_devcoredump.h" 27 #include "xe_dma_buf.h" 28 #include "xe_drm_client.h" 29 #include "xe_drv.h" 30 #include "xe_exec.h" 31 #include "xe_exec_queue.h" 32 #include "xe_force_wake.h" 33 #include "xe_ggtt.h" 34 #include "xe_gsc_proxy.h" 35 #include "xe_gt.h" 36 #include "xe_gt_mcr.h" 37 #include "xe_gt_printk.h" 38 #include "xe_gt_sriov_vf.h" 39 #include "xe_guc.h" 40 #include "xe_hwmon.h" 41 #include "xe_irq.h" 42 #include "xe_memirq.h" 43 #include "xe_mmio.h" 44 #include "xe_module.h" 45 #include "xe_pat.h" 46 #include "xe_pcode.h" 47 #include "xe_pm.h" 48 #include "xe_query.h" 49 #include "xe_sriov.h" 50 #include "xe_tile.h" 51 #include "xe_ttm_stolen_mgr.h" 52 #include "xe_ttm_sys_mgr.h" 53 #include "xe_vm.h" 54 #include "xe_vram.h" 55 #include "xe_wait_user_fence.h" 56 57 static int xe_file_open(struct drm_device *dev, struct drm_file *file) 58 { 59 struct xe_device *xe = to_xe_device(dev); 60 struct xe_drm_client *client; 61 struct xe_file *xef; 62 int ret = -ENOMEM; 63 64 xef = kzalloc(sizeof(*xef), GFP_KERNEL); 65 if (!xef) 66 return ret; 67 68 client = xe_drm_client_alloc(); 69 if (!client) { 70 kfree(xef); 71 return ret; 72 } 73 74 xef->drm = file; 75 xef->client = client; 76 xef->xe = xe; 77 78 mutex_init(&xef->vm.lock); 79 xa_init_flags(&xef->vm.xa, XA_FLAGS_ALLOC1); 80 81 mutex_init(&xef->exec_queue.lock); 82 xa_init_flags(&xef->exec_queue.xa, XA_FLAGS_ALLOC1); 83 84 spin_lock(&xe->clients.lock); 85 xe->clients.count++; 86 spin_unlock(&xe->clients.lock); 87 88 file->driver_priv = xef; 89 return 0; 90 } 91 92 static void xe_file_close(struct drm_device *dev, struct drm_file *file) 93 { 94 struct xe_device *xe = to_xe_device(dev); 95 struct xe_file *xef = file->driver_priv; 96 struct xe_vm *vm; 97 struct xe_exec_queue *q; 98 unsigned long idx; 99 100 /* 101 * No need for exec_queue.lock here as there is no contention for it 102 * when FD is closing as IOCTLs presumably can't be modifying the 103 * xarray. Taking exec_queue.lock here causes undue dependency on 104 * vm->lock taken during xe_exec_queue_kill(). 105 */ 106 xa_for_each(&xef->exec_queue.xa, idx, q) { 107 xe_exec_queue_kill(q); 108 xe_exec_queue_put(q); 109 } 110 xa_destroy(&xef->exec_queue.xa); 111 mutex_destroy(&xef->exec_queue.lock); 112 mutex_lock(&xef->vm.lock); 113 xa_for_each(&xef->vm.xa, idx, vm) 114 xe_vm_close_and_put(vm); 115 mutex_unlock(&xef->vm.lock); 116 xa_destroy(&xef->vm.xa); 117 mutex_destroy(&xef->vm.lock); 118 119 spin_lock(&xe->clients.lock); 120 xe->clients.count--; 121 spin_unlock(&xe->clients.lock); 122 123 xe_drm_client_put(xef->client); 124 kfree(xef); 125 } 126 127 static const struct drm_ioctl_desc xe_ioctls[] = { 128 DRM_IOCTL_DEF_DRV(XE_DEVICE_QUERY, xe_query_ioctl, DRM_RENDER_ALLOW), 129 DRM_IOCTL_DEF_DRV(XE_GEM_CREATE, xe_gem_create_ioctl, DRM_RENDER_ALLOW), 130 DRM_IOCTL_DEF_DRV(XE_GEM_MMAP_OFFSET, xe_gem_mmap_offset_ioctl, 131 DRM_RENDER_ALLOW), 132 DRM_IOCTL_DEF_DRV(XE_VM_CREATE, xe_vm_create_ioctl, DRM_RENDER_ALLOW), 133 DRM_IOCTL_DEF_DRV(XE_VM_DESTROY, xe_vm_destroy_ioctl, DRM_RENDER_ALLOW), 134 DRM_IOCTL_DEF_DRV(XE_VM_BIND, xe_vm_bind_ioctl, DRM_RENDER_ALLOW), 135 DRM_IOCTL_DEF_DRV(XE_EXEC, xe_exec_ioctl, DRM_RENDER_ALLOW), 136 DRM_IOCTL_DEF_DRV(XE_EXEC_QUEUE_CREATE, xe_exec_queue_create_ioctl, 137 DRM_RENDER_ALLOW), 138 DRM_IOCTL_DEF_DRV(XE_EXEC_QUEUE_DESTROY, xe_exec_queue_destroy_ioctl, 139 DRM_RENDER_ALLOW), 140 DRM_IOCTL_DEF_DRV(XE_EXEC_QUEUE_GET_PROPERTY, xe_exec_queue_get_property_ioctl, 141 DRM_RENDER_ALLOW), 142 DRM_IOCTL_DEF_DRV(XE_WAIT_USER_FENCE, xe_wait_user_fence_ioctl, 143 DRM_RENDER_ALLOW), 144 }; 145 146 static long xe_drm_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 147 { 148 struct drm_file *file_priv = file->private_data; 149 struct xe_device *xe = to_xe_device(file_priv->minor->dev); 150 long ret; 151 152 if (xe_device_wedged(xe)) 153 return -ECANCELED; 154 155 ret = xe_pm_runtime_get_ioctl(xe); 156 if (ret >= 0) 157 ret = drm_ioctl(file, cmd, arg); 158 xe_pm_runtime_put(xe); 159 160 return ret; 161 } 162 163 #ifdef CONFIG_COMPAT 164 static long xe_drm_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 165 { 166 struct drm_file *file_priv = file->private_data; 167 struct xe_device *xe = to_xe_device(file_priv->minor->dev); 168 long ret; 169 170 if (xe_device_wedged(xe)) 171 return -ECANCELED; 172 173 ret = xe_pm_runtime_get_ioctl(xe); 174 if (ret >= 0) 175 ret = drm_compat_ioctl(file, cmd, arg); 176 xe_pm_runtime_put(xe); 177 178 return ret; 179 } 180 #else 181 /* similarly to drm_compat_ioctl, let's it be assigned to .compat_ioct unconditionally */ 182 #define xe_drm_compat_ioctl NULL 183 #endif 184 185 static const struct file_operations xe_driver_fops = { 186 .owner = THIS_MODULE, 187 .open = drm_open, 188 .release = drm_release_noglobal, 189 .unlocked_ioctl = xe_drm_ioctl, 190 .mmap = drm_gem_mmap, 191 .poll = drm_poll, 192 .read = drm_read, 193 .compat_ioctl = xe_drm_compat_ioctl, 194 .llseek = noop_llseek, 195 #ifdef CONFIG_PROC_FS 196 .show_fdinfo = drm_show_fdinfo, 197 #endif 198 }; 199 200 static struct drm_driver driver = { 201 /* Don't use MTRRs here; the Xserver or userspace app should 202 * deal with them for Intel hardware. 203 */ 204 .driver_features = 205 DRIVER_GEM | 206 DRIVER_RENDER | DRIVER_SYNCOBJ | 207 DRIVER_SYNCOBJ_TIMELINE | DRIVER_GEM_GPUVA, 208 .open = xe_file_open, 209 .postclose = xe_file_close, 210 211 .gem_prime_import = xe_gem_prime_import, 212 213 .dumb_create = xe_bo_dumb_create, 214 .dumb_map_offset = drm_gem_ttm_dumb_map_offset, 215 #ifdef CONFIG_PROC_FS 216 .show_fdinfo = xe_drm_client_fdinfo, 217 #endif 218 .ioctls = xe_ioctls, 219 .num_ioctls = ARRAY_SIZE(xe_ioctls), 220 .fops = &xe_driver_fops, 221 .name = DRIVER_NAME, 222 .desc = DRIVER_DESC, 223 .date = DRIVER_DATE, 224 .major = DRIVER_MAJOR, 225 .minor = DRIVER_MINOR, 226 .patchlevel = DRIVER_PATCHLEVEL, 227 }; 228 229 static void xe_device_destroy(struct drm_device *dev, void *dummy) 230 { 231 struct xe_device *xe = to_xe_device(dev); 232 233 if (xe->preempt_fence_wq) 234 destroy_workqueue(xe->preempt_fence_wq); 235 236 if (xe->ordered_wq) 237 destroy_workqueue(xe->ordered_wq); 238 239 if (xe->unordered_wq) 240 destroy_workqueue(xe->unordered_wq); 241 242 ttm_device_fini(&xe->ttm); 243 } 244 245 struct xe_device *xe_device_create(struct pci_dev *pdev, 246 const struct pci_device_id *ent) 247 { 248 struct xe_device *xe; 249 int err; 250 251 xe_display_driver_set_hooks(&driver); 252 253 err = drm_aperture_remove_conflicting_pci_framebuffers(pdev, &driver); 254 if (err) 255 return ERR_PTR(err); 256 257 xe = devm_drm_dev_alloc(&pdev->dev, &driver, struct xe_device, drm); 258 if (IS_ERR(xe)) 259 return xe; 260 261 err = ttm_device_init(&xe->ttm, &xe_ttm_funcs, xe->drm.dev, 262 xe->drm.anon_inode->i_mapping, 263 xe->drm.vma_offset_manager, false, false); 264 if (WARN_ON(err)) 265 goto err; 266 267 err = drmm_add_action_or_reset(&xe->drm, xe_device_destroy, NULL); 268 if (err) 269 goto err; 270 271 xe->info.devid = pdev->device; 272 xe->info.revid = pdev->revision; 273 xe->info.force_execlist = xe_modparam.force_execlist; 274 275 spin_lock_init(&xe->irq.lock); 276 spin_lock_init(&xe->clients.lock); 277 278 init_waitqueue_head(&xe->ufence_wq); 279 280 err = drmm_mutex_init(&xe->drm, &xe->usm.lock); 281 if (err) 282 goto err; 283 284 xa_init_flags(&xe->usm.asid_to_vm, XA_FLAGS_ALLOC); 285 286 if (IS_ENABLED(CONFIG_DRM_XE_DEBUG)) { 287 /* Trigger a large asid and an early asid wrap. */ 288 u32 asid; 289 290 BUILD_BUG_ON(XE_MAX_ASID < 2); 291 err = xa_alloc_cyclic(&xe->usm.asid_to_vm, &asid, NULL, 292 XA_LIMIT(XE_MAX_ASID - 2, XE_MAX_ASID - 1), 293 &xe->usm.next_asid, GFP_KERNEL); 294 drm_WARN_ON(&xe->drm, err); 295 if (err >= 0) 296 xa_erase(&xe->usm.asid_to_vm, asid); 297 } 298 299 spin_lock_init(&xe->pinned.lock); 300 INIT_LIST_HEAD(&xe->pinned.kernel_bo_present); 301 INIT_LIST_HEAD(&xe->pinned.external_vram); 302 INIT_LIST_HEAD(&xe->pinned.evicted); 303 304 xe->preempt_fence_wq = alloc_ordered_workqueue("xe-preempt-fence-wq", 0); 305 xe->ordered_wq = alloc_ordered_workqueue("xe-ordered-wq", 0); 306 xe->unordered_wq = alloc_workqueue("xe-unordered-wq", 0, 0); 307 if (!xe->ordered_wq || !xe->unordered_wq || 308 !xe->preempt_fence_wq) { 309 /* 310 * Cleanup done in xe_device_destroy via 311 * drmm_add_action_or_reset register above 312 */ 313 drm_err(&xe->drm, "Failed to allocate xe workqueues\n"); 314 err = -ENOMEM; 315 goto err; 316 } 317 318 err = xe_display_create(xe); 319 if (WARN_ON(err)) 320 goto err; 321 322 return xe; 323 324 err: 325 return ERR_PTR(err); 326 } 327 328 /* 329 * The driver-initiated FLR is the highest level of reset that we can trigger 330 * from within the driver. It is different from the PCI FLR in that it doesn't 331 * fully reset the SGUnit and doesn't modify the PCI config space and therefore 332 * it doesn't require a re-enumeration of the PCI BARs. However, the 333 * driver-initiated FLR does still cause a reset of both GT and display and a 334 * memory wipe of local and stolen memory, so recovery would require a full HW 335 * re-init and saving/restoring (or re-populating) the wiped memory. Since we 336 * perform the FLR as the very last action before releasing access to the HW 337 * during the driver release flow, we don't attempt recovery at all, because 338 * if/when a new instance of i915 is bound to the device it will do a full 339 * re-init anyway. 340 */ 341 static void xe_driver_flr(struct xe_device *xe) 342 { 343 const unsigned int flr_timeout = 3 * MICRO; /* specs recommend a 3s wait */ 344 struct xe_gt *gt = xe_root_mmio_gt(xe); 345 int ret; 346 347 if (xe_mmio_read32(gt, GU_CNTL_PROTECTED) & DRIVERINT_FLR_DIS) { 348 drm_info_once(&xe->drm, "BIOS Disabled Driver-FLR\n"); 349 return; 350 } 351 352 drm_dbg(&xe->drm, "Triggering Driver-FLR\n"); 353 354 /* 355 * Make sure any pending FLR requests have cleared by waiting for the 356 * FLR trigger bit to go to zero. Also clear GU_DEBUG's DRIVERFLR_STATUS 357 * to make sure it's not still set from a prior attempt (it's a write to 358 * clear bit). 359 * Note that we should never be in a situation where a previous attempt 360 * is still pending (unless the HW is totally dead), but better to be 361 * safe in case something unexpected happens 362 */ 363 ret = xe_mmio_wait32(gt, GU_CNTL, DRIVERFLR, 0, flr_timeout, NULL, false); 364 if (ret) { 365 drm_err(&xe->drm, "Driver-FLR-prepare wait for ready failed! %d\n", ret); 366 return; 367 } 368 xe_mmio_write32(gt, GU_DEBUG, DRIVERFLR_STATUS); 369 370 /* Trigger the actual Driver-FLR */ 371 xe_mmio_rmw32(gt, GU_CNTL, 0, DRIVERFLR); 372 373 /* Wait for hardware teardown to complete */ 374 ret = xe_mmio_wait32(gt, GU_CNTL, DRIVERFLR, 0, flr_timeout, NULL, false); 375 if (ret) { 376 drm_err(&xe->drm, "Driver-FLR-teardown wait completion failed! %d\n", ret); 377 return; 378 } 379 380 /* Wait for hardware/firmware re-init to complete */ 381 ret = xe_mmio_wait32(gt, GU_DEBUG, DRIVERFLR_STATUS, DRIVERFLR_STATUS, 382 flr_timeout, NULL, false); 383 if (ret) { 384 drm_err(&xe->drm, "Driver-FLR-reinit wait completion failed! %d\n", ret); 385 return; 386 } 387 388 /* Clear sticky completion status */ 389 xe_mmio_write32(gt, GU_DEBUG, DRIVERFLR_STATUS); 390 } 391 392 static void xe_driver_flr_fini(void *arg) 393 { 394 struct xe_device *xe = arg; 395 396 if (xe->needs_flr_on_fini) 397 xe_driver_flr(xe); 398 } 399 400 static void xe_device_sanitize(void *arg) 401 { 402 struct xe_device *xe = arg; 403 struct xe_gt *gt; 404 u8 id; 405 406 for_each_gt(gt, xe, id) 407 xe_gt_sanitize(gt); 408 } 409 410 static int xe_set_dma_info(struct xe_device *xe) 411 { 412 unsigned int mask_size = xe->info.dma_mask_size; 413 int err; 414 415 dma_set_max_seg_size(xe->drm.dev, xe_sg_segment_size(xe->drm.dev)); 416 417 err = dma_set_mask(xe->drm.dev, DMA_BIT_MASK(mask_size)); 418 if (err) 419 goto mask_err; 420 421 err = dma_set_coherent_mask(xe->drm.dev, DMA_BIT_MASK(mask_size)); 422 if (err) 423 goto mask_err; 424 425 return 0; 426 427 mask_err: 428 drm_err(&xe->drm, "Can't set DMA mask/consistent mask (%d)\n", err); 429 return err; 430 } 431 432 static bool verify_lmem_ready(struct xe_gt *gt) 433 { 434 u32 val = xe_mmio_read32(gt, GU_CNTL) & LMEM_INIT; 435 436 return !!val; 437 } 438 439 static int wait_for_lmem_ready(struct xe_device *xe) 440 { 441 struct xe_gt *gt = xe_root_mmio_gt(xe); 442 unsigned long timeout, start; 443 444 if (!IS_DGFX(xe)) 445 return 0; 446 447 if (IS_SRIOV_VF(xe)) 448 return 0; 449 450 if (verify_lmem_ready(gt)) 451 return 0; 452 453 drm_dbg(&xe->drm, "Waiting for lmem initialization\n"); 454 455 start = jiffies; 456 timeout = start + msecs_to_jiffies(60 * 1000); /* 60 sec! */ 457 458 do { 459 if (signal_pending(current)) 460 return -EINTR; 461 462 /* 463 * The boot firmware initializes local memory and 464 * assesses its health. If memory training fails, 465 * the punit will have been instructed to keep the GT powered 466 * down.we won't be able to communicate with it 467 * 468 * If the status check is done before punit updates the register, 469 * it can lead to the system being unusable. 470 * use a timeout and defer the probe to prevent this. 471 */ 472 if (time_after(jiffies, timeout)) { 473 drm_dbg(&xe->drm, "lmem not initialized by firmware\n"); 474 return -EPROBE_DEFER; 475 } 476 477 msleep(20); 478 479 } while (!verify_lmem_ready(gt)); 480 481 drm_dbg(&xe->drm, "lmem ready after %ums", 482 jiffies_to_msecs(jiffies - start)); 483 484 return 0; 485 } 486 487 /** 488 * xe_device_probe_early: Device early probe 489 * @xe: xe device instance 490 * 491 * Initialize MMIO resources that don't require any 492 * knowledge about tile count. Also initialize pcode and 493 * check vram initialization on root tile. 494 * 495 * Return: 0 on success, error code on failure 496 */ 497 int xe_device_probe_early(struct xe_device *xe) 498 { 499 int err; 500 501 err = xe_mmio_init(xe); 502 if (err) 503 return err; 504 505 xe_sriov_probe_early(xe); 506 507 err = xe_pcode_probe_early(xe); 508 if (err) 509 return err; 510 511 err = wait_for_lmem_ready(xe); 512 if (err) 513 return err; 514 515 xe->wedged.mode = xe_modparam.wedged_mode; 516 517 return 0; 518 } 519 520 static int xe_device_set_has_flat_ccs(struct xe_device *xe) 521 { 522 u32 reg; 523 int err; 524 525 if (GRAPHICS_VER(xe) < 20 || !xe->info.has_flat_ccs) 526 return 0; 527 528 struct xe_gt *gt = xe_root_mmio_gt(xe); 529 530 err = xe_force_wake_get(gt_to_fw(gt), XE_FW_GT); 531 if (err) 532 return err; 533 534 reg = xe_gt_mcr_unicast_read_any(gt, XE2_FLAT_CCS_BASE_RANGE_LOWER); 535 xe->info.has_flat_ccs = (reg & XE2_FLAT_CCS_ENABLE); 536 537 if (!xe->info.has_flat_ccs) 538 drm_dbg(&xe->drm, 539 "Flat CCS has been disabled in bios, May lead to performance impact"); 540 541 return xe_force_wake_put(gt_to_fw(gt), XE_FW_GT); 542 } 543 544 int xe_device_probe(struct xe_device *xe) 545 { 546 struct xe_tile *tile; 547 struct xe_gt *gt; 548 int err; 549 u8 last_gt; 550 u8 id; 551 552 xe_pat_init_early(xe); 553 554 err = xe_sriov_init(xe); 555 if (err) 556 return err; 557 558 xe->info.mem_region_mask = 1; 559 err = xe_display_init_nommio(xe); 560 if (err) 561 return err; 562 563 err = xe_set_dma_info(xe); 564 if (err) 565 return err; 566 567 err = xe_mmio_probe_tiles(xe); 568 if (err) 569 return err; 570 571 xe_ttm_sys_mgr_init(xe); 572 573 for_each_gt(gt, xe, id) { 574 err = xe_gt_init_early(gt); 575 if (err) 576 return err; 577 } 578 579 for_each_tile(tile, xe, id) { 580 if (IS_SRIOV_VF(xe)) { 581 xe_guc_comm_init_early(&tile->primary_gt->uc.guc); 582 err = xe_gt_sriov_vf_bootstrap(tile->primary_gt); 583 if (err) 584 return err; 585 err = xe_gt_sriov_vf_query_config(tile->primary_gt); 586 if (err) 587 return err; 588 } 589 err = xe_ggtt_init_early(tile->mem.ggtt); 590 if (err) 591 return err; 592 if (IS_SRIOV_VF(xe)) { 593 err = xe_memirq_init(&tile->sriov.vf.memirq); 594 if (err) 595 return err; 596 } 597 } 598 599 for_each_gt(gt, xe, id) { 600 err = xe_gt_init_hwconfig(gt); 601 if (err) 602 return err; 603 } 604 605 err = xe_devcoredump_init(xe); 606 if (err) 607 return err; 608 err = devm_add_action_or_reset(xe->drm.dev, xe_driver_flr_fini, xe); 609 if (err) 610 return err; 611 612 err = xe_display_init_noirq(xe); 613 if (err) 614 return err; 615 616 err = xe_irq_install(xe); 617 if (err) 618 goto err; 619 620 err = xe_device_set_has_flat_ccs(xe); 621 if (err) 622 goto err_irq_shutdown; 623 624 err = xe_vram_probe(xe); 625 if (err) 626 goto err_irq_shutdown; 627 628 for_each_tile(tile, xe, id) { 629 err = xe_tile_init_noalloc(tile); 630 if (err) 631 goto err_irq_shutdown; 632 } 633 634 /* Allocate and map stolen after potential VRAM resize */ 635 xe_ttm_stolen_mgr_init(xe); 636 637 /* 638 * Now that GT is initialized (TTM in particular), 639 * we can try to init display, and inherit the initial fb. 640 * This is the reason the first allocation needs to be done 641 * inside display. 642 */ 643 err = xe_display_init_noaccel(xe); 644 if (err) 645 goto err_irq_shutdown; 646 647 for_each_gt(gt, xe, id) { 648 last_gt = id; 649 650 err = xe_gt_init(gt); 651 if (err) 652 goto err_fini_gt; 653 } 654 655 xe_heci_gsc_init(xe); 656 657 err = xe_display_init(xe); 658 if (err) 659 goto err_fini_gt; 660 661 err = drm_dev_register(&xe->drm, 0); 662 if (err) 663 goto err_fini_display; 664 665 xe_display_register(xe); 666 667 xe_debugfs_register(xe); 668 669 xe_hwmon_register(xe); 670 671 return devm_add_action_or_reset(xe->drm.dev, xe_device_sanitize, xe); 672 673 err_fini_display: 674 xe_display_driver_remove(xe); 675 676 err_fini_gt: 677 for_each_gt(gt, xe, id) { 678 if (id < last_gt) 679 xe_gt_remove(gt); 680 else 681 break; 682 } 683 684 err_irq_shutdown: 685 xe_irq_shutdown(xe); 686 err: 687 xe_display_fini(xe); 688 return err; 689 } 690 691 static void xe_device_remove_display(struct xe_device *xe) 692 { 693 xe_display_unregister(xe); 694 695 drm_dev_unplug(&xe->drm); 696 xe_display_driver_remove(xe); 697 } 698 699 void xe_device_remove(struct xe_device *xe) 700 { 701 struct xe_gt *gt; 702 u8 id; 703 704 xe_device_remove_display(xe); 705 706 xe_display_fini(xe); 707 708 xe_heci_gsc_fini(xe); 709 710 for_each_gt(gt, xe, id) 711 xe_gt_remove(gt); 712 713 xe_irq_shutdown(xe); 714 } 715 716 void xe_device_shutdown(struct xe_device *xe) 717 { 718 } 719 720 void xe_device_wmb(struct xe_device *xe) 721 { 722 struct xe_gt *gt = xe_root_mmio_gt(xe); 723 724 wmb(); 725 if (IS_DGFX(xe)) 726 xe_mmio_write32(gt, SOFTWARE_FLAGS_SPR33, 0); 727 } 728 729 /** 730 * xe_device_td_flush() - Flush transient L3 cache entries 731 * @xe: The device 732 * 733 * Display engine has direct access to memory and is never coherent with L3/L4 734 * caches (or CPU caches), however KMD is responsible for specifically flushing 735 * transient L3 GPU cache entries prior to the flip sequence to ensure scanout 736 * can happen from such a surface without seeing corruption. 737 * 738 * Display surfaces can be tagged as transient by mapping it using one of the 739 * various L3:XD PAT index modes on Xe2. 740 * 741 * Note: On non-discrete xe2 platforms, like LNL, the entire L3 cache is flushed 742 * at the end of each submission via PIPE_CONTROL for compute/render, since SA 743 * Media is not coherent with L3 and we want to support render-vs-media 744 * usescases. For other engines like copy/blt the HW internally forces uncached 745 * behaviour, hence why we can skip the TDF on such platforms. 746 */ 747 void xe_device_td_flush(struct xe_device *xe) 748 { 749 struct xe_gt *gt; 750 u8 id; 751 752 if (!IS_DGFX(xe) || GRAPHICS_VER(xe) < 20) 753 return; 754 755 for_each_gt(gt, xe, id) { 756 if (xe_gt_is_media_type(gt)) 757 continue; 758 759 if (xe_force_wake_get(gt_to_fw(gt), XE_FW_GT)) 760 return; 761 762 xe_mmio_write32(gt, XE2_TDF_CTRL, TRANSIENT_FLUSH_REQUEST); 763 /* 764 * FIXME: We can likely do better here with our choice of 765 * timeout. Currently we just assume the worst case, i.e. 150us, 766 * which is believed to be sufficient to cover the worst case 767 * scenario on current platforms if all cache entries are 768 * transient and need to be flushed.. 769 */ 770 if (xe_mmio_wait32(gt, XE2_TDF_CTRL, TRANSIENT_FLUSH_REQUEST, 0, 771 150, NULL, false)) 772 xe_gt_err_once(gt, "TD flush timeout\n"); 773 774 xe_force_wake_put(gt_to_fw(gt), XE_FW_GT); 775 } 776 } 777 778 u32 xe_device_ccs_bytes(struct xe_device *xe, u64 size) 779 { 780 return xe_device_has_flat_ccs(xe) ? 781 DIV_ROUND_UP_ULL(size, NUM_BYTES_PER_CCS_BYTE(xe)) : 0; 782 } 783 784 /** 785 * xe_device_assert_mem_access - Inspect the current runtime_pm state. 786 * @xe: xe device instance 787 * 788 * To be used before any kind of memory access. It will splat a debug warning 789 * if the device is currently sleeping. But it doesn't guarantee in any way 790 * that the device is going to remain awake. Xe PM runtime get and put 791 * functions might be added to the outer bound of the memory access, while 792 * this check is intended for inner usage to splat some warning if the worst 793 * case has just happened. 794 */ 795 void xe_device_assert_mem_access(struct xe_device *xe) 796 { 797 xe_assert(xe, !xe_pm_runtime_suspended(xe)); 798 } 799 800 void xe_device_snapshot_print(struct xe_device *xe, struct drm_printer *p) 801 { 802 struct xe_gt *gt; 803 u8 id; 804 805 drm_printf(p, "PCI ID: 0x%04x\n", xe->info.devid); 806 drm_printf(p, "PCI revision: 0x%02x\n", xe->info.revid); 807 808 for_each_gt(gt, xe, id) { 809 drm_printf(p, "GT id: %u\n", id); 810 drm_printf(p, "\tType: %s\n", 811 gt->info.type == XE_GT_TYPE_MAIN ? "main" : "media"); 812 drm_printf(p, "\tIP ver: %u.%u.%u\n", 813 REG_FIELD_GET(GMD_ID_ARCH_MASK, gt->info.gmdid), 814 REG_FIELD_GET(GMD_ID_RELEASE_MASK, gt->info.gmdid), 815 REG_FIELD_GET(GMD_ID_REVID, gt->info.gmdid)); 816 drm_printf(p, "\tCS reference clock: %u\n", gt->info.reference_clock); 817 } 818 } 819 820 u64 xe_device_canonicalize_addr(struct xe_device *xe, u64 address) 821 { 822 return sign_extend64(address, xe->info.va_bits - 1); 823 } 824 825 u64 xe_device_uncanonicalize_addr(struct xe_device *xe, u64 address) 826 { 827 return address & GENMASK_ULL(xe->info.va_bits - 1, 0); 828 } 829 830 /** 831 * xe_device_declare_wedged - Declare device wedged 832 * @xe: xe device instance 833 * 834 * This is a final state that can only be cleared with a mudule 835 * re-probe (unbind + bind). 836 * In this state every IOCTL will be blocked so the GT cannot be used. 837 * In general it will be called upon any critical error such as gt reset 838 * failure or guc loading failure. 839 * If xe.wedged module parameter is set to 2, this function will be called 840 * on every single execution timeout (a.k.a. GPU hang) right after devcoredump 841 * snapshot capture. In this mode, GT reset won't be attempted so the state of 842 * the issue is preserved for further debugging. 843 */ 844 void xe_device_declare_wedged(struct xe_device *xe) 845 { 846 if (xe->wedged.mode == 0) { 847 drm_dbg(&xe->drm, "Wedged mode is forcibly disabled\n"); 848 return; 849 } 850 851 if (!atomic_xchg(&xe->wedged.flag, 1)) { 852 xe->needs_flr_on_fini = true; 853 drm_err(&xe->drm, 854 "CRITICAL: Xe has declared device %s as wedged.\n" 855 "IOCTLs and executions are blocked. Only a rebind may clear the failure\n" 856 "Please file a _new_ bug report at https://gitlab.freedesktop.org/drm/xe/kernel/issues/new\n", 857 dev_name(xe->drm.dev)); 858 } 859 } 860