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