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