1/* 2 * Copyright (C) 2012-2018 Rob Clark <robclark@freedesktop.org> 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 FROM, 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 * SOFTWARE. 22 * 23 * Authors: 24 * Rob Clark <robclark@freedesktop.org> 25 */ 26 27#include <sys/types.h> 28#include <sys/stat.h> 29#include <unistd.h> 30 31#include "freedreno_drmif.h" 32#include "freedreno_priv.h" 33 34static pthread_mutex_t table_lock = PTHREAD_MUTEX_INITIALIZER; 35 36static uint32_t 37u32_hash(const void *key) 38{ 39 return _mesa_hash_data(key, sizeof(uint32_t)); 40} 41 42static bool 43u32_equals(const void *key1, const void *key2) 44{ 45 return *(const uint32_t *)key1 == *(const uint32_t *)key2; 46} 47 48 49struct fd_device * kgsl_device_new(int fd); 50struct fd_device * msm_device_new(int fd); 51 52struct fd_device * fd_device_new(int fd) 53{ 54 struct fd_device *dev; 55 drmVersionPtr version; 56 57 /* figure out if we are kgsl or msm drm driver: */ 58 version = drmGetVersion(fd); 59 if (!version) { 60 ERROR_MSG("cannot get version: %s", strerror(errno)); 61 return NULL; 62 } 63 64 if (!strcmp(version->name, "msm")) { 65 DEBUG_MSG("msm DRM device"); 66 if (version->version_major != 1) { 67 ERROR_MSG("unsupported version: %u.%u.%u", version->version_major, 68 version->version_minor, version->version_patchlevel); 69 dev = NULL; 70 goto out; 71 } 72 73 dev = msm_device_new(fd); 74 dev->version = version->version_minor; 75#if HAVE_FREEDRENO_KGSL 76 } else if (!strcmp(version->name, "kgsl")) { 77 DEBUG_MSG("kgsl DRM device"); 78 dev = kgsl_device_new(fd); 79#endif 80 } else { 81 ERROR_MSG("unknown device: %s", version->name); 82 dev = NULL; 83 } 84 85out: 86 drmFreeVersion(version); 87 88 if (!dev) 89 return NULL; 90 91 p_atomic_set(&dev->refcnt, 1); 92 dev->fd = fd; 93 dev->handle_table = _mesa_hash_table_create(NULL, u32_hash, u32_equals); 94 dev->name_table = _mesa_hash_table_create(NULL, u32_hash, u32_equals); 95 fd_bo_cache_init(&dev->bo_cache, FALSE); 96 fd_bo_cache_init(&dev->ring_cache, TRUE); 97 98 return dev; 99} 100 101/* like fd_device_new() but creates it's own private dup() of the fd 102 * which is close()d when the device is finalized. 103 */ 104struct fd_device * fd_device_new_dup(int fd) 105{ 106 int dup_fd = dup(fd); 107 struct fd_device *dev = fd_device_new(dup_fd); 108 if (dev) 109 dev->closefd = 1; 110 else 111 close(dup_fd); 112 return dev; 113} 114 115struct fd_device * fd_device_ref(struct fd_device *dev) 116{ 117 p_atomic_inc(&dev->refcnt); 118 return dev; 119} 120 121static void fd_device_del_impl(struct fd_device *dev) 122{ 123 int close_fd = dev->closefd ? dev->fd : -1; 124 fd_bo_cache_cleanup(&dev->bo_cache, 0); 125 _mesa_hash_table_destroy(dev->handle_table, NULL); 126 _mesa_hash_table_destroy(dev->name_table, NULL); 127 dev->funcs->destroy(dev); 128 if (close_fd >= 0) 129 close(close_fd); 130} 131 132void fd_device_del_locked(struct fd_device *dev) 133{ 134 if (!atomic_dec_and_test(&dev->refcnt)) 135 return; 136 fd_device_del_impl(dev); 137} 138 139void fd_device_del(struct fd_device *dev) 140{ 141 if (!atomic_dec_and_test(&dev->refcnt)) 142 return; 143 pthread_mutex_lock(&table_lock); 144 fd_device_del_impl(dev); 145 pthread_mutex_unlock(&table_lock); 146} 147 148int fd_device_fd(struct fd_device *dev) 149{ 150 return dev->fd; 151} 152 153enum fd_version fd_device_version(struct fd_device *dev) 154{ 155 return dev->version; 156} 157