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