gallium/hash_table: remove some function wrappers
[mesa.git] / src / gallium / winsys / etnaviv / drm / etnaviv_drm_winsys.c
1 /*
2 * Copyright (c) 2015 Etnaviv Project
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sub license,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the
12 * next paragraph) shall be included in all copies or substantial portions
13 * of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Christian Gmeiner <christian.gmeiner@gmail.com>
25 */
26
27 #include <sys/stat.h>
28
29 #include "util/u_hash_table.h"
30 #include "util/u_memory.h"
31
32 #include "etnaviv/etnaviv_screen.h"
33 #include "etnaviv/hw/common.xml.h"
34 #include "etnaviv_drm_public.h"
35
36 #include <stdio.h>
37
38 static struct pipe_screen *
39 screen_create(struct renderonly *ro)
40 {
41 struct etna_device *dev;
42 struct etna_gpu *gpu;
43 uint64_t val;
44 int i;
45
46 dev = etna_device_new_dup(ro->gpu_fd);
47 if (!dev) {
48 fprintf(stderr, "Error creating device\n");
49 return NULL;
50 }
51
52 for (i = 0;; i++) {
53 gpu = etna_gpu_new(dev, i);
54 if (!gpu) {
55 fprintf(stderr, "Error creating gpu\n");
56 return NULL;
57 }
58
59 /* Look for a 3D capable GPU */
60 int ret = etna_gpu_get_param(gpu, ETNA_GPU_FEATURES_0, &val);
61 if (ret == 0 && (val & chipFeatures_PIPE_3D))
62 break;
63
64 etna_gpu_del(gpu);
65 }
66
67 return etna_screen_create(dev, gpu, ro);
68 }
69
70 static struct hash_table *etna_tab = NULL;
71
72 static mtx_t etna_screen_mutex = _MTX_INITIALIZER_NP;
73
74 static void
75 etna_drm_screen_destroy(struct pipe_screen *pscreen)
76 {
77 struct etna_screen *screen = etna_screen(pscreen);
78 boolean destroy;
79
80 mtx_lock(&etna_screen_mutex);
81 destroy = --screen->refcnt == 0;
82 if (destroy) {
83 int fd = etna_device_fd(screen->dev);
84 _mesa_hash_table_remove_key(etna_tab, intptr_to_pointer(fd));
85 }
86 mtx_unlock(&etna_screen_mutex);
87
88 if (destroy) {
89 pscreen->destroy = screen->winsys_priv;
90 pscreen->destroy(pscreen);
91 }
92 }
93
94 struct pipe_screen *
95 etna_drm_screen_create_renderonly(struct renderonly *ro)
96 {
97 struct pipe_screen *pscreen = NULL;
98
99 mtx_lock(&etna_screen_mutex);
100 if (!etna_tab) {
101 etna_tab = util_hash_table_create_fd_keys();
102 if (!etna_tab)
103 goto unlock;
104 }
105
106 pscreen = util_hash_table_get(etna_tab, intptr_to_pointer(ro->gpu_fd));
107 if (pscreen) {
108 etna_screen(pscreen)->refcnt++;
109 } else {
110 pscreen = screen_create(ro);
111 if (pscreen) {
112 int fd = etna_device_fd(etna_screen(pscreen)->dev);
113 _mesa_hash_table_insert(etna_tab, intptr_to_pointer(fd), pscreen);
114
115 /* Bit of a hack, to avoid circular linkage dependency,
116 * ie. pipe driver having to call in to winsys, we
117 * override the pipe drivers screen->destroy() */
118 etna_screen(pscreen)->winsys_priv = pscreen->destroy;
119 pscreen->destroy = etna_drm_screen_destroy;
120 }
121 }
122
123 unlock:
124 mtx_unlock(&etna_screen_mutex);
125 return pscreen;
126 }
127
128 struct pipe_screen *
129 etna_drm_screen_create(int fd)
130 {
131 struct renderonly ro = {
132 .create_for_resource = renderonly_create_gpu_import_for_resource,
133 .kms_fd = -1,
134 .gpu_fd = fd
135 };
136
137 return etna_drm_screen_create_renderonly(&ro);
138 }