xref: /openbmc/linux/drivers/gpu/drm/msm/msm_gem_vma.c (revision ccb01374)
1 /*
2  * Copyright (C) 2016 Red Hat
3  * Author: Rob Clark <robdclark@gmail.com>
4  *
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.
8  *
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
12  * more details.
13  *
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/>.
16  */
17 
18 #include "msm_drv.h"
19 #include "msm_gem.h"
20 #include "msm_mmu.h"
21 
22 static void
23 msm_gem_address_space_destroy(struct kref *kref)
24 {
25 	struct msm_gem_address_space *aspace = container_of(kref,
26 			struct msm_gem_address_space, kref);
27 
28 	drm_mm_takedown(&aspace->mm);
29 	if (aspace->mmu)
30 		aspace->mmu->funcs->destroy(aspace->mmu);
31 	kfree(aspace);
32 }
33 
34 
35 void msm_gem_address_space_put(struct msm_gem_address_space *aspace)
36 {
37 	if (aspace)
38 		kref_put(&aspace->kref, msm_gem_address_space_destroy);
39 }
40 
41 /* Actually unmap memory for the vma */
42 void msm_gem_purge_vma(struct msm_gem_address_space *aspace,
43 		struct msm_gem_vma *vma)
44 {
45 	unsigned size = vma->node.size << PAGE_SHIFT;
46 
47 	/* Print a message if we try to purge a vma in use */
48 	if (WARN_ON(vma->inuse > 0))
49 		return;
50 
51 	/* Don't do anything if the memory isn't mapped */
52 	if (!vma->mapped)
53 		return;
54 
55 	if (aspace->mmu)
56 		aspace->mmu->funcs->unmap(aspace->mmu, vma->iova, size);
57 
58 	vma->mapped = false;
59 }
60 
61 /* Remove reference counts for the mapping */
62 void msm_gem_unmap_vma(struct msm_gem_address_space *aspace,
63 		struct msm_gem_vma *vma)
64 {
65 	if (!WARN_ON(!vma->iova))
66 		vma->inuse--;
67 }
68 
69 int
70 msm_gem_map_vma(struct msm_gem_address_space *aspace,
71 		struct msm_gem_vma *vma, struct sg_table *sgt, int npages)
72 {
73 	unsigned size = npages << PAGE_SHIFT;
74 	int ret = 0;
75 
76 	if (WARN_ON(!vma->iova))
77 		return -EINVAL;
78 
79 	/* Increase the usage counter */
80 	vma->inuse++;
81 
82 	if (vma->mapped)
83 		return 0;
84 
85 	vma->mapped = true;
86 
87 	if (aspace->mmu)
88 		ret = aspace->mmu->funcs->map(aspace->mmu, vma->iova, sgt,
89 				size, IOMMU_READ | IOMMU_WRITE);
90 
91 	if (ret)
92 		vma->mapped = false;
93 
94 	return ret;
95 }
96 
97 /* Close an iova.  Warn if it is still in use */
98 void msm_gem_close_vma(struct msm_gem_address_space *aspace,
99 		struct msm_gem_vma *vma)
100 {
101 	if (WARN_ON(vma->inuse > 0 || vma->mapped))
102 		return;
103 
104 	spin_lock(&aspace->lock);
105 	if (vma->iova)
106 		drm_mm_remove_node(&vma->node);
107 	spin_unlock(&aspace->lock);
108 
109 	vma->iova = 0;
110 
111 	msm_gem_address_space_put(aspace);
112 }
113 
114 /* Initialize a new vma and allocate an iova for it */
115 int msm_gem_init_vma(struct msm_gem_address_space *aspace,
116 		struct msm_gem_vma *vma, int npages)
117 {
118 	int ret;
119 
120 	if (WARN_ON(vma->iova))
121 		return -EBUSY;
122 
123 	spin_lock(&aspace->lock);
124 	ret = drm_mm_insert_node(&aspace->mm, &vma->node, npages);
125 	spin_unlock(&aspace->lock);
126 
127 	if (ret)
128 		return ret;
129 
130 	vma->iova = vma->node.start << PAGE_SHIFT;
131 	vma->mapped = false;
132 
133 	kref_get(&aspace->kref);
134 
135 	return 0;
136 }
137 
138 
139 struct msm_gem_address_space *
140 msm_gem_address_space_create(struct device *dev, struct iommu_domain *domain,
141 		const char *name)
142 {
143 	struct msm_gem_address_space *aspace;
144 	u64 size = domain->geometry.aperture_end -
145 		domain->geometry.aperture_start;
146 
147 	aspace = kzalloc(sizeof(*aspace), GFP_KERNEL);
148 	if (!aspace)
149 		return ERR_PTR(-ENOMEM);
150 
151 	spin_lock_init(&aspace->lock);
152 	aspace->name = name;
153 	aspace->mmu = msm_iommu_new(dev, domain);
154 
155 	drm_mm_init(&aspace->mm, (domain->geometry.aperture_start >> PAGE_SHIFT),
156 		size >> PAGE_SHIFT);
157 
158 	kref_init(&aspace->kref);
159 
160 	return aspace;
161 }
162 
163 struct msm_gem_address_space *
164 msm_gem_address_space_create_a2xx(struct device *dev, struct msm_gpu *gpu,
165 		const char *name, uint64_t va_start, uint64_t va_end)
166 {
167 	struct msm_gem_address_space *aspace;
168 	u64 size = va_end - va_start;
169 
170 	aspace = kzalloc(sizeof(*aspace), GFP_KERNEL);
171 	if (!aspace)
172 		return ERR_PTR(-ENOMEM);
173 
174 	spin_lock_init(&aspace->lock);
175 	aspace->name = name;
176 	aspace->mmu = msm_gpummu_new(dev, gpu);
177 
178 	drm_mm_init(&aspace->mm, (va_start >> PAGE_SHIFT),
179 		size >> PAGE_SHIFT);
180 
181 	kref_init(&aspace->kref);
182 
183 	return aspace;
184 }
185