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_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 = aperture_remove_conflicting_pci_devices(pdev, driver.name);
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",
354 WQ_MEM_RECLAIM);
355 xe->ordered_wq = alloc_ordered_workqueue("xe-ordered-wq", 0);
356 xe->unordered_wq = alloc_workqueue("xe-unordered-wq", 0, 0);
357 xe->destroy_wq = alloc_workqueue("xe-destroy-wq", 0, 0);
358 if (!xe->ordered_wq || !xe->unordered_wq ||
359 !xe->preempt_fence_wq || !xe->destroy_wq) {
360 /*
361 * Cleanup done in xe_device_destroy via
362 * drmm_add_action_or_reset register above
363 */
364 drm_err(&xe->drm, "Failed to allocate xe workqueues\n");
365 err = -ENOMEM;
366 goto err;
367 }
368
369 err = xe_display_create(xe);
370 if (WARN_ON(err))
371 goto err;
372
373 return xe;
374
375 err:
376 return ERR_PTR(err);
377 }
378 ALLOW_ERROR_INJECTION(xe_device_create, ERRNO); /* See xe_pci_probe() */
379
xe_driver_flr_disabled(struct xe_device * xe)380 static bool xe_driver_flr_disabled(struct xe_device *xe)
381 {
382 return xe_mmio_read32(xe_root_tile_mmio(xe), GU_CNTL_PROTECTED) & DRIVERINT_FLR_DIS;
383 }
384
385 /*
386 * The driver-initiated FLR is the highest level of reset that we can trigger
387 * from within the driver. It is different from the PCI FLR in that it doesn't
388 * fully reset the SGUnit and doesn't modify the PCI config space and therefore
389 * it doesn't require a re-enumeration of the PCI BARs. However, the
390 * driver-initiated FLR does still cause a reset of both GT and display and a
391 * memory wipe of local and stolen memory, so recovery would require a full HW
392 * re-init and saving/restoring (or re-populating) the wiped memory. Since we
393 * perform the FLR as the very last action before releasing access to the HW
394 * during the driver release flow, we don't attempt recovery at all, because
395 * if/when a new instance of i915 is bound to the device it will do a full
396 * re-init anyway.
397 */
__xe_driver_flr(struct xe_device * xe)398 static void __xe_driver_flr(struct xe_device *xe)
399 {
400 const unsigned int flr_timeout = 3 * MICRO; /* specs recommend a 3s wait */
401 struct xe_mmio *mmio = xe_root_tile_mmio(xe);
402 int ret;
403
404 drm_dbg(&xe->drm, "Triggering Driver-FLR\n");
405
406 /*
407 * Make sure any pending FLR requests have cleared by waiting for the
408 * FLR trigger bit to go to zero. Also clear GU_DEBUG's DRIVERFLR_STATUS
409 * to make sure it's not still set from a prior attempt (it's a write to
410 * clear bit).
411 * Note that we should never be in a situation where a previous attempt
412 * is still pending (unless the HW is totally dead), but better to be
413 * safe in case something unexpected happens
414 */
415 ret = xe_mmio_wait32(mmio, GU_CNTL, DRIVERFLR, 0, flr_timeout, NULL, false);
416 if (ret) {
417 drm_err(&xe->drm, "Driver-FLR-prepare wait for ready failed! %d\n", ret);
418 return;
419 }
420 xe_mmio_write32(mmio, GU_DEBUG, DRIVERFLR_STATUS);
421
422 /* Trigger the actual Driver-FLR */
423 xe_mmio_rmw32(mmio, GU_CNTL, 0, DRIVERFLR);
424
425 /* Wait for hardware teardown to complete */
426 ret = xe_mmio_wait32(mmio, GU_CNTL, DRIVERFLR, 0, flr_timeout, NULL, false);
427 if (ret) {
428 drm_err(&xe->drm, "Driver-FLR-teardown wait completion failed! %d\n", ret);
429 return;
430 }
431
432 /* Wait for hardware/firmware re-init to complete */
433 ret = xe_mmio_wait32(mmio, GU_DEBUG, DRIVERFLR_STATUS, DRIVERFLR_STATUS,
434 flr_timeout, NULL, false);
435 if (ret) {
436 drm_err(&xe->drm, "Driver-FLR-reinit wait completion failed! %d\n", ret);
437 return;
438 }
439
440 /* Clear sticky completion status */
441 xe_mmio_write32(mmio, GU_DEBUG, DRIVERFLR_STATUS);
442 }
443
xe_driver_flr(struct xe_device * xe)444 static void xe_driver_flr(struct xe_device *xe)
445 {
446 if (xe_driver_flr_disabled(xe)) {
447 drm_info_once(&xe->drm, "BIOS Disabled Driver-FLR\n");
448 return;
449 }
450
451 __xe_driver_flr(xe);
452 }
453
xe_driver_flr_fini(void * arg)454 static void xe_driver_flr_fini(void *arg)
455 {
456 struct xe_device *xe = arg;
457
458 if (xe->needs_flr_on_fini)
459 xe_driver_flr(xe);
460 }
461
xe_device_sanitize(void * arg)462 static void xe_device_sanitize(void *arg)
463 {
464 struct xe_device *xe = arg;
465 struct xe_gt *gt;
466 u8 id;
467
468 for_each_gt(gt, xe, id)
469 xe_gt_sanitize(gt);
470 }
471
xe_set_dma_info(struct xe_device * xe)472 static int xe_set_dma_info(struct xe_device *xe)
473 {
474 unsigned int mask_size = xe->info.dma_mask_size;
475 int err;
476
477 dma_set_max_seg_size(xe->drm.dev, xe_sg_segment_size(xe->drm.dev));
478
479 err = dma_set_mask(xe->drm.dev, DMA_BIT_MASK(mask_size));
480 if (err)
481 goto mask_err;
482
483 err = dma_set_coherent_mask(xe->drm.dev, DMA_BIT_MASK(mask_size));
484 if (err)
485 goto mask_err;
486
487 return 0;
488
489 mask_err:
490 drm_err(&xe->drm, "Can't set DMA mask/consistent mask (%d)\n", err);
491 return err;
492 }
493
verify_lmem_ready(struct xe_device * xe)494 static bool verify_lmem_ready(struct xe_device *xe)
495 {
496 u32 val = xe_mmio_read32(xe_root_tile_mmio(xe), GU_CNTL) & LMEM_INIT;
497
498 return !!val;
499 }
500
wait_for_lmem_ready(struct xe_device * xe)501 static int wait_for_lmem_ready(struct xe_device *xe)
502 {
503 unsigned long timeout, start;
504
505 if (!IS_DGFX(xe))
506 return 0;
507
508 if (IS_SRIOV_VF(xe))
509 return 0;
510
511 if (verify_lmem_ready(xe))
512 return 0;
513
514 drm_dbg(&xe->drm, "Waiting for lmem initialization\n");
515
516 start = jiffies;
517 timeout = start + msecs_to_jiffies(60 * 1000); /* 60 sec! */
518
519 do {
520 if (signal_pending(current))
521 return -EINTR;
522
523 /*
524 * The boot firmware initializes local memory and
525 * assesses its health. If memory training fails,
526 * the punit will have been instructed to keep the GT powered
527 * down.we won't be able to communicate with it
528 *
529 * If the status check is done before punit updates the register,
530 * it can lead to the system being unusable.
531 * use a timeout and defer the probe to prevent this.
532 */
533 if (time_after(jiffies, timeout)) {
534 drm_dbg(&xe->drm, "lmem not initialized by firmware\n");
535 return -EPROBE_DEFER;
536 }
537
538 msleep(20);
539
540 } while (!verify_lmem_ready(xe));
541
542 drm_dbg(&xe->drm, "lmem ready after %ums",
543 jiffies_to_msecs(jiffies - start));
544
545 return 0;
546 }
547 ALLOW_ERROR_INJECTION(wait_for_lmem_ready, ERRNO); /* See xe_pci_probe() */
548
update_device_info(struct xe_device * xe)549 static void update_device_info(struct xe_device *xe)
550 {
551 /* disable features that are not available/applicable to VFs */
552 if (IS_SRIOV_VF(xe)) {
553 xe->info.probe_display = 0;
554 xe->info.has_heci_gscfi = 0;
555 xe->info.skip_guc_pc = 1;
556 xe->info.skip_pcode = 1;
557 }
558 }
559
560 /**
561 * xe_device_probe_early: Device early probe
562 * @xe: xe device instance
563 *
564 * Initialize MMIO resources that don't require any
565 * knowledge about tile count. Also initialize pcode and
566 * check vram initialization on root tile.
567 *
568 * Return: 0 on success, error code on failure
569 */
xe_device_probe_early(struct xe_device * xe)570 int xe_device_probe_early(struct xe_device *xe)
571 {
572 int err;
573
574 err = xe_mmio_init(xe);
575 if (err)
576 return err;
577
578 xe_sriov_probe_early(xe);
579
580 update_device_info(xe);
581
582 err = xe_pcode_probe_early(xe);
583 if (err)
584 return err;
585
586 err = wait_for_lmem_ready(xe);
587 if (err)
588 return err;
589
590 xe->wedged.mode = xe_modparam.wedged_mode;
591
592 return 0;
593 }
594
probe_has_flat_ccs(struct xe_device * xe)595 static int probe_has_flat_ccs(struct xe_device *xe)
596 {
597 struct xe_gt *gt;
598 unsigned int fw_ref;
599 u32 reg;
600
601 /* Always enabled/disabled, no runtime check to do */
602 if (GRAPHICS_VER(xe) < 20 || !xe->info.has_flat_ccs)
603 return 0;
604
605 gt = xe_root_mmio_gt(xe);
606
607 fw_ref = xe_force_wake_get(gt_to_fw(gt), XE_FW_GT);
608 if (!fw_ref)
609 return -ETIMEDOUT;
610
611 reg = xe_gt_mcr_unicast_read_any(gt, XE2_FLAT_CCS_BASE_RANGE_LOWER);
612 xe->info.has_flat_ccs = (reg & XE2_FLAT_CCS_ENABLE);
613
614 if (!xe->info.has_flat_ccs)
615 drm_dbg(&xe->drm,
616 "Flat CCS has been disabled in bios, May lead to performance impact");
617
618 xe_force_wake_put(gt_to_fw(gt), fw_ref);
619 return 0;
620 }
621
xe_device_probe(struct xe_device * xe)622 int xe_device_probe(struct xe_device *xe)
623 {
624 struct xe_tile *tile;
625 struct xe_gt *gt;
626 int err;
627 u8 last_gt;
628 u8 id;
629
630 xe_pat_init_early(xe);
631
632 err = xe_sriov_init(xe);
633 if (err)
634 return err;
635
636 xe->info.mem_region_mask = 1;
637 err = xe_display_init_nommio(xe);
638 if (err)
639 return err;
640
641 err = xe_set_dma_info(xe);
642 if (err)
643 return err;
644
645 err = xe_mmio_probe_tiles(xe);
646 if (err)
647 return err;
648
649 xe_ttm_sys_mgr_init(xe);
650
651 for_each_gt(gt, xe, id) {
652 err = xe_gt_init_early(gt);
653 if (err)
654 return err;
655
656 /*
657 * Only after this point can GT-specific MMIO operations
658 * (including things like communication with the GuC)
659 * be performed.
660 */
661 xe_gt_mmio_init(gt);
662 }
663
664 for_each_tile(tile, xe, id) {
665 if (IS_SRIOV_VF(xe)) {
666 xe_guc_comm_init_early(&tile->primary_gt->uc.guc);
667 err = xe_gt_sriov_vf_bootstrap(tile->primary_gt);
668 if (err)
669 return err;
670 err = xe_gt_sriov_vf_query_config(tile->primary_gt);
671 if (err)
672 return err;
673 }
674 err = xe_ggtt_init_early(tile->mem.ggtt);
675 if (err)
676 return err;
677 err = xe_memirq_init(&tile->memirq);
678 if (err)
679 return err;
680 }
681
682 for_each_gt(gt, xe, id) {
683 err = xe_gt_init_hwconfig(gt);
684 if (err)
685 return err;
686 }
687
688 err = xe_devcoredump_init(xe);
689 if (err)
690 return err;
691 err = devm_add_action_or_reset(xe->drm.dev, xe_driver_flr_fini, xe);
692 if (err)
693 return err;
694
695 err = xe_display_init_noirq(xe);
696 if (err)
697 return err;
698
699 err = xe_irq_install(xe);
700 if (err)
701 goto err;
702
703 err = probe_has_flat_ccs(xe);
704 if (err)
705 goto err;
706
707 err = xe_vram_probe(xe);
708 if (err)
709 goto err;
710
711 for_each_tile(tile, xe, id) {
712 err = xe_tile_init_noalloc(tile);
713 if (err)
714 goto err;
715 }
716
717 /* Allocate and map stolen after potential VRAM resize */
718 xe_ttm_stolen_mgr_init(xe);
719
720 /*
721 * Now that GT is initialized (TTM in particular),
722 * we can try to init display, and inherit the initial fb.
723 * This is the reason the first allocation needs to be done
724 * inside display.
725 */
726 err = xe_display_init_noaccel(xe);
727 if (err)
728 goto err;
729
730 for_each_gt(gt, xe, id) {
731 last_gt = id;
732
733 err = xe_gt_init(gt);
734 if (err)
735 goto err_fini_gt;
736 }
737
738 xe_heci_gsc_init(xe);
739
740 err = xe_oa_init(xe);
741 if (err)
742 goto err_fini_gt;
743
744 err = xe_display_init(xe);
745 if (err)
746 goto err_fini_oa;
747
748 err = drm_dev_register(&xe->drm, 0);
749 if (err)
750 goto err_fini_display;
751
752 xe_display_register(xe);
753
754 xe_oa_register(xe);
755
756 xe_debugfs_register(xe);
757
758 xe_hwmon_register(xe);
759
760 for_each_gt(gt, xe, id)
761 xe_gt_sanitize_freq(gt);
762
763 return devm_add_action_or_reset(xe->drm.dev, xe_device_sanitize, xe);
764
765 err_fini_display:
766 xe_display_driver_remove(xe);
767
768 err_fini_oa:
769 xe_oa_fini(xe);
770
771 err_fini_gt:
772 for_each_gt(gt, xe, id) {
773 if (id < last_gt)
774 xe_gt_remove(gt);
775 else
776 break;
777 }
778
779 err:
780 xe_display_fini(xe);
781 return err;
782 }
783
xe_device_remove_display(struct xe_device * xe)784 static void xe_device_remove_display(struct xe_device *xe)
785 {
786 xe_display_unregister(xe);
787
788 drm_dev_unplug(&xe->drm);
789 xe_display_driver_remove(xe);
790 }
791
xe_device_remove(struct xe_device * xe)792 void xe_device_remove(struct xe_device *xe)
793 {
794 struct xe_gt *gt;
795 u8 id;
796
797 xe_oa_unregister(xe);
798
799 xe_device_remove_display(xe);
800
801 xe_display_fini(xe);
802
803 xe_oa_fini(xe);
804
805 xe_heci_gsc_fini(xe);
806
807 for_each_gt(gt, xe, id)
808 xe_gt_remove(gt);
809 }
810
xe_device_shutdown(struct xe_device * xe)811 void xe_device_shutdown(struct xe_device *xe)
812 {
813 struct xe_gt *gt;
814 u8 id;
815
816 drm_dbg(&xe->drm, "Shutting down device\n");
817
818 if (xe_driver_flr_disabled(xe)) {
819 xe_display_pm_shutdown(xe);
820
821 xe_irq_suspend(xe);
822
823 for_each_gt(gt, xe, id)
824 xe_gt_shutdown(gt);
825
826 xe_display_pm_shutdown_late(xe);
827 } else {
828 /* BOOM! */
829 __xe_driver_flr(xe);
830 }
831 }
832
833 /**
834 * xe_device_wmb() - Device specific write memory barrier
835 * @xe: the &xe_device
836 *
837 * While wmb() is sufficient for a barrier if we use system memory, on discrete
838 * platforms with device memory we additionally need to issue a register write.
839 * Since it doesn't matter which register we write to, use the read-only VF_CAP
840 * register that is also marked as accessible by the VFs.
841 */
xe_device_wmb(struct xe_device * xe)842 void xe_device_wmb(struct xe_device *xe)
843 {
844 wmb();
845 if (IS_DGFX(xe))
846 xe_mmio_write32(xe_root_tile_mmio(xe), VF_CAP_REG, 0);
847 }
848
849 /**
850 * xe_device_td_flush() - Flush transient L3 cache entries
851 * @xe: The device
852 *
853 * Display engine has direct access to memory and is never coherent with L3/L4
854 * caches (or CPU caches), however KMD is responsible for specifically flushing
855 * transient L3 GPU cache entries prior to the flip sequence to ensure scanout
856 * can happen from such a surface without seeing corruption.
857 *
858 * Display surfaces can be tagged as transient by mapping it using one of the
859 * various L3:XD PAT index modes on Xe2.
860 *
861 * Note: On non-discrete xe2 platforms, like LNL, the entire L3 cache is flushed
862 * at the end of each submission via PIPE_CONTROL for compute/render, since SA
863 * Media is not coherent with L3 and we want to support render-vs-media
864 * usescases. For other engines like copy/blt the HW internally forces uncached
865 * behaviour, hence why we can skip the TDF on such platforms.
866 */
xe_device_td_flush(struct xe_device * xe)867 void xe_device_td_flush(struct xe_device *xe)
868 {
869 struct xe_gt *gt;
870 unsigned int fw_ref;
871 u8 id;
872
873 if (!IS_DGFX(xe) || GRAPHICS_VER(xe) < 20)
874 return;
875
876 if (XE_WA(xe_root_mmio_gt(xe), 16023588340)) {
877 xe_device_l2_flush(xe);
878 return;
879 }
880
881 for_each_gt(gt, xe, id) {
882 if (xe_gt_is_media_type(gt))
883 continue;
884
885 fw_ref = xe_force_wake_get(gt_to_fw(gt), XE_FW_GT);
886 if (!fw_ref)
887 return;
888
889 xe_mmio_write32(>->mmio, XE2_TDF_CTRL, TRANSIENT_FLUSH_REQUEST);
890 /*
891 * FIXME: We can likely do better here with our choice of
892 * timeout. Currently we just assume the worst case, i.e. 150us,
893 * which is believed to be sufficient to cover the worst case
894 * scenario on current platforms if all cache entries are
895 * transient and need to be flushed..
896 */
897 if (xe_mmio_wait32(>->mmio, XE2_TDF_CTRL, TRANSIENT_FLUSH_REQUEST, 0,
898 150, NULL, false))
899 xe_gt_err_once(gt, "TD flush timeout\n");
900
901 xe_force_wake_put(gt_to_fw(gt), fw_ref);
902 }
903 }
904
xe_device_l2_flush(struct xe_device * xe)905 void xe_device_l2_flush(struct xe_device *xe)
906 {
907 struct xe_gt *gt;
908 unsigned int fw_ref;
909
910 gt = xe_root_mmio_gt(xe);
911
912 if (!XE_WA(gt, 16023588340))
913 return;
914
915 fw_ref = xe_force_wake_get(gt_to_fw(gt), XE_FW_GT);
916 if (!fw_ref)
917 return;
918
919 spin_lock(>->global_invl_lock);
920 xe_mmio_write32(>->mmio, XE2_GLOBAL_INVAL, 0x1);
921
922 if (xe_mmio_wait32(>->mmio, XE2_GLOBAL_INVAL, 0x1, 0x0, 500, NULL, true))
923 xe_gt_err_once(gt, "Global invalidation timeout\n");
924 spin_unlock(>->global_invl_lock);
925
926 xe_force_wake_put(gt_to_fw(gt), fw_ref);
927 }
928
xe_device_ccs_bytes(struct xe_device * xe,u64 size)929 u32 xe_device_ccs_bytes(struct xe_device *xe, u64 size)
930 {
931 return xe_device_has_flat_ccs(xe) ?
932 DIV_ROUND_UP_ULL(size, NUM_BYTES_PER_CCS_BYTE(xe)) : 0;
933 }
934
935 /**
936 * xe_device_assert_mem_access - Inspect the current runtime_pm state.
937 * @xe: xe device instance
938 *
939 * To be used before any kind of memory access. It will splat a debug warning
940 * if the device is currently sleeping. But it doesn't guarantee in any way
941 * that the device is going to remain awake. Xe PM runtime get and put
942 * functions might be added to the outer bound of the memory access, while
943 * this check is intended for inner usage to splat some warning if the worst
944 * case has just happened.
945 */
xe_device_assert_mem_access(struct xe_device * xe)946 void xe_device_assert_mem_access(struct xe_device *xe)
947 {
948 xe_assert(xe, !xe_pm_runtime_suspended(xe));
949 }
950
xe_device_snapshot_print(struct xe_device * xe,struct drm_printer * p)951 void xe_device_snapshot_print(struct xe_device *xe, struct drm_printer *p)
952 {
953 struct xe_gt *gt;
954 u8 id;
955
956 drm_printf(p, "PCI ID: 0x%04x\n", xe->info.devid);
957 drm_printf(p, "PCI revision: 0x%02x\n", xe->info.revid);
958
959 for_each_gt(gt, xe, id) {
960 drm_printf(p, "GT id: %u\n", id);
961 drm_printf(p, "\tTile: %u\n", gt->tile->id);
962 drm_printf(p, "\tType: %s\n",
963 gt->info.type == XE_GT_TYPE_MAIN ? "main" : "media");
964 drm_printf(p, "\tIP ver: %u.%u.%u\n",
965 REG_FIELD_GET(GMD_ID_ARCH_MASK, gt->info.gmdid),
966 REG_FIELD_GET(GMD_ID_RELEASE_MASK, gt->info.gmdid),
967 REG_FIELD_GET(GMD_ID_REVID, gt->info.gmdid));
968 drm_printf(p, "\tCS reference clock: %u\n", gt->info.reference_clock);
969 }
970 }
971
xe_device_canonicalize_addr(struct xe_device * xe,u64 address)972 u64 xe_device_canonicalize_addr(struct xe_device *xe, u64 address)
973 {
974 return sign_extend64(address, xe->info.va_bits - 1);
975 }
976
xe_device_uncanonicalize_addr(struct xe_device * xe,u64 address)977 u64 xe_device_uncanonicalize_addr(struct xe_device *xe, u64 address)
978 {
979 return address & GENMASK_ULL(xe->info.va_bits - 1, 0);
980 }
981
xe_device_wedged_fini(struct drm_device * drm,void * arg)982 static void xe_device_wedged_fini(struct drm_device *drm, void *arg)
983 {
984 struct xe_device *xe = arg;
985
986 xe_pm_runtime_put(xe);
987 }
988
989 /**
990 * xe_device_declare_wedged - Declare device wedged
991 * @xe: xe device instance
992 *
993 * This is a final state that can only be cleared with a mudule
994 * re-probe (unbind + bind).
995 * In this state every IOCTL will be blocked so the GT cannot be used.
996 * In general it will be called upon any critical error such as gt reset
997 * failure or guc loading failure.
998 * If xe.wedged module parameter is set to 2, this function will be called
999 * on every single execution timeout (a.k.a. GPU hang) right after devcoredump
1000 * snapshot capture. In this mode, GT reset won't be attempted so the state of
1001 * the issue is preserved for further debugging.
1002 */
xe_device_declare_wedged(struct xe_device * xe)1003 void xe_device_declare_wedged(struct xe_device *xe)
1004 {
1005 struct xe_gt *gt;
1006 u8 id;
1007
1008 if (xe->wedged.mode == 0) {
1009 drm_dbg(&xe->drm, "Wedged mode is forcibly disabled\n");
1010 return;
1011 }
1012
1013 xe_pm_runtime_get_noresume(xe);
1014
1015 if (drmm_add_action_or_reset(&xe->drm, xe_device_wedged_fini, xe)) {
1016 drm_err(&xe->drm, "Failed to register xe_device_wedged_fini clean-up. Although device is wedged.\n");
1017 return;
1018 }
1019
1020 if (!atomic_xchg(&xe->wedged.flag, 1)) {
1021 xe->needs_flr_on_fini = true;
1022 drm_err(&xe->drm,
1023 "CRITICAL: Xe has declared device %s as wedged.\n"
1024 "IOCTLs and executions are blocked. Only a rebind may clear the failure\n"
1025 "Please file a _new_ bug report at https://gitlab.freedesktop.org/drm/xe/kernel/issues/new\n",
1026 dev_name(xe->drm.dev));
1027 }
1028
1029 for_each_gt(gt, xe, id)
1030 xe_gt_declare_wedged(gt);
1031 }
1032