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