1 /**************************************************************************
3 * Copyright 2011 Intel Corporation
4 * Copyright 2012 Francisco Jerez
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:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
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.
28 * Kristian Høgsberg <krh@bitplanet.net>
29 * Benjamin Franzke <benjaminfranzke@googlemail.com>
31 **************************************************************************/
38 #ifdef HAVE_PIPE_LOADER_XCB
45 #include "state_tracker/drm_driver.h"
46 #include "pipe_loader_priv.h"
48 #include "util/u_memory.h"
49 #include "util/u_dl.h"
50 #include "util/u_debug.h"
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)
57 struct pipe_loader_drm_device
{
58 struct pipe_loader_device base
;
59 struct util_dl_library
*lib
;
63 #define pipe_loader_drm_device(dev) ((struct pipe_loader_drm_device *)dev)
65 static struct pipe_loader_ops pipe_loader_drm_ops
;
67 #ifdef HAVE_PIPE_LOADER_XCB
70 get_xcb_screen(xcb_screen_iterator_t iter
, int screen
)
72 for (; iter
.rem
; --screen
, xcb_screen_next(&iter
))
82 pipe_loader_drm_x_auth(int fd
)
84 #ifdef HAVE_PIPE_LOADER_XCB
85 /* Try authenticate with the X server to give us access to devices that X
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
;
93 xcb_dri2_authenticate_cookie_t authenticate_cookie
;
94 xcb_dri2_authenticate_reply_t
*authenticate
;
97 xcb_conn
= xcb_connect(NULL
, &screen
);
102 xcb_setup
= xcb_get_setup(xcb_conn
);
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
);
113 if (!connect
|| connect
->driver_name_length
114 + connect
->device_name_length
== 0) {
119 if (drmGetMagic(fd
, &magic
))
122 authenticate_cookie
= xcb_dri2_authenticate_unchecked(xcb_conn
,
125 authenticate
= xcb_dri2_authenticate_reply(xcb_conn
,
131 xcb_disconnect(xcb_conn
);
137 pipe_loader_drm_probe_fd(struct pipe_loader_device
**dev
, int fd
,
140 struct pipe_loader_drm_device
*ddev
= CALLOC_STRUCT(pipe_loader_drm_device
);
141 int vendor_id
, chip_id
;
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
;
151 ddev
->base
.type
= PIPE_LOADER_DEVICE_PLATFORM
;
153 ddev
->base
.ops
= &pipe_loader_drm_ops
;
157 pipe_loader_drm_x_auth(fd
);
159 ddev
->base
.driver_name
= loader_get_driver_for_fd(fd
, _LOADER_GALLIUM
);
160 if (!ddev
->base
.driver_name
)
172 open_drm_minor(int minor
)
175 snprintf(path
, sizeof(path
), DRM_DEV_NAME
, DRM_DIR_NAME
, minor
);
176 return open(path
, O_RDWR
, 0);
180 open_drm_render_node_minor(int minor
)
183 snprintf(path
, sizeof(path
), DRM_RENDER_NODE_DEV_NAME_FORMAT
, DRM_DIR_NAME
,
185 return open(path
, O_RDWR
, 0);
189 pipe_loader_drm_probe(struct pipe_loader_device
**devs
, int ndev
)
191 int i
, k
, fd
, num_render_node_devs
;
197 } render_node_devs
[DRM_RENDER_NODE_MAX_NODES
];
199 /* Look for render nodes first */
200 for (i
= DRM_RENDER_NODE_MIN_MINOR
, j
= 0;
201 i
<= DRM_RENDER_NODE_MAX_MINOR
; i
++) {
202 fd
= open_drm_render_node_minor(i
);
203 struct pipe_loader_device
*dev
;
207 if (!pipe_loader_drm_probe_fd(&dev
, fd
, false)) {
212 render_node_devs
[j
].vendor_id
= dev
->u
.pci
.vendor_id
;
213 render_node_devs
[j
].chip_id
= dev
->u
.pci
.chip_id
;
219 dev
->ops
->release(&dev
);
224 num_render_node_devs
= j
;
226 /* Next look for drm devices. */
227 for (i
= 0; i
< DRM_MAX_MINOR
; i
++) {
228 struct pipe_loader_device
*dev
;
229 boolean duplicate
= FALSE
;
230 fd
= open_drm_minor(i
);
234 if (!pipe_loader_drm_probe_fd(&dev
, fd
, true)) {
239 /* Check to make sure we aren't already accessing this device via
242 for (k
= 0; k
< num_render_node_devs
; k
++) {
243 if (dev
->u
.pci
.vendor_id
== render_node_devs
[k
].vendor_id
&&
244 dev
->u
.pci
.chip_id
== render_node_devs
[k
].chip_id
) {
246 dev
->ops
->release(&dev
);
258 dev
->ops
->release(&dev
);
268 pipe_loader_drm_release(struct pipe_loader_device
**dev
)
270 struct pipe_loader_drm_device
*ddev
= pipe_loader_drm_device(*dev
);
273 util_dl_close(ddev
->lib
);
276 FREE(ddev
->base
.driver_name
);
281 static const struct drm_conf_ret
*
282 pipe_loader_drm_configuration(struct pipe_loader_device
*dev
,
285 struct pipe_loader_drm_device
*ddev
= pipe_loader_drm_device(dev
);
286 const struct drm_driver_descriptor
*dd
;
291 dd
= (const struct drm_driver_descriptor
*)
292 util_dl_get_proc_address(ddev
->lib
, "driver_descriptor");
294 /* sanity check on the name */
295 if (!dd
|| strcmp(dd
->name
, ddev
->base
.driver_name
) != 0)
298 if (!dd
->configuration
)
301 return dd
->configuration(conf
);
304 static struct pipe_screen
*
305 pipe_loader_drm_create_screen(struct pipe_loader_device
*dev
,
306 const char *library_paths
)
308 struct pipe_loader_drm_device
*ddev
= pipe_loader_drm_device(dev
);
309 const struct drm_driver_descriptor
*dd
;
312 ddev
->lib
= pipe_loader_find_module(dev
, library_paths
);
316 dd
= (const struct drm_driver_descriptor
*)
317 util_dl_get_proc_address(ddev
->lib
, "driver_descriptor");
319 /* sanity check on the name */
320 if (!dd
|| strcmp(dd
->name
, ddev
->base
.driver_name
) != 0)
323 return dd
->create_screen(ddev
->fd
);
326 static struct pipe_loader_ops pipe_loader_drm_ops
= {
327 .create_screen
= pipe_loader_drm_create_screen
,
328 .configuration
= pipe_loader_drm_configuration
,
329 .release
= pipe_loader_drm_release