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