• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/os_file.h"
30 #include "util/u_hash_table.h"
31 #include "util/u_memory.h"
32 #include "util/u_pointer.h"
33 
34 #include "etnaviv/etnaviv_screen.h"
35 #include "etnaviv/hw/common.xml.h"
36 #include "etnaviv_drm_public.h"
37 
38 #include <stdio.h>
39 
hash_file_description(const void * key)40 static uint32_t hash_file_description(const void *key)
41 {
42    int fd = pointer_to_intptr(key);
43    struct stat stat;
44 
45    // File descriptions can't be hashed, but it should be safe to assume
46    // that the same file description will always refer to he same file
47    if(fstat(fd, &stat) == -1)
48       return ~0; // Make sure fstat failing won't result in a random hash
49 
50    return stat.st_dev ^ stat.st_ino ^ stat.st_rdev;
51 }
52 
53 
equal_file_description(const void * key1,const void * key2)54 static bool equal_file_description(const void *key1, const void *key2)
55 {
56    int ret;
57    int fd1 = pointer_to_intptr(key1);
58    int fd2 = pointer_to_intptr(key2);
59    struct stat stat1, stat2;
60 
61    // If the file descriptors are the same, the file description will be too
62    // This will also catch sentinels, such as -1
63    if (fd1 == fd2)
64       return true;
65 
66    ret = os_same_file_description(fd1, fd2);
67    if (ret >= 0)
68       return (ret == 0);
69 
70    {
71       static bool has_warned;
72       if (!has_warned)
73          fprintf(stderr, "os_same_file_description couldn't determine if "
74                  "two DRM fds reference the same file description. (%s)\n"
75                  "Let's just assume that file descriptors for the same file probably"
76                  "share the file description instead. This may cause problems when"
77                  "that isn't the case.\n", strerror(errno));
78       has_warned = true;
79    }
80 
81    // Let's at least check that it's the same file, different files can't
82    // have the same file descriptions
83    fstat(fd1, &stat1);
84    fstat(fd2, &stat2);
85 
86    return stat1.st_dev == stat2.st_dev &&
87           stat1.st_ino == stat2.st_ino &&
88           stat1.st_rdev == stat2.st_rdev;
89 }
90 
91 
92 static struct hash_table *
hash_table_create_file_description_keys(void)93 hash_table_create_file_description_keys(void)
94 {
95    return _mesa_hash_table_create(NULL, hash_file_description, equal_file_description);
96 }
97 
98 static struct pipe_screen *
screen_create(struct renderonly * ro)99 screen_create(struct renderonly *ro)
100 {
101    struct etna_device *dev;
102    struct etna_gpu *gpu;
103    uint64_t val;
104    int i;
105 
106    dev = etna_device_new_dup(ro->gpu_fd);
107    if (!dev) {
108       fprintf(stderr, "Error creating device\n");
109       return NULL;
110    }
111 
112    for (i = 0;; i++) {
113       gpu = etna_gpu_new(dev, i);
114       if (!gpu) {
115          fprintf(stderr, "Error creating gpu\n");
116          return NULL;
117       }
118 
119       /* Look for a 3D capable GPU */
120       int ret = etna_gpu_get_param(gpu, ETNA_GPU_FEATURES_0, &val);
121       if (ret == 0 && (val & chipFeatures_PIPE_3D))
122          break;
123 
124       etna_gpu_del(gpu);
125    }
126 
127    return etna_screen_create(dev, gpu, ro);
128 }
129 
130 static struct hash_table *etna_tab = NULL;
131 
132 static mtx_t etna_screen_mutex = _MTX_INITIALIZER_NP;
133 
134 static void
etna_drm_screen_destroy(struct pipe_screen * pscreen)135 etna_drm_screen_destroy(struct pipe_screen *pscreen)
136 {
137    struct etna_screen *screen = etna_screen(pscreen);
138    boolean destroy;
139 
140    mtx_lock(&etna_screen_mutex);
141    destroy = --screen->refcnt == 0;
142    if (destroy) {
143       int fd = etna_device_fd(screen->dev);
144       _mesa_hash_table_remove_key(etna_tab, intptr_to_pointer(fd));
145    }
146    mtx_unlock(&etna_screen_mutex);
147 
148    if (destroy) {
149       pscreen->destroy = screen->winsys_priv;
150       pscreen->destroy(pscreen);
151    }
152 }
153 
154 struct pipe_screen *
etna_drm_screen_create_renderonly(struct renderonly * ro)155 etna_drm_screen_create_renderonly(struct renderonly *ro)
156 {
157    struct pipe_screen *pscreen = NULL;
158 
159    mtx_lock(&etna_screen_mutex);
160    if (!etna_tab) {
161       etna_tab = hash_table_create_file_description_keys();
162       if (!etna_tab)
163          goto unlock;
164    }
165 
166    pscreen = util_hash_table_get(etna_tab, intptr_to_pointer(ro->gpu_fd));
167    if (pscreen) {
168       etna_screen(pscreen)->refcnt++;
169    } else {
170       pscreen = screen_create(ro);
171       if (pscreen) {
172          int fd = etna_device_fd(etna_screen(pscreen)->dev);
173          _mesa_hash_table_insert(etna_tab, intptr_to_pointer(fd), pscreen);
174 
175          /* Bit of a hack, to avoid circular linkage dependency,
176          * ie. pipe driver having to call in to winsys, we
177          * override the pipe drivers screen->destroy() */
178          etna_screen(pscreen)->winsys_priv = pscreen->destroy;
179       pscreen->destroy = etna_drm_screen_destroy;
180       }
181    }
182 
183 unlock:
184    mtx_unlock(&etna_screen_mutex);
185    return pscreen;
186 }
187 
188 struct pipe_screen *
etna_drm_screen_create(int fd)189 etna_drm_screen_create(int fd)
190 {
191    struct renderonly ro = {
192       .create_for_resource = renderonly_create_gpu_import_for_resource,
193       .kms_fd = -1,
194       .gpu_fd = fd
195    };
196 
197    return etna_drm_screen_create_renderonly(&ro);
198 }
199