pipe-loader: add pipe_loader_ops::configuration()
[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
37 #ifdef HAVE_PIPE_LOADER_XCB
38
39 #include <xcb/dri2.h>
40
41 #endif
42
43 #include "loader.h"
44 #include "state_tracker/drm_driver.h"
45 #include "pipe_loader_priv.h"
46
47 #include "util/u_memory.h"
48 #include "util/u_dl.h"
49 #include "util/u_debug.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 struct util_dl_library *lib;
59 int fd;
60 };
61
62 #define pipe_loader_drm_device(dev) ((struct pipe_loader_drm_device *)dev)
63
64 static struct pipe_loader_ops pipe_loader_drm_ops;
65
66 static void
67 pipe_loader_drm_x_auth(int fd)
68 {
69 #ifdef HAVE_PIPE_LOADER_XCB
70 /* Try authenticate with the X server to give us access to devices that X
71 * is running on. */
72 xcb_connection_t *xcb_conn;
73 const xcb_setup_t *xcb_setup;
74 xcb_screen_iterator_t s;
75 xcb_dri2_connect_cookie_t connect_cookie;
76 xcb_dri2_connect_reply_t *connect;
77 drm_magic_t magic;
78 xcb_dri2_authenticate_cookie_t authenticate_cookie;
79 xcb_dri2_authenticate_reply_t *authenticate;
80
81 xcb_conn = xcb_connect(NULL, NULL);
82
83 if(!xcb_conn)
84 return;
85
86 xcb_setup = xcb_get_setup(xcb_conn);
87
88 if (!xcb_setup)
89 goto disconnect;
90
91 s = xcb_setup_roots_iterator(xcb_setup);
92 connect_cookie = xcb_dri2_connect_unchecked(xcb_conn, s.data->root,
93 XCB_DRI2_DRIVER_TYPE_DRI);
94 connect = xcb_dri2_connect_reply(xcb_conn, connect_cookie, NULL);
95
96 if (!connect || connect->driver_name_length
97 + connect->device_name_length == 0) {
98
99 goto disconnect;
100 }
101
102 if (drmGetMagic(fd, &magic))
103 goto disconnect;
104
105 authenticate_cookie = xcb_dri2_authenticate_unchecked(xcb_conn,
106 s.data->root,
107 magic);
108 authenticate = xcb_dri2_authenticate_reply(xcb_conn,
109 authenticate_cookie,
110 NULL);
111 FREE(authenticate);
112
113 disconnect:
114 xcb_disconnect(xcb_conn);
115
116 #endif
117 }
118
119 bool
120 pipe_loader_drm_probe_fd(struct pipe_loader_device **dev, int fd,
121 boolean auth_x)
122 {
123 struct pipe_loader_drm_device *ddev = CALLOC_STRUCT(pipe_loader_drm_device);
124 int vendor_id, chip_id;
125
126 if (!ddev)
127 return false;
128
129 if (loader_get_pci_id_for_fd(fd, &vendor_id, &chip_id)) {
130 ddev->base.type = PIPE_LOADER_DEVICE_PCI;
131 ddev->base.u.pci.vendor_id = vendor_id;
132 ddev->base.u.pci.chip_id = chip_id;
133 } else {
134 ddev->base.type = PIPE_LOADER_DEVICE_PLATFORM;
135 }
136 ddev->base.ops = &pipe_loader_drm_ops;
137 ddev->fd = fd;
138
139 if (auth_x)
140 pipe_loader_drm_x_auth(fd);
141
142 ddev->base.driver_name = loader_get_driver_for_fd(fd, _LOADER_GALLIUM);
143 if (!ddev->base.driver_name)
144 goto fail;
145
146 *dev = &ddev->base;
147 return true;
148
149 fail:
150 FREE(ddev);
151 return false;
152 }
153
154 static int
155 open_drm_minor(int minor)
156 {
157 char path[PATH_MAX];
158 snprintf(path, sizeof(path), DRM_DEV_NAME, DRM_DIR_NAME, minor);
159 return open(path, O_RDWR, 0);
160 }
161
162 static int
163 open_drm_render_node_minor(int minor)
164 {
165 char path[PATH_MAX];
166 snprintf(path, sizeof(path), DRM_RENDER_NODE_DEV_NAME_FORMAT, DRM_DIR_NAME,
167 minor);
168 return open(path, O_RDWR, 0);
169 }
170
171 int
172 pipe_loader_drm_probe(struct pipe_loader_device **devs, int ndev)
173 {
174 int i, k, fd, num_render_node_devs;
175 int j = 0;
176
177 struct {
178 unsigned vendor_id;
179 unsigned chip_id;
180 } render_node_devs[DRM_RENDER_NODE_MAX_NODES];
181
182 /* Look for render nodes first */
183 for (i = DRM_RENDER_NODE_MIN_MINOR, j = 0;
184 i <= DRM_RENDER_NODE_MAX_MINOR; i++) {
185 fd = open_drm_render_node_minor(i);
186 struct pipe_loader_device *dev;
187 if (fd < 0)
188 continue;
189
190 if (!pipe_loader_drm_probe_fd(&dev, fd, false)) {
191 close(fd);
192 continue;
193 }
194
195 render_node_devs[j].vendor_id = dev->u.pci.vendor_id;
196 render_node_devs[j].chip_id = dev->u.pci.chip_id;
197
198 if (j < ndev) {
199 devs[j] = dev;
200 } else {
201 close(fd);
202 dev->ops->release(&dev);
203 }
204 j++;
205 }
206
207 num_render_node_devs = j;
208
209 /* Next look for drm devices. */
210 for (i = 0; i < DRM_MAX_MINOR; i++) {
211 struct pipe_loader_device *dev;
212 boolean duplicate = FALSE;
213 fd = open_drm_minor(i);
214 if (fd < 0)
215 continue;
216
217 if (!pipe_loader_drm_probe_fd(&dev, fd, true)) {
218 close(fd);
219 continue;
220 }
221
222 /* Check to make sure we aren't already accessing this device via
223 * render nodes.
224 */
225 for (k = 0; k < num_render_node_devs; k++) {
226 if (dev->u.pci.vendor_id == render_node_devs[k].vendor_id &&
227 dev->u.pci.chip_id == render_node_devs[k].chip_id) {
228 close(fd);
229 dev->ops->release(&dev);
230 duplicate = TRUE;
231 break;
232 }
233 }
234
235 if (duplicate)
236 continue;
237
238 if (j < ndev) {
239 devs[j] = dev;
240 } else {
241 dev->ops->release(&dev);
242 }
243
244 j++;
245 }
246
247 return j;
248 }
249
250 static void
251 pipe_loader_drm_release(struct pipe_loader_device **dev)
252 {
253 struct pipe_loader_drm_device *ddev = pipe_loader_drm_device(*dev);
254
255 if (ddev->lib)
256 util_dl_close(ddev->lib);
257
258 close(ddev->fd);
259 /* XXX: Free ddev->base.driver_name - strdup at loader_get_driver_for_fd */
260 FREE(ddev);
261 *dev = NULL;
262 }
263
264 static const struct drm_conf_ret *
265 pipe_loader_drm_configuration(struct pipe_loader_device *dev,
266 enum drm_conf conf)
267 {
268 struct pipe_loader_drm_device *ddev = pipe_loader_drm_device(dev);
269 const struct drm_driver_descriptor *dd;
270
271 if (!ddev->lib)
272 return NULL;
273
274 dd = (const struct drm_driver_descriptor *)
275 util_dl_get_proc_address(ddev->lib, "driver_descriptor");
276
277 /* sanity check on the name */
278 if (!dd || strcmp(dd->name, ddev->base.driver_name) != 0)
279 return NULL;
280
281 if (!dd->configuration)
282 return NULL;
283
284 return dd->configuration(conf);
285 }
286
287 static struct pipe_screen *
288 pipe_loader_drm_create_screen(struct pipe_loader_device *dev,
289 const char *library_paths)
290 {
291 struct pipe_loader_drm_device *ddev = pipe_loader_drm_device(dev);
292 const struct drm_driver_descriptor *dd;
293
294 if (!ddev->lib)
295 ddev->lib = pipe_loader_find_module(dev, library_paths);
296 if (!ddev->lib)
297 return NULL;
298
299 dd = (const struct drm_driver_descriptor *)
300 util_dl_get_proc_address(ddev->lib, "driver_descriptor");
301
302 /* sanity check on the name */
303 if (!dd || strcmp(dd->name, ddev->base.driver_name) != 0)
304 return NULL;
305
306 return dd->create_screen(ddev->fd);
307 }
308
309 static struct pipe_loader_ops pipe_loader_drm_ops = {
310 .create_screen = pipe_loader_drm_create_screen,
311 .configuration = pipe_loader_drm_configuration,
312 .release = pipe_loader_drm_release
313 };