tegra: Initial support
[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 = "vc4",
120 .create_screen = pipe_vc4_create_screen,
121 .configuration = pipe_default_configuration_query,
122 },
123 {
124 .driver_name = "vc5",
125 .create_screen = pipe_vc5_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);
211 return false;
212 }
213
214 static int
215 open_drm_render_node_minor(int minor)
216 {
217 char path[PATH_MAX];
218 snprintf(path, sizeof(path), DRM_RENDER_NODE_DEV_NAME_FORMAT, DRM_DIR_NAME,
219 minor);
220 return loader_open_device(path);
221 }
222
223 int
224 pipe_loader_drm_probe(struct pipe_loader_device **devs, int ndev)
225 {
226 int i, j, fd;
227
228 for (i = DRM_RENDER_NODE_MIN_MINOR, j = 0;
229 i <= DRM_RENDER_NODE_MAX_MINOR; i++) {
230 struct pipe_loader_device *dev;
231
232 fd = open_drm_render_node_minor(i);
233 if (fd < 0)
234 continue;
235
236 if (!pipe_loader_drm_probe_fd(&dev, fd)) {
237 close(fd);
238 continue;
239 }
240
241 if (j < ndev) {
242 devs[j] = dev;
243 } else {
244 close(fd);
245 dev->ops->release(&dev);
246 }
247 j++;
248 }
249
250 return j;
251 }
252
253 static void
254 pipe_loader_drm_release(struct pipe_loader_device **dev)
255 {
256 struct pipe_loader_drm_device *ddev = pipe_loader_drm_device(*dev);
257
258 #ifndef GALLIUM_STATIC_TARGETS
259 if (ddev->lib)
260 util_dl_close(ddev->lib);
261 #endif
262
263 close(ddev->fd);
264 FREE(ddev->base.driver_name);
265 pipe_loader_base_release(dev);
266 }
267
268 static const struct drm_conf_ret *
269 pipe_loader_drm_configuration(struct pipe_loader_device *dev,
270 enum drm_conf conf)
271 {
272 struct pipe_loader_drm_device *ddev = pipe_loader_drm_device(dev);
273
274 if (!ddev->dd->configuration)
275 return NULL;
276
277 return ddev->dd->configuration(conf);
278 }
279
280 static struct pipe_screen *
281 pipe_loader_drm_create_screen(struct pipe_loader_device *dev,
282 const struct pipe_screen_config *config)
283 {
284 struct pipe_loader_drm_device *ddev = pipe_loader_drm_device(dev);
285
286 return ddev->dd->create_screen(ddev->fd, config);
287 }
288
289 char *
290 pipe_loader_drm_get_driinfo_xml(const char *driver_name)
291 {
292 char *xml = NULL;
293 struct util_dl_library *lib = NULL;
294 const struct drm_driver_descriptor *dd =
295 get_driver_descriptor(driver_name, &lib);
296 if (!dd)
297 goto out;
298
299 const struct drm_conf_ret *conf = dd->configuration(DRM_CONF_XML_OPTIONS);
300 if (!conf)
301 goto out;
302
303 xml = strdup((const char *)conf->val.val_pointer);
304
305 out:
306 if (lib)
307 util_dl_close(lib);
308 return xml;
309 }
310
311 static const struct pipe_loader_ops pipe_loader_drm_ops = {
312 .create_screen = pipe_loader_drm_create_screen,
313 .configuration = pipe_loader_drm_configuration,
314 .release = pipe_loader_drm_release
315 };