1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2016 Red Hat
4 * Author: Rob Clark <robdclark@gmail.com>
13 msm_gem_address_space_destroy(struct kref *kref)
15 struct msm_gem_address_space *aspace = container_of(kref,
16 struct msm_gem_address_space, kref);
18 drm_mm_takedown(&aspace->mm);
20 aspace->mmu->funcs->destroy(aspace->mmu);
26 void msm_gem_address_space_put(struct msm_gem_address_space *aspace)
29 kref_put(&aspace->kref, msm_gem_address_space_destroy);
32 struct msm_gem_address_space *
33 msm_gem_address_space_get(struct msm_gem_address_space *aspace)
35 if (!IS_ERR_OR_NULL(aspace))
36 kref_get(&aspace->kref);
41 /* Actually unmap memory for the vma */
42 void msm_gem_vma_purge(struct msm_gem_vma *vma)
44 struct msm_gem_address_space *aspace = vma->aspace;
45 unsigned size = vma->node.size;
47 /* Don't do anything if the memory isn't mapped */
51 aspace->mmu->funcs->unmap(aspace->mmu, vma->iova, size);
56 /* Map and pin vma: */
58 msm_gem_vma_map(struct msm_gem_vma *vma, int prot,
59 struct sg_table *sgt, int size)
61 struct msm_gem_address_space *aspace = vma->aspace;
64 if (GEM_WARN_ON(!vma->iova))
76 * NOTE: iommu/io-pgtable can allocate pages, so we cannot hold
77 * a lock across map/unmap which is also used in the job_run()
78 * path, as this can cause deadlock in job_run() vs shrinker/
81 * Revisit this if we can come up with a scheme to pre-alloc pages
82 * for the pgtable in map/unmap ops.
84 ret = aspace->mmu->funcs->map(aspace->mmu, vma->iova, sgt, size, prot);
93 /* Close an iova. Warn if it is still in use */
94 void msm_gem_vma_close(struct msm_gem_vma *vma)
96 struct msm_gem_address_space *aspace = vma->aspace;
98 GEM_WARN_ON(vma->mapped);
100 spin_lock(&aspace->lock);
102 drm_mm_remove_node(&vma->node);
103 spin_unlock(&aspace->lock);
107 msm_gem_address_space_put(aspace);
110 struct msm_gem_vma *msm_gem_vma_new(struct msm_gem_address_space *aspace)
112 struct msm_gem_vma *vma;
114 vma = kzalloc(sizeof(*vma), GFP_KERNEL);
118 vma->aspace = aspace;
123 /* Initialize a new vma and allocate an iova for it */
124 int msm_gem_vma_init(struct msm_gem_vma *vma, int size,
125 u64 range_start, u64 range_end)
127 struct msm_gem_address_space *aspace = vma->aspace;
130 if (GEM_WARN_ON(!aspace))
133 if (GEM_WARN_ON(vma->iova))
136 spin_lock(&aspace->lock);
137 ret = drm_mm_insert_node_in_range(&aspace->mm, &vma->node,
139 range_start, range_end, 0);
140 spin_unlock(&aspace->lock);
145 vma->iova = vma->node.start;
148 kref_get(&aspace->kref);
153 struct msm_gem_address_space *
154 msm_gem_address_space_create(struct msm_mmu *mmu, const char *name,
155 u64 va_start, u64 size)
157 struct msm_gem_address_space *aspace;
160 return ERR_CAST(mmu);
162 aspace = kzalloc(sizeof(*aspace), GFP_KERNEL);
164 return ERR_PTR(-ENOMEM);
166 spin_lock_init(&aspace->lock);
169 aspace->va_start = va_start;
170 aspace->va_size = size;
172 drm_mm_init(&aspace->mm, va_start, size);
174 kref_init(&aspace->kref);