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_vram_types.h" 68 #include "xe_vsec.h" 69 #include "xe_wait_user_fence.h" 70 #include "xe_wa.h" 71 72 #include <generated/xe_device_wa_oob.h> 73 #include <generated/xe_wa_oob.h> 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 xe_bo_dev_fini(&xe->bo_device); 399 400 if (xe->preempt_fence_wq) 401 destroy_workqueue(xe->preempt_fence_wq); 402 403 if (xe->ordered_wq) 404 destroy_workqueue(xe->ordered_wq); 405 406 if (xe->unordered_wq) 407 destroy_workqueue(xe->unordered_wq); 408 409 if (xe->destroy_wq) 410 destroy_workqueue(xe->destroy_wq); 411 412 ttm_device_fini(&xe->ttm); 413 } 414 415 struct xe_device *xe_device_create(struct pci_dev *pdev, 416 const struct pci_device_id *ent) 417 { 418 struct xe_device *xe; 419 int err; 420 421 xe_display_driver_set_hooks(&driver); 422 423 err = aperture_remove_conflicting_pci_devices(pdev, driver.name); 424 if (err) 425 return ERR_PTR(err); 426 427 xe = devm_drm_dev_alloc(&pdev->dev, &driver, struct xe_device, drm); 428 if (IS_ERR(xe)) 429 return xe; 430 431 err = ttm_device_init(&xe->ttm, &xe_ttm_funcs, xe->drm.dev, 432 xe->drm.anon_inode->i_mapping, 433 xe->drm.vma_offset_manager, false, false); 434 if (WARN_ON(err)) 435 goto err; 436 437 xe_bo_dev_init(&xe->bo_device); 438 err = drmm_add_action_or_reset(&xe->drm, xe_device_destroy, NULL); 439 if (err) 440 goto err; 441 442 err = xe_shrinker_create(xe); 443 if (err) 444 goto err; 445 446 xe->info.devid = pdev->device; 447 xe->info.revid = pdev->revision; 448 xe->info.force_execlist = xe_modparam.force_execlist; 449 xe->atomic_svm_timeslice_ms = 5; 450 451 err = xe_irq_init(xe); 452 if (err) 453 goto err; 454 455 init_waitqueue_head(&xe->ufence_wq); 456 457 init_rwsem(&xe->usm.lock); 458 459 xa_init_flags(&xe->usm.asid_to_vm, XA_FLAGS_ALLOC); 460 461 if (IS_ENABLED(CONFIG_DRM_XE_DEBUG)) { 462 /* Trigger a large asid and an early asid wrap. */ 463 u32 asid; 464 465 BUILD_BUG_ON(XE_MAX_ASID < 2); 466 err = xa_alloc_cyclic(&xe->usm.asid_to_vm, &asid, NULL, 467 XA_LIMIT(XE_MAX_ASID - 2, XE_MAX_ASID - 1), 468 &xe->usm.next_asid, GFP_KERNEL); 469 drm_WARN_ON(&xe->drm, err); 470 if (err >= 0) 471 xa_erase(&xe->usm.asid_to_vm, asid); 472 } 473 474 err = xe_bo_pinned_init(xe); 475 if (err) 476 goto err; 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 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_cscfi = 0; 686 xe->info.has_heci_gscfi = 0; 687 xe->info.skip_guc_pc = 1; 688 xe->info.skip_pcode = 1; 689 } 690 } 691 692 static int xe_device_vram_alloc(struct xe_device *xe) 693 { 694 struct xe_vram_region *vram; 695 696 if (!IS_DGFX(xe)) 697 return 0; 698 699 vram = drmm_kzalloc(&xe->drm, sizeof(*vram), GFP_KERNEL); 700 if (!vram) 701 return -ENOMEM; 702 703 xe->mem.vram = vram; 704 return 0; 705 } 706 707 /** 708 * xe_device_probe_early: Device early probe 709 * @xe: xe device instance 710 * 711 * Initialize MMIO resources that don't require any 712 * knowledge about tile count. Also initialize pcode and 713 * check vram initialization on root tile. 714 * 715 * Return: 0 on success, error code on failure 716 */ 717 int xe_device_probe_early(struct xe_device *xe) 718 { 719 int err; 720 721 xe_wa_device_init(xe); 722 xe_wa_process_device_oob(xe); 723 724 err = xe_mmio_probe_early(xe); 725 if (err) 726 return err; 727 728 xe_sriov_probe_early(xe); 729 730 sriov_update_device_info(xe); 731 732 err = xe_pcode_probe_early(xe); 733 if (err || xe_survivability_mode_is_requested(xe)) { 734 int save_err = err; 735 736 /* 737 * Try to leave device in survivability mode if device is 738 * possible, but still return the previous error for error 739 * propagation 740 */ 741 err = xe_survivability_mode_enable(xe); 742 if (err) 743 return err; 744 745 return save_err; 746 } 747 748 err = wait_for_lmem_ready(xe); 749 if (err) 750 return err; 751 752 xe->wedged.mode = xe_modparam.wedged_mode; 753 754 err = xe_device_vram_alloc(xe); 755 if (err) 756 return err; 757 758 return 0; 759 } 760 ALLOW_ERROR_INJECTION(xe_device_probe_early, ERRNO); /* See xe_pci_probe() */ 761 762 static int probe_has_flat_ccs(struct xe_device *xe) 763 { 764 struct xe_gt *gt; 765 unsigned int fw_ref; 766 u32 reg; 767 768 /* Always enabled/disabled, no runtime check to do */ 769 if (GRAPHICS_VER(xe) < 20 || !xe->info.has_flat_ccs || IS_SRIOV_VF(xe)) 770 return 0; 771 772 gt = xe_root_mmio_gt(xe); 773 774 fw_ref = xe_force_wake_get(gt_to_fw(gt), XE_FW_GT); 775 if (!fw_ref) 776 return -ETIMEDOUT; 777 778 reg = xe_gt_mcr_unicast_read_any(gt, XE2_FLAT_CCS_BASE_RANGE_LOWER); 779 xe->info.has_flat_ccs = (reg & XE2_FLAT_CCS_ENABLE); 780 781 if (!xe->info.has_flat_ccs) 782 drm_dbg(&xe->drm, 783 "Flat CCS has been disabled in bios, May lead to performance impact"); 784 785 xe_force_wake_put(gt_to_fw(gt), fw_ref); 786 787 return 0; 788 } 789 790 int xe_device_probe(struct xe_device *xe) 791 { 792 struct xe_tile *tile; 793 struct xe_gt *gt; 794 int err; 795 u8 id; 796 797 xe_pat_init_early(xe); 798 799 err = xe_sriov_init(xe); 800 if (err) 801 return err; 802 803 xe->info.mem_region_mask = 1; 804 805 err = xe_set_dma_info(xe); 806 if (err) 807 return err; 808 809 err = xe_mmio_probe_tiles(xe); 810 if (err) 811 return err; 812 813 for_each_gt(gt, xe, id) { 814 err = xe_gt_init_early(gt); 815 if (err) 816 return err; 817 } 818 819 for_each_tile(tile, xe, id) { 820 err = xe_ggtt_init_early(tile->mem.ggtt); 821 if (err) 822 return err; 823 } 824 825 /* 826 * From here on, if a step fails, make sure a Driver-FLR is triggereed 827 */ 828 err = devm_add_action_or_reset(xe->drm.dev, xe_driver_flr_fini, xe); 829 if (err) 830 return err; 831 832 err = probe_has_flat_ccs(xe); 833 if (err) 834 return err; 835 836 err = xe_vram_probe(xe); 837 if (err) 838 return err; 839 840 for_each_tile(tile, xe, id) { 841 err = xe_tile_init_noalloc(tile); 842 if (err) 843 return err; 844 } 845 846 /* 847 * Allow allocations only now to ensure xe_display_init_early() 848 * is the first to allocate, always. 849 */ 850 err = xe_ttm_sys_mgr_init(xe); 851 if (err) 852 return err; 853 854 /* Allocate and map stolen after potential VRAM resize */ 855 err = xe_ttm_stolen_mgr_init(xe); 856 if (err) 857 return err; 858 859 /* 860 * Now that GT is initialized (TTM in particular), 861 * we can try to init display, and inherit the initial fb. 862 * This is the reason the first allocation needs to be done 863 * inside display. 864 */ 865 err = xe_display_init_early(xe); 866 if (err) 867 return err; 868 869 for_each_tile(tile, xe, id) { 870 err = xe_tile_init(tile); 871 if (err) 872 return err; 873 } 874 875 err = xe_irq_install(xe); 876 if (err) 877 return err; 878 879 for_each_gt(gt, xe, id) { 880 err = xe_gt_init(gt); 881 if (err) 882 return err; 883 } 884 885 if (xe->tiles->media_gt && 886 XE_WA(xe->tiles->media_gt, 15015404425_disable)) 887 XE_DEVICE_WA_DISABLE(xe, 15015404425); 888 889 err = xe_devcoredump_init(xe); 890 if (err) 891 return err; 892 893 xe_nvm_init(xe); 894 895 err = xe_heci_gsc_init(xe); 896 if (err) 897 return err; 898 899 err = xe_oa_init(xe); 900 if (err) 901 return err; 902 903 err = xe_display_init(xe); 904 if (err) 905 return err; 906 907 err = xe_pxp_init(xe); 908 if (err) 909 return err; 910 911 err = drm_dev_register(&xe->drm, 0); 912 if (err) 913 return err; 914 915 xe_display_register(xe); 916 917 err = xe_oa_register(xe); 918 if (err) 919 goto err_unregister_display; 920 921 err = xe_pmu_register(&xe->pmu); 922 if (err) 923 goto err_unregister_display; 924 925 err = xe_device_sysfs_init(xe); 926 if (err) 927 goto err_unregister_display; 928 929 xe_debugfs_register(xe); 930 931 err = xe_hwmon_register(xe); 932 if (err) 933 goto err_unregister_display; 934 935 err = xe_i2c_probe(xe); 936 if (err) 937 goto err_unregister_display; 938 939 for_each_gt(gt, xe, id) 940 xe_gt_sanitize_freq(gt); 941 942 xe_vsec_init(xe); 943 944 err = xe_sriov_late_init(xe); 945 if (err) 946 goto err_unregister_display; 947 948 return devm_add_action_or_reset(xe->drm.dev, xe_device_sanitize, xe); 949 950 err_unregister_display: 951 xe_display_unregister(xe); 952 953 return err; 954 } 955 956 void xe_device_remove(struct xe_device *xe) 957 { 958 xe_display_unregister(xe); 959 960 xe_nvm_fini(xe); 961 962 drm_dev_unplug(&xe->drm); 963 964 xe_bo_pci_dev_remove_all(xe); 965 } 966 967 void xe_device_shutdown(struct xe_device *xe) 968 { 969 struct xe_gt *gt; 970 u8 id; 971 972 drm_dbg(&xe->drm, "Shutting down device\n"); 973 974 if (xe_driver_flr_disabled(xe)) { 975 xe_display_pm_shutdown(xe); 976 977 xe_irq_suspend(xe); 978 979 for_each_gt(gt, xe, id) 980 xe_gt_shutdown(gt); 981 982 xe_display_pm_shutdown_late(xe); 983 } else { 984 /* BOOM! */ 985 __xe_driver_flr(xe); 986 } 987 } 988 989 /** 990 * xe_device_wmb() - Device specific write memory barrier 991 * @xe: the &xe_device 992 * 993 * While wmb() is sufficient for a barrier if we use system memory, on discrete 994 * platforms with device memory we additionally need to issue a register write. 995 * Since it doesn't matter which register we write to, use the read-only VF_CAP 996 * register that is also marked as accessible by the VFs. 997 */ 998 void xe_device_wmb(struct xe_device *xe) 999 { 1000 wmb(); 1001 if (IS_DGFX(xe)) 1002 xe_mmio_write32(xe_root_tile_mmio(xe), VF_CAP_REG, 0); 1003 } 1004 1005 /* 1006 * Issue a TRANSIENT_FLUSH_REQUEST and wait for completion on each gt. 1007 */ 1008 static void tdf_request_sync(struct xe_device *xe) 1009 { 1010 unsigned int fw_ref; 1011 struct xe_gt *gt; 1012 u8 id; 1013 1014 for_each_gt(gt, xe, id) { 1015 if (xe_gt_is_media_type(gt)) 1016 continue; 1017 1018 fw_ref = xe_force_wake_get(gt_to_fw(gt), XE_FW_GT); 1019 if (!fw_ref) 1020 return; 1021 1022 xe_mmio_write32(>->mmio, XE2_TDF_CTRL, TRANSIENT_FLUSH_REQUEST); 1023 1024 /* 1025 * FIXME: We can likely do better here with our choice of 1026 * timeout. Currently we just assume the worst case, i.e. 150us, 1027 * which is believed to be sufficient to cover the worst case 1028 * scenario on current platforms if all cache entries are 1029 * transient and need to be flushed.. 1030 */ 1031 if (xe_mmio_wait32(>->mmio, XE2_TDF_CTRL, TRANSIENT_FLUSH_REQUEST, 0, 1032 150, NULL, false)) 1033 xe_gt_err_once(gt, "TD flush timeout\n"); 1034 1035 xe_force_wake_put(gt_to_fw(gt), fw_ref); 1036 } 1037 } 1038 1039 void xe_device_l2_flush(struct xe_device *xe) 1040 { 1041 struct xe_gt *gt; 1042 unsigned int fw_ref; 1043 1044 gt = xe_root_mmio_gt(xe); 1045 1046 if (!XE_WA(gt, 16023588340)) 1047 return; 1048 1049 fw_ref = xe_force_wake_get(gt_to_fw(gt), XE_FW_GT); 1050 if (!fw_ref) 1051 return; 1052 1053 spin_lock(>->global_invl_lock); 1054 1055 xe_mmio_write32(>->mmio, XE2_GLOBAL_INVAL, 0x1); 1056 if (xe_mmio_wait32(>->mmio, XE2_GLOBAL_INVAL, 0x1, 0x0, 500, NULL, true)) 1057 xe_gt_err_once(gt, "Global invalidation timeout\n"); 1058 1059 spin_unlock(>->global_invl_lock); 1060 1061 xe_force_wake_put(gt_to_fw(gt), fw_ref); 1062 } 1063 1064 /** 1065 * xe_device_td_flush() - Flush transient L3 cache entries 1066 * @xe: The device 1067 * 1068 * Display engine has direct access to memory and is never coherent with L3/L4 1069 * caches (or CPU caches), however KMD is responsible for specifically flushing 1070 * transient L3 GPU cache entries prior to the flip sequence to ensure scanout 1071 * can happen from such a surface without seeing corruption. 1072 * 1073 * Display surfaces can be tagged as transient by mapping it using one of the 1074 * various L3:XD PAT index modes on Xe2. 1075 * 1076 * Note: On non-discrete xe2 platforms, like LNL, the entire L3 cache is flushed 1077 * at the end of each submission via PIPE_CONTROL for compute/render, since SA 1078 * Media is not coherent with L3 and we want to support render-vs-media 1079 * usescases. For other engines like copy/blt the HW internally forces uncached 1080 * behaviour, hence why we can skip the TDF on such platforms. 1081 */ 1082 void xe_device_td_flush(struct xe_device *xe) 1083 { 1084 struct xe_gt *root_gt; 1085 1086 if (!IS_DGFX(xe) || GRAPHICS_VER(xe) < 20) 1087 return; 1088 1089 root_gt = xe_root_mmio_gt(xe); 1090 if (XE_WA(root_gt, 16023588340)) { 1091 /* A transient flush is not sufficient: flush the L2 */ 1092 xe_device_l2_flush(xe); 1093 } else { 1094 xe_guc_pc_apply_flush_freq_limit(&root_gt->uc.guc.pc); 1095 tdf_request_sync(xe); 1096 xe_guc_pc_remove_flush_freq_limit(&root_gt->uc.guc.pc); 1097 } 1098 } 1099 1100 u32 xe_device_ccs_bytes(struct xe_device *xe, u64 size) 1101 { 1102 return xe_device_has_flat_ccs(xe) ? 1103 DIV_ROUND_UP_ULL(size, NUM_BYTES_PER_CCS_BYTE(xe)) : 0; 1104 } 1105 1106 /** 1107 * xe_device_assert_mem_access - Inspect the current runtime_pm state. 1108 * @xe: xe device instance 1109 * 1110 * To be used before any kind of memory access. It will splat a debug warning 1111 * if the device is currently sleeping. But it doesn't guarantee in any way 1112 * that the device is going to remain awake. Xe PM runtime get and put 1113 * functions might be added to the outer bound of the memory access, while 1114 * this check is intended for inner usage to splat some warning if the worst 1115 * case has just happened. 1116 */ 1117 void xe_device_assert_mem_access(struct xe_device *xe) 1118 { 1119 xe_assert(xe, !xe_pm_runtime_suspended(xe)); 1120 } 1121 1122 void xe_device_snapshot_print(struct xe_device *xe, struct drm_printer *p) 1123 { 1124 struct xe_gt *gt; 1125 u8 id; 1126 1127 drm_printf(p, "PCI ID: 0x%04x\n", xe->info.devid); 1128 drm_printf(p, "PCI revision: 0x%02x\n", xe->info.revid); 1129 1130 for_each_gt(gt, xe, id) { 1131 drm_printf(p, "GT id: %u\n", id); 1132 drm_printf(p, "\tTile: %u\n", gt->tile->id); 1133 drm_printf(p, "\tType: %s\n", 1134 gt->info.type == XE_GT_TYPE_MAIN ? "main" : "media"); 1135 drm_printf(p, "\tIP ver: %u.%u.%u\n", 1136 REG_FIELD_GET(GMD_ID_ARCH_MASK, gt->info.gmdid), 1137 REG_FIELD_GET(GMD_ID_RELEASE_MASK, gt->info.gmdid), 1138 REG_FIELD_GET(GMD_ID_REVID, gt->info.gmdid)); 1139 drm_printf(p, "\tCS reference clock: %u\n", gt->info.reference_clock); 1140 } 1141 } 1142 1143 u64 xe_device_canonicalize_addr(struct xe_device *xe, u64 address) 1144 { 1145 return sign_extend64(address, xe->info.va_bits - 1); 1146 } 1147 1148 u64 xe_device_uncanonicalize_addr(struct xe_device *xe, u64 address) 1149 { 1150 return address & GENMASK_ULL(xe->info.va_bits - 1, 0); 1151 } 1152 1153 static void xe_device_wedged_fini(struct drm_device *drm, void *arg) 1154 { 1155 struct xe_device *xe = arg; 1156 1157 xe_pm_runtime_put(xe); 1158 } 1159 1160 /** 1161 * xe_device_declare_wedged - Declare device wedged 1162 * @xe: xe device instance 1163 * 1164 * This is a final state that can only be cleared with a module 1165 * re-probe (unbind + bind). 1166 * In this state every IOCTL will be blocked so the GT cannot be used. 1167 * In general it will be called upon any critical error such as gt reset 1168 * failure or guc loading failure. Userspace will be notified of this state 1169 * through device wedged uevent. 1170 * If xe.wedged module parameter is set to 2, this function will be called 1171 * on every single execution timeout (a.k.a. GPU hang) right after devcoredump 1172 * snapshot capture. In this mode, GT reset won't be attempted so the state of 1173 * the issue is preserved for further debugging. 1174 */ 1175 void xe_device_declare_wedged(struct xe_device *xe) 1176 { 1177 struct xe_gt *gt; 1178 u8 id; 1179 1180 if (xe->wedged.mode == 0) { 1181 drm_dbg(&xe->drm, "Wedged mode is forcibly disabled\n"); 1182 return; 1183 } 1184 1185 xe_pm_runtime_get_noresume(xe); 1186 1187 if (drmm_add_action_or_reset(&xe->drm, xe_device_wedged_fini, xe)) { 1188 drm_err(&xe->drm, "Failed to register xe_device_wedged_fini clean-up. Although device is wedged.\n"); 1189 return; 1190 } 1191 1192 if (!atomic_xchg(&xe->wedged.flag, 1)) { 1193 xe->needs_flr_on_fini = true; 1194 drm_err(&xe->drm, 1195 "CRITICAL: Xe has declared device %s as wedged.\n" 1196 "IOCTLs and executions are blocked. Only a rebind may clear the failure\n" 1197 "Please file a _new_ bug report at https://gitlab.freedesktop.org/drm/xe/kernel/issues/new\n", 1198 dev_name(xe->drm.dev)); 1199 1200 /* Notify userspace of wedged device */ 1201 drm_dev_wedged_event(&xe->drm, 1202 DRM_WEDGE_RECOVERY_REBIND | DRM_WEDGE_RECOVERY_BUS_RESET, 1203 NULL); 1204 } 1205 1206 for_each_gt(gt, xe, id) 1207 xe_gt_declare_wedged(gt); 1208 } 1209