2 * Copyright (C) 2013 Rob Clark <robclark@freedesktop.org>
3 * Copyright (C) 2014-2016 Emil Velikov <emil.l.velikov@gmail.com>
4 * Copyright (C) 2016 Intel Corporation
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * Rob Clark <robclark@freedesktop.org>
37 #include <sys/mkdev.h>
39 #ifdef MAJOR_IN_SYSMACROS
40 #include <sys/sysmacros.h>
49 #include "xmlconfig.h"
55 #include "pci_id_driver_map.h"
57 static void default_logger(int level
, const char *fmt
, ...)
59 if (level
<= _LOADER_WARNING
) {
62 vfprintf(stderr
, fmt
, args
);
67 static void (*log_
)(int level
, const char *fmt
, ...) = default_logger
;
70 loader_open_device(const char *device_name
)
74 fd
= open(device_name
, O_RDWR
| O_CLOEXEC
);
75 if (fd
== -1 && errno
== EINVAL
)
78 fd
= open(device_name
, O_RDWR
);
80 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
85 #if defined(HAVE_LIBDRM)
87 static const char __driConfigOptionsLoader
[] =
89 DRI_CONF_SECTION_INITIALIZATION
90 DRI_CONF_DEVICE_ID_PATH_TAG()
94 static char *loader_get_dri_config_device_id(void)
96 driOptionCache defaultInitOptions
;
97 driOptionCache userInitOptions
;
100 driParseOptionInfo(&defaultInitOptions
, __driConfigOptionsLoader
);
101 driParseConfigFiles(&userInitOptions
, &defaultInitOptions
, 0, "loader");
102 if (driCheckOption(&userInitOptions
, "device_id", DRI_STRING
))
103 prime
= strdup(driQueryOptionstr(&userInitOptions
, "device_id"));
104 driDestroyOptionCache(&userInitOptions
);
105 driDestroyOptionInfo(&defaultInitOptions
);
111 static char *drm_construct_id_path_tag(drmDevicePtr device
)
113 /* Length of "pci-xxxx_xx_xx_x\0" */
114 #define PCI_ID_PATH_TAG_LENGTH 17
117 if (device
->bustype
== DRM_BUS_PCI
) {
118 tag
= calloc(PCI_ID_PATH_TAG_LENGTH
, sizeof(char));
122 snprintf(tag
, PCI_ID_PATH_TAG_LENGTH
, "pci-%04x_%02x_%02x_%1u",
123 device
->businfo
.pci
->domain
, device
->businfo
.pci
->bus
,
124 device
->businfo
.pci
->dev
, device
->businfo
.pci
->func
);
129 static bool drm_device_matches_tag(drmDevicePtr device
, const char *prime_tag
)
131 char *tag
= drm_construct_id_path_tag(device
);
137 ret
= strcmp(tag
, prime_tag
);
143 static char *drm_get_id_path_tag_for_fd(int fd
)
148 if (drmGetDevice(fd
, &device
) != 0)
151 tag
= drm_construct_id_path_tag(device
);
152 drmFreeDevice(&device
);
156 int loader_get_user_preferred_fd(int default_fd
, int *different_device
)
158 /* Arbitrary "maximum" value of drm devices. */
159 #define MAX_DRM_DEVICES 32
160 const char *dri_prime
= getenv("DRI_PRIME");
161 char *default_tag
, *prime
= NULL
;
162 drmDevicePtr devices
[MAX_DRM_DEVICES
];
163 int i
, num_devices
, fd
;
167 prime
= strdup(dri_prime
);
170 prime
= loader_get_dri_config_device_id();
174 *different_device
= 0;
178 default_tag
= drm_get_id_path_tag_for_fd(default_fd
);
179 if (default_tag
== NULL
)
182 num_devices
= drmGetDevices(devices
, MAX_DRM_DEVICES
);
186 /* two format are supported:
187 * "1": choose any other card than the card used by default.
188 * id_path_tag: (for example "pci-0000_02_00_0") choose the card
189 * with this id_path_tag.
191 if (!strcmp(prime
,"1")) {
192 /* Hmm... detection for 2-7 seems to be broken. Oh well ...
193 * Pick the first render device that is not our own.
195 for (i
= 0; i
< num_devices
; i
++) {
196 if (devices
[i
]->available_nodes
& 1 << DRM_NODE_RENDER
&&
197 !drm_device_matches_tag(devices
[i
], default_tag
)) {
204 for (i
= 0; i
< num_devices
; i
++) {
205 if (devices
[i
]->available_nodes
& 1 << DRM_NODE_RENDER
&&
206 drm_device_matches_tag(devices
[i
], prime
)) {
215 drmFreeDevices(devices
, num_devices
);
219 fd
= loader_open_device(devices
[i
]->nodes
[DRM_NODE_RENDER
]);
220 drmFreeDevices(devices
, num_devices
);
226 *different_device
= !!strcmp(default_tag
, prime
);
233 *different_device
= 0;
240 int loader_get_user_preferred_fd(int default_fd
, int *different_device
)
242 *different_device
= 0;
247 #if defined(HAVE_LIBDRM)
249 dev_node_from_fd(int fd
, unsigned int *maj
, unsigned int *min
)
253 if (fstat(fd
, &buf
) < 0) {
254 log_(_LOADER_WARNING
, "MESA-LOADER: failed to stat fd %d\n", fd
);
258 if (!S_ISCHR(buf
.st_mode
)) {
259 log_(_LOADER_WARNING
, "MESA-LOADER: fd %d not a character device\n", fd
);
263 *maj
= major(buf
.st_rdev
);
264 *min
= minor(buf
.st_rdev
);
270 #if defined(HAVE_LIBDRM)
273 drm_get_pci_id_for_fd(int fd
, int *vendor_id
, int *chip_id
)
278 if (drmGetDevice(fd
, &device
) == 0) {
279 if (device
->bustype
== DRM_BUS_PCI
) {
280 *vendor_id
= device
->deviceinfo
.pci
->vendor_id
;
281 *chip_id
= device
->deviceinfo
.pci
->device_id
;
285 log_(_LOADER_WARNING
, "MESA-LOADER: device is not located on the PCI bus\n");
288 drmFreeDevice(&device
);
291 log_(_LOADER_WARNING
, "MESA-LOADER: failed to retrieve device information\n");
301 loader_get_pci_id_for_fd(int fd
, int *vendor_id
, int *chip_id
)
304 if (drm_get_pci_id_for_fd(fd
, vendor_id
, chip_id
))
311 #if defined(HAVE_LIBDRM)
313 drm_get_device_name_for_fd(int fd
)
315 unsigned int maj
, min
;
319 if (dev_node_from_fd(fd
, &maj
, &min
) < 0)
322 n
= snprintf(buf
, sizeof(buf
), DRM_DEV_NAME
, DRM_DIR_NAME
, min
);
323 if (n
== -1 || n
>= sizeof(buf
))
331 loader_get_device_name_for_fd(int fd
)
336 if ((result
= drm_get_device_name_for_fd(fd
)))
343 loader_get_driver_for_fd(int fd
)
345 int vendor_id
, chip_id
, i
, j
;
348 if (!loader_get_pci_id_for_fd(fd
, &vendor_id
, &chip_id
)) {
351 /* fallback to drmGetVersion(): */
352 drmVersionPtr version
= drmGetVersion(fd
);
355 log_(_LOADER_WARNING
, "failed to get driver name for fd %d\n", fd
);
359 driver
= strndup(version
->name
, version
->name_len
);
360 log_(_LOADER_INFO
, "using driver %s for %d\n", driver
, fd
);
362 drmFreeVersion(version
);
368 for (i
= 0; driver_map
[i
].driver
; i
++) {
369 if (vendor_id
!= driver_map
[i
].vendor_id
)
372 if (driver_map
[i
].predicate
&& !driver_map
[i
].predicate(fd
))
375 if (driver_map
[i
].num_chips_ids
== -1) {
376 driver
= strdup(driver_map
[i
].driver
);
380 for (j
= 0; j
< driver_map
[i
].num_chips_ids
; j
++)
381 if (driver_map
[i
].chip_ids
[j
] == chip_id
) {
382 driver
= strdup(driver_map
[i
].driver
);
388 log_(driver
? _LOADER_DEBUG
: _LOADER_WARNING
,
389 "pci id for fd %d: %04x:%04x, driver %s\n",
390 fd
, vendor_id
, chip_id
, driver
);
395 loader_set_logger(void (*logger
)(int level
, const char *fmt
, ...))
400 /* XXX: Local definition to avoid pulling the heavyweight GL/gl.h and
401 * GL/internal/dri_interface.h
404 #ifndef __DRI_DRIVER_GET_EXTENSIONS
405 #define __DRI_DRIVER_GET_EXTENSIONS "__driDriverGetExtensions"
409 loader_get_extensions_name(const char *driver_name
)
413 if (asprintf(&name
, "%s_%s", __DRI_DRIVER_GET_EXTENSIONS
, driver_name
) < 0)
416 const size_t len
= strlen(name
);
417 for (size_t i
= 0; i
< len
; i
++) {