~mmach/netext73/mesa-haswell

« back to all changes in this revision

Viewing changes to src/loader/loader.c

  • Committer: mmach
  • Date: 2022-09-22 19:56:13 UTC
  • Revision ID: netbit73@gmail.com-20220922195613-wtik9mmy20tmor0i
2022-09-22 21:17:09

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
#include "util/os_file.h"
51
 
 
52
 
#ifdef HAVE_LIBDRM
53
 
#include <xf86drm.h>
54
 
#define MAX_DRM_DEVICES 64
55
 
#ifdef USE_DRICONF
56
 
#include "util/xmlconfig.h"
57
 
#include "util/driconf.h"
58
 
#endif
59
 
#endif
60
 
 
61
 
#include "util/macros.h"
62
 
 
63
 
#define __IS_LOADER
64
 
#include "pci_id_driver_map.h"
65
 
 
66
 
/* For systems like Hurd */
67
 
#ifndef PATH_MAX
68
 
#define PATH_MAX 4096
69
 
#endif
70
 
 
71
 
static void default_logger(int level, const char *fmt, ...)
72
 
{
73
 
   if (level <= _LOADER_WARNING) {
74
 
      va_list args;
75
 
      va_start(args, fmt);
76
 
      vfprintf(stderr, fmt, args);
77
 
      va_end(args);
78
 
   }
79
 
}
80
 
 
81
 
static loader_logger *log_ = default_logger;
82
 
 
83
 
int
84
 
loader_open_device(const char *device_name)
85
 
{
86
 
   int fd;
87
 
#ifdef O_CLOEXEC
88
 
   fd = open(device_name, O_RDWR | O_CLOEXEC);
89
 
   if (fd == -1 && errno == EINVAL)
90
 
#endif
91
 
   {
92
 
      fd = open(device_name, O_RDWR);
93
 
      if (fd != -1)
94
 
         fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
95
 
   }
96
 
   if (fd == -1 && errno == EACCES) {
97
 
      log_(_LOADER_WARNING, "failed to open %s: %s\n",
98
 
           device_name, strerror(errno));
99
 
   }
100
 
   return fd;
101
 
}
102
 
 
103
 
static char *loader_get_kernel_driver_name(int fd)
104
 
{
105
 
#if HAVE_LIBDRM
106
 
   char *driver;
107
 
   drmVersionPtr version = drmGetVersion(fd);
108
 
 
109
 
   if (!version) {
110
 
      log_(_LOADER_WARNING, "failed to get driver name for fd %d\n", fd);
111
 
      return NULL;
112
 
   }
113
 
 
114
 
   driver = strndup(version->name, version->name_len);
115
 
   log_(driver ? _LOADER_DEBUG : _LOADER_WARNING, "using driver %s for %d\n",
116
 
        driver, fd);
117
 
 
118
 
   drmFreeVersion(version);
119
 
   return driver;
120
 
#else
121
 
   return NULL;
122
 
#endif
123
 
}
124
 
 
125
 
bool
126
 
is_kernel_i915(int fd)
127
 
{
128
 
   char *kernel_driver = loader_get_kernel_driver_name(fd);
129
 
   bool is_i915 = kernel_driver && strcmp(kernel_driver, "i915") == 0;
130
 
 
131
 
   free(kernel_driver);
132
 
   return is_i915;
133
 
}
134
 
 
135
 
#if defined(HAVE_LIBDRM)
136
 
int
137
 
loader_open_render_node(const char *name)
138
 
{
139
 
   drmDevicePtr devices[MAX_DRM_DEVICES], device;
140
 
   int i, num_devices, fd = -1;
141
 
 
142
 
   num_devices = drmGetDevices2(0, devices, MAX_DRM_DEVICES);
143
 
   if (num_devices <= 0)
144
 
      return -ENOENT;
145
 
 
146
 
   for (i = 0; i < num_devices; i++) {
147
 
      device = devices[i];
148
 
 
149
 
      if ((device->available_nodes & (1 << DRM_NODE_RENDER)) &&
150
 
          (device->bustype == DRM_BUS_PLATFORM)) {
151
 
         drmVersionPtr version;
152
 
 
153
 
         fd = loader_open_device(device->nodes[DRM_NODE_RENDER]);
154
 
         if (fd < 0)
155
 
            continue;
156
 
 
157
 
         version = drmGetVersion(fd);
158
 
         if (!version) {
159
 
            close(fd);
160
 
            continue;
161
 
         }
162
 
 
163
 
         if (strcmp(version->name, name) != 0) {
164
 
            drmFreeVersion(version);
165
 
            close(fd);
166
 
            continue;
167
 
         }
168
 
 
169
 
         drmFreeVersion(version);
170
 
         break;
171
 
      }
172
 
   }
173
 
   drmFreeDevices(devices, num_devices);
174
 
 
175
 
   if (i == num_devices)
176
 
      return -ENOENT;
177
 
 
178
 
   return fd;
179
 
}
180
 
 
181
 
