2 * Copyright (C) 2013 Rob Clark <robclark@freedesktop.org>
4 * This code is derived from the following files.
6 * * src/glx/dri3_common.c
7 * Copyright © 2013 Keith Packard
9 * * src/egl/drivers/dri2/common.c
10 * * src/gbm/backends/dri/driver_name.c
11 * Copyright © 2011 Intel Corporation
14 * Kristian Høgsberg <krh@bitplanet.net>
15 * Benjamin Franzke <benjaminfranzke@googlemail.com>
17 * * src/gallium/targets/egl-static/egl.c
18 * Copyright (C) 2010-2011 LunarG Inc.
21 * Chia-I Wu <olv@lunarg.com>
23 * * src/gallium/state_trackers/egl/drm/native_drm.c
24 * Copyright (C) 2010 Chia-I Wu <olv@0xlab.org>
26 * * src/egl/drivers/dri2/platform_android.c
28 * Copyright (C) 2010-2011 Chia-I Wu <olvaffe@gmail.com>
29 * Copyright (C) 2010-2011 LunarG Inc.
31 * Based on platform_x11, which has
33 * Copyright © 2011 Intel Corporation
35 * * src/gallium/auxiliary/pipe-loader/pipe_loader_drm.c
36 * Copyright 2011 Intel Corporation
37 * Copyright 2012 Francisco Jerez
38 * All Rights Reserved.
41 * Kristian Høgsberg <krh@bitplanet.net>
42 * Benjamin Franzke <benjaminfranzke@googlemail.com>
44 * Permission is hereby granted, free of charge, to any person obtaining a
45 * copy of this software and associated documentation files (the "Software"),
46 * to deal in the Software without restriction, including without limitation
47 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
48 * and/or sell copies of the Software, and to permit persons to whom the
49 * Software is furnished to do so, subject to the following conditions:
51 * The above copyright notice and this permission notice (including the next
52 * paragraph) shall be included in all copies or substantial portions of the
55 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
56 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
57 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
58 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
59 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
60 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
64 * Rob Clark <robclark@freedesktop.org>
76 #include <sys/types.h>
80 #ifndef __NOT_HAVE_DRM_H
85 #include "pci_id_driver_map.h"
87 static void default_logger(int level
, const char *fmt
, ...)
89 if (level
<= _LOADER_WARNING
) {
92 vfprintf(stderr
, fmt
, args
);
97 static void (*log_
)(int level
, const char *fmt
, ...) = default_logger
;
102 static void *udev_handle
= NULL
;
105 udev_dlopen_handle(void)
108 udev_handle
= dlopen("libudev.so.1", RTLD_LOCAL
| RTLD_LAZY
);
111 /* libudev.so.1 changed the return types of the two unref functions
112 * from voids to pointers. We don't use those return values, and the
113 * only ABI I've heard that cares about this kind of change (calling
114 * a function with a void * return that actually only returns void)
117 udev_handle
= dlopen("libudev.so.0", RTLD_LOCAL
| RTLD_LAZY
);
120 log_(_LOADER_WARNING
, "Couldn't dlopen libudev.so.1 or "
121 "libudev.so.0, driver detection may be broken.\n");
129 static int dlsym_failed
= 0;
132 checked_dlsym(void *dlopen_handle
, const char *name
)
134 void *result
= dlsym(dlopen_handle
, name
);
140 #define UDEV_SYMBOL(ret, name, args) \
141 ret (*name) args = checked_dlsym(udev_dlopen_handle(), #name);
144 static inline struct udev_device
*
145 udev_device_new_from_fd(struct udev
*udev
, int fd
)
147 struct udev_device
*device
;
149 UDEV_SYMBOL(struct udev_device
*, udev_device_new_from_devnum
,
150 (struct udev
*udev
, char type
, dev_t devnum
));
155 if (fstat(fd
, &buf
) < 0) {
156 log_(_LOADER_WARNING
, "MESA-LOADER: failed to stat fd %d\n", fd
);
160 device
= udev_device_new_from_devnum(udev
, 'c', buf
.st_rdev
);
161 if (device
== NULL
) {
162 log_(_LOADER_WARNING
,
163 "MESA-LOADER: could not create udev device for fd %d\n", fd
);
171 libudev_get_pci_id_for_fd(int fd
, int *vendor_id
, int *chip_id
)
173 struct udev
*udev
= NULL
;
174 struct udev_device
*device
= NULL
, *parent
;
176 UDEV_SYMBOL(struct udev
*, udev_new
, (void));
177 UDEV_SYMBOL(struct udev_device
*, udev_device_get_parent
,
178 (struct udev_device
*));
179 UDEV_SYMBOL(const char *, udev_device_get_property_value
,
180 (struct udev_device
*, const char *));
181 UDEV_SYMBOL(struct udev_device
*, udev_device_unref
,
182 (struct udev_device
*));
183 UDEV_SYMBOL(struct udev
*, udev_unref
, (struct udev
*));
191 device
= udev_device_new_from_fd(udev
, fd
);
195 parent
= udev_device_get_parent(device
);
196 if (parent
== NULL
) {
197 log_(_LOADER_WARNING
, "MESA-LOADER: could not get parent device\n");
201 pci_id
= udev_device_get_property_value(parent
, "PCI_ID");
202 if (pci_id
== NULL
||
203 sscanf(pci_id
, "%x:%x", vendor_id
, chip_id
) != 2) {
204 log_(_LOADER_WARNING
, "MESA-LOADER: malformed or no PCI ID\n");
211 udev_device_unref(device
);
215 return (*chip_id
>= 0);
219 #if defined(HAVE_SYSFS)
221 dev_node_from_fd(int fd
, unsigned int *maj
, unsigned int *min
)
225 if (fstat(fd
, &buf
) < 0) {
226 log_(_LOADER_WARNING
, "MESA-LOADER: failed to stat fd %d\n", fd
);
230 if (!S_ISCHR(buf
.st_mode
)) {
231 log_(_LOADER_WARNING
, "MESA-LOADER: fd %d not a character device\n", fd
);
235 *maj
= major(buf
.st_rdev
);
236 *min
= minor(buf
.st_rdev
);
242 sysfs_get_pci_id_for_fd(int fd
, int *vendor_id
, int *chip_id
)
244 unsigned int maj
, min
;
248 if (dev_node_from_fd(fd
, &maj
, &min
) < 0) {
253 snprintf(buf
, sizeof(buf
), "/sys/dev/char/%d:%d/device/vendor", maj
, min
);
254 if (!(f
= fopen(buf
, "r"))) {
258 if (fscanf(f
, "%x", vendor_id
) != 1) {
264 snprintf(buf
, sizeof(buf
), "/sys/dev/char/%d:%d/device/device", maj
, min
);
265 if (!(f
= fopen(buf
, "r"))) {
269 if (fscanf(f
, "%x", chip_id
) != 1) {
279 #if !defined(__NOT_HAVE_DRM_H)
281 #include <i915_drm.h>
283 #include <radeon_drm.h>
286 drm_get_pci_id_for_fd(int fd
, int *vendor_id
, int *chip_id
)
288 drmVersionPtr version
;
292 version
= drmGetVersion(fd
);
294 log_(_LOADER_WARNING
, "MESA-LOADER: invalid drm fd\n");
297 if (!version
->name
) {
298 log_(_LOADER_WARNING
, "MESA-LOADER: unable to determine the driver name\n");
299 drmFreeVersion(version
);
303 if (strcmp(version
->name
, "i915") == 0) {
304 struct drm_i915_getparam gp
;
309 memset(&gp
, 0, sizeof(gp
));
310 gp
.param
= I915_PARAM_CHIPSET_ID
;
312 ret
= drmCommandWriteRead(fd
, DRM_I915_GETPARAM
, &gp
, sizeof(gp
));
314 log_(_LOADER_WARNING
, "MESA-LOADER: failed to get param for i915\n");
318 else if (strcmp(version
->name
, "radeon") == 0) {
319 struct drm_radeon_info info
;
324 memset(&info
, 0, sizeof(info
));
325 info
.request
= RADEON_INFO_DEVICE_ID
;
326 info
.value
= (unsigned long) chip_id
;
327 ret
= drmCommandWriteRead(fd
, DRM_RADEON_INFO
, &info
, sizeof(info
));
329 log_(_LOADER_WARNING
, "MESA-LOADER: failed to get info for radeon\n");
333 else if (strcmp(version
->name
, "nouveau") == 0) {
338 else if (strcmp(version
->name
, "vmwgfx") == 0) {
344 drmFreeVersion(version
);
346 return (*chip_id
>= 0);
352 loader_get_pci_id_for_fd(int fd
, int *vendor_id
, int *chip_id
)
355 if (libudev_get_pci_id_for_fd(fd
, vendor_id
, chip_id
))
359 if (sysfs_get_pci_id_for_fd(fd
, vendor_id
, chip_id
))
362 #if !defined(__NOT_HAVE_DRM_H)
363 if (drm_get_pci_id_for_fd(fd
, vendor_id
, chip_id
))
372 libudev_get_device_name_for_fd(int fd
)
374 char *device_name
= NULL
;
376 struct udev_device
*device
;
377 const char *const_device_name
;
378 UDEV_SYMBOL(struct udev
*, udev_new
, (void));
379 UDEV_SYMBOL(const char *, udev_device_get_devnode
,
380 (struct udev_device
*));
381 UDEV_SYMBOL(struct udev_device
*, udev_device_unref
,
382 (struct udev_device
*));
383 UDEV_SYMBOL(struct udev
*, udev_unref
, (struct udev
*));
386 device
= udev_device_new_from_fd(udev
, fd
);
390 const_device_name
= udev_device_get_devnode(device
);
391 if (!const_device_name
)
393 device_name
= strdup(const_device_name
);
396 udev_device_unref(device
);
405 sysfs_get_device_name_for_fd(int fd
)
407 char *device_name
= NULL
;
408 unsigned int maj
, min
;
411 static const char match
[9] = "\0DEVNAME=";
414 if (dev_node_from_fd(fd
, &maj
, &min
) < 0)
417 snprintf(buf
, sizeof(buf
), "/sys/dev/char/%d:%d/uevent", maj
, min
);
418 if (!(f
= fopen(buf
, "r")))
421 while (expected
< sizeof(match
)) {
427 } else if (c
== match
[expected
] )
433 strcpy(buf
, "/dev/");
434 if (fgets(buf
+ 5, sizeof(buf
) - 5, f
))
435 device_name
= strdup(buf
);
444 loader_get_device_name_for_fd(int fd
)
449 if ((result
= libudev_get_device_name_for_fd(fd
)))
453 if ((result
= sysfs_get_device_name_for_fd(fd
)))
460 loader_get_driver_for_fd(int fd
, unsigned driver_types
)
462 int vendor_id
, chip_id
, i
, j
;
466 driver_types
= _LOADER_GALLIUM
| _LOADER_DRI
;
468 if (!loader_get_pci_id_for_fd(fd
, &vendor_id
, &chip_id
)) {
470 #ifndef __NOT_HAVE_DRM_H
471 /* fallback to drmGetVersion(): */
472 drmVersionPtr version
= drmGetVersion(fd
);
475 log_(_LOADER_WARNING
, "failed to get driver name for fd %d\n", fd
);
479 driver
= strndup(version
->name
, version
->name_len
);
480 log_(_LOADER_INFO
, "using driver %s for %d\n", driver
, fd
);
482 drmFreeVersion(version
);
488 for (i
= 0; driver_map
[i
].driver
; i
++) {
489 if (vendor_id
!= driver_map
[i
].vendor_id
)
492 if (!(driver_types
& driver_map
[i
].driver_types
))
495 if (driver_map
[i
].predicate
&& !driver_map
[i
].predicate(fd
))
498 if (driver_map
[i
].num_chips_ids
== -1) {
499 driver
= strdup(driver_map
[i
].driver
);
503 for (j
= 0; j
< driver_map
[i
].num_chips_ids
; j
++)
504 if (driver_map
[i
].chip_ids
[j
] == chip_id
) {
505 driver
= strdup(driver_map
[i
].driver
);
511 log_(driver
? _LOADER_DEBUG
: _LOADER_WARNING
,
512 "pci id for fd %d: %04x:%04x, driver %s\n",
513 fd
, vendor_id
, chip_id
, driver
);
518 loader_set_logger(void (*logger
)(int level
, const char *fmt
, ...))