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 **************************************************************************/
37 #ifdef HAVE_PIPE_LOADER_XCB
44 #include "state_tracker/drm_driver.h"
45 #include "pipe_loader_priv.h"
47 #include "util/u_memory.h"
48 #include "util/u_dl.h"
49 #include "util/u_debug.h"
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)
56 struct pipe_loader_drm_device
{
57 struct pipe_loader_device base
;
58 struct util_dl_library
*lib
;
62 #define pipe_loader_drm_device(dev) ((struct pipe_loader_drm_device *)dev)
64 static struct pipe_loader_ops pipe_loader_drm_ops
;
67 pipe_loader_drm_x_auth(int fd
)
69 #if HAVE_PIPE_LOADER_XCB
70 /* Try authenticate with the X server to give us access to devices that X
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
;
78 xcb_dri2_authenticate_cookie_t authenticate_cookie
;
79 xcb_dri2_authenticate_reply_t
*authenticate
;
81 xcb_conn
= xcb_connect(NULL
, NULL
);
86 xcb_setup
= xcb_get_setup(xcb_conn
);
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
);
96 if (!connect
|| connect
->driver_name_length
97 + connect
->device_name_length
== 0) {
102 if (drmGetMagic(fd
, &magic
))
105 authenticate_cookie
= xcb_dri2_authenticate_unchecked(xcb_conn
,
108 authenticate
= xcb_dri2_authenticate_reply(xcb_conn
,
114 xcb_disconnect(xcb_conn
);
120 pipe_loader_drm_probe_fd(struct pipe_loader_device
**dev
, int fd
,
123 struct pipe_loader_drm_device
*ddev
= CALLOC_STRUCT(pipe_loader_drm_device
);
124 int vendor_id
, chip_id
;
126 if (loader_get_pci_id_for_fd(fd
, &vendor_id
, &chip_id
)) {
127 ddev
->base
.type
= PIPE_LOADER_DEVICE_PCI
;
128 ddev
->base
.u
.pci
.vendor_id
= vendor_id
;
129 ddev
->base
.u
.pci
.chip_id
= chip_id
;
131 ddev
->base
.type
= PIPE_LOADER_DEVICE_PLATFORM
;
133 ddev
->base
.ops
= &pipe_loader_drm_ops
;
137 pipe_loader_drm_x_auth(fd
);
139 ddev
->base
.driver_name
= loader_get_driver_for_fd(fd
, _LOADER_GALLIUM
);
140 if (!ddev
->base
.driver_name
)
152 open_drm_minor(int minor
)
155 snprintf(path
, sizeof(path
), DRM_DEV_NAME
, DRM_DIR_NAME
, minor
);
156 return open(path
, O_RDWR
, 0);
160 open_drm_render_node_minor(int minor
)
163 snprintf(path
, sizeof(path
), DRM_RENDER_NODE_DEV_NAME_FORMAT
, DRM_DIR_NAME
,
165 return open(path
, O_RDWR
, 0);
169 pipe_loader_drm_probe(struct pipe_loader_device
**devs
, int ndev
)
171 int i
, k
, fd
, num_render_node_devs
;
177 } render_node_devs
[DRM_RENDER_NODE_MAX_NODES
];
179 /* Look for render nodes first */
180 for (i
= DRM_RENDER_NODE_MIN_MINOR
, j
= 0;
181 i
<= DRM_RENDER_NODE_MAX_MINOR
; i
++) {
182 fd
= open_drm_render_node_minor(i
);
183 struct pipe_loader_device
*dev
;
187 if (!pipe_loader_drm_probe_fd(&dev
, fd
, false)) {
192 render_node_devs
[j
].vendor_id
= dev
->u
.pci
.vendor_id
;
193 render_node_devs
[j
].chip_id
= dev
->u
.pci
.chip_id
;
199 dev
->ops
->release(&dev
);
204 num_render_node_devs
= j
;
206 /* Next look for drm devices. */
207 for (i
= 0; i
< DRM_MAX_MINOR
; i
++) {
208 struct pipe_loader_device
*dev
;
209 boolean duplicate
= FALSE
;
210 fd
= open_drm_minor(i
);
214 if (!pipe_loader_drm_probe_fd(&dev
, fd
, true)) {
219 /* Check to make sure we aren't already accessing this device via
222 for (k
= 0; k
< num_render_node_devs
; k
++) {
223 if (dev
->u
.pci
.vendor_id
== render_node_devs
[k
].vendor_id
&&
224 dev
->u
.pci
.chip_id
== render_node_devs
[k
].chip_id
) {
226 dev
->ops
->release(&dev
);
238 dev
->ops
->release(&dev
);
248 pipe_loader_drm_release(struct pipe_loader_device
**dev
)
250 struct pipe_loader_drm_device
*ddev
= pipe_loader_drm_device(*dev
);
253 util_dl_close(ddev
->lib
);
260 static struct pipe_screen
*
261 pipe_loader_drm_create_screen(struct pipe_loader_device
*dev
,
262 const char *library_paths
)
264 struct pipe_loader_drm_device
*ddev
= pipe_loader_drm_device(dev
);
265 const struct drm_driver_descriptor
*dd
;
268 ddev
->lib
= pipe_loader_find_module(dev
, library_paths
);
272 dd
= (const struct drm_driver_descriptor
*)
273 util_dl_get_proc_address(ddev
->lib
, "driver_descriptor");
275 /* sanity check on the name */
276 if (!dd
|| strcmp(dd
->name
, ddev
->base
.driver_name
) != 0)
279 return dd
->create_screen(ddev
->fd
);
282 static struct pipe_loader_ops pipe_loader_drm_ops
= {
283 .create_screen
= pipe_loader_drm_create_screen
,
284 .release
= pipe_loader_drm_release