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