Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (C) 2016 Red Hat
4 * Author: Rob Clark <robdclark@gmail.com>
5 */
6
7#include "msm_drv.h"
8#include "msm_fence.h"
9#include "msm_gem.h"
10#include "msm_mmu.h"
11
12static void
13msm_gem_address_space_destroy(struct kref *kref)
14{
15 struct msm_gem_address_space *aspace = container_of(kref,
16 struct msm_gem_address_space, kref);
17
18 drm_mm_takedown(&aspace->mm);
19 if (aspace->mmu)
20 aspace->mmu->funcs->destroy(aspace->mmu);
21 put_pid(aspace->pid);
22 kfree(aspace);
23}
24
25
26void msm_gem_address_space_put(struct msm_gem_address_space *aspace)
27{
28 if (aspace)
29 kref_put(&aspace->kref, msm_gem_address_space_destroy);
30}
31
32struct msm_gem_address_space *
33msm_gem_address_space_get(struct msm_gem_address_space *aspace)
34{
35 if (!IS_ERR_OR_NULL(aspace))
36 kref_get(&aspace->kref);
37
38 return aspace;
39}
40
41/* Actually unmap memory for the vma */
42void msm_gem_vma_purge(struct msm_gem_vma *vma)
43{
44 struct msm_gem_address_space *aspace = vma->aspace;
45 unsigned size = vma->node.size;
46
47 /* Don't do anything if the memory isn't mapped */
48 if (!vma->mapped)
49 return;
50
51 aspace->mmu->funcs->unmap(aspace->mmu, vma->iova, size);
52
53 vma->mapped = false;
54}
55
56/* Map and pin vma: */
57int
58msm_gem_vma_map(struct msm_gem_vma *vma, int prot,
59 struct sg_table *sgt, int size)
60{
61 struct msm_gem_address_space *aspace = vma->aspace;
62 int ret;
63
64 if (GEM_WARN_ON(!vma->iova))
65 return -EINVAL;
66
67 if (vma->mapped)
68 return 0;
69
70 vma->mapped = true;
71
72 if (!aspace)
73 return 0;
74
75 /*
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/
79 * reclaim.
80 *
81 * Revisit this if we can come up with a scheme to pre-alloc pages
82 * for the pgtable in map/unmap ops.
83 */
84 ret = aspace->mmu->funcs->map(aspace->mmu, vma->iova, sgt, size, prot);
85
86 if (ret) {
87 vma->mapped = false;
88 }
89
90 return ret;
91}
92
93/* Close an iova. Warn if it is still in use */
94void msm_gem_vma_close(struct msm_gem_vma *vma)
95{
96 struct msm_gem_address_space *aspace = vma->aspace;
97
98 GEM_WARN_ON(vma->mapped);
99
100 spin_lock(&aspace->lock);
101 if (vma->iova)
102 drm_mm_remove_node(&vma->node);
103 spin_unlock(&aspace->lock);
104
105 vma->iova = 0;
106
107 msm_gem_address_space_put(aspace);
108}
109
110struct msm_gem_vma *msm_gem_vma_new(struct msm_gem_address_space *aspace)
111{
112 struct msm_gem_vma *vma;
113
114 vma = kzalloc(sizeof(*vma), GFP_KERNEL);
115 if (!vma)
116 return NULL;
117
118 vma->aspace = aspace;
119
120 return vma;
121}
122
123/* Initialize a new vma and allocate an iova for it */
124int msm_gem_vma_init(struct msm_gem_vma *vma, int size,
125 u64 range_start, u64 range_end)
126{
127 struct msm_gem_address_space *aspace = vma->aspace;
128 int ret;
129
130 if (GEM_WARN_ON(!aspace))
131 return -EINVAL;
132
133 if (GEM_WARN_ON(vma->iova))
134 return -EBUSY;
135
136 spin_lock(&aspace->lock);
137 ret = drm_mm_insert_node_in_range(&aspace->mm, &vma->node,
138 size, PAGE_SIZE, 0,
139 range_start, range_end, 0);
140 spin_unlock(&aspace->lock);
141
142 if (ret)
143 return ret;
144
145 vma->iova = vma->node.start;
146 vma->mapped = false;
147
148 kref_get(&aspace->kref);
149
150 return 0;
151}
152
153struct msm_gem_address_space *
154msm_gem_address_space_create(struct msm_mmu *mmu, const char *name,
155 u64 va_start, u64 size)
156{
157 struct msm_gem_address_space *aspace;
158
159 if (IS_ERR(mmu))
160 return ERR_CAST(mmu);
161
162 aspace = kzalloc(sizeof(*aspace), GFP_KERNEL);
163 if (!aspace)
164 return ERR_PTR(-ENOMEM);
165
166 spin_lock_init(&aspace->lock);
167 aspace->name = name;
168 aspace->mmu = mmu;
169 aspace->va_start = va_start;
170 aspace->va_size = size;
171
172 drm_mm_init(&aspace->mm, va_start, size);
173
174 kref_init(&aspace->kref);
175
176 return aspace;
177}
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
22void
23msm_gem_unmap_vma(struct msm_gem_address_space *aspace,
24 struct msm_gem_vma *vma, struct sg_table *sgt)
25{
26 if (!vma->iova)
27 return;
28
29 if (aspace->mmu) {
30 unsigned size = vma->node.size << PAGE_SHIFT;
31 aspace->mmu->funcs->unmap(aspace->mmu, vma->iova, sgt, size);
32 }
33
34 drm_mm_remove_node(&vma->node);
35
36 vma->iova = 0;
37}
38
39int
40msm_gem_map_vma(struct msm_gem_address_space *aspace,
41 struct msm_gem_vma *vma, struct sg_table *sgt, int npages)
42{
43 int ret;
44
45 if (WARN_ON(drm_mm_node_allocated(&vma->node)))
46 return 0;
47
48 ret = drm_mm_insert_node(&aspace->mm, &vma->node, npages,
49 0, DRM_MM_SEARCH_DEFAULT);
50 if (ret)
51 return ret;
52
53 vma->iova = vma->node.start << PAGE_SHIFT;
54
55 if (aspace->mmu) {
56 unsigned size = npages << PAGE_SHIFT;
57 ret = aspace->mmu->funcs->map(aspace->mmu, vma->iova, sgt,
58 size, IOMMU_READ | IOMMU_WRITE);
59 }
60
61 return ret;
62}
63
64void
65msm_gem_address_space_destroy(struct msm_gem_address_space *aspace)
66{
67 drm_mm_takedown(&aspace->mm);
68 if (aspace->mmu)
69 aspace->mmu->funcs->destroy(aspace->mmu);
70 kfree(aspace);
71}
72
73struct msm_gem_address_space *
74msm_gem_address_space_create(struct device *dev, struct iommu_domain *domain,
75 const char *name)
76{
77 struct msm_gem_address_space *aspace;
78
79 aspace = kzalloc(sizeof(*aspace), GFP_KERNEL);
80 if (!aspace)
81 return ERR_PTR(-ENOMEM);
82
83 aspace->name = name;
84 aspace->mmu = msm_iommu_new(dev, domain);
85
86 drm_mm_init(&aspace->mm, (domain->geometry.aperture_start >> PAGE_SHIFT),
87 (domain->geometry.aperture_end >> PAGE_SHIFT) - 1);
88
89 return aspace;
90}