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