Merge tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm
[sfrench/cifs-2.6.git] / drivers / gpu / drm / msm / msm_gem_vma.c
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, int prot,
72                 struct sg_table *sgt, int npages)
73 {
74         unsigned size = npages << PAGE_SHIFT;
75         int ret = 0;
76
77         if (WARN_ON(!vma->iova))
78                 return -EINVAL;
79
80         /* Increase the usage counter */
81         vma->inuse++;
82
83         if (vma->mapped)
84                 return 0;
85
86         vma->mapped = true;
87
88         if (aspace->mmu)
89                 ret = aspace->mmu->funcs->map(aspace->mmu, vma->iova, sgt,
90                                 size, prot);
91
92         if (ret)
93                 vma->mapped = false;
94
95         return ret;
96 }
97
98 /* Close an iova.  Warn if it is still in use */
99 void msm_gem_close_vma(struct msm_gem_address_space *aspace,
100                 struct msm_gem_vma *vma)
101 {
102         if (WARN_ON(vma->inuse > 0 || vma->mapped))
103                 return;
104
105         spin_lock(&aspace->lock);
106         if (vma->iova)
107                 drm_mm_remove_node(&vma->node);
108         spin_unlock(&aspace->lock);
109
110         vma->iova = 0;
111
112         msm_gem_address_space_put(aspace);
113 }
114
115 /* Initialize a new vma and allocate an iova for it */
116 int msm_gem_init_vma(struct msm_gem_address_space *aspace,
117                 struct msm_gem_vma *vma, int npages)
118 {
119         int ret;
120
121         if (WARN_ON(vma->iova))
122                 return -EBUSY;
123
124         spin_lock(&aspace->lock);
125         ret = drm_mm_insert_node(&aspace->mm, &vma->node, npages);
126         spin_unlock(&aspace->lock);
127
128         if (ret)
129                 return ret;
130
131         vma->iova = vma->node.start << PAGE_SHIFT;
132         vma->mapped = false;
133
134         kref_get(&aspace->kref);
135
136         return 0;
137 }
138
139
140 struct msm_gem_address_space *
141 msm_gem_address_space_create(struct device *dev, struct iommu_domain *domain,
142                 const char *name)
143 {
144         struct msm_gem_address_space *aspace;
145         u64 size = domain->geometry.aperture_end -
146                 domain->geometry.aperture_start;
147
148         aspace = kzalloc(sizeof(*aspace), GFP_KERNEL);
149         if (!aspace)
150                 return ERR_PTR(-ENOMEM);
151
152         spin_lock_init(&aspace->lock);
153         aspace->name = name;
154         aspace->mmu = msm_iommu_new(dev, domain);
155
156         drm_mm_init(&aspace->mm, (domain->geometry.aperture_start >> PAGE_SHIFT),
157                 size >> PAGE_SHIFT);
158
159         kref_init(&aspace->kref);
160
161         return aspace;
162 }
163
164 struct msm_gem_address_space *
165 msm_gem_address_space_create_a2xx(struct device *dev, struct msm_gpu *gpu,
166                 const char *name, uint64_t va_start, uint64_t va_end)
167 {
168         struct msm_gem_address_space *aspace;
169         u64 size = va_end - va_start;
170
171         aspace = kzalloc(sizeof(*aspace), GFP_KERNEL);
172         if (!aspace)
173                 return ERR_PTR(-ENOMEM);
174
175         spin_lock_init(&aspace->lock);
176         aspace->name = name;
177         aspace->mmu = msm_gpummu_new(dev, gpu);
178
179         drm_mm_init(&aspace->mm, (va_start >> PAGE_SHIFT),
180                 size >> PAGE_SHIFT);
181
182         kref_init(&aspace->kref);
183
184         return aspace;
185 }