Path: blob/21.2-virgl/src/gallium/winsys/svga/drm/vmw_screen.c
4573 views
/**********************************************************1* Copyright 2009-2015 VMware, Inc. All rights reserved.2*3* Permission is hereby granted, free of charge, to any person4* obtaining a copy of this software and associated documentation5* files (the "Software"), to deal in the Software without6* restriction, including without limitation the rights to use, copy,7* modify, merge, publish, distribute, sublicense, and/or sell copies8* of the Software, and to permit persons to whom the Software is9* furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice shall be12* included in all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,15* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF16* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND17* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS18* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN19* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN20* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*23**********************************************************/242526#include "vmw_screen.h"27#include "vmw_fence.h"28#include "vmw_context.h"2930#include "util/os_file.h"31#include "util/u_memory.h"32#include "pipe/p_compiler.h"33#include "util/u_hash_table.h"34#ifdef MAJOR_IN_MKDEV35#include <sys/mkdev.h>36#endif37#ifdef MAJOR_IN_SYSMACROS38#include <sys/sysmacros.h>39#endif40#include <sys/stat.h>41#include <unistd.h>42#include <fcntl.h>4344static struct hash_table *dev_hash = NULL;4546static bool vmw_dev_compare(const void *key1, const void *key2)47{48return (major(*(dev_t *)key1) == major(*(dev_t *)key2) &&49minor(*(dev_t *)key1) == minor(*(dev_t *)key2));50}5152static uint32_t vmw_dev_hash(const void *key)53{54return (major(*(dev_t *) key) << 16) | minor(*(dev_t *) key);55}5657/* Called from vmw_drm_create_screen(), creates and initializes the58* vmw_winsys_screen structure, which is the main entity in this59* module.60* First, check whether a vmw_winsys_screen object already exists for61* this device, and in that case return that one, making sure that we62* have our own file descriptor open to DRM.63*/6465struct vmw_winsys_screen *66vmw_winsys_create( int fd )67{68struct vmw_winsys_screen *vws;69struct stat stat_buf;70const char *getenv_val;7172if (dev_hash == NULL) {73dev_hash = _mesa_hash_table_create(NULL, vmw_dev_hash, vmw_dev_compare);74if (dev_hash == NULL)75return NULL;76}7778if (fstat(fd, &stat_buf))79return NULL;8081vws = util_hash_table_get(dev_hash, &stat_buf.st_rdev);82if (vws) {83vws->open_count++;84return vws;85}8687vws = CALLOC_STRUCT(vmw_winsys_screen);88if (!vws)89goto out_no_vws;9091vws->device = stat_buf.st_rdev;92vws->open_count = 1;93vws->ioctl.drm_fd = os_dupfd_cloexec(fd);94vws->force_coherent = FALSE;95if (!vmw_ioctl_init(vws))96goto out_no_ioctl;9798vws->base.have_gb_dma = !vws->force_coherent;99vws->base.need_to_rebind_resources = FALSE;100vws->base.have_transfer_from_buffer_cmd = vws->base.have_vgpu10;101vws->base.have_constant_buffer_offset_cmd = FALSE;102getenv_val = getenv("SVGA_FORCE_KERNEL_UNMAPS");103vws->cache_maps = !getenv_val || strcmp(getenv_val, "0") == 0;104vws->fence_ops = vmw_fence_ops_create(vws);105if (!vws->fence_ops)106goto out_no_fence_ops;107108if(!vmw_pools_init(vws))109goto out_no_pools;110111if (!vmw_winsys_screen_init_svga(vws))112goto out_no_svga;113114_mesa_hash_table_insert(dev_hash, &vws->device, vws);115116cnd_init(&vws->cs_cond);117mtx_init(&vws->cs_mutex, mtx_plain);118119return vws;120out_no_svga:121vmw_pools_cleanup(vws);122out_no_pools:123vws->fence_ops->destroy(vws->fence_ops);124out_no_fence_ops:125vmw_ioctl_cleanup(vws);126out_no_ioctl:127close(vws->ioctl.drm_fd);128FREE(vws);129out_no_vws:130return NULL;131}132133void134vmw_winsys_destroy(struct vmw_winsys_screen *vws)135{136if (--vws->open_count == 0) {137_mesa_hash_table_remove_key(dev_hash, &vws->device);138vmw_pools_cleanup(vws);139vws->fence_ops->destroy(vws->fence_ops);140vmw_ioctl_cleanup(vws);141close(vws->ioctl.drm_fd);142mtx_destroy(&vws->cs_mutex);143cnd_destroy(&vws->cs_cond);144FREE(vws);145}146}147148149