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>
80 #include <sys/types.h>
84 #ifndef __NOT_HAVE_DRM_H
89 #include "pci_id_driver_map.h"
91 static void default_logger(int level
, const char *fmt
, ...)
93 if (level
<= _LOADER_WARNING
) {
96 vfprintf(stderr
, fmt
, args
);
101 static void (*log_
)(int level
, const char *fmt
, ...) = default_logger
;
106 static void *udev_handle
= NULL
;
109 udev_dlopen_handle(void)
112 udev_handle
= dlopen("libudev.so.1", RTLD_LOCAL
| RTLD_LAZY
);
115 /* libudev.so.1 changed the return types of the two unref functions
116 * from voids to pointers. We don't use those return values, and the
117 * only ABI I've heard that cares about this kind of change (calling
118 * a function with a void * return that actually only returns void)
121 udev_handle
= dlopen("libudev.so.0", RTLD_LOCAL
| RTLD_LAZY
);
124 log_(_LOADER_WARNING
, "Couldn't dlopen libudev.so.1 or "
125 "libudev.so.0, driver detection may be broken.\n");
133 static int dlsym_failed
= 0;
136 checked_dlsym(void *dlopen_handle
, const char *name
)
138 void *result
= dlsym(dlopen_handle
, name
);
144 #define UDEV_SYMBOL(ret, name, args) \
145 ret (*name) args = checked_dlsym(udev_dlopen_handle(), #name);
148 static inline struct udev_device
*
149 udev_device_new_from_fd(struct udev
*udev
, int fd
)
151 struct udev_device
*device
;
153 UDEV_SYMBOL(struct udev_device
*, udev_device_new_from_devnum
,
154 (struct udev
*udev
, char type
, dev_t devnum
));
159 if (fstat(fd
, &buf
) < 0) {
160 log_(_LOADER_WARNING
, "MESA-LOADER: failed to stat fd %d\n", fd
);
164 device
= udev_device_new_from_devnum(udev
, 'c', buf
.st_rdev
);
165 if (device
== NULL
) {
166 log_(_LOADER_WARNING
,
167 "MESA-LOADER: could not create udev device for fd %d\n", fd
);
175 libudev_get_pci_id_for_fd(int fd
, int *vendor_id
, int *chip_id
)
177 struct udev
*udev
= NULL
;
178 struct udev_device
*device
= NULL
, *parent
;
180 UDEV_SYMBOL(struct udev
*, udev_new
, (void));
181 UDEV_SYMBOL(struct udev_device
*, udev_device_get_parent
,
182 (struct udev_device
*));
183 UDEV_SYMBOL(const char *, udev_device_get_property_value
,
184 (struct udev_device
*, const char *));
185 UDEV_SYMBOL(struct udev_device
*, udev_device_unref
,
186 (struct udev_device
*));
187 UDEV_SYMBOL(struct udev
*, udev_unref
, (struct udev
*));
195 device
= udev_device_new_from_fd(udev
, fd
);
199 parent
= udev_device_get_parent(device
);
200 if (parent
== NULL
) {
201 log_(_LOADER_WARNING
, "MESA-LOADER: could not get parent device\n");
205 pci_id
= udev_device_get_property_value(parent
, "PCI_ID");
206 if (pci_id
== NULL
||
207 sscanf(pci_id
, "%x:%x", vendor_id
, chip_id
) != 2) {
208 log_(_LOADER_WARNING
, "MESA-LOADER: malformed or no PCI ID\n");
215 udev_device_unref(device
);
219 return (*chip_id
>= 0);
223 get_render_node_from_id_path_tag(struct udev
*udev
,
227 struct udev_device
*device
;
228 struct udev_enumerate
*e
;
229 struct udev_list_entry
*entry
;
230 const char *path
, *id_path_tag_tmp
;
233 UDEV_SYMBOL(struct udev_enumerate
*, udev_enumerate_new
,
235 UDEV_SYMBOL(int, udev_enumerate_add_match_subsystem
,
236 (struct udev_enumerate
*, const char *));
237 UDEV_SYMBOL(int, udev_enumerate_add_match_sysname
,
238 (struct udev_enumerate
*, const char *));
239 UDEV_SYMBOL(int, udev_enumerate_scan_devices
,
240 (struct udev_enumerate
*));
241 UDEV_SYMBOL(struct udev_list_entry
*, udev_enumerate_get_list_entry
,
242 (struct udev_enumerate
*));
243 UDEV_SYMBOL(struct udev_list_entry
*, udev_list_entry_get_next
,
244 (struct udev_list_entry
*));
245 UDEV_SYMBOL(const char *, udev_list_entry_get_name
,
246 (struct udev_list_entry
*));
247 UDEV_SYMBOL(struct udev_device
*, udev_device_new_from_syspath
,
248 (struct udev
*, const char *));
249 UDEV_SYMBOL(const char *, udev_device_get_property_value
,
250 (struct udev_device
*, const char *));
251 UDEV_SYMBOL(const char *, udev_device_get_devnode
,
252 (struct udev_device
*));
253 UDEV_SYMBOL(struct udev_device
*, udev_device_unref
,
254 (struct udev_device
*));
256 e
= udev_enumerate_new(udev
);
257 udev_enumerate_add_match_subsystem(e
, "drm");
258 udev_enumerate_add_match_sysname(e
, "render*");
260 udev_enumerate_scan_devices(e
);
261 udev_list_entry_foreach(entry
, udev_enumerate_get_list_entry(e
)) {
262 path
= udev_list_entry_get_name(entry
);
263 device
= udev_device_new_from_syspath(udev
, path
);
266 id_path_tag_tmp
= udev_device_get_property_value(device
, "ID_PATH_TAG");
267 if (id_path_tag_tmp
) {
268 if ((!another_tag
&& !strcmp(id_path_tag
, id_path_tag_tmp
)) ||
269 (another_tag
&& strcmp(id_path_tag
, id_path_tag_tmp
))) {
274 udev_device_unref(device
);
278 path_res
= strdup(udev_device_get_devnode(device
));
279 udev_device_unref(device
);
286 get_id_path_tag_from_fd(struct udev
*udev
, int fd
)
288 struct udev_device
*device
;
289 const char *id_path_tag_tmp
;
291 UDEV_SYMBOL(const char *, udev_device_get_property_value
,
292 (struct udev_device
*, const char *));
293 UDEV_SYMBOL(struct udev_device
*, udev_device_unref
,
294 (struct udev_device
*));
296 device
= udev_device_new_from_fd(udev
, fd
);
300 id_path_tag_tmp
= udev_device_get_property_value(device
, "ID_PATH_TAG");
301 if (!id_path_tag_tmp
)
304 id_path_tag
= strdup(id_path_tag_tmp
);
306 udev_device_unref(device
);
311 drm_open_device(const char *device_name
)
315 fd
= open(device_name
, O_RDWR
| O_CLOEXEC
);
316 if (fd
== -1 && errno
== EINVAL
)
319 fd
= open(device_name
, O_RDWR
);
321 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
326 int loader_get_user_preferred_fd(int default_fd
, int *different_device
)
329 const char *dri_prime
= getenv("DRI_PRIME");
331 int is_different_device
= 0, fd
= default_fd
;
332 char *default_device_id_path_tag
;
333 char *device_name
= NULL
;
334 char another_tag
= 0;
335 UDEV_SYMBOL(struct udev
*, udev_new
, (void));
336 UDEV_SYMBOL(struct udev
*, udev_unref
, (struct udev
*));
339 prime
= strdup(dri_prime
);
342 *different_device
= 0;
350 default_device_id_path_tag
= get_id_path_tag_from_fd(udev
, default_fd
);
351 if (!default_device_id_path_tag
)
354 is_different_device
= 1;
355 /* two format are supported:
356 * "1": choose any other card than the card used by default.
357 * id_path_tag: (for example "pci-0000_02_00_0") choose the card
358 * with this id_path_tag.
360 if (!strcmp(prime
,"1")) {
362 prime
= strdup(default_device_id_path_tag
);
363 /* request a card with a different card than the default card */
365 } else if (!strcmp(default_device_id_path_tag
, prime
))
366 /* we are to get a new fd (render-node) of the same device */
367 is_different_device
= 0;
369 device_name
= get_render_node_from_id_path_tag(udev
,
372 if (device_name
== NULL
) {
373 is_different_device
= 0;
374 goto default_device_clean
;
377 fd
= drm_open_device(device_name
);
382 is_different_device
= 0;
386 default_device_clean
:
387 free(default_device_id_path_tag
);
393 *different_device
= is_different_device
;
397 int loader_get_user_preferred_fd(int default_fd
, int *different_device
)
399 *different_device
= 0;
404 #if defined(HAVE_SYSFS)
406 dev_node_from_fd(int fd
, unsigned int *maj
, unsigned int *min
)
410 if (fstat(fd
, &buf
) < 0) {
411 log_(_LOADER_WARNING
, "MESA-LOADER: failed to stat fd %d\n", fd
);
415 if (!S_ISCHR(buf
.st_mode
)) {
416 log_(_LOADER_WARNING
, "MESA-LOADER: fd %d not a character device\n", fd
);
420 *maj
= major(buf
.st_rdev
);
421 *min
= minor(buf
.st_rdev
);
427 sysfs_get_pci_id_for_fd(int fd
, int *vendor_id
, int *chip_id
)
429 unsigned int maj
, min
;
433 if (dev_node_from_fd(fd
, &maj
, &min
) < 0) {
438 snprintf(buf
, sizeof(buf
), "/sys/dev/char/%d:%d/device/vendor", maj
, min
);
439 if (!(f
= fopen(buf
, "r"))) {
443 if (fscanf(f
, "%x", vendor_id
) != 1) {
449 snprintf(buf
, sizeof(buf
), "/sys/dev/char/%d:%d/device/device", maj
, min
);
450 if (!(f
= fopen(buf
, "r"))) {
454 if (fscanf(f
, "%x", chip_id
) != 1) {
464 #if !defined(__NOT_HAVE_DRM_H)
466 #include <i915_drm.h>
468 #include <radeon_drm.h>
471 drm_get_pci_id_for_fd(int fd
, int *vendor_id
, int *chip_id
)
473 drmVersionPtr version
;
477 version
= drmGetVersion(fd
);
479 log_(_LOADER_WARNING
, "MESA-LOADER: invalid drm fd\n");
482 if (!version
->name
) {
483 log_(_LOADER_WARNING
, "MESA-LOADER: unable to determine the driver name\n");
484 drmFreeVersion(version
);
488 if (strcmp(version
->name
, "i915") == 0) {
489 struct drm_i915_getparam gp
;
494 memset(&gp
, 0, sizeof(gp
));
495 gp
.param
= I915_PARAM_CHIPSET_ID
;
497 ret
= drmCommandWriteRead(fd
, DRM_I915_GETPARAM
, &gp
, sizeof(gp
));
499 log_(_LOADER_WARNING
, "MESA-LOADER: failed to get param for i915\n");
503 else if (strcmp(version
->name
, "radeon") == 0) {
504 struct drm_radeon_info info
;
509 memset(&info
, 0, sizeof(info
));
510 info
.request
= RADEON_INFO_DEVICE_ID
;
511 info
.value
= (unsigned long) chip_id
;
512 ret
= drmCommandWriteRead(fd
, DRM_RADEON_INFO
, &info
, sizeof(info
));
514 log_(_LOADER_WARNING
, "MESA-LOADER: failed to get info for radeon\n");
518 else if (strcmp(version
->name
, "nouveau") == 0) {
523 else if (strcmp(version
->name
, "vmwgfx") == 0) {
529 drmFreeVersion(version
);
531 return (*chip_id
>= 0);
537 loader_get_pci_id_for_fd(int fd
, int *vendor_id
, int *chip_id
)
540 if (libudev_get_pci_id_for_fd(fd
, vendor_id
, chip_id
))
544 if (sysfs_get_pci_id_for_fd(fd
, vendor_id
, chip_id
))
547 #if !defined(__NOT_HAVE_DRM_H)
548 if (drm_get_pci_id_for_fd(fd
, vendor_id
, chip_id
))
557 libudev_get_device_name_for_fd(int fd
)
559 char *device_name
= NULL
;
561 struct udev_device
*device
;
562 const char *const_device_name
;
563 UDEV_SYMBOL(struct udev
*, udev_new
, (void));
564 UDEV_SYMBOL(const char *, udev_device_get_devnode
,
565 (struct udev_device
*));
566 UDEV_SYMBOL(struct udev_device
*, udev_device_unref
,
567 (struct udev_device
*));
568 UDEV_SYMBOL(struct udev
*, udev_unref
, (struct udev
*));
571 device
= udev_device_new_from_fd(udev
, fd
);
575 const_device_name
= udev_device_get_devnode(device
);
576 if (!const_device_name
)
578 device_name
= strdup(const_device_name
);
581 udev_device_unref(device
);
590 sysfs_get_device_name_for_fd(int fd
)
592 char *device_name
= NULL
;
593 unsigned int maj
, min
;
596 static const char match
[9] = "\0DEVNAME=";
599 if (dev_node_from_fd(fd
, &maj
, &min
) < 0)
602 snprintf(buf
, sizeof(buf
), "/sys/dev/char/%d:%d/uevent", maj
, min
);
603 if (!(f
= fopen(buf
, "r")))
606 while (expected
< sizeof(match
)) {
612 } else if (c
== match
[expected
] )
618 strcpy(buf
, "/dev/");
619 if (fgets(buf
+ 5, sizeof(buf
) - 5, f
))
620 device_name
= strdup(buf
);
629 loader_get_device_name_for_fd(int fd
)
634 if ((result
= libudev_get_device_name_for_fd(fd
)))
638 if ((result
= sysfs_get_device_name_for_fd(fd
)))
645 loader_get_driver_for_fd(int fd
, unsigned driver_types
)
647 int vendor_id
, chip_id
, i
, j
;
651 driver_types
= _LOADER_GALLIUM
| _LOADER_DRI
;
653 if (!loader_get_pci_id_for_fd(fd
, &vendor_id
, &chip_id
)) {
655 #ifndef __NOT_HAVE_DRM_H
656 /* fallback to drmGetVersion(): */
657 drmVersionPtr version
= drmGetVersion(fd
);
660 log_(_LOADER_WARNING
, "failed to get driver name for fd %d\n", fd
);
664 driver
= strndup(version
->name
, version
->name_len
);
665 log_(_LOADER_INFO
, "using driver %s for %d\n", driver
, fd
);
667 drmFreeVersion(version
);
673 for (i
= 0; driver_map
[i
].driver
; i
++) {
674 if (vendor_id
!= driver_map
[i
].vendor_id
)
677 if (!(driver_types
& driver_map
[i
].driver_types
))
680 if (driver_map
[i
].predicate
&& !driver_map
[i
].predicate(fd
))
683 if (driver_map
[i
].num_chips_ids
== -1) {
684 driver
= strdup(driver_map
[i
].driver
);
688 for (j
= 0; j
< driver_map
[i
].num_chips_ids
; j
++)
689 if (driver_map
[i
].chip_ids
[j
] == chip_id
) {
690 driver
= strdup(driver_map
[i
].driver
);
696 log_(driver
? _LOADER_DEBUG
: _LOADER_WARNING
,
697 "pci id for fd %d: %04x:%04x, driver %s\n",
698 fd
, vendor_id
, chip_id
, driver
);
703 loader_set_logger(void (*logger
)(int level
, const char *fmt
, ...))