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>
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
;
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(struct udev_list_entry
*, udev_list_entry_get_next
,
277 (struct udev_list_entry
*));
278 UDEV_SYMBOL(const char *, udev_list_entry_get_name
,
279 (struct udev_list_entry
*));
280 UDEV_SYMBOL(struct udev_device
*, udev_device_new_from_syspath
,
281 (struct udev
*, const char *));
282 UDEV_SYMBOL(const char *, udev_device_get_property_value
,
283 (struct udev_device
*, const char *));
284 UDEV_SYMBOL(const char *, udev_device_get_devnode
,
285 (struct udev_device
*));
286 UDEV_SYMBOL(struct udev_device
*, udev_device_unref
,
287 (struct udev_device
*));
289 e
= udev_enumerate_new(udev
);
290 udev_enumerate_add_match_subsystem(e
, "drm");
291 udev_enumerate_add_match_sysname(e
, "render*");
293 udev_enumerate_scan_devices(e
);
294 udev_list_entry_foreach(entry
, udev_enumerate_get_list_entry(e
)) {
295 path
= udev_list_entry_get_name(entry
);
296 device
= udev_device_new_from_syspath(udev
, path
);
299 id_path_tag_tmp
= udev_device_get_property_value(device
, "ID_PATH_TAG");
300 if (id_path_tag_tmp
) {
301 if ((!another_tag
&& !strcmp(id_path_tag
, id_path_tag_tmp
)) ||
302 (another_tag
&& strcmp(id_path_tag
, id_path_tag_tmp
))) {
307 udev_device_unref(device
);
311 path_res
= strdup(udev_device_get_devnode(device
));
312 udev_device_unref(device
);
319 get_id_path_tag_from_fd(struct udev
*udev
, int fd
)
321 struct udev_device
*device
;
322 const char *id_path_tag_tmp
;
324 UDEV_SYMBOL(const char *, udev_device_get_property_value
,
325 (struct udev_device
*, const char *));
326 UDEV_SYMBOL(struct udev_device
*, udev_device_unref
,
327 (struct udev_device
*));
329 device
= udev_device_new_from_fd(udev
, fd
);
333 id_path_tag_tmp
= udev_device_get_property_value(device
, "ID_PATH_TAG");
334 if (!id_path_tag_tmp
)
337 id_path_tag
= strdup(id_path_tag_tmp
);
339 udev_device_unref(device
);
344 const char __driConfigOptionsLoader
[] =
346 DRI_CONF_SECTION_INITIALIZATION
347 DRI_CONF_DEVICE_ID_PATH_TAG()
352 int loader_get_user_preferred_fd(int default_fd
, int *different_device
)
356 driOptionCache defaultInitOptions
;
357 driOptionCache userInitOptions
;
359 const char *dri_prime
= getenv("DRI_PRIME");
361 int is_different_device
= 0, fd
= default_fd
;
362 char *default_device_id_path_tag
;
363 char *device_name
= NULL
;
364 char another_tag
= 0;
365 UDEV_SYMBOL(struct udev
*, udev_new
, (void));
366 UDEV_SYMBOL(struct udev
*, udev_unref
, (struct udev
*));
369 prime
= strdup(dri_prime
);
372 driParseOptionInfo(&defaultInitOptions
, __driConfigOptionsLoader
);
373 driParseConfigFiles(&userInitOptions
, &defaultInitOptions
, 0, "loader");
374 if (driCheckOption(&userInitOptions
, "device_id", DRI_STRING
))
375 prime
= strdup(driQueryOptionstr(&userInitOptions
, "device_id"));
376 driDestroyOptionCache(&userInitOptions
);
377 driDestroyOptionInfo(&defaultInitOptions
);
382 *different_device
= 0;
390 default_device_id_path_tag
= get_id_path_tag_from_fd(udev
, default_fd
);
391 if (!default_device_id_path_tag
)
394 is_different_device
= 1;
395 /* two format are supported:
396 * "1": choose any other card than the card used by default.
397 * id_path_tag: (for example "pci-0000_02_00_0") choose the card
398 * with this id_path_tag.
400 if (!strcmp(prime
,"1")) {
402 prime
= strdup(default_device_id_path_tag
);
403 /* request a card with a different card than the default card */
405 } else if (!strcmp(default_device_id_path_tag
, prime
))
406 /* we are to get a new fd (render-node) of the same device */
407 is_different_device
= 0;
409 device_name
= get_render_node_from_id_path_tag(udev
,
412 if (device_name
== NULL
) {
413 is_different_device
= 0;
414 goto default_device_clean
;
417 fd
= loader_open_device(device_name
);
422 is_different_device
= 0;
426 default_device_clean
:
427 free(default_device_id_path_tag
);
433 *different_device
= is_different_device
;
437 int loader_get_user_preferred_fd(int default_fd
, int *different_device
)
439 *different_device
= 0;
444 #if defined(HAVE_SYSFS)
446 dev_node_from_fd(int fd
, unsigned int *maj
, unsigned int *min
)
450 if (fstat(fd
, &buf
) < 0) {
451 log_(_LOADER_WARNING
, "MESA-LOADER: failed to stat fd %d\n", fd
);
455 if (!S_ISCHR(buf
.st_mode
)) {
456 log_(_LOADER_WARNING
, "MESA-LOADER: fd %d not a character device\n", fd
);
460 *maj
= major(buf
.st_rdev
);
461 *min
= minor(buf
.st_rdev
);
467 sysfs_get_pci_id_for_fd(int fd
, int *vendor_id
, int *chip_id
)
469 unsigned int maj
, min
;
473 if (dev_node_from_fd(fd
, &maj
, &min
) < 0) {
478 snprintf(buf
, sizeof(buf
), "/sys/dev/char/%d:%d/device/vendor", maj
, min
);
479 if (!(f
= fopen(buf
, "r"))) {
483 if (fscanf(f
, "%x", vendor_id
) != 1) {
489 snprintf(buf
, sizeof(buf
), "/sys/dev/char/%d:%d/device/device", maj
, min
);
490 if (!(f
= fopen(buf
, "r"))) {
494 if (fscanf(f
, "%x", chip_id
) != 1) {
504 #if !defined(__NOT_HAVE_DRM_H)
506 #include <i915_drm.h>
508 #include <radeon_drm.h>
511 drm_get_pci_id_for_fd(int fd
, int *vendor_id
, int *chip_id
)
513 drmVersionPtr version
;
517 version
= drmGetVersion(fd
);
519 log_(_LOADER_WARNING
, "MESA-LOADER: invalid drm fd\n");
522 if (!version
->name
) {
523 log_(_LOADER_WARNING
, "MESA-LOADER: unable to determine the driver name\n");
524 drmFreeVersion(version
);
528 if (strcmp(version
->name
, "i915") == 0) {
529 struct drm_i915_getparam gp
;
534 memset(&gp
, 0, sizeof(gp
));
535 gp
.param
= I915_PARAM_CHIPSET_ID
;
537 ret
= drmCommandWriteRead(fd
, DRM_I915_GETPARAM
, &gp
, sizeof(gp
));
539 log_(_LOADER_WARNING
, "MESA-LOADER: failed to get param for i915\n");
543 else if (strcmp(version
->name
, "radeon") == 0) {
544 struct drm_radeon_info info
;
549 memset(&info
, 0, sizeof(info
));
550 info
.request
= RADEON_INFO_DEVICE_ID
;
551 info
.value
= (unsigned long) chip_id
;
552 ret
= drmCommandWriteRead(fd
, DRM_RADEON_INFO
, &info
, sizeof(info
));
554 log_(_LOADER_WARNING
, "MESA-LOADER: failed to get info for radeon\n");
558 else if (strcmp(version
->name
, "nouveau") == 0) {
563 else if (strcmp(version
->name
, "vmwgfx") == 0) {
569 drmFreeVersion(version
);
571 return (*chip_id
>= 0);
577 loader_get_pci_id_for_fd(int fd
, int *vendor_id
, int *chip_id
)
580 if (libudev_get_pci_id_for_fd(fd
, vendor_id
, chip_id
))
584 if (sysfs_get_pci_id_for_fd(fd
, vendor_id
, chip_id
))
587 #if !defined(__NOT_HAVE_DRM_H)
588 if (drm_get_pci_id_for_fd(fd
, vendor_id
, chip_id
))
597 libudev_get_device_name_for_fd(int fd
)
599 char *device_name
= NULL
;
601 struct udev_device
*device
;
602 const char *const_device_name
;
603 UDEV_SYMBOL(struct udev
*, udev_new
, (void));
604 UDEV_SYMBOL(const char *, udev_device_get_devnode
,
605 (struct udev_device
*));
606 UDEV_SYMBOL(struct udev_device
*, udev_device_unref
,
607 (struct udev_device
*));
608 UDEV_SYMBOL(struct udev
*, udev_unref
, (struct udev
*));
614 device
= udev_device_new_from_fd(udev
, fd
);
618 const_device_name
= udev_device_get_devnode(device
);
619 if (!const_device_name
)
621 device_name
= strdup(const_device_name
);
624 udev_device_unref(device
);
633 sysfs_get_device_name_for_fd(int fd
)
635 char *device_name
= NULL
;
636 unsigned int maj
, min
;
639 static const char match
[9] = "\0DEVNAME=";
642 if (dev_node_from_fd(fd
, &maj
, &min
) < 0)
645 snprintf(buf
, sizeof(buf
), "/sys/dev/char/%d:%d/uevent", maj
, min
);
646 if (!(f
= fopen(buf
, "r")))
649 while (expected
< sizeof(match
)) {
655 } else if (c
== match
[expected
] )
661 strcpy(buf
, "/dev/");
662 if (fgets(buf
+ 5, sizeof(buf
) - 5, f
))
663 device_name
= strdup(buf
);
672 loader_get_device_name_for_fd(int fd
)
677 if ((result
= libudev_get_device_name_for_fd(fd
)))
681 if ((result
= sysfs_get_device_name_for_fd(fd
)))
688 loader_get_driver_for_fd(int fd
, unsigned driver_types
)
690 int vendor_id
, chip_id
, i
, j
;
694 driver_types
= _LOADER_GALLIUM
| _LOADER_DRI
;
696 if (!loader_get_pci_id_for_fd(fd
, &vendor_id
, &chip_id
)) {
698 #ifndef __NOT_HAVE_DRM_H
699 /* fallback to drmGetVersion(): */
700 drmVersionPtr version
= drmGetVersion(fd
);
703 log_(_LOADER_WARNING
, "failed to get driver name for fd %d\n", fd
);
707 driver
= strndup(version
->name
, version
->name_len
);
708 log_(_LOADER_INFO
, "using driver %s for %d\n", driver
, fd
);
710 drmFreeVersion(version
);
716 for (i
= 0; driver_map
[i
].driver
; i
++) {
717 if (vendor_id
!= driver_map
[i
].vendor_id
)
720 if (!(driver_types
& driver_map
[i
].driver_types
))
723 if (driver_map
[i
].predicate
&& !driver_map
[i
].predicate(fd
))
726 if (driver_map
[i
].num_chips_ids
== -1) {
727 driver
= strdup(driver_map
[i
].driver
);
731 for (j
= 0; j
< driver_map
[i
].num_chips_ids
; j
++)
732 if (driver_map
[i
].chip_ids
[j
] == chip_id
) {
733 driver
= strdup(driver_map
[i
].driver
);
739 log_(driver
? _LOADER_DEBUG
: _LOADER_WARNING
,
740 "pci id for fd %d: %04x:%04x, driver %s\n",
741 fd
, vendor_id
, chip_id
, driver
);
746 loader_set_logger(void (*logger
)(int level
, const char *fmt
, ...))