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