char *
182
 
loader_get_render_node(dev_t device)
183
 
{
184
 
   char *render_node = NULL;
185
 
   drmDevicePtr dev_ptr;
186
 
 
187
 
   if (drmGetDeviceFromDevId(device, 0, &dev_ptr) < 0)
188
 
      return NULL;
189
 
 
190
 
   if (dev_ptr->available_nodes & (1 << DRM_NODE_RENDER)) {
191
 
      render_node = strdup(dev_ptr->nodes[DRM_NODE_RENDER]);
192
 
      if (!render_node)
193
 
         log_(_LOADER_DEBUG, "MESA-LOADER: failed to allocate memory for render node\n");
194
 
   }
195
 
 
196
 
   drmFreeDevice(&dev_ptr);
197
 
 
198
 
   return render_node;
199
 
}
200
 
 
201
 
#ifdef USE_DRICONF
202
 
static const driOptionDescription __driConfigOptionsLoader[] = {
203
 
    DRI_CONF_SECTION_INITIALIZATION
204
 
        DRI_CONF_DEVICE_ID_PATH_TAG()
205
 
        DRI_CONF_DRI_DRIVER()
206
 
    DRI_CONF_SECTION_END
207
 
};
208
 
 
209
 
static char *loader_get_dri_config_driver(int fd)
210
 
{
211
 
   driOptionCache defaultInitOptions;
212
 
   driOptionCache userInitOptions;
213
 
   char *dri_driver = NULL;
214
 
   char *kernel_driver = loader_get_kernel_driver_name(fd);
215
 
 
216
 
   driParseOptionInfo(&defaultInitOptions, __driConfigOptionsLoader,
217
 
                      ARRAY_SIZE(__driConfigOptionsLoader));
218
 
   driParseConfigFiles(&userInitOptions, &defaultInitOptions, 0,
219
 
                       "loader", kernel_driver, NULL, NULL, 0, NULL, 0);
220
 
   if (driCheckOption(&userInitOptions, "dri_driver", DRI_STRING)) {
221
 
      char *opt = driQueryOptionstr(&userInitOptions, "dri_driver");
222
 
      /* not an empty string */
223
 
      if (*opt)
224
 
         dri_driver = strdup(opt);
225
 
   }
226
 
   driDestroyOptionCache(&userInitOptions);
227
 
   driDestroyOptionInfo(&defaultInitOptions);
228
 
 
229
 
   free(kernel_driver);
230
 
   return dri_driver;
231
 
}
232
 
 
233
 
static char *loader_get_dri_config_device_id(void)
234
 
{
235
 
   driOptionCache defaultInitOptions;
236
 
   driOptionCache userInitOptions;
237
 
   char *prime = NULL;
238
 
 
239
 
   driParseOptionInfo(&defaultInitOptions, __driConfigOptionsLoader,
240
 
                      ARRAY_SIZE(__driConfigOptionsLoader));
241
 
   driParseConfigFiles(&userInitOptions, &defaultInitOptions, 0,
242
 
                       "loader", NULL, NULL, NULL, 0, NULL, 0);
243
 
   if (driCheckOption(&userInitOptions, "device_id", DRI_STRING))
244
 
      prime = strdup(driQueryOptionstr(&userInitOptions, "device_id"));
245
 
   driDestroyOptionCache(&userInitOptions);
246
 
   driDestroyOptionInfo(&defaultInitOptions);
247
 
 
248
 
   return prime;
249
 
}
250
 
#endif
251
 
 
252
 
static char *drm_construct_id_path_tag(drmDevicePtr device)
253
 
