1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2018 Broadcom
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21bf215546Sopenharmony_ci * DEALINGS IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include <c11/threads.h>
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci#include "util/macros.h"
27bf215546Sopenharmony_ci#include "util/hash_table.h"
28bf215546Sopenharmony_ci#include "util/vma.h"
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci#include <xf86drm.h>
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ci#ifdef __linux__
33bf215546Sopenharmony_ci#define DRM_MAJOR 226
34bf215546Sopenharmony_ci#endif
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_citypedef int (*ioctl_fn_t)(int fd, unsigned long request, void *arg);
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_cistruct shim_bo;
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_cistruct shim_device {
41bf215546Sopenharmony_ci   /* Mapping from int fd to struct shim_fd *. */
42bf215546Sopenharmony_ci   struct hash_table *fd_map;
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_ci   /* Mapping from mmap offset to shim_bo */
45bf215546Sopenharmony_ci   struct hash_table_u64 *offset_map;
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_ci   mtx_t mem_lock;
48bf215546Sopenharmony_ci   /* Heap from which shim_bo are allocated */
49bf215546Sopenharmony_ci   struct util_vma_heap mem_heap;
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci   int mem_fd;
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_ci   int (**driver_ioctls)(int fd, unsigned long request, void *arg);
54bf215546Sopenharmony_ci   int driver_ioctl_count;
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci   void (*driver_bo_free)(struct shim_bo *bo);
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci   /* Returned by drmGetVersion(). */
59bf215546Sopenharmony_ci   const char *driver_name;
60bf215546Sopenharmony_ci
61bf215546Sopenharmony_ci   /* Returned by drmGetBusid(). */
62bf215546Sopenharmony_ci   const char *unique;
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_ci   int version_major, version_minor, version_patchlevel;
65bf215546Sopenharmony_ci   int bus_type;
66bf215546Sopenharmony_ci};
67bf215546Sopenharmony_ci
68bf215546Sopenharmony_ciextern struct shim_device shim_device;
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_cistruct shim_fd {
71bf215546Sopenharmony_ci   int fd;
72bf215546Sopenharmony_ci   int refcount;
73bf215546Sopenharmony_ci   mtx_t handle_lock;
74bf215546Sopenharmony_ci   /* mapping from int gem handle to struct shim_bo *. */
75bf215546Sopenharmony_ci   struct hash_table *handles;
76bf215546Sopenharmony_ci};
77bf215546Sopenharmony_ci
78bf215546Sopenharmony_cistruct shim_bo {
79bf215546Sopenharmony_ci   uint64_t mem_addr;
80bf215546Sopenharmony_ci   void *map;
81bf215546Sopenharmony_ci   int refcount;
82bf215546Sopenharmony_ci   uint32_t size;
83bf215546Sopenharmony_ci};
84bf215546Sopenharmony_ci
85bf215546Sopenharmony_ci/* Core support. */
86bf215546Sopenharmony_ciextern int render_node_minor;
87bf215546Sopenharmony_civoid drm_shim_device_init(void);
88bf215546Sopenharmony_civoid drm_shim_override_file(const char *contents,
89bf215546Sopenharmony_ci                            const char *path_format, ...) PRINTFLIKE(2, 3);
90bf215546Sopenharmony_civoid drm_shim_fd_register(int fd, struct shim_fd *shim_fd);
91bf215546Sopenharmony_civoid drm_shim_fd_unregister(int fd);
92bf215546Sopenharmony_cistruct shim_fd *drm_shim_fd_lookup(int fd);
93bf215546Sopenharmony_ciint drm_shim_ioctl(int fd, unsigned long request, void *arg);
94bf215546Sopenharmony_civoid *drm_shim_mmap(struct shim_fd *shim_fd, size_t length, int prot, int flags,
95bf215546Sopenharmony_ci                    int fd, off64_t offset);
96bf215546Sopenharmony_ci
97bf215546Sopenharmony_ciint drm_shim_bo_init(struct shim_bo *bo, size_t size);
98bf215546Sopenharmony_civoid drm_shim_bo_get(struct shim_bo *bo);
99bf215546Sopenharmony_civoid drm_shim_bo_put(struct shim_bo *bo);
100bf215546Sopenharmony_cistruct shim_bo *drm_shim_bo_lookup(struct shim_fd *shim_fd, int handle);
101bf215546Sopenharmony_ciint drm_shim_bo_get_handle(struct shim_fd *shim_fd, struct shim_bo *bo);
102bf215546Sopenharmony_ciuint64_t drm_shim_bo_get_mmap_offset(struct shim_fd *shim_fd,
103bf215546Sopenharmony_ci                                     struct shim_bo *bo);
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_ci/* driver-specific hooks. */
106bf215546Sopenharmony_civoid drm_shim_driver_init(void);
107bf215546Sopenharmony_ciextern bool drm_shim_driver_prefers_new_render_node;
108