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