1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
/*
* Copyright (C) 2016 Red Hat
* Author: Rob Clark <robdclark@gmail.com>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "msm_drv.h"
#include "msm_gem.h"
#include "msm_mmu.h"
/* SDE address space operations */
static void smmu_aspace_unmap_vma(struct msm_gem_address_space *aspace,
struct msm_gem_vma *vma, struct sg_table *sgt,
unsigned int flags)
{
if (!vma->iova)
return;
if (aspace) {
aspace->mmu->funcs->unmap_dma_buf(aspace->mmu, sgt,
DMA_BIDIRECTIONAL, flags);
}
vma->iova = 0;
msm_gem_address_space_put(aspace);
}
static int smmu_aspace_map_vma(struct msm_gem_address_space *aspace,
struct msm_gem_vma *vma, struct sg_table *sgt,
int npages, unsigned int flags)
{
int ret = -EINVAL;
if (!aspace || !aspace->domain_attached)
return ret;
ret = aspace->mmu->funcs->map_dma_buf(aspace->mmu, sgt,
DMA_BIDIRECTIONAL, flags);
if (!ret)
vma->iova = sg_dma_address(sgt->sgl);
/* Get a reference to the aspace to keep it around */
kref_get(&aspace->kref);
return ret;
}
static void smmu_aspace_destroy(struct msm_gem_address_space *aspace)
{
if (aspace->mmu)
aspace->mmu->funcs->destroy(aspace->mmu);
}
static void smmu_aspace_add_to_active(
struct msm_gem_address_space *aspace,
struct msm_gem_object *msm_obj)
{
WARN_ON(!mutex_is_locked(&aspace->list_lock));
list_move_tail(&msm_obj->iova_list, &aspace->active_list);
msm_obj->in_active_list = true;
}
static void smmu_aspace_remove_from_active(
struct msm_gem_address_space *aspace,
struct msm_gem_object *obj)
{
struct msm_gem_object *msm_obj, *next;
WARN_ON(!mutex_is_locked(&aspace->list_lock));
list_for_each_entry_safe(msm_obj, next, &aspace->active_list,
iova_list) {
if (msm_obj == obj) {
msm_obj->in_active_list = false;
list_del(&msm_obj->iova_list);
break;
}
}
}
static int smmu_aspace_register_cb(
struct msm_gem_address_space *aspace,
void (*cb)(void *, bool),
void *cb_data)
{
struct aspace_client *aclient = NULL;
struct aspace_client *temp;
if (!aspace)
return -EINVAL;
if (!aspace->domain_attached)
return -EACCES;
aclient = kzalloc(sizeof(*aclient), GFP_KERNEL);
if (!aclient)
return -ENOMEM;
aclient->cb = cb;
aclient->cb_data = cb_data;
INIT_LIST_HEAD(&aclient->list);
/* check if callback is already registered */
mutex_lock(&aspace->list_lock);
list_for_each_entry(temp, &aspace->clients, list) {
if ((temp->cb == aclient->cb) &&
(temp->cb_data == aclient->cb_data)) {
kfree(aclient);
mutex_unlock(&aspace->list_lock);
return -EEXIST;
}
}
list_move_tail(&aclient->list, &aspace->clients);
mutex_unlock(&aspace->list_lock);
return 0;
}
static int smmu_aspace_unregister_cb(
struct msm_gem_address_space *aspace,
void (*cb)(void *, bool),
void *cb_data)
{
struct aspace_client *aclient = NULL;
int rc = -ENOENT;
if (!aspace || !cb)
return -EINVAL;
mutex_lock(&aspace->list_lock);
list_for_each_entry(aclient, &aspace->clients, list) {
if ((aclient->cb == cb) &&
(aclient->cb_data == cb_data)) {
list_del(&aclient->list);
kfree(aclient);
rc = 0;
break;
}
}
mutex_unlock(&aspace->list_lock);
return rc;
}
static const struct msm_gem_aspace_ops smmu_aspace_ops = {
.map = smmu_aspace_map_vma,
.unmap = smmu_aspace_unmap_vma,
.destroy = smmu_aspace_destroy,
.add_to_active = smmu_aspace_add_to_active,
.remove_from_active = smmu_aspace_remove_from_active,
.register_cb = smmu_aspace_register_cb,
.unregister_cb = smmu_aspace_unregister_cb,
};
struct msm_gem_address_space *
msm_gem_smmu_address_space_create(struct drm_device *dev, struct msm_mmu *mmu,
const char *name)
{
struct msm_gem_address_space *aspace;
if (!mmu)
return ERR_PTR(-EINVAL);
aspace = kzalloc(sizeof(*aspace), GFP_KERNEL);
if (!aspace)
return ERR_PTR(-ENOMEM);
spin_lock_init(&aspace->lock);
aspace->dev = dev;
aspace->name = name;
aspace->mmu = mmu;
aspace->ops = &smmu_aspace_ops;
INIT_LIST_HEAD(&aspace->active_list);
INIT_LIST_HEAD(&aspace->clients);
kref_init(&aspace->kref);
mutex_init(&aspace->list_lock);
return aspace;
}
/* GPU address space operations */
static void iommu_aspace_unmap_vma(struct msm_gem_address_space *aspace,
struct msm_gem_vma *vma, struct sg_table *sgt,
unsigned int flags)
{
if (!aspace || !vma->iova)
return;
if (aspace->mmu) {
unsigned size = vma->node.size << PAGE_SHIFT;
aspace->mmu->funcs->unmap(aspace->mmu, vma->iova, sgt, size);
}
spin_lock(&aspace->lock);
drm_mm_remove_node(&vma->node);
spin_unlock(&aspace->lock);
vma->iova = 0;
msm_gem_address_space_put(aspace);
}
static int iommu_aspace_map_vma(struct msm_gem_address_space *aspace,
struct msm_gem_vma *vma, struct sg_table *sgt,
int npages, unsigned int flags)
{
int ret;
spin_lock(&aspace->lock);
if (WARN_ON(drm_mm_node_allocated(&vma->node))) {
spin_unlock(&aspace->lock);
return 0;
}
ret = drm_mm_insert_node(&aspace->mm, &vma->node, npages);
spin_unlock(&aspace->lock);
if (ret)
return ret;
vma->iova = vma->node.start << PAGE_SHIFT;
if (aspace->mmu) {
unsigned size = npages << PAGE_SHIFT;
ret = aspace->mmu->funcs->map(aspace->mmu, vma->iova, sgt,
size, IOMMU_READ | IOMMU_WRITE);
}
/* Get a reference to the aspace to keep it around */
kref_get(&aspace->kref);
return ret;
}
static void iommu_aspace_destroy(struct msm_gem_address_space *aspace)
{
drm_mm_takedown(&aspace->mm);
if (aspace->mmu)
aspace->mmu->funcs->destroy(aspace->mmu);
}
static const struct msm_gem_aspace_ops msm_iommu_aspace_ops = {
.map = iommu_aspace_map_vma,
.unmap = iommu_aspace_unmap_vma,
.destroy = iommu_aspace_destroy,
};
struct msm_gem_address_space *
msm_gem_address_space_create(struct device *dev, struct iommu_domain *domain,
const char *name)
{
struct msm_gem_address_space *aspace;
aspace = kzalloc(sizeof(*aspace), GFP_KERNEL);
if (!aspace)
return ERR_PTR(-ENOMEM);
spin_lock_init(&aspace->lock);
aspace->name = name;
aspace->mmu = msm_iommu_new(dev, domain);
aspace->ops = &msm_iommu_aspace_ops;
drm_mm_init(&aspace->mm, (domain->geometry.aperture_start >> PAGE_SHIFT),
(domain->geometry.aperture_end >> PAGE_SHIFT) - 1);
kref_init(&aspace->kref);
return aspace;
}
/* Generic address space operations */
static void
msm_gem_address_space_destroy(struct kref *kref)
{
struct msm_gem_address_space *aspace = container_of(kref,
struct msm_gem_address_space, kref);
if (aspace && aspace->ops->destroy)
aspace->ops->destroy(aspace);
kfree(aspace);
}
void msm_gem_address_space_put(struct msm_gem_address_space *aspace)
{
if (aspace)
kref_put(&aspace->kref, msm_gem_address_space_destroy);
}
void
msm_gem_unmap_vma(struct msm_gem_address_space *aspace,
struct msm_gem_vma *vma, struct sg_table *sgt,
unsigned int flags)
{
if (aspace && aspace->ops->unmap)
aspace->ops->unmap(aspace, vma, sgt, flags);
}
int
msm_gem_map_vma(struct msm_gem_address_space *aspace,
struct msm_gem_vma *vma, struct sg_table *sgt, int npages,
unsigned int flags)
{
if (aspace && aspace->ops->map)
return aspace->ops->map(aspace, vma, sgt, npages, flags);
return -EINVAL;
}
struct device *msm_gem_get_aspace_device(struct msm_gem_address_space *aspace)
{
struct device *client_dev = NULL;
if (aspace && aspace->mmu && aspace->mmu->funcs->get_dev)
client_dev = aspace->mmu->funcs->get_dev(aspace->mmu);
return client_dev;
}
void msm_gem_add_obj_to_aspace_active_list(
struct msm_gem_address_space *aspace,
struct drm_gem_object *obj)
{
struct msm_gem_object *msm_obj = to_msm_bo(obj);
if (aspace && aspace->ops && aspace->ops->add_to_active)
aspace->ops->add_to_active(aspace, msm_obj);
}
void msm_gem_remove_obj_from_aspace_active_list(
struct msm_gem_address_space *aspace,
struct drm_gem_object *obj)
{
struct msm_gem_object *msm_obj = to_msm_bo(obj);
if (aspace && aspace->ops && aspace->ops->remove_from_active)
aspace->ops->remove_from_active(aspace, msm_obj);
}
int msm_gem_address_space_register_cb(struct msm_gem_address_space *aspace,
void (*cb)(void *, bool),
void *cb_data)
{
if (aspace && aspace->ops && aspace->ops->register_cb)
return aspace->ops->register_cb(aspace, cb, cb_data);
return -EINVAL;
}
int msm_gem_address_space_unregister_cb(struct msm_gem_address_space *aspace,
void (*cb)(void *, bool),
void *cb_data)
{
if (aspace && aspace->ops && aspace->ops->unregister_cb)
return aspace->ops->unregister_cb(aspace, cb, cb_data);
return -EINVAL;
}
|