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 TUNGSTEN GRAPHICS 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
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 DRIVER_MAP_GALLIUM_ONLY
52 #include "pci_ids/pci_id_driver_map.h"
54 struct pipe_loader_drm_device
{
55 struct pipe_loader_device base
;
56 struct util_dl_library
*lib
;
60 #define pipe_loader_drm_device(dev) ((struct pipe_loader_drm_device *)dev)
63 find_drm_pci_id(struct pipe_loader_drm_device
*ddev
)
65 struct udev
*udev
= NULL
;
66 struct udev_device
*parent
, *device
= NULL
;
70 if (fstat(ddev
->fd
, &stat
) < 0)
77 device
= udev_device_new_from_devnum(udev
, 'c', stat
.st_rdev
);
81 parent
= udev_device_get_parent(device
);
85 pci_id
= udev_device_get_property_value(parent
, "PCI_ID");
87 sscanf(pci_id
, "%x:%x", &ddev
->base
.u
.pci
.vendor_id
,
88 &ddev
->base
.u
.pci
.chip_id
) != 2)
91 udev_device_unref(device
);
98 udev_device_unref(device
);
106 find_drm_driver_name(struct pipe_loader_drm_device
*ddev
)
108 struct pipe_loader_device
*dev
= &ddev
->base
;
111 for (i
= 0; driver_map
[i
].driver
; i
++) {
112 if (dev
->u
.pci
.vendor_id
!= driver_map
[i
].vendor_id
)
115 if (driver_map
[i
].num_chips_ids
== -1) {
116 dev
->driver_name
= driver_map
[i
].driver
;
120 for (j
= 0; j
< driver_map
[i
].num_chips_ids
; j
++) {
121 if (dev
->u
.pci
.chip_id
== driver_map
[i
].chip_ids
[j
]) {
122 dev
->driver_name
= driver_map
[i
].driver
;
134 static struct pipe_loader_ops pipe_loader_drm_ops
;
137 pipe_loader_drm_x_auth(int fd
)
139 #if HAVE_PIPE_LOADER_XCB
140 /* Try authenticate with the X server to give us access to devices that X
142 xcb_connection_t
*xcb_conn
;
143 const xcb_setup_t
*xcb_setup
;
144 xcb_screen_iterator_t s
;
145 xcb_dri2_connect_cookie_t connect_cookie
;
146 xcb_dri2_connect_reply_t
*connect
;
148 xcb_dri2_authenticate_cookie_t authenticate_cookie
;
149 xcb_dri2_authenticate_reply_t
*authenticate
;
151 xcb_conn
= xcb_connect(NULL
, NULL
);
156 xcb_setup
= xcb_get_setup(xcb_conn
);
161 s
= xcb_setup_roots_iterator(xcb_setup
);
162 connect_cookie
= xcb_dri2_connect_unchecked(xcb_conn
, s
.data
->root
,
163 XCB_DRI2_DRIVER_TYPE_DRI
);
164 connect
= xcb_dri2_connect_reply(xcb_conn
, connect_cookie
, NULL
);
166 if (!connect
|| connect
->driver_name_length
167 + connect
->device_name_length
== 0) {
172 if (drmGetMagic(fd
, &magic
))
175 authenticate_cookie
= xcb_dri2_authenticate_unchecked(xcb_conn
,
178 authenticate
= xcb_dri2_authenticate_reply(xcb_conn
,
184 xcb_disconnect(xcb_conn
);
190 pipe_loader_drm_probe_fd(struct pipe_loader_device
**dev
, int fd
)
192 struct pipe_loader_drm_device
*ddev
= CALLOC_STRUCT(pipe_loader_drm_device
);
194 ddev
->base
.type
= PIPE_LOADER_DEVICE_PCI
;
195 ddev
->base
.ops
= &pipe_loader_drm_ops
;
198 pipe_loader_drm_x_auth(fd
);
200 if (!find_drm_pci_id(ddev
))
203 if (!find_drm_driver_name(ddev
))
215 open_drm_minor(int minor
)
218 snprintf(path
, sizeof(path
), DRM_DEV_NAME
, DRM_DIR_NAME
, minor
);
219 return open(path
, O_RDWR
, 0);
223 pipe_loader_drm_probe(struct pipe_loader_device
**devs
, int ndev
)
227 for (i
= 0, j
= 0; i
< DRM_MAX_MINOR
; i
++) {
228 fd
= open_drm_minor(i
);
232 if (j
>= ndev
|| !pipe_loader_drm_probe_fd(&devs
[j
], fd
))
242 pipe_loader_drm_release(struct pipe_loader_device
**dev
)
244 struct pipe_loader_drm_device
*ddev
= pipe_loader_drm_device(*dev
);
247 util_dl_close(ddev
->lib
);
254 static struct pipe_screen
*
255 pipe_loader_drm_create_screen(struct pipe_loader_device
*dev
,
256 const char *library_paths
)
258 struct pipe_loader_drm_device
*ddev
= pipe_loader_drm_device(dev
);
259 const struct drm_driver_descriptor
*dd
;
262 ddev
->lib
= pipe_loader_find_module(dev
, library_paths
);
266 dd
= (const struct drm_driver_descriptor
*)
267 util_dl_get_proc_address(ddev
->lib
, "driver_descriptor");
269 /* sanity check on the name */
270 if (!dd
|| strcmp(dd
->name
, ddev
->base
.driver_name
) != 0)
273 return dd
->create_screen(ddev
->fd
);
276 static struct pipe_loader_ops pipe_loader_drm_ops
= {
277 .create_screen
= pipe_loader_drm_create_screen
,
278 .release
= pipe_loader_drm_release