2 * Copyright (C) 2013 Red Hat
3 * Author: Rob Clark <robdclark@gmail.com>
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);
41 return iommu_attach_device(iommu->domain, mmu->dev);
44 static void msm_iommu_detach(struct msm_mmu *mmu, const char * const *names,
47 struct msm_iommu *iommu = to_msm_iommu(mmu);
48 iommu_detach_device(iommu->domain, mmu->dev);
51 static int msm_iommu_map(struct msm_mmu *mmu, uint64_t iova,
52 struct sg_table *sgt, unsigned len, int prot)
54 struct msm_iommu *iommu = to_msm_iommu(mmu);
55 struct iommu_domain *domain = iommu->domain;
56 struct scatterlist *sg;
57 unsigned long da = iova;
64 for_each_sg(sgt->sgl, sg, sgt->nents, i) {
65 dma_addr_t pa = sg_phys(sg) - sg->offset;
66 size_t bytes = sg->length + sg->offset;
68 VERB("map[%d]: %08lx %08lx(%zx)", i, da, (unsigned long)pa, bytes);
70 ret = iommu_map(domain, da, pa, bytes, prot);
82 for_each_sg(sgt->sgl, sg, i, j) {
83 size_t bytes = sg->length + sg->offset;
84 iommu_unmap(domain, da, bytes);
90 static int msm_iommu_unmap(struct msm_mmu *mmu, uint64_t iova,
91 struct sg_table *sgt, unsigned len)
93 struct msm_iommu *iommu = to_msm_iommu(mmu);
94 struct iommu_domain *domain = iommu->domain;
95 struct scatterlist *sg;
96 unsigned long da = iova;
99 for_each_sg(sgt->sgl, sg, sgt->nents, i) {
100 size_t bytes = sg->length + sg->offset;
103 unmapped = iommu_unmap(domain, da, bytes);
104 if (unmapped < bytes)
107 VERB("unmap[%d]: %08lx(%zx)", i, da, bytes);
109 BUG_ON(!PAGE_ALIGNED(bytes));
117 static void msm_iommu_destroy(struct msm_mmu *mmu)
119 struct msm_iommu *iommu = to_msm_iommu(mmu);
120 iommu_domain_free(iommu->domain);
124 static const struct msm_mmu_funcs funcs = {
125 .attach = msm_iommu_attach,
126 .detach = msm_iommu_detach,
127 .map = msm_iommu_map,
128 .unmap = msm_iommu_unmap,
129 .destroy = msm_iommu_destroy,
132 struct msm_mmu *msm_iommu_new(struct device *dev, struct iommu_domain *domain)
134 struct msm_iommu *iommu;
136 iommu = kzalloc(sizeof(*iommu), GFP_KERNEL);
138 return ERR_PTR(-ENOMEM);
140 iommu->domain = domain;
141 msm_mmu_init(&iommu->base, dev, &funcs);
142 iommu_set_fault_handler(domain, msm_fault_handler, iommu);