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