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>
79 #include "xmlconfig.h"
84 #include <sys/types.h>
93 #include "pci_id_driver_map.h"
95 static void default_logger(int level
, const char *fmt
, ...)
97 if (level
<= _LOADER_WARNING
) {
100 vfprintf(stderr
, fmt
, args
);
105 static void (*log_
)(int level
, const char *fmt
, ...) = default_logger
;
108 loader_open_device(const char *device_name
)
112 fd
= open(device_name
, O_RDWR
| O_CLOEXEC
);
113 if (fd
== -1 && errno
== EINVAL
)
116 fd
= open(device_name
, O_RDWR
);
118 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
126 static void *udev_handle
= NULL
;
129 udev_dlopen_handle(void)
132 unsigned flags
= RTLD_NOLOAD
| RTLD_LOCAL
| RTLD_LAZY
;
135 /* libudev.so.1 changed the return types of the two unref functions
136 * from voids to pointers. We don't use those return values, and the
137 * only ABI I've heard that cares about this kind of change (calling
138 * a function with a void * return that actually only returns void)
142 /* First try opening an already linked libudev, then try loading one */
144 for (version
= 1; version
>= 0; version
--) {
145 snprintf(name
, sizeof(name
), "libudev.so.%d", version
);
146 udev_handle
= dlopen(name
, flags
);
151 if ((flags
& RTLD_NOLOAD
) == 0)
154 flags
&= ~RTLD_NOLOAD
;
157 log_(_LOADER_WARNING
,
158 "Couldn't dlopen libudev.so.1 or "
159 "libudev.so.0, driver detection may be broken.\n");
163 static int dlsym_failed
= 0;
166 checked_dlsym(void *dlopen_handle
, const char *name
)
168 void *result
= dlsym(dlopen_handle
, name
);
174 #define UDEV_SYMBOL(ret, name, args) \
175 ret (*name) args = checked_dlsym(udev_dlopen_handle(), #name);
178 static inline struct udev_device
*
179 udev_device_new_from_fd(struct udev
*udev
, int fd
)
181 struct udev_device
*device
;
183 UDEV_SYMBOL(struct udev_device
*, udev_device_new_from_devnum
,
184 (struct udev
*udev
, char type
, dev_t devnum
));
189 if (fstat(fd
, &buf
) < 0) {
190 log_(_LOADER_WARNING
, "MESA-LOADER: failed to stat fd %d\n", fd
);
194 device
= udev_device_new_from_devnum(udev
, 'c', buf
.st_rdev
);
195 if (device
== NULL
) {
196 log_(_LOADER_WARNING
,
197 "MESA-LOADER: could not create udev device for fd %d\n", fd
);
205 libudev_get_pci_id_for_fd(int fd
, int *vendor_id
, int *chip_id
)
207 struct udev
*udev
= NULL
;
208 struct udev_device
*device
= NULL
, *parent
;
210 UDEV_SYMBOL(struct udev
*, udev_new
, (void));
211 UDEV_SYMBOL(struct udev_device
*, udev_device_get_parent
,
212 (struct udev_device
*));
213 UDEV_SYMBOL(const char *, udev_device_get_property_value
,
214 (struct udev_device
*, const char *));
215 UDEV_SYMBOL(struct udev_device
*, udev_device_unref
,
216 (struct udev_device
*));
217 UDEV_SYMBOL(struct udev
*, udev_unref
, (struct udev
*));
225 device
= udev_device_new_from_fd(udev
, fd
);
229 parent
= udev_device_get_parent(device
);
230 if (parent
== NULL
) {
231 log_(_LOADER_WARNING
, "MESA-LOADER: could not get parent device\n");
235 pci_id
= udev_device_get_property_value(parent
, "PCI_ID");
236 if (pci_id
== NULL
) {
237 log_(_LOADER_INFO
, "MESA-LOADER: no PCI ID\n");
240 } else if (sscanf(pci_id
, "%x:%x", vendor_id
, chip_id
) != 2) {
241 log_(_LOADER_WARNING
, "MESA-LOADER: malformed PCI ID\n");
248 udev_device_unref(device
);
252 return (*chip_id
>= 0);
256 get_render_node_from_id_path_tag(struct udev
*udev
,
260 struct udev_device
*device
;
261 struct udev_enumerate
*e
;
262 struct udev_list_entry
*entry
;
263 const char *path
, *id_path_tag_tmp
;
266 UDEV_SYMBOL(struct udev_enumerate
*, udev_enumerate_new
,
268 UDEV_SYMBOL(int, udev_enumerate_add_match_subsystem
,
269 (struct udev_enumerate
*, const char *));
270 UDEV_SYMBOL(int, udev_enumerate_add_match_sysname
,
271 (struct udev_enumerate
*, const char *));
272 UDEV_SYMBOL(int, udev_enumerate_scan_devices
,
273 (struct udev_enumerate
*));
274 UDEV_SYMBOL(struct udev_list_entry
*, udev_enumerate_get_list_entry
,
275 (struct udev_enumerate
*));
276 UDEV_SYMBOL(void, udev_enumerate_unref
,
277 (struct udev_enumerate
*));
278 UDEV_SYMBOL(struct udev_list_entry
*, udev_list_entry_get_next
,
279 (struct udev_list_entry
*));
280 UDEV_SYMBOL(const char *, udev_list_entry_get_name
,
281 (struct udev_list_entry
*));
282 UDEV_SYMBOL(struct udev_device
*, udev_device_new_from_syspath
,
283 (struct udev
*, const char *));
284 UDEV_SYMBOL(const char *, udev_device_get_property_value
,
285 (struct udev_device
*, const char *));
286 UDEV_SYMBOL(const char *, udev_device_get_devnode
,
287 (struct udev_device
*));
288 UDEV_SYMBOL(struct udev_device
*, udev_device_unref
,
289 (struct udev_device
*));
291 e
= udev_enumerate_new(udev
);
292 udev_enumerate_add_match_subsystem(e
, "drm");
293 udev_enumerate_add_match_sysname(e
, "render*");
295 udev_enumerate_scan_devices(e
);
296 udev_list_entry_foreach(entry
, udev_enumerate_get_list_entry(e
)) {
297 path
= udev_list_entry_get_name(entry
);
298 device
= udev_device_new_from_syspath(udev
, path
);
301 id_path_tag_tmp
= udev_device_get_property_value(device
, "ID_PATH_TAG");
302 if (id_path_tag_tmp
) {
303 if ((!another_tag
&& !strcmp(id_path_tag
, id_path_tag_tmp
)) ||
304 (another_tag
&& strcmp(id_path_tag
, id_path_tag_tmp
))) {
309 udev_device_unref(device
);
312 udev_enumerate_unref(e
);
315 path_res
= strdup(udev_device_get_devnode(device
));
316 udev_device_unref(device
);
323 get_id_path_tag_from_fd(struct udev
*udev
, int fd
)
325 struct udev_device
*device
;
326 const char *id_path_tag_tmp
;
328 UDEV_SYMBOL(const char *, udev_device_get_property_value
,
329 (struct udev_device
*, const char *));
330 UDEV_SYMBOL(struct udev_device
*, udev_device_unref
,
331 (struct udev_device
*));
333 device
= udev_device_new_from_fd(udev
, fd
);
337 id_path_tag_tmp
= udev_device_get_property_value(device
, "ID_PATH_TAG");
338 if (!id_path_tag_tmp
)
341 id_path_tag
= strdup(id_path_tag_tmp
);
343 udev_device_unref(device
);
348 const char __driConfigOptionsLoader
[] =
350 DRI_CONF_SECTION_INITIALIZATION
351 DRI_CONF_DEVICE_ID_PATH_TAG()
356 int loader_get_user_preferred_fd(int default_fd
, int *different_device
)
360 driOptionCache defaultInitOptions
;
361 driOptionCache userInitOptions
;
363 const char *dri_prime
= getenv("DRI_PRIME");
365 int is_different_device
= 0, fd
= default_fd
;
366 char *default_device_id_path_tag
;
367 char *device_name
= NULL
;
368 char another_tag
= 0;
369 UDEV_SYMBOL(struct udev
*, udev_new
, (void));
370 UDEV_SYMBOL(struct udev
*, udev_unref
, (struct udev
*));
373 prime
= strdup(dri_prime
);
376 driParseOptionInfo(&defaultInitOptions
, __driConfigOptionsLoader
);
377 driParseConfigFiles(&userInitOptions
, &defaultInitOptions
, 0, "loader");
378 if (driCheckOption(&userInitOptions
, "device_id", DRI_STRING
))
379 prime
= strdup(driQueryOptionstr(&userInitOptions
, "device_id"));
380 driDestroyOptionCache(&userInitOptions
);
381 driDestroyOptionInfo(&defaultInitOptions
);
386 *different_device
= 0;
394 default_device_id_path_tag
= get_id_path_tag_from_fd(udev
, default_fd
);
395 if (!default_device_id_path_tag
)
398 is_different_device
= 1;
399 /* two format are supported:
400 * "1": choose any other card than the card used by default.
401 * id_path_tag: (for example "pci-0000_02_00_0") choose the card
402 * with this id_path_tag.
404 if (!strcmp(prime
,"1")) {
406 prime
= strdup(default_device_id_path_tag
);
407 /* request a card with a different card than the default card */
409 } else if (!strcmp(default_device_id_path_tag
, prime
))
410 /* we are to get a new fd (render-node) of the same device */
411 is_different_device
= 0;
413 device_name
= get_render_node_from_id_path_tag(udev
,
416 if (device_name
== NULL
) {
417 is_different_device
= 0;
418 goto default_device_clean
;
421 fd
= loader_open_device(device_name
);
426 is_different_device
= 0;
430 default_device_clean
:
431 free(default_device_id_path_tag
);
437 *different_device
= is_different_device
;
441 int loader_get_user_preferred_fd(int default_fd
, int *different_device
)
443 *different_device
= 0;
448 #if defined(HAVE_SYSFS)
450 dev_node_from_fd(int fd
, unsigned int *maj
, unsigned int *min
)
454 if (fstat(fd
, &buf
) < 0) {
455 log_(_LOADER_WARNING
, "MESA-LOADER: failed to stat fd %d\n", fd
);
459 if (!S_ISCHR(buf
.st_mode
)) {
460 log_(_LOADER_WARNING
, "MESA-LOADER: fd %d not a character device\n", fd
);
464 *maj
= major(buf
.st_rdev
);
465 *min
= minor(buf
.st_rdev
);
471 sysfs_get_pci_id_for_fd(int fd
, int *vendor_id
, int *chip_id
)
473 unsigned int maj
, min
;
477 if (dev_node_from_fd(fd
, &maj
, &min
) < 0) {
482 snprintf(buf
, sizeof(buf
), "/sys/dev/char/%d:%d/device/vendor", maj
, min
);
483 if (!(f
= fopen(buf
, "r"))) {
487 if (fscanf(f
, "%x", vendor_id
) != 1) {
493 snprintf(buf
, sizeof(buf
), "/sys/dev/char/%d:%d/device/device", maj
, min
);
494 if (!(f
= fopen(buf
, "r"))) {
498 if (fscanf(f
, "%x", chip_id
) != 1) {
508 #if defined(HAVE_LIBDRM)
510 #include <i915_drm.h>
512 #include <radeon_drm.h>
515 drm_get_pci_id_for_fd(int fd
, int *vendor_id
, int *chip_id
)
517 drmVersionPtr version
;
521 version
= drmGetVersion(fd
);
523 log_(_LOADER_WARNING
, "MESA-LOADER: invalid drm fd\n");
526 if (!version
->name
) {
527 log_(_LOADER_WARNING
, "MESA-LOADER: unable to determine the driver name\n");
528 drmFreeVersion(version
);
532 if (strcmp(version
->name
, "i915") == 0) {
533 struct drm_i915_getparam gp
;
538 memset(&gp
, 0, sizeof(gp
));
539 gp
.param
= I915_PARAM_CHIPSET_ID
;
541 ret
= drmCommandWriteRead(fd
, DRM_I915_GETPARAM
, &gp
, sizeof(gp
));
543 log_(_LOADER_WARNING
, "MESA-LOADER: failed to get param for i915\n");
547 else if (strcmp(version
->name
, "radeon") == 0) {
548 struct drm_radeon_info info
;
553 memset(&info
, 0, sizeof(info
));
554 info
.request
= RADEON_INFO_DEVICE_ID
;
555 info
.value
= (unsigned long) chip_id
;
556 ret
= drmCommandWriteRead(fd
, DRM_RADEON_INFO
, &info
, sizeof(info
));
558 log_(_LOADER_WARNING
, "MESA-LOADER: failed to get info for radeon\n");
562 else if (strcmp(version
->name
, "nouveau") == 0) {
567 else if (strcmp(version
->name
, "vmwgfx") == 0) {
573 drmFreeVersion(version
);
575 return (*chip_id
>= 0);
581 loader_get_pci_id_for_fd(int fd
, int *vendor_id
, int *chip_id
)
584 if (libudev_get_pci_id_for_fd(fd
, vendor_id
, chip_id
))
588 if (sysfs_get_pci_id_for_fd(fd
, vendor_id
, chip_id
))
592 if (drm_get_pci_id_for_fd(fd
, vendor_id
, chip_id
))
601 libudev_get_device_name_for_fd(int fd
)
603 char *device_name
= NULL
;
605 struct udev_device
*device
;
606 const char *const_device_name
;
607 UDEV_SYMBOL(struct udev
*, udev_new
, (void));
608 UDEV_SYMBOL(const char *, udev_device_get_devnode
,
609 (struct udev_device
*));
610 UDEV_SYMBOL(struct udev_device
*, udev_device_unref
,
611 (struct udev_device
*));
612 UDEV_SYMBOL(struct udev
*, udev_unref
, (struct udev
*));
618 device
= udev_device_new_from_fd(udev
, fd
);
622 const_device_name
= udev_device_get_devnode(device
);
623 if (!const_device_name
)
625 device_name
= strdup(const_device_name
);
628 udev_device_unref(device
);
637 sysfs_get_device_name_for_fd(int fd
)
639 char *device_name
= NULL
;
640 unsigned int maj
, min
;
643 static const char match
[9] = "\0DEVNAME=";
646 if (dev_node_from_fd(fd
, &maj
, &min
) < 0)
649 snprintf(buf
, sizeof(buf
), "/sys/dev/char/%d:%d/uevent", maj
, min
);
650 if (!(f
= fopen(buf
, "r")))
653 while (expected
< sizeof(match
)) {
659 } else if (c
== match
[expected
] )
665 strcpy(buf
, "/dev/");
666 if (fgets(buf
+ 5, sizeof(buf
) - 5, f
))
667 device_name
= strdup(buf
);
676 loader_get_device_name_for_fd(int fd
)
681 if ((result
= libudev_get_device_name_for_fd(fd
)))
685 if ((result
= sysfs_get_device_name_for_fd(fd
)))
692 loader_get_driver_for_fd(int fd
, unsigned driver_types
)
694 int vendor_id
, chip_id
, i
, j
;
698 driver_types
= _LOADER_GALLIUM
| _LOADER_DRI
;
700 if (!loader_get_pci_id_for_fd(fd
, &vendor_id
, &chip_id
)) {
703 /* fallback to drmGetVersion(): */
704 drmVersionPtr version
= drmGetVersion(fd
);
707 log_(_LOADER_WARNING
, "failed to get driver name for fd %d\n", fd
);
711 driver
= strndup(version
->name
, version
->name_len
);
712 log_(_LOADER_INFO
, "using driver %s for %d\n", driver
, fd
);
714 drmFreeVersion(version
);
720 for (i
= 0; driver_map
[i
].driver
; i
++) {
721 if (vendor_id
!= driver_map
[i
].vendor_id
)
724 if (!(driver_types
& driver_map
[i
].driver_types
))
727 if (driver_map
[i
].predicate
&& !driver_map
[i
].predicate(fd
))
730 if (driver_map
[i
].num_chips_ids
== -1) {
731 driver
= strdup(driver_map
[i
].driver
);
735 for (j
= 0; j
< driver_map
[i
].num_chips_ids
; j
++)
736 if (driver_map
[i
].chip_ids
[j
] == chip_id
) {
737 driver
= strdup(driver_map
[i
].driver
);
743 log_(driver
? _LOADER_DEBUG
: _LOADER_WARNING
,
744 "pci id for fd %d: %04x:%04x, driver %s\n",
745 fd
, vendor_id
, chip_id
, driver
);
750 loader_set_logger(void (*logger
)(int level
, const char *fmt
, ...))