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