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