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/>.
21 #define DUMMY_CONTEXT 0x1
25 struct iommu_domain *domain;
27 #define to_msm_iommu(x) container_of(x, struct msm_iommu, base)
29 static int msm_fault_handler(struct iommu_domain *iommu, struct device *dev,
30 unsigned long iova, int flags, void *arg)
32 pr_warn_ratelimited("*** fault: iova=%08lx, flags=%d\n", iova, flags);
36 static int msm_iommu_attach(struct msm_mmu *mmu, const char **names, int cnt)
38 struct device *dev = mmu->dev;
39 struct msm_iommu *iommu = to_msm_iommu(mmu);
42 for (i = 0; i < cnt; i++) {
43 struct device *msm_iommu_get_ctx(const char *ctx_name);
44 struct device *ctx = msm_iommu_get_ctx(names[i]);
45 if (IS_ERR_OR_NULL(ctx)) {
46 dev_warn(dev, "couldn't get %s context", names[i]);
50 if (ctx == (struct device *)DUMMY_CONTEXT) {
51 return iommu_attach_device(iommu->domain, mmu->dev);
53 ret = iommu_attach_device(iommu->domain, ctx);
57 dev_warn(dev, "could not attach iommu to %s", names[i]);
65 static void msm_iommu_detach(struct msm_mmu *mmu, const char **names, int cnt)
67 struct msm_iommu *iommu = to_msm_iommu(mmu);
70 for (i = 0; i < cnt; i++) {
71 struct device *msm_iommu_get_ctx(const char *ctx_name);
72 struct device *ctx = msm_iommu_get_ctx(names[i]);
73 if (IS_ERR_OR_NULL(ctx))
76 if (ctx == (struct device *)DUMMY_CONTEXT) {
77 iommu_detach_device(iommu->domain, mmu->dev);
80 iommu_detach_device(iommu->domain, ctx);
85 static int msm_iommu_map(struct msm_mmu *mmu, uint32_t iova,
86 struct sg_table *sgt, unsigned len, int prot)
88 struct msm_iommu *iommu = to_msm_iommu(mmu);
89 struct iommu_domain *domain = iommu->domain;
90 struct scatterlist *sg;
91 unsigned int da = iova;
98 for_each_sg(sgt->sgl, sg, sgt->nents, i) {
99 u32 pa = sg_phys(sg) - sg->offset;
100 size_t bytes = sg->length + sg->offset;
102 VERB("map[%d]: %08x %08x(%zx)", i, iova, pa, bytes);
104 ret = iommu_map(domain, da, pa, bytes, prot);
116 for_each_sg(sgt->sgl, sg, i, j) {
117 size_t bytes = sg->length + sg->offset;
118 iommu_unmap(domain, da, bytes);
124 static int msm_iommu_unmap(struct msm_mmu *mmu, uint32_t iova,
125 struct sg_table *sgt, unsigned len)
127 struct msm_iommu *iommu = to_msm_iommu(mmu);
128 struct iommu_domain *domain = iommu->domain;
129 struct scatterlist *sg;
130 unsigned int da = iova;
133 for_each_sg(sgt->sgl, sg, sgt->nents, i) {
134 size_t bytes = sg->length + sg->offset;
137 unmapped = iommu_unmap(domain, da, bytes);
138 if (unmapped < bytes)
141 VERB("unmap[%d]: %08x(%zx)", i, iova, bytes);
143 BUG_ON(!PAGE_ALIGNED(bytes));
151 static void msm_iommu_destroy(struct msm_mmu *mmu)
153 struct msm_iommu *iommu = to_msm_iommu(mmu);
154 iommu_domain_free(iommu->domain);
158 static const struct msm_mmu_funcs funcs = {
159 .attach = msm_iommu_attach,
160 .detach = msm_iommu_detach,
161 .map = msm_iommu_map,
162 .unmap = msm_iommu_unmap,
163 .destroy = msm_iommu_destroy,
166 struct msm_mmu *msm_iommu_new(struct device *dev, struct iommu_domain *domain)
168 struct msm_iommu *iommu;
170 iommu = kzalloc(sizeof(*iommu), GFP_KERNEL);
172 return ERR_PTR(-ENOMEM);
174 iommu->domain = domain;
175 msm_mmu_init(&iommu->base, dev, &funcs);
176 iommu_set_fault_handler(domain, msm_fault_handler, dev);