• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #endif
140 
141 static const struct drm_driver_descriptor *
get_driver_descriptor(const char * driver_name,struct util_dl_library ** plib)142 get_driver_descriptor(const char *driver_name, struct util_dl_library **plib)
143 {
144 #ifdef GALLIUM_STATIC_TARGETS
145    for (int i = 0; i < ARRAY_SIZE(driver_descriptors); i++) {
146       if (strcmp(driver_descriptors[i].driver_name, driver_name) == 0)
147          return &driver_descriptors[i];
148    }
149 #else
150    *plib = pipe_loader_find_module(driver_name, PIPE_SEARCH_DIR);
151    if (!*plib)
152       return NULL;
153 
154    const struct drm_driver_descriptor *dd =
155          (const struct drm_driver_descriptor *)
156          util_dl_get_proc_address(*plib, "driver_descriptor");
157 
158    /* sanity check on the driver name */
159    if (dd && strcmp(dd->driver_name, driver_name) == 0)
160       return dd;
161 #endif
162 
163    return NULL;
164 }
165 
166 bool
pipe_loader_drm_probe_fd(struct pipe_loader_device ** dev,int fd)167 pipe_loader_drm_probe_fd(struct pipe_loader_device **dev, int fd)
168 {
169    struct pipe_loader_drm_device *ddev = CALLOC_STRUCT(pipe_loader_drm_device);
170    int vendor_id, chip_id;
171 
172    if (!ddev)
173       return false;
174 
175    if (loader_get_pci_id_for_fd(fd, &vendor_id, &chip_id)) {
176       ddev->base.type = PIPE_LOADER_DEVICE_PCI;
177       ddev->base.u.pci.vendor_id = vendor_id;
178       ddev->base.u.pci.chip_id = chip_id;
179    } else {
180       ddev->base.type = PIPE_LOADER_DEVICE_PLATFORM;
181    }
182    ddev->base.ops = &pipe_loader_drm_ops;
183    ddev->fd = fd;
184 
185    ddev->base.driver_name = loader_get_driver_for_fd(fd);
186    if (!ddev->base.driver_name)
187       goto fail;
188 
189    struct util_dl_library **plib = NULL;
190 #ifndef GALLIUM_STATIC_TARGETS
191    plib = &ddev->lib;
192 #endif
193    ddev->dd = get_driver_descriptor(ddev->base.driver_name, plib);
194    if (!ddev->dd)
195       goto fail;
196 
197    *dev = &ddev->base;
198    return true;
199 
200   fail:
201 #ifndef GALLIUM_STATIC_TARGETS
202    if (ddev->lib)
203       util_dl_close(ddev->lib);
204 #endif
205    FREE(ddev);
206    return false;
207 }
208 
209 static int
open_drm_render_node_minor(int minor)210 open_drm_render_node_minor(int minor)
211 {
212    char path[PATH_MAX];
213    snprintf(path, sizeof(path), DRM_RENDER_NODE_DEV_NAME_FORMAT, DRM_DIR_NAME,
214             minor);
215    return loader_open_device(path);
216 }
217 
218 int
pipe_loader_drm_probe(struct pipe_loader_device ** devs,int ndev)219 pipe_loader_drm_probe(struct pipe_loader_device **devs, int ndev)
220 {
221    int i, j, fd;
222 
223    for (i = DRM_RENDER_NODE_MIN_MINOR, j = 0;
224         i <= DRM_RENDER_NODE_MAX_MINOR; i++) {
225       struct pipe_loader_device *dev;
226 
227       fd = open_drm_render_node_minor(i);
228       if (fd < 0)
229          continue;
230 
231       if (!pipe_loader_drm_probe_fd(&dev, fd)) {
232          close(fd);
233          continue;
234       }
235 
236       if (j < ndev) {
237          devs[j] = dev;
238       } else {
239          close(fd);
240          dev->ops->release(&dev);
241       }
242       j++;
243    }
244 
245    return j;
246 }
247 
248 static void
pipe_loader_drm_release(struct pipe_loader_device ** dev)249 pipe_loader_drm_release(struct pipe_loader_device **dev)
250 {
251    struct pipe_loader_drm_device *ddev = pipe_loader_drm_device(*dev);
252 
253 #ifndef GALLIUM_STATIC_TARGETS
254    if (ddev->lib)
255       util_dl_close(ddev->lib);
256 #endif
257 
258    close(ddev->fd);
259    FREE(ddev->base.driver_name);
260    pipe_loader_base_release(dev);
261 }
262 
263 static const struct drm_conf_ret *
pipe_loader_drm_configuration(struct pipe_loader_device * dev,enum drm_conf conf)264 pipe_loader_drm_configuration(struct pipe_loader_device *dev,
265                               enum drm_conf conf)
266 {
267    struct pipe_loader_drm_device *ddev = pipe_loader_drm_device(dev);
268 
269    if (!ddev->dd->configuration)
270       return NULL;
271 
272    return ddev->dd->configuration(conf);
273 }
274 
275 static struct pipe_screen *
pipe_loader_drm_create_screen(struct pipe_loader_device * dev,const struct pipe_screen_config * config)276 pipe_loader_drm_create_screen(struct pipe_loader_device *dev,
277                               const struct pipe_screen_config *config)
278 {
279    struct pipe_loader_drm_device *ddev = pipe_loader_drm_device(dev);
280 
281    return ddev->dd->create_screen(ddev->fd, config);
282 }
283 
284 char *
pipe_loader_drm_get_driinfo_xml(const char * driver_name)285 pipe_loader_drm_get_driinfo_xml(const char *driver_name)
286 {
287    char *xml = NULL;
288    struct util_dl_library *lib = NULL;
289    const struct drm_driver_descriptor *dd =
290       get_driver_descriptor(driver_name, &lib);
291    if (!dd)
292       goto out;
293 
294    const struct drm_conf_ret *conf = dd->configuration(DRM_CONF_XML_OPTIONS);
295    if (!conf)
296       goto out;
297 
298    xml = strdup((const char *)conf->val.val_pointer);
299 
300 out:
301    if (lib)
302       util_dl_close(lib);
303    return xml;
304 }
305 
306 static const struct pipe_loader_ops pipe_loader_drm_ops = {
307    .create_screen = pipe_loader_drm_create_screen,
308    .configuration = pipe_loader_drm_configuration,
309    .release = pipe_loader_drm_release
310 };
311