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