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
;
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 log_(_LOADER_INFO
, "MESA-LOADER: no PCI ID\n");
214 } else if (sscanf(pci_id
, "%x:%x", vendor_id
, chip_id
) != 2) {
215 log_(_LOADER_WARNING
, "MESA-LOADER: malformed PCI ID\n");
222 udev_device_unref(device
);
226 return (*chip_id
>= 0);
230 get_render_node_from_id_path_tag(struct udev
*udev
,
234 struct udev_device
*device
;
235 struct udev_enumerate
*e
;
236 struct udev_list_entry
*entry
;
237 const char *path
, *id_path_tag_tmp
;
240 UDEV_SYMBOL(struct udev_enumerate
*, udev_enumerate_new
,
242 UDEV_SYMBOL(int, udev_enumerate_add_match_subsystem
,
243 (struct udev_enumerate
*, const char *));
244 UDEV_SYMBOL(int, udev_enumerate_add_match_sysname
,
245 (struct udev_enumerate
*, const char *));
246 UDEV_SYMBOL(int, udev_enumerate_scan_devices
,
247 (struct udev_enumerate
*));
248 UDEV_SYMBOL(struct udev_list_entry
*, udev_enumerate_get_list_entry
,
249 (struct udev_enumerate
*));
250 UDEV_SYMBOL(struct udev_list_entry
*, udev_list_entry_get_next
,
251 (struct udev_list_entry
*));
252 UDEV_SYMBOL(const char *, udev_list_entry_get_name
,
253 (struct udev_list_entry
*));
254 UDEV_SYMBOL(struct udev_device
*, udev_device_new_from_syspath
,
255 (struct udev
*, const char *));
256 UDEV_SYMBOL(const char *, udev_device_get_property_value
,
257 (struct udev_device
*, const char *));
258 UDEV_SYMBOL(const char *, udev_device_get_devnode
,
259 (struct udev_device
*));
260 UDEV_SYMBOL(struct udev_device
*, udev_device_unref
,
261 (struct udev_device
*));
263 e
= udev_enumerate_new(udev
);
264 udev_enumerate_add_match_subsystem(e
, "drm");
265 udev_enumerate_add_match_sysname(e
, "render*");
267 udev_enumerate_scan_devices(e
);
268 udev_list_entry_foreach(entry
, udev_enumerate_get_list_entry(e
)) {
269 path
= udev_list_entry_get_name(entry
);
270 device
= udev_device_new_from_syspath(udev
, path
);
273 id_path_tag_tmp
= udev_device_get_property_value(device
, "ID_PATH_TAG");
274 if (id_path_tag_tmp
) {
275 if ((!another_tag
&& !strcmp(id_path_tag
, id_path_tag_tmp
)) ||
276 (another_tag
&& strcmp(id_path_tag
, id_path_tag_tmp
))) {
281 udev_device_unref(device
);
285 path_res
= strdup(udev_device_get_devnode(device
));
286 udev_device_unref(device
);
293 get_id_path_tag_from_fd(struct udev
*udev
, int fd
)
295 struct udev_device
*device
;
296 const char *id_path_tag_tmp
;
298 UDEV_SYMBOL(const char *, udev_device_get_property_value
,
299 (struct udev_device
*, const char *));
300 UDEV_SYMBOL(struct udev_device
*, udev_device_unref
,
301 (struct udev_device
*));
303 device
= udev_device_new_from_fd(udev
, fd
);
307 id_path_tag_tmp
= udev_device_get_property_value(device
, "ID_PATH_TAG");
308 if (!id_path_tag_tmp
)
311 id_path_tag
= strdup(id_path_tag_tmp
);
313 udev_device_unref(device
);
318 drm_open_device(const char *device_name
)
322 fd
= open(device_name
, O_RDWR
| O_CLOEXEC
);
323 if (fd
== -1 && errno
== EINVAL
)
326 fd
= open(device_name
, O_RDWR
);
328 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
334 const char __driConfigOptionsLoader
[] =
336 DRI_CONF_SECTION_INITIALIZATION
337 DRI_CONF_DEVICE_ID_PATH_TAG()
342 int loader_get_user_preferred_fd(int default_fd
, int *different_device
)
346 driOptionCache defaultInitOptions
;
347 driOptionCache userInitOptions
;
349 const char *dri_prime
= getenv("DRI_PRIME");
351 int is_different_device
= 0, fd
= default_fd
;
352 char *default_device_id_path_tag
;
353 char *device_name
= NULL
;
354 char another_tag
= 0;
355 UDEV_SYMBOL(struct udev
*, udev_new
, (void));
356 UDEV_SYMBOL(struct udev
*, udev_unref
, (struct udev
*));
359 prime
= strdup(dri_prime
);
362 driParseOptionInfo(&defaultInitOptions
, __driConfigOptionsLoader
);
363 driParseConfigFiles(&userInitOptions
, &defaultInitOptions
, 0, "loader");
364 if (driCheckOption(&userInitOptions
, "device_id", DRI_STRING
))
365 prime
= strdup(driQueryOptionstr(&userInitOptions
, "device_id"));
366 driDestroyOptionCache(&userInitOptions
);
367 driDestroyOptionInfo(&defaultInitOptions
);
372 *different_device
= 0;
380 default_device_id_path_tag
= get_id_path_tag_from_fd(udev
, default_fd
);
381 if (!default_device_id_path_tag
)
384 is_different_device
= 1;
385 /* two format are supported:
386 * "1": choose any other card than the card used by default.
387 * id_path_tag: (for example "pci-0000_02_00_0") choose the card
388 * with this id_path_tag.
390 if (!strcmp(prime
,"1")) {
392 prime
= strdup(default_device_id_path_tag
);
393 /* request a card with a different card than the default card */
395 } else if (!strcmp(default_device_id_path_tag
, prime
))
396 /* we are to get a new fd (render-node) of the same device */
397 is_different_device
= 0;
399 device_name
= get_render_node_from_id_path_tag(udev
,
402 if (device_name
== NULL
) {
403 is_different_device
= 0;
404 goto default_device_clean
;
407 fd
= drm_open_device(device_name
);
412 is_different_device
= 0;
416 default_device_clean
:
417 free(default_device_id_path_tag
);
423 *different_device
= is_different_device
;
427 int loader_get_user_preferred_fd(int default_fd
, int *different_device
)
429 *different_device
= 0;
434 #if defined(HAVE_SYSFS)
436 dev_node_from_fd(int fd
, unsigned int *maj
, unsigned int *min
)
440 if (fstat(fd
, &buf
) < 0) {
441 log_(_LOADER_WARNING
, "MESA-LOADER: failed to stat fd %d\n", fd
);
445 if (!S_ISCHR(buf
.st_mode
)) {
446 log_(_LOADER_WARNING
, "MESA-LOADER: fd %d not a character device\n", fd
);
450 *maj
= major(buf
.st_rdev
);
451 *min
= minor(buf
.st_rdev
);
457 sysfs_get_pci_id_for_fd(int fd
, int *vendor_id
, int *chip_id
)
459 unsigned int maj
, min
;
463 if (dev_node_from_fd(fd
, &maj
, &min
) < 0) {
468 snprintf(buf
, sizeof(buf
), "/sys/dev/char/%d:%d/device/vendor", maj
, min
);
469 if (!(f
= fopen(buf
, "r"))) {
473 if (fscanf(f
, "%x", vendor_id
) != 1) {
479 snprintf(buf
, sizeof(buf
), "/sys/dev/char/%d:%d/device/device", maj
, min
);
480 if (!(f
= fopen(buf
, "r"))) {
484 if (fscanf(f
, "%x", chip_id
) != 1) {
494 #if !defined(__NOT_HAVE_DRM_H)
496 #include <i915_drm.h>
498 #include <radeon_drm.h>
501 drm_get_pci_id_for_fd(int fd
, int *vendor_id
, int *chip_id
)
503 drmVersionPtr version
;
507 version
= drmGetVersion(fd
);
509 log_(_LOADER_WARNING
, "MESA-LOADER: invalid drm fd\n");
512 if (!version
->name
) {
513 log_(_LOADER_WARNING
, "MESA-LOADER: unable to determine the driver name\n");
514 drmFreeVersion(version
);
518 if (strcmp(version
->name
, "i915") == 0) {
519 struct drm_i915_getparam gp
;
524 memset(&gp
, 0, sizeof(gp
));
525 gp
.param
= I915_PARAM_CHIPSET_ID
;
527 ret
= drmCommandWriteRead(fd
, DRM_I915_GETPARAM
, &gp
, sizeof(gp
));
529 log_(_LOADER_WARNING
, "MESA-LOADER: failed to get param for i915\n");
533 else if (strcmp(version
->name
, "radeon") == 0) {
534 struct drm_radeon_info info
;
539 memset(&info
, 0, sizeof(info
));
540 info
.request
= RADEON_INFO_DEVICE_ID
;
541 info
.value
= (unsigned long) chip_id
;
542 ret
= drmCommandWriteRead(fd
, DRM_RADEON_INFO
, &info
, sizeof(info
));
544 log_(_LOADER_WARNING
, "MESA-LOADER: failed to get info for radeon\n");
548 else if (strcmp(version
->name
, "nouveau") == 0) {
553 else if (strcmp(version
->name
, "vmwgfx") == 0) {
559 drmFreeVersion(version
);
561 return (*chip_id
>= 0);
567 loader_get_pci_id_for_fd(int fd
, int *vendor_id
, int *chip_id
)
570 if (libudev_get_pci_id_for_fd(fd
, vendor_id
, chip_id
))
574 if (sysfs_get_pci_id_for_fd(fd
, vendor_id
, chip_id
))
577 #if !defined(__NOT_HAVE_DRM_H)
578 if (drm_get_pci_id_for_fd(fd
, vendor_id
, chip_id
))
587 libudev_get_device_name_for_fd(int fd
)
589 char *device_name
= NULL
;
591 struct udev_device
*device
;
592 const char *const_device_name
;
593 UDEV_SYMBOL(struct udev
*, udev_new
, (void));
594 UDEV_SYMBOL(const char *, udev_device_get_devnode
,
595 (struct udev_device
*));
596 UDEV_SYMBOL(struct udev_device
*, udev_device_unref
,
597 (struct udev_device
*));
598 UDEV_SYMBOL(struct udev
*, udev_unref
, (struct udev
*));
604 device
= udev_device_new_from_fd(udev
, fd
);
608 const_device_name
= udev_device_get_devnode(device
);
609 if (!const_device_name
)
611 device_name
= strdup(const_device_name
);
614 udev_device_unref(device
);
623 sysfs_get_device_name_for_fd(int fd
)
625 char *device_name
= NULL
;
626 unsigned int maj
, min
;
629 static const char match
[9] = "\0DEVNAME=";
632 if (dev_node_from_fd(fd
, &maj
, &min
) < 0)
635 snprintf(buf
, sizeof(buf
), "/sys/dev/char/%d:%d/uevent", maj
, min
);
636 if (!(f
= fopen(buf
, "r")))
639 while (expected
< sizeof(match
)) {
645 } else if (c
== match
[expected
] )
651 strcpy(buf
, "/dev/");
652 if (fgets(buf
+ 5, sizeof(buf
) - 5, f
))
653 device_name
= strdup(buf
);
662 loader_get_device_name_for_fd(int fd
)
667 if ((result
= libudev_get_device_name_for_fd(fd
)))
671 if ((result
= sysfs_get_device_name_for_fd(fd
)))
678 loader_get_driver_for_fd(int fd
, unsigned driver_types
)
680 int vendor_id
, chip_id
, i
, j
;
684 driver_types
= _LOADER_GALLIUM
| _LOADER_DRI
;
686 if (!loader_get_pci_id_for_fd(fd
, &vendor_id
, &chip_id
)) {
688 #ifndef __NOT_HAVE_DRM_H
689 /* fallback to drmGetVersion(): */
690 drmVersionPtr version
= drmGetVersion(fd
);
693 log_(_LOADER_WARNING
, "failed to get driver name for fd %d\n", fd
);
697 driver
= strndup(version
->name
, version
->name_len
);
698 log_(_LOADER_INFO
, "using driver %s for %d\n", driver
, fd
);
700 drmFreeVersion(version
);
706 for (i
= 0; driver_map
[i
].driver
; i
++) {
707 if (vendor_id
!= driver_map
[i
].vendor_id
)
710 if (!(driver_types
& driver_map
[i
].driver_types
))
713 if (driver_map
[i
].predicate
&& !driver_map
[i
].predicate(fd
))
716 if (driver_map
[i
].num_chips_ids
== -1) {
717 driver
= strdup(driver_map
[i
].driver
);
721 for (j
= 0; j
< driver_map
[i
].num_chips_ids
; j
++)
722 if (driver_map
[i
].chip_ids
[j
] == chip_id
) {
723 driver
= strdup(driver_map
[i
].driver
);
729 log_(driver
? _LOADER_DEBUG
: _LOADER_WARNING
,
730 "pci id for fd %d: %04x:%04x, driver %s\n",
731 fd
, vendor_id
, chip_id
, driver
);
736 loader_set_logger(void (*logger
)(int level
, const char *fmt
, ...))