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>
78 #include "xmlconfig.h"
84 #include <sys/types.h>
88 #ifndef __NOT_HAVE_DRM_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
;
110 static void *udev_handle
= NULL
;
113 udev_dlopen_handle(void)
116 udev_handle
= dlopen("libudev.so.1", RTLD_LOCAL
| RTLD_LAZY
);
119 /* libudev.so.1 changed the return types of the two unref functions
120 * from voids to pointers. We don't use those return values, and the
121 * only ABI I've heard that cares about this kind of change (calling
122 * a function with a void * return that actually only returns void)
125 udev_handle
= dlopen("libudev.so.0", RTLD_LOCAL
| RTLD_LAZY
);
128 log_(_LOADER_WARNING
, "Couldn't dlopen libudev.so.1 or "
129 "libudev.so.0, driver detection may be broken.\n");
137 static int dlsym_failed
= 0;
140 checked_dlsym(void *dlopen_handle
, const char *name
)
142 void *result
= dlsym(dlopen_handle
, name
);
148 #define UDEV_SYMBOL(ret, name, args) \
149 ret (*name) args = checked_dlsym(udev_dlopen_handle(), #name);
152 static inline struct udev_device
*
153 udev_device_new_from_fd(struct udev
*udev
, int fd
)
155 struct udev_device
*device
;
157 UDEV_SYMBOL(struct udev_device
*, udev_device_new_from_devnum
,
158 (struct udev
*udev
, char type
, dev_t devnum
));
163 if (fstat(fd
, &buf
) < 0) {
164 log_(_LOADER_WARNING
, "MESA-LOADER: failed to stat fd %d\n", fd
);
168 device
= udev_device_new_from_devnum(udev
, 'c', buf
.st_rdev
);
169 if (device
== NULL
) {
170 log_(_LOADER_WARNING
,
171 "MESA-LOADER: could not create udev device for fd %d\n", fd
);
179 libudev_get_pci_id_for_fd(int fd
, int *vendor_id
, int *chip_id
)
181 struct udev
*udev
= NULL
;
182 struct udev_device
*device
= NULL
, *parent
;
184 UDEV_SYMBOL(struct udev
*, udev_new
, (void));
185 UDEV_SYMBOL(struct udev_device
*, udev_device_get_parent
,
186 (struct udev_device
*));
187 UDEV_SYMBOL(const char *, udev_device_get_property_value
,
188 (struct udev_device
*, const char *));
189 UDEV_SYMBOL(struct udev_device
*, udev_device_unref
,
190 (struct udev_device
*));
191 UDEV_SYMBOL(struct udev
*, udev_unref
, (struct udev
*));
199 device
= udev_device_new_from_fd(udev
, fd
);
203 parent
= udev_device_get_parent(device
);
204 if (parent
== NULL
) {
205 log_(_LOADER_WARNING
, "MESA-LOADER: could not get parent device\n");
209 pci_id
= udev_device_get_property_value(parent
, "PCI_ID");
210 if (pci_id
== NULL
||
211 sscanf(pci_id
, "%x:%x", vendor_id
, chip_id
) != 2) {
212 log_(_LOADER_WARNING
, "MESA-LOADER: malformed or no PCI ID\n");
219 udev_device_unref(device
);
223 return (*chip_id
>= 0);
227 get_render_node_from_id_path_tag(struct udev
*udev
,
231 struct udev_device
*device
;
232 struct udev_enumerate
*e
;
233 struct udev_list_entry
*entry
;
234 const char *path
, *id_path_tag_tmp
;
237 UDEV_SYMBOL(struct udev_enumerate
*, udev_enumerate_new
,
239 UDEV_SYMBOL(int, udev_enumerate_add_match_subsystem
,
240 (struct udev_enumerate
*, const char *));
241 UDEV_SYMBOL(int, udev_enumerate_add_match_sysname
,
242 (struct udev_enumerate
*, const char *));
243 UDEV_SYMBOL(int, udev_enumerate_scan_devices
,
244 (struct udev_enumerate
*));
245 UDEV_SYMBOL(struct udev_list_entry
*, udev_enumerate_get_list_entry
,
246 (struct udev_enumerate
*));
247 UDEV_SYMBOL(struct udev_list_entry
*, udev_list_entry_get_next
,
248 (struct udev_list_entry
*));
249 UDEV_SYMBOL(const char *, udev_list_entry_get_name
,
250 (struct udev_list_entry
*));
251 UDEV_SYMBOL(struct udev_device
*, udev_device_new_from_syspath
,
252 (struct udev
*, const char *));
253 UDEV_SYMBOL(const char *, udev_device_get_property_value
,
254 (struct udev_device
*, const char *));
255 UDEV_SYMBOL(const char *, udev_device_get_devnode
,
256 (struct udev_device
*));
257 UDEV_SYMBOL(struct udev_device
*, udev_device_unref
,
258 (struct udev_device
*));
260 e
= udev_enumerate_new(udev
);
261 udev_enumerate_add_match_subsystem(e
, "drm");
262 udev_enumerate_add_match_sysname(e
, "render*");
264 udev_enumerate_scan_devices(e
);
265 udev_list_entry_foreach(entry
, udev_enumerate_get_list_entry(e
)) {
266 path
= udev_list_entry_get_name(entry
);
267 device
= udev_device_new_from_syspath(udev
, path
);
270 id_path_tag_tmp
= udev_device_get_property_value(device
, "ID_PATH_TAG");
271 if (id_path_tag_tmp
) {
272 if ((!another_tag
&& !strcmp(id_path_tag
, id_path_tag_tmp
)) ||
273 (another_tag
&& strcmp(id_path_tag
, id_path_tag_tmp
))) {
278 udev_device_unref(device
);
282 path_res
= strdup(udev_device_get_devnode(device
));
283 udev_device_unref(device
);
290 get_id_path_tag_from_fd(struct udev
*udev
, int fd
)
292 struct udev_device
*device
;
293 const char *id_path_tag_tmp
;
295 UDEV_SYMBOL(const char *, udev_device_get_property_value
,
296 (struct udev_device
*, const char *));
297 UDEV_SYMBOL(struct udev_device
*, udev_device_unref
,
298 (struct udev_device
*));
300 device
= udev_device_new_from_fd(udev
, fd
);
304 id_path_tag_tmp
= udev_device_get_property_value(device
, "ID_PATH_TAG");
305 if (!id_path_tag_tmp
)
308 id_path_tag
= strdup(id_path_tag_tmp
);
310 udev_device_unref(device
);
315 drm_open_device(const char *device_name
)
319 fd
= open(device_name
, O_RDWR
| O_CLOEXEC
);
320 if (fd
== -1 && errno
== EINVAL
)
323 fd
= open(device_name
, O_RDWR
);
325 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
331 const char __driConfigOptionsLoader
[] =
333 DRI_CONF_SECTION_INITIALIZATION
334 DRI_CONF_DEVICE_ID_PATH_TAG()
339 int loader_get_user_preferred_fd(int default_fd
, int *different_device
)
343 driOptionCache defaultInitOptions
;
344 driOptionCache userInitOptions
;
346 const char *dri_prime
= getenv("DRI_PRIME");
348 int is_different_device
= 0, fd
= default_fd
;
349 char *default_device_id_path_tag
;
350 char *device_name
= NULL
;
351 char another_tag
= 0;
352 UDEV_SYMBOL(struct udev
*, udev_new
, (void));
353 UDEV_SYMBOL(struct udev
*, udev_unref
, (struct udev
*));
356 prime
= strdup(dri_prime
);
359 driParseOptionInfo(&defaultInitOptions
, __driConfigOptionsLoader
);
360 driParseConfigFiles(&userInitOptions
, &defaultInitOptions
, 0, "loader");
361 if (driCheckOption(&userInitOptions
, "device_id", DRI_STRING
))
362 prime
= strdup(driQueryOptionstr(&userInitOptions
, "device_id"));
363 driDestroyOptionCache(&userInitOptions
);
364 driDestroyOptionInfo(&defaultInitOptions
);
369 *different_device
= 0;
377 default_device_id_path_tag
= get_id_path_tag_from_fd(udev
, default_fd
);
378 if (!default_device_id_path_tag
)
381 is_different_device
= 1;
382 /* two format are supported:
383 * "1": choose any other card than the card used by default.
384 * id_path_tag: (for example "pci-0000_02_00_0") choose the card
385 * with this id_path_tag.
387 if (!strcmp(prime
,"1")) {
389 prime
= strdup(default_device_id_path_tag
);
390 /* request a card with a different card than the default card */
392 } else if (!strcmp(default_device_id_path_tag
, prime
))
393 /* we are to get a new fd (render-node) of the same device */
394 is_different_device
= 0;
396 device_name
= get_render_node_from_id_path_tag(udev
,
399 if (device_name
== NULL
) {
400 is_different_device
= 0;
401 goto default_device_clean
;
404 fd
= drm_open_device(device_name
);
409 is_different_device
= 0;
413 default_device_clean
:
414 free(default_device_id_path_tag
);
420 *different_device
= is_different_device
;
424 int loader_get_user_preferred_fd(int default_fd
, int *different_device
)
426 *different_device
= 0;
431 #if defined(HAVE_SYSFS)
433 dev_node_from_fd(int fd
, unsigned int *maj
, unsigned int *min
)
437 if (fstat(fd
, &buf
) < 0) {
438 log_(_LOADER_WARNING
, "MESA-LOADER: failed to stat fd %d\n", fd
);
442 if (!S_ISCHR(buf
.st_mode
)) {
443 log_(_LOADER_WARNING
, "MESA-LOADER: fd %d not a character device\n", fd
);
447 *maj
= major(buf
.st_rdev
);
448 *min
= minor(buf
.st_rdev
);
454 sysfs_get_pci_id_for_fd(int fd
, int *vendor_id
, int *chip_id
)
456 unsigned int maj
, min
;
460 if (dev_node_from_fd(fd
, &maj
, &min
) < 0) {
465 snprintf(buf
, sizeof(buf
), "/sys/dev/char/%d:%d/device/vendor", maj
, min
);
466 if (!(f
= fopen(buf
, "r"))) {
470 if (fscanf(f
, "%x", vendor_id
) != 1) {
476 snprintf(buf
, sizeof(buf
), "/sys/dev/char/%d:%d/device/device", maj
, min
);
477 if (!(f
= fopen(buf
, "r"))) {
481 if (fscanf(f
, "%x", chip_id
) != 1) {
491 #if !defined(__NOT_HAVE_DRM_H)
493 #include <i915_drm.h>
495 #include <radeon_drm.h>
498 drm_get_pci_id_for_fd(int fd
, int *vendor_id
, int *chip_id
)
500 drmVersionPtr version
;
504 version
= drmGetVersion(fd
);
506 log_(_LOADER_WARNING
, "MESA-LOADER: invalid drm fd\n");
509 if (!version
->name
) {
510 log_(_LOADER_WARNING
, "MESA-LOADER: unable to determine the driver name\n");
511 drmFreeVersion(version
);
515 if (strcmp(version
->name
, "i915") == 0) {
516 struct drm_i915_getparam gp
;
521 memset(&gp
, 0, sizeof(gp
));
522 gp
.param
= I915_PARAM_CHIPSET_ID
;
524 ret
= drmCommandWriteRead(fd
, DRM_I915_GETPARAM
, &gp
, sizeof(gp
));
526 log_(_LOADER_WARNING
, "MESA-LOADER: failed to get param for i915\n");
530 else if (strcmp(version
->name
, "radeon") == 0) {
531 struct drm_radeon_info info
;
536 memset(&info
, 0, sizeof(info
));
537 info
.request
= RADEON_INFO_DEVICE_ID
;
538 info
.value
= (unsigned long) chip_id
;
539 ret
= drmCommandWriteRead(fd
, DRM_RADEON_INFO
, &info
, sizeof(info
));
541 log_(_LOADER_WARNING
, "MESA-LOADER: failed to get info for radeon\n");
545 else if (strcmp(version
->name
, "nouveau") == 0) {
550 else if (strcmp(version
->name
, "vmwgfx") == 0) {
556 drmFreeVersion(version
);
558 return (*chip_id
>= 0);
564 loader_get_pci_id_for_fd(int fd
, int *vendor_id
, int *chip_id
)
567 if (libudev_get_pci_id_for_fd(fd
, vendor_id
, chip_id
))
571 if (sysfs_get_pci_id_for_fd(fd
, vendor_id
, chip_id
))
574 #if !defined(__NOT_HAVE_DRM_H)
575 if (drm_get_pci_id_for_fd(fd
, vendor_id
, chip_id
))
584 libudev_get_device_name_for_fd(int fd
)
586 char *device_name
= NULL
;
588 struct udev_device
*device
;
589 const char *const_device_name
;
590 UDEV_SYMBOL(struct udev
*, udev_new
, (void));
591 UDEV_SYMBOL(const char *, udev_device_get_devnode
,
592 (struct udev_device
*));
593 UDEV_SYMBOL(struct udev_device
*, udev_device_unref
,
594 (struct udev_device
*));
595 UDEV_SYMBOL(struct udev
*, udev_unref
, (struct udev
*));
601 device
= udev_device_new_from_fd(udev
, fd
);
605 const_device_name
= udev_device_get_devnode(device
);
606 if (!const_device_name
)
608 device_name
= strdup(const_device_name
);
611 udev_device_unref(device
);
620 sysfs_get_device_name_for_fd(int fd
)
622 char *device_name
= NULL
;
623 unsigned int maj
, min
;
626 static const char match
[9] = "\0DEVNAME=";
629 if (dev_node_from_fd(fd
, &maj
, &min
) < 0)
632 snprintf(buf
, sizeof(buf
), "/sys/dev/char/%d:%d/uevent", maj
, min
);
633 if (!(f
= fopen(buf
, "r")))
636 while (expected
< sizeof(match
)) {
642 } else if (c
== match
[expected
] )
648 strcpy(buf
, "/dev/");
649 if (fgets(buf
+ 5, sizeof(buf
) - 5, f
))
650 device_name
= strdup(buf
);
659 loader_get_device_name_for_fd(int fd
)
664 if ((result
= libudev_get_device_name_for_fd(fd
)))
668 if ((result
= sysfs_get_device_name_for_fd(fd
)))
675 loader_get_driver_for_fd(int fd
, unsigned driver_types
)
677 int vendor_id
, chip_id
, i
, j
;
681 driver_types
= _LOADER_GALLIUM
| _LOADER_DRI
;
683 if (!loader_get_pci_id_for_fd(fd
, &vendor_id
, &chip_id
)) {
685 #ifndef __NOT_HAVE_DRM_H
686 /* fallback to drmGetVersion(): */
687 drmVersionPtr version
= drmGetVersion(fd
);
690 log_(_LOADER_WARNING
, "failed to get driver name for fd %d\n", fd
);
694 driver
= strndup(version
->name
, version
->name_len
);
695 log_(_LOADER_INFO
, "using driver %s for %d\n", driver
, fd
);
697 drmFreeVersion(version
);
703 for (i
= 0; driver_map
[i
].driver
; i
++) {
704 if (vendor_id
!= driver_map
[i
].vendor_id
)
707 if (!(driver_types
& driver_map
[i
].driver_types
))
710 if (driver_map
[i
].predicate
&& !driver_map
[i
].predicate(fd
))
713 if (driver_map
[i
].num_chips_ids
== -1) {
714 driver
= strdup(driver_map
[i
].driver
);
718 for (j
= 0; j
< driver_map
[i
].num_chips_ids
; j
++)
719 if (driver_map
[i
].chip_ids
[j
] == chip_id
) {
720 driver
= strdup(driver_map
[i
].driver
);
726 log_(driver
? _LOADER_DEBUG
: _LOADER_WARNING
,
727 "pci id for fd %d: %04x:%04x, driver %s\n",
728 fd
, vendor_id
, chip_id
, driver
);
733 loader_set_logger(void (*logger
)(int level
, const char *fmt
, ...))