2
* Copyright (c) 2015 Etnaviv Project
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:
11
* The above copyright notice and this permission notice (including the
12
* next paragraph) shall be included in all copies or substantial portions
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.
24
* Christian Gmeiner <christian.gmeiner@gmail.com>
29
#include "util/os_file.h"
30
#include "util/u_hash_table.h"
31
#include "util/u_pointer.h"
33
#include "etnaviv/etnaviv_screen.h"
34
#include "etnaviv/hw/common.xml.h"
35
#include "etnaviv_drm_public.h"
39
static uint32_t hash_file_description(const void *key)
41
int fd = pointer_to_intptr(key);
44
// File descriptions can't be hashed, but it should be safe to assume
45
// that the same file description will always refer to he same file
46
if(fstat(fd, &stat) == -1)
47
return ~0; // Make sure fstat failing won't result in a random hash
49
return stat.st_dev ^ stat.st_ino ^ stat.st_rdev;
53
static bool equal_file_description(const void *key1, const void *key2)
56
int fd1 = pointer_to_intptr(key1);
57
int fd2 = pointer_to_intptr(key2);
58
struct stat stat1, stat2;
60
// If the file descriptors are the same, the file description will be too
61
// This will also catch sentinels, such as -1
65
ret = os_same_file_description(fd1, fd2);
70
static bool has_warned;
72
fprintf(stderr, "os_same_file_description couldn't determine if "
73
"two DRM fds reference the same file description. (%s)\n"
74
"Let's just assume that file descriptors for the same file probably"
75
"share the file description instead. This may cause problems when"
76
"that isn't the case.\n", strerror(errno));
80
// Let's at least check that it's the same file, different files can't
81
// have the same file descriptions
85
return stat1.st_dev == stat2.st_dev &&
86
stat1.st_ino == stat2.st_ino &&
87
stat1.st_rdev == stat2.st_rdev;
91
static struct hash_table *
92
hash_table_create_file_description_keys(void)
94
return _mesa_hash_table_create(NULL, hash_file_description, equal_file_description);
97
static struct pipe_screen *
98
screen_create(int gpu_fd, struct renderonly *ro)
100
struct etna_device *dev;
101
struct etna_gpu *gpu;
105
dev = etna_device_new_dup(gpu_fd);
107
fprintf(stderr, "Error creating device\n");
112
gpu = etna_gpu_new(dev, i);
114
fprintf(stderr, "Error creating gpu\n");
118
/* Look for a 3D capable GPU */
119
int ret = etna_gpu_get_param(gpu, ETNA_GPU_FEATURES_0, &val);
120
if (ret == 0 && (val & chipFeatures_PIPE_3D))
126
return etna_screen_create(dev, gpu, ro);
129
static struct hash_table *fd_tab = NULL;
131
static mtx_t etna_screen_mutex = _MTX_INITIALIZER_NP;
134
etna_drm_screen_destroy(struct pipe_screen *pscreen)
136
struct etna_screen *screen = etna_screen(pscreen);
139
mtx_lock(&etna_screen_mutex);
140
destroy = --screen->refcnt == 0;
142
int fd = etna_device_fd(screen->dev);
143
_mesa_hash_table_remove_key(fd_tab, intptr_to_pointer(fd));
145
if (!fd_tab->entries) {
146
_mesa_hash_table_destroy(fd_tab, NULL);
150
mtx_unlock(&etna_screen_mutex);
153
pscreen->destroy = screen->winsys_priv;
154
pscreen->destroy(pscreen);
158
static struct pipe_screen *
159
etna_lookup_or_create_screen(int gpu_fd, struct renderonly *ro)
161
struct pipe_screen *pscreen = NULL;
163
mtx_lock(&etna_screen_mutex);
165
fd_tab = hash_table_create_file_description_keys();
170
pscreen = util_hash_table_get(fd_tab, intptr_to_pointer(gpu_fd));
172
etna_screen(pscreen)->refcnt++;
174
pscreen = screen_create(gpu_fd, ro);
176
int fd = etna_device_fd(etna_screen(pscreen)->dev);
177
_mesa_hash_table_insert(fd_tab, intptr_to_pointer(fd), pscreen);
179
/* Bit of a hack, to avoid circular linkage dependency,
180
* ie. pipe driver having to call in to winsys, we
181
* override the pipe drivers screen->destroy() */
182
etna_screen(pscreen)->winsys_priv = pscreen->destroy;
183
pscreen->destroy = etna_drm_screen_destroy;
188
mtx_unlock(&etna_screen_mutex);
193
etna_drm_screen_create_renderonly(struct renderonly *ro)
195
return etna_lookup_or_create_screen(ro->gpu_fd, ro);
199
etna_drm_screen_create(int fd)
201
return etna_lookup_or_create_screen(fd, NULL);