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