811f8a25713e5ade25b0509bd70b470b7f7f5826
[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 #include "loader.h"
75
76 #ifndef __NOT_HAVE_DRM_H
77 #include <xf86drm.h>
78 #endif
79
80 #define __IS_LOADER
81 #include "pci_ids/pci_id_driver_map.h"
82
83 static void default_logger(int level, const char *fmt, ...)
84 {
85 if (level <= _LOADER_WARNING) {
86 va_list args;
87 va_start(args, fmt);
88 vfprintf(stderr, fmt, args);
89 va_end(args);
90 }
91 }
92
93 static void (*log_)(int level, const char *fmt, ...) = default_logger;
94
95 #ifdef HAVE_LIBUDEV
96 #include <libudev.h>
97
98 static void *udev_handle = NULL;
99
100 static void *
101 udev_dlopen_handle(void)
102 {
103 if (!udev_handle) {
104 udev_handle = dlopen("libudev.so.1", RTLD_LOCAL | RTLD_LAZY);
105
106 if (!udev_handle) {
107 /* libudev.so.1 changed the return types of the two unref functions
108 * from voids to pointers. We don't use those return values, and the
109 * only ABI I've heard that cares about this kind of change (calling
110 * a function with a void * return that actually only returns void)
111 * might be ia64.
112 */
113 udev_handle = dlopen("libudev.so.0", RTLD_LOCAL | RTLD_LAZY);
114
115 if (!udev_handle) {
116 log_(_LOADER_FATAL, "Couldn't dlopen libudev.so.1 or libudev.so.0, "
117 "driver detection may be broken.\n");
118 }
119 }
120 }
121
122 return udev_handle;
123 }
124
125 static void *
126 asserted_dlsym(void *dlopen_handle, const char *name)
127 {
128 void *result = dlsym(dlopen_handle, name);
129 assert(result);
130 return result;
131 }
132
133 #define UDEV_SYMBOL(ret, name, args) \
134 ret (*name) args = asserted_dlsym(udev_dlopen_handle(), #name);
135
136
137 static inline struct udev_device *
138 udev_device_new_from_fd(struct udev *udev, int fd)
139 {
140 struct udev_device *device;
141 struct stat buf;
142 UDEV_SYMBOL(struct udev_device *, udev_device_new_from_devnum,
143 (struct udev *udev, char type, dev_t devnum));
144
145 if (fstat(fd, &buf) < 0) {
146 log_(_LOADER_WARNING, "MESA-LOADER: failed to stat fd %d\n", fd);
147 return NULL;
148 }
149
150 device = udev_device_new_from_devnum(udev, 'c', buf.st_rdev);
151 if (device == NULL) {
152 log_(_LOADER_WARNING,
153 "MESA-LOADER: could not create udev device for fd %d\n", fd);
154 return NULL;
155 }
156
157 return device;
158 }
159
160 int
161 loader_get_pci_id_for_fd(int fd, int *vendor_id, int *chip_id)
162 {
163 struct udev *udev = NULL;
164 struct udev_device *device = NULL, *parent;
165 const char *pci_id;
166 UDEV_SYMBOL(struct udev *, udev_new, (void));
167 UDEV_SYMBOL(struct udev_device *, udev_device_get_parent,
168 (struct udev_device *));
169 UDEV_SYMBOL(const char *, udev_device_get_property_value,
170 (struct udev_device *, const char *));
171 UDEV_SYMBOL(struct udev_device *, udev_device_unref,
172 (struct udev_device *));
173 UDEV_SYMBOL(struct udev *, udev_unref, (struct udev *));
174
175 *chip_id = -1;
176
177 udev = udev_new();
178 device = udev_device_new_from_fd(udev, fd);
179 if (!device)
180 goto out;
181
182 parent = udev_device_get_parent(device);
183 if (parent == NULL) {
184 log_(_LOADER_WARNING, "MESA-LOADER: could not get parent device\n");
185 goto out;
186 }
187
188 pci_id = udev_device_get_property_value(parent, "PCI_ID");
189 if (pci_id == NULL ||
190 sscanf(pci_id, "%x:%x", vendor_id, chip_id) != 2) {
191 log_(_LOADER_WARNING, "MESA-LOADER: malformed or no PCI ID\n");
192 *chip_id = -1;
193 goto out;
194 }
195
196 out:
197 if (device)
198 udev_device_unref(device);
199 if (udev)
200 udev_unref(udev);
201
202 return (*chip_id >= 0);
203 }
204
205 #elif defined(ANDROID) && !defined(__NOT_HAVE_DRM_H)
206
207 /* for i915 */
208 #include <i915_drm.h>
209 /* for radeon */
210 #include <radeon_drm.h>
211
212 int
213 loader_get_pci_id_for_fd(int fd, int *vendor_id, int *chip_id)
214 {
215 drmVersionPtr version;
216
217 *chip_id = -1;
218
219 version = drmGetVersion(fd);
220 if (!version) {
221 log_(_LOADER_WARNING, "MESA-LOADER: invalid drm fd\n");
222 return FALSE;
223 }
224 if (!version->name) {
225 log_(_LOADER_WARNING, "MESA-LOADER: unable to determine the driver name\n");
226 drmFreeVersion(version);
227 return FALSE;
228 }
229
230 if (strcmp(version->name, "i915") == 0) {
231 struct drm_i915_getparam gp;
232 int ret;
233
234 *vendor_id = 0x8086;
235
236 memset(&gp, 0, sizeof(gp));
237 gp.param = I915_PARAM_CHIPSET_ID;
238 gp.value = chip_id;
239 ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
240 if (ret) {
241 log_(_LOADER_WARNING, "MESA-LOADER: failed to get param for i915\n");
242 *chip_id = -1;
243 }
244 }
245 else if (strcmp(version->name, "radeon") == 0) {
246 struct drm_radeon_info info;
247 int ret;
248
249 *vendor_id = 0x1002;
250
251 memset(&info, 0, sizeof(info));
252 info.request = RADEON_INFO_DEVICE_ID;
253 info.value = (unsigned long) chip_id;
254 ret = drmCommandWriteRead(fd, DRM_RADEON_INFO, &info, sizeof(info));
255 if (ret) {
256 log_(_LOADER_WARNING, "MESA-LOADER: failed to get info for radeon\n");
257 *chip_id = -1;
258 }
259 }
260 else if (strcmp(version->name, "nouveau") == 0) {
261 *vendor_id = 0x10de;
262 /* not used */
263 *chip_id = 0;
264 }
265 else if (strcmp(version->name, "vmwgfx") == 0) {
266 *vendor_id = 0x15ad;
267 /* assume SVGA II */
268 *chip_id = 0x0405;
269 }
270
271 drmFreeVersion(version);
272
273 return (*chip_id >= 0);
274 }
275
276 #else
277
278 int
279 loader_get_pci_id_for_fd(int fd, int *vendor_id, int *chip_id)
280 {
281 return 0;
282 }
283
284 #endif
285
286
287 char *
288 loader_get_device_name_for_fd(int fd)
289 {
290 char *device_name = NULL;
291 #ifdef HAVE_LIBUDEV
292 struct udev *udev;
293 struct udev_device *device;
294 const char *const_device_name;
295 UDEV_SYMBOL(struct udev *, udev_new, (void));
296 UDEV_SYMBOL(const char *, udev_device_get_devnode,
297 (struct udev_device *));
298 UDEV_SYMBOL(struct udev_device *, udev_device_unref,
299 (struct udev_device *));
300 UDEV_SYMBOL(struct udev *, udev_unref, (struct udev *));
301
302 udev = udev_new();
303 device = udev_device_new_from_fd(udev, fd);
304 if (device == NULL)
305 return NULL;
306
307 const_device_name = udev_device_get_devnode(device);
308 if (!const_device_name)
309 goto out;
310 device_name = strdup(const_device_name);
311
312 out:
313 udev_device_unref(device);
314 udev_unref(udev);
315 #endif
316 return device_name;
317 }
318
319 char *
320 loader_get_driver_for_fd(int fd, unsigned driver_types)
321 {
322 int vendor_id, chip_id, i, j;
323 char *driver = NULL;
324
325 if (!driver_types)
326 driver_types = _LOADER_GALLIUM | _LOADER_DRI;
327
328 if (!loader_get_pci_id_for_fd(fd, &vendor_id, &chip_id)) {
329
330 #ifndef __NOT_HAVE_DRM_H
331 /* fallback to drmGetVersion(): */
332 drmVersionPtr version = drmGetVersion(fd);
333
334 if (!version) {
335 log_(_LOADER_WARNING, "failed to get driver name for fd %d\n", fd);
336 return NULL;
337 }
338
339 driver = strndup(version->name, version->name_len);
340 log_(_LOADER_INFO, "using driver %s for %d\n", driver, fd);
341
342 drmFreeVersion(version);
343 #endif
344
345 return driver;
346 }
347
348 for (i = 0; driver_map[i].driver; i++) {
349 if (vendor_id != driver_map[i].vendor_id)
350 continue;
351
352 if (!(driver_types & driver_map[i].driver_types))
353 continue;
354
355 if (driver_map[i].num_chips_ids == -1) {
356 driver = strdup(driver_map[i].driver);
357 goto out;
358 }
359
360 for (j = 0; j < driver_map[i].num_chips_ids; j++)
361 if (driver_map[i].chip_ids[j] == chip_id) {
362 driver = strdup(driver_map[i].driver);
363 goto out;
364 }
365 }
366
367 out:
368 log_(driver ? _LOADER_DEBUG : _LOADER_WARNING,
369 "pci id for fd %d: %04x:%04x, driver %s\n",
370 fd, vendor_id, chip_id, driver);
371 return driver;
372 }
373
374 void
375 loader_set_logger(void (*logger)(int level, const char *fmt, ...))
376 {
377 log_ = logger;
378 }