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