Revert "pipe-loader: simplify pipe_loader_drm_probe"
[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 "state_tracker/drm_driver.h"
40 #include "pipe_loader_priv.h"
41
42 #include "util/u_memory.h"
43 #include "util/u_dl.h"
44 #include "util/u_debug.h"
45
46 #define DRM_RENDER_NODE_DEV_NAME_FORMAT "%s/renderD%d"
47 #define DRM_RENDER_NODE_MAX_NODES 63
48 #define DRM_RENDER_NODE_MIN_MINOR 128
49 #define DRM_RENDER_NODE_MAX_MINOR (DRM_RENDER_NODE_MIN_MINOR + DRM_RENDER_NODE_MAX_NODES)
50
51 struct pipe_loader_drm_device {
52 struct pipe_loader_device base;
53 struct util_dl_library *lib;
54 int fd;
55 };
56
57 #define pipe_loader_drm_device(dev) ((struct pipe_loader_drm_device *)dev)
58
59 static struct pipe_loader_ops pipe_loader_drm_ops;
60
61 bool
62 pipe_loader_drm_probe_fd(struct pipe_loader_device **dev, int fd)
63 {
64 struct pipe_loader_drm_device *ddev = CALLOC_STRUCT(pipe_loader_drm_device);
65 int vendor_id, chip_id;
66
67 if (!ddev)
68 return false;
69
70 if (loader_get_pci_id_for_fd(fd, &vendor_id, &chip_id)) {
71 ddev->base.type = PIPE_LOADER_DEVICE_PCI;
72 ddev->base.u.pci.vendor_id = vendor_id;
73 ddev->base.u.pci.chip_id = chip_id;
74 } else {
75 ddev->base.type = PIPE_LOADER_DEVICE_PLATFORM;
76 }
77 ddev->base.ops = &pipe_loader_drm_ops;
78 ddev->fd = fd;
79
80 ddev->base.driver_name = loader_get_driver_for_fd(fd, _LOADER_GALLIUM);
81 if (!ddev->base.driver_name)
82 goto fail;
83
84 *dev = &ddev->base;
85 return true;
86
87 fail:
88 FREE(ddev);
89 return false;
90 }
91
92 static int
93 open_drm_render_node_minor(int minor)
94 {
95 char path[PATH_MAX];
96 snprintf(path, sizeof(path), DRM_RENDER_NODE_DEV_NAME_FORMAT, DRM_DIR_NAME,
97 minor);
98 return loader_open_device(path);
99 }
100
101 int
102 pipe_loader_drm_probe(struct pipe_loader_device **devs, int ndev)
103 {
104 int i, j, fd;
105
106 for (i = DRM_RENDER_NODE_MIN_MINOR, j = 0;
107 i <= DRM_RENDER_NODE_MAX_MINOR; i++) {
108 fd = open_drm_render_node_minor(i);
109 struct pipe_loader_device *dev;
110 if (fd < 0)
111 continue;
112
113 if (!pipe_loader_drm_probe_fd(&dev, fd)) {
114 close(fd);
115 continue;
116 }
117
118 if (j < ndev) {
119 devs[j] = dev;
120 } else {
121 close(fd);
122 dev->ops->release(&dev);
123 }
124 j++;
125 }
126
127 return j;
128 }
129
130 static void
131 pipe_loader_drm_release(struct pipe_loader_device **dev)
132 {
133 struct pipe_loader_drm_device *ddev = pipe_loader_drm_device(*dev);
134
135 if (ddev->lib)
136 util_dl_close(ddev->lib);
137
138 close(ddev->fd);
139 FREE(ddev->base.driver_name);
140 FREE(ddev);
141 *dev = NULL;
142 }
143
144 static const struct drm_conf_ret *
145 pipe_loader_drm_configuration(struct pipe_loader_device *dev,
146 enum drm_conf conf)
147 {
148 struct pipe_loader_drm_device *ddev = pipe_loader_drm_device(dev);
149 const struct drm_driver_descriptor *dd;
150
151 if (!ddev->lib)
152 return NULL;
153
154 dd = (const struct drm_driver_descriptor *)
155 util_dl_get_proc_address(ddev->lib, "driver_descriptor");
156
157 /* sanity check on the name */
158 if (!dd || strcmp(dd->name, ddev->base.driver_name) != 0)
159 return NULL;
160
161 if (!dd->configuration)
162 return NULL;
163
164 return dd->configuration(conf);
165 }
166
167 static struct pipe_screen *
168 pipe_loader_drm_create_screen(struct pipe_loader_device *dev,
169 const char *library_paths)
170 {
171 struct pipe_loader_drm_device *ddev = pipe_loader_drm_device(dev);
172 const struct drm_driver_descriptor *dd;
173
174 if (!ddev->lib)
175 ddev->lib = pipe_loader_find_module(dev, library_paths);
176 if (!ddev->lib)
177 return NULL;
178
179 dd = (const struct drm_driver_descriptor *)
180 util_dl_get_proc_address(ddev->lib, "driver_descriptor");
181
182 /* sanity check on the name */
183 if (!dd || strcmp(dd->name, ddev->base.driver_name) != 0)
184 return NULL;
185
186 return dd->create_screen(ddev->fd);
187 }
188
189 static struct pipe_loader_ops pipe_loader_drm_ops = {
190 .create_screen = pipe_loader_drm_create_screen,
191 .configuration = pipe_loader_drm_configuration,
192 .release = pipe_loader_drm_release
193 };