Debian Codegraph @

src/egl/main/egldevice.c

raw
1 /**************************************************************************
2 *
3 * Copyright 2015, 2018 Collabora
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #ifdef HAVE_LIBDRM
29 #include <xf86drm.h>
30 #endif
31 #include "util/compiler.h"
32 #include "util/macros.h"
33 #include "util/u_debug.h"
34
35 #include "eglcurrent.h"
36 #include "egldevice.h"
37 #include "eglglobals.h"
38 #include "egllog.h"
39 #include "egltypedefs.h"
40 #include "egldriver.h"
41
42 struct _egl_device {
43 _EGLDevice *Next;
44
45 const char *extensions;
46
47 EGLBoolean MESA_device_software;
48 EGLBoolean EXT_device_drm;
49 EGLBoolean EXT_device_drm_render_node;
50 EGLBoolean EXT_device_query_name;
51 EGLBoolean EXT_device_persistent_id;
52
53 #ifdef HAVE_LIBDRM
54 drmDevicePtr device;
55 #endif
56
57 /* Cache for EGL_EXT_device_query_name and EXT_device_persistent_id. */
58 struct egl_device_info device_info;
59 };
60
61 void
62 _eglFiniDevice(void)
63 {
64 _EGLDevice *dev_list, *dev;
65
66 /* atexit function is called with global mutex locked */
67
68 dev_list = _eglGlobal.DeviceList;
69
70 /* The first device is static allocated SW device */
71 assert(dev_list);
72 assert(_eglDeviceSupports(dev_list, _EGL_DEVICE_SOFTWARE));
73 dev_list = dev_list->Next;
74
75 while (dev_list) {
76 /* pop list head */
77 dev = dev_list;
78 dev_list = dev_list->Next;
79
80 #ifdef HAVE_LIBDRM
81 assert(_eglDeviceSupports(dev, _EGL_DEVICE_DRM));
82 drmFreeDevice(&dev->device);
83 #endif
84 free(dev->device_info.vendor_name);
85 free(dev->device_info.renderer_name);
86 free(dev->device_info.driver_name);
87 free(dev);
88 }
89
90 _eglGlobal.DeviceList = NULL;
91 }
92
93 EGLBoolean
94 _eglCheckDeviceHandle(EGLDeviceEXT device)
95 {
96 _EGLDevice *cur;
97
98 simple_mtx_lock(_eglGlobal.Mutex);
99 cur = _eglGlobal.DeviceList;
100 while (cur) {
101 if (cur == (_EGLDevice *)device)
102 break;
103 cur = cur->Next;
104 }
105 simple_mtx_unlock(_eglGlobal.Mutex);
106 return (cur != NULL);
107 }
108
109 _EGLDevice _eglSoftwareDevice = {
110 /* TODO: EGL_EXT_device_drm support for KMS + llvmpipe */
111 .extensions = "EGL_MESA_device_software EGL_EXT_device_drm_render_node",
112 .MESA_device_software = EGL_TRUE,
113 .EXT_device_drm_render_node = EGL_TRUE,
114 };
115
116 #ifdef HAVE_LIBDRM
117 extern const _EGLDriver _eglDriver;
118 /*
119 * Negative value on error, zero if newly added, one if already in list.
120 */
121 static int
122 _eglAddDRMDevice(drmDevicePtr device)
123 {
124 _EGLDevice *dev;
125
126 assert(device->available_nodes & ((1 << DRM_NODE_RENDER)));
127
128 /* TODO: uncomment this assert, which is a sanity check.
129 *
130 * assert(device->available_nodes & ((1 << DRM_NODE_PRIMARY)));
131 *
132 * DRM shim does not expose a primary node, so the CI would fail if we had
133 * this assert. DRM shim is being used to run shader-db. We need to
134 * investigate what should be done (probably fixing DRM shim).
135 */
136
137 dev = _eglGlobal.DeviceList;
138
139 /* The first device is always software */
140 assert(dev);
141 assert(_eglDeviceSupports(dev, _EGL_DEVICE_SOFTWARE));
142
143 while (dev->Next) {
144 dev = dev->Next;
145
146 assert(_eglDeviceSupports(dev, _EGL_DEVICE_DRM));
147 if (drmDevicesEqual(device, dev->device) != 0)
148 return 1;
149 }
150
151 dev->Next = calloc(1, sizeof(_EGLDevice));
152 if (!dev->Next)
153 return -1;
154
155 dev = dev->Next;
156
157 /* Need to have render node for query to work. */
158 bool supports_info_query =
159 device->available_nodes & (1 << DRM_NODE_RENDER) &&
160 _eglDriver.QueryDeviceInfo;
161
162 if (supports_info_query)
163 dev->extensions =
164 "EGL_EXT_device_drm EGL_EXT_device_drm_render_node "
165 "EGL_EXT_device_query_name EGL_EXT_device_persistent_id";
166 else
167 dev->extensions = "EGL_EXT_device_drm EGL_EXT_device_drm_render_node";
168 dev->EXT_device_drm = EGL_TRUE;
169 dev->EXT_device_drm_render_node = EGL_TRUE;
170 if (supports_info_query) {
171 dev->EXT_device_query_name = EGL_TRUE;
172 dev->EXT_device_persistent_id = EGL_TRUE;
173 }
174 dev->device = device;
175
176 return 0;
177 }
178 #endif
179
180 /* Finds a device in DeviceList, for the given fd.
181 *
182 * The fd must be of a render-capable device, as there are only render-capable
183 * devices in DeviceList.
184 *
185 * If a software device, the fd is ignored.
186 */
187 _EGLDevice *
188 _eglFindDevice(int fd, bool software)
189 {
190 _EGLDevice *dev;
191
192 simple_mtx_lock(_eglGlobal.Mutex);
193 dev = _eglGlobal.DeviceList;
194
195 /* The first device is always software */
196 assert(dev);
197 assert(_eglDeviceSupports(dev, _EGL_DEVICE_SOFTWARE));
198 if (software)
199 goto out;
200
201 #ifdef HAVE_LIBDRM
202 drmDevicePtr device;
203
204 if (drmGetDevice2(fd, 0, &device) != 0) {
205 dev = NULL;
206 goto out;
207 }
208
209 while (dev->Next) {
210 dev = dev->Next;
211
212 if (_eglDeviceSupports(dev, _EGL_DEVICE_DRM) &&
213 drmDevicesEqual(device, dev->device) != 0) {
214 goto cleanup_drm;
215 }
216 }
217
218 /* Couldn't find an EGLDevice for the device. */
219 dev = NULL;
220
221 cleanup_drm:
222 drmFreeDevice(&device);
223
224 #else
225 _eglLog(_EGL_FATAL,
226 "Driver bug: Built without libdrm, yet looking for HW device");
227 dev = NULL;
228 #endif
229
230 out:
231 simple_mtx_unlock(_eglGlobal.Mutex);
232 return dev;
233 }
234
235 #ifdef HAVE_LIBDRM
236 drmDevicePtr
237 _eglDeviceDrm(_EGLDevice *dev)
238 {
239 if (!dev)
240 return NULL;
241
242 return dev->device;
243 }
244 #endif
245
246 _EGLDevice *
247 _eglDeviceNext(_EGLDevice *dev)
248 {
249 if (!dev)
250 return NULL;
251
252 return dev->Next;
253 }
254
255 EGLBoolean
256 _eglDeviceSupports(_EGLDevice *dev, _EGLDeviceExtension ext)
257 {
258 switch (ext) {
259 case _EGL_DEVICE_SOFTWARE:
260 return dev->MESA_device_software;
261 case _EGL_DEVICE_DRM:
262 return dev->EXT_device_drm;
263 case _EGL_DEVICE_DRM_RENDER_NODE:
264 return dev->EXT_device_drm_render_node;
265 case _EGL_DEVICE_QUERY_NAME:
266 return dev->EXT_device_query_name;
267 case _EGL_DEVICE_PERSISTENT_ID:
268 return dev->EXT_device_persistent_id;
269 default:
270 assert(0);
271 return EGL_FALSE;
272 };
273 }
274
275 EGLBoolean
276 _eglQueryDeviceAttribEXT(_EGLDevice *dev, EGLint attribute, EGLAttrib *value)
277 {
278 switch (attribute) {
279 default:
280 _eglError(EGL_BAD_ATTRIBUTE, "eglQueryDeviceAttribEXT");
281 return EGL_FALSE;
282 }
283 }
284
285 EGLBoolean
286 _eglQueryDeviceBinaryEXT(_EGLDevice *dev, EGLint name, EGLint max_size,
287 void *value, EGLint *size)
288 {
289 if (!_eglDeviceSupports(dev, _EGL_DEVICE_DRM) ||
290 !_eglDeviceSupports(dev, _EGL_DEVICE_PERSISTENT_ID) ||
291 !(name == EGL_DEVICE_UUID_EXT || name == EGL_DRIVER_UUID_EXT) ||
292 (value != NULL && max_size < EGL_UUID_SIZE) ||
293 size == NULL) {
294 _eglError(EGL_BAD_ATTRIBUTE, "eglQueryDeviceBinaryEXT");
295 return EGL_FALSE;
296 }
297
298 #ifdef HAVE_LIBDRM
299 drmDevicePtr device = _eglDeviceDrm(dev);
300 /* Software device does not have render node. */
301 const char *render_node =
302 _eglDeviceSupports(dev, _EGL_DEVICE_SOFTWARE) ?
303 "" : device->nodes[DRM_NODE_RENDER];
304 if (!_eglDriver.QueryDeviceInfo(render_node, &dev->device_info)) {
305 _eglError(EGL_BAD_DEVICE_EXT, "eglQueryDeviceBinaryEXT");
306 return EGL_FALSE;
307 }
308 if (value && name == EGL_DEVICE_UUID_EXT) {
309 memcpy(value, dev->device_info.device_uuid, EGL_UUID_SIZE);
310 }
311 if (value && name == EGL_DRIVER_UUID_EXT) {
312 memcpy(value, dev->device_info.driver_uuid, EGL_UUID_SIZE);
313 }
314 *size = EGL_UUID_SIZE;
315 return EGL_TRUE;
316 #else
317 /* This should never happen: we don't yet support EGL_DEVICE_DRM nor
318 * EGL_DEVICE_PERSISTENT_ID for the software device, and physical devices are
319 * only exposed when libdrm is available. */
320 assert(0);
321 _eglError(EGL_BAD_ATTRIBUTE, "eglQueryDeviceBinaryEXT");
322 return EGL_FALSE;
323 #endif
324 }
325
326 const char *
327 _eglQueryDeviceStringEXT(_EGLDevice *dev, EGLint name)
328 {
329 #ifdef HAVE_LIBDRM
330 drmDevicePtr device = _eglDeviceDrm(dev);
331 /* Software device does not have render node. */
332 const char *render_node =
333 _eglDeviceSupports(dev, _EGL_DEVICE_SOFTWARE) ?
334 "" : device->nodes[DRM_NODE_RENDER];
335 #endif
336
337 switch (name) {
338 case EGL_EXTENSIONS:
339 return dev->extensions;
340 case EGL_DRM_DEVICE_FILE_EXT:
341 if (!_eglDeviceSupports(dev, _EGL_DEVICE_DRM))
342 break;
343 #ifdef HAVE_LIBDRM
344 return dev->device->nodes[DRM_NODE_PRIMARY];
345 #else
346 /* This should never happen: we don't yet support EGL_DEVICE_DRM for the
347 * software device, and physical devices are only exposed when libdrm is
348 * available. */
349 assert(0);
350 break;
351 #endif
352 case EGL_DRM_RENDER_NODE_FILE_EXT:
353 if (!_eglDeviceSupports(dev, _EGL_DEVICE_DRM_RENDER_NODE))
354 break;
355 #ifdef HAVE_LIBDRM
356 /* EGLDevice represents a software device, so no render node
357 * should be advertised. */
358 if (_eglDeviceSupports(dev, _EGL_DEVICE_SOFTWARE))
359 return NULL;
360 /* We create EGLDevice's only for render capable devices. */
361 assert(dev->device->available_nodes & (1 << DRM_NODE_RENDER));
362 return dev->device->nodes[DRM_NODE_RENDER];
363 #else
364 /* Physical devices are only exposed when libdrm is available. */
365 assert(_eglDeviceSupports(dev, _EGL_DEVICE_SOFTWARE));
366 return NULL;
367 #endif
368 /* EGL_EXT_device_query_name */
369 case EGL_VENDOR:
370 if (!_eglDeviceSupports(dev, _EGL_DEVICE_QUERY_NAME))
371 break;
372 #ifdef HAVE_LIBDRM
373 if (!_eglDriver.QueryDeviceInfo(render_node, &dev->device_info)) {
374 _eglError(EGL_BAD_DEVICE_EXT, "eglQueryDeviceStringEXT");
375 return NULL;
376 }
377 return dev->device_info.vendor_name;
378 #else
379 /* Physical devices are only exposed when libdrm is available. */
380 assert(_eglDeviceSupports(dev, _EGL_DEVICE_SOFTWARE));
381 return NULL;
382 #endif
383 case EGL_RENDERER_EXT:
384 if (!_eglDeviceSupports(dev, _EGL_DEVICE_QUERY_NAME))
385 break;
386 #ifdef HAVE_LIBDRM
387 if (!_eglDriver.QueryDeviceInfo(render_node, &dev->device_info)) {
388 _eglError(EGL_BAD_DEVICE_EXT, "eglQueryDeviceStringEXT");
389 return NULL;
390 }
391 return dev->device_info.renderer_name;
392 #else
393 /* Physical devices are only exposed when libdrm is available. */
394 assert(_eglDeviceSupports(dev, _EGL_DEVICE_SOFTWARE));
395 return NULL;
396 #endif
397 /* EGL_EXT_device_persistent_id */
398 case EGL_DRIVER_NAME_EXT:
399 if (!_eglDeviceSupports(dev, _EGL_DEVICE_PERSISTENT_ID))
400 break;
401 #ifdef HAVE_LIBDRM
402 if (!_eglDriver.QueryDeviceInfo(render_node, &dev->device_info)) {
403 _eglError(EGL_BAD_DEVICE_EXT, "eglQueryDeviceStringEXT");
404 return NULL;
405 }
406 return dev->device_info.driver_name;
407 #else
408 /* Physical devices are only exposed when libdrm is available. */
409 assert(_eglDeviceSupports(dev, _EGL_DEVICE_SOFTWARE));
410 return NULL;
411 #endif
412 }
413 _eglError(EGL_BAD_PARAMETER, "eglQueryDeviceStringEXT");
414 return NULL;
415 }
416
417 /* Do a fresh lookup for devices.
418 *
419 * Walks through the DeviceList, discarding no longer available ones
420 * and adding new ones as applicable.
421 *
422 * Must be called with the global lock held.
423 */
424 int
425 _eglDeviceRefreshList(void)
426 {
427 ASSERTED _EGLDevice *dev;
428 int count = 0;
429
430 dev = _eglGlobal.DeviceList;
431
432 /* The first device is always software */
433 assert(dev);
434 assert(_eglDeviceSupports(dev, _EGL_DEVICE_SOFTWARE));
435 count++;
436
437 #ifdef HAVE_LIBDRM
438 drmDevicePtr devices[64];
439 int num_devs, ret;
440
441 num_devs = drmGetDevices2(0, devices, ARRAY_SIZE(devices));
442 for (int i = 0; i < num_devs; i++) {
443 if (!(devices[i]->available_nodes & (1 << DRM_NODE_RENDER))) {
444 drmFreeDevice(&devices[i]);
445 continue;
446 }
447
448 ret = _eglAddDRMDevice(devices[i]);
449
450 /* Device is not added - error or already present */
451 if (ret != 0)
452 drmFreeDevice(&devices[i]);
453
454 if (ret >= 0)
455 count++;
456 }
457 #endif
458
459 return count;
460 }
461
462 EGLBoolean
463 _eglQueryDevicesEXT(EGLint max_devices, _EGLDevice **devices,
464 EGLint *num_devices)
465 {
466 _EGLDevice *dev, *devs, *swrast;
467 int i = 0, num_devs;
468 bool request_software =
469 debug_get_bool_option("LIBGL_ALWAYS_SOFTWARE", false);
470
471 if ((devices && max_devices <= 0) || !num_devices)
472 return _eglError(EGL_BAD_PARAMETER, "eglQueryDevicesEXT");
473
474 simple_mtx_lock(_eglGlobal.Mutex);
475
476 num_devs = _eglDeviceRefreshList();
477 devs = _eglGlobal.DeviceList;
478
479 #ifdef HAVE_SWRAST
480 swrast = devs;
481 #else
482 swrast = NULL;
483 num_devs--;
484 #endif
485
486 /* The first device is swrast. Start with the non-swrast device. */
487 devs = devs->Next;
488
489 /* bail early if we only care about the count */
490 if (!devices) {
491 *num_devices = request_software ? !!swrast : num_devs;
492 goto out;
493 }
494
495 if (request_software) {
496 *num_devices = !!swrast;
497 devices[0] = swrast;
498 goto out;
499 }
500
501 *num_devices = MIN2(num_devs, max_devices);
502
503 /* Add non-swrast devices first and add swrast last.
504 *
505 * By default, the user is likely to pick the first device so having the
506 * software (aka least performant) one is not a good idea.
507 */
508 for (i = 0, dev = devs; dev && i < max_devices; i++) {
509 devices[i] = dev;
510 dev = dev->Next;
511 }
512
513 /* User requested the full device list, add the software device. */
514 if (max_devices >= num_devs && swrast) {
515 assert(_eglDeviceSupports(swrast, _EGL_DEVICE_SOFTWARE));
516 devices[num_devs - 1] = swrast;
517 }
518
519 out:
520 simple_mtx_unlock(_eglGlobal.Mutex);
521
522 return EGL_TRUE;
523 }