freedreno_device.c revision 857b0bc6
1/* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */ 2 3/* 4 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org> 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice (including the next 14 * paragraph) shall be included in all copies or substantial portions of the 15 * Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 * SOFTWARE. 24 * 25 * Authors: 26 * Rob Clark <robclark@freedesktop.org> 27 */ 28 29#include <sys/types.h> 30#include <sys/stat.h> 31#include <unistd.h> 32 33#include "freedreno_drmif.h" 34#include "freedreno_priv.h" 35 36static pthread_mutex_t table_lock = PTHREAD_MUTEX_INITIALIZER; 37 38struct fd_device * kgsl_device_new(int fd); 39struct fd_device * msm_device_new(int fd); 40 41static void 42add_bucket(struct fd_device *dev, int size) 43{ 44 unsigned int i = dev->num_buckets; 45 46 assert(i < ARRAY_SIZE(dev->cache_bucket)); 47 48 list_inithead(&dev->cache_bucket[i].list); 49 dev->cache_bucket[i].size = size; 50 dev->num_buckets++; 51} 52 53static void 54init_cache_buckets(struct fd_device *dev) 55{ 56 unsigned long size, cache_max_size = 64 * 1024 * 1024; 57 58 /* OK, so power of two buckets was too wasteful of memory. 59 * Give 3 other sizes between each power of two, to hopefully 60 * cover things accurately enough. (The alternative is 61 * probably to just go for exact matching of sizes, and assume 62 * that for things like composited window resize the tiled 63 * width/height alignment and rounding of sizes to pages will 64 * get us useful cache hit rates anyway) 65 */ 66 add_bucket(dev, 4096); 67 add_bucket(dev, 4096 * 2); 68 add_bucket(dev, 4096 * 3); 69 70 /* Initialize the linked lists for BO reuse cache. */ 71 for (size = 4 * 4096; size <= cache_max_size; size *= 2) { 72 add_bucket(dev, size); 73 add_bucket(dev, size + size * 1 / 4); 74 add_bucket(dev, size + size * 2 / 4); 75 add_bucket(dev, size + size * 3 / 4); 76 } 77} 78 79struct fd_device * fd_device_new(int fd) 80{ 81 struct fd_device *dev; 82 drmVersionPtr version; 83 84 /* figure out if we are kgsl or msm drm driver: */ 85 version = drmGetVersion(fd); 86 if (!version) { 87 ERROR_MSG("cannot get version: %s", strerror(errno)); 88 return NULL; 89 } 90 91 if (!strcmp(version->name, "kgsl")) { 92 DEBUG_MSG("kgsl DRM device"); 93 dev = kgsl_device_new(fd); 94 } else if (!strcmp(version->name, "msm")) { 95 DEBUG_MSG("msm DRM device"); 96 dev = msm_device_new(fd); 97 } else { 98 ERROR_MSG("unknown device: %s", version->name); 99 dev = NULL; 100 } 101 drmFreeVersion(version); 102 103 if (!dev) 104 return NULL; 105 106 atomic_set(&dev->refcnt, 1); 107 dev->fd = fd; 108 dev->handle_table = drmHashCreate(); 109 dev->name_table = drmHashCreate(); 110 init_cache_buckets(dev); 111 112 return dev; 113} 114 115/* like fd_device_new() but creates it's own private dup() of the fd 116 * which is close()d when the device is finalized. 117 */ 118struct fd_device * fd_device_new_dup(int fd) 119{ 120 struct fd_device *dev = fd_device_new(dup(fd)); 121 if (dev) 122 dev->closefd = 1; 123 return dev; 124} 125 126struct fd_device * fd_device_ref(struct fd_device *dev) 127{ 128 atomic_inc(&dev->refcnt); 129 return dev; 130} 131 132static void fd_device_del_impl(struct fd_device *dev) 133{ 134 fd_cleanup_bo_cache(dev, 0); 135 drmHashDestroy(dev->handle_table); 136 drmHashDestroy(dev->name_table); 137 if (dev->closefd) 138 close(dev->fd); 139 dev->funcs->destroy(dev); 140} 141 142void fd_device_del_locked(struct fd_device *dev) 143{ 144 if (!atomic_dec_and_test(&dev->refcnt)) 145 return; 146 fd_device_del_impl(dev); 147} 148 149void fd_device_del(struct fd_device *dev) 150{ 151 if (!atomic_dec_and_test(&dev->refcnt)) 152 return; 153 pthread_mutex_lock(&table_lock); 154 fd_device_del_impl(dev); 155 pthread_mutex_unlock(&table_lock); 156} 157