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 *iommu, struct device *dev,
28 unsigned long iova, int flags, void *arg)
30 DBG("*** fault: iova=%08lx, flags=%d", iova, flags);
34 static int msm_iommu_attach(struct msm_mmu *mmu, const char **names, int cnt)
36 struct drm_device *dev = mmu->dev;
37 struct msm_iommu *iommu = to_msm_iommu(mmu);
40 for (i = 0; i < cnt; i++) {
41 struct device *msm_iommu_get_ctx(const char *ctx_name);
42 struct device *ctx = msm_iommu_get_ctx(names[i]);
43 if (IS_ERR_OR_NULL(ctx)) {
44 dev_warn(dev->dev, "couldn't get %s context", names[i]);
47 ret = iommu_attach_device(iommu->domain, ctx);
49 dev_warn(dev->dev, "could not attach iommu to %s", names[i]);
57 static void msm_iommu_detach(struct msm_mmu *mmu, const char **names, int cnt)
59 struct msm_iommu *iommu = to_msm_iommu(mmu);
62 for (i = 0; i < cnt; i++) {
63 struct device *msm_iommu_get_ctx(const char *ctx_name);
64 struct device *ctx = msm_iommu_get_ctx(names[i]);
65 if (IS_ERR_OR_NULL(ctx))
67 iommu_detach_device(iommu->domain, ctx);
71 static int msm_iommu_map(struct msm_mmu *mmu, uint32_t iova,
72 struct sg_table *sgt, unsigned len, int prot)
74 struct msm_iommu *iommu = to_msm_iommu(mmu);
75 struct iommu_domain *domain = iommu->domain;
76 struct scatterlist *sg;
77 unsigned int da = iova;
84 for_each_sg(sgt->sgl, sg, sgt->nents, i) {
85 u32 pa = sg_phys(sg) - sg->offset;
86 size_t bytes = sg->length + sg->offset;
88 VERB("map[%d]: %08x %08x(%x)", i, iova, pa, bytes);
90 ret = iommu_map(domain, da, pa, bytes, prot);
102 for_each_sg(sgt->sgl, sg, i, j) {
103 size_t bytes = sg->length + sg->offset;
104 iommu_unmap(domain, da, bytes);
110 static int msm_iommu_unmap(struct msm_mmu *mmu, uint32_t iova,
111 struct sg_table *sgt, unsigned len)
113 struct msm_iommu *iommu = to_msm_iommu(mmu);
114 struct iommu_domain *domain = iommu->domain;
115 struct scatterlist *sg;
116 unsigned int da = iova;
119 for_each_sg(sgt->sgl, sg, sgt->nents, i) {
120 size_t bytes = sg->length + sg->offset;
123 unmapped = iommu_unmap(domain, da, bytes);
124 if (unmapped < bytes)
127 VERB("unmap[%d]: %08x(%x)", i, iova, bytes);
129 BUG_ON(!PAGE_ALIGNED(bytes));
137 static void msm_iommu_destroy(struct msm_mmu *mmu)
139 struct msm_iommu *iommu = to_msm_iommu(mmu);
140 iommu_domain_free(iommu->domain);
144 static const struct msm_mmu_funcs funcs = {
145 .attach = msm_iommu_attach,
146 .detach = msm_iommu_detach,
147 .map = msm_iommu_map,
148 .unmap = msm_iommu_unmap,
149 .destroy = msm_iommu_destroy,
152 struct msm_mmu *msm_iommu_new(struct drm_device *dev, struct iommu_domain *domain)
154 struct msm_iommu *iommu;
156 iommu = kzalloc(sizeof(*iommu), GFP_KERNEL);
158 return ERR_PTR(-ENOMEM);
160 iommu->domain = domain;
161 msm_mmu_init(&iommu->base, dev, &funcs);
162 iommu_set_fault_handler(domain, msm_fault_handler, dev);