1 /**
2 * \file xf86drm.c
3 * User-level interface to DRM device
4 *
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Kevin E. Martin <martin@valinux.com>
7 */
8
9 /*
10 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
11 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
12 * All Rights Reserved.
13 *
14 * Permission is hereby granted, free of charge, to any person obtaining a
15 * copy of this software and associated documentation files (the "Software"),
16 * to deal in the Software without restriction, including without limitation
17 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 * and/or sell copies of the Software, and to permit persons to whom the
19 * Software is furnished to do so, subject to the following conditions:
20 *
21 * The above copyright notice and this permission notice (including the next
22 * paragraph) shall be included in all copies or substantial portions of the
23 * Software.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
31 * DEALINGS IN THE SOFTWARE.
32 */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <stdbool.h>
37 #include <unistd.h>
38 #include <string.h>
39 #include <strings.h>
40 #include <ctype.h>
41 #include <dirent.h>
42 #include <stddef.h>
43 #include <fcntl.h>
44 #include <errno.h>
45 #include <limits.h>
46 #include <signal.h>
47 #include <time.h>
48 #include <sys/types.h>
49 #include <sys/stat.h>
50 #define stat_t struct stat
51 #include <sys/ioctl.h>
52 #include <sys/time.h>
53 #include <stdarg.h>
54 #ifdef MAJOR_IN_MKDEV
55 #include <sys/mkdev.h>
56 #endif
57 #ifdef MAJOR_IN_SYSMACROS
58 #include <sys/sysmacros.h>
59 #endif
60 #include <math.h>
61
62 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
63
64 /* Not all systems have MAP_FAILED defined */
65 #ifndef MAP_FAILED
66 #define MAP_FAILED ((void *)-1)
67 #endif
68
69 #include "xf86drm.h"
70 #include "libdrm_macros.h"
71
72 #include "util_math.h"
73
74 #ifdef __OpenBSD__
75 #define DRM_PRIMARY_MINOR_NAME "drm"
76 #define DRM_CONTROL_MINOR_NAME "drmC"
77 #define DRM_RENDER_MINOR_NAME "drmR"
78 #else
79 #define DRM_PRIMARY_MINOR_NAME "card"
80 #define DRM_CONTROL_MINOR_NAME "controlD"
81 #define DRM_RENDER_MINOR_NAME "renderD"
82 #endif
83
84 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
85 #define DRM_MAJOR 145
86 #endif
87
88 #ifdef __NetBSD__
89 #define DRM_MAJOR 34
90 #endif
91
92 #ifdef __OpenBSD__
93 #ifdef __i386__
94 #define DRM_MAJOR 88
95 #else
96 #define DRM_MAJOR 87
97 #endif
98 #endif /* __OpenBSD__ */
99
100 #ifndef DRM_MAJOR
101 #define DRM_MAJOR 226 /* Linux */
102 #endif
103
104 #if defined(__OpenBSD__) || defined(__DragonFly__)
105 struct drm_pciinfo {
106 uint16_t domain;
107 uint8_t bus;
108 uint8_t dev;
109 uint8_t func;
110 uint16_t vendor_id;
111 uint16_t device_id;
112 uint16_t subvendor_id;
113 uint16_t subdevice_id;
114 uint8_t revision_id;
115 };
116
117 #define DRM_IOCTL_GET_PCIINFO DRM_IOR(0x15, struct drm_pciinfo)
118 #endif
119
120 #define DRM_MSG_VERBOSITY 3
121
122 #define memclear(s) memset(&s, 0, sizeof(s))
123
124 static drmServerInfoPtr drm_server_info;
125
drmSetServerInfo(drmServerInfoPtr info)126 drm_public void drmSetServerInfo(drmServerInfoPtr info)
127 {
128 drm_server_info = info;
129 }
130
131 /**
132 * Output a message to stderr.
133 *
134 * \param format printf() like format string.
135 *
136 * \internal
137 * This function is a wrapper around vfprintf().
138 */
139
140 static int DRM_PRINTFLIKE(1, 0)
drmDebugPrint(const char * format,va_list ap)141 drmDebugPrint(const char *format, va_list ap)
142 {
143 return vfprintf(stderr, format, ap);
144 }
145
146 drm_public void
drmMsg(const char * format,...)147 drmMsg(const char *format, ...)
148 {
149 va_list ap;
150 const char *env;
151 if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) ||
152 (drm_server_info && drm_server_info->debug_print))
153 {
154 va_start(ap, format);
155 if (drm_server_info) {
156 drm_server_info->debug_print(format,ap);
157 } else {
158 drmDebugPrint(format, ap);
159 }
160 va_end(ap);
161 }
162 }
163
164 static void *drmHashTable = NULL; /* Context switch callbacks */
165
drmGetHashTable(void)166 drm_public void *drmGetHashTable(void)
167 {
168 return drmHashTable;
169 }
170
drmMalloc(int size)171 drm_public void *drmMalloc(int size)
172 {
173 return calloc(1, size);
174 }
175
drmFree(void * pt)176 drm_public void drmFree(void *pt)
177 {
178 free(pt);
179 }
180
181 /**
182 * Call ioctl, restarting if it is interrupted
183 */
184 drm_public int
drmIoctl(int fd,unsigned long request,void * arg)185 drmIoctl(int fd, unsigned long request, void *arg)
186 {
187 int ret;
188
189 do {
190 ret = ioctl(fd, request, arg);
191 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
192 return ret;
193 }
194
drmGetKeyFromFd(int fd)195 static unsigned long drmGetKeyFromFd(int fd)
196 {
197 stat_t st;
198
199 st.st_rdev = 0;
200 fstat(fd, &st);
201 return st.st_rdev;
202 }
203
drmGetEntry(int fd)204 drm_public drmHashEntry *drmGetEntry(int fd)
205 {
206 unsigned long key = drmGetKeyFromFd(fd);
207 void *value;
208 drmHashEntry *entry;
209
210 if (!drmHashTable)
211 drmHashTable = drmHashCreate();
212
213 if (drmHashLookup(drmHashTable, key, &value)) {
214 entry = drmMalloc(sizeof(*entry));
215 entry->fd = fd;
216 entry->f = NULL;
217 entry->tagTable = drmHashCreate();
218 drmHashInsert(drmHashTable, key, entry);
219 } else {
220 entry = value;
221 }
222 return entry;
223 }
224
225 /**
226 * Compare two busid strings
227 *
228 * \param first
229 * \param second
230 *
231 * \return 1 if matched.
232 *
233 * \internal
234 * This function compares two bus ID strings. It understands the older
235 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
236 * domain, b is bus, d is device, f is function.
237 */
drmMatchBusID(const char * id1,const char * id2,int pci_domain_ok)238 static int drmMatchBusID(const char *id1, const char *id2, int pci_domain_ok)
239 {
240 /* First, check if the IDs are exactly the same */
241 if (strcasecmp(id1, id2) == 0)
242 return 1;
243
244 /* Try to match old/new-style PCI bus IDs. */
245 if (strncasecmp(id1, "pci", 3) == 0) {
246 unsigned int o1, b1, d1, f1;
247 unsigned int o2, b2, d2, f2;
248 int ret;
249
250 ret = sscanf(id1, "pci:%04x:%02x:%02x.%u", &o1, &b1, &d1, &f1);
251 if (ret != 4) {
252 o1 = 0;
253 ret = sscanf(id1, "PCI:%u:%u:%u", &b1, &d1, &f1);
254 if (ret != 3)
255 return 0;
256 }
257
258 ret = sscanf(id2, "pci:%04x:%02x:%02x.%u", &o2, &b2, &d2, &f2);
259 if (ret != 4) {
260 o2 = 0;
261 ret = sscanf(id2, "PCI:%u:%u:%u", &b2, &d2, &f2);
262 if (ret != 3)
263 return 0;
264 }
265
266 /* If domains aren't properly supported by the kernel interface,
267 * just ignore them, which sucks less than picking a totally random
268 * card with "open by name"
269 */
270 if (!pci_domain_ok)
271 o1 = o2 = 0;
272
273 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
274 return 0;
275 else
276 return 1;
277 }
278 return 0;
279 }
280
281 /**
282 * Handles error checking for chown call.
283 *
284 * \param path to file.
285 * \param id of the new owner.
286 * \param id of the new group.
287 *
288 * \return zero if success or -1 if failure.
289 *
290 * \internal
291 * Checks for failure. If failure was caused by signal call chown again.
292 * If any other failure happened then it will output error message using
293 * drmMsg() call.
294 */
295 #if !UDEV
chown_check_return(const char * path,uid_t owner,gid_t group)296 static int chown_check_return(const char *path, uid_t owner, gid_t group)
297 {
298 int rv;
299
300 do {
301 rv = chown(path, owner, group);
302 } while (rv != 0 && errno == EINTR);
303
304 if (rv == 0)
305 return 0;
306
307 drmMsg("Failed to change owner or group for file %s! %d: %s\n",
308 path, errno, strerror(errno));
309 return -1;
310 }
311 #endif
312
313 /**
314 * Open the DRM device, creating it if necessary.
315 *
316 * \param dev major and minor numbers of the device.
317 * \param minor minor number of the device.
318 *
319 * \return a file descriptor on success, or a negative value on error.
320 *
321 * \internal
322 * Assembles the device name from \p minor and opens it, creating the device
323 * special file node with the major and minor numbers specified by \p dev and
324 * parent directory if necessary and was called by root.
325 */
drmOpenDevice(dev_t dev,int minor,int type)326 static int drmOpenDevice(dev_t dev, int minor, int type)
327 {
328 stat_t st;
329 const char *dev_name;
330 char buf[64];
331 int fd;
332 mode_t devmode = DRM_DEV_MODE, serv_mode;
333 gid_t serv_group;
334 #if !UDEV
335 int isroot = !geteuid();
336 uid_t user = DRM_DEV_UID;
337 gid_t group = DRM_DEV_GID;
338 #endif
339
340 switch (type) {
341 case DRM_NODE_PRIMARY:
342 dev_name = DRM_DEV_NAME;
343 break;
344 case DRM_NODE_CONTROL:
345 dev_name = DRM_CONTROL_DEV_NAME;
346 break;
347 case DRM_NODE_RENDER:
348 dev_name = DRM_RENDER_DEV_NAME;
349 break;
350 default:
351 return -EINVAL;
352 };
353
354 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
355 drmMsg("drmOpenDevice: node name is %s\n", buf);
356
357 if (drm_server_info && drm_server_info->get_perms) {
358 drm_server_info->get_perms(&serv_group, &serv_mode);
359 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
360 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
361 }
362
363 #if !UDEV
364 if (stat(DRM_DIR_NAME, &st)) {
365 if (!isroot)
366 return DRM_ERR_NOT_ROOT;
367 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
368 chown_check_return(DRM_DIR_NAME, 0, 0); /* root:root */
369 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
370 }
371
372 /* Check if the device node exists and create it if necessary. */
373 if (stat(buf, &st)) {
374 if (!isroot)
375 return DRM_ERR_NOT_ROOT;
376 remove(buf);
377 mknod(buf, S_IFCHR | devmode, dev);
378 }
379
380 if (drm_server_info && drm_server_info->get_perms) {
381 group = ((int)serv_group >= 0) ? serv_group : DRM_DEV_GID;
382 chown_check_return(buf, user, group);
383 chmod(buf, devmode);
384 }
385 #else
386 /* if we modprobed then wait for udev */
387 {
388 int udev_count = 0;
389 wait_for_udev:
390 if (stat(DRM_DIR_NAME, &st)) {
391 usleep(20);
392 udev_count++;
393
394 if (udev_count == 50)
395 return -1;
396 goto wait_for_udev;
397 }
398
399 if (stat(buf, &st)) {
400 usleep(20);
401 udev_count++;
402
403 if (udev_count == 50)
404 return -1;
405 goto wait_for_udev;
406 }
407 }
408 #endif
409
410 fd = open(buf, O_RDWR | O_CLOEXEC, 0);
411 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
412 fd, fd < 0 ? strerror(errno) : "OK");
413 if (fd >= 0)
414 return fd;
415
416 #if !UDEV
417 /* Check if the device node is not what we expect it to be, and recreate it
418 * and try again if so.
419 */
420 if (st.st_rdev != dev) {
421 if (!isroot)
422 return DRM_ERR_NOT_ROOT;
423 remove(buf);
424 mknod(buf, S_IFCHR | devmode, dev);
425 if (drm_server_info && drm_server_info->get_perms) {
426 chown_check_return(buf, user, group);
427 chmod(buf, devmode);
428 }
429 }
430 fd = open(buf, O_RDWR | O_CLOEXEC, 0);
431 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
432 fd, fd < 0 ? strerror(errno) : "OK");
433 if (fd >= 0)
434 return fd;
435
436 drmMsg("drmOpenDevice: Open failed\n");
437 remove(buf);
438 #endif
439 return -errno;
440 }
441
442
443 /**
444 * Open the DRM device
445 *
446 * \param minor device minor number.
447 * \param create allow to create the device if set.
448 *
449 * \return a file descriptor on success, or a negative value on error.
450 *
451 * \internal
452 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
453 * name from \p minor and opens it.
454 */
drmOpenMinor(int minor,int create,int type)455 static int drmOpenMinor(int minor, int create, int type)
456 {
457 int fd;
458 char buf[64];
459 const char *dev_name;
460
461 if (create)
462 return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
463
464 switch (type) {
465 case DRM_NODE_PRIMARY:
466 dev_name = DRM_DEV_NAME;
467 break;
468 case DRM_NODE_CONTROL:
469 dev_name = DRM_CONTROL_DEV_NAME;
470 break;
471 case DRM_NODE_RENDER:
472 dev_name = DRM_RENDER_DEV_NAME;
473 break;
474 default:
475 return -EINVAL;
476 };
477
478 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
479 if ((fd = open(buf, O_RDWR | O_CLOEXEC, 0)) >= 0)
480 return fd;
481 return -errno;
482 }
483
484
485 /**
486 * Determine whether the DRM kernel driver has been loaded.
487 *
488 * \return 1 if the DRM driver is loaded, 0 otherwise.
489 *
490 * \internal
491 * Determine the presence of the kernel driver by attempting to open the 0
492 * minor and get version information. For backward compatibility with older
493 * Linux implementations, /proc/dri is also checked.
494 */
drmAvailable(void)495 drm_public int drmAvailable(void)
496 {
497 drmVersionPtr version;
498 int retval = 0;
499 int fd;
500
501 if ((fd = drmOpenMinor(0, 1, DRM_NODE_PRIMARY)) < 0) {
502 #ifdef __linux__
503 /* Try proc for backward Linux compatibility */
504 if (!access("/proc/dri/0", R_OK))
505 return 1;
506 #endif
507 return 0;
508 }
509
510 if ((version = drmGetVersion(fd))) {
511 retval = 1;
512 drmFreeVersion(version);
513 }
514 close(fd);
515
516 return retval;
517 }
518
drmGetMinorBase(int type)519 static int drmGetMinorBase(int type)
520 {
521 switch (type) {
522 case DRM_NODE_PRIMARY:
523 return 0;
524 case DRM_NODE_CONTROL:
525 return 64;
526 case DRM_NODE_RENDER:
527 return 128;
528 default:
529 return -1;
530 };
531 }
532
drmGetMinorType(int minor)533 static int drmGetMinorType(int minor)
534 {
535 int type = minor >> 6;
536
537 if (minor < 0)
538 return -1;
539
540 switch (type) {
541 case DRM_NODE_PRIMARY:
542 case DRM_NODE_CONTROL:
543 case DRM_NODE_RENDER:
544 return type;
545 default:
546 return -1;
547 }
548 }
549
drmGetMinorName(int type)550 static const char *drmGetMinorName(int type)
551 {
552 switch (type) {
553 case DRM_NODE_PRIMARY:
554 return DRM_PRIMARY_MINOR_NAME;
555 case DRM_NODE_CONTROL:
556 return DRM_CONTROL_MINOR_NAME;
557 case DRM_NODE_RENDER:
558 return DRM_RENDER_MINOR_NAME;
559 default:
560 return NULL;
561 }
562 }
563
564 /**
565 * Open the device by bus ID.
566 *
567 * \param busid bus ID.
568 * \param type device node type.
569 *
570 * \return a file descriptor on success, or a negative value on error.
571 *
572 * \internal
573 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
574 * comparing the device bus ID with the one supplied.
575 *
576 * \sa drmOpenMinor() and drmGetBusid().
577 */
drmOpenByBusid(const char * busid,int type)578 static int drmOpenByBusid(const char *busid, int type)
579 {
580 int i, pci_domain_ok = 1;
581 int fd;
582 const char *buf;
583 drmSetVersion sv;
584 int base = drmGetMinorBase(type);
585
586 if (base < 0)
587 return -1;
588
589 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
590 for (i = base; i < base + DRM_MAX_MINOR; i++) {
591 fd = drmOpenMinor(i, 1, type);
592 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
593 if (fd >= 0) {
594 /* We need to try for 1.4 first for proper PCI domain support
595 * and if that fails, we know the kernel is busted
596 */
597 sv.drm_di_major = 1;
598 sv.drm_di_minor = 4;
599 sv.drm_dd_major = -1; /* Don't care */
600 sv.drm_dd_minor = -1; /* Don't care */
601 if (drmSetInterfaceVersion(fd, &sv)) {
602 #ifndef __alpha__
603 pci_domain_ok = 0;
604 #endif
605 sv.drm_di_major = 1;
606 sv.drm_di_minor = 1;
607 sv.drm_dd_major = -1; /* Don't care */
608 sv.drm_dd_minor = -1; /* Don't care */
609 drmMsg("drmOpenByBusid: Interface 1.4 failed, trying 1.1\n");
610 drmSetInterfaceVersion(fd, &sv);
611 }
612 buf = drmGetBusid(fd);
613 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
614 if (buf && drmMatchBusID(buf, busid, pci_domain_ok)) {
615 drmFreeBusid(buf);
616 return fd;
617 }
618 if (buf)
619 drmFreeBusid(buf);
620 close(fd);
621 }
622 }
623 return -1;
624 }
625
626
627 /**
628 * Open the device by name.
629 *
630 * \param name driver name.
631 * \param type the device node type.
632 *
633 * \return a file descriptor on success, or a negative value on error.
634 *
635 * \internal
636 * This function opens the first minor number that matches the driver name and
637 * isn't already in use. If it's in use it then it will already have a bus ID
638 * assigned.
639 *
640 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
641 */
drmOpenByName(const char * name,int type)642 static int drmOpenByName(const char *name, int type)
643 {
644 int i;
645 int fd;
646 drmVersionPtr version;
647 char * id;
648 int base = drmGetMinorBase(type);
649
650 if (base < 0)
651 return -1;
652
653 /*
654 * Open the first minor number that matches the driver name and isn't
655 * already in use. If it's in use it will have a busid assigned already.
656 */
657 for (i = base; i < base + DRM_MAX_MINOR; i++) {
658 if ((fd = drmOpenMinor(i, 1, type)) >= 0) {
659 if ((version = drmGetVersion(fd))) {
660 if (!strcmp(version->name, name)) {
661 drmFreeVersion(version);
662 id = drmGetBusid(fd);
663 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
664 if (!id || !*id) {
665 if (id)
666 drmFreeBusid(id);
667 return fd;
668 } else {
669 drmFreeBusid(id);
670 }
671 } else {
672 drmFreeVersion(version);
673 }
674 }
675 close(fd);
676 }
677 }
678
679 #ifdef __linux__
680 /* Backward-compatibility /proc support */
681 for (i = 0; i < 8; i++) {
682 char proc_name[64], buf[512];
683 char *driver, *pt, *devstring;
684 int retcode;
685
686 sprintf(proc_name, "/proc/dri/%d/name", i);
687 if ((fd = open(proc_name, 0, 0)) >= 0) {
688 retcode = read(fd, buf, sizeof(buf)-1);
689 close(fd);
690 if (retcode) {
691 buf[retcode-1] = '\0';
692 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
693 ;
694 if (*pt) { /* Device is next */
695 *pt = '\0';
696 if (!strcmp(driver, name)) { /* Match */
697 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
698 ;
699 if (*pt) { /* Found busid */
700 return drmOpenByBusid(++pt, type);
701 } else { /* No busid */
702 return drmOpenDevice(strtol(devstring, NULL, 0),i, type);
703 }
704 }
705 }
706 }
707 }
708 }
709 #endif
710
711 return -1;
712 }
713
714
715 /**
716 * Open the DRM device.
717 *
718 * Looks up the specified name and bus ID, and opens the device found. The
719 * entry in /dev/dri is created if necessary and if called by root.
720 *
721 * \param name driver name. Not referenced if bus ID is supplied.
722 * \param busid bus ID. Zero if not known.
723 *
724 * \return a file descriptor on success, or a negative value on error.
725 *
726 * \internal
727 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
728 * otherwise.
729 */
drmOpen(const char * name,const char * busid)730 drm_public int drmOpen(const char *name, const char *busid)
731 {
732 return drmOpenWithType(name, busid, DRM_NODE_PRIMARY);
733 }
734
735 /**
736 * Open the DRM device with specified type.
737 *
738 * Looks up the specified name and bus ID, and opens the device found. The
739 * entry in /dev/dri is created if necessary and if called by root.
740 *
741 * \param name driver name. Not referenced if bus ID is supplied.
742 * \param busid bus ID. Zero if not known.
743 * \param type the device node type to open, PRIMARY, CONTROL or RENDER
744 *
745 * \return a file descriptor on success, or a negative value on error.
746 *
747 * \internal
748 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
749 * otherwise.
750 */
drmOpenWithType(const char * name,const char * busid,int type)751 drm_public int drmOpenWithType(const char *name, const char *busid, int type)
752 {
753 if (name != NULL && drm_server_info &&
754 drm_server_info->load_module && !drmAvailable()) {
755 /* try to load the kernel module */
756 if (!drm_server_info->load_module(name)) {
757 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
758 return -1;
759 }
760 }
761
762 if (busid) {
763 int fd = drmOpenByBusid(busid, type);
764 if (fd >= 0)
765 return fd;
766 }
767
768 if (name)
769 return drmOpenByName(name, type);
770
771 return -1;
772 }
773
drmOpenControl(int minor)774 drm_public int drmOpenControl(int minor)
775 {
776 return drmOpenMinor(minor, 0, DRM_NODE_CONTROL);
777 }
778
drmOpenRender(int minor)779 drm_public int drmOpenRender(int minor)
780 {
781 return drmOpenMinor(minor, 0, DRM_NODE_RENDER);
782 }
783
784 /**
785 * Free the version information returned by drmGetVersion().
786 *
787 * \param v pointer to the version information.
788 *
789 * \internal
790 * It frees the memory pointed by \p %v as well as all the non-null strings
791 * pointers in it.
792 */
drmFreeVersion(drmVersionPtr v)793 drm_public void drmFreeVersion(drmVersionPtr v)
794 {
795 if (!v)
796 return;
797 drmFree(v->name);
798 drmFree(v->date);
799 drmFree(v->desc);
800 drmFree(v);
801 }
802
803
804 /**
805 * Free the non-public version information returned by the kernel.
806 *
807 * \param v pointer to the version information.
808 *
809 * \internal
810 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
811 * the non-null strings pointers in it.
812 */
drmFreeKernelVersion(drm_version_t * v)813 static void drmFreeKernelVersion(drm_version_t *v)
814 {
815 if (!v)
816 return;
817 drmFree(v->name);
818 drmFree(v->date);
819 drmFree(v->desc);
820 drmFree(v);
821 }
822
823
824 /**
825 * Copy version information.
826 *
827 * \param d destination pointer.
828 * \param s source pointer.
829 *
830 * \internal
831 * Used by drmGetVersion() to translate the information returned by the ioctl
832 * interface in a private structure into the public structure counterpart.
833 */
drmCopyVersion(drmVersionPtr d,const drm_version_t * s)834 static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
835 {
836 d->version_major = s->version_major;
837 d->version_minor = s->version_minor;
838 d->version_patchlevel = s->version_patchlevel;
839 d->name_len = s->name_len;
840 d->name = strdup(s->name);
841 d->date_len = s->date_len;
842 d->date = strdup(s->date);
843 d->desc_len = s->desc_len;
844 d->desc = strdup(s->desc);
845 }
846
847
848 /**
849 * Query the driver version information.
850 *
851 * \param fd file descriptor.
852 *
853 * \return pointer to a drmVersion structure which should be freed with
854 * drmFreeVersion().
855 *
856 * \note Similar information is available via /proc/dri.
857 *
858 * \internal
859 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
860 * first with zeros to get the string lengths, and then the actually strings.
861 * It also null-terminates them since they might not be already.
862 */
drmGetVersion(int fd)863 drm_public drmVersionPtr drmGetVersion(int fd)
864 {
865 drmVersionPtr retval;
866 drm_version_t *version = drmMalloc(sizeof(*version));
867
868 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
869 drmFreeKernelVersion(version);
870 return NULL;
871 }
872
873 if (version->name_len)
874 version->name = drmMalloc(version->name_len + 1);
875 if (version->date_len)
876 version->date = drmMalloc(version->date_len + 1);
877 if (version->desc_len)
878 version->desc = drmMalloc(version->desc_len + 1);
879
880 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
881 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
882 drmFreeKernelVersion(version);
883 return NULL;
884 }
885
886 /* The results might not be null-terminated strings, so terminate them. */
887 if (version->name_len) version->name[version->name_len] = '\0';
888 if (version->date_len) version->date[version->date_len] = '\0';
889 if (version->desc_len) version->desc[version->desc_len] = '\0';
890
891 retval = drmMalloc(sizeof(*retval));
892 drmCopyVersion(retval, version);
893 drmFreeKernelVersion(version);
894 return retval;
895 }
896
897
898 /**
899 * Get version information for the DRM user space library.
900 *
901 * This version number is driver independent.
902 *
903 * \param fd file descriptor.
904 *
905 * \return version information.
906 *
907 * \internal
908 * This function allocates and fills a drm_version structure with a hard coded
909 * version number.
910 */
drmGetLibVersion(int fd)911 drm_public drmVersionPtr drmGetLibVersion(int fd)
912 {
913 drm_version_t *version = drmMalloc(sizeof(*version));
914
915 /* Version history:
916 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
917 * revision 1.0.x = original DRM interface with no drmGetLibVersion
918 * entry point and many drm<Device> extensions
919 * revision 1.1.x = added drmCommand entry points for device extensions
920 * added drmGetLibVersion to identify libdrm.a version
921 * revision 1.2.x = added drmSetInterfaceVersion
922 * modified drmOpen to handle both busid and name
923 * revision 1.3.x = added server + memory manager
924 */
925 version->version_major = 1;
926 version->version_minor = 3;
927 version->version_patchlevel = 0;
928
929 return (drmVersionPtr)version;
930 }
931
drmGetCap(int fd,uint64_t capability,uint64_t * value)932 drm_public int drmGetCap(int fd, uint64_t capability, uint64_t *value)
933 {
934 struct drm_get_cap cap;
935 int ret;
936
937 memclear(cap);
938 cap.capability = capability;
939
940 ret = drmIoctl(fd, DRM_IOCTL_GET_CAP, &cap);
941 if (ret)
942 return ret;
943
944 *value = cap.value;
945 return 0;
946 }
947
drmSetClientCap(int fd,uint64_t capability,uint64_t value)948 drm_public int drmSetClientCap(int fd, uint64_t capability, uint64_t value)
949 {
950 struct drm_set_client_cap cap;
951
952 memclear(cap);
953 cap.capability = capability;
954 cap.value = value;
955
956 return drmIoctl(fd, DRM_IOCTL_SET_CLIENT_CAP, &cap);
957 }
958
959 /**
960 * Free the bus ID information.
961 *
962 * \param busid bus ID information string as given by drmGetBusid().
963 *
964 * \internal
965 * This function is just frees the memory pointed by \p busid.
966 */
drmFreeBusid(const char * busid)967 drm_public void drmFreeBusid(const char *busid)
968 {
969 drmFree((void *)busid);
970 }
971
972
973 /**
974 * Get the bus ID of the device.
975 *
976 * \param fd file descriptor.
977 *
978 * \return bus ID string.
979 *
980 * \internal
981 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
982 * get the string length and data, passing the arguments in a drm_unique
983 * structure.
984 */
drmGetBusid(int fd)985 drm_public char *drmGetBusid(int fd)
986 {
987 drm_unique_t u;
988
989 memclear(u);
990
991 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
992 return NULL;
993 u.unique = drmMalloc(u.unique_len + 1);
994 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u)) {
995 drmFree(u.unique);
996 return NULL;
997 }
998 u.unique[u.unique_len] = '\0';
999
1000 return u.unique;
1001 }
1002
1003
1004 /**
1005 * Set the bus ID of the device.
1006 *
1007 * \param fd file descriptor.
1008 * \param busid bus ID string.
1009 *
1010 * \return zero on success, negative on failure.
1011 *
1012 * \internal
1013 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
1014 * the arguments in a drm_unique structure.
1015 */
drmSetBusid(int fd,const char * busid)1016 drm_public int drmSetBusid(int fd, const char *busid)
1017 {
1018 drm_unique_t u;
1019
1020 memclear(u);
1021 u.unique = (char *)busid;
1022 u.unique_len = strlen(busid);
1023
1024 if (drmIoctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
1025 return -errno;
1026 }
1027 return 0;
1028 }
1029
drmGetMagic(int fd,drm_magic_t * magic)1030 drm_public int drmGetMagic(int fd, drm_magic_t * magic)
1031 {
1032 drm_auth_t auth;
1033
1034 memclear(auth);
1035
1036 *magic = 0;
1037 if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
1038 return -errno;
1039 *magic = auth.magic;
1040 return 0;
1041 }
1042
drmAuthMagic(int fd,drm_magic_t magic)1043 drm_public int drmAuthMagic(int fd, drm_magic_t magic)
1044 {
1045 drm_auth_t auth;
1046
1047 memclear(auth);
1048 auth.magic = magic;
1049 if (drmIoctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
1050 return -errno;
1051 return 0;
1052 }
1053
1054 /**
1055 * Specifies a range of memory that is available for mapping by a
1056 * non-root process.
1057 *
1058 * \param fd file descriptor.
1059 * \param offset usually the physical address. The actual meaning depends of
1060 * the \p type parameter. See below.
1061 * \param size of the memory in bytes.
1062 * \param type type of the memory to be mapped.
1063 * \param flags combination of several flags to modify the function actions.
1064 * \param handle will be set to a value that may be used as the offset
1065 * parameter for mmap().
1066 *
1067 * \return zero on success or a negative value on error.
1068 *
1069 * \par Mapping the frame buffer
1070 * For the frame buffer
1071 * - \p offset will be the physical address of the start of the frame buffer,
1072 * - \p size will be the size of the frame buffer in bytes, and
1073 * - \p type will be DRM_FRAME_BUFFER.
1074 *
1075 * \par
1076 * The area mapped will be uncached. If MTRR support is available in the
1077 * kernel, the frame buffer area will be set to write combining.
1078 *
1079 * \par Mapping the MMIO register area
1080 * For the MMIO register area,
1081 * - \p offset will be the physical address of the start of the register area,
1082 * - \p size will be the size of the register area bytes, and
1083 * - \p type will be DRM_REGISTERS.
1084 * \par
1085 * The area mapped will be uncached.
1086 *
1087 * \par Mapping the SAREA
1088 * For the SAREA,
1089 * - \p offset will be ignored and should be set to zero,
1090 * - \p size will be the desired size of the SAREA in bytes,
1091 * - \p type will be DRM_SHM.
1092 *
1093 * \par
1094 * A shared memory area of the requested size will be created and locked in
1095 * kernel memory. This area may be mapped into client-space by using the handle
1096 * returned.
1097 *
1098 * \note May only be called by root.
1099 *
1100 * \internal
1101 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
1102 * the arguments in a drm_map structure.
1103 */
drmAddMap(int fd,drm_handle_t offset,drmSize size,drmMapType type,drmMapFlags flags,drm_handle_t * handle)1104 drm_public int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
1105 drmMapFlags flags, drm_handle_t *handle)
1106 {
1107 drm_map_t map;
1108
1109 memclear(map);
1110 map.offset = offset;
1111 map.size = size;
1112 map.type = type;
1113 map.flags = flags;
1114 if (drmIoctl(fd, DRM_IOCTL_ADD_MAP, &map))
1115 return -errno;
1116 if (handle)
1117 *handle = (drm_handle_t)(uintptr_t)map.handle;
1118 return 0;
1119 }
1120
drmRmMap(int fd,drm_handle_t handle)1121 drm_public int drmRmMap(int fd, drm_handle_t handle)
1122 {
1123 drm_map_t map;
1124
1125 memclear(map);
1126 map.handle = (void *)(uintptr_t)handle;
1127
1128 if(drmIoctl(fd, DRM_IOCTL_RM_MAP, &map))
1129 return -errno;
1130 return 0;
1131 }
1132
1133 /**
1134 * Make buffers available for DMA transfers.
1135 *
1136 * \param fd file descriptor.
1137 * \param count number of buffers.
1138 * \param size size of each buffer.
1139 * \param flags buffer allocation flags.
1140 * \param agp_offset offset in the AGP aperture
1141 *
1142 * \return number of buffers allocated, negative on error.
1143 *
1144 * \internal
1145 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
1146 *
1147 * \sa drm_buf_desc.
1148 */
drmAddBufs(int fd,int count,int size,drmBufDescFlags flags,int agp_offset)1149 drm_public int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
1150 int agp_offset)
1151 {
1152 drm_buf_desc_t request;
1153
1154 memclear(request);
1155 request.count = count;
1156 request.size = size;
1157 request.flags = flags;
1158 request.agp_start = agp_offset;
1159
1160 if (drmIoctl(fd, DRM_IOCTL_ADD_BUFS, &request))
1161 return -errno;
1162 return request.count;
1163 }
1164
drmMarkBufs(int fd,double low,double high)1165 drm_public int drmMarkBufs(int fd, double low, double high)
1166 {
1167 drm_buf_info_t info;
1168 int i;
1169
1170 memclear(info);
1171
1172 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1173 return -EINVAL;
1174
1175 if (!info.count)
1176 return -EINVAL;
1177
1178 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1179 return -ENOMEM;
1180
1181 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1182 int retval = -errno;
1183 drmFree(info.list);
1184 return retval;
1185 }
1186
1187 for (i = 0; i < info.count; i++) {
1188 info.list[i].low_mark = low * info.list[i].count;
1189 info.list[i].high_mark = high * info.list[i].count;
1190 if (drmIoctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
1191 int retval = -errno;
1192 drmFree(info.list);
1193 return retval;
1194 }
1195 }
1196 drmFree(info.list);
1197
1198 return 0;
1199 }
1200
1201 /**
1202 * Free buffers.
1203 *
1204 * \param fd file descriptor.
1205 * \param count number of buffers to free.
1206 * \param list list of buffers to be freed.
1207 *
1208 * \return zero on success, or a negative value on failure.
1209 *
1210 * \note This function is primarily used for debugging.
1211 *
1212 * \internal
1213 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
1214 * the arguments in a drm_buf_free structure.
1215 */
drmFreeBufs(int fd,int count,int * list)1216 drm_public int drmFreeBufs(int fd, int count, int *list)
1217 {
1218 drm_buf_free_t request;
1219
1220 memclear(request);
1221 request.count = count;
1222 request.list = list;
1223 if (drmIoctl(fd, DRM_IOCTL_FREE_BUFS, &request))
1224 return -errno;
1225 return 0;
1226 }
1227
1228
1229 /**
1230 * Close the device.
1231 *
1232 * \param fd file descriptor.
1233 *
1234 * \internal
1235 * This function closes the file descriptor.
1236 */
drmClose(int fd)1237 drm_public int drmClose(int fd)
1238 {
1239 unsigned long key = drmGetKeyFromFd(fd);
1240 drmHashEntry *entry = drmGetEntry(fd);
1241
1242 drmHashDestroy(entry->tagTable);
1243 entry->fd = 0;
1244 entry->f = NULL;
1245 entry->tagTable = NULL;
1246
1247 drmHashDelete(drmHashTable, key);
1248 drmFree(entry);
1249
1250 return close(fd);
1251 }
1252
1253
1254 /**
1255 * Map a region of memory.
1256 *
1257 * \param fd file descriptor.
1258 * \param handle handle returned by drmAddMap().
1259 * \param size size in bytes. Must match the size used by drmAddMap().
1260 * \param address will contain the user-space virtual address where the mapping
1261 * begins.
1262 *
1263 * \return zero on success, or a negative value on failure.
1264 *
1265 * \internal
1266 * This function is a wrapper for mmap().
1267 */
drmMap(int fd,drm_handle_t handle,drmSize size,drmAddressPtr address)1268 drm_public int drmMap(int fd, drm_handle_t handle, drmSize size,
1269 drmAddressPtr address)
1270 {
1271 static unsigned long pagesize_mask = 0;
1272
1273 if (fd < 0)
1274 return -EINVAL;
1275
1276 if (!pagesize_mask)
1277 pagesize_mask = getpagesize() - 1;
1278
1279 size = (size + pagesize_mask) & ~pagesize_mask;
1280
1281 *address = drm_mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
1282 if (*address == MAP_FAILED)
1283 return -errno;
1284 return 0;
1285 }
1286
1287
1288 /**
1289 * Unmap mappings obtained with drmMap().
1290 *
1291 * \param address address as given by drmMap().
1292 * \param size size in bytes. Must match the size used by drmMap().
1293 *
1294 * \return zero on success, or a negative value on failure.
1295 *
1296 * \internal
1297 * This function is a wrapper for munmap().
1298 */
drmUnmap(drmAddress address,drmSize size)1299 drm_public int drmUnmap(drmAddress address, drmSize size)
1300 {
1301 return drm_munmap(address, size);
1302 }
1303
drmGetBufInfo(int fd)1304 drm_public drmBufInfoPtr drmGetBufInfo(int fd)
1305 {
1306 drm_buf_info_t info;
1307 drmBufInfoPtr retval;
1308 int i;
1309
1310 memclear(info);
1311
1312 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1313 return NULL;
1314
1315 if (info.count) {
1316 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1317 return NULL;
1318
1319 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1320 drmFree(info.list);
1321 return NULL;
1322 }
1323
1324 retval = drmMalloc(sizeof(*retval));
1325 retval->count = info.count;
1326 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1327 for (i = 0; i < info.count; i++) {
1328 retval->list[i].count = info.list[i].count;
1329 retval->list[i].size = info.list[i].size;
1330 retval->list[i].low_mark = info.list[i].low_mark;
1331 retval->list[i].high_mark = info.list[i].high_mark;
1332 }
1333 drmFree(info.list);
1334 return retval;
1335 }
1336 return NULL;
1337 }
1338
1339 /**
1340 * Map all DMA buffers into client-virtual space.
1341 *
1342 * \param fd file descriptor.
1343 *
1344 * \return a pointer to a ::drmBufMap structure.
1345 *
1346 * \note The client may not use these buffers until obtaining buffer indices
1347 * with drmDMA().
1348 *
1349 * \internal
1350 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1351 * information about the buffers in a drm_buf_map structure into the
1352 * client-visible data structures.
1353 */
drmMapBufs(int fd)1354 drm_public drmBufMapPtr drmMapBufs(int fd)
1355 {
1356 drm_buf_map_t bufs;
1357 drmBufMapPtr retval;
1358 int i;
1359
1360 memclear(bufs);
1361 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
1362 return NULL;
1363
1364 if (!bufs.count)
1365 return NULL;
1366
1367 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1368 return NULL;
1369
1370 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
1371 drmFree(bufs.list);
1372 return NULL;
1373 }
1374
1375 retval = drmMalloc(sizeof(*retval));
1376 retval->count = bufs.count;
1377 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1378 for (i = 0; i < bufs.count; i++) {
1379 retval->list[i].idx = bufs.list[i].idx;
1380 retval->list[i].total = bufs.list[i].total;
1381 retval->list[i].used = 0;
1382 retval->list[i].address = bufs.list[i].address;
1383 }
1384
1385 drmFree(bufs.list);
1386 return retval;
1387 }
1388
1389
1390 /**
1391 * Unmap buffers allocated with drmMapBufs().
1392 *
1393 * \return zero on success, or negative value on failure.
1394 *
1395 * \internal
1396 * Calls munmap() for every buffer stored in \p bufs and frees the
1397 * memory allocated by drmMapBufs().
1398 */
drmUnmapBufs(drmBufMapPtr bufs)1399 drm_public int drmUnmapBufs(drmBufMapPtr bufs)
1400 {
1401 int i;
1402
1403 for (i = 0; i < bufs->count; i++) {
1404 drm_munmap(bufs->list[i].address, bufs->list[i].total);
1405 }
1406
1407 drmFree(bufs->list);
1408 drmFree(bufs);
1409 return 0;
1410 }
1411
1412
1413 #define DRM_DMA_RETRY 16
1414
1415 /**
1416 * Reserve DMA buffers.
1417 *
1418 * \param fd file descriptor.
1419 * \param request
1420 *
1421 * \return zero on success, or a negative value on failure.
1422 *
1423 * \internal
1424 * Assemble the arguments into a drm_dma structure and keeps issuing the
1425 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1426 */
drmDMA(int fd,drmDMAReqPtr request)1427 drm_public int drmDMA(int fd, drmDMAReqPtr request)
1428 {
1429 drm_dma_t dma;
1430 int ret, i = 0;
1431
1432 dma.context = request->context;
1433 dma.send_count = request->send_count;
1434 dma.send_indices = request->send_list;
1435 dma.send_sizes = request->send_sizes;
1436 dma.flags = request->flags;
1437 dma.request_count = request->request_count;
1438 dma.request_size = request->request_size;
1439 dma.request_indices = request->request_list;
1440 dma.request_sizes = request->request_sizes;
1441 dma.granted_count = 0;
1442
1443 do {
1444 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1445 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1446
1447 if ( ret == 0 ) {
1448 request->granted_count = dma.granted_count;
1449 return 0;
1450 } else {
1451 return -errno;
1452 }
1453 }
1454
1455
1456 /**
1457 * Obtain heavyweight hardware lock.
1458 *
1459 * \param fd file descriptor.
1460 * \param context context.
1461 * \param flags flags that determine the state of the hardware when the function
1462 * returns.
1463 *
1464 * \return always zero.
1465 *
1466 * \internal
1467 * This function translates the arguments into a drm_lock structure and issue
1468 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1469 */
drmGetLock(int fd,drm_context_t context,drmLockFlags flags)1470 drm_public int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
1471 {
1472 drm_lock_t lock;
1473
1474 memclear(lock);
1475 lock.context = context;
1476 lock.flags = 0;
1477 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1478 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1479 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1480 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1481 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1482 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1483
1484 while (drmIoctl(fd, DRM_IOCTL_LOCK, &lock))
1485 ;
1486 return 0;
1487 }
1488
1489 /**
1490 * Release the hardware lock.
1491 *
1492 * \param fd file descriptor.
1493 * \param context context.
1494 *
1495 * \return zero on success, or a negative value on failure.
1496 *
1497 * \internal
1498 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1499 * argument in a drm_lock structure.
1500 */
drmUnlock(int fd,drm_context_t context)1501 drm_public int drmUnlock(int fd, drm_context_t context)
1502 {
1503 drm_lock_t lock;
1504
1505 memclear(lock);
1506 lock.context = context;
1507 return drmIoctl(fd, DRM_IOCTL_UNLOCK, &lock);
1508 }
1509
drmGetReservedContextList(int fd,int * count)1510 drm_public drm_context_t *drmGetReservedContextList(int fd, int *count)
1511 {
1512 drm_ctx_res_t res;
1513 drm_ctx_t *list;
1514 drm_context_t * retval;
1515 int i;
1516
1517 memclear(res);
1518 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1519 return NULL;
1520
1521 if (!res.count)
1522 return NULL;
1523
1524 if (!(list = drmMalloc(res.count * sizeof(*list))))
1525 return NULL;
1526 if (!(retval = drmMalloc(res.count * sizeof(*retval))))
1527 goto err_free_list;
1528
1529 res.contexts = list;
1530 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1531 goto err_free_context;
1532
1533 for (i = 0; i < res.count; i++)
1534 retval[i] = list[i].handle;
1535 drmFree(list);
1536
1537 *count = res.count;
1538 return retval;
1539
1540 err_free_list:
1541 drmFree(list);
1542 err_free_context:
1543 drmFree(retval);
1544 return NULL;
1545 }
1546
drmFreeReservedContextList(drm_context_t * pt)1547 drm_public void drmFreeReservedContextList(drm_context_t *pt)
1548 {
1549 drmFree(pt);
1550 }
1551
1552 /**
1553 * Create context.
1554 *
1555 * Used by the X server during GLXContext initialization. This causes
1556 * per-context kernel-level resources to be allocated.
1557 *
1558 * \param fd file descriptor.
1559 * \param handle is set on success. To be used by the client when requesting DMA
1560 * dispatch with drmDMA().
1561 *
1562 * \return zero on success, or a negative value on failure.
1563 *
1564 * \note May only be called by root.
1565 *
1566 * \internal
1567 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1568 * argument in a drm_ctx structure.
1569 */
drmCreateContext(int fd,drm_context_t * handle)1570 drm_public int drmCreateContext(int fd, drm_context_t *handle)
1571 {
1572 drm_ctx_t ctx;
1573
1574 memclear(ctx);
1575 if (drmIoctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
1576 return -errno;
1577 *handle = ctx.handle;
1578 return 0;
1579 }
1580
drmSwitchToContext(int fd,drm_context_t context)1581 drm_public int drmSwitchToContext(int fd, drm_context_t context)
1582 {
1583 drm_ctx_t ctx;
1584
1585 memclear(ctx);
1586 ctx.handle = context;
1587 if (drmIoctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
1588 return -errno;
1589 return 0;
1590 }
1591
drmSetContextFlags(int fd,drm_context_t context,drm_context_tFlags flags)1592 drm_public int drmSetContextFlags(int fd, drm_context_t context,
1593 drm_context_tFlags flags)
1594 {
1595 drm_ctx_t ctx;
1596
1597 /*
1598 * Context preserving means that no context switches are done between DMA
1599 * buffers from one context and the next. This is suitable for use in the
1600 * X server (which promises to maintain hardware context), or in the
1601 * client-side library when buffers are swapped on behalf of two threads.
1602 */
1603 memclear(ctx);
1604 ctx.handle = context;
1605 if (flags & DRM_CONTEXT_PRESERVED)
1606 ctx.flags |= _DRM_CONTEXT_PRESERVED;
1607 if (flags & DRM_CONTEXT_2DONLY)
1608 ctx.flags |= _DRM_CONTEXT_2DONLY;
1609 if (drmIoctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
1610 return -errno;
1611 return 0;
1612 }
1613
drmGetContextFlags(int fd,drm_context_t context,drm_context_tFlagsPtr flags)1614 drm_public int drmGetContextFlags(int fd, drm_context_t context,
1615 drm_context_tFlagsPtr flags)
1616 {
1617 drm_ctx_t ctx;
1618
1619 memclear(ctx);
1620 ctx.handle = context;
1621 if (drmIoctl(fd, DRM_IOCTL_GET_CTX, &ctx))
1622 return -errno;
1623 *flags = 0;
1624 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
1625 *flags |= DRM_CONTEXT_PRESERVED;
1626 if (ctx.flags & _DRM_CONTEXT_2DONLY)
1627 *flags |= DRM_CONTEXT_2DONLY;
1628 return 0;
1629 }
1630
1631 /**
1632 * Destroy context.
1633 *
1634 * Free any kernel-level resources allocated with drmCreateContext() associated
1635 * with the context.
1636 *
1637 * \param fd file descriptor.
1638 * \param handle handle given by drmCreateContext().
1639 *
1640 * \return zero on success, or a negative value on failure.
1641 *
1642 * \note May only be called by root.
1643 *
1644 * \internal
1645 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1646 * argument in a drm_ctx structure.
1647 */
drmDestroyContext(int fd,drm_context_t handle)1648 drm_public int drmDestroyContext(int fd, drm_context_t handle)
1649 {
1650 drm_ctx_t ctx;
1651
1652 memclear(ctx);
1653 ctx.handle = handle;
1654 if (drmIoctl(fd, DRM_IOCTL_RM_CTX, &ctx))
1655 return -errno;
1656 return 0;
1657 }
1658
drmCreateDrawable(int fd,drm_drawable_t * handle)1659 drm_public int drmCreateDrawable(int fd, drm_drawable_t *handle)
1660 {
1661 drm_draw_t draw;
1662
1663 memclear(draw);
1664 if (drmIoctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
1665 return -errno;
1666 *handle = draw.handle;
1667 return 0;
1668 }
1669
drmDestroyDrawable(int fd,drm_drawable_t handle)1670 drm_public int drmDestroyDrawable(int fd, drm_drawable_t handle)
1671 {
1672 drm_draw_t draw;
1673
1674 memclear(draw);
1675 draw.handle = handle;
1676 if (drmIoctl(fd, DRM_IOCTL_RM_DRAW, &draw))
1677 return -errno;
1678 return 0;
1679 }
1680
drmUpdateDrawableInfo(int fd,drm_drawable_t handle,drm_drawable_info_type_t type,unsigned int num,void * data)1681 drm_public int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
1682 drm_drawable_info_type_t type,
1683 unsigned int num, void *data)
1684 {
1685 drm_update_draw_t update;
1686
1687 memclear(update);
1688 update.handle = handle;
1689 update.type = type;
1690 update.num = num;
1691 update.data = (unsigned long long)(unsigned long)data;
1692
1693 if (drmIoctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
1694 return -errno;
1695
1696 return 0;
1697 }
1698
drmCrtcGetSequence(int fd,uint32_t crtcId,uint64_t * sequence,uint64_t * ns)1699 drm_public int drmCrtcGetSequence(int fd, uint32_t crtcId, uint64_t *sequence,
1700 uint64_t *ns)
1701 {
1702 struct drm_crtc_get_sequence get_seq;
1703 int ret;
1704
1705 memclear(get_seq);
1706 get_seq.crtc_id = crtcId;
1707 ret = drmIoctl(fd, DRM_IOCTL_CRTC_GET_SEQUENCE, &get_seq);
1708 if (ret)
1709 return ret;
1710
1711 if (sequence)
1712 *sequence = get_seq.sequence;
1713 if (ns)
1714 *ns = get_seq.sequence_ns;
1715 return 0;
1716 }
1717
drmCrtcQueueSequence(int fd,uint32_t crtcId,uint32_t flags,uint64_t sequence,uint64_t * sequence_queued,uint64_t user_data)1718 drm_public int drmCrtcQueueSequence(int fd, uint32_t crtcId, uint32_t flags,
1719 uint64_t sequence,
1720 uint64_t *sequence_queued,
1721 uint64_t user_data)
1722 {
1723 struct drm_crtc_queue_sequence queue_seq;
1724 int ret;
1725
1726 memclear(queue_seq);
1727 queue_seq.crtc_id = crtcId;
1728 queue_seq.flags = flags;
1729 queue_seq.sequence = sequence;
1730 queue_seq.user_data = user_data;
1731
1732 ret = drmIoctl(fd, DRM_IOCTL_CRTC_QUEUE_SEQUENCE, &queue_seq);
1733 if (ret == 0 && sequence_queued)
1734 *sequence_queued = queue_seq.sequence;
1735
1736 return ret;
1737 }
1738
1739 /**
1740 * Acquire the AGP device.
1741 *
1742 * Must be called before any of the other AGP related calls.
1743 *
1744 * \param fd file descriptor.
1745 *
1746 * \return zero on success, or a negative value on failure.
1747 *
1748 * \internal
1749 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1750 */
drmAgpAcquire(int fd)1751 drm_public int drmAgpAcquire(int fd)
1752 {
1753 if (drmIoctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
1754 return -errno;
1755 return 0;
1756 }
1757
1758
1759 /**
1760 * Release the AGP device.
1761 *
1762 * \param fd file descriptor.
1763 *
1764 * \return zero on success, or a negative value on failure.
1765 *
1766 * \internal
1767 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1768 */
drmAgpRelease(int fd)1769 drm_public int drmAgpRelease(int fd)
1770 {
1771 if (drmIoctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
1772 return -errno;
1773 return 0;
1774 }
1775
1776
1777 /**
1778 * Set the AGP mode.
1779 *
1780 * \param fd file descriptor.
1781 * \param mode AGP mode.
1782 *
1783 * \return zero on success, or a negative value on failure.
1784 *
1785 * \internal
1786 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1787 * argument in a drm_agp_mode structure.
1788 */
drmAgpEnable(int fd,unsigned long mode)1789 drm_public int drmAgpEnable(int fd, unsigned long mode)
1790 {
1791 drm_agp_mode_t m;
1792
1793 memclear(m);
1794 m.mode = mode;
1795 if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
1796 return -errno;
1797 return 0;
1798 }
1799
1800
1801 /**
1802 * Allocate a chunk of AGP memory.
1803 *
1804 * \param fd file descriptor.
1805 * \param size requested memory size in bytes. Will be rounded to page boundary.
1806 * \param type type of memory to allocate.
1807 * \param address if not zero, will be set to the physical address of the
1808 * allocated memory.
1809 * \param handle on success will be set to a handle of the allocated memory.
1810 *
1811 * \return zero on success, or a negative value on failure.
1812 *
1813 * \internal
1814 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1815 * arguments in a drm_agp_buffer structure.
1816 */
drmAgpAlloc(int fd,unsigned long size,unsigned long type,unsigned long * address,drm_handle_t * handle)1817 drm_public int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
1818 unsigned long *address, drm_handle_t *handle)
1819 {
1820 drm_agp_buffer_t b;
1821
1822 memclear(b);
1823 *handle = DRM_AGP_NO_HANDLE;
1824 b.size = size;
1825 b.type = type;
1826 if (drmIoctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
1827 return -errno;
1828 if (address != 0UL)
1829 *address = b.physical;
1830 *handle = b.handle;
1831 return 0;
1832 }
1833
1834
1835 /**
1836 * Free a chunk of AGP memory.
1837 *
1838 * \param fd file descriptor.
1839 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1840 *
1841 * \return zero on success, or a negative value on failure.
1842 *
1843 * \internal
1844 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1845 * argument in a drm_agp_buffer structure.
1846 */
drmAgpFree(int fd,drm_handle_t handle)1847 drm_public int drmAgpFree(int fd, drm_handle_t handle)
1848 {
1849 drm_agp_buffer_t b;
1850
1851 memclear(b);
1852 b.handle = handle;
1853 if (drmIoctl(fd, DRM_IOCTL_AGP_FREE, &b))
1854 return -errno;
1855 return 0;
1856 }
1857
1858
1859 /**
1860 * Bind a chunk of AGP memory.
1861 *
1862 * \param fd file descriptor.
1863 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1864 * \param offset offset in bytes. It will round to page boundary.
1865 *
1866 * \return zero on success, or a negative value on failure.
1867 *
1868 * \internal
1869 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1870 * argument in a drm_agp_binding structure.
1871 */
drmAgpBind(int fd,drm_handle_t handle,unsigned long offset)1872 drm_public int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
1873 {
1874 drm_agp_binding_t b;
1875
1876 memclear(b);
1877 b.handle = handle;
1878 b.offset = offset;
1879 if (drmIoctl(fd, DRM_IOCTL_AGP_BIND, &b))
1880 return -errno;
1881 return 0;
1882 }
1883
1884
1885 /**
1886 * Unbind a chunk of AGP memory.
1887 *
1888 * \param fd file descriptor.
1889 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1890 *
1891 * \return zero on success, or a negative value on failure.
1892 *
1893 * \internal
1894 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1895 * the argument in a drm_agp_binding structure.
1896 */
drmAgpUnbind(int fd,drm_handle_t handle)1897 drm_public int drmAgpUnbind(int fd, drm_handle_t handle)
1898 {
1899 drm_agp_binding_t b;
1900
1901 memclear(b);
1902 b.handle = handle;
1903 if (drmIoctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
1904 return -errno;
1905 return 0;
1906 }
1907
1908
1909 /**
1910 * Get AGP driver major version number.
1911 *
1912 * \param fd file descriptor.
1913 *
1914 * \return major version number on success, or a negative value on failure..
1915 *
1916 * \internal
1917 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1918 * necessary information in a drm_agp_info structure.
1919 */
drmAgpVersionMajor(int fd)1920 drm_public int drmAgpVersionMajor(int fd)
1921 {
1922 drm_agp_info_t i;
1923
1924 memclear(i);
1925
1926 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1927 return -errno;
1928 return i.agp_version_major;
1929 }
1930
1931
1932 /**
1933 * Get AGP driver minor version number.
1934 *
1935 * \param fd file descriptor.
1936 *
1937 * \return minor version number on success, or a negative value on failure.
1938 *
1939 * \internal
1940 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1941 * necessary information in a drm_agp_info structure.
1942 */
drmAgpVersionMinor(int fd)1943 drm_public int drmAgpVersionMinor(int fd)
1944 {
1945 drm_agp_info_t i;
1946
1947 memclear(i);
1948
1949 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1950 return -errno;
1951 return i.agp_version_minor;
1952 }
1953
1954
1955 /**
1956 * Get AGP mode.
1957 *
1958 * \param fd file descriptor.
1959 *
1960 * \return mode on success, or zero on failure.
1961 *
1962 * \internal
1963 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1964 * necessary information in a drm_agp_info structure.
1965 */
drmAgpGetMode(int fd)1966 drm_public unsigned long drmAgpGetMode(int fd)
1967 {
1968 drm_agp_info_t i;
1969
1970 memclear(i);
1971
1972 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1973 return 0;
1974 return i.mode;
1975 }
1976
1977
1978 /**
1979 * Get AGP aperture base.
1980 *
1981 * \param fd file descriptor.
1982 *
1983 * \return aperture base on success, zero on failure.
1984 *
1985 * \internal
1986 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1987 * necessary information in a drm_agp_info structure.
1988 */
drmAgpBase(int fd)1989 drm_public unsigned long drmAgpBase(int fd)
1990 {
1991 drm_agp_info_t i;
1992
1993 memclear(i);
1994
1995 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1996 return 0;
1997 return i.aperture_base;
1998 }
1999
2000
2001 /**
2002 * Get AGP aperture size.
2003 *
2004 * \param fd file descriptor.
2005 *
2006 * \return aperture size on success, zero on failure.
2007 *
2008 * \internal
2009 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2010 * necessary information in a drm_agp_info structure.
2011 */
drmAgpSize(int fd)2012 drm_public unsigned long drmAgpSize(int fd)
2013 {
2014 drm_agp_info_t i;
2015
2016 memclear(i);
2017
2018 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2019 return 0;
2020 return i.aperture_size;
2021 }
2022
2023
2024 /**
2025 * Get used AGP memory.
2026 *
2027 * \param fd file descriptor.
2028 *
2029 * \return memory used on success, or zero on failure.
2030 *
2031 * \internal
2032 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2033 * necessary information in a drm_agp_info structure.
2034 */
drmAgpMemoryUsed(int fd)2035 drm_public unsigned long drmAgpMemoryUsed(int fd)
2036 {
2037 drm_agp_info_t i;
2038
2039 memclear(i);
2040
2041 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2042 return 0;
2043 return i.memory_used;
2044 }
2045
2046
2047 /**
2048 * Get available AGP memory.
2049 *
2050 * \param fd file descriptor.
2051 *
2052 * \return memory available on success, or zero on failure.
2053 *
2054 * \internal
2055 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2056 * necessary information in a drm_agp_info structure.
2057 */
drmAgpMemoryAvail(int fd)2058 drm_public unsigned long drmAgpMemoryAvail(int fd)
2059 {
2060 drm_agp_info_t i;
2061
2062 memclear(i);
2063
2064 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2065 return 0;
2066 return i.memory_allowed;
2067 }
2068
2069
2070 /**
2071 * Get hardware vendor ID.
2072 *
2073 * \param fd file descriptor.
2074 *
2075 * \return vendor ID on success, or zero on failure.
2076 *
2077 * \internal
2078 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2079 * necessary information in a drm_agp_info structure.
2080 */
drmAgpVendorId(int fd)2081 drm_public unsigned int drmAgpVendorId(int fd)
2082 {
2083 drm_agp_info_t i;
2084
2085 memclear(i);
2086
2087 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2088 return 0;
2089 return i.id_vendor;
2090 }
2091
2092
2093 /**
2094 * Get hardware device ID.
2095 *
2096 * \param fd file descriptor.
2097 *
2098 * \return zero on success, or zero on failure.
2099 *
2100 * \internal
2101 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2102 * necessary information in a drm_agp_info structure.
2103 */
drmAgpDeviceId(int fd)2104 drm_public unsigned int drmAgpDeviceId(int fd)
2105 {
2106 drm_agp_info_t i;
2107
2108 memclear(i);
2109
2110 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2111 return 0;
2112 return i.id_device;
2113 }
2114
drmScatterGatherAlloc(int fd,unsigned long size,drm_handle_t * handle)2115 drm_public int drmScatterGatherAlloc(int fd, unsigned long size,
2116 drm_handle_t *handle)
2117 {
2118 drm_scatter_gather_t sg;
2119
2120 memclear(sg);
2121
2122 *handle = 0;
2123 sg.size = size;
2124 if (drmIoctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
2125 return -errno;
2126 *handle = sg.handle;
2127 return 0;
2128 }
2129
drmScatterGatherFree(int fd,drm_handle_t handle)2130 drm_public int drmScatterGatherFree(int fd, drm_handle_t handle)
2131 {
2132 drm_scatter_gather_t sg;
2133
2134 memclear(sg);
2135 sg.handle = handle;
2136 if (drmIoctl(fd, DRM_IOCTL_SG_FREE, &sg))
2137 return -errno;
2138 return 0;
2139 }
2140
2141 /**
2142 * Wait for VBLANK.
2143 *
2144 * \param fd file descriptor.
2145 * \param vbl pointer to a drmVBlank structure.
2146 *
2147 * \return zero on success, or a negative value on failure.
2148 *
2149 * \internal
2150 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
2151 */
drmWaitVBlank(int fd,drmVBlankPtr vbl)2152 drm_public int drmWaitVBlank(int fd, drmVBlankPtr vbl)
2153 {
2154 struct timespec timeout, cur;
2155 int ret;
2156
2157 ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
2158 if (ret < 0) {
2159 fprintf(stderr, "clock_gettime failed: %s\n", strerror(errno));
2160 goto out;
2161 }
2162 timeout.tv_sec++;
2163
2164 do {
2165 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
2166 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
2167 if (ret && errno == EINTR) {
2168 clock_gettime(CLOCK_MONOTONIC, &cur);
2169 /* Timeout after 1s */
2170 if (cur.tv_sec > timeout.tv_sec + 1 ||
2171 (cur.tv_sec == timeout.tv_sec && cur.tv_nsec >=
2172 timeout.tv_nsec)) {
2173 errno = EBUSY;
2174 ret = -1;
2175 break;
2176 }
2177 }
2178 } while (ret && errno == EINTR);
2179
2180 out:
2181 return ret;
2182 }
2183
drmError(int err,const char * label)2184 drm_public int drmError(int err, const char *label)
2185 {
2186 switch (err) {
2187 case DRM_ERR_NO_DEVICE:
2188 fprintf(stderr, "%s: no device\n", label);
2189 break;
2190 case DRM_ERR_NO_ACCESS:
2191 fprintf(stderr, "%s: no access\n", label);
2192 break;
2193 case DRM_ERR_NOT_ROOT:
2194 fprintf(stderr, "%s: not root\n", label);
2195 break;
2196 case DRM_ERR_INVALID:
2197 fprintf(stderr, "%s: invalid args\n", label);
2198 break;
2199 default:
2200 if (err < 0)
2201 err = -err;
2202 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
2203 break;
2204 }
2205
2206 return 1;
2207 }
2208
2209 /**
2210 * Install IRQ handler.
2211 *
2212 * \param fd file descriptor.
2213 * \param irq IRQ number.
2214 *
2215 * \return zero on success, or a negative value on failure.
2216 *
2217 * \internal
2218 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2219 * argument in a drm_control structure.
2220 */
drmCtlInstHandler(int fd,int irq)2221 drm_public int drmCtlInstHandler(int fd, int irq)
2222 {
2223 drm_control_t ctl;
2224
2225 memclear(ctl);
2226 ctl.func = DRM_INST_HANDLER;
2227 ctl.irq = irq;
2228 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2229 return -errno;
2230 return 0;
2231 }
2232
2233
2234 /**
2235 * Uninstall IRQ handler.
2236 *
2237 * \param fd file descriptor.
2238 *
2239 * \return zero on success, or a negative value on failure.
2240 *
2241 * \internal
2242 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2243 * argument in a drm_control structure.
2244 */
drmCtlUninstHandler(int fd)2245 drm_public int drmCtlUninstHandler(int fd)
2246 {
2247 drm_control_t ctl;
2248
2249 memclear(ctl);
2250 ctl.func = DRM_UNINST_HANDLER;
2251 ctl.irq = 0;
2252 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2253 return -errno;
2254 return 0;
2255 }
2256
drmFinish(int fd,int context,drmLockFlags flags)2257 drm_public int drmFinish(int fd, int context, drmLockFlags flags)
2258 {
2259 drm_lock_t lock;
2260
2261 memclear(lock);
2262 lock.context = context;
2263 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
2264 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
2265 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
2266 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
2267 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
2268 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
2269 if (drmIoctl(fd, DRM_IOCTL_FINISH, &lock))
2270 return -errno;
2271 return 0;
2272 }
2273
2274 /**
2275 * Get IRQ from bus ID.
2276 *
2277 * \param fd file descriptor.
2278 * \param busnum bus number.
2279 * \param devnum device number.
2280 * \param funcnum function number.
2281 *
2282 * \return IRQ number on success, or a negative value on failure.
2283 *
2284 * \internal
2285 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
2286 * arguments in a drm_irq_busid structure.
2287 */
drmGetInterruptFromBusID(int fd,int busnum,int devnum,int funcnum)2288 drm_public int drmGetInterruptFromBusID(int fd, int busnum, int devnum,
2289 int funcnum)
2290 {
2291 drm_irq_busid_t p;
2292
2293 memclear(p);
2294 p.busnum = busnum;
2295 p.devnum = devnum;
2296 p.funcnum = funcnum;
2297 if (drmIoctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
2298 return -errno;
2299 return p.irq;
2300 }
2301
drmAddContextTag(int fd,drm_context_t context,void * tag)2302 drm_public int drmAddContextTag(int fd, drm_context_t context, void *tag)
2303 {
2304 drmHashEntry *entry = drmGetEntry(fd);
2305
2306 if (drmHashInsert(entry->tagTable, context, tag)) {
2307 drmHashDelete(entry->tagTable, context);
2308 drmHashInsert(entry->tagTable, context, tag);
2309 }
2310 return 0;
2311 }
2312
drmDelContextTag(int fd,drm_context_t context)2313 drm_public int drmDelContextTag(int fd, drm_context_t context)
2314 {
2315 drmHashEntry *entry = drmGetEntry(fd);
2316
2317 return drmHashDelete(entry->tagTable, context);
2318 }
2319
drmGetContextTag(int fd,drm_context_t context)2320 drm_public void *drmGetContextTag(int fd, drm_context_t context)
2321 {
2322 drmHashEntry *entry = drmGetEntry(fd);
2323 void *value;
2324
2325 if (drmHashLookup(entry->tagTable, context, &value))
2326 return NULL;
2327
2328 return value;
2329 }
2330
drmAddContextPrivateMapping(int fd,drm_context_t ctx_id,drm_handle_t handle)2331 drm_public int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2332 drm_handle_t handle)
2333 {
2334 drm_ctx_priv_map_t map;
2335
2336 memclear(map);
2337 map.ctx_id = ctx_id;
2338 map.handle = (void *)(uintptr_t)handle;
2339
2340 if (drmIoctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
2341 return -errno;
2342 return 0;
2343 }
2344
drmGetContextPrivateMapping(int fd,drm_context_t ctx_id,drm_handle_t * handle)2345 drm_public int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2346 drm_handle_t *handle)
2347 {
2348 drm_ctx_priv_map_t map;
2349
2350 memclear(map);
2351 map.ctx_id = ctx_id;
2352
2353 if (drmIoctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
2354 return -errno;
2355 if (handle)
2356 *handle = (drm_handle_t)(uintptr_t)map.handle;
2357
2358 return 0;
2359 }
2360
drmGetMap(int fd,int idx,drm_handle_t * offset,drmSize * size,drmMapType * type,drmMapFlags * flags,drm_handle_t * handle,int * mtrr)2361 drm_public int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
2362 drmMapType *type, drmMapFlags *flags,
2363 drm_handle_t *handle, int *mtrr)
2364 {
2365 drm_map_t map;
2366
2367 memclear(map);
2368 map.offset = idx;
2369 if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map))
2370 return -errno;
2371 *offset = map.offset;
2372 *size = map.size;
2373 *type = map.type;
2374 *flags = map.flags;
2375 *handle = (unsigned long)map.handle;
2376 *mtrr = map.mtrr;
2377 return 0;
2378 }
2379
drmGetClient(int fd,int idx,int * auth,int * pid,int * uid,unsigned long * magic,unsigned long * iocs)2380 drm_public int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
2381 unsigned long *magic, unsigned long *iocs)
2382 {
2383 drm_client_t client;
2384
2385 memclear(client);
2386 client.idx = idx;
2387 if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client))
2388 return -errno;
2389 *auth = client.auth;
2390 *pid = client.pid;
2391 *uid = client.uid;
2392 *magic = client.magic;
2393 *iocs = client.iocs;
2394 return 0;
2395 }
2396
drmGetStats(int fd,drmStatsT * stats)2397 drm_public int drmGetStats(int fd, drmStatsT *stats)
2398 {
2399 drm_stats_t s;
2400 unsigned i;
2401
2402 memclear(s);
2403 if (drmIoctl(fd, DRM_IOCTL_GET_STATS, &s))
2404 return -errno;
2405
2406 stats->count = 0;
2407 memset(stats, 0, sizeof(*stats));
2408 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
2409 return -1;
2410
2411 #define SET_VALUE \
2412 stats->data[i].long_format = "%-20.20s"; \
2413 stats->data[i].rate_format = "%8.8s"; \
2414 stats->data[i].isvalue = 1; \
2415 stats->data[i].verbose = 0
2416
2417 #define SET_COUNT \
2418 stats->data[i].long_format = "%-20.20s"; \
2419 stats->data[i].rate_format = "%5.5s"; \
2420 stats->data[i].isvalue = 0; \
2421 stats->data[i].mult_names = "kgm"; \
2422 stats->data[i].mult = 1000; \
2423 stats->data[i].verbose = 0
2424
2425 #define SET_BYTE \
2426 stats->data[i].long_format = "%-20.20s"; \
2427 stats->data[i].rate_format = "%5.5s"; \
2428 stats->data[i].isvalue = 0; \
2429 stats->data[i].mult_names = "KGM"; \
2430 stats->data[i].mult = 1024; \
2431 stats->data[i].verbose = 0
2432
2433
2434 stats->count = s.count;
2435 for (i = 0; i < s.count; i++) {
2436 stats->data[i].value = s.data[i].value;
2437 switch (s.data[i].type) {
2438 case _DRM_STAT_LOCK:
2439 stats->data[i].long_name = "Lock";
2440 stats->data[i].rate_name = "Lock";
2441 SET_VALUE;
2442 break;
2443 case _DRM_STAT_OPENS:
2444 stats->data[i].long_name = "Opens";
2445 stats->data[i].rate_name = "O";
2446 SET_COUNT;
2447 stats->data[i].verbose = 1;
2448 break;
2449 case _DRM_STAT_CLOSES:
2450 stats->data[i].long_name = "Closes";
2451 stats->data[i].rate_name = "Lock";
2452 SET_COUNT;
2453 stats->data[i].verbose = 1;
2454 break;
2455 case _DRM_STAT_IOCTLS:
2456 stats->data[i].long_name = "Ioctls";
2457 stats->data[i].rate_name = "Ioc/s";
2458 SET_COUNT;
2459 break;
2460 case _DRM_STAT_LOCKS:
2461 stats->data[i].long_name = "Locks";
2462 stats->data[i].rate_name = "Lck/s";
2463 SET_COUNT;
2464 break;
2465 case _DRM_STAT_UNLOCKS:
2466 stats->data[i].long_name = "Unlocks";
2467 stats->data[i].rate_name = "Unl/s";
2468 SET_COUNT;
2469 break;
2470 case _DRM_STAT_IRQ:
2471 stats->data[i].long_name = "IRQs";
2472 stats->data[i].rate_name = "IRQ/s";
2473 SET_COUNT;
2474 break;
2475 case _DRM_STAT_PRIMARY:
2476 stats->data[i].long_name = "Primary Bytes";
2477 stats->data[i].rate_name = "PB/s";
2478 SET_BYTE;
2479 break;
2480 case _DRM_STAT_SECONDARY:
2481 stats->data[i].long_name = "Secondary Bytes";
2482 stats->data[i].rate_name = "SB/s";
2483 SET_BYTE;
2484 break;
2485 case _DRM_STAT_DMA:
2486 stats->data[i].long_name = "DMA";
2487 stats->data[i].rate_name = "DMA/s";
2488 SET_COUNT;
2489 break;
2490 case _DRM_STAT_SPECIAL:
2491 stats->data[i].long_name = "Special DMA";
2492 stats->data[i].rate_name = "dma/s";
2493 SET_COUNT;
2494 break;
2495 case _DRM_STAT_MISSED:
2496 stats->data[i].long_name = "Miss";
2497 stats->data[i].rate_name = "Ms/s";
2498 SET_COUNT;
2499 break;
2500 case _DRM_STAT_VALUE:
2501 stats->data[i].long_name = "Value";
2502 stats->data[i].rate_name = "Value";
2503 SET_VALUE;
2504 break;
2505 case _DRM_STAT_BYTE:
2506 stats->data[i].long_name = "Bytes";
2507 stats->data[i].rate_name = "B/s";
2508 SET_BYTE;
2509 break;
2510 case _DRM_STAT_COUNT:
2511 default:
2512 stats->data[i].long_name = "Count";
2513 stats->data[i].rate_name = "Cnt/s";
2514 SET_COUNT;
2515 break;
2516 }
2517 }
2518 return 0;
2519 }
2520
2521 /**
2522 * Issue a set-version ioctl.
2523 *
2524 * \param fd file descriptor.
2525 * \param drmCommandIndex command index
2526 * \param data source pointer of the data to be read and written.
2527 * \param size size of the data to be read and written.
2528 *
2529 * \return zero on success, or a negative value on failure.
2530 *
2531 * \internal
2532 * It issues a read-write ioctl given by
2533 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2534 */
drmSetInterfaceVersion(int fd,drmSetVersion * version)2535 drm_public int drmSetInterfaceVersion(int fd, drmSetVersion *version)
2536 {
2537 int retcode = 0;
2538 drm_set_version_t sv;
2539
2540 memclear(sv);
2541 sv.drm_di_major = version->drm_di_major;
2542 sv.drm_di_minor = version->drm_di_minor;
2543 sv.drm_dd_major = version->drm_dd_major;
2544 sv.drm_dd_minor = version->drm_dd_minor;
2545
2546 if (drmIoctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
2547 retcode = -errno;
2548 }
2549
2550 version->drm_di_major = sv.drm_di_major;
2551 version->drm_di_minor = sv.drm_di_minor;
2552 version->drm_dd_major = sv.drm_dd_major;
2553 version->drm_dd_minor = sv.drm_dd_minor;
2554
2555 return retcode;
2556 }
2557
2558 /**
2559 * Send a device-specific command.
2560 *
2561 * \param fd file descriptor.
2562 * \param drmCommandIndex command index
2563 *
2564 * \return zero on success, or a negative value on failure.
2565 *
2566 * \internal
2567 * It issues a ioctl given by
2568 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2569 */
drmCommandNone(int fd,unsigned long drmCommandIndex)2570 drm_public int drmCommandNone(int fd, unsigned long drmCommandIndex)
2571 {
2572 unsigned long request;
2573
2574 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2575
2576 if (drmIoctl(fd, request, NULL)) {
2577 return -errno;
2578 }
2579 return 0;
2580 }
2581
2582
2583 /**
2584 * Send a device-specific read command.
2585 *
2586 * \param fd file descriptor.
2587 * \param drmCommandIndex command index
2588 * \param data destination pointer of the data to be read.
2589 * \param size size of the data to be read.
2590 *
2591 * \return zero on success, or a negative value on failure.
2592 *
2593 * \internal
2594 * It issues a read ioctl given by
2595 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2596 */
drmCommandRead(int fd,unsigned long drmCommandIndex,void * data,unsigned long size)2597 drm_public int drmCommandRead(int fd, unsigned long drmCommandIndex,
2598 void *data, unsigned long size)
2599 {
2600 unsigned long request;
2601
2602 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2603 DRM_COMMAND_BASE + drmCommandIndex, size);
2604
2605 if (drmIoctl(fd, request, data)) {
2606 return -errno;
2607 }
2608 return 0;
2609 }
2610
2611
2612 /**
2613 * Send a device-specific write command.
2614 *
2615 * \param fd file descriptor.
2616 * \param drmCommandIndex command index
2617 * \param data source pointer of the data to be written.
2618 * \param size size of the data to be written.
2619 *
2620 * \return zero on success, or a negative value on failure.
2621 *
2622 * \internal
2623 * It issues a write ioctl given by
2624 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2625 */
drmCommandWrite(int fd,unsigned long drmCommandIndex,void * data,unsigned long size)2626 drm_public int drmCommandWrite(int fd, unsigned long drmCommandIndex,
2627 void *data, unsigned long size)
2628 {
2629 unsigned long request;
2630
2631 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2632 DRM_COMMAND_BASE + drmCommandIndex, size);
2633
2634 if (drmIoctl(fd, request, data)) {
2635 return -errno;
2636 }
2637 return 0;
2638 }
2639
2640
2641 /**
2642 * Send a device-specific read-write command.
2643 *
2644 * \param fd file descriptor.
2645 * \param drmCommandIndex command index
2646 * \param data source pointer of the data to be read and written.
2647 * \param size size of the data to be read and written.
2648 *
2649 * \return zero on success, or a negative value on failure.
2650 *
2651 * \internal
2652 * It issues a read-write ioctl given by
2653 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2654 */
drmCommandWriteRead(int fd,unsigned long drmCommandIndex,void * data,unsigned long size)2655 drm_public int drmCommandWriteRead(int fd, unsigned long drmCommandIndex,
2656 void *data, unsigned long size)
2657 {
2658 unsigned long request;
2659
2660 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2661 DRM_COMMAND_BASE + drmCommandIndex, size);
2662
2663 if (drmIoctl(fd, request, data))
2664 return -errno;
2665 return 0;
2666 }
2667
2668 #define DRM_MAX_FDS 16
2669 static struct {
2670 char *BusID;
2671 int fd;
2672 int refcount;
2673 int type;
2674 } connection[DRM_MAX_FDS];
2675
2676 static int nr_fds = 0;
2677
drmOpenOnce(void * unused,const char * BusID,int * newlyopened)2678 drm_public int drmOpenOnce(void *unused, const char *BusID, int *newlyopened)
2679 {
2680 return drmOpenOnceWithType(BusID, newlyopened, DRM_NODE_PRIMARY);
2681 }
2682
drmOpenOnceWithType(const char * BusID,int * newlyopened,int type)2683 drm_public int drmOpenOnceWithType(const char *BusID, int *newlyopened,
2684 int type)
2685 {
2686 int i;
2687 int fd;
2688
2689 for (i = 0; i < nr_fds; i++)
2690 if ((strcmp(BusID, connection[i].BusID) == 0) &&
2691 (connection[i].type == type)) {
2692 connection[i].refcount++;
2693 *newlyopened = 0;
2694 return connection[i].fd;
2695 }
2696
2697 fd = drmOpenWithType(NULL, BusID, type);
2698 if (fd < 0 || nr_fds == DRM_MAX_FDS)
2699 return fd;
2700
2701 connection[nr_fds].BusID = strdup(BusID);
2702 connection[nr_fds].fd = fd;
2703 connection[nr_fds].refcount = 1;
2704 connection[nr_fds].type = type;
2705 *newlyopened = 1;
2706
2707 if (0)
2708 fprintf(stderr, "saved connection %d for %s %d\n",
2709 nr_fds, connection[nr_fds].BusID,
2710 strcmp(BusID, connection[nr_fds].BusID));
2711
2712 nr_fds++;
2713
2714 return fd;
2715 }
2716
drmCloseOnce(int fd)2717 drm_public void drmCloseOnce(int fd)
2718 {
2719 int i;
2720
2721 for (i = 0; i < nr_fds; i++) {
2722 if (fd == connection[i].fd) {
2723 if (--connection[i].refcount == 0) {
2724 drmClose(connection[i].fd);
2725 free(connection[i].BusID);
2726
2727 if (i < --nr_fds)
2728 connection[i] = connection[nr_fds];
2729
2730 return;
2731 }
2732 }
2733 }
2734 }
2735
drmSetMaster(int fd)2736 drm_public int drmSetMaster(int fd)
2737 {
2738 return drmIoctl(fd, DRM_IOCTL_SET_MASTER, NULL);
2739 }
2740
drmDropMaster(int fd)2741 drm_public int drmDropMaster(int fd)
2742 {
2743 return drmIoctl(fd, DRM_IOCTL_DROP_MASTER, NULL);
2744 }
2745
drmIsMaster(int fd)2746 drm_public int drmIsMaster(int fd)
2747 {
2748 /* Detect master by attempting something that requires master.
2749 *
2750 * Authenticating magic tokens requires master and 0 is an
2751 * internal kernel detail which we could use. Attempting this on
2752 * a master fd would fail therefore fail with EINVAL because 0
2753 * is invalid.
2754 *
2755 * A non-master fd will fail with EACCES, as the kernel checks
2756 * for master before attempting to do anything else.
2757 *
2758 * Since we don't want to leak implementation details, use
2759 * EACCES.
2760 */
2761 return drmAuthMagic(fd, 0) != -EACCES;
2762 }
2763
drmGetDeviceNameFromFd(int fd)2764 drm_public char *drmGetDeviceNameFromFd(int fd)
2765 {
2766 char name[128];
2767 struct stat sbuf;
2768 dev_t d;
2769 int i;
2770
2771 /* The whole drmOpen thing is a fiasco and we need to find a way
2772 * back to just using open(2). For now, however, lets just make
2773 * things worse with even more ad hoc directory walking code to
2774 * discover the device file name. */
2775
2776 fstat(fd, &sbuf);
2777 d = sbuf.st_rdev;
2778
2779 for (i = 0; i < DRM_MAX_MINOR; i++) {
2780 snprintf(name, sizeof name, DRM_DEV_NAME, DRM_DIR_NAME, i);
2781 if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d)
2782 break;
2783 }
2784 if (i == DRM_MAX_MINOR)
2785 return NULL;
2786
2787 return strdup(name);
2788 }
2789
drmNodeIsDRM(int maj,int min)2790 static bool drmNodeIsDRM(int maj, int min)
2791 {
2792 #ifdef __linux__
2793 char path[64];
2794 struct stat sbuf;
2795
2796 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device/drm",
2797 maj, min);
2798 return stat(path, &sbuf) == 0;
2799 #else
2800 return maj == DRM_MAJOR;
2801 #endif
2802 }
2803
drmGetNodeTypeFromFd(int fd)2804 drm_public int drmGetNodeTypeFromFd(int fd)
2805 {
2806 struct stat sbuf;
2807 int maj, min, type;
2808
2809 if (fstat(fd, &sbuf))
2810 return -1;
2811
2812 maj = major(sbuf.st_rdev);
2813 min = minor(sbuf.st_rdev);
2814
2815 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode)) {
2816 errno = EINVAL;
2817 return -1;
2818 }
2819
2820 type = drmGetMinorType(min);
2821 if (type == -1)
2822 errno = ENODEV;
2823 return type;
2824 }
2825
drmPrimeHandleToFD(int fd,uint32_t handle,uint32_t flags,int * prime_fd)2826 drm_public int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags,
2827 int *prime_fd)
2828 {
2829 struct drm_prime_handle args;
2830 int ret;
2831
2832 memclear(args);
2833 args.fd = -1;
2834 args.handle = handle;
2835 args.flags = flags;
2836 ret = drmIoctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
2837 if (ret)
2838 return ret;
2839
2840 *prime_fd = args.fd;
2841 return 0;
2842 }
2843
drmPrimeFDToHandle(int fd,int prime_fd,uint32_t * handle)2844 drm_public int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
2845 {
2846 struct drm_prime_handle args;
2847 int ret;
2848
2849 memclear(args);
2850 args.fd = prime_fd;
2851 ret = drmIoctl(fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);
2852 if (ret)
2853 return ret;
2854
2855 *handle = args.handle;
2856 return 0;
2857 }
2858
drmGetMinorNameForFD(int fd,int type)2859 static char *drmGetMinorNameForFD(int fd, int type)
2860 {
2861 #ifdef __linux__
2862 DIR *sysdir;
2863 struct dirent *ent;
2864 struct stat sbuf;
2865 const char *name = drmGetMinorName(type);
2866 int len;
2867 char dev_name[64], buf[64];
2868 int maj, min;
2869
2870 if (!name)
2871 return NULL;
2872
2873 len = strlen(name);
2874
2875 if (fstat(fd, &sbuf))
2876 return NULL;
2877
2878 maj = major(sbuf.st_rdev);
2879 min = minor(sbuf.st_rdev);
2880
2881 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
2882 return NULL;
2883
2884 snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
2885
2886 sysdir = opendir(buf);
2887 if (!sysdir)
2888 return NULL;
2889
2890 while ((ent = readdir(sysdir))) {
2891 if (strncmp(ent->d_name, name, len) == 0) {
2892 snprintf(dev_name, sizeof(dev_name), DRM_DIR_NAME "/%s",
2893 ent->d_name);
2894
2895 closedir(sysdir);
2896 return strdup(dev_name);
2897 }
2898 }
2899
2900 closedir(sysdir);
2901 return NULL;
2902 #else
2903 struct stat sbuf;
2904 char buf[PATH_MAX + 1];
2905 const char *dev_name;
2906 unsigned int maj, min;
2907 int n, base;
2908
2909 if (fstat(fd, &sbuf))
2910 return NULL;
2911
2912 maj = major(sbuf.st_rdev);
2913 min = minor(sbuf.st_rdev);
2914
2915 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
2916 return NULL;
2917
2918 switch (type) {
2919 case DRM_NODE_PRIMARY:
2920 dev_name = DRM_DEV_NAME;
2921 break;
2922 case DRM_NODE_CONTROL:
2923 dev_name = DRM_CONTROL_DEV_NAME;
2924 break;
2925 case DRM_NODE_RENDER:
2926 dev_name = DRM_RENDER_DEV_NAME;
2927 break;
2928 default:
2929 return NULL;
2930 };
2931
2932 base = drmGetMinorBase(type);
2933 if (base < 0)
2934 return NULL;
2935
2936 n = snprintf(buf, sizeof(buf), dev_name, DRM_DIR_NAME, min - base);
2937 if (n == -1 || n >= sizeof(buf))
2938 return NULL;
2939
2940 return strdup(buf);
2941 #endif
2942 }
2943
drmGetPrimaryDeviceNameFromFd(int fd)2944 drm_public char *drmGetPrimaryDeviceNameFromFd(int fd)
2945 {
2946 return drmGetMinorNameForFD(fd, DRM_NODE_PRIMARY);
2947 }
2948
drmGetRenderDeviceNameFromFd(int fd)2949 drm_public char *drmGetRenderDeviceNameFromFd(int fd)
2950 {
2951 return drmGetMinorNameForFD(fd, DRM_NODE_RENDER);
2952 }
2953
2954 #ifdef __linux__
2955 static char * DRM_PRINTFLIKE(2, 3)
sysfs_uevent_get(const char * path,const char * fmt,...)2956 sysfs_uevent_get(const char *path, const char *fmt, ...)
2957 {
2958 char filename[PATH_MAX + 1], *key, *line = NULL, *value = NULL;
2959 size_t size = 0, len;
2960 ssize_t num;
2961 va_list ap;
2962 FILE *fp;
2963
2964 va_start(ap, fmt);
2965 num = vasprintf(&key, fmt, ap);
2966 va_end(ap);
2967 len = num;
2968
2969 snprintf(filename, sizeof(filename), "%s/uevent", path);
2970
2971 fp = fopen(filename, "r");
2972 if (!fp) {
2973 free(key);
2974 return NULL;
2975 }
2976
2977 while ((num = getline(&line, &size, fp)) >= 0) {
2978 if ((strncmp(line, key, len) == 0) && (line[len] == '=')) {
2979 char *start = line + len + 1, *end = line + num - 1;
2980
2981 if (*end != '\n')
2982 end++;
2983
2984 value = strndup(start, end - start);
2985 break;
2986 }
2987 }
2988
2989 free(line);
2990 fclose(fp);
2991
2992 free(key);
2993
2994 return value;
2995 }
2996 #endif
2997
2998 /* Little white lie to avoid major rework of the existing code */
2999 #define DRM_BUS_VIRTIO 0x10
3000
3001 #ifdef __linux__
get_subsystem_type(const char * device_path)3002 static int get_subsystem_type(const char *device_path)
3003 {
3004 char path[PATH_MAX + 1] = "";
3005 char link[PATH_MAX + 1] = "";
3006 char *name;
3007 struct {
3008 const char *name;
3009 int bus_type;
3010 } bus_types[] = {
3011 { "/pci", DRM_BUS_PCI },
3012 { "/usb", DRM_BUS_USB },
3013 { "/platform", DRM_BUS_PLATFORM },
3014 { "/spi", DRM_BUS_PLATFORM },
3015 { "/host1x", DRM_BUS_HOST1X },
3016 { "/virtio", DRM_BUS_VIRTIO },
3017 };
3018
3019 strncpy(path, device_path, PATH_MAX);
3020 strncat(path, "/subsystem", PATH_MAX);
3021
3022 if (readlink(path, link, PATH_MAX) < 0)
3023 return -errno;
3024
3025 name = strrchr(link, '/');
3026 if (!name)
3027 return -EINVAL;
3028
3029 for (unsigned i = 0; i < ARRAY_SIZE(bus_types); i++) {
3030 if (strncmp(name, bus_types[i].name, strlen(bus_types[i].name)) == 0)
3031 return bus_types[i].bus_type;
3032 }
3033
3034 return -EINVAL;
3035 }
3036 #endif
3037
drmParseSubsystemType(int maj,int min)3038 static int drmParseSubsystemType(int maj, int min)
3039 {
3040 #ifdef __linux__
3041 char path[PATH_MAX + 1] = "";
3042 char real_path[PATH_MAX + 1] = "";
3043 int subsystem_type;
3044
3045 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3046 if (!realpath(path, real_path))
3047 return -errno;
3048 snprintf(path, sizeof(path), "%s", real_path);
3049
3050 subsystem_type = get_subsystem_type(path);
3051 if (subsystem_type == DRM_BUS_VIRTIO) {
3052 strncat(path, "/..", PATH_MAX);
3053 subsystem_type = get_subsystem_type(path);
3054 }
3055 return subsystem_type;
3056 #elif defined(__OpenBSD__) || defined(__DragonFly__)
3057 return DRM_BUS_PCI;
3058 #else
3059 #warning "Missing implementation of drmParseSubsystemType"
3060 return -EINVAL;
3061 #endif
3062 }
3063
3064 static void
get_pci_path(int maj,int min,char * pci_path)3065 get_pci_path(int maj, int min, char *pci_path)
3066 {
3067 char path[PATH_MAX + 1], *term;
3068
3069 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3070 if (!realpath(path, pci_path)) {
3071 strcpy(pci_path, path);
3072 return;
3073 }
3074
3075 term = strrchr(pci_path, '/');
3076 if (term && strncmp(term, "/virtio", 7) == 0)
3077 *term = 0;
3078 }
3079
drmParsePciBusInfo(int maj,int min,drmPciBusInfoPtr info)3080 static int drmParsePciBusInfo(int maj, int min, drmPciBusInfoPtr info)
3081 {
3082 #ifdef __linux__
3083 unsigned int domain, bus, dev, func;
3084 char pci_path[PATH_MAX + 1], *value;
3085 int num;
3086
3087 get_pci_path(maj, min, pci_path);
3088
3089 value = sysfs_uevent_get(pci_path, "PCI_SLOT_NAME");
3090 if (!value)
3091 return -ENOENT;
3092
3093 num = sscanf(value, "%04x:%02x:%02x.%1u", &domain, &bus, &dev, &func);
3094 free(value);
3095
3096 if (num != 4)
3097 return -EINVAL;
3098
3099 info->domain = domain;
3100 info->bus = bus;
3101 info->dev = dev;
3102 info->func = func;
3103
3104 return 0;
3105 #elif defined(__OpenBSD__) || defined(__DragonFly__)
3106 struct drm_pciinfo pinfo;
3107 int fd, type;
3108
3109 type = drmGetMinorType(min);
3110 if (type == -1)
3111 return -ENODEV;
3112
3113 fd = drmOpenMinor(min, 0, type);
3114 if (fd < 0)
3115 return -errno;
3116
3117 if (drmIoctl(fd, DRM_IOCTL_GET_PCIINFO, &pinfo)) {
3118 close(fd);
3119 return -errno;
3120 }
3121 close(fd);
3122
3123 info->domain = pinfo.domain;
3124 info->bus = pinfo.bus;
3125 info->dev = pinfo.dev;
3126 info->func = pinfo.func;
3127
3128 return 0;
3129 #else
3130 #warning "Missing implementation of drmParsePciBusInfo"
3131 return -EINVAL;
3132 #endif
3133 }
3134
drmDevicesEqual(drmDevicePtr a,drmDevicePtr b)3135 drm_public int drmDevicesEqual(drmDevicePtr a, drmDevicePtr b)
3136 {
3137 if (a == NULL || b == NULL)
3138 return 0;
3139
3140 if (a->bustype != b->bustype)
3141 return 0;
3142
3143 switch (a->bustype) {
3144 case DRM_BUS_PCI:
3145 return memcmp(a->businfo.pci, b->businfo.pci, sizeof(drmPciBusInfo)) == 0;
3146
3147 case DRM_BUS_USB:
3148 return memcmp(a->businfo.usb, b->businfo.usb, sizeof(drmUsbBusInfo)) == 0;
3149
3150 case DRM_BUS_PLATFORM:
3151 return memcmp(a->businfo.platform, b->businfo.platform, sizeof(drmPlatformBusInfo)) == 0;
3152
3153 case DRM_BUS_HOST1X:
3154 return memcmp(a->businfo.host1x, b->businfo.host1x, sizeof(drmHost1xBusInfo)) == 0;
3155
3156 default:
3157 break;
3158 }
3159
3160 return 0;
3161 }
3162
drmGetNodeType(const char * name)3163 static int drmGetNodeType(const char *name)
3164 {
3165 if (strncmp(name, DRM_PRIMARY_MINOR_NAME,
3166 sizeof(DRM_PRIMARY_MINOR_NAME) - 1) == 0)
3167 return DRM_NODE_PRIMARY;
3168
3169 if (strncmp(name, DRM_CONTROL_MINOR_NAME,
3170 sizeof(DRM_CONTROL_MINOR_NAME ) - 1) == 0)
3171 return DRM_NODE_CONTROL;
3172
3173 if (strncmp(name, DRM_RENDER_MINOR_NAME,
3174 sizeof(DRM_RENDER_MINOR_NAME) - 1) == 0)
3175 return DRM_NODE_RENDER;
3176
3177 return -EINVAL;
3178 }
3179
drmGetMaxNodeName(void)3180 static int drmGetMaxNodeName(void)
3181 {
3182 return sizeof(DRM_DIR_NAME) +
3183 MAX3(sizeof(DRM_PRIMARY_MINOR_NAME),
3184 sizeof(DRM_CONTROL_MINOR_NAME),
3185 sizeof(DRM_RENDER_MINOR_NAME)) +
3186 3 /* length of the node number */;
3187 }
3188
3189 #ifdef __linux__
parse_separate_sysfs_files(int maj,int min,drmPciDeviceInfoPtr device,bool ignore_revision)3190 static int parse_separate_sysfs_files(int maj, int min,
3191 drmPciDeviceInfoPtr device,
3192 bool ignore_revision)
3193 {
3194 static const char *attrs[] = {
3195 "revision", /* Older kernels are missing the file, so check for it first */
3196 "vendor",
3197 "device",
3198 "subsystem_vendor",
3199 "subsystem_device",
3200 };
3201 char path[PATH_MAX + 1], pci_path[PATH_MAX + 1];
3202 unsigned int data[ARRAY_SIZE(attrs)];
3203 FILE *fp;
3204 int ret;
3205
3206 get_pci_path(maj, min, pci_path);
3207
3208 for (unsigned i = ignore_revision ? 1 : 0; i < ARRAY_SIZE(attrs); i++) {
3209 snprintf(path, PATH_MAX, "%s/%s", pci_path, attrs[i]);
3210 fp = fopen(path, "r");
3211 if (!fp)
3212 return -errno;
3213
3214 ret = fscanf(fp, "%x", &data[i]);
3215 fclose(fp);
3216 if (ret != 1)
3217 return -errno;
3218
3219 }
3220
3221 device->revision_id = ignore_revision ? 0xff : data[0] & 0xff;
3222 device->vendor_id = data[1] & 0xffff;
3223 device->device_id = data[2] & 0xffff;
3224 device->subvendor_id = data[3] & 0xffff;
3225 device->subdevice_id = data[4] & 0xffff;
3226
3227 return 0;
3228 }
3229
parse_config_sysfs_file(int maj,int min,drmPciDeviceInfoPtr device)3230 static int parse_config_sysfs_file(int maj, int min,
3231 drmPciDeviceInfoPtr device)
3232 {
3233 char path[PATH_MAX + 1], pci_path[PATH_MAX + 1];
3234 unsigned char config[64];
3235 int fd, ret;
3236
3237 get_pci_path(maj, min, pci_path);
3238
3239 snprintf(path, PATH_MAX, "%s/config", pci_path);
3240 fd = open(path, O_RDONLY);
3241 if (fd < 0)
3242 return -errno;
3243
3244 ret = read(fd, config, sizeof(config));
3245 close(fd);
3246 if (ret < 0)
3247 return -errno;
3248
3249 device->vendor_id = config[0] | (config[1] << 8);
3250 device->device_id = config[2] | (config[3] << 8);
3251 device->revision_id = config[8];
3252 device->subvendor_id = config[44] | (config[45] << 8);
3253 device->subdevice_id = config[46] | (config[47] << 8);
3254
3255 return 0;
3256 }
3257 #endif
3258
drmParsePciDeviceInfo(int maj,int min,drmPciDeviceInfoPtr device,uint32_t flags)3259 static int drmParsePciDeviceInfo(int maj, int min,
3260 drmPciDeviceInfoPtr device,
3261 uint32_t flags)
3262 {
3263 #ifdef __linux__
3264 if (!(flags & DRM_DEVICE_GET_PCI_REVISION))
3265 return parse_separate_sysfs_files(maj, min, device, true);
3266
3267 if (parse_separate_sysfs_files(maj, min, device, false))
3268 return parse_config_sysfs_file(maj, min, device);
3269
3270 return 0;
3271 #elif defined(__OpenBSD__) || defined(__DragonFly__)
3272 struct drm_pciinfo pinfo;
3273 int fd, type;
3274
3275 type = drmGetMinorType(min);
3276 if (type == -1)
3277 return -ENODEV;
3278
3279 fd = drmOpenMinor(min, 0, type);
3280 if (fd < 0)
3281 return -errno;
3282
3283 if (drmIoctl(fd, DRM_IOCTL_GET_PCIINFO, &pinfo)) {
3284 close(fd);
3285 return -errno;
3286 }
3287 close(fd);
3288
3289 device->vendor_id = pinfo.vendor_id;
3290 device->device_id = pinfo.device_id;
3291 device->revision_id = pinfo.revision_id;
3292 device->subvendor_id = pinfo.subvendor_id;
3293 device->subdevice_id = pinfo.subdevice_id;
3294
3295 return 0;
3296 #else
3297 #warning "Missing implementation of drmParsePciDeviceInfo"
3298 return -EINVAL;
3299 #endif
3300 }
3301
drmFreePlatformDevice(drmDevicePtr device)3302 static void drmFreePlatformDevice(drmDevicePtr device)
3303 {
3304 if (device->deviceinfo.platform) {
3305 if (device->deviceinfo.platform->compatible) {
3306 char **compatible = device->deviceinfo.platform->compatible;
3307
3308 while (*compatible) {
3309 free(*compatible);
3310 compatible++;
3311 }
3312
3313 free(device->deviceinfo.platform->compatible);
3314 }
3315 }
3316 }
3317
drmFreeHost1xDevice(drmDevicePtr device)3318 static void drmFreeHost1xDevice(drmDevicePtr device)
3319 {
3320 if (device->deviceinfo.host1x) {
3321 if (device->deviceinfo.host1x->compatible) {
3322 char **compatible = device->deviceinfo.host1x->compatible;
3323
3324 while (*compatible) {
3325 free(*compatible);
3326 compatible++;
3327 }
3328
3329 free(device->deviceinfo.host1x->compatible);
3330 }
3331 }
3332 }
3333
drmFreeDevice(drmDevicePtr * device)3334 drm_public void drmFreeDevice(drmDevicePtr *device)
3335 {
3336 if (device == NULL)
3337 return;
3338
3339 if (*device) {
3340 switch ((*device)->bustype) {
3341 case DRM_BUS_PLATFORM:
3342 drmFreePlatformDevice(*device);
3343 break;
3344
3345 case DRM_BUS_HOST1X:
3346 drmFreeHost1xDevice(*device);
3347 break;
3348 }
3349 }
3350
3351 free(*device);
3352 *device = NULL;
3353 }
3354
drmFreeDevices(drmDevicePtr devices[],int count)3355 drm_public void drmFreeDevices(drmDevicePtr devices[], int count)
3356 {
3357 int i;
3358
3359 if (devices == NULL)
3360 return;
3361
3362 for (i = 0; i < count; i++)
3363 if (devices[i])
3364 drmFreeDevice(&devices[i]);
3365 }
3366
drmDeviceAlloc(unsigned int type,const char * node,size_t bus_size,size_t device_size,char ** ptrp)3367 static drmDevicePtr drmDeviceAlloc(unsigned int type, const char *node,
3368 size_t bus_size, size_t device_size,
3369 char **ptrp)
3370 {
3371 size_t max_node_length, extra, size;
3372 drmDevicePtr device;
3373 unsigned int i;
3374 char *ptr;
3375
3376 max_node_length = ALIGN(drmGetMaxNodeName(), sizeof(void *));
3377 extra = DRM_NODE_MAX * (sizeof(void *) + max_node_length);
3378
3379 size = sizeof(*device) + extra + bus_size + device_size;
3380
3381 device = calloc(1, size);
3382 if (!device)
3383 return NULL;
3384
3385 device->available_nodes = 1 << type;
3386
3387 ptr = (char *)device + sizeof(*device);
3388 device->nodes = (char **)ptr;
3389
3390 ptr += DRM_NODE_MAX * sizeof(void *);
3391
3392 for (i = 0; i < DRM_NODE_MAX; i++) {
3393 device->nodes[i] = ptr;
3394 ptr += max_node_length;
3395 }
3396
3397 memcpy(device->nodes[type], node, max_node_length);
3398
3399 *ptrp = ptr;
3400
3401 return device;
3402 }
3403
drmProcessPciDevice(drmDevicePtr * device,const char * node,int node_type,int maj,int min,bool fetch_deviceinfo,uint32_t flags)3404 static int drmProcessPciDevice(drmDevicePtr *device,
3405 const char *node, int node_type,
3406 int maj, int min, bool fetch_deviceinfo,
3407 uint32_t flags)
3408 {
3409 drmDevicePtr dev;
3410 char *addr;
3411 int ret;
3412
3413 dev = drmDeviceAlloc(node_type, node, sizeof(drmPciBusInfo),
3414 sizeof(drmPciDeviceInfo), &addr);
3415 if (!dev)
3416 return -ENOMEM;
3417
3418 dev->bustype = DRM_BUS_PCI;
3419
3420 dev->businfo.pci = (drmPciBusInfoPtr)addr;
3421
3422 ret = drmParsePciBusInfo(maj, min, dev->businfo.pci);
3423 if (ret)
3424 goto free_device;
3425
3426 // Fetch the device info if the user has requested it
3427 if (fetch_deviceinfo) {
3428 addr += sizeof(drmPciBusInfo);
3429 dev->deviceinfo.pci = (drmPciDeviceInfoPtr)addr;
3430
3431 ret = drmParsePciDeviceInfo(maj, min, dev->deviceinfo.pci, flags);
3432 if (ret)
3433 goto free_device;
3434 }
3435
3436 *device = dev;
3437
3438 return 0;
3439
3440 free_device:
3441 free(dev);
3442 return ret;
3443 }
3444
drmParseUsbBusInfo(int maj,int min,drmUsbBusInfoPtr info)3445 static int drmParseUsbBusInfo(int maj, int min, drmUsbBusInfoPtr info)
3446 {
3447 #ifdef __linux__
3448 char path[PATH_MAX + 1], *value;
3449 unsigned int bus, dev;
3450 int ret;
3451
3452 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3453
3454 value = sysfs_uevent_get(path, "BUSNUM");
3455 if (!value)
3456 return -ENOENT;
3457
3458 ret = sscanf(value, "%03u", &bus);
3459 free(value);
3460
3461 if (ret <= 0)
3462 return -errno;
3463
3464 value = sysfs_uevent_get(path, "DEVNUM");
3465 if (!value)
3466 return -ENOENT;
3467
3468 ret = sscanf(value, "%03u", &dev);
3469 free(value);
3470
3471 if (ret <= 0)
3472 return -errno;
3473
3474 info->bus = bus;
3475 info->dev = dev;
3476
3477 return 0;
3478 #else
3479 #warning "Missing implementation of drmParseUsbBusInfo"
3480 return -EINVAL;
3481 #endif
3482 }
3483
drmParseUsbDeviceInfo(int maj,int min,drmUsbDeviceInfoPtr info)3484 static int drmParseUsbDeviceInfo(int maj, int min, drmUsbDeviceInfoPtr info)
3485 {
3486 #ifdef __linux__
3487 char path[PATH_MAX + 1], *value;
3488 unsigned int vendor, product;
3489 int ret;
3490
3491 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3492
3493 value = sysfs_uevent_get(path, "PRODUCT");
3494 if (!value)
3495 return -ENOENT;
3496
3497 ret = sscanf(value, "%x/%x", &vendor, &product);
3498 free(value);
3499
3500 if (ret <= 0)
3501 return -errno;
3502
3503 info->vendor = vendor;
3504 info->product = product;
3505
3506 return 0;
3507 #else
3508 #warning "Missing implementation of drmParseUsbDeviceInfo"
3509 return -EINVAL;
3510 #endif
3511 }
3512
drmProcessUsbDevice(drmDevicePtr * device,const char * node,int node_type,int maj,int min,bool fetch_deviceinfo,uint32_t flags)3513 static int drmProcessUsbDevice(drmDevicePtr *device, const char *node,
3514 int node_type, int maj, int min,
3515 bool fetch_deviceinfo, uint32_t flags)
3516 {
3517 drmDevicePtr dev;
3518 char *ptr;
3519 int ret;
3520
3521 dev = drmDeviceAlloc(node_type, node, sizeof(drmUsbBusInfo),
3522 sizeof(drmUsbDeviceInfo), &ptr);
3523 if (!dev)
3524 return -ENOMEM;
3525
3526 dev->bustype = DRM_BUS_USB;
3527
3528 dev->businfo.usb = (drmUsbBusInfoPtr)ptr;
3529
3530 ret = drmParseUsbBusInfo(maj, min, dev->businfo.usb);
3531 if (ret < 0)
3532 goto free_device;
3533
3534 if (fetch_deviceinfo) {
3535 ptr += sizeof(drmUsbBusInfo);
3536 dev->deviceinfo.usb = (drmUsbDeviceInfoPtr)ptr;
3537
3538 ret = drmParseUsbDeviceInfo(maj, min, dev->deviceinfo.usb);
3539 if (ret < 0)
3540 goto free_device;
3541 }
3542
3543 *device = dev;
3544
3545 return 0;
3546
3547 free_device:
3548 free(dev);
3549 return ret;
3550 }
3551
drmParseOFBusInfo(int maj,int min,char * fullname)3552 static int drmParseOFBusInfo(int maj, int min, char *fullname)
3553 {
3554 #ifdef __linux__
3555 char path[PATH_MAX + 1], *name, *tmp_name;
3556
3557 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3558
3559 name = sysfs_uevent_get(path, "OF_FULLNAME");
3560 tmp_name = name;
3561 if (!name) {
3562 /* If the device lacks OF data, pick the MODALIAS info */
3563 name = sysfs_uevent_get(path, "MODALIAS");
3564 if (!name)
3565 return -ENOENT;
3566
3567 /* .. and strip the MODALIAS=[platform,usb...]: part. */
3568 tmp_name = strrchr(name, ':');
3569 if (!tmp_name) {
3570 free(name);
3571 return -ENOENT;
3572 }
3573 tmp_name++;
3574 }
3575
3576 strncpy(fullname, tmp_name, DRM_PLATFORM_DEVICE_NAME_LEN);
3577 fullname[DRM_PLATFORM_DEVICE_NAME_LEN - 1] = '\0';
3578 free(name);
3579
3580 return 0;
3581 #else
3582 #warning "Missing implementation of drmParseOFBusInfo"
3583 return -EINVAL;
3584 #endif
3585 }
3586
drmParseOFDeviceInfo(int maj,int min,char *** compatible)3587 static int drmParseOFDeviceInfo(int maj, int min, char ***compatible)
3588 {
3589 #ifdef __linux__
3590 char path[PATH_MAX + 1], *value, *tmp_name;
3591 unsigned int count, i;
3592 int err;
3593
3594 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3595
3596 value = sysfs_uevent_get(path, "OF_COMPATIBLE_N");
3597 if (value) {
3598 sscanf(value, "%u", &count);
3599 free(value);
3600 } else {
3601 /* Assume one entry if the device lack OF data */
3602 count = 1;
3603 }
3604
3605 *compatible = calloc(count + 1, sizeof(char *));
3606 if (!*compatible)
3607 return -ENOMEM;
3608
3609 for (i = 0; i < count; i++) {
3610 value = sysfs_uevent_get(path, "OF_COMPATIBLE_%u", i);
3611 tmp_name = value;
3612 if (!value) {
3613 /* If the device lacks OF data, pick the MODALIAS info */
3614 value = sysfs_uevent_get(path, "MODALIAS");
3615 if (!value) {
3616 err = -ENOENT;
3617 goto free;
3618 }
3619
3620 /* .. and strip the MODALIAS=[platform,usb...]: part. */
3621 tmp_name = strrchr(value, ':');
3622 if (!tmp_name) {
3623 free(value);
3624 return -ENOENT;
3625 }
3626 tmp_name = strdup(tmp_name + 1);
3627 free(value);
3628 }
3629
3630 (*compatible)[i] = tmp_name;
3631 }
3632
3633 return 0;
3634
3635 free:
3636 while (i--)
3637 free((*compatible)[i]);
3638
3639 free(*compatible);
3640 return err;
3641 #else
3642 #warning "Missing implementation of drmParseOFDeviceInfo"
3643 return -EINVAL;
3644 #endif
3645 }
3646
drmProcessPlatformDevice(drmDevicePtr * device,const char * node,int node_type,int maj,int min,bool fetch_deviceinfo,uint32_t flags)3647 static int drmProcessPlatformDevice(drmDevicePtr *device,
3648 const char *node, int node_type,
3649 int maj, int min, bool fetch_deviceinfo,
3650 uint32_t flags)
3651 {
3652 drmDevicePtr dev;
3653 char *ptr;
3654 int ret;
3655
3656 dev = drmDeviceAlloc(node_type, node, sizeof(drmPlatformBusInfo),
3657 sizeof(drmPlatformDeviceInfo), &ptr);
3658 if (!dev)
3659 return -ENOMEM;
3660
3661 dev->bustype = DRM_BUS_PLATFORM;
3662
3663 dev->businfo.platform = (drmPlatformBusInfoPtr)ptr;
3664
3665 ret = drmParseOFBusInfo(maj, min, dev->businfo.platform->fullname);
3666 if (ret < 0)
3667 goto free_device;
3668
3669 if (fetch_deviceinfo) {
3670 ptr += sizeof(drmPlatformBusInfo);
3671 dev->deviceinfo.platform = (drmPlatformDeviceInfoPtr)ptr;
3672
3673 ret = drmParseOFDeviceInfo(maj, min, &dev->deviceinfo.platform->compatible);
3674 if (ret < 0)
3675 goto free_device;
3676 }
3677
3678 *device = dev;
3679
3680 return 0;
3681
3682 free_device:
3683 free(dev);
3684 return ret;
3685 }
3686
drmProcessHost1xDevice(drmDevicePtr * device,const char * node,int node_type,int maj,int min,bool fetch_deviceinfo,uint32_t flags)3687 static int drmProcessHost1xDevice(drmDevicePtr *device,
3688 const char *node, int node_type,
3689 int maj, int min, bool fetch_deviceinfo,
3690 uint32_t flags)
3691 {
3692 drmDevicePtr dev;
3693 char *ptr;
3694 int ret;
3695
3696 dev = drmDeviceAlloc(node_type, node, sizeof(drmHost1xBusInfo),
3697 sizeof(drmHost1xDeviceInfo), &ptr);
3698 if (!dev)
3699 return -ENOMEM;
3700
3701 dev->bustype = DRM_BUS_HOST1X;
3702
3703 dev->businfo.host1x = (drmHost1xBusInfoPtr)ptr;
3704
3705 ret = drmParseOFBusInfo(maj, min, dev->businfo.host1x->fullname);
3706 if (ret < 0)
3707 goto free_device;
3708
3709 if (fetch_deviceinfo) {
3710 ptr += sizeof(drmHost1xBusInfo);
3711 dev->deviceinfo.host1x = (drmHost1xDeviceInfoPtr)ptr;
3712
3713 ret = drmParseOFDeviceInfo(maj, min, &dev->deviceinfo.host1x->compatible);
3714 if (ret < 0)
3715 goto free_device;
3716 }
3717
3718 *device = dev;
3719
3720 return 0;
3721
3722 free_device:
3723 free(dev);
3724 return ret;
3725 }
3726
3727 static int
process_device(drmDevicePtr * device,const char * d_name,int req_subsystem_type,bool fetch_deviceinfo,uint32_t flags)3728 process_device(drmDevicePtr *device, const char *d_name,
3729 int req_subsystem_type,
3730 bool fetch_deviceinfo, uint32_t flags)
3731 {
3732 struct stat sbuf;
3733 char node[PATH_MAX + 1];
3734 int node_type, subsystem_type;
3735 unsigned int maj, min;
3736
3737 node_type = drmGetNodeType(d_name);
3738 if (node_type < 0)
3739 return -1;
3740
3741 snprintf(node, PATH_MAX, "%s/%s", DRM_DIR_NAME, d_name);
3742 if (stat(node, &sbuf))
3743 return -1;
3744
3745 maj = major(sbuf.st_rdev);
3746 min = minor(sbuf.st_rdev);
3747
3748 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
3749 return -1;
3750
3751 subsystem_type = drmParseSubsystemType(maj, min);
3752 if (req_subsystem_type != -1 && req_subsystem_type != subsystem_type)
3753 return -1;
3754
3755 switch (subsystem_type) {
3756 case DRM_BUS_PCI:
3757 return drmProcessPciDevice(device, node, node_type, maj, min,
3758 fetch_deviceinfo, flags);
3759 case DRM_BUS_USB:
3760 return drmProcessUsbDevice(device, node, node_type, maj, min,
3761 fetch_deviceinfo, flags);
3762 case DRM_BUS_PLATFORM:
3763 return drmProcessPlatformDevice(device, node, node_type, maj, min,
3764 fetch_deviceinfo, flags);
3765 case DRM_BUS_HOST1X:
3766 return drmProcessHost1xDevice(device, node, node_type, maj, min,
3767 fetch_deviceinfo, flags);
3768 default:
3769 return -1;
3770 }
3771 }
3772
3773 /* Consider devices located on the same bus as duplicate and fold the respective
3774 * entries into a single one.
3775 *
3776 * Note: this leaves "gaps" in the array, while preserving the length.
3777 */
drmFoldDuplicatedDevices(drmDevicePtr local_devices[],int count)3778 static void drmFoldDuplicatedDevices(drmDevicePtr local_devices[], int count)
3779 {
3780 int node_type, i, j;
3781
3782 for (i = 0; i < count; i++) {
3783 for (j = i + 1; j < count; j++) {
3784 if (drmDevicesEqual(local_devices[i], local_devices[j])) {
3785 local_devices[i]->available_nodes |= local_devices[j]->available_nodes;
3786 node_type = log2(local_devices[j]->available_nodes);
3787 memcpy(local_devices[i]->nodes[node_type],
3788 local_devices[j]->nodes[node_type], drmGetMaxNodeName());
3789 drmFreeDevice(&local_devices[j]);
3790 }
3791 }
3792 }
3793 }
3794
3795 /* Check that the given flags are valid returning 0 on success */
3796 static int
drm_device_validate_flags(uint32_t flags)3797 drm_device_validate_flags(uint32_t flags)
3798 {
3799 return (flags & ~DRM_DEVICE_GET_PCI_REVISION);
3800 }
3801
3802 static bool
drm_device_has_rdev(drmDevicePtr device,dev_t find_rdev)3803 drm_device_has_rdev(drmDevicePtr device, dev_t find_rdev)
3804 {
3805 struct stat sbuf;
3806
3807 for (int i = 0; i < DRM_NODE_MAX; i++) {
3808 if (device->available_nodes & 1 << i) {
3809 if (stat(device->nodes[i], &sbuf) == 0 &&
3810 sbuf.st_rdev == find_rdev)
3811 return true;
3812 }
3813 }
3814 return false;
3815 }
3816
3817 /*
3818 * The kernel drm core has a number of places that assume maximum of
3819 * 3x64 devices nodes. That's 64 for each of primary, control and
3820 * render nodes. Rounded it up to 256 for simplicity.
3821 */
3822 #define MAX_DRM_NODES 256
3823
3824 /**
3825 * Get information about the opened drm device
3826 *
3827 * \param fd file descriptor of the drm device
3828 * \param flags feature/behaviour bitmask
3829 * \param device the address of a drmDevicePtr where the information
3830 * will be allocated in stored
3831 *
3832 * \return zero on success, negative error code otherwise.
3833 *
3834 * \note Unlike drmGetDevice it does not retrieve the pci device revision field
3835 * unless the DRM_DEVICE_GET_PCI_REVISION \p flag is set.
3836 */
drmGetDevice2(int fd,uint32_t flags,drmDevicePtr * device)3837 drm_public int drmGetDevice2(int fd, uint32_t flags, drmDevicePtr *device)
3838 {
3839 #ifdef __OpenBSD__
3840 /*
3841 * DRI device nodes on OpenBSD are not in their own directory, they reside
3842 * in /dev along with a large number of statically generated /dev nodes.
3843 * Avoid stat'ing all of /dev needlessly by implementing this custom path.
3844 */
3845 drmDevicePtr d;
3846 struct stat sbuf;
3847 char node[PATH_MAX + 1];
3848 const char *dev_name;
3849 int node_type, subsystem_type;
3850 int maj, min, n, ret, base;
3851
3852 if (fd == -1 || device == NULL)
3853 return -EINVAL;
3854
3855 if (fstat(fd, &sbuf))
3856 return -errno;
3857
3858 maj = major(sbuf.st_rdev);
3859 min = minor(sbuf.st_rdev);
3860
3861 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
3862 return -EINVAL;
3863
3864 node_type = drmGetMinorType(min);
3865 if (node_type == -1)
3866 return -ENODEV;
3867
3868 switch (node_type) {
3869 case DRM_NODE_PRIMARY:
3870 dev_name = DRM_DEV_NAME;
3871 break;
3872 case DRM_NODE_CONTROL:
3873 dev_name = DRM_CONTROL_DEV_NAME;
3874 break;
3875 case DRM_NODE_RENDER:
3876 dev_name = DRM_RENDER_DEV_NAME;
3877 break;
3878 default:
3879 return -EINVAL;
3880 };
3881
3882 base = drmGetMinorBase(node_type);
3883 if (base < 0)
3884 return -EINVAL;
3885
3886 n = snprintf(node, PATH_MAX, dev_name, DRM_DIR_NAME, min - base);
3887 if (n == -1 || n >= PATH_MAX)
3888 return -errno;
3889 if (stat(node, &sbuf))
3890 return -EINVAL;
3891
3892 subsystem_type = drmParseSubsystemType(maj, min);
3893 if (subsystem_type != DRM_BUS_PCI)
3894 return -ENODEV;
3895
3896 ret = drmProcessPciDevice(&d, node, node_type, maj, min, true, flags);
3897 if (ret)
3898 return ret;
3899
3900 *device = d;
3901
3902 return 0;
3903 #else
3904 drmDevicePtr local_devices[MAX_DRM_NODES];
3905 drmDevicePtr d;
3906 DIR *sysdir;
3907 struct dirent *dent;
3908 struct stat sbuf;
3909 int subsystem_type;
3910 int maj, min;
3911 int ret, i, node_count;
3912 dev_t find_rdev;
3913
3914 if (drm_device_validate_flags(flags))
3915 return -EINVAL;
3916
3917 if (fd == -1 || device == NULL)
3918 return -EINVAL;
3919
3920 if (fstat(fd, &sbuf))
3921 return -errno;
3922
3923 find_rdev = sbuf.st_rdev;
3924 maj = major(sbuf.st_rdev);
3925 min = minor(sbuf.st_rdev);
3926
3927 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
3928 return -EINVAL;
3929
3930 subsystem_type = drmParseSubsystemType(maj, min);
3931 if (subsystem_type < 0)
3932 return subsystem_type;
3933
3934 sysdir = opendir(DRM_DIR_NAME);
3935 if (!sysdir)
3936 return -errno;
3937
3938 i = 0;
3939 while ((dent = readdir(sysdir))) {
3940 ret = process_device(&d, dent->d_name, subsystem_type, true, flags);
3941 if (ret)
3942 continue;
3943
3944 if (i >= MAX_DRM_NODES) {
3945 fprintf(stderr, "More than %d drm nodes detected. "
3946 "Please report a bug - that should not happen.\n"
3947 "Skipping extra nodes\n", MAX_DRM_NODES);
3948 break;
3949 }
3950 local_devices[i] = d;
3951 i++;
3952 }
3953 node_count = i;
3954
3955 drmFoldDuplicatedDevices(local_devices, node_count);
3956
3957 *device = NULL;
3958
3959 for (i = 0; i < node_count; i++) {
3960 if (!local_devices[i])
3961 continue;
3962
3963 if (drm_device_has_rdev(local_devices[i], find_rdev))
3964 *device = local_devices[i];
3965 else
3966 drmFreeDevice(&local_devices[i]);
3967 }
3968
3969 closedir(sysdir);
3970 if (*device == NULL)
3971 return -ENODEV;
3972 return 0;
3973 #endif
3974 }
3975
3976 /**
3977 * Get information about the opened drm device
3978 *
3979 * \param fd file descriptor of the drm device
3980 * \param device the address of a drmDevicePtr where the information
3981 * will be allocated in stored
3982 *
3983 * \return zero on success, negative error code otherwise.
3984 */
drmGetDevice(int fd,drmDevicePtr * device)3985 drm_public int drmGetDevice(int fd, drmDevicePtr *device)
3986 {
3987 return drmGetDevice2(fd, DRM_DEVICE_GET_PCI_REVISION, device);
3988 }
3989
3990 /**
3991 * Get drm devices on the system
3992 *
3993 * \param flags feature/behaviour bitmask
3994 * \param devices the array of devices with drmDevicePtr elements
3995 * can be NULL to get the device number first
3996 * \param max_devices the maximum number of devices for the array
3997 *
3998 * \return on error - negative error code,
3999 * if devices is NULL - total number of devices available on the system,
4000 * alternatively the number of devices stored in devices[], which is
4001 * capped by the max_devices.
4002 *
4003 * \note Unlike drmGetDevices it does not retrieve the pci device revision field
4004 * unless the DRM_DEVICE_GET_PCI_REVISION \p flag is set.
4005 */
drmGetDevices2(uint32_t flags,drmDevicePtr devices[],int max_devices)4006 drm_public int drmGetDevices2(uint32_t flags, drmDevicePtr devices[],
4007 int max_devices)
4008 {
4009 drmDevicePtr local_devices[MAX_DRM_NODES];
4010 drmDevicePtr device;
4011 DIR *sysdir;
4012 struct dirent *dent;
4013 int ret, i, node_count, device_count;
4014
4015 if (drm_device_validate_flags(flags))
4016 return -EINVAL;
4017
4018 sysdir = opendir(DRM_DIR_NAME);
4019 if (!sysdir)
4020 return -errno;
4021
4022 i = 0;
4023 while ((dent = readdir(sysdir))) {
4024 ret = process_device(&device, dent->d_name, -1, devices != NULL, flags);
4025 if (ret)
4026 continue;
4027
4028 if (i >= MAX_DRM_NODES) {
4029 fprintf(stderr, "More than %d drm nodes detected. "
4030 "Please report a bug - that should not happen.\n"
4031 "Skipping extra nodes\n", MAX_DRM_NODES);
4032 break;
4033 }
4034 local_devices[i] = device;
4035 i++;
4036 }
4037 node_count = i;
4038
4039 drmFoldDuplicatedDevices(local_devices, node_count);
4040
4041 device_count = 0;
4042 for (i = 0; i < node_count; i++) {
4043 if (!local_devices[i])
4044 continue;
4045
4046 if ((devices != NULL) && (device_count < max_devices))
4047 devices[device_count] = local_devices[i];
4048 else
4049 drmFreeDevice(&local_devices[i]);
4050
4051 device_count++;
4052 }
4053
4054 closedir(sysdir);
4055 return device_count;
4056 }
4057
4058 /**
4059 * Get drm devices on the system
4060 *
4061 * \param devices the array of devices with drmDevicePtr elements
4062 * can be NULL to get the device number first
4063 * \param max_devices the maximum number of devices for the array
4064 *
4065 * \return on error - negative error code,
4066 * if devices is NULL - total number of devices available on the system,
4067 * alternatively the number of devices stored in devices[], which is
4068 * capped by the max_devices.
4069 */
drmGetDevices(drmDevicePtr devices[],int max_devices)4070 drm_public int drmGetDevices(drmDevicePtr devices[], int max_devices)
4071 {
4072 return drmGetDevices2(DRM_DEVICE_GET_PCI_REVISION, devices, max_devices);
4073 }
4074
drmGetDeviceNameFromFd2(int fd)4075 drm_public char *drmGetDeviceNameFromFd2(int fd)
4076 {
4077 #ifdef __linux__
4078 struct stat sbuf;
4079 char path[PATH_MAX + 1], *value;
4080 unsigned int maj, min;
4081
4082 if (fstat(fd, &sbuf))
4083 return NULL;
4084
4085 maj = major(sbuf.st_rdev);
4086 min = minor(sbuf.st_rdev);
4087
4088 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
4089 return NULL;
4090
4091 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d", maj, min);
4092
4093 value = sysfs_uevent_get(path, "DEVNAME");
4094 if (!value)
4095 return NULL;
4096
4097 snprintf(path, sizeof(path), "/dev/%s", value);
4098 free(value);
4099
4100 return strdup(path);
4101 #else
4102 struct stat sbuf;
4103 char node[PATH_MAX + 1];
4104 const char *dev_name;
4105 int node_type;
4106 int maj, min, n, base;
4107
4108 if (fstat(fd, &sbuf))
4109 return NULL;
4110
4111 maj = major(sbuf.st_rdev);
4112 min = minor(sbuf.st_rdev);
4113
4114 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
4115 return NULL;
4116
4117 node_type = drmGetMinorType(min);
4118 if (node_type == -1)
4119 return NULL;
4120
4121 switch (node_type) {
4122 case DRM_NODE_PRIMARY:
4123 dev_name = DRM_DEV_NAME;
4124 break;
4125 case DRM_NODE_CONTROL:
4126 dev_name = DRM_CONTROL_DEV_NAME;
4127 break;
4128 case DRM_NODE_RENDER:
4129 dev_name = DRM_RENDER_DEV_NAME;
4130 break;
4131 default:
4132 return NULL;
4133 };
4134
4135 base = drmGetMinorBase(node_type);
4136 if (base < 0)
4137 return NULL;
4138
4139 n = snprintf(node, PATH_MAX, dev_name, DRM_DIR_NAME, min - base);
4140 if (n == -1 || n >= PATH_MAX)
4141 return NULL;
4142
4143 return strdup(node);
4144 #endif
4145 }
4146
drmSyncobjCreate(int fd,uint32_t flags,uint32_t * handle)4147 drm_public int drmSyncobjCreate(int fd, uint32_t flags, uint32_t *handle)
4148 {
4149 struct drm_syncobj_create args;
4150 int ret;
4151
4152 memclear(args);
4153 args.flags = flags;
4154 args.handle = 0;
4155 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_CREATE, &args);
4156 if (ret)
4157 return ret;
4158 *handle = args.handle;
4159 return 0;
4160 }
4161
drmSyncobjDestroy(int fd,uint32_t handle)4162 drm_public int drmSyncobjDestroy(int fd, uint32_t handle)
4163 {
4164 struct drm_syncobj_destroy args;
4165
4166 memclear(args);
4167 args.handle = handle;
4168 return drmIoctl(fd, DRM_IOCTL_SYNCOBJ_DESTROY, &args);
4169 }
4170
drmSyncobjHandleToFD(int fd,uint32_t handle,int * obj_fd)4171 drm_public int drmSyncobjHandleToFD(int fd, uint32_t handle, int *obj_fd)
4172 {
4173 struct drm_syncobj_handle args;
4174 int ret;
4175
4176 memclear(args);
4177 args.fd = -1;
4178 args.handle = handle;
4179 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, &args);
4180 if (ret)
4181 return ret;
4182 *obj_fd = args.fd;
4183 return 0;
4184 }
4185
drmSyncobjFDToHandle(int fd,int obj_fd,uint32_t * handle)4186 drm_public int drmSyncobjFDToHandle(int fd, int obj_fd, uint32_t *handle)
4187 {
4188 struct drm_syncobj_handle args;
4189 int ret;
4190
4191 memclear(args);
4192 args.fd = obj_fd;
4193 args.handle = 0;
4194 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, &args);
4195 if (ret)
4196 return ret;
4197 *handle = args.handle;
4198 return 0;
4199 }
4200
drmSyncobjImportSyncFile(int fd,uint32_t handle,int sync_file_fd)4201 drm_public int drmSyncobjImportSyncFile(int fd, uint32_t handle,
4202 int sync_file_fd)
4203 {
4204 struct drm_syncobj_handle args;
4205
4206 memclear(args);
4207 args.fd = sync_file_fd;
4208 args.handle = handle;
4209 args.flags = DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE;
4210 return drmIoctl(fd, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, &args);
4211 }
4212
drmSyncobjExportSyncFile(int fd,uint32_t handle,int * sync_file_fd)4213 drm_public int drmSyncobjExportSyncFile(int fd, uint32_t handle,
4214 int *sync_file_fd)
4215 {
4216 struct drm_syncobj_handle args;
4217 int ret;
4218
4219 memclear(args);
4220 args.fd = -1;
4221 args.handle = handle;
4222 args.flags = DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE;
4223 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, &args);
4224 if (ret)
4225 return ret;
4226 *sync_file_fd = args.fd;
4227 return 0;
4228 }
4229
drmSyncobjWait(int fd,uint32_t * handles,unsigned num_handles,int64_t timeout_nsec,unsigned flags,uint32_t * first_signaled)4230 drm_public int drmSyncobjWait(int fd, uint32_t *handles, unsigned num_handles,
4231 int64_t timeout_nsec, unsigned flags,
4232 uint32_t *first_signaled)
4233 {
4234 struct drm_syncobj_wait args;
4235 int ret;
4236
4237 memclear(args);
4238 args.handles = (uintptr_t)handles;
4239 args.timeout_nsec = timeout_nsec;
4240 args.count_handles = num_handles;
4241 args.flags = flags;
4242
4243 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_WAIT, &args);
4244 if (ret < 0)
4245 return -errno;
4246
4247 if (first_signaled)
4248 *first_signaled = args.first_signaled;
4249 return ret;
4250 }
4251
drmSyncobjReset(int fd,const uint32_t * handles,uint32_t handle_count)4252 drm_public int drmSyncobjReset(int fd, const uint32_t *handles,
4253 uint32_t handle_count)
4254 {
4255 struct drm_syncobj_array args;
4256 int ret;
4257
4258 memclear(args);
4259 args.handles = (uintptr_t)handles;
4260 args.count_handles = handle_count;
4261
4262 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_RESET, &args);
4263 return ret;
4264 }
4265
drmSyncobjSignal(int fd,const uint32_t * handles,uint32_t handle_count)4266 drm_public int drmSyncobjSignal(int fd, const uint32_t *handles,
4267 uint32_t handle_count)
4268 {
4269 struct drm_syncobj_array args;
4270 int ret;
4271
4272 memclear(args);
4273 args.handles = (uintptr_t)handles;
4274 args.count_handles = handle_count;
4275
4276 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_SIGNAL, &args);
4277 return ret;
4278 }
4279
drmSyncobjTimelineSignal(int fd,const uint32_t * handles,uint64_t * points,uint32_t handle_count)4280 drm_public int drmSyncobjTimelineSignal(int fd, const uint32_t *handles,
4281 uint64_t *points, uint32_t handle_count)
4282 {
4283 struct drm_syncobj_timeline_array args;
4284 int ret;
4285
4286 memclear(args);
4287 args.handles = (uintptr_t)handles;
4288 args.points = (uintptr_t)points;
4289 args.count_handles = handle_count;
4290
4291 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_TIMELINE_SIGNAL, &args);
4292 return ret;
4293 }
4294
drmSyncobjTimelineWait(int fd,uint32_t * handles,uint64_t * points,unsigned num_handles,int64_t timeout_nsec,unsigned flags,uint32_t * first_signaled)4295 drm_public int drmSyncobjTimelineWait(int fd, uint32_t *handles, uint64_t *points,
4296 unsigned num_handles,
4297 int64_t timeout_nsec, unsigned flags,
4298 uint32_t *first_signaled)
4299 {
4300 struct drm_syncobj_timeline_wait args;
4301 int ret;
4302
4303 memclear(args);
4304 args.handles = (uintptr_t)handles;
4305 args.points = (uintptr_t)points;
4306 args.timeout_nsec = timeout_nsec;
4307 args.count_handles = num_handles;
4308 args.flags = flags;
4309
4310 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_TIMELINE_WAIT, &args);
4311 if (ret < 0)
4312 return -errno;
4313
4314 if (first_signaled)
4315 *first_signaled = args.first_signaled;
4316 return ret;
4317 }
4318
4319
drmSyncobjQuery(int fd,uint32_t * handles,uint64_t * points,uint32_t handle_count)4320 drm_public int drmSyncobjQuery(int fd, uint32_t *handles, uint64_t *points,
4321 uint32_t handle_count)
4322 {
4323 struct drm_syncobj_timeline_array args;
4324 int ret;
4325
4326 memclear(args);
4327 args.handles = (uintptr_t)handles;
4328 args.points = (uintptr_t)points;
4329 args.count_handles = handle_count;
4330
4331 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_QUERY, &args);
4332 if (ret)
4333 return ret;
4334 return 0;
4335 }
4336
drmSyncobjTransfer(int fd,uint32_t dst_handle,uint64_t dst_point,uint32_t src_handle,uint64_t src_point,uint32_t flags)4337 drm_public int drmSyncobjTransfer(int fd,
4338 uint32_t dst_handle, uint64_t dst_point,
4339 uint32_t src_handle, uint64_t src_point,
4340 uint32_t flags)
4341 {
4342 struct drm_syncobj_transfer args;
4343 int ret;
4344
4345 memclear(args);
4346 args.src_handle = src_handle;
4347 args.dst_handle = dst_handle;
4348 args.src_point = src_point;
4349 args.dst_point = dst_point;
4350 args.flags = flags;
4351
4352 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_TRANSFER, &args);
4353
4354 return ret;
4355 }
4356