loader: slim down loader_get_pci_id_for_fd implementation(s)
[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 #endif
307
308 int loader_get_user_preferred_fd(int default_fd, int *different_device)
309 {
310 struct udev *udev;
311 #ifdef USE_DRICONF
312 driOptionCache defaultInitOptions;
313 driOptionCache userInitOptions;
314 #endif
315 const char *dri_prime = getenv("DRI_PRIME");
316 char *prime = NULL;
317 int is_different_device = 0, fd = default_fd;
318 char *default_device_id_path_tag;
319 char *device_name = NULL;
320 char another_tag = 0;
321 UDEV_SYMBOL(struct udev *, udev_new, (void));
322 UDEV_SYMBOL(struct udev *, udev_unref, (struct udev *));
323
324 if (dri_prime)
325 prime = strdup(dri_prime);
326 #ifdef USE_DRICONF
327 else {
328 driParseOptionInfo(&defaultInitOptions, __driConfigOptionsLoader);
329 driParseConfigFiles(&userInitOptions, &defaultInitOptions, 0, "loader");
330 if (driCheckOption(&userInitOptions, "device_id", DRI_STRING))
331 prime = strdup(driQueryOptionstr(&userInitOptions, "device_id"));
332 driDestroyOptionCache(&userInitOptions);
333 driDestroyOptionInfo(&defaultInitOptions);
334 }
335 #endif
336
337 if (prime == NULL) {
338 *different_device = 0;
339 return default_fd;
340 }
341
342 udev = udev_new();
343 if (!udev)
344 goto prime_clean;
345
346 default_device_id_path_tag = get_id_path_tag_from_fd(udev, default_fd);
347 if (!default_device_id_path_tag)
348 goto udev_clean;
349
350 is_different_device = 1;
351 /* two format are supported:
352 * "1": choose any other card than the card used by default.
353 * id_path_tag: (for example "pci-0000_02_00_0") choose the card
354 * with this id_path_tag.
355 */
356 if (!strcmp(prime,"1")) {
357 free(prime);
358 prime = strdup(default_device_id_path_tag);
359 /* request a card with a different card than the default card */
360 another_tag = 1;
361 } else if (!strcmp(default_device_id_path_tag, prime))
362 /* we are to get a new fd (render-node) of the same device */
363 is_different_device = 0;
364
365 device_name = get_render_node_from_id_path_tag(udev,
366 prime,
367 another_tag);
368 if (device_name == NULL) {
369 is_different_device = 0;
370 goto default_device_clean;
371 }
372
373 fd = loader_open_device(device_name);
374 if (fd >= 0) {
375 close(default_fd);
376 } else {
377 fd = default_fd;
378 is_different_device = 0;
379 }
380 free(device_name);
381
382 default_device_clean:
383 free(default_device_id_path_tag);
384 udev_clean:
385 udev_unref(udev);
386 prime_clean:
387 free(prime);
388
389 *different_device = is_different_device;
390 return fd;
391 }
392 #else
393 int loader_get_user_preferred_fd(int default_fd, int *different_device)
394 {
395 *different_device = 0;
396 return default_fd;
397 }
398 #endif
399
400 #if defined(HAVE_SYSFS) || defined(HAVE_LIBDRM)
401 static int
402 dev_node_from_fd(int fd, unsigned int *maj, unsigned int *min)
403 {
404 struct stat buf;
405
406 if (fstat(fd, &buf) < 0) {
407 log_(_LOADER_WARNING, "MESA-LOADER: failed to stat fd %d\n", fd);
408 return -1;
409 }
410
411 if (!S_ISCHR(buf.st_mode)) {
412 log_(_LOADER_WARNING, "MESA-LOADER: fd %d not a character device\n", fd);
413 return -1;
414 }
415
416 *maj = major(buf.st_rdev);
417 *min = minor(buf.st_rdev);
418
419 return 0;
420 }
421 #endif
422
423 #if defined(HAVE_LIBDRM)
424
425 static int
426 drm_get_pci_id_for_fd(int fd, int *vendor_id, int *chip_id)
427 {
428 drmDevicePtr device;
429 int ret;
430
431 if (drmGetDevice(fd, &device) == 0) {
432 if (device->bustype == DRM_BUS_PCI) {
433 *vendor_id = device->deviceinfo.pci->vendor_id;
434 *chip_id = device->deviceinfo.pci->device_id;
435 ret = 1;
436 }
437 else {
438 log_(_LOADER_WARNING, "MESA-LOADER: device is not located on the PCI bus\n");
439 ret = 0;
440 }
441 drmFreeDevice(&device);
442 }
443 else {
444 log_(_LOADER_WARNING, "MESA-LOADER: failed to retrieve device information\n");
445 ret = 0;
446 }
447
448 return ret;
449 }
450 #endif
451
452
453 int
454 loader_get_pci_id_for_fd(int fd, int *vendor_id, int *chip_id)
455 {
456 #if HAVE_LIBDRM
457 if (drm_get_pci_id_for_fd(fd, vendor_id, chip_id))
458 return 1;
459 #endif
460 return 0;
461 }
462
463
464 #ifdef HAVE_LIBUDEV
465 static char *
466 libudev_get_device_name_for_fd(int fd)
467 {
468 char *device_name = NULL;
469 struct udev *udev;
470 struct udev_device *device;
471 const char *const_device_name;
472 UDEV_SYMBOL(struct udev *, udev_new, (void));
473 UDEV_SYMBOL(const char *, udev_device_get_devnode,
474 (struct udev_device *));
475 UDEV_SYMBOL(struct udev_device *, udev_device_unref,
476 (struct udev_device *));
477 UDEV_SYMBOL(struct udev *, udev_unref, (struct udev *));
478
479 if (dlsym_failed)
480 return NULL;
481
482 udev = udev_new();
483 device = udev_device_new_from_fd(udev, fd);
484 if (device == NULL)
485 return NULL;
486
487 const_device_name = udev_device_get_devnode(device);
488 if (!const_device_name)
489 goto out;
490 device_name = strdup(const_device_name);
491
492 out:
493 udev_device_unref(device);
494 udev_unref(udev);
495 return device_name;
496 }
497 #endif
498
499
500 #if HAVE_SYSFS
501 static char *
502 sysfs_get_device_name_for_fd(int fd)
503 {
504 char *device_name = NULL;
505 unsigned int maj, min;
506 FILE *f;
507 char buf[0x40];
508 static const char match[9] = "\nDEVNAME=";
509 int expected = 1;
510
511 if (dev_node_from_fd(fd, &maj, &min) < 0)
512 return NULL;
513
514 snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/uevent", maj, min);
515 if (!(f = fopen(buf, "r")))
516 return NULL;
517
518 while (expected < sizeof(match)) {
519 int c = getc(f);
520
521 if (c == EOF) {
522 fclose(f);
523 return NULL;
524 } else if (c == match[expected] )
525 expected++;
526 else
527 expected = 0;
528 }
529
530 strcpy(buf, "/dev/");
531 if (fgets(buf + 5, sizeof(buf) - 5, f)) {
532 buf[strcspn(buf, "\n")] = '\0';
533 device_name = strdup(buf);
534 }
535
536 fclose(f);
537 return device_name;
538 }
539 #endif
540
541 #if defined(HAVE_LIBDRM)
542 static char *
543 drm_get_device_name_for_fd(int fd)
544 {
545 unsigned int maj, min;
546 char buf[0x40];
547 int n;
548
549 if (dev_node_from_fd(fd, &maj, &min) < 0)
550 return NULL;
551
552 n = snprintf(buf, sizeof(buf), DRM_DEV_NAME, DRM_DIR_NAME, min);
553 if (n == -1 || n >= sizeof(buf))
554 return NULL;
555
556 return strdup(buf);
557 }
558 #endif
559
560 char *
561 loader_get_device_name_for_fd(int fd)
562 {
563 char *result = NULL;
564
565 #if HAVE_LIBUDEV
566 if ((result = libudev_get_device_name_for_fd(fd)))
567 return result;
568 #endif
569 #if HAVE_SYSFS
570 if ((result = sysfs_get_device_name_for_fd(fd)))
571 return result;
572 #endif
573 #if HAVE_LIBDRM
574 if ((result = drm_get_device_name_for_fd(fd)))
575 return result;
576 #endif
577 return result;
578 }
579
580 char *
581 loader_get_driver_for_fd(int fd, unsigned driver_types)
582 {
583 int vendor_id, chip_id, i, j;
584 char *driver = NULL;
585
586 if (!driver_types)
587 driver_types = _LOADER_GALLIUM | _LOADER_DRI;
588
589 if (!loader_get_pci_id_for_fd(fd, &vendor_id, &chip_id)) {
590
591 #if HAVE_LIBDRM
592 /* fallback to drmGetVersion(): */
593 drmVersionPtr version = drmGetVersion(fd);
594
595 if (!version) {
596 log_(_LOADER_WARNING, "failed to get driver name for fd %d\n", fd);
597 return NULL;
598 }
599
600 driver = strndup(version->name, version->name_len);
601 log_(_LOADER_INFO, "using driver %s for %d\n", driver, fd);
602
603 drmFreeVersion(version);
604 #endif
605
606 return driver;
607 }
608
609 for (i = 0; driver_map[i].driver; i++) {
610 if (vendor_id != driver_map[i].vendor_id)
611 continue;
612
613 if (!(driver_types & driver_map[i].driver_types))
614 continue;
615
616 if (driver_map[i].predicate && !driver_map[i].predicate(fd))
617 continue;
618
619 if (driver_map[i].num_chips_ids == -1) {
620 driver = strdup(driver_map[i].driver);
621 goto out;
622 }
623
624 for (j = 0; j < driver_map[i].num_chips_ids; j++)
625 if (driver_map[i].chip_ids[j] == chip_id) {
626 driver = strdup(driver_map[i].driver);
627 goto out;
628 }
629 }
630
631 out:
632 log_(driver ? _LOADER_DEBUG : _LOADER_WARNING,
633 "pci id for fd %d: %04x:%04x, driver %s\n",
634 fd, vendor_id, chip_id, driver);
635 return driver;
636 }
637
638 void
639 loader_set_logger(void (*logger)(int level, const char *fmt, ...))
640 {
641 log_ = logger;
642 }