xref: /linux/drivers/gpu/drm/xe/xe_device.c (revision b0a3a811634aa7422f8a92196db47a06a70bda16)
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_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_pagemap_util.h>
18 #include <drm/drm_print.h>
19 #include <kunit/static_stub.h>
20 #include <uapi/drm/xe_drm.h>
21 
22 #include "display/xe_display.h"
23 #include "instructions/xe_gpu_commands.h"
24 #include "regs/xe_gt_regs.h"
25 #include "regs/xe_regs.h"
26 #include "xe_bo.h"
27 #include "xe_bo_evict.h"
28 #include "xe_configfs.h"
29 #include "xe_debugfs.h"
30 #include "xe_defaults.h"
31 #include "xe_devcoredump.h"
32 #include "xe_device_sysfs.h"
33 #include "xe_dma_buf.h"
34 #include "xe_drm_client.h"
35 #include "xe_drv.h"
36 #include "xe_exec.h"
37 #include "xe_exec_queue.h"
38 #include "xe_force_wake.h"
39 #include "xe_ggtt.h"
40 #include "xe_gt.h"
41 #include "xe_gt_mcr.h"
42 #include "xe_gt_printk.h"
43 #include "xe_gt_sriov_vf.h"
44 #include "xe_guc.h"
45 #include "xe_guc_pc.h"
46 #include "xe_hw_engine_group.h"
47 #include "xe_hwmon.h"
48 #include "xe_i2c.h"
49 #include "xe_irq.h"
50 #include "xe_late_bind_fw.h"
51 #include "xe_log.h"
52 #include "xe_mmio.h"
53 #include "xe_mmio_gem.h"
54 #include "xe_module.h"
55 #include "xe_nvm.h"
56 #include "xe_oa.h"
57 #include "xe_observation.h"
58 #include "xe_pagefault.h"
59 #include "xe_pat.h"
60 #include "xe_pcode.h"
61 #include "xe_pm.h"
62 #include "xe_pmu.h"
63 #include "xe_psmi.h"
64 #include "xe_pxp.h"
65 #include "xe_query.h"
66 #include "xe_ras.h"
67 #include "xe_shrinker.h"
68 #include "xe_soc_remapper.h"
69 #include "xe_survivability_mode.h"
70 #include "xe_sriov.h"
71 #include "xe_svm.h"
72 #include "xe_sysctrl.h"
73 #include "xe_tile.h"
74 #include "xe_ttm_stolen_mgr.h"
75 #include "xe_ttm_sys_mgr.h"
76 #include "xe_vm.h"
77 #include "xe_vm_madvise.h"
78 #include "xe_vram.h"
79 #include "xe_vram_types.h"
80 #include "xe_vsec.h"
81 #include "xe_wait_user_fence.h"
82 #include "xe_wa.h"
83 
84 #include <generated/xe_device_wa_oob.h>
85 #include <generated/xe_wa_oob.h>
86 
87 static int xe_file_open(struct drm_device *dev, struct drm_file *file)
88 {
89 	struct xe_device *xe = to_xe_device(dev);
90 	struct xe_drm_client *client;
91 	struct xe_file *xef;
92 	int ret = -ENOMEM;
93 	struct task_struct *task = NULL;
94 
95 	xef = kzalloc_obj(*xef);
96 	if (!xef)
97 		return ret;
98 
99 	client = xe_drm_client_alloc();
100 	if (!client) {
101 		kfree(xef);
102 		return ret;
103 	}
104 
105 	xef->drm = file;
106 	xef->client = client;
107 	xef->xe = xe;
108 
109 	mutex_init(&xef->vm.lock);
110 	xa_init_flags(&xef->vm.xa, XA_FLAGS_ALLOC1);
111 
112 	mutex_init(&xef->exec_queue.lock);
113 	xa_init_flags(&xef->exec_queue.xa, XA_FLAGS_ALLOC1);
114 
115 	mutex_init(&xef->mmio_gem.lock);
116 
117 	file->driver_priv = xef;
118 	kref_init(&xef->refcount);
119 
120 	task = get_pid_task(rcu_access_pointer(file->pid), PIDTYPE_PID);
121 	if (task) {
122 		xef->process_name = kstrdup(task->comm, GFP_KERNEL);
123 		xef->pid = task->pid;
124 		put_task_struct(task);
125 	}
126 
127 	return 0;
128 }
129 
130 static void xe_file_destroy(struct kref *ref)
131 {
132 	struct xe_file *xef = container_of(ref, struct xe_file, refcount);
133 
134 	xa_destroy(&xef->exec_queue.xa);
135 	mutex_destroy(&xef->exec_queue.lock);
136 	xa_destroy(&xef->vm.xa);
137 	mutex_destroy(&xef->vm.lock);
138 
139 	mutex_destroy(&xef->mmio_gem.lock);
140 
141 	xe_drm_client_put(xef->client);
142 	kfree(xef->process_name);
143 	kfree(xef);
144 }
145 
146 /**
147  * xe_file_get() - Take a reference to the xe file object
148  * @xef: Pointer to the xe file
149  *
150  * Anyone with a pointer to xef must take a reference to the xe file
151  * object using this call.
152  *
153  * Return: xe file pointer
154  */
155 struct xe_file *xe_file_get(struct xe_file *xef)
156 {
157 	kref_get(&xef->refcount);
158 	return xef;
159 }
160 
161 /**
162  * xe_file_put() - Drop a reference to the xe file object
163  * @xef: Pointer to the xe file
164  *
165  * Used to drop reference to the xef object
166  */
167 void xe_file_put(struct xe_file *xef)
168 {
169 	kref_put(&xef->refcount, xe_file_destroy);
170 }
171 
172 static void xe_file_close(struct drm_device *dev, struct drm_file *file)
173 {
174 	struct xe_device *xe = to_xe_device(dev);
175 	struct xe_file *xef = file->driver_priv;
176 	struct xe_vm *vm;
177 	struct xe_exec_queue *q;
178 	unsigned long idx;
179 
180 	guard(xe_pm_runtime)(xe);
181 
182 	/*
183 	 * No need for exec_queue.lock here as there is no contention for it
184 	 * when FD is closing as IOCTLs presumably can't be modifying the
185 	 * xarray. Taking exec_queue.lock here causes undue dependency on
186 	 * vm->lock taken during xe_exec_queue_kill().
187 	 */
188 	xa_for_each(&xef->exec_queue.xa, idx, q) {
189 		if (q->vm && q->hwe->hw_engine_group)
190 			xe_hw_engine_group_del_exec_queue(q->hwe->hw_engine_group, q);
191 		xe_exec_queue_kill(q);
192 		xe_exec_queue_put(q);
193 	}
194 	xa_for_each(&xef->vm.xa, idx, vm)
195 		xe_vm_close_and_put(vm);
196 
197 	scoped_guard(mutex, &xef->mmio_gem.lock) {
198 		if (xef->mmio_gem.pci_barrier) {
199 			xe_mmio_gem_destroy(xef->mmio_gem.pci_barrier, file);
200 			xef->mmio_gem.pci_barrier = NULL;
201 		}
202 	}
203 
204 	xe_file_put(xef);
205 }
206 
207 static const struct drm_ioctl_desc xe_ioctls[] = {
208 	DRM_IOCTL_DEF_DRV(XE_DEVICE_QUERY, xe_query_ioctl, DRM_RENDER_ALLOW),
209 	DRM_IOCTL_DEF_DRV(XE_GEM_CREATE, xe_gem_create_ioctl, DRM_RENDER_ALLOW),
210 	DRM_IOCTL_DEF_DRV(XE_GEM_MMAP_OFFSET, xe_gem_mmap_offset_ioctl,
211 			  DRM_RENDER_ALLOW),
212 	DRM_IOCTL_DEF_DRV(XE_VM_CREATE, xe_vm_create_ioctl, DRM_RENDER_ALLOW),
213 	DRM_IOCTL_DEF_DRV(XE_VM_DESTROY, xe_vm_destroy_ioctl, DRM_RENDER_ALLOW),
214 	DRM_IOCTL_DEF_DRV(XE_VM_BIND, xe_vm_bind_ioctl, DRM_RENDER_ALLOW),
215 	DRM_IOCTL_DEF_DRV(XE_EXEC, xe_exec_ioctl, DRM_RENDER_ALLOW),
216 	DRM_IOCTL_DEF_DRV(XE_EXEC_QUEUE_CREATE, xe_exec_queue_create_ioctl,
217 			  DRM_RENDER_ALLOW),
218 	DRM_IOCTL_DEF_DRV(XE_EXEC_QUEUE_DESTROY, xe_exec_queue_destroy_ioctl,
219 			  DRM_RENDER_ALLOW),
220 	DRM_IOCTL_DEF_DRV(XE_EXEC_QUEUE_GET_PROPERTY, xe_exec_queue_get_property_ioctl,
221 			  DRM_RENDER_ALLOW),
222 	DRM_IOCTL_DEF_DRV(XE_WAIT_USER_FENCE, xe_wait_user_fence_ioctl,
223 			  DRM_RENDER_ALLOW),
224 	DRM_IOCTL_DEF_DRV(XE_OBSERVATION, xe_observation_ioctl, DRM_RENDER_ALLOW),
225 	DRM_IOCTL_DEF_DRV(XE_MADVISE, xe_vm_madvise_ioctl, DRM_RENDER_ALLOW),
226 	DRM_IOCTL_DEF_DRV(XE_VM_QUERY_MEM_RANGE_ATTRS, xe_vm_query_vmas_attrs_ioctl,
227 			  DRM_RENDER_ALLOW),
228 	DRM_IOCTL_DEF_DRV(XE_EXEC_QUEUE_SET_PROPERTY, xe_exec_queue_set_property_ioctl,
229 			  DRM_RENDER_ALLOW),
230 	DRM_IOCTL_DEF_DRV(XE_VM_GET_PROPERTY, xe_vm_get_property_ioctl,
231 			  DRM_RENDER_ALLOW),
232 };
233 
234 static long xe_drm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
235 {
236 	struct drm_file *file_priv = file->private_data;
237 	struct xe_device *xe = to_xe_device(file_priv->minor->dev);
238 	long ret;
239 
240 	if (xe_device_wedged(xe))
241 		return -ECANCELED;
242 
243 	ACQUIRE(xe_pm_runtime_ioctl, pm)(xe);
244 	ret = ACQUIRE_ERR(xe_pm_runtime_ioctl, &pm);
245 	if (ret >= 0)
246 		ret = drm_ioctl(file, cmd, arg);
247 
248 	return ret;
249 }
250 
251 #ifdef CONFIG_COMPAT
252 static long xe_drm_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
253 {
254 	struct drm_file *file_priv = file->private_data;
255 	struct xe_device *xe = to_xe_device(file_priv->minor->dev);
256 	long ret;
257 
258 	if (xe_device_wedged(xe))
259 		return -ECANCELED;
260 
261 	ACQUIRE(xe_pm_runtime_ioctl, pm)(xe);
262 	ret = ACQUIRE_ERR(xe_pm_runtime_ioctl, &pm);
263 	if (ret >= 0)
264 		ret = drm_compat_ioctl(file, cmd, arg);
265 
266 	return ret;
267 }
268 #else
269 /* similarly to drm_compat_ioctl, let's it be assigned to .compat_ioct unconditionally */
270 #define xe_drm_compat_ioctl NULL
271 #endif
272 
273 static int xe_mmap(struct file *filp, struct vm_area_struct *vma)
274 {
275 	struct drm_file *priv = filp->private_data;
276 	struct drm_device *dev = priv->minor->dev;
277 
278 	if (drm_dev_is_unplugged(dev))
279 		return -ENODEV;
280 
281 	return drm_gem_mmap(filp, vma);
282 }
283 
284 static const struct file_operations xe_driver_fops = {
285 	.owner = THIS_MODULE,
286 	.open = drm_open,
287 	.release = drm_release_noglobal,
288 	.unlocked_ioctl = xe_drm_ioctl,
289 	.mmap = xe_mmap,
290 	.poll = drm_poll,
291 	.read = drm_read,
292 	.compat_ioctl = xe_drm_compat_ioctl,
293 	.llseek = noop_llseek,
294 #ifdef CONFIG_PROC_FS
295 	.show_fdinfo = drm_show_fdinfo,
296 #endif
297 	.fop_flags = FOP_UNSIGNED_OFFSET,
298 };
299 
300 /**
301  * xe_is_xe_file() - Is the file an xe device file?
302  * @file: The file.
303  *
304  * Checks whether the file is opened against
305  * an xe device.
306  *
307  * Return: %true if an xe file, %false if not.
308  */
309 bool xe_is_xe_file(const struct file *file)
310 {
311 	return file->f_op == &xe_driver_fops;
312 }
313 
314 static const struct drm_driver regular_driver = {
315 	.driver_features =
316 	    XE_DISPLAY_DRIVER_FEATURES |
317 	    DRIVER_GEM |
318 	    DRIVER_RENDER | DRIVER_SYNCOBJ |
319 	    DRIVER_SYNCOBJ_TIMELINE,
320 	.open = xe_file_open,
321 	.postclose = xe_file_close,
322 
323 	.gem_prime_import = xe_gem_prime_import,
324 
325 	.dumb_create = xe_bo_dumb_create,
326 	.dumb_map_offset = drm_gem_ttm_dumb_map_offset,
327 #ifdef CONFIG_PROC_FS
328 	.show_fdinfo = xe_drm_client_fdinfo,
329 #endif
330 	.ioctls = xe_ioctls,
331 	.num_ioctls = ARRAY_SIZE(xe_ioctls),
332 	.fops = &xe_driver_fops,
333 	.name = DRIVER_NAME,
334 	.desc = DRIVER_DESC,
335 	.major = DRIVER_MAJOR,
336 	.minor = DRIVER_MINOR,
337 	.patchlevel = DRIVER_PATCHLEVEL,
338 	XE_DISPLAY_DRIVER_OPS,
339 };
340 
341 #ifdef CONFIG_PCI_IOV
342 static const struct drm_ioctl_desc xe_ioctls_admin_only[] = {
343 	DRM_IOCTL_DEF_DRV(XE_DEVICE_QUERY, xe_query_ioctl, DRM_RENDER_ALLOW),
344 	DRM_IOCTL_DEF_DRV(XE_OBSERVATION, xe_observation_ioctl, DRM_RENDER_ALLOW),
345 };
346 
347 static const struct drm_driver admin_only_driver = {
348 	.driver_features =
349 	    DRIVER_GEM | DRIVER_RENDER,
350 	.open = xe_file_open,
351 	.postclose = xe_file_close,
352 	.ioctls = xe_ioctls_admin_only,
353 	.num_ioctls = ARRAY_SIZE(xe_ioctls_admin_only),
354 	.fops = &xe_driver_fops,
355 	.name = DRIVER_NAME,
356 	.desc = DRIVER_DESC,
357 	.major = DRIVER_MAJOR,
358 	.minor = DRIVER_MINOR,
359 	.patchlevel = DRIVER_PATCHLEVEL,
360 };
361 
362 /**
363  * xe_device_is_admin_only() - Check whether device is admin only or not.
364  * @xe: the &xe_device to check
365  *
366  * Return: true if the device is admin only, false otherwise.
367  */
368 bool xe_device_is_admin_only(const struct xe_device *xe)
369 {
370 	KUNIT_STATIC_STUB_REDIRECT(xe_device_is_admin_only, xe);
371 	return xe->drm.driver == &admin_only_driver;
372 }
373 #endif
374 
375 static void xe_device_destroy(struct drm_device *dev, void *dummy)
376 {
377 	struct xe_device *xe = to_xe_device(dev);
378 
379 	xe_bo_dev_fini(&xe->bo_device);
380 
381 	if (xe->preempt_fence_wq)
382 		destroy_workqueue(xe->preempt_fence_wq);
383 
384 	if (xe->ordered_wq)
385 		destroy_workqueue(xe->ordered_wq);
386 
387 	if (xe->unordered_wq)
388 		destroy_workqueue(xe->unordered_wq);
389 
390 	if (xe->destroy_wq)
391 		destroy_workqueue(xe->destroy_wq);
392 
393 	ttm_device_fini(&xe->ttm);
394 }
395 
396 /**
397  * xe_device_create() - Create a new &xe_device instance
398  * @pdev: the parent &pci_dev
399  *
400  * Allocate and initialize a device managed Xe device structure.
401  *
402  * Return: pointer to new &xe_device on success, or ERR_PTR on failure.
403  */
404 struct xe_device *xe_device_create(struct pci_dev *pdev)
405 {
406 	const struct drm_driver *driver = &regular_driver;
407 	struct xe_device *xe;
408 	int err;
409 
410 #ifdef CONFIG_PCI_IOV
411 	/*
412 	 * Since XE device is not initialized yet, read from configfs
413 	 * directly to decide whether we are in admin-only PF mode or not.
414 	 */
415 	if (xe_configfs_admin_only_pf(pdev))
416 		driver = &admin_only_driver;
417 #endif
418 
419 	err = aperture_remove_conflicting_pci_devices(pdev, driver->name);
420 	if (err)
421 		return ERR_PTR(err);
422 
423 	xe = devm_drm_dev_alloc(&pdev->dev, driver, struct xe_device, drm);
424 	if (IS_ERR(xe))
425 		return xe;
426 
427 	err = xe_device_init_early(xe);
428 	if (err)
429 		return ERR_PTR(err);
430 
431 	return xe;
432 }
433 ALLOW_ERROR_INJECTION(xe_device_create, ERRNO); /* See xe_pci_probe() */
434 
435 static void xe_device_parse_modparam(struct xe_device *xe)
436 {
437 	xe->atomic_svm_timeslice_ms = 5;
438 	xe->min_run_period_lr_ms = 5;
439 	xe->info.num_pf_work = xe_modparam.num_pf_work;
440 	if (xe->info.num_pf_work < 1)
441 		xe->info.num_pf_work = 1;
442 	else if (xe->info.num_pf_work > XE_PAGEFAULT_WORK_MAX)
443 		xe->info.num_pf_work = XE_PAGEFAULT_WORK_MAX;
444 }
445 
446 /**
447  * xe_device_init_early() - Initialize a new &xe_device instance
448  * @xe: the &xe_device to initialize
449  *
450  * Return: 0 on success or a negative error code on failure.
451  */
452 int xe_device_init_early(struct xe_device *xe)
453 {
454 	int err;
455 
456 	err = ttm_device_init(&xe->ttm, &xe_ttm_funcs, xe->drm.dev,
457 			      xe->drm.anon_inode->i_mapping,
458 			      xe->drm.vma_offset_manager,
459 			      TTM_ALLOCATION_POOL_BENEFICIAL_ORDER(get_order(SZ_2M)));
460 	if (err)
461 		return err;
462 
463 	xe_bo_dev_init(&xe->bo_device);
464 	err = drmm_add_action_or_reset(&xe->drm, xe_device_destroy, NULL);
465 	if (err)
466 		return err;
467 
468 	err = xe_shrinker_create(xe);
469 	if (err)
470 		return err;
471 
472 	xe_device_parse_modparam(xe);
473 
474 	err = xe_irq_init(xe);
475 	if (err)
476 		return err;
477 
478 	xe_validation_device_init(&xe->val);
479 
480 	init_waitqueue_head(&xe->ufence_wq);
481 
482 	init_rwsem(&xe->usm.lock);
483 
484 	err = xe_pagemap_shrinker_create(xe);
485 	if (err)
486 		return err;
487 
488 	xa_init_flags(&xe->usm.asid_to_vm, XA_FLAGS_ALLOC);
489 
490 	if (IS_ENABLED(CONFIG_DRM_XE_DEBUG)) {
491 		/* Trigger a large asid and an early asid wrap. */
492 		u32 asid;
493 
494 		BUILD_BUG_ON(XE_MAX_ASID < 2);
495 		err = xa_alloc_cyclic(&xe->usm.asid_to_vm, &asid, NULL,
496 				      XA_LIMIT(XE_MAX_ASID - 2, XE_MAX_ASID - 1),
497 				      &xe->usm.next_asid, GFP_KERNEL);
498 		drm_WARN_ON(&xe->drm, err);
499 		if (err >= 0)
500 			xa_erase(&xe->usm.asid_to_vm, asid);
501 	}
502 
503 	err = xe_bo_pinned_init(xe);
504 	if (err)
505 		return err;
506 
507 	xe->preempt_fence_wq = alloc_ordered_workqueue("xe-preempt-fence-wq",
508 						       WQ_MEM_RECLAIM);
509 	xe->ordered_wq = alloc_ordered_workqueue("xe-ordered-wq", 0);
510 	xe->unordered_wq = alloc_workqueue("xe-unordered-wq", WQ_PERCPU, 0);
511 	xe->destroy_wq = alloc_workqueue("xe-destroy-wq", WQ_PERCPU | WQ_MEM_RECLAIM, 0);
512 	if (!xe->ordered_wq || !xe->unordered_wq ||
513 	    !xe->preempt_fence_wq || !xe->destroy_wq) {
514 		/*
515 		 * Cleanup done in xe_device_destroy via
516 		 * drmm_add_action_or_reset register above
517 		 */
518 		drm_err(&xe->drm, "Failed to allocate xe workqueues\n");
519 		return -ENOMEM;
520 	}
521 
522 	err = drmm_mutex_init(&xe->drm, &xe->pmt.lock);
523 	if (err)
524 		return err;
525 
526 	err = xe_pm_init_early(xe);
527 	if (err)
528 		return err;
529 
530 	return 0;
531 }
532 
533 static bool xe_driver_flr_disabled(struct xe_device *xe)
534 {
535 	if (IS_SRIOV_VF(xe))
536 		return true;
537 
538 	if (xe_mmio_read32(xe_root_tile_mmio(xe), GU_CNTL_PROTECTED) & DRIVERINT_FLR_DIS) {
539 		drm_info(&xe->drm, "Driver-FLR disabled by BIOS\n");
540 		return true;
541 	}
542 
543 	return false;
544 }
545 
546 /*
547  * The driver-initiated FLR is the highest level of reset that we can trigger
548  * from within the driver. It is different from the PCI FLR in that it doesn't
549  * fully reset the SGUnit and doesn't modify the PCI config space and therefore
550  * it doesn't require a re-enumeration of the PCI BARs. However, the
551  * driver-initiated FLR does still cause a reset of both GT and display and a
552  * memory wipe of local and stolen memory, so recovery would require a full HW
553  * re-init and saving/restoring (or re-populating) the wiped memory. Since we
554  * perform the FLR as the very last action before releasing access to the HW
555  * during the driver release flow, we don't attempt recovery at all, because
556  * if/when a new instance of Xe is bound to the device it will do a full
557  * re-init anyway.
558  */
559 static void __xe_driver_flr(struct xe_device *xe)
560 {
561 	const unsigned int flr_timeout = 3 * USEC_PER_SEC; /* specs recommend a 3s wait */
562 	struct xe_mmio *mmio = xe_root_tile_mmio(xe);
563 	int ret;
564 
565 	drm_dbg(&xe->drm, "Triggering Driver-FLR\n");
566 
567 	/*
568 	 * Make sure any pending FLR requests have cleared by waiting for the
569 	 * FLR trigger bit to go to zero. Also clear GU_DEBUG's DRIVERFLR_STATUS
570 	 * to make sure it's not still set from a prior attempt (it's a write to
571 	 * clear bit).
572 	 * Note that we should never be in a situation where a previous attempt
573 	 * is still pending (unless the HW is totally dead), but better to be
574 	 * safe in case something unexpected happens
575 	 */
576 	ret = xe_mmio_wait32(mmio, GU_CNTL, DRIVERFLR, 0, flr_timeout, NULL, false);
577 	if (ret) {
578 		drm_err(&xe->drm, "Driver-FLR-prepare wait for ready failed! %d\n", ret);
579 		return;
580 	}
581 	xe_mmio_write32(mmio, GU_DEBUG, DRIVERFLR_STATUS);
582 
583 	/* Trigger the actual Driver-FLR */
584 	xe_mmio_rmw32(mmio, GU_CNTL, 0, DRIVERFLR);
585 
586 	/* Wait for hardware teardown to complete */
587 	ret = xe_mmio_wait32(mmio, GU_CNTL, DRIVERFLR, 0, flr_timeout, NULL, false);
588 	if (ret) {
589 		drm_err(&xe->drm, "Driver-FLR-teardown wait completion failed! %d\n", ret);
590 		return;
591 	}
592 
593 	/* Wait for hardware/firmware re-init to complete */
594 	ret = xe_mmio_wait32(mmio, GU_DEBUG, DRIVERFLR_STATUS, DRIVERFLR_STATUS,
595 			     flr_timeout, NULL, false);
596 	if (ret) {
597 		drm_err(&xe->drm, "Driver-FLR-reinit wait completion failed! %d\n", ret);
598 		return;
599 	}
600 
601 	/* Clear sticky completion status */
602 	xe_mmio_write32(mmio, GU_DEBUG, DRIVERFLR_STATUS);
603 }
604 
605 static void xe_driver_flr(struct xe_device *xe)
606 {
607 	if (xe_driver_flr_disabled(xe))
608 		return;
609 
610 	__xe_driver_flr(xe);
611 }
612 
613 static void xe_driver_flr_fini(void *arg)
614 {
615 	struct xe_device *xe = arg;
616 
617 	if (xe->needs_flr_on_fini)
618 		xe_driver_flr(xe);
619 }
620 
621 static void xe_device_sanitize(void *arg)
622 {
623 	struct xe_device *xe = arg;
624 	struct xe_gt *gt;
625 	u8 id;
626 
627 	for_each_gt(gt, xe, id)
628 		xe_gt_sanitize(gt);
629 }
630 
631 static int xe_set_dma_info(struct xe_device *xe)
632 {
633 	unsigned int mask_size = xe->info.dma_mask_size;
634 	int err;
635 
636 	dma_set_max_seg_size(xe->drm.dev, xe_sg_segment_size(xe->drm.dev));
637 
638 	err = dma_set_mask(xe->drm.dev, DMA_BIT_MASK(mask_size));
639 	if (err)
640 		goto mask_err;
641 
642 	err = dma_set_coherent_mask(xe->drm.dev, DMA_BIT_MASK(mask_size));
643 	if (err)
644 		goto mask_err;
645 
646 	return 0;
647 
648 mask_err:
649 	drm_err(&xe->drm, "Can't set DMA mask/consistent mask (%d)\n", err);
650 	return err;
651 }
652 
653 static void assert_lmem_ready(struct xe_device *xe)
654 {
655 	if (!IS_DGFX(xe) || IS_SRIOV_VF(xe))
656 		return;
657 
658 	xe_assert(xe, xe_mmio_read32(xe_root_tile_mmio(xe), GU_CNTL) &
659 		  LMEM_INIT);
660 }
661 
662 static void vf_update_device_info(struct xe_device *xe)
663 {
664 	xe_assert(xe, IS_SRIOV_VF(xe));
665 	/* disable features that are not available/applicable to VFs */
666 	xe->info.probe_display = 0;
667 	xe->info.has_heci_cscfi = 0;
668 	xe->info.has_heci_gscfi = 0;
669 	xe->info.has_i2c = 0;
670 	xe->info.has_late_bind = 0;
671 	xe->info.skip_guc_pc = 1;
672 	xe->info.skip_pcode = 1;
673 	xe->info.has_drm_ras = false;
674 }
675 
676 static int xe_device_vram_alloc(struct xe_device *xe)
677 {
678 	struct xe_vram_region *vram;
679 
680 	if (!IS_DGFX(xe))
681 		return 0;
682 
683 	vram = drmm_kzalloc(&xe->drm, sizeof(*vram), GFP_KERNEL);
684 	if (!vram)
685 		return -ENOMEM;
686 
687 	xe->mem.vram = vram;
688 	return 0;
689 }
690 
691 /**
692  * xe_device_probe_early: Device early probe
693  * @xe: xe device instance
694  *
695  * Initialize MMIO resources that don't require any
696  * knowledge about tile count. Also initialize pcode and
697  * check vram initialization on root tile.
698  *
699  * Return: 0 on success, error code on failure
700  */
701 int xe_device_probe_early(struct xe_device *xe)
702 {
703 	int err;
704 
705 	xe_wa_device_init(xe);
706 	xe_wa_process_device_oob(xe);
707 
708 	err = xe_mmio_probe_early(xe);
709 	if (err)
710 		return err;
711 
712 	xe_sriov_probe_early(xe);
713 
714 	if (xe_device_is_admin_only(xe) && !IS_SRIOV_PF(xe)) {
715 		xe_err(xe, "Can't run Admin-only mode without SR-IOV PF mode!\n");
716 		return -ENODEV;
717 	}
718 
719 	if (IS_SRIOV_VF(xe))
720 		vf_update_device_info(xe);
721 
722 	/*
723 	 * Check for pcode uncore_init status to confirm if the SoC
724 	 * initialization is complete. Until done, any MMIO or lmem access from
725 	 * the driver will be blocked
726 	 */
727 	err = xe_pcode_probe_early(xe);
728 	if (err || xe_survivability_mode_is_requested(xe)) {
729 		int save_err = err;
730 
731 		/*
732 		 * Try to leave device in survivability mode if device is
733 		 * possible, but still return the previous error for error
734 		 * propagation
735 		 */
736 		err = xe_survivability_mode_boot_enable(xe);
737 		if (err)
738 			return err;
739 
740 		return save_err;
741 	}
742 
743 	/*
744 	 * Make sure the lmem is initialized and ready to use. xe_pcode_ready()
745 	 * is flagged after full initialization is complete. Assert if lmem is
746 	 * not initialized.
747 	 */
748 	assert_lmem_ready(xe);
749 
750 	xe->wedged.mode = xe_device_validate_wedged_mode(xe, xe_modparam.wedged_mode) ?
751 			  XE_DEFAULT_WEDGED_MODE : xe_modparam.wedged_mode;
752 	drm_dbg(&xe->drm, "wedged_mode: setting mode (%u) %s\n",
753 		xe->wedged.mode, xe_wedged_mode_to_string(xe->wedged.mode));
754 
755 	err = xe_device_vram_alloc(xe);
756 	if (err)
757 		return err;
758 
759 	return 0;
760 }
761 ALLOW_ERROR_INJECTION(xe_device_probe_early, ERRNO); /* See xe_pci_probe() */
762 
763 static int probe_has_flat_ccs(struct xe_device *xe)
764 {
765 	struct xe_gt *gt;
766 	u32 reg;
767 
768 	/* Always enabled/disabled, no runtime check to do */
769 	if (GRAPHICS_VER(xe) < 20 || !xe->info.has_flat_ccs || IS_SRIOV_VF(xe))
770 		return 0;
771 
772 	gt = xe_root_mmio_gt(xe);
773 	if (!gt)
774 		return 0;
775 
776 	CLASS(xe_force_wake, fw_ref)(gt_to_fw(gt), XE_FW_GT);
777 	if (!fw_ref.domains)
778 		return -ETIMEDOUT;
779 
780 	reg = xe_gt_mcr_unicast_read_any(gt, XE2_FLAT_CCS_BASE_RANGE_LOWER);
781 	xe->info.has_flat_ccs = (reg & XE2_FLAT_CCS_ENABLE);
782 
783 	if (!xe->info.has_flat_ccs)
784 		drm_dbg(&xe->drm,
785 			"Flat CCS has been disabled in bios, May lead to performance impact");
786 
787 	return 0;
788 }
789 
790 /*
791  * Detect if the driver is being run on pre-production hardware.  We don't
792  * keep workarounds for pre-production hardware long term, so print an
793  * error and add taint if we're being loaded on a pre-production platform
794  * for which the pre-prod workarounds have already been removed.
795  *
796  * The general policy is that we'll remove any workarounds that only apply to
797  * pre-production hardware around the time force_probe restrictions are lifted
798  * for a platform of the next major IP generation (for example, Xe2 pre-prod
799  * workarounds should be removed around the time the first Xe3 platforms have
800  * force_probe lifted).
801  */
802 static void detect_preproduction_hw(struct xe_device *xe)
803 {
804 	struct xe_gt *gt;
805 	int id;
806 
807 	/*
808 	 * SR-IOV VFs don't have access to the FUSE2 register, so we can't
809 	 * check pre-production status there.  But the host OS will notice
810 	 * and report the pre-production status, which should be enough to
811 	 * help us catch mistaken use of pre-production hardware.
812 	 */
813 	if (IS_SRIOV_VF(xe))
814 		return;
815 
816 	/*
817 	 * The "SW_CAP" fuse contains a bit indicating whether the device is a
818 	 * production or pre-production device.  This fuse is reflected through
819 	 * the GT "FUSE2" register, even though the contents of the fuse are
820 	 * not GT-specific.  Every GT's reflection of this fuse should show the
821 	 * same value, so we'll just use the first available GT for lookup.
822 	 */
823 	for_each_gt(gt, xe, id)
824 		break;
825 
826 	if (!gt)
827 		return;
828 
829 	CLASS(xe_force_wake, fw_ref)(gt_to_fw(gt), XE_FW_GT);
830 	if (!xe_force_wake_ref_has_domain(fw_ref.domains, XE_FW_GT)) {
831 		xe_gt_err(gt, "Forcewake failure; cannot determine production/pre-production hw status.\n");
832 		return;
833 	}
834 
835 	if (xe_mmio_read32(&gt->mmio, FUSE2) & PRODUCTION_HW)
836 		return;
837 
838 	xe_info(xe, "Pre-production hardware detected.\n");
839 	if (!xe->info.has_pre_prod_wa) {
840 		xe_err(xe, "Pre-production workarounds for this platform have already been removed.\n");
841 		add_taint(TAINT_MACHINE_CHECK, LOCKDEP_STILL_OK);
842 	}
843 }
844 
845 static void xe_device_wedged_fini(struct drm_device *drm, void *arg)
846 {
847 	struct xe_device *xe = arg;
848 
849 	if (atomic_read(&xe->wedged.flag))
850 		xe_pm_runtime_put(xe);
851 }
852 
853 #ifdef CONFIG_DRM_XE_DEBUG_PAGE_SIZE
854 static int xe_debug_page_size_alloc_ctrl_init(struct xe_device *xe)
855 {
856 	int err;
857 
858 	err = drmm_mutex_init(&xe->drm, &xe->page_size_alloc_ctrl.lock);
859 	if (err)
860 		return err;
861 
862 	xe->page_size_alloc_ctrl.mode = XE_PAGE_SIZE_ALLOC_CTRL_MODE_NONE;
863 	xe->page_size_alloc_ctrl.cur_index = 0;
864 
865 	return 0;
866 }
867 #else
868 static int xe_debug_page_size_alloc_ctrl_init(struct xe_device *xe)
869 {
870 	return 0;
871 }
872 #endif
873 
874 int xe_device_probe(struct xe_device *xe)
875 {
876 	struct xe_tile *tile;
877 	struct xe_gt *gt;
878 	int err;
879 	u8 id;
880 
881 	xe_pat_init_early(xe);
882 
883 	err = xe_sriov_init(xe);
884 	if (err)
885 		return err;
886 
887 	xe->info.mem_region_mask = 1;
888 
889 	err = xe_set_dma_info(xe);
890 	if (err)
891 		return err;
892 
893 	err = xe_mmio_probe_tiles(xe);
894 	if (err)
895 		return err;
896 
897 	for_each_gt(gt, xe, id) {
898 		err = xe_gt_init_early(gt);
899 		if (err)
900 			return err;
901 	}
902 
903 	/*
904 	 * Wa_16029380221: The affected GT will always use non-coherent
905 	 * access to page tables, so we must do uncached writes from the
906 	 * CPU.
907 	 */
908 	for_each_gt(gt, xe, id)
909 		if (XE_GT_WA(gt, 16029380221))
910 			xe->info.has_cached_pt = false;
911 
912 	for_each_tile(tile, xe, id) {
913 		err = xe_ggtt_init_early(tile->mem.ggtt);
914 		if (err)
915 			return err;
916 	}
917 
918 	/*
919 	 * From here on, if a step fails, make sure a Driver-FLR is triggereed
920 	 */
921 	err = devm_add_action_or_reset(xe->drm.dev, xe_driver_flr_fini, xe);
922 	if (err)
923 		return err;
924 
925 	err = probe_has_flat_ccs(xe);
926 	if (err)
927 		return err;
928 
929 	err = xe_vram_probe(xe);
930 	if (err)
931 		return err;
932 
933 	for_each_tile(tile, xe, id) {
934 		err = xe_tile_init_noalloc(tile);
935 		if (err)
936 			return err;
937 	}
938 
939 	/*
940 	 * Allow allocations only now to ensure xe_display_init_early()
941 	 * is the first to allocate, always.
942 	 */
943 	err = xe_ttm_sys_mgr_init(xe);
944 	if (err)
945 		return err;
946 
947 	/* Allocate and map stolen after potential VRAM resize */
948 	err = xe_ttm_stolen_mgr_init(xe);
949 	if (err)
950 		return err;
951 
952 	err = xe_soc_remapper_init(xe);
953 	if (err)
954 		return err;
955 
956 	err = xe_sysctrl_init(xe);
957 	if (err)
958 		return err;
959 
960 	xe_ras_init(xe);
961 
962 	/*
963 	 * Now that GT is initialized (TTM in particular),
964 	 * we can try to init display, and inherit the initial fb.
965 	 * This is the reason the first allocation needs to be done
966 	 * inside display.
967 	 */
968 	err = xe_display_init_early(xe);
969 	if (err)
970 		return err;
971 
972 	err = xe_vram_reserve_memtest_bo(xe);
973 	if (err)
974 		return err;
975 
976 	for_each_tile(tile, xe, id) {
977 		err = xe_tile_init(tile);
978 		if (err)
979 			return err;
980 	}
981 
982 	err = xe_irq_install(xe);
983 	if (err)
984 		return err;
985 
986 	for_each_gt(gt, xe, id) {
987 		err = xe_gt_init(gt);
988 		if (err)
989 			return err;
990 	}
991 
992 	err = xe_vram_memtest(xe);
993 	if (err)
994 		return err;
995 
996 	err = xe_pagefault_init(xe);
997 	if (err)
998 		return err;
999 
1000 	if (xe->tiles->media_gt &&
1001 	    XE_GT_WA(xe->tiles->media_gt, 15015404425_disable))
1002 		XE_DEVICE_WA_DISABLE(xe, 15015404425);
1003 
1004 	err = xe_devcoredump_init(xe);
1005 	if (err)
1006 		return err;
1007 
1008 	xe_nvm_init(xe);
1009 
1010 	err = xe_heci_gsc_init(xe);
1011 	if (err)
1012 		return err;
1013 
1014 	err = xe_late_bind_init(&xe->late_bind);
1015 	if (err)
1016 		return err;
1017 
1018 	err = xe_oa_init(xe);
1019 	if (err)
1020 		return err;
1021 
1022 	err = xe_display_init(xe);
1023 	if (err)
1024 		return err;
1025 
1026 	err = xe_pxp_init(xe);
1027 	if (err)
1028 		return err;
1029 
1030 	err = xe_psmi_init(xe);
1031 	if (err)
1032 		return err;
1033 
1034 	err = xe_debug_page_size_alloc_ctrl_init(xe);
1035 	if (err)
1036 		return err;
1037 
1038 	err = drm_dev_register(&xe->drm, 0);
1039 	if (err)
1040 		return err;
1041 
1042 	xe_display_register(xe);
1043 
1044 	err = xe_oa_register(xe);
1045 	if (err)
1046 		goto err_unregister_display;
1047 
1048 	err = xe_pmu_register(&xe->pmu);
1049 	if (err)
1050 		goto err_unregister_display;
1051 
1052 	err = xe_device_sysfs_init(xe);
1053 	if (err)
1054 		goto err_unregister_display;
1055 
1056 	xe_debugfs_register(xe);
1057 
1058 	err = xe_hwmon_register(xe);
1059 	if (err)
1060 		goto err_unregister_display;
1061 
1062 	err = xe_i2c_probe(xe);
1063 	if (err)
1064 		goto err_unregister_display;
1065 
1066 	for_each_gt(gt, xe, id)
1067 		xe_gt_sanitize_freq(gt);
1068 
1069 	xe_vsec_init(xe);
1070 
1071 	err = xe_sriov_init_late(xe);
1072 	if (err)
1073 		goto err_unregister_display;
1074 
1075 	detect_preproduction_hw(xe);
1076 
1077 	err = drmm_add_action_or_reset(&xe->drm, xe_device_wedged_fini, xe);
1078 	if (err)
1079 		goto err_unregister_display;
1080 
1081 	/*
1082 	 * Process and log any errors detected by hardware. Possible results can
1083 	 * include declaring the device as wedged, which must be done only after
1084 	 * xe_device_wedged_fini() is registered.
1085 	 */
1086 	xe_ras_process_errors(xe);
1087 
1088 	err = devm_add_action_or_reset(xe->drm.dev, xe_device_sanitize, xe);
1089 	if (err)
1090 		goto err_unregister_display;
1091 
1092 	return 0;
1093 
1094 err_unregister_display:
1095 	xe_display_unregister(xe);
1096 	drm_dev_unregister(&xe->drm);
1097 
1098 	return err;
1099 }
1100 
1101 void xe_device_remove(struct xe_device *xe)
1102 {
1103 	xe_display_unregister(xe);
1104 
1105 	drm_dev_unplug(&xe->drm);
1106 
1107 	xe_bo_pci_dev_remove_all(xe);
1108 }
1109 
1110 void xe_device_shutdown(struct xe_device *xe)
1111 {
1112 	struct xe_gt *gt;
1113 	u8 id;
1114 
1115 	drm_dbg(&xe->drm, "Shutting down device\n");
1116 
1117 	xe_display_shutdown(xe);
1118 
1119 	xe_irq_suspend(xe);
1120 
1121 	for_each_gt(gt, xe, id)
1122 		xe_gt_shutdown(gt);
1123 
1124 	xe_display_shutdown_late(xe);
1125 
1126 	if (!xe_driver_flr_disabled(xe)) {
1127 		/* BOOM! */
1128 		__xe_driver_flr(xe);
1129 	}
1130 }
1131 
1132 /**
1133  * xe_device_wmb() - Device specific write memory barrier
1134  * @xe: the &xe_device
1135  *
1136  * While wmb() is sufficient for a barrier if we use system memory, on discrete
1137  * platforms with device memory we additionally need to issue a register write.
1138  * Since it doesn't matter which register we write to, use the read-only VF_CAP
1139  * register that is also marked as accessible by the VFs.
1140  */
1141 void xe_device_wmb(struct xe_device *xe)
1142 {
1143 	wmb();
1144 	if (IS_DGFX(xe))
1145 		xe_mmio_write32(xe_root_tile_mmio(xe), VF_CAP_REG, 0);
1146 }
1147 
1148 /*
1149  * Issue a TRANSIENT_FLUSH_REQUEST and wait for completion on each gt.
1150  */
1151 static void tdf_request_sync(struct xe_device *xe)
1152 {
1153 	struct xe_gt *gt;
1154 	u8 id;
1155 
1156 	for_each_gt_with_type(gt, xe, id, BIT(XE_GT_TYPE_MAIN)) {
1157 		CLASS(xe_force_wake, fw_ref)(gt_to_fw(gt), XE_FW_GT);
1158 		if (!fw_ref.domains)
1159 			return;
1160 
1161 		xe_mmio_write32(&gt->mmio, XE2_TDF_CTRL, TRANSIENT_FLUSH_REQUEST);
1162 
1163 		/*
1164 		 * FIXME: We can likely do better here with our choice of
1165 		 * timeout. Currently we just assume the worst case, i.e. 150us,
1166 		 * which is believed to be sufficient to cover the worst case
1167 		 * scenario on current platforms if all cache entries are
1168 		 * transient and need to be flushed..
1169 		 */
1170 		if (xe_mmio_wait32(&gt->mmio, XE2_TDF_CTRL, TRANSIENT_FLUSH_REQUEST, 0,
1171 				   300, NULL, false))
1172 			xe_gt_err_once(gt, "TD flush timeout\n");
1173 	}
1174 }
1175 
1176 /**
1177  * xe_device_is_l2_flush_optimized - if L2 flush is optimized by HW
1178  * @xe: The device to check.
1179  *
1180  * Return: true if the HW device optimizing L2 flush, false otherwise.
1181  */
1182 bool xe_device_is_l2_flush_optimized(struct xe_device *xe)
1183 {
1184 	/* XA is *always* flushed, like at the end-of-submssion (and maybe other
1185 	 * places), just that internally as an optimisation hw doesn't need to make
1186 	 * that a full flush (which will also include XA) when Media is
1187 	 * off/powergated, since it doesn't need to worry about GT caches vs Media
1188 	 * coherency, and only CPU vs GPU coherency, so can make that flush a
1189 	 * targeted XA flush, since stuff tagged with XA now means it's shared with
1190 	 * the CPU. The main implication is that we now need to somehow flush non-XA before
1191 	 * freeing system memory pages, otherwise dirty cachelines could be flushed after the free
1192 	 * (like if Media suddenly turns on and does a full flush)
1193 	 */
1194 	if (GRAPHICS_VER(xe) >= 35 && !IS_DGFX(xe))
1195 		return true;
1196 	return false;
1197 }
1198 
1199 void xe_device_l2_flush(struct xe_device *xe, bool force)
1200 {
1201 	struct xe_gt *gt;
1202 
1203 	gt = xe_root_mmio_gt(xe);
1204 	if (!gt)
1205 		return;
1206 
1207 	if (!force && !XE_GT_WA(gt, 16023588340))
1208 		return;
1209 
1210 	CLASS(xe_force_wake, fw_ref)(gt_to_fw(gt), XE_FW_GT);
1211 	if (!fw_ref.domains)
1212 		return;
1213 
1214 	spin_lock(&gt->global_invl_lock);
1215 
1216 	xe_mmio_write32(&gt->mmio, XE2_GLOBAL_INVAL, 0x1);
1217 	if (xe_mmio_wait32(&gt->mmio, XE2_GLOBAL_INVAL, 0x1, 0x0, 1000, NULL, true))
1218 		xe_gt_err_once(gt, "Global invalidation timeout\n");
1219 
1220 	spin_unlock(&gt->global_invl_lock);
1221 }
1222 
1223 /**
1224  * xe_device_td_flush() - Flush transient L3 cache entries
1225  * @xe: The device
1226  *
1227  * Display engine has direct access to memory and is never coherent with L3/L4
1228  * caches (or CPU caches), however KMD is responsible for specifically flushing
1229  * transient L3 GPU cache entries prior to the flip sequence to ensure scanout
1230  * can happen from such a surface without seeing corruption.
1231  *
1232  * Display surfaces can be tagged as transient by mapping it using one of the
1233  * various L3:XD PAT index modes on Xe2.
1234  *
1235  * Note: On non-discrete xe2 platforms, like LNL, the entire L3 cache is flushed
1236  * at the end of each submission via PIPE_CONTROL for compute/render, since SA
1237  * Media is not coherent with L3 and we want to support render-vs-media
1238  * usescases. For other engines like copy/blt the HW internally forces uncached
1239  * behaviour, hence why we can skip the TDF on such platforms.
1240  */
1241 void xe_device_td_flush(struct xe_device *xe)
1242 {
1243 	struct xe_gt *root_gt;
1244 
1245 	/*
1246 	 * From Xe3p onward the HW takes care of flush of TD entries also along
1247 	 * with flushing XA entries, which will be at the usual sync points,
1248 	 * like at the end of submission, so no manual flush is needed here.
1249 	 */
1250 	if (GRAPHICS_VER(xe) >= 35)
1251 		return;
1252 
1253 	if (!IS_DGFX(xe) || GRAPHICS_VER(xe) < 20)
1254 		return;
1255 
1256 	root_gt = xe_root_mmio_gt(xe);
1257 	if (!root_gt)
1258 		return;
1259 
1260 	if (XE_GT_WA(root_gt, 16023588340)) {
1261 		/* A transient flush is not sufficient: flush the L2 */
1262 		xe_device_l2_flush(xe, false);
1263 	} else {
1264 		xe_guc_pc_apply_flush_freq_limit(&root_gt->uc.guc.pc);
1265 		tdf_request_sync(xe);
1266 		xe_guc_pc_remove_flush_freq_limit(&root_gt->uc.guc.pc);
1267 	}
1268 }
1269 
1270 u32 xe_device_ccs_bytes(struct xe_device *xe, u64 size)
1271 {
1272 	return xe_device_has_flat_ccs(xe) ?
1273 		DIV_ROUND_UP_ULL(size, NUM_BYTES_PER_CCS_BYTE(xe)) : 0;
1274 }
1275 
1276 /**
1277  * xe_device_assert_mem_access - Inspect the current runtime_pm state.
1278  * @xe: xe device instance
1279  *
1280  * To be used before any kind of memory access. It will splat a debug warning
1281  * if the device is currently sleeping. But it doesn't guarantee in any way
1282  * that the device is going to remain awake. Xe PM runtime get and put
1283  * functions might be added to the outer bound of the memory access, while
1284  * this check is intended for inner usage to splat some warning if the worst
1285  * case has just happened.
1286  */
1287 void xe_device_assert_mem_access(struct xe_device *xe)
1288 {
1289 	xe_assert(xe, !xe_pm_runtime_suspended(xe));
1290 }
1291 
1292 void xe_device_snapshot_print(struct xe_device *xe, struct drm_printer *p)
1293 {
1294 	struct xe_gt *gt;
1295 	u8 id;
1296 
1297 	drm_printf(p, "PCI ID: 0x%04x\n", xe->info.devid);
1298 	drm_printf(p, "PCI revision: 0x%02x\n", xe->info.revid);
1299 
1300 	for_each_gt(gt, xe, id) {
1301 		drm_printf(p, "GT id: %u\n", id);
1302 		drm_printf(p, "\tTile: %u\n", gt->tile->id);
1303 		drm_printf(p, "\tType: %s\n",
1304 			   gt->info.type == XE_GT_TYPE_MAIN ? "main" : "media");
1305 		drm_printf(p, "\tIP ver: %u.%u.%u\n",
1306 			   REG_FIELD_GET(GMD_ID_ARCH_MASK, gt->info.gmdid),
1307 			   REG_FIELD_GET(GMD_ID_RELEASE_MASK, gt->info.gmdid),
1308 			   REG_FIELD_GET(GMD_ID_REVID, gt->info.gmdid));
1309 		drm_printf(p, "\tCS reference clock: %u\n", gt->info.reference_clock);
1310 	}
1311 }
1312 
1313 u64 xe_device_canonicalize_addr(struct xe_device *xe, u64 address)
1314 {
1315 	return sign_extend64(address, xe->info.va_bits - 1);
1316 }
1317 
1318 u64 xe_device_uncanonicalize_addr(struct xe_device *xe, u64 address)
1319 {
1320 	return address & GENMASK_ULL(xe->info.va_bits - 1, 0);
1321 }
1322 
1323 /**
1324  * DOC: Xe Device Wedging
1325  *
1326  * Xe driver uses drm device wedged uevent as documented in Documentation/gpu/drm-uapi.rst.
1327  * When device is in wedged state, every IOCTL will be blocked and GT cannot
1328  * be used. The conditions under which the driver declares the device wedged
1329  * depend on the wedged mode configuration (see &enum xe_wedged_mode). The
1330  * default recovery method for a wedged state is rebind/bus-reset.
1331  *
1332  * Another recovery method is vendor-specific. Below are the cases that send
1333  * ``WEDGED=vendor-specific`` recovery method in drm device wedged uevent.
1334  *
1335  * Case: Firmware Flash
1336  * --------------------
1337  *
1338  * Identification Hint
1339  * +++++++++++++++++++
1340  *
1341  * ``WEDGED=vendor-specific`` drm device wedged uevent with
1342  * :ref:`Runtime Survivability mode <xe-survivability-mode>` is used to notify
1343  * admin/userspace consumer about the need for a firmware flash.
1344  *
1345  * Recovery Procedure
1346  * ++++++++++++++++++
1347  *
1348  * Once ``WEDGED=vendor-specific`` drm device wedged uevent is received, follow
1349  * the below steps
1350  *
1351  * - Check Runtime Survivability mode sysfs.
1352  *   If enabled, firmware flash is required to recover the device.
1353  *
1354  *   /sys/bus/pci/devices/<device>/survivability_mode
1355  *
1356  * - Admin/userspace consumer can use firmware flashing tools like fwupd to flash
1357  *   firmware and restore device to normal operation.
1358  */
1359 
1360 /**
1361  * xe_device_set_wedged_method - Set wedged recovery method
1362  * @xe: xe device instance
1363  * @method: recovery method to set
1364  *
1365  * Set wedged recovery method to be sent in drm wedged uevent.
1366  */
1367 void xe_device_set_wedged_method(struct xe_device *xe, unsigned long method)
1368 {
1369 	xe->wedged.method = method;
1370 }
1371 
1372 #define WEDGED_URL	"https://docs.kernel.org/gpu/drm-uapi.html#device-wedging"
1373 #define XE_BUG_URL	"https://gitlab.freedesktop.org/drm/xe/kernel/issues/new"
1374 
1375 /**
1376  * xe_device_declare_wedged - Declare device wedged
1377  * @xe: xe device instance
1378  *
1379  * This is a final state that can only be cleared with the recovery method
1380  * specified in the drm wedged uevent. The method can be set using
1381  * xe_device_set_wedged_method before declaring the device as wedged. If no method
1382  * is set, reprobe (unbind/re-bind) will be sent by default.
1383  *
1384  * In this state every IOCTL will be blocked so the GT cannot be used.
1385  * In general it will be called upon any critical error such as gt reset
1386  * failure or guc loading failure. Userspace will be notified of this state
1387  * through device wedged uevent.
1388  * If xe.wedged module parameter is set to 2, this function will be called
1389  * on every single execution timeout (a.k.a. GPU hang) right after devcoredump
1390  * snapshot capture. In this mode, GT reset won't be attempted so the state of
1391  * the issue is preserved for further debugging.
1392  */
1393 void xe_device_declare_wedged(struct xe_device *xe)
1394 {
1395 	struct xe_gt *gt;
1396 	u8 id;
1397 
1398 	if (xe->wedged.mode == XE_WEDGED_MODE_NEVER) {
1399 		drm_dbg(&xe->drm, "Wedged mode is forcibly disabled\n");
1400 		return;
1401 	}
1402 
1403 	if (!atomic_xchg(&xe->wedged.flag, 1)) {
1404 		xe->needs_flr_on_fini = true;
1405 		xe_pm_runtime_get_noresume(xe);
1406 
1407 		xe_log_err_fatal(xe, WEDGED, -EIO, "Device declared wedged!\n");
1408 		xe_err_once(xe, "IOCTLs and executions are now blocked!\n"
1409 			    "For recovery procedure, refer to %s\n"
1410 			    "Please file a _new_ bug report at %s\n",
1411 			    WEDGED_URL, XE_BUG_URL);
1412 	}
1413 
1414 	for_each_gt(gt, xe, id)
1415 		xe_gt_declare_wedged(gt);
1416 
1417 	if (xe_device_wedged(xe)) {
1418 		/*
1419 		 * XE_WEDGED_MODE_UPON_ANY_HANG_NO_RESET is intended for debugging
1420 		 * hangs, so wedge the device with 'none' recovery method and have
1421 		 * it available to the user for debugging.
1422 		 */
1423 		if (xe->wedged.mode == XE_WEDGED_MODE_UPON_ANY_HANG_NO_RESET)
1424 			xe_device_set_wedged_method(xe, DRM_WEDGE_RECOVERY_NONE);
1425 		/* If no wedge recovery method is set, use default */
1426 		else if (!xe->wedged.method)
1427 			xe_device_set_wedged_method(xe, DRM_WEDGE_RECOVERY_REBIND |
1428 						    DRM_WEDGE_RECOVERY_BUS_RESET);
1429 
1430 		/* Notify userspace of wedged device */
1431 		drm_dev_wedged_event(&xe->drm, xe->wedged.method, NULL);
1432 	}
1433 }
1434 
1435 /**
1436  * xe_device_validate_wedged_mode - Check if given mode is supported
1437  * @xe: the &xe_device
1438  * @mode: requested mode to validate
1439  *
1440  * Check whether the provided wedged mode is supported.
1441  *
1442  * Return: 0 if mode is supported, error code otherwise.
1443  */
1444 int xe_device_validate_wedged_mode(struct xe_device *xe, unsigned int mode)
1445 {
1446 	if (mode > XE_WEDGED_MODE_UPON_ANY_HANG_NO_RESET) {
1447 		drm_dbg(&xe->drm, "wedged_mode: invalid value (%u)\n", mode);
1448 		return -EINVAL;
1449 	} else if (mode == XE_WEDGED_MODE_UPON_ANY_HANG_NO_RESET && (IS_SRIOV_VF(xe) ||
1450 		   (IS_SRIOV_PF(xe) && !IS_ENABLED(CONFIG_DRM_XE_DEBUG)))) {
1451 		drm_dbg(&xe->drm, "wedged_mode: (%u) %s mode is not supported for %s\n",
1452 			mode, xe_wedged_mode_to_string(mode),
1453 			xe_sriov_mode_to_string(xe_device_sriov_mode(xe)));
1454 		return -EPERM;
1455 	}
1456 
1457 	return 0;
1458 }
1459 
1460 /**
1461  * xe_wedged_mode_to_string - Convert enum value to string.
1462  * @mode: the &xe_wedged_mode to convert
1463  *
1464  * Returns: wedged mode as a user friendly string.
1465  */
1466 const char *xe_wedged_mode_to_string(enum xe_wedged_mode mode)
1467 {
1468 	switch (mode) {
1469 	case XE_WEDGED_MODE_NEVER:
1470 		return "never";
1471 	case XE_WEDGED_MODE_UPON_CRITICAL_ERROR:
1472 		return "upon-critical-error";
1473 	case XE_WEDGED_MODE_UPON_ANY_HANG_NO_RESET:
1474 		return "upon-any-hang-no-reset";
1475 	default:
1476 		return "<invalid>";
1477 	}
1478 }
1479 
1480 /**
1481  * xe_device_asid_to_vm() - Find VM from ASID
1482  * @xe: the &xe_device
1483  * @asid: Address space ID
1484  *
1485  * Find a VM from ASID and take a reference to VM which caller must drop.
1486  * Reclaim safe.
1487  *
1488  * Return: VM on success, ERR_PTR on failure
1489  */
1490 struct xe_vm *xe_device_asid_to_vm(struct xe_device *xe, u32 asid)
1491 {
1492 	struct xe_vm *vm;
1493 
1494 	down_read(&xe->usm.lock);
1495 	vm = xa_load(&xe->usm.asid_to_vm, asid);
1496 	if (vm)
1497 		xe_vm_get(vm);
1498 	else
1499 		vm = ERR_PTR(-EINVAL);
1500 	up_read(&xe->usm.lock);
1501 
1502 	return vm;
1503 }
1504