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