0c343cc7bdd47b601ba9e1820e361c771c422b99
[mesa.git] / src / gallium / winsys / svga / drm / vmw_screen.c
1 /**********************************************************
2 * Copyright 2009 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 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <unistd.h>
37
38 static struct util_hash_table *dev_hash = NULL;
39
40 static int vmw_dev_compare(void *key1, void *key2)
41 {
42 return (major(*(dev_t *)key1) == major(*(dev_t *)key2) &&
43 minor(*(dev_t *)key1) == minor(*(dev_t *)key2)) ? 0 : 1;
44 }
45
46 static unsigned vmw_dev_hash(void *key)
47 {
48 return (major(*(dev_t *) key) << 16) | minor(*(dev_t *) key);
49 }
50
51 /* Called from vmw_drm_create_screen(), creates and initializes the
52 * vmw_winsys_screen structure, which is the main entity in this
53 * module.
54 * First, check whether a vmw_winsys_screen object already exists for
55 * this device, and in that case return that one, making sure that we
56 * have our own file descriptor open to DRM.
57 */
58
59 struct vmw_winsys_screen *
60 vmw_winsys_create( int fd, boolean use_old_scanout_flag )
61 {
62 struct vmw_winsys_screen *vws;
63 struct stat stat_buf;
64
65 if (dev_hash == NULL) {
66 dev_hash = util_hash_table_create(vmw_dev_hash, vmw_dev_compare);
67 if (dev_hash == NULL)
68 return NULL;
69 }
70
71 if (fstat(fd, &stat_buf))
72 return NULL;
73
74 vws = util_hash_table_get(dev_hash, &stat_buf.st_rdev);
75 if (vws) {
76 vws->open_count++;
77 return vws;
78 }
79
80 vws = CALLOC_STRUCT(vmw_winsys_screen);
81 if (!vws)
82 goto out_no_vws;
83
84 vws->device = stat_buf.st_rdev;
85 vws->open_count = 1;
86 vws->ioctl.drm_fd = dup(fd);
87 vws->use_old_scanout_flag = use_old_scanout_flag;
88 vws->base.have_gb_dma = TRUE;
89
90 if (!vmw_ioctl_init(vws))
91 goto out_no_ioctl;
92
93 vws->fence_ops = vmw_fence_ops_create(vws);
94 if (!vws->fence_ops)
95 goto out_no_fence_ops;
96
97 if(!vmw_pools_init(vws))
98 goto out_no_pools;
99
100 if (!vmw_winsys_screen_init_svga(vws))
101 goto out_no_svga;
102
103 if (util_hash_table_set(dev_hash, &vws->device, vws) != PIPE_OK)
104 goto out_no_hash_insert;
105
106 return vws;
107 out_no_hash_insert:
108 out_no_svga:
109 vmw_pools_cleanup(vws);
110 out_no_pools:
111 vws->fence_ops->destroy(vws->fence_ops);
112 out_no_fence_ops:
113 vmw_ioctl_cleanup(vws);
114 out_no_ioctl:
115 close(vws->ioctl.drm_fd);
116 FREE(vws);
117 out_no_vws:
118 return NULL;
119 }
120
121 void
122 vmw_winsys_destroy(struct vmw_winsys_screen *vws)
123 {
124 if (--vws->open_count == 0) {
125 util_hash_table_remove(dev_hash, &vws->device);
126 vmw_pools_cleanup(vws);
127 vws->fence_ops->destroy(vws->fence_ops);
128 vmw_ioctl_cleanup(vws);
129 close(vws->ioctl.drm_fd);
130 FREE(vws);
131 }
132 }