drm-shim: move handle lock to shim_fd
[mesa.git] / src / drm-shim / drm_shim.h
1 /*
2 * Copyright © 2018 Broadcom
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #include <c11/threads.h>
25
26 #include "util/macros.h"
27 #include "util/hash_table.h"
28
29 #include <xf86drm.h>
30
31 #ifdef __linux__
32 #define DRM_MAJOR 226
33 #endif
34
35 typedef int (*ioctl_fn_t)(int fd, unsigned long request, void *arg);
36
37 struct shim_bo;
38
39 struct shim_device {
40 /* Mapping from int fd to struct shim_fd *. */
41 struct hash_table *fd_map;
42
43 int (**driver_ioctls)(int fd, unsigned long request, void *arg);
44 int driver_ioctl_count;
45
46 void (*driver_bo_free)(struct shim_bo *bo);
47
48 /* Returned by drmGetVersion(). */
49 const char *driver_name;
50 int version_major, version_minor, version_patchlevel;
51 int bus_type;
52 };
53
54 extern struct shim_device shim_device;
55
56 struct shim_fd {
57 int fd;
58 mtx_t handle_lock;
59 /* mapping from int gem handle to struct shim_bo *. */
60 struct hash_table *handles;
61 };
62
63 struct shim_bo {
64 int fd;
65 void *map;
66 int refcount;
67 uint32_t size;
68 };
69
70 /* Core support. */
71 extern int render_node_minor;
72 void drm_shim_device_init(void);
73 void drm_shim_override_file(const char *contents,
74 const char *path_format, ...) PRINTFLIKE(2, 3);
75 void drm_shim_fd_register(int fd, struct shim_fd *shim_fd);
76 struct shim_fd *drm_shim_fd_lookup(int fd);
77 int drm_shim_ioctl(int fd, unsigned long request, void *arg);
78 void *drm_shim_mmap(struct shim_fd *shim_fd, size_t length, int prot, int flags,
79 int fd, off_t offset);
80
81 void drm_shim_bo_init(struct shim_bo *bo, size_t size);
82 void drm_shim_bo_get(struct shim_bo *bo);
83 void drm_shim_bo_put(struct shim_bo *bo);
84 struct shim_bo *drm_shim_bo_lookup(struct shim_fd *shim_fd, int handle);
85 int drm_shim_bo_get_handle(struct shim_fd *shim_fd, struct shim_bo *bo);
86 uint64_t drm_shim_bo_get_mmap_offset(struct shim_fd *shim_fd,
87 struct shim_bo *bo);
88
89 /* driver-specific hooks. */
90 void drm_shim_driver_init(void);
91 extern bool drm_shim_driver_prefers_new_render_node;