From: Pranjal Ramajor Asha Kanojiya <[email protected]>

Expose sysfs files for each DBC representing the current state of that DBC.
For example, sysfs for DBC ID 0 and accel minor number 0 looks like this,

/sys/class/accel/accel0/dbc0_state

Following are the states and their corresponding values,
DBC_STATE_IDLE (0)
DBC_STATE_ASSIGNED (1)
DBC_STATE_BEFORE_SHUTDOWN (2)
DBC_STATE_AFTER_SHUTDOWN (3)
DBC_STATE_BEFORE_POWER_UP (4)
DBC_STATE_AFTER_POWER_UP (5)

Signed-off-by: Pranjal Ramajor Asha Kanojiya <[email protected]>
Signed-off-by: Youssef Samir <[email protected]>
---
 Documentation/ABI/stable/sysfs-driver-qaic |  16 +++
 drivers/accel/qaic/Makefile                |   1 +
 drivers/accel/qaic/qaic.h                  |  25 +++++
 drivers/accel/qaic/qaic_control.c          |   2 +
 drivers/accel/qaic/qaic_drv.c              |   8 ++
 drivers/accel/qaic/qaic_sysfs.c            | 109 +++++++++++++++++++++
 6 files changed, 161 insertions(+)
 create mode 100644 Documentation/ABI/stable/sysfs-driver-qaic
 create mode 100644 drivers/accel/qaic/qaic_sysfs.c

diff --git a/Documentation/ABI/stable/sysfs-driver-qaic 
b/Documentation/ABI/stable/sysfs-driver-qaic
new file mode 100644
index 000000000000..d967e8a20c1c
--- /dev/null
+++ b/Documentation/ABI/stable/sysfs-driver-qaic
@@ -0,0 +1,16 @@
+What:          
/sys/bus/pci/drivers/qaic/XXXX:XX:XX.X/accel/accel<minor_nr>/dbc<N>_state
+Date:          October 2025
+KernelVersion: 6.19
+Contact:       Jeff Hugo <[email protected]>
+Description:   Represents the current state of DMA Bridge channel (DBC). Below 
are the possible
+               states,
+               IDLE (0) -              DBC is free and can be activated
+               ASSIGNED (1) -          DBC is activated and a workload is 
running on device
+               BEFORE_SHUTDOWN (2) -   Sub-system associated with this 
workload has crashed and
+                                       it will shutdown soon
+               AFTER_SHUTDOWN (3) -    Sub-system associated with this 
workload has crashed and
+                                       it has shutdown
+               BEFORE_POWER_UP (4) -   Sub-system associated with this 
workload is shutdown and
+                                       it will be powered up soon
+               AFTER_POWER_UP (5) -    Sub-system associated with this 
workload is now powered up
+Users:         Any userspace application or clients interested in DBC state.
diff --git a/drivers/accel/qaic/Makefile b/drivers/accel/qaic/Makefile
index 1106b876f737..8f6746e5f03a 100644
--- a/drivers/accel/qaic/Makefile
+++ b/drivers/accel/qaic/Makefile
@@ -11,6 +11,7 @@ qaic-y := \
        qaic_data.o \
        qaic_drv.o \
        qaic_ras.o \
+       qaic_sysfs.o \
        qaic_timesync.o \
        sahara.o
 
diff --git a/drivers/accel/qaic/qaic.h b/drivers/accel/qaic/qaic.h
index 820d133236dd..4c2f25249e95 100644
--- a/drivers/accel/qaic/qaic.h
+++ b/drivers/accel/qaic/qaic.h
@@ -47,6 +47,22 @@ enum __packed dev_states {
        QAIC_ONLINE,
 };
 
