Git init
authorKibum Kim <kb0929.kim@samsung.com>
Fri, 6 Jan 2012 15:47:42 +0000 (00:47 +0900)
committerKibum Kim <kb0929.kim@samsung.com>
Fri, 6 Jan 2012 15:47:42 +0000 (00:47 +0900)
162 files changed:
AUTHORS [new file with mode: 0644]
COPYING [new file with mode: 0644]
ChangeLog [new file with mode: 0644]
Makefile.am [new file with mode: 0644]
README [new file with mode: 0644]
RELEASING [new file with mode: 0644]
autogen.sh [new file with mode: 0755]
configure.ac [new file with mode: 0644]
debian/README.source [new file with mode: 0644]
debian/changelog [new file with mode: 0644]
debian/compat [new file with mode: 0644]
debian/control [new file with mode: 0644]
debian/copyright [new file with mode: 0644]
debian/libdrm-dev.install [new file with mode: 0644]
debian/libdrm-dev.links [new file with mode: 0644]
debian/libdrm-intel1.install [new file with mode: 0644]
debian/libdrm-nouveau1.install [new file with mode: 0644]
debian/libdrm-radeon1.install [new file with mode: 0644]
debian/libdrm-slp1.install [new file with mode: 0644]
debian/libdrm2.install [new file with mode: 0644]
debian/libkms1.install [new file with mode: 0644]
debian/rules [new file with mode: 0755]
debian/watch [new file with mode: 0644]
debian/xsfbs/repack.sh [new file with mode: 0644]
debian/xsfbs/xsfbs.mk [new file with mode: 0644]
debian/xsfbs/xsfbs.sh [new file with mode: 0644]
include/Makefile.am [new file with mode: 0644]
include/drm/Makefile.am [new file with mode: 0755]
include/drm/drm.h [new file with mode: 0644]
include/drm/drm_mode.h [new file with mode: 0755]
include/drm/drm_sarea.h [new file with mode: 0644]
include/drm/exynos_drm.h [new file with mode: 0644]
include/drm/i810_drm.h [new file with mode: 0644]
include/drm/i830_drm.h [new file with mode: 0644]
include/drm/i915_drm.h [new file with mode: 0644]
include/drm/mach64_drm.h [new file with mode: 0644]
include/drm/mga_drm.h [new file with mode: 0644]
include/drm/nouveau_drm.h [new file with mode: 0644]
include/drm/r128_drm.h [new file with mode: 0644]
include/drm/radeon_drm.h [new file with mode: 0644]
include/drm/savage_drm.h [new file with mode: 0644]
include/drm/sis_drm.h [new file with mode: 0644]
include/drm/via_drm.h [new file with mode: 0644]
include/drm/vmwgfx_drm.h [new file with mode: 0644]
intel/Makefile.am [new file with mode: 0644]
intel/intel_bufmgr.c [new file with mode: 0644]
intel/intel_bufmgr.h [new file with mode: 0644]
intel/intel_bufmgr_fake.c [new file with mode: 0644]
intel/intel_bufmgr_gem.c [new file with mode: 0644]
intel/intel_bufmgr_priv.h [new file with mode: 0644]
intel/intel_chipset.h [new file with mode: 0644]
intel/intel_debug.h [new file with mode: 0644]
intel/libdrm_intel.pc.in [new file with mode: 0644]
intel/mm.c [new file with mode: 0644]
intel/mm.h [new file with mode: 0644]
libdrm.pc.in [new file with mode: 0644]
libdrm_lists.h [new file with mode: 0644]
libkms/Makefile.am [new file with mode: 0644]
libkms/api.c [new file with mode: 0644]
libkms/dumb.c [new file with mode: 0755]
libkms/intel.c [new file with mode: 0644]
libkms/internal.h [new file with mode: 0644]
libkms/libkms.h [new file with mode: 0644]
libkms/libkms.pc.in [new file with mode: 0644]
libkms/linux.c [new file with mode: 0644]
libkms/nouveau.c [new file with mode: 0644]
libkms/radeon.c [new file with mode: 0644]
libkms/slp.c [new file with mode: 0644]
libkms/vmwgfx.c [new file with mode: 0644]
m4/.gitignore [new file with mode: 0644]
nouveau/Makefile.am [new file with mode: 0644]
nouveau/libdrm_nouveau.pc.in [new file with mode: 0644]
nouveau/nouveau_bo.c [new file with mode: 0644]
nouveau/nouveau_bo.h [new file with mode: 0644]
nouveau/nouveau_channel.c [new file with mode: 0644]
nouveau/nouveau_channel.h [new file with mode: 0644]
nouveau/nouveau_device.c [new file with mode: 0644]
nouveau/nouveau_device.h [new file with mode: 0644]
nouveau/nouveau_drmif.h [new file with mode: 0644]
nouveau/nouveau_grobj.c [new file with mode: 0644]
nouveau/nouveau_grobj.h [new file with mode: 0644]
nouveau/nouveau_notifier.c [new file with mode: 0644]
nouveau/nouveau_notifier.h [new file with mode: 0644]
nouveau/nouveau_private.h [new file with mode: 0644]
nouveau/nouveau_pushbuf.c [new file with mode: 0644]
nouveau/nouveau_pushbuf.h [new file with mode: 0644]
nouveau/nouveau_reloc.c [new file with mode: 0644]
nouveau/nouveau_reloc.h [new file with mode: 0644]
nouveau/nouveau_resource.c [new file with mode: 0644]
nouveau/nouveau_resource.h [new file with mode: 0644]
nouveau/nv04_pushbuf.h [new file with mode: 0644]
nouveau/nvc0_pushbuf.h [new file with mode: 0644]
radeon/Makefile.am [new file with mode: 0644]
radeon/bof.c [new file with mode: 0644]
radeon/bof.h [new file with mode: 0644]
radeon/libdrm_radeon.pc.in [new file with mode: 0644]
radeon/radeon_bo.c [new file with mode: 0644]
radeon/radeon_bo.h [new file with mode: 0644]
radeon/radeon_bo_gem.c [new file with mode: 0644]
radeon/radeon_bo_gem.h [new file with mode: 0644]
radeon/radeon_bo_int.h [new file with mode: 0644]
radeon/radeon_cs.c [new file with mode: 0644]
radeon/radeon_cs.h [new file with mode: 0644]
radeon/radeon_cs_gem.c [new file with mode: 0644]
radeon/radeon_cs_gem.h [new file with mode: 0644]
radeon/radeon_cs_int.h [new file with mode: 0644]
radeon/radeon_cs_space.c [new file with mode: 0644]
slp/Makefile.am [new file with mode: 0644]
slp/drm_slp_bufmgr.c [new file with mode: 0755]
slp/drm_slp_bufmgr.h [new file with mode: 0755]
slp/libdrm_slp.pc.in [new file with mode: 0644]
tests/Makefile.am [new file with mode: 0644]
tests/auth.c [new file with mode: 0644]
tests/dristat.c [new file with mode: 0644]
tests/drmstat.c [new file with mode: 0644]
tests/drmtest.c [new file with mode: 0644]
tests/drmtest.h [new file with mode: 0644]
tests/gem_basic.c [new file with mode: 0644]
tests/gem_flink.c [new file with mode: 0644]
tests/gem_mmap.c [new file with mode: 0644]
tests/gem_readwrite.c [new file with mode: 0644]
tests/gemtest/Makefile.am [new file with mode: 0644]
tests/gemtest/gemtest.c [new file with mode: 0644]
tests/getclient.c [new file with mode: 0644]
tests/getstats.c [new file with mode: 0644]
tests/getversion.c [new file with mode: 0644]
tests/kmstest/Makefile.am [new file with mode: 0644]
tests/kmstest/main.c [new file with mode: 0644]
tests/lock.c [new file with mode: 0644]
tests/modeprint/Makefile.am [new file with mode: 0644]
tests/modeprint/modeprint.c [new file with mode: 0644]
tests/modetest/Makefile.am [new file with mode: 0644]
tests/modetest/modetest.c [new file with mode: 0755]
tests/name_from_fd.c [new file with mode: 0644]
tests/openclose.c [new file with mode: 0644]
tests/planetest/Makefile.am [new file with mode: 0644]
tests/planetest/planetest.c [new file with mode: 0644]
tests/setversion.c [new file with mode: 0644]
tests/ttmtest/AUTHORS [new file with mode: 0644]
tests/ttmtest/ChangeLog [new file with mode: 0644]
tests/ttmtest/Makefile.am [new file with mode: 0644]
tests/ttmtest/NEWS [new file with mode: 0644]
tests/ttmtest/README [new file with mode: 0644]
tests/ttmtest/configure.ac [new file with mode: 0644]
tests/ttmtest/reconf [new file with mode: 0755]
tests/ttmtest/src/Makefile.am [new file with mode: 0644]
tests/ttmtest/src/ttmtest.c [new file with mode: 0644]
tests/ttmtest/src/xf86dri.c [new file with mode: 0644]
tests/ttmtest/src/xf86dri.h [new file with mode: 0644]
tests/ttmtest/src/xf86dristr.h [new file with mode: 0644]
tests/updatedraw.c [new file with mode: 0644]
tests/vbltest/Makefile.am [new file with mode: 0644]
tests/vbltest/vbltest.c [new file with mode: 0644]
xf86atomic.h [new file with mode: 0644]
xf86drm.c [new file with mode: 0644]
xf86drm.h [new file with mode: 0644]
xf86drmHash.c [new file with mode: 0644]
xf86drmMode.c [new file with mode: 0644]
xf86drmMode.h [new file with mode: 0644]
xf86drmRandom.c [new file with mode: 0644]
xf86drmSL.c [new file with mode: 0644]
xf86mm.h [new file with mode: 0644]

diff --git a/AUTHORS b/AUTHORS
new file mode 100644 (file)
index 0000000..55581aa
--- /dev/null
+++ b/AUTHORS
@@ -0,0 +1,5 @@
+Rickard E. (Rik) Faith <faith@valinux.com>
+Kevin E. Martin <martin@valinux.com>
+SangJin Lee <lsj119@samsung.com>
+SooChan Lim <sc1.lim@samsung.com>
+Boram Park <boram1288.park@samsung.com>
diff --git a/COPYING b/COPYING
new file mode 100644 (file)
index 0000000..8d597e5
--- /dev/null
+++ b/COPYING
@@ -0,0 +1,18 @@
+Copyright (C) 2000 - 2011 Samsung Electronics co., Ltd. All Rights Reserved.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is fur-
+nished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FIT-
+NESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
+XFREE86 PROJECT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CON-
+NECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/ChangeLog b/ChangeLog
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/Makefile.am b/Makefile.am
new file mode 100644 (file)
index 0000000..3846d3a
--- /dev/null
@@ -0,0 +1,76 @@
+#  Copyright 2005 Adam Jackson.
+#
+#  Permission is hereby granted, free of charge, to any person obtaining a
+#  copy of this software and associated documentation files (the "Software"),
+#  to deal in the Software without restriction, including without limitation
+#  on the rights to use, copy, modify, merge, publish, distribute, sub
+#  license, and/or sell copies of the Software, and to permit persons to whom
+#  the Software is furnished to do so, subject to the following conditions:
+#
+#  The above copyright notice and this permission notice (including the next
+#  paragraph) shall be included in all copies or substantial portions of the
+#  Software.
+#
+#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+#  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+#  FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
+#  ADAM JACKSON BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+#  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+#  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+ACLOCAL_AMFLAGS = -I m4 ${ACLOCAL_FLAGS}
+
+DISTCHECK_CONFIGURE_FLAGS = --enable-nouveau-experimental-api --enable-radeon-experimental-api
+
+pkgconfigdir = @pkgconfigdir@
+pkgconfig_DATA = libdrm.pc
+
+if HAVE_LIBKMS
+LIBKMS_SUBDIR = libkms
+endif
+
+if HAVE_INTEL
+INTEL_SUBDIR = intel
+endif
+
+if HAVE_NOUVEAU
+NOUVEAU_SUBDIR = nouveau
+endif
+
+if HAVE_RADEON
+RADEON_SUBDIR = radeon
+endif
+
+if HAVE_SLP
+SLP_SUBDIR = slp
+endif
+
+SUBDIRS = . $(LIBKMS_SUBDIR) $(INTEL_SUBDIR) $(NOUVEAU_SUBDIR) $(RADEON_SUBDIR) $(SLP_SUBDIR) tests include
+
+libdrm_la_LTLIBRARIES = libdrm.la
+libdrm_ladir = $(libdir)
+libdrm_la_LDFLAGS = -version-number 2:4:0 -no-undefined
+libdrm_la_LIBADD = @CLOCK_LIB@
+
+libdrm_la_CPPFLAGS = -I$(top_srcdir)/include/drm
+
+libdrm_la_SOURCES =                            \
+       xf86drm.c                               \
+       xf86drmHash.c                           \
+       xf86drmRandom.c                         \
+       xf86drmSL.c                             \
+       xf86drmMode.c                           \
+       xf86atomic.h                            \
+       libdrm_lists.h
+
+libdrmincludedir = ${includedir}
+libdrminclude_HEADERS = xf86drm.h xf86drmMode.h
+
+EXTRA_DIST = libdrm.pc.in include/drm/*
+
+copy-headers :
+       cp -r $(kernel_source)/usr/include/drm $(top_srcdir)/include
+
+commit-headers : copy-headers
+       git add include
+       git commit -am "Copy headers from kernel $$(GIT_DIR=$(kernel_source)/.git git describe)"
diff --git a/README b/README
new file mode 100644 (file)
index 0000000..603a1c1
--- /dev/null
+++ b/README
@@ -0,0 +1,41 @@
+libdrm - userspace library for drm
+
+This  is libdrm,  a userspace  library for  accessing the  DRM, direct
+rendering  manager, on  Linux,  BSD and  other  operating systes  that
+support the  ioctl interface.  The library  provides wrapper functions
+for the  ioctls to avoid  exposing the kernel interface  directly, and
+for chipsets with drm memory manager, support for tracking relocations
+and  buffers.   libdrm  is  a  low-level library,  typically  used  by
+graphics drivers  such as the Mesa  DRI drivers, the  X drivers, libva
+and  similar projects.  New  functionality in  the kernel  DRM drivers
+typically requires  a new  libdrm, but a  new libdrm will  always work
+with an older kernel.
+
+
+Compiling
+---------
+
+libdrm  is  a  standard  autotools  packages and  follows  the  normal
+configure, build  and install steps.   The first step is  to configure
+the package, which is done by running the configure shell script:
+
+       ./configure
+
+By default, libdrm  will install into the /usr/local/  prefix.  If you
+want  to  install   this  DRM  to  replace  your   system  copy,  pass
+--prefix=/usr and  --exec-prefix=/ to configure.  If  you are building
+libdrm  from a  git checkout,  you first  need to  run  the autogen.sh
+script.  You can  pass any options to autogen.sh  that you would other
+wise  pass to configure,  or you  can just  re-run configure  with the
+options you need once autogen.sh finishes.
+
+Next step is to build libdrm:
+
+       make
+
+and once make finishes successfully, install the package using
+
+       make install
+
+If you are install into a system location, you will need to be root to
+perform the install step.
diff --git a/RELEASING b/RELEASING
new file mode 100644 (file)
index 0000000..3f07146
--- /dev/null
+++ b/RELEASING
@@ -0,0 +1,66 @@
+The release criteria for libdrm is essentially "if you need a release,
+make one".  There is no designated release engineer or maintainer.
+Anybody is free to make a release if there's a certain feature or bug
+fix they need in a released version of libdrm.
+
+When new ioctl definitions are merged into drm-next, we will add
+support to libdrm, at which point we typically create a new release.
+However, this is up to whoever is driving the feature in question.
+
+Follow these steps to release a new version of libdrm:
+
+  1) Ensure that there are no local, uncommitted/unpushed
+     modifications. You're probably in a good state if both "git diff
+     HEAD" and "git log master..origin/master" give no output.
+
+  3) Bump the version number in configure.ac. We seem to have settled
+     for 2.4.x as the versioning scheme for libdrm, so just bump the
+     micro version.
+
+  4) Run autoconf and then re-run ./configure so the build system
+     picks up the new version number.
+
+  5) Verify that the code passes "make distcheck".  libdrm is tricky
+     to distcheck since the test suite will need to become drm master.
+     This means that you need to run it outside X, that is, in text
+     mode (KMS or no KMS doesn't matter).
+
+     Running "make distcheck" should result in no warnings or errors
+     and end with a message of the form:
+
+       =============================================
+       libdrm-X.Y.Z archives ready for distribution:
+       libdrm-X.Y.Z.tar.gz
+       libdrm-X.Y.Z.tar.bz2
+       =============================================
+
+     Make sure that the version number reported by distcheck and in
+     the tarball names matches the number you bumped to in configure.ac.
+
+  6) Commit the configure.ac change and make an annotated tag for that
+     commit with the version number of the release as the name and a
+     message of "libdrm X.Y.Z".  For example, for the 2.4.16 release
+     the command is:
+
+       git tag -a 2.4.16 -m "libdrm 2.4.16"
+
+  7) Push the commit and tag by saying
+
+       git push --tags origin master
+
+     assuming the remote for the upstream libdrm repo is called origin.
+
+  6) Use the release.sh script from the xorg/util/modular repo to
+     upload the tarballs to the freedesktop.org download area and
+     create an annouce email template.  The script takes three
+     arguments: a "section", the previous tag and the new tag we just
+     created.  For 2.4.16 again, the command is:
+
+       ../modular/release.sh libdrm 2.4.15 2.4.16
+
+     This copies the two tarballs to freedesktop.org and creates
+     libdrm-2.4.16.announce which has a detailed summary of the
+     changes, links to the tarballs, MD5 and SHA1 sums and pre-filled
+     out email headers.  Fill out the blank between the email headers
+     and the list of changes with a brief message of what changed or
+     what prompted this release.  Send out the email and you're done!
diff --git a/autogen.sh b/autogen.sh
new file mode 100755 (executable)
index 0000000..30d679f
--- /dev/null
@@ -0,0 +1,6 @@
+#! /bin/sh
+
+test -n "$srcdir" || srcdir=`dirname "$0"`
+test -n "$srcdir" || srcdir=.
+autoreconf --force --install --verbose "$srcdir"
+test -n "$NOCONFIGURE" || "$srcdir/configure" "$@"
diff --git a/configure.ac b/configure.ac
new file mode 100644 (file)
index 0000000..6f6e5c1
--- /dev/null
@@ -0,0 +1,323 @@
+#  Copyright 2005 Adam Jackson.
+#
+#  Permission is hereby granted, free of charge, to any person obtaining a
+#  copy of this software and associated documentation files (the "Software"),
+#  to deal in the Software without restriction, including without limitation
+#  on the rights to use, copy, modify, merge, publish, distribute, sub
+#  license, and/or sell copies of the Software, and to permit persons to whom
+#  the Software is furnished to do so, subject to the following conditions:
+#
+#  The above copyright notice and this permission notice (including the next
+#  paragraph) shall be included in all copies or substantial portions of the
+#  Software.
+#
+#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+#  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+#  FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
+#  ADAM JACKSON BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+#  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+#  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+AC_PREREQ([2.63])
+AC_INIT([libdrm],
+        [2.4.27],
+        [https://bugs.freedesktop.org/enter_bug.cgi?product=DRI],
+        [libdrm])
+
+AC_CONFIG_HEADERS([config.h])
+AC_CONFIG_SRCDIR([Makefile.am])
+AC_CONFIG_MACRO_DIR([m4])
+AC_CONFIG_AUX_DIR([build-aux])
+
+AM_INIT_AUTOMAKE([1.10 foreign dist-bzip2])
+AM_MAINTAINER_MODE([enable])
+
+# Enable quiet compiles on automake 1.11.
+m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])])
+
+# Check for programs
+AC_PROG_CC
+
+AC_USE_SYSTEM_EXTENSIONS
+AC_SYS_LARGEFILE
+AC_FUNC_ALLOCA
+
+# Initialize libtool
+LT_PREREQ([2.2])
+LT_INIT([disable-static])
+
+
+PKG_CHECK_MODULES(PTHREADSTUBS, pthread-stubs)
+AC_SUBST(PTHREADSTUBS_CFLAGS)
+AC_SUBST(PTHREADSTUBS_LIBS)
+
+PKG_CHECK_MODULES(PCIACCESS, [pciaccess >= 0.10])
+AC_SUBST(PCIACCESS_CFLAGS)
+AC_SUBST(PCIACCESS_LIBS)
+
+pkgconfigdir=${libdir}/pkgconfig
+AC_SUBST(pkgconfigdir)
+AC_ARG_ENABLE([udev],
+              [AS_HELP_STRING([--enable-udev],
+                              [Enable support for using udev instead of mknod (default: disabled)])],
+              [UDEV=$enableval], [UDEV=no])
+
+AC_ARG_ENABLE(libkms,
+             AS_HELP_STRING([--disable-libkms],
+             [Disable KMS mm abstraction library (default: auto)]),
+             [LIBKMS=$enableval], [LIBKMS=auto])
+
+AC_ARG_ENABLE(intel,
+             AS_HELP_STRING([--disable-intel],
+             [Enable support for intel's KMS API (default: auto)]),
+             [INTEL=$enableval], [INTEL=auto])
+
+AC_ARG_ENABLE(radeon,
+             AS_HELP_STRING([--disable-radeon],
+             [Enable support for radeon's KMS API (default: auto)]),
+             [RADEON=$enableval], [RADEON=auto])
+
+AC_ARG_ENABLE(vmwgfx-experimental-api,
+             AS_HELP_STRING([--enable-vmwgfx-experimental-api],
+             [Install vmwgfx's experimental kernel API header (default: disabled)]),
+             [VMWGFX=$enableval], [VMWGFX=no])
+
+AC_ARG_ENABLE(nouveau-experimental-api,
+             AS_HELP_STRING([--enable-nouveau-experimental-api],
+             [Enable support for nouveau's experimental API (default: disabled)]),
+             [NOUVEAU=$enableval], [NOUVEAU=no])
+
+AC_ARG_ENABLE(slp,
+             AS_HELP_STRING([--disable-slp],
+             [Enable support for slp's API (default: auto)]),
+             [SLP=$enableval], [SLP=auto])
+
+AC_ARG_VAR([bufmgr_dir], [Directory of slp-bufmgr])
+
+if test "x$bufmgr_dir" = xyes; then
+  AC_DEFINE_UNQUOTED(BUFMGR_DIR, "$bufmgr_dir", [Directory for the modules of slp_bufmgr])
+else
+  AC_DEFINE(BUFMGR_DIR, "/usr/lib/bufmgr", [Directory for the modules of slp_bufmgr])
+fi
+
+dnl ===========================================================================
+dnl check compiler flags
+AC_DEFUN([LIBDRM_CC_TRY_FLAG], [
+  AC_MSG_CHECKING([whether $CC supports $1])
+
+  libdrm_save_CFLAGS="$CFLAGS"
+  CFLAGS="$CFLAGS $1"
+
+  AC_COMPILE_IFELSE([ ], [libdrm_cc_flag=yes], [libdrm_cc_flag=no])
+  CFLAGS="$libdrm_save_CFLAGS"
+
+  if test "x$libdrm_cc_flag" = "xyes"; then
+    ifelse([$2], , :, [$2])
+  else
+    ifelse([$3], , :, [$3])
+  fi
+  AC_MSG_RESULT([$libdrm_cc_flag])
+])
+
+dnl We use clock_gettime to check for timeouts in drmWaitVBlank
+
+AC_CHECK_FUNCS([clock_gettime], [CLOCK_LIB=],
+               [AC_CHECK_LIB([rt], [clock_gettime], [CLOCK_LIB=-lrt],
+                             [AC_MSG_ERROR([Couldn't find clock_gettime])])])
+AC_SUBST([CLOCK_LIB])
+
+dnl Use lots of warning flags with with gcc and compatible compilers
+
+dnl Note: if you change the following variable, the cache is automatically
+dnl skipped and all flags rechecked.  So there's no need to do anything
+dnl else.  If for any reason you need to force a recheck, just change
+dnl MAYBE_WARN in an ignorable way (like adding whitespace)
+
+MAYBE_WARN="-Wall -Wextra \
+-Wsign-compare -Werror-implicit-function-declaration \
+-Wpointer-arith -Wwrite-strings -Wstrict-prototypes \
+-Wmissing-prototypes -Wmissing-declarations -Wnested-externs \
+-Wpacked -Wswitch-enum -Wmissing-format-attribute \
+-Wstrict-aliasing=2 -Winit-self -Wunsafe-loop-optimizations \
+-Wdeclaration-after-statement -Wold-style-definition \
+-Wno-missing-field-initializers -Wno-unused-parameter \
+-Wno-attributes -Wno-long-long -Winline"
+
+# invalidate cached value if MAYBE_WARN has changed
+if test "x$libdrm_cv_warn_maybe" != "x$MAYBE_WARN"; then
+       unset libdrm_cv_warn_cflags
+fi
+AC_CACHE_CHECK([for supported warning flags], libdrm_cv_warn_cflags, [
+       echo
+       WARN_CFLAGS=""
+
+       # Some warning options are not supported by all versions of
+       # gcc, so test all desired options against the current
+       # compiler.
+       #
+       # Note that there are some order dependencies
+       # here. Specifically, an option that disables a warning will
+       # have no net effect if a later option then enables that
+       # warnings, (perhaps implicitly). So we put some grouped
+       # options (-Wall and -Wextra) up front and the -Wno options
+       # last.
+
+       for W in $MAYBE_WARN; do
+               LIBDRM_CC_TRY_FLAG([$W], [WARN_CFLAGS="$WARN_CFLAGS $W"])
+       done
+
+       libdrm_cv_warn_cflags=$WARN_CFLAGS
+       libdrm_cv_warn_maybe=$MAYBE_WARN
+
+       AC_MSG_CHECKING([which warning flags were supported])])
+WARN_CFLAGS="$libdrm_cv_warn_cflags"
+
+if test "x$UDEV" = xyes; then
+       AC_DEFINE(UDEV, 1, [Have UDEV support])
+fi
+
+AC_CANONICAL_HOST
+if test "x$LIBKMS" = xauto ; then
+       case $host_os in
+               linux*)         LIBKMS="yes" ;;
+               *)              LIBKMS="no" ;;
+       esac
+fi
+
+AM_CONDITIONAL(HAVE_LIBKMS, [test "x$LIBKMS" = xyes])
+
+AM_CONDITIONAL(HAVE_VMWGFX, [test "x$VMWGFX" = xyes])
+if test "x$VMWGFX" = xyes; then
+       AC_DEFINE(HAVE_VMWGFX, 1, [Have vmwgfx kernel headers])
+fi
+
+AM_CONDITIONAL(HAVE_NOUVEAU, [test "x$NOUVEAU" = xyes])
+if test "x$NOUVEAU" = xyes; then
+       AC_DEFINE(HAVE_NOUVEAU, 1, [Have nouveau (nvidia) support])
+fi
+
+PKG_CHECK_MODULES(CAIRO, cairo, [HAVE_CAIRO=yes], [HAVE_CAIRO=no])
+if test "x$HAVE_CAIRO" = xyes; then
+       AC_DEFINE(HAVE_CAIRO, 1, [Have cairo support])
+fi
+AM_CONDITIONAL(HAVE_CAIRO, [test "x$HAVE_CAIRO" = xyes])
+
+# For enumerating devices in test case
+PKG_CHECK_MODULES(LIBUDEV, libudev, [HAVE_LIBUDEV=yes], [HAVE_LIBUDEV=no])
+if test "x$HAVE_LIBUDEV" = xyes; then
+       AC_DEFINE(HAVE_LIBUDEV, 1, [Have libudev support])
+fi
+AM_CONDITIONAL(HAVE_LIBUDEV, [test "x$HAVE_LIBUDEV" = xyes])
+
+if test "x$INTEL" != "xno" -o "x$RADEON" != "xno" -o "x$SLP" != "xno"; then
+    # Check for atomic intrinsics
+    AC_CACHE_CHECK([for native atomic primitives], drm_cv_atomic_primitives,
+    [
+           drm_cv_atomic_primitives="none"
+
+           AC_LINK_IFELSE([AC_LANG_PROGRAM([[
+    int atomic_add(int i) { return __sync_fetch_and_add (&i, 1); }
+    int atomic_cmpxchg(int i, int j, int k) { return __sync_val_compare_and_swap (&i, j, k); }
+                                            ]],[[]])],
+                           [drm_cv_atomic_primitives="Intel"],[])
+
+           if test "x$drm_cv_atomic_primitives" = "xnone"; then
+                   AC_CHECK_HEADER([atomic_ops.h], drm_cv_atomic_primitives="libatomic-ops")
+           fi
+
+           # atomic functions defined in <atomic.h> & libc on Solaris
+           if test "x$drm_cv_atomic_primitives" = "xnone"; then
+                   AC_CHECK_FUNC([atomic_cas_uint],
+                                 drm_cv_atomic_primitives="Solaris")
+           fi
+
+    ])
+    if test "x$drm_cv_atomic_primitives" = xIntel; then
+           AC_DEFINE(HAVE_LIBDRM_ATOMIC_PRIMITIVES, 1,
+                     [Enable if your compiler supports the Intel __sync_* atomic primitives])
+    fi
+    if test "x$drm_cv_atomic_primitives" = "xlibatomic-ops"; then
+           AC_DEFINE(HAVE_LIB_ATOMIC_OPS, 1, [Enable if you have libatomic-ops-dev installed])
+    fi
+
+    if test "x$drm_cv_atomic_primitives" = "xnone"; then
+           if test "x$INTEL" != "xauto"; then
+                   if test "x$INTEL" != "xno"; then
+                        AC_MSG_ERROR([libdrm_intel depends upon atomic operations, which were not found for your compiler/cpu. Try compiling with -march=native, or install the libatomics-op-dev package, or, failing both of those, disable support for Intel GPUs by passing --disable-intel to ./configure])
+                   fi
+           else
+                   AC_MSG_WARN([Disabling libdrm_intel. It depends on atomic operations, which were not found for your compiler/cpu. Try compiling with -march=native, or install the libatomics-op-dev package.])
+                   INTEL=no
+           fi
+           if test "x$RADEON" != "xauto"; then
+                   if test "x$RADEON" != "xno"; then
+                        AC_MSG_ERROR([libdrm_radeon depends upon atomic operations, which were not found for your compiler/cpu. Try compiling with -march=native, or install the libatomics-op-dev package, or, failing both of those, disable support for Radeon support by passing --disable-radeon to ./configure])
+                   fi
+           else
+                   AC_MSG_WARN([Disabling libdrm_radeon. It depends on atomic operations, which were not found for your compiler/cpu. Try compiling with -march=native, or install the libatomics-op-dev package.])
+                   RADEON=no
+           fi
+
+    else
+           if test "x$INTEL" != "xno"; then
+                   INTEL=yes
+           fi
+           if test "x$RADEON" != "xno"; then
+                   RADEON=yes
+           fi
+    fi
+fi
+
+if test "x$SLP" != "xno"; then
+       AC_DEFINE(HAVE_SLP, 1, [Have slp])
+fi
+
+AM_CONDITIONAL(HAVE_SLP, [test "x$SLP" != "xno"])
+AM_CONDITIONAL(HAVE_INTEL, [test "x$INTEL" != "xno"])
+AM_CONDITIONAL(HAVE_RADEON, [test "x$RADEON" != "xno"])
+if test "x$RADEON" = xyes; then
+       AC_DEFINE(HAVE_RADEON, 1, [Have radeon support])
+fi
+
+AC_ARG_WITH([kernel-source],
+            [AS_HELP_STRING([--with-kernel-source],
+              [specify path to linux kernel source])],
+           [kernel_source="$with_kernel_source"])
+AC_SUBST(kernel_source)
+
+AC_SUBST(WARN_CFLAGS)
+AC_CONFIG_FILES([
+       Makefile
+       libkms/Makefile
+       libkms/libkms.pc
+       slp/Makefile
+       slp/libdrm_slp.pc
+       intel/Makefile
+       intel/libdrm_intel.pc
+       radeon/Makefile
+       radeon/libdrm_radeon.pc
+       nouveau/Makefile
+       nouveau/libdrm_nouveau.pc
+       tests/Makefile
+       tests/modeprint/Makefile
+       tests/modetest/Makefile
+       tests/kmstest/Makefile
+       tests/vbltest/Makefile
+       tests/gemtest/Makefile
+       tests/planetest/Makefile
+       include/Makefile
+       include/drm/Makefile
+       libdrm.pc])
+AC_OUTPUT
+
+echo ""
+echo "$PACKAGE_STRING will be compiled with:"
+echo ""
+echo "  libkms         $LIBKMS"
+echo "  Intel API      $INTEL"
+echo "  vmwgfx API     $VMWGFX"
+echo "  Radeon API     $RADEON"
+echo "  Nouveau API    $NOUVEAU"
+echo "  SLP API        $SLP"
+echo "  SLP bufmgr_dir $bufmgr_dir"
+echo ""
diff --git a/debian/README.source b/debian/README.source
new file mode 100644 (file)
index 0000000..54eb75a
--- /dev/null
@@ -0,0 +1,73 @@
+------------------------------------------------------
+Quick Guide To Patching This Package For The Impatient
+------------------------------------------------------
+
+1. Make sure you have quilt installed
+2. Unpack the package as usual with "dpkg-source -x"
+3. Run the "patch" target in debian/rules
+4. Create a new patch with "quilt new" (see quilt(1))
+5. Edit all the files you want to include in the patch with "quilt edit"
+   (see quilt(1)).
+6. Write the patch with "quilt refresh" (see quilt(1))
+7. Run the "clean" target in debian/rules
+
+Alternatively, instead of using quilt directly, you can drop the patch in to
+debian/patches and add the name of the patch to debian/patches/series.
+
+------------------------------------
+Guide To The X Strike Force Packages
+------------------------------------
+
+The X Strike Force team maintains X packages in git repositories on
+git.debian.org in the pkg-xorg subdirectory. Most upstream packages
+are actually maintained in git repositories as well, so they often
+just need to be pulled into git.debian.org in a "upstream-*" branch.
+Otherwise, the upstream sources are manually installed in the Debian
+git repository.
+
+The .orig.tar.gz upstream source file could be generated using this
+"upstream-*" branch in the Debian git repository but it is actually
+copied from upstream tarballs directly.
+
+Due to X.org being highly modular, packaging all X.org applications
+as their own independent packages would have created too many Debian
+packages. For this reason, some X.org applications have been grouped
+into larger packages: xutils, xutils-dev, x11-apps, x11-session-utils,
+x11-utils, x11-xfs-utils, x11-xkb-utils, x11-xserver-utils.
+Most packages, including the X.org server itself and all libraries
+and drivers are, however maintained independently.
+
+The Debian packaging is added by creating the "debian-*" git branch
+which contains the aforementioned "upstream-*" branch plus the debian/
+repository files.
+When a patch has to be applied to the Debian package, two solutions
+are involved:
+* If the patch is available in one of the upstream branches, it
+  may be git'cherry-picked into the Debian repository. In this
+  case, it appears directly in the .diff.gz.
+* Otherwise, the patch is added to debian/patches/ which is managed
+  with quilt as documented in /usr/share/doc/quilt/README.source.
+
+quilt is actually invoked by the Debian X packaging through a larger
+set of scripts called XSFBS. XSFBS brings some other X specific
+features such as managing dependencies and conflicts due to the video
+and input driver ABIs.
+XSFBS itself is maintained in a separate repository at
+  git://git.debian.org/pkg-xorg/xsfbs.git
+and it is pulled inside the other Debian X repositories when needed.
+
+The XSFBS patching system requires a build dependency on quilt. Also
+a dependency on $(STAMP_DIR)/patch has to be added to debian/rules
+so that the XSFBS patching occurs before the actual build. So the
+very first target of the build (likely the one running autoreconf)
+should depend on $(STAMP_DIR)/patch. It should also not depend on
+anything so that parallel builds are correctly supported (nothing
+should probably run while patching is being done). And finally, the
+clean target should depend on the xsfclean target so that patches
+are unapplied on clean.
+
+When the upstream sources contain some DFSG-nonfree files, they are
+listed in text files in debian/prune/ in the "debian-*" branch of
+the Debian repository. XSFBS' scripts then take care of removing
+these listed files during the build so as to generate a modified
+DFSG-free .orig.tar.gz tarball.
diff --git a/debian/changelog b/debian/changelog
new file mode 100644 (file)
index 0000000..b54541e
--- /dev/null
@@ -0,0 +1,970 @@
+libdrm (2.4.27-8slp2) unstable; urgency=low
+
+  * Bug Fix : fix the mutax lock
+  * Git: 165.213.180.234:slp/pkgs/xorg/lib/libdrm
+  * Tag: libdrm_2.4.27-8slp2
+
+ -- SooChan Lim <sc1.lim@samsung.com>  Mon, 05 Dec 2011 21:00:24 +0900
+
+libdrm (2.4.27-7slp2) unstable; urgency=low
+
+  * Check if input param is valid
+  * Refactoring: Move common code
+  * Git: 165.213.180.234:slp/pkgs/xorg/lib/libdrm
+  * Tag: libdrm_2.4.27-7slp2
+
+ -- Boram Park <boram1288.park@samsung.com>  Mon, 05 Dec 2011 10:52:44 +0900
+
+libdrm (2.4.27-6slp2) unstable; urgency=low
+
+  * G[S]et physical address from(to) gem
+  * Git: 165.213.180.234:slp/pkgs/xorg/lib/libdrm
+  * Tag: libdrm_2.4.27-6slp2
+
+ -- Boram Park <boram1288.park@samsung.com>  Wed, 30 Nov 2011 08:50:08 +0900
+
+libdrm (2.4.27-5slp2) unstable; urgency=low
+
+  * has user_data
+  * Git: 165.213.180.234:slp/pkgs/xorg/lib/libdrm
+  * Tag: libdrm_2.4.27-5slp2
+
+ -- Boram Park <boram1288.park@samsung.com>  Thu, 24 Nov 2011 15:29:13 +0900
+
+libdrm (2.4.27-4slp2) unstable; urgency=low
+
+  * drm_slp_bufmgr_cache_flush should return the result
+  * Git: 165.213.180.234:slp/pkgs/xorg/lib/libdrm
+  * Tag: libdrm_2.4.27-4slp2
+
+ -- Boram Park <boram1288.park@samsung.com>  Tue, 22 Nov 2011 20:46:10 +0900
+
+libdrm (2.4.27-3slp2) unstable; urgency=low
+
+  * bo can be NULL
+  * Git: 165.213.180.234:slp/pkgs/xorg/lib/libdrm
+  * Tag: libdrm_2.4.27-3slp2
+
+ -- Boram Park <boram1288.park@samsung.com>  Wed, 16 Nov 2011 19:11:54 +0900
+
+libdrm (2.4.27-2slp2) unstable; urgency=low
+
+  * bo can't be NULL
+  * patch from kernel
+  * Git: 165.213.180.234:slp/pkgs/xorg/lib/libdrm
+  * Tag: libdrm_2.4.27-2slp2
+
+ -- Boram Park <boram1288.park@samsung.com>  Mon, 14 Nov 2011 18:54:40 +0900
+
+libdrm (2.4.27-1slp2) unstable; urgency=low
+
+  * Update version to 2.4.27
+  * patch from kernel
+  * Git: 165.213.180.234:slp/pkgs/xorg/lib/libdrm
+  * Tag: libdrm_2.4.27-1slp2
+
+ -- Boram Park <boram1288.park@samsung.com>  Mon, 07 Nov 2011 17:20:23 +0900
+
+libdrm (2.4.26-4slp2) unstable; urgency=low
+
+  * generate packages for both i386 and armel
+  * Git: 165.213.180.234:slp/pkgs/xorg/lib/libdrm
+  * Tag: libdrm_2.4.26-4slp2
+
+ -- SooChan Lim <sc1.lim@samsung.com>  Wed, 19 Oct 2011 19:44:09 +0900
+
+libdrm (2.4.26-3slp2) unstable; urgency=low
+
+  * add cacheflush operation
+  * Git: 165.213.180.234:slp/pkgs/xorg/lib/libdrm
+  * Tag: libdrm_2.4.26-3slp2
+
+ -- Boram Park <boram1288.park@samsung.com>  Tue, 18 Oct 2011 11:27:30 +0900
+
+libdrm (2.4.26-2slp2) unstable; urgency=low
+
+  * define SLP in config.h
+  * Git: 165.213.180.234:slp/pkgs/xorg/lib/libdrm
+  * Tag: libdrm_2.4.26-2slp2
+
+ -- SooChan Lim <sc1.lim@samsung.com>  Thu, 13 Oct 2011 20:27:14 +0900
+
+libdrm (2.4.26-1slp2) unstable; urgency=low
+
+  * upgrade 2.4.26
+  * Git: 165.213.180.234:slp/pkgs/xorg/lib/libdrm
+  * Tag: libdrm_2.4.26-1slp2
+
+ -- Boram Park <boram1288.park@samsung.com>  Thu, 13 Oct 2011 16:14:42 +0900
+
+libdrm (2.4.23-8slp2) unstable; urgency=low
+
+  * Load SLP buffer manager
+  * Git: 165.213.180.234:slp/pkgs/xorg/lib/libdrm
+  * Tag: libdrm_2.4.23-8slp2
+
+ -- Boram Park <boram1288.park@samsung.com>  Sat, 08 Oct 2011 15:40:47 +0900
+
+libdrm (2.4.23-7slp2) unstable; urgency=low
+
+  * Remove the dependency of ump & pvr2d
+  * Git: 165.213.180.234:slp/pkgs/xorg/lib/libdrm
+  * Tag: libdrm_2.4.23-7slp2
+
+ -- Boram Park <boram1288.park@samsung.com>  Thu, 06 Oct 2011 16:07:14 +0900
+
+libdrm (2.4.23-6slp2) unstable; urgency=low
+
+  * add lock/unlock buffer for ump
+  * Git: 165.213.180.234:slp/pkgs/xorg/lib/libdrm
+  * Tag: libdrm_2.4.23-6slp2
+
+ -- SooChan Lim <sc1.lim@samsung.com>  Tue, 05 Jul 2011 16:32:49 +0900
+
+libdrm (2.4.23-5slp2) unstable; urgency=low
+
+  * modify the dependency packages
+  * Git: 165.213.180.234:slp/pkgs/xorg/lib/libdrm
+  * Tag: libdrm_2.4.23-5slp2
+
+ -- SooChan Lim <sc1.lim@samsung.com>  Thu, 09 Jun 2011 10:45:02 +0900
+
+libdrm (2.4.23-4slp2) unstable; urgency=low
+
+  * version up
+  * Git: 165.213.180.234:slp/pkgs/xorg/lib/libdrm
+  * Tag: libdrm_2.4.23-4slp2
+
+ -- SooChan Lim <sc1.lim@samsung.com>  Fri, 03 Jun 2011 18:02:05 +0900
+
+libdrm (2.4.23-3slp2) unstable; urgency=low
+
+  * add drm_slp modules
+  * Git: 165.213.180.234:slp/pkgs/xorg/lib/libdrm
+  * Tag: libdrm_2.4.23-3slp2
+
+ -- SooChan Lim <sc1.lim@samsung.com>  Fri, 03 Jun 2011 14:05:17 +0900
+
+libdrm (2.4.23-2slp2) unstable; urgency=low
+
+  * [X11R7.6] upgrade package
+  * Git: 165.213.180.234:slp/pkgs/xorg/lib/libdrm
+  * Tag: libdrm_2.4.23-2slp2
+
+ -- SooChan Lim <sc1.lim@samsung.com>  Sun, 06 Mar 2011 15:32:04 +0900
+
+libdrm (2.4.23-1slp2) unstable; urgency=low
+
+  * [X11R7.6] upgrade package
+  * Git: 165.213.180.234:slp/pkgs/xorg/lib/libdrm
+  * Tag: libdrm_2.4.23-1slp2
+
+ -- SooChan Lim <sc1.lim@samsung.com>  Mon, 03 Jan 2011 13:44:14 +0900
+
+libdrm (2.4.21-1slp2) unstable; urgency=low
+
+  * version upgrade
+  * Git: 165.213.180.234:/git/slp/pkgs/xorg/lib/libdrm
+  * Tag: libdrm_2.4.21-1slp2
+
+ -- SooChan Lim <sc1.lim@samsung.com>  Fri, 05 Nov 2010 20:43:36 +0900
+
+libdrm (2.4.21-1ubuntu2.1) unstable; urgency=low
+
+  * Add 03_intel_limit_tiled_pitches.patch: Fixes a problem where framebuffers
+    with a width greater than 2048 pixels could not be allocated on pre-i965
+    generation intel GPUs, which is common for multi-monitor setups. Backport
+    of upstream commit 726210f87. (LP: #619663)
+
+ -- Robert Hooker <robert.hooker@canonical.com>  Fri, 08 Oct 2010 12:08:09 -0400
+
+libdrm (2.4.21-1ubuntu2) unstable; urgency=low
+
+  * debian/rules: Don't install upstream changelog. This saves > 1 MB of CD
+    space.
+
+ -- Martin Pitt <martin.pitt@ubuntu.com>  Sun, 01 Aug 2010 20:46:42 +0200
+
+libdrm (2.4.21-1ubuntu1) unstable; urgency=low
+
+  * Merge from debian experimental. Remaining Ubuntu changes:
+    - Enable libdrm-intel for ports architectures until plymouth is fixed to
+      not need it unconditionally.
+    - Move runtime libraries to /lib for plymouth.
+
+ -- Robert Hooker <sarvatt@ubuntu.com>  Thu, 10 Jun 2010 18:52:09 -0400
+
+libdrm (2.4.21-1) experimental; urgency=low
+
+  [ Christopher James Halse Rogers ]
+  * debian/rules:
+    - Add libkms to build
+    - Build vmwgfx experimental API.  The drm module is available in the 2.6.34
+      kernel so we might as well build the userspace bits.
+  * debian/control:
+    - Add libkms1, libkms1-dbg packages on linux
+  * debian/patches/02_build_libkms_against_in_tree_drm:
+    - Link libkms against libdrm as it uses symbols from libdrm.
+
+  [ Robert Hooker ]
+  * New upstream release.
+  * Refresh 02_build_libkms_against_in_tree_drm.
+  * Update libdrm-intel1.symbols, libdrm-radeon1.symbols and shlibs.
+
+  [ Julien Cristau ]
+  * Update the copyright file to hopefully include all licenses variations and
+    copyright statements from the source tree.
+  * Mark new libdrm_radeon symbols private.  They shouldn't actually be
+    exported.
+  * Same with libkms.  Also don't set a minimum version to 2.4.20, since we
+    didn't ship it before anyway.
+
+ -- Julien Cristau <jcristau@debian.org>  Thu, 10 Jun 2010 23:24:54 +0200
+
+libdrm (2.4.20-3) experimental; urgency=low
+
+  [ Sven Joachim ]
+  * Update libdrm-nouveau1 to the ABI of Linux 2.6.34.
+    - Drop 03_revert_abi_change.diff.
+    - Bump libdrm-nouveau shlibs and symbols versions to 2.4.20-3~
+      to ensure that packages built against this version are not used
+      with an older libdrm-nouveau1 version.
+    - Add versioned Breaks against xserver-xorg-video-nouveau to force
+      an upgrade of that package and prevent X segfaults.
+  * Include full SONAME in libdrm-nouveau1.install.
+  * Update xsfbs to 81fc271788605b52e85c2d11635a0371fb44605e0.
+
+ -- Julien Cristau <jcristau@debian.org>  Wed, 26 May 2010 10:33:22 +0200
+
+libdrm (2.4.20-2ubuntu1) unstable; urgency=low
+
+  * Merge from debian experimental, remaining changes:
+    - Dropped patches:
+      02_fix_kms_detection_with_linux_backport_modules.diff - Obsolete
+      03_revert_abi_change.diff - Obsolete
+      04_git_nouveau_fix_sigsegv_in_nouveau_bo_new_tile.patch - Upstream
+  * Refresh libdrm-nouveau1 symbols
+
+ -- Robert Hooker <sarvatt@ubuntu.com>  Fri, 14 May 2010 13:47:41 -0400
+
+libdrm (2.4.20-2) experimental; urgency=low
+
+  * Upload again, faking a new upstream version, since a screw-up on
+    ftpmaster side trashed all files from experimental.
+
+ -- Cyril Brulebois <kibi@debian.org>  Wed, 28 Apr 2010 01:54:44 +0200
+
+libdrm (2.4.20-1) experimental; urgency=low
+
+  * New upstream release.
+    + Cherry-pick upstream fixes 107ccd92 and 332739e3.
+  * Update libdrm-intel1.symbols, libdrm-radeon1.symbols and shlibs.
+  * Disable libkms for now.
+
+ -- Brice Goglin <bgoglin@debian.org>  Fri, 16 Apr 2010 07:14:41 +0200
+
+libdrm (2.4.18-5) unstable; urgency=low
+
+  * Upload to unstable.
+
+ -- Cyril Brulebois <kibi@debian.org>  Wed, 14 Apr 2010 13:02:34 +0200
+
+libdrm (2.4.18-4) experimental; urgency=low
+
+  * Steal 03_revert_abi_change.diff from Ubuntu to revert the nouveau ABI
+    change.  Current Debian kernels support only the old ABI.
+    Thanks Sven Joachim!
+  * Build a libdrm-nouveau1 package on Linux architectures (Closes: #568162).
+    Patch adapted from the Ubuntu package. Thanks Sven Joachim!
+
+ -- Brice Goglin <bgoglin@debian.org>  Wed, 24 Mar 2010 22:27:00 +0100
+
+libdrm (2.4.18-3) unstable; urgency=low
+
+  * Include full SONAME in libdrm*.install to prevent accidental breakage.
+  * Add back the drm headers in libdrm-dev.
+
+ -- Julien Cristau <jcristau@debian.org>  Tue, 16 Mar 2010 12:28:50 +0100
+
+libdrm (2.4.18-2) unstable; urgency=low
+
+  * Fix FTBFS on non-Linux architectures (Closes: #570851): Replace
+    --{enable,disable}-radeon-experimental-api configure flag with
+    --{enable,disable}-radeon since it got renamed.
+  * Add ${misc:Depends} where it was missing, and fold all Depends.
+  * Bump Standards-Version from 3.8.3 to 3.8.4 (no changes needed).
+  * Add myself to Uploaders.
+
+ -- Cyril Brulebois <kibi@debian.org>  Mon, 22 Feb 2010 15:31:47 +0100
+
+libdrm (2.4.18-1ubuntu3) unstable; urgency=low
+
+  * debian/patches/04_git_nouveau_fix_sigsegv_in_nouveau_bo_new_tile.patch:
+    + Cherry pick from upstream git.  Fixes a segfault in libdrm_nouveau on
+      X start for some cards (LP: #547124)
+
+ -- Christopher James Halse Rogers <raof@ubuntu.com>  Mon, 12 Apr 2010 11:14:11 +1000
+
+libdrm (2.4.18-1ubuntu2) unstable; urgency=low
+
+  * libdrm-nouveau1.symbols: Add a missing symbol introduced in 2.4.18.
+
+ -- Timo Aaltonen <tjaalton@ubuntu.com>  Fri, 19 Feb 2010 14:34:02 +0200
+
+libdrm (2.4.18-1ubuntu1) unstable; urgency=low
+
+  * Merge from Debian unstable.
+  * Remove the nouveau patches, since they are included in this version.
+  * Add 03_revert_abi_change.diff to revert the nouveau ABI change.
+    We need the old ABI until the kernel has been updated.
+
+ -- Timo Aaltonen <tjaalton@ubuntu.com>  Fri, 19 Feb 2010 14:19:08 +0200
+
+libdrm (2.4.18-1) unstable; urgency=low
+
+  * New upstream release.
+
+ -- Brice Goglin <bgoglin@debian.org>  Thu, 18 Feb 2010 08:06:14 +0100
+
+libdrm (2.4.17-1ubuntu3) unstable; urgency=low
+
+  * Unapply patches in debian/patches from the source package.
+
+ -- Steve Langasek <steve.langasek@ubuntu.com>  Thu, 18 Feb 2010 06:48:37 -0800
+
+libdrm (2.4.17-1ubuntu2) unstable; urgency=low
+
+  * Move runtime libraries to /lib (needed by plymouth)
+
+ -- Steve Langasek <steve.langasek@ubuntu.com>  Thu, 18 Feb 2010 06:11:08 -0800
+
+libdrm (2.4.17-1ubuntu1) unstable; urgency=low
+
+  * Merge from Debian unstable.  Previous merge was from the unreleased
+    2.4.17-1 in pkg-xorg git.  This merge picks up the finalised changelog
+    and few extra changes.  Remaining Ubuntu changes:
+    + rules, control, libdrm-nouveau1.symbols, libdrm-nouveau1.install:
+      Enable libdrm_nouveau
+  * debian/control:
+    + libdrm-dev: Only Depend on libdrm-nouveau1 on linux-any.
+  * debian/copyright:
+    + Remove copyright section for no-longer-shipped add_libdrm-nouveau.patch.
+  * debian/patches/02_fix_kms_detection_with_linux_backport_modules.diff:
+    + Add /sys/.../lbm-drm/ to search path for controlD* file, as well as
+      /sys/.../drm/.  linux-backport-modules-nouveau has the drm module renamed
+      to lbm_drm to avoid conflicts with other modules' drm.  This fixes
+      drmCheckModesettingAvailable when using linux-backports-modules-nouveau.
+  * debian/patches/03_update_nouveau_defines_1.diff:
+  * debian/patches/04_update_nouveau_defines_2.diff:
+  * debian/patches/05_update_nouveau_defines_3.diff:
+    + Patches taken from git.  Update #defines in nouveau headers to make allow
+      recent DDXs to build.
+  * debian/patches/06_nouveau_free_objects_on_channel_close.diff:
+  * debian/patches/07_nouveau_fix_mem_leak_on_channel_free.diff:
+    + Patches taken from git.  Fix a couple of memory leaks when freeing
+      channels
+  * debian/libdrm-nouveau1.symbols:
+    + Add new symbol added in 06_nouveau_free_objects_on_channel_close.
+  * debian/patches/08_nouveau_no_flush_notify_on_channel_free.diff:
+    + Patch taken from git.  Avoid calling flush_notify when part of the
+      channel's context might be freed.
+  * debian/patchs/09_add_BEGIN_RING_NI.diff:
+    + Patch taken from git.  Add BEGIN_RING_NI inline to nouveau_pushbuf.h.
+      Used in nv50 accel code.
+
+ -- Christopher James Halse Rogers <raof@ubuntu.com>  Mon, 01 Feb 2010 15:55:14 +1100
+
+libdrm (2.4.17-0ubuntu2) unstable; urgency=low
+
+  * Enable libdrm-intel for ports architectures (LP: #507765)
+  * Adjust libdrm-dev dependencies to reflect nouveau being linux-any
+
+ -- Emmet Hikory <persia@ubuntu.com>  Fri, 15 Jan 2010 12:31:40 +0900
+
+libdrm (2.4.17-0ubuntu1) unstable; urgency=low
+
+  * Merge with Debian unstable, remaining changes:
+    + control, rules, libdrm-nouveau1.symbols: Enable libdrm_nouveau.
+  * Update libdrm-nouveau1.symbols and shlibs.
+  * Drop 02_silent_master.diff, applied upstream.
+
+ -- Timo Aaltonen <tjaalton@ubuntu.com>  Sat, 09 Jan 2010 00:00:29 +0200
+
+libdrm (2.4.17-1) unstable; urgency=low
+
+  [ Julien Cristau ]
+  * New upstream release, closes: #560434, #567831.
+  * Update patch 01_default_perms.diff to account for upstream move of libdrm
+    to toplevel.
+  * Update libdrm2.symbols and shlibs.
+  * Update libdrm-intel1.symbols and shlibs.
+  * Bump debhelper build-dep, we use dh_strip --remaining-packages (closes:
+    #558443).  Thanks, Sergio Gelato!
+  * Enable libdrm_radeon, interface to kernel graphics memory management on
+    radeon (closes: #558786).
+  * Rename the build directory to not include DEB_BUILD_GNU_TYPE for no
+    good reason.  Thanks, Colin Watson!
+  * Remove myself from Uploaders
+
+  [ Brice Goglin ]
+  * Bump linux-libc-dev dependency to 2.6.32, thanks Piotr Engelking,
+    closes: #561224.
+  * Add libdrm-radeon1 symbols and update shlibs.
+  * Update debian/copyright.
+
+  [ Timo Aaltonen ]
+  * Update libdrm2.symbols and shlibs.
+
+ -- Brice Goglin <bgoglin@debian.org>  Sun, 31 Jan 2010 20:12:38 +0100
+
+libdrm (2.4.15-1) unstable; urgency=low
+
+  * New upstream release.
+    + update libdrm-intel1 symbols and shlibs
+  * Only build libdrm-intel on x86 (linux and kfreebsd).
+
+ -- Julien Cristau <jcristau@debian.org>  Mon, 23 Nov 2009 17:00:57 +0100
+
+libdrm (2.4.14-1ubuntu2) unstable; urgency=low
+
+  * debian/patches/02_silent_master.diff:
+    - The library noisily writes to stderr every time the drm master
+      is set or dropped; this can have the side-effect that the kernel
+      repaints all of the console messages over top of whatever you
+      left on the screen.  That's fugly.
+
+ -- Scott James Remnant <scott@ubuntu.com>  Fri, 11 Dec 2009 04:24:50 +0000
+
+libdrm (2.4.14-1ubuntu1) unstable; urgency=low
+
+  * Merge from Debian unstable. (LP: #446080)
+
+ -- Timo Aaltonen <tjaalton@ubuntu.com>  Thu, 08 Oct 2009 16:35:53 +0300
+
+libdrm (2.4.14-1) unstable; urgency=low
+
+  * Parse space-separated DEB_BUILD_OPTIONS, and handle parallel=N.
+  * New upstream release.
+  * Bump Standards-Version to 3.8.3.
+
+ -- Julien Cristau <jcristau@debian.org>  Thu, 24 Sep 2009 21:53:09 +0200
+
+libdrm (2.4.13-1ubuntu1) unstable; urgency=low
+
+  * Merge with Debian. (LP: #420803)
+    + Included fixes:
+      - radeon: fix GTT writing space check
+        (LP: #411115)
+    + Remaining Ubuntu changes:
+      - control:
+        + libdrm-intel1 Replaces libdrm2 (<= 2.4.1-0ubuntu5)
+        + libdrm-dev depends on newer linux-libc-dev on !lpia
+        + libdrm-dev depends on libdrm-{radeon1,nouveau1}
+        + add libdrm-{radeon1,nouveau1}{,-dbg} source packages
+      - rules:
+        + Add libdrm-nouveau1 and libdrm-nouveau1-dbg package
+        + Enable radeon and nouveau experimental apis
+      - copyright:
+        + Update to include new files from add_libdrm-nouveau.patch
+      - libdrm-dev.install: Also install r300_reg.h, via_3d_reg.h,
+        xgi_drm.h, the nouveau headers, and other headers not provided by
+        the kernel.
+      - libdrm-{radeon1,nouveau1}.{install,symbols}:  Add nouveau and
+        radeon
+  * control:
+    + Increase linux-libc-dev version dependency to 2.6.30-2.4 for !lpia
+    + Drop Replaces on linux-libc-dev
+
+ -- Bryce Harrington <bryce@ubuntu.com>  Tue, 08 Sep 2009 15:18:34 -0700
+
+libdrm (2.4.13-1) unstable; urgency=low
+
+  [ Christopher James Halse Rogers ]
+  * debian/control:
+    + Remove scary 'built from DRM snapshot' warning from long description of
+      libdrm-intel1{,-dbg}
+
+  [ Julien Cristau ]
+  * New upstream release.
+  * Update libdrm-intel1.symbols.
+
+ -- Julien Cristau <jcristau@debian.org>  Sat, 05 Sep 2009 13:15:36 +0200
+
+libdrm (2.4.12-1ubuntu1) unstable; urgency=low
+
+  * Merge from Debian unstable.
+  * Drop 101_update_libdrm-nouveau_interface.patch, included upstream.
+
+ -- Timo Aaltonen <tjaalton@ubuntu.com>  Tue, 28 Jul 2009 14:13:58 +0300
+
+libdrm (2.4.12-1) unstable; urgency=low
+
+  * New upstream release.
+
+ -- Brice Goglin <bgoglin@debian.org>  Tue, 21 Jul 2009 15:29:03 +0200
+
+libdrm (2.4.11-1ubuntu1) unstable; urgency=low
+
+  [ Timo Aaltonen ]
+  * Merge from Debian unstable.
+
+  [ Christopher James Halse Rogers ]
+  * Add 101_update_libdrm-nouveau_interface.patch.  Backport several
+    libdrm-nouveau commits from upstream which are needed to interface with
+    the new 0.0.14 nouveau kernel interface provided by recent nouveau.ko
+    (LP: #395700)
+  * debian/libdrm-nouveau1.symbols: Add new symbols introduced in
+    101_update_libdrm-nouveau_interface
+  * debian/libdrm-dev.install:
+    + Remove drm_mode.h from libdrm-dev again; this is shipped in
+      linux-libc-dev.
+
+ -- Timo Aaltonen <tjaalton@ubuntu.com>  Wed, 08 Jul 2009 14:52:50 +0300
+
+libdrm (2.4.11-1) unstable; urgency=low
+
+  * New upstream release.
+  * Also pull in additional fix for libdrm-intel: Only do BO caching up to
+    64MB objects.
+  * Update libdrm-intel1.symbols and bump shlibs.
+  * Add README.source from xsfbs.  Bump Standards-Version to 3.8.1.
+  * Remove Thierry Reding from Uploaders, he doesn't seem to be around anymore
+    :(
+
+ -- Julien Cristau <jcristau@debian.org>  Mon, 08 Jun 2009 16:22:04 +0200
+
+libdrm (2.4.11-0ubuntu1) unstable; urgency=low
+
+  * New upstream release
+  * Add 100_bo_cache_up_to_64mb.patch.  Cherrypick from upstream.
+    This avoids making objects significantly bigger than they would be
+    otherwise, which would result in some failing at binding to the GTT.
+    Fixes issue with hangs viewing large images.
+    (LP: #330460)
+  * libdrm-intel1.symbols: Add drm_intel_bo_disable_reuse and
+    drm_intel_get_pipe_from_crtc_id
+  * Don't try to install ChangeLog since not included in upstream release.
+
+ -- Bryce Harrington <bryce@ubuntu.com>  Tue, 02 Jun 2009 17:31:34 -0700
+
+libdrm (2.4.9-2ubuntu1) unstable; urgency=low
+
+  * Merge from debian unstable, remaining changes:
+    - control:
+      + libdrm-intel1 Replaces libdrm2 (<= 2.4.1-0ubuntu5)
+      + libdrm-dev depends on linux-libc-dev => 2.6.28-5.15 on
+        [amd64 armel i386] only, and >= 2.6.28-2.4 on lpia
+      + Remove scary 'built from DRM snapshot' warning from long description of
+        libdrm-intel1{,-dbg}
+      + libdrm-dev depends on libdrm-nouveau1
+      + add libdrm-nouveau1{,-dbg} source packages
+    - rules:
+      + Add libdrm-nouveau1 and libdrm-nouveau1-dbg package
+      + Enable nouveau experimental api
+    - copyright:
+      + Update to include new files from add_libdrm-nouveau.patch
+    - libdrm-dev.install: Also install r300_reg.h, via_3d_reg.h,
+      xgi_drm.h, the nouveau headers, and other headers not provided by
+      the kernel.
+
+ -- Bryce Harrington <bryce@ubuntu.com>  Mon, 11 May 2009 18:56:10 -0700
+
+libdrm (2.4.9-2) unstable; urgency=low
+
+  * Ship all drm headers on kfreebsd, again.
+  * Move -dbg packages to new debug section.
+
+ -- Julien Cristau <jcristau@debian.org>  Sun, 03 May 2009 18:55:42 +0200
+
+libdrm (2.4.9-1) unstable; urgency=low
+
+  [ Brice Goglin ]
+  * New upstream release.
+    + Remove buggy symlinks for the upstream tarball.
+  * Add myself to Uploaders.
+
+  [ Julien Cristau ]
+  * Make the linux-libc-dev dependency linux-only (closes: #521253).  Thanks,
+    Petr Salinger!
+
+ -- Brice Goglin <bgoglin@debian.org>  Sat, 11 Apr 2009 23:12:49 +0200
+
+libdrm (2.4.5-2) unstable; urgency=low
+
+  * Add drm_mode.h to the list of headers we don't ship.
+
+ -- Julien Cristau <jcristau@debian.org>  Wed, 25 Mar 2009 10:56:53 +0100
+
+libdrm (2.4.5-0ubuntu5) unstable; urgency=low
+
+  * debian/patches/02_libdrm_nouveau_update.patch:
+    - Pull in upstream changes to libdrm-nouveau.
+      + Fixes a memory leak in fence handling.
+      + Fixes an error the DDX reports in accessing the frontbuffer.
+      + Should be reverted for the next upstream version of libdrm.
+
+ -- Christopher James Halse Rogers <raof@ubuntu.com>  Sat, 04 Apr 2009 17:54:51 +1100
+
+libdrm (2.4.5-0ubuntu4) unstable; urgency=low
+
+  * debian/libdrm-nouveau1.symbols:
+    - Fix thinko copied over from libdrm-intel1.symbols.  The file should
+      specify libdrm-nouveau1 as the corresponding package, not libdrm2.
+      Fixes xserver-xorg-video-nouveau failing to depend on libdrm-nouveau1
+      (LP: #350925)
+  * debian/control:
+    - There's no good reason to restrict the dependency of libdrm-dev on
+      libdrm-nouveau1 to x86 architectures.
+
+ -- Christopher James Halse Rogers <raof@ubuntu.com>  Sun, 29 Mar 2009 17:32:31 +1100
+
+libdrm (2.4.5-0ubuntu3) unstable; urgency=low
+
+  * debian/control: libdrm-dev should also depend on libdrm-nouveau1
+    Fixes xserver-xorg-video-nouveau FTBFS on amd64.
+    (LP: #346556)
+
+ -- Christopher James Halse Rogers <raof@ubuntu.com>  Sat, 21 Mar 2009 11:17:59 +1100
+
+libdrm (2.4.5-0ubuntu2) unstable; urgency=low
+
+  * debian/control: nouveau package description should refer to it, not
+    intel. (LP: #341294)
+  * debian/libdrm-dev.install: Also install r300_reg.h, via_3d_reg.h, and
+    xgi_drm.h.
+  * debian/rules:  Enforce --fail-missing again
+
+ -- Bryce Harrington <bryce@ubuntu.com>  Wed, 18 Mar 2009 18:50:31 -0700
+
+libdrm (2.4.5-0ubuntu1) unstable; urgency=low
+
+  [Bryce Harrington]
+  * Merge from debian-unstable (unreleased git).  Remaining changes:
+    - debian/control: libdrm-intel1 Replaces libdrm2 (<= 2.4.1-0ubuntu5)
+    - debian/control: libdrm-dev Depends on linux-libc-dev only
+      on [amd64 armel i386] until the other archs have caught up.
+    - debian/control: libdrm-dev Replace linux-libc-dev (<= 2.6.28-3.4),
+      since it includes the drm headers. We will drop them from libdrm-dev
+      when the drivers support the new headers.
+    - debian/control: Split the linux-libc-dev dep on lpia and depend
+      on >= 2.6.28-2.4 on lpia
+    - debian/control: libdrm-dev Depends on linux-libc-dev (>= 2.6.28-5.15).
+  * debian/rules: enable nouveau experimental api
+
+  [Christopher James Halse Rogers]
+  * debian/rules, debian/control, debian/libdrm-nouveau1.install:
+    + Add libdrm-nouveau1 and libdrm-nouveau1-dbg package
+  * debian/libdrm-nouveau1.symbols:
+    + Add initial symbols file.
+  * debian/control:
+    + Remove scary 'built from DRM snapshot' warning from long description of
+      libdrm-intel1{,-dbg}
+  * debian/libdrm-dev.install:
+    + Also install the libdrm-nouveau headers; these aren't going to be shipped
+      by the kernel any time soon.
+  * debian/copyright:
+    + Update to include new files from add_libdrm-nouveau.patch
+
+ -- Bryce Harrington <bryce@ubuntu.com>  Mon, 09 Mar 2009 16:43:00 -0700
+
+libdrm (2.4.5-1) unstable; urgency=low
+
+  * New upstream release. (closes: #505740)
+
+  [ Timo Aaltonen ]
+  * New upstream release. (closes: #505740)
+  * debian/rules:
+    -Run autoreconf at build time, build-depend on automake and libtool.
+     (closes: #482727)
+    -Add a debian/libdrm2.symbols file and '-c4' parameter to dh_makeshlibs
+     to fail if new symbols are added. Don't use Debian versions for now.
+
+  [ Julien Cristau ]
+  * Add a new package for the intel-specific bits (libdrm-intel1)
+  * Build-depend on pkg-config and libpthread-stubs0-dev (closes: #502078).
+    Thanks, Frank Lichtenheld!
+  * Don't mention *.la in libdrm-dev.install.
+  * Make libdrm-dev depend on libdrm-intel1 on x86.
+  * On Linux, let udev create the device files.
+
+ -- Julien Cristau <jcristau@debian.org>  Tue, 24 Mar 2009 22:20:50 +0100
+
+libdrm (2.4.4-0ubuntu6) unstable; urgency=low
+
+  * Bump dep on linux-libc-dev dep to >= 2.6.28-1.4 on lpia as this version of
+    the headers now include a compatibility #define which mesa requires.
+
+ -- Loic Minier <lool@dooz.org>  Tue, 03 Feb 2009 23:20:27 +0100
+
+libdrm (2.4.4-0ubuntu5) unstable; urgency=low
+
+  * Revert XS-Original-Vcs-* to Vcs-*, this *is* maintained in Git, sorry.
+  * Split the linux-libc-dev dep on lpia and depend on >= 2.6.28-1.1 on lpia
+    -- the version differs because it's built from linux-lpia instead of
+    linux; this is not going to be fixable anytime soon, so we should be
+    careful to use arch-specific versions for linux-libc-dev deps/bdeps.
+
+ -- Loic Minier <lool@dooz.org>  Sun, 01 Feb 2009 16:25:46 +0100
+
+libdrm (2.4.4-0ubuntu4) unstable; urgency=low
+
+  * Also depend on linux-libc-dev on [lpia] as linux-libc-dev_2.6.28-1.2_lpia
+    has the drm headers; the avoidance of this dep on other arches just helps
+    installability, but because the headers are missing you're unlikely to do
+    anything with the lib anyway.  The solution is a linux-ports upload.
+  * Rename Vcs-* to XS-Original-Vcs-*.
+
+ -- Loic Minier <lool@dooz.org>  Sat, 31 Jan 2009 23:50:33 +0100
+
+libdrm (2.4.4-0ubuntu3) unstable; urgency=low
+
+  * libdrm-dev Depends on linux-libc-dev only on [amd64 armel i386]
+    until the other archs have caught up.
+
+ -- Timo Aaltonen <tjaalton@ubuntu.com>  Sat, 31 Jan 2009 12:37:12 +0200
+
+libdrm (2.4.4-0ubuntu2) unstable; urgency=low
+
+  * debian/control: Add a missing comma to libdrm-dev's Depends.
+
+ -- Timo Aaltonen <tjaalton@ubuntu.com>  Fri, 23 Jan 2009 13:45:58 +0200
+
+libdrm (2.4.4-0ubuntu1) unstable; urgency=low
+
+  * Merge with Debian experimental, remaining changes:
+    - libdrm-intel1 Replaces libdrm2 (<= 2.4.1-0ubuntu5)
+    - libdrm-dev: Replace linux-libc-dev (<= 2.6.28-3.4), since it
+      includes the drm headers. We will drop them from libdrm-dev
+      after unstable alpha2 when the drivers support the new headers.
+    - debian/libdrm-dev.install: Don't install the drm headers anymore,
+      the drivers should build against the ones provided by the kernel.
+  * debian/libdrm-dev.install: Include a couple of headers not found
+    in the 2.6.28 kernel (mach64, nouveau). (LP: #314771)
+  * debian/control: libdrm-dev Depends on linux-libc-dev (>= 2.6.28-5.15).
+
+ -- Timo Aaltonen <tjaalton@ubuntu.com>  Fri, 23 Jan 2009 10:19:06 +0200
+
+libdrm (2.4.1-0ubuntu9) unstable; urgency=low
+
+  * debian/libdrm-dev.install: Don't install the drm headers anymore,
+    the drivers should build against the ones provided by the kernel.
+    (LP: #308387)
+
+ -- Timo Aaltonen <tjaalton@ubuntu.com>  Fri, 02 Jan 2009 12:48:40 +0200
+
+libdrm (2.4.1-0ubuntu8) unstable; urgency=low
+
+  * libdrm-dev: Replace linux-libc-dev (<= 2.6.28-3.4), since it
+    includes the drm headers. We will drop them from libdrm-dev
+    after unstable alpha2 when the drivers support the new headers.
+    (LP: #308387)
+
+ -- Timo Aaltonen <tjaalton@ubuntu.com>  Tue, 16 Dec 2008 23:29:38 +0200
+
+libdrm (2.4.1-0ubuntu7) unstable; urgency=low
+
+  * Merge with debian-experimental git, remaining changes:
+    - libdrm-intel1 Replaces libdrm2 (<= 2.4.1-0ubuntu5)
+
+ -- Timo Aaltonen <tepsipakki@ubuntu.com>  Wed, 03 Dec 2008 09:24:35 +0200
+
+libdrm (2.4.1-0ubuntu6) unstable; urgency=low
+
+  * libdrm-intel1 Replaces libdrm2 (<= 2.4.1-0ubuntu5)
+
+ -- Timo Aaltonen <tepsipakki@ubuntu.com>  Thu, 27 Nov 2008 10:25:29 +0200
+
+libdrm (2.4.1-0ubuntu5) unstable; urgency=low
+
+  * Split the symbols file (libdrm2, libdrm-intel1), remove shlibs and
+    use dh_makeshlibs properly instead.
+
+ -- Timo Aaltonen <tepsipakki@ubuntu.com>  Thu, 27 Nov 2008 09:42:59 +0200
+
+libdrm (2.4.1-0ubuntu4) unstable; urgency=low
+
+  * debian/libdrm2.install: Careful with those wildmarks.
+    libdrm2 got libdrm_intel.so too, don't do that.
+
+ -- Timo Aaltonen <tepsipakki@ubuntu.com>  Thu, 27 Nov 2008 08:58:47 +0200
+
+libdrm (2.4.1-0ubuntu3) unstable; urgency=low
+
+  * Actually build the new libdrm-intel1 package.
+
+ -- Timo Aaltonen <tepsipakki@ubuntu.com>  Wed, 26 Nov 2008 14:41:08 +0200
+
+libdrm (2.4.1-0ubuntu2) unstable; urgency=low
+
+  * Build-depend on libpthread-stubs0-dev and pkg-config (FTBFS).
+  * Make libdrm-dev depend on libdrm-intel1.
+
+ -- Timo Aaltonen <tepsipakki@ubuntu.com>  Wed, 26 Nov 2008 01:15:07 +0200
+
+libdrm (2.4.1-0ubuntu1) unstable; urgency=low
+
+  * New upstream release.
+
+ -- Timo Aaltonen <tepsipakki@ubuntu.com>  Wed, 26 Nov 2008 00:38:11 +0200
+
+libdrm (2.3.1-2) unstable; urgency=high
+
+  * Remove from the source package a bunch of files that are only used by the
+    kernel drm component.  This gets rid of the mga, r128 and radeon
+    microcode, and thus closes: #502675.  Thanks, Ben Hutchings!
+
+ -- Julien Cristau <jcristau@debian.org>  Sun, 19 Oct 2008 14:26:37 +0200
+
+libdrm (2.3.1-1) unstable; urgency=low
+
+  [ Brice Goglin ]
+  * Update upstream URL in debian/copyright.
+  * Bump Standards-Version to 3.7.3 (no changes).
+  * Drop the XS- prefix from Vcs-Git and Vcs-Browser fields in debian/control.
+  * Install the upstream ChangeLog.
+
+  [ Julien Cristau ]
+  * New upstream release (needed for mesa 7.1 and newer xserver).
+  * Note: this release removes the memory manager (TTM) interface used by the
+    i915tex dri driver.
+  * debian/rules: don't call configure with --host if we're not
+    cross-building, and fix some rules dependencies.
+
+  [ Timo Aaltonen ]
+  * Bump the shlibs to 2.3.1.
+
+ -- Julien Cristau <jcristau@debian.org>  Sun, 13 Jul 2008 19:07:49 +0200
+
+libdrm (2.3.0-4) unstable; urgency=low
+
+  [ David Nusinow ]
+  * Add NEWS.Debian explaining the change in the last upload to interested
+    administrators.
+
+  [ Julien Cristau ]
+  * Upload to unstable.
+
+ -- Julien Cristau <jcristau@debian.org>  Fri, 20 Apr 2007 05:06:34 +0200
+
+libdrm (2.3.0-3) experimental; urgency=low
+
+  * Add myself to uploaders
+  * Patch libdrm to default to device permission 666 so we don't have to do it
+    in xorg.conf. The only way libdrm can do anything is through the server
+    anyway. This can still be overridden by a user's xorg.conf. This change
+    also requires adding quilt to the build-depends
+
+ -- David Nusinow <dnusinow@debian.org>  Sun, 15 Apr 2007 13:08:50 -0400
+
+libdrm (2.3.0-2) unstable; urgency=low
+
+  * Update my email address in debian/control.
+  * Add XS-Vcs-Git and XS-Vcs-Browser in debian/control.
+  * Upload to unstable.
+
+ -- Julien Cristau <jcristau@debian.org>  Thu, 12 Apr 2007 19:06:57 +0200
+
+libdrm (2.3.0-1) experimental; urgency=low
+
+  [ Thierry Reding ]
+  * New upstream release.
+  * Set the Debian X Strike Force as maintainer.
+  * Add myself to uploaders.
+  * Add a debugging symbol package for libdrm2.
+
+  [ Julien Cristau ]
+  * Bump shlibs to libdrm2 >= 2.3.0.
+  * Add myself to uploaders.
+  * Add build-dep on dpkg-dev >= 1.13.19 to make sure that the binary:Version
+    substvar is available.
+  * libdrm2-dbg depends on libdrm2 (= ${binary:Version}).
+  * Don't install libdrm.la, and use dh_install --list-missing.
+
+ -- Julien Cristau <julien.cristau@ens-lyon.org>  Thu,  4 Jan 2007 18:56:08 +0100
+
+libdrm (2.2.0-0.1) experimental; urgency=low
+
+  * Non-maintainer upload.
+  * New upstream release.
+  * Bump Standards-Version to 3.7.2, no changes required.
+  * Bump debhelper compatibility to 5 and adjust build-dependency.
+  * Don't try to install pkgconfig files from usr/share/pkgconfig because
+    there is nothing in that directory.
+
+ -- Thierry Reding <thierry@gilfi.de>  Sat, 18 Nov 2006 19:50:26 +0100
+
+libdrm (2.0.2-0.1) unstable; urgency=low
+
+  * Non-maintainer upload.
+  * New upstream release (closes: #377166).
+    - Includes a fix for FTBFS on GNU/kFreeBSD (closes: #332994).
+  * Manually force static build.
+
+ -- Andres Salomon <dilinger@debian.org>  Sat, 23 Sep 2006 06:32:23 +0000
+
+libdrm (2.0.1-1) unstable; urgency=high
+
+  * New upstream release
+    - Fixes a pathological hash table smash discovered by the Coverity scanner
+    - updates the installed header files for various new #defines
+
+ -- David Nusinow <dnusinow@debian.org>  Tue,  4 Apr 2006 23:46:05 -0400
+
+libdrm (2.0-1) experimental; urgency=low
+
+  * First upload to Debian
+
+ -- David Nusinow <dnusinow@debian.org>  Thu,  5 Jan 2006 22:45:27 -0500
+
+libdrm (2.0-0ubuntu1) unstable; urgency=low
+
+  * New upstream release.
+  * Change binary package from libdrm1 to libdrm2, following soversion bump.
+
+ -- Daniel Stone <daniel.stone@ubuntu.com>  Mon, 12 Dec 2005 13:05:22 +1100
+
+libdrm (1.0.5-0ubuntu1) unstable; urgency=low
+
+  * New upstream version.
+
+ -- Daniel Stone <daniel.stone@ubuntu.com>  Wed,  2 Nov 2005 01:56:07 +1100
+
+libdrm (1.0.3-3) unstable; urgency=low
+
+  * Yay for understandable bug reports! *gmprf*
+  * debian/control:libdrm1 =~ s/development/runtime/ (closes: bug#325515)
+
+ -- Marcelo E. Magallon <mmagallo@debian.org>  Fri, 16 Sep 2005 09:46:05 -0600
+
+libdrm (1.0.3-2) unstable; urgency=low
+
+  * libdrm.pc.in: add -ldrm to Libs
+
+ -- Marcelo E. Magallon <mmagallo@debian.org>  Thu, 08 Sep 2005 20:49:01 -0600
+
+libdrm (1.0.3-1) unstable; urgency=low
+
+  * New upstream
+
+ -- Marcelo E. Magallon <mmagallo@debian.org>  Sun, 28 Aug 2005 11:12:07 -0600
+
+libdrm (1.0.2-3) unstable; urgency=low
+
+  * debian/control: it's "Direct Rendering Infraestructure".  I was rather
+    sure it stand for interface... thanks Michel.  (closes: bug#324514)
+  * debian/control: forgot to actually write this in the file.  Build-Depends
+    on libx11-dev.  Thanks Kurt (closes: bug#324560)
+
+ -- Marcelo E. Magallon <mmagallo@debian.org>  Sun, 28 Aug 2005 11:01:41 -0600
+
+libdrm (1.0.2-2) unstable; urgency=low
+
+  * Forgot to fix the other broken bit :-P
+
+ -- Marcelo E. Magallon <mmagallo@debian.org>  Fri, 19 Aug 2005 22:01:32 -0600
+
+libdrm (1.0.2-1) unstable; urgency=low
+
+  * Initial release. Closes: #324074
+
+ -- Marcelo E. Magallon <mmagallo@debian.org>  Fri, 19 Aug 2005 21:11:18 -0600
diff --git a/debian/compat b/debian/compat
new file mode 100644 (file)
index 0000000..7ed6ff8
--- /dev/null
@@ -0,0 +1 @@
+5
diff --git a/debian/control b/debian/control
new file mode 100644 (file)
index 0000000..ab53105
--- /dev/null
@@ -0,0 +1,119 @@
+Source: libdrm
+Priority: optional
+Maintainer: SooChan Lim <sc1.lim@samsung.com>, Sangjin Lee <lsj119@samsung.com>, Debian X Strike Force <debian-x@lists.debian.org>
+Uploaders: Julien Cristau <jcristau@debian.org>, David Nusinow <dnusinow@debian.org>, Brice Goglin <bgoglin@debian.org>, SooChan Lim <sc1.lim@samsung.com>
+Build-Depends: debhelper (>= 5.0.0), libx11-dev, dpkg-dev (>= 1.13.19), quilt (>= 0.40), automake, libtool, pkg-config, libpthread-stubs0-dev, libpciaccess-dev
+Standards-Version: 3.8.3
+Section: libs
+Vcs-Git: git://git.debian.org/git/pkg-xorg/lib/libdrm
+Vcs-Browser: http://git.debian.org/?p=pkg-xorg/lib/libdrm.git
+
+Package: libdrm-dev
+Section: libdevel
+Architecture: any
+Depends: libdrm2 (= ${binary:Version}), libdrm-slp1
+Replaces: linux-libc-dev (>= 2.6.29)
+#Depends: linux-libc-dev (>= 2.6.28) [linux-any], libdrm2 (= ${binary:Version}), libdrm-intel1 (= ${binary:Version}) [amd64 i386 kfreebsd-amd64 kfreebsd-i386]
+Description: Userspace interface to kernel DRM services -- development files
+ This library implements the userspace interface to the kernel DRM
+ services.  DRM stands for "Direct Rendering Manager", which is the
+ kernelspace portion of the "Direct Rendering Infrastructure" (DRI).
+ The DRI is currently used on Linux to provide hardware-accelerated
+ OpenGL drivers.
+ .
+ This package provides the development environment for libdrm.
+
+Package: libdrm2
+Section: libs
+Architecture: any
+Depends: ${shlibs:Depends}, ${misc:Depends}
+Description: Userspace interface to kernel DRM services -- runtime
+ This library implements the userspace interface to the kernel DRM
+ services.  DRM stands for "Direct Rendering Manager", which is the
+ kernelspace portion of the "Direct Rendering Infrastructure" (DRI).
+ The DRI is currently used on Linux to provide hardware-accelerated
+ OpenGL drivers.
+ .
+ This package provides the runtime environment for libdrm.
+
+Package: libdrm2-dbg
+Section: debug
+Priority: extra
+Architecture: any
+Depends: libdrm2 (= ${binary:Version})
+Description: Userspace interface to kernel DRM services -- debugging symbols
+ This library implements the userspace interface to the kernel DRM
+ services.  DRM stands for "Direct Rendering Manager", which is the
+ kernelspace portion of the "Direct Rendering Infrastructure" (DRI).
+ The DRI is currently used on Linux to provide hardware-accelerated
+ OpenGL drivers.
+ .
+ This package provides debugging symbols for the libdrm2 package.
+
+Package: libdrm-slp1
+Section: libs
+Architecture: any
+Depends: ${shlibs:Depends}, ${misc:Depends}
+Description: Userspace interface to slp-specific kernel DRM services -- runtime
+ This library implements the userspace interface to the intel-specific kernel
+ DRM services.  DRM stands for "Direct Rendering Manager", which is the
+ kernelspace portion of the "Direct Rendering Infrastructure" (DRI). The DRI is
+ currently used on Linux to provide hardware-accelerated OpenGL drivers.
+
+Package: libdrm-slp1-dbg
+Section: debug
+Priority: extra
+Architecture: any
+Depends: libdrm-slp1 (= ${binary:Version}), ${misc:Depends}
+Description: Userspace interface to slp-specific kernel DRM services -- debugging symbols
+ This library implements the userspace interface to the kernel DRM services.
+ DRM stands for "Direct Rendering Manager", which is the kernelspace portion
+ of the "Direct Rendering Infrastructure" (DRI). The DRI is currently used on
+ Linux to provide hardware-accelerated OpenGL drivers.
+ .
+ This package provides the debugging symbols for the libdrm-slp package.
+
+Package: libkms1
+Section: libs
+Architecture: any
+Depends: ${shlibs:Depends}, ${misc:Depends}
+Multi-Arch: same
+Pre-Depends: ${misc:Pre-Depends}
+Description: Userspace interface to kernel DRM buffer management
+ This library implements a unified userspace interface to the different buffer
+ management interfaces of the kernel DRM hardware drivers.
+
+Package: libkms1-dbg
+Section: debug
+Priority: extra
+Architecture: any
+Depends: libkms1 (= ${binary:Version}), ${misc:Depends}
+Multi-Arch: same
+Description: Userspace interface to kernel DRM buffer management -- debugging symbols
+ This library implements a unified userspace interface to the different buffer
+ management interfaces of the kernel DRM hardware drivers.
+ .
+ This package provides the debugging symbols for the libkms1 package.
+
+#Package: libdrm-intel1
+#Section: libs
+#Architecture: amd64 i386 kfreebsd-amd64 kfreebsd-i386
+#Depends: ${shlibs:Depends}, ${misc:Depends}
+#Description: Userspace interface to intel-specific kernel DRM services -- runtime
+# This library implements the userspace interface to the intel-specific kernel
+# DRM services.  DRM stands for "Direct Rendering Manager", which is the
+# kernelspace portion of the "Direct Rendering Infrastructure" (DRI). The DRI is
+# currently used on Linux to provide hardware-accelerated OpenGL drivers.
+
+#Package: libdrm-intel1-dbg
+#Section: debug
+#Priority: extra
+#Architecture: amd64 i386 kfreebsd-amd64 kfreebsd-i386
+#Depends: libdrm-intel1 (= ${binary:Version}), ${misc:Depends}
+#Description: Userspace interface to intel-specific kernel DRM services -- debugging symbols
+# This library implements the userspace interface to the kernel DRM services.
+# DRM stands for "Direct Rendering Manager", which is the kernelspace portion
+# of the "Direct Rendering Infrastructure" (DRI). The DRI is currently used on
+# Linux to provide hardware-accelerated OpenGL drivers.
+# .
+# This package provides the debugging symbols for the libdrm-intel1 package.
diff --git a/debian/copyright b/debian/copyright
new file mode 100644 (file)
index 0000000..312f609
--- /dev/null
@@ -0,0 +1,328 @@
+Copyright 2000 - 2011 Samsung Electronics co., Ltd. All Rights Reserved.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is fur-
+nished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FIT-
+NESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
+XFREE86 PROJECT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CON-
+NECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+ Copyright 2005 Adam Jackson.
+
+ Permission is hereby granted, free of charge, to any person obtaining
+ a copy of this software and associated documentation files (the
+ "Software"), to deal in the Software without restriction, including
+ without limitation on the rights to use, copy, modify, merge,
+ publish, distribute, sub license, and/or sell copies of the Software,
+ and to permit persons to whom the Software is furnished to do so,
+ subject to the following conditions:
+
+ The above copyright notice and this permission notice (including the
+ next paragraph) shall be included in all copies or substantial
+ portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ NON-INFRINGEMENT.  IN NO EVENT SHALL ADAM JACKSON BE LIABLE FOR ANY
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+
+ Copyright 2000 Precision Insight, Inc., Cedar Park, Texas.
+ Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
+ Copyright 2002 Tungsten Graphics, Inc., Cedar Park, Texas.
+ All Rights Reserved.
+
+ Permission is hereby granted, free of charge, to any person obtaining
+ a copy of this software and associated documentation files (the
+ "Software"), to deal in the Software without restriction, including
+ without limitation the rights to use, copy, modify, merge, publish,
+ distribute, sublicense, and/or sell copies of the Software, and to
+ permit persons to whom the Software is furnished to do so, subject to
+ the following conditions:
+
+ The above copyright notice and this permission notice (including the
+ next paragraph) shall be included in all copies or substantial
+ portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ NONINFRINGEMENT.  IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS
+ SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ DEALINGS IN THE SOFTWARE.
+
+
+ Copyright (c) 2007-2008 Dave Airlie <airlied@linux.ie>
+ Copyright (c) 2007-2008 Jakob Bornecrantz <wallbraker@gmail.com>
+ Copyright (c) 2008 Red Hat Inc.
+ Copyright (c) 2007-2008 Tungsten Graphics, Inc., Cedar Park, TX., USA
+ Copyright (c) 2007-2009 Intel Corporation
+
+ Permission is hereby granted, free of charge, to any person obtaining a
+ copy of this software and associated documentation files (the "Software"),
+ to deal in the Software without restriction, including without limitation
+ the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ and/or sell copies of the Software, and to permit persons to whom the
+ Software is furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ IN THE SOFTWARE.
+
+
+ Copyright 2002-2006 Tungsten Graphics, Inc., Cedar Park, Texas.
+ All Rights Reserved.
+
+ Permission is hereby granted, free of charge, to any person obtaining a
+ copy of this software and associated documentation files (the "Software"),
+ to deal in the Software without restriction, including without limitation
+ the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ and/or sell copies of the Software, and to permit persons to whom the
+ Software is furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice (including the next
+ paragraph) shall be included in all copies or substantial portions of the
+ Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ OTHER DEALINGS IN THE SOFTWARE.
+
+
+ Copyright 2000 Gareth Hughes
+ Copyright 2002 Frank C. Earl
+ Copyright 2002-2003 Leif Delgass
+ All Rights Reserved.
+
+ Permission is hereby granted, free of charge, to any person obtaining a
+ copy of this software and associated documentation files (the "Software"),
+ to deal in the Software without restriction, including without limitation
+ the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ and/or sell copies of the Software, and to permit persons to whom the
+ Software is furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice (including the next
+ paragraph) shall be included in all copies or substantial portions of the
+ Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ THE COPYRIGHT OWNER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+
+ Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
+ Copyright 2005 Stephane Marchesin.
+ All rights reserved.
+
+ Permission is hereby granted, free of charge, to any person obtaining a
+ copy of this software and associated documentation files (the "Software"),
+ to deal in the Software without restriction, including without limitation
+ the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ and/or sell copies of the Software, and to permit persons to whom the
+ Software is furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice (including the next
+ paragraph) shall be included in all copies or substantial portions of the
+ Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ OTHER DEALINGS IN THE SOFTWARE.
+
+
+ Copyright 2004  Felix Kuehling
+ All Rights Reserved.
+
+ Permission is hereby granted, free of charge, to any person obtaining a
+ copy of this software and associated documentation files (the "Software"),
+ to deal in the Software without restriction, including without limitation
+ the rights to use, copy, modify, merge, publish, distribute, sub license,
+ and/or sell copies of the Software, and to permit persons to whom the
+ Software is furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice (including the
+ next paragraph) shall be included in all copies or substantial portions
+ of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ NON-INFRINGEMENT. IN NO EVENT SHALL FELIX KUEHLING BE LIABLE FOR
+ ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
+ CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+
+ Copyright 2005 Eric Anholt
+ Copyright Â© 2007-2008 Intel Corporation
+ Copyright Â© 2008 Jérôme Glisse
+ All Rights Reserved.
+
+ Permission is hereby granted, free of charge, to any person obtaining a
+ copy of this software and associated documentation files (the "Software"),
+ to deal in the Software without restriction, including without limitation
+ the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ and/or sell copies of the Software, and to permit persons to whom the
+ Software is furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice (including the next
+ paragraph) shall be included in all copies or substantial portions of the
+ Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ SOFTWARE.
+
+
+ Copyright 1998-2003 VIA Technologies, Inc. All Rights Reserved.
+ Copyright 2001-2003 S3 Graphics, Inc. All Rights Reserved.
+
+ Permission is hereby granted, free of charge, to any person obtaining a
+ copy of this software and associated documentation files (the "Software"),
+ to deal in the Software without restriction, including without limitation
+ the rights to use, copy, modify, merge, publish, distribute, sub license,
+ and/or sell copies of the Software, and to permit persons to whom the
+ Software is furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice (including the
+ next paragraph) shall be included in all copies or substantial portions
+ of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
+ VIA, S3 GRAPHICS, AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ DEALINGS IN THE SOFTWARE.
+
+
+ Copyright Â© 2009 VMware, Inc., Palo Alto, CA., USA
+ Copyright Â© 2007-2009 Red Hat Inc.
+ Copyright Â© 2007 Intel Corporation
+ Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA
+ Copyright Â© 2008 Dave Airlie
+ Copyright Â© 2008 Jérôme Glisse
+ Copyright Â© 2008 Nicolai Haehnle
+ All Rights Reserved.
+
+ Permission is hereby granted, free of charge, to any person obtaining a
+ copy of this software and associated documentation files (the
+ "Software"), to deal in the Software without restriction, including
+ without limitation the rights to use, copy, modify, merge, publish,
+ distribute, sub license, and/or sell copies of the Software, and to
+ permit persons to whom the Software is furnished to do so, subject to
+ the following conditions:
+
+ The above copyright notice and this permission notice (including the
+ next paragraph) shall be included in all copies or substantial portions
+ of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
+ THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
+ DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+ OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+ USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+
+ Copyright (C) 1999 Wittawat Yamwong
+
+ Permission is hereby granted, free of charge, to any person obtaining a
+ copy of this software and associated documentation files (the "Software"),
+ to deal in the Software without restriction, including without limitation
+ the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ and/or sell copies of the Software, and to permit persons to whom the
+ Software is furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included
+ in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ WITTAWAT YAMWONG, OR ANY OTHER CONTRIBUTORS BE LIABLE FOR ANY CLAIM,?
+ DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR?
+ OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE?
+ OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+
+ Copyright (c) 2007 Nouveau Project
+
+ Permission is hereby granted, free of charge, to any person obtaining a
+ copy of this software and associated documentation files (the "Software"),
+ to deal in the Software without restriction, including without limitation
+ the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ and/or sell copies of the Software, and to permit persons to whom the
+ Software is furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
+ OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ SOFTWARE.
+
+
+ Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
+
+ Permission is hereby granted, free of charge, to any person obtaining a
+ copy of this software and associated documentation files (the "Software"),
+ to deal in the Software without restriction, including without limitation
+ on the rights to use, copy, modify, merge, publish, distribute, sub
+ license, and/or sell copies of the Software, and to permit persons to whom
+ the Software is furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice (including the next
+ paragraph) shall be included in all copies or substantial portions of the
+ Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
+ THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
+ DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+ OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+ USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/debian/libdrm-dev.install b/debian/libdrm-dev.install
new file mode 100644 (file)
index 0000000..0c219ff
--- /dev/null
@@ -0,0 +1,6 @@
+usr/include/*
+usr/lib/lib*.a
+usr/lib/lib*.la
+usr/lib/libdrm.so
+usr/lib/libdrm_slp.so
+usr/lib/pkgconfig/*
diff --git a/debian/libdrm-dev.links b/debian/libdrm-dev.links
new file mode 100644 (file)
index 0000000..6b60255
--- /dev/null
@@ -0,0 +1,6 @@
+#/usr/lib/libdrm.so.2 /usr/lib/libdrm.so
+#/usr/lib/libdrm_slp.so.1.0.0 /usr/lib/libdrm.so
+#/usr/lib/libdrm_intel.so.1 /usr/lib/libdrm_intel.so
+#/usr/lib/libdrm_nouveau.so.1 /usr/lib/libdrm_nouveau.so
+#/usr/lib/libdrm_radeon.so.1 /usr/lib/libdrm_radeon.so
+#/usr/lib/libkms.so.1 /usr/lib/libkms.so
diff --git a/debian/libdrm-intel1.install b/debian/libdrm-intel1.install
new file mode 100644 (file)
index 0000000..dc7beed
--- /dev/null
@@ -0,0 +1 @@
+usr/lib/libdrm_intel.so.1* /lib
diff --git a/debian/libdrm-nouveau1.install b/debian/libdrm-nouveau1.install
new file mode 100644 (file)
index 0000000..6ea6284
--- /dev/null
@@ -0,0 +1 @@
+usr/lib/libdrm_nouveau.so.1* /lib
diff --git a/debian/libdrm-radeon1.install b/debian/libdrm-radeon1.install
new file mode 100644 (file)
index 0000000..4aa8bb7
--- /dev/null
@@ -0,0 +1 @@
+usr/lib/libdrm_radeon.so.1* /lib
diff --git a/debian/libdrm-slp1.install b/debian/libdrm-slp1.install
new file mode 100644 (file)
index 0000000..3155c00
--- /dev/null
@@ -0,0 +1 @@
+usr/lib/libdrm_slp*.so.* /usr/lib
diff --git a/debian/libdrm2.install b/debian/libdrm2.install
new file mode 100644 (file)
index 0000000..8383687
--- /dev/null
@@ -0,0 +1 @@
+usr/lib/libdrm.so.* /usr/lib
diff --git a/debian/libkms1.install b/debian/libkms1.install
new file mode 100644 (file)
index 0000000..c317048
--- /dev/null
@@ -0,0 +1,2 @@
+usr/lib/libkms.so.1* /usr/lib
+usr/lib/libkms*.so /usr/lib
diff --git a/debian/rules b/debian/rules
new file mode 100755 (executable)
index 0000000..eb4d728
--- /dev/null
@@ -0,0 +1,190 @@
+#!/usr/bin/make -f
+# -*- makefile -*-
+# Sample debian/rules that uses debhelper.
+# This file was originally written by Joey Hess and Craig Small.
+# As a special exception, when this file is copied by dh-make into a
+# dh-make output file, you may use that output file without restriction.
+# This special exception was added by Craig Small in version 0.37 of dh-make.
+
+# Uncomment this to turn on verbose mode.
+#export DH_VERBOSE=1
+
+include debian/xsfbs/xsfbs.mk
+
+# These are used for cross-compiling and for saving the configure script
+# from having to guess our platform (since we know it already)
+DEB_HOST_GNU_TYPE   = $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
+DEB_BUILD_GNU_TYPE  = $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
+DEB_HOST_ARCH_OS    = $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)
+
+ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
+#confflags += --build=$(DEB_HOST_GNU_TYPE)
+       confflags += --build=$(DEB_HOST_GNU_TYPE) --host=$(DEB_HOST_GNU_TYPE)
+else
+       confflags += --build=$(DEB_HOST_GNU_TYPE) --host=$(DEB_HOST_GNU_TYPE)
+#      confflags += --build=$(DEB_BUILD_GNU_TYPE) --host=$(DEB_HOST_GNU_TYPE)
+endif
+#ifeq (linux, $(DEB_HOST_ARCH_OS))
+#      confflags += --enable-udev
+#      confflags += --enable-libkms
+#      LIBKMS = yes
+#      confflags += --enable-vmwgfx-experimental-api
+#      confflags += --enable-nouveau-experimental-api
+#      NOUVEAU = yes
+#      confflags += --enable-radeon
+#      RADEON = yes
+#else
+confflags += --enable-udev
+#confflags += --disable-udev
+confflags += --enable-libkms
+LIBKMS=yes
+confflags += --disable-nouveau-experimental-api
+NOUVEAU = no
+confflags += --disable-radeon
+RADEON = no
+#endif
+
+#ifneq (,$(filter linux kfreebsd,$(DEB_HOST_ARCH_OS)))
+#      INTEL = yes
+#endif
+
+#ifeq ($(INTEL), yes)
+#      confflags += --enable-intel
+#else
+#      confflags += --disable-intel
+#endif
+
+confflags += --disable-intel
+INTEL=no
+
+CFLAGS = -Wall -g
+LDFLAGS +=  -Wl,--hash-style=both -Wl,--as-needed
+ifneq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
+       CFLAGS += -O0
+else
+       CFLAGS += -O2
+endif
+ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
+       NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
+       MAKEFLAGS += -j$(NUMJOBS)
+endif
+
+#soolim
+INTEL=no
+LIBKMS=yes
+RADEON=no
+NOUVEAU=no
+SLP=yes
+
+#configure: $(STAMP_DIR)/patch
+configure:
+       dh_testdir
+       autoreconf -vfi
+
+obj-$(DEB_BUILD_GNU_TYPE)/config.status: configure
+       dh_testdir
+       test -d obj-$(DEB_BUILD_GNU_TYPE) || mkdir obj-$(DEB_BUILD_GNU_TYPE)
+       cd obj-$(DEB_BUILD_GNU_TYPE) && \
+       ../configure --prefix=/usr --mandir=\$${prefix}/share/man \
+               --infodir=\$${prefix}/share/info \
+               --enable-static=yes $(confflags) \
+               CFLAGS="$(CFLAGS)" \
+               LDFLAGS="$(CFLAGS)"
+
+build: build-stamp
+build-stamp: obj-$(DEB_BUILD_GNU_TYPE)/config.status
+       dh_testdir
+       cd obj-$(DEB_BUILD_GNU_TYPE) && $(MAKE)
+       >$@
+
+clean: xsfclean
+       dh_testdir
+       dh_testroot
+       rm -f build-stamp
+
+       rm -f config.cache config.log config.status
+       rm -f */config.cache */config.log */config.status
+       rm -f conftest* */conftest*
+       rm -rf autom4te.cache */autom4te.cache
+       rm -rf obj-*
+       rm -f $$(find -name Makefile.in)
+       rm -f compile config.guess config.sub configure depcomp install-sh
+       rm -f ltmain.sh missing INSTALL aclocal.m4 config.h.in mkinstalldirs
+       rm -f libdrm.pc libdrm/config.h.in
+
+       dh_clean
+
+install: build
+       dh_testdir
+       dh_testroot
+       dh_clean -k
+       dh_installdirs
+
+       cd obj-$(DEB_BUILD_GNU_TYPE) && $(MAKE) install DESTDIR=$(CURDIR)/debian/tmp
+#commented by sj76.park
+#ifeq (linux, $(DEB_HOST_ARCH_OS))
+#      # remove files provided by linux-libc-dev
+#      for file in drm_mode.h drm_sarea.h drm.h i915_drm.h mga_drm.h r128_drm.h radeon_drm.h savage_drm.h sis_drm.h via_drm.h; do \
+#              rm -f debian/tmp/usr/include/drm/$$file; \
+#      done
+#endif
+
+# Build architecture-independent files here.
+binary-indep: build install
+# We have nothing to do by default.
+
+# Build architecture-dependent files here.
+binary-arch: build install
+       dh_testdir -s
+       dh_testroot -s
+       dh_installchangelogs -s ChangeLog
+       dh_installdocs -s
+       dh_installexamples -s
+       dh_install -s --sourcedir=debian/tmp --fail-missing
+       #dh_install -s --sourcedir=debian/tmp
+       #dh_install -s --sourcedir=debian/tmp -X.la --fail-missing
+       dh_link -s
+       dh_strip -plibdrm2 --dbg-package=libdrm2-dbg
+ifeq ($(INTEL), yes)
+       #dh_strip -plibdrm-intel1 --dbg-package=libdrm-intel1-dbg
+endif
+ifeq ($(NOUVEAU), yes)
+       #dh_strip -plibdrm-nouveau1 --dbg-package=libdrm-nouveau1-dbg
+endif
+ifeq ($(RADEON), yes)
+       #dh_strip -plibdrm-radeon1 --dbg-package=libdrm-radeon1-dbg
+endif
+ifeq ($(LIBKMS), yes)
+       dh_strip -p libkms1 --dbg-package=libkms1-dbg
+endif
+ifeq ($(SLP), yes)
+       dh_strip -p libdrm-slp1 --dbg-package=libdrm-slp1-dbg
+endif
+       dh_strip -s
+       #dh_strip -s --remaining-packages
+       dh_compress -s
+       dh_fixperms -s
+       dh_makeshlibs -plibdrm2 -V'libdrm2 (>= 2.4.17)' -- -c4
+ifeq ($(INTEL), yes)
+       dh_makeshlibs -plibdrm-intel1 -V'libdrm-intel1 (>= 2.4.21)' -- -c4
+endif
+ifeq ($(NOUVEAU), yes)
+       dh_makeshlibs -plibdrm-nouveau1 -V'libdrm-nouveau1 (>= 2.4.20-3~)' -- -c4
+endif
+ifeq ($(RADEON), yes)
+       dh_makeshlibs -plibdrm-radeon1 -V'libdrm-radeon1 (>= 2.4.20)' -- -c4
+endif
+ifeq ($(LIBKMS), yes)
+       dh_makeshlibs -plibkms1 -V'libkms1' -- -c4
+endif
+ifeq ($(SLP), yes)
+       dh_makeshlibs -plibdrm-slp1 -V'libdrm-slp1 (>= 2.4)' -- -c4
+endif
+       dh_installdeb -s
+       dh_shlibdeps -s
+       dh_gencontrol -s
+       dh_md5sums -s
+       dh_builddeb -s
+
+binary: binary-indep binary-arch
+.PHONY: build clean binary-indep binary-arch binary install
diff --git a/debian/watch b/debian/watch
new file mode 100644 (file)
index 0000000..60b4b63
--- /dev/null
@@ -0,0 +1,3 @@
+version=3
+
+http://dri.freedesktop.org/libdrm/libdrm-(.*)\.tar\.gz
diff --git a/debian/xsfbs/repack.sh b/debian/xsfbs/repack.sh
new file mode 100644 (file)
index 0000000..5935cc9
--- /dev/null
@@ -0,0 +1,32 @@
+#!/bin/sh
+
+set -e
+
+if ! [ -d debian/prune ]; then
+       exit 0
+fi
+
+if [ "x$1" != x--upstream-version ]; then
+       exit 1
+fi
+
+version="$2"
+filename="$3"
+
+if [ -z "$version" ] || ! [ -f "$filename" ]; then
+       exit 1
+fi
+
+dir="$(pwd)"
+tempdir="$(mktemp -d)"
+
+cd "$tempdir"
+tar xf "$dir/$filename"
+cat "$dir"/debian/prune/* | while read file; do rm -f */$file; done
+
+tar czf "$dir/$filename" *
+cd "$dir"
+rm -rf "$tempdir"
+echo "Done pruning upstream tarball"
+
+exit 0
diff --git a/debian/xsfbs/xsfbs.mk b/debian/xsfbs/xsfbs.mk
new file mode 100644 (file)
index 0000000..3c59c20
--- /dev/null
@@ -0,0 +1,285 @@
+#!/usr/bin/make -f
+
+# Debian X Strike Force Build System (XSFBS): Make portion
+
+# Copyright 1996 Stephen Early
+# Copyright 1997 Mark Eichin
+# Copyright 1998-2005, 2007 Branden Robinson
+# Copyright 2005 David Nusinow
+#
+# Licensed under the GNU General Public License, version 2.  See the file
+# /usr/share/common-licenses/GPL or <http://www.gnu.org/copyleft/gpl.txt>.
+
+# Originally by Stephen Early <sde1000@debian.org>
+# Modified by Mark W. Eichin <eichin@kitten.gen.ma.us>
+# Modified by Adam Heath <doogie@debian.org>
+# Modified by Branden Robinson <branden@debian.org>
+# Modified by Fabio Massimo Di Nitto <fabbione@fabbione.net>
+# Modified by David Nusinow <dnusinow@debian.org>
+# Acknowledgements to Manoj Srivastava.
+
+# Pass $(DH_OPTIONS) into the environment for debhelper's benefit.
+export DH_OPTIONS
+
+# force quilt to not use ~/.quiltrc and to use debian/patches
+QUILT = QUILT_PATCHES=debian/patches quilt --quiltrc /dev/null
+
+# Set up parameters for the upstream build environment.
+
+# Determine (source) package name from Debian changelog.
+SOURCE_NAME:=$(shell dpkg-parsechangelog -ldebian/changelog \
+                        | grep '^Source:' | awk '{print $$2}')
+
+# Determine package version from Debian changelog.
+SOURCE_VERSION:=$(shell dpkg-parsechangelog -ldebian/changelog \
+                        | grep '^Version:' | awk '{print $$2}')
+
+# Determine upstream version number.
+UPSTREAM_VERSION:=$(shell echo $(SOURCE_VERSION) | sed 's/-.*//')
+
+# Determine the source version without the epoch for make-orig-tar-gz
+NO_EPOCH_VER:=$(shell echo $(UPSTREAM_VERSION) | sed 's/^.://')
+
+# Figure out who's building this package.
+BUILDER:=$(shell echo $${DEBEMAIL:-$${EMAIL:-$$(echo $$LOGNAME@$$(cat /etc/mailname 2>/dev/null))}})
+
+# Find out if this is an official build; an official build has nothing but
+# digits, dots, and/or the codename of a release in the Debian part of the
+# version number.  Anything else indicates an unofficial build.
+OFFICIAL_BUILD:=$(shell VERSION=$(SOURCE_VERSION); if ! expr "$$(echo $${VERSION\#\#*-} | sed 's/\(woody\|sarge\|etch\|lenny\)//g')" : ".*[^0-9.].*" >/dev/null 2>&1; then echo yes; fi)
+
+# Set up parameters for the Debian build environment.
+
+# Determine our architecture.
+BUILD_ARCH:=$(shell dpkg-architecture -qDEB_BUILD_ARCH)
+# Work around some old-time dpkg braindamage.
+BUILD_ARCH:=$(subst i486,i386,$(BUILD_ARCH))
+# The DEB_HOST_ARCH variable may be set per the Debian cross-compilation policy.
+ifdef DEB_HOST_ARCH
+ ARCH:=$(DEB_HOST_ARCH)
+else
+ # dpkg-cross sets the ARCH environment variable; if set, use it.
+ ifdef ARCH
+  ARCH:=$(ARCH)
+ else
+  ARCH:=$(BUILD_ARCH)
+ endif
+endif
+
+# $(STAMP_DIR) houses stamp files for complex targets.
+STAMP_DIR:=stampdir
+
+# $(DEBTREEDIR) is where all install rules are told (via $(DESTDIR)) to place
+# their files.
+DEBTREEDIR:=$(CURDIR)/debian/tmp
+
+# All "important" targets have four lines:
+#   1) A target name that is invoked by a package-building tool or the user.
+#      This consists of a dependency on a "$(STAMP_DIR)/"-prefixed counterpart.
+#   2) A line delcaring 1) as a phony target (".PHONY:").
+#   3) A "$(STAMP_DIR)/"-prefixed target which does the actual work, and may
+#   depend on other targets.
+#   4) A line declaring 3) as a member of the $(stampdir_targets) variable; the
+#   "$(STAMP_DIR)/" prefix is omitted.
+#
+# This indirection is needed so that the "stamp" files that signify when a rule
+# is done can be located in a separate "stampdir".  Recall that make has no way
+# to know when a goal has been met for a phony target (like "build" or
+# "install").
+#
+# At the end of each "$(STAMP_DIR)/" target, be sure to run the command ">$@"
+# so that the target will not be run again.  Removing the file will make Make
+# run the target over.
+
+# All phony targets should be declared as dependencies of .PHONY, even if they
+# do not have "($STAMP_DIR)/"-prefixed counterparts.
+
+# Define a harmless default rule to keep things from going nuts by accident.
+.PHONY: default
+default:
+
+# Set up the $(STAMP_DIR) directory.
+.PHONY: stampdir
+stampdir_targets+=stampdir
+stampdir: $(STAMP_DIR)/stampdir
+$(STAMP_DIR)/stampdir:
+       mkdir $(STAMP_DIR)
+       >$@
+
+# Set up the package build directory as quilt expects to find it.
+.PHONY: prepare
+stampdir_targets+=prepare
+prepare: $(STAMP_DIR)/prepare
+$(STAMP_DIR)/prepare: $(STAMP_DIR)/logdir $(STAMP_DIR)/genscripts
+       >$@
+
+.PHONY: logdir
+stampdir_targets+=logdir
+logdir: $(STAMP_DIR)/logdir
+$(STAMP_DIR)/logdir: $(STAMP_DIR)/stampdir
+       mkdir -p $(STAMP_DIR)/log
+       >$@
+
+# Apply all patches to the upstream source.
+.PHONY: patch
+stampdir_targets+=patch
+patch: $(STAMP_DIR)/patch
+$(STAMP_DIR)/patch: $(STAMP_DIR)/prepare
+       if ! [ `which quilt` ]; then \
+               echo "Couldn't find quilt. Please install it or add it to the build-depends for this package."; \
+               exit 1; \
+       fi; \
+       if $(QUILT) next >/dev/null 2>&1; then \
+         echo -n "Applying patches..."; \
+         if $(QUILT) push -a -v >$(STAMP_DIR)/log/patch 2>&1; then \
+           cat $(STAMP_DIR)/log/patch; \
+           echo "successful."; \
+         else \
+           cat $(STAMP_DIR)/log/patch; \
+           echo "failed! (check $(STAMP_DIR)/log/patch for details)"; \
+           exit 1; \
+         fi; \
+       else \
+         echo "No patches to apply"; \
+       fi; \
+       >$@
+
+# Revert all patches to the upstream source.
+.PHONY: unpatch
+unpatch: $(STAMP_DIR)/logdir
+       rm -f $(STAMP_DIR)/patch
+       @echo -n "Unapplying patches..."; \
+       if $(QUILT) applied >/dev/null 2>/dev/null; then \
+         if $(QUILT) pop -a -v >$(STAMP_DIR)/log/unpatch 2>&1; then \
+           cat $(STAMP_DIR)/log/unpatch; \
+           echo "successful."; \
+         else \
+           cat $(STAMP_DIR)/log/unpatch; \
+           echo "failed! (check $(STAMP_DIR)/log/unpatch for details)"; \
+           exit 1; \
+         fi; \
+       else \
+         echo "nothing to do."; \
+       fi
+
+# Clean the generated maintainer scripts.
+.PHONY: cleanscripts
+cleanscripts:
+       rm -f $(STAMP_DIR)/genscripts
+       rm -f debian/*.config \
+             debian/*.postinst \
+             debian/*.postrm \
+             debian/*.preinst \
+             debian/*.prerm
+
+# Clean the package build tree.
+.PHONY: xsfclean
+xsfclean: cleanscripts unpatch
+       dh_testdir
+       rm -rf .pc
+       rm -rf $(STAMP_DIR)
+       dh_clean
+
+# Remove files from the upstream source tree that we don't need, or which have
+# licensing problems.  It must be run before creating the .orig.tar.gz.
+#
+# Note: This rule is for Debian package maintainers' convenience, and is not
+# needed for conventional build scenarios.
+.PHONY: prune-upstream-tree
+prune-upstream-tree:
+       # Ensure we're in the correct directory.
+       dh_testdir
+       grep -rvh '^#' debian/prune/ | xargs --no-run-if-empty rm -rf
+
+# Verify that there are no offsets or fuzz in the patches we apply.
+#
+# Note: This rule is for Debian package maintainers' convenience, and is not
+# needed for conventional build scenarios.
+.PHONY: patch-audit
+patch-audit: prepare unpatch
+       @echo -n "Auditing patches..."; \
+       >$(STAMP_DIR)/log/patch; \
+       FUZZY=; \
+       while [ -n "$$($(QUILT) next)" ]; do \
+         RESULT=$$($(QUILT) push -v | tee -a $(STAMP_DIR)/log/patch | grep ^Hunk | sed 's/^Hunk.*\(succeeded\|FAILED\).*/\1/');\
+         case "$$RESULT" in \
+           succeeded) \
+             echo "fuzzy patch: $$($(QUILT) top)" \
+               | tee -a $(STAMP_DIR)/log/$$($(QUILT) top); \
+             FUZZY=yes; \
+             ;; \
+           FAILED) \
+             echo "broken patch: $$($(QUILT) next)" \
+               | tee -a $(STAMP_DIR)/log/$$($(QUILT) next); \
+             exit 1; \
+             ;; \
+         esac; \
+       done; \
+       if [ -n "$$FUZZY" ]; then \
+         echo "there were fuzzy patches; please fix."; \
+         exit 1; \
+       else \
+         echo "done."; \
+       fi
+
+# Generate the maintainer scripts.
+.PHONY: genscripts
+stampdir_targets+=genscripts
+genscripts: $(STAMP_DIR)/genscripts
+$(STAMP_DIR)/genscripts: $(STAMP_DIR)/stampdir
+       for FILE in debian/*.config.in \
+                   debian/*.postinst.in \
+                   debian/*.postrm.in \
+                   debian/*.preinst.in \
+                   debian/*.prerm.in; do \
+         if [ -e "$$FILE" ]; then \
+           MAINTSCRIPT=$$(echo $$FILE | sed 's/.in$$//'); \
+           sed -n '1,/^#INCLUDE_SHELL_LIB#$$/p' <$$FILE \
+             | sed -e '/^#INCLUDE_SHELL_LIB#$$/d' >$$MAINTSCRIPT.tmp; \
+           cat debian/xsfbs/xsfbs.sh >>$$MAINTSCRIPT.tmp; \
+           sed -n '/^#INCLUDE_SHELL_LIB#$$/,$$p' <$$FILE \
+             | sed -e '/^#INCLUDE_SHELL_LIB#$$/d' >>$$MAINTSCRIPT.tmp; \
+           sed -e 's/@SOURCE_VERSION@/$(SOURCE_VERSION)/' \
+               -e 's/@OFFICIAL_BUILD@/$(OFFICIAL_BUILD)/' \
+             <$$MAINTSCRIPT.tmp >$$MAINTSCRIPT; \
+           rm $$MAINTSCRIPT.tmp; \
+         fi; \
+       done
+       # Validate syntax of generated shell scripts.
+       #sh debian/scripts/validate-posix-sh debian/*.config \
+       #                                    debian/*.postinst \
+       #                                    debian/*.postrm \
+       #                                    debian/*.preinst \
+       #                                    debian/*.prerm
+       >$@
+
+# Compute dependencies for drivers
+#
+VIDEODEP = $(shell cat /usr/share/xserver-xorg/videodrvdep 2>/dev/null)
+INPUTDEP = $(shell cat /usr/share/xserver-xorg/xinputdep 2>/dev/null)
+
+# these two can be removed post-squeeze
+VIDEOABI = $(shell cat /usr/share/xserver-xorg/videoabiver 2>/dev/null)
+INPUTABI = $(shell cat /usr/share/xserver-xorg/inputabiver 2>/dev/null)
+VIDDRIVER_PROVIDES = xserver-xorg-video-$(VIDEOABI), xorg-driver-video
+INPDRIVER_PROVIDES = xserver-xorg-input-$(INPUTABI), xorg-driver-input
+
+ifeq ($(PACKAGE),)
+PACKAGE=$(shell awk '/^Package:/ { print $$2; exit }' < debian/control)
+endif
+
+.PHONY: serverabi
+serverabi: install
+ifeq ($(VIDEODEP),)
+       @echo 'error: xserver-xorg-dev >= 1.7.6.901 needs to be installed'
+       @exit 1
+else
+       echo "xviddriver:Depends=$(VIDEODEP)" >> debian/$(PACKAGE).substvars
+       echo "xinpdriver:Depends=$(INPUTDEP)" >> debian/$(PACKAGE).substvars
+       # the following is there for compatibility...
+       echo "xviddriver:Provides=$(VIDDRIVER_PROVIDES)" >> debian/$(PACKAGE).substvars
+       echo "xinpdriver:Provides=$(INPDRIVER_PROVIDES)" >> debian/$(PACKAGE).substvars
+       echo "xserver:Depends=$(VIDEODEP), $(INPUTDEP)" >> debian/$(PACKAGE).substvars
+endif
+
+# vim:set noet ai sts=8 sw=8 tw=0:
diff --git a/debian/xsfbs/xsfbs.sh b/debian/xsfbs/xsfbs.sh
new file mode 100644 (file)
index 0000000..813fd8d
--- /dev/null
@@ -0,0 +1,622 @@
+# This is the X Strike Force shell library for X Window System package
+# maintainer scripts.  It serves to define shell functions commonly used by
+# such packages, and performs some error checking necessary for proper operation
+# of those functions.  By itself, it does not "do" much; the maintainer scripts
+# invoke the functions defined here to accomplish package installation and
+# removal tasks.
+
+# If you are reading this within a Debian package maintainer script (e.g.,
+# /var/lib/dpkg/info/PACKAGE.{config,preinst,postinst,prerm,postrm}), you can
+# skip past this library by scanning forward in this file to the string
+# "GOBSTOPPER".
+
+SOURCE_VERSION=@SOURCE_VERSION@
+OFFICIAL_BUILD=@OFFICIAL_BUILD@
+
+# Use special abnormal exit codes so that problems with this library are more
+# easily tracked down.
+SHELL_LIB_INTERNAL_ERROR=86
+SHELL_LIB_THROWN_ERROR=74
+SHELL_LIB_USAGE_ERROR=99
+
+# old -> new variable names
+if [ -z "$DEBUG_XORG_PACKAGE" ] && [ -n "$DEBUG_XFREE86_PACKAGE" ]; then
+  DEBUG_XORG_PACKAGE="$DEBUG_XFREE86_PACKAGE"
+fi
+if [ -z "$DEBUG_XORG_DEBCONF" ] && [ -n "$DEBUG_XFREE86_DEBCONF" ]; then
+  DEBUG_XORG_DEBCONF="$DEBUG_XFREE86_DEBCONF"
+fi
+
+# initial sanity checks
+if [ -z "$THIS_PACKAGE" ]; then
+  cat >&2 <<EOF
+Error: package maintainer script attempted to use shell library without
+definining \$THIS_PACKAGE shell variable.  Please report the package name,
+version, and the text of this error message to the Debian Bug Tracking System.
+Visit <http://www.debian.org/Bugs/Reporting> on the World Wide Web for
+instructions, read the file /usr/share/doc/debian/bug-reporting.txt from the
+"doc-debian" package, or install the "reportbug" package and use the command of
+the same name to file a report against version $SOURCE_VERSION of this package.
+EOF
+  exit $SHELL_LIB_USAGE_ERROR
+fi
+
+if [ -z "$THIS_SCRIPT" ]; then
+  cat >&2 <<EOF
+Error: package maintainer script attempted to use shell library without
+definining \$THIS_SCRIPT shell variable.  Please report the package name,
+version, and the text of this error message to the Debian Bug Tracking System.
+Visit <http://www.debian.org/Bugs/Reporting> on the World Wide Web for
+instructions, read the file /usr/share/doc/debian/bug-reporting.txt from the
+"doc-debian" package, or install the "reportbug" package and use the command of
+the same name to file a report against version $SOURCE_VERSION of the
+"$THIS_PACKAGE" package.
+EOF
+  exit $SHELL_LIB_USAGE_ERROR
+fi
+
+if [ "$1" = "reconfigure" ] || [ -n "$DEBCONF_RECONFIGURE" ]; then
+  RECONFIGURE="true"
+else
+  RECONFIGURE=
+fi
+
+if ([ "$1" = "install" ] || [ "$1" = "configure" ]) && [ -z "$2" ]; then
+  FIRSTINST="yes"
+fi
+
+if [ -z "$RECONFIGURE" ] && [ -z "$FIRSTINST" ]; then
+  UPGRADE="yes"
+fi
+
+trap "message;\
+      message \"Received signal.  Aborting $THIS_PACKAGE package $THIS_SCRIPT script.\";\
+      message;\
+      exit 1" HUP INT QUIT TERM
+
+reject_nondigits () {
+  # syntax: reject_nondigits [ operand ... ]
+  #
+  # scan operands (typically shell variables whose values cannot be trusted) for
+  # characters other than decimal digits and barf if any are found
+  while [ -n "$1" ]; do
+    # does the operand contain anything but digits?
+    if ! expr "$1" : "[[:digit:]]\+$" > /dev/null 2>&1; then
+      # can't use die(), because it wraps message() which wraps this function
+      echo "$THIS_PACKAGE $THIS_SCRIPT error: reject_nondigits() encountered" \
+           "possibly malicious garbage \"$1\"" >&2
+      exit $SHELL_LIB_THROWN_ERROR
+    fi
+    shift
+  done
+}
+
+reject_unlikely_path_chars () {
+  # syntax: reject_unlikely_path_chars [ operand ... ]
+  #
+  # scan operands (typically shell variables whose values cannot be trusted) for
+  # characters unlikely to be seen in a path and which the shell might
+  # interpret and barf if any are found
+  while [ -n "$1" ]; do
+    # does the operand contain any funny characters?
+    if expr "$1" : '.*[!$&()*;<>?|].*' > /dev/null 2>&1; then
+      # can't use die(), because I want to avoid forward references
+      echo "$THIS_PACKAGE $THIS_SCRIPT error: reject_unlikely_path_chars()" \
+           "encountered possibly malicious garbage \"$1\"" >&2
+      exit $SHELL_LIB_THROWN_ERROR
+    fi
+    shift
+  done
+}
+
+# Query the terminal to establish a default number of columns to use for
+# displaying messages to the user.  This is used only as a fallback in the
+# event the COLUMNS variable is not set.  ($COLUMNS can react to SIGWINCH while
+# the script is running, and this cannot, only being calculated once.)
+DEFCOLUMNS=$(stty size 2> /dev/null | awk '{print $2}') || true
+if ! expr "$DEFCOLUMNS" : "[[:digit:]]\+$" > /dev/null 2>&1; then
+  DEFCOLUMNS=80
+fi
+
+message () {
+  # pretty-print messages of arbitrary length
+  reject_nondigits "$COLUMNS"
+  echo "$*" | fmt -t -w ${COLUMNS:-$DEFCOLUMNS} >&2
+}
+
+observe () {
+  # syntax: observe message ...
+  #
+  # issue observational message suitable for logging someday when support for
+  # it exists in dpkg
+  if [ -n "$DEBUG_XORG_PACKAGE" ]; then
+    message "$THIS_PACKAGE $THIS_SCRIPT note: $*"
+  fi
+}
+
+warn () {
+  # syntax: warn message ...
+  #
+  # issue warning message suitable for logging someday when support for
+  # it exists in dpkg; also send to standard error
+  message "$THIS_PACKAGE $THIS_SCRIPT warning: $*"
+}
+
+die () {
+  # syntax: die message ...
+  #
+  # exit script with error message
+  message "$THIS_PACKAGE $THIS_SCRIPT error: $*"
+  exit $SHELL_LIB_THROWN_ERROR
+}
+
+internal_error () {
+  # exit script with error; essentially a "THIS SHOULD NEVER HAPPEN" message
+  message "internal error: $*"
+  if [ -n "$OFFICIAL_BUILD" ]; then
+    message "Please report a bug in the $THIS_SCRIPT script of the" \
+            "$THIS_PACKAGE package, version $SOURCE_VERSION to the Debian Bug" \
+            "Tracking System.  Include all messages above that mention the" \
+            "$THIS_PACKAGE package.  Visit " \
+            "<http://www.debian.org/Bugs/Reporting> on the World Wide Web for" \
+            "instructions, read the file" \
+            "/usr/share/doc/debian/bug-reporting.txt from the doc-debian" \
+            "package, or install the reportbug package and use the command of" \
+            "the same name to file a report."
+  fi
+  exit $SHELL_LIB_INTERNAL_ERROR
+}
+
+usage_error () {
+  message "usage error: $*"
+  message "Please report a bug in the $THIS_SCRIPT script of the" \
+          "$THIS_PACKAGE package, version $SOURCE_VERSION to the Debian Bug" \
+          "Tracking System.  Include all messages above that mention the" \
+          "$THIS_PACKAGE package.  Visit " \
+          "<http://www.debian.org/Bugs/Reporting> on the World Wide Web for" \
+          "instructions, read the file" \
+          "/usr/share/doc/debian/bug-reporting.txt from the doc-debian" \
+          "package, or install the reportbug package and use the command of" \
+          "the same name to file a report."
+  exit $SHELL_LIB_USAGE_ERROR
+}
+
+font_update () {
+  # run $UPDATECMDS in $FONTDIRS
+
+  local dir cmd shortcmd x_font_dir_prefix
+
+  x_font_dir_prefix="/usr/share/fonts/X11"
+
+  if [ -z "$UPDATECMDS" ]; then
+    usage_error "font_update() called but \$UPDATECMDS not set"
+  fi
+  if [ -z "$FONTDIRS" ]; then
+    usage_error "font_update() called but \$FONTDIRS not set"
+  fi
+
+  reject_unlikely_path_chars "$UPDATECMDS"
+  reject_unlikely_path_chars "$FONTDIRS"
+
+  for dir in $FONTDIRS; do
+    if [ -d "$x_font_dir_prefix/$dir" ]; then
+      for cmd in $UPDATECMDS; do
+        if which "$cmd" > /dev/null 2>&1; then
+          shortcmd=${cmd##*/}
+          observe "running $shortcmd in $dir font directory"
+         cmd_opts=
+          if [ "$shortcmd" = "update-fonts-alias" ]; then
+            cmd_opts=--x11r7-layout
+          fi
+          if [ "$shortcmd" = "update-fonts-dir" ]; then
+            cmd_opts=--x11r7-layout
+          fi
+          if [ "$shortcmd" = "update-fonts-scale" ]; then
+            cmd_opts=--x11r7-layout
+          fi
+          $cmd $cmd_opts $dir || warn "$cmd $cmd_opts $dir" \
+                              "failed; font directory data may not" \
+                              "be up to date"
+        else
+          warn "$cmd not found; not updating corresponding $dir font" \
+               "directory data"
+        fi
+      done
+    else
+      warn "$dir is not a directory; not updating font directory data"
+    fi
+  done
+}
+
+remove_conffile_prepare () {
+  # syntax: remove_conffile_prepare filename official_md5sum ...
+  #
+  # Check a conffile "filename" against a list of canonical MD5 checksums.
+  # If the file's current MD5 checksum matches one of the "official_md5sum"
+  # operands provided, then prepare the conffile for removal from the system.
+  # We defer actual deletion until the package is configured so that we can
+  # roll this operation back if package installation fails.
+  #
+  # Call this function from a preinst script in the event $1 is "upgrade" or
+  # "install" and verify $2 to ensure the package is being upgraded from a
+  # version (or installed over a version removed-but-not-purged) prior to the
+  # one in which the conffile was obsoleted.
+
+  local conffile current_checksum
+
+  # validate arguments
+  if [ $# -lt 2 ]; then
+    usage_error "remove_conffile_prepare() called with wrong number of" \
+                "arguments; expected at least 2, got $#"
+    exit $SHELL_LIB_USAGE_ERROR
+  fi
+
+  conffile="$1"
+  shift
+
+  # does the conffile even exist?
+  if [ -e "$conffile" ]; then
+    # calculate its checksum
+    current_checksum=$(md5sum < "$conffile" | sed 's/[[:space:]].*//')
+    # compare it to each supplied checksum
+    while [ -n "$1" ]; do
+      if [ "$current_checksum" = "$1" ]; then
+        # we found a match; move the confffile and stop looking
+        observe "preparing obsolete conffile $conffile for removal"
+        mv "$conffile" "$conffile.$THIS_PACKAGE-tmp"
+        break
+      fi
+      shift
+    done
+  fi
+}
+
+remove_conffile_lookup () {
+  # syntax: remove_conffile_lookup package filename
+  #
+  # Lookup the md5sum of a conffile in dpkg's database, and prepare for removal
+  # if it matches the actual file's md5sum.
+  #
+  # Call this function when you would call remove_conffile_prepare but only
+  # want to check against dpkg's status database instead of known checksums.
+
+  local package conffile old_md5sum
+
+  # validate arguments
+  if [ $# -ne 2 ]; then
+    usage_error "remove_conffile_lookup() called with wrong number of" \
+                "arguments; expected 1, got $#"
+    exit $SHELL_LIB_USAGE_ERROR
+  fi
+
+  package="$1"
+  conffile="$2"
+
+  if ! [ -e "$conffile" ]; then
+    return
+  fi
+  old_md5sum="$(dpkg-query -W -f='${Conffiles}' "$package" | \
+    awk '{ if (match($0, "^ '"$conffile"' ")) print $2}')"
+  if [ -n "$old_md5sum" ]; then
+    remove_conffile_prepare "$conffile" "$old_md5sum"
+  fi
+}
+
+remove_conffile_commit () {
+  # syntax: remove_conffile_commit filename
+  #
+  # Complete the removal of a conffile "filename" that has become obsolete.
+  #
+  # Call this function from a postinst script after having used
+  # remove_conffile_prepare() in the preinst.
+
+  local conffile
+
+  # validate arguments
+  if [ $# -ne 1 ]; then
+    usage_error "remove_conffile_commit() called with wrong number of" \
+                "arguments; expected 1, got $#"
+    exit $SHELL_LIB_USAGE_ERROR
+  fi
+
+  conffile="$1"
+
+  # if the temporary file created by remove_conffile_prepare() exists, remove it
+  if [ -e "$conffile.$THIS_PACKAGE-tmp" ]; then
+    observe "committing removal of obsolete conffile $conffile"
+    rm "$conffile.$THIS_PACKAGE-tmp"
+  fi
+}
+
+remove_conffile_rollback () {
+  # syntax: remove_conffile_rollback filename
+  #
+  # Roll back the removal of a conffile "filename".
+  #
+  # Call this function from a postrm script in the event $1 is "abort-upgrade"
+  # or "abort-install" is  after having used remove_conffile_prepare() in the
+  # preinst.
+
+  local conffile
+
+  # validate arguments
+  if [ $# -ne 1 ]; then
+    usage_error "remove_conffile_rollback() called with wrong number of" \
+                "arguments; expected 1, got $#"
+    exit $SHELL_LIB_USAGE_ERROR
+  fi
+
+  conffile="$1"
+
+  # if the temporary file created by remove_conffile_prepare() exists, move it
+  # back
+  if [ -e "$conffile.$THIS_PACKAGE-tmp" ]; then
+    observe "rolling back removal of obsolete conffile $conffile"
+    mv "$conffile.$THIS_PACKAGE-tmp" "$conffile"
+  fi
+}
+
+replace_conffile_with_symlink_prepare () {
+  # syntax: replace_conffile_with_symlink_prepare oldfilename newfilename \
+  # official_md5sum ...
+  #
+  # Check a conffile "oldfilename" against a list of canonical MD5 checksums.
+  # If the file's current MD5 checksum matches one of the "official_md5sum"
+  # operands provided, then prepare the conffile for removal from the system.
+  # We defer actual deletion until the package is configured so that we can
+  # roll this operation back if package installation fails. Otherwise copy it
+  # to newfilename and let dpkg handle it through conffiles mechanism.
+  #
+  # Call this function from a preinst script in the event $1 is "upgrade" or
+  # "install" and verify $2 to ensure the package is being upgraded from a
+  # version (or installed over a version removed-but-not-purged) prior to the
+  # one in which the conffile was obsoleted.
+
+  local conffile current_checksum
+
+  # validate arguments
+  if [ $# -lt 3 ]; then
+    usage_error "replace_conffile_with_symlink_prepare() called with wrong" \
+                " number of arguments; expected at least 3, got $#"
+    exit $SHELL_LIB_USAGE_ERROR
+  fi
+
+  oldconffile="$1"
+  shift
+  newconffile="$1"
+  shift
+
+  remove_conffile_prepare "$_oldconffile" "$@"
+  # If $oldconffile still exists, then md5sums didn't match.
+  # Copy it to new one.
+  if [ -f "$oldconffile" ]; then
+    cp "$oldconffile" "$newconffile"
+  fi
+
+}
+
+replace_conffile_with_symlink_commit () {
+  # syntax: replace_conffile_with_symlink_commit oldfilename
+  #
+  # Complete the removal of a conffile "oldfilename" that has been
+  # replaced by a symlink.
+  #
+  # Call this function from a postinst script after having used
+  # replace_conffile_with_symlink_prepare() in the preinst.
+
+  local conffile
+
+  # validate arguments
+  if [ $# -ne 1 ]; then
+    usage_error "replace_conffile_with_symlink_commit() called with wrong" \
+                "number of arguments; expected 1, got $#"
+    exit $SHELL_LIB_USAGE_ERROR
+  fi
+
+  conffile="$1"
+
+  remove_conffile_commit "$conffile"
+}
+
+replace_conffile_with_symlink_rollback () {
+  # syntax: replace_conffile_with_symlink_rollback oldfilename newfilename
+  #
+  # Roll back the replacing of a conffile "oldfilename" with symlink to
+  # "newfilename".
+  #
+  # Call this function from a postrm script in the event $1 is "abort-upgrade"
+  # or "abort-install" and verify $2 to ensure the package failed to upgrade
+  # from a version (or install over a version removed-but-not-purged) prior
+  # to the one in which the conffile was obsoleted.
+  # You should have  used replace_conffile_with_symlink_prepare() in the
+  # preinst.
+
+  local conffile
+
+  # validate arguments
+  if [ $# -ne 2 ]; then
+    usage_error "replace_conffile_with_symlink_rollback() called with wrong" \
+                "number of arguments; expected 2, got $#"
+    exit $SHELL_LIB_USAGE_ERROR
+  fi
+
+  oldconffile="$1"
+  newconffile="$2"
+
+  remove_conffile_rollback "$_oldconffile"
+  if [ -f "$newconffile" ]; then
+    rm "$newconffile"
+  fi
+}
+
+run () {
+  # syntax: run command [ argument ... ]
+  #
+  # Run specified command with optional arguments and report its exit status.
+  # Useful for commands whose exit status may be nonzero, but still acceptable,
+  # or commands whose failure is not fatal to us.
+  #
+  # NOTE: Do *not* use this function with db_get or db_metaget commands; in
+  # those cases the return value of the debconf command *must* be checked
+  # before the string returned by debconf is used for anything.
+
+  local retval
+
+  # validate arguments
+  if [ $# -lt 1 ]; then
+    usage_error "run() called with wrong number of arguments; expected at" \
+                "least 1, got $#"
+    exit $SHELL_LIB_USAGE_ERROR
+  fi
+
+  "$@" || retval=$?
+
+  if [ ${retval:-0} -ne 0 ]; then
+    observe "command \"$*\" exited with status $retval"
+  fi
+}
+
+make_symlink_sane () {
+  # syntax: make_symlink_sane symlink target
+  #
+  # Ensure that the symbolic link symlink exists, and points to target.
+  #
+  # If symlink does not exist, create it and point it at target.
+  #
+  # If symlink exists but is not a symbolic link, back it up.
+  #
+  # If symlink exists, is a symbolic link, but points to the wrong location, fix
+  # it.
+  #
+  # If symlink exists, is a symbolic link, and already points to target, do
+  # nothing.
+  #
+  # This function wouldn't be needed if ln had an -I, --idempotent option.
+
+  # Validate arguments.
+  if [ $# -ne 2 ]; then
+    usage_error "make_symlink_sane() called with wrong number of arguments;" \
+      "expected 2, got $#"
+    exit $SHELL_LIB_USAGE_ERROR
+  fi
+
+  # We could just use the positional parameters as-is, but that makes things
+  # harder to follow.
+  local symlink target
+
+  symlink="$1"
+  target="$2"
+
+  if [ -L "$symlink" ] && [ "$(readlink "$symlink")" = "$target" ]; then
+      observe "link from $symlink to $target already exists"
+  else
+    observe "creating symbolic link from $symlink to $target"
+    mkdir -p "${target%/*}" "${symlink%/*}"
+    ln -s -b -S ".dpkg-old" "$target" "$symlink"
+  fi
+}
+
+migrate_dir_to_symlink () {
+  # syntax: migrate_dir_to_symlink old_location new_location
+  #
+  # Per Debian Policy section 6.5.4, "A directory will never be replaced by a
+  # symbolic link to a directory or vice versa; instead, the existing state
+  # (symlink or not) will be left alone and dpkg will follow the symlink if
+  # there is one."
+  #
+  # We have to do it ourselves.
+  #
+  # This function moves the contents of old_location, a directory, into
+  # new_location, a directory, then makes old_location a symbolic link to
+  # new_location.
+  #
+  # old_location need not exist, but if it does, it must be a directory (or a
+  # symlink to a directory).  If it is not, it is backed up.  If new_location
+  # exists already and is not a directory, it is backed up.
+  #
+  # This function should be called from a package's preinst so that other
+  # packages unpacked after this one --- but before this package's postinst runs
+  # --- are unpacked into new_location even if their payloads contain
+  # old_location filespecs.
+
+  # Validate arguments.
+  if [ $# -ne 2 ]; then
+    usage_error "migrate_dir_to_symlink() called with wrong number of"
+                "arguments; expected 2, got $#"
+    exit $SHELL_LIB_USAGE_ERROR
+  fi
+
+  # We could just use the positional parameters as-is, but that makes things
+  # harder to follow.
+  local new old
+
+  old="$1"
+  new="$2"
+
+  # Is old location a symlink?
+  if [ -L "$old" ]; then
+    # Does it already point to new location?
+    if [ "$(readlink "$old")" = "$new" ]; then
+      # Nothing to do; migration has already been done.
+      observe "migration of $old to $new already done"
+      return 0
+    else
+      # Back it up.
+      warn "backing up symbolic link $old as $old.dpkg-old"
+      mv -b "$old" "$old.dpkg-old"
+    fi
+  fi
+
+  # Does old location exist, but is not a directory?
+  if [ -e "$old" ] && ! [ -d "$old" ]; then
+      # Back it up.
+      warn "backing up non-directory $old as $old.dpkg-old"
+      mv -b "$old" "$old.dpkg-old"
+  fi
+
+  observe "migrating $old to $new"
+
+  # Is new location a symlink?
+  if [ -L "$new" ]; then
+    # Does it point the wrong way, i.e., back to where we're migrating from?
+    if [ "$(readlink "$new")" = "$old" ]; then
+      # Get rid of it.
+      observe "removing symbolic link $new which points to $old"
+      rm "$new"
+    else
+      # Back it up.
+      warn "backing up symbolic link $new as $new.dpkg-old"
+      mv -b "$new" "$new.dpkg-old"
+    fi
+  fi
+
+  # Does new location exist, but is not a directory?
+  if [ -e "$new" ] && ! [ -d "$new" ]; then
+    warn "backing up non-directory $new as $new.dpkg-old"
+    mv -b "$new" "$new.dpkg-old"
+  fi
+
+  # Create new directory if it does not yet exist.
+  if ! [ -e "$new" ]; then
+    observe "creating $new"
+    mkdir -p "$new"
+  fi
+
+  # Copy files in old location to new location.  Back up any filenames that
+  # already exist in the new location with the extension ".dpkg-old".
+  observe "copying files from $old to $new"
+  if ! (cd "$old" && cp -a -b -S ".dpkg-old" . "$new"); then
+    die "error(s) encountered while copying files from $old to $new"
+  fi
+
+  # Remove files at old location.
+  observe "removing $old"
+  rm -r "$old"
+
+  # Create symlink from old location to new location.
+  make_symlink_sane "$old" "$new"
+}
+
+# vim:set ai et sw=2 ts=2 tw=80:
+
+# GOBSTOPPER: The X Strike Force shell library ends here.
diff --git a/include/Makefile.am b/include/Makefile.am
new file mode 100644 (file)
index 0000000..55ea506
--- /dev/null
@@ -0,0 +1 @@
+SUBDIRS = drm
diff --git a/include/drm/Makefile.am b/include/drm/Makefile.am
new file mode 100755 (executable)
index 0000000..095962e
--- /dev/null
@@ -0,0 +1,43 @@
+#  Copyright 2005 Adam Jackson.
+#
+#  Permission is hereby granted, free of charge, to any person obtaining a
+#  copy of this software and associated documentation files (the "Software"),
+#  to deal in the Software without restriction, including without limitation
+#  on the rights to use, copy, modify, merge, publish, distribute, sub
+#  license, and/or sell copies of the Software, and to permit persons to whom
+#  the Software is furnished to do so, subject to the following conditions:
+#
+#  The above copyright notice and this permission notice (including the next
+#  paragraph) shall be included in all copies or substantial portions of the
+#  Software.
+#
+#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+#  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+#  FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
+#  ADAM JACKSON BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+#  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+#  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+# XXX airlied says, nothing besides *_drm.h and drm*.h should be necessary.
+# however, r300 and via need their reg headers installed in order to build.
+# better solutions are welcome.
+
+klibdrmincludedir = ${includedir}/libdrm
+klibdrminclude_HEADERS = \
+       drm.h \
+       drm_mode.h \
+       drm_sarea.h \
+       i915_drm.h \
+       mga_drm.h \
+       nouveau_drm.h \
+       r128_drm.h \
+       radeon_drm.h \
+       savage_drm.h \
+       sis_drm.h \
+       via_drm.h \
+       mach64_drm.h \
+       exynos_drm.h
+
+if HAVE_VMWGFX
+klibdrminclude_HEADERS += vmwgfx_drm.h
+endif
diff --git a/include/drm/drm.h b/include/drm/drm.h
new file mode 100644 (file)
index 0000000..8adb9d5
--- /dev/null
@@ -0,0 +1,807 @@
+/**
+ * \file drm.h
+ * Header for the Direct Rendering Manager
+ *
+ * \author Rickard E. (Rik) Faith <faith@valinux.com>
+ *
+ * \par Acknowledgments:
+ * Dec 1999, Richard Henderson <rth@twiddle.net>, move to generic \c cmpxchg.
+ */
+
+/*
+ * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
+ * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
+ * All rights reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef _DRM_H_
+#define _DRM_H_
+
+#if defined(__linux__)
+
+#include <linux/types.h>
+#include <asm/ioctl.h>
+typedef unsigned int drm_handle_t;
+
+#else /* One of the BSDs */
+
+#include <sys/ioccom.h>
+#include <sys/types.h>
+typedef int8_t   __s8;
+typedef uint8_t  __u8;
+typedef int16_t  __s16;
+typedef uint16_t __u16;
+typedef int32_t  __s32;
+typedef uint32_t __u32;
+typedef int64_t  __s64;
+typedef uint64_t __u64;
+typedef unsigned long drm_handle_t;
+
+#endif
+
+#define DRM_NAME       "drm"     /**< Name in kernel, /dev, and /proc */
+#define DRM_MIN_ORDER  5         /**< At least 2^5 bytes = 32 bytes */
+#define DRM_MAX_ORDER  22        /**< Up to 2^22 bytes = 4MB */
+#define DRM_RAM_PERCENT 10       /**< How much system ram can we lock? */
+
+#define _DRM_LOCK_HELD 0x80000000U /**< Hardware lock is held */
+#define _DRM_LOCK_CONT 0x40000000U /**< Hardware lock is contended */
+#define _DRM_LOCK_IS_HELD(lock)           ((lock) & _DRM_LOCK_HELD)
+#define _DRM_LOCK_IS_CONT(lock)           ((lock) & _DRM_LOCK_CONT)
+#define _DRM_LOCKING_CONTEXT(lock) ((lock) & ~(_DRM_LOCK_HELD|_DRM_LOCK_CONT))
+
+typedef unsigned int drm_context_t;
+typedef unsigned int drm_drawable_t;
+typedef unsigned int drm_magic_t;
+
+/**
+ * Cliprect.
+ *
+ * \warning: If you change this structure, make sure you change
+ * XF86DRIClipRectRec in the server as well
+ *
+ * \note KW: Actually it's illegal to change either for
+ * backwards-compatibility reasons.
+ */
+struct drm_clip_rect {
+       unsigned short x1;
+       unsigned short y1;
+       unsigned short x2;
+       unsigned short y2;
+};
+
+/**
+ * Drawable information.
+ */
+struct drm_drawable_info {
+       unsigned int num_rects;
+       struct drm_clip_rect *rects;
+};
+
+/**
+ * Texture region,
+ */
+struct drm_tex_region {
+       unsigned char next;
+       unsigned char prev;
+       unsigned char in_use;
+       unsigned char padding;
+       unsigned int age;
+};
+
+/**
+ * Hardware lock.
+ *
+ * The lock structure is a simple cache-line aligned integer.  To avoid
+ * processor bus contention on a multiprocessor system, there should not be any
+ * other data stored in the same cache line.
+ */
+struct drm_hw_lock {
+       __volatile__ unsigned int lock;         /**< lock variable */
+       char padding[60];                       /**< Pad to cache line */
+};
+
+/**
+ * DRM_IOCTL_VERSION ioctl argument type.
+ *
+ * \sa drmGetVersion().
+ */
+struct drm_version {
+       int version_major;        /**< Major version */
+       int version_minor;        /**< Minor version */
+       int version_patchlevel;   /**< Patch level */
+       size_t name_len;          /**< Length of name buffer */
+       char *name;       /**< Name of driver */
+       size_t date_len;          /**< Length of date buffer */
+       char *date;       /**< User-space buffer to hold date */
+       size_t desc_len;          /**< Length of desc buffer */
+       char *desc;       /**< User-space buffer to hold desc */
+};
+
+/**
+ * DRM_IOCTL_GET_UNIQUE ioctl argument type.
+ *
+ * \sa drmGetBusid() and drmSetBusId().
+ */
+struct drm_unique {
+       size_t unique_len;        /**< Length of unique */
+       char *unique;     /**< Unique name for driver instantiation */
+};
+
+struct drm_list {
+       int count;                /**< Length of user-space structures */
+       struct drm_version *version;
+};
+
+struct drm_block {
+       int unused;
+};
+
+/**
+ * DRM_IOCTL_CONTROL ioctl argument type.
+ *
+ * \sa drmCtlInstHandler() and drmCtlUninstHandler().
+ */
+struct drm_control {
+       enum {
+               DRM_ADD_COMMAND,
+               DRM_RM_COMMAND,
+               DRM_INST_HANDLER,
+               DRM_UNINST_HANDLER
+       } func;
+       int irq;
+};
+
+/**
+ * Type of memory to map.
+ */
+enum drm_map_type {
+       _DRM_FRAME_BUFFER = 0,    /**< WC (no caching), no core dump */
+       _DRM_REGISTERS = 1,       /**< no caching, no core dump */
+       _DRM_SHM = 2,             /**< shared, cached */
+       _DRM_AGP = 3,             /**< AGP/GART */
+       _DRM_SCATTER_GATHER = 4,  /**< Scatter/gather memory for PCI DMA */
+       _DRM_CONSISTENT = 5,      /**< Consistent memory for PCI DMA */
+       _DRM_GEM = 6,             /**< GEM object */
+};
+
+/**
+ * Memory mapping flags.
+ */
+enum drm_map_flags {
+       _DRM_RESTRICTED = 0x01,      /**< Cannot be mapped to user-virtual */
+       _DRM_READ_ONLY = 0x02,
+       _DRM_LOCKED = 0x04,          /**< shared, cached, locked */
+       _DRM_KERNEL = 0x08,          /**< kernel requires access */
+       _DRM_WRITE_COMBINING = 0x10, /**< use write-combining if available */
+       _DRM_CONTAINS_LOCK = 0x20,   /**< SHM page that contains lock */
+       _DRM_REMOVABLE = 0x40,       /**< Removable mapping */
+       _DRM_DRIVER = 0x80           /**< Managed by driver */
+};
+
+struct drm_ctx_priv_map {
+       unsigned int ctx_id;     /**< Context requesting private mapping */
+       void *handle;            /**< Handle of map */
+};
+
+/**
+ * DRM_IOCTL_GET_MAP, DRM_IOCTL_ADD_MAP and DRM_IOCTL_RM_MAP ioctls
+ * argument type.
+ *
+ * \sa drmAddMap().
+ */
+struct drm_map {
+       unsigned long offset;    /**< Requested physical address (0 for SAREA)*/
+       unsigned long size;      /**< Requested physical size (bytes) */
+       enum drm_map_type type;  /**< Type of memory to map */
+       enum drm_map_flags flags;        /**< Flags */
+       void *handle;            /**< User-space: "Handle" to pass to mmap() */
+                                /**< Kernel-space: kernel-virtual address */
+       int mtrr;                /**< MTRR slot used */
+       /*   Private data */
+};
+
+/**
+ * DRM_IOCTL_GET_CLIENT ioctl argument type.
+ */
+struct drm_client {
+       int idx;                /**< Which client desired? */
+       int auth;               /**< Is client authenticated? */
+       unsigned long pid;      /**< Process ID */
+       unsigned long uid;      /**< User ID */
+       unsigned long magic;    /**< Magic */
+       unsigned long iocs;     /**< Ioctl count */
+};
+
+enum drm_stat_type {
+       _DRM_STAT_LOCK,
+       _DRM_STAT_OPENS,
+       _DRM_STAT_CLOSES,
+       _DRM_STAT_IOCTLS,
+       _DRM_STAT_LOCKS,
+       _DRM_STAT_UNLOCKS,
+       _DRM_STAT_VALUE,        /**< Generic value */
+       _DRM_STAT_BYTE,         /**< Generic byte counter (1024bytes/K) */
+       _DRM_STAT_COUNT,        /**< Generic non-byte counter (1000/k) */
+
+       _DRM_STAT_IRQ,          /**< IRQ */
+       _DRM_STAT_PRIMARY,      /**< Primary DMA bytes */
+       _DRM_STAT_SECONDARY,    /**< Secondary DMA bytes */
+       _DRM_STAT_DMA,          /**< DMA */
+       _DRM_STAT_SPECIAL,      /**< Special DMA (e.g., priority or polled) */
+       _DRM_STAT_MISSED        /**< Missed DMA opportunity */
+           /* Add to the *END* of the list */
+};
+
+/**
+ * DRM_IOCTL_GET_STATS ioctl argument type.
+ */
+struct drm_stats {
+       unsigned long count;
+       struct {
+               unsigned long value;
+               enum drm_stat_type type;
+       } data[15];
+};
+
+/**
+ * Hardware locking flags.
+ */
+enum drm_lock_flags {
+       _DRM_LOCK_READY = 0x01,      /**< Wait until hardware is ready for DMA */
+       _DRM_LOCK_QUIESCENT = 0x02,  /**< Wait until hardware quiescent */
+       _DRM_LOCK_FLUSH = 0x04,      /**< Flush this context's DMA queue first */
+       _DRM_LOCK_FLUSH_ALL = 0x08,  /**< Flush all DMA queues first */
+       /* These *HALT* flags aren't supported yet
+          -- they will be used to support the
+          full-screen DGA-like mode. */
+       _DRM_HALT_ALL_QUEUES = 0x10, /**< Halt all current and future queues */
+       _DRM_HALT_CUR_QUEUES = 0x20  /**< Halt all current queues */
+};
+
+/**
+ * DRM_IOCTL_LOCK, DRM_IOCTL_UNLOCK and DRM_IOCTL_FINISH ioctl argument type.
+ *
+ * \sa drmGetLock() and drmUnlock().
+ */
+struct drm_lock {
+       int context;
+       enum drm_lock_flags flags;
+};
+
+/**
+ * DMA flags
+ *
+ * \warning
+ * These values \e must match xf86drm.h.
+ *
+ * \sa drm_dma.
+ */
+enum drm_dma_flags {
+       /* Flags for DMA buffer dispatch */
+       _DRM_DMA_BLOCK = 0x01,        /**<
+                                      * Block until buffer dispatched.
+                                      *
+                                      * \note The buffer may not yet have
+                                      * been processed by the hardware --
+                                      * getting a hardware lock with the
+                                      * hardware quiescent will ensure
+                                      * that the buffer has been
+                                      * processed.
+                                      */
+       _DRM_DMA_WHILE_LOCKED = 0x02, /**< Dispatch while lock held */
+       _DRM_DMA_PRIORITY = 0x04,     /**< High priority dispatch */
+
+       /* Flags for DMA buffer request */
+       _DRM_DMA_WAIT = 0x10,         /**< Wait for free buffers */
+       _DRM_DMA_SMALLER_OK = 0x20,   /**< Smaller-than-requested buffers OK */
+       _DRM_DMA_LARGER_OK = 0x40     /**< Larger-than-requested buffers OK */
+};
+
+/**
+ * DRM_IOCTL_ADD_BUFS and DRM_IOCTL_MARK_BUFS ioctl argument type.
+ *
+ * \sa drmAddBufs().
+ */
+struct drm_buf_desc {
+       int count;               /**< Number of buffers of this size */
+       int size;                /**< Size in bytes */
+       int low_mark;            /**< Low water mark */
+       int high_mark;           /**< High water mark */
+       enum {
+               _DRM_PAGE_ALIGN = 0x01, /**< Align on page boundaries for DMA */
+               _DRM_AGP_BUFFER = 0x02, /**< Buffer is in AGP space */
+               _DRM_SG_BUFFER = 0x04,  /**< Scatter/gather memory buffer */
+               _DRM_FB_BUFFER = 0x08,  /**< Buffer is in frame buffer */
+               _DRM_PCI_BUFFER_RO = 0x10 /**< Map PCI DMA buffer read-only */
+       } flags;
+       unsigned long agp_start; /**<
+                                 * Start address of where the AGP buffers are
+                                 * in the AGP aperture
+                                 */
+};
+
+/**
+ * DRM_IOCTL_INFO_BUFS ioctl argument type.
+ */
+struct drm_buf_info {
+       int count;              /**< Entries in list */
+       struct drm_buf_desc *list;
+};
+
+/**
+ * DRM_IOCTL_FREE_BUFS ioctl argument type.
+ */
+struct drm_buf_free {
+       int count;
+       int *list;
+};
+
+/**
+ * Buffer information
+ *
+ * \sa drm_buf_map.
+ */
+struct drm_buf_pub {
+       int idx;                       /**< Index into the master buffer list */
+       int total;                     /**< Buffer size */
+       int used;                      /**< Amount of buffer in use (for DMA) */
+       void *address;         /**< Address of buffer */
+};
+
+/**
+ * DRM_IOCTL_MAP_BUFS ioctl argument type.
+ */
+struct drm_buf_map {
+       int count;              /**< Length of the buffer list */
+#ifdef __cplusplus
+       void *virt;
+#else
+       void *virtual;          /**< Mmap'd area in user-virtual */
+#endif
+       struct drm_buf_pub *list;       /**< Buffer information */
+};
+
+/**
+ * DRM_IOCTL_DMA ioctl argument type.
+ *
+ * Indices here refer to the offset into the buffer list in drm_buf_get.
+ *
+ * \sa drmDMA().
+ */
+struct drm_dma {
+       int context;                      /**< Context handle */
+       int send_count;                   /**< Number of buffers to send */
+       int *send_indices;        /**< List of handles to buffers */
+       int *send_sizes;                  /**< Lengths of data to send */
+       enum drm_dma_flags flags;         /**< Flags */
+       int request_count;                /**< Number of buffers requested */
+       int request_size;                 /**< Desired size for buffers */
+       int *request_indices;     /**< Buffer information */
+       int *request_sizes;
+       int granted_count;                /**< Number of buffers granted */
+};
+
+enum drm_ctx_flags {
+       _DRM_CONTEXT_PRESERVED = 0x01,
+       _DRM_CONTEXT_2DONLY = 0x02
+};
+
+/**
+ * DRM_IOCTL_ADD_CTX ioctl argument type.
+ *
+ * \sa drmCreateContext() and drmDestroyContext().
+ */
+struct drm_ctx {
+       drm_context_t handle;
+       enum drm_ctx_flags flags;
+};
+
+/**
+ * DRM_IOCTL_RES_CTX ioctl argument type.
+ */
+struct drm_ctx_res {
+       int count;
+       struct drm_ctx *contexts;
+};
+
+/**
+ * DRM_IOCTL_ADD_DRAW and DRM_IOCTL_RM_DRAW ioctl argument type.
+ */
+struct drm_draw {
+       drm_drawable_t handle;
+};
+
+/**
+ * DRM_IOCTL_UPDATE_DRAW ioctl argument type.
+ */
+typedef enum {
+       DRM_DRAWABLE_CLIPRECTS,
+} drm_drawable_info_type_t;
+
+struct drm_update_draw {
+       drm_drawable_t handle;
+       unsigned int type;
+       unsigned int num;
+       unsigned long long data;
+};
+
+/**
+ * DRM_IOCTL_GET_MAGIC and DRM_IOCTL_AUTH_MAGIC ioctl argument type.
+ */
+struct drm_auth {
+       drm_magic_t magic;
+};
+
+/**
+ * DRM_IOCTL_IRQ_BUSID ioctl argument type.
+ *
+ * \sa drmGetInterruptFromBusID().
+ */
+struct drm_irq_busid {
+       int irq;        /**< IRQ number */
+       int busnum;     /**< bus number */
+       int devnum;     /**< device number */
+       int funcnum;    /**< function number */
+};
+
+enum drm_vblank_seq_type {
+       _DRM_VBLANK_ABSOLUTE = 0x0,     /**< Wait for specific vblank sequence number */
+       _DRM_VBLANK_RELATIVE = 0x1,     /**< Wait for given number of vblanks */
+       _DRM_VBLANK_EVENT = 0x4000000,   /**< Send event instead of blocking */
+       _DRM_VBLANK_FLIP = 0x8000000,   /**< Scheduled buffer swap should flip */
+       _DRM_VBLANK_NEXTONMISS = 0x10000000,    /**< If missed, wait for next vblank */
+       _DRM_VBLANK_SECONDARY = 0x20000000,     /**< Secondary display controller */
+       _DRM_VBLANK_SIGNAL = 0x40000000 /**< Send signal instead of blocking, unsupported */
+};
+
+#define _DRM_VBLANK_TYPES_MASK (_DRM_VBLANK_ABSOLUTE | _DRM_VBLANK_RELATIVE)
+#define _DRM_VBLANK_FLAGS_MASK (_DRM_VBLANK_EVENT | _DRM_VBLANK_SIGNAL | \
+                               _DRM_VBLANK_SECONDARY | _DRM_VBLANK_NEXTONMISS)
+
+struct drm_wait_vblank_request {
+       enum drm_vblank_seq_type type;
+       unsigned int sequence;
+       unsigned long signal;
+};
+
+struct drm_wait_vblank_reply {
+       enum drm_vblank_seq_type type;
+       unsigned int sequence;
+       long tval_sec;
+       long tval_usec;
+};
+
+/**
+ * DRM_IOCTL_WAIT_VBLANK ioctl argument type.
+ *
+ * \sa drmWaitVBlank().
+ */
+union drm_wait_vblank {
+       struct drm_wait_vblank_request request;
+       struct drm_wait_vblank_reply reply;
+};
+
+#define _DRM_PRE_MODESET 1
+#define _DRM_POST_MODESET 2
+
+/**
+ * DRM_IOCTL_MODESET_CTL ioctl argument type
+ *
+ * \sa drmModesetCtl().
+ */
+struct drm_modeset_ctl {
+       __u32 crtc;
+       __u32 cmd;
+};
+
+/**
+ * DRM_IOCTL_AGP_ENABLE ioctl argument type.
+ *
+ * \sa drmAgpEnable().
+ */
+struct drm_agp_mode {
+       unsigned long mode;     /**< AGP mode */
+};
+
+/**
+ * DRM_IOCTL_AGP_ALLOC and DRM_IOCTL_AGP_FREE ioctls argument type.
+ *
+ * \sa drmAgpAlloc() and drmAgpFree().
+ */
+struct drm_agp_buffer {
+       unsigned long size;     /**< In bytes -- will round to page boundary */
+       unsigned long handle;   /**< Used for binding / unbinding */
+       unsigned long type;     /**< Type of memory to allocate */
+       unsigned long physical; /**< Physical used by i810 */
+};
+
+/**
+ * DRM_IOCTL_AGP_BIND and DRM_IOCTL_AGP_UNBIND ioctls argument type.
+ *
+ * \sa drmAgpBind() and drmAgpUnbind().
+ */
+struct drm_agp_binding {
+       unsigned long handle;   /**< From drm_agp_buffer */
+       unsigned long offset;   /**< In bytes -- will round to page boundary */
+};
+
+/**
+ * DRM_IOCTL_AGP_INFO ioctl argument type.
+ *
+ * \sa drmAgpVersionMajor(), drmAgpVersionMinor(), drmAgpGetMode(),
+ * drmAgpBase(), drmAgpSize(), drmAgpMemoryUsed(), drmAgpMemoryAvail(),
+ * drmAgpVendorId() and drmAgpDeviceId().
+ */
+struct drm_agp_info {
+       int agp_version_major;
+       int agp_version_minor;
+       unsigned long mode;
+       unsigned long aperture_base;    /* physical address */
+       unsigned long aperture_size;    /* bytes */
+       unsigned long memory_allowed;   /* bytes */
+       unsigned long memory_used;
+
+       /* PCI information */
+       unsigned short id_vendor;
+       unsigned short id_device;
+};
+
+/**
+ * DRM_IOCTL_SG_ALLOC ioctl argument type.
+ */
+struct drm_scatter_gather {
+       unsigned long size;     /**< In bytes -- will round to page boundary */
+       unsigned long handle;   /**< Used for mapping / unmapping */
+};
+
+/**
+ * DRM_IOCTL_SET_VERSION ioctl argument type.
+ */
+struct drm_set_version {
+       int drm_di_major;
+       int drm_di_minor;
+       int drm_dd_major;
+       int drm_dd_minor;
+};
+
+/** DRM_IOCTL_GEM_CLOSE ioctl argument type */
+struct drm_gem_close {
+       /** Handle of the object to be closed. */
+       __u32 handle;
+       __u32 pad;
+};
+
+/** DRM_IOCTL_GEM_FLINK ioctl argument type */
+struct drm_gem_flink {
+       /** Handle for the object being named */
+       __u32 handle;
+
+       /** Returned global name */
+       __u32 name;
+};
+
+/** DRM_IOCTL_GEM_OPEN ioctl argument type */
+struct drm_gem_open {
+       /** Name of object being opened */
+       __u32 name;
+
+       /** Returned handle for the object */
+       __u32 handle;
+
+       /** Returned size of the object */
+       __u64 size;
+};
+
+/** DRM_IOCTL_GET_CAP ioctl argument type */
+struct drm_get_cap {
+       __u64 capability;
+       __u64 value;
+};
+
+#include "drm_mode.h"
+
+#define DRM_IOCTL_BASE                 'd'
+#define DRM_IO(nr)                     _IO(DRM_IOCTL_BASE,nr)
+#define DRM_IOR(nr,type)               _IOR(DRM_IOCTL_BASE,nr,type)
+#define DRM_IOW(nr,type)               _IOW(DRM_IOCTL_BASE,nr,type)
+#define DRM_IOWR(nr,type)              _IOWR(DRM_IOCTL_BASE,nr,type)
+
+#define DRM_IOCTL_VERSION              DRM_IOWR(0x00, struct drm_version)
+#define DRM_IOCTL_GET_UNIQUE           DRM_IOWR(0x01, struct drm_unique)
+#define DRM_IOCTL_GET_MAGIC            DRM_IOR( 0x02, struct drm_auth)
+#define DRM_IOCTL_IRQ_BUSID            DRM_IOWR(0x03, struct drm_irq_busid)
+#define DRM_IOCTL_GET_MAP               DRM_IOWR(0x04, struct drm_map)
+#define DRM_IOCTL_GET_CLIENT            DRM_IOWR(0x05, struct drm_client)
+#define DRM_IOCTL_GET_STATS             DRM_IOR( 0x06, struct drm_stats)
+#define DRM_IOCTL_SET_VERSION          DRM_IOWR(0x07, struct drm_set_version)
+#define DRM_IOCTL_MODESET_CTL           DRM_IOW(0x08, struct drm_modeset_ctl)
+#define DRM_IOCTL_GEM_CLOSE            DRM_IOW (0x09, struct drm_gem_close)
+#define DRM_IOCTL_GEM_FLINK            DRM_IOWR(0x0a, struct drm_gem_flink)
+#define DRM_IOCTL_GEM_OPEN             DRM_IOWR(0x0b, struct drm_gem_open)
+#define DRM_IOCTL_GET_CAP              DRM_IOWR(0x0c, struct drm_get_cap)
+
+#define DRM_IOCTL_SET_UNIQUE           DRM_IOW( 0x10, struct drm_unique)
+#define DRM_IOCTL_AUTH_MAGIC           DRM_IOW( 0x11, struct drm_auth)
+#define DRM_IOCTL_BLOCK                        DRM_IOWR(0x12, struct drm_block)
+#define DRM_IOCTL_UNBLOCK              DRM_IOWR(0x13, struct drm_block)
+#define DRM_IOCTL_CONTROL              DRM_IOW( 0x14, struct drm_control)
+#define DRM_IOCTL_ADD_MAP              DRM_IOWR(0x15, struct drm_map)
+#define DRM_IOCTL_ADD_BUFS             DRM_IOWR(0x16, struct drm_buf_desc)
+#define DRM_IOCTL_MARK_BUFS            DRM_IOW( 0x17, struct drm_buf_desc)
+#define DRM_IOCTL_INFO_BUFS            DRM_IOWR(0x18, struct drm_buf_info)
+#define DRM_IOCTL_MAP_BUFS             DRM_IOWR(0x19, struct drm_buf_map)
+#define DRM_IOCTL_FREE_BUFS            DRM_IOW( 0x1a, struct drm_buf_free)
+
+#define DRM_IOCTL_RM_MAP               DRM_IOW( 0x1b, struct drm_map)
+
+#define DRM_IOCTL_SET_SAREA_CTX                DRM_IOW( 0x1c, struct drm_ctx_priv_map)
+#define DRM_IOCTL_GET_SAREA_CTX        DRM_IOWR(0x1d, struct drm_ctx_priv_map)
+
+#define DRM_IOCTL_SET_MASTER            DRM_IO(0x1e)
+#define DRM_IOCTL_DROP_MASTER           DRM_IO(0x1f)
+
+#define DRM_IOCTL_ADD_CTX              DRM_IOWR(0x20, struct drm_ctx)
+#define DRM_IOCTL_RM_CTX               DRM_IOWR(0x21, struct drm_ctx)
+#define DRM_IOCTL_MOD_CTX              DRM_IOW( 0x22, struct drm_ctx)
+#define DRM_IOCTL_GET_CTX              DRM_IOWR(0x23, struct drm_ctx)
+#define DRM_IOCTL_SWITCH_CTX           DRM_IOW( 0x24, struct drm_ctx)
+#define DRM_IOCTL_NEW_CTX              DRM_IOW( 0x25, struct drm_ctx)
+#define DRM_IOCTL_RES_CTX              DRM_IOWR(0x26, struct drm_ctx_res)
+#define DRM_IOCTL_ADD_DRAW             DRM_IOWR(0x27, struct drm_draw)
+#define DRM_IOCTL_RM_DRAW              DRM_IOWR(0x28, struct drm_draw)
+#define DRM_IOCTL_DMA                  DRM_IOWR(0x29, struct drm_dma)
+#define DRM_IOCTL_LOCK                 DRM_IOW( 0x2a, struct drm_lock)
+#define DRM_IOCTL_UNLOCK               DRM_IOW( 0x2b, struct drm_lock)
+#define DRM_IOCTL_FINISH               DRM_IOW( 0x2c, struct drm_lock)
+
+#define DRM_IOCTL_AGP_ACQUIRE          DRM_IO(  0x30)
+#define DRM_IOCTL_AGP_RELEASE          DRM_IO(  0x31)
+#define DRM_IOCTL_AGP_ENABLE           DRM_IOW( 0x32, struct drm_agp_mode)
+#define DRM_IOCTL_AGP_INFO             DRM_IOR( 0x33, struct drm_agp_info)
+#define DRM_IOCTL_AGP_ALLOC            DRM_IOWR(0x34, struct drm_agp_buffer)
+#define DRM_IOCTL_AGP_FREE             DRM_IOW( 0x35, struct drm_agp_buffer)
+#define DRM_IOCTL_AGP_BIND             DRM_IOW( 0x36, struct drm_agp_binding)
+#define DRM_IOCTL_AGP_UNBIND           DRM_IOW( 0x37, struct drm_agp_binding)
+
+#define DRM_IOCTL_SG_ALLOC             DRM_IOWR(0x38, struct drm_scatter_gather)
+#define DRM_IOCTL_SG_FREE              DRM_IOW( 0x39, struct drm_scatter_gather)
+
+#define DRM_IOCTL_WAIT_VBLANK          DRM_IOWR(0x3a, union drm_wait_vblank)
+
+#define DRM_IOCTL_UPDATE_DRAW          DRM_IOW(0x3f, struct drm_update_draw)
+
+#define DRM_IOCTL_MODE_GETRESOURCES    DRM_IOWR(0xA0, struct drm_mode_card_res)
+#define DRM_IOCTL_MODE_GETCRTC         DRM_IOWR(0xA1, struct drm_mode_crtc)
+#define DRM_IOCTL_MODE_SETCRTC         DRM_IOWR(0xA2, struct drm_mode_crtc)
+#define DRM_IOCTL_MODE_CURSOR          DRM_IOWR(0xA3, struct drm_mode_cursor)
+#define DRM_IOCTL_MODE_GETGAMMA                DRM_IOWR(0xA4, struct drm_mode_crtc_lut)
+#define DRM_IOCTL_MODE_SETGAMMA                DRM_IOWR(0xA5, struct drm_mode_crtc_lut)
+#define DRM_IOCTL_MODE_GETENCODER      DRM_IOWR(0xA6, struct drm_mode_get_encoder)
+#define DRM_IOCTL_MODE_GETCONNECTOR    DRM_IOWR(0xA7, struct drm_mode_get_connector)
+#define DRM_IOCTL_MODE_ATTACHMODE      DRM_IOWR(0xA8, struct drm_mode_mode_cmd)
+#define DRM_IOCTL_MODE_DETACHMODE      DRM_IOWR(0xA9, struct drm_mode_mode_cmd)
+
+#define DRM_IOCTL_MODE_GETPROPERTY     DRM_IOWR(0xAA, struct drm_mode_get_property)
+#define DRM_IOCTL_MODE_SETPROPERTY     DRM_IOWR(0xAB, struct drm_mode_connector_set_property)
+#define DRM_IOCTL_MODE_GETPROPBLOB     DRM_IOWR(0xAC, struct drm_mode_get_blob)
+#define DRM_IOCTL_MODE_GETFB           DRM_IOWR(0xAD, struct drm_mode_fb_cmd)
+#define DRM_IOCTL_MODE_ADDFB           DRM_IOWR(0xAE, struct drm_mode_fb_cmd)
+#define DRM_IOCTL_MODE_RMFB            DRM_IOWR(0xAF, unsigned int)
+#define DRM_IOCTL_MODE_PAGE_FLIP       DRM_IOWR(0xB0, struct drm_mode_crtc_page_flip)
+#define DRM_IOCTL_MODE_DIRTYFB         DRM_IOWR(0xB1, struct drm_mode_fb_dirty_cmd)
+
+#define DRM_IOCTL_MODE_CREATE_DUMB DRM_IOWR(0xB2, struct drm_mode_create_dumb)
+#define DRM_IOCTL_MODE_MAP_DUMB    DRM_IOWR(0xB3, struct drm_mode_map_dumb)
+#define DRM_IOCTL_MODE_DESTROY_DUMB    DRM_IOWR(0xB4, struct drm_mode_destroy_dumb)
+#define DRM_IOCTL_MODE_GETPLANERESOURCES DRM_IOWR(0xB5, struct drm_mode_get_plane_res)
+#define DRM_IOCTL_MODE_GETPLANE        DRM_IOWR(0xB6, struct drm_mode_get_plane)
+#define DRM_IOCTL_MODE_SETPLANE        DRM_IOWR(0xB7, struct drm_mode_set_plane)
+#define DRM_IOCTL_MODE_ADDFB2          DRM_IOWR(0xB8, struct drm_mode_fb_cmd2)
+
+/**
+ * Device specific ioctls should only be in their respective headers
+ * The device specific ioctl range is from 0x40 to 0x99.
+ * Generic IOCTLS restart at 0xA0.
+ *
+ * \sa drmCommandNone(), drmCommandRead(), drmCommandWrite(), and
+ * drmCommandReadWrite().
+ */
+#define DRM_COMMAND_BASE                0x40
+#define DRM_COMMAND_END                        0xA0
+
+/**
+ * Header for events written back to userspace on the drm fd.  The
+ * type defines the type of event, the length specifies the total
+ * length of the event (including the header), and user_data is
+ * typically a 64 bit value passed with the ioctl that triggered the
+ * event.  A read on the drm fd will always only return complete
+ * events, that is, if for example the read buffer is 100 bytes, and
+ * there are two 64 byte events pending, only one will be returned.
+ *
+ * Event types 0 - 0x7fffffff are generic drm events, 0x80000000 and
+ * up are chipset specific.
+ */
+struct drm_event {
+       __u32 type;
+       __u32 length;
+};
+
+#define DRM_EVENT_VBLANK 0x01
+#define DRM_EVENT_FLIP_COMPLETE 0x02
+
+struct drm_event_vblank {
+       struct drm_event base;
+       __u64 user_data;
+       __u32 tv_sec;
+       __u32 tv_usec;
+       __u32 sequence;
+       __u32 reserved;
+};
+
+#define DRM_CAP_DUMB_BUFFER 0x1
+#define DRM_CAP_VBLANK_HIGH_CRTC   0x2
+
+/* typedef area */
+typedef struct drm_clip_rect drm_clip_rect_t;
+typedef struct drm_drawable_info drm_drawable_info_t;
+typedef struct drm_tex_region drm_tex_region_t;
+typedef struct drm_hw_lock drm_hw_lock_t;
+typedef struct drm_version drm_version_t;
+typedef struct drm_unique drm_unique_t;
+typedef struct drm_list drm_list_t;
+typedef struct drm_block drm_block_t;
+typedef struct drm_control drm_control_t;
+typedef enum drm_map_type drm_map_type_t;
+typedef enum drm_map_flags drm_map_flags_t;
+typedef struct drm_ctx_priv_map drm_ctx_priv_map_t;
+typedef struct drm_map drm_map_t;
+typedef struct drm_client drm_client_t;
+typedef enum drm_stat_type drm_stat_type_t;
+typedef struct drm_stats drm_stats_t;
+typedef enum drm_lock_flags drm_lock_flags_t;
+typedef struct drm_lock drm_lock_t;
+typedef enum drm_dma_flags drm_dma_flags_t;
+typedef struct drm_buf_desc drm_buf_desc_t;
+typedef struct drm_buf_info drm_buf_info_t;
+typedef struct drm_buf_free drm_buf_free_t;
+typedef struct drm_buf_pub drm_buf_pub_t;
+typedef struct drm_buf_map drm_buf_map_t;
+typedef struct drm_dma drm_dma_t;
+typedef union drm_wait_vblank drm_wait_vblank_t;
+typedef struct drm_agp_mode drm_agp_mode_t;
+typedef enum drm_ctx_flags drm_ctx_flags_t;
+typedef struct drm_ctx drm_ctx_t;
+typedef struct drm_ctx_res drm_ctx_res_t;
+typedef struct drm_draw drm_draw_t;
+typedef struct drm_update_draw drm_update_draw_t;
+typedef struct drm_auth drm_auth_t;
+typedef struct drm_irq_busid drm_irq_busid_t;
+typedef enum drm_vblank_seq_type drm_vblank_seq_type_t;
+
+typedef struct drm_agp_buffer drm_agp_buffer_t;
+typedef struct drm_agp_binding drm_agp_binding_t;
+typedef struct drm_agp_info drm_agp_info_t;
+typedef struct drm_scatter_gather drm_scatter_gather_t;
+typedef struct drm_set_version drm_set_version_t;
+
+#endif
diff --git a/include/drm/drm_mode.h b/include/drm/drm_mode.h
new file mode 100755 (executable)
index 0000000..b4e6d7b
--- /dev/null
@@ -0,0 +1,442 @@
+/*
+ * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
+ * Copyright (c) 2007 Jakob Bornecrantz <wallbraker@gmail.com>
+ * Copyright (c) 2008 Red Hat Inc.
+ * Copyright (c) 2007-2008 Tungsten Graphics, Inc., Cedar Park, TX., USA
+ * Copyright (c) 2007-2008 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#ifndef _DRM_MODE_H
+#define _DRM_MODE_H
+
+#include <linux/videodev2.h>
+
+#define DRM_DISPLAY_INFO_LEN   32
+#define DRM_CONNECTOR_NAME_LEN 32
+#define DRM_DISPLAY_MODE_LEN   32
+#define DRM_PROP_NAME_LEN      32
+
+#define DRM_MODE_TYPE_BUILTIN  (1<<0)
+#define DRM_MODE_TYPE_CLOCK_C  ((1<<1) | DRM_MODE_TYPE_BUILTIN)
+#define DRM_MODE_TYPE_CRTC_C   ((1<<2) | DRM_MODE_TYPE_BUILTIN)
+#define DRM_MODE_TYPE_PREFERRED        (1<<3)
+#define DRM_MODE_TYPE_DEFAULT  (1<<4)
+#define DRM_MODE_TYPE_USERDEF  (1<<5)
+#define DRM_MODE_TYPE_DRIVER   (1<<6)
+
+/* Video mode flags */
+/* bit compatible with the xorg definitions. */
+#define DRM_MODE_FLAG_PHSYNC   (1<<0)
+#define DRM_MODE_FLAG_NHSYNC   (1<<1)
+#define DRM_MODE_FLAG_PVSYNC   (1<<2)
+#define DRM_MODE_FLAG_NVSYNC   (1<<3)
+#define DRM_MODE_FLAG_INTERLACE        (1<<4)
+#define DRM_MODE_FLAG_DBLSCAN  (1<<5)
+#define DRM_MODE_FLAG_CSYNC    (1<<6)
+#define DRM_MODE_FLAG_PCSYNC   (1<<7)
+#define DRM_MODE_FLAG_NCSYNC   (1<<8)
+#define DRM_MODE_FLAG_HSKEW    (1<<9) /* hskew provided */
+#define DRM_MODE_FLAG_BCAST    (1<<10)
+#define DRM_MODE_FLAG_PIXMUX   (1<<11)
+#define DRM_MODE_FLAG_DBLCLK   (1<<12)
+#define DRM_MODE_FLAG_CLKDIV2  (1<<13)
+
+/* DPMS flags */
+/* bit compatible with the xorg definitions. */
+#define DRM_MODE_DPMS_ON       0
+#define DRM_MODE_DPMS_STANDBY  1
+#define DRM_MODE_DPMS_SUSPEND  2
+#define DRM_MODE_DPMS_OFF      3
+
+/* Scaling mode options */
+#define DRM_MODE_SCALE_NONE            0 /* Unmodified timing (display or
+                                            software can still scale) */
+#define DRM_MODE_SCALE_FULLSCREEN      1 /* Full screen, ignore aspect */
+#define DRM_MODE_SCALE_CENTER          2 /* Centered, no scaling */
+#define DRM_MODE_SCALE_ASPECT          3 /* Full screen, preserve aspect */
+
+/* Dithering mode options */
+#define DRM_MODE_DITHERING_OFF 0
+#define DRM_MODE_DITHERING_ON  1
+#define DRM_MODE_DITHERING_AUTO 2
+
+/* Dirty info options */
+#define DRM_MODE_DIRTY_OFF      0
+#define DRM_MODE_DIRTY_ON       1
+#define DRM_MODE_DIRTY_ANNOTATE 2
+
+struct drm_mode_modeinfo {
+       __u32 clock;
+       __u16 hdisplay, hsync_start, hsync_end, htotal, hskew;
+       __u16 vdisplay, vsync_start, vsync_end, vtotal, vscan;
+
+       __u32 vrefresh;
+
+       __u32 flags;
+       __u32 type;
+       char name[DRM_DISPLAY_MODE_LEN];
+};
+
+struct drm_mode_card_res {
+       __u64 fb_id_ptr;
+       __u64 crtc_id_ptr;
+       __u64 connector_id_ptr;
+       __u64 encoder_id_ptr;
+       __u32 count_fbs;
+       __u32 count_crtcs;
+       __u32 count_connectors;
+       __u32 count_encoders;
+       __u32 min_width, max_width;
+       __u32 min_height, max_height;
+};
+
+struct drm_mode_crtc {
+       __u64 set_connectors_ptr;
+       __u32 count_connectors;
+
+       __u32 crtc_id; /**< Id */
+       __u32 fb_id; /**< Id of framebuffer */
+
+       __u32 x, y; /**< Position on the frameuffer */
+
+       __u32 gamma_size;
+       __u32 mode_valid;
+       struct drm_mode_modeinfo mode;
+};
+
+#define DRM_MODE_PRESENT_TOP_FIELD     (1<<0)
+#define DRM_MODE_PRESENT_BOTTOM_FIELD  (1<<1)
+
+/* Planes blend with or override other bits on the CRTC */
+struct drm_mode_set_plane {
+       __u32 plane_id;
+       __u32 crtc_id;
+       __u32 fb_id; /* fb object contains surface format type */
+       __u32 flags; /* see above flags */
+
+       /* Signed dest location allows it to be partially off screen */
+       __s32 crtc_x, crtc_y;
+       __u32 crtc_w, crtc_h;
+
+       /* Source values are 16.16 fixed point */
+       __u32 src_x, src_y;
+       __u32 src_h, src_w;
+};
+
+struct drm_mode_get_plane {
+       __u32 plane_id;
+
+       __u32 crtc_id;
+       __u32 fb_id;
+
+       __u32 possible_crtcs;
+       __u32 gamma_size;
+
+       __u32 count_format_types;
+       __u64 format_type_ptr;
+};
+
+struct drm_mode_get_plane_res {
+       __u64 plane_id_ptr;
+       __u32 count_planes;
+};
+
+#define DRM_MODE_ENCODER_NONE  0
+#define DRM_MODE_ENCODER_DAC   1
+#define DRM_MODE_ENCODER_TMDS  2
+#define DRM_MODE_ENCODER_LVDS  3
+#define DRM_MODE_ENCODER_TVDAC 4
+
+struct drm_mode_get_encoder {
+       __u32 encoder_id;
+       __u32 encoder_type;
+
+       __u32 crtc_id; /**< Id of crtc */
+
+       __u32 possible_crtcs;
+       __u32 possible_clones;
+};
+
+/* This is for connectors with multiple signal types. */
+/* Try to match DRM_MODE_CONNECTOR_X as closely as possible. */
+#define DRM_MODE_SUBCONNECTOR_Automatic        0
+#define DRM_MODE_SUBCONNECTOR_Unknown  0
+#define DRM_MODE_SUBCONNECTOR_DVID     3
+#define DRM_MODE_SUBCONNECTOR_DVIA     4
+#define DRM_MODE_SUBCONNECTOR_Composite        5
+#define DRM_MODE_SUBCONNECTOR_SVIDEO   6
+#define DRM_MODE_SUBCONNECTOR_Component        8
+#define DRM_MODE_SUBCONNECTOR_SCART    9
+
+#define DRM_MODE_CONNECTOR_Unknown     0
+#define DRM_MODE_CONNECTOR_VGA         1
+#define DRM_MODE_CONNECTOR_DVII                2
+#define DRM_MODE_CONNECTOR_DVID                3
+#define DRM_MODE_CONNECTOR_DVIA                4
+#define DRM_MODE_CONNECTOR_Composite   5
+#define DRM_MODE_CONNECTOR_SVIDEO      6
+#define DRM_MODE_CONNECTOR_LVDS                7
+#define DRM_MODE_CONNECTOR_Component   8
+#define DRM_MODE_CONNECTOR_9PinDIN     9
+#define DRM_MODE_CONNECTOR_DisplayPort 10
+#define DRM_MODE_CONNECTOR_HDMIA       11
+#define DRM_MODE_CONNECTOR_HDMIB       12
+#define DRM_MODE_CONNECTOR_TV          13
+#define DRM_MODE_CONNECTOR_eDP         14
+
+struct drm_mode_get_connector {
+
+       __u64 encoders_ptr;
+       __u64 modes_ptr;
+       __u64 props_ptr;
+       __u64 prop_values_ptr;
+
+       __u32 count_modes;
+       __u32 count_props;
+       __u32 count_encoders;
+
+       __u32 encoder_id; /**< Current Encoder */
+       __u32 connector_id; /**< Id */
+       __u32 connector_type;
+       __u32 connector_type_id;
+
+       __u32 connection;
+       __u32 mm_width, mm_height; /**< HxW in millimeters */
+       __u32 subpixel;
+};
+
+#define DRM_MODE_PROP_PENDING  (1<<0)
+#define DRM_MODE_PROP_RANGE    (1<<1)
+#define DRM_MODE_PROP_IMMUTABLE        (1<<2)
+#define DRM_MODE_PROP_ENUM     (1<<3) /* enumerated type with text strings */
+#define DRM_MODE_PROP_BLOB     (1<<4)
+
+struct drm_mode_property_enum {
+       __u64 value;
+       char name[DRM_PROP_NAME_LEN];
+};
+
+struct drm_mode_get_property {
+       __u64 values_ptr; /* values and blob lengths */
+       __u64 enum_blob_ptr; /* enum and blob id ptrs */
+
+       __u32 prop_id;
+       __u32 flags;
+       char name[DRM_PROP_NAME_LEN];
+
+       __u32 count_values;
+       __u32 count_enum_blobs;
+};
+
+struct drm_mode_connector_set_property {
+       __u64 value;
+       __u32 prop_id;
+       __u32 connector_id;
+};
+
+struct drm_mode_get_blob {
+       __u32 blob_id;
+       __u32 length;
+       __u64 data;
+};
+
+struct drm_mode_fb_cmd {
+       __u32 fb_id;
+       __u32 width, height;
+       __u32 pitch;
+       __u32 bpp;
+       __u32 depth;
+       /* driver specific handle */
+       __u32 handle;
+};
+
+#define DRM_MODE_FB_INTERLACED (1<<0) /* for interlaced framebuffers */
+
+struct drm_mode_fb_cmd2 {
+       __u32 fb_id;
+       __u32 width, height;
+       __u32 pixel_format; /* fourcc code from drm_fourcc.h */
+       __u32 flags; /* see above flags */
+
+       /*
+        * In case of planar formats, this ioctl allows up to 4
+        * buffer objects with offets and pitches per plane.
+        * The pitch and offset order is dictated by the fourcc,
+        * e.g. NV12 (http://fourcc.org/yuv.php#NV12) is described as:
+        *
+        *   YUV 4:2:0 image with a plane of 8 bit Y samples
+        *   followed by an interleaved U/V plane containing
+        *   8 bit 2x2 subsampled colour difference samples.
+        *
+        * So it would consist of Y as offset[0] and UV as
+        * offeset[1].  Note that offset[0] will generally
+        * be 0.
+        */
+       __u32 handles[4];
+       __u32 pitches[4]; /* pitch for each plane */
+       __u32 offsets[4]; /* offset of each plane */
+};
+
+#define DRM_MODE_FB_DIRTY_ANNOTATE_COPY 0x01
+#define DRM_MODE_FB_DIRTY_ANNOTATE_FILL 0x02
+#define DRM_MODE_FB_DIRTY_FLAGS         0x03
+
+/*
+ * Mark a region of a framebuffer as dirty.
+ *
+ * Some hardware does not automatically update display contents
+ * as a hardware or software draw to a framebuffer. This ioctl
+ * allows userspace to tell the kernel and the hardware what
+ * regions of the framebuffer have changed.
+ *
+ * The kernel or hardware is free to update more then just the
+ * region specified by the clip rects. The kernel or hardware
+ * may also delay and/or coalesce several calls to dirty into a
+ * single update.
+ *
+ * Userspace may annotate the updates, the annotates are a
+ * promise made by the caller that the change is either a copy
+ * of pixels or a fill of a single color in the region specified.
+ *
+ * If the DRM_MODE_FB_DIRTY_ANNOTATE_COPY flag is given then
+ * the number of updated regions are half of num_clips given,
+ * where the clip rects are paired in src and dst. The width and
+ * height of each one of the pairs must match.
+ *
+ * If the DRM_MODE_FB_DIRTY_ANNOTATE_FILL flag is given the caller
+ * promises that the region specified of the clip rects is filled
+ * completely with a single color as given in the color argument.
+ */
+
+struct drm_mode_fb_dirty_cmd {
+       __u32 fb_id;
+       __u32 flags;
+       __u32 color;
+       __u32 num_clips;
+       __u64 clips_ptr;
+};
+
+struct drm_mode_mode_cmd {
+       __u32 connector_id;
+       struct drm_mode_modeinfo mode;
+};
+
+#define DRM_MODE_CURSOR_BO     (1<<0)
+#define DRM_MODE_CURSOR_MOVE   (1<<1)
+
+/*
+ * depending on the value in flags different members are used.
+ *
+ * CURSOR_BO uses
+ *    crtc
+ *    width
+ *    height
+ *    handle - if 0 turns the cursor of
+ *
+ * CURSOR_MOVE uses
+ *    crtc
+ *    x
+ *    y
+ */
+struct drm_mode_cursor {
+       __u32 flags;
+       __u32 crtc_id;
+       __s32 x;
+       __s32 y;
+       __u32 width;
+       __u32 height;
+       /* driver specific handle */
+       __u32 handle;
+};
+
+struct drm_mode_crtc_lut {
+       __u32 crtc_id;
+       __u32 gamma_size;
+
+       /* pointers to arrays */
+       __u64 red;
+       __u64 green;
+       __u64 blue;
+};
+
+#define DRM_MODE_PAGE_FLIP_EVENT 0x01
+#define DRM_MODE_PAGE_FLIP_FLAGS DRM_MODE_PAGE_FLIP_EVENT
+
+/*
+ * Request a page flip on the specified crtc.
+ *
+ * This ioctl will ask KMS to schedule a page flip for the specified
+ * crtc.  Once any pending rendering targeting the specified fb (as of
+ * ioctl time) has completed, the crtc will be reprogrammed to display
+ * that fb after the next vertical refresh.  The ioctl returns
+ * immediately, but subsequent rendering to the current fb will block
+ * in the execbuffer ioctl until the page flip happens.  If a page
+ * flip is already pending as the ioctl is called, EBUSY will be
+ * returned.
+ *
+ * The ioctl supports one flag, DRM_MODE_PAGE_FLIP_EVENT, which will
+ * request that drm sends back a vblank event (see drm.h: struct
+ * drm_event_vblank) when the page flip is done.  The user_data field
+ * passed in with this ioctl will be returned as the user_data field
+ * in the vblank event struct.
+ *
+ * The reserved field must be zero until we figure out something
+ * clever to use it for.
+ */
+
+struct drm_mode_crtc_page_flip {
+       __u32 crtc_id;
+       __u32 fb_id;
+       __u32 flags;
+       __u32 reserved;
+       __u64 user_data;
+};
+
+/* create a dumb scanout buffer */
+struct drm_mode_create_dumb {
+       uint32_t height;
+       uint32_t width;
+       uint32_t bpp;
+       uint32_t flags;
+       /* handle, pitch, size will be returned */
+       uint32_t handle;
+       uint32_t pitch;
+       uint64_t size;
+};
+
+/* set up for mmap of a dumb scanout buffer */
+struct drm_mode_map_dumb {
+       /** Handle for the object being mapped. */
+       __u32 handle;
+       __u32 pad;
+       /**
+        * Fake offset to use for subsequent mmap call
+        *
+        * This is a fixed-size type for 32/64 compatibility.
+        */
+       __u64 offset;
+};
+
+struct drm_mode_destroy_dumb {
+       uint32_t handle;
+};
+
+#endif
diff --git a/include/drm/drm_sarea.h b/include/drm/drm_sarea.h
new file mode 100644 (file)
index 0000000..7325558
--- /dev/null
@@ -0,0 +1,82 @@
+/**
+ * \file drm_sarea.h
+ * \brief SAREA definitions
+ *
+ * \author Michel Dänzer <michel@daenzer.net>
+ */
+
+/*
+ * Copyright 2002 Tungsten Graphics, Inc., Cedar Park, Texas.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef _DRM_SAREA_H_
+#define _DRM_SAREA_H_
+
+#include "drm.h"
+
+/* SAREA area needs to be at least a page */
+#if defined(__alpha__)
+#define SAREA_MAX                       0x2000U
+#elif defined(__ia64__)
+#define SAREA_MAX                       0x10000U       /* 64kB */
+#else
+/* Intel 830M driver needs at least 8k SAREA */
+#define SAREA_MAX                       0x2000U
+#endif
+
+/** Maximum number of drawables in the SAREA */
+#define SAREA_MAX_DRAWABLES            256
+
+#define SAREA_DRAWABLE_CLAIMED_ENTRY    0x80000000
+
+/** SAREA drawable */
+struct drm_sarea_drawable {
+       unsigned int stamp;
+       unsigned int flags;
+};
+
+/** SAREA frame */
+struct drm_sarea_frame {
+       unsigned int x;
+       unsigned int y;
+       unsigned int width;
+       unsigned int height;
+       unsigned int fullscreen;
+};
+
+/** SAREA */
+struct drm_sarea {
+    /** first thing is always the DRM locking structure */
+       struct drm_hw_lock lock;
+    /** \todo Use readers/writer lock for drm_sarea::drawable_lock */
+       struct drm_hw_lock drawable_lock;
+       struct drm_sarea_drawable drawableTable[SAREA_MAX_DRAWABLES];   /**< drawables */
+       struct drm_sarea_frame frame;   /**< frame */
+       drm_context_t dummy_context;
+};
+
+typedef struct drm_sarea_drawable drm_sarea_drawable_t;
+typedef struct drm_sarea_frame drm_sarea_frame_t;
+typedef struct drm_sarea drm_sarea_t;
+
+#endif                         /* _DRM_SAREA_H_ */
diff --git a/include/drm/exynos_drm.h b/include/drm/exynos_drm.h
new file mode 100644 (file)
index 0000000..545289b
--- /dev/null
@@ -0,0 +1,205 @@
+/* exynos_drm.h
+ *
+ * Copyright (c) 2011 Samsung Electronics Co., Ltd.
+ * Authors:
+ *     Inki Dae <inki.dae@samsung.com>
+ *     Joonyoung Shim <jy0922.shim@samsung.com>
+ *     Seung-Woo Kim <sw0312.kim@samsung.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef _EXYNOS_DRM_H_
+#define _EXYNOS_DRM_H_
+
+/**
+ * User-desired buffer creation information structure.
+ *
+ * @size: user-desired memory allocation size.
+ *     - this size value would be page-aligned internally.
+ * @flags: user request for setting memory type or cache attributes.
+ * @handle: returned a handle to created gem object.
+ *     - this handle will be set by gem module of kernel side.
+ */
+struct drm_exynos_gem_create {
+       uint64_t size;
+       unsigned int flags;
+       unsigned int handle;
+};
+
+/**
+ * User-desired user ptr import structure.
+ *
+ * @size: a pointer of size values to each buffer pointed by each user_ptr.
+ * @user_ptr: points to user space address mmaped.
+ * @handle: a pointer of gem handles.
+ *     this variable would be set by gem framework of kernel side.
+ *
+ * this structure would be used to create new gem and the gem includes
+ * physical address corresponding to user_ptr. the purpose of using this
+ * structure is to create new gem object corresponding to user_ptr.
+ */
+struct drm_exynos_gem_userptr_imp {
+       unsigned int size;
+       uint64_t user_ptr;
+       unsigned int handle;
+};
+
+/**
+ * A structure for getting buffer offset.
+ *
+ * @handle: a pointer to gem object created.
+ * @pad: just padding to be 64-bit aligned.
+ * @offset: relatived offset value of the memory region allocated.
+ *     - this value should be set by user.
+ */
+struct drm_exynos_gem_map_off {
+       unsigned int handle;
+       unsigned int pad;
+       uint64_t offset;
+};
+
+/**
+ * A structure for mapping buffer.
+ *
+ * @handle: a handle to gem object created.
+ * @pad: just padding to be 64-bit aligned.
+ * @size: memory size to be mapped.
+ * @mapped: having user virtual address mmaped.
+ *     - this variable would be filled by exynos gem module
+ *     of kernel side with user virtual address which is allocated
+ *     by do_mmap().
+ */
+struct drm_exynos_gem_mmap {
+       unsigned int handle;
+       unsigned int pad;
+       uint64_t size;
+       uint64_t mapped;
+};
+
+/**
+ * A structure for ump.
+ *
+ * @gem_handle: a pointer to gem object created.
+ * @secure_id: ump secure id and this value would be filled
+ *             by kernel side.
+ */
+struct drm_exynos_gem_ump {
+       unsigned int gem_handle;
+       unsigned int secure_id;
+};
+
+
+/* temporary codes for legacy fimc and mfc drivers. */
+
+/**
+ * A structure for getting physical address corresponding to a gem handle.
+ */
+struct drm_exynos_gem_get_phy {
+       unsigned int gem_handle;
+       unsigned int pad;
+       uint64_t size;
+       uint64_t phy_addr;
+};
+
+/**
+ * A structure for importing physical memory to a gem.
+ */
+struct drm_exynos_gem_phy_imp {
+       uint64_t phy_addr;
+       uint64_t size;
+       unsigned int gem_handle;
+       unsigned int pad;
+};
+
+/* indicate cache units. */
+enum e_drm_exynos_gem_cache_sel {
+       EXYNOS_DRM_L1_CACHE     = 1,
+       EXYNOS_DRM_L2_CACHE     = 2,
+       EXYNOS_DRM_ALL_CACHE    = 3
+};
+
+/* indicate cache operation types. */
+enum e_drm_exynos_gem_cache_op {
+       EXYNOS_DRM_CACHE_INV    = 4,
+       EXYNOS_DRM_CACHE_CLN    = 8,
+       EXYNOS_DRM_CACHE_FSH    = 0xC
+};
+
+/**
+ * A structure for cache operation.
+ *
+ * @usr_addr: user space address.
+ *     P.S. it SHOULD BE user space.
+ * @size: buffer size for cache operation.
+ * @flags: select cache unit and cache operation.
+ */
+struct drm_exynos_gem_cache_op {
+       uint64_t usr_addr;
+       unsigned int size;
+       unsigned int flags;
+};
+
+struct drm_exynos_plane_set_zpos {
+       __u32 plane_id;
+       __s32 zpos;
+};
+
+#define DRM_EXYNOS_GEM_CREATE          0x00
+#define DRM_EXYNOS_GEM_MAP_OFFSET      0x01
+#define DRM_EXYNOS_GEM_MMAP            0x02
+#define DRM_EXYNOS_GEM_USERPTR_IMP     0x03
+#define DRM_EXYNOS_PLANE_SET_ZPOS      0x05
+
+/* temporary ioctl command. */
+#define DRM_EXYNOS_GEM_EXPORT_UMP      0x10
+#define DRM_EXYNOS_GEM_CACHE_OP                0x12
+
+#define DRM_EXYNOS_GEM_GET_PHY         0x13
+#define DRM_EXYNOS_GEM_PHY_IMP         0x14
+
+#define DRM_IOCTL_EXYNOS_GEM_CREATE            DRM_IOWR(DRM_COMMAND_BASE + \
+               DRM_EXYNOS_GEM_CREATE, struct drm_exynos_gem_create)
+
+#define DRM_IOCTL_EXYNOS_GEM_MAP_OFFSET        DRM_IOWR(DRM_COMMAND_BASE + \
+               DRM_EXYNOS_GEM_MAP_OFFSET, struct drm_exynos_gem_map_off)
+
+#define DRM_IOCTL_EXYNOS_GEM_MMAP      DRM_IOWR(DRM_COMMAND_BASE + \
+               DRM_EXYNOS_GEM_MMAP, struct drm_exynos_gem_mmap)
+
+#define DRM_IOCTL_EXYNOS_GEM_USERPTR_IMP       DRM_IOWR(DRM_COMMAND_BASE + \
+               DRM_EXYNOS_GEM_USERPTR_IMP, struct drm_exynos_gem_userptr_imp)
+
+#define DRM_IOCTL_EXYNOS_GEM_EXPORT_UMP        DRM_IOWR(DRM_COMMAND_BASE + \
+               DRM_EXYNOS_GEM_EXPORT_UMP, struct drm_exynos_gem_ump)
+
+#define DRM_IOCTL_EXYNOS_GEM_CACHE_OP  DRM_IOWR(DRM_COMMAND_BASE + \
+               DRM_EXYNOS_GEM_CACHE_OP, struct drm_exynos_gem_cache_op)
+
+/* temporary ioctl command. */
+#define DRM_IOCTL_EXYNOS_GEM_GET_PHY   DRM_IOWR(DRM_COMMAND_BASE + \
+               DRM_EXYNOS_GEM_GET_PHY, struct drm_exynos_gem_get_phy)
+#define DRM_IOCTL_EXYNOS_GEM_PHY_IMP   DRM_IOWR(DRM_COMMAND_BASE + \
+               DRM_EXYNOS_GEM_PHY_IMP, struct drm_exynos_gem_phy_imp)
+
+#define DRM_IOCTL_EXYNOS_PLANE_SET_ZPOS        DRM_IOWR(DRM_COMMAND_BASE + \
+               DRM_EXYNOS_PLANE_SET_ZPOS, struct drm_exynos_plane_set_zpos)
+
+#endif
diff --git a/include/drm/i810_drm.h b/include/drm/i810_drm.h
new file mode 100644 (file)
index 0000000..7a10bb6
--- /dev/null
@@ -0,0 +1,281 @@
+#ifndef _I810_DRM_H_
+#define _I810_DRM_H_
+
+/* WARNING: These defines must be the same as what the Xserver uses.
+ * if you change them, you must change the defines in the Xserver.
+ */
+
+#ifndef _I810_DEFINES_
+#define _I810_DEFINES_
+
+#define I810_DMA_BUF_ORDER             12
+#define I810_DMA_BUF_SZ                (1<<I810_DMA_BUF_ORDER)
+#define I810_DMA_BUF_NR                256
+#define I810_NR_SAREA_CLIPRECTS        8
+
+/* Each region is a minimum of 64k, and there are at most 64 of them.
+ */
+#define I810_NR_TEX_REGIONS 64
+#define I810_LOG_MIN_TEX_REGION_SIZE 16
+#endif
+
+#define I810_UPLOAD_TEX0IMAGE  0x1     /* handled clientside */
+#define I810_UPLOAD_TEX1IMAGE  0x2     /* handled clientside */
+#define I810_UPLOAD_CTX        0x4
+#define I810_UPLOAD_BUFFERS    0x8
+#define I810_UPLOAD_TEX0       0x10
+#define I810_UPLOAD_TEX1       0x20
+#define I810_UPLOAD_CLIPRECTS  0x40
+
+/* Indices into buf.Setup where various bits of state are mirrored per
+ * context and per buffer.  These can be fired at the card as a unit,
+ * or in a piecewise fashion as required.
+ */
+
+/* Destbuffer state
+ *    - backbuffer linear offset and pitch -- invarient in the current dri
+ *    - zbuffer linear offset and pitch -- also invarient
+ *    - drawing origin in back and depth buffers.
+ *
+ * Keep the depth/back buffer state here to accommodate private buffers
+ * in the future.
+ */
+#define I810_DESTREG_DI0  0    /* CMD_OP_DESTBUFFER_INFO (2 dwords) */
+#define I810_DESTREG_DI1  1
+#define I810_DESTREG_DV0  2    /* GFX_OP_DESTBUFFER_VARS (2 dwords) */
+#define I810_DESTREG_DV1  3
+#define I810_DESTREG_DR0  4    /* GFX_OP_DRAWRECT_INFO (4 dwords) */
+#define I810_DESTREG_DR1  5
+#define I810_DESTREG_DR2  6
+#define I810_DESTREG_DR3  7
+#define I810_DESTREG_DR4  8
+#define I810_DEST_SETUP_SIZE 10
+
+/* Context state
+ */
+#define I810_CTXREG_CF0   0    /* GFX_OP_COLOR_FACTOR */
+#define I810_CTXREG_CF1   1
+#define I810_CTXREG_ST0   2    /* GFX_OP_STIPPLE */
+#define I810_CTXREG_ST1   3
+#define I810_CTXREG_VF    4    /* GFX_OP_VERTEX_FMT */
+#define I810_CTXREG_MT    5    /* GFX_OP_MAP_TEXELS */
+#define I810_CTXREG_MC0   6    /* GFX_OP_MAP_COLOR_STAGES - stage 0 */
+#define I810_CTXREG_MC1   7    /* GFX_OP_MAP_COLOR_STAGES - stage 1 */
+#define I810_CTXREG_MC2   8    /* GFX_OP_MAP_COLOR_STAGES - stage 2 */
+#define I810_CTXREG_MA0   9    /* GFX_OP_MAP_ALPHA_STAGES - stage 0 */
+#define I810_CTXREG_MA1   10   /* GFX_OP_MAP_ALPHA_STAGES - stage 1 */
+#define I810_CTXREG_MA2   11   /* GFX_OP_MAP_ALPHA_STAGES - stage 2 */
+#define I810_CTXREG_SDM   12   /* GFX_OP_SRC_DEST_MONO */
+#define I810_CTXREG_FOG   13   /* GFX_OP_FOG_COLOR */
+#define I810_CTXREG_B1    14   /* GFX_OP_BOOL_1 */
+#define I810_CTXREG_B2    15   /* GFX_OP_BOOL_2 */
+#define I810_CTXREG_LCS   16   /* GFX_OP_LINEWIDTH_CULL_SHADE_MODE */
+#define I810_CTXREG_PV    17   /* GFX_OP_PV_RULE -- Invarient! */
+#define I810_CTXREG_ZA    18   /* GFX_OP_ZBIAS_ALPHAFUNC */
+#define I810_CTXREG_AA    19   /* GFX_OP_ANTIALIAS */
+#define I810_CTX_SETUP_SIZE 20
+
+/* Texture state (per tex unit)
+ */
+#define I810_TEXREG_MI0  0     /* GFX_OP_MAP_INFO (4 dwords) */
+#define I810_TEXREG_MI1  1
+#define I810_TEXREG_MI2  2
+#define I810_TEXREG_MI3  3
+#define I810_TEXREG_MF   4     /* GFX_OP_MAP_FILTER */
+#define I810_TEXREG_MLC  5     /* GFX_OP_MAP_LOD_CTL */
+#define I810_TEXREG_MLL  6     /* GFX_OP_MAP_LOD_LIMITS */
+#define I810_TEXREG_MCS  7     /* GFX_OP_MAP_COORD_SETS ??? */
+#define I810_TEX_SETUP_SIZE 8
+
+/* Flags for clear ioctl
+ */
+#define I810_FRONT   0x1
+#define I810_BACK    0x2
+#define I810_DEPTH   0x4
+
+typedef enum _drm_i810_init_func {
+       I810_INIT_DMA = 0x01,
+       I810_CLEANUP_DMA = 0x02,
+       I810_INIT_DMA_1_4 = 0x03
+} drm_i810_init_func_t;
+
+/* This is the init structure after v1.2 */
+typedef struct _drm_i810_init {
+       drm_i810_init_func_t func;
+       unsigned int mmio_offset;
+       unsigned int buffers_offset;
+       int sarea_priv_offset;
+       unsigned int ring_start;
+       unsigned int ring_end;
+       unsigned int ring_size;
+       unsigned int front_offset;
+       unsigned int back_offset;
+       unsigned int depth_offset;
+       unsigned int overlay_offset;
+       unsigned int overlay_physical;
+       unsigned int w;
+       unsigned int h;
+       unsigned int pitch;
+       unsigned int pitch_bits;
+} drm_i810_init_t;
+
+/* This is the init structure prior to v1.2 */
+typedef struct _drm_i810_pre12_init {
+       drm_i810_init_func_t func;
+       unsigned int mmio_offset;
+       unsigned int buffers_offset;
+       int sarea_priv_offset;
+       unsigned int ring_start;
+       unsigned int ring_end;
+       unsigned int ring_size;
+       unsigned int front_offset;
+       unsigned int back_offset;
+       unsigned int depth_offset;
+       unsigned int w;
+       unsigned int h;
+       unsigned int pitch;
+       unsigned int pitch_bits;
+} drm_i810_pre12_init_t;
+
+/* Warning: If you change the SAREA structure you must change the Xserver
+ * structure as well */
+
+typedef struct _drm_i810_tex_region {
+       unsigned char next, prev;       /* indices to form a circular LRU  */
+       unsigned char in_use;   /* owned by a client, or free? */
+       int age;                /* tracked by clients to update local LRU's */
+} drm_i810_tex_region_t;
+
+typedef struct _drm_i810_sarea {
+       unsigned int ContextState[I810_CTX_SETUP_SIZE];
+       unsigned int BufferState[I810_DEST_SETUP_SIZE];
+       unsigned int TexState[2][I810_TEX_SETUP_SIZE];
+       unsigned int dirty;
+
+       unsigned int nbox;
+       struct drm_clip_rect boxes[I810_NR_SAREA_CLIPRECTS];
+
+       /* Maintain an LRU of contiguous regions of texture space.  If
+        * you think you own a region of texture memory, and it has an
+        * age different to the one you set, then you are mistaken and
+        * it has been stolen by another client.  If global texAge
+        * hasn't changed, there is no need to walk the list.
+        *
+        * These regions can be used as a proxy for the fine-grained
+        * texture information of other clients - by maintaining them
+        * in the same lru which is used to age their own textures,
+        * clients have an approximate lru for the whole of global
+        * texture space, and can make informed decisions as to which
+        * areas to kick out.  There is no need to choose whether to
+        * kick out your own texture or someone else's - simply eject
+        * them all in LRU order.
+        */
+
+       drm_i810_tex_region_t texList[I810_NR_TEX_REGIONS + 1];
+       /* Last elt is sentinal */
+       int texAge;             /* last time texture was uploaded */
+       int last_enqueue;       /* last time a buffer was enqueued */
+       int last_dispatch;      /* age of the most recently dispatched buffer */
+       int last_quiescent;     /*  */
+       int ctxOwner;           /* last context to upload state */
+
+       int vertex_prim;
+
+       int pf_enabled;         /* is pageflipping allowed? */
+       int pf_active;
+       int pf_current_page;    /* which buffer is being displayed? */
+} drm_i810_sarea_t;
+
+/* WARNING: If you change any of these defines, make sure to change the
+ * defines in the Xserver file (xf86drmMga.h)
+ */
+
+/* i810 specific ioctls
+ * The device specific ioctl range is 0x40 to 0x79.
+ */
+#define DRM_I810_INIT          0x00
+#define DRM_I810_VERTEX                0x01
+#define DRM_I810_CLEAR         0x02
+#define DRM_I810_FLUSH         0x03
+#define DRM_I810_GETAGE                0x04
+#define DRM_I810_GETBUF                0x05
+#define DRM_I810_SWAP          0x06
+#define DRM_I810_COPY          0x07
+#define DRM_I810_DOCOPY                0x08
+#define DRM_I810_OV0INFO       0x09
+#define DRM_I810_FSTATUS       0x0a
+#define DRM_I810_OV0FLIP       0x0b
+#define DRM_I810_MC            0x0c
+#define DRM_I810_RSTATUS       0x0d
+#define DRM_I810_FLIP          0x0e
+
+#define DRM_IOCTL_I810_INIT            DRM_IOW( DRM_COMMAND_BASE + DRM_I810_INIT, drm_i810_init_t)
+#define DRM_IOCTL_I810_VERTEX          DRM_IOW( DRM_COMMAND_BASE + DRM_I810_VERTEX, drm_i810_vertex_t)
+#define DRM_IOCTL_I810_CLEAR           DRM_IOW( DRM_COMMAND_BASE + DRM_I810_CLEAR, drm_i810_clear_t)
+#define DRM_IOCTL_I810_FLUSH           DRM_IO(  DRM_COMMAND_BASE + DRM_I810_FLUSH)
+#define DRM_IOCTL_I810_GETAGE          DRM_IO(  DRM_COMMAND_BASE + DRM_I810_GETAGE)
+#define DRM_IOCTL_I810_GETBUF          DRM_IOWR(DRM_COMMAND_BASE + DRM_I810_GETBUF, drm_i810_dma_t)
+#define DRM_IOCTL_I810_SWAP            DRM_IO(  DRM_COMMAND_BASE + DRM_I810_SWAP)
+#define DRM_IOCTL_I810_COPY            DRM_IOW( DRM_COMMAND_BASE + DRM_I810_COPY, drm_i810_copy_t)
+#define DRM_IOCTL_I810_DOCOPY          DRM_IO(  DRM_COMMAND_BASE + DRM_I810_DOCOPY)
+#define DRM_IOCTL_I810_OV0INFO         DRM_IOR( DRM_COMMAND_BASE + DRM_I810_OV0INFO, drm_i810_overlay_t)
+#define DRM_IOCTL_I810_FSTATUS         DRM_IO ( DRM_COMMAND_BASE + DRM_I810_FSTATUS)
+#define DRM_IOCTL_I810_OV0FLIP         DRM_IO ( DRM_COMMAND_BASE + DRM_I810_OV0FLIP)
+#define DRM_IOCTL_I810_MC              DRM_IOW( DRM_COMMAND_BASE + DRM_I810_MC, drm_i810_mc_t)
+#define DRM_IOCTL_I810_RSTATUS         DRM_IO ( DRM_COMMAND_BASE + DRM_I810_RSTATUS)
+#define DRM_IOCTL_I810_FLIP             DRM_IO ( DRM_COMMAND_BASE + DRM_I810_FLIP)
+
+typedef struct _drm_i810_clear {
+       int clear_color;
+       int clear_depth;
+       int flags;
+} drm_i810_clear_t;
+
+/* These may be placeholders if we have more cliprects than
+ * I810_NR_SAREA_CLIPRECTS.  In that case, the client sets discard to
+ * false, indicating that the buffer will be dispatched again with a
+ * new set of cliprects.
+ */
+typedef struct _drm_i810_vertex {
+       int idx;                /* buffer index */
+       int used;               /* nr bytes in use */
+       int discard;            /* client is finished with the buffer? */
+} drm_i810_vertex_t;
+
+typedef struct _drm_i810_copy_t {
+       int idx;                /* buffer index */
+       int used;               /* nr bytes in use */
+       void *address;          /* Address to copy from */
+} drm_i810_copy_t;
+
+#define PR_TRIANGLES         (0x0<<18)
+#define PR_TRISTRIP_0        (0x1<<18)
+#define PR_TRISTRIP_1        (0x2<<18)
+#define PR_TRIFAN            (0x3<<18)
+#define PR_POLYGON           (0x4<<18)
+#define PR_LINES             (0x5<<18)
+#define PR_LINESTRIP         (0x6<<18)
+#define PR_RECTS             (0x7<<18)
+#define PR_MASK              (0x7<<18)
+
+typedef struct drm_i810_dma {
+       void *virtual;
+       int request_idx;
+       int request_size;
+       int granted;
+} drm_i810_dma_t;
+
+typedef struct _drm_i810_overlay_t {
+       unsigned int offset;    /* Address of the Overlay Regs */
+       unsigned int physical;
+} drm_i810_overlay_t;
+
+typedef struct _drm_i810_mc {
+       int idx;                /* buffer index */
+       int used;               /* nr bytes in use */
+       int num_blocks;         /* number of GFXBlocks */
+       int *length;            /* List of lengths for GFXBlocks (FUTURE) */
+       unsigned int last_render;       /* Last Render Request */
+} drm_i810_mc_t;
+
+#endif                         /* _I810_DRM_H_ */
diff --git a/include/drm/i830_drm.h b/include/drm/i830_drm.h
new file mode 100644 (file)
index 0000000..e1b08e4
--- /dev/null
@@ -0,0 +1,342 @@
+#ifndef _I830_DRM_H_
+#define _I830_DRM_H_
+
+/* WARNING: These defines must be the same as what the Xserver uses.
+ * if you change them, you must change the defines in the Xserver.
+ *
+ * KW: Actually, you can't ever change them because doing so would
+ * break backwards compatibility.
+ */
+
+#ifndef _I830_DEFINES_
+#define _I830_DEFINES_
+
+#define I830_DMA_BUF_ORDER             12
+#define I830_DMA_BUF_SZ                        (1<<I830_DMA_BUF_ORDER)
+#define I830_DMA_BUF_NR                        256
+#define I830_NR_SAREA_CLIPRECTS                8
+
+/* Each region is a minimum of 64k, and there are at most 64 of them.
+ */
+#define I830_NR_TEX_REGIONS 64
+#define I830_LOG_MIN_TEX_REGION_SIZE 16
+
+/* KW: These aren't correct but someone set them to two and then
+ * released the module.  Now we can't change them as doing so would
+ * break backwards compatibility.
+ */
+#define I830_TEXTURE_COUNT     2
+#define I830_TEXBLEND_COUNT    I830_TEXTURE_COUNT
+
+#define I830_TEXBLEND_SIZE     12      /* (4 args + op) * 2 + COLOR_FACTOR */
+
+#define I830_UPLOAD_CTX                        0x1
+#define I830_UPLOAD_BUFFERS            0x2
+#define I830_UPLOAD_CLIPRECTS          0x4
+#define I830_UPLOAD_TEX0_IMAGE         0x100   /* handled clientside */
+#define I830_UPLOAD_TEX0_CUBE          0x200   /* handled clientside */
+#define I830_UPLOAD_TEX1_IMAGE         0x400   /* handled clientside */
+#define I830_UPLOAD_TEX1_CUBE          0x800   /* handled clientside */
+#define I830_UPLOAD_TEX2_IMAGE         0x1000  /* handled clientside */
+#define I830_UPLOAD_TEX2_CUBE          0x2000  /* handled clientside */
+#define I830_UPLOAD_TEX3_IMAGE         0x4000  /* handled clientside */
+#define I830_UPLOAD_TEX3_CUBE          0x8000  /* handled clientside */
+#define I830_UPLOAD_TEX_N_IMAGE(n)     (0x100 << (n * 2))
+#define I830_UPLOAD_TEX_N_CUBE(n)      (0x200 << (n * 2))
+#define I830_UPLOAD_TEXIMAGE_MASK      0xff00
+#define I830_UPLOAD_TEX0                       0x10000
+#define I830_UPLOAD_TEX1                       0x20000
+#define I830_UPLOAD_TEX2                       0x40000
+#define I830_UPLOAD_TEX3                       0x80000
+#define I830_UPLOAD_TEX_N(n)           (0x10000 << (n))
+#define I830_UPLOAD_TEX_MASK           0xf0000
+#define I830_UPLOAD_TEXBLEND0          0x100000
+#define I830_UPLOAD_TEXBLEND1          0x200000
+#define I830_UPLOAD_TEXBLEND2          0x400000
+#define I830_UPLOAD_TEXBLEND3          0x800000
+#define I830_UPLOAD_TEXBLEND_N(n)      (0x100000 << (n))
+#define I830_UPLOAD_TEXBLEND_MASK      0xf00000
+#define I830_UPLOAD_TEX_PALETTE_N(n)    (0x1000000 << (n))
+#define I830_UPLOAD_TEX_PALETTE_SHARED 0x4000000
+#define I830_UPLOAD_STIPPLE            0x8000000
+
+/* Indices into buf.Setup where various bits of state are mirrored per
+ * context and per buffer.  These can be fired at the card as a unit,
+ * or in a piecewise fashion as required.
+ */
+
+/* Destbuffer state
+ *    - backbuffer linear offset and pitch -- invarient in the current dri
+ *    - zbuffer linear offset and pitch -- also invarient
+ *    - drawing origin in back and depth buffers.
+ *
+ * Keep the depth/back buffer state here to accommodate private buffers
+ * in the future.
+ */
+
+#define I830_DESTREG_CBUFADDR 0
+#define I830_DESTREG_DBUFADDR 1
+#define I830_DESTREG_DV0 2
+#define I830_DESTREG_DV1 3
+#define I830_DESTREG_SENABLE 4
+#define I830_DESTREG_SR0 5
+#define I830_DESTREG_SR1 6
+#define I830_DESTREG_SR2 7
+#define I830_DESTREG_DR0 8
+#define I830_DESTREG_DR1 9
+#define I830_DESTREG_DR2 10
+#define I830_DESTREG_DR3 11
+#define I830_DESTREG_DR4 12
+#define I830_DEST_SETUP_SIZE 13
+
+/* Context state
+ */
+#define I830_CTXREG_STATE1             0
+#define I830_CTXREG_STATE2             1
+#define I830_CTXREG_STATE3             2
+#define I830_CTXREG_STATE4             3
+#define I830_CTXREG_STATE5             4
+#define I830_CTXREG_IALPHAB            5
+#define I830_CTXREG_STENCILTST         6
+#define I830_CTXREG_ENABLES_1          7
+#define I830_CTXREG_ENABLES_2          8
+#define I830_CTXREG_AA                 9
+#define I830_CTXREG_FOGCOLOR           10
+#define I830_CTXREG_BLENDCOLR0         11
+#define I830_CTXREG_BLENDCOLR          12      /* Dword 1 of 2 dword command */
+#define I830_CTXREG_VF                 13
+#define I830_CTXREG_VF2                        14
+#define I830_CTXREG_MCSB0              15
+#define I830_CTXREG_MCSB1              16
+#define I830_CTX_SETUP_SIZE            17
+
+/* 1.3: Stipple state
+ */
+#define I830_STPREG_ST0 0
+#define I830_STPREG_ST1 1
+#define I830_STP_SETUP_SIZE 2
+
+/* Texture state (per tex unit)
+ */
+
+#define I830_TEXREG_MI0        0       /* GFX_OP_MAP_INFO (6 dwords) */
+#define I830_TEXREG_MI1        1
+#define I830_TEXREG_MI2        2
+#define I830_TEXREG_MI3        3
+#define I830_TEXREG_MI4        4
+#define I830_TEXREG_MI5        5
+#define I830_TEXREG_MF 6       /* GFX_OP_MAP_FILTER */
+#define I830_TEXREG_MLC        7       /* GFX_OP_MAP_LOD_CTL */
+#define I830_TEXREG_MLL        8       /* GFX_OP_MAP_LOD_LIMITS */
+#define I830_TEXREG_MCS        9       /* GFX_OP_MAP_COORD_SETS */
+#define I830_TEX_SETUP_SIZE 10
+
+#define I830_TEXREG_TM0LI      0       /* load immediate 2 texture map n */
+#define I830_TEXREG_TM0S0      1
+#define I830_TEXREG_TM0S1      2
+#define I830_TEXREG_TM0S2      3
+#define I830_TEXREG_TM0S3      4
+#define I830_TEXREG_TM0S4      5
+#define I830_TEXREG_NOP0       6       /* noop */
+#define I830_TEXREG_NOP1       7       /* noop */
+#define I830_TEXREG_NOP2       8       /* noop */
+#define __I830_TEXREG_MCS      9       /* GFX_OP_MAP_COORD_SETS -- shared */
+#define __I830_TEX_SETUP_SIZE   10
+
+#define I830_FRONT   0x1
+#define I830_BACK    0x2
+#define I830_DEPTH   0x4
+
+#endif                         /* _I830_DEFINES_ */
+
+typedef struct _drm_i830_init {
+       enum {
+               I830_INIT_DMA = 0x01,
+               I830_CLEANUP_DMA = 0x02
+       } func;
+       unsigned int mmio_offset;
+       unsigned int buffers_offset;
+       int sarea_priv_offset;
+       unsigned int ring_start;
+       unsigned int ring_end;
+       unsigned int ring_size;
+       unsigned int front_offset;
+       unsigned int back_offset;
+       unsigned int depth_offset;
+       unsigned int w;
+       unsigned int h;
+       unsigned int pitch;
+       unsigned int pitch_bits;
+       unsigned int back_pitch;
+       unsigned int depth_pitch;
+       unsigned int cpp;
+} drm_i830_init_t;
+
+/* Warning: If you change the SAREA structure you must change the Xserver
+ * structure as well */
+
+typedef struct _drm_i830_tex_region {
+       unsigned char next, prev;       /* indices to form a circular LRU  */
+       unsigned char in_use;   /* owned by a client, or free? */
+       int age;                /* tracked by clients to update local LRU's */
+} drm_i830_tex_region_t;
+
+typedef struct _drm_i830_sarea {
+       unsigned int ContextState[I830_CTX_SETUP_SIZE];
+       unsigned int BufferState[I830_DEST_SETUP_SIZE];
+       unsigned int TexState[I830_TEXTURE_COUNT][I830_TEX_SETUP_SIZE];
+       unsigned int TexBlendState[I830_TEXBLEND_COUNT][I830_TEXBLEND_SIZE];
+       unsigned int TexBlendStateWordsUsed[I830_TEXBLEND_COUNT];
+       unsigned int Palette[2][256];
+       unsigned int dirty;
+
+       unsigned int nbox;
+       struct drm_clip_rect boxes[I830_NR_SAREA_CLIPRECTS];
+
+       /* Maintain an LRU of contiguous regions of texture space.  If
+        * you think you own a region of texture memory, and it has an
+        * age different to the one you set, then you are mistaken and
+        * it has been stolen by another client.  If global texAge
+        * hasn't changed, there is no need to walk the list.
+        *
+        * These regions can be used as a proxy for the fine-grained
+        * texture information of other clients - by maintaining them
+        * in the same lru which is used to age their own textures,
+        * clients have an approximate lru for the whole of global
+        * texture space, and can make informed decisions as to which
+        * areas to kick out.  There is no need to choose whether to
+        * kick out your own texture or someone else's - simply eject
+        * them all in LRU order.
+        */
+
+       drm_i830_tex_region_t texList[I830_NR_TEX_REGIONS + 1];
+       /* Last elt is sentinal */
+       int texAge;             /* last time texture was uploaded */
+       int last_enqueue;       /* last time a buffer was enqueued */
+       int last_dispatch;      /* age of the most recently dispatched buffer */
+       int last_quiescent;     /*  */
+       int ctxOwner;           /* last context to upload state */
+
+       int vertex_prim;
+
+       int pf_enabled;         /* is pageflipping allowed? */
+       int pf_active;
+       int pf_current_page;    /* which buffer is being displayed? */
+
+       int perf_boxes;         /* performance boxes to be displayed */
+
+       /* Here's the state for texunits 2,3:
+        */
+       unsigned int TexState2[I830_TEX_SETUP_SIZE];
+       unsigned int TexBlendState2[I830_TEXBLEND_SIZE];
+       unsigned int TexBlendStateWordsUsed2;
+
+       unsigned int TexState3[I830_TEX_SETUP_SIZE];
+       unsigned int TexBlendState3[I830_TEXBLEND_SIZE];
+       unsigned int TexBlendStateWordsUsed3;
+
+       unsigned int StippleState[I830_STP_SETUP_SIZE];
+} drm_i830_sarea_t;
+
+/* Flags for perf_boxes
+ */
+#define I830_BOX_RING_EMPTY    0x1     /* populated by kernel */
+#define I830_BOX_FLIP          0x2     /* populated by kernel */
+#define I830_BOX_WAIT          0x4     /* populated by kernel & client */
+#define I830_BOX_TEXTURE_LOAD  0x8     /* populated by kernel */
+#define I830_BOX_LOST_CONTEXT  0x10    /* populated by client */
+
+/* I830 specific ioctls
+ * The device specific ioctl range is 0x40 to 0x79.
+ */
+#define DRM_I830_INIT  0x00
+#define DRM_I830_VERTEX        0x01
+#define DRM_I830_CLEAR 0x02
+#define DRM_I830_FLUSH 0x03
+#define DRM_I830_GETAGE        0x04
+#define DRM_I830_GETBUF        0x05
+#define DRM_I830_SWAP  0x06
+#define DRM_I830_COPY  0x07
+#define DRM_I830_DOCOPY        0x08
+#define DRM_I830_FLIP  0x09
+#define DRM_I830_IRQ_EMIT      0x0a
+#define DRM_I830_IRQ_WAIT      0x0b
+#define DRM_I830_GETPARAM      0x0c
+#define DRM_I830_SETPARAM      0x0d
+
+#define DRM_IOCTL_I830_INIT            DRM_IOW( DRM_COMMAND_BASE + DRM_IOCTL_I830_INIT, drm_i830_init_t)
+#define DRM_IOCTL_I830_VERTEX          DRM_IOW( DRM_COMMAND_BASE + DRM_IOCTL_I830_VERTEX, drm_i830_vertex_t)
+#define DRM_IOCTL_I830_CLEAR           DRM_IOW( DRM_COMMAND_BASE + DRM_IOCTL_I830_CLEAR, drm_i830_clear_t)
+#define DRM_IOCTL_I830_FLUSH           DRM_IO ( DRM_COMMAND_BASE + DRM_IOCTL_I830_FLUSH)
+#define DRM_IOCTL_I830_GETAGE          DRM_IO ( DRM_COMMAND_BASE + DRM_IOCTL_I830_GETAGE)
+#define DRM_IOCTL_I830_GETBUF          DRM_IOWR(DRM_COMMAND_BASE + DRM_IOCTL_I830_GETBUF, drm_i830_dma_t)
+#define DRM_IOCTL_I830_SWAP            DRM_IO ( DRM_COMMAND_BASE + DRM_IOCTL_I830_SWAP)
+#define DRM_IOCTL_I830_COPY            DRM_IOW( DRM_COMMAND_BASE + DRM_IOCTL_I830_COPY, drm_i830_copy_t)
+#define DRM_IOCTL_I830_DOCOPY          DRM_IO ( DRM_COMMAND_BASE + DRM_IOCTL_I830_DOCOPY)
+#define DRM_IOCTL_I830_FLIP            DRM_IO ( DRM_COMMAND_BASE + DRM_IOCTL_I830_FLIP)
+#define DRM_IOCTL_I830_IRQ_EMIT         DRM_IOWR(DRM_COMMAND_BASE + DRM_IOCTL_I830_IRQ_EMIT, drm_i830_irq_emit_t)
+#define DRM_IOCTL_I830_IRQ_WAIT         DRM_IOW( DRM_COMMAND_BASE + DRM_IOCTL_I830_IRQ_WAIT, drm_i830_irq_wait_t)
+#define DRM_IOCTL_I830_GETPARAM         DRM_IOWR(DRM_COMMAND_BASE + DRM_IOCTL_I830_GETPARAM, drm_i830_getparam_t)
+#define DRM_IOCTL_I830_SETPARAM         DRM_IOWR(DRM_COMMAND_BASE + DRM_IOCTL_I830_SETPARAM, drm_i830_setparam_t)
+
+typedef struct _drm_i830_clear {
+       int clear_color;
+       int clear_depth;
+       int flags;
+       unsigned int clear_colormask;
+       unsigned int clear_depthmask;
+} drm_i830_clear_t;
+
+/* These may be placeholders if we have more cliprects than
+ * I830_NR_SAREA_CLIPRECTS.  In that case, the client sets discard to
+ * false, indicating that the buffer will be dispatched again with a
+ * new set of cliprects.
+ */
+typedef struct _drm_i830_vertex {
+       int idx;                /* buffer index */
+       int used;               /* nr bytes in use */
+       int discard;            /* client is finished with the buffer? */
+} drm_i830_vertex_t;
+
+typedef struct _drm_i830_copy_t {
+       int idx;                /* buffer index */
+       int used;               /* nr bytes in use */
+       void *address;  /* Address to copy from */
+} drm_i830_copy_t;
+
+typedef struct drm_i830_dma {
+       void *virtual;
+       int request_idx;
+       int request_size;
+       int granted;
+} drm_i830_dma_t;
+
+/* 1.3: Userspace can request & wait on irq's:
+ */
+typedef struct drm_i830_irq_emit {
+       int *irq_seq;
+} drm_i830_irq_emit_t;
+
+typedef struct drm_i830_irq_wait {
+       int irq_seq;
+} drm_i830_irq_wait_t;
+
+/* 1.3: New ioctl to query kernel params:
+ */
+#define I830_PARAM_IRQ_ACTIVE            1
+
+typedef struct drm_i830_getparam {
+       int param;
+       int *value;
+} drm_i830_getparam_t;
+
+/* 1.3: New ioctl to set kernel params:
+ */
+#define I830_SETPARAM_USE_MI_BATCHBUFFER_START            1
+
+typedef struct drm_i830_setparam {
+       int param;
+       int value;
+} drm_i830_setparam_t;
+
+#endif                         /* _I830_DRM_H_ */
diff --git a/include/drm/i915_drm.h b/include/drm/i915_drm.h
new file mode 100644 (file)
index 0000000..adc2392
--- /dev/null
@@ -0,0 +1,838 @@
+/*
+ * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+#ifndef _I915_DRM_H_
+#define _I915_DRM_H_
+
+#include "drm.h"
+
+/* Please note that modifications to all structs defined here are
+ * subject to backwards-compatibility constraints.
+ */
+
+/* Each region is a minimum of 16k, and there are at most 255 of them.
+ */
+#define I915_NR_TEX_REGIONS 255        /* table size 2k - maximum due to use
+                                * of chars for next/prev indices */
+#define I915_LOG_MIN_TEX_REGION_SIZE 14
+
+typedef struct _drm_i915_init {
+       enum {
+               I915_INIT_DMA = 0x01,
+               I915_CLEANUP_DMA = 0x02,
+               I915_RESUME_DMA = 0x03
+       } func;
+       unsigned int mmio_offset;
+       int sarea_priv_offset;
+       unsigned int ring_start;
+       unsigned int ring_end;
+       unsigned int ring_size;
+       unsigned int front_offset;
+       unsigned int back_offset;
+       unsigned int depth_offset;
+       unsigned int w;
+       unsigned int h;
+       unsigned int pitch;
+       unsigned int pitch_bits;
+       unsigned int back_pitch;
+       unsigned int depth_pitch;
+       unsigned int cpp;
+       unsigned int chipset;
+} drm_i915_init_t;
+
+typedef struct _drm_i915_sarea {
+       struct drm_tex_region texList[I915_NR_TEX_REGIONS + 1];
+       int last_upload;        /* last time texture was uploaded */
+       int last_enqueue;       /* last time a buffer was enqueued */
+       int last_dispatch;      /* age of the most recently dispatched buffer */
+       int ctxOwner;           /* last context to upload state */
+       int texAge;
+       int pf_enabled;         /* is pageflipping allowed? */
+       int pf_active;
+       int pf_current_page;    /* which buffer is being displayed? */
+       int perf_boxes;         /* performance boxes to be displayed */
+       int width, height;      /* screen size in pixels */
+
+       drm_handle_t front_handle;
+       int front_offset;
+       int front_size;
+
+       drm_handle_t back_handle;
+       int back_offset;
+       int back_size;
+
+       drm_handle_t depth_handle;
+       int depth_offset;
+       int depth_size;
+
+       drm_handle_t tex_handle;
+       int tex_offset;
+       int tex_size;
+       int log_tex_granularity;
+       int pitch;
+       int rotation;           /* 0, 90, 180 or 270 */
+       int rotated_offset;
+       int rotated_size;
+       int rotated_pitch;
+       int virtualX, virtualY;
+
+       unsigned int front_tiled;
+       unsigned int back_tiled;
+       unsigned int depth_tiled;
+       unsigned int rotated_tiled;
+       unsigned int rotated2_tiled;
+
+       int pipeA_x;
+       int pipeA_y;
+       int pipeA_w;
+       int pipeA_h;
+       int pipeB_x;
+       int pipeB_y;
+       int pipeB_w;
+       int pipeB_h;
+
+       /* fill out some space for old userspace triple buffer */
+       drm_handle_t unused_handle;
+       __u32 unused1, unused2, unused3;
+
+       /* buffer object handles for static buffers. May change
+        * over the lifetime of the client.
+        */
+       __u32 front_bo_handle;
+       __u32 back_bo_handle;
+       __u32 unused_bo_handle;
+       __u32 depth_bo_handle;
+
+} drm_i915_sarea_t;
+
+/* due to userspace building against these headers we need some compat here */
+#define planeA_x pipeA_x
+#define planeA_y pipeA_y
+#define planeA_w pipeA_w
+#define planeA_h pipeA_h
+#define planeB_x pipeB_x
+#define planeB_y pipeB_y
+#define planeB_w pipeB_w
+#define planeB_h pipeB_h
+
+/* Flags for perf_boxes
+ */
+#define I915_BOX_RING_EMPTY    0x1
+#define I915_BOX_FLIP          0x2
+#define I915_BOX_WAIT          0x4
+#define I915_BOX_TEXTURE_LOAD  0x8
+#define I915_BOX_LOST_CONTEXT  0x10
+
+/* I915 specific ioctls
+ * The device specific ioctl range is 0x40 to 0x79.
+ */
+#define DRM_I915_INIT          0x00
+#define DRM_I915_FLUSH         0x01
+#define DRM_I915_FLIP          0x02
+#define DRM_I915_BATCHBUFFER   0x03
+#define DRM_I915_IRQ_EMIT      0x04
+#define DRM_I915_IRQ_WAIT      0x05
+#define DRM_I915_GETPARAM      0x06
+#define DRM_I915_SETPARAM      0x07
+#define DRM_I915_ALLOC         0x08
+#define DRM_I915_FREE          0x09
+#define DRM_I915_INIT_HEAP     0x0a
+#define DRM_I915_CMDBUFFER     0x0b
+#define DRM_I915_DESTROY_HEAP  0x0c
+#define DRM_I915_SET_VBLANK_PIPE       0x0d
+#define DRM_I915_GET_VBLANK_PIPE       0x0e
+#define DRM_I915_VBLANK_SWAP   0x0f
+#define DRM_I915_HWS_ADDR      0x11
+#define DRM_I915_GEM_INIT      0x13
+#define DRM_I915_GEM_EXECBUFFER        0x14
+#define DRM_I915_GEM_PIN       0x15
+#define DRM_I915_GEM_UNPIN     0x16
+#define DRM_I915_GEM_BUSY      0x17
+#define DRM_I915_GEM_THROTTLE  0x18
+#define DRM_I915_GEM_ENTERVT   0x19
+#define DRM_I915_GEM_LEAVEVT   0x1a
+#define DRM_I915_GEM_CREATE    0x1b
+#define DRM_I915_GEM_PREAD     0x1c
+#define DRM_I915_GEM_PWRITE    0x1d
+#define DRM_I915_GEM_MMAP      0x1e
+#define DRM_I915_GEM_SET_DOMAIN        0x1f
+#define DRM_I915_GEM_SW_FINISH 0x20
+#define DRM_I915_GEM_SET_TILING        0x21
+#define DRM_I915_GEM_GET_TILING        0x22
+#define DRM_I915_GEM_GET_APERTURE 0x23
+#define DRM_I915_GEM_MMAP_GTT  0x24
+#define DRM_I915_GET_PIPE_FROM_CRTC_ID 0x25
+#define DRM_I915_GEM_MADVISE   0x26
+#define DRM_I915_OVERLAY_PUT_IMAGE     0x27
+#define DRM_I915_OVERLAY_ATTRS 0x28
+#define DRM_I915_GEM_EXECBUFFER2       0x29
+
+#define DRM_IOCTL_I915_INIT            DRM_IOW( DRM_COMMAND_BASE + DRM_I915_INIT, drm_i915_init_t)
+#define DRM_IOCTL_I915_FLUSH           DRM_IO ( DRM_COMMAND_BASE + DRM_I915_FLUSH)
+#define DRM_IOCTL_I915_FLIP            DRM_IO ( DRM_COMMAND_BASE + DRM_I915_FLIP)
+#define DRM_IOCTL_I915_BATCHBUFFER     DRM_IOW( DRM_COMMAND_BASE + DRM_I915_BATCHBUFFER, drm_i915_batchbuffer_t)
+#define DRM_IOCTL_I915_IRQ_EMIT         DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_IRQ_EMIT, drm_i915_irq_emit_t)
+#define DRM_IOCTL_I915_IRQ_WAIT         DRM_IOW( DRM_COMMAND_BASE + DRM_I915_IRQ_WAIT, drm_i915_irq_wait_t)
+#define DRM_IOCTL_I915_GETPARAM         DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_GETPARAM, drm_i915_getparam_t)
+#define DRM_IOCTL_I915_SETPARAM         DRM_IOW( DRM_COMMAND_BASE + DRM_I915_SETPARAM, drm_i915_setparam_t)
+#define DRM_IOCTL_I915_ALLOC            DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_ALLOC, drm_i915_mem_alloc_t)
+#define DRM_IOCTL_I915_FREE             DRM_IOW( DRM_COMMAND_BASE + DRM_I915_FREE, drm_i915_mem_free_t)
+#define DRM_IOCTL_I915_INIT_HEAP        DRM_IOW( DRM_COMMAND_BASE + DRM_I915_INIT_HEAP, drm_i915_mem_init_heap_t)
+#define DRM_IOCTL_I915_CMDBUFFER       DRM_IOW( DRM_COMMAND_BASE + DRM_I915_CMDBUFFER, drm_i915_cmdbuffer_t)
+#define DRM_IOCTL_I915_DESTROY_HEAP    DRM_IOW( DRM_COMMAND_BASE + DRM_I915_DESTROY_HEAP, drm_i915_mem_destroy_heap_t)
+#define DRM_IOCTL_I915_SET_VBLANK_PIPE DRM_IOW( DRM_COMMAND_BASE + DRM_I915_SET_VBLANK_PIPE, drm_i915_vblank_pipe_t)
+#define DRM_IOCTL_I915_GET_VBLANK_PIPE DRM_IOR( DRM_COMMAND_BASE + DRM_I915_GET_VBLANK_PIPE, drm_i915_vblank_pipe_t)
+#define DRM_IOCTL_I915_VBLANK_SWAP     DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_VBLANK_SWAP, drm_i915_vblank_swap_t)
+#define DRM_IOCTL_I915_HWS_ADDR                DRM_IOW(DRM_COMMAND_BASE + DRM_I915_HWS_ADDR, struct drm_i915_gem_init)
+#define DRM_IOCTL_I915_GEM_INIT                DRM_IOW(DRM_COMMAND_BASE + DRM_I915_GEM_INIT, struct drm_i915_gem_init)
+#define DRM_IOCTL_I915_GEM_EXECBUFFER  DRM_IOW(DRM_COMMAND_BASE + DRM_I915_GEM_EXECBUFFER, struct drm_i915_gem_execbuffer)
+#define DRM_IOCTL_I915_GEM_EXECBUFFER2 DRM_IOW(DRM_COMMAND_BASE + DRM_I915_GEM_EXECBUFFER2, struct drm_i915_gem_execbuffer2)
+#define DRM_IOCTL_I915_GEM_PIN         DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_GEM_PIN, struct drm_i915_gem_pin)
+#define DRM_IOCTL_I915_GEM_UNPIN       DRM_IOW(DRM_COMMAND_BASE + DRM_I915_GEM_UNPIN, struct drm_i915_gem_unpin)
+#define DRM_IOCTL_I915_GEM_BUSY                DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_GEM_BUSY, struct drm_i915_gem_busy)
+#define DRM_IOCTL_I915_GEM_THROTTLE    DRM_IO ( DRM_COMMAND_BASE + DRM_I915_GEM_THROTTLE)
+#define DRM_IOCTL_I915_GEM_ENTERVT     DRM_IO(DRM_COMMAND_BASE + DRM_I915_GEM_ENTERVT)
+#define DRM_IOCTL_I915_GEM_LEAVEVT     DRM_IO(DRM_COMMAND_BASE + DRM_I915_GEM_LEAVEVT)
+#define DRM_IOCTL_I915_GEM_CREATE      DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_GEM_CREATE, struct drm_i915_gem_create)
+#define DRM_IOCTL_I915_GEM_PREAD       DRM_IOW (DRM_COMMAND_BASE + DRM_I915_GEM_PREAD, struct drm_i915_gem_pread)
+#define DRM_IOCTL_I915_GEM_PWRITE      DRM_IOW (DRM_COMMAND_BASE + DRM_I915_GEM_PWRITE, struct drm_i915_gem_pwrite)
+#define DRM_IOCTL_I915_GEM_MMAP                DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_GEM_MMAP, struct drm_i915_gem_mmap)
+#define DRM_IOCTL_I915_GEM_MMAP_GTT    DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_GEM_MMAP_GTT, struct drm_i915_gem_mmap_gtt)
+#define DRM_IOCTL_I915_GEM_SET_DOMAIN  DRM_IOW (DRM_COMMAND_BASE + DRM_I915_GEM_SET_DOMAIN, struct drm_i915_gem_set_domain)
+#define DRM_IOCTL_I915_GEM_SW_FINISH   DRM_IOW (DRM_COMMAND_BASE + DRM_I915_GEM_SW_FINISH, struct drm_i915_gem_sw_finish)
+#define DRM_IOCTL_I915_GEM_SET_TILING  DRM_IOWR (DRM_COMMAND_BASE + DRM_I915_GEM_SET_TILING, struct drm_i915_gem_set_tiling)
+#define DRM_IOCTL_I915_GEM_GET_TILING  DRM_IOWR (DRM_COMMAND_BASE + DRM_I915_GEM_GET_TILING, struct drm_i915_gem_get_tiling)
+#define DRM_IOCTL_I915_GEM_GET_APERTURE        DRM_IOR  (DRM_COMMAND_BASE + DRM_I915_GEM_GET_APERTURE, struct drm_i915_gem_get_aperture)
+#define DRM_IOCTL_I915_GET_PIPE_FROM_CRTC_ID DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_GET_PIPE_FROM_CRTC_ID, struct drm_i915_get_pipe_from_crtc_id)
+#define DRM_IOCTL_I915_GEM_MADVISE     DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_GEM_MADVISE, struct drm_i915_gem_madvise)
+#define DRM_IOCTL_I915_OVERLAY_PUT_IMAGE       DRM_IOW(DRM_COMMAND_BASE + DRM_IOCTL_I915_OVERLAY_ATTRS, struct drm_intel_overlay_put_image)
+#define DRM_IOCTL_I915_OVERLAY_ATTRS   DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_OVERLAY_ATTRS, struct drm_intel_overlay_attrs)
+
+/* Allow drivers to submit batchbuffers directly to hardware, relying
+ * on the security mechanisms provided by hardware.
+ */
+typedef struct drm_i915_batchbuffer {
+       int start;              /* agp offset */
+       int used;               /* nr bytes in use */
+       int DR1;                /* hw flags for GFX_OP_DRAWRECT_INFO */
+       int DR4;                /* window origin for GFX_OP_DRAWRECT_INFO */
+       int num_cliprects;      /* mulitpass with multiple cliprects? */
+       struct drm_clip_rect *cliprects;        /* pointer to userspace cliprects */
+} drm_i915_batchbuffer_t;
+
+/* As above, but pass a pointer to userspace buffer which can be
+ * validated by the kernel prior to sending to hardware.
+ */
+typedef struct _drm_i915_cmdbuffer {
+       char *buf;      /* pointer to userspace command buffer */
+       int sz;                 /* nr bytes in buf */
+       int DR1;                /* hw flags for GFX_OP_DRAWRECT_INFO */
+       int DR4;                /* window origin for GFX_OP_DRAWRECT_INFO */
+       int num_cliprects;      /* mulitpass with multiple cliprects? */
+       struct drm_clip_rect *cliprects;        /* pointer to userspace cliprects */
+} drm_i915_cmdbuffer_t;
+
+/* Userspace can request & wait on irq's:
+ */
+typedef struct drm_i915_irq_emit {
+       int *irq_seq;
+} drm_i915_irq_emit_t;
+
+typedef struct drm_i915_irq_wait {
+       int irq_seq;
+} drm_i915_irq_wait_t;
+
+/* Ioctl to query kernel params:
+ */
+#define I915_PARAM_IRQ_ACTIVE            1
+#define I915_PARAM_ALLOW_BATCHBUFFER     2
+#define I915_PARAM_LAST_DISPATCH         3
+#define I915_PARAM_CHIPSET_ID            4
+#define I915_PARAM_HAS_GEM               5
+#define I915_PARAM_NUM_FENCES_AVAIL      6
+#define I915_PARAM_HAS_OVERLAY           7
+#define I915_PARAM_HAS_PAGEFLIPPING     8
+#define I915_PARAM_HAS_EXECBUF2          9
+#define I915_PARAM_HAS_BSD              10
+#define I915_PARAM_HAS_BLT              11
+#define I915_PARAM_HAS_RELAXED_FENCING  12
+#define I915_PARAM_HAS_COHERENT_RINGS   13
+#define I915_PARAM_HAS_EXEC_CONSTANTS   14
+#define I915_PARAM_HAS_RELAXED_DELTA    15
+
+typedef struct drm_i915_getparam {
+       int param;
+       int *value;
+} drm_i915_getparam_t;
+
+/* Ioctl to set kernel params:
+ */
+#define I915_SETPARAM_USE_MI_BATCHBUFFER_START            1
+#define I915_SETPARAM_TEX_LRU_LOG_GRANULARITY             2
+#define I915_SETPARAM_ALLOW_BATCHBUFFER                   3
+#define I915_SETPARAM_NUM_USED_FENCES                     4
+
+typedef struct drm_i915_setparam {
+       int param;
+       int value;
+} drm_i915_setparam_t;
+
+/* A memory manager for regions of shared memory:
+ */
+#define I915_MEM_REGION_AGP 1
+
+typedef struct drm_i915_mem_alloc {
+       int region;
+       int alignment;
+       int size;
+       int *region_offset;     /* offset from start of fb or agp */
+} drm_i915_mem_alloc_t;
+
+typedef struct drm_i915_mem_free {
+       int region;
+       int region_offset;
+} drm_i915_mem_free_t;
+
+typedef struct drm_i915_mem_init_heap {
+       int region;
+       int size;
+       int start;
+} drm_i915_mem_init_heap_t;
+
+/* Allow memory manager to be torn down and re-initialized (eg on
+ * rotate):
+ */
+typedef struct drm_i915_mem_destroy_heap {
+       int region;
+} drm_i915_mem_destroy_heap_t;
+
+/* Allow X server to configure which pipes to monitor for vblank signals
+ */
+#define        DRM_I915_VBLANK_PIPE_A  1
+#define        DRM_I915_VBLANK_PIPE_B  2
+
+typedef struct drm_i915_vblank_pipe {
+       int pipe;
+} drm_i915_vblank_pipe_t;
+
+/* Schedule buffer swap at given vertical blank:
+ */
+typedef struct drm_i915_vblank_swap {
+       drm_drawable_t drawable;
+       enum drm_vblank_seq_type seqtype;
+       unsigned int sequence;
+} drm_i915_vblank_swap_t;
+
+typedef struct drm_i915_hws_addr {
+       __u64 addr;
+} drm_i915_hws_addr_t;
+
+struct drm_i915_gem_init {
+       /**
+        * Beginning offset in the GTT to be managed by the DRM memory
+        * manager.
+        */
+       __u64 gtt_start;
+       /**
+        * Ending offset in the GTT to be managed by the DRM memory
+        * manager.
+        */
+       __u64 gtt_end;
+};
+
+struct drm_i915_gem_create {
+       /**
+        * Requested size for the object.
+        *
+        * The (page-aligned) allocated size for the object will be returned.
+        */
+       __u64 size;
+       /**
+        * Returned handle for the object.
+        *
+        * Object handles are nonzero.
+        */
+       __u32 handle;
+       __u32 pad;
+};
+
+struct drm_i915_gem_pread {
+       /** Handle for the object being read. */
+       __u32 handle;
+       __u32 pad;
+       /** Offset into the object to read from */
+       __u64 offset;
+       /** Length of data to read */
+       __u64 size;
+       /**
+        * Pointer to write the data into.
+        *
+        * This is a fixed-size type for 32/64 compatibility.
+        */
+       __u64 data_ptr;
+};
+
+struct drm_i915_gem_pwrite {
+       /** Handle for the object being written to. */
+       __u32 handle;
+       __u32 pad;
+       /** Offset into the object to write to */
+       __u64 offset;
+       /** Length of data to write */
+       __u64 size;
+       /**
+        * Pointer to read the data from.
+        *
+        * This is a fixed-size type for 32/64 compatibility.
+        */
+       __u64 data_ptr;
+};
+
+struct drm_i915_gem_mmap {
+       /** Handle for the object being mapped. */
+       __u32 handle;
+       __u32 pad;
+       /** Offset in the object to map. */
+       __u64 offset;
+       /**
+        * Length of data to map.
+        *
+        * The value will be page-aligned.
+        */
+       __u64 size;
+       /**
+        * Returned pointer the data was mapped at.
+        *
+        * This is a fixed-size type for 32/64 compatibility.
+        */
+       __u64 addr_ptr;
+};
+
+struct drm_i915_gem_mmap_gtt {
+       /** Handle for the object being mapped. */
+       __u32 handle;
+       __u32 pad;
+       /**
+        * Fake offset to use for subsequent mmap call
+        *
+        * This is a fixed-size type for 32/64 compatibility.
+        */
+       __u64 offset;
+};
+
+struct drm_i915_gem_set_domain {
+       /** Handle for the object */
+       __u32 handle;
+
+       /** New read domains */
+       __u32 read_domains;
+
+       /** New write domain */
+       __u32 write_domain;
+};
+
+struct drm_i915_gem_sw_finish {
+       /** Handle for the object */
+       __u32 handle;
+};
+
+struct drm_i915_gem_relocation_entry {
+       /**
+        * Handle of the buffer being pointed to by this relocation entry.
+        *
+        * It's appealing to make this be an index into the mm_validate_entry
+        * list to refer to the buffer, but this allows the driver to create
+        * a relocation list for state buffers and not re-write it per
+        * exec using the buffer.
+        */
+       __u32 target_handle;
+
+       /**
+        * Value to be added to the offset of the target buffer to make up
+        * the relocation entry.
+        */
+       __u32 delta;
+
+       /** Offset in the buffer the relocation entry will be written into */
+       __u64 offset;
+
+       /**
+        * Offset value of the target buffer that the relocation entry was last
+        * written as.
+        *
+        * If the buffer has the same offset as last time, we can skip syncing
+        * and writing the relocation.  This value is written back out by
+        * the execbuffer ioctl when the relocation is written.
+        */
+       __u64 presumed_offset;
+
+       /**
+        * Target memory domains read by this operation.
+        */
+       __u32 read_domains;
+
+       /**
+        * Target memory domains written by this operation.
+        *
+        * Note that only one domain may be written by the whole
+        * execbuffer operation, so that where there are conflicts,
+        * the application will get -EINVAL back.
+        */
+       __u32 write_domain;
+};
+
+/** @{
+ * Intel memory domains
+ *
+ * Most of these just align with the various caches in
+ * the system and are used to flush and invalidate as
+ * objects end up cached in different domains.
+ */
+/** CPU cache */
+#define I915_GEM_DOMAIN_CPU            0x00000001
+/** Render cache, used by 2D and 3D drawing */
+#define I915_GEM_DOMAIN_RENDER         0x00000002
+/** Sampler cache, used by texture engine */
+#define I915_GEM_DOMAIN_SAMPLER                0x00000004
+/** Command queue, used to load batch buffers */
+#define I915_GEM_DOMAIN_COMMAND                0x00000008
+/** Instruction cache, used by shader programs */
+#define I915_GEM_DOMAIN_INSTRUCTION    0x00000010
+/** Vertex address cache */
+#define I915_GEM_DOMAIN_VERTEX         0x00000020
+/** GTT domain - aperture and scanout */
+#define I915_GEM_DOMAIN_GTT            0x00000040
+/** @} */
+
+struct drm_i915_gem_exec_object {
+       /**
+        * User's handle for a buffer to be bound into the GTT for this
+        * operation.
+        */
+       __u32 handle;
+
+       /** Number of relocations to be performed on this buffer */
+       __u32 relocation_count;
+       /**
+        * Pointer to array of struct drm_i915_gem_relocation_entry containing
+        * the relocations to be performed in this buffer.
+        */
+       __u64 relocs_ptr;
+
+       /** Required alignment in graphics aperture */
+       __u64 alignment;
+
+       /**
+        * Returned value of the updated offset of the object, for future
+        * presumed_offset writes.
+        */
+       __u64 offset;
+};
+
+struct drm_i915_gem_execbuffer {
+       /**
+        * List of buffers to be validated with their relocations to be
+        * performend on them.
+        *
+        * This is a pointer to an array of struct drm_i915_gem_validate_entry.
+        *
+        * These buffers must be listed in an order such that all relocations
+        * a buffer is performing refer to buffers that have already appeared
+        * in the validate list.
+        */
+       __u64 buffers_ptr;
+       __u32 buffer_count;
+
+       /** Offset in the batchbuffer to start execution from. */
+       __u32 batch_start_offset;
+       /** Bytes used in batchbuffer from batch_start_offset */
+       __u32 batch_len;
+       __u32 DR1;
+       __u32 DR4;
+       __u32 num_cliprects;
+       /** This is a struct drm_clip_rect *cliprects */
+       __u64 cliprects_ptr;
+};
+
+struct drm_i915_gem_exec_object2 {
+       /**
+        * User's handle for a buffer to be bound into the GTT for this
+        * operation.
+        */
+       __u32 handle;
+
+       /** Number of relocations to be performed on this buffer */
+       __u32 relocation_count;
+       /**
+        * Pointer to array of struct drm_i915_gem_relocation_entry containing
+        * the relocations to be performed in this buffer.
+        */
+       __u64 relocs_ptr;
+
+       /** Required alignment in graphics aperture */
+       __u64 alignment;
+
+       /**
+        * Returned value of the updated offset of the object, for future
+        * presumed_offset writes.
+        */
+       __u64 offset;
+
+#define EXEC_OBJECT_NEEDS_FENCE (1<<0)
+       __u64 flags;
+       __u64 rsvd1;
+       __u64 rsvd2;
+};
+
+struct drm_i915_gem_execbuffer2 {
+       /**
+        * List of gem_exec_object2 structs
+        */
+       __u64 buffers_ptr;
+       __u32 buffer_count;
+
+       /** Offset in the batchbuffer to start execution from. */
+       __u32 batch_start_offset;
+       /** Bytes used in batchbuffer from batch_start_offset */
+       __u32 batch_len;
+       __u32 DR1;
+       __u32 DR4;
+       __u32 num_cliprects;
+       /** This is a struct drm_clip_rect *cliprects */
+       __u64 cliprects_ptr;
+#define I915_EXEC_RING_MASK              (7<<0)
+#define I915_EXEC_DEFAULT                (0<<0)
+#define I915_EXEC_RENDER                 (1<<0)
+#define I915_EXEC_BSD                    (2<<0)
+#define I915_EXEC_BLT                    (3<<0)
+
+/* Used for switching the constants addressing mode on gen4+ RENDER ring.
+ * Gen6+ only supports relative addressing to dynamic state (default) and
+ * absolute addressing.
+ *
+ * These flags are ignored for the BSD and BLT rings.
+ */
+#define I915_EXEC_CONSTANTS_MASK       (3<<6)
+#define I915_EXEC_CONSTANTS_REL_GENERAL (0<<6) /* default */
+#define I915_EXEC_CONSTANTS_ABSOLUTE   (1<<6)
+#define I915_EXEC_CONSTANTS_REL_SURFACE (2<<6) /* gen4/5 only */
+       __u64 flags;
+       __u64 rsvd1;
+       __u64 rsvd2;
+};
+
+struct drm_i915_gem_pin {
+       /** Handle of the buffer to be pinned. */
+       __u32 handle;
+       __u32 pad;
+
+       /** alignment required within the aperture */
+       __u64 alignment;
+
+       /** Returned GTT offset of the buffer. */
+       __u64 offset;
+};
+
+struct drm_i915_gem_unpin {
+       /** Handle of the buffer to be unpinned. */
+       __u32 handle;
+       __u32 pad;
+};
+
+struct drm_i915_gem_busy {
+       /** Handle of the buffer to check for busy */
+       __u32 handle;
+
+       /** Return busy status (1 if busy, 0 if idle) */
+       __u32 busy;
+};
+
+#define I915_TILING_NONE       0
+#define I915_TILING_X          1
+#define I915_TILING_Y          2
+
+#define I915_BIT_6_SWIZZLE_NONE                0
+#define I915_BIT_6_SWIZZLE_9           1
+#define I915_BIT_6_SWIZZLE_9_10                2
+#define I915_BIT_6_SWIZZLE_9_11                3
+#define I915_BIT_6_SWIZZLE_9_10_11     4
+/* Not seen by userland */
+#define I915_BIT_6_SWIZZLE_UNKNOWN     5
+/* Seen by userland. */
+#define I915_BIT_6_SWIZZLE_9_17                6
+#define I915_BIT_6_SWIZZLE_9_10_17     7
+
+struct drm_i915_gem_set_tiling {
+       /** Handle of the buffer to have its tiling state updated */
+       __u32 handle;
+
+       /**
+        * Tiling mode for the object (I915_TILING_NONE, I915_TILING_X,
+        * I915_TILING_Y).
+        *
+        * This value is to be set on request, and will be updated by the
+        * kernel on successful return with the actual chosen tiling layout.
+        *
+        * The tiling mode may be demoted to I915_TILING_NONE when the system
+        * has bit 6 swizzling that can't be managed correctly by GEM.
+        *
+        * Buffer contents become undefined when changing tiling_mode.
+        */
+       __u32 tiling_mode;
+
+       /**
+        * Stride in bytes for the object when in I915_TILING_X or
+        * I915_TILING_Y.
+        */
+       __u32 stride;
+
+       /**
+        * Returned address bit 6 swizzling required for CPU access through
+        * mmap mapping.
+        */
+       __u32 swizzle_mode;
+};
+
+struct drm_i915_gem_get_tiling {
+       /** Handle of the buffer to get tiling state for. */
+       __u32 handle;
+
+       /**
+        * Current tiling mode for the object (I915_TILING_NONE, I915_TILING_X,
+        * I915_TILING_Y).
+        */
+       __u32 tiling_mode;
+
+       /**
+        * Returned address bit 6 swizzling required for CPU access through
+        * mmap mapping.
+        */
+       __u32 swizzle_mode;
+};
+
+struct drm_i915_gem_get_aperture {
+       /** Total size of the aperture used by i915_gem_execbuffer, in bytes */
+       __u64 aper_size;
+
+       /**
+        * Available space in the aperture used by i915_gem_execbuffer, in
+        * bytes
+        */
+       __u64 aper_available_size;
+};
+
+struct drm_i915_get_pipe_from_crtc_id {
+       /** ID of CRTC being requested **/
+       __u32 crtc_id;
+
+       /** pipe of requested CRTC **/
+       __u32 pipe;
+};
+
+#define I915_MADV_WILLNEED 0
+#define I915_MADV_DONTNEED 1
+#define __I915_MADV_PURGED 2 /* internal state */
+
+struct drm_i915_gem_madvise {
+       /** Handle of the buffer to change the backing store advice */
+       __u32 handle;
+
+       /* Advice: either the buffer will be needed again in the near future,
+        *         or wont be and could be discarded under memory pressure.
+        */
+       __u32 madv;
+
+       /** Whether the backing store still exists. */
+       __u32 retained;
+};
+
+/* flags */
+#define I915_OVERLAY_TYPE_MASK                 0xff
+#define I915_OVERLAY_YUV_PLANAR        0x01
+#define I915_OVERLAY_YUV_PACKED        0x02
+#define I915_OVERLAY_RGB               0x03
+
+#define I915_OVERLAY_DEPTH_MASK                0xff00
+#define I915_OVERLAY_RGB24             0x1000
+#define I915_OVERLAY_RGB16             0x2000
+#define I915_OVERLAY_RGB15             0x3000
+#define I915_OVERLAY_YUV422            0x0100
+#define I915_OVERLAY_YUV411            0x0200
+#define I915_OVERLAY_YUV420            0x0300
+#define I915_OVERLAY_YUV410            0x0400
+
+#define I915_OVERLAY_SWAP_MASK         0xff0000
+#define I915_OVERLAY_NO_SWAP           0x000000
+#define I915_OVERLAY_UV_SWAP           0x010000
+#define I915_OVERLAY_Y_SWAP            0x020000
+#define I915_OVERLAY_Y_AND_UV_SWAP     0x030000
+
+#define I915_OVERLAY_FLAGS_MASK                0xff000000
+#define I915_OVERLAY_ENABLE            0x01000000
+
+struct drm_intel_overlay_put_image {
+       /* various flags and src format description */
+       __u32 flags;
+       /* source picture description */
+       __u32 bo_handle;
+       /* stride values and offsets are in bytes, buffer relative */
+       __u16 stride_Y; /* stride for packed formats */
+       __u16 stride_UV;
+       __u32 offset_Y; /* offset for packet formats */
+       __u32 offset_U;
+       __u32 offset_V;
+       /* in pixels */
+       __u16 src_width;
+       __u16 src_height;
+       /* to compensate the scaling factors for partially covered surfaces */
+       __u16 src_scan_width;
+       __u16 src_scan_height;
+       /* output crtc description */
+       __u32 crtc_id;
+       __u16 dst_x;
+       __u16 dst_y;
+       __u16 dst_width;
+       __u16 dst_height;
+};
+
+/* flags */
+#define I915_OVERLAY_UPDATE_ATTRS      (1<<0)
+#define I915_OVERLAY_UPDATE_GAMMA      (1<<1)
+struct drm_intel_overlay_attrs {
+       __u32 flags;
+       __u32 color_key;
+       __s32 brightness;
+       __u32 contrast;
+       __u32 saturation;
+       __u32 gamma0;
+       __u32 gamma1;
+       __u32 gamma2;
+       __u32 gamma3;
+       __u32 gamma4;
+       __u32 gamma5;
+};
+
+#endif                         /* _I915_DRM_H_ */
diff --git a/include/drm/mach64_drm.h b/include/drm/mach64_drm.h
new file mode 100644 (file)
index 0000000..1f5fd84
--- /dev/null
@@ -0,0 +1,256 @@
+/* mach64_drm.h -- Public header for the mach64 driver -*- linux-c -*-
+ * Created: Thu Nov 30 20:04:32 2000 by gareth@valinux.com
+ */
+/*
+ * Copyright 2000 Gareth Hughes
+ * Copyright 2002 Frank C. Earl
+ * Copyright 2002-2003 Leif Delgass
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT OWNER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+ * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * Authors:
+ *    Gareth Hughes <gareth@valinux.com>
+ *    Frank C. Earl <fearl@airmail.net>
+ *    Leif Delgass <ldelgass@retinalburn.net>
+ */
+
+#ifndef __MACH64_DRM_H__
+#define __MACH64_DRM_H__
+
+/* WARNING: If you change any of these defines, make sure to change the
+ * defines in the Xserver file (mach64_sarea.h)
+ */
+#ifndef __MACH64_SAREA_DEFINES__
+#define __MACH64_SAREA_DEFINES__
+
+/* What needs to be changed for the current vertex buffer?
+ * GH: We're going to be pedantic about this.  We want the card to do as
+ * little as possible, so let's avoid having it fetch a whole bunch of
+ * register values that don't change all that often, if at all.
+ */
+#define MACH64_UPLOAD_DST_OFF_PITCH    0x0001
+#define MACH64_UPLOAD_Z_OFF_PITCH      0x0002
+#define MACH64_UPLOAD_Z_ALPHA_CNTL     0x0004
+#define MACH64_UPLOAD_SCALE_3D_CNTL    0x0008
+#define MACH64_UPLOAD_DP_FOG_CLR       0x0010
+#define MACH64_UPLOAD_DP_WRITE_MASK    0x0020
+#define MACH64_UPLOAD_DP_PIX_WIDTH     0x0040
+#define MACH64_UPLOAD_SETUP_CNTL       0x0080
+#define MACH64_UPLOAD_MISC             0x0100
+#define MACH64_UPLOAD_TEXTURE          0x0200
+#define MACH64_UPLOAD_TEX0IMAGE                0x0400
+#define MACH64_UPLOAD_TEX1IMAGE                0x0800
+#define MACH64_UPLOAD_CLIPRECTS                0x1000  /* handled client-side */
+#define MACH64_UPLOAD_CONTEXT          0x00ff
+#define MACH64_UPLOAD_ALL              0x1fff
+
+/* DMA buffer size
+ */
+#define MACH64_BUFFER_SIZE             16384
+
+/* Max number of swaps allowed on the ring
+ * before the client must wait
+ */
+#define MACH64_MAX_QUEUED_FRAMES        3U
+
+/* Byte offsets for host blit buffer data
+ */
+#define MACH64_HOSTDATA_BLIT_OFFSET    104
+
+/* Keep these small for testing.
+ */
+#define MACH64_NR_SAREA_CLIPRECTS      8
+
+#define MACH64_CARD_HEAP               0
+#define MACH64_AGP_HEAP                        1
+#define MACH64_NR_TEX_HEAPS            2
+#define MACH64_NR_TEX_REGIONS          64
+#define MACH64_LOG_TEX_GRANULARITY     16
+
+#define MACH64_TEX_MAXLEVELS           1
+
+#define MACH64_NR_CONTEXT_REGS         15
+#define MACH64_NR_TEXTURE_REGS         4
+
+#endif                         /* __MACH64_SAREA_DEFINES__ */
+
+typedef struct {
+       unsigned int dst_off_pitch;
+
+       unsigned int z_off_pitch;
+       unsigned int z_cntl;
+       unsigned int alpha_tst_cntl;
+
+       unsigned int scale_3d_cntl;
+
+       unsigned int sc_left_right;
+       unsigned int sc_top_bottom;
+
+       unsigned int dp_fog_clr;
+       unsigned int dp_write_mask;
+       unsigned int dp_pix_width;
+       unsigned int dp_mix;
+       unsigned int dp_src;
+
+       unsigned int clr_cmp_cntl;
+       unsigned int gui_traj_cntl;
+
+       unsigned int setup_cntl;
+
+       unsigned int tex_size_pitch;
+       unsigned int tex_cntl;
+       unsigned int secondary_tex_off;
+       unsigned int tex_offset;
+} drm_mach64_context_regs_t;
+
+typedef struct drm_mach64_sarea {
+       /* The channel for communication of state information to the kernel
+        * on firing a vertex dma buffer.
+        */
+       drm_mach64_context_regs_t context_state;
+       unsigned int dirty;
+       unsigned int vertsize;
+
+       /* The current cliprects, or a subset thereof.
+        */
+       struct drm_clip_rect boxes[MACH64_NR_SAREA_CLIPRECTS];
+       unsigned int nbox;
+
+       /* Counters for client-side throttling of rendering clients.
+        */
+       unsigned int frames_queued;
+
+       /* Texture memory LRU.
+        */
+       struct drm_tex_region tex_list[MACH64_NR_TEX_HEAPS][MACH64_NR_TEX_REGIONS +
+                                                      1];
+       unsigned int tex_age[MACH64_NR_TEX_HEAPS];
+       int ctx_owner;
+} drm_mach64_sarea_t;
+
+/* WARNING: If you change any of these defines, make sure to change the
+ * defines in the Xserver file (mach64_common.h)
+ */
+
+/* Mach64 specific ioctls
+ * The device specific ioctl range is 0x40 to 0x79.
+ */
+
+#define DRM_MACH64_INIT           0x00
+#define DRM_MACH64_IDLE           0x01
+#define DRM_MACH64_RESET          0x02
+#define DRM_MACH64_SWAP           0x03
+#define DRM_MACH64_CLEAR          0x04
+#define DRM_MACH64_VERTEX         0x05
+#define DRM_MACH64_BLIT           0x06
+#define DRM_MACH64_FLUSH          0x07
+#define DRM_MACH64_GETPARAM       0x08
+
+#define DRM_IOCTL_MACH64_INIT           DRM_IOW( DRM_COMMAND_BASE + DRM_MACH64_INIT, drm_mach64_init_t)
+#define DRM_IOCTL_MACH64_IDLE           DRM_IO(  DRM_COMMAND_BASE + DRM_MACH64_IDLE )
+#define DRM_IOCTL_MACH64_RESET          DRM_IO(  DRM_COMMAND_BASE + DRM_MACH64_RESET )
+#define DRM_IOCTL_MACH64_SWAP           DRM_IO(  DRM_COMMAND_BASE + DRM_MACH64_SWAP )
+#define DRM_IOCTL_MACH64_CLEAR          DRM_IOW( DRM_COMMAND_BASE + DRM_MACH64_CLEAR, drm_mach64_clear_t)
+#define DRM_IOCTL_MACH64_VERTEX         DRM_IOW( DRM_COMMAND_BASE + DRM_MACH64_VERTEX, drm_mach64_vertex_t)
+#define DRM_IOCTL_MACH64_BLIT           DRM_IOW( DRM_COMMAND_BASE + DRM_MACH64_BLIT, drm_mach64_blit_t)
+#define DRM_IOCTL_MACH64_FLUSH          DRM_IO(  DRM_COMMAND_BASE + DRM_MACH64_FLUSH )
+#define DRM_IOCTL_MACH64_GETPARAM       DRM_IOWR( DRM_COMMAND_BASE + DRM_MACH64_GETPARAM, drm_mach64_getparam_t)
+
+/* Buffer flags for clears
+ */
+#define MACH64_FRONT                   0x1
+#define MACH64_BACK                    0x2
+#define MACH64_DEPTH                   0x4
+
+/* Primitive types for vertex buffers
+ */
+#define MACH64_PRIM_POINTS             0x00000000
+#define MACH64_PRIM_LINES              0x00000001
+#define MACH64_PRIM_LINE_LOOP          0x00000002
+#define MACH64_PRIM_LINE_STRIP         0x00000003
+#define MACH64_PRIM_TRIANGLES          0x00000004
+#define MACH64_PRIM_TRIANGLE_STRIP     0x00000005
+#define MACH64_PRIM_TRIANGLE_FAN       0x00000006
+#define MACH64_PRIM_QUADS              0x00000007
+#define MACH64_PRIM_QUAD_STRIP         0x00000008
+#define MACH64_PRIM_POLYGON            0x00000009
+
+typedef enum _drm_mach64_dma_mode_t {
+       MACH64_MODE_DMA_ASYNC,
+       MACH64_MODE_DMA_SYNC,
+       MACH64_MODE_MMIO
+} drm_mach64_dma_mode_t;
+
+typedef struct drm_mach64_init {
+       enum {
+               DRM_MACH64_INIT_DMA = 0x01,
+               DRM_MACH64_CLEANUP_DMA = 0x02
+       } func;
+
+       unsigned long sarea_priv_offset;
+       int is_pci;
+       drm_mach64_dma_mode_t dma_mode;
+
+       unsigned int fb_bpp;
+       unsigned int front_offset, front_pitch;
+       unsigned int back_offset, back_pitch;
+
+       unsigned int depth_bpp;
+       unsigned int depth_offset, depth_pitch;
+
+       unsigned long fb_offset;
+       unsigned long mmio_offset;
+       unsigned long ring_offset;
+       unsigned long buffers_offset;
+       unsigned long agp_textures_offset;
+} drm_mach64_init_t;
+
+typedef struct drm_mach64_clear {
+       unsigned int flags;
+       int x, y, w, h;
+       unsigned int clear_color;
+       unsigned int clear_depth;
+} drm_mach64_clear_t;
+
+typedef struct drm_mach64_vertex {
+       int prim;
+       void *buf;              /* Address of vertex buffer */
+       unsigned long used;     /* Number of bytes in buffer */
+       int discard;            /* Client finished with buffer? */
+} drm_mach64_vertex_t;
+
+typedef struct drm_mach64_blit {
+       void *buf;
+       int pitch;
+       int offset;
+       int format;
+       unsigned short x, y;
+       unsigned short width, height;
+} drm_mach64_blit_t;
+
+typedef struct drm_mach64_getparam {
+       enum {
+               MACH64_PARAM_FRAMES_QUEUED = 0x01,
+               MACH64_PARAM_IRQ_NR = 0x02
+       } param;
+       void *value;
+} drm_mach64_getparam_t;
+
+#endif
diff --git a/include/drm/mga_drm.h b/include/drm/mga_drm.h
new file mode 100644 (file)
index 0000000..b630e8f
--- /dev/null
@@ -0,0 +1,419 @@
+/* mga_drm.h -- Public header for the Matrox g200/g400 driver -*- linux-c -*-
+ * Created: Tue Jan 25 01:50:01 1999 by jhartmann@precisioninsight.com
+ *
+ * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
+ * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
+ * All rights reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * Authors:
+ *    Jeff Hartmann <jhartmann@valinux.com>
+ *    Keith Whitwell <keith@tungstengraphics.com>
+ *
+ * Rewritten by:
+ *    Gareth Hughes <gareth@valinux.com>
+ */
+
+#ifndef __MGA_DRM_H__
+#define __MGA_DRM_H__
+
+#include "drm.h"
+
+/* WARNING: If you change any of these defines, make sure to change the
+ * defines in the Xserver file (mga_sarea.h)
+ */
+
+#ifndef __MGA_SAREA_DEFINES__
+#define __MGA_SAREA_DEFINES__
+
+/* WARP pipe flags
+ */
+#define MGA_F                  0x1     /* fog */
+#define MGA_A                  0x2     /* alpha */
+#define MGA_S                  0x4     /* specular */
+#define MGA_T2                 0x8     /* multitexture */
+
+#define MGA_WARP_TGZ           0
+#define MGA_WARP_TGZF          (MGA_F)
+#define MGA_WARP_TGZA          (MGA_A)
+#define MGA_WARP_TGZAF         (MGA_F|MGA_A)
+#define MGA_WARP_TGZS          (MGA_S)
+#define MGA_WARP_TGZSF         (MGA_S|MGA_F)
+#define MGA_WARP_TGZSA         (MGA_S|MGA_A)
+#define MGA_WARP_TGZSAF                (MGA_S|MGA_F|MGA_A)
+#define MGA_WARP_T2GZ          (MGA_T2)
+#define MGA_WARP_T2GZF         (MGA_T2|MGA_F)
+#define MGA_WARP_T2GZA         (MGA_T2|MGA_A)
+#define MGA_WARP_T2GZAF                (MGA_T2|MGA_A|MGA_F)
+#define MGA_WARP_T2GZS         (MGA_T2|MGA_S)
+#define MGA_WARP_T2GZSF                (MGA_T2|MGA_S|MGA_F)
+#define MGA_WARP_T2GZSA                (MGA_T2|MGA_S|MGA_A)
+#define MGA_WARP_T2GZSAF       (MGA_T2|MGA_S|MGA_F|MGA_A)
+
+#define MGA_MAX_G200_PIPES     8       /* no multitex */
+#define MGA_MAX_G400_PIPES     16
+#define MGA_MAX_WARP_PIPES     MGA_MAX_G400_PIPES
+#define MGA_WARP_UCODE_SIZE    32768   /* in bytes */
+
+#define MGA_CARD_TYPE_G200     1
+#define MGA_CARD_TYPE_G400     2
+#define MGA_CARD_TYPE_G450     3       /* not currently used */
+#define MGA_CARD_TYPE_G550     4
+
+#define MGA_FRONT              0x1
+#define MGA_BACK               0x2
+#define MGA_DEPTH              0x4
+
+/* What needs to be changed for the current vertex dma buffer?
+ */
+#define MGA_UPLOAD_CONTEXT     0x1
+#define MGA_UPLOAD_TEX0                0x2
+#define MGA_UPLOAD_TEX1                0x4
+#define MGA_UPLOAD_PIPE                0x8
+#define MGA_UPLOAD_TEX0IMAGE   0x10    /* handled client-side */
+#define MGA_UPLOAD_TEX1IMAGE   0x20    /* handled client-side */
+#define MGA_UPLOAD_2D          0x40
+#define MGA_WAIT_AGE           0x80    /* handled client-side */
+#define MGA_UPLOAD_CLIPRECTS   0x100   /* handled client-side */
+#if 0
+#define MGA_DMA_FLUSH          0x200   /* set when someone gets the lock
+                                          quiescent */
+#endif
+
+/* 32 buffers of 64k each, total 2 meg.
+ */
+#define MGA_BUFFER_SIZE                (1 << 16)
+#define MGA_NUM_BUFFERS                128
+
+/* Keep these small for testing.
+ */
+#define MGA_NR_SAREA_CLIPRECTS 8
+
+/* 2 heaps (1 for card, 1 for agp), each divided into upto 128
+ * regions, subject to a minimum region size of (1<<16) == 64k.
+ *
+ * Clients may subdivide regions internally, but when sharing between
+ * clients, the region size is the minimum granularity.
+ */
+
+#define MGA_CARD_HEAP                  0
+#define MGA_AGP_HEAP                   1
+#define MGA_NR_TEX_HEAPS               2
+#define MGA_NR_TEX_REGIONS             16
+#define MGA_LOG_MIN_TEX_REGION_SIZE    16
+
+#define  DRM_MGA_IDLE_RETRY          2048
+
+#endif                         /* __MGA_SAREA_DEFINES__ */
+
+/* Setup registers for 3D context
+ */
+typedef struct {
+       unsigned int dstorg;
+       unsigned int maccess;
+       unsigned int plnwt;
+       unsigned int dwgctl;
+       unsigned int alphactrl;
+       unsigned int fogcolor;
+       unsigned int wflag;
+       unsigned int tdualstage0;
+       unsigned int tdualstage1;
+       unsigned int fcol;
+       unsigned int stencil;
+       unsigned int stencilctl;
+} drm_mga_context_regs_t;
+
+/* Setup registers for 2D, X server
+ */
+typedef struct {
+       unsigned int pitch;
+} drm_mga_server_regs_t;
+
+/* Setup registers for each texture unit
+ */
+typedef struct {
+       unsigned int texctl;
+       unsigned int texctl2;
+       unsigned int texfilter;
+       unsigned int texbordercol;
+       unsigned int texorg;
+       unsigned int texwidth;
+       unsigned int texheight;
+       unsigned int texorg1;
+       unsigned int texorg2;
+       unsigned int texorg3;
+       unsigned int texorg4;
+} drm_mga_texture_regs_t;
+
+/* General aging mechanism
+ */
+typedef struct {
+       unsigned int head;      /* Position of head pointer          */
+       unsigned int wrap;      /* Primary DMA wrap count            */
+} drm_mga_age_t;
+
+typedef struct _drm_mga_sarea {
+       /* The channel for communication of state information to the kernel
+        * on firing a vertex dma buffer.
+        */
+       drm_mga_context_regs_t context_state;
+       drm_mga_server_regs_t server_state;
+       drm_mga_texture_regs_t tex_state[2];
+       unsigned int warp_pipe;
+       unsigned int dirty;
+       unsigned int vertsize;
+
+       /* The current cliprects, or a subset thereof.
+        */
+       struct drm_clip_rect boxes[MGA_NR_SAREA_CLIPRECTS];
+       unsigned int nbox;
+
+       /* Information about the most recently used 3d drawable.  The
+        * client fills in the req_* fields, the server fills in the
+        * exported_ fields and puts the cliprects into boxes, above.
+        *
+        * The client clears the exported_drawable field before
+        * clobbering the boxes data.
+        */
+       unsigned int req_drawable;      /* the X drawable id */
+       unsigned int req_draw_buffer;   /* MGA_FRONT or MGA_BACK */
+
+       unsigned int exported_drawable;
+       unsigned int exported_index;
+       unsigned int exported_stamp;
+       unsigned int exported_buffers;
+       unsigned int exported_nfront;
+       unsigned int exported_nback;
+       int exported_back_x, exported_front_x, exported_w;
+       int exported_back_y, exported_front_y, exported_h;
+       struct drm_clip_rect exported_boxes[MGA_NR_SAREA_CLIPRECTS];
+
+       /* Counters for aging textures and for client-side throttling.
+        */
+       unsigned int status[4];
+       unsigned int last_wrap;
+
+       drm_mga_age_t last_frame;
+       unsigned int last_enqueue;      /* last time a buffer was enqueued */
+       unsigned int last_dispatch;     /* age of the most recently dispatched buffer */
+       unsigned int last_quiescent;    /*  */
+
+       /* LRU lists for texture memory in agp space and on the card.
+        */
+       struct drm_tex_region texList[MGA_NR_TEX_HEAPS][MGA_NR_TEX_REGIONS + 1];
+       unsigned int texAge[MGA_NR_TEX_HEAPS];
+
+       /* Mechanism to validate card state.
+        */
+       int ctxOwner;
+} drm_mga_sarea_t;
+
+/* MGA specific ioctls
+ * The device specific ioctl range is 0x40 to 0x79.
+ */
+#define DRM_MGA_INIT     0x00
+#define DRM_MGA_FLUSH    0x01
+#define DRM_MGA_RESET    0x02
+#define DRM_MGA_SWAP     0x03
+#define DRM_MGA_CLEAR    0x04
+#define DRM_MGA_VERTEX   0x05
+#define DRM_MGA_INDICES  0x06
+#define DRM_MGA_ILOAD    0x07
+#define DRM_MGA_BLIT     0x08
+#define DRM_MGA_GETPARAM 0x09
+
+/* 3.2:
+ * ioctls for operating on fences.
+ */
+#define DRM_MGA_SET_FENCE      0x0a
+#define DRM_MGA_WAIT_FENCE     0x0b
+#define DRM_MGA_DMA_BOOTSTRAP  0x0c
+
+#define DRM_IOCTL_MGA_INIT     DRM_IOW( DRM_COMMAND_BASE + DRM_MGA_INIT, drm_mga_init_t)
+#define DRM_IOCTL_MGA_FLUSH    DRM_IOW( DRM_COMMAND_BASE + DRM_MGA_FLUSH, drm_lock_t)
+#define DRM_IOCTL_MGA_RESET    DRM_IO(  DRM_COMMAND_BASE + DRM_MGA_RESET)
+#define DRM_IOCTL_MGA_SWAP     DRM_IO(  DRM_COMMAND_BASE + DRM_MGA_SWAP)
+#define DRM_IOCTL_MGA_CLEAR    DRM_IOW( DRM_COMMAND_BASE + DRM_MGA_CLEAR, drm_mga_clear_t)
+#define DRM_IOCTL_MGA_VERTEX   DRM_IOW( DRM_COMMAND_BASE + DRM_MGA_VERTEX, drm_mga_vertex_t)
+#define DRM_IOCTL_MGA_INDICES  DRM_IOW( DRM_COMMAND_BASE + DRM_MGA_INDICES, drm_mga_indices_t)
+#define DRM_IOCTL_MGA_ILOAD    DRM_IOW( DRM_COMMAND_BASE + DRM_MGA_ILOAD, drm_mga_iload_t)
+#define DRM_IOCTL_MGA_BLIT     DRM_IOW( DRM_COMMAND_BASE + DRM_MGA_BLIT, drm_mga_blit_t)
+#define DRM_IOCTL_MGA_GETPARAM DRM_IOWR(DRM_COMMAND_BASE + DRM_MGA_GETPARAM, drm_mga_getparam_t)
+#define DRM_IOCTL_MGA_SET_FENCE     DRM_IOW( DRM_COMMAND_BASE + DRM_MGA_SET_FENCE, __u32)
+#define DRM_IOCTL_MGA_WAIT_FENCE    DRM_IOWR(DRM_COMMAND_BASE + DRM_MGA_WAIT_FENCE, __u32)
+#define DRM_IOCTL_MGA_DMA_BOOTSTRAP DRM_IOWR(DRM_COMMAND_BASE + DRM_MGA_DMA_BOOTSTRAP, drm_mga_dma_bootstrap_t)
+
+typedef struct _drm_mga_warp_index {
+       int installed;
+       unsigned long phys_addr;
+       int size;
+} drm_mga_warp_index_t;
+
+typedef struct drm_mga_init {
+       enum {
+               MGA_INIT_DMA = 0x01,
+               MGA_CLEANUP_DMA = 0x02
+       } func;
+
+       unsigned long sarea_priv_offset;
+
+       int chipset;
+       int sgram;
+
+       unsigned int maccess;
+
+       unsigned int fb_cpp;
+       unsigned int front_offset, front_pitch;
+       unsigned int back_offset, back_pitch;
+
+       unsigned int depth_cpp;
+       unsigned int depth_offset, depth_pitch;
+
+       unsigned int texture_offset[MGA_NR_TEX_HEAPS];
+       unsigned int texture_size[MGA_NR_TEX_HEAPS];
+
+       unsigned long fb_offset;
+       unsigned long mmio_offset;
+       unsigned long status_offset;
+       unsigned long warp_offset;
+       unsigned long primary_offset;
+       unsigned long buffers_offset;
+} drm_mga_init_t;
+
+typedef struct drm_mga_dma_bootstrap {
+       /**
+        * \name AGP texture region
+        *
+        * On return from the DRM_MGA_DMA_BOOTSTRAP ioctl, these fields will
+        * be filled in with the actual AGP texture settings.
+        *
+        * \warning
+        * If these fields are non-zero, but dma_mga_dma_bootstrap::agp_mode
+        * is zero, it means that PCI memory (most likely through the use of
+        * an IOMMU) is being used for "AGP" textures.
+        */
+       /*@{ */
+       unsigned long texture_handle; /**< Handle used to map AGP textures. */
+       __u32 texture_size;           /**< Size of the AGP texture region. */
+       /*@} */
+
+       /**
+        * Requested size of the primary DMA region.
+        *
+        * On return from the DRM_MGA_DMA_BOOTSTRAP ioctl, this field will be
+        * filled in with the actual AGP mode.  If AGP was not available
+        */
+       __u32 primary_size;
+
+       /**
+        * Requested number of secondary DMA buffers.
+        *
+        * On return from the DRM_MGA_DMA_BOOTSTRAP ioctl, this field will be
+        * filled in with the actual number of secondary DMA buffers
+        * allocated.  Particularly when PCI DMA is used, this may be
+        * (subtantially) less than the number requested.
+        */
+       __u32 secondary_bin_count;
+
+       /**
+        * Requested size of each secondary DMA buffer.
+        *
+        * While the kernel \b is free to reduce
+        * dma_mga_dma_bootstrap::secondary_bin_count, it is \b not allowed
+        * to reduce dma_mga_dma_bootstrap::secondary_bin_size.
+        */
+       __u32 secondary_bin_size;
+
+       /**
+        * Bit-wise mask of AGPSTAT2_* values.  Currently only \c AGPSTAT2_1X,
+        * \c AGPSTAT2_2X, and \c AGPSTAT2_4X are supported.  If this value is
+        * zero, it means that PCI DMA should be used, even if AGP is
+        * possible.
+        *
+        * On return from the DRM_MGA_DMA_BOOTSTRAP ioctl, this field will be
+        * filled in with the actual AGP mode.  If AGP was not available
+        * (i.e., PCI DMA was used), this value will be zero.
+        */
+       __u32 agp_mode;
+
+       /**
+        * Desired AGP GART size, measured in megabytes.
+        */
+       __u8 agp_size;
+} drm_mga_dma_bootstrap_t;
+
+typedef struct drm_mga_clear {
+       unsigned int flags;
+       unsigned int clear_color;
+       unsigned int clear_depth;
+       unsigned int color_mask;
+       unsigned int depth_mask;
+} drm_mga_clear_t;
+
+typedef struct drm_mga_vertex {
+       int idx;                /* buffer to queue */
+       int used;               /* bytes in use */
+       int discard;            /* client finished with buffer?  */
+} drm_mga_vertex_t;
+
+typedef struct drm_mga_indices {
+       int idx;                /* buffer to queue */
+       unsigned int start;
+       unsigned int end;
+       int discard;            /* client finished with buffer?  */
+} drm_mga_indices_t;
+
+typedef struct drm_mga_iload {
+       int idx;
+       unsigned int dstorg;
+       unsigned int length;
+} drm_mga_iload_t;
+
+typedef struct _drm_mga_blit {
+       unsigned int planemask;
+       unsigned int srcorg;
+       unsigned int dstorg;
+       int src_pitch, dst_pitch;
+       int delta_sx, delta_sy;
+       int delta_dx, delta_dy;
+       int height, ydir;       /* flip image vertically */
+       int source_pitch, dest_pitch;
+} drm_mga_blit_t;
+
+/* 3.1: An ioctl to get parameters that aren't available to the 3d
+ * client any other way.
+ */
+#define MGA_PARAM_IRQ_NR            1
+
+/* 3.2: Query the actual card type.  The DDX only distinguishes between
+ * G200 chips and non-G200 chips, which it calls G400.  It turns out that
+ * there are some very sublte differences between the G4x0 chips and the G550
+ * chips.  Using this parameter query, a client-side driver can detect the
+ * difference between a G4x0 and a G550.
+ */
+#define MGA_PARAM_CARD_TYPE         2
+
+typedef struct drm_mga_getparam {
+       int param;
+       void *value;
+} drm_mga_getparam_t;
+
+#endif
diff --git a/include/drm/nouveau_drm.h b/include/drm/nouveau_drm.h
new file mode 100644 (file)
index 0000000..b18cad0
--- /dev/null
@@ -0,0 +1,208 @@
+/*
+ * Copyright 2005 Stephane Marchesin.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef __NOUVEAU_DRM_H__
+#define __NOUVEAU_DRM_H__
+
+#define NOUVEAU_DRM_HEADER_PATCHLEVEL 16
+
+struct drm_nouveau_channel_alloc {
+       uint32_t     fb_ctxdma_handle;
+       uint32_t     tt_ctxdma_handle;
+
+       int          channel;
+       uint32_t     pushbuf_domains;
+
+       /* Notifier memory */
+       uint32_t     notifier_handle;
+
+       /* DRM-enforced subchannel assignments */
+       struct {
+               uint32_t handle;
+               uint32_t grclass;
+       } subchan[8];
+       uint32_t nr_subchan;
+};
+
+struct drm_nouveau_channel_free {
+       int channel;
+};
+
+struct drm_nouveau_grobj_alloc {
+       int      channel;
+       uint32_t handle;
+       int      class;
+};
+
+struct drm_nouveau_notifierobj_alloc {
+       uint32_t channel;
+       uint32_t handle;
+       uint32_t size;
+       uint32_t offset;
+};
+
+struct drm_nouveau_gpuobj_free {
+       int      channel;
+       uint32_t handle;
+};
+
+/* FIXME : maybe unify {GET,SET}PARAMs */
+#define NOUVEAU_GETPARAM_PCI_VENDOR      3
+#define NOUVEAU_GETPARAM_PCI_DEVICE      4
+#define NOUVEAU_GETPARAM_BUS_TYPE        5
+#define NOUVEAU_GETPARAM_FB_PHYSICAL     6
+#define NOUVEAU_GETPARAM_AGP_PHYSICAL    7
+#define NOUVEAU_GETPARAM_FB_SIZE         8
+#define NOUVEAU_GETPARAM_AGP_SIZE        9
+#define NOUVEAU_GETPARAM_PCI_PHYSICAL    10
+#define NOUVEAU_GETPARAM_CHIPSET_ID      11
+#define NOUVEAU_GETPARAM_VM_VRAM_BASE    12
+#define NOUVEAU_GETPARAM_GRAPH_UNITS     13
+#define NOUVEAU_GETPARAM_PTIMER_TIME     14
+#define NOUVEAU_GETPARAM_HAS_BO_USAGE    15
+#define NOUVEAU_GETPARAM_HAS_PAGEFLIP    16
+struct drm_nouveau_getparam {
+       uint64_t param;
+       uint64_t value;
+};
+
+struct drm_nouveau_setparam {
+       uint64_t param;
+       uint64_t value;
+};
+
+#define NOUVEAU_GEM_DOMAIN_CPU       (1 << 0)
+#define NOUVEAU_GEM_DOMAIN_VRAM      (1 << 1)
+#define NOUVEAU_GEM_DOMAIN_GART      (1 << 2)
+#define NOUVEAU_GEM_DOMAIN_MAPPABLE  (1 << 3)
+
+#define NOUVEAU_GEM_TILE_LAYOUT_MASK 0x0000ff00
+#define NOUVEAU_GEM_TILE_16BPP       0x00000001
+#define NOUVEAU_GEM_TILE_32BPP       0x00000002
+#define NOUVEAU_GEM_TILE_ZETA        0x00000004
+#define NOUVEAU_GEM_TILE_NONCONTIG   0x00000008
+
+struct drm_nouveau_gem_info {
+       uint32_t handle;
+       uint32_t domain;
+       uint64_t size;
+       uint64_t offset;
+       uint64_t map_handle;
+       uint32_t tile_mode;
+       uint32_t tile_flags;
+};
+
+struct drm_nouveau_gem_new {
+       struct drm_nouveau_gem_info info;
+       uint32_t channel_hint;
+       uint32_t align;
+};
+
+#define NOUVEAU_GEM_MAX_BUFFERS 1024
+struct drm_nouveau_gem_pushbuf_bo_presumed {
+       uint32_t valid;
+       uint32_t domain;
+       uint64_t offset;
+};
+
+struct drm_nouveau_gem_pushbuf_bo {
+       uint64_t user_priv;
+       uint32_t handle;
+       uint32_t read_domains;
+       uint32_t write_domains;
+       uint32_t valid_domains;
+       struct drm_nouveau_gem_pushbuf_bo_presumed presumed;
+};
+
+#define NOUVEAU_GEM_RELOC_LOW  (1 << 0)
+#define NOUVEAU_GEM_RELOC_HIGH (1 << 1)
+#define NOUVEAU_GEM_RELOC_OR   (1 << 2)
+#define NOUVEAU_GEM_MAX_RELOCS 1024
+struct drm_nouveau_gem_pushbuf_reloc {
+       uint32_t reloc_bo_index;
+       uint32_t reloc_bo_offset;
+       uint32_t bo_index;
+       uint32_t flags;
+       uint32_t data;
+       uint32_t vor;
+       uint32_t tor;
+};
+
+#define NOUVEAU_GEM_MAX_PUSH 512
+struct drm_nouveau_gem_pushbuf_push {
+       uint32_t bo_index;
+       uint32_t pad;
+       uint64_t offset;
+       uint64_t length;
+};
+
+struct drm_nouveau_gem_pushbuf {
+       uint32_t channel;
+       uint32_t nr_buffers;
+       uint64_t buffers;
+       uint32_t nr_relocs;
+       uint32_t nr_push;
+       uint64_t relocs;
+       uint64_t push;
+       uint32_t suffix0;
+       uint32_t suffix1;
+       uint64_t vram_available;
+       uint64_t gart_available;
+};
+
+#define NOUVEAU_GEM_CPU_PREP_NOWAIT                                  0x00000001
+#define NOUVEAU_GEM_CPU_PREP_NOBLOCK                                 0x00000002
+#define NOUVEAU_GEM_CPU_PREP_WRITE                                   0x00000004
+struct drm_nouveau_gem_cpu_prep {
+       uint32_t handle;
+       uint32_t flags;
+};
+
+struct drm_nouveau_gem_cpu_fini {
+       uint32_t handle;
+};
+
+enum nouveau_bus_type {
+       NV_AGP     = 0,
+       NV_PCI     = 1,
+       NV_PCIE    = 2,
+};
+
+struct drm_nouveau_sarea {
+};
+
+#define DRM_NOUVEAU_GETPARAM           0x00
+#define DRM_NOUVEAU_SETPARAM           0x01
+#define DRM_NOUVEAU_CHANNEL_ALLOC      0x02
+#define DRM_NOUVEAU_CHANNEL_FREE       0x03
+#define DRM_NOUVEAU_GROBJ_ALLOC        0x04
+#define DRM_NOUVEAU_NOTIFIEROBJ_ALLOC  0x05
+#define DRM_NOUVEAU_GPUOBJ_FREE        0x06
+#define DRM_NOUVEAU_GEM_NEW            0x40
+#define DRM_NOUVEAU_GEM_PUSHBUF        0x41
+#define DRM_NOUVEAU_GEM_CPU_PREP       0x42
+#define DRM_NOUVEAU_GEM_CPU_FINI       0x43
+#define DRM_NOUVEAU_GEM_INFO           0x44
+
+#endif /* __NOUVEAU_DRM_H__ */
diff --git a/include/drm/r128_drm.h b/include/drm/r128_drm.h
new file mode 100644 (file)
index 0000000..ede78ff
--- /dev/null
@@ -0,0 +1,326 @@
+/* r128_drm.h -- Public header for the r128 driver -*- linux-c -*-
+ * Created: Wed Apr  5 19:24:19 2000 by kevin@precisioninsight.com
+ */
+/*
+ * Copyright 2000 Precision Insight, Inc., Cedar Park, Texas.
+ * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
+ * All rights reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Authors:
+ *    Gareth Hughes <gareth@valinux.com>
+ *    Kevin E. Martin <martin@valinux.com>
+ */
+
+#ifndef __R128_DRM_H__
+#define __R128_DRM_H__
+
+/* WARNING: If you change any of these defines, make sure to change the
+ * defines in the X server file (r128_sarea.h)
+ */
+#ifndef __R128_SAREA_DEFINES__
+#define __R128_SAREA_DEFINES__
+
+/* What needs to be changed for the current vertex buffer?
+ */
+#define R128_UPLOAD_CONTEXT            0x001
+#define R128_UPLOAD_SETUP              0x002
+#define R128_UPLOAD_TEX0               0x004
+#define R128_UPLOAD_TEX1               0x008
+#define R128_UPLOAD_TEX0IMAGES         0x010
+#define R128_UPLOAD_TEX1IMAGES         0x020
+#define R128_UPLOAD_CORE               0x040
+#define R128_UPLOAD_MASKS              0x080
+#define R128_UPLOAD_WINDOW             0x100
+#define R128_UPLOAD_CLIPRECTS          0x200   /* handled client-side */
+#define R128_REQUIRE_QUIESCENCE                0x400
+#define R128_UPLOAD_ALL                        0x7ff
+
+#define R128_FRONT                     0x1
+#define R128_BACK                      0x2
+#define R128_DEPTH                     0x4
+
+/* Primitive types
+ */
+#define R128_POINTS                    0x1
+#define R128_LINES                     0x2
+#define R128_LINE_STRIP                        0x3
+#define R128_TRIANGLES                 0x4
+#define R128_TRIANGLE_FAN              0x5
+#define R128_TRIANGLE_STRIP            0x6
+
+/* Vertex/indirect buffer size
+ */
+#define R128_BUFFER_SIZE               16384
+
+/* Byte offsets for indirect buffer data
+ */
+#define R128_INDEX_PRIM_OFFSET         20
+#define R128_HOSTDATA_BLIT_OFFSET      32
+
+/* Keep these small for testing.
+ */
+#define R128_NR_SAREA_CLIPRECTS                12
+
+/* There are 2 heaps (local/AGP).  Each region within a heap is a
+ *  minimum of 64k, and there are at most 64 of them per heap.
+ */
+#define R128_LOCAL_TEX_HEAP            0
+#define R128_AGP_TEX_HEAP              1
+#define R128_NR_TEX_HEAPS              2
+#define R128_NR_TEX_REGIONS            64
+#define R128_LOG_TEX_GRANULARITY       16
+
+#define R128_NR_CONTEXT_REGS           12
+
+#define R128_MAX_TEXTURE_LEVELS                11
+#define R128_MAX_TEXTURE_UNITS         2
+
+#endif                         /* __R128_SAREA_DEFINES__ */
+
+typedef struct {
+       /* Context state - can be written in one large chunk */
+       unsigned int dst_pitch_offset_c;
+       unsigned int dp_gui_master_cntl_c;
+       unsigned int sc_top_left_c;
+       unsigned int sc_bottom_right_c;
+       unsigned int z_offset_c;
+       unsigned int z_pitch_c;
+       unsigned int z_sten_cntl_c;
+       unsigned int tex_cntl_c;
+       unsigned int misc_3d_state_cntl_reg;
+       unsigned int texture_clr_cmp_clr_c;
+       unsigned int texture_clr_cmp_msk_c;
+       unsigned int fog_color_c;
+
+       /* Texture state */
+       unsigned int tex_size_pitch_c;
+       unsigned int constant_color_c;
+
+       /* Setup state */
+       unsigned int pm4_vc_fpu_setup;
+       unsigned int setup_cntl;
+
+       /* Mask state */
+       unsigned int dp_write_mask;
+       unsigned int sten_ref_mask_c;
+       unsigned int plane_3d_mask_c;
+
+       /* Window state */
+       unsigned int window_xy_offset;
+
+       /* Core state */
+       unsigned int scale_3d_cntl;
+} drm_r128_context_regs_t;
+
+/* Setup registers for each texture unit
+ */
+typedef struct {
+       unsigned int tex_cntl;
+       unsigned int tex_combine_cntl;
+       unsigned int tex_size_pitch;
+       unsigned int tex_offset[R128_MAX_TEXTURE_LEVELS];
+       unsigned int tex_border_color;
+} drm_r128_texture_regs_t;
+
+typedef struct drm_r128_sarea {
+       /* The channel for communication of state information to the kernel
+        * on firing a vertex buffer.
+        */
+       drm_r128_context_regs_t context_state;
+       drm_r128_texture_regs_t tex_state[R128_MAX_TEXTURE_UNITS];
+       unsigned int dirty;
+       unsigned int vertsize;
+       unsigned int vc_format;
+
+       /* The current cliprects, or a subset thereof.
+        */
+       struct drm_clip_rect boxes[R128_NR_SAREA_CLIPRECTS];
+       unsigned int nbox;
+
+       /* Counters for client-side throttling of rendering clients.
+        */
+       unsigned int last_frame;
+       unsigned int last_dispatch;
+
+       struct drm_tex_region tex_list[R128_NR_TEX_HEAPS][R128_NR_TEX_REGIONS + 1];
+       unsigned int tex_age[R128_NR_TEX_HEAPS];
+       int ctx_owner;
+       int pfAllowPageFlip;    /* number of 3d windows (0,1,2 or more) */
+       int pfCurrentPage;      /* which buffer is being displayed? */
+} drm_r128_sarea_t;
+
+/* WARNING: If you change any of these defines, make sure to change the
+ * defines in the Xserver file (xf86drmR128.h)
+ */
+
+/* Rage 128 specific ioctls
+ * The device specific ioctl range is 0x40 to 0x79.
+ */
+#define DRM_R128_INIT       0x00
+#define DRM_R128_CCE_START  0x01
+#define DRM_R128_CCE_STOP   0x02
+#define DRM_R128_CCE_RESET  0x03
+#define DRM_R128_CCE_IDLE   0x04
+/* 0x05 not used */
+#define DRM_R128_RESET      0x06
+#define DRM_R128_SWAP       0x07
+#define DRM_R128_CLEAR      0x08
+#define DRM_R128_VERTEX     0x09
+#define DRM_R128_INDICES    0x0a
+#define DRM_R128_BLIT       0x0b
+#define DRM_R128_DEPTH      0x0c
+#define DRM_R128_STIPPLE    0x0d
+/* 0x0e not used */
+#define DRM_R128_INDIRECT   0x0f
+#define DRM_R128_FULLSCREEN 0x10
+#define DRM_R128_CLEAR2     0x11
+#define DRM_R128_GETPARAM   0x12
+#define DRM_R128_FLIP       0x13
+
+#define DRM_IOCTL_R128_INIT       DRM_IOW( DRM_COMMAND_BASE + DRM_R128_INIT, drm_r128_init_t)
+#define DRM_IOCTL_R128_CCE_START  DRM_IO(  DRM_COMMAND_BASE + DRM_R128_CCE_START)
+#define DRM_IOCTL_R128_CCE_STOP   DRM_IOW( DRM_COMMAND_BASE + DRM_R128_CCE_STOP, drm_r128_cce_stop_t)
+#define DRM_IOCTL_R128_CCE_RESET  DRM_IO(  DRM_COMMAND_BASE + DRM_R128_CCE_RESET)
+#define DRM_IOCTL_R128_CCE_IDLE   DRM_IO(  DRM_COMMAND_BASE + DRM_R128_CCE_IDLE)
+/* 0x05 not used */
+#define DRM_IOCTL_R128_RESET      DRM_IO(  DRM_COMMAND_BASE + DRM_R128_RESET)
+#define DRM_IOCTL_R128_SWAP       DRM_IO(  DRM_COMMAND_BASE + DRM_R128_SWAP)
+#define DRM_IOCTL_R128_CLEAR      DRM_IOW( DRM_COMMAND_BASE + DRM_R128_CLEAR, drm_r128_clear_t)
+#define DRM_IOCTL_R128_VERTEX     DRM_IOW( DRM_COMMAND_BASE + DRM_R128_VERTEX, drm_r128_vertex_t)
+#define DRM_IOCTL_R128_INDICES    DRM_IOW( DRM_COMMAND_BASE + DRM_R128_INDICES, drm_r128_indices_t)
+#define DRM_IOCTL_R128_BLIT       DRM_IOW( DRM_COMMAND_BASE + DRM_R128_BLIT, drm_r128_blit_t)
+#define DRM_IOCTL_R128_DEPTH      DRM_IOW( DRM_COMMAND_BASE + DRM_R128_DEPTH, drm_r128_depth_t)
+#define DRM_IOCTL_R128_STIPPLE    DRM_IOW( DRM_COMMAND_BASE + DRM_R128_STIPPLE, drm_r128_stipple_t)
+/* 0x0e not used */
+#define DRM_IOCTL_R128_INDIRECT   DRM_IOWR(DRM_COMMAND_BASE + DRM_R128_INDIRECT, drm_r128_indirect_t)
+#define DRM_IOCTL_R128_FULLSCREEN DRM_IOW( DRM_COMMAND_BASE + DRM_R128_FULLSCREEN, drm_r128_fullscreen_t)
+#define DRM_IOCTL_R128_CLEAR2     DRM_IOW( DRM_COMMAND_BASE + DRM_R128_CLEAR2, drm_r128_clear2_t)
+#define DRM_IOCTL_R128_GETPARAM   DRM_IOWR( DRM_COMMAND_BASE + DRM_R128_GETPARAM, drm_r128_getparam_t)
+#define DRM_IOCTL_R128_FLIP       DRM_IO(  DRM_COMMAND_BASE + DRM_R128_FLIP)
+
+typedef struct drm_r128_init {
+       enum {
+               R128_INIT_CCE = 0x01,
+               R128_CLEANUP_CCE = 0x02
+       } func;
+       unsigned long sarea_priv_offset;
+       int is_pci;
+       int cce_mode;
+       int cce_secure;
+       int ring_size;
+       int usec_timeout;
+
+       unsigned int fb_bpp;
+       unsigned int front_offset, front_pitch;
+       unsigned int back_offset, back_pitch;
+       unsigned int depth_bpp;
+       unsigned int depth_offset, depth_pitch;
+       unsigned int span_offset;
+
+       unsigned long fb_offset;
+       unsigned long mmio_offset;
+       unsigned long ring_offset;
+       unsigned long ring_rptr_offset;
+       unsigned long buffers_offset;
+       unsigned long agp_textures_offset;
+} drm_r128_init_t;
+
+typedef struct drm_r128_cce_stop {
+       int flush;
+       int idle;
+} drm_r128_cce_stop_t;
+
+typedef struct drm_r128_clear {
+       unsigned int flags;
+       unsigned int clear_color;
+       unsigned int clear_depth;
+       unsigned int color_mask;
+       unsigned int depth_mask;
+} drm_r128_clear_t;
+
+typedef struct drm_r128_vertex {
+       int prim;
+       int idx;                /* Index of vertex buffer */
+       int count;              /* Number of vertices in buffer */
+       int discard;            /* Client finished with buffer? */
+} drm_r128_vertex_t;
+
+typedef struct drm_r128_indices {
+       int prim;
+       int idx;
+       int start;
+       int end;
+       int discard;            /* Client finished with buffer? */
+} drm_r128_indices_t;
+
+typedef struct drm_r128_blit {
+       int idx;
+       int pitch;
+       int offset;
+       int format;
+       unsigned short x, y;
+       unsigned short width, height;
+} drm_r128_blit_t;
+
+typedef struct drm_r128_depth {
+       enum {
+               R128_WRITE_SPAN = 0x01,
+               R128_WRITE_PIXELS = 0x02,
+               R128_READ_SPAN = 0x03,
+               R128_READ_PIXELS = 0x04
+       } func;
+       int n;
+       int *x;
+       int *y;
+       unsigned int *buffer;
+       unsigned char *mask;
+} drm_r128_depth_t;
+
+typedef struct drm_r128_stipple {
+       unsigned int *mask;
+} drm_r128_stipple_t;
+
+typedef struct drm_r128_indirect {
+       int idx;
+       int start;
+       int end;
+       int discard;
+} drm_r128_indirect_t;
+
+typedef struct drm_r128_fullscreen {
+       enum {
+               R128_INIT_FULLSCREEN = 0x01,
+               R128_CLEANUP_FULLSCREEN = 0x02
+       } func;
+} drm_r128_fullscreen_t;
+
+/* 2.3: An ioctl to get parameters that aren't available to the 3d
+ * client any other way.
+ */
+#define R128_PARAM_IRQ_NR            1
+
+typedef struct drm_r128_getparam {
+       int param;
+       void *value;
+} drm_r128_getparam_t;
+
+#endif
diff --git a/include/drm/radeon_drm.h b/include/drm/radeon_drm.h
new file mode 100644 (file)
index 0000000..3b762d6
--- /dev/null
@@ -0,0 +1,916 @@
+/* radeon_drm.h -- Public header for the radeon driver -*- linux-c -*-
+ *
+ * Copyright 2000 Precision Insight, Inc., Cedar Park, Texas.
+ * Copyright 2000 VA Linux Systems, Inc., Fremont, California.
+ * Copyright 2002 Tungsten Graphics, Inc., Cedar Park, Texas.
+ * All rights reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Authors:
+ *    Kevin E. Martin <martin@valinux.com>
+ *    Gareth Hughes <gareth@valinux.com>
+ *    Keith Whitwell <keith@tungstengraphics.com>
+ */
+
+#ifndef __RADEON_DRM_H__
+#define __RADEON_DRM_H__
+
+#include "drm.h"
+
+/* WARNING: If you change any of these defines, make sure to change the
+ * defines in the X server file (radeon_sarea.h)
+ */
+#ifndef __RADEON_SAREA_DEFINES__
+#define __RADEON_SAREA_DEFINES__
+
+/* Old style state flags, required for sarea interface (1.1 and 1.2
+ * clears) and 1.2 drm_vertex2 ioctl.
+ */
+#define RADEON_UPLOAD_CONTEXT          0x00000001
+#define RADEON_UPLOAD_VERTFMT          0x00000002
+#define RADEON_UPLOAD_LINE             0x00000004
+#define RADEON_UPLOAD_BUMPMAP          0x00000008
+#define RADEON_UPLOAD_MASKS            0x00000010
+#define RADEON_UPLOAD_VIEWPORT         0x00000020
+#define RADEON_UPLOAD_SETUP            0x00000040
+#define RADEON_UPLOAD_TCL              0x00000080
+#define RADEON_UPLOAD_MISC             0x00000100
+#define RADEON_UPLOAD_TEX0             0x00000200
+#define RADEON_UPLOAD_TEX1             0x00000400
+#define RADEON_UPLOAD_TEX2             0x00000800
+#define RADEON_UPLOAD_TEX0IMAGES       0x00001000
+#define RADEON_UPLOAD_TEX1IMAGES       0x00002000
+#define RADEON_UPLOAD_TEX2IMAGES       0x00004000
+#define RADEON_UPLOAD_CLIPRECTS                0x00008000      /* handled client-side */
+#define RADEON_REQUIRE_QUIESCENCE      0x00010000
+#define RADEON_UPLOAD_ZBIAS            0x00020000      /* version 1.2 and newer */
+#define RADEON_UPLOAD_ALL              0x003effff
+#define RADEON_UPLOAD_CONTEXT_ALL       0x003e01ff
+
+/* New style per-packet identifiers for use in cmd_buffer ioctl with
+ * the RADEON_EMIT_PACKET command.  Comments relate new packets to old
+ * state bits and the packet size:
+ */
+#define RADEON_EMIT_PP_MISC                         0  /* context/7 */
+#define RADEON_EMIT_PP_CNTL                         1  /* context/3 */
+#define RADEON_EMIT_RB3D_COLORPITCH                 2  /* context/1 */
+#define RADEON_EMIT_RE_LINE_PATTERN                 3  /* line/2 */
+#define RADEON_EMIT_SE_LINE_WIDTH                   4  /* line/1 */
+#define RADEON_EMIT_PP_LUM_MATRIX                   5  /* bumpmap/1 */
+#define RADEON_EMIT_PP_ROT_MATRIX_0                 6  /* bumpmap/2 */
+#define RADEON_EMIT_RB3D_STENCILREFMASK             7  /* masks/3 */
+#define RADEON_EMIT_SE_VPORT_XSCALE                 8  /* viewport/6 */
+#define RADEON_EMIT_SE_CNTL                         9  /* setup/2 */
+#define RADEON_EMIT_SE_CNTL_STATUS                  10 /* setup/1 */
+#define RADEON_EMIT_RE_MISC                         11 /* misc/1 */
+#define RADEON_EMIT_PP_TXFILTER_0                   12 /* tex0/6 */
+#define RADEON_EMIT_PP_BORDER_COLOR_0               13 /* tex0/1 */
+#define RADEON_EMIT_PP_TXFILTER_1                   14 /* tex1/6 */
+#define RADEON_EMIT_PP_BORDER_COLOR_1               15 /* tex1/1 */
+#define RADEON_EMIT_PP_TXFILTER_2                   16 /* tex2/6 */
+#define RADEON_EMIT_PP_BORDER_COLOR_2               17 /* tex2/1 */
+#define RADEON_EMIT_SE_ZBIAS_FACTOR                 18 /* zbias/2 */
+#define RADEON_EMIT_SE_TCL_OUTPUT_VTX_FMT           19 /* tcl/11 */
+#define RADEON_EMIT_SE_TCL_MATERIAL_EMMISSIVE_RED   20 /* material/17 */
+#define R200_EMIT_PP_TXCBLEND_0                     21 /* tex0/4 */
+#define R200_EMIT_PP_TXCBLEND_1                     22 /* tex1/4 */
+#define R200_EMIT_PP_TXCBLEND_2                     23 /* tex2/4 */
+#define R200_EMIT_PP_TXCBLEND_3                     24 /* tex3/4 */
+#define R200_EMIT_PP_TXCBLEND_4                     25 /* tex4/4 */
+#define R200_EMIT_PP_TXCBLEND_5                     26 /* tex5/4 */
+#define R200_EMIT_PP_TXCBLEND_6                     27 /* /4 */
+#define R200_EMIT_PP_TXCBLEND_7                     28 /* /4 */
+#define R200_EMIT_TCL_LIGHT_MODEL_CTL_0             29 /* tcl/7 */
+#define R200_EMIT_TFACTOR_0                         30 /* tf/7 */
+#define R200_EMIT_VTX_FMT_0                         31 /* vtx/5 */
+#define R200_EMIT_VAP_CTL                           32 /* vap/1 */
+#define R200_EMIT_MATRIX_SELECT_0                   33 /* msl/5 */
+#define R200_EMIT_TEX_PROC_CTL_2                    34 /* tcg/5 */
+#define R200_EMIT_TCL_UCP_VERT_BLEND_CTL            35 /* tcl/1 */
+#define R200_EMIT_PP_TXFILTER_0                     36 /* tex0/6 */
+#define R200_EMIT_PP_TXFILTER_1                     37 /* tex1/6 */
+#define R200_EMIT_PP_TXFILTER_2                     38 /* tex2/6 */
+#define R200_EMIT_PP_TXFILTER_3                     39 /* tex3/6 */
+#define R200_EMIT_PP_TXFILTER_4                     40 /* tex4/6 */
+#define R200_EMIT_PP_TXFILTER_5                     41 /* tex5/6 */
+#define R200_EMIT_PP_TXOFFSET_0                     42 /* tex0/1 */
+#define R200_EMIT_PP_TXOFFSET_1                     43 /* tex1/1 */
+#define R200_EMIT_PP_TXOFFSET_2                     44 /* tex2/1 */
+#define R200_EMIT_PP_TXOFFSET_3                     45 /* tex3/1 */
+#define R200_EMIT_PP_TXOFFSET_4                     46 /* tex4/1 */
+#define R200_EMIT_PP_TXOFFSET_5                     47 /* tex5/1 */
+#define R200_EMIT_VTE_CNTL                          48 /* vte/1 */
+#define R200_EMIT_OUTPUT_VTX_COMP_SEL               49 /* vtx/1 */
+#define R200_EMIT_PP_TAM_DEBUG3                     50 /* tam/1 */
+#define R200_EMIT_PP_CNTL_X                         51 /* cst/1 */
+#define R200_EMIT_RB3D_DEPTHXY_OFFSET               52 /* cst/1 */
+#define R200_EMIT_RE_AUX_SCISSOR_CNTL               53 /* cst/1 */
+#define R200_EMIT_RE_SCISSOR_TL_0                   54 /* cst/2 */
+#define R200_EMIT_RE_SCISSOR_TL_1                   55 /* cst/2 */
+#define R200_EMIT_RE_SCISSOR_TL_2                   56 /* cst/2 */
+#define R200_EMIT_SE_VAP_CNTL_STATUS                57 /* cst/1 */
+#define R200_EMIT_SE_VTX_STATE_CNTL                 58 /* cst/1 */
+#define R200_EMIT_RE_POINTSIZE                      59 /* cst/1 */
+#define R200_EMIT_TCL_INPUT_VTX_VECTOR_ADDR_0       60 /* cst/4 */
+#define R200_EMIT_PP_CUBIC_FACES_0                  61
+#define R200_EMIT_PP_CUBIC_OFFSETS_0                62
+#define R200_EMIT_PP_CUBIC_FACES_1                  63
+#define R200_EMIT_PP_CUBIC_OFFSETS_1                64
+#define R200_EMIT_PP_CUBIC_FACES_2                  65
+#define R200_EMIT_PP_CUBIC_OFFSETS_2                66
+#define R200_EMIT_PP_CUBIC_FACES_3                  67
+#define R200_EMIT_PP_CUBIC_OFFSETS_3                68
+#define R200_EMIT_PP_CUBIC_FACES_4                  69
+#define R200_EMIT_PP_CUBIC_OFFSETS_4                70
+#define R200_EMIT_PP_CUBIC_FACES_5                  71
+#define R200_EMIT_PP_CUBIC_OFFSETS_5                72
+#define RADEON_EMIT_PP_TEX_SIZE_0                   73
+#define RADEON_EMIT_PP_TEX_SIZE_1                   74
+#define RADEON_EMIT_PP_TEX_SIZE_2                   75
+#define R200_EMIT_RB3D_BLENDCOLOR                   76
+#define R200_EMIT_TCL_POINT_SPRITE_CNTL             77
+#define RADEON_EMIT_PP_CUBIC_FACES_0                78
+#define RADEON_EMIT_PP_CUBIC_OFFSETS_T0             79
+#define RADEON_EMIT_PP_CUBIC_FACES_1                80
+#define RADEON_EMIT_PP_CUBIC_OFFSETS_T1             81
+#define RADEON_EMIT_PP_CUBIC_FACES_2                82
+#define RADEON_EMIT_PP_CUBIC_OFFSETS_T2             83
+#define R200_EMIT_PP_TRI_PERF_CNTL                  84
+#define R200_EMIT_PP_AFS_0                          85
+#define R200_EMIT_PP_AFS_1                          86
+#define R200_EMIT_ATF_TFACTOR                       87
+#define R200_EMIT_PP_TXCTLALL_0                     88
+#define R200_EMIT_PP_TXCTLALL_1                     89
+#define R200_EMIT_PP_TXCTLALL_2                     90
+#define R200_EMIT_PP_TXCTLALL_3                     91
+#define R200_EMIT_PP_TXCTLALL_4                     92
+#define R200_EMIT_PP_TXCTLALL_5                     93
+#define R200_EMIT_VAP_PVS_CNTL                      94
+#define RADEON_MAX_STATE_PACKETS                    95
+
+/* Commands understood by cmd_buffer ioctl.  More can be added but
+ * obviously these can't be removed or changed:
+ */
+#define RADEON_CMD_PACKET      1       /* emit one of the register packets above */
+#define RADEON_CMD_SCALARS     2       /* emit scalar data */
+#define RADEON_CMD_VECTORS     3       /* emit vector data */
+#define RADEON_CMD_DMA_DISCARD 4       /* discard current dma buf */
+#define RADEON_CMD_PACKET3     5       /* emit hw packet */
+#define RADEON_CMD_PACKET3_CLIP 6      /* emit hw packet wrapped in cliprects */
+#define RADEON_CMD_SCALARS2     7      /* r200 stopgap */
+#define RADEON_CMD_WAIT         8      /* emit hw wait commands -- note:
+                                        *  doesn't make the cpu wait, just
+                                        *  the graphics hardware */
+#define RADEON_CMD_VECLINEAR   9       /* another r200 stopgap */
+
+typedef union {
+       int i;
+       struct {
+               unsigned char cmd_type, pad0, pad1, pad2;
+       } header;
+       struct {
+               unsigned char cmd_type, packet_id, pad0, pad1;
+       } packet;
+       struct {
+               unsigned char cmd_type, offset, stride, count;
+       } scalars;
+       struct {
+               unsigned char cmd_type, offset, stride, count;
+       } vectors;
+       struct {
+               unsigned char cmd_type, addr_lo, addr_hi, count;
+       } veclinear;
+       struct {
+               unsigned char cmd_type, buf_idx, pad0, pad1;
+       } dma;
+       struct {
+               unsigned char cmd_type, flags, pad0, pad1;
+       } wait;
+} drm_radeon_cmd_header_t;
+
+#define RADEON_WAIT_2D  0x1
+#define RADEON_WAIT_3D  0x2
+
+/* Allowed parameters for R300_CMD_PACKET3
+ */
+#define R300_CMD_PACKET3_CLEAR         0
+#define R300_CMD_PACKET3_RAW           1
+
+/* Commands understood by cmd_buffer ioctl for R300.
+ * The interface has not been stabilized, so some of these may be removed
+ * and eventually reordered before stabilization.
+ */
+#define R300_CMD_PACKET0               1
+#define R300_CMD_VPU                   2       /* emit vertex program upload */
+#define R300_CMD_PACKET3               3       /* emit a packet3 */
+#define R300_CMD_END3D                 4       /* emit sequence ending 3d rendering */
+#define R300_CMD_CP_DELAY              5
+#define R300_CMD_DMA_DISCARD           6
+#define R300_CMD_WAIT                  7
+#      define R300_WAIT_2D             0x1
+#      define R300_WAIT_3D             0x2
+/* these two defines are DOING IT WRONG - however
+ * we have userspace which relies on using these.
+ * The wait interface is backwards compat new 
+ * code should use the NEW_WAIT defines below
+ * THESE ARE NOT BIT FIELDS
+ */
+#      define R300_WAIT_2D_CLEAN       0x3
+#      define R300_WAIT_3D_CLEAN       0x4
+
+#      define R300_NEW_WAIT_2D_3D      0x3
+#      define R300_NEW_WAIT_2D_2D_CLEAN        0x4
+#      define R300_NEW_WAIT_3D_3D_CLEAN        0x6
+#      define R300_NEW_WAIT_2D_2D_CLEAN_3D_3D_CLEAN    0x8
+
+#define R300_CMD_SCRATCH               8
+#define R300_CMD_R500FP                 9
+
+typedef union {
+       unsigned int u;
+       struct {
+               unsigned char cmd_type, pad0, pad1, pad2;
+       } header;
+       struct {
+               unsigned char cmd_type, count, reglo, reghi;
+       } packet0;
+       struct {
+               unsigned char cmd_type, count, adrlo, adrhi;
+       } vpu;
+       struct {
+               unsigned char cmd_type, packet, pad0, pad1;
+       } packet3;
+       struct {
+               unsigned char cmd_type, packet;
+               unsigned short count;   /* amount of packet2 to emit */
+       } delay;
+       struct {
+               unsigned char cmd_type, buf_idx, pad0, pad1;
+       } dma;
+       struct {
+               unsigned char cmd_type, flags, pad0, pad1;
+       } wait;
+       struct {
+               unsigned char cmd_type, reg, n_bufs, flags;
+       } scratch;
+       struct {
+               unsigned char cmd_type, count, adrlo, adrhi_flags;
+       } r500fp;
+} drm_r300_cmd_header_t;
+
+#define RADEON_FRONT                   0x1
+#define RADEON_BACK                    0x2
+#define RADEON_DEPTH                   0x4
+#define RADEON_STENCIL                 0x8
+#define RADEON_CLEAR_FASTZ             0x80000000
+#define RADEON_USE_HIERZ               0x40000000
+#define RADEON_USE_COMP_ZBUF           0x20000000
+
+#define R500FP_CONSTANT_TYPE  (1 << 1)
+#define R500FP_CONSTANT_CLAMP (1 << 2)
+
+/* Primitive types
+ */
+#define RADEON_POINTS                  0x1
+#define RADEON_LINES                   0x2
+#define RADEON_LINE_STRIP              0x3
+#define RADEON_TRIANGLES               0x4
+#define RADEON_TRIANGLE_FAN            0x5
+#define RADEON_TRIANGLE_STRIP          0x6
+
+/* Vertex/indirect buffer size
+ */
+#define RADEON_BUFFER_SIZE             65536
+
+/* Byte offsets for indirect buffer data
+ */
+#define RADEON_INDEX_PRIM_OFFSET       20
+
+#define RADEON_SCRATCH_REG_OFFSET      32
+
+#define R600_SCRATCH_REG_OFFSET         256
+
+#define RADEON_NR_SAREA_CLIPRECTS      12
+
+/* There are 2 heaps (local/GART).  Each region within a heap is a
+ * minimum of 64k, and there are at most 64 of them per heap.
+ */
+#define RADEON_LOCAL_TEX_HEAP          0
+#define RADEON_GART_TEX_HEAP           1
+#define RADEON_NR_TEX_HEAPS            2
+#define RADEON_NR_TEX_REGIONS          64
+#define RADEON_LOG_TEX_GRANULARITY     16
+
+#define RADEON_MAX_TEXTURE_LEVELS      12
+#define RADEON_MAX_TEXTURE_UNITS       3
+
+#define RADEON_MAX_SURFACES            8
+
+/* Blits have strict offset rules.  All blit offset must be aligned on
+ * a 1K-byte boundary.
+ */
+#define RADEON_OFFSET_SHIFT             10
+#define RADEON_OFFSET_ALIGN             (1 << RADEON_OFFSET_SHIFT)
+#define RADEON_OFFSET_MASK              (RADEON_OFFSET_ALIGN - 1)
+
+#endif                         /* __RADEON_SAREA_DEFINES__ */
+
+typedef struct {
+       unsigned int red;
+       unsigned int green;
+       unsigned int blue;
+       unsigned int alpha;
+} radeon_color_regs_t;
+
+typedef struct {
+       /* Context state */
+       unsigned int pp_misc;   /* 0x1c14 */
+       unsigned int pp_fog_color;
+       unsigned int re_solid_color;
+       unsigned int rb3d_blendcntl;
+       unsigned int rb3d_depthoffset;
+       unsigned int rb3d_depthpitch;
+       unsigned int rb3d_zstencilcntl;
+
+       unsigned int pp_cntl;   /* 0x1c38 */
+       unsigned int rb3d_cntl;
+       unsigned int rb3d_coloroffset;
+       unsigned int re_width_height;
+       unsigned int rb3d_colorpitch;
+       unsigned int se_cntl;
+
+       /* Vertex format state */
+       unsigned int se_coord_fmt;      /* 0x1c50 */
+
+       /* Line state */
+       unsigned int re_line_pattern;   /* 0x1cd0 */
+       unsigned int re_line_state;
+
+       unsigned int se_line_width;     /* 0x1db8 */
+
+       /* Bumpmap state */
+       unsigned int pp_lum_matrix;     /* 0x1d00 */
+
+       unsigned int pp_rot_matrix_0;   /* 0x1d58 */
+       unsigned int pp_rot_matrix_1;
+
+       /* Mask state */
+       unsigned int rb3d_stencilrefmask;       /* 0x1d7c */
+       unsigned int rb3d_ropcntl;
+       unsigned int rb3d_planemask;
+
+       /* Viewport state */
+       unsigned int se_vport_xscale;   /* 0x1d98 */
+       unsigned int se_vport_xoffset;
+       unsigned int se_vport_yscale;
+       unsigned int se_vport_yoffset;
+       unsigned int se_vport_zscale;
+       unsigned int se_vport_zoffset;
+
+       /* Setup state */
+       unsigned int se_cntl_status;    /* 0x2140 */
+
+       /* Misc state */
+       unsigned int re_top_left;       /* 0x26c0 */
+       unsigned int re_misc;
+} drm_radeon_context_regs_t;
+
+typedef struct {
+       /* Zbias state */
+       unsigned int se_zbias_factor;   /* 0x1dac */
+       unsigned int se_zbias_constant;
+} drm_radeon_context2_regs_t;
+
+/* Setup registers for each texture unit
+ */
+typedef struct {
+       unsigned int pp_txfilter;
+       unsigned int pp_txformat;
+       unsigned int pp_txoffset;
+       unsigned int pp_txcblend;
+       unsigned int pp_txablend;
+       unsigned int pp_tfactor;
+       unsigned int pp_border_color;
+} drm_radeon_texture_regs_t;
+
+typedef struct {
+       unsigned int start;
+       unsigned int finish;
+       unsigned int prim:8;
+       unsigned int stateidx:8;
+       unsigned int numverts:16;       /* overloaded as offset/64 for elt prims */
+       unsigned int vc_format; /* vertex format */
+} drm_radeon_prim_t;
+
+typedef struct {
+       drm_radeon_context_regs_t context;
+       drm_radeon_texture_regs_t tex[RADEON_MAX_TEXTURE_UNITS];
+       drm_radeon_context2_regs_t context2;
+       unsigned int dirty;
+} drm_radeon_state_t;
+
+typedef struct {
+       /* The channel for communication of state information to the
+        * kernel on firing a vertex buffer with either of the
+        * obsoleted vertex/index ioctls.
+        */
+       drm_radeon_context_regs_t context_state;
+       drm_radeon_texture_regs_t tex_state[RADEON_MAX_TEXTURE_UNITS];
+       unsigned int dirty;
+       unsigned int vertsize;
+       unsigned int vc_format;
+
+       /* The current cliprects, or a subset thereof.
+        */
+       struct drm_clip_rect boxes[RADEON_NR_SAREA_CLIPRECTS];
+       unsigned int nbox;
+
+       /* Counters for client-side throttling of rendering clients.
+        */
+       unsigned int last_frame;
+       unsigned int last_dispatch;
+       unsigned int last_clear;
+
+       struct drm_tex_region tex_list[RADEON_NR_TEX_HEAPS][RADEON_NR_TEX_REGIONS +
+                                                      1];
+       unsigned int tex_age[RADEON_NR_TEX_HEAPS];
+       int ctx_owner;
+       int pfState;            /* number of 3d windows (0,1,2ormore) */
+       int pfCurrentPage;      /* which buffer is being displayed? */
+       int crtc2_base;         /* CRTC2 frame offset */
+       int tiling_enabled;     /* set by drm, read by 2d + 3d clients */
+} drm_radeon_sarea_t;
+
+/* WARNING: If you change any of these defines, make sure to change the
+ * defines in the Xserver file (xf86drmRadeon.h)
+ *
+ * KW: actually it's illegal to change any of this (backwards compatibility).
+ */
+
+/* Radeon specific ioctls
+ * The device specific ioctl range is 0x40 to 0x79.
+ */
+#define DRM_RADEON_CP_INIT    0x00
+#define DRM_RADEON_CP_START   0x01
+#define DRM_RADEON_CP_STOP    0x02
+#define DRM_RADEON_CP_RESET   0x03
+#define DRM_RADEON_CP_IDLE    0x04
+#define DRM_RADEON_RESET      0x05
+#define DRM_RADEON_FULLSCREEN 0x06
+#define DRM_RADEON_SWAP       0x07
+#define DRM_RADEON_CLEAR      0x08
+#define DRM_RADEON_VERTEX     0x09
+#define DRM_RADEON_INDICES    0x0A
+#define DRM_RADEON_NOT_USED
+#define DRM_RADEON_STIPPLE    0x0C
+#define DRM_RADEON_INDIRECT   0x0D
+#define DRM_RADEON_TEXTURE    0x0E
+#define DRM_RADEON_VERTEX2    0x0F
+#define DRM_RADEON_CMDBUF     0x10
+#define DRM_RADEON_GETPARAM   0x11
+#define DRM_RADEON_FLIP       0x12
+#define DRM_RADEON_ALLOC      0x13
+#define DRM_RADEON_FREE       0x14
+#define DRM_RADEON_INIT_HEAP  0x15
+#define DRM_RADEON_IRQ_EMIT   0x16
+#define DRM_RADEON_IRQ_WAIT   0x17
+#define DRM_RADEON_CP_RESUME  0x18
+#define DRM_RADEON_SETPARAM   0x19
+#define DRM_RADEON_SURF_ALLOC 0x1a
+#define DRM_RADEON_SURF_FREE  0x1b
+/* KMS ioctl */
+#define DRM_RADEON_GEM_INFO            0x1c
+#define DRM_RADEON_GEM_CREATE          0x1d
+#define DRM_RADEON_GEM_MMAP            0x1e
+#define DRM_RADEON_GEM_PREAD           0x21
+#define DRM_RADEON_GEM_PWRITE          0x22
+#define DRM_RADEON_GEM_SET_DOMAIN      0x23
+#define DRM_RADEON_GEM_WAIT_IDLE       0x24
+#define DRM_RADEON_CS                  0x26
+#define DRM_RADEON_INFO                        0x27
+#define DRM_RADEON_GEM_SET_TILING      0x28
+#define DRM_RADEON_GEM_GET_TILING      0x29
+#define DRM_RADEON_GEM_BUSY            0x2a
+
+#define DRM_IOCTL_RADEON_CP_INIT    DRM_IOW( DRM_COMMAND_BASE + DRM_RADEON_CP_INIT, drm_radeon_init_t)
+#define DRM_IOCTL_RADEON_CP_START   DRM_IO(  DRM_COMMAND_BASE + DRM_RADEON_CP_START)
+#define DRM_IOCTL_RADEON_CP_STOP    DRM_IOW( DRM_COMMAND_BASE + DRM_RADEON_CP_STOP, drm_radeon_cp_stop_t)
+#define DRM_IOCTL_RADEON_CP_RESET   DRM_IO(  DRM_COMMAND_BASE + DRM_RADEON_CP_RESET)
+#define DRM_IOCTL_RADEON_CP_IDLE    DRM_IO(  DRM_COMMAND_BASE + DRM_RADEON_CP_IDLE)
+#define DRM_IOCTL_RADEON_RESET      DRM_IO(  DRM_COMMAND_BASE + DRM_RADEON_RESET)
+#define DRM_IOCTL_RADEON_FULLSCREEN DRM_IOW( DRM_COMMAND_BASE + DRM_RADEON_FULLSCREEN, drm_radeon_fullscreen_t)
+#define DRM_IOCTL_RADEON_SWAP       DRM_IO(  DRM_COMMAND_BASE + DRM_RADEON_SWAP)
+#define DRM_IOCTL_RADEON_CLEAR      DRM_IOW( DRM_COMMAND_BASE + DRM_RADEON_CLEAR, drm_radeon_clear_t)
+#define DRM_IOCTL_RADEON_VERTEX     DRM_IOW( DRM_COMMAND_BASE + DRM_RADEON_VERTEX, drm_radeon_vertex_t)
+#define DRM_IOCTL_RADEON_INDICES    DRM_IOW( DRM_COMMAND_BASE + DRM_RADEON_INDICES, drm_radeon_indices_t)
+#define DRM_IOCTL_RADEON_STIPPLE    DRM_IOW( DRM_COMMAND_BASE + DRM_RADEON_STIPPLE, drm_radeon_stipple_t)
+#define DRM_IOCTL_RADEON_INDIRECT   DRM_IOWR(DRM_COMMAND_BASE + DRM_RADEON_INDIRECT, drm_radeon_indirect_t)
+#define DRM_IOCTL_RADEON_TEXTURE    DRM_IOWR(DRM_COMMAND_BASE + DRM_RADEON_TEXTURE, drm_radeon_texture_t)
+#define DRM_IOCTL_RADEON_VERTEX2    DRM_IOW( DRM_COMMAND_BASE + DRM_RADEON_VERTEX2, drm_radeon_vertex2_t)
+#define DRM_IOCTL_RADEON_CMDBUF     DRM_IOW( DRM_COMMAND_BASE + DRM_RADEON_CMDBUF, drm_radeon_cmd_buffer_t)
+#define DRM_IOCTL_RADEON_GETPARAM   DRM_IOWR(DRM_COMMAND_BASE + DRM_RADEON_GETPARAM, drm_radeon_getparam_t)
+#define DRM_IOCTL_RADEON_FLIP       DRM_IO(  DRM_COMMAND_BASE + DRM_RADEON_FLIP)
+#define DRM_IOCTL_RADEON_ALLOC      DRM_IOWR(DRM_COMMAND_BASE + DRM_RADEON_ALLOC, drm_radeon_mem_alloc_t)
+#define DRM_IOCTL_RADEON_FREE       DRM_IOW( DRM_COMMAND_BASE + DRM_RADEON_FREE, drm_radeon_mem_free_t)
+#define DRM_IOCTL_RADEON_INIT_HEAP  DRM_IOW( DRM_COMMAND_BASE + DRM_RADEON_INIT_HEAP, drm_radeon_mem_init_heap_t)
+#define DRM_IOCTL_RADEON_IRQ_EMIT   DRM_IOWR(DRM_COMMAND_BASE + DRM_RADEON_IRQ_EMIT, drm_radeon_irq_emit_t)
+#define DRM_IOCTL_RADEON_IRQ_WAIT   DRM_IOW( DRM_COMMAND_BASE + DRM_RADEON_IRQ_WAIT, drm_radeon_irq_wait_t)
+#define DRM_IOCTL_RADEON_CP_RESUME  DRM_IO(  DRM_COMMAND_BASE + DRM_RADEON_CP_RESUME)
+#define DRM_IOCTL_RADEON_SETPARAM   DRM_IOW( DRM_COMMAND_BASE + DRM_RADEON_SETPARAM, drm_radeon_setparam_t)
+#define DRM_IOCTL_RADEON_SURF_ALLOC DRM_IOW( DRM_COMMAND_BASE + DRM_RADEON_SURF_ALLOC, drm_radeon_surface_alloc_t)
+#define DRM_IOCTL_RADEON_SURF_FREE  DRM_IOW( DRM_COMMAND_BASE + DRM_RADEON_SURF_FREE, drm_radeon_surface_free_t)
+/* KMS */
+#define DRM_IOCTL_RADEON_GEM_INFO      DRM_IOWR(DRM_COMMAND_BASE + DRM_RADEON_GEM_INFO, struct drm_radeon_gem_info)
+#define DRM_IOCTL_RADEON_GEM_CREATE    DRM_IOWR(DRM_COMMAND_BASE + DRM_RADEON_GEM_CREATE, struct drm_radeon_gem_create)
+#define DRM_IOCTL_RADEON_GEM_MMAP      DRM_IOWR(DRM_COMMAND_BASE + DRM_RADEON_GEM_MMAP, struct drm_radeon_gem_mmap)
+#define DRM_IOCTL_RADEON_GEM_PREAD     DRM_IOWR(DRM_COMMAND_BASE + DRM_RADEON_GEM_PREAD, struct drm_radeon_gem_pread)
+#define DRM_IOCTL_RADEON_GEM_PWRITE    DRM_IOWR(DRM_COMMAND_BASE + DRM_RADEON_GEM_PWRITE, struct drm_radeon_gem_pwrite)
+#define DRM_IOCTL_RADEON_GEM_SET_DOMAIN        DRM_IOWR(DRM_COMMAND_BASE + DRM_RADEON_GEM_SET_DOMAIN, struct drm_radeon_gem_set_domain)
+#define DRM_IOCTL_RADEON_GEM_WAIT_IDLE DRM_IOW(DRM_COMMAND_BASE + DRM_RADEON_GEM_WAIT_IDLE, struct drm_radeon_gem_wait_idle)
+#define DRM_IOCTL_RADEON_CS            DRM_IOWR(DRM_COMMAND_BASE + DRM_RADEON_CS, struct drm_radeon_cs)
+#define DRM_IOCTL_RADEON_INFO          DRM_IOWR(DRM_COMMAND_BASE + DRM_RADEON_INFO, struct drm_radeon_info)
+#define DRM_IOCTL_RADEON_SET_TILING    DRM_IOWR(DRM_COMMAND_BASE + DRM_RADEON_GEM_SET_TILING, struct drm_radeon_gem_set_tiling)
+#define DRM_IOCTL_RADEON_GET_TILING    DRM_IOWR(DRM_COMMAND_BASE + DRM_RADEON_GEM_GET_TILING, struct drm_radeon_gem_get_tiling)
+#define DRM_IOCTL_RADEON_GEM_BUSY      DRM_IOWR(DRM_COMMAND_BASE + DRM_RADEON_GEM_BUSY, struct drm_radeon_gem_busy)
+
+typedef struct drm_radeon_init {
+       enum {
+               RADEON_INIT_CP = 0x01,
+               RADEON_CLEANUP_CP = 0x02,
+               RADEON_INIT_R200_CP = 0x03,
+               RADEON_INIT_R300_CP = 0x04,
+               RADEON_INIT_R600_CP = 0x05
+       } func;
+       unsigned long sarea_priv_offset;
+       int is_pci;
+       int cp_mode;
+       int gart_size;
+       int ring_size;
+       int usec_timeout;
+
+       unsigned int fb_bpp;
+       unsigned int front_offset, front_pitch;
+       unsigned int back_offset, back_pitch;
+       unsigned int depth_bpp;
+       unsigned int depth_offset, depth_pitch;
+
+       unsigned long fb_offset;
+       unsigned long mmio_offset;
+       unsigned long ring_offset;
+       unsigned long ring_rptr_offset;
+       unsigned long buffers_offset;
+       unsigned long gart_textures_offset;
+} drm_radeon_init_t;
+
+typedef struct drm_radeon_cp_stop {
+       int flush;
+       int idle;
+} drm_radeon_cp_stop_t;
+
+typedef struct drm_radeon_fullscreen {
+       enum {
+               RADEON_INIT_FULLSCREEN = 0x01,
+               RADEON_CLEANUP_FULLSCREEN = 0x02
+       } func;
+} drm_radeon_fullscreen_t;
+
+#define CLEAR_X1       0
+#define CLEAR_Y1       1
+#define CLEAR_X2       2
+#define CLEAR_Y2       3
+#define CLEAR_DEPTH    4
+
+typedef union drm_radeon_clear_rect {
+       float f[5];
+       unsigned int ui[5];
+} drm_radeon_clear_rect_t;
+
+typedef struct drm_radeon_clear {
+       unsigned int flags;
+       unsigned int clear_color;
+       unsigned int clear_depth;
+       unsigned int color_mask;
+       unsigned int depth_mask;        /* misnamed field:  should be stencil */
+       drm_radeon_clear_rect_t *depth_boxes;
+} drm_radeon_clear_t;
+
+typedef struct drm_radeon_vertex {
+       int prim;
+       int idx;                /* Index of vertex buffer */
+       int count;              /* Number of vertices in buffer */
+       int discard;            /* Client finished with buffer? */
+} drm_radeon_vertex_t;
+
+typedef struct drm_radeon_indices {
+       int prim;
+       int idx;
+       int start;
+       int end;
+       int discard;            /* Client finished with buffer? */
+} drm_radeon_indices_t;
+
+/* v1.2 - obsoletes drm_radeon_vertex and drm_radeon_indices
+ *      - allows multiple primitives and state changes in a single ioctl
+ *      - supports driver change to emit native primitives
+ */
+typedef struct drm_radeon_vertex2 {
+       int idx;                /* Index of vertex buffer */
+       int discard;            /* Client finished with buffer? */
+       int nr_states;
+       drm_radeon_state_t *state;
+       int nr_prims;
+       drm_radeon_prim_t *prim;
+} drm_radeon_vertex2_t;
+
+/* v1.3 - obsoletes drm_radeon_vertex2
+ *      - allows arbitarily large cliprect list
+ *      - allows updating of tcl packet, vector and scalar state
+ *      - allows memory-efficient description of state updates
+ *      - allows state to be emitted without a primitive
+ *           (for clears, ctx switches)
+ *      - allows more than one dma buffer to be referenced per ioctl
+ *      - supports tcl driver
+ *      - may be extended in future versions with new cmd types, packets
+ */
+typedef struct drm_radeon_cmd_buffer {
+       int bufsz;
+       char *buf;
+       int nbox;
+       struct drm_clip_rect *boxes;
+} drm_radeon_cmd_buffer_t;
+
+typedef struct drm_radeon_tex_image {
+       unsigned int x, y;      /* Blit coordinates */
+       unsigned int width, height;
+       const void *data;
+} drm_radeon_tex_image_t;
+
+typedef struct drm_radeon_texture {
+       unsigned int offset;
+       int pitch;
+       int format;
+       int width;              /* Texture image coordinates */
+       int height;
+       drm_radeon_tex_image_t *image;
+} drm_radeon_texture_t;
+
+typedef struct drm_radeon_stipple {
+       unsigned int *mask;
+} drm_radeon_stipple_t;
+
+typedef struct drm_radeon_indirect {
+       int idx;
+       int start;
+       int end;
+       int discard;
+} drm_radeon_indirect_t;
+
+/* enum for card type parameters */
+#define RADEON_CARD_PCI 0
+#define RADEON_CARD_AGP 1
+#define RADEON_CARD_PCIE 2
+
+/* 1.3: An ioctl to get parameters that aren't available to the 3d
+ * client any other way.
+ */
+#define RADEON_PARAM_GART_BUFFER_OFFSET    1   /* card offset of 1st GART buffer */
+#define RADEON_PARAM_LAST_FRAME            2
+#define RADEON_PARAM_LAST_DISPATCH         3
+#define RADEON_PARAM_LAST_CLEAR            4
+/* Added with DRM version 1.6. */
+#define RADEON_PARAM_IRQ_NR                5
+#define RADEON_PARAM_GART_BASE             6   /* card offset of GART base */
+/* Added with DRM version 1.8. */
+#define RADEON_PARAM_REGISTER_HANDLE       7   /* for drmMap() */
+#define RADEON_PARAM_STATUS_HANDLE         8
+#define RADEON_PARAM_SAREA_HANDLE          9
+#define RADEON_PARAM_GART_TEX_HANDLE       10
+#define RADEON_PARAM_SCRATCH_OFFSET        11
+#define RADEON_PARAM_CARD_TYPE             12
+#define RADEON_PARAM_VBLANK_CRTC           13   /* VBLANK CRTC */
+#define RADEON_PARAM_FB_LOCATION           14   /* FB location */
+#define RADEON_PARAM_NUM_GB_PIPES          15   /* num GB pipes */
+#define RADEON_PARAM_DEVICE_ID             16
+#define RADEON_PARAM_NUM_Z_PIPES           17   /* num Z pipes */
+
+typedef struct drm_radeon_getparam {
+       int param;
+       void *value;
+} drm_radeon_getparam_t;
+
+/* 1.6: Set up a memory manager for regions of shared memory:
+ */
+#define RADEON_MEM_REGION_GART 1
+#define RADEON_MEM_REGION_FB   2
+
+typedef struct drm_radeon_mem_alloc {
+       int region;
+       int alignment;
+       int size;
+       int *region_offset;     /* offset from start of fb or GART */
+} drm_radeon_mem_alloc_t;
+
+typedef struct drm_radeon_mem_free {
+       int region;
+       int region_offset;
+} drm_radeon_mem_free_t;
+
+typedef struct drm_radeon_mem_init_heap {
+       int region;
+       int size;
+       int start;
+} drm_radeon_mem_init_heap_t;
+
+/* 1.6: Userspace can request & wait on irq's:
+ */
+typedef struct drm_radeon_irq_emit {
+       int *irq_seq;
+} drm_radeon_irq_emit_t;
+
+typedef struct drm_radeon_irq_wait {
+       int irq_seq;
+} drm_radeon_irq_wait_t;
+
+/* 1.10: Clients tell the DRM where they think the framebuffer is located in
+ * the card's address space, via a new generic ioctl to set parameters
+ */
+
+typedef struct drm_radeon_setparam {
+       unsigned int param;
+       __s64 value;
+} drm_radeon_setparam_t;
+
+#define RADEON_SETPARAM_FB_LOCATION    1       /* determined framebuffer location */
+#define RADEON_SETPARAM_SWITCH_TILING  2       /* enable/disable color tiling */
+#define RADEON_SETPARAM_PCIGART_LOCATION 3     /* PCI Gart Location */
+#define RADEON_SETPARAM_NEW_MEMMAP 4           /* Use new memory map */
+#define RADEON_SETPARAM_PCIGART_TABLE_SIZE 5    /* PCI GART Table Size */
+#define RADEON_SETPARAM_VBLANK_CRTC 6           /* VBLANK CRTC */
+/* 1.14: Clients can allocate/free a surface
+ */
+typedef struct drm_radeon_surface_alloc {
+       unsigned int address;
+       unsigned int size;
+       unsigned int flags;
+} drm_radeon_surface_alloc_t;
+
+typedef struct drm_radeon_surface_free {
+       unsigned int address;
+} drm_radeon_surface_free_t;
+
+#define        DRM_RADEON_VBLANK_CRTC1         1
+#define        DRM_RADEON_VBLANK_CRTC2         2
+
+/*
+ * Kernel modesetting world below.
+ */
+#define RADEON_GEM_DOMAIN_CPU          0x1
+#define RADEON_GEM_DOMAIN_GTT          0x2
+#define RADEON_GEM_DOMAIN_VRAM         0x4
+
+struct drm_radeon_gem_info {
+       uint64_t        gart_size;
+       uint64_t        vram_size;
+       uint64_t        vram_visible;
+};
+
+#define RADEON_GEM_NO_BACKING_STORE 1
+
+struct drm_radeon_gem_create {
+       uint64_t        size;
+       uint64_t        alignment;
+       uint32_t        handle;
+       uint32_t        initial_domain;
+       uint32_t        flags;
+};
+
+#define RADEON_TILING_MACRO       0x1
+#define RADEON_TILING_MICRO       0x2
+#define RADEON_TILING_SWAP_16BIT  0x4
+#define RADEON_TILING_SWAP_32BIT  0x8
+#define RADEON_TILING_SURFACE     0x10 /* this object requires a surface
+                                       * when mapped - i.e. front buffer */
+#define RADEON_TILING_MICRO_SQUARE 0x20
+
+struct drm_radeon_gem_set_tiling {
+       uint32_t        handle;
+       uint32_t        tiling_flags;
+       uint32_t        pitch;
+};
+
+struct drm_radeon_gem_get_tiling {
+       uint32_t        handle;
+       uint32_t        tiling_flags;
+       uint32_t        pitch;
+};
+
+struct drm_radeon_gem_mmap {
+       uint32_t        handle;
+       uint32_t        pad;
+       uint64_t        offset;
+       uint64_t        size;
+       uint64_t        addr_ptr;
+};
+
+struct drm_radeon_gem_set_domain {
+       uint32_t        handle;
+       uint32_t        read_domains;
+       uint32_t        write_domain;
+};
+
+struct drm_radeon_gem_wait_idle {
+       uint32_t        handle;
+       uint32_t        pad;
+};
+
+struct drm_radeon_gem_busy {
+       uint32_t        handle;
+       uint32_t        domain;
+};
+
+struct drm_radeon_gem_pread {
+       /** Handle for the object being read. */
+       uint32_t handle;
+       uint32_t pad;
+       /** Offset into the object to read from */
+       uint64_t offset;
+       /** Length of data to read */
+       uint64_t size;
+       /** Pointer to write the data into. */
+       /* void *, but pointers are not 32/64 compatible */
+       uint64_t data_ptr;
+};
+
+struct drm_radeon_gem_pwrite {
+       /** Handle for the object being written to. */
+       uint32_t handle;
+       uint32_t pad;
+       /** Offset into the object to write to */
+       uint64_t offset;
+       /** Length of data to write */
+       uint64_t size;
+       /** Pointer to read the data from. */
+       /* void *, but pointers are not 32/64 compatible */
+       uint64_t data_ptr;
+};
+
+#define RADEON_CHUNK_ID_RELOCS 0x01
+#define RADEON_CHUNK_ID_IB     0x02
+
+struct drm_radeon_cs_chunk {
+       uint32_t                chunk_id;
+       uint32_t                length_dw;
+       uint64_t                chunk_data;
+};
+
+struct drm_radeon_cs_reloc {
+       uint32_t                handle;
+       uint32_t                read_domains;
+       uint32_t                write_domain;
+       uint32_t                flags;
+};
+
+struct drm_radeon_cs {
+       uint32_t                num_chunks;
+       uint32_t                cs_id;
+       /* this points to uint64_t * which point to cs chunks */
+       uint64_t                chunks;
+       /* updates to the limits after this CS ioctl */
+       uint64_t                gart_limit;
+       uint64_t                vram_limit;
+};
+
+#define RADEON_INFO_DEVICE_ID          0x00
+#define RADEON_INFO_NUM_GB_PIPES       0x01
+#define RADEON_INFO_NUM_Z_PIPES        0x02
+#define RADEON_INFO_ACCEL_WORKING      0x03
+#define RADEON_INFO_CRTC_FROM_ID       0x04
+#define RADEON_INFO_ACCEL_WORKING2     0x05
+#define RADEON_INFO_TILING_CONFIG      0x06
+#define RADEON_INFO_WANT_HYPERZ                0x07
+
+struct drm_radeon_info {
+       uint32_t                request;
+       uint32_t                pad;
+       uint64_t                value;
+};
+
+#endif
diff --git a/include/drm/savage_drm.h b/include/drm/savage_drm.h
new file mode 100644 (file)
index 0000000..f7a75ef
--- /dev/null
@@ -0,0 +1,210 @@
+/* savage_drm.h -- Public header for the savage driver
+ *
+ * Copyright 2004  Felix Kuehling
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sub license,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NON-INFRINGEMENT. IN NO EVENT SHALL FELIX KUEHLING BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
+ * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef __SAVAGE_DRM_H__
+#define __SAVAGE_DRM_H__
+
+#ifndef __SAVAGE_SAREA_DEFINES__
+#define __SAVAGE_SAREA_DEFINES__
+
+/* 2 heaps (1 for card, 1 for agp), each divided into upto 128
+ * regions, subject to a minimum region size of (1<<16) == 64k.
+ *
+ * Clients may subdivide regions internally, but when sharing between
+ * clients, the region size is the minimum granularity.
+ */
+
+#define SAVAGE_CARD_HEAP               0
+#define SAVAGE_AGP_HEAP                        1
+#define SAVAGE_NR_TEX_HEAPS            2
+#define SAVAGE_NR_TEX_REGIONS          16
+#define SAVAGE_LOG_MIN_TEX_REGION_SIZE 16
+
+#endif                         /* __SAVAGE_SAREA_DEFINES__ */
+
+typedef struct _drm_savage_sarea {
+       /* LRU lists for texture memory in agp space and on the card.
+        */
+       struct drm_tex_region texList[SAVAGE_NR_TEX_HEAPS][SAVAGE_NR_TEX_REGIONS +
+                                                     1];
+       unsigned int texAge[SAVAGE_NR_TEX_HEAPS];
+
+       /* Mechanism to validate card state.
+        */
+       int ctxOwner;
+} drm_savage_sarea_t, *drm_savage_sarea_ptr;
+
+/* Savage-specific ioctls
+ */
+#define DRM_SAVAGE_BCI_INIT            0x00
+#define DRM_SAVAGE_BCI_CMDBUF           0x01
+#define DRM_SAVAGE_BCI_EVENT_EMIT      0x02
+#define DRM_SAVAGE_BCI_EVENT_WAIT      0x03
+
+#define DRM_IOCTL_SAVAGE_INIT          DRM_IOW( DRM_COMMAND_BASE + DRM_SAVAGE_BCI_INIT, drm_savage_init_t)
+#define DRM_IOCTL_SAVAGE_CMDBUF                DRM_IOW( DRM_COMMAND_BASE + DRM_SAVAGE_BCI_CMDBUF, drm_savage_cmdbuf_t)
+#define DRM_IOCTL_SAVAGE_EVENT_EMIT    DRM_IOWR(DRM_COMMAND_BASE + DRM_SAVAGE_BCI_EVENT_EMIT, drm_savage_event_emit_t)
+#define DRM_IOCTL_SAVAGE_EVENT_WAIT    DRM_IOW( DRM_COMMAND_BASE + DRM_SAVAGE_BCI_EVENT_WAIT, drm_savage_event_wait_t)
+
+#define SAVAGE_DMA_PCI 1
+#define SAVAGE_DMA_AGP 3
+typedef struct drm_savage_init {
+       enum {
+               SAVAGE_INIT_BCI = 1,
+               SAVAGE_CLEANUP_BCI = 2
+       } func;
+       unsigned int sarea_priv_offset;
+
+       /* some parameters */
+       unsigned int cob_size;
+       unsigned int bci_threshold_lo, bci_threshold_hi;
+       unsigned int dma_type;
+
+       /* frame buffer layout */
+       unsigned int fb_bpp;
+       unsigned int front_offset, front_pitch;
+       unsigned int back_offset, back_pitch;
+       unsigned int depth_bpp;
+       unsigned int depth_offset, depth_pitch;
+
+       /* local textures */
+       unsigned int texture_offset;
+       unsigned int texture_size;
+
+       /* physical locations of non-permanent maps */
+       unsigned long status_offset;
+       unsigned long buffers_offset;
+       unsigned long agp_textures_offset;
+       unsigned long cmd_dma_offset;
+} drm_savage_init_t;
+
+typedef union drm_savage_cmd_header drm_savage_cmd_header_t;
+typedef struct drm_savage_cmdbuf {
+       /* command buffer in client's address space */
+       drm_savage_cmd_header_t *cmd_addr;
+       unsigned int size;      /* size of the command buffer in 64bit units */
+
+       unsigned int dma_idx;   /* DMA buffer index to use */
+       int discard;            /* discard DMA buffer when done */
+       /* vertex buffer in client's address space */
+       unsigned int *vb_addr;
+       unsigned int vb_size;   /* size of client vertex buffer in bytes */
+       unsigned int vb_stride; /* stride of vertices in 32bit words */
+       /* boxes in client's address space */
+       struct drm_clip_rect *box_addr;
+       unsigned int nbox;      /* number of clipping boxes */
+} drm_savage_cmdbuf_t;
+
+#define SAVAGE_WAIT_2D  0x1    /* wait for 2D idle before updating event tag */
+#define SAVAGE_WAIT_3D  0x2    /* wait for 3D idle before updating event tag */
+#define SAVAGE_WAIT_IRQ 0x4    /* emit or wait for IRQ, not implemented yet */
+typedef struct drm_savage_event {
+       unsigned int count;
+       unsigned int flags;
+} drm_savage_event_emit_t, drm_savage_event_wait_t;
+
+/* Commands for the cmdbuf ioctl
+ */
+#define SAVAGE_CMD_STATE       0       /* a range of state registers */
+#define SAVAGE_CMD_DMA_PRIM    1       /* vertices from DMA buffer */
+#define SAVAGE_CMD_VB_PRIM     2       /* vertices from client vertex buffer */
+#define SAVAGE_CMD_DMA_IDX     3       /* indexed vertices from DMA buffer */
+#define SAVAGE_CMD_VB_IDX      4       /* indexed vertices client vertex buffer */
+#define SAVAGE_CMD_CLEAR       5       /* clear buffers */
+#define SAVAGE_CMD_SWAP                6       /* swap buffers */
+
+/* Primitive types
+*/
+#define SAVAGE_PRIM_TRILIST    0       /* triangle list */
+#define SAVAGE_PRIM_TRISTRIP   1       /* triangle strip */
+#define SAVAGE_PRIM_TRIFAN     2       /* triangle fan */
+#define SAVAGE_PRIM_TRILIST_201        3       /* reorder verts for correct flat
+                                        * shading on s3d */
+
+/* Skip flags (vertex format)
+ */
+#define SAVAGE_SKIP_Z          0x01
+#define SAVAGE_SKIP_W          0x02
+#define SAVAGE_SKIP_C0         0x04
+#define SAVAGE_SKIP_C1         0x08
+#define SAVAGE_SKIP_S0         0x10
+#define SAVAGE_SKIP_T0         0x20
+#define SAVAGE_SKIP_ST0                0x30
+#define SAVAGE_SKIP_S1         0x40
+#define SAVAGE_SKIP_T1         0x80
+#define SAVAGE_SKIP_ST1                0xc0
+#define SAVAGE_SKIP_ALL_S3D    0x3f
+#define SAVAGE_SKIP_ALL_S4     0xff
+
+/* Buffer names for clear command
+ */
+#define SAVAGE_FRONT           0x1
+#define SAVAGE_BACK            0x2
+#define SAVAGE_DEPTH           0x4
+
+/* 64-bit command header
+ */
+union drm_savage_cmd_header {
+       struct {
+               unsigned char cmd;      /* command */
+               unsigned char pad0;
+               unsigned short pad1;
+               unsigned short pad2;
+               unsigned short pad3;
+       } cmd;                  /* generic */
+       struct {
+               unsigned char cmd;
+               unsigned char global;   /* need idle engine? */
+               unsigned short count;   /* number of consecutive registers */
+               unsigned short start;   /* first register */
+               unsigned short pad3;
+       } state;                /* SAVAGE_CMD_STATE */
+       struct {
+               unsigned char cmd;
+               unsigned char prim;     /* primitive type */
+               unsigned short skip;    /* vertex format (skip flags) */
+               unsigned short count;   /* number of vertices */
+               unsigned short start;   /* first vertex in DMA/vertex buffer */
+       } prim;                 /* SAVAGE_CMD_DMA_PRIM, SAVAGE_CMD_VB_PRIM */
+       struct {
+               unsigned char cmd;
+               unsigned char prim;
+               unsigned short skip;
+               unsigned short count;   /* number of indices that follow */
+               unsigned short pad3;
+       } idx;                  /* SAVAGE_CMD_DMA_IDX, SAVAGE_CMD_VB_IDX */
+       struct {
+               unsigned char cmd;
+               unsigned char pad0;
+               unsigned short pad1;
+               unsigned int flags;
+       } clear0;               /* SAVAGE_CMD_CLEAR */
+       struct {
+               unsigned int mask;
+               unsigned int value;
+       } clear1;               /* SAVAGE_CMD_CLEAR data */
+};
+
+#endif
diff --git a/include/drm/sis_drm.h b/include/drm/sis_drm.h
new file mode 100644 (file)
index 0000000..30f7b38
--- /dev/null
@@ -0,0 +1,67 @@
+/* sis_drv.h -- Private header for sis driver -*- linux-c -*- */
+/*
+ * Copyright 2005 Eric Anholt
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ */
+
+#ifndef __SIS_DRM_H__
+#define __SIS_DRM_H__
+
+/* SiS specific ioctls */
+#define NOT_USED_0_3
+#define DRM_SIS_FB_ALLOC       0x04
+#define DRM_SIS_FB_FREE                0x05
+#define NOT_USED_6_12
+#define DRM_SIS_AGP_INIT       0x13
+#define DRM_SIS_AGP_ALLOC      0x14
+#define DRM_SIS_AGP_FREE       0x15
+#define DRM_SIS_FB_INIT                0x16
+
+#define DRM_IOCTL_SIS_FB_ALLOC         DRM_IOWR(DRM_COMMAND_BASE + DRM_SIS_FB_ALLOC, drm_sis_mem_t)
+#define DRM_IOCTL_SIS_FB_FREE          DRM_IOW( DRM_COMMAND_BASE + DRM_SIS_FB_FREE, drm_sis_mem_t)
+#define DRM_IOCTL_SIS_AGP_INIT         DRM_IOWR(DRM_COMMAND_BASE + DRM_SIS_AGP_INIT, drm_sis_agp_t)
+#define DRM_IOCTL_SIS_AGP_ALLOC                DRM_IOWR(DRM_COMMAND_BASE + DRM_SIS_AGP_ALLOC, drm_sis_mem_t)
+#define DRM_IOCTL_SIS_AGP_FREE         DRM_IOW( DRM_COMMAND_BASE + DRM_SIS_AGP_FREE, drm_sis_mem_t)
+#define DRM_IOCTL_SIS_FB_INIT          DRM_IOW( DRM_COMMAND_BASE + DRM_SIS_FB_INIT, drm_sis_fb_t)
+/*
+#define DRM_IOCTL_SIS_FLIP             DRM_IOW( 0x48, drm_sis_flip_t)
+#define DRM_IOCTL_SIS_FLIP_INIT                DRM_IO(  0x49)
+#define DRM_IOCTL_SIS_FLIP_FINAL       DRM_IO(  0x50)
+*/
+
+typedef struct {
+       int context;
+       unsigned int offset;
+       unsigned int size;
+       unsigned long free;
+} drm_sis_mem_t;
+
+typedef struct {
+       unsigned int offset, size;
+} drm_sis_agp_t;
+
+typedef struct {
+       unsigned int offset, size;
+} drm_sis_fb_t;
+
+#endif                         /* __SIS_DRM_H__ */
diff --git a/include/drm/via_drm.h b/include/drm/via_drm.h
new file mode 100644 (file)
index 0000000..182f879
--- /dev/null
@@ -0,0 +1,275 @@
+/*
+ * Copyright 1998-2003 VIA Technologies, Inc. All Rights Reserved.
+ * Copyright 2001-2003 S3 Graphics, Inc. All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sub license,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
+ * VIA, S3 GRAPHICS, AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+#ifndef _VIA_DRM_H_
+#define _VIA_DRM_H_
+
+#include "drm.h"
+
+/* WARNING: These defines must be the same as what the Xserver uses.
+ * if you change them, you must change the defines in the Xserver.
+ */
+
+#ifndef _VIA_DEFINES_
+#define _VIA_DEFINES_
+
+#include "via_drmclient.h"
+
+#define VIA_NR_SAREA_CLIPRECTS         8
+#define VIA_NR_XVMC_PORTS               10
+#define VIA_NR_XVMC_LOCKS               5
+#define VIA_MAX_CACHELINE_SIZE          64
+#define XVMCLOCKPTR(saPriv,lockNo)                                     \
+       ((__volatile__ struct drm_hw_lock *)(((((unsigned long) (saPriv)->XvMCLockArea) + \
+                                     (VIA_MAX_CACHELINE_SIZE - 1)) &   \
+                                    ~(VIA_MAX_CACHELINE_SIZE - 1)) +   \
+                                   VIA_MAX_CACHELINE_SIZE*(lockNo)))
+
+/* Each region is a minimum of 64k, and there are at most 64 of them.
+ */
+#define VIA_NR_TEX_REGIONS 64
+#define VIA_LOG_MIN_TEX_REGION_SIZE 16
+#endif
+
+#define VIA_UPLOAD_TEX0IMAGE  0x1      /* handled clientside */
+#define VIA_UPLOAD_TEX1IMAGE  0x2      /* handled clientside */
+#define VIA_UPLOAD_CTX        0x4
+#define VIA_UPLOAD_BUFFERS    0x8
+#define VIA_UPLOAD_TEX0       0x10
+#define VIA_UPLOAD_TEX1       0x20
+#define VIA_UPLOAD_CLIPRECTS  0x40
+#define VIA_UPLOAD_ALL        0xff
+
+/* VIA specific ioctls */
+#define DRM_VIA_ALLOCMEM       0x00
+#define DRM_VIA_FREEMEM                0x01
+#define DRM_VIA_AGP_INIT       0x02
+#define DRM_VIA_FB_INIT                0x03
+#define DRM_VIA_MAP_INIT       0x04
+#define DRM_VIA_DEC_FUTEX       0x05
+#define NOT_USED
+#define DRM_VIA_DMA_INIT       0x07
+#define DRM_VIA_CMDBUFFER      0x08
+#define DRM_VIA_FLUSH          0x09
+#define DRM_VIA_PCICMD         0x0a
+#define DRM_VIA_CMDBUF_SIZE    0x0b
+#define NOT_USED
+#define DRM_VIA_WAIT_IRQ        0x0d
+#define DRM_VIA_DMA_BLIT        0x0e
+#define DRM_VIA_BLIT_SYNC       0x0f
+
+#define DRM_IOCTL_VIA_ALLOCMEM   DRM_IOWR(DRM_COMMAND_BASE + DRM_VIA_ALLOCMEM, drm_via_mem_t)
+#define DRM_IOCTL_VIA_FREEMEM    DRM_IOW( DRM_COMMAND_BASE + DRM_VIA_FREEMEM, drm_via_mem_t)
+#define DRM_IOCTL_VIA_AGP_INIT   DRM_IOWR(DRM_COMMAND_BASE + DRM_VIA_AGP_INIT, drm_via_agp_t)
+#define DRM_IOCTL_VIA_FB_INIT    DRM_IOWR(DRM_COMMAND_BASE + DRM_VIA_FB_INIT, drm_via_fb_t)
+#define DRM_IOCTL_VIA_MAP_INIT   DRM_IOWR(DRM_COMMAND_BASE + DRM_VIA_MAP_INIT, drm_via_init_t)
+#define DRM_IOCTL_VIA_DEC_FUTEX   DRM_IOW( DRM_COMMAND_BASE + DRM_VIA_DEC_FUTEX, drm_via_futex_t)
+#define DRM_IOCTL_VIA_DMA_INIT   DRM_IOWR(DRM_COMMAND_BASE + DRM_VIA_DMA_INIT, drm_via_dma_init_t)
+#define DRM_IOCTL_VIA_CMDBUFFER          DRM_IOW( DRM_COMMAND_BASE + DRM_VIA_CMDBUFFER, drm_via_cmdbuffer_t)
+#define DRM_IOCTL_VIA_FLUSH      DRM_IO(  DRM_COMMAND_BASE + DRM_VIA_FLUSH)
+#define DRM_IOCTL_VIA_PCICMD     DRM_IOW( DRM_COMMAND_BASE + DRM_VIA_PCICMD, drm_via_cmdbuffer_t)
+#define DRM_IOCTL_VIA_CMDBUF_SIZE DRM_IOWR( DRM_COMMAND_BASE + DRM_VIA_CMDBUF_SIZE, \
+                                           drm_via_cmdbuf_size_t)
+#define DRM_IOCTL_VIA_WAIT_IRQ    DRM_IOWR( DRM_COMMAND_BASE + DRM_VIA_WAIT_IRQ, drm_via_irqwait_t)
+#define DRM_IOCTL_VIA_DMA_BLIT    DRM_IOW(DRM_COMMAND_BASE + DRM_VIA_DMA_BLIT, drm_via_dmablit_t)
+#define DRM_IOCTL_VIA_BLIT_SYNC   DRM_IOW(DRM_COMMAND_BASE + DRM_VIA_BLIT_SYNC, drm_via_blitsync_t)
+
+/* Indices into buf.Setup where various bits of state are mirrored per
+ * context and per buffer.  These can be fired at the card as a unit,
+ * or in a piecewise fashion as required.
+ */
+
+#define VIA_TEX_SETUP_SIZE 8
+
+/* Flags for clear ioctl
+ */
+#define VIA_FRONT   0x1
+#define VIA_BACK    0x2
+#define VIA_DEPTH   0x4
+#define VIA_STENCIL 0x8
+#define VIA_MEM_VIDEO   0      /* matches drm constant */
+#define VIA_MEM_AGP     1      /* matches drm constant */
+#define VIA_MEM_SYSTEM  2
+#define VIA_MEM_MIXED   3
+#define VIA_MEM_UNKNOWN 4
+
+typedef struct {
+       __u32 offset;
+       __u32 size;
+} drm_via_agp_t;
+
+typedef struct {
+       __u32 offset;
+       __u32 size;
+} drm_via_fb_t;
+
+typedef struct {
+       __u32 context;
+       __u32 type;
+       __u32 size;
+       unsigned long index;
+       unsigned long offset;
+} drm_via_mem_t;
+
+typedef struct _drm_via_init {
+       enum {
+               VIA_INIT_MAP = 0x01,
+               VIA_CLEANUP_MAP = 0x02
+       } func;
+
+       unsigned long sarea_priv_offset;
+       unsigned long fb_offset;
+       unsigned long mmio_offset;
+       unsigned long agpAddr;
+} drm_via_init_t;
+
+typedef struct _drm_via_futex {
+       enum {
+               VIA_FUTEX_WAIT = 0x00,
+               VIA_FUTEX_WAKE = 0X01
+       } func;
+       __u32 ms;
+       __u32 lock;
+       __u32 val;
+} drm_via_futex_t;
+
+typedef struct _drm_via_dma_init {
+       enum {
+               VIA_INIT_DMA = 0x01,
+               VIA_CLEANUP_DMA = 0x02,
+               VIA_DMA_INITIALIZED = 0x03
+       } func;
+
+       unsigned long offset;
+       unsigned long size;
+       unsigned long reg_pause_addr;
+} drm_via_dma_init_t;
+
+typedef struct _drm_via_cmdbuffer {
+       char *buf;
+       unsigned long size;
+} drm_via_cmdbuffer_t;
+
+/* Warning: If you change the SAREA structure you must change the Xserver
+ * structure as well */
+
+typedef struct _drm_via_tex_region {
+       unsigned char next, prev;       /* indices to form a circular LRU  */
+       unsigned char inUse;    /* owned by a client, or free? */
+       int age;                /* tracked by clients to update local LRU's */
+} drm_via_tex_region_t;
+
+typedef struct _drm_via_sarea {
+       unsigned int dirty;
+       unsigned int nbox;
+       struct drm_clip_rect boxes[VIA_NR_SAREA_CLIPRECTS];
+       drm_via_tex_region_t texList[VIA_NR_TEX_REGIONS + 1];
+       int texAge;             /* last time texture was uploaded */
+       int ctxOwner;           /* last context to upload state */
+       int vertexPrim;
+
+       /*
+        * Below is for XvMC.
+        * We want the lock integers alone on, and aligned to, a cache line.
+        * Therefore this somewhat strange construct.
+        */
+
+       char XvMCLockArea[VIA_MAX_CACHELINE_SIZE * (VIA_NR_XVMC_LOCKS + 1)];
+
+       unsigned int XvMCDisplaying[VIA_NR_XVMC_PORTS];
+       unsigned int XvMCSubPicOn[VIA_NR_XVMC_PORTS];
+       unsigned int XvMCCtxNoGrabbed;  /* Last context to hold decoder */
+
+       /* Used by the 3d driver only at this point, for pageflipping:
+        */
+       unsigned int pfCurrentOffset;
+} drm_via_sarea_t;
+
+typedef struct _drm_via_cmdbuf_size {
+       enum {
+               VIA_CMDBUF_SPACE = 0x01,
+               VIA_CMDBUF_LAG = 0x02
+       } func;
+       int wait;
+       __u32 size;
+} drm_via_cmdbuf_size_t;
+
+typedef enum {
+       VIA_IRQ_ABSOLUTE = 0x0,
+       VIA_IRQ_RELATIVE = 0x1,
+       VIA_IRQ_SIGNAL = 0x10000000,
+       VIA_IRQ_FORCE_SEQUENCE = 0x20000000
+} via_irq_seq_type_t;
+
+#define VIA_IRQ_FLAGS_MASK 0xF0000000
+
+enum drm_via_irqs {
+       drm_via_irq_hqv0 = 0,
+       drm_via_irq_hqv1,
+       drm_via_irq_dma0_dd,
+       drm_via_irq_dma0_td,
+       drm_via_irq_dma1_dd,
+       drm_via_irq_dma1_td,
+       drm_via_irq_num
+};
+
+struct drm_via_wait_irq_request {
+       unsigned irq;
+       via_irq_seq_type_t type;
+       __u32 sequence;
+       __u32 signal;
+};
+
+typedef union drm_via_irqwait {
+       struct drm_via_wait_irq_request request;
+       struct drm_wait_vblank_reply reply;
+} drm_via_irqwait_t;
+
+typedef struct drm_via_blitsync {
+       __u32 sync_handle;
+       unsigned engine;
+} drm_via_blitsync_t;
+
+/* - * Below,"flags" is currently unused but will be used for possible future
+ * extensions like kernel space bounce buffers for bad alignments and
+ * blit engine busy-wait polling for better latency in the absence of
+ * interrupts.
+ */
+
+typedef struct drm_via_dmablit {
+       __u32 num_lines;
+       __u32 line_length;
+
+       __u32 fb_addr;
+       __u32 fb_stride;
+
+       unsigned char *mem_addr;
+       __u32 mem_stride;
+
+       __u32 flags;
+       int to_fb;
+
+       drm_via_blitsync_t sync;
+} drm_via_dmablit_t;
+
+#endif                         /* _VIA_DRM_H_ */
diff --git a/include/drm/vmwgfx_drm.h b/include/drm/vmwgfx_drm.h
new file mode 100644 (file)
index 0000000..4d08423
--- /dev/null
@@ -0,0 +1,614 @@
+/**************************************************************************
+ *
+ * Copyright Â© 2009 VMware, Inc., Palo Alto, CA., USA
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+ * USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ **************************************************************************/
+
+#ifndef __VMWGFX_DRM_H__
+#define __VMWGFX_DRM_H__
+
+#define DRM_VMW_MAX_SURFACE_FACES 6
+#define DRM_VMW_MAX_MIP_LEVELS 24
+
+#define DRM_VMW_EXT_NAME_LEN 128
+
+#define DRM_VMW_GET_PARAM            0
+#define DRM_VMW_ALLOC_DMABUF         1
+#define DRM_VMW_UNREF_DMABUF         2
+#define DRM_VMW_CURSOR_BYPASS        3
+/* guarded by DRM_VMW_PARAM_NUM_STREAMS != 0*/
+#define DRM_VMW_CONTROL_STREAM       4
+#define DRM_VMW_CLAIM_STREAM         5
+#define DRM_VMW_UNREF_STREAM         6
+/* guarded by DRM_VMW_PARAM_3D == 1 */
+#define DRM_VMW_CREATE_CONTEXT       7
+#define DRM_VMW_UNREF_CONTEXT        8
+#define DRM_VMW_CREATE_SURFACE       9
+#define DRM_VMW_UNREF_SURFACE        10
+#define DRM_VMW_REF_SURFACE          11
+#define DRM_VMW_EXECBUF              12
+#define DRM_VMW_FIFO_DEBUG           13
+#define DRM_VMW_FENCE_WAIT           14
+/* guarded by minor version >= 2 */
+#define DRM_VMW_UPDATE_LAYOUT        15
+
+
+/*************************************************************************/
+/**
+ * DRM_VMW_GET_PARAM - get device information.
+ *
+ * DRM_VMW_PARAM_FIFO_OFFSET:
+ * Offset to use to map the first page of the FIFO read-only.
+ * The fifo is mapped using the mmap() system call on the drm device.
+ *
+ * DRM_VMW_PARAM_OVERLAY_IOCTL:
+ * Does the driver support the overlay ioctl.
+ */
+
+#define DRM_VMW_PARAM_NUM_STREAMS      0
+#define DRM_VMW_PARAM_NUM_FREE_STREAMS 1
+#define DRM_VMW_PARAM_3D               2
+#define DRM_VMW_PARAM_FIFO_OFFSET      3
+#define DRM_VMW_PARAM_HW_CAPS          4
+#define DRM_VMW_PARAM_FIFO_CAPS        5
+
+/**
+ * struct drm_vmw_getparam_arg
+ *
+ * @value: Returned value. //Out
+ * @param: Parameter to query. //In.
+ *
+ * Argument to the DRM_VMW_GET_PARAM Ioctl.
+ */
+
+struct drm_vmw_getparam_arg {
+       uint64_t value;
+       uint32_t param;
+       uint32_t pad64;
+};
+
+/*************************************************************************/
+/**
+ * DRM_VMW_EXTENSION - Query device extensions.
+ */
+
+/**
+ * struct drm_vmw_extension_rep
+ *
+ * @exists: The queried extension exists.
+ * @driver_ioctl_offset: Ioctl number of the first ioctl in the extension.
+ * @driver_sarea_offset: Offset to any space in the DRI SAREA
+ * used by the extension.
+ * @major: Major version number of the extension.
+ * @minor: Minor version number of the extension.
+ * @pl: Patch level version number of the extension.
+ *
+ * Output argument to the DRM_VMW_EXTENSION Ioctl.
+ */
+
+struct drm_vmw_extension_rep {
+       int32_t exists;
+       uint32_t driver_ioctl_offset;
+       uint32_t driver_sarea_offset;
+       uint32_t major;
+       uint32_t minor;
+       uint32_t pl;
+       uint32_t pad64;
+};
+
+/**
+ * union drm_vmw_extension_arg
+ *
+ * @extension - Ascii name of the extension to be queried. //In
+ * @rep - Reply as defined above. //Out
+ *
+ * Argument to the DRM_VMW_EXTENSION Ioctl.
+ */
+
+union drm_vmw_extension_arg {
+       char extension[DRM_VMW_EXT_NAME_LEN];
+       struct drm_vmw_extension_rep rep;
+};
+
+/*************************************************************************/
+/**
+ * DRM_VMW_CREATE_CONTEXT - Create a host context.
+ *
+ * Allocates a device unique context id, and queues a create context command
+ * for the host. Does not wait for host completion.
+ */
+
+/**
+ * struct drm_vmw_context_arg
+ *
+ * @cid: Device unique context ID.
+ *
+ * Output argument to the DRM_VMW_CREATE_CONTEXT Ioctl.
+ * Input argument to the DRM_VMW_UNREF_CONTEXT Ioctl.
+ */
+
+struct drm_vmw_context_arg {
+       int32_t cid;
+       uint32_t pad64;
+};
+
+/*************************************************************************/
+/**
+ * DRM_VMW_UNREF_CONTEXT - Create a host context.
+ *
+ * Frees a global context id, and queues a destroy host command for the host.
+ * Does not wait for host completion. The context ID can be used directly
+ * in the command stream and shows up as the same context ID on the host.
+ */
+
+/*************************************************************************/
+/**
+ * DRM_VMW_CREATE_SURFACE - Create a host suface.
+ *
+ * Allocates a device unique surface id, and queues a create surface command
+ * for the host. Does not wait for host completion. The surface ID can be
+ * used directly in the command stream and shows up as the same surface
+ * ID on the host.
+ */
+
+/**
+ * struct drm_wmv_surface_create_req
+ *
+ * @flags: Surface flags as understood by the host.
+ * @format: Surface format as understood by the host.
+ * @mip_levels: Number of mip levels for each face.
+ * An unused face should have 0 encoded.
+ * @size_addr: Address of a user-space array of sruct drm_vmw_size
+ * cast to an uint64_t for 32-64 bit compatibility.
+ * The size of the array should equal the total number of mipmap levels.
+ * @shareable: Boolean whether other clients (as identified by file descriptors)
+ * may reference this surface.
+ * @scanout: Boolean whether the surface is intended to be used as a
+ * scanout.
+ *
+ * Input data to the DRM_VMW_CREATE_SURFACE Ioctl.
+ * Output data from the DRM_VMW_REF_SURFACE Ioctl.
+ */
+
+struct drm_vmw_surface_create_req {
+       uint32_t flags;
+       uint32_t format;
+       uint32_t mip_levels[DRM_VMW_MAX_SURFACE_FACES];
+       uint64_t size_addr;
+       int32_t shareable;
+       int32_t scanout;
+};
+
+/**
+ * struct drm_wmv_surface_arg
+ *
+ * @sid: Surface id of created surface or surface to destroy or reference.
+ *
+ * Output data from the DRM_VMW_CREATE_SURFACE Ioctl.
+ * Input argument to the DRM_VMW_UNREF_SURFACE Ioctl.
+ * Input argument to the DRM_VMW_REF_SURFACE Ioctl.
+ */
+
+struct drm_vmw_surface_arg {
+       int32_t sid;
+       uint32_t pad64;
+};
+
+/**
+ * struct drm_vmw_size ioctl.
+ *
+ * @width - mip level width
+ * @height - mip level height
+ * @depth - mip level depth
+ *
+ * Description of a mip level.
+ * Input data to the DRM_WMW_CREATE_SURFACE Ioctl.
+ */
+
+struct drm_vmw_size {
+       uint32_t width;
+       uint32_t height;
+       uint32_t depth;
+       uint32_t pad64;
+};
+
+/**
+ * union drm_vmw_surface_create_arg
+ *
+ * @rep: Output data as described above.
+ * @req: Input data as described above.
+ *
+ * Argument to the DRM_VMW_CREATE_SURFACE Ioctl.
+ */
+
+union drm_vmw_surface_create_arg {
+       struct drm_vmw_surface_arg rep;
+       struct drm_vmw_surface_create_req req;
+};
+
+/*************************************************************************/
+/**
+ * DRM_VMW_REF_SURFACE - Reference a host surface.
+ *
+ * Puts a reference on a host surface with a give sid, as previously
+ * returned by the DRM_VMW_CREATE_SURFACE ioctl.
+ * A reference will make sure the surface isn't destroyed while we hold
+ * it and will allow the calling client to use the surface ID in the command
+ * stream.
+ *
+ * On successful return, the Ioctl returns the surface information given
+ * in the DRM_VMW_CREATE_SURFACE ioctl.
+ */
+
+/**
+ * union drm_vmw_surface_reference_arg
+ *
+ * @rep: Output data as described above.
+ * @req: Input data as described above.
+ *
+ * Argument to the DRM_VMW_REF_SURFACE Ioctl.
+ */
+
+union drm_vmw_surface_reference_arg {
+       struct drm_vmw_surface_create_req rep;
+       struct drm_vmw_surface_arg req;
+};
+
+/*************************************************************************/
+/**
+ * DRM_VMW_UNREF_SURFACE - Unreference a host surface.
+ *
+ * Clear a reference previously put on a host surface.
+ * When all references are gone, including the one implicitly placed
+ * on creation,
+ * a destroy surface command will be queued for the host.
+ * Does not wait for completion.
+ */
+
+/*************************************************************************/
+/**
+ * DRM_VMW_EXECBUF
+ *
+ * Submit a command buffer for execution on the host, and return a
+ * fence sequence that when signaled, indicates that the command buffer has
+ * executed.
+ */
+
+/**
+ * struct drm_vmw_execbuf_arg
+ *
+ * @commands: User-space address of a command buffer cast to an uint64_t.
+ * @command-size: Size in bytes of the command buffer.
+ * @throttle-us: Sleep until software is less than @throttle_us
+ * microseconds ahead of hardware. The driver may round this value
+ * to the nearest kernel tick.
+ * @fence_rep: User-space address of a struct drm_vmw_fence_rep cast to an
+ * uint64_t.
+ * @version: Allows expanding the execbuf ioctl parameters without breaking
+ * backwards compatibility, since user-space will always tell the kernel
+ * which version it uses.
+ * @flags: Execbuf flags. None currently.
+ *
+ * Argument to the DRM_VMW_EXECBUF Ioctl.
+ */
+
+#define DRM_VMW_EXECBUF_VERSION 0
+
+struct drm_vmw_execbuf_arg {
+       uint64_t commands;
+       uint32_t command_size;
+       uint32_t throttle_us;
+       uint64_t fence_rep;
+        uint32_t version;
+        uint32_t flags;
+};
+
+/**
+ * struct drm_vmw_fence_rep
+ *
+ * @fence_seq: Fence sequence associated with a command submission.
+ * @error: This member should've been set to -EFAULT on submission.
+ * The following actions should be take on completion:
+ * error == -EFAULT: Fence communication failed. The host is synchronized.
+ * Use the last fence id read from the FIFO fence register.
+ * error != 0 && error != -EFAULT:
+ * Fence submission failed. The host is synchronized. Use the fence_seq member.
+ * error == 0: All is OK, The host may not be synchronized.
+ * Use the fence_seq member.
+ *
+ * Input / Output data to the DRM_VMW_EXECBUF Ioctl.
+ */
+
+struct drm_vmw_fence_rep {
+       uint64_t fence_seq;
+       int32_t error;
+       uint32_t pad64;
+};
+
+/*************************************************************************/
+/**
+ * DRM_VMW_ALLOC_DMABUF
+ *
+ * Allocate a DMA buffer that is visible also to the host.
+ * NOTE: The buffer is
+ * identified by a handle and an offset, which are private to the guest, but
+ * useable in the command stream. The guest kernel may translate these
+ * and patch up the command stream accordingly. In the future, the offset may
+ * be zero at all times, or it may disappear from the interface before it is
+ * fixed.
+ *
+ * The DMA buffer may stay user-space mapped in the guest at all times,
+ * and is thus suitable for sub-allocation.
+ *
+ * DMA buffers are mapped using the mmap() syscall on the drm device.
+ */
+
+/**
+ * struct drm_vmw_alloc_dmabuf_req
+ *
+ * @size: Required minimum size of the buffer.
+ *
+ * Input data to the DRM_VMW_ALLOC_DMABUF Ioctl.
+ */
+
+struct drm_vmw_alloc_dmabuf_req {
+       uint32_t size;
+       uint32_t pad64;
+};
+
+/**
+ * struct drm_vmw_dmabuf_rep
+ *
+ * @map_handle: Offset to use in the mmap() call used to map the buffer.
+ * @handle: Handle unique to this buffer. Used for unreferencing.
+ * @cur_gmr_id: GMR id to use in the command stream when this buffer is
+ * referenced. See not above.
+ * @cur_gmr_offset: Offset to use in the command stream when this buffer is
+ * referenced. See note above.
+ *
+ * Output data from the DRM_VMW_ALLOC_DMABUF Ioctl.
+ */
+
+struct drm_vmw_dmabuf_rep {
+       uint64_t map_handle;
+       uint32_t handle;
+       uint32_t cur_gmr_id;
+       uint32_t cur_gmr_offset;
+       uint32_t pad64;
+};
+
+/**
+ * union drm_vmw_dmabuf_arg
+ *
+ * @req: Input data as described above.
+ * @rep: Output data as described above.
+ *
+ * Argument to the DRM_VMW_ALLOC_DMABUF Ioctl.
+ */
+
+union drm_vmw_alloc_dmabuf_arg {
+       struct drm_vmw_alloc_dmabuf_req req;
+       struct drm_vmw_dmabuf_rep rep;
+};
+
+/*************************************************************************/
+/**
+ * DRM_VMW_UNREF_DMABUF - Free a DMA buffer.
+ *
+ */
+
+/**
+ * struct drm_vmw_unref_dmabuf_arg
+ *
+ * @handle: Handle indicating what buffer to free. Obtained from the
+ * DRM_VMW_ALLOC_DMABUF Ioctl.
+ *
+ * Argument to the DRM_VMW_UNREF_DMABUF Ioctl.
+ */
+
+struct drm_vmw_unref_dmabuf_arg {
+       uint32_t handle;
+       uint32_t pad64;
+};
+
+/*************************************************************************/
+/**
+ * DRM_VMW_FIFO_DEBUG - Get last FIFO submission.
+ *
+ * This IOCTL copies the last FIFO submission directly out of the FIFO buffer.
+ */
+
+/**
+ * struct drm_vmw_fifo_debug_arg
+ *
+ * @debug_buffer: User space address of a debug_buffer cast to an uint64_t //In
+ * @debug_buffer_size: Size in bytes of debug buffer //In
+ * @used_size: Number of bytes copied to the buffer // Out
+ * @did_not_fit: Boolean indicating that the fifo contents did not fit. //Out
+ *
+ * Argument to the DRM_VMW_FIFO_DEBUG Ioctl.
+ */
+
+struct drm_vmw_fifo_debug_arg {
+       uint64_t debug_buffer;
+       uint32_t debug_buffer_size;
+       uint32_t used_size;
+       int32_t did_not_fit;
+       uint32_t pad64;
+};
+
+struct drm_vmw_fence_wait_arg {
+       uint64_t sequence;
+       uint64_t kernel_cookie;
+       int32_t cookie_valid;
+       int32_t pad64;
+};
+
+/*************************************************************************/
+/**
+ * DRM_VMW_CONTROL_STREAM - Control overlays, aka streams.
+ *
+ * This IOCTL controls the overlay units of the svga device.
+ * The SVGA overlay units does not work like regular hardware units in
+ * that they do not automaticaly read back the contents of the given dma
+ * buffer. But instead only read back for each call to this ioctl, and
+ * at any point between this call being made and a following call that
+ * either changes the buffer or disables the stream.
+ */
+
+/**
+ * struct drm_vmw_rect
+ *
+ * Defines a rectangle. Used in the overlay ioctl to define
+ * source and destination rectangle.
+ */
+
+struct drm_vmw_rect {
+       int32_t x;
+       int32_t y;
+       uint32_t w;
+       uint32_t h;
+};
+
+/**
+ * struct drm_vmw_control_stream_arg
+ *
+ * @stream_id: Stearm to control
+ * @enabled: If false all following arguments are ignored.
+ * @handle: Handle to buffer for getting data from.
+ * @format: Format of the overlay as understood by the host.
+ * @width: Width of the overlay.
+ * @height: Height of the overlay.
+ * @size: Size of the overlay in bytes.
+ * @pitch: Array of pitches, the two last are only used for YUV12 formats.
+ * @offset: Offset from start of dma buffer to overlay.
+ * @src: Source rect, must be within the defined area above.
+ * @dst: Destination rect, x and y may be negative.
+ *
+ * Argument to the DRM_VMW_CONTROL_STREAM Ioctl.
+ */
+
+struct drm_vmw_control_stream_arg {
+       uint32_t stream_id;
+       uint32_t enabled;
+
+       uint32_t flags;
+       uint32_t color_key;
+
+       uint32_t handle;
+       uint32_t offset;
+       int32_t format;
+       uint32_t size;
+       uint32_t width;
+       uint32_t height;
+       uint32_t pitch[3];
+
+       uint32_t pad64;
+       struct drm_vmw_rect src;
+       struct drm_vmw_rect dst;
+};
+
+/*************************************************************************/
+/**
+ * DRM_VMW_CURSOR_BYPASS - Give extra information about cursor bypass.
+ *
+ */
+
+#define DRM_VMW_CURSOR_BYPASS_ALL    (1 << 0)
+#define DRM_VMW_CURSOR_BYPASS_FLAGS       (1)
+
+/**
+ * struct drm_vmw_cursor_bypass_arg
+ *
+ * @flags: Flags.
+ * @crtc_id: Crtc id, only used if DMR_CURSOR_BYPASS_ALL isn't passed.
+ * @xpos: X position of cursor.
+ * @ypos: Y position of cursor.
+ * @xhot: X hotspot.
+ * @yhot: Y hotspot.
+ *
+ * Argument to the DRM_VMW_CURSOR_BYPASS Ioctl.
+ */
+
+struct drm_vmw_cursor_bypass_arg {
+       uint32_t flags;
+       uint32_t crtc_id;
+       int32_t xpos;
+       int32_t ypos;
+       int32_t xhot;
+       int32_t yhot;
+};
+
+/*************************************************************************/
+/**
+ * DRM_VMW_CLAIM_STREAM - Claim a single stream.
+ */
+
+/**
+ * struct drm_vmw_context_arg
+ *
+ * @stream_id: Device unique context ID.
+ *
+ * Output argument to the DRM_VMW_CREATE_CONTEXT Ioctl.
+ * Input argument to the DRM_VMW_UNREF_CONTEXT Ioctl.
+ */
+
+struct drm_vmw_stream_arg {
+       uint32_t stream_id;
+       uint32_t pad64;
+};
+
+/*************************************************************************/
+/**
+ * DRM_VMW_UNREF_STREAM - Unclaim a stream.
+ *
+ * Return a single stream that was claimed by this process. Also makes
+ * sure that the stream has been stopped.
+ */
+
+/*************************************************************************/
+/**
+ * DRM_VMW_UPDATE_LAYOUT - Update layout
+ *
+ * Updates the prefered modes and connection status for connectors. The
+ * command conisits of one drm_vmw_update_layout_arg pointing out a array
+ * of num_outputs drm_vmw_rect's.
+ */
+
+/**
+ * struct drm_vmw_update_layout_arg
+ *
+ * @num_outputs: number of active
+ * @rects: pointer to array of drm_vmw_rect
+ *
+ * Input argument to the DRM_VMW_UPDATE_LAYOUT Ioctl.
+ */
+
+struct drm_vmw_update_layout_arg {
+       uint32_t num_outputs;
+       uint32_t pad64;
+       uint64_t rects;
+};
+
+#endif
diff --git a/intel/Makefile.am b/intel/Makefile.am
new file mode 100644 (file)
index 0000000..7a44aaf
--- /dev/null
@@ -0,0 +1,51 @@
+# Copyright Â© 2008 Intel Corporation
+#
+# Permission is hereby granted, free of charge, to any person obtaining a
+# copy of this software and associated documentation files (the "Software"),
+# to deal in the Software without restriction, including without limitation
+# the rights to use, copy, modify, merge, publish, distribute, sublicense,
+# and/or sell copies of the Software, and to permit persons to whom the
+# Software is furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice (including the next
+# paragraph) shall be included in all copies or substantial portions of the
+# Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+# IN THE SOFTWARE.
+#
+# Authors:
+#    Eric Anholt <eric@anholt.net>
+
+AM_CFLAGS = \
+       $(WARN_CFLAGS) \
+       -I$(top_srcdir) \
+       -I$(top_srcdir)/intel \
+       $(PTHREADSTUBS_CFLAGS) \
+       $(PCIACCESS_CFLAGS) \
+       -I$(top_srcdir)/include/drm
+
+libdrm_intel_la_LTLIBRARIES = libdrm_intel.la
+libdrm_intel_ladir = $(libdir)
+libdrm_intel_la_LDFLAGS = -version-number 1:0:0 -no-undefined
+libdrm_intel_la_LIBADD = ../libdrm.la @PTHREADSTUBS_LIBS@ @PCIACCESS_LIBS@ @CLOCK_LIB@
+
+libdrm_intel_la_SOURCES = \
+       intel_bufmgr.c \
+       intel_bufmgr_priv.h \
+       intel_bufmgr_fake.c \
+       intel_bufmgr_gem.c \
+       intel_chipset.h \
+       mm.c \
+       mm.h
+
+libdrm_intelincludedir = ${includedir}/libdrm
+libdrm_intelinclude_HEADERS = intel_bufmgr.h \
+                             intel_debug.h
+
+pkgconfig_DATA = libdrm_intel.pc
diff --git a/intel/intel_bufmgr.c b/intel/intel_bufmgr.c
new file mode 100644 (file)
index 0000000..905556f
--- /dev/null
@@ -0,0 +1,321 @@
+/*
+ * Copyright Â© 2007 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Authors:
+ *    Eric Anholt <eric@anholt.net>
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <string.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <assert.h>
+#include <errno.h>
+#include <drm.h>
+#include <i915_drm.h>
+#include <pciaccess.h>
+#include "intel_bufmgr.h"
+#include "intel_bufmgr_priv.h"
+#include "xf86drm.h"
+
+/** @file intel_bufmgr.c
+ *
+ * Convenience functions for buffer management methods.
+ */
+
+drm_intel_bo *drm_intel_bo_alloc(drm_intel_bufmgr *bufmgr, const char *name,
+                                unsigned long size, unsigned int alignment)
+{
+       return bufmgr->bo_alloc(bufmgr, name, size, alignment);
+}
+
+drm_intel_bo *drm_intel_bo_alloc_for_render(drm_intel_bufmgr *bufmgr,
+                                           const char *name,
+                                           unsigned long size,
+                                           unsigned int alignment)
+{
+       return bufmgr->bo_alloc_for_render(bufmgr, name, size, alignment);
+}
+
+drm_intel_bo *
+drm_intel_bo_alloc_tiled(drm_intel_bufmgr *bufmgr, const char *name,
+                        int x, int y, int cpp, uint32_t *tiling_mode,
+                        unsigned long *pitch, unsigned long flags)
+{
+       return bufmgr->bo_alloc_tiled(bufmgr, name, x, y, cpp,
+                                     tiling_mode, pitch, flags);
+}
+
+void drm_intel_bo_reference(drm_intel_bo *bo)
+{
+       bo->bufmgr->bo_reference(bo);
+}
+
+void drm_intel_bo_unreference(drm_intel_bo *bo)
+{
+       if (bo == NULL)
+               return;
+
+       bo->bufmgr->bo_unreference(bo);
+}
+
+int drm_intel_bo_map(drm_intel_bo *buf, int write_enable)
+{
+       return buf->bufmgr->bo_map(buf, write_enable);
+}
+
+int drm_intel_bo_unmap(drm_intel_bo *buf)
+{
+       return buf->bufmgr->bo_unmap(buf);
+}
+
+int
+drm_intel_bo_subdata(drm_intel_bo *bo, unsigned long offset,
+                    unsigned long size, const void *data)
+{
+       return bo->bufmgr->bo_subdata(bo, offset, size, data);
+}
+
+int
+drm_intel_bo_get_subdata(drm_intel_bo *bo, unsigned long offset,
+                        unsigned long size, void *data)
+{
+       int ret;
+       if (bo->bufmgr->bo_get_subdata)
+               return bo->bufmgr->bo_get_subdata(bo, offset, size, data);
+
+       if (size == 0 || data == NULL)
+               return 0;
+
+       ret = drm_intel_bo_map(bo, 0);
+       if (ret)
+               return ret;
+       memcpy(data, (unsigned char *)bo->virtual + offset, size);
+       drm_intel_bo_unmap(bo);
+       return 0;
+}
+
+void drm_intel_bo_wait_rendering(drm_intel_bo *bo)
+{
+       bo->bufmgr->bo_wait_rendering(bo);
+}
+
+void drm_intel_bufmgr_destroy(drm_intel_bufmgr *bufmgr)
+{
+       bufmgr->destroy(bufmgr);
+}
+
+int
+drm_intel_bo_exec(drm_intel_bo *bo, int used,
+                 drm_clip_rect_t * cliprects, int num_cliprects, int DR4)
+{
+       return bo->bufmgr->bo_exec(bo, used, cliprects, num_cliprects, DR4);
+}
+
+int
+drm_intel_bo_mrb_exec(drm_intel_bo *bo, int used,
+               drm_clip_rect_t *cliprects, int num_cliprects, int DR4,
+               unsigned int rings)
+{
+       if (bo->bufmgr->bo_mrb_exec)
+               return bo->bufmgr->bo_mrb_exec(bo, used,
+                                       cliprects, num_cliprects, DR4,
+                                       rings);
+
+       switch (rings) {
+       case I915_EXEC_DEFAULT:
+       case I915_EXEC_RENDER:
+               return bo->bufmgr->bo_exec(bo, used,
+                                          cliprects, num_cliprects, DR4);
+       default:
+               return -ENODEV;
+       }
+}
+
+void drm_intel_bufmgr_set_debug(drm_intel_bufmgr *bufmgr, int enable_debug)
+{
+       bufmgr->debug = enable_debug;
+}
+
+int drm_intel_bufmgr_check_aperture_space(drm_intel_bo ** bo_array, int count)
+{
+       return bo_array[0]->bufmgr->check_aperture_space(bo_array, count);
+}
+
+int drm_intel_bo_flink(drm_intel_bo *bo, uint32_t * name)
+{
+       if (bo->bufmgr->bo_flink)
+               return bo->bufmgr->bo_flink(bo, name);
+
+       return -ENODEV;
+}
+
+int
+drm_intel_bo_emit_reloc(drm_intel_bo *bo, uint32_t offset,
+                       drm_intel_bo *target_bo, uint32_t target_offset,
+                       uint32_t read_domains, uint32_t write_domain)
+{
+       return bo->bufmgr->bo_emit_reloc(bo, offset,
+                                        target_bo, target_offset,
+                                        read_domains, write_domain);
+}
+
+/* For fence registers, not GL fences */
+int
+drm_intel_bo_emit_reloc_fence(drm_intel_bo *bo, uint32_t offset,
+                             drm_intel_bo *target_bo, uint32_t target_offset,
+                             uint32_t read_domains, uint32_t write_domain)
+{
+       return bo->bufmgr->bo_emit_reloc_fence(bo, offset,
+                                              target_bo, target_offset,
+                                              read_domains, write_domain);
+}
+
+
+int drm_intel_bo_pin(drm_intel_bo *bo, uint32_t alignment)
+{
+       if (bo->bufmgr->bo_pin)
+               return bo->bufmgr->bo_pin(bo, alignment);
+
+       return -ENODEV;
+}
+
+int drm_intel_bo_unpin(drm_intel_bo *bo)
+{
+       if (bo->bufmgr->bo_unpin)
+               return bo->bufmgr->bo_unpin(bo);
+
+       return -ENODEV;
+}
+
+int drm_intel_bo_set_tiling(drm_intel_bo *bo, uint32_t * tiling_mode,
+                           uint32_t stride)
+{
+       if (bo->bufmgr->bo_set_tiling)
+               return bo->bufmgr->bo_set_tiling(bo, tiling_mode, stride);
+
+       *tiling_mode = I915_TILING_NONE;
+       return 0;
+}
+
+int drm_intel_bo_get_tiling(drm_intel_bo *bo, uint32_t * tiling_mode,
+                           uint32_t * swizzle_mode)
+{
+       if (bo->bufmgr->bo_get_tiling)
+               return bo->bufmgr->bo_get_tiling(bo, tiling_mode, swizzle_mode);
+
+       *tiling_mode = I915_TILING_NONE;
+       *swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
+       return 0;
+}
+
+int drm_intel_bo_disable_reuse(drm_intel_bo *bo)
+{
+       if (bo->bufmgr->bo_disable_reuse)
+               return bo->bufmgr->bo_disable_reuse(bo);
+       return 0;
+}
+
+int drm_intel_bo_is_reusable(drm_intel_bo *bo)
+{
+       if (bo->bufmgr->bo_is_reusable)
+               return bo->bufmgr->bo_is_reusable(bo);
+       return 0;
+}
+
+int drm_intel_bo_busy(drm_intel_bo *bo)
+{
+       if (bo->bufmgr->bo_busy)
+               return bo->bufmgr->bo_busy(bo);
+       return 0;
+}
+
+int drm_intel_bo_madvise(drm_intel_bo *bo, int madv)
+{
+       if (bo->bufmgr->bo_madvise)
+               return bo->bufmgr->bo_madvise(bo, madv);
+       return -1;
+}
+
+int drm_intel_bo_references(drm_intel_bo *bo, drm_intel_bo *target_bo)
+{
+       return bo->bufmgr->bo_references(bo, target_bo);
+}
+
+int drm_intel_get_pipe_from_crtc_id(drm_intel_bufmgr *bufmgr, int crtc_id)
+{
+       if (bufmgr->get_pipe_from_crtc_id)
+               return bufmgr->get_pipe_from_crtc_id(bufmgr, crtc_id);
+       return -1;
+}
+
+static size_t
+drm_intel_probe_agp_aperture_size(int fd)
+{
+       struct pci_device *pci_dev;
+       size_t size = 0;
+       int ret;
+
+       ret = pci_system_init();
+       if (ret)
+               goto err;
+
+       /* XXX handle multiple adaptors? */
+       pci_dev = pci_device_find_by_slot(0, 0, 2, 0);
+       if (pci_dev == NULL)
+               goto err;
+
+       ret = pci_device_probe(pci_dev);
+       if (ret)
+               goto err;
+
+       size = pci_dev->regions[2].size;
+err:
+       pci_system_cleanup ();
+       return size;
+}
+
+int drm_intel_get_aperture_sizes(int fd,
+                                size_t *mappable,
+                                size_t *total)
+{
+
+       struct drm_i915_gem_get_aperture aperture;
+       int ret;
+
+       ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_GET_APERTURE, &aperture);
+       if (ret)
+               return ret;
+
+       *mappable = 0;
+       /* XXX add a query for the kernel value? */
+       if (*mappable == 0)
+               *mappable = drm_intel_probe_agp_aperture_size(fd);
+       if (*mappable == 0)
+               *mappable = 64 * 1024 * 1024; /* minimum possible value */
+       *total = aperture.aper_size;
+       return 0;
+}
diff --git a/intel/intel_bufmgr.h b/intel/intel_bufmgr.h
new file mode 100644 (file)
index 0000000..abe9711
--- /dev/null
@@ -0,0 +1,232 @@
+/*
+ * Copyright Â© 2008 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Authors:
+ *    Eric Anholt <eric@anholt.net>
+ *
+ */
+
+/**
+ * @file intel_bufmgr.h
+ *
+ * Public definitions of Intel-specific bufmgr functions.
+ */
+
+#ifndef INTEL_BUFMGR_H
+#define INTEL_BUFMGR_H
+
+#include <stdint.h>
+
+struct drm_clip_rect;
+
+typedef struct _drm_intel_bufmgr drm_intel_bufmgr;
+typedef struct _drm_intel_bo drm_intel_bo;
+
+struct _drm_intel_bo {
+       /**
+        * Size in bytes of the buffer object.
+        *
+        * The size may be larger than the size originally requested for the
+        * allocation, such as being aligned to page size.
+        */
+       unsigned long size;
+
+       /**
+        * Alignment requirement for object
+        *
+        * Used for GTT mapping & pinning the object.
+        */
+       unsigned long align;
+
+       /**
+        * Last seen card virtual address (offset from the beginning of the
+        * aperture) for the object.  This should be used to fill relocation
+        * entries when calling drm_intel_bo_emit_reloc()
+        */
+       unsigned long offset;
+
+       /**
+        * Virtual address for accessing the buffer data.  Only valid while
+        * mapped.
+        */
+#ifdef __cplusplus
+       void *virt;
+#else
+       void *virtual;
+#endif
+
+       /** Buffer manager context associated with this buffer object */
+       drm_intel_bufmgr *bufmgr;
+
+       /**
+        * MM-specific handle for accessing object
+        */
+       int handle;
+};
+
+#define BO_ALLOC_FOR_RENDER (1<<0)
+
+drm_intel_bo *drm_intel_bo_alloc(drm_intel_bufmgr *bufmgr, const char *name,
+                                unsigned long size, unsigned int alignment);
+drm_intel_bo *drm_intel_bo_alloc_for_render(drm_intel_bufmgr *bufmgr,
+                                           const char *name,
+                                           unsigned long size,
+                                           unsigned int alignment);
+drm_intel_bo *drm_intel_bo_alloc_tiled(drm_intel_bufmgr *bufmgr,
+                                      const char *name,
+                                      int x, int y, int cpp,
+                                      uint32_t *tiling_mode,
+                                      unsigned long *pitch,
+                                      unsigned long flags);
+void drm_intel_bo_reference(drm_intel_bo *bo);
+void drm_intel_bo_unreference(drm_intel_bo *bo);
+int drm_intel_bo_map(drm_intel_bo *bo, int write_enable);
+int drm_intel_bo_unmap(drm_intel_bo *bo);
+
+int drm_intel_bo_subdata(drm_intel_bo *bo, unsigned long offset,
+                        unsigned long size, const void *data);
+int drm_intel_bo_get_subdata(drm_intel_bo *bo, unsigned long offset,
+                            unsigned long size, void *data);
+void drm_intel_bo_wait_rendering(drm_intel_bo *bo);
+
+void drm_intel_bufmgr_set_debug(drm_intel_bufmgr *bufmgr, int enable_debug);
+void drm_intel_bufmgr_destroy(drm_intel_bufmgr *bufmgr);
+int drm_intel_bo_exec(drm_intel_bo *bo, int used,
+                     struct drm_clip_rect *cliprects, int num_cliprects, int DR4);
+int drm_intel_bo_mrb_exec(drm_intel_bo *bo, int used,
+                       struct drm_clip_rect *cliprects, int num_cliprects, int DR4,
+                       unsigned int flags);
+int drm_intel_bufmgr_check_aperture_space(drm_intel_bo ** bo_array, int count);
+
+int drm_intel_bo_emit_reloc(drm_intel_bo *bo, uint32_t offset,
+                           drm_intel_bo *target_bo, uint32_t target_offset,
+                           uint32_t read_domains, uint32_t write_domain);
+int drm_intel_bo_emit_reloc_fence(drm_intel_bo *bo, uint32_t offset,
+                                 drm_intel_bo *target_bo,
+                                 uint32_t target_offset,
+                                 uint32_t read_domains, uint32_t write_domain);
+int drm_intel_bo_pin(drm_intel_bo *bo, uint32_t alignment);
+int drm_intel_bo_unpin(drm_intel_bo *bo);
+int drm_intel_bo_set_tiling(drm_intel_bo *bo, uint32_t * tiling_mode,
+                           uint32_t stride);
+int drm_intel_bo_get_tiling(drm_intel_bo *bo, uint32_t * tiling_mode,
+                           uint32_t * swizzle_mode);
+int drm_intel_bo_flink(drm_intel_bo *bo, uint32_t * name);
+int drm_intel_bo_busy(drm_intel_bo *bo);
+int drm_intel_bo_madvise(drm_intel_bo *bo, int madv);
+
+int drm_intel_bo_disable_reuse(drm_intel_bo *bo);
+int drm_intel_bo_is_reusable(drm_intel_bo *bo);
+int drm_intel_bo_references(drm_intel_bo *bo, drm_intel_bo *target_bo);
+
+/* drm_intel_bufmgr_gem.c */
+drm_intel_bufmgr *drm_intel_bufmgr_gem_init(int fd, int batch_size);
+drm_intel_bo *drm_intel_bo_gem_create_from_name(drm_intel_bufmgr *bufmgr,
+                                               const char *name,
+                                               unsigned int handle);
+void drm_intel_bufmgr_gem_enable_reuse(drm_intel_bufmgr *bufmgr);
+void drm_intel_bufmgr_gem_enable_fenced_relocs(drm_intel_bufmgr *bufmgr);
+int drm_intel_gem_bo_map_gtt(drm_intel_bo *bo);
+int drm_intel_gem_bo_unmap_gtt(drm_intel_bo *bo);
+int drm_intel_gem_bo_get_reloc_count(drm_intel_bo *bo);
+void drm_intel_gem_bo_clear_relocs(drm_intel_bo *bo, int start);
+void drm_intel_gem_bo_start_gtt_access(drm_intel_bo *bo, int write_enable);
+
+int drm_intel_get_pipe_from_crtc_id(drm_intel_bufmgr *bufmgr, int crtc_id);
+
+int drm_intel_get_aperture_sizes(int fd, size_t *mappable, size_t *total);
+
+/* drm_intel_bufmgr_fake.c */
+drm_intel_bufmgr *drm_intel_bufmgr_fake_init(int fd,
+                                            unsigned long low_offset,
+                                            void *low_virtual,
+                                            unsigned long size,
+                                            volatile unsigned int
+                                            *last_dispatch);
+void drm_intel_bufmgr_fake_set_last_dispatch(drm_intel_bufmgr *bufmgr,
+                                            volatile unsigned int
+                                            *last_dispatch);
+void drm_intel_bufmgr_fake_set_exec_callback(drm_intel_bufmgr *bufmgr,
+                                            int (*exec) (drm_intel_bo *bo,
+                                                         unsigned int used,
+                                                         void *priv),
+                                            void *priv);
+void drm_intel_bufmgr_fake_set_fence_callback(drm_intel_bufmgr *bufmgr,
+                                             unsigned int (*emit) (void *priv),
+                                             void (*wait) (unsigned int fence,
+                                                           void *priv),
+                                             void *priv);
+drm_intel_bo *drm_intel_bo_fake_alloc_static(drm_intel_bufmgr *bufmgr,
+                                            const char *name,
+                                            unsigned long offset,
+                                            unsigned long size, void *virt);
+void drm_intel_bo_fake_disable_backing_store(drm_intel_bo *bo,
+                                            void (*invalidate_cb) (drm_intel_bo
+                                                                   * bo,
+                                                                   void *ptr),
+                                            void *ptr);
+
+void drm_intel_bufmgr_fake_contended_lock_take(drm_intel_bufmgr *bufmgr);
+void drm_intel_bufmgr_fake_evict_all(drm_intel_bufmgr *bufmgr);
+
+/** @{ Compatibility defines to keep old code building despite the symbol rename
+ * from dri_* to drm_intel_*
+ */
+#define dri_bo drm_intel_bo
+#define dri_bufmgr drm_intel_bufmgr
+#define dri_bo_alloc drm_intel_bo_alloc
+#define dri_bo_reference drm_intel_bo_reference
+#define dri_bo_unreference drm_intel_bo_unreference
+#define dri_bo_map drm_intel_bo_map
+#define dri_bo_unmap drm_intel_bo_unmap
+#define dri_bo_subdata drm_intel_bo_subdata
+#define dri_bo_get_subdata drm_intel_bo_get_subdata
+#define dri_bo_wait_rendering drm_intel_bo_wait_rendering
+#define dri_bufmgr_set_debug drm_intel_bufmgr_set_debug
+#define dri_bufmgr_destroy drm_intel_bufmgr_destroy
+#define dri_bo_exec drm_intel_bo_exec
+#define dri_bufmgr_check_aperture_space drm_intel_bufmgr_check_aperture_space
+#define dri_bo_emit_reloc(reloc_bo, read, write, target_offset,                \
+                         reloc_offset, target_bo)                      \
+       drm_intel_bo_emit_reloc(reloc_bo, reloc_offset,                 \
+                               target_bo, target_offset,               \
+                               read, write);
+#define dri_bo_pin drm_intel_bo_pin
+#define dri_bo_unpin drm_intel_bo_unpin
+#define dri_bo_get_tiling drm_intel_bo_get_tiling
+#define dri_bo_set_tiling(bo, mode) drm_intel_bo_set_tiling(bo, mode, 0)
+#define dri_bo_flink drm_intel_bo_flink
+#define intel_bufmgr_gem_init drm_intel_bufmgr_gem_init
+#define intel_bo_gem_create_from_name drm_intel_bo_gem_create_from_name
+#define intel_bufmgr_gem_enable_reuse drm_intel_bufmgr_gem_enable_reuse
+#define intel_bufmgr_fake_init drm_intel_bufmgr_fake_init
+#define intel_bufmgr_fake_set_last_dispatch drm_intel_bufmgr_fake_set_last_dispatch
+#define intel_bufmgr_fake_set_exec_callback drm_intel_bufmgr_fake_set_exec_callback
+#define intel_bufmgr_fake_set_fence_callback drm_intel_bufmgr_fake_set_fence_callback
+#define intel_bo_fake_alloc_static drm_intel_bo_fake_alloc_static
+#define intel_bo_fake_disable_backing_store drm_intel_bo_fake_disable_backing_store
+#define intel_bufmgr_fake_contended_lock_take drm_intel_bufmgr_fake_contended_lock_take
+#define intel_bufmgr_fake_evict_all drm_intel_bufmgr_fake_evict_all
+
+/** @{ */
+
+#endif /* INTEL_BUFMGR_H */
diff --git a/intel/intel_bufmgr_fake.c b/intel/intel_bufmgr_fake.c
new file mode 100644 (file)
index 0000000..d9b5cfd
--- /dev/null
@@ -0,0 +1,1633 @@
+/**************************************************************************
+ * 
+ * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
+ * All Rights Reserved.
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ * 
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ * 
+ **************************************************************************/
+
+/* Originally a fake version of the buffer manager so that we can
+ * prototype the changes in a driver fairly quickly, has been fleshed
+ * out to a fully functional interim solution.
+ *
+ * Basically wraps the old style memory management in the new
+ * programming interface, but is more expressive and avoids many of
+ * the bugs in the old texture manager.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <errno.h>
+#include <xf86drm.h>
+#include <pthread.h>
+#include "intel_bufmgr.h"
+#include "intel_bufmgr_priv.h"
+#include "drm.h"
+#include "i915_drm.h"
+#include "mm.h"
+#include "libdrm_lists.h"
+
+/* Support gcc's __FUNCTION__ for people using other compilers */
+#if !defined(__GNUC__) && !defined(__FUNCTION__)
+# define __FUNCTION__ __func__ /* C99 */
+#endif
+
+#define DBG(...) do {                                  \
+       if (bufmgr_fake->bufmgr.debug)                  \
+               drmMsg(__VA_ARGS__);                    \
+} while (0)
+
+/* Internal flags:
+ */
+#define BM_NO_BACKING_STORE                    0x00000001
+#define BM_NO_FENCE_SUBDATA                    0x00000002
+#define BM_PINNED                              0x00000004
+
+/* Wrapper around mm.c's mem_block, which understands that you must
+ * wait for fences to expire before memory can be freed.  This is
+ * specific to our use of memcpy for uploads - an upload that was
+ * processed through the command queue wouldn't need to care about
+ * fences.
+ */
+#define MAX_RELOCS 4096
+
+struct fake_buffer_reloc {
+       /** Buffer object that the relocation points at. */
+       drm_intel_bo *target_buf;
+       /** Offset of the relocation entry within reloc_buf. */
+       uint32_t offset;
+       /**
+        * Cached value of the offset when we last performed this relocation.
+        */
+       uint32_t last_target_offset;
+       /** Value added to target_buf's offset to get the relocation entry. */
+       uint32_t delta;
+       /** Cache domains the target buffer is read into. */
+       uint32_t read_domains;
+       /** Cache domain the target buffer will have dirty cachelines in. */
+       uint32_t write_domain;
+};
+
+struct block {
+       struct block *next, *prev;
+       struct mem_block *mem;  /* BM_MEM_AGP */
+
+       /**
+        * Marks that the block is currently in the aperture and has yet to be
+        * fenced.
+        */
+       unsigned on_hardware:1;
+       /**
+        * Marks that the block is currently fenced (being used by rendering)
+        * and can't be freed until @fence is passed.
+        */
+       unsigned fenced:1;
+
+       /** Fence cookie for the block. */
+       unsigned fence;         /* Split to read_fence, write_fence */
+
+       drm_intel_bo *bo;
+       void *virtual;
+};
+
+typedef struct _bufmgr_fake {
+       drm_intel_bufmgr bufmgr;
+
+       pthread_mutex_t lock;
+
+       unsigned long low_offset;
+       unsigned long size;
+       void *virtual;
+
+       struct mem_block *heap;
+
+       unsigned buf_nr;        /* for generating ids */
+
+       /**
+        * List of blocks which are currently in the GART but haven't been
+        * fenced yet.
+        */
+       struct block on_hardware;
+       /**
+        * List of blocks which are in the GART and have an active fence on
+        * them.
+        */
+       struct block fenced;
+       /**
+        * List of blocks which have an expired fence and are ready to be
+        * evicted.
+        */
+       struct block lru;
+
+       unsigned int last_fence;
+
+       unsigned fail:1;
+       unsigned need_fence:1;
+       int thrashing;
+
+       /**
+        * Driver callback to emit a fence, returning the cookie.
+        *
+        * This allows the driver to hook in a replacement for the DRM usage in
+        * bufmgr_fake.
+        *
+        * Currently, this also requires that a write flush be emitted before
+        * emitting the fence, but this should change.
+        */
+       unsigned int (*fence_emit) (void *private);
+       /** Driver callback to wait for a fence cookie to have passed. */
+       void (*fence_wait) (unsigned int fence, void *private);
+       void *fence_priv;
+
+       /**
+        * Driver callback to execute a buffer.
+        *
+        * This allows the driver to hook in a replacement for the DRM usage in
+        * bufmgr_fake.
+        */
+       int (*exec) (drm_intel_bo *bo, unsigned int used, void *priv);
+       void *exec_priv;
+
+       /** Driver-supplied argument to driver callbacks */
+       void *driver_priv;
+       /**
+        * Pointer to kernel-updated sarea data for the last completed user irq
+        */
+       volatile int *last_dispatch;
+
+       int fd;
+
+       int debug;
+
+       int performed_rendering;
+} drm_intel_bufmgr_fake;
+
+typedef struct _drm_intel_bo_fake {
+       drm_intel_bo bo;
+
+       unsigned id;            /* debug only */
+       const char *name;
+
+       unsigned dirty:1;
+       /**
+        * has the card written to this buffer - we make need to copy it back
+        */
+       unsigned card_dirty:1;
+       unsigned int refcount;
+       /* Flags may consist of any of the DRM_BO flags, plus
+        * DRM_BO_NO_BACKING_STORE and BM_NO_FENCE_SUBDATA, which are the
+        * first two driver private flags.
+        */
+       uint64_t flags;
+       /** Cache domains the target buffer is read into. */
+       uint32_t read_domains;
+       /** Cache domain the target buffer will have dirty cachelines in. */
+       uint32_t write_domain;
+
+       unsigned int alignment;
+       int is_static, validated;
+       unsigned int map_count;
+
+       /** relocation list */
+       struct fake_buffer_reloc *relocs;
+       int nr_relocs;
+       /**
+        * Total size of the target_bos of this buffer.
+        *
+        * Used for estimation in check_aperture.
+        */
+       unsigned int child_size;
+
+       struct block *block;
+       void *backing_store;
+       void (*invalidate_cb) (drm_intel_bo *bo, void *ptr);
+       void *invalidate_ptr;
+} drm_intel_bo_fake;
+
+static int clear_fenced(drm_intel_bufmgr_fake *bufmgr_fake,
+                       unsigned int fence_cookie);
+
+#define MAXFENCE 0x7fffffff
+
+static int
+FENCE_LTE(unsigned a, unsigned b)
+{
+       if (a == b)
+               return 1;
+
+       if (a < b && b - a < (1 << 24))
+               return 1;
+
+       if (a > b && MAXFENCE - a + b < (1 << 24))
+               return 1;
+
+       return 0;
+}
+
+void
+drm_intel_bufmgr_fake_set_fence_callback(drm_intel_bufmgr *bufmgr,
+                                        unsigned int (*emit) (void *priv),
+                                        void (*wait) (unsigned int fence,
+                                                      void *priv),
+                                        void *priv)
+{
+       drm_intel_bufmgr_fake *bufmgr_fake = (drm_intel_bufmgr_fake *) bufmgr;
+
+       bufmgr_fake->fence_emit = emit;
+       bufmgr_fake->fence_wait = wait;
+       bufmgr_fake->fence_priv = priv;
+}
+
+static unsigned int
+_fence_emit_internal(drm_intel_bufmgr_fake *bufmgr_fake)
+{
+       struct drm_i915_irq_emit ie;
+       int ret, seq = 1;
+
+       if (bufmgr_fake->fence_emit != NULL) {
+               seq = bufmgr_fake->fence_emit(bufmgr_fake->fence_priv);
+               return seq;
+       }
+
+       ie.irq_seq = &seq;
+       ret = drmCommandWriteRead(bufmgr_fake->fd, DRM_I915_IRQ_EMIT,
+                                 &ie, sizeof(ie));
+       if (ret) {
+               drmMsg("%s: drm_i915_irq_emit: %d\n", __FUNCTION__, ret);
+               abort();
+       }
+
+       DBG("emit 0x%08x\n", seq);
+       return seq;
+}
+
+static void
+_fence_wait_internal(drm_intel_bufmgr_fake *bufmgr_fake, int seq)
+{
+       struct drm_i915_irq_wait iw;
+       int hw_seq, busy_count = 0;
+       int ret;
+       int kernel_lied;
+
+       if (bufmgr_fake->fence_wait != NULL) {
+               bufmgr_fake->fence_wait(seq, bufmgr_fake->fence_priv);
+               clear_fenced(bufmgr_fake, seq);
+               return;
+       }
+
+       iw.irq_seq = seq;
+
+       DBG("wait 0x%08x\n", iw.irq_seq);
+
+       /* The kernel IRQ_WAIT implementation is all sorts of broken.
+        * 1) It returns 1 to 0x7fffffff instead of using the full 32-bit
+        *    unsigned range.
+        * 2) It returns 0 if hw_seq >= seq, not seq - hw_seq < 0 on the 32-bit
+        *    signed range.
+        * 3) It waits if seq < hw_seq, not seq - hw_seq > 0 on the 32-bit
+        *    signed range.
+        * 4) It returns -EBUSY in 3 seconds even if the hardware is still
+        *    successfully chewing through buffers.
+        *
+        * Assume that in userland we treat sequence numbers as ints, which
+        * makes some of the comparisons convenient, since the sequence
+        * numbers are all postive signed integers.
+        *
+        * From this we get several cases we need to handle.  Here's a timeline.
+        * 0x2   0x7                                    0x7ffffff8   0x7ffffffd
+        *   |    |                                             |    |
+        * ------------------------------------------------------------
+        *
+        * A) Normal wait for hw to catch up
+        * hw_seq seq
+        *   |    |
+        * ------------------------------------------------------------
+        * seq - hw_seq = 5.  If we call IRQ_WAIT, it will wait for hw to
+        * catch up.
+        *
+        * B) Normal wait for a sequence number that's already passed.
+        * seq    hw_seq
+        *   |    |
+        * ------------------------------------------------------------
+        * seq - hw_seq = -5.  If we call IRQ_WAIT, it returns 0 quickly.
+        *
+        * C) Hardware has already wrapped around ahead of us
+        * hw_seq                                                    seq
+        *   |                                                       |
+        * ------------------------------------------------------------
+        * seq - hw_seq = 0x80000000 - 5.  If we called IRQ_WAIT, it would wait
+        * for hw_seq >= seq, which may never occur.  Thus, we want to catch
+        * this in userland and return 0.
+        *
+        * D) We've wrapped around ahead of the hardware.
+        * seq                                                      hw_seq
+        *   |                                                       |
+        * ------------------------------------------------------------
+        * seq - hw_seq = -(0x80000000 - 5).  If we called IRQ_WAIT, it would
+        * return 0 quickly because hw_seq >= seq, even though the hardware
+        * isn't caught up. Thus, we need to catch this early return in
+        * userland and bother the kernel until the hardware really does
+        * catch up.
+        *
+        * E) Hardware might wrap after we test in userland.
+        *                                                  hw_seq  seq
+        *                                                      |    |
+        * ------------------------------------------------------------
+        * seq - hw_seq = 5.  If we call IRQ_WAIT, it will likely see seq >=
+        * hw_seq and wait.  However, suppose hw_seq wraps before we make it
+        * into the kernel.  The kernel sees hw_seq >= seq and waits for 3
+        * seconds then returns -EBUSY.  This is case C).  We should catch
+        * this and then return successfully.
+        *
+        * F) Hardware might take a long time on a buffer.
+        * hw_seq seq
+        *   |    |
+        * -------------------------------------------------------------------
+        * seq - hw_seq = 5.  If we call IRQ_WAIT, if sequence 2 through 5
+        * take too long, it will return -EBUSY.  Batchbuffers in the
+        * gltestperf demo were seen to take up to 7 seconds.  We should
+        * catch early -EBUSY return and keep trying.
+        */
+
+       do {
+               /* Keep a copy of last_dispatch so that if the wait -EBUSYs
+                * because the hardware didn't catch up in 3 seconds, we can
+                * see if it at least made progress and retry.
+                */
+               hw_seq = *bufmgr_fake->last_dispatch;
+
+               /* Catch case C */
+               if (seq - hw_seq > 0x40000000)
+                       return;
+
+               ret = drmCommandWrite(bufmgr_fake->fd, DRM_I915_IRQ_WAIT,
+                                     &iw, sizeof(iw));
+               /* Catch case D */
+               kernel_lied = (ret == 0) && (seq - *bufmgr_fake->last_dispatch <
+                                            -0x40000000);
+
+               /* Catch case E */
+               if (ret == -EBUSY
+                   && (seq - *bufmgr_fake->last_dispatch > 0x40000000))
+                       ret = 0;
+
+               /* Catch case F: Allow up to 15 seconds chewing on one buffer. */
+               if ((ret == -EBUSY) && (hw_seq != *bufmgr_fake->last_dispatch))
+                       busy_count = 0;
+               else
+                       busy_count++;
+       } while (kernel_lied || ret == -EAGAIN || ret == -EINTR ||
+                (ret == -EBUSY && busy_count < 5));
+
+       if (ret != 0) {
+               drmMsg("%s:%d: Error waiting for fence: %s.\n", __FILE__,
+                      __LINE__, strerror(-ret));
+               abort();
+       }
+       clear_fenced(bufmgr_fake, seq);
+}
+
+static int
+_fence_test(drm_intel_bufmgr_fake *bufmgr_fake, unsigned fence)
+{
+       /* Slight problem with wrap-around:
+        */
+       return fence == 0 || FENCE_LTE(fence, bufmgr_fake->last_fence);
+}
+
+/**
+ * Allocate a memory manager block for the buffer.
+ */
+static int
+alloc_block(drm_intel_bo *bo)
+{
+       drm_intel_bo_fake *bo_fake = (drm_intel_bo_fake *) bo;
+       drm_intel_bufmgr_fake *bufmgr_fake =
+           (drm_intel_bufmgr_fake *) bo->bufmgr;
+       struct block *block = (struct block *)calloc(sizeof *block, 1);
+       unsigned int align_log2 = ffs(bo_fake->alignment) - 1;
+       unsigned int sz;
+
+       if (!block)
+               return 1;
+
+       sz = (bo->size + bo_fake->alignment - 1) & ~(bo_fake->alignment - 1);
+
+       block->mem = mmAllocMem(bufmgr_fake->heap, sz, align_log2, 0);
+       if (!block->mem) {
+               free(block);
+               return 0;
+       }
+
+       DRMINITLISTHEAD(block);
+
+       /* Insert at head or at tail??? */
+       DRMLISTADDTAIL(block, &bufmgr_fake->lru);
+
+       block->virtual = (uint8_t *) bufmgr_fake->virtual +
+           block->mem->ofs - bufmgr_fake->low_offset;
+       block->bo = bo;
+
+       bo_fake->block = block;
+
+       return 1;
+}
+
+/* Release the card storage associated with buf:
+ */
+static void
+free_block(drm_intel_bufmgr_fake *bufmgr_fake, struct block *block,
+          int skip_dirty_copy)
+{
+       drm_intel_bo_fake *bo_fake;
+       DBG("free block %p %08x %d %d\n", block, block->mem->ofs,
+           block->on_hardware, block->fenced);
+
+       if (!block)
+               return;
+
+       bo_fake = (drm_intel_bo_fake *) block->bo;
+
+       if (bo_fake->flags & (BM_PINNED | BM_NO_BACKING_STORE))
+               skip_dirty_copy = 1;
+
+       if (!skip_dirty_copy && (bo_fake->card_dirty == 1)) {
+               memcpy(bo_fake->backing_store, block->virtual, block->bo->size);
+               bo_fake->card_dirty = 0;
+               bo_fake->dirty = 1;
+       }
+
+       if (block->on_hardware) {
+               block->bo = NULL;
+       } else if (block->fenced) {
+               block->bo = NULL;
+       } else {
+               DBG("    - free immediately\n");
+               DRMLISTDEL(block);
+
+               mmFreeMem(block->mem);
+               free(block);
+       }
+}
+
+static void
+alloc_backing_store(drm_intel_bo *bo)
+{
+       drm_intel_bufmgr_fake *bufmgr_fake =
+           (drm_intel_bufmgr_fake *) bo->bufmgr;
+       drm_intel_bo_fake *bo_fake = (drm_intel_bo_fake *) bo;
+       assert(!bo_fake->backing_store);
+       assert(!(bo_fake->flags & (BM_PINNED | BM_NO_BACKING_STORE)));
+
+       bo_fake->backing_store = malloc(bo->size);
+
+       DBG("alloc_backing - buf %d %p %d\n", bo_fake->id,
+           bo_fake->backing_store, bo->size);
+       assert(bo_fake->backing_store);
+}
+
+static void
+free_backing_store(drm_intel_bo *bo)
+{
+       drm_intel_bo_fake *bo_fake = (drm_intel_bo_fake *) bo;
+
+       if (bo_fake->backing_store) {
+               assert(!(bo_fake->flags & (BM_PINNED | BM_NO_BACKING_STORE)));
+               free(bo_fake->backing_store);
+               bo_fake->backing_store = NULL;
+       }
+}
+
+static void
+set_dirty(drm_intel_bo *bo)
+{
+       drm_intel_bufmgr_fake *bufmgr_fake =
+           (drm_intel_bufmgr_fake *) bo->bufmgr;
+       drm_intel_bo_fake *bo_fake = (drm_intel_bo_fake *) bo;
+
+       if (bo_fake->flags & BM_NO_BACKING_STORE
+           && bo_fake->invalidate_cb != NULL)
+               bo_fake->invalidate_cb(bo, bo_fake->invalidate_ptr);
+
+       assert(!(bo_fake->flags & BM_PINNED));
+
+       DBG("set_dirty - buf %d\n", bo_fake->id);
+       bo_fake->dirty = 1;
+}
+
+static int
+evict_lru(drm_intel_bufmgr_fake *bufmgr_fake, unsigned int max_fence)
+{
+       struct block *block, *tmp;
+
+       DBG("%s\n", __FUNCTION__);
+
+       DRMLISTFOREACHSAFE(block, tmp, &bufmgr_fake->lru) {
+               drm_intel_bo_fake *bo_fake = (drm_intel_bo_fake *) block->bo;
+
+               if (bo_fake != NULL && (bo_fake->flags & BM_NO_FENCE_SUBDATA))
+                       continue;
+
+               if (block->fence && max_fence && !FENCE_LTE(block->fence,
+                                                           max_fence))
+                       return 0;
+
+               set_dirty(&bo_fake->bo);
+               bo_fake->block = NULL;
+
+               free_block(bufmgr_fake, block, 0);
+               return 1;
+       }
+
+       return 0;
+}
+
+static int
+evict_mru(drm_intel_bufmgr_fake *bufmgr_fake)
+{
+       struct block *block, *tmp;
+
+       DBG("%s\n", __FUNCTION__);
+
+       DRMLISTFOREACHSAFEREVERSE(block, tmp, &bufmgr_fake->lru) {
+               drm_intel_bo_fake *bo_fake = (drm_intel_bo_fake *) block->bo;
+
+               if (bo_fake && (bo_fake->flags & BM_NO_FENCE_SUBDATA))
+                       continue;
+
+               set_dirty(&bo_fake->bo);
+               bo_fake->block = NULL;
+
+               free_block(bufmgr_fake, block, 0);
+               return 1;
+       }
+
+       return 0;
+}
+
+/**
+ * Removes all objects from the fenced list older than the given fence.
+ */
+static int
+clear_fenced(drm_intel_bufmgr_fake *bufmgr_fake, unsigned int fence_cookie)
+{
+       struct block *block, *tmp;
+       int ret = 0;
+
+       bufmgr_fake->last_fence = fence_cookie;
+       DRMLISTFOREACHSAFE(block, tmp, &bufmgr_fake->fenced) {
+               assert(block->fenced);
+
+               if (_fence_test(bufmgr_fake, block->fence)) {
+
+                       block->fenced = 0;
+
+                       if (!block->bo) {
+                               DBG("delayed free: offset %x sz %x\n",
+                                   block->mem->ofs, block->mem->size);
+                               DRMLISTDEL(block);
+                               mmFreeMem(block->mem);
+                               free(block);
+                       } else {
+                               DBG("return to lru: offset %x sz %x\n",
+                                   block->mem->ofs, block->mem->size);
+                               DRMLISTDEL(block);
+                               DRMLISTADDTAIL(block, &bufmgr_fake->lru);
+                       }
+
+                       ret = 1;
+               } else {
+                       /* Blocks are ordered by fence, so if one fails, all
+                        * from here will fail also:
+                        */
+                       DBG("fence not passed: offset %x sz %x %d %d \n",
+                           block->mem->ofs, block->mem->size, block->fence,
+                           bufmgr_fake->last_fence);
+                       break;
+               }
+       }
+
+       DBG("%s: %d\n", __FUNCTION__, ret);
+       return ret;
+}
+
+static void
+fence_blocks(drm_intel_bufmgr_fake *bufmgr_fake, unsigned fence)
+{
+       struct block *block, *tmp;
+
+       DRMLISTFOREACHSAFE(block, tmp, &bufmgr_fake->on_hardware) {
+               DBG("Fence block %p (sz 0x%x ofs %x buf %p) with fence %d\n",
+                   block, block->mem->size, block->mem->ofs, block->bo, fence);
+               block->fence = fence;
+
+               block->on_hardware = 0;
+               block->fenced = 1;
+
+               /* Move to tail of pending list here
+                */
+               DRMLISTDEL(block);
+               DRMLISTADDTAIL(block, &bufmgr_fake->fenced);
+       }
+
+       assert(DRMLISTEMPTY(&bufmgr_fake->on_hardware));
+}
+
+static int
+evict_and_alloc_block(drm_intel_bo *bo)
+{
+       drm_intel_bufmgr_fake *bufmgr_fake =
+           (drm_intel_bufmgr_fake *) bo->bufmgr;
+       drm_intel_bo_fake *bo_fake = (drm_intel_bo_fake *) bo;
+
+       assert(bo_fake->block == NULL);
+
+       /* Search for already free memory:
+        */
+       if (alloc_block(bo))
+               return 1;
+
+       /* If we're not thrashing, allow lru eviction to dig deeper into
+        * recently used textures.  We'll probably be thrashing soon:
+        */
+       if (!bufmgr_fake->thrashing) {
+               while (evict_lru(bufmgr_fake, 0))
+                       if (alloc_block(bo))
+                               return 1;
+       }
+
+       /* Keep thrashing counter alive?
+        */
+       if (bufmgr_fake->thrashing)
+               bufmgr_fake->thrashing = 20;
+
+       /* Wait on any already pending fences - here we are waiting for any
+        * freed memory that has been submitted to hardware and fenced to
+        * become available:
+        */
+       while (!DRMLISTEMPTY(&bufmgr_fake->fenced)) {
+               uint32_t fence = bufmgr_fake->fenced.next->fence;
+               _fence_wait_internal(bufmgr_fake, fence);
+
+               if (alloc_block(bo))
+                       return 1;
+       }
+
+       if (!DRMLISTEMPTY(&bufmgr_fake->on_hardware)) {
+               while (!DRMLISTEMPTY(&bufmgr_fake->fenced)) {
+                       uint32_t fence = bufmgr_fake->fenced.next->fence;
+                       _fence_wait_internal(bufmgr_fake, fence);
+               }
+
+               if (!bufmgr_fake->thrashing) {
+                       DBG("thrashing\n");
+               }
+               bufmgr_fake->thrashing = 20;
+
+               if (alloc_block(bo))
+                       return 1;
+       }
+
+       while (evict_mru(bufmgr_fake))
+               if (alloc_block(bo))
+                       return 1;
+
+       DBG("%s 0x%x bytes failed\n", __FUNCTION__, bo->size);
+
+       return 0;
+}
+
+/***********************************************************************
+ * Public functions
+ */
+
+/**
+ * Wait for hardware idle by emitting a fence and waiting for it.
+ */
+static void
+drm_intel_bufmgr_fake_wait_idle(drm_intel_bufmgr_fake *bufmgr_fake)
+{
+       unsigned int cookie;
+
+       cookie = _fence_emit_internal(bufmgr_fake);
+       _fence_wait_internal(bufmgr_fake, cookie);
+}
+
+/**
+ * Wait for rendering to a buffer to complete.
+ *
+ * It is assumed that the bathcbuffer which performed the rendering included
+ * the necessary flushing.
+ */
+static void
+drm_intel_fake_bo_wait_rendering_locked(drm_intel_bo *bo)
+{
+       drm_intel_bufmgr_fake *bufmgr_fake =
+           (drm_intel_bufmgr_fake *) bo->bufmgr;
+       drm_intel_bo_fake *bo_fake = (drm_intel_bo_fake *) bo;
+
+       if (bo_fake->block == NULL || !bo_fake->block->fenced)
+               return;
+
+       _fence_wait_internal(bufmgr_fake, bo_fake->block->fence);
+}
+
+static void
+drm_intel_fake_bo_wait_rendering(drm_intel_bo *bo)
+{
+       drm_intel_bufmgr_fake *bufmgr_fake =
+           (drm_intel_bufmgr_fake *) bo->bufmgr;
+
+       pthread_mutex_lock(&bufmgr_fake->lock);
+       drm_intel_fake_bo_wait_rendering_locked(bo);
+       pthread_mutex_unlock(&bufmgr_fake->lock);
+}
+
+/* Specifically ignore texture memory sharing.
+ *  -- just evict everything
+ *  -- and wait for idle
+ */
+void
+drm_intel_bufmgr_fake_contended_lock_take(drm_intel_bufmgr *bufmgr)
+{
+       drm_intel_bufmgr_fake *bufmgr_fake = (drm_intel_bufmgr_fake *) bufmgr;
+       struct block *block, *tmp;
+
+       pthread_mutex_lock(&bufmgr_fake->lock);
+
+       bufmgr_fake->need_fence = 1;
+       bufmgr_fake->fail = 0;
+
+       /* Wait for hardware idle.  We don't know where acceleration has been
+        * happening, so we'll need to wait anyway before letting anything get
+        * put on the card again.
+        */
+       drm_intel_bufmgr_fake_wait_idle(bufmgr_fake);
+
+       /* Check that we hadn't released the lock without having fenced the last
+        * set of buffers.
+        */
+       assert(DRMLISTEMPTY(&bufmgr_fake->fenced));
+       assert(DRMLISTEMPTY(&bufmgr_fake->on_hardware));
+
+       DRMLISTFOREACHSAFE(block, tmp, &bufmgr_fake->lru) {
+               assert(_fence_test(bufmgr_fake, block->fence));
+               set_dirty(block->bo);
+       }
+
+       pthread_mutex_unlock(&bufmgr_fake->lock);
+}
+
+static drm_intel_bo *
+drm_intel_fake_bo_alloc(drm_intel_bufmgr *bufmgr,
+                       const char *name,
+                       unsigned long size,
+                       unsigned int alignment)
+{
+       drm_intel_bufmgr_fake *bufmgr_fake;
+       drm_intel_bo_fake *bo_fake;
+
+       bufmgr_fake = (drm_intel_bufmgr_fake *) bufmgr;
+
+       assert(size != 0);
+
+       bo_fake = calloc(1, sizeof(*bo_fake));
+       if (!bo_fake)
+               return NULL;
+
+       bo_fake->bo.size = size;
+       bo_fake->bo.offset = -1;
+       bo_fake->bo.virtual = NULL;
+       bo_fake->bo.bufmgr = bufmgr;
+       bo_fake->refcount = 1;
+
+       /* Alignment must be a power of two */
+       assert((alignment & (alignment - 1)) == 0);
+       if (alignment == 0)
+               alignment = 1;
+       bo_fake->alignment = alignment;
+       bo_fake->id = ++bufmgr_fake->buf_nr;
+       bo_fake->name = name;
+       bo_fake->flags = 0;
+       bo_fake->is_static = 0;
+
+       DBG("drm_bo_alloc: (buf %d: %s, %d kb)\n", bo_fake->id, bo_fake->name,
+           bo_fake->bo.size / 1024);
+
+       return &bo_fake->bo;
+}
+
+static drm_intel_bo *
+drm_intel_fake_bo_alloc_tiled(drm_intel_bufmgr * bufmgr,
+                             const char *name,
+                             int x, int y, int cpp,
+                             uint32_t *tiling_mode,
+                             unsigned long *pitch,
+                             unsigned long flags)
+{
+       unsigned long stride, aligned_y;
+
+       /* No runtime tiling support for fake. */
+       *tiling_mode = I915_TILING_NONE;
+
+       /* Align it for being a render target.  Shouldn't need anything else. */
+       stride = x * cpp;
+       stride = ROUND_UP_TO(stride, 64);
+
+       /* 965 subspan loading alignment */
+       aligned_y = ALIGN(y, 2);
+
+       *pitch = stride;
+
+       return drm_intel_fake_bo_alloc(bufmgr, name, stride * aligned_y,
+                                      4096);
+}
+
+drm_intel_bo *
+drm_intel_bo_fake_alloc_static(drm_intel_bufmgr *bufmgr,
+                              const char *name,
+                              unsigned long offset,
+                              unsigned long size, void *virtual)
+{
+       drm_intel_bufmgr_fake *bufmgr_fake;
+       drm_intel_bo_fake *bo_fake;
+
+       bufmgr_fake = (drm_intel_bufmgr_fake *) bufmgr;
+
+       assert(size != 0);
+
+       bo_fake = calloc(1, sizeof(*bo_fake));
+       if (!bo_fake)
+               return NULL;
+
+       bo_fake->bo.size = size;
+       bo_fake->bo.offset = offset;
+       bo_fake->bo.virtual = virtual;
+       bo_fake->bo.bufmgr = bufmgr;
+       bo_fake->refcount = 1;
+       bo_fake->id = ++bufmgr_fake->buf_nr;
+       bo_fake->name = name;
+       bo_fake->flags = BM_PINNED;
+       bo_fake->is_static = 1;
+
+       DBG("drm_bo_alloc_static: (buf %d: %s, %d kb)\n", bo_fake->id,
+           bo_fake->name, bo_fake->bo.size / 1024);
+
+       return &bo_fake->bo;
+}
+
+static void
+drm_intel_fake_bo_reference(drm_intel_bo *bo)
+{
+       drm_intel_bufmgr_fake *bufmgr_fake =
+           (drm_intel_bufmgr_fake *) bo->bufmgr;
+       drm_intel_bo_fake *bo_fake = (drm_intel_bo_fake *) bo;
+
+       pthread_mutex_lock(&bufmgr_fake->lock);
+       bo_fake->refcount++;
+       pthread_mutex_unlock(&bufmgr_fake->lock);
+}
+
+static void
+drm_intel_fake_bo_reference_locked(drm_intel_bo *bo)
+{
+       drm_intel_bo_fake *bo_fake = (drm_intel_bo_fake *) bo;
+
+       bo_fake->refcount++;
+}
+
+static void
+drm_intel_fake_bo_unreference_locked(drm_intel_bo *bo)
+{
+       drm_intel_bufmgr_fake *bufmgr_fake =
+           (drm_intel_bufmgr_fake *) bo->bufmgr;
+       drm_intel_bo_fake *bo_fake = (drm_intel_bo_fake *) bo;
+       int i;
+
+       if (--bo_fake->refcount == 0) {
+               assert(bo_fake->map_count == 0);
+               /* No remaining references, so free it */
+               if (bo_fake->block)
+                       free_block(bufmgr_fake, bo_fake->block, 1);
+               free_backing_store(bo);
+
+               for (i = 0; i < bo_fake->nr_relocs; i++)
+                       drm_intel_fake_bo_unreference_locked(bo_fake->relocs[i].
+                                                            target_buf);
+
+               DBG("drm_bo_unreference: free buf %d %s\n", bo_fake->id,
+                   bo_fake->name);
+
+               free(bo_fake->relocs);
+               free(bo);
+       }
+}
+
+static void
+drm_intel_fake_bo_unreference(drm_intel_bo *bo)
+{
+       drm_intel_bufmgr_fake *bufmgr_fake =
+           (drm_intel_bufmgr_fake *) bo->bufmgr;
+
+       pthread_mutex_lock(&bufmgr_fake->lock);
+       drm_intel_fake_bo_unreference_locked(bo);
+       pthread_mutex_unlock(&bufmgr_fake->lock);
+}
+
+/**
+ * Set the buffer as not requiring backing store, and instead get the callback
+ * invoked whenever it would be set dirty.
+ */
+void
+drm_intel_bo_fake_disable_backing_store(drm_intel_bo *bo,
+                                       void (*invalidate_cb) (drm_intel_bo *bo,
+                                                              void *ptr),
+                                       void *ptr)
+{
+       drm_intel_bufmgr_fake *bufmgr_fake =
+           (drm_intel_bufmgr_fake *) bo->bufmgr;
+       drm_intel_bo_fake *bo_fake = (drm_intel_bo_fake *) bo;
+
+       pthread_mutex_lock(&bufmgr_fake->lock);
+
+       if (bo_fake->backing_store)
+               free_backing_store(bo);
+
+       bo_fake->flags |= BM_NO_BACKING_STORE;
+
+       DBG("disable_backing_store set buf %d dirty\n", bo_fake->id);
+       bo_fake->dirty = 1;
+       bo_fake->invalidate_cb = invalidate_cb;
+       bo_fake->invalidate_ptr = ptr;
+
+       /* Note that it is invalid right from the start.  Also note
+        * invalidate_cb is called with the bufmgr locked, so cannot
+        * itself make bufmgr calls.
+        */
+       if (invalidate_cb != NULL)
+               invalidate_cb(bo, ptr);
+
+       pthread_mutex_unlock(&bufmgr_fake->lock);
+}
+
+/**
+ * Map a buffer into bo->virtual, allocating either card memory space (If
+ * BM_NO_BACKING_STORE or BM_PINNED) or backing store, as necessary.
+ */
+static int
+ drm_intel_fake_bo_map_locked(drm_intel_bo *bo, int write_enable)
+{
+       drm_intel_bufmgr_fake *bufmgr_fake =
+           (drm_intel_bufmgr_fake *) bo->bufmgr;
+       drm_intel_bo_fake *bo_fake = (drm_intel_bo_fake *) bo;
+
+       /* Static buffers are always mapped. */
+       if (bo_fake->is_static) {
+               if (bo_fake->card_dirty) {
+                       drm_intel_bufmgr_fake_wait_idle(bufmgr_fake);
+                       bo_fake->card_dirty = 0;
+               }
+               return 0;
+       }
+
+       /* Allow recursive mapping.  Mesa may recursively map buffers with
+        * nested display loops, and it is used internally in bufmgr_fake
+        * for relocation.
+        */
+       if (bo_fake->map_count++ != 0)
+               return 0;
+
+       {
+               DBG("drm_bo_map: (buf %d: %s, %d kb)\n", bo_fake->id,
+                   bo_fake->name, bo_fake->bo.size / 1024);
+
+               if (bo->virtual != NULL) {
+                       drmMsg("%s: already mapped\n", __FUNCTION__);
+                       abort();
+               } else if (bo_fake->flags & (BM_NO_BACKING_STORE | BM_PINNED)) {
+
+                       if (!bo_fake->block && !evict_and_alloc_block(bo)) {
+                               DBG("%s: alloc failed\n", __FUNCTION__);
+                               bufmgr_fake->fail = 1;
+                               return 1;
+                       } else {
+                               assert(bo_fake->block);
+                               bo_fake->dirty = 0;
+
+                               if (!(bo_fake->flags & BM_NO_FENCE_SUBDATA) &&
+                                   bo_fake->block->fenced) {
+                                       drm_intel_fake_bo_wait_rendering_locked
+                                           (bo);
+                               }
+
+                               bo->virtual = bo_fake->block->virtual;
+                       }
+               } else {
+                       if (write_enable)
+                               set_dirty(bo);
+
+                       if (bo_fake->backing_store == 0)
+                               alloc_backing_store(bo);
+
+                       if ((bo_fake->card_dirty == 1) && bo_fake->block) {
+                               if (bo_fake->block->fenced)
+                                       drm_intel_fake_bo_wait_rendering_locked
+                                           (bo);
+
+                               memcpy(bo_fake->backing_store,
+                                      bo_fake->block->virtual,
+                                      bo_fake->block->bo->size);
+                               bo_fake->card_dirty = 0;
+                       }
+
+                       bo->virtual = bo_fake->backing_store;
+               }
+       }
+
+       return 0;
+}
+
+static int
+ drm_intel_fake_bo_map(drm_intel_bo *bo, int write_enable)
+{
+       drm_intel_bufmgr_fake *bufmgr_fake =
+           (drm_intel_bufmgr_fake *) bo->bufmgr;
+       int ret;
+
+       pthread_mutex_lock(&bufmgr_fake->lock);
+       ret = drm_intel_fake_bo_map_locked(bo, write_enable);
+       pthread_mutex_unlock(&bufmgr_fake->lock);
+
+       return ret;
+}
+
+static int
+ drm_intel_fake_bo_unmap_locked(drm_intel_bo *bo)
+{
+       drm_intel_bufmgr_fake *bufmgr_fake =
+           (drm_intel_bufmgr_fake *) bo->bufmgr;
+       drm_intel_bo_fake *bo_fake = (drm_intel_bo_fake *) bo;
+
+       /* Static buffers are always mapped. */
+       if (bo_fake->is_static)
+               return 0;
+
+       assert(bo_fake->map_count != 0);
+       if (--bo_fake->map_count != 0)
+               return 0;
+
+       DBG("drm_bo_unmap: (buf %d: %s, %d kb)\n", bo_fake->id, bo_fake->name,
+           bo_fake->bo.size / 1024);
+
+       bo->virtual = NULL;
+
+       return 0;
+}
+
+static int drm_intel_fake_bo_unmap(drm_intel_bo *bo)
+{
+       drm_intel_bufmgr_fake *bufmgr_fake =
+           (drm_intel_bufmgr_fake *) bo->bufmgr;
+       int ret;
+
+       pthread_mutex_lock(&bufmgr_fake->lock);
+       ret = drm_intel_fake_bo_unmap_locked(bo);
+       pthread_mutex_unlock(&bufmgr_fake->lock);
+
+       return ret;
+}
+
+static int
+drm_intel_fake_bo_subdata(drm_intel_bo *bo, unsigned long offset,
+                         unsigned long size, const void *data)
+{
+       int ret;
+
+       if (size == 0 || data == NULL)
+               return 0;
+
+       ret = drm_intel_bo_map(bo, 1);
+       if (ret)
+               return ret;
+       memcpy((unsigned char *)bo->virtual + offset, data, size);
+       drm_intel_bo_unmap(bo);
+       return 0;
+}
+
+static void
+ drm_intel_fake_kick_all_locked(drm_intel_bufmgr_fake *bufmgr_fake)
+{
+       struct block *block, *tmp;
+
+       bufmgr_fake->performed_rendering = 0;
+       /* okay for ever BO that is on the HW kick it off.
+          seriously not afraid of the POLICE right now */
+       DRMLISTFOREACHSAFE(block, tmp, &bufmgr_fake->on_hardware) {
+               drm_intel_bo_fake *bo_fake = (drm_intel_bo_fake *) block->bo;
+
+               block->on_hardware = 0;
+               free_block(bufmgr_fake, block, 0);
+               bo_fake->block = NULL;
+               bo_fake->validated = 0;
+               if (!(bo_fake->flags & BM_NO_BACKING_STORE))
+                       bo_fake->dirty = 1;
+       }
+
+}
+
+static int
+ drm_intel_fake_bo_validate(drm_intel_bo *bo)
+{
+       drm_intel_bufmgr_fake *bufmgr_fake;
+       drm_intel_bo_fake *bo_fake = (drm_intel_bo_fake *) bo;
+
+       bufmgr_fake = (drm_intel_bufmgr_fake *) bo->bufmgr;
+
+       DBG("drm_bo_validate: (buf %d: %s, %d kb)\n", bo_fake->id,
+           bo_fake->name, bo_fake->bo.size / 1024);
+
+       /* Sanity check: Buffers should be unmapped before being validated.
+        * This is not so much of a problem for bufmgr_fake, but TTM refuses,
+        * and the problem is harder to debug there.
+        */
+       assert(bo_fake->map_count == 0);
+
+       if (bo_fake->is_static) {
+               /* Add it to the needs-fence list */
+               bufmgr_fake->need_fence = 1;
+               return 0;
+       }
+
+       /* Allocate the card memory */
+       if (!bo_fake->block && !evict_and_alloc_block(bo)) {
+               bufmgr_fake->fail = 1;
+               DBG("Failed to validate buf %d:%s\n", bo_fake->id,
+                   bo_fake->name);
+               return -1;
+       }
+
+       assert(bo_fake->block);
+       assert(bo_fake->block->bo == &bo_fake->bo);
+
+       bo->offset = bo_fake->block->mem->ofs;
+
+       /* Upload the buffer contents if necessary */
+       if (bo_fake->dirty) {
+               DBG("Upload dirty buf %d:%s, sz %d offset 0x%x\n", bo_fake->id,
+                   bo_fake->name, bo->size, bo_fake->block->mem->ofs);
+
+               assert(!(bo_fake->flags & (BM_NO_BACKING_STORE | BM_PINNED)));
+
+               /* Actually, should be able to just wait for a fence on the
+                * mmory, hich we would be tracking when we free it.  Waiting
+                * for idle is a sufficiently large hammer for now.
+                */
+               drm_intel_bufmgr_fake_wait_idle(bufmgr_fake);
+
+               /* we may never have mapped this BO so it might not have any
+                * backing store if this happens it should be rare, but 0 the
+                * card memory in any case */
+               if (bo_fake->backing_store)
+                       memcpy(bo_fake->block->virtual, bo_fake->backing_store,
+                              bo->size);
+               else
+                       memset(bo_fake->block->virtual, 0, bo->size);
+
+               bo_fake->dirty = 0;
+       }
+
+       bo_fake->block->fenced = 0;
+       bo_fake->block->on_hardware = 1;
+       DRMLISTDEL(bo_fake->block);
+       DRMLISTADDTAIL(bo_fake->block, &bufmgr_fake->on_hardware);
+
+       bo_fake->validated = 1;
+       bufmgr_fake->need_fence = 1;
+
+       return 0;
+}
+
+static void
+drm_intel_fake_fence_validated(drm_intel_bufmgr *bufmgr)
+{
+       drm_intel_bufmgr_fake *bufmgr_fake = (drm_intel_bufmgr_fake *) bufmgr;
+       unsigned int cookie;
+
+       cookie = _fence_emit_internal(bufmgr_fake);
+       fence_blocks(bufmgr_fake, cookie);
+
+       DBG("drm_fence_validated: 0x%08x cookie\n", cookie);
+}
+
+static void
+drm_intel_fake_destroy(drm_intel_bufmgr *bufmgr)
+{
+       drm_intel_bufmgr_fake *bufmgr_fake = (drm_intel_bufmgr_fake *) bufmgr;
+
+       pthread_mutex_destroy(&bufmgr_fake->lock);
+       mmDestroy(bufmgr_fake->heap);
+       free(bufmgr);
+}
+
+static int
+drm_intel_fake_emit_reloc(drm_intel_bo *bo, uint32_t offset,
+                         drm_intel_bo *target_bo, uint32_t target_offset,
+                         uint32_t read_domains, uint32_t write_domain)
+{
+       drm_intel_bufmgr_fake *bufmgr_fake =
+           (drm_intel_bufmgr_fake *) bo->bufmgr;
+       struct fake_buffer_reloc *r;
+       drm_intel_bo_fake *bo_fake = (drm_intel_bo_fake *) bo;
+       drm_intel_bo_fake *target_fake = (drm_intel_bo_fake *) target_bo;
+       int i;
+
+       pthread_mutex_lock(&bufmgr_fake->lock);
+
+       assert(bo);
+       assert(target_bo);
+
+       if (bo_fake->relocs == NULL) {
+               bo_fake->relocs =
+                   malloc(sizeof(struct fake_buffer_reloc) * MAX_RELOCS);
+       }
+
+       r = &bo_fake->relocs[bo_fake->nr_relocs++];
+
+       assert(bo_fake->nr_relocs <= MAX_RELOCS);
+
+       drm_intel_fake_bo_reference_locked(target_bo);
+
+       if (!target_fake->is_static) {
+               bo_fake->child_size +=
+                   ALIGN(target_bo->size, target_fake->alignment);
+               bo_fake->child_size += target_fake->child_size;
+       }
+       r->target_buf = target_bo;
+       r->offset = offset;
+       r->last_target_offset = target_bo->offset;
+       r->delta = target_offset;
+       r->read_domains = read_domains;
+       r->write_domain = write_domain;
+
+       if (bufmgr_fake->debug) {
+               /* Check that a conflicting relocation hasn't already been
+                * emitted.
+                */
+               for (i = 0; i < bo_fake->nr_relocs - 1; i++) {
+                       struct fake_buffer_reloc *r2 = &bo_fake->relocs[i];
+
+                       assert(r->offset != r2->offset);
+               }
+       }
+
+       pthread_mutex_unlock(&bufmgr_fake->lock);
+
+       return 0;
+}
+
+/**
+ * Incorporates the validation flags associated with each relocation into
+ * the combined validation flags for the buffer on this batchbuffer submission.
+ */
+static void
+drm_intel_fake_calculate_domains(drm_intel_bo *bo)
+{
+       drm_intel_bo_fake *bo_fake = (drm_intel_bo_fake *) bo;
+       int i;
+
+       for (i = 0; i < bo_fake->nr_relocs; i++) {
+               struct fake_buffer_reloc *r = &bo_fake->relocs[i];
+               drm_intel_bo_fake *target_fake =
+                   (drm_intel_bo_fake *) r->target_buf;
+
+               /* Do the same for the tree of buffers we depend on */
+               drm_intel_fake_calculate_domains(r->target_buf);
+
+               target_fake->read_domains |= r->read_domains;
+               target_fake->write_domain |= r->write_domain;
+       }
+}
+
+static int
+drm_intel_fake_reloc_and_validate_buffer(drm_intel_bo *bo)
+{
+       drm_intel_bufmgr_fake *bufmgr_fake =
+           (drm_intel_bufmgr_fake *) bo->bufmgr;
+       drm_intel_bo_fake *bo_fake = (drm_intel_bo_fake *) bo;
+       int i, ret;
+
+       assert(bo_fake->map_count == 0);
+
+       for (i = 0; i < bo_fake->nr_relocs; i++) {
+               struct fake_buffer_reloc *r = &bo_fake->relocs[i];
+               drm_intel_bo_fake *target_fake =
+                   (drm_intel_bo_fake *) r->target_buf;
+               uint32_t reloc_data;
+
+               /* Validate the target buffer if that hasn't been done. */
+               if (!target_fake->validated) {
+                       ret =
+                           drm_intel_fake_reloc_and_validate_buffer(r->target_buf);
+                       if (ret != 0) {
+                               if (bo->virtual != NULL)
+                                       drm_intel_fake_bo_unmap_locked(bo);
+                               return ret;
+                       }
+               }
+
+               /* Calculate the value of the relocation entry. */
+               if (r->target_buf->offset != r->last_target_offset) {
+                       reloc_data = r->target_buf->offset + r->delta;
+
+                       if (bo->virtual == NULL)
+                               drm_intel_fake_bo_map_locked(bo, 1);
+
+                       *(uint32_t *) ((uint8_t *) bo->virtual + r->offset) =
+                           reloc_data;
+
+                       r->last_target_offset = r->target_buf->offset;
+               }
+       }
+
+       if (bo->virtual != NULL)
+               drm_intel_fake_bo_unmap_locked(bo);
+
+       if (bo_fake->write_domain != 0) {
+               if (!(bo_fake->flags & (BM_NO_BACKING_STORE | BM_PINNED))) {
+                       if (bo_fake->backing_store == 0)
+                               alloc_backing_store(bo);
+               }
+               bo_fake->card_dirty = 1;
+               bufmgr_fake->performed_rendering = 1;
+       }
+
+       return drm_intel_fake_bo_validate(bo);
+}
+
+static void
+drm_intel_bo_fake_post_submit(drm_intel_bo *bo)
+{
+       drm_intel_bufmgr_fake *bufmgr_fake =
+           (drm_intel_bufmgr_fake *) bo->bufmgr;
+       drm_intel_bo_fake *bo_fake = (drm_intel_bo_fake *) bo;
+       int i;
+
+       for (i = 0; i < bo_fake->nr_relocs; i++) {
+               struct fake_buffer_reloc *r = &bo_fake->relocs[i];
+               drm_intel_bo_fake *target_fake =
+                   (drm_intel_bo_fake *) r->target_buf;
+
+               if (target_fake->validated)
+                       drm_intel_bo_fake_post_submit(r->target_buf);
+
+               DBG("%s@0x%08x + 0x%08x -> %s@0x%08x + 0x%08x\n",
+                   bo_fake->name, (uint32_t) bo->offset, r->offset,
+                   target_fake->name, (uint32_t) r->target_buf->offset,
+                   r->delta);
+       }
+
+       assert(bo_fake->map_count == 0);
+       bo_fake->validated = 0;
+       bo_fake->read_domains = 0;
+       bo_fake->write_domain = 0;
+}
+
+void
+drm_intel_bufmgr_fake_set_exec_callback(drm_intel_bufmgr *bufmgr,
+                                            int (*exec) (drm_intel_bo *bo,
+                                                         unsigned int used,
+                                                         void *priv),
+                                            void *priv)
+{
+       drm_intel_bufmgr_fake *bufmgr_fake = (drm_intel_bufmgr_fake *) bufmgr;
+
+       bufmgr_fake->exec = exec;
+       bufmgr_fake->exec_priv = priv;
+}
+
+static int
+drm_intel_fake_bo_exec(drm_intel_bo *bo, int used,
+                      drm_clip_rect_t * cliprects, int num_cliprects, int DR4)
+{
+       drm_intel_bufmgr_fake *bufmgr_fake =
+           (drm_intel_bufmgr_fake *) bo->bufmgr;
+       drm_intel_bo_fake *batch_fake = (drm_intel_bo_fake *) bo;
+       struct drm_i915_batchbuffer batch;
+       int ret;
+       int retry_count = 0;
+
+       pthread_mutex_lock(&bufmgr_fake->lock);
+
+       bufmgr_fake->performed_rendering = 0;
+
+       drm_intel_fake_calculate_domains(bo);
+
+       batch_fake->read_domains = I915_GEM_DOMAIN_COMMAND;
+
+       /* we've ran out of RAM so blow the whole lot away and retry */
+restart:
+       ret = drm_intel_fake_reloc_and_validate_buffer(bo);
+       if (bufmgr_fake->fail == 1) {
+               if (retry_count == 0) {
+                       retry_count++;
+                       drm_intel_fake_kick_all_locked(bufmgr_fake);
+                       bufmgr_fake->fail = 0;
+                       goto restart;
+               } else          /* dump out the memory here */
+                       mmDumpMemInfo(bufmgr_fake->heap);
+       }
+
+       assert(ret == 0);
+
+       if (bufmgr_fake->exec != NULL) {
+               int ret = bufmgr_fake->exec(bo, used, bufmgr_fake->exec_priv);
+               if (ret != 0) {
+                       pthread_mutex_unlock(&bufmgr_fake->lock);
+                       return ret;
+               }
+       } else {
+               batch.start = bo->offset;
+               batch.used = used;
+               batch.cliprects = cliprects;
+               batch.num_cliprects = num_cliprects;
+               batch.DR1 = 0;
+               batch.DR4 = DR4;
+
+               if (drmCommandWrite
+                   (bufmgr_fake->fd, DRM_I915_BATCHBUFFER, &batch,
+                    sizeof(batch))) {
+                       drmMsg("DRM_I915_BATCHBUFFER: %d\n", -errno);
+                       pthread_mutex_unlock(&bufmgr_fake->lock);
+                       return -errno;
+               }
+       }
+
+       drm_intel_fake_fence_validated(bo->bufmgr);
+
+       drm_intel_bo_fake_post_submit(bo);
+
+       pthread_mutex_unlock(&bufmgr_fake->lock);
+
+       return 0;
+}
+
+/**
+ * Return an error if the list of BOs will exceed the aperture size.
+ *
+ * This is a rough guess and likely to fail, as during the validate sequence we
+ * may place a buffer in an inopportune spot early on and then fail to fit
+ * a set smaller than the aperture.
+ */
+static int
+drm_intel_fake_check_aperture_space(drm_intel_bo ** bo_array, int count)
+{
+       drm_intel_bufmgr_fake *bufmgr_fake =
+           (drm_intel_bufmgr_fake *) bo_array[0]->bufmgr;
+       unsigned int sz = 0;
+       int i;
+
+       for (i = 0; i < count; i++) {
+               drm_intel_bo_fake *bo_fake = (drm_intel_bo_fake *) bo_array[i];
+
+               if (bo_fake == NULL)
+                       continue;
+
+               if (!bo_fake->is_static)
+                       sz += ALIGN(bo_array[i]->size, bo_fake->alignment);
+               sz += bo_fake->child_size;
+       }
+
+       if (sz > bufmgr_fake->size) {
+               DBG("check_space: overflowed bufmgr size, %dkb vs %dkb\n",
+                   sz / 1024, bufmgr_fake->size / 1024);
+               return -1;
+       }
+
+       DBG("drm_check_space: sz %dkb vs bufgr %dkb\n", sz / 1024,
+           bufmgr_fake->size / 1024);
+       return 0;
+}
+
+/**
+ * Evicts all buffers, waiting for fences to pass and copying contents out
+ * as necessary.
+ *
+ * Used by the X Server on LeaveVT, when the card memory is no longer our
+ * own.
+ */
+void drm_intel_bufmgr_fake_evict_all(drm_intel_bufmgr *bufmgr)
+{
+       drm_intel_bufmgr_fake *bufmgr_fake = (drm_intel_bufmgr_fake *) bufmgr;
+       struct block *block, *tmp;
+
+       pthread_mutex_lock(&bufmgr_fake->lock);
+
+       bufmgr_fake->need_fence = 1;
+       bufmgr_fake->fail = 0;
+
+       /* Wait for hardware idle.  We don't know where acceleration has been
+        * happening, so we'll need to wait anyway before letting anything get
+        * put on the card again.
+        */
+       drm_intel_bufmgr_fake_wait_idle(bufmgr_fake);
+
+       /* Check that we hadn't released the lock without having fenced the last
+        * set of buffers.
+        */
+       assert(DRMLISTEMPTY(&bufmgr_fake->fenced));
+       assert(DRMLISTEMPTY(&bufmgr_fake->on_hardware));
+
+       DRMLISTFOREACHSAFE(block, tmp, &bufmgr_fake->lru) {
+               drm_intel_bo_fake *bo_fake = (drm_intel_bo_fake *) block->bo;
+               /* Releases the memory, and memcpys dirty contents out if
+                * necessary.
+                */
+               free_block(bufmgr_fake, block, 0);
+               bo_fake->block = NULL;
+       }
+
+       pthread_mutex_unlock(&bufmgr_fake->lock);
+}
+
+void drm_intel_bufmgr_fake_set_last_dispatch(drm_intel_bufmgr *bufmgr,
+                                            volatile unsigned int
+                                            *last_dispatch)
+{
+       drm_intel_bufmgr_fake *bufmgr_fake = (drm_intel_bufmgr_fake *) bufmgr;
+
+       bufmgr_fake->last_dispatch = (volatile int *)last_dispatch;
+}
+
+drm_intel_bufmgr *drm_intel_bufmgr_fake_init(int fd,
+                                            unsigned long low_offset,
+                                            void *low_virtual,
+                                            unsigned long size,
+                                            volatile unsigned int
+                                            *last_dispatch)
+{
+       drm_intel_bufmgr_fake *bufmgr_fake;
+
+       bufmgr_fake = calloc(1, sizeof(*bufmgr_fake));
+
+       if (pthread_mutex_init(&bufmgr_fake->lock, NULL) != 0) {
+               free(bufmgr_fake);
+               return NULL;
+       }
+
+       /* Initialize allocator */
+       DRMINITLISTHEAD(&bufmgr_fake->fenced);
+       DRMINITLISTHEAD(&bufmgr_fake->on_hardware);
+       DRMINITLISTHEAD(&bufmgr_fake->lru);
+
+       bufmgr_fake->low_offset = low_offset;
+       bufmgr_fake->virtual = low_virtual;
+       bufmgr_fake->size = size;
+       bufmgr_fake->heap = mmInit(low_offset, size);
+
+       /* Hook in methods */
+       bufmgr_fake->bufmgr.bo_alloc = drm_intel_fake_bo_alloc;
+       bufmgr_fake->bufmgr.bo_alloc_for_render = drm_intel_fake_bo_alloc;
+       bufmgr_fake->bufmgr.bo_alloc_tiled = drm_intel_fake_bo_alloc_tiled;
+       bufmgr_fake->bufmgr.bo_reference = drm_intel_fake_bo_reference;
+       bufmgr_fake->bufmgr.bo_unreference = drm_intel_fake_bo_unreference;
+       bufmgr_fake->bufmgr.bo_map = drm_intel_fake_bo_map;
+       bufmgr_fake->bufmgr.bo_unmap = drm_intel_fake_bo_unmap;
+       bufmgr_fake->bufmgr.bo_subdata = drm_intel_fake_bo_subdata;
+       bufmgr_fake->bufmgr.bo_wait_rendering =
+           drm_intel_fake_bo_wait_rendering;
+       bufmgr_fake->bufmgr.bo_emit_reloc = drm_intel_fake_emit_reloc;
+       bufmgr_fake->bufmgr.destroy = drm_intel_fake_destroy;
+       bufmgr_fake->bufmgr.bo_exec = drm_intel_fake_bo_exec;
+       bufmgr_fake->bufmgr.check_aperture_space =
+           drm_intel_fake_check_aperture_space;
+       bufmgr_fake->bufmgr.debug = 0;
+
+       bufmgr_fake->fd = fd;
+       bufmgr_fake->last_dispatch = (volatile int *)last_dispatch;
+
+       return &bufmgr_fake->bufmgr;
+}
diff --git a/intel/intel_bufmgr_gem.c b/intel/intel_bufmgr_gem.c
new file mode 100644 (file)
index 0000000..dd58c0c
--- /dev/null
@@ -0,0 +1,2300 @@
+/**************************************************************************
+ *
+ * Copyright Â© 2007 Red Hat Inc.
+ * Copyright Â© 2007 Intel Corporation
+ * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+ * USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ *
+ **************************************************************************/
+/*
+ * Authors: Thomas Hellström <thomas-at-tungstengraphics-dot-com>
+ *          Keith Whitwell <keithw-at-tungstengraphics-dot-com>
+ *         Eric Anholt <eric@anholt.net>
+ *         Dave Airlie <airlied@linux.ie>
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <xf86drm.h>
+#include <xf86atomic.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <assert.h>
+#include <pthread.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <stdbool.h>
+
+#include "errno.h"
+#include "libdrm_lists.h"
+#include "intel_bufmgr.h"
+#include "intel_bufmgr_priv.h"
+#include "intel_chipset.h"
+#include "string.h"
+
+#include "i915_drm.h"
+
+#define DBG(...) do {                                  \
+       if (bufmgr_gem->bufmgr.debug)                   \
+               fprintf(stderr, __VA_ARGS__);           \
+} while (0)
+
+#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
+
+typedef struct _drm_intel_bo_gem drm_intel_bo_gem;
+
+struct drm_intel_gem_bo_bucket {
+       drmMMListHead head;
+       unsigned long size;
+};
+
+typedef struct _drm_intel_bufmgr_gem {
+       drm_intel_bufmgr bufmgr;
+
+       int fd;
+
+       int max_relocs;
+
+       pthread_mutex_t lock;
+
+       struct drm_i915_gem_exec_object *exec_objects;
+       struct drm_i915_gem_exec_object2 *exec2_objects;
+       drm_intel_bo **exec_bos;
+       int exec_size;
+       int exec_count;
+
+       /** Array of lists of cached gem objects of power-of-two sizes */
+       struct drm_intel_gem_bo_bucket cache_bucket[14 * 4];
+       int num_buckets;
+       time_t time;
+
+       drmMMListHead named;
+
+       uint64_t gtt_size;
+       int available_fences;
+       int pci_device;
+       int gen;
+       unsigned int has_bsd : 1;
+       unsigned int has_blt : 1;
+       unsigned int has_relaxed_fencing : 1;
+       unsigned int bo_reuse : 1;
+       bool fenced_relocs;
+} drm_intel_bufmgr_gem;
+
+#define DRM_INTEL_RELOC_FENCE (1<<0)
+
+typedef struct _drm_intel_reloc_target_info {
+       drm_intel_bo *bo;
+       int flags;
+} drm_intel_reloc_target;
+
+struct _drm_intel_bo_gem {
+       drm_intel_bo bo;
+
+       atomic_t refcount;
+       uint32_t gem_handle;
+       const char *name;
+
+       /**
+        * Kenel-assigned global name for this object
+        */
+       unsigned int global_name;
+       drmMMListHead name_list;
+
+       /**
+        * Index of the buffer within the validation list while preparing a
+        * batchbuffer execution.
+        */
+       int validate_index;
+
+       /**
+        * Current tiling mode
+        */
+       uint32_t tiling_mode;
+       uint32_t swizzle_mode;
+       unsigned long stride;
+
+       time_t free_time;
+
+       /** Array passed to the DRM containing relocation information. */
+       struct drm_i915_gem_relocation_entry *relocs;
+       /**
+        * Array of info structs corresponding to relocs[i].target_handle etc
+        */
+       drm_intel_reloc_target *reloc_target_info;
+       /** Number of entries in relocs */
+       int reloc_count;
+       /** Mapped address for the buffer, saved across map/unmap cycles */
+       void *mem_virtual;
+       /** GTT virtual address for the buffer, saved across map/unmap cycles */
+       void *gtt_virtual;
+
+       /** BO cache list */
+       drmMMListHead head;
+
+       /**
+        * Boolean of whether this BO and its children have been included in
+        * the current drm_intel_bufmgr_check_aperture_space() total.
+        */
+       bool included_in_check_aperture;
+
+       /**
+        * Boolean of whether this buffer has been used as a relocation
+        * target and had its size accounted for, and thus can't have any
+        * further relocations added to it.
+        */
+       bool used_as_reloc_target;
+
+       /**
+        * Boolean of whether we have encountered an error whilst building the relocation tree.
+        */
+       bool has_error;
+
+       /**
+        * Boolean of whether this buffer can be re-used
+        */
+       bool reusable;
+
+       /**
+        * Size in bytes of this buffer and its relocation descendents.
+        *
+        * Used to avoid costly tree walking in
+        * drm_intel_bufmgr_check_aperture in the common case.
+        */
+       int reloc_tree_size;
+
+       /**
+        * Number of potential fence registers required by this buffer and its
+        * relocations.
+        */
+       int reloc_tree_fences;
+
+       /** Flags that we may need to do the SW_FINSIH ioctl on unmap. */
+       bool mapped_cpu_write;
+};
+
+static unsigned int
+drm_intel_gem_estimate_batch_space(drm_intel_bo ** bo_array, int count);
+
+static unsigned int
+drm_intel_gem_compute_batch_space(drm_intel_bo ** bo_array, int count);
+
+static int
+drm_intel_gem_bo_get_tiling(drm_intel_bo *bo, uint32_t * tiling_mode,
+                           uint32_t * swizzle_mode);
+
+static int
+drm_intel_gem_bo_set_tiling_internal(drm_intel_bo *bo,
+                                    uint32_t tiling_mode,
+                                    uint32_t stride);
+
+static void drm_intel_gem_bo_unreference_locked_timed(drm_intel_bo *bo,
+                                                     time_t time);
+
+static void drm_intel_gem_bo_unreference(drm_intel_bo *bo);
+
+static void drm_intel_gem_bo_free(drm_intel_bo *bo);
+
+static unsigned long
+drm_intel_gem_bo_tile_size(drm_intel_bufmgr_gem *bufmgr_gem, unsigned long size,
+                          uint32_t *tiling_mode)
+{
+       unsigned long min_size, max_size;
+       unsigned long i;
+
+       if (*tiling_mode == I915_TILING_NONE)
+               return size;
+
+       /* 965+ just need multiples of page size for tiling */
+       if (bufmgr_gem->gen >= 4)
+               return ROUND_UP_TO(size, 4096);
+
+       /* Older chips need powers of two, of at least 512k or 1M */
+       if (bufmgr_gem->gen == 3) {
+               min_size = 1024*1024;
+               max_size = 128*1024*1024;
+       } else {
+               min_size = 512*1024;
+               max_size = 64*1024*1024;
+       }
+
+       if (size > max_size) {
+               *tiling_mode = I915_TILING_NONE;
+               return size;
+       }
+
+       /* Do we need to allocate every page for the fence? */
+       if (bufmgr_gem->has_relaxed_fencing)
+               return ROUND_UP_TO(size, 4096);
+
+       for (i = min_size; i < size; i <<= 1)
+               ;
+
+       return i;
+}
+
+/*
+ * Round a given pitch up to the minimum required for X tiling on a
+ * given chip.  We use 512 as the minimum to allow for a later tiling
+ * change.
+ */
+static unsigned long
+drm_intel_gem_bo_tile_pitch(drm_intel_bufmgr_gem *bufmgr_gem,
+                           unsigned long pitch, uint32_t *tiling_mode)
+{
+       unsigned long tile_width;
+       unsigned long i;
+
+       /* If untiled, then just align it so that we can do rendering
+        * to it with the 3D engine.
+        */
+       if (*tiling_mode == I915_TILING_NONE)
+               return ALIGN(pitch, 64);
+
+       if (*tiling_mode == I915_TILING_X
+                       || (IS_915(bufmgr_gem) && *tiling_mode == I915_TILING_Y))
+               tile_width = 512;
+       else
+               tile_width = 128;
+
+       /* 965 is flexible */
+       if (bufmgr_gem->gen >= 4)
+               return ROUND_UP_TO(pitch, tile_width);
+
+       /* The older hardware has a maximum pitch of 8192 with tiled
+        * surfaces, so fallback to untiled if it's too large.
+        */
+       if (pitch > 8192) {
+               *tiling_mode = I915_TILING_NONE;
+               return ALIGN(pitch, 64);
+       }
+
+       /* Pre-965 needs power of two tile width */
+       for (i = tile_width; i < pitch; i <<= 1)
+               ;
+
+       return i;
+}
+
+static struct drm_intel_gem_bo_bucket *
+drm_intel_gem_bo_bucket_for_size(drm_intel_bufmgr_gem *bufmgr_gem,
+                                unsigned long size)
+{
+       int i;
+
+       for (i = 0; i < bufmgr_gem->num_buckets; i++) {
+               struct drm_intel_gem_bo_bucket *bucket =
+                   &bufmgr_gem->cache_bucket[i];
+               if (bucket->size >= size) {
+                       return bucket;
+               }
+       }
+
+       return NULL;
+}
+
+static void
+drm_intel_gem_dump_validation_list(drm_intel_bufmgr_gem *bufmgr_gem)
+{
+       int i, j;
+
+       for (i = 0; i < bufmgr_gem->exec_count; i++) {
+               drm_intel_bo *bo = bufmgr_gem->exec_bos[i];
+               drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
+
+               if (bo_gem->relocs == NULL) {
+                       DBG("%2d: %d (%s)\n", i, bo_gem->gem_handle,
+                           bo_gem->name);
+                       continue;
+               }
+
+               for (j = 0; j < bo_gem->reloc_count; j++) {
+                       drm_intel_bo *target_bo = bo_gem->reloc_target_info[j].bo;
+                       drm_intel_bo_gem *target_gem =
+                           (drm_intel_bo_gem *) target_bo;
+
+                       DBG("%2d: %d (%s)@0x%08llx -> "
+                           "%d (%s)@0x%08lx + 0x%08x\n",
+                           i,
+                           bo_gem->gem_handle, bo_gem->name,
+                           (unsigned long long)bo_gem->relocs[j].offset,
+                           target_gem->gem_handle,
+                           target_gem->name,
+                           target_bo->offset,
+                           bo_gem->relocs[j].delta);
+               }
+       }
+}
+
+static inline void
+drm_intel_gem_bo_reference(drm_intel_bo *bo)
+{
+       drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
+
+       atomic_inc(&bo_gem->refcount);
+}
+
+/**
+ * Adds the given buffer to the list of buffers to be validated (moved into the
+ * appropriate memory type) with the next batch submission.
+ *
+ * If a buffer is validated multiple times in a batch submission, it ends up
+ * with the intersection of the memory type flags and the union of the
+ * access flags.
+ */
+static void
+drm_intel_add_validate_buffer(drm_intel_bo *bo)
+{
+       drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
+       drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
+       int index;
+
+       if (bo_gem->validate_index != -1)
+               return;
+
+       /* Extend the array of validation entries as necessary. */
+       if (bufmgr_gem->exec_count == bufmgr_gem->exec_size) {
+               int new_size = bufmgr_gem->exec_size * 2;
+
+               if (new_size == 0)
+                       new_size = 5;
+
+               bufmgr_gem->exec_objects =
+                   realloc(bufmgr_gem->exec_objects,
+                           sizeof(*bufmgr_gem->exec_objects) * new_size);
+               bufmgr_gem->exec_bos =
+                   realloc(bufmgr_gem->exec_bos,
+                           sizeof(*bufmgr_gem->exec_bos) * new_size);
+               bufmgr_gem->exec_size = new_size;
+       }
+
+       index = bufmgr_gem->exec_count;
+       bo_gem->validate_index = index;
+       /* Fill in array entry */
+       bufmgr_gem->exec_objects[index].handle = bo_gem->gem_handle;
+       bufmgr_gem->exec_objects[index].relocation_count = bo_gem->reloc_count;
+       bufmgr_gem->exec_objects[index].relocs_ptr = (uintptr_t) bo_gem->relocs;
+       bufmgr_gem->exec_objects[index].alignment = 0;
+       bufmgr_gem->exec_objects[index].offset = 0;
+       bufmgr_gem->exec_bos[index] = bo;
+       bufmgr_gem->exec_count++;
+}
+
+static void
+drm_intel_add_validate_buffer2(drm_intel_bo *bo, int need_fence)
+{
+       drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bo->bufmgr;
+       drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *)bo;
+       int index;
+
+       if (bo_gem->validate_index != -1) {
+               if (need_fence)
+                       bufmgr_gem->exec2_objects[bo_gem->validate_index].flags |=
+                               EXEC_OBJECT_NEEDS_FENCE;
+               return;
+       }
+
+       /* Extend the array of validation entries as necessary. */
+       if (bufmgr_gem->exec_count == bufmgr_gem->exec_size) {
+               int new_size = bufmgr_gem->exec_size * 2;
+
+               if (new_size == 0)
+                       new_size = 5;
+
+               bufmgr_gem->exec2_objects =
+                       realloc(bufmgr_gem->exec2_objects,
+                               sizeof(*bufmgr_gem->exec2_objects) * new_size);
+               bufmgr_gem->exec_bos =
+                       realloc(bufmgr_gem->exec_bos,
+                               sizeof(*bufmgr_gem->exec_bos) * new_size);
+               bufmgr_gem->exec_size = new_size;
+       }
+
+       index = bufmgr_gem->exec_count;
+       bo_gem->validate_index = index;
+       /* Fill in array entry */
+       bufmgr_gem->exec2_objects[index].handle = bo_gem->gem_handle;
+       bufmgr_gem->exec2_objects[index].relocation_count = bo_gem->reloc_count;
+       bufmgr_gem->exec2_objects[index].relocs_ptr = (uintptr_t)bo_gem->relocs;
+       bufmgr_gem->exec2_objects[index].alignment = 0;
+       bufmgr_gem->exec2_objects[index].offset = 0;
+       bufmgr_gem->exec_bos[index] = bo;
+       bufmgr_gem->exec2_objects[index].flags = 0;
+       bufmgr_gem->exec2_objects[index].rsvd1 = 0;
+       bufmgr_gem->exec2_objects[index].rsvd2 = 0;
+       if (need_fence) {
+               bufmgr_gem->exec2_objects[index].flags |=
+                       EXEC_OBJECT_NEEDS_FENCE;
+       }
+       bufmgr_gem->exec_count++;
+}
+
+#define RELOC_BUF_SIZE(x) ((I915_RELOC_HEADER + x * I915_RELOC0_STRIDE) * \
+       sizeof(uint32_t))
+
+static void
+drm_intel_bo_gem_set_in_aperture_size(drm_intel_bufmgr_gem *bufmgr_gem,
+                                     drm_intel_bo_gem *bo_gem)
+{
+       int size;
+
+       assert(!bo_gem->used_as_reloc_target);
+
+       /* The older chipsets are far-less flexible in terms of tiling,
+        * and require tiled buffer to be size aligned in the aperture.
+        * This means that in the worst possible case we will need a hole
+        * twice as large as the object in order for it to fit into the
+        * aperture. Optimal packing is for wimps.
+        */
+       size = bo_gem->bo.size;
+       if (bufmgr_gem->gen < 4 && bo_gem->tiling_mode != I915_TILING_NONE) {
+               int min_size;
+
+               if (bufmgr_gem->has_relaxed_fencing) {
+                       if (bufmgr_gem->gen == 3)
+                               min_size = 1024*1024;
+                       else
+                               min_size = 512*1024;
+
+                       while (min_size < size)
+                               min_size *= 2;
+               } else
+                       min_size = size;
+
+               /* Account for worst-case alignment. */
+               size = 2 * min_size;
+       }
+
+       bo_gem->reloc_tree_size = size;
+}
+
+static int
+drm_intel_setup_reloc_list(drm_intel_bo *bo)
+{
+       drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
+       drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
+       unsigned int max_relocs = bufmgr_gem->max_relocs;
+
+       if (bo->size / 4 < max_relocs)
+               max_relocs = bo->size / 4;
+
+       bo_gem->relocs = malloc(max_relocs *
+                               sizeof(struct drm_i915_gem_relocation_entry));
+       bo_gem->reloc_target_info = malloc(max_relocs *
+                                          sizeof(drm_intel_reloc_target));
+       if (bo_gem->relocs == NULL || bo_gem->reloc_target_info == NULL) {
+               bo_gem->has_error = true;
+
+               free (bo_gem->relocs);
+               bo_gem->relocs = NULL;
+
+               free (bo_gem->reloc_target_info);
+               bo_gem->reloc_target_info = NULL;
+
+               return 1;
+       }
+
+       return 0;
+}
+
+static int
+drm_intel_gem_bo_busy(drm_intel_bo *bo)
+{
+       drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
+       drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
+       struct drm_i915_gem_busy busy;
+       int ret;
+
+       memset(&busy, 0, sizeof(busy));
+       busy.handle = bo_gem->gem_handle;
+
+       ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_BUSY, &busy);
+
+       return (ret == 0 && busy.busy);
+}
+
+static int
+drm_intel_gem_bo_madvise_internal(drm_intel_bufmgr_gem *bufmgr_gem,
+                                 drm_intel_bo_gem *bo_gem, int state)
+{
+       struct drm_i915_gem_madvise madv;
+
+       madv.handle = bo_gem->gem_handle;
+       madv.madv = state;
+       madv.retained = 1;
+       drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_MADVISE, &madv);
+
+       return madv.retained;
+}
+
+static int
+drm_intel_gem_bo_madvise(drm_intel_bo *bo, int madv)
+{
+       return drm_intel_gem_bo_madvise_internal
+               ((drm_intel_bufmgr_gem *) bo->bufmgr,
+                (drm_intel_bo_gem *) bo,
+                madv);
+}
+
+/* drop the oldest entries that have been purged by the kernel */
+static void
+drm_intel_gem_bo_cache_purge_bucket(drm_intel_bufmgr_gem *bufmgr_gem,
+                                   struct drm_intel_gem_bo_bucket *bucket)
+{
+       while (!DRMLISTEMPTY(&bucket->head)) {
+               drm_intel_bo_gem *bo_gem;
+
+               bo_gem = DRMLISTENTRY(drm_intel_bo_gem,
+                                     bucket->head.next, head);
+               if (drm_intel_gem_bo_madvise_internal
+                   (bufmgr_gem, bo_gem, I915_MADV_DONTNEED))
+                       break;
+
+               DRMLISTDEL(&bo_gem->head);
+               drm_intel_gem_bo_free(&bo_gem->bo);
+       }
+}
+
+static drm_intel_bo *
+drm_intel_gem_bo_alloc_internal(drm_intel_bufmgr *bufmgr,
+                               const char *name,
+                               unsigned long size,
+                               unsigned long flags,
+                               uint32_t tiling_mode,
+                               unsigned long stride)
+{
+       drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr;
+       drm_intel_bo_gem *bo_gem;
+       unsigned int page_size = getpagesize();
+       int ret;
+       struct drm_intel_gem_bo_bucket *bucket;
+       bool alloc_from_cache;
+       unsigned long bo_size;
+       bool for_render = false;
+
+       if (flags & BO_ALLOC_FOR_RENDER)
+               for_render = true;
+
+       /* Round the allocated size up to a power of two number of pages. */
+       bucket = drm_intel_gem_bo_bucket_for_size(bufmgr_gem, size);
+
+       /* If we don't have caching at this size, don't actually round the
+        * allocation up.
+        */
+       if (bucket == NULL) {
+               bo_size = size;
+               if (bo_size < page_size)
+                       bo_size = page_size;
+       } else {
+               bo_size = bucket->size;
+       }
+
+       pthread_mutex_lock(&bufmgr_gem->lock);
+       /* Get a buffer out of the cache if available */
+retry:
+       alloc_from_cache = false;
+       if (bucket != NULL && !DRMLISTEMPTY(&bucket->head)) {
+               if (for_render) {
+                       /* Allocate new render-target BOs from the tail (MRU)
+                        * of the list, as it will likely be hot in the GPU
+                        * cache and in the aperture for us.
+                        */
+                       bo_gem = DRMLISTENTRY(drm_intel_bo_gem,
+                                             bucket->head.prev, head);
+                       DRMLISTDEL(&bo_gem->head);
+                       alloc_from_cache = true;
+               } else {
+                       /* For non-render-target BOs (where we're probably
+                        * going to map it first thing in order to fill it
+                        * with data), check if the last BO in the cache is
+                        * unbusy, and only reuse in that case. Otherwise,
+                        * allocating a new buffer is probably faster than
+                        * waiting for the GPU to finish.
+                        */
+                       bo_gem = DRMLISTENTRY(drm_intel_bo_gem,
+                                             bucket->head.next, head);
+                       if (!drm_intel_gem_bo_busy(&bo_gem->bo)) {
+                               alloc_from_cache = true;
+                               DRMLISTDEL(&bo_gem->head);
+                       }
+               }
+
+               if (alloc_from_cache) {
+                       if (!drm_intel_gem_bo_madvise_internal
+                           (bufmgr_gem, bo_gem, I915_MADV_WILLNEED)) {
+                               drm_intel_gem_bo_free(&bo_gem->bo);
+                               drm_intel_gem_bo_cache_purge_bucket(bufmgr_gem,
+                                                                   bucket);
+                               goto retry;
+                       }
+
+                       if (drm_intel_gem_bo_set_tiling_internal(&bo_gem->bo,
+                                                                tiling_mode,
+                                                                stride)) {
+                               drm_intel_gem_bo_free(&bo_gem->bo);
+                               goto retry;
+                       }
+               }
+       }
+       pthread_mutex_unlock(&bufmgr_gem->lock);
+
+       if (!alloc_from_cache) {
+               struct drm_i915_gem_create create;
+
+               bo_gem = calloc(1, sizeof(*bo_gem));
+               if (!bo_gem)
+                       return NULL;
+
+               bo_gem->bo.size = bo_size;
+               memset(&create, 0, sizeof(create));
+               create.size = bo_size;
+
+               ret = drmIoctl(bufmgr_gem->fd,
+                              DRM_IOCTL_I915_GEM_CREATE,
+                              &create);
+               bo_gem->gem_handle = create.handle;
+               bo_gem->bo.handle = bo_gem->gem_handle;
+               if (ret != 0) {
+                       free(bo_gem);
+                       return NULL;
+               }
+               bo_gem->bo.bufmgr = bufmgr;
+
+               bo_gem->tiling_mode = I915_TILING_NONE;
+               bo_gem->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
+               bo_gem->stride = 0;
+
+               if (drm_intel_gem_bo_set_tiling_internal(&bo_gem->bo,
+                                                        tiling_mode,
+                                                        stride)) {
+                   drm_intel_gem_bo_free(&bo_gem->bo);
+                   return NULL;
+               }
+
+               DRMINITLISTHEAD(&bo_gem->name_list);
+       }
+
+       bo_gem->name = name;
+       atomic_set(&bo_gem->refcount, 1);
+       bo_gem->validate_index = -1;
+       bo_gem->reloc_tree_fences = 0;
+       bo_gem->used_as_reloc_target = false;
+       bo_gem->has_error = false;
+       bo_gem->reusable = true;
+
+       drm_intel_bo_gem_set_in_aperture_size(bufmgr_gem, bo_gem);
+
+       DBG("bo_create: buf %d (%s) %ldb\n",
+           bo_gem->gem_handle, bo_gem->name, size);
+
+       return &bo_gem->bo;
+}
+
+static drm_intel_bo *
+drm_intel_gem_bo_alloc_for_render(drm_intel_bufmgr *bufmgr,
+                                 const char *name,
+                                 unsigned long size,
+                                 unsigned int alignment)
+{
+       return drm_intel_gem_bo_alloc_internal(bufmgr, name, size,
+                                              BO_ALLOC_FOR_RENDER,
+                                              I915_TILING_NONE, 0);
+}
+
+static drm_intel_bo *
+drm_intel_gem_bo_alloc(drm_intel_bufmgr *bufmgr,
+                      const char *name,
+                      unsigned long size,
+                      unsigned int alignment)
+{
+       return drm_intel_gem_bo_alloc_internal(bufmgr, name, size, 0,
+                                              I915_TILING_NONE, 0);
+}
+
+static drm_intel_bo *
+drm_intel_gem_bo_alloc_tiled(drm_intel_bufmgr *bufmgr, const char *name,
+                            int x, int y, int cpp, uint32_t *tiling_mode,
+                            unsigned long *pitch, unsigned long flags)
+{
+       drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bufmgr;
+       unsigned long size, stride;
+       uint32_t tiling;
+
+       do {
+               unsigned long aligned_y, height_alignment;
+
+               tiling = *tiling_mode;
+
+               /* If we're tiled, our allocations are in 8 or 32-row blocks,
+                * so failure to align our height means that we won't allocate
+                * enough pages.
+                *
+                * If we're untiled, we still have to align to 2 rows high
+                * because the data port accesses 2x2 blocks even if the
+                * bottom row isn't to be rendered, so failure to align means
+                * we could walk off the end of the GTT and fault.  This is
+                * documented on 965, and may be the case on older chipsets
+                * too so we try to be careful.
+                */
+               aligned_y = y;
+               height_alignment = 2;
+
+               if (IS_GEN2(bufmgr_gem) && tiling != I915_TILING_NONE)
+                       height_alignment = 16;
+               else if (tiling == I915_TILING_X
+                       || (IS_915(bufmgr_gem) && tiling == I915_TILING_Y))
+                       height_alignment = 8;
+               else if (tiling == I915_TILING_Y)
+                       height_alignment = 32;
+               aligned_y = ALIGN(y, height_alignment);
+
+               stride = x * cpp;
+               stride = drm_intel_gem_bo_tile_pitch(bufmgr_gem, stride, tiling_mode);
+               size = stride * aligned_y;
+               size = drm_intel_gem_bo_tile_size(bufmgr_gem, size, tiling_mode);
+       } while (*tiling_mode != tiling);
+       *pitch = stride;
+
+       if (tiling == I915_TILING_NONE)
+               stride = 0;
+
+       return drm_intel_gem_bo_alloc_internal(bufmgr, name, size, flags,
+                                              tiling, stride);
+}
+
+/**
+ * Returns a drm_intel_bo wrapping the given buffer object handle.
+ *
+ * This can be used when one application needs to pass a buffer object
+ * to another.
+ */
+drm_intel_bo *
+drm_intel_bo_gem_create_from_name(drm_intel_bufmgr *bufmgr,
+                                 const char *name,
+                                 unsigned int handle)
+{
+       drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr;
+       drm_intel_bo_gem *bo_gem;
+       int ret;
+       struct drm_gem_open open_arg;
+       struct drm_i915_gem_get_tiling get_tiling;
+       drmMMListHead *list;
+
+       /* At the moment most applications only have a few named bo.
+        * For instance, in a DRI client only the render buffers passed
+        * between X and the client are named. And since X returns the
+        * alternating names for the front/back buffer a linear search
+        * provides a sufficiently fast match.
+        */
+       for (list = bufmgr_gem->named.next;
+            list != &bufmgr_gem->named;
+            list = list->next) {
+               bo_gem = DRMLISTENTRY(drm_intel_bo_gem, list, name_list);
+               if (bo_gem->global_name == handle) {
+                       drm_intel_gem_bo_reference(&bo_gem->bo);
+                       return &bo_gem->bo;
+               }
+       }
+
+       bo_gem = calloc(1, sizeof(*bo_gem));
+       if (!bo_gem)
+               return NULL;
+
+       memset(&open_arg, 0, sizeof(open_arg));
+       open_arg.name = handle;
+       ret = drmIoctl(bufmgr_gem->fd,
+                      DRM_IOCTL_GEM_OPEN,
+                      &open_arg);
+       if (ret != 0) {
+               DBG("Couldn't reference %s handle 0x%08x: %s\n",
+                   name, handle, strerror(errno));
+               free(bo_gem);
+               return NULL;
+       }
+       bo_gem->bo.size = open_arg.size;
+       bo_gem->bo.offset = 0;
+       bo_gem->bo.virtual = NULL;
+       bo_gem->bo.bufmgr = bufmgr;
+       bo_gem->name = name;
+       atomic_set(&bo_gem->refcount, 1);
+       bo_gem->validate_index = -1;
+       bo_gem->gem_handle = open_arg.handle;
+       bo_gem->bo.handle = open_arg.handle;
+       bo_gem->global_name = handle;
+       bo_gem->reusable = false;
+
+       memset(&get_tiling, 0, sizeof(get_tiling));
+       get_tiling.handle = bo_gem->gem_handle;
+       ret = drmIoctl(bufmgr_gem->fd,
+                      DRM_IOCTL_I915_GEM_GET_TILING,
+                      &get_tiling);
+       if (ret != 0) {
+               drm_intel_gem_bo_unreference(&bo_gem->bo);
+               return NULL;
+       }
+       bo_gem->tiling_mode = get_tiling.tiling_mode;
+       bo_gem->swizzle_mode = get_tiling.swizzle_mode;
+       /* XXX stride is unknown */
+       drm_intel_bo_gem_set_in_aperture_size(bufmgr_gem, bo_gem);
+
+       DRMLISTADDTAIL(&bo_gem->name_list, &bufmgr_gem->named);
+       DBG("bo_create_from_handle: %d (%s)\n", handle, bo_gem->name);
+
+       return &bo_gem->bo;
+}
+
+static void
+drm_intel_gem_bo_free(drm_intel_bo *bo)
+{
+       drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
+       drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
+       struct drm_gem_close close;
+       int ret;
+
+       if (bo_gem->mem_virtual)
+               munmap(bo_gem->mem_virtual, bo_gem->bo.size);
+       if (bo_gem->gtt_virtual)
+               munmap(bo_gem->gtt_virtual, bo_gem->bo.size);
+
+       /* Close this object */
+       memset(&close, 0, sizeof(close));
+       close.handle = bo_gem->gem_handle;
+       ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_GEM_CLOSE, &close);
+       if (ret != 0) {
+               DBG("DRM_IOCTL_GEM_CLOSE %d failed (%s): %s\n",
+                   bo_gem->gem_handle, bo_gem->name, strerror(errno));
+       }
+       free(bo);
+}
+
+/** Frees all cached buffers significantly older than @time. */
+static void
+drm_intel_gem_cleanup_bo_cache(drm_intel_bufmgr_gem *bufmgr_gem, time_t time)
+{
+       int i;
+
+       if (bufmgr_gem->time == time)
+               return;
+
+       for (i = 0; i < bufmgr_gem->num_buckets; i++) {
+               struct drm_intel_gem_bo_bucket *bucket =
+                   &bufmgr_gem->cache_bucket[i];
+
+               while (!DRMLISTEMPTY(&bucket->head)) {
+                       drm_intel_bo_gem *bo_gem;
+
+                       bo_gem = DRMLISTENTRY(drm_intel_bo_gem,
+                                             bucket->head.next, head);
+                       if (time - bo_gem->free_time <= 1)
+                               break;
+
+                       DRMLISTDEL(&bo_gem->head);
+
+                       drm_intel_gem_bo_free(&bo_gem->bo);
+               }
+       }
+
+       bufmgr_gem->time = time;
+}
+
+static void
+drm_intel_gem_bo_unreference_final(drm_intel_bo *bo, time_t time)
+{
+       drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
+       drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
+       struct drm_intel_gem_bo_bucket *bucket;
+       int i;
+
+       /* Unreference all the target buffers */
+       for (i = 0; i < bo_gem->reloc_count; i++) {
+               if (bo_gem->reloc_target_info[i].bo != bo) {
+                       drm_intel_gem_bo_unreference_locked_timed(bo_gem->
+                                                                 reloc_target_info[i].bo,
+                                                                 time);
+               }
+       }
+       bo_gem->reloc_count = 0;
+       bo_gem->used_as_reloc_target = false;
+
+       DBG("bo_unreference final: %d (%s)\n",
+           bo_gem->gem_handle, bo_gem->name);
+
+       /* release memory associated with this object */
+       if (bo_gem->reloc_target_info) {
+               free(bo_gem->reloc_target_info);
+               bo_gem->reloc_target_info = NULL;
+       }
+       if (bo_gem->relocs) {
+               free(bo_gem->relocs);
+               bo_gem->relocs = NULL;
+       }
+
+       DRMLISTDEL(&bo_gem->name_list);
+
+       bucket = drm_intel_gem_bo_bucket_for_size(bufmgr_gem, bo->size);
+       /* Put the buffer into our internal cache for reuse if we can. */
+       if (bufmgr_gem->bo_reuse && bo_gem->reusable && bucket != NULL &&
+           drm_intel_gem_bo_madvise_internal(bufmgr_gem, bo_gem,
+                                             I915_MADV_DONTNEED)) {
+               bo_gem->free_time = time;
+
+               bo_gem->name = NULL;
+               bo_gem->validate_index = -1;
+
+               DRMLISTADDTAIL(&bo_gem->head, &bucket->head);
+       } else {
+               drm_intel_gem_bo_free(bo);
+       }
+}
+
+static void drm_intel_gem_bo_unreference_locked_timed(drm_intel_bo *bo,
+                                                     time_t time)
+{
+       drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
+
+       assert(atomic_read(&bo_gem->refcount) > 0);
+       if (atomic_dec_and_test(&bo_gem->refcount))
+               drm_intel_gem_bo_unreference_final(bo, time);
+}
+
+static void drm_intel_gem_bo_unreference(drm_intel_bo *bo)
+{
+       drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
+
+       assert(atomic_read(&bo_gem->refcount) > 0);
+       if (atomic_dec_and_test(&bo_gem->refcount)) {
+               drm_intel_bufmgr_gem *bufmgr_gem =
+                   (drm_intel_bufmgr_gem *) bo->bufmgr;
+               struct timespec time;
+
+               clock_gettime(CLOCK_MONOTONIC, &time);
+
+               pthread_mutex_lock(&bufmgr_gem->lock);
+               drm_intel_gem_bo_unreference_final(bo, time.tv_sec);
+               drm_intel_gem_cleanup_bo_cache(bufmgr_gem, time.tv_sec);
+               pthread_mutex_unlock(&bufmgr_gem->lock);
+       }
+}
+
+static int drm_intel_gem_bo_map(drm_intel_bo *bo, int write_enable)
+{
+       drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
+       drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
+       struct drm_i915_gem_set_domain set_domain;
+       int ret;
+
+       pthread_mutex_lock(&bufmgr_gem->lock);
+
+       if (!bo_gem->mem_virtual) {
+               struct drm_i915_gem_mmap mmap_arg;
+
+               DBG("bo_map: %d (%s)\n", bo_gem->gem_handle, bo_gem->name);
+
+               memset(&mmap_arg, 0, sizeof(mmap_arg));
+               mmap_arg.handle = bo_gem->gem_handle;
+               mmap_arg.offset = 0;
+               mmap_arg.size = bo->size;
+               ret = drmIoctl(bufmgr_gem->fd,
+                              DRM_IOCTL_I915_GEM_MMAP,
+                              &mmap_arg);
+               if (ret != 0) {
+                       ret = -errno;
+                       DBG("%s:%d: Error mapping buffer %d (%s): %s .\n",
+                           __FILE__, __LINE__, bo_gem->gem_handle,
+                           bo_gem->name, strerror(errno));
+                       pthread_mutex_unlock(&bufmgr_gem->lock);
+                       return ret;
+               }
+               bo_gem->mem_virtual = (void *)(uintptr_t) mmap_arg.addr_ptr;
+       }
+       DBG("bo_map: %d (%s) -> %p\n", bo_gem->gem_handle, bo_gem->name,
+           bo_gem->mem_virtual);
+       bo->virtual = bo_gem->mem_virtual;
+
+       set_domain.handle = bo_gem->gem_handle;
+       set_domain.read_domains = I915_GEM_DOMAIN_CPU;
+       if (write_enable)
+               set_domain.write_domain = I915_GEM_DOMAIN_CPU;
+       else
+               set_domain.write_domain = 0;
+       ret = drmIoctl(bufmgr_gem->fd,
+                      DRM_IOCTL_I915_GEM_SET_DOMAIN,
+                      &set_domain);
+       if (ret != 0) {
+               DBG("%s:%d: Error setting to CPU domain %d: %s\n",
+                   __FILE__, __LINE__, bo_gem->gem_handle,
+                   strerror(errno));
+       }
+
+       if (write_enable)
+               bo_gem->mapped_cpu_write = true;
+
+       pthread_mutex_unlock(&bufmgr_gem->lock);
+
+       return 0;
+}
+
+int drm_intel_gem_bo_map_gtt(drm_intel_bo *bo)
+{
+       drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
+       drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
+       struct drm_i915_gem_set_domain set_domain;
+       int ret;
+
+       pthread_mutex_lock(&bufmgr_gem->lock);
+
+       /* Get a mapping of the buffer if we haven't before. */
+       if (bo_gem->gtt_virtual == NULL) {
+               struct drm_i915_gem_mmap_gtt mmap_arg;
+
+               DBG("bo_map_gtt: mmap %d (%s)\n", bo_gem->gem_handle,
+                   bo_gem->name);
+
+               memset(&mmap_arg, 0, sizeof(mmap_arg));
+               mmap_arg.handle = bo_gem->gem_handle;
+
+               /* Get the fake offset back... */
+               ret = drmIoctl(bufmgr_gem->fd,
+                              DRM_IOCTL_I915_GEM_MMAP_GTT,
+                              &mmap_arg);
+               if (ret != 0) {
+                       ret = -errno;
+                       DBG("%s:%d: Error preparing buffer map %d (%s): %s .\n",
+                           __FILE__, __LINE__,
+                           bo_gem->gem_handle, bo_gem->name,
+                           strerror(errno));
+                       pthread_mutex_unlock(&bufmgr_gem->lock);
+                       return ret;
+               }
+
+               /* and mmap it */
+               bo_gem->gtt_virtual = mmap(0, bo->size, PROT_READ | PROT_WRITE,
+                                          MAP_SHARED, bufmgr_gem->fd,
+                                          mmap_arg.offset);
+               if (bo_gem->gtt_virtual == MAP_FAILED) {
+                       bo_gem->gtt_virtual = NULL;
+                       ret = -errno;
+                       DBG("%s:%d: Error mapping buffer %d (%s): %s .\n",
+                           __FILE__, __LINE__,
+                           bo_gem->gem_handle, bo_gem->name,
+                           strerror(errno));
+                       pthread_mutex_unlock(&bufmgr_gem->lock);
+                       return ret;
+               }
+       }
+
+       bo->virtual = bo_gem->gtt_virtual;
+
+       DBG("bo_map_gtt: %d (%s) -> %p\n", bo_gem->gem_handle, bo_gem->name,
+           bo_gem->gtt_virtual);
+
+       /* Now move it to the GTT domain so that the CPU caches are flushed */
+       set_domain.handle = bo_gem->gem_handle;
+       set_domain.read_domains = I915_GEM_DOMAIN_GTT;
+       set_domain.write_domain = I915_GEM_DOMAIN_GTT;
+       ret = drmIoctl(bufmgr_gem->fd,
+                      DRM_IOCTL_I915_GEM_SET_DOMAIN,
+                      &set_domain);
+       if (ret != 0) {
+               DBG("%s:%d: Error setting domain %d: %s\n",
+                   __FILE__, __LINE__, bo_gem->gem_handle,
+                   strerror(errno));
+       }
+
+       pthread_mutex_unlock(&bufmgr_gem->lock);
+
+       return 0;
+}
+
+static int drm_intel_gem_bo_unmap(drm_intel_bo *bo)
+{
+       drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
+       drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
+       struct drm_i915_gem_sw_finish sw_finish;
+       int ret = 0;
+
+       if (bo == NULL)
+               return 0;
+
+       pthread_mutex_lock(&bufmgr_gem->lock);
+
+       if (bo_gem->mapped_cpu_write) {
+               /* Cause a flush to happen if the buffer's pinned for
+                * scanout, so the results show up in a timely manner.
+                * Unlike GTT set domains, this only does work if the
+                * buffer should be scanout-related.
+                */
+               sw_finish.handle = bo_gem->gem_handle;
+               ret = drmIoctl(bufmgr_gem->fd,
+                              DRM_IOCTL_I915_GEM_SW_FINISH,
+                              &sw_finish);
+               ret = ret == -1 ? -errno : 0;
+
+               bo_gem->mapped_cpu_write = false;
+       }
+
+       bo->virtual = NULL;
+       pthread_mutex_unlock(&bufmgr_gem->lock);
+
+       return ret;
+}
+
+int drm_intel_gem_bo_unmap_gtt(drm_intel_bo *bo)
+{
+       return drm_intel_gem_bo_unmap(bo);
+}
+
+static int
+drm_intel_gem_bo_subdata(drm_intel_bo *bo, unsigned long offset,
+                        unsigned long size, const void *data)
+{
+       drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
+       drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
+       struct drm_i915_gem_pwrite pwrite;
+       int ret;
+
+       memset(&pwrite, 0, sizeof(pwrite));
+       pwrite.handle = bo_gem->gem_handle;
+       pwrite.offset = offset;
+       pwrite.size = size;
+       pwrite.data_ptr = (uint64_t) (uintptr_t) data;
+       ret = drmIoctl(bufmgr_gem->fd,
+                      DRM_IOCTL_I915_GEM_PWRITE,
+                      &pwrite);
+       if (ret != 0) {
+               ret = -errno;
+               DBG("%s:%d: Error writing data to buffer %d: (%d %d) %s .\n",
+                   __FILE__, __LINE__, bo_gem->gem_handle, (int)offset,
+                   (int)size, strerror(errno));
+       }
+
+       return ret;
+}
+
+static int
+drm_intel_gem_get_pipe_from_crtc_id(drm_intel_bufmgr *bufmgr, int crtc_id)
+{
+       drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr;
+       struct drm_i915_get_pipe_from_crtc_id get_pipe_from_crtc_id;
+       int ret;
+
+       get_pipe_from_crtc_id.crtc_id = crtc_id;
+       ret = drmIoctl(bufmgr_gem->fd,
+                      DRM_IOCTL_I915_GET_PIPE_FROM_CRTC_ID,
+                      &get_pipe_from_crtc_id);
+       if (ret != 0) {
+               /* We return -1 here to signal that we don't
+                * know which pipe is associated with this crtc.
+                * This lets the caller know that this information
+                * isn't available; using the wrong pipe for
+                * vblank waiting can cause the chipset to lock up
+                */
+               return -1;
+       }
+
+       return get_pipe_from_crtc_id.pipe;
+}
+
+static int
+drm_intel_gem_bo_get_subdata(drm_intel_bo *bo, unsigned long offset,
+                            unsigned long size, void *data)
+{
+       drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
+       drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
+       struct drm_i915_gem_pread pread;
+       int ret;
+
+       memset(&pread, 0, sizeof(pread));
+       pread.handle = bo_gem->gem_handle;
+       pread.offset = offset;
+       pread.size = size;
+       pread.data_ptr = (uint64_t) (uintptr_t) data;
+       ret = drmIoctl(bufmgr_gem->fd,
+                      DRM_IOCTL_I915_GEM_PREAD,
+                      &pread);
+       if (ret != 0) {
+               ret = -errno;
+               DBG("%s:%d: Error reading data from buffer %d: (%d %d) %s .\n",
+                   __FILE__, __LINE__, bo_gem->gem_handle, (int)offset,
+                   (int)size, strerror(errno));
+       }
+
+       return ret;
+}
+
+/** Waits for all GPU rendering with the object to have completed. */
+static void
+drm_intel_gem_bo_wait_rendering(drm_intel_bo *bo)
+{
+       drm_intel_gem_bo_start_gtt_access(bo, 1);
+}
+
+/**
+ * Sets the object to the GTT read and possibly write domain, used by the X
+ * 2D driver in the absence of kernel support to do drm_intel_gem_bo_map_gtt().
+ *
+ * In combination with drm_intel_gem_bo_pin() and manual fence management, we
+ * can do tiled pixmaps this way.
+ */
+void
+drm_intel_gem_bo_start_gtt_access(drm_intel_bo *bo, int write_enable)
+{
+       drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
+       drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
+       struct drm_i915_gem_set_domain set_domain;
+       int ret;
+
+       set_domain.handle = bo_gem->gem_handle;
+       set_domain.read_domains = I915_GEM_DOMAIN_GTT;
+       set_domain.write_domain = write_enable ? I915_GEM_DOMAIN_GTT : 0;
+       ret = drmIoctl(bufmgr_gem->fd,
+                      DRM_IOCTL_I915_GEM_SET_DOMAIN,
+                      &set_domain);
+       if (ret != 0) {
+               DBG("%s:%d: Error setting memory domains %d (%08x %08x): %s .\n",
+                   __FILE__, __LINE__, bo_gem->gem_handle,
+                   set_domain.read_domains, set_domain.write_domain,
+                   strerror(errno));
+       }
+}
+
+static void
+drm_intel_bufmgr_gem_destroy(drm_intel_bufmgr *bufmgr)
+{
+       drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr;
+       int i;
+
+       free(bufmgr_gem->exec2_objects);
+       free(bufmgr_gem->exec_objects);
+       free(bufmgr_gem->exec_bos);
+
+       pthread_mutex_destroy(&bufmgr_gem->lock);
+
+       /* Free any cached buffer objects we were going to reuse */
+       for (i = 0; i < bufmgr_gem->num_buckets; i++) {
+               struct drm_intel_gem_bo_bucket *bucket =
+                   &bufmgr_gem->cache_bucket[i];
+               drm_intel_bo_gem *bo_gem;
+
+               while (!DRMLISTEMPTY(&bucket->head)) {
+                       bo_gem = DRMLISTENTRY(drm_intel_bo_gem,
+                                             bucket->head.next, head);
+                       DRMLISTDEL(&bo_gem->head);
+
+                       drm_intel_gem_bo_free(&bo_gem->bo);
+               }
+       }
+
+       free(bufmgr);
+}
+
+/**
+ * Adds the target buffer to the validation list and adds the relocation
+ * to the reloc_buffer's relocation list.
+ *
+ * The relocation entry at the given offset must already contain the
+ * precomputed relocation value, because the kernel will optimize out
+ * the relocation entry write when the buffer hasn't moved from the
+ * last known offset in target_bo.
+ */
+static int
+do_bo_emit_reloc(drm_intel_bo *bo, uint32_t offset,
+                drm_intel_bo *target_bo, uint32_t target_offset,
+                uint32_t read_domains, uint32_t write_domain,
+                bool need_fence)
+{
+       drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
+       drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
+       drm_intel_bo_gem *target_bo_gem = (drm_intel_bo_gem *) target_bo;
+       bool fenced_command;
+
+       if (bo_gem->has_error)
+               return -ENOMEM;
+
+       if (target_bo_gem->has_error) {
+               bo_gem->has_error = true;
+               return -ENOMEM;
+       }
+
+       /* We never use HW fences for rendering on 965+ */
+       if (bufmgr_gem->gen >= 4)
+               need_fence = false;
+
+       fenced_command = need_fence;
+       if (target_bo_gem->tiling_mode == I915_TILING_NONE)
+               need_fence = false;
+
+       /* Create a new relocation list if needed */
+       if (bo_gem->relocs == NULL && drm_intel_setup_reloc_list(bo))
+               return -ENOMEM;
+
+       /* Check overflow */
+       assert(bo_gem->reloc_count < bufmgr_gem->max_relocs);
+
+       /* Check args */
+       assert(offset <= bo->size - 4);
+       assert((write_domain & (write_domain - 1)) == 0);
+
+       /* Make sure that we're not adding a reloc to something whose size has
+        * already been accounted for.
+        */
+       assert(!bo_gem->used_as_reloc_target);
+       if (target_bo_gem != bo_gem) {
+               target_bo_gem->used_as_reloc_target = true;
+               bo_gem->reloc_tree_size += target_bo_gem->reloc_tree_size;
+       }
+       /* An object needing a fence is a tiled buffer, so it won't have
+        * relocs to other buffers.
+        */
+       if (need_fence)
+               target_bo_gem->reloc_tree_fences = 1;
+       bo_gem->reloc_tree_fences += target_bo_gem->reloc_tree_fences;
+
+       bo_gem->relocs[bo_gem->reloc_count].offset = offset;
+       bo_gem->relocs[bo_gem->reloc_count].delta = target_offset;
+       bo_gem->relocs[bo_gem->reloc_count].target_handle =
+           target_bo_gem->gem_handle;
+       bo_gem->relocs[bo_gem->reloc_count].read_domains = read_domains;
+       bo_gem->relocs[bo_gem->reloc_count].write_domain = write_domain;
+       bo_gem->relocs[bo_gem->reloc_count].presumed_offset = target_bo->offset;
+
+       bo_gem->reloc_target_info[bo_gem->reloc_count].bo = target_bo;
+       if (target_bo != bo)
+               drm_intel_gem_bo_reference(target_bo);
+       if (fenced_command)
+               bo_gem->reloc_target_info[bo_gem->reloc_count].flags =
+                       DRM_INTEL_RELOC_FENCE;
+       else
+               bo_gem->reloc_target_info[bo_gem->reloc_count].flags = 0;
+
+       bo_gem->reloc_count++;
+
+       return 0;
+}
+
+static int
+drm_intel_gem_bo_emit_reloc(drm_intel_bo *bo, uint32_t offset,
+                           drm_intel_bo *target_bo, uint32_t target_offset,
+                           uint32_t read_domains, uint32_t write_domain)
+{
+       drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bo->bufmgr;
+
+       return do_bo_emit_reloc(bo, offset, target_bo, target_offset,
+                               read_domains, write_domain,
+                               !bufmgr_gem->fenced_relocs);
+}
+
+static int
+drm_intel_gem_bo_emit_reloc_fence(drm_intel_bo *bo, uint32_t offset,
+                                 drm_intel_bo *target_bo,
+                                 uint32_t target_offset,
+                                 uint32_t read_domains, uint32_t write_domain)
+{
+       return do_bo_emit_reloc(bo, offset, target_bo, target_offset,
+                               read_domains, write_domain, true);
+}
+
+int
+drm_intel_gem_bo_get_reloc_count(drm_intel_bo *bo)
+{
+       drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
+
+       return bo_gem->reloc_count;
+}
+
+/**
+ * Removes existing relocation entries in the BO after "start".
+ *
+ * This allows a user to avoid a two-step process for state setup with
+ * counting up all the buffer objects and doing a
+ * drm_intel_bufmgr_check_aperture_space() before emitting any of the
+ * relocations for the state setup.  Instead, save the state of the
+ * batchbuffer including drm_intel_gem_get_reloc_count(), emit all the
+ * state, and then check if it still fits in the aperture.
+ *
+ * Any further drm_intel_bufmgr_check_aperture_space() queries
+ * involving this buffer in the tree are undefined after this call.
+ */
+void
+drm_intel_gem_bo_clear_relocs(drm_intel_bo *bo, int start)
+{
+       drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
+       int i;
+       struct timespec time;
+
+       clock_gettime(CLOCK_MONOTONIC, &time);
+
+       assert(bo_gem->reloc_count >= start);
+       /* Unreference the cleared target buffers */
+       for (i = start; i < bo_gem->reloc_count; i++) {
+               if (bo_gem->reloc_target_info[i].bo != bo) {
+                       drm_intel_gem_bo_unreference_locked_timed(bo_gem->
+                                                                 reloc_target_info[i].bo,
+                                                                 time.tv_sec);
+               }
+       }
+       bo_gem->reloc_count = start;
+}
+
+/**
+ * Walk the tree of relocations rooted at BO and accumulate the list of
+ * validations to be performed and update the relocation buffers with
+ * index values into the validation list.
+ */
+static void
+drm_intel_gem_bo_process_reloc(drm_intel_bo *bo)
+{
+       drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
+       int i;
+
+       if (bo_gem->relocs == NULL)
+               return;
+
+       for (i = 0; i < bo_gem->reloc_count; i++) {
+               drm_intel_bo *target_bo = bo_gem->reloc_target_info[i].bo;
+
+               if (target_bo == bo)
+                       continue;
+
+               /* Continue walking the tree depth-first. */
+               drm_intel_gem_bo_process_reloc(target_bo);
+
+               /* Add the target to the validate list */
+               drm_intel_add_validate_buffer(target_bo);
+       }
+}
+
+static void
+drm_intel_gem_bo_process_reloc2(drm_intel_bo *bo)
+{
+       drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *)bo;
+       int i;
+
+       if (bo_gem->relocs == NULL)
+               return;
+
+       for (i = 0; i < bo_gem->reloc_count; i++) {
+               drm_intel_bo *target_bo = bo_gem->reloc_target_info[i].bo;
+               int need_fence;
+
+               if (target_bo == bo)
+                       continue;
+
+               /* Continue walking the tree depth-first. */
+               drm_intel_gem_bo_process_reloc2(target_bo);
+
+               need_fence = (bo_gem->reloc_target_info[i].flags &
+                             DRM_INTEL_RELOC_FENCE);
+
+               /* Add the target to the validate list */
+               drm_intel_add_validate_buffer2(target_bo, need_fence);
+       }
+}
+
+
+static void
+drm_intel_update_buffer_offsets(drm_intel_bufmgr_gem *bufmgr_gem)
+{
+       int i;
+
+       for (i = 0; i < bufmgr_gem->exec_count; i++) {
+               drm_intel_bo *bo = bufmgr_gem->exec_bos[i];
+               drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
+
+               /* Update the buffer offset */
+               if (bufmgr_gem->exec_objects[i].offset != bo->offset) {
+                       DBG("BO %d (%s) migrated: 0x%08lx -> 0x%08llx\n",
+                           bo_gem->gem_handle, bo_gem->name, bo->offset,
+                           (unsigned long long)bufmgr_gem->exec_objects[i].
+                           offset);
+                       bo->offset = bufmgr_gem->exec_objects[i].offset;
+               }
+       }
+}
+
+static void
+drm_intel_update_buffer_offsets2 (drm_intel_bufmgr_gem *bufmgr_gem)
+{
+       int i;
+
+       for (i = 0; i < bufmgr_gem->exec_count; i++) {
+               drm_intel_bo *bo = bufmgr_gem->exec_bos[i];
+               drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *)bo;
+
+               /* Update the buffer offset */
+               if (bufmgr_gem->exec2_objects[i].offset != bo->offset) {
+                       DBG("BO %d (%s) migrated: 0x%08lx -> 0x%08llx\n",
+                           bo_gem->gem_handle, bo_gem->name, bo->offset,
+                           (unsigned long long)bufmgr_gem->exec2_objects[i].offset);
+                       bo->offset = bufmgr_gem->exec2_objects[i].offset;
+               }
+       }
+}
+
+static int
+drm_intel_gem_bo_exec(drm_intel_bo *bo, int used,
+                     drm_clip_rect_t * cliprects, int num_cliprects, int DR4)
+{
+       drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
+       drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
+       struct drm_i915_gem_execbuffer execbuf;
+       int ret, i;
+
+       if (bo_gem->has_error)
+               return -ENOMEM;
+
+       pthread_mutex_lock(&bufmgr_gem->lock);
+       /* Update indices and set up the validate list. */
+       drm_intel_gem_bo_process_reloc(bo);
+
+       /* Add the batch buffer to the validation list.  There are no
+        * relocations pointing to it.
+        */
+       drm_intel_add_validate_buffer(bo);
+
+       execbuf.buffers_ptr = (uintptr_t) bufmgr_gem->exec_objects;
+       execbuf.buffer_count = bufmgr_gem->exec_count;
+       execbuf.batch_start_offset = 0;
+       execbuf.batch_len = used;
+       execbuf.cliprects_ptr = (uintptr_t) cliprects;
+       execbuf.num_cliprects = num_cliprects;
+       execbuf.DR1 = 0;
+       execbuf.DR4 = DR4;
+
+       ret = drmIoctl(bufmgr_gem->fd,
+                      DRM_IOCTL_I915_GEM_EXECBUFFER,
+                      &execbuf);
+       if (ret != 0) {
+               ret = -errno;
+               if (errno == ENOSPC) {
+                       DBG("Execbuffer fails to pin. "
+                           "Estimate: %u. Actual: %u. Available: %u\n",
+                           drm_intel_gem_estimate_batch_space(bufmgr_gem->exec_bos,
+                                                              bufmgr_gem->
+                                                              exec_count),
+                           drm_intel_gem_compute_batch_space(bufmgr_gem->exec_bos,
+                                                             bufmgr_gem->
+                                                             exec_count),
+                           (unsigned int)bufmgr_gem->gtt_size);
+               }
+       }
+       drm_intel_update_buffer_offsets(bufmgr_gem);
+
+       if (bufmgr_gem->bufmgr.debug)
+               drm_intel_gem_dump_validation_list(bufmgr_gem);
+
+       for (i = 0; i < bufmgr_gem->exec_count; i++) {
+               drm_intel_bo *bo = bufmgr_gem->exec_bos[i];
+               drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
+
+               /* Disconnect the buffer from the validate list */
+               bo_gem->validate_index = -1;
+               bufmgr_gem->exec_bos[i] = NULL;
+       }
+       bufmgr_gem->exec_count = 0;
+       pthread_mutex_unlock(&bufmgr_gem->lock);
+
+       return ret;
+}
+
+static int
+drm_intel_gem_bo_mrb_exec2(drm_intel_bo *bo, int used,
+                       drm_clip_rect_t *cliprects, int num_cliprects, int DR4,
+                       unsigned int flags)
+{
+       drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bo->bufmgr;
+       struct drm_i915_gem_execbuffer2 execbuf;
+       int ret, i;
+
+       switch (flags & 0x7) {
+       default:
+               return -EINVAL;
+       case I915_EXEC_BLT:
+               if (!bufmgr_gem->has_blt)
+                       return -EINVAL;
+               break;
+       case I915_EXEC_BSD:
+               if (!bufmgr_gem->has_bsd)
+                       return -EINVAL;
+               break;
+       case I915_EXEC_RENDER:
+       case I915_EXEC_DEFAULT:
+               break;
+       }
+
+       pthread_mutex_lock(&bufmgr_gem->lock);
+       /* Update indices and set up the validate list. */
+       drm_intel_gem_bo_process_reloc2(bo);
+
+       /* Add the batch buffer to the validation list.  There are no relocations
+        * pointing to it.
+        */
+       drm_intel_add_validate_buffer2(bo, 0);
+
+       execbuf.buffers_ptr = (uintptr_t)bufmgr_gem->exec2_objects;
+       execbuf.buffer_count = bufmgr_gem->exec_count;
+       execbuf.batch_start_offset = 0;
+       execbuf.batch_len = used;
+       execbuf.cliprects_ptr = (uintptr_t)cliprects;
+       execbuf.num_cliprects = num_cliprects;
+       execbuf.DR1 = 0;
+       execbuf.DR4 = DR4;
+       execbuf.flags = flags;
+       execbuf.rsvd1 = 0;
+       execbuf.rsvd2 = 0;
+
+       ret = drmIoctl(bufmgr_gem->fd,
+                      DRM_IOCTL_I915_GEM_EXECBUFFER2,
+                      &execbuf);
+       if (ret != 0) {
+               ret = -errno;
+               if (ret == -ENOSPC) {
+                       DBG("Execbuffer fails to pin. "
+                           "Estimate: %u. Actual: %u. Available: %u\n",
+                           drm_intel_gem_estimate_batch_space(bufmgr_gem->exec_bos,
+                                                              bufmgr_gem->exec_count),
+                           drm_intel_gem_compute_batch_space(bufmgr_gem->exec_bos,
+                                                             bufmgr_gem->exec_count),
+                           (unsigned int) bufmgr_gem->gtt_size);
+               }
+       }
+       drm_intel_update_buffer_offsets2(bufmgr_gem);
+
+       if (bufmgr_gem->bufmgr.debug)
+               drm_intel_gem_dump_validation_list(bufmgr_gem);
+
+       for (i = 0; i < bufmgr_gem->exec_count; i++) {
+               drm_intel_bo *bo = bufmgr_gem->exec_bos[i];
+               drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *)bo;
+
+               /* Disconnect the buffer from the validate list */
+               bo_gem->validate_index = -1;
+               bufmgr_gem->exec_bos[i] = NULL;
+       }
+       bufmgr_gem->exec_count = 0;
+       pthread_mutex_unlock(&bufmgr_gem->lock);
+
+       return ret;
+}
+
+static int
+drm_intel_gem_bo_exec2(drm_intel_bo *bo, int used,
+                      drm_clip_rect_t *cliprects, int num_cliprects,
+                      int DR4)
+{
+       return drm_intel_gem_bo_mrb_exec2(bo, used,
+                                       cliprects, num_cliprects, DR4,
+                                       I915_EXEC_RENDER);
+}
+
+static int
+drm_intel_gem_bo_pin(drm_intel_bo *bo, uint32_t alignment)
+{
+       drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
+       drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
+       struct drm_i915_gem_pin pin;
+       int ret;
+
+       memset(&pin, 0, sizeof(pin));
+       pin.handle = bo_gem->gem_handle;
+       pin.alignment = alignment;
+
+       ret = drmIoctl(bufmgr_gem->fd,
+                      DRM_IOCTL_I915_GEM_PIN,
+                      &pin);
+       if (ret != 0)
+               return -errno;
+
+       bo->offset = pin.offset;
+       return 0;
+}
+
+static int
+drm_intel_gem_bo_unpin(drm_intel_bo *bo)
+{
+       drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
+       drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
+       struct drm_i915_gem_unpin unpin;
+       int ret;
+
+       memset(&unpin, 0, sizeof(unpin));
+       unpin.handle = bo_gem->gem_handle;
+
+       ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_UNPIN, &unpin);
+       if (ret != 0)
+               return -errno;
+
+       return 0;
+}
+
+static int
+drm_intel_gem_bo_set_tiling_internal(drm_intel_bo *bo,
+                                    uint32_t tiling_mode,
+                                    uint32_t stride)
+{
+       drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
+       drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
+       struct drm_i915_gem_set_tiling set_tiling;
+       int ret;
+
+       if (bo_gem->global_name == 0 &&
+           tiling_mode == bo_gem->tiling_mode &&
+           stride == bo_gem->stride)
+               return 0;
+
+       memset(&set_tiling, 0, sizeof(set_tiling));
+       do {
+               /* set_tiling is slightly broken and overwrites the
+                * input on the error path, so we have to open code
+                * rmIoctl.
+                */
+               set_tiling.handle = bo_gem->gem_handle;
+               set_tiling.tiling_mode = tiling_mode;
+               set_tiling.stride = stride;
+
+               ret = ioctl(bufmgr_gem->fd,
+                           DRM_IOCTL_I915_GEM_SET_TILING,
+                           &set_tiling);
+       } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
+       if (ret == -1)
+               return -errno;
+
+       bo_gem->tiling_mode = set_tiling.tiling_mode;
+       bo_gem->swizzle_mode = set_tiling.swizzle_mode;
+       bo_gem->stride = set_tiling.stride;
+       return 0;
+}
+
+static int
+drm_intel_gem_bo_set_tiling(drm_intel_bo *bo, uint32_t * tiling_mode,
+                           uint32_t stride)
+{
+       drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
+       drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
+       int ret;
+
+       /* Linear buffers have no stride. By ensuring that we only ever use
+        * stride 0 with linear buffers, we simplify our code.
+        */
+       if (*tiling_mode == I915_TILING_NONE)
+               stride = 0;
+
+       ret = drm_intel_gem_bo_set_tiling_internal(bo, *tiling_mode, stride);
+       if (ret == 0)
+               drm_intel_bo_gem_set_in_aperture_size(bufmgr_gem, bo_gem);
+
+       *tiling_mode = bo_gem->tiling_mode;
+       return ret;
+}
+
+static int
+drm_intel_gem_bo_get_tiling(drm_intel_bo *bo, uint32_t * tiling_mode,
+                           uint32_t * swizzle_mode)
+{
+       drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
+
+       *tiling_mode = bo_gem->tiling_mode;
+       *swizzle_mode = bo_gem->swizzle_mode;
+       return 0;
+}
+
+static int
+drm_intel_gem_bo_flink(drm_intel_bo *bo, uint32_t * name)
+{
+       drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
+       drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
+       struct drm_gem_flink flink;
+       int ret;
+
+       if (!bo_gem->global_name) {
+               memset(&flink, 0, sizeof(flink));
+               flink.handle = bo_gem->gem_handle;
+
+               ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_GEM_FLINK, &flink);
+               if (ret != 0)
+                       return -errno;
+               bo_gem->global_name = flink.name;
+               bo_gem->reusable = false;
+
+               DRMLISTADDTAIL(&bo_gem->name_list, &bufmgr_gem->named);
+       }
+
+       *name = bo_gem->global_name;
+       return 0;
+}
+
+/**
+ * Enables unlimited caching of buffer objects for reuse.
+ *
+ * This is potentially very memory expensive, as the cache at each bucket
+ * size is only bounded by how many buffers of that size we've managed to have
+ * in flight at once.
+ */
+void
+drm_intel_bufmgr_gem_enable_reuse(drm_intel_bufmgr *bufmgr)
+{
+       drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr;
+
+       bufmgr_gem->bo_reuse = true;
+}
+
+/**
+ * Enable use of fenced reloc type.
+ *
+ * New code should enable this to avoid unnecessary fence register
+ * allocation.  If this option is not enabled, all relocs will have fence
+ * register allocated.
+ */
+void
+drm_intel_bufmgr_gem_enable_fenced_relocs(drm_intel_bufmgr *bufmgr)
+{
+       drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bufmgr;
+
+       if (bufmgr_gem->bufmgr.bo_exec == drm_intel_gem_bo_exec2)
+               bufmgr_gem->fenced_relocs = true;
+}
+
+/**
+ * Return the additional aperture space required by the tree of buffer objects
+ * rooted at bo.
+ */
+static int
+drm_intel_gem_bo_get_aperture_space(drm_intel_bo *bo)
+{
+       drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
+       int i;
+       int total = 0;
+
+       if (bo == NULL || bo_gem->included_in_check_aperture)
+               return 0;
+
+       total += bo->size;
+       bo_gem->included_in_check_aperture = true;
+
+       for (i = 0; i < bo_gem->reloc_count; i++)
+               total +=
+                   drm_intel_gem_bo_get_aperture_space(bo_gem->
+                                                       reloc_target_info[i].bo);
+
+       return total;
+}
+
+/**
+ * Count the number of buffers in this list that need a fence reg
+ *
+ * If the count is greater than the number of available regs, we'll have
+ * to ask the caller to resubmit a batch with fewer tiled buffers.
+ *
+ * This function over-counts if the same buffer is used multiple times.
+ */
+static unsigned int
+drm_intel_gem_total_fences(drm_intel_bo ** bo_array, int count)
+{
+       int i;
+       unsigned int total = 0;
+
+       for (i = 0; i < count; i++) {
+               drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo_array[i];
+
+               if (bo_gem == NULL)
+                       continue;
+
+               total += bo_gem->reloc_tree_fences;
+       }
+       return total;
+}
+
+/**
+ * Clear the flag set by drm_intel_gem_bo_get_aperture_space() so we're ready
+ * for the next drm_intel_bufmgr_check_aperture_space() call.
+ */
+static void
+drm_intel_gem_bo_clear_aperture_space_flag(drm_intel_bo *bo)
+{
+       drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
+       int i;
+
+       if (bo == NULL || !bo_gem->included_in_check_aperture)
+               return;
+
+       bo_gem->included_in_check_aperture = false;
+
+       for (i = 0; i < bo_gem->reloc_count; i++)
+               drm_intel_gem_bo_clear_aperture_space_flag(bo_gem->
+                                                          reloc_target_info[i].bo);
+}
+
+/**
+ * Return a conservative estimate for the amount of aperture required
+ * for a collection of buffers. This may double-count some buffers.
+ */
+static unsigned int
+drm_intel_gem_estimate_batch_space(drm_intel_bo **bo_array, int count)
+{
+       int i;
+       unsigned int total = 0;
+
+       for (i = 0; i < count; i++) {
+               drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo_array[i];
+               if (bo_gem != NULL)
+                       total += bo_gem->reloc_tree_size;
+       }
+       return total;
+}
+
+/**
+ * Return the amount of aperture needed for a collection of buffers.
+ * This avoids double counting any buffers, at the cost of looking
+ * at every buffer in the set.
+ */
+static unsigned int
+drm_intel_gem_compute_batch_space(drm_intel_bo **bo_array, int count)
+{
+       int i;
+       unsigned int total = 0;
+
+       for (i = 0; i < count; i++) {
+               total += drm_intel_gem_bo_get_aperture_space(bo_array[i]);
+               /* For the first buffer object in the array, we get an
+                * accurate count back for its reloc_tree size (since nothing
+                * had been flagged as being counted yet).  We can save that
+                * value out as a more conservative reloc_tree_size that
+                * avoids double-counting target buffers.  Since the first
+                * buffer happens to usually be the batch buffer in our
+                * callers, this can pull us back from doing the tree
+                * walk on every new batch emit.
+                */
+               if (i == 0) {
+                       drm_intel_bo_gem *bo_gem =
+                           (drm_intel_bo_gem *) bo_array[i];
+                       bo_gem->reloc_tree_size = total;
+               }
+       }
+
+       for (i = 0; i < count; i++)
+               drm_intel_gem_bo_clear_aperture_space_flag(bo_array[i]);
+       return total;
+}
+
+/**
+ * Return -1 if the batchbuffer should be flushed before attempting to
+ * emit rendering referencing the buffers pointed to by bo_array.
+ *
+ * This is required because if we try to emit a batchbuffer with relocations
+ * to a tree of buffers that won't simultaneously fit in the aperture,
+ * the rendering will return an error at a point where the software is not
+ * prepared to recover from it.
+ *
+ * However, we also want to emit the batchbuffer significantly before we reach
+ * the limit, as a series of batchbuffers each of which references buffers
+ * covering almost all of the aperture means that at each emit we end up
+ * waiting to evict a buffer from the last rendering, and we get synchronous
+ * performance.  By emitting smaller batchbuffers, we eat some CPU overhead to
+ * get better parallelism.
+ */
+static int
+drm_intel_gem_check_aperture_space(drm_intel_bo **bo_array, int count)
+{
+       drm_intel_bufmgr_gem *bufmgr_gem =
+           (drm_intel_bufmgr_gem *) bo_array[0]->bufmgr;
+       unsigned int total = 0;
+       unsigned int threshold = bufmgr_gem->gtt_size * 3 / 4;
+       int total_fences;
+
+       /* Check for fence reg constraints if necessary */
+       if (bufmgr_gem->available_fences) {
+               total_fences = drm_intel_gem_total_fences(bo_array, count);
+               if (total_fences > bufmgr_gem->available_fences)
+                       return -ENOSPC;
+       }
+
+       total = drm_intel_gem_estimate_batch_space(bo_array, count);
+
+       if (total > threshold)
+               total = drm_intel_gem_compute_batch_space(bo_array, count);
+
+       if (total > threshold) {
+               DBG("check_space: overflowed available aperture, "
+                   "%dkb vs %dkb\n",
+                   total / 1024, (int)bufmgr_gem->gtt_size / 1024);
+               return -ENOSPC;
+       } else {
+               DBG("drm_check_space: total %dkb vs bufgr %dkb\n", total / 1024,
+                   (int)bufmgr_gem->gtt_size / 1024);
+               return 0;
+       }
+}
+
+/*
+ * Disable buffer reuse for objects which are shared with the kernel
+ * as scanout buffers
+ */
+static int
+drm_intel_gem_bo_disable_reuse(drm_intel_bo *bo)
+{
+       drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
+
+       bo_gem->reusable = false;
+       return 0;
+}
+
+static int
+drm_intel_gem_bo_is_reusable(drm_intel_bo *bo)
+{
+       drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
+
+       return bo_gem->reusable;
+}
+
+static int
+_drm_intel_gem_bo_references(drm_intel_bo *bo, drm_intel_bo *target_bo)
+{
+       drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
+       int i;
+
+       for (i = 0; i < bo_gem->reloc_count; i++) {
+               if (bo_gem->reloc_target_info[i].bo == target_bo)
+                       return 1;
+               if (bo == bo_gem->reloc_target_info[i].bo)
+                       continue;
+               if (_drm_intel_gem_bo_references(bo_gem->reloc_target_info[i].bo,
+                                               target_bo))
+                       return 1;
+       }
+
+       return 0;
+}
+
+/** Return true if target_bo is referenced by bo's relocation tree. */
+static int
+drm_intel_gem_bo_references(drm_intel_bo *bo, drm_intel_bo *target_bo)
+{
+       drm_intel_bo_gem *target_bo_gem = (drm_intel_bo_gem *) target_bo;
+
+       if (bo == NULL || target_bo == NULL)
+               return 0;
+       if (target_bo_gem->used_as_reloc_target)
+               return _drm_intel_gem_bo_references(bo, target_bo);
+       return 0;
+}
+
+static void
+add_bucket(drm_intel_bufmgr_gem *bufmgr_gem, int size)
+{
+       unsigned int i = bufmgr_gem->num_buckets;
+
+       assert(i < ARRAY_SIZE(bufmgr_gem->cache_bucket));
+
+       DRMINITLISTHEAD(&bufmgr_gem->cache_bucket[i].head);
+       bufmgr_gem->cache_bucket[i].size = size;
+       bufmgr_gem->num_buckets++;
+}
+
+static void
+init_cache_buckets(drm_intel_bufmgr_gem *bufmgr_gem)
+{
+       unsigned long size, cache_max_size = 64 * 1024 * 1024;
+
+       /* OK, so power of two buckets was too wasteful of memory.
+        * Give 3 other sizes between each power of two, to hopefully
+        * cover things accurately enough.  (The alternative is
+        * probably to just go for exact matching of sizes, and assume
+        * that for things like composited window resize the tiled
+        * width/height alignment and rounding of sizes to pages will
+        * get us useful cache hit rates anyway)
+        */
+       add_bucket(bufmgr_gem, 4096);
+       add_bucket(bufmgr_gem, 4096 * 2);
+       add_bucket(bufmgr_gem, 4096 * 3);
+
+       /* Initialize the linked lists for BO reuse cache. */
+       for (size = 4 * 4096; size <= cache_max_size; size *= 2) {
+               add_bucket(bufmgr_gem, size);
+
+               add_bucket(bufmgr_gem, size + size * 1 / 4);
+               add_bucket(bufmgr_gem, size + size * 2 / 4);
+               add_bucket(bufmgr_gem, size + size * 3 / 4);
+       }
+}
+
+/**
+ * Initializes the GEM buffer manager, which uses the kernel to allocate, map,
+ * and manage map buffer objections.
+ *
+ * \param fd File descriptor of the opened DRM device.
+ */
+drm_intel_bufmgr *
+drm_intel_bufmgr_gem_init(int fd, int batch_size)
+{
+       drm_intel_bufmgr_gem *bufmgr_gem;
+       struct drm_i915_gem_get_aperture aperture;
+       drm_i915_getparam_t gp;
+       int ret, tmp;
+       bool exec2 = false;
+
+       bufmgr_gem = calloc(1, sizeof(*bufmgr_gem));
+       if (bufmgr_gem == NULL)
+               return NULL;
+
+       bufmgr_gem->fd = fd;
+
+       if (pthread_mutex_init(&bufmgr_gem->lock, NULL) != 0) {
+               free(bufmgr_gem);
+               return NULL;
+       }
+
+       ret = drmIoctl(bufmgr_gem->fd,
+                      DRM_IOCTL_I915_GEM_GET_APERTURE,
+                      &aperture);
+
+       if (ret == 0)
+               bufmgr_gem->gtt_size = aperture.aper_available_size;
+       else {
+               fprintf(stderr, "DRM_IOCTL_I915_GEM_APERTURE failed: %s\n",
+                       strerror(errno));
+               bufmgr_gem->gtt_size = 128 * 1024 * 1024;
+               fprintf(stderr, "Assuming %dkB available aperture size.\n"
+                       "May lead to reduced performance or incorrect "
+                       "rendering.\n",
+                       (int)bufmgr_gem->gtt_size / 1024);
+       }
+
+       gp.param = I915_PARAM_CHIPSET_ID;
+       gp.value = &bufmgr_gem->pci_device;
+       ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
+       if (ret) {
+               fprintf(stderr, "get chip id failed: %d [%d]\n", ret, errno);
+               fprintf(stderr, "param: %d, val: %d\n", gp.param, *gp.value);
+       }
+
+       if (IS_GEN2(bufmgr_gem))
+               bufmgr_gem->gen = 2;
+       else if (IS_GEN3(bufmgr_gem))
+               bufmgr_gem->gen = 3;
+       else if (IS_GEN4(bufmgr_gem))
+               bufmgr_gem->gen = 4;
+       else
+               bufmgr_gem->gen = 6;
+
+       gp.value = &tmp;
+
+       gp.param = I915_PARAM_HAS_EXECBUF2;
+       ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
+       if (!ret)
+               exec2 = true;
+
+       gp.param = I915_PARAM_HAS_BSD;
+       ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
+       bufmgr_gem->has_bsd = ret == 0;
+
+       gp.param = I915_PARAM_HAS_BLT;
+       ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
+       bufmgr_gem->has_blt = ret == 0;
+
+       gp.param = I915_PARAM_HAS_RELAXED_FENCING;
+       ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
+       bufmgr_gem->has_relaxed_fencing = ret == 0;
+
+       if (bufmgr_gem->gen < 4) {
+               gp.param = I915_PARAM_NUM_FENCES_AVAIL;
+               gp.value = &bufmgr_gem->available_fences;
+               ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
+               if (ret) {
+                       fprintf(stderr, "get fences failed: %d [%d]\n", ret,
+                               errno);
+                       fprintf(stderr, "param: %d, val: %d\n", gp.param,
+                               *gp.value);
+                       bufmgr_gem->available_fences = 0;
+               } else {
+                       /* XXX The kernel reports the total number of fences,
+                        * including any that may be pinned.
+                        *
+                        * We presume that there will be at least one pinned
+                        * fence for the scanout buffer, but there may be more
+                        * than one scanout and the user may be manually
+                        * pinning buffers. Let's move to execbuffer2 and
+                        * thereby forget the insanity of using fences...
+                        */
+                       bufmgr_gem->available_fences -= 2;
+                       if (bufmgr_gem->available_fences < 0)
+                               bufmgr_gem->available_fences = 0;
+               }
+       }
+
+       /* Let's go with one relocation per every 2 dwords (but round down a bit
+        * since a power of two will mean an extra page allocation for the reloc
+        * buffer).
+        *
+        * Every 4 was too few for the blender benchmark.
+        */
+       bufmgr_gem->max_relocs = batch_size / sizeof(uint32_t) / 2 - 2;
+
+       bufmgr_gem->bufmgr.bo_alloc = drm_intel_gem_bo_alloc;
+       bufmgr_gem->bufmgr.bo_alloc_for_render =
+           drm_intel_gem_bo_alloc_for_render;
+       bufmgr_gem->bufmgr.bo_alloc_tiled = drm_intel_gem_bo_alloc_tiled;
+       bufmgr_gem->bufmgr.bo_reference = drm_intel_gem_bo_reference;
+       bufmgr_gem->bufmgr.bo_unreference = drm_intel_gem_bo_unreference;
+       bufmgr_gem->bufmgr.bo_map = drm_intel_gem_bo_map;
+       bufmgr_gem->bufmgr.bo_unmap = drm_intel_gem_bo_unmap;
+       bufmgr_gem->bufmgr.bo_subdata = drm_intel_gem_bo_subdata;
+       bufmgr_gem->bufmgr.bo_get_subdata = drm_intel_gem_bo_get_subdata;
+       bufmgr_gem->bufmgr.bo_wait_rendering = drm_intel_gem_bo_wait_rendering;
+       bufmgr_gem->bufmgr.bo_emit_reloc = drm_intel_gem_bo_emit_reloc;
+       bufmgr_gem->bufmgr.bo_emit_reloc_fence = drm_intel_gem_bo_emit_reloc_fence;
+       bufmgr_gem->bufmgr.bo_pin = drm_intel_gem_bo_pin;
+       bufmgr_gem->bufmgr.bo_unpin = drm_intel_gem_bo_unpin;
+       bufmgr_gem->bufmgr.bo_get_tiling = drm_intel_gem_bo_get_tiling;
+       bufmgr_gem->bufmgr.bo_set_tiling = drm_intel_gem_bo_set_tiling;
+       bufmgr_gem->bufmgr.bo_flink = drm_intel_gem_bo_flink;
+       /* Use the new one if available */
+       if (exec2) {
+               bufmgr_gem->bufmgr.bo_exec = drm_intel_gem_bo_exec2;
+               bufmgr_gem->bufmgr.bo_mrb_exec = drm_intel_gem_bo_mrb_exec2;
+       } else
+               bufmgr_gem->bufmgr.bo_exec = drm_intel_gem_bo_exec;
+       bufmgr_gem->bufmgr.bo_busy = drm_intel_gem_bo_busy;
+       bufmgr_gem->bufmgr.bo_madvise = drm_intel_gem_bo_madvise;
+       bufmgr_gem->bufmgr.destroy = drm_intel_bufmgr_gem_destroy;
+       bufmgr_gem->bufmgr.debug = 0;
+       bufmgr_gem->bufmgr.check_aperture_space =
+           drm_intel_gem_check_aperture_space;
+       bufmgr_gem->bufmgr.bo_disable_reuse = drm_intel_gem_bo_disable_reuse;
+       bufmgr_gem->bufmgr.bo_is_reusable = drm_intel_gem_bo_is_reusable;
+       bufmgr_gem->bufmgr.get_pipe_from_crtc_id =
+           drm_intel_gem_get_pipe_from_crtc_id;
+       bufmgr_gem->bufmgr.bo_references = drm_intel_gem_bo_references;
+
+       DRMINITLISTHEAD(&bufmgr_gem->named);
+       init_cache_buckets(bufmgr_gem);
+
+       return &bufmgr_gem->bufmgr;
+}
diff --git a/intel/intel_bufmgr_priv.h b/intel/intel_bufmgr_priv.h
new file mode 100644 (file)
index 0000000..0b62520
--- /dev/null
@@ -0,0 +1,287 @@
+/*
+ * Copyright Â© 2008 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Authors:
+ *    Eric Anholt <eric@anholt.net>
+ *
+ */
+
+/**
+ * @file intel_bufmgr_priv.h
+ *
+ * Private definitions of Intel-specific bufmgr functions and structures.
+ */
+
+#ifndef INTEL_BUFMGR_PRIV_H
+#define INTEL_BUFMGR_PRIV_H
+
+/**
+ * Context for a buffer manager instance.
+ *
+ * Contains public methods followed by private storage for the buffer manager.
+ */
+struct _drm_intel_bufmgr {
+       /**
+        * Allocate a buffer object.
+        *
+        * Buffer objects are not necessarily initially mapped into CPU virtual
+        * address space or graphics device aperture.  They must be mapped
+        * using bo_map() or drm_intel_gem_bo_map_gtt() to be used by the CPU.
+        */
+       drm_intel_bo *(*bo_alloc) (drm_intel_bufmgr *bufmgr, const char *name,
+                                  unsigned long size, unsigned int alignment);
+
+       /**
+        * Allocate a buffer object, hinting that it will be used as a
+        * render target.
+        *
+        * This is otherwise the same as bo_alloc.
+        */
+       drm_intel_bo *(*bo_alloc_for_render) (drm_intel_bufmgr *bufmgr,
+                                             const char *name,
+                                             unsigned long size,
+                                             unsigned int alignment);
+
+       /**
+        * Allocate a tiled buffer object.
+        *
+        * Alignment for tiled objects is set automatically; the 'flags'
+        * argument provides a hint about how the object will be used initially.
+        *
+        * Valid tiling formats are:
+        *  I915_TILING_NONE
+        *  I915_TILING_X
+        *  I915_TILING_Y
+        *
+        * Note the tiling format may be rejected; callers should check the
+        * 'tiling_mode' field on return, as well as the pitch value, which
+        * may have been rounded up to accommodate for tiling restrictions.
+        */
+       drm_intel_bo *(*bo_alloc_tiled) (drm_intel_bufmgr *bufmgr,
+                                        const char *name,
+                                        int x, int y, int cpp,
+                                        uint32_t *tiling_mode,
+                                        unsigned long *pitch,
+                                        unsigned long flags);
+
+       /** Takes a reference on a buffer object */
+       void (*bo_reference) (drm_intel_bo *bo);
+
+       /**
+        * Releases a reference on a buffer object, freeing the data if
+        * no references remain.
+        */
+       void (*bo_unreference) (drm_intel_bo *bo);
+
+       /**
+        * Maps the buffer into userspace.
+        *
+        * This function will block waiting for any existing execution on the
+        * buffer to complete, first.  The resulting mapping is available at
+        * buf->virtual.
+        */
+       int (*bo_map) (drm_intel_bo *bo, int write_enable);
+
+       /**
+        * Reduces the refcount on the userspace mapping of the buffer
+        * object.
+        */
+       int (*bo_unmap) (drm_intel_bo *bo);
+
+       /**
+        * Write data into an object.
+        *
+        * This is an optional function, if missing,
+        * drm_intel_bo will map/memcpy/unmap.
+        */
+       int (*bo_subdata) (drm_intel_bo *bo, unsigned long offset,
+                          unsigned long size, const void *data);
+
+       /**
+        * Read data from an object
+        *
+        * This is an optional function, if missing,
+        * drm_intel_bo will map/memcpy/unmap.
+        */
+       int (*bo_get_subdata) (drm_intel_bo *bo, unsigned long offset,
+                              unsigned long size, void *data);
+
+       /**
+        * Waits for rendering to an object by the GPU to have completed.
+        *
+        * This is not required for any access to the BO by bo_map,
+        * bo_subdata, etc.  It is merely a way for the driver to implement
+        * glFinish.
+        */
+       void (*bo_wait_rendering) (drm_intel_bo *bo);
+
+       /**
+        * Tears down the buffer manager instance.
+        */
+       void (*destroy) (drm_intel_bufmgr *bufmgr);
+
+       /**
+        * Add relocation entry in reloc_buf, which will be updated with the
+        * target buffer's real offset on on command submission.
+        *
+        * Relocations remain in place for the lifetime of the buffer object.
+        *
+        * \param bo Buffer to write the relocation into.
+        * \param offset Byte offset within reloc_bo of the pointer to
+        *                      target_bo.
+        * \param target_bo Buffer whose offset should be written into the
+        *                  relocation entry.
+        * \param target_offset Constant value to be added to target_bo's
+        *                      offset in relocation entry.
+        * \param read_domains GEM read domains which the buffer will be
+        *                      read into by the command that this relocation
+        *                      is part of.
+        * \param write_domains GEM read domains which the buffer will be
+        *                      dirtied in by the command that this
+        *                      relocation is part of.
+        */
+       int (*bo_emit_reloc) (drm_intel_bo *bo, uint32_t offset,
+                             drm_intel_bo *target_bo, uint32_t target_offset,
+                             uint32_t read_domains, uint32_t write_domain);
+       int (*bo_emit_reloc_fence)(drm_intel_bo *bo, uint32_t offset,
+                                  drm_intel_bo *target_bo,
+                                  uint32_t target_offset,
+                                  uint32_t read_domains,
+                                  uint32_t write_domain);
+
+       /** Executes the command buffer pointed to by bo. */
+       int (*bo_exec) (drm_intel_bo *bo, int used,
+                       drm_clip_rect_t *cliprects, int num_cliprects,
+                       int DR4);
+
+       /** Executes the command buffer pointed to by bo on the selected
+        * ring buffer
+        */
+       int (*bo_mrb_exec) (drm_intel_bo *bo, int used,
+                           drm_clip_rect_t *cliprects, int num_cliprects,
+                           int DR4, unsigned flags);
+
+       /**
+        * Pin a buffer to the aperture and fix the offset until unpinned
+        *
+        * \param buf Buffer to pin
+        * \param alignment Required alignment for aperture, in bytes
+        */
+       int (*bo_pin) (drm_intel_bo *bo, uint32_t alignment);
+
+       /**
+        * Unpin a buffer from the aperture, allowing it to be removed
+        *
+        * \param buf Buffer to unpin
+        */
+       int (*bo_unpin) (drm_intel_bo *bo);
+
+       /**
+        * Ask that the buffer be placed in tiling mode
+        *
+        * \param buf Buffer to set tiling mode for
+        * \param tiling_mode desired, and returned tiling mode
+        */
+       int (*bo_set_tiling) (drm_intel_bo *bo, uint32_t * tiling_mode,
+                             uint32_t stride);
+
+       /**
+        * Get the current tiling (and resulting swizzling) mode for the bo.
+        *
+        * \param buf Buffer to get tiling mode for
+        * \param tiling_mode returned tiling mode
+        * \param swizzle_mode returned swizzling mode
+        */
+       int (*bo_get_tiling) (drm_intel_bo *bo, uint32_t * tiling_mode,
+                             uint32_t * swizzle_mode);
+
+       /**
+        * Create a visible name for a buffer which can be used by other apps
+        *
+        * \param buf Buffer to create a name for
+        * \param name Returned name
+        */
+       int (*bo_flink) (drm_intel_bo *bo, uint32_t * name);
+
+       /**
+        * Returns 1 if mapping the buffer for write could cause the process
+        * to block, due to the object being active in the GPU.
+        */
+       int (*bo_busy) (drm_intel_bo *bo);
+
+       /**
+        * Specify the volatility of the buffer.
+        * \param bo Buffer to create a name for
+        * \param madv The purgeable status
+        *
+        * Use I915_MADV_DONTNEED to mark the buffer as purgeable, and it will be
+        * reclaimed under memory pressure. If you subsequently require the buffer,
+        * then you must pass I915_MADV_WILLNEED to mark the buffer as required.
+        *
+        * Returns 1 if the buffer was retained, or 0 if it was discarded whilst
+        * marked as I915_MADV_DONTNEED.
+        */
+       int (*bo_madvise) (drm_intel_bo *bo, int madv);
+
+       int (*check_aperture_space) (drm_intel_bo ** bo_array, int count);
+
+       /**
+        * Disable buffer reuse for buffers which will be shared in some way,
+        * as with scanout buffers. When the buffer reference count goes to
+        * zero, it will be freed and not placed in the reuse list.
+        *
+        * \param bo Buffer to disable reuse for
+        */
+       int (*bo_disable_reuse) (drm_intel_bo *bo);
+
+       /**
+        * Query whether a buffer is reusable.
+        *
+        * \param bo Buffer to query
+        */
+       int (*bo_is_reusable) (drm_intel_bo *bo);
+
+       /**
+        *
+        * Return the pipe associated with a crtc_id so that vblank
+        * synchronization can use the correct data in the request.
+        * This is only supported for KMS and gem at this point, when
+        * unsupported, this function returns -1 and leaves the decision
+        * of what to do in that case to the caller
+        *
+        * \param bufmgr the associated buffer manager
+        * \param crtc_id the crtc identifier
+        */
+       int (*get_pipe_from_crtc_id) (drm_intel_bufmgr *bufmgr, int crtc_id);
+
+       /** Returns true if target_bo is in the relocation tree rooted at bo. */
+       int (*bo_references) (drm_intel_bo *bo, drm_intel_bo *target_bo);
+
+       /**< Enables verbose debugging printouts */
+       int debug;
+};
+
+#define ALIGN(value, alignment)        ((value + alignment - 1) & ~(alignment - 1))
+#define ROUND_UP_TO(x, y)      (((x) + (y) - 1) / (y) * (y))
+#define ROUND_UP_TO_MB(x)      ROUND_UP_TO((x), 1024*1024)
+
+#endif /* INTEL_BUFMGR_PRIV_H */
diff --git a/intel/intel_chipset.h b/intel/intel_chipset.h
new file mode 100644 (file)
index 0000000..b4e0747
--- /dev/null
@@ -0,0 +1,101 @@
+/*
+ *
+ * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+#ifndef _INTEL_CHIPSET_H
+#define _INTEL_CHIPSET_H
+
+#define IS_830(dev) ((dev)->pci_device == 0x3577)
+#define IS_845(dev) ((dev)->pci_device == 0x2562)
+#define IS_85X(dev) ((dev)->pci_device == 0x3582)
+#define IS_865(dev) ((dev)->pci_device == 0x2572)
+
+#define IS_GEN2(dev) (IS_830(dev) ||                           \
+                     IS_845(dev) ||                            \
+                     IS_85X(dev) ||                            \
+                     IS_865(dev))
+
+#define IS_915G(dev) ((dev)->pci_device == 0x2582 ||           \
+                      (dev)->pci_device == 0x258a)
+#define IS_915GM(dev) ((dev)->pci_device == 0x2592)
+#define IS_945G(dev) ((dev)->pci_device == 0x2772)
+#define IS_945GM(dev) ((dev)->pci_device == 0x27A2 ||          \
+                        (dev)->pci_device == 0x27AE)
+
+#define IS_915(dev) (IS_915G(dev) ||                           \
+                    IS_915GM(dev))
+
+#define IS_945(dev) (IS_945G(dev) ||                           \
+                    IS_945GM(dev) ||                           \
+                    IS_G33(dev) ||                             \
+                    IS_PINEVIEW(dev))
+
+#define IS_G33(dev)    ((dev)->pci_device == 0x29C2 ||         \
+                        (dev)->pci_device == 0x29B2 ||         \
+                        (dev)->pci_device == 0x29D2)
+
+#define IS_PINEVIEW(dev) ((dev)->pci_device == 0xa001 ||       \
+                         (dev)->pci_device == 0xa011)
+
+#define IS_GEN3(dev) (IS_915(dev) ||                           \
+                     IS_945(dev) ||                            \
+                     IS_G33(dev) ||                            \
+                     IS_PINEVIEW(dev))
+
+#define IS_I965GM(dev) ((dev)->pci_device == 0x2A02)
+
+#define IS_GEN4(dev) ((dev)->pci_device == 0x2972 ||   \
+                     (dev)->pci_device == 0x2982 ||    \
+                     (dev)->pci_device == 0x2992 ||    \
+                     (dev)->pci_device == 0x29A2 ||    \
+                     (dev)->pci_device == 0x2A02 ||    \
+                     (dev)->pci_device == 0x2A12 ||    \
+                     (dev)->pci_device == 0x2A42 ||    \
+                     (dev)->pci_device == 0x2E02 ||    \
+                     (dev)->pci_device == 0x2E12 ||    \
+                     (dev)->pci_device == 0x2E22 ||    \
+                     (dev)->pci_device == 0x2E32 ||    \
+                     (dev)->pci_device == 0x2E42 ||    \
+                     (dev)->pci_device == 0x0042 ||    \
+                     (dev)->pci_device == 0x0046 ||    \
+                     IS_I965GM(dev) || \
+                     IS_G4X(dev))
+
+#define IS_GM45(dev) ((dev)->pci_device == 0x2A42)
+
+#define IS_G4X(dev) ((dev)->pci_device == 0x2E02 || \
+                     (dev)->pci_device == 0x2E12 || \
+                     (dev)->pci_device == 0x2E22 || \
+                     (dev)->pci_device == 0x2E32 || \
+                     (dev)->pci_device == 0x2E42 || \
+                    IS_GM45(dev))
+
+#define IS_9XX(dev) (IS_GEN3(dev) ||                           \
+                    IS_GEN4(dev) ||                            \
+                    IS_GEN5(dev) ||                            \
+                    IS_GEN6(dev))
+
+#endif /* _INTEL_CHIPSET_H */
diff --git a/intel/intel_debug.h b/intel/intel_debug.h
new file mode 100644 (file)
index 0000000..fa0737c
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * Copyright Â© 2011 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Authors:
+ *    Ben Widawsky <ben@bwidawsk.net>
+ *
+ */
+
+#ifndef INTEL_DEBUG_H
+#define INTEL_DEBUG_H
+
+#include <stdint.h>
+
+#define SHADER_DEBUG_SOCKET "/var/run/gen_debug"
+#define DEBUG_HANDSHAKE_VERSION 0x3
+#define DEBUG_HANDSHAKE_ACK "okay"
+
+/* First byte must always be the 1 byte version */
+struct intel_debug_handshake {
+       uint32_t version;
+       int flink_handle;
+       uint32_t per_thread_scratch;
+} __attribute__((packed));
+
+#endif
diff --git a/intel/libdrm_intel.pc.in b/intel/libdrm_intel.pc.in
new file mode 100644 (file)
index 0000000..3ba6793
--- /dev/null
@@ -0,0 +1,11 @@
+prefix=@prefix@
+exec_prefix=@exec_prefix@
+libdir=@libdir@
+includedir=@includedir@
+
+Name: libdrm
+Description: Userspace interface to kernel DRM services
+Version: @PACKAGE_VERSION@
+Requires: libdrm
+Libs: -L${libdir} -ldrm_intel
+Cflags: -I${includedir} -I${includedir}/libdrm
diff --git a/intel/mm.c b/intel/mm.c
new file mode 100644 (file)
index 0000000..1069745
--- /dev/null
@@ -0,0 +1,271 @@
+/*
+ * GLX Hardware Device Driver common code
+ * Copyright (C) 1999 Wittawat Yamwong
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * WITTAWAT YAMWONG, OR ANY OTHER CONTRIBUTORS BE LIABLE FOR ANY CLAIM, 
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
+ * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+#include <stdlib.h>
+#include <assert.h>
+
+#include "xf86drm.h"
+#include "mm.h"
+
+void mmDumpMemInfo(const struct mem_block *heap)
+{
+       drmMsg("Memory heap %p:\n", (void *)heap);
+       if (heap == 0) {
+               drmMsg("  heap == 0\n");
+       } else {
+               const struct mem_block *p;
+
+               for (p = heap->next; p != heap; p = p->next) {
+                       drmMsg("  Offset:%08x, Size:%08x, %c%c\n", p->ofs,
+                              p->size, p->free ? 'F' : '.',
+                              p->reserved ? 'R' : '.');
+               }
+
+               drmMsg("\nFree list:\n");
+
+               for (p = heap->next_free; p != heap; p = p->next_free) {
+                       drmMsg(" FREE Offset:%08x, Size:%08x, %c%c\n", p->ofs,
+                              p->size, p->free ? 'F' : '.',
+                              p->reserved ? 'R' : '.');
+               }
+
+       }
+       drmMsg("End of memory blocks\n");
+}
+
+struct mem_block *mmInit(int ofs, int size)
+{
+       struct mem_block *heap, *block;
+
+       if (size <= 0)
+               return NULL;
+
+       heap = (struct mem_block *)calloc(1, sizeof(struct mem_block));
+       if (!heap)
+               return NULL;
+
+       block = (struct mem_block *)calloc(1, sizeof(struct mem_block));
+       if (!block) {
+               free(heap);
+               return NULL;
+       }
+
+       heap->next = block;
+       heap->prev = block;
+       heap->next_free = block;
+       heap->prev_free = block;
+
+       block->heap = heap;
+       block->next = heap;
+       block->prev = heap;
+       block->next_free = heap;
+       block->prev_free = heap;
+
+       block->ofs = ofs;
+       block->size = size;
+       block->free = 1;
+
+       return heap;
+}
+
+static struct mem_block *SliceBlock(struct mem_block *p,
+                                   int startofs, int size,
+                                   int reserved, int alignment)
+{
+       struct mem_block *newblock;
+
+       /* break left  [p, newblock, p->next], then p = newblock */
+       if (startofs > p->ofs) {
+               newblock =
+                   (struct mem_block *)calloc(1, sizeof(struct mem_block));
+               if (!newblock)
+                       return NULL;
+               newblock->ofs = startofs;
+               newblock->size = p->size - (startofs - p->ofs);
+               newblock->free = 1;
+               newblock->heap = p->heap;
+
+               newblock->next = p->next;
+               newblock->prev = p;
+               p->next->prev = newblock;
+               p->next = newblock;
+
+               newblock->next_free = p->next_free;
+               newblock->prev_free = p;
+               p->next_free->prev_free = newblock;
+               p->next_free = newblock;
+
+               p->size -= newblock->size;
+               p = newblock;
+       }
+
+       /* break right, also [p, newblock, p->next] */
+       if (size < p->size) {
+               newblock =
+                   (struct mem_block *)calloc(1, sizeof(struct mem_block));
+               if (!newblock)
+                       return NULL;
+               newblock->ofs = startofs + size;
+               newblock->size = p->size - size;
+               newblock->free = 1;
+               newblock->heap = p->heap;
+
+               newblock->next = p->next;
+               newblock->prev = p;
+               p->next->prev = newblock;
+               p->next = newblock;
+
+               newblock->next_free = p->next_free;
+               newblock->prev_free = p;
+               p->next_free->prev_free = newblock;
+               p->next_free = newblock;
+
+               p->size = size;
+       }
+
+       /* p = middle block */
+       p->free = 0;
+
+       /* Remove p from the free list: 
+        */
+       p->next_free->prev_free = p->prev_free;
+       p->prev_free->next_free = p->next_free;
+
+       p->next_free = 0;
+       p->prev_free = 0;
+
+       p->reserved = reserved;
+       return p;
+}
+
+struct mem_block *mmAllocMem(struct mem_block *heap, int size, int align2,
+                            int startSearch)
+{
+       struct mem_block *p;
+       const int mask = (1 << align2) - 1;
+       int startofs = 0;
+       int endofs;
+
+       if (!heap || align2 < 0 || size <= 0)
+               return NULL;
+
+       for (p = heap->next_free; p != heap; p = p->next_free) {
+               assert(p->free);
+
+               startofs = (p->ofs + mask) & ~mask;
+               if (startofs < startSearch) {
+                       startofs = startSearch;
+               }
+               endofs = startofs + size;
+               if (endofs <= (p->ofs + p->size))
+                       break;
+       }
+
+       if (p == heap)
+               return NULL;
+
+       assert(p->free);
+       p = SliceBlock(p, startofs, size, 0, mask + 1);
+
+       return p;
+}
+
+struct mem_block *mmFindBlock(struct mem_block *heap, int start)
+{
+       struct mem_block *p;
+
+       for (p = heap->next; p != heap; p = p->next) {
+               if (p->ofs == start)
+                       return p;
+       }
+
+       return NULL;
+}
+
+static int Join2Blocks(struct mem_block *p)
+{
+       /* XXX there should be some assertions here */
+
+       /* NOTE: heap->free == 0 */
+
+       if (p->free && p->next->free) {
+               struct mem_block *q = p->next;
+
+               assert(p->ofs + p->size == q->ofs);
+               p->size += q->size;
+
+               p->next = q->next;
+               q->next->prev = p;
+
+               q->next_free->prev_free = q->prev_free;
+               q->prev_free->next_free = q->next_free;
+
+               free(q);
+               return 1;
+       }
+       return 0;
+}
+
+int mmFreeMem(struct mem_block *b)
+{
+       if (!b)
+               return 0;
+
+       if (b->free) {
+               drmMsg("block already free\n");
+               return -1;
+       }
+       if (b->reserved) {
+               drmMsg("block is reserved\n");
+               return -1;
+       }
+
+       b->free = 1;
+       b->next_free = b->heap->next_free;
+       b->prev_free = b->heap;
+       b->next_free->prev_free = b;
+       b->prev_free->next_free = b;
+
+       Join2Blocks(b);
+       if (b->prev != b->heap)
+               Join2Blocks(b->prev);
+
+       return 0;
+}
+
+void mmDestroy(struct mem_block *heap)
+{
+       struct mem_block *p;
+
+       if (!heap)
+               return;
+
+       for (p = heap->next; p != heap;) {
+               struct mem_block *next = p->next;
+               free(p);
+               p = next;
+       }
+
+       free(heap);
+}
diff --git a/intel/mm.h b/intel/mm.h
new file mode 100644 (file)
index 0000000..8a5235b
--- /dev/null
@@ -0,0 +1,94 @@
+/*
+ * GLX Hardware Device Driver common code
+ * Copyright (C) 1999 Wittawat Yamwong
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * KEITH WHITWELL, OR ANY OTHER CONTRIBUTORS BE LIABLE FOR ANY CLAIM, 
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
+ * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+/**
+ * Memory manager code.  Primarily used by device drivers to manage texture
+ * heaps, etc.
+ */
+
+#ifndef MM_H
+#define MM_H
+
+struct mem_block {
+       struct mem_block *next, *prev;
+       struct mem_block *next_free, *prev_free;
+       struct mem_block *heap;
+       int ofs, size;
+       unsigned int free:1;
+       unsigned int reserved:1;
+};
+
+/* Rename the variables in the drm copy of this code so that it doesn't
+ * conflict with mesa or whoever else has copied it around.
+ */
+#define mmInit drm_mmInit
+#define mmAllocMem drm_mmAllocMem
+#define mmFreeMem drm_mmFreeMem
+#define mmFindBlock drm_mmFindBlock
+#define mmDestroy drm_mmDestroy
+#define mmDumpMemInfo drm_mmDumpMemInfo
+
+/** 
+ * input: total size in bytes
+ * return: a heap pointer if OK, NULL if error
+ */
+extern struct mem_block *mmInit(int ofs, int size);
+
+/**
+ * Allocate 'size' bytes with 2^align2 bytes alignment,
+ * restrict the search to free memory after 'startSearch'
+ * depth and back buffers should be in different 4mb banks
+ * to get better page hits if possible
+ * input:      size = size of block
+ *             align2 = 2^align2 bytes alignment
+ *             startSearch = linear offset from start of heap to begin search
+ * return: pointer to the allocated block, 0 if error
+ */
+extern struct mem_block *mmAllocMem(struct mem_block *heap, int size,
+                                   int align2, int startSearch);
+
+/**
+ * Free block starts at offset
+ * input: pointer to a block
+ * return: 0 if OK, -1 if error
+ */
+extern int mmFreeMem(struct mem_block *b);
+
+/**
+ * Free block starts at offset
+ * input: pointer to a heap, start offset
+ * return: pointer to a block
+ */
+extern struct mem_block *mmFindBlock(struct mem_block *heap, int start);
+
+/**
+ * destroy MM
+ */
+extern void mmDestroy(struct mem_block *mmInit);
+
+/**
+ * For debuging purpose.
+ */
+extern void mmDumpMemInfo(const struct mem_block *mmInit);
+
+#endif
diff --git a/libdrm.pc.in b/libdrm.pc.in
new file mode 100644 (file)
index 0000000..b46e2a6
--- /dev/null
@@ -0,0 +1,10 @@
+prefix=@prefix@
+exec_prefix=@exec_prefix@
+libdir=@libdir@
+includedir=@includedir@
+
+Name: libdrm
+Description: Userspace interface to kernel DRM services
+Version: @PACKAGE_VERSION@
+Libs: -L${libdir} -ldrm
+Cflags: -I${includedir} -I${includedir}/libdrm
diff --git a/libdrm_lists.h b/libdrm_lists.h
new file mode 100644 (file)
index 0000000..6410f57
--- /dev/null
@@ -0,0 +1,89 @@
+/**************************************************************************
+ *
+ * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND. USA.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+ * USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ */
+
+/*
+ * List macros heavily inspired by the Linux kernel
+ * list handling. No list looping yet.
+ */
+
+#include <stddef.h>
+
+typedef struct _drmMMListHead
+{
+    struct _drmMMListHead *prev;
+    struct _drmMMListHead *next;
+} drmMMListHead;
+
+#define DRMINITLISTHEAD(__item)                       \
+  do{                                         \
+    (__item)->prev = (__item);                \
+    (__item)->next = (__item);                \
+  } while (0)
+
+#define DRMLISTADD(__item, __list)             \
+  do {                                         \
+    (__item)->prev = (__list);                 \
+    (__item)->next = (__list)->next;           \
+    (__list)->next->prev = (__item);           \
+    (__list)->next = (__item);                 \
+  } while (0)
+
+#define DRMLISTADDTAIL(__item, __list)         \
+  do {                                         \
+    (__item)->next = (__list);                 \
+    (__item)->prev = (__list)->prev;           \
+    (__list)->prev->next = (__item);           \
+    (__list)->prev = (__item);                 \
+  } while(0)
+
+#define DRMLISTDEL(__item)                     \
+  do {                                         \
+    (__item)->prev->next = (__item)->next;     \
+    (__item)->next->prev = (__item)->prev;     \
+  } while(0)
+
+#define DRMLISTDELINIT(__item)                 \
+  do {                                         \
+    (__item)->prev->next = (__item)->next;     \
+    (__item)->next->prev = (__item)->prev;     \
+    (__item)->next = (__item);                 \
+    (__item)->prev = (__item);                 \
+  } while(0)
+
+#define DRMLISTENTRY(__type, __item, __field)   \
+    ((__type *)(((char *) (__item)) - offsetof(__type, __field)))
+
+#define DRMLISTEMPTY(__item) ((__item)->next == (__item))
+
+#define DRMLISTFOREACHSAFE(__item, __temp, __list)                     \
+       for ((__item) = (__list)->next, (__temp) = (__item)->next;      \
+            (__item) != (__list);                                      \
+            (__item) = (__temp), (__temp) = (__item)->next)
+
+#define DRMLISTFOREACHSAFEREVERSE(__item, __temp, __list)              \
+       for ((__item) = (__list)->prev, (__temp) = (__item)->prev;      \
+            (__item) != (__list);                                      \
+            (__item) = (__temp), (__temp) = (__item)->prev)
diff --git a/libkms/Makefile.am b/libkms/Makefile.am
new file mode 100644 (file)
index 0000000..ad4fd19
--- /dev/null
@@ -0,0 +1,44 @@
+AM_CFLAGS = \
+       $(WARN_CFLAGS) \
+       -I$(top_srcdir)/include/drm \
+       -I$(top_srcdir)
+
+libkms_la_LTLIBRARIES = libkms.la
+libkms_ladir = $(libdir)
+libkms_la_LDFLAGS = -version-number 1:0:0 -no-undefined
+libkms_la_LIBADD =
+
+#if HAVE_LIBUDEV
+#libkms_la_LIBADD += $(LIBUDEV_LIBS)
+#endif
+
+libkms_la_SOURCES = \
+       internal.h \
+       linux.c \
+       intel.c \
+       dumb.c \
+       api.c
+
+if HAVE_VMWGFX
+libkms_la_SOURCES += vmwgfx.c
+endif
+
+if HAVE_NOUVEAU
+libkms_la_SOURCES += nouveau.c
+endif
+
+if HAVE_RADEON
+libkms_la_SOURCES += radeon.c
+endif
+
+if HAVE_SLP
+libkms_la_SOURCES += slp.c
+endif
+
+libkmsincludedir = ${includedir}/libkms
+libkmsinclude_HEADERS = libkms.h
+
+pkgconfigdir = @pkgconfigdir@
+pkgconfig_DATA = libkms.pc
+
+EXTRA_DIST = libkms.pc.in
diff --git a/libkms/api.c b/libkms/api.c
new file mode 100644 (file)
index 0000000..4a05f3d
--- /dev/null
@@ -0,0 +1,138 @@
+/**************************************************************************
+ *
+ * Copyright Â© 2009 VMware, Inc., Palo Alto, CA., USA
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+ * USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ **************************************************************************/
+
+
+#include "config.h"
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include "internal.h"
+
+int kms_create(int fd, struct kms_driver **out)
+{
+       return linux_create(fd, out);
+}
+
+int kms_get_prop(struct kms_driver *kms, unsigned key, unsigned *out)
+{
+       switch (key) {
+       case KMS_BO_TYPE:
+               break;
+       default:
+               return -EINVAL;
+       }
+       return kms->get_prop(kms, key, out);
+}
+
+int kms_destroy(struct kms_driver **kms)
+{
+       if (!(*kms))
+               return 0;
+
+       free(*kms);
+       *kms = NULL;
+       return 0;
+}
+
+int kms_bo_create(struct kms_driver *kms, const unsigned *attr, struct kms_bo **out)
+{
+       unsigned width = 0;
+       unsigned height = 0;
+       enum kms_bo_type type = KMS_BO_TYPE_SCANOUT_X8R8G8B8;
+       int i;
+
+       for (i = 0; attr[i];) {
+               unsigned key = attr[i++];
+               unsigned value = attr[i++];
+
+               switch (key) {
+               case KMS_WIDTH:
+                       width = value;
+                       break;
+               case KMS_HEIGHT:
+                       height = value;
+                       break;
+               case KMS_BO_TYPE:
+                       type = value;
+                       break;
+               default:
+                       return EINVAL;
+               }
+       }
+
+       if (width == 0 || height == 0)
+               return -EINVAL;
+
+       /* XXX sanity check type */
+
+       if (type == KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8 &&
+           (width != 64 || height != 64))
+               return -EINVAL;
+
+       return kms->bo_create(kms, width, height, type, attr, out);
+}
+
+int kms_bo_get_prop(struct kms_bo *bo, unsigned key, unsigned *out)
+{
+       switch (key) {
+       case KMS_PITCH:
+               *out = bo->pitch;
+               break;
+       case KMS_HANDLE:
+               *out = bo->handle;
+               break;
+       default:
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
+int kms_bo_map(struct kms_bo *bo, void **out)
+{
+       return bo->kms->bo_map(bo, out);
+}
+
+int kms_bo_unmap(struct kms_bo *bo)
+{
+       return bo->kms->bo_unmap(bo);
+}
+
+int kms_bo_destroy(struct kms_bo **bo)
+{
+       int ret;
+
+       if (!(*bo))
+               return 0;
+
+       ret = (*bo)->kms->bo_destroy(*bo);
+       if (ret)
+               return ret;
+
+       *bo = NULL;
+       return 0;
+}
diff --git a/libkms/dumb.c b/libkms/dumb.c
new file mode 100755 (executable)
index 0000000..440efb3
--- /dev/null
@@ -0,0 +1,221 @@
+/**************************************************************************
+ *
+ * Copyright Â© 2009 VMware, Inc., Palo Alto, CA., USA
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+ * USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ **************************************************************************/
+
+
+#define HAVE_STDINT_H
+#define _FILE_OFFSET_BITS 64
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "internal.h"
+
+#include <sys/mman.h>
+#include <sys/ioctl.h>
+#include "xf86drm.h"
+
+#include "i915_drm.h"
+
+struct dumb_bo
+{
+       struct kms_bo base;
+       unsigned map_count;
+};
+
+static int
+dumb_get_prop(struct kms_driver *kms, unsigned key, unsigned *out)
+{
+       switch (key) {
+       case KMS_BO_TYPE:
+               *out = KMS_BO_TYPE_SCANOUT_X8R8G8B8 | KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8;
+               break;
+       default:
+               return -EINVAL;
+       }
+       return 0;
+}
+
+static int
+dumb_destroy(struct kms_driver *kms)
+{
+       free(kms);
+       return 0;
+}
+
+static int
+dumb_bo_create(struct kms_driver *kms,
+                const unsigned width, const unsigned height,
+                const enum kms_bo_type type, const unsigned *attr,
+                struct kms_bo **out)
+{
+       struct drm_mode_create_dumb arg;
+       struct dumb_bo *bo;
+       int i, ret;
+
+       for (i = 0; attr[i]; i += 2) {
+               switch (attr[i]) {
+               case KMS_WIDTH:
+               case KMS_HEIGHT:
+                       break;
+               case KMS_BO_TYPE:
+                       break;
+               default:
+                       return -EINVAL;
+               }
+       }
+
+       bo = calloc(1, sizeof(*bo));
+       if (!bo)
+               return -ENOMEM;
+
+       memset(&arg, 0, sizeof(arg));
+
+       /* All BO_TYPE currently are 32bpp formats */
+       arg.bpp = 32;
+       arg.width = width;
+       arg.height = height;
+
+       ret = drmIoctl(kms->fd, DRM_IOCTL_MODE_CREATE_DUMB, &arg);
+       if (ret)
+               goto err_free;
+
+       bo->base.kms = kms;
+       bo->base.handle = arg.handle;
+       bo->base.size = arg.size;
+       bo->base.pitch = arg.pitch;
+
+       *out = &bo->base;
+
+       return 0;
+
+err_free:
+       free(bo);
+       return ret;
+}
+
+static int
+dumb_bo_get_prop(struct kms_bo *bo, unsigned key, unsigned *out)
+{
+       switch (key) {
+       default:
+               return -EINVAL;
+       }
+}
+
+static int
+dumb_bo_map(struct kms_bo *_bo, void **out)
+{
+       struct dumb_bo *bo = (struct dumb_bo *)_bo;
+       struct drm_mode_map_dumb arg;
+       void *map = NULL;
+       int ret;
+
+       if (bo->base.ptr) {
+               bo->map_count++;
+               *out = bo->base.ptr;
+               return 0;
+       }
+
+       memset(&arg, 0, sizeof(arg));
+       arg.handle = bo->base.handle;
+
+       ret = drmIoctl(bo->base.kms->fd, DRM_IOCTL_MODE_MAP_DUMB, &arg);
+       if (ret)
+               return ret;
+
+       map = mmap(0, bo->base.size, PROT_READ | PROT_WRITE, MAP_SHARED, bo->base.kms->fd, arg.offset);
+       if (map == MAP_FAILED)
+               return -errno;
+
+       bo->base.ptr = map;
+       bo->map_count++;
+       *out = bo->base.ptr;
+
+       return 0;
+}
+
+static int
+dumb_bo_unmap(struct kms_bo *_bo)
+{
+       struct dumb_bo *bo = (struct dumb_bo *)_bo;
+       bo->map_count--;
+       return 0;
+}
+
+static int
+dumb_bo_destroy(struct kms_bo *_bo)
+{
+       struct dumb_bo *bo = (struct dumb_bo *)_bo;
+       struct drm_mode_destroy_dumb arg;
+       int ret;
+
+       if (bo->base.ptr) {
+               /* XXX Sanity check map_count */
+               munmap(bo->base.ptr, bo->base.size);
+               bo->base.ptr = NULL;
+       }
+
+       memset(&arg, 0, sizeof(arg));
+       arg.handle = bo->base.handle;
+
+       ret = drmIoctl(bo->base.kms->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &arg);
+       if (ret)
+               return -errno;
+
+       free(bo);
+       return 0;
+}
+
+int
+dumb_create(int fd, struct kms_driver **out)
+{
+       struct kms_driver *kms;
+       int ret;
+       uint64_t cap = 0;
+
+       ret = drmGetCap(fd, DRM_CAP_DUMB_BUFFER, &cap);
+       if (ret || cap == 0)
+               return -EINVAL;
+
+       kms = calloc(1, sizeof(*kms));
+       if (!kms)
+               return -ENOMEM;
+
+       kms->fd = fd;
+
+       kms->bo_create = dumb_bo_create;
+       kms->bo_map = dumb_bo_map;
+       kms->bo_unmap = dumb_bo_unmap;
+       kms->bo_get_prop = dumb_bo_get_prop;
+       kms->bo_destroy = dumb_bo_destroy;
+       kms->get_prop = dumb_get_prop;
+       kms->destroy = dumb_destroy;
+       *out = kms;
+
+       return 0;
+}
diff --git a/libkms/intel.c b/libkms/intel.c
new file mode 100644 (file)
index 0000000..8b8249b
--- /dev/null
@@ -0,0 +1,238 @@
+/**************************************************************************
+ *
+ * Copyright Â© 2009 VMware, Inc., Palo Alto, CA., USA
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+ * USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ **************************************************************************/
+
+
+#define HAVE_STDINT_H
+#define _FILE_OFFSET_BITS 64
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "internal.h"
+
+#include <sys/mman.h>
+#include <sys/ioctl.h>
+#include "xf86drm.h"
+
+#include "i915_drm.h"
+
+struct intel_bo
+{
+       struct kms_bo base;
+       unsigned map_count;
+};
+
+static int
+intel_get_prop(struct kms_driver *kms, unsigned key, unsigned *out)
+{
+       switch (key) {
+       case KMS_BO_TYPE:
+               *out = KMS_BO_TYPE_SCANOUT_X8R8G8B8 | KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8;
+               break;
+       default:
+               return -EINVAL;
+       }
+       return 0;
+}
+
+static int
+intel_destroy(struct kms_driver *kms)
+{
+       free(kms);
+       return 0;
+}
+
+static int
+intel_bo_create(struct kms_driver *kms,
+                const unsigned width, const unsigned height,
+                const enum kms_bo_type type, const unsigned *attr,
+                struct kms_bo **out)
+{
+       struct drm_i915_gem_create arg;
+       unsigned size, pitch;
+       struct intel_bo *bo;
+       int i, ret;
+
+       for (i = 0; attr[i]; i += 2) {
+               switch (attr[i]) {
+               case KMS_WIDTH:
+               case KMS_HEIGHT:
+               case KMS_BO_TYPE:
+                       break;
+               default:
+                       return -EINVAL;
+               }
+       }
+
+       bo = calloc(1, sizeof(*bo));
+       if (!bo)
+               return -ENOMEM;
+
+       if (type == KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8) {
+               pitch = 64 * 4;
+               size = 64 * 64 * 4;
+       } else if (type == KMS_BO_TYPE_SCANOUT_X8R8G8B8) {
+               pitch = width * 4;
+               pitch = (pitch + 512 - 1) & ~(512 - 1);
+               size = pitch * ((height + 4 - 1) & ~(4 - 1));
+       } else {
+               return -EINVAL;
+       }
+
+       memset(&arg, 0, sizeof(arg));
+       arg.size = size;
+
+       ret = drmCommandWriteRead(kms->fd, DRM_I915_GEM_CREATE, &arg, sizeof(arg));
+       if (ret)
+               goto err_free;
+
+       bo->base.kms = kms;
+       bo->base.handle = arg.handle;
+       bo->base.size = size;
+       bo->base.pitch = pitch;
+
+       *out = &bo->base;
+       if (type == KMS_BO_TYPE_SCANOUT_X8R8G8B8 && pitch > 512) {
+               struct drm_i915_gem_set_tiling tile;
+
+               memset(&tile, 0, sizeof(tile));
+               tile.handle = bo->base.handle;
+               tile.tiling_mode = I915_TILING_X;
+               tile.stride = bo->base.pitch;
+
+               ret = drmCommandWriteRead(kms->fd, DRM_I915_GEM_SET_TILING, &tile, sizeof(tile));
+#if 0
+               if (ret) {
+                       kms_bo_destroy(out);
+                       return ret;
+               }
+#endif
+       }
+
+       return 0;
+
+err_free:
+       free(bo);
+       return ret;
+}
+
+static int
+intel_bo_get_prop(struct kms_bo *bo, unsigned key, unsigned *out)
+{
+       switch (key) {
+       default:
+               return -EINVAL;
+       }
+}
+
+static int
+intel_bo_map(struct kms_bo *_bo, void **out)
+{
+       struct intel_bo *bo = (struct intel_bo *)_bo;
+       struct drm_i915_gem_mmap_gtt arg;
+       void *map = NULL;
+       int ret;
+
+       if (bo->base.ptr) {
+               bo->map_count++;
+               *out = bo->base.ptr;
+               return 0;
+       }
+
+       memset(&arg, 0, sizeof(arg));
+       arg.handle = bo->base.handle;
+
+       ret = drmCommandWriteRead(bo->base.kms->fd, DRM_I915_GEM_MMAP_GTT, &arg, sizeof(arg));
+       if (ret)
+               return ret;
+
+       map = mmap(0, bo->base.size, PROT_READ | PROT_WRITE, MAP_SHARED, bo->base.kms->fd, arg.offset);
+       if (map == MAP_FAILED)
+               return -errno;
+
+       bo->base.ptr = map;
+       bo->map_count++;
+       *out = bo->base.ptr;
+
+       return 0;
+}
+
+static int
+intel_bo_unmap(struct kms_bo *_bo)
+{
+       struct intel_bo *bo = (struct intel_bo *)_bo;
+       bo->map_count--;
+       return 0;
+}
+
+static int
+intel_bo_destroy(struct kms_bo *_bo)
+{
+       struct intel_bo *bo = (struct intel_bo *)_bo;
+       struct drm_gem_close arg;
+       int ret;
+
+       if (bo->base.ptr) {
+               /* XXX Sanity check map_count */
+               munmap(bo->base.ptr, bo->base.size);
+               bo->base.ptr = NULL;
+       }
+
+       memset(&arg, 0, sizeof(arg));
+       arg.handle = bo->base.handle;
+
+       ret = drmIoctl(bo->base.kms->fd, DRM_IOCTL_GEM_CLOSE, &arg);
+       if (ret)
+               return -errno;
+
+       free(bo);
+       return 0;
+}
+
+int
+intel_create(int fd, struct kms_driver **out)
+{
+       struct kms_driver *kms;
+
+       kms = calloc(1, sizeof(*kms));
+       if (!kms)
+               return -ENOMEM;
+
+       kms->fd = fd;
+
+       kms->bo_create = intel_bo_create;
+       kms->bo_map = intel_bo_map;
+       kms->bo_unmap = intel_bo_unmap;
+       kms->bo_get_prop = intel_bo_get_prop;
+       kms->bo_destroy = intel_bo_destroy;
+       kms->get_prop = intel_get_prop;
+       kms->destroy = intel_destroy;
+       *out = kms;
+
+       return 0;
+}
diff --git a/libkms/internal.h b/libkms/internal.h
new file mode 100644 (file)
index 0000000..145b4b2
--- /dev/null
@@ -0,0 +1,79 @@
+/**************************************************************************
+ *
+ * Copyright Â© 2009 VMware, Inc., Palo Alto, CA., USA
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+ * USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ **************************************************************************/
+
+
+#ifndef INTERNAL_H_
+#define INTERNAL_H_
+
+#include "libkms.h"
+
+struct kms_driver
+{
+       int (*get_prop)(struct kms_driver *kms, const unsigned key,
+                       unsigned *out);
+       int (*destroy)(struct kms_driver *kms);
+
+       int (*bo_create)(struct kms_driver *kms,
+                        unsigned width,
+                        unsigned height,
+                        enum kms_bo_type type,
+                        const unsigned *attr,
+                        struct kms_bo **out);
+       int (*bo_get_prop)(struct kms_bo *bo, const unsigned key,
+                          unsigned *out);
+       int (*bo_map)(struct kms_bo *bo, void **out);
+       int (*bo_unmap)(struct kms_bo *bo);
+       int (*bo_destroy)(struct kms_bo *bo);
+
+       int fd;
+};
+
+struct kms_bo
+{
+       struct kms_driver *kms;
+       void *ptr;
+       size_t size;
+       size_t offset;
+       size_t pitch;
+       unsigned handle;
+};
+
+int linux_create(int fd, struct kms_driver **out);
+
+int vmwgfx_create(int fd, struct kms_driver **out);
+
+int intel_create(int fd, struct kms_driver **out);
+
+int dumb_create(int fd, struct kms_driver **out);
+
+int nouveau_create(int fd, struct kms_driver **out);
+
+int radeon_create(int fd, struct kms_driver **out);
+
+int slp_create(int fd, struct kms_driver **out);
+
+#endif
diff --git a/libkms/libkms.h b/libkms/libkms.h
new file mode 100644 (file)
index 0000000..4664442
--- /dev/null
@@ -0,0 +1,74 @@
+/**************************************************************************
+ *
+ * Copyright Â© 2009 VMware, Inc., Palo Alto, CA., USA
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+ * USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ **************************************************************************/
+
+
+#ifndef _LIBKMS_H_
+#define _LIBKMS_H_
+
+/**
+ * \file
+ *
+ */
+
+struct kms_driver;
+struct kms_bo;
+
+enum kms_attrib
+{
+       KMS_TERMINATE_PROP_LIST,
+#define KMS_TERMINATE_PROP_LIST KMS_TERMINATE_PROP_LIST
+       KMS_BO_TYPE,
+#define KMS_BO_TYPE KMS_BO_TYPE
+       KMS_WIDTH,
+#define KMS_WIDTH KMS_WIDTH
+       KMS_HEIGHT,
+#define KMS_HEIGHT KMS_HEIGHT
+       KMS_PITCH,
+#define KMS_PITCH KMS_PITCH
+       KMS_HANDLE,
+#define KMS_HANDLE KMS_HANDLE
+};
+
+enum kms_bo_type
+{
+       KMS_BO_TYPE_SCANOUT_X8R8G8B8 = (1 << 0),
+#define KMS_BO_TYPE_SCANOUT_X8R8G8B8 KMS_BO_TYPE_SCANOUT_X8R8G8B8
+       KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8 =  (1 << 1),
+#define KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8 KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8
+};
+
+int kms_create(int fd, struct kms_driver **out);
+int kms_get_prop(struct kms_driver *kms, unsigned key, unsigned *out);
+int kms_destroy(struct kms_driver **kms);
+
+int kms_bo_create(struct kms_driver *kms, const unsigned *attr, struct kms_bo **out);
+int kms_bo_get_prop(struct kms_bo *bo, unsigned key, unsigned *out);
+int kms_bo_map(struct kms_bo *bo, void **out);
+int kms_bo_unmap(struct kms_bo *bo);
+int kms_bo_destroy(struct kms_bo **bo);
+
+#endif
diff --git a/libkms/libkms.pc.in b/libkms/libkms.pc.in
new file mode 100644 (file)
index 0000000..511535a
--- /dev/null
@@ -0,0 +1,10 @@
+prefix=@prefix@
+exec_prefix=@exec_prefix@
+libdir=@libdir@
+includedir=@includedir@
+
+Name: libkms
+Description: Library that abstract aways the different mm interface for kernel drivers
+Version: 1.0.0
+Libs: -L${libdir} -lkms
+Cflags: -I${includedir}/libkms
diff --git a/libkms/linux.c b/libkms/linux.c
new file mode 100644 (file)
index 0000000..75b7cfe
--- /dev/null
@@ -0,0 +1,230 @@
+/**************************************************************************
+ *
+ * Copyright Â© 2009 VMware, Inc., Palo Alto, CA., USA
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+ * USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ **************************************************************************/
+/*
+ * Thanks to krh and jcristau for the tips on
+ * going from fd to pci id via fstat and udev.
+ */
+
+
+#include "config.h"
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <xf86drm.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <sys/stat.h>
+
+#include "internal.h"
+
+#define PATH_SIZE 512
+
+static int
+linux_name_from_sysfs(int fd, char **out)
+{
+       char path[PATH_SIZE+1] = ""; /* initialize to please valgrind */
+       char link[PATH_SIZE+1] = "";
+       struct stat buffer;
+       unsigned maj, min;
+       char* slash_name;
+       int ret;
+
+       /* 
+        * Inside the sysfs directory for the device there is a symlink
+        * to the directory representing the driver module, that path
+        * happens to hold the name of the driver.
+        *
+        * So lets get the symlink for the drm device. Then read the link
+        * and filter out the last directory which happens to be the name
+        * of the driver, which we can use to load the correct interface.
+        *
+        * Thanks to Ray Strode of Plymouth for the code.
+        */
+
+       ret = fstat(fd, &buffer);
+       if (ret)
+               return ret;
+
+       if (!S_ISCHR(buffer.st_mode))
+               return -EINVAL;
+
+       maj = major(buffer.st_rdev);
+       min = minor(buffer.st_rdev);
+
+       snprintf(path, PATH_SIZE, "/sys/dev/char/%d:%d/device/driver", maj, min);
+
+       if (readlink(path, link, PATH_SIZE) < 0)
+               return -EINVAL;
+
+       /* link looks something like this: ../../../bus/pci/drivers/intel */
+       slash_name = strrchr(link, '/');
+       if (!slash_name)
+               return -EINVAL;
+
+       /* copy name and at the same time remove the slash */
+       *out = strdup(slash_name + 1);
+       return 0;
+}
+
+static int
+linux_from_sysfs(int fd, struct kms_driver **out)
+{
+       char *name;
+       int ret;
+
+       ret = linux_name_from_sysfs(fd, &name);
+       if (ret)
+               return ret;
+
+       if (!strcmp(name, "intel"))
+               ret = intel_create(fd, out);
+#ifdef HAVE_VMWGFX
+       else if (!strcmp(name, "vmwgfx"))
+               ret = vmwgfx_create(fd, out);
+#endif
+#ifdef HAVE_NOUVEAU
+       else if (!strcmp(name, "nouveau"))
+               ret = nouveau_create(fd, out);
+#endif
+#ifdef HAVE_RADEON
+       else if (!strcmp(name, "radeon"))
+               ret = radeon_create(fd, out);
+#endif
+#ifdef HAVE_SLP
+       else if (!strcmp(name, "exynos-drm"))
+               ret = slp_create(fd, out);
+#endif
+       else
+               ret = -ENOSYS;
+
+       free(name);
+       return ret;
+}
+
+#if 0
+#define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE
+#include <libudev.h>
+
+struct create_record
+{
+       unsigned vendor;
+       unsigned chip;
+       int (*func)(int fd, struct kms_driver **out);
+};
+
+static struct create_record table[] = {
+       { 0x8086, 0x2a42, intel_create }, /* i965 */
+#ifdef HAVE_VMWGFX
+       { 0x15ad, 0x0405, vmwgfx_create }, /* VMware vGPU */
+#endif
+       { 0, 0, NULL },
+};
+
+static int
+linux_get_pciid_from_fd(int fd, unsigned *vendor_id, unsigned *chip_id)
+{
+       struct udev *udev;
+       struct udev_device *device;
+       struct udev_device *parent;
+       const char *pci_id;
+       struct stat buffer;
+       int ret;
+
+       ret = fstat(fd, &buffer);
+       if (ret)
+               return -EINVAL;
+
+       if (!S_ISCHR(buffer.st_mode))
+               return -EINVAL;
+
+       udev = udev_new();
+       if (!udev)
+               return -ENOMEM;
+
+       device = udev_device_new_from_devnum(udev, 'c', buffer.st_rdev);
+       if (!device)
+               goto err_free_udev;
+
+       parent = udev_device_get_parent(device);
+       if (!parent)
+               goto err_free_device;
+
+       pci_id = udev_device_get_property_value(parent, "PCI_ID");
+       if (!pci_id)
+               goto err_free_device;
+
+       if (sscanf(pci_id, "%x:%x", vendor_id, chip_id) != 2)
+               goto err_free_device;
+
+       udev_device_unref(device);
+       udev_unref(udev);
+
+       return 0;
+
+err_free_device:
+       udev_device_unref(device);
+err_free_udev:
+       udev_unref(udev);
+       return -EINVAL;
+}
+
+static int
+linux_from_udev(int fd, struct kms_driver **out)
+{
+       unsigned vendor_id, chip_id;
+       int ret, i;
+
+       ret = linux_get_pciid_from_fd(fd, &vendor_id, &chip_id);
+       if (ret)
+               return ret;
+
+       for (i = 0; table[i].func; i++)
+               if (table[i].vendor == vendor_id && table[i].chip == chip_id)
+                       return table[i].func(fd, out);
+
+       return -ENOSYS;
+}
+#else
+static int
+linux_from_udev(int fd, struct kms_driver **out)
+{
+       return -ENOSYS;
+}
+#endif
+
+int
+linux_create(int fd, struct kms_driver **out)
+{
+       if (!dumb_create(fd, out))
+               return 0;
+
+       if (!linux_from_udev(fd, out))
+               return 0;
+
+       return linux_from_sysfs(fd, out);
+}
diff --git a/libkms/nouveau.c b/libkms/nouveau.c
new file mode 100644 (file)
index 0000000..0e24a15
--- /dev/null
@@ -0,0 +1,220 @@
+/**************************************************************************
+ *
+ * Copyright Â© 2009 VMware, Inc., Palo Alto, CA., USA
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+ * USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ **************************************************************************/
+
+
+#define HAVE_STDINT_H
+#define _FILE_OFFSET_BITS 64
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "internal.h"
+
+#include <sys/mman.h>
+#include <sys/ioctl.h>
+#include "xf86drm.h"
+
+#include "nouveau_drm.h"
+
+struct nouveau_bo
+{
+       struct kms_bo base;
+       uint64_t map_handle;
+       unsigned map_count;
+};
+
+static int
+nouveau_get_prop(struct kms_driver *kms, unsigned key, unsigned *out)
+{
+       switch (key) {
+       case KMS_BO_TYPE:
+               *out = KMS_BO_TYPE_SCANOUT_X8R8G8B8 | KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8;
+               break;
+       default:
+               return -EINVAL;
+       }
+       return 0;
+}
+
+static int
+nouveau_destroy(struct kms_driver *kms)
+{
+       free(kms);
+       return 0;
+}
+
+static int
+nouveau_bo_create(struct kms_driver *kms,
+                const unsigned width, const unsigned height,
+                const enum kms_bo_type type, const unsigned *attr,
+                struct kms_bo **out)
+{
+       struct drm_nouveau_gem_new arg;
+       unsigned size, pitch;
+       struct nouveau_bo *bo;
+       int i, ret;
+
+       for (i = 0; attr[i]; i += 2) {
+               switch (attr[i]) {
+               case KMS_WIDTH:
+               case KMS_HEIGHT:
+               case KMS_BO_TYPE:
+                       break;
+               default:
+                       return -EINVAL;
+               }
+       }
+
+       bo = calloc(1, sizeof(*bo));
+       if (!bo)
+               return -ENOMEM;
+
+       if (type == KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8) {
+               pitch = 64 * 4;
+               size = 64 * 64 * 4;
+       } else if (type == KMS_BO_TYPE_SCANOUT_X8R8G8B8) {
+               pitch = width * 4;
+               pitch = (pitch + 512 - 1) & ~(512 - 1);
+               size = pitch * height;
+       } else {
+               return -EINVAL;
+       }
+
+       memset(&arg, 0, sizeof(arg));
+       arg.info.size = size;
+       arg.info.domain = NOUVEAU_GEM_DOMAIN_MAPPABLE | NOUVEAU_GEM_DOMAIN_VRAM;
+       arg.info.tile_mode = 0;
+       arg.info.tile_flags = 0;
+       arg.align = 512;
+       arg.channel_hint = 0;
+
+       ret = drmCommandWriteRead(kms->fd, DRM_NOUVEAU_GEM_NEW, &arg, sizeof(arg));
+       if (ret)
+               goto err_free;
+
+       bo->base.kms = kms;
+       bo->base.handle = arg.info.handle;
+       bo->base.size = size;
+       bo->base.pitch = pitch;
+       bo->map_handle = arg.info.map_handle;
+
+       *out = &bo->base;
+
+       return 0;
+
+err_free:
+       free(bo);
+       return ret;
+}
+
+static int
+nouveau_bo_get_prop(struct kms_bo *bo, unsigned key, unsigned *out)
+{
+       switch (key) {
+       default:
+               return -EINVAL;
+       }
+}
+
+static int
+nouveau_bo_map(struct kms_bo *_bo, void **out)
+{
+       struct nouveau_bo *bo = (struct nouveau_bo *)_bo;
+       void *map = NULL;
+
+       if (bo->base.ptr) {
+               bo->map_count++;
+               *out = bo->base.ptr;
+               return 0;
+       }
+
+       map = mmap(0, bo->base.size, PROT_READ | PROT_WRITE, MAP_SHARED, bo->base.kms->fd, bo->map_handle);
+       if (map == MAP_FAILED)
+               return -errno;
+
+       bo->base.ptr = map;
+       bo->map_count++;
+       *out = bo->base.ptr;
+
+       return 0;
+}
+
+static int
+nouveau_bo_unmap(struct kms_bo *_bo)
+{
+       struct nouveau_bo *bo = (struct nouveau_bo *)_bo;
+       bo->map_count--;
+       return 0;
+}
+
+static int
+nouveau_bo_destroy(struct kms_bo *_bo)
+{
+       struct nouveau_bo *bo = (struct nouveau_bo *)_bo;
+       struct drm_gem_close arg;
+       int ret;
+
+       if (bo->base.ptr) {
+               /* XXX Sanity check map_count */
+               munmap(bo->base.ptr, bo->base.size);
+               bo->base.ptr = NULL;
+       }
+
+       memset(&arg, 0, sizeof(arg));
+       arg.handle = bo->base.handle;
+
+       ret = drmIoctl(bo->base.kms->fd, DRM_IOCTL_GEM_CLOSE, &arg);
+       if (ret)
+               return -errno;
+
+       free(bo);
+       return 0;
+}
+
+int
+nouveau_create(int fd, struct kms_driver **out)
+{
+       struct kms_driver *kms;
+
+       kms = calloc(1, sizeof(*kms));
+       if (!kms)
+               return -ENOMEM;
+
+       kms->fd = fd;
+
+       kms->bo_create = nouveau_bo_create;
+       kms->bo_map = nouveau_bo_map;
+       kms->bo_unmap = nouveau_bo_unmap;
+       kms->bo_get_prop = nouveau_bo_get_prop;
+       kms->bo_destroy = nouveau_bo_destroy;
+       kms->get_prop = nouveau_get_prop;
+       kms->destroy = nouveau_destroy;
+       *out = kms;
+
+       return 0;
+}
diff --git a/libkms/radeon.c b/libkms/radeon.c
new file mode 100644 (file)
index 0000000..f5e382a
--- /dev/null
@@ -0,0 +1,242 @@
+/**************************************************************************
+ *
+ * Copyright Â© 2009 VMware, Inc., Palo Alto, CA., USA
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+ * USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ **************************************************************************/
+
+
+#define HAVE_STDINT_H
+#define _FILE_OFFSET_BITS 64
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "internal.h"
+
+#include <sys/mman.h>
+#include <sys/ioctl.h>
+#include "xf86drm.h"
+
+#include "radeon_drm.h"
+
+
+#define ALIGNMENT 512
+
+struct radeon_bo
+{
+       struct kms_bo base;
+       unsigned map_count;
+};
+
+static int
+radeon_get_prop(struct kms_driver *kms, unsigned key, unsigned *out)
+{
+       switch (key) {
+       case KMS_BO_TYPE:
+               *out = KMS_BO_TYPE_SCANOUT_X8R8G8B8 | KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8;
+               break;
+       default:
+               return -EINVAL;
+       }
+       return 0;
+}
+
+static int
+radeon_destroy(struct kms_driver *kms)
+{
+       free(kms);
+       return 0;
+}
+
+static int
+radeon_bo_create(struct kms_driver *kms,
+                const unsigned width, const unsigned height,
+                const enum kms_bo_type type, const unsigned *attr,
+                struct kms_bo **out)
+{
+       struct drm_radeon_gem_create arg;
+       unsigned size, pitch;
+       struct radeon_bo *bo;
+       int i, ret;
+
+       for (i = 0; attr[i]; i += 2) {
+               switch (attr[i]) {
+               case KMS_WIDTH:
+               case KMS_HEIGHT:
+               case KMS_BO_TYPE:
+                       break;
+               default:
+                       return -EINVAL;
+               }
+       }
+
+       switch (type) {
+       case KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8:
+               pitch = 4 * 64;
+               size  = 4 * 64 * 64;
+               break;
+       case KMS_BO_TYPE_SCANOUT_X8R8G8B8:
+               pitch = width * 4;
+               pitch = (pitch + ALIGNMENT - 1) & ~(ALIGNMENT - 1);
+               size  = pitch * height;
+               break;
+       default:
+               return -EINVAL;
+       }
+
+       bo = calloc(1, sizeof(*bo));
+       if (!bo)
+               return -ENOMEM;
+
+       memset(&arg, 0, sizeof(arg));
+       arg.size = size;
+       arg.alignment = ALIGNMENT;
+       arg.initial_domain = RADEON_GEM_DOMAIN_CPU;
+       arg.flags = 0;
+       arg.handle = 0;
+
+       ret = drmCommandWriteRead(kms->fd, DRM_RADEON_GEM_CREATE,
+                                 &arg, sizeof(arg));
+       if (ret)
+               goto err_free;
+
+       bo->base.kms = kms;
+       bo->base.handle = arg.handle;
+       bo->base.size = size;
+       bo->base.pitch = pitch;
+       bo->base.offset = 0;
+       bo->map_count = 0;
+
+       *out = &bo->base;
+
+       return 0;
+
+err_free:
+       free(bo);
+       return ret;
+}
+
+static int
+radeon_bo_get_prop(struct kms_bo *bo, unsigned key, unsigned *out)
+{
+       switch (key) {
+       default:
+               return -EINVAL;
+       }
+}
+
+static int
+radeon_bo_map(struct kms_bo *_bo, void **out)
+{
+       struct radeon_bo *bo = (struct radeon_bo *)_bo;
+       struct drm_radeon_gem_mmap arg;
+       void *map = NULL;
+       int ret;
+
+       if (bo->base.ptr) {
+               bo->map_count++;
+               *out = bo->base.ptr;
+               return 0;
+       }
+
+       memset(&arg, 0, sizeof(arg));
+       arg.handle = bo->base.handle;
+       arg.offset = bo->base.offset;
+       arg.size = (uint64_t)bo->base.size;
+
+       ret = drmCommandWriteRead(bo->base.kms->fd, DRM_RADEON_GEM_MMAP,
+                               &arg, sizeof(arg));
+       if (ret)
+               return -errno;
+
+       map = mmap(0, arg.size, PROT_READ | PROT_WRITE, MAP_SHARED,
+                  bo->base.kms->fd, arg.addr_ptr);
+       if (map == MAP_FAILED)
+               return -errno;
+
+       bo->base.ptr = map;
+       bo->map_count++;
+       *out = bo->base.ptr;
+
+       return 0;
+}
+
+static int
+radeon_bo_unmap(struct kms_bo *_bo)
+{
+       struct radeon_bo *bo = (struct radeon_bo *)_bo;
+       if (--bo->map_count == 0) {
+               munmap(bo->base.ptr, bo->base.size);
+               bo->base.ptr = NULL;
+       }
+       return 0;
+}
+
+static int
+radeon_bo_destroy(struct kms_bo *_bo)
+{
+       struct radeon_bo *bo = (struct radeon_bo *)_bo;
+       struct drm_gem_close arg;
+       int ret;
+
+       if (bo->base.ptr) {
+               /* XXX Sanity check map_count */
+               munmap(bo->base.ptr, bo->base.size);
+               bo->base.ptr = NULL;
+       }
+
+       memset(&arg, 0, sizeof(arg));
+       arg.handle = bo->base.handle;
+
+       ret = drmIoctl(bo->base.kms->fd, DRM_IOCTL_GEM_CLOSE, &arg);
+       if (ret)
+               return -errno;
+
+       free(bo);
+       return 0;
+}
+
+int
+radeon_create(int fd, struct kms_driver **out)
+{
+       struct kms_driver *kms;
+
+       kms = calloc(1, sizeof(*kms));
+       if (!kms)
+               return -ENOMEM;
+
+       kms->fd = fd;
+
+       kms->bo_create = radeon_bo_create;
+       kms->bo_map = radeon_bo_map;
+       kms->bo_unmap = radeon_bo_unmap;
+       kms->bo_get_prop = radeon_bo_get_prop;
+       kms->bo_destroy = radeon_bo_destroy;
+       kms->get_prop = radeon_get_prop;
+       kms->destroy = radeon_destroy;
+       *out = kms;
+
+       return 0;
+}
diff --git a/libkms/slp.c b/libkms/slp.c
new file mode 100644 (file)
index 0000000..263f2ab
--- /dev/null
@@ -0,0 +1,222 @@
+/**************************************************************************
+ *
+ * Copyright Â© 2009 VMware, Inc., Palo Alto, CA., USA
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+ * USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ **************************************************************************/
+
+
+#define HAVE_STDINT_H
+#define _FILE_OFFSET_BITS 64
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "internal.h"
+
+#include <sys/mman.h>
+#include <sys/ioctl.h>
+#include "xf86drm.h"
+
+#include "exynos_drm.h"
+
+struct slp_bo
+{
+       struct kms_bo base;
+       unsigned map_count;
+};
+
+static int
+slp_get_prop(struct kms_driver *kms, unsigned key, unsigned *out)
+{
+       switch (key) {
+       case KMS_BO_TYPE:
+               *out = KMS_BO_TYPE_SCANOUT_X8R8G8B8 | KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8;
+               break;
+       default:
+               return -EINVAL;
+       }
+       return 0;
+}
+
+static int
+slp_destroy(struct kms_driver *kms)
+{
+       free(kms);
+       return 0;
+}
+
+static int
+slp_bo_create(struct kms_driver *kms,
+                const unsigned width, const unsigned height,
+                const enum kms_bo_type type, const unsigned *attr,
+                struct kms_bo **out)
+{
+       struct drm_exynos_gem_create arg;
+       unsigned size, pitch;
+       struct slp_bo *bo;
+       int i, ret;
+
+       for (i = 0; attr[i]; i += 2) {
+               switch (attr[i]) {
+               case KMS_WIDTH:
+               case KMS_HEIGHT:
+               case KMS_BO_TYPE:
+                       break;
+               default:
+                       return -EINVAL;
+               }
+       }
+
+       bo = calloc(1, sizeof(*bo));
+       if (!bo)
+               return -ENOMEM;
+
+       if (type == KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8) {
+               pitch = 64 * 4;
+               size = 64 * 64 * 4;
+       } else if (type == KMS_BO_TYPE_SCANOUT_X8R8G8B8) {
+               pitch = width * 4;
+               pitch = (pitch + 512 - 1) & ~(512 - 1);
+               size = pitch * ((height + 4 - 1) & ~(4 - 1));
+       } else {
+               return -EINVAL;
+       }
+
+       memset(&arg, 0, sizeof(arg));
+       arg.size = size;
+
+       ret = drmCommandWriteRead(kms->fd, DRM_EXYNOS_GEM_CREATE, &arg, sizeof(arg));
+       if (ret)
+               goto err_free;
+
+       bo->base.kms = kms;
+       bo->base.handle = arg.handle;
+       bo->base.size = size;
+       bo->base.pitch = pitch;
+
+       *out = &bo->base;
+
+       return 0;
+
+err_free:
+       free(bo);
+       return ret;
+}
+
+static int
+slp_bo_get_prop(struct kms_bo *bo, unsigned key, unsigned *out)
+{
+       switch (key) {
+       default:
+               return -EINVAL;
+       }
+}
+
+static int
+slp_bo_map(struct kms_bo *_bo, void **out)
+{
+       struct slp_bo *bo = (struct slp_bo *)_bo;
+       struct drm_exynos_gem_map_off arg;
+       void *map = NULL;
+       int ret;
+
+       if (bo->base.ptr) {
+               bo->map_count++;
+               *out = bo->base.ptr;
+               return 0;
+       }
+
+       memset(&arg, 0, sizeof(arg));
+       arg.handle = bo->base.handle;
+
+       ret = drmCommandWriteRead(bo->base.kms->fd, DRM_EXYNOS_GEM_MAP_OFFSET, &arg, sizeof(arg));
+       if (ret)
+               return ret;
+
+       map = mmap(0, bo->base.size, PROT_READ | PROT_WRITE, MAP_SHARED, bo->base.kms->fd, arg.offset);
+       if (map == MAP_FAILED)
+               return -errno;
+
+       bo->base.ptr = map;
+       bo->map_count++;
+       *out = bo->base.ptr;
+
+       return 0;
+}
+
+static int
+slp_bo_unmap(struct kms_bo *_bo)
+{
+       struct slp_bo *bo = (struct slp_bo *)_bo;
+       bo->map_count--;
+       return 0;
+}
+
+static int
+slp_bo_destroy(struct kms_bo *_bo)
+{
+       struct slp_bo *bo = (struct slp_bo *)_bo;
+       struct drm_gem_close arg;
+       int ret;
+
+       if (bo->base.ptr) {
+               /* XXX Sanity check map_count */
+               munmap(bo->base.ptr, bo->base.size);
+               bo->base.ptr = NULL;
+       }
+
+       memset(&arg, 0, sizeof(arg));
+       arg.handle = bo->base.handle;
+
+       ret = drmIoctl(bo->base.kms->fd, DRM_IOCTL_GEM_CLOSE, &arg);
+       if (ret)
+               return -errno;
+
+       free(bo);
+       return 0;
+}
+
+int
+slp_create(int fd, struct kms_driver **out)
+{
+       struct kms_driver *kms;
+
+       kms = calloc(1, sizeof(*kms));
+       if (!kms)
+               return -ENOMEM;
+
+       kms->fd = fd;
+
+       kms->bo_create = slp_bo_create;
+       kms->bo_map = slp_bo_map;
+       kms->bo_unmap = slp_bo_unmap;
+       kms->bo_get_prop = slp_bo_get_prop;
+       kms->bo_destroy = slp_bo_destroy;
+       kms->get_prop = slp_get_prop;
+       kms->destroy = slp_destroy;
+       *out = kms;
+
+       return 0;
+}
diff --git a/libkms/vmwgfx.c b/libkms/vmwgfx.c
new file mode 100644 (file)
index 0000000..d594b3b
--- /dev/null
@@ -0,0 +1,207 @@
+/**************************************************************************
+ *
+ * Copyright Â© 2009 VMware, Inc., Palo Alto, CA., USA
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+ * USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ **************************************************************************/
+
+
+#define HAVE_STDINT_H
+#define _FILE_OFFSET_BITS 64
+
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include "internal.h"
+
+#include <sys/mman.h>
+#include "xf86drm.h"
+#include "vmwgfx_drm.h"
+
+struct vmwgfx_bo
+{
+       struct kms_bo base;
+       uint64_t map_handle;
+       unsigned map_count;
+};
+
+static int
+vmwgfx_get_prop(struct kms_driver *kms, unsigned key, unsigned *out)
+{
+       switch (key) {
+       case KMS_BO_TYPE:
+               *out = KMS_BO_TYPE_SCANOUT_X8R8G8B8 | KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8;
+               break;
+       default:
+               return -EINVAL;
+       }
+       return 0;
+}
+
+static int
+vmwgfx_destroy(struct kms_driver *kms)
+{
+       free(kms);
+       return 0;
+}
+
+static int
+vmwgfx_bo_create(struct kms_driver *kms,
+                const unsigned width, const unsigned height,
+                const enum kms_bo_type type, const unsigned *attr,
+                struct kms_bo **out)
+{
+       struct vmwgfx_bo *bo;
+       int i, ret;
+
+       for (i = 0; attr[i]; i += 2) {
+               switch (attr[i]) {
+               case KMS_WIDTH:
+               case KMS_HEIGHT:
+               case KMS_BO_TYPE:
+                       break;
+               default:
+                       return -EINVAL;
+               }
+       }
+
+       bo = calloc(1, sizeof(*bo));
+       if (!bo)
+               return -EINVAL;
+
+       {
+               union drm_vmw_alloc_dmabuf_arg arg;
+               struct drm_vmw_alloc_dmabuf_req *req = &arg.req;
+               struct drm_vmw_dmabuf_rep *rep = &arg.rep;
+
+               memset(&arg, 0, sizeof(arg));
+               req->size = width * height * 4;
+               bo->base.size = req->size;
+               bo->base.pitch = width * 4;
+               bo->base.kms = kms;
+
+               do {
+                       ret = drmCommandWriteRead(bo->base.kms->fd,
+                                                 DRM_VMW_ALLOC_DMABUF,
+                                                 &arg, sizeof(arg));
+               } while (ret == -ERESTART);
+
+               if (ret)
+                       goto err_free;
+
+               bo->base.handle = rep->handle;
+               bo->map_handle = rep->map_handle;
+               bo->base.handle = rep->cur_gmr_id;
+               bo->base.offset = rep->cur_gmr_offset;
+       }
+
+       *out = &bo->base;
+
+       return 0;
+
+err_free:
+       free(bo);
+       return ret;
+}
+
+static int
+vmwgfx_bo_get_prop(struct kms_bo *bo, unsigned key, unsigned *out)
+{
+       switch (key) {
+       default:
+               return -EINVAL;
+       }
+}
+
+static int
+vmwgfx_bo_map(struct kms_bo *_bo, void **out)
+{
+       struct vmwgfx_bo *bo = (struct vmwgfx_bo *)_bo;
+       void *map;
+
+       if (bo->base.ptr) {
+               bo->map_count++;
+               *out = bo->base.ptr;
+               return 0;
+       }
+
+       map = mmap(NULL, bo->base.size, PROT_READ | PROT_WRITE, MAP_SHARED, bo->base.kms->fd, bo->map_handle);
+       if (map == MAP_FAILED)
+               return -errno;
+
+       bo->base.ptr = map;
+       bo->map_count++;
+       *out = bo->base.ptr;
+
+       return 0;
+}
+
+static int
+vmwgfx_bo_unmap(struct kms_bo *_bo)
+{
+       struct vmwgfx_bo *bo = (struct vmwgfx_bo *)_bo;
+       bo->map_count--;
+       return 0;
+}
+
+static int
+vmwgfx_bo_destroy(struct kms_bo *_bo)
+{
+       struct vmwgfx_bo *bo = (struct vmwgfx_bo *)_bo;
+       struct drm_vmw_unref_dmabuf_arg arg;
+
+       if (bo->base.ptr) {
+               /* XXX Sanity check map_count */
+               munmap(bo->base.ptr, bo->base.size);
+               bo->base.ptr = NULL;
+       }
+
+       memset(&arg, 0, sizeof(arg));
+       arg.handle = bo->base.handle;
+       drmCommandWrite(bo->base.kms->fd, DRM_VMW_UNREF_DMABUF, &arg, sizeof(arg));
+
+       free(bo);
+       return 0;
+}
+
+int
+vmwgfx_create(int fd, struct kms_driver **out)
+{
+       struct kms_driver *kms;
+
+       kms = calloc(1, sizeof(*kms));
+       if (!kms)
+               return -ENOMEM;
+
+       kms->fd = fd;
+
+       kms->bo_create = vmwgfx_bo_create;
+       kms->bo_map = vmwgfx_bo_map;
+       kms->bo_unmap = vmwgfx_bo_unmap;
+       kms->bo_get_prop = vmwgfx_bo_get_prop;
+       kms->bo_destroy = vmwgfx_bo_destroy;
+       kms->get_prop = vmwgfx_get_prop;
+       kms->destroy = vmwgfx_destroy;
+       *out = kms;
+       return 0;
+}
diff --git a/m4/.gitignore b/m4/.gitignore
new file mode 100644 (file)
index 0000000..464ba5c
--- /dev/null
@@ -0,0 +1,5 @@
+libtool.m4
+lt~obsolete.m4
+ltoptions.m4
+ltsugar.m4
+ltversion.m4
diff --git a/nouveau/Makefile.am b/nouveau/Makefile.am
new file mode 100644 (file)
index 0000000..8b89916
--- /dev/null
@@ -0,0 +1,43 @@
+AM_CFLAGS = \
+       $(WARN_CFLAGS) \
+       -I$(top_srcdir) \
+       -I$(top_srcdir)/nouveau \
+       $(PTHREADSTUBS_CFLAGS) \
+       -I$(top_srcdir)/include/drm
+
+libdrm_nouveau_la_LTLIBRARIES = libdrm_nouveau.la
+libdrm_nouveau_ladir = $(libdir)
+libdrm_nouveau_la_LDFLAGS = -version-number 1:0:0 -no-undefined
+libdrm_nouveau_la_LIBADD = ../libdrm.la @PTHREADSTUBS_LIBS@
+
+libdrm_nouveau_la_SOURCES = \
+                           nouveau_device.c \
+                           nouveau_channel.c \
+                           nouveau_pushbuf.c \
+                           nouveau_grobj.c \
+                           nouveau_notifier.c \
+                           nouveau_bo.c \
+                           nouveau_resource.c \
+                           nouveau_private.h \
+                           nouveau_reloc.c
+
+libdrm_nouveaucommonincludedir = ${includedir}/nouveau
+libdrm_nouveaucommoninclude_HEADERS = \
+                               nouveau_device.h \
+                               nouveau_channel.h \
+                               nouveau_grobj.h \
+                               nouveau_notifier.h \
+                               nouveau_pushbuf.h \
+                               nv04_pushbuf.h \
+                               nvc0_pushbuf.h \
+                               nouveau_bo.h \
+                               nouveau_resource.h \
+                               nouveau_reloc.h
+
+
+libdrm_nouveauincludedir = ${includedir}/libdrm
+libdrm_nouveauinclude_HEADERS = \
+                               nouveau_drmif.h
+
+pkgconfigdir = @pkgconfigdir@
+pkgconfig_DATA = libdrm_nouveau.pc
diff --git a/nouveau/libdrm_nouveau.pc.in b/nouveau/libdrm_nouveau.pc.in
new file mode 100644 (file)
index 0000000..c78a28a
--- /dev/null
@@ -0,0 +1,11 @@
+prefix=@prefix@
+exec_prefix=@exec_prefix@
+libdir=@libdir@
+includedir=@includedir@
+
+Name: libdrm_nouveau
+Description: Userspace interface to nouveau kernel DRM services
+Version: 0.6
+Libs: -L${libdir} -ldrm_nouveau
+Cflags: -I${includedir} -I${includedir}/libdrm -I${includedir}/nouveau
+Requires.private: libdrm
diff --git a/nouveau/nouveau_bo.c b/nouveau/nouveau_bo.c
new file mode 100644 (file)
index 0000000..d6bb22d
--- /dev/null
@@ -0,0 +1,549 @@
+/*
+ * Copyright 2007 Nouveau Project
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
+ * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+#include <stdint.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <assert.h>
+
+#include <sys/mman.h>
+
+#include "nouveau_private.h"
+
+int
+nouveau_bo_init(struct nouveau_device *dev)
+{
+       return 0;
+}
+
+void
+nouveau_bo_takedown(struct nouveau_device *dev)
+{
+}
+
+static int
+nouveau_bo_info(struct nouveau_bo_priv *nvbo, struct drm_nouveau_gem_info *arg)
+{
+       nvbo->handle = nvbo->base.handle = arg->handle;
+       nvbo->domain = arg->domain;
+       nvbo->size = arg->size;
+       nvbo->offset = arg->offset;
+       nvbo->map_handle = arg->map_handle;
+       nvbo->base.tile_mode = arg->tile_mode;
+       /* XXX - flag inverted for backwards compatibility */
+       nvbo->base.tile_flags = arg->tile_flags ^ NOUVEAU_GEM_TILE_NONCONTIG;
+       return 0;
+}
+
+static int
+nouveau_bo_allocated(struct nouveau_bo_priv *nvbo)
+{
+       if (nvbo->sysmem || nvbo->handle)
+               return 1;
+       return 0;
+}
+
+static int
+nouveau_bo_ualloc(struct nouveau_bo_priv *nvbo)
+{
+       if (nvbo->user || nvbo->sysmem) {
+               assert(nvbo->sysmem);
+               return 0;
+       }
+
+       nvbo->sysmem = malloc(nvbo->size);
+       if (!nvbo->sysmem)
+               return -ENOMEM;
+
+       return 0;
+}
+
+static void
+nouveau_bo_ufree(struct nouveau_bo_priv *nvbo)
+{
+       if (nvbo->sysmem) {
+               if (!nvbo->user)
+                       free(nvbo->sysmem);
+               nvbo->sysmem = NULL;
+       }
+}
+
+static void
+nouveau_bo_kfree(struct nouveau_bo_priv *nvbo)
+{
+       struct nouveau_device_priv *nvdev = nouveau_device(nvbo->base.device);
+       struct drm_gem_close req;
+
+       if (!nvbo->handle)
+               return;
+
+       if (nvbo->map) {
+               munmap(nvbo->map, nvbo->size);
+               nvbo->map = NULL;
+       }
+
+       req.handle = nvbo->handle;
+       nvbo->handle = 0;
+       drmIoctl(nvdev->fd, DRM_IOCTL_GEM_CLOSE, &req);
+}
+
+static int
+nouveau_bo_kalloc(struct nouveau_bo_priv *nvbo, struct nouveau_channel *chan)
+{
+       struct nouveau_device_priv *nvdev = nouveau_device(nvbo->base.device);
+       struct drm_nouveau_gem_new req;
+       struct drm_nouveau_gem_info *info = &req.info;
+       int ret;
+
+       if (nvbo->handle)
+               return 0;
+
+       req.channel_hint = chan ? chan->id : 0;
+       req.align = nvbo->align;
+
+
+       info->size = nvbo->size;
+       info->domain = 0;
+
+       if (nvbo->flags & NOUVEAU_BO_VRAM)
+               info->domain |= NOUVEAU_GEM_DOMAIN_VRAM;
+       if (nvbo->flags & NOUVEAU_BO_GART)
+               info->domain |= NOUVEAU_GEM_DOMAIN_GART;
+       if (!info->domain) {
+               info->domain |= (NOUVEAU_GEM_DOMAIN_VRAM |
+                                NOUVEAU_GEM_DOMAIN_GART);
+       }
+
+       if (nvbo->flags & NOUVEAU_BO_MAP)
+               info->domain |= NOUVEAU_GEM_DOMAIN_MAPPABLE;
+
+       info->tile_mode = nvbo->base.tile_mode;
+       info->tile_flags = nvbo->base.tile_flags;
+       /* XXX - flag inverted for backwards compatibility */
+       info->tile_flags ^= NOUVEAU_GEM_TILE_NONCONTIG;
+       if (!nvdev->has_bo_usage)
+               info->tile_flags &= NOUVEAU_GEM_TILE_LAYOUT_MASK;
+
+       ret = drmCommandWriteRead(nvdev->fd, DRM_NOUVEAU_GEM_NEW,
+                                 &req, sizeof(req));
+       if (ret)
+               return ret;
+
+       nouveau_bo_info(nvbo, &req.info);
+       return 0;
+}
+
+static int
+nouveau_bo_kmap(struct nouveau_bo_priv *nvbo)
+{
+       struct nouveau_device_priv *nvdev = nouveau_device(nvbo->base.device);
+
+       if (nvbo->map)
+               return 0;
+
+       if (!nvbo->map_handle)
+               return -EINVAL;
+
+       nvbo->map = mmap(0, nvbo->size, PROT_READ | PROT_WRITE,
+                        MAP_SHARED, nvdev->fd, nvbo->map_handle);
+       if (nvbo->map == MAP_FAILED) {
+               nvbo->map = NULL;
+               return -errno;
+       }
+
+       return 0;
+}
+
+int
+nouveau_bo_new_tile(struct nouveau_device *dev, uint32_t flags, int align,
+                   int size, uint32_t tile_mode, uint32_t tile_flags,
+                   struct nouveau_bo **bo)
+{
+       struct nouveau_bo_priv *nvbo;
+       int ret;
+
+       if (!dev || !bo || *bo)
+               return -EINVAL;
+
+       nvbo = calloc(1, sizeof(struct nouveau_bo_priv));
+       if (!nvbo)
+               return -ENOMEM;
+       nvbo->base.device = dev;
+       nvbo->base.size = size;
+       nvbo->base.tile_mode = tile_mode;
+       nvbo->base.tile_flags = tile_flags;
+
+       nvbo->refcount = 1;
+       nvbo->flags = flags;
+       nvbo->size = size;
+       nvbo->align = align;
+
+       if (flags & (NOUVEAU_BO_VRAM | NOUVEAU_BO_GART)) {
+               ret = nouveau_bo_kalloc(nvbo, NULL);
+               if (ret) {
+                       nouveau_bo_ref(NULL, (void *)&nvbo);
+                       return ret;
+               }
+       }
+
+       *bo = &nvbo->base;
+       return 0;
+}
+
+int
+nouveau_bo_new(struct nouveau_device *dev, uint32_t flags, int align,
+              int size, struct nouveau_bo **bo)
+{
+       return nouveau_bo_new_tile(dev, flags, align, size, 0, 0, bo);
+}
+
+int
+nouveau_bo_user(struct nouveau_device *dev, void *ptr, int size,
+               struct nouveau_bo **bo)
+{
+       struct nouveau_bo_priv *nvbo;
+       int ret;
+
+       ret = nouveau_bo_new(dev, NOUVEAU_BO_MAP, 0, size, bo);
+       if (ret)
+               return ret;
+       nvbo = nouveau_bo(*bo);
+
+       nvbo->sysmem = ptr;
+       nvbo->user = 1;
+       return 0;
+}
+
+int
+nouveau_bo_wrap(struct nouveau_device *dev, uint32_t handle,
+               struct nouveau_bo **bo)
+{
+       struct nouveau_device_priv *nvdev = nouveau_device(dev);
+       struct drm_nouveau_gem_info req;
+       struct nouveau_bo_priv *nvbo;
+       int ret;
+
+       ret = nouveau_bo_new(dev, 0, 0, 0, bo);
+       if (ret)
+               return ret;
+       nvbo = nouveau_bo(*bo);
+
+       req.handle = handle;
+       ret = drmCommandWriteRead(nvdev->fd, DRM_NOUVEAU_GEM_INFO,
+                                 &req, sizeof(req));
+       if (ret) {
+               nouveau_bo_ref(NULL, bo);
+               return ret;
+       }
+
+       nouveau_bo_info(nvbo, &req);
+       nvbo->base.size = nvbo->size;
+       return 0;
+}
+
+int
+nouveau_bo_handle_get(struct nouveau_bo *bo, uint32_t *handle)
+{
+       struct nouveau_device_priv *nvdev = nouveau_device(bo->device);
+       struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
+       int ret;
+       if (!bo || !handle)
+               return -EINVAL;
+
+       if (!nvbo->global_handle) {
+               struct drm_gem_flink req;
+               ret = nouveau_bo_kalloc(nvbo, NULL);
+               if (ret)
+                       return ret;
+
+               req.handle = nvbo->handle;
+               ret = drmIoctl(nvdev->fd, DRM_IOCTL_GEM_FLINK, &req);
+               if (ret) {
+                       nouveau_bo_kfree(nvbo);
+                       return ret;
+               }
+
+               nvbo->global_handle = req.name;
+       }
+       *handle = nvbo->global_handle;
+       return 0;
+}
+int
+nouveau_bo_handle_ref(struct nouveau_device *dev, uint32_t handle,
+                     struct nouveau_bo **bo)
+{
+       struct nouveau_device_priv *nvdev = nouveau_device(dev);
+       struct nouveau_bo_priv *nvbo;
+       struct drm_gem_open req;
+       int ret;
+
+       req.name = handle;
+       ret = drmIoctl(nvdev->fd, DRM_IOCTL_GEM_OPEN, &req);
+       if (ret) {
+               nouveau_bo_ref(NULL, bo);
+               return ret;
+       }
+
+       ret = nouveau_bo_wrap(dev, req.handle, bo);
+       if (ret) {
+               nouveau_bo_ref(NULL, bo);
+               return ret;
+       }
+
+       nvbo = nouveau_bo(*bo);
+       nvbo->base.handle = nvbo->handle;
+       return 0;
+} 
+
+static void
+nouveau_bo_del(struct nouveau_bo **bo)
+{
+       struct nouveau_bo_priv *nvbo;
+
+       if (!bo || !*bo)
+               return;
+       nvbo = nouveau_bo(*bo);
+       *bo = NULL;
+
+       if (--nvbo->refcount)
+               return;
+
+       if (nvbo->pending) {
+               nvbo->pending = NULL;
+               nouveau_pushbuf_flush(nvbo->pending_channel, 0);
+       }
+
+       nouveau_bo_ufree(nvbo);
+       nouveau_bo_kfree(nvbo);
+       free(nvbo);
+}
+
+int
+nouveau_bo_ref(struct nouveau_bo *ref, struct nouveau_bo **pbo)
+{
+       if (!pbo)
+               return -EINVAL;
+
+       if (ref)
+               nouveau_bo(ref)->refcount++;
+
+       if (*pbo)
+               nouveau_bo_del(pbo);
+
+       *pbo = ref;
+       return 0;
+}
+
+static int
+nouveau_bo_wait(struct nouveau_bo *bo, int cpu_write, int no_wait, int no_block)
+{
+       struct nouveau_device_priv *nvdev = nouveau_device(bo->device);
+       struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
+       struct drm_nouveau_gem_cpu_prep req;
+       int ret;
+
+       if (!nvbo->global_handle && !nvbo->write_marker && !cpu_write)
+               return 0;
+
+       if (nvbo->pending &&
+           (nvbo->pending->write_domains || cpu_write)) {
+               nvbo->pending = NULL;
+               nouveau_pushbuf_flush(nvbo->pending_channel, 0);
+       }
+
+       req.handle = nvbo->handle;
+       req.flags = 0;
+       if (cpu_write)
+               req.flags |= NOUVEAU_GEM_CPU_PREP_WRITE;
+       if (no_wait)
+               req.flags |= NOUVEAU_GEM_CPU_PREP_NOWAIT;
+       if (no_block)
+               req.flags |= NOUVEAU_GEM_CPU_PREP_NOBLOCK;
+
+       do {
+               ret = drmCommandWrite(nvdev->fd, DRM_NOUVEAU_GEM_CPU_PREP,
+                                     &req, sizeof(req));
+       } while (ret == -EAGAIN);
+       if (ret)
+               return ret;
+
+       if (ret == 0)
+               nvbo->write_marker = 0;
+       return 0;
+}
+
+int
+nouveau_bo_map_range(struct nouveau_bo *bo, uint32_t delta, uint32_t size,
+                    uint32_t flags)
+{
+       struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
+       int ret;
+
+       if (!nvbo || bo->map)
+               return -EINVAL;
+
+       if (!nouveau_bo_allocated(nvbo)) {
+               if (nvbo->flags & (NOUVEAU_BO_VRAM | NOUVEAU_BO_GART)) {
+                       ret = nouveau_bo_kalloc(nvbo, NULL);
+                       if (ret)
+                               return ret;
+               }
+
+               if (!nouveau_bo_allocated(nvbo)) {
+                       ret = nouveau_bo_ualloc(nvbo);
+                       if (ret)
+                               return ret;
+               }
+       }
+
+       if (nvbo->sysmem) {
+               bo->map = (char *)nvbo->sysmem + delta;
+       } else {
+               ret = nouveau_bo_kmap(nvbo);
+               if (ret)
+                       return ret;
+
+               if (!(flags & NOUVEAU_BO_NOSYNC)) {
+                       ret = nouveau_bo_wait(bo, (flags & NOUVEAU_BO_WR),
+                                             (flags & NOUVEAU_BO_NOWAIT), 0);
+                       if (ret)
+                               return ret;
+
+                       nvbo->map_refcnt++;
+               }
+
+               bo->map = (char *)nvbo->map + delta;
+       }
+
+       return 0;
+}
+
+void
+nouveau_bo_map_flush(struct nouveau_bo *bo, uint32_t delta, uint32_t size)
+{
+}
+
+int
+nouveau_bo_map(struct nouveau_bo *bo, uint32_t flags)
+{
+       return nouveau_bo_map_range(bo, 0, bo->size, flags);
+}
+
+void
+nouveau_bo_unmap(struct nouveau_bo *bo)
+{
+       struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
+
+       if (bo->map && !nvbo->sysmem && nvbo->map_refcnt) {
+               struct nouveau_device_priv *nvdev = nouveau_device(bo->device);
+               struct drm_nouveau_gem_cpu_fini req;
+
+               req.handle = nvbo->handle;
+               drmCommandWrite(nvdev->fd, DRM_NOUVEAU_GEM_CPU_FINI,
+                               &req, sizeof(req));
+               nvbo->map_refcnt--;
+       }
+
+       bo->map = NULL;
+}
+
+int
+nouveau_bo_busy(struct nouveau_bo *bo, uint32_t access)
+{
+       return nouveau_bo_wait(bo, (access & NOUVEAU_BO_WR), 1, 1);
+}
+
+uint32_t
+nouveau_bo_pending(struct nouveau_bo *bo)
+{
+       struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
+       uint32_t flags;
+
+       if (!nvbo->pending)
+               return 0;
+
+       flags = 0;
+       if (nvbo->pending->read_domains)
+               flags |= NOUVEAU_BO_RD;
+       if (nvbo->pending->write_domains)
+               flags |= NOUVEAU_BO_WR;
+
+       return flags;
+}
+
+struct drm_nouveau_gem_pushbuf_bo *
+nouveau_bo_emit_buffer(struct nouveau_channel *chan, struct nouveau_bo *bo)
+{
+       struct nouveau_pushbuf_priv *nvpb = &nouveau_channel(chan)->pb;
+       struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
+       struct drm_nouveau_gem_pushbuf_bo *pbbo;
+       struct nouveau_bo *ref = NULL;
+       int ret;
+
+       if (nvbo->pending)
+               return nvbo->pending;
+
+       if (!nvbo->handle) {
+               ret = nouveau_bo_kalloc(nvbo, chan);
+               if (ret)
+                       return NULL;
+
+               if (nvbo->sysmem) {
+                       void *sysmem_tmp = nvbo->sysmem;
+
+                       nvbo->sysmem = NULL;
+                       ret = nouveau_bo_map(bo, NOUVEAU_BO_WR);
+                       if (ret)
+                               return NULL;
+                       nvbo->sysmem = sysmem_tmp;
+
+                       memcpy(bo->map, nvbo->sysmem, nvbo->base.size);
+                       nouveau_bo_ufree(nvbo);
+                       nouveau_bo_unmap(bo);
+               }
+       }
+
+       if (nvpb->nr_buffers >= NOUVEAU_GEM_MAX_BUFFERS)
+               return NULL;
+       pbbo = nvpb->buffers + nvpb->nr_buffers++;
+       nvbo->pending = pbbo;
+       nvbo->pending_channel = chan;
+       nvbo->pending_refcnt = 0;
+
+       nouveau_bo_ref(bo, &ref);
+       pbbo->user_priv = (uint64_t)(unsigned long)ref;
+       pbbo->handle = nvbo->handle;
+       pbbo->valid_domains = NOUVEAU_GEM_DOMAIN_VRAM | NOUVEAU_GEM_DOMAIN_GART;
+       pbbo->read_domains = 0;
+       pbbo->write_domains = 0;
+       pbbo->presumed.domain = nvbo->domain;
+       pbbo->presumed.offset = nvbo->offset;
+       pbbo->presumed.valid = 1;
+       return pbbo;
+}
diff --git a/nouveau/nouveau_bo.h b/nouveau/nouveau_bo.h
new file mode 100644 (file)
index 0000000..3a1f2d4
--- /dev/null
@@ -0,0 +1,104 @@
+/*
+ * Copyright 2007 Nouveau Project
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
+ * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef __NOUVEAU_BO_H__
+#define __NOUVEAU_BO_H__
+
+/* Relocation/Buffer type flags */
+#define NOUVEAU_BO_VRAM   (1 << 0)
+#define NOUVEAU_BO_GART   (1 << 1)
+#define NOUVEAU_BO_RD     (1 << 2)
+#define NOUVEAU_BO_WR     (1 << 3)
+#define NOUVEAU_BO_RDWR   (NOUVEAU_BO_RD | NOUVEAU_BO_WR)
+#define NOUVEAU_BO_MAP    (1 << 4)
+#define NOUVEAU_BO_LOW    (1 << 6)
+#define NOUVEAU_BO_HIGH   (1 << 7)
+#define NOUVEAU_BO_OR     (1 << 8)
+#define NOUVEAU_BO_INVAL  (1 << 12)
+#define NOUVEAU_BO_NOSYNC (1 << 13)
+#define NOUVEAU_BO_NOWAIT (1 << 14)
+#define NOUVEAU_BO_IFLUSH (1 << 15)
+#define NOUVEAU_BO_DUMMY  (1 << 31)
+
+#define NOUVEAU_BO_TILE_LAYOUT_MASK 0x0000ff00
+#define NOUVEAU_BO_TILE_16BPP       0x00000001
+#define NOUVEAU_BO_TILE_32BPP       0x00000002
+#define NOUVEAU_BO_TILE_ZETA        0x00000004
+#define NOUVEAU_BO_TILE_SCANOUT     0x00000008
+
+struct nouveau_bo {
+       struct nouveau_device *device;
+       uint32_t handle;
+
+       uint64_t size;
+       void *map;
+
+       uint32_t tile_mode;
+       uint32_t tile_flags;
+};
+
+int
+nouveau_bo_new(struct nouveau_device *, uint32_t flags, int align, int size,
+              struct nouveau_bo **);
+
+int
+nouveau_bo_new_tile(struct nouveau_device *, uint32_t flags, int align,
+                   int size, uint32_t tile_mode, uint32_t tile_flags,
+                   struct nouveau_bo **);
+
+int
+nouveau_bo_user(struct nouveau_device *, void *ptr, int size,
+               struct nouveau_bo **);
+
+int
+nouveau_bo_wrap(struct nouveau_device *, uint32_t handle, struct nouveau_bo **);
+
+int
+nouveau_bo_handle_get(struct nouveau_bo *, uint32_t *);
+
+int
+nouveau_bo_handle_ref(struct nouveau_device *, uint32_t handle,
+                     struct nouveau_bo **);
+
+int
+nouveau_bo_ref(struct nouveau_bo *, struct nouveau_bo **);
+
+int
+nouveau_bo_map_range(struct nouveau_bo *, uint32_t delta, uint32_t size,
+                    uint32_t flags);
+
+void
+nouveau_bo_map_flush(struct nouveau_bo *, uint32_t delta, uint32_t size);
+
+int
+nouveau_bo_map(struct nouveau_bo *, uint32_t flags);
+
+void
+nouveau_bo_unmap(struct nouveau_bo *);
+
+int
+nouveau_bo_busy(struct nouveau_bo *, uint32_t access);
+
+uint32_t
+nouveau_bo_pending(struct nouveau_bo *);
+
+#endif
diff --git a/nouveau/nouveau_channel.c b/nouveau/nouveau_channel.c
new file mode 100644 (file)
index 0000000..96fa03b
--- /dev/null
@@ -0,0 +1,142 @@
+/*
+ * Copyright 2007 Nouveau Project
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
+ * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+#include "nouveau_private.h"
+
+int
+nouveau_channel_alloc(struct nouveau_device *dev, uint32_t fb_ctxdma,
+                     uint32_t tt_ctxdma, int pushbuf_size,
+                     struct nouveau_channel **chan)
+{
+       struct nouveau_device_priv *nvdev = nouveau_device(dev);
+       struct nouveau_channel_priv *nvchan;
+       unsigned i;
+       int ret;
+
+       if (!nvdev || !chan || *chan)
+           return -EINVAL;
+
+       nvchan = calloc(1, sizeof(struct nouveau_channel_priv));
+       if (!nvchan)
+               return -ENOMEM;
+       nvchan->base.device = dev;
+
+       nvchan->drm.fb_ctxdma_handle = fb_ctxdma;
+       nvchan->drm.tt_ctxdma_handle = tt_ctxdma;
+       ret = drmCommandWriteRead(nvdev->fd, DRM_NOUVEAU_CHANNEL_ALLOC,
+                                 &nvchan->drm, sizeof(nvchan->drm));
+       if (ret) {
+               free(nvchan);
+               return ret;
+       }
+
+       nvchan->base.id = nvchan->drm.channel;
+       if (nouveau_grobj_ref(&nvchan->base, nvchan->drm.fb_ctxdma_handle,
+                             &nvchan->base.vram) ||
+           nouveau_grobj_ref(&nvchan->base, nvchan->drm.tt_ctxdma_handle,
+                             &nvchan->base.gart)) {
+               nouveau_channel_free((void *)&nvchan);
+               return -EINVAL;
+       }
+
+       /* Mark all DRM-assigned subchannels as in-use */
+       for (i = 0; i < nvchan->drm.nr_subchan; i++) {
+               struct nouveau_grobj_priv *gr = calloc(1, sizeof(*gr));
+
+               gr->base.bound = NOUVEAU_GROBJ_BOUND_EXPLICIT;
+               gr->base.subc = i;
+               gr->base.handle = nvchan->drm.subchan[i].handle;
+               gr->base.grclass = nvchan->drm.subchan[i].grclass;
+               gr->base.channel = &nvchan->base;
+
+               nvchan->base.subc[i].gr = &gr->base;
+       }
+
+       if (dev->chipset < 0xc0) {
+               ret = nouveau_bo_wrap(dev, nvchan->drm.notifier_handle,
+                                     &nvchan->notifier_bo);
+               if (!ret)
+                       ret = nouveau_bo_map(nvchan->notifier_bo,
+                                            NOUVEAU_BO_RDWR);
+               if (ret) {
+                       nouveau_channel_free((void *)&nvchan);
+                       return ret;
+               }
+
+               ret = nouveau_grobj_alloc(&nvchan->base, 0x00000000, 0x0030,
+                                         &nvchan->base.nullobj);
+               if (ret) {
+                       nouveau_channel_free((void *)&nvchan);
+                       return ret;
+               }
+       }
+
+       ret = nouveau_pushbuf_init(&nvchan->base, pushbuf_size);
+       if (ret) {
+               nouveau_channel_free((void *)&nvchan);
+               return ret;
+       }
+
+       *chan = &nvchan->base;
+       return 0;
+}
+
+void
+nouveau_channel_free(struct nouveau_channel **chan)
+{
+       struct nouveau_channel_priv *nvchan;
+       struct nouveau_device_priv *nvdev;
+       struct drm_nouveau_channel_free cf;
+       unsigned i;
+
+       if (!chan || !*chan)
+               return;
+       nvchan = nouveau_channel(*chan);
+       (*chan)->flush_notify = NULL;
+       *chan = NULL;
+       nvdev = nouveau_device(nvchan->base.device);
+
+       FIRE_RING(&nvchan->base);
+
+       nouveau_pushbuf_fini(&nvchan->base);
+       if (nvchan->notifier_bo) {
+               nouveau_bo_unmap(nvchan->notifier_bo);
+               nouveau_bo_ref(NULL, &nvchan->notifier_bo);
+       }
+
+       for (i = 0; i < nvchan->drm.nr_subchan; i++)
+               free(nvchan->base.subc[i].gr);
+
+       nouveau_grobj_free(&nvchan->base.vram);
+       nouveau_grobj_free(&nvchan->base.gart);
+       nouveau_grobj_free(&nvchan->base.nullobj);
+
+       cf.channel = nvchan->drm.channel;
+       drmCommandWrite(nvdev->fd, DRM_NOUVEAU_CHANNEL_FREE, &cf, sizeof(cf));
+       free(nvchan);
+}
+
+
diff --git a/nouveau/nouveau_channel.h b/nouveau/nouveau_channel.h
new file mode 100644 (file)
index 0000000..d61a4c0
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2007 Nouveau Project
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
+ * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef __NOUVEAU_CHANNEL_H__
+#define __NOUVEAU_CHANNEL_H__
+
+struct nouveau_subchannel {
+       struct nouveau_grobj *gr;
+       unsigned sequence;
+};
+
+struct nouveau_channel {
+       uint32_t *cur;
+       uint32_t *end;
+
+       struct nouveau_device *device;
+       int id;
+
+       struct nouveau_grobj *nullobj;
+       struct nouveau_grobj *vram;
+       struct nouveau_grobj *gart;
+
+       void *user_private;
+       void (*hang_notify)(struct nouveau_channel *);
+       void (*flush_notify)(struct nouveau_channel *);
+
+       struct nouveau_subchannel subc[8];
+       unsigned subc_sequence;
+};
+
+int
+nouveau_channel_alloc(struct nouveau_device *, uint32_t fb, uint32_t tt,
+                     int pushbuf_size, struct nouveau_channel **);
+
+void
+nouveau_channel_free(struct nouveau_channel **);
+
+#endif
diff --git a/nouveau/nouveau_device.c b/nouveau/nouveau_device.c
new file mode 100644 (file)
index 0000000..425c5d2
--- /dev/null
@@ -0,0 +1,198 @@
+/*
+ * Copyright 2007 Nouveau Project
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
+ * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+
+#include "nouveau_private.h"
+
+int
+nouveau_device_open_existing(struct nouveau_device **dev, int close,
+                            int fd, drm_context_t ctx)
+{
+       struct nouveau_device_priv *nvdev;
+       drmVersionPtr ver;
+       uint64_t value;
+       int ret;
+
+       if (!dev || *dev)
+           return -EINVAL;
+
+       nvdev = calloc(1, sizeof(*nvdev));
+       if (!nvdev)
+           return -ENOMEM;
+       nvdev->fd = fd;
+       nvdev->ctx = ctx;
+       nvdev->needs_close = close;
+
+       ver = drmGetVersion(fd);
+       if (!ver) {
+               nouveau_device_close((void *)&nvdev);
+               return -EINVAL;
+       }
+
+       if ((ver->version_major == 0 && ver->version_patchlevel != 16) ||
+            ver->version_major > 1) {
+               nouveau_device_close((void *)&nvdev);
+               return -EINVAL;
+       }
+
+       drmFreeVersion(ver);
+
+       ret = nouveau_device_get_param(&nvdev->base,
+                                      NOUVEAU_GETPARAM_VM_VRAM_BASE, &value);
+       if (ret) {
+               nouveau_device_close((void *)&nvdev);
+               return ret;
+       }
+       nvdev->base.vm_vram_base = value;
+
+       ret = nouveau_device_get_param(&nvdev->base,
+                                      NOUVEAU_GETPARAM_FB_SIZE, &value);
+       if (ret) {
+               nouveau_device_close((void *)&nvdev);
+               return ret;
+       }
+       nvdev->base.vm_vram_size = value;
+
+       ret = nouveau_device_get_param(&nvdev->base,
+                                      NOUVEAU_GETPARAM_AGP_SIZE, &value);
+       if (ret) {
+               nouveau_device_close((void *)&nvdev);
+               return ret;
+       }
+       nvdev->base.vm_gart_size = value;
+
+       ret = nouveau_bo_init(&nvdev->base);
+       if (ret) {
+               nouveau_device_close((void *)&nvdev);
+               return ret;
+       }
+
+       ret = nouveau_device_get_param(&nvdev->base,
+                                      NOUVEAU_GETPARAM_CHIPSET_ID, &value);
+       if (ret) {
+               nouveau_device_close((void *)&nvdev);
+               return ret;
+       }
+       nvdev->base.chipset = value;
+
+       ret = nouveau_device_get_param(&nvdev->base,
+                                      NOUVEAU_GETPARAM_HAS_BO_USAGE, &value);
+       if (!ret)
+               nvdev->has_bo_usage = value;
+
+       *dev = &nvdev->base;
+       return 0;
+}
+
+int
+nouveau_device_open(struct nouveau_device **dev, const char *busid)
+{
+       drm_context_t ctx;
+       int fd, ret;
+
+       if (!dev || *dev)
+               return -EINVAL;
+
+       fd = drmOpen("nouveau", busid);
+       if (fd < 0)
+               return -EINVAL;
+
+       ret = drmCreateContext(fd, &ctx);
+       if (ret) {
+               drmClose(fd);
+               return ret;
+       }
+
+       ret = nouveau_device_open_existing(dev, 1, fd, ctx);
+       if (ret) {
+           drmDestroyContext(fd, ctx);
+           drmClose(fd);
+           return ret;
+       }
+
+       return 0;
+}
+
+void
+nouveau_device_close(struct nouveau_device **dev)
+{
+       struct nouveau_device_priv *nvdev;
+
+       if (!dev || !*dev)
+               return;
+       nvdev = nouveau_device(*dev);
+       *dev = NULL;
+
+       nouveau_bo_takedown(&nvdev->base);
+
+       if (nvdev->needs_close) {
+               drmDestroyContext(nvdev->fd, nvdev->ctx);
+               drmClose(nvdev->fd);
+       }
+       free(nvdev);
+}
+
+int
+nouveau_device_get_param(struct nouveau_device *dev,
+                        uint64_t param, uint64_t *value)
+{
+       struct nouveau_device_priv *nvdev = nouveau_device(dev);
+       struct drm_nouveau_getparam g;
+       int ret;
+
+       if (!nvdev || !value)
+               return -EINVAL;
+
+       g.param = param;
+       ret = drmCommandWriteRead(nvdev->fd, DRM_NOUVEAU_GETPARAM,
+                                 &g, sizeof(g));
+       if (ret)
+               return ret;
+
+       *value = g.value;
+       return 0;
+}
+
+int
+nouveau_device_set_param(struct nouveau_device *dev,
+                        uint64_t param, uint64_t value)
+{
+       struct nouveau_device_priv *nvdev = nouveau_device(dev);
+       struct drm_nouveau_setparam s;
+       int ret;
+
+       if (!nvdev)
+               return -EINVAL;
+
+       s.param = param;
+       s.value = value;
+       ret = drmCommandWriteRead(nvdev->fd, DRM_NOUVEAU_SETPARAM,
+                                 &s, sizeof(s));
+       if (ret)
+               return ret;
+
+       return 0;
+}
+
diff --git a/nouveau/nouveau_device.h b/nouveau/nouveau_device.h
new file mode 100644 (file)
index 0000000..c0d9333
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * Copyright 2007 Nouveau Project
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
+ * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef __NOUVEAU_DEVICE_H__
+#define __NOUVEAU_DEVICE_H__
+
+struct nouveau_device {
+       unsigned chipset;
+       uint64_t vm_vram_base;
+       uint64_t vm_vram_size;
+       uint64_t vm_gart_size;
+};
+
+#endif
diff --git a/nouveau/nouveau_drmif.h b/nouveau/nouveau_drmif.h
new file mode 100644 (file)
index 0000000..ec226a2
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2008 Nouveau Project
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
+ * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef __NOUVEAU_DRMIF_H__
+#define __NOUVEAU_DRMIF_H__
+
+#include <stdint.h>
+#include <xf86drm.h>
+
+#include "nouveau_device.h"
+
+struct nouveau_device_priv {
+       struct nouveau_device base;
+
+       int fd;
+       drm_context_t ctx;
+       drmLock *lock;
+       int needs_close;
+       int has_bo_usage;
+};
+#define nouveau_device(n) ((struct nouveau_device_priv *)(n))
+
+int
+nouveau_device_open_existing(struct nouveau_device **, int close,
+                            int fd, drm_context_t ctx);
+
+int
+nouveau_device_open(struct nouveau_device **, const char *busid);
+
+void
+nouveau_device_close(struct nouveau_device **);
+
+int
+nouveau_device_get_param(struct nouveau_device *, uint64_t param, uint64_t *v);
+
+int
+nouveau_device_set_param(struct nouveau_device *, uint64_t param, uint64_t val);
+
+#endif
diff --git a/nouveau/nouveau_grobj.c b/nouveau/nouveau_grobj.c
new file mode 100644 (file)
index 0000000..c6b98f1
--- /dev/null
@@ -0,0 +1,147 @@
+/*
+ * Copyright 2007 Nouveau Project
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
+ * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <stdlib.h>
+#include <errno.h>
+
+#include "nouveau_private.h"
+
+int
+nouveau_grobj_alloc(struct nouveau_channel *chan, uint32_t handle,
+                   int class, struct nouveau_grobj **grobj)
+{
+       struct nouveau_device_priv *nvdev = nouveau_device(chan->device);
+       struct nouveau_grobj_priv *nvgrobj;
+       struct drm_nouveau_grobj_alloc g;
+       int ret;
+
+       if (!nvdev || !grobj || *grobj)
+               return -EINVAL;
+
+       nvgrobj = calloc(1, sizeof(*nvgrobj));
+       if (!nvgrobj)
+               return -ENOMEM;
+       nvgrobj->base.channel = chan;
+       nvgrobj->base.handle  = handle;
+       nvgrobj->base.grclass = class;
+       nvgrobj->base.bound   = NOUVEAU_GROBJ_UNBOUND;
+       nvgrobj->base.subc    = -1;
+
+       g.channel = chan->id;
+       g.handle  = handle;
+       g.class   = class;
+       ret = drmCommandWrite(nvdev->fd, DRM_NOUVEAU_GROBJ_ALLOC,
+                             &g, sizeof(g));
+       if (ret) {
+               nouveau_grobj_free((void *)&nvgrobj);
+               return ret;
+       }
+
+       *grobj = &nvgrobj->base;
+       return 0;
+}
+
+int
+nouveau_grobj_ref(struct nouveau_channel *chan, uint32_t handle,
+                 struct nouveau_grobj **grobj)
+{
+       struct nouveau_grobj_priv *nvgrobj;
+
+       if (!chan || !grobj || *grobj)
+               return -EINVAL;
+
+       nvgrobj = calloc(1, sizeof(struct nouveau_grobj_priv));
+       if (!nvgrobj)
+               return -ENOMEM;
+       nvgrobj->base.channel = chan;
+       nvgrobj->base.handle = handle;
+       nvgrobj->base.grclass = 0;
+
+       *grobj = &nvgrobj->base;
+       return 0;
+}
+
+void
+nouveau_grobj_free(struct nouveau_grobj **grobj)
+{
+       struct nouveau_device_priv *nvdev;
+       struct nouveau_channel_priv *chan;
+       struct nouveau_grobj_priv *nvgrobj;
+
+       if (!grobj || !*grobj)
+               return;
+       nvgrobj = nouveau_grobj(*grobj);
+       *grobj = NULL;
+
+
+       chan = nouveau_channel(nvgrobj->base.channel);
+       nvdev = nouveau_device(chan->base.device);
+
+       if (nvgrobj->base.grclass) {
+               struct drm_nouveau_gpuobj_free f;
+
+               FIRE_RING(&chan->base);
+
+               f.channel = chan->drm.channel;
+               f.handle  = nvgrobj->base.handle;
+               drmCommandWrite(nvdev->fd, DRM_NOUVEAU_GPUOBJ_FREE,
+                               &f, sizeof(f)); 
+       }
+       free(nvgrobj);
+}
+
+void
+nouveau_grobj_autobind(struct nouveau_grobj *grobj)
+{
+       struct nouveau_channel *chan = grobj->channel;
+       struct nouveau_subchannel *subc = NULL;
+       int i;
+
+       for (i = 0; i < 8; i++) {
+               struct nouveau_subchannel *scc = &grobj->channel->subc[i];
+
+               if (scc->gr && scc->gr->bound == NOUVEAU_GROBJ_BOUND_EXPLICIT)
+                       continue;
+
+               if (!subc || scc->sequence < subc->sequence)
+                       subc = scc;
+       }
+
+       if (subc->gr) {
+               subc->gr->bound = NOUVEAU_GROBJ_UNBOUND;
+               subc->gr->subc  = -1;
+       }
+
+       subc->gr = grobj;
+       subc->gr->bound = NOUVEAU_GROBJ_BOUND;
+       subc->gr->subc  = subc - &grobj->channel->subc[0];
+
+       WAIT_RING(chan, 2);
+       if (chan->device->chipset < 0xc0) {
+               OUT_RING (chan, (1 << 18) | (grobj->subc << 13));
+               OUT_RING (chan, grobj->handle);
+       } else {
+               OUT_RING (chan, (2 << 28) | (1 << 16) | (grobj->subc << 13));
+               OUT_RING (chan, grobj->grclass);
+       }
+}
+
diff --git a/nouveau/nouveau_grobj.h b/nouveau/nouveau_grobj.h
new file mode 100644 (file)
index 0000000..51ac7d9
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2007 Nouveau Project
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
+ * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef __NOUVEAU_GROBJ_H__
+#define __NOUVEAU_GROBJ_H__
+
+#include "nouveau_channel.h"
+
+struct nouveau_grobj {
+       struct nouveau_channel *channel;
+       int grclass;
+       uint32_t handle;
+
+       enum {
+               NOUVEAU_GROBJ_UNBOUND = 0,
+               NOUVEAU_GROBJ_BOUND = 1,
+               NOUVEAU_GROBJ_BOUND_EXPLICIT = 2
+       } bound;
+       int subc;
+};
+
+int nouveau_grobj_alloc(struct nouveau_channel *, uint32_t handle,
+                              int class, struct nouveau_grobj **);
+int nouveau_grobj_ref(struct nouveau_channel *, uint32_t handle,
+                            struct nouveau_grobj **);
+void nouveau_grobj_free(struct nouveau_grobj **);
+void nouveau_grobj_autobind(struct nouveau_grobj *);
+
+#endif
diff --git a/nouveau/nouveau_notifier.c b/nouveau/nouveau_notifier.c
new file mode 100644 (file)
index 0000000..513fa63
--- /dev/null
@@ -0,0 +1,148 @@
+/*
+ * Copyright 2007 Nouveau Project
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
+ * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <stdlib.h>
+#include <errno.h>
+#include <sys/time.h>
+
+#include "nouveau_private.h"
+
+#define NOTIFIER(__v)                                                          \
+       struct nouveau_notifier_priv *nvnotify = nouveau_notifier(notifier);   \
+       volatile uint32_t *__v = (uint32_t *)((char *)nvnotify->map + (id * 32))
+
+int
+nouveau_notifier_alloc(struct nouveau_channel *chan, uint32_t handle,
+                      int count, struct nouveau_notifier **notifier)
+{
+       struct nouveau_notifier_priv *nvnotify;
+       int ret;
+
+       if (!chan || !notifier || *notifier)
+               return -EINVAL;
+
+       nvnotify = calloc(1, sizeof(struct nouveau_notifier_priv));
+       if (!nvnotify)
+               return -ENOMEM;
+       nvnotify->base.channel = chan;
+       nvnotify->base.handle  = handle;
+
+       nvnotify->drm.channel = chan->id;
+       nvnotify->drm.handle  = handle;
+       nvnotify->drm.size    = (count * 32);
+       if ((ret = drmCommandWriteRead(nouveau_device(chan->device)->fd,
+                                      DRM_NOUVEAU_NOTIFIEROBJ_ALLOC,
+                                      &nvnotify->drm,
+                                      sizeof(nvnotify->drm)))) {
+               nouveau_notifier_free((void *)&nvnotify);
+               return ret;
+       }
+
+       nvnotify->map = (char *)nouveau_channel(chan)->notifier_bo->map +
+                               nvnotify->drm.offset;
+       *notifier = &nvnotify->base;
+       return 0;
+}
+
+void
+nouveau_notifier_free(struct nouveau_notifier **notifier)
+{
+
+       struct nouveau_notifier_priv *nvnotify;
+       struct nouveau_channel_priv *nvchan;
+       struct nouveau_device_priv *nvdev;
+       struct drm_nouveau_gpuobj_free f;
+
+       if (!notifier || !*notifier)
+               return;
+       nvnotify = nouveau_notifier(*notifier);
+       *notifier = NULL;
+
+       nvchan = nouveau_channel(nvnotify->base.channel);
+       nvdev   = nouveau_device(nvchan->base.device);
+
+       FIRE_RING(&nvchan->base);
+
+       f.channel = nvchan->drm.channel;
+       f.handle  = nvnotify->base.handle;
+       drmCommandWrite(nvdev->fd, DRM_NOUVEAU_GPUOBJ_FREE, &f, sizeof(f));             
+       free(nvnotify);
+}
+
+void
+nouveau_notifier_reset(struct nouveau_notifier *notifier, int id)
+{
+       NOTIFIER(n);
+
+       n[NV_NOTIFY_TIME_0      /4] = 0x00000000;
+       n[NV_NOTIFY_TIME_1      /4] = 0x00000000;
+       n[NV_NOTIFY_RETURN_VALUE/4] = 0x00000000;
+       n[NV_NOTIFY_STATE       /4] = (NV_NOTIFY_STATE_STATUS_IN_PROCESS <<
+                                      NV_NOTIFY_STATE_STATUS_SHIFT);
+}
+
+uint32_t
+nouveau_notifier_status(struct nouveau_notifier *notifier, int id)
+{
+       NOTIFIER(n);
+
+       return n[NV_NOTIFY_STATE/4] >> NV_NOTIFY_STATE_STATUS_SHIFT;
+}
+
+uint32_t
+nouveau_notifier_return_val(struct nouveau_notifier *notifier, int id)
+{
+       NOTIFIER(n);
+
+       return n[NV_NOTIFY_RETURN_VALUE/4];
+}
+
+static inline double
+gettime(void)
+{
+       struct timeval tv;
+
+       gettimeofday(&tv, NULL);
+       return (double)tv.tv_sec + tv.tv_usec / 1000000.0;
+}
+
+int
+nouveau_notifier_wait_status(struct nouveau_notifier *notifier, int id,
+                            uint32_t status, double timeout)
+{
+       NOTIFIER(n);
+       double time = 0, t_start = gettime();
+
+       while (time <= timeout) {
+               uint32_t v;
+
+               v = n[NV_NOTIFY_STATE/4] >> NV_NOTIFY_STATE_STATUS_SHIFT;
+               if (v == status)
+                       return 0;
+
+               if (timeout)
+                       time = gettime() - t_start;
+       }
+
+       return -EBUSY;
+}
+
diff --git a/nouveau/nouveau_notifier.h b/nouveau/nouveau_notifier.h
new file mode 100644 (file)
index 0000000..dbc6a3b
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ * Copyright 2007 Nouveau Project
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
+ * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef __NOUVEAU_NOTIFIER_H__
+#define __NOUVEAU_NOTIFIER_H__
+
+#define NV_NOTIFIER_SIZE                                                      32
+#define NV_NOTIFY_TIME_0                                              0x00000000
+#define NV_NOTIFY_TIME_1                                              0x00000004
+#define NV_NOTIFY_RETURN_VALUE                                        0x00000008
+#define NV_NOTIFY_STATE                                               0x0000000C
+#define NV_NOTIFY_STATE_STATUS_MASK                                   0xFF000000
+#define NV_NOTIFY_STATE_STATUS_SHIFT                                          24
+#define NV_NOTIFY_STATE_STATUS_COMPLETED                                    0x00
+#define NV_NOTIFY_STATE_STATUS_IN_PROCESS                                   0x01
+#define NV_NOTIFY_STATE_ERROR_CODE_MASK                               0x0000FFFF
+#define NV_NOTIFY_STATE_ERROR_CODE_SHIFT                                       0
+
+struct nouveau_notifier {
+       struct nouveau_channel *channel;
+       uint32_t handle;
+};
+
+int
+nouveau_notifier_alloc(struct nouveau_channel *, uint32_t handle, int count,
+                      struct nouveau_notifier **);
+
+void
+nouveau_notifier_free(struct nouveau_notifier **);
+
+void
+nouveau_notifier_reset(struct nouveau_notifier *, int id);
+
+uint32_t
+nouveau_notifier_status(struct nouveau_notifier *, int id);
+
+uint32_t
+nouveau_notifier_return_val(struct nouveau_notifier *, int id);
+
+int
+nouveau_notifier_wait_status(struct nouveau_notifier *, int id, uint32_t status,
+                            double timeout);
+
+#endif
diff --git a/nouveau/nouveau_private.h b/nouveau/nouveau_private.h
new file mode 100644 (file)
index 0000000..124fe87
--- /dev/null
@@ -0,0 +1,136 @@
+/*
+ * Copyright 2007 Nouveau Project
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
+ * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef __NOUVEAU_PRIVATE_H__
+#define __NOUVEAU_PRIVATE_H__
+
+#include <stdint.h>
+#include <xf86drm.h>
+#include <nouveau_drm.h>
+
+#include "nouveau_drmif.h"
+#include "nouveau_device.h"
+#include "nouveau_channel.h"
+#include "nouveau_grobj.h"
+#include "nouveau_notifier.h"
+#include "nouveau_bo.h"
+#include "nouveau_resource.h"
+#include "nouveau_pushbuf.h"
+#include "nouveau_reloc.h"
+
+#define CALPB_BUFFERS 3
+
+struct nouveau_pushbuf_priv {
+       uint32_t cal_suffix0;
+       uint32_t cal_suffix1;
+       struct nouveau_bo *buffer[CALPB_BUFFERS];
+       int current;
+       int current_offset;
+
+       unsigned *pushbuf;
+       unsigned size;
+
+       uint32_t *marker;
+       unsigned marker_offset;
+       unsigned marker_relocs;
+       unsigned marker_push;
+
+       struct drm_nouveau_gem_pushbuf_bo *buffers;
+       unsigned nr_buffers;
+       struct drm_nouveau_gem_pushbuf_reloc *relocs;
+       unsigned nr_relocs;
+       struct drm_nouveau_gem_pushbuf_push push[NOUVEAU_GEM_MAX_PUSH];
+       unsigned nr_push;
+};
+#define nouveau_pushbuf(n) ((struct nouveau_pushbuf_priv *)(n))
+
+int
+nouveau_pushbuf_init(struct nouveau_channel *, int buf_size);
+void
+nouveau_pushbuf_fini(struct nouveau_channel *);
+
+struct nouveau_channel_priv {
+       struct nouveau_channel base;
+
+       struct drm_nouveau_channel_alloc drm;
+
+       struct nouveau_bo *notifier_bo;
+
+       struct nouveau_pushbuf_priv pb;
+};
+#define nouveau_channel(n) ((struct nouveau_channel_priv *)(n))
+
+struct nouveau_grobj_priv {
+       struct nouveau_grobj base;
+};
+#define nouveau_grobj(n) ((struct nouveau_grobj_priv *)(n))
+
+struct nouveau_notifier_priv {
+       struct nouveau_notifier base;
+
+       struct drm_nouveau_notifierobj_alloc drm;
+       volatile void *map;
+};
+#define nouveau_notifier(n) ((struct nouveau_notifier_priv *)(n))
+
+struct nouveau_bo_priv {
+       struct nouveau_bo base;
+       int refcount;
+
+       /* Buffer configuration + usage hints */
+       unsigned flags;
+       unsigned size;
+       unsigned align;
+       int user;
+
+       /* Tracking */
+       struct drm_nouveau_gem_pushbuf_bo *pending;
+       struct nouveau_channel *pending_channel;
+       int pending_refcnt;
+       int write_marker;
+
+       /* Userspace object */
+       void *sysmem;
+
+       /* Kernel object */
+       uint32_t global_handle;
+       drm_handle_t handle;
+       uint64_t map_handle;
+       int map_refcnt;
+       void *map;
+
+       /* Last known information from kernel on buffer status */
+       uint64_t offset;
+       uint32_t domain;
+};
+#define nouveau_bo(n) ((struct nouveau_bo_priv *)(n))
+
+int
+nouveau_bo_init(struct nouveau_device *);
+
+void
+nouveau_bo_takedown(struct nouveau_device *);
+
+struct drm_nouveau_gem_pushbuf_bo *
+nouveau_bo_emit_buffer(struct nouveau_channel *, struct nouveau_bo *);
+
+#endif
diff --git a/nouveau/nouveau_pushbuf.c b/nouveau/nouveau_pushbuf.c
new file mode 100644 (file)
index 0000000..59f60d9
--- /dev/null
@@ -0,0 +1,344 @@
+/*
+ * Copyright 2007 Nouveau Project
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
+ * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <assert.h>
+
+#include "nouveau_private.h"
+
+#define PB_BUFMGR_DWORDS   (4096 / 2)
+#define PB_MIN_USER_DWORDS  2048
+
+static int
+nouveau_pushbuf_space(struct nouveau_channel *chan, unsigned min)
+{
+       struct nouveau_channel_priv *nvchan = nouveau_channel(chan);
+       struct nouveau_pushbuf_priv *nvpb = &nvchan->pb;
+       struct nouveau_bo *bo;
+       int ret;
+
+       if (min < PB_MIN_USER_DWORDS)
+               min = PB_MIN_USER_DWORDS;
+
+       nvpb->current_offset = chan->cur - nvpb->pushbuf;
+       if (chan->cur + min + 2 <= chan->end)
+               return 0;
+
+       nvpb->current++;
+       if (nvpb->current == CALPB_BUFFERS)
+               nvpb->current = 0;
+       bo = nvpb->buffer[nvpb->current];
+
+       ret = nouveau_bo_map(bo, NOUVEAU_BO_WR);
+       if (ret)
+               return ret;
+
+       nvpb->size = (bo->size - 8) / 4;
+       nvpb->pushbuf = bo->map;
+       nvpb->current_offset = 0;
+
+       chan->cur = nvpb->pushbuf;
+       chan->end = nvpb->pushbuf + nvpb->size;
+
+       nouveau_bo_unmap(bo);
+       return 0;
+}
+
+static void
+nouveau_pushbuf_fini_call(struct nouveau_channel *chan)
+{
+       struct nouveau_channel_priv *nvchan = nouveau_channel(chan);
+       struct nouveau_pushbuf_priv *nvpb = &nvchan->pb;
+       int i;
+
+       for (i = 0; i < CALPB_BUFFERS; i++)
+               nouveau_bo_ref(NULL, &nvpb->buffer[i]);
+       nvpb->pushbuf = NULL;
+}
+
+static int
+nouveau_pushbuf_init_call(struct nouveau_channel *chan, int buf_size)
+{
+       struct drm_nouveau_gem_pushbuf req;
+       struct nouveau_channel_priv *nvchan = nouveau_channel(chan);
+       struct nouveau_pushbuf_priv *nvpb = &nvchan->pb;
+       struct nouveau_device *dev = chan->device;
+       uint32_t flags = 0;
+       int i, ret;
+
+       if (nvchan->drm.pushbuf_domains & NOUVEAU_GEM_DOMAIN_GART)
+               flags |= NOUVEAU_BO_GART;
+       else
+               flags |= NOUVEAU_BO_VRAM;
+
+       req.channel = chan->id;
+       req.nr_push = 0;
+       ret = drmCommandWriteRead(nouveau_device(dev)->fd,
+                                 DRM_NOUVEAU_GEM_PUSHBUF, &req, sizeof(req));
+       if (ret)
+               return ret;
+
+       for (i = 0; i < CALPB_BUFFERS; i++) {
+               ret = nouveau_bo_new(dev, flags | NOUVEAU_BO_MAP,
+                                    0, buf_size, &nvpb->buffer[i]);
+               if (ret) {
+                       nouveau_pushbuf_fini_call(chan);
+                       return ret;
+               }
+       }
+
+       nvpb->cal_suffix0 = req.suffix0;
+       nvpb->cal_suffix1 = req.suffix1;
+       return 0;
+}
+
+int
+nouveau_pushbuf_init(struct nouveau_channel *chan, int buf_size)
+{
+       struct nouveau_channel_priv *nvchan = nouveau_channel(chan);
+       struct nouveau_pushbuf_priv *nvpb = &nvchan->pb;
+       int ret;
+
+       ret = nouveau_pushbuf_init_call(chan, buf_size);
+       if (ret)
+               return ret;
+
+       ret = nouveau_pushbuf_space(chan, 0);
+       if (ret)
+               return ret;
+
+       nvpb->buffers = calloc(NOUVEAU_GEM_MAX_BUFFERS,
+                              sizeof(struct drm_nouveau_gem_pushbuf_bo));
+       nvpb->relocs = calloc(NOUVEAU_GEM_MAX_RELOCS,
+                             sizeof(struct drm_nouveau_gem_pushbuf_reloc));
+       return 0;
+}
+
+void
+nouveau_pushbuf_fini(struct nouveau_channel *chan)
+{
+       struct nouveau_channel_priv *nvchan = nouveau_channel(chan);
+       struct nouveau_pushbuf_priv *nvpb = &nvchan->pb;
+       nouveau_pushbuf_fini_call(chan);
+       free(nvpb->buffers);
+       free(nvpb->relocs);
+}
+
+static int
+nouveau_pushbuf_bo_add(struct nouveau_channel *chan, struct nouveau_bo *bo,
+                      unsigned offset, unsigned length)
+{
+       struct nouveau_channel_priv *nvchan = nouveau_channel(chan);
+       struct nouveau_pushbuf_priv *nvpb = &nvchan->pb;
+       struct drm_nouveau_gem_pushbuf_push *p = &nvpb->push[nvpb->nr_push++];
+       struct drm_nouveau_gem_pushbuf_bo *pbbo;
+       struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
+
+       pbbo = nouveau_bo_emit_buffer(chan, bo);
+       if (!pbbo)
+               return -ENOMEM;
+       pbbo->valid_domains &= nvchan->drm.pushbuf_domains;
+       pbbo->read_domains |= nvchan->drm.pushbuf_domains;
+       nvbo->pending_refcnt++;
+
+       p->bo_index = pbbo - nvpb->buffers;
+       p->offset = offset;
+       p->length = length;
+       return 0;
+}
+
+int
+nouveau_pushbuf_submit(struct nouveau_channel *chan, struct nouveau_bo *bo,
+                      unsigned offset, unsigned length)
+{
+       struct nouveau_pushbuf_priv *nvpb = &nouveau_channel(chan)->pb;
+       int ret, len;
+
+       if ((AVAIL_RING(chan) + nvpb->current_offset) != nvpb->size) {
+               if (nvpb->cal_suffix0 || nvpb->cal_suffix1) {
+                       *(chan->cur++) = nvpb->cal_suffix0;
+                       *(chan->cur++) = nvpb->cal_suffix1;
+               }
+
+               len = (chan->cur - nvpb->pushbuf) - nvpb->current_offset;
+
+               ret = nouveau_pushbuf_bo_add(chan, nvpb->buffer[nvpb->current],
+                                            nvpb->current_offset * 4, len * 4);
+               if (ret)
+                       return ret;
+
+               nvpb->current_offset += len;
+       }
+
+       return bo ? nouveau_pushbuf_bo_add(chan, bo, offset, length) : 0;
+}
+
+static void
+nouveau_pushbuf_bo_unref(struct nouveau_pushbuf_priv *nvpb, int index)
+{
+       struct drm_nouveau_gem_pushbuf_bo *pbbo = &nvpb->buffers[index];
+       struct nouveau_bo *bo = (void *)(unsigned long)pbbo->user_priv;
+       struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
+
+       if (--nvbo->pending_refcnt)
+               return;
+
+       if (pbbo->presumed.valid == 0) {
+               nvbo->domain = pbbo->presumed.domain;
+               nvbo->offset = pbbo->presumed.offset;
+       }
+
+       nvbo->pending = NULL;
+       nouveau_bo_ref(NULL, &bo);
+
+       /* we only ever remove from the tail of the pending lists,
+        * so this is safe.
+        */
+       nvpb->nr_buffers--;
+}
+
+int
+nouveau_pushbuf_flush(struct nouveau_channel *chan, unsigned min)
+{
+       struct nouveau_device_priv *nvdev = nouveau_device(chan->device);
+       struct nouveau_channel_priv *nvchan = nouveau_channel(chan);
+       struct nouveau_pushbuf_priv *nvpb = &nvchan->pb;
+       struct drm_nouveau_gem_pushbuf req;
+       unsigned i;
+       int ret;
+
+       ret = nouveau_pushbuf_submit(chan, NULL, 0, 0);
+       if (ret)
+               return ret;
+
+       if (!nvpb->nr_push)
+               return 0;
+
+       req.channel = chan->id;
+       req.nr_push = nvpb->nr_push;
+       req.push = (uint64_t)(unsigned long)nvpb->push;
+       req.nr_buffers = nvpb->nr_buffers;
+       req.buffers = (uint64_t)(unsigned long)nvpb->buffers;
+       req.nr_relocs = nvpb->nr_relocs;
+       req.relocs = (uint64_t)(unsigned long)nvpb->relocs;
+       req.suffix0 = nvpb->cal_suffix0;
+       req.suffix1 = nvpb->cal_suffix1;
+
+       do {
+               ret = drmCommandWriteRead(nvdev->fd, DRM_NOUVEAU_GEM_PUSHBUF,
+                                         &req, sizeof(req));
+       } while (ret == -EAGAIN);
+       nvpb->cal_suffix0 = req.suffix0;
+       nvpb->cal_suffix1 = req.suffix1;
+       nvdev->base.vm_vram_size = req.vram_available;
+       nvdev->base.vm_gart_size = req.gart_available;
+
+       /* Update presumed offset/domain for any buffers that moved.
+        * Dereference all buffers on validate list
+        */
+       for (i = 0; i < nvpb->nr_relocs; i++) {
+               nouveau_pushbuf_bo_unref(nvpb, nvpb->relocs[i].bo_index);
+               nouveau_pushbuf_bo_unref(nvpb, nvpb->relocs[i].reloc_bo_index);
+       }
+
+       for (i = 0; i < nvpb->nr_push; i++)
+               nouveau_pushbuf_bo_unref(nvpb, nvpb->push[i].bo_index);
+
+       nvpb->nr_buffers = 0;
+       nvpb->nr_relocs = 0;
+       nvpb->nr_push = 0;
+
+       /* Allocate space for next push buffer */
+       if (nouveau_pushbuf_space(chan, min))
+               assert(0);
+
+       if (chan->flush_notify)
+               chan->flush_notify(chan);
+
+       nvpb->marker = NULL;
+       return ret;
+}
+
+int
+nouveau_pushbuf_marker_emit(struct nouveau_channel *chan,
+                           unsigned wait_dwords, unsigned wait_relocs)
+{
+       struct nouveau_pushbuf_priv *nvpb = &nouveau_channel(chan)->pb;
+
+       if (AVAIL_RING(chan) < wait_dwords)
+               return nouveau_pushbuf_flush(chan, wait_dwords);
+
+       if (nvpb->nr_relocs + wait_relocs >= NOUVEAU_GEM_MAX_RELOCS)
+               return nouveau_pushbuf_flush(chan, wait_dwords);
+
+       nvpb->marker = chan->cur;
+       nvpb->marker_offset = nvpb->current_offset;
+       nvpb->marker_push = nvpb->nr_push;
+       nvpb->marker_relocs = nvpb->nr_relocs;
+       return 0;
+}
+
+void
+nouveau_pushbuf_marker_undo(struct nouveau_channel *chan)
+{
+       struct nouveau_pushbuf_priv *nvpb = &nouveau_channel(chan)->pb;
+       unsigned i;
+
+       if (!nvpb->marker)
+               return;
+
+       /* undo any relocs/buffers added to the list since last marker */
+       for (i = nvpb->marker_relocs; i < nvpb->nr_relocs; i++) {
+               nouveau_pushbuf_bo_unref(nvpb, nvpb->relocs[i].bo_index);
+               nouveau_pushbuf_bo_unref(nvpb, nvpb->relocs[i].reloc_bo_index);
+       }
+       nvpb->nr_relocs = nvpb->marker_relocs;
+
+       for (i = nvpb->marker_push; i < nvpb->nr_push; i++)
+               nouveau_pushbuf_bo_unref(nvpb, nvpb->push[i].bo_index);
+       nvpb->nr_push = nvpb->marker_push;
+
+       /* reset pushbuf back to last marker */
+       chan->cur = nvpb->marker;
+       nvpb->current_offset = nvpb->marker_offset;
+       nvpb->marker = NULL;
+}
+
+int
+nouveau_pushbuf_emit_reloc(struct nouveau_channel *chan, void *ptr,
+                          struct nouveau_bo *bo, uint32_t data, uint32_t data2,
+                          uint32_t flags, uint32_t vor, uint32_t tor)
+{
+       struct nouveau_pushbuf_priv *nvpb = &nouveau_channel(chan)->pb;
+       int ret;
+
+       ret = nouveau_reloc_emit(chan, nvpb->buffer[nvpb->current],
+                                (char *)ptr - (char *)nvpb->pushbuf, ptr,
+                                bo, data, data2, flags, vor, tor);
+       if (ret)
+               return ret;
+
+       return 0;
+}
+
diff --git a/nouveau/nouveau_pushbuf.h b/nouveau/nouveau_pushbuf.h
new file mode 100644 (file)
index 0000000..2a98789
--- /dev/null
@@ -0,0 +1,162 @@
+/*
+ * Copyright 2007 Nouveau Project
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
+ * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef __NOUVEAU_PUSHBUF_H__
+#define __NOUVEAU_PUSHBUF_H__
+
+#include <assert.h>
+#include <string.h>
+
+#include "nouveau_bo.h"
+#include "nouveau_grobj.h"
+
+int
+nouveau_pushbuf_flush(struct nouveau_channel *, unsigned min);
+
+int
+nouveau_pushbuf_marker_emit(struct nouveau_channel *chan,
+                           unsigned wait_dwords, unsigned wait_relocs);
+
+void
+nouveau_pushbuf_marker_undo(struct nouveau_channel *chan);
+
+int
+nouveau_pushbuf_emit_reloc(struct nouveau_channel *, void *ptr,
+                          struct nouveau_bo *, uint32_t data, uint32_t data2,
+                          uint32_t flags, uint32_t vor, uint32_t tor);
+
+int
+nouveau_pushbuf_submit(struct nouveau_channel *chan, struct nouveau_bo *bo,
+                      unsigned offset, unsigned length);
+
+/* Push buffer access macros */
+static __inline__ int
+MARK_RING(struct nouveau_channel *chan, unsigned dwords, unsigned relocs)
+{
+       return nouveau_pushbuf_marker_emit(chan, dwords, relocs);
+}
+
+static __inline__ void
+MARK_UNDO(struct nouveau_channel *chan)
+{
+       nouveau_pushbuf_marker_undo(chan);
+}
+
+static __inline__ void
+OUT_RING(struct nouveau_channel *chan, unsigned data)
+{
+       *(chan->cur++) = (data);
+}
+
+static __inline__ void
+OUT_RINGp(struct nouveau_channel *chan, const void *data, unsigned size)
+{
+       memcpy(chan->cur, data, size * 4);
+       chan->cur += size;
+}
+
+static __inline__ void
+OUT_RINGf(struct nouveau_channel *chan, float f)
+{
+       union { uint32_t i; float f; } c;
+       c.f = f;
+       OUT_RING(chan, c.i);
+}
+
+static __inline__ unsigned
+AVAIL_RING(struct nouveau_channel *chan)
+{
+       return chan->end - chan->cur;
+}
+
+static __inline__ void
+WAIT_RING(struct nouveau_channel *chan, unsigned size)
+{
+       if (chan->cur + size > chan->end)
+               nouveau_pushbuf_flush(chan, size);
+}
+
+static __inline__ void
+FIRE_RING(struct nouveau_channel *chan)
+{
+       nouveau_pushbuf_flush(chan, 0);
+}
+
+static __inline__ int
+OUT_RELOC(struct nouveau_channel *chan, struct nouveau_bo *bo,
+         unsigned data, unsigned flags, unsigned vor, unsigned tor)
+{
+       return nouveau_pushbuf_emit_reloc(chan, chan->cur++, bo,
+                                         data, 0, flags, vor, tor);
+}
+
+static __inline__ int
+OUT_RELOC2(struct nouveau_channel *chan, struct nouveau_bo *bo,
+          unsigned data, unsigned data2, unsigned flags,
+          unsigned vor, unsigned tor)
+{
+       return nouveau_pushbuf_emit_reloc(chan, chan->cur++, bo,
+                                         data, data2, flags, vor, tor);
+}
+
+/* Raw data + flags depending on FB/TT buffer */
+static __inline__ int
+OUT_RELOCd(struct nouveau_channel *chan, struct nouveau_bo *bo,
+          unsigned data, unsigned flags, unsigned vor, unsigned tor)
+{
+       return OUT_RELOC(chan, bo, data, flags | NOUVEAU_BO_OR, vor, tor);
+}
+
+/* FB/TT object handle */
+static __inline__ int
+OUT_RELOCo(struct nouveau_channel *chan, struct nouveau_bo *bo,
+          unsigned flags)
+{
+       return OUT_RELOC(chan, bo, 0, flags | NOUVEAU_BO_OR,
+                        chan->vram->handle, chan->gart->handle);
+}
+
+/* Low 32-bits of offset */
+static __inline__ int
+OUT_RELOCl(struct nouveau_channel *chan, struct nouveau_bo *bo,
+          unsigned delta, unsigned flags)
+{
+       return OUT_RELOC(chan, bo, delta, flags | NOUVEAU_BO_LOW, 0, 0);
+}
+
+/* Low 32-bits of offset + GPU linear access range info */
+static __inline__ int
+OUT_RELOCr(struct nouveau_channel *chan, struct nouveau_bo *bo,
+          unsigned delta, unsigned size, unsigned flags)
+{
+       return OUT_RELOC2(chan, bo, delta, size, flags | NOUVEAU_BO_LOW, 0, 0);
+}
+
+/* High 32-bits of offset */
+static __inline__ int
+OUT_RELOCh(struct nouveau_channel *chan, struct nouveau_bo *bo,
+          unsigned delta, unsigned flags)
+{
+       return OUT_RELOC(chan, bo, delta, flags | NOUVEAU_BO_HIGH, 0, 0);
+}
+
+#endif
diff --git a/nouveau/nouveau_reloc.c b/nouveau/nouveau_reloc.c
new file mode 100644 (file)
index 0000000..cd219db
--- /dev/null
@@ -0,0 +1,154 @@
+/*
+ * Copyright 2010 Nouveau Project
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
+ * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <assert.h>
+
+#include "nouveau_private.h"
+
+static uint32_t
+nouveau_reloc_calc(struct drm_nouveau_gem_pushbuf_bo *pbbo,
+                  struct drm_nouveau_gem_pushbuf_reloc *r)
+{
+       uint32_t push = 0;
+
+       if (r->flags & NOUVEAU_GEM_RELOC_LOW)
+               push = (pbbo->presumed.offset + r->data);
+       else
+       if (r->flags & NOUVEAU_GEM_RELOC_HIGH)
+               push = (pbbo->presumed.offset + r->data) >> 32;
+       else
+               push = r->data;
+
+       if (r->flags & NOUVEAU_GEM_RELOC_OR) {
+               if (pbbo->presumed.domain & NOUVEAU_GEM_DOMAIN_VRAM)
+                       push |= r->vor;
+               else
+                       push |= r->tor;
+       }
+
+       return push;
+}
+
+int
+nouveau_reloc_emit(struct nouveau_channel *chan, struct nouveau_bo *reloc_bo,
+                  uint32_t reloc_offset, uint32_t *reloc_ptr,
+                  struct nouveau_bo *bo, uint32_t data, uint32_t data2,
+                  uint32_t flags, uint32_t vor, uint32_t tor)
+{
+       struct nouveau_pushbuf_priv *nvpb = &nouveau_channel(chan)->pb;
+       struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
+       struct drm_nouveau_gem_pushbuf_reloc *r;
+       struct drm_nouveau_gem_pushbuf_bo *pbbo, *rpbbo;
+       uint32_t domains = 0;
+
+       if (nvpb->nr_relocs >= NOUVEAU_GEM_MAX_RELOCS) {
+               fprintf(stderr, "too many relocs!!\n");
+               return -ENOMEM;
+       }
+
+       if (nvbo->user && (flags & NOUVEAU_BO_WR)) {
+               fprintf(stderr, "write to user buffer!!\n");
+               return -EINVAL;
+       }
+
+       /* We're about to reloc a user buffer, better make sure we don't cause
+        * a double migration.
+        */
+       if (!(nvbo->flags & (NOUVEAU_BO_GART | NOUVEAU_BO_VRAM)))
+               nvbo->flags |= (flags & (NOUVEAU_BO_GART | NOUVEAU_BO_VRAM));
+
+       /* add buffer to validation list */
+       pbbo = nouveau_bo_emit_buffer(chan, bo);
+       if (!pbbo) {
+               fprintf(stderr, "buffer emit fail :(\n");
+               return -ENOMEM;
+       }
+       nouveau_bo(bo)->pending_refcnt++;
+
+       if (flags & (NOUVEAU_BO_VRAM | NOUVEAU_BO_GART)) {
+               if (flags & NOUVEAU_BO_VRAM)
+                       domains |= NOUVEAU_GEM_DOMAIN_VRAM;
+               if (flags & NOUVEAU_BO_GART)
+                       domains |= NOUVEAU_GEM_DOMAIN_GART;
+       } else
+               domains |= nvbo->domain;
+
+       if (!(pbbo->valid_domains & domains)) {
+               fprintf(stderr, "no valid domains remain!\n");
+               return -EINVAL;
+       }
+       pbbo->valid_domains &= domains;
+
+       assert(flags & NOUVEAU_BO_RDWR);
+       if (flags & NOUVEAU_BO_RD) {
+               pbbo->read_domains |= domains;
+       }
+       if (flags & NOUVEAU_BO_WR) {
+               pbbo->write_domains |= domains;
+               nvbo->write_marker = 1;
+       }
+
+       /* nvc0 gallium driver uses reloc_emit() with NULL target buffer
+        * to inform bufmgr of a buffer's use - however, we need something
+        * to track, so create a reloc for now, and hope it never triggers
+        * (it shouldn't, constant virtual address..)..
+        */
+       if (!reloc_bo) {
+               reloc_bo  = nvpb->buffer[nvpb->current];
+               reloc_offset = 0;
+               reloc_ptr = NULL;
+       }
+
+       /* add reloc target bo to validation list, and create the reloc */
+       rpbbo = nouveau_bo_emit_buffer(chan, reloc_bo);
+       if (!rpbbo)
+               return -ENOMEM;
+       nouveau_bo(reloc_bo)->pending_refcnt++;
+
+       r = nvpb->relocs + nvpb->nr_relocs++;
+       r->reloc_bo_index = rpbbo - nvpb->buffers;
+       r->reloc_bo_offset = reloc_offset;
+       r->bo_index = pbbo - nvpb->buffers;
+       r->flags = 0;
+       if (flags & NOUVEAU_BO_LOW)
+               r->flags |= NOUVEAU_GEM_RELOC_LOW;
+       if (flags & NOUVEAU_BO_HIGH)
+               r->flags |= NOUVEAU_GEM_RELOC_HIGH;
+       if (flags & NOUVEAU_BO_OR)
+               r->flags |= NOUVEAU_GEM_RELOC_OR;
+       r->data = data;
+       r->vor = vor;
+       r->tor = tor;
+
+       if (reloc_ptr) {
+               if (flags & NOUVEAU_BO_DUMMY)
+                       *reloc_ptr = 0;
+               else
+                       *reloc_ptr = nouveau_reloc_calc(pbbo, r);
+       }
+
+       return 0;
+}
+
diff --git a/nouveau/nouveau_reloc.h b/nouveau/nouveau_reloc.h
new file mode 100644 (file)
index 0000000..24ddb52
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * Copyright 2010 Nouveau Project
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
+ * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef __NOUVEAU_RELOC_H__
+#define __NOUVEAU_RELOC_H__
+
+int
+nouveau_reloc_emit(struct nouveau_channel *chan, struct nouveau_bo *reloc_bo,
+                  uint32_t reloc_offset, uint32_t *reloc_ptr,
+                  struct nouveau_bo *bo, uint32_t data, uint32_t data2,
+                  uint32_t flags, uint32_t vor, uint32_t tor);
+
+#endif
diff --git a/nouveau/nouveau_resource.c b/nouveau/nouveau_resource.c
new file mode 100644 (file)
index 0000000..7acaf7d
--- /dev/null
@@ -0,0 +1,124 @@
+/*
+ * Copyright 2007 Nouveau Project
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
+ * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <stdlib.h>
+#include <errno.h>
+
+#include "nouveau_private.h"
+
+int
+nouveau_resource_init(struct nouveau_resource **heap,
+                     unsigned start, unsigned size)
+{
+       struct nouveau_resource *r;
+
+       r = calloc(1, sizeof(struct nouveau_resource));
+       if (!r)
+               return 1;
+
+       r->start = start;
+       r->size  = size;
+       *heap = r;
+       return 0;
+}
+
+void
+nouveau_resource_destroy(struct nouveau_resource **heap)
+{
+       if (!*heap)
+               return;
+       free(*heap);
+       *heap = NULL;
+}
+
+int
+nouveau_resource_alloc(struct nouveau_resource *heap, unsigned size, void *priv,
+                      struct nouveau_resource **res)
+{
+       struct nouveau_resource *r;
+
+       if (!heap || !size || !res || *res)
+               return 1;
+
+       while (heap) {
+               if (!heap->in_use && heap->size >= size) {
+                       r = calloc(1, sizeof(struct nouveau_resource));
+                       if (!r)
+                               return 1;
+
+                       r->start  = (heap->start + heap->size) - size;
+                       r->size   = size;
+                       r->in_use = 1;
+                       r->priv   = priv;
+
+                       heap->size -= size;
+
+                       r->next = heap->next;
+                       if (heap->next)
+                               heap->next->prev = r;
+                       r->prev = heap;
+                       heap->next = r;
+
+                       *res = r;
+                       return 0;
+               }
+                       
+               heap = heap->next;
+       }
+
+       return 1;
+}
+
+void
+nouveau_resource_free(struct nouveau_resource **res)
+{
+       struct nouveau_resource *r;
+
+       if (!res || !*res)
+               return;
+       r = *res;
+       *res = NULL;
+
+       r->in_use = 0;
+
+       if (r->next && !r->next->in_use) {
+               struct nouveau_resource *new = r->next;
+
+               new->prev = r->prev;
+               if (r->prev)
+                       r->prev->next = new;
+               new->size += r->size;
+               new->start = r->start;
+
+               free(r);
+               r = new;
+       }
+
+       if (r->prev && !r->prev->in_use) {
+               r->prev->next = r->next;
+               if (r->next)
+                       r->next->prev = r->prev;
+               r->prev->size += r->size;
+               free(r);
+       }
+       
+}
diff --git a/nouveau/nouveau_resource.h b/nouveau/nouveau_resource.h
new file mode 100644 (file)
index 0000000..b760dfb
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2007 Nouveau Project
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
+ * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef __NOUVEAU_RESOURCE_H__
+#define __NOUVEAU_RESOURCE_H__
+
+struct nouveau_resource {
+       struct nouveau_resource *prev;
+       struct nouveau_resource *next;
+
+       int in_use;
+       void *priv;
+
+       unsigned int start;
+       unsigned int size;
+};
+
+int
+nouveau_resource_init(struct nouveau_resource **heap, unsigned start,
+                     unsigned size);
+
+void
+nouveau_resource_destroy(struct nouveau_resource **heap);
+
+int
+nouveau_resource_alloc(struct nouveau_resource *heap, unsigned size, void *priv,
+                      struct nouveau_resource **);
+
+void
+nouveau_resource_free(struct nouveau_resource **);
+
+#endif
diff --git a/nouveau/nv04_pushbuf.h b/nouveau/nv04_pushbuf.h
new file mode 100644 (file)
index 0000000..586b284
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2007 Nouveau Project
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
+ * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef __NV04_PUSHBUF_H__
+#define __NV04_PUSHBUF_H__
+
+#include "nouveau_pushbuf.h"
+
+static __inline__ void
+BEGIN_RING(struct nouveau_channel *chan, struct nouveau_grobj *gr,
+          unsigned mthd, unsigned size)
+{
+       if (gr->bound == NOUVEAU_GROBJ_UNBOUND)
+               nouveau_grobj_autobind(gr);
+       chan->subc[gr->subc].sequence = chan->subc_sequence++;
+
+       WAIT_RING(chan, size + 1);
+       OUT_RING(chan, (gr->subc << 13) | (size << 18) | mthd);
+}
+
+/* non-incrementing BEGIN_RING */
+static __inline__ void
+BEGIN_RING_NI(struct nouveau_channel *chan, struct nouveau_grobj *gr,
+          unsigned mthd, unsigned size)
+{
+       BEGIN_RING(chan, gr, mthd | 0x40000000, size);
+}
+
+static __inline__ void
+BIND_RING(struct nouveau_channel *chan, struct nouveau_grobj *gr, unsigned sc)
+{
+       struct nouveau_subchannel *subc = &gr->channel->subc[sc];
+
+       if (subc->gr) {
+               if (subc->gr->bound == NOUVEAU_GROBJ_BOUND_EXPLICIT)
+                       assert(0);
+               subc->gr->bound = NOUVEAU_GROBJ_UNBOUND;
+       }
+       subc->gr = gr;
+       subc->gr->subc = sc;
+       subc->gr->bound = NOUVEAU_GROBJ_BOUND_EXPLICIT;
+
+       BEGIN_RING(chan, gr, 0x0000, 1);
+       OUT_RING  (chan, gr->handle);
+}
+
+#endif
diff --git a/nouveau/nvc0_pushbuf.h b/nouveau/nvc0_pushbuf.h
new file mode 100644 (file)
index 0000000..40dc7e6
--- /dev/null
@@ -0,0 +1,92 @@
+/*
+ * Copyright 2010 Nouveau Project
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
+ * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef __NVC0_PUSHBUF_H__
+#define __NVC0_PUSHBUF_H__
+
+#include "nouveau_pushbuf.h"
+
+#define SUBC_BIND(chan, gr) do {                                               \
+       if (gr->bound == NOUVEAU_GROBJ_UNBOUND)                                \
+               nouveau_grobj_autobind(gr);                                    \
+       chan->subc[gr->subc].sequence = chan->subc_sequence++;                 \
+} while (0)
+
+/* incremental methods */
+static __inline__ void
+BEGIN_RING(struct nouveau_channel *chan, struct nouveau_grobj *gr,
+          unsigned mthd, unsigned size)
+{
+       SUBC_BIND(chan, gr);
+       WAIT_RING(chan, size + 1);
+       OUT_RING (chan, (0x2 << 28) | (size << 16) | (gr->subc << 13) | (mthd >> 2));
+}
+
+/* non-incremental */
+static __inline__ void
+BEGIN_RING_NI(struct nouveau_channel *chan, struct nouveau_grobj *gr,
+             unsigned mthd, unsigned size)
+{
+       SUBC_BIND(chan, gr);
+       WAIT_RING(chan, size + 1);
+       OUT_RING (chan, (0x6 << 28) | (size << 16) | (gr->subc << 13) | (mthd >> 2));
+}
+
+/* increment-once */
+static __inline__ void
+BEGIN_RING_1I(struct nouveau_channel *chan, struct nouveau_grobj *gr,
+             unsigned mthd, unsigned size)
+{
+       SUBC_BIND(chan, gr);
+       WAIT_RING(chan, size + 1);
+       OUT_RING (chan, (0xa << 28) | (size << 16) | (gr->subc << 13) | (mthd >> 2));
+}
+
+/* inline-data */
+static __inline__ void
+IMMED_RING(struct nouveau_channel *chan, struct nouveau_grobj *gr,
+          unsigned mthd, unsigned data)
+{
+       SUBC_BIND(chan, gr);
+       WAIT_RING(chan, 1);
+       OUT_RING (chan, (0x8 << 28) | (data << 16) | (gr->subc << 13) | (mthd >> 2));
+}
+
+static __inline__ void
+BIND_RING(struct nouveau_channel *chan, struct nouveau_grobj *gr, unsigned sc)
+{
+       struct nouveau_subchannel *subc = &gr->channel->subc[sc];
+
+       if (subc->gr) {
+               if (subc->gr->bound == NOUVEAU_GROBJ_BOUND_EXPLICIT)
+                       assert(0);
+               subc->gr->bound = NOUVEAU_GROBJ_UNBOUND;
+       }
+       subc->gr = gr;
+       subc->gr->subc = sc;
+       subc->gr->bound = NOUVEAU_GROBJ_BOUND_EXPLICIT;
+
+       BEGIN_RING(chan, gr, 0x0000, 1);
+       OUT_RING  (chan, gr->grclass);
+}
+
+#endif
diff --git a/radeon/Makefile.am b/radeon/Makefile.am
new file mode 100644 (file)
index 0000000..dc94b5f
--- /dev/null
@@ -0,0 +1,58 @@
+# Copyright Â© 2008 Jérôme Glisse
+#
+# Permission is hereby granted, free of charge, to any person obtaining a
+# copy of this software and associated documentation files (the "Software"),
+# to deal in the Software without restriction, including without limitation
+# the rights to use, copy, modify, merge, publish, distribute, sublicense,
+# and/or sell copies of the Software, and to permit persons to whom the
+# Software is furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice (including the next
+# paragraph) shall be included in all copies or substantial portions of the
+# Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+# IN THE SOFTWARE.
+#
+# Authors:
+#    Jérôme Glisse <glisse@freedesktop.org>
+
+AM_CFLAGS = \
+       $(WARN_CFLAGS) \
+       -I$(top_srcdir) \
+       -I$(top_srcdir)/radeon \
+       $(PTHREADSTUBS_CFLAGS) \
+       -I$(top_srcdir)/include/drm
+
+libdrm_radeon_la_LTLIBRARIES = libdrm_radeon.la
+libdrm_radeon_ladir = $(libdir)
+libdrm_radeon_la_LDFLAGS = -version-number 1:0:0 -no-undefined
+libdrm_radeon_la_LIBADD = ../libdrm.la @PTHREADSTUBS_LIBS@
+
+libdrm_radeon_la_SOURCES = \
+       radeon_bo_gem.c \
+       radeon_cs_gem.c \
+       radeon_cs_space.c \
+       radeon_bo.c \
+       radeon_cs.c \
+       bof.c \
+       bof.h
+
+libdrm_radeonincludedir = ${includedir}/libdrm
+libdrm_radeoninclude_HEADERS = \
+       radeon_bo.h \
+       radeon_cs.h \
+       radeon_bo_gem.h \
+       radeon_cs_gem.h \
+       radeon_bo_int.h \
+       radeon_cs_int.h
+
+pkgconfigdir = @pkgconfigdir@
+pkgconfig_DATA = libdrm_radeon.pc
+
+EXTRA_DIST = libdrm_radeon.pc.in
diff --git a/radeon/bof.c b/radeon/bof.c
new file mode 100644 (file)
index 0000000..0598cc6
--- /dev/null
@@ -0,0 +1,477 @@
+/*
+ * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * on the rights to use, copy, modify, merge, publish, distribute, sub
+ * license, and/or sell copies of the Software, and to permit persons to whom
+ * the Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+ * USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * Authors:
+ *      Jerome Glisse
+ */
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include "bof.h"
+
+/*
+ * helpers
+ */
+static int bof_entry_grow(bof_t *bof)
+{
+       bof_t **array;
+
+       if (bof->array_size < bof->nentry)
+               return 0;
+       array = realloc(bof->array, (bof->nentry + 16) * sizeof(void*));
+       if (array == NULL)
+               return -ENOMEM;
+       bof->array = array;
+       bof->nentry += 16;
+       return 0;
+}
+
+/*
+ * object 
+ */
+bof_t *bof_object(void)
+{
+       bof_t *object;
+
+       object = calloc(1, sizeof(bof_t));
+       if (object == NULL)
+               return NULL;
+       object->refcount = 1;
+       object->type = BOF_TYPE_OBJECT;
+       object->size = 12;
+       return object;
+}
+
+bof_t *bof_object_get(bof_t *object, const char *keyname)
+{
+       unsigned i;
+
+       for (i = 0; i < object->array_size; i += 2) {
+               if (!strcmp(object->array[i]->value, keyname)) {
+                       return object->array[i + 1];
+               }
+       }
+       return NULL;
+}
+
+int bof_object_set(bof_t *object, const char *keyname, bof_t *value)
+{
+       bof_t *key;
+       int r;
+
+       if (object->type != BOF_TYPE_OBJECT)
+               return -EINVAL;
+       r = bof_entry_grow(object);
+       if (r)
+               return r;
+       key = bof_string(keyname);
+       if (key == NULL)
+               return -ENOMEM;
+       object->array[object->array_size++] = key;
+       object->array[object->array_size++] = value;
+       object->size += value->size;
+       object->size += key->size;
+       bof_incref(value);
+       return 0;
+}
+
+/*
+ * array
+ */
+bof_t *bof_array(void)
+{
+       bof_t *array = bof_object();
+
+       if (array == NULL)
+               return NULL;
+       array->type = BOF_TYPE_ARRAY;
+       array->size = 12;
+       return array;
+}
+
+int bof_array_append(bof_t *array, bof_t *value)
+{
+       int r;
+       if (array->type != BOF_TYPE_ARRAY)
+               return -EINVAL;
+       r = bof_entry_grow(array);
+       if (r)
+               return r;
+       array->array[array->array_size++] = value;
+       array->size += value->size;
+       bof_incref(value);
+       return 0;
+}
+
+bof_t *bof_array_get(bof_t *bof, unsigned i)
+{
+       if (!bof_is_array(bof) || i >= bof->array_size)
+               return NULL;
+       return bof->array[i];
+}
+
+unsigned bof_array_size(bof_t *bof)
+{
+       if (!bof_is_array(bof))
+               return 0;
+       return bof->array_size;
+}
+
+/*
+ * blob
+ */
+bof_t *bof_blob(unsigned size, void *value)
+{
+       bof_t *blob = bof_object();
+
+       if (blob == NULL)
+               return NULL;
+       blob->type = BOF_TYPE_BLOB;
+       blob->value = calloc(1, size);
+       if (blob->value == NULL) {
+               bof_decref(blob);
+               return NULL;
+       }
+       blob->size = size;
+       memcpy(blob->value, value, size);
+       blob->size += 12;
+       return blob;
+}
+
+unsigned bof_blob_size(bof_t *bof)
+{
+       if (!bof_is_blob(bof))
+               return 0;
+       return bof->size - 12;
+}
+
+void *bof_blob_value(bof_t *bof)
+{
+       if (!bof_is_blob(bof))
+               return NULL;
+       return bof->value;
+}
+
+/*
+ * string
+ */
+bof_t *bof_string(const char *value)
+{
+       bof_t *string = bof_object();
+
+       if (string == NULL)
+               return NULL;
+       string->type = BOF_TYPE_STRING;
+       string->size = strlen(value) + 1;
+       string->value = calloc(1, string->size);
+       if (string->value == NULL) {
+               bof_decref(string);
+               return NULL;
+       }
+       strcpy(string->value, value);
+       string->size += 12;
+       return string;
+}
+
+/*
+ *  int32
+ */
+bof_t *bof_int32(int32_t value)
+{
+       bof_t *int32 = bof_object();
+
+       if (int32 == NULL)
+               return NULL;
+       int32->type = BOF_TYPE_INT32;
+       int32->size = 4;
+       int32->value = calloc(1, int32->size);
+       if (int32->value == NULL) {
+               bof_decref(int32);
+               return NULL;
+       }
+       memcpy(int32->value, &value, 4);
+       int32->size += 12;
+       return int32;
+}
+
+int32_t bof_int32_value(bof_t *bof)
+{
+       return *((uint32_t*)bof->value);
+}
+
+/*
+ *  common
+ */
+static void bof_indent(int level)
+{
+       int i;
+
+       for (i = 0; i < level; i++)
+               fprintf(stderr, " ");
+}
+
+static void bof_print_bof(bof_t *bof, int level, int entry)
+{
+       bof_indent(level);
+       if (bof == NULL) {
+               fprintf(stderr, "--NULL-- for entry %d\n", entry);
+               return;
+       }
+       switch (bof->type) {
+       case BOF_TYPE_STRING:
+               fprintf(stderr, "%p string [%s %d]\n", bof, (char*)bof->value, bof->size);
+               break;
+       case BOF_TYPE_INT32:
+               fprintf(stderr, "%p int32 [%d %d]\n", bof, *(int*)bof->value, bof->size);
+               break;
+       case BOF_TYPE_BLOB:
+               fprintf(stderr, "%p blob [%d]\n", bof, bof->size);
+               break;
+       case BOF_TYPE_NULL:
+               fprintf(stderr, "%p null [%d]\n", bof, bof->size);
+               break;
+       case BOF_TYPE_OBJECT:
+               fprintf(stderr, "%p object [%d %d]\n", bof, bof->array_size / 2, bof->size);
+               break;
+       case BOF_TYPE_ARRAY:
+               fprintf(stderr, "%p array [%d %d]\n", bof, bof->array_size, bof->size);
+               break;
+       default:
+               fprintf(stderr, "%p unknown [%d]\n", bof, bof->type);
+               return;
+       }
+}
+
+static void bof_print_rec(bof_t *bof, int level, int entry)
+{
+       unsigned i;
+
+       bof_print_bof(bof, level, entry);
+       for (i = 0; i < bof->array_size; i++) {
+               bof_print_rec(bof->array[i], level + 2, i);
+       }
+}
+
+void bof_print(bof_t *bof)
+{
+       bof_print_rec(bof, 0, 0);
+}
+
+static int bof_read(bof_t *root, FILE *file, long end, int level)
+{
+       bof_t *bof = NULL;
+       int r;
+
+       if (ftell(file) >= end) {
+               return 0;
+       }
+       r = bof_entry_grow(root);
+       if (r)
+               return r;
+       bof = bof_object();
+       if (bof == NULL)
+               return -ENOMEM;
+       bof->offset = ftell(file);
+       r = fread(&bof->type, 4, 1, file);
+       if (r != 1)
+               goto out_err;
+       r = fread(&bof->size, 4, 1, file);
+       if (r != 1)
+               goto out_err;
+       r = fread(&bof->array_size, 4, 1, file);
+       if (r != 1)
+               goto out_err;
+       switch (bof->type) {
+       case BOF_TYPE_STRING:
+       case BOF_TYPE_INT32:
+       case BOF_TYPE_BLOB:
+               bof->value = calloc(1, bof->size - 12);
+               if (bof->value == NULL) {
+                       goto out_err;
+               }
+               r = fread(bof->value, bof->size - 12, 1, file);
+               if (r != 1) {
+                       fprintf(stderr, "error reading %d\n", bof->size - 12);
+                       goto out_err;
+               }
+               break;
+       case BOF_TYPE_NULL:
+               return 0;
+       case BOF_TYPE_OBJECT:
+       case BOF_TYPE_ARRAY:
+               r = bof_read(bof, file, bof->offset + bof->size, level + 2);
+               if (r)
+                       goto out_err;
+               break;
+       default:
+               fprintf(stderr, "invalid type %d\n", bof->type);
+               goto out_err;
+       }
+       root->array[root->centry++] = bof;
+       return bof_read(root, file, end, level);
+out_err:
+       bof_decref(bof);
+       return -EINVAL;
+}
+
+bof_t *bof_load_file(const char *filename)
+{
+       bof_t *root = bof_object();
+       int r;
+
+       if (root == NULL) {
+               fprintf(stderr, "%s failed to create root object\n", __func__);
+               return NULL;
+       }
+       root->file = fopen(filename, "r");
+       if (root->file == NULL)
+               goto out_err;
+       r = fseek(root->file, 0L, SEEK_SET);
+       if (r) {
+               fprintf(stderr, "%s failed to seek into file %s\n", __func__, filename);
+               goto out_err;
+       }
+       root->offset = ftell(root->file);
+       r = fread(&root->type, 4, 1, root->file);
+       if (r != 1)
+               goto out_err;
+       r = fread(&root->size, 4, 1, root->file);
+       if (r != 1)
+               goto out_err;
+       r = fread(&root->array_size, 4, 1, root->file);
+       if (r != 1)
+               goto out_err;
+       r = bof_read(root, root->file, root->offset + root->size, 2);
+       if (r)
+               goto out_err;
+       return root;
+out_err:
+       bof_decref(root);
+       return NULL;
+}
+
+void bof_incref(bof_t *bof)
+{
+       bof->refcount++;
+}
+
+void bof_decref(bof_t *bof)
+{
+       unsigned i;
+
+       if (bof == NULL)
+               return;
+       if (--bof->refcount > 0)
+               return;
+       for (i = 0; i < bof->array_size; i++) {
+               bof_decref(bof->array[i]);
+               bof->array[i] = NULL;
+       }
+       bof->array_size = 0;
+       if (bof->file) {
+               fclose(bof->file);
+               bof->file = NULL;
+       }
+       free(bof->array);
+       free(bof->value);
+       free(bof);
+}
+
+static int bof_file_write(bof_t *bof, FILE *file)
+{
+       unsigned i;
+       int r;
+
+       r = fwrite(&bof->type, 4, 1, file);
+       if (r != 1)
+               return -EINVAL;
+       r = fwrite(&bof->size, 4, 1, file);
+       if (r != 1)
+               return -EINVAL;
+       r = fwrite(&bof->array_size, 4, 1, file);
+       if (r != 1)
+               return -EINVAL;
+       switch (bof->type) {
+       case BOF_TYPE_NULL:
+               if (bof->size)
+                       return -EINVAL;
+               break;
+       case BOF_TYPE_STRING:
+       case BOF_TYPE_INT32:
+       case BOF_TYPE_BLOB:
+               r = fwrite(bof->value, bof->size - 12, 1, file);
+               if (r != 1)
+                       return -EINVAL;
+               break;
+       case BOF_TYPE_OBJECT:
+       case BOF_TYPE_ARRAY:
+               for (i = 0; i < bof->array_size; i++) {
+                       r = bof_file_write(bof->array[i], file);
+                       if (r)
+                               return r;
+               }
+               break;
+       default:
+               return -EINVAL;
+       }
+       return 0;
+}
+
+int bof_dump_file(bof_t *bof, const char *filename)
+{
+       unsigned i;
+       int r = 0;
+
+       if (bof->file) {
+               fclose(bof->file);
+               bof->file = NULL;
+       }
+       bof->file = fopen(filename, "w");
+       if (bof->file == NULL) {
+               fprintf(stderr, "%s failed to open file %s\n", __func__, filename);
+               r = -EINVAL;
+               goto out_err;
+       }
+       r = fseek(bof->file, 0L, SEEK_SET);
+       if (r) {
+               fprintf(stderr, "%s failed to seek into file %s\n", __func__, filename);
+               goto out_err;
+       }
+       r = fwrite(&bof->type, 4, 1, bof->file);
+       if (r != 1)
+               goto out_err;
+       r = fwrite(&bof->size, 4, 1, bof->file);
+       if (r != 1)
+               goto out_err;
+       r = fwrite(&bof->array_size, 4, 1, bof->file);
+       if (r != 1)
+               goto out_err;
+       for (i = 0; i < bof->array_size; i++) {
+               r = bof_file_write(bof->array[i], bof->file);
+               if (r)
+                       return r;
+       }
+out_err:
+       fclose(bof->file);
+       bof->file = NULL;
+       return r;
+}
diff --git a/radeon/bof.h b/radeon/bof.h
new file mode 100644 (file)
index 0000000..014affb
--- /dev/null
@@ -0,0 +1,90 @@
+/*
+ * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * on the rights to use, copy, modify, merge, publish, distribute, sub
+ * license, and/or sell copies of the Software, and to permit persons to whom
+ * the Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+ * USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * Authors:
+ *      Jerome Glisse
+ */
+#ifndef BOF_H
+#define BOF_H
+
+#include <stdio.h>
+#include <stdint.h>
+
+#define BOF_TYPE_STRING                0
+#define BOF_TYPE_NULL          1
+#define BOF_TYPE_BLOB          2
+#define BOF_TYPE_OBJECT                3
+#define BOF_TYPE_ARRAY         4
+#define BOF_TYPE_INT32         5
+
+struct bof;
+
+typedef struct bof {
+       struct bof      **array;
+       unsigned        centry;
+       unsigned        nentry;
+       unsigned        refcount;
+       FILE            *file;
+       uint32_t        type;
+       uint32_t        size;
+       uint32_t        array_size;
+       void            *value;
+       long            offset;
+} bof_t;
+
+extern int bof_file_flush(bof_t *root);
+extern bof_t *bof_file_new(const char *filename);
+extern int bof_object_dump(bof_t *object, const char *filename);
+
+/* object */
+extern bof_t *bof_object(void);
+extern bof_t *bof_object_get(bof_t *object, const char *keyname);
+extern int bof_object_set(bof_t *object, const char *keyname, bof_t *value);
+/* array */
+extern bof_t *bof_array(void);
+extern int bof_array_append(bof_t *array, bof_t *value);
+extern bof_t *bof_array_get(bof_t *bof, unsigned i);
+extern unsigned bof_array_size(bof_t *bof);
+/* blob */
+extern bof_t *bof_blob(unsigned size, void *value);
+extern unsigned bof_blob_size(bof_t *bof);
+extern void *bof_blob_value(bof_t *bof);
+/* string */
+extern bof_t *bof_string(const char *value);
+/* int32 */
+extern bof_t *bof_int32(int32_t value);
+extern int32_t bof_int32_value(bof_t *bof);
+/* common functions */
+extern void bof_decref(bof_t *bof);
+extern void bof_incref(bof_t *bof);
+extern bof_t *bof_load_file(const char *filename);
+extern int bof_dump_file(bof_t *bof, const char *filename);
+extern void bof_print(bof_t *bof);
+
+static inline int bof_is_object(bof_t *bof){return (bof->type == BOF_TYPE_OBJECT);}
+static inline int bof_is_blob(bof_t *bof){return (bof->type == BOF_TYPE_BLOB);}
+static inline int bof_is_null(bof_t *bof){return (bof->type == BOF_TYPE_NULL);}
+static inline int bof_is_int32(bof_t *bof){return (bof->type == BOF_TYPE_INT32);}
+static inline int bof_is_array(bof_t *bof){return (bof->type == BOF_TYPE_ARRAY);}
+static inline int bof_is_string(bof_t *bof){return (bof->type == BOF_TYPE_STRING);}
+
+#endif
diff --git a/radeon/libdrm_radeon.pc.in b/radeon/libdrm_radeon.pc.in
new file mode 100644 (file)
index 0000000..68ef0ab
--- /dev/null
@@ -0,0 +1,10 @@
+prefix=@prefix@
+exec_prefix=@exec_prefix@
+libdir=@libdir@
+includedir=@includedir@
+
+Name: libdrm_radeon
+Description: Userspace interface to kernel DRM services for radeon
+Version: @PACKAGE_VERSION@
+Libs: -L${libdir} -ldrm_radeon
+Cflags: -I${includedir} -I${includedir}/libdrm
diff --git a/radeon/radeon_bo.c b/radeon/radeon_bo.c
new file mode 100644 (file)
index 0000000..6a0f8e7
--- /dev/null
@@ -0,0 +1,141 @@
+/*
+ * Copyright Â© 2008 Dave Airlie
+ * Copyright Â© 2008 Jérôme Glisse
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
+ * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+ * USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ */
+/*
+ * Authors:
+ *      Dave Airlie
+ *      Jérôme Glisse <glisse@freedesktop.org>
+ */
+#include <radeon_bo.h>
+#include <radeon_bo_int.h>
+
+void radeon_bo_debug(struct radeon_bo *bo, const char *op)
+{
+    struct radeon_bo_int *boi = (struct radeon_bo_int *)bo;
+
+    fprintf(stderr, "%s %p 0x%08X 0x%08X 0x%08X\n",
+            op, bo, bo->handle, boi->size, boi->cref);
+}
+
+struct radeon_bo *radeon_bo_open(struct radeon_bo_manager *bom,
+                                 uint32_t handle,
+                                 uint32_t size,
+                                 uint32_t alignment,
+                                 uint32_t domains,
+                                 uint32_t flags)
+{
+    struct radeon_bo *bo;
+    bo = bom->funcs->bo_open(bom, handle, size, alignment, domains, flags);
+    return bo;
+}
+
+void radeon_bo_ref(struct radeon_bo *bo)
+{
+    struct radeon_bo_int *boi = (struct radeon_bo_int *)bo;
+    boi->cref++;
+    boi->bom->funcs->bo_ref(boi);
+}
+
+struct radeon_bo *radeon_bo_unref(struct radeon_bo *bo)
+{
+    struct radeon_bo_int *boi = (struct radeon_bo_int *)bo;
+    if (bo == NULL)
+        return NULL;
+
+    boi->cref--;
+    return boi->bom->funcs->bo_unref(boi);
+}
+
+int radeon_bo_map(struct radeon_bo *bo, int write)
+{
+    struct radeon_bo_int *boi = (struct radeon_bo_int *)bo;
+    return boi->bom->funcs->bo_map(boi, write);
+}
+
+int radeon_bo_unmap(struct radeon_bo *bo)
+{
+    struct radeon_bo_int *boi = (struct radeon_bo_int *)bo;
+    return boi->bom->funcs->bo_unmap(boi);
+}
+
+int radeon_bo_wait(struct radeon_bo *bo)
+{
+    struct radeon_bo_int *boi = (struct radeon_bo_int *)bo;
+    if (!boi->bom->funcs->bo_wait)
+        return 0;
+    return boi->bom->funcs->bo_wait(boi);
+}
+
+int radeon_bo_is_busy(struct radeon_bo *bo, uint32_t *domain)
+{
+    struct radeon_bo_int *boi = (struct radeon_bo_int *)bo;
+    return boi->bom->funcs->bo_is_busy(boi, domain);
+}
+
+int radeon_bo_set_tiling(struct radeon_bo *bo,
+                         uint32_t tiling_flags, uint32_t pitch)
+{
+    struct radeon_bo_int *boi = (struct radeon_bo_int *)bo;
+    return boi->bom->funcs->bo_set_tiling(boi, tiling_flags, pitch);
+}
+
+int radeon_bo_get_tiling(struct radeon_bo *bo,
+                         uint32_t *tiling_flags, uint32_t *pitch)
+{
+    struct radeon_bo_int *boi = (struct radeon_bo_int *)bo;
+    return boi->bom->funcs->bo_get_tiling(boi, tiling_flags, pitch);
+}
+
+int radeon_bo_is_static(struct radeon_bo *bo)
+{
+    struct radeon_bo_int *boi = (struct radeon_bo_int *)bo;
+    if (boi->bom->funcs->bo_is_static)
+        return boi->bom->funcs->bo_is_static(boi);
+    return 0;
+}
+
+int radeon_bo_is_referenced_by_cs(struct radeon_bo *bo, struct radeon_cs *cs)
+{
+    struct radeon_bo_int *boi = (struct radeon_bo_int *)bo;
+    return boi->cref > 1;
+}
+
+uint32_t radeon_bo_get_handle(struct radeon_bo *bo)
+{
+    return bo->handle;
+}
+
+uint32_t radeon_bo_get_src_domain(struct radeon_bo *bo)
+{
+    struct radeon_bo_int *boi = (struct radeon_bo_int *)bo;
+    uint32_t src_domain;
+
+    src_domain = boi->space_accounted & 0xffff;
+    if (!src_domain)
+        src_domain = boi->space_accounted >> 16;
+
+    return src_domain;
+}
diff --git a/radeon/radeon_bo.h b/radeon/radeon_bo.h
new file mode 100644 (file)
index 0000000..37478a0
--- /dev/null
@@ -0,0 +1,74 @@
+/*
+ * Copyright Â© 2008 Jérôme Glisse
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
+ * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+ * USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ */
+/*
+ * Authors:
+ *      Jérôme Glisse <glisse@freedesktop.org>
+ */
+#ifndef RADEON_BO_H
+#define RADEON_BO_H
+
+#include <stdio.h>
+#include <stdint.h>
+
+/* bo object */
+#define RADEON_BO_FLAGS_MACRO_TILE  1
+#define RADEON_BO_FLAGS_MICRO_TILE  2
+#define RADEON_BO_FLAGS_MICRO_TILE_SQUARE 0x20
+
+struct radeon_bo_manager;
+struct radeon_cs;
+
+struct radeon_bo {
+    void                        *ptr;
+    uint32_t                    flags;
+    uint32_t                    handle;
+    uint32_t                    size;
+};
+
+struct radeon_bo_manager;
+
+void radeon_bo_debug(struct radeon_bo *bo, const char *op);
+
+struct radeon_bo *radeon_bo_open(struct radeon_bo_manager *bom,
+                                 uint32_t handle,
+                                 uint32_t size,
+                                 uint32_t alignment,
+                                 uint32_t domains,
+                                 uint32_t flags);
+
+void radeon_bo_ref(struct radeon_bo *bo);
+struct radeon_bo *radeon_bo_unref(struct radeon_bo *bo);
+int radeon_bo_map(struct radeon_bo *bo, int write);
+int radeon_bo_unmap(struct radeon_bo *bo);
+int radeon_bo_wait(struct radeon_bo *bo);
+int radeon_bo_is_busy(struct radeon_bo *bo, uint32_t *domain);
+int radeon_bo_set_tiling(struct radeon_bo *bo, uint32_t tiling_flags, uint32_t pitch);
+int radeon_bo_get_tiling(struct radeon_bo *bo, uint32_t *tiling_flags, uint32_t *pitch);
+int radeon_bo_is_static(struct radeon_bo *bo);
+int radeon_bo_is_referenced_by_cs(struct radeon_bo *bo, struct radeon_cs *cs);
+uint32_t radeon_bo_get_handle(struct radeon_bo *bo);
+uint32_t radeon_bo_get_src_domain(struct radeon_bo *bo);
+#endif
diff --git a/radeon/radeon_bo_gem.c b/radeon/radeon_bo_gem.c
new file mode 100644 (file)
index 0000000..719fba7
--- /dev/null
@@ -0,0 +1,351 @@
+/*
+ * Copyright Â© 2008 Dave Airlie
+ * Copyright Â© 2008 Jérôme Glisse
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
+ * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+ * USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ */
+/*
+ * Authors:
+ *      Dave Airlie
+ *      Jérôme Glisse <glisse@freedesktop.org>
+ */
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <errno.h>
+#include "xf86drm.h"
+#include "xf86atomic.h"
+#include "drm.h"
+#include "radeon_drm.h"
+#include "radeon_bo.h"
+#include "radeon_bo_int.h"
+#include "radeon_bo_gem.h"
+
+struct radeon_bo_gem {
+    struct radeon_bo_int base;
+    uint32_t            name;
+    int                 map_count;
+    atomic_t            reloc_in_cs;
+    void *priv_ptr;
+};
+
+struct bo_manager_gem {
+    struct radeon_bo_manager    base;
+};
+
+static int bo_wait(struct radeon_bo_int *boi);
+    
+static struct radeon_bo *bo_open(struct radeon_bo_manager *bom,
+                                 uint32_t handle,
+                                 uint32_t size,
+                                 uint32_t alignment,
+                                 uint32_t domains,
+                                 uint32_t flags)
+{
+    struct radeon_bo_gem *bo;
+    int r;
+
+    bo = (struct radeon_bo_gem*)calloc(1, sizeof(struct radeon_bo_gem));
+    if (bo == NULL) {
+        return NULL;
+    }
+
+    bo->base.bom = bom;
+    bo->base.handle = 0;
+    bo->base.size = size;
+    bo->base.alignment = alignment;
+    bo->base.domains = domains;
+    bo->base.flags = flags;
+    bo->base.ptr = NULL;
+    atomic_set(&bo->reloc_in_cs, 0);
+    bo->map_count = 0;
+    if (handle) {
+        struct drm_gem_open open_arg;
+
+        memset(&open_arg, 0, sizeof(open_arg));
+        open_arg.name = handle;
+        r = drmIoctl(bom->fd, DRM_IOCTL_GEM_OPEN, &open_arg);
+        if (r != 0) {
+            free(bo);
+            return NULL;
+        }
+        bo->base.handle = open_arg.handle;
+        bo->base.size = open_arg.size;
+        bo->name = handle;
+    } else {
+        struct drm_radeon_gem_create args;
+
+        args.size = size;
+        args.alignment = alignment;
+        args.initial_domain = bo->base.domains;
+        args.flags = 0;
+        args.handle = 0;
+        r = drmCommandWriteRead(bom->fd, DRM_RADEON_GEM_CREATE,
+                                &args, sizeof(args));
+        bo->base.handle = args.handle;
+        if (r) {
+            fprintf(stderr, "Failed to allocate :\n");
+            fprintf(stderr, "   size      : %d bytes\n", size);
+            fprintf(stderr, "   alignment : %d bytes\n", alignment);
+            fprintf(stderr, "   domains   : %d\n", bo->base.domains);
+            free(bo);
+            return NULL;
+        }
+    }
+    radeon_bo_ref((struct radeon_bo*)bo);
+    return (struct radeon_bo*)bo;
+}
+
+static void bo_ref(struct radeon_bo_int *boi)
+{
+}
+
+static struct radeon_bo *bo_unref(struct radeon_bo_int *boi)
+{
+    struct radeon_bo_gem *bo_gem = (struct radeon_bo_gem*)boi;
+    struct drm_gem_close args;
+
+    if (boi->cref) {
+        return (struct radeon_bo *)boi;
+    }
+    if (bo_gem->priv_ptr) {
+        munmap(bo_gem->priv_ptr, boi->size);
+    }
+
+    /* Zero out args to make valgrind happy */
+    memset(&args, 0, sizeof(args));
+
+    /* close object */
+    args.handle = boi->handle;
+    drmIoctl(boi->bom->fd, DRM_IOCTL_GEM_CLOSE, &args);
+    memset(bo_gem, 0, sizeof(struct radeon_bo_gem));
+    free(bo_gem);
+    return NULL;
+}
+
+static int bo_map(struct radeon_bo_int *boi, int write)
+{
+    struct radeon_bo_gem *bo_gem = (struct radeon_bo_gem*)boi;
+    struct drm_radeon_gem_mmap args;
+    int r;
+    void *ptr;
+
+    if (bo_gem->map_count++ != 0) {
+        return 0;
+    }
+    if (bo_gem->priv_ptr) {
+        goto wait;
+    }
+
+    boi->ptr = NULL;
+
+    /* Zero out args to make valgrind happy */
+    memset(&args, 0, sizeof(args));
+    args.handle = boi->handle;
+    args.offset = 0;
+    args.size = (uint64_t)boi->size;
+    r = drmCommandWriteRead(boi->bom->fd,
+                            DRM_RADEON_GEM_MMAP,
+                            &args,
+                            sizeof(args));
+    if (r) {
+        fprintf(stderr, "error mapping %p 0x%08X (error = %d)\n",
+                boi, boi->handle, r);
+        return r;
+    }
+    ptr = mmap(0, args.size, PROT_READ|PROT_WRITE, MAP_SHARED, boi->bom->fd, args.addr_ptr);
+    if (ptr == MAP_FAILED)
+        return -errno;
+    bo_gem->priv_ptr = ptr;
+wait:
+    boi->ptr = bo_gem->priv_ptr;
+    r = bo_wait(boi);
+    if (r)
+        return r;
+    return 0;
+}
+
+static int bo_unmap(struct radeon_bo_int *boi)
+{
+    struct radeon_bo_gem *bo_gem = (struct radeon_bo_gem*)boi;
+
+    if (--bo_gem->map_count > 0) {
+        return 0;
+    }
+    //munmap(bo->ptr, bo->size);
+    boi->ptr = NULL;
+    return 0;
+}
+
+static int bo_wait(struct radeon_bo_int *boi)
+{
+    struct drm_radeon_gem_wait_idle args;
+    int ret;
+
+    /* Zero out args to make valgrind happy */
+    memset(&args, 0, sizeof(args));
+    args.handle = boi->handle;
+    do {
+        ret = drmCommandWriteRead(boi->bom->fd, DRM_RADEON_GEM_WAIT_IDLE,
+                                  &args, sizeof(args));
+    } while (ret == -EBUSY);
+    return ret;
+}
+
+static int bo_is_busy(struct radeon_bo_int *boi, uint32_t *domain)
+{
+    struct drm_radeon_gem_busy args;
+    int ret;
+
+    args.handle = boi->handle;
+    args.domain = 0;
+
+    ret = drmCommandWriteRead(boi->bom->fd, DRM_RADEON_GEM_BUSY,
+                              &args, sizeof(args));
+
+    *domain = args.domain;
+    return ret;
+}
+
+static int bo_set_tiling(struct radeon_bo_int *boi, uint32_t tiling_flags,
+                         uint32_t pitch)
+{
+    struct drm_radeon_gem_set_tiling args;
+    int r;
+
+    args.handle = boi->handle;
+    args.tiling_flags = tiling_flags;
+    args.pitch = pitch;
+
+    r = drmCommandWriteRead(boi->bom->fd,
+                            DRM_RADEON_GEM_SET_TILING,
+                            &args,
+                            sizeof(args));
+    return r;
+}
+
+static int bo_get_tiling(struct radeon_bo_int *boi, uint32_t *tiling_flags,
+                         uint32_t *pitch)
+{
+    struct drm_radeon_gem_set_tiling args = {};
+    int r;
+
+    args.handle = boi->handle;
+
+    r = drmCommandWriteRead(boi->bom->fd,
+                            DRM_RADEON_GEM_GET_TILING,
+                            &args,
+                            sizeof(args));
+
+    if (r)
+        return r;
+
+    *tiling_flags = args.tiling_flags;
+    *pitch = args.pitch;
+    return r;
+}
+
+static struct radeon_bo_funcs bo_gem_funcs = {
+    bo_open,
+    bo_ref,
+    bo_unref,
+    bo_map,
+    bo_unmap,
+    bo_wait,
+    NULL,
+    bo_set_tiling,
+    bo_get_tiling,
+    bo_is_busy,
+};
+
+struct radeon_bo_manager *radeon_bo_manager_gem_ctor(int fd)
+{
+    struct bo_manager_gem *bomg;
+
+    bomg = (struct bo_manager_gem*)calloc(1, sizeof(struct bo_manager_gem));
+    if (bomg == NULL) {
+        return NULL;
+    }
+    bomg->base.funcs = &bo_gem_funcs;
+    bomg->base.fd = fd;
+    return (struct radeon_bo_manager*)bomg;
+}
+
+void radeon_bo_manager_gem_dtor(struct radeon_bo_manager *bom)
+{
+    struct bo_manager_gem *bomg = (struct bo_manager_gem*)bom;
+
+    if (bom == NULL) {
+        return;
+    }
+    free(bomg);
+}
+
+uint32_t radeon_gem_name_bo(struct radeon_bo *bo)
+{
+    struct radeon_bo_gem *bo_gem = (struct radeon_bo_gem*)bo;
+    return bo_gem->name;
+}
+
+void *radeon_gem_get_reloc_in_cs(struct radeon_bo *bo)
+{
+    struct radeon_bo_gem *bo_gem = (struct radeon_bo_gem*)bo;
+    return &bo_gem->reloc_in_cs;
+}
+
+int radeon_gem_get_kernel_name(struct radeon_bo *bo, uint32_t *name)
+{
+    struct radeon_bo_int *boi = (struct radeon_bo_int *)bo;
+    struct drm_gem_flink flink;
+    int r;
+
+    flink.handle = bo->handle;
+    r = drmIoctl(boi->bom->fd, DRM_IOCTL_GEM_FLINK, &flink);
+    if (r) {
+        return r;
+    }
+    *name = flink.name;
+    return 0;
+}
+
+int radeon_gem_set_domain(struct radeon_bo *bo, uint32_t read_domains, uint32_t write_domain)
+{
+    struct radeon_bo_int *boi = (struct radeon_bo_int *)bo;
+    struct drm_radeon_gem_set_domain args;
+    int r;
+
+    args.handle = bo->handle;
+    args.read_domains = read_domains;
+    args.write_domain = write_domain;
+
+    r = drmCommandWriteRead(boi->bom->fd,
+                            DRM_RADEON_GEM_SET_DOMAIN,
+                            &args,
+                            sizeof(args));
+    return r;
+}
diff --git a/radeon/radeon_bo_gem.h b/radeon/radeon_bo_gem.h
new file mode 100644 (file)
index 0000000..0af8610
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * Copyright Â© 2008 Dave Airlie
+ * Copyright Â© 2008 Jérôme Glisse
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
+ * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+ * USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ */
+/*
+ * Authors:
+ *      Dave Airlie
+ *      Jérôme Glisse <glisse@freedesktop.org>
+ */
+#ifndef RADEON_BO_GEM_H
+#define RADEON_BO_GEM_H
+
+#include "radeon_bo.h"
+
+struct radeon_bo_manager *radeon_bo_manager_gem_ctor(int fd);
+void radeon_bo_manager_gem_dtor(struct radeon_bo_manager *bom);
+
+uint32_t radeon_gem_name_bo(struct radeon_bo *bo);
+void *radeon_gem_get_reloc_in_cs(struct radeon_bo *bo);
+int radeon_gem_set_domain(struct radeon_bo *bo, uint32_t read_domains, uint32_t write_domain);
+int radeon_gem_get_kernel_name(struct radeon_bo *bo, uint32_t *name);
+#endif
diff --git a/radeon/radeon_bo_int.h b/radeon/radeon_bo_int.h
new file mode 100644 (file)
index 0000000..9589ead
--- /dev/null
@@ -0,0 +1,45 @@
+#ifndef RADEON_BO_INT
+#define RADEON_BO_INT
+
+struct radeon_bo_manager {
+    struct radeon_bo_funcs  *funcs;
+    int                     fd;
+};
+
+struct radeon_bo_int {
+    void                        *ptr;
+    uint32_t                    flags;
+    uint32_t                    handle;
+    uint32_t                    size;
+    /* private members */
+    uint32_t                    alignment;
+    uint32_t                    domains;
+    unsigned                    cref;
+    struct radeon_bo_manager    *bom;
+    uint32_t                    space_accounted;
+    uint32_t                    referenced_in_cs;
+};
+
+/* bo functions */
+struct radeon_bo_funcs {
+    struct radeon_bo *(*bo_open)(struct radeon_bo_manager *bom,
+                                 uint32_t handle,
+                                 uint32_t size,
+                                 uint32_t alignment,
+                                 uint32_t domains,
+                                 uint32_t flags);
+    void (*bo_ref)(struct radeon_bo_int *bo);
+    struct radeon_bo *(*bo_unref)(struct radeon_bo_int *bo);
+    int (*bo_map)(struct radeon_bo_int *bo, int write);
+    int (*bo_unmap)(struct radeon_bo_int *bo);
+    int (*bo_wait)(struct radeon_bo_int *bo);
+    int (*bo_is_static)(struct radeon_bo_int *bo);
+    int (*bo_set_tiling)(struct radeon_bo_int *bo, uint32_t tiling_flags,
+                         uint32_t pitch);
+    int (*bo_get_tiling)(struct radeon_bo_int *bo, uint32_t *tiling_flags,
+                         uint32_t *pitch);
+    int (*bo_is_busy)(struct radeon_bo_int *bo, uint32_t *domain);
+    int (*bo_is_referenced_by_cs)(struct radeon_bo_int *bo, struct radeon_cs *cs);
+};
+
+#endif
diff --git a/radeon/radeon_cs.c b/radeon/radeon_cs.c
new file mode 100644 (file)
index 0000000..d0e922b
--- /dev/null
@@ -0,0 +1,96 @@
+
+#include <stdio.h>
+#include "radeon_cs.h"
+#include "radeon_cs_int.h"
+
+struct radeon_cs *radeon_cs_create(struct radeon_cs_manager *csm, uint32_t ndw)
+{
+    struct radeon_cs_int *csi = csm->funcs->cs_create(csm, ndw);
+    return (struct radeon_cs *)csi;
+}
+
+int radeon_cs_write_reloc(struct radeon_cs *cs,
+                          struct radeon_bo *bo,
+                          uint32_t read_domain,
+                          uint32_t write_domain,
+                          uint32_t flags)
+{
+    struct radeon_cs_int *csi = (struct radeon_cs_int *)cs;
+
+    return csi->csm->funcs->cs_write_reloc(csi,
+                                           bo,
+                                           read_domain,
+                                           write_domain,
+                                           flags);
+}
+
+int radeon_cs_begin(struct radeon_cs *cs,
+                    uint32_t ndw,
+                    const char *file,
+                    const char *func,
+                    int line)
+{
+    struct radeon_cs_int *csi = (struct radeon_cs_int *)cs;
+    return csi->csm->funcs->cs_begin(csi, ndw, file, func, line);
+}
+
+int radeon_cs_end(struct radeon_cs *cs,
+                  const char *file,
+                  const char *func,
+                  int line)
+{
+    struct radeon_cs_int *csi = (struct radeon_cs_int *)cs;
+    return csi->csm->funcs->cs_end(csi, file, func, line);
+}
+
+int radeon_cs_emit(struct radeon_cs *cs)
+{
+    struct radeon_cs_int *csi = (struct radeon_cs_int *)cs;
+    return csi->csm->funcs->cs_emit(csi);
+}
+
+int radeon_cs_destroy(struct radeon_cs *cs)
+{
+    struct radeon_cs_int *csi = (struct radeon_cs_int *)cs;
+    return csi->csm->funcs->cs_destroy(csi);
+}
+
+int radeon_cs_erase(struct radeon_cs *cs)
+{
+    struct radeon_cs_int *csi = (struct radeon_cs_int *)cs;
+    return csi->csm->funcs->cs_erase(csi);
+}
+
+int radeon_cs_need_flush(struct radeon_cs *cs)
+{
+    struct radeon_cs_int *csi = (struct radeon_cs_int *)cs;
+    return csi->csm->funcs->cs_need_flush(csi);
+}
+
+void radeon_cs_print(struct radeon_cs *cs, FILE *file)
+{
+    struct radeon_cs_int *csi = (struct radeon_cs_int *)cs;
+    csi->csm->funcs->cs_print(csi, file);
+}
+
+void radeon_cs_set_limit(struct radeon_cs *cs, uint32_t domain, uint32_t limit)
+{
+    struct radeon_cs_int *csi = (struct radeon_cs_int *)cs;
+    if (domain == RADEON_GEM_DOMAIN_VRAM)
+        csi->csm->vram_limit = limit;
+    else
+        csi->csm->gart_limit = limit;
+}
+
+void radeon_cs_space_set_flush(struct radeon_cs *cs, void (*fn)(void *), void *data)
+{
+    struct radeon_cs_int *csi = (struct radeon_cs_int *)cs;
+    csi->space_flush_fn = fn;
+    csi->space_flush_data = data;
+}
+
+uint32_t radeon_cs_get_id(struct radeon_cs *cs)
+{
+    struct radeon_cs_int *csi = (struct radeon_cs_int *)cs;
+    return csi->id;
+}
diff --git a/radeon/radeon_cs.h b/radeon/radeon_cs.h
new file mode 100644 (file)
index 0000000..f68a624
--- /dev/null
@@ -0,0 +1,141 @@
+/*
+ * Copyright Â© 2008 Nicolai Haehnle
+ * Copyright Â© 2008 Jérôme Glisse
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+ * USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ */
+/*
+ * Authors:
+ *      Aapo Tahkola <aet@rasterburn.org>
+ *      Nicolai Haehnle <prefect_@gmx.net>
+ *      Jérôme Glisse <glisse@freedesktop.org>
+ */
+#ifndef RADEON_CS_H
+#define RADEON_CS_H
+
+#include <stdint.h>
+#include <string.h>
+#include "drm.h"
+#include "radeon_drm.h"
+#include "radeon_bo.h"
+
+struct radeon_cs_reloc {
+    struct radeon_bo    *bo;
+    uint32_t            read_domain;
+    uint32_t            write_domain;
+    uint32_t            flags;
+};
+
+
+#define RADEON_CS_SPACE_OK 0
+#define RADEON_CS_SPACE_OP_TO_BIG 1
+#define RADEON_CS_SPACE_FLUSH 2
+
+struct radeon_cs {
+    uint32_t *packets;
+    unsigned cdw;
+    unsigned ndw;
+    unsigned                    section_ndw;
+    unsigned                    section_cdw;
+};
+
+#define MAX_SPACE_BOS (32)
+
+struct radeon_cs_manager;
+
+extern struct radeon_cs *radeon_cs_create(struct radeon_cs_manager *csm,
+                                          uint32_t ndw);
+
+extern int radeon_cs_begin(struct radeon_cs *cs,
+                           uint32_t ndw,
+                           const char *file,
+                           const char *func, int line);
+extern int radeon_cs_end(struct radeon_cs *cs,
+                         const char *file,
+                         const char *func,
+                         int line);
+extern int radeon_cs_emit(struct radeon_cs *cs);
+extern int radeon_cs_destroy(struct radeon_cs *cs);
+extern int radeon_cs_erase(struct radeon_cs *cs);
+extern int radeon_cs_need_flush(struct radeon_cs *cs);
+extern void radeon_cs_print(struct radeon_cs *cs, FILE *file);
+extern void radeon_cs_set_limit(struct radeon_cs *cs, uint32_t domain, uint32_t limit);
+extern void radeon_cs_space_set_flush(struct radeon_cs *cs, void (*fn)(void *), void *data);
+extern int radeon_cs_write_reloc(struct radeon_cs *cs,
+                                 struct radeon_bo *bo,
+                                 uint32_t read_domain,
+                                 uint32_t write_domain,
+                                 uint32_t flags);
+extern uint32_t radeon_cs_get_id(struct radeon_cs *cs);
+/*
+ * add a persistent BO to the list
+ * a persistent BO is one that will be referenced across flushes,
+ * i.e. colorbuffer, textures etc.
+ * They get reset when a new "operation" happens, where an operation
+ * is a state emission with a color/textures etc followed by a bunch of vertices.
+ */
+void radeon_cs_space_add_persistent_bo(struct radeon_cs *cs,
+                                       struct radeon_bo *bo,
+                                       uint32_t read_domains,
+                                       uint32_t write_domain);
+
+/* reset the persistent BO list */
+void radeon_cs_space_reset_bos(struct radeon_cs *cs);
+
+/* do a space check with the current persistent BO list */
+int radeon_cs_space_check(struct radeon_cs *cs);
+
+/* do a space check with the current persistent BO list and a temporary BO
+ * a temporary BO is like a DMA buffer, which  gets flushed with the
+ * command buffer */
+int radeon_cs_space_check_with_bo(struct radeon_cs *cs,
+                                  struct radeon_bo *bo,
+                                  uint32_t read_domains,
+                                  uint32_t write_domain);
+
+static inline void radeon_cs_write_dword(struct radeon_cs *cs, uint32_t dword)
+{
+    cs->packets[cs->cdw++] = dword;
+    if (cs->section_ndw) {
+        cs->section_cdw++;
+    }
+}
+
+static inline void radeon_cs_write_qword(struct radeon_cs *cs, uint64_t qword)
+{
+    memcpy(cs->packets + cs->cdw, &qword, sizeof(uint64_t));
+    cs->cdw += 2;
+    if (cs->section_ndw) {
+        cs->section_cdw += 2;
+    }
+}
+
+static inline void radeon_cs_write_table(struct radeon_cs *cs,
+                                         const void *data, uint32_t size)
+{
+    memcpy(cs->packets + cs->cdw, data, size * 4);
+    cs->cdw += size;
+    if (cs->section_ndw) {
+        cs->section_cdw += size;
+    }
+}
+#endif
diff --git a/radeon/radeon_cs_gem.c b/radeon/radeon_cs_gem.c
new file mode 100644 (file)
index 0000000..9834bcf
--- /dev/null
@@ -0,0 +1,548 @@
+/*
+ * Copyright Â© 2008 Jérôme Glisse
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
+ * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+ * USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ */
+/*
+ * Authors:
+ *      Aapo Tahkola <aet@rasterburn.org>
+ *      Nicolai Haehnle <prefect_@gmx.net>
+ *      Jérôme Glisse <glisse@freedesktop.org>
+ */
+#include <assert.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <pthread.h>
+#include <sys/mman.h>
+#include <sys/ioctl.h>
+#include "radeon_cs.h"
+#include "radeon_cs_int.h"
+#include "radeon_bo_int.h"
+#include "radeon_cs_gem.h"
+#include "radeon_bo_gem.h"
+#include "drm.h"
+#include "xf86drm.h"
+#include "xf86atomic.h"
+#include "radeon_drm.h"
+#include "bof.h"
+
+#define CS_BOF_DUMP 0
+
+struct radeon_cs_manager_gem {
+    struct radeon_cs_manager    base;
+    uint32_t                    device_id;
+    unsigned                    nbof;
+};
+
+#pragma pack(1)
+struct cs_reloc_gem {
+    uint32_t    handle;
+    uint32_t    read_domain;
+    uint32_t    write_domain;
+    uint32_t    flags;
+};
+
+#pragma pack()
+#define RELOC_SIZE (sizeof(struct cs_reloc_gem) / sizeof(uint32_t))
+
+struct cs_gem {
+    struct radeon_cs_int        base;
+    struct drm_radeon_cs        cs;
+    struct drm_radeon_cs_chunk  chunks[2];
+    unsigned                    nrelocs;
+    uint32_t                    *relocs;
+    struct radeon_bo_int        **relocs_bo;
+};
+
+static pthread_mutex_t id_mutex = PTHREAD_MUTEX_INITIALIZER;
+static uint32_t cs_id_source = 0;
+
+/**
+ * result is undefined if called with ~0
+ */
+static uint32_t get_first_zero(const uint32_t n)
+{
+    /* __builtin_ctz returns number of trailing zeros. */
+    return 1 << __builtin_ctz(~n);
+}
+
+/**
+ * Returns a free id for cs.
+ * If there is no free id we return zero
+ **/
+static uint32_t generate_id(void)
+{
+    uint32_t r = 0;
+    pthread_mutex_lock( &id_mutex );
+    /* check for free ids */
+    if (cs_id_source != ~r) {
+        /* find first zero bit */
+        r = get_first_zero(cs_id_source);
+
+        /* set id as reserved */
+        cs_id_source |= r;
+    }
+    pthread_mutex_unlock( &id_mutex );
+    return r;
+}
+
+/**
+ * Free the id for later reuse
+ **/
+static void free_id(uint32_t id)
+{
+    pthread_mutex_lock( &id_mutex );
+
+    cs_id_source &= ~id;
+
+    pthread_mutex_unlock( &id_mutex );
+}
+
+static struct radeon_cs_int *cs_gem_create(struct radeon_cs_manager *csm,
+                                       uint32_t ndw)
+{
+    struct cs_gem *csg;
+
+    /* max cmd buffer size is 64Kb */
+    if (ndw > (64 * 1024 / 4)) {
+        return NULL;
+    }
+    csg = (struct cs_gem*)calloc(1, sizeof(struct cs_gem));
+    if (csg == NULL) {
+        return NULL;
+    }
+    csg->base.csm = csm;
+    csg->base.ndw = 64 * 1024 / 4;
+    csg->base.packets = (uint32_t*)calloc(1, 64 * 1024);
+    if (csg->base.packets == NULL) {
+        free(csg);
+        return NULL;
+    }
+    csg->base.relocs_total_size = 0;
+    csg->base.crelocs = 0;
+    csg->base.id = generate_id();
+    csg->nrelocs = 4096 / (4 * 4) ;
+    csg->relocs_bo = (struct radeon_bo_int**)calloc(1,
+                                                csg->nrelocs*sizeof(void*));
+    if (csg->relocs_bo == NULL) {
+        free(csg->base.packets);
+        free(csg);
+        return NULL;
+    }
+    csg->base.relocs = csg->relocs = (uint32_t*)calloc(1, 4096);
+    if (csg->relocs == NULL) {
+        free(csg->relocs_bo);
+        free(csg->base.packets);
+        free(csg);
+        return NULL;
+    }
+    csg->chunks[0].chunk_id = RADEON_CHUNK_ID_IB;
+    csg->chunks[0].length_dw = 0;
+    csg->chunks[0].chunk_data = (uint64_t)(uintptr_t)csg->base.packets;
+    csg->chunks[1].chunk_id = RADEON_CHUNK_ID_RELOCS;
+    csg->chunks[1].length_dw = 0;
+    csg->chunks[1].chunk_data = (uint64_t)(uintptr_t)csg->relocs;
+    return (struct radeon_cs_int*)csg;
+}
+
+static int cs_gem_write_reloc(struct radeon_cs_int *cs,
+                              struct radeon_bo *bo,
+                              uint32_t read_domain,
+                              uint32_t write_domain,
+                              uint32_t flags)
+{
+    struct radeon_bo_int *boi = (struct radeon_bo_int *)bo;
+    struct cs_gem *csg = (struct cs_gem*)cs;
+    struct cs_reloc_gem *reloc;
+    uint32_t idx;
+    unsigned i;
+
+    assert(boi->space_accounted);
+
+    /* check domains */
+    if ((read_domain && write_domain) || (!read_domain && !write_domain)) {
+        /* in one CS a bo can only be in read or write domain but not
+         * in read & write domain at the same sime
+         */
+        return -EINVAL;
+    }
+    if (read_domain == RADEON_GEM_DOMAIN_CPU) {
+        return -EINVAL;
+    }
+    if (write_domain == RADEON_GEM_DOMAIN_CPU) {
+        return -EINVAL;
+    }
+    /* use bit field hash function to determine
+       if this bo is for sure not in this cs.*/
+    if ((atomic_read((atomic_t *)radeon_gem_get_reloc_in_cs(bo)) & cs->id)) {
+        /* check if bo is already referenced.
+         * Scanning from end to begin reduces cycles with mesa because
+         * it often relocates same shared dma bo again. */
+        for(i = cs->crelocs; i != 0;) {
+            --i;
+            idx = i * RELOC_SIZE;
+            reloc = (struct cs_reloc_gem*)&csg->relocs[idx];
+            if (reloc->handle == bo->handle) {
+                /* Check domains must be in read or write. As we check already
+                 * checked that in argument one of the read or write domain was
+                 * set we only need to check that if previous reloc as the read
+                 * domain set then the read_domain should also be set for this
+                 * new relocation.
+                 */
+                /* the DDX expects to read and write from same pixmap */
+                if (write_domain && (reloc->read_domain & write_domain)) {
+                    reloc->read_domain = 0;
+                    reloc->write_domain = write_domain;
+                } else if (read_domain & reloc->write_domain) {
+                    reloc->read_domain = 0;
+                } else {
+                    if (write_domain != reloc->write_domain)
+                        return -EINVAL;
+                    if (read_domain != reloc->read_domain)
+                        return -EINVAL;
+                }
+
+                reloc->read_domain |= read_domain;
+                reloc->write_domain |= write_domain;
+                /* update flags */
+                reloc->flags |= (flags & reloc->flags);
+                /* write relocation packet */
+                radeon_cs_write_dword((struct radeon_cs *)cs, 0xc0001000);
+                radeon_cs_write_dword((struct radeon_cs *)cs, idx);
+                return 0;
+            }
+        }
+    }
+    /* new relocation */
+    if (csg->base.crelocs >= csg->nrelocs) {
+        /* allocate more memory (TODO: should use a slab allocatore maybe) */
+        uint32_t *tmp, size;
+        size = ((csg->nrelocs + 1) * sizeof(struct radeon_bo*));
+        tmp = (uint32_t*)realloc(csg->relocs_bo, size);
+        if (tmp == NULL) {
+            return -ENOMEM;
+        }
+        csg->relocs_bo = (struct radeon_bo_int **)tmp;
+        size = ((csg->nrelocs + 1) * RELOC_SIZE * 4);
+        tmp = (uint32_t*)realloc(csg->relocs, size);
+        if (tmp == NULL) {
+            return -ENOMEM;
+        }
+        cs->relocs = csg->relocs = tmp;
+        csg->nrelocs += 1;
+        csg->chunks[1].chunk_data = (uint64_t)(uintptr_t)csg->relocs;
+    }
+    csg->relocs_bo[csg->base.crelocs] = boi;
+    idx = (csg->base.crelocs++) * RELOC_SIZE;
+    reloc = (struct cs_reloc_gem*)&csg->relocs[idx];
+    reloc->handle = bo->handle;
+    reloc->read_domain = read_domain;
+    reloc->write_domain = write_domain;
+    reloc->flags = flags;
+    csg->chunks[1].length_dw += RELOC_SIZE;
+    radeon_bo_ref(bo);
+    /* bo might be referenced from another context so have to use atomic opertions */
+    atomic_add((atomic_t *)radeon_gem_get_reloc_in_cs(bo), cs->id);
+    cs->relocs_total_size += boi->size;
+    radeon_cs_write_dword((struct radeon_cs *)cs, 0xc0001000);
+    radeon_cs_write_dword((struct radeon_cs *)cs, idx);
+    return 0;
+}
+
+static int cs_gem_begin(struct radeon_cs_int *cs,
+                        uint32_t ndw,
+                        const char *file,
+                        const char *func,
+                        int line)
+{
+
+    if (cs->section_ndw) {
+        fprintf(stderr, "CS already in a section(%s,%s,%d)\n",
+                cs->section_file, cs->section_func, cs->section_line);
+        fprintf(stderr, "CS can't start section(%s,%s,%d)\n",
+                file, func, line);
+        return -EPIPE;
+    }
+    cs->section_ndw = ndw;
+    cs->section_cdw = 0;
+    cs->section_file = file;
+    cs->section_func = func;
+    cs->section_line = line;
+
+    if (cs->cdw + ndw > cs->ndw) {
+        uint32_t tmp, *ptr;
+
+        /* round up the required size to a multiple of 1024 */
+        tmp = (cs->cdw + ndw + 0x3FF) & (~0x3FF);
+        ptr = (uint32_t*)realloc(cs->packets, 4 * tmp);
+        if (ptr == NULL) {
+            return -ENOMEM;
+        }
+        cs->packets = ptr;
+        cs->ndw = tmp;
+    }
+    return 0;
+}
+
+static int cs_gem_end(struct radeon_cs_int *cs,
+                      const char *file,
+                      const char *func,
+                      int line)
+
+{
+    if (!cs->section_ndw) {
+        fprintf(stderr, "CS no section to end at (%s,%s,%d)\n",
+                file, func, line);
+        return -EPIPE;
+    }
+    if (cs->section_ndw != cs->section_cdw) {
+        fprintf(stderr, "CS section size missmatch start at (%s,%s,%d) %d vs %d\n",
+                cs->section_file, cs->section_func, cs->section_line, cs->section_ndw, cs->section_cdw);
+        fprintf(stderr, "CS section end at (%s,%s,%d)\n",
+                file, func, line);
+
+        /* We must reset the section even when there is error. */
+        cs->section_ndw = 0;
+        return -EPIPE;
+    }
+    cs->section_ndw = 0;
+    return 0;
+}
+
+static void cs_gem_dump_bof(struct radeon_cs_int *cs)
+{
+    struct cs_gem *csg = (struct cs_gem*)cs;
+    struct radeon_cs_manager_gem *csm;
+    bof_t *bcs, *blob, *array, *bo, *size, *handle, *device_id, *root;
+    char tmp[256];
+    unsigned i;
+
+    csm = (struct radeon_cs_manager_gem *)cs->csm;
+    root = device_id = bcs = blob = array = bo = size = handle = NULL;
+    root = bof_object();
+    if (root == NULL)
+        goto out_err;
+    device_id = bof_int32(csm->device_id);
+    if (device_id == NULL)
+        return;
+    if (bof_object_set(root, "device_id", device_id))
+        goto out_err;
+    bof_decref(device_id);
+    device_id = NULL;
+    /* dump relocs */
+    blob = bof_blob(csg->nrelocs * 16, csg->relocs);
+    if (blob == NULL)
+        goto out_err;
+    if (bof_object_set(root, "reloc", blob))
+        goto out_err;
+    bof_decref(blob);
+    blob = NULL;
+    /* dump cs */
+    blob = bof_blob(cs->cdw * 4, cs->packets);
+    if (blob == NULL)
+        goto out_err;
+    if (bof_object_set(root, "pm4", blob))
+        goto out_err;
+    bof_decref(blob);
+    blob = NULL;
+    /* dump bo */
+    array = bof_array();
+    if (array == NULL)
+        goto out_err;
+    for (i = 0; i < csg->base.crelocs; i++) {
+        bo = bof_object();
+        if (bo == NULL)
+            goto out_err;
+        size = bof_int32(csg->relocs_bo[i]->size);
+        if (size == NULL)
+            goto out_err;
+        if (bof_object_set(bo, "size", size))
+            goto out_err;
+        bof_decref(size);
+        size = NULL;
+        handle = bof_int32(csg->relocs_bo[i]->handle);
+        if (handle == NULL)
+            goto out_err;
+        if (bof_object_set(bo, "handle", handle))
+            goto out_err;
+        bof_decref(handle);
+        handle = NULL;
+        radeon_bo_map((struct radeon_bo*)csg->relocs_bo[i], 0);
+        blob = bof_blob(csg->relocs_bo[i]->size, csg->relocs_bo[i]->ptr);
+        radeon_bo_unmap((struct radeon_bo*)csg->relocs_bo[i]);
+        if (blob == NULL)
+            goto out_err;
+        if (bof_object_set(bo, "data", blob))
+            goto out_err;
+        bof_decref(blob);
+        blob = NULL;
+        if (bof_array_append(array, bo))
+            goto out_err;
+        bof_decref(bo);
+        bo = NULL;
+    }
+    if (bof_object_set(root, "bo", array))
+        goto out_err;
+    sprintf(tmp, "d-0x%04X-%08d.bof", csm->device_id, csm->nbof++);
+    bof_dump_file(root, tmp);
+out_err:
+    bof_decref(blob);
+    bof_decref(array);
+    bof_decref(bo);
+    bof_decref(size);
+    bof_decref(handle);
+    bof_decref(device_id);
+    bof_decref(root);
+}
+
+static int cs_gem_emit(struct radeon_cs_int *cs)
+{
+    struct cs_gem *csg = (struct cs_gem*)cs;
+    uint64_t chunk_array[2];
+    unsigned i;
+    int r;
+
+#if CS_BOF_DUMP
+    cs_gem_dump_bof(cs);
+#endif
+    csg->chunks[0].length_dw = cs->cdw;
+
+    chunk_array[0] = (uint64_t)(uintptr_t)&csg->chunks[0];
+    chunk_array[1] = (uint64_t)(uintptr_t)&csg->chunks[1];
+
+    csg->cs.num_chunks = 2;
+    csg->cs.chunks = (uint64_t)(uintptr_t)chunk_array;
+
+    r = drmCommandWriteRead(cs->csm->fd, DRM_RADEON_CS,
+                            &csg->cs, sizeof(struct drm_radeon_cs));
+    for (i = 0; i < csg->base.crelocs; i++) {
+        csg->relocs_bo[i]->space_accounted = 0;
+        /* bo might be referenced from another context so have to use atomic opertions */
+        atomic_dec((atomic_t *)radeon_gem_get_reloc_in_cs((struct radeon_bo*)csg->relocs_bo[i]), cs->id);
+        radeon_bo_unref((struct radeon_bo *)csg->relocs_bo[i]);
+        csg->relocs_bo[i] = NULL;
+    }
+
+    cs->csm->read_used = 0;
+    cs->csm->vram_write_used = 0;
+    cs->csm->gart_write_used = 0;
+    return r;
+}
+
+static int cs_gem_destroy(struct radeon_cs_int *cs)
+{
+    struct cs_gem *csg = (struct cs_gem*)cs;
+
+    free_id(cs->id);
+    free(csg->relocs_bo);
+    free(cs->relocs);
+    free(cs->packets);
+    free(cs);
+    return 0;
+}
+
+static int cs_gem_erase(struct radeon_cs_int *cs)
+{
+    struct cs_gem *csg = (struct cs_gem*)cs;
+    unsigned i;
+
+    if (csg->relocs_bo) {
+        for (i = 0; i < csg->base.crelocs; i++) {
+            if (csg->relocs_bo[i]) {
+                /* bo might be referenced from another context so have to use atomic opertions */
+                atomic_dec((atomic_t *)radeon_gem_get_reloc_in_cs((struct radeon_bo*)csg->relocs_bo[i]), cs->id);
+                radeon_bo_unref((struct radeon_bo *)csg->relocs_bo[i]);
+                csg->relocs_bo[i] = NULL;
+            }
+        }
+    }
+    cs->relocs_total_size = 0;
+    cs->cdw = 0;
+    cs->section_ndw = 0;
+    cs->crelocs = 0;
+    csg->chunks[0].length_dw = 0;
+    csg->chunks[1].length_dw = 0;
+    return 0;
+}
+
+static int cs_gem_need_flush(struct radeon_cs_int *cs)
+{
+    return 0; //(cs->relocs_total_size > (32*1024*1024));
+}
+
+static void cs_gem_print(struct radeon_cs_int *cs, FILE *file)
+{
+    struct radeon_cs_manager_gem *csm;
+    unsigned int i;
+
+    csm = (struct radeon_cs_manager_gem *)cs->csm;
+    fprintf(file, "VENDORID:DEVICEID 0x%04X:0x%04X\n", 0x1002, csm->device_id);
+    for (i = 0; i < cs->cdw; i++) {
+        fprintf(file, "0x%08X\n", cs->packets[i]);
+    }
+}
+
+static struct radeon_cs_funcs radeon_cs_gem_funcs = {
+    cs_gem_create,
+    cs_gem_write_reloc,
+    cs_gem_begin,
+    cs_gem_end,
+    cs_gem_emit,
+    cs_gem_destroy,
+    cs_gem_erase,
+    cs_gem_need_flush,
+    cs_gem_print,
+};
+
+static int radeon_get_device_id(int fd, uint32_t *device_id)
+{
+    struct drm_radeon_info info = {};
+    int r;
+
+    *device_id = 0;
+    info.request = RADEON_INFO_DEVICE_ID;
+    info.value = (uintptr_t)device_id;
+    r = drmCommandWriteRead(fd, DRM_RADEON_INFO, &info,
+                            sizeof(struct drm_radeon_info));
+    return r;
+}
+
+struct radeon_cs_manager *radeon_cs_manager_gem_ctor(int fd)
+{
+    struct radeon_cs_manager_gem *csm;
+
+    csm = calloc(1, sizeof(struct radeon_cs_manager_gem));
+    if (csm == NULL) {
+        return NULL;
+    }
+    csm->base.funcs = &radeon_cs_gem_funcs;
+    csm->base.fd = fd;
+    radeon_get_device_id(fd, &csm->device_id);
+    return &csm->base;
+}
+
+void radeon_cs_manager_gem_dtor(struct radeon_cs_manager *csm)
+{
+    free(csm);
+}
diff --git a/radeon/radeon_cs_gem.h b/radeon/radeon_cs_gem.h
new file mode 100644 (file)
index 0000000..5dea38a
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * Copyright Â© 2008 Nicolai Haehnle
+ * Copyright Â© 2008 Jérôme Glisse
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
+ * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+ * USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ */
+/*
+ * Authors:
+ *      Aapo Tahkola <aet@rasterburn.org>
+ *      Nicolai Haehnle <prefect_@gmx.net>
+ *      Jérôme Glisse <glisse@freedesktop.org>
+ */
+#ifndef RADEON_CS_GEM_H
+#define RADEON_CS_GEM_H
+
+#include "radeon_cs.h"
+
+struct radeon_cs_manager *radeon_cs_manager_gem_ctor(int fd);
+void radeon_cs_manager_gem_dtor(struct radeon_cs_manager *csm);
+
+#endif
diff --git a/radeon/radeon_cs_int.h b/radeon/radeon_cs_int.h
new file mode 100644 (file)
index 0000000..6cee574
--- /dev/null
@@ -0,0 +1,67 @@
+
+#ifndef _RADEON_CS_INT_H_
+#define _RADEON_CS_INT_H_
+
+struct radeon_cs_space_check {
+    struct radeon_bo_int *bo;
+    uint32_t read_domains;
+    uint32_t write_domain;
+    uint32_t new_accounted;
+};
+
+struct radeon_cs_int {
+    /* keep first two in same place */
+    uint32_t                    *packets;    
+    unsigned                    cdw;
+    unsigned                    ndw;
+    unsigned                    section_ndw;
+    unsigned                    section_cdw;
+    /* private members */
+    struct radeon_cs_manager    *csm;
+    void                        *relocs;
+    unsigned                    crelocs;
+    unsigned                    relocs_total_size;
+    const char                  *section_file;
+    const char                  *section_func;
+    int                         section_line;
+    struct radeon_cs_space_check bos[MAX_SPACE_BOS];
+    int                         bo_count;
+    void                        (*space_flush_fn)(void *);
+    void                        *space_flush_data;
+    uint32_t                    id;
+};
+
+/* cs functions */
+struct radeon_cs_funcs {
+    struct radeon_cs_int *(*cs_create)(struct radeon_cs_manager *csm,
+                                   uint32_t ndw);
+    int (*cs_write_reloc)(struct radeon_cs_int *cs,
+                          struct radeon_bo *bo,
+                          uint32_t read_domain,
+                          uint32_t write_domain,
+                          uint32_t flags);
+    int (*cs_begin)(struct radeon_cs_int *cs,
+                    uint32_t ndw,
+                   const char *file,
+                   const char *func,
+                   int line);
+    int (*cs_end)(struct radeon_cs_int *cs,
+                 const char *file, const char *func,
+                 int line);
+
+
+    int (*cs_emit)(struct radeon_cs_int *cs);
+    int (*cs_destroy)(struct radeon_cs_int *cs);
+    int (*cs_erase)(struct radeon_cs_int *cs);
+    int (*cs_need_flush)(struct radeon_cs_int *cs);
+    void (*cs_print)(struct radeon_cs_int *cs, FILE *file);
+};
+
+struct radeon_cs_manager {
+    struct radeon_cs_funcs  *funcs;
+    int                     fd;
+    int32_t vram_limit, gart_limit;
+    int32_t vram_write_used, gart_write_used;
+    int32_t read_used;
+};
+#endif
diff --git a/radeon/radeon_cs_space.c b/radeon/radeon_cs_space.c
new file mode 100644 (file)
index 0000000..208e1bb
--- /dev/null
@@ -0,0 +1,242 @@
+/*
+ * Copyright Â© 2009 Red Hat Inc.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
+ * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+ * USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ */
+/*
+ */
+#include <assert.h>
+#include <errno.h>
+#include <stdlib.h>
+#include "radeon_cs.h"
+#include "radeon_bo_int.h"
+#include "radeon_cs_int.h"
+
+struct rad_sizes {
+    int32_t op_read;
+    int32_t op_gart_write;
+    int32_t op_vram_write;
+};
+
+static inline int radeon_cs_setup_bo(struct radeon_cs_space_check *sc, struct rad_sizes *sizes)
+{
+    uint32_t read_domains, write_domain;
+    struct radeon_bo_int *bo;
+
+    bo = sc->bo;
+    sc->new_accounted = 0;
+    read_domains = sc->read_domains;
+    write_domain = sc->write_domain;
+
+    /* legacy needs a static check */
+    if (radeon_bo_is_static((struct radeon_bo *)sc->bo)) {
+        bo->space_accounted = sc->new_accounted = (read_domains << 16) | write_domain;
+        return 0;
+    }
+
+    /* already accounted this bo */
+    if (write_domain && (write_domain == bo->space_accounted)) {
+        sc->new_accounted = bo->space_accounted;
+        return 0;
+    }
+    if (read_domains && ((read_domains << 16) == bo->space_accounted)) {
+        sc->new_accounted = bo->space_accounted;
+        return 0;
+    }
+
+    if (bo->space_accounted == 0) {
+        if (write_domain == RADEON_GEM_DOMAIN_VRAM)
+            sizes->op_vram_write += bo->size;
+        else if (write_domain == RADEON_GEM_DOMAIN_GTT)
+            sizes->op_gart_write += bo->size;
+        else
+            sizes->op_read += bo->size;
+        sc->new_accounted = (read_domains << 16) | write_domain;
+    } else {
+        uint16_t old_read, old_write;
+
+        old_read = bo->space_accounted >> 16;
+        old_write = bo->space_accounted & 0xffff;
+
+        if (write_domain && (old_read & write_domain)) {
+            sc->new_accounted = write_domain;
+            /* moving from read to a write domain */
+            if (write_domain == RADEON_GEM_DOMAIN_VRAM) {
+                sizes->op_read -= bo->size;
+                sizes->op_vram_write += bo->size;
+            } else if (write_domain == RADEON_GEM_DOMAIN_GTT) {
+                sizes->op_read -= bo->size;
+                sizes->op_gart_write += bo->size;
+            }
+        } else if (read_domains & old_write) {
+            sc->new_accounted = bo->space_accounted & 0xffff;
+        } else {
+            /* rewrite the domains */
+            if (write_domain != old_write)
+                fprintf(stderr,"WRITE DOMAIN RELOC FAILURE 0x%x %d %d\n", bo->handle, write_domain, old_write);
+            if (read_domains != old_read)
+               fprintf(stderr,"READ DOMAIN RELOC FAILURE 0x%x %d %d\n", bo->handle, read_domains, old_read);
+            return RADEON_CS_SPACE_FLUSH;
+        }
+    }
+    return 0;
+}
+
+static int radeon_cs_do_space_check(struct radeon_cs_int *cs, struct radeon_cs_space_check *new_tmp)
+{
+    struct radeon_cs_manager *csm = cs->csm;
+    int i;
+    struct radeon_bo_int *bo;
+    struct rad_sizes sizes;
+    int ret;
+
+    /* check the totals for this operation */
+
+    if (cs->bo_count == 0 && !new_tmp)
+        return 0;
+
+    memset(&sizes, 0, sizeof(struct rad_sizes));
+
+    /* prepare */
+    for (i = 0; i < cs->bo_count; i++) {
+        ret = radeon_cs_setup_bo(&cs->bos[i], &sizes);
+        if (ret)
+            return ret;
+    }
+
+    if (new_tmp) {
+        ret = radeon_cs_setup_bo(new_tmp, &sizes);
+        if (ret)
+            return ret;
+    }
+
+    if (sizes.op_read < 0)
+        sizes.op_read = 0;
+
+    /* check sizes - operation first */
+    if ((sizes.op_read + sizes.op_gart_write > csm->gart_limit) ||
+        (sizes.op_vram_write > csm->vram_limit)) {
+        return RADEON_CS_SPACE_OP_TO_BIG;
+    }
+
+    if (((csm->vram_write_used + sizes.op_vram_write) > csm->vram_limit) ||
+        ((csm->read_used + csm->gart_write_used + sizes.op_gart_write + sizes.op_read) > csm->gart_limit)) {
+        return RADEON_CS_SPACE_FLUSH;
+    }
+
+    csm->gart_write_used += sizes.op_gart_write;
+    csm->vram_write_used += sizes.op_vram_write;
+    csm->read_used += sizes.op_read;
+    /* commit */
+    for (i = 0; i < cs->bo_count; i++) {
+        bo = cs->bos[i].bo;
+        bo->space_accounted = cs->bos[i].new_accounted;
+    }
+    if (new_tmp)
+        new_tmp->bo->space_accounted = new_tmp->new_accounted;
+
+    return RADEON_CS_SPACE_OK;
+}
+
+void radeon_cs_space_add_persistent_bo(struct radeon_cs *cs, struct radeon_bo *bo, uint32_t read_domains, uint32_t write_domain)
+{
+    struct radeon_cs_int *csi = (struct radeon_cs_int *)cs;
+    struct radeon_bo_int *boi = (struct radeon_bo_int *)bo;
+    int i;
+    for (i = 0; i < csi->bo_count; i++) {
+        if (csi->bos[i].bo == boi &&
+            csi->bos[i].read_domains == read_domains &&
+            csi->bos[i].write_domain == write_domain)
+            return;
+    }
+    radeon_bo_ref(bo);
+    i = csi->bo_count;
+    csi->bos[i].bo = boi;
+    csi->bos[i].read_domains = read_domains;
+    csi->bos[i].write_domain = write_domain;
+    csi->bos[i].new_accounted = 0;
+    csi->bo_count++;
+
+    assert(csi->bo_count < MAX_SPACE_BOS);
+}
+
+static int radeon_cs_check_space_internal(struct radeon_cs_int *cs,
+                      struct radeon_cs_space_check *tmp_bo)
+{
+    int ret;
+    int flushed = 0;
+
+again:
+    ret = radeon_cs_do_space_check(cs, tmp_bo);
+    if (ret == RADEON_CS_SPACE_OP_TO_BIG)
+        return -1;
+    if (ret == RADEON_CS_SPACE_FLUSH) {
+        (*cs->space_flush_fn)(cs->space_flush_data);
+        if (flushed)
+            return -1;
+        flushed = 1;
+        goto again;
+    }
+    return 0;
+}
+
+int radeon_cs_space_check_with_bo(struct radeon_cs *cs,
+                  struct radeon_bo *bo,
+                  uint32_t read_domains, uint32_t write_domain)
+{
+    struct radeon_cs_int *csi = (struct radeon_cs_int *)cs;
+    struct radeon_bo_int *boi = (struct radeon_bo_int *)bo;
+    struct radeon_cs_space_check temp_bo;
+
+    int ret = 0;
+
+    if (bo) {
+        temp_bo.bo = boi;
+        temp_bo.read_domains = read_domains;
+        temp_bo.write_domain = write_domain;
+        temp_bo.new_accounted = 0;
+    }
+
+    ret = radeon_cs_check_space_internal(csi, bo ? &temp_bo : NULL);
+    return ret;
+}
+
+int radeon_cs_space_check(struct radeon_cs *cs)
+{
+    struct radeon_cs_int *csi = (struct radeon_cs_int *)cs;
+    return radeon_cs_check_space_internal(csi, NULL);
+}
+
+void radeon_cs_space_reset_bos(struct radeon_cs *cs)
+{
+    struct radeon_cs_int *csi = (struct radeon_cs_int *)cs;
+    int i;
+    for (i = 0; i < csi->bo_count; i++) {
+        radeon_bo_unref((struct radeon_bo *)csi->bos[i].bo);
+        csi->bos[i].bo = NULL;
+        csi->bos[i].read_domains = 0;
+        csi->bos[i].write_domain = 0;
+        csi->bos[i].new_accounted = 0;
+    }
+    csi->bo_count = 0;
+}
diff --git a/slp/Makefile.am b/slp/Makefile.am
new file mode 100644 (file)
index 0000000..132662b
--- /dev/null
@@ -0,0 +1,22 @@
+SUBDIRS = .
+
+AM_CFLAGS = \
+       $(WARN_CFLAGS) \
+       -I$(top_srcdir) \
+       -I$(top_srcdir)/slp \
+       $(PTHREADSTUBS_CFLAGS) \
+       -I$(top_srcdir)/include/drm
+
+libdrm_slp_la_LTLIBRARIES = libdrm_slp.la
+libdrm_slp_ladir = $(libdir)
+libdrm_slp_la_LDFLAGS = -version-number 1:0:0 -no-undefined
+libdrm_slp_la_LIBADD = ../libdrm.la @PTHREADSTUBS_LIBS@ @CLOCK_LIB@ -ldl
+
+libdrm_slp_la_SOURCES = \
+       drm_slp_bufmgr.c \
+       drm_slp_bufmgr.h
+
+libdrm_slpincludedir = ${includedir}/libdrm
+libdrm_slpinclude_HEADERS = drm_slp_bufmgr.h
+
+pkgconfig_DATA = libdrm_slp.pc
diff --git a/slp/drm_slp_bufmgr.c b/slp/drm_slp_bufmgr.c
new file mode 100755 (executable)
index 0000000..baa5731
--- /dev/null
@@ -0,0 +1,717 @@
+/**************************************************************************
+
+xserver-xorg-video-sec
+
+Copyright 2011 Samsung Electronics co., Ltd. All Rights Reserved.
+
+Contact: SooChan Lim <sc1.lim@samsung.com>, Sangjin Lee <lsj119@samsung.com>
+
+Permission is hereby granted, free of charge, to any person obtaining a
+copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sub license, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice (including the
+next paragraph) shall be included in all copies or substantial portions
+of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
+ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+**************************************************************************/
+
+#include "config.h"
+
+#include <unistd.h>
+#include <limits.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <dlfcn.h>
+#include <dirent.h>
+#include <string.h>
+#include <errno.h>
+
+#include "drm_slp_bufmgr.h"
+
+#define PREFIX_LIB    "libdrm_slp_"
+#define SUFFIX_LIB    ".so"
+#define DEFAULT_LIB  PREFIX_LIB"default"SUFFIX_LIB
+
+#define NUM_TRY_LOCK   10
+#define SEM_NAME               "pixmap_1"
+#define SEM_DEBUG 0
+
+typedef struct{
+       void* data;
+
+       drm_data_free free_func ;
+}drm_slp_user_data;
+
+static int
+_sem_wait_wrapper(sem_t* sem)
+{
+       int res = 0;
+       int num_try = NUM_TRY_LOCK;
+
+       do
+       {
+               res = sem_wait(sem);
+               num_try--;
+       } while((res == -1) && (errno == EINTR) && (num_try >= 0));
+
+       if(res == -1)
+       {
+               fprintf(stderr,
+                               "[libdrm] error %s:%d(sem:%p, num_try:%d) PID:%04d\n",
+                               __FUNCTION__,
+                               __LINE__,
+                               sem,
+                               num_try,
+                               getpid());
+               return 0;
+       }
+#if SEM_DEBUG
+       else
+       {
+               fprintf(stderr,
+                               "[libdrm]   LOCK >> %s:%d(sem:%p, num_try:%d) PID:%04d\n",
+                               __FUNCTION__,
+                               __LINE__,
+                               sem,
+                               num_try,
+                               getpid());
+       }
+#endif
+
+       return 1;
+}
+
+static int
+_sem_post_wrapper(sem_t* sem)
+{
+       int res = 0;
+       int num_try = NUM_TRY_LOCK;
+
+       do
+       {
+               res = sem_post(sem);
+               num_try--;
+
+       } while((res == -1) && (errno == EINTR) && (num_try >= 0));
+
+       if(res == -1)
+       {
+               fprintf(stderr,
+                               "[libdrm] error %s:%d(sem:%p, num_try:%d) PID:%04d\n",
+                               __FUNCTION__,
+                               __LINE__,
+                               sem,
+                               num_try,
+                               getpid());
+               return 0;
+       }
+#if SEM_DEBUG
+       else
+       {
+               fprintf(stderr,
+                               "[libdrm] UNLOCK << %s:%d(sem:%p, num_try:%d) PID:%04d\n",
+                               __FUNCTION__,
+                               __LINE__,
+                               sem,
+                               num_try,
+                               getpid());
+       }
+#endif
+
+       return 1;
+}
+
+static int
+_sem_open(drm_slp_bufmgr bufmgr)
+{
+       bufmgr->semObj.handle = sem_open(SEM_NAME, O_CREAT, 0777, 1);
+       if(bufmgr->semObj.handle == SEM_FAILED)
+       {
+               fprintf(stderr,
+                               "[libdrm] error %s:%d(name:%s) PID:%04d\n",
+                               __FUNCTION__,
+                               __LINE__,
+                               SEM_NAME,
+                               getpid());
+               bufmgr->semObj.handle = NULL;
+               return 0;
+       }
+#if SEM_DEBUG
+       else
+       {
+               fprintf(stderr,
+                               "[libdrm] OPEN %s:%d(sem:%p) PID:%04d\n",
+                               __FUNCTION__,
+                               __LINE__,
+                               bufmgr->semObj.handle,
+                               getpid());
+       }
+#endif
+
+       bufmgr->semObj.status = STATUS_UNLOCK;
+
+       return 1;
+}
+
+static int
+_sem_close(drm_slp_bufmgr bufmgr)
+{
+       _sem_wait_wrapper(bufmgr->semObj.handle);
+       sem_unlink(SEM_NAME);
+       return 1;
+}
+
+static int
+_sem_lock(drm_slp_bufmgr bufmgr)
+{
+       if(bufmgr->semObj.status != STATUS_UNLOCK) return 0;
+
+       if(!_sem_wait_wrapper(bufmgr->semObj.handle)) return 0;
+       bufmgr->semObj.status = STATUS_LOCK;
+       return 1;
+}
+
+static int
+_sem_unlock(drm_slp_bufmgr bufmgr)
+{
+       if(bufmgr->semObj.status != STATUS_LOCK) return 0;
+
+       _sem_post_wrapper(bufmgr->semObj.handle);
+       bufmgr->semObj.status = STATUS_UNLOCK;
+       return 1;
+}
+
+static drm_slp_bufmgr
+_load_bufmgr(int fd, const char *file, void *arg)
+{
+       char path[PATH_MAX] = {0,};
+       drm_slp_bufmgr bufmgr = NULL;
+       int (*bufmgr_init)(drm_slp_bufmgr bufmgr, int fd, void *arg);
+       void *module;
+
+       snprintf(path, sizeof(path), BUFMGR_DIR "/%s", file);
+
+       module = dlopen(path, RTLD_LAZY);
+       if (!module) {
+               fprintf(stderr,
+                       "[libdrm] failed to load module: %s(%s)\n",
+                       dlerror(), file);
+               return NULL;
+       }
+
+       bufmgr_init = dlsym(module, "init_slp_bufmgr");
+       if (!bufmgr_init) {
+               fprintf(stderr,
+                       "[libdrm] failed to lookup init function: %s(%s)\n",
+                       dlerror(), file);
+               return NULL;
+       }
+
+       bufmgr = calloc(sizeof(struct _drm_slp_bufmgr), 1);
+       if(!bufmgr)
+       {
+               return NULL;
+       }
+
+       if(!bufmgr_init(bufmgr, fd, arg))
+       {
+               fprintf(stderr,"[libdrm] Fail to init module(%s)\n", file);
+               free(bufmgr);
+               bufmgr = NULL;
+               return NULL;
+       }
+
+       fprintf(stderr,"[libdrm] Success to load module(%s)\n", file);
+
+       return bufmgr;
+}
+
+drm_slp_bufmgr drm_slp_bufmgr_init(int fd, void *arg)
+{
+       drm_slp_bufmgr bufmgr = NULL;
+       const char *p = NULL;
+
+       if (fd < 0)
+               return NULL;
+
+       p = getenv ("SLP_BUFMGR_MODULE");
+       if (p) {
+               char file[PATH_MAX] = {0,};
+               snprintf(file, sizeof(file), PREFIX_LIB"%s"SUFFIX_LIB, p);
+               bufmgr = _load_bufmgr (fd, file, arg);
+       }
+
+       if (!bufmgr)
+               bufmgr = _load_bufmgr (fd, DEFAULT_LIB, arg);
+
+       if (!bufmgr) {
+               struct dirent **namelist;
+               int found = 0;
+               int n;
+
+               n = scandir(BUFMGR_DIR, &namelist, 0, alphasort);
+               if (n < 0)
+                       fprintf(stderr,"[libdrm] no files : %s\n", BUFMGR_DIR);
+               else {
+                       while(n--) {
+                               if (!found && strstr (namelist[n]->d_name, PREFIX_LIB)) {
+                                       char *p = strstr (namelist[n]->d_name, SUFFIX_LIB);
+                                       if (!strcmp (p, SUFFIX_LIB))
+                                       {
+                                               bufmgr = _load_bufmgr (fd, namelist[n]->d_name, arg);
+                                               if (bufmgr)
+                                                       found = 1;
+                                       }
+                               }
+                               free(namelist[n]);
+                       }
+                       free(namelist);
+               }
+       }
+
+       if (!bufmgr)
+       {
+               fprintf(stderr,"[libdrm] backend is NULL.\n");
+               return NULL;
+       }
+
+       if (pthread_mutex_init(&bufmgr->lock, NULL) != 0) {
+               bufmgr->bufmgr_destroy(bufmgr);
+               free(bufmgr);
+               return NULL;
+       }
+
+       return bufmgr;
+}
+
+void drm_slp_bufmgr_destroy(drm_slp_bufmgr bufmgr)
+{
+       if(!bufmgr)
+               return;
+
+       bufmgr->bufmgr_destroy(bufmgr);
+
+       if(bufmgr->semObj.isOpened)
+       {
+               _sem_close(bufmgr);
+       }
+
+       pthread_mutex_destroy(&bufmgr->lock);
+       free(bufmgr);
+}
+
+int drm_slp_bufmgr_lock(drm_slp_bufmgr bufmgr)
+{
+       if(!bufmgr)
+               return 0;
+
+       pthread_mutex_lock(&bufmgr->lock);
+
+       if(!bufmgr->semObj.isOpened)
+       {
+               if(_sem_open(bufmgr) != 1)
+               {
+                       pthread_mutex_unlock(&bufmgr->lock);
+                       return 0;
+               }
+               bufmgr->semObj.isOpened = 1;
+       }
+
+       if(_sem_lock(bufmgr) != 1)
+       {
+               pthread_mutex_unlock(&bufmgr->lock);
+               return 0;
+       }
+
+       pthread_mutex_unlock(&bufmgr->lock);
+
+       return 1;
+}
+
+int drm_slp_bufmgr_unlock(drm_slp_bufmgr bufmgr)
+{
+       if(!bufmgr)
+               return 0;
+
+       pthread_mutex_lock(&bufmgr->lock);
+
+       if(_sem_unlock(bufmgr) != 1)
+       {
+               pthread_mutex_unlock(&bufmgr->lock);
+               return 0;
+       }
+
+       pthread_mutex_unlock(&bufmgr->lock);
+
+       return 1;
+}
+
+int drm_slp_bufmgr_cache_flush(drm_slp_bufmgr bufmgr, drm_slp_bo bo, int flags)
+{
+       int ret;
+
+       if (!bufmgr && !bo)
+               return 0;
+
+       if (bo)
+       {
+               if(!bo->bufmgr)
+                       return 0;
+
+               pthread_mutex_lock(&bo->bufmgr->lock);
+               ret = bo->bufmgr->bufmgr_cache_flush(bufmgr, bo, flags);
+               pthread_mutex_unlock(&bo->bufmgr->lock);
+       }
+       else
+       {
+               pthread_mutex_lock(&bufmgr->lock);
+               ret = bufmgr->bufmgr_cache_flush(bufmgr, NULL, flags);
+               pthread_mutex_unlock(&bufmgr->lock);
+       }
+
+       return ret;
+}
+
+int drm_slp_bo_size(drm_slp_bo bo)
+{
+       int size;
+       drm_slp_bufmgr bufmgr;
+
+       if(!bo || !bo->bufmgr)
+               return 0;
+       bufmgr = bo->bufmgr;
+
+       pthread_mutex_lock(&bufmgr->lock);
+       size = bo->bufmgr->bo_size(bo);
+       pthread_mutex_unlock(&bufmgr->lock);
+
+       return size;
+}
+
+drm_slp_bo drm_slp_bo_ref(drm_slp_bo bo)
+{
+       drm_slp_bufmgr bufmgr;
+
+       if(!bo || !bo->bufmgr)
+               return NULL;
+       bufmgr = bo->bufmgr;
+
+       pthread_mutex_lock(&bufmgr->lock);
+
+       bo->ref_cnt++;
+
+       pthread_mutex_unlock(&bufmgr->lock);
+
+       return bo;
+}
+
+void drm_slp_bo_unref(drm_slp_bo bo)
+{
+       drm_slp_bufmgr bufmgr;
+
+       if(!bo || !bo->bufmgr)
+               return;
+       bufmgr = bo->bufmgr;
+
+       pthread_mutex_lock(&bufmgr->lock);
+
+       if(0 >= bo->ref_cnt)
+               return;
+       bo->ref_cnt--;
+       if(bo->ref_cnt == 0)
+       {
+               bufmgr->bo_free(bo);
+
+               if(bo->user_data)
+               {
+                       void* rd;
+                       drm_slp_user_data* old_data;
+                       unsigned long key;
+
+                       while(1==drmSLFirst(bo->user_data, &key, &rd))
+                       {
+                               old_data = (drm_slp_user_data*)rd;
+
+                               if(old_data->free_func)
+                               {
+                                       old_data->free_func(old_data->data);
+                                       old_data->data = NULL;
+                                       free(old_data);
+                               }
+                               drmSLDelete(bo->user_data, key);
+                       }
+
+                       drmSLDestroy(bo->user_data);
+                       bo->user_data = (void*)0;
+               }
+
+               free(bo);
+       }
+
+       pthread_mutex_unlock(&bufmgr->lock);
+}
+
+drm_slp_bo drm_slp_bo_alloc(drm_slp_bufmgr bufmgr, const char * name, int size)
+{
+       drm_slp_bo bo=NULL;
+
+       if(!bufmgr || size <= 0)
+               return NULL;
+
+       bo = calloc(sizeof(struct _drm_slp_bo), 1);
+       if(!bo)
+               return NULL;
+
+       bo->bufmgr = bufmgr;
+
+       pthread_mutex_lock(&bufmgr->lock);
+       if(!bufmgr->bo_alloc(bo, name, size))
+       {
+               free(bo);
+               pthread_mutex_unlock(&bufmgr->lock);
+               return NULL;
+       }
+       bo->ref_cnt = 1;
+       pthread_mutex_unlock(&bufmgr->lock);
+
+       return bo;
+}
+
+drm_slp_bo drm_slp_bo_attach(drm_slp_bufmgr bufmgr,
+                             const char*    name,
+                             void*          ptr,
+                             int            size,
+                             void*          native_handle)
+{
+       drm_slp_bo bo;
+
+       if(!bufmgr)
+               return NULL;
+
+       bo = calloc(sizeof(struct _drm_slp_bo), 1);
+       if(!bo)
+               return NULL;
+
+       bo->bufmgr = bufmgr;
+
+       pthread_mutex_lock(&bufmgr->lock);
+       if(!bufmgr->bo_attach(bo, name, ptr, size, native_handle))
+       {
+               free(bo);
+               pthread_mutex_unlock(&bufmgr->lock);
+               return NULL;
+       }
+       bo->ref_cnt = 1;
+       pthread_mutex_unlock(&bufmgr->lock);
+
+       return bo;
+}
+
+drm_slp_bo drm_slp_bo_import(drm_slp_bufmgr bufmgr, unsigned int key)
+{
+       drm_slp_bo bo;
+
+       if(!bufmgr)
+               return NULL;
+
+       bo = calloc(sizeof(struct _drm_slp_bo), 1);
+       if(!bo)
+               return NULL;
+
+       bo->bufmgr = bufmgr;
+
+       pthread_mutex_lock(&bufmgr->lock);
+       if(!bufmgr->bo_import(bo, key))
+       {
+               free(bo);
+               pthread_mutex_unlock(&bufmgr->lock);
+               return NULL;
+       }
+       bo->ref_cnt = 1;
+       pthread_mutex_unlock(&bufmgr->lock);
+
+       return bo;
+}
+
+unsigned int drm_slp_bo_export(drm_slp_bo bo)
+{
+       int ret;
+
+       if(!bo || !bo->bufmgr)
+               return 0;
+
+       pthread_mutex_lock(&bo->bufmgr->lock);
+       ret = bo->bufmgr->bo_export(bo);
+       pthread_mutex_unlock(&bo->bufmgr->lock);
+
+       return ret;
+}
+
+void * drm_slp_bo_map(drm_slp_bo bo, int device, int opt)
+{
+       void* ret;
+
+       if(!bo || !bo->bufmgr)
+               return 0;
+
+       pthread_mutex_lock(&bo->bufmgr->lock);
+       ret = bo->bufmgr->bo_map(bo, device, opt);
+       pthread_mutex_unlock(&bo->bufmgr->lock);
+
+       return ret;
+}
+
+int drm_slp_bo_unmap(drm_slp_bo bo, int device)
+{
+       int ret;
+
+       if(!bo || !bo->bufmgr)
+               return 0;
+
+       pthread_mutex_lock(&bo->bufmgr->lock);
+       ret = bo->bufmgr->bo_unmap(bo, device);
+       pthread_mutex_unlock(&bo->bufmgr->lock);
+
+       return 0;
+}
+
+int drm_slp_bo_swap(drm_slp_bo bo1, drm_slp_bo bo2)
+{
+       void* temp;
+
+       if(!bo1 || !bo1->bufmgr)
+               return 0;
+
+       if(!bo2 || !bo2->bufmgr)
+               return 0;
+
+       if(bo1->bufmgr != bo2->bufmgr)
+               return 0;
+
+       if(bo1->bufmgr->bo_size(bo1) != bo2->bufmgr->bo_size(bo2))
+               return 0;
+
+       pthread_mutex_lock(&bo1->bufmgr->lock);
+       temp = bo1->private;
+       bo1->private = bo2->private;
+       bo2->private = temp;
+       pthread_mutex_unlock(&bo1->bufmgr->lock);
+
+       return 1;
+}
+
+int drm_slp_bo_add_user_data(drm_slp_bo bo, unsigned long key, drm_data_free data_free_func)
+{
+       int ret;
+       drm_slp_user_data* data;
+
+       if(!bo)
+               return 0;
+
+       if(!bo->user_data)
+               bo->user_data = drmSLCreate();
+
+       data = calloc(1, sizeof(drm_slp_user_data));
+       if(!data)
+               return 0;
+
+       data->free_func = data_free_func;
+       data->data = (void*)0;
+
+       ret = drmSLInsert(bo->user_data, key, data);
+       if(ret == 1) /* Already in list */
+       {
+               free(data);
+               return 0;
+       }
+
+       return 1;
+}
+
+int drm_slp_bo_set_user_data(drm_slp_bo bo, unsigned long key, void* data)
+{
+       void *rd;
+       drm_slp_user_data* old_data;
+
+       if(!bo || !bo->user_data)
+               return 0;
+
+       if(drmSLLookup(bo->user_data, key, &rd))
+               return 0;
+
+       old_data = (drm_slp_user_data*)rd;
+       if (!old_data)
+               return 0;
+
+       if(old_data->free_func)
+       {
+               old_data->free_func(old_data->data);
+               old_data->data = NULL;
+       }
+
+       old_data->data = data;
+
+       return 1;
+}
+
+int drm_slp_bo_get_user_data(drm_slp_bo bo, unsigned long key, void** data)
+{
+       void *rd;
+       drm_slp_user_data* old_data;
+
+       if(!bo || !bo->user_data)
+               return 0;
+
+       if(drmSLLookup(bo->user_data, key, &rd))
+               return 0;
+
+       old_data = (drm_slp_user_data*)rd;
+       if (!old_data)
+               return 0;
+
+       if (data)
+               *data = old_data->data;
+
+       return 1;
+}
+
+int drm_slp_bo_delete_user_data(drm_slp_bo bo, unsigned long key)
+{
+       void *rd;
+       drm_slp_user_data* old_data=(void*)0;
+
+       if(!bo || !bo->user_data)
+               return 0;
+
+       if(drmSLLookup(bo->user_data, key, &rd))
+               return 0;
+
+       old_data = (drm_slp_user_data*)rd;
+       if (!old_data)
+               return 0;
+
+       if(old_data->free_func)
+       {
+               old_data->free_func(old_data->data);
+               free(old_data);
+       }
+       drmSLDelete(bo->user_data, key);
+
+       return 1;
+}
diff --git a/slp/drm_slp_bufmgr.h b/slp/drm_slp_bufmgr.h
new file mode 100755 (executable)
index 0000000..23a4c4b
--- /dev/null
@@ -0,0 +1,156 @@
+/**************************************************************************
+
+xserver-xorg-video-sec
+
+Copyright 2011 Samsung Electronics co., Ltd. All Rights Reserved.
+
+Contact: SooChan Lim <sc1.lim@samsung.com>, Sangjin Lee <lsj119@samsung.com>
+
+Permission is hereby granted, free of charge, to any person obtaining a
+copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sub license, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice (including the
+next paragraph) shall be included in all copies or substantial portions
+of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
+ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+**************************************************************************/
+
+#ifndef _DRM_SLP_BUFMGR_H_
+#define _DRM_SLP_BUFMGR_H_
+
+#include <semaphore.h>
+#include <pthread.h>
+#include <xf86drm.h>
+
+typedef struct _drm_slp_bo * drm_slp_bo;
+typedef struct _drm_slp_bufmgr * drm_slp_bufmgr;
+
+struct _drm_slp_bo
+{
+       drm_slp_bufmgr bufmgr;
+       int ref_cnt;            /*atomic count*/
+       void *user_data;
+
+       /* private data */
+       void *private;
+};
+
+typedef enum
+{
+       STATUS_UNLOCK,
+       STATUS_READY_TO_LOCK,
+       STATUS_LOCK,
+} lock_status;
+
+struct _drm_slp_bufmgr
+{
+       pthread_mutex_t lock;
+       struct {
+               int isOpened;
+               lock_status status;
+               sem_t* handle;
+       } semObj;
+
+       void         (*bufmgr_destroy)(drm_slp_bufmgr bufmgr);
+       int          (*bufmgr_cache_flush)(drm_slp_bufmgr bufmgr, drm_slp_bo bo, int flags);
+
+       int          (*bo_size)(drm_slp_bo bo);
+
+       void         (*bo_free)(drm_slp_bo bo);
+       int          (*bo_alloc)(drm_slp_bo bo,
+                                const char*    name,
+                                int            size);
+
+       int          (*bo_attach)(drm_slp_bo bo,
+                                 const char*    name,
+                                 void*          ptr,
+                                 int            size,
+                                 void*          native_handle);
+
+       int          (*bo_import)(drm_slp_bo bo, unsigned int key);
+       unsigned int (*bo_export)(drm_slp_bo bo);
+
+       void*        (*bo_map)(drm_slp_bo bo, int device, int opt);
+       int          (*bo_unmap)(drm_slp_bo bo, int device);
+
+
+       /* Padding for future extension */
+       void (*reserved1) (void);
+       void (*reserved2) (void);
+       void (*reserved3) (void);
+       void (*reserved4) (void);
+       void (*reserved5) (void);
+       void (*reserved6) (void);
+
+       /* private data */
+       void *private;
+};
+
+/* DRM_SLP_DEVICE_TYPE */
+#define DRM_SLP_DEVICE_CPU     1
+#define DRM_SLP_DEVICE_2D      2
+#define DRM_SLP_DEVICE_3D      3
+
+/* DRM_SLP_OPTION */
+#define DRM_SLP_OPTION_INDEX0  0
+#define DRM_SLP_OPTION_INDEX1  1
+#define DRM_SLP_OPTION_INDEX2  2
+#define DRM_SLP_OPTION_READ    (1 << 24)
+#define DRM_SLP_OPTION_WRITE   (1 << 25)
+
+#define DRM_SLP_CACHE_INV   0x01
+#define DRM_SLP_CACHE_CLN   0x02
+#define DRM_SLP_CACHE_ALL   0x10
+#define DRM_SLP_CACHE_FLUSH     (DRM_SLP_CACHE_INV|DRM_SLP_CACHE_CLN)
+#define DRM_SLP_CACHE_FLUSH_ALL (DRM_SLP_CACHE_FLUSH|DRM_SLP_CACHE_ALL)
+
+drm_slp_bufmgr drm_slp_bufmgr_init(int fd, void * arg);
+
+void    drm_slp_bufmgr_destroy(drm_slp_bufmgr bufmgr);
+int     drm_slp_bufmgr_lock(drm_slp_bufmgr bufmgr);
+int     drm_slp_bufmgr_unlock(drm_slp_bufmgr bufmgr);
+int     drm_slp_bufmgr_cache_flush(drm_slp_bufmgr bufmgr, drm_slp_bo bo, int flags);
+
+int     drm_slp_bo_size (drm_slp_bo bo);
+
+drm_slp_bo  drm_slp_bo_ref(drm_slp_bo bo);
+void        drm_slp_bo_unref(drm_slp_bo bo);
+
+drm_slp_bo drm_slp_bo_alloc(drm_slp_bufmgr bufmgr,
+                            const char*    name,
+                            int            size);
+drm_slp_bo drm_slp_bo_attach(drm_slp_bufmgr bufmgr,
+                             const char*    name,
+                             void*          ptr,
+                             int            size,
+                             void*          native_handle);
+
+drm_slp_bo   drm_slp_bo_import(drm_slp_bufmgr bufmgr, unsigned int key);
+unsigned int drm_slp_bo_export(drm_slp_bo bo);
+
+void*   drm_slp_bo_map(drm_slp_bo bo, int device, int opt);
+int     drm_slp_bo_unmap(drm_slp_bo bo, int device);
+
+int drm_slp_bo_swap(drm_slp_bo bo1, drm_slp_bo bo2);
+
+
+typedef void   (*drm_data_free)(void *);
+int drm_slp_bo_add_user_data(drm_slp_bo bo, unsigned long key, drm_data_free data_free_func);
+int drm_slp_bo_delete_user_data(drm_slp_bo bo, unsigned long key);
+int drm_slp_bo_set_user_data(drm_slp_bo bo, unsigned long key, void* data);
+int drm_slp_bo_get_user_data(drm_slp_bo bo, unsigned long key, void** data);
+
+#endif /* _DRM_SLP_BUFMGR_H_ */
diff --git a/slp/libdrm_slp.pc.in b/slp/libdrm_slp.pc.in
new file mode 100644 (file)
index 0000000..220d38b
--- /dev/null
@@ -0,0 +1,11 @@
+prefix=@prefix@
+exec_prefix=@exec_prefix@
+libdir=@libdir@
+includedir=@includedir@
+
+Name: libdrm
+Description: Userspace interface to kernel DRM services
+Version: @PACKAGE_VERSION@
+Requires: libdrm
+Libs: -L${libdir} -ldrm_slp
+Cflags: -I${includedir} -I${includedir}/libdrm
diff --git a/tests/Makefile.am b/tests/Makefile.am
new file mode 100644 (file)
index 0000000..832afc4
--- /dev/null
@@ -0,0 +1,62 @@
+NULL:=#
+
+AM_CPPFLAGS = \
+       -I $(top_srcdir)/include/drm \
+       -I $(top_srcdir)
+
+LDADD = $(top_builddir)/libdrm.la
+
+check_PROGRAMS = \
+       dristat \
+       drmstat
+
+SUBDIRS = modeprint
+
+if HAVE_LIBKMS
+SUBDIRS += kmstest modetest planetest
+endif
+
+SUBDIRS += gemtest $(NULL)
+
+if HAVE_LIBUDEV
+
+check_LTLIBRARIES = libdrmtest.la
+
+libdrmtest_la_SOURCES = \
+       drmtest.c \
+       drmtest.h
+
+libdrmtest_la_LIBADD = \
+       $(top_builddir)/libdrm.la \
+       $(LIBUDEV_LIBS)
+
+LDADD += libdrmtest.la
+
+XFAIL_TESTS =                                  \
+       auth                                    \
+       lock
+
+TESTS =                                                \
+       openclose                               \
+       getversion                              \
+       getclient                               \
+       getstats                                \
+       setversion                              \
+       updatedraw                              \
+       name_from_fd                            \
+       $(NULL)
+
+SUBDIRS += vbltest $(NULL)
+
+if HAVE_INTEL
+TESTS +=                                       \
+       gem_basic                               \
+       gem_flink                               \
+       gem_readwrite                           \
+       gem_mmap                                \
+       $(NULL)
+endif
+
+check_PROGRAMS += $(TESTS)
+
+endif
diff --git a/tests/auth.c b/tests/auth.c
new file mode 100644 (file)
index 0000000..9b6fca9
--- /dev/null
@@ -0,0 +1,137 @@
+/*
+ * Copyright Â© 2007 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Authors:
+ *    Eric Anholt <eric@anholt.net>
+ *
+ */
+
+#include <limits.h>
+#include "drmtest.h"
+
+enum auth_event {
+       SERVER_READY,
+       CLIENT_MAGIC,
+       CLIENT_DONE,
+};
+
+int commfd[2];
+
+static void wait_event(int pipe, enum auth_event expected_event)
+{
+       int ret;
+       enum auth_event event;
+       unsigned char in;
+
+       ret = read(commfd[pipe], &in, 1);
+       if (ret == -1)
+               err(1, "read error");
+       event = in;
+
+       if (event != expected_event)
+               errx(1, "unexpected event: %d\n", event);
+}
+
+static void
+send_event(int pipe, enum auth_event send_event)
+{
+       int ret;
+       unsigned char event;
+
+       event = send_event;
+       ret = write(commfd[pipe], &event, 1);
+       if (ret == -1)
+               err(1, "failed to send event %d", event);
+}
+
+static void client()
+{
+       struct drm_auth auth;
+       int drmfd, ret;
+
+       /* XXX: Should make sure we open the same DRM as the master */
+       wait_event(0, SERVER_READY);
+
+       drmfd = drm_open_any();
+
+       /* Get a client magic number and pass it to the master for auth. */
+       auth.magic = 0; /* Quiet valgrind */
+       ret = ioctl(drmfd, DRM_IOCTL_GET_MAGIC, &auth);
+       if (ret == -1)
+               err(1, "Couldn't get client magic");
+       send_event(0, CLIENT_MAGIC);
+       ret = write(commfd[0], &auth.magic, sizeof(auth.magic));
+       if (ret == -1)
+               err(1, "Couldn't write auth data");
+
+       /* Signal that the client is completely done. */
+       send_event(0, CLIENT_DONE);
+}
+
+static void server()
+{
+       int drmfd, ret;
+       struct drm_auth auth;
+
+       drmfd = drm_open_any_master();
+
+       auth.magic = 0xd0d0d0d0;
+       ret = ioctl(drmfd, DRM_IOCTL_AUTH_MAGIC, &auth);
+       if (ret != -1 || errno != EINVAL)
+               errx(1, "Authenticating bad magic succeeded\n");
+
+       send_event(1, SERVER_READY);
+
+       wait_event(1, CLIENT_MAGIC);
+       ret = read(commfd[1], &auth.magic, sizeof(auth.magic));
+       if (ret == -1)
+               err(1, "Failure to read client magic");
+
+       ret = ioctl(drmfd, DRM_IOCTL_AUTH_MAGIC, &auth);
+       if (ret == -1)
+               err(1, "Failure to authenticate client magic\n");
+
+       wait_event(1, CLIENT_DONE);
+}
+
+/**
+ * Checks DRM authentication mechanisms.
+ */
+int main(int argc, char **argv)
+{
+       int ret;
+
+       ret = pipe(commfd);
+       if (ret == -1)
+               err(1, "Couldn't create pipe");
+
+       ret = fork();
+       if (ret == -1)
+               err(1, "failure to fork client");
+       if (ret == 0)
+               client();
+       else
+               server();
+
+       return 0;
+}
+
diff --git a/tests/dristat.c b/tests/dristat.c
new file mode 100644 (file)
index 0000000..900a3e6
--- /dev/null
@@ -0,0 +1,280 @@
+/* dristat.c -- 
+ * Created: Mon Jan 15 05:05:07 2001 by faith@acm.org
+ *
+ * Copyright 2000 VA Linux Systems, Inc., Fremont, California.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ * 
+ * Authors: Rickard E. (Rik) Faith <faith@valinux.com>
+ * 
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include "xf86drm.h"
+#include "xf86drmRandom.c"
+#include "xf86drmHash.c"
+#include "xf86drm.c"
+
+#define DRM_VERSION 0x00000001
+#define DRM_MEMORY  0x00000002
+#define DRM_CLIENTS 0x00000004
+#define DRM_STATS   0x00000008
+#define DRM_BUSID   0x00000010
+
+static void getversion(int fd)
+{
+    drmVersionPtr version;
+    
+    version = drmGetVersion(fd);
+    if (version) {
+       printf("  Version information:\n");
+       printf("    Name: %s\n", version->name ? version->name : "?");
+       printf("    Version: %d.%d.%d\n",
+              version->version_major,
+              version->version_minor,
+              version->version_patchlevel);
+       printf("    Date: %s\n", version->date ? version->date : "?");
+       printf("    Desc: %s\n", version->desc ? version->desc : "?");
+       drmFreeVersion(version);
+    } else {
+       printf("  No version information available\n");
+    }
+}
+
+static void getbusid(int fd)
+{
+    const char *busid = drmGetBusid(fd);
+    
+    printf("  Busid: %s\n", *busid ? busid : "(not set)");
+    drmFreeBusid(busid);
+}
+
+
+static void getvm(int fd)
+{
+    int             i;
+    const char      *typename;
+    char            flagname[33];
+    drm_handle_t    offset;
+    drmSize         size;
+    drmMapType      type;
+    drmMapFlags     flags;
+    drm_handle_t    handle;
+    int             mtrr;
+
+    printf("  VM map information:\n");
+    printf("  flags: (R)estricted (r)ead/(w)rite (l)ocked (k)ernel (W)rite-combine (L)ock:\n");
+    printf("    slot     offset       size type flags    address mtrr\n");
+
+    for (i = 0;
+        !drmGetMap(fd, i, &offset, &size, &type, &flags, &handle, &mtrr);
+        i++) {
+       
+       switch (type) {
+       case DRM_FRAME_BUFFER:   typename = "FB";  break;
+       case DRM_REGISTERS:      typename = "REG"; break;
+       case DRM_SHM:            typename = "SHM"; break;
+       case DRM_AGP:            typename = "AGP"; break;
+       case DRM_SCATTER_GATHER: typename = "SG";  break;
+       default:                 typename = "???"; break;
+       }
+
+       flagname[0] = (flags & DRM_RESTRICTED)      ? 'R' : ' ';
+       flagname[1] = (flags & DRM_READ_ONLY)       ? 'r' : 'w';
+       flagname[2] = (flags & DRM_LOCKED)          ? 'l' : ' ';
+       flagname[3] = (flags & DRM_KERNEL)          ? 'k' : ' ';
+       flagname[4] = (flags & DRM_WRITE_COMBINING) ? 'W' : ' ';
+       flagname[5] = (flags & DRM_CONTAINS_LOCK)   ? 'L' : ' ';
+       flagname[6] = '\0';
+       
+       printf("    %4d 0x%08lx 0x%08lx %3.3s %6.6s 0x%08lx ",
+              i, (unsigned long)offset, (unsigned long)size,
+              typename, flagname, (unsigned long)handle);
+       if (mtrr < 0) printf("none\n");
+       else          printf("%4d\n", mtrr);
+    }
+}
+
+static void getclients(int fd)
+{
+    int           i;
+    int           auth;
+    int           pid;
+    int           uid;
+    unsigned long magic;
+    unsigned long iocs;
+    char          buf[64];
+    char          cmd[40];
+    int           procfd;
+    
+    printf("  DRI client information:\n");
+    printf("    a   pid   uid      magic     ioctls   prog\n");
+
+    for (i = 0; !drmGetClient(fd, i, &auth, &pid, &uid, &magic, &iocs); i++) {
+       sprintf(buf, "/proc/%d/cmdline", pid);
+       memset(cmd, 0, sizeof(cmd));
+       if ((procfd = open(buf, O_RDONLY, 0)) >= 0) {
+           read(procfd, cmd, sizeof(cmd)-1);
+           close(procfd);
+       }
+       if (*cmd) {
+           char *pt;
+
+           for (pt = cmd; *pt; pt++) if (!isprint(*pt)) *pt = ' ';
+           printf("    %c %5d %5d %10lu %10lu   %s\n",
+                  auth ? 'y' : 'n', pid, uid, magic, iocs, cmd);
+       } else {
+           printf("    %c %5d %5d %10lu %10lu\n",
+                  auth ? 'y' : 'n', pid, uid, magic, iocs);
+       }
+    }
+}
+
+static void printhuman(unsigned long value, const char *name, int mult)
+{
+    const char *p;
+    double     f;
+                               /* Print width 5 number in width 6 space */
+    if (value < 100000) {
+       printf(" %5lu", value);
+       return;
+    }
+
+    p = name;
+    f = (double)value / (double)mult;
+    if (f < 10.0) {
+       printf(" %4.2f%c", f, *p);
+       return;
+    }
+
+    p++;
+    f = (double)value / (double)mult;
+    if (f < 10.0) {
+       printf(" %4.2f%c", f, *p);
+       return;
+    }
+    
+    p++;
+    f = (double)value / (double)mult;
+    if (f < 10.0) {
+       printf(" %4.2f%c", f, *p);
+       return;
+    }
+}
+
+static void getstats(int fd, int i)
+{
+    drmStatsT prev, curr;
+    int       j;
+    double    rate;
+    
+    printf("  System statistics:\n");
+
+    if (drmGetStats(fd, &prev)) return;
+    if (!i) {
+       for (j = 0; j < prev.count; j++) {
+           printf("    ");
+           printf(prev.data[j].long_format, prev.data[j].long_name);
+           if (prev.data[j].isvalue) printf(" 0x%08lx\n", prev.data[j].value);
+           else                      printf(" %10lu\n", prev.data[j].value);
+       }
+       return;
+    }
+
+    printf("    ");
+    for (j = 0; j < prev.count; j++)
+       if (!prev.data[j].verbose) {
+           printf(" ");
+           printf(prev.data[j].rate_format, prev.data[j].rate_name);
+       }
+    printf("\n");
+    
+    for (;;) {
+       sleep(i);
+       if (drmGetStats(fd, &curr)) return;
+       printf("    ");
+       for (j = 0; j < curr.count; j++) {
+           if (curr.data[j].verbose) continue;
+           if (curr.data[j].isvalue) {
+               printf(" %08lx", curr.data[j].value);
+           } else {
+               rate = (curr.data[j].value - prev.data[j].value) / (double)i;
+               printhuman(rate, curr.data[j].mult_names, curr.data[j].mult);
+           }
+       }
+       printf("\n");
+       memcpy(&prev, &curr, sizeof(prev));
+    }
+    
+}
+
+int main(int argc, char **argv)
+{
+    int  c;
+    int  mask     = 0;
+    int  minor    = 0;
+    int  interval = 0;
+    int  fd;
+    char buf[64];
+    int  i;
+
+    while ((c = getopt(argc, argv, "avmcsbM:i:")) != EOF)
+       switch (c) {
+       case 'a': mask = ~0;                          break;
+       case 'v': mask |= DRM_VERSION;                break;
+       case 'm': mask |= DRM_MEMORY;                 break;
+       case 'c': mask |= DRM_CLIENTS;                break;
+       case 's': mask |= DRM_STATS;                  break;
+       case 'b': mask |= DRM_BUSID;                  break;
+       case 'i': interval = strtol(optarg, NULL, 0); break;
+       case 'M': minor = strtol(optarg, NULL, 0);    break;
+       default:
+           fprintf( stderr, "Usage: dristat [options]\n\n" );
+           fprintf( stderr, "Displays DRM information. Use with no arguments to display available cards.\n\n" );
+           fprintf( stderr, "  -a            Show all available information\n" );
+           fprintf( stderr, "  -b            Show DRM bus ID's\n" );
+           fprintf( stderr, "  -c            Display information about DRM clients\n" );
+           fprintf( stderr, "  -i [interval] Continuously display statistics every [interval] seconds\n" );
+           fprintf( stderr, "  -v            Display DRM module and card version information\n" );
+           fprintf( stderr, "  -m            Display memory use information\n" );
+           fprintf( stderr, "  -s            Display DRM statistics\n" );
+           fprintf( stderr, "  -M [minor]    Select card by minor number\n" );
+           return 1;
+       }
+
+    for (i = 0; i < 16; i++) if (!minor || i == minor) {
+       sprintf(buf, DRM_DEV_NAME, DRM_DIR_NAME, i);
+       fd = drmOpenMinor(i, 1, DRM_NODE_RENDER);
+       if (fd >= 0) {
+           printf("%s\n", buf);
+           if (mask & DRM_BUSID)   getbusid(fd);
+           if (mask & DRM_VERSION) getversion(fd);
+           if (mask & DRM_MEMORY)  getvm(fd);
+           if (mask & DRM_CLIENTS) getclients(fd);
+           if (mask & DRM_STATS)   getstats(fd, interval);
+           close(fd);
+       }
+    }
+
+    return 0; 
+}
diff --git a/tests/drmstat.c b/tests/drmstat.c
new file mode 100644 (file)
index 0000000..345b8d2
--- /dev/null
@@ -0,0 +1,435 @@
+/* drmstat.c -- DRM device status and testing program
+ * Created: Tue Jan  5 08:19:24 1999 by faith@precisioninsight.com
+ *
+ * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
+ * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ * 
+ * Authors: Rickard E. (Rik) Faith <faith@valinux.com>
+ * 
+ */
+
+#include "config.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/time.h>
+#include <sys/mman.h>
+#include <getopt.h>
+#include <strings.h>
+#include <errno.h>
+#include <signal.h>
+#include <fcntl.h>
+#ifdef HAVE_ALLOCA_H
+# include <alloca.h>
+#endif
+#include "xf86drm.h"
+
+/* Support gcc's __FUNCTION__ for people using other compilers */
+#if !defined(__GNUC__) && !defined(__FUNCTION__)
+# define __FUNCTION__ __func__ /* C99 */
+#endif
+
+int sigio_fd;
+
+static double usec(struct timeval *end, struct timeval *start)
+{
+    double e = end->tv_sec   * 1000000 + end->tv_usec;
+    double s = start->tv_sec * 1000000 + start->tv_usec;
+
+    return e - s;
+}
+
+static void getversion(int fd)
+{
+    drmVersionPtr version;
+    
+    version = drmGetVersion(fd);
+    if (version) {
+       printf( "Name: %s\n", version->name ? version->name : "?" );
+       printf( "    Version: %d.%d.%d\n",
+               version->version_major,
+               version->version_minor,
+               version->version_patchlevel );
+       printf( "    Date: %s\n", version->date ? version->date : "?" );
+       printf( "    Desc: %s\n", version->desc ? version->desc : "?" );
+       drmFreeVersion(version);
+    } else {
+       printf( "No driver available\n" );
+    }
+}
+       
+void handler(int fd, void *oldctx, void *newctx)
+{
+    printf("Got fd %d\n", fd);
+}
+
+void process_sigio(char *device)
+{
+    int              fd;
+
+    if ((fd = open(device, 0)) < 0) {
+       drmError(-errno, __FUNCTION__);
+       exit(1);
+    }
+
+    sigio_fd = fd;
+    /*  drmInstallSIGIOHandler(fd, handler); */
+    for (;;) sleep(60);
+}
+
+int main(int argc, char **argv)
+{
+    int            c;
+    int            r  = 0;
+    int            fd = -1;
+    drm_handle_t      handle;
+    void           *address;
+    char           *pt;
+    unsigned long  count;
+    unsigned long  offset;
+    unsigned long  size;
+    drm_context_t  context;
+    int            loops;
+    char           buf[1024];
+    int            i;
+    drmBufInfoPtr  info;
+    drmBufMapPtr   bufs;
+    drmLockPtr     lock;
+    int            secs;
+
+    while ((c = getopt(argc, argv,
+                      "lc:vo:O:f:s:w:W:b:r:R:P:L:C:XS:B:F:")) != EOF)
+       switch (c) {
+       case 'F':
+           count  = strtoul(optarg, NULL, 0);
+           if (!fork()) {
+               dup(fd);
+               sleep(count);
+           }
+           close(fd);
+           break;
+       case 'v': getversion(fd);                                        break;
+       case 'X':
+           if ((r = drmCreateContext(fd, &context))) {
+               drmError(r, argv[0]);
+               return 1;
+           }
+           printf( "Got %d\n", context);
+           break;
+       case 'S':
+           process_sigio(optarg);
+           break;
+       case 'C':
+           if ((r = drmSwitchToContext(fd, strtoul(optarg, NULL, 0)))) {
+               drmError(r, argv[0]);
+               return 1;
+           }
+           break;
+       case 'c':
+           if ((r = drmSetBusid(fd,optarg))) {
+               drmError(r, argv[0]);
+               return 1;
+           }
+           break;
+       case 'o':
+           if ((fd = drmOpen(optarg, NULL)) < 0) {
+               drmError(fd, argv[0]);
+               return 1;
+           }
+           break;
+       case 'O':
+           if ((fd = drmOpen(NULL, optarg)) < 0) {
+               drmError(fd, argv[0]);
+               return 1;
+           }
+           break;
+       case 'B':               /* Test buffer allocation */
+           count  = strtoul(optarg, &pt, 0);
+           size   = strtoul(pt+1, &pt, 0);
+           secs   = strtoul(pt+1, NULL, 0);
+           {
+               drmDMAReq      dma;
+               int            *indices, *sizes;
+
+               indices = alloca(sizeof(*indices) * count);
+               sizes   = alloca(sizeof(*sizes)   * count);
+               dma.context         = context;
+               dma.send_count      = 0;
+               dma.request_count   = count;
+               dma.request_size    = size;
+               dma.request_list    = indices;
+               dma.request_sizes   = sizes;
+               dma.flags           = DRM_DMA_WAIT;
+               if ((r = drmDMA(fd, &dma))) {
+                   drmError(r, argv[0]);
+                   return 1;
+               }
+               for (i = 0; i < dma.granted_count; i++) {
+                   printf("%5d: index = %d, size = %d\n",
+                          i, dma.request_list[i], dma.request_sizes[i]);
+               }
+               sleep(secs);
+               drmFreeBufs(fd, dma.granted_count, indices);
+           }
+           break;
+       case 'b':
+           count   = strtoul(optarg, &pt, 0);
+           size    = strtoul(pt+1, NULL, 0);
+           if ((r = drmAddBufs(fd, count, size, 0, 65536)) < 0) {
+               drmError(r, argv[0]);
+               return 1;
+           }
+           if (!(info = drmGetBufInfo(fd))) {
+               drmError(0, argv[0]);
+               return 1;
+           }
+           for (i = 0; i < info->count; i++) {
+               printf("%5d buffers of size %6d (low = %d, high = %d)\n",
+                      info->list[i].count,
+                      info->list[i].size,
+                      info->list[i].low_mark,
+                      info->list[i].high_mark);
+           }
+           if ((r = drmMarkBufs(fd, 0.50, 0.80))) {
+               drmError(r, argv[0]);
+               return 1;
+           }
+           if (!(info = drmGetBufInfo(fd))) {
+               drmError(0, argv[0]);
+               return 1;
+           }
+           for (i = 0; i < info->count; i++) {
+               printf("%5d buffers of size %6d (low = %d, high = %d)\n",
+                      info->list[i].count,
+                      info->list[i].size,
+                      info->list[i].low_mark,
+                      info->list[i].high_mark);
+           }
+           printf("===== /proc/dri/0/mem =====\n");
+           sprintf(buf, "cat /proc/dri/0/mem");
+           system(buf);
+#if 1
+           if (!(bufs = drmMapBufs(fd))) {
+               drmError(0, argv[0]);
+               return 1;
+           }
+           printf("===============================\n");
+           printf( "%d bufs\n", bufs->count);
+           for (i = 0; i < bufs->count; i++) {
+               printf( "  %4d: %8d bytes at %p\n",
+                       i,
+                       bufs->list[i].total,
+                       bufs->list[i].address);
+           }
+           printf("===== /proc/dri/0/vma =====\n");
+           sprintf(buf, "cat /proc/dri/0/vma");
+           system(buf);
+#endif
+           break;
+       case 'f':
+           offset  = strtoul(optarg, &pt, 0);
+           size    = strtoul(pt+1, NULL, 0);
+           handle  = 0;
+           if ((r = drmAddMap(fd, offset, size,
+                              DRM_FRAME_BUFFER, 0, &handle))) {
+               drmError(r, argv[0]);
+               return 1;
+           }
+           printf("0x%08lx:0x%04lx added\n", offset, size);
+           printf("===== /proc/dri/0/mem =====\n");
+           sprintf(buf, "cat /proc/dri/0/mem");
+           system(buf);
+           break;
+       case 'r':
+       case 'R':
+           offset  = strtoul(optarg, &pt, 0);
+           size    = strtoul(pt+1, NULL, 0);
+           handle  = 0;
+           if ((r = drmAddMap(fd, offset, size,
+                              DRM_REGISTERS,
+                              c == 'R' ? DRM_READ_ONLY : 0,
+                              &handle))) {
+               drmError(r, argv[0]);
+               return 1;
+           }
+           printf("0x%08lx:0x%04lx added\n", offset, size);
+           printf("===== /proc/dri/0/mem =====\n");
+           sprintf(buf, "cat /proc/dri/0/mem");
+           system(buf);
+           break;
+       case 's':
+           size = strtoul(optarg, &pt, 0);
+           handle = 0;
+           if ((r = drmAddMap(fd, 0, size,
+                              DRM_SHM, DRM_CONTAINS_LOCK,
+                              &handle))) {
+               drmError(r, argv[0]);
+               return 1;
+           }
+           printf("0x%04lx byte shm added at 0x%08lx\n", size, handle);
+           sprintf(buf, "cat /proc/dri/0/vm");
+           system(buf);
+           break;
+       case 'P':
+           offset  = strtoul(optarg, &pt, 0);
+           size    = strtoul(pt+1, NULL, 0);
+           address = NULL;
+           if ((r = drmMap(fd, offset, size, &address))) {
+               drmError(r, argv[0]);
+               return 1;
+           }
+           printf("0x%08lx:0x%04lx mapped at %p for pid %d\n",
+                  offset, size, address, getpid());
+           printf("===== /proc/dri/0/vma =====\n");
+           sprintf(buf, "cat /proc/dri/0/vma");
+           system(buf);
+           mprotect((void *)offset, size, PROT_READ);
+           printf("===== /proc/dri/0/vma =====\n");
+           sprintf(buf, "cat /proc/dri/0/vma");
+           system(buf);
+           break;
+       case 'w':
+       case 'W':
+           offset  = strtoul(optarg, &pt, 0);
+           size    = strtoul(pt+1, NULL, 0);
+           address = NULL;
+           if ((r = drmMap(fd, offset, size, &address))) {
+               drmError(r, argv[0]);
+               return 1;
+           }
+           printf("0x%08lx:0x%04lx mapped at %p for pid %d\n",
+                  offset, size, address, getpid());
+           printf("===== /proc/%d/maps =====\n", getpid());
+           sprintf(buf, "cat /proc/%d/maps", getpid());
+           system(buf);
+           printf("===== /proc/dri/0/mem =====\n");
+           sprintf(buf, "cat /proc/dri/0/mem");
+           system(buf);
+           printf("===== /proc/dri/0/vma =====\n");
+           sprintf(buf, "cat /proc/dri/0/vma");
+           system(buf);
+           printf("===== READING =====\n");
+           for (i = 0; i < 0x10; i++)
+               printf("%02x ", (unsigned int)((unsigned char *)address)[i]);
+           printf("\n");
+           if (c == 'w') {
+               printf("===== WRITING =====\n");
+               for (i = 0; i < size; i+=2) {
+                   ((char *)address)[i]   = i & 0xff;
+                   ((char *)address)[i+1] = i & 0xff;
+               }
+           }
+           printf("===== READING =====\n");
+           for (i = 0; i < 0x10; i++)
+               printf("%02x ", (unsigned int)((unsigned char *)address)[i]);
+           printf("\n");
+           printf("===== /proc/dri/0/vma =====\n");
+           sprintf(buf, "cat /proc/dri/0/vma");
+           system(buf);
+           break;
+       case 'L':
+           context = strtoul(optarg, &pt, 0);
+           offset  = strtoul(pt+1, &pt, 0);
+           size    = strtoul(pt+1, &pt, 0);
+           loops   = strtoul(pt+1, NULL, 0);
+           address = NULL;
+           if ((r = drmMap(fd, offset, size, &address))) {
+               drmError(r, argv[0]);
+               return 1;
+           }
+           lock       = address;
+#if 1
+           {
+               int            counter = 0;
+               struct timeval loop_start, loop_end;
+               struct timeval lock_start, lock_end;
+               double         wt;
+#define HISTOSIZE 9
+               int            histo[HISTOSIZE];
+               int            output = 0;
+               int            fast   = 0;
+
+               if (loops < 0) {
+                   loops = -loops;
+                   ++output;
+               }
+
+               for (i = 0; i < HISTOSIZE; i++) histo[i] = 0;
+
+               gettimeofday(&loop_start, NULL);
+               for (i = 0; i < loops; i++) {
+                   gettimeofday(&lock_start, NULL);
+                   DRM_LIGHT_LOCK_COUNT(fd,lock,context,fast);
+                   gettimeofday(&lock_end, NULL);
+                   DRM_UNLOCK(fd,lock,context);
+                   ++counter;
+                   wt = usec(&lock_end, &lock_start);
+                   if      (wt <=      2.5) ++histo[8];
+                   if      (wt <       5.0) ++histo[0];
+                   else if (wt <      50.0) ++histo[1];
+                   else if (wt <     500.0) ++histo[2];
+                   else if (wt <    5000.0) ++histo[3];
+                   else if (wt <   50000.0) ++histo[4];
+                   else if (wt <  500000.0) ++histo[5];
+                   else if (wt < 5000000.0) ++histo[6];
+                   else                     ++histo[7];
+                   if (output) printf( "%.2f uSec, %d fast\n", wt, fast);
+               }
+               gettimeofday(&loop_end, NULL);
+               printf( "Average wait time = %.2f usec, %d fast\n",
+                       usec(&loop_end, &loop_start) /  counter, fast);
+               printf( "%9d <=     2.5 uS\n", histo[8]);
+               printf( "%9d <        5 uS\n", histo[0]);
+               printf( "%9d <       50 uS\n", histo[1]);
+               printf( "%9d <      500 uS\n", histo[2]);
+               printf( "%9d <     5000 uS\n", histo[3]);
+               printf( "%9d <    50000 uS\n", histo[4]);
+               printf( "%9d <   500000 uS\n", histo[5]);
+               printf( "%9d <  5000000 uS\n", histo[6]);
+               printf( "%9d >= 5000000 uS\n", histo[7]);
+           }
+#else
+           printf( "before lock: 0x%08x\n", lock->lock);
+           printf( "lock: 0x%08x\n", lock->lock);
+           sleep(5);
+           printf( "unlock: 0x%08x\n", lock->lock);
+#endif
+           break;
+       default:
+           fprintf( stderr, "Usage: drmstat [options]\n" );
+           return 1;
+       }
+
+    return r; 
+}
+
+void
+xf86VDrvMsgVerb(int scrnIndex, int type, int verb, const char *format,
+                va_list args)
+{
+       vfprintf(stderr, format, args);
+}
+
+int xf86ConfigDRI[10];
diff --git a/tests/drmtest.c b/tests/drmtest.c
new file mode 100644 (file)
index 0000000..022994a
--- /dev/null
@@ -0,0 +1,135 @@
+/*
+ * Copyright Â© 2007 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Authors:
+ *    Eric Anholt <eric@anholt.net>
+ *
+ */
+
+#include <string.h>
+#include <fcntl.h>
+#include <fnmatch.h>
+#include <sys/stat.h>
+#include <sys/ioctl.h>
+#include "drmtest.h"
+
+#define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE
+#include <libudev.h>
+
+static int is_master(int fd)
+{
+       drm_client_t client;
+       int ret;
+
+       /* Check that we're the only opener and authed. */
+       client.idx = 0;
+       ret = ioctl(fd, DRM_IOCTL_GET_CLIENT, &client);
+       assert (ret == 0);
+       if (!client.auth)
+               return 0;
+       client.idx = 1;
+       ret = ioctl(fd, DRM_IOCTL_GET_CLIENT, &client);
+       if (ret != -1 || errno != EINVAL)
+               return 0;
+
+       return 1;
+}
+
+/** Open the first DRM device matching the criteria */
+int drm_open_matching(const char *pci_glob, int flags)
+{
+       struct udev *udev;
+       struct udev_enumerate *e;
+       struct udev_device *device, *parent;
+        struct udev_list_entry *entry;
+       const char *pci_id, *path;
+       const char *usub, *dnode;
+       int fd;
+
+       udev = udev_new();
+       if (udev == NULL) {
+               fprintf(stderr, "failed to initialize udev context\n");
+               abort();
+       }
+
+       fd = -1;
+       e = udev_enumerate_new(udev);
+       udev_enumerate_add_match_subsystem(e, "drm");
+        udev_enumerate_scan_devices(e);
+        udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
+               path = udev_list_entry_get_name(entry);
+               device = udev_device_new_from_syspath(udev, path);
+               parent = udev_device_get_parent(device);
+               usub = udev_device_get_subsystem(parent);
+               /* Filter out KMS output devices. */
+               if (!usub || (strcmp(usub, "pci") != 0))
+                       continue;
+               pci_id = udev_device_get_property_value(parent, "PCI_ID");
+               if (fnmatch(pci_glob, pci_id, 0) != 0)
+                       continue;
+               dnode = udev_device_get_devnode(device);
+               if (strstr(dnode, "control"))
+                       continue;
+               fd = open(dnode, O_RDWR);
+               if (fd < 0)
+                       continue;
+               if ((flags & DRM_TEST_MASTER) && !is_master(fd)) {
+                       close(fd);
+                       fd = -1;
+                       continue;
+               }
+
+               break;
+       }
+        udev_enumerate_unref(e);
+       udev_unref(udev);
+
+       return fd;
+}
+
+int drm_open_any(void)
+{
+       int fd = drm_open_matching("*:*", 0);
+
+       if (fd < 0) {
+               fprintf(stderr, "failed to open any drm device\n");
+               exit(0);
+       }
+
+       return fd;
+}
+
+/**
+ * Open the first DRM device we can find where we end up being the master.
+ */
+int drm_open_any_master(void)
+{
+       int fd = drm_open_matching("*:*", DRM_TEST_MASTER);
+
+       if (fd < 0) {
+               fprintf(stderr, "failed to open any drm device\n");
+               exit(0);
+       }
+
+       return fd;
+
+}
diff --git a/tests/drmtest.h b/tests/drmtest.h
new file mode 100644 (file)
index 0000000..55bb446
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * Copyright Â© 2007 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Authors:
+ *    Eric Anholt <eric@anholt.net>
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <assert.h>
+#include <errno.h>
+
+#include "xf86drm.h"
+
+#define DRM_TEST_MASTER 0x01
+
+int drm_open_any(void);
+int drm_open_any_master(void);
+int drm_open_matching(const char *pci_glob, int flags);
diff --git a/tests/gem_basic.c b/tests/gem_basic.c
new file mode 100644 (file)
index 0000000..4e4b6cb
--- /dev/null
@@ -0,0 +1,102 @@
+/*
+ * Copyright Â© 2008 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Authors:
+ *    Eric Anholt <eric@anholt.net>
+ *
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <errno.h>
+#include <sys/stat.h>
+#include "drm.h"
+#include "i915_drm.h"
+
+static void
+test_bad_close(int fd)
+{
+       struct drm_gem_close close;
+       int ret;
+
+       printf("Testing error return on bad close ioctl.\n");
+
+       close.handle = 0x10101010;
+       ret = ioctl(fd, DRM_IOCTL_GEM_CLOSE, &close);
+
+       assert(ret == -1 && errno == EINVAL);
+}
+
+static void
+test_create_close(int fd)
+{
+       struct drm_i915_gem_create create;
+       struct drm_gem_close close;
+       int ret;
+
+       printf("Testing creating and closing an object.\n");
+
+       memset(&create, 0, sizeof(create));
+       create.size = 16 * 1024;
+       ret = ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create);
+       assert(ret == 0);
+
+       close.handle = create.handle;
+       ret = ioctl(fd, DRM_IOCTL_GEM_CLOSE, &close);
+}
+
+static void
+test_create_fd_close(int fd)
+{
+       struct drm_i915_gem_create create;
+       int ret;
+
+       printf("Testing closing with an object allocated.\n");
+
+       memset(&create, 0, sizeof(create));
+       create.size = 16 * 1024;
+       ret = ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create);
+       assert(ret == 0);
+
+       close(fd);
+}
+
+int main(int argc, char **argv)
+{
+       int fd;
+
+       fd = drm_open_matching("8086:*", 0);
+       if (fd < 0) {
+               fprintf(stderr, "failed to open intel drm device\n");
+               return 0;
+       }
+
+       test_bad_close(fd);
+       test_create_close(fd);
+       test_create_fd_close(fd);
+
+       return 0;
+}
diff --git a/tests/gem_flink.c b/tests/gem_flink.c
new file mode 100644 (file)
index 0000000..8dc8832
--- /dev/null
@@ -0,0 +1,132 @@
+/*
+ * Copyright Â© 2008 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Authors:
+ *    Eric Anholt <eric@anholt.net>
+ *
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <errno.h>
+#include <sys/stat.h>
+#include "drm.h"
+#include "i915_drm.h"
+
+static void
+test_flink(int fd)
+{
+       struct drm_i915_gem_create create;
+       struct drm_gem_flink flink;
+       struct drm_gem_open open;
+       int ret;
+
+       printf("Testing flink and open.\n");
+
+       memset(&create, 0, sizeof(create));
+       create.size = 16 * 1024;
+       ret = ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create);
+       assert(ret == 0);
+
+       flink.handle = create.handle;
+       ret = ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink);
+       assert(ret == 0);
+
+       open.name = flink.name;
+       ret = ioctl(fd, DRM_IOCTL_GEM_OPEN, &open);
+       assert(ret == 0);
+       assert(open.handle != 0);
+}
+
+static void
+test_double_flink(int fd)
+{
+       struct drm_i915_gem_create create;
+       struct drm_gem_flink flink;
+       struct drm_gem_flink flink2;
+       int ret;
+
+       printf("Testing repeated flink.\n");
+
+       memset(&create, 0, sizeof(create));
+       create.size = 16 * 1024;
+       ret = ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create);
+       assert(ret == 0);
+
+       flink.handle = create.handle;
+       ret = ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink);
+       assert(ret == 0);
+
+       flink2.handle = create.handle;
+       ret = ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink2);
+       assert(ret == 0);
+       assert(flink2.name == flink.name);
+}
+
+static void
+test_bad_flink(int fd)
+{
+       struct drm_gem_flink flink;
+       int ret;
+
+       printf("Testing error return on bad flink ioctl.\n");
+
+       flink.handle = 0x10101010;
+       ret = ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink);
+       assert(ret == -1 && errno == ENOENT);
+}
+
+static void
+test_bad_open(int fd)
+{
+       struct drm_gem_open open;
+       int ret;
+
+       printf("Testing error return on bad open ioctl.\n");
+
+       open.name = 0x10101010;
+       ret = ioctl(fd, DRM_IOCTL_GEM_OPEN, &open);
+
+       assert(ret == -1 && errno == ENOENT);
+}
+
+int main(int argc, char **argv)
+{
+       int fd;
+
+       fd = drm_open_matching("8086:*", 0);
+       if (fd < 0) {
+               fprintf(stderr, "failed to open intel drm device, skipping\n");
+               return 0;
+       }
+
+       test_flink(fd);
+       test_double_flink(fd);
+       test_bad_flink(fd);
+       test_bad_open(fd);
+
+       return 0;
+}
diff --git a/tests/gem_mmap.c b/tests/gem_mmap.c
new file mode 100644 (file)
index 0000000..2239789
--- /dev/null
@@ -0,0 +1,136 @@
+/*
+ * Copyright Â© 2008 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Authors:
+ *    Eric Anholt <eric@anholt.net>
+ *
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <errno.h>
+#include <sys/stat.h>
+#include "drm.h"
+#include "i915_drm.h"
+
+#define OBJECT_SIZE 16384
+
+int do_read(int fd, int handle, void *buf, int offset, int size)
+{
+       struct drm_i915_gem_pread read;
+
+       /* Ensure that we don't have any convenient data in buf in case
+        * we fail.
+        */
+       memset(buf, 0xd0, size);
+
+       memset(&read, 0, sizeof(read));
+       read.handle = handle;
+       read.data_ptr = (uintptr_t)buf;
+       read.size = size;
+       read.offset = offset;
+
+       return ioctl(fd, DRM_IOCTL_I915_GEM_PREAD, &read);
+}
+
+int do_write(int fd, int handle, void *buf, int offset, int size)
+{
+       struct drm_i915_gem_pwrite write;
+
+       memset(&write, 0, sizeof(write));
+       write.handle = handle;
+       write.data_ptr = (uintptr_t)buf;
+       write.size = size;
+       write.offset = offset;
+
+       return ioctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &write);
+}
+
+int main(int argc, char **argv)
+{
+       int fd;
+       struct drm_i915_gem_create create;
+       struct drm_i915_gem_mmap mmap;
+       struct drm_gem_close unref;
+       uint8_t expected[OBJECT_SIZE];
+       uint8_t buf[OBJECT_SIZE];
+       uint8_t *addr;
+       int ret;
+       int handle;
+
+       fd = drm_open_matching("8086:*", 0);
+       if (fd < 0) {
+               fprintf(stderr, "failed to open intel drm device, skipping\n");
+               return 0;
+       }
+
+       memset(&mmap, 0, sizeof(mmap));
+       mmap.handle = 0x10101010;
+       mmap.offset = 0;
+       mmap.size = 4096;
+       printf("Testing mmaping of bad object.\n");
+       ret = ioctl(fd, DRM_IOCTL_I915_GEM_MMAP, &mmap);
+       assert(ret == -1 && errno == ENOENT);
+
+       memset(&create, 0, sizeof(create));
+       create.size = OBJECT_SIZE;
+       ret = ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create);
+       assert(ret == 0);
+       handle = create.handle;
+
+       printf("Testing mmaping of newly created object.\n");
+       mmap.handle = handle;
+       mmap.offset = 0;
+       mmap.size = OBJECT_SIZE;
+       ret = ioctl(fd, DRM_IOCTL_I915_GEM_MMAP, &mmap);
+       assert(ret == 0);
+       addr = (uint8_t *)(uintptr_t)mmap.addr_ptr;
+
+       printf("Testing contents of newly created object.\n");
+       memset(expected, 0, sizeof(expected));
+       assert(memcmp(addr, expected, sizeof(expected)) == 0);
+
+       printf("Testing coherency of writes and mmap reads.\n");
+       memset(buf, 0, sizeof(buf));
+       memset(buf + 1024, 0x01, 1024);
+       memset(expected + 1024, 0x01, 1024);
+       ret = do_write(fd, handle, buf, 0, OBJECT_SIZE);
+       assert(ret == 0);
+       assert(memcmp(buf, addr, sizeof(buf)) == 0);
+
+       printf("Testing that mapping stays after close\n");
+       unref.handle = handle;
+       ret = ioctl(fd, DRM_IOCTL_GEM_CLOSE, &unref);
+       assert(ret == 0);
+       assert(memcmp(buf, addr, sizeof(buf)) == 0);
+
+       printf("Testing unmapping\n");
+       munmap(addr, OBJECT_SIZE);
+
+       close(fd);
+
+       return 0;
+}
diff --git a/tests/gem_readwrite.c b/tests/gem_readwrite.c
new file mode 100644 (file)
index 0000000..07dc853
--- /dev/null
@@ -0,0 +1,139 @@
+/*
+ * Copyright Â© 2008 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Authors:
+ *    Eric Anholt <eric@anholt.net>
+ *
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <errno.h>
+#include <sys/stat.h>
+#include "drm.h"
+#include "i915_drm.h"
+
+#define OBJECT_SIZE 16384
+
+int do_read(int fd, int handle, void *buf, int offset, int size)
+{
+       struct drm_i915_gem_pread read;
+
+       /* Ensure that we don't have any convenient data in buf in case
+        * we fail.
+        */
+       memset(buf, 0xd0, size);
+
+       memset(&read, 0, sizeof(read));
+       read.handle = handle;
+       read.data_ptr = (uintptr_t)buf;
+       read.size = size;
+       read.offset = offset;
+
+       return ioctl(fd, DRM_IOCTL_I915_GEM_PREAD, &read);
+}
+
+int do_write(int fd, int handle, void *buf, int offset, int size)
+{
+       struct drm_i915_gem_pwrite write;
+
+       memset(&write, 0, sizeof(write));
+       write.handle = handle;
+       write.data_ptr = (uintptr_t)buf;
+       write.size = size;
+       write.offset = offset;
+
+       return ioctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &write);
+}
+
+int main(int argc, char **argv)
+{
+       int fd;
+       struct drm_i915_gem_create create;
+       uint8_t expected[OBJECT_SIZE];
+       uint8_t buf[OBJECT_SIZE];
+       int ret;
+       int handle;
+
+       fd = drm_open_matching("8086:*", 0);
+       if (fd < 0) {
+               fprintf(stderr, "failed to open intel drm device, skipping\n");
+               return 0;
+       }
+
+       memset(&create, 0, sizeof(create));
+       create.size = OBJECT_SIZE;
+       ret = ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create);
+       assert(ret == 0);
+       handle = create.handle;
+
+       printf("Testing contents of newly created object.\n");
+       ret = do_read(fd, handle, buf, 0, OBJECT_SIZE);
+       assert(ret == 0);
+       memset(&expected, 0, sizeof(expected));
+       assert(memcmp(expected, buf, sizeof(expected)) == 0);
+
+       printf("Testing read beyond end of buffer.\n");
+       ret = do_read(fd, handle, buf, OBJECT_SIZE / 2, OBJECT_SIZE);
+       printf("%d %d\n", ret, errno);
+       assert(ret == -1 && errno == EINVAL);
+
+       printf("Testing full write of buffer\n");
+       memset(buf, 0, sizeof(buf));
+       memset(buf + 1024, 0x01, 1024);
+       memset(expected + 1024, 0x01, 1024);
+       ret = do_write(fd, handle, buf, 0, OBJECT_SIZE);
+       assert(ret == 0);
+       ret = do_read(fd, handle, buf, 0, OBJECT_SIZE);
+       assert(ret == 0);
+       assert(memcmp(buf, expected, sizeof(buf)) == 0);
+
+       printf("Testing partial write of buffer\n");
+       memset(buf + 4096, 0x02, 1024);
+       memset(expected + 4096, 0x02, 1024);
+       ret = do_write(fd, handle, buf + 4096, 4096, 1024);
+       assert(ret == 0);
+       ret = do_read(fd, handle, buf, 0, OBJECT_SIZE);
+       assert(ret == 0);
+       assert(memcmp(buf, expected, sizeof(buf)) == 0);
+
+       printf("Testing partial read of buffer\n");
+       ret = do_read(fd, handle, buf, 512, 1024);
+       assert(ret == 0);
+       assert(memcmp(buf, expected + 512, 1024) == 0);
+
+       printf("Testing read of bad buffer handle\n");
+       ret = do_read(fd, 1234, buf, 0, 1024);
+       assert(ret == -1 && errno == ENOENT);
+
+       printf("Testing write of bad buffer handle\n");
+       ret = do_write(fd, 1234, buf, 0, 1024);
+       assert(ret == -1 && errno == ENOENT);
+
+       close(fd);
+
+       return 0;
+}
diff --git a/tests/gemtest/Makefile.am b/tests/gemtest/Makefile.am
new file mode 100644 (file)
index 0000000..08432a1
--- /dev/null
@@ -0,0 +1,13 @@
+AM_CFLAGS = \
+       -I$(top_srcdir)/include/drm \
+       -I$(top_srcdir) \
+       $(CAIRO_CFLAGS)
+
+noinst_PROGRAMS = \
+       gemtest
+
+gemtest_SOURCES = \
+       gemtest.c
+gemtest_LDADD = \
+       $(top_builddir)/libdrm.la \
+       $(CAIRO_LIBS)
diff --git a/tests/gemtest/gemtest.c b/tests/gemtest/gemtest.c
new file mode 100644 (file)
index 0000000..970ac17
--- /dev/null
@@ -0,0 +1,906 @@
+/*
+ * DRM based mode setting test program
+ * Copyright 2008 Tungsten Graphics
+ *   Jakob Bornecrantz <jakob@tungstengraphics.com>
+ * Copyright 2008 Intel Corporation
+ *   Jesse Barnes <jesse.barnes@intel.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+/*
+ * This fairly simple test program dumps output in a similar format to the
+ * "xrandr" tool everyone knows & loves.  It's necessarily slightly different
+ * since the kernel separates outputs into encoder and connector structures,
+ * each with their own unique ID.  The program also allows test testing of the
+ * memory management and mode setting APIs by allowing the user to specify a
+ * connector and mode to use for mode setting.  If all works as expected, a
+ * blue background should be painted on the monitor attached to the specified
+ * connector after the selected mode is set.
+ *
+ * TODO: use cairo to write the mode info on the selected output once
+ *       the mode has been programmed, along with possible test patterns.
+ */
+#include "config.h"
+
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/poll.h>
+#include <sys/time.h>
+#include <sys/mman.h>
+
+#include "xf86drm.h"
+#include "xf86drmMode.h"
+#include "exynos_drm.h"
+
+#ifdef HAVE_CAIRO
+#include <math.h>
+#include <cairo.h>
+#endif
+
+#define ALIGN(x, a) (((x) + (a) - 1) & ~((a) + 1))
+
+/*
+ * DIRECT_MAP:
+ *     - at mapping request, physical memory allocated by gem interface
+ *     would be mmaped to user space directly.
+ *
+ * INDIRECT_MAP:
+ *     - when user tries to access user space memory after mmap,
+ *     in kernel side, the page fault occurs and then the memory would
+ *     be mmaped to physical memory. user can not aware of this moment.
+ *
+ *     P.S. in this feature, mmap call is not real-mmaped, just fake-mmaped
+ *     and to test it, enable only one of them please.
+ */
+
+#define DIRECT_MAP
+//#define INDIRECT_MAP
+
+#define CACHE_TEST
+
+#define USERPTR_TEST
+
+drmModeRes *resources;
+int fd, modes;
+
+#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
+
+struct type_name {
+       int type;
+       char *name;
+};
+
+#define type_name_fn(res) \
+char * res##_str(int type) {                   \
+       int i;                                          \
+       for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \
+               if (res##_names[i].type == type)        \
+                       return res##_names[i].name;     \
+       }                                               \
+       return "(invalid)";                             \
+}
+
+struct type_name encoder_type_names[] = {
+       { DRM_MODE_ENCODER_NONE, "none" },
+       { DRM_MODE_ENCODER_DAC, "DAC" },
+       { DRM_MODE_ENCODER_TMDS, "TMDS" },
+       { DRM_MODE_ENCODER_LVDS, "LVDS" },
+       { DRM_MODE_ENCODER_TVDAC, "TVDAC" },
+};
+
+type_name_fn(encoder_type)
+
+struct type_name connector_status_names[] = {
+       { DRM_MODE_CONNECTED, "connected" },
+       { DRM_MODE_DISCONNECTED, "disconnected" },
+       { DRM_MODE_UNKNOWNCONNECTION, "unknown" },
+};
+
+type_name_fn(connector_status)
+
+struct type_name connector_type_names[] = {
+       { DRM_MODE_CONNECTOR_Unknown, "unknown" },
+       { DRM_MODE_CONNECTOR_VGA, "VGA" },
+       { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
+       { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
+       { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
+       { DRM_MODE_CONNECTOR_Composite, "composite" },
+       { DRM_MODE_CONNECTOR_SVIDEO, "s-video" },
+       { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
+       { DRM_MODE_CONNECTOR_Component, "component" },
+       { DRM_MODE_CONNECTOR_9PinDIN, "9-pin DIN" },
+       { DRM_MODE_CONNECTOR_DisplayPort, "displayport" },
+       { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
+       { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
+       { DRM_MODE_CONNECTOR_TV, "TV" },
+       { DRM_MODE_CONNECTOR_eDP, "embedded displayport" },
+};
+
+type_name_fn(connector_type)
+
+void dump_encoders(void)
+{
+       drmModeEncoder *encoder;
+       int i;
+
+       printf("Encoders:\n");
+       printf("id\tcrtc\ttype\tpossible crtcs\tpossible clones\t\n");
+       for (i = 0; i < resources->count_encoders; i++) {
+               encoder = drmModeGetEncoder(fd, resources->encoders[i]);
+
+               if (!encoder) {
+                       fprintf(stderr, "could not get encoder %i: %s\n",
+                               resources->encoders[i], strerror(errno));
+                       continue;
+               }
+               printf("%d\t%d\t%s\t0x%08x\t0x%08x\n",
+                      encoder->encoder_id,
+                      encoder->crtc_id,
+                      encoder_type_str(encoder->encoder_type),
+                      encoder->possible_crtcs,
+                      encoder->possible_clones);
+               drmModeFreeEncoder(encoder);
+       }
+       printf("\n");
+}
+
+void dump_mode(drmModeModeInfo *mode)
+{
+       printf("  %s %d %d %d %d %d %d %d %d %d\n",
+              mode->name,
+              mode->vrefresh,
+              mode->hdisplay,
+              mode->hsync_start,
+              mode->hsync_end,
+              mode->htotal,
+              mode->vdisplay,
+              mode->vsync_start,
+              mode->vsync_end,
+              mode->vtotal);
+}
+
+static void
+dump_props(drmModeConnector *connector)
+{
+       drmModePropertyPtr props;
+       int i;
+
+       for (i = 0; i < connector->count_props; i++) {
+               props = drmModeGetProperty(fd, connector->props[i]);
+               printf("\t%s, flags %d\n", props->name, props->flags);
+               drmModeFreeProperty(props);
+       }
+}
+
+void dump_connectors(void)
+{
+       drmModeConnector *connector;
+       int i, j;
+
+       printf("Connectors:\n");
+       printf("id\tencoder\tstatus\t\ttype\tsize (mm)\tmodes\tencoders\n");
+       for (i = 0; i < resources->count_connectors; i++) {
+               connector = drmModeGetConnector(fd, resources->connectors[i]);
+
+               if (!connector) {
+                       fprintf(stderr, "could not get connector %i: %s\n",
+                               resources->connectors[i], strerror(errno));
+                       continue;
+               }
+
+               printf("%d\t%d\t%s\t%s\t%dx%d\t\t%d\t",
+                      connector->connector_id,
+                      connector->encoder_id,
+                      connector_status_str(connector->connection),
+                      connector_type_str(connector->connector_type),
+                      connector->mmWidth, connector->mmHeight,
+                      connector->count_modes);
+
+               for (j = 0; j < connector->count_encoders; j++)
+                       printf("%s%d", j > 0 ? ", " : "", connector->encoders[j]);
+               printf("\n");
+
+               if (!connector->count_modes)
+                       continue;
+
+               printf("  modes:\n");
+               printf("  name refresh (Hz) hdisp hss hse htot vdisp "
+                      "vss vse vtot)\n");
+               for (j = 0; j < connector->count_modes; j++)
+                       dump_mode(&connector->modes[j]);
+
+               printf("  props:\n");
+               dump_props(connector);
+
+               drmModeFreeConnector(connector);
+       }
+       printf("\n");
+}
+
+void dump_crtcs(void)
+{
+       drmModeCrtc *crtc;
+       int i;
+
+       printf("CRTCs:\n");
+       printf("id\tfb\tpos\tsize\n");
+       for (i = 0; i < resources->count_crtcs; i++) {
+               crtc = drmModeGetCrtc(fd, resources->crtcs[i]);
+
+               if (!crtc) {
+                       fprintf(stderr, "could not get crtc %i: %s\n",
+                               resources->crtcs[i], strerror(errno));
+                       continue;
+               }
+               printf("%d\t%d\t(%d,%d)\t(%dx%d)\n",
+                      crtc->crtc_id,
+                      crtc->buffer_id,
+                      crtc->x, crtc->y,
+                      crtc->width, crtc->height);
+               dump_mode(&crtc->mode);
+
+               drmModeFreeCrtc(crtc);
+       }
+       printf("\n");
+}
+
+void dump_framebuffers(void)
+{
+       drmModeFB *fb;
+       int i;
+
+       printf("Frame buffers:\n");
+       printf("id\tsize\tpitch\n");
+       for (i = 0; i < resources->count_fbs; i++) {
+               fb = drmModeGetFB(fd, resources->fbs[i]);
+
+               if (!fb) {
+                       fprintf(stderr, "could not get fb %i: %s\n",
+                               resources->fbs[i], strerror(errno));
+                       continue;
+               }
+               printf("%u\t(%ux%u)\t%u\n",
+                      fb->fb_id,
+                      fb->width, fb->height,
+                      fb->pitch);
+
+               drmModeFreeFB(fb);
+       }
+       printf("\n");
+}
+
+/*
+ * Mode setting with the kernel interfaces is a bit of a chore.
+ * First you have to find the connector in question and make sure the
+ * requested mode is available.
+ * Then you need to find the encoder attached to that connector so you
+ * can bind it with a free crtc.
+ */
+struct connector {
+       uint32_t id;
+       char mode_str[64];
+       drmModeModeInfo *mode;
+       drmModeEncoder *encoder;
+       int crtc;
+       unsigned int fb_id[2], current_fb_id;
+       struct timeval start;
+
+       int swap_count;
+};     
+
+static void
+connector_find_mode(struct connector *c)
+{
+       drmModeConnector *connector;
+       int i, j;
+
+       /* First, find the connector & mode */
+       c->mode = NULL;
+       for (i = 0; i < resources->count_connectors; i++) {
+               connector = drmModeGetConnector(fd, resources->connectors[i]);
+
+               if (!connector) {
+                       fprintf(stderr, "could not get connector %i: %s\n",
+                               resources->connectors[i], strerror(errno));
+                       drmModeFreeConnector(connector);
+                       continue;
+               }
+
+               if (!connector->count_modes) {
+                       drmModeFreeConnector(connector);
+                       continue;
+               }
+
+               if (connector->connector_id != c->id) {
+                       drmModeFreeConnector(connector);
+                       continue;
+               }
+
+               for (j = 0; j < connector->count_modes; j++) {
+                       c->mode = &connector->modes[j];
+                       if (!strcmp(c->mode->name, c->mode_str))
+                               break;
+               }
+
+               /* Found it, break out */
+               if (c->mode)
+                       break;
+
+               drmModeFreeConnector(connector);
+       }
+
+       if (!c->mode) {
+               fprintf(stderr, "failed to find mode \"%s\"\n", c->mode_str);
+               return;
+       }
+
+       /* Now get the encoder */
+       for (i = 0; i < resources->count_encoders; i++) {
+               c->encoder = drmModeGetEncoder(fd, resources->encoders[i]);
+
+               if (!c->encoder) {
+                       fprintf(stderr, "could not get encoder %i: %s\n",
+                               resources->encoders[i], strerror(errno));
+                       drmModeFreeEncoder(c->encoder);
+                       continue;
+               }
+
+               if (c->encoder->encoder_id  == connector->encoder_id)
+                       break;
+
+               drmModeFreeEncoder(c->encoder);
+       }
+
+       if (c->crtc == -1)
+               c->crtc = c->encoder->crtc_id;
+}
+
+static void
+make_pwetty(void *data, int width, int height, int stride)
+{
+#ifdef HAVE_CAIRO
+       cairo_surface_t *surface;
+       cairo_t *cr;
+       int x, y;
+
+       surface = cairo_image_surface_create_for_data(data,
+                                                     CAIRO_FORMAT_ARGB32,
+                                                     width, height,
+                                                     stride);
+       cr = cairo_create(surface);
+       cairo_surface_destroy(surface);
+
+       cairo_set_line_cap(cr, CAIRO_LINE_CAP_SQUARE);
+       for (x = 0; x < width; x += 250)
+               for (y = 0; y < height; y += 250) {
+                       char buf[64];
+
+                       cairo_move_to(cr, x, y - 20);
+                       cairo_line_to(cr, x, y + 20);
+                       cairo_move_to(cr, x - 20, y);
+                       cairo_line_to(cr, x + 20, y);
+                       cairo_new_sub_path(cr);
+                       cairo_arc(cr, x, y, 10, 0, M_PI * 2);
+                       cairo_set_line_width(cr, 4);
+                       cairo_set_source_rgb(cr, 0, 0, 0);
+                       cairo_stroke_preserve(cr);
+                       cairo_set_source_rgb(cr, 1, 1, 1);
+                       cairo_set_line_width(cr, 2);
+                       cairo_stroke(cr);
+
+                       snprintf(buf, sizeof buf, "%d, %d", x, y);
+                       cairo_move_to(cr, x + 20, y + 20);
+                       cairo_text_path(cr, buf);
+                       cairo_set_source_rgb(cr, 0, 0, 0);
+                       cairo_stroke_preserve(cr);
+                       cairo_set_source_rgb(cr, 1, 1, 1);
+                       cairo_fill(cr);
+               }
+
+       cairo_destroy(cr);
+#endif
+}
+
+void
+page_flip_handler(int fd, unsigned int frame,
+                 unsigned int sec, unsigned int usec, void *data)
+{
+       struct connector *c;
+       unsigned int new_fb_id;
+       struct timeval end;
+       double t;
+
+       c = data;
+       if (c->current_fb_id == c->fb_id[0])
+               new_fb_id = c->fb_id[1];
+       else
+               new_fb_id = c->fb_id[0];
+                       
+       drmModePageFlip(fd, c->crtc, new_fb_id,
+                       DRM_MODE_PAGE_FLIP_EVENT, c);
+       c->current_fb_id = new_fb_id;
+       c->swap_count++;
+       if (c->swap_count == 60) {
+               gettimeofday(&end, NULL);
+               t = end.tv_sec + end.tv_usec * 1e-6 -
+                       (c->start.tv_sec + c->start.tv_usec * 1e-6);
+               fprintf(stderr, "freq: %.02fHz\n", c->swap_count / t);
+               c->swap_count = 0;
+               c->start = end;
+       }
+}
+
+static int exynos_gem_create(int fd, struct drm_exynos_gem_create *gem)
+{
+       int ret;
+
+       if (!gem) {
+               fprintf(stderr, "gem object is null.\n");
+               return -EFAULT;
+       }
+
+       ret = ioctl(fd, DRM_IOCTL_EXYNOS_GEM_CREATE, gem);
+       if (ret < 0) {
+               fprintf(stderr, "failed to create gem buffer: %s\n",
+                               strerror(-ret));
+               return ret;
+       }
+
+       return 0;
+}
+
+static int exynos_gem_map_offset(int fd, struct drm_exynos_gem_map_off *map_off)
+{
+       int ret;
+
+       ret = ioctl(fd, DRM_IOCTL_EXYNOS_GEM_MAP_OFFSET, map_off);
+       if (ret < 0) {
+               fprintf(stderr, "failed to get buffer offset: %s\n",
+                               strerror(-ret));
+               return ret;
+       }
+
+       return 0;
+}
+
+static int exynos_gem_mmap(int fd, struct drm_exynos_gem_mmap *in_mmap)
+{
+       int ret;
+
+       ret = ioctl(fd, DRM_IOCTL_EXYNOS_GEM_MMAP, in_mmap);
+       if (ret < 0) {
+               fprintf(stderr, "failed to get buffer offset: %s\n",
+                               strerror(-ret));
+               return ret;
+       }
+
+       return 0;
+}
+
+static int exynos_gem_close(int fd, struct drm_gem_close *gem_close)
+{
+       int ret;
+
+       ret = ioctl(fd, DRM_IOCTL_GEM_CLOSE, gem_close);
+       if (ret < 0) {
+               fprintf(stderr, "failed to close gem buffer: %s\n",
+                               strerror(-ret));
+               return ret;
+       }
+
+       return 0;
+}
+
+static int exynos_gem_get_ump(int fd, struct drm_exynos_gem_ump *gem_ump)
+{
+       int ret;
+
+       ret = ioctl(fd, DRM_IOCTL_EXYNOS_GEM_EXPORT_UMP, gem_ump);
+       if (ret < 0) {
+               fprintf(stderr, "failed to get ump: %s\n",
+                               strerror(-ret));
+               return ret;
+       }
+
+       return 0;
+}
+
+static int exynos_gem_cache_op(int fd,
+                               struct drm_exynos_gem_cache_op *cache_op)
+{
+       int ret;
+
+       ret = ioctl(fd, DRM_IOCTL_EXYNOS_GEM_CACHE_OP, cache_op);
+       if (ret < 0) {
+               fprintf(stderr, "failed to cache operation: %s\n",
+                               strerror(-ret));
+               return ret;
+       }
+
+       return 0;
+}
+
+static int exynos_gem_userptr_imp(int fd,
+                       struct drm_exynos_gem_userptr_imp *imp)
+{
+       int ret;
+
+       ret = ioctl(fd, DRM_IOCTL_EXYNOS_GEM_USERPTR_IMP, imp);
+       if (ret < 0) {
+               fprintf(stderr, "failed to cache operation: %s\n",
+                               strerror(-ret));
+               return ret;
+       }
+
+       return 0;
+}
+
+static void
+set_mode(struct connector *c, int count, int page_flip)
+{
+       struct drm_exynos_gem_create gem1, gem2;
+       struct drm_exynos_gem_map_off map_off1, map_off2;
+       struct drm_exynos_gem_mmap mmap1;
+       struct drm_exynos_gem_cache_op cache_op;
+       struct drm_exynos_gem_ump gem_ump;
+       struct drm_exynos_gem_userptr_imp imp;
+       struct drm_gem_close args;
+       unsigned int fb_id, other_fb_id;
+       int i, ret, width, height, x, stride;
+       unsigned handle;
+       void *usr_addr1, *usr_addr2;
+       drmEventContext evctx;
+       unsigned int tmp;
+
+       width = 0;
+       height = 0;
+       for (i = 0; i < count; i++) {
+               connector_find_mode(&c[i]);
+               if (c[i].mode == NULL)
+                       continue;
+               width += c[i].mode->hdisplay;
+               if (height < c[i].mode->vdisplay)
+                       height = c[i].mode->vdisplay;
+       }
+
+#ifdef CACHE_TEST
+       cache_op.flags = 0;
+       cache_op.flags = (EXYNOS_DRM_ALL_CACHE | EXYNOS_DRM_CACHE_FSH);
+
+       exynos_gem_cache_op(fd, &cache_op);
+#endif
+
+       /* allocate gem buffer. */
+       gem1.size = 1024 * 600 * 4;
+
+       ret = exynos_gem_create(fd, &gem1);
+       if (ret < 0)
+               return;
+
+       handle = gem1.handle;
+
+#ifdef DIRECT_MAP
+       mmap1.handle = handle;
+       mmap1.size = gem1.size;
+       ret = exynos_gem_mmap(fd, &mmap1);
+       if (ret < 0)
+               return;
+
+       usr_addr1 = mmap1.mapped;
+       memset(usr_addr1, 0x88, mmap1.size);
+#endif
+#ifdef INDIRECT_MAP
+       map_off1.handle = handle;
+
+       /* get map offset. */
+       ret = exynos_gem_map_offset(fd, &map_off1);
+       if (ret < 0)
+               return;
+
+       usr_addr1 = mmap(0, gem1.size, PROT_READ | PROT_WRITE,
+                       MAP_SHARED, fd, map_off1.offset);
+
+       memset(usr_addr1, 0x88, gem1.size);
+#endif
+
+#ifdef USERPTR_TEST
+       memset(&imp, 0, sizeof(struct drm_exynos_gem_userptr_imp));
+
+       imp.size = mmap1.size;
+       imp.user_ptr = usr_addr1;
+
+       printf("size = 0x%x, addr = 0x%x\n", imp.size, imp.user_ptr);
+
+       ret = exynos_gem_userptr_imp(fd, &imp);
+       if (ret < 0)
+               return;
+
+       printf("handle = 0x%x\n", imp.handle);
+#endif
+
+       /* get secure id for ump. */
+       gem_ump.gem_handle = handle;
+
+       exynos_gem_get_ump(fd, &gem_ump);
+
+       printf("secure id = %d\n", gem_ump.secure_id);
+
+       ret = drmModeAddFB(fd, width, height, 32, 32, stride, handle, &fb_id);
+       if (ret) {
+               fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
+               return;
+       }
+
+       x = 0;
+       for (i = 0; i < count; i++) {
+               if (c[i].mode == NULL)
+                       continue;
+
+               printf("setting mode %s on connector %d, crtc %d\n",
+                      c[i].mode_str, c[i].id, c[i].crtc);
+
+               ret = drmModeSetCrtc(fd, c[i].crtc, fb_id, x, 0,
+                                    &c[i].id, 1, c[i].mode);
+               x += c[i].mode->hdisplay;
+
+               if (ret) {
+                       fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
+                       return;
+               }
+       }
+
+       if (!page_flip)
+               return;
+
+       /* for second gem object, INDIRECT_MODE would be used. */
+
+       gem2.size = 1024 * 600 * 4;
+
+       /* allocate gem buffer. */
+       ret = exynos_gem_create(fd, &gem2);
+       if (ret < 0)
+               return;
+
+       handle = map_off2.handle = gem2.handle;
+
+       /* get map offset. */
+       ret = exynos_gem_map_offset(fd, &map_off2);
+       if (ret < 0)
+               return;
+
+       usr_addr2 = mmap(0, gem2.size, PROT_READ | PROT_WRITE,
+                       MAP_SHARED, fd, map_off2.offset);
+
+       memset(usr_addr2, 0x0, gem2.size);
+       ret = drmModeAddFB(fd, width, height, 32, 32, stride, handle,
+                          &other_fb_id);
+       if (ret) {
+               fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
+               return;
+       }
+
+       for (i = 0; i < count; i++) {
+               if (c[i].mode == NULL)
+                       continue;
+
+               drmModePageFlip(fd, c[i].crtc, other_fb_id,
+                               DRM_MODE_PAGE_FLIP_EVENT, &c[i]);
+               gettimeofday(&c[i].start, NULL);
+               c[i].swap_count = 0;
+               c[i].fb_id[0] = fb_id;
+               c[i].fb_id[1] = other_fb_id;
+               c[i].current_fb_id = other_fb_id;
+       }
+
+       memset(&evctx, 0, sizeof evctx);
+       evctx.version = DRM_EVENT_CONTEXT_VERSION;
+       evctx.vblank_handler = NULL;
+       evctx.page_flip_handler = page_flip_handler;
+
+       while (1) {
+               struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
+               fd_set fds;
+               int ret;
+
+               FD_ZERO(&fds);
+               FD_SET(0, &fds);
+               FD_SET(fd, &fds);
+               ret = select(fd + 1, &fds, NULL, NULL, &timeout);
+
+               if (ret <= 0) {
+                       fprintf(stderr, "select timed out or error (ret %d)\n",
+                               ret);
+                       continue;
+               } else if (FD_ISSET(0, &fds)) {
+                       break;
+               }
+
+               drmHandleEvent(fd, &evctx);
+       }
+
+       /* release user space memroy. */
+#if defined(INDIRECT_MAP) || defined(DIRECT_MAP)
+       munmap(usr_addr1, mmap1.size);
+#endif
+       munmap(usr_addr2, gem2.size);
+
+       args.handle = gem1.handle;
+
+       /* relese gem buffer. */
+       ret = exynos_gem_close(fd, &args);
+       if (ret < 0)
+               return;
+
+       args.handle = gem2.handle;
+       ret = exynos_gem_close(fd, &args);
+       if (ret < 0)
+               return;
+
+#ifdef USERPTR_TEST
+       args.handle = imp.handle;
+       ret = exynos_gem_close(fd, &args);
+       if (ret < 0)
+               return;
+#endif
+}
+
+extern char *optarg;
+extern int optind, opterr, optopt;
+static char optstr[] = "ecpmfs:v";
+
+void usage(char *name)
+{
+       fprintf(stderr, "usage: %s [-ecpmf]\n", name);
+       fprintf(stderr, "\t-e\tlist encoders\n");
+       fprintf(stderr, "\t-c\tlist connectors\n");
+       fprintf(stderr, "\t-p\tlist CRTCs (pipes)\n");
+       fprintf(stderr, "\t-m\tlist modes\n");
+       fprintf(stderr, "\t-f\tlist framebuffers\n");
+       fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
+       fprintf(stderr, "\t-s <connector_id>:<mode>\tset a mode\n");
+       fprintf(stderr, "\t-s <connector_id>@<crtc_id>:<mode>\tset a mode\n");
+       fprintf(stderr, "\n\tDefault is to dump all info.\n");
+       exit(0);
+}
+
+#define dump_resource(res) if (res) dump_##res()
+
+static int page_flipping_supported(int fd)
+{
+       /*FIXME: generic ioctl needed? */
+       return 1;
+#if 0
+       int ret, value;
+       struct drm_i915_getparam gp;
+
+       gp.param = I915_PARAM_HAS_PAGEFLIPPING;
+       gp.value = &value;
+
+       ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
+       if (ret) {
+               fprintf(stderr, "drm_i915_getparam: %m\n");
+               return 0;
+       }
+
+       return *gp.value;
+#endif
+}
+
+int main(int argc, char **argv)
+{
+       int c;
+       int encoders = 0, connectors = 0, crtcs = 0, framebuffers = 0;
+       int test_vsync = 0;
+       char *modules[] = { "exynos-drm", "i915", "radeon", "nouveau" };
+       char *modeset = NULL;
+       int i, count = 0;
+       struct connector con_args[2];
+       
+       opterr = 0;
+       while ((c = getopt(argc, argv, optstr)) != -1) {
+               switch (c) {
+               case 'e':
+                       encoders = 1;
+                       break;
+               case 'c':
+                       connectors = 1;
+                       break;
+               case 'p':
+                       crtcs = 1;
+                       break;
+               case 'm':
+                       modes = 1;
+                       break;
+               case 'f':
+                       framebuffers = 1;
+                       break;
+               case 'v':
+                       test_vsync = 1;
+                       break;
+               case 's':
+                       modeset = strdup(optarg);
+                       con_args[count].crtc = -1;
+                       if (sscanf(optarg, "%d:%64s",
+                                  &con_args[count].id,
+                                  con_args[count].mode_str) != 2 &&
+                           sscanf(optarg, "%d@%d:%64s",
+                                  &con_args[count].id,
+                                  &con_args[count].crtc,
+                                  con_args[count].mode_str) != 3)
+                               usage(argv[0]);
+                       count++;                                      
+                       break;
+               default:
+                       usage(argv[0]);
+                       break;
+               }
+       }
+
+       if (argc == 1)
+               encoders = connectors = crtcs = modes = framebuffers = 1;
+
+       for (i = 0; i < ARRAY_SIZE(modules); i++) {
+               printf("trying to load module %s...", modules[i]);
+               fd = drmOpen(modules[i], NULL);
+               if (fd < 0) {
+                       printf("failed.\n");
+               } else {
+                       printf("success.\n");
+                       break;
+               }
+       }
+
+       if (test_vsync && !page_flipping_supported(fd)) {
+               fprintf(stderr, "page flipping not supported by drm.\n");
+               return -1;
+       }
+
+       if (i == ARRAY_SIZE(modules)) {
+               fprintf(stderr, "failed to load any modules, aborting.\n");
+               return -1;
+       }
+
+       resources = drmModeGetResources(fd);
+       if (!resources) {
+               fprintf(stderr, "drmModeGetResources failed: %s\n",
+                       strerror(errno));
+               drmClose(fd);
+               return 1;
+       }
+
+       dump_resource(encoders);
+       dump_resource(connectors);
+       dump_resource(crtcs);
+       dump_resource(framebuffers);
+
+       if (count > 0) {
+               set_mode(con_args, count, test_vsync);
+               getchar();
+       }
+
+       drmModeFreeResources(resources);
+
+       return 0;
+}
diff --git a/tests/getclient.c b/tests/getclient.c
new file mode 100644 (file)
index 0000000..349c16e
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * Copyright Â© 2007 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Authors:
+ *    Eric Anholt <eric@anholt.net>
+ *
+ */
+
+#include <limits.h>
+#include "drmtest.h"
+
+/**
+ * Checks DRM_IOCTL_GET_CLIENT.
+ */
+int main(int argc, char **argv)
+{
+       int fd, ret;
+       drm_client_t client;
+
+       fd = drm_open_any();
+
+       /* Look for client index 0.  This should exist whether we're operating
+        * on an otherwise unused drm device, or the X Server is running on
+        * the device.
+        */
+       client.idx = 0;
+       ret = ioctl(fd, DRM_IOCTL_GET_CLIENT, &client);
+       assert(ret == 0);
+
+       /* Look for some absurd client index and make sure it's invalid.
+        * The DRM drivers currently always return data, so the user has
+        * no real way to detect when the list has terminated.  That's bad,
+        * and this test is XFAIL as a result.
+        */
+       client.idx = 0x7fffffff;
+       ret = ioctl(fd, DRM_IOCTL_GET_CLIENT, &client);
+       assert(ret == -1 && errno == EINVAL);
+
+       close(fd);
+       return 0;
+}
diff --git a/tests/getstats.c b/tests/getstats.c
new file mode 100644 (file)
index 0000000..bd55b12
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * Copyright Â© 2007 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Authors:
+ *    Eric Anholt <eric@anholt.net>
+ *
+ */
+
+#include <limits.h>
+#include "drmtest.h"
+
+/**
+ * Checks DRM_IOCTL_GET_STATS.
+ *
+ * I don't care too much about the actual contents, just that the kernel
+ * doesn't crash.
+ */
+int main(int argc, char **argv)
+{
+       int fd, ret;
+       drm_stats_t stats;
+
+       fd = drm_open_any();
+
+       ret = ioctl(fd, DRM_IOCTL_GET_STATS, &stats);
+       assert(ret == 0);
+
+       assert(stats.count >= 0);
+
+       close(fd);
+       return 0;
+}
diff --git a/tests/getversion.c b/tests/getversion.c
new file mode 100644 (file)
index 0000000..711d376
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * Copyright Â© 2007 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Authors:
+ *    Eric Anholt <eric@anholt.net>
+ *
+ */
+
+#include "drmtest.h"
+
+/**
+ * Checks DRM_IOCTL_GET_VERSION and libdrm's drmGetVersion() interface to it.
+ */
+int main(int argc, char **argv)
+{
+       int fd;
+       drmVersionPtr v;
+
+       fd = drm_open_any();
+       v = drmGetVersion(fd);
+       assert(strlen(v->name) != 0);
+       assert(strlen(v->date) != 0);
+       assert(strlen(v->desc) != 0);
+       if (strcmp(v->name, "i915") == 0)
+               assert(v->version_major >= 1);
+       drmFree(v);
+       close(fd);
+       return 0;
+}
diff --git a/tests/kmstest/Makefile.am b/tests/kmstest/Makefile.am
new file mode 100644 (file)
index 0000000..ae562a1
--- /dev/null
@@ -0,0 +1,17 @@
+AM_CFLAGS = \
+       -I$(top_srcdir)/include/drm \
+       -I$(top_srcdir)/libkms/ \
+       -I$(top_srcdir)
+
+noinst_PROGRAMS = \
+       kmstest
+
+kmstest_SOURCES = \
+       main.c
+
+kmstest_LDADD = \
+       $(top_builddir)/libdrm.la \
+       $(top_builddir)/libkms/libkms.la
+
+run: kmstest
+       ./kmstest
diff --git a/tests/kmstest/main.c b/tests/kmstest/main.c
new file mode 100644 (file)
index 0000000..5df0a38
--- /dev/null
@@ -0,0 +1,91 @@
+/**************************************************************************
+ *
+ * Copyright Â© 2009 VMware, Inc., Palo Alto, CA., USA
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+ * USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ **************************************************************************/
+
+
+#include <stdio.h>
+#include <string.h>
+#include "xf86drm.h"
+#include "libkms.h"
+
+#define CHECK_RET_RETURN(ret, str) \
+       if (ret < 0) { \
+               printf("%s: %s (%s)\n", __func__, str, strerror(-ret)); \
+               return ret; \
+       }
+
+int test_bo(struct kms_driver *kms)
+{
+       struct kms_bo *bo;
+       int ret;
+       unsigned attrs[7] = {
+               KMS_WIDTH, 1024,
+               KMS_HEIGHT, 768,
+               KMS_BO_TYPE, KMS_BO_TYPE_SCANOUT_X8R8G8B8,
+               KMS_TERMINATE_PROP_LIST,
+       };
+
+       ret = kms_bo_create(kms, attrs, &bo);
+       CHECK_RET_RETURN(ret, "Could not create bo");
+
+       kms_bo_destroy(&bo);
+
+       return 0;
+}
+
+char *drivers[] = {
+       "i915",
+       "radeon",
+       "nouveau",
+       "vmwgfx",
+       NULL
+};
+
+int main(int argc, char** argv)
+{
+       struct kms_driver *kms;
+       int ret, fd, i;
+
+       for (i = 0, fd = -1; fd < 0 && drivers[i]; i++)
+               fd = drmOpen(drivers[i], NULL);
+       CHECK_RET_RETURN(fd, "Could not open device");
+
+       ret = kms_create(fd, &kms);
+       CHECK_RET_RETURN(ret, "Failed to create kms driver");
+
+       ret = test_bo(kms);
+       if (ret)
+               goto err;
+
+       printf("%s: All ok!\n", __func__);
+
+       kms_destroy(&kms);
+       return 0;
+
+err:
+       kms_destroy(&kms);
+       return ret;
+}
diff --git a/tests/lock.c b/tests/lock.c
new file mode 100644 (file)
index 0000000..86caa28
--- /dev/null
@@ -0,0 +1,263 @@
+/*
+ * Copyright Â© 2007 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Authors:
+ *    Eric Anholt <eric@anholt.net>
+ *
+ */
+
+/** @file lock.c
+ * Tests various potential failures of the DRM locking mechanisms
+ */
+
+#include <limits.h>
+#include "drmtest.h"
+
+enum auth_event {
+       SERVER_READY,
+       CLIENT_MAGIC,
+       SERVER_LOCKED,
+       CLIENT_LOCKED,
+};
+
+int commfd[2];
+unsigned int lock1 = 0x00001111;
+unsigned int lock2 = 0x00002222;
+
+/* return time in milliseconds */
+static unsigned int
+get_millis()
+{
+       struct timeval tv;
+
+       gettimeofday(&tv, NULL);
+       return tv.tv_sec * 1000 + tv.tv_usec / 1000;
+}
+
+static void
+wait_event(int pipe, enum auth_event expected_event)
+{
+       int ret;
+       enum auth_event event;
+       unsigned char in;
+
+       ret = read(commfd[pipe], &in, 1);
+       if (ret == -1)
+               err(1, "read error");
+       event = in;
+
+       if (event != expected_event)
+               errx(1, "unexpected event: %d\n", event);
+}
+
+static void
+send_event(int pipe, enum auth_event send_event)
+{
+       int ret;
+       unsigned char event;
+
+       event = send_event;
+       ret = write(commfd[pipe], &event, 1);
+       if (ret == -1)
+               err(1, "failed to send event %d", event);
+}
+
+static void
+client_auth(int drmfd)
+{
+       struct drm_auth auth;
+       int ret;
+
+       /* Get a client magic number and pass it to the master for auth. */
+       ret = ioctl(drmfd, DRM_IOCTL_GET_MAGIC, &auth);
+       if (ret == -1)
+               err(1, "Couldn't get client magic");
+       send_event(0, CLIENT_MAGIC);
+       ret = write(commfd[0], &auth.magic, sizeof(auth.magic));
+       if (ret == -1)
+               err(1, "Couldn't write auth data");
+}
+
+static void
+server_auth(int drmfd)
+{
+       struct drm_auth auth;
+       int ret;
+
+       send_event(1, SERVER_READY);
+       wait_event(1, CLIENT_MAGIC);
+       ret = read(commfd[1], &auth.magic, sizeof(auth.magic));
+       if (ret == -1)
+               err(1, "Failure to read client magic");
+
+       ret = ioctl(drmfd, DRM_IOCTL_AUTH_MAGIC, &auth);
+       if (ret == -1)
+               err(1, "Failure to authenticate client magic\n");
+}
+
+/** Tests that locking is successful in normal conditions */
+static void
+test_lock_unlock(int drmfd)
+{
+       int ret;
+
+       ret = drmGetLock(drmfd, lock1, 0);
+       if (ret != 0)
+               err(1, "Locking failed");
+       ret = drmUnlock(drmfd, lock1);
+       if (ret != 0)
+               err(1, "Unlocking failed");
+}
+
+/** Tests that unlocking the lock while it's not held works correctly */
+static void
+test_unlock_unlocked(int drmfd)
+{
+       int ret;
+
+       ret = drmUnlock(drmfd, lock1);
+       if (ret == 0)
+               err(1, "Unlocking unlocked lock succeeded");
+}
+
+/** Tests that unlocking a lock held by another context fails appropriately */
+static void
+test_unlock_unowned(int drmfd)
+{
+       int ret;
+
+       ret = drmGetLock(drmfd, lock1, 0);
+       assert(ret == 0);
+       ret = drmUnlock(drmfd, lock2);
+       if (ret == 0)
+               errx(1, "Unlocking other context's lock succeeded");
+       ret = drmUnlock(drmfd, lock1);
+       assert(ret == 0);
+}
+
+/**
+ * Tests that an open/close by the same process doesn't result in the lock
+ * being dropped.
+ */
+static void test_open_close_locked(drmfd)
+{
+       int ret, tempfd;
+
+       ret = drmGetLock(drmfd, lock1, 0);
+       assert(ret == 0);
+       /* XXX: Need to make sure that this is the same device as drmfd */
+       tempfd = drm_open_any();
+       close(tempfd);
+       ret = drmUnlock(drmfd, lock1);
+       if (ret != 0)
+               errx(1, "lock lost during open/close by same pid");
+}
+
+static void client()
+{
+       int drmfd, ret;
+       unsigned int time;
+
+       wait_event(0, SERVER_READY);
+
+       /* XXX: Should make sure we open the same DRM as the master */
+       drmfd = drm_open_any();
+
+       client_auth(drmfd);
+
+       /* Wait for the server to grab the lock, then grab it ourselves (to
+        * contest it).  Hopefully we hit it within the window of when the
+        * server locks.
+        */
+       wait_event(0, SERVER_LOCKED);
+       ret = drmGetLock(drmfd, lock2, 0);
+       time = get_millis();
+       if (ret != 0)
+               err(1, "Failed to get lock on client\n");
+       drmUnlock(drmfd, lock2);
+
+       /* Tell the server that our locking completed, and when it did */
+       send_event(0, CLIENT_LOCKED);
+       ret = write(commfd[0], &time, sizeof(time));
+
+       close(drmfd);
+       exit(0);
+}
+
+static void server()
+{
+       int drmfd, tempfd, ret;
+       unsigned int client_time, unlock_time;
+
+       drmfd = drm_open_any_master();
+
+       test_lock_unlock(drmfd);
+       test_unlock_unlocked(drmfd);
+       test_unlock_unowned(drmfd);
+       test_open_close_locked(drmfd);
+
+       /* Perform the authentication sequence with the client. */
+       server_auth(drmfd);
+
+       /* Now, test that the client attempting to lock while the server
+        * holds the lock works correctly.
+        */
+       ret = drmGetLock(drmfd, lock1, 0);
+       assert(ret == 0);
+       send_event(1, SERVER_LOCKED);
+       /* Wait a while for the client to do its thing */
+       sleep(1);
+       ret = drmUnlock(drmfd, lock1);
+       assert(ret == 0);
+       unlock_time = get_millis();
+
+       wait_event(1, CLIENT_LOCKED);
+       ret = read(commfd[1], &client_time, sizeof(client_time));
+       if (ret == -1)
+               err(1, "Failure to read client magic");
+
+       if (client_time < unlock_time)
+               errx(1, "Client took lock before server released it");
+
+       close(drmfd);
+}
+
+int main(int argc, char **argv)
+{
+       int ret;
+
+
+       ret = pipe(commfd);
+       if (ret == -1)
+               err(1, "Couldn't create pipe");
+
+       ret = fork();
+       if (ret == -1)
+               err(1, "failure to fork client");
+       if (ret == 0)
+               client();
+       else
+               server();
+
+       return 0;
+}
+
diff --git a/tests/modeprint/Makefile.am b/tests/modeprint/Makefile.am
new file mode 100644 (file)
index 0000000..c4862ac
--- /dev/null
@@ -0,0 +1,11 @@
+AM_CFLAGS = \
+       -I$(top_srcdir)/include/drm \
+       -I$(top_srcdir)
+
+noinst_PROGRAMS = \
+       modeprint
+
+modeprint_SOURCES = \
+       modeprint.c
+modeprint_LDADD = \
+       $(top_builddir)/libdrm.la
diff --git a/tests/modeprint/modeprint.c b/tests/modeprint/modeprint.c
new file mode 100644 (file)
index 0000000..545ff40
--- /dev/null
@@ -0,0 +1,403 @@
+/*
+ * \file modedemo.c
+ * Test program to dump DRM kernel mode setting related information.
+ * Queries the kernel for all available information and dumps it to stdout.
+ *
+ * \author Jakob Bornecrantz <wallbraker@gmail.com>
+ */
+
+/*
+ * Copyright (c) 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas.
+ * Copyright (c) 2007-2008 Jakob Bornecrantz <wallbraker@gmail.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ */
+
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <unistd.h>
+#include <string.h>
+#include <inttypes.h>
+
+#include "xf86drm.h"
+#include "xf86drmMode.h"
+
+int connectors;
+int full_props;
+int edid;
+int modes;
+int full_modes;
+int encoders;
+int crtcs;
+int fbs;
+char *module_name;
+
+const char* getConnectionText(drmModeConnection conn)
+{
+       switch (conn) {
+       case DRM_MODE_CONNECTED:
+               return "connected";
+       case DRM_MODE_DISCONNECTED:
+               return "disconnected";
+       default:
+               return "unknown";
+       }
+
+}
+
+int printMode(struct drm_mode_modeinfo *mode)
+{
+       if (full_modes) {
+               printf("Mode: %s\n", mode->name);
+               printf("\tclock       : %i\n", mode->clock);
+               printf("\thdisplay    : %i\n", mode->hdisplay);
+               printf("\thsync_start : %i\n", mode->hsync_start);
+               printf("\thsync_end   : %i\n", mode->hsync_end);
+               printf("\thtotal      : %i\n", mode->htotal);
+               printf("\thskew       : %i\n", mode->hskew);
+               printf("\tvdisplay    : %i\n", mode->vdisplay);
+               printf("\tvsync_start : %i\n", mode->vsync_start);
+               printf("\tvsync_end   : %i\n", mode->vsync_end);
+               printf("\tvtotal      : %i\n", mode->vtotal);
+               printf("\tvscan       : %i\n", mode->vscan);
+               printf("\tvrefresh    : %i\n", mode->vrefresh);
+               printf("\tflags       : %i\n", mode->flags);
+       } else {
+               printf("Mode: \"%s\" %ix%i %i\n", mode->name,
+                               mode->hdisplay, mode->vdisplay, mode->vrefresh);
+       }
+       return 0;
+}
+
+int printProperty(int fd, drmModeResPtr res, drmModePropertyPtr props, uint64_t value)
+{
+       const char *name = NULL;
+       int j;
+
+       printf("Property: %s\n", props->name);
+       printf("\tid           : %i\n", props->prop_id);
+       printf("\tflags        : %i\n", props->flags);
+       printf("\tcount_values : %d\n", props->count_values);
+
+
+       if (props->count_values) {
+               printf("\tvalues       :");
+               for (j = 0; j < props->count_values; j++)
+                       printf(" %" PRIu64, props->values[j]);
+               printf("\n");
+       }
+
+
+       printf("\tcount_enums  : %d\n", props->count_enums);
+
+       if (props->flags & DRM_MODE_PROP_BLOB) {
+               drmModePropertyBlobPtr blob;
+
+               blob = drmModeGetPropertyBlob(fd, value);
+               if (blob) {
+                       printf("blob is %d length, %08X\n", blob->length, *(uint32_t *)blob->data);
+                       drmModeFreePropertyBlob(blob);
+               } else {
+                       printf("error getting blob %" PRIu64 "\n", value);
+               }
+
+       } else {
+               if (!strncmp(props->name, "DPMS", 4))
+                       ;
+
+               for (j = 0; j < props->count_enums; j++) {
+                       printf("\t\t%lld = %s\n", props->enums[j].value, props->enums[j].name);
+                       if (props->enums[j].value == value)
+                               name = props->enums[j].name;
+               }
+
+               if (props->count_enums && name) {
+                       printf("\tcon_value    : %s\n", name);
+               } else {
+                       printf("\tcon_value    : %" PRIu64 "\n", value);
+               }
+       }
+
+       return 0;
+}
+
+int printConnector(int fd, drmModeResPtr res, drmModeConnectorPtr connector, uint32_t id)
+{
+       int i = 0;
+       struct drm_mode_modeinfo *mode = NULL;
+       drmModePropertyPtr props;
+
+       printf("Connector: %d-%d\n", connector->connector_type, connector->connector_type_id);
+       printf("\tid             : %i\n", id);
+       printf("\tencoder id     : %i\n", connector->encoder_id);
+       printf("\tconn           : %s\n", getConnectionText(connector->connection));
+       printf("\tsize           : %ix%i (mm)\n", connector->mmWidth, connector->mmHeight);
+       printf("\tcount_modes    : %i\n", connector->count_modes);
+       printf("\tcount_props    : %i\n", connector->count_props);
+       if (connector->count_props) {
+               printf("\tprops          :");
+               for (i = 0; i < connector->count_props; i++)
+                       printf(" %i", connector->props[i]);
+               printf("\n");
+       }
+
+       printf("\tcount_encoders : %i\n", connector->count_encoders);
+       if (connector->count_encoders) {
+               printf("\tencoders       :");
+               for (i = 0; i < connector->count_encoders; i++)
+                       printf(" %i", connector->encoders[i]);
+               printf("\n");
+       }
+
+       if (modes) {
+               for (i = 0; i < connector->count_modes; i++) {
+                       mode = (struct drm_mode_modeinfo *)&connector->modes[i];
+                       printMode(mode);
+               }
+       }
+
+       if (full_props) {
+               for (i = 0; i < connector->count_props; i++) {
+                       props = drmModeGetProperty(fd, connector->props[i]);
+                       if (props) {
+                               printProperty(fd, res, props, connector->prop_values[i]);
+                               drmModeFreeProperty(props);
+                       }
+               }
+       }
+
+       return 0;
+}
+
+int printEncoder(int fd, drmModeResPtr res, drmModeEncoderPtr encoder, uint32_t id)
+{
+       printf("Encoder\n");
+       printf("\tid     :%i\n", id);
+       printf("\tcrtc_id   :%d\n", encoder->crtc_id);
+       printf("\ttype   :%d\n", encoder->encoder_type);
+       printf("\tpossible_crtcs  :0x%x\n", encoder->possible_crtcs);
+       printf("\tpossible_clones :0x%x\n", encoder->possible_clones);
+       return 0;
+}
+
+int printCrtc(int fd, drmModeResPtr res, drmModeCrtcPtr crtc, uint32_t id)
+{
+       printf("Crtc\n");
+       printf("\tid             : %i\n", id);
+       printf("\tx              : %i\n", crtc->x);
+       printf("\ty              : %i\n", crtc->y);
+       printf("\twidth          : %i\n", crtc->width);
+       printf("\theight         : %i\n", crtc->height);
+       printf("\tmode           : %p\n", &crtc->mode);
+       printf("\tgamma size     : %d\n", crtc->gamma_size);
+
+       return 0;
+}
+
+int printFrameBuffer(int fd, drmModeResPtr res, drmModeFBPtr fb)
+{
+       printf("Framebuffer\n");
+       printf("\thandle    : %i\n", fb->handle);
+       printf("\twidth     : %i\n", fb->width);
+       printf("\theight    : %i\n", fb->height);
+       printf("\tpitch     : %i\n", fb->pitch);;
+       printf("\tbpp       : %i\n", fb->bpp);
+       printf("\tdepth     : %i\n", fb->depth);
+       printf("\tbuffer_id : %i\n", fb->handle);
+
+       return 0;
+}
+
+int printRes(int fd, drmModeResPtr res)
+{
+       int i;
+       drmModeFBPtr fb;
+       drmModeCrtcPtr crtc;
+       drmModeEncoderPtr encoder;
+       drmModeConnectorPtr connector;
+
+       printf("Resources\n\n");
+
+       printf("count_connectors : %i\n", res->count_connectors);
+       printf("count_encoders   : %i\n", res->count_encoders);
+       printf("count_crtcs      : %i\n", res->count_crtcs);
+       printf("count_fbs        : %i\n", res->count_fbs);
+
+       printf("\n");
+
+       if (connectors) {
+               for (i = 0; i < res->count_connectors; i++) {
+                       connector = drmModeGetConnector(fd, res->connectors[i]);
+
+                       if (!connector)
+                               printf("Could not get connector %i\n", res->connectors[i]);
+                       else {
+                               printConnector(fd, res, connector, res->connectors[i]);
+                               drmModeFreeConnector(connector);
+                       }
+               }
+               printf("\n");
+       }
+
+
+       if (encoders) {
+               for (i = 0; i < res->count_encoders; i++) {
+                       encoder = drmModeGetEncoder(fd, res->encoders[i]);
+
+                       if (!encoder)
+                               printf("Could not get encoder %i\n", res->encoders[i]);
+                       else {
+                               printEncoder(fd, res, encoder, res->encoders[i]);
+                               drmModeFreeEncoder(encoder);
+                       }
+               }
+               printf("\n");
+       }
+
+       if (crtcs) {
+               for (i = 0; i < res->count_crtcs; i++) {
+                       crtc = drmModeGetCrtc(fd, res->crtcs[i]);
+
+                       if (!crtc)
+                               printf("Could not get crtc %i\n", res->crtcs[i]);
+                       else {
+                               printCrtc(fd, res, crtc, res->crtcs[i]);
+                               drmModeFreeCrtc(crtc);
+                       }
+               }
+               printf("\n");
+       }
+
+       if (fbs) {
+               for (i = 0; i < res->count_fbs; i++) {
+                       fb = drmModeGetFB(fd, res->fbs[i]);
+
+                       if (!fb)
+                               printf("Could not get fb %i\n", res->fbs[i]);
+                       else {
+                               printFrameBuffer(fd, res, fb);
+                               drmModeFreeFB(fb);
+                       }
+               }
+       }
+
+       return 0;
+}
+
+void args(int argc, char **argv)
+{
+       int i;
+
+       fbs = 0;
+       edid = 0;
+       crtcs = 0;
+       modes = 0;
+       encoders = 0;
+       full_modes = 0;
+       full_props = 0;
+       connectors = 0;
+
+       module_name = argv[1];
+
+       for (i = 2; i < argc; i++) {
+               if (strcmp(argv[i], "-fb") == 0) {
+                       fbs = 1;
+               } else if (strcmp(argv[i], "-crtcs") == 0) {
+                       crtcs = 1;
+               } else if (strcmp(argv[i], "-cons") == 0) {
+                       connectors = 1;
+                       modes = 1;
+               } else if (strcmp(argv[i], "-modes") == 0) {
+                       connectors = 1;
+                       modes = 1;
+               } else if (strcmp(argv[i], "-full") == 0) {
+                       connectors = 1;
+                       modes = 1;
+                       full_modes = 1;
+               } else if (strcmp(argv[i], "-props") == 0) {
+                       connectors = 1;
+                       full_props = 1;
+               } else if (strcmp(argv[i], "-edids") == 0) {
+                       connectors = 1;
+                       edid = 1;
+               } else if (strcmp(argv[i], "-encoders") == 0) {
+                       encoders = 1;
+               } else if (strcmp(argv[i], "-v") == 0) {
+                       fbs = 1;
+                       edid = 1;
+                       crtcs = 1;
+                       modes = 1;
+                       encoders = 1;
+                       full_modes = 1;
+                       full_props = 1;
+                       connectors = 1;
+               }
+       }
+
+       if (argc == 2) {
+               fbs = 1;
+               edid = 1;
+               crtcs = 1;
+               modes = 1;
+               encoders = 1;
+               full_modes = 0;
+               full_props = 0;
+               connectors = 1;
+       }
+}
+
+int main(int argc, char **argv)
+{
+       int fd;
+       drmModeResPtr res;
+
+       if (argc == 1) {
+               printf("Please add modulename as first argument\n");
+               return 1;
+       }
+
+       args(argc, argv);
+
+       printf("Starting test\n");
+
+       fd = drmOpen(module_name, NULL);
+
+       if (fd < 0) {
+               printf("Failed to open the card fd (%d)\n",fd);
+               return 1;
+       }
+
+       res = drmModeGetResources(fd);
+       if (res == 0) {
+               printf("Failed to get resources from card\n");
+               drmClose(fd);
+               return 1;
+       }
+
+       printRes(fd, res);
+
+       drmModeFreeResources(res);
+
+       printf("Ok\n");
+
+       return 0;
+}
diff --git a/tests/modetest/Makefile.am b/tests/modetest/Makefile.am
new file mode 100644 (file)
index 0000000..ea39685
--- /dev/null
@@ -0,0 +1,17 @@
+AM_CFLAGS = \
+       -I$(top_srcdir)/include/drm \
+       -I$(top_srcdir)/libkms/ \
+       -I$(top_srcdir)/slp/ \
+       -I$(top_srcdir) \
+       $(CAIRO_CFLAGS)
+
+noinst_PROGRAMS = \
+       modetest
+
+modetest_SOURCES = \
+       modetest.c
+modetest_LDADD = \
+       $(top_builddir)/libdrm.la \
+       $(top_builddir)/libkms/libkms.la \
+       $(top_builddir)/slp/libdrm_slp.la \
+       $(CAIRO_LIBS)
diff --git a/tests/modetest/modetest.c b/tests/modetest/modetest.c
new file mode 100755 (executable)
index 0000000..0c49ad7
--- /dev/null
@@ -0,0 +1,864 @@
+/*
+ * DRM based mode setting test program
+ * Copyright 2008 Tungsten Graphics
+ *   Jakob Bornecrantz <jakob@tungstengraphics.com>
+ * Copyright 2008 Intel Corporation
+ *   Jesse Barnes <jesse.barnes@intel.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+/*
+ * This fairly simple test program dumps output in a similar format to the
+ * "xrandr" tool everyone knows & loves.  It's necessarily slightly different
+ * since the kernel separates outputs into encoder and connector structures,
+ * each with their own unique ID.  The program also allows test testing of the
+ * memory management and mode setting APIs by allowing the user to specify a
+ * connector and mode to use for mode setting.  If all works as expected, a
+ * blue background should be painted on the monitor attached to the specified
+ * connector after the selected mode is set.
+ *
+ * TODO: use cairo to write the mode info on the selected output once
+ *       the mode has been programmed, along with possible test patterns.
+ */
+#include "config.h"
+
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/poll.h>
+#include <sys/time.h>
+
+#include "xf86drm.h"
+#include "xf86drmMode.h"
+#include "libkms.h"
+
+#define TEST_SLP
+#ifdef TEST_SLP
+#include "drm_slp_bufmgr.h"
+#include "exynos_drm.h"
+
+static drm_slp_bufmgr slp_bufmgr=NULL;
+#endif
+
+#ifdef HAVE_CAIRO
+#include <math.h>
+#include <cairo.h>
+#endif
+
+drmModeRes *resources;
+int fd, modes;
+
+#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
+
+struct type_name {
+       int type;
+       char *name;
+};
+
+#define type_name_fn(res) \
+char * res##_str(int type) {                   \
+       int i;                                          \
+       for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \
+               if (res##_names[i].type == type)        \
+                       return res##_names[i].name;     \
+       }                                               \
+       return "(invalid)";                             \
+}
+
+struct type_name encoder_type_names[] = {
+       { DRM_MODE_ENCODER_NONE, "none" },
+       { DRM_MODE_ENCODER_DAC, "DAC" },
+       { DRM_MODE_ENCODER_TMDS, "TMDS" },
+       { DRM_MODE_ENCODER_LVDS, "LVDS" },
+       { DRM_MODE_ENCODER_TVDAC, "TVDAC" },
+};
+
+type_name_fn(encoder_type)
+
+struct type_name connector_status_names[] = {
+       { DRM_MODE_CONNECTED, "connected" },
+       { DRM_MODE_DISCONNECTED, "disconnected" },
+       { DRM_MODE_UNKNOWNCONNECTION, "unknown" },
+};
+
+type_name_fn(connector_status)
+
+struct type_name connector_type_names[] = {
+       { DRM_MODE_CONNECTOR_Unknown, "unknown" },
+       { DRM_MODE_CONNECTOR_VGA, "VGA" },
+       { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
+       { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
+       { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
+       { DRM_MODE_CONNECTOR_Composite, "composite" },
+       { DRM_MODE_CONNECTOR_SVIDEO, "s-video" },
+       { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
+       { DRM_MODE_CONNECTOR_Component, "component" },
+       { DRM_MODE_CONNECTOR_9PinDIN, "9-pin DIN" },
+       { DRM_MODE_CONNECTOR_DisplayPort, "displayport" },
+       { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
+       { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
+       { DRM_MODE_CONNECTOR_TV, "TV" },
+       { DRM_MODE_CONNECTOR_eDP, "embedded displayport" },
+};
+
+type_name_fn(connector_type)
+
+void dump_encoders(void)
+{
+       drmModeEncoder *encoder;
+       int i;
+
+       printf("Encoders:\n");
+       printf("id\tcrtc\ttype\tpossible crtcs\tpossible clones\t\n");
+       for (i = 0; i < resources->count_encoders; i++) {
+               encoder = drmModeGetEncoder(fd, resources->encoders[i]);
+
+               if (!encoder) {
+                       fprintf(stderr, "could not get encoder %i: %s\n",
+                               resources->encoders[i], strerror(errno));
+                       continue;
+               }
+               printf("%d\t%d\t%s\t0x%08x\t0x%08x\n",
+                      encoder->encoder_id,
+                      encoder->crtc_id,
+                      encoder_type_str(encoder->encoder_type),
+                      encoder->possible_crtcs,
+                      encoder->possible_clones);
+               drmModeFreeEncoder(encoder);
+       }
+       printf("\n");
+}
+
+void dump_mode(drmModeModeInfo *mode)
+{
+       printf("  %s %d %d %d %d %d %d %d %d %d\n",
+              mode->name,
+              mode->vrefresh,
+              mode->hdisplay,
+              mode->hsync_start,
+              mode->hsync_end,
+              mode->htotal,
+              mode->vdisplay,
+              mode->vsync_start,
+              mode->vsync_end,
+              mode->vtotal);
+}
+
+static void
+dump_props(drmModeConnector *connector)
+{
+       drmModePropertyPtr props;
+       int i;
+
+       for (i = 0; i < connector->count_props; i++) {
+               props = drmModeGetProperty(fd, connector->props[i]);
+               printf("\t%s, flags %d\n", props->name, props->flags);
+               drmModeFreeProperty(props);
+       }
+}
+
+void dump_connectors(void)
+{
+       drmModeConnector *connector;
+       int i, j;
+
+       printf("Connectors:\n");
+       printf("id\tencoder\tstatus\t\ttype\tsize (mm)\tmodes\tencoders\n");
+       for (i = 0; i < resources->count_connectors; i++) {
+               connector = drmModeGetConnector(fd, resources->connectors[i]);
+
+               if (!connector) {
+                       fprintf(stderr, "could not get connector %i: %s\n",
+                               resources->connectors[i], strerror(errno));
+                       continue;
+               }
+
+               printf("%d\t%d\t%s\t%s\t%dx%d\t\t%d\t",
+                      connector->connector_id,
+                      connector->encoder_id,
+                      connector_status_str(connector->connection),
+                      connector_type_str(connector->connector_type),
+                      connector->mmWidth, connector->mmHeight,
+                      connector->count_modes);
+
+               for (j = 0; j < connector->count_encoders; j++)
+                       printf("%s%d", j > 0 ? ", " : "", connector->encoders[j]);
+               printf("\n");
+
+               if (!connector->count_modes)
+                       continue;
+
+               printf("  modes:\n");
+               printf("  name refresh (Hz) hdisp hss hse htot vdisp "
+                      "vss vse vtot)\n");
+               for (j = 0; j < connector->count_modes; j++)
+                       dump_mode(&connector->modes[j]);
+
+               printf("  props:\n");
+               dump_props(connector);
+
+               drmModeFreeConnector(connector);
+       }
+       printf("\n");
+}
+
+void dump_crtcs(void)
+{
+       drmModeCrtc *crtc;
+       int i;
+
+       printf("CRTCs:\n");
+       printf("id\tfb\tpos\tsize\n");
+       for (i = 0; i < resources->count_crtcs; i++) {
+               crtc = drmModeGetCrtc(fd, resources->crtcs[i]);
+
+               if (!crtc) {
+                       fprintf(stderr, "could not get crtc %i: %s\n",
+                               resources->crtcs[i], strerror(errno));
+                       continue;
+               }
+               printf("%d\t%d\t(%d,%d)\t(%dx%d)\n",
+                      crtc->crtc_id,
+                      crtc->buffer_id,
+                      crtc->x, crtc->y,
+                      crtc->width, crtc->height);
+               dump_mode(&crtc->mode);
+
+               drmModeFreeCrtc(crtc);
+       }
+       printf("\n");
+}
+
+void dump_framebuffers(void)
+{
+       drmModeFB *fb;
+       int i;
+
+       printf("Frame buffers:\n");
+       printf("id\tsize\tpitch\n");
+       for (i = 0; i < resources->count_fbs; i++) {
+               fb = drmModeGetFB(fd, resources->fbs[i]);
+
+               if (!fb) {
+                       fprintf(stderr, "could not get fb %i: %s\n",
+                               resources->fbs[i], strerror(errno));
+                       continue;
+               }
+               printf("%u\t(%ux%u)\t%u\n",
+                      fb->fb_id,
+                      fb->width, fb->height,
+                      fb->pitch);
+
+               drmModeFreeFB(fb);
+       }
+       printf("\n");
+}
+
+/*
+ * Mode setting with the kernel interfaces is a bit of a chore.
+ * First you have to find the connector in question and make sure the
+ * requested mode is available.
+ * Then you need to find the encoder attached to that connector so you
+ * can bind it with a free crtc.
+ */
+struct connector {
+       uint32_t id;
+       char mode_str[64];
+       drmModeModeInfo *mode;
+       drmModeEncoder *encoder;
+       int crtc;
+       unsigned int fb_id[2], current_fb_id;
+       struct timeval start;
+
+       int swap_count;
+};     
+
+static void
+connector_find_mode(struct connector *c)
+{
+       drmModeConnector *connector;
+       int i, j;
+
+       /* First, find the connector & mode */
+       c->mode = NULL;
+       for (i = 0; i < resources->count_connectors; i++) {
+               connector = drmModeGetConnector(fd, resources->connectors[i]);
+
+               if (!connector) {
+                       fprintf(stderr, "could not get connector %i: %s\n",
+                               resources->connectors[i], strerror(errno));
+                       drmModeFreeConnector(connector);
+                       continue;
+               }
+
+               if (!connector->count_modes) {
+                       drmModeFreeConnector(connector);
+                       continue;
+               }
+
+               if (connector->connector_id != c->id) {
+                       drmModeFreeConnector(connector);
+                       continue;
+               }
+
+               for (j = 0; j < connector->count_modes; j++) {
+                       c->mode = &connector->modes[j];
+                       if (!strcmp(c->mode->name, c->mode_str))
+                               break;
+               }
+
+               /* Found it, break out */
+               if (c->mode)
+                       break;
+
+               drmModeFreeConnector(connector);
+       }
+
+       if (!c->mode) {
+               fprintf(stderr, "failed to find mode \"%s\"\n", c->mode_str);
+               return;
+       }
+
+       /* Now get the encoder */
+       for (i = 0; i < resources->count_encoders; i++) {
+               c->encoder = drmModeGetEncoder(fd, resources->encoders[i]);
+
+               if (!c->encoder) {
+                       fprintf(stderr, "could not get encoder %i: %s\n",
+                               resources->encoders[i], strerror(errno));
+                       drmModeFreeEncoder(c->encoder);
+                       continue;
+               }
+
+               if (c->encoder->encoder_id  == connector->encoder_id)
+                       break;
+
+               drmModeFreeEncoder(c->encoder);
+       }
+
+       if (c->crtc == -1)
+               c->crtc = c->encoder->crtc_id;
+}
+
+static struct kms_bo *
+allocate_buffer(struct kms_driver *kms,
+               int width, int height, int *stride)
+{
+       struct kms_bo *bo;
+       unsigned bo_attribs[] = {
+               KMS_WIDTH,   0,
+               KMS_HEIGHT,  0,
+               KMS_BO_TYPE, KMS_BO_TYPE_SCANOUT_X8R8G8B8,
+               KMS_TERMINATE_PROP_LIST
+       };
+       int ret;
+
+       bo_attribs[1] = width;
+       bo_attribs[3] = height;
+
+       ret = kms_bo_create(kms, bo_attribs, &bo);
+       if (ret) {
+               fprintf(stderr, "failed to alloc buffer: %s\n",
+                       strerror(-ret));
+               return NULL;
+       }
+
+       ret = kms_bo_get_prop(bo, KMS_PITCH, stride);
+       if (ret) {
+               fprintf(stderr, "failed to retreive buffer stride: %s\n",
+                       strerror(-ret));
+               kms_bo_destroy(&bo);
+               return NULL;
+       }
+
+       return bo;
+}
+
+static void
+make_pwetty(void *data, int width, int height, int stride)
+{
+#ifdef HAVE_CAIRO
+       cairo_surface_t *surface;
+       cairo_t *cr;
+       int x, y;
+
+       surface = cairo_image_surface_create_for_data(data,
+                                                     CAIRO_FORMAT_ARGB32,
+                                                     width, height,
+                                                     stride);
+       cr = cairo_create(surface);
+       cairo_surface_destroy(surface);
+
+       cairo_set_line_cap(cr, CAIRO_LINE_CAP_SQUARE);
+       for (x = 0; x < width; x += 250)
+               for (y = 0; y < height; y += 250) {
+                       char buf[64];
+
+                       cairo_move_to(cr, x, y - 20);
+                       cairo_line_to(cr, x, y + 20);
+                       cairo_move_to(cr, x - 20, y);
+                       cairo_line_to(cr, x + 20, y);
+                       cairo_new_sub_path(cr);
+                       cairo_arc(cr, x, y, 10, 0, M_PI * 2);
+                       cairo_set_line_width(cr, 4);
+                       cairo_set_source_rgb(cr, 0, 0, 0);
+                       cairo_stroke_preserve(cr);
+                       cairo_set_source_rgb(cr, 1, 1, 1);
+                       cairo_set_line_width(cr, 2);
+                       cairo_stroke(cr);
+
+                       snprintf(buf, sizeof buf, "%d, %d", x, y);
+                       cairo_move_to(cr, x + 20, y + 20);
+                       cairo_text_path(cr, buf);
+                       cairo_set_source_rgb(cr, 0, 0, 0);
+                       cairo_stroke_preserve(cr);
+                       cairo_set_source_rgb(cr, 1, 1, 1);
+                       cairo_fill(cr);
+               }
+
+       cairo_destroy(cr);
+#endif
+}
+
+#ifdef TEST_SLP
+static drm_slp_bo
+make_drmbuf_from_kms(struct kms_bo* bo)
+{
+       unsigned int hKms;
+       drm_slp_bo drmBo = NULL;
+
+       kms_bo_get_prop(bo, KMS_HANDLE, &hKms);
+       drmBo = drm_slp_bo_attach(slp_bufmgr, NULL, NULL, 0, &hKms);
+       if(!drmBo)
+       {
+               fprintf(stderr, "Fail attach\n");
+               return drmBo;
+       }
+
+       fprintf(stderr, "DRM_BO(%p) from KMS(%p, %d)\n", drmBo, bo, hKms);
+       return drmBo;
+}
+#endif
+
+static int
+create_test_buffer(struct kms_driver *kms,
+                  int width, int height, int *stride_out,
+                  struct kms_bo **bo_out)
+{
+       struct kms_bo *bo;
+       int ret, i, j, stride;
+       void *virtual;
+#ifdef TEST_SLP
+       drm_slp_bo drmBo;
+#endif
+
+
+       bo = allocate_buffer(kms, width, height, &stride);
+       if (!bo)
+               return -1;
+
+#ifdef TEST_SLP
+       drmBo = make_drmbuf_from_kms(bo);
+       virtual = drm_slp_bo_map(drmBo, DRM_SLP_DEVICE_CPU, DRM_SLP_OPTION_READ|DRM_SLP_OPTION_WRITE);
+#else
+       ret = kms_bo_map(bo, &virtual);
+       if (ret) {
+               fprintf(stderr, "failed to map buffer: %s\n",
+                       strerror(-ret));
+               kms_bo_destroy(&bo);
+               return -1;
+       }
+#endif
+
+       /* paint the buffer with colored tiles */
+       for (j = 0; j < height; j++) {
+               uint32_t *fb_ptr = (uint32_t*)((char*)virtual + j * stride);
+               for (i = 0; i < width; i++) {
+                       div_t d = div(i, width);
+                       fb_ptr[i] =
+                               0x00130502 * (d.quot >> 6) +
+                               0x000a1120 * (d.rem >> 6);
+               }
+       }
+
+       make_pwetty(virtual, width, height, stride);
+
+#ifdef TEST_SLP
+       drm_slp_bo_unmap(drmBo, DRM_SLP_DEVICE_CPU);
+#else
+       kms_bo_unmap(bo);
+#endif
+
+       *bo_out = bo;
+       *stride_out = stride;
+       return 0;
+}
+
+static int
+create_grey_buffer(struct kms_driver *kms,
+                  int width, int height, int *stride_out,
+                  struct kms_bo **bo_out)
+{
+       struct kms_bo *bo;
+       int size, ret, stride;
+       void *virtual;
+
+       bo = allocate_buffer(kms, width, height, &stride);
+       if (!bo)
+               return -1;
+
+       ret = kms_bo_map(bo, &virtual);
+       if (ret) {
+               fprintf(stderr, "failed to map buffer: %s\n",
+                       strerror(-ret));
+               kms_bo_destroy(&bo);
+               return -1;
+       }
+
+       size = stride * height;
+       memset(virtual, 0x77, size);
+       kms_bo_unmap(bo);
+
+       *bo_out = bo;
+       *stride_out = stride;
+
+       return 0;
+}
+
+void
+page_flip_handler(int fd, unsigned int frame,
+                 unsigned int sec, unsigned int usec, void *data)
+{
+       struct connector *c;
+       unsigned int new_fb_id;
+       struct timeval end;
+       double t;
+
+       c = data;
+       if (c->current_fb_id == c->fb_id[0])
+               new_fb_id = c->fb_id[1];
+       else
+               new_fb_id = c->fb_id[0];
+                       
+       drmModePageFlip(fd, c->crtc, new_fb_id,
+                       DRM_MODE_PAGE_FLIP_EVENT, c);
+       c->current_fb_id = new_fb_id;
+       c->swap_count++;
+       if (c->swap_count == 60) {
+               gettimeofday(&end, NULL);
+               t = end.tv_sec + end.tv_usec * 1e-6 -
+                       (c->start.tv_sec + c->start.tv_usec * 1e-6);
+               fprintf(stderr, "freq: %.02fHz\n", c->swap_count / t);
+               c->swap_count = 0;
+               c->start = end;
+       }
+}
+
+static void
+set_mode(struct connector *c, int count, int page_flip)
+{
+       struct kms_driver *kms;
+       struct kms_bo *bo, *other_bo;
+       unsigned int fb_id, other_fb_id;
+       int i, ret, width, height, x, stride;
+       unsigned handle;
+       drmEventContext evctx;
+
+       width = 0;
+       height = 0;
+       for (i = 0; i < count; i++) {
+               connector_find_mode(&c[i]);
+               if (c[i].mode == NULL)
+                       continue;
+               width += c[i].mode->hdisplay;
+               if (height < c[i].mode->vdisplay)
+                       height = c[i].mode->vdisplay;
+       }
+
+       ret = kms_create(fd, &kms);
+       if (ret) {
+               fprintf(stderr, "failed to create kms driver: %s\n",
+                       strerror(-ret));
+               return;
+       }
+
+#ifdef TEST_SLP
+       slp_bufmgr = drm_slp_bufmgr_init(fd, NULL);
+       if(!slp_bufmgr)
+       {
+               fprintf(stderr, "failed to make slp_bufmgr\n");
+               return;
+       }
+#endif
+
+       if (create_test_buffer(kms, width, height, &stride, &bo))
+               return;
+
+       kms_bo_get_prop(bo, KMS_HANDLE, &handle);
+       ret = drmModeAddFB(fd, width, height, 24, 32, stride, handle, &fb_id);
+       if (ret) {
+               fprintf(stderr, "failed to add fb (%ux%u): %s\n",
+                       width, height, strerror(errno));
+               return;
+       }
+
+       x = 0;
+       for (i = 0; i < count; i++) {
+               if (c[i].mode == NULL)
+                       continue;
+
+               printf("setting mode %s on connector %d, crtc %d\n",
+                      c[i].mode_str, c[i].id, c[i].crtc);
+
+               ret = drmModeSetCrtc(fd, c[i].crtc, fb_id, x, 0,
+                                    &c[i].id, 1, c[i].mode);
+
+               /* XXX: Actually check if this is needed */
+               drmModeDirtyFB(fd, fb_id, NULL, 0);
+
+               x += c[i].mode->hdisplay;
+
+               if (ret) {
+                       fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
+                       return;
+               }
+       }
+
+       if (!page_flip)
+               return;
+       
+       if (create_grey_buffer(kms, width, height, &stride, &other_bo))
+               return;
+
+       kms_bo_get_prop(other_bo, KMS_HANDLE, &handle);
+       ret = drmModeAddFB(fd, width, height, 32, 32, stride, handle,
+                          &other_fb_id);
+       if (ret) {
+               fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
+               return;
+       }
+
+       for (i = 0; i < count; i++) {
+               if (c[i].mode == NULL)
+                       continue;
+
+               ret = drmModePageFlip(fd, c[i].crtc, other_fb_id,
+                                     DRM_MODE_PAGE_FLIP_EVENT, &c[i]);
+               if (ret) {
+                       fprintf(stderr, "failed to page flip: %s\n", strerror(errno));
+                       return;
+               }
+               gettimeofday(&c[i].start, NULL);
+               c[i].swap_count = 0;
+               c[i].fb_id[0] = fb_id;
+               c[i].fb_id[1] = other_fb_id;
+               c[i].current_fb_id = other_fb_id;
+       }
+
+       memset(&evctx, 0, sizeof evctx);
+       evctx.version = DRM_EVENT_CONTEXT_VERSION;
+       evctx.vblank_handler = NULL;
+       evctx.page_flip_handler = page_flip_handler;
+       
+       while (1) {
+#if 0
+               struct pollfd pfd[2];
+
+               pfd[0].fd = 0;
+               pfd[0].events = POLLIN;
+               pfd[1].fd = fd;
+               pfd[1].events = POLLIN;
+
+               if (poll(pfd, 2, -1) < 0) {
+                       fprintf(stderr, "poll error\n");
+                       break;
+               }
+
+               if (pfd[0].revents)
+                       break;
+#else
+               struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
+               fd_set fds;
+               int ret;
+
+               FD_ZERO(&fds);
+               FD_SET(0, &fds);
+               FD_SET(fd, &fds);
+               ret = select(fd + 1, &fds, NULL, NULL, &timeout);
+
+               if (ret <= 0) {
+                       fprintf(stderr, "select timed out or error (ret %d)\n",
+                               ret);
+                       continue;
+               } else if (FD_ISSET(0, &fds)) {
+                       break;
+               }
+#endif
+
+               drmHandleEvent(fd, &evctx);
+       }
+
+       kms_bo_destroy(&bo);
+       kms_bo_destroy(&other_bo);
+       kms_destroy(&kms);
+}
+
+extern char *optarg;
+extern int optind, opterr, optopt;
+static char optstr[] = "ecpmfs:v";
+
+void usage(char *name)
+{
+       fprintf(stderr, "usage: %s [-ecpmf]\n", name);
+       fprintf(stderr, "\t-e\tlist encoders\n");
+       fprintf(stderr, "\t-c\tlist connectors\n");
+       fprintf(stderr, "\t-p\tlist CRTCs (pipes)\n");
+       fprintf(stderr, "\t-m\tlist modes\n");
+       fprintf(stderr, "\t-f\tlist framebuffers\n");
+       fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
+       fprintf(stderr, "\t-s <connector_id>:<mode>\tset a mode\n");
+       fprintf(stderr, "\t-s <connector_id>@<crtc_id>:<mode>\tset a mode\n");
+       fprintf(stderr, "\n\tDefault is to dump all info.\n");
+       exit(0);
+}
+
+#define dump_resource(res) if (res) dump_##res()
+
+static int page_flipping_supported(int fd)
+{
+       /*FIXME: generic ioctl needed? */
+       return 1;
+#if 0
+       int ret, value;
+       struct drm_i915_getparam gp;
+
+       gp.param = I915_PARAM_HAS_PAGEFLIPPING;
+       gp.value = &value;
+
+       ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
+       if (ret) {
+               fprintf(stderr, "drm_i915_getparam: %m\n");
+               return 0;
+       }
+
+       return *gp.value;
+#endif
+}
+
+int main(int argc, char **argv)
+{
+       int c;
+       int encoders = 0, connectors = 0, crtcs = 0, framebuffers = 0;
+       int test_vsync = 0;
+       char *modules[] = { "i915", "radeon", "nouveau", "vmwgfx", "exynos-drm" };
+       char *modeset = NULL;
+       int i, count = 0;
+       struct connector con_args[2];
+       
+       opterr = 0;
+       while ((c = getopt(argc, argv, optstr)) != -1) {
+               switch (c) {
+               case 'e':
+                       encoders = 1;
+                       break;
+               case 'c':
+                       connectors = 1;
+                       break;
+               case 'p':
+                       crtcs = 1;
+                       break;
+               case 'm':
+                       modes = 1;
+                       break;
+               case 'f':
+                       framebuffers = 1;
+                       break;
+               case 'v':
+                       test_vsync = 1;
+                       break;
+               case 's':
+                       modeset = strdup(optarg);
+                       con_args[count].crtc = -1;
+                       if (sscanf(optarg, "%d:%64s",
+                                  &con_args[count].id,
+                                  con_args[count].mode_str) != 2 &&
+                           sscanf(optarg, "%d@%d:%64s",
+                                  &con_args[count].id,
+                                  &con_args[count].crtc,
+                                  con_args[count].mode_str) != 3)
+                               usage(argv[0]);
+                       count++;                                      
+                       break;
+               default:
+                       usage(argv[0]);
+                       break;
+               }
+       }
+
+       if (argc == 1)
+               encoders = connectors = crtcs = modes = framebuffers = 1;
+
+       for (i = 0; i < ARRAY_SIZE(modules); i++) {
+               printf("trying to load module %s...", modules[i]);
+               fd = drmOpen(modules[i], NULL);
+               if (fd < 0) {
+                       printf("failed.\n");
+               } else {
+                       printf("success.\n");
+                       break;
+               }
+       }
+
+       if (test_vsync && !page_flipping_supported(fd)) {
+               fprintf(stderr, "page flipping not supported by drm.\n");
+               return -1;
+       }
+
+       if (i == ARRAY_SIZE(modules)) {
+               fprintf(stderr, "failed to load any modules, aborting.\n");
+               return -1;
+       }
+
+       resources = drmModeGetResources(fd);
+       if (!resources) {
+               fprintf(stderr, "drmModeGetResources failed: %s\n",
+                       strerror(errno));
+               drmClose(fd);
+               return 1;
+       }
+
+       dump_resource(encoders);
+       dump_resource(connectors);
+       dump_resource(crtcs);
+       dump_resource(framebuffers);
+
+       if (count > 0) {
+               set_mode(con_args, count, test_vsync);
+               getchar();
+       }
+
+       drmModeFreeResources(resources);
+
+       return 0;
+}
diff --git a/tests/name_from_fd.c b/tests/name_from_fd.c
new file mode 100644 (file)
index 0000000..330c8ff
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * Copyright Â© 2009 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Authors:
+ *    Kristian Høgsberg <krh@bitplanet.net>
+ *
+ */
+
+#include <unistd.h>
+#include <fcntl.h>
+#include <limits.h>
+#include "drmtest.h"
+
+/**
+ * Checks drmGetDeviceNameFromFd
+ *
+ * This tests that we can get the actual version out, and that setting invalid
+ * major/minor numbers fails appropriately.  It does not check the actual
+ * behavior differenses resulting from an increased DI version.
+ */
+int main(int argc, char **argv)
+{
+       int fd, ret;
+       drm_set_version_t sv, version;
+       const char *name = "/dev/dri/card0";
+       char *v;
+
+       fd = open("/dev/dri/card0", O_RDWR);
+       if (fd == -1)
+               return 0;
+
+       v = drmGetDeviceNameFromFd(fd);
+       close(fd);
+
+       assert(strcmp(name, v) == 0);
+       drmFree(v);
+
+       return 0;
+}
diff --git a/tests/openclose.c b/tests/openclose.c
new file mode 100644 (file)
index 0000000..946a445
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * Copyright Â© 2007 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Authors:
+ *    Eric Anholt <eric@anholt.net>
+ *
+ */
+
+#include "drmtest.h"
+
+int main(int argc, char **argv)
+{
+       int fd;
+
+       fd = drm_open_any();
+       close(fd);
+       return 0;
+}
diff --git a/tests/planetest/Makefile.am b/tests/planetest/Makefile.am
new file mode 100644 (file)
index 0000000..bc94ed4
--- /dev/null
@@ -0,0 +1,15 @@
+AM_CFLAGS = \
+       -I$(top_srcdir)/include/drm \
+       -I$(top_srcdir)/libkms/ \
+       -I$(top_srcdir) \
+       $(CAIRO_CFLAGS)
+
+noinst_PROGRAMS = \
+       planetest
+
+planetest_SOURCES = \
+       planetest.c
+planetest_LDADD = \
+       $(top_builddir)/libdrm.la \
+       $(top_builddir)/libkms/libkms.la \
+       $(CAIRO_LIBS)
diff --git a/tests/planetest/planetest.c b/tests/planetest/planetest.c
new file mode 100644 (file)
index 0000000..ac7493c
--- /dev/null
@@ -0,0 +1,468 @@
+#include "config.h"
+
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <errno.h>
+#include <getopt.h>
+
+#include "xf86drm.h"
+#include "xf86drmMode.h"
+#include "libkms.h"
+#include "exynos_drm.h"
+
+#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
+
+drmModeRes *resources;
+int fd;
+
+struct connector {
+       uint32_t id;
+       char mode_str[64];
+       drmModeModeInfo *mode;
+       drmModeEncoder *encoder;
+       int crtc;
+       unsigned int fb_id[2], current_fb_id;
+       struct timeval start;
+
+       int swap_count;
+};
+
+struct fb_data {
+       struct kms_driver *kms;
+       struct kms_bo *bo;
+       unsigned int fb_id;
+};
+
+static void
+connector_find_mode(struct connector *c)
+{
+       drmModeConnector *connector;
+       int i, j;
+
+       /* First, find the connector & mode */
+       c->mode = NULL;
+       for (i = 0; i < resources->count_connectors; i++) {
+               connector = drmModeGetConnector(fd, resources->connectors[i]);
+
+               if (!connector) {
+                       fprintf(stderr, "could not get connector %i: %s\n",
+                               resources->connectors[i], strerror(errno));
+                       drmModeFreeConnector(connector);
+                       continue;
+               }
+
+               if (!connector->count_modes) {
+                       drmModeFreeConnector(connector);
+                       continue;
+               }
+
+               if (connector->connector_id != c->id) {
+                       drmModeFreeConnector(connector);
+                       continue;
+               }
+
+               for (j = 0; j < connector->count_modes; j++) {
+                       c->mode = &connector->modes[j];
+                       if (!strcmp(c->mode->name, c->mode_str))
+                               break;
+               }
+
+               /* Found it, break out */
+               if (c->mode)
+                       break;
+
+               drmModeFreeConnector(connector);
+       }
+
+       if (!c->mode) {
+               fprintf(stderr, "failed to find mode \"%s\"\n", c->mode_str);
+               return;
+       }
+
+       /* Now get the encoder */
+       for (i = 0; i < resources->count_encoders; i++) {
+               c->encoder = drmModeGetEncoder(fd, resources->encoders[i]);
+
+               if (!c->encoder) {
+                       fprintf(stderr, "could not get encoder %i: %s\n",
+                               resources->encoders[i], strerror(errno));
+                       drmModeFreeEncoder(c->encoder);
+                       continue;
+               }
+
+               if (c->encoder->encoder_id  == connector->encoder_id)
+                       break;
+
+               drmModeFreeEncoder(c->encoder);
+       }
+
+       if (c->crtc == -1)
+               c->crtc = c->encoder->crtc_id;
+}
+
+static struct kms_bo *
+allocate_buffer(struct kms_driver *kms,
+               int width, int height, unsigned *stride)
+{
+       struct kms_bo *bo;
+       unsigned bo_attribs[] = {
+               KMS_WIDTH,   0,
+               KMS_HEIGHT,  0,
+               KMS_BO_TYPE, KMS_BO_TYPE_SCANOUT_X8R8G8B8,
+               KMS_TERMINATE_PROP_LIST
+       };
+       int ret;
+
+       bo_attribs[1] = width;
+       bo_attribs[3] = height;
+
+       ret = kms_bo_create(kms, bo_attribs, &bo);
+       if (ret) {
+               fprintf(stderr, "failed to alloc buffer: %s\n",
+                       strerror(-ret));
+               return NULL;
+       }
+
+       ret = kms_bo_get_prop(bo, KMS_PITCH, stride);
+       if (ret) {
+               fprintf(stderr, "failed to retreive buffer stride: %s\n",
+                       strerror(-ret));
+               kms_bo_destroy(&bo);
+               return NULL;
+       }
+
+       return bo;
+}
+
+static int
+create_grey_buffer(struct kms_driver *kms,
+                  int width, int height, int *stride_out,
+                  struct kms_bo **bo_out, int color)
+{
+       struct kms_bo *bo;
+       int size, ret;
+       unsigned stride;
+       void *virtual;
+
+       bo = allocate_buffer(kms, width, height, &stride);
+       if (!bo)
+               return -1;
+
+       ret = kms_bo_map(bo, &virtual);
+       if (ret) {
+               fprintf(stderr, "failed to map buffer: %s\n",
+                       strerror(-ret));
+               kms_bo_destroy(&bo);
+               return -1;
+       }
+
+       size = stride * height;
+       memset(virtual, color, size);
+       kms_bo_unmap(bo);
+
+       *bo_out = bo;
+       *stride_out = stride;
+
+       return 0;
+}
+
+static int
+connector_find_plane(struct connector *c)
+{
+       drmModePlaneRes *plane_resources;
+       drmModePlane *ovr;
+       uint32_t id = 0;
+       int i;
+
+       plane_resources = drmModeGetPlaneResources(fd);
+       if (!plane_resources) {
+               fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
+                       strerror(errno));
+               return 0;
+       }
+
+       for (i = 0; i < plane_resources->count_planes; i++) {
+               ovr = drmModeGetPlane(fd, plane_resources->planes[i]);
+               if (!ovr) {
+                       fprintf(stderr, "drmModeGetPlane failed: %s\n",
+                               strerror(errno));
+                       continue;
+               }
+
+               if (ovr->possible_crtcs & (1<<i)) {
+                       id = ovr->plane_id;
+                       drmModeFreePlane(ovr);
+                       break;
+               }
+               drmModeFreePlane(ovr);
+       }
+
+       return id;
+}
+
+static int
+connector_find_plane2(struct connector *c, unsigned int *plane_id)
+{
+       drmModePlaneRes *plane_resources;
+       drmModePlane *ovr;
+       int i;
+
+       plane_resources = drmModeGetPlaneResources(fd);
+       if (!plane_resources) {
+               fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
+                       strerror(errno));
+               return -1;
+       }
+
+       for (i = 0; i < plane_resources->count_planes; i++) {
+               plane_id[i] = 0;
+
+               ovr = drmModeGetPlane(fd, plane_resources->planes[i]);
+               if (!ovr) {
+                       fprintf(stderr, "drmModeGetPlane failed: %s\n",
+                               strerror(errno));
+                       continue;
+               }
+
+               if (ovr->possible_crtcs & (1 << 0))
+                       plane_id[i] = ovr->plane_id;
+               drmModeFreePlane(ovr);
+       }
+
+       return 0;
+}
+
+static struct fb_data *make_fb(int w, int h, int color)
+{
+       struct fb_data *fb_data;
+       struct kms_driver *kms;
+       struct kms_bo *bo;
+       unsigned int fb_id;
+       unsigned handle;
+       int stride;
+       int err;
+
+       fb_data = malloc(sizeof(struct fb_data));
+       if (!fb_data)
+               return NULL;
+
+       memset(fb_data, 0, sizeof(struct fb_data));
+
+       err = kms_create(fd, &kms);
+       if (err) {
+               fprintf(stderr, "failed to create kms driver: %s\n",
+                       strerror(-err));
+               goto err_alloc;
+       }
+
+       if (create_grey_buffer(kms, w, h, &stride, &bo, color))
+               goto err_alloc;
+
+       kms_bo_get_prop(bo, KMS_HANDLE, &handle);
+       err = drmModeAddFB(fd, w, h, 32, 32, stride, handle, &fb_id);
+       if (err) {
+               fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
+               goto err_alloc;
+       }
+
+       fb_data->fb_id = fb_id;
+       fb_data->kms = kms;
+       fb_data->bo = bo;
+
+       return fb_data;
+
+err_alloc:
+       free(fb_data);
+       return NULL;
+}
+
+static int exynos_plane_set_zpos(int fd, unsigned int plane_id, int zpos)
+{
+       struct drm_exynos_plane_set_zpos zpos_req;
+       int ret;
+
+       zpos_req.plane_id = plane_id;
+       zpos_req.zpos = zpos;
+
+       ret = ioctl(fd, DRM_IOCTL_EXYNOS_PLANE_SET_ZPOS, &zpos_req);
+       if (ret < 0) {
+               fprintf(stderr, "failed to set plane zpos: %s\n",
+                               strerror(-ret));
+               return ret;
+       }
+
+       return 0;
+}
+
+#define PLANE_NR       5
+static void planetest_start(int nr, int start_plane, int crtc, int connector)
+{
+       struct connector c;
+       struct fb_data *fb_data[PLANE_NR];
+       unsigned int plane_id[PLANE_NR];
+       int height;
+       int width;
+       int x;
+       int y;
+       int i;
+       int ret;
+
+       c.id = connector;
+       c.crtc = crtc;
+
+       if (nr < 1 || nr > PLANE_NR) {
+               fprintf(stderr, "wrong plane count\n");
+               return;
+       }
+
+       if (start_plane < 0 || start_plane > (nr -1) ) {
+               fprintf(stderr, "Wrong start plane\n");
+               return;
+       }
+
+       connector_find_mode(&c);
+
+       if (c.mode == NULL) {
+               fprintf(stderr, "mode is NULL\n");
+               return;
+       }
+
+       width = c.mode->hdisplay;
+       height = c.mode->vdisplay;
+
+       width /= 8;
+       height /= 8;
+       x = width;
+       y = height;
+
+       ret = connector_find_plane2(&c, plane_id);
+       if (ret < 0)
+               goto err;
+
+       for (i = start_plane; i < nr; i++) {
+               if (!plane_id[i])
+                       continue;
+
+               fb_data[i] = make_fb(width, height, 0x30 + 0x20 * i);
+               if (!fb_data[i])
+                       return;
+
+               if (exynos_plane_set_zpos(fd, plane_id[i], i))
+                       goto err;
+
+               if (drmModeSetPlane(fd, plane_id[i], c.crtc, fb_data[i]->fb_id,
+                                       x, y, width, height,
+                                       0, 0, width, height)) {
+                       fprintf(stderr, "failed to enable plane: %s\n",
+                                       strerror(errno));
+                       goto err;
+               }
+
+               x += width - 30;
+               y += height - 30;
+               width += 40;
+               height += 40;
+       }
+
+       getchar();
+
+err:
+       for (i = start_plane; i < nr; i++) {
+               if (fb_data[i]) {
+                       kms_bo_destroy(&fb_data[i]->bo);
+                       kms_destroy(&fb_data[i]->kms);
+                       free(fb_data[i]);
+               }
+
+               if (!plane_id[i])
+                       continue;
+
+               if (drmModeSetPlane(fd, plane_id[i], c.crtc,
+                               0, 0, 0, /* bufferId, crtc_x, crtc_y */
+                               0, 0, /* crtc_w, crtc_h */
+                               0, 0, 0, 0 /* src_XXX */)) {
+                       fprintf(stderr, "failed to enable plane: %s\n",
+                                       strerror(errno));
+                       goto err;
+               }
+       }
+}
+
+static void help(void)
+{
+       printf("Usage: ./planetest [OPTION]\n"
+              " [OPTION]\n"
+              " -s <connector_id>@<crtc_id>/<plane count>/<start_plane>\n"
+              " -h Usage\n"
+              "\n"
+              "Default: connector 11, crtc 3, plane_count 5, start_plane 1\n");
+       exit(0);
+}
+
+static char optstr[] = "hs:";
+
+int main(int argc, char **argv)
+{
+       char *modules[] = { "exynos-drm" };
+       int opt;
+       int i, connector_id, nr, start_plane;
+       uint32_t crtc_id;
+
+       // default option (LCD)
+       crtc_id = 3;
+       connector_id = 11;
+       nr = 5;
+       start_plane = 1;
+
+       /* parse args */
+       opterr = 0;
+       while ((opt = getopt(argc, argv, optstr)) != -1) {
+               switch (opt) {
+               case 's':
+                       if (sscanf(optarg, "%d@%d/%d/%d",
+                                  &connector_id,
+                                  &crtc_id,
+                                  &nr,
+                                  &start_plane) != 4) {
+                               help();
+                               break;
+                       }
+                       break;
+               case 'h':
+               default:
+                       help();
+                       break;
+               }
+               break;
+       }
+
+       for (i = 0; i < ARRAY_SIZE(modules); i++) {
+               printf("trying to load module %s...", modules[i]);
+               fd = drmOpen(modules[i], NULL);
+               if (fd < 0) {
+                       printf("failed.\n");
+               } else {
+                       printf("success.\n");
+                       break;
+               }
+       }
+
+       resources = drmModeGetResources(fd);
+       if (!resources) {
+               fprintf(stderr, "drmModeGetResources failed: %s\n",
+                       strerror(errno));
+               drmClose(fd);
+               return 1;
+       }
+
+       planetest_start(nr, start_plane, crtc_id, connector_id);
+
+       /* TODO */
+
+       drmModeFreeResources(resources);
+
+       return 0;
+}
diff --git a/tests/setversion.c b/tests/setversion.c
new file mode 100644 (file)
index 0000000..3aaf7cc
--- /dev/null
@@ -0,0 +1,89 @@
+/*
+ * Copyright Â© 2007 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Authors:
+ *    Eric Anholt <eric@anholt.net>
+ *
+ */
+
+#include <limits.h>
+#include "drmtest.h"
+
+/**
+ * Checks DRM_IOCTL_SET_VERSION.
+ *
+ * This tests that we can get the actual version out, and that setting invalid
+ * major/minor numbers fails appropriately.  It does not check the actual
+ * behavior differenses resulting from an increased DI version.
+ */
+int main(int argc, char **argv)
+{
+       int fd, ret;
+       drm_set_version_t sv, version;
+
+       if (getuid() != 0) {
+               fprintf(stderr, "setversion test requires root, skipping\n");
+               return 0;
+       }
+
+       fd = drm_open_any_master();
+
+       /* First, check that we can get the DD/DI versions. */
+       memset(&version, 0, sizeof(version));
+       version.drm_di_major = -1;
+       version.drm_di_minor = -1;
+       version.drm_dd_major = -1;
+       version.drm_dd_minor = -1;
+       ret = ioctl(fd, DRM_IOCTL_SET_VERSION, &version);
+       assert(ret == 0);
+       assert(version.drm_di_major != -1);
+       assert(version.drm_di_minor != -1);
+       assert(version.drm_dd_major != -1);
+       assert(version.drm_dd_minor != -1);
+
+       /* Check that an invalid DI major fails */
+       sv = version;
+       sv.drm_di_major++;
+       ret = ioctl(fd, DRM_IOCTL_SET_VERSION, &sv);
+       assert(ret == -1 && errno == EINVAL);
+
+       /* Check that an invalid DI minor fails */
+       sv = version;
+       sv.drm_di_major++;
+       ret = ioctl(fd, DRM_IOCTL_SET_VERSION, &sv);
+       assert(ret == -1 && errno == EINVAL);
+
+       /* Check that an invalid DD major fails */
+       sv = version;
+       sv.drm_dd_major++;
+       ret = ioctl(fd, DRM_IOCTL_SET_VERSION, &sv);
+       assert(ret == -1 && errno == EINVAL);
+
+       /* Check that an invalid DD minor fails */
+       sv = version;
+       sv.drm_dd_minor++;
+       ret = ioctl(fd, DRM_IOCTL_SET_VERSION, &sv);
+       assert(ret == -1 && errno == EINVAL);
+
+       close(fd);
+       return 0;
+}
diff --git a/tests/ttmtest/AUTHORS b/tests/ttmtest/AUTHORS
new file mode 100644 (file)
index 0000000..fa4a089
--- /dev/null
@@ -0,0 +1 @@
+Thomas Hellström <thomas-at-tungstengraphics.com> and others.
diff --git a/tests/ttmtest/ChangeLog b/tests/ttmtest/ChangeLog
new file mode 100644 (file)
index 0000000..4588c8d
--- /dev/null
@@ -0,0 +1,23 @@
+2006-01-24  Thomas Hellström  <thomas-at-tungstengraphics.com>
+
+       * configure.ac:
+       * src/ttmtest.c:
+
+       Fixed include path.
+
+2006-01-24  Thomas Hellström  <thomas-at-tungstengraphics.com>
+
+       * AUTHORS:
+       * Makefile.am:
+       * configure.ac:
+       * reconf:
+       * src/Makefile.am:
+       * src/ttmtest.c: (fastrdtsc), (time_diff), (releaseContext),
+       (testAGP), (main):
+       * src/xf86dri.c: (uniDRIDestroyContext), (uniDRICreateDrawable),
+       (uniDRIDestroyDrawable), (uniDRIGetDrawableInfo):
+       * src/xf86dri.h:
+       * src/xf86dristr.h:
+
+       Initial import of the ttmtest utility.
+       
\ No newline at end of file
diff --git a/tests/ttmtest/Makefile.am b/tests/ttmtest/Makefile.am
new file mode 100644 (file)
index 0000000..af437a6
--- /dev/null
@@ -0,0 +1 @@
+SUBDIRS = src
diff --git a/tests/ttmtest/NEWS b/tests/ttmtest/NEWS
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/ttmtest/README b/tests/ttmtest/README
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/ttmtest/configure.ac b/tests/ttmtest/configure.ac
new file mode 100644 (file)
index 0000000..c41e91a
--- /dev/null
@@ -0,0 +1,33 @@
+AC_INIT
+AC_PROG_CC
+AC_PATH_X
+if test "x$no_x" != "xyes"; then
+  savecpp="$CPPFLAGS"
+  CPPFLAGS="$CPPFLAGS -I$x_includes"
+  AC_CHECK_HEADER($x_includes/X11/Xlib.h,,\
+        [AC_MSG_ERROR(Could not find X installation.)])
+  CPPFLAGS="$savecpp"
+  MDRIINC="-I$x_includes"
+  LIBS="-L$x_libraries $LIBS"
+else
+  AC_MSG_ERROR(Could not find X installation. Aborting.)
+fi
+AC_ARG_WITH(libdrm,
+            AC_HELP_STRING([--with-libdrm=DIR],
+                           [Installation prefix of libdrm [[default=/usr]]]),
+            [libdrmpref="$withval"],
+            [libdrmpref="/usr"])
+savecpp="$CPPFLAGS"
+MDRIINC="-I$libdrmpref/include -I$libdrmpref/include/drm -I$x_includes"
+CPPFLAGS="$CPPFLAGS $MDRIINC"
+AC_CHECK_HEADER(xf86drm.h,,\
+                [AC_MSG_ERROR(Could not find libdrm installation. Use --with-libdrm=<libdrm_installation_prefix>)])
+AC_CHECK_HEADER(drm.h,,\
+                [AC_MSG_ERROR(Could not find libdrm installation. Use --with-libdrm=<libdrm_installation_prefix>)])
+CPPFLAGS="$savecpp"
+LIBS="-L$libdrmpref/lib64 -L$libdrmpref/lib $LIBS"
+AC_SUBST(MDRIINC)
+AC_SYS_LARGEFILE
+AM_INIT_AUTOMAKE(minidri,0.1.0)
+AM_CONFIG_HEADER(config.h)
+AC_OUTPUT([Makefile src/Makefile])
diff --git a/tests/ttmtest/reconf b/tests/ttmtest/reconf
new file mode 100755 (executable)
index 0000000..e64d00a
--- /dev/null
@@ -0,0 +1,2 @@
+#!/bin/sh
+autoreconf -v --install || exit 1
\ No newline at end of file
diff --git a/tests/ttmtest/src/Makefile.am b/tests/ttmtest/src/Makefile.am
new file mode 100644 (file)
index 0000000..b7ee829
--- /dev/null
@@ -0,0 +1,8 @@
+INCLUDES = @MDRIINC@
+bin_PROGRAMS = ttmtest
+ttmtest_SOURCES = \
+       ttmtest.c \
+       xf86dri.c \
+       xf86dri.h \
+       xf86dristr.h 
+ttmtest_LDADD = -ldrm -lXext -lX11
diff --git a/tests/ttmtest/src/ttmtest.c b/tests/ttmtest/src/ttmtest.c
new file mode 100644 (file)
index 0000000..36df242
--- /dev/null
@@ -0,0 +1,430 @@
+/**************************************************************************
+ * 
+ * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, TX., USA
+ * All Rights Reserved.
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 
+ * USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ * 
+ * 
+ **************************************************************************/
+/*
+ * Authors: Thomas Hellström <thomas-at-tungstengraphics-dot-com>
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <X11/Xlib.h>
+#include <X11/Xutil.h>
+#include <stdint.h>
+#include <drm/drm.h>
+#include "xf86dri.h"
+#include "xf86drm.h"
+#include "stdio.h"
+#include "sys/types.h"
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include <stdlib.h>
+#include "sys/mman.h"
+
+typedef struct
+{
+    enum
+    {
+       haveNothing,
+       haveDisplay,
+       haveConnection,
+       haveDriverName,
+       haveDeviceInfo,
+       haveDRM,
+       haveContext
+    }
+    state;
+
+    Display *display;
+    int screen;
+    drm_handle_t sAreaOffset;
+    char *curBusID;
+    char *driverName;
+    int drmFD;
+    XVisualInfo visualInfo;
+    XID id;
+    drm_context_t hwContext;
+    void *driPriv;
+    int driPrivSize;
+    int fbSize;
+    int fbOrigin;
+    int fbStride;
+    drm_handle_t fbHandle;
+    int ddxDriverMajor;
+    int ddxDriverMinor;
+    int ddxDriverPatch;
+} TinyDRIContext;
+
+#ifndef __x86_64__
+static unsigned
+fastrdtsc(void)
+{
+    unsigned eax;
+    __asm__ volatile ("\t"
+       "pushl  %%ebx\n\t"
+       "cpuid\n\t" ".byte 0x0f, 0x31\n\t" "popl %%ebx\n":"=a" (eax)
+       :"0"(0)
+       :"ecx", "edx", "cc");
+
+    return eax;
+}
+#else
+static unsigned
+fastrdtsc(void)
+{
+    unsigned eax;
+    __asm__ volatile ("\t" "cpuid\n\t" ".byte 0x0f, 0x31\n\t":"=a" (eax)
+       :"0"(0)
+       :"ecx", "edx", "ebx", "cc");
+
+    return eax;
+}
+#endif
+
+void
+bmError(int val, const char *file, const char *function, int line)
+{
+    fprintf(stderr, "Fatal video memory manager error \"%s\".\n"
+       "Check kernel logs or set the LIBGL_DEBUG\n"
+       "environment variable to \"verbose\" for more info.\n"
+       "Detected in file %s, line %d, function %s.\n",
+       strerror(-val), file, line, function);
+    abort();
+}
+
+#define BM_CKFATAL(val)                                               \
+  do{                                                         \
+    int tstVal = (val);                                               \
+    if (tstVal)                                               \
+      bmError(tstVal, __FILE__, __FUNCTION__, __LINE__);       \
+  } while(0);
+
+static unsigned
+time_diff(unsigned t, unsigned t2)
+{
+    return ((t < t2) ? t2 - t : 0xFFFFFFFFU - (t - t2 - 1));
+}
+
+static int
+releaseContext(TinyDRIContext * ctx)
+{
+    switch (ctx->state) {
+    case haveContext:
+       uniDRIDestroyContext(ctx->display, ctx->screen, ctx->id);
+    case haveDRM:
+       drmClose(ctx->drmFD);
+    case haveDeviceInfo:
+       XFree(ctx->driPriv);
+    case haveDriverName:
+       XFree(ctx->driverName);
+    case haveConnection:
+       XFree(ctx->curBusID);
+       uniDRICloseConnection(ctx->display, ctx->screen);
+    case haveDisplay:
+       XCloseDisplay(ctx->display);
+    default:
+       break;
+    }
+    return -1;
+}
+
+static void
+readBuf(void *buf, unsigned long size)
+{
+    volatile unsigned *buf32 = (unsigned *)buf;
+    unsigned *end = (unsigned *)buf32 + size / sizeof(*buf32);
+
+    while (buf32 < end) {
+       (void)*buf32++;
+    }
+}
+
+static int
+benchmarkBuffer(TinyDRIContext * ctx, unsigned long size,
+    unsigned long *ticks)
+{
+    unsigned long curTime, oldTime;
+    int ret;
+    drmBO buf;
+    void *virtual;
+
+    /*
+     * Test system memory objects.
+     */
+    oldTime = fastrdtsc();
+    BM_CKFATAL(drmBOCreate(ctx->drmFD, size, 0, NULL,
+                          DRM_BO_FLAG_READ |
+                          DRM_BO_FLAG_WRITE |
+                          DRM_BO_FLAG_MEM_LOCAL, 0, &buf));
+    curTime = fastrdtsc();
+    *ticks++ = time_diff(oldTime, curTime);
+
+    oldTime = fastrdtsc();
+    BM_CKFATAL(drmBOMap(ctx->drmFD, &buf,
+           DRM_BO_FLAG_READ | DRM_BO_FLAG_WRITE, 0, &virtual));
+    curTime = fastrdtsc();
+    *ticks++ = time_diff(oldTime, curTime);
+
+    oldTime = fastrdtsc();
+    memset(virtual, 0xF0, buf.size);
+    curTime = fastrdtsc();
+    *ticks++ = time_diff(oldTime, curTime);
+
+    oldTime = fastrdtsc();
+    memset(virtual, 0x0F, buf.size);
+    curTime = fastrdtsc();
+    *ticks++ = time_diff(oldTime, curTime);
+
+    oldTime = fastrdtsc();
+    readBuf(virtual, buf.size);
+    curTime = fastrdtsc();
+    *ticks++ = time_diff(oldTime, curTime);
+
+    oldTime = fastrdtsc();
+    BM_CKFATAL(drmBOUnmap(ctx->drmFD, &buf));
+    curTime = fastrdtsc();
+    *ticks++ = time_diff(oldTime, curTime);
+
+    /*
+     * Test TT bound buffer objects.
+     */
+
+    oldTime = fastrdtsc();
+    BM_CKFATAL(drmBOSetStatus(ctx->drmFD, &buf,
+                            DRM_BO_FLAG_MEM_TT, 
+                            DRM_BO_MASK_MEM, 
+                             0,0,0));
+    curTime = fastrdtsc();
+    *ticks++ = time_diff(oldTime, curTime);
+
+    oldTime = fastrdtsc();
+    BM_CKFATAL(drmBOMap(ctx->drmFD, &buf,
+           DRM_BO_FLAG_READ | DRM_BO_FLAG_WRITE, 0, &virtual));
+    curTime = fastrdtsc();
+    *ticks++ = time_diff(oldTime, curTime);
+
+    oldTime = fastrdtsc();
+    memset(virtual, 0xF0, buf.size);
+    curTime = fastrdtsc();
+    *ticks++ = time_diff(oldTime, curTime);
+
+    oldTime = fastrdtsc();
+    memset(virtual, 0x0F, buf.size);
+    curTime = fastrdtsc();
+    *ticks++ = time_diff(oldTime, curTime);
+
+    oldTime = fastrdtsc();
+    readBuf(virtual, buf.size);
+    curTime = fastrdtsc();
+    *ticks++ = time_diff(oldTime, curTime);
+
+    BM_CKFATAL(drmBOUnmap(ctx->drmFD, &buf));
+
+    oldTime = fastrdtsc();
+    BM_CKFATAL(drmBOSetStatus(ctx->drmFD, &buf,
+                            DRM_BO_FLAG_MEM_LOCAL, DRM_BO_MASK_MEM, 0, 0,0));
+    curTime = fastrdtsc();
+    *ticks++ = time_diff(oldTime, curTime);
+
+    /*
+     * Test cached buffers objects.
+     */
+
+    oldTime = fastrdtsc();
+    ret = drmBOSetStatus(ctx->drmFD, &buf,
+                        DRM_BO_FLAG_MEM_TT | 
+                        DRM_BO_FLAG_CACHED | 
+                        DRM_BO_FLAG_FORCE_CACHING,
+                        DRM_BO_MASK_MEMTYPE | 
+                        DRM_BO_FLAG_FORCE_CACHING,
+                        0, 0, 0);
+    curTime = fastrdtsc();
+
+    if (ret) {
+       printf("Couldn't bind cached. Probably no support\n");
+       BM_CKFATAL(drmBOUnreference(ctx->drmFD, &buf));
+       return 1;
+    }
+    *ticks++ = time_diff(oldTime, curTime);
+
+    oldTime = fastrdtsc();
+    BM_CKFATAL(drmBOMap(ctx->drmFD, &buf,
+           DRM_BO_FLAG_READ | DRM_BO_FLAG_WRITE, 0, &virtual));
+
+    curTime = fastrdtsc();
+    *ticks++ = time_diff(oldTime, curTime);
+
+    oldTime = fastrdtsc();
+    memset(virtual, 0xF0, buf.size);
+    curTime = fastrdtsc();
+    *ticks++ = time_diff(oldTime, curTime);
+
+    oldTime = fastrdtsc();
+    memset(virtual, 0x0F, buf.size);
+    curTime = fastrdtsc();
+    *ticks++ = time_diff(oldTime, curTime);
+
+    oldTime = fastrdtsc();
+    readBuf(virtual, buf.size);
+    curTime = fastrdtsc();
+    *ticks++ = time_diff(oldTime, curTime);
+
+    BM_CKFATAL(drmBOUnmap(ctx->drmFD, &buf));
+    BM_CKFATAL(drmBOUnreference(ctx->drmFD, &buf));
+
+    return 0;
+}
+
+static void
+testAGP(TinyDRIContext * ctx)
+{
+    unsigned long ticks[128], *pTicks;
+    unsigned long size = 8 * 1024;
+    int ret;
+
+    ret = benchmarkBuffer(ctx, size, ticks);
+    if (ret < 0) {
+       fprintf(stderr, "Buffer error %s\n", strerror(-ret));
+       return;
+    }
+    pTicks = ticks;
+
+    printf("Buffer size %d bytes\n", size);
+    printf("System memory timings ********************************\n");
+    printf("Creation took            %12lu ticks\n", *pTicks++);
+    printf("Mapping took             %12lu ticks\n", *pTicks++);
+    printf("Writing took             %12lu ticks\n", *pTicks++);
+    printf("Writing Again took       %12lu ticks\n", *pTicks++);
+    printf("Reading took             %12lu ticks\n", *pTicks++);
+    printf("Unmapping took           %12lu ticks\n", *pTicks++);
+
+    printf("\nTT Memory timings ************************************\n");
+    printf("Moving to TT took        %12lu ticks\n", *pTicks++);
+    printf("Mapping in TT took       %12lu ticks\n", *pTicks++);
+    printf("Writing to TT took       %12lu ticks\n", *pTicks++);
+    printf("Writing again to TT took %12lu ticks\n", *pTicks++);
+    printf("Reading from TT took     %12lu ticks\n", *pTicks++);
+    printf("Moving to system took    %12lu ticks\n", *pTicks++);
+
+    if (ret == 1)
+       return;
+
+    printf("\nCached TT Memory timings *****************************\n");
+    printf("Moving to CTT took       %12lu ticks\n", *pTicks++);
+    printf("Mapping in CTT took      %12lu ticks\n", *pTicks++);
+    printf("Writing to CTT took      %12lu ticks\n", *pTicks++);
+    printf("Re-writing to CTT took   %12lu ticks\n", *pTicks++);
+    printf("Reading from CTT took    %12lu ticks\n", *pTicks++);
+    printf("\n\n");
+}
+
+int
+main()
+{
+    int ret, screen, isCapable;
+    char *displayName = ":0";
+    TinyDRIContext ctx;
+    unsigned magic;
+
+    ctx.screen = 0;
+    ctx.state = haveNothing;
+    ctx.display = XOpenDisplay(displayName);
+    if (!ctx.display) {
+       fprintf(stderr, "Could not open display\n");
+       return releaseContext(&ctx);
+    }
+    ctx.state = haveDisplay;
+
+    ret =
+       uniDRIQueryDirectRenderingCapable(ctx.display, ctx.screen,
+       &isCapable);
+    if (!ret || !isCapable) {
+       fprintf(stderr, "No DRI on this display:sceen\n");
+       return releaseContext(&ctx);
+    }
+
+    if (!uniDRIOpenConnection(ctx.display, ctx.screen, &ctx.sAreaOffset,
+           &ctx.curBusID)) {
+       fprintf(stderr, "Could not open DRI connection.\n");
+       return releaseContext(&ctx);
+    }
+    ctx.state = haveConnection;
+
+    if (!uniDRIGetClientDriverName(ctx.display, ctx.screen,
+           &ctx.ddxDriverMajor, &ctx.ddxDriverMinor,
+           &ctx.ddxDriverPatch, &ctx.driverName)) {
+       fprintf(stderr, "Could not get DRI driver name.\n");
+       return releaseContext(&ctx);
+    }
+    ctx.state = haveDriverName;
+
+    if (!uniDRIGetDeviceInfo(ctx.display, ctx.screen,
+           &ctx.fbHandle, &ctx.fbOrigin, &ctx.fbSize,
+           &ctx.fbStride, &ctx.driPrivSize, &ctx.driPriv)) {
+       fprintf(stderr, "Could not get DRI device info.\n");
+       return releaseContext(&ctx);
+    }
+    ctx.state = haveDriverName;
+
+    if ((ctx.drmFD = drmOpen(NULL, ctx.curBusID)) < 0) {
+       perror("DRM Device could not be opened");
+       return releaseContext(&ctx);
+    }
+    ctx.state = haveDRM;
+
+    drmGetMagic(ctx.drmFD, &magic);
+    if (!uniDRIAuthConnection(ctx.display, ctx.screen, magic)) {
+       fprintf(stderr, "Could not get X server to authenticate us.\n");
+       return releaseContext(&ctx);
+    }
+
+    ret = XMatchVisualInfo(ctx.display, ctx.screen, 24, TrueColor,
+       &ctx.visualInfo);
+    if (!ret) {
+       ret = XMatchVisualInfo(ctx.display, ctx.screen, 16, TrueColor,
+           &ctx.visualInfo);
+       if (!ret) {
+           fprintf(stderr, "Could not find a matching visual.\n");
+           return releaseContext(&ctx);
+       }
+    }
+
+    if (!uniDRICreateContext(ctx.display, ctx.screen, ctx.visualInfo.visual,
+           &ctx.id, &ctx.hwContext)) {
+       fprintf(stderr, "Could not create DRI context.\n");
+       return releaseContext(&ctx);
+    }
+    ctx.state = haveContext;
+
+    testAGP(&ctx);
+
+    releaseContext(&ctx);
+    printf("Terminating normally\n");
+    return 0;
+}
diff --git a/tests/ttmtest/src/xf86dri.c b/tests/ttmtest/src/xf86dri.c
new file mode 100644 (file)
index 0000000..e6e0b89
--- /dev/null
@@ -0,0 +1,603 @@
+/* $XFree86: xc/lib/GL/dri/XF86dri.c,v 1.13 2002/10/30 12:51:25 alanh Exp $ */
+/**************************************************************************
+
+Copyright 1998-1999 Precision Insight, Inc., Cedar Park, Texas.
+Copyright 2000 VA Linux Systems, Inc.
+All Rights Reserved.
+
+Permission is hereby granted, free of charge, to any person obtaining a
+copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sub license, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice (including the
+next paragraph) shall be included in all copies or substantial portions
+of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
+ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+**************************************************************************/
+
+/*
+ * Authors:
+ *   Kevin E. Martin <martin@valinux.com>
+ *   Jens Owen <jens@tungstengraphics.com>
+ *   Rickard E. (Rik) Faith <faith@valinux.com>
+ *
+ */
+
+/* THIS IS NOT AN X CONSORTIUM STANDARD */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <X11/Xlibint.h>
+#include <X11/extensions/Xext.h>
+#include <X11/extensions/extutil.h>
+#include <stdint.h>
+#include "xf86dristr.h"
+
+static XExtensionInfo _xf86dri_info_data;
+static XExtensionInfo *xf86dri_info = &_xf86dri_info_data;
+static char xf86dri_extension_name[] = XF86DRINAME;
+
+#define uniDRICheckExtension(dpy,i,val) \
+  XextCheckExtension (dpy, i, xf86dri_extension_name, val)
+
+/*****************************************************************************
+ *                                                                           *
+ *                        private utility routines                          *
+ *                                                                           *
+ *****************************************************************************/
+
+static int close_display(Display * dpy, XExtCodes * extCodes);
+static /* const */ XExtensionHooks xf86dri_extension_hooks = {
+    NULL,                             /* create_gc */
+    NULL,                             /* copy_gc */
+    NULL,                             /* flush_gc */
+    NULL,                             /* free_gc */
+    NULL,                             /* create_font */
+    NULL,                             /* free_font */
+    close_display,                    /* close_display */
+    NULL,                             /* wire_to_event */
+    NULL,                             /* event_to_wire */
+    NULL,                             /* error */
+    NULL,                             /* error_string */
+};
+
+static
+XEXT_GENERATE_FIND_DISPLAY(find_display, xf86dri_info,
+    xf86dri_extension_name, &xf86dri_extension_hooks, 0, NULL)
+
+    static XEXT_GENERATE_CLOSE_DISPLAY(close_display, xf86dri_info)
+
+/*****************************************************************************
+ *                                                                           *
+ *                 public XFree86-DRI Extension routines                    *
+ *                                                                           *
+ *****************************************************************************/
+#if 0
+#include <stdio.h>
+#define TRACE(msg)  fprintf(stderr,"uniDRI%s\n", msg);
+#else
+#define TRACE(msg)
+#endif
+    Bool uniDRIQueryExtension(dpy, event_basep, error_basep)
+    Display *dpy;
+    int *event_basep, *error_basep;
+{
+    XExtDisplayInfo *info = find_display(dpy);
+
+    TRACE("QueryExtension...");
+    if (XextHasExtension(info)) {
+       *event_basep = info->codes->first_event;
+       *error_basep = info->codes->first_error;
+       TRACE("QueryExtension... return True");
+       return True;
+    } else {
+       TRACE("QueryExtension... return False");
+       return False;
+    }
+}
+
+Bool
+uniDRIQueryVersion(dpy, majorVersion, minorVersion, patchVersion)
+    Display *dpy;
+    int *majorVersion;
+    int *minorVersion;
+    int *patchVersion;
+{
+    XExtDisplayInfo *info = find_display(dpy);
+    xXF86DRIQueryVersionReply rep;
+    xXF86DRIQueryVersionReq *req;
+
+    TRACE("QueryVersion...");
+    uniDRICheckExtension(dpy, info, False);
+
+    LockDisplay(dpy);
+    GetReq(XF86DRIQueryVersion, req);
+    req->reqType = info->codes->major_opcode;
+    req->driReqType = X_XF86DRIQueryVersion;
+    if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
+       UnlockDisplay(dpy);
+       SyncHandle();
+       TRACE("QueryVersion... return False");
+       return False;
+    }
+    *majorVersion = rep.majorVersion;
+    *minorVersion = rep.minorVersion;
+    *patchVersion = rep.patchVersion;
+    UnlockDisplay(dpy);
+    SyncHandle();
+    TRACE("QueryVersion... return True");
+    return True;
+}
+
+Bool
+uniDRIQueryDirectRenderingCapable(dpy, screen, isCapable)
+    Display *dpy;
+    int screen;
+    Bool *isCapable;
+{
+    XExtDisplayInfo *info = find_display(dpy);
+    xXF86DRIQueryDirectRenderingCapableReply rep;
+    xXF86DRIQueryDirectRenderingCapableReq *req;
+
+    TRACE("QueryDirectRenderingCapable...");
+    uniDRICheckExtension(dpy, info, False);
+
+    LockDisplay(dpy);
+    GetReq(XF86DRIQueryDirectRenderingCapable, req);
+    req->reqType = info->codes->major_opcode;
+    req->driReqType = X_XF86DRIQueryDirectRenderingCapable;
+    req->screen = screen;
+    if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
+       UnlockDisplay(dpy);
+       SyncHandle();
+       TRACE("QueryDirectRenderingCapable... return False");
+       return False;
+    }
+    *isCapable = rep.isCapable;
+    UnlockDisplay(dpy);
+    SyncHandle();
+    TRACE("QueryDirectRenderingCapable... return True");
+    return True;
+}
+
+Bool
+uniDRIOpenConnection(dpy, screen, hSAREA, busIdString)
+    Display *dpy;
+    int screen;
+    drm_handle_t *hSAREA;
+    char **busIdString;
+{
+    XExtDisplayInfo *info = find_display(dpy);
+    xXF86DRIOpenConnectionReply rep;
+    xXF86DRIOpenConnectionReq *req;
+
+    TRACE("OpenConnection...");
+    uniDRICheckExtension(dpy, info, False);
+
+    LockDisplay(dpy);
+    GetReq(XF86DRIOpenConnection, req);
+    req->reqType = info->codes->major_opcode;
+    req->driReqType = X_XF86DRIOpenConnection;
+    req->screen = screen;
+    if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
+       UnlockDisplay(dpy);
+       SyncHandle();
+       TRACE("OpenConnection... return False");
+       return False;
+    }
+
+    *hSAREA = rep.hSAREALow;
+#ifdef LONG64
+    if (sizeof(drm_handle_t) == 8) {
+       *hSAREA |= ((unsigned long)rep.hSAREAHigh) << 32;
+    }
+#endif
+    if (rep.length) {
+       if (!(*busIdString = (char *)Xcalloc(rep.busIdStringLength + 1, 1))) {
+           _XEatData(dpy, ((rep.busIdStringLength + 3) & ~3));
+           UnlockDisplay(dpy);
+           SyncHandle();
+           TRACE("OpenConnection... return False");
+           return False;
+       }
+       _XReadPad(dpy, *busIdString, rep.busIdStringLength);
+    } else {
+       *busIdString = NULL;
+    }
+    UnlockDisplay(dpy);
+    SyncHandle();
+    TRACE("OpenConnection... return True");
+    return True;
+}
+
+Bool
+uniDRIAuthConnection(dpy, screen, magic)
+    Display *dpy;
+    int screen;
+    drm_magic_t magic;
+{
+    XExtDisplayInfo *info = find_display(dpy);
+    xXF86DRIAuthConnectionReq *req;
+    xXF86DRIAuthConnectionReply rep;
+
+    TRACE("AuthConnection...");
+    uniDRICheckExtension(dpy, info, False);
+
+    LockDisplay(dpy);
+    GetReq(XF86DRIAuthConnection, req);
+    req->reqType = info->codes->major_opcode;
+    req->driReqType = X_XF86DRIAuthConnection;
+    req->screen = screen;
+    req->magic = magic;
+    rep.authenticated = 0;
+    if (!_XReply(dpy, (xReply *) & rep, 0, xFalse) || !rep.authenticated) {
+       UnlockDisplay(dpy);
+       SyncHandle();
+       TRACE("AuthConnection... return False");
+       return False;
+    }
+    UnlockDisplay(dpy);
+    SyncHandle();
+    TRACE("AuthConnection... return True");
+    return True;
+}
+
+Bool
+uniDRICloseConnection(dpy, screen)
+    Display *dpy;
+    int screen;
+{
+    XExtDisplayInfo *info = find_display(dpy);
+    xXF86DRICloseConnectionReq *req;
+
+    TRACE("CloseConnection...");
+
+    uniDRICheckExtension(dpy, info, False);
+
+    LockDisplay(dpy);
+    GetReq(XF86DRICloseConnection, req);
+    req->reqType = info->codes->major_opcode;
+    req->driReqType = X_XF86DRICloseConnection;
+    req->screen = screen;
+    UnlockDisplay(dpy);
+    SyncHandle();
+    TRACE("CloseConnection... return True");
+    return True;
+}
+
+Bool
+uniDRIGetClientDriverName(dpy, screen, ddxDriverMajorVersion,
+    ddxDriverMinorVersion, ddxDriverPatchVersion, clientDriverName)
+    Display *dpy;
+    int screen;
+    int *ddxDriverMajorVersion;
+    int *ddxDriverMinorVersion;
+    int *ddxDriverPatchVersion;
+    char **clientDriverName;
+{
+    XExtDisplayInfo *info = find_display(dpy);
+    xXF86DRIGetClientDriverNameReply rep;
+    xXF86DRIGetClientDriverNameReq *req;
+
+    TRACE("GetClientDriverName...");
+    uniDRICheckExtension(dpy, info, False);
+
+    LockDisplay(dpy);
+    GetReq(XF86DRIGetClientDriverName, req);
+    req->reqType = info->codes->major_opcode;
+    req->driReqType = X_XF86DRIGetClientDriverName;
+    req->screen = screen;
+    if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
+       UnlockDisplay(dpy);
+       SyncHandle();
+       TRACE("GetClientDriverName... return False");
+       return False;
+    }
+
+    *ddxDriverMajorVersion = rep.ddxDriverMajorVersion;
+    *ddxDriverMinorVersion = rep.ddxDriverMinorVersion;
+    *ddxDriverPatchVersion = rep.ddxDriverPatchVersion;
+
+    if (rep.length) {
+       if (!(*clientDriverName =
+               (char *)Xcalloc(rep.clientDriverNameLength + 1, 1))) {
+           _XEatData(dpy, ((rep.clientDriverNameLength + 3) & ~3));
+           UnlockDisplay(dpy);
+           SyncHandle();
+           TRACE("GetClientDriverName... return False");
+           return False;
+       }
+       _XReadPad(dpy, *clientDriverName, rep.clientDriverNameLength);
+    } else {
+       *clientDriverName = NULL;
+    }
+    UnlockDisplay(dpy);
+    SyncHandle();
+    TRACE("GetClientDriverName... return True");
+    return True;
+}
+
+Bool
+uniDRICreateContextWithConfig(dpy, screen, configID, context, hHWContext)
+    Display *dpy;
+    int screen;
+    int configID;
+    XID *context;
+    drm_context_t *hHWContext;
+{
+    XExtDisplayInfo *info = find_display(dpy);
+    xXF86DRICreateContextReply rep;
+    xXF86DRICreateContextReq *req;
+
+    TRACE("CreateContext...");
+    uniDRICheckExtension(dpy, info, False);
+
+    LockDisplay(dpy);
+    GetReq(XF86DRICreateContext, req);
+    req->reqType = info->codes->major_opcode;
+    req->driReqType = X_XF86DRICreateContext;
+    req->visual = configID;
+    req->screen = screen;
+    *context = XAllocID(dpy);
+    req->context = *context;
+    if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
+       UnlockDisplay(dpy);
+       SyncHandle();
+       TRACE("CreateContext... return False");
+       return False;
+    }
+    *hHWContext = rep.hHWContext;
+    UnlockDisplay(dpy);
+    SyncHandle();
+    TRACE("CreateContext... return True");
+    return True;
+}
+
+Bool
+uniDRICreateContext(dpy, screen, visual, context, hHWContext)
+    Display *dpy;
+    int screen;
+    Visual *visual;
+    XID *context;
+    drm_context_t *hHWContext;
+{
+    return uniDRICreateContextWithConfig(dpy, screen, visual->visualid,
+       context, hHWContext);
+}
+
+Bool
+uniDRIDestroyContext(Display * ndpy, int screen, XID context)
+{
+    Display *const dpy = (Display *) ndpy;
+    XExtDisplayInfo *info = find_display(dpy);
+    xXF86DRIDestroyContextReq *req;
+
+    TRACE("DestroyContext...");
+    uniDRICheckExtension(dpy, info, False);
+
+    LockDisplay(dpy);
+    GetReq(XF86DRIDestroyContext, req);
+    req->reqType = info->codes->major_opcode;
+    req->driReqType = X_XF86DRIDestroyContext;
+    req->screen = screen;
+    req->context = context;
+    UnlockDisplay(dpy);
+    SyncHandle();
+    TRACE("DestroyContext... return True");
+    return True;
+}
+
+Bool
+uniDRICreateDrawable(Display * ndpy, int screen,
+    Drawable drawable, drm_drawable_t * hHWDrawable)
+{
+    Display *const dpy = (Display *) ndpy;
+    XExtDisplayInfo *info = find_display(dpy);
+    xXF86DRICreateDrawableReply rep;
+    xXF86DRICreateDrawableReq *req;
+
+    TRACE("CreateDrawable...");
+    uniDRICheckExtension(dpy, info, False);
+
+    LockDisplay(dpy);
+    GetReq(XF86DRICreateDrawable, req);
+    req->reqType = info->codes->major_opcode;
+    req->driReqType = X_XF86DRICreateDrawable;
+    req->screen = screen;
+    req->drawable = drawable;
+    if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
+       UnlockDisplay(dpy);
+       SyncHandle();
+       TRACE("CreateDrawable... return False");
+       return False;
+    }
+    *hHWDrawable = rep.hHWDrawable;
+    UnlockDisplay(dpy);
+    SyncHandle();
+    TRACE("CreateDrawable... return True");
+    return True;
+}
+
+Bool
+uniDRIDestroyDrawable(Display * ndpy, int screen, Drawable drawable)
+{
+    Display *const dpy = (Display *) ndpy;
+    XExtDisplayInfo *info = find_display(dpy);
+    xXF86DRIDestroyDrawableReq *req;
+
+    TRACE("DestroyDrawable...");
+    uniDRICheckExtension(dpy, info, False);
+
+    LockDisplay(dpy);
+    GetReq(XF86DRIDestroyDrawable, req);
+    req->reqType = info->codes->major_opcode;
+    req->driReqType = X_XF86DRIDestroyDrawable;
+    req->screen = screen;
+    req->drawable = drawable;
+    UnlockDisplay(dpy);
+    SyncHandle();
+    TRACE("DestroyDrawable... return True");
+    return True;
+}
+
+Bool
+uniDRIGetDrawableInfo(Display * dpy, int screen, Drawable drawable,
+    unsigned int *index, unsigned int *stamp,
+    int *X, int *Y, int *W, int *H,
+    int *numClipRects, drm_clip_rect_t ** pClipRects,
+    int *backX, int *backY,
+    int *numBackClipRects, drm_clip_rect_t ** pBackClipRects)
+{
+    XExtDisplayInfo *info = find_display(dpy);
+    xXF86DRIGetDrawableInfoReply rep;
+    xXF86DRIGetDrawableInfoReq *req;
+    int total_rects;
+
+    TRACE("GetDrawableInfo...");
+    uniDRICheckExtension(dpy, info, False);
+
+    LockDisplay(dpy);
+    GetReq(XF86DRIGetDrawableInfo, req);
+    req->reqType = info->codes->major_opcode;
+    req->driReqType = X_XF86DRIGetDrawableInfo;
+    req->screen = screen;
+    req->drawable = drawable;
+
+    if (!_XReply(dpy, (xReply *) & rep, 1, xFalse)) {
+       UnlockDisplay(dpy);
+       SyncHandle();
+       TRACE("GetDrawableInfo... return False");
+       return False;
+    }
+    *index = rep.drawableTableIndex;
+    *stamp = rep.drawableTableStamp;
+    *X = (int)rep.drawableX;
+    *Y = (int)rep.drawableY;
+    *W = (int)rep.drawableWidth;
+    *H = (int)rep.drawableHeight;
+    *numClipRects = rep.numClipRects;
+    total_rects = *numClipRects;
+
+    *backX = rep.backX;
+    *backY = rep.backY;
+    *numBackClipRects = rep.numBackClipRects;
+    total_rects += *numBackClipRects;
+
+#if 0
+    /* Because of the fix in Xserver/GL/dri/xf86dri.c, this check breaks
+     * backwards compatibility (Because of the >> 2 shift) but the fix
+     * enables multi-threaded apps to work.
+     */
+    if (rep.length != ((((SIZEOF(xXF86DRIGetDrawableInfoReply) -
+                       SIZEOF(xGenericReply) +
+                       total_rects * sizeof(drm_clip_rect_t)) +
+                   3) & ~3) >> 2)) {
+       _XEatData(dpy, rep.length);
+       UnlockDisplay(dpy);
+       SyncHandle();
+       TRACE("GetDrawableInfo... return False");
+       return False;
+    }
+#endif
+
+    if (*numClipRects) {
+       int len = sizeof(drm_clip_rect_t) * (*numClipRects);
+
+       *pClipRects = (drm_clip_rect_t *) Xcalloc(len, 1);
+       if (*pClipRects)
+           _XRead(dpy, (char *)*pClipRects, len);
+    } else {
+       *pClipRects = NULL;
+    }
+
+    if (*numBackClipRects) {
+       int len = sizeof(drm_clip_rect_t) * (*numBackClipRects);
+
+       *pBackClipRects = (drm_clip_rect_t *) Xcalloc(len, 1);
+       if (*pBackClipRects)
+           _XRead(dpy, (char *)*pBackClipRects, len);
+    } else {
+       *pBackClipRects = NULL;
+    }
+
+    UnlockDisplay(dpy);
+    SyncHandle();
+    TRACE("GetDrawableInfo... return True");
+    return True;
+}
+
+Bool
+uniDRIGetDeviceInfo(dpy, screen, hFrameBuffer,
+    fbOrigin, fbSize, fbStride, devPrivateSize, pDevPrivate)
+    Display *dpy;
+    int screen;
+    drm_handle_t *hFrameBuffer;
+    int *fbOrigin;
+    int *fbSize;
+    int *fbStride;
+    int *devPrivateSize;
+    void **pDevPrivate;
+{
+    XExtDisplayInfo *info = find_display(dpy);
+    xXF86DRIGetDeviceInfoReply rep;
+    xXF86DRIGetDeviceInfoReq *req;
+
+    TRACE("GetDeviceInfo...");
+    uniDRICheckExtension(dpy, info, False);
+
+    LockDisplay(dpy);
+    GetReq(XF86DRIGetDeviceInfo, req);
+    req->reqType = info->codes->major_opcode;
+    req->driReqType = X_XF86DRIGetDeviceInfo;
+    req->screen = screen;
+    if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
+       UnlockDisplay(dpy);
+       SyncHandle();
+       TRACE("GetDeviceInfo... return False");
+       return False;
+    }
+
+    *hFrameBuffer = rep.hFrameBufferLow;
+#ifdef LONG64
+    if (sizeof(drm_handle_t) == 8) {
+       *hFrameBuffer |= ((unsigned long)rep.hFrameBufferHigh) << 32;
+    }
+#endif
+
+    *fbOrigin = rep.framebufferOrigin;
+    *fbSize = rep.framebufferSize;
+    *fbStride = rep.framebufferStride;
+    *devPrivateSize = rep.devPrivateSize;
+
+    if (rep.length) {
+       if (!(*pDevPrivate = (void *)Xcalloc(rep.devPrivateSize, 1))) {
+           _XEatData(dpy, ((rep.devPrivateSize + 3) & ~3));
+           UnlockDisplay(dpy);
+           SyncHandle();
+           TRACE("GetDeviceInfo... return False");
+           return False;
+       }
+       _XRead(dpy, (char *)*pDevPrivate, rep.devPrivateSize);
+    } else {
+       *pDevPrivate = NULL;
+    }
+
+    UnlockDisplay(dpy);
+    SyncHandle();
+    TRACE("GetDeviceInfo... return True");
+    return True;
+}
diff --git a/tests/ttmtest/src/xf86dri.h b/tests/ttmtest/src/xf86dri.h
new file mode 100644 (file)
index 0000000..8fb7896
--- /dev/null
@@ -0,0 +1,116 @@
+/* $XFree86: xc/lib/GL/dri/xf86dri.h,v 1.8 2002/10/30 12:51:25 alanh Exp $ */
+/**************************************************************************
+
+Copyright 1998-1999 Precision Insight, Inc., Cedar Park, Texas.
+Copyright 2000 VA Linux Systems, Inc.
+All Rights Reserved.
+
+Permission is hereby granted, free of charge, to any person obtaining a
+copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sub license, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice (including the
+next paragraph) shall be included in all copies or substantial portions
+of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
+ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+**************************************************************************/
+
+/**
+ * \file xf86dri.h
+ * Protocol numbers and function prototypes for DRI X protocol.
+ *
+ * \author Kevin E. Martin <martin@valinux.com>
+ * \author Jens Owen <jens@tungstengraphics.com>
+ * \author Rickard E. (Rik) Faith <faith@valinux.com>
+ */
+
+#ifndef _XF86DRI_H_
+#define _XF86DRI_H_
+
+#include <X11/Xfuncproto.h>
+#include <drm/drm.h>
+
+#define X_XF86DRIQueryVersion                  0
+#define X_XF86DRIQueryDirectRenderingCapable   1
+#define X_XF86DRIOpenConnection                        2
+#define X_XF86DRICloseConnection               3
+#define X_XF86DRIGetClientDriverName           4
+#define X_XF86DRICreateContext                 5
+#define X_XF86DRIDestroyContext                        6
+#define X_XF86DRICreateDrawable                        7
+#define X_XF86DRIDestroyDrawable               8
+#define X_XF86DRIGetDrawableInfo               9
+#define X_XF86DRIGetDeviceInfo                 10
+#define X_XF86DRIAuthConnection                 11
+#define X_XF86DRIOpenFullScreen                 12     /* Deprecated */
+#define X_XF86DRICloseFullScreen                13     /* Deprecated */
+
+#define XF86DRINumberEvents            0
+
+#define XF86DRIClientNotLocal          0
+#define XF86DRIOperationNotSupported   1
+#define XF86DRINumberErrors            (XF86DRIOperationNotSupported + 1)
+
+#ifndef _XF86DRI_SERVER_
+
+_XFUNCPROTOBEGIN
+    Bool uniDRIQueryExtension(Display * dpy, int *event_base,
+    int *error_base);
+
+Bool uniDRIQueryVersion(Display * dpy, int *majorVersion, int *minorVersion,
+    int *patchVersion);
+
+Bool uniDRIQueryDirectRenderingCapable(Display * dpy, int screen,
+    Bool * isCapable);
+
+Bool uniDRIOpenConnection(Display * dpy, int screen, drm_handle_t * hSAREA,
+    char **busIDString);
+
+Bool uniDRIAuthConnection(Display * dpy, int screen, drm_magic_t magic);
+
+Bool uniDRICloseConnection(Display * dpy, int screen);
+
+Bool uniDRIGetClientDriverName(Display * dpy, int screen,
+    int *ddxDriverMajorVersion, int *ddxDriverMinorVersion,
+    int *ddxDriverPatchVersion, char **clientDriverName);
+
+Bool uniDRICreateContext(Display * dpy, int screen, Visual * visual,
+    XID * ptr_to_returned_context_id, drm_context_t * hHWContext);
+
+Bool uniDRICreateContextWithConfig(Display * dpy, int screen, int configID,
+    XID * ptr_to_returned_context_id, drm_context_t * hHWContext);
+
+extern Bool uniDRIDestroyContext(Display * dpy, int screen, XID context_id);
+
+extern Bool uniDRICreateDrawable(Display * dpy, int screen,
+    Drawable drawable, drm_drawable_t * hHWDrawable);
+
+extern Bool uniDRIDestroyDrawable(Display * dpy, int screen,
+    Drawable drawable);
+
+Bool uniDRIGetDrawableInfo(Display * dpy, int screen, Drawable drawable,
+    unsigned int *index, unsigned int *stamp,
+    int *X, int *Y, int *W, int *H,
+    int *numClipRects, drm_clip_rect_t ** pClipRects,
+    int *backX, int *backY,
+    int *numBackClipRects, drm_clip_rect_t ** pBackClipRects);
+
+Bool uniDRIGetDeviceInfo(Display * dpy, int screen,
+    drm_handle_t * hFrameBuffer, int *fbOrigin, int *fbSize,
+    int *fbStride, int *devPrivateSize, void **pDevPrivate);
+
+_XFUNCPROTOEND
+#endif /* _XF86DRI_SERVER_ */
+#endif /* _XF86DRI_H_ */
diff --git a/tests/ttmtest/src/xf86dristr.h b/tests/ttmtest/src/xf86dristr.h
new file mode 100644 (file)
index 0000000..3b43438
--- /dev/null
@@ -0,0 +1,390 @@
+/* $XFree86: xc/lib/GL/dri/xf86dristr.h,v 1.10 2002/10/30 12:51:25 alanh Exp $ */
+/**************************************************************************
+
+Copyright 1998-1999 Precision Insight, Inc., Cedar Park, Texas.
+Copyright 2000 VA Linux Systems, Inc.
+All Rights Reserved.
+
+Permission is hereby granted, free of charge, to any person obtaining a
+copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sub license, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice (including the
+next paragraph) shall be included in all copies or substantial portions
+of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
+ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+**************************************************************************/
+
+/*
+ * Authors:
+ *   Kevin E. Martin <martin@valinux.com>
+ *   Jens Owen <jens@tungstengraphics.com>
+ *   Rickard E. (Rik) Fiath <faith@valinux.com>
+ *
+ */
+
+#ifndef _XF86DRISTR_H_
+#define _XF86DRISTR_H_
+
+#include "xf86dri.h"
+
+#define XF86DRINAME "XFree86-DRI"
+
+/* The DRI version number.  This was originally set to be the same of the
+ * XFree86 version number.  However, this version is really indepedent of
+ * the XFree86 version.
+ *
+ * Version History:
+ *    4.0.0: Original
+ *    4.0.1: Patch to bump clipstamp when windows are destroyed, 28 May 02
+ *    4.1.0: Add transition from single to multi in DRMInfo rec, 24 Jun 02
+ */
+#define XF86DRI_MAJOR_VERSION  4
+#define XF86DRI_MINOR_VERSION  1
+#define XF86DRI_PATCH_VERSION  0
+
+typedef struct _XF86DRIQueryVersion
+{
+    CARD8 reqType;                    /* always DRIReqCode */
+    CARD8 driReqType;                 /* always X_DRIQueryVersion */
+    CARD16 length B16;
+} xXF86DRIQueryVersionReq;
+
+#define sz_xXF86DRIQueryVersionReq     4
+
+typedef struct
+{
+    BYTE type;                        /* X_Reply */
+    BOOL pad1;
+    CARD16 sequenceNumber B16;
+    CARD32 length B32;
+    CARD16 majorVersion B16;          /* major version of DRI protocol */
+    CARD16 minorVersion B16;          /* minor version of DRI protocol */
+    CARD32 patchVersion B32;          /* patch version of DRI protocol */
+    CARD32 pad3 B32;
+    CARD32 pad4 B32;
+    CARD32 pad5 B32;
+    CARD32 pad6 B32;
+} xXF86DRIQueryVersionReply;
+
+#define sz_xXF86DRIQueryVersionReply   32
+
+typedef struct _XF86DRIQueryDirectRenderingCapable
+{
+    CARD8 reqType;                    /* always DRIReqCode */
+    CARD8 driReqType;                 /* X_DRIQueryDirectRenderingCapable */
+    CARD16 length B16;
+    CARD32 screen B32;
+} xXF86DRIQueryDirectRenderingCapableReq;
+
+#define sz_xXF86DRIQueryDirectRenderingCapableReq      8
+
+typedef struct
+{
+    BYTE type;                        /* X_Reply */
+    BOOL pad1;
+    CARD16 sequenceNumber B16;
+    CARD32 length B32;
+    BOOL isCapable;
+    BOOL pad2;
+    BOOL pad3;
+    BOOL pad4;
+    CARD32 pad5 B32;
+    CARD32 pad6 B32;
+    CARD32 pad7 B32;
+    CARD32 pad8 B32;
+    CARD32 pad9 B32;
+} xXF86DRIQueryDirectRenderingCapableReply;
+
+#define sz_xXF86DRIQueryDirectRenderingCapableReply    32
+
+typedef struct _XF86DRIOpenConnection
+{
+    CARD8 reqType;                    /* always DRIReqCode */
+    CARD8 driReqType;                 /* always X_DRIOpenConnection */
+    CARD16 length B16;
+    CARD32 screen B32;
+} xXF86DRIOpenConnectionReq;
+
+#define sz_xXF86DRIOpenConnectionReq   8
+
+typedef struct
+{
+    BYTE type;                        /* X_Reply */
+    BOOL pad1;
+    CARD16 sequenceNumber B16;
+    CARD32 length B32;
+    CARD32 hSAREALow B32;
+    CARD32 hSAREAHigh B32;
+    CARD32 busIdStringLength B32;
+    CARD32 pad6 B32;
+    CARD32 pad7 B32;
+    CARD32 pad8 B32;
+} xXF86DRIOpenConnectionReply;
+
+#define sz_xXF86DRIOpenConnectionReply 32
+
+typedef struct _XF86DRIAuthConnection
+{
+    CARD8 reqType;                    /* always DRIReqCode */
+    CARD8 driReqType;                 /* always X_DRICloseConnection */
+    CARD16 length B16;
+    CARD32 screen B32;
+    CARD32 magic B32;
+} xXF86DRIAuthConnectionReq;
+
+#define sz_xXF86DRIAuthConnectionReq   12
+
+typedef struct
+{
+    BYTE type;
+    BOOL pad1;
+    CARD16 sequenceNumber B16;
+    CARD32 length B32;
+    CARD32 authenticated B32;
+    CARD32 pad2 B32;
+    CARD32 pad3 B32;
+    CARD32 pad4 B32;
+    CARD32 pad5 B32;
+    CARD32 pad6 B32;
+} xXF86DRIAuthConnectionReply;
+
+#define zx_xXF86DRIAuthConnectionReply  32
+
+typedef struct _XF86DRICloseConnection
+{
+    CARD8 reqType;                    /* always DRIReqCode */
+    CARD8 driReqType;                 /* always X_DRICloseConnection */
+    CARD16 length B16;
+    CARD32 screen B32;
+} xXF86DRICloseConnectionReq;
+
+#define sz_xXF86DRICloseConnectionReq  8
+
+typedef struct _XF86DRIGetClientDriverName
+{
+    CARD8 reqType;                    /* always DRIReqCode */
+    CARD8 driReqType;                 /* always X_DRIGetClientDriverName */
+    CARD16 length B16;
+    CARD32 screen B32;
+} xXF86DRIGetClientDriverNameReq;
+
+#define sz_xXF86DRIGetClientDriverNameReq      8
+
+typedef struct
+{
+    BYTE type;                        /* X_Reply */
+    BOOL pad1;
+    CARD16 sequenceNumber B16;
+    CARD32 length B32;
+    CARD32 ddxDriverMajorVersion B32;
+    CARD32 ddxDriverMinorVersion B32;
+    CARD32 ddxDriverPatchVersion B32;
+    CARD32 clientDriverNameLength B32;
+    CARD32 pad5 B32;
+    CARD32 pad6 B32;
+} xXF86DRIGetClientDriverNameReply;
+
+#define sz_xXF86DRIGetClientDriverNameReply    32
+
+typedef struct _XF86DRICreateContext
+{
+    CARD8 reqType;                    /* always DRIReqCode */
+    CARD8 driReqType;                 /* always X_DRICreateContext */
+    CARD16 length B16;
+    CARD32 screen B32;
+    CARD32 visual B32;
+    CARD32 context B32;
+} xXF86DRICreateContextReq;
+
+#define sz_xXF86DRICreateContextReq    16
+
+typedef struct
+{
+    BYTE type;                        /* X_Reply */
+    BOOL pad1;
+    CARD16 sequenceNumber B16;
+    CARD32 length B32;
+    CARD32 hHWContext B32;
+    CARD32 pad2 B32;
+    CARD32 pad3 B32;
+    CARD32 pad4 B32;
+    CARD32 pad5 B32;
+    CARD32 pad6 B32;
+} xXF86DRICreateContextReply;
+
+#define sz_xXF86DRICreateContextReply  32
+
+typedef struct _XF86DRIDestroyContext
+{
+    CARD8 reqType;                    /* always DRIReqCode */
+    CARD8 driReqType;                 /* always X_DRIDestroyContext */
+    CARD16 length B16;
+    CARD32 screen B32;
+    CARD32 context B32;
+} xXF86DRIDestroyContextReq;
+
+#define sz_xXF86DRIDestroyContextReq   12
+
+typedef struct _XF86DRICreateDrawable
+{
+    CARD8 reqType;                    /* always DRIReqCode */
+    CARD8 driReqType;                 /* always X_DRICreateDrawable */
+    CARD16 length B16;
+    CARD32 screen B32;
+    CARD32 drawable B32;
+} xXF86DRICreateDrawableReq;
+
+#define sz_xXF86DRICreateDrawableReq   12
+
+typedef struct
+{
+    BYTE type;                        /* X_Reply */
+    BOOL pad1;
+    CARD16 sequenceNumber B16;
+    CARD32 length B32;
+    CARD32 hHWDrawable B32;
+    CARD32 pad2 B32;
+    CARD32 pad3 B32;
+    CARD32 pad4 B32;
+    CARD32 pad5 B32;
+    CARD32 pad6 B32;
+} xXF86DRICreateDrawableReply;
+
+#define sz_xXF86DRICreateDrawableReply 32
+
+typedef struct _XF86DRIDestroyDrawable
+{
+    CARD8 reqType;                    /* always DRIReqCode */
+    CARD8 driReqType;                 /* always X_DRIDestroyDrawable */
+    CARD16 length B16;
+    CARD32 screen B32;
+    CARD32 drawable B32;
+} xXF86DRIDestroyDrawableReq;
+
+#define sz_xXF86DRIDestroyDrawableReq  12
+
+typedef struct _XF86DRIGetDrawableInfo
+{
+    CARD8 reqType;                    /* always DRIReqCode */
+    CARD8 driReqType;                 /* always X_DRIGetDrawableInfo */
+    CARD16 length B16;
+    CARD32 screen B32;
+    CARD32 drawable B32;
+} xXF86DRIGetDrawableInfoReq;
+
+#define sz_xXF86DRIGetDrawableInfoReq  12
+
+typedef struct
+{
+    BYTE type;                        /* X_Reply */
+    BOOL pad1;
+    CARD16 sequenceNumber B16;
+    CARD32 length B32;
+    CARD32 drawableTableIndex B32;
+    CARD32 drawableTableStamp B32;
+    INT16 drawableX B16;
+    INT16 drawableY B16;
+    INT16 drawableWidth B16;
+    INT16 drawableHeight B16;
+    CARD32 numClipRects B32;
+    INT16 backX B16;
+    INT16 backY B16;
+    CARD32 numBackClipRects B32;
+} xXF86DRIGetDrawableInfoReply;
+
+#define sz_xXF86DRIGetDrawableInfoReply        36
+
+typedef struct _XF86DRIGetDeviceInfo
+{
+    CARD8 reqType;                    /* always DRIReqCode */
+    CARD8 driReqType;                 /* always X_DRIGetDeviceInfo */
+    CARD16 length B16;
+    CARD32 screen B32;
+} xXF86DRIGetDeviceInfoReq;
+
+#define sz_xXF86DRIGetDeviceInfoReq    8
+
+typedef struct
+{
+    BYTE type;                        /* X_Reply */
+    BOOL pad1;
+    CARD16 sequenceNumber B16;
+    CARD32 length B32;
+    CARD32 hFrameBufferLow B32;
+    CARD32 hFrameBufferHigh B32;
+    CARD32 framebufferOrigin B32;
+    CARD32 framebufferSize B32;
+    CARD32 framebufferStride B32;
+    CARD32 devPrivateSize B32;
+} xXF86DRIGetDeviceInfoReply;
+
+#define sz_xXF86DRIGetDeviceInfoReply  32
+
+typedef struct _XF86DRIOpenFullScreen
+{
+    CARD8 reqType;                    /* always DRIReqCode */
+    CARD8 driReqType;                 /* always X_DRIOpenFullScreen */
+    CARD16 length B16;
+    CARD32 screen B32;
+    CARD32 drawable B32;
+} xXF86DRIOpenFullScreenReq;
+
+#define sz_xXF86DRIOpenFullScreenReq    12
+
+typedef struct
+{
+    BYTE type;
+    BOOL pad1;
+    CARD16 sequenceNumber B16;
+    CARD32 length B32;
+    CARD32 isFullScreen B32;
+    CARD32 pad2 B32;
+    CARD32 pad3 B32;
+    CARD32 pad4 B32;
+    CARD32 pad5 B32;
+    CARD32 pad6 B32;
+} xXF86DRIOpenFullScreenReply;
+
+#define sz_xXF86DRIOpenFullScreenReply  32
+
+typedef struct _XF86DRICloseFullScreen
+{
+    CARD8 reqType;                    /* always DRIReqCode */
+    CARD8 driReqType;                 /* always X_DRICloseFullScreen */
+    CARD16 length B16;
+    CARD32 screen B32;
+    CARD32 drawable B32;
+} xXF86DRICloseFullScreenReq;
+
+#define sz_xXF86DRICloseFullScreenReq   12
+
+typedef struct
+{
+    BYTE type;
+    BOOL pad1;
+    CARD16 sequenceNumber B16;
+    CARD32 length B32;
+    CARD32 pad2 B32;
+    CARD32 pad3 B32;
+    CARD32 pad4 B32;
+    CARD32 pad5 B32;
+    CARD32 pad6 B32;
+    CARD32 pad7 B32;
+} xXF86DRICloseFullScreenReply;
+
+#define sz_xXF86DRICloseFullScreenReply  32
+
+#endif /* _XF86DRISTR_H_ */
diff --git a/tests/updatedraw.c b/tests/updatedraw.c
new file mode 100644 (file)
index 0000000..a61eb15
--- /dev/null
@@ -0,0 +1,153 @@
+/*
+ * Copyright Â© 2007 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Authors:
+ *    Eric Anholt <eric@anholt.net>
+ *
+ */
+
+#include "drmtest.h"
+
+static void
+set_draw_cliprects_empty(int fd, int drawable)
+{
+       int ret;
+       struct drm_update_draw update;
+
+       update.handle = drawable;
+       update.type = DRM_DRAWABLE_CLIPRECTS;
+       update.num = 0;
+       update.data = 0;
+
+       ret = ioctl(fd, DRM_IOCTL_UPDATE_DRAW, &update);
+       assert(ret == 0);
+}
+
+static void
+set_draw_cliprects_empty_fail(int fd, int drawable)
+{
+       int ret;
+       struct drm_update_draw update;
+
+       update.handle = drawable;
+       update.type = DRM_DRAWABLE_CLIPRECTS;
+       update.num = 0;
+       update.data = 0;
+
+       ret = ioctl(fd, DRM_IOCTL_UPDATE_DRAW, &update);
+       assert(ret == -1 && errno == EINVAL);
+}
+
+static void
+set_draw_cliprects_2(int fd, int drawable)
+{
+       int ret;
+       struct drm_update_draw update;
+       drm_clip_rect_t rects[2];
+
+       rects[0].x1 = 0;
+       rects[0].y1 = 0;
+       rects[0].x2 = 10;
+       rects[0].y2 = 10;
+
+       rects[1].x1 = 10;
+       rects[1].y1 = 10;
+       rects[1].x2 = 20;
+       rects[1].y2 = 20;
+
+       update.handle = drawable;
+       update.type = DRM_DRAWABLE_CLIPRECTS;
+       update.num = 2;
+       update.data = (unsigned long long)(uintptr_t)&rects;
+
+       ret = ioctl(fd, DRM_IOCTL_UPDATE_DRAW, &update);
+       assert(ret == 0);
+}
+
+static int add_drawable(int fd)
+{
+       drm_draw_t drawarg;
+       int ret;
+
+       /* Create a drawable.
+        * IOCTL_ADD_DRAW is RDWR, though it should really just be RD
+        */
+       drawarg.handle = 0;
+       ret = ioctl(fd, DRM_IOCTL_ADD_DRAW, &drawarg);
+       assert(ret == 0);
+       return drawarg.handle;
+}
+
+static int rm_drawable(int fd, int drawable, int fail)
+{
+       drm_draw_t drawarg;
+       int ret;
+
+       /* Create a drawable.
+        * IOCTL_ADD_DRAW is RDWR, though it should really just be RD
+        */
+       drawarg.handle = drawable;
+       ret = ioctl(fd, DRM_IOCTL_RM_DRAW, &drawarg);
+       if (!fail)
+               assert(ret == 0);
+       else
+               assert(ret == -1 && errno == EINVAL);
+
+       return drawarg.handle;
+}
+
+/**
+ * Tests drawable management: adding, removing, and updating the cliprects of
+ * drawables.
+ */
+int main(int argc, char **argv)
+{
+       int fd, ret, d1, d2;
+
+       if (getuid() != 0) {
+               fprintf(stderr, "updatedraw test requires root, skipping\n");
+               return 0;
+       }
+
+       fd = drm_open_any_master();
+
+       d1 = add_drawable(fd);
+       d2 = add_drawable(fd);
+       /* Do a series of cliprect updates */
+       set_draw_cliprects_empty(fd, d1);
+       set_draw_cliprects_empty(fd, d2);
+       set_draw_cliprects_2(fd, d1);
+       set_draw_cliprects_empty(fd, d1);
+
+       /* Remove our drawables */
+       rm_drawable(fd, d1, 0);
+       rm_drawable(fd, d2, 0);
+
+       /* Check that removing an unknown drawable returns error */
+       rm_drawable(fd, 0x7fffffff, 1);
+
+       /* Attempt to set cliprects on a nonexistent drawable */
+       set_draw_cliprects_empty_fail(fd, d1);
+
+       close(fd);
+       return 0;
+}
diff --git a/tests/vbltest/Makefile.am b/tests/vbltest/Makefile.am
new file mode 100644 (file)
index 0000000..77f9037
--- /dev/null
@@ -0,0 +1,11 @@
+AM_CFLAGS = \
+       -I$(top_srcdir)/include/drm \
+       -I$(top_srcdir)
+
+noinst_PROGRAMS = \
+       vbltest
+
+vbltest_SOURCES = \
+       vbltest.c
+vbltest_LDADD = \
+       $(top_builddir)/libdrm.la
diff --git a/tests/vbltest/vbltest.c b/tests/vbltest/vbltest.c
new file mode 100644 (file)
index 0000000..20dae78
--- /dev/null
@@ -0,0 +1,200 @@
+/*
+ * DRM based mode setting test program
+ * Copyright 2008 Tungsten Graphics
+ *   Jakob Bornecrantz <jakob@tungstengraphics.com>
+ * Copyright 2008 Intel Corporation
+ *   Jesse Barnes <jesse.barnes@intel.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+/*
+ * This fairly simple test program dumps output in a similar format to the
+ * "xrandr" tool everyone knows & loves.  It's necessarily slightly different
+ * since the kernel separates outputs into encoder and connector structures,
+ * each with their own unique ID.  The program also allows test testing of the
+ * memory management and mode setting APIs by allowing the user to specify a
+ * connector and mode to use for mode setting.  If all works as expected, a
+ * blue background should be painted on the monitor attached to the specified
+ * connector after the selected mode is set.
+ *
+ * TODO: use cairo to write the mode info on the selected output once
+ *       the mode has been programmed, along with possible test patterns.
+ */
+#include "config.h"
+
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/poll.h>
+#include <sys/time.h>
+
+#include "xf86drm.h"
+#include "xf86drmMode.h"
+
+#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
+
+extern char *optarg;
+extern int optind, opterr, optopt;
+static char optstr[] = "s";
+
+int secondary = 0;
+
+struct vbl_info {
+       unsigned int vbl_count;
+       struct timeval start;
+};
+
+static void vblank_handler(int fd, unsigned int frame, unsigned int sec,
+                          unsigned int usec, void *data)
+{
+       drmVBlank vbl;
+       struct timeval end;
+       struct vbl_info *info = data;
+       double t;
+
+       vbl.request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT;
+       if (secondary)
+               vbl.request.type |= DRM_VBLANK_SECONDARY;
+       vbl.request.sequence = 1;
+       vbl.request.signal = (unsigned long)data;
+
+       drmWaitVBlank(fd, &vbl);
+
+       info->vbl_count++;
+
+       if (info->vbl_count == 60) {
+               gettimeofday(&end, NULL);
+               t = end.tv_sec + end.tv_usec * 1e-6 -
+                       (info->start.tv_sec + info->start.tv_usec * 1e-6);
+               fprintf(stderr, "freq: %.02fHz\n", info->vbl_count / t);
+               info->vbl_count = 0;
+               info->start = end;
+       }
+}
+
+static void usage(char *name)
+{
+       fprintf(stderr, "usage: %s [-s]\n", name);
+       fprintf(stderr, "\t-s\tuse secondary pipe\n");
+       exit(0);
+}
+
+int main(int argc, char **argv)
+{
+       int i, c, fd, ret;
+       char *modules[] = { "i915", "radeon", "nouveau", "vmwgfx", "exynos-drm" };
+       drmVBlank vbl;
+       drmEventContext evctx;
+       struct vbl_info handler_info;
+
+       opterr = 0;
+       while ((c = getopt(argc, argv, optstr)) != -1) {
+               switch (c) {
+               case 's':
+                       secondary = 1;
+                       break;
+               default:
+                       usage(argv[0]);
+                       break;
+               }
+       }
+
+       for (i = 0; i < ARRAY_SIZE(modules); i++) {
+               printf("trying to load module %s...", modules[i]);
+               fd = drmOpen(modules[i], NULL);
+               if (fd < 0) {
+                       printf("failed.\n");
+               } else {
+                       printf("success.\n");
+                       break;
+               }
+       }
+
+       if (i == ARRAY_SIZE(modules)) {
+               fprintf(stderr, "failed to load any modules, aborting.\n");
+               return -1;
+       }
+
+       /* Get current count first */
+       vbl.request.type = DRM_VBLANK_RELATIVE;
+       if (secondary)
+               vbl.request.type |= DRM_VBLANK_SECONDARY;
+       vbl.request.sequence = 0;
+       ret = drmWaitVBlank(fd, &vbl);
+       if (ret != 0) {
+               printf("drmWaitVBlank (relative) failed ret: %i\n", ret);
+               return -1;
+       }
+
+       printf("starting count: %d\n", vbl.request.sequence);
+
+       handler_info.vbl_count = 0;
+       gettimeofday(&handler_info.start, NULL);
+
+       /* Queue an event for frame + 1 */
+       vbl.request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT;
+       if (secondary)
+               vbl.request.type |= DRM_VBLANK_SECONDARY;
+       vbl.request.sequence = 1;
+       vbl.request.signal = (unsigned long)&handler_info;
+       ret = drmWaitVBlank(fd, &vbl);
+       if (ret != 0) {
+               printf("drmWaitVBlank (relative, event) failed ret: %i\n", ret);
+               return -1;
+       }
+
+       /* Set up our event handler */
+       memset(&evctx, 0, sizeof evctx);
+       evctx.version = DRM_EVENT_CONTEXT_VERSION;
+       evctx.vblank_handler = vblank_handler;
+       evctx.page_flip_handler = NULL;
+
+       /* Poll for events */
+       while (1) {
+               struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
+               fd_set fds;
+               int ret;
+
+               FD_ZERO(&fds);
+               FD_SET(0, &fds);
+               FD_SET(fd, &fds);
+               ret = select(fd + 1, &fds, NULL, NULL, &timeout);
+
+               if (ret <= 0) {
+                       fprintf(stderr, "select timed out or error (ret %d)\n",
+                               ret);
+                       continue;
+               } else if (FD_ISSET(0, &fds)) {
+                       break;
+               }
+
+               ret = drmHandleEvent(fd, &evctx);
+               if (ret != 0) {
+                       printf("drmHandleEvent failed: %i\n", ret);
+                       return -1;
+               }
+       }
+
+       return 0;
+}
diff --git a/xf86atomic.h b/xf86atomic.h
new file mode 100644 (file)
index 0000000..db2f619
--- /dev/null
@@ -0,0 +1,99 @@
+/*
+ * Copyright Â© 2009 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Authors:
+ *    Chris Wilson <chris@chris-wilson.co.uk>
+ *
+ */
+
+/**
+ * @file xf86atomics.h
+ *
+ * Private definitions for atomic operations
+ */
+
+#ifndef LIBDRM_ATOMICS_H
+#define LIBDRM_ATOMICS_H
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#if HAVE_LIBDRM_ATOMIC_PRIMITIVES
+
+#define HAS_ATOMIC_OPS 1
+
+typedef struct {
+       int atomic;
+} atomic_t;
+
+# define atomic_read(x) ((x)->atomic)
+# define atomic_set(x, val) ((x)->atomic = (val))
+# define atomic_inc(x) ((void) __sync_fetch_and_add (&(x)->atomic, 1))
+# define atomic_dec_and_test(x) (__sync_fetch_and_add (&(x)->atomic, -1) == 1)
+# define atomic_add(x, v) ((void) __sync_add_and_fetch(&(x)->atomic, (v)))
+# define atomic_dec(x, v) ((void) __sync_sub_and_fetch(&(x)->atomic, (v)))
+# define atomic_cmpxchg(x, oldv, newv) __sync_val_compare_and_swap (&(x)->atomic, oldv, newv)
+
+#endif
+
+#if HAVE_LIB_ATOMIC_OPS
+#include <atomic_ops.h>
+
+#define HAS_ATOMIC_OPS 1
+
+typedef struct {
+       AO_t atomic;
+} atomic_t;
+
+# define atomic_read(x) AO_load_full(&(x)->atomic)
+# define atomic_set(x, val) AO_store_full(&(x)->atomic, (val))
+# define atomic_inc(x) ((void) AO_fetch_and_add1_full(&(x)->atomic))
+# define atomic_add(x, v) ((void) AO_fetch_and_add_full(&(x)->atomic, (v)))
+# define atomic_dec(x, v) ((void) AO_fetch_and_add_full(&(x)->atomic, -(v)))
+# define atomic_dec_and_test(x) (AO_fetch_and_sub1_full(&(x)->atomic) == 1)
+# define atomic_cmpxchg(x, oldv, newv) AO_compare_and_swap_full(&(x)->atomic, oldv, newv)
+
+#endif
+
+#if defined(__sun) && !defined(HAS_ATOMIC_OPS)  /* Solaris & OpenSolaris */
+
+#include <sys/atomic.h>
+#define HAS_ATOMIC_OPS 1
+
+typedef struct { uint_t atomic; } atomic_t;
+
+# define atomic_read(x) (int) ((x)->atomic)
+# define atomic_set(x, val) ((x)->atomic = (uint_t)(val))
+# define atomic_inc(x) (atomic_inc_uint (&(x)->atomic))
+# define atomic_dec_and_test(x) (atomic_dec_uint_nv(&(x)->atomic) == 1)
+# define atomic_add(x, v) (atomic_add_int(&(x)->atomic, (v)))
+# define atomic_dec(x, v) (atomic_add_int(&(x)->atomic, -(v)))
+# define atomic_cmpxchg(x, oldv, newv) atomic_cas_uint (&(x)->atomic, oldv, newv)
+
+#endif
+
+#if ! HAS_ATOMIC_OPS
+#error libdrm requires atomic operations, please define them for your CPU/compiler.
+#endif
+
+#endif
diff --git a/xf86drm.c b/xf86drm.c
new file mode 100644 (file)
index 0000000..6ea068f
--- /dev/null
+++ b/xf86drm.c
@@ -0,0 +1,2544 @@
+/**
+ * \file xf86drm.c 
+ * User-level interface to DRM device
+ *
+ * \author Rickard E. (Rik) Faith <faith@valinux.com>
+ * \author Kevin E. Martin <martin@valinux.com>
+ */
+
+/*
+ * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
+ * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <strings.h>
+#include <ctype.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <signal.h>
+#include <time.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#define stat_t struct stat
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/time.h>
+#include <stdarg.h>
+
+/* Not all systems have MAP_FAILED defined */
+#ifndef MAP_FAILED
+#define MAP_FAILED ((void *)-1)
+#endif
+
+#include "xf86drm.h"
+
+#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
+#define DRM_MAJOR 145
+#endif
+
+#ifdef __NetBSD__
+#define DRM_MAJOR 34
+#endif
+
+# ifdef __OpenBSD__
+#  define DRM_MAJOR 81
+# endif
+
+#ifndef DRM_MAJOR
+#define DRM_MAJOR 226          /* Linux */
+#endif
+
+/*
+ * This definition needs to be changed on some systems if dev_t is a structure.
+ * If there is a header file we can get it from, there would be best.
+ */
+#ifndef makedev
+#define makedev(x,y)    ((dev_t)(((x) << 8) | (y)))
+#endif
+
+#define DRM_MSG_VERBOSITY 3
+
+#define DRM_NODE_CONTROL 0
+#define DRM_NODE_RENDER 1
+
+static drmServerInfoPtr drm_server_info;
+
+void drmSetServerInfo(drmServerInfoPtr info)
+{
+    drm_server_info = info;
+}
+
+/**
+ * Output a message to stderr.
+ *
+ * \param format printf() like format string.
+ *
+ * \internal
+ * This function is a wrapper around vfprintf().
+ */
+
+static int drmDebugPrint(const char *format, va_list ap)
+{
+    return vfprintf(stderr, format, ap);
+}
+
+static int (*drm_debug_print)(const char *format, va_list ap) = drmDebugPrint;
+
+void
+drmMsg(const char *format, ...)
+{
+    va_list    ap;
+    const char *env;
+    if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) || drm_server_info)
+    {
+       va_start(ap, format);
+       if (drm_server_info) {
+         drm_server_info->debug_print(format,ap);
+       } else {
+         drm_debug_print(format, ap);
+       }
+       va_end(ap);
+    }
+}
+
+void
+drmSetDebugMsgFunction(int (*debug_msg_ptr)(const char *format, va_list ap))
+{
+    drm_debug_print = debug_msg_ptr;
+}
+
+static void *drmHashTable = NULL; /* Context switch callbacks */
+
+void *drmGetHashTable(void)
+{
+    return drmHashTable;
+}
+
+void *drmMalloc(int size)
+{
+    void *pt;
+    if ((pt = malloc(size)))
+       memset(pt, 0, size);
+    return pt;
+}
+
+void drmFree(void *pt)
+{
+    if (pt)
+       free(pt);
+}
+
+/**
+ * Call ioctl, restarting if it is interupted
+ */
+int
+drmIoctl(int fd, unsigned long request, void *arg)
+{
+    int        ret;
+
+    do {
+       ret = ioctl(fd, request, arg);
+    } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
+    return ret;
+}
+
+static unsigned long drmGetKeyFromFd(int fd)
+{
+    stat_t     st;
+
+    st.st_rdev = 0;
+    fstat(fd, &st);
+    return st.st_rdev;
+}
+
+drmHashEntry *drmGetEntry(int fd)
+{
+    unsigned long key = drmGetKeyFromFd(fd);
+    void          *value;
+    drmHashEntry  *entry;
+
+    if (!drmHashTable)
+       drmHashTable = drmHashCreate();
+
+    if (drmHashLookup(drmHashTable, key, &value)) {
+       entry           = drmMalloc(sizeof(*entry));
+       entry->fd       = fd;
+       entry->f        = NULL;
+       entry->tagTable = drmHashCreate();
+       drmHashInsert(drmHashTable, key, entry);
+    } else {
+       entry = value;
+    }
+    return entry;
+}
+
+/**
+ * Compare two busid strings
+ *
+ * \param first
+ * \param second
+ *
+ * \return 1 if matched.
+ *
+ * \internal
+ * This function compares two bus ID strings.  It understands the older
+ * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format.  In the format, o is
+ * domain, b is bus, d is device, f is function.
+ */
+static int drmMatchBusID(const char *id1, const char *id2, int pci_domain_ok)
+{
+    /* First, check if the IDs are exactly the same */
+    if (strcasecmp(id1, id2) == 0)
+       return 1;
+
+    /* Try to match old/new-style PCI bus IDs. */
+    if (strncasecmp(id1, "pci", 3) == 0) {
+       unsigned int o1, b1, d1, f1;
+       unsigned int o2, b2, d2, f2;
+       int ret;
+
+       ret = sscanf(id1, "pci:%04x:%02x:%02x.%u", &o1, &b1, &d1, &f1);
+       if (ret != 4) {
+           o1 = 0;
+           ret = sscanf(id1, "PCI:%u:%u:%u", &b1, &d1, &f1);
+           if (ret != 3)
+               return 0;
+       }
+
+       ret = sscanf(id2, "pci:%04x:%02x:%02x.%u", &o2, &b2, &d2, &f2);
+       if (ret != 4) {
+           o2 = 0;
+           ret = sscanf(id2, "PCI:%u:%u:%u", &b2, &d2, &f2);
+           if (ret != 3)
+               return 0;
+       }
+
+       /* If domains aren't properly supported by the kernel interface,
+        * just ignore them, which sucks less than picking a totally random
+        * card with "open by name"
+        */
+       if (!pci_domain_ok)
+               o1 = o2 = 0;
+
+       if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
+           return 0;
+       else
+           return 1;
+    }
+    return 0;
+}
+
+/**
+ * Handles error checking for chown call.
+ *
+ * \param path to file.
+ * \param id of the new owner.
+ * \param id of the new group.
+ *
+ * \return zero if success or -1 if failure.
+ *
+ * \internal
+ * Checks for failure. If failure was caused by signal call chown again.
+ * If any other failure happened then it will output error mesage using
+ * drmMsg() call.
+ */
+static int chown_check_return(const char *path, uid_t owner, gid_t group)
+{
+       int rv;
+
+       do {
+               rv = chown(path, owner, group);
+       } while (rv != 0 && errno == EINTR);
+
+       if (rv == 0)
+               return 0;
+
+       drmMsg("Failed to change owner or group for file %s! %d: %s\n",
+                       path, errno, strerror(errno));
+       return -1;
+}
+
+/**
+ * Open the DRM device, creating it if necessary.
+ *
+ * \param dev major and minor numbers of the device.
+ * \param minor minor number of the device.
+ * 
+ * \return a file descriptor on success, or a negative value on error.
+ *
+ * \internal
+ * Assembles the device name from \p minor and opens it, creating the device
+ * special file node with the major and minor numbers specified by \p dev and
+ * parent directory if necessary and was called by root.
+ */
+static int drmOpenDevice(long dev, int minor, int type)
+{
+    stat_t          st;
+    char            buf[64];
+    int             fd;
+    mode_t          devmode = DRM_DEV_MODE, serv_mode;
+    int             isroot  = !geteuid();
+    uid_t           user    = DRM_DEV_UID;
+    gid_t           group   = DRM_DEV_GID, serv_group;
+    
+    sprintf(buf, type ? DRM_DEV_NAME : DRM_CONTROL_DEV_NAME, DRM_DIR_NAME, minor);
+    drmMsg("drmOpenDevice: node name is %s\n", buf);
+
+    if (drm_server_info) {
+       drm_server_info->get_perms(&serv_group, &serv_mode);
+       devmode  = serv_mode ? serv_mode : DRM_DEV_MODE;
+       devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
+       group = (serv_group >= 0) ? serv_group : DRM_DEV_GID;
+    }
+
+#if !defined(UDEV)
+    if (stat(DRM_DIR_NAME, &st)) {
+       if (!isroot)
+           return DRM_ERR_NOT_ROOT;
+       mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
+       chown_check_return(DRM_DIR_NAME, 0, 0); /* root:root */
+       chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
+    }
+
+    /* Check if the device node exists and create it if necessary. */
+    if (stat(buf, &st)) {
+       if (!isroot)
+           return DRM_ERR_NOT_ROOT;
+       remove(buf);
+       mknod(buf, S_IFCHR | devmode, dev);
+    }
+
+    if (drm_server_info) {
+       chown_check_return(buf, user, group);
+       chmod(buf, devmode);
+    }
+#else
+    /* if we modprobed then wait for udev */
+    {
+       int udev_count = 0;
+wait_for_udev:
+        if (stat(DRM_DIR_NAME, &st)) {
+               usleep(20);
+               udev_count++;
+
+               if (udev_count == 50)
+                       return -1;
+               goto wait_for_udev;
+       }
+
+       if (stat(buf, &st)) {
+               usleep(20);
+               udev_count++;
+
+               if (udev_count == 50)
+                       return -1;
+               goto wait_for_udev;
+       }
+    }
+#endif
+
+    fd = open(buf, O_RDWR, 0);
+    drmMsg("drmOpenDevice: open result is %d, (%s)\n",
+               fd, fd < 0 ? strerror(errno) : "OK");
+    if (fd >= 0)
+       return fd;
+
+#if !defined(UDEV)
+    /* Check if the device node is not what we expect it to be, and recreate it
+     * and try again if so.
+     */
+    if (st.st_rdev != dev) {
+       if (!isroot)
+           return DRM_ERR_NOT_ROOT;
+       remove(buf);
+       mknod(buf, S_IFCHR | devmode, dev);
+       if (drm_server_info) {
+           chown_check_return(buf, user, group);
+           chmod(buf, devmode);
+       }
+    }
+    fd = open(buf, O_RDWR, 0);
+    drmMsg("drmOpenDevice: open result is %d, (%s)\n",
+               fd, fd < 0 ? strerror(errno) : "OK");
+    if (fd >= 0)
+       return fd;
+
+    drmMsg("drmOpenDevice: Open failed\n");
+    remove(buf);
+#endif
+    return -errno;
+}
+
+
+/**
+ * Open the DRM device
+ *
+ * \param minor device minor number.
+ * \param create allow to create the device if set.
+ *
+ * \return a file descriptor on success, or a negative value on error.
+ * 
+ * \internal
+ * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
+ * name from \p minor and opens it.
+ */
+static int drmOpenMinor(int minor, int create, int type)
+{
+    int  fd;
+    char buf[64];
+    
+    if (create)
+       return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
+    
+    sprintf(buf, type ? DRM_DEV_NAME : DRM_CONTROL_DEV_NAME, DRM_DIR_NAME, minor);
+    if ((fd = open(buf, O_RDWR, 0)) >= 0)
+       return fd;
+    return -errno;
+}
+
+
+/**
+ * Determine whether the DRM kernel driver has been loaded.
+ * 
+ * \return 1 if the DRM driver is loaded, 0 otherwise.
+ *
+ * \internal 
+ * Determine the presence of the kernel driver by attempting to open the 0
+ * minor and get version information.  For backward compatibility with older
+ * Linux implementations, /proc/dri is also checked.
+ */
+int drmAvailable(void)
+{
+    drmVersionPtr version;
+    int           retval = 0;
+    int           fd;
+
+    if ((fd = drmOpenMinor(0, 1, DRM_NODE_RENDER)) < 0) {
+#ifdef __linux__
+       /* Try proc for backward Linux compatibility */
+       if (!access("/proc/dri/0", R_OK))
+           return 1;
+#endif
+       return 0;
+    }
+    
+    if ((version = drmGetVersion(fd))) {
+       retval = 1;
+       drmFreeVersion(version);
+    }
+    close(fd);
+
+    return retval;
+}
+
+
+/**
+ * Open the device by bus ID.
+ *
+ * \param busid bus ID.
+ *
+ * \return a file descriptor on success, or a negative value on error.
+ *
+ * \internal
+ * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
+ * comparing the device bus ID with the one supplied.
+ *
+ * \sa drmOpenMinor() and drmGetBusid().
+ */
+static int drmOpenByBusid(const char *busid)
+{
+    int        i, pci_domain_ok = 1;
+    int        fd;
+    const char *buf;
+    drmSetVersion sv;
+
+    drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
+    for (i = 0; i < DRM_MAX_MINOR; i++) {
+       fd = drmOpenMinor(i, 1, DRM_NODE_RENDER);
+       drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
+       if (fd >= 0) {
+           /* We need to try for 1.4 first for proper PCI domain support
+            * and if that fails, we know the kernel is busted
+            */
+           sv.drm_di_major = 1;
+           sv.drm_di_minor = 4;
+           sv.drm_dd_major = -1;       /* Don't care */
+           sv.drm_dd_minor = -1;       /* Don't care */
+           if (drmSetInterfaceVersion(fd, &sv)) {
+#ifndef __alpha__
+               pci_domain_ok = 0;
+#endif
+               sv.drm_di_major = 1;
+               sv.drm_di_minor = 1;
+               sv.drm_dd_major = -1;       /* Don't care */
+               sv.drm_dd_minor = -1;       /* Don't care */
+               drmMsg("drmOpenByBusid: Interface 1.4 failed, trying 1.1\n",fd);
+               drmSetInterfaceVersion(fd, &sv);
+           }
+           buf = drmGetBusid(fd);
+           drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
+           if (buf && drmMatchBusID(buf, busid, pci_domain_ok)) {
+               drmFreeBusid(buf);
+               return fd;
+           }
+           if (buf)
+               drmFreeBusid(buf);
+           close(fd);
+       }
+    }
+    return -1;
+}
+
+
+/**
+ * Open the device by name.
+ *
+ * \param name driver name.
+ * 
+ * \return a file descriptor on success, or a negative value on error.
+ * 
+ * \internal
+ * This function opens the first minor number that matches the driver name and
+ * isn't already in use.  If it's in use it then it will already have a bus ID
+ * assigned.
+ * 
+ * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
+ */
+static int drmOpenByName(const char *name)
+{
+    int           i;
+    int           fd;
+    drmVersionPtr version;
+    char *        id;
+    
+    if (!drmAvailable()) {
+       if (!drm_server_info) {
+           return -1;
+       }
+       else {
+           /* try to load the kernel module now */
+           if (!drm_server_info->load_module(name)) {
+               drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
+               return -1;
+           }
+       }
+    }
+
+    /*
+     * Open the first minor number that matches the driver name and isn't
+     * already in use.  If it's in use it will have a busid assigned already.
+     */
+    for (i = 0; i < DRM_MAX_MINOR; i++) {
+       if ((fd = drmOpenMinor(i, 1, DRM_NODE_RENDER)) >= 0) {
+           if ((version = drmGetVersion(fd))) {
+               if (!strcmp(version->name, name)) {
+                   drmFreeVersion(version);
+                   id = drmGetBusid(fd);
+                   drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
+                   if (!id || !*id) {
+                       if (id)
+                           drmFreeBusid(id);
+                       return fd;
+                   } else {
+                       drmFreeBusid(id);
+                   }
+               } else {
+                   drmFreeVersion(version);
+               }
+           }
+           close(fd);
+       }
+    }
+
+#ifdef __linux__
+    /* Backward-compatibility /proc support */
+    for (i = 0; i < 8; i++) {
+       char proc_name[64], buf[512];
+       char *driver, *pt, *devstring;
+       int  retcode;
+       
+       sprintf(proc_name, "/proc/dri/%d/name", i);
+       if ((fd = open(proc_name, 0, 0)) >= 0) {
+           retcode = read(fd, buf, sizeof(buf)-1);
+           close(fd);
+           if (retcode) {
+               buf[retcode-1] = '\0';
+               for (driver = pt = buf; *pt && *pt != ' '; ++pt)
+                   ;
+               if (*pt) { /* Device is next */
+                   *pt = '\0';
+                   if (!strcmp(driver, name)) { /* Match */
+                       for (devstring = ++pt; *pt && *pt != ' '; ++pt)
+                           ;
+                       if (*pt) { /* Found busid */
+                           return drmOpenByBusid(++pt);
+                       } else { /* No busid */
+                           return drmOpenDevice(strtol(devstring, NULL, 0),i, DRM_NODE_RENDER);
+                       }
+                   }
+               }
+           }
+       }
+    }
+#endif
+
+    return -1;
+}
+
+
+/**
+ * Open the DRM device.
+ *
+ * Looks up the specified name and bus ID, and opens the device found.  The
+ * entry in /dev/dri is created if necessary and if called by root.
+ *
+ * \param name driver name. Not referenced if bus ID is supplied.
+ * \param busid bus ID. Zero if not known.
+ * 
+ * \return a file descriptor on success, or a negative value on error.
+ * 
+ * \internal
+ * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
+ * otherwise.
+ */
+int drmOpen(const char *name, const char *busid)
+{
+    if (!drmAvailable() && name != NULL && drm_server_info) {
+       /* try to load the kernel */
+       if (!drm_server_info->load_module(name)) {
+           drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
+           return -1;
+       }
+    }
+
+    if (busid) {
+       int fd = drmOpenByBusid(busid);
+       if (fd >= 0)
+           return fd;
+    }
+    
+    if (name)
+       return drmOpenByName(name);
+
+    return -1;
+}
+
+int drmOpenControl(int minor)
+{
+    return drmOpenMinor(minor, 0, DRM_NODE_CONTROL);
+}
+
+/**
+ * Free the version information returned by drmGetVersion().
+ *
+ * \param v pointer to the version information.
+ *
+ * \internal
+ * It frees the memory pointed by \p %v as well as all the non-null strings
+ * pointers in it.
+ */
+void drmFreeVersion(drmVersionPtr v)
+{
+    if (!v)
+       return;
+    drmFree(v->name);
+    drmFree(v->date);
+    drmFree(v->desc);
+    drmFree(v);
+}
+
+
+/**
+ * Free the non-public version information returned by the kernel.
+ *
+ * \param v pointer to the version information.
+ *
+ * \internal
+ * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
+ * the non-null strings pointers in it.
+ */
+static void drmFreeKernelVersion(drm_version_t *v)
+{
+    if (!v)
+       return;
+    drmFree(v->name);
+    drmFree(v->date);
+    drmFree(v->desc);
+    drmFree(v);
+}
+
+
+/**
+ * Copy version information.
+ * 
+ * \param d destination pointer.
+ * \param s source pointer.
+ * 
+ * \internal
+ * Used by drmGetVersion() to translate the information returned by the ioctl
+ * interface in a private structure into the public structure counterpart.
+ */
+static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
+{
+    d->version_major      = s->version_major;
+    d->version_minor      = s->version_minor;
+    d->version_patchlevel = s->version_patchlevel;
+    d->name_len           = s->name_len;
+    d->name               = strdup(s->name);
+    d->date_len           = s->date_len;
+    d->date               = strdup(s->date);
+    d->desc_len           = s->desc_len;
+    d->desc               = strdup(s->desc);
+}
+
+
+/**
+ * Query the driver version information.
+ *
+ * \param fd file descriptor.
+ * 
+ * \return pointer to a drmVersion structure which should be freed with
+ * drmFreeVersion().
+ * 
+ * \note Similar information is available via /proc/dri.
+ * 
+ * \internal
+ * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
+ * first with zeros to get the string lengths, and then the actually strings.
+ * It also null-terminates them since they might not be already.
+ */
+drmVersionPtr drmGetVersion(int fd)
+{
+    drmVersionPtr retval;
+    drm_version_t *version = drmMalloc(sizeof(*version));
+
+    version->name_len    = 0;
+    version->name        = NULL;
+    version->date_len    = 0;
+    version->date        = NULL;
+    version->desc_len    = 0;
+    version->desc        = NULL;
+
+    if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
+       drmFreeKernelVersion(version);
+       return NULL;
+    }
+
+    if (version->name_len)
+       version->name    = drmMalloc(version->name_len + 1);
+    if (version->date_len)
+       version->date    = drmMalloc(version->date_len + 1);
+    if (version->desc_len)
+       version->desc    = drmMalloc(version->desc_len + 1);
+
+    if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
+       drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
+       drmFreeKernelVersion(version);
+       return NULL;
+    }
+
+    /* The results might not be null-terminated strings, so terminate them. */
+    if (version->name_len) version->name[version->name_len] = '\0';
+    if (version->date_len) version->date[version->date_len] = '\0';
+    if (version->desc_len) version->desc[version->desc_len] = '\0';
+
+    retval = drmMalloc(sizeof(*retval));
+    drmCopyVersion(retval, version);
+    drmFreeKernelVersion(version);
+    return retval;
+}
+
+
+/**
+ * Get version information for the DRM user space library.
+ * 
+ * This version number is driver independent.
+ * 
+ * \param fd file descriptor.
+ *
+ * \return version information.
+ * 
+ * \internal
+ * This function allocates and fills a drm_version structure with a hard coded
+ * version number.
+ */
+drmVersionPtr drmGetLibVersion(int fd)
+{
+    drm_version_t *version = drmMalloc(sizeof(*version));
+
+    /* Version history:
+     *   NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
+     *   revision 1.0.x = original DRM interface with no drmGetLibVersion
+     *                    entry point and many drm<Device> extensions
+     *   revision 1.1.x = added drmCommand entry points for device extensions
+     *                    added drmGetLibVersion to identify libdrm.a version
+     *   revision 1.2.x = added drmSetInterfaceVersion
+     *                    modified drmOpen to handle both busid and name
+     *   revision 1.3.x = added server + memory manager
+     */
+    version->version_major      = 1;
+    version->version_minor      = 3;
+    version->version_patchlevel = 0;
+
+    return (drmVersionPtr)version;
+}
+
+int drmGetCap(int fd, uint64_t capability, uint64_t *value)
+{
+       struct drm_get_cap cap = { capability, 0 };
+       int ret;
+
+       ret = drmIoctl(fd, DRM_IOCTL_GET_CAP, &cap);
+       if (ret)
+               return ret;
+
+       *value = cap.value;
+       return 0;
+}
+
+/**
+ * Free the bus ID information.
+ *
+ * \param busid bus ID information string as given by drmGetBusid().
+ *
+ * \internal
+ * This function is just frees the memory pointed by \p busid.
+ */
+void drmFreeBusid(const char *busid)
+{
+    drmFree((void *)busid);
+}
+
+
+/**
+ * Get the bus ID of the device.
+ *
+ * \param fd file descriptor.
+ *
+ * \return bus ID string.
+ *
+ * \internal
+ * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
+ * get the string length and data, passing the arguments in a drm_unique
+ * structure.
+ */
+char *drmGetBusid(int fd)
+{
+    drm_unique_t u;
+
+    u.unique_len = 0;
+    u.unique     = NULL;
+
+    if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
+       return NULL;
+    u.unique = drmMalloc(u.unique_len + 1);
+    if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
+       return NULL;
+    u.unique[u.unique_len] = '\0';
+
+    return u.unique;
+}
+
+
+/**
+ * Set the bus ID of the device.
+ *
+ * \param fd file descriptor.
+ * \param busid bus ID string.
+ *
+ * \return zero on success, negative on failure.
+ *
+ * \internal
+ * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
+ * the arguments in a drm_unique structure.
+ */
+int drmSetBusid(int fd, const char *busid)
+{
+    drm_unique_t u;
+
+    u.unique     = (char *)busid;
+    u.unique_len = strlen(busid);
+
+    if (drmIoctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
+       return -errno;
+    }
+    return 0;
+}
+
+int drmGetMagic(int fd, drm_magic_t * magic)
+{
+    drm_auth_t auth;
+
+    *magic = 0;
+    if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
+       return -errno;
+    *magic = auth.magic;
+    return 0;
+}
+
+int drmAuthMagic(int fd, drm_magic_t magic)
+{
+    drm_auth_t auth;
+
+    auth.magic = magic;
+    if (drmIoctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
+       return -errno;
+    return 0;
+}
+
+/**
+ * Specifies a range of memory that is available for mapping by a
+ * non-root process.
+ *
+ * \param fd file descriptor.
+ * \param offset usually the physical address. The actual meaning depends of
+ * the \p type parameter. See below.
+ * \param size of the memory in bytes.
+ * \param type type of the memory to be mapped.
+ * \param flags combination of several flags to modify the function actions.
+ * \param handle will be set to a value that may be used as the offset
+ * parameter for mmap().
+ * 
+ * \return zero on success or a negative value on error.
+ *
+ * \par Mapping the frame buffer
+ * For the frame buffer
+ * - \p offset will be the physical address of the start of the frame buffer,
+ * - \p size will be the size of the frame buffer in bytes, and
+ * - \p type will be DRM_FRAME_BUFFER.
+ *
+ * \par
+ * The area mapped will be uncached. If MTRR support is available in the
+ * kernel, the frame buffer area will be set to write combining. 
+ *
+ * \par Mapping the MMIO register area
+ * For the MMIO register area,
+ * - \p offset will be the physical address of the start of the register area,
+ * - \p size will be the size of the register area bytes, and
+ * - \p type will be DRM_REGISTERS.
+ * \par
+ * The area mapped will be uncached. 
+ * 
+ * \par Mapping the SAREA
+ * For the SAREA,
+ * - \p offset will be ignored and should be set to zero,
+ * - \p size will be the desired size of the SAREA in bytes,
+ * - \p type will be DRM_SHM.
+ * 
+ * \par
+ * A shared memory area of the requested size will be created and locked in
+ * kernel memory. This area may be mapped into client-space by using the handle
+ * returned. 
+ * 
+ * \note May only be called by root.
+ *
+ * \internal
+ * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
+ * the arguments in a drm_map structure.
+ */
+int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
+             drmMapFlags flags, drm_handle_t *handle)
+{
+    drm_map_t map;
+
+    map.offset  = offset;
+    map.size    = size;
+    map.handle  = 0;
+    map.type    = type;
+    map.flags   = flags;
+    if (drmIoctl(fd, DRM_IOCTL_ADD_MAP, &map))
+       return -errno;
+    if (handle)
+       *handle = (drm_handle_t)(uintptr_t)map.handle;
+    return 0;
+}
+
+int drmRmMap(int fd, drm_handle_t handle)
+{
+    drm_map_t map;
+
+    map.handle = (void *)(uintptr_t)handle;
+
+    if(drmIoctl(fd, DRM_IOCTL_RM_MAP, &map))
+       return -errno;
+    return 0;
+}
+
+/**
+ * Make buffers available for DMA transfers.
+ * 
+ * \param fd file descriptor.
+ * \param count number of buffers.
+ * \param size size of each buffer.
+ * \param flags buffer allocation flags.
+ * \param agp_offset offset in the AGP aperture 
+ *
+ * \return number of buffers allocated, negative on error.
+ *
+ * \internal
+ * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
+ *
+ * \sa drm_buf_desc.
+ */
+int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
+              int agp_offset)
+{
+    drm_buf_desc_t request;
+
+    request.count     = count;
+    request.size      = size;
+    request.low_mark  = 0;
+    request.high_mark = 0;
+    request.flags     = flags;
+    request.agp_start = agp_offset;
+
+    if (drmIoctl(fd, DRM_IOCTL_ADD_BUFS, &request))
+       return -errno;
+    return request.count;
+}
+
+int drmMarkBufs(int fd, double low, double high)
+{
+    drm_buf_info_t info;
+    int            i;
+
+    info.count = 0;
+    info.list  = NULL;
+
+    if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
+       return -EINVAL;
+
+    if (!info.count)
+       return -EINVAL;
+
+    if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
+       return -ENOMEM;
+
+    if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
+       int retval = -errno;
+       drmFree(info.list);
+       return retval;
+    }
+
+    for (i = 0; i < info.count; i++) {
+       info.list[i].low_mark  = low  * info.list[i].count;
+       info.list[i].high_mark = high * info.list[i].count;
+       if (drmIoctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
+           int retval = -errno;
+           drmFree(info.list);
+           return retval;
+       }
+    }
+    drmFree(info.list);
+
+    return 0;
+}
+
+/**
+ * Free buffers.
+ *
+ * \param fd file descriptor.
+ * \param count number of buffers to free.
+ * \param list list of buffers to be freed.
+ *
+ * \return zero on success, or a negative value on failure.
+ * 
+ * \note This function is primarily used for debugging.
+ * 
+ * \internal
+ * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
+ * the arguments in a drm_buf_free structure.
+ */
+int drmFreeBufs(int fd, int count, int *list)
+{
+    drm_buf_free_t request;
+
+    request.count = count;
+    request.list  = list;
+    if (drmIoctl(fd, DRM_IOCTL_FREE_BUFS, &request))
+       return -errno;
+    return 0;
+}
+
+
+/**
+ * Close the device.
+ *
+ * \param fd file descriptor.
+ *
+ * \internal
+ * This function closes the file descriptor.
+ */
+int drmClose(int fd)
+{
+    unsigned long key    = drmGetKeyFromFd(fd);
+    drmHashEntry  *entry = drmGetEntry(fd);
+
+    drmHashDestroy(entry->tagTable);
+    entry->fd       = 0;
+    entry->f        = NULL;
+    entry->tagTable = NULL;
+
+    drmHashDelete(drmHashTable, key);
+    drmFree(entry);
+
+    return close(fd);
+}
+
+
+/**
+ * Map a region of memory.
+ *
+ * \param fd file descriptor.
+ * \param handle handle returned by drmAddMap().
+ * \param size size in bytes. Must match the size used by drmAddMap().
+ * \param address will contain the user-space virtual address where the mapping
+ * begins.
+ *
+ * \return zero on success, or a negative value on failure.
+ * 
+ * \internal
+ * This function is a wrapper for mmap().
+ */
+int drmMap(int fd, drm_handle_t handle, drmSize size, drmAddressPtr address)
+{
+    static unsigned long pagesize_mask = 0;
+
+    if (fd < 0)
+       return -EINVAL;
+
+    if (!pagesize_mask)
+       pagesize_mask = getpagesize() - 1;
+
+    size = (size + pagesize_mask) & ~pagesize_mask;
+
+    *address = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
+    if (*address == MAP_FAILED)
+       return -errno;
+    return 0;
+}
+
+
+/**
+ * Unmap mappings obtained with drmMap().
+ *
+ * \param address address as given by drmMap().
+ * \param size size in bytes. Must match the size used by drmMap().
+ * 
+ * \return zero on success, or a negative value on failure.
+ *
+ * \internal
+ * This function is a wrapper for munmap().
+ */
+int drmUnmap(drmAddress address, drmSize size)
+{
+    return munmap(address, size);
+}
+
+drmBufInfoPtr drmGetBufInfo(int fd)
+{
+    drm_buf_info_t info;
+    drmBufInfoPtr  retval;
+    int            i;
+
+    info.count = 0;
+    info.list  = NULL;
+
+    if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
+       return NULL;
+
+    if (info.count) {
+       if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
+           return NULL;
+
+       if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
+           drmFree(info.list);
+           return NULL;
+       }
+
+       retval = drmMalloc(sizeof(*retval));
+       retval->count = info.count;
+       retval->list  = drmMalloc(info.count * sizeof(*retval->list));
+       for (i = 0; i < info.count; i++) {
+           retval->list[i].count     = info.list[i].count;
+           retval->list[i].size      = info.list[i].size;
+           retval->list[i].low_mark  = info.list[i].low_mark;
+           retval->list[i].high_mark = info.list[i].high_mark;
+       }
+       drmFree(info.list);
+       return retval;
+    }
+    return NULL;
+}
+
+/**
+ * Map all DMA buffers into client-virtual space.
+ *
+ * \param fd file descriptor.
+ *
+ * \return a pointer to a ::drmBufMap structure.
+ *
+ * \note The client may not use these buffers until obtaining buffer indices
+ * with drmDMA().
+ * 
+ * \internal
+ * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
+ * information about the buffers in a drm_buf_map structure into the
+ * client-visible data structures.
+ */ 
+drmBufMapPtr drmMapBufs(int fd)
+{
+    drm_buf_map_t bufs;
+    drmBufMapPtr  retval;
+    int           i;
+
+    bufs.count = 0;
+    bufs.list  = NULL;
+    bufs.virtual = NULL;
+    if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
+       return NULL;
+
+    if (!bufs.count)
+       return NULL;
+
+       if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
+           return NULL;
+
+       if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
+           drmFree(bufs.list);
+           return NULL;
+       }
+
+       retval = drmMalloc(sizeof(*retval));
+       retval->count = bufs.count;
+       retval->list  = drmMalloc(bufs.count * sizeof(*retval->list));
+       for (i = 0; i < bufs.count; i++) {
+           retval->list[i].idx     = bufs.list[i].idx;
+           retval->list[i].total   = bufs.list[i].total;
+           retval->list[i].used    = 0;
+           retval->list[i].address = bufs.list[i].address;
+       }
+
+       drmFree(bufs.list);
+       
+       return retval;
+}
+
+
+/**
+ * Unmap buffers allocated with drmMapBufs().
+ *
+ * \return zero on success, or negative value on failure.
+ *
+ * \internal
+ * Calls munmap() for every buffer stored in \p bufs and frees the
+ * memory allocated by drmMapBufs().
+ */
+int drmUnmapBufs(drmBufMapPtr bufs)
+{
+    int i;
+
+    for (i = 0; i < bufs->count; i++) {
+       munmap(bufs->list[i].address, bufs->list[i].total);
+    }
+
+    drmFree(bufs->list);
+    drmFree(bufs);
+       
+    return 0;
+}
+
+
+#define DRM_DMA_RETRY          16
+
+/**
+ * Reserve DMA buffers.
+ *
+ * \param fd file descriptor.
+ * \param request 
+ * 
+ * \return zero on success, or a negative value on failure.
+ *
+ * \internal
+ * Assemble the arguments into a drm_dma structure and keeps issuing the
+ * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
+ */
+int drmDMA(int fd, drmDMAReqPtr request)
+{
+    drm_dma_t dma;
+    int ret, i = 0;
+
+    dma.context         = request->context;
+    dma.send_count      = request->send_count;
+    dma.send_indices    = request->send_list;
+    dma.send_sizes      = request->send_sizes;
+    dma.flags           = request->flags;
+    dma.request_count   = request->request_count;
+    dma.request_size    = request->request_size;
+    dma.request_indices = request->request_list;
+    dma.request_sizes   = request->request_sizes;
+    dma.granted_count   = 0;
+
+    do {
+       ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
+    } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
+
+    if ( ret == 0 ) {
+       request->granted_count = dma.granted_count;
+       return 0;
+    } else {
+       return -errno;
+    }
+}
+
+
+/**
+ * Obtain heavyweight hardware lock.
+ *
+ * \param fd file descriptor.
+ * \param context context.
+ * \param flags flags that determine the sate of the hardware when the function
+ * returns.
+ * 
+ * \return always zero.
+ * 
+ * \internal
+ * This function translates the arguments into a drm_lock structure and issue
+ * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
+ */
+int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
+{
+    drm_lock_t lock;
+
+    lock.context = context;
+    lock.flags   = 0;
+    if (flags & DRM_LOCK_READY)      lock.flags |= _DRM_LOCK_READY;
+    if (flags & DRM_LOCK_QUIESCENT)  lock.flags |= _DRM_LOCK_QUIESCENT;
+    if (flags & DRM_LOCK_FLUSH)      lock.flags |= _DRM_LOCK_FLUSH;
+    if (flags & DRM_LOCK_FLUSH_ALL)  lock.flags |= _DRM_LOCK_FLUSH_ALL;
+    if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
+    if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
+
+    while (drmIoctl(fd, DRM_IOCTL_LOCK, &lock))
+       ;
+    return 0;
+}
+
+/**
+ * Release the hardware lock.
+ *
+ * \param fd file descriptor.
+ * \param context context.
+ * 
+ * \return zero on success, or a negative value on failure.
+ * 
+ * \internal
+ * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
+ * argument in a drm_lock structure.
+ */
+int drmUnlock(int fd, drm_context_t context)
+{
+    drm_lock_t lock;
+
+    lock.context = context;
+    lock.flags   = 0;
+    return drmIoctl(fd, DRM_IOCTL_UNLOCK, &lock);
+}
+
+drm_context_t *drmGetReservedContextList(int fd, int *count)
+{
+    drm_ctx_res_t res;
+    drm_ctx_t     *list;
+    drm_context_t * retval;
+    int           i;
+
+    res.count    = 0;
+    res.contexts = NULL;
+    if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
+       return NULL;
+
+    if (!res.count)
+       return NULL;
+
+    if (!(list   = drmMalloc(res.count * sizeof(*list))))
+       return NULL;
+    if (!(retval = drmMalloc(res.count * sizeof(*retval)))) {
+       drmFree(list);
+       return NULL;
+    }
+
+    res.contexts = list;
+    if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
+       return NULL;
+
+    for (i = 0; i < res.count; i++)
+       retval[i] = list[i].handle;
+    drmFree(list);
+
+    *count = res.count;
+    return retval;
+}
+
+void drmFreeReservedContextList(drm_context_t *pt)
+{
+    drmFree(pt);
+}
+
+/**
+ * Create context.
+ *
+ * Used by the X server during GLXContext initialization. This causes
+ * per-context kernel-level resources to be allocated.
+ *
+ * \param fd file descriptor.
+ * \param handle is set on success. To be used by the client when requesting DMA
+ * dispatch with drmDMA().
+ * 
+ * \return zero on success, or a negative value on failure.
+ * 
+ * \note May only be called by root.
+ * 
+ * \internal
+ * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
+ * argument in a drm_ctx structure.
+ */
+int drmCreateContext(int fd, drm_context_t *handle)
+{
+    drm_ctx_t ctx;
+
+    ctx.flags = 0;     /* Modified with functions below */
+    if (drmIoctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
+       return -errno;
+    *handle = ctx.handle;
+    return 0;
+}
+
+int drmSwitchToContext(int fd, drm_context_t context)
+{
+    drm_ctx_t ctx;
+
+    ctx.handle = context;
+    if (drmIoctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
+       return -errno;
+    return 0;
+}
+
+int drmSetContextFlags(int fd, drm_context_t context, drm_context_tFlags flags)
+{
+    drm_ctx_t ctx;
+
+    /*
+     * Context preserving means that no context switches are done between DMA
+     * buffers from one context and the next.  This is suitable for use in the
+     * X server (which promises to maintain hardware context), or in the
+     * client-side library when buffers are swapped on behalf of two threads.
+     */
+    ctx.handle = context;
+    ctx.flags  = 0;
+    if (flags & DRM_CONTEXT_PRESERVED)
+       ctx.flags |= _DRM_CONTEXT_PRESERVED;
+    if (flags & DRM_CONTEXT_2DONLY)
+       ctx.flags |= _DRM_CONTEXT_2DONLY;
+    if (drmIoctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
+       return -errno;
+    return 0;
+}
+
+int drmGetContextFlags(int fd, drm_context_t context,
+                       drm_context_tFlagsPtr flags)
+{
+    drm_ctx_t ctx;
+
+    ctx.handle = context;
+    if (drmIoctl(fd, DRM_IOCTL_GET_CTX, &ctx))
+       return -errno;
+    *flags = 0;
+    if (ctx.flags & _DRM_CONTEXT_PRESERVED)
+       *flags |= DRM_CONTEXT_PRESERVED;
+    if (ctx.flags & _DRM_CONTEXT_2DONLY)
+       *flags |= DRM_CONTEXT_2DONLY;
+    return 0;
+}
+
+/**
+ * Destroy context.
+ *
+ * Free any kernel-level resources allocated with drmCreateContext() associated
+ * with the context.
+ * 
+ * \param fd file descriptor.
+ * \param handle handle given by drmCreateContext().
+ * 
+ * \return zero on success, or a negative value on failure.
+ * 
+ * \note May only be called by root.
+ * 
+ * \internal
+ * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
+ * argument in a drm_ctx structure.
+ */
+int drmDestroyContext(int fd, drm_context_t handle)
+{
+    drm_ctx_t ctx;
+    ctx.handle = handle;
+    if (drmIoctl(fd, DRM_IOCTL_RM_CTX, &ctx))
+       return -errno;
+    return 0;
+}
+
+int drmCreateDrawable(int fd, drm_drawable_t *handle)
+{
+    drm_draw_t draw;
+    if (drmIoctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
+       return -errno;
+    *handle = draw.handle;
+    return 0;
+}
+
+int drmDestroyDrawable(int fd, drm_drawable_t handle)
+{
+    drm_draw_t draw;
+    draw.handle = handle;
+    if (drmIoctl(fd, DRM_IOCTL_RM_DRAW, &draw))
+       return -errno;
+    return 0;
+}
+
+int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
+                          drm_drawable_info_type_t type, unsigned int num,
+                          void *data)
+{
+    drm_update_draw_t update;
+
+    update.handle = handle;
+    update.type = type;
+    update.num = num;
+    update.data = (unsigned long long)(unsigned long)data;
+
+    if (drmIoctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
+       return -errno;
+
+    return 0;
+}
+
+/**
+ * Acquire the AGP device.
+ *
+ * Must be called before any of the other AGP related calls.
+ *
+ * \param fd file descriptor.
+ * 
+ * \return zero on success, or a negative value on failure.
+ * 
+ * \internal
+ * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
+ */
+int drmAgpAcquire(int fd)
+{
+    if (drmIoctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
+       return -errno;
+    return 0;
+}
+
+
+/**
+ * Release the AGP device.
+ *
+ * \param fd file descriptor.
+ * 
+ * \return zero on success, or a negative value on failure.
+ * 
+ * \internal
+ * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
+ */
+int drmAgpRelease(int fd)
+{
+    if (drmIoctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
+       return -errno;
+    return 0;
+}
+
+
+/**
+ * Set the AGP mode.
+ *
+ * \param fd file descriptor.
+ * \param mode AGP mode.
+ * 
+ * \return zero on success, or a negative value on failure.
+ * 
+ * \internal
+ * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
+ * argument in a drm_agp_mode structure.
+ */
+int drmAgpEnable(int fd, unsigned long mode)
+{
+    drm_agp_mode_t m;
+
+    m.mode = mode;
+    if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
+       return -errno;
+    return 0;
+}
+
+
+/**
+ * Allocate a chunk of AGP memory.
+ *
+ * \param fd file descriptor.
+ * \param size requested memory size in bytes. Will be rounded to page boundary.
+ * \param type type of memory to allocate.
+ * \param address if not zero, will be set to the physical address of the
+ * allocated memory.
+ * \param handle on success will be set to a handle of the allocated memory.
+ * 
+ * \return zero on success, or a negative value on failure.
+ * 
+ * \internal
+ * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
+ * arguments in a drm_agp_buffer structure.
+ */
+int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
+               unsigned long *address, drm_handle_t *handle)
+{
+    drm_agp_buffer_t b;
+
+    *handle = DRM_AGP_NO_HANDLE;
+    b.size   = size;
+    b.handle = 0;
+    b.type   = type;
+    if (drmIoctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
+       return -errno;
+    if (address != 0UL)
+       *address = b.physical;
+    *handle = b.handle;
+    return 0;
+}
+
+
+/**
+ * Free a chunk of AGP memory.
+ *
+ * \param fd file descriptor.
+ * \param handle handle to the allocated memory, as given by drmAgpAllocate().
+ * 
+ * \return zero on success, or a negative value on failure.
+ * 
+ * \internal
+ * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
+ * argument in a drm_agp_buffer structure.
+ */
+int drmAgpFree(int fd, drm_handle_t handle)
+{
+    drm_agp_buffer_t b;
+
+    b.size   = 0;
+    b.handle = handle;
+    if (drmIoctl(fd, DRM_IOCTL_AGP_FREE, &b))
+       return -errno;
+    return 0;
+}
+
+
+/**
+ * Bind a chunk of AGP memory.
+ *
+ * \param fd file descriptor.
+ * \param handle handle to the allocated memory, as given by drmAgpAllocate().
+ * \param offset offset in bytes. It will round to page boundary.
+ * 
+ * \return zero on success, or a negative value on failure.
+ * 
+ * \internal
+ * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
+ * argument in a drm_agp_binding structure.
+ */
+int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
+{
+    drm_agp_binding_t b;
+
+    b.handle = handle;
+    b.offset = offset;
+    if (drmIoctl(fd, DRM_IOCTL_AGP_BIND, &b))
+       return -errno;
+    return 0;
+}
+
+
+/**
+ * Unbind a chunk of AGP memory.
+ *
+ * \param fd file descriptor.
+ * \param handle handle to the allocated memory, as given by drmAgpAllocate().
+ * 
+ * \return zero on success, or a negative value on failure.
+ * 
+ * \internal
+ * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
+ * the argument in a drm_agp_binding structure.
+ */
+int drmAgpUnbind(int fd, drm_handle_t handle)
+{
+    drm_agp_binding_t b;
+
+    b.handle = handle;
+    b.offset = 0;
+    if (drmIoctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
+       return -errno;
+    return 0;
+}
+
+
+/**
+ * Get AGP driver major version number.
+ *
+ * \param fd file descriptor.
+ * 
+ * \return major version number on success, or a negative value on failure..
+ * 
+ * \internal
+ * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
+ * necessary information in a drm_agp_info structure.
+ */
+int drmAgpVersionMajor(int fd)
+{
+    drm_agp_info_t i;
+
+    if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
+       return -errno;
+    return i.agp_version_major;
+}
+
+
+/**
+ * Get AGP driver minor version number.
+ *
+ * \param fd file descriptor.
+ * 
+ * \return minor version number on success, or a negative value on failure.
+ * 
+ * \internal
+ * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
+ * necessary information in a drm_agp_info structure.
+ */
+int drmAgpVersionMinor(int fd)
+{
+    drm_agp_info_t i;
+
+    if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
+       return -errno;
+    return i.agp_version_minor;
+}
+
+
+/**
+ * Get AGP mode.
+ *
+ * \param fd file descriptor.
+ * 
+ * \return mode on success, or zero on failure.
+ * 
+ * \internal
+ * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
+ * necessary information in a drm_agp_info structure.
+ */
+unsigned long drmAgpGetMode(int fd)
+{
+    drm_agp_info_t i;
+
+    if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
+       return 0;
+    return i.mode;
+}
+
+
+/**
+ * Get AGP aperture base.
+ *
+ * \param fd file descriptor.
+ * 
+ * \return aperture base on success, zero on failure.
+ * 
+ * \internal
+ * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
+ * necessary information in a drm_agp_info structure.
+ */
+unsigned long drmAgpBase(int fd)
+{
+    drm_agp_info_t i;
+
+    if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
+       return 0;
+    return i.aperture_base;
+}
+
+
+/**
+ * Get AGP aperture size.
+ *
+ * \param fd file descriptor.
+ * 
+ * \return aperture size on success, zero on failure.
+ * 
+ * \internal
+ * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
+ * necessary information in a drm_agp_info structure.
+ */
+unsigned long drmAgpSize(int fd)
+{
+    drm_agp_info_t i;
+
+    if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
+       return 0;
+    return i.aperture_size;
+}
+
+
+/**
+ * Get used AGP memory.
+ *
+ * \param fd file descriptor.
+ * 
+ * \return memory used on success, or zero on failure.
+ * 
+ * \internal
+ * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
+ * necessary information in a drm_agp_info structure.
+ */
+unsigned long drmAgpMemoryUsed(int fd)
+{
+    drm_agp_info_t i;
+
+    if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
+       return 0;
+    return i.memory_used;
+}
+
+
+/**
+ * Get available AGP memory.
+ *
+ * \param fd file descriptor.
+ * 
+ * \return memory available on success, or zero on failure.
+ * 
+ * \internal
+ * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
+ * necessary information in a drm_agp_info structure.
+ */
+unsigned long drmAgpMemoryAvail(int fd)
+{
+    drm_agp_info_t i;
+
+    if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
+       return 0;
+    return i.memory_allowed;
+}
+
+
+/**
+ * Get hardware vendor ID.
+ *
+ * \param fd file descriptor.
+ * 
+ * \return vendor ID on success, or zero on failure.
+ * 
+ * \internal
+ * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
+ * necessary information in a drm_agp_info structure.
+ */
+unsigned int drmAgpVendorId(int fd)
+{
+    drm_agp_info_t i;
+
+    if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
+       return 0;
+    return i.id_vendor;
+}
+
+
+/**
+ * Get hardware device ID.
+ *
+ * \param fd file descriptor.
+ * 
+ * \return zero on success, or zero on failure.
+ * 
+ * \internal
+ * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
+ * necessary information in a drm_agp_info structure.
+ */
+unsigned int drmAgpDeviceId(int fd)
+{
+    drm_agp_info_t i;
+
+    if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
+       return 0;
+    return i.id_device;
+}
+
+int drmScatterGatherAlloc(int fd, unsigned long size, drm_handle_t *handle)
+{
+    drm_scatter_gather_t sg;
+
+    *handle = 0;
+    sg.size   = size;
+    sg.handle = 0;
+    if (drmIoctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
+       return -errno;
+    *handle = sg.handle;
+    return 0;
+}
+
+int drmScatterGatherFree(int fd, drm_handle_t handle)
+{
+    drm_scatter_gather_t sg;
+
+    sg.size   = 0;
+    sg.handle = handle;
+    if (drmIoctl(fd, DRM_IOCTL_SG_FREE, &sg))
+       return -errno;
+    return 0;
+}
+
+/**
+ * Wait for VBLANK.
+ *
+ * \param fd file descriptor.
+ * \param vbl pointer to a drmVBlank structure.
+ * 
+ * \return zero on success, or a negative value on failure.
+ * 
+ * \internal
+ * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
+ */
+int drmWaitVBlank(int fd, drmVBlankPtr vbl)
+{
+    struct timespec timeout, cur;
+    int ret;
+
+    ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
+    if (ret < 0) {
+       fprintf(stderr, "clock_gettime failed: %s\n", strerror(ret));
+       goto out;
+    }
+    timeout.tv_sec++;
+
+    do {
+       ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
+       vbl->request.type &= ~DRM_VBLANK_RELATIVE;
+       if (ret && errno == EINTR) {
+              clock_gettime(CLOCK_MONOTONIC, &cur);
+              /* Timeout after 1s */
+              if (cur.tv_sec > timeout.tv_sec + 1 ||
+                  (cur.tv_sec == timeout.tv_sec && cur.tv_nsec >=
+                   timeout.tv_nsec)) {
+                      errno = EBUSY;
+                      ret = -1;
+                      break;
+              }
+       }
+    } while (ret && errno == EINTR);
+
+out:
+    return ret;
+}
+
+int drmError(int err, const char *label)
+{
+    switch (err) {
+    case DRM_ERR_NO_DEVICE:
+       fprintf(stderr, "%s: no device\n", label);
+       break;
+    case DRM_ERR_NO_ACCESS:
+       fprintf(stderr, "%s: no access\n", label);
+       break;
+    case DRM_ERR_NOT_ROOT:
+       fprintf(stderr, "%s: not root\n", label);
+       break;
+    case DRM_ERR_INVALID:
+       fprintf(stderr, "%s: invalid args\n", label);
+       break;
+    default:
+       if (err < 0)
+           err = -err;
+       fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
+       break;
+    }
+
+    return 1;
+}
+
+/**
+ * Install IRQ handler.
+ *
+ * \param fd file descriptor.
+ * \param irq IRQ number.
+ * 
+ * \return zero on success, or a negative value on failure.
+ * 
+ * \internal
+ * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
+ * argument in a drm_control structure.
+ */
+int drmCtlInstHandler(int fd, int irq)
+{
+    drm_control_t ctl;
+
+    ctl.func  = DRM_INST_HANDLER;
+    ctl.irq   = irq;
+    if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
+       return -errno;
+    return 0;
+}
+
+
+/**
+ * Uninstall IRQ handler.
+ *
+ * \param fd file descriptor.
+ * 
+ * \return zero on success, or a negative value on failure.
+ * 
+ * \internal
+ * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
+ * argument in a drm_control structure.
+ */
+int drmCtlUninstHandler(int fd)
+{
+    drm_control_t ctl;
+
+    ctl.func  = DRM_UNINST_HANDLER;
+    ctl.irq   = 0;
+    if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
+       return -errno;
+    return 0;
+}
+
+int drmFinish(int fd, int context, drmLockFlags flags)
+{
+    drm_lock_t lock;
+
+    lock.context = context;
+    lock.flags   = 0;
+    if (flags & DRM_LOCK_READY)      lock.flags |= _DRM_LOCK_READY;
+    if (flags & DRM_LOCK_QUIESCENT)  lock.flags |= _DRM_LOCK_QUIESCENT;
+    if (flags & DRM_LOCK_FLUSH)      lock.flags |= _DRM_LOCK_FLUSH;
+    if (flags & DRM_LOCK_FLUSH_ALL)  lock.flags |= _DRM_LOCK_FLUSH_ALL;
+    if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
+    if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
+    if (drmIoctl(fd, DRM_IOCTL_FINISH, &lock))
+       return -errno;
+    return 0;
+}
+
+/**
+ * Get IRQ from bus ID.
+ *
+ * \param fd file descriptor.
+ * \param busnum bus number.
+ * \param devnum device number.
+ * \param funcnum function number.
+ * 
+ * \return IRQ number on success, or a negative value on failure.
+ * 
+ * \internal
+ * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
+ * arguments in a drm_irq_busid structure.
+ */
+int drmGetInterruptFromBusID(int fd, int busnum, int devnum, int funcnum)
+{
+    drm_irq_busid_t p;
+
+    p.busnum  = busnum;
+    p.devnum  = devnum;
+    p.funcnum = funcnum;
+    if (drmIoctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
+       return -errno;
+    return p.irq;
+}
+
+int drmAddContextTag(int fd, drm_context_t context, void *tag)
+{
+    drmHashEntry  *entry = drmGetEntry(fd);
+
+    if (drmHashInsert(entry->tagTable, context, tag)) {
+       drmHashDelete(entry->tagTable, context);
+       drmHashInsert(entry->tagTable, context, tag);
+    }
+    return 0;
+}
+
+int drmDelContextTag(int fd, drm_context_t context)
+{
+    drmHashEntry  *entry = drmGetEntry(fd);
+
+    return drmHashDelete(entry->tagTable, context);
+}
+
+void *drmGetContextTag(int fd, drm_context_t context)
+{
+    drmHashEntry  *entry = drmGetEntry(fd);
+    void          *value;
+
+    if (drmHashLookup(entry->tagTable, context, &value))
+       return NULL;
+
+    return value;
+}
+
+int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
+                                drm_handle_t handle)
+{
+    drm_ctx_priv_map_t map;
+
+    map.ctx_id = ctx_id;
+    map.handle = (void *)(uintptr_t)handle;
+
+    if (drmIoctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
+       return -errno;
+    return 0;
+}
+
+int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
+                                drm_handle_t *handle)
+{
+    drm_ctx_priv_map_t map;
+
+    map.ctx_id = ctx_id;
+
+    if (drmIoctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
+       return -errno;
+    if (handle)
+       *handle = (drm_handle_t)(uintptr_t)map.handle;
+
+    return 0;
+}
+
+int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
+             drmMapType *type, drmMapFlags *flags, drm_handle_t *handle,
+             int *mtrr)
+{
+    drm_map_t map;
+
+    map.offset = idx;
+    if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map))
+       return -errno;
+    *offset = map.offset;
+    *size   = map.size;
+    *type   = map.type;
+    *flags  = map.flags;
+    *handle = (unsigned long)map.handle;
+    *mtrr   = map.mtrr;
+    return 0;
+}
+
+int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
+                unsigned long *magic, unsigned long *iocs)
+{
+    drm_client_t client;
+
+    client.idx = idx;
+    if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client))
+       return -errno;
+    *auth      = client.auth;
+    *pid       = client.pid;
+    *uid       = client.uid;
+    *magic     = client.magic;
+    *iocs      = client.iocs;
+    return 0;
+}
+
+int drmGetStats(int fd, drmStatsT *stats)
+{
+    drm_stats_t s;
+    int         i;
+
+    if (drmIoctl(fd, DRM_IOCTL_GET_STATS, &s))
+       return -errno;
+
+    stats->count = 0;
+    memset(stats, 0, sizeof(*stats));
+    if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
+       return -1;
+
+#define SET_VALUE                              \
+    stats->data[i].long_format = "%-20.20s";   \
+    stats->data[i].rate_format = "%8.8s";      \
+    stats->data[i].isvalue     = 1;            \
+    stats->data[i].verbose     = 0
+
+#define SET_COUNT                              \
+    stats->data[i].long_format = "%-20.20s";   \
+    stats->data[i].rate_format = "%5.5s";      \
+    stats->data[i].isvalue     = 0;            \
+    stats->data[i].mult_names  = "kgm";        \
+    stats->data[i].mult        = 1000;         \
+    stats->data[i].verbose     = 0
+
+#define SET_BYTE                               \
+    stats->data[i].long_format = "%-20.20s";   \
+    stats->data[i].rate_format = "%5.5s";      \
+    stats->data[i].isvalue     = 0;            \
+    stats->data[i].mult_names  = "KGM";        \
+    stats->data[i].mult        = 1024;         \
+    stats->data[i].verbose     = 0
+
+
+    stats->count = s.count;
+    for (i = 0; i < s.count; i++) {
+       stats->data[i].value = s.data[i].value;
+       switch (s.data[i].type) {
+       case _DRM_STAT_LOCK:
+           stats->data[i].long_name = "Lock";
+           stats->data[i].rate_name = "Lock";
+           SET_VALUE;
+           break;
+       case _DRM_STAT_OPENS:
+           stats->data[i].long_name = "Opens";
+           stats->data[i].rate_name = "O";
+           SET_COUNT;
+           stats->data[i].verbose   = 1;
+           break;
+       case _DRM_STAT_CLOSES:
+           stats->data[i].long_name = "Closes";
+           stats->data[i].rate_name = "Lock";
+           SET_COUNT;
+           stats->data[i].verbose   = 1;
+           break;
+       case _DRM_STAT_IOCTLS:
+           stats->data[i].long_name = "Ioctls";
+           stats->data[i].rate_name = "Ioc/s";
+           SET_COUNT;
+           break;
+       case _DRM_STAT_LOCKS:
+           stats->data[i].long_name = "Locks";
+           stats->data[i].rate_name = "Lck/s";
+           SET_COUNT;
+           break;
+       case _DRM_STAT_UNLOCKS:
+           stats->data[i].long_name = "Unlocks";
+           stats->data[i].rate_name = "Unl/s";
+           SET_COUNT;
+           break;
+       case _DRM_STAT_IRQ:
+           stats->data[i].long_name = "IRQs";
+           stats->data[i].rate_name = "IRQ/s";
+           SET_COUNT;
+           break;
+       case _DRM_STAT_PRIMARY:
+           stats->data[i].long_name = "Primary Bytes";
+           stats->data[i].rate_name = "PB/s";
+           SET_BYTE;
+           break;
+       case _DRM_STAT_SECONDARY:
+           stats->data[i].long_name = "Secondary Bytes";
+           stats->data[i].rate_name = "SB/s";
+           SET_BYTE;
+           break;
+       case _DRM_STAT_DMA:
+           stats->data[i].long_name = "DMA";
+           stats->data[i].rate_name = "DMA/s";
+           SET_COUNT;
+           break;
+       case _DRM_STAT_SPECIAL:
+           stats->data[i].long_name = "Special DMA";
+           stats->data[i].rate_name = "dma/s";
+           SET_COUNT;
+           break;
+       case _DRM_STAT_MISSED:
+           stats->data[i].long_name = "Miss";
+           stats->data[i].rate_name = "Ms/s";
+           SET_COUNT;
+           break;
+       case _DRM_STAT_VALUE:
+           stats->data[i].long_name = "Value";
+           stats->data[i].rate_name = "Value";
+           SET_VALUE;
+           break;
+       case _DRM_STAT_BYTE:
+           stats->data[i].long_name = "Bytes";
+           stats->data[i].rate_name = "B/s";
+           SET_BYTE;
+           break;
+       case _DRM_STAT_COUNT:
+       default:
+           stats->data[i].long_name = "Count";
+           stats->data[i].rate_name = "Cnt/s";
+           SET_COUNT;
+           break;
+       }
+    }
+    return 0;
+}
+
+/**
+ * Issue a set-version ioctl.
+ *
+ * \param fd file descriptor.
+ * \param drmCommandIndex command index 
+ * \param data source pointer of the data to be read and written.
+ * \param size size of the data to be read and written.
+ * 
+ * \return zero on success, or a negative value on failure.
+ * 
+ * \internal
+ * It issues a read-write ioctl given by 
+ * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
+ */
+int drmSetInterfaceVersion(int fd, drmSetVersion *version)
+{
+    int retcode = 0;
+    drm_set_version_t sv;
+
+    sv.drm_di_major = version->drm_di_major;
+    sv.drm_di_minor = version->drm_di_minor;
+    sv.drm_dd_major = version->drm_dd_major;
+    sv.drm_dd_minor = version->drm_dd_minor;
+
+    if (drmIoctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
+       retcode = -errno;
+    }
+
+    version->drm_di_major = sv.drm_di_major;
+    version->drm_di_minor = sv.drm_di_minor;
+    version->drm_dd_major = sv.drm_dd_major;
+    version->drm_dd_minor = sv.drm_dd_minor;
+
+    return retcode;
+}
+
+/**
+ * Send a device-specific command.
+ *
+ * \param fd file descriptor.
+ * \param drmCommandIndex command index 
+ * 
+ * \return zero on success, or a negative value on failure.
+ * 
+ * \internal
+ * It issues a ioctl given by 
+ * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
+ */
+int drmCommandNone(int fd, unsigned long drmCommandIndex)
+{
+    void *data = NULL; /* dummy */
+    unsigned long request;
+
+    request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
+
+    if (drmIoctl(fd, request, data)) {
+       return -errno;
+    }
+    return 0;
+}
+
+
+/**
+ * Send a device-specific read command.
+ *
+ * \param fd file descriptor.
+ * \param drmCommandIndex command index 
+ * \param data destination pointer of the data to be read.
+ * \param size size of the data to be read.
+ * 
+ * \return zero on success, or a negative value on failure.
+ *
+ * \internal
+ * It issues a read ioctl given by 
+ * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
+ */
+int drmCommandRead(int fd, unsigned long drmCommandIndex, void *data,
+                   unsigned long size)
+{
+    unsigned long request;
+
+    request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE, 
+       DRM_COMMAND_BASE + drmCommandIndex, size);
+
+    if (drmIoctl(fd, request, data)) {
+       return -errno;
+    }
+    return 0;
+}
+
+
+/**
+ * Send a device-specific write command.
+ *
+ * \param fd file descriptor.
+ * \param drmCommandIndex command index 
+ * \param data source pointer of the data to be written.
+ * \param size size of the data to be written.
+ * 
+ * \return zero on success, or a negative value on failure.
+ * 
+ * \internal
+ * It issues a write ioctl given by 
+ * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
+ */
+int drmCommandWrite(int fd, unsigned long drmCommandIndex, void *data,
+                    unsigned long size)
+{
+    unsigned long request;
+
+    request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE, 
+       DRM_COMMAND_BASE + drmCommandIndex, size);
+
+    if (drmIoctl(fd, request, data)) {
+       return -errno;
+    }
+    return 0;
+}
+
+
+/**
+ * Send a device-specific read-write command.
+ *
+ * \param fd file descriptor.
+ * \param drmCommandIndex command index 
+ * \param data source pointer of the data to be read and written.
+ * \param size size of the data to be read and written.
+ * 
+ * \return zero on success, or a negative value on failure.
+ * 
+ * \internal
+ * It issues a read-write ioctl given by 
+ * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
+ */
+int drmCommandWriteRead(int fd, unsigned long drmCommandIndex, void *data,
+                        unsigned long size)
+{
+    unsigned long request;
+
+    request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE, 
+       DRM_COMMAND_BASE + drmCommandIndex, size);
+
+    if (drmIoctl(fd, request, data))
+       return -errno;
+    return 0;
+}
+
+#define DRM_MAX_FDS 16
+static struct {
+    char *BusID;
+    int fd;
+    int refcount;
+} connection[DRM_MAX_FDS];
+
+static int nr_fds = 0;
+
+int drmOpenOnce(void *unused, 
+               const char *BusID,
+               int *newlyopened)
+{
+    int i;
+    int fd;
+   
+    for (i = 0; i < nr_fds; i++)
+       if (strcmp(BusID, connection[i].BusID) == 0) {
+           connection[i].refcount++;
+           *newlyopened = 0;
+           return connection[i].fd;
+       }
+
+    fd = drmOpen(unused, BusID);
+    if (fd <= 0 || nr_fds == DRM_MAX_FDS)
+       return fd;
+   
+    connection[nr_fds].BusID = strdup(BusID);
+    connection[nr_fds].fd = fd;
+    connection[nr_fds].refcount = 1;
+    *newlyopened = 1;
+
+    if (0)
+       fprintf(stderr, "saved connection %d for %s %d\n", 
+               nr_fds, connection[nr_fds].BusID, 
+               strcmp(BusID, connection[nr_fds].BusID));
+
+    nr_fds++;
+
+    return fd;
+}
+
+void drmCloseOnce(int fd)
+{
+    int i;
+
+    for (i = 0; i < nr_fds; i++) {
+       if (fd == connection[i].fd) {
+           if (--connection[i].refcount == 0) {
+               drmClose(connection[i].fd);
+               free(connection[i].BusID);
+           
+               if (i < --nr_fds) 
+                   connection[i] = connection[nr_fds];
+
+               return;
+           }
+       }
+    }
+}
+
+int drmSetMaster(int fd)
+{
+       return ioctl(fd, DRM_IOCTL_SET_MASTER, 0);
+}
+
+int drmDropMaster(int fd)
+{
+       return ioctl(fd, DRM_IOCTL_DROP_MASTER, 0);
+}
+
+char *drmGetDeviceNameFromFd(int fd)
+{
+       char name[128];
+       struct stat sbuf;
+       dev_t d;
+       int i;
+
+       /* The whole drmOpen thing is a fiasco and we need to find a way
+        * back to just using open(2).  For now, however, lets just make
+        * things worse with even more ad hoc directory walking code to
+        * discover the device file name. */
+
+       fstat(fd, &sbuf);
+       d = sbuf.st_rdev;
+
+       for (i = 0; i < DRM_MAX_MINOR; i++) {
+               snprintf(name, sizeof name, DRM_DEV_NAME, DRM_DIR_NAME, i);
+               if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d)
+                       break;
+       }
+       if (i == DRM_MAX_MINOR)
+               return NULL;
+
+       return strdup(name);
+}
diff --git a/xf86drm.h b/xf86drm.h
new file mode 100644 (file)
index 0000000..76eb94e
--- /dev/null
+++ b/xf86drm.h
@@ -0,0 +1,734 @@
+/**
+ * \file xf86drm.h 
+ * OS-independent header for DRM user-level library interface.
+ *
+ * \author Rickard E. (Rik) Faith <faith@valinux.com>
+ */
+/*
+ * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
+ * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+#ifndef _XF86DRM_H_
+#define _XF86DRM_H_
+
+#include <stdarg.h>
+#include <sys/types.h>
+#include <stdint.h>
+#include <drm.h>
+
+#if defined(__cplusplus) || defined(c_plusplus)
+extern "C" {
+#endif
+
+#ifndef DRM_MAX_MINOR
+#define DRM_MAX_MINOR   16
+#endif
+
+#if defined(__linux__)
+
+#define DRM_IOCTL_NR(n)                _IOC_NR(n)
+#define DRM_IOC_VOID           _IOC_NONE
+#define DRM_IOC_READ           _IOC_READ
+#define DRM_IOC_WRITE          _IOC_WRITE
+#define DRM_IOC_READWRITE      _IOC_READ|_IOC_WRITE
+#define DRM_IOC(dir, group, nr, size) _IOC(dir, group, nr, size)
+
+#else /* One of the *BSDs */
+
+#include <sys/ioccom.h>
+#define DRM_IOCTL_NR(n)         ((n) & 0xff)
+#define DRM_IOC_VOID            IOC_VOID
+#define DRM_IOC_READ            IOC_OUT
+#define DRM_IOC_WRITE           IOC_IN
+#define DRM_IOC_READWRITE       IOC_INOUT
+#define DRM_IOC(dir, group, nr, size) _IOC(dir, group, nr, size)
+
+#endif
+
+                               /* Defaults, if nothing set in xf86config */
+#define DRM_DEV_UID     0
+#define DRM_DEV_GID     0
+/* Default /dev/dri directory permissions 0755 */
+#define DRM_DEV_DIRMODE                \
+       (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
+#define DRM_DEV_MODE    (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP)
+
+#define DRM_DIR_NAME  "/dev/dri"
+#define DRM_DEV_NAME  "%s/card%d"
+#define DRM_CONTROL_DEV_NAME  "%s/controlD%d"
+#define DRM_PROC_NAME "/proc/dri/" /* For backward Linux compatibility */
+
+#define DRM_ERR_NO_DEVICE  (-1001)
+#define DRM_ERR_NO_ACCESS  (-1002)
+#define DRM_ERR_NOT_ROOT   (-1003)
+#define DRM_ERR_INVALID    (-1004)
+#define DRM_ERR_NO_FD      (-1005)
+
+#define DRM_AGP_NO_HANDLE 0
+
+typedef unsigned int  drmSize,     *drmSizePtr;            /**< For mapped regions */
+typedef void          *drmAddress, **drmAddressPtr; /**< For mapped regions */
+
+typedef struct _drmServerInfo {
+  int (*debug_print)(const char *format, va_list ap);
+  int (*load_module)(const char *name);
+  void (*get_perms)(gid_t *, mode_t *);
+} drmServerInfo, *drmServerInfoPtr;
+
+typedef struct drmHashEntry {
+    int      fd;
+    void     (*f)(int, void *, void *);
+    void     *tagTable;
+} drmHashEntry;
+
+extern int drmIoctl(int fd, unsigned long request, void *arg);
+extern void *drmGetHashTable(void);
+extern drmHashEntry *drmGetEntry(int fd);
+
+/**
+ * Driver version information.
+ *
+ * \sa drmGetVersion() and drmSetVersion().
+ */
+typedef struct _drmVersion {
+    int     version_major;        /**< Major version */
+    int     version_minor;        /**< Minor version */
+    int     version_patchlevel;   /**< Patch level */
+    int     name_len;            /**< Length of name buffer */
+    char    *name;               /**< Name of driver */
+    int     date_len;             /**< Length of date buffer */
+    char    *date;                /**< User-space buffer to hold date */
+    int     desc_len;            /**< Length of desc buffer */
+    char    *desc;                /**< User-space buffer to hold desc */
+} drmVersion, *drmVersionPtr;
+
+typedef struct _drmStats {
+    unsigned long count;            /**< Number of data */
+    struct {
+       unsigned long value;         /**< Value from kernel */
+       const char    *long_format;  /**< Suggested format for long_name */
+       const char    *long_name;    /**< Long name for value */
+       const char    *rate_format;  /**< Suggested format for rate_name */
+       const char    *rate_name;    /**< Short name for value per second */
+       int           isvalue;       /**< True if value (vs. counter) */
+       const char    *mult_names;   /**< Multiplier names (e.g., "KGM") */
+       int           mult;          /**< Multiplier value (e.g., 1024) */
+       int           verbose;       /**< Suggest only in verbose output */
+    } data[15];
+} drmStatsT;
+
+
+                               /* All of these enums *MUST* match with the
+                                   kernel implementation -- so do *NOT*
+                                   change them!  (The drmlib implementation
+                                   will just copy the flags instead of
+                                   translating them.) */
+typedef enum {
+    DRM_FRAME_BUFFER    = 0,      /**< WC, no caching, no core dump */
+    DRM_REGISTERS       = 1,      /**< no caching, no core dump */
+    DRM_SHM             = 2,      /**< shared, cached */
+    DRM_AGP             = 3,     /**< AGP/GART */
+    DRM_SCATTER_GATHER  = 4,     /**< PCI scatter/gather */
+    DRM_CONSISTENT      = 5      /**< PCI consistent */
+} drmMapType;
+
+typedef enum {
+    DRM_RESTRICTED      = 0x0001, /**< Cannot be mapped to client-virtual */
+    DRM_READ_ONLY       = 0x0002, /**< Read-only in client-virtual */
+    DRM_LOCKED          = 0x0004, /**< Physical pages locked */
+    DRM_KERNEL          = 0x0008, /**< Kernel requires access */
+    DRM_WRITE_COMBINING = 0x0010, /**< Use write-combining, if available */
+    DRM_CONTAINS_LOCK   = 0x0020, /**< SHM page that contains lock */
+    DRM_REMOVABLE      = 0x0040  /**< Removable mapping */
+} drmMapFlags;
+
+/**
+ * \warning These values *MUST* match drm.h
+ */
+typedef enum {
+    /** \name Flags for DMA buffer dispatch */
+    /*@{*/
+    DRM_DMA_BLOCK        = 0x01, /**< 
+                                 * Block until buffer dispatched.
+                                 * 
+                                 * \note the buffer may not yet have been
+                                 * processed by the hardware -- getting a
+                                 * hardware lock with the hardware quiescent
+                                 * will ensure that the buffer has been
+                                 * processed.
+                                 */
+    DRM_DMA_WHILE_LOCKED = 0x02, /**< Dispatch while lock held */
+    DRM_DMA_PRIORITY     = 0x04, /**< High priority dispatch */
+    /*@}*/
+
+    /** \name Flags for DMA buffer request */
+    /*@{*/
+    DRM_DMA_WAIT         = 0x10, /**< Wait for free buffers */
+    DRM_DMA_SMALLER_OK   = 0x20, /**< Smaller-than-requested buffers OK */
+    DRM_DMA_LARGER_OK    = 0x40  /**< Larger-than-requested buffers OK */
+    /*@}*/
+} drmDMAFlags;
+
+typedef enum {
+    DRM_PAGE_ALIGN       = 0x01,
+    DRM_AGP_BUFFER       = 0x02,
+    DRM_SG_BUFFER        = 0x04,
+    DRM_FB_BUFFER        = 0x08,
+    DRM_PCI_BUFFER_RO    = 0x10
+} drmBufDescFlags;
+
+typedef enum {
+    DRM_LOCK_READY      = 0x01, /**< Wait until hardware is ready for DMA */
+    DRM_LOCK_QUIESCENT  = 0x02, /**< Wait until hardware quiescent */
+    DRM_LOCK_FLUSH      = 0x04, /**< Flush this context's DMA queue first */
+    DRM_LOCK_FLUSH_ALL  = 0x08, /**< Flush all DMA queues first */
+                               /* These *HALT* flags aren't supported yet
+                                   -- they will be used to support the
+                                   full-screen DGA-like mode. */
+    DRM_HALT_ALL_QUEUES = 0x10, /**< Halt all current and future queues */
+    DRM_HALT_CUR_QUEUES = 0x20  /**< Halt all current queues */
+} drmLockFlags;
+
+typedef enum {
+    DRM_CONTEXT_PRESERVED = 0x01, /**< This context is preserved and
+                                    never swapped. */
+    DRM_CONTEXT_2DONLY    = 0x02  /**< This context is for 2D rendering only. */
+} drm_context_tFlags, *drm_context_tFlagsPtr;
+
+typedef struct _drmBufDesc {
+    int              count;      /**< Number of buffers of this size */
+    int              size;       /**< Size in bytes */
+    int              low_mark;   /**< Low water mark */
+    int              high_mark;          /**< High water mark */
+} drmBufDesc, *drmBufDescPtr;
+
+typedef struct _drmBufInfo {
+    int              count;      /**< Number of buffers described in list */
+    drmBufDescPtr    list;       /**< List of buffer descriptions */
+} drmBufInfo, *drmBufInfoPtr;
+
+typedef struct _drmBuf {
+    int              idx;        /**< Index into the master buffer list */
+    int              total;      /**< Buffer size */
+    int              used;       /**< Amount of buffer in use (for DMA) */
+    drmAddress       address;    /**< Address */
+} drmBuf, *drmBufPtr;
+
+/**
+ * Buffer mapping information.
+ *
+ * Used by drmMapBufs() and drmUnmapBufs() to store information about the
+ * mapped buffers.
+ */
+typedef struct _drmBufMap {
+    int              count;      /**< Number of buffers mapped */
+    drmBufPtr        list;       /**< Buffers */
+} drmBufMap, *drmBufMapPtr;
+
+typedef struct _drmLock {
+    volatile unsigned int lock;
+    char                      padding[60];
+    /* This is big enough for most current (and future?) architectures:
+       DEC Alpha:              32 bytes
+       Intel Merced:           ?
+       Intel P5/PPro/PII/PIII: 32 bytes
+       Intel StrongARM:        32 bytes
+       Intel i386/i486:        16 bytes
+       MIPS:                   32 bytes (?)
+       Motorola 68k:           16 bytes
+       Motorola PowerPC:       32 bytes
+       Sun SPARC:              32 bytes
+    */
+} drmLock, *drmLockPtr;
+
+/**
+ * Indices here refer to the offset into
+ * list in drmBufInfo
+ */
+typedef struct _drmDMAReq {
+    drm_context_t    context;            /**< Context handle */
+    int           send_count;     /**< Number of buffers to send */
+    int           *send_list;     /**< List of handles to buffers */
+    int           *send_sizes;    /**< Lengths of data to send, in bytes */
+    drmDMAFlags   flags;          /**< Flags */
+    int           request_count;  /**< Number of buffers requested */
+    int           request_size;          /**< Desired size of buffers requested */
+    int           *request_list;  /**< Buffer information */
+    int           *request_sizes; /**< Minimum acceptable sizes */
+    int           granted_count;  /**< Number of buffers granted at this size */
+} drmDMAReq, *drmDMAReqPtr;
+
+typedef struct _drmRegion {
+    drm_handle_t     handle;
+    unsigned int  offset;
+    drmSize       size;
+    drmAddress    map;
+} drmRegion, *drmRegionPtr;
+
+typedef struct _drmTextureRegion {
+    unsigned char next;
+    unsigned char prev;
+    unsigned char in_use;
+    unsigned char padding;     /**< Explicitly pad this out */
+    unsigned int  age;
+} drmTextureRegion, *drmTextureRegionPtr;
+
+
+typedef enum {
+    DRM_VBLANK_ABSOLUTE = 0x0, /**< Wait for specific vblank sequence number */
+    DRM_VBLANK_RELATIVE = 0x1, /**< Wait for given number of vblanks */
+    /* bits 1-6 are reserved for high crtcs */
+    DRM_VBLANK_HIGH_CRTC_MASK = 0x0000003e,
+    DRM_VBLANK_EVENT = 0x4000000,      /**< Send event instead of blocking */
+    DRM_VBLANK_FLIP = 0x8000000,       /**< Scheduled buffer swap should flip */
+    DRM_VBLANK_NEXTONMISS = 0x10000000,        /**< If missed, wait for next vblank */
+    DRM_VBLANK_SECONDARY = 0x20000000, /**< Secondary display controller */
+    DRM_VBLANK_SIGNAL   = 0x40000000   /* Send signal instead of blocking */
+} drmVBlankSeqType;
+#define DRM_VBLANK_HIGH_CRTC_SHIFT 1
+
+typedef struct _drmVBlankReq {
+       drmVBlankSeqType type;
+       unsigned int sequence;
+       unsigned long signal;
+} drmVBlankReq, *drmVBlankReqPtr;
+
+typedef struct _drmVBlankReply {
+       drmVBlankSeqType type;
+       unsigned int sequence;
+       long tval_sec;
+       long tval_usec;
+} drmVBlankReply, *drmVBlankReplyPtr;
+
+typedef union _drmVBlank {
+       drmVBlankReq request;
+       drmVBlankReply reply;
+} drmVBlank, *drmVBlankPtr;
+
+typedef struct _drmSetVersion {
+       int drm_di_major;
+       int drm_di_minor;
+       int drm_dd_major;
+       int drm_dd_minor;
+} drmSetVersion, *drmSetVersionPtr;
+
+#define __drm_dummy_lock(lock) (*(__volatile__ unsigned int *)lock)
+
+#define DRM_LOCK_HELD  0x80000000U /**< Hardware lock is held */
+#define DRM_LOCK_CONT  0x40000000U /**< Hardware lock is contended */
+
+#if defined(__GNUC__) && (__GNUC__ >= 2)
+# if defined(__i386) || defined(__AMD64__) || defined(__x86_64__) || defined(__amd64__)
+                               /* Reflect changes here to drmP.h */
+#define DRM_CAS(lock,old,new,__ret)                                    \
+       do {                                                           \
+                int __dummy;   /* Can't mark eax as clobbered */      \
+               __asm__ __volatile__(                                  \
+                       "lock ; cmpxchg %4,%1\n\t"                     \
+                        "setnz %0"                                     \
+                       : "=d" (__ret),                                \
+                         "=m" (__drm_dummy_lock(lock)),               \
+                          "=a" (__dummy)                               \
+                       : "2" (old),                                   \
+                         "r" (new));                                  \
+       } while (0)
+
+#elif defined(__alpha__)
+
+#define        DRM_CAS(lock, old, new, ret)            \
+       do {                                    \
+               int tmp, old32;                 \
+               __asm__ __volatile__(           \
+               "       addl    $31, %5, %3\n"  \
+               "1:     ldl_l   %0, %2\n"       \
+               "       cmpeq   %0, %3, %1\n"   \
+               "       beq     %1, 2f\n"       \
+               "       mov     %4, %0\n"       \
+               "       stl_c   %0, %2\n"       \
+               "       beq     %0, 3f\n"       \
+               "       mb\n"                   \
+               "2:     cmpeq   %1, 0, %1\n"    \
+               ".subsection 2\n"               \
+               "3:     br      1b\n"           \
+               ".previous"                     \
+               : "=&r"(tmp), "=&r"(ret),       \
+                 "=m"(__drm_dummy_lock(lock)), \
+                 "=&r"(old32)                  \
+               : "r"(new), "r"(old)            \
+               : "memory");                    \
+       } while (0)
+
+#elif defined(__sparc__)
+
+#define DRM_CAS(lock,old,new,__ret)                            \
+do {   register unsigned int __old __asm("o0");                \
+       register unsigned int __new __asm("o1");                \
+       register volatile unsigned int *__lock __asm("o2");     \
+       __old = old;                                            \
+       __new = new;                                            \
+       __lock = (volatile unsigned int *)lock;                 \
+       __asm__ __volatile__(                                   \
+               /*"cas [%2], %3, %0"*/                          \
+               ".word 0xd3e29008\n\t"                          \
+               /*"membar #StoreStore | #StoreLoad"*/           \
+               ".word 0x8143e00a"                              \
+               : "=&r" (__new)                                 \
+               : "0" (__new),                                  \
+                 "r" (__lock),                                 \
+                 "r" (__old)                                   \
+               : "memory");                                    \
+       __ret = (__new != __old);                               \
+} while(0)
+
+#elif defined(__ia64__)
+
+#ifdef __INTEL_COMPILER
+/* this currently generates bad code (missing stop bits)... */
+#include <ia64intrin.h>
+
+#define DRM_CAS(lock,old,new,__ret)                                          \
+       do {                                                                  \
+               unsigned long __result, __old = (old) & 0xffffffff;             \
+               __mf();                                                         \
+               __result = _InterlockedCompareExchange_acq(&__drm_dummy_lock(lock), (new), __old);\
+               __ret = (__result) != (__old);                                  \
+/*             __ret = (__sync_val_compare_and_swap(&__drm_dummy_lock(lock), \
+                                                    (old), (new))            \
+                        != (old));                                           */\
+       } while (0)
+
+#else
+#define DRM_CAS(lock,old,new,__ret)                                      \
+       do {                                                              \
+               unsigned int __result, __old = (old);                     \
+               __asm__ __volatile__(                                     \
+                       "mf\n"                                            \
+                       "mov ar.ccv=%2\n"                                 \
+                       ";;\n"                                            \
+                       "cmpxchg4.acq %0=%1,%3,ar.ccv"                    \
+                       : "=r" (__result), "=m" (__drm_dummy_lock(lock))  \
+                       : "r" ((unsigned long)__old), "r" (new)                   \
+                       : "memory");                                      \
+               __ret = (__result) != (__old);                            \
+       } while (0)
+
+#endif
+
+#elif defined(__powerpc__)
+
+#define DRM_CAS(lock,old,new,__ret)                    \
+       do {                                            \
+               __asm__ __volatile__(                   \
+                       "sync;"                         \
+                       "0:    lwarx %0,0,%1;"          \
+                       "      xor. %0,%3,%0;"          \
+                       "      bne 1f;"                 \
+                       "      stwcx. %2,0,%1;"         \
+                       "      bne- 0b;"                \
+                       "1:    "                        \
+                       "sync;"                         \
+               : "=&r"(__ret)                          \
+               : "r"(lock), "r"(new), "r"(old)         \
+               : "cr0", "memory");                     \
+       } while (0)
+
+#endif /* architecture */
+#endif /* __GNUC__ >= 2 */
+
+#ifndef DRM_CAS
+#define DRM_CAS(lock,old,new,ret) do { ret=1; } while (0) /* FAST LOCK FAILS */
+#endif
+
+#if defined(__alpha__)
+#define DRM_CAS_RESULT(_result)                long _result
+#elif defined(__powerpc__)
+#define DRM_CAS_RESULT(_result)                int _result
+#else
+#define DRM_CAS_RESULT(_result)                char _result
+#endif
+
+#define DRM_LIGHT_LOCK(fd,lock,context)                                \
+       do {                                                           \
+                DRM_CAS_RESULT(__ret);                                 \
+               DRM_CAS(lock,context,DRM_LOCK_HELD|context,__ret);     \
+                if (__ret) drmGetLock(fd,context,0);                   \
+        } while(0)
+
+                               /* This one counts fast locks -- for
+                                   benchmarking only. */
+#define DRM_LIGHT_LOCK_COUNT(fd,lock,context,count)                    \
+       do {                                                           \
+                DRM_CAS_RESULT(__ret);                                 \
+               DRM_CAS(lock,context,DRM_LOCK_HELD|context,__ret);     \
+                if (__ret) drmGetLock(fd,context,0);                   \
+                else       ++count;                                    \
+        } while(0)
+
+#define DRM_LOCK(fd,lock,context,flags)                                \
+       do {                                                           \
+               if (flags) drmGetLock(fd,context,flags);               \
+               else       DRM_LIGHT_LOCK(fd,lock,context);            \
+       } while(0)
+
+#define DRM_UNLOCK(fd,lock,context)                                    \
+       do {                                                           \
+                DRM_CAS_RESULT(__ret);                                 \
+               DRM_CAS(lock,DRM_LOCK_HELD|context,context,__ret);     \
+                if (__ret) drmUnlock(fd,context);                      \
+        } while(0)
+
+                               /* Simple spin locks */
+#define DRM_SPINLOCK(spin,val)                                         \
+       do {                                                           \
+            DRM_CAS_RESULT(__ret);                                     \
+           do {                                                       \
+               DRM_CAS(spin,0,val,__ret);                             \
+               if (__ret) while ((spin)->lock);                       \
+           } while (__ret);                                           \
+       } while(0)
+
+#define DRM_SPINLOCK_TAKE(spin,val)                                    \
+       do {                                                           \
+            DRM_CAS_RESULT(__ret);                                     \
+            int  cur;                                                  \
+           do {                                                       \
+                cur = (*spin).lock;                                    \
+               DRM_CAS(spin,cur,val,__ret);                           \
+           } while (__ret);                                           \
+       } while(0)
+
+#define DRM_SPINLOCK_COUNT(spin,val,count,__ret)                       \
+       do {                                                           \
+            int  __i;                                                  \
+            __ret = 1;                                                 \
+            for (__i = 0; __ret && __i < count; __i++) {               \
+               DRM_CAS(spin,0,val,__ret);                             \
+               if (__ret) for (;__i < count && (spin)->lock; __i++);  \
+           }                                                          \
+       } while(0)
+
+#define DRM_SPINUNLOCK(spin,val)                                       \
+       do {                                                           \
+            DRM_CAS_RESULT(__ret);                                     \
+            if ((*spin).lock == val) { /* else server stole lock */    \
+               do {                                                   \
+                   DRM_CAS(spin,val,0,__ret);                         \
+               } while (__ret);                                       \
+            }                                                          \
+       } while(0)
+
+
+
+/* General user-level programmer's API: unprivileged */
+extern int           drmAvailable(void);
+extern int           drmOpen(const char *name, const char *busid);
+extern int drmOpenControl(int minor);
+extern int           drmClose(int fd);
+extern drmVersionPtr drmGetVersion(int fd);
+extern drmVersionPtr drmGetLibVersion(int fd);
+extern int           drmGetCap(int fd, uint64_t capability, uint64_t *value);
+extern void          drmFreeVersion(drmVersionPtr);
+extern int           drmGetMagic(int fd, drm_magic_t * magic);
+extern char          *drmGetBusid(int fd);
+extern int           drmGetInterruptFromBusID(int fd, int busnum, int devnum,
+                                             int funcnum);
+extern int           drmGetMap(int fd, int idx, drm_handle_t *offset,
+                              drmSize *size, drmMapType *type,
+                              drmMapFlags *flags, drm_handle_t *handle,
+                              int *mtrr);
+extern int           drmGetClient(int fd, int idx, int *auth, int *pid,
+                                 int *uid, unsigned long *magic,
+                                 unsigned long *iocs);
+extern int           drmGetStats(int fd, drmStatsT *stats);
+extern int           drmSetInterfaceVersion(int fd, drmSetVersion *version);
+extern int           drmCommandNone(int fd, unsigned long drmCommandIndex);
+extern int           drmCommandRead(int fd, unsigned long drmCommandIndex,
+                                    void *data, unsigned long size);
+extern int           drmCommandWrite(int fd, unsigned long drmCommandIndex,
+                                     void *data, unsigned long size);
+extern int           drmCommandWriteRead(int fd, unsigned long drmCommandIndex,
+                                         void *data, unsigned long size);
+
+/* General user-level programmer's API: X server (root) only  */
+extern void          drmFreeBusid(const char *busid);
+extern int           drmSetBusid(int fd, const char *busid);
+extern int           drmAuthMagic(int fd, drm_magic_t magic);
+extern int           drmAddMap(int fd,
+                              drm_handle_t offset,
+                              drmSize size,
+                              drmMapType type,
+                              drmMapFlags flags,
+                              drm_handle_t * handle);
+extern int          drmRmMap(int fd, drm_handle_t handle);
+extern int          drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
+                                                drm_handle_t handle);
+
+extern int           drmAddBufs(int fd, int count, int size,
+                               drmBufDescFlags flags,
+                               int agp_offset);
+extern int           drmMarkBufs(int fd, double low, double high);
+extern int           drmCreateContext(int fd, drm_context_t * handle);
+extern int           drmSetContextFlags(int fd, drm_context_t context,
+                                       drm_context_tFlags flags);
+extern int           drmGetContextFlags(int fd, drm_context_t context,
+                                       drm_context_tFlagsPtr flags);
+extern int           drmAddContextTag(int fd, drm_context_t context, void *tag);
+extern int           drmDelContextTag(int fd, drm_context_t context);
+extern void          *drmGetContextTag(int fd, drm_context_t context);
+extern drm_context_t * drmGetReservedContextList(int fd, int *count);
+extern void          drmFreeReservedContextList(drm_context_t *);
+extern int           drmSwitchToContext(int fd, drm_context_t context);
+extern int           drmDestroyContext(int fd, drm_context_t handle);
+extern int           drmCreateDrawable(int fd, drm_drawable_t * handle);
+extern int           drmDestroyDrawable(int fd, drm_drawable_t handle);
+extern int           drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
+                                          drm_drawable_info_type_t type,
+                                          unsigned int num, void *data);
+extern int           drmCtlInstHandler(int fd, int irq);
+extern int           drmCtlUninstHandler(int fd);
+
+/* General user-level programmer's API: authenticated client and/or X */
+extern int           drmMap(int fd,
+                           drm_handle_t handle,
+                           drmSize size,
+                           drmAddressPtr address);
+extern int           drmUnmap(drmAddress address, drmSize size);
+extern drmBufInfoPtr drmGetBufInfo(int fd);
+extern drmBufMapPtr  drmMapBufs(int fd);
+extern int           drmUnmapBufs(drmBufMapPtr bufs);
+extern int           drmDMA(int fd, drmDMAReqPtr request);
+extern int           drmFreeBufs(int fd, int count, int *list);
+extern int           drmGetLock(int fd,
+                               drm_context_t context,
+                               drmLockFlags flags);
+extern int           drmUnlock(int fd, drm_context_t context);
+extern int           drmFinish(int fd, int context, drmLockFlags flags);
+extern int          drmGetContextPrivateMapping(int fd, drm_context_t ctx_id, 
+                                                drm_handle_t * handle);
+
+/* AGP/GART support: X server (root) only */
+extern int           drmAgpAcquire(int fd);
+extern int           drmAgpRelease(int fd);
+extern int           drmAgpEnable(int fd, unsigned long mode);
+extern int           drmAgpAlloc(int fd, unsigned long size,
+                                unsigned long type, unsigned long *address,
+                                drm_handle_t *handle);
+extern int           drmAgpFree(int fd, drm_handle_t handle);
+extern int          drmAgpBind(int fd, drm_handle_t handle,
+                               unsigned long offset);
+extern int           drmAgpUnbind(int fd, drm_handle_t handle);
+
+/* AGP/GART info: authenticated client and/or X */
+extern int           drmAgpVersionMajor(int fd);
+extern int           drmAgpVersionMinor(int fd);
+extern unsigned long drmAgpGetMode(int fd);
+extern unsigned long drmAgpBase(int fd); /* Physical location */
+extern unsigned long drmAgpSize(int fd); /* Bytes */
+extern unsigned long drmAgpMemoryUsed(int fd);
+extern unsigned long drmAgpMemoryAvail(int fd);
+extern unsigned int  drmAgpVendorId(int fd);
+extern unsigned int  drmAgpDeviceId(int fd);
+
+/* PCI scatter/gather support: X server (root) only */
+extern int           drmScatterGatherAlloc(int fd, unsigned long size,
+                                          drm_handle_t *handle);
+extern int           drmScatterGatherFree(int fd, drm_handle_t handle);
+
+extern int           drmWaitVBlank(int fd, drmVBlankPtr vbl);
+
+/* Support routines */
+extern void          drmSetServerInfo(drmServerInfoPtr info);
+extern int           drmError(int err, const char *label);
+extern void          *drmMalloc(int size);
+extern void          drmFree(void *pt);
+
+/* Hash table routines */
+extern void *drmHashCreate(void);
+extern int  drmHashDestroy(void *t);
+extern int  drmHashLookup(void *t, unsigned long key, void **value);
+extern int  drmHashInsert(void *t, unsigned long key, void *value);
+extern int  drmHashDelete(void *t, unsigned long key);
+extern int  drmHashFirst(void *t, unsigned long *key, void **value);
+extern int  drmHashNext(void *t, unsigned long *key, void **value);
+
+/* PRNG routines */
+extern void          *drmRandomCreate(unsigned long seed);
+extern int           drmRandomDestroy(void *state);
+extern unsigned long drmRandom(void *state);
+extern double        drmRandomDouble(void *state);
+
+/* Skip list routines */
+
+extern void *drmSLCreate(void);
+extern int  drmSLDestroy(void *l);
+extern int  drmSLLookup(void *l, unsigned long key, void **value);
+extern int  drmSLInsert(void *l, unsigned long key, void *value);
+extern int  drmSLDelete(void *l, unsigned long key);
+extern int  drmSLNext(void *l, unsigned long *key, void **value);
+extern int  drmSLFirst(void *l, unsigned long *key, void **value);
+extern void drmSLDump(void *l);
+extern int  drmSLLookupNeighbors(void *l, unsigned long key,
+                                unsigned long *prev_key, void **prev_value,
+                                unsigned long *next_key, void **next_value);
+
+extern int drmOpenOnce(void *unused, const char *BusID, int *newlyopened);
+extern void drmCloseOnce(int fd);
+extern void drmMsg(const char *format, ...);
+
+extern int drmSetMaster(int fd);
+extern int drmDropMaster(int fd);
+
+#define DRM_EVENT_CONTEXT_VERSION 2
+
+typedef struct _drmEventContext {
+
+       /* This struct is versioned so we can add more pointers if we
+        * add more events. */
+       int version;
+
+       void (*vblank_handler)(int fd,
+                              unsigned int sequence, 
+                              unsigned int tv_sec,
+                              unsigned int tv_usec,
+                              void *user_data);
+
+       void (*page_flip_handler)(int fd,
+                                 unsigned int sequence,
+                                 unsigned int tv_sec,
+                                 unsigned int tv_usec,
+                                 void *user_data);
+
+} drmEventContext, *drmEventContextPtr;
+
+extern int drmHandleEvent(int fd, drmEventContextPtr evctx);
+
+extern char *drmGetDeviceNameFromFd(int fd);
+
+#if defined(__cplusplus) || defined(c_plusplus)
+}
+#endif
+
+#endif
diff --git a/xf86drmHash.c b/xf86drmHash.c
new file mode 100644 (file)
index 0000000..82cbc2a
--- /dev/null
@@ -0,0 +1,428 @@
+/* xf86drmHash.c -- Small hash table support for integer -> integer mapping
+ * Created: Sun Apr 18 09:35:45 1999 by faith@precisioninsight.com
+ *
+ * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Authors: Rickard E. (Rik) Faith <faith@valinux.com>
+ *
+ * DESCRIPTION
+ *
+ * This file contains a straightforward implementation of a fixed-sized
+ * hash table using self-organizing linked lists [Knuth73, pp. 398-399] for
+ * collision resolution.  There are two potentially interesting things
+ * about this implementation:
+ *
+ * 1) The table is power-of-two sized.  Prime sized tables are more
+ * traditional, but do not have a significant advantage over power-of-two
+ * sized table, especially when double hashing is not used for collision
+ * resolution.
+ *
+ * 2) The hash computation uses a table of random integers [Hanson97,
+ * pp. 39-41].
+ *
+ * FUTURE ENHANCEMENTS
+ *
+ * With a table size of 512, the current implementation is sufficient for a
+ * few hundred keys.  Since this is well above the expected size of the
+ * tables for which this implementation was designed, the implementation of
+ * dynamic hash tables was postponed until the need arises.  A common (and
+ * naive) approach to dynamic hash table implementation simply creates a
+ * new hash table when necessary, rehashes all the data into the new table,
+ * and destroys the old table.  The approach in [Larson88] is superior in
+ * two ways: 1) only a portion of the table is expanded when needed,
+ * distributing the expansion cost over several insertions, and 2) portions
+ * of the table can be locked, enabling a scalable thread-safe
+ * implementation.
+ *
+ * REFERENCES
+ *
+ * [Hanson97] David R. Hanson.  C Interfaces and Implementations:
+ * Techniques for Creating Reusable Software.  Reading, Massachusetts:
+ * Addison-Wesley, 1997.
+ *
+ * [Knuth73] Donald E. Knuth. The Art of Computer Programming.  Volume 3:
+ * Sorting and Searching.  Reading, Massachusetts: Addison-Wesley, 1973.
+ *
+ * [Larson88] Per-Ake Larson. "Dynamic Hash Tables".  CACM 31(4), April
+ * 1988, pp. 446-457.
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#define HASH_MAIN 0
+
+#if !HASH_MAIN
+# include "xf86drm.h"
+#endif
+
+#define HASH_MAGIC 0xdeadbeef
+#define HASH_DEBUG 0
+#define HASH_SIZE  512         /* Good for about 100 entries */
+                               /* If you change this value, you probably
+                                   have to change the HashHash hashing
+                                   function! */
+
+#if HASH_MAIN
+#define HASH_ALLOC malloc
+#define HASH_FREE  free
+#define HASH_RANDOM_DECL
+#define HASH_RANDOM_INIT(seed)  srandom(seed)
+#define HASH_RANDOM             random()
+#define HASH_RANDOM_DESTROY
+#else
+#define HASH_ALLOC drmMalloc
+#define HASH_FREE  drmFree
+#define HASH_RANDOM_DECL        void *state
+#define HASH_RANDOM_INIT(seed)  state = drmRandomCreate(seed)
+#define HASH_RANDOM             drmRandom(state)
+#define HASH_RANDOM_DESTROY     drmRandomDestroy(state)
+
+#endif
+
+typedef struct HashBucket {
+    unsigned long     key;
+    void              *value;
+    struct HashBucket *next;
+} HashBucket, *HashBucketPtr;
+
+typedef struct HashTable {
+    unsigned long    magic;
+    unsigned long    entries;
+    unsigned long    hits;     /* At top of linked list */
+    unsigned long    partials; /* Not at top of linked list */
+    unsigned long    misses;   /* Not in table */
+    HashBucketPtr    buckets[HASH_SIZE];
+    int              p0;
+    HashBucketPtr    p1;
+} HashTable, *HashTablePtr;
+
+#if HASH_MAIN
+extern void *drmHashCreate(void);
+extern int  drmHashDestroy(void *t);
+extern int  drmHashLookup(void *t, unsigned long key, unsigned long *value);
+extern int  drmHashInsert(void *t, unsigned long key, unsigned long value);
+extern int  drmHashDelete(void *t, unsigned long key);
+#endif
+
+static unsigned long HashHash(unsigned long key)
+{
+    unsigned long        hash = 0;
+    unsigned long        tmp  = key;
+    static int           init = 0;
+    static unsigned long scatter[256];
+    int                  i;
+
+    if (!init) {
+       HASH_RANDOM_DECL;
+       HASH_RANDOM_INIT(37);
+       for (i = 0; i < 256; i++) scatter[i] = HASH_RANDOM;
+       HASH_RANDOM_DESTROY;
+       ++init;
+    }
+
+    while (tmp) {
+       hash = (hash << 1) + scatter[tmp & 0xff];
+       tmp >>= 8;
+    }
+
+    hash %= HASH_SIZE;
+#if HASH_DEBUG
+    printf( "Hash(%d) = %d\n", key, hash);
+#endif
+    return hash;
+}
+
+void *drmHashCreate(void)
+{
+    HashTablePtr table;
+    int          i;
+
+    table           = HASH_ALLOC(sizeof(*table));
+    if (!table) return NULL;
+    table->magic    = HASH_MAGIC;
+    table->entries  = 0;
+    table->hits     = 0;
+    table->partials = 0;
+    table->misses   = 0;
+
+    for (i = 0; i < HASH_SIZE; i++) table->buckets[i] = NULL;
+    return table;
+}
+
+int drmHashDestroy(void *t)
+{
+    HashTablePtr  table = (HashTablePtr)t;
+    HashBucketPtr bucket;
+    HashBucketPtr next;
+    int           i;
+
+    if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
+
+    for (i = 0; i < HASH_SIZE; i++) {
+       for (bucket = table->buckets[i]; bucket;) {
+           next = bucket->next;
+           HASH_FREE(bucket);
+           bucket = next;
+       }
+    }
+    HASH_FREE(table);
+    return 0;
+}
+
+/* Find the bucket and organize the list so that this bucket is at the
+   top. */
+
+static HashBucketPtr HashFind(HashTablePtr table,
+                             unsigned long key, unsigned long *h)
+{
+    unsigned long hash = HashHash(key);
+    HashBucketPtr prev = NULL;
+    HashBucketPtr bucket;
+
+    if (h) *h = hash;
+
+    for (bucket = table->buckets[hash]; bucket; bucket = bucket->next) {
+       if (bucket->key == key) {
+           if (prev) {
+                               /* Organize */
+               prev->next           = bucket->next;
+               bucket->next         = table->buckets[hash];
+               table->buckets[hash] = bucket;
+               ++table->partials;
+           } else {
+               ++table->hits;
+           }
+           return bucket;
+       }
+       prev = bucket;
+    }
+    ++table->misses;
+    return NULL;
+}
+
+int drmHashLookup(void *t, unsigned long key, void **value)
+{
+    HashTablePtr  table = (HashTablePtr)t;
+    HashBucketPtr bucket;
+
+    if (!table || table->magic != HASH_MAGIC) return -1; /* Bad magic */
+
+    bucket = HashFind(table, key, NULL);
+    if (!bucket) return 1;     /* Not found */
+    *value = bucket->value;
+    return 0;                  /* Found */
+}
+
+int drmHashInsert(void *t, unsigned long key, void *value)
+{
+    HashTablePtr  table = (HashTablePtr)t;
+    HashBucketPtr bucket;
+    unsigned long hash;
+
+    if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
+
+    if (HashFind(table, key, &hash)) return 1; /* Already in table */
+
+    bucket               = HASH_ALLOC(sizeof(*bucket));
+    if (!bucket) return -1;    /* Error */
+    bucket->key          = key;
+    bucket->value        = value;
+    bucket->next         = table->buckets[hash];
+    table->buckets[hash] = bucket;
+#if HASH_DEBUG
+    printf("Inserted %d at %d/%p\n", key, hash, bucket);
+#endif
+    return 0;                  /* Added to table */
+}
+
+int drmHashDelete(void *t, unsigned long key)
+{
+    HashTablePtr  table = (HashTablePtr)t;
+    unsigned long hash;
+    HashBucketPtr bucket;
+
+    if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
+
+    bucket = HashFind(table, key, &hash);
+
+    if (!bucket) return 1;     /* Not found */
+
+    table->buckets[hash] = bucket->next;
+    HASH_FREE(bucket);
+    return 0;
+}
+
+int drmHashNext(void *t, unsigned long *key, void **value)
+{
+    HashTablePtr  table = (HashTablePtr)t;
+
+    while (table->p0 < HASH_SIZE) {
+       if (table->p1) {
+           *key       = table->p1->key;
+           *value     = table->p1->value;
+           table->p1  = table->p1->next;
+           return 1;
+       }
+       table->p1 = table->buckets[table->p0];
+       ++table->p0;
+    }
+    return 0;
+}
+
+int drmHashFirst(void *t, unsigned long *key, void **value)
+{
+    HashTablePtr  table = (HashTablePtr)t;
+
+    if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
+
+    table->p0 = 0;
+    table->p1 = table->buckets[0];
+    return drmHashNext(table, key, value);
+}
+
+#if HASH_MAIN
+#define DIST_LIMIT 10
+static int dist[DIST_LIMIT];
+
+static void clear_dist(void) {
+    int i;
+
+    for (i = 0; i < DIST_LIMIT; i++) dist[i] = 0;
+}
+
+static int count_entries(HashBucketPtr bucket)
+{
+    int count = 0;
+
+    for (; bucket; bucket = bucket->next) ++count;
+    return count;
+}
+
+static void update_dist(int count)
+{
+    if (count >= DIST_LIMIT) ++dist[DIST_LIMIT-1];
+    else                     ++dist[count];
+}
+
+static void compute_dist(HashTablePtr table)
+{
+    int           i;
+    HashBucketPtr bucket;
+
+    printf("Entries = %ld, hits = %ld, partials = %ld, misses = %ld\n",
+          table->entries, table->hits, table->partials, table->misses);
+    clear_dist();
+    for (i = 0; i < HASH_SIZE; i++) {
+       bucket = table->buckets[i];
+       update_dist(count_entries(bucket));
+    }
+    for (i = 0; i < DIST_LIMIT; i++) {
+       if (i != DIST_LIMIT-1) printf("%5d %10d\n", i, dist[i]);
+       else                   printf("other %10d\n", dist[i]);
+    }
+}
+
+static void check_table(HashTablePtr table,
+                       unsigned long key, unsigned long value)
+{
+    unsigned long retval  = 0;
+    int           retcode = drmHashLookup(table, key, &retval);
+
+    switch (retcode) {
+    case -1:
+       printf("Bad magic = 0x%08lx:"
+              " key = %lu, expected = %lu, returned = %lu\n",
+              table->magic, key, value, retval);
+       break;
+    case 1:
+       printf("Not found: key = %lu, expected = %lu returned = %lu\n",
+              key, value, retval);
+       break;
+    case 0:
+       if (value != retval)
+           printf("Bad value: key = %lu, expected = %lu, returned = %lu\n",
+                  key, value, retval);
+       break;
+    default:
+       printf("Bad retcode = %d: key = %lu, expected = %lu, returned = %lu\n",
+              retcode, key, value, retval);
+       break;
+    }
+}
+
+int main(void)
+{
+    HashTablePtr table;
+    int          i;
+
+    printf("\n***** 256 consecutive integers ****\n");
+    table = drmHashCreate();
+    for (i = 0; i < 256; i++) drmHashInsert(table, i, i);
+    for (i = 0; i < 256; i++) check_table(table, i, i);
+    for (i = 256; i >= 0; i--) check_table(table, i, i);
+    compute_dist(table);
+    drmHashDestroy(table);
+
+    printf("\n***** 1024 consecutive integers ****\n");
+    table = drmHashCreate();
+    for (i = 0; i < 1024; i++) drmHashInsert(table, i, i);
+    for (i = 0; i < 1024; i++) check_table(table, i, i);
+    for (i = 1024; i >= 0; i--) check_table(table, i, i);
+    compute_dist(table);
+    drmHashDestroy(table);
+
+    printf("\n***** 1024 consecutive page addresses (4k pages) ****\n");
+    table = drmHashCreate();
+    for (i = 0; i < 1024; i++) drmHashInsert(table, i*4096, i);
+    for (i = 0; i < 1024; i++) check_table(table, i*4096, i);
+    for (i = 1024; i >= 0; i--) check_table(table, i*4096, i);
+    compute_dist(table);
+    drmHashDestroy(table);
+
+    printf("\n***** 1024 random integers ****\n");
+    table = drmHashCreate();
+    srandom(0xbeefbeef);
+    for (i = 0; i < 1024; i++) drmHashInsert(table, random(), i);
+    srandom(0xbeefbeef);
+    for (i = 0; i < 1024; i++) check_table(table, random(), i);
+    srandom(0xbeefbeef);
+    for (i = 0; i < 1024; i++) check_table(table, random(), i);
+    compute_dist(table);
+    drmHashDestroy(table);
+
+    printf("\n***** 5000 random integers ****\n");
+    table = drmHashCreate();
+    srandom(0xbeefbeef);
+    for (i = 0; i < 5000; i++) drmHashInsert(table, random(), i);
+    srandom(0xbeefbeef);
+    for (i = 0; i < 5000; i++) check_table(table, random(), i);
+    srandom(0xbeefbeef);
+    for (i = 0; i < 5000; i++) check_table(table, random(), i);
+    compute_dist(table);
+    drmHashDestroy(table);
+
+    return 0;
+}
+#endif
diff --git a/xf86drmMode.c b/xf86drmMode.c
new file mode 100644 (file)
index 0000000..792880d
--- /dev/null
@@ -0,0 +1,976 @@
+/*
+ * \file xf86drmMode.c
+ * Header for DRM modesetting interface.
+ *
+ * \author Jakob Bornecrantz <wallbraker@gmail.com>
+ *
+ * \par Acknowledgements:
+ * Feb 2007, Dave Airlie <airlied@linux.ie>
+ */
+
+/*
+ * Copyright (c) 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas.
+ * Copyright (c) 2007-2008 Dave Airlie <airlied@linux.ie>
+ * Copyright (c) 2007-2008 Jakob Bornecrantz <wallbraker@gmail.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ */
+
+/*
+ * TODO the types we are after are defined in diffrent headers on diffrent
+ * platforms find which headers to include to get uint32_t
+ */
+#include <stdint.h>
+#include <sys/ioctl.h>
+#include <stdio.h>
+
+#include "xf86drmMode.h"
+#include "xf86drm.h"
+#include <drm.h>
+#include <string.h>
+#include <dirent.h>
+#include <unistd.h>
+#include <errno.h>
+
+#define U642VOID(x) ((void *)(unsigned long)(x))
+#define VOID2U64(x) ((uint64_t)(unsigned long)(x))
+
+static inline int DRM_IOCTL(int fd, unsigned long cmd, void *arg)
+{
+       int ret = drmIoctl(fd, cmd, arg);
+       return ret < 0 ? -errno : ret;
+}
+
+/*
+ * Util functions
+ */
+
+void* drmAllocCpy(void *array, int count, int entry_size)
+{
+       char *r;
+       int i;
+
+       if (!count || !array || !entry_size)
+               return 0;
+
+       if (!(r = drmMalloc(count*entry_size)))
+               return 0;
+
+       for (i = 0; i < count; i++)
+               memcpy(r+(entry_size*i), array+(entry_size*i), entry_size);
+
+       return r;
+}
+
+/*
+ * A couple of free functions.
+ */
+
+void drmModeFreeModeInfo(drmModeModeInfoPtr ptr)
+{
+       if (!ptr)
+               return;
+
+       drmFree(ptr);
+}
+
+void drmModeFreeResources(drmModeResPtr ptr)
+{
+       if (!ptr)
+               return;
+
+       drmFree(ptr->fbs);
+       drmFree(ptr->crtcs);
+       drmFree(ptr->connectors);
+       drmFree(ptr->encoders);
+       drmFree(ptr);
+
+}
+
+void drmModeFreeFB(drmModeFBPtr ptr)
+{
+       if (!ptr)
+               return;
+
+       /* we might add more frees later. */
+       drmFree(ptr);
+}
+
+void drmModeFreeCrtc(drmModeCrtcPtr ptr)
+{
+       if (!ptr)
+               return;
+
+       drmFree(ptr);
+
+}
+
+void drmModeFreeConnector(drmModeConnectorPtr ptr)
+{
+       if (!ptr)
+               return;
+
+       drmFree(ptr->encoders);
+       drmFree(ptr->prop_values);
+       drmFree(ptr->props);
+       drmFree(ptr->modes);
+       drmFree(ptr);
+
+}
+
+void drmModeFreeEncoder(drmModeEncoderPtr ptr)
+{
+       drmFree(ptr);
+}
+
+/*
+ * ModeSetting functions.
+ */
+
+drmModeResPtr drmModeGetResources(int fd)
+{
+       struct drm_mode_card_res res, counts;
+       drmModeResPtr r = 0;
+
+retry:
+       memset(&res, 0, sizeof(struct drm_mode_card_res));
+       if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
+               return 0;
+
+       counts = res;
+
+       if (res.count_fbs) {
+               res.fb_id_ptr = VOID2U64(drmMalloc(res.count_fbs*sizeof(uint32_t)));
+               if (!res.fb_id_ptr)
+                       goto err_allocs;
+       }
+       if (res.count_crtcs) {
+               res.crtc_id_ptr = VOID2U64(drmMalloc(res.count_crtcs*sizeof(uint32_t)));
+               if (!res.crtc_id_ptr)
+                       goto err_allocs;
+       }
+       if (res.count_connectors) {
+               res.connector_id_ptr = VOID2U64(drmMalloc(res.count_connectors*sizeof(uint32_t)));
+               if (!res.connector_id_ptr)
+                       goto err_allocs;
+       }
+       if (res.count_encoders) {
+               res.encoder_id_ptr = VOID2U64(drmMalloc(res.count_encoders*sizeof(uint32_t)));
+               if (!res.encoder_id_ptr)
+                       goto err_allocs;
+       }
+
+       if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
+               goto err_allocs;
+
+       /* The number of available connectors and etc may have changed with a
+        * hotplug event in between the ioctls, in which case the field is
+        * silently ignored by the kernel.
+        */
+       if (counts.count_fbs < res.count_fbs ||
+           counts.count_crtcs < res.count_crtcs ||
+           counts.count_connectors < res.count_connectors ||
+           counts.count_encoders < res.count_encoders)
+       {
+               drmFree(U642VOID(res.fb_id_ptr));
+               drmFree(U642VOID(res.crtc_id_ptr));
+               drmFree(U642VOID(res.connector_id_ptr));
+               drmFree(U642VOID(res.encoder_id_ptr));
+
+               goto retry;
+       }
+
+       /*
+        * return
+        */
+       if (!(r = drmMalloc(sizeof(*r))))
+               goto err_allocs;
+
+       r->min_width     = res.min_width;
+       r->max_width     = res.max_width;
+       r->min_height    = res.min_height;
+       r->max_height    = res.max_height;
+       r->count_fbs     = res.count_fbs;
+       r->count_crtcs   = res.count_crtcs;
+       r->count_connectors = res.count_connectors;
+       r->count_encoders = res.count_encoders;
+
+       r->fbs        = drmAllocCpy(U642VOID(res.fb_id_ptr), res.count_fbs, sizeof(uint32_t));
+       r->crtcs      = drmAllocCpy(U642VOID(res.crtc_id_ptr), res.count_crtcs, sizeof(uint32_t));
+       r->connectors = drmAllocCpy(U642VOID(res.connector_id_ptr), res.count_connectors, sizeof(uint32_t));
+       r->encoders   = drmAllocCpy(U642VOID(res.encoder_id_ptr), res.count_encoders, sizeof(uint32_t));
+       if ((res.count_fbs && !r->fbs) ||
+           (res.count_crtcs && !r->crtcs) ||
+           (res.count_connectors && !r->connectors) ||
+           (res.count_encoders && !r->encoders))
+       {
+               drmFree(r->fbs);
+               drmFree(r->crtcs);
+               drmFree(r->connectors);
+               drmFree(r->encoders);
+               drmFree(r);
+               r = 0;
+       }
+
+err_allocs:
+       drmFree(U642VOID(res.fb_id_ptr));
+       drmFree(U642VOID(res.crtc_id_ptr));
+       drmFree(U642VOID(res.connector_id_ptr));
+       drmFree(U642VOID(res.encoder_id_ptr));
+
+       return r;
+}
+
+int drmModeAddFB(int fd, uint32_t width, uint32_t height, uint8_t depth,
+                 uint8_t bpp, uint32_t pitch, uint32_t bo_handle,
+                uint32_t *buf_id)
+{
+       struct drm_mode_fb_cmd f;
+       int ret;
+
+       f.width  = width;
+       f.height = height;
+       f.pitch  = pitch;
+       f.bpp    = bpp;
+       f.depth  = depth;
+       f.handle = bo_handle;
+
+       if ((ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ADDFB, &f)))
+               return ret;
+
+       *buf_id = f.fb_id;
+       return 0;
+}
+
+int drmModeAddFB2(int fd, uint32_t width, uint32_t height,
+                 uint32_t pixel_format, uint32_t bo_handles[4],
+                 uint32_t pitches[4], uint32_t offsets[4],
+                 uint32_t *buf_id)
+{
+       struct drm_mode_fb_cmd2 f;
+       int ret;
+
+       f.width  = width;
+       f.height = height;
+       f.pixel_format = pixel_format;
+       memcpy(f.handles, bo_handles, sizeof(uint32_t) * 4);
+       memcpy(f.pitches, pitches, sizeof(uint32_t) * 4);
+       memcpy(f.offsets, offsets, sizeof(uint32_t) * 4);
+
+       if ((ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ADDFB2, &f)))
+               return ret;
+
+       *buf_id = f.fb_id;
+
+       memcpy(bo_handles, f.handles, sizeof(uint32_t) * 4);
+       memcpy(pitches, f.pitches, sizeof(uint32_t) * 4);
+       memcpy(offsets, f.offsets, sizeof(uint32_t) * 4);
+
+       return 0;
+}
+
+int drmModeRmFB(int fd, uint32_t bufferId)
+{
+       return DRM_IOCTL(fd, DRM_IOCTL_MODE_RMFB, &bufferId);
+
+
+}
+
+drmModeFBPtr drmModeGetFB(int fd, uint32_t buf)
+{
+       struct drm_mode_fb_cmd info;
+       drmModeFBPtr r;
+
+       info.fb_id = buf;
+
+       if (drmIoctl(fd, DRM_IOCTL_MODE_GETFB, &info))
+               return NULL;
+
+       if (!(r = drmMalloc(sizeof(*r))))
+               return NULL;
+
+       r->fb_id = info.fb_id;
+       r->width = info.width;
+       r->height = info.height;
+       r->pitch = info.pitch;
+       r->bpp = info.bpp;
+       r->handle = info.handle;
+       r->depth = info.depth;
+
+       return r;
+}
+
+int drmModeDirtyFB(int fd, uint32_t bufferId,
+                  drmModeClipPtr clips, uint32_t num_clips)
+{
+       struct drm_mode_fb_dirty_cmd dirty = { 0 };
+
+       dirty.fb_id = bufferId;
+       dirty.clips_ptr = VOID2U64(clips);
+       dirty.num_clips = num_clips;
+
+       return DRM_IOCTL(fd, DRM_IOCTL_MODE_DIRTYFB, &dirty);
+}
+
+
+/*
+ * Crtc functions
+ */
+
+drmModeCrtcPtr drmModeGetCrtc(int fd, uint32_t crtcId)
+{
+       struct drm_mode_crtc crtc;
+       drmModeCrtcPtr r;
+
+       crtc.crtc_id = crtcId;
+
+       if (drmIoctl(fd, DRM_IOCTL_MODE_GETCRTC, &crtc))
+               return 0;
+
+       /*
+        * return
+        */
+
+       if (!(r = drmMalloc(sizeof(*r))))
+               return 0;
+
+       r->crtc_id         = crtc.crtc_id;
+       r->x               = crtc.x;
+       r->y               = crtc.y;
+       r->mode_valid      = crtc.mode_valid;
+       if (r->mode_valid)
+               memcpy(&r->mode, &crtc.mode, sizeof(struct drm_mode_modeinfo));
+       r->buffer_id       = crtc.fb_id;
+       r->gamma_size      = crtc.gamma_size;
+       return r;
+}
+
+
+int drmModeSetCrtc(int fd, uint32_t crtcId, uint32_t bufferId,
+                   uint32_t x, uint32_t y, uint32_t *connectors, int count,
+                  drmModeModeInfoPtr mode)
+{
+       struct drm_mode_crtc crtc;
+
+       crtc.x             = x;
+       crtc.y             = y;
+       crtc.crtc_id       = crtcId;
+       crtc.fb_id         = bufferId;
+       crtc.set_connectors_ptr = VOID2U64(connectors);
+       crtc.count_connectors = count;
+       if (mode) {
+         memcpy(&crtc.mode, mode, sizeof(struct drm_mode_modeinfo));
+         crtc.mode_valid = 1;
+       } else
+         crtc.mode_valid = 0;
+
+       return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETCRTC, &crtc);
+}
+
+/*
+ * Cursor manipulation
+ */
+
+int drmModeSetCursor(int fd, uint32_t crtcId, uint32_t bo_handle, uint32_t width, uint32_t height)
+{
+       struct drm_mode_cursor arg;
+
+       arg.flags = DRM_MODE_CURSOR_BO;
+       arg.crtc_id = crtcId;
+       arg.width = width;
+       arg.height = height;
+       arg.handle = bo_handle;
+
+       return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR, &arg);
+}
+
+int drmModeMoveCursor(int fd, uint32_t crtcId, int x, int y)
+{
+       struct drm_mode_cursor arg;
+
+       arg.flags = DRM_MODE_CURSOR_MOVE;
+       arg.crtc_id = crtcId;
+       arg.x = x;
+       arg.y = y;
+
+       return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR, &arg);
+}
+
+/*
+ * Encoder get
+ */
+drmModeEncoderPtr drmModeGetEncoder(int fd, uint32_t encoder_id)
+{
+       struct drm_mode_get_encoder enc;
+       drmModeEncoderPtr r = NULL;
+
+       enc.encoder_id = encoder_id;
+       enc.encoder_type = 0;
+       enc.possible_crtcs = 0;
+       enc.possible_clones = 0;
+
+       if (drmIoctl(fd, DRM_IOCTL_MODE_GETENCODER, &enc))
+               return 0;
+
+       if (!(r = drmMalloc(sizeof(*r))))
+               return 0;
+
+       r->encoder_id = enc.encoder_id;
+       r->crtc_id = enc.crtc_id;
+       r->encoder_type = enc.encoder_type;
+       r->possible_crtcs = enc.possible_crtcs;
+       r->possible_clones = enc.possible_clones;
+
+       return r;
+}
+
+/*
+ * Connector manipulation
+ */
+
+drmModeConnectorPtr drmModeGetConnector(int fd, uint32_t connector_id)
+{
+       struct drm_mode_get_connector conn, counts;
+       drmModeConnectorPtr r = NULL;
+
+retry:
+       memset(&conn, 0, sizeof(struct drm_mode_get_connector));
+       conn.connector_id = connector_id;
+
+       if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
+               return 0;
+
+       counts = conn;
+
+       if (conn.count_props) {
+               conn.props_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint32_t)));
+               if (!conn.props_ptr)
+                       goto err_allocs;
+               conn.prop_values_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint64_t)));
+               if (!conn.prop_values_ptr)
+                       goto err_allocs;
+       }
+
+       if (conn.count_modes) {
+               conn.modes_ptr = VOID2U64(drmMalloc(conn.count_modes*sizeof(struct drm_mode_modeinfo)));
+               if (!conn.modes_ptr)
+                       goto err_allocs;
+       }
+
+       if (conn.count_encoders) {
+               conn.encoders_ptr = VOID2U64(drmMalloc(conn.count_encoders*sizeof(uint32_t)));
+               if (!conn.encoders_ptr)
+                       goto err_allocs;
+       }
+
+       if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
+               goto err_allocs;
+
+       /* The number of available connectors and etc may have changed with a
+        * hotplug event in between the ioctls, in which case the field is
+        * silently ignored by the kernel.
+        */
+       if (counts.count_props < conn.count_props ||
+           counts.count_modes < conn.count_modes ||
+           counts.count_encoders < conn.count_encoders) {
+               drmFree(U642VOID(conn.props_ptr));
+               drmFree(U642VOID(conn.prop_values_ptr));
+               drmFree(U642VOID(conn.modes_ptr));
+               drmFree(U642VOID(conn.encoders_ptr));
+
+               goto retry;
+       }
+
+       if(!(r = drmMalloc(sizeof(*r)))) {
+               goto err_allocs;
+       }
+
+       r->connector_id = conn.connector_id;
+       r->encoder_id = conn.encoder_id;
+       r->connection   = conn.connection;
+       r->mmWidth      = conn.mm_width;
+       r->mmHeight     = conn.mm_height;
+       /* convert subpixel from kernel to userspace */
+       r->subpixel     = conn.subpixel + 1;
+       r->count_modes  = conn.count_modes;
+       r->count_props  = conn.count_props;
+       r->props        = drmAllocCpy(U642VOID(conn.props_ptr), conn.count_props, sizeof(uint32_t));
+       r->prop_values  = drmAllocCpy(U642VOID(conn.prop_values_ptr), conn.count_props, sizeof(uint64_t));
+       r->modes        = drmAllocCpy(U642VOID(conn.modes_ptr), conn.count_modes, sizeof(struct drm_mode_modeinfo));
+       r->count_encoders = conn.count_encoders;
+       r->encoders     = drmAllocCpy(U642VOID(conn.encoders_ptr), conn.count_encoders, sizeof(uint32_t));
+       r->connector_type  = conn.connector_type;
+       r->connector_type_id = conn.connector_type_id;
+
+       if ((r->count_props && !r->props) ||
+           (r->count_props && !r->prop_values) ||
+           (r->count_modes && !r->modes) ||
+           (r->count_encoders && !r->encoders)) {
+               drmFree(r->props);
+               drmFree(r->prop_values);
+               drmFree(r->modes);
+               drmFree(r->encoders);
+               drmFree(r);
+               r = 0;
+       }
+
+err_allocs:
+       drmFree(U642VOID(conn.prop_values_ptr));
+       drmFree(U642VOID(conn.props_ptr));
+       drmFree(U642VOID(conn.modes_ptr));
+       drmFree(U642VOID(conn.encoders_ptr));
+
+       return r;
+}
+
+int drmModeAttachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
+{
+       struct drm_mode_mode_cmd res;
+
+       memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
+       res.connector_id = connector_id;
+
+       return DRM_IOCTL(fd, DRM_IOCTL_MODE_ATTACHMODE, &res);
+}
+
+int drmModeDetachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
+{
+       struct drm_mode_mode_cmd res;
+
+       memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
+       res.connector_id = connector_id;
+
+       return DRM_IOCTL(fd, DRM_IOCTL_MODE_DETACHMODE, &res);
+}
+
+
+drmModePropertyPtr drmModeGetProperty(int fd, uint32_t property_id)
+{
+       struct drm_mode_get_property prop;
+       drmModePropertyPtr r;
+
+       prop.prop_id = property_id;
+       prop.count_enum_blobs = 0;
+       prop.count_values = 0;
+       prop.flags = 0;
+       prop.enum_blob_ptr = 0;
+       prop.values_ptr = 0;
+
+       if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop))
+               return 0;
+
+       if (prop.count_values)
+               prop.values_ptr = VOID2U64(drmMalloc(prop.count_values * sizeof(uint64_t)));
+
+       if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_ENUM))
+               prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(struct drm_mode_property_enum)));
+
+       if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_BLOB)) {
+               prop.values_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
+               prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
+       }
+
+       if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop)) {
+               r = NULL;
+               goto err_allocs;
+       }
+
+       if (!(r = drmMalloc(sizeof(*r))))
+               return NULL;
+
+       r->prop_id = prop.prop_id;
+       r->count_values = prop.count_values;
+
+       r->flags = prop.flags;
+       if (prop.count_values)
+               r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_values, sizeof(uint64_t));
+       if (prop.flags & DRM_MODE_PROP_ENUM) {
+               r->count_enums = prop.count_enum_blobs;
+               r->enums = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(struct drm_mode_property_enum));
+       } else if (prop.flags & DRM_MODE_PROP_BLOB) {
+               r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_enum_blobs, sizeof(uint32_t));
+               r->blob_ids = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(uint32_t));
+               r->count_blobs = prop.count_enum_blobs;
+       }
+       strncpy(r->name, prop.name, DRM_PROP_NAME_LEN);
+       r->name[DRM_PROP_NAME_LEN-1] = 0;
+
+err_allocs:
+       drmFree(U642VOID(prop.values_ptr));
+       drmFree(U642VOID(prop.enum_blob_ptr));
+
+       return r;
+}
+
+void drmModeFreeProperty(drmModePropertyPtr ptr)
+{
+       if (!ptr)
+               return;
+
+       drmFree(ptr->values);
+       drmFree(ptr->enums);
+       drmFree(ptr);
+}
+
+drmModePropertyBlobPtr drmModeGetPropertyBlob(int fd, uint32_t blob_id)
+{
+       struct drm_mode_get_blob blob;
+       drmModePropertyBlobPtr r;
+
+       blob.length = 0;
+       blob.data = 0;
+       blob.blob_id = blob_id;
+
+       if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob))
+               return NULL;
+
+       if (blob.length)
+               blob.data = VOID2U64(drmMalloc(blob.length));
+
+       if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob)) {
+               r = NULL;
+               goto err_allocs;
+       }
+
+       if (!(r = drmMalloc(sizeof(*r))))
+               goto err_allocs;
+
+       r->id = blob.blob_id;
+       r->length = blob.length;
+       r->data = drmAllocCpy(U642VOID(blob.data), 1, blob.length);
+
+err_allocs:
+       drmFree(U642VOID(blob.data));
+       return r;
+}
+
+void drmModeFreePropertyBlob(drmModePropertyBlobPtr ptr)
+{
+       if (!ptr)
+               return;
+
+       drmFree(ptr->data);
+       drmFree(ptr);
+}
+
+int drmModeConnectorSetProperty(int fd, uint32_t connector_id, uint32_t property_id,
+                            uint64_t value)
+{
+       struct drm_mode_connector_set_property osp;
+
+       osp.connector_id = connector_id;
+       osp.prop_id = property_id;
+       osp.value = value;
+
+       return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPROPERTY, &osp);
+}
+
+/*
+ * checks if a modesetting capable driver has attached to the pci id
+ * returns 0 if modesetting supported.
+ *  -EINVAL or invalid bus id
+ *  -ENOSYS if no modesetting support
+*/
+int drmCheckModesettingSupported(const char *busid)
+{
+#ifdef __linux__
+       char pci_dev_dir[1024];
+       int domain, bus, dev, func;
+       DIR *sysdir;
+       struct dirent *dent;
+       int found = 0, ret;
+
+       ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev, &func);
+       if (ret != 4)
+               return -EINVAL;
+
+       sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/drm",
+               domain, bus, dev, func);
+
+       sysdir = opendir(pci_dev_dir);
+       if (sysdir) {
+               dent = readdir(sysdir);
+               while (dent) {
+                       if (!strncmp(dent->d_name, "controlD", 8)) {
+                               found = 1;
+                               break;
+                       }
+
+                       dent = readdir(sysdir);
+               }
+               closedir(sysdir);
+               if (found)
+                       return 0;
+       }
+
+       sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/",
+               domain, bus, dev, func);
+
+       sysdir = opendir(pci_dev_dir);
+       if (!sysdir)
+               return -EINVAL;
+
+       dent = readdir(sysdir);
+       while (dent) {
+               if (!strncmp(dent->d_name, "drm:controlD", 12)) {
+                       found = 1;
+                       break;
+               }
+
+               dent = readdir(sysdir);
+       }
+
+       closedir(sysdir);
+       if (found)
+               return 0;
+#endif
+       return -ENOSYS;
+
+}
+
+int drmModeCrtcGetGamma(int fd, uint32_t crtc_id, uint32_t size,
+                       uint16_t *red, uint16_t *green, uint16_t *blue)
+{
+       struct drm_mode_crtc_lut l;
+
+       l.crtc_id = crtc_id;
+       l.gamma_size = size;
+       l.red = VOID2U64(red);
+       l.green = VOID2U64(green);
+       l.blue = VOID2U64(blue);
+
+       return DRM_IOCTL(fd, DRM_IOCTL_MODE_GETGAMMA, &l);
+}
+
+int drmModeCrtcSetGamma(int fd, uint32_t crtc_id, uint32_t size,
+                       uint16_t *red, uint16_t *green, uint16_t *blue)
+{
+       struct drm_mode_crtc_lut l;
+
+       l.crtc_id = crtc_id;
+       l.gamma_size = size;
+       l.red = VOID2U64(red);
+       l.green = VOID2U64(green);
+       l.blue = VOID2U64(blue);
+
+       return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETGAMMA, &l);
+}
+
+int drmHandleEvent(int fd, drmEventContextPtr evctx)
+{
+       char buffer[1024];
+       int len, i;
+       struct drm_event *e;
+       struct drm_event_vblank *vblank;
+       
+       /* The DRM read semantics guarantees that we always get only
+        * complete events. */
+
+       len = read(fd, buffer, sizeof buffer);
+       if (len == 0)
+               return 0;
+       if (len < sizeof *e)
+               return -1;
+
+       i = 0;
+       while (i < len) {
+               e = (struct drm_event *) &buffer[i];
+               switch (e->type) {
+               case DRM_EVENT_VBLANK:
+                       if (evctx->version < 1 ||
+                           evctx->vblank_handler == NULL)
+                               break;
+                       vblank = (struct drm_event_vblank *) e;
+                       evctx->vblank_handler(fd,
+                                             vblank->sequence, 
+                                             vblank->tv_sec,
+                                             vblank->tv_usec,
+                                             U642VOID (vblank->user_data));
+                       break;
+               case DRM_EVENT_FLIP_COMPLETE:
+                       if (evctx->version < 2 ||
+                           evctx->page_flip_handler == NULL)
+                               break;
+                       vblank = (struct drm_event_vblank *) e;
+                       evctx->page_flip_handler(fd,
+                                                vblank->sequence,
+                                                vblank->tv_sec,
+                                                vblank->tv_usec,
+                                                U642VOID (vblank->user_data));
+                       break;
+               default:
+                       break;
+               }
+               i += e->length;
+       }
+
+       return 0;
+}
+
+int drmModePageFlip(int fd, uint32_t crtc_id, uint32_t fb_id,
+                   uint32_t flags, void *user_data)
+{
+       struct drm_mode_crtc_page_flip flip;
+
+       flip.fb_id = fb_id;
+       flip.crtc_id = crtc_id;
+       flip.user_data = VOID2U64(user_data);
+       flip.flags = flags;
+       flip.reserved = 0;
+
+       return DRM_IOCTL(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip);
+}
+
+int drmModeSetPlane(int fd, uint32_t plane_id, uint32_t crtc_id,
+                   uint32_t fb_id, uint32_t crtc_x, uint32_t crtc_y,
+                   uint32_t crtc_w, uint32_t crtc_h,
+                   uint32_t src_x, uint32_t src_y,
+                   uint32_t src_w, uint32_t src_h)
+
+{
+       struct drm_mode_set_plane s;
+
+       s.plane_id = plane_id;
+       s.crtc_id = crtc_id;
+       s.fb_id = fb_id;
+       s.crtc_x = crtc_x;
+       s.crtc_y = crtc_y;
+       s.crtc_w = crtc_w;
+       s.crtc_h = crtc_h;
+       s.src_x = src_x;
+       s.src_y = src_y;
+       s.src_w = src_w;
+       s.src_h = src_h;
+
+       return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPLANE, &s);
+}
+
+
+drmModePlanePtr drmModeGetPlane(int fd, uint32_t plane_id)
+{
+       struct drm_mode_get_plane ovr, counts;
+       drmModePlanePtr r = 0;
+
+retry:
+       memset(&ovr, 0, sizeof(struct drm_mode_get_plane));
+       ovr.plane_id = plane_id;
+       if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr))
+               return 0;
+
+       counts = ovr;
+
+       if (ovr.count_format_types) {
+               ovr.format_type_ptr = VOID2U64(drmMalloc(ovr.count_format_types *
+                                                        sizeof(uint32_t)));
+               if (!ovr.format_type_ptr)
+                       goto err_allocs;
+       }
+
+       if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr))
+               goto err_allocs;
+
+       if (counts.count_format_types < ovr.count_format_types) {
+               drmFree(U642VOID(ovr.format_type_ptr));
+               goto retry;
+       }
+
+       if (!(r = drmMalloc(sizeof(*r))))
+               goto err_allocs;
+
+       r->count_formats = ovr.count_format_types;
+       r->plane_id = ovr.plane_id;
+       r->crtc_id = ovr.crtc_id;
+       r->fb_id = ovr.fb_id;
+       r->possible_crtcs = ovr.possible_crtcs;
+       r->gamma_size = ovr.gamma_size;
+       r->formats = drmAllocCpy(U642VOID(ovr.format_type_ptr),
+                                ovr.count_format_types, sizeof(uint32_t));
+       if (ovr.count_format_types && !r->formats) {
+               drmFree(r->formats);
+               r = 0;
+       }
+
+err_allocs:
+       drmFree(U642VOID(ovr.format_type_ptr));
+
+       return r;
+}
+
+void drmModeFreePlaneResources(drmModePlaneResPtr ptr)
+{
+       if (!ptr)
+               return;
+
+       drmFree(ptr->planes);
+       drmFree(ptr);
+}
+
+void drmModeFreePlane(drmModePlanePtr ptr)
+{
+       if (!ptr)
+               return;
+
+       drmFree(ptr->formats);
+       drmFree(ptr);
+}
+
+drmModePlaneResPtr drmModeGetPlaneResources(int fd)
+{
+       struct drm_mode_get_plane_res res, counts;
+       drmModePlaneResPtr r = 0;
+
+retry:
+       memset(&res, 0, sizeof(struct drm_mode_get_plane_res));
+       if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res))
+               return 0;
+
+       counts = res;
+
+       if (res.count_planes) {
+               res.plane_id_ptr = VOID2U64(drmMalloc(res.count_planes *
+                                                       sizeof(uint32_t)));
+               if (!res.plane_id_ptr)
+                       goto err_allocs;
+       }
+
+       if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res))
+               goto err_allocs;
+
+       if (counts.count_planes < res.count_planes) {
+               drmFree(U642VOID(res.plane_id_ptr));
+               goto retry;
+       }
+
+       if (!(r = drmMalloc(sizeof(*r))))
+               goto err_allocs;
+
+       r->count_planes = res.count_planes;
+       r->planes = drmAllocCpy(U642VOID(res.plane_id_ptr),
+                                 res.count_planes, sizeof(uint32_t));
+       if (res.count_planes && !r->planes) {
+               drmFree(r->planes);
+               r = 0;
+       }
+
+err_allocs:
+       drmFree(U642VOID(res.plane_id_ptr));
+
+       return r;
+}
diff --git a/xf86drmMode.h b/xf86drmMode.h
new file mode 100644 (file)
index 0000000..201719f
--- /dev/null
@@ -0,0 +1,429 @@
+/*
+ * \file xf86drmMode.h
+ * Header for DRM modesetting interface.
+ *
+ * \author Jakob Bornecrantz <wallbraker@gmail.com>
+ *
+ * \par Acknowledgements:
+ * Feb 2007, Dave Airlie <airlied@linux.ie>
+ */
+
+/*
+ * Copyright (c) 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas.
+ * Copyright (c) 2007-2008 Dave Airlie <airlied@linux.ie>
+ * Copyright (c) 2007-2008 Jakob Bornecrantz <wallbraker@gmail.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ */
+
+#if defined(__cplusplus) || defined(c_plusplus)
+extern "C" {
+#endif
+
+#include <drm.h>
+
+/*
+ * This is the interface for modesetting for drm.
+ *
+ * In order to use this interface you must include either <stdint.h> or another
+ * header defining uint32_t, int32_t and uint16_t.
+ *
+ * It aims to provide a randr1.2 compatible interface for modesettings in the
+ * kernel, the interface is also ment to be used by libraries like EGL.
+ *
+ * More information can be found in randrproto.txt which can be found here:
+ * http://gitweb.freedesktop.org/?p=xorg/proto/randrproto.git
+ *
+ * There are some major diffrences to be noted. Unlike the randr1.2 proto you
+ * need to create the memory object of the framebuffer yourself with the ttm
+ * buffer object interface. This object needs to be pinned.
+ */
+
+/*
+ * If we pickup an old version of drm.h which doesn't include drm_mode.h
+ * we should redefine defines. This is so that builds doesn't breaks with
+ * new libdrm on old kernels.
+ */
+#ifndef _DRM_MODE_H
+
+#define DRM_DISPLAY_INFO_LEN    32
+#define DRM_CONNECTOR_NAME_LEN  32
+#define DRM_DISPLAY_MODE_LEN    32
+#define DRM_PROP_NAME_LEN       32
+
+#define DRM_MODE_TYPE_BUILTIN   (1<<0)
+#define DRM_MODE_TYPE_CLOCK_C   ((1<<1) | DRM_MODE_TYPE_BUILTIN)
+#define DRM_MODE_TYPE_CRTC_C    ((1<<2) | DRM_MODE_TYPE_BUILTIN)
+#define DRM_MODE_TYPE_PREFERRED (1<<3)
+#define DRM_MODE_TYPE_DEFAULT   (1<<4)
+#define DRM_MODE_TYPE_USERDEF   (1<<5)
+#define DRM_MODE_TYPE_DRIVER    (1<<6)
+
+/* Video mode flags */
+/* bit compatible with the xorg definitions. */
+#define DRM_MODE_FLAG_PHSYNC    (1<<0)
+#define DRM_MODE_FLAG_NHSYNC    (1<<1)
+#define DRM_MODE_FLAG_PVSYNC    (1<<2)
+#define DRM_MODE_FLAG_NVSYNC    (1<<3)
+#define DRM_MODE_FLAG_INTERLACE (1<<4)
+#define DRM_MODE_FLAG_DBLSCAN   (1<<5)
+#define DRM_MODE_FLAG_CSYNC     (1<<6)
+#define DRM_MODE_FLAG_PCSYNC    (1<<7)
+#define DRM_MODE_FLAG_NCSYNC    (1<<8)
+#define DRM_MODE_FLAG_HSKEW     (1<<9) /* hskew provided */
+#define DRM_MODE_FLAG_BCAST     (1<<10)
+#define DRM_MODE_FLAG_PIXMUX    (1<<11)
+#define DRM_MODE_FLAG_DBLCLK    (1<<12)
+#define DRM_MODE_FLAG_CLKDIV2   (1<<13)
+
+/* DPMS flags */
+/* bit compatible with the xorg definitions. */
+#define DRM_MODE_DPMS_ON        0
+#define DRM_MODE_DPMS_STANDBY   1
+#define DRM_MODE_DPMS_SUSPEND   2
+#define DRM_MODE_DPMS_OFF       3
+
+/* Scaling mode options */
+#define DRM_MODE_SCALE_NON_GPU          0
+#define DRM_MODE_SCALE_FULLSCREEN       1
+#define DRM_MODE_SCALE_NO_SCALE         2
+#define DRM_MODE_SCALE_ASPECT           3
+
+/* Dithering mode options */
+#define DRM_MODE_DITHERING_OFF  0
+#define DRM_MODE_DITHERING_ON   1
+
+#define DRM_MODE_ENCODER_NONE   0
+#define DRM_MODE_ENCODER_DAC    1
+#define DRM_MODE_ENCODER_TMDS   2
+#define DRM_MODE_ENCODER_LVDS   3
+#define DRM_MODE_ENCODER_TVDAC  4
+
+#define DRM_MODE_SUBCONNECTOR_Automatic 0
+#define DRM_MODE_SUBCONNECTOR_Unknown   0
+#define DRM_MODE_SUBCONNECTOR_DVID      3
+#define DRM_MODE_SUBCONNECTOR_DVIA      4
+#define DRM_MODE_SUBCONNECTOR_Composite 5
+#define DRM_MODE_SUBCONNECTOR_SVIDEO    6
+#define DRM_MODE_SUBCONNECTOR_Component 8
+
+#define DRM_MODE_CONNECTOR_Unknown      0
+#define DRM_MODE_CONNECTOR_VGA          1
+#define DRM_MODE_CONNECTOR_DVII         2
+#define DRM_MODE_CONNECTOR_DVID         3
+#define DRM_MODE_CONNECTOR_DVIA         4
+#define DRM_MODE_CONNECTOR_Composite    5
+#define DRM_MODE_CONNECTOR_SVIDEO       6
+#define DRM_MODE_CONNECTOR_LVDS         7
+#define DRM_MODE_CONNECTOR_Component    8
+#define DRM_MODE_CONNECTOR_9PinDIN      9
+#define DRM_MODE_CONNECTOR_DisplayPort  10
+#define DRM_MODE_CONNECTOR_HDMIA        11
+#define DRM_MODE_CONNECTOR_HDMIB        12
+#define DRM_MODE_CONNECTOR_TV          13
+#define DRM_MODE_CONNECTOR_eDP         14
+
+#define DRM_MODE_PROP_PENDING   (1<<0)
+#define DRM_MODE_PROP_RANGE     (1<<1)
+#define DRM_MODE_PROP_IMMUTABLE (1<<2)
+#define DRM_MODE_PROP_ENUM      (1<<3) /* enumerated type with text strings */
+#define DRM_MODE_PROP_BLOB      (1<<4)
+
+#define DRM_MODE_CURSOR_BO      (1<<0)
+#define DRM_MODE_CURSOR_MOVE    (1<<1)
+
+#endif /* _DRM_MODE_H */
+
+
+/*
+ * Feature defines
+ *
+ * Just because these are defined doesn't mean that the kernel
+ * can do that feature, its just for new code vs old libdrm.
+ */
+#define DRM_MODE_FEATURE_KMS           1
+#define DRM_MODE_FEATURE_DIRTYFB       1
+
+
+typedef struct _drmModeRes {
+
+       int count_fbs;
+       uint32_t *fbs;
+
+       int count_crtcs;
+       uint32_t *crtcs;
+
+       int count_connectors;
+       uint32_t *connectors;
+
+       int count_encoders;
+       uint32_t *encoders;
+
+       uint32_t min_width, max_width;
+       uint32_t min_height, max_height;
+} drmModeRes, *drmModeResPtr;
+
+typedef struct _drmModeModeInfo {
+       uint32_t clock;
+       uint16_t hdisplay, hsync_start, hsync_end, htotal, hskew;
+       uint16_t vdisplay, vsync_start, vsync_end, vtotal, vscan;
+
+       uint32_t vrefresh;
+
+       uint32_t flags;
+       uint32_t type;
+       char name[DRM_DISPLAY_MODE_LEN];
+} drmModeModeInfo, *drmModeModeInfoPtr;
+
+typedef struct _drmModeFB {
+       uint32_t fb_id;
+       uint32_t width, height;
+       uint32_t pitch;
+       uint32_t bpp;
+       uint32_t depth;
+       /* driver specific handle */
+       uint32_t handle;
+} drmModeFB, *drmModeFBPtr;
+
+typedef struct drm_clip_rect drmModeClip, *drmModeClipPtr;
+
+typedef struct _drmModePropertyBlob {
+       uint32_t id;
+       uint32_t length;
+       void *data;
+} drmModePropertyBlobRes, *drmModePropertyBlobPtr;
+
+typedef struct _drmModeProperty {
+       uint32_t prop_id;
+       uint32_t flags;
+       char name[DRM_PROP_NAME_LEN];
+       int count_values;
+       uint64_t *values; // store the blob lengths
+       int count_enums;
+       struct drm_mode_property_enum *enums;
+       int count_blobs;
+       uint32_t *blob_ids; // store the blob IDs
+} drmModePropertyRes, *drmModePropertyPtr;
+
+typedef struct _drmModeCrtc {
+       uint32_t crtc_id;
+       uint32_t buffer_id; /**< FB id to connect to 0 = disconnect */
+
+       uint32_t x, y; /**< Position on the framebuffer */
+       uint32_t width, height;
+       int mode_valid;
+       drmModeModeInfo mode;
+
+       int gamma_size; /**< Number of gamma stops */
+
+} drmModeCrtc, *drmModeCrtcPtr;
+
+typedef struct _drmModeEncoder {
+       uint32_t encoder_id;
+       uint32_t encoder_type;
+       uint32_t crtc_id;
+       uint32_t possible_crtcs;
+       uint32_t possible_clones;
+} drmModeEncoder, *drmModeEncoderPtr;
+
+typedef enum {
+       DRM_MODE_CONNECTED         = 1,
+       DRM_MODE_DISCONNECTED      = 2,
+       DRM_MODE_UNKNOWNCONNECTION = 3
+} drmModeConnection;
+
+typedef enum {
+       DRM_MODE_SUBPIXEL_UNKNOWN        = 1,
+       DRM_MODE_SUBPIXEL_HORIZONTAL_RGB = 2,
+       DRM_MODE_SUBPIXEL_HORIZONTAL_BGR = 3,
+       DRM_MODE_SUBPIXEL_VERTICAL_RGB   = 4,
+       DRM_MODE_SUBPIXEL_VERTICAL_BGR   = 5,
+       DRM_MODE_SUBPIXEL_NONE           = 6
+} drmModeSubPixel;
+
+typedef struct _drmModeConnector {
+       uint32_t connector_id;
+       uint32_t encoder_id; /**< Encoder currently connected to */
+       uint32_t connector_type;
+       uint32_t connector_type_id;
+       drmModeConnection connection;
+       uint32_t mmWidth, mmHeight; /**< HxW in millimeters */
+       drmModeSubPixel subpixel;
+
+       int count_modes;
+       drmModeModeInfoPtr modes;
+
+       int count_props;
+       uint32_t *props; /**< List of property ids */
+       uint64_t *prop_values; /**< List of property values */
+
+       int count_encoders;
+       uint32_t *encoders; /**< List of encoder ids */
+} drmModeConnector, *drmModeConnectorPtr;
+
+typedef struct _drmModePlane {
+       uint32_t count_formats;
+       uint32_t *formats;
+       uint32_t plane_id;
+
+       uint32_t crtc_id;
+       uint32_t fb_id;
+
+       uint32_t crtc_x, crtc_y;
+       uint32_t x, y;
+
+       uint32_t possible_crtcs;
+       uint32_t gamma_size;
+} drmModePlane, *drmModePlanePtr;
+
+typedef struct _drmModePlaneRes {
+       uint32_t count_planes;
+       uint32_t *planes;
+} drmModePlaneRes, *drmModePlaneResPtr;
+
+extern void drmModeFreeModeInfo( drmModeModeInfoPtr ptr );
+extern void drmModeFreeResources( drmModeResPtr ptr );
+extern void drmModeFreeFB( drmModeFBPtr ptr );
+extern void drmModeFreeCrtc( drmModeCrtcPtr ptr );
+extern void drmModeFreeConnector( drmModeConnectorPtr ptr );
+extern void drmModeFreeEncoder( drmModeEncoderPtr ptr );
+extern void drmModeFreePlaneResources( drmModePlaneResPtr ptr );
+extern void drmModeFreePlane( drmModePlanePtr ptr );
+
+/**
+ * Retrives all of the resources associated with a card.
+ */
+extern drmModeResPtr drmModeGetResources(int fd);
+
+/*
+ * FrameBuffer manipulation.
+ */
+
+/**
+ * Retrive information about framebuffer bufferId
+ */
+extern drmModeFBPtr drmModeGetFB(int fd, uint32_t bufferId);
+
+/**
+ * Creates a new framebuffer with an buffer object as its scanout buffer.
+ */
+extern int drmModeAddFB(int fd, uint32_t width, uint32_t height, uint8_t depth,
+                       uint8_t bpp, uint32_t pitch, uint32_t bo_handle,
+                       uint32_t *buf_id);
+/* ...with a specific pixel format */
+extern int drmModeAddFB2(int fd, uint32_t width, uint32_t height,
+                        uint32_t pixel_format, uint32_t bo_handles[4],
+                        uint32_t pitches[4], uint32_t offsets[4],
+                        uint32_t *buf_id);
+/**
+ * Destroies the given framebuffer.
+ */
+extern int drmModeRmFB(int fd, uint32_t bufferId);
+
+/**
+ * Mark a region of a framebuffer as dirty.
+ */
+extern int drmModeDirtyFB(int fd, uint32_t bufferId,
+                         drmModeClipPtr clips, uint32_t num_clips);
+
+
+/*
+ * Crtc functions
+ */
+
+/**
+ * Retrive information about the ctrt crtcId
+ */
+extern drmModeCrtcPtr drmModeGetCrtc(int fd, uint32_t crtcId);
+
+/**
+ * Set the mode on a crtc crtcId with the given mode modeId.
+ */
+int drmModeSetCrtc(int fd, uint32_t crtcId, uint32_t bufferId,
+                   uint32_t x, uint32_t y, uint32_t *connectors, int count,
+                  drmModeModeInfoPtr mode);
+
+/*
+ * Cursor functions
+ */
+
+/**
+ * Set the cursor on crtc
+ */
+int drmModeSetCursor(int fd, uint32_t crtcId, uint32_t bo_handle, uint32_t width, uint32_t height);
+
+/**
+ * Move the cursor on crtc
+ */
+int drmModeMoveCursor(int fd, uint32_t crtcId, int x, int y);
+
+/**
+ * Encoder functions
+ */
+drmModeEncoderPtr drmModeGetEncoder(int fd, uint32_t encoder_id);
+
+/*
+ * Connector manipulation
+ */
+
+/**
+ * Retrive information about the connector connectorId.
+ */
+extern drmModeConnectorPtr drmModeGetConnector(int fd,
+               uint32_t connectorId);
+
+/**
+ * Attaches the given mode to an connector.
+ */
+extern int drmModeAttachMode(int fd, uint32_t connectorId, drmModeModeInfoPtr mode_info);
+
+/**
+ * Detaches a mode from the connector
+ * must be unused, by the given mode.
+ */
+extern int drmModeDetachMode(int fd, uint32_t connectorId, drmModeModeInfoPtr mode_info);
+
+extern drmModePropertyPtr drmModeGetProperty(int fd, uint32_t propertyId);
+extern void drmModeFreeProperty(drmModePropertyPtr ptr);
+
+extern drmModePropertyBlobPtr drmModeGetPropertyBlob(int fd, uint32_t blob_id);
+extern void drmModeFreePropertyBlob(drmModePropertyBlobPtr ptr);
+extern int drmModeConnectorSetProperty(int fd, uint32_t connector_id, uint32_t property_id,
+                                   uint64_t value);
+extern int drmCheckModesettingSupported(const char *busid);
+
+extern int drmModeCrtcSetGamma(int fd, uint32_t crtc_id, uint32_t size,
+                              uint16_t *red, uint16_t *green, uint16_t *blue);
+extern int drmModeCrtcGetGamma(int fd, uint32_t crtc_id, uint32_t size,
+                              uint16_t *red, uint16_t *green, uint16_t *blue);
+extern int drmModePageFlip(int fd, uint32_t crtc_id, uint32_t fb_id,
+                          uint32_t flags, void *user_data);
+
+extern drmModePlaneResPtr drmModeGetPlaneResources(int fd);
+extern drmModePlanePtr drmModeGetPlane(int fd, uint32_t plane_id);
+extern int drmModeSetPlane(int fd, uint32_t plane_id, uint32_t crtc_id,
+                          uint32_t fb_id, uint32_t crtc_x, uint32_t crtc_y,
+                          uint32_t crtc_w, uint32_t crtc_h,
+                          uint32_t src_x, uint32_t src_y,
+                          uint32_t src_w, uint32_t src_h);
+
+#if defined(__cplusplus) || defined(c_plusplus)
+}
+#endif
diff --git a/xf86drmRandom.c b/xf86drmRandom.c
new file mode 100644 (file)
index 0000000..ecab9e2
--- /dev/null
@@ -0,0 +1,208 @@
+/* xf86drmRandom.c -- "Minimal Standard" PRNG Implementation
+ * Created: Mon Apr 19 08:28:13 1999 by faith@precisioninsight.com
+ *
+ * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ * 
+ * Authors: Rickard E. (Rik) Faith <faith@valinux.com>
+ *
+ * DESCRIPTION
+ *
+ * This file contains a simple, straightforward implementation of the Park
+ * & Miller "Minimal Standard" PRNG [PM88, PMS93], which is a Lehmer
+ * multiplicative linear congruential generator (MLCG) with a period of
+ * 2^31-1.
+ *
+ * This implementation is intended to provide a reliable, portable PRNG
+ * that is suitable for testing a hash table implementation and for
+ * implementing skip lists.
+ *
+ * FUTURE ENHANCEMENTS
+ *
+ * If initial seeds are not selected randomly, two instances of the PRNG
+ * can be correlated.  [Knuth81, pp. 32-33] describes a shuffling technique
+ * that can eliminate this problem.
+ *
+ * If PRNGs are used for simulation, the period of the current
+ * implementation may be too short.  [LE88] discusses methods of combining
+ * MLCGs to produce much longer periods, and suggests some alternative
+ * values for A and M.  [LE90 and Sch92] also provide information on
+ * long-period PRNGs.
+ *
+ * REFERENCES
+ *
+ * [Knuth81] Donald E. Knuth. The Art of Computer Programming.  Volume 2:
+ * Seminumerical Algorithms.  Reading, Massachusetts: Addison-Wesley, 1981.
+ *
+ * [LE88] Pierre L'Ecuyer. "Efficient and Portable Combined Random Number
+ * Generators".  CACM 31(6), June 1988, pp. 742-774.
+ *
+ * [LE90] Pierre L'Ecuyer. "Random Numbers for Simulation". CACM 33(10,
+ * October 1990, pp. 85-97.
+ *
+ * [PM88] Stephen K. Park and Keith W. Miller. "Random Number Generators:
+ * Good Ones are Hard to Find". CACM 31(10), October 1988, pp. 1192-1201.
+ *
+ * [Sch92] Bruce Schneier. "Pseudo-Ransom Sequence Generator for 32-Bit
+ * CPUs".  Dr. Dobb's Journal 17(2), February 1992, pp. 34, 37-38, 40.
+ *
+ * [PMS93] Stephen K. Park, Keith W. Miller, and Paul K. Stockmeyer.  In
+ * "Technical Correspondence: Remarks on Choosing and Implementing Random
+ * Number Generators". CACM 36(7), July 1993, pp. 105-110.
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#define RANDOM_MAIN 0
+
+#if !RANDOM_MAIN
+# include "xf86drm.h"
+#endif
+
+#define RANDOM_MAGIC 0xfeedbeef
+#define RANDOM_DEBUG 0
+
+#if RANDOM_MAIN
+#define RANDOM_ALLOC malloc
+#define RANDOM_FREE  free
+#else
+#define RANDOM_ALLOC drmMalloc
+#define RANDOM_FREE  drmFree
+#endif
+
+typedef struct RandomState {
+    unsigned long magic;
+    unsigned long a;
+    unsigned long m;
+    unsigned long q;           /* m div a */
+    unsigned long r;           /* m mod a */
+    unsigned long check;
+    long          seed;
+} RandomState;
+
+#if RANDOM_MAIN
+extern void          *drmRandomCreate(unsigned long seed);
+extern int           drmRandomDestroy(void *state);
+extern unsigned long drmRandom(void *state);
+extern double        drmRandomDouble(void *state);
+#endif
+
+void *drmRandomCreate(unsigned long seed)
+{
+    RandomState  *state;
+
+    state           = RANDOM_ALLOC(sizeof(*state));
+    if (!state) return NULL;
+    state->magic    = RANDOM_MAGIC;
+#if 0
+                               /* Park & Miller, October 1988 */
+    state->a        = 16807;
+    state->m        = 2147483647;
+    state->check    = 1043618065; /* After 10000 iterations */
+#else
+                               /* Park, Miller, and Stockmeyer, July 1993 */
+    state->a        = 48271;
+    state->m        = 2147483647;
+    state->check    = 399268537; /* After 10000 iterations */
+#endif
+    state->q        = state->m / state->a;
+    state->r        = state->m % state->a;
+
+    state->seed     = seed;
+                               /* Check for illegal boundary conditions,
+                                   and choose closest legal value. */
+    if (state->seed <= 0)        state->seed = 1;
+    if (state->seed >= state->m) state->seed = state->m - 1;
+
+    return state;
+}
+
+int drmRandomDestroy(void *state)
+{
+    RANDOM_FREE(state);
+    return 0;
+}
+
+unsigned long drmRandom(void *state)
+{
+    RandomState   *s = (RandomState *)state;
+    long          hi;
+    long          lo;
+
+    hi      = s->seed / s->q;
+    lo      = s->seed % s->q;
+    s->seed = s->a * lo - s->r * hi;
+    if (s->seed <= 0) s->seed += s->m;
+
+    return s->seed;
+}
+
+double drmRandomDouble(void *state)
+{
+    RandomState *s = (RandomState *)state;
+    
+    return (double)drmRandom(state)/(double)s->m;
+}
+
+#if RANDOM_MAIN
+static void check_period(long seed)
+{
+    unsigned long count = 0;
+    unsigned long initial;
+    void          *state;
+    
+    state = drmRandomCreate(seed);
+    initial = drmRandom(state);
+    ++count;
+    while (initial != drmRandom(state)) {
+       if (!++count) break;
+    }
+    printf("With seed of %10ld, period = %10lu (0x%08lx)\n",
+          seed, count, count);
+    drmRandomDestroy(state);
+}
+
+int main(void)
+{
+    RandomState   *state;
+    int           i;
+    unsigned long rand;
+
+    state = drmRandomCreate(1);
+    for (i = 0; i < 10000; i++) {
+       rand = drmRandom(state);
+    }
+    printf("After 10000 iterations: %lu (%lu expected): %s\n",
+          rand, state->check,
+          rand - state->check ? "*INCORRECT*" : "CORRECT");
+    drmRandomDestroy(state);
+
+    printf("Checking periods...\n");
+    check_period(1);
+    check_period(2);
+    check_period(31415926);
+    
+    return 0;
+}
+#endif
diff --git a/xf86drmSL.c b/xf86drmSL.c
new file mode 100644 (file)
index 0000000..1937507
--- /dev/null
@@ -0,0 +1,477 @@
+/* xf86drmSL.c -- Skip list support
+ * Created: Mon May 10 09:28:13 1999 by faith@precisioninsight.com
+ *
+ * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ * 
+ * Authors: Rickard E. (Rik) Faith <faith@valinux.com>
+ *
+ * DESCRIPTION
+ *
+ * This file contains a straightforward skip list implementation.n
+ *
+ * FUTURE ENHANCEMENTS
+ *
+ * REFERENCES
+ *
+ * [Pugh90] William Pugh.  Skip Lists: A Probabilistic Alternative to
+ * Balanced Trees. CACM 33(6), June 1990, pp. 668-676.
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#define SL_MAIN 0
+
+#if !SL_MAIN
+# include "xf86drm.h"
+#else
+# include <sys/time.h>
+#endif
+
+#define SL_LIST_MAGIC  0xfacade00LU
+#define SL_ENTRY_MAGIC 0x00fab1edLU
+#define SL_FREED_MAGIC 0xdecea5edLU
+#define SL_MAX_LEVEL   16
+#define SL_DEBUG       0
+#define SL_RANDOM_SEED 0xc01055a1LU
+
+#if SL_MAIN
+#define SL_ALLOC malloc
+#define SL_FREE  free
+#define SL_RANDOM_DECL        static int state = 0;
+#define SL_RANDOM_INIT(seed)  if (!state) { srandom(seed); ++state; }
+#define SL_RANDOM             random()
+#else
+#define SL_ALLOC drmMalloc
+#define SL_FREE  drmFree
+#define SL_RANDOM_DECL        static void *state = NULL
+#define SL_RANDOM_INIT(seed)  if (!state) state = drmRandomCreate(seed)
+#define SL_RANDOM             drmRandom(state)
+
+#endif
+
+typedef struct SLEntry {
+    unsigned long     magic;      /* SL_ENTRY_MAGIC */
+    unsigned long     key;
+    void              *value;
+    int               levels;
+    struct SLEntry    *forward[1]; /* variable sized array */
+} SLEntry, *SLEntryPtr;
+
+typedef struct SkipList {
+    unsigned long    magic;    /* SL_LIST_MAGIC */
+    int              level;
+    int              count;
+    SLEntryPtr       head;
+    SLEntryPtr       p0;       /* Position for iteration */
+} SkipList, *SkipListPtr;
+
+#if SL_MAIN
+extern void *drmSLCreate(void);
+extern int  drmSLDestroy(void *l);
+extern int  drmSLLookup(void *l, unsigned long key, void **value);
+extern int  drmSLInsert(void *l, unsigned long key, void *value);
+extern int  drmSLDelete(void *l, unsigned long key);
+extern int  drmSLNext(void *l, unsigned long *key, void **value);
+extern int  drmSLFirst(void *l, unsigned long *key, void **value);
+extern void drmSLDump(void *l);
+extern int  drmSLLookupNeighbors(void *l, unsigned long key,
+                                unsigned long *prev_key, void **prev_value,
+                                unsigned long *next_key, void **next_value);
+#endif
+
+static SLEntryPtr SLCreateEntry(int max_level, unsigned long key, void *value)
+{
+    SLEntryPtr entry;
+    
+    if (max_level < 0 || max_level > SL_MAX_LEVEL) max_level = SL_MAX_LEVEL;
+
+    entry         = SL_ALLOC(sizeof(*entry)
+                            + (max_level + 1) * sizeof(entry->forward[0]));
+    if (!entry) return NULL;
+    entry->magic  = SL_ENTRY_MAGIC;
+    entry->key    = key;
+    entry->value  = value;
+    entry->levels = max_level + 1;
+
+    return entry;
+}
+
+static int SLRandomLevel(void)
+{
+    int level = 1;
+    SL_RANDOM_DECL;
+
+    SL_RANDOM_INIT(SL_RANDOM_SEED);
+    
+    while ((SL_RANDOM & 0x01) && level < SL_MAX_LEVEL) ++level;
+    return level;
+}
+
+void *drmSLCreate(void)
+{
+    SkipListPtr  list;
+    int          i;
+
+    list           = SL_ALLOC(sizeof(*list));
+    if (!list) return NULL;
+    list->magic    = SL_LIST_MAGIC;
+    list->level    = 0;
+    list->head     = SLCreateEntry(SL_MAX_LEVEL, 0, NULL);
+    list->count    = 0;
+
+    for (i = 0; i <= SL_MAX_LEVEL; i++) list->head->forward[i] = NULL;
+    
+    return list;
+}
+
+int drmSLDestroy(void *l)
+{
+    SkipListPtr   list  = (SkipListPtr)l;
+    SLEntryPtr    entry;
+    SLEntryPtr    next;
+
+    if (list->magic != SL_LIST_MAGIC) return -1; /* Bad magic */
+
+    for (entry = list->head; entry; entry = next) {
+       if (entry->magic != SL_ENTRY_MAGIC) return -1; /* Bad magic */
+       next         = entry->forward[0];
+       entry->magic = SL_FREED_MAGIC;
+       SL_FREE(entry);
+    }
+
+    list->magic = SL_FREED_MAGIC;
+    SL_FREE(list);
+    return 0;
+}
+
+static SLEntryPtr SLLocate(void *l, unsigned long key, SLEntryPtr *update)
+{
+    SkipListPtr   list  = (SkipListPtr)l;
+    SLEntryPtr    entry;
+    int           i;
+
+    if (list->magic != SL_LIST_MAGIC) return NULL;
+
+    for (i = list->level, entry = list->head; i >= 0; i--) {
+       while (entry->forward[i] && entry->forward[i]->key < key)
+           entry = entry->forward[i];
+       update[i] = entry;
+    }
+
+    return entry->forward[0];
+}
+
+int drmSLInsert(void *l, unsigned long key, void *value)
+{
+    SkipListPtr   list  = (SkipListPtr)l;
+    SLEntryPtr    entry;
+    SLEntryPtr    update[SL_MAX_LEVEL + 1];
+    int           level;
+    int           i;
+
+    if (list->magic != SL_LIST_MAGIC) return -1; /* Bad magic */
+
+    entry = SLLocate(list, key, update);
+
+    if (entry && entry->key == key) return 1; /* Already in list */
+
+
+    level = SLRandomLevel();
+    if (level > list->level) {
+       level = ++list->level;
+       update[level] = list->head;
+    }
+
+    entry = SLCreateEntry(level, key, value);
+
+                               /* Fix up forward pointers */
+    for (i = 0; i <= level; i++) {
+       entry->forward[i]     = update[i]->forward[i];
+       update[i]->forward[i] = entry;
+    }
+
+    ++list->count;
+    return 0;                  /* Added to table */
+}
+
+int drmSLDelete(void *l, unsigned long key)
+{
+    SkipListPtr   list = (SkipListPtr)l;
+    SLEntryPtr    update[SL_MAX_LEVEL + 1];
+    SLEntryPtr    entry;
+    int           i;
+
+    if (list->magic != SL_LIST_MAGIC) return -1; /* Bad magic */
+
+    entry = SLLocate(list, key, update);
+
+    if (!entry || entry->key != key) return 1; /* Not found */
+
+                               /* Fix up forward pointers */
+    for (i = 0; i <= list->level; i++) {
+       if (update[i]->forward[i] == entry)
+           update[i]->forward[i] = entry->forward[i];
+    }
+
+    entry->magic = SL_FREED_MAGIC;
+    SL_FREE(entry);
+
+    while (list->level && !list->head->forward[list->level]) --list->level;
+    --list->count;
+    return 0;
+}
+
+int drmSLLookup(void *l, unsigned long key, void **value)
+{
+    SkipListPtr   list = (SkipListPtr)l;
+    SLEntryPtr    update[SL_MAX_LEVEL + 1];
+    SLEntryPtr    entry;
+
+    entry = SLLocate(list, key, update);
+
+    if (entry && entry->key == key) {
+       *value = entry->value;
+       return 0;
+    }
+    *value = NULL;
+    return -1;
+}
+
+int drmSLLookupNeighbors(void *l, unsigned long key,
+                        unsigned long *prev_key, void **prev_value,
+                        unsigned long *next_key, void **next_value)
+{
+    SkipListPtr   list = (SkipListPtr)l;
+    SLEntryPtr    update[SL_MAX_LEVEL + 1];
+    int           retcode = 0;
+
+    *prev_key   = *next_key   = key;
+    *prev_value = *next_value = NULL;
+       
+    if (update[0]) {
+       *prev_key   = update[0]->key;
+       *prev_value = update[0]->value;
+       ++retcode;
+       if (update[0]->forward[0]) {
+           *next_key   = update[0]->forward[0]->key;
+           *next_value = update[0]->forward[0]->value;
+           ++retcode;
+       }
+    }
+    return retcode;
+}
+
+int drmSLNext(void *l, unsigned long *key, void **value)
+{
+    SkipListPtr   list = (SkipListPtr)l;
+    SLEntryPtr    entry;
+    
+    if (list->magic != SL_LIST_MAGIC) return -1; /* Bad magic */
+
+    entry    = list->p0;
+
+    if (entry) {
+       list->p0 = entry->forward[0];
+       *key     = entry->key;
+       *value   = entry->value;
+       return 1;
+    }
+    list->p0 = NULL;
+    return 0;
+}
+
+int drmSLFirst(void *l, unsigned long *key, void **value)
+{
+    SkipListPtr   list = (SkipListPtr)l;
+    
+    if (list->magic != SL_LIST_MAGIC) return -1; /* Bad magic */
+    
+    list->p0 = list->head->forward[0];
+    return drmSLNext(list, key, value);
+}
+
+/* Dump internal data structures for debugging. */
+void drmSLDump(void *l)
+{
+    SkipListPtr   list = (SkipListPtr)l;
+    SLEntryPtr    entry;
+    int           i;
+    
+    if (list->magic != SL_LIST_MAGIC) {
+       printf("Bad magic: 0x%08lx (expected 0x%08lx)\n",
+              list->magic, SL_LIST_MAGIC);
+       return;
+    }
+
+    printf("Level = %d, count = %d\n", list->level, list->count);
+    for (entry = list->head; entry; entry = entry->forward[0]) {
+       if (entry->magic != SL_ENTRY_MAGIC) {
+           printf("Bad magic: 0x%08lx (expected 0x%08lx)\n",
+                  list->magic, SL_ENTRY_MAGIC);
+       }
+       printf("\nEntry %p <0x%08lx, %p> has %2d levels\n",
+              entry, entry->key, entry->value, entry->levels);
+       for (i = 0; i < entry->levels; i++) {
+           if (entry->forward[i]) {
+               printf("   %2d: %p <0x%08lx, %p>\n",
+                      i,
+                      entry->forward[i],
+                      entry->forward[i]->key,
+                      entry->forward[i]->value);
+           } else {
+               printf("   %2d: %p\n", i, entry->forward[i]);
+           }
+       }
+    }
+}
+
+#if SL_MAIN
+static void print(SkipListPtr list)
+{
+    unsigned long key;
+    void          *value;
+    
+    if (drmSLFirst(list, &key, &value)) {
+       do {
+           printf("key = %5lu, value = %p\n", key, value);
+       } while (drmSLNext(list, &key, &value));
+    }
+}
+
+static double do_time(int size, int iter)
+{
+    SkipListPtr    list;
+    int            i, j;
+    unsigned long  keys[1000000];
+    unsigned long  previous;
+    unsigned long  key;
+    void           *value;
+    struct timeval start, stop;
+    double         usec;
+    SL_RANDOM_DECL;
+
+    SL_RANDOM_INIT(12345);
+    
+    list = drmSLCreate();
+
+    for (i = 0; i < size; i++) {
+       keys[i] = SL_RANDOM;
+       drmSLInsert(list, keys[i], NULL);
+    }
+
+    previous = 0;
+    if (drmSLFirst(list, &key, &value)) {
+       do {
+           if (key <= previous) {
+               printf( "%lu !< %lu\n", previous, key);
+           }
+           previous = key;
+       } while (drmSLNext(list, &key, &value));
+    }
+    
+    gettimeofday(&start, NULL);
+    for (j = 0; j < iter; j++) {
+       for (i = 0; i < size; i++) {
+           if (drmSLLookup(list, keys[i], &value))
+               printf("Error %lu %d\n", keys[i], i);
+       }
+    }
+    gettimeofday(&stop, NULL);
+    
+    usec = (double)(stop.tv_sec * 1000000 + stop.tv_usec
+                   - start.tv_sec * 1000000 - start.tv_usec) / (size * iter);
+    
+    printf("%0.2f microseconds for list length %d\n", usec, size);
+
+    drmSLDestroy(list);
+    
+    return usec;
+}
+
+static void print_neighbors(void *list, unsigned long key)
+{
+    unsigned long prev_key = 0;
+    unsigned long next_key = 0;
+    void          *prev_value;
+    void          *next_value;
+    int           retval;
+
+    retval = drmSLLookupNeighbors(list, key,
+                                 &prev_key, &prev_value,
+                                 &next_key, &next_value);
+    printf("Neighbors of %5lu: %d %5lu %5lu\n",
+          key, retval, prev_key, next_key);
+}
+
+int main(void)
+{
+    SkipListPtr    list;
+    double         usec, usec2, usec3, usec4;
+
+    list = drmSLCreate();
+    printf( "list at %p\n", list);
+
+    print(list);
+    printf("\n==============================\n\n");
+
+    drmSLInsert(list, 123, NULL);
+    drmSLInsert(list, 213, NULL);
+    drmSLInsert(list, 50, NULL);
+    print(list);
+    printf("\n==============================\n\n");
+    
+    print_neighbors(list, 0);
+    print_neighbors(list, 50);
+    print_neighbors(list, 51);
+    print_neighbors(list, 123);
+    print_neighbors(list, 200);
+    print_neighbors(list, 213);
+    print_neighbors(list, 256);
+    printf("\n==============================\n\n");    
+    
+    drmSLDelete(list, 50);
+    print(list);
+    printf("\n==============================\n\n");
+
+    drmSLDump(list);
+    drmSLDestroy(list);
+    printf("\n==============================\n\n");
+
+    usec  = do_time(100, 10000);
+    usec2 = do_time(1000, 500);
+    printf("Table size increased by %0.2f, search time increased by %0.2f\n",
+          1000.0/100.0, usec2 / usec);
+    
+    usec3 = do_time(10000, 50);
+    printf("Table size increased by %0.2f, search time increased by %0.2f\n",
+          10000.0/100.0, usec3 / usec);
+    
+    usec4 = do_time(100000, 4);
+    printf("Table size increased by %0.2f, search time increased by %0.2f\n",
+          100000.0/100.0, usec4 / usec);
+
+    return 0;
+}
+#endif
diff --git a/xf86mm.h b/xf86mm.h
new file mode 100644 (file)
index 0000000..a31de42
--- /dev/null
+++ b/xf86mm.h
@@ -0,0 +1,198 @@
+/**************************************************************************
+ * 
+ * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND. USA.
+ * All Rights Reserved.
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 
+ * USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ * 
+ * 
+ **************************************************************************/
+
+#ifndef _XF86MM_H_
+#define _XF86MM_H_
+#include <stddef.h>
+#include <stdint.h>
+#include "drm.h"
+
+/*
+ * Note on multithreaded applications using this interface.
+ * Libdrm is not threadsafe, so common buffer, TTM, and fence objects need to
+ * be protected using an external mutex.
+ *
+ * Note: Don't protect the following functions, as it may lead to deadlocks:
+ * drmBOUnmap().
+ * The kernel is synchronizing and refcounting buffer maps. 
+ * User space only needs to refcount object usage within the same application.
+ */
+
+
+/*
+ * List macros heavily inspired by the Linux kernel
+ * list handling. No list looping yet.
+ */
+
+typedef struct _drmMMListHead
+{
+    struct _drmMMListHead *prev;
+    struct _drmMMListHead *next;
+} drmMMListHead;
+
+#define DRMINITLISTHEAD(__item)                       \
+  do{                                         \
+    (__item)->prev = (__item);                \
+    (__item)->next = (__item);                \
+  } while (0)
+
+#define DRMLISTADD(__item, __list)             \
+  do {                                         \
+    (__item)->prev = (__list);                 \
+    (__item)->next = (__list)->next;           \
+    (__list)->next->prev = (__item);           \
+    (__list)->next = (__item);                 \
+  } while (0)
+
+#define DRMLISTADDTAIL(__item, __list)         \
+  do {                                         \
+    (__item)->next = (__list);                 \
+    (__item)->prev = (__list)->prev;           \
+    (__list)->prev->next = (__item);           \
+    (__list)->prev = (__item);                 \
+  } while(0)
+
+#define DRMLISTDEL(__item)                     \
+  do {                                         \
+    (__item)->prev->next = (__item)->next;     \
+    (__item)->next->prev = (__item)->prev;     \
+  } while(0)
+
+#define DRMLISTDELINIT(__item)                 \
+  do {                                         \
+    (__item)->prev->next = (__item)->next;     \
+    (__item)->next->prev = (__item)->prev;     \
+    (__item)->next = (__item);                 \
+    (__item)->prev = (__item);                 \
+  } while(0)
+
+#define DRMLISTENTRY(__type, __item, __field)   \
+    ((__type *)(((char *) (__item)) - offsetof(__type, __field)))
+
+#define DRMLISTEMPTY(__item) ((__item)->next == (__item))
+
+#define DRMLISTFOREACHSAFE(__item, __temp, __list)                     \
+       for ((__item) = (__list)->next, (__temp) = (__item)->next;      \
+            (__item) != (__list);                                      \
+            (__item) = (__temp), (__temp) = (__item)->next)
+
+#define DRMLISTFOREACHSAFEREVERSE(__item, __temp, __list)              \
+       for ((__item) = (__list)->prev, (__temp) = (__item)->prev;      \
+            (__item) != (__list);                                      \
+            (__item) = (__temp), (__temp) = (__item)->prev)
+
+typedef struct _drmFence
+{
+    unsigned handle;
+    int fence_class;
+    unsigned type; 
+    unsigned flags;
+    unsigned signaled;
+    uint32_t sequence;
+    unsigned pad[4]; /* for future expansion */
+} drmFence;
+
+typedef struct _drmBO
+{
+    unsigned handle;
+    uint64_t mapHandle;
+    uint64_t flags;
+    uint64_t proposedFlags;
+    unsigned mapFlags;
+    unsigned long size;
+    unsigned long offset;
+    unsigned long start;
+    unsigned replyFlags;
+    unsigned fenceFlags;
+    unsigned pageAlignment;
+    unsigned tileInfo;
+    unsigned hwTileStride;
+    unsigned desiredTileStride;
+    void *virtual;
+    void *mapVirtual;
+    int mapCount;
+    unsigned pad[8];     /* for future expansion */
+} drmBO;
+
+/*
+ * Fence functions.
+ */
+
+extern int drmFenceCreate(int fd, unsigned flags, int fence_class,
+                          unsigned type, drmFence *fence);
+extern int drmFenceReference(int fd, unsigned handle, drmFence *fence);
+extern int drmFenceUnreference(int fd, const drmFence *fence);
+extern int drmFenceFlush(int fd, drmFence *fence, unsigned flush_type);
+extern int drmFenceSignaled(int fd, drmFence *fence, 
+                            unsigned fenceType, int *signaled);
+extern int drmFenceWait(int fd, unsigned flags, drmFence *fence, 
+                        unsigned flush_type);
+extern int drmFenceEmit(int fd, unsigned flags, drmFence *fence, 
+                        unsigned emit_type);
+extern int drmFenceBuffers(int fd, unsigned flags, uint32_t fence_class, drmFence *fence);
+
+
+/*
+ * Buffer object functions.
+ */
+
+extern int drmBOCreate(int fd, unsigned long size,
+                      unsigned pageAlignment, void *user_buffer,
+                      uint64_t mask, unsigned hint, drmBO *buf);
+extern int drmBOReference(int fd, unsigned handle, drmBO *buf);
+extern int drmBOUnreference(int fd, drmBO *buf);
+extern int drmBOMap(int fd, drmBO *buf, unsigned mapFlags, unsigned mapHint,
+                   void **address);
+extern int drmBOUnmap(int fd, drmBO *buf);
+extern int drmBOFence(int fd, drmBO *buf, unsigned flags, unsigned fenceHandle);
+extern int drmBOInfo(int fd, drmBO *buf);
+extern int drmBOBusy(int fd, drmBO *buf, int *busy);
+
+extern int drmBOWaitIdle(int fd, drmBO *buf, unsigned hint);
+
+/*
+ * Initialization functions.
+ */
+
+extern int drmMMInit(int fd, unsigned long pOffset, unsigned long pSize,
+                    unsigned memType);
+extern int drmMMTakedown(int fd, unsigned memType);
+extern int drmMMLock(int fd, unsigned memType, int lockBM, int ignoreNoEvict);
+extern int drmMMUnlock(int fd, unsigned memType, int unlockBM);
+extern int drmMMInfo(int fd, unsigned memType, uint64_t *size);
+extern int drmBOSetStatus(int fd, drmBO *buf, 
+                         uint64_t flags, uint64_t mask,
+                         unsigned int hint, 
+                         unsigned int desired_tile_stride,
+                         unsigned int tile_info);
+extern int drmBOVersion(int fd, unsigned int *major,
+                       unsigned int *minor,
+                       unsigned int *patchlevel);
+
+
+#endif