{
254
 
   char *tag = NULL;
255
 
 
256
 
   if (device->bustype == DRM_BUS_PCI) {
257
 
      if (asprintf(&tag, "pci-%04x_%02x_%02x_%1u",
258
 
                   device->businfo.pci->domain,
259
 
                   device->businfo.pci->bus,
260
 
                   device->businfo.pci->dev,
261
 
                   device->businfo.pci->func) < 0) {
262
 
         return NULL;
263
 
      }
264
 
   } else if (device->bustype == DRM_BUS_PLATFORM ||
265
 
              device->bustype == DRM_BUS_HOST1X) {
266
 
      char *fullname, *name, *address;
267
 
 
268
 
      if (device->bustype == DRM_BUS_PLATFORM)
269
 
         fullname = device->businfo.platform->fullname;
270
 
      else
271
 
         fullname = device->businfo.host1x->fullname;
272
 
 
273
 
      name = strrchr(fullname, '/');
274
 
      if (!name)
275
 
         name = strdup(fullname);
276
 
      else
277
 
         name = strdup(name + 1);
278
 
 
279
 
      address = strchr(name, '@');
280
 
      if (address) {
281
 
         *address++ = '\0';
282
 
 
283
 
         if (asprintf(&tag, "platform-%s_%s", address, name) < 0)
284
 
            tag = NULL;
285
 
      } else {
286
 
         if (asprintf(&tag, "platform-%s", name) < 0)
287
 
            tag = NULL;
288
 
      }
289
 
 
290
 
      free(name);
291
 
   }
292
 
   return tag;
293
 
}
294
 
 
295
 
static bool drm_device_matches_tag(drmDevicePtr device, const char *prime_tag)
296
 
{
297
 
   char *tag = drm_construct_id_path_tag(device);
298
 
   int ret;
299
 
 
300
 
   if (tag == NULL)
301
 
      return false;
302
 
 
303
 
   ret = strcmp(tag, prime_tag);
304
 
 
305
 
   free(tag);
306
 
   return ret == 0;
307
 
}
308
 
 
309
 
static char *drm_get_id_path_tag_for_fd(int fd)
310
 
{
311
 
   drmDevicePtr device;
312
 
   char *tag;
313
 
 
314
 
   if (drmGetDevice2(fd, 0, &device) != 0)
315
 
       return NULL;
316
 
 
317
 
   tag = drm_construct_id_path_tag(device);
318
 
   drmFreeDevice(&device);
319
 
   return tag;
320
 
}
321
 
 
322
 
int loader_get_user_preferred_fd(int default_fd, bool *different_device)
323
 
{
324
 
   const char *dri_prime = getenv("DRI_PRIME");
325
 
   char *default_tag, *prime = NULL;
326
 
   drmDevicePtr devices[MAX_DRM_DEVICES];
327
 
   int i, num_devices, fd = -1;
328
 
 
329
 
   if (dri_prime)
330
 
      prime = strdup(dri_prime);
331
 
#ifdef USE_DRICONF
332
 
   else
333
 
      prime = loader_get_dri_config_device_id();
334
 
#endif
335
 
 
336
 
   if (prime == NULL) {
337
 
      *different_device = false;
338
 
      return default_fd;
339
 
   }
340
 
 
341
 
   default_tag = drm_get_id_path_tag_for_fd(default_fd);
342
 
   if (default_tag == NULL)
343
 
      goto err;
344
 
 
345
 
   num_devices = drmGetDevices2(0, devices, MAX_DRM_DEVICES);
346
 
   if (num_devices <= 0)
347
 
      goto err;
348
 
 
349
 
   for (i = 0; i < num_devices; i++) {
350
 
      if (!(devices[i]->available_nodes & 1 << DRM_NODE_RENDER))
351
 
         continue;
352
 
 
353
 
      /* two formats of DRI_PRIME are supported:
354
 
       * "1": choose any other card than the card used by default.
355
 
       * id_path_tag: (for example "pci-0000_02_00_0") choose the card
356
 
       * with this id_path_tag.
357
 
       */
358
 
      if (!strcmp(prime,"1")) {
359
 
         if (drm_device_matches_tag(devices[i], default_tag))
360
 
            continue;
361
 
      } else {
362
 
         if (!drm_device_matches_tag(devices[i], prime))
363
 
            continue;
364
 
      }
365
 
 
366
 
      fd = loader_open_device(devices[i]->nodes[DRM_NODE_RENDER]);
367
 
      break;
368
 
   }
369
 
   drmFreeDevices(devices, num_devices);
370
 
 
371
 
   if (i == num_devices)
372
 
      goto err;
373
 
 
374
 
   if (fd < 0)
375
 
      goto err;
376
 
 
377
 
   close(default_fd);
378
 
 
379
 
   *different_device = !!strcmp(default_tag, prime);
380
 
 
381
 
   free(default_tag);
382
 
   free(prime);
383
 
   return fd;
384
 
 
385
 
 err:
386
 
   *different_device = false;
387
 
 
388
 
   free(default_tag);
389
 
   free(prime);
390
 
   return default_fd;
391
 
}
392
 
