diff mbox series

[2/4] dma: Support for multiple PTDMA

Message ID 1569310303-29192-1-git-send-email-Sanju.Mehta@amd.com (mailing list archive)
State Changes Requested
Headers show
Series *** AMD PTDMA driver *** | expand

Commit Message

Mehta, Sanju Sept. 24, 2019, 7:32 a.m. UTC
From: Sanjay R Mehta <sanju.mehta@amd.com>

Enable management of multiple PTDMA engine in a system.
Each device will get a unique identifier, as well as
uniquely named resources. Treat each PTDMA as an orthogonal
unit and register resources individually.

Signed-off-by: Sanjay R Mehta <sanju.mehta@amd.com>
Reviewed-by: Shyam Sundar S K <Shyam-sundar.S-k@amd.com>
Reviewed-by: Rajesh Kumar <Rajesh1.Kumar@amd.com>
---
 drivers/dma/ptdma/ptdma-dev.c |   7 +--
 drivers/dma/ptdma/ptdma-ops.c | 111 ++++++++++++++++++++++++++++++++++++++----
 drivers/dma/ptdma/ptdma.h     |   5 ++
 3 files changed, 110 insertions(+), 13 deletions(-)
diff mbox series

Patch

diff --git a/drivers/dma/ptdma/ptdma-dev.c b/drivers/dma/ptdma/ptdma-dev.c
index ce3e85d..e69999b 100644
--- a/drivers/dma/ptdma/ptdma-dev.c
+++ b/drivers/dma/ptdma/ptdma-dev.c
@@ -245,7 +245,7 @@  int pt_core_init(struct pt_device *pt)
 	iowrite32(CMD_CONFIG_REQID, pt->io_regs + CMD_REQID_CONFIG_OFFSET);
 
 	/* Allocate a dma pool for the queue */
-	snprintf(dma_pool_name, sizeof(dma_pool_name), "pt_q");
+	snprintf(dma_pool_name, sizeof(dma_pool_name), "%s_q", pt->name);
 	dma_pool = dma_pool_create(dma_pool_name, dev,
 				   PT_DMAPOOL_MAX_SIZE,
 				   PT_DMAPOOL_ALIGN, 0);
@@ -311,7 +311,7 @@  int pt_core_init(struct pt_device *pt)
 
 	dev_dbg(dev, "Requesting an IRQ...\n");
 	/* Request an irq */
