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/mkdev.h>
86 #ifdef MAJOR_IN_SYSMACROS
87 #include <sys/sysmacros.h>
96 #include "pci_id_driver_map.h"
98 static void default_logger(int level
, const char *fmt
, ...)
100 if (level
<= _LOADER_WARNING
) {
103 vfprintf(stderr
, fmt
, args
);
108 static void (*log_
)(int level
, const char *fmt
, ...) = default_logger
;
111 loader_open_device(const char *device_name
)
115 fd
= open(device_name
, O_RDWR
| O_CLOEXEC
);
116 if (fd
== -1 && errno
== EINVAL
)
119 fd
= open(device_name
, O_RDWR
);
121 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
129 static void *udev_handle
= NULL
;
132 udev_dlopen_handle(void)
135 unsigned flags
= RTLD_NOLOAD
| RTLD_LOCAL
| RTLD_LAZY
;
138 /* libudev.so.1 changed the return types of the two unref functions
139 * from voids to pointers. We don't use those return values, and the
140 * only ABI I've heard that cares about this kind of change (calling
141 * a function with a void * return that actually only returns void)
145 /* First try opening an already linked libudev, then try loading one */
147 for (version
= 1; version
>= 0; version
--) {
148 snprintf(name
, sizeof(name
), "libudev.so.%d", version
);
149 udev_handle
= dlopen(name
, flags
);
154 if ((flags
& RTLD_NOLOAD
) == 0)
157 flags
&= ~RTLD_NOLOAD
;
160 log_(_LOADER_WARNING
,
161 "Couldn't dlopen libudev.so.1 or "
162 "libudev.so.0, driver detection may be broken.\n");
166 static int dlsym_failed
= 0;
169 checked_dlsym(void *dlopen_handle
, const char *name
)
171 void *result
= dlsym(dlopen_handle
, name
);
177 #define UDEV_SYMBOL(ret, name, args) \
178 ret (*name) args = checked_dlsym(udev_dlopen_handle(), #name);
181 static inline struct udev_device
*
182 udev_device_new_from_fd(struct udev
*udev
, int fd
)
184 struct udev_device
*device
;
186 UDEV_SYMBOL(struct udev_device
*, udev_device_new_from_devnum
,
187 (struct udev
*udev
, char type
, dev_t devnum
));
192 if (fstat(fd
, &buf
) < 0) {
193 log_(_LOADER_WARNING
, "MESA-LOADER: failed to stat fd %d\n", fd
);
197 device
= udev_device_new_from_devnum(udev
, 'c', buf
.st_rdev
);
198 if (device
== NULL
) {
199 log_(_LOADER_WARNING
,
200 "MESA-LOADER: could not create udev device for fd %d\n", fd
);
208 get_render_node_from_id_path_tag(struct udev
*udev
,
212 struct udev_device
*device
;
213 struct udev_enumerate
*e
;
214 struct udev_list_entry
*entry
;
215 const char *path
, *id_path_tag_tmp
;
218 UDEV_SYMBOL(struct udev_enumerate
*, udev_enumerate_new
,
220 UDEV_SYMBOL(int, udev_enumerate_add_match_subsystem
,
221 (struct udev_enumerate
*, const char *));
222 UDEV_SYMBOL(int, udev_enumerate_add_match_sysname
,
223 (struct udev_enumerate
*, const char *));
224 UDEV_SYMBOL(int, udev_enumerate_scan_devices
,
225 (struct udev_enumerate
*));
226 UDEV_SYMBOL(struct udev_list_entry
*, udev_enumerate_get_list_entry
,
227 (struct udev_enumerate
*));
228 UDEV_SYMBOL(void, udev_enumerate_unref
,
229 (struct udev_enumerate
*));
230 UDEV_SYMBOL(struct udev_list_entry
*, udev_list_entry_get_next
,
231 (struct udev_list_entry
*));
232 UDEV_SYMBOL(const char *, udev_list_entry_get_name
,
233 (struct udev_list_entry
*));
234 UDEV_SYMBOL(struct udev_device
*, udev_device_new_from_syspath
,
235 (struct udev
*, const char *));
236 UDEV_SYMBOL(const char *, udev_device_get_property_value
,
237 (struct udev_device
*, const char *));
238 UDEV_SYMBOL(const char *, udev_device_get_devnode
,
239 (struct udev_device
*));
240 UDEV_SYMBOL(struct udev_device
*, udev_device_unref
,
241 (struct udev_device
*));
243 e
= udev_enumerate_new(udev
);
244 udev_enumerate_add_match_subsystem(e
, "drm");
245 udev_enumerate_add_match_sysname(e
, "render*");
247 udev_enumerate_scan_devices(e
);
248 udev_list_entry_foreach(entry
, udev_enumerate_get_list_entry(e
)) {
249 path
= udev_list_entry_get_name(entry
);
250 device
= udev_device_new_from_syspath(udev
, path
);
253 id_path_tag_tmp
= udev_device_get_property_value(device
, "ID_PATH_TAG");
254 if (id_path_tag_tmp
) {
255 if ((!another_tag
&& !strcmp(id_path_tag
, id_path_tag_tmp
)) ||
256 (another_tag
&& strcmp(id_path_tag
, id_path_tag_tmp
))) {
261 udev_device_unref(device
);
264 udev_enumerate_unref(e
);
267 path_res
= strdup(udev_device_get_devnode(device
));
268 udev_device_unref(device
);
275 get_id_path_tag_from_fd(struct udev
*udev
, int fd
)
277 struct udev_device
*device
;
278 const char *id_path_tag_tmp
;
280 UDEV_SYMBOL(const char *, udev_device_get_property_value
,
281 (struct udev_device
*, const char *));
282 UDEV_SYMBOL(struct udev_device
*, udev_device_unref
,
283 (struct udev_device
*));
285 device
= udev_device_new_from_fd(udev
, fd
);
289 id_path_tag_tmp
= udev_device_get_property_value(device
, "ID_PATH_TAG");
290 if (!id_path_tag_tmp
)
293 id_path_tag
= strdup(id_path_tag_tmp
);
295 udev_device_unref(device
);
300 const char __driConfigOptionsLoader
[] =
302 DRI_CONF_SECTION_INITIALIZATION
303 DRI_CONF_DEVICE_ID_PATH_TAG()
307 static char *loader_get_dri_config_device_id(void)
309 driOptionCache defaultInitOptions
;
310 driOptionCache userInitOptions
;
313 driParseOptionInfo(&defaultInitOptions
, __driConfigOptionsLoader
);
314 driParseConfigFiles(&userInitOptions
, &defaultInitOptions
, 0, "loader");
315 if (driCheckOption(&userInitOptions
, "device_id", DRI_STRING
))
316 prime
= strdup(driQueryOptionstr(&userInitOptions
, "device_id"));
317 driDestroyOptionCache(&userInitOptions
);
318 driDestroyOptionInfo(&defaultInitOptions
);
324 int loader_get_user_preferred_fd(int default_fd
, int *different_device
)
327 const char *dri_prime
= getenv("DRI_PRIME");
329 int is_different_device
= 0, fd
= default_fd
;
330 char *default_device_id_path_tag
;
331 char *device_name
= NULL
;
332 char another_tag
= 0;
333 UDEV_SYMBOL(struct udev
*, udev_new
, (void));
334 UDEV_SYMBOL(struct udev
*, udev_unref
, (struct udev
*));
337 prime
= strdup(dri_prime
);
340 prime
= loader_get_dri_config_device_id();
344 *different_device
= 0;
352 default_device_id_path_tag
= get_id_path_tag_from_fd(udev
, default_fd
);
353 if (!default_device_id_path_tag
)
356 is_different_device
= 1;
357 /* two format are supported:
358 * "1": choose any other card than the card used by default.
359 * id_path_tag: (for example "pci-0000_02_00_0") choose the card
360 * with this id_path_tag.
362 if (!strcmp(prime
,"1")) {
364 prime
= strdup(default_device_id_path_tag
);
365 /* request a card with a different card than the default card */
367 } else if (!strcmp(default_device_id_path_tag
, prime
))
368 /* we are to get a new fd (render-node) of the same device */
369 is_different_device
= 0;
371 device_name
= get_render_node_from_id_path_tag(udev
,
374 if (device_name
== NULL
) {
375 is_different_device
= 0;
376 goto default_device_clean
;
379 fd
= loader_open_device(device_name
);
384 is_different_device
= 0;
388 default_device_clean
:
389 free(default_device_id_path_tag
);
395 *different_device
= is_different_device
;
399 int loader_get_user_preferred_fd(int default_fd
, int *different_device
)
401 *different_device
= 0;
406 #if defined(HAVE_SYSFS) || defined(HAVE_LIBDRM)
408 dev_node_from_fd(int fd
, unsigned int *maj
, unsigned int *min
)
412 if (fstat(fd
, &buf
) < 0) {
413 log_(_LOADER_WARNING
, "MESA-LOADER: failed to stat fd %d\n", fd
);
417 if (!S_ISCHR(buf
.st_mode
)) {
418 log_(_LOADER_WARNING
, "MESA-LOADER: fd %d not a character device\n", fd
);
422 *maj
= major(buf
.st_rdev
);
423 *min
= minor(buf
.st_rdev
);
429 #if defined(HAVE_LIBDRM)
432 drm_get_pci_id_for_fd(int fd
, int *vendor_id
, int *chip_id
)
437 if (drmGetDevice(fd
, &device
) == 0) {
438 if (device
->bustype
== DRM_BUS_PCI
) {
439 *vendor_id
= device
->deviceinfo
.pci
->vendor_id
;
440 *chip_id
= device
->deviceinfo
.pci
->device_id
;
444 log_(_LOADER_WARNING
, "MESA-LOADER: device is not located on the PCI bus\n");
447 drmFreeDevice(&device
);
450 log_(_LOADER_WARNING
, "MESA-LOADER: failed to retrieve device information\n");
460 loader_get_pci_id_for_fd(int fd
, int *vendor_id
, int *chip_id
)
463 if (drm_get_pci_id_for_fd(fd
, vendor_id
, chip_id
))
472 libudev_get_device_name_for_fd(int fd
)
474 char *device_name
= NULL
;
476 struct udev_device
*device
;
477 const char *const_device_name
;
478 UDEV_SYMBOL(struct udev
*, udev_new
, (void));
479 UDEV_SYMBOL(const char *, udev_device_get_devnode
,
480 (struct udev_device
*));
481 UDEV_SYMBOL(struct udev_device
*, udev_device_unref
,
482 (struct udev_device
*));
483 UDEV_SYMBOL(struct udev
*, udev_unref
, (struct udev
*));
489 device
= udev_device_new_from_fd(udev
, fd
);
493 const_device_name
= udev_device_get_devnode(device
);
494 if (!const_device_name
)
496 device_name
= strdup(const_device_name
);
499 udev_device_unref(device
);
508 sysfs_get_device_name_for_fd(int fd
)
510 char *device_name
= NULL
;
511 unsigned int maj
, min
;
514 static const char match
[9] = "\nDEVNAME=";
517 if (dev_node_from_fd(fd
, &maj
, &min
) < 0)
520 snprintf(buf
, sizeof(buf
), "/sys/dev/char/%d:%d/uevent", maj
, min
);
521 if (!(f
= fopen(buf
, "r")))
524 while (expected
< sizeof(match
)) {
530 } else if (c
== match
[expected
] )
536 strcpy(buf
, "/dev/");
537 if (fgets(buf
+ 5, sizeof(buf
) - 5, f
)) {
538 buf
[strcspn(buf
, "\n")] = '\0';
539 device_name
= strdup(buf
);
547 #if defined(HAVE_LIBDRM)
549 drm_get_device_name_for_fd(int fd
)
551 unsigned int maj
, min
;
555 if (dev_node_from_fd(fd
, &maj
, &min
) < 0)
558 n
= snprintf(buf
, sizeof(buf
), DRM_DEV_NAME
, DRM_DIR_NAME
, min
);
559 if (n
== -1 || n
>= sizeof(buf
))
567 loader_get_device_name_for_fd(int fd
)
572 if ((result
= libudev_get_device_name_for_fd(fd
)))
576 if ((result
= sysfs_get_device_name_for_fd(fd
)))
580 if ((result
= drm_get_device_name_for_fd(fd
)))
587 loader_get_driver_for_fd(int fd
, unsigned driver_types
)
589 int vendor_id
, chip_id
, i
, j
;
593 driver_types
= _LOADER_GALLIUM
| _LOADER_DRI
;
595 if (!loader_get_pci_id_for_fd(fd
, &vendor_id
, &chip_id
)) {
598 /* fallback to drmGetVersion(): */
599 drmVersionPtr version
= drmGetVersion(fd
);
602 log_(_LOADER_WARNING
, "failed to get driver name for fd %d\n", fd
);
606 driver
= strndup(version
->name
, version
->name_len
);
607 log_(_LOADER_INFO
, "using driver %s for %d\n", driver
, fd
);
609 drmFreeVersion(version
);
615 for (i
= 0; driver_map
[i
].driver
; i
++) {
616 if (vendor_id
!= driver_map
[i
].vendor_id
)
619 if (!(driver_types
& driver_map
[i
].driver_types
))
622 if (driver_map
[i
].predicate
&& !driver_map
[i
].predicate(fd
))
625 if (driver_map
[i
].num_chips_ids
== -1) {
626 driver
= strdup(driver_map
[i
].driver
);
630 for (j
= 0; j
< driver_map
[i
].num_chips_ids
; j
++)
631 if (driver_map
[i
].chip_ids
[j
] == chip_id
) {
632 driver
= strdup(driver_map
[i
].driver
);
638 log_(driver
? _LOADER_DEBUG
: _LOADER_WARNING
,
639 "pci id for fd %d: %04x:%04x, driver %s\n",
640 fd
, vendor_id
, chip_id
, driver
);
645 loader_set_logger(void (*logger
)(int level
, const char *fmt
, ...))