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