loader: add gpu selection code via DRI_PRIME.
[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 <stdarg.h>
68 #include <stdio.h>
69 #include <string.h>
70 #ifdef HAVE_LIBUDEV
71 #include <assert.h>
72 #include <dlfcn.h>
73 #include <fcntl.h>
74 #include <unistd.h>
75 #include <stdlib.h>
76 #include <errno.h>
77 #endif
78 #ifdef HAVE_SYSFS
79 #include <sys/stat.h>
80 #include <sys/types.h>
81 #endif
82 #include "loader.h"
83
84 #ifndef __NOT_HAVE_DRM_H
85 #include <xf86drm.h>
86 #endif
87
88 #define __IS_LOADER
89 #include "pci_id_driver_map.h"
90
91 static void default_logger(int level, const char *fmt, ...)
92 {
93 if (level <= _LOADER_WARNING) {
94 va_list args;
95 va_start(args, fmt);
96 vfprintf(stderr, fmt, args);
97 va_end(args);
98 }
99 }
100
101 static void (*log_)(int level, const char *fmt, ...) = default_logger;
102
103 #ifdef HAVE_LIBUDEV
104 #include <libudev.h>
105
106 static void *udev_handle = NULL;
107
108 static void *
109 udev_dlopen_handle(void)
110 {
111 if (!udev_handle) {
112 udev_handle = dlopen("libudev.so.1", RTLD_LOCAL | RTLD_LAZY);
113
114 if (!udev_handle) {
115 /* libudev.so.1 changed the return types of the two unref functions
116 * from voids to pointers. We don't use those return values, and the
117 * only ABI I've heard that cares about this kind of change (calling
118 * a function with a void * return that actually only returns void)
119 * might be ia64.
120 */
121 udev_handle = dlopen("libudev.so.0", RTLD_LOCAL | RTLD_LAZY);
122
123 if (!udev_handle) {
124 log_(_LOADER_WARNING, "Couldn't dlopen libudev.so.1 or "
125 "libudev.so.0, driver detection may be broken.\n");
126 }
127 }
128 }
129
130 return udev_handle;
131 }
132
133 static int dlsym_failed = 0;
134
135 static void *
136 checked_dlsym(void *dlopen_handle, const char *name)
137 {
138 void *result = dlsym(dlopen_handle, name);
139 if (!result)
140 dlsym_failed = 1;
141 return result;
142 }
143
144 #define UDEV_SYMBOL(ret, name, args) \
145 ret (*name) args = checked_dlsym(udev_dlopen_handle(), #name);
146
147
148 static inline struct udev_device *
149 udev_device_new_from_fd(struct udev *udev, int fd)
150 {
151 struct udev_device *device;
152 struct stat buf;
153 UDEV_SYMBOL(struct udev_device *, udev_device_new_from_devnum,
154 (struct udev *udev, char type, dev_t devnum));
155
156 if (dlsym_failed)
157 return NULL;
158
159 if (fstat(fd, &buf) < 0) {
160 log_(_LOADER_WARNING, "MESA-LOADER: failed to stat fd %d\n", fd);
161 return NULL;
162 }
163
164 device = udev_device_new_from_devnum(udev, 'c', buf.st_rdev);
165 if (device == NULL) {
166 log_(_LOADER_WARNING,
167 "MESA-LOADER: could not create udev device for fd %d\n", fd);
168 return NULL;
169 }
170
171 return device;
172 }
173
174 static int
175 libudev_get_pci_id_for_fd(int fd, int *vendor_id, int *chip_id)
176 {
177 struct udev *udev = NULL;
178 struct udev_device *device = NULL, *parent;
179 const char *pci_id;
180 UDEV_SYMBOL(struct udev *, udev_new, (void));
181 UDEV_SYMBOL(struct udev_device *, udev_device_get_parent,
182 (struct udev_device *));
183 UDEV_SYMBOL(const char *, udev_device_get_property_value,
184 (struct udev_device *, const char *));
185 UDEV_SYMBOL(struct udev_device *, udev_device_unref,
186 (struct udev_device *));
187 UDEV_SYMBOL(struct udev *, udev_unref, (struct udev *));
188
189 *chip_id = -1;
190
191 if (dlsym_failed)
192 return 0;
193
194 udev = udev_new();
195 device = udev_device_new_from_fd(udev, fd);
196 if (!device)
197 goto out;
198
199 parent = udev_device_get_parent(device);
200 if (parent == NULL) {
201 log_(_LOADER_WARNING, "MESA-LOADER: could not get parent device\n");
202 goto out;
203 }
204
205 pci_id = udev_device_get_property_value(parent, "PCI_ID");
206 if (pci_id == NULL ||
207 sscanf(pci_id, "%x:%x", vendor_id, chip_id) != 2) {
208 log_(_LOADER_WARNING, "MESA-LOADER: malformed or no PCI ID\n");
209 *chip_id = -1;
210 goto out;
211 }
212
213 out:
214 if (device)
215 udev_device_unref(device);
216 if (udev)
217 udev_unref(udev);
218
219 return (*chip_id >= 0);
220 }
221
222 static char *
223 get_render_node_from_id_path_tag(struct udev *udev,
224 char *id_path_tag,
225 char another_tag)
226 {
227 struct udev_device *device;
228 struct udev_enumerate *e;
229 struct udev_list_entry *entry;
230 const char *path, *id_path_tag_tmp;
231 char *path_res;
232 char found = 0;
233 UDEV_SYMBOL(struct udev_enumerate *, udev_enumerate_new,
234 (struct udev *));
235 UDEV_SYMBOL(int, udev_enumerate_add_match_subsystem,
236 (struct udev_enumerate *, const char *));
237 UDEV_SYMBOL(int, udev_enumerate_add_match_sysname,
238 (struct udev_enumerate *, const char *));
239 UDEV_SYMBOL(int, udev_enumerate_scan_devices,
240 (struct udev_enumerate *));
241 UDEV_SYMBOL(struct udev_list_entry *, udev_enumerate_get_list_entry,
242 (struct udev_enumerate *));
243 UDEV_SYMBOL(struct udev_list_entry *, udev_list_entry_get_next,
244 (struct udev_list_entry *));
245 UDEV_SYMBOL(const char *, udev_list_entry_get_name,
246 (struct udev_list_entry *));
247 UDEV_SYMBOL(struct udev_device *, udev_device_new_from_syspath,
248 (struct udev *, const char *));
249 UDEV_SYMBOL(const char *, udev_device_get_property_value,
250 (struct udev_device *, const char *));
251 UDEV_SYMBOL(const char *, udev_device_get_devnode,
252 (struct udev_device *));
253 UDEV_SYMBOL(struct udev_device *, udev_device_unref,
254 (struct udev_device *));
255
256 e = udev_enumerate_new(udev);
257 udev_enumerate_add_match_subsystem(e, "drm");
258 udev_enumerate_add_match_sysname(e, "render*");
259
260 udev_enumerate_scan_devices(e);
261 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
262 path = udev_list_entry_get_name(entry);
263 device = udev_device_new_from_syspath(udev, path);
264 if (!device)
265 continue;
266 id_path_tag_tmp = udev_device_get_property_value(device, "ID_PATH_TAG");
267 if (id_path_tag_tmp) {
268 if ((!another_tag && !strcmp(id_path_tag, id_path_tag_tmp)) ||
269 (another_tag && strcmp(id_path_tag, id_path_tag_tmp))) {
270 found = 1;
271 break;
272 }
273 }
274 udev_device_unref(device);
275 }
276
277 if (found) {
278 path_res = strdup(udev_device_get_devnode(device));
279 udev_device_unref(device);
280 return path_res;
281 }
282 return NULL;
283 }
284
285 static char *
286 get_id_path_tag_from_fd(struct udev *udev, int fd)
287 {
288 struct udev_device *device;
289 const char *id_path_tag_tmp;
290 char *id_path_tag;
291 UDEV_SYMBOL(const char *, udev_device_get_property_value,
292 (struct udev_device *, const char *));
293 UDEV_SYMBOL(struct udev_device *, udev_device_unref,
294 (struct udev_device *));
295
296 device = udev_device_new_from_fd(udev, fd);
297 if (!device)
298 return NULL;
299
300 id_path_tag_tmp = udev_device_get_property_value(device, "ID_PATH_TAG");
301 if (!id_path_tag_tmp)
302 return NULL;
303
304 id_path_tag = strdup(id_path_tag_tmp);
305
306 udev_device_unref(device);
307 return id_path_tag;
308 }
309
310 static int
311 drm_open_device(const char *device_name)
312 {
313 int fd;
314 #ifdef O_CLOEXEC
315 fd = open(device_name, O_RDWR | O_CLOEXEC);
316 if (fd == -1 && errno == EINVAL)
317 #endif
318 {
319 fd = open(device_name, O_RDWR);
320 if (fd != -1)
321 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
322 }
323 return fd;
324 }
325
326 int loader_get_user_preferred_fd(int default_fd, int *different_device)
327 {
328 struct udev *udev;
329 const char *dri_prime = getenv("DRI_PRIME");
330 char *prime = NULL;
331 int is_different_device = 0, fd = default_fd;
332 char *default_device_id_path_tag;
333 char *device_name = NULL;
334 char another_tag = 0;
335 UDEV_SYMBOL(struct udev *, udev_new, (void));
336 UDEV_SYMBOL(struct udev *, udev_unref, (struct udev *));
337
338 if (dri_prime)
339 prime = strdup(dri_prime);
340
341 if (prime == NULL) {
342 *different_device = 0;
343 return default_fd;
344 }
345
346 udev = udev_new();
347 if (!udev)
348 goto prime_clean;
349
350 default_device_id_path_tag = get_id_path_tag_from_fd(udev, default_fd);
351 if (!default_device_id_path_tag)
352 goto udev_clean;
353
354 is_different_device = 1;
355 /* two format are supported:
356 * "1": choose any other card than the card used by default.
357 * id_path_tag: (for example "pci-0000_02_00_0") choose the card
358 * with this id_path_tag.
359 */
360 if (!strcmp(prime,"1")) {
361 free(prime);
362 prime = strdup(default_device_id_path_tag);
363 /* request a card with a different card than the default card */
364 another_tag = 1;
365 } else if (!strcmp(default_device_id_path_tag, prime))
366 /* we are to get a new fd (render-node) of the same device */
367 is_different_device = 0;
368
369 device_name = get_render_node_from_id_path_tag(udev,
370 prime,
371 another_tag);
372 if (device_name == NULL) {
373 is_different_device = 0;
374 goto default_device_clean;
375 }
376
377 fd = drm_open_device(device_name);
378 if (fd > 0) {
379 close(default_fd);
380 } else {
381 fd = default_fd;
382 is_different_device = 0;
383 }
384 free(device_name);
385
386 default_device_clean:
387 free(default_device_id_path_tag);
388 udev_clean:
389 udev_unref(udev);
390 prime_clean:
391 free(prime);
392
393 *different_device = is_different_device;
394 return fd;
395 }
396 #else
397 int loader_get_user_preferred_fd(int default_fd, int *different_device)
398 {
399 *different_device = 0;
400 return default_fd;
401 }
402 #endif
403
404 #if defined(HAVE_SYSFS)
405 static int
406 dev_node_from_fd(int fd, unsigned int *maj, unsigned int *min)
407 {
408 struct stat buf;
409
410 if (fstat(fd, &buf) < 0) {
411 log_(_LOADER_WARNING, "MESA-LOADER: failed to stat fd %d\n", fd);
412 return -1;
413 }
414
415 if (!S_ISCHR(buf.st_mode)) {
416 log_(_LOADER_WARNING, "MESA-LOADER: fd %d not a character device\n", fd);
417 return -1;
418 }
419
420 *maj = major(buf.st_rdev);
421 *min = minor(buf.st_rdev);
422
423 return 0;
424 }
425
426 static int
427 sysfs_get_pci_id_for_fd(int fd, int *vendor_id, int *chip_id)
428 {
429 unsigned int maj, min;
430 FILE *f;
431 char buf[0x40];
432
433 if (dev_node_from_fd(fd, &maj, &min) < 0) {
434 *chip_id = -1;
435 return 0;
436 }
437
438 snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/vendor", maj, min);
439 if (!(f = fopen(buf, "r"))) {
440 *chip_id = -1;
441 return 0;
442 }
443 if (fscanf(f, "%x", vendor_id) != 1) {
444 *chip_id = -1;
445 fclose(f);
446 return 0;
447 }
448 fclose(f);
449 snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/device", maj, min);
450 if (!(f = fopen(buf, "r"))) {
451 *chip_id = -1;
452 return 0;
453 }
454 if (fscanf(f, "%x", chip_id) != 1) {
455 *chip_id = -1;
456 fclose(f);
457 return 0;
458 }
459 fclose(f);
460 return 1;
461 }
462 #endif
463
464 #if !defined(__NOT_HAVE_DRM_H)
465 /* for i915 */
466 #include <i915_drm.h>
467 /* for radeon */
468 #include <radeon_drm.h>
469
470 static int
471 drm_get_pci_id_for_fd(int fd, int *vendor_id, int *chip_id)
472 {
473 drmVersionPtr version;
474
475 *chip_id = -1;
476
477 version = drmGetVersion(fd);
478 if (!version) {
479 log_(_LOADER_WARNING, "MESA-LOADER: invalid drm fd\n");
480 return 0;
481 }
482 if (!version->name) {
483 log_(_LOADER_WARNING, "MESA-LOADER: unable to determine the driver name\n");
484 drmFreeVersion(version);
485 return 0;
486 }
487
488 if (strcmp(version->name, "i915") == 0) {
489 struct drm_i915_getparam gp;
490 int ret;
491
492 *vendor_id = 0x8086;
493
494 memset(&gp, 0, sizeof(gp));
495 gp.param = I915_PARAM_CHIPSET_ID;
496 gp.value = chip_id;
497 ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
498 if (ret) {
499 log_(_LOADER_WARNING, "MESA-LOADER: failed to get param for i915\n");
500 *chip_id = -1;
501 }
502 }
503 else if (strcmp(version->name, "radeon") == 0) {
504 struct drm_radeon_info info;
505 int ret;
506
507 *vendor_id = 0x1002;
508
509 memset(&info, 0, sizeof(info));
510 info.request = RADEON_INFO_DEVICE_ID;
511 info.value = (unsigned long) chip_id;
512 ret = drmCommandWriteRead(fd, DRM_RADEON_INFO, &info, sizeof(info));
513 if (ret) {
514 log_(_LOADER_WARNING, "MESA-LOADER: failed to get info for radeon\n");
515 *chip_id = -1;
516 }
517 }
518 else if (strcmp(version->name, "nouveau") == 0) {
519 *vendor_id = 0x10de;
520 /* not used */
521 *chip_id = 0;
522 }
523 else if (strcmp(version->name, "vmwgfx") == 0) {
524 *vendor_id = 0x15ad;
525 /* assume SVGA II */
526 *chip_id = 0x0405;
527 }
528
529 drmFreeVersion(version);
530
531 return (*chip_id >= 0);
532 }
533 #endif
534
535
536 int
537 loader_get_pci_id_for_fd(int fd, int *vendor_id, int *chip_id)
538 {
539 #if HAVE_LIBUDEV
540 if (libudev_get_pci_id_for_fd(fd, vendor_id, chip_id))
541 return 1;
542 #endif
543 #if HAVE_SYSFS
544 if (sysfs_get_pci_id_for_fd(fd, vendor_id, chip_id))
545 return 1;
546 #endif
547 #if !defined(__NOT_HAVE_DRM_H)
548 if (drm_get_pci_id_for_fd(fd, vendor_id, chip_id))
549 return 1;
550 #endif
551 return 0;
552 }
553
554
555 #ifdef HAVE_LIBUDEV
556 static char *
557 libudev_get_device_name_for_fd(int fd)
558 {
559 char *device_name = NULL;
560 struct udev *udev;
561 struct udev_device *device;
562 const char *const_device_name;
563 UDEV_SYMBOL(struct udev *, udev_new, (void));
564 UDEV_SYMBOL(const char *, udev_device_get_devnode,
565 (struct udev_device *));
566 UDEV_SYMBOL(struct udev_device *, udev_device_unref,
567 (struct udev_device *));
568 UDEV_SYMBOL(struct udev *, udev_unref, (struct udev *));
569
570 udev = udev_new();
571 device = udev_device_new_from_fd(udev, fd);
572 if (device == NULL)
573 return NULL;
574
575 const_device_name = udev_device_get_devnode(device);
576 if (!const_device_name)
577 goto out;
578 device_name = strdup(const_device_name);
579
580 out:
581 udev_device_unref(device);
582 udev_unref(udev);
583 return device_name;
584 }
585 #endif
586
587
588 #if HAVE_SYSFS
589 static char *
590 sysfs_get_device_name_for_fd(int fd)
591 {
592 char *device_name = NULL;
593 unsigned int maj, min;
594 FILE *f;
595 char buf[0x40];
596 static const char match[9] = "\0DEVNAME=";
597 int expected = 1;
598
599 if (dev_node_from_fd(fd, &maj, &min) < 0)
600 return NULL;
601
602 snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/uevent", maj, min);
603 if (!(f = fopen(buf, "r")))
604 return NULL;
605
606 while (expected < sizeof(match)) {
607 int c = getc(f);
608
609 if (c == EOF) {
610 fclose(f);
611 return NULL;
612 } else if (c == match[expected] )
613 expected++;
614 else
615 expected = 0;
616 }
617
618 strcpy(buf, "/dev/");
619 if (fgets(buf + 5, sizeof(buf) - 5, f))
620 device_name = strdup(buf);
621
622 fclose(f);
623 return device_name;
624 }
625 #endif
626
627
628 char *
629 loader_get_device_name_for_fd(int fd)
630 {
631 char *result = NULL;
632
633 #if HAVE_LIBUDEV
634 if ((result = libudev_get_device_name_for_fd(fd)))
635 return result;
636 #endif
637 #if HAVE_SYSFS
638 if ((result = sysfs_get_device_name_for_fd(fd)))
639 return result;
640 #endif
641 return result;
642 }
643
644 char *
645 loader_get_driver_for_fd(int fd, unsigned driver_types)
646 {
647 int vendor_id, chip_id, i, j;
648 char *driver = NULL;
649
650 if (!driver_types)
651 driver_types = _LOADER_GALLIUM | _LOADER_DRI;
652
653 if (!loader_get_pci_id_for_fd(fd, &vendor_id, &chip_id)) {
654
655 #ifndef __NOT_HAVE_DRM_H
656 /* fallback to drmGetVersion(): */
657 drmVersionPtr version = drmGetVersion(fd);
658
659 if (!version) {
660 log_(_LOADER_WARNING, "failed to get driver name for fd %d\n", fd);
661 return NULL;
662 }
663
664 driver = strndup(version->name, version->name_len);
665 log_(_LOADER_INFO, "using driver %s for %d\n", driver, fd);
666
667 drmFreeVersion(version);
668 #endif
669
670 return driver;
671 }
672
673 for (i = 0; driver_map[i].driver; i++) {
674 if (vendor_id != driver_map[i].vendor_id)
675 continue;
676
677 if (!(driver_types & driver_map[i].driver_types))
678 continue;
679
680 if (driver_map[i].predicate && !driver_map[i].predicate(fd))
681 continue;
682
683 if (driver_map[i].num_chips_ids == -1) {
684 driver = strdup(driver_map[i].driver);
685 goto out;
686 }
687
688 for (j = 0; j < driver_map[i].num_chips_ids; j++)
689 if (driver_map[i].chip_ids[j] == chip_id) {
690 driver = strdup(driver_map[i].driver);
691 goto out;
692 }
693 }
694
695 out:
696 log_(driver ? _LOADER_DEBUG : _LOADER_WARNING,
697 "pci id for fd %d: %04x:%04x, driver %s\n",
698 fd, vendor_id, chip_id, driver);
699 return driver;
700 }
701
702 void
703 loader_set_logger(void (*logger)(int level, const char *fmt, ...))
704 {
705 log_ = logger;
706 }