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