1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2013 Red Hat
12 struct iommu_domain *domain;
14 #define to_msm_iommu(x) container_of(x, struct msm_iommu, base)
16 static int msm_fault_handler(struct iommu_domain *domain, struct device *dev,
17 unsigned long iova, int flags, void *arg)
19 struct msm_iommu *iommu = arg;
20 if (iommu->base.handler)
21 return iommu->base.handler(iommu->base.arg, iova, flags);
22 pr_warn_ratelimited("*** fault: iova=%16lx, flags=%d\n", iova, flags);
26 static void msm_iommu_detach(struct msm_mmu *mmu)
28 struct msm_iommu *iommu = to_msm_iommu(mmu);
30 iommu_detach_device(iommu->domain, mmu->dev);
33 static int msm_iommu_map(struct msm_mmu *mmu, uint64_t iova,
34 struct sg_table *sgt, size_t len, int prot)
36 struct msm_iommu *iommu = to_msm_iommu(mmu);
39 ret = iommu_map_sg(iommu->domain, iova, sgt->sgl, sgt->nents, prot);
42 return (ret == len) ? 0 : -EINVAL;
45 static int msm_iommu_unmap(struct msm_mmu *mmu, uint64_t iova, size_t len)
47 struct msm_iommu *iommu = to_msm_iommu(mmu);
49 iommu_unmap(iommu->domain, iova, len);
54 static void msm_iommu_destroy(struct msm_mmu *mmu)
56 struct msm_iommu *iommu = to_msm_iommu(mmu);
57 iommu_domain_free(iommu->domain);
61 static const struct msm_mmu_funcs funcs = {
62 .detach = msm_iommu_detach,
64 .unmap = msm_iommu_unmap,
65 .destroy = msm_iommu_destroy,
68 struct msm_mmu *msm_iommu_new(struct device *dev, struct iommu_domain *domain)
70 struct msm_iommu *iommu;
74 return ERR_PTR(-ENODEV);
76 iommu = kzalloc(sizeof(*iommu), GFP_KERNEL);
78 return ERR_PTR(-ENOMEM);
80 iommu->domain = domain;
81 msm_mmu_init(&iommu->base, dev, &funcs);
82 iommu_set_fault_handler(domain, msm_fault_handler, iommu);
84 ret = iommu_attach_device(iommu->domain, dev);