X-Git-Url: https://git.libre-soc.org/?p=mesa.git;a=blobdiff_plain;f=src%2Floader%2Floader.c;h=668e6d5184b09ecd1e09e757b746938a6c74b608;hp=3b28a0e7db7a4c26f5cdb9fb1706f2bbd7a27439;hb=e45dc931362b90c18b31b23ee1a82e721dbdc38e;hpb=de6e6a347dd12c079a098f3f3095394eed326166 diff --git a/src/loader/loader.c b/src/loader/loader.c index 3b28a0e7db7..668e6d5184b 100644 --- a/src/loader/loader.c +++ b/src/loader/loader.c @@ -26,6 +26,7 @@ * Rob Clark */ +#include #include #include #include @@ -35,25 +36,36 @@ #include #include #include +#include +#include #ifdef MAJOR_IN_MKDEV #include #endif #ifdef MAJOR_IN_SYSMACROS #include #endif +#include +#include #include "loader.h" #ifdef HAVE_LIBDRM #include #ifdef USE_DRICONF -#include "xmlconfig.h" -#include "xmlpool.h" +#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) { @@ -64,7 +76,7 @@ static void default_logger(int level, const char *fmt, ...) } } -static void (*log_)(int level, const char *fmt, ...) = default_logger; +static loader_logger *log_ = default_logger; int loader_open_device(const char *device_name) @@ -82,15 +94,118 @@ loader_open_device(const char *device_name) return fd; } +static char *loader_get_kernel_driver_name(int fd) +{ +#if HAVE_LIBDRM + char *driver; + drmVersionPtr version = drmGetVersion(fd); + + if (!version) { + log_(_LOADER_WARNING, "failed to get driver name for fd %d\n", fd); + return NULL; + } + + driver = strndup(version->name, version->name_len); + + drmFreeVersion(version); + return driver; +#else + return NULL; +#endif +} + #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; + + devices = calloc(num, sizeof(*devices)); + if (!devices) + return -ENOMEM; + + err = drmGetDevices2(0, devices, num); + if (err < 0) { + render = err; + goto free; + } + + for (i = 0; i < num; i++) { + device = devices[i]; + + if ((device->available_nodes & (1 << DRM_NODE_RENDER)) && + (device->bustype == DRM_BUS_PLATFORM)) { + drmVersionPtr version; + + fd = loader_open_device(device->nodes[DRM_NODE_RENDER]); + if (fd < 0) + continue; + + version = drmGetVersion(fd); + if (!version) { + close(fd); + continue; + } + + if (strcmp(version->name, name) != 0) { + drmFreeVersion(version); + close(fd); + continue; + } + + drmFreeVersion(version); + render = fd; + break; + } + } + + drmFreeDevices(devices, num); + +free: + free(devices); + return render; +} + #ifdef USE_DRICONF static const char __driConfigOptionsLoader[] = DRI_CONF_BEGIN DRI_CONF_SECTION_INITIALIZATION DRI_CONF_DEVICE_ID_PATH_TAG() + DRI_CONF_DRI_DRIVER() DRI_CONF_SECTION_END DRI_CONF_END; +static char *loader_get_dri_config_driver(int fd) +{ + driOptionCache defaultInitOptions; + driOptionCache userInitOptions; + char *dri_driver = NULL; + char *kernel_driver = loader_get_kernel_driver_name(fd); + + driParseOptionInfo(&defaultInitOptions, __driConfigOptionsLoader); + driParseConfigFiles(&userInitOptions, &defaultInitOptions, 0, + "loader", kernel_driver, NULL, 0); + if (driCheckOption(&userInitOptions, "dri_driver", DRI_STRING)) { + char *opt = driQueryOptionstr(&userInitOptions, "dri_driver"); + /* not an empty string */ + if (*opt) + dri_driver = strdup(opt); + } + driDestroyOptionCache(&userInitOptions); + driDestroyOptionInfo(&defaultInitOptions); + + free(kernel_driver); + return dri_driver; +} + static char *loader_get_dri_config_device_id(void) { driOptionCache defaultInitOptions; @@ -98,7 +213,8 @@ static char *loader_get_dri_config_device_id(void) char *prime = NULL; driParseOptionInfo(&defaultInitOptions, __driConfigOptionsLoader); - driParseConfigFiles(&userInitOptions, &defaultInitOptions, 0, "loader"); + driParseConfigFiles(&userInitOptions, &defaultInitOptions, 0, + "loader", NULL, NULL, 0); if (driCheckOption(&userInitOptions, "device_id", DRI_STRING)) prime = strdup(driQueryOptionstr(&userInitOptions, "device_id")); driDestroyOptionCache(&userInitOptions); @@ -110,18 +226,43 @@ static char *loader_get_dri_config_device_id(void) static char *drm_construct_id_path_tag(drmDevicePtr device) { -/* Length of "pci-xxxx_xx_xx_x\0" */ -#define PCI_ID_PATH_TAG_LENGTH 17 char *tag = NULL; if (device->bustype == DRM_BUS_PCI) { - tag = calloc(PCI_ID_PATH_TAG_LENGTH, sizeof(char)); - if (tag == NULL) - return NULL; + if (asprintf(&tag, "pci-%04x_%02x_%02x_%1u", + device->businfo.pci->domain, + device->businfo.pci->bus, + device->businfo.pci->dev, + device->businfo.pci->func) < 0) { + return NULL; + } + } else if (device->bustype == DRM_BUS_PLATFORM || + device->bustype == DRM_BUS_HOST1X) { + char *fullname, *name, *address; + + if (device->bustype == DRM_BUS_PLATFORM) + fullname = device->businfo.platform->fullname; + else + fullname = device->businfo.host1x->fullname; + + name = strrchr(fullname, '/'); + if (!name) + name = strdup(fullname); + else + name = strdup(name + 1); + + address = strchr(name, '@'); + if (address) { + *address++ = '\0'; + + if (asprintf(&tag, "platform-%s_%s", address, name) < 0) + tag = NULL; + } else { + if (asprintf(&tag, "platform-%s", name) < 0) + tag = NULL; + } - snprintf(tag, PCI_ID_PATH_TAG_LENGTH, "pci-%04x_%02x_%02x_%1u", - device->businfo.pci->domain, device->businfo.pci->bus, - device->businfo.pci->dev, device->businfo.pci->func); + free(name); } return tag; } @@ -145,7 +286,7 @@ static char *drm_get_id_path_tag_for_fd(int fd) drmDevicePtr device; char *tag; - if (drmGetDevice(fd, &device) != 0) + if (drmGetDevice2(fd, 0, &device) != 0) return NULL; tag = drm_construct_id_path_tag(device); @@ -153,7 +294,7 @@ static char *drm_get_id_path_tag_for_fd(int fd) return tag; } -int loader_get_user_preferred_fd(int default_fd, int *different_device) +int loader_get_user_preferred_fd(int default_fd, bool *different_device) { /* Arbitrary "maximum" value of drm devices. */ #define MAX_DRM_DEVICES 32 @@ -171,7 +312,7 @@ int loader_get_user_preferred_fd(int default_fd, int *different_device) #endif if (prime == NULL) { - *different_device = 0; + *different_device = false; return default_fd; } @@ -179,7 +320,7 @@ int loader_get_user_preferred_fd(int default_fd, int *different_device) if (default_tag == NULL) goto err; - num_devices = drmGetDevices(devices, MAX_DRM_DEVICES); + num_devices = drmGetDevices2(0, devices, MAX_DRM_DEVICES); if (num_devices < 0) goto err; @@ -230,66 +371,49 @@ int loader_get_user_preferred_fd(int default_fd, int *different_device) return fd; err: - *different_device = 0; + *different_device = false; free(default_tag); free(prime); return default_fd; } #else -int loader_get_user_preferred_fd(int default_fd, int *different_device) +int +loader_open_render_node(const char *name) { - *different_device = 0; - return default_fd; + return -1; } -#endif -#if defined(HAVE_LIBDRM) -static int -dev_node_from_fd(int fd, unsigned int *maj, unsigned int *min) +int loader_get_user_preferred_fd(int default_fd, bool *different_device) { - struct stat buf; - - if (fstat(fd, &buf) < 0) { - log_(_LOADER_WARNING, "MESA-LOADER: failed to stat fd %d\n", fd); - return -1; - } - - if (!S_ISCHR(buf.st_mode)) { - log_(_LOADER_WARNING, "MESA-LOADER: fd %d not a character device\n", fd); - return -1; - } - - *maj = major(buf.st_rdev); - *min = minor(buf.st_rdev); - - return 0; + *different_device = false; + return default_fd; } #endif #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 (drmGetDevice(fd, &device) == 0) { + 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_WARNING, "MESA-LOADER: device is not located on the PCI bus\n"); - ret = 0; + log_(_LOADER_DEBUG, "MESA-LOADER: device is not located on the PCI bus\n"); + ret = false; } drmFreeDevice(&device); } else { log_(_LOADER_WARNING, "MESA-LOADER: failed to retrieve device information\n"); - ret = 0; + ret = false; } return ret; @@ -297,35 +421,14 @@ drm_get_pci_id_for_fd(int fd, int *vendor_id, int *chip_id) #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; -} - - -#if defined(HAVE_LIBDRM) -static char * -drm_get_device_name_for_fd(int fd) -{ - unsigned int maj, min; - char buf[0x40]; - int n; - - if (dev_node_from_fd(fd, &maj, &min) < 0) - return NULL; - - n = snprintf(buf, sizeof(buf), DRM_DEV_NAME, DRM_DIR_NAME, min); - if (n == -1 || n >= sizeof(buf)) - return NULL; - - return strdup(buf); + return false; } -#endif char * loader_get_device_name_for_fd(int fd) @@ -333,9 +436,9 @@ loader_get_device_name_for_fd(int fd) char *result = NULL; #if HAVE_LIBDRM - if ((result = drm_get_device_name_for_fd(fd))) - return result; + result = drmGetDeviceNameFromFd2(fd); #endif + return result; } @@ -356,27 +459,20 @@ loader_get_driver_for_fd(int fd) return strdup(driver); } - if (!loader_get_pci_id_for_fd(fd, &vendor_id, &chip_id)) { - -#if HAVE_LIBDRM - /* fallback to drmGetVersion(): */ - drmVersionPtr version = drmGetVersion(fd); - - if (!version) { - log_(_LOADER_WARNING, "failed to get driver name for fd %d\n", fd); - return NULL; - } - - driver = strndup(version->name, version->name_len); - log_(_LOADER_INFO, "using driver %s for %d\n", driver, fd); - - drmFreeVersion(version); +#if defined(HAVE_LIBDRM) && defined(USE_DRICONF) + driver = loader_get_dri_config_driver(fd); + if (driver) + return driver; #endif + if (!loader_get_pci_id_for_fd(fd, &vendor_id, &chip_id)) { + driver = loader_get_kernel_driver_name(fd); + if (driver) + log_(_LOADER_INFO, "using driver %s for %d\n", driver, fd); 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; @@ -403,19 +499,11 @@ out: } void -loader_set_logger(void (*logger)(int level, const char *fmt, ...)) +loader_set_logger(loader_logger *logger) { log_ = logger; } -/* XXX: Local definition to avoid pulling the heavyweight GL/gl.h and - * GL/internal/dri_interface.h - */ - -#ifndef __DRI_DRIVER_GET_EXTENSIONS -#define __DRI_DRIVER_GET_EXTENSIONS "__driDriverGetExtensions" -#endif - char * loader_get_extensions_name(const char *driver_name) { @@ -426,9 +514,97 @@ loader_get_extensions_name(const char *driver_name) const size_t len = strlen(name); for (size_t i = 0; i < len; i++) { - if (name[i] == '-') - name[i] = '_'; + if (name[i] == '-') + name[i] = '_'; } return name; } + +/** + * Opens a DRI driver using its driver name, returning the __DRIextension + * entrypoints. + * + * \param driverName - a name like "i965", "radeon", "nouveau", etc. + * \param out_driver - Address where the dlopen() return value will be stored. + * \param search_path_vars - NULL-terminated list of env vars that can be used + * to override the DEFAULT_DRIVER_DIR search path. + */ +const struct __DRIextensionRec ** +loader_open_driver(const char *driver_name, + void **out_driver_handle, + const char **search_path_vars) +{ + char path[PATH_MAX], *search_paths, *next, *end; + char *get_extensions_name; + const struct __DRIextensionRec **extensions = NULL; + const struct __DRIextensionRec **(*get_extensions)(void); + + search_paths = NULL; + if (geteuid() == getuid() && search_path_vars) { + for (int i = 0; search_path_vars[i] != NULL; i++) { + search_paths = getenv(search_path_vars[i]); + if (search_paths) + break; + } + } + if (search_paths == NULL) + search_paths = DEFAULT_DRIVER_DIR; + + void *driver = NULL; + end = search_paths + strlen(search_paths); + for (char *p = search_paths; p < end; p = next + 1) { + int len; + next = strchr(p, ':'); + if (next == NULL) + next = end; + + len = next - p; +#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 + if (driver == NULL) { + snprintf(path, sizeof(path), "%.*s/%s_dri.so", len, p, driver_name); + driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL); + if (driver == NULL) + log_(_LOADER_DEBUG, "MESA-LOADER: failed to open %s: %s\n", + path, dlerror()); + } + /* not need continue to loop all paths once the driver is found */ + if (driver != NULL) + break; + } + + if (driver == NULL) { + log_(_LOADER_WARNING, "MESA-LOADER: failed to open %s (search paths %s)\n", + driver_name, search_paths); + *out_driver_handle = NULL; + return NULL; + } + + log_(_LOADER_DEBUG, "MESA-LOADER: dlopen(%s)\n", path); + + get_extensions_name = loader_get_extensions_name(driver_name); + if (get_extensions_name) { + get_extensions = dlsym(driver, get_extensions_name); + if (get_extensions) { + extensions = get_extensions(); + } else { + log_(_LOADER_DEBUG, "MESA-LOADER: driver does not expose %s(): %s\n", + get_extensions_name, dlerror()); + } + free(get_extensions_name); + } + + if (!extensions) + extensions = dlsym(driver, __DRI_DRIVER_EXTENSIONS); + if (extensions == NULL) { + log_(_LOADER_WARNING, + "MESA-LOADER: driver exports no extensions (%s)\n", dlerror()); + dlclose(driver); + } + + *out_driver_handle = driver; + return extensions; +}