xref: /openbmc/linux/drivers/gpu/drm/msm/msm_iommu.c (revision 78babc1633c4b0664ea516500c2ace9bf1f17bc7)
1871d812aSRob Clark /*
2871d812aSRob Clark  * Copyright (C) 2013 Red Hat
3871d812aSRob Clark  * Author: Rob Clark <robdclark@gmail.com>
4871d812aSRob Clark  *
5871d812aSRob Clark  * This program is free software; you can redistribute it and/or modify it
6871d812aSRob Clark  * under the terms of the GNU General Public License version 2 as published by
7871d812aSRob Clark  * the Free Software Foundation.
8871d812aSRob Clark  *
9871d812aSRob Clark  * This program is distributed in the hope that it will be useful, but WITHOUT
10871d812aSRob Clark  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11871d812aSRob Clark  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12871d812aSRob Clark  * more details.
13871d812aSRob Clark  *
14871d812aSRob Clark  * You should have received a copy of the GNU General Public License along with
15871d812aSRob Clark  * this program.  If not, see <http://www.gnu.org/licenses/>.
16871d812aSRob Clark  */
17871d812aSRob Clark 
18871d812aSRob Clark #include "msm_drv.h"
19871d812aSRob Clark #include "msm_mmu.h"
20871d812aSRob Clark 
21871d812aSRob Clark struct msm_iommu {
22871d812aSRob Clark 	struct msm_mmu base;
23871d812aSRob Clark 	struct iommu_domain *domain;
24871d812aSRob Clark };
25871d812aSRob Clark #define to_msm_iommu(x) container_of(x, struct msm_iommu, base)
26871d812aSRob Clark 
27871d812aSRob Clark static int msm_fault_handler(struct iommu_domain *iommu, struct device *dev,
28871d812aSRob Clark 		unsigned long iova, int flags, void *arg)
29871d812aSRob Clark {
306814dbf9SRob Clark 	pr_warn_ratelimited("*** fault: iova=%08lx, flags=%d\n", iova, flags);
316814dbf9SRob Clark 	return 0;
32871d812aSRob Clark }
33871d812aSRob Clark 
34d72ab599SRob Clark static int msm_iommu_attach(struct msm_mmu *mmu, const char * const *names,
35d72ab599SRob Clark 			    int cnt)
36871d812aSRob Clark {
37871d812aSRob Clark 	struct msm_iommu *iommu = to_msm_iommu(mmu);
38944fc36cSRob Clark 	return iommu_attach_device(iommu->domain, mmu->dev);
39871d812aSRob Clark }
40871d812aSRob Clark 
41d72ab599SRob Clark static void msm_iommu_detach(struct msm_mmu *mmu, const char * const *names,
42d72ab599SRob Clark 			     int cnt)
4387e956e9SStephane Viau {
4487e956e9SStephane Viau 	struct msm_iommu *iommu = to_msm_iommu(mmu);
45944fc36cSRob Clark 	iommu_detach_device(iommu->domain, mmu->dev);
4687e956e9SStephane Viau }
4787e956e9SStephane Viau 
48*78babc16SRob Clark static int msm_iommu_map(struct msm_mmu *mmu, uint64_t iova,
49871d812aSRob Clark 		struct sg_table *sgt, unsigned len, int prot)
50871d812aSRob Clark {
51871d812aSRob Clark 	struct msm_iommu *iommu = to_msm_iommu(mmu);
52871d812aSRob Clark 	struct iommu_domain *domain = iommu->domain;
53871d812aSRob Clark 	struct scatterlist *sg;
54*78babc16SRob Clark 	unsigned long da = iova;
55871d812aSRob Clark 	unsigned int i, j;
56871d812aSRob Clark 	int ret;
57871d812aSRob Clark 
58871d812aSRob Clark 	if (!domain || !sgt)
59871d812aSRob Clark 		return -EINVAL;
60871d812aSRob Clark 
61871d812aSRob Clark 	for_each_sg(sgt->sgl, sg, sgt->nents, i) {
6269be1f4eSArchit Taneja 		dma_addr_t pa = sg_phys(sg) - sg->offset;
63871d812aSRob Clark 		size_t bytes = sg->length + sg->offset;
64871d812aSRob Clark 
65*78babc16SRob Clark 		VERB("map[%d]: %08lx %08lx(%zx)", i, da, (unsigned long)pa, bytes);
66871d812aSRob Clark 
67871d812aSRob Clark 		ret = iommu_map(domain, da, pa, bytes, prot);
68871d812aSRob Clark 		if (ret)
69871d812aSRob Clark 			goto fail;
70871d812aSRob Clark 
71871d812aSRob Clark 		da += bytes;
72871d812aSRob Clark 	}
73871d812aSRob Clark 
74871d812aSRob Clark 	return 0;
75871d812aSRob Clark 
76871d812aSRob Clark fail:
77871d812aSRob Clark 	da = iova;
78871d812aSRob Clark 
79871d812aSRob Clark 	for_each_sg(sgt->sgl, sg, i, j) {
80871d812aSRob Clark 		size_t bytes = sg->length + sg->offset;
81871d812aSRob Clark 		iommu_unmap(domain, da, bytes);
82871d812aSRob Clark 		da += bytes;
83871d812aSRob Clark 	}
84871d812aSRob Clark 	return ret;
85871d812aSRob Clark }
86871d812aSRob Clark 
87*78babc16SRob Clark static int msm_iommu_unmap(struct msm_mmu *mmu, uint64_t iova,
88871d812aSRob Clark 		struct sg_table *sgt, unsigned len)
89871d812aSRob Clark {
90871d812aSRob Clark 	struct msm_iommu *iommu = to_msm_iommu(mmu);
91871d812aSRob Clark 	struct iommu_domain *domain = iommu->domain;
92871d812aSRob Clark 	struct scatterlist *sg;
93*78babc16SRob Clark 	unsigned long da = iova;
94871d812aSRob Clark 	int i;
95871d812aSRob Clark 
96871d812aSRob Clark 	for_each_sg(sgt->sgl, sg, sgt->nents, i) {
97871d812aSRob Clark 		size_t bytes = sg->length + sg->offset;
98871d812aSRob Clark 		size_t unmapped;
99871d812aSRob Clark 
100871d812aSRob Clark 		unmapped = iommu_unmap(domain, da, bytes);
101871d812aSRob Clark 		if (unmapped < bytes)
102871d812aSRob Clark 			return unmapped;
103871d812aSRob Clark 
104*78babc16SRob Clark 		VERB("unmap[%d]: %08lx(%zx)", i, da, bytes);
105871d812aSRob Clark 
106cf3198c2SFabian Frederick 		BUG_ON(!PAGE_ALIGNED(bytes));
107871d812aSRob Clark 
108871d812aSRob Clark 		da += bytes;
109871d812aSRob Clark 	}
110871d812aSRob Clark 
111871d812aSRob Clark 	return 0;
112871d812aSRob Clark }
113871d812aSRob Clark 
114871d812aSRob Clark static void msm_iommu_destroy(struct msm_mmu *mmu)
115871d812aSRob Clark {
116871d812aSRob Clark 	struct msm_iommu *iommu = to_msm_iommu(mmu);
117871d812aSRob Clark 	iommu_domain_free(iommu->domain);
118871d812aSRob Clark 	kfree(iommu);
119871d812aSRob Clark }
120871d812aSRob Clark 
121871d812aSRob Clark static const struct msm_mmu_funcs funcs = {
122871d812aSRob Clark 		.attach = msm_iommu_attach,
12387e956e9SStephane Viau 		.detach = msm_iommu_detach,
124871d812aSRob Clark 		.map = msm_iommu_map,
125871d812aSRob Clark 		.unmap = msm_iommu_unmap,
126871d812aSRob Clark 		.destroy = msm_iommu_destroy,
127871d812aSRob Clark };
128871d812aSRob Clark 
129944fc36cSRob Clark struct msm_mmu *msm_iommu_new(struct device *dev, struct iommu_domain *domain)
130871d812aSRob Clark {
131871d812aSRob Clark 	struct msm_iommu *iommu;
132871d812aSRob Clark 
133871d812aSRob Clark 	iommu = kzalloc(sizeof(*iommu), GFP_KERNEL);
134871d812aSRob Clark 	if (!iommu)
135871d812aSRob Clark 		return ERR_PTR(-ENOMEM);
136871d812aSRob Clark 
137871d812aSRob Clark 	iommu->domain = domain;
138871d812aSRob Clark 	msm_mmu_init(&iommu->base, dev, &funcs);
139871d812aSRob Clark 	iommu_set_fault_handler(domain, msm_fault_handler, dev);
140871d812aSRob Clark 
141871d812aSRob Clark 	return &iommu->base;
142871d812aSRob Clark }
143