#else
393
 
int
394
 
loader_open_render_node(const char *name)
395
 
{
396
 
   return -1;
397
 
}
398
 
 
399
 
char *
400
 
loader_get_render_node(dev_t device)
401
 
{
402
 
   return NULL;
403
 
}
404
 
 
405
 
int loader_get_user_preferred_fd(int default_fd, bool *different_device)
406
 
{
407
 
   *different_device = false;
408
 
   return default_fd;
409
 
}
410
 
#endif
411
 
 
412
 
#if defined(HAVE_LIBDRM)
413
 
 
414
 
static bool
415
 
drm_get_pci_id_for_fd(int fd, int *vendor_id, int *chip_id)
416
 
{
417
 
   drmDevicePtr device;
418
 
 
419
 
   if (drmGetDevice2(fd, 0, &device) != 0) {
420
 
      log_(_LOADER_WARNING, "MESA-LOADER: failed to retrieve device information\n");
421
 
      return false;
422
 
   }
423
 
 
424
 
   if (device->bustype != DRM_BUS_PCI) {
425
 
      drmFreeDevice(&device);
426
 
      log_(_LOADER_DEBUG, "MESA-LOADER: device is not located on the PCI bus\n");
427
 
      return false;
428
 
   }
429
 
 
430
 
   *vendor_id = device->deviceinfo.pci->vendor_id;
431
 
   *chip_id = device->deviceinfo.pci->device_id;
432
 
   drmFreeDevice(&device);
433
 
   return true;
434
 
}
435
 
#endif
436
 
 
437
 
#ifdef __linux__
438
 
static int loader_get_linux_pci_field(int maj, int min, const char *field)
439
 
{
440
 
   char path[PATH_MAX + 1];
441
 
   snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device/%s", maj, min, field);
442
 
 
443
 
   char *field_str = os_read_file(path, NULL);
444
 
   if (!field_str) {
445
 
      /* Probably non-PCI device. */
446
 
      return 0;
447
 
   }
448
 
 
449
 
   int value = (int)strtoll(field_str, NULL, 16);
450
 
   free(field_str);
451
 
 
452
 
   return value;
453
 
}
454
 
 
455
 
static bool
456
 
loader_get_linux_pci_id_for_fd(int fd, int *vendor_id, int *chip_id)
457
 
{
458
 
   struct stat sbuf;
459
 
   if (fstat(fd, &sbuf) != 0) {
460
 
      log_(_LOADER_DEBUG, "MESA-LOADER: failed to fstat fd\n");
461
 
      return false;
462
 
   }
463
 
 
464
 
   int maj = major(sbuf.st_rdev);
465
 
   int min = minor(sbuf.st_rdev);
466
 
 
467
 
   *vendor_id = loader_get_linux_pci_field(maj, min, "vendor");
468
 
   *chip_id = loader_get_linux_pci_field(maj, min, "device");
469
 
 
470
 
   return *vendor_id && *chip_id;
471
 
}
472
 
#endif /* __linux__ */
473
 
 
474
 
bool
475
 
loader_get_pci_id_for_fd(int fd, int *vendor_id, int *chip_id)
476
 
{
477
 
#ifdef __linux__
478
 
   /* Implementation without causing full enumeration of DRM devices. */
479
 
   if (loader_get_linux_pci_id_for_fd(fd, vendor_id, chip_id))
480
 
      return true;
481
 
#endif
482
 
 
483
 
#if HAVE_LIBDRM
484
 
   return drm_get_pci_id_for_fd(fd, vendor_id, chip_id);
485
 
#endif
486
 
   return false;
487
 
}
488
 
 
489
 
char *
490
 
loader_get_device_name_for_fd(int fd)
491
 
{
492
 
   char *result = NULL;
493
 
 
494
 
#if HAVE_LIBDRM
495
 
   result = drmGetDeviceNameFromFd2(fd);
496
 
#endif
497
 
 
498
 
   return result;
499
 
}
500
 
 
501
 
