2 * Copyright (C) 2013 Red Hat
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
23 struct iommu_domain *domain;
25 #define to_msm_iommu(x) container_of(x, struct msm_iommu, base)
27 static int msm_fault_handler(struct iommu_domain *domain, struct device *dev,
28 unsigned long iova, int flags, void *arg)
30 struct msm_iommu *iommu = arg;
31 if (iommu->base.handler)
32 return iommu->base.handler(iommu->base.arg, iova, flags);
33 pr_warn_ratelimited("*** fault: iova=%08lx, flags=%d\n", iova, flags);
37 static int msm_iommu_attach(struct msm_mmu *mmu, const char * const *names,
40 struct msm_iommu *iommu = to_msm_iommu(mmu);
42 return iommu_attach_device(iommu->domain, mmu->dev);
45 static void msm_iommu_detach(struct msm_mmu *mmu, const char * const *names,
48 struct msm_iommu *iommu = to_msm_iommu(mmu);
50 iommu_detach_device(iommu->domain, mmu->dev);
53 static int msm_iommu_map(struct msm_mmu *mmu, uint64_t iova,
54 struct sg_table *sgt, unsigned len, int prot)
56 struct msm_iommu *iommu = to_msm_iommu(mmu);
59 ret = iommu_map_sg(iommu->domain, iova, sgt->sgl, sgt->nents, prot);
62 return (ret == len) ? 0 : -EINVAL;
65 static int msm_iommu_unmap(struct msm_mmu *mmu, uint64_t iova, unsigned len)
67 struct msm_iommu *iommu = to_msm_iommu(mmu);
69 iommu_unmap(iommu->domain, iova, len);
74 static void msm_iommu_destroy(struct msm_mmu *mmu)
76 struct msm_iommu *iommu = to_msm_iommu(mmu);
77 iommu_domain_free(iommu->domain);
81 static const struct msm_mmu_funcs funcs = {
82 .attach = msm_iommu_attach,
83 .detach = msm_iommu_detach,
85 .unmap = msm_iommu_unmap,
86 .destroy = msm_iommu_destroy,
89 struct msm_mmu *msm_iommu_new(struct device *dev, struct iommu_domain *domain)
91 struct msm_iommu *iommu;
93 iommu = kzalloc(sizeof(*iommu), GFP_KERNEL);
95 return ERR_PTR(-ENOMEM);
97 iommu->domain = domain;
98 msm_mmu_init(&iommu->base, dev, &funcs);
99 iommu_set_fault_handler(domain, msm_fault_handler, iommu);