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