1 // SPDX-License-Identifier: MIT
3 * Copyright © 2021 Intel Corporation
8 #include <linux/units.h>
10 #include <drm/drm_aperture.h>
11 #include <drm/drm_atomic_helper.h>
12 #include <drm/drm_gem_ttm_helper.h>
13 #include <drm/drm_ioctl.h>
14 #include <drm/drm_managed.h>
15 #include <drm/drm_print.h>
16 #include <drm/xe_drm.h>
18 #include "regs/xe_gt_regs.h"
19 #include "regs/xe_regs.h"
21 #include "xe_debugfs.h"
22 #include "xe_display.h"
23 #include "xe_dma_buf.h"
24 #include "xe_drm_client.h"
26 #include "xe_exec_queue.h"
30 #include "xe_gt_mcr.h"
33 #include "xe_module.h"
39 #include "xe_ttm_stolen_mgr.h"
40 #include "xe_ttm_sys_mgr.h"
42 #include "xe_wait_user_fence.h"
46 struct lockdep_map xe_device_mem_access_lockdep_map = {
47 .name = "xe_device_mem_access_lockdep_map"
51 static int xe_file_open(struct drm_device *dev, struct drm_file *file)
53 struct xe_device *xe = to_xe_device(dev);
54 struct xe_drm_client *client;
58 xef = kzalloc(sizeof(*xef), GFP_KERNEL);
62 client = xe_drm_client_alloc();
72 mutex_init(&xef->vm.lock);
73 xa_init_flags(&xef->vm.xa, XA_FLAGS_ALLOC1);
75 mutex_init(&xef->exec_queue.lock);
76 xa_init_flags(&xef->exec_queue.xa, XA_FLAGS_ALLOC1);
78 spin_lock(&xe->clients.lock);
80 spin_unlock(&xe->clients.lock);
82 file->driver_priv = xef;
86 static void device_kill_persistent_exec_queues(struct xe_device *xe,
89 static void xe_file_close(struct drm_device *dev, struct drm_file *file)
91 struct xe_device *xe = to_xe_device(dev);
92 struct xe_file *xef = file->driver_priv;
94 struct xe_exec_queue *q;
97 mutex_lock(&xef->exec_queue.lock);
98 xa_for_each(&xef->exec_queue.xa, idx, q) {
99 xe_exec_queue_kill(q);
100 xe_exec_queue_put(q);
102 mutex_unlock(&xef->exec_queue.lock);
103 xa_destroy(&xef->exec_queue.xa);
104 mutex_destroy(&xef->exec_queue.lock);
105 device_kill_persistent_exec_queues(xe, xef);
107 mutex_lock(&xef->vm.lock);
108 xa_for_each(&xef->vm.xa, idx, vm)
109 xe_vm_close_and_put(vm);
110 mutex_unlock(&xef->vm.lock);
111 xa_destroy(&xef->vm.xa);
112 mutex_destroy(&xef->vm.lock);
114 spin_lock(&xe->clients.lock);
116 spin_unlock(&xe->clients.lock);
118 xe_drm_client_put(xef->client);
122 static const struct drm_ioctl_desc xe_ioctls[] = {
123 DRM_IOCTL_DEF_DRV(XE_DEVICE_QUERY, xe_query_ioctl, DRM_RENDER_ALLOW),
124 DRM_IOCTL_DEF_DRV(XE_GEM_CREATE, xe_gem_create_ioctl, DRM_RENDER_ALLOW),
125 DRM_IOCTL_DEF_DRV(XE_GEM_MMAP_OFFSET, xe_gem_mmap_offset_ioctl,
127 DRM_IOCTL_DEF_DRV(XE_VM_CREATE, xe_vm_create_ioctl, DRM_RENDER_ALLOW),
128 DRM_IOCTL_DEF_DRV(XE_VM_DESTROY, xe_vm_destroy_ioctl, DRM_RENDER_ALLOW),
129 DRM_IOCTL_DEF_DRV(XE_VM_BIND, xe_vm_bind_ioctl, DRM_RENDER_ALLOW),
130 DRM_IOCTL_DEF_DRV(XE_EXEC, xe_exec_ioctl, DRM_RENDER_ALLOW),
131 DRM_IOCTL_DEF_DRV(XE_EXEC_QUEUE_CREATE, xe_exec_queue_create_ioctl,
133 DRM_IOCTL_DEF_DRV(XE_EXEC_QUEUE_DESTROY, xe_exec_queue_destroy_ioctl,
135 DRM_IOCTL_DEF_DRV(XE_EXEC_QUEUE_GET_PROPERTY, xe_exec_queue_get_property_ioctl,
137 DRM_IOCTL_DEF_DRV(XE_WAIT_USER_FENCE, xe_wait_user_fence_ioctl,
141 static const struct file_operations xe_driver_fops = {
142 .owner = THIS_MODULE,
144 .release = drm_release_noglobal,
145 .unlocked_ioctl = drm_ioctl,
146 .mmap = drm_gem_mmap,
149 .compat_ioctl = drm_compat_ioctl,
150 .llseek = noop_llseek,
151 #ifdef CONFIG_PROC_FS
152 .show_fdinfo = drm_show_fdinfo,
156 static void xe_driver_release(struct drm_device *dev)
158 struct xe_device *xe = to_xe_device(dev);
160 pci_set_drvdata(to_pci_dev(xe->drm.dev), NULL);
163 static struct drm_driver driver = {
164 /* Don't use MTRRs here; the Xserver or userspace app should
165 * deal with them for Intel hardware.
169 DRIVER_RENDER | DRIVER_SYNCOBJ |
170 DRIVER_SYNCOBJ_TIMELINE | DRIVER_GEM_GPUVA,
171 .open = xe_file_open,
172 .postclose = xe_file_close,
174 .gem_prime_import = xe_gem_prime_import,
176 .dumb_create = xe_bo_dumb_create,
177 .dumb_map_offset = drm_gem_ttm_dumb_map_offset,
178 #ifdef CONFIG_PROC_FS
179 .show_fdinfo = xe_drm_client_fdinfo,
181 .release = &xe_driver_release,
184 .num_ioctls = ARRAY_SIZE(xe_ioctls),
185 .fops = &xe_driver_fops,
189 .major = DRIVER_MAJOR,
190 .minor = DRIVER_MINOR,
191 .patchlevel = DRIVER_PATCHLEVEL,
194 static void xe_device_destroy(struct drm_device *dev, void *dummy)
196 struct xe_device *xe = to_xe_device(dev);
199 destroy_workqueue(xe->ordered_wq);
201 if (xe->unordered_wq)
202 destroy_workqueue(xe->unordered_wq);
204 ttm_device_fini(&xe->ttm);
207 struct xe_device *xe_device_create(struct pci_dev *pdev,
208 const struct pci_device_id *ent)
210 struct xe_device *xe;
213 xe_display_driver_set_hooks(&driver);
215 err = drm_aperture_remove_conflicting_pci_framebuffers(pdev, &driver);
219 xe = devm_drm_dev_alloc(&pdev->dev, &driver, struct xe_device, drm);
223 err = ttm_device_init(&xe->ttm, &xe_ttm_funcs, xe->drm.dev,
224 xe->drm.anon_inode->i_mapping,
225 xe->drm.vma_offset_manager, false, false);
229 err = drmm_add_action_or_reset(&xe->drm, xe_device_destroy, NULL);
233 xe->info.devid = pdev->device;
234 xe->info.revid = pdev->revision;
235 xe->info.force_execlist = xe_modparam.force_execlist;
237 spin_lock_init(&xe->irq.lock);
238 spin_lock_init(&xe->clients.lock);
240 init_waitqueue_head(&xe->ufence_wq);
242 drmm_mutex_init(&xe->drm, &xe->usm.lock);
243 xa_init_flags(&xe->usm.asid_to_vm, XA_FLAGS_ALLOC);
245 if (IS_ENABLED(CONFIG_DRM_XE_DEBUG)) {
246 /* Trigger a large asid and an early asid wrap. */
249 BUILD_BUG_ON(XE_MAX_ASID < 2);
250 err = xa_alloc_cyclic(&xe->usm.asid_to_vm, &asid, NULL,
251 XA_LIMIT(XE_MAX_ASID - 2, XE_MAX_ASID - 1),
252 &xe->usm.next_asid, GFP_KERNEL);
253 drm_WARN_ON(&xe->drm, err);
255 xa_erase(&xe->usm.asid_to_vm, asid);
258 drmm_mutex_init(&xe->drm, &xe->persistent_engines.lock);
259 INIT_LIST_HEAD(&xe->persistent_engines.list);
261 spin_lock_init(&xe->pinned.lock);
262 INIT_LIST_HEAD(&xe->pinned.kernel_bo_present);
263 INIT_LIST_HEAD(&xe->pinned.external_vram);
264 INIT_LIST_HEAD(&xe->pinned.evicted);
266 xe->ordered_wq = alloc_ordered_workqueue("xe-ordered-wq", 0);
267 xe->unordered_wq = alloc_workqueue("xe-unordered-wq", 0, 0);
268 if (!xe->ordered_wq || !xe->unordered_wq) {
269 drm_err(&xe->drm, "Failed to allocate xe workqueues\n");
274 err = xe_display_create(xe);
285 * The driver-initiated FLR is the highest level of reset that we can trigger
286 * from within the driver. It is different from the PCI FLR in that it doesn't
287 * fully reset the SGUnit and doesn't modify the PCI config space and therefore
288 * it doesn't require a re-enumeration of the PCI BARs. However, the
289 * driver-initiated FLR does still cause a reset of both GT and display and a
290 * memory wipe of local and stolen memory, so recovery would require a full HW
291 * re-init and saving/restoring (or re-populating) the wiped memory. Since we
292 * perform the FLR as the very last action before releasing access to the HW
293 * during the driver release flow, we don't attempt recovery at all, because
294 * if/when a new instance of i915 is bound to the device it will do a full
297 static void xe_driver_flr(struct xe_device *xe)
299 const unsigned int flr_timeout = 3 * MICRO; /* specs recommend a 3s wait */
300 struct xe_gt *gt = xe_root_mmio_gt(xe);
303 if (xe_mmio_read32(gt, GU_CNTL_PROTECTED) & DRIVERINT_FLR_DIS) {
304 drm_info_once(&xe->drm, "BIOS Disabled Driver-FLR\n");
308 drm_dbg(&xe->drm, "Triggering Driver-FLR\n");
311 * Make sure any pending FLR requests have cleared by waiting for the
312 * FLR trigger bit to go to zero. Also clear GU_DEBUG's DRIVERFLR_STATUS
313 * to make sure it's not still set from a prior attempt (it's a write to
315 * Note that we should never be in a situation where a previous attempt
316 * is still pending (unless the HW is totally dead), but better to be
317 * safe in case something unexpected happens
319 ret = xe_mmio_wait32(gt, GU_CNTL, DRIVERFLR, 0, flr_timeout, NULL, false);
321 drm_err(&xe->drm, "Driver-FLR-prepare wait for ready failed! %d\n", ret);
324 xe_mmio_write32(gt, GU_DEBUG, DRIVERFLR_STATUS);
326 /* Trigger the actual Driver-FLR */
327 xe_mmio_rmw32(gt, GU_CNTL, 0, DRIVERFLR);
329 /* Wait for hardware teardown to complete */
330 ret = xe_mmio_wait32(gt, GU_CNTL, DRIVERFLR, 0, flr_timeout, NULL, false);
332 drm_err(&xe->drm, "Driver-FLR-teardown wait completion failed! %d\n", ret);
336 /* Wait for hardware/firmware re-init to complete */
337 ret = xe_mmio_wait32(gt, GU_DEBUG, DRIVERFLR_STATUS, DRIVERFLR_STATUS,
338 flr_timeout, NULL, false);
340 drm_err(&xe->drm, "Driver-FLR-reinit wait completion failed! %d\n", ret);
344 /* Clear sticky completion status */
345 xe_mmio_write32(gt, GU_DEBUG, DRIVERFLR_STATUS);
348 static void xe_driver_flr_fini(struct drm_device *drm, void *arg)
350 struct xe_device *xe = arg;
352 if (xe->needs_flr_on_fini)
356 static void xe_device_sanitize(struct drm_device *drm, void *arg)
358 struct xe_device *xe = arg;
362 for_each_gt(gt, xe, id)
366 static int xe_set_dma_info(struct xe_device *xe)
368 unsigned int mask_size = xe->info.dma_mask_size;
371 dma_set_max_seg_size(xe->drm.dev, xe_sg_segment_size(xe->drm.dev));
373 err = dma_set_mask(xe->drm.dev, DMA_BIT_MASK(mask_size));
377 err = dma_set_coherent_mask(xe->drm.dev, DMA_BIT_MASK(mask_size));
384 drm_err(&xe->drm, "Can't set DMA mask/consistent mask (%d)\n", err);
389 * Initialize MMIO resources that don't require any knowledge about tile count.
391 int xe_device_probe_early(struct xe_device *xe)
395 err = xe_mmio_init(xe);
399 err = xe_mmio_root_tile_init(xe);
406 static int xe_device_set_has_flat_ccs(struct xe_device *xe)
411 if (GRAPHICS_VER(xe) < 20 || !xe->info.has_flat_ccs)
414 struct xe_gt *gt = xe_root_mmio_gt(xe);
416 err = xe_force_wake_get(gt_to_fw(gt), XE_FW_GT);
420 reg = xe_gt_mcr_unicast_read_any(gt, XE2_FLAT_CCS_BASE_RANGE_LOWER);
421 xe->info.has_flat_ccs = (reg & XE2_FLAT_CCS_ENABLE);
423 if (!xe->info.has_flat_ccs)
425 "Flat CCS has been disabled in bios, May lead to performance impact");
427 return xe_force_wake_put(gt_to_fw(gt), XE_FW_GT);
430 int xe_device_probe(struct xe_device *xe)
432 struct xe_tile *tile;
437 xe_pat_init_early(xe);
439 xe->info.mem_region_mask = 1;
440 err = xe_display_init_nommio(xe);
444 err = xe_set_dma_info(xe);
448 xe_mmio_probe_tiles(xe);
450 xe_ttm_sys_mgr_init(xe);
452 for_each_gt(gt, xe, id)
453 xe_force_wake_init_gt(gt, gt_to_fw(gt));
455 for_each_tile(tile, xe, id) {
456 err = xe_ggtt_init_early(tile->mem.ggtt);
461 err = drmm_add_action_or_reset(&xe->drm, xe_driver_flr_fini, xe);
465 for_each_gt(gt, xe, id) {
466 err = xe_pcode_probe(gt);
471 err = xe_display_init_noirq(xe);
475 err = xe_irq_install(xe);
479 for_each_gt(gt, xe, id) {
480 err = xe_gt_init_early(gt);
482 goto err_irq_shutdown;
485 err = xe_device_set_has_flat_ccs(xe);
487 goto err_irq_shutdown;
489 err = xe_mmio_probe_vram(xe);
491 goto err_irq_shutdown;
493 for_each_tile(tile, xe, id) {
494 err = xe_tile_init_noalloc(tile);
496 goto err_irq_shutdown;
499 /* Allocate and map stolen after potential VRAM resize */
500 xe_ttm_stolen_mgr_init(xe);
503 * Now that GT is initialized (TTM in particular),
504 * we can try to init display, and inherit the initial fb.
505 * This is the reason the first allocation needs to be done
508 err = xe_display_init_noaccel(xe);
510 goto err_irq_shutdown;
512 for_each_gt(gt, xe, id) {
513 err = xe_gt_init(gt);
515 goto err_irq_shutdown;
518 xe_heci_gsc_init(xe);
520 err = xe_display_init(xe);
522 goto err_irq_shutdown;
524 err = drm_dev_register(&xe->drm, 0);
526 goto err_fini_display;
528 xe_display_register(xe);
530 xe_debugfs_register(xe);
532 xe_hwmon_register(xe);
534 err = drmm_add_action_or_reset(&xe->drm, xe_device_sanitize, xe);
541 xe_display_driver_remove(xe);
550 static void xe_device_remove_display(struct xe_device *xe)
552 xe_display_unregister(xe);
554 drm_dev_unplug(&xe->drm);
555 xe_display_driver_remove(xe);
558 void xe_device_remove(struct xe_device *xe)
560 xe_device_remove_display(xe);
564 xe_heci_gsc_fini(xe);
569 void xe_device_shutdown(struct xe_device *xe)
573 void xe_device_add_persistent_exec_queues(struct xe_device *xe, struct xe_exec_queue *q)
575 mutex_lock(&xe->persistent_engines.lock);
576 list_add_tail(&q->persistent.link, &xe->persistent_engines.list);
577 mutex_unlock(&xe->persistent_engines.lock);
580 void xe_device_remove_persistent_exec_queues(struct xe_device *xe,
581 struct xe_exec_queue *q)
583 mutex_lock(&xe->persistent_engines.lock);
584 if (!list_empty(&q->persistent.link))
585 list_del(&q->persistent.link);
586 mutex_unlock(&xe->persistent_engines.lock);
589 static void device_kill_persistent_exec_queues(struct xe_device *xe,
592 struct xe_exec_queue *q, *next;
594 mutex_lock(&xe->persistent_engines.lock);
595 list_for_each_entry_safe(q, next, &xe->persistent_engines.list,
597 if (q->persistent.xef == xef) {
598 xe_exec_queue_kill(q);
599 list_del_init(&q->persistent.link);
601 mutex_unlock(&xe->persistent_engines.lock);
604 void xe_device_wmb(struct xe_device *xe)
606 struct xe_gt *gt = xe_root_mmio_gt(xe);
610 xe_mmio_write32(gt, SOFTWARE_FLAGS_SPR33, 0);
613 u32 xe_device_ccs_bytes(struct xe_device *xe, u64 size)
615 return xe_device_has_flat_ccs(xe) ?
616 DIV_ROUND_UP_ULL(size, NUM_BYTES_PER_CCS_BYTE(xe)) : 0;
619 bool xe_device_mem_access_ongoing(struct xe_device *xe)
621 if (xe_pm_read_callback_task(xe) != NULL)
624 return atomic_read(&xe->mem_access.ref);
627 void xe_device_assert_mem_access(struct xe_device *xe)
629 XE_WARN_ON(!xe_device_mem_access_ongoing(xe));
632 bool xe_device_mem_access_get_if_ongoing(struct xe_device *xe)
636 if (xe_pm_read_callback_task(xe) == current)
639 active = xe_pm_runtime_get_if_active(xe);
641 int ref = atomic_inc_return(&xe->mem_access.ref);
643 xe_assert(xe, ref != S32_MAX);
649 void xe_device_mem_access_get(struct xe_device *xe)
654 * This looks racy, but should be fine since the pm_callback_task only
655 * transitions from NULL -> current (and back to NULL again), during the
656 * runtime_resume() or runtime_suspend() callbacks, for which there can
657 * only be a single one running for our device. We only need to prevent
658 * recursively calling the runtime_get or runtime_put from those
659 * callbacks, as well as preventing triggering any access_ongoing
662 if (xe_pm_read_callback_task(xe) == current)
666 * Since the resume here is synchronous it can be quite easy to deadlock
667 * if we are not careful. Also in practice it might be quite timing
668 * sensitive to ever see the 0 -> 1 transition with the callers locks
669 * held, so deadlocks might exist but are hard for lockdep to ever see.
670 * With this in mind, help lockdep learn about the potentially scary
671 * stuff that can happen inside the runtime_resume callback by acquiring
672 * a dummy lock (it doesn't protect anything and gets compiled out on
673 * non-debug builds). Lockdep then only needs to see the
674 * mem_access_lockdep_map -> runtime_resume callback once, and then can
675 * hopefully validate all the (callers_locks) -> mem_access_lockdep_map.
676 * For example if the (callers_locks) are ever grabbed in the
677 * runtime_resume callback, lockdep should give us a nice splat.
679 lock_map_acquire(&xe_device_mem_access_lockdep_map);
680 lock_map_release(&xe_device_mem_access_lockdep_map);
682 xe_pm_runtime_get(xe);
683 ref = atomic_inc_return(&xe->mem_access.ref);
685 xe_assert(xe, ref != S32_MAX);
689 void xe_device_mem_access_put(struct xe_device *xe)
693 if (xe_pm_read_callback_task(xe) == current)
696 ref = atomic_dec_return(&xe->mem_access.ref);
697 xe_pm_runtime_put(xe);
699 xe_assert(xe, ref >= 0);