static char *
502
 
loader_get_pci_driver(int fd)
503
 
{
504
 
   int vendor_id, chip_id, i, j;
505
 
   char *driver = NULL;
506
 
 
507
 
   if (!loader_get_pci_id_for_fd(fd, &vendor_id, &chip_id))
508
 
      return NULL;
509
 
 
510
 
   for (i = 0; i < ARRAY_SIZE(driver_map); i++) {
511
 
      if (vendor_id != driver_map[i].vendor_id)
512
 
         continue;
513
 
 
514
 
      if (driver_map[i].predicate && !driver_map[i].predicate(fd))
515
 
         continue;
516
 
 
517
 
      if (driver_map[i].num_chips_ids == -1) {
518
 
         driver = strdup(driver_map[i].driver);
519
 
         goto out;
520
 
      }
521
 
 
522
 
      for (j = 0; j < driver_map[i].num_chips_ids; j++)
523
 
         if (driver_map[i].chip_ids[j] == chip_id) {
524
 
            driver = strdup(driver_map[i].driver);
525
 
            goto out;
526
 
         }
527
 
   }
528
 
 
529
 
out:
530
 
   log_(driver ? _LOADER_DEBUG : _LOADER_WARNING,
531
 
         "pci id for fd %d: %04x:%04x, driver %s\n",
532
 
         fd, vendor_id, chip_id, driver);
533
 
   return driver;
534
 
}
535
 
 
536
 
char *
537
 
loader_get_driver_for_fd(int fd)
538
 
{
539
 
   char *driver;
540
 
 
541
 
   /* Allow an environment variable to force choosing a different driver
542
 
    * binary.  If that driver binary can't survive on this FD, that's the
543
 
    * user's problem, but this allows vc4 simulator to run on an i965 host,
544
 
    * and may be useful for some touch testing of i915 on an i965 host.
545
 
    */
546
 
   if (geteuid() == getuid()) {
547
 
      driver = getenv("MESA_LOADER_DRIVER_OVERRIDE");
548
 
      if (driver)
549
 
         return strdup(driver);
550
 
   }
551
 
 
552
 
#if defined(HAVE_LIBDRM) && defined(USE_DRICONF)
553
 
   driver = loader_get_dri_config_driver(fd);
554
 
   if (driver)
555
 
      return driver;
556
 
#endif
557
 
 
558
 
   driver = loader_get_pci_driver(fd);
559
 
   if (!driver)
560
 
      driver = loader_get_kernel_driver_name(fd);
561
 
 
562
 
   return driver;
563
 
}
564
 
 
565
 
void
566
 
loader_set_logger(loader_logger *logger)
567
 
{
568
 
   log_ = logger;
569
 
}
570
 
 
571
 
char *
572
 
loader_get_extensions_name(const char *driver_name)
573
 
{
574
 
   char *name = NULL;
575
 
 
576
 
   if (asprintf(&name, "%s_%s", __DRI_DRIVER_GET_EXTENSIONS, driver_name) < 0)
577
 
      return NULL;
578
 
 
579
 
   const size_t len = strlen(name);
580
 
   for (size_t i = 0; i < len; i++) {
581
 
      if (name[i] == '-')
582
 
         name[i] = '_';
583
 
   }
584
 
 
585
 
   return name;
586
 
}
587
 
 
588
 
/**
589
 
 * Opens a driver or backend using its name, returning the library handle.
590
 
 *
591
 
 * \param driverName - a name like "i965", "radeon", "nouveau", etc.
592
 
 * \param lib_suffix - a suffix to append to the driver name to generate the
593
 
 * full library name.
594
 
 * \param search_path_vars - NULL-terminated list of env vars that can be used
595
 
 * \param default_search_path - a colon-separted list of directories used if
596
 
 * search_path_vars is NULL or none of the vars are set in the environment.
597
 
 * \param warn_on_fail - Log a warning if the driver is not found.
598
 
 */
599
 
void *
600
 
loader_open_driver_lib(const char *driver_name,
601
 
                       const char *lib_suffix,
602
 
                       const char **search_path_vars,
603
 
                       const char *default_search_path,
604
 
                       bool warn_on_fail)
605
 
