@@ -10,6 +10,7 @@
#include <linux/bitmap.h>
#include <linux/delay.h>
+#include <linux/dma-iommu.h>
#include <linux/dma-mapping.h>
#include <linux/err.h>
#include <linux/export.h>
@@ -895,6 +896,92 @@ static const struct iommu_ops ipmmu_ops
.pgsize_bitmap = SZ_1G | SZ_2M | SZ_4K,
};
+static struct iommu_domain *ipmmu_domain_alloc_dma(unsigned type)
+{
+ struct iommu_domain *io_domain;
+
+ if (type != IOMMU_DOMAIN_DMA)
+ return NULL;
+
+ io_domain = __ipmmu_domain_alloc(type);
+ if (io_domain)
+ iommu_get_dma_cookie(io_domain);
+
+ return io_domain;
+}
+
+static void ipmmu_domain_free_dma(struct iommu_domain *io_domain)
+{
+ iommu_put_dma_cookie(io_domain);
+ ipmmu_domain_free(io_domain);
+}
+
+static int ipmmu_add_device_dma(struct device *dev)
+{
+ struct iommu_group *group;
+
+ /* only accept devices with iommus property */
+ if (of_count_phandle_with_args(dev->of_node, "iommus",
+ "#iommu-cells") < 0)
+ return -ENODEV;
+
+ group = iommu_group_get_for_dev(dev);
+ if (IS_ERR(group))
+ return PTR_ERR(group);
+
+ return 0;
+}
+
+static void ipmmu_remove_device_dma(struct device *dev)
+{
+ iommu_group_remove_device(dev);
+}
+
+static struct iommu_group *ipmmu_device_group_dma(struct device *dev)
+{
+ struct iommu_group *group;
+ int ret;
+
+ group = generic_device_group(dev);
+ if (IS_ERR(group))
+ return group;
+
+ ret = ipmmu_init_platform_device(dev, group);
+ if (ret) {
+ iommu_group_put(group);
+ group = ERR_PTR(ret);
+ }
+
+ return group;
+}
+
+#ifdef CONFIG_OF_IOMMU
+static int ipmmu_of_xlate_dma(struct device *dev,
+ struct of_phandle_args *spec)
+{
+ /* dummy callback to satisfy of_iommu_configure() */
+ return 0;
+}
+#endif
+
+static const struct iommu_ops ipmmu_ops_dma = {
+ .domain_alloc = ipmmu_domain_alloc_dma,
+ .domain_free = ipmmu_domain_free_dma,
+ .attach_dev = ipmmu_attach_device,
+ .detach_dev = ipmmu_detach_device,
+ .map = ipmmu_map,
+ .unmap = ipmmu_unmap,
+ .map_sg = default_iommu_map_sg,
+ .iova_to_phys = ipmmu_iova_to_phys,
+ .add_device = ipmmu_add_device_dma,
+ .remove_device = ipmmu_remove_device_dma,
+ .device_group = ipmmu_device_group_dma,
+ .pgsize_bitmap = SZ_4K,
+#ifdef CONFIG_OF_IOMMU
+ .of_xlate = ipmmu_of_xlate_dma,
+#endif
+};
+
/* -----------------------------------------------------------------------------
* Probe/remove and init
*/
@@ -1068,7 +1155,7 @@ static int __init ipmmu_vmsa_iommu_of_se
{
const struct of_device_id *match;
const struct ipmmu_features *feature;
- static const struct iommu_ops *ops = &ipmmu_ops;
+ static const struct iommu_ops *ops = &ipmmu_ops_dma;
ipmmu_init();