• 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 #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/os_file.h"
46 #include "util/u_memory.h"
47 #include "util/u_dl.h"
48 #include "util/u_debug.h"
49 #include "util/xmlconfig.h"
50 
51 #define DRM_RENDER_NODE_DEV_NAME_FORMAT "%s/renderD%d"
52 #define DRM_RENDER_NODE_MAX_NODES 63
53 #define DRM_RENDER_NODE_MIN_MINOR 128
54 #define DRM_RENDER_NODE_MAX_MINOR (DRM_RENDER_NODE_MIN_MINOR + DRM_RENDER_NODE_MAX_NODES)
55 
56 struct pipe_loader_drm_device {
57    struct pipe_loader_device base;
58    const struct drm_driver_descriptor *dd;
59 #ifndef GALLIUM_STATIC_TARGETS
60    struct util_dl_library *lib;
61 #endif
62    int fd;
63 };
64 
65 #define pipe_loader_drm_device(dev) ((struct pipe_loader_drm_device *)dev)
66 
67 static const struct pipe_loader_ops pipe_loader_drm_ops;
68 
69 #ifdef GALLIUM_STATIC_TARGETS
70 static const struct drm_driver_descriptor *driver_descriptors[] = {
71    &i915_driver_descriptor,
72    &iris_driver_descriptor,
73    &crocus_driver_descriptor,
74    &nouveau_driver_descriptor,
75    &r300_driver_descriptor,
76    &r600_driver_descriptor,
77    &radeonsi_driver_descriptor,
78    &vmwgfx_driver_descriptor,
79    &kgsl_driver_descriptor,
80    &msm_driver_descriptor,
81    &virtio_gpu_driver_descriptor,
82    &v3d_driver_descriptor,
83    &vc4_driver_descriptor,
84    &panfrost_driver_descriptor,
85    &asahi_driver_descriptor,
86    &etnaviv_driver_descriptor,
87    &tegra_driver_descriptor,
88    &lima_driver_descriptor,
89    &zink_driver_descriptor,
90 };
91 #endif
92 
93 static const struct drm_driver_descriptor *
get_driver_descriptor(const char * driver_name,struct util_dl_library ** plib)94 get_driver_descriptor(const char *driver_name, struct util_dl_library **plib)
95 {
96 #ifdef GALLIUM_STATIC_TARGETS
97    for (int i = 0; i < ARRAY_SIZE(driver_descriptors); i++) {
98       if (strcmp(driver_descriptors[i]->driver_name, driver_name) == 0)
99          return driver_descriptors[i];
100    }
101    return &kmsro_driver_descriptor;
102 #else
103    const char *search_dir = getenv("GALLIUM_PIPE_SEARCH_DIR");
104    if (search_dir == NULL)
105       search_dir = PIPE_SEARCH_DIR;
106 
107    *plib = pipe_loader_find_module(driver_name, search_dir);
108    if (!*plib)
109       return NULL;
110 
111    const struct drm_driver_descriptor *dd =
112          (const struct drm_driver_descriptor *)
113          util_dl_get_proc_address(*plib, "driver_descriptor");
114 
115    /* sanity check on the driver name */
116    if (dd && strcmp(dd->driver_name, driver_name) == 0)
117       return dd;
118 #endif
119 
120    return NULL;
121 }
122 
123 static bool
pipe_loader_drm_probe_fd_nodup(struct pipe_loader_device ** dev,int fd,bool zink)124 pipe_loader_drm_probe_fd_nodup(struct pipe_loader_device **dev, int fd, bool zink)
125 {
126    struct pipe_loader_drm_device *ddev = CALLOC_STRUCT(pipe_loader_drm_device);
127    int vendor_id, chip_id;
128 
129    if (!ddev)
130       return false;
131 
132    if (loader_get_pci_id_for_fd(fd, &vendor_id, &chip_id)) {
133       ddev->base.type = PIPE_LOADER_DEVICE_PCI;
134       ddev->base.u.pci.vendor_id = vendor_id;
135       ddev->base.u.pci.chip_id = chip_id;
136    } else {
137       ddev->base.type = PIPE_LOADER_DEVICE_PLATFORM;
138    }
139    ddev->base.ops = &pipe_loader_drm_ops;
140    ddev->fd = fd;
141 
142    if (zink)
143       ddev->base.driver_name = strdup("zink");
144    else
145       ddev->base.driver_name = loader_get_driver_for_fd(fd);
146    if (!ddev->base.driver_name)
147       goto fail;
148 
149    /* For the closed source AMD OpenGL driver, we want libgbm to load
150     * "amdgpu_dri.so", but we want Gallium multimedia drivers to load
151     * "radeonsi". So change amdgpu to radeonsi for Gallium.
152     */
153    if (strcmp(ddev->base.driver_name, "amdgpu") == 0) {
154       FREE(ddev->base.driver_name);
155       ddev->base.driver_name = strdup("radeonsi");
156    }
157 
158    struct util_dl_library **plib = NULL;
159 #ifndef GALLIUM_STATIC_TARGETS
160    plib = &ddev->lib;
161 #endif
162    ddev->dd = get_driver_descriptor(ddev->base.driver_name, plib);
163 
164    /* vgem is a virtual device; don't try using it with kmsro */
165    if (strcmp(ddev->base.driver_name, "vgem") == 0)
166       goto fail;
167 
168    /* kmsro supports lots of drivers, try as a fallback */
169    if (!ddev->dd && !zink)
170       ddev->dd = get_driver_descriptor("kmsro", plib);
171 
172    if (!ddev->dd)
173       goto fail;
174 
175    *dev = &ddev->base;
176    return true;
177 
178   fail:
179 #ifndef GALLIUM_STATIC_TARGETS
180    if (ddev->lib)
181       util_dl_close(ddev->lib);
182 #endif
183    FREE(ddev->base.driver_name);
184    FREE(ddev);
185    return false;
186 }
187 
188 bool
pipe_loader_drm_probe_fd(struct pipe_loader_device ** dev,int fd,bool zink)189 pipe_loader_drm_probe_fd(struct pipe_loader_device **dev, int fd, bool zink)
190 {
191    bool ret;
192    int new_fd;
193 
194    if (fd < 0 || (new_fd = os_dupfd_cloexec(fd)) < 0)
195      return false;
196 
197    ret = pipe_loader_drm_probe_fd_nodup(dev, new_fd, zink);
198    if (!ret)
199       close(new_fd);
200 
201    return ret;
202 }
203 
204 static int
open_drm_render_node_minor(int minor)205 open_drm_render_node_minor(int minor)
206 {
207    char path[PATH_MAX];
208    snprintf(path, sizeof(path), DRM_RENDER_NODE_DEV_NAME_FORMAT, DRM_DIR_NAME,
209             minor);
210    return loader_open_device(path);
211 }
212 
213 static int
pipe_loader_drm_probe_internal(struct pipe_loader_device ** devs,int ndev,bool zink)214 pipe_loader_drm_probe_internal(struct pipe_loader_device **devs, int ndev, bool zink)
215 {
216    int i, j, fd;
217 
218    for (i = DRM_RENDER_NODE_MIN_MINOR, j = 0;
219         i <= DRM_RENDER_NODE_MAX_MINOR; i++) {
220       struct pipe_loader_device *dev;
221 
222       fd = open_drm_render_node_minor(i);
223       if (fd < 0)
224          continue;
225 
226       if (!pipe_loader_drm_probe_fd_nodup(&dev, fd, zink)) {
227          close(fd);
228          continue;
229       }
230 
231       if (j < ndev) {
232          devs[j] = dev;
233       } else {
234          close(fd);
235          dev->ops->release(&dev);
236       }
237       j++;
238    }
239 
240    return j;
241 }
242 
243 int
pipe_loader_drm_probe(struct pipe_loader_device ** devs,int ndev)244 pipe_loader_drm_probe(struct pipe_loader_device **devs, int ndev)
245 {
246    return pipe_loader_drm_probe_internal(devs, ndev, false);
247 }
248 
249 #ifdef HAVE_ZINK
250 int
pipe_loader_drm_zink_probe(struct pipe_loader_device ** devs,int ndev)251 pipe_loader_drm_zink_probe(struct pipe_loader_device **devs, int ndev)
252 {
253    return pipe_loader_drm_probe_internal(devs, ndev, true);
254 }
255 #endif
256 
257 static void
pipe_loader_drm_release(struct pipe_loader_device ** dev)258 pipe_loader_drm_release(struct pipe_loader_device **dev)
259 {
260    struct pipe_loader_drm_device *ddev = pipe_loader_drm_device(*dev);
261 
262 #ifndef GALLIUM_STATIC_TARGETS
263    if (ddev->lib)
264       util_dl_close(ddev->lib);
265 #endif
266 
267    close(ddev->fd);
268    FREE(ddev->base.driver_name);
269    pipe_loader_base_release(dev);
270 }
271 
272 int
pipe_loader_get_compatible_render_capable_device_fd(int kms_only_fd)273 pipe_loader_get_compatible_render_capable_device_fd(int kms_only_fd)
274 {
275    bool is_platform_device;
276    struct pipe_loader_device *dev;
277    const char * const drivers[] = {
278 #if defined GALLIUM_ASAHI
279       "asahi",
280 #endif
281 #if defined GALLIUM_ETNAVIV
282       "etnaviv",
283 #endif
284 #if defined GALLIUM_FREEDRENO
285       "msm",
286 #endif
287 #if defined GALLIUM_LIMA
288       "lima",
289 #endif
290 #if defined GALLIUM_PANFROST
291       "panfrost",
292 #endif
293 #if defined GALLIUM_V3D
294       "v3d",
295 #endif
296 #if defined GALLIUM_VC4
297       "vc4",
298 #endif
299    };
300 
301    if (!pipe_loader_drm_probe_fd(&dev, kms_only_fd, false))
302       return -1;
303    is_platform_device = (dev->type == PIPE_LOADER_DEVICE_PLATFORM);
304    pipe_loader_release(&dev, 1);
305 
306    /* For display-only devices that are not on the platform bus, we can't assume
307     * that any of the rendering devices are compatible. */
308    if (!is_platform_device)
309       return -1;
310 
311    /* For platform display-only devices, we try to find a render-capable device
312     * on the platform bus and that should be compatible with the display-only
313     * device. */
314    if (ARRAY_SIZE(drivers) == 0)
315       return -1;
316 
317    return loader_open_render_node_platform_device(drivers, ARRAY_SIZE(drivers));
318 }
319 
320 static const struct driOptionDescription *
pipe_loader_drm_get_driconf(struct pipe_loader_device * dev,unsigned * count)321 pipe_loader_drm_get_driconf(struct pipe_loader_device *dev, unsigned *count)
322 {
323    struct pipe_loader_drm_device *ddev = pipe_loader_drm_device(dev);
324 
325    *count = ddev->dd->driconf_count;
326    return ddev->dd->driconf;
327 }
328 
329 static struct pipe_screen *
pipe_loader_drm_create_screen(struct pipe_loader_device * dev,const struct pipe_screen_config * config,bool sw_vk)330 pipe_loader_drm_create_screen(struct pipe_loader_device *dev,
331                               const struct pipe_screen_config *config, bool sw_vk)
332 {
333    struct pipe_loader_drm_device *ddev = pipe_loader_drm_device(dev);
334 
335    return ddev->dd->create_screen(ddev->fd, config);
336 }
337 
338 const struct driOptionDescription *
pipe_loader_drm_get_driconf_by_name(const char * driver_name,unsigned * count)339 pipe_loader_drm_get_driconf_by_name(const char *driver_name, unsigned *count)
340 {
341    driOptionDescription *driconf = NULL;
342    struct util_dl_library *lib = NULL;
343    const struct drm_driver_descriptor *dd =
344       get_driver_descriptor(driver_name, &lib);
345 
346    if (!dd) {
347       *count = 0;
348    } else {
349       *count = dd->driconf_count;
350       size_t size = sizeof(*driconf) * *count;
351       driconf = malloc(size);
352       memcpy(driconf, dd->driconf, size);
353    }
354    if (lib)
355       util_dl_close(lib);
356 
357    return driconf;
358 }
359 
360 static const struct pipe_loader_ops pipe_loader_drm_ops = {
361    .create_screen = pipe_loader_drm_create_screen,
362    .get_driconf = pipe_loader_drm_get_driconf,
363    .release = pipe_loader_drm_release
364 };
365