Skip to contentSkip to conversation

cdx: fix driver managed dma support

PATCH RESEND In review Nipun Gupta 3 participants 4 messages Jun 5, 2023 – Jun 8, 2023 AMD cdx bus driver

lore
Share & tools
lore

Nipun Gupta

author maintainer
#0 lore raw

PATCH RESEND 1/1 cdx: fix driver managed dma support

The devices on cdx could be bound to drivers with the device DMA managed by kernel drivers or user-space applications. As multiple devices can be placed in the same IOMMU group, the DMA on these devices must either be entirely under kernel control or userspace control. Fix the CDX bus driver to acknowlege the driver_managed_dma flag and call the appropriate iommu APIs.

Fixes 2959ab247061 ("cdx: add the cdx bus driver")

---

I have resend this patch to include fixes, closes and reported by tags. We request that this fix be incorporated into the 6.4 release, along with the inclusion of CDX bus driver support.
Original patch was sent at:
https://lore.kernel.org/lkml/[email protected]/

 drivers/cdx/cdx.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
drivers/cdx/cdx.c +18 -0
index dc74fae02a78..42bbe5ed370d 100644
@@ -63,6 +63,8 @@
 #include <linux/mm.h>
 #include <linux/xarray.h>
 #include <linux/cdx/cdx_bus.h>
+#include <linux/iommu.h>
+#include <linux/dma-map-ops.h>
 #include "cdx.h"
 
 /* Default DMA mask for devices on a CDX bus */
@@ -267,6 +269,7 @@ static void cdx_shutdown(struct device *dev)
 
 static int cdx_dma_configure(struct device *dev)
 {
+	struct cdx_driver *cdx_drv = to_cdx_driver(dev->driver);
 	struct cdx_device *cdx_dev = to_cdx_device(dev);
 	u32 input_id = cdx_dev->req_id;
 	int ret;
@@ -277,9 +280,23 @@ static int cdx_dma_configure(struct device *dev)
 		return ret;
 	}
 
+	if (!ret && !cdx_drv->driver_managed_dma) {
+		ret = iommu_device_use_default_domain(dev);
+		if (ret)
+			arch_teardown_dma_ops(dev);
+	}
+
 	return 0;
 }
 
+static void cdx_dma_cleanup(struct device *dev)
+{
+	struct cdx_driver *cdx_drv = to_cdx_driver(dev->driver);
+
+	if (!cdx_drv->driver_managed_dma)
+		iommu_device_unuse_default_domain(dev);
+}
+
 /* show configuration fields */
 #define cdx_config_attr(field, format_string)	\
 static ssize_t	\
@@ -503,6 +520,7 @@ struct bus_type cdx_bus_type = {
 	.remove		= cdx_remove,
 	.shutdown	= cdx_shutdown,
 	.dma_configure	= cdx_dma_configure,
+	.dma_cleanup	= cdx_dma_cleanup,
 	.bus_groups	= cdx_bus_groups,
 	.dev_groups	= cdx_dev_groups,
 };
Signature · 1 line
2.17.1
#1 lore raw
Quoted 11 lines · Nipun Gupta

The devices on cdx could be bound to drivers with the device DMA managed by kernel drivers or user-space applications. As multiple devices can be placed in the same IOMMU group, the DMA on these devices must either be entirely under kernel control or userspace control. Fix the CDX bus driver to acknowlege the driver_managed_dma flag and call the appropriate iommu APIs.

Fixes 2959ab247061 ("cdx: add the cdx bus driver")
Quoted 6 lines

---

I have resend this patch to include fixes, closes and reported by tags. We request that this fix be incorporated into the 6.4 release, along with the inclusion of CDX bus driver support.

What do you mean by "inclusion of CDX bus driver support"? What patches exactly?

confused,

greg k-h

author maintainer
#2 lore raw
Quoted 15 lines · Greg KH
Quoted 13 lines · Nipun Gupta

The devices on cdx could be bound to drivers with the device DMA managed by kernel drivers or user-space applications. As multiple devices can be placed in the same IOMMU group, the DMA on these devices must either be entirely under kernel control or userspace control. Fix the CDX bus driver to acknowlege the driver_managed_dma flag and call the appropriate iommu APIs.

Fixes 2959ab247061 ("cdx: add the cdx bus driver")

