loader: add dri_driver option to override dri driver to load
[mesa.git] / src / loader / loader.c
1 /*
2 * Copyright (C) 2013 Rob Clark <robclark@freedesktop.org>
3 * Copyright (C) 2014-2016 Emil Velikov <emil.l.velikov@gmail.com>
4 * Copyright (C) 2016 Intel Corporation
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * Authors:
26 * Rob Clark <robclark@freedesktop.org>
27 */
28
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <sys/stat.h>
32 #include <stdarg.h>
33 #include <stdio.h>
34 #include <stdbool.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include <stdlib.h>
38 #ifdef MAJOR_IN_MKDEV
39 #include <sys/mkdev.h>
40 #endif
41 #ifdef MAJOR_IN_SYSMACROS
42 #include <sys/sysmacros.h>
43 #endif
44 #include "loader.h"
45
46 #ifdef HAVE_LIBDRM
47 #include <xf86drm.h>
48 #ifdef USE_DRICONF
49 #include "util/xmlconfig.h"
50 #include "util/xmlpool.h"
51 #endif
52 #endif
53
54 #define __IS_LOADER
55 #include "pci_id_driver_map.h"
56
57 static void default_logger(int level, const char *fmt, ...)
58 {
59 if (level <= _LOADER_WARNING) {
60 va_list args;
61 va_start(args, fmt);
62 vfprintf(stderr, fmt, args);
63 va_end(args);
64 }
65 }
66
67 static void (*log_)(int level, const char *fmt, ...) = default_logger;
68
69 int
70 loader_open_device(const char *device_name)
71 {
72 int fd;
73 #ifdef O_CLOEXEC
74 fd = open(device_name, O_RDWR | O_CLOEXEC);
75 if (fd == -1 && errno == EINVAL)
76 #endif
77 {
78 fd = open(device_name, O_RDWR);
79 if (fd != -1)
80 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
81 }
82 return fd;
83 }
84
85 static char *loader_get_kernel_driver_name(int fd)
86 {
87 #if HAVE_LIBDRM
88 char *driver;
89 drmVersionPtr version = drmGetVersion(fd);
90
91 if (!version) {
92 log_(_LOADER_WARNING, "failed to get driver name for fd %d\n", fd);
93 return NULL;
94 }
95
96 driver = strndup(version->name, version->name_len);
97
98 drmFreeVersion(version);
99 return driver;
100 #else
101 return NULL;
102 #endif
103 }
104
105 #if defined(HAVE_LIBDRM)
106 #ifdef USE_DRICONF
107 static const char __driConfigOptionsLoader[] =
108 DRI_CONF_BEGIN
109 DRI_CONF_SECTION_INITIALIZATION
110 DRI_CONF_DEVICE_ID_PATH_TAG()
111 DRI_CONF_DRI_DRIVER()
112 DRI_CONF_SECTION_END
113 DRI_CONF_END;
114
115 static char *loader_get_dri_config_driver(int fd)
116 {
117 driOptionCache defaultInitOptions;
118 driOptionCache userInitOptions;
119 char *dri_driver = NULL;
120 char *kernel_driver = loader_get_kernel_driver_name(fd);
121
122 driParseOptionInfo(&defaultInitOptions, __driConfigOptionsLoader);
123 driParseConfigFiles(&userInitOptions, &defaultInitOptions, 0,
124 "loader", kernel_driver);
125 if (driCheckOption(&userInitOptions, "dri_driver", DRI_STRING)) {
126 char *opt = driQueryOptionstr(&userInitOptions, "dri_driver");
127 /* not an empty string */
128 if (*opt)
129 dri_driver = strdup(opt);
130 }
131 driDestroyOptionCache(&userInitOptions);
132 driDestroyOptionInfo(&defaultInitOptions);
133
134 free(kernel_driver);
135 return dri_driver;
136 }
137
138 static char *loader_get_dri_config_device_id(void)
139 {
140 driOptionCache defaultInitOptions;
141 driOptionCache userInitOptions;
142 char *prime = NULL;
143
144 driParseOptionInfo(&defaultInitOptions, __driConfigOptionsLoader);
145 driParseConfigFiles(&userInitOptions, &defaultInitOptions, 0, "loader", NULL);
146 if (driCheckOption(&userInitOptions, "device_id", DRI_STRING))
147 prime = strdup(driQueryOptionstr(&userInitOptions, "device_id"));
148 driDestroyOptionCache(&userInitOptions);
149 driDestroyOptionInfo(&defaultInitOptions);
150
151 return prime;
152 }
153 #endif
154
155 static char *drm_construct_id_path_tag(drmDevicePtr device)
156 {
157 char *tag = NULL;
158
159 if (device->bustype == DRM_BUS_PCI) {
160 if (asprintf(&tag, "pci-%04x_%02x_%02x_%1u",
161 device->businfo.pci->domain,
162 device->businfo.pci->bus,
163 device->businfo.pci->dev,
164 device->businfo.pci->func) < 0) {
165 return NULL;
166 }
167 } else if (device->bustype == DRM_BUS_PLATFORM ||
168 device->bustype == DRM_BUS_HOST1X) {
169 char *fullname, *name, *address;
170
171 if (device->bustype == DRM_BUS_PLATFORM)
172 fullname = device->businfo.platform->fullname;
173 else
174 fullname = device->businfo.host1x->fullname;
175
176 name = strrchr(fullname, '/');
177 if (!name)
178 name = strdup(fullname);
179 else
180 name = strdup(name + 1);
181
182 address = strchr(name, '@');
183 if (address) {
184 *address++ = '\0';
185
186 if (asprintf(&tag, "platform-%s_%s", address, name) < 0)
187 tag = NULL;
188 } else {
189 if (asprintf(&tag, "platform-%s", name) < 0)
190 tag = NULL;
191 }
192
193 free(name);
194 }
195 return tag;
196 }
197
198 static bool drm_device_matches_tag(drmDevicePtr device, const char *prime_tag)
199 {
200 char *tag = drm_construct_id_path_tag(device);
201 int ret;
202
203 if (tag == NULL)
204 return false;
205
206 ret = strcmp(tag, prime_tag);
207
208 free(tag);
209 return ret == 0;
210 }
211
212 static char *drm_get_id_path_tag_for_fd(int fd)
213 {
214 drmDevicePtr device;
215 char *tag;
216
217 if (drmGetDevice2(fd, 0, &device) != 0)
218 return NULL;
219
220 tag = drm_construct_id_path_tag(device);
221 drmFreeDevice(&device);
222 return tag;
223 }
224
225 int loader_get_user_preferred_fd(int default_fd, bool *different_device)
226 {
227 /* Arbitrary "maximum" value of drm devices. */
228 #define MAX_DRM_DEVICES 32
229 const char *dri_prime = getenv("DRI_PRIME");
230 char *default_tag, *prime = NULL;
231 drmDevicePtr devices[MAX_DRM_DEVICES];
232 int i, num_devices, fd;
233 bool found = false;
234
235 if (dri_prime)
236 prime = strdup(dri_prime);
237 #ifdef USE_DRICONF
238 else
239 prime = loader_get_dri_config_device_id();
240 #endif
241
242 if (prime == NULL) {
243 *different_device = false;
244 return default_fd;
245 }
246
247 default_tag = drm_get_id_path_tag_for_fd(default_fd);
248 if (default_tag == NULL)
249 goto err;
250
251 num_devices = drmGetDevices2(0, devices, MAX_DRM_DEVICES);
252 if (num_devices < 0)
253 goto err;
254
255 /* two format are supported:
256 * "1": choose any other card than the card used by default.
257 * id_path_tag: (for example "pci-0000_02_00_0") choose the card
258 * with this id_path_tag.
259 */
260 if (!strcmp(prime,"1")) {
261 /* Hmm... detection for 2-7 seems to be broken. Oh well ...
262 * Pick the first render device that is not our own.
263 */
264 for (i = 0; i < num_devices; i++) {
265 if (devices[i]->available_nodes & 1 << DRM_NODE_RENDER &&
266 !drm_device_matches_tag(devices[i], default_tag)) {
267
268 found = true;
269 break;
270 }
271 }
272 } else {
273 for (i = 0; i < num_devices; i++) {
274 if (devices[i]->available_nodes & 1 << DRM_NODE_RENDER &&
275 drm_device_matches_tag(devices[i], prime)) {
276
277 found = true;
278 break;
279 }
280 }
281 }
282
283 if (!found) {
284 drmFreeDevices(devices, num_devices);
285 goto err;
286 }
287
288 fd = loader_open_device(devices[i]->nodes[DRM_NODE_RENDER]);
289 drmFreeDevices(devices, num_devices);
290 if (fd < 0)
291 goto err;
292
293 close(default_fd);
294
295 *different_device = !!strcmp(default_tag, prime);
296
297 free(default_tag);
298 free(prime);
299 return fd;
300
301 err:
302 *different_device = false;
303
304 free(default_tag);
305 free(prime);
306 return default_fd;
307 }
308 #else
309 int loader_get_user_preferred_fd(int default_fd, bool *different_device)
310 {
311 *different_device = false;
312 return default_fd;
313 }
314 #endif
315
316 #if defined(HAVE_LIBDRM)
317
318 static int
319 drm_get_pci_id_for_fd(int fd, int *vendor_id, int *chip_id)
320 {
321 drmDevicePtr device;
322 int ret;
323
324 if (drmGetDevice2(fd, 0, &device) == 0) {
325 if (device->bustype == DRM_BUS_PCI) {
326 *vendor_id = device->deviceinfo.pci->vendor_id;
327 *chip_id = device->deviceinfo.pci->device_id;
328 ret = 1;
329 }
330 else {
331 log_(_LOADER_DEBUG, "MESA-LOADER: device is not located on the PCI bus\n");
332 ret = 0;
333 }
334 drmFreeDevice(&device);
335 }
336 else {
337 log_(_LOADER_WARNING, "MESA-LOADER: failed to retrieve device information\n");
338 ret = 0;
339 }
340
341 return ret;
342 }
343 #endif
344
345
346 int
347 loader_get_pci_id_for_fd(int fd, int *vendor_id, int *chip_id)
348 {
349 #if HAVE_LIBDRM
350 if (drm_get_pci_id_for_fd(fd, vendor_id, chip_id))
351 return 1;
352 #endif
353 return 0;
354 }
355
356 char *
357 loader_get_device_name_for_fd(int fd)
358 {
359 char *result = NULL;
360
361 #if HAVE_LIBDRM
362 result = drmGetDeviceNameFromFd2(fd);
363 #endif
364
365 return result;
366 }
367
368 char *
369 loader_get_driver_for_fd(int fd)
370 {
371 int vendor_id, chip_id, i, j;
372 char *driver = NULL;
373
374 /* Allow an environment variable to force choosing a different driver
375 * binary. If that driver binary can't survive on this FD, that's the
376 * user's problem, but this allows vc4 simulator to run on an i965 host,
377 * and may be useful for some touch testing of i915 on an i965 host.
378 */
379 if (geteuid() == getuid()) {
380 driver = getenv("MESA_LOADER_DRIVER_OVERRIDE");
381 if (driver)
382 return strdup(driver);
383 }
384
385 #if defined(HAVE_LIBDRM) && defined(USE_DRICONF)
386 driver = loader_get_dri_config_driver(fd);
387 if (driver)
388 return driver;
389 #endif
390
391 if (!loader_get_pci_id_for_fd(fd, &vendor_id, &chip_id)) {
392 driver = loader_get_kernel_driver_name(fd);
393 if (driver)
394 log_(_LOADER_INFO, "using driver %s for %d\n", driver, fd);
395 return driver;
396 }
397
398 for (i = 0; driver_map[i].driver; i++) {
399 if (vendor_id != driver_map[i].vendor_id)
400 continue;
401
402 if (driver_map[i].predicate && !driver_map[i].predicate(fd))
403 continue;
404
405 if (driver_map[i].num_chips_ids == -1) {
406 driver = strdup(driver_map[i].driver);
407 goto out;
408 }
409
410 for (j = 0; j < driver_map[i].num_chips_ids; j++)
411 if (driver_map[i].chip_ids[j] == chip_id) {
412 driver = strdup(driver_map[i].driver);
413 goto out;
414 }
415 }
416
417 out:
418 log_(driver ? _LOADER_DEBUG : _LOADER_WARNING,
419 "pci id for fd %d: %04x:%04x, driver %s\n",
420 fd, vendor_id, chip_id, driver);
421 return driver;
422 }
423
424 void
425 loader_set_logger(void (*logger)(int level, const char *fmt, ...))
426 {
427 log_ = logger;
428 }
429
430 /* XXX: Local definition to avoid pulling the heavyweight GL/gl.h and
431 * GL/internal/dri_interface.h
432 */
433
434 #ifndef __DRI_DRIVER_GET_EXTENSIONS
435 #define __DRI_DRIVER_GET_EXTENSIONS "__driDriverGetExtensions"
436 #endif
437
438 char *
439 loader_get_extensions_name(const char *driver_name)
440 {
441 char *name = NULL;
442
443 if (asprintf(&name, "%s_%s", __DRI_DRIVER_GET_EXTENSIONS, driver_name) < 0)
444 return NULL;
445
446 const size_t len = strlen(name);
447 for (size_t i = 0; i < len; i++) {
448 if (name[i] == '-')
449 name[i] = '_';
450 }
451
452 return name;
453 }