Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/etnaviv/drm/etnaviv_bo.c
4565 views
1
/*
2
* Copyright (C) 2014 Etnaviv Project
3
*
4
* Permission is hereby granted, free of charge, to any person obtaining a
5
* copy of this software and associated documentation files (the "Software"),
6
* to deal in the Software without restriction, including without limitation
7
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
* and/or sell copies of the Software, and to permit persons to whom the
9
* Software is furnished to do so, subject to the following conditions:
10
*
11
* The above copyright notice and this permission notice (including the next
12
* paragraph) shall be included in all copies or substantial portions of the
13
* Software.
14
*
15
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
* SOFTWARE.
22
*
23
* Authors:
24
* Christian Gmeiner <[email protected]>
25
*/
26
27
#include "os/os_mman.h"
28
#include "util/hash_table.h"
29
30
#include "etnaviv_priv.h"
31
#include "etnaviv_drmif.h"
32
33
simple_mtx_t etna_drm_table_lock = _SIMPLE_MTX_INITIALIZER_NP;
34
void _etna_bo_del(struct etna_bo *bo);
35
36
/* set buffer name, and add to table, call w/ etna_drm_table_lock held: */
37
static void set_name(struct etna_bo *bo, uint32_t name)
38
{
39
simple_mtx_assert_locked(&etna_drm_table_lock);
40
41
bo->name = name;
42
/* add ourself into the name table: */
43
_mesa_hash_table_insert(bo->dev->name_table, &bo->name, bo);
44
}
45
46
/* Called under etna_drm_table_lock */
47
void _etna_bo_del(struct etna_bo *bo)
48
{
49
VG_BO_FREE(bo);
50
51
simple_mtx_assert_locked(&etna_drm_table_lock);
52
53
if (bo->va)
54
util_vma_heap_free(&bo->dev->address_space, bo->va, bo->size);
55
56
if (bo->map)
57
os_munmap(bo->map, bo->size);
58
59
if (bo->handle) {
60
struct drm_gem_close req = {
61
.handle = bo->handle,
62
};
63
64
if (bo->name)
65
_mesa_hash_table_remove_key(bo->dev->name_table, &bo->name);
66
67
_mesa_hash_table_remove_key(bo->dev->handle_table, &bo->handle);
68
drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
69
}
70
71
free(bo);
72
}
73
74
/* lookup a buffer from it's handle, call w/ etna_drm_table_lock held: */
75
static struct etna_bo *lookup_bo(void *tbl, uint32_t handle)
76
{
77
struct etna_bo *bo = NULL;
78
struct hash_entry *entry;
79
80
simple_mtx_assert_locked(&etna_drm_table_lock);
81
82
entry = _mesa_hash_table_search(tbl, &handle);
83
84
if (entry) {
85
/* found, incr refcnt and return: */
86
bo = etna_bo_ref(entry->data);
87
88
/* don't break the bucket if this bo was found in one */
89
list_delinit(&bo->list);
90
}
91
92
return bo;
93
}
94
95
/* allocate a new buffer object, call w/ etna_drm_table_lock held */
96
static struct etna_bo *bo_from_handle(struct etna_device *dev,
97
uint32_t size, uint32_t handle, uint32_t flags)
98
{
99
struct etna_bo *bo = calloc(sizeof(*bo), 1);
100
101
simple_mtx_assert_locked(&etna_drm_table_lock);
102
103
if (!bo) {
104
struct drm_gem_close req = {
105
.handle = handle,
106
};
107
108
drmIoctl(dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
109
110
return NULL;
111
}
112
113
bo->dev = etna_device_ref(dev);
114
bo->size = size;
115
bo->handle = handle;
116
bo->flags = flags;
117
p_atomic_set(&bo->refcnt, 1);
118
list_inithead(&bo->list);
119
/* add ourselves to the handle table: */
120
_mesa_hash_table_insert(dev->handle_table, &bo->handle, bo);
121
122
if (dev->use_softpin)
123
bo->va = util_vma_heap_alloc(&dev->address_space, bo->size, 4096);
124
125
return bo;
126
}
127
128
/* allocate a new (un-tiled) buffer object */
129
struct etna_bo *etna_bo_new(struct etna_device *dev, uint32_t size,
130
uint32_t flags)
131
{
132
struct etna_bo *bo;
133
int ret;
134
struct drm_etnaviv_gem_new req = {
135
.flags = flags,
136
};
137
138
bo = etna_bo_cache_alloc(&dev->bo_cache, &size, flags);
139
if (bo)
140
return bo;
141
142
req.size = size;
143
ret = drmCommandWriteRead(dev->fd, DRM_ETNAVIV_GEM_NEW,
144
&req, sizeof(req));
145
if (ret)
146
return NULL;
147
148
simple_mtx_lock(&etna_drm_table_lock);
149
bo = bo_from_handle(dev, size, req.handle, flags);
150
bo->reuse = 1;
151
simple_mtx_unlock(&etna_drm_table_lock);
152
153
VG_BO_ALLOC(bo);
154
155
return bo;
156
}
157
158
struct etna_bo *etna_bo_ref(struct etna_bo *bo)
159
{
160
p_atomic_inc(&bo->refcnt);
161
162
return bo;
163
}
164
165
/* get buffer info */
166
static int get_buffer_info(struct etna_bo *bo)
167
{
168
int ret;
169
struct drm_etnaviv_gem_info req = {
170
.handle = bo->handle,
171
};
172
173
ret = drmCommandWriteRead(bo->dev->fd, DRM_ETNAVIV_GEM_INFO,
174
&req, sizeof(req));
175
if (ret) {
176
return ret;
177
}
178
179
/* really all we need for now is mmap offset */
180
bo->offset = req.offset;
181
182
return 0;
183
}
184
185
/* import a buffer object from DRI2 name */
186
struct etna_bo *etna_bo_from_name(struct etna_device *dev,
187
uint32_t name)
188
{
189
struct etna_bo *bo;
190
struct drm_gem_open req = {
191
.name = name,
192
};
193
194
simple_mtx_lock(&etna_drm_table_lock);
195
196
/* check name table first, to see if bo is already open: */
197
bo = lookup_bo(dev->name_table, name);
198
if (bo)
199
goto out_unlock;
200
201
if (drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req)) {
202
ERROR_MSG("gem-open failed: %s", strerror(errno));
203
goto out_unlock;
204
}
205
206
bo = lookup_bo(dev->handle_table, req.handle);
207
if (bo)
208
goto out_unlock;
209
210
bo = bo_from_handle(dev, req.size, req.handle, 0);
211
if (bo) {
212
set_name(bo, name);
213
VG_BO_ALLOC(bo);
214
}
215
216
out_unlock:
217
simple_mtx_unlock(&etna_drm_table_lock);
218
219
return bo;
220
}
221
222
/* import a buffer from dmabuf fd, does not take ownership of the
223
* fd so caller should close() the fd when it is otherwise done
224
* with it (even if it is still using the 'struct etna_bo *')
225
*/
226
struct etna_bo *etna_bo_from_dmabuf(struct etna_device *dev, int fd)
227
{
228
struct etna_bo *bo;
229
int ret, size;
230
uint32_t handle;
231
232
/* take the lock before calling drmPrimeFDToHandle to avoid
233
* racing against etna_bo_del, which might invalidate the
234
* returned handle.
235
*/
236
simple_mtx_lock(&etna_drm_table_lock);
237
238
ret = drmPrimeFDToHandle(dev->fd, fd, &handle);
239
if (ret) {
240
simple_mtx_unlock(&etna_drm_table_lock);
241
return NULL;
242
}
243
244
bo = lookup_bo(dev->handle_table, handle);
245
if (bo)
246
goto out_unlock;
247
248
/* lseek() to get bo size */
249
size = lseek(fd, 0, SEEK_END);
250
lseek(fd, 0, SEEK_CUR);
251
252
bo = bo_from_handle(dev, size, handle, 0);
253
254
VG_BO_ALLOC(bo);
255
256
out_unlock:
257
simple_mtx_unlock(&etna_drm_table_lock);
258
259
return bo;
260
}
261
262
/* destroy a buffer object */
263
void etna_bo_del(struct etna_bo *bo)
264
{
265
if (!bo)
266
return;
267
268
struct etna_device *dev = bo->dev;
269
270
simple_mtx_lock(&etna_drm_table_lock);
271
272
/* Must test under table lock to avoid racing with the from_dmabuf/name
273
* paths, which rely on the BO refcount to be stable over the lookup, so
274
* they can grab a reference when the BO is found in the hash.
275
*/
276
if (!p_atomic_dec_zero(&bo->refcnt))
277
goto out;
278
279
if (bo->reuse && (etna_bo_cache_free(&dev->bo_cache, bo) == 0))
280
goto out;
281
282
_etna_bo_del(bo);
283
etna_device_del_locked(dev);
284
out:
285
simple_mtx_unlock(&etna_drm_table_lock);
286
}
287
288
/* get the global flink/DRI2 buffer name */
289
int etna_bo_get_name(struct etna_bo *bo, uint32_t *name)
290
{
291
if (!bo->name) {
292
struct drm_gem_flink req = {
293
.handle = bo->handle,
294
};
295
int ret;
296
297
ret = drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_FLINK, &req);
298
if (ret) {
299
return ret;
300
}
301
302
simple_mtx_lock(&etna_drm_table_lock);
303
set_name(bo, req.name);
304
simple_mtx_unlock(&etna_drm_table_lock);
305
bo->reuse = 0;
306
}
307
308
*name = bo->name;
309
310
return 0;
311
}
312
313
uint32_t etna_bo_handle(struct etna_bo *bo)
314
{
315
return bo->handle;
316
}
317
318
/* caller owns the dmabuf fd that is returned and is responsible
319
* to close() it when done
320
*/
321
int etna_bo_dmabuf(struct etna_bo *bo)
322
{
323
int ret, prime_fd;
324
325
ret = drmPrimeHandleToFD(bo->dev->fd, bo->handle, DRM_CLOEXEC,
326
&prime_fd);
327
if (ret) {
328
ERROR_MSG("failed to get dmabuf fd: %d", ret);
329
return ret;
330
}
331
332
bo->reuse = 0;
333
334
return prime_fd;
335
}
336
337
uint32_t etna_bo_size(struct etna_bo *bo)
338
{
339
return bo->size;
340
}
341
342
uint32_t etna_bo_gpu_va(struct etna_bo *bo)
343
{
344
return bo->va;
345
}
346
347
void *etna_bo_map(struct etna_bo *bo)
348
{
349
if (!bo->map) {
350
if (!bo->offset) {
351
get_buffer_info(bo);
352
}
353
354
bo->map = os_mmap(0, bo->size, PROT_READ | PROT_WRITE,
355
MAP_SHARED, bo->dev->fd, bo->offset);
356
if (bo->map == MAP_FAILED) {
357
ERROR_MSG("mmap failed: %s", strerror(errno));
358
bo->map = NULL;
359
}
360
}
361
362
return bo->map;
363
}
364
365
int etna_bo_cpu_prep(struct etna_bo *bo, uint32_t op)
366
{
367
struct drm_etnaviv_gem_cpu_prep req = {
368
.handle = bo->handle,
369
.op = op,
370
};
371
372
get_abs_timeout(&req.timeout, 5000000000);
373
374
return drmCommandWrite(bo->dev->fd, DRM_ETNAVIV_GEM_CPU_PREP,
375
&req, sizeof(req));
376
}
377
378
void etna_bo_cpu_fini(struct etna_bo *bo)
379
{
380
struct drm_etnaviv_gem_cpu_fini req = {
381
.handle = bo->handle,
382
};
383
384
drmCommandWrite(bo->dev->fd, DRM_ETNAVIV_GEM_CPU_FINI,
385
&req, sizeof(req));
386
}
387
388