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