{
606
 
   char path[PATH_MAX];
607
 
   const char *search_paths, *next, *end;
608
 
 
609
 
   search_paths = NULL;
610
 
   if (geteuid() == getuid() && search_path_vars) {
611
 
      for (int i = 0; search_path_vars[i] != NULL; i++) {
612
 
         search_paths = getenv(search_path_vars[i]);
613
 
         if (search_paths)
614
 
            break;
615
 
      }
616
 
   }
617
 
   if (search_paths == NULL)
618
 
      search_paths = default_search_path;
619
 
 
620
 
   void *driver = NULL;
621
 
   const char *dl_error = NULL;
622
 
   end = search_paths + strlen(search_paths);
623
 
   for (const char *p = search_paths; p < end; p = next + 1) {
624
 
      int len;
625
 
      next = strchr(p, ':');
626
 
      if (next == NULL)
627
 
         next = end;
628
 
 
629
 
      len = next - p;
630
 
#if USE_ELF_TLS
631
 
      snprintf(path, sizeof(path), "%.*s/tls/%s%s.so", len,
632
 
               p, driver_name, lib_suffix);
633
 
      driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
634
 
#endif
635
 
      if (driver == NULL) {
636
 
         snprintf(path, sizeof(path), "%.*s/%s%s.so", len,
637
 
                  p, driver_name, lib_suffix);
638
 
         driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
639
 
         if (driver == NULL) {
640
 
            dl_error = dlerror();
641
 
            log_(_LOADER_DEBUG, "MESA-LOADER: failed to open %s: %s\n",
642
 
                 path, dl_error);
643
 
         }
644
 
      }
645
 
      /* not need continue to loop all paths once the driver is found */
646
 
      if (driver != NULL)
647
 
         break;
648
 
   }
649
 
 
650
 
   if (driver == NULL) {
651
 
      if (warn_on_fail) {
652
 
         log_(_LOADER_WARNING,
653
 
              "MESA-LOADER: failed to open %s: %s (search paths %s, suffix %s)\n",
654
 
              driver_name, dl_error, search_paths, lib_suffix);
655
 
      }
656
 
      return NULL;
657
 
   }
658
 
 
659
 
   log_(_LOADER_DEBUG, "MESA-LOADER: dlopen(%s)\n", path);
660
 
 
661
 
   return driver;
662
 
}
663
 
 
664
 
/**
665
 
 * Opens a DRI driver using its driver name, returning the __DRIextension
666
 
 * entrypoints.
667
 
 *
668
 
 * \param driverName - a name like "i965", "radeon", "nouveau", etc.
669
 
 * \param out_driver - Address where the dlopen() return value will be stored.
670
 
 * \param search_path_vars - NULL-terminated list of env vars that can be used
671
 
 * to override the DEFAULT_DRIVER_DIR search path.
672
 
 */
673
 
const struct __DRIextensionRec **
674
 
loader_open_driver(const char *driver_name,
675
 
                   void **out_driver_handle,
676
 
                   const char **search_path_vars)
677
 
{
678
 
   char *get_extensions_name;
679
 
   const struct __DRIextensionRec **extensions = NULL;
680
 
   const struct __DRIextensionRec **(*get_extensions)(void);
681
 
   void *driver = loader_open_driver_lib(driver_name, "_dri", search_path_vars,
682
 
                                         DEFAULT_DRIVER_DIR, true);
683
 
 
684
 
   if (!driver)
685
 
      goto failed;
686
 
 
687
 
   get_extensions_name = loader_get_extensions_name(driver_name);
688
 
   if (get_extensions_name) {
689
 
      get_extensions = dlsym(driver, get_extensions_name);
690
 
      if (get_extensions) {
691
 
         extensions = get_extensions();
692
 
      } else {
693
 
         log_(_LOADER_DEBUG, "MESA-LOADER: driver does not expose %s(): %s\n",
694
 
              get_extensions_name, dlerror());
695
 
      }
696
 
      free(get_extensions_name);
697
 
   }
698
 
 
699
 
   if (!extensions)
700
 
      extensions = dlsym(driver, __DRI_DRIVER_EXTENSIONS);
701
 
   if (extensions == NULL) {
702
 
      log_(_LOADER_WARNING,
703
 
           "MESA-LOADER: driver exports no extensions (%s)\n", dlerror());
704
 
      dlclose(driver);
705
 
      driver = NULL;
706
 
   }
707
 
 
708
 
failed:
709
 
   *out_driver_handle = driver;
710
 
   return extensions;
711
 
}