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>
39 #include <sys/param.h>
41 #include <sys/mkdev.h>
43 #ifdef MAJOR_IN_SYSMACROS
44 #include <sys/sysmacros.h>
47 #include <GL/internal/dri_interface.h>
53 #include "util/xmlconfig.h"
54 #include "util/xmlpool.h"
59 #include "pci_id_driver_map.h"
61 static void default_logger(int level
, const char *fmt
, ...)
63 if (level
<= _LOADER_WARNING
) {
66 vfprintf(stderr
, fmt
, args
);
71 static loader_logger
*log_
= default_logger
;
74 loader_open_device(const char *device_name
)
78 fd
= open(device_name
, O_RDWR
| O_CLOEXEC
);
79 if (fd
== -1 && errno
== EINVAL
)
82 fd
= open(device_name
, O_RDWR
);
84 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
89 static char *loader_get_kernel_driver_name(int fd
)
93 drmVersionPtr version
= drmGetVersion(fd
);
96 log_(_LOADER_WARNING
, "failed to get driver name for fd %d\n", fd
);
100 driver
= strndup(version
->name
, version
->name_len
);
102 drmFreeVersion(version
);
109 #if defined(HAVE_LIBDRM)
111 loader_open_render_node(const char *name
)
113 drmDevicePtr
*devices
, device
;
114 int err
, render
= -ENOENT
, fd
;
117 err
= drmGetDevices2(0, NULL
, 0);
123 devices
= calloc(num
, sizeof(*devices
));
127 err
= drmGetDevices2(0, devices
, num
);
133 for (i
= 0; i
< num
; i
++) {
136 if ((device
->available_nodes
& (1 << DRM_NODE_RENDER
)) &&
137 (device
->bustype
== DRM_BUS_PLATFORM
)) {
138 drmVersionPtr version
;
140 fd
= loader_open_device(device
->nodes
[DRM_NODE_RENDER
]);
144 version
= drmGetVersion(fd
);
150 if (strcmp(version
->name
, name
) != 0) {
151 drmFreeVersion(version
);
156 drmFreeVersion(version
);
162 drmFreeDevices(devices
, num
);
170 static const char __driConfigOptionsLoader
[] =
172 DRI_CONF_SECTION_INITIALIZATION
173 DRI_CONF_DEVICE_ID_PATH_TAG()
174 DRI_CONF_DRI_DRIVER()
178 static char *loader_get_dri_config_driver(int fd
)
180 driOptionCache defaultInitOptions
;
181 driOptionCache userInitOptions
;
182 char *dri_driver
= NULL
;
183 char *kernel_driver
= loader_get_kernel_driver_name(fd
);
185 driParseOptionInfo(&defaultInitOptions
, __driConfigOptionsLoader
);
186 driParseConfigFiles(&userInitOptions
, &defaultInitOptions
, 0,
187 "loader", kernel_driver
);
188 if (driCheckOption(&userInitOptions
, "dri_driver", DRI_STRING
)) {
189 char *opt
= driQueryOptionstr(&userInitOptions
, "dri_driver");
190 /* not an empty string */
192 dri_driver
= strdup(opt
);
194 driDestroyOptionCache(&userInitOptions
);
195 driDestroyOptionInfo(&defaultInitOptions
);
201 static char *loader_get_dri_config_device_id(void)
203 driOptionCache defaultInitOptions
;
204 driOptionCache userInitOptions
;
207 driParseOptionInfo(&defaultInitOptions
, __driConfigOptionsLoader
);
208 driParseConfigFiles(&userInitOptions
, &defaultInitOptions
, 0, "loader", NULL
);
209 if (driCheckOption(&userInitOptions
, "device_id", DRI_STRING
))
210 prime
= strdup(driQueryOptionstr(&userInitOptions
, "device_id"));
211 driDestroyOptionCache(&userInitOptions
);
212 driDestroyOptionInfo(&defaultInitOptions
);
218 static char *drm_construct_id_path_tag(drmDevicePtr device
)
222 if (device
->bustype
== DRM_BUS_PCI
) {
223 if (asprintf(&tag
, "pci-%04x_%02x_%02x_%1u",
224 device
->businfo
.pci
->domain
,
225 device
->businfo
.pci
->bus
,
226 device
->businfo
.pci
->dev
,
227 device
->businfo
.pci
->func
) < 0) {
230 } else if (device
->bustype
== DRM_BUS_PLATFORM
||
231 device
->bustype
== DRM_BUS_HOST1X
) {
232 char *fullname
, *name
, *address
;
234 if (device
->bustype
== DRM_BUS_PLATFORM
)
235 fullname
= device
->businfo
.platform
->fullname
;
237 fullname
= device
->businfo
.host1x
->fullname
;
239 name
= strrchr(fullname
, '/');
241 name
= strdup(fullname
);
243 name
= strdup(name
+ 1);
245 address
= strchr(name
, '@');
249 if (asprintf(&tag
, "platform-%s_%s", address
, name
) < 0)
252 if (asprintf(&tag
, "platform-%s", name
) < 0)
261 static bool drm_device_matches_tag(drmDevicePtr device
, const char *prime_tag
)
263 char *tag
= drm_construct_id_path_tag(device
);
269 ret
= strcmp(tag
, prime_tag
);
275 static char *drm_get_id_path_tag_for_fd(int fd
)
280 if (drmGetDevice2(fd
, 0, &device
) != 0)
283 tag
= drm_construct_id_path_tag(device
);
284 drmFreeDevice(&device
);
288 int loader_get_user_preferred_fd(int default_fd
, bool *different_device
)
290 /* Arbitrary "maximum" value of drm devices. */
291 #define MAX_DRM_DEVICES 32
292 const char *dri_prime
= getenv("DRI_PRIME");
293 char *default_tag
, *prime
= NULL
;
294 drmDevicePtr devices
[MAX_DRM_DEVICES
];
295 int i
, num_devices
, fd
;
299 prime
= strdup(dri_prime
);
302 prime
= loader_get_dri_config_device_id();
306 *different_device
= false;
310 default_tag
= drm_get_id_path_tag_for_fd(default_fd
);
311 if (default_tag
== NULL
)
314 num_devices
= drmGetDevices2(0, devices
, MAX_DRM_DEVICES
);
318 /* two format are supported:
319 * "1": choose any other card than the card used by default.
320 * id_path_tag: (for example "pci-0000_02_00_0") choose the card
321 * with this id_path_tag.
323 if (!strcmp(prime
,"1")) {
324 /* Hmm... detection for 2-7 seems to be broken. Oh well ...
325 * Pick the first render device that is not our own.
327 for (i
= 0; i
< num_devices
; i
++) {
328 if (devices
[i
]->available_nodes
& 1 << DRM_NODE_RENDER
&&
329 !drm_device_matches_tag(devices
[i
], default_tag
)) {
336 for (i
= 0; i
< num_devices
; i
++) {
337 if (devices
[i
]->available_nodes
& 1 << DRM_NODE_RENDER
&&
338 drm_device_matches_tag(devices
[i
], prime
)) {
347 drmFreeDevices(devices
, num_devices
);
351 fd
= loader_open_device(devices
[i
]->nodes
[DRM_NODE_RENDER
]);
352 drmFreeDevices(devices
, num_devices
);
358 *different_device
= !!strcmp(default_tag
, prime
);
365 *different_device
= false;
373 loader_open_render_node(const char *name
)
378 int loader_get_user_preferred_fd(int default_fd
, bool *different_device
)
380 *different_device
= false;
385 #if defined(HAVE_LIBDRM)
388 drm_get_pci_id_for_fd(int fd
, int *vendor_id
, int *chip_id
)
393 if (drmGetDevice2(fd
, 0, &device
) == 0) {
394 if (device
->bustype
== DRM_BUS_PCI
) {
395 *vendor_id
= device
->deviceinfo
.pci
->vendor_id
;
396 *chip_id
= device
->deviceinfo
.pci
->device_id
;
400 log_(_LOADER_DEBUG
, "MESA-LOADER: device is not located on the PCI bus\n");
403 drmFreeDevice(&device
);
406 log_(_LOADER_WARNING
, "MESA-LOADER: failed to retrieve device information\n");
416 loader_get_pci_id_for_fd(int fd
, int *vendor_id
, int *chip_id
)
419 if (drm_get_pci_id_for_fd(fd
, vendor_id
, chip_id
))
426 loader_get_device_name_for_fd(int fd
)
431 result
= drmGetDeviceNameFromFd2(fd
);
438 loader_get_driver_for_fd(int fd
)
440 int vendor_id
, chip_id
, i
, j
;
443 /* Allow an environment variable to force choosing a different driver
444 * binary. If that driver binary can't survive on this FD, that's the
445 * user's problem, but this allows vc4 simulator to run on an i965 host,
446 * and may be useful for some touch testing of i915 on an i965 host.
448 if (geteuid() == getuid()) {
449 driver
= getenv("MESA_LOADER_DRIVER_OVERRIDE");
451 return strdup(driver
);
454 #if defined(HAVE_LIBDRM) && defined(USE_DRICONF)
455 driver
= loader_get_dri_config_driver(fd
);
460 if (!loader_get_pci_id_for_fd(fd
, &vendor_id
, &chip_id
)) {
461 driver
= loader_get_kernel_driver_name(fd
);
463 log_(_LOADER_INFO
, "using driver %s for %d\n", driver
, fd
);
467 for (i
= 0; driver_map
[i
].driver
; i
++) {
468 if (vendor_id
!= driver_map
[i
].vendor_id
)
471 if (driver_map
[i
].predicate
&& !driver_map
[i
].predicate(fd
))
474 if (driver_map
[i
].num_chips_ids
== -1) {
475 driver
= strdup(driver_map
[i
].driver
);
479 for (j
= 0; j
< driver_map
[i
].num_chips_ids
; j
++)
480 if (driver_map
[i
].chip_ids
[j
] == chip_id
) {
481 driver
= strdup(driver_map
[i
].driver
);
487 log_(driver
? _LOADER_DEBUG
: _LOADER_WARNING
,
488 "pci id for fd %d: %04x:%04x, driver %s\n",
489 fd
, vendor_id
, chip_id
, driver
);
494 loader_set_logger(loader_logger
*logger
)
500 loader_get_extensions_name(const char *driver_name
)
504 if (asprintf(&name
, "%s_%s", __DRI_DRIVER_GET_EXTENSIONS
, driver_name
) < 0)
507 const size_t len
= strlen(name
);
508 for (size_t i
= 0; i
< len
; i
++) {
517 * Opens a DRI driver using its driver name, returning the __DRIextension
520 * \param driverName - a name like "i965", "radeon", "nouveau", etc.
521 * \param out_driver - Address where the dlopen() return value will be stored.
522 * \param search_path_vars - NULL-terminated list of env vars that can be used
523 * to override the DEFAULT_DRIVER_DIR search path.
525 const struct __DRIextensionRec
**
526 loader_open_driver(const char *driver_name
,
527 void **out_driver_handle
,
528 const char **search_path_vars
)
530 char path
[PATH_MAX
], *search_paths
, *next
, *end
;
531 char *get_extensions_name
;
532 const struct __DRIextensionRec
**extensions
= NULL
;
533 const struct __DRIextensionRec
**(*get_extensions
)(void);
536 if (geteuid() == getuid() && search_path_vars
) {
537 for (int i
= 0; search_path_vars
[i
] != NULL
; i
++) {
538 search_paths
= getenv(search_path_vars
[i
]);
543 if (search_paths
== NULL
)
544 search_paths
= DEFAULT_DRIVER_DIR
;
547 end
= search_paths
+ strlen(search_paths
);
548 for (char *p
= search_paths
; p
< end
; p
= next
+ 1) {
550 next
= strchr(p
, ':');
556 snprintf(path
, sizeof(path
), "%.*s/tls/%s_dri.so", len
, p
, driver_name
);
557 driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
559 if (driver
== NULL
) {
560 snprintf(path
, sizeof(path
), "%.*s/%s_dri.so", len
, p
, driver_name
);
561 driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
563 log_(_LOADER_DEBUG
, "MESA-LOADER: failed to open %s: %s\n",
566 /* not need continue to loop all paths once the driver is found */
571 if (driver
== NULL
) {
572 log_(_LOADER_WARNING
, "MESA-LOADER: failed to open %s (search paths %s)\n",
573 driver_name
, search_paths
);
574 *out_driver_handle
= NULL
;
578 log_(_LOADER_DEBUG
, "MESA-LOADER: dlopen(%s)\n", path
);
580 get_extensions_name
= loader_get_extensions_name(driver_name
);
581 if (get_extensions_name
) {
582 get_extensions
= dlsym(driver
, get_extensions_name
);
583 if (get_extensions
) {
584 extensions
= get_extensions();
586 log_(_LOADER_DEBUG
, "MESA-LOADER: driver does not expose %s(): %s\n",
587 get_extensions_name
, dlerror());
589 free(get_extensions_name
);
593 extensions
= dlsym(driver
, __DRI_DRIVER_EXTENSIONS
);
594 if (extensions
== NULL
) {
595 log_(_LOADER_WARNING
,
596 "MESA-LOADER: driver exports no extensions (%s)\n", dlerror());
600 *out_driver_handle
= driver
;