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