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