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