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