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