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.
34 #ifdef HAVE_XORG_CONFIG_H
35 #include <xorg-config.h>
40 # include "xf86_OSproc.h"
42 # include "xf86_ansic.h"
43 # define _DRM_MALLOC xalloc
44 # define _DRM_FREE xfree
45 # ifndef XFree86LOADER
46 # include <sys/mman.h>
60 # include <sys/types.h>
61 # include <sys/stat.h>
62 # define stat_t struct stat
63 # include <sys/ioctl.h>
64 # include <sys/mman.h>
65 # include <sys/time.h>
67 # define _DRM_MALLOC malloc
68 # define _DRM_FREE free
73 /* Not all systems have MAP_FAILED defined */
75 #define MAP_FAILED ((void *)-1)
80 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
93 #define DRM_MAJOR 226 /* Linux */
97 #define DRM_MAX_MINOR 16
101 * This definition needs to be changed on some systems if dev_t is a structure.
102 * If there is a header file we can get it from, there would be best.
105 #define makedev(x,y) ((dev_t)(((x) << 8) | (y)))
108 #define DRM_MSG_VERBOSITY 3
111 * Output a message to stderr.
113 * \param format printf() like format string.
116 * This function is a wrapper around vfprintf().
119 drmMsg(const char *format, ...)
123 #ifndef XFree86Server
125 if ((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose"))
128 va_start(ap, format);
130 xf86VDrvMsgVerb(-1, X_NONE, DRM_MSG_VERBOSITY, format, ap);
132 vfprintf(stderr, format, ap);
138 static void *drmHashTable = NULL; /* Context switch callbacks */
140 typedef struct drmHashEntry {
142 void (*f)(int, void *, void *);
146 void *drmMalloc(int size)
149 if ((pt = _DRM_MALLOC(size))) memset(pt, 0, size);
153 void drmFree(void *pt)
155 if (pt) _DRM_FREE(pt);
158 /* drmStrdup can't use strdup(3), since it doesn't call _DRM_MALLOC... */
159 static char *drmStrdup(const char *s)
166 retval = _DRM_MALLOC(strlen(s)+1);
176 static unsigned long drmGetKeyFromFd(int fd)
185 static drmHashEntry *drmGetEntry(int fd)
187 unsigned long key = drmGetKeyFromFd(fd);
191 if (!drmHashTable) drmHashTable = drmHashCreate();
193 if (drmHashLookup(drmHashTable, key, &value)) {
194 entry = drmMalloc(sizeof(*entry));
197 entry->tagTable = drmHashCreate();
198 drmHashInsert(drmHashTable, key, entry);
206 * Compare two busid strings
211 * \return 1 if matched.
214 * This function compares two bus ID strings. It understands the older
215 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
216 * domain, b is bus, d is device, f is function.
218 static int drmMatchBusID(const char *id1, const char *id2)
220 /* First, check if the IDs are exactly the same */
221 if (strcasecmp(id1, id2) == 0)
224 /* Try to match old/new-style PCI bus IDs. */
225 if (strncasecmp(id1, "pci", 3) == 0) {
230 ret = sscanf(id1, "pci:%04x:%02x:%02x.%d", &o1, &b1, &d1, &f1);
233 ret = sscanf(id1, "PCI:%d:%d:%d", &b1, &d1, &f1);
238 ret = sscanf(id2, "pci:%04x:%02x:%02x.%d", &o2, &b2, &d2, &f2);
241 ret = sscanf(id2, "PCI:%d:%d:%d", &b2, &d2, &f2);
246 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
255 * Open the DRM device, creating it if necessary.
257 * \param dev major and minor numbers of the device.
258 * \param minor minor number of the device.
260 * \return a file descriptor on success, or a negative value on error.
263 * Assembles the device name from \p minor and opens it, creating the device
264 * special file node with the major and minor numbers specified by \p dev and
265 * parent directory if necessary and was called by root.
267 static int drmOpenDevice(long dev, int minor)
272 mode_t devmode = DRM_DEV_MODE;
273 int isroot = !geteuid();
274 #if defined(XFree86Server)
275 uid_t user = DRM_DEV_UID;
276 gid_t group = DRM_DEV_GID;
279 sprintf(buf, DRM_DEV_NAME, DRM_DIR_NAME, minor);
280 drmMsg("drmOpenDevice: node name is %s\n", buf);
282 #if defined(XFree86Server)
283 devmode = xf86ConfigDRI.mode ? xf86ConfigDRI.mode : DRM_DEV_MODE;
284 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
285 group = (xf86ConfigDRI.group >= 0) ? xf86ConfigDRI.group : DRM_DEV_GID;
288 if (stat(DRM_DIR_NAME, &st)) {
289 if (!isroot) return DRM_ERR_NOT_ROOT;
290 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
291 chown(DRM_DIR_NAME, 0, 0); /* root:root */
292 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
295 /* Check if the device node exists and create it if necessary. */
296 if (stat(buf, &st)) {
297 if (!isroot) return DRM_ERR_NOT_ROOT;
299 mknod(buf, S_IFCHR | devmode, dev);
301 #if defined(XFree86Server)
302 chown(buf, user, group);
306 fd = open(buf, O_RDWR, 0);
307 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
308 fd, fd < 0 ? strerror(errno) : "OK");
309 if (fd >= 0) return fd;
311 /* Check if the device node is not what we expect it to be, and recreate it
312 * and try again if so.
314 if (st.st_rdev != dev) {
315 if (!isroot) return DRM_ERR_NOT_ROOT;
317 mknod(buf, S_IFCHR | devmode, dev);
318 #if defined(XFree86Server)
319 chown(buf, user, group);
323 fd = open(buf, O_RDWR, 0);
324 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
325 fd, fd < 0 ? strerror(errno) : "OK");
326 if (fd >= 0) return fd;
328 drmMsg("drmOpenDevice: Open failed\n");
335 * Open the DRM device
337 * \param minor device minor number.
338 * \param create allow to create the device if set.
340 * \return a file descriptor on success, or a negative value on error.
343 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
344 * name from \p minor and opens it.
346 static int drmOpenMinor(int minor, int create)
351 if (create) return drmOpenDevice(makedev(DRM_MAJOR, minor), minor);
353 sprintf(buf, DRM_DEV_NAME, DRM_DIR_NAME, minor);
354 if ((fd = open(buf, O_RDWR, 0)) >= 0) return fd;
360 * Determine whether the DRM kernel driver has been loaded.
362 * \return 1 if the DRM driver is loaded, 0 otherwise.
365 * Determine the presence of the kernel driver by attempting to open the 0
366 * minor and get version information. For backward compatibility with older
367 * Linux implementations, /proc/dri is also checked.
369 int drmAvailable(void)
371 drmVersionPtr version;
375 if ((fd = drmOpenMinor(0, 1)) < 0) {
377 /* Try proc for backward Linux compatibility */
378 if (!access("/proc/dri/0", R_OK)) return 1;
383 if ((version = drmGetVersion(fd))) {
385 drmFreeVersion(version);
394 * Open the device by bus ID.
396 * \param busid bus ID.
398 * \return a file descriptor on success, or a negative value on error.
401 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
402 * comparing the device bus ID with the one supplied.
404 * \sa drmOpenMinor() and drmGetBusid().
406 static int drmOpenByBusid(const char *busid)
413 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
414 for (i = 0; i < DRM_MAX_MINOR; i++) {
415 fd = drmOpenMinor(i, 1);
416 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
420 sv.drm_dd_major = -1; /* Don't care */
421 sv.drm_dd_minor = -1; /* Don't care */
422 drmSetInterfaceVersion(fd, &sv);
423 buf = drmGetBusid(fd);
424 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
425 if (buf && drmMatchBusID(buf, busid)) {
429 if (buf) drmFreeBusid(buf);
438 * Open the device by name.
440 * \param name driver name.
442 * \return a file descriptor on success, or a negative value on error.
445 * This function opens the first minor number that matches the driver name and
446 * isn't already in use. If it's in use it then it will already have a bus ID
449 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
451 static int drmOpenByName(const char *name)
455 drmVersionPtr version;
458 if (!drmAvailable()) {
459 #if !defined(XFree86Server)
462 /* try to load the kernel module now */
463 if (!xf86LoadKernelModule(name)) {
464 ErrorF("[drm] failed to load kernel module \"%s\"\n",
472 * Open the first minor number that matches the driver name and isn't
473 * already in use. If it's in use it will have a busid assigned already.
475 for (i = 0; i < DRM_MAX_MINOR; i++) {
476 if ((fd = drmOpenMinor(i, 1)) >= 0) {
477 if ((version = drmGetVersion(fd))) {
478 if (!strcmp(version->name, name)) {
479 drmFreeVersion(version);
480 id = drmGetBusid(fd);
481 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
490 drmFreeVersion(version);
498 /* Backward-compatibility /proc support */
499 for (i = 0; i < 8; i++) {
500 char proc_name[64], buf[512];
501 char *driver, *pt, *devstring;
504 sprintf(proc_name, "/proc/dri/%d/name", i);
505 if ((fd = open(proc_name, 0, 0)) >= 0) {
506 retcode = read(fd, buf, sizeof(buf)-1);
509 buf[retcode-1] = '\0';
510 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
512 if (*pt) { /* Device is next */
514 if (!strcmp(driver, name)) { /* Match */
515 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
517 if (*pt) { /* Found busid */
518 return drmOpenByBusid(++pt);
519 } else { /* No busid */
520 return drmOpenDevice(strtol(devstring, NULL, 0),i);
534 * Open the DRM device.
536 * Looks up the specified name and bus ID, and opens the device found. The
537 * entry in /dev/dri is created if necessary and if called by root.
539 * \param name driver name. Not referenced if bus ID is supplied.
540 * \param busid bus ID. Zero if not known.
542 * \return a file descriptor on success, or a negative value on error.
545 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
548 int drmOpen(const char *name, const char *busid)
551 if (!drmAvailable() && name != NULL) {
552 /* try to load the kernel */
553 if (!xf86LoadKernelModule(name)) {
554 ErrorF("[drm] failed to load kernel module \"%s\"\n",
564 fd = drmOpenByBusid(busid);
570 return drmOpenByName(name);
577 * Free the version information returned by drmGetVersion().
579 * \param v pointer to the version information.
582 * It frees the memory pointed by \p %v as well as all the non-null strings
585 void drmFreeVersion(drmVersionPtr v)
588 if (v->name) drmFree(v->name);
589 if (v->date) drmFree(v->date);
590 if (v->desc) drmFree(v->desc);
596 * Free the non-public version information returned by the kernel.
598 * \param v pointer to the version information.
601 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
602 * the non-null strings pointers in it.
604 static void drmFreeKernelVersion(drm_version_t *v)
607 if (v->name) drmFree(v->name);
608 if (v->date) drmFree(v->date);
609 if (v->desc) drmFree(v->desc);
615 * Copy version information.
617 * \param d destination pointer.
618 * \param s source pointer.
621 * Used by drmGetVersion() to translate the information returned by the ioctl
622 * interface in a private structure into the public structure counterpart.
624 static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
626 d->version_major = s->version_major;
627 d->version_minor = s->version_minor;
628 d->version_patchlevel = s->version_patchlevel;
629 d->name_len = s->name_len;
630 d->name = drmStrdup(s->name);
631 d->date_len = s->date_len;
632 d->date = drmStrdup(s->date);
633 d->desc_len = s->desc_len;
634 d->desc = drmStrdup(s->desc);
639 * Query the driver version information.
641 * \param fd file descriptor.
643 * \return pointer to a drmVersion structure which should be freed with
646 * \note Similar information is available via /proc/dri.
649 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
650 * first with zeros to get the string lengths, and then the actually strings.
651 * It also null-terminates them since they might not be already.
653 drmVersionPtr drmGetVersion(int fd)
655 drmVersionPtr retval;
656 drm_version_t *version = drmMalloc(sizeof(*version));
658 version->name_len = 0;
659 version->name = NULL;
660 version->date_len = 0;
661 version->date = NULL;
662 version->desc_len = 0;
663 version->desc = NULL;
665 if (ioctl(fd, DRM_IOCTL_VERSION, version)) {
666 drmFreeKernelVersion(version);
670 if (version->name_len)
671 version->name = drmMalloc(version->name_len + 1);
672 if (version->date_len)
673 version->date = drmMalloc(version->date_len + 1);
674 if (version->desc_len)
675 version->desc = drmMalloc(version->desc_len + 1);
677 if (ioctl(fd, DRM_IOCTL_VERSION, version)) {
678 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
679 drmFreeKernelVersion(version);
683 /* The results might not be null-terminated strings, so terminate them. */
684 if (version->name_len) version->name[version->name_len] = '\0';
685 if (version->date_len) version->date[version->date_len] = '\0';
686 if (version->desc_len) version->desc[version->desc_len] = '\0';
688 retval = drmMalloc(sizeof(*retval));
689 drmCopyVersion(retval, version);
690 drmFreeKernelVersion(version);
696 * Get version information for the DRM user space library.
698 * This version number is driver independent.
700 * \param fd file descriptor.
702 * \return version information.
705 * This function allocates and fills a drm_version structure with a hard coded
708 drmVersionPtr drmGetLibVersion(int fd)
710 drm_version_t *version = drmMalloc(sizeof(*version));
713 * revision 1.0.x = original DRM interface with no drmGetLibVersion
714 * entry point and many drm<Device> extensions
715 * revision 1.1.x = added drmCommand entry points for device extensions
716 * added drmGetLibVersion to identify libdrm.a version
717 * revision 1.2.x = added drmSetInterfaceVersion
718 * modified drmOpen to handle both busid and name
720 version->version_major = 1;
721 version->version_minor = 2;
722 version->version_patchlevel = 0;
724 return (drmVersionPtr)version;
729 * Free the bus ID information.
731 * \param busid bus ID information string as given by drmGetBusid().
734 * This function is just frees the memory pointed by \p busid.
736 void drmFreeBusid(const char *busid)
738 drmFree((void *)busid);
743 * Get the bus ID of the device.
745 * \param fd file descriptor.
747 * \return bus ID string.
750 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
751 * get the string length and data, passing the arguments in a drm_unique
754 char *drmGetBusid(int fd)
761 if (ioctl(fd, DRM_IOCTL_GET_UNIQUE, &u)) return NULL;
762 u.unique = drmMalloc(u.unique_len + 1);
763 if (ioctl(fd, DRM_IOCTL_GET_UNIQUE, &u)) return NULL;
764 u.unique[u.unique_len] = '\0';
771 * Set the bus ID of the device.
773 * \param fd file descriptor.
774 * \param busid bus ID string.
776 * \return zero on success, negative on failure.
779 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
780 * the arguments in a drm_unique structure.
782 int drmSetBusid(int fd, const char *busid)
786 u.unique = (char *)busid;
787 u.unique_len = strlen(busid);
789 if (ioctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
795 int drmGetMagic(int fd, drm_magic_t * magic)
800 if (ioctl(fd, DRM_IOCTL_GET_MAGIC, &auth)) return -errno;
805 int drmAuthMagic(int fd, drm_magic_t magic)
810 if (ioctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth)) return -errno;
815 * Specifies a range of memory that is available for mapping by a
818 * \param fd file descriptor.
819 * \param offset usually the physical address. The actual meaning depends of
820 * the \p type parameter. See below.
821 * \param size of the memory in bytes.
822 * \param type type of the memory to be mapped.
823 * \param flags combination of several flags to modify the function actions.
824 * \param handle will be set to a value that may be used as the offset
825 * parameter for mmap().
827 * \return zero on success or a negative value on error.
829 * \par Mapping the frame buffer
830 * For the frame buffer
831 * - \p offset will be the physical address of the start of the frame buffer,
832 * - \p size will be the size of the frame buffer in bytes, and
833 * - \p type will be DRM_FRAME_BUFFER.
836 * The area mapped will be uncached. If MTRR support is available in the
837 * kernel, the frame buffer area will be set to write combining.
839 * \par Mapping the MMIO register area
840 * For the MMIO register area,
841 * - \p offset will be the physical address of the start of the register area,
842 * - \p size will be the size of the register area bytes, and
843 * - \p type will be DRM_REGISTERS.
845 * The area mapped will be uncached.
847 * \par Mapping the SAREA
849 * - \p offset will be ignored and should be set to zero,
850 * - \p size will be the desired size of the SAREA in bytes,
851 * - \p type will be DRM_SHM.
854 * A shared memory area of the requested size will be created and locked in
855 * kernel memory. This area may be mapped into client-space by using the handle
858 * \note May only be called by root.
861 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
862 * the arguments in a drm_map structure.
864 int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
865 drmMapFlags flags, drm_handle_t *handle)
874 if (ioctl(fd, DRM_IOCTL_ADD_MAP, &map)) return -errno;
875 if (handle) *handle = (drm_handle_t)map.handle;
879 int drmRmMap(int fd, drm_handle_t handle)
883 map.handle = (void *)handle;
885 if(ioctl(fd, DRM_IOCTL_RM_MAP, &map)) return -errno;
890 * Make buffers available for DMA transfers.
892 * \param fd file descriptor.
893 * \param count number of buffers.
894 * \param size size of each buffer.
895 * \param flags buffer allocation flags.
896 * \param agp_offset offset in the AGP aperture
898 * \return number of buffers allocated, negative on error.
901 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
905 int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
908 drm_buf_desc_t request;
910 request.count = count;
912 request.low_mark = 0;
913 request.high_mark = 0;
914 request.flags = flags;
915 request.agp_start = agp_offset;
917 if (ioctl(fd, DRM_IOCTL_ADD_BUFS, &request)) return -errno;
918 return request.count;
921 int drmMarkBufs(int fd, double low, double high)
929 if (ioctl(fd, DRM_IOCTL_INFO_BUFS, &info)) return -EINVAL;
931 if (!info.count) return -EINVAL;
933 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
936 if (ioctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
942 for (i = 0; i < info.count; i++) {
943 info.list[i].low_mark = low * info.list[i].count;
944 info.list[i].high_mark = high * info.list[i].count;
945 if (ioctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
959 * \param fd file descriptor.
960 * \param count number of buffers to free.
961 * \param list list of buffers to be freed.
963 * \return zero on success, or a negative value on failure.
965 * \note This function is primarily used for debugging.
968 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
969 * the arguments in a drm_buf_free structure.
971 int drmFreeBufs(int fd, int count, int *list)
973 drm_buf_free_t request;
975 request.count = count;
977 if (ioctl(fd, DRM_IOCTL_FREE_BUFS, &request)) return -errno;
985 * \param fd file descriptor.
988 * This function closes the file descriptor.
992 unsigned long key = drmGetKeyFromFd(fd);
993 drmHashEntry *entry = drmGetEntry(fd);
995 drmHashDestroy(entry->tagTable);
998 entry->tagTable = NULL;
1000 drmHashDelete(drmHashTable, key);
1008 * Map a region of memory.
1010 * \param fd file descriptor.
1011 * \param handle handle returned by drmAddMap().
1012 * \param size size in bytes. Must match the size used by drmAddMap().
1013 * \param address will contain the user-space virtual address where the mapping
1016 * \return zero on success, or a negative value on failure.
1019 * This function is a wrapper for mmap().
1021 int drmMap(int fd, drm_handle_t handle, drmSize size, drmAddressPtr address)
1023 static unsigned long pagesize_mask = 0;
1025 if (fd < 0) return -EINVAL;
1028 pagesize_mask = getpagesize() - 1;
1030 size = (size + pagesize_mask) & ~pagesize_mask;
1032 *address = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
1033 if (*address == MAP_FAILED) return -errno;
1039 * Unmap mappings obtained with drmMap().
1041 * \param address address as given by drmMap().
1042 * \param size size in bytes. Must match the size used by drmMap().
1044 * \return zero on success, or a negative value on failure.
1047 * This function is a wrapper for munmap().
1049 int drmUnmap(drmAddress address, drmSize size)
1051 return munmap(address, size);
1054 drmBufInfoPtr drmGetBufInfo(int fd)
1056 drm_buf_info_t info;
1057 drmBufInfoPtr retval;
1063 if (ioctl(fd, DRM_IOCTL_INFO_BUFS, &info)) return NULL;
1066 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1069 if (ioctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1074 retval = drmMalloc(sizeof(*retval));
1075 retval->count = info.count;
1076 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1077 for (i = 0; i < info.count; i++) {
1078 retval->list[i].count = info.list[i].count;
1079 retval->list[i].size = info.list[i].size;
1080 retval->list[i].low_mark = info.list[i].low_mark;
1081 retval->list[i].high_mark = info.list[i].high_mark;
1090 * Map all DMA buffers into client-virtual space.
1092 * \param fd file descriptor.
1094 * \return a pointer to a ::drmBufMap structure.
1096 * \note The client may not use these buffers until obtaining buffer indices
1100 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1101 * information about the buffers in a drm_buf_map structure into the
1102 * client-visible data structures.
1104 drmBufMapPtr drmMapBufs(int fd)
1107 drmBufMapPtr retval;
1112 bufs.virtual = NULL;
1113 if (ioctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) return NULL;
1115 if (!bufs.count) return NULL;
1117 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1120 if (ioctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
1125 retval = drmMalloc(sizeof(*retval));
1126 retval->count = bufs.count;
1127 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1128 for (i = 0; i < bufs.count; i++) {
1129 retval->list[i].idx = bufs.list[i].idx;
1130 retval->list[i].total = bufs.list[i].total;
1131 retval->list[i].used = 0;
1132 retval->list[i].address = bufs.list[i].address;
1142 * Unmap buffers allocated with drmMapBufs().
1144 * \return zero on success, or negative value on failure.
1147 * Calls munmap() for every buffer stored in \p bufs and frees the
1148 * memory allocated by drmMapBufs().
1150 int drmUnmapBufs(drmBufMapPtr bufs)
1154 for (i = 0; i < bufs->count; i++) {
1155 munmap(bufs->list[i].address, bufs->list[i].total);
1158 drmFree(bufs->list);
1165 #define DRM_DMA_RETRY 16
1168 * Reserve DMA buffers.
1170 * \param fd file descriptor.
1173 * \return zero on success, or a negative value on failure.
1176 * Assemble the arguments into a drm_dma structure and keeps issuing the
1177 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1179 int drmDMA(int fd, drmDMAReqPtr request)
1184 dma.context = request->context;
1185 dma.send_count = request->send_count;
1186 dma.send_indices = request->send_list;
1187 dma.send_sizes = request->send_sizes;
1188 dma.flags = request->flags;
1189 dma.request_count = request->request_count;
1190 dma.request_size = request->request_size;
1191 dma.request_indices = request->request_list;
1192 dma.request_sizes = request->request_sizes;
1193 dma.granted_count = 0;
1196 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1197 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1200 request->granted_count = dma.granted_count;
1209 * Obtain heavyweight hardware lock.
1211 * \param fd file descriptor.
1212 * \param context context.
1213 * \param flags flags that determine the sate of the hardware when the function
1216 * \return always zero.
1219 * This function translates the arguments into a drm_lock structure and issue
1220 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1222 int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
1226 lock.context = context;
1228 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1229 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1230 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1231 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1232 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1233 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1235 while (ioctl(fd, DRM_IOCTL_LOCK, &lock))
1241 * Release the hardware lock.
1243 * \param fd file descriptor.
1244 * \param context context.
1246 * \return zero on success, or a negative value on failure.
1249 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1250 * argument in a drm_lock structure.
1252 int drmUnlock(int fd, drm_context_t context)
1256 lock.context = context;
1258 return ioctl(fd, DRM_IOCTL_UNLOCK, &lock);
1261 drm_context_t *drmGetReservedContextList(int fd, int *count)
1265 drm_context_t * retval;
1269 res.contexts = NULL;
1270 if (ioctl(fd, DRM_IOCTL_RES_CTX, &res)) return NULL;
1272 if (!res.count) return NULL;
1274 if (!(list = drmMalloc(res.count * sizeof(*list)))) return NULL;
1275 if (!(retval = drmMalloc(res.count * sizeof(*retval)))) {
1280 res.contexts = list;
1281 if (ioctl(fd, DRM_IOCTL_RES_CTX, &res)) return NULL;
1283 for (i = 0; i < res.count; i++) retval[i] = list[i].handle;
1290 void drmFreeReservedContextList(drm_context_t *pt)
1298 * Used by the X server during GLXContext initialization. This causes
1299 * per-context kernel-level resources to be allocated.
1301 * \param fd file descriptor.
1302 * \param handle is set on success. To be used by the client when requesting DMA
1303 * dispatch with drmDMA().
1305 * \return zero on success, or a negative value on failure.
1307 * \note May only be called by root.
1310 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1311 * argument in a drm_ctx structure.
1313 int drmCreateContext(int fd, drm_context_t *handle)
1317 ctx.flags = 0; /* Modified with functions below */
1318 if (ioctl(fd, DRM_IOCTL_ADD_CTX, &ctx)) return -errno;
1319 *handle = ctx.handle;
1323 int drmSwitchToContext(int fd, drm_context_t context)
1327 ctx.handle = context;
1328 if (ioctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx)) return -errno;
1332 int drmSetContextFlags(int fd, drm_context_t context, drm_context_tFlags flags)
1337 * Context preserving means that no context switches are done between DMA
1338 * buffers from one context and the next. This is suitable for use in the
1339 * X server (which promises to maintain hardware context), or in the
1340 * client-side library when buffers are swapped on behalf of two threads.
1342 ctx.handle = context;
1344 if (flags & DRM_CONTEXT_PRESERVED) ctx.flags |= _DRM_CONTEXT_PRESERVED;
1345 if (flags & DRM_CONTEXT_2DONLY) ctx.flags |= _DRM_CONTEXT_2DONLY;
1346 if (ioctl(fd, DRM_IOCTL_MOD_CTX, &ctx)) return -errno;
1350 int drmGetContextFlags(int fd, drm_context_t context,
1351 drm_context_tFlagsPtr flags)
1355 ctx.handle = context;
1356 if (ioctl(fd, DRM_IOCTL_GET_CTX, &ctx)) return -errno;
1358 if (ctx.flags & _DRM_CONTEXT_PRESERVED) *flags |= DRM_CONTEXT_PRESERVED;
1359 if (ctx.flags & _DRM_CONTEXT_2DONLY) *flags |= DRM_CONTEXT_2DONLY;
1366 * Free any kernel-level resources allocated with drmCreateContext() associated
1369 * \param fd file descriptor.
1370 * \param handle handle given by drmCreateContext().
1372 * \return zero on success, or a negative value on failure.
1374 * \note May only be called by root.
1377 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1378 * argument in a drm_ctx structure.
1380 int drmDestroyContext(int fd, drm_context_t handle)
1383 ctx.handle = handle;
1384 if (ioctl(fd, DRM_IOCTL_RM_CTX, &ctx)) return -errno;
1388 int drmCreateDrawable(int fd, drm_drawable_t *handle)
1391 if (ioctl(fd, DRM_IOCTL_ADD_DRAW, &draw)) return -errno;
1392 *handle = draw.handle;
1396 int drmDestroyDrawable(int fd, drm_drawable_t handle)
1399 draw.handle = handle;
1400 if (ioctl(fd, DRM_IOCTL_RM_DRAW, &draw)) return -errno;
1404 int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
1405 drm_drawable_info_type_t type, unsigned int num,
1408 drm_update_draw_t update;
1410 update.handle = handle;
1413 update.data = (unsigned long long)(unsigned long)data;
1415 if (ioctl(fd, DRM_IOCTL_UPDATE_DRAW, &update)) return -errno;
1421 * Acquire the AGP device.
1423 * Must be called before any of the other AGP related calls.
1425 * \param fd file descriptor.
1427 * \return zero on success, or a negative value on failure.
1430 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1432 int drmAgpAcquire(int fd)
1434 if (ioctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL)) return -errno;
1440 * Release the AGP device.
1442 * \param fd file descriptor.
1444 * \return zero on success, or a negative value on failure.
1447 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1449 int drmAgpRelease(int fd)
1451 if (ioctl(fd, DRM_IOCTL_AGP_RELEASE, NULL)) return -errno;
1459 * \param fd file descriptor.
1460 * \param mode AGP mode.
1462 * \return zero on success, or a negative value on failure.
1465 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1466 * argument in a drm_agp_mode structure.
1468 int drmAgpEnable(int fd, unsigned long mode)
1473 if (ioctl(fd, DRM_IOCTL_AGP_ENABLE, &m)) return -errno;
1479 * Allocate a chunk of AGP memory.
1481 * \param fd file descriptor.
1482 * \param size requested memory size in bytes. Will be rounded to page boundary.
1483 * \param type type of memory to allocate.
1484 * \param address if not zero, will be set to the physical address of the
1486 * \param handle on success will be set to a handle of the allocated memory.
1488 * \return zero on success, or a negative value on failure.
1491 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1492 * arguments in a drm_agp_buffer structure.
1494 int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
1495 unsigned long *address, drm_handle_t *handle)
1499 *handle = DRM_AGP_NO_HANDLE;
1503 if (ioctl(fd, DRM_IOCTL_AGP_ALLOC, &b)) return -errno;
1504 if (address != 0UL) *address = b.physical;
1511 * Free a chunk of AGP memory.
1513 * \param fd file descriptor.
1514 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1516 * \return zero on success, or a negative value on failure.
1519 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1520 * argument in a drm_agp_buffer structure.
1522 int drmAgpFree(int fd, drm_handle_t handle)
1528 if (ioctl(fd, DRM_IOCTL_AGP_FREE, &b)) return -errno;
1534 * Bind a chunk of AGP memory.
1536 * \param fd file descriptor.
1537 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1538 * \param offset offset in bytes. It will round to page boundary.
1540 * \return zero on success, or a negative value on failure.
1543 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1544 * argument in a drm_agp_binding structure.
1546 int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
1548 drm_agp_binding_t b;
1552 if (ioctl(fd, DRM_IOCTL_AGP_BIND, &b)) return -errno;
1558 * Unbind a chunk of AGP memory.
1560 * \param fd file descriptor.
1561 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1563 * \return zero on success, or a negative value on failure.
1566 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1567 * the argument in a drm_agp_binding structure.
1569 int drmAgpUnbind(int fd, drm_handle_t handle)
1571 drm_agp_binding_t b;
1575 if (ioctl(fd, DRM_IOCTL_AGP_UNBIND, &b)) return -errno;
1581 * Get AGP driver major version number.
1583 * \param fd file descriptor.
1585 * \return major version number on success, or a negative value on failure..
1588 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1589 * necessary information in a drm_agp_info structure.
1591 int drmAgpVersionMajor(int fd)
1595 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return -errno;
1596 return i.agp_version_major;
1601 * Get AGP driver minor version number.
1603 * \param fd file descriptor.
1605 * \return minor version number on success, or a negative value on failure.
1608 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1609 * necessary information in a drm_agp_info structure.
1611 int drmAgpVersionMinor(int fd)
1615 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return -errno;
1616 return i.agp_version_minor;
1623 * \param fd file descriptor.
1625 * \return mode on success, or zero on failure.
1628 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1629 * necessary information in a drm_agp_info structure.
1631 unsigned long drmAgpGetMode(int fd)
1635 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return 0;
1641 * Get AGP aperture base.
1643 * \param fd file descriptor.
1645 * \return aperture base on success, zero on failure.
1648 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1649 * necessary information in a drm_agp_info structure.
1651 unsigned long drmAgpBase(int fd)
1655 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return 0;
1656 return i.aperture_base;
1661 * Get AGP aperture size.
1663 * \param fd file descriptor.
1665 * \return aperture size on success, zero on failure.
1668 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1669 * necessary information in a drm_agp_info structure.
1671 unsigned long drmAgpSize(int fd)
1675 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return 0;
1676 return i.aperture_size;
1681 * Get used AGP memory.
1683 * \param fd file descriptor.
1685 * \return memory used on success, or zero on failure.
1688 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1689 * necessary information in a drm_agp_info structure.
1691 unsigned long drmAgpMemoryUsed(int fd)
1695 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return 0;
1696 return i.memory_used;
1701 * Get available AGP memory.
1703 * \param fd file descriptor.
1705 * \return memory available on success, or zero on failure.
1708 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1709 * necessary information in a drm_agp_info structure.
1711 unsigned long drmAgpMemoryAvail(int fd)
1715 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return 0;
1716 return i.memory_allowed;
1721 * Get hardware vendor ID.
1723 * \param fd file descriptor.
1725 * \return vendor ID on success, or zero on failure.
1728 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1729 * necessary information in a drm_agp_info structure.
1731 unsigned int drmAgpVendorId(int fd)
1735 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return 0;
1741 * Get hardware device ID.
1743 * \param fd file descriptor.
1745 * \return zero on success, or zero on failure.
1748 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1749 * necessary information in a drm_agp_info structure.
1751 unsigned int drmAgpDeviceId(int fd)
1755 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return 0;
1759 int drmScatterGatherAlloc(int fd, unsigned long size, drm_handle_t *handle)
1761 drm_scatter_gather_t sg;
1766 if (ioctl(fd, DRM_IOCTL_SG_ALLOC, &sg)) return -errno;
1767 *handle = sg.handle;
1771 int drmScatterGatherFree(int fd, drm_handle_t handle)
1773 drm_scatter_gather_t sg;
1777 if (ioctl(fd, DRM_IOCTL_SG_FREE, &sg)) return -errno;
1784 * \param fd file descriptor.
1785 * \param vbl pointer to a drmVBlank structure.
1787 * \return zero on success, or a negative value on failure.
1790 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
1792 int drmWaitVBlank(int fd, drmVBlankPtr vbl)
1797 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
1798 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
1799 } while (ret && errno == EINTR);
1804 int drmError(int err, const char *label)
1807 case DRM_ERR_NO_DEVICE: fprintf(stderr, "%s: no device\n", label); break;
1808 case DRM_ERR_NO_ACCESS: fprintf(stderr, "%s: no access\n", label); break;
1809 case DRM_ERR_NOT_ROOT: fprintf(stderr, "%s: not root\n", label); break;
1810 case DRM_ERR_INVALID: fprintf(stderr, "%s: invalid args\n", label);break;
1812 if (err < 0) err = -err;
1813 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
1821 * Install IRQ handler.
1823 * \param fd file descriptor.
1824 * \param irq IRQ number.
1826 * \return zero on success, or a negative value on failure.
1829 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
1830 * argument in a drm_control structure.
1832 int drmCtlInstHandler(int fd, int irq)
1836 ctl.func = DRM_INST_HANDLER;
1838 if (ioctl(fd, DRM_IOCTL_CONTROL, &ctl)) return -errno;
1844 * Uninstall IRQ handler.
1846 * \param fd file descriptor.
1848 * \return zero on success, or a negative value on failure.
1851 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
1852 * argument in a drm_control structure.
1854 int drmCtlUninstHandler(int fd)
1858 ctl.func = DRM_UNINST_HANDLER;
1860 if (ioctl(fd, DRM_IOCTL_CONTROL, &ctl)) return -errno;
1864 int drmFinish(int fd, int context, drmLockFlags flags)
1868 lock.context = context;
1870 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1871 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1872 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1873 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1874 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1875 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1876 if (ioctl(fd, DRM_IOCTL_FINISH, &lock)) return -errno;
1881 * Get IRQ from bus ID.
1883 * \param fd file descriptor.
1884 * \param busnum bus number.
1885 * \param devnum device number.
1886 * \param funcnum function number.
1888 * \return IRQ number on success, or a negative value on failure.
1891 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
1892 * arguments in a drm_irq_busid structure.
1894 int drmGetInterruptFromBusID(int fd, int busnum, int devnum, int funcnum)
1900 p.funcnum = funcnum;
1901 if (ioctl(fd, DRM_IOCTL_IRQ_BUSID, &p)) return -errno;
1905 int drmAddContextTag(int fd, drm_context_t context, void *tag)
1907 drmHashEntry *entry = drmGetEntry(fd);
1909 if (drmHashInsert(entry->tagTable, context, tag)) {
1910 drmHashDelete(entry->tagTable, context);
1911 drmHashInsert(entry->tagTable, context, tag);
1916 int drmDelContextTag(int fd, drm_context_t context)
1918 drmHashEntry *entry = drmGetEntry(fd);
1920 return drmHashDelete(entry->tagTable, context);
1923 void *drmGetContextTag(int fd, drm_context_t context)
1925 drmHashEntry *entry = drmGetEntry(fd);
1928 if (drmHashLookup(entry->tagTable, context, &value)) return NULL;
1933 int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
1934 drm_handle_t handle)
1936 drm_ctx_priv_map_t map;
1938 map.ctx_id = ctx_id;
1939 map.handle = (void *)handle;
1941 if (ioctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map)) return -errno;
1945 int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
1946 drm_handle_t *handle)
1948 drm_ctx_priv_map_t map;
1950 map.ctx_id = ctx_id;
1952 if (ioctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map)) return -errno;
1953 if (handle) *handle = (drm_handle_t)map.handle;
1958 int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
1959 drmMapType *type, drmMapFlags *flags, drm_handle_t *handle,
1965 if (ioctl(fd, DRM_IOCTL_GET_MAP, &map)) return -errno;
1966 *offset = map.offset;
1970 *handle = (unsigned long)map.handle;
1975 int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
1976 unsigned long *magic, unsigned long *iocs)
1978 drm_client_t client;
1981 if (ioctl(fd, DRM_IOCTL_GET_CLIENT, &client)) return -errno;
1982 *auth = client.auth;
1985 *magic = client.magic;
1986 *iocs = client.iocs;
1990 int drmGetStats(int fd, drmStatsT *stats)
1995 if (ioctl(fd, DRM_IOCTL_GET_STATS, &s)) return -errno;
1998 memset(stats, 0, sizeof(*stats));
1999 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
2003 stats->data[i].long_format = "%-20.20s"; \
2004 stats->data[i].rate_format = "%8.8s"; \
2005 stats->data[i].isvalue = 1; \
2006 stats->data[i].verbose = 0
2009 stats->data[i].long_format = "%-20.20s"; \
2010 stats->data[i].rate_format = "%5.5s"; \
2011 stats->data[i].isvalue = 0; \
2012 stats->data[i].mult_names = "kgm"; \
2013 stats->data[i].mult = 1000; \
2014 stats->data[i].verbose = 0
2017 stats->data[i].long_format = "%-20.20s"; \
2018 stats->data[i].rate_format = "%5.5s"; \
2019 stats->data[i].isvalue = 0; \
2020 stats->data[i].mult_names = "KGM"; \
2021 stats->data[i].mult = 1024; \
2022 stats->data[i].verbose = 0
2025 stats->count = s.count;
2026 for (i = 0; i < s.count; i++) {
2027 stats->data[i].value = s.data[i].value;
2028 switch (s.data[i].type) {
2029 case _DRM_STAT_LOCK:
2030 stats->data[i].long_name = "Lock";
2031 stats->data[i].rate_name = "Lock";
2034 case _DRM_STAT_OPENS:
2035 stats->data[i].long_name = "Opens";
2036 stats->data[i].rate_name = "O";
2038 stats->data[i].verbose = 1;
2040 case _DRM_STAT_CLOSES:
2041 stats->data[i].long_name = "Closes";
2042 stats->data[i].rate_name = "Lock";
2044 stats->data[i].verbose = 1;
2046 case _DRM_STAT_IOCTLS:
2047 stats->data[i].long_name = "Ioctls";
2048 stats->data[i].rate_name = "Ioc/s";
2051 case _DRM_STAT_LOCKS:
2052 stats->data[i].long_name = "Locks";
2053 stats->data[i].rate_name = "Lck/s";
2056 case _DRM_STAT_UNLOCKS:
2057 stats->data[i].long_name = "Unlocks";
2058 stats->data[i].rate_name = "Unl/s";
2062 stats->data[i].long_name = "IRQs";
2063 stats->data[i].rate_name = "IRQ/s";
2066 case _DRM_STAT_PRIMARY:
2067 stats->data[i].long_name = "Primary Bytes";
2068 stats->data[i].rate_name = "PB/s";
2071 case _DRM_STAT_SECONDARY:
2072 stats->data[i].long_name = "Secondary Bytes";
2073 stats->data[i].rate_name = "SB/s";
2077 stats->data[i].long_name = "DMA";
2078 stats->data[i].rate_name = "DMA/s";
2081 case _DRM_STAT_SPECIAL:
2082 stats->data[i].long_name = "Special DMA";
2083 stats->data[i].rate_name = "dma/s";
2086 case _DRM_STAT_MISSED:
2087 stats->data[i].long_name = "Miss";
2088 stats->data[i].rate_name = "Ms/s";
2091 case _DRM_STAT_VALUE:
2092 stats->data[i].long_name = "Value";
2093 stats->data[i].rate_name = "Value";
2096 case _DRM_STAT_BYTE:
2097 stats->data[i].long_name = "Bytes";
2098 stats->data[i].rate_name = "B/s";
2101 case _DRM_STAT_COUNT:
2103 stats->data[i].long_name = "Count";
2104 stats->data[i].rate_name = "Cnt/s";
2113 * Issue a set-version ioctl.
2115 * \param fd file descriptor.
2116 * \param drmCommandIndex command index
2117 * \param data source pointer of the data to be read and written.
2118 * \param size size of the data to be read and written.
2120 * \return zero on success, or a negative value on failure.
2123 * It issues a read-write ioctl given by
2124 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2126 int drmSetInterfaceVersion(int fd, drmSetVersion *version)
2129 drm_set_version_t sv;
2131 sv.drm_di_major = version->drm_di_major;
2132 sv.drm_di_minor = version->drm_di_minor;
2133 sv.drm_dd_major = version->drm_dd_major;
2134 sv.drm_dd_minor = version->drm_dd_minor;
2136 if (ioctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
2140 version->drm_di_major = sv.drm_di_major;
2141 version->drm_di_minor = sv.drm_di_minor;
2142 version->drm_dd_major = sv.drm_dd_major;
2143 version->drm_dd_minor = sv.drm_dd_minor;
2149 * Send a device-specific command.
2151 * \param fd file descriptor.
2152 * \param drmCommandIndex command index
2154 * \return zero on success, or a negative value on failure.
2157 * It issues a ioctl given by
2158 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2160 int drmCommandNone(int fd, unsigned long drmCommandIndex)
2162 void *data = NULL; /* dummy */
2163 unsigned long request;
2165 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2167 if (ioctl(fd, request, data)) {
2175 * Send a device-specific read command.
2177 * \param fd file descriptor.
2178 * \param drmCommandIndex command index
2179 * \param data destination pointer of the data to be read.
2180 * \param size size of the data to be read.
2182 * \return zero on success, or a negative value on failure.
2185 * It issues a read ioctl given by
2186 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2188 int drmCommandRead(int fd, unsigned long drmCommandIndex, void *data,
2191 unsigned long request;
2193 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2194 DRM_COMMAND_BASE + drmCommandIndex, size);
2196 if (ioctl(fd, request, data)) {
2204 * Send a device-specific write command.
2206 * \param fd file descriptor.
2207 * \param drmCommandIndex command index
2208 * \param data source pointer of the data to be written.
2209 * \param size size of the data to be written.
2211 * \return zero on success, or a negative value on failure.
2214 * It issues a write ioctl given by
2215 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2217 int drmCommandWrite(int fd, unsigned long drmCommandIndex, void *data,
2220 unsigned long request;
2222 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2223 DRM_COMMAND_BASE + drmCommandIndex, size);
2225 if (ioctl(fd, request, data)) {
2233 * Send a device-specific read-write command.
2235 * \param fd file descriptor.
2236 * \param drmCommandIndex command index
2237 * \param data source pointer of the data to be read and written.
2238 * \param size size of the data to be read and written.
2240 * \return zero on success, or a negative value on failure.
2243 * It issues a read-write ioctl given by
2244 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2246 int drmCommandWriteRead(int fd, unsigned long drmCommandIndex, void *data,
2249 unsigned long request;
2251 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2252 DRM_COMMAND_BASE + drmCommandIndex, size);
2254 if (ioctl(fd, request, data)) {
2264 * DRM_FENCE_FLAG_EMIT
2265 * DRM_FENCE_FLAG_SHAREABLE
2266 * DRM_FENCE_MASK_DRIVER
2269 int drmFenceCreate(int fd, unsigned flags, int class,unsigned type,
2272 drm_fence_arg_t arg;
2274 memset(&arg, 0, sizeof(arg));
2277 arg.op = drm_fence_create;
2278 if (ioctl(fd, DRM_IOCTL_FENCE, &arg))
2280 fence->handle = arg.handle;
2281 fence->class = arg.class;
2282 fence->type = arg.type;
2283 fence->flags = arg.flags;
2284 fence->signaled = 0;
2290 * DRM_FENCE_FLAG_SHAREABLE
2291 * DRM_FENCE_MASK_DRIVER
2294 int drmFenceBuffers(int fd, unsigned flags, drmFence *fence)
2296 drm_fence_arg_t arg;
2298 memset(&arg, 0, sizeof(arg));
2300 arg.op = drm_fence_buffers;
2301 if (ioctl(fd, DRM_IOCTL_FENCE, &arg))
2303 fence->handle = arg.handle;
2304 fence->class = arg.class;
2305 fence->type = arg.type;
2306 fence->flags = arg.flags;
2307 fence->signaled = 0;
2311 int drmFenceDestroy(int fd, const drmFence *fence)
2313 drm_fence_arg_t arg;
2315 memset(&arg, 0, sizeof(arg));
2316 arg.handle = fence->handle;
2317 arg.op = drm_fence_destroy;
2318 if (ioctl(fd, DRM_IOCTL_FENCE, &arg))
2323 int drmFenceReference(int fd, unsigned handle, drmFence *fence)
2325 drm_fence_arg_t arg;
2327 memset(&arg, 0, sizeof(arg));
2328 arg.handle = handle;
2329 arg.op = drm_fence_reference;
2330 if (ioctl(fd, DRM_IOCTL_FENCE, &arg))
2332 fence->handle = arg.handle;
2333 fence->class = arg.class;
2334 fence->type = arg.type;
2335 fence->flags = arg.flags;
2336 fence->signaled = arg.signaled;
2340 int drmFenceUnreference(int fd, const drmFence *fence)
2342 drm_fence_arg_t arg;
2344 memset(&arg, 0, sizeof(arg));
2345 arg.handle = fence->handle;
2346 arg.op = drm_fence_unreference;
2347 if (ioctl(fd, DRM_IOCTL_FENCE, &arg))
2352 int drmFenceFlush(int fd, drmFence *fence, unsigned flush_type)
2354 drm_fence_arg_t arg;
2356 memset(&arg, 0, sizeof(arg));
2357 arg.handle = fence->handle;
2358 arg.type = flush_type;
2359 arg.op = drm_fence_flush;
2360 if (ioctl(fd, DRM_IOCTL_FENCE, &arg))
2362 fence->class = arg.class;
2363 fence->type = arg.type;
2364 fence->signaled = arg.signaled;
2368 int drmFenceUpdate(int fd, drmFence *fence)
2370 drm_fence_arg_t arg;
2372 memset(&arg, 0, sizeof(arg));
2373 arg.handle = fence->handle;
2374 arg.op = drm_fence_signaled;
2375 if (ioctl(fd, DRM_IOCTL_FENCE, &arg))
2377 fence->class = arg.class;
2378 fence->type = arg.type;
2379 fence->signaled = arg.signaled;
2383 int drmFenceSignaled(int fd, drmFence *fence, unsigned fenceType,
2389 if ((fence->flags & DRM_FENCE_FLAG_SHAREABLE) ||
2390 ((fenceType & fence->signaled) != fenceType)) {
2392 ret = drmFenceFlush(fd, fence, fenceType);
2397 *signaled = ((fenceType & fence->signaled) == fenceType);
2404 * DRM_FENCE_FLAG_SHAREABLE
2405 * DRM_FENCE_MASK_DRIVER
2409 int drmFenceEmit(int fd, unsigned flags, drmFence *fence, unsigned emit_type)
2411 drm_fence_arg_t arg;
2413 memset(&arg, 0, sizeof(arg));
2415 arg.handle = fence->handle;
2416 arg.type = emit_type;
2417 arg.op = drm_fence_emit;
2418 if (ioctl(fd, DRM_IOCTL_FENCE, &arg))
2420 fence->class = arg.class;
2421 fence->type = arg.type;
2422 fence->signaled = arg.signaled;
2428 * DRM_FENCE_FLAG_WAIT_LAZY
2429 * DRM_FENCE_FLAG_WAIT_IGNORE_SIGNALS
2432 int drmFenceWait(int fd, unsigned flags, drmFence *fence, unsigned flush_type)
2434 drm_fence_arg_t arg;
2437 if (flush_type == 0) {
2438 flush_type = fence->type;
2441 if (!(fence->flags & DRM_FENCE_FLAG_SHAREABLE)) {
2442 if ((flush_type & fence->signaled) == flush_type) {
2447 memset(&arg, 0, sizeof(arg));
2448 arg.handle = fence->handle;
2449 arg.type = flush_type;
2451 arg.op = drm_fence_wait;
2453 ret = ioctl(fd, DRM_IOCTL_FENCE, &arg);
2454 } while (ret != 0 && errno == EAGAIN);
2459 fence->class = arg.class;
2460 fence->type = arg.type;
2461 fence->signaled = arg.signaled;
2465 static int drmAdjustListNodes(drmBOList *list)
2471 while(list->numCurrent < list->numTarget) {
2472 node = (drmBONode *) malloc(sizeof(*node));
2478 DRMLISTADD(&node->head, &list->free);
2481 while(list->numCurrent > list->numTarget) {
2482 l = list->free.next;
2483 if (l == &list->free)
2486 node = DRMLISTENTRY(drmBONode, l, head);
2493 void drmBOFreeList(drmBOList *list)
2499 l = list->list.next;
2500 while(l != &list->list) {
2502 node = DRMLISTENTRY(drmBONode, l, head);
2504 l = list->free.next;
2509 l = list->free.next;
2510 while(l != &list->free) {
2512 node = DRMLISTENTRY(drmBONode, l, head);
2514 l = list->free.next;
2519 int drmBOResetList(drmBOList *list) {
2524 ret = drmAdjustListNodes(list);
2528 l = list->list.next;
2529 while(l != &list->list) {
2531 DRMLISTADD(l, &list->free);
2533 l = list->list.next;
2535 return drmAdjustListNodes(list);
2538 static drmBONode *drmAddListItem(drmBOList *list, drmBO *item,
2545 l = list->free.next;
2546 if (l == &list->free) {
2547 node = (drmBONode *) malloc(sizeof(*node));
2554 node = DRMLISTENTRY(drmBONode, l, head);
2559 DRMLISTADD(&node->head, &list->list);
2564 void *drmBOListIterator(drmBOList *list)
2566 void *ret = list->list.next;
2568 if (ret == &list->list)
2573 void *drmBOListNext(drmBOList *list, void *iterator)
2577 drmMMListHead *l = (drmMMListHead *) iterator;
2579 if (ret == &list->list)
2584 drmBO *drmBOListBuf(void *iterator)
2587 drmMMListHead *l = (drmMMListHead *) iterator;
2588 node = DRMLISTENTRY(drmBONode, l, head);
2594 int drmBOCreateList(int numTarget, drmBOList *list)
2596 DRMINITLISTHEAD(&list->list);
2597 DRMINITLISTHEAD(&list->free);
2598 list->numTarget = numTarget;
2599 list->numCurrent = 0;
2600 list->numOnList = 0;
2601 return drmAdjustListNodes(list);
2604 static void drmBOCopyReply(const drm_bo_arg_reply_t *rep,
2607 buf->handle = rep->handle;
2608 buf->flags = rep->flags;
2609 buf->size = rep->size;
2610 buf->offset = rep->offset;
2611 buf->mapHandle = rep->arg_handle;
2612 buf->mask = rep->mask;
2613 buf->start = rep->buffer_start;
2614 buf->fenceFlags = rep->fence_flags;
2615 buf->replyFlags = rep->rep_flags;
2620 int drmBOCreate(int fd, void *ttm, unsigned long start, unsigned long size,
2621 void *user_buffer, drm_bo_type_t type, unsigned mask,
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));
2636 buf->virtual = NULL;
2639 case drm_bo_type_dc:
2640 req->buffer_start = start;
2642 case drm_bo_type_user:
2643 req->buffer_start = (unsigned long) user_buffer;
2644 buf->virtual = user_buffer;
2646 case drm_bo_type_fake:
2647 req->buffer_start = start;
2652 req->op = drm_bo_create;
2655 ret = ioctl(fd, DRM_IOCTL_BUFOBJ, &arg);
2656 } while (ret != 0 && errno == EAGAIN);
2664 fprintf(stderr, "Error %d\n", rep->ret);
2668 drmBOCopyReply(rep, buf);
2669 buf->mapVirtual = NULL;
2675 int drmBODestroy(int fd, drmBO *buf)
2678 drm_bo_arg_request_t *req = &arg.d.req;
2679 drm_bo_arg_reply_t *rep = &arg.d.rep;
2681 if (buf->mapVirtual && (buf->type != drm_bo_type_fake)) {
2682 (void) drmUnmap(buf->mapVirtual, buf->start + buf->size);
2683 buf->mapVirtual = NULL;
2684 buf->virtual = NULL;
2687 memset(&arg, 0, sizeof(arg));
2688 req->handle = buf->handle;
2689 req->op = drm_bo_destroy;
2691 if (ioctl(fd, DRM_IOCTL_BUFOBJ, &arg))
2704 int drmBOReference(int fd, unsigned handle, drmBO *buf)
2708 drm_bo_arg_request_t *req = &arg.d.req;
2709 drm_bo_arg_reply_t *rep = &arg.d.rep;
2711 memset(&arg, 0, sizeof(arg));
2712 req->handle = handle;
2713 req->op = drm_bo_reference;
2715 if (ioctl(fd, DRM_IOCTL_BUFOBJ, &arg))
2724 drmBOCopyReply(rep, buf);
2725 buf->type = drm_bo_type_dc;
2726 buf->mapVirtual = NULL;
2728 buf->virtual = NULL;
2733 int drmBOUnReference(int fd, drmBO *buf)
2736 drm_bo_arg_request_t *req = &arg.d.req;
2737 drm_bo_arg_reply_t *rep = &arg.d.rep;
2740 if (buf->mapVirtual && (buf->type != drm_bo_type_fake)) {
2741 (void) munmap(buf->mapVirtual, buf->start + buf->size);
2742 buf->mapVirtual = NULL;
2743 buf->virtual = NULL;
2746 memset(&arg, 0, sizeof(arg));
2747 req->handle = buf->handle;
2748 req->op = drm_bo_unreference;
2750 if (ioctl(fd, DRM_IOCTL_BUFOBJ, &arg))
2764 * Flags can be DRM_BO_FLAG_READ, DRM_BO_FLAG_WRITE or'ed together
2765 * Hint currently be DRM_BO_HINT_DONT_BLOCK, which makes the
2766 * call return an -EBUSY if it can' immediately honor the mapping request.
2769 int drmBOMap(int fd, drmBO *buf, unsigned mapFlags, unsigned mapHint,
2774 drm_bo_arg_request_t *req = &arg.d.req;
2775 drm_bo_arg_reply_t *rep = &arg.d.rep;
2779 * Make sure we have a virtual address of the buffer.
2782 if (!buf->virtual && buf->type != drm_bo_type_fake) {
2784 virtual = mmap(0, buf->size + buf->start,
2785 PROT_READ | PROT_WRITE, MAP_SHARED,
2786 fd, buf->mapHandle);
2787 if (virtual == MAP_FAILED) {
2792 buf->mapVirtual = virtual;
2793 buf->virtual = ((char *) virtual) + buf->start;
2796 memset(&arg, 0, sizeof(arg));
2797 req->handle = buf->handle;
2798 req->mask = mapFlags;
2799 req->hint = mapHint;
2800 req->op = drm_bo_map;
2803 * May hang if the buffer object is busy.
2804 * This IOCTL synchronizes the buffer.
2808 ret = ioctl(fd, DRM_IOCTL_BUFOBJ, &arg);
2809 } while (ret != 0 && errno == EAGAIN);
2818 drmBOCopyReply(rep, buf);
2819 buf->mapFlags = mapFlags;
2821 *address = buf->virtual;
2826 int drmBOUnmap(int fd, drmBO *buf)
2829 drm_bo_arg_request_t *req = &arg.d.req;
2830 drm_bo_arg_reply_t *rep = &arg.d.rep;
2833 memset(&arg, 0, sizeof(arg));
2834 req->handle = buf->handle;
2835 req->op = drm_bo_unmap;
2837 if (ioctl(fd, DRM_IOCTL_BUFOBJ, &arg)) {
2848 int drmBOValidate(int fd, drmBO *buf, unsigned flags, unsigned mask,
2852 drm_bo_arg_request_t *req = &arg.d.req;
2853 drm_bo_arg_reply_t *rep = &arg.d.rep;
2856 memset(&arg, 0, sizeof(arg));
2857 req->handle = buf->handle;
2860 req->arg_handle = mask; /* Encode mask in the arg_handle field :/ */
2861 req->op = drm_bo_validate;
2864 ret = ioctl(fd, DRM_IOCTL_BUFOBJ, &arg);
2865 } while (ret && errno == EAGAIN);
2874 drmBOCopyReply(rep, buf);
2879 int drmBOFence(int fd, drmBO *buf, unsigned flags, unsigned fenceHandle)
2882 drm_bo_arg_request_t *req = &arg.d.req;
2883 drm_bo_arg_reply_t *rep = &arg.d.rep;
2886 memset(&arg, 0, sizeof(arg));
2887 req->handle = buf->handle;
2889 req->arg_handle = fenceHandle;
2890 req->op = drm_bo_validate;
2892 ret = ioctl(fd, DRM_IOCTL_BUFOBJ, &arg);
2903 int drmBOInfo(int fd, drmBO *buf)
2906 drm_bo_arg_request_t *req = &arg.d.req;
2907 drm_bo_arg_reply_t *rep = &arg.d.rep;
2910 memset(&arg, 0, sizeof(arg));
2911 req->handle = buf->handle;
2912 req->op = drm_bo_info;
2914 ret = ioctl(fd, DRM_IOCTL_BUFOBJ, &arg);
2922 drmBOCopyReply(rep, buf);
2926 int drmBOWaitIdle(int fd, drmBO *buf, unsigned hint)
2929 drm_bo_arg_request_t *req = &arg.d.req;
2930 drm_bo_arg_reply_t *rep = &arg.d.rep;
2933 if ((buf->flags & DRM_BO_FLAG_SHAREABLE) ||
2934 (buf->replyFlags & DRM_BO_REP_BUSY)) {
2935 memset(&arg, 0, sizeof(arg));
2936 req->handle = buf->handle;
2937 req->op = drm_bo_wait_idle;
2941 ret = ioctl(fd, DRM_IOCTL_BUFOBJ, &arg);
2942 } while (ret && errno == EAGAIN);
2950 drmBOCopyReply(rep, buf);
2955 int drmBOBusy(int fd, drmBO *buf, int *busy)
2957 if (!(buf->flags & DRM_BO_FLAG_SHAREABLE) &&
2958 !(buf->replyFlags & DRM_BO_REP_BUSY)) {
2962 int ret = drmBOInfo(fd, buf);
2965 *busy = (buf->replyFlags & DRM_BO_REP_BUSY);
2971 int drmAddValidateItem(drmBOList *list, drmBO *buf, unsigned flags,
2975 drmBONode *node, *cur;
2976 unsigned oldFlags, newFlags;
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, 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);