From: Dominik Grzegorzek <[email protected]>

Introduce an immutable eudebug property for exec_queues,
using a flags value to enable eudebug-specific features. For now, the
engine LRC uses this flag to enable the runalone hardware feature.
Runalone ensures that only one hardware engine in a group
[rcs0, ccs0-3] is active on a tile.

v2: - check CONFIG_DRM_XE_EUDEBUG and LR mode (Matthew)
    - disable preempt (Dominik)
    - lrc_create remove from engine init
v3: - remove redundant runalone setup (Sashiko)
v4: - deny if pxp is on (Sashiko)
    - deny if secondary mq (Sashiko)
    - deny if parallel (Sashiko)
    - really deny if pxp (Claude)
v5: - fix the multiqueue (Sashiko)

Cc: Matthew Brost <[email protected]>
Signed-off-by: Dominik Grzegorzek <[email protected]>
Signed-off-by: Mika Kuoppala <[email protected]>
---
 .../ABI/testing/sysfs-driver-intel-xe-eudebug | 10 +++
 drivers/gpu/drm/xe/xe_exec_queue.c            | 81 ++++++++++++++++++-
 drivers/gpu/drm/xe/xe_exec_queue.h            |  2 +
 drivers/gpu/drm/xe/xe_exec_queue_types.h      |  7 ++
 drivers/gpu/drm/xe/xe_hw_engine.h             |  9 +++
 include/uapi/drm/xe_drm.h                     | 12 +++
 6 files changed, 120 insertions(+), 1 deletion(-)

diff --git a/Documentation/ABI/testing/sysfs-driver-intel-xe-eudebug 
b/Documentation/ABI/testing/sysfs-driver-intel-xe-eudebug
index 884a791789ad..50ab7db1f811 100644
--- a/Documentation/ABI/testing/sysfs-driver-intel-xe-eudebug
+++ b/Documentation/ABI/testing/sysfs-driver-intel-xe-eudebug
@@ -24,6 +24,16 @@ Description: RW. Controls whether the EU debugger interface 
is armed for
                have to close them or be terminated. A connection shows up as
                an "anon_inode:[xe_eudebug]" entry under /proc/<pid>/fd.
 
+               Once enabled, any client with access to the render node can
+               also create debuggable exec queues
+               (DRM_XE_EXEC_QUEUE_SET_PROPERTY_EUDEBUG), whether or not a
+               debugger is attached. While such a queue executes, the other
+               render and compute engines of its tile are kept idle
+               (runalone), and as its preemption timeout is infinite it is
+               not reset if it does not yield the engine when preempted.
+               Work from other clients on those engines can therefore be
+               held off for as long as such a queue keeps running.
+
                Each connection allocates about 4 MiB of kernel memory for
                its event queue, held until the connection is closed. The
                number of connections is not limited beyond the file
diff --git a/drivers/gpu/drm/xe/xe_exec_queue.c 
b/drivers/gpu/drm/xe/xe_exec_queue.c
index 7ade915c59a8..e65bda492e58 100644
--- a/drivers/gpu/drm/xe/xe_exec_queue.c
+++ b/drivers/gpu/drm/xe/xe_exec_queue.c
@@ -31,6 +31,7 @@
 #include "xe_trace.h"
 #include "xe_vm.h"
 #include "xe_pxp.h"
+#include "xe_eudebug.h"
 
 /**
  * DOC: Execution Queue
@@ -416,6 +417,9 @@ static int __xe_exec_queue_init(struct xe_exec_queue *q, 
u32 exec_queue_flags)
        if (q->flags & EXEC_QUEUE_FLAG_DISABLE_STATE_CACHE_PERF_FIX)
                flags |= XE_LRC_DISABLE_STATE_CACHE_PERF_FIX;
 
+       if (q->eudebug_flags & EXEC_QUEUE_EUDEBUG_FLAG_ENABLE)
+               flags |= XE_LRC_CREATE_RUNALONE;
+
        err = q->ops->init(q);
        if (err)
                return err;
@@ -1050,6 +1054,57 @@ static int exec_queue_set_state_cache_perf_fix(struct 
xe_device *xe, struct xe_e
        return 0;
 }
 
+static int exec_queue_set_eudebug(struct xe_device *xe, struct xe_exec_queue 
*q,
+                                 u64 value)
+{
+       const u64 known_flags = DRM_XE_EXEC_QUEUE_EUDEBUG_FLAG_ENABLE;
+
+       if (XE_IOCTL_DBG(xe, !xe_engine_supports_eudebug(q->class)))
+               return -EINVAL;
+
+       if (XE_IOCTL_DBG(xe, (value & ~known_flags)))
+               return -EINVAL;
+
+       if (XE_IOCTL_DBG(xe, !IS_ENABLED(CONFIG_DRM_XE_EUDEBUG)))
+               return -EOPNOTSUPP;
+
+       /*
+        * The preemption timeout below is infinite, which is only safe where
+        * no dma-fence waits for the queue to be preempted: a fault mode vm
+        * has no preempt fences.
+        */
+       if (XE_IOCTL_DBG(xe, !xe_exec_queue_is_lr(q) ||
+                        !xe_vm_in_fault_mode(q->vm)))
+               return -EINVAL;
+
+       if (XE_IOCTL_DBG(xe, xe_exec_queue_is_parallel(q)))
+               return -EINVAL;
+
+       if (XE_IOCTL_DBG(xe,
+                        !(value & DRM_XE_EXEC_QUEUE_EUDEBUG_FLAG_ENABLE)))
+               return -EINVAL;
+
+       if (XE_IOCTL_DBG(xe, !xe_eudebug_is_enabled(xe)))
+               return -EPERM;
+
+       q->eudebug_flags = EXEC_QUEUE_EUDEBUG_FLAG_ENABLE;
+       q->sched_props.preempt_timeout_us = 0;
+
+       return 0;
+}
+
+/**
+ * xe_exec_queue_is_debuggable() - Whether an exec_queue is debuggable
+ * @q: The exec_queue
+ *
+ * Return: True if @q was created with %DRM_XE_EXEC_QUEUE_SET_PROPERTY_EUDEBUG,
+ * false otherwise.
+ */
+bool xe_exec_queue_is_debuggable(struct xe_exec_queue *q)
+{
+       return q->eudebug_flags & EXEC_QUEUE_EUDEBUG_FLAG_ENABLE;
+}
+
 typedef int (*xe_exec_queue_set_property_fn)(struct xe_device *xe,
                                             struct xe_exec_queue *q,
                                             u64 value);
