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>
75 #include "pci_ids/pci_id_driver_map.h"
77 static void default_logger(int level
, const char *fmt
, ...)
79 if (level
>= _LOADER_WARNING
) {
82 vfprintf(stderr
, fmt
, args
);
84 fprintf(stderr
, "\n");
88 static void (*log
)(int level
, const char *fmt
, ...) = default_logger
;
93 static inline struct udev_device
*
94 udev_device_new_from_fd(struct udev
*udev
, int fd
)
96 struct udev_device
*device
;
99 if (fstat(fd
, &buf
) < 0) {
100 log(_LOADER_WARNING
, "MESA-LOADER: failed to stat fd %d", fd
);
104 device
= udev_device_new_from_devnum(udev
, 'c', buf
.st_rdev
);
105 if (device
== NULL
) {
107 "MESA-LOADER: could not create udev device for fd %d", fd
);
115 loader_get_pci_id_for_fd(int fd
, int *vendor_id
, int *chip_id
)
117 struct udev
*udev
= NULL
;
118 struct udev_device
*device
= NULL
, *parent
;
125 device
= udev_device_new_from_fd(udev
, fd
);
129 parent
= udev_device_get_parent(device
);
130 if (parent
== NULL
) {
131 log(_LOADER_WARNING
, "MESA-LOADER: could not get parent device");
135 pci_id
= udev_device_get_property_value(parent
, "PCI_ID");
136 if (pci_id
== NULL
||
137 sscanf(pci_id
, "%x:%x", vendor_id
, chip_id
) != 2) {
138 log(_LOADER_WARNING
, "MESA-LOADER: malformed or no PCI ID");
145 udev_device_unref(device
);
149 return (*chip_id
>= 0);
152 #elif defined(ANDROID) && !defined(_EGL_NO_DRM)
155 #include <i915_drm.h>
157 #include <radeon_drm.h>
160 loader_get_pci_id_for_fd(int fd
, int *vendor_id
, int *chip_id
)
162 drmVersionPtr version
;
166 version
= drmGetVersion(fd
);
168 log(_LOADER_WARNING
, "MESA-LOADER: invalid drm fd");
171 if (!version
->name
) {
172 log(_LOADER_WARNING
, "MESA-LOADER: unable to determine the driver name");
173 drmFreeVersion(version
);
177 if (strcmp(version
->name
, "i915") == 0) {
178 struct drm_i915_getparam gp
;
183 memset(&gp
, 0, sizeof(gp
));
184 gp
.param
= I915_PARAM_CHIPSET_ID
;
186 ret
= drmCommandWriteRead(fd
, DRM_I915_GETPARAM
, &gp
, sizeof(gp
));
188 log(_LOADER_WARNING
, "MESA-LOADER: failed to get param for i915");
192 else if (strcmp(version
->name
, "radeon") == 0) {
193 struct drm_radeon_info info
;
198 memset(&info
, 0, sizeof(info
));
199 info
.request
= RADEON_INFO_DEVICE_ID
;
200 info
.value
= (unsigned long) chip_id
;
201 ret
= drmCommandWriteRead(fd
, DRM_RADEON_INFO
, &info
, sizeof(info
));
203 log(_LOADER_WARNING
, "MESA-LOADER: failed to get info for radeon");
207 else if (strcmp(version
->name
, "nouveau") == 0) {
212 else if (strcmp(version
->name
, "vmwgfx") == 0) {
218 drmFreeVersion(version
);
220 return (*chip_id
>= 0);
226 loader_get_pci_id_for_fd(int fd
, int *vendor_id
, int *chip_id
)
235 loader_get_device_name_for_fd(int fd
)
237 char *device_name
= NULL
;
240 struct udev_device
*device
;
241 const char *const_device_name
;
244 device
= udev_device_new_from_fd(udev
, fd
);
248 const_device_name
= udev_device_get_devnode(device
);
249 if (!const_device_name
)
251 device_name
= strdup(const_device_name
);
254 udev_device_unref(device
);
261 loader_get_driver_for_fd(int fd
, unsigned driver_types
)
263 int vendor_id
, chip_id
, i
, j
;
267 driver_types
= _LOADER_GALLIUM
| _LOADER_DRI
;
269 if (!loader_get_pci_id_for_fd(fd
, &vendor_id
, &chip_id
)) {
270 /* fallback to drmGetVersion(): */
271 drmVersionPtr version
= drmGetVersion(fd
);
274 log(_LOADER_WARNING
, "failed to get driver name for fd %d", fd
);
278 driver
= strndup(version
->name
, version
->name_len
);
279 log(_LOADER_INFO
, "using driver %s for %d", driver
, fd
);
281 drmFreeVersion(version
);
286 for (i
= 0; driver_map
[i
].driver
; i
++) {
287 if (vendor_id
!= driver_map
[i
].vendor_id
)
290 if (!(driver_types
& driver_map
[i
].driver_types
))
293 if (driver_map
[i
].num_chips_ids
== -1) {
294 driver
= strdup(driver_map
[i
].driver
);
298 for (j
= 0; j
< driver_map
[i
].num_chips_ids
; j
++)
299 if (driver_map
[i
].chip_ids
[j
] == chip_id
) {
300 driver
= strdup(driver_map
[i
].driver
);
306 log(driver
? _LOADER_INFO
: _LOADER_WARNING
,
307 "pci id for fd %d: %04x:%04x, driver %s",
308 fd
, vendor_id
, chip_id
, driver
);
313 loader_set_logger(void (*logger
)(int level
, const char *fmt
, ...))