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