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