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 #include "util/vma.h" 29 30 #include <xf86drm.h> 31 32 #ifdef __linux__ 33 #define DRM_MAJOR 226 34 #endif 35 36 typedef int (*ioctl_fn_t)(int fd, unsigned long request, void *arg); 37 38 struct shim_bo; 39 40 struct shim_device { 41 /* Mapping from int fd to struct shim_fd *. */ 42 struct hash_table *fd_map; 43 44 /* Mapping from mmap offset to shim_bo */ 45 struct hash_table_u64 *offset_map; 46 47 /* IOMEM region */ 48 struct { 49 off64_t start; 50 size_t size; 51 void *(*mmap)(size_t length, int prot, int flags, off64_t offset); 52 } iomem_region; 53 54 mtx_t mem_lock; 55 /* Heap from which shim_bo are allocated */ 56 struct util_vma_heap mem_heap; 57 58 int mem_fd; 59 60 int (**driver_ioctls)(int fd, unsigned long request, void *arg); 61 int driver_ioctl_count; 62 63 void (*driver_bo_free)(struct shim_bo *bo); 64 65 /* Returned by drmGetVersion(). */ 66 const char *driver_name; 67 68 /* Returned by drmGetBusid(). */ 69 const char *unique; 70 71 int version_major, version_minor, version_patchlevel; 72 int bus_type; 73 }; 74 75 extern struct shim_device shim_device; 76 77 struct shim_fd { 78 int fd; 79 int refcount; 80 mtx_t handle_lock; 81 /* mapping from int gem handle to struct shim_bo *. */ 82 struct hash_table *handles; 83 }; 84 85 struct shim_bo { 86 uint64_t mem_addr; 87 void *map; 88 int refcount; 89 uint32_t size; 90 }; 91 92 /* Core support. */ 93 extern int render_node_minor; 94 void drm_shim_device_init(void); 95 void drm_shim_override_file(const char *contents, 96 const char *path_format, ...) PRINTFLIKE(2, 3); 97 void drm_shim_fd_register(int fd, struct shim_fd *shim_fd); 98 void drm_shim_fd_unregister(int fd); 99 struct shim_fd *drm_shim_fd_lookup(int fd); 100 int drm_shim_ioctl(int fd, unsigned long request, void *arg); 101 void *drm_shim_mmap(struct shim_fd *shim_fd, size_t length, int prot, int flags, 102 int fd, off64_t offset); 103 104 int drm_shim_bo_init(struct shim_bo *bo, size_t size); 105 void drm_shim_bo_get(struct shim_bo *bo); 106 void drm_shim_bo_put(struct shim_bo *bo); 107 struct shim_bo *drm_shim_bo_lookup(struct shim_fd *shim_fd, int handle); 108 int drm_shim_bo_get_handle(struct shim_fd *shim_fd, struct shim_bo *bo); 109 uint64_t drm_shim_bo_get_mmap_offset(struct shim_fd *shim_fd, 110 struct shim_bo *bo); 111 void drm_shim_init_iomem_region(off64_t offset, size_t size, 112 void *(*mmap_handler)(size_t, int, int, off64_t)); 113 114 /* driver-specific hooks. */ 115 void drm_shim_driver_init(void); 116 extern bool drm_shim_driver_prefers_new_render_node; 117