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_i2c.h" 47 #include "xe_irq.h" 48 #include "xe_mmio.h" 49 #include "xe_module.h" 50 #include "xe_nvm.h" 51 #include "xe_oa.h" 52 #include "xe_observation.h" 53 #include "xe_pat.h" 54 #include "xe_pcode.h" 55 #include "xe_pm.h" 56 #include "xe_pmu.h" 57 #include "xe_pxp.h" 58 #include "xe_query.h" 59 #include "xe_shrinker.h" 60 #include "xe_survivability_mode.h" 61 #include "xe_sriov.h" 62 #include "xe_tile.h" 63 #include "xe_ttm_stolen_mgr.h" 64 #include "xe_ttm_sys_mgr.h" 65 #include "xe_vm.h" 66 #include "xe_vram.h" 67 #include "xe_vsec.h" 68 #include "xe_wait_user_fence.h" 69 #include "xe_wa.h" 70 71 #include <generated/xe_wa_oob.h> 72 73 static int xe_file_open(struct drm_device *dev, struct drm_file *file) 74 { 75 struct xe_device *xe = to_xe_device(dev); 76 struct xe_drm_client *client; 77 struct xe_file *xef; 78 int ret = -ENOMEM; 79 struct task_struct *task = NULL; 80 81 xef = kzalloc(sizeof(*xef), GFP_KERNEL); 82 if (!xef) 83 return ret; 84 85 client = xe_drm_client_alloc(); 86 if (!client) { 87 kfree(xef); 88 return ret; 89 } 90 91 xef->drm = file; 92 xef->client = client; 93 xef->xe = xe; 94 95 mutex_init(&xef->vm.lock); 96 xa_init_flags(&xef->vm.xa, XA_FLAGS_ALLOC1); 97 98 mutex_init(&xef->exec_queue.lock); 99 xa_init_flags(&xef->exec_queue.xa, XA_FLAGS_ALLOC1); 100 101 file->driver_priv = xef; 102 kref_init(&xef->refcount); 103 104 task = get_pid_task(rcu_access_pointer(file->pid), PIDTYPE_PID); 105 if (task) { 106 xef->process_name = kstrdup(task->comm, GFP_KERNEL); 107 xef->pid = task->pid; 108 put_task_struct(task); 109 } 110 111 return 0; 112 } 113 114 static void xe_file_destroy(struct kref *ref) 115 { 116 struct xe_file *xef = container_of(ref, struct xe_file, refcount); 117 118 xa_destroy(&xef->exec_queue.xa); 119 mutex_destroy(&xef->exec_queue.lock); 120 xa_destroy(&xef->vm.xa); 121 mutex_destroy(&xef->vm.lock); 122 123 xe_drm_client_put(xef->client); 124 kfree(xef->process_name); 125 kfree(xef); 126 } 127 128 /** 129 * xe_file_get() - Take a reference to the xe file object 130 * @xef: Pointer to the xe file 131 * 132 * Anyone with a pointer to xef must take a reference to the xe file 133 * object using this call. 134 * 135 * Return: xe file pointer 136 */ 137 struct xe_file *xe_file_get(struct xe_file *xef) 138 { 139 kref_get(&xef->refcount); 140 return xef; 141 } 142 143 /** 144 * xe_file_put() - Drop a reference to the xe file object 145 * @xef: Pointer to the xe file 146 * 147 * Used to drop reference to the xef object 148 */ 149 void xe_file_put(struct xe_file *xef) 150 { 151 kref_put(&xef->refcount, xe_file_destroy); 152 } 153 154 static void xe_file_close(struct drm_device *dev, struct drm_file *file) 155 { 156 struct xe_device *xe = to_xe_device(dev); 157 struct xe_file *xef = file->driver_priv; 158 struct xe_vm *vm; 159 struct xe_exec_queue *q; 160 unsigned long idx; 161 162 xe_pm_runtime_get(xe); 163 164 /* 165 * No need for exec_queue.lock here as there is no contention for it 166 * when FD is closing as IOCTLs presumably can't be modifying the 167 * xarray. Taking exec_queue.lock here causes undue dependency on 168 * vm->lock taken during xe_exec_queue_kill(). 169 */ 170 xa_for_each(&xef->exec_queue.xa, idx, q) { 171 if (q->vm && q->hwe->hw_engine_group) 172 xe_hw_engine_group_del_exec_queue(q->hwe->hw_engine_group, q); 173 xe_exec_queue_kill(q); 174 xe_exec_queue_put(q); 175 } 176 xa_for_each(&xef->vm.xa, idx, vm) 177 xe_vm_close_and_put(vm); 178 179 xe_file_put(xef); 180 181 xe_pm_runtime_put(xe); 182 } 183 184 static const struct drm_ioctl_desc xe_ioctls[] = { 185 DRM_IOCTL_DEF_DRV(XE_DEVICE_QUERY, xe_query_ioctl, DRM_RENDER_ALLOW), 186 DRM_IOCTL_DEF_DRV(XE_GEM_CREATE, xe_gem_create_ioctl, DRM_RENDER_ALLOW), 187 DRM_IOCTL_DEF_DRV(XE_GEM_MMAP_OFFSET, xe_gem_mmap_offset_ioctl, 188 DRM_RENDER_ALLOW), 189 DRM_IOCTL_DEF_DRV(XE_VM_CREATE, xe_vm_create_ioctl, DRM_RENDER_ALLOW), 190 DRM_IOCTL_DEF_DRV(XE_VM_DESTROY, xe_vm_destroy_ioctl, DRM_RENDER_ALLOW), 191 DRM_IOCTL_DEF_DRV(XE_VM_BIND, xe_vm_bind_ioctl, DRM_RENDER_ALLOW), 192 DRM_IOCTL_DEF_DRV(XE_EXEC, xe_exec_ioctl, DRM_RENDER_ALLOW), 193 DRM_IOCTL_DEF_DRV(XE_EXEC_QUEUE_CREATE, xe_exec_queue_create_ioctl, 194 DRM_RENDER_ALLOW), 195 DRM_IOCTL_DEF_DRV(XE_EXEC_QUEUE_DESTROY, xe_exec_queue_destroy_ioctl, 196 DRM_RENDER_ALLOW), 197 DRM_IOCTL_DEF_DRV(XE_EXEC_QUEUE_GET_PROPERTY, xe_exec_queue_get_property_ioctl, 198 DRM_RENDER_ALLOW), 199 DRM_IOCTL_DEF_DRV(XE_WAIT_USER_FENCE, xe_wait_user_fence_ioctl, 200 DRM_RENDER_ALLOW), 201 DRM_IOCTL_DEF_DRV(XE_OBSERVATION, xe_observation_ioctl, DRM_RENDER_ALLOW), 202 }; 203 204 static long xe_drm_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 205 { 206 struct drm_file *file_priv = file->private_data; 207 struct xe_device *xe = to_xe_device(file_priv->minor->dev); 208 long ret; 209 210 if (xe_device_wedged(xe)) 211 return -ECANCELED; 212 213 ret = xe_pm_runtime_get_ioctl(xe); 214 if (ret >= 0) 215 ret = drm_ioctl(file, cmd, arg); 216 xe_pm_runtime_put(xe); 217 218 return ret; 219 } 220 221 #ifdef CONFIG_COMPAT 222 static long xe_drm_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 223 { 224 struct drm_file *file_priv = file->private_data; 225 struct xe_device *xe = to_xe_device(file_priv->minor->dev); 226 long ret; 227 228 if (xe_device_wedged(xe)) 229 return -ECANCELED; 230 231 ret = xe_pm_runtime_get_ioctl(xe); 232 if (ret >= 0) 233 ret = drm_compat_ioctl(file, cmd, arg); 234 xe_pm_runtime_put(xe); 235 236 return ret; 237 } 238 #else 239 /* similarly to drm_compat_ioctl, let's it be assigned to .compat_ioct unconditionally */ 240 #define xe_drm_compat_ioctl NULL 241 #endif 242 243 static void barrier_open(struct vm_area_struct *vma) 244 { 245 drm_dev_get(vma->vm_private_data); 246 } 247 248 static void barrier_close(struct vm_area_struct *vma) 249 { 250 drm_dev_put(vma->vm_private_data); 251 } 252 253 static void barrier_release_dummy_page(struct drm_device *dev, void *res) 254 { 255 struct page *dummy_page = (struct page *)res; 256 257 __free_page(dummy_page); 258 } 259 260 static vm_fault_t barrier_fault(struct vm_fault *vmf) 261 { 262 struct drm_device *dev = vmf->vma->vm_private_data; 263 struct vm_area_struct *vma = vmf->vma; 264 vm_fault_t ret = VM_FAULT_NOPAGE; 265 pgprot_t prot; 266 int idx; 267 268 prot = vm_get_page_prot(vma->vm_flags); 269 270 if (drm_dev_enter(dev, &idx)) { 271 unsigned long pfn; 272 273 #define LAST_DB_PAGE_OFFSET 0x7ff001 274 pfn = PHYS_PFN(pci_resource_start(to_pci_dev(dev->dev), 0) + 275 LAST_DB_PAGE_OFFSET); 276 ret = vmf_insert_pfn_prot(vma, vma->vm_start, pfn, 277 pgprot_noncached(prot)); 278 drm_dev_exit(idx); 279 } else { 280 struct page *page; 281 282 /* Allocate new dummy page to map all the VA range in this VMA to it*/ 283 page = alloc_page(GFP_KERNEL | __GFP_ZERO); 284 if (!page) 285 return VM_FAULT_OOM; 286 287 /* Set the page to be freed using drmm release action */ 288 if (drmm_add_action_or_reset(dev, barrier_release_dummy_page, page)) 289 return VM_FAULT_OOM; 290 291 ret = vmf_insert_pfn_prot(vma, vma->vm_start, page_to_pfn(page), 292 prot); 293 } 294 295 return ret; 296 } 297 298 static const struct vm_operations_struct vm_ops_barrier = { 299 .open = barrier_open, 300 .close = barrier_close, 301 .fault = barrier_fault, 302 }; 303 304 static int xe_pci_barrier_mmap(struct file *filp, 305 struct vm_area_struct *vma) 306 { 307 struct drm_file *priv = filp->private_data; 308 struct drm_device *dev = priv->minor->dev; 309 struct xe_device *xe = to_xe_device(dev); 310 311 if (!IS_DGFX(xe)) 312 return -EINVAL; 313 314 if (vma->vm_end - vma->vm_start > SZ_4K) 315 return -EINVAL; 316 317 if (is_cow_mapping(vma->vm_flags)) 318 return -EINVAL; 319 320 if (vma->vm_flags & (VM_READ | VM_EXEC)) 321 return -EINVAL; 322 323 vm_flags_clear(vma, VM_MAYREAD | VM_MAYEXEC); 324 vm_flags_set(vma, VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP | VM_IO); 325 vma->vm_ops = &vm_ops_barrier; 326 vma->vm_private_data = dev; 327 drm_dev_get(vma->vm_private_data); 328 329 return 0; 330 } 331 332 static int xe_mmap(struct file *filp, struct vm_area_struct *vma) 333 { 334 struct drm_file *priv = filp->private_data; 335 struct drm_device *dev = priv->minor->dev; 336 337 if (drm_dev_is_unplugged(dev)) 338 return -ENODEV; 339 340 switch (vma->vm_pgoff) { 341 case XE_PCI_BARRIER_MMAP_OFFSET >> XE_PTE_SHIFT: 342 return xe_pci_barrier_mmap(filp, vma); 343 } 344 345 return drm_gem_mmap(filp, vma); 346 } 347 348 static const struct file_operations xe_driver_fops = { 349 .owner = THIS_MODULE, 350 .open = drm_open, 351 .release = drm_release_noglobal, 352 .unlocked_ioctl = xe_drm_ioctl, 353 .mmap = xe_mmap, 354 .poll = drm_poll, 355 .read = drm_read, 356 .compat_ioctl = xe_drm_compat_ioctl, 357 .llseek = noop_llseek, 358 #ifdef CONFIG_PROC_FS 359 .show_fdinfo = drm_show_fdinfo, 360 #endif 361 .fop_flags = FOP_UNSIGNED_OFFSET, 362 }; 363 364 static struct drm_driver driver = { 365 /* Don't use MTRRs here; the Xserver or userspace app should 366 * deal with them for Intel hardware. 367 */ 368 .driver_features = 369 DRIVER_GEM | 370 DRIVER_RENDER | DRIVER_SYNCOBJ | 371 DRIVER_SYNCOBJ_TIMELINE | DRIVER_GEM_GPUVA, 372 .open = xe_file_open, 373 .postclose = xe_file_close, 374 375 .gem_prime_import = xe_gem_prime_import, 376 377 .dumb_create = xe_bo_dumb_create, 378 .dumb_map_offset = drm_gem_ttm_dumb_map_offset, 379 #ifdef CONFIG_PROC_FS 380 .show_fdinfo = xe_drm_client_fdinfo, 381 #endif 382 .ioctls = xe_ioctls, 383 .num_ioctls = ARRAY_SIZE(xe_ioctls), 384 .fops = &xe_driver_fops, 385 .name = DRIVER_NAME, 386 .desc = DRIVER_DESC, 387 .major = DRIVER_MAJOR, 388 .minor = DRIVER_MINOR, 389 .patchlevel = DRIVER_PATCHLEVEL, 390 }; 391 392 static void xe_device_destroy(struct drm_device *dev, void *dummy) 393 { 394 struct xe_device *xe = to_xe_device(dev); 395 396 xe_bo_dev_fini(&xe->bo_device); 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 (xe->destroy_wq) 408 destroy_workqueue(xe->destroy_wq); 409 410 ttm_device_fini(&xe->ttm); 411 } 412 413 struct xe_device *xe_device_create(struct pci_dev *pdev, 414 const struct pci_device_id *ent) 415 { 416 struct xe_device *xe; 417 int err; 418 419 xe_display_driver_set_hooks(&driver); 420 421 err = aperture_remove_conflicting_pci_devices(pdev, driver.name); 422 if (err) 423 return ERR_PTR(err); 424 425 xe = devm_drm_dev_alloc(&pdev->dev, &driver, struct xe_device, drm); 426 if (IS_ERR(xe)) 427 return xe; 428 429 err = ttm_device_init(&xe->ttm, &xe_ttm_funcs, xe->drm.dev, 430 xe->drm.anon_inode->i_mapping, 431 xe->drm.vma_offset_manager, false, false); 432 if (WARN_ON(err)) 433 goto err; 434 435 xe_bo_dev_init(&xe->bo_device); 436 err = drmm_add_action_or_reset(&xe->drm, xe_device_destroy, NULL); 437 if (err) 438 goto err; 439 440 err = xe_shrinker_create(xe); 441 if (err) 442 goto err; 443 444 xe->info.devid = pdev->device; 445 xe->info.revid = pdev->revision; 446 xe->info.force_execlist = xe_modparam.force_execlist; 447 xe->atomic_svm_timeslice_ms = 5; 448 449 err = xe_irq_init(xe); 450 if (err) 451 goto err; 452 453 init_waitqueue_head(&xe->ufence_wq); 454 455 init_rwsem(&xe->usm.lock); 456 457 xa_init_flags(&xe->usm.asid_to_vm, XA_FLAGS_ALLOC); 458 459 if (IS_ENABLED(CONFIG_DRM_XE_DEBUG)) { 460 /* Trigger a large asid and an early asid wrap. */ 461 u32 asid; 462 463 BUILD_BUG_ON(XE_MAX_ASID < 2); 464 err = xa_alloc_cyclic(&xe->usm.asid_to_vm, &asid, NULL, 465 XA_LIMIT(XE_MAX_ASID - 2, XE_MAX_ASID - 1), 466 &xe->usm.next_asid, GFP_KERNEL); 467 drm_WARN_ON(&xe->drm, err); 468 if (err >= 0) 469 xa_erase(&xe->usm.asid_to_vm, asid); 470 } 471 472 err = xe_bo_pinned_init(xe); 473 if (err) 474 goto err; 475 476 xe->preempt_fence_wq = alloc_ordered_workqueue("xe-preempt-fence-wq", 477 WQ_MEM_RECLAIM); 478 xe->ordered_wq = alloc_ordered_workqueue("xe-ordered-wq", 0); 479 xe->unordered_wq = alloc_workqueue("xe-unordered-wq", 0, 0); 480 xe->destroy_wq = alloc_workqueue("xe-destroy-wq", 0, 0); 481 if (!xe->ordered_wq || !xe->unordered_wq || 482 !xe->preempt_fence_wq || !xe->destroy_wq) { 483 /* 484 * Cleanup done in xe_device_destroy via 485 * drmm_add_action_or_reset register above 486 */ 487 drm_err(&xe->drm, "Failed to allocate xe workqueues\n"); 488 err = -ENOMEM; 489 goto err; 490 } 491 492 err = drmm_mutex_init(&xe->drm, &xe->pmt.lock); 493 if (err) 494 goto err; 495 496 return xe; 497 498 err: 499 return ERR_PTR(err); 500 } 501 ALLOW_ERROR_INJECTION(xe_device_create, ERRNO); /* See xe_pci_probe() */ 502 503 static bool xe_driver_flr_disabled(struct xe_device *xe) 504 { 505 if (IS_SRIOV_VF(xe)) 506 return true; 507 508 if (xe_mmio_read32(xe_root_tile_mmio(xe), GU_CNTL_PROTECTED) & DRIVERINT_FLR_DIS) { 509 drm_info(&xe->drm, "Driver-FLR disabled by BIOS\n"); 510 return true; 511 } 512 513 return false; 514 } 515 516 /* 517 * The driver-initiated FLR is the highest level of reset that we can trigger 518 * from within the driver. It is different from the PCI FLR in that it doesn't 519 * fully reset the SGUnit and doesn't modify the PCI config space and therefore 520 * it doesn't require a re-enumeration of the PCI BARs. However, the 521 * driver-initiated FLR does still cause a reset of both GT and display and a 522 * memory wipe of local and stolen memory, so recovery would require a full HW 523 * re-init and saving/restoring (or re-populating) the wiped memory. Since we 524 * perform the FLR as the very last action before releasing access to the HW 525 * during the driver release flow, we don't attempt recovery at all, because 526 * if/when a new instance of i915 is bound to the device it will do a full 527 * re-init anyway. 528 */ 529 static void __xe_driver_flr(struct xe_device *xe) 530 { 531 const unsigned int flr_timeout = 3 * USEC_PER_SEC; /* specs recommend a 3s wait */ 532 struct xe_mmio *mmio = xe_root_tile_mmio(xe); 533 int ret; 534 535 drm_dbg(&xe->drm, "Triggering Driver-FLR\n"); 536 537 /* 538 * Make sure any pending FLR requests have cleared by waiting for the 539 * FLR trigger bit to go to zero. Also clear GU_DEBUG's DRIVERFLR_STATUS 540 * to make sure it's not still set from a prior attempt (it's a write to 541 * clear bit). 542 * Note that we should never be in a situation where a previous attempt 543 * is still pending (unless the HW is totally dead), but better to be 544 * safe in case something unexpected happens 545 */ 546 ret = xe_mmio_wait32(mmio, GU_CNTL, DRIVERFLR, 0, flr_timeout, NULL, false); 547 if (ret) { 548 drm_err(&xe->drm, "Driver-FLR-prepare wait for ready failed! %d\n", ret); 549 return; 550 } 551 xe_mmio_write32(mmio, GU_DEBUG, DRIVERFLR_STATUS); 552 553 /* Trigger the actual Driver-FLR */ 554 xe_mmio_rmw32(mmio, GU_CNTL, 0, DRIVERFLR); 555 556 /* Wait for hardware teardown to complete */ 557 ret = xe_mmio_wait32(mmio, GU_CNTL, DRIVERFLR, 0, flr_timeout, NULL, false); 558 if (ret) { 559 drm_err(&xe->drm, "Driver-FLR-teardown wait completion failed! %d\n", ret); 560 return; 561 } 562 563 /* Wait for hardware/firmware re-init to complete */ 564 ret = xe_mmio_wait32(mmio, GU_DEBUG, DRIVERFLR_STATUS, DRIVERFLR_STATUS, 565 flr_timeout, NULL, false); 566 if (ret) { 567 drm_err(&xe->drm, "Driver-FLR-reinit wait completion failed! %d\n", ret); 568 return; 569 } 570 571 /* Clear sticky completion status */ 572 xe_mmio_write32(mmio, GU_DEBUG, DRIVERFLR_STATUS); 573 } 574 575 static void xe_driver_flr(struct xe_device *xe) 576 { 577 if (xe_driver_flr_disabled(xe)) 578 return; 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 sriov_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 sriov_update_device_info(xe); 710 711 err = xe_pcode_probe_early(xe); 712 if (err || xe_survivability_mode_is_requested(xe)) { 713 int save_err = err; 714 715 /* 716 * Try to leave device in survivability mode if device is 717 * possible, but still return the previous error for error 718 * propagation 719 */ 720 err = xe_survivability_mode_enable(xe); 721 if (err) 722 return err; 723 724 return save_err; 725 } 726 727 err = wait_for_lmem_ready(xe); 728 if (err) 729 return err; 730 731 xe->wedged.mode = xe_modparam.wedged_mode; 732 733 return 0; 734 } 735 ALLOW_ERROR_INJECTION(xe_device_probe_early, ERRNO); /* See xe_pci_probe() */ 736 737 static int probe_has_flat_ccs(struct xe_device *xe) 738 { 739 struct xe_gt *gt; 740 unsigned int fw_ref; 741 u32 reg; 742 743 /* Always enabled/disabled, no runtime check to do */ 744 if (GRAPHICS_VER(xe) < 20 || !xe->info.has_flat_ccs || IS_SRIOV_VF(xe)) 745 return 0; 746 747 gt = xe_root_mmio_gt(xe); 748 749 fw_ref = xe_force_wake_get(gt_to_fw(gt), XE_FW_GT); 750 if (!fw_ref) 751 return -ETIMEDOUT; 752 753 reg = xe_gt_mcr_unicast_read_any(gt, XE2_FLAT_CCS_BASE_RANGE_LOWER); 754 xe->info.has_flat_ccs = (reg & XE2_FLAT_CCS_ENABLE); 755 756 if (!xe->info.has_flat_ccs) 757 drm_dbg(&xe->drm, 758 "Flat CCS has been disabled in bios, May lead to performance impact"); 759 760 xe_force_wake_put(gt_to_fw(gt), fw_ref); 761 762 return 0; 763 } 764 765 int xe_device_probe(struct xe_device *xe) 766 { 767 struct xe_tile *tile; 768 struct xe_gt *gt; 769 int err; 770 u8 id; 771 772 xe_pat_init_early(xe); 773 774 err = xe_sriov_init(xe); 775 if (err) 776 return err; 777 778 xe->info.mem_region_mask = 1; 779 780 err = xe_set_dma_info(xe); 781 if (err) 782 return err; 783 784 err = xe_mmio_probe_tiles(xe); 785 if (err) 786 return err; 787 788 for_each_gt(gt, xe, id) { 789 err = xe_gt_init_early(gt); 790 if (err) 791 return err; 792 } 793 794 for_each_tile(tile, xe, id) { 795 err = xe_ggtt_init_early(tile->mem.ggtt); 796 if (err) 797 return err; 798 } 799 800 err = xe_devcoredump_init(xe); 801 if (err) 802 return err; 803 804 /* 805 * From here on, if a step fails, make sure a Driver-FLR is triggereed 806 */ 807 err = devm_add_action_or_reset(xe->drm.dev, xe_driver_flr_fini, xe); 808 if (err) 809 return err; 810 811 err = probe_has_flat_ccs(xe); 812 if (err) 813 return err; 814 815 err = xe_vram_probe(xe); 816 if (err) 817 return err; 818 819 for_each_tile(tile, xe, id) { 820 err = xe_tile_init_noalloc(tile); 821 if (err) 822 return err; 823 } 824 825 /* 826 * Allow allocations only now to ensure xe_display_init_early() 827 * is the first to allocate, always. 828 */ 829 err = xe_ttm_sys_mgr_init(xe); 830 if (err) 831 return err; 832 833 /* Allocate and map stolen after potential VRAM resize */ 834 err = xe_ttm_stolen_mgr_init(xe); 835 if (err) 836 return err; 837 838 /* 839 * Now that GT is initialized (TTM in particular), 840 * we can try to init display, and inherit the initial fb. 841 * This is the reason the first allocation needs to be done 842 * inside display. 843 */ 844 err = xe_display_init_early(xe); 845 if (err) 846 return err; 847 848 for_each_tile(tile, xe, id) { 849 err = xe_tile_init(tile); 850 if (err) 851 return err; 852 } 853 854 err = xe_irq_install(xe); 855 if (err) 856 return err; 857 858 for_each_gt(gt, xe, id) { 859 err = xe_gt_init(gt); 860 if (err) 861 return err; 862 } 863 864 xe_nvm_init(xe); 865 866 err = xe_heci_gsc_init(xe); 867 if (err) 868 return err; 869 870 err = xe_oa_init(xe); 871 if (err) 872 return err; 873 874 err = xe_display_init(xe); 875 if (err) 876 return err; 877 878 err = xe_pxp_init(xe); 879 if (err) 880 return err; 881 882 err = drm_dev_register(&xe->drm, 0); 883 if (err) 884 return err; 885 886 xe_display_register(xe); 887 888 err = xe_oa_register(xe); 889 if (err) 890 goto err_unregister_display; 891 892 err = xe_pmu_register(&xe->pmu); 893 if (err) 894 goto err_unregister_display; 895 896 err = xe_device_sysfs_init(xe); 897 if (err) 898 goto err_unregister_display; 899 900 xe_debugfs_register(xe); 901 902 err = xe_hwmon_register(xe); 903 if (err) 904 goto err_unregister_display; 905 906 err = xe_i2c_probe(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 return devm_add_action_or_reset(xe->drm.dev, xe_device_sanitize, xe); 916 917 err_unregister_display: 918 xe_display_unregister(xe); 919 920 return err; 921 } 922 923 void xe_device_remove(struct xe_device *xe) 924 { 925 xe_display_unregister(xe); 926 927 xe_nvm_fini(xe); 928 929 drm_dev_unplug(&xe->drm); 930 931 xe_bo_pci_dev_remove_all(xe); 932 } 933 934 void xe_device_shutdown(struct xe_device *xe) 935 { 936 struct xe_gt *gt; 937 u8 id; 938 939 drm_dbg(&xe->drm, "Shutting down device\n"); 940 941 if (xe_driver_flr_disabled(xe)) { 942 xe_display_pm_shutdown(xe); 943 944 xe_irq_suspend(xe); 945 946 for_each_gt(gt, xe, id) 947 xe_gt_shutdown(gt); 948 949 xe_display_pm_shutdown_late(xe); 950 } else { 951 /* BOOM! */ 952 __xe_driver_flr(xe); 953 } 954 } 955 956 /** 957 * xe_device_wmb() - Device specific write memory barrier 958 * @xe: the &xe_device 959 * 960 * While wmb() is sufficient for a barrier if we use system memory, on discrete 961 * platforms with device memory we additionally need to issue a register write. 962 * Since it doesn't matter which register we write to, use the read-only VF_CAP 963 * register that is also marked as accessible by the VFs. 964 */ 965 void xe_device_wmb(struct xe_device *xe) 966 { 967 wmb(); 968 if (IS_DGFX(xe)) 969 xe_mmio_write32(xe_root_tile_mmio(xe), VF_CAP_REG, 0); 970 } 971 972 /* 973 * Issue a TRANSIENT_FLUSH_REQUEST and wait for completion on each gt. 974 */ 975 static void tdf_request_sync(struct xe_device *xe) 976 { 977 unsigned int fw_ref; 978 struct xe_gt *gt; 979 u8 id; 980 981 for_each_gt(gt, xe, id) { 982 if (xe_gt_is_media_type(gt)) 983 continue; 984 985 fw_ref = xe_force_wake_get(gt_to_fw(gt), XE_FW_GT); 986 if (!fw_ref) 987 return; 988 989 xe_mmio_write32(>->mmio, XE2_TDF_CTRL, TRANSIENT_FLUSH_REQUEST); 990 991 /* 992 * FIXME: We can likely do better here with our choice of 993 * timeout. Currently we just assume the worst case, i.e. 150us, 994 * which is believed to be sufficient to cover the worst case 995 * scenario on current platforms if all cache entries are 996 * transient and need to be flushed.. 997 */ 998 if (xe_mmio_wait32(>->mmio, XE2_TDF_CTRL, TRANSIENT_FLUSH_REQUEST, 0, 999 150, NULL, false)) 1000 xe_gt_err_once(gt, "TD flush timeout\n"); 1001 1002 xe_force_wake_put(gt_to_fw(gt), fw_ref); 1003 } 1004 } 1005 1006 void xe_device_l2_flush(struct xe_device *xe) 1007 { 1008 struct xe_gt *gt; 1009 unsigned int fw_ref; 1010 1011 gt = xe_root_mmio_gt(xe); 1012 1013 if (!XE_WA(gt, 16023588340)) 1014 return; 1015 1016 fw_ref = xe_force_wake_get(gt_to_fw(gt), XE_FW_GT); 1017 if (!fw_ref) 1018 return; 1019 1020 spin_lock(>->global_invl_lock); 1021 1022 xe_mmio_write32(>->mmio, XE2_GLOBAL_INVAL, 0x1); 1023 if (xe_mmio_wait32(>->mmio, XE2_GLOBAL_INVAL, 0x1, 0x0, 500, NULL, true)) 1024 xe_gt_err_once(gt, "Global invalidation timeout\n"); 1025 1026 spin_unlock(>->global_invl_lock); 1027 1028 xe_force_wake_put(gt_to_fw(gt), fw_ref); 1029 } 1030 1031 /** 1032 * xe_device_td_flush() - Flush transient L3 cache entries 1033 * @xe: The device 1034 * 1035 * Display engine has direct access to memory and is never coherent with L3/L4 1036 * caches (or CPU caches), however KMD is responsible for specifically flushing 1037 * transient L3 GPU cache entries prior to the flip sequence to ensure scanout 1038 * can happen from such a surface without seeing corruption. 1039 * 1040 * Display surfaces can be tagged as transient by mapping it using one of the 1041 * various L3:XD PAT index modes on Xe2. 1042 * 1043 * Note: On non-discrete xe2 platforms, like LNL, the entire L3 cache is flushed 1044 * at the end of each submission via PIPE_CONTROL for compute/render, since SA 1045 * Media is not coherent with L3 and we want to support render-vs-media 1046 * usescases. For other engines like copy/blt the HW internally forces uncached 1047 * behaviour, hence why we can skip the TDF on such platforms. 1048 */ 1049 void xe_device_td_flush(struct xe_device *xe) 1050 { 1051 struct xe_gt *root_gt; 1052 1053 if (!IS_DGFX(xe) || GRAPHICS_VER(xe) < 20) 1054 return; 1055 1056 root_gt = xe_root_mmio_gt(xe); 1057 if (XE_WA(root_gt, 16023588340)) { 1058 /* A transient flush is not sufficient: flush the L2 */ 1059 xe_device_l2_flush(xe); 1060 } else { 1061 xe_guc_pc_apply_flush_freq_limit(&root_gt->uc.guc.pc); 1062 tdf_request_sync(xe); 1063 xe_guc_pc_remove_flush_freq_limit(&root_gt->uc.guc.pc); 1064 } 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 NULL); 1171 } 1172 1173 for_each_gt(gt, xe, id) 1174 xe_gt_declare_wedged(gt); 1175 } 1176