#include <string.h>
#include <unistd.h>
#include <stdlib.h>
+#include <limits.h>
#include <sys/param.h>
#ifdef MAJOR_IN_MKDEV
#include <sys/mkdev.h>
#ifdef HAVE_LIBDRM
#include <xf86drm.h>
+#define MAX_DRM_DEVICES 64
#ifdef USE_DRICONF
#include "util/xmlconfig.h"
#include "util/xmlpool.h"
#endif
#endif
+#include "util/macros.h"
+
#define __IS_LOADER
#include "pci_id_driver_map.h"
+/* For systems like Hurd */
+#ifndef PATH_MAX
+#define PATH_MAX 4096
+#endif
+
static void default_logger(int level, const char *fmt, ...)
{
if (level <= _LOADER_WARNING) {
if (fd != -1)
fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
}
+ if (fd == -1 && errno == EACCES) {
+ log_(_LOADER_WARNING, "failed to open %s: %s\n",
+ device_name, strerror(errno));
+ }
return fd;
}
#endif
}
+bool
+is_kernel_i915(int fd)
+{
+ char *kernel_driver = loader_get_kernel_driver_name(fd);
+ bool is_i915 = kernel_driver && strcmp(kernel_driver, "i915") == 0;
+
+ free(kernel_driver);
+ return is_i915;
+}
+
#if defined(HAVE_LIBDRM)
int
loader_open_render_node(const char *name)
{
- drmDevicePtr *devices, device;
- int err, render = -ENOENT, fd;
- unsigned int num, i;
-
- err = drmGetDevices2(0, NULL, 0);
- if (err < 0)
- return err;
-
- num = err;
+ drmDevicePtr devices[MAX_DRM_DEVICES], device;
+ int i, num_devices, fd = -1;
- devices = calloc(num, sizeof(*devices));
- if (!devices)
- return -ENOMEM;
-
- err = drmGetDevices2(0, devices, num);
- if (err < 0) {
- render = err;
- goto free;
- }
+ num_devices = drmGetDevices2(0, devices, MAX_DRM_DEVICES);
+ if (num_devices <= 0)
+ return -ENOENT;
- for (i = 0; i < num; i++) {
+ for (i = 0; i < num_devices; i++) {
device = devices[i];
if ((device->available_nodes & (1 << DRM_NODE_RENDER)) &&
}
drmFreeVersion(version);
- render = fd;
break;
}
}
+ drmFreeDevices(devices, num_devices);
- drmFreeDevices(devices, num);
+ if (i == num_devices)
+ return -ENOENT;
-free:
- free(devices);
- return render;
+ return fd;
}
#ifdef USE_DRICONF
driParseOptionInfo(&defaultInitOptions, __driConfigOptionsLoader);
driParseConfigFiles(&userInitOptions, &defaultInitOptions, 0,
- "loader", kernel_driver);
+ "loader", kernel_driver, NULL, 0);
if (driCheckOption(&userInitOptions, "dri_driver", DRI_STRING)) {
char *opt = driQueryOptionstr(&userInitOptions, "dri_driver");
/* not an empty string */
char *prime = NULL;
driParseOptionInfo(&defaultInitOptions, __driConfigOptionsLoader);
- driParseConfigFiles(&userInitOptions, &defaultInitOptions, 0, "loader", NULL);
+ driParseConfigFiles(&userInitOptions, &defaultInitOptions, 0,
+ "loader", NULL, NULL, 0);
if (driCheckOption(&userInitOptions, "device_id", DRI_STRING))
prime = strdup(driQueryOptionstr(&userInitOptions, "device_id"));
driDestroyOptionCache(&userInitOptions);
int loader_get_user_preferred_fd(int default_fd, bool *different_device)
{
-/* Arbitrary "maximum" value of drm devices. */
-#define MAX_DRM_DEVICES 32
const char *dri_prime = getenv("DRI_PRIME");
char *default_tag, *prime = NULL;
drmDevicePtr devices[MAX_DRM_DEVICES];
- int i, num_devices, fd;
- bool found = false;
+ int i, num_devices, fd = -1;
if (dri_prime)
prime = strdup(dri_prime);
goto err;
num_devices = drmGetDevices2(0, devices, MAX_DRM_DEVICES);
- if (num_devices < 0)
+ if (num_devices <= 0)
goto err;
- /* two format are supported:
- * "1": choose any other card than the card used by default.
- * id_path_tag: (for example "pci-0000_02_00_0") choose the card
- * with this id_path_tag.
- */
- if (!strcmp(prime,"1")) {
- /* Hmm... detection for 2-7 seems to be broken. Oh well ...
- * Pick the first render device that is not our own.
- */
- for (i = 0; i < num_devices; i++) {
- if (devices[i]->available_nodes & 1 << DRM_NODE_RENDER &&
- !drm_device_matches_tag(devices[i], default_tag)) {
+ for (i = 0; i < num_devices; i++) {
+ if (!(devices[i]->available_nodes & 1 << DRM_NODE_RENDER))
+ continue;
- found = true;
- break;
- }
+ /* two formats of DRI_PRIME are supported:
+ * "1": choose any other card than the card used by default.
+ * id_path_tag: (for example "pci-0000_02_00_0") choose the card
+ * with this id_path_tag.
+ */
+ if (!strcmp(prime,"1")) {
+ if (drm_device_matches_tag(devices[i], default_tag))
+ continue;
+ } else {
+ if (!drm_device_matches_tag(devices[i], prime))
+ continue;
}
- } else {
- for (i = 0; i < num_devices; i++) {
- if (devices[i]->available_nodes & 1 << DRM_NODE_RENDER &&
- drm_device_matches_tag(devices[i], prime)) {
- found = true;
- break;
- }
- }
+ fd = loader_open_device(devices[i]->nodes[DRM_NODE_RENDER]);
+ break;
}
+ drmFreeDevices(devices, num_devices);
- if (!found) {
- drmFreeDevices(devices, num_devices);
+ if (i == num_devices)
goto err;
- }
- fd = loader_open_device(devices[i]->nodes[DRM_NODE_RENDER]);
- drmFreeDevices(devices, num_devices);
if (fd < 0)
goto err;
#if defined(HAVE_LIBDRM)
-static int
+static bool
drm_get_pci_id_for_fd(int fd, int *vendor_id, int *chip_id)
{
drmDevicePtr device;
- int ret;
+ bool ret;
if (drmGetDevice2(fd, 0, &device) == 0) {
if (device->bustype == DRM_BUS_PCI) {
*vendor_id = device->deviceinfo.pci->vendor_id;
*chip_id = device->deviceinfo.pci->device_id;
- ret = 1;
+ ret = true;
}
else {
log_(_LOADER_DEBUG, "MESA-LOADER: device is not located on the PCI bus\n");
- ret = 0;
+ ret = false;
}
drmFreeDevice(&device);
}
else {
log_(_LOADER_WARNING, "MESA-LOADER: failed to retrieve device information\n");
- ret = 0;
+ ret = false;
}
return ret;
#endif
-int
+bool
loader_get_pci_id_for_fd(int fd, int *vendor_id, int *chip_id)
{
#if HAVE_LIBDRM
- if (drm_get_pci_id_for_fd(fd, vendor_id, chip_id))
- return 1;
+ return drm_get_pci_id_for_fd(fd, vendor_id, chip_id);
#endif
- return 0;
+ return false;
}
char *
return driver;
}
- for (i = 0; driver_map[i].driver; i++) {
+ for (i = 0; i < ARRAY_SIZE(driver_map); i++) {
if (vendor_id != driver_map[i].vendor_id)
continue;
next = end;
len = next - p;
-#if GLX_USE_TLS
+#if USE_ELF_TLS
snprintf(path, sizeof(path), "%.*s/tls/%s_dri.so", len, p, driver_name);
driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
#endif