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