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