xref: /linux/drivers/gpu/drm/xe/xe_device.c (revision 2de3f38fbf89d3cb96d1237aa7a10c0f6480f450)
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_shrinker.h"
56 #include "xe_survivability_mode.h"
57 #include "xe_sriov.h"
58 #include "xe_tile.h"
59 #include "xe_ttm_stolen_mgr.h"
60 #include "xe_ttm_sys_mgr.h"
61 #include "xe_vm.h"
62 #include "xe_vram.h"
63 #include "xe_vsec.h"
64 #include "xe_wait_user_fence.h"
65 #include "xe_wa.h"
66 
67 #include <generated/xe_wa_oob.h>
68 
69 static int xe_file_open(struct drm_device *dev, struct drm_file *file)
70 {
71 	struct xe_device *xe = to_xe_device(dev);
72 	struct xe_drm_client *client;
73 	struct xe_file *xef;
74 	int ret = -ENOMEM;
75 	struct task_struct *task = NULL;
76 
77 	xef = kzalloc(sizeof(*xef), GFP_KERNEL);
78 	if (!xef)
79 		return ret;
80 
81 	client = xe_drm_client_alloc();
82 	if (!client) {
83 		kfree(xef);
84 		return ret;
85 	}
86 
87 	xef->drm = file;
88 	xef->client = client;
89 	xef->xe = xe;
90 
91 	mutex_init(&xef->vm.lock);
92 	xa_init_flags(&xef->vm.xa, XA_FLAGS_ALLOC1);
93 
94 	mutex_init(&xef->exec_queue.lock);
95 	xa_init_flags(&xef->exec_queue.xa, XA_FLAGS_ALLOC1);
96 
97 	file->driver_priv = xef;
98 	kref_init(&xef->refcount);
99 
100 	task = get_pid_task(rcu_access_pointer(file->pid), PIDTYPE_PID);
101 	if (task) {
102 		xef->process_name = kstrdup(task->comm, GFP_KERNEL);
103 		xef->pid = task->pid;
104 		put_task_struct(task);
105 	}
106 
107 	return 0;
108 }
109 
110 static void xe_file_destroy(struct kref *ref)
111 {
112 	struct xe_file *xef = container_of(ref, struct xe_file, refcount);
113 
114 	xa_destroy(&xef->exec_queue.xa);
115 	mutex_destroy(&xef->exec_queue.lock);
116 	xa_destroy(&xef->vm.xa);
117 	mutex_destroy(&xef->vm.lock);
118 
119 	xe_drm_client_put(xef->client);
120 	kfree(xef->process_name);
121 	kfree(xef);
122 }
123 
124 /**
125  * xe_file_get() - Take a reference to the xe file object
126  * @xef: Pointer to the xe file
127  *
128  * Anyone with a pointer to xef must take a reference to the xe file
129  * object using this call.
130  *
131  * Return: xe file pointer
132  */
133 struct xe_file *xe_file_get(struct xe_file *xef)
134 {
135 	kref_get(&xef->refcount);
136 	return xef;
137 }
138 
139 /**
140  * xe_file_put() - Drop a reference to the xe file object
141  * @xef: Pointer to the xe file
142  *
143  * Used to drop reference to the xef object
144  */
145 void xe_file_put(struct xe_file *xef)
146 {
147 	kref_put(&xef->refcount, xe_file_destroy);
148 }
149 
150 static void xe_file_close(struct drm_device *dev, struct drm_file *file)
151 {
152 	struct xe_device *xe = to_xe_device(dev);
153 	struct xe_file *xef = file->driver_priv;
154 	struct xe_vm *vm;
155 	struct xe_exec_queue *q;
156 	unsigned long idx;
157 
158 	xe_pm_runtime_get(xe);
159 
160 	/*
161 	 * No need for exec_queue.lock here as there is no contention for it
162 	 * when FD is closing as IOCTLs presumably can't be modifying the
163 	 * xarray. Taking exec_queue.lock here causes undue dependency on
164 	 * vm->lock taken during xe_exec_queue_kill().
165 	 */
166 	xa_for_each(&xef->exec_queue.xa, idx, q) {
167 		if (q->vm && q->hwe->hw_engine_group)
168 			xe_hw_engine_group_del_exec_queue(q->hwe->hw_engine_group, q);
169 		xe_exec_queue_kill(q);
170 		xe_exec_queue_put(q);
171 	}
172 	xa_for_each(&xef->vm.xa, idx, vm)
173 		xe_vm_close_and_put(vm);
174 
175 	xe_file_put(xef);
176 
177 	xe_pm_runtime_put(xe);
178 }
179 
180 static const struct drm_ioctl_desc xe_ioctls[] = {
181 	DRM_IOCTL_DEF_DRV(XE_DEVICE_QUERY, xe_query_ioctl, DRM_RENDER_ALLOW),
182 	DRM_IOCTL_DEF_DRV(XE_GEM_CREATE, xe_gem_create_ioctl, DRM_RENDER_ALLOW),
183 	DRM_IOCTL_DEF_DRV(XE_GEM_MMAP_OFFSET, xe_gem_mmap_offset_ioctl,
184 			  DRM_RENDER_ALLOW),
185 	DRM_IOCTL_DEF_DRV(XE_VM_CREATE, xe_vm_create_ioctl, DRM_RENDER_ALLOW),
186 	DRM_IOCTL_DEF_DRV(XE_VM_DESTROY, xe_vm_destroy_ioctl, DRM_RENDER_ALLOW),
187 	DRM_IOCTL_DEF_DRV(XE_VM_BIND, xe_vm_bind_ioctl, DRM_RENDER_ALLOW),
188 	DRM_IOCTL_DEF_DRV(XE_EXEC, xe_exec_ioctl, DRM_RENDER_ALLOW),
189 	DRM_IOCTL_DEF_DRV(XE_EXEC_QUEUE_CREATE, xe_exec_queue_create_ioctl,
190 			  DRM_RENDER_ALLOW),
191 	DRM_IOCTL_DEF_DRV(XE_EXEC_QUEUE_DESTROY, xe_exec_queue_destroy_ioctl,
192 			  DRM_RENDER_ALLOW),
193 	DRM_IOCTL_DEF_DRV(XE_EXEC_QUEUE_GET_PROPERTY, xe_exec_queue_get_property_ioctl,
194 			  DRM_RENDER_ALLOW),
195 	DRM_IOCTL_DEF_DRV(XE_WAIT_USER_FENCE, xe_wait_user_fence_ioctl,
196 			  DRM_RENDER_ALLOW),
197 	DRM_IOCTL_DEF_DRV(XE_OBSERVATION, xe_observation_ioctl, DRM_RENDER_ALLOW),
198 };
199 
200 static long xe_drm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
201 {
202 	struct drm_file *file_priv = file->private_data;
203 	struct xe_device *xe = to_xe_device(file_priv->minor->dev);
204 	long ret;
205 
206 	if (xe_device_wedged(xe))
207 		return -ECANCELED;
208 
209 	ret = xe_pm_runtime_get_ioctl(xe);
210 	if (ret >= 0)
211 		ret = drm_ioctl(file, cmd, arg);
212 	xe_pm_runtime_put(xe);
213 
214 	return ret;
215 }
216 
217 #ifdef CONFIG_COMPAT
218 static long xe_drm_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
219 {
220 	struct drm_file *file_priv = file->private_data;
221 	struct xe_device *xe = to_xe_device(file_priv->minor->dev);
222 	long ret;
223 
224 	if (xe_device_wedged(xe))
225 		return -ECANCELED;
226 
227 	ret = xe_pm_runtime_get_ioctl(xe);
228 	if (ret >= 0)
229 		ret = drm_compat_ioctl(file, cmd, arg);
230 	xe_pm_runtime_put(xe);
231 
232 	return ret;
233 }
234 #else
235 /* similarly to drm_compat_ioctl, let's it be assigned to .compat_ioct unconditionally */
236 #define xe_drm_compat_ioctl NULL
237 #endif
238 
239 static void barrier_open(struct vm_area_struct *vma)
240 {
241 	drm_dev_get(vma->vm_private_data);
242 }
243 
244 static void barrier_close(struct vm_area_struct *vma)
245 {
246 	drm_dev_put(vma->vm_private_data);
247 }
248 
249 static void barrier_release_dummy_page(struct drm_device *dev, void *res)
250 {
251 	struct page *dummy_page = (struct page *)res;
252 
253 	__free_page(dummy_page);
254 }
255 
256 static vm_fault_t barrier_fault(struct vm_fault *vmf)
257 {
258 	struct drm_device *dev = vmf->vma->vm_private_data;
259 	struct vm_area_struct *vma = vmf->vma;
260 	vm_fault_t ret = VM_FAULT_NOPAGE;
261 	pgprot_t prot;
262 	int idx;
263 
264 	prot = vm_get_page_prot(vma->vm_flags);
265 
266 	if (drm_dev_enter(dev, &idx)) {
267 		unsigned long pfn;
268 
269 #define LAST_DB_PAGE_OFFSET 0x7ff001
270 		pfn = PHYS_PFN(pci_resource_start(to_pci_dev(dev->dev), 0) +
271 				LAST_DB_PAGE_OFFSET);
272 		ret = vmf_insert_pfn_prot(vma, vma->vm_start, pfn,
273 					  pgprot_noncached(prot));
274 		drm_dev_exit(idx);
275 	} else {
276 		struct page *page;
277 
278 		/* Allocate new dummy page to map all the VA range in this VMA to it*/
279 		page = alloc_page(GFP_KERNEL | __GFP_ZERO);
280 		if (!page)
281 			return VM_FAULT_OOM;
282 
283 		/* Set the page to be freed using drmm release action */
284 		if (drmm_add_action_or_reset(dev, barrier_release_dummy_page, page))
285 			return VM_FAULT_OOM;
286 
287 		ret = vmf_insert_pfn_prot(vma, vma->vm_start, page_to_pfn(page),
288 					  prot);
289 	}
290 
291 	return ret;
292 }
293 
294 static const struct vm_operations_struct vm_ops_barrier = {
295 	.open = barrier_open,
296 	.close = barrier_close,
297 	.fault = barrier_fault,
298 };
299 
300 static int xe_pci_barrier_mmap(struct file *filp,
301 			       struct vm_area_struct *vma)
302 {
303 	struct drm_file *priv = filp->private_data;
304 	struct drm_device *dev = priv->minor->dev;
305 	struct xe_device *xe = to_xe_device(dev);
306 
307 	if (!IS_DGFX(xe))
308 		return -EINVAL;
309 
310 	if (vma->vm_end - vma->vm_start > SZ_4K)
311 		return -EINVAL;
312 
313 	if (is_cow_mapping(vma->vm_flags))
314 		return -EINVAL;
315 
316 	if (vma->vm_flags & (VM_READ | VM_EXEC))
317 		return -EINVAL;
318 
319 	vm_flags_clear(vma, VM_MAYREAD | VM_MAYEXEC);
320 	vm_flags_set(vma, VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP | VM_IO);
321 	vma->vm_ops = &vm_ops_barrier;
322 	vma->vm_private_data = dev;
323 	drm_dev_get(vma->vm_private_data);
324 
325 	return 0;
326 }
327 
328 static int xe_mmap(struct file *filp, struct vm_area_struct *vma)
329 {
330 	struct drm_file *priv = filp->private_data;
331 	struct drm_device *dev = priv->minor->dev;
332 
333 	if (drm_dev_is_unplugged(dev))
334 		return -ENODEV;
335 
336 	switch (vma->vm_pgoff) {
337 	case XE_PCI_BARRIER_MMAP_OFFSET >> XE_PTE_SHIFT:
338 		return xe_pci_barrier_mmap(filp, vma);
339 	}
340 
341 	return drm_gem_mmap(filp, vma);
342 }
343 
344 static const struct file_operations xe_driver_fops = {
345 	.owner = THIS_MODULE,
346 	.open = drm_open,
347 	.release = drm_release_noglobal,
348 	.unlocked_ioctl = xe_drm_ioctl,
349 	.mmap = xe_mmap,
350 	.poll = drm_poll,
351 	.read = drm_read,
352 	.compat_ioctl = xe_drm_compat_ioctl,
353 	.llseek = noop_llseek,
354 #ifdef CONFIG_PROC_FS
355 	.show_fdinfo = drm_show_fdinfo,
356 #endif
357 	.fop_flags = FOP_UNSIGNED_OFFSET,
358 };
359 
360 static struct drm_driver driver = {
361 	/* Don't use MTRRs here; the Xserver or userspace app should
362 	 * deal with them for Intel hardware.
363 	 */
364 	.driver_features =
365 	    DRIVER_GEM |
366 	    DRIVER_RENDER | DRIVER_SYNCOBJ |
367 	    DRIVER_SYNCOBJ_TIMELINE | DRIVER_GEM_GPUVA,
368 	.open = xe_file_open,
369 	.postclose = xe_file_close,
370 
371 	.gem_prime_import = xe_gem_prime_import,
372 
373 	.dumb_create = xe_bo_dumb_create,
374 	.dumb_map_offset = drm_gem_ttm_dumb_map_offset,
375 #ifdef CONFIG_PROC_FS
376 	.show_fdinfo = xe_drm_client_fdinfo,
377 #endif
378 	.ioctls = xe_ioctls,
379 	.num_ioctls = ARRAY_SIZE(xe_ioctls),
380 	.fops = &xe_driver_fops,
381 	.name = DRIVER_NAME,
382 	.desc = DRIVER_DESC,
383 	.major = DRIVER_MAJOR,
384 	.minor = DRIVER_MINOR,
385 	.patchlevel = DRIVER_PATCHLEVEL,
386 };
387 
388 static void xe_device_destroy(struct drm_device *dev, void *dummy)
389 {
390 	struct xe_device *xe = to_xe_device(dev);
391 
392 	xe_bo_dev_fini(&xe->bo_device);
393 
394 	if (xe->preempt_fence_wq)
395 		destroy_workqueue(xe->preempt_fence_wq);
396 
397 	if (xe->ordered_wq)
398 		destroy_workqueue(xe->ordered_wq);
399 
400 	if (xe->unordered_wq)
401 		destroy_workqueue(xe->unordered_wq);
402 
403 	if (!IS_ERR_OR_NULL(xe->mem.shrinker))
404 		xe_shrinker_destroy(xe->mem.shrinker);
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 	xe_bo_dev_init(&xe->bo_device);
435 	err = drmm_add_action_or_reset(&xe->drm, xe_device_destroy, NULL);
436 	if (err)
437 		goto err;
438 
439 	xe->mem.shrinker = xe_shrinker_create(xe);
440 	if (IS_ERR(xe->mem.shrinker))
441 		return ERR_CAST(xe->mem.shrinker);
442 
443 	xe->info.devid = pdev->device;
444 	xe->info.revid = pdev->revision;
445 	xe->info.force_execlist = xe_modparam.force_execlist;
446 
447 	err = xe_irq_init(xe);
448 	if (err)
449 		goto err;
450 
451 	init_waitqueue_head(&xe->ufence_wq);
452 
453 	init_rwsem(&xe->usm.lock);
454 
455 	xa_init_flags(&xe->usm.asid_to_vm, XA_FLAGS_ALLOC);
456 
457 	if (IS_ENABLED(CONFIG_DRM_XE_DEBUG)) {
458 		/* Trigger a large asid and an early asid wrap. */
459 		u32 asid;
460 
461 		BUILD_BUG_ON(XE_MAX_ASID < 2);
462 		err = xa_alloc_cyclic(&xe->usm.asid_to_vm, &asid, NULL,
463 				      XA_LIMIT(XE_MAX_ASID - 2, XE_MAX_ASID - 1),
464 				      &xe->usm.next_asid, GFP_KERNEL);
465 		drm_WARN_ON(&xe->drm, err);
466 		if (err >= 0)
467 			xa_erase(&xe->usm.asid_to_vm, asid);
468 	}
469 
470 	spin_lock_init(&xe->pinned.lock);
471 	INIT_LIST_HEAD(&xe->pinned.kernel_bo_present);
472 	INIT_LIST_HEAD(&xe->pinned.external_vram);
473 	INIT_LIST_HEAD(&xe->pinned.evicted);
474 
475 	xe->preempt_fence_wq = alloc_ordered_workqueue("xe-preempt-fence-wq",
476 						       WQ_MEM_RECLAIM);
477 	xe->ordered_wq = alloc_ordered_workqueue("xe-ordered-wq", 0);
478 	xe->unordered_wq = alloc_workqueue("xe-unordered-wq", 0, 0);
479 	xe->destroy_wq = alloc_workqueue("xe-destroy-wq", 0, 0);
480 	if (!xe->ordered_wq || !xe->unordered_wq ||
481 	    !xe->preempt_fence_wq || !xe->destroy_wq) {
482 		/*
483 		 * Cleanup done in xe_device_destroy via
484 		 * drmm_add_action_or_reset register above
485 		 */
486 		drm_err(&xe->drm, "Failed to allocate xe workqueues\n");
487 		err = -ENOMEM;
488 		goto err;
489 	}
490 
491 	err = drmm_mutex_init(&xe->drm, &xe->pmt.lock);
492 	if (err)
493 		goto err;
494 
495 	err = xe_display_create(xe);
496 	if (WARN_ON(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_gscfi = 0;
687 		xe->info.skip_guc_pc = 1;
688 		xe->info.skip_pcode = 1;
689 	}
690 }
691 
692 /**
693  * xe_device_probe_early: Device early probe
694  * @xe: xe device instance
695  *
696  * Initialize MMIO resources that don't require any
697  * knowledge about tile count. Also initialize pcode and
698  * check vram initialization on root tile.
699  *
700  * Return: 0 on success, error code on failure
701  */
702 int xe_device_probe_early(struct xe_device *xe)
703 {
704 	int err;
705 
706 	err = xe_mmio_probe_early(xe);
707 	if (err)
708 		return err;
709 
710 	xe_sriov_probe_early(xe);
711 
712 	sriov_update_device_info(xe);
713 
714 	err = xe_pcode_probe_early(xe);
715 	if (err) {
716 		int save_err = err;
717 
718 		/*
719 		 * Try to leave device in survivability mode if device is
720 		 * possible, but still return the previous error for error
721 		 * propagation
722 		 */
723 		err = xe_survivability_mode_enable(xe);
724 		if (err)
725 			return err;
726 
727 		return save_err;
728 	}
729 
730 	err = wait_for_lmem_ready(xe);
731 	if (err)
732 		return err;
733 
734 	xe->wedged.mode = xe_modparam.wedged_mode;
735 
736 	return 0;
737 }
738 ALLOW_ERROR_INJECTION(xe_device_probe_early, ERRNO); /* See xe_pci_probe() */
739 
740 static int probe_has_flat_ccs(struct xe_device *xe)
741 {
742 	struct xe_gt *gt;
743 	unsigned int fw_ref;
744 	u32 reg;
745 
746 	/* Always enabled/disabled, no runtime check to do */
747 	if (GRAPHICS_VER(xe) < 20 || !xe->info.has_flat_ccs || IS_SRIOV_VF(xe))
748 		return 0;
749 
750 	gt = xe_root_mmio_gt(xe);
751 
752 	fw_ref = xe_force_wake_get(gt_to_fw(gt), XE_FW_GT);
753 	if (!fw_ref)
754 		return -ETIMEDOUT;
755 
756 	reg = xe_gt_mcr_unicast_read_any(gt, XE2_FLAT_CCS_BASE_RANGE_LOWER);
757 	xe->info.has_flat_ccs = (reg & XE2_FLAT_CCS_ENABLE);
758 
759 	if (!xe->info.has_flat_ccs)
760 		drm_dbg(&xe->drm,
761 			"Flat CCS has been disabled in bios, May lead to performance impact");
762 
763 	xe_force_wake_put(gt_to_fw(gt), fw_ref);
764 
765 	return 0;
766 }
767 
768 int xe_device_probe(struct xe_device *xe)
769 {
770 	struct xe_tile *tile;
771 	struct xe_gt *gt;
772 	int err;
773 	u8 id;
774 
775 	xe_pat_init_early(xe);
776 
777 	err = xe_sriov_init(xe);
778 	if (err)
779 		return err;
780 
781 	xe->info.mem_region_mask = 1;
782 
783 	err = xe_set_dma_info(xe);
784 	if (err)
785 		return err;
786 
787 	err = xe_mmio_probe_tiles(xe);
788 	if (err)
789 		return err;
790 
791 	err = xe_ttm_sys_mgr_init(xe);
792 	if (err)
793 		return err;
794 
795 	for_each_gt(gt, xe, id) {
796 		err = xe_gt_init_early(gt);
797 		if (err)
798 			return err;
799 
800 		/*
801 		 * Only after this point can GT-specific MMIO operations
802 		 * (including things like communication with the GuC)
803 		 * be performed.
804 		 */
805 		xe_gt_mmio_init(gt);
806 	}
807 
808 	for_each_tile(tile, xe, id) {
809 		if (IS_SRIOV_VF(xe)) {
810 			xe_guc_comm_init_early(&tile->primary_gt->uc.guc);
811 			err = xe_gt_sriov_vf_bootstrap(tile->primary_gt);
812 			if (err)
813 				return err;
814 			err = xe_gt_sriov_vf_query_config(tile->primary_gt);
815 			if (err)
816 				return err;
817 		}
818 		err = xe_ggtt_init_early(tile->mem.ggtt);
819 		if (err)
820 			return err;
821 		err = xe_memirq_init(&tile->memirq);
822 		if (err)
823 			return err;
824 	}
825 
826 	for_each_gt(gt, xe, id) {
827 		err = xe_gt_init_hwconfig(gt);
828 		if (err)
829 			return err;
830 	}
831 
832 	err = xe_devcoredump_init(xe);
833 	if (err)
834 		return err;
835 
836 	/*
837 	 * From here on, if a step fails, make sure a Driver-FLR is triggereed
838 	 */
839 	err = devm_add_action_or_reset(xe->drm.dev, xe_driver_flr_fini, xe);
840 	if (err)
841 		return err;
842 
843 	err = probe_has_flat_ccs(xe);
844 	if (err)
845 		return err;
846 
847 	err = xe_vram_probe(xe);
848 	if (err)
849 		return err;
850 
851 	for_each_tile(tile, xe, id) {
852 		err = xe_tile_init_noalloc(tile);
853 		if (err)
854 			return err;
855 	}
856 
857 	/* Allocate and map stolen after potential VRAM resize */
858 	err = xe_ttm_stolen_mgr_init(xe);
859 	if (err)
860 		return err;
861 
862 	/*
863 	 * Now that GT is initialized (TTM in particular),
864 	 * we can try to init display, and inherit the initial fb.
865 	 * This is the reason the first allocation needs to be done
866 	 * inside display.
867 	 */
868 	err = xe_display_init_early(xe);
869 	if (err)
870 		return err;
871 
872 	for_each_tile(tile, xe, id) {
873 		err = xe_tile_init(tile);
874 		if (err)
875 			return err;
876 	}
877 
878 	err = xe_irq_install(xe);
879 	if (err)
880 		return err;
881 
882 	for_each_gt(gt, xe, id) {
883 		err = xe_gt_init(gt);
884 		if (err)
885 			return err;
886 	}
887 
888 	err = xe_heci_gsc_init(xe);
889 	if (err)
890 		return err;
891 
892 	err = xe_oa_init(xe);
893 	if (err)
894 		return err;
895 
896 	err = xe_display_init(xe);
897 	if (err)
898 		return err;
899 
900 	err = xe_pxp_init(xe);
901 	if (err)
902 		return err;
903 
904 	err = drm_dev_register(&xe->drm, 0);
905 	if (err)
906 		return err;
907 
908 	xe_display_register(xe);
909 
910 	err = xe_oa_register(xe);
911 	if (err)
912 		goto err_unregister_display;
913 
914 	err = xe_pmu_register(&xe->pmu);
915 	if (err)
916 		goto err_unregister_display;
917 
918 	xe_debugfs_register(xe);
919 
920 	err = xe_hwmon_register(xe);
921 	if (err)
922 		goto err_unregister_display;
923 
924 	for_each_gt(gt, xe, id)
925 		xe_gt_sanitize_freq(gt);
926 
927 	xe_vsec_init(xe);
928 
929 	return devm_add_action_or_reset(xe->drm.dev, xe_device_sanitize, xe);
930 
931 err_unregister_display:
932 	xe_display_unregister(xe);
933 
934 	return err;
935 }
936 
937 void xe_device_remove(struct xe_device *xe)
938 {
939 	xe_display_unregister(xe);
940 
941 	drm_dev_unplug(&xe->drm);
942 }
943 
944 void xe_device_shutdown(struct xe_device *xe)
945 {
946 	struct xe_gt *gt;
947 	u8 id;
948 
949 	drm_dbg(&xe->drm, "Shutting down device\n");
950 
951 	if (xe_driver_flr_disabled(xe)) {
952 		xe_display_pm_shutdown(xe);
953 
954 		xe_irq_suspend(xe);
955 
956 		for_each_gt(gt, xe, id)
957 			xe_gt_shutdown(gt);
958 
959 		xe_display_pm_shutdown_late(xe);
960 	} else {
961 		/* BOOM! */
962 		__xe_driver_flr(xe);
963 	}
964 }
965 
966 /**
967  * xe_device_wmb() - Device specific write memory barrier
968  * @xe: the &xe_device
969  *
970  * While wmb() is sufficient for a barrier if we use system memory, on discrete
971  * platforms with device memory we additionally need to issue a register write.
972  * Since it doesn't matter which register we write to, use the read-only VF_CAP
973  * register that is also marked as accessible by the VFs.
974  */
975 void xe_device_wmb(struct xe_device *xe)
976 {
977 	wmb();
978 	if (IS_DGFX(xe))
979 		xe_mmio_write32(xe_root_tile_mmio(xe), VF_CAP_REG, 0);
980 }
981 
982 /**
983  * xe_device_td_flush() - Flush transient L3 cache entries
984  * @xe: The device
985  *
986  * Display engine has direct access to memory and is never coherent with L3/L4
987  * caches (or CPU caches), however KMD is responsible for specifically flushing
988  * transient L3 GPU cache entries prior to the flip sequence to ensure scanout
989  * can happen from such a surface without seeing corruption.
990  *
991  * Display surfaces can be tagged as transient by mapping it using one of the
992  * various L3:XD PAT index modes on Xe2.
993  *
994  * Note: On non-discrete xe2 platforms, like LNL, the entire L3 cache is flushed
995  * at the end of each submission via PIPE_CONTROL for compute/render, since SA
996  * Media is not coherent with L3 and we want to support render-vs-media
997  * usescases. For other engines like copy/blt the HW internally forces uncached
998  * behaviour, hence why we can skip the TDF on such platforms.
999  */
1000 void xe_device_td_flush(struct xe_device *xe)
1001 {
1002 	struct xe_gt *gt;
1003 	unsigned int fw_ref;
1004 	u8 id;
1005 
1006 	if (!IS_DGFX(xe) || GRAPHICS_VER(xe) < 20)
1007 		return;
1008 
1009 	if (XE_WA(xe_root_mmio_gt(xe), 16023588340)) {
1010 		xe_device_l2_flush(xe);
1011 		return;
1012 	}
1013 
1014 	for_each_gt(gt, xe, id) {
1015 		if (xe_gt_is_media_type(gt))
1016 			continue;
1017 
1018 		fw_ref = xe_force_wake_get(gt_to_fw(gt), XE_FW_GT);
1019 		if (!fw_ref)
1020 			return;
1021 
1022 		xe_mmio_write32(&gt->mmio, XE2_TDF_CTRL, TRANSIENT_FLUSH_REQUEST);
1023 		/*
1024 		 * FIXME: We can likely do better here with our choice of
1025 		 * timeout. Currently we just assume the worst case, i.e. 150us,
1026 		 * which is believed to be sufficient to cover the worst case
1027 		 * scenario on current platforms if all cache entries are
1028 		 * transient and need to be flushed..
1029 		 */
1030 		if (xe_mmio_wait32(&gt->mmio, XE2_TDF_CTRL, TRANSIENT_FLUSH_REQUEST, 0,
1031 				   150, NULL, false))
1032 			xe_gt_err_once(gt, "TD flush timeout\n");
1033 
1034 		xe_force_wake_put(gt_to_fw(gt), fw_ref);
1035 	}
1036 }
1037 
1038 void xe_device_l2_flush(struct xe_device *xe)
1039 {
1040 	struct xe_gt *gt;
1041 	unsigned int fw_ref;
1042 
1043 	gt = xe_root_mmio_gt(xe);
1044 
1045 	if (!XE_WA(gt, 16023588340))
1046 		return;
1047 
1048 	fw_ref = xe_force_wake_get(gt_to_fw(gt), XE_FW_GT);
1049 	if (!fw_ref)
1050 		return;
1051 
1052 	spin_lock(&gt->global_invl_lock);
1053 	xe_mmio_write32(&gt->mmio, XE2_GLOBAL_INVAL, 0x1);
1054 
1055 	if (xe_mmio_wait32(&gt->mmio, XE2_GLOBAL_INVAL, 0x1, 0x0, 500, NULL, true))
1056 		xe_gt_err_once(gt, "Global invalidation timeout\n");
1057 	spin_unlock(&gt->global_invl_lock);
1058 
1059 	xe_force_wake_put(gt_to_fw(gt), fw_ref);
1060 }
1061 
1062 u32 xe_device_ccs_bytes(struct xe_device *xe, u64 size)
1063 {
1064 	return xe_device_has_flat_ccs(xe) ?
1065 		DIV_ROUND_UP_ULL(size, NUM_BYTES_PER_CCS_BYTE(xe)) : 0;
1066 }
1067 
1068 /**
1069  * xe_device_assert_mem_access - Inspect the current runtime_pm state.
1070  * @xe: xe device instance
1071  *
1072  * To be used before any kind of memory access. It will splat a debug warning
1073  * if the device is currently sleeping. But it doesn't guarantee in any way
1074  * that the device is going to remain awake. Xe PM runtime get and put
1075  * functions might be added to the outer bound of the memory access, while
1076  * this check is intended for inner usage to splat some warning if the worst
1077  * case has just happened.
1078  */
1079 void xe_device_assert_mem_access(struct xe_device *xe)
1080 {
1081 	xe_assert(xe, !xe_pm_runtime_suspended(xe));
1082 }
1083 
1084 void xe_device_snapshot_print(struct xe_device *xe, struct drm_printer *p)
1085 {
1086 	struct xe_gt *gt;
1087 	u8 id;
1088 
1089 	drm_printf(p, "PCI ID: 0x%04x\n", xe->info.devid);
1090 	drm_printf(p, "PCI revision: 0x%02x\n", xe->info.revid);
1091 
1092 	for_each_gt(gt, xe, id) {
1093 		drm_printf(p, "GT id: %u\n", id);
1094 		drm_printf(p, "\tTile: %u\n", gt->tile->id);
1095 		drm_printf(p, "\tType: %s\n",
1096 			   gt->info.type == XE_GT_TYPE_MAIN ? "main" : "media");
1097 		drm_printf(p, "\tIP ver: %u.%u.%u\n",
1098 			   REG_FIELD_GET(GMD_ID_ARCH_MASK, gt->info.gmdid),
1099 			   REG_FIELD_GET(GMD_ID_RELEASE_MASK, gt->info.gmdid),
1100 			   REG_FIELD_GET(GMD_ID_REVID, gt->info.gmdid));
1101 		drm_printf(p, "\tCS reference clock: %u\n", gt->info.reference_clock);
1102 	}
1103 }
1104 
1105 u64 xe_device_canonicalize_addr(struct xe_device *xe, u64 address)
1106 {
1107 	return sign_extend64(address, xe->info.va_bits - 1);
1108 }
1109 
1110 u64 xe_device_uncanonicalize_addr(struct xe_device *xe, u64 address)
1111 {
1112 	return address & GENMASK_ULL(xe->info.va_bits - 1, 0);
1113 }
1114 
1115 static void xe_device_wedged_fini(struct drm_device *drm, void *arg)
1116 {
1117 	struct xe_device *xe = arg;
1118 
1119 	xe_pm_runtime_put(xe);
1120 }
1121 
1122 /**
1123  * xe_device_declare_wedged - Declare device wedged
1124  * @xe: xe device instance
1125  *
1126  * This is a final state that can only be cleared with a module
1127  * re-probe (unbind + bind).
1128  * In this state every IOCTL will be blocked so the GT cannot be used.
1129  * In general it will be called upon any critical error such as gt reset
1130  * failure or guc loading failure. Userspace will be notified of this state
1131  * through device wedged uevent.
1132  * If xe.wedged module parameter is set to 2, this function will be called
1133  * on every single execution timeout (a.k.a. GPU hang) right after devcoredump
1134  * snapshot capture. In this mode, GT reset won't be attempted so the state of
1135  * the issue is preserved for further debugging.
1136  */
1137 void xe_device_declare_wedged(struct xe_device *xe)
1138 {
1139 	struct xe_gt *gt;
1140 	u8 id;
1141 
1142 	if (xe->wedged.mode == 0) {
1143 		drm_dbg(&xe->drm, "Wedged mode is forcibly disabled\n");
1144 		return;
1145 	}
1146 
1147 	xe_pm_runtime_get_noresume(xe);
1148 
1149 	if (drmm_add_action_or_reset(&xe->drm, xe_device_wedged_fini, xe)) {
1150 		drm_err(&xe->drm, "Failed to register xe_device_wedged_fini clean-up. Although device is wedged.\n");
1151 		return;
1152 	}
1153 
1154 	if (!atomic_xchg(&xe->wedged.flag, 1)) {
1155 		xe->needs_flr_on_fini = true;
1156 		drm_err(&xe->drm,
1157 			"CRITICAL: Xe has declared device %s as wedged.\n"
1158 			"IOCTLs and executions are blocked. Only a rebind may clear the failure\n"
1159 			"Please file a _new_ bug report at https://gitlab.freedesktop.org/drm/xe/kernel/issues/new\n",
1160 			dev_name(xe->drm.dev));
1161 
1162 		/* Notify userspace of wedged device */
1163 		drm_dev_wedged_event(&xe->drm,
1164 				     DRM_WEDGE_RECOVERY_REBIND | DRM_WEDGE_RECOVERY_BUS_RESET);
1165 	}
1166 
1167 	for_each_gt(gt, xe, id)
1168 		xe_gt_declare_wedged(gt);
1169 }
1170