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