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/error-injection.h> 11 #include <linux/units.h> 12 13 #include <drm/drm_client.h> 14 #include <drm/drm_gem_ttm_helper.h> 15 #include <drm/drm_ioctl.h> 16 #include <drm/drm_managed.h> 17 #include <drm/drm_pagemap_util.h> 18 #include <drm/drm_print.h> 19 #include <kunit/static_stub.h> 20 #include <uapi/drm/xe_drm.h> 21 22 #include "display/xe_display.h" 23 #include "instructions/xe_gpu_commands.h" 24 #include "regs/xe_gt_regs.h" 25 #include "regs/xe_regs.h" 26 #include "xe_bo.h" 27 #include "xe_bo_evict.h" 28 #include "xe_configfs.h" 29 #include "xe_debugfs.h" 30 #include "xe_cpu_bind.h" 31 #include "xe_defaults.h" 32 #include "xe_devcoredump.h" 33 #include "xe_device_sysfs.h" 34 #include "xe_dma_buf.h" 35 #include "xe_drm_client.h" 36 #include "xe_drv.h" 37 #include "xe_exec.h" 38 #include "xe_exec_queue.h" 39 #include "xe_force_wake.h" 40 #include "xe_ggtt.h" 41 #include "xe_gt.h" 42 #include "xe_gt_mcr.h" 43 #include "xe_gt_printk.h" 44 #include "xe_gt_sriov_vf.h" 45 #include "xe_guc.h" 46 #include "xe_guc_pc.h" 47 #include "xe_hw_engine_group.h" 48 #include "xe_hwmon.h" 49 #include "xe_i2c.h" 50 #include "xe_irq.h" 51 #include "xe_late_bind_fw.h" 52 #include "xe_log.h" 53 #include "xe_mmio.h" 54 #include "xe_mmio_gem.h" 55 #include "xe_module.h" 56 #include "xe_nvm.h" 57 #include "xe_oa.h" 58 #include "xe_observation.h" 59 #include "xe_pagefault.h" 60 #include "xe_pat.h" 61 #include "xe_pcode.h" 62 #include "xe_pm.h" 63 #include "xe_pmu.h" 64 #include "xe_psmi.h" 65 #include "xe_pxp.h" 66 #include "xe_query.h" 67 #include "xe_ras.h" 68 #include "xe_shrinker.h" 69 #include "xe_soc_remapper.h" 70 #include "xe_survivability_mode.h" 71 #include "xe_sriov.h" 72 #include "xe_svm.h" 73 #include "xe_sysctrl.h" 74 #include "xe_tile.h" 75 #include "xe_ttm_stolen_mgr.h" 76 #include "xe_ttm_sys_mgr.h" 77 #include "xe_vm.h" 78 #include "xe_vm_madvise.h" 79 #include "xe_vram.h" 80 #include "xe_vram_types.h" 81 #include "xe_vsec.h" 82 #include "xe_wait_user_fence.h" 83 #include "xe_wa.h" 84 85 #include <generated/xe_device_wa_oob.h> 86 #include <generated/xe_wa_oob.h> 87 88 static int xe_file_open(struct drm_device *dev, struct drm_file *file) 89 { 90 struct xe_device *xe = to_xe_device(dev); 91 struct xe_drm_client *client; 92 struct xe_file *xef; 93 int ret = -ENOMEM; 94 struct task_struct *task = NULL; 95 96 xef = kzalloc_obj(*xef); 97 if (!xef) 98 return ret; 99 100 client = xe_drm_client_alloc(); 101 if (!client) { 102 kfree(xef); 103 return ret; 104 } 105 106 xef->drm = file; 107 xef->client = client; 108 xef->xe = xe; 109 110 mutex_init(&xef->vm.lock); 111 xa_init_flags(&xef->vm.xa, XA_FLAGS_ALLOC1); 112 113 mutex_init(&xef->exec_queue.lock); 114 xa_init_flags(&xef->exec_queue.xa, XA_FLAGS_ALLOC1); 115 116 mutex_init(&xef->mmio_gem.lock); 117 118 file->driver_priv = xef; 119 kref_init(&xef->refcount); 120 121 task = get_pid_task(rcu_access_pointer(file->pid), PIDTYPE_PID); 122 if (task) { 123 xef->process_name = kstrdup(task->comm, GFP_KERNEL); 124 xef->pid = task->pid; 125 put_task_struct(task); 126 } 127 128 return 0; 129 } 130 131 static void xe_file_destroy(struct kref *ref) 132 { 133 struct xe_file *xef = container_of(ref, struct xe_file, refcount); 134 135 xa_destroy(&xef->exec_queue.xa); 136 mutex_destroy(&xef->exec_queue.lock); 137 xa_destroy(&xef->vm.xa); 138 mutex_destroy(&xef->vm.lock); 139 140 mutex_destroy(&xef->mmio_gem.lock); 141 142 xe_drm_client_put(xef->client); 143 kfree(xef->process_name); 144 kfree(xef); 145 } 146 147 /** 148 * xe_file_get() - Take a reference to the xe file object 149 * @xef: Pointer to the xe file 150 * 151 * Anyone with a pointer to xef must take a reference to the xe file 152 * object using this call. 153 * 154 * Return: xe file pointer 155 */ 156 struct xe_file *xe_file_get(struct xe_file *xef) 157 { 158 kref_get(&xef->refcount); 159 return xef; 160 } 161 162 /** 163 * xe_file_put() - Drop a reference to the xe file object 164 * @xef: Pointer to the xe file 165 * 166 * Used to drop reference to the xef object 167 */ 168 void xe_file_put(struct xe_file *xef) 169 { 170 kref_put(&xef->refcount, xe_file_destroy); 171 } 172 173 static void xe_file_close(struct drm_device *dev, struct drm_file *file) 174 { 175 struct xe_device *xe = to_xe_device(dev); 176 struct xe_file *xef = file->driver_priv; 177 struct xe_vm *vm; 178 struct xe_exec_queue *q; 179 unsigned long idx; 180 181 guard(xe_pm_runtime)(xe); 182 183 /* 184 * No need for exec_queue.lock here as there is no contention for it 185 * when FD is closing as IOCTLs presumably can't be modifying the 186 * xarray. Taking exec_queue.lock here causes undue dependency on 187 * vm->lock taken during xe_exec_queue_kill(). 188 */ 189 xa_for_each(&xef->exec_queue.xa, idx, q) { 190 if (q->vm && q->hwe->hw_engine_group) 191 xe_hw_engine_group_del_exec_queue(q->hwe->hw_engine_group, q); 192 xe_exec_queue_kill(q); 193 xe_exec_queue_put(q); 194 } 195 xa_for_each(&xef->vm.xa, idx, vm) 196 xe_vm_close_and_put(vm); 197 198 scoped_guard(mutex, &xef->mmio_gem.lock) { 199 if (xef->mmio_gem.pci_barrier) { 200 xe_mmio_gem_destroy(xef->mmio_gem.pci_barrier, file); 201 xef->mmio_gem.pci_barrier = NULL; 202 } 203 } 204 205 xe_file_put(xef); 206 } 207 208 static const struct drm_ioctl_desc xe_ioctls[] = { 209 DRM_IOCTL_DEF_DRV(XE_DEVICE_QUERY, xe_query_ioctl, DRM_RENDER_ALLOW), 210 DRM_IOCTL_DEF_DRV(XE_GEM_CREATE, xe_gem_create_ioctl, DRM_RENDER_ALLOW), 211 DRM_IOCTL_DEF_DRV(XE_GEM_MMAP_OFFSET, xe_gem_mmap_offset_ioctl, 212 DRM_RENDER_ALLOW), 213 DRM_IOCTL_DEF_DRV(XE_VM_CREATE, xe_vm_create_ioctl, DRM_RENDER_ALLOW), 214 DRM_IOCTL_DEF_DRV(XE_VM_DESTROY, xe_vm_destroy_ioctl, DRM_RENDER_ALLOW), 215 DRM_IOCTL_DEF_DRV(XE_VM_BIND, xe_vm_bind_ioctl, DRM_RENDER_ALLOW), 216 DRM_IOCTL_DEF_DRV(XE_EXEC, xe_exec_ioctl, DRM_RENDER_ALLOW), 217 DRM_IOCTL_DEF_DRV(XE_EXEC_QUEUE_CREATE, xe_exec_queue_create_ioctl, 218 DRM_RENDER_ALLOW), 219 DRM_IOCTL_DEF_DRV(XE_EXEC_QUEUE_DESTROY, xe_exec_queue_destroy_ioctl, 220 DRM_RENDER_ALLOW), 221 DRM_IOCTL_DEF_DRV(XE_EXEC_QUEUE_GET_PROPERTY, xe_exec_queue_get_property_ioctl, 222 DRM_RENDER_ALLOW), 223 DRM_IOCTL_DEF_DRV(XE_WAIT_USER_FENCE, xe_wait_user_fence_ioctl, 224 DRM_RENDER_ALLOW), 225 DRM_IOCTL_DEF_DRV(XE_OBSERVATION, xe_observation_ioctl, DRM_RENDER_ALLOW), 226 DRM_IOCTL_DEF_DRV(XE_MADVISE, xe_vm_madvise_ioctl, DRM_RENDER_ALLOW), 227 DRM_IOCTL_DEF_DRV(XE_VM_QUERY_MEM_RANGE_ATTRS, xe_vm_query_vmas_attrs_ioctl, 228 DRM_RENDER_ALLOW), 229 DRM_IOCTL_DEF_DRV(XE_EXEC_QUEUE_SET_PROPERTY, xe_exec_queue_set_property_ioctl, 230 DRM_RENDER_ALLOW), 231 DRM_IOCTL_DEF_DRV(XE_VM_GET_PROPERTY, xe_vm_get_property_ioctl, 232 DRM_RENDER_ALLOW), 233 }; 234 235 static long xe_drm_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 236 { 237 struct drm_file *file_priv = file->private_data; 238 struct xe_device *xe = to_xe_device(file_priv->minor->dev); 239 long ret; 240 241 if (xe_device_wedged(xe)) 242 return -ECANCELED; 243 244 ACQUIRE(xe_pm_runtime_ioctl, pm)(xe); 245 ret = ACQUIRE_ERR(xe_pm_runtime_ioctl, &pm); 246 if (ret >= 0) 247 ret = drm_ioctl(file, cmd, arg); 248 249 return ret; 250 } 251 252 #ifdef CONFIG_COMPAT 253 static long xe_drm_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 254 { 255 struct drm_file *file_priv = file->private_data; 256 struct xe_device *xe = to_xe_device(file_priv->minor->dev); 257 long ret; 258 259 if (xe_device_wedged(xe)) 260 return -ECANCELED; 261 262 ACQUIRE(xe_pm_runtime_ioctl, pm)(xe); 263 ret = ACQUIRE_ERR(xe_pm_runtime_ioctl, &pm); 264 if (ret >= 0) 265 ret = drm_compat_ioctl(file, cmd, arg); 266 267 return ret; 268 } 269 #else 270 /* similarly to drm_compat_ioctl, let's it be assigned to .compat_ioct unconditionally */ 271 #define xe_drm_compat_ioctl NULL 272 #endif 273 274 static int xe_mmap(struct file *filp, struct vm_area_struct *vma) 275 { 276 struct drm_file *priv = filp->private_data; 277 struct drm_device *dev = priv->minor->dev; 278 279 if (drm_dev_is_unplugged(dev)) 280 return -ENODEV; 281 282 return drm_gem_mmap(filp, vma); 283 } 284 285 static const struct file_operations xe_driver_fops = { 286 .owner = THIS_MODULE, 287 .open = drm_open, 288 .release = drm_release_noglobal, 289 .unlocked_ioctl = xe_drm_ioctl, 290 .mmap = xe_mmap, 291 .poll = drm_poll, 292 .read = drm_read, 293 .compat_ioctl = xe_drm_compat_ioctl, 294 .llseek = noop_llseek, 295 #ifdef CONFIG_PROC_FS 296 .show_fdinfo = drm_show_fdinfo, 297 #endif 298 .fop_flags = FOP_UNSIGNED_OFFSET, 299 }; 300 301 /** 302 * xe_is_xe_file() - Is the file an xe device file? 303 * @file: The file. 304 * 305 * Checks whether the file is opened against 306 * an xe device. 307 * 308 * Return: %true if an xe file, %false if not. 309 */ 310 bool xe_is_xe_file(const struct file *file) 311 { 312 return file->f_op == &xe_driver_fops; 313 } 314 315 static const struct drm_driver regular_driver = { 316 .driver_features = 317 XE_DISPLAY_DRIVER_FEATURES | 318 DRIVER_GEM | 319 DRIVER_RENDER | DRIVER_SYNCOBJ | 320 DRIVER_SYNCOBJ_TIMELINE, 321 .open = xe_file_open, 322 .postclose = xe_file_close, 323 324 .gem_prime_import = xe_gem_prime_import, 325 326 .dumb_create = xe_bo_dumb_create, 327 .dumb_map_offset = drm_gem_ttm_dumb_map_offset, 328 #ifdef CONFIG_PROC_FS 329 .show_fdinfo = xe_drm_client_fdinfo, 330 #endif 331 .ioctls = xe_ioctls, 332 .num_ioctls = ARRAY_SIZE(xe_ioctls), 333 .fops = &xe_driver_fops, 334 .name = DRIVER_NAME, 335 .desc = DRIVER_DESC, 336 .major = DRIVER_MAJOR, 337 .minor = DRIVER_MINOR, 338 .patchlevel = DRIVER_PATCHLEVEL, 339 XE_DISPLAY_DRIVER_OPS, 340 }; 341 342 #ifdef CONFIG_PCI_IOV 343 static const struct drm_ioctl_desc xe_ioctls_admin_only[] = { 344 DRM_IOCTL_DEF_DRV(XE_DEVICE_QUERY, xe_query_ioctl, DRM_RENDER_ALLOW), 345 DRM_IOCTL_DEF_DRV(XE_OBSERVATION, xe_observation_ioctl, DRM_RENDER_ALLOW), 346 }; 347 348 static const struct drm_driver admin_only_driver = { 349 .driver_features = 350 DRIVER_GEM | DRIVER_RENDER, 351 .open = xe_file_open, 352 .postclose = xe_file_close, 353 .ioctls = xe_ioctls_admin_only, 354 .num_ioctls = ARRAY_SIZE(xe_ioctls_admin_only), 355 .fops = &xe_driver_fops, 356 .name = DRIVER_NAME, 357 .desc = DRIVER_DESC, 358 .major = DRIVER_MAJOR, 359 .minor = DRIVER_MINOR, 360 .patchlevel = DRIVER_PATCHLEVEL, 361 }; 362 363 /** 364 * xe_device_is_admin_only() - Check whether device is admin only or not. 365 * @xe: the &xe_device to check 366 * 367 * Return: true if the device is admin only, false otherwise. 368 */ 369 bool xe_device_is_admin_only(const struct xe_device *xe) 370 { 371 KUNIT_STATIC_STUB_REDIRECT(xe_device_is_admin_only, xe); 372 return xe->drm.driver == &admin_only_driver; 373 } 374 #endif 375 376 static void xe_device_destroy(struct drm_device *dev, void *dummy) 377 { 378 struct xe_device *xe = to_xe_device(dev); 379 380 xe_bo_dev_fini(&xe->bo_device); 381 382 if (xe->preempt_fence_wq) 383 destroy_workqueue(xe->preempt_fence_wq); 384 385 if (xe->ordered_wq) 386 destroy_workqueue(xe->ordered_wq); 387 388 if (xe->unordered_wq) 389 destroy_workqueue(xe->unordered_wq); 390 391 if (xe->destroy_wq) 392 destroy_workqueue(xe->destroy_wq); 393 394 ttm_device_fini(&xe->ttm); 395 } 396 397 /** 398 * xe_device_create() - Create a new &xe_device instance 399 * @pdev: the parent &pci_dev 400 * 401 * Allocate and initialize a device managed Xe device structure. 402 * 403 * Return: pointer to new &xe_device on success, or ERR_PTR on failure. 404 */ 405 struct xe_device *xe_device_create(struct pci_dev *pdev) 406 { 407 const struct drm_driver *driver = ®ular_driver; 408 struct xe_device *xe; 409 int err; 410 411 #ifdef CONFIG_PCI_IOV 412 /* 413 * Since XE device is not initialized yet, read from configfs 414 * directly to decide whether we are in admin-only PF mode or not. 415 */ 416 if (xe_configfs_admin_only_pf(pdev)) 417 driver = &admin_only_driver; 418 #endif 419 420 err = aperture_remove_conflicting_pci_devices(pdev, driver->name); 421 if (err) 422 return ERR_PTR(err); 423 424 xe = devm_drm_dev_alloc(&pdev->dev, driver, struct xe_device, drm); 425 if (IS_ERR(xe)) 426 return xe; 427 428 err = xe_device_init_early(xe); 429 if (err) 430 return ERR_PTR(err); 431 432 return xe; 433 } 434 ALLOW_ERROR_INJECTION(xe_device_create, ERRNO); /* See xe_pci_probe() */ 435 436 static void xe_device_parse_modparam(struct xe_device *xe) 437 { 438 xe->atomic_svm_timeslice_ms = 5; 439 xe->min_run_period_lr_ms = 5; 440 xe->info.num_pf_work = xe_modparam.num_pf_work; 441 if (xe->info.num_pf_work < 1) 442 xe->info.num_pf_work = 1; 443 else if (xe->info.num_pf_work > XE_PAGEFAULT_WORK_MAX) 444 xe->info.num_pf_work = XE_PAGEFAULT_WORK_MAX; 445 } 446 447 /** 448 * xe_device_init_early() - Initialize a new &xe_device instance 449 * @xe: the &xe_device to initialize 450 * 451 * Return: 0 on success or a negative error code on failure. 452 */ 453 int xe_device_init_early(struct xe_device *xe) 454 { 455 int err; 456 457 err = ttm_device_init(&xe->ttm, &xe_ttm_funcs, xe->drm.dev, 458 xe->drm.anon_inode->i_mapping, 459 xe->drm.vma_offset_manager, 460 TTM_ALLOCATION_POOL_BENEFICIAL_ORDER(get_order(SZ_2M))); 461 if (err) 462 return err; 463 464 xe_bo_dev_init(&xe->bo_device); 465 err = drmm_add_action_or_reset(&xe->drm, xe_device_destroy, NULL); 466 if (err) 467 return err; 468 469 err = xe_shrinker_create(xe); 470 if (err) 471 return err; 472 473 xe_device_parse_modparam(xe); 474 475 err = xe_irq_init(xe); 476 if (err) 477 return err; 478 479 xe_validation_device_init(&xe->val); 480 481 init_waitqueue_head(&xe->ufence_wq); 482 483 init_rwsem(&xe->usm.lock); 484 485 err = xe_pagemap_shrinker_create(xe); 486 if (err) 487 return err; 488 489 xa_init_flags(&xe->usm.asid_to_vm, XA_FLAGS_ALLOC); 490 491 if (IS_ENABLED(CONFIG_DRM_XE_DEBUG)) { 492 /* Trigger a large asid and an early asid wrap. */ 493 u32 asid; 494 495 BUILD_BUG_ON(XE_MAX_ASID < 2); 496 err = xa_alloc_cyclic(&xe->usm.asid_to_vm, &asid, NULL, 497 XA_LIMIT(XE_MAX_ASID - 2, XE_MAX_ASID - 1), 498 &xe->usm.next_asid, GFP_KERNEL); 499 drm_WARN_ON(&xe->drm, err); 500 if (err >= 0) 501 xa_erase(&xe->usm.asid_to_vm, asid); 502 } 503 504 err = xe_bo_pinned_init(xe); 505 if (err) 506 return err; 507 508 xe->preempt_fence_wq = alloc_ordered_workqueue("xe-preempt-fence-wq", 509 WQ_MEM_RECLAIM); 510 xe->ordered_wq = alloc_ordered_workqueue("xe-ordered-wq", 0); 511 xe->unordered_wq = alloc_workqueue("xe-unordered-wq", WQ_PERCPU, 0); 512 xe->destroy_wq = alloc_workqueue("xe-destroy-wq", WQ_PERCPU | WQ_MEM_RECLAIM, 0); 513 if (!xe->ordered_wq || !xe->unordered_wq || 514 !xe->preempt_fence_wq || !xe->destroy_wq) { 515 /* 516 * Cleanup done in xe_device_destroy via 517 * drmm_add_action_or_reset register above 518 */ 519 drm_err(&xe->drm, "Failed to allocate xe workqueues\n"); 520 return -ENOMEM; 521 } 522 523 err = drmm_mutex_init(&xe->drm, &xe->pmt.lock); 524 if (err) 525 return err; 526 527 err = xe_pm_init_early(xe); 528 if (err) 529 return err; 530 531 return 0; 532 } 533 534 static bool xe_driver_flr_disabled(struct xe_device *xe) 535 { 536 if (IS_SRIOV_VF(xe)) 537 return true; 538 539 if (xe_mmio_read32(xe_root_tile_mmio(xe), GU_CNTL_PROTECTED) & DRIVERINT_FLR_DIS) { 540 drm_info(&xe->drm, "Driver-FLR disabled by BIOS\n"); 541 return true; 542 } 543 544 return false; 545 } 546 547 /* 548 * The driver-initiated FLR is the highest level of reset that we can trigger 549 * from within the driver. It is different from the PCI FLR in that it doesn't 550 * fully reset the SGUnit and doesn't modify the PCI config space and therefore 551 * it doesn't require a re-enumeration of the PCI BARs. However, the 552 * driver-initiated FLR does still cause a reset of both GT and display and a 553 * memory wipe of local and stolen memory, so recovery would require a full HW 554 * re-init and saving/restoring (or re-populating) the wiped memory. Since we 555 * perform the FLR as the very last action before releasing access to the HW 556 * during the driver release flow, we don't attempt recovery at all, because 557 * if/when a new instance of Xe is bound to the device it will do a full 558 * re-init anyway. 559 */ 560 static void __xe_driver_flr(struct xe_device *xe) 561 { 562 const unsigned int flr_timeout = 3 * USEC_PER_SEC; /* specs recommend a 3s wait */ 563 struct xe_mmio *mmio = xe_root_tile_mmio(xe); 564 int ret; 565 566 drm_dbg(&xe->drm, "Triggering Driver-FLR\n"); 567 568 /* 569 * Make sure any pending FLR requests have cleared by waiting for the 570 * FLR trigger bit to go to zero. Also clear GU_DEBUG's DRIVERFLR_STATUS 571 * to make sure it's not still set from a prior attempt (it's a write to 572 * clear bit). 573 * Note that we should never be in a situation where a previous attempt 574 * is still pending (unless the HW is totally dead), but better to be 575 * safe in case something unexpected happens 576 */ 577 ret = xe_mmio_wait32(mmio, GU_CNTL, DRIVERFLR, 0, flr_timeout, NULL, false); 578 if (ret) { 579 drm_err(&xe->drm, "Driver-FLR-prepare wait for ready failed! %d\n", ret); 580 return; 581 } 582 xe_mmio_write32(mmio, GU_DEBUG, DRIVERFLR_STATUS); 583 584 /* Trigger the actual Driver-FLR */ 585 xe_mmio_rmw32(mmio, GU_CNTL, 0, DRIVERFLR); 586 587 /* Wait for hardware teardown to complete */ 588 ret = xe_mmio_wait32(mmio, GU_CNTL, DRIVERFLR, 0, flr_timeout, NULL, false); 589 if (ret) { 590 drm_err(&xe->drm, "Driver-FLR-teardown wait completion failed! %d\n", ret); 591 return; 592 } 593 594 /* Wait for hardware/firmware re-init to complete */ 595 ret = xe_mmio_wait32(mmio, GU_DEBUG, DRIVERFLR_STATUS, DRIVERFLR_STATUS, 596 flr_timeout, NULL, false); 597 if (ret) { 598 drm_err(&xe->drm, "Driver-FLR-reinit wait completion failed! %d\n", ret); 599 return; 600 } 601 602 /* Clear sticky completion status */ 603 xe_mmio_write32(mmio, GU_DEBUG, DRIVERFLR_STATUS); 604 } 605 606 static void xe_driver_flr(struct xe_device *xe) 607 { 608 if (xe_driver_flr_disabled(xe)) 609 return; 610 611 __xe_driver_flr(xe); 612 } 613 614 static void xe_driver_flr_fini(void *arg) 615 { 616 struct xe_device *xe = arg; 617 618 if (xe->needs_flr_on_fini) 619 xe_driver_flr(xe); 620 } 621 622 static void xe_device_sanitize(void *arg) 623 { 624 struct xe_device *xe = arg; 625 struct xe_gt *gt; 626 u8 id; 627 628 for_each_gt(gt, xe, id) 629 xe_gt_sanitize(gt); 630 } 631 632 static int xe_set_dma_info(struct xe_device *xe) 633 { 634 unsigned int mask_size = xe->info.dma_mask_size; 635 int err; 636 637 dma_set_max_seg_size(xe->drm.dev, xe_sg_segment_size(xe->drm.dev)); 638 639 err = dma_set_mask(xe->drm.dev, DMA_BIT_MASK(mask_size)); 640 if (err) 641 goto mask_err; 642 643 err = dma_set_coherent_mask(xe->drm.dev, DMA_BIT_MASK(mask_size)); 644 if (err) 645 goto mask_err; 646 647 return 0; 648 649 mask_err: 650 drm_err(&xe->drm, "Can't set DMA mask/consistent mask (%d)\n", err); 651 return err; 652 } 653 654 static void assert_lmem_ready(struct xe_device *xe) 655 { 656 if (!IS_DGFX(xe) || IS_SRIOV_VF(xe)) 657 return; 658 659 xe_assert(xe, xe_mmio_read32(xe_root_tile_mmio(xe), GU_CNTL) & 660 LMEM_INIT); 661 } 662 663 static void vf_update_device_info(struct xe_device *xe) 664 { 665 xe_assert(xe, IS_SRIOV_VF(xe)); 666 /* disable features that are not available/applicable to VFs */ 667 xe->info.probe_display = 0; 668 xe->info.has_heci_cscfi = 0; 669 xe->info.has_heci_gscfi = 0; 670 xe->info.has_i2c = 0; 671 xe->info.has_late_bind = 0; 672 xe->info.skip_guc_pc = 1; 673 xe->info.skip_pcode = 1; 674 xe->info.has_drm_ras = false; 675 xe->info.has_device_uid = false; 676 } 677 678 static int xe_device_vram_alloc(struct xe_device *xe) 679 { 680 struct xe_vram_region *vram; 681 682 if (!IS_DGFX(xe)) 683 return 0; 684 685 vram = drmm_kzalloc(&xe->drm, sizeof(*vram), GFP_KERNEL); 686 if (!vram) 687 return -ENOMEM; 688 689 xe->mem.vram = vram; 690 return 0; 691 } 692 693 static void wedged_work(struct work_struct *work); 694 static void xe_device_wedged_fini(struct drm_device *drm, void *arg); 695 696 static int xe_device_wedged_init(struct xe_device *xe) 697 { 698 INIT_WORK(&xe->wedged.work, wedged_work); 699 700 return drmm_add_action_or_reset(&xe->drm, xe_device_wedged_fini, xe); 701 } 702 703 /** 704 * xe_device_probe_early: Device early probe 705 * @xe: xe device instance 706 * 707 * Initialize MMIO resources that don't require any 708 * knowledge about tile count. Also initialize pcode and 709 * check vram initialization on root tile. 710 * 711 * Return: 0 on success, error code on failure 712 */ 713 int xe_device_probe_early(struct xe_device *xe) 714 { 715 int err; 716 717 xe_wa_device_init(xe); 718 xe_wa_process_device_oob(xe); 719 720 err = xe_mmio_probe_early(xe); 721 if (err) 722 return err; 723 724 xe_sriov_probe_early(xe); 725 726 if (xe_device_is_admin_only(xe) && !IS_SRIOV_PF(xe)) { 727 xe_err(xe, "Can't run Admin-only mode without SR-IOV PF mode!\n"); 728 return -ENODEV; 729 } 730 731 if (IS_SRIOV_VF(xe)) 732 vf_update_device_info(xe); 733 734 /* 735 * Check for pcode uncore_init status to confirm if the SoC 736 * initialization is complete. Until done, any MMIO or lmem access from 737 * the driver will be blocked 738 */ 739 err = xe_pcode_probe_early(xe); 740 if (err || xe_survivability_mode_is_requested(xe)) { 741 int save_err = err; 742 743 /* 744 * Try to leave device in survivability mode if device is 745 * possible, but still return the previous error for error 746 * propagation 747 */ 748 err = xe_survivability_mode_boot_enable(xe); 749 if (err) 750 return err; 751 752 return save_err; 753 } 754 755 /* 756 * Make sure the lmem is initialized and ready to use. xe_pcode_ready() 757 * is flagged after full initialization is complete. Assert if lmem is 758 * not initialized. 759 */ 760 assert_lmem_ready(xe); 761 762 xe->wedged.mode = xe_device_validate_wedged_mode(xe, xe_modparam.wedged_mode) ? 763 XE_DEFAULT_WEDGED_MODE : xe_modparam.wedged_mode; 764 drm_dbg(&xe->drm, "wedged_mode: setting mode (%u) %s\n", 765 xe->wedged.mode, xe_wedged_mode_to_string(xe->wedged.mode)); 766 767 err = xe_device_vram_alloc(xe); 768 if (err) 769 return err; 770 771 err = xe_device_wedged_init(xe); 772 if (err) 773 return err; 774 775 return 0; 776 } 777 ALLOW_ERROR_INJECTION(xe_device_probe_early, ERRNO); /* See xe_pci_probe() */ 778 779 static int probe_has_flat_ccs(struct xe_device *xe) 780 { 781 struct xe_gt *gt; 782 u32 reg; 783 784 /* Always enabled/disabled, no runtime check to do */ 785 if (GRAPHICS_VER(xe) < 20 || !xe->info.has_flat_ccs || IS_SRIOV_VF(xe)) 786 return 0; 787 788 gt = xe_root_mmio_gt(xe); 789 if (!gt) 790 return 0; 791 792 CLASS(xe_force_wake, fw_ref)(gt_to_fw(gt), XE_FW_GT); 793 if (!fw_ref.domains) 794 return -ETIMEDOUT; 795 796 reg = xe_gt_mcr_unicast_read_any(gt, XE2_FLAT_CCS_BASE_RANGE_LOWER); 797 xe->info.has_flat_ccs = (reg & XE2_FLAT_CCS_ENABLE); 798 799 if (!xe->info.has_flat_ccs) 800 drm_dbg(&xe->drm, 801 "Flat CCS has been disabled in bios, May lead to performance impact"); 802 803 return 0; 804 } 805 806 /* 807 * Detect if the driver is being run on pre-production hardware. We don't 808 * keep workarounds for pre-production hardware long term, so print an 809 * error and add taint if we're being loaded on a pre-production platform 810 * for which the pre-prod workarounds have already been removed. 811 * 812 * The general policy is that we'll remove any workarounds that only apply to 813 * pre-production hardware around the time force_probe restrictions are lifted 814 * for a platform of the next major IP generation (for example, Xe2 pre-prod 815 * workarounds should be removed around the time the first Xe3 platforms have 816 * force_probe lifted). 817 */ 818 static void detect_preproduction_hw(struct xe_device *xe) 819 { 820 struct xe_gt *gt; 821 int id; 822 823 /* 824 * SR-IOV VFs don't have access to the FUSE2 register, so we can't 825 * check pre-production status there. But the host OS will notice 826 * and report the pre-production status, which should be enough to 827 * help us catch mistaken use of pre-production hardware. 828 */ 829 if (IS_SRIOV_VF(xe)) 830 return; 831 832 /* 833 * The "SW_CAP" fuse contains a bit indicating whether the device is a 834 * production or pre-production device. This fuse is reflected through 835 * the GT "FUSE2" register, even though the contents of the fuse are 836 * not GT-specific. Every GT's reflection of this fuse should show the 837 * same value, so we'll just use the first available GT for lookup. 838 */ 839 for_each_gt(gt, xe, id) 840 break; 841 842 if (!gt) 843 return; 844 845 CLASS(xe_force_wake, fw_ref)(gt_to_fw(gt), XE_FW_GT); 846 if (!xe_force_wake_ref_has_domain(fw_ref.domains, XE_FW_GT)) { 847 xe_gt_err(gt, "Forcewake failure; cannot determine production/pre-production hw status.\n"); 848 return; 849 } 850 851 if (xe_mmio_read32(>->mmio, FUSE2) & PRODUCTION_HW) 852 return; 853 854 xe_info(xe, "Pre-production hardware detected.\n"); 855 if (!xe->info.has_pre_prod_wa) { 856 xe_err(xe, "Pre-production workarounds for this platform have already been removed.\n"); 857 add_taint(TAINT_MACHINE_CHECK, LOCKDEP_STILL_OK); 858 } 859 } 860 861 static void xe_device_wedged_fini(struct drm_device *drm, void *arg) 862 { 863 struct xe_device *xe = arg; 864 865 if (atomic_read(&xe->wedged.flag)) 866 xe_pm_runtime_put(xe); 867 } 868 869 #ifdef CONFIG_DRM_XE_DEBUG_PAGE_SIZE 870 static int xe_debug_page_size_alloc_ctrl_init(struct xe_device *xe) 871 { 872 int err; 873 874 err = drmm_mutex_init(&xe->drm, &xe->page_size_alloc_ctrl.lock); 875 if (err) 876 return err; 877 878 xe->page_size_alloc_ctrl.mode = XE_PAGE_SIZE_ALLOC_CTRL_MODE_NONE; 879 xe->page_size_alloc_ctrl.cur_index = 0; 880 881 return 0; 882 } 883 #else 884 static int xe_debug_page_size_alloc_ctrl_init(struct xe_device *xe) 885 { 886 return 0; 887 } 888 #endif 889 890 static void xe_uid_probe(struct xe_device *xe) 891 { 892 if (xe->info.has_device_uid) 893 xe->device_uid = xe_mmio_read64_2x32(xe_root_tile_mmio(xe), CRI_DEVICE_UID); 894 } 895 896 int xe_device_probe(struct xe_device *xe) 897 { 898 struct xe_tile *tile; 899 struct xe_gt *gt; 900 int err; 901 u8 id; 902 903 xe_uid_probe(xe); 904 905 err = xe_pat_init_early(xe); 906 if (err) 907 return err; 908 909 err = xe_sriov_init(xe); 910 if (err) 911 return err; 912 913 xe->info.mem_region_mask = 1; 914 915 err = xe_set_dma_info(xe); 916 if (err) 917 return err; 918 919 err = xe_mmio_probe_tiles(xe); 920 if (err) 921 return err; 922 923 for_each_gt(gt, xe, id) { 924 err = xe_gt_init_early(gt); 925 if (err) 926 return err; 927 } 928 929 /* 930 * Wa_16029380221: The affected GT will always use non-coherent 931 * access to page tables, so we must do uncached writes from the 932 * CPU. 933 */ 934 for_each_gt(gt, xe, id) 935 if (XE_GT_WA(gt, 16029380221)) 936 xe->info.has_cached_pt = false; 937 938 for_each_tile(tile, xe, id) { 939 err = xe_ggtt_init_early(tile->mem.ggtt); 940 if (err) 941 return err; 942 } 943 944 /* 945 * From here on, if a step fails, make sure a Driver-FLR is triggereed 946 */ 947 err = devm_add_action_or_reset(xe->drm.dev, xe_driver_flr_fini, xe); 948 if (err) 949 return err; 950 951 err = probe_has_flat_ccs(xe); 952 if (err) 953 return err; 954 955 err = xe_vram_probe(xe); 956 if (err) 957 return err; 958 959 for_each_tile(tile, xe, id) { 960 err = xe_tile_init_noalloc(tile); 961 if (err) 962 return err; 963 } 964 965 /* 966 * Allow allocations only now to ensure xe_display_init_early() 967 * is the first to allocate, always. 968 */ 969 err = xe_ttm_sys_mgr_init(xe); 970 if (err) 971 return err; 972 973 /* Allocate and map stolen after potential VRAM resize */ 974 err = xe_ttm_stolen_mgr_init(xe); 975 if (err) 976 return err; 977 978 err = xe_soc_remapper_init(xe); 979 if (err) 980 return err; 981 982 err = xe_sysctrl_init(xe); 983 if (err) 984 return err; 985 986 xe_ras_init(xe); 987 988 /* 989 * Now that GT is initialized (TTM in particular), 990 * we can try to init display, and inherit the initial fb. 991 * This is the reason the first allocation needs to be done 992 * inside display. 993 */ 994 err = xe_display_init_early(xe); 995 if (err) 996 return err; 997 998 err = xe_vram_reserve_memtest_bo(xe); 999 if (err) 1000 return err; 1001 1002 for_each_tile(tile, xe, id) { 1003 err = xe_tile_init(tile); 1004 if (err) 1005 return err; 1006 } 1007 1008 err = xe_irq_install(xe); 1009 if (err) 1010 return err; 1011 1012 for_each_gt(gt, xe, id) { 1013 err = xe_gt_init(gt); 1014 if (err) 1015 return err; 1016 } 1017 1018 err = xe_vram_memtest(xe); 1019 if (err) 1020 return err; 1021 1022 err = xe_cpu_bind_init(xe); 1023 if (err) 1024 return err; 1025 1026 err = xe_pagefault_init(xe); 1027 if (err) 1028 return err; 1029 1030 if (xe->tiles->media_gt && 1031 XE_GT_WA(xe->tiles->media_gt, 15015404425_disable)) 1032 XE_DEVICE_WA_DISABLE(xe, 15015404425); 1033 1034 err = xe_devcoredump_init(xe); 1035 if (err) 1036 return err; 1037 1038 xe_nvm_init(xe); 1039 1040 err = xe_heci_gsc_init(xe); 1041 if (err) 1042 return err; 1043 1044 err = xe_late_bind_init(&xe->late_bind); 1045 if (err) 1046 return err; 1047 1048 err = xe_oa_init(xe); 1049 if (err) 1050 return err; 1051 1052 err = xe_display_init(xe); 1053 if (err) 1054 return err; 1055 1056 err = xe_pxp_init(xe); 1057 if (err) 1058 return err; 1059 1060 err = xe_psmi_init(xe); 1061 if (err) 1062 return err; 1063 1064 err = xe_debug_page_size_alloc_ctrl_init(xe); 1065 if (err) 1066 return err; 1067 1068 err = drm_dev_register(&xe->drm, 0); 1069 if (err) 1070 return err; 1071 1072 xe_display_register(xe); 1073 1074 err = xe_oa_register(xe); 1075 if (err) 1076 goto err_unregister_display; 1077 1078 err = xe_pmu_register(&xe->pmu); 1079 if (err) 1080 goto err_unregister_display; 1081 1082 err = xe_device_sysfs_init(xe); 1083 if (err) 1084 goto err_unregister_display; 1085 1086 xe_debugfs_register(xe); 1087 1088 err = xe_hwmon_register(xe); 1089 if (err) 1090 goto err_unregister_display; 1091 1092 err = xe_i2c_probe(xe); 1093 if (err) 1094 goto err_unregister_display; 1095 1096 for_each_gt(gt, xe, id) 1097 xe_gt_sanitize_freq(gt); 1098 1099 xe_vsec_init(xe); 1100 1101 err = xe_sriov_init_late(xe); 1102 if (err) 1103 goto err_unregister_display; 1104 1105 detect_preproduction_hw(xe); 1106 1107 /* 1108 * Process and log any errors detected by hardware. Possible results can 1109 * include declaring the device as wedged, which must be done only after 1110 * xe_device_wedged_fini() is registered. 1111 */ 1112 xe_ras_process_errors(xe); 1113 1114 err = devm_add_action_or_reset(xe->drm.dev, xe_device_sanitize, xe); 1115 if (err) 1116 goto err_unregister_display; 1117 1118 return 0; 1119 1120 err_unregister_display: 1121 xe_display_unregister(xe); 1122 drm_dev_unregister(&xe->drm); 1123 1124 return err; 1125 } 1126 1127 void xe_device_remove(struct xe_device *xe) 1128 { 1129 xe_display_unregister(xe); 1130 1131 drm_dev_unplug(&xe->drm); 1132 1133 xe_bo_pci_dev_remove_all(xe); 1134 } 1135 1136 void xe_device_shutdown(struct xe_device *xe) 1137 { 1138 struct xe_gt *gt; 1139 u8 id; 1140 1141 drm_dbg(&xe->drm, "Shutting down device\n"); 1142 1143 xe_display_shutdown(xe); 1144 1145 xe_irq_suspend(xe); 1146 1147 for_each_gt(gt, xe, id) 1148 xe_gt_shutdown(gt); 1149 1150 xe_display_shutdown_late(xe); 1151 1152 if (!xe_driver_flr_disabled(xe)) { 1153 /* BOOM! */ 1154 __xe_driver_flr(xe); 1155 } 1156 } 1157 1158 /** 1159 * xe_device_wmb() - Device specific write memory barrier 1160 * @xe: the &xe_device 1161 * 1162 * While wmb() is sufficient for a barrier if we use system memory, on discrete 1163 * platforms with device memory we additionally need to issue a register write. 1164 * Since it doesn't matter which register we write to, use the read-only VF_CAP 1165 * register that is also marked as accessible by the VFs. 1166 */ 1167 void xe_device_wmb(struct xe_device *xe) 1168 { 1169 wmb(); 1170 if (IS_DGFX(xe)) 1171 xe_mmio_write32(xe_root_tile_mmio(xe), VF_CAP_REG, 0); 1172 } 1173 1174 /* 1175 * Issue a TRANSIENT_FLUSH_REQUEST and wait for completion on each gt. 1176 */ 1177 static void tdf_request_sync(struct xe_device *xe) 1178 { 1179 struct xe_gt *gt; 1180 u8 id; 1181 1182 for_each_gt_with_type(gt, xe, id, BIT(XE_GT_TYPE_MAIN)) { 1183 CLASS(xe_force_wake, fw_ref)(gt_to_fw(gt), XE_FW_GT); 1184 if (!fw_ref.domains) 1185 return; 1186 1187 xe_mmio_write32(>->mmio, XE2_TDF_CTRL, TRANSIENT_FLUSH_REQUEST); 1188 1189 /* 1190 * FIXME: We can likely do better here with our choice of 1191 * timeout. Currently we just assume the worst case, i.e. 150us, 1192 * which is believed to be sufficient to cover the worst case 1193 * scenario on current platforms if all cache entries are 1194 * transient and need to be flushed.. 1195 */ 1196 if (xe_mmio_wait32(>->mmio, XE2_TDF_CTRL, TRANSIENT_FLUSH_REQUEST, 0, 1197 300, NULL, false)) 1198 xe_gt_err_once(gt, "TD flush timeout\n"); 1199 } 1200 } 1201 1202 /** 1203 * xe_device_is_l2_flush_optimized - if L2 flush is optimized by HW 1204 * @xe: The device to check. 1205 * 1206 * Return: true if the HW device optimizing L2 flush, false otherwise. 1207 */ 1208 bool xe_device_is_l2_flush_optimized(struct xe_device *xe) 1209 { 1210 /* XA is *always* flushed, like at the end-of-submssion (and maybe other 1211 * places), just that internally as an optimisation hw doesn't need to make 1212 * that a full flush (which will also include XA) when Media is 1213 * off/powergated, since it doesn't need to worry about GT caches vs Media 1214 * coherency, and only CPU vs GPU coherency, so can make that flush a 1215 * targeted XA flush, since stuff tagged with XA now means it's shared with 1216 * the CPU. The main implication is that we now need to somehow flush non-XA before 1217 * freeing system memory pages, otherwise dirty cachelines could be flushed after the free 1218 * (like if Media suddenly turns on and does a full flush) 1219 */ 1220 if (GRAPHICS_VER(xe) >= 35 && !IS_DGFX(xe)) 1221 return true; 1222 return false; 1223 } 1224 1225 void xe_device_l2_flush(struct xe_device *xe, bool force) 1226 { 1227 struct xe_gt *gt; 1228 1229 gt = xe_root_mmio_gt(xe); 1230 if (!gt) 1231 return; 1232 1233 if (!force && !XE_GT_WA(gt, 16023588340)) 1234 return; 1235 1236 CLASS(xe_force_wake, fw_ref)(gt_to_fw(gt), XE_FW_GT); 1237 if (!fw_ref.domains) 1238 return; 1239 1240 spin_lock(>->global_invl_lock); 1241 1242 xe_mmio_write32(>->mmio, XE2_GLOBAL_INVAL, 0x1); 1243 if (xe_mmio_wait32(>->mmio, XE2_GLOBAL_INVAL, 0x1, 0x0, 1000, NULL, true)) 1244 xe_gt_err_once(gt, "Global invalidation timeout\n"); 1245 1246 spin_unlock(>->global_invl_lock); 1247 } 1248 1249 /** 1250 * xe_device_td_flush() - Flush transient L3 cache entries 1251 * @xe: The device 1252 * 1253 * Display engine has direct access to memory and is never coherent with L3/L4 1254 * caches (or CPU caches), however KMD is responsible for specifically flushing 1255 * transient L3 GPU cache entries prior to the flip sequence to ensure scanout 1256 * can happen from such a surface without seeing corruption. 1257 * 1258 * Display surfaces can be tagged as transient by mapping it using one of the 1259 * various L3:XD PAT index modes on Xe2. 1260 * 1261 * Note: On non-discrete xe2 platforms, like LNL, the entire L3 cache is flushed 1262 * at the end of each submission via PIPE_CONTROL for compute/render, since SA 1263 * Media is not coherent with L3 and we want to support render-vs-media 1264 * usescases. For other engines like copy/blt the HW internally forces uncached 1265 * behaviour, hence why we can skip the TDF on such platforms. 1266 */ 1267 void xe_device_td_flush(struct xe_device *xe) 1268 { 1269 struct xe_gt *root_gt; 1270 1271 /* 1272 * From Xe3p onward the HW takes care of flush of TD entries also along 1273 * with flushing XA entries, which will be at the usual sync points, 1274 * like at the end of submission, so no manual flush is needed here. 1275 */ 1276 if (GRAPHICS_VER(xe) >= 35) 1277 return; 1278 1279 if (!IS_DGFX(xe) || GRAPHICS_VER(xe) < 20) 1280 return; 1281 1282 root_gt = xe_root_mmio_gt(xe); 1283 if (!root_gt) 1284 return; 1285 1286 if (XE_GT_WA(root_gt, 16023588340)) { 1287 /* A transient flush is not sufficient: flush the L2 */ 1288 xe_device_l2_flush(xe, false); 1289 } else { 1290 xe_guc_pc_apply_flush_freq_limit(&root_gt->uc.guc.pc); 1291 tdf_request_sync(xe); 1292 xe_guc_pc_remove_flush_freq_limit(&root_gt->uc.guc.pc); 1293 } 1294 } 1295 1296 u32 xe_device_ccs_bytes(struct xe_device *xe, u64 size) 1297 { 1298 return xe_device_has_flat_ccs(xe) ? 1299 DIV_ROUND_UP_ULL(size, NUM_BYTES_PER_CCS_BYTE(xe)) : 0; 1300 } 1301 1302 /** 1303 * xe_device_assert_mem_access - Inspect the current runtime_pm state. 1304 * @xe: xe device instance 1305 * 1306 * To be used before any kind of memory access. It will splat a debug warning 1307 * if the device is currently sleeping. But it doesn't guarantee in any way 1308 * that the device is going to remain awake. Xe PM runtime get and put 1309 * functions might be added to the outer bound of the memory access, while 1310 * this check is intended for inner usage to splat some warning if the worst 1311 * case has just happened. 1312 */ 1313 void xe_device_assert_mem_access(struct xe_device *xe) 1314 { 1315 xe_assert(xe, !xe_pm_runtime_suspended(xe)); 1316 } 1317 1318 void xe_device_snapshot_print(struct xe_device *xe, struct drm_printer *p) 1319 { 1320 struct xe_gt *gt; 1321 u8 id; 1322 1323 drm_printf(p, "PCI ID: 0x%04x\n", xe->info.devid); 1324 drm_printf(p, "PCI revision: 0x%02x\n", xe->info.revid); 1325 1326 for_each_gt(gt, xe, id) { 1327 drm_printf(p, "GT id: %u\n", id); 1328 drm_printf(p, "\tTile: %u\n", gt->tile->id); 1329 drm_printf(p, "\tType: %s\n", 1330 gt->info.type == XE_GT_TYPE_MAIN ? "main" : "media"); 1331 drm_printf(p, "\tIP ver: %u.%u.%u\n", 1332 REG_FIELD_GET(GMD_ID_ARCH_MASK, gt->info.gmdid), 1333 REG_FIELD_GET(GMD_ID_RELEASE_MASK, gt->info.gmdid), 1334 REG_FIELD_GET(GMD_ID_REVID, gt->info.gmdid)); 1335 drm_printf(p, "\tCS reference clock: %u\n", gt->info.reference_clock); 1336 } 1337 } 1338 1339 u64 xe_device_canonicalize_addr(struct xe_device *xe, u64 address) 1340 { 1341 return sign_extend64(address, xe->info.va_bits - 1); 1342 } 1343 1344 u64 xe_device_uncanonicalize_addr(struct xe_device *xe, u64 address) 1345 { 1346 return address & GENMASK_ULL(xe->info.va_bits - 1, 0); 1347 } 1348 1349 /** 1350 * DOC: Xe Device Wedging 1351 * 1352 * Xe driver uses drm device wedged uevent as documented in Documentation/gpu/drm-uapi.rst. 1353 * When device is in wedged state, every IOCTL will be blocked and GT cannot 1354 * be used. The conditions under which the driver declares the device wedged 1355 * depend on the wedged mode configuration (see &enum xe_wedged_mode). The 1356 * default recovery method for a wedged state is rebind/bus-reset. 1357 * 1358 * Another recovery method is vendor-specific. Below are the cases that send 1359 * ``WEDGED=vendor-specific`` recovery method in drm device wedged uevent. 1360 * 1361 * Case: Firmware Flash 1362 * -------------------- 1363 * 1364 * Identification Hint 1365 * +++++++++++++++++++ 1366 * 1367 * ``WEDGED=vendor-specific`` drm device wedged uevent with 1368 * :ref:`Runtime Survivability mode <xe-survivability-mode>` is used to notify 1369 * admin/userspace consumer about the need for a firmware flash. 1370 * 1371 * Recovery Procedure 1372 * ++++++++++++++++++ 1373 * 1374 * Once ``WEDGED=vendor-specific`` drm device wedged uevent is received, follow 1375 * the below steps 1376 * 1377 * - Check Runtime Survivability mode sysfs. 1378 * If enabled, firmware flash is required to recover the device. 1379 * 1380 * /sys/bus/pci/devices/<device>/survivability_mode 1381 * 1382 * - Admin/userspace consumer can use firmware flashing tools like fwupd to flash 1383 * firmware and restore device to normal operation. 1384 */ 1385 1386 static void wedged_work(struct work_struct *work) 1387 { 1388 struct xe_device *xe = container_of(work, struct xe_device, wedged.work); 1389 1390 /* Notify userspace of wedged device */ 1391 drm_dev_wedged_event(&xe->drm, xe->wedged.method, NULL); 1392 } 1393 1394 #define WEDGED_URL "https://docs.kernel.org/gpu/drm-uapi.html#device-wedging" 1395 #define XE_BUG_URL "https://gitlab.freedesktop.org/drm/xe/kernel/issues/new" 1396 1397 /** 1398 * xe_device_declare_wedged - Declare device wedged 1399 * @xe: xe device instance 1400 * @method: method(s) to be used for recovery 1401 * 1402 * This is a final state that can only be cleared with the recovery method 1403 * specified in the drm wedged uevent. 1404 * 1405 * In this state every IOCTL will be blocked so the GT cannot be used. 1406 * In general it will be called upon any critical error such as gt reset 1407 * failure or guc loading failure. Userspace will be notified of this state 1408 * through device wedged uevent. 1409 * If xe.wedged module parameter is set to 2, this function will be called 1410 * on every single execution timeout (a.k.a. GPU hang) right after devcoredump 1411 * snapshot capture. In this mode, GT reset won't be attempted so the state of 1412 * the issue is preserved for further debugging. 1413 */ 1414 void xe_device_declare_wedged(struct xe_device *xe, unsigned long method) 1415 { 1416 struct xe_gt *gt; 1417 u8 id; 1418 1419 if (xe->wedged.mode == XE_WEDGED_MODE_NEVER) { 1420 drm_dbg(&xe->drm, "Wedged mode is forcibly disabled\n"); 1421 return; 1422 } 1423 1424 if (!atomic_xchg(&xe->wedged.flag, 1)) { 1425 xe->needs_flr_on_fini = true; 1426 xe_pm_runtime_get_noresume(xe); 1427 1428 xe_log_err_fatal(xe, WEDGED, -EIO, "Device declared wedged!\n"); 1429 xe_err_once(xe, "IOCTLs and executions are now blocked!\n" 1430 "For recovery procedure, refer to %s\n" 1431 "Please file a _new_ bug report at %s\n", 1432 WEDGED_URL, XE_BUG_URL); 1433 1434 for_each_initialized_gt(gt, xe, id) 1435 xe_gt_declare_wedged(gt); 1436 1437 /* 1438 * XE_WEDGED_MODE_UPON_ANY_HANG_NO_RESET is intended for debugging 1439 * hangs, so wedge the device with 'none' recovery method and have 1440 * it available to the user for debugging. 1441 */ 1442 if (xe->wedged.mode == XE_WEDGED_MODE_UPON_ANY_HANG_NO_RESET) 1443 xe->wedged.method = DRM_WEDGE_RECOVERY_NONE; 1444 else 1445 xe->wedged.method = method; 1446 1447 schedule_work(&xe->wedged.work); 1448 } 1449 } 1450 1451 /** 1452 * xe_device_validate_wedged_mode - Check if given mode is supported 1453 * @xe: the &xe_device 1454 * @mode: requested mode to validate 1455 * 1456 * Check whether the provided wedged mode is supported. 1457 * 1458 * Return: 0 if mode is supported, error code otherwise. 1459 */ 1460 int xe_device_validate_wedged_mode(struct xe_device *xe, unsigned int mode) 1461 { 1462 if (mode > XE_WEDGED_MODE_UPON_ANY_HANG_NO_RESET) { 1463 drm_dbg(&xe->drm, "wedged_mode: invalid value (%u)\n", mode); 1464 return -EINVAL; 1465 } else if (mode == XE_WEDGED_MODE_UPON_ANY_HANG_NO_RESET && (IS_SRIOV_VF(xe) || 1466 (IS_SRIOV_PF(xe) && !IS_ENABLED(CONFIG_DRM_XE_DEBUG)))) { 1467 drm_dbg(&xe->drm, "wedged_mode: (%u) %s mode is not supported for %s\n", 1468 mode, xe_wedged_mode_to_string(mode), 1469 xe_sriov_mode_to_string(xe_device_sriov_mode(xe))); 1470 return -EPERM; 1471 } 1472 1473 return 0; 1474 } 1475 1476 /** 1477 * xe_wedged_mode_to_string - Convert enum value to string. 1478 * @mode: the &xe_wedged_mode to convert 1479 * 1480 * Returns: wedged mode as a user friendly string. 1481 */ 1482 const char *xe_wedged_mode_to_string(enum xe_wedged_mode mode) 1483 { 1484 switch (mode) { 1485 case XE_WEDGED_MODE_NEVER: 1486 return "never"; 1487 case XE_WEDGED_MODE_UPON_CRITICAL_ERROR: 1488 return "upon-critical-error"; 1489 case XE_WEDGED_MODE_UPON_ANY_HANG_NO_RESET: 1490 return "upon-any-hang-no-reset"; 1491 default: 1492 return "<invalid>"; 1493 } 1494 } 1495 1496 /** 1497 * xe_device_asid_to_vm() - Find VM from ASID 1498 * @xe: the &xe_device 1499 * @asid: Address space ID 1500 * 1501 * Find a VM from ASID and take a reference to VM which caller must drop. 1502 * Reclaim safe. 1503 * 1504 * Return: VM on success, ERR_PTR on failure 1505 */ 1506 struct xe_vm *xe_device_asid_to_vm(struct xe_device *xe, u32 asid) 1507 { 1508 struct xe_vm *vm; 1509 1510 down_read(&xe->usm.lock); 1511 vm = xa_load(&xe->usm.asid_to_vm, asid); 1512 if (vm) 1513 xe_vm_get(vm); 1514 else 1515 vm = ERR_PTR(-EINVAL); 1516 up_read(&xe->usm.lock); 1517 1518 return vm; 1519 } 1520