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.
47 #include <sys/types.h>
49 #define stat_t struct stat
50 #include <sys/ioctl.h>
54 /* Not all systems have MAP_FAILED defined */
56 #define MAP_FAILED ((void *)-1)
62 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
75 #define DRM_MAJOR 226 /* Linux */
79 * This definition needs to be changed on some systems if dev_t is a structure.
80 * If there is a header file we can get it from, there would be best.
83 #define makedev(x,y) ((dev_t)(((x) << 8) | (y)))
86 #define DRM_MSG_VERBOSITY 3
88 #define DRM_NODE_CONTROL 0
89 #define DRM_NODE_PRIMARY 1
90 #define DRM_NODE_RENDER 2
92 static drmServerInfoPtr drm_server_info;
94 void drmSetServerInfo(drmServerInfoPtr info)
96 drm_server_info = info;
100 * Output a message to stderr.
102 * \param format printf() like format string.
105 * This function is a wrapper around vfprintf().
108 static int DRM_PRINTFLIKE(1, 0)
109 drmDebugPrint(const char *format, va_list ap)
111 return vfprintf(stderr, format, ap);
114 typedef int DRM_PRINTFLIKE(1, 0) (*debug_msg_func_t)(const char *format,
117 static debug_msg_func_t drm_debug_print = drmDebugPrint;
120 drmMsg(const char *format, ...)
124 if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) || drm_server_info)
126 va_start(ap, format);
127 if (drm_server_info) {
128 drm_server_info->debug_print(format,ap);
130 drm_debug_print(format, ap);
137 drmSetDebugMsgFunction(debug_msg_func_t debug_msg_ptr)
139 drm_debug_print = debug_msg_ptr;
142 static void *drmHashTable = NULL; /* Context switch callbacks */
144 void *drmGetHashTable(void)
149 void *drmMalloc(int size)
152 if ((pt = malloc(size)))
157 void drmFree(void *pt)
164 * Call ioctl, restarting if it is interupted
167 drmIoctl(int fd, unsigned long request, void *arg)
172 ret = ioctl(fd, request, arg);
173 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
177 static unsigned long drmGetKeyFromFd(int fd)
186 drmHashEntry *drmGetEntry(int fd)
188 unsigned long key = drmGetKeyFromFd(fd);
193 drmHashTable = drmHashCreate();
195 if (drmHashLookup(drmHashTable, key, &value)) {
196 entry = drmMalloc(sizeof(*entry));
199 entry->tagTable = drmHashCreate();
200 drmHashInsert(drmHashTable, key, entry);
208 * Compare two busid strings
213 * \return 1 if matched.
216 * This function compares two bus ID strings. It understands the older
217 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
218 * domain, b is bus, d is device, f is function.
220 static int drmMatchBusID(const char *id1, const char *id2, int pci_domain_ok)
222 /* First, check if the IDs are exactly the same */
223 if (strcasecmp(id1, id2) == 0)
226 /* Try to match old/new-style PCI bus IDs. */
227 if (strncasecmp(id1, "pci", 3) == 0) {
228 unsigned int o1, b1, d1, f1;
229 unsigned int o2, b2, d2, f2;
232 ret = sscanf(id1, "pci:%04x:%02x:%02x.%u", &o1, &b1, &d1, &f1);
235 ret = sscanf(id1, "PCI:%u:%u:%u", &b1, &d1, &f1);
240 ret = sscanf(id2, "pci:%04x:%02x:%02x.%u", &o2, &b2, &d2, &f2);
243 ret = sscanf(id2, "PCI:%u:%u:%u", &b2, &d2, &f2);
248 /* If domains aren't properly supported by the kernel interface,
249 * just ignore them, which sucks less than picking a totally random
250 * card with "open by name"
255 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
264 * Handles error checking for chown call.
266 * \param path to file.
267 * \param id of the new owner.
268 * \param id of the new group.
270 * \return zero if success or -1 if failure.
273 * Checks for failure. If failure was caused by signal call chown again.
274 * If any other failure happened then it will output error mesage using
277 static int chown_check_return(const char *path, uid_t owner, gid_t group)
282 rv = chown(path, owner, group);
283 } while (rv != 0 && errno == EINTR);
288 drmMsg("Failed to change owner or group for file %s! %d: %s\n",
289 path, errno, strerror(errno));
294 * Open the DRM device, creating it if necessary.
296 * \param dev major and minor numbers of the device.
297 * \param minor minor number of the device.
299 * \return a file descriptor on success, or a negative value on error.
302 * Assembles the device name from \p minor and opens it, creating the device
303 * special file node with the major and minor numbers specified by \p dev and
304 * parent directory if necessary and was called by root.
306 static int drmOpenDevice(dev_t dev, int minor, int type)
309 const char *dev_name;
312 mode_t devmode = DRM_DEV_MODE, serv_mode;
313 int isroot = !geteuid();
314 uid_t user = DRM_DEV_UID;
315 gid_t group = DRM_DEV_GID, serv_group;
318 case DRM_NODE_PRIMARY:
319 dev_name = DRM_DEV_NAME;
321 case DRM_NODE_CONTROL:
322 dev_name = DRM_CONTROL_DEV_NAME;
324 case DRM_NODE_RENDER:
325 dev_name = DRM_RENDER_DEV_NAME;
331 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
332 drmMsg("drmOpenDevice: node name is %s\n", buf);
334 if (drm_server_info) {
335 drm_server_info->get_perms(&serv_group, &serv_mode);
336 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
337 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
338 group = (serv_group >= 0) ? serv_group : DRM_DEV_GID;
342 if (stat(DRM_DIR_NAME, &st)) {
344 return DRM_ERR_NOT_ROOT;
345 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
346 chown_check_return(DRM_DIR_NAME, 0, 0); /* root:root */
347 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
350 /* Check if the device node exists and create it if necessary. */
351 if (stat(buf, &st)) {
353 return DRM_ERR_NOT_ROOT;
355 mknod(buf, S_IFCHR | devmode, dev);
358 if (drm_server_info) {
359 chown_check_return(buf, user, group);
363 /* if we modprobed then wait for udev */
367 if (stat(DRM_DIR_NAME, &st)) {
371 if (udev_count == 50)
376 if (stat(buf, &st)) {
380 if (udev_count == 50)
387 fd = open(buf, O_RDWR, 0);
388 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
389 fd, fd < 0 ? strerror(errno) : "OK");
394 /* Check if the device node is not what we expect it to be, and recreate it
395 * and try again if so.
397 if (st.st_rdev != dev) {
399 return DRM_ERR_NOT_ROOT;
401 mknod(buf, S_IFCHR | devmode, dev);
402 if (drm_server_info) {
403 chown_check_return(buf, user, group);
407 fd = open(buf, O_RDWR, 0);
408 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
409 fd, fd < 0 ? strerror(errno) : "OK");
413 drmMsg("drmOpenDevice: Open failed\n");
421 * Open the DRM device
423 * \param minor device minor number.
424 * \param create allow to create the device if set.
426 * \return a file descriptor on success, or a negative value on error.
429 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
430 * name from \p minor and opens it.
432 static int drmOpenMinor(int minor, int create, int type)
436 const char *dev_name;
439 return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
442 case DRM_NODE_PRIMARY:
443 dev_name = DRM_DEV_NAME;
445 case DRM_NODE_CONTROL:
446 dev_name = DRM_CONTROL_DEV_NAME;
448 case DRM_NODE_RENDER:
449 dev_name = DRM_RENDER_DEV_NAME;
455 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
456 if ((fd = open(buf, O_RDWR, 0)) >= 0)
463 * Determine whether the DRM kernel driver has been loaded.
465 * \return 1 if the DRM driver is loaded, 0 otherwise.
468 * Determine the presence of the kernel driver by attempting to open the 0
469 * minor and get version information. For backward compatibility with older
470 * Linux implementations, /proc/dri is also checked.
472 int drmAvailable(void)
474 drmVersionPtr version;
478 if ((fd = drmOpenMinor(0, 1, DRM_NODE_PRIMARY)) < 0) {
480 /* Try proc for backward Linux compatibility */
481 if (!access("/proc/dri/0", R_OK))
487 if ((version = drmGetVersion(fd))) {
489 drmFreeVersion(version);
498 * Open the device by bus ID.
500 * \param busid bus ID.
502 * \return a file descriptor on success, or a negative value on error.
505 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
506 * comparing the device bus ID with the one supplied.
508 * \sa drmOpenMinor() and drmGetBusid().
510 static int drmOpenByBusid(const char *busid)
512 int i, pci_domain_ok = 1;
517 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
518 for (i = 0; i < DRM_MAX_MINOR; i++) {
519 fd = drmOpenMinor(i, 1, DRM_NODE_PRIMARY);
520 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
522 /* We need to try for 1.4 first for proper PCI domain support
523 * and if that fails, we know the kernel is busted
527 sv.drm_dd_major = -1; /* Don't care */
528 sv.drm_dd_minor = -1; /* Don't care */
529 if (drmSetInterfaceVersion(fd, &sv)) {
535 sv.drm_dd_major = -1; /* Don't care */
536 sv.drm_dd_minor = -1; /* Don't care */
537 drmMsg("drmOpenByBusid: Interface 1.4 failed, trying 1.1\n");
538 drmSetInterfaceVersion(fd, &sv);
540 buf = drmGetBusid(fd);
541 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
542 if (buf && drmMatchBusID(buf, busid, pci_domain_ok)) {
556 * Open the device by name.
558 * \param name driver name.
560 * \return a file descriptor on success, or a negative value on error.
563 * This function opens the first minor number that matches the driver name and
564 * isn't already in use. If it's in use it then it will already have a bus ID
567 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
569 static int drmOpenByName(const char *name)
573 drmVersionPtr version;
577 * Open the first minor number that matches the driver name and isn't
578 * already in use. If it's in use it will have a busid assigned already.
580 for (i = 0; i < DRM_MAX_MINOR; i++) {
581 if ((fd = drmOpenMinor(i, 1, DRM_NODE_PRIMARY)) >= 0) {
582 if ((version = drmGetVersion(fd))) {
583 if (!strcmp(version->name, name)) {
584 drmFreeVersion(version);
585 id = drmGetBusid(fd);
586 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
595 drmFreeVersion(version);
603 /* Backward-compatibility /proc support */
604 for (i = 0; i < 8; i++) {
605 char proc_name[64], buf[512];
606 char *driver, *pt, *devstring;
609 sprintf(proc_name, "/proc/dri/%d/name", i);
610 if ((fd = open(proc_name, 0, 0)) >= 0) {
611 retcode = read(fd, buf, sizeof(buf)-1);
614 buf[retcode-1] = '\0';
615 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
617 if (*pt) { /* Device is next */
619 if (!strcmp(driver, name)) { /* Match */
620 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
622 if (*pt) { /* Found busid */
623 return drmOpenByBusid(++pt);
624 } else { /* No busid */
625 return drmOpenDevice(strtol(devstring, NULL, 0),i, DRM_NODE_PRIMARY);
639 * Open the DRM device.
641 * Looks up the specified name and bus ID, and opens the device found. The
642 * entry in /dev/dri is created if necessary and if called by root.
644 * \param name driver name. Not referenced if bus ID is supplied.
645 * \param busid bus ID. Zero if not known.
647 * \return a file descriptor on success, or a negative value on error.
650 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
653 int drmOpen(const char *name, const char *busid)
655 if (!drmAvailable() && name != NULL && drm_server_info) {
656 /* try to load the kernel */
657 if (!drm_server_info->load_module(name)) {
658 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
664 int fd = drmOpenByBusid(busid);
670 return drmOpenByName(name);
675 int drmOpenControl(int minor)
677 return drmOpenMinor(minor, 0, DRM_NODE_CONTROL);
680 int drmOpenRender(int minor)
682 return drmOpenMinor(minor, 0, DRM_NODE_RENDER);
686 * Free the version information returned by drmGetVersion().
688 * \param v pointer to the version information.
691 * It frees the memory pointed by \p %v as well as all the non-null strings
694 void drmFreeVersion(drmVersionPtr v)
706 * Free the non-public version information returned by the kernel.
708 * \param v pointer to the version information.
711 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
712 * the non-null strings pointers in it.
714 static void drmFreeKernelVersion(drm_version_t *v)
726 * Copy version information.
728 * \param d destination pointer.
729 * \param s source pointer.
732 * Used by drmGetVersion() to translate the information returned by the ioctl
733 * interface in a private structure into the public structure counterpart.
735 static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
737 d->version_major = s->version_major;
738 d->version_minor = s->version_minor;
739 d->version_patchlevel = s->version_patchlevel;
740 d->name_len = s->name_len;
741 d->name = strdup(s->name);
742 d->date_len = s->date_len;
743 d->date = strdup(s->date);
744 d->desc_len = s->desc_len;
745 d->desc = strdup(s->desc);
750 * Query the driver version information.
752 * \param fd file descriptor.
754 * \return pointer to a drmVersion structure which should be freed with
757 * \note Similar information is available via /proc/dri.
760 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
761 * first with zeros to get the string lengths, and then the actually strings.
762 * It also null-terminates them since they might not be already.
764 drmVersionPtr drmGetVersion(int fd)
766 drmVersionPtr retval;
767 drm_version_t *version = drmMalloc(sizeof(*version));
769 version->name_len = 0;
770 version->name = NULL;
771 version->date_len = 0;
772 version->date = NULL;
773 version->desc_len = 0;
774 version->desc = NULL;
776 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
777 drmFreeKernelVersion(version);
781 if (version->name_len)
782 version->name = drmMalloc(version->name_len + 1);
783 if (version->date_len)
784 version->date = drmMalloc(version->date_len + 1);
785 if (version->desc_len)
786 version->desc = drmMalloc(version->desc_len + 1);
788 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
789 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
790 drmFreeKernelVersion(version);
794 /* The results might not be null-terminated strings, so terminate them. */
795 if (version->name_len) version->name[version->name_len] = '\0';
796 if (version->date_len) version->date[version->date_len] = '\0';
797 if (version->desc_len) version->desc[version->desc_len] = '\0';
799 retval = drmMalloc(sizeof(*retval));
800 drmCopyVersion(retval, version);
801 drmFreeKernelVersion(version);
807 * Get version information for the DRM user space library.
809 * This version number is driver independent.
811 * \param fd file descriptor.
813 * \return version information.
816 * This function allocates and fills a drm_version structure with a hard coded
819 drmVersionPtr drmGetLibVersion(int fd)
821 drm_version_t *version = drmMalloc(sizeof(*version));
824 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
825 * revision 1.0.x = original DRM interface with no drmGetLibVersion
826 * entry point and many drm<Device> extensions
827 * revision 1.1.x = added drmCommand entry points for device extensions
828 * added drmGetLibVersion to identify libdrm.a version
829 * revision 1.2.x = added drmSetInterfaceVersion
830 * modified drmOpen to handle both busid and name
831 * revision 1.3.x = added server + memory manager
833 version->version_major = 1;
834 version->version_minor = 3;
835 version->version_patchlevel = 0;
837 return (drmVersionPtr)version;
840 int drmGetCap(int fd, uint64_t capability, uint64_t *value)
842 struct drm_get_cap cap = { capability, 0 };
845 ret = drmIoctl(fd, DRM_IOCTL_GET_CAP, &cap);
853 int drmSetClientCap(int fd, uint64_t capability, uint64_t value)
855 struct drm_set_client_cap cap = { capability, value };
857 return drmIoctl(fd, DRM_IOCTL_SET_CLIENT_CAP, &cap);
861 * Free the bus ID information.
863 * \param busid bus ID information string as given by drmGetBusid().
866 * This function is just frees the memory pointed by \p busid.
868 void drmFreeBusid(const char *busid)
870 drmFree((void *)busid);
875 * Get the bus ID of the device.
877 * \param fd file descriptor.
879 * \return bus ID string.
882 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
883 * get the string length and data, passing the arguments in a drm_unique
886 char *drmGetBusid(int fd)
893 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
895 u.unique = drmMalloc(u.unique_len + 1);
896 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
898 u.unique[u.unique_len] = '\0';
905 * Set the bus ID of the device.
907 * \param fd file descriptor.
908 * \param busid bus ID string.
910 * \return zero on success, negative on failure.
913 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
914 * the arguments in a drm_unique structure.
916 int drmSetBusid(int fd, const char *busid)
920 u.unique = (char *)busid;
921 u.unique_len = strlen(busid);
923 if (drmIoctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
929 int drmGetMagic(int fd, drm_magic_t * magic)
934 if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
940 int drmAuthMagic(int fd, drm_magic_t magic)
945 if (drmIoctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
951 * Specifies a range of memory that is available for mapping by a
954 * \param fd file descriptor.
955 * \param offset usually the physical address. The actual meaning depends of
956 * the \p type parameter. See below.
957 * \param size of the memory in bytes.
958 * \param type type of the memory to be mapped.
959 * \param flags combination of several flags to modify the function actions.
960 * \param handle will be set to a value that may be used as the offset
961 * parameter for mmap().
963 * \return zero on success or a negative value on error.
965 * \par Mapping the frame buffer
966 * For the frame buffer
967 * - \p offset will be the physical address of the start of the frame buffer,
968 * - \p size will be the size of the frame buffer in bytes, and
969 * - \p type will be DRM_FRAME_BUFFER.
972 * The area mapped will be uncached. If MTRR support is available in the
973 * kernel, the frame buffer area will be set to write combining.
975 * \par Mapping the MMIO register area
976 * For the MMIO register area,
977 * - \p offset will be the physical address of the start of the register area,
978 * - \p size will be the size of the register area bytes, and
979 * - \p type will be DRM_REGISTERS.
981 * The area mapped will be uncached.
983 * \par Mapping the SAREA
985 * - \p offset will be ignored and should be set to zero,
986 * - \p size will be the desired size of the SAREA in bytes,
987 * - \p type will be DRM_SHM.
990 * A shared memory area of the requested size will be created and locked in
991 * kernel memory. This area may be mapped into client-space by using the handle
994 * \note May only be called by root.
997 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
998 * the arguments in a drm_map structure.
1000 int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
1001 drmMapFlags flags, drm_handle_t *handle)
1005 map.offset = offset;
1010 if (drmIoctl(fd, DRM_IOCTL_ADD_MAP, &map))
1013 *handle = (drm_handle_t)(uintptr_t)map.handle;
1017 int drmRmMap(int fd, drm_handle_t handle)
1021 map.handle = (void *)(uintptr_t)handle;
1023 if(drmIoctl(fd, DRM_IOCTL_RM_MAP, &map))
1029 * Make buffers available for DMA transfers.
1031 * \param fd file descriptor.
1032 * \param count number of buffers.
1033 * \param size size of each buffer.
1034 * \param flags buffer allocation flags.
1035 * \param agp_offset offset in the AGP aperture
1037 * \return number of buffers allocated, negative on error.
1040 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
1044 int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
1047 drm_buf_desc_t request;
1049 request.count = count;
1050 request.size = size;
1051 request.low_mark = 0;
1052 request.high_mark = 0;
1053 request.flags = flags;
1054 request.agp_start = agp_offset;
1056 if (drmIoctl(fd, DRM_IOCTL_ADD_BUFS, &request))
1058 return request.count;
1061 int drmMarkBufs(int fd, double low, double high)
1063 drm_buf_info_t info;
1069 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1075 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1078 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1079 int retval = -errno;
1084 for (i = 0; i < info.count; i++) {
1085 info.list[i].low_mark = low * info.list[i].count;
1086 info.list[i].high_mark = high * info.list[i].count;
1087 if (drmIoctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
1088 int retval = -errno;
1101 * \param fd file descriptor.
1102 * \param count number of buffers to free.
1103 * \param list list of buffers to be freed.
1105 * \return zero on success, or a negative value on failure.
1107 * \note This function is primarily used for debugging.
1110 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
1111 * the arguments in a drm_buf_free structure.
1113 int drmFreeBufs(int fd, int count, int *list)
1115 drm_buf_free_t request;
1117 request.count = count;
1118 request.list = list;
1119 if (drmIoctl(fd, DRM_IOCTL_FREE_BUFS, &request))
1128 * \param fd file descriptor.
1131 * This function closes the file descriptor.
1133 int drmClose(int fd)
1135 unsigned long key = drmGetKeyFromFd(fd);
1136 drmHashEntry *entry = drmGetEntry(fd);
1138 drmHashDestroy(entry->tagTable);
1141 entry->tagTable = NULL;
1143 drmHashDelete(drmHashTable, key);
1151 * Map a region of memory.
1153 * \param fd file descriptor.
1154 * \param handle handle returned by drmAddMap().
1155 * \param size size in bytes. Must match the size used by drmAddMap().
1156 * \param address will contain the user-space virtual address where the mapping
1159 * \return zero on success, or a negative value on failure.
1162 * This function is a wrapper for mmap().
1164 int drmMap(int fd, drm_handle_t handle, drmSize size, drmAddressPtr address)
1166 static unsigned long pagesize_mask = 0;
1172 pagesize_mask = getpagesize() - 1;
1174 size = (size + pagesize_mask) & ~pagesize_mask;
1176 *address = drm_mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
1177 if (*address == MAP_FAILED)
1184 * Unmap mappings obtained with drmMap().
1186 * \param address address as given by drmMap().
1187 * \param size size in bytes. Must match the size used by drmMap().
1189 * \return zero on success, or a negative value on failure.
1192 * This function is a wrapper for munmap().
1194 int drmUnmap(drmAddress address, drmSize size)
1196 return drm_munmap(address, size);
1199 drmBufInfoPtr drmGetBufInfo(int fd)
1201 drm_buf_info_t info;
1202 drmBufInfoPtr retval;
1208 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1212 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1215 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1220 retval = drmMalloc(sizeof(*retval));
1221 retval->count = info.count;
1222 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1223 for (i = 0; i < info.count; i++) {
1224 retval->list[i].count = info.list[i].count;
1225 retval->list[i].size = info.list[i].size;
1226 retval->list[i].low_mark = info.list[i].low_mark;
1227 retval->list[i].high_mark = info.list[i].high_mark;
1236 * Map all DMA buffers into client-virtual space.
1238 * \param fd file descriptor.
1240 * \return a pointer to a ::drmBufMap structure.
1242 * \note The client may not use these buffers until obtaining buffer indices
1246 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1247 * information about the buffers in a drm_buf_map structure into the
1248 * client-visible data structures.
1250 drmBufMapPtr drmMapBufs(int fd)
1253 drmBufMapPtr retval;
1258 bufs.virtual = NULL;
1259 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
1265 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1268 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
1273 retval = drmMalloc(sizeof(*retval));
1274 retval->count = bufs.count;
1275 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1276 for (i = 0; i < bufs.count; i++) {
1277 retval->list[i].idx = bufs.list[i].idx;
1278 retval->list[i].total = bufs.list[i].total;
1279 retval->list[i].used = 0;
1280 retval->list[i].address = bufs.list[i].address;
1290 * Unmap buffers allocated with drmMapBufs().
1292 * \return zero on success, or negative value on failure.
1295 * Calls munmap() for every buffer stored in \p bufs and frees the
1296 * memory allocated by drmMapBufs().
1298 int drmUnmapBufs(drmBufMapPtr bufs)
1302 for (i = 0; i < bufs->count; i++) {
1303 drm_munmap(bufs->list[i].address, bufs->list[i].total);
1306 drmFree(bufs->list);
1313 #define DRM_DMA_RETRY 16
1316 * Reserve DMA buffers.
1318 * \param fd file descriptor.
1321 * \return zero on success, or a negative value on failure.
1324 * Assemble the arguments into a drm_dma structure and keeps issuing the
1325 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1327 int drmDMA(int fd, drmDMAReqPtr request)
1332 dma.context = request->context;
1333 dma.send_count = request->send_count;
1334 dma.send_indices = request->send_list;
1335 dma.send_sizes = request->send_sizes;
1336 dma.flags = request->flags;
1337 dma.request_count = request->request_count;
1338 dma.request_size = request->request_size;
1339 dma.request_indices = request->request_list;
1340 dma.request_sizes = request->request_sizes;
1341 dma.granted_count = 0;
1344 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1345 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1348 request->granted_count = dma.granted_count;
1357 * Obtain heavyweight hardware lock.
1359 * \param fd file descriptor.
1360 * \param context context.
1361 * \param flags flags that determine the sate of the hardware when the function
1364 * \return always zero.
1367 * This function translates the arguments into a drm_lock structure and issue
1368 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1370 int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
1374 lock.context = context;
1376 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1377 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1378 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1379 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1380 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1381 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1383 while (drmIoctl(fd, DRM_IOCTL_LOCK, &lock))
1389 * Release the hardware lock.
1391 * \param fd file descriptor.
1392 * \param context context.
1394 * \return zero on success, or a negative value on failure.
1397 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1398 * argument in a drm_lock structure.
1400 int drmUnlock(int fd, drm_context_t context)
1404 lock.context = context;
1406 return drmIoctl(fd, DRM_IOCTL_UNLOCK, &lock);
1409 drm_context_t *drmGetReservedContextList(int fd, int *count)
1413 drm_context_t * retval;
1417 res.contexts = NULL;
1418 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1424 if (!(list = drmMalloc(res.count * sizeof(*list))))
1426 if (!(retval = drmMalloc(res.count * sizeof(*retval)))) {
1431 res.contexts = list;
1432 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1435 for (i = 0; i < res.count; i++)
1436 retval[i] = list[i].handle;
1443 void drmFreeReservedContextList(drm_context_t *pt)
1451 * Used by the X server during GLXContext initialization. This causes
1452 * per-context kernel-level resources to be allocated.
1454 * \param fd file descriptor.
1455 * \param handle is set on success. To be used by the client when requesting DMA
1456 * dispatch with drmDMA().
1458 * \return zero on success, or a negative value on failure.
1460 * \note May only be called by root.
1463 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1464 * argument in a drm_ctx structure.
1466 int drmCreateContext(int fd, drm_context_t *handle)
1470 ctx.flags = 0; /* Modified with functions below */
1471 if (drmIoctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
1473 *handle = ctx.handle;
1477 int drmSwitchToContext(int fd, drm_context_t context)
1481 ctx.handle = context;
1482 if (drmIoctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
1487 int drmSetContextFlags(int fd, drm_context_t context, drm_context_tFlags flags)
1492 * Context preserving means that no context switches are done between DMA
1493 * buffers from one context and the next. This is suitable for use in the
1494 * X server (which promises to maintain hardware context), or in the
1495 * client-side library when buffers are swapped on behalf of two threads.
1497 ctx.handle = context;
1499 if (flags & DRM_CONTEXT_PRESERVED)
1500 ctx.flags |= _DRM_CONTEXT_PRESERVED;
1501 if (flags & DRM_CONTEXT_2DONLY)
1502 ctx.flags |= _DRM_CONTEXT_2DONLY;
1503 if (drmIoctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
1508 int drmGetContextFlags(int fd, drm_context_t context,
1509 drm_context_tFlagsPtr flags)
1513 ctx.handle = context;
1514 if (drmIoctl(fd, DRM_IOCTL_GET_CTX, &ctx))
1517 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
1518 *flags |= DRM_CONTEXT_PRESERVED;
1519 if (ctx.flags & _DRM_CONTEXT_2DONLY)
1520 *flags |= DRM_CONTEXT_2DONLY;
1527 * Free any kernel-level resources allocated with drmCreateContext() associated
1530 * \param fd file descriptor.
1531 * \param handle handle given by drmCreateContext().
1533 * \return zero on success, or a negative value on failure.
1535 * \note May only be called by root.
1538 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1539 * argument in a drm_ctx structure.
1541 int drmDestroyContext(int fd, drm_context_t handle)
1544 ctx.handle = handle;
1545 if (drmIoctl(fd, DRM_IOCTL_RM_CTX, &ctx))
1550 int drmCreateDrawable(int fd, drm_drawable_t *handle)
1553 if (drmIoctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
1555 *handle = draw.handle;
1559 int drmDestroyDrawable(int fd, drm_drawable_t handle)
1562 draw.handle = handle;
1563 if (drmIoctl(fd, DRM_IOCTL_RM_DRAW, &draw))
1568 int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
1569 drm_drawable_info_type_t type, unsigned int num,
1572 drm_update_draw_t update;
1574 update.handle = handle;
1577 update.data = (unsigned long long)(unsigned long)data;
1579 if (drmIoctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
1586 * Acquire the AGP device.
1588 * Must be called before any of the other AGP related calls.
1590 * \param fd file descriptor.
1592 * \return zero on success, or a negative value on failure.
1595 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1597 int drmAgpAcquire(int fd)
1599 if (drmIoctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
1606 * Release the AGP device.
1608 * \param fd file descriptor.
1610 * \return zero on success, or a negative value on failure.
1613 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1615 int drmAgpRelease(int fd)
1617 if (drmIoctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
1626 * \param fd file descriptor.
1627 * \param mode AGP mode.
1629 * \return zero on success, or a negative value on failure.
1632 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1633 * argument in a drm_agp_mode structure.
1635 int drmAgpEnable(int fd, unsigned long mode)
1640 if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
1647 * Allocate a chunk of AGP memory.
1649 * \param fd file descriptor.
1650 * \param size requested memory size in bytes. Will be rounded to page boundary.
1651 * \param type type of memory to allocate.
1652 * \param address if not zero, will be set to the physical address of the
1654 * \param handle on success will be set to a handle of the allocated memory.
1656 * \return zero on success, or a negative value on failure.
1659 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1660 * arguments in a drm_agp_buffer structure.
1662 int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
1663 unsigned long *address, drm_handle_t *handle)
1667 *handle = DRM_AGP_NO_HANDLE;
1671 if (drmIoctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
1674 *address = b.physical;
1681 * Free a chunk of AGP memory.
1683 * \param fd file descriptor.
1684 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1686 * \return zero on success, or a negative value on failure.
1689 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1690 * argument in a drm_agp_buffer structure.
1692 int drmAgpFree(int fd, drm_handle_t handle)
1698 if (drmIoctl(fd, DRM_IOCTL_AGP_FREE, &b))
1705 * Bind a chunk of AGP memory.
1707 * \param fd file descriptor.
1708 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1709 * \param offset offset in bytes. It will round to page boundary.
1711 * \return zero on success, or a negative value on failure.
1714 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1715 * argument in a drm_agp_binding structure.
1717 int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
1719 drm_agp_binding_t b;
1723 if (drmIoctl(fd, DRM_IOCTL_AGP_BIND, &b))
1730 * Unbind a chunk of AGP memory.
1732 * \param fd file descriptor.
1733 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1735 * \return zero on success, or a negative value on failure.
1738 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1739 * the argument in a drm_agp_binding structure.
1741 int drmAgpUnbind(int fd, drm_handle_t handle)
1743 drm_agp_binding_t b;
1747 if (drmIoctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
1754 * Get AGP driver major version number.
1756 * \param fd file descriptor.
1758 * \return major version number on success, or a negative value on failure..
1761 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1762 * necessary information in a drm_agp_info structure.
1764 int drmAgpVersionMajor(int fd)
1768 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1770 return i.agp_version_major;
1775 * Get AGP driver minor version number.
1777 * \param fd file descriptor.
1779 * \return minor version number on success, or a negative value on failure.
1782 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1783 * necessary information in a drm_agp_info structure.
1785 int drmAgpVersionMinor(int fd)
1789 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1791 return i.agp_version_minor;
1798 * \param fd file descriptor.
1800 * \return mode on success, or zero on failure.
1803 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1804 * necessary information in a drm_agp_info structure.
1806 unsigned long drmAgpGetMode(int fd)
1810 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1817 * Get AGP aperture base.
1819 * \param fd file descriptor.
1821 * \return aperture base on success, zero on failure.
1824 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1825 * necessary information in a drm_agp_info structure.
1827 unsigned long drmAgpBase(int fd)
1831 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1833 return i.aperture_base;
1838 * Get AGP aperture size.
1840 * \param fd file descriptor.
1842 * \return aperture size on success, zero on failure.
1845 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1846 * necessary information in a drm_agp_info structure.
1848 unsigned long drmAgpSize(int fd)
1852 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1854 return i.aperture_size;
1859 * Get used AGP memory.
1861 * \param fd file descriptor.
1863 * \return memory used on success, or zero on failure.
1866 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1867 * necessary information in a drm_agp_info structure.
1869 unsigned long drmAgpMemoryUsed(int fd)
1873 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1875 return i.memory_used;
1880 * Get available AGP memory.
1882 * \param fd file descriptor.
1884 * \return memory available on success, or zero on failure.
1887 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1888 * necessary information in a drm_agp_info structure.
1890 unsigned long drmAgpMemoryAvail(int fd)
1894 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1896 return i.memory_allowed;
1901 * Get hardware vendor ID.
1903 * \param fd file descriptor.
1905 * \return vendor ID on success, or zero on failure.
1908 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1909 * necessary information in a drm_agp_info structure.
1911 unsigned int drmAgpVendorId(int fd)
1915 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1922 * Get hardware device ID.
1924 * \param fd file descriptor.
1926 * \return zero on success, or zero on failure.
1929 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1930 * necessary information in a drm_agp_info structure.
1932 unsigned int drmAgpDeviceId(int fd)
1936 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1941 int drmScatterGatherAlloc(int fd, unsigned long size, drm_handle_t *handle)
1943 drm_scatter_gather_t sg;
1948 if (drmIoctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
1950 *handle = sg.handle;
1954 int drmScatterGatherFree(int fd, drm_handle_t handle)
1956 drm_scatter_gather_t sg;
1960 if (drmIoctl(fd, DRM_IOCTL_SG_FREE, &sg))
1968 * \param fd file descriptor.
1969 * \param vbl pointer to a drmVBlank structure.
1971 * \return zero on success, or a negative value on failure.
1974 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
1976 int drmWaitVBlank(int fd, drmVBlankPtr vbl)
1978 struct timespec timeout, cur;
1981 ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
1983 fprintf(stderr, "clock_gettime failed: %s\n", strerror(errno));
1989 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
1990 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
1991 if (ret && errno == EINTR) {
1992 clock_gettime(CLOCK_MONOTONIC, &cur);
1993 /* Timeout after 1s */
1994 if (cur.tv_sec > timeout.tv_sec + 1 ||
1995 (cur.tv_sec == timeout.tv_sec && cur.tv_nsec >=
2002 } while (ret && errno == EINTR);
2008 int drmError(int err, const char *label)
2011 case DRM_ERR_NO_DEVICE:
2012 fprintf(stderr, "%s: no device\n", label);
2014 case DRM_ERR_NO_ACCESS:
2015 fprintf(stderr, "%s: no access\n", label);
2017 case DRM_ERR_NOT_ROOT:
2018 fprintf(stderr, "%s: not root\n", label);
2020 case DRM_ERR_INVALID:
2021 fprintf(stderr, "%s: invalid args\n", label);
2026 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
2034 * Install IRQ handler.
2036 * \param fd file descriptor.
2037 * \param irq IRQ number.
2039 * \return zero on success, or a negative value on failure.
2042 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2043 * argument in a drm_control structure.
2045 int drmCtlInstHandler(int fd, int irq)
2049 ctl.func = DRM_INST_HANDLER;
2051 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2058 * Uninstall IRQ handler.
2060 * \param fd file descriptor.
2062 * \return zero on success, or a negative value on failure.
2065 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2066 * argument in a drm_control structure.
2068 int drmCtlUninstHandler(int fd)
2072 ctl.func = DRM_UNINST_HANDLER;
2074 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2079 int drmFinish(int fd, int context, drmLockFlags flags)
2083 lock.context = context;
2085 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
2086 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
2087 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
2088 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
2089 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
2090 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
2091 if (drmIoctl(fd, DRM_IOCTL_FINISH, &lock))
2097 * Get IRQ from bus ID.
2099 * \param fd file descriptor.
2100 * \param busnum bus number.
2101 * \param devnum device number.
2102 * \param funcnum function number.
2104 * \return IRQ number on success, or a negative value on failure.
2107 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
2108 * arguments in a drm_irq_busid structure.
2110 int drmGetInterruptFromBusID(int fd, int busnum, int devnum, int funcnum)
2116 p.funcnum = funcnum;
2117 if (drmIoctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
2122 int drmAddContextTag(int fd, drm_context_t context, void *tag)
2124 drmHashEntry *entry = drmGetEntry(fd);
2126 if (drmHashInsert(entry->tagTable, context, tag)) {
2127 drmHashDelete(entry->tagTable, context);
2128 drmHashInsert(entry->tagTable, context, tag);
2133 int drmDelContextTag(int fd, drm_context_t context)
2135 drmHashEntry *entry = drmGetEntry(fd);
2137 return drmHashDelete(entry->tagTable, context);
2140 void *drmGetContextTag(int fd, drm_context_t context)
2142 drmHashEntry *entry = drmGetEntry(fd);
2145 if (drmHashLookup(entry->tagTable, context, &value))
2151 int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2152 drm_handle_t handle)
2154 drm_ctx_priv_map_t map;
2156 map.ctx_id = ctx_id;
2157 map.handle = (void *)(uintptr_t)handle;
2159 if (drmIoctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
2164 int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2165 drm_handle_t *handle)
2167 drm_ctx_priv_map_t map;
2169 map.ctx_id = ctx_id;
2171 if (drmIoctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
2174 *handle = (drm_handle_t)(uintptr_t)map.handle;
2179 int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
2180 drmMapType *type, drmMapFlags *flags, drm_handle_t *handle,
2186 if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map))
2188 *offset = map.offset;
2192 *handle = (unsigned long)map.handle;
2197 int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
2198 unsigned long *magic, unsigned long *iocs)
2200 drm_client_t client;
2203 if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client))
2205 *auth = client.auth;
2208 *magic = client.magic;
2209 *iocs = client.iocs;
2213 int drmGetStats(int fd, drmStatsT *stats)
2218 if (drmIoctl(fd, DRM_IOCTL_GET_STATS, &s))
2222 memset(stats, 0, sizeof(*stats));
2223 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
2227 stats->data[i].long_format = "%-20.20s"; \
2228 stats->data[i].rate_format = "%8.8s"; \
2229 stats->data[i].isvalue = 1; \
2230 stats->data[i].verbose = 0
2233 stats->data[i].long_format = "%-20.20s"; \
2234 stats->data[i].rate_format = "%5.5s"; \
2235 stats->data[i].isvalue = 0; \
2236 stats->data[i].mult_names = "kgm"; \
2237 stats->data[i].mult = 1000; \
2238 stats->data[i].verbose = 0
2241 stats->data[i].long_format = "%-20.20s"; \
2242 stats->data[i].rate_format = "%5.5s"; \
2243 stats->data[i].isvalue = 0; \
2244 stats->data[i].mult_names = "KGM"; \
2245 stats->data[i].mult = 1024; \
2246 stats->data[i].verbose = 0
2249 stats->count = s.count;
2250 for (i = 0; i < s.count; i++) {
2251 stats->data[i].value = s.data[i].value;
2252 switch (s.data[i].type) {
2253 case _DRM_STAT_LOCK:
2254 stats->data[i].long_name = "Lock";
2255 stats->data[i].rate_name = "Lock";
2258 case _DRM_STAT_OPENS:
2259 stats->data[i].long_name = "Opens";
2260 stats->data[i].rate_name = "O";
2262 stats->data[i].verbose = 1;
2264 case _DRM_STAT_CLOSES:
2265 stats->data[i].long_name = "Closes";
2266 stats->data[i].rate_name = "Lock";
2268 stats->data[i].verbose = 1;
2270 case _DRM_STAT_IOCTLS:
2271 stats->data[i].long_name = "Ioctls";
2272 stats->data[i].rate_name = "Ioc/s";
2275 case _DRM_STAT_LOCKS:
2276 stats->data[i].long_name = "Locks";
2277 stats->data[i].rate_name = "Lck/s";
2280 case _DRM_STAT_UNLOCKS:
2281 stats->data[i].long_name = "Unlocks";
2282 stats->data[i].rate_name = "Unl/s";
2286 stats->data[i].long_name = "IRQs";
2287 stats->data[i].rate_name = "IRQ/s";
2290 case _DRM_STAT_PRIMARY:
2291 stats->data[i].long_name = "Primary Bytes";
2292 stats->data[i].rate_name = "PB/s";
2295 case _DRM_STAT_SECONDARY:
2296 stats->data[i].long_name = "Secondary Bytes";
2297 stats->data[i].rate_name = "SB/s";
2301 stats->data[i].long_name = "DMA";
2302 stats->data[i].rate_name = "DMA/s";
2305 case _DRM_STAT_SPECIAL:
2306 stats->data[i].long_name = "Special DMA";
2307 stats->data[i].rate_name = "dma/s";
2310 case _DRM_STAT_MISSED:
2311 stats->data[i].long_name = "Miss";
2312 stats->data[i].rate_name = "Ms/s";
2315 case _DRM_STAT_VALUE:
2316 stats->data[i].long_name = "Value";
2317 stats->data[i].rate_name = "Value";
2320 case _DRM_STAT_BYTE:
2321 stats->data[i].long_name = "Bytes";
2322 stats->data[i].rate_name = "B/s";
2325 case _DRM_STAT_COUNT:
2327 stats->data[i].long_name = "Count";
2328 stats->data[i].rate_name = "Cnt/s";
2337 * Issue a set-version ioctl.
2339 * \param fd file descriptor.
2340 * \param drmCommandIndex command index
2341 * \param data source pointer of the data to be read and written.
2342 * \param size size of the data to be read and written.
2344 * \return zero on success, or a negative value on failure.
2347 * It issues a read-write ioctl given by
2348 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2350 int drmSetInterfaceVersion(int fd, drmSetVersion *version)
2353 drm_set_version_t sv;
2355 sv.drm_di_major = version->drm_di_major;
2356 sv.drm_di_minor = version->drm_di_minor;
2357 sv.drm_dd_major = version->drm_dd_major;
2358 sv.drm_dd_minor = version->drm_dd_minor;
2360 if (drmIoctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
2364 version->drm_di_major = sv.drm_di_major;
2365 version->drm_di_minor = sv.drm_di_minor;
2366 version->drm_dd_major = sv.drm_dd_major;
2367 version->drm_dd_minor = sv.drm_dd_minor;
2373 * Send a device-specific command.
2375 * \param fd file descriptor.
2376 * \param drmCommandIndex command index
2378 * \return zero on success, or a negative value on failure.
2381 * It issues a ioctl given by
2382 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2384 int drmCommandNone(int fd, unsigned long drmCommandIndex)
2386 void *data = NULL; /* dummy */
2387 unsigned long request;
2389 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2391 if (drmIoctl(fd, request, data)) {
2399 * Send a device-specific read command.
2401 * \param fd file descriptor.
2402 * \param drmCommandIndex command index
2403 * \param data destination pointer of the data to be read.
2404 * \param size size of the data to be read.
2406 * \return zero on success, or a negative value on failure.
2409 * It issues a read ioctl given by
2410 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2412 int drmCommandRead(int fd, unsigned long drmCommandIndex, void *data,
2415 unsigned long request;
2417 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2418 DRM_COMMAND_BASE + drmCommandIndex, size);
2420 if (drmIoctl(fd, request, data)) {
2428 * Send a device-specific write command.
2430 * \param fd file descriptor.
2431 * \param drmCommandIndex command index
2432 * \param data source pointer of the data to be written.
2433 * \param size size of the data to be written.
2435 * \return zero on success, or a negative value on failure.
2438 * It issues a write ioctl given by
2439 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2441 int drmCommandWrite(int fd, unsigned long drmCommandIndex, void *data,
2444 unsigned long request;
2446 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2447 DRM_COMMAND_BASE + drmCommandIndex, size);
2449 if (drmIoctl(fd, request, data)) {
2457 * Send a device-specific read-write command.
2459 * \param fd file descriptor.
2460 * \param drmCommandIndex command index
2461 * \param data source pointer of the data to be read and written.
2462 * \param size size of the data to be read and written.
2464 * \return zero on success, or a negative value on failure.
2467 * It issues a read-write ioctl given by
2468 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2470 int drmCommandWriteRead(int fd, unsigned long drmCommandIndex, void *data,
2473 unsigned long request;
2475 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2476 DRM_COMMAND_BASE + drmCommandIndex, size);
2478 if (drmIoctl(fd, request, data))
2483 #define DRM_MAX_FDS 16
2488 } connection[DRM_MAX_FDS];
2490 static int nr_fds = 0;
2492 int drmOpenOnce(void *unused,
2499 for (i = 0; i < nr_fds; i++)
2500 if (strcmp(BusID, connection[i].BusID) == 0) {
2501 connection[i].refcount++;
2503 return connection[i].fd;
2506 fd = drmOpen(unused, BusID);
2507 if (fd <= 0 || nr_fds == DRM_MAX_FDS)
2510 connection[nr_fds].BusID = strdup(BusID);
2511 connection[nr_fds].fd = fd;
2512 connection[nr_fds].refcount = 1;
2516 fprintf(stderr, "saved connection %d for %s %d\n",
2517 nr_fds, connection[nr_fds].BusID,
2518 strcmp(BusID, connection[nr_fds].BusID));
2525 void drmCloseOnce(int fd)
2529 for (i = 0; i < nr_fds; i++) {
2530 if (fd == connection[i].fd) {
2531 if (--connection[i].refcount == 0) {
2532 drmClose(connection[i].fd);
2533 free(connection[i].BusID);
2536 connection[i] = connection[nr_fds];
2544 int drmSetMaster(int fd)
2546 return ioctl(fd, DRM_IOCTL_SET_MASTER, 0);
2549 int drmDropMaster(int fd)
2551 return ioctl(fd, DRM_IOCTL_DROP_MASTER, 0);
2554 char *drmGetDeviceNameFromFd(int fd)
2561 /* The whole drmOpen thing is a fiasco and we need to find a way
2562 * back to just using open(2). For now, however, lets just make
2563 * things worse with even more ad hoc directory walking code to
2564 * discover the device file name. */
2569 for (i = 0; i < DRM_MAX_MINOR; i++) {
2570 snprintf(name, sizeof name, DRM_DEV_NAME, DRM_DIR_NAME, i);
2571 if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d)
2574 if (i == DRM_MAX_MINOR)
2577 return strdup(name);
2580 int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd)
2582 struct drm_prime_handle args;
2585 args.handle = handle;
2587 ret = drmIoctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
2591 *prime_fd = args.fd;
2595 int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
2597 struct drm_prime_handle args;
2602 ret = drmIoctl(fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);
2606 *handle = args.handle;