xref: /linux/drivers/gpu/drm/xe/xe_device.c (revision 37aeccf5f839c155e8c9100937a01059b24e61b5)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2021 Intel Corporation
4  */
5 
6 #include "xe_device.h"
7 
8 #include <linux/delay.h>
9 #include <linux/fault-inject.h>
10 #include <linux/units.h>
11 
12 #include <drm/drm_aperture.h>
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 = drm_aperture_remove_conflicting_pci_framebuffers(pdev, &driver);
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 	u32 reg;
608 	int err;
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 	err = xe_force_wake_get(gt_to_fw(gt), XE_FW_GT);
617 	if (err)
618 		return err;
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 	return xe_force_wake_put(gt_to_fw(gt), XE_FW_GT);
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 	return devm_add_action_or_reset(xe->drm.dev, xe_device_sanitize, xe);
772 
773 err_fini_display:
774 	xe_display_driver_remove(xe);
775 
776 err_fini_oa:
777 	xe_oa_fini(xe);
778 
779 err_fini_gt:
780 	for_each_gt(gt, xe, id) {
781 		if (id < last_gt)
782 			xe_gt_remove(gt);
783 		else
784 			break;
785 	}
786 
787 err:
788 	xe_display_fini(xe);
789 	return err;
790 }
791 
792 static void xe_device_remove_display(struct xe_device *xe)
793 {
794 	xe_display_unregister(xe);
795 
796 	drm_dev_unplug(&xe->drm);
797 	xe_display_driver_remove(xe);
798 }
799 
800 void xe_device_remove(struct xe_device *xe)
801 {
802 	struct xe_gt *gt;
803 	u8 id;
804 
805 	xe_oa_unregister(xe);
806 
807 	xe_device_remove_display(xe);
808 
809 	xe_display_fini(xe);
810 
811 	xe_oa_fini(xe);
812 
813 	xe_heci_gsc_fini(xe);
814 
815 	for_each_gt(gt, xe, id)
816 		xe_gt_remove(gt);
817 }
818 
819 void xe_device_shutdown(struct xe_device *xe)
820 {
821 	struct xe_gt *gt;
822 	u8 id;
823 
824 	drm_dbg(&xe->drm, "Shutting down device\n");
825 
826 	if (xe_driver_flr_disabled(xe)) {
827 		xe_display_pm_shutdown(xe);
828 
829 		xe_irq_suspend(xe);
830 
831 		for_each_gt(gt, xe, id)
832 			xe_gt_shutdown(gt);
833 
834 		xe_display_pm_shutdown_late(xe);
835 	} else {
836 		/* BOOM! */
837 		__xe_driver_flr(xe);
838 	}
839 }
840 
841 /**
842  * xe_device_wmb() - Device specific write memory barrier
843  * @xe: the &xe_device
844  *
845  * While wmb() is sufficient for a barrier if we use system memory, on discrete
846  * platforms with device memory we additionally need to issue a register write.
847  * Since it doesn't matter which register we write to, use the read-only VF_CAP
848  * register that is also marked as accessible by the VFs.
849  */
850 void xe_device_wmb(struct xe_device *xe)
851 {
852 	wmb();
853 	if (IS_DGFX(xe))
854 		xe_mmio_write32(xe_root_tile_mmio(xe), VF_CAP_REG, 0);
855 }
856 
857 /**
858  * xe_device_td_flush() - Flush transient L3 cache entries
859  * @xe: The device
860  *
861  * Display engine has direct access to memory and is never coherent with L3/L4
862  * caches (or CPU caches), however KMD is responsible for specifically flushing
863  * transient L3 GPU cache entries prior to the flip sequence to ensure scanout
864  * can happen from such a surface without seeing corruption.
865  *
866  * Display surfaces can be tagged as transient by mapping it using one of the
867  * various L3:XD PAT index modes on Xe2.
868  *
869  * Note: On non-discrete xe2 platforms, like LNL, the entire L3 cache is flushed
870  * at the end of each submission via PIPE_CONTROL for compute/render, since SA
871  * Media is not coherent with L3 and we want to support render-vs-media
872  * usescases. For other engines like copy/blt the HW internally forces uncached
873  * behaviour, hence why we can skip the TDF on such platforms.
874  */
875 void xe_device_td_flush(struct xe_device *xe)
876 {
877 	struct xe_gt *gt;
878 	u8 id;
879 
880 	if (!IS_DGFX(xe) || GRAPHICS_VER(xe) < 20)
881 		return;
882 
883 	if (XE_WA(xe_root_mmio_gt(xe), 16023588340)) {
884 		xe_device_l2_flush(xe);
885 		return;
886 	}
887 
888 	for_each_gt(gt, xe, id) {
889 		if (xe_gt_is_media_type(gt))
890 			continue;
891 
892 		if (xe_force_wake_get(gt_to_fw(gt), XE_FW_GT))
893 			return;
894 
895 		xe_mmio_write32(&gt->mmio, XE2_TDF_CTRL, TRANSIENT_FLUSH_REQUEST);
896 		/*
897 		 * FIXME: We can likely do better here with our choice of
898 		 * timeout. Currently we just assume the worst case, i.e. 150us,
899 		 * which is believed to be sufficient to cover the worst case
900 		 * scenario on current platforms if all cache entries are
901 		 * transient and need to be flushed..
902 		 */
903 		if (xe_mmio_wait32(&gt->mmio, XE2_TDF_CTRL, TRANSIENT_FLUSH_REQUEST, 0,
904 				   150, NULL, false))
905 			xe_gt_err_once(gt, "TD flush timeout\n");
906 
907 		xe_force_wake_put(gt_to_fw(gt), XE_FW_GT);
908 	}
909 }
910 
911 void xe_device_l2_flush(struct xe_device *xe)
912 {
913 	struct xe_gt *gt;
914 	int err;
915 
916 	gt = xe_root_mmio_gt(xe);
917 
918 	if (!XE_WA(gt, 16023588340))
919 		return;
920 
921 	err = xe_force_wake_get(gt_to_fw(gt), XE_FW_GT);
922 	if (err)
923 		return;
924 
925 	spin_lock(&gt->global_invl_lock);
926 	xe_mmio_write32(&gt->mmio, XE2_GLOBAL_INVAL, 0x1);
927 
928 	if (xe_mmio_wait32(&gt->mmio, XE2_GLOBAL_INVAL, 0x1, 0x0, 150, NULL, true))
929 		xe_gt_err_once(gt, "Global invalidation timeout\n");
930 	spin_unlock(&gt->global_invl_lock);
931 
932 	xe_force_wake_put(gt_to_fw(gt), XE_FW_GT);
933 }
934 
935 u32 xe_device_ccs_bytes(struct xe_device *xe, u64 size)
936 {
937 	return xe_device_has_flat_ccs(xe) ?
938 		DIV_ROUND_UP_ULL(size, NUM_BYTES_PER_CCS_BYTE(xe)) : 0;
939 }
940 
941 /**
942  * xe_device_assert_mem_access - Inspect the current runtime_pm state.
943  * @xe: xe device instance
944  *
945  * To be used before any kind of memory access. It will splat a debug warning
946  * if the device is currently sleeping. But it doesn't guarantee in any way
947  * that the device is going to remain awake. Xe PM runtime get and put
948  * functions might be added to the outer bound of the memory access, while
949  * this check is intended for inner usage to splat some warning if the worst
950  * case has just happened.
951  */
952 void xe_device_assert_mem_access(struct xe_device *xe)
953 {
954 	xe_assert(xe, !xe_pm_runtime_suspended(xe));
955 }
956 
957 void xe_device_snapshot_print(struct xe_device *xe, struct drm_printer *p)
958 {
959 	struct xe_gt *gt;
960 	u8 id;
961 
962 	drm_printf(p, "PCI ID: 0x%04x\n", xe->info.devid);
963 	drm_printf(p, "PCI revision: 0x%02x\n", xe->info.revid);
964 
965 	for_each_gt(gt, xe, id) {
966 		drm_printf(p, "GT id: %u\n", id);
967 		drm_printf(p, "\tTile: %u\n", gt->tile->id);
968 		drm_printf(p, "\tType: %s\n",
969 			   gt->info.type == XE_GT_TYPE_MAIN ? "main" : "media");
970 		drm_printf(p, "\tIP ver: %u.%u.%u\n",
971 			   REG_FIELD_GET(GMD_ID_ARCH_MASK, gt->info.gmdid),
972 			   REG_FIELD_GET(GMD_ID_RELEASE_MASK, gt->info.gmdid),
973 			   REG_FIELD_GET(GMD_ID_REVID, gt->info.gmdid));
974 		drm_printf(p, "\tCS reference clock: %u\n", gt->info.reference_clock);
975 	}
976 }
977 
978 u64 xe_device_canonicalize_addr(struct xe_device *xe, u64 address)
979 {
980 	return sign_extend64(address, xe->info.va_bits - 1);
981 }
982 
983 u64 xe_device_uncanonicalize_addr(struct xe_device *xe, u64 address)
984 {
985 	return address & GENMASK_ULL(xe->info.va_bits - 1, 0);
986 }
987 
988 static void xe_device_wedged_fini(struct drm_device *drm, void *arg)
989 {
990 	struct xe_device *xe = arg;
991 
992 	xe_pm_runtime_put(xe);
993 }
994 
995 /**
996  * xe_device_declare_wedged - Declare device wedged
997  * @xe: xe device instance
998  *
999  * This is a final state that can only be cleared with a mudule
1000  * re-probe (unbind + bind).
1001  * In this state every IOCTL will be blocked so the GT cannot be used.
1002  * In general it will be called upon any critical error such as gt reset
1003  * failure or guc loading failure.
1004  * If xe.wedged module parameter is set to 2, this function will be called
1005  * on every single execution timeout (a.k.a. GPU hang) right after devcoredump
1006  * snapshot capture. In this mode, GT reset won't be attempted so the state of
1007  * the issue is preserved for further debugging.
1008  */
1009 void xe_device_declare_wedged(struct xe_device *xe)
1010 {
1011 	struct xe_gt *gt;
1012 	u8 id;
1013 
1014 	if (xe->wedged.mode == 0) {
1015 		drm_dbg(&xe->drm, "Wedged mode is forcibly disabled\n");
1016 		return;
1017 	}
1018 
1019 	xe_pm_runtime_get_noresume(xe);
1020 
1021 	if (drmm_add_action_or_reset(&xe->drm, xe_device_wedged_fini, xe)) {
1022 		drm_err(&xe->drm, "Failed to register xe_device_wedged_fini clean-up. Although device is wedged.\n");
1023 		return;
1024 	}
1025 
1026 	if (!atomic_xchg(&xe->wedged.flag, 1)) {
1027 		xe->needs_flr_on_fini = true;
1028 		drm_err(&xe->drm,
1029 			"CRITICAL: Xe has declared device %s as wedged.\n"
1030 			"IOCTLs and executions are blocked. Only a rebind may clear the failure\n"
1031 			"Please file a _new_ bug report at https://gitlab.freedesktop.org/drm/xe/kernel/issues/new\n",
1032 			dev_name(xe->drm.dev));
1033 	}
1034 
1035 	for_each_gt(gt, xe, id)
1036 		xe_gt_declare_wedged(gt);
1037 }
1038