2 * Copyright (C) 2016 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 msm_gem_address_space_destroy(struct kref *kref)
25 struct msm_gem_address_space *aspace = container_of(kref,
26 struct msm_gem_address_space, kref);
28 drm_mm_takedown(&aspace->mm);
30 aspace->mmu->funcs->destroy(aspace->mmu);
35 void msm_gem_address_space_put(struct msm_gem_address_space *aspace)
38 kref_put(&aspace->kref, msm_gem_address_space_destroy);
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)
45 unsigned size = vma->node.size << PAGE_SHIFT;
47 /* Print a message if we try to purge a vma in use */
48 if (WARN_ON(vma->inuse > 0))
51 /* Don't do anything if the memory isn't mapped */
56 aspace->mmu->funcs->unmap(aspace->mmu, vma->iova, size);
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)
65 if (!WARN_ON(!vma->iova))
70 msm_gem_map_vma(struct msm_gem_address_space *aspace,
71 struct msm_gem_vma *vma, struct sg_table *sgt, int npages)
73 unsigned size = npages << PAGE_SHIFT;
76 if (WARN_ON(!vma->iova))
79 /* Increase the usage counter */
88 ret = aspace->mmu->funcs->map(aspace->mmu, vma->iova, sgt,
89 size, IOMMU_READ | IOMMU_WRITE);
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)
101 if (WARN_ON(vma->inuse > 0 || vma->mapped))
104 spin_lock(&aspace->lock);
106 drm_mm_remove_node(&vma->node);
107 spin_unlock(&aspace->lock);
111 msm_gem_address_space_put(aspace);
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)
120 if (WARN_ON(vma->iova))
123 spin_lock(&aspace->lock);
124 ret = drm_mm_insert_node(&aspace->mm, &vma->node, npages);
125 spin_unlock(&aspace->lock);
130 vma->iova = vma->node.start << PAGE_SHIFT;
133 kref_get(&aspace->kref);
139 struct msm_gem_address_space *
140 msm_gem_address_space_create(struct device *dev, struct iommu_domain *domain,
143 struct msm_gem_address_space *aspace;
144 u64 size = domain->geometry.aperture_end -
145 domain->geometry.aperture_start;
147 aspace = kzalloc(sizeof(*aspace), GFP_KERNEL);
149 return ERR_PTR(-ENOMEM);
151 spin_lock_init(&aspace->lock);
153 aspace->mmu = msm_iommu_new(dev, domain);
155 drm_mm_init(&aspace->mm, (domain->geometry.aperture_start >> PAGE_SHIFT),
158 kref_init(&aspace->kref);
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)
167 struct msm_gem_address_space *aspace;
168 u64 size = va_end - va_start;
170 aspace = kzalloc(sizeof(*aspace), GFP_KERNEL);
172 return ERR_PTR(-ENOMEM);
174 spin_lock_init(&aspace->lock);
176 aspace->mmu = msm_gpummu_new(dev, gpu);
178 drm_mm_init(&aspace->mm, (va_start >> PAGE_SHIFT),
181 kref_init(&aspace->kref);