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