pipe-loader: move dup(fd) within pipe_loader_drm_probe_fd
[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 .configuration = pipe_default_configuration_query,
73 },
74 {
75 .driver_name = "nouveau",
76 .create_screen = pipe_nouveau_create_screen,
77 .configuration = pipe_default_configuration_query,
78 },
79 {
80 .driver_name = "r300",
81 .create_screen = pipe_r300_create_screen,
82 .configuration = pipe_default_configuration_query,
83 },
84 {
85 .driver_name = "r600",
86 .create_screen = pipe_r600_create_screen,
87 .configuration = pipe_default_configuration_query,
88 },
89 {
90 .driver_name = "radeonsi",
91 .create_screen = pipe_radeonsi_create_screen,
92 .configuration = pipe_radeonsi_configuration_query,
93 },
94 {
95 .driver_name = "vmwgfx",
96 .create_screen = pipe_vmwgfx_create_screen,
97 .configuration = pipe_default_configuration_query,
98 },
99 {
100 .driver_name = "kgsl",
101 .create_screen = pipe_freedreno_create_screen,
102 .configuration = pipe_default_configuration_query,
103 },
104 {
105 .driver_name = "msm",
106 .create_screen = pipe_freedreno_create_screen,
107 .configuration = pipe_default_configuration_query,
108 },
109 {
110 .driver_name = "pl111",
111 .create_screen = pipe_pl111_create_screen,
112 .configuration = pipe_default_configuration_query,
113 },
114 {
115 .driver_name = "virtio_gpu",
116 .create_screen = pipe_virgl_create_screen,
117 .configuration = pipe_default_configuration_query,
118 },
119 {
120 .driver_name = "v3d",
121 .create_screen = pipe_v3d_create_screen,
122 .configuration = pipe_default_configuration_query,
123 },
124 {
125 .driver_name = "vc4",
126 .create_screen = pipe_vc4_create_screen,
127 .configuration = pipe_default_configuration_query,
128 },
129 {
130 .driver_name = "etnaviv",
131 .create_screen = pipe_etna_create_screen,
132 .configuration = pipe_default_configuration_query,
133 },
134 {
135 .driver_name = "imx-drm",
136 .create_screen = pipe_imx_drm_create_screen,
137 .configuration = pipe_default_configuration_query,
138 },
139 {
140 .driver_name = "tegra",
141 .create_screen = pipe_tegra_create_screen,
142 .configuration = pipe_default_configuration_query,
143 },
144 };
145 #endif
146
147 static const struct drm_driver_descriptor *
148 get_driver_descriptor(const char *driver_name, struct util_dl_library **plib)
149 {
150 #ifdef GALLIUM_STATIC_TARGETS
151 for (int i = 0; i < ARRAY_SIZE(driver_descriptors); i++) {
152 if (strcmp(driver_descriptors[i].driver_name, driver_name) == 0)
153 return &driver_descriptors[i];
154 }
155 #else
156 *plib = pipe_loader_find_module(driver_name, PIPE_SEARCH_DIR);
157 if (!*plib)
158 return NULL;
159
160 const struct drm_driver_descriptor *dd =
161 (const struct drm_driver_descriptor *)
162 util_dl_get_proc_address(*plib, "driver_descriptor");
163
164 /* sanity check on the driver name */
165 if (dd && strcmp(dd->driver_name, driver_name) == 0)
166 return dd;
167 #endif
168
169 return NULL;
170 }
171
172 static bool
173 pipe_loader_drm_probe_fd_nodup(struct pipe_loader_device **dev, int fd)
174 {
175 struct pipe_loader_drm_device *ddev = CALLOC_STRUCT(pipe_loader_drm_device);
176 int vendor_id, chip_id;
177
178 if (!ddev)
179 return false;
180
181 if (loader_get_pci_id_for_fd(fd, &vendor_id, &chip_id)) {
182 ddev->base.type = PIPE_LOADER_DEVICE_PCI;
183 ddev->base.u.pci.vendor_id = vendor_id;
184 ddev->base.u.pci.chip_id = chip_id;
185 } else {
186 ddev->base.type = PIPE_LOADER_DEVICE_PLATFORM;
187 }
188 ddev->base.ops = &pipe_loader_drm_ops;
189 ddev->fd = fd;
190
191 ddev->base.driver_name = loader_get_driver_for_fd(fd);
192 if (!ddev->base.driver_name)
193 goto fail;
194
195 struct util_dl_library **plib = NULL;
196 #ifndef GALLIUM_STATIC_TARGETS
197 plib = &ddev->lib;
198 #endif
199 ddev->dd = get_driver_descriptor(ddev->base.driver_name, plib);
200 if (!ddev->dd)
201 goto fail;
202
203 *dev = &ddev->base;
204 return true;
205
206 fail:
207 #ifndef GALLIUM_STATIC_TARGETS
208 if (ddev->lib)
209 util_dl_close(ddev->lib);
210 #endif
211 FREE(ddev->base.driver_name);
212 FREE(ddev);
213 return false;
214 }
215
216 bool
217 pipe_loader_drm_probe_fd(struct pipe_loader_device **dev, int fd)
218 {
219 bool ret;
220 int new_fd;
221
222 if (fd < 0 || (new_fd = fcntl(fd, F_DUPFD_CLOEXEC, 3)) < 0)
223 return false;
224
225 ret = pipe_loader_drm_probe_fd_nodup(dev, new_fd);
226 if (!ret)
227 close(new_fd);
228
229 return ret;
230 }
231
232 static int
233 open_drm_render_node_minor(int minor)
234 {
235 char path[PATH_MAX];
236 snprintf(path, sizeof(path), DRM_RENDER_NODE_DEV_NAME_FORMAT, DRM_DIR_NAME,
237 minor);
238 return loader_open_device(path);
239 }
240
241 int
242 pipe_loader_drm_probe(struct pipe_loader_device **devs, int ndev)
243 {
244 int i, j, fd;
245
246 for (i = DRM_RENDER_NODE_MIN_MINOR, j = 0;
247 i <= DRM_RENDER_NODE_MAX_MINOR; i++) {
248 struct pipe_loader_device *dev;
249
250 fd = open_drm_render_node_minor(i);
251 if (fd < 0)
252 continue;
253
254 if (!pipe_loader_drm_probe_fd_nodup(&dev, fd)) {
255 close(fd);
256 continue;
257 }
258
259 if (j < ndev) {
260 devs[j] = dev;
261 } else {
262 close(fd);
263 dev->ops->release(&dev);
264 }
265 j++;
266 }
267
268 return j;
269 }
270
271 static void
272 pipe_loader_drm_release(struct pipe_loader_device **dev)
273 {
274 struct pipe_loader_drm_device *ddev = pipe_loader_drm_device(*dev);
275
276 #ifndef GALLIUM_STATIC_TARGETS
277 if (ddev->lib)
278 util_dl_close(ddev->lib);
279 #endif
280
281 close(ddev->fd);
282 FREE(ddev->base.driver_name);
283 pipe_loader_base_release(dev);
284 }
285
286 static const struct drm_conf_ret *
287 pipe_loader_drm_configuration(struct pipe_loader_device *dev,
288 enum drm_conf conf)
289 {
290 struct pipe_loader_drm_device *ddev = pipe_loader_drm_device(dev);
291
292 if (!ddev->dd->configuration)
293 return NULL;
294
295 return ddev->dd->configuration(conf);
296 }
297
298 static struct pipe_screen *
299 pipe_loader_drm_create_screen(struct pipe_loader_device *dev,
300 const struct pipe_screen_config *config)
301 {
302 struct pipe_loader_drm_device *ddev = pipe_loader_drm_device(dev);
303
304 return ddev->dd->create_screen(ddev->fd, config);
305 }
306
307 char *
308 pipe_loader_drm_get_driinfo_xml(const char *driver_name)
309 {
310 char *xml = NULL;
311 struct util_dl_library *lib = NULL;
312 const struct drm_driver_descriptor *dd =
313 get_driver_descriptor(driver_name, &lib);
314 if (!dd)
315 goto out;
316
317 const struct drm_conf_ret *conf = dd->configuration(DRM_CONF_XML_OPTIONS);
318 if (!conf)
319 goto out;
320
321 xml = strdup((const char *)conf->val.val_pointer);
322
323 out:
324 if (lib)
325 util_dl_close(lib);
326 return xml;
327 }
328
329 static const struct pipe_loader_ops pipe_loader_drm_ops = {
330 .create_screen = pipe_loader_drm_create_screen,
331 .configuration = pipe_loader_drm_configuration,
332 .release = pipe_loader_drm_release
333 };