loader: simplify loader_get_user_preferred_fd()
[mesa.git] / src / loader / loader.c
1 /*
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
5 *
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:
12 *
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
15 * Software.
16 *
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
23 * SOFTWARE.
24 *
25 * Authors:
26 * Rob Clark <robclark@freedesktop.org>
27 */
28
29 #include <dlfcn.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <sys/stat.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <stdbool.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <stdlib.h>
39 #include <limits.h>
40 #include <sys/param.h>
41 #ifdef MAJOR_IN_MKDEV
42 #include <sys/mkdev.h>
43 #endif
44 #ifdef MAJOR_IN_SYSMACROS
45 #include <sys/sysmacros.h>
46 #endif
47 #include <GL/gl.h>
48 #include <GL/internal/dri_interface.h>
49 #include "loader.h"
50
51 #ifdef HAVE_LIBDRM
52 #include <xf86drm.h>
53 #define MAX_DRM_DEVICES 64
54 #ifdef USE_DRICONF
55 #include "util/xmlconfig.h"
56 #include "util/xmlpool.h"
57 #endif
58 #endif
59
60 #include "util/macros.h"
61
62 #define __IS_LOADER
63 #include "pci_id_driver_map.h"
64
65 /* For systems like Hurd */
66 #ifndef PATH_MAX
67 #define PATH_MAX 4096
68 #endif
69
70 static void default_logger(int level, const char *fmt, ...)
71 {
72 if (level <= _LOADER_WARNING) {
73 va_list args;
74 va_start(args, fmt);
75 vfprintf(stderr, fmt, args);
76 va_end(args);
77 }
78 }
79
80 static loader_logger *log_ = default_logger;
81
82 int
83 loader_open_device(const char *device_name)
84 {
85 int fd;
86 #ifdef O_CLOEXEC
87 fd = open(device_name, O_RDWR | O_CLOEXEC);
88 if (fd == -1 && errno == EINVAL)
89 #endif
90 {
91 fd = open(device_name, O_RDWR);
92 if (fd != -1)
93 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
94 }
95 if (fd == -1 && errno == EACCES) {
96 log_(_LOADER_WARNING, "failed to open %s: %s\n",
97 device_name, strerror(errno));
98 }
99 return fd;
100 }
101
102 static char *loader_get_kernel_driver_name(int fd)
103 {
104 #if HAVE_LIBDRM
105 char *driver;
106 drmVersionPtr version = drmGetVersion(fd);
107
108 if (!version) {
109 log_(_LOADER_WARNING, "failed to get driver name for fd %d\n", fd);
110 return NULL;
111 }
112
113 driver = strndup(version->name, version->name_len);
114
115 drmFreeVersion(version);
116 return driver;
117 #else
118 return NULL;
119 #endif
120 }
121
122 bool
123 is_kernel_i915(int fd)
124 {
125 char *kernel_driver = loader_get_kernel_driver_name(fd);
126 bool is_i915 = kernel_driver && strcmp(kernel_driver, "i915") == 0;
127
128 free(kernel_driver);
129 return is_i915;
130 }
131
132 #if defined(HAVE_LIBDRM)
133 int
134 loader_open_render_node(const char *name)
135 {
136 drmDevicePtr devices[MAX_DRM_DEVICES], device;
137 int i, num_devices, fd = -1;
138
139 num_devices = drmGetDevices2(0, devices, MAX_DRM_DEVICES);
140 if (num_devices <= 0)
141 return -ENOENT;
142
143 for (i = 0; i < num_devices; i++) {
144 device = devices[i];
145
146 if ((device->available_nodes & (1 << DRM_NODE_RENDER)) &&
147 (device->bustype == DRM_BUS_PLATFORM)) {
148 drmVersionPtr version;
149
150 fd = loader_open_device(device->nodes[DRM_NODE_RENDER]);
151 if (fd < 0)
152 continue;
153
154 version = drmGetVersion(fd);
155 if (!version) {
156 close(fd);
157 continue;
158 }
159
160 if (strcmp(version->name, name) != 0) {
161 drmFreeVersion(version);
162 close(fd);
163 continue;
164 }
165
166 drmFreeVersion(version);
167 break;
168 }
169 }
170 drmFreeDevices(devices, num_devices);
171
172 if (i == num_devices)
173 return -ENOENT;
174
175 return fd;
176 }
177
178 #ifdef USE_DRICONF
179 static const char __driConfigOptionsLoader[] =
180 DRI_CONF_BEGIN
181 DRI_CONF_SECTION_INITIALIZATION
182 DRI_CONF_DEVICE_ID_PATH_TAG()
183 DRI_CONF_DRI_DRIVER()
184 DRI_CONF_SECTION_END
185 DRI_CONF_END;
186
187 static char *loader_get_dri_config_driver(int fd)
188 {
189 driOptionCache defaultInitOptions;
190 driOptionCache userInitOptions;
191 char *dri_driver = NULL;
192 char *kernel_driver = loader_get_kernel_driver_name(fd);
193
194 driParseOptionInfo(&defaultInitOptions, __driConfigOptionsLoader);
195 driParseConfigFiles(&userInitOptions, &defaultInitOptions, 0,
196 "loader", kernel_driver, NULL, 0);
197 if (driCheckOption(&userInitOptions, "dri_driver", DRI_STRING)) {
198 char *opt = driQueryOptionstr(&userInitOptions, "dri_driver");
199 /* not an empty string */
200 if (*opt)
201 dri_driver = strdup(opt);
202 }
203 driDestroyOptionCache(&userInitOptions);
204 driDestroyOptionInfo(&defaultInitOptions);
205
206 free(kernel_driver);
207 return dri_driver;
208 }
209
210 static char *loader_get_dri_config_device_id(void)
211 {
212 driOptionCache defaultInitOptions;
213 driOptionCache userInitOptions;
214 char *prime = NULL;
215
216 driParseOptionInfo(&defaultInitOptions, __driConfigOptionsLoader);
217 driParseConfigFiles(&userInitOptions, &defaultInitOptions, 0,
218 "loader", NULL, NULL, 0);
219 if (driCheckOption(&userInitOptions, "device_id", DRI_STRING))
220 prime = strdup(driQueryOptionstr(&userInitOptions, "device_id"));
221 driDestroyOptionCache(&userInitOptions);
222 driDestroyOptionInfo(&defaultInitOptions);
223
224 return prime;
225 }
226 #endif
227
228 static char *drm_construct_id_path_tag(drmDevicePtr device)
229 {
230 char *tag = NULL;
231
232 if (device->bustype == DRM_BUS_PCI) {
233 if (asprintf(&tag, "pci-%04x_%02x_%02x_%1u",
234 device->businfo.pci->domain,
235 device->businfo.pci->bus,
236 device->businfo.pci->dev,
237 device->businfo.pci->func) < 0) {
238 return NULL;
239 }
240 } else if (device->bustype == DRM_BUS_PLATFORM ||
241 device->bustype == DRM_BUS_HOST1X) {
242 char *fullname, *name, *address;
243
244 if (device->bustype == DRM_BUS_PLATFORM)
245 fullname = device->businfo.platform->fullname;
246 else
247 fullname = device->businfo.host1x->fullname;
248
249 name = strrchr(fullname, '/');
250 if (!name)
251 name = strdup(fullname);
252 else
253 name = strdup(name + 1);
254
255 address = strchr(name, '@');
256 if (address) {
257 *address++ = '\0';
258
259 if (asprintf(&tag, "platform-%s_%s", address, name) < 0)
260 tag = NULL;
261 } else {
262 if (asprintf(&tag, "platform-%s", name) < 0)
263 tag = NULL;
264 }
265
266 free(name);
267 }
268 return tag;
269 }
270
271 static bool drm_device_matches_tag(drmDevicePtr device, const char *prime_tag)
272 {
273 char *tag = drm_construct_id_path_tag(device);
274 int ret;
275
276 if (tag == NULL)
277 return false;
278
279 ret = strcmp(tag, prime_tag);
280
281 free(tag);
282 return ret == 0;
283 }
284
285 static char *drm_get_id_path_tag_for_fd(int fd)
286 {
287 drmDevicePtr device;
288 char *tag;
289
290 if (drmGetDevice2(fd, 0, &device) != 0)
291 return NULL;
292
293 tag = drm_construct_id_path_tag(device);
294 drmFreeDevice(&device);
295 return tag;
296 }
297
298 int loader_get_user_preferred_fd(int default_fd, bool *different_device)
299 {
300 const char *dri_prime = getenv("DRI_PRIME");
301 char *default_tag, *prime = NULL;
302 drmDevicePtr devices[MAX_DRM_DEVICES];
303 int i, num_devices, fd = -1;
304
305 if (dri_prime)
306 prime = strdup(dri_prime);
307 #ifdef USE_DRICONF
308 else
309 prime = loader_get_dri_config_device_id();
310 #endif
311
312 if (prime == NULL) {
313 *different_device = false;
314 return default_fd;
315 }
316
317 default_tag = drm_get_id_path_tag_for_fd(default_fd);
318 if (default_tag == NULL)
319 goto err;
320
321 num_devices = drmGetDevices2(0, devices, MAX_DRM_DEVICES);
322 if (num_devices <= 0)
323 goto err;
324
325 for (i = 0; i < num_devices; i++) {
326 if (!(devices[i]->available_nodes & 1 << DRM_NODE_RENDER))
327 continue;
328
329 /* two formats of DRI_PRIME are supported:
330 * "1": choose any other card than the card used by default.
331 * id_path_tag: (for example "pci-0000_02_00_0") choose the card
332 * with this id_path_tag.
333 */
334 if (!strcmp(prime,"1")) {
335 if (drm_device_matches_tag(devices[i], default_tag))
336 continue;
337 } else {
338 if (!drm_device_matches_tag(devices[i], prime))
339 continue;
340 }
341
342 fd = loader_open_device(devices[i]->nodes[DRM_NODE_RENDER]);
343 break;
344 }
345 drmFreeDevices(devices, num_devices);
346
347 if (i == num_devices)
348 goto err;
349
350 if (fd < 0)
351 goto err;
352
353 close(default_fd);
354
355 *different_device = !!strcmp(default_tag, prime);
356
357 free(default_tag);
358 free(prime);
359 return fd;
360
361 err:
362 *different_device = false;
363
364 free(default_tag);
365 free(prime);
366 return default_fd;
367 }
368 #else
369 int
370 loader_open_render_node(const char *name)
371 {
372 return -1;
373 }
374
375 int loader_get_user_preferred_fd(int default_fd, bool *different_device)
376 {
377 *different_device = false;
378 return default_fd;
379 }
380 #endif
381
382 #if defined(HAVE_LIBDRM)
383
384 static bool
385 drm_get_pci_id_for_fd(int fd, int *vendor_id, int *chip_id)
386 {
387 drmDevicePtr device;
388 bool ret;
389
390 if (drmGetDevice2(fd, 0, &device) == 0) {
391 if (device->bustype == DRM_BUS_PCI) {
392 *vendor_id = device->deviceinfo.pci->vendor_id;
393 *chip_id = device->deviceinfo.pci->device_id;
394 ret = true;
395 }
396 else {
397 log_(_LOADER_DEBUG, "MESA-LOADER: device is not located on the PCI bus\n");
398 ret = false;
399 }
400 drmFreeDevice(&device);
401 }
402 else {
403 log_(_LOADER_WARNING, "MESA-LOADER: failed to retrieve device information\n");
404 ret = false;
405 }
406
407 return ret;
408 }
409 #endif
410
411
412 bool
413 loader_get_pci_id_for_fd(int fd, int *vendor_id, int *chip_id)
414 {
415 #if HAVE_LIBDRM
416 return drm_get_pci_id_for_fd(fd, vendor_id, chip_id);
417 #endif
418 return false;
419 }
420
421 char *
422 loader_get_device_name_for_fd(int fd)
423 {
424 char *result = NULL;
425
426 #if HAVE_LIBDRM
427 result = drmGetDeviceNameFromFd2(fd);
428 #endif
429
430 return result;
431 }
432
433 char *
434 loader_get_driver_for_fd(int fd)
435 {
436 int vendor_id, chip_id, i, j;
437 char *driver = NULL;
438
439 /* Allow an environment variable to force choosing a different driver
440 * binary. If that driver binary can't survive on this FD, that's the
441 * user's problem, but this allows vc4 simulator to run on an i965 host,
442 * and may be useful for some touch testing of i915 on an i965 host.
443 */
444 if (geteuid() == getuid()) {
445 driver = getenv("MESA_LOADER_DRIVER_OVERRIDE");
446 if (driver)
447 return strdup(driver);
448 }
449
450 #if defined(HAVE_LIBDRM) && defined(USE_DRICONF)
451 driver = loader_get_dri_config_driver(fd);
452 if (driver)
453 return driver;
454 #endif
455
456 if (!loader_get_pci_id_for_fd(fd, &vendor_id, &chip_id)) {
457 driver = loader_get_kernel_driver_name(fd);
458 if (driver)
459 log_(_LOADER_INFO, "using driver %s for %d\n", driver, fd);
460 return driver;
461 }
462
463 for (i = 0; i < ARRAY_SIZE(driver_map); i++) {
464 if (vendor_id != driver_map[i].vendor_id)
465 continue;
466
467 if (driver_map[i].predicate && !driver_map[i].predicate(fd))
468 continue;
469
470 if (driver_map[i].num_chips_ids == -1) {
471 driver = strdup(driver_map[i].driver);
472 goto out;
473 }
474
475 for (j = 0; j < driver_map[i].num_chips_ids; j++)
476 if (driver_map[i].chip_ids[j] == chip_id) {
477 driver = strdup(driver_map[i].driver);
478 goto out;
479 }
480 }
481
482 out:
483 log_(driver ? _LOADER_DEBUG : _LOADER_WARNING,
484 "pci id for fd %d: %04x:%04x, driver %s\n",
485 fd, vendor_id, chip_id, driver);
486 return driver;
487 }
488
489 void
490 loader_set_logger(loader_logger *logger)
491 {
492 log_ = logger;
493 }
494
495 char *
496 loader_get_extensions_name(const char *driver_name)
497 {
498 char *name = NULL;
499
500 if (asprintf(&name, "%s_%s", __DRI_DRIVER_GET_EXTENSIONS, driver_name) < 0)
501 return NULL;
502
503 const size_t len = strlen(name);
504 for (size_t i = 0; i < len; i++) {
505 if (name[i] == '-')
506 name[i] = '_';
507 }
508
509 return name;
510 }
511
512 /**
513 * Opens a DRI driver using its driver name, returning the __DRIextension
514 * entrypoints.
515 *
516 * \param driverName - a name like "i965", "radeon", "nouveau", etc.
517 * \param out_driver - Address where the dlopen() return value will be stored.
518 * \param search_path_vars - NULL-terminated list of env vars that can be used
519 * to override the DEFAULT_DRIVER_DIR search path.
520 */
521 const struct __DRIextensionRec **
522 loader_open_driver(const char *driver_name,
523 void **out_driver_handle,
524 const char **search_path_vars)
525 {
526 char path[PATH_MAX], *search_paths, *next, *end;
527 char *get_extensions_name;
528 const struct __DRIextensionRec **extensions = NULL;
529 const struct __DRIextensionRec **(*get_extensions)(void);
530
531 search_paths = NULL;
532 if (geteuid() == getuid() && search_path_vars) {
533 for (int i = 0; search_path_vars[i] != NULL; i++) {
534 search_paths = getenv(search_path_vars[i]);
535 if (search_paths)
536 break;
537 }
538 }
539 if (search_paths == NULL)
540 search_paths = DEFAULT_DRIVER_DIR;
541
542 void *driver = NULL;
543 end = search_paths + strlen(search_paths);
544 for (char *p = search_paths; p < end; p = next + 1) {
545 int len;
546 next = strchr(p, ':');
547 if (next == NULL)
548 next = end;
549
550 len = next - p;
551 #if USE_ELF_TLS
552 snprintf(path, sizeof(path), "%.*s/tls/%s_dri.so", len, p, driver_name);
553 driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
554 #endif
555 if (driver == NULL) {
556 snprintf(path, sizeof(path), "%.*s/%s_dri.so", len, p, driver_name);
557 driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
558 if (driver == NULL)
559 log_(_LOADER_DEBUG, "MESA-LOADER: failed to open %s: %s\n",
560 path, dlerror());
561 }
562 /* not need continue to loop all paths once the driver is found */
563 if (driver != NULL)
564 break;
565 }
566
567 if (driver == NULL) {
568 log_(_LOADER_WARNING, "MESA-LOADER: failed to open %s (search paths %s)\n",
569 driver_name, search_paths);
570 *out_driver_handle = NULL;
571 return NULL;
572 }
573
574 log_(_LOADER_DEBUG, "MESA-LOADER: dlopen(%s)\n", path);
575
576 get_extensions_name = loader_get_extensions_name(driver_name);
577 if (get_extensions_name) {
578 get_extensions = dlsym(driver, get_extensions_name);
579 if (get_extensions) {
580 extensions = get_extensions();
581 } else {
582 log_(_LOADER_DEBUG, "MESA-LOADER: driver does not expose %s(): %s\n",
583 get_extensions_name, dlerror());
584 }
585 free(get_extensions_name);
586 }
587
588 if (!extensions)
589 extensions = dlsym(driver, __DRI_DRIVER_EXTENSIONS);
590 if (extensions == NULL) {
591 log_(_LOADER_WARNING,
592 "MESA-LOADER: driver exports no extensions (%s)\n", dlerror());
593 dlclose(driver);
594 }
595
596 *out_driver_handle = driver;
597 return extensions;
598 }