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 #if defined(HAVE_LIBDRM)
87 static const char __driConfigOptionsLoader
[] =
89 DRI_CONF_SECTION_INITIALIZATION
90 DRI_CONF_DEVICE_ID_PATH_TAG()
94 static char *loader_get_dri_config_device_id(void)
96 driOptionCache defaultInitOptions
;
97 driOptionCache userInitOptions
;
100 driParseOptionInfo(&defaultInitOptions
, __driConfigOptionsLoader
);
101 driParseConfigFiles(&userInitOptions
, &defaultInitOptions
, 0, "loader");
102 if (driCheckOption(&userInitOptions
, "device_id", DRI_STRING
))
103 prime
= strdup(driQueryOptionstr(&userInitOptions
, "device_id"));
104 driDestroyOptionCache(&userInitOptions
);
105 driDestroyOptionInfo(&defaultInitOptions
);
111 static char *drm_construct_id_path_tag(drmDevicePtr device
)
113 #define PCI_ID_PATH_TAG_LENGTH sizeof("pci-xxxx_xx_xx_x")
116 if (device
->bustype
== DRM_BUS_PCI
) {
117 tag
= calloc(PCI_ID_PATH_TAG_LENGTH
, sizeof(char));
121 snprintf(tag
, PCI_ID_PATH_TAG_LENGTH
, "pci-%04x_%02x_%02x_%1u",
122 device
->businfo
.pci
->domain
, device
->businfo
.pci
->bus
,
123 device
->businfo
.pci
->dev
, device
->businfo
.pci
->func
);
128 static bool drm_device_matches_tag(drmDevicePtr device
, const char *prime_tag
)
130 char *tag
= drm_construct_id_path_tag(device
);
136 ret
= strcmp(tag
, prime_tag
);
142 static char *drm_get_id_path_tag_for_fd(int fd
)
147 if (drmGetDevice2(fd
, 0, &device
) != 0)
150 tag
= drm_construct_id_path_tag(device
);
151 drmFreeDevice(&device
);
155 int loader_get_user_preferred_fd(int default_fd
, bool *different_device
)
157 /* Arbitrary "maximum" value of drm devices. */
158 #define MAX_DRM_DEVICES 32
159 const char *dri_prime
= getenv("DRI_PRIME");
160 char *default_tag
, *prime
= NULL
;
161 drmDevicePtr devices
[MAX_DRM_DEVICES
];
162 int i
, num_devices
, fd
;
166 prime
= strdup(dri_prime
);
169 prime
= loader_get_dri_config_device_id();
173 *different_device
= false;
177 default_tag
= drm_get_id_path_tag_for_fd(default_fd
);
178 if (default_tag
== NULL
)
181 num_devices
= drmGetDevices2(0, devices
, MAX_DRM_DEVICES
);
185 /* two format are supported:
186 * "1": choose any other card than the card used by default.
187 * id_path_tag: (for example "pci-0000_02_00_0") choose the card
188 * with this id_path_tag.
190 if (!strcmp(prime
,"1")) {
191 /* Hmm... detection for 2-7 seems to be broken. Oh well ...
192 * Pick the first render device that is not our own.
194 for (i
= 0; i
< num_devices
; i
++) {
195 if (devices
[i
]->available_nodes
& 1 << DRM_NODE_RENDER
&&
196 !drm_device_matches_tag(devices
[i
], default_tag
)) {
203 for (i
= 0; i
< num_devices
; i
++) {
204 if (devices
[i
]->available_nodes
& 1 << DRM_NODE_RENDER
&&
205 drm_device_matches_tag(devices
[i
], prime
)) {
214 drmFreeDevices(devices
, num_devices
);
218 fd
= loader_open_device(devices
[i
]->nodes
[DRM_NODE_RENDER
]);
219 drmFreeDevices(devices
, num_devices
);
225 *different_device
= !!strcmp(default_tag
, prime
);
232 *different_device
= false;
239 int loader_get_user_preferred_fd(int default_fd
, bool *different_device
)
241 *different_device
= false;
246 #if defined(HAVE_LIBDRM)
249 drm_get_pci_id_for_fd(int fd
, int *vendor_id
, int *chip_id
)
254 if (drmGetDevice2(fd
, 0, &device
) == 0) {
255 if (device
->bustype
== DRM_BUS_PCI
) {
256 *vendor_id
= device
->deviceinfo
.pci
->vendor_id
;
257 *chip_id
= device
->deviceinfo
.pci
->device_id
;
261 log_(_LOADER_DEBUG
, "MESA-LOADER: device is not located on the PCI bus\n");
264 drmFreeDevice(&device
);
267 log_(_LOADER_WARNING
, "MESA-LOADER: failed to retrieve device information\n");
277 loader_get_pci_id_for_fd(int fd
, int *vendor_id
, int *chip_id
)
280 if (drm_get_pci_id_for_fd(fd
, vendor_id
, chip_id
))
287 loader_get_device_name_for_fd(int fd
)
292 result
= drmGetDeviceNameFromFd2(fd
);
299 loader_get_driver_for_fd(int fd
)
301 int vendor_id
, chip_id
, i
, j
;
304 /* Allow an environment variable to force choosing a different driver
305 * binary. If that driver binary can't survive on this FD, that's the
306 * user's problem, but this allows vc4 simulator to run on an i965 host,
307 * and may be useful for some touch testing of i915 on an i965 host.
309 if (geteuid() == getuid()) {
310 driver
= getenv("MESA_LOADER_DRIVER_OVERRIDE");
312 return strdup(driver
);
315 if (!loader_get_pci_id_for_fd(fd
, &vendor_id
, &chip_id
)) {
318 /* fallback to drmGetVersion(): */
319 drmVersionPtr version
= drmGetVersion(fd
);
322 log_(_LOADER_WARNING
, "failed to get driver name for fd %d\n", fd
);
326 driver
= strndup(version
->name
, version
->name_len
);
327 log_(_LOADER_INFO
, "using driver %s for %d\n", driver
, fd
);
329 drmFreeVersion(version
);
335 for (i
= 0; driver_map
[i
].driver
; i
++) {
336 if (vendor_id
!= driver_map
[i
].vendor_id
)
339 if (driver_map
[i
].predicate
&& !driver_map
[i
].predicate(fd
))
342 if (driver_map
[i
].num_chips_ids
== -1) {
343 driver
= strdup(driver_map
[i
].driver
);
347 for (j
= 0; j
< driver_map
[i
].num_chips_ids
; j
++)
348 if (driver_map
[i
].chip_ids
[j
] == chip_id
) {
349 driver
= strdup(driver_map
[i
].driver
);
355 log_(driver
? _LOADER_DEBUG
: _LOADER_WARNING
,
356 "pci id for fd %d: %04x:%04x, driver %s\n",
357 fd
, vendor_id
, chip_id
, driver
);
362 loader_set_logger(void (*logger
)(int level
, const char *fmt
, ...))
367 /* XXX: Local definition to avoid pulling the heavyweight GL/gl.h and
368 * GL/internal/dri_interface.h
371 #ifndef __DRI_DRIVER_GET_EXTENSIONS
372 #define __DRI_DRIVER_GET_EXTENSIONS "__driDriverGetExtensions"
376 loader_get_extensions_name(const char *driver_name
)
380 if (asprintf(&name
, "%s_%s", __DRI_DRIVER_GET_EXTENSIONS
, driver_name
) < 0)
383 const size_t len
= strlen(name
);
384 for (size_t i
= 0; i
< len
; i
++) {