-	ret = request_irq(pt->pt_irq, pt_core_irq_handler, 0, "pt", pt);
+	ret = request_irq(pt->pt_irq, pt_core_irq_handler, 0, pt->name, pt);
 	if (ret) {
 		dev_err(dev, "unable to allocate an IRQ\n");
 		goto e_pool;
@@ -338,7 +338,8 @@  int pt_core_init(struct pt_device *pt)
 	dev_dbg(dev, "Starting threads...\n");
 	/* Create a kthread for command queue */
 
-	kthread = kthread_create(pt_cmd_queue_thread, cmd_q, "pt-q");
+	kthread = kthread_create(pt_cmd_queue_thread, cmd_q,
+				 "%s-q", pt->name);
 	if (IS_ERR(kthread)) {
 		dev_err(dev, "error creating queue thread (%ld)\n",
 			PTR_ERR(kthread));
diff --git a/drivers/dma/ptdma/ptdma-ops.c b/drivers/dma/ptdma/ptdma-ops.c
index ca94802..0c3023a 100644
--- a/drivers/dma/ptdma/ptdma-ops.c
+++ b/drivers/dma/ptdma/ptdma-ops.c
@@ -20,13 +20,16 @@ 
 #include <linux/sched.h>
 #include <linux/interrupt.h>
 #include <linux/spinlock.h>
+#include <linux/spinlock_types.h>
+#include <linux/types.h>
 #include <linux/mutex.h>
 #include <linux/delay.h>
 #include <linux/cpu.h>
 
 #include "ptdma.h"
 
-static struct pt_device *pt_dev;
+/* Ever-increasing value to produce unique unit numbers */
+static atomic_t pt_ordinal;
 
 struct pt_tasklet_data {
 	struct completion completion;
@@ -63,24 +66,105 @@  static char *pt_error_codes[] = {
 	"ERR 43: LSB_PARITY_ERR",
 };
 
-static inline struct pt_device *pt_get_device(void)
+void pt_log_error(struct pt_device *d, int e)
 {
-	return pt_dev;
+	dev_err(d->dev, "PTDMA error: %s (0x%x)\n", pt_error_codes[e], e);
 }
 
+/* List of PTDMAs, PTDMA count, read-write access lock, and access functions
+ *
+ * Lock structure: get pt_unit_lock for reading whenever we need to
+ * examine the PTDMA list. While holding it for reading we can acquire
+ * the RR lock to update the round-robin next-PTDMA pointer. The unit lock
+ * must be acquired before the RR lock.
+ *
+ * If the unit-lock is acquired for writing, we have total control over
+ * the list, so there's no value in getting the RR lock.
+ */
+static DEFINE_RWLOCK(pt_unit_lock);
+static LIST_HEAD(pt_units);
+
+/* Round-robin counter */
+static DEFINE_SPINLOCK(pt_rr_lock);
+static struct pt_device *pt_rr;
+
+/*
+ * pt_add_device - add a PTDMA device to the list
+ *
+ * @pt: pt_device struct pointer
+ *
+ * Put this PTDMA on the unit list, which makes it available
+ * for use.
+ *
+ * Returns zero if a PTDMA device is present, -ENODEV otherwise.
+ */
 void pt_add_device(struct pt_device *pt)
 {
-	pt_dev = pt;
+	unsigned long flags;
+
+	write_lock_irqsave(&pt_unit_lock, flags);
+	list_add_tail(&pt->entry, &pt_units);
+	if (!pt_rr)
+		/* We already have the list lock (we're first) so this
+		 * pointer can't change on us. Set its initial value.
+		 */
+		pt_rr = pt;
+	write_unlock_irqrestore(&pt_unit_lock, flags);
 }
 
+/*
+ * pt_del_device - remove a PTDMA device from the list
+ *
+ * @pt: pt_device struct pointer
+ *
+ * Remove this unit from the list of devices. If the next device
+ * up for use is this one, adjust the pointer. If this is the last
+ * device, NULL the pointer.
+ */
 void pt_del_device(struct pt_device *pt)
 {
-	pt_dev = NULL;
+	unsigned long flags;
+
+	write_lock_irqsave(&pt_unit_lock, flags);
+	if (pt_rr == pt) {
+		/* pt_unit_lock is read/write; any read access
+		 * will be suspended while we make changes to the
+		 * list and RR pointer.
+		 */
+		if (list_is_last(&pt_rr->entry, &pt_units))
+			pt_rr = list_first_entry(&pt_units, struct pt_device,
+						  entry);
+		else
+			pt_rr = list_next_entry(pt_rr, entry);
+	}
+	list_del(&pt->entry);
+	if (list_empty(&pt_units))
+		pt_rr = NULL;
+	write_unlock_irqrestore(&pt_unit_lock, flags);
 }
 
-void pt_log_error(struct pt_device *d, int e)
+static struct pt_device *pt_get_device(void)
 {
-	dev_err(d->dev, "PTDMA error: %s (0x%x)\n", pt_error_codes[e], e);
+	unsigned long flags;
+	struct pt_device *dp = NULL;
+
+	/* We round-robin through the unit list.
+	 * The (pt_rr) pointer refers to the next unit to use.
+	 */
+	read_lock_irqsave(&pt_unit_lock, flags);
+	if (!list_empty(&pt_units)) {
+		spin_lock(&pt_rr_lock);
+		dp = pt_rr;
+		if (list_is_last(&pt_rr->entry, &pt_units))
+			pt_rr = list_first_entry(&pt_units, struct pt_device,
+						  entry);
+		else
+			pt_rr = list_next_entry(pt_rr, entry);
+		spin_unlock(&pt_rr_lock);
+	}
+	read_unlock_irqrestore(&pt_unit_lock, flags);
+
+	return dp;
 }
 
 /*
@@ -90,10 +174,14 @@  void pt_log_error(struct pt_device *d, int e)
  */
 int pt_present(void)
 {
-	if (pt_get_device())
-		return 0;
+	unsigned long flags;
+	int ret;
+
+	read_lock_irqsave(&pt_unit_lock, flags);
+	ret = list_empty(&pt_units);
+	read_unlock_irqrestore(&pt_unit_lock, flags);
 
-	return -ENODEV;
+	return ret ? -ENODEV : 0;
 }
 
 /*
@@ -286,6 +374,7 @@  struct pt_device *pt_alloc_struct(struct device *dev)
 	if (!pt)
 		return NULL;
 	pt->dev = dev;
+	pt->ord = atomic_inc_return(&pt_ordinal);
 
 	INIT_LIST_HEAD(&pt->cmd);
 	INIT_LIST_HEAD(&pt->backlog);
@@ -298,6 +387,8 @@  struct pt_device *pt_alloc_struct(struct device *dev)
 	init_waitqueue_head(&pt->lsb_queue);
 	init_waitqueue_head(&pt->suspend_queue);
 
+	snprintf(pt->name, MAX_PT_NAME_LEN, "pt-%u", pt->ord);
+
 	return pt;
 }
 
diff --git a/drivers/dma/ptdma/ptdma.h b/drivers/dma/ptdma/ptdma.h
index 75b8e25..4e89517 100644
--- a/drivers/dma/ptdma/ptdma.h
+++ b/drivers/dma/ptdma/ptdma.h
@@ -26,6 +26,7 @@ 
 #include <linux/wait.h>
 #include <linux/dmapool.h>
 
+#define MAX_PT_NAME_LEN			16
 #define MAX_DMAPOOL_NAME_LEN		32
 
 #define MAX_HW_QUEUES			1
@@ -280,7 +281,11 @@  struct pt_cmd_queue {
 } ____cacheline_aligned;
 
 struct pt_device {
+	struct list_head entry;
+
 	unsigned int version;
+	unsigned int ord;
+	char name[MAX_PT_NAME_LEN];
 
 	struct device *dev;