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