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>
40
#include <sys/param.h>
42
#include <sys/mkdev.h>
44
#ifdef MAJOR_IN_SYSMACROS
45
#include <sys/sysmacros.h>
48
#include <GL/internal/dri_interface.h>
50
#include "util/os_file.h"
54
#define MAX_DRM_DEVICES 64
56
#include "util/xmlconfig.h"
57
#include "util/driconf.h"
61
#include "util/macros.h"
64
#include "pci_id_driver_map.h"
66
/* For systems like Hurd */
71
static void default_logger(int level, const char *fmt, ...)
73
if (level <= _LOADER_WARNING) {
76
vfprintf(stderr, fmt, args);
81
static loader_logger *log_ = default_logger;
84
loader_open_device(const char *device_name)
88
fd = open(device_name, O_RDWR | O_CLOEXEC);
89
if (fd == -1 && errno == EINVAL)
92
fd = open(device_name, O_RDWR);
94
fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
96
if (fd == -1 && errno == EACCES) {
97
log_(_LOADER_WARNING, "failed to open %s: %s\n",
98
device_name, strerror(errno));
103
static char *loader_get_kernel_driver_name(int fd)
107
drmVersionPtr version = drmGetVersion(fd);
110
log_(_LOADER_WARNING, "failed to get driver name for fd %d\n", fd);
114
driver = strndup(version->name, version->name_len);
115
log_(driver ? _LOADER_DEBUG : _LOADER_WARNING, "using driver %s for %d\n",
118
drmFreeVersion(version);
126
is_kernel_i915(int fd)
128
char *kernel_driver = loader_get_kernel_driver_name(fd);
129
bool is_i915 = kernel_driver && strcmp(kernel_driver, "i915") == 0;
135
#if defined(HAVE_LIBDRM)
137
loader_open_render_node(const char *name)
139
drmDevicePtr devices[MAX_DRM_DEVICES], device;
140
int i, num_devices, fd = -1;
142
num_devices = drmGetDevices2(0, devices, MAX_DRM_DEVICES);
143
if (num_devices <= 0)
146
for (i = 0; i < num_devices; i++) {
149
if ((device->available_nodes & (1 << DRM_NODE_RENDER)) &&
150
(device->bustype == DRM_BUS_PLATFORM)) {
151
drmVersionPtr version;
153
fd = loader_open_device(device->nodes[DRM_NODE_RENDER]);
157
version = drmGetVersion(fd);
163
if (strcmp(version->name, name) != 0) {
164
drmFreeVersion(version);
169
drmFreeVersion(version);
173
drmFreeDevices(devices, num_devices);
175
if (i == num_devices)
182
loader_get_render_node(dev_t device)
184
char *render_node = NULL;
185
drmDevicePtr dev_ptr;
187
if (drmGetDeviceFromDevId(device, 0, &dev_ptr) < 0)
190
if (dev_ptr->available_nodes & (1 << DRM_NODE_RENDER)) {
191
render_node = strdup(dev_ptr->nodes[DRM_NODE_RENDER]);
193
log_(_LOADER_DEBUG, "MESA-LOADER: failed to allocate memory for render node\n");
196
drmFreeDevice(&dev_ptr);
202
static const driOptionDescription __driConfigOptionsLoader[] = {
203
DRI_CONF_SECTION_INITIALIZATION
204
DRI_CONF_DEVICE_ID_PATH_TAG()
205
DRI_CONF_DRI_DRIVER()
209
static char *loader_get_dri_config_driver(int fd)
211
driOptionCache defaultInitOptions;
212
driOptionCache userInitOptions;
213
char *dri_driver = NULL;
214
char *kernel_driver = loader_get_kernel_driver_name(fd);
216
driParseOptionInfo(&defaultInitOptions, __driConfigOptionsLoader,
217
ARRAY_SIZE(__driConfigOptionsLoader));
218
driParseConfigFiles(&userInitOptions, &defaultInitOptions, 0,
219
"loader", kernel_driver, NULL, NULL, 0, NULL, 0);
220
if (driCheckOption(&userInitOptions, "dri_driver", DRI_STRING)) {
221
char *opt = driQueryOptionstr(&userInitOptions, "dri_driver");
222
/* not an empty string */
224
dri_driver = strdup(opt);
226
driDestroyOptionCache(&userInitOptions);
227
driDestroyOptionInfo(&defaultInitOptions);
233
static char *loader_get_dri_config_device_id(void)
235
driOptionCache defaultInitOptions;
236
driOptionCache userInitOptions;
239
driParseOptionInfo(&defaultInitOptions, __driConfigOptionsLoader,
240
ARRAY_SIZE(__driConfigOptionsLoader));
241
driParseConfigFiles(&userInitOptions, &defaultInitOptions, 0,
242
"loader", NULL, NULL, NULL, 0, NULL, 0);
243
if (driCheckOption(&userInitOptions, "device_id", DRI_STRING))
244
prime = strdup(driQueryOptionstr(&userInitOptions, "device_id"));
245
driDestroyOptionCache(&userInitOptions);
246
driDestroyOptionInfo(&defaultInitOptions);
252
static char *drm_construct_id_path_tag(drmDevicePtr device)
256
if (device->bustype == DRM_BUS_PCI) {
257
if (asprintf(&tag, "pci-%04x_%02x_%02x_%1u",
258
device->businfo.pci->domain,
259
device->businfo.pci->bus,
260
device->businfo.pci->dev,
261
device->businfo.pci->func) < 0) {
264
} else if (device->bustype == DRM_BUS_PLATFORM ||
265
device->bustype == DRM_BUS_HOST1X) {
266
char *fullname, *name, *address;
268
if (device->bustype == DRM_BUS_PLATFORM)
269
fullname = device->businfo.platform->fullname;
271
fullname = device->businfo.host1x->fullname;
273
name = strrchr(fullname, '/');
275
name = strdup(fullname);
277
name = strdup(name + 1);
279
address = strchr(name, '@');
283
if (asprintf(&tag, "platform-%s_%s", address, name) < 0)
286
if (asprintf(&tag, "platform-%s", name) < 0)
295
static bool drm_device_matches_tag(drmDevicePtr device, const char *prime_tag)
297
char *tag = drm_construct_id_path_tag(device);
303
ret = strcmp(tag, prime_tag);
309
static char *drm_get_id_path_tag_for_fd(int fd)
314
if (drmGetDevice2(fd, 0, &device) != 0)
317
tag = drm_construct_id_path_tag(device);
318
drmFreeDevice(&device);
322
int loader_get_user_preferred_fd(int default_fd, bool *different_device)
324
const char *dri_prime = getenv("DRI_PRIME");
325
char *default_tag, *prime = NULL;
326
drmDevicePtr devices[MAX_DRM_DEVICES];
327
int i, num_devices, fd = -1;
330
prime = strdup(dri_prime);
333
prime = loader_get_dri_config_device_id();
337
*different_device = false;
341
default_tag = drm_get_id_path_tag_for_fd(default_fd);
342
if (default_tag == NULL)
345
num_devices = drmGetDevices2(0, devices, MAX_DRM_DEVICES);
346
if (num_devices <= 0)
349
for (i = 0; i < num_devices; i++) {
350
if (!(devices[i]->available_nodes & 1 << DRM_NODE_RENDER))
353
/* two formats of DRI_PRIME are supported:
354
* "1": choose any other card than the card used by default.
355
* id_path_tag: (for example "pci-0000_02_00_0") choose the card
356
* with this id_path_tag.
358
if (!strcmp(prime,"1")) {
359
if (drm_device_matches_tag(devices[i], default_tag))
362
if (!drm_device_matches_tag(devices[i], prime))
366
fd = loader_open_device(devices[i]->nodes[DRM_NODE_RENDER]);
369
drmFreeDevices(devices, num_devices);
371
if (i == num_devices)
379
*different_device = !!strcmp(default_tag, prime);
386
*different_device = false;
394
loader_open_render_node(const char *name)
400
loader_get_render_node(dev_t device)
405
int loader_get_user_preferred_fd(int default_fd, bool *different_device)
407
*different_device = false;
412
#if defined(HAVE_LIBDRM)
415
drm_get_pci_id_for_fd(int fd, int *vendor_id, int *chip_id)
419
if (drmGetDevice2(fd, 0, &device) != 0) {
420
log_(_LOADER_WARNING, "MESA-LOADER: failed to retrieve device information\n");
424
if (device->bustype != DRM_BUS_PCI) {
425
drmFreeDevice(&device);
426
log_(_LOADER_DEBUG, "MESA-LOADER: device is not located on the PCI bus\n");
430
*vendor_id = device->deviceinfo.pci->vendor_id;
431
*chip_id = device->deviceinfo.pci->device_id;
432
drmFreeDevice(&device);
438
static int loader_get_linux_pci_field(int maj, int min, const char *field)
440
char path[PATH_MAX + 1];
441
snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device/%s", maj, min, field);
443
char *field_str = os_read_file(path, NULL);
445
/* Probably non-PCI device. */
449
int value = (int)strtoll(field_str, NULL, 16);
456
loader_get_linux_pci_id_for_fd(int fd, int *vendor_id, int *chip_id)
459
if (fstat(fd, &sbuf) != 0) {
460
log_(_LOADER_DEBUG, "MESA-LOADER: failed to fstat fd\n");
464
int maj = major(sbuf.st_rdev);
465
int min = minor(sbuf.st_rdev);
467
*vendor_id = loader_get_linux_pci_field(maj, min, "vendor");
468
*chip_id = loader_get_linux_pci_field(maj, min, "device");
470
return *vendor_id && *chip_id;
472
#endif /* __linux__ */
475
loader_get_pci_id_for_fd(int fd, int *vendor_id, int *chip_id)
478
/* Implementation without causing full enumeration of DRM devices. */
479
if (loader_get_linux_pci_id_for_fd(fd, vendor_id, chip_id))
484
return drm_get_pci_id_for_fd(fd, vendor_id, chip_id);
490
loader_get_device_name_for_fd(int fd)
495
result = drmGetDeviceNameFromFd2(fd);
502
loader_get_pci_driver(int fd)
504
int vendor_id, chip_id, i, j;
507
if (!loader_get_pci_id_for_fd(fd, &vendor_id, &chip_id))
510
for (i = 0; i < ARRAY_SIZE(driver_map); i++) {
511
if (vendor_id != driver_map[i].vendor_id)
514
if (driver_map[i].predicate && !driver_map[i].predicate(fd))
517
if (driver_map[i].num_chips_ids == -1) {
518
driver = strdup(driver_map[i].driver);
522
for (j = 0; j < driver_map[i].num_chips_ids; j++)
523
if (driver_map[i].chip_ids[j] == chip_id) {
524
driver = strdup(driver_map[i].driver);
530
log_(driver ? _LOADER_DEBUG : _LOADER_WARNING,
531
"pci id for fd %d: %04x:%04x, driver %s\n",
532
fd, vendor_id, chip_id, driver);
537
loader_get_driver_for_fd(int fd)
541
/* Allow an environment variable to force choosing a different driver
542
* binary. If that driver binary can't survive on this FD, that's the
543
* user's problem, but this allows vc4 simulator to run on an i965 host,
544
* and may be useful for some touch testing of i915 on an i965 host.
546
if (geteuid() == getuid()) {
547
driver = getenv("MESA_LOADER_DRIVER_OVERRIDE");
549
return strdup(driver);
552
#if defined(HAVE_LIBDRM) && defined(USE_DRICONF)
553
driver = loader_get_dri_config_driver(fd);
558
driver = loader_get_pci_driver(fd);
560
driver = loader_get_kernel_driver_name(fd);
566
loader_set_logger(loader_logger *logger)
572
loader_get_extensions_name(const char *driver_name)
576
if (asprintf(&name, "%s_%s", __DRI_DRIVER_GET_EXTENSIONS, driver_name) < 0)
579
const size_t len = strlen(name);
580
for (size_t i = 0; i < len; i++) {
589
* Opens a driver or backend using its name, returning the library handle.
591
* \param driverName - a name like "i965", "radeon", "nouveau", etc.
592
* \param lib_suffix - a suffix to append to the driver name to generate the
594
* \param search_path_vars - NULL-terminated list of env vars that can be used
595
* \param default_search_path - a colon-separted list of directories used if
596
* search_path_vars is NULL or none of the vars are set in the environment.
597
* \param warn_on_fail - Log a warning if the driver is not found.
600
loader_open_driver_lib(const char *driver_name,
601
const char *lib_suffix,
602
const char **search_path_vars,
603
const char *default_search_path,
607
const char *search_paths, *next, *end;
610
if (geteuid() == getuid() && search_path_vars) {
611
for (int i = 0; search_path_vars[i] != NULL; i++) {
612
search_paths = getenv(search_path_vars[i]);
617
if (search_paths == NULL)
618
search_paths = default_search_path;
621
const char *dl_error = NULL;
622
end = search_paths + strlen(search_paths);
623
for (const char *p = search_paths; p < end; p = next + 1) {
625
next = strchr(p, ':');
631
snprintf(path, sizeof(path), "%.*s/tls/%s%s.so", len,
632
p, driver_name, lib_suffix);
633
driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
635
if (driver == NULL) {
636
snprintf(path, sizeof(path), "%.*s/%s%s.so", len,
637
p, driver_name, lib_suffix);
638
driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
639
if (driver == NULL) {
640
dl_error = dlerror();
641
log_(_LOADER_DEBUG, "MESA-LOADER: failed to open %s: %s\n",
645
/* not need continue to loop all paths once the driver is found */
650
if (driver == NULL) {
652
log_(_LOADER_WARNING,
653
"MESA-LOADER: failed to open %s: %s (search paths %s, suffix %s)\n",
654
driver_name, dl_error, search_paths, lib_suffix);
659
log_(_LOADER_DEBUG, "MESA-LOADER: dlopen(%s)\n", path);
665
* Opens a DRI driver using its driver name, returning the __DRIextension
668
* \param driverName - a name like "i965", "radeon", "nouveau", etc.
669
* \param out_driver - Address where the dlopen() return value will be stored.
670
* \param search_path_vars - NULL-terminated list of env vars that can be used
671
* to override the DEFAULT_DRIVER_DIR search path.
673
const struct __DRIextensionRec **
674
loader_open_driver(const char *driver_name,
675
void **out_driver_handle,
676
const char **search_path_vars)
678
char *get_extensions_name;
679
const struct __DRIextensionRec **extensions = NULL;
680
const struct __DRIextensionRec **(*get_extensions)(void);
681
void *driver = loader_open_driver_lib(driver_name, "_dri", search_path_vars,
682
DEFAULT_DRIVER_DIR, true);
687
get_extensions_name = loader_get_extensions_name(driver_name);
688
if (get_extensions_name) {
689
get_extensions = dlsym(driver, get_extensions_name);
690
if (get_extensions) {
691
extensions = get_extensions();
693
log_(_LOADER_DEBUG, "MESA-LOADER: driver does not expose %s(): %s\n",
694
get_extensions_name, dlerror());
696
free(get_extensions_name);
700
extensions = dlsym(driver, __DRI_DRIVER_EXTENSIONS);
701
if (extensions == NULL) {
702
log_(_LOADER_WARNING,
703
"MESA-LOADER: driver exports no extensions (%s)\n", dlerror());
709
*out_driver_handle = driver;