@@ -1064,6 +1119,7 @@ static const xe_exec_queue_set_property_fn 
exec_queue_set_property_funcs[] = {
                                                        
exec_queue_set_multi_queue_priority,
        [DRM_XE_EXEC_QUEUE_SET_DISABLE_STATE_CACHE_PERF_FIX] =
                                                        
exec_queue_set_state_cache_perf_fix,
+       [DRM_XE_EXEC_QUEUE_SET_PROPERTY_EUDEBUG] = exec_queue_set_eudebug,
 };
 
 /**
@@ -1136,6 +1192,28 @@ static int exec_queue_user_ext_check(struct 
xe_exec_queue *q, u64 properties)
                         (properties & 
BIT_ULL(DRM_XE_EXEC_QUEUE_SET_PROPERTY_PXP_TYPE))))
                return -EINVAL;
 
+       /*
+        * A debugger can inspect and modify the EU state and memory of a
+        * debuggable queue, so it must not be combined with protected content.
+        * Test the property mask rather than the queue state, as the extensions
+        * are applied in the order userspace chained them and either one may be
+        * processed first.
+        */
+       if (XE_IOCTL_DBG(xe, (properties & 
BIT_ULL(DRM_XE_EXEC_QUEUE_SET_PROPERTY_EUDEBUG)) &&
+                        (properties & 
BIT_ULL(DRM_XE_EXEC_QUEUE_SET_PROPERTY_PXP_TYPE))))
+               return -EINVAL;
+
+       /*
+        * EUDEBUG turns on runalone, which lets a single engine of the group
+        * execute at a time, while a multi-queue group is there to run several
+        * in parallel. Reject the combination. Testing the property mask
+        * covers the primary as well, and does not depend on the order in
+        * which userspace chained the extensions.
+        */
+       if (XE_IOCTL_DBG(xe, (properties & 
BIT_ULL(DRM_XE_EXEC_QUEUE_SET_PROPERTY_EUDEBUG)) &&
+                        (properties & 
BIT_ULL(DRM_XE_EXEC_QUEUE_SET_PROPERTY_MULTI_GROUP))))
+               return -EINVAL;
+
        return 0;
 }
 
@@ -1171,7 +1249,8 @@ static int exec_queue_user_ext_set_property(struct 
xe_device *xe,
                         ext.property != 
DRM_XE_EXEC_QUEUE_SET_HANG_REPLAY_STATE &&
                         ext.property != 
DRM_XE_EXEC_QUEUE_SET_PROPERTY_MULTI_GROUP &&
                         ext.property != 
DRM_XE_EXEC_QUEUE_SET_PROPERTY_MULTI_QUEUE_PRIORITY &&
-                        ext.property != 
DRM_XE_EXEC_QUEUE_SET_DISABLE_STATE_CACHE_PERF_FIX))
+                        ext.property != 
DRM_XE_EXEC_QUEUE_SET_DISABLE_STATE_CACHE_PERF_FIX &&
+                        ext.property != 
DRM_XE_EXEC_QUEUE_SET_PROPERTY_EUDEBUG))
                return -EINVAL;
 
        idx = array_index_nospec(ext.property, 
ARRAY_SIZE(exec_queue_set_property_funcs));
diff --git a/drivers/gpu/drm/xe/xe_exec_queue.h 
b/drivers/gpu/drm/xe/xe_exec_queue.h
index e8963f85cabd..95a0d84b9590 100644
--- a/drivers/gpu/drm/xe/xe_exec_queue.h
+++ b/drivers/gpu/drm/xe/xe_exec_queue.h
@@ -162,4 +162,6 @@ int xe_exec_queue_contexts_hwsp_rebase(struct xe_exec_queue 
*q, void *scratch);
 struct xe_lrc *xe_exec_queue_lrc(struct xe_exec_queue *q);
 struct xe_lrc *xe_exec_queue_get_lrc(struct xe_exec_queue *q, u16 idx);
 
