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