041a59212be79efb1f607805cf0ae9719d5b9a67
[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 <GL/gl.h>
45 #include <GL/internal/dri_interface.h>
46 #include "loader.h"
47
48 #ifdef HAVE_LIBDRM
49 #include <xf86drm.h>
50 #ifdef USE_DRICONF
51 #include "util/xmlconfig.h"
52 #include "util/xmlpool.h"
53 #endif
54 #endif
55
56 #define __IS_LOADER
57 #include "pci_id_driver_map.h"
58
59 static void default_logger(int level, const char *fmt, ...)
60 {
61 if (level <= _LOADER_WARNING) {
62 va_list args;
63 va_start(args, fmt);
64 vfprintf(stderr, fmt, args);
65 va_end(args);
66 }
67 }
68
69 static void (*log_)(int level, const char *fmt, ...) = default_logger;
70
71 int
72 loader_open_device(const char *device_name)
73 {
74 int fd;
75 #ifdef O_CLOEXEC
76 fd = open(device_name, O_RDWR | O_CLOEXEC);
77 if (fd == -1 && errno == EINVAL)
78 #endif
79 {
80 fd = open(device_name, O_RDWR);
81 if (fd != -1)
82 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
83 }
84 return fd;
85 }
86
87 static char *loader_get_kernel_driver_name(int fd)
88 {
89 #if HAVE_LIBDRM
90 char *driver;
91 drmVersionPtr version = drmGetVersion(fd);
92
93 if (!version) {
94 log_(_LOADER_WARNING, "failed to get driver name for fd %d\n", fd);
95 return NULL;
96 }
97
98 driver = strndup(version->name, version->name_len);
99
100 drmFreeVersion(version);
101 return driver;
102 #else
103 return NULL;
104 #endif
105 }
106
107 #if defined(HAVE_LIBDRM)
108 int
109 loader_open_render_node(const char *name)
110 {
111 drmDevicePtr *devices, device;
112 int err, render = -ENOENT, fd;
113 unsigned int num, i;
114
115 err = drmGetDevices2(0, NULL, 0);
116 if (err < 0)
117 return err;
118
119 num = err;
120
121 devices = calloc(num, sizeof(*devices));
122 if (!devices)
123 return -ENOMEM;
124
125 err = drmGetDevices2(0, devices, num);
126 if (err < 0) {
127 render = err;
128 goto free;
129 }
130
131 for (i = 0; i < num; i++) {
132 device = devices[i];
133
134 if ((device->available_nodes & (1 << DRM_NODE_RENDER)) &&
135 (device->bustype == DRM_BUS_PLATFORM)) {
136 drmVersionPtr version;
137
138 fd = open(device->nodes[DRM_NODE_RENDER], O_RDWR | O_CLOEXEC);
139 if (fd < 0)
140 continue;
141
142 version = drmGetVersion(fd);
143 if (!version) {
144 close(fd);
145 continue;
146 }
147
148 if (strcmp(version->name, name) != 0) {
149 drmFreeVersion(version);
150 close(fd);
151 continue;
152 }
153
154 drmFreeVersion(version);
155 render = fd;
156 break;
157 }
158 }
159
160 drmFreeDevices(devices, num);
161
162 free:
163 free(devices);
164 return render;
165 }
166
167 #ifdef USE_DRICONF
168 static const char __driConfigOptionsLoader[] =
169 DRI_CONF_BEGIN
170 DRI_CONF_SECTION_INITIALIZATION
171 DRI_CONF_DEVICE_ID_PATH_TAG()
172 DRI_CONF_DRI_DRIVER()
173 DRI_CONF_SECTION_END
174 DRI_CONF_END;
175
176 static char *loader_get_dri_config_driver(int fd)
177 {
178 driOptionCache defaultInitOptions;
179 driOptionCache userInitOptions;
180 char *dri_driver = NULL;
181 char *kernel_driver = loader_get_kernel_driver_name(fd);
182
183 driParseOptionInfo(&defaultInitOptions, __driConfigOptionsLoader);
184 driParseConfigFiles(&userInitOptions, &defaultInitOptions, 0,
185 "loader", kernel_driver);
186 if (driCheckOption(&userInitOptions, "dri_driver", DRI_STRING)) {
187 char *opt = driQueryOptionstr(&userInitOptions, "dri_driver");
188 /* not an empty string */
189 if (*opt)
190 dri_driver = strdup(opt);
191 }
192 driDestroyOptionCache(&userInitOptions);
193 driDestroyOptionInfo(&defaultInitOptions);
194
195 free(kernel_driver);
196 return dri_driver;
197 }
198
199 static char *loader_get_dri_config_device_id(void)
200 {
201 driOptionCache defaultInitOptions;
202 driOptionCache userInitOptions;
203 char *prime = NULL;
204
205 driParseOptionInfo(&defaultInitOptions, __driConfigOptionsLoader);
206 driParseConfigFiles(&userInitOptions, &defaultInitOptions, 0, "loader", NULL);
207 if (driCheckOption(&userInitOptions, "device_id", DRI_STRING))
208 prime = strdup(driQueryOptionstr(&userInitOptions, "device_id"));
209 driDestroyOptionCache(&userInitOptions);
210 driDestroyOptionInfo(&defaultInitOptions);
211
212 return prime;
213 }
214 #endif
215
216 static char *drm_construct_id_path_tag(drmDevicePtr device)
217 {
218 char *tag = NULL;
219
220 if (device->bustype == DRM_BUS_PCI) {
221 if (asprintf(&tag, "pci-%04x_%02x_%02x_%1u",
222 device->businfo.pci->domain,
223 device->businfo.pci->bus,
224 device->businfo.pci->dev,
225 device->businfo.pci->func) < 0) {
226 return NULL;
227 }
228 } else if (device->bustype == DRM_BUS_PLATFORM ||
229 device->bustype == DRM_BUS_HOST1X) {
230 char *fullname, *name, *address;
231
232 if (device->bustype == DRM_BUS_PLATFORM)
233 fullname = device->businfo.platform->fullname;
234 else
235 fullname = device->businfo.host1x->fullname;
236
237 name = strrchr(fullname, '/');
238 if (!name)
239 name = strdup(fullname);
240 else
241 name = strdup(name + 1);
242
243 address = strchr(name, '@');
244 if (address) {
245 *address++ = '\0';
246
247 if (asprintf(&tag, "platform-%s_%s", address, name) < 0)
248 tag = NULL;
249 } else {
250 if (asprintf(&tag, "platform-%s", name) < 0)
251 tag = NULL;
252 }
253
254 free(name);
255 }
256 return tag;
257 }
258
259 static bool drm_device_matches_tag(drmDevicePtr device, const char *prime_tag)
260 {
261 char *tag = drm_construct_id_path_tag(device);
262 int ret;
263
264 if (tag == NULL)
265 return false;
266
267 ret = strcmp(tag, prime_tag);
268
269 free(tag);
270 return ret == 0;
271 }
272
273 static char *drm_get_id_path_tag_for_fd(int fd)
274 {
275 drmDevicePtr device;
276 char *tag;
277
278 if (drmGetDevice2(fd, 0, &device) != 0)
279 return NULL;
280
281 tag = drm_construct_id_path_tag(device);
282 drmFreeDevice(&device);
283 return tag;
284 }
285
286 int loader_get_user_preferred_fd(int default_fd, bool *different_device)
287 {
288 /* Arbitrary "maximum" value of drm devices. */
289 #define MAX_DRM_DEVICES 32
290 const char *dri_prime = getenv("DRI_PRIME");
291 char *default_tag, *prime = NULL;
292 drmDevicePtr devices[MAX_DRM_DEVICES];
293 int i, num_devices, fd;
294 bool found = false;
295
296 if (dri_prime)
297 prime = strdup(dri_prime);
298 #ifdef USE_DRICONF
299 else
300 prime = loader_get_dri_config_device_id();
301 #endif
302
303 if (prime == NULL) {
304 *different_device = false;
305 return default_fd;
306 }
307
308 default_tag = drm_get_id_path_tag_for_fd(default_fd);
309 if (default_tag == NULL)
310 goto err;
311
312 num_devices = drmGetDevices2(0, devices, MAX_DRM_DEVICES);
313 if (num_devices < 0)
314 goto err;
315
316 /* two format are supported:
317 * "1": choose any other card than the card used by default.
318 * id_path_tag: (for example "pci-0000_02_00_0") choose the card
319 * with this id_path_tag.
320 */
321 if (!strcmp(prime,"1")) {
322 /* Hmm... detection for 2-7 seems to be broken. Oh well ...
323 * Pick the first render device that is not our own.
324 */
325 for (i = 0; i < num_devices; i++) {
326 if (devices[i]->available_nodes & 1 << DRM_NODE_RENDER &&
327 !drm_device_matches_tag(devices[i], default_tag)) {
328
329 found = true;
330 break;
331 }
332 }
333 } else {
334 for (i = 0; i < num_devices; i++) {
335 if (devices[i]->available_nodes & 1 << DRM_NODE_RENDER &&
336 drm_device_matches_tag(devices[i], prime)) {
337
338 found = true;
339 break;
340 }
341 }
342 }
343
344 if (!found) {
345 drmFreeDevices(devices, num_devices);
346 goto err;
347 }
348
349 fd = loader_open_device(devices[i]->nodes[DRM_NODE_RENDER]);
350 drmFreeDevices(devices, num_devices);
351 if (fd < 0)
352 goto err;
353
354 close(default_fd);
355
356 *different_device = !!strcmp(default_tag, prime);
357
358 free(default_tag);
359 free(prime);
360 return fd;
361
362 err:
363 *different_device = false;
364
365 free(default_tag);
366 free(prime);
367 return default_fd;
368 }
369 #else
370 int
371 loader_open_render_node(const char *name)
372 {
373 return -1;
374 }
375
376 int loader_get_user_preferred_fd(int default_fd, bool *different_device)
377 {
378 *different_device = false;
379 return default_fd;
380 }
381 #endif
382
383 #if defined(HAVE_LIBDRM)
384
385 static int
386 drm_get_pci_id_for_fd(int fd, int *vendor_id, int *chip_id)
387 {
388 drmDevicePtr device;
389 int ret;
390
391 if (drmGetDevice2(fd, 0, &device) == 0) {
392 if (device->bustype == DRM_BUS_PCI) {
393 *vendor_id = device->deviceinfo.pci->vendor_id;
394 *chip_id = device->deviceinfo.pci->device_id;
395 ret = 1;
396 }
397 else {
398 log_(_LOADER_DEBUG, "MESA-LOADER: device is not located on the PCI bus\n");
399 ret = 0;
400 }
401 drmFreeDevice(&device);
402 }
403 else {
404 log_(_LOADER_WARNING, "MESA-LOADER: failed to retrieve device information\n");
405 ret = 0;
406 }
407
408 return ret;
409 }
410 #endif
411
412
413 int
414 loader_get_pci_id_for_fd(int fd, int *vendor_id, int *chip_id)
415 {
416 #if HAVE_LIBDRM
417 if (drm_get_pci_id_for_fd(fd, vendor_id, chip_id))
418 return 1;
419 #endif
420 return 0;
421 }
422
423 char *
424 loader_get_device_name_for_fd(int fd)
425 {
426 char *result = NULL;
427
428 #if HAVE_LIBDRM
429 result = drmGetDeviceNameFromFd2(fd);
430 #endif
431
432 return result;
433 }
434
435 char *
436 loader_get_driver_for_fd(int fd)
437 {
438 int vendor_id, chip_id, i, j;
439 char *driver = NULL;
440
441 /* Allow an environment variable to force choosing a different driver
442 * binary. If that driver binary can't survive on this FD, that's the
443 * user's problem, but this allows vc4 simulator to run on an i965 host,
444 * and may be useful for some touch testing of i915 on an i965 host.
445 */
446 if (geteuid() == getuid()) {
447 driver = getenv("MESA_LOADER_DRIVER_OVERRIDE");
448 if (driver)
449 return strdup(driver);
450 }
451
452 #if defined(HAVE_LIBDRM) && defined(USE_DRICONF)
453 driver = loader_get_dri_config_driver(fd);
454 if (driver)
455 return driver;
456 #endif
457
458 if (!loader_get_pci_id_for_fd(fd, &vendor_id, &chip_id)) {
459 driver = loader_get_kernel_driver_name(fd);
460 if (driver)
461 log_(_LOADER_INFO, "using driver %s for %d\n", driver, fd);
462 return driver;
463 }
464
465 for (i = 0; driver_map[i].driver; i++) {
466 if (vendor_id != driver_map[i].vendor_id)
467 continue;
468
469 if (driver_map[i].predicate && !driver_map[i].predicate(fd))
470 continue;
471
472 if (driver_map[i].num_chips_ids == -1) {
473 driver = strdup(driver_map[i].driver);
474 goto out;
475 }
476
477 for (j = 0; j < driver_map[i].num_chips_ids; j++)
478 if (driver_map[i].chip_ids[j] == chip_id) {
479 driver = strdup(driver_map[i].driver);
480 goto out;
481 }
482 }
483
484 out:
485 log_(driver ? _LOADER_DEBUG : _LOADER_WARNING,
486 "pci id for fd %d: %04x:%04x, driver %s\n",
487 fd, vendor_id, chip_id, driver);
488 return driver;
489 }
490
491 void
492 loader_set_logger(void (*logger)(int level, const char *fmt, ...))
493 {
494 log_ = logger;
495 }
496
497 char *
498 loader_get_extensions_name(const char *driver_name)
499 {
500 char *name = NULL;
501
502 if (asprintf(&name, "%s_%s", __DRI_DRIVER_GET_EXTENSIONS, driver_name) < 0)
503 return NULL;
504
505 const size_t len = strlen(name);
506 for (size_t i = 0; i < len; i++) {
507 if (name[i] == '-')
508 name[i] = '_';
509 }
510
511 return name;
512 }