[PATCH 4/4] dmaengine: Add debugfs entries for PTDMA information

From: Mehta, Sanju
Date: Tue Sep 24 2019 - 03:33:42 EST


From: Sanjay R Mehta <sanju.mehta@xxxxxxx>

Expose data about the configuration and operation of the PTDMA
through debugfs entries: device name, capabilities, configuration,
statistics.

Signed-off-by: Sanjay R Mehta <sanju.mehta@xxxxxxx>
Reviewed-by: Shyam Sundar S K <Shyam-sundar.S-k@xxxxxxx>
Reviewed-by: Rajesh Kumar <Rajesh1.Kumar@xxxxxxx>
---
drivers/dma/ptdma/Makefile | 3 +-
drivers/dma/ptdma/ptdma-debugfs.c | 249 ++++++++++++++++++++++++++++++++++++++
drivers/dma/ptdma/ptdma-dev.c | 15 +++
drivers/dma/ptdma/ptdma.h | 14 +++
4 files changed, 280 insertions(+), 1 deletion(-)
create mode 100644 drivers/dma/ptdma/ptdma-debugfs.c

diff --git a/drivers/dma/ptdma/Makefile b/drivers/dma/ptdma/Makefile
index 5fc174a..0c8ee97 100644
--- a/drivers/dma/ptdma/Makefile
+++ b/drivers/dma/ptdma/Makefile
@@ -7,6 +7,7 @@ obj-$(CONFIG_AMD_PTDMA) += ptdma.o

ptdma-objs := ptdma-ops.o \
ptdma-dev.o \
- ptdma-dmaengine.o
+ ptdma-dmaengine.o \
+ ptdma-debugfs.o

