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 struct pipe_loader_drm_device
{
52 struct pipe_loader_device base
;
53 struct util_dl_library
*lib
;
57 #define pipe_loader_drm_device(dev) ((struct pipe_loader_drm_device *)dev)
59 static struct pipe_loader_ops pipe_loader_drm_ops
;
62 pipe_loader_drm_x_auth(int fd
)
64 #if HAVE_PIPE_LOADER_XCB
65 /* Try authenticate with the X server to give us access to devices that X
67 xcb_connection_t
*xcb_conn
;
68 const xcb_setup_t
*xcb_setup
;
69 xcb_screen_iterator_t s
;
70 xcb_dri2_connect_cookie_t connect_cookie
;
71 xcb_dri2_connect_reply_t
*connect
;
73 xcb_dri2_authenticate_cookie_t authenticate_cookie
;
74 xcb_dri2_authenticate_reply_t
*authenticate
;
76 xcb_conn
= xcb_connect(NULL
, NULL
);
81 xcb_setup
= xcb_get_setup(xcb_conn
);
86 s
= xcb_setup_roots_iterator(xcb_setup
);
87 connect_cookie
= xcb_dri2_connect_unchecked(xcb_conn
, s
.data
->root
,
88 XCB_DRI2_DRIVER_TYPE_DRI
);
89 connect
= xcb_dri2_connect_reply(xcb_conn
, connect_cookie
, NULL
);
91 if (!connect
|| connect
->driver_name_length
92 + connect
->device_name_length
== 0) {
97 if (drmGetMagic(fd
, &magic
))
100 authenticate_cookie
= xcb_dri2_authenticate_unchecked(xcb_conn
,
103 authenticate
= xcb_dri2_authenticate_reply(xcb_conn
,
109 xcb_disconnect(xcb_conn
);
115 pipe_loader_drm_probe_fd(struct pipe_loader_device
**dev
, int fd
,
118 struct pipe_loader_drm_device
*ddev
= CALLOC_STRUCT(pipe_loader_drm_device
);
119 int vendor_id
, chip_id
;
121 if (loader_get_pci_id_for_fd(fd
, &vendor_id
, &chip_id
)) {
122 ddev
->base
.type
= PIPE_LOADER_DEVICE_PCI
;
123 ddev
->base
.u
.pci
.vendor_id
= vendor_id
;
124 ddev
->base
.u
.pci
.chip_id
= chip_id
;
126 ddev
->base
.type
= PIPE_LOADER_DEVICE_PLATFORM
;
128 ddev
->base
.ops
= &pipe_loader_drm_ops
;
132 pipe_loader_drm_x_auth(fd
);
134 ddev
->base
.driver_name
= loader_get_driver_for_fd(fd
, _LOADER_GALLIUM
);
135 if (!ddev
->base
.driver_name
)
147 open_drm_minor(int minor
)
150 snprintf(path
, sizeof(path
), DRM_DEV_NAME
, DRM_DIR_NAME
, minor
);
151 return open(path
, O_RDWR
, 0);
155 pipe_loader_drm_probe(struct pipe_loader_device
**devs
, int ndev
)
159 for (i
= 0, j
= 0; i
< DRM_MAX_MINOR
; i
++) {
160 fd
= open_drm_minor(i
);
164 if (j
>= ndev
|| !pipe_loader_drm_probe_fd(&devs
[j
], fd
, true))
174 pipe_loader_drm_release(struct pipe_loader_device
**dev
)
176 struct pipe_loader_drm_device
*ddev
= pipe_loader_drm_device(*dev
);
179 util_dl_close(ddev
->lib
);
186 static struct pipe_screen
*
187 pipe_loader_drm_create_screen(struct pipe_loader_device
*dev
,
188 const char *library_paths
)
190 struct pipe_loader_drm_device
*ddev
= pipe_loader_drm_device(dev
);
191 const struct drm_driver_descriptor
*dd
;
194 ddev
->lib
= pipe_loader_find_module(dev
, library_paths
);
198 dd
= (const struct drm_driver_descriptor
*)
199 util_dl_get_proc_address(ddev
->lib
, "driver_descriptor");
201 /* sanity check on the name */
202 if (!dd
|| strcmp(dd
->name
, ddev
->base
.driver_name
) != 0)
205 return dd
->create_screen(ddev
->fd
);
208 static struct pipe_loader_ops pipe_loader_drm_ops
= {
209 .create_screen
= pipe_loader_drm_create_screen
,
210 .release
= pipe_loader_drm_release