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