OSDN Git Service

994a284385c7b2d51b5300b31422743130d89306
[android-x86/external-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         .name = "i915",
93         .driver_name = "i915",
94         .create_screen = pipe_i915_create_screen,
95         .configuration = configuration_query,
96     },
97 #ifdef USE_VC4_SIMULATOR
98     /* VC4 simulator and ILO (i965) are mutually exclusive (error at
99      * configure). As the latter is unconditionally added, keep this one above
100      * it.
101      */
102     {
103         .name = "i965",
104         .driver_name = "vc4",
105         .create_screen = pipe_vc4_create_screen,
106         .configuration = configuration_query,
107     },
108 #endif
109     {
110         .name = "i965",
111         .driver_name = "i915",
112         .create_screen = pipe_ilo_create_screen,
113         .configuration = configuration_query,
114     },
115     {
116         .name = "nouveau",
117         .driver_name = "nouveau",
118         .create_screen = pipe_nouveau_create_screen,
119         .configuration = configuration_query,
120     },
121     {
122         .name = "r300",
123         .driver_name = "radeon",
124         .create_screen = pipe_r300_create_screen,
125         .configuration = configuration_query,
126     },
127     {
128         .name = "r600",
129         .driver_name = "radeon",
130         .create_screen = pipe_r600_create_screen,
131         .configuration = configuration_query,
132     },
133     {
134         .name = "radeonsi",
135         .driver_name = "radeon",
136         .create_screen = pipe_radeonsi_create_screen,
137         .configuration = configuration_query,
138     },
139     {
140         .name = "vmwgfx",
141         .driver_name = "vmwgfx",
142         .create_screen = pipe_vmwgfx_create_screen,
143         .configuration = configuration_query,
144     },
145     {
146         .name = "kgsl",
147         .driver_name = "freedreno",
148         .create_screen = pipe_freedreno_create_screen,
149         .configuration = configuration_query,
150     },
151     {
152         .name = "msm",
153         .driver_name = "freedreno",
154         .create_screen = pipe_freedreno_create_screen,
155         .configuration = configuration_query,
156     },
157     {
158         .name = "virtio_gpu",
159         .driver_name = "virtio-gpu",
160         .create_screen = pipe_virgl_create_screen,
161         .configuration = configuration_query,
162     },
163     {
164         .name = "vc4",
165         .driver_name = "vc4",
166         .create_screen = pipe_vc4_create_screen,
167         .configuration = configuration_query,
168     },
169 };
170 #endif
171
172 bool
173 pipe_loader_drm_probe_fd(struct pipe_loader_device **dev, int fd)
174 {
175    struct pipe_loader_drm_device *ddev = CALLOC_STRUCT(pipe_loader_drm_device);
176    int vendor_id, chip_id;
177
178    if (!ddev)
179       return false;
180
181    if (loader_get_pci_id_for_fd(fd, &vendor_id, &chip_id)) {
182       ddev->base.type = PIPE_LOADER_DEVICE_PCI;
183       ddev->base.u.pci.vendor_id = vendor_id;
184       ddev->base.u.pci.chip_id = chip_id;
185    } else {
186       ddev->base.type = PIPE_LOADER_DEVICE_PLATFORM;
187    }
188    ddev->base.ops = &pipe_loader_drm_ops;
189    ddev->fd = fd;
190
191    ddev->base.driver_name = loader_get_driver_for_fd(fd, _LOADER_GALLIUM);
192    if (!ddev->base.driver_name)
193       goto fail;
194
195 #ifdef GALLIUM_STATIC_TARGETS
196    for (int i = 0; i < ARRAY_SIZE(driver_descriptors); i++) {
197       if (strcmp(driver_descriptors[i].name, ddev->base.driver_name) == 0) {
198          ddev->dd = &driver_descriptors[i];
199          break;
200       }
201    }
202    if (!ddev->dd)
203       goto fail;
204 #else
205    ddev->lib = pipe_loader_find_module(&ddev->base, PIPE_SEARCH_DIR);
206    if (!ddev->lib)
207       goto fail;
208
209    ddev->dd = (const struct drm_driver_descriptor *)
210       util_dl_get_proc_address(ddev->lib, "driver_descriptor");
211
212    /* sanity check on the name */
213    if (!ddev->dd || strcmp(ddev->dd->name, ddev->base.driver_name) != 0)
214       goto fail;
215 #endif
216
217    *dev = &ddev->base;
218    return true;
219
220   fail:
221 #ifndef GALLIUM_STATIC_TARGETS
222    if (ddev->lib)
223       util_dl_close(ddev->lib);
224 #endif
225    FREE(ddev);
226    return false;
227 }
228
229 static int
230 open_drm_render_node_minor(int minor)
231 {
232    char path[PATH_MAX];
233    snprintf(path, sizeof(path), DRM_RENDER_NODE_DEV_NAME_FORMAT, DRM_DIR_NAME,
234             minor);
235    return loader_open_device(path);
236 }
237
238 int
239 pipe_loader_drm_probe(struct pipe_loader_device **devs, int ndev)
240 {
241    int i, j, fd;
242
243    for (i = DRM_RENDER_NODE_MIN_MINOR, j = 0;
244         i <= DRM_RENDER_NODE_MAX_MINOR; i++) {
245       struct pipe_loader_device *dev;
246
247       fd = open_drm_render_node_minor(i);
248       if (fd < 0)
249          continue;
250
251       if (!pipe_loader_drm_probe_fd(&dev, fd)) {
252          close(fd);
253          continue;
254       }
255
256       if (j < ndev) {
257          devs[j] = dev;
258       } else {
259          close(fd);
260          dev->ops->release(&dev);
261       }
262       j++;
263    }
264
265    return j;
266 }
267
268 static void
269 pipe_loader_drm_release(struct pipe_loader_device **dev)
270 {
271    struct pipe_loader_drm_device *ddev = pipe_loader_drm_device(*dev);
272
273 #ifndef GALLIUM_STATIC_TARGETS
274    if (ddev->lib)
275       util_dl_close(ddev->lib);
276 #endif
277
278    close(ddev->fd);
279    FREE(ddev->base.driver_name);
280    FREE(ddev);
281    *dev = NULL;
282 }
283
284 static const struct drm_conf_ret *
285 pipe_loader_drm_configuration(struct pipe_loader_device *dev,
286                               enum drm_conf conf)
287 {
288    struct pipe_loader_drm_device *ddev = pipe_loader_drm_device(dev);
289
290    if (!ddev->dd->configuration)
291       return NULL;
292
293    return ddev->dd->configuration(conf);
294 }
295
296 static struct pipe_screen *
297 pipe_loader_drm_create_screen(struct pipe_loader_device *dev)
298 {
299    struct pipe_loader_drm_device *ddev = pipe_loader_drm_device(dev);
300
301    return ddev->dd->create_screen(ddev->fd);
302 }
303
304 static const struct pipe_loader_ops pipe_loader_drm_ops = {
305    .create_screen = pipe_loader_drm_create_screen,
306    .configuration = pipe_loader_drm_configuration,
307    .release = pipe_loader_drm_release
308 };