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