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