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