+enum dbc_states {
+       /* DBC is free and can be activated */
+       DBC_STATE_IDLE,
+       /* DBC is activated and a workload is running on device */
+       DBC_STATE_ASSIGNED,
+       /* Sub-system associated with this workload has crashed and it will 
shutdown soon */
+       DBC_STATE_BEFORE_SHUTDOWN,
+       /* Sub-system associated with this workload has crashed and it has 
shutdown */
+       DBC_STATE_AFTER_SHUTDOWN,
+       /* Sub-system associated with this workload is shutdown and it will be 
powered up soon */
+       DBC_STATE_BEFORE_POWER_UP,
+       /* Sub-system associated with this workload is now powered up */
+       DBC_STATE_AFTER_POWER_UP,
+       DBC_STATE_MAX,
+};
+
 extern bool datapath_polling;
 
 struct qaic_user {
@@ -114,6 +130,8 @@ struct dma_bridge_chan {
        unsigned int            irq;
        /* Polling work item to simulate interrupts */
        struct work_struct      poll_work;
+       /* Represents various states of this DBC from enum dbc_states */
+       unsigned int            state;
 };
 
 struct qaic_device {
@@ -195,6 +213,8 @@ struct qaic_drm_device {
        struct list_head        users;
        /* Synchronizes access to users list */
        struct mutex            users_mutex;
+       /* Pointer to array of DBC sysfs attributes */
+       void                    *sysfs_attrs;
 };
 
 struct qaic_bo {
@@ -319,4 +339,9 @@ int qaic_perf_stats_bo_ioctl(struct drm_device *dev, void 
*data, struct drm_file
 int qaic_detach_slice_bo_ioctl(struct drm_device *dev, void *data, struct 
drm_file *file_priv);
 void irq_polling_work(struct work_struct *work);
 
+/* qaic_sysfs.c */
+int qaic_sysfs_init(struct qaic_drm_device *qddev);
+void qaic_sysfs_remove(struct qaic_drm_device *qddev);
+void set_dbc_state(struct qaic_device *qdev, u32 dbc_id, unsigned int state);
+
 #endif /* _QAIC_H_ */
diff --git a/drivers/accel/qaic/qaic_control.c 
b/drivers/accel/qaic/qaic_control.c
index b86a8e48e731..b1ab1282f9d3 100644
--- a/drivers/accel/qaic/qaic_control.c
+++ b/drivers/accel/qaic/qaic_control.c
@@ -309,6 +309,7 @@ static void save_dbc_buf(struct qaic_device *qdev, struct 
ioctl_resources *resou
                enable_dbc(qdev, dbc_id, usr);
                qdev->dbc[dbc_id].in_use = true;
                resources->buf = NULL;
+               set_dbc_state(qdev, dbc_id, DBC_STATE_ASSIGNED);
        }
 }
 
@@ -921,6 +922,7 @@ static int decode_deactivate(struct qaic_device *qdev, void 
*trans, u32 *msg_len
        }
 
        release_dbc(qdev, dbc_id);
+       set_dbc_state(qdev, dbc_id, DBC_STATE_IDLE);
        *msg_len += sizeof(*in_trans);
 
        return 0;
diff --git a/drivers/accel/qaic/qaic_drv.c b/drivers/accel/qaic/qaic_drv.c
index e162f4b8a262..a8a16f20320f 100644
--- a/drivers/accel/qaic/qaic_drv.c
+++ b/drivers/accel/qaic/qaic_drv.c
@@ -270,6 +270,13 @@ static int qaic_create_drm_device(struct qaic_device 
*qdev, s32 partition_id)
                return ret;
        }
 
+       ret = qaic_sysfs_init(qddev);
+       if (ret) {
+               drm_dev_unregister(drm);
+               pci_dbg(qdev->pdev, "qaic_sysfs_init failed %d\n", ret);
+               return ret;
+       }
+
        qaic_debugfs_init(qddev);
 
        return ret;
@@ -281,6 +288,7 @@ static void qaic_destroy_drm_device(struct qaic_device 
*qdev, s32 partition_id)
        struct drm_device *drm = to_drm(qddev);
        struct qaic_user *usr;
 
+       qaic_sysfs_remove(qddev);
        drm_dev_unregister(drm);
        qddev->partition_id = 0;
        /*
diff --git a/drivers/accel/qaic/qaic_sysfs.c b/drivers/accel/qaic/qaic_sysfs.c
new file mode 100644
index 000000000000..5d7aa1a5f4d1
--- /dev/null
+++ b/drivers/accel/qaic/qaic_sysfs.c
@@ -0,0 +1,109 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+/* Copyright (c) 2020-2025, The Linux Foundation. All rights reserved. */
+
+#include <drm/drm_file.h>
+#include <drm/drm_managed.h>
+#include <linux/device.h>
+#include <linux/kernel.h>
+#include <linux/kobject.h>
+#include <linux/mutex.h>
+#include <linux/sysfs.h>
+
+#include "qaic.h"
+
+#define NAME_LEN               14
+
+struct dbc_attribute {
+       struct device_attribute dev_attr;
+       u32 dbc_id;
+       char name[NAME_LEN];
+};
+
+static ssize_t dbc_state_show(struct device *dev, struct device_attribute *a, 
char *buf)
+{
+       struct dbc_attribute *dbc_attr = container_of(a, struct dbc_attribute, 
dev_attr);
+       struct drm_minor *minor = dev_get_drvdata(dev);
+       struct qaic_device *qdev;
+
+       qdev = to_qaic_device(minor->dev);
+       return sysfs_emit(buf, "%d\n", qdev->dbc[dbc_attr->dbc_id].state);
+}
+
+void set_dbc_state(struct qaic_device *qdev, u32 dbc_id, unsigned int state)
+{
+       struct device *kdev = to_accel_kdev(qdev->qddev);
+       char *envp[3] = {};
+       char state_str[16];
+       char id_str[12];
+
+       envp[0] = id_str;
+       envp[1] = state_str;
+
+       if (state >= DBC_STATE_MAX)
+               return;
+       if (dbc_id >= qdev->num_dbc)
+               return;
+       if (state == qdev->dbc[dbc_id].state)
+               return;
+
+       snprintf(id_str, ARRAY_SIZE(id_str), "DBC_ID=%d", dbc_id);
+       snprintf(state_str, ARRAY_SIZE(state_str), "DBC_STATE=%d", state);
+
+       qdev->dbc[dbc_id].state = state;
+       kobject_uevent_env(&kdev->kobj, KOBJ_CHANGE, envp);
+}
+
+int qaic_sysfs_init(struct qaic_drm_device *qddev)
+{
+       struct device *kdev = to_accel_kdev(qddev);
+       struct drm_device *drm = to_drm(qddev);
+       u32 num_dbc = qddev->qdev->num_dbc;
+       struct dbc_attribute *dbc_attrs;
+       int i, ret;
+
+       dbc_attrs = drmm_kcalloc(drm, num_dbc, sizeof(*dbc_attrs), GFP_KERNEL);
+       if (!dbc_attrs)
+               return -ENOMEM;
+
+       for (i = 0; i < num_dbc; ++i) {
+               struct dbc_attribute *dbc_attr = &dbc_attrs[i];
+
+               sysfs_attr_init(&dbc_attr->dev_attr.attr);
+               dbc_attr->dbc_id = i;
+               snprintf(dbc_attr->name, NAME_LEN, "dbc%d_state", i);
+               dbc_attr->dev_attr.attr.name = dbc_attr->name;
+               dbc_attr->dev_attr.attr.mode = 0444;
+               dbc_attr->dev_attr.show = dbc_state_show;
+               ret = sysfs_create_file(&kdev->kobj, &dbc_attr->dev_attr.attr);
+               if (ret) {
+                       int j;
+
+                       for (j = 0; j < i; ++j) {
+                               dbc_attr = &dbc_attrs[j];
+                               sysfs_remove_file(&kdev->kobj, 
&dbc_attr->dev_attr.attr);
+                       }
+                       drmm_kfree(drm, dbc_attrs);
+                       return ret;
+               }
+       }
+
+       qddev->sysfs_attrs = dbc_attrs;
+       return 0;
+}
+
+void qaic_sysfs_remove(struct qaic_drm_device *qddev)
+{
+       struct dbc_attribute *dbc_attrs = qddev->sysfs_attrs;
+       struct device *kdev = to_accel_kdev(qddev);
+       u32 num_dbc = qddev->qdev->num_dbc;
+       int i;
+
+       if (!dbc_attrs)
+               return;
+
+       qddev->sysfs_attrs = NULL;
+       for (i = 0; i < num_dbc; ++i)
+               sysfs_remove_file(&kdev->kobj, &dbc_attrs[i].dev_attr.attr);
+       drmm_kfree(to_drm(qddev), dbc_attrs);
+}
-- 
2.43.0

Reply via email to