loader: separate USE_DRICONF code into separate function
[mesa.git] / src / loader / loader.c
1 /*
2 * Copyright (C) 2013 Rob Clark <robclark@freedesktop.org>
3 *
4 * This code is derived from the following files.
5 *
6 * * src/glx/dri3_common.c
7 * Copyright © 2013 Keith Packard
8 *
9 * * src/egl/drivers/dri2/common.c
10 * * src/gbm/backends/dri/driver_name.c
11 * Copyright © 2011 Intel Corporation
12 *
13 * Authors:
14 * Kristian Høgsberg <krh@bitplanet.net>
15 * Benjamin Franzke <benjaminfranzke@googlemail.com>
16 *
17 * * src/gallium/targets/egl-static/egl.c
18 * Copyright (C) 2010-2011 LunarG Inc.
19 *
20 * Authors:
21 * Chia-I Wu <olv@lunarg.com>
22 *
23 * * src/gallium/state_trackers/egl/drm/native_drm.c
24 * Copyright (C) 2010 Chia-I Wu <olv@0xlab.org>
25 *
26 * * src/egl/drivers/dri2/platform_android.c
27 *
28 * Copyright (C) 2010-2011 Chia-I Wu <olvaffe@gmail.com>
29 * Copyright (C) 2010-2011 LunarG Inc.
30 *
31 * Based on platform_x11, which has
32 *
33 * Copyright © 2011 Intel Corporation
34 *
35 * * src/gallium/auxiliary/pipe-loader/pipe_loader_drm.c
36 * Copyright 2011 Intel Corporation
37 * Copyright 2012 Francisco Jerez
38 * All Rights Reserved.
39 *
40 * Authors:
41 * Kristian Høgsberg <krh@bitplanet.net>
42 * Benjamin Franzke <benjaminfranzke@googlemail.com>
43 *
44 * Permission is hereby granted, free of charge, to any person obtaining a
45 * copy of this software and associated documentation files (the "Software"),
46 * to deal in the Software without restriction, including without limitation
47 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
48 * and/or sell copies of the Software, and to permit persons to whom the
49 * Software is furnished to do so, subject to the following conditions:
50 *
51 * The above copyright notice and this permission notice (including the next
52 * paragraph) shall be included in all copies or substantial portions of the
53 * Software.
54 *
55 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
56 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
57 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
58 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
59 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
60 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
61 * SOFTWARE.
62 *
63 * Authors:
64 * Rob Clark <robclark@freedesktop.org>
65 */
66
67 #include <errno.h>
68 #include <fcntl.h>
69 #include <sys/stat.h>
70 #include <stdarg.h>
71 #include <stdio.h>
72 #include <string.h>
73 #ifdef HAVE_LIBUDEV
74 #include <assert.h>
75 #include <dlfcn.h>
76 #include <unistd.h>
77 #include <stdlib.h>
78 #ifdef USE_DRICONF
79 #include "xmlconfig.h"
80 #include "xmlpool.h"
81 #endif
82 #endif
83 #ifdef MAJOR_IN_MKDEV
84 #include <sys/mkdev.h>
85 #endif
86 #ifdef MAJOR_IN_SYSMACROS
87 #include <sys/sysmacros.h>
88 #endif
89 #include "loader.h"
90
91 #ifdef HAVE_LIBDRM
92 #include <xf86drm.h>
93 #endif
94
95 #define __IS_LOADER
96 #include "pci_id_driver_map.h"
97
98 static void default_logger(int level, const char *fmt, ...)
99 {
100 if (level <= _LOADER_WARNING) {
101 va_list args;
102 va_start(args, fmt);
103 vfprintf(stderr, fmt, args);
104 va_end(args);
105 }
106 }
107
108 static void (*log_)(int level, const char *fmt, ...) = default_logger;
109
110 int
111 loader_open_device(const char *device_name)
112 {
113 int fd;
114 #ifdef O_CLOEXEC
115 fd = open(device_name, O_RDWR | O_CLOEXEC);
116 if (fd == -1 && errno == EINVAL)
117 #endif
118 {
119 fd = open(device_name, O_RDWR);
120 if (fd != -1)
121 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
122 }
123 return fd;
124 }
125
126 #ifdef HAVE_LIBUDEV
127 #include <libudev.h>
128
129 static void *udev_handle = NULL;
130
131 static void *
132 udev_dlopen_handle(void)
133 {
134 char name[80];
135 unsigned flags = RTLD_NOLOAD | RTLD_LOCAL | RTLD_LAZY;
136 int version;
137
138 /* libudev.so.1 changed the return types of the two unref functions
139 * from voids to pointers. We don't use those return values, and the
140 * only ABI I've heard that cares about this kind of change (calling
141 * a function with a void * return that actually only returns void)
142 * might be ia64.
143 */
144
145 /* First try opening an already linked libudev, then try loading one */
146 do {
147 for (version = 1; version >= 0; version--) {
148 snprintf(name, sizeof(name), "libudev.so.%d", version);
149 udev_handle = dlopen(name, flags);
150 if (udev_handle)
151 return udev_handle;
152 }
153
154 if ((flags & RTLD_NOLOAD) == 0)
155 break;
156
157 flags &= ~RTLD_NOLOAD;
158 } while (1);
159
160 log_(_LOADER_WARNING,
161 "Couldn't dlopen libudev.so.1 or "
162 "libudev.so.0, driver detection may be broken.\n");
163 return NULL;
164 }
165
166 static int dlsym_failed = 0;
167
168 static void *
169 checked_dlsym(void *dlopen_handle, const char *name)
170 {
171 void *result = dlsym(dlopen_handle, name);
172 if (!result)
173 dlsym_failed = 1;
174 return result;
175 }
176
177 #define UDEV_SYMBOL(ret, name, args) \
178 ret (*name) args = checked_dlsym(udev_dlopen_handle(), #name);
179
180
181 static inline struct udev_device *
182 udev_device_new_from_fd(struct udev *udev, int fd)
183 {
184 struct udev_device *device;
185 struct stat buf;
186 UDEV_SYMBOL(struct udev_device *, udev_device_new_from_devnum,
187 (struct udev *udev, char type, dev_t devnum));
188
189 if (dlsym_failed)
190 return NULL;
191
192 if (fstat(fd, &buf) < 0) {
193 log_(_LOADER_WARNING, "MESA-LOADER: failed to stat fd %d\n", fd);
194 return NULL;
195 }
196
197 device = udev_device_new_from_devnum(udev, 'c', buf.st_rdev);
198 if (device == NULL) {
199 log_(_LOADER_WARNING,
200 "MESA-LOADER: could not create udev device for fd %d\n", fd);
201 return NULL;
202 }
203
204 return device;
205 }
206
207 static char *
208 get_render_node_from_id_path_tag(struct udev *udev,
209 char *id_path_tag,
210 char another_tag)
211 {
212 struct udev_device *device;
213 struct udev_enumerate *e;
214 struct udev_list_entry *entry;
215 const char *path, *id_path_tag_tmp;
216 char *path_res;
217 char found = 0;
218 UDEV_SYMBOL(struct udev_enumerate *, udev_enumerate_new,
219 (struct udev *));
220 UDEV_SYMBOL(int, udev_enumerate_add_match_subsystem,
221 (struct udev_enumerate *, const char *));
222 UDEV_SYMBOL(int, udev_enumerate_add_match_sysname,
223 (struct udev_enumerate *, const char *));
224 UDEV_SYMBOL(int, udev_enumerate_scan_devices,
225 (struct udev_enumerate *));
226 UDEV_SYMBOL(struct udev_list_entry *, udev_enumerate_get_list_entry,
227 (struct udev_enumerate *));
228 UDEV_SYMBOL(void, udev_enumerate_unref,
229 (struct udev_enumerate *));
230 UDEV_SYMBOL(struct udev_list_entry *, udev_list_entry_get_next,
231 (struct udev_list_entry *));
232 UDEV_SYMBOL(const char *, udev_list_entry_get_name,
233 (struct udev_list_entry *));
234 UDEV_SYMBOL(struct udev_device *, udev_device_new_from_syspath,
235 (struct udev *, const char *));
236 UDEV_SYMBOL(const char *, udev_device_get_property_value,
237 (struct udev_device *, const char *));
238 UDEV_SYMBOL(const char *, udev_device_get_devnode,
239 (struct udev_device *));
240 UDEV_SYMBOL(struct udev_device *, udev_device_unref,
241 (struct udev_device *));
242
243 e = udev_enumerate_new(udev);
244 udev_enumerate_add_match_subsystem(e, "drm");
245 udev_enumerate_add_match_sysname(e, "render*");
246
247 udev_enumerate_scan_devices(e);
248 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
249 path = udev_list_entry_get_name(entry);
250 device = udev_device_new_from_syspath(udev, path);
251 if (!device)
252 continue;
253 id_path_tag_tmp = udev_device_get_property_value(device, "ID_PATH_TAG");
254 if (id_path_tag_tmp) {
255 if ((!another_tag && !strcmp(id_path_tag, id_path_tag_tmp)) ||
256 (another_tag && strcmp(id_path_tag, id_path_tag_tmp))) {
257 found = 1;
258 break;
259 }
260 }
261 udev_device_unref(device);
262 }
263
264 udev_enumerate_unref(e);
265
266 if (found) {
267 path_res = strdup(udev_device_get_devnode(device));
268 udev_device_unref(device);
269 return path_res;
270 }
271 return NULL;
272 }
273
274 static char *
275 get_id_path_tag_from_fd(struct udev *udev, int fd)
276 {
277 struct udev_device *device;
278 const char *id_path_tag_tmp;
279 char *id_path_tag;
280 UDEV_SYMBOL(const char *, udev_device_get_property_value,
281 (struct udev_device *, const char *));
282 UDEV_SYMBOL(struct udev_device *, udev_device_unref,
283 (struct udev_device *));
284
285 device = udev_device_new_from_fd(udev, fd);
286 if (!device)
287 return NULL;
288
289 id_path_tag_tmp = udev_device_get_property_value(device, "ID_PATH_TAG");
290 if (!id_path_tag_tmp)
291 return NULL;
292
293 id_path_tag = strdup(id_path_tag_tmp);
294
295 udev_device_unref(device);
296 return id_path_tag;
297 }
298
299 #ifdef USE_DRICONF
300 const char __driConfigOptionsLoader[] =
301 DRI_CONF_BEGIN
302 DRI_CONF_SECTION_INITIALIZATION
303 DRI_CONF_DEVICE_ID_PATH_TAG()
304 DRI_CONF_SECTION_END
305 DRI_CONF_END;
306
307 static char *loader_get_dri_config_device_id(void)
308 {
309 driOptionCache defaultInitOptions;
310 driOptionCache userInitOptions;
311 char *prime = NULL;
312
313 driParseOptionInfo(&defaultInitOptions, __driConfigOptionsLoader);
314 driParseConfigFiles(&userInitOptions, &defaultInitOptions, 0, "loader");
315 if (driCheckOption(&userInitOptions, "device_id", DRI_STRING))
316 prime = strdup(driQueryOptionstr(&userInitOptions, "device_id"));
317 driDestroyOptionCache(&userInitOptions);
318 driDestroyOptionInfo(&defaultInitOptions);
319
320 return prime;
321 }
322 #endif
323
324 int loader_get_user_preferred_fd(int default_fd, int *different_device)
325 {
326 struct udev *udev;
327 const char *dri_prime = getenv("DRI_PRIME");
328 char *prime = NULL;
329 int is_different_device = 0, fd = default_fd;
330 char *default_device_id_path_tag;
331 char *device_name = NULL;
332 char another_tag = 0;
333 UDEV_SYMBOL(struct udev *, udev_new, (void));
334 UDEV_SYMBOL(struct udev *, udev_unref, (struct udev *));
335
336 if (dri_prime)
337 prime = strdup(dri_prime);
338 #ifdef USE_DRICONF
339 else
340 prime = loader_get_dri_config_device_id();
341 #endif
342
343 if (prime == NULL) {
344 *different_device = 0;
345 return default_fd;
346 }
347
348 udev = udev_new();
349 if (!udev)
350 goto prime_clean;
351
352 default_device_id_path_tag = get_id_path_tag_from_fd(udev, default_fd);
353 if (!default_device_id_path_tag)
354 goto udev_clean;
355
356 is_different_device = 1;
357 /* two format are supported:
358 * "1": choose any other card than the card used by default.
359 * id_path_tag: (for example "pci-0000_02_00_0") choose the card
360 * with this id_path_tag.
361 */
362 if (!strcmp(prime,"1")) {
363 free(prime);
364 prime = strdup(default_device_id_path_tag);
365 /* request a card with a different card than the default card */
366 another_tag = 1;
367 } else if (!strcmp(default_device_id_path_tag, prime))
368 /* we are to get a new fd (render-node) of the same device */
369 is_different_device = 0;
370
371 device_name = get_render_node_from_id_path_tag(udev,
372 prime,
373 another_tag);
374 if (device_name == NULL) {
375 is_different_device = 0;
376 goto default_device_clean;
377 }
378
379 fd = loader_open_device(device_name);
380 if (fd >= 0) {
381 close(default_fd);
382 } else {
383 fd = default_fd;
384 is_different_device = 0;
385 }
386 free(device_name);
387
388 default_device_clean:
389 free(default_device_id_path_tag);
390 udev_clean:
391 udev_unref(udev);
392 prime_clean:
393 free(prime);
394
395 *different_device = is_different_device;
396 return fd;
397 }
398 #else
399 int loader_get_user_preferred_fd(int default_fd, int *different_device)
400 {
401 *different_device = 0;
402 return default_fd;
403 }
404 #endif
405
406 #if defined(HAVE_SYSFS) || defined(HAVE_LIBDRM)
407 static int
408 dev_node_from_fd(int fd, unsigned int *maj, unsigned int *min)
409 {
410 struct stat buf;
411
412 if (fstat(fd, &buf) < 0) {
413 log_(_LOADER_WARNING, "MESA-LOADER: failed to stat fd %d\n", fd);
414 return -1;
415 }
416
417 if (!S_ISCHR(buf.st_mode)) {
418 log_(_LOADER_WARNING, "MESA-LOADER: fd %d not a character device\n", fd);
419 return -1;
420 }
421
422 *maj = major(buf.st_rdev);
423 *min = minor(buf.st_rdev);
424
425 return 0;
426 }
427 #endif
428
429 #if defined(HAVE_LIBDRM)
430
431 static int
432 drm_get_pci_id_for_fd(int fd, int *vendor_id, int *chip_id)
433 {
434 drmDevicePtr device;
435 int ret;
436
437 if (drmGetDevice(fd, &device) == 0) {
438 if (device->bustype == DRM_BUS_PCI) {
439 *vendor_id = device->deviceinfo.pci->vendor_id;
440 *chip_id = device->deviceinfo.pci->device_id;
441 ret = 1;
442 }
443 else {
444 log_(_LOADER_WARNING, "MESA-LOADER: device is not located on the PCI bus\n");
445 ret = 0;
446 }
447 drmFreeDevice(&device);
448 }
449 else {
450 log_(_LOADER_WARNING, "MESA-LOADER: failed to retrieve device information\n");
451 ret = 0;
452 }
453
454 return ret;
455 }
456 #endif
457
458
459 int
460 loader_get_pci_id_for_fd(int fd, int *vendor_id, int *chip_id)
461 {
462 #if HAVE_LIBDRM
463 if (drm_get_pci_id_for_fd(fd, vendor_id, chip_id))
464 return 1;
465 #endif
466 return 0;
467 }
468
469
470 #ifdef HAVE_LIBUDEV
471 static char *
472 libudev_get_device_name_for_fd(int fd)
473 {
474 char *device_name = NULL;
475 struct udev *udev;
476 struct udev_device *device;
477 const char *const_device_name;
478 UDEV_SYMBOL(struct udev *, udev_new, (void));
479 UDEV_SYMBOL(const char *, udev_device_get_devnode,
480 (struct udev_device *));
481 UDEV_SYMBOL(struct udev_device *, udev_device_unref,
482 (struct udev_device *));
483 UDEV_SYMBOL(struct udev *, udev_unref, (struct udev *));
484
485 if (dlsym_failed)
486 return NULL;
487
488 udev = udev_new();
489 device = udev_device_new_from_fd(udev, fd);
490 if (device == NULL)
491 return NULL;
492
493 const_device_name = udev_device_get_devnode(device);
494 if (!const_device_name)
495 goto out;
496 device_name = strdup(const_device_name);
497
498 out:
499 udev_device_unref(device);
500 udev_unref(udev);
501 return device_name;
502 }
503 #endif
504
505
506 #if HAVE_SYSFS
507 static char *
508 sysfs_get_device_name_for_fd(int fd)
509 {
510 char *device_name = NULL;
511 unsigned int maj, min;
512 FILE *f;
513 char buf[0x40];
514 static const char match[9] = "\nDEVNAME=";
515 int expected = 1;
516
517 if (dev_node_from_fd(fd, &maj, &min) < 0)
518 return NULL;
519
520 snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/uevent", maj, min);
521 if (!(f = fopen(buf, "r")))
522 return NULL;
523
524 while (expected < sizeof(match)) {
525 int c = getc(f);
526
527 if (c == EOF) {
528 fclose(f);
529 return NULL;
530 } else if (c == match[expected] )
531 expected++;
532 else
533 expected = 0;
534 }
535
536 strcpy(buf, "/dev/");
537 if (fgets(buf + 5, sizeof(buf) - 5, f)) {
538 buf[strcspn(buf, "\n")] = '\0';
539 device_name = strdup(buf);
540 }
541
542 fclose(f);
543 return device_name;
544 }
545 #endif
546
547 #if defined(HAVE_LIBDRM)
548 static char *
549 drm_get_device_name_for_fd(int fd)
550 {
551 unsigned int maj, min;
552 char buf[0x40];
553 int n;
554
555 if (dev_node_from_fd(fd, &maj, &min) < 0)
556 return NULL;
557
558 n = snprintf(buf, sizeof(buf), DRM_DEV_NAME, DRM_DIR_NAME, min);
559 if (n == -1 || n >= sizeof(buf))
560 return NULL;
561
562 return strdup(buf);
563 }
564 #endif
565
566 char *
567 loader_get_device_name_for_fd(int fd)
568 {
569 char *result = NULL;
570
571 #if HAVE_LIBUDEV
572 if ((result = libudev_get_device_name_for_fd(fd)))
573 return result;
574 #endif
575 #if HAVE_SYSFS
576 if ((result = sysfs_get_device_name_for_fd(fd)))
577 return result;
578 #endif
579 #if HAVE_LIBDRM
580 if ((result = drm_get_device_name_for_fd(fd)))
581 return result;
582 #endif
583 return result;
584 }
585
586 char *
587 loader_get_driver_for_fd(int fd, unsigned driver_types)
588 {
589 int vendor_id, chip_id, i, j;
590 char *driver = NULL;
591
592 if (!driver_types)
593 driver_types = _LOADER_GALLIUM | _LOADER_DRI;
594
595 if (!loader_get_pci_id_for_fd(fd, &vendor_id, &chip_id)) {
596
597 #if HAVE_LIBDRM
598 /* fallback to drmGetVersion(): */
599 drmVersionPtr version = drmGetVersion(fd);
600
601 if (!version) {
602 log_(_LOADER_WARNING, "failed to get driver name for fd %d\n", fd);
603 return NULL;
604 }
605
606 driver = strndup(version->name, version->name_len);
607 log_(_LOADER_INFO, "using driver %s for %d\n", driver, fd);
608
609 drmFreeVersion(version);
610 #endif
611
612 return driver;
613 }
614
615 for (i = 0; driver_map[i].driver; i++) {
616 if (vendor_id != driver_map[i].vendor_id)
617 continue;
618
619 if (!(driver_types & driver_map[i].driver_types))
620 continue;
621
622 if (driver_map[i].predicate && !driver_map[i].predicate(fd))
623 continue;
624
625 if (driver_map[i].num_chips_ids == -1) {
626 driver = strdup(driver_map[i].driver);
627 goto out;
628 }
629
630 for (j = 0; j < driver_map[i].num_chips_ids; j++)
631 if (driver_map[i].chip_ids[j] == chip_id) {
632 driver = strdup(driver_map[i].driver);
633 goto out;
634 }
635 }
636
637 out:
638 log_(driver ? _LOADER_DEBUG : _LOADER_WARNING,
639 "pci id for fd %d: %04x:%04x, driver %s\n",
640 fd, vendor_id, chip_id, driver);
641 return driver;
642 }
643
644 void
645 loader_set_logger(void (*logger)(int level, const char *fmt, ...))
646 {
647 log_ = logger;
648 }