+bool xe_exec_queue_is_debuggable(struct xe_exec_queue *q);
+
 #endif
diff --git a/drivers/gpu/drm/xe/xe_exec_queue_types.h 
b/drivers/gpu/drm/xe/xe_exec_queue_types.h
index 48c05398b016..32893341287d 100644
--- a/drivers/gpu/drm/xe/xe_exec_queue_types.h
+++ b/drivers/gpu/drm/xe/xe_exec_queue_types.h
@@ -155,6 +155,13 @@ struct xe_exec_queue {
        /** @ban_reason: Bitmask of ban reasons 
(DRM_XE_EXEC_QUEUE_BAN_REASON_*) */
        atomic_t ban_reason;
 
+       /**
+        * @eudebug_flags: immutable eudebug flags for this exec queue.
+        * Set up with DRM_XE_EXEC_QUEUE_SET_PROPERTY_EUDEBUG.
+        */
+#define EXEC_QUEUE_EUDEBUG_FLAG_ENABLE         BIT(0)
+       unsigned long eudebug_flags;
+
        union {
                /** @execlist: execlist backend specific state for exec queue */
                struct xe_execlist_exec_queue *execlist;
diff --git a/drivers/gpu/drm/xe/xe_hw_engine.h 
b/drivers/gpu/drm/xe/xe_hw_engine.h
index c3ee37f8cfc0..532d69677981 100644
--- a/drivers/gpu/drm/xe/xe_hw_engine.h
+++ b/drivers/gpu/drm/xe/xe_hw_engine.h
@@ -78,4 +78,13 @@ enum xe_force_wake_domains xe_hw_engine_to_fw_domain(struct 
xe_hw_engine *hwe);
 
 u32 xe_hw_engine_mmio_read32(struct xe_hw_engine *hwe, struct xe_reg reg);
 
+static inline bool xe_engine_supports_eudebug(const enum xe_engine_class ec)
+{
+       if (ec == XE_ENGINE_CLASS_COMPUTE ||
+           ec == XE_ENGINE_CLASS_RENDER)
+               return true;
+
+       return false;
+}
+
 #endif
diff --git a/include/uapi/drm/xe_drm.h b/include/uapi/drm/xe_drm.h
index e84b11324f08..1a157bf1589c 100644
--- a/include/uapi/drm/xe_drm.h
+++ b/include/uapi/drm/xe_drm.h
@@ -1396,6 +1396,16 @@ struct drm_xe_vm_get_property {
  *  - %DRM_XE_EXEC_QUEUE_SET_DISABLE_STATE_CACHE_PERF_FIX - Set the queue to
  *    enable render color cache keying on BTP+BTI instead of just BTI
  *    (only valid for render queues).
+ *  - %DRM_XE_EXEC_QUEUE_SET_PROPERTY_EUDEBUG - Make the queue debuggable
+ *    by an EU debugger. The queue runs in runalone mode, one engine at
+ *    a time, with a preemption timeout of 0. Fails with -EINVAL unless
+ *    the value is exactly %DRM_XE_EXEC_QUEUE_EUDEBUG_FLAG_ENABLE and the
+ *    queue is a width 1 render or compute queue on a VM created with
+ *    %DRM_XE_VM_CREATE_FLAG_FAULT_MODE,
+ *    not combined with %DRM_XE_EXEC_QUEUE_SET_PROPERTY_PXP_TYPE or
+ *    %DRM_XE_EXEC_QUEUE_SET_PROPERTY_MULTI_GROUP. Fails with
+ *    -EOPNOTSUPP if the kernel is built without CONFIG_DRM_XE_EUDEBUG,
+ *    and with -EPERM if eudebug is not enabled through sysfs.
  *
  * The example below shows how to use &struct drm_xe_exec_queue_create to 
create
  * a simple exec_queue (no parallel submission) of class
@@ -1443,6 +1453,8 @@ struct drm_xe_exec_queue_create {
 #define     DRM_XE_MULTI_GROUP_CREATE                          (1ull << 63)
 #define   DRM_XE_EXEC_QUEUE_SET_PROPERTY_MULTI_QUEUE_PRIORITY  5
 #define   DRM_XE_EXEC_QUEUE_SET_DISABLE_STATE_CACHE_PERF_FIX   6
+#define   DRM_XE_EXEC_QUEUE_SET_PROPERTY_EUDEBUG               7
+#define     DRM_XE_EXEC_QUEUE_EUDEBUG_FLAG_ENABLE              (1 << 0)
        /** @extensions: Pointer to the first extension struct, if any */
        __u64 extensions;
 
-- 
2.53.0

Reply via email to