ptdma-$(CONFIG_PCI) += ptdma-pci.o
diff --git a/drivers/dma/ptdma/ptdma-debugfs.c b/drivers/dma/ptdma/ptdma-debugfs.c
new file mode 100644
index 0000000..ec5d3d3
--- /dev/null
+++ b/drivers/dma/ptdma/ptdma-debugfs.c
@@ -0,0 +1,249 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * AMD Passthrough DMA device driver
+ *
+ * Copyright (C) 2019 Advanced Micro Devices, Inc.
+ *
+ * Author: Sanjay R Mehta <sanju.mehta@xxxxxxx>
+ *
+ * Based on CCP driver
+ * Original copyright message:
+ *
+ * Copyright (C) 2017 Advanced Micro Devices, Inc.
+ *
+ * Author: Gary R Hook <gary.hook@xxxxxxx>
+ */
+
+#include <linux/debugfs.h>
+
+#include "ptdma.h"
+
+/* DebugFS helpers */
+#define OBUFP (obuf + oboff)
+#define OBUFLEN 512
+#define OBUFSPC (OBUFLEN - oboff)
+#define OSCNPRINTF(fmt, ...) \
+ scnprintf(OBUFP, OBUFSPC, fmt, ## __VA_ARGS__)
+
+#define MAX_NAME_LEN 20
+#define BUFLEN 63
+#define RI_VERSION_NUM 0x0000003F
+
+#define RI_NUM_VQM 0x00078000
+#define RI_NVQM_SHIFT 15
+#define RI_NVQM(r) (((r) * RI_NUM_VQM) >> RI_NVQM_SHIFT)
+#define RI_LSB_ENTRIES 0x0FF80000
+#define RI_NLSB_SHIFT 19
+#define RI_NLSB(r) (((r) * RI_LSB_ENTRIES) >> RI_NLSB_SHIFT)
+
+static struct dentry *pt_debugfs_dir;
+static DEFINE_MUTEX(pt_debugfs_lock);
+
+static ssize_t ptdma_debugfs_info_read(struct file *filp, char __user *ubuf,
+ size_t count, loff_t *offp)
+{
+ struct pt_device *pt = filp->private_data;
+ unsigned int oboff = 0;
+ unsigned int regval;
+ ssize_t ret;
+ char *obuf;
+
+ if (!pt)
+ return 0;
+
+ obuf = kmalloc(OBUFLEN, GFP_KERNEL);
+ if (!obuf)
+ return -ENOMEM;
+
+ oboff += OSCNPRINTF("Device name: %s\n", pt->name);
+ oboff += OSCNPRINTF(" # Queues: %d\n", 1);
+ oboff += OSCNPRINTF(" # Cmds: %d\n", pt->cmd_count);
+
+ regval = ioread32(pt->io_regs + CMD_PT_VERSION);
+
+ oboff += OSCNPRINTF(" Version: %d\n", regval & RI_VERSION_NUM);
+ oboff += OSCNPRINTF(" Engines:");
+ oboff += OSCNPRINTF("\n");
+ oboff += OSCNPRINTF(" Queues: %d\n",
+ (regval & RI_NUM_VQM) >> RI_NVQM_SHIFT);
+ oboff += OSCNPRINTF("LSB Entries: %d\n",
+ (regval & RI_LSB_ENTRIES) >> RI_NLSB_SHIFT);
+
+ ret = simple_read_from_buffer(ubuf, count, offp, obuf, oboff);
+ kfree(obuf);
+
+ return ret;
+}
+
+/* Return a formatted buffer containing the current
+ * statistics of queue for PTDMA
+ */
+static ssize_t ptdma_debugfs_stats_read(struct file *filp, char __user *ubuf,
+ size_t count, loff_t *offp)
+{
+ struct pt_device *pt = filp->private_data;
+ unsigned long total_pt_ops = 0;
+ unsigned long total_ops = 0;
+ unsigned int oboff = 0;
+ ssize_t ret = 0;
+ char *obuf;
+ struct pt_cmd_queue *cmd_q = &pt->cmd_q;
+
+ total_ops += cmd_q->total_ops;
+ total_pt_ops += cmd_q->total_pt_ops;
+
+ obuf = kmalloc(OBUFLEN, GFP_KERNEL);
+ if (!obuf)
+ return -ENOMEM;
+
+ oboff += OSCNPRINTF("Total Interrupts Handled: %ld\n",
+ pt->total_interrupts);
+ oboff += OSCNPRINTF(" Total Operations: %ld\n",
+ total_ops);
+ oboff += OSCNPRINTF(" Pass-Thru: %ld\n",
+ total_pt_ops);
+
+ ret = simple_read_from_buffer(ubuf, count, offp, obuf, oboff);
+ kfree(obuf);
+
+ return ret;
+}
+
+/* Reset the counters in a queue
+ */
+static void ptdma_debugfs_reset_queue_stats(struct pt_cmd_queue *cmd_q)
+{
+ cmd_q->total_ops = 0L;
+ cmd_q->total_pt_ops = 0L;
+}
+
+/* A value was written to the stats variable, which
+ * should be used to reset the queue counters across
+ * that device.
+ */
+static ssize_t ptdma_debugfs_stats_write(struct file *filp,
+ const char __user *ubuf,
+ size_t count, loff_t *offp)
+{
+ struct pt_device *pt = filp->private_data;
+
+ ptdma_debugfs_reset_queue_stats(&pt->cmd_q);
+ pt->total_interrupts = 0L;
+
+ return count;
+}
+
+/* Return a formatted buffer containing the current information
+ * for that queue
+ */
+static ssize_t ptdma_debugfs_queue_read(struct file *filp, char __user *ubuf,
+ size_t count, loff_t *offp)
+{
+ struct pt_cmd_queue *cmd_q = filp->private_data;
+ unsigned int oboff = 0;
+ unsigned int regval;
+ ssize_t ret;
+ char *obuf;
+
+ if (!cmd_q)
+ return 0;
+
+ obuf = kmalloc(OBUFLEN, GFP_KERNEL);
+ if (!obuf)
+ return -ENOMEM;
+
+ oboff += OSCNPRINTF(" Total Queue Operations: %ld\n",
+ cmd_q->total_ops);
+ oboff += OSCNPRINTF(" Pass-Thru: %ld\n",
+ cmd_q->total_pt_ops);
+
+ regval = ioread32(cmd_q->reg_int_enable);
+ oboff += OSCNPRINTF(" Enabled Interrupts:");
+ if (regval & INT_EMPTY_QUEUE)
+ oboff += OSCNPRINTF(" EMPTY");
+ if (regval & INT_QUEUE_STOPPED)
+ oboff += OSCNPRINTF(" STOPPED");
+ if (regval & INT_ERROR)
+ oboff += OSCNPRINTF(" ERROR");
+ if (regval & INT_COMPLETION)
+ oboff += OSCNPRINTF(" COMPLETION");
+ oboff += OSCNPRINTF("\n");
+
+ ret = simple_read_from_buffer(ubuf, count, offp, obuf, oboff);
+ kfree(obuf);
+
+ return ret;
+}
+
+/* A value was written to the stats variable for a
+ * queue. Reset the queue counters to this value.
+ */
+static ssize_t ptdma_debugfs_queue_write(struct file *filp,
+ const char __user *ubuf,
+ size_t count, loff_t *offp)
+{
+ struct pt_cmd_queue *cmd_q = filp->private_data;
+
+ ptdma_debugfs_reset_queue_stats(cmd_q);
+
+ return count;
+}
+
+static const struct file_operations pt_debugfs_info_ops = {
+ .owner = THIS_MODULE,
+ .open = simple_open,
+ .read = ptdma_debugfs_info_read,
+ .write = NULL,
+};
+
+static const struct file_operations pt_debugfs_queue_ops = {
+ .owner = THIS_MODULE,
+ .open = simple_open,
+ .read = ptdma_debugfs_queue_read,
+ .write = ptdma_debugfs_queue_write,
+};
+
+static const struct file_operations pt_debugfs_stats_ops = {
+ .owner = THIS_MODULE,
+ .open = simple_open,
+ .read = ptdma_debugfs_stats_read,
+ .write = ptdma_debugfs_stats_write,
+};
+
+void ptdma_debugfs_setup(struct pt_device *pt)
+{
+ struct pt_cmd_queue *cmd_q;
+ char name[MAX_NAME_LEN + 1];
+ struct dentry *debugfs_q_instance;
+
+ if (!debugfs_initialized())
+ return;
+
+ mutex_lock(&pt_debugfs_lock);
+ if (!pt_debugfs_dir)
+ pt_debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
+ mutex_unlock(&pt_debugfs_lock);
+
+ pt->debugfs_instance = debugfs_create_dir(pt->name, pt_debugfs_dir);
+
+ debugfs_create_file("info", 0400, pt->debugfs_instance, pt,
+ &pt_debugfs_info_ops);
+
+ debugfs_create_file("stats", 0600, pt->debugfs_instance, pt,
+ &pt_debugfs_stats_ops);
+
+ cmd_q = &pt->cmd_q;
+
+ snprintf(name, MAX_NAME_LEN - 1, "q");
+
+ debugfs_q_instance =
+ debugfs_create_dir(name, pt->debugfs_instance);
+
+ debugfs_create_file("stats", 0600, debugfs_q_instance, cmd_q,
+ &pt_debugfs_queue_ops);
+}
+
+void ptdma_debugfs_destroy(void)
+{
+ debugfs_remove_recursive(pt_debugfs_dir);
+}
diff --git a/drivers/dma/ptdma/ptdma-dev.c b/drivers/dma/ptdma/ptdma-dev.c
index 1a7a982..29a8c40 100644
--- a/drivers/dma/ptdma/ptdma-dev.c
+++ b/drivers/dma/ptdma/ptdma-dev.c
@@ -18,6 +18,7 @@
#include <linux/kernel.h>
#include <linux/pci.h>
#include <linux/kthread.h>
+#include <linux/debugfs.h>
#include <linux/dma-mapping.h>
#include <linux/interrupt.h>

@@ -102,6 +103,8 @@ static int pt_core_execute_cmd(struct ptdma_desc *desc,
int i;
int ret = 0;

+ cmd_q->total_ops++;
+
if (PT_CMD_SOC(desc)) {
PT_CMD_IOC(desc) = 1;
PT_CMD_SOC(desc) = 0;
@@ -153,6 +156,8 @@ int pt_core_perform_passthru(struct pt_op *op)
union pt_function function;
struct pt_dma_info *saddr = &op->src.u.dma;

+ op->cmd_q->total_pt_ops++;
+
memset(&desc, 0, Q_DESC_SIZE);

PT_CMD_ENGINE(&desc) = PT_ENGINE_PASSTHRU;
@@ -222,6 +227,7 @@ static irqreturn_t pt_core_irq_handler(int irq, void *data)
struct pt_device *pt = (struct pt_device *)data;

pt_core_disable_queue_interrupts(pt);
+ pt->total_interrupts++;
tasklet_schedule(&pt->irq_tasklet);

return IRQ_HANDLED;
@@ -362,6 +368,9 @@ int pt_core_init(struct pt_device *pt)
if (ret)
goto e_kthread;

+ /* Set up debugfs entries */
+ ptdma_debugfs_setup(pt);
+
return 0;


@@ -393,6 +402,12 @@ void pt_core_destroy(struct pt_device *pt)
/* Remove this device from the list of available units first */
pt_del_device(pt);

+ /* We're in the process of tearing down the entire driver;
+ * when all the devices are gone clean up debugfs
+ */
+ if (pt_present())
+ ptdma_debugfs_destroy();
+
/* Disable and clear interrupts */
pt_core_disable_queue_interrupts(pt);

diff --git a/drivers/dma/ptdma/ptdma.h b/drivers/dma/ptdma/ptdma.h
index e8bb4e7..ba23d50 100644
--- a/drivers/dma/ptdma/ptdma.h
+++ b/drivers/dma/ptdma/ptdma.h
@@ -53,6 +53,7 @@
#define CMD_QUEUE_PRIO_OFFSET 0x00
#define CMD_REQID_CONFIG_OFFSET 0x04
#define CMD_TIMEOUT_OFFSET 0x08
+#define CMD_PT_VERSION 0x10

#define CMD_Q_CONTROL_BASE 0x0000
#define CMD_Q_TAIL_LO_BASE 0x0004
@@ -312,6 +313,10 @@ struct pt_cmd_queue {
/* Interrupt wait queue */
wait_queue_head_t int_queue;
unsigned int int_rcvd;
+
+ /* Per-queue Statistics */
+ unsigned long total_ops;
+ unsigned long total_pt_ops;
} ____cacheline_aligned;

struct pt_device {
@@ -357,6 +362,12 @@ struct pt_device {
/* Suspend support */
unsigned int suspending;
wait_queue_head_t suspend_queue;
+
+ /* Device Statistics */
+ unsigned long total_interrupts;
+
+ /* DebugFS info */
+ struct dentry *debugfs_instance;
};

struct pt_dma_info {
@@ -503,6 +514,9 @@ int pt_run_cmd(struct pt_cmd_queue *cmd_q, struct pt_cmd *cmd);
int pt_dmaengine_register(struct pt_device *pt);
void pt_dmaengine_unregister(struct pt_device *pt);

+void ptdma_debugfs_setup(struct pt_device *pt);
+void ptdma_debugfs_destroy(void);
+
int pt_core_init(struct pt_device *pt);
void pt_core_destroy(struct pt_device *pt);

--
2.7.4