917c7ef2f2510c78cd9ab792729341685782adc7
[mesa.git] / src / gallium / winsys / svga / drm / vmw_screen.c
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/u_memory.h"
32 #include "pipe/p_compiler.h"
33 #include "util/u_hash_table.h"
34 #ifdef MAJOR_IN_MKDEV
35 #include <sys/mkdev.h>
36 #endif
37 #ifdef MAJOR_IN_SYSMACROS
38 #include <sys/sysmacros.h>
39 #endif
40 #include <sys/stat.h>
41 #include <unistd.h>
42 #include <fcntl.h>
43
44 static struct hash_table *dev_hash = NULL;
45
46 static bool vmw_dev_compare(const void *key1, const void *key2)
47 {
48 return (major(*(dev_t *)key1) == major(*(dev_t *)key2) &&
49 minor(*(dev_t *)key1) == minor(*(dev_t *)key2));
50 }
51
52 static uint32_t vmw_dev_hash(const void *key)
53 {
54 return (major(*(dev_t *) key) << 16) | minor(*(dev_t *) key);
55 }
56
57 /* Called from vmw_drm_create_screen(), creates and initializes the
58 * vmw_winsys_screen structure, which is the main entity in this
59 * module.
60 * First, check whether a vmw_winsys_screen object already exists for
61 * this device, and in that case return that one, making sure that we
62 * have our own file descriptor open to DRM.
63 */
64
65 struct vmw_winsys_screen *
66 vmw_winsys_create( int fd )
67 {
68 struct vmw_winsys_screen *vws;
69 struct stat stat_buf;
70 const char *getenv_val;
71
72 if (dev_hash == NULL) {
73 dev_hash = _mesa_hash_table_create(NULL, vmw_dev_hash, vmw_dev_compare);
74 if (dev_hash == NULL)
75 return NULL;
76 }
77
78 if (fstat(fd, &stat_buf))
79 return NULL;
80
81 vws = util_hash_table_get(dev_hash, &stat_buf.st_rdev);
82 if (vws) {
83 vws->open_count++;
84 return vws;
85 }
86
87 vws = CALLOC_STRUCT(vmw_winsys_screen);
88 if (!vws)
89 goto out_no_vws;
90
91 vws->device = stat_buf.st_rdev;
92 vws->open_count = 1;
93 vws->ioctl.drm_fd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
94 vws->force_coherent = FALSE;
95 if (!vmw_ioctl_init(vws))
96 goto out_no_ioctl;
97
98 vws->base.have_gb_dma = !vws->force_coherent;
99 vws->base.need_to_rebind_resources = FALSE;
100 vws->base.have_transfer_from_buffer_cmd = vws->base.have_vgpu10;
101 vws->base.have_constant_buffer_offset_cmd = FALSE;
102 getenv_val = getenv("SVGA_FORCE_KERNEL_UNMAPS");
103 vws->cache_maps = !getenv_val || strcmp(getenv_val, "0") == 0;
104 vws->fence_ops = vmw_fence_ops_create(vws);
105 if (!vws->fence_ops)
106 goto out_no_fence_ops;
107
108 if(!vmw_pools_init(vws))
109 goto out_no_pools;
110
111 if (!vmw_winsys_screen_init_svga(vws))
112 goto out_no_svga;
113
114 _mesa_hash_table_insert(dev_hash, &vws->device, vws);
115
116 cnd_init(&vws->cs_cond);
117 mtx_init(&vws->cs_mutex, mtx_plain);
118
119 return vws;
120 out_no_svga:
121 vmw_pools_cleanup(vws);
122 out_no_pools:
123 vws->fence_ops->destroy(vws->fence_ops);
124 out_no_fence_ops:
125 vmw_ioctl_cleanup(vws);
126 out_no_ioctl:
127 close(vws->ioctl.drm_fd);
128 FREE(vws);
129 out_no_vws:
130 return NULL;
131 }
132
133 void
134 vmw_winsys_destroy(struct vmw_winsys_screen *vws)
135 {
136 if (--vws->open_count == 0) {
137 _mesa_hash_table_remove_key(dev_hash, &vws->device);
138 vmw_pools_cleanup(vws);
139 vws->fence_ops->destroy(vws->fence_ops);
140 vmw_ioctl_cleanup(vws);
141 close(vws->ioctl.drm_fd);
142 mtx_destroy(&vws->cs_mutex);
143 cnd_destroy(&vws->cs_cond);
144 FREE(vws);
145 }
146 }