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
|
/*
* Copyright © 2018 Broadcom
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
/** @file
*
* Implements core GEM support (particularly ioctls) underneath the libc ioctl
* wrappers, and calls into the driver-specific code as necessary.
*/
#include <c11/threads.h>
#include <errno.h>
#include <linux/memfd.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <unistd.h>
#include "drm-uapi/drm.h"
#include "drm_shim.h"
#include "util/hash_table.h"
#include "util/u_atomic.h"
static mtx_t handle_lock = _MTX_INITIALIZER_NP;
#ifndef HAVE_MEMFD_CREATE
#include <sys/syscall.h>
static inline int
memfd_create(const char *name, unsigned int flags)
{
return syscall(SYS_memfd_create, name, flags);
}
#endif
/* Global state for the shim shared between libc, core, and driver. */
struct shim_device shim_device;
static uint32_t
uint_key_hash(const void *key)
{
return (uintptr_t)key;
}
static bool
uint_key_compare(const void *a, const void *b)
{
return a == b;
}
/**
* Called when the first libc shim is called, to initialize GEM simulation
* state (other than the shims themselves).
*/
void
drm_shim_device_init(void)
{
shim_device.fd_map = _mesa_hash_table_create(NULL,
uint_key_hash,
uint_key_compare);
drm_shim_driver_init();
}
static struct shim_fd *
drm_shim_file_create(int fd)
{
struct shim_fd *shim_fd = calloc(1, sizeof(*shim_fd));
shim_fd->fd = fd;
shim_fd->handles = _mesa_hash_table_create(NULL,
uint_key_hash,
uint_key_compare);
return shim_fd;
}
/**
* Called when the libc shims have interposed an open or dup of our simulated
* DRM device.
*/
void drm_shim_fd_register(int fd, struct shim_fd *shim_fd)
{
if (!shim_fd)
shim_fd = drm_shim_file_create(fd);
_mesa_hash_table_insert(shim_device.fd_map, (void *)(uintptr_t)(fd + 1), shim_fd);
}
struct shim_fd *
drm_shim_fd_lookup(int fd)
{
if (fd == -1)
return NULL;
struct hash_entry *entry =
_mesa_hash_table_search(shim_device.fd_map, (void *)(uintptr_t)(fd + 1));
if (!entry)
return NULL;
return entry->data;
}
/* ioctl used by drmGetVersion() */
static int
drm_shim_ioctl_version(int fd, unsigned long request, void *arg)
{
struct drm_version *args = arg;
const char *date = "20190320";
const char *desc = "shim";
args->version_major = shim_device.version_major;
args->version_minor = shim_device.version_minor;
args->version_patchlevel = shim_device.version_patchlevel;
if (args->name)
strncpy(args->name, shim_device.driver_name, args->name_len);
if (args->date)
strncpy(args->date, date, args->date_len);
if (args->desc)
strncpy(args->desc, desc, args->desc_len);
args->name_len = strlen(shim_device.driver_name);
args->date_len = strlen(date);
args->desc_len = strlen(desc);
return 0;
}
static int
drm_shim_ioctl_get_cap(int fd, unsigned long request, void *arg)
{
struct drm_get_cap *gc = arg;
switch (gc->capability) {
case DRM_CAP_PRIME:
case DRM_CAP_SYNCOBJ:
gc->value = 1;
return 0;
default:
fprintf(stderr, "DRM_IOCTL_GET_CAP: unhandled 0x%x\n",
(int)gc->capability);
return -1;
}
}
static int
drm_shim_ioctl_gem_close(int fd, unsigned long request, void *arg)
{
struct shim_fd *shim_fd = drm_shim_fd_lookup(fd);
struct drm_gem_close *c = arg;
if (!c->handle)
return 0;
mtx_lock(&handle_lock);
struct hash_entry *entry =
_mesa_hash_table_search(shim_fd->handles, (void *)(uintptr_t)c->handle);
if (!entry) {
mtx_unlock(&handle_lock);
return -EINVAL;
}
struct shim_bo *bo = entry->data;
_mesa_hash_table_remove(shim_fd->handles, entry);
drm_shim_bo_put(bo);
mtx_unlock(&handle_lock);
return 0;
}
static int
drm_shim_ioctl_stub(int fd, unsigned long request, void *arg)
{
return 0;
}
ioctl_fn_t core_ioctls[] = {
[_IOC_NR(DRM_IOCTL_VERSION)] = drm_shim_ioctl_version,
[_IOC_NR(DRM_IOCTL_GET_CAP)] = drm_shim_ioctl_get_cap,
[_IOC_NR(DRM_IOCTL_GEM_CLOSE)] = drm_shim_ioctl_gem_close,
[_IOC_NR(DRM_IOCTL_SYNCOBJ_CREATE)] = drm_shim_ioctl_stub,
[_IOC_NR(DRM_IOCTL_SYNCOBJ_DESTROY)] = drm_shim_ioctl_stub,
[_IOC_NR(DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD)] = drm_shim_ioctl_stub,
[_IOC_NR(DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE)] = drm_shim_ioctl_stub,
};
/**
* Implements the GEM core ioctls, and calls into driver-specific ioctls.
*/
int
drm_shim_ioctl(int fd, unsigned long request, void *arg)
{
int type = _IOC_TYPE(request);
int nr = _IOC_NR(request);
assert(type == DRM_IOCTL_BASE);
if (nr >= DRM_COMMAND_BASE && nr < DRM_COMMAND_END) {
int driver_nr = nr - DRM_COMMAND_BASE;
if (driver_nr < shim_device.driver_ioctl_count &&
shim_device.driver_ioctls[driver_nr]) {
return shim_device.driver_ioctls[driver_nr](fd, request, arg);
}
} else {
if (nr < ARRAY_SIZE(core_ioctls) && core_ioctls[nr]) {
return core_ioctls[nr](fd, request, arg);
}
}
if (nr >= DRM_COMMAND_BASE && nr < DRM_COMMAND_END) {
fprintf(stderr,
"DRM_SHIM: unhandled driver DRM ioctl %d (0x%08lx)\n",
nr - DRM_COMMAND_BASE, request);
} else {
fprintf(stderr,
"DRM_SHIM: unhandled core DRM ioctl 0x%X (0x%08lx)\n",
nr, request);
}
abort();
}
void
drm_shim_bo_init(struct shim_bo *bo, size_t size)
{
bo->size = size;
bo->fd = memfd_create("shim bo", MFD_CLOEXEC);
if (bo->fd == -1) {
fprintf(stderr, "Failed to create BO: %s\n", strerror(errno));
abort();
}
if (ftruncate(bo->fd, size) == -1) {
fprintf(stderr, "Failed to size BO: %s\n", strerror(errno));
abort();
}
}
struct shim_bo *
drm_shim_bo_lookup(struct shim_fd *shim_fd, int handle)
{
if (!handle)
return NULL;
mtx_lock(&handle_lock);
struct hash_entry *entry =
_mesa_hash_table_search(shim_fd->handles, (void *)(uintptr_t)handle);
struct shim_bo *bo = entry ? entry->data : NULL;
mtx_unlock(&handle_lock);
if (bo)
p_atomic_inc(&bo->refcount);
return bo;
}
void
drm_shim_bo_get(struct shim_bo *bo)
{
p_atomic_inc(&bo->refcount);
}
void
drm_shim_bo_put(struct shim_bo *bo)
{
if (p_atomic_dec_return(&bo->refcount) == 0)
return;
if (shim_device.driver_bo_free)
shim_device.driver_bo_free(bo);
close(bo->fd);
free(bo);
}
int
drm_shim_bo_get_handle(struct shim_fd *shim_fd, struct shim_bo *bo)
{
/* We should probably have some real datastructure for finding the free
* number.
*/
mtx_lock(&handle_lock);
for (int new_handle = 1; ; new_handle++) {
void *key = (void *)(uintptr_t)new_handle;
if (!_mesa_hash_table_search(shim_fd->handles, key)) {
drm_shim_bo_get(bo);
_mesa_hash_table_insert(shim_fd->handles, key, bo);
mtx_unlock(&handle_lock);
return new_handle;
}
}
mtx_unlock(&handle_lock);
return 0;
}
/* Creates an mmap offset for the BO in the DRM fd.
*
* XXX: We should be maintaining a u_mm allocator where the mmap offsets
* allocate the size of the BO and it can be used to look the BO back up.
* Instead, we just stuff the shim's pointer as the return value, and treat
* the incoming mmap offset on the DRM fd as a BO pointer. This doesn't work
* if someone tries to map a subset of the BO, but it's enough to get V3D
* working for now.
*/
uint64_t
drm_shim_bo_get_mmap_offset(struct shim_fd *shim_fd, struct shim_bo *bo)
{
return (uintptr_t)bo;
}
/* For mmap() on the DRM fd, look up the BO from the "offset" and map the BO's
* fd.
*/
void *
drm_shim_mmap(struct shim_fd *shim_fd, size_t length, int prot, int flags,
int fd, off_t offset)
{
struct shim_bo *bo = (void *)(uintptr_t)offset;
return mmap(NULL, length, prot, flags, bo->fd, 0);
}
|