1 /**********************************************************
2 * Copyright 2009-2015 VMware, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 **********************************************************/
25
26
27 #include "vmw_screen.h"
28 #include "vmw_fence.h"
29 #include "vmw_context.h"
30
31 #include "util/os_file.h"
32 #include "util/u_memory.h"
33 #include "pipe/p_compiler.h"
34 #include "util/u_hash_table.h"
35 #ifdef MAJOR_IN_MKDEV
36 #include <sys/mkdev.h>
37 #endif
38 #ifdef MAJOR_IN_SYSMACROS
39 #include <sys/sysmacros.h>
40 #endif
41 #include <sys/stat.h>
42 #include <unistd.h>
43 #include <fcntl.h>
44
45 static struct hash_table *dev_hash = NULL;
46
vmw_dev_compare(const void * key1,const void * key2)47 static bool vmw_dev_compare(const void *key1, const void *key2)
48 {
49 return (major(*(dev_t *)key1) == major(*(dev_t *)key2) &&
50 minor(*(dev_t *)key1) == minor(*(dev_t *)key2));
51 }
52
vmw_dev_hash(const void * key)53 static uint32_t vmw_dev_hash(const void *key)
54 {
55 return (major(*(dev_t *) key) << 16) | minor(*(dev_t *) key);
56 }
57
58 /* Called from vmw_drm_create_screen(), creates and initializes the
59 * vmw_winsys_screen structure, which is the main entity in this
60 * module.
61 * First, check whether a vmw_winsys_screen object already exists for
62 * this device, and in that case return that one, making sure that we
63 * have our own file descriptor open to DRM.
64 */
65
66 struct vmw_winsys_screen *
vmw_winsys_create(int fd)67 vmw_winsys_create( int fd )
68 {
69 struct vmw_winsys_screen *vws;
70 struct stat stat_buf;
71 const char *getenv_val;
72
73 if (dev_hash == NULL) {
74 dev_hash = _mesa_hash_table_create(NULL, vmw_dev_hash, vmw_dev_compare);
75 if (dev_hash == NULL)
76 return NULL;
77 }
78
79 if (fstat(fd, &stat_buf))
80 return NULL;
81
82 vws = util_hash_table_get(dev_hash, &stat_buf.st_rdev);
83 if (vws) {
84 vws->open_count++;
85 return vws;
86 }
87
88 vws = CALLOC_STRUCT(vmw_winsys_screen);
89 if (!vws)
90 goto out_no_vws;
91
92 vws->device = stat_buf.st_rdev;
93 vws->open_count = 1;
94 vws->ioctl.drm_fd = os_dupfd_cloexec(fd);
95 vws->force_coherent = FALSE;
96 if (!vmw_ioctl_init(vws))
97 goto out_no_ioctl;
98
99 vws->base.have_gb_dma = !vws->force_coherent;
100 vws->base.need_to_rebind_resources = FALSE;
101 vws->base.have_transfer_from_buffer_cmd = vws->base.have_vgpu10;
102 vws->base.have_constant_buffer_offset_cmd = FALSE;
103 getenv_val = getenv("SVGA_FORCE_KERNEL_UNMAPS");
104 vws->cache_maps = !getenv_val || strcmp(getenv_val, "0") == 0;
105 vws->fence_ops = vmw_fence_ops_create(vws);
106 if (!vws->fence_ops)
107 goto out_no_fence_ops;
108
109 if(!vmw_pools_init(vws))
110 goto out_no_pools;
111
112 if (!vmw_winsys_screen_init_svga(vws))
113 goto out_no_svga;
114
115 _mesa_hash_table_insert(dev_hash, &vws->device, vws);
116
117 cnd_init(&vws->cs_cond);
118 mtx_init(&vws->cs_mutex, mtx_plain);
119
120 return vws;
121 out_no_svga:
122 vmw_pools_cleanup(vws);
123 out_no_pools:
124 vws->fence_ops->destroy(vws->fence_ops);
125 out_no_fence_ops:
126 vmw_ioctl_cleanup(vws);
127 out_no_ioctl:
128 close(vws->ioctl.drm_fd);
129 FREE(vws);
130 out_no_vws:
131 return NULL;
132 }
133
134 void
vmw_winsys_destroy(struct vmw_winsys_screen * vws)135 vmw_winsys_destroy(struct vmw_winsys_screen *vws)
136 {
137 if (--vws->open_count == 0) {
138 _mesa_hash_table_remove_key(dev_hash, &vws->device);
139 vmw_pools_cleanup(vws);
140 vws->fence_ops->destroy(vws->fence_ops);
141 vmw_ioctl_cleanup(vws);
142 close(vws->ioctl.drm_fd);
143 mtx_destroy(&vws->cs_mutex);
144 cnd_destroy(&vws->cs_cond);
145 FREE(vws);
146 }
147 }
148