1 /**************************************************************************
2 *
3 * Copyright 2011 Intel Corporation
4 * Copyright 2012 Francisco Jerez
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 * Authors:
28 * Kristian Høgsberg <krh@bitplanet.net>
29 * Benjamin Franzke <benjaminfranzke@googlemail.com>
30 *
31 **************************************************************************/
32
33 #include <fcntl.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <xf86drm.h>
37 #include <unistd.h>
38 #include <fcntl.h>
39
40 #include "loader.h"
41 #include "target-helpers/drm_helper_public.h"
42 #include "frontend/drm_driver.h"
43 #include "pipe_loader_priv.h"
44
45 #include "util/os_file.h"
46 #include "util/u_memory.h"
47 #include "util/u_dl.h"
48 #include "util/u_debug.h"
49 #include "util/xmlconfig.h"
50
51 #define DRM_RENDER_NODE_DEV_NAME_FORMAT "%s/renderD%d"
52 #define DRM_RENDER_NODE_MAX_NODES 63
53 #define DRM_RENDER_NODE_MIN_MINOR 128
54 #define DRM_RENDER_NODE_MAX_MINOR (DRM_RENDER_NODE_MIN_MINOR + DRM_RENDER_NODE_MAX_NODES)
55
56 struct pipe_loader_drm_device {
57 struct pipe_loader_device base;
58 const struct drm_driver_descriptor *dd;
59 #ifndef GALLIUM_STATIC_TARGETS
60 struct util_dl_library *lib;
61 #endif
62 int fd;
63 };
64
65 #define pipe_loader_drm_device(dev) ((struct pipe_loader_drm_device *)dev)
66
67 static const struct pipe_loader_ops pipe_loader_drm_ops;
68
69 #ifdef GALLIUM_STATIC_TARGETS
70 static const struct drm_driver_descriptor *driver_descriptors[] = {
71 &i915_driver_descriptor,
72 &iris_driver_descriptor,
73 &nouveau_driver_descriptor,
74 &r300_driver_descriptor,
75 &r600_driver_descriptor,
76 &radeonsi_driver_descriptor,
77 &vmwgfx_driver_descriptor,
78 &kgsl_driver_descriptor,
79 &msm_driver_descriptor,
80 &virtio_gpu_driver_descriptor,
81 &v3d_driver_descriptor,
82 &vc4_driver_descriptor,
83 &panfrost_driver_descriptor,
84 &etnaviv_driver_descriptor,
85 &tegra_driver_descriptor,
86 &lima_driver_descriptor,
87 &zink_driver_descriptor,
88 };
89 #endif
90
91 static const struct drm_driver_descriptor *
get_driver_descriptor(const char * driver_name,struct util_dl_library ** plib)92 get_driver_descriptor(const char *driver_name, struct util_dl_library **plib)
93 {
94 #ifdef GALLIUM_STATIC_TARGETS
95 for (int i = 0; i < ARRAY_SIZE(driver_descriptors); i++) {
96 if (strcmp(driver_descriptors[i]->driver_name, driver_name) == 0)
97 return driver_descriptors[i];
98 }
99 return &kmsro_driver_descriptor;
100 #else
101 const char *search_dir = getenv("GALLIUM_PIPE_SEARCH_DIR");
102 if (search_dir == NULL)
103 search_dir = PIPE_SEARCH_DIR;
104
105 *plib = pipe_loader_find_module(driver_name, search_dir);
106 if (!*plib)
107 return NULL;
108
109 const struct drm_driver_descriptor *dd =
110 (const struct drm_driver_descriptor *)
111 util_dl_get_proc_address(*plib, "driver_descriptor");
112
113 /* sanity check on the driver name */
114 if (dd && strcmp(dd->driver_name, driver_name) == 0)
115 return dd;
116 #endif
117
118 return NULL;
119 }
120
121 static bool
pipe_loader_drm_probe_fd_nodup(struct pipe_loader_device ** dev,int fd)122 pipe_loader_drm_probe_fd_nodup(struct pipe_loader_device **dev, int fd)
123 {
124 struct pipe_loader_drm_device *ddev = CALLOC_STRUCT(pipe_loader_drm_device);
125 int vendor_id, chip_id;
126
127 if (!ddev)
128 return false;
129
130 if (loader_get_pci_id_for_fd(fd, &vendor_id, &chip_id)) {
131 ddev->base.type = PIPE_LOADER_DEVICE_PCI;
132 ddev->base.u.pci.vendor_id = vendor_id;
133 ddev->base.u.pci.chip_id = chip_id;
134 } else {
135 ddev->base.type = PIPE_LOADER_DEVICE_PLATFORM;
136 }
137 ddev->base.ops = &pipe_loader_drm_ops;
138 ddev->fd = fd;
139
140 ddev->base.driver_name = loader_get_driver_for_fd(fd);
141 if (!ddev->base.driver_name)
142 goto fail;
143
144 /* For the closed source AMD OpenGL driver, we want libgbm to load
145 * "amdgpu_dri.so", but we want Gallium multimedia drivers to load
146 * "radeonsi". So change amdgpu to radeonsi for Gallium.
147 */
148 if (strcmp(ddev->base.driver_name, "amdgpu") == 0) {
149 FREE(ddev->base.driver_name);
150 ddev->base.driver_name = strdup("radeonsi");
151 }
152
153 struct util_dl_library **plib = NULL;
154 #ifndef GALLIUM_STATIC_TARGETS
155 plib = &ddev->lib;
156 #endif
157 ddev->dd = get_driver_descriptor(ddev->base.driver_name, plib);
158
159 /* kmsro supports lots of drivers, try as a fallback */
160 if (!ddev->dd)
161 ddev->dd = get_driver_descriptor("kmsro", plib);
162
163 if (!ddev->dd)
164 goto fail;
165
166 *dev = &ddev->base;
167 return true;
168
169 fail:
170 #ifndef GALLIUM_STATIC_TARGETS
171 if (ddev->lib)
172 util_dl_close(ddev->lib);
173 #endif
174 FREE(ddev->base.driver_name);
175 FREE(ddev);
176 return false;
177 }
178
179 bool
pipe_loader_drm_probe_fd(struct pipe_loader_device ** dev,int fd)180 pipe_loader_drm_probe_fd(struct pipe_loader_device **dev, int fd)
181 {
182 bool ret;
183 int new_fd;
184
185 if (fd < 0 || (new_fd = os_dupfd_cloexec(fd)) < 0)
186 return false;
187
188 ret = pipe_loader_drm_probe_fd_nodup(dev, new_fd);
189 if (!ret)
190 close(new_fd);
191
192 return ret;
193 }
194
195 static int
open_drm_render_node_minor(int minor)196 open_drm_render_node_minor(int minor)
197 {
198 char path[PATH_MAX];
199 snprintf(path, sizeof(path), DRM_RENDER_NODE_DEV_NAME_FORMAT, DRM_DIR_NAME,
200 minor);
201 return loader_open_device(path);
202 }
203
204 int
pipe_loader_drm_probe(struct pipe_loader_device ** devs,int ndev)205 pipe_loader_drm_probe(struct pipe_loader_device **devs, int ndev)
206 {
207 int i, j, fd;
208
209 for (i = DRM_RENDER_NODE_MIN_MINOR, j = 0;
210 i <= DRM_RENDER_NODE_MAX_MINOR; i++) {
211 struct pipe_loader_device *dev;
212
213 fd = open_drm_render_node_minor(i);
214 if (fd < 0)
215 continue;
216
217 if (!pipe_loader_drm_probe_fd_nodup(&dev, fd)) {
218 close(fd);
219 continue;
220 }
221
222 if (j < ndev) {
223 devs[j] = dev;
224 } else {
225 close(fd);
226 dev->ops->release(&dev);
227 }
228 j++;
229 }
230
231 return j;
232 }
233
234 static void
pipe_loader_drm_release(struct pipe_loader_device ** dev)235 pipe_loader_drm_release(struct pipe_loader_device **dev)
236 {
237 struct pipe_loader_drm_device *ddev = pipe_loader_drm_device(*dev);
238
239 #ifndef GALLIUM_STATIC_TARGETS
240 if (ddev->lib)
241 util_dl_close(ddev->lib);
242 #endif
243
244 close(ddev->fd);
245 FREE(ddev->base.driver_name);
246 pipe_loader_base_release(dev);
247 }
248
249 static const struct driOptionDescription *
pipe_loader_drm_get_driconf(struct pipe_loader_device * dev,unsigned * count)250 pipe_loader_drm_get_driconf(struct pipe_loader_device *dev, unsigned *count)
251 {
252 struct pipe_loader_drm_device *ddev = pipe_loader_drm_device(dev);
253
254 *count = ddev->dd->driconf_count;
255 return ddev->dd->driconf;
256 }
257
258 static struct pipe_screen *
pipe_loader_drm_create_screen(struct pipe_loader_device * dev,const struct pipe_screen_config * config)259 pipe_loader_drm_create_screen(struct pipe_loader_device *dev,
260 const struct pipe_screen_config *config)
261 {
262 struct pipe_loader_drm_device *ddev = pipe_loader_drm_device(dev);
263
264 return ddev->dd->create_screen(ddev->fd, config);
265 }
266
267 const struct driOptionDescription *
pipe_loader_drm_get_driconf_by_name(const char * driver_name,unsigned * count)268 pipe_loader_drm_get_driconf_by_name(const char *driver_name, unsigned *count)
269 {
270 driOptionDescription *driconf = NULL;
271 struct util_dl_library *lib = NULL;
272 const struct drm_driver_descriptor *dd =
273 get_driver_descriptor(driver_name, &lib);
274
275 if (!dd) {
276 *count = 0;
277 } else {
278 *count = dd->driconf_count;
279 size_t size = sizeof(*driconf) * *count;
280 driconf = malloc(size);
281 memcpy(driconf, dd->driconf, size);
282 }
283 if (lib)
284 util_dl_close(lib);
285
286 return driconf;
287 }
288
289 static const struct pipe_loader_ops pipe_loader_drm_ops = {
290 .create_screen = pipe_loader_drm_create_screen,
291 .get_driconf = pipe_loader_drm_get_driconf,
292 .release = pipe_loader_drm_release
293 };
294