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