loader: add optional /sys filesystem method for PCI identification.
[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 #endif
74 #ifdef HAVE_SYSFS
75 #include <sys/stat.h>
76 #include <sys/types.h>
77 #endif
78 #include "loader.h"
79
80 #ifndef __NOT_HAVE_DRM_H
81 #include <xf86drm.h>
82 #endif
83
84 #define __IS_LOADER
85 #include "pci_id_driver_map.h"
86
87 static void default_logger(int level, const char *fmt, ...)
88 {
89 if (level <= _LOADER_WARNING) {
90 va_list args;
91 va_start(args, fmt);
92 vfprintf(stderr, fmt, args);
93 va_end(args);
94 }
95 }
96
97 static void (*log_)(int level, const char *fmt, ...) = default_logger;
98
99 #ifdef HAVE_LIBUDEV
100 #include <libudev.h>
101
102 static void *udev_handle = NULL;
103
104 static void *
105 udev_dlopen_handle(void)
106 {
107 if (!udev_handle) {
108 udev_handle = dlopen("libudev.so.1", RTLD_LOCAL | RTLD_LAZY);
109
110 if (!udev_handle) {
111 /* libudev.so.1 changed the return types of the two unref functions
112 * from voids to pointers. We don't use those return values, and the
113 * only ABI I've heard that cares about this kind of change (calling
114 * a function with a void * return that actually only returns void)
115 * might be ia64.
116 */
117 udev_handle = dlopen("libudev.so.0", RTLD_LOCAL | RTLD_LAZY);
118
119 if (!udev_handle) {
120 log_(_LOADER_WARNING, "Couldn't dlopen libudev.so.1 or "
121 "libudev.so.0, driver detection may be broken.\n");
122 }
123 }
124 }
125
126 return udev_handle;
127 }
128
129 static int dlsym_failed = 0;
130
131 static void *
132 checked_dlsym(void *dlopen_handle, const char *name)
133 {
134 void *result = dlsym(dlopen_handle, name);
135 if (!result)
136 dlsym_failed = 1;
137 return result;
138 }
139
140 #define UDEV_SYMBOL(ret, name, args) \
141 ret (*name) args = checked_dlsym(udev_dlopen_handle(), #name);
142
143
144 static inline struct udev_device *
145 udev_device_new_from_fd(struct udev *udev, int fd)
146 {
147 struct udev_device *device;
148 struct stat buf;
149 UDEV_SYMBOL(struct udev_device *, udev_device_new_from_devnum,
150 (struct udev *udev, char type, dev_t devnum));
151
152 if (dlsym_failed)
153 return NULL;
154
155 if (fstat(fd, &buf) < 0) {
156 log_(_LOADER_WARNING, "MESA-LOADER: failed to stat fd %d\n", fd);
157 return NULL;
158 }
159
160 device = udev_device_new_from_devnum(udev, 'c', buf.st_rdev);
161 if (device == NULL) {
162 log_(_LOADER_WARNING,
163 "MESA-LOADER: could not create udev device for fd %d\n", fd);
164 return NULL;
165 }
166
167 return device;
168 }
169
170 static int
171 libudev_get_pci_id_for_fd(int fd, int *vendor_id, int *chip_id)
172 {
173 struct udev *udev = NULL;
174 struct udev_device *device = NULL, *parent;
175 const char *pci_id;
176 UDEV_SYMBOL(struct udev *, udev_new, (void));
177 UDEV_SYMBOL(struct udev_device *, udev_device_get_parent,
178 (struct udev_device *));
179 UDEV_SYMBOL(const char *, udev_device_get_property_value,
180 (struct udev_device *, const char *));
181 UDEV_SYMBOL(struct udev_device *, udev_device_unref,
182 (struct udev_device *));
183 UDEV_SYMBOL(struct udev *, udev_unref, (struct udev *));
184
185 *chip_id = -1;
186
187 if (dlsym_failed)
188 return 0;
189
190 udev = udev_new();
191 device = udev_device_new_from_fd(udev, fd);
192 if (!device)
193 goto out;
194
195 parent = udev_device_get_parent(device);
196 if (parent == NULL) {
197 log_(_LOADER_WARNING, "MESA-LOADER: could not get parent device\n");
198 goto out;
199 }
200
201 pci_id = udev_device_get_property_value(parent, "PCI_ID");
202 if (pci_id == NULL ||
203 sscanf(pci_id, "%x:%x", vendor_id, chip_id) != 2) {
204 log_(_LOADER_WARNING, "MESA-LOADER: malformed or no PCI ID\n");
205 *chip_id = -1;
206 goto out;
207 }
208
209 out:
210 if (device)
211 udev_device_unref(device);
212 if (udev)
213 udev_unref(udev);
214
215 return (*chip_id >= 0);
216 }
217 #endif
218
219 #if defined(HAVE_SYSFS)
220 static int
221 dev_node_from_fd(int fd, unsigned int *maj, unsigned int *min)
222 {
223 struct stat buf;
224
225 if (fstat(fd, &buf) < 0) {
226 log_(_LOADER_WARNING, "MESA-LOADER: failed to stat fd %d\n", fd);
227 return -1;
228 }
229
230 if (!S_ISCHR(buf.st_mode)) {
231 log_(_LOADER_WARNING, "MESA-LOADER: fd %d not a character device\n", fd);
232 return -1;
233 }
234
235 *maj = major(buf.st_rdev);
236 *min = minor(buf.st_rdev);
237
238 return 0;
239 }
240
241 static int
242 sysfs_get_pci_id_for_fd(int fd, int *vendor_id, int *chip_id)
243 {
244 unsigned int maj, min;
245 FILE *f;
246 char buf[0x40];
247
248 if (dev_node_from_fd(fd, &maj, &min) < 0) {
249 *chip_id = -1;
250 return 0;
251 }
252
253 snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/vendor", maj, min);
254 if (!(f = fopen(buf, "r"))) {
255 *chip_id = -1;
256 return 0;
257 }
258 if (fscanf(f, "%x", vendor_id) != 1) {
259 *chip_id = -1;
260 fclose(f);
261 return 0;
262 }
263 fclose(f);
264 snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/device", maj, min);
265 if (!(f = fopen(buf, "r"))) {
266 *chip_id = -1;
267 return 0;
268 }
269 if (fscanf(f, "%x", chip_id) != 1) {
270 *chip_id = -1;
271 fclose(f);
272 return 0;
273 }
274 fclose(f);
275 return 1;
276 }
277 #endif
278
279 #if !defined(__NOT_HAVE_DRM_H)
280 /* for i915 */
281 #include <i915_drm.h>
282 /* for radeon */
283 #include <radeon_drm.h>
284
285 static int
286 drm_get_pci_id_for_fd(int fd, int *vendor_id, int *chip_id)
287 {
288 drmVersionPtr version;
289
290 *chip_id = -1;
291
292 version = drmGetVersion(fd);
293 if (!version) {
294 log_(_LOADER_WARNING, "MESA-LOADER: invalid drm fd\n");
295 return 0;
296 }
297 if (!version->name) {
298 log_(_LOADER_WARNING, "MESA-LOADER: unable to determine the driver name\n");
299 drmFreeVersion(version);
300 return 0;
301 }
302
303 if (strcmp(version->name, "i915") == 0) {
304 struct drm_i915_getparam gp;
305 int ret;
306
307 *vendor_id = 0x8086;
308
309 memset(&gp, 0, sizeof(gp));
310 gp.param = I915_PARAM_CHIPSET_ID;
311 gp.value = chip_id;
312 ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
313 if (ret) {
314 log_(_LOADER_WARNING, "MESA-LOADER: failed to get param for i915\n");
315 *chip_id = -1;
316 }
317 }
318 else if (strcmp(version->name, "radeon") == 0) {
319 struct drm_radeon_info info;
320 int ret;
321
322 *vendor_id = 0x1002;
323
324 memset(&info, 0, sizeof(info));
325 info.request = RADEON_INFO_DEVICE_ID;
326 info.value = (unsigned long) chip_id;
327 ret = drmCommandWriteRead(fd, DRM_RADEON_INFO, &info, sizeof(info));
328 if (ret) {
329 log_(_LOADER_WARNING, "MESA-LOADER: failed to get info for radeon\n");
330 *chip_id = -1;
331 }
332 }
333 else if (strcmp(version->name, "nouveau") == 0) {
334 *vendor_id = 0x10de;
335 /* not used */
336 *chip_id = 0;
337 }
338 else if (strcmp(version->name, "vmwgfx") == 0) {
339 *vendor_id = 0x15ad;
340 /* assume SVGA II */
341 *chip_id = 0x0405;
342 }
343
344 drmFreeVersion(version);
345
346 return (*chip_id >= 0);
347 }
348 #endif
349
350
351 int
352 loader_get_pci_id_for_fd(int fd, int *vendor_id, int *chip_id)
353 {
354 #if HAVE_LIBUDEV
355 if (libudev_get_pci_id_for_fd(fd, vendor_id, chip_id))
356 return 1;
357 #endif
358 #if HAVE_SYSFS
359 if (sysfs_get_pci_id_for_fd(fd, vendor_id, chip_id))
360 return 1;
361 #endif
362 #if !defined(__NOT_HAVE_DRM_H)
363 if (drm_get_pci_id_for_fd(fd, vendor_id, chip_id))
364 return 1;
365 #endif
366 return 0;
367 }
368
369
370 #ifdef HAVE_LIBUDEV
371 static char *
372 libudev_get_device_name_for_fd(int fd)
373 {
374 char *device_name = NULL;
375 struct udev *udev;
376 struct udev_device *device;
377 const char *const_device_name;
378 UDEV_SYMBOL(struct udev *, udev_new, (void));
379 UDEV_SYMBOL(const char *, udev_device_get_devnode,
380 (struct udev_device *));
381 UDEV_SYMBOL(struct udev_device *, udev_device_unref,
382 (struct udev_device *));
383 UDEV_SYMBOL(struct udev *, udev_unref, (struct udev *));
384
385 udev = udev_new();
386 device = udev_device_new_from_fd(udev, fd);
387 if (device == NULL)
388 return NULL;
389
390 const_device_name = udev_device_get_devnode(device);
391 if (!const_device_name)
392 goto out;
393 device_name = strdup(const_device_name);
394
395 out:
396 udev_device_unref(device);
397 udev_unref(udev);
398 return device_name;
399 }
400 #endif
401
402
403 #if HAVE_SYSFS
404 static char *
405 sysfs_get_device_name_for_fd(int fd)
406 {
407 char *device_name = NULL;
408 unsigned int maj, min;
409 FILE *f;
410 char buf[0x40];
411 static const char match[9] = "\0DEVNAME=";
412 int expected = 1;
413
414 if (dev_node_from_fd(fd, &maj, &min) < 0)
415 return NULL;
416
417 snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/uevent", maj, min);
418 if (!(f = fopen(buf, "r")))
419 return NULL;
420
421 while (expected < sizeof(match)) {
422 int c = getc(f);
423
424 if (c == EOF) {
425 fclose(f);
426 return NULL;
427 } else if (c == match[expected] )
428 expected++;
429 else
430 expected = 0;
431 }
432
433 strcpy(buf, "/dev/");
434 if (fgets(buf + 5, sizeof(buf) - 5, f))
435 device_name = strdup(buf);
436
437 fclose(f);
438 return device_name;
439 }
440 #endif
441
442
443 char *
444 loader_get_device_name_for_fd(int fd)
445 {
446 char *result = NULL;
447
448 #if HAVE_LIBUDEV
449 if ((result = libudev_get_device_name_for_fd(fd)))
450 return result;
451 #endif
452 #if HAVE_SYSFS
453 if ((result = sysfs_get_device_name_for_fd(fd)))
454 return result;
455 #endif
456 return result;
457 }
458
459 char *
460 loader_get_driver_for_fd(int fd, unsigned driver_types)
461 {
462 int vendor_id, chip_id, i, j;
463 char *driver = NULL;
464
465 if (!driver_types)
466 driver_types = _LOADER_GALLIUM | _LOADER_DRI;
467
468 if (!loader_get_pci_id_for_fd(fd, &vendor_id, &chip_id)) {
469
470 #ifndef __NOT_HAVE_DRM_H
471 /* fallback to drmGetVersion(): */
472 drmVersionPtr version = drmGetVersion(fd);
473
474 if (!version) {
475 log_(_LOADER_WARNING, "failed to get driver name for fd %d\n", fd);
476 return NULL;
477 }
478
479 driver = strndup(version->name, version->name_len);
480 log_(_LOADER_INFO, "using driver %s for %d\n", driver, fd);
481
482 drmFreeVersion(version);
483 #endif
484
485 return driver;
486 }
487
488 for (i = 0; driver_map[i].driver; i++) {
489 if (vendor_id != driver_map[i].vendor_id)
490 continue;
491
492 if (!(driver_types & driver_map[i].driver_types))
493 continue;
494
495 if (driver_map[i].predicate && !driver_map[i].predicate(fd))
496 continue;
497
498 if (driver_map[i].num_chips_ids == -1) {
499 driver = strdup(driver_map[i].driver);
500 goto out;
501 }
502
503 for (j = 0; j < driver_map[i].num_chips_ids; j++)
504 if (driver_map[i].chip_ids[j] == chip_id) {
505 driver = strdup(driver_map[i].driver);
506 goto out;
507 }
508 }
509
510 out:
511 log_(driver ? _LOADER_DEBUG : _LOADER_WARNING,
512 "pci id for fd %d: %04x:%04x, driver %s\n",
513 fd, vendor_id, chip_id, driver);
514 return driver;
515 }
516
517 void
518 loader_set_logger(void (*logger)(int level, const char *fmt, ...))
519 {
520 log_ = logger;
521 }