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