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