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