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.
45 #include <sys/types.h>
47 #define stat_t struct stat
48 #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__)
74 #define DRM_MAJOR 226 /* Linux */
78 #define DRM_MAX_MINOR 16
82 * This definition needs to be changed on some systems if dev_t is a structure.
83 * If there is a header file we can get it from, there would be best.
86 #define makedev(x,y) ((dev_t)(((x) << 8) | (y)))
89 #define DRM_MSG_VERBOSITY 3
91 static drmServerInfoPtr drm_server_info;
93 void drmSetServerInfo(drmServerInfoPtr info)
95 drm_server_info = info;
99 * Output a message to stderr.
101 * \param format printf() like format string.
104 * This function is a wrapper around vfprintf().
107 static int drmDebugPrint(const char *format, va_list ap)
109 return vfprintf(stderr, format, ap);
112 static int (*drm_debug_print)(const char *format, va_list ap) = drmDebugPrint;
115 drmMsg(const char *format, ...)
119 if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) || drm_server_info)
121 va_start(ap, format);
122 if (drm_server_info) {
123 drm_server_info->debug_print(format,ap);
125 drm_debug_print(format, ap);
132 drmSetDebugMsgFunction(int (*debug_msg_ptr)(const char *format, va_list ap))
134 drm_debug_print = debug_msg_ptr;
137 static void *drmHashTable = NULL; /* Context switch callbacks */
139 void *drmGetHashTable(void)
144 void *drmMalloc(int size)
147 if ((pt = malloc(size))) memset(pt, 0, size);
151 void drmFree(void *pt)
156 /* drmStrdup can't use strdup(3), since it doesn't call _DRM_MALLOC... */
157 static char *drmStrdup(const char *s)
164 retval = malloc(strlen(s)+1);
174 static unsigned long drmGetKeyFromFd(int fd)
183 drmHashEntry *drmGetEntry(int fd)
185 unsigned long key = drmGetKeyFromFd(fd);
189 if (!drmHashTable) drmHashTable = drmHashCreate();
191 if (drmHashLookup(drmHashTable, key, &value)) {
192 entry = drmMalloc(sizeof(*entry));
195 entry->tagTable = drmHashCreate();
196 drmHashInsert(drmHashTable, key, entry);
204 * Compare two busid strings
209 * \return 1 if matched.
212 * This function compares two bus ID strings. It understands the older
213 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
214 * domain, b is bus, d is device, f is function.
216 static int drmMatchBusID(const char *id1, const char *id2)
218 /* First, check if the IDs are exactly the same */
219 if (strcasecmp(id1, id2) == 0)
222 /* Try to match old/new-style PCI bus IDs. */
223 if (strncasecmp(id1, "pci", 3) == 0) {
228 ret = sscanf(id1, "pci:%04x:%02x:%02x.%d", &o1, &b1, &d1, &f1);
231 ret = sscanf(id1, "PCI:%d:%d:%d", &b1, &d1, &f1);
236 ret = sscanf(id2, "pci:%04x:%02x:%02x.%d", &o2, &b2, &d2, &f2);
239 ret = sscanf(id2, "PCI:%d:%d:%d", &b2, &d2, &f2);
244 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
253 * Open the DRM device, creating it if necessary.
255 * \param dev major and minor numbers of the device.
256 * \param minor minor number of the device.
258 * \return a file descriptor on success, or a negative value on error.
261 * Assembles the device name from \p minor and opens it, creating the device
262 * special file node with the major and minor numbers specified by \p dev and
263 * parent directory if necessary and was called by root.
265 static int drmOpenDevice(long dev, int minor)
270 mode_t devmode = DRM_DEV_MODE, serv_mode;
271 int isroot = !geteuid();
272 uid_t user = DRM_DEV_UID;
273 gid_t group = DRM_DEV_GID, serv_group;
275 sprintf(buf, DRM_DEV_NAME, DRM_DIR_NAME, minor);
276 drmMsg("drmOpenDevice: node name is %s\n", buf);
278 if (drm_server_info) {
279 drm_server_info->get_perms(&serv_group, &serv_mode);
280 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
281 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
282 group = (serv_group >= 0) ? serv_group : DRM_DEV_GID;
285 if (stat(DRM_DIR_NAME, &st)) {
286 if (!isroot) return DRM_ERR_NOT_ROOT;
287 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
288 chown(DRM_DIR_NAME, 0, 0); /* root:root */
289 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
292 /* Check if the device node exists and create it if necessary. */
293 if (stat(buf, &st)) {
294 if (!isroot) return DRM_ERR_NOT_ROOT;
296 mknod(buf, S_IFCHR | devmode, dev);
299 if (drm_server_info) {
300 chown(buf, user, group);
304 fd = open(buf, O_RDWR, 0);
305 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
306 fd, fd < 0 ? strerror(errno) : "OK");
307 if (fd >= 0) return fd;
309 /* Check if the device node is not what we expect it to be, and recreate it
310 * and try again if so.
312 if (st.st_rdev != dev) {
313 if (!isroot) return DRM_ERR_NOT_ROOT;
315 mknod(buf, S_IFCHR | devmode, dev);
316 if (drm_server_info) {
317 chown(buf, user, group);
321 fd = open(buf, O_RDWR, 0);
322 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
323 fd, fd < 0 ? strerror(errno) : "OK");
324 if (fd >= 0) return fd;
326 drmMsg("drmOpenDevice: Open failed\n");
333 * Open the DRM device
335 * \param minor device minor number.
336 * \param create allow to create the device if set.
338 * \return a file descriptor on success, or a negative value on error.
341 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
342 * name from \p minor and opens it.
344 static int drmOpenMinor(int minor, int create)
349 if (create) return drmOpenDevice(makedev(DRM_MAJOR, minor), minor);
351 sprintf(buf, DRM_DEV_NAME, DRM_DIR_NAME, minor);
352 if ((fd = open(buf, O_RDWR, 0)) >= 0) return fd;
358 * Determine whether the DRM kernel driver has been loaded.
360 * \return 1 if the DRM driver is loaded, 0 otherwise.
363 * Determine the presence of the kernel driver by attempting to open the 0
364 * minor and get version information. For backward compatibility with older
365 * Linux implementations, /proc/dri is also checked.
367 int drmAvailable(void)
369 drmVersionPtr version;
373 if ((fd = drmOpenMinor(0, 1)) < 0) {
375 /* Try proc for backward Linux compatibility */
376 if (!access("/proc/dri/0", R_OK)) return 1;
381 if ((version = drmGetVersion(fd))) {
383 drmFreeVersion(version);
392 * Open the device by bus ID.
394 * \param busid bus ID.
396 * \return a file descriptor on success, or a negative value on error.
399 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
400 * comparing the device bus ID with the one supplied.
402 * \sa drmOpenMinor() and drmGetBusid().
404 static int drmOpenByBusid(const char *busid)
411 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
412 for (i = 0; i < DRM_MAX_MINOR; i++) {
413 fd = drmOpenMinor(i, 1);
414 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
418 sv.drm_dd_major = -1; /* Don't care */
419 sv.drm_dd_minor = -1; /* Don't care */
420 drmSetInterfaceVersion(fd, &sv);
421 buf = drmGetBusid(fd);
422 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
423 if (buf && drmMatchBusID(buf, busid)) {
427 if (buf) drmFreeBusid(buf);
436 * Open the device by name.
438 * \param name driver name.
440 * \return a file descriptor on success, or a negative value on error.
443 * This function opens the first minor number that matches the driver name and
444 * isn't already in use. If it's in use it then it will already have a bus ID
447 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
449 static int drmOpenByName(const char *name)
453 drmVersionPtr version;
456 if (!drmAvailable()) {
457 if (!drm_server_info)
460 /* try to load the kernel module now */
461 if (!drm_server_info->load_module(name)) {
462 drmMsg("[drm] failed to load kernel module \"%s\"\n",
470 * Open the first minor number that matches the driver name and isn't
471 * already in use. If it's in use it will have a busid assigned already.
473 for (i = 0; i < DRM_MAX_MINOR; i++) {
474 if ((fd = drmOpenMinor(i, 1)) >= 0) {
475 if ((version = drmGetVersion(fd))) {
476 if (!strcmp(version->name, name)) {
477 drmFreeVersion(version);
478 id = drmGetBusid(fd);
479 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
488 drmFreeVersion(version);
496 /* Backward-compatibility /proc support */
497 for (i = 0; i < 8; i++) {
498 char proc_name[64], buf[512];
499 char *driver, *pt, *devstring;
502 sprintf(proc_name, "/proc/dri/%d/name", i);
503 if ((fd = open(proc_name, 0, 0)) >= 0) {
504 retcode = read(fd, buf, sizeof(buf)-1);
507 buf[retcode-1] = '\0';
508 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
510 if (*pt) { /* Device is next */
512 if (!strcmp(driver, name)) { /* Match */
513 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
515 if (*pt) { /* Found busid */
516 return drmOpenByBusid(++pt);
517 } else { /* No busid */
518 return drmOpenDevice(strtol(devstring, NULL, 0),i);
532 * Open the DRM device.
534 * Looks up the specified name and bus ID, and opens the device found. The
535 * entry in /dev/dri is created if necessary and if called by root.
537 * \param name driver name. Not referenced if bus ID is supplied.
538 * \param busid bus ID. Zero if not known.
540 * \return a file descriptor on success, or a negative value on error.
543 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
546 int drmOpen(const char *name, const char *busid)
548 if (!drmAvailable() && name != NULL && drm_server_info) {
549 /* try to load the kernel */
550 if (!drm_server_info->load_module(name)) {
551 drmMsg("[drm] failed to load kernel module \"%s\"\n",
560 fd = drmOpenByBusid(busid);
566 return drmOpenByName(name);
573 * Free the version information returned by drmGetVersion().
575 * \param v pointer to the version information.
578 * It frees the memory pointed by \p %v as well as all the non-null strings
581 void drmFreeVersion(drmVersionPtr v)
592 * Free the non-public version information returned by the kernel.
594 * \param v pointer to the version information.
597 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
598 * the non-null strings pointers in it.
600 static void drmFreeKernelVersion(drm_version_t *v)
611 * Copy version information.
613 * \param d destination pointer.
614 * \param s source pointer.
617 * Used by drmGetVersion() to translate the information returned by the ioctl
618 * interface in a private structure into the public structure counterpart.
620 static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
622 d->version_major = s->version_major;
623 d->version_minor = s->version_minor;
624 d->version_patchlevel = s->version_patchlevel;
625 d->name_len = s->name_len;
626 d->name = drmStrdup(s->name);
627 d->date_len = s->date_len;
628 d->date = drmStrdup(s->date);
629 d->desc_len = s->desc_len;
630 d->desc = drmStrdup(s->desc);
635 * Query the driver version information.
637 * \param fd file descriptor.
639 * \return pointer to a drmVersion structure which should be freed with
642 * \note Similar information is available via /proc/dri.
645 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
646 * first with zeros to get the string lengths, and then the actually strings.
647 * It also null-terminates them since they might not be already.
649 drmVersionPtr drmGetVersion(int fd)
651 drmVersionPtr retval;
652 drm_version_t *version = drmMalloc(sizeof(*version));
654 version->name_len = 0;
655 version->name = NULL;
656 version->date_len = 0;
657 version->date = NULL;
658 version->desc_len = 0;
659 version->desc = NULL;
661 if (ioctl(fd, DRM_IOCTL_VERSION, version)) {
662 drmFreeKernelVersion(version);
666 if (version->name_len)
667 version->name = drmMalloc(version->name_len + 1);
668 if (version->date_len)
669 version->date = drmMalloc(version->date_len + 1);
670 if (version->desc_len)
671 version->desc = drmMalloc(version->desc_len + 1);
673 if (ioctl(fd, DRM_IOCTL_VERSION, version)) {
674 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
675 drmFreeKernelVersion(version);
679 /* The results might not be null-terminated strings, so terminate them. */
680 if (version->name_len) version->name[version->name_len] = '\0';
681 if (version->date_len) version->date[version->date_len] = '\0';
682 if (version->desc_len) version->desc[version->desc_len] = '\0';
684 retval = drmMalloc(sizeof(*retval));
685 drmCopyVersion(retval, version);
686 drmFreeKernelVersion(version);
692 * Get version information for the DRM user space library.
694 * This version number is driver independent.
696 * \param fd file descriptor.
698 * \return version information.
701 * This function allocates and fills a drm_version structure with a hard coded
704 drmVersionPtr drmGetLibVersion(int fd)
706 drm_version_t *version = drmMalloc(sizeof(*version));
709 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
710 * revision 1.0.x = original DRM interface with no drmGetLibVersion
711 * entry point and many drm<Device> extensions
712 * revision 1.1.x = added drmCommand entry points for device extensions
713 * added drmGetLibVersion to identify libdrm.a version
714 * revision 1.2.x = added drmSetInterfaceVersion
715 * modified drmOpen to handle both busid and name
716 * revision 1.3.x = added server + memory manager
718 version->version_major = 1;
719 version->version_minor = 3;
720 version->version_patchlevel = 0;
722 return (drmVersionPtr)version;
727 * Free the bus ID information.
729 * \param busid bus ID information string as given by drmGetBusid().
732 * This function is just frees the memory pointed by \p busid.
734 void drmFreeBusid(const char *busid)
736 drmFree((void *)busid);
741 * Get the bus ID of the device.
743 * \param fd file descriptor.
745 * \return bus ID string.
748 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
749 * get the string length and data, passing the arguments in a drm_unique
752 char *drmGetBusid(int fd)
759 if (ioctl(fd, DRM_IOCTL_GET_UNIQUE, &u)) return NULL;
760 u.unique = drmMalloc(u.unique_len + 1);
761 if (ioctl(fd, DRM_IOCTL_GET_UNIQUE, &u)) return NULL;
762 u.unique[u.unique_len] = '\0';
769 * Set the bus ID of the device.
771 * \param fd file descriptor.
772 * \param busid bus ID string.
774 * \return zero on success, negative on failure.
777 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
778 * the arguments in a drm_unique structure.
780 int drmSetBusid(int fd, const char *busid)
784 u.unique = (char *)busid;
785 u.unique_len = strlen(busid);
787 if (ioctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
793 int drmGetMagic(int fd, drm_magic_t * magic)
798 if (ioctl(fd, DRM_IOCTL_GET_MAGIC, &auth)) return -errno;
803 int drmAuthMagic(int fd, drm_magic_t magic)
808 if (ioctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth)) return -errno;
813 * Specifies a range of memory that is available for mapping by a
816 * \param fd file descriptor.
817 * \param offset usually the physical address. The actual meaning depends of
818 * the \p type parameter. See below.
819 * \param size of the memory in bytes.
820 * \param type type of the memory to be mapped.
821 * \param flags combination of several flags to modify the function actions.
822 * \param handle will be set to a value that may be used as the offset
823 * parameter for mmap().
825 * \return zero on success or a negative value on error.
827 * \par Mapping the frame buffer
828 * For the frame buffer
829 * - \p offset will be the physical address of the start of the frame buffer,
830 * - \p size will be the size of the frame buffer in bytes, and
831 * - \p type will be DRM_FRAME_BUFFER.
834 * The area mapped will be uncached. If MTRR support is available in the
835 * kernel, the frame buffer area will be set to write combining.
837 * \par Mapping the MMIO register area
838 * For the MMIO register area,
839 * - \p offset will be the physical address of the start of the register area,
840 * - \p size will be the size of the register area bytes, and
841 * - \p type will be DRM_REGISTERS.
843 * The area mapped will be uncached.
845 * \par Mapping the SAREA
847 * - \p offset will be ignored and should be set to zero,
848 * - \p size will be the desired size of the SAREA in bytes,
849 * - \p type will be DRM_SHM.
852 * A shared memory area of the requested size will be created and locked in
853 * kernel memory. This area may be mapped into client-space by using the handle
856 * \note May only be called by root.
859 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
860 * the arguments in a drm_map structure.
862 int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
863 drmMapFlags flags, drm_handle_t *handle)
872 if (ioctl(fd, DRM_IOCTL_ADD_MAP, &map)) return -errno;
873 if (handle) *handle = (drm_handle_t)map.handle;
877 int drmRmMap(int fd, drm_handle_t handle)
881 map.handle = (void *)handle;
883 if(ioctl(fd, DRM_IOCTL_RM_MAP, &map)) return -errno;
888 * Make buffers available for DMA transfers.
890 * \param fd file descriptor.
891 * \param count number of buffers.
892 * \param size size of each buffer.
893 * \param flags buffer allocation flags.
894 * \param agp_offset offset in the AGP aperture
896 * \return number of buffers allocated, negative on error.
899 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
903 int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
906 drm_buf_desc_t request;
908 request.count = count;
910 request.low_mark = 0;
911 request.high_mark = 0;
912 request.flags = flags;
913 request.agp_start = agp_offset;
915 if (ioctl(fd, DRM_IOCTL_ADD_BUFS, &request)) return -errno;
916 return request.count;
919 int drmMarkBufs(int fd, double low, double high)
927 if (ioctl(fd, DRM_IOCTL_INFO_BUFS, &info)) return -EINVAL;
929 if (!info.count) return -EINVAL;
931 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
934 if (ioctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
940 for (i = 0; i < info.count; i++) {
941 info.list[i].low_mark = low * info.list[i].count;
942 info.list[i].high_mark = high * info.list[i].count;
943 if (ioctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
957 * \param fd file descriptor.
958 * \param count number of buffers to free.
959 * \param list list of buffers to be freed.
961 * \return zero on success, or a negative value on failure.
963 * \note This function is primarily used for debugging.
966 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
967 * the arguments in a drm_buf_free structure.
969 int drmFreeBufs(int fd, int count, int *list)
971 drm_buf_free_t request;
973 request.count = count;
975 if (ioctl(fd, DRM_IOCTL_FREE_BUFS, &request)) return -errno;
983 * \param fd file descriptor.
986 * This function closes the file descriptor.
990 unsigned long key = drmGetKeyFromFd(fd);
991 drmHashEntry *entry = drmGetEntry(fd);
993 drmHashDestroy(entry->tagTable);
996 entry->tagTable = NULL;
998 drmHashDelete(drmHashTable, key);
1006 * Map a region of memory.
1008 * \param fd file descriptor.
1009 * \param handle handle returned by drmAddMap().
1010 * \param size size in bytes. Must match the size used by drmAddMap().
1011 * \param address will contain the user-space virtual address where the mapping
1014 * \return zero on success, or a negative value on failure.
1017 * This function is a wrapper for mmap().
1019 int drmMap(int fd, drm_handle_t handle, drmSize size, drmAddressPtr address)
1021 static unsigned long pagesize_mask = 0;
1023 if (fd < 0) return -EINVAL;
1026 pagesize_mask = getpagesize() - 1;
1028 size = (size + pagesize_mask) & ~pagesize_mask;
1030 *address = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
1031 if (*address == MAP_FAILED) return -errno;
1037 * Unmap mappings obtained with drmMap().
1039 * \param address address as given by drmMap().
1040 * \param size size in bytes. Must match the size used by drmMap().
1042 * \return zero on success, or a negative value on failure.
1045 * This function is a wrapper for munmap().
1047 int drmUnmap(drmAddress address, drmSize size)
1049 return munmap(address, size);
1052 drmBufInfoPtr drmGetBufInfo(int fd)
1054 drm_buf_info_t info;
1055 drmBufInfoPtr retval;
1061 if (ioctl(fd, DRM_IOCTL_INFO_BUFS, &info)) return NULL;
1064 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1067 if (ioctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1072 retval = drmMalloc(sizeof(*retval));
1073 retval->count = info.count;
1074 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1075 for (i = 0; i < info.count; i++) {
1076 retval->list[i].count = info.list[i].count;
1077 retval->list[i].size = info.list[i].size;
1078 retval->list[i].low_mark = info.list[i].low_mark;
1079 retval->list[i].high_mark = info.list[i].high_mark;
1088 * Map all DMA buffers into client-virtual space.
1090 * \param fd file descriptor.
1092 * \return a pointer to a ::drmBufMap structure.
1094 * \note The client may not use these buffers until obtaining buffer indices
1098 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1099 * information about the buffers in a drm_buf_map structure into the
1100 * client-visible data structures.
1102 drmBufMapPtr drmMapBufs(int fd)
1105 drmBufMapPtr retval;
1110 bufs.virtual = NULL;
1111 if (ioctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) return NULL;
1113 if (!bufs.count) return NULL;
1115 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1118 if (ioctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
1123 retval = drmMalloc(sizeof(*retval));
1124 retval->count = bufs.count;
1125 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1126 for (i = 0; i < bufs.count; i++) {
1127 retval->list[i].idx = bufs.list[i].idx;
1128 retval->list[i].total = bufs.list[i].total;
1129 retval->list[i].used = 0;
1130 retval->list[i].address = bufs.list[i].address;
1140 * Unmap buffers allocated with drmMapBufs().
1142 * \return zero on success, or negative value on failure.
1145 * Calls munmap() for every buffer stored in \p bufs and frees the
1146 * memory allocated by drmMapBufs().
1148 int drmUnmapBufs(drmBufMapPtr bufs)
1152 for (i = 0; i < bufs->count; i++) {
1153 munmap(bufs->list[i].address, bufs->list[i].total);
1156 drmFree(bufs->list);
1163 #define DRM_DMA_RETRY 16
1166 * Reserve DMA buffers.
1168 * \param fd file descriptor.
1171 * \return zero on success, or a negative value on failure.
1174 * Assemble the arguments into a drm_dma structure and keeps issuing the
1175 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1177 int drmDMA(int fd, drmDMAReqPtr request)
1182 dma.context = request->context;
1183 dma.send_count = request->send_count;
1184 dma.send_indices = request->send_list;
1185 dma.send_sizes = request->send_sizes;
1186 dma.flags = request->flags;
1187 dma.request_count = request->request_count;
1188 dma.request_size = request->request_size;
1189 dma.request_indices = request->request_list;
1190 dma.request_sizes = request->request_sizes;
1191 dma.granted_count = 0;
1194 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1195 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1198 request->granted_count = dma.granted_count;
1207 * Obtain heavyweight hardware lock.
1209 * \param fd file descriptor.
1210 * \param context context.
1211 * \param flags flags that determine the sate of the hardware when the function
1214 * \return always zero.
1217 * This function translates the arguments into a drm_lock structure and issue
1218 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1220 int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
1224 lock.context = context;
1226 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1227 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1228 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1229 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1230 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1231 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1233 while (ioctl(fd, DRM_IOCTL_LOCK, &lock))
1239 * Release the hardware lock.
1241 * \param fd file descriptor.
1242 * \param context context.
1244 * \return zero on success, or a negative value on failure.
1247 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1248 * argument in a drm_lock structure.
1250 int drmUnlock(int fd, drm_context_t context)
1254 lock.context = context;
1256 return ioctl(fd, DRM_IOCTL_UNLOCK, &lock);
1259 drm_context_t *drmGetReservedContextList(int fd, int *count)
1263 drm_context_t * retval;
1267 res.contexts = NULL;
1268 if (ioctl(fd, DRM_IOCTL_RES_CTX, &res)) return NULL;
1270 if (!res.count) return NULL;
1272 if (!(list = drmMalloc(res.count * sizeof(*list)))) return NULL;
1273 if (!(retval = drmMalloc(res.count * sizeof(*retval)))) {
1278 res.contexts = list;
1279 if (ioctl(fd, DRM_IOCTL_RES_CTX, &res)) return NULL;
1281 for (i = 0; i < res.count; i++) retval[i] = list[i].handle;
1288 void drmFreeReservedContextList(drm_context_t *pt)
1296 * Used by the X server during GLXContext initialization. This causes
1297 * per-context kernel-level resources to be allocated.
1299 * \param fd file descriptor.
1300 * \param handle is set on success. To be used by the client when requesting DMA
1301 * dispatch with drmDMA().
1303 * \return zero on success, or a negative value on failure.
1305 * \note May only be called by root.
1308 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1309 * argument in a drm_ctx structure.
1311 int drmCreateContext(int fd, drm_context_t *handle)
1315 ctx.flags = 0; /* Modified with functions below */
1316 if (ioctl(fd, DRM_IOCTL_ADD_CTX, &ctx)) return -errno;
1317 *handle = ctx.handle;
1321 int drmSwitchToContext(int fd, drm_context_t context)
1325 ctx.handle = context;
1326 if (ioctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx)) return -errno;
1330 int drmSetContextFlags(int fd, drm_context_t context, drm_context_tFlags flags)
1335 * Context preserving means that no context switches are done between DMA
1336 * buffers from one context and the next. This is suitable for use in the
1337 * X server (which promises to maintain hardware context), or in the
1338 * client-side library when buffers are swapped on behalf of two threads.
1340 ctx.handle = context;
1342 if (flags & DRM_CONTEXT_PRESERVED) ctx.flags |= _DRM_CONTEXT_PRESERVED;
1343 if (flags & DRM_CONTEXT_2DONLY) ctx.flags |= _DRM_CONTEXT_2DONLY;
1344 if (ioctl(fd, DRM_IOCTL_MOD_CTX, &ctx)) return -errno;
1348 int drmGetContextFlags(int fd, drm_context_t context,
1349 drm_context_tFlagsPtr flags)
1353 ctx.handle = context;
1354 if (ioctl(fd, DRM_IOCTL_GET_CTX, &ctx)) return -errno;
1356 if (ctx.flags & _DRM_CONTEXT_PRESERVED) *flags |= DRM_CONTEXT_PRESERVED;
1357 if (ctx.flags & _DRM_CONTEXT_2DONLY) *flags |= DRM_CONTEXT_2DONLY;
1364 * Free any kernel-level resources allocated with drmCreateContext() associated
1367 * \param fd file descriptor.
1368 * \param handle handle given by drmCreateContext().
1370 * \return zero on success, or a negative value on failure.
1372 * \note May only be called by root.
1375 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1376 * argument in a drm_ctx structure.
1378 int drmDestroyContext(int fd, drm_context_t handle)
1381 ctx.handle = handle;
1382 if (ioctl(fd, DRM_IOCTL_RM_CTX, &ctx)) return -errno;
1386 int drmCreateDrawable(int fd, drm_drawable_t *handle)
1389 if (ioctl(fd, DRM_IOCTL_ADD_DRAW, &draw)) return -errno;
1390 *handle = draw.handle;
1394 int drmDestroyDrawable(int fd, drm_drawable_t handle)
1397 draw.handle = handle;
1398 if (ioctl(fd, DRM_IOCTL_RM_DRAW, &draw)) return -errno;
1402 int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
1403 drm_drawable_info_type_t type, unsigned int num,
1406 drm_update_draw_t update;
1408 update.handle = handle;
1411 update.data = (unsigned long long)(unsigned long)data;
1413 if (ioctl(fd, DRM_IOCTL_UPDATE_DRAW, &update)) return -errno;
1419 * Acquire the AGP device.
1421 * Must be called before any of the other AGP related calls.
1423 * \param fd file descriptor.
1425 * \return zero on success, or a negative value on failure.
1428 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1430 int drmAgpAcquire(int fd)
1432 if (ioctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL)) return -errno;
1438 * Release the AGP device.
1440 * \param fd file descriptor.
1442 * \return zero on success, or a negative value on failure.
1445 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1447 int drmAgpRelease(int fd)
1449 if (ioctl(fd, DRM_IOCTL_AGP_RELEASE, NULL)) return -errno;
1457 * \param fd file descriptor.
1458 * \param mode AGP mode.
1460 * \return zero on success, or a negative value on failure.
1463 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1464 * argument in a drm_agp_mode structure.
1466 int drmAgpEnable(int fd, unsigned long mode)
1471 if (ioctl(fd, DRM_IOCTL_AGP_ENABLE, &m)) return -errno;
1477 * Allocate a chunk of AGP memory.
1479 * \param fd file descriptor.
1480 * \param size requested memory size in bytes. Will be rounded to page boundary.
1481 * \param type type of memory to allocate.
1482 * \param address if not zero, will be set to the physical address of the
1484 * \param handle on success will be set to a handle of the allocated memory.
1486 * \return zero on success, or a negative value on failure.
1489 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1490 * arguments in a drm_agp_buffer structure.
1492 int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
1493 unsigned long *address, drm_handle_t *handle)
1497 *handle = DRM_AGP_NO_HANDLE;
1501 if (ioctl(fd, DRM_IOCTL_AGP_ALLOC, &b)) return -errno;
1502 if (address != 0UL) *address = b.physical;
1509 * Free a chunk of AGP memory.
1511 * \param fd file descriptor.
1512 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1514 * \return zero on success, or a negative value on failure.
1517 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1518 * argument in a drm_agp_buffer structure.
1520 int drmAgpFree(int fd, drm_handle_t handle)
1526 if (ioctl(fd, DRM_IOCTL_AGP_FREE, &b)) return -errno;
1532 * Bind a chunk of AGP memory.
1534 * \param fd file descriptor.
1535 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1536 * \param offset offset in bytes. It will round to page boundary.
1538 * \return zero on success, or a negative value on failure.
1541 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1542 * argument in a drm_agp_binding structure.
1544 int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
1546 drm_agp_binding_t b;
1550 if (ioctl(fd, DRM_IOCTL_AGP_BIND, &b)) return -errno;
1556 * Unbind a chunk of AGP memory.
1558 * \param fd file descriptor.
1559 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1561 * \return zero on success, or a negative value on failure.
1564 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1565 * the argument in a drm_agp_binding structure.
1567 int drmAgpUnbind(int fd, drm_handle_t handle)
1569 drm_agp_binding_t b;
1573 if (ioctl(fd, DRM_IOCTL_AGP_UNBIND, &b)) return -errno;
1579 * Get AGP driver major version number.
1581 * \param fd file descriptor.
1583 * \return major version number on success, or a negative value on failure..
1586 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1587 * necessary information in a drm_agp_info structure.
1589 int drmAgpVersionMajor(int fd)
1593 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return -errno;
1594 return i.agp_version_major;
1599 * Get AGP driver minor version number.
1601 * \param fd file descriptor.
1603 * \return minor version number on success, or a negative value on failure.
1606 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1607 * necessary information in a drm_agp_info structure.
1609 int drmAgpVersionMinor(int fd)
1613 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return -errno;
1614 return i.agp_version_minor;
1621 * \param fd file descriptor.
1623 * \return mode on success, or zero on failure.
1626 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1627 * necessary information in a drm_agp_info structure.
1629 unsigned long drmAgpGetMode(int fd)
1633 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return 0;
1639 * Get AGP aperture base.
1641 * \param fd file descriptor.
1643 * \return aperture base on success, zero on failure.
1646 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1647 * necessary information in a drm_agp_info structure.
1649 unsigned long drmAgpBase(int fd)
1653 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return 0;
1654 return i.aperture_base;
1659 * Get AGP aperture size.
1661 * \param fd file descriptor.
1663 * \return aperture size on success, zero on failure.
1666 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1667 * necessary information in a drm_agp_info structure.
1669 unsigned long drmAgpSize(int fd)
1673 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return 0;
1674 return i.aperture_size;
1679 * Get used AGP memory.
1681 * \param fd file descriptor.
1683 * \return memory used on success, or zero on failure.
1686 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1687 * necessary information in a drm_agp_info structure.
1689 unsigned long drmAgpMemoryUsed(int fd)
1693 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return 0;
1694 return i.memory_used;
1699 * Get available AGP memory.
1701 * \param fd file descriptor.
1703 * \return memory available on success, or zero on failure.
1706 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1707 * necessary information in a drm_agp_info structure.
1709 unsigned long drmAgpMemoryAvail(int fd)
1713 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return 0;
1714 return i.memory_allowed;
1719 * Get hardware vendor ID.
1721 * \param fd file descriptor.
1723 * \return vendor ID 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 int drmAgpVendorId(int fd)
1733 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return 0;
1739 * Get hardware device ID.
1741 * \param fd file descriptor.
1743 * \return zero on success, or zero on failure.
1746 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1747 * necessary information in a drm_agp_info structure.
1749 unsigned int drmAgpDeviceId(int fd)
1753 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return 0;
1757 int drmScatterGatherAlloc(int fd, unsigned long size, drm_handle_t *handle)
1759 drm_scatter_gather_t sg;
1764 if (ioctl(fd, DRM_IOCTL_SG_ALLOC, &sg)) return -errno;
1765 *handle = sg.handle;
1769 int drmScatterGatherFree(int fd, drm_handle_t handle)
1771 drm_scatter_gather_t sg;
1775 if (ioctl(fd, DRM_IOCTL_SG_FREE, &sg)) return -errno;
1782 * \param fd file descriptor.
1783 * \param vbl pointer to a drmVBlank structure.
1785 * \return zero on success, or a negative value on failure.
1788 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
1790 int drmWaitVBlank(int fd, drmVBlankPtr vbl)
1795 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
1796 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
1797 } while (ret && errno == EINTR);
1802 int drmError(int err, const char *label)
1805 case DRM_ERR_NO_DEVICE: fprintf(stderr, "%s: no device\n", label); break;
1806 case DRM_ERR_NO_ACCESS: fprintf(stderr, "%s: no access\n", label); break;
1807 case DRM_ERR_NOT_ROOT: fprintf(stderr, "%s: not root\n", label); break;
1808 case DRM_ERR_INVALID: fprintf(stderr, "%s: invalid args\n", label);break;
1810 if (err < 0) err = -err;
1811 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
1819 * Install IRQ handler.
1821 * \param fd file descriptor.
1822 * \param irq IRQ number.
1824 * \return zero on success, or a negative value on failure.
1827 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
1828 * argument in a drm_control structure.
1830 int drmCtlInstHandler(int fd, int irq)
1834 ctl.func = DRM_INST_HANDLER;
1836 if (ioctl(fd, DRM_IOCTL_CONTROL, &ctl)) return -errno;
1842 * Uninstall IRQ handler.
1844 * \param fd file descriptor.
1846 * \return zero on success, or a negative value on failure.
1849 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
1850 * argument in a drm_control structure.
1852 int drmCtlUninstHandler(int fd)
1856 ctl.func = DRM_UNINST_HANDLER;
1858 if (ioctl(fd, DRM_IOCTL_CONTROL, &ctl)) return -errno;
1862 int drmFinish(int fd, int context, drmLockFlags flags)
1866 lock.context = context;
1868 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1869 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1870 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1871 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1872 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1873 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1874 if (ioctl(fd, DRM_IOCTL_FINISH, &lock)) return -errno;
1879 * Get IRQ from bus ID.
1881 * \param fd file descriptor.
1882 * \param busnum bus number.
1883 * \param devnum device number.
1884 * \param funcnum function number.
1886 * \return IRQ number on success, or a negative value on failure.
1889 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
1890 * arguments in a drm_irq_busid structure.
1892 int drmGetInterruptFromBusID(int fd, int busnum, int devnum, int funcnum)
1898 p.funcnum = funcnum;
1899 if (ioctl(fd, DRM_IOCTL_IRQ_BUSID, &p)) return -errno;
1903 int drmAddContextTag(int fd, drm_context_t context, void *tag)
1905 drmHashEntry *entry = drmGetEntry(fd);
1907 if (drmHashInsert(entry->tagTable, context, tag)) {
1908 drmHashDelete(entry->tagTable, context);
1909 drmHashInsert(entry->tagTable, context, tag);
1914 int drmDelContextTag(int fd, drm_context_t context)
1916 drmHashEntry *entry = drmGetEntry(fd);
1918 return drmHashDelete(entry->tagTable, context);
1921 void *drmGetContextTag(int fd, drm_context_t context)
1923 drmHashEntry *entry = drmGetEntry(fd);
1926 if (drmHashLookup(entry->tagTable, context, &value)) return NULL;
1931 int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
1932 drm_handle_t handle)
1934 drm_ctx_priv_map_t map;
1936 map.ctx_id = ctx_id;
1937 map.handle = (void *)handle;
1939 if (ioctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map)) return -errno;
1943 int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
1944 drm_handle_t *handle)
1946 drm_ctx_priv_map_t map;
1948 map.ctx_id = ctx_id;
1950 if (ioctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map)) return -errno;
1951 if (handle) *handle = (drm_handle_t)map.handle;
1956 int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
1957 drmMapType *type, drmMapFlags *flags, drm_handle_t *handle,
1963 if (ioctl(fd, DRM_IOCTL_GET_MAP, &map)) return -errno;
1964 *offset = map.offset;
1968 *handle = (unsigned long)map.handle;
1973 int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
1974 unsigned long *magic, unsigned long *iocs)
1976 drm_client_t client;
1979 if (ioctl(fd, DRM_IOCTL_GET_CLIENT, &client)) return -errno;
1980 *auth = client.auth;
1983 *magic = client.magic;
1984 *iocs = client.iocs;
1988 int drmGetStats(int fd, drmStatsT *stats)
1993 if (ioctl(fd, DRM_IOCTL_GET_STATS, &s)) return -errno;
1996 memset(stats, 0, sizeof(*stats));
1997 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
2001 stats->data[i].long_format = "%-20.20s"; \
2002 stats->data[i].rate_format = "%8.8s"; \
2003 stats->data[i].isvalue = 1; \
2004 stats->data[i].verbose = 0
2007 stats->data[i].long_format = "%-20.20s"; \
2008 stats->data[i].rate_format = "%5.5s"; \
2009 stats->data[i].isvalue = 0; \
2010 stats->data[i].mult_names = "kgm"; \
2011 stats->data[i].mult = 1000; \
2012 stats->data[i].verbose = 0
2015 stats->data[i].long_format = "%-20.20s"; \
2016 stats->data[i].rate_format = "%5.5s"; \
2017 stats->data[i].isvalue = 0; \
2018 stats->data[i].mult_names = "KGM"; \
2019 stats->data[i].mult = 1024; \
2020 stats->data[i].verbose = 0
2023 stats->count = s.count;
2024 for (i = 0; i < s.count; i++) {
2025 stats->data[i].value = s.data[i].value;
2026 switch (s.data[i].type) {
2027 case _DRM_STAT_LOCK:
2028 stats->data[i].long_name = "Lock";
2029 stats->data[i].rate_name = "Lock";
2032 case _DRM_STAT_OPENS:
2033 stats->data[i].long_name = "Opens";
2034 stats->data[i].rate_name = "O";
2036 stats->data[i].verbose = 1;
2038 case _DRM_STAT_CLOSES:
2039 stats->data[i].long_name = "Closes";
2040 stats->data[i].rate_name = "Lock";
2042 stats->data[i].verbose = 1;
2044 case _DRM_STAT_IOCTLS:
2045 stats->data[i].long_name = "Ioctls";
2046 stats->data[i].rate_name = "Ioc/s";
2049 case _DRM_STAT_LOCKS:
2050 stats->data[i].long_name = "Locks";
2051 stats->data[i].rate_name = "Lck/s";
2054 case _DRM_STAT_UNLOCKS:
2055 stats->data[i].long_name = "Unlocks";
2056 stats->data[i].rate_name = "Unl/s";
2060 stats->data[i].long_name = "IRQs";
2061 stats->data[i].rate_name = "IRQ/s";
2064 case _DRM_STAT_PRIMARY:
2065 stats->data[i].long_name = "Primary Bytes";
2066 stats->data[i].rate_name = "PB/s";
2069 case _DRM_STAT_SECONDARY:
2070 stats->data[i].long_name = "Secondary Bytes";
2071 stats->data[i].rate_name = "SB/s";
2075 stats->data[i].long_name = "DMA";
2076 stats->data[i].rate_name = "DMA/s";
2079 case _DRM_STAT_SPECIAL:
2080 stats->data[i].long_name = "Special DMA";
2081 stats->data[i].rate_name = "dma/s";
2084 case _DRM_STAT_MISSED:
2085 stats->data[i].long_name = "Miss";
2086 stats->data[i].rate_name = "Ms/s";
2089 case _DRM_STAT_VALUE:
2090 stats->data[i].long_name = "Value";
2091 stats->data[i].rate_name = "Value";
2094 case _DRM_STAT_BYTE:
2095 stats->data[i].long_name = "Bytes";
2096 stats->data[i].rate_name = "B/s";
2099 case _DRM_STAT_COUNT:
2101 stats->data[i].long_name = "Count";
2102 stats->data[i].rate_name = "Cnt/s";
2111 * Issue a set-version ioctl.
2113 * \param fd file descriptor.
2114 * \param drmCommandIndex command index
2115 * \param data source pointer of the data to be read and written.
2116 * \param size size of the data to be read and written.
2118 * \return zero on success, or a negative value on failure.
2121 * It issues a read-write ioctl given by
2122 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2124 int drmSetInterfaceVersion(int fd, drmSetVersion *version)
2127 drm_set_version_t sv;
2129 sv.drm_di_major = version->drm_di_major;
2130 sv.drm_di_minor = version->drm_di_minor;
2131 sv.drm_dd_major = version->drm_dd_major;
2132 sv.drm_dd_minor = version->drm_dd_minor;
2134 if (ioctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
2138 version->drm_di_major = sv.drm_di_major;
2139 version->drm_di_minor = sv.drm_di_minor;
2140 version->drm_dd_major = sv.drm_dd_major;
2141 version->drm_dd_minor = sv.drm_dd_minor;
2147 * Send a device-specific command.
2149 * \param fd file descriptor.
2150 * \param drmCommandIndex command index
2152 * \return zero on success, or a negative value on failure.
2155 * It issues a ioctl given by
2156 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2158 int drmCommandNone(int fd, unsigned long drmCommandIndex)
2160 void *data = NULL; /* dummy */
2161 unsigned long request;
2163 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2165 if (ioctl(fd, request, data)) {
2173 * Send a device-specific read command.
2175 * \param fd file descriptor.
2176 * \param drmCommandIndex command index
2177 * \param data destination pointer of the data to be read.
2178 * \param size size of the data to be read.
2180 * \return zero on success, or a negative value on failure.
2183 * It issues a read ioctl given by
2184 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2186 int drmCommandRead(int fd, unsigned long drmCommandIndex, void *data,
2189 unsigned long request;
2191 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2192 DRM_COMMAND_BASE + drmCommandIndex, size);
2194 if (ioctl(fd, request, data)) {
2202 * Send a device-specific write command.
2204 * \param fd file descriptor.
2205 * \param drmCommandIndex command index
2206 * \param data source pointer of the data to be written.
2207 * \param size size of the data to be written.
2209 * \return zero on success, or a negative value on failure.
2212 * It issues a write ioctl given by
2213 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2215 int drmCommandWrite(int fd, unsigned long drmCommandIndex, void *data,
2218 unsigned long request;
2220 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2221 DRM_COMMAND_BASE + drmCommandIndex, size);
2223 if (ioctl(fd, request, data)) {
2231 * Send a device-specific read-write command.
2233 * \param fd file descriptor.
2234 * \param drmCommandIndex command index
2235 * \param data source pointer of the data to be read and written.
2236 * \param size size of the data to be read and written.
2238 * \return zero on success, or a negative value on failure.
2241 * It issues a read-write ioctl given by
2242 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2244 int drmCommandWriteRead(int fd, unsigned long drmCommandIndex, void *data,
2247 unsigned long request;
2249 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2250 DRM_COMMAND_BASE + drmCommandIndex, size);
2252 if (ioctl(fd, request, data)) {
2261 * DRM_FENCE_FLAG_EMIT
2262 * DRM_FENCE_FLAG_SHAREABLE
2263 * DRM_FENCE_MASK_DRIVER
2266 int drmFenceCreate(int fd, unsigned flags, int class, unsigned type,
2269 drm_fence_arg_t arg;
2271 memset(&arg, 0, sizeof(arg));
2275 arg.op = drm_fence_create;
2276 if (ioctl(fd, DRM_IOCTL_FENCE, &arg))
2278 fence->handle = arg.handle;
2279 fence->class = arg.class;
2280 fence->type = arg.type;
2281 fence->flags = arg.flags;
2282 fence->signaled = 0;
2288 * DRM_FENCE_FLAG_SHAREABLE
2289 * DRM_FENCE_MASK_DRIVER
2292 int drmFenceBuffers(int fd, unsigned flags, drmFence *fence)
2294 drm_fence_arg_t arg;
2296 memset(&arg, 0, sizeof(arg));
2298 arg.op = drm_fence_buffers;
2299 if (ioctl(fd, DRM_IOCTL_FENCE, &arg))
2301 fence->handle = arg.handle;
2302 fence->class = arg.class;
2303 fence->type = arg.type;
2304 fence->flags = arg.flags;
2305 fence->signaled = 0;
2309 int drmFenceDestroy(int fd, const drmFence *fence)
2311 drm_fence_arg_t arg;
2313 memset(&arg, 0, sizeof(arg));
2314 arg.handle = fence->handle;
2315 arg.op = drm_fence_destroy;
2316 if (ioctl(fd, DRM_IOCTL_FENCE, &arg))
2321 int drmFenceReference(int fd, unsigned handle, drmFence *fence)
2323 drm_fence_arg_t arg;
2325 memset(&arg, 0, sizeof(arg));
2326 arg.handle = handle;
2327 arg.op = drm_fence_reference;
2328 if (ioctl(fd, DRM_IOCTL_FENCE, &arg))
2330 fence->handle = arg.handle;
2331 fence->class = arg.class;
2332 fence->type = arg.type;
2333 fence->flags = arg.flags;
2334 fence->signaled = arg.signaled;
2338 int drmFenceUnreference(int fd, const drmFence *fence)
2340 drm_fence_arg_t arg;
2342 memset(&arg, 0, sizeof(arg));
2343 arg.handle = fence->handle;
2344 arg.op = drm_fence_unreference;
2345 if (ioctl(fd, DRM_IOCTL_FENCE, &arg))
2350 int drmFenceFlush(int fd, drmFence *fence, unsigned flush_type)
2352 drm_fence_arg_t arg;
2354 memset(&arg, 0, sizeof(arg));
2355 arg.handle = fence->handle;
2356 arg.type = flush_type;
2357 arg.op = drm_fence_flush;
2358 if (ioctl(fd, DRM_IOCTL_FENCE, &arg))
2360 fence->class = arg.class;
2361 fence->type = arg.type;
2362 fence->signaled = arg.signaled;
2366 int drmFenceUpdate(int fd, drmFence *fence)
2368 drm_fence_arg_t arg;
2370 memset(&arg, 0, sizeof(arg));
2371 arg.handle = fence->handle;
2372 arg.op = drm_fence_signaled;
2373 if (ioctl(fd, DRM_IOCTL_FENCE, &arg))
2375 fence->class = arg.class;
2376 fence->type = arg.type;
2377 fence->signaled = arg.signaled;
2381 int drmFenceSignaled(int fd, drmFence *fence, unsigned fenceType,
2387 if ((fence->flags & DRM_FENCE_FLAG_SHAREABLE) ||
2388 ((fenceType & fence->signaled) != fenceType)) {
2390 ret = drmFenceFlush(fd, fence, fenceType);
2395 *signaled = ((fenceType & fence->signaled) == fenceType);
2402 * DRM_FENCE_FLAG_SHAREABLE
2403 * DRM_FENCE_MASK_DRIVER
2407 int drmFenceEmit(int fd, unsigned flags, drmFence *fence, unsigned emit_type)
2409 drm_fence_arg_t arg;
2411 memset(&arg, 0, sizeof(arg));
2412 arg.class = fence->class;
2414 arg.handle = fence->handle;
2415 arg.type = emit_type;
2416 arg.op = drm_fence_emit;
2417 if (ioctl(fd, DRM_IOCTL_FENCE, &arg))
2419 fence->class = arg.class;
2420 fence->type = arg.type;
2421 fence->signaled = arg.signaled;
2427 * DRM_FENCE_FLAG_WAIT_LAZY
2428 * DRM_FENCE_FLAG_WAIT_IGNORE_SIGNALS
2431 int drmFenceWait(int fd, unsigned flags, drmFence *fence, unsigned flush_type)
2433 drm_fence_arg_t arg;
2436 if (flush_type == 0) {
2437 flush_type = fence->type;
2440 if (!(fence->flags & DRM_FENCE_FLAG_SHAREABLE)) {
2441 if ((flush_type & fence->signaled) == flush_type) {
2446 memset(&arg, 0, sizeof(arg));
2447 arg.handle = fence->handle;
2448 arg.type = flush_type;
2450 arg.op = drm_fence_wait;
2452 ret = ioctl(fd, DRM_IOCTL_FENCE, &arg);
2453 } while (ret != 0 && errno == EAGAIN);
2458 fence->class = arg.class;
2459 fence->type = arg.type;
2460 fence->signaled = arg.signaled;
2464 static int drmAdjustListNodes(drmBOList *list)
2470 while(list->numCurrent < list->numTarget) {
2471 node = (drmBONode *) malloc(sizeof(*node));
2477 DRMLISTADD(&node->head, &list->free);
2480 while(list->numCurrent > list->numTarget) {
2481 l = list->free.next;
2482 if (l == &list->free)
2485 node = DRMLISTENTRY(drmBONode, l, head);
2492 void drmBOFreeList(drmBOList *list)
2497 l = list->list.next;
2498 while(l != &list->list) {
2500 node = DRMLISTENTRY(drmBONode, l, head);
2502 l = list->list.next;
2507 l = list->free.next;
2508 while(l != &list->free) {
2510 node = DRMLISTENTRY(drmBONode, l, head);
2512 l = list->free.next;
2517 int drmBOResetList(drmBOList *list) {
2522 ret = drmAdjustListNodes(list);
2526 l = list->list.next;
2527 while(l != &list->list) {
2529 DRMLISTADD(l, &list->free);
2531 l = list->list.next;
2533 return drmAdjustListNodes(list);
2536 static drmBONode *drmAddListItem(drmBOList *list, drmBO *item,
2543 l = list->free.next;
2544 if (l == &list->free) {
2545 node = (drmBONode *) malloc(sizeof(*node));
2552 node = DRMLISTENTRY(drmBONode, l, head);
2557 DRMLISTADD(&node->head, &list->list);
2562 void *drmBOListIterator(drmBOList *list)
2564 void *ret = list->list.next;
2566 if (ret == &list->list)
2571 void *drmBOListNext(drmBOList *list, void *iterator)
2575 drmMMListHead *l = (drmMMListHead *) iterator;
2577 if (ret == &list->list)
2582 drmBO *drmBOListBuf(void *iterator)
2585 drmMMListHead *l = (drmMMListHead *) iterator;
2586 node = DRMLISTENTRY(drmBONode, l, head);
2592 int drmBOCreateList(int numTarget, drmBOList *list)
2594 DRMINITLISTHEAD(&list->list);
2595 DRMINITLISTHEAD(&list->free);
2596 list->numTarget = numTarget;
2597 list->numCurrent = 0;
2598 list->numOnList = 0;
2599 return drmAdjustListNodes(list);
2602 static void drmBOCopyReply(const drm_bo_arg_reply_t *rep,
2605 buf->handle = rep->handle;
2606 buf->flags = rep->flags;
2607 buf->size = rep->size;
2608 buf->offset = rep->offset;
2609 buf->mapHandle = rep->arg_handle;
2610 buf->mask = rep->mask;
2611 buf->start = rep->buffer_start;
2612 buf->fenceFlags = rep->fence_flags;
2613 buf->replyFlags = rep->rep_flags;
2614 buf->pageAlignment = rep->page_alignment;
2619 int drmBOCreate(int fd, unsigned long start, unsigned long size,
2620 unsigned pageAlignment, void *user_buffer, drm_bo_type_t type,
2622 unsigned hint, drmBO *buf)
2625 drm_bo_arg_request_t *req = &arg.d.req;
2626 drm_bo_arg_reply_t *rep = &arg.d.rep;
2629 memset(buf, 0, sizeof(*buf));
2630 memset(&arg, 0, sizeof(arg));
2635 req->page_alignment = pageAlignment;
2637 buf->virtual = NULL;
2640 case drm_bo_type_dc:
2641 req->buffer_start = start;
2643 case drm_bo_type_user:
2644 req->buffer_start = (unsigned long) user_buffer;
2645 buf->virtual = user_buffer;
2647 case drm_bo_type_fake:
2648 req->buffer_start = start;
2653 req->op = drm_bo_create;
2656 ret = ioctl(fd, DRM_IOCTL_BUFOBJ, &arg);
2657 } while (ret != 0 && errno == EAGAIN);
2665 fprintf(stderr, "Error %d\n", rep->ret);
2669 drmBOCopyReply(rep, buf);
2670 buf->mapVirtual = NULL;
2676 int drmBODestroy(int fd, drmBO *buf)
2679 drm_bo_arg_request_t *req = &arg.d.req;
2680 drm_bo_arg_reply_t *rep = &arg.d.rep;
2682 if (buf->mapVirtual && (buf->type != drm_bo_type_fake)) {
2683 (void) drmUnmap(buf->mapVirtual, buf->start + buf->size);
2684 buf->mapVirtual = NULL;
2685 buf->virtual = NULL;
2688 memset(&arg, 0, sizeof(arg));
2689 req->handle = buf->handle;
2690 req->op = drm_bo_destroy;
2692 if (ioctl(fd, DRM_IOCTL_BUFOBJ, &arg))
2705 int drmBOReference(int fd, unsigned handle, drmBO *buf)
2709 drm_bo_arg_request_t *req = &arg.d.req;
2710 drm_bo_arg_reply_t *rep = &arg.d.rep;
2712 memset(&arg, 0, sizeof(arg));
2713 req->handle = handle;
2714 req->op = drm_bo_reference;
2716 if (ioctl(fd, DRM_IOCTL_BUFOBJ, &arg))
2725 drmBOCopyReply(rep, buf);
2726 buf->type = drm_bo_type_dc;
2727 buf->mapVirtual = NULL;
2729 buf->virtual = NULL;
2734 int drmBOUnReference(int fd, drmBO *buf)
2737 drm_bo_arg_request_t *req = &arg.d.req;
2738 drm_bo_arg_reply_t *rep = &arg.d.rep;
2741 if (buf->mapVirtual && (buf->type != drm_bo_type_fake)) {
2742 (void) munmap(buf->mapVirtual, buf->start + buf->size);
2743 buf->mapVirtual = NULL;
2744 buf->virtual = NULL;
2747 memset(&arg, 0, sizeof(arg));
2748 req->handle = buf->handle;
2749 req->op = drm_bo_unreference;
2751 if (ioctl(fd, DRM_IOCTL_BUFOBJ, &arg))
2765 * Flags can be DRM_BO_FLAG_READ, DRM_BO_FLAG_WRITE or'ed together
2766 * Hint currently be DRM_BO_HINT_DONT_BLOCK, which makes the
2767 * call return an -EBUSY if it can' immediately honor the mapping request.
2770 int drmBOMap(int fd, drmBO *buf, unsigned mapFlags, unsigned mapHint,
2775 drm_bo_arg_request_t *req = &arg.d.req;
2776 drm_bo_arg_reply_t *rep = &arg.d.rep;
2780 * Make sure we have a virtual address of the buffer.
2783 if (!buf->virtual && buf->type != drm_bo_type_fake) {
2785 virtual = mmap(0, buf->size + buf->start,
2786 PROT_READ | PROT_WRITE, MAP_SHARED,
2787 fd, buf->mapHandle);
2788 if (virtual == MAP_FAILED) {
2793 buf->mapVirtual = virtual;
2794 buf->virtual = ((char *) virtual) + buf->start;
2797 memset(&arg, 0, sizeof(arg));
2798 req->handle = buf->handle;
2799 req->mask = mapFlags;
2800 req->hint = mapHint;
2801 req->op = drm_bo_map;
2804 * May hang if the buffer object is busy.
2805 * This IOCTL synchronizes the buffer.
2809 ret = ioctl(fd, DRM_IOCTL_BUFOBJ, &arg);
2810 } while (ret != 0 && errno == EAGAIN);
2819 drmBOCopyReply(rep, buf);
2820 buf->mapFlags = mapFlags;
2822 *address = buf->virtual;
2827 int drmBOUnmap(int fd, drmBO *buf)
2830 drm_bo_arg_request_t *req = &arg.d.req;
2831 drm_bo_arg_reply_t *rep = &arg.d.rep;
2834 memset(&arg, 0, sizeof(arg));
2835 req->handle = buf->handle;
2836 req->op = drm_bo_unmap;
2838 if (ioctl(fd, DRM_IOCTL_BUFOBJ, &arg)) {
2849 int drmBOValidate(int fd, drmBO *buf, unsigned flags, unsigned mask,
2853 drm_bo_arg_request_t *req = &arg.d.req;
2854 drm_bo_arg_reply_t *rep = &arg.d.rep;
2857 memset(&arg, 0, sizeof(arg));
2858 req->handle = buf->handle;
2861 req->arg_handle = mask; /* Encode mask in the arg_handle field :/ */
2862 req->op = drm_bo_validate;
2865 ret = ioctl(fd, DRM_IOCTL_BUFOBJ, &arg);
2866 } while (ret && errno == EAGAIN);
2875 drmBOCopyReply(rep, buf);
2880 int drmBOFence(int fd, drmBO *buf, unsigned flags, unsigned fenceHandle)
2883 drm_bo_arg_request_t *req = &arg.d.req;
2884 drm_bo_arg_reply_t *rep = &arg.d.rep;
2887 memset(&arg, 0, sizeof(arg));
2888 req->handle = buf->handle;
2890 req->arg_handle = fenceHandle;
2891 req->op = drm_bo_fence;
2893 ret = ioctl(fd, DRM_IOCTL_BUFOBJ, &arg);
2904 int drmBOInfo(int fd, drmBO *buf)
2907 drm_bo_arg_request_t *req = &arg.d.req;
2908 drm_bo_arg_reply_t *rep = &arg.d.rep;
2911 memset(&arg, 0, sizeof(arg));
2912 req->handle = buf->handle;
2913 req->op = drm_bo_info;
2915 ret = ioctl(fd, DRM_IOCTL_BUFOBJ, &arg);
2923 drmBOCopyReply(rep, buf);
2927 int drmBOWaitIdle(int fd, drmBO *buf, unsigned hint)
2930 drm_bo_arg_request_t *req = &arg.d.req;
2931 drm_bo_arg_reply_t *rep = &arg.d.rep;
2934 if ((buf->flags & DRM_BO_FLAG_SHAREABLE) ||
2935 (buf->replyFlags & DRM_BO_REP_BUSY)) {
2936 memset(&arg, 0, sizeof(arg));
2937 req->handle = buf->handle;
2938 req->op = drm_bo_wait_idle;
2942 ret = ioctl(fd, DRM_IOCTL_BUFOBJ, &arg);
2943 } while (ret && errno == EAGAIN);
2951 drmBOCopyReply(rep, buf);
2956 int drmBOBusy(int fd, drmBO *buf, int *busy)
2958 if (!(buf->flags & DRM_BO_FLAG_SHAREABLE) &&
2959 !(buf->replyFlags & DRM_BO_REP_BUSY)) {
2963 int ret = drmBOInfo(fd, buf);
2966 *busy = (buf->replyFlags & DRM_BO_REP_BUSY);
2972 int drmAddValidateItem(drmBOList *list, drmBO *buf, unsigned flags,
2976 drmBONode *node, *cur;
2982 for (l = list->list.next; l != &list->list; l = l->next) {
2983 node = DRMLISTENTRY(drmBONode, l, head);
2984 if (node->buf == buf) {
2990 cur = drmAddListItem(list, buf, flags, mask);
2992 drmMsg("Out of memory creating validate list node.\n");
2999 unsigned memMask = (cur->arg1 | mask) & DRM_BO_MASK_MEM;
3000 unsigned memFlags = cur->arg0 & flags & memMask;
3003 drmMsg("Incompatible memory location requests "
3004 "on validate list.\n");
3005 drmMsg("Previous flag: 0x%08lx, mask: 0x%08lx\n",
3006 cur->arg0, cur->arg1);
3007 drmMsg("Current flag: 0x%08lx, mask: 0x%08lx\n",
3011 if (mask & cur->arg1 & ~DRM_BO_MASK_MEM & (cur->arg0 ^ flags)) {
3012 drmMsg("Incompatible buffer flag requests "
3013 "on validate list.\n");
3014 drmMsg("Previous flag: 0x%08lx, mask: 0x%08lx\n",
3015 cur->arg0, cur->arg1);
3016 drmMsg("Current flag: 0x%08lx, mask: 0x%08lx\n",
3021 cur->arg0 = memFlags | ((cur->arg0 | flags) &
3022 cur->arg1 & ~DRM_BO_MASK_MEM);
3028 int drmBOValidateList(int fd, drmBOList *list)
3033 drm_bo_arg_t *arg, *first;
3034 drm_bo_arg_request_t *req;
3035 drm_bo_arg_reply_t *rep;
3036 drm_u64_t *prevNext = NULL;
3042 for (l = list->list.next; l != &list->list; l = l->next) {
3043 node = DRMLISTENTRY(drmBONode, l, head);
3045 arg = &node->bo_arg;
3052 *prevNext = (unsigned long) arg;
3054 memset(arg, 0, sizeof(*arg));
3055 prevNext = &arg->next;
3056 req->handle = node->buf->handle;
3057 req->op = drm_bo_validate;
3058 req->mask = node->arg0;
3060 req->arg_handle = node->arg1;
3067 ret = ioctl(fd, DRM_IOCTL_BUFOBJ, first);
3068 } while (ret && errno == EAGAIN);
3074 for (l = list->list.next; l != &list->list; l = l->next) {
3075 node = DRMLISTENTRY(drmBONode, l, head);
3076 arg = &node->bo_arg;
3079 if (!arg->handled) {
3080 drmMsg("Unhandled request\n");
3087 drmBOCopyReply(rep, buf);
3094 int drmBOFenceList(int fd, drmBOList *list, unsigned fenceHandle)
3099 drm_bo_arg_t *arg, *first;
3100 drm_bo_arg_request_t *req;
3101 drm_bo_arg_reply_t *rep;
3102 drm_u64_t *prevNext = NULL;
3104 unsigned fence_flags;
3109 for (l = list->list.next; l != &list->list; l = l->next) {
3110 node = DRMLISTENTRY(drmBONode, l, head);
3112 arg = &node->bo_arg;
3119 *prevNext = (unsigned long) arg;
3121 memset(arg, 0, sizeof(*arg));
3122 prevNext = &arg->next;
3123 req->handle = node->buf->handle;
3124 req->op = drm_bo_fence;
3125 req->mask = node->arg0;
3126 req->arg_handle = fenceHandle;
3132 ret = ioctl(fd, DRM_IOCTL_BUFOBJ, first);
3137 for (l = list->list.next; l != &list->list; l = l->next) {
3138 node = DRMLISTENTRY(drmBONode, l, head);
3140 arg = &node->bo_arg;
3147 drmBOCopyReply(rep, node->buf);
3153 int drmMMInit(int fd, unsigned long pOffset, unsigned long pSize,
3156 drm_mm_init_arg_t arg;
3158 memset(&arg, 0, sizeof(arg));
3159 arg.req.op = mm_init;
3160 arg.req.p_offset = pOffset;
3161 arg.req.p_size = pSize;
3162 arg.req.mem_type = memType;
3164 if (ioctl(fd, DRM_IOCTL_MM_INIT, &arg))
3170 int drmMMTakedown(int fd, unsigned memType)
3172 drm_mm_init_arg_t arg;
3175 memset(&arg, 0, sizeof(arg));
3176 arg.req.op = mm_takedown;
3177 arg.req.mem_type = memType;
3179 if (ioctl(fd, DRM_IOCTL_MM_INIT, &arg))
3185 int drmMMLock(int fd, unsigned memType)
3187 drm_mm_init_arg_t arg;
3190 memset(&arg, 0, sizeof(arg));
3191 arg.req.op = mm_lock;
3192 arg.req.mem_type = memType;
3195 ret = ioctl(fd, DRM_IOCTL_MM_INIT, &arg);
3196 } while (ret && errno == EAGAIN);
3201 int drmMMUnlock(int fd, unsigned memType)
3203 drm_mm_init_arg_t arg;
3206 memset(&arg, 0, sizeof(arg));
3207 arg.req.op = mm_unlock;
3208 arg.req.mem_type = memType;
3211 ret = ioctl(fd, DRM_IOCTL_MM_INIT, &arg);
3212 } while (ret && errno == EAGAIN);
3217 #define DRM_MAX_FDS 16
3222 } connection[DRM_MAX_FDS];
3224 static int nr_fds = 0;
3226 int drmOpenOnce(void *unused,
3233 for (i = 0; i < nr_fds; i++)
3234 if (strcmp(BusID, connection[i].BusID) == 0) {
3235 connection[i].refcount++;
3237 return connection[i].fd;
3240 fd = drmOpen(unused, BusID);
3241 if (fd <= 0 || nr_fds == DRM_MAX_FDS)
3244 connection[nr_fds].BusID = strdup(BusID);
3245 connection[nr_fds].fd = fd;
3246 connection[nr_fds].refcount = 1;
3250 fprintf(stderr, "saved connection %d for %s %d\n",
3251 nr_fds, connection[nr_fds].BusID,
3252 strcmp(BusID, connection[nr_fds].BusID));
3259 void drmCloseOnce(int fd)
3263 for (i = 0; i < nr_fds; i++) {
3264 if (fd == connection[i].fd) {
3265 if (--connection[i].refcount == 0) {
3266 drmClose(connection[i].fd);
3267 free(connection[i].BusID);
3270 connection[i] = connection[nr_fds];