Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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")
Signed-off-by: Nipun Gupta <nipun.gupta@amd.com>
Reported-by: Alex Williamson <alex.williamson@redhat.com>
Closes: https://lore.kernel.org/lkml/20230524134831.28dc97e2.alex.williamson@redhat.com/
Reviewed-by: Nikhil Agarwal <nikhil.agarwal@amd.com>
Message-ID: <20230605131009.6869-1-nipun.gupta@amd.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Nipun Gupta and committed by
Greg Kroah-Hartman
b8c5ff76 9e66fb52

+18
+18
drivers/cdx/cdx.c
··· 62 62 #include <linux/mm.h> 63 63 #include <linux/xarray.h> 64 64 #include <linux/cdx/cdx_bus.h> 65 + #include <linux/iommu.h> 66 + #include <linux/dma-map-ops.h> 65 67 #include "cdx.h" 66 68 67 69 /* Default DMA mask for devices on a CDX bus */ ··· 259 257 260 258 static int cdx_dma_configure(struct device *dev) 261 259 { 260 + struct cdx_driver *cdx_drv = to_cdx_driver(dev->driver); 262 261 struct cdx_device *cdx_dev = to_cdx_device(dev); 263 262 u32 input_id = cdx_dev->req_id; 264 263 int ret; ··· 270 267 return ret; 271 268 } 272 269 270 + if (!ret && !cdx_drv->driver_managed_dma) { 271 + ret = iommu_device_use_default_domain(dev); 272 + if (ret) 273 + arch_teardown_dma_ops(dev); 274 + } 275 + 273 276 return 0; 277 + } 278 + 279 + static void cdx_dma_cleanup(struct device *dev) 280 + { 281 + struct cdx_driver *cdx_drv = to_cdx_driver(dev->driver); 282 + 283 + if (!cdx_drv->driver_managed_dma) 284 + iommu_device_unuse_default_domain(dev); 274 285 } 275 286 276 287 /* show configuration fields */ ··· 422 405 .remove = cdx_remove, 423 406 .shutdown = cdx_shutdown, 424 407 .dma_configure = cdx_dma_configure, 408 + .dma_cleanup = cdx_dma_cleanup, 425 409 .bus_groups = cdx_bus_groups, 426 410 .dev_groups = cdx_dev_groups, 427 411 };