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