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>
54 #include "util/xmlconfig.h"
55 #include "util/xmlpool.h"
60 #include "pci_id_driver_map.h"
62 static void default_logger(int level
, const char *fmt
, ...)
64 if (level
<= _LOADER_WARNING
) {
67 vfprintf(stderr
, fmt
, args
);
72 static loader_logger
*log_
= default_logger
;
75 loader_open_device(const char *device_name
)
79 fd
= open(device_name
, O_RDWR
| O_CLOEXEC
);
80 if (fd
== -1 && errno
== EINVAL
)
83 fd
= open(device_name
, O_RDWR
);
85 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
90 static char *loader_get_kernel_driver_name(int fd
)
94 drmVersionPtr version
= drmGetVersion(fd
);
97 log_(_LOADER_WARNING
, "failed to get driver name for fd %d\n", fd
);
101 driver
= strndup(version
->name
, version
->name_len
);
103 drmFreeVersion(version
);
110 #if defined(HAVE_LIBDRM)
112 loader_open_render_node(const char *name
)
114 drmDevicePtr
*devices
, device
;
115 int err
, render
= -ENOENT
, fd
;
118 err
= drmGetDevices2(0, NULL
, 0);
124 devices
= calloc(num
, sizeof(*devices
));
128 err
= drmGetDevices2(0, devices
, num
);
134 for (i
= 0; i
< num
; i
++) {
137 if ((device
->available_nodes
& (1 << DRM_NODE_RENDER
)) &&
138 (device
->bustype
== DRM_BUS_PLATFORM
)) {
139 drmVersionPtr version
;
141 fd
= loader_open_device(device
->nodes
[DRM_NODE_RENDER
]);
145 version
= drmGetVersion(fd
);
151 if (strcmp(version
->name
, name
) != 0) {
152 drmFreeVersion(version
);
157 drmFreeVersion(version
);
163 drmFreeDevices(devices
, num
);
171 static const char __driConfigOptionsLoader
[] =
173 DRI_CONF_SECTION_INITIALIZATION
174 DRI_CONF_DEVICE_ID_PATH_TAG()
175 DRI_CONF_DRI_DRIVER()
179 static char *loader_get_dri_config_driver(int fd
)
181 driOptionCache defaultInitOptions
;
182 driOptionCache userInitOptions
;
183 char *dri_driver
= NULL
;
184 char *kernel_driver
= loader_get_kernel_driver_name(fd
);
186 driParseOptionInfo(&defaultInitOptions
, __driConfigOptionsLoader
);
187 driParseConfigFiles(&userInitOptions
, &defaultInitOptions
, 0,
188 "loader", kernel_driver
);
189 if (driCheckOption(&userInitOptions
, "dri_driver", DRI_STRING
)) {
190 char *opt
= driQueryOptionstr(&userInitOptions
, "dri_driver");
191 /* not an empty string */
193 dri_driver
= strdup(opt
);
195 driDestroyOptionCache(&userInitOptions
);
196 driDestroyOptionInfo(&defaultInitOptions
);
202 static char *loader_get_dri_config_device_id(void)
204 driOptionCache defaultInitOptions
;
205 driOptionCache userInitOptions
;
208 driParseOptionInfo(&defaultInitOptions
, __driConfigOptionsLoader
);
209 driParseConfigFiles(&userInitOptions
, &defaultInitOptions
, 0, "loader", NULL
);
210 if (driCheckOption(&userInitOptions
, "device_id", DRI_STRING
))
211 prime
= strdup(driQueryOptionstr(&userInitOptions
, "device_id"));
212 driDestroyOptionCache(&userInitOptions
);
213 driDestroyOptionInfo(&defaultInitOptions
);
219 static char *drm_construct_id_path_tag(drmDevicePtr device
)
223 if (device
->bustype
== DRM_BUS_PCI
) {
224 if (asprintf(&tag
, "pci-%04x_%02x_%02x_%1u",
225 device
->businfo
.pci
->domain
,
226 device
->businfo
.pci
->bus
,
227 device
->businfo
.pci
->dev
,
228 device
->businfo
.pci
->func
) < 0) {
231 } else if (device
->bustype
== DRM_BUS_PLATFORM
||
232 device
->bustype
== DRM_BUS_HOST1X
) {
233 char *fullname
, *name
, *address
;
235 if (device
->bustype
== DRM_BUS_PLATFORM
)
236 fullname
= device
->businfo
.platform
->fullname
;
238 fullname
= device
->businfo
.host1x
->fullname
;
240 name
= strrchr(fullname
, '/');
242 name
= strdup(fullname
);
244 name
= strdup(name
+ 1);
246 address
= strchr(name
, '@');
250 if (asprintf(&tag
, "platform-%s_%s", address
, name
) < 0)
253 if (asprintf(&tag
, "platform-%s", name
) < 0)
262 static bool drm_device_matches_tag(drmDevicePtr device
, const char *prime_tag
)
264 char *tag
= drm_construct_id_path_tag(device
);
270 ret
= strcmp(tag
, prime_tag
);
276 static char *drm_get_id_path_tag_for_fd(int fd
)
281 if (drmGetDevice2(fd
, 0, &device
) != 0)
284 tag
= drm_construct_id_path_tag(device
);
285 drmFreeDevice(&device
);
289 int loader_get_user_preferred_fd(int default_fd
, bool *different_device
)
291 /* Arbitrary "maximum" value of drm devices. */
292 #define MAX_DRM_DEVICES 32
293 const char *dri_prime
= getenv("DRI_PRIME");
294 char *default_tag
, *prime
= NULL
;
295 drmDevicePtr devices
[MAX_DRM_DEVICES
];
296 int i
, num_devices
, fd
;
300 prime
= strdup(dri_prime
);
303 prime
= loader_get_dri_config_device_id();
307 *different_device
= false;
311 default_tag
= drm_get_id_path_tag_for_fd(default_fd
);
312 if (default_tag
== NULL
)
315 num_devices
= drmGetDevices2(0, devices
, MAX_DRM_DEVICES
);
319 /* two format are supported:
320 * "1": choose any other card than the card used by default.
321 * id_path_tag: (for example "pci-0000_02_00_0") choose the card
322 * with this id_path_tag.
324 if (!strcmp(prime
,"1")) {
325 /* Hmm... detection for 2-7 seems to be broken. Oh well ...
326 * Pick the first render device that is not our own.
328 for (i
= 0; i
< num_devices
; i
++) {
329 if (devices
[i
]->available_nodes
& 1 << DRM_NODE_RENDER
&&
330 !drm_device_matches_tag(devices
[i
], default_tag
)) {
337 for (i
= 0; i
< num_devices
; i
++) {
338 if (devices
[i
]->available_nodes
& 1 << DRM_NODE_RENDER
&&
339 drm_device_matches_tag(devices
[i
], prime
)) {
348 drmFreeDevices(devices
, num_devices
);
352 fd
= loader_open_device(devices
[i
]->nodes
[DRM_NODE_RENDER
]);
353 drmFreeDevices(devices
, num_devices
);
359 *different_device
= !!strcmp(default_tag
, prime
);
366 *different_device
= false;
374 loader_open_render_node(const char *name
)
379 int loader_get_user_preferred_fd(int default_fd
, bool *different_device
)
381 *different_device
= false;
386 #if defined(HAVE_LIBDRM)
389 drm_get_pci_id_for_fd(int fd
, int *vendor_id
, int *chip_id
)
394 if (drmGetDevice2(fd
, 0, &device
) == 0) {
395 if (device
->bustype
== DRM_BUS_PCI
) {
396 *vendor_id
= device
->deviceinfo
.pci
->vendor_id
;
397 *chip_id
= device
->deviceinfo
.pci
->device_id
;
401 log_(_LOADER_DEBUG
, "MESA-LOADER: device is not located on the PCI bus\n");
404 drmFreeDevice(&device
);
407 log_(_LOADER_WARNING
, "MESA-LOADER: failed to retrieve device information\n");
417 loader_get_pci_id_for_fd(int fd
, int *vendor_id
, int *chip_id
)
420 if (drm_get_pci_id_for_fd(fd
, vendor_id
, chip_id
))
427 loader_get_device_name_for_fd(int fd
)
432 result
= drmGetDeviceNameFromFd2(fd
);
439 loader_get_driver_for_fd(int fd
)
441 int vendor_id
, chip_id
, i
, j
;
444 /* Allow an environment variable to force choosing a different driver
445 * binary. If that driver binary can't survive on this FD, that's the
446 * user's problem, but this allows vc4 simulator to run on an i965 host,
447 * and may be useful for some touch testing of i915 on an i965 host.
449 if (geteuid() == getuid()) {
450 driver
= getenv("MESA_LOADER_DRIVER_OVERRIDE");
452 return strdup(driver
);
455 #if defined(HAVE_LIBDRM) && defined(USE_DRICONF)
456 driver
= loader_get_dri_config_driver(fd
);
461 if (!loader_get_pci_id_for_fd(fd
, &vendor_id
, &chip_id
)) {
462 driver
= loader_get_kernel_driver_name(fd
);
464 log_(_LOADER_INFO
, "using driver %s for %d\n", driver
, fd
);
468 for (i
= 0; driver_map
[i
].driver
; i
++) {
469 if (vendor_id
!= driver_map
[i
].vendor_id
)
472 if (driver_map
[i
].predicate
&& !driver_map
[i
].predicate(fd
))
475 if (driver_map
[i
].num_chips_ids
== -1) {
476 driver
= strdup(driver_map
[i
].driver
);
480 for (j
= 0; j
< driver_map
[i
].num_chips_ids
; j
++)
481 if (driver_map
[i
].chip_ids
[j
] == chip_id
) {
482 driver
= strdup(driver_map
[i
].driver
);
488 log_(driver
? _LOADER_DEBUG
: _LOADER_WARNING
,
489 "pci id for fd %d: %04x:%04x, driver %s\n",
490 fd
, vendor_id
, chip_id
, driver
);
495 loader_set_logger(loader_logger
*logger
)
501 loader_get_extensions_name(const char *driver_name
)
505 if (asprintf(&name
, "%s_%s", __DRI_DRIVER_GET_EXTENSIONS
, driver_name
) < 0)
508 const size_t len
= strlen(name
);
509 for (size_t i
= 0; i
< len
; i
++) {
518 * Opens a DRI driver using its driver name, returning the __DRIextension
521 * \param driverName - a name like "i965", "radeon", "nouveau", etc.
522 * \param out_driver - Address where the dlopen() return value will be stored.
523 * \param search_path_vars - NULL-terminated list of env vars that can be used
524 * to override the DEFAULT_DRIVER_DIR search path.
526 const struct __DRIextensionRec
**
527 loader_open_driver(const char *driver_name
,
528 void **out_driver_handle
,
529 const char **search_path_vars
)
531 char path
[PATH_MAX
], *search_paths
, *next
, *end
;
532 char *get_extensions_name
;
533 const struct __DRIextensionRec
**extensions
= NULL
;
534 const struct __DRIextensionRec
**(*get_extensions
)(void);
537 if (geteuid() == getuid() && search_path_vars
) {
538 for (int i
= 0; search_path_vars
[i
] != NULL
; i
++) {
539 search_paths
= getenv(search_path_vars
[i
]);
544 if (search_paths
== NULL
)
545 search_paths
= DEFAULT_DRIVER_DIR
;
548 end
= search_paths
+ strlen(search_paths
);
549 for (char *p
= search_paths
; p
< end
; p
= next
+ 1) {
551 next
= strchr(p
, ':');
557 snprintf(path
, sizeof(path
), "%.*s/tls/%s_dri.so", len
, p
, driver_name
);
558 driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
560 if (driver
== NULL
) {
561 snprintf(path
, sizeof(path
), "%.*s/%s_dri.so", len
, p
, driver_name
);
562 driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
564 log_(_LOADER_DEBUG
, "MESA-LOADER: failed to open %s: %s\n",
567 /* not need continue to loop all paths once the driver is found */
572 if (driver
== NULL
) {
573 log_(_LOADER_WARNING
, "MESA-LOADER: failed to open %s (search paths %s)\n",
574 driver_name
, search_paths
);
575 *out_driver_handle
= NULL
;
579 log_(_LOADER_DEBUG
, "MESA-LOADER: dlopen(%s)\n", path
);
581 get_extensions_name
= loader_get_extensions_name(driver_name
);
582 if (get_extensions_name
) {
583 get_extensions
= dlsym(driver
, get_extensions_name
);
584 if (get_extensions
) {
585 extensions
= get_extensions();
587 log_(_LOADER_DEBUG
, "MESA-LOADER: driver does not expose %s(): %s\n",
588 get_extensions_name
, dlerror());
590 free(get_extensions_name
);
594 extensions
= dlsym(driver
, __DRI_DRIVER_EXTENSIONS
);
595 if (extensions
== NULL
) {
596 log_(_LOADER_WARNING
,
597 "MESA-LOADER: driver exports no extensions (%s)\n", dlerror());
601 *out_driver_handle
= driver
;