3 * User-level interface to DRM device
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Kevin E. Martin <martin@valinux.com>
10 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
11 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
12 * All Rights Reserved.
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:
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
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.
46 #include <sys/types.h>
48 #define stat_t struct stat
49 #include <sys/ioctl.h>
54 /* Not all systems have MAP_FAILED defined */
56 #define MAP_FAILED ((void *)-1)
61 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
75 #define DRM_MAJOR 226 /* Linux */
79 #define DRM_MAX_MINOR 16
83 * This definition needs to be changed on some systems if dev_t is a structure.
84 * If there is a header file we can get it from, there would be best.
87 #define makedev(x,y) ((dev_t)(((x) << 8) | (y)))
90 #define DRM_MSG_VERBOSITY 3
92 #define DRM_NODE_CONTROL 0
93 #define DRM_NODE_RENDER 1
95 static drmServerInfoPtr drm_server_info
;
97 void drmSetServerInfo(drmServerInfoPtr info
)
99 drm_server_info
= info
;
103 * Output a message to stderr.
105 * \param format printf() like format string.
108 * This function is a wrapper around vfprintf().
111 static int drmDebugPrint(const char *format
, va_list ap
)
113 return vfprintf(stderr
, format
, ap
);
116 static int (*drm_debug_print
)(const char *format
, va_list ap
) = drmDebugPrint
;
119 drmMsg(const char *format
, ...)
123 if (((env
= getenv("LIBGL_DEBUG")) && strstr(env
, "verbose")) || drm_server_info
)
125 va_start(ap
, format
);
126 if (drm_server_info
) {
127 drm_server_info
->debug_print(format
,ap
);
129 drm_debug_print(format
, ap
);
136 drmSetDebugMsgFunction(int (*debug_msg_ptr
)(const char *format
, va_list ap
))
138 drm_debug_print
= debug_msg_ptr
;
141 static void *drmHashTable
= NULL
; /* Context switch callbacks */
143 void *drmGetHashTable(void)
148 void *drmMalloc(int size
)
151 if ((pt
= malloc(size
)))
156 void drmFree(void *pt
)
162 /* drmStrdup can't use strdup(3), since it doesn't call _DRM_MALLOC... */
163 static char *drmStrdup(const char *s
)
170 retval
= malloc(strlen(s
)+1);
180 * Call ioctl, restarting if it is interupted
183 drmIoctl(int fd
, unsigned long request
, void *arg
)
188 ret
= ioctl(fd
, request
, arg
);
189 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
193 static unsigned long drmGetKeyFromFd(int fd
)
202 drmHashEntry
*drmGetEntry(int fd
)
204 unsigned long key
= drmGetKeyFromFd(fd
);
209 drmHashTable
= drmHashCreate();
211 if (drmHashLookup(drmHashTable
, key
, &value
)) {
212 entry
= drmMalloc(sizeof(*entry
));
215 entry
->tagTable
= drmHashCreate();
216 drmHashInsert(drmHashTable
, key
, entry
);
224 * Compare two busid strings
229 * \return 1 if matched.
232 * This function compares two bus ID strings. It understands the older
233 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
234 * domain, b is bus, d is device, f is function.
236 static int drmMatchBusID(const char *id1
, const char *id2
)
238 /* First, check if the IDs are exactly the same */
239 if (strcasecmp(id1
, id2
) == 0)
242 /* Try to match old/new-style PCI bus IDs. */
243 if (strncasecmp(id1
, "pci", 3) == 0) {
248 ret
= sscanf(id1
, "pci:%04x:%02x:%02x.%d", &o1
, &b1
, &d1
, &f1
);
251 ret
= sscanf(id1
, "PCI:%d:%d:%d", &b1
, &d1
, &f1
);
256 ret
= sscanf(id2
, "pci:%04x:%02x:%02x.%d", &o2
, &b2
, &d2
, &f2
);
259 ret
= sscanf(id2
, "PCI:%d:%d:%d", &b2
, &d2
, &f2
);
264 if ((o1
!= o2
) || (b1
!= b2
) || (d1
!= d2
) || (f1
!= f2
))
273 * Open the DRM device, creating it if necessary.
275 * \param dev major and minor numbers of the device.
276 * \param minor minor number of the device.
278 * \return a file descriptor on success, or a negative value on error.
281 * Assembles the device name from \p minor and opens it, creating the device
282 * special file node with the major and minor numbers specified by \p dev and
283 * parent directory if necessary and was called by root.
285 static int drmOpenDevice(long dev
, int minor
, int type
)
290 mode_t devmode
= DRM_DEV_MODE
, serv_mode
;
291 int isroot
= !geteuid();
292 uid_t user
= DRM_DEV_UID
;
293 gid_t group
= DRM_DEV_GID
, serv_group
;
295 sprintf(buf
, type
? DRM_DEV_NAME
: DRM_CONTROL_DEV_NAME
, DRM_DIR_NAME
, minor
);
296 drmMsg("drmOpenDevice: node name is %s\n", buf
);
298 if (drm_server_info
) {
299 drm_server_info
->get_perms(&serv_group
, &serv_mode
);
300 devmode
= serv_mode
? serv_mode
: DRM_DEV_MODE
;
301 devmode
&= ~(S_IXUSR
|S_IXGRP
|S_IXOTH
);
302 group
= (serv_group
>= 0) ? serv_group
: DRM_DEV_GID
;
306 if (stat(DRM_DIR_NAME
, &st
)) {
308 return DRM_ERR_NOT_ROOT
;
309 mkdir(DRM_DIR_NAME
, DRM_DEV_DIRMODE
);
310 chown(DRM_DIR_NAME
, 0, 0); /* root:root */
311 chmod(DRM_DIR_NAME
, DRM_DEV_DIRMODE
);
314 /* Check if the device node exists and create it if necessary. */
315 if (stat(buf
, &st
)) {
317 return DRM_ERR_NOT_ROOT
;
319 mknod(buf
, S_IFCHR
| devmode
, dev
);
322 if (drm_server_info
) {
323 chown(buf
, user
, group
);
327 /* if we modprobed then wait for udev */
331 if (stat(DRM_DIR_NAME
, &st
)) {
335 if (udev_count
== 50)
340 if (stat(buf
, &st
)) {
344 if (udev_count
== 50)
351 fd
= open(buf
, O_RDWR
, 0);
352 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
353 fd
, fd
< 0 ? strerror(errno
) : "OK");
357 /* Check if the device node is not what we expect it to be, and recreate it
358 * and try again if so.
360 if (st
.st_rdev
!= dev
) {
362 return DRM_ERR_NOT_ROOT
;
364 mknod(buf
, S_IFCHR
| devmode
, dev
);
365 if (drm_server_info
) {
366 chown(buf
, user
, group
);
370 fd
= open(buf
, O_RDWR
, 0);
371 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
372 fd
, fd
< 0 ? strerror(errno
) : "OK");
376 drmMsg("drmOpenDevice: Open failed\n");
383 * Open the DRM device
385 * \param minor device minor number.
386 * \param create allow to create the device if set.
388 * \return a file descriptor on success, or a negative value on error.
391 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
392 * name from \p minor and opens it.
394 static int drmOpenMinor(int minor
, int create
, int type
)
400 return drmOpenDevice(makedev(DRM_MAJOR
, minor
), minor
, type
);
402 sprintf(buf
, type
? DRM_DEV_NAME
: DRM_CONTROL_DEV_NAME
, DRM_DIR_NAME
, minor
);
403 if ((fd
= open(buf
, O_RDWR
, 0)) >= 0)
410 * Determine whether the DRM kernel driver has been loaded.
412 * \return 1 if the DRM driver is loaded, 0 otherwise.
415 * Determine the presence of the kernel driver by attempting to open the 0
416 * minor and get version information. For backward compatibility with older
417 * Linux implementations, /proc/dri is also checked.
419 int drmAvailable(void)
421 drmVersionPtr version
;
425 if ((fd
= drmOpenMinor(0, 1, DRM_NODE_RENDER
)) < 0) {
427 /* Try proc for backward Linux compatibility */
428 if (!access("/proc/dri/0", R_OK
))
434 if ((version
= drmGetVersion(fd
))) {
436 drmFreeVersion(version
);
445 * Open the device by bus ID.
447 * \param busid bus ID.
449 * \return a file descriptor on success, or a negative value on error.
452 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
453 * comparing the device bus ID with the one supplied.
455 * \sa drmOpenMinor() and drmGetBusid().
457 static int drmOpenByBusid(const char *busid
)
464 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid
);
465 for (i
= 0; i
< DRM_MAX_MINOR
; i
++) {
466 fd
= drmOpenMinor(i
, 1, DRM_NODE_RENDER
);
467 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd
);
471 sv
.drm_dd_major
= -1; /* Don't care */
472 sv
.drm_dd_minor
= -1; /* Don't care */
473 drmSetInterfaceVersion(fd
, &sv
);
474 buf
= drmGetBusid(fd
);
475 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf
);
476 if (buf
&& drmMatchBusID(buf
, busid
)) {
490 * Open the device by name.
492 * \param name driver name.
494 * \return a file descriptor on success, or a negative value on error.
497 * This function opens the first minor number that matches the driver name and
498 * isn't already in use. If it's in use it then it will already have a bus ID
501 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
503 static int drmOpenByName(const char *name
)
507 drmVersionPtr version
;
510 if (!drmAvailable()) {
511 if (!drm_server_info
) {
515 /* try to load the kernel module now */
516 if (!drm_server_info
->load_module(name
)) {
517 drmMsg("[drm] failed to load kernel module \"%s\"\n", name
);
524 * Open the first minor number that matches the driver name and isn't
525 * already in use. If it's in use it will have a busid assigned already.
527 for (i
= 0; i
< DRM_MAX_MINOR
; i
++) {
528 if ((fd
= drmOpenMinor(i
, 1, DRM_NODE_RENDER
)) >= 0) {
529 if ((version
= drmGetVersion(fd
))) {
530 if (!strcmp(version
->name
, name
)) {
531 drmFreeVersion(version
);
532 id
= drmGetBusid(fd
);
533 drmMsg("drmGetBusid returned '%s'\n", id
? id
: "NULL");
542 drmFreeVersion(version
);
550 /* Backward-compatibility /proc support */
551 for (i
= 0; i
< 8; i
++) {
552 char proc_name
[64], buf
[512];
553 char *driver
, *pt
, *devstring
;
556 sprintf(proc_name
, "/proc/dri/%d/name", i
);
557 if ((fd
= open(proc_name
, 0, 0)) >= 0) {
558 retcode
= read(fd
, buf
, sizeof(buf
)-1);
561 buf
[retcode
-1] = '\0';
562 for (driver
= pt
= buf
; *pt
&& *pt
!= ' '; ++pt
)
564 if (*pt
) { /* Device is next */
566 if (!strcmp(driver
, name
)) { /* Match */
567 for (devstring
= ++pt
; *pt
&& *pt
!= ' '; ++pt
)
569 if (*pt
) { /* Found busid */
570 return drmOpenByBusid(++pt
);
571 } else { /* No busid */
572 return drmOpenDevice(strtol(devstring
, NULL
, 0),i
, DRM_NODE_RENDER
);
586 * Open the DRM device.
588 * Looks up the specified name and bus ID, and opens the device found. The
589 * entry in /dev/dri is created if necessary and if called by root.
591 * \param name driver name. Not referenced if bus ID is supplied.
592 * \param busid bus ID. Zero if not known.
594 * \return a file descriptor on success, or a negative value on error.
597 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
600 int drmOpen(const char *name
, const char *busid
)
602 if (!drmAvailable() && name
!= NULL
&& drm_server_info
) {
603 /* try to load the kernel */
604 if (!drm_server_info
->load_module(name
)) {
605 drmMsg("[drm] failed to load kernel module \"%s\"\n", name
);
611 int fd
= drmOpenByBusid(busid
);
617 return drmOpenByName(name
);
622 int drmOpenControl(int minor
)
624 return drmOpenMinor(minor
, 0, DRM_NODE_CONTROL
);
628 * Free the version information returned by drmGetVersion().
630 * \param v pointer to the version information.
633 * It frees the memory pointed by \p %v as well as all the non-null strings
636 void drmFreeVersion(drmVersionPtr v
)
648 * Free the non-public version information returned by the kernel.
650 * \param v pointer to the version information.
653 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
654 * the non-null strings pointers in it.
656 static void drmFreeKernelVersion(drm_version_t
*v
)
668 * Copy version information.
670 * \param d destination pointer.
671 * \param s source pointer.
674 * Used by drmGetVersion() to translate the information returned by the ioctl
675 * interface in a private structure into the public structure counterpart.
677 static void drmCopyVersion(drmVersionPtr d
, const drm_version_t
*s
)
679 d
->version_major
= s
->version_major
;
680 d
->version_minor
= s
->version_minor
;
681 d
->version_patchlevel
= s
->version_patchlevel
;
682 d
->name_len
= s
->name_len
;
683 d
->name
= drmStrdup(s
->name
);
684 d
->date_len
= s
->date_len
;
685 d
->date
= drmStrdup(s
->date
);
686 d
->desc_len
= s
->desc_len
;
687 d
->desc
= drmStrdup(s
->desc
);
692 * Query the driver version information.
694 * \param fd file descriptor.
696 * \return pointer to a drmVersion structure which should be freed with
699 * \note Similar information is available via /proc/dri.
702 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
703 * first with zeros to get the string lengths, and then the actually strings.
704 * It also null-terminates them since they might not be already.
706 drmVersionPtr
drmGetVersion(int fd
)
708 drmVersionPtr retval
;
709 drm_version_t
*version
= drmMalloc(sizeof(*version
));
711 version
->name_len
= 0;
712 version
->name
= NULL
;
713 version
->date_len
= 0;
714 version
->date
= NULL
;
715 version
->desc_len
= 0;
716 version
->desc
= NULL
;
718 if (drmIoctl(fd
, DRM_IOCTL_VERSION
, version
)) {
719 drmFreeKernelVersion(version
);
723 if (version
->name_len
)
724 version
->name
= drmMalloc(version
->name_len
+ 1);
725 if (version
->date_len
)
726 version
->date
= drmMalloc(version
->date_len
+ 1);
727 if (version
->desc_len
)
728 version
->desc
= drmMalloc(version
->desc_len
+ 1);
730 if (drmIoctl(fd
, DRM_IOCTL_VERSION
, version
)) {
731 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno
));
732 drmFreeKernelVersion(version
);
736 /* The results might not be null-terminated strings, so terminate them. */
737 if (version
->name_len
) version
->name
[version
->name_len
] = '\0';
738 if (version
->date_len
) version
->date
[version
->date_len
] = '\0';
739 if (version
->desc_len
) version
->desc
[version
->desc_len
] = '\0';
741 retval
= drmMalloc(sizeof(*retval
));
742 drmCopyVersion(retval
, version
);
743 drmFreeKernelVersion(version
);
749 * Get version information for the DRM user space library.
751 * This version number is driver independent.
753 * \param fd file descriptor.
755 * \return version information.
758 * This function allocates and fills a drm_version structure with a hard coded
761 drmVersionPtr
drmGetLibVersion(int fd
)
763 drm_version_t
*version
= drmMalloc(sizeof(*version
));
766 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
767 * revision 1.0.x = original DRM interface with no drmGetLibVersion
768 * entry point and many drm<Device> extensions
769 * revision 1.1.x = added drmCommand entry points for device extensions
770 * added drmGetLibVersion to identify libdrm.a version
771 * revision 1.2.x = added drmSetInterfaceVersion
772 * modified drmOpen to handle both busid and name
773 * revision 1.3.x = added server + memory manager
775 version
->version_major
= 1;
776 version
->version_minor
= 3;
777 version
->version_patchlevel
= 0;
779 return (drmVersionPtr
)version
;
784 * Free the bus ID information.
786 * \param busid bus ID information string as given by drmGetBusid().
789 * This function is just frees the memory pointed by \p busid.
791 void drmFreeBusid(const char *busid
)
793 drmFree((void *)busid
);
798 * Get the bus ID of the device.
800 * \param fd file descriptor.
802 * \return bus ID string.
805 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
806 * get the string length and data, passing the arguments in a drm_unique
809 char *drmGetBusid(int fd
)
816 if (drmIoctl(fd
, DRM_IOCTL_GET_UNIQUE
, &u
))
818 u
.unique
= drmMalloc(u
.unique_len
+ 1);
819 if (drmIoctl(fd
, DRM_IOCTL_GET_UNIQUE
, &u
))
821 u
.unique
[u
.unique_len
] = '\0';
828 * Set the bus ID of the device.
830 * \param fd file descriptor.
831 * \param busid bus ID string.
833 * \return zero on success, negative on failure.
836 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
837 * the arguments in a drm_unique structure.
839 int drmSetBusid(int fd
, const char *busid
)
843 u
.unique
= (char *)busid
;
844 u
.unique_len
= strlen(busid
);
846 if (drmIoctl(fd
, DRM_IOCTL_SET_UNIQUE
, &u
)) {
852 int drmGetMagic(int fd
, drm_magic_t
* magic
)
857 if (drmIoctl(fd
, DRM_IOCTL_GET_MAGIC
, &auth
))
863 int drmAuthMagic(int fd
, drm_magic_t magic
)
868 if (drmIoctl(fd
, DRM_IOCTL_AUTH_MAGIC
, &auth
))
874 * Specifies a range of memory that is available for mapping by a
877 * \param fd file descriptor.
878 * \param offset usually the physical address. The actual meaning depends of
879 * the \p type parameter. See below.
880 * \param size of the memory in bytes.
881 * \param type type of the memory to be mapped.
882 * \param flags combination of several flags to modify the function actions.
883 * \param handle will be set to a value that may be used as the offset
884 * parameter for mmap().
886 * \return zero on success or a negative value on error.
888 * \par Mapping the frame buffer
889 * For the frame buffer
890 * - \p offset will be the physical address of the start of the frame buffer,
891 * - \p size will be the size of the frame buffer in bytes, and
892 * - \p type will be DRM_FRAME_BUFFER.
895 * The area mapped will be uncached. If MTRR support is available in the
896 * kernel, the frame buffer area will be set to write combining.
898 * \par Mapping the MMIO register area
899 * For the MMIO register area,
900 * - \p offset will be the physical address of the start of the register area,
901 * - \p size will be the size of the register area bytes, and
902 * - \p type will be DRM_REGISTERS.
904 * The area mapped will be uncached.
906 * \par Mapping the SAREA
908 * - \p offset will be ignored and should be set to zero,
909 * - \p size will be the desired size of the SAREA in bytes,
910 * - \p type will be DRM_SHM.
913 * A shared memory area of the requested size will be created and locked in
914 * kernel memory. This area may be mapped into client-space by using the handle
917 * \note May only be called by root.
920 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
921 * the arguments in a drm_map structure.
923 int drmAddMap(int fd
, drm_handle_t offset
, drmSize size
, drmMapType type
,
924 drmMapFlags flags
, drm_handle_t
*handle
)
933 if (drmIoctl(fd
, DRM_IOCTL_ADD_MAP
, &map
))
936 *handle
= (drm_handle_t
)map
.handle
;
940 int drmRmMap(int fd
, drm_handle_t handle
)
944 map
.handle
= (void *)handle
;
946 if(drmIoctl(fd
, DRM_IOCTL_RM_MAP
, &map
))
952 * Make buffers available for DMA transfers.
954 * \param fd file descriptor.
955 * \param count number of buffers.
956 * \param size size of each buffer.
957 * \param flags buffer allocation flags.
958 * \param agp_offset offset in the AGP aperture
960 * \return number of buffers allocated, negative on error.
963 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
967 int drmAddBufs(int fd
, int count
, int size
, drmBufDescFlags flags
,
970 drm_buf_desc_t request
;
972 request
.count
= count
;
974 request
.low_mark
= 0;
975 request
.high_mark
= 0;
976 request
.flags
= flags
;
977 request
.agp_start
= agp_offset
;
979 if (drmIoctl(fd
, DRM_IOCTL_ADD_BUFS
, &request
))
981 return request
.count
;
984 int drmMarkBufs(int fd
, double low
, double high
)
992 if (drmIoctl(fd
, DRM_IOCTL_INFO_BUFS
, &info
))
998 if (!(info
.list
= drmMalloc(info
.count
* sizeof(*info
.list
))))
1001 if (drmIoctl(fd
, DRM_IOCTL_INFO_BUFS
, &info
)) {
1002 int retval
= -errno
;
1007 for (i
= 0; i
< info
.count
; i
++) {
1008 info
.list
[i
].low_mark
= low
* info
.list
[i
].count
;
1009 info
.list
[i
].high_mark
= high
* info
.list
[i
].count
;
1010 if (drmIoctl(fd
, DRM_IOCTL_MARK_BUFS
, &info
.list
[i
])) {
1011 int retval
= -errno
;
1024 * \param fd file descriptor.
1025 * \param count number of buffers to free.
1026 * \param list list of buffers to be freed.
1028 * \return zero on success, or a negative value on failure.
1030 * \note This function is primarily used for debugging.
1033 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
1034 * the arguments in a drm_buf_free structure.
1036 int drmFreeBufs(int fd
, int count
, int *list
)
1038 drm_buf_free_t request
;
1040 request
.count
= count
;
1041 request
.list
= list
;
1042 if (drmIoctl(fd
, DRM_IOCTL_FREE_BUFS
, &request
))
1051 * \param fd file descriptor.
1054 * This function closes the file descriptor.
1056 int drmClose(int fd
)
1058 unsigned long key
= drmGetKeyFromFd(fd
);
1059 drmHashEntry
*entry
= drmGetEntry(fd
);
1061 drmHashDestroy(entry
->tagTable
);
1064 entry
->tagTable
= NULL
;
1066 drmHashDelete(drmHashTable
, key
);
1074 * Map a region of memory.
1076 * \param fd file descriptor.
1077 * \param handle handle returned by drmAddMap().
1078 * \param size size in bytes. Must match the size used by drmAddMap().
1079 * \param address will contain the user-space virtual address where the mapping
1082 * \return zero on success, or a negative value on failure.
1085 * This function is a wrapper for mmap().
1087 int drmMap(int fd
, drm_handle_t handle
, drmSize size
, drmAddressPtr address
)
1089 static unsigned long pagesize_mask
= 0;
1095 pagesize_mask
= getpagesize() - 1;
1097 size
= (size
+ pagesize_mask
) & ~pagesize_mask
;
1099 *address
= mmap(0, size
, PROT_READ
|PROT_WRITE
, MAP_SHARED
, fd
, handle
);
1100 if (*address
== MAP_FAILED
)
1107 * Unmap mappings obtained with drmMap().
1109 * \param address address as given by drmMap().
1110 * \param size size in bytes. Must match the size used by drmMap().
1112 * \return zero on success, or a negative value on failure.
1115 * This function is a wrapper for munmap().
1117 int drmUnmap(drmAddress address
, drmSize size
)
1119 return munmap(address
, size
);
1122 drmBufInfoPtr
drmGetBufInfo(int fd
)
1124 drm_buf_info_t info
;
1125 drmBufInfoPtr retval
;
1131 if (drmIoctl(fd
, DRM_IOCTL_INFO_BUFS
, &info
))
1135 if (!(info
.list
= drmMalloc(info
.count
* sizeof(*info
.list
))))
1138 if (drmIoctl(fd
, DRM_IOCTL_INFO_BUFS
, &info
)) {
1143 retval
= drmMalloc(sizeof(*retval
));
1144 retval
->count
= info
.count
;
1145 retval
->list
= drmMalloc(info
.count
* sizeof(*retval
->list
));
1146 for (i
= 0; i
< info
.count
; i
++) {
1147 retval
->list
[i
].count
= info
.list
[i
].count
;
1148 retval
->list
[i
].size
= info
.list
[i
].size
;
1149 retval
->list
[i
].low_mark
= info
.list
[i
].low_mark
;
1150 retval
->list
[i
].high_mark
= info
.list
[i
].high_mark
;
1159 * Map all DMA buffers into client-virtual space.
1161 * \param fd file descriptor.
1163 * \return a pointer to a ::drmBufMap structure.
1165 * \note The client may not use these buffers until obtaining buffer indices
1169 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1170 * information about the buffers in a drm_buf_map structure into the
1171 * client-visible data structures.
1173 drmBufMapPtr
drmMapBufs(int fd
)
1176 drmBufMapPtr retval
;
1181 bufs
.virtual = NULL
;
1182 if (drmIoctl(fd
, DRM_IOCTL_MAP_BUFS
, &bufs
))
1188 if (!(bufs
.list
= drmMalloc(bufs
.count
* sizeof(*bufs
.list
))))
1191 if (drmIoctl(fd
, DRM_IOCTL_MAP_BUFS
, &bufs
)) {
1196 retval
= drmMalloc(sizeof(*retval
));
1197 retval
->count
= bufs
.count
;
1198 retval
->list
= drmMalloc(bufs
.count
* sizeof(*retval
->list
));
1199 for (i
= 0; i
< bufs
.count
; i
++) {
1200 retval
->list
[i
].idx
= bufs
.list
[i
].idx
;
1201 retval
->list
[i
].total
= bufs
.list
[i
].total
;
1202 retval
->list
[i
].used
= 0;
1203 retval
->list
[i
].address
= bufs
.list
[i
].address
;
1213 * Unmap buffers allocated with drmMapBufs().
1215 * \return zero on success, or negative value on failure.
1218 * Calls munmap() for every buffer stored in \p bufs and frees the
1219 * memory allocated by drmMapBufs().
1221 int drmUnmapBufs(drmBufMapPtr bufs
)
1225 for (i
= 0; i
< bufs
->count
; i
++) {
1226 munmap(bufs
->list
[i
].address
, bufs
->list
[i
].total
);
1229 drmFree(bufs
->list
);
1236 #define DRM_DMA_RETRY 16
1239 * Reserve DMA buffers.
1241 * \param fd file descriptor.
1244 * \return zero on success, or a negative value on failure.
1247 * Assemble the arguments into a drm_dma structure and keeps issuing the
1248 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1250 int drmDMA(int fd
, drmDMAReqPtr request
)
1255 dma
.context
= request
->context
;
1256 dma
.send_count
= request
->send_count
;
1257 dma
.send_indices
= request
->send_list
;
1258 dma
.send_sizes
= request
->send_sizes
;
1259 dma
.flags
= request
->flags
;
1260 dma
.request_count
= request
->request_count
;
1261 dma
.request_size
= request
->request_size
;
1262 dma
.request_indices
= request
->request_list
;
1263 dma
.request_sizes
= request
->request_sizes
;
1264 dma
.granted_count
= 0;
1267 ret
= ioctl( fd
, DRM_IOCTL_DMA
, &dma
);
1268 } while ( ret
&& errno
== EAGAIN
&& i
++ < DRM_DMA_RETRY
);
1271 request
->granted_count
= dma
.granted_count
;
1280 * Obtain heavyweight hardware lock.
1282 * \param fd file descriptor.
1283 * \param context context.
1284 * \param flags flags that determine the sate of the hardware when the function
1287 * \return always zero.
1290 * This function translates the arguments into a drm_lock structure and issue
1291 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1293 int drmGetLock(int fd
, drm_context_t context
, drmLockFlags flags
)
1297 lock
.context
= context
;
1299 if (flags
& DRM_LOCK_READY
) lock
.flags
|= _DRM_LOCK_READY
;
1300 if (flags
& DRM_LOCK_QUIESCENT
) lock
.flags
|= _DRM_LOCK_QUIESCENT
;
1301 if (flags
& DRM_LOCK_FLUSH
) lock
.flags
|= _DRM_LOCK_FLUSH
;
1302 if (flags
& DRM_LOCK_FLUSH_ALL
) lock
.flags
|= _DRM_LOCK_FLUSH_ALL
;
1303 if (flags
& DRM_HALT_ALL_QUEUES
) lock
.flags
|= _DRM_HALT_ALL_QUEUES
;
1304 if (flags
& DRM_HALT_CUR_QUEUES
) lock
.flags
|= _DRM_HALT_CUR_QUEUES
;
1306 while (drmIoctl(fd
, DRM_IOCTL_LOCK
, &lock
))
1312 * Release the hardware lock.
1314 * \param fd file descriptor.
1315 * \param context context.
1317 * \return zero on success, or a negative value on failure.
1320 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1321 * argument in a drm_lock structure.
1323 int drmUnlock(int fd
, drm_context_t context
)
1327 lock
.context
= context
;
1329 return drmIoctl(fd
, DRM_IOCTL_UNLOCK
, &lock
);
1332 drm_context_t
*drmGetReservedContextList(int fd
, int *count
)
1336 drm_context_t
* retval
;
1340 res
.contexts
= NULL
;
1341 if (drmIoctl(fd
, DRM_IOCTL_RES_CTX
, &res
))
1347 if (!(list
= drmMalloc(res
.count
* sizeof(*list
))))
1349 if (!(retval
= drmMalloc(res
.count
* sizeof(*retval
)))) {
1354 res
.contexts
= list
;
1355 if (drmIoctl(fd
, DRM_IOCTL_RES_CTX
, &res
))
1358 for (i
= 0; i
< res
.count
; i
++)
1359 retval
[i
] = list
[i
].handle
;
1366 void drmFreeReservedContextList(drm_context_t
*pt
)
1374 * Used by the X server during GLXContext initialization. This causes
1375 * per-context kernel-level resources to be allocated.
1377 * \param fd file descriptor.
1378 * \param handle is set on success. To be used by the client when requesting DMA
1379 * dispatch with drmDMA().
1381 * \return zero on success, or a negative value on failure.
1383 * \note May only be called by root.
1386 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1387 * argument in a drm_ctx structure.
1389 int drmCreateContext(int fd
, drm_context_t
*handle
)
1393 ctx
.flags
= 0; /* Modified with functions below */
1394 if (drmIoctl(fd
, DRM_IOCTL_ADD_CTX
, &ctx
))
1396 *handle
= ctx
.handle
;
1400 int drmSwitchToContext(int fd
, drm_context_t context
)
1404 ctx
.handle
= context
;
1405 if (drmIoctl(fd
, DRM_IOCTL_SWITCH_CTX
, &ctx
))
1410 int drmSetContextFlags(int fd
, drm_context_t context
, drm_context_tFlags flags
)
1415 * Context preserving means that no context switches are done between DMA
1416 * buffers from one context and the next. This is suitable for use in the
1417 * X server (which promises to maintain hardware context), or in the
1418 * client-side library when buffers are swapped on behalf of two threads.
1420 ctx
.handle
= context
;
1422 if (flags
& DRM_CONTEXT_PRESERVED
)
1423 ctx
.flags
|= _DRM_CONTEXT_PRESERVED
;
1424 if (flags
& DRM_CONTEXT_2DONLY
)
1425 ctx
.flags
|= _DRM_CONTEXT_2DONLY
;
1426 if (drmIoctl(fd
, DRM_IOCTL_MOD_CTX
, &ctx
))
1431 int drmGetContextFlags(int fd
, drm_context_t context
,
1432 drm_context_tFlagsPtr flags
)
1436 ctx
.handle
= context
;
1437 if (drmIoctl(fd
, DRM_IOCTL_GET_CTX
, &ctx
))
1440 if (ctx
.flags
& _DRM_CONTEXT_PRESERVED
)
1441 *flags
|= DRM_CONTEXT_PRESERVED
;
1442 if (ctx
.flags
& _DRM_CONTEXT_2DONLY
)
1443 *flags
|= DRM_CONTEXT_2DONLY
;
1450 * Free any kernel-level resources allocated with drmCreateContext() associated
1453 * \param fd file descriptor.
1454 * \param handle handle given by drmCreateContext().
1456 * \return zero on success, or a negative value on failure.
1458 * \note May only be called by root.
1461 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1462 * argument in a drm_ctx structure.
1464 int drmDestroyContext(int fd
, drm_context_t handle
)
1467 ctx
.handle
= handle
;
1468 if (drmIoctl(fd
, DRM_IOCTL_RM_CTX
, &ctx
))
1473 int drmCreateDrawable(int fd
, drm_drawable_t
*handle
)
1476 if (drmIoctl(fd
, DRM_IOCTL_ADD_DRAW
, &draw
))
1478 *handle
= draw
.handle
;
1482 int drmDestroyDrawable(int fd
, drm_drawable_t handle
)
1485 draw
.handle
= handle
;
1486 if (drmIoctl(fd
, DRM_IOCTL_RM_DRAW
, &draw
))
1491 int drmUpdateDrawableInfo(int fd
, drm_drawable_t handle
,
1492 drm_drawable_info_type_t type
, unsigned int num
,
1495 drm_update_draw_t update
;
1497 update
.handle
= handle
;
1500 update
.data
= (unsigned long long)(unsigned long)data
;
1502 if (drmIoctl(fd
, DRM_IOCTL_UPDATE_DRAW
, &update
))
1509 * Acquire the AGP device.
1511 * Must be called before any of the other AGP related calls.
1513 * \param fd file descriptor.
1515 * \return zero on success, or a negative value on failure.
1518 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1520 int drmAgpAcquire(int fd
)
1522 if (drmIoctl(fd
, DRM_IOCTL_AGP_ACQUIRE
, NULL
))
1529 * Release the AGP device.
1531 * \param fd file descriptor.
1533 * \return zero on success, or a negative value on failure.
1536 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1538 int drmAgpRelease(int fd
)
1540 if (drmIoctl(fd
, DRM_IOCTL_AGP_RELEASE
, NULL
))
1549 * \param fd file descriptor.
1550 * \param mode AGP mode.
1552 * \return zero on success, or a negative value on failure.
1555 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1556 * argument in a drm_agp_mode structure.
1558 int drmAgpEnable(int fd
, unsigned long mode
)
1563 if (drmIoctl(fd
, DRM_IOCTL_AGP_ENABLE
, &m
))
1570 * Allocate a chunk of AGP memory.
1572 * \param fd file descriptor.
1573 * \param size requested memory size in bytes. Will be rounded to page boundary.
1574 * \param type type of memory to allocate.
1575 * \param address if not zero, will be set to the physical address of the
1577 * \param handle on success will be set to a handle of the allocated memory.
1579 * \return zero on success, or a negative value on failure.
1582 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1583 * arguments in a drm_agp_buffer structure.
1585 int drmAgpAlloc(int fd
, unsigned long size
, unsigned long type
,
1586 unsigned long *address
, drm_handle_t
*handle
)
1590 *handle
= DRM_AGP_NO_HANDLE
;
1594 if (drmIoctl(fd
, DRM_IOCTL_AGP_ALLOC
, &b
))
1597 *address
= b
.physical
;
1604 * Free a chunk of AGP memory.
1606 * \param fd file descriptor.
1607 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1609 * \return zero on success, or a negative value on failure.
1612 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1613 * argument in a drm_agp_buffer structure.
1615 int drmAgpFree(int fd
, drm_handle_t handle
)
1621 if (drmIoctl(fd
, DRM_IOCTL_AGP_FREE
, &b
))
1628 * Bind a chunk of AGP memory.
1630 * \param fd file descriptor.
1631 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1632 * \param offset offset in bytes. It will round to page boundary.
1634 * \return zero on success, or a negative value on failure.
1637 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1638 * argument in a drm_agp_binding structure.
1640 int drmAgpBind(int fd
, drm_handle_t handle
, unsigned long offset
)
1642 drm_agp_binding_t b
;
1646 if (drmIoctl(fd
, DRM_IOCTL_AGP_BIND
, &b
))
1653 * Unbind a chunk of AGP memory.
1655 * \param fd file descriptor.
1656 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1658 * \return zero on success, or a negative value on failure.
1661 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1662 * the argument in a drm_agp_binding structure.
1664 int drmAgpUnbind(int fd
, drm_handle_t handle
)
1666 drm_agp_binding_t b
;
1670 if (drmIoctl(fd
, DRM_IOCTL_AGP_UNBIND
, &b
))
1677 * Get AGP driver major version number.
1679 * \param fd file descriptor.
1681 * \return major version number on success, or a negative value on failure..
1684 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1685 * necessary information in a drm_agp_info structure.
1687 int drmAgpVersionMajor(int fd
)
1691 if (drmIoctl(fd
, DRM_IOCTL_AGP_INFO
, &i
))
1693 return i
.agp_version_major
;
1698 * Get AGP driver minor version number.
1700 * \param fd file descriptor.
1702 * \return minor version number on success, or a negative value on failure.
1705 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1706 * necessary information in a drm_agp_info structure.
1708 int drmAgpVersionMinor(int fd
)
1712 if (drmIoctl(fd
, DRM_IOCTL_AGP_INFO
, &i
))
1714 return i
.agp_version_minor
;
1721 * \param fd file descriptor.
1723 * \return mode on success, or zero on failure.
1726 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1727 * necessary information in a drm_agp_info structure.
1729 unsigned long drmAgpGetMode(int fd
)
1733 if (drmIoctl(fd
, DRM_IOCTL_AGP_INFO
, &i
))
1740 * Get AGP aperture base.
1742 * \param fd file descriptor.
1744 * \return aperture base on success, zero on failure.
1747 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1748 * necessary information in a drm_agp_info structure.
1750 unsigned long drmAgpBase(int fd
)
1754 if (drmIoctl(fd
, DRM_IOCTL_AGP_INFO
, &i
))
1756 return i
.aperture_base
;
1761 * Get AGP aperture size.
1763 * \param fd file descriptor.
1765 * \return aperture size on success, zero on failure.
1768 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1769 * necessary information in a drm_agp_info structure.
1771 unsigned long drmAgpSize(int fd
)
1775 if (drmIoctl(fd
, DRM_IOCTL_AGP_INFO
, &i
))
1777 return i
.aperture_size
;
1782 * Get used AGP memory.
1784 * \param fd file descriptor.
1786 * \return memory used on success, or zero on failure.
1789 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1790 * necessary information in a drm_agp_info structure.
1792 unsigned long drmAgpMemoryUsed(int fd
)
1796 if (drmIoctl(fd
, DRM_IOCTL_AGP_INFO
, &i
))
1798 return i
.memory_used
;
1803 * Get available AGP memory.
1805 * \param fd file descriptor.
1807 * \return memory available on success, or zero on failure.
1810 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1811 * necessary information in a drm_agp_info structure.
1813 unsigned long drmAgpMemoryAvail(int fd
)
1817 if (drmIoctl(fd
, DRM_IOCTL_AGP_INFO
, &i
))
1819 return i
.memory_allowed
;
1824 * Get hardware vendor ID.
1826 * \param fd file descriptor.
1828 * \return vendor ID on success, or zero on failure.
1831 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1832 * necessary information in a drm_agp_info structure.
1834 unsigned int drmAgpVendorId(int fd
)
1838 if (drmIoctl(fd
, DRM_IOCTL_AGP_INFO
, &i
))
1845 * Get hardware device ID.
1847 * \param fd file descriptor.
1849 * \return zero on success, or zero on failure.
1852 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1853 * necessary information in a drm_agp_info structure.
1855 unsigned int drmAgpDeviceId(int fd
)
1859 if (drmIoctl(fd
, DRM_IOCTL_AGP_INFO
, &i
))
1864 int drmScatterGatherAlloc(int fd
, unsigned long size
, drm_handle_t
*handle
)
1866 drm_scatter_gather_t sg
;
1871 if (drmIoctl(fd
, DRM_IOCTL_SG_ALLOC
, &sg
))
1873 *handle
= sg
.handle
;
1877 int drmScatterGatherFree(int fd
, drm_handle_t handle
)
1879 drm_scatter_gather_t sg
;
1883 if (drmIoctl(fd
, DRM_IOCTL_SG_FREE
, &sg
))
1891 * \param fd file descriptor.
1892 * \param vbl pointer to a drmVBlank structure.
1894 * \return zero on success, or a negative value on failure.
1897 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
1899 int drmWaitVBlank(int fd
, drmVBlankPtr vbl
)
1901 struct timespec timeout
, cur
;
1904 ret
= clock_gettime(CLOCK_MONOTONIC
, &timeout
);
1906 fprintf(stderr
, "clock_gettime failed: %s\n", strerror(ret
));
1912 ret
= ioctl(fd
, DRM_IOCTL_WAIT_VBLANK
, vbl
);
1913 vbl
->request
.type
&= ~DRM_VBLANK_RELATIVE
;
1914 if (ret
&& errno
== EINTR
) {
1915 clock_gettime(CLOCK_MONOTONIC
, &cur
);
1916 /* Timeout after 1s */
1917 if (cur
.tv_sec
> timeout
.tv_sec
+ 1 ||
1918 (cur
.tv_sec
== timeout
.tv_sec
&& cur
.tv_nsec
>=
1925 } while (ret
&& errno
== EINTR
);
1931 int drmError(int err
, const char *label
)
1934 case DRM_ERR_NO_DEVICE
:
1935 fprintf(stderr
, "%s: no device\n", label
);
1937 case DRM_ERR_NO_ACCESS
:
1938 fprintf(stderr
, "%s: no access\n", label
);
1940 case DRM_ERR_NOT_ROOT
:
1941 fprintf(stderr
, "%s: not root\n", label
);
1943 case DRM_ERR_INVALID
:
1944 fprintf(stderr
, "%s: invalid args\n", label
);
1949 fprintf( stderr
, "%s: error %d (%s)\n", label
, err
, strerror(err
) );
1957 * Install IRQ handler.
1959 * \param fd file descriptor.
1960 * \param irq IRQ number.
1962 * \return zero on success, or a negative value on failure.
1965 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
1966 * argument in a drm_control structure.
1968 int drmCtlInstHandler(int fd
, int irq
)
1972 ctl
.func
= DRM_INST_HANDLER
;
1974 if (drmIoctl(fd
, DRM_IOCTL_CONTROL
, &ctl
))
1981 * Uninstall IRQ handler.
1983 * \param fd file descriptor.
1985 * \return zero on success, or a negative value on failure.
1988 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
1989 * argument in a drm_control structure.
1991 int drmCtlUninstHandler(int fd
)
1995 ctl
.func
= DRM_UNINST_HANDLER
;
1997 if (drmIoctl(fd
, DRM_IOCTL_CONTROL
, &ctl
))
2002 int drmFinish(int fd
, int context
, drmLockFlags flags
)
2006 lock
.context
= context
;
2008 if (flags
& DRM_LOCK_READY
) lock
.flags
|= _DRM_LOCK_READY
;
2009 if (flags
& DRM_LOCK_QUIESCENT
) lock
.flags
|= _DRM_LOCK_QUIESCENT
;
2010 if (flags
& DRM_LOCK_FLUSH
) lock
.flags
|= _DRM_LOCK_FLUSH
;
2011 if (flags
& DRM_LOCK_FLUSH_ALL
) lock
.flags
|= _DRM_LOCK_FLUSH_ALL
;
2012 if (flags
& DRM_HALT_ALL_QUEUES
) lock
.flags
|= _DRM_HALT_ALL_QUEUES
;
2013 if (flags
& DRM_HALT_CUR_QUEUES
) lock
.flags
|= _DRM_HALT_CUR_QUEUES
;
2014 if (drmIoctl(fd
, DRM_IOCTL_FINISH
, &lock
))
2020 * Get IRQ from bus ID.
2022 * \param fd file descriptor.
2023 * \param busnum bus number.
2024 * \param devnum device number.
2025 * \param funcnum function number.
2027 * \return IRQ number on success, or a negative value on failure.
2030 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
2031 * arguments in a drm_irq_busid structure.
2033 int drmGetInterruptFromBusID(int fd
, int busnum
, int devnum
, int funcnum
)
2039 p
.funcnum
= funcnum
;
2040 if (drmIoctl(fd
, DRM_IOCTL_IRQ_BUSID
, &p
))
2045 int drmAddContextTag(int fd
, drm_context_t context
, void *tag
)
2047 drmHashEntry
*entry
= drmGetEntry(fd
);
2049 if (drmHashInsert(entry
->tagTable
, context
, tag
)) {
2050 drmHashDelete(entry
->tagTable
, context
);
2051 drmHashInsert(entry
->tagTable
, context
, tag
);
2056 int drmDelContextTag(int fd
, drm_context_t context
)
2058 drmHashEntry
*entry
= drmGetEntry(fd
);
2060 return drmHashDelete(entry
->tagTable
, context
);
2063 void *drmGetContextTag(int fd
, drm_context_t context
)
2065 drmHashEntry
*entry
= drmGetEntry(fd
);
2068 if (drmHashLookup(entry
->tagTable
, context
, &value
))
2074 int drmAddContextPrivateMapping(int fd
, drm_context_t ctx_id
,
2075 drm_handle_t handle
)
2077 drm_ctx_priv_map_t map
;
2079 map
.ctx_id
= ctx_id
;
2080 map
.handle
= (void *)handle
;
2082 if (drmIoctl(fd
, DRM_IOCTL_SET_SAREA_CTX
, &map
))
2087 int drmGetContextPrivateMapping(int fd
, drm_context_t ctx_id
,
2088 drm_handle_t
*handle
)
2090 drm_ctx_priv_map_t map
;
2092 map
.ctx_id
= ctx_id
;
2094 if (drmIoctl(fd
, DRM_IOCTL_GET_SAREA_CTX
, &map
))
2097 *handle
= (drm_handle_t
)map
.handle
;
2102 int drmGetMap(int fd
, int idx
, drm_handle_t
*offset
, drmSize
*size
,
2103 drmMapType
*type
, drmMapFlags
*flags
, drm_handle_t
*handle
,
2109 if (drmIoctl(fd
, DRM_IOCTL_GET_MAP
, &map
))
2111 *offset
= map
.offset
;
2115 *handle
= (unsigned long)map
.handle
;
2120 int drmGetClient(int fd
, int idx
, int *auth
, int *pid
, int *uid
,
2121 unsigned long *magic
, unsigned long *iocs
)
2123 drm_client_t client
;
2126 if (drmIoctl(fd
, DRM_IOCTL_GET_CLIENT
, &client
))
2128 *auth
= client
.auth
;
2131 *magic
= client
.magic
;
2132 *iocs
= client
.iocs
;
2136 int drmGetStats(int fd
, drmStatsT
*stats
)
2141 if (drmIoctl(fd
, DRM_IOCTL_GET_STATS
, &s
))
2145 memset(stats
, 0, sizeof(*stats
));
2146 if (s
.count
> sizeof(stats
->data
)/sizeof(stats
->data
[0]))
2150 stats->data[i].long_format = "%-20.20s"; \
2151 stats->data[i].rate_format = "%8.8s"; \
2152 stats->data[i].isvalue = 1; \
2153 stats->data[i].verbose = 0
2156 stats->data[i].long_format = "%-20.20s"; \
2157 stats->data[i].rate_format = "%5.5s"; \
2158 stats->data[i].isvalue = 0; \
2159 stats->data[i].mult_names = "kgm"; \
2160 stats->data[i].mult = 1000; \
2161 stats->data[i].verbose = 0
2164 stats->data[i].long_format = "%-20.20s"; \
2165 stats->data[i].rate_format = "%5.5s"; \
2166 stats->data[i].isvalue = 0; \
2167 stats->data[i].mult_names = "KGM"; \
2168 stats->data[i].mult = 1024; \
2169 stats->data[i].verbose = 0
2172 stats
->count
= s
.count
;
2173 for (i
= 0; i
< s
.count
; i
++) {
2174 stats
->data
[i
].value
= s
.data
[i
].value
;
2175 switch (s
.data
[i
].type
) {
2176 case _DRM_STAT_LOCK
:
2177 stats
->data
[i
].long_name
= "Lock";
2178 stats
->data
[i
].rate_name
= "Lock";
2181 case _DRM_STAT_OPENS
:
2182 stats
->data
[i
].long_name
= "Opens";
2183 stats
->data
[i
].rate_name
= "O";
2185 stats
->data
[i
].verbose
= 1;
2187 case _DRM_STAT_CLOSES
:
2188 stats
->data
[i
].long_name
= "Closes";
2189 stats
->data
[i
].rate_name
= "Lock";
2191 stats
->data
[i
].verbose
= 1;
2193 case _DRM_STAT_IOCTLS
:
2194 stats
->data
[i
].long_name
= "Ioctls";
2195 stats
->data
[i
].rate_name
= "Ioc/s";
2198 case _DRM_STAT_LOCKS
:
2199 stats
->data
[i
].long_name
= "Locks";
2200 stats
->data
[i
].rate_name
= "Lck/s";
2203 case _DRM_STAT_UNLOCKS
:
2204 stats
->data
[i
].long_name
= "Unlocks";
2205 stats
->data
[i
].rate_name
= "Unl/s";
2209 stats
->data
[i
].long_name
= "IRQs";
2210 stats
->data
[i
].rate_name
= "IRQ/s";
2213 case _DRM_STAT_PRIMARY
:
2214 stats
->data
[i
].long_name
= "Primary Bytes";
2215 stats
->data
[i
].rate_name
= "PB/s";
2218 case _DRM_STAT_SECONDARY
:
2219 stats
->data
[i
].long_name
= "Secondary Bytes";
2220 stats
->data
[i
].rate_name
= "SB/s";
2224 stats
->data
[i
].long_name
= "DMA";
2225 stats
->data
[i
].rate_name
= "DMA/s";
2228 case _DRM_STAT_SPECIAL
:
2229 stats
->data
[i
].long_name
= "Special DMA";
2230 stats
->data
[i
].rate_name
= "dma/s";
2233 case _DRM_STAT_MISSED
:
2234 stats
->data
[i
].long_name
= "Miss";
2235 stats
->data
[i
].rate_name
= "Ms/s";
2238 case _DRM_STAT_VALUE
:
2239 stats
->data
[i
].long_name
= "Value";
2240 stats
->data
[i
].rate_name
= "Value";
2243 case _DRM_STAT_BYTE
:
2244 stats
->data
[i
].long_name
= "Bytes";
2245 stats
->data
[i
].rate_name
= "B/s";
2248 case _DRM_STAT_COUNT
:
2250 stats
->data
[i
].long_name
= "Count";
2251 stats
->data
[i
].rate_name
= "Cnt/s";
2260 * Issue a set-version ioctl.
2262 * \param fd file descriptor.
2263 * \param drmCommandIndex command index
2264 * \param data source pointer of the data to be read and written.
2265 * \param size size of the data to be read and written.
2267 * \return zero on success, or a negative value on failure.
2270 * It issues a read-write ioctl given by
2271 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2273 int drmSetInterfaceVersion(int fd
, drmSetVersion
*version
)
2276 drm_set_version_t sv
;
2278 sv
.drm_di_major
= version
->drm_di_major
;
2279 sv
.drm_di_minor
= version
->drm_di_minor
;
2280 sv
.drm_dd_major
= version
->drm_dd_major
;
2281 sv
.drm_dd_minor
= version
->drm_dd_minor
;
2283 if (drmIoctl(fd
, DRM_IOCTL_SET_VERSION
, &sv
)) {
2287 version
->drm_di_major
= sv
.drm_di_major
;
2288 version
->drm_di_minor
= sv
.drm_di_minor
;
2289 version
->drm_dd_major
= sv
.drm_dd_major
;
2290 version
->drm_dd_minor
= sv
.drm_dd_minor
;
2296 * Send a device-specific command.
2298 * \param fd file descriptor.
2299 * \param drmCommandIndex command index
2301 * \return zero on success, or a negative value on failure.
2304 * It issues a ioctl given by
2305 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2307 int drmCommandNone(int fd
, unsigned long drmCommandIndex
)
2309 void *data
= NULL
; /* dummy */
2310 unsigned long request
;
2312 request
= DRM_IO( DRM_COMMAND_BASE
+ drmCommandIndex
);
2314 if (drmIoctl(fd
, request
, data
)) {
2322 * Send a device-specific read command.
2324 * \param fd file descriptor.
2325 * \param drmCommandIndex command index
2326 * \param data destination pointer of the data to be read.
2327 * \param size size of the data to be read.
2329 * \return zero on success, or a negative value on failure.
2332 * It issues a read ioctl given by
2333 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2335 int drmCommandRead(int fd
, unsigned long drmCommandIndex
, void *data
,
2338 unsigned long request
;
2340 request
= DRM_IOC( DRM_IOC_READ
, DRM_IOCTL_BASE
,
2341 DRM_COMMAND_BASE
+ drmCommandIndex
, size
);
2343 if (drmIoctl(fd
, request
, data
)) {
2351 * Send a device-specific write command.
2353 * \param fd file descriptor.
2354 * \param drmCommandIndex command index
2355 * \param data source pointer of the data to be written.
2356 * \param size size of the data to be written.
2358 * \return zero on success, or a negative value on failure.
2361 * It issues a write ioctl given by
2362 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2364 int drmCommandWrite(int fd
, unsigned long drmCommandIndex
, void *data
,
2367 unsigned long request
;
2369 request
= DRM_IOC( DRM_IOC_WRITE
, DRM_IOCTL_BASE
,
2370 DRM_COMMAND_BASE
+ drmCommandIndex
, size
);
2372 if (drmIoctl(fd
, request
, data
)) {
2380 * Send a device-specific read-write command.
2382 * \param fd file descriptor.
2383 * \param drmCommandIndex command index
2384 * \param data source pointer of the data to be read and written.
2385 * \param size size of the data to be read and written.
2387 * \return zero on success, or a negative value on failure.
2390 * It issues a read-write ioctl given by
2391 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2393 int drmCommandWriteRead(int fd
, unsigned long drmCommandIndex
, void *data
,
2396 unsigned long request
;
2398 request
= DRM_IOC( DRM_IOC_READ
|DRM_IOC_WRITE
, DRM_IOCTL_BASE
,
2399 DRM_COMMAND_BASE
+ drmCommandIndex
, size
);
2401 if (drmIoctl(fd
, request
, data
))
2406 #define DRM_MAX_FDS 16
2411 } connection
[DRM_MAX_FDS
];
2413 static int nr_fds
= 0;
2415 int drmOpenOnce(void *unused
,
2422 for (i
= 0; i
< nr_fds
; i
++)
2423 if (strcmp(BusID
, connection
[i
].BusID
) == 0) {
2424 connection
[i
].refcount
++;
2426 return connection
[i
].fd
;
2429 fd
= drmOpen(unused
, BusID
);
2430 if (fd
<= 0 || nr_fds
== DRM_MAX_FDS
)
2433 connection
[nr_fds
].BusID
= strdup(BusID
);
2434 connection
[nr_fds
].fd
= fd
;
2435 connection
[nr_fds
].refcount
= 1;
2439 fprintf(stderr
, "saved connection %d for %s %d\n",
2440 nr_fds
, connection
[nr_fds
].BusID
,
2441 strcmp(BusID
, connection
[nr_fds
].BusID
));
2448 void drmCloseOnce(int fd
)
2452 for (i
= 0; i
< nr_fds
; i
++) {
2453 if (fd
== connection
[i
].fd
) {
2454 if (--connection
[i
].refcount
== 0) {
2455 drmClose(connection
[i
].fd
);
2456 free(connection
[i
].BusID
);
2459 connection
[i
] = connection
[nr_fds
];
2467 int drmSetMaster(int fd
)
2471 fprintf(stderr
,"Setting master \n");
2472 ret
= ioctl(fd
, DRM_IOCTL_SET_MASTER
, 0);
2476 int drmDropMaster(int fd
)
2479 fprintf(stderr
,"Dropping master \n");
2480 ret
= ioctl(fd
, DRM_IOCTL_DROP_MASTER
, 0);