pipe-loader: Default to kmsro if probe fails
[mesa.git] / src / gallium / auxiliary / pipe-loader / pipe_loader_drm.c
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 "state_tracker/drm_driver.h"
43 #include "pipe_loader_priv.h"
44
45 #include "util/u_memory.h"
46 #include "util/u_dl.h"
47 #include "util/u_debug.h"
48
49 #define DRM_RENDER_NODE_DEV_NAME_FORMAT "%s/renderD%d"
50 #define DRM_RENDER_NODE_MAX_NODES 63
51 #define DRM_RENDER_NODE_MIN_MINOR 128
52 #define DRM_RENDER_NODE_MAX_MINOR (DRM_RENDER_NODE_MIN_MINOR + DRM_RENDER_NODE_MAX_NODES)
53
54 struct pipe_loader_drm_device {
55 struct pipe_loader_device base;
56 const struct drm_driver_descriptor *dd;
57 #ifndef GALLIUM_STATIC_TARGETS
58 struct util_dl_library *lib;
59 #endif
60 int fd;
61 };
62
63 #define pipe_loader_drm_device(dev) ((struct pipe_loader_drm_device *)dev)
64
65 static const struct pipe_loader_ops pipe_loader_drm_ops;
66
67 #ifdef GALLIUM_STATIC_TARGETS
68 static const struct drm_driver_descriptor driver_descriptors[] = {
69 {
70 .driver_name = "i915",
71 .create_screen = pipe_i915_create_screen,
72 },
73 {
74 .driver_name = "iris",
75 .create_screen = pipe_iris_create_screen,
76 .driconf_xml = &iris_driconf_xml,
77 },
78 {
79 .driver_name = "nouveau",
80 .create_screen = pipe_nouveau_create_screen,
81 },
82 {
83 .driver_name = "r300",
84 .create_screen = pipe_r300_create_screen,
85 },
86 {
87 .driver_name = "r600",
88 .create_screen = pipe_r600_create_screen,
89 },
90 {
91 .driver_name = "radeonsi",
92 .create_screen = pipe_radeonsi_create_screen,
93 .driconf_xml = &radeonsi_driconf_xml,
94 },
95 {
96 .driver_name = "vmwgfx",
97 .create_screen = pipe_vmwgfx_create_screen,
98 },
99 {
100 .driver_name = "kgsl",
101 .create_screen = pipe_freedreno_create_screen,
102 },
103 {
104 .driver_name = "msm",
105 .create_screen = pipe_freedreno_create_screen,
106 },
107 {
108 .driver_name = "virtio_gpu",
109 .create_screen = pipe_virgl_create_screen,
110 .driconf_xml = &virgl_driconf_xml,
111 },
112 {
113 .driver_name = "v3d",
114 .create_screen = pipe_v3d_create_screen,
115 .driconf_xml = &v3d_driconf_xml,
116 },
117 {
118 .driver_name = "vc4",
119 .create_screen = pipe_vc4_create_screen,
120 .driconf_xml = &v3d_driconf_xml,
121 },
122 {
123 .driver_name = "panfrost",
124 .create_screen = pipe_panfrost_create_screen,
125 },
126 {
127 .driver_name = "etnaviv",
128 .create_screen = pipe_etna_create_screen,
129 },
130 {
131 .driver_name = "tegra",
132 .create_screen = pipe_tegra_create_screen,
133 },
134 {
135 .driver_name = "lima",
136 .create_screen = pipe_lima_create_screen,
137 },
138 };
139
140 static const struct drm_driver_descriptor default_driver_descriptor = {
141 .driver_name = "kmsro",
142 .create_screen = pipe_kmsro_create_screen,
143 .driconf_xml = &v3d_driconf_xml,
144 };
145
146 #endif
147
148 static const struct drm_driver_descriptor *
149 get_driver_descriptor(const char *driver_name, struct util_dl_library **plib)
150 {
151 #ifdef GALLIUM_STATIC_TARGETS
152 for (int i = 0; i < ARRAY_SIZE(driver_descriptors); i++) {
153 if (strcmp(driver_descriptors[i].driver_name, driver_name) == 0)
154 return &driver_descriptors[i];
155 }
156 return &default_driver_descriptor;
157 #else
158 *plib = pipe_loader_find_module(driver_name, PIPE_SEARCH_DIR);
159 if (!*plib)
160 return NULL;
161
162 const struct drm_driver_descriptor *dd =
163 (const struct drm_driver_descriptor *)
164 util_dl_get_proc_address(*plib, "driver_descriptor");
165
166 /* sanity check on the driver name */
167 if (dd && strcmp(dd->driver_name, driver_name) == 0)
168 return dd;
169 #endif
170
171 return NULL;
172 }
173
174 static bool
175 pipe_loader_drm_probe_fd_nodup(struct pipe_loader_device **dev, int fd)
176 {
177 struct pipe_loader_drm_device *ddev = CALLOC_STRUCT(pipe_loader_drm_device);
178 int vendor_id, chip_id;
179
180 if (!ddev)
181 return false;
182
183 if (loader_get_pci_id_for_fd(fd, &vendor_id, &chip_id)) {
184 ddev->base.type = PIPE_LOADER_DEVICE_PCI;
185 ddev->base.u.pci.vendor_id = vendor_id;
186 ddev->base.u.pci.chip_id = chip_id;
187 } else {
188 ddev->base.type = PIPE_LOADER_DEVICE_PLATFORM;
189 }
190 ddev->base.ops = &pipe_loader_drm_ops;
191 ddev->fd = fd;
192
193 ddev->base.driver_name = loader_get_driver_for_fd(fd);
194 if (!ddev->base.driver_name)
195 goto fail;
196
197 /* For the closed source AMD OpenGL driver, we want libgbm to load
198 * "amdgpu_dri.so", but we want Gallium multimedia drivers to load
199 * "radeonsi". So change amdgpu to radeonsi for Gallium.
200 */
201 if (strcmp(ddev->base.driver_name, "amdgpu") == 0) {
202 FREE(ddev->base.driver_name);
203 ddev->base.driver_name = strdup("radeonsi");
204 }
205
206 struct util_dl_library **plib = NULL;
207 #ifndef GALLIUM_STATIC_TARGETS
208 plib = &ddev->lib;
209 #endif
210 ddev->dd = get_driver_descriptor(ddev->base.driver_name, plib);
211
212 /* kmsro supports lots of drivers, try as a fallback */
213 if (!ddev->dd)
214 ddev->dd = get_driver_descriptor("kmsro", plib);
215
216 if (!ddev->dd)
217 goto fail;
218
219 *dev = &ddev->base;
220 return true;
221
222 fail:
223 #ifndef GALLIUM_STATIC_TARGETS
224 if (ddev->lib)
225 util_dl_close(ddev->lib);
226 #endif
227 FREE(ddev->base.driver_name);
228 FREE(ddev);
229 return false;
230 }
231
232 bool
233 pipe_loader_drm_probe_fd(struct pipe_loader_device **dev, int fd)
234 {
235 bool ret;
236 int new_fd;
237
238 if (fd < 0 || (new_fd = fcntl(fd, F_DUPFD_CLOEXEC, 3)) < 0)
239 return false;
240
241 ret = pipe_loader_drm_probe_fd_nodup(dev, new_fd);
242 if (!ret)
243 close(new_fd);
244
245 return ret;
246 }
247
248 static int
249 open_drm_render_node_minor(int minor)
250 {
251 char path[PATH_MAX];
252 snprintf(path, sizeof(path), DRM_RENDER_NODE_DEV_NAME_FORMAT, DRM_DIR_NAME,
253 minor);
254 return loader_open_device(path);
255 }
256
257 int
258 pipe_loader_drm_probe(struct pipe_loader_device **devs, int ndev)
259 {
260 int i, j, fd;
261
262 for (i = DRM_RENDER_NODE_MIN_MINOR, j = 0;
263 i <= DRM_RENDER_NODE_MAX_MINOR; i++) {
264 struct pipe_loader_device *dev;
265
266 fd = open_drm_render_node_minor(i);
267 if (fd < 0)
268 continue;
269
270 if (!pipe_loader_drm_probe_fd_nodup(&dev, fd)) {
271 close(fd);
272 continue;
273 }
274
275 if (j < ndev) {
276 devs[j] = dev;
277 } else {
278 close(fd);
279 dev->ops->release(&dev);
280 }
281 j++;
282 }
283
284 return j;
285 }
286
287 static void
288 pipe_loader_drm_release(struct pipe_loader_device **dev)
289 {
290 struct pipe_loader_drm_device *ddev = pipe_loader_drm_device(*dev);
291
292 #ifndef GALLIUM_STATIC_TARGETS
293 if (ddev->lib)
294 util_dl_close(ddev->lib);
295 #endif
296
297 close(ddev->fd);
298 FREE(ddev->base.driver_name);
299 pipe_loader_base_release(dev);
300 }
301
302 static const char *
303 pipe_loader_drm_get_driconf_xml(struct pipe_loader_device *dev)
304 {
305 struct pipe_loader_drm_device *ddev = pipe_loader_drm_device(dev);
306
307 if (!ddev->dd->driconf_xml)
308 return NULL;
309
310 return *ddev->dd->driconf_xml;
311 }
312
313 static struct pipe_screen *
314 pipe_loader_drm_create_screen(struct pipe_loader_device *dev,
315 const struct pipe_screen_config *config)
316 {
317 struct pipe_loader_drm_device *ddev = pipe_loader_drm_device(dev);
318
319 return ddev->dd->create_screen(ddev->fd, config);
320 }
321
322 char *
323 pipe_loader_drm_get_driinfo_xml(const char *driver_name)
324 {
325 char *xml = NULL;
326 struct util_dl_library *lib = NULL;
327 const struct drm_driver_descriptor *dd =
328 get_driver_descriptor(driver_name, &lib);
329
330 if (dd && dd->driconf_xml)
331 xml = strdup(*dd->driconf_xml);
332
333 if (lib)
334 util_dl_close(lib);
335 return xml;
336 }
337
338 static const struct pipe_loader_ops pipe_loader_drm_ops = {
339 .create_screen = pipe_loader_drm_create_screen,
340 .get_driconf_xml = pipe_loader_drm_get_driconf_xml,
341 .release = pipe_loader_drm_release
342 };