---

Quoted 7 lines
Quoted 4 lines

I have resend this patch to include fixes, closes and reported by tags. We request that this fix be incorporated into the 6.4 release, along with the inclusion of CDX bus driver support.

What do you mean by "inclusion of CDX bus driver support"? What patches exactly?

Sorry for the confusion. I mean that CDX bus support would be coming up in 6.4 release and this patch (cdx: fix driver managed dma support) is a fix for the driver managed dma support in the bus. So if this patch can be part of 6.4 release (i.e. added in upcoming rc release/s for 6.4)?

Thanks,
Nipun

Quoted 4 lines

confused,

greg k-h

maintainer Reviewed-by on v1 1/1
#3 lore raw
In reply to Nipun Gupta
Quoted 89 lines

-----Original Message-----
From: Nipun Gupta <[email protected]>
Sent: Monday, June 5, 2023 6:40 PM
To: [email protected]; [email protected]; [email protected]; [email protected]
Cc: git (AMD-Xilinx) <[email protected]>; Anand, Harpreet <[email protected]>; Jansen Van Vuuren, Pieter <pieter.jansen-van- [email protected]>; Agarwal, Nikhil <[email protected]>; Simek, Michal <[email protected]>; Gupta, Nipun <[email protected]>
Subject: [RESEND PATCH] cdx: fix driver managed dma support

The devices on cdx could be bound to drivers with the device DMA managed by kernel drivers or user-space applications.
As multiple devices can be placed in the same IOMMU group, the DMA on these devices must either be entirely under kernel control or userspace control. Fix the CDX bus driver to acknowlege the driver_managed_dma flag and call the appropriate iommu APIs.

Fixes 2959ab247061 ("cdx: add the cdx bus driver")

Closes:
https://lore.kernel.org/lkml/20230524134831.28dc97e2.alex.williamson@re
dhat.com/
---

I have resend this patch to include fixes, closes and reported by tags. We request that this fix be incorporated into the 6.4 release, along with the inclusion of CDX bus driver support.
Original patch was sent at:
https://lore.kernel.org/lkml/20230531114759.6173-1-
[email protected]/

 drivers/cdx/cdx.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
drivers/cdx/cdx.c index renamed from drivers/cdx/cdx.c +0 -0

dc74fae02a78..42bbe5ed370d 100644

drivers/cdx/cdx.c +2 -0
@@ -63,6 +63,8 @@
 #include <linux/mm.h>
 #include <linux/xarray.h>
 #include <linux/cdx/cdx_bus.h>
+#include <linux/iommu.h>
+#include <linux/dma-map-ops.h>
 #include "cdx.h"
 
 /* Default DMA mask for devices on a CDX bus */ @@ -267,6 +269,7 @@

static void cdx_shutdown(struct device *dev)

static int cdx_dma_configure(struct device *dev) {

drivers/cdx/cdx.c +15 -1
+	struct cdx_driver *cdx_drv = to_cdx_driver(dev->driver);
 	struct cdx_device *cdx_dev = to_cdx_device(dev);
 	u32 input_id = cdx_dev->req_id;
 	int ret;
@@ -277,9 +280,23 @@ static int cdx_dma_configure(struct device *dev)
 		return ret;
 	}
 
+	if (!ret && !cdx_drv->driver_managed_dma) {
+		ret = iommu_device_use_default_domain(dev);
+		if (ret)
+			arch_teardown_dma_ops(dev);
+	}
+
 	return 0;
 }
 
+static void cdx_dma_cleanup(struct device *dev) {
+	struct cdx_driver *cdx_drv = to_cdx_driver(dev->driver);
+
+	if (!cdx_drv->driver_managed_dma)
+		iommu_device_unuse_default_domain(dev);
+}
+
 /* show configuration fields */
 #define cdx_config_attr(field, format_string)	\
 static ssize_t	\
@@ -503,6 +520,7 @@ struct bus_type cdx_bus_type = {
 	.remove		= cdx_remove,
 	.shutdown	= cdx_shutdown,
 	.dma_configure	= cdx_dma_configure,
+	.dma_cleanup	= cdx_dma_cleanup,
 	.bus_groups	= cdx_bus_groups,
 	.dev_groups	= cdx_dev_groups,
 };
--

2.17.1