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
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:
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
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
26 * Rob Clark <robclark@freedesktop.org>
39 #include <sys/mkdev.h>
41 #ifdef MAJOR_IN_SYSMACROS
42 #include <sys/sysmacros.h>
49 #include "util/xmlconfig.h"
50 #include "util/xmlpool.h"
55 #include "pci_id_driver_map.h"
57 static void default_logger(int level
, const char *fmt
, ...)
59 if (level
<= _LOADER_WARNING
) {
62 vfprintf(stderr
, fmt
, args
);
67 static void (*log_
)(int level
, const char *fmt
, ...) = default_logger
;
70 loader_open_device(const char *device_name
)
74 fd
= open(device_name
, O_RDWR
| O_CLOEXEC
);
75 if (fd
== -1 && errno
== EINVAL
)
78 fd
= open(device_name
, O_RDWR
);
80 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
85 static char *loader_get_kernel_driver_name(int fd
)
89 drmVersionPtr version
= drmGetVersion(fd
);
92 log_(_LOADER_WARNING
, "failed to get driver name for fd %d\n", fd
);
96 driver
= strndup(version
->name
, version
->name_len
);
98 drmFreeVersion(version
);
105 #if defined(HAVE_LIBDRM)
107 loader_open_render_node(const char *name
)
109 drmDevicePtr
*devices
, device
;
110 int err
, render
= -ENOENT
, fd
;
113 err
= drmGetDevices2(0, NULL
, 0);
119 devices
= calloc(num
, sizeof(*devices
));
123 err
= drmGetDevices2(0, devices
, num
);
129 for (i
= 0; i
< num
; i
++) {
132 if ((device
->available_nodes
& (1 << DRM_NODE_RENDER
)) &&
133 (device
->bustype
== DRM_BUS_PLATFORM
)) {
134 drmVersionPtr version
;
136 fd
= open(device
->nodes
[DRM_NODE_RENDER
], O_RDWR
| O_CLOEXEC
);
140 version
= drmGetVersion(fd
);
146 if (strcmp(version
->name
, name
) != 0) {
147 drmFreeVersion(version
);
152 drmFreeVersion(version
);
158 drmFreeDevices(devices
, num
);
166 static const char __driConfigOptionsLoader
[] =
168 DRI_CONF_SECTION_INITIALIZATION
169 DRI_CONF_DEVICE_ID_PATH_TAG()
170 DRI_CONF_DRI_DRIVER()
174 static char *loader_get_dri_config_driver(int fd
)
176 driOptionCache defaultInitOptions
;
177 driOptionCache userInitOptions
;
178 char *dri_driver
= NULL
;
179 char *kernel_driver
= loader_get_kernel_driver_name(fd
);
181 driParseOptionInfo(&defaultInitOptions
, __driConfigOptionsLoader
);
182 driParseConfigFiles(&userInitOptions
, &defaultInitOptions
, 0,
183 "loader", kernel_driver
);
184 if (driCheckOption(&userInitOptions
, "dri_driver", DRI_STRING
)) {
185 char *opt
= driQueryOptionstr(&userInitOptions
, "dri_driver");
186 /* not an empty string */
188 dri_driver
= strdup(opt
);
190 driDestroyOptionCache(&userInitOptions
);
191 driDestroyOptionInfo(&defaultInitOptions
);
197 static char *loader_get_dri_config_device_id(void)
199 driOptionCache defaultInitOptions
;
200 driOptionCache userInitOptions
;
203 driParseOptionInfo(&defaultInitOptions
, __driConfigOptionsLoader
);
204 driParseConfigFiles(&userInitOptions
, &defaultInitOptions
, 0, "loader", NULL
);
205 if (driCheckOption(&userInitOptions
, "device_id", DRI_STRING
))
206 prime
= strdup(driQueryOptionstr(&userInitOptions
, "device_id"));
207 driDestroyOptionCache(&userInitOptions
);
208 driDestroyOptionInfo(&defaultInitOptions
);
214 static char *drm_construct_id_path_tag(drmDevicePtr device
)
218 if (device
->bustype
== DRM_BUS_PCI
) {
219 if (asprintf(&tag
, "pci-%04x_%02x_%02x_%1u",
220 device
->businfo
.pci
->domain
,
221 device
->businfo
.pci
->bus
,
222 device
->businfo
.pci
->dev
,
223 device
->businfo
.pci
->func
) < 0) {
226 } else if (device
->bustype
== DRM_BUS_PLATFORM
||
227 device
->bustype
== DRM_BUS_HOST1X
) {
228 char *fullname
, *name
, *address
;
230 if (device
->bustype
== DRM_BUS_PLATFORM
)
231 fullname
= device
->businfo
.platform
->fullname
;
233 fullname
= device
->businfo
.host1x
->fullname
;
235 name
= strrchr(fullname
, '/');
237 name
= strdup(fullname
);
239 name
= strdup(name
+ 1);
241 address
= strchr(name
, '@');
245 if (asprintf(&tag
, "platform-%s_%s", address
, name
) < 0)
248 if (asprintf(&tag
, "platform-%s", name
) < 0)
257 static bool drm_device_matches_tag(drmDevicePtr device
, const char *prime_tag
)
259 char *tag
= drm_construct_id_path_tag(device
);
265 ret
= strcmp(tag
, prime_tag
);
271 static char *drm_get_id_path_tag_for_fd(int fd
)
276 if (drmGetDevice2(fd
, 0, &device
) != 0)
279 tag
= drm_construct_id_path_tag(device
);
280 drmFreeDevice(&device
);
284 int loader_get_user_preferred_fd(int default_fd
, bool *different_device
)
286 /* Arbitrary "maximum" value of drm devices. */
287 #define MAX_DRM_DEVICES 32
288 const char *dri_prime
= getenv("DRI_PRIME");
289 char *default_tag
, *prime
= NULL
;
290 drmDevicePtr devices
[MAX_DRM_DEVICES
];
291 int i
, num_devices
, fd
;
295 prime
= strdup(dri_prime
);
298 prime
= loader_get_dri_config_device_id();
302 *different_device
= false;
306 default_tag
= drm_get_id_path_tag_for_fd(default_fd
);
307 if (default_tag
== NULL
)
310 num_devices
= drmGetDevices2(0, devices
, MAX_DRM_DEVICES
);
314 /* two format are supported:
315 * "1": choose any other card than the card used by default.
316 * id_path_tag: (for example "pci-0000_02_00_0") choose the card
317 * with this id_path_tag.
319 if (!strcmp(prime
,"1")) {
320 /* Hmm... detection for 2-7 seems to be broken. Oh well ...
321 * Pick the first render device that is not our own.
323 for (i
= 0; i
< num_devices
; i
++) {
324 if (devices
[i
]->available_nodes
& 1 << DRM_NODE_RENDER
&&
325 !drm_device_matches_tag(devices
[i
], default_tag
)) {
332 for (i
= 0; i
< num_devices
; i
++) {
333 if (devices
[i
]->available_nodes
& 1 << DRM_NODE_RENDER
&&
334 drm_device_matches_tag(devices
[i
], prime
)) {
343 drmFreeDevices(devices
, num_devices
);
347 fd
= loader_open_device(devices
[i
]->nodes
[DRM_NODE_RENDER
]);
348 drmFreeDevices(devices
, num_devices
);
354 *different_device
= !!strcmp(default_tag
, prime
);
361 *different_device
= false;
369 loader_open_render_node(const char *name
)
374 int loader_get_user_preferred_fd(int default_fd
, bool *different_device
)
376 *different_device
= false;
381 #if defined(HAVE_LIBDRM)
384 drm_get_pci_id_for_fd(int fd
, int *vendor_id
, int *chip_id
)
389 if (drmGetDevice2(fd
, 0, &device
) == 0) {
390 if (device
->bustype
== DRM_BUS_PCI
) {
391 *vendor_id
= device
->deviceinfo
.pci
->vendor_id
;
392 *chip_id
= device
->deviceinfo
.pci
->device_id
;
396 log_(_LOADER_DEBUG
, "MESA-LOADER: device is not located on the PCI bus\n");
399 drmFreeDevice(&device
);
402 log_(_LOADER_WARNING
, "MESA-LOADER: failed to retrieve device information\n");
412 loader_get_pci_id_for_fd(int fd
, int *vendor_id
, int *chip_id
)
415 if (drm_get_pci_id_for_fd(fd
, vendor_id
, chip_id
))
422 loader_get_device_name_for_fd(int fd
)
427 result
= drmGetDeviceNameFromFd2(fd
);
434 loader_get_driver_for_fd(int fd
)
436 int vendor_id
, chip_id
, i
, j
;
439 /* Allow an environment variable to force choosing a different driver
440 * binary. If that driver binary can't survive on this FD, that's the
441 * user's problem, but this allows vc4 simulator to run on an i965 host,
442 * and may be useful for some touch testing of i915 on an i965 host.
444 if (geteuid() == getuid()) {
445 driver
= getenv("MESA_LOADER_DRIVER_OVERRIDE");
447 return strdup(driver
);
450 #if defined(HAVE_LIBDRM) && defined(USE_DRICONF)
451 driver
= loader_get_dri_config_driver(fd
);
456 if (!loader_get_pci_id_for_fd(fd
, &vendor_id
, &chip_id
)) {
457 driver
= loader_get_kernel_driver_name(fd
);
459 log_(_LOADER_INFO
, "using driver %s for %d\n", driver
, fd
);
463 for (i
= 0; driver_map
[i
].driver
; i
++) {
464 if (vendor_id
!= driver_map
[i
].vendor_id
)
467 if (driver_map
[i
].predicate
&& !driver_map
[i
].predicate(fd
))
470 if (driver_map
[i
].num_chips_ids
== -1) {
471 driver
= strdup(driver_map
[i
].driver
);
475 for (j
= 0; j
< driver_map
[i
].num_chips_ids
; j
++)
476 if (driver_map
[i
].chip_ids
[j
] == chip_id
) {
477 driver
= strdup(driver_map
[i
].driver
);
483 log_(driver
? _LOADER_DEBUG
: _LOADER_WARNING
,
484 "pci id for fd %d: %04x:%04x, driver %s\n",
485 fd
, vendor_id
, chip_id
, driver
);
490 loader_set_logger(void (*logger
)(int level
, const char *fmt
, ...))
495 /* XXX: Local definition to avoid pulling the heavyweight GL/gl.h and
496 * GL/internal/dri_interface.h
499 #ifndef __DRI_DRIVER_GET_EXTENSIONS
500 #define __DRI_DRIVER_GET_EXTENSIONS "__driDriverGetExtensions"
504 loader_get_extensions_name(const char *driver_name
)
508 if (asprintf(&name
, "%s_%s", __DRI_DRIVER_GET_EXTENSIONS
, driver_name
) < 0)
511 const size_t len
= strlen(name
);
512 for (size_t i
= 0; i
< len
; i
++) {