f935a16
diff --git a/drivers/gpu/drm/Kconfig b/drivers/gpu/drm/Kconfig
f935a16
index 2418429..566c468 100644
f935a16
--- a/drivers/gpu/drm/Kconfig
f935a16
+++ b/drivers/gpu/drm/Kconfig
f935a16
@@ -159,6 +159,14 @@ config DRM_SAVAGE
f935a16
 	  Choose this option if you have a Savage3D/4/SuperSavage/Pro/Twister
f935a16
 	  chipset. If M is selected the module will be called savage.
f935a16
 
f935a16
+config DRM_VGEM
f935a16
+	tristate "Virtual GEM provider"
f935a16
+	depends on DRM
f935a16
+	help
f935a16
+	  Choose this option to get a virtual graphics memory manager,
f935a16
+	  as used by Mesa's software renderer for enhanced performance.
f935a16
+	  If M is selected the module will be called vgem.
f935a16
+
f935a16
 source "drivers/gpu/drm/exynos/Kconfig"
f935a16
 
f935a16
 source "drivers/gpu/drm/vmwgfx/Kconfig"
f935a16
diff --git a/drivers/gpu/drm/Makefile b/drivers/gpu/drm/Makefile
f935a16
index 0cde1b8..021bf8a 100644
f935a16
--- a/drivers/gpu/drm/Makefile
f935a16
+++ b/drivers/gpu/drm/Makefile
f935a16
@@ -34,6 +34,7 @@ obj-$(CONFIG_DRM_SIS)   += sis/
f935a16
 obj-$(CONFIG_DRM_SAVAGE)+= savage/
f935a16
 obj-$(CONFIG_DRM_VMWGFX)+= vmwgfx/
f935a16
 obj-$(CONFIG_DRM_VIA)	+=via/
f935a16
+obj-$(CONFIG_DRM_VGEM)	+= vgem/
f935a16
 obj-$(CONFIG_DRM_NOUVEAU) +=nouveau/
f935a16
 obj-$(CONFIG_DRM_EXYNOS) +=exynos/
f935a16
 obj-$(CONFIG_DRM_GMA500) += gma500/
f935a16
diff --git a/drivers/gpu/drm/vgem/Makefile b/drivers/gpu/drm/vgem/Makefile
f935a16
new file mode 100644
f935a16
index 0000000..3f4c7b8
f935a16
--- /dev/null
f935a16
+++ b/drivers/gpu/drm/vgem/Makefile
f935a16
@@ -0,0 +1,4 @@
f935a16
+ccflags-y := -Iinclude/drm
f935a16
+vgem-y := vgem_drv.o
f935a16
+
f935a16
+obj-$(CONFIG_DRM_VGEM)	+= vgem.o
f935a16
diff --git a/drivers/gpu/drm/vgem/vgem_drv.c b/drivers/gpu/drm/vgem/vgem_drv.c
f935a16
new file mode 100644
f935a16
index 0000000..16f88ee
f935a16
--- /dev/null
f935a16
+++ b/drivers/gpu/drm/vgem/vgem_drv.c
f935a16
@@ -0,0 +1,377 @@
f935a16
+/*
f935a16
+ * Copyright 2011 Red Hat, Inc.
f935a16
+ *
f935a16
+ * Permission is hereby granted, free of charge, to any person obtaining a
f935a16
+ * copy of this software and associated documentation files (the "Software")
f935a16
+ * to deal in the software without restriction, including without limitation
f935a16
+ * on the rights to use, copy, modify, merge, publish, distribute, sub
f935a16
+ * license, and/or sell copies of the Software, and to permit persons to whom
f935a16
+ * them Software is furnished to do so, subject to the following conditions:
f935a16
+ *
f935a16
+ * The above copyright notice and this permission notice (including the next
f935a16
+ * paragraph) shall be included in all copies or substantial portions of the
f935a16
+ * Software.
f935a16
+ *
f935a16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
f935a16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTIBILITY,
f935a16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
f935a16
+ * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER
f935a16
+ * IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM, OUT OF OR IN
f935a16
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
f935a16
+ *
f935a16
+ * Authors:
f935a16
+ *	Adam Jackson <ajax@redhat.com>
f935a16
+ */
f935a16
+
f935a16
+/**
f935a16
+ * This is vgem, a (non-hardware-backed) GEM service.  This is used by Mesa's
f935a16
+ * software renderer and the X server for efficient buffer sharing.
f935a16
+ */
f935a16
+
f935a16
+#include "drmP.h"
f935a16
+#include "drm.h"
f935a16
+#include "vgem_drm.h"
f935a16
+#include <linux/module.h>
f935a16
+#include <linux/ramfs.h>
f935a16
+#include <linux/shmem_fs.h>
f935a16
+
f935a16
+#define DRIVER_NAME	"vgem"
f935a16
+#define DRIVER_DESC	"Virtual GEM provider"
f935a16
+#define DRIVER_DATE	"20120112"
f935a16
+#define DRIVER_MAJOR	1
f935a16
+#define DRIVER_MINOR	0
f935a16
+
f935a16
+#define to_vgem_bo(x) container_of(x, struct drm_vgem_gem_object, base)
f935a16
+
f935a16
+struct drm_vgem_gem_object {
f935a16
+	struct drm_gem_object base;
f935a16
+	struct page **pages;
f935a16
+};
f935a16
+
f935a16
+static int vgem_load(struct drm_device *dev, unsigned long flags)
f935a16
+{
f935a16
+	return 0;
f935a16
+}
f935a16
+
f935a16
+static int vgem_unload(struct drm_device *dev)
f935a16
+{
f935a16
+	return 0;
f935a16
+}
f935a16
+
f935a16
+static void vgem_preclose(struct drm_device *dev, struct drm_file *file)
f935a16
+{
f935a16
+}
f935a16
+
f935a16
+static void vgem_lastclose(struct drm_device *dev)
f935a16
+{
f935a16
+}
f935a16
+
f935a16
+static int vgem_gem_init_object(struct drm_gem_object *obj)
f935a16
+{
f935a16
+	return 0;
f935a16
+}
f935a16
+
f935a16
+static void vgem_gem_put_pages(struct drm_vgem_gem_object *obj)
f935a16
+{
f935a16
+	int num_pages = obj->base.size / PAGE_SIZE;
f935a16
+	int i;
f935a16
+
f935a16
+	for (i = 0; i < num_pages; i++) {
f935a16
+		page_cache_release(obj->pages[i]);
f935a16
+	}
f935a16
+
f935a16
+	drm_free_large(obj->pages);
f935a16
+	obj->pages = NULL;
f935a16
+}
f935a16
+
f935a16
+static void vgem_gem_free_object(struct drm_gem_object *obj)
f935a16
+{
f935a16
+	struct drm_vgem_gem_object *vgem_obj = to_vgem_bo(obj);
f935a16
+
f935a16
+	if (obj)
f935a16
+		drm_gem_free_mmap_offset(obj);
f935a16
+
f935a16
+	drm_gem_object_release(obj);
f935a16
+
f935a16
+	if (vgem_obj->pages)
f935a16
+		vgem_gem_put_pages(vgem_obj);
f935a16
+
f935a16
+	kfree(vgem_obj);
f935a16
+}
f935a16
+
f935a16
+static int vgem_gem_get_pages(struct drm_vgem_gem_object *obj)
f935a16
+{
f935a16
+	struct address_space *mapping;
f935a16
+	gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
f935a16
+	int num_pages, i, ret = 0;
f935a16
+
f935a16
+	num_pages = obj->base.size / PAGE_SIZE;
f935a16
+
f935a16
+	if (!obj->pages) {
f935a16
+		obj->pages = drm_malloc_ab(num_pages, sizeof(struct page *));
f935a16
+		if (obj->pages == NULL)
f935a16
+			return -ENOMEM;
f935a16
+	}
f935a16
+
f935a16
+	mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
f935a16
+	gfpmask |= mapping_gfp_mask(mapping);
f935a16
+
f935a16
+	if (WARN_ON(mapping == NULL))
f935a16
+		return VM_FAULT_SIGBUS;
f935a16
+
f935a16
+	for (i = 0; i < num_pages; i++) {
f935a16
+		struct page *page;
f935a16
+		page = shmem_read_mapping_page_gfp(mapping, i, gfpmask);
f935a16
+		if (IS_ERR(page)) {
f935a16
+			ret = PTR_ERR(page);
f935a16
+			goto err_out;
f935a16
+		}
f935a16
+		obj->pages[i] = page;
f935a16
+	}
f935a16
+
f935a16
+	return ret;
f935a16
+
f935a16
+err_out:
f935a16
+	while (i--)
f935a16
+		page_cache_release(obj->pages[i]);
f935a16
+	drm_free_large(obj->pages);
f935a16
+	obj->pages = NULL;
f935a16
+	return ret;
f935a16
+}
f935a16
+
f935a16
+static int vgem_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
f935a16
+{
f935a16
+	struct drm_vgem_gem_object *obj = to_vgem_bo(vma->vm_private_data);
f935a16
+	loff_t num_pages;
f935a16
+	pgoff_t page_offset;
f935a16
+	int ret;
f935a16
+
f935a16
+	/* We don't use vmf->pgoff since that has the fake offset */
f935a16
+	page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
f935a16
+		PAGE_SHIFT;
f935a16
+
f935a16
+	num_pages = obj->base.size / PAGE_SIZE;
f935a16
+
f935a16
+	if (WARN_ON(page_offset > num_pages))
f935a16
+		return VM_FAULT_SIGBUS;
f935a16
+
f935a16
+	ret = vgem_gem_get_pages(obj);
f935a16
+	if (ret)
f935a16
+		return ret;
f935a16
+
f935a16
+	ret = vm_insert_page(vma, (unsigned long)vmf->virtual_address,
f935a16
+			     obj->pages[page_offset]);
f935a16
+
f935a16
+	/* Pretty dumb handler for now */
f935a16
+	switch (ret) {
f935a16
+	case 0:
f935a16
+	case -ERESTARTSYS:
f935a16
+	case -EINTR:
f935a16
+		return VM_FAULT_NOPAGE;
f935a16
+	default:
f935a16
+		return VM_FAULT_SIGBUS;
f935a16
+	}
f935a16
+}
f935a16
+
f935a16
+static const struct vm_operations_struct vgem_gem_vm_ops = {
f935a16
+	.fault = vgem_gem_fault,
f935a16
+	.open = drm_gem_vm_open,
f935a16
+	.close = drm_gem_vm_close,
f935a16
+};
f935a16
+
f935a16
+/* ioctls */
f935a16
+
f935a16
+static struct drm_gem_object *vgem_gem_create(struct drm_device *dev,
f935a16
+					      struct drm_file *file,
f935a16
+					      unsigned int *handle,
f935a16
+					      unsigned long size)
f935a16
+{
f935a16
+	struct drm_vgem_gem_object *obj;
f935a16
+	struct drm_gem_object *gem_object;
f935a16
+	int err;
f935a16
+
f935a16
+	size = roundup(size, PAGE_SIZE);
f935a16
+
f935a16
+	obj = kzalloc(sizeof(*obj), GFP_KERNEL);
f935a16
+	if (!obj)
f935a16
+		return ERR_PTR(-ENOMEM);
f935a16
+
f935a16
+	gem_object = &obj->base;
f935a16
+
f935a16
+	if ((err = drm_gem_object_init(dev, gem_object, size)))
f935a16
+		goto out;
f935a16
+
f935a16
+	if ((err = drm_gem_create_mmap_offset(gem_object)))
f935a16
+		goto mmap_out;
f935a16
+
f935a16
+	if ((err = drm_gem_handle_create(file, gem_object, handle)))
f935a16
+		goto handle_out;
f935a16
+
f935a16
+	drm_gem_object_unreference_unlocked(gem_object);
f935a16
+
f935a16
+	return gem_object;
f935a16
+
f935a16
+handle_out:
f935a16
+	drm_gem_free_mmap_offset(gem_object);
f935a16
+
f935a16
+mmap_out:
f935a16
+	drm_gem_object_release(gem_object);
f935a16
+
f935a16
+out:
f935a16
+	kfree(gem_object);
f935a16
+
f935a16
+	return ERR_PTR(err);
f935a16
+}
f935a16
+
f935a16
+static int vgem_gem_create_ioctl(struct drm_device *dev, void *data,
f935a16
+				 struct drm_file *file)
f935a16
+{
f935a16
+	struct vgem_gem_create *args = data;
f935a16
+	struct drm_gem_object *gem_object;
f935a16
+
f935a16
+	gem_object = vgem_gem_create(dev, file, &args->handle, args->size);
f935a16
+
f935a16
+	if (IS_ERR(gem_object))
f935a16
+		return PTR_ERR(gem_object);
f935a16
+
f935a16
+	return 0;
f935a16
+}
f935a16
+
f935a16
+static int vgem_gem_mmap_ioctl(struct drm_device *dev, void *data,
f935a16
+			       struct drm_file *file)
f935a16
+{
f935a16
+	struct vgem_gem_mmap *args = data;
f935a16
+	struct drm_gem_object *obj;
f935a16
+
f935a16
+	obj = drm_gem_object_lookup(dev, file, args->handle);
f935a16
+	if (!obj)
f935a16
+		return -ENOENT;
f935a16
+
f935a16
+	obj->filp->private_data = obj;
f935a16
+
f935a16
+	BUG_ON(!obj->map_list.map);
f935a16
+
f935a16
+	args->mapped = (uint64_t)obj->map_list.hash.key << PAGE_SHIFT;
f935a16
+
f935a16
+	drm_gem_object_unreference_unlocked(obj);
f935a16
+
f935a16
+	return 0;
f935a16
+}
f935a16
+
f935a16
+static int vgem_gem_getparam_ioctl(struct drm_device *dev, void *data,
f935a16
+				 struct drm_file *file)
f935a16
+{
f935a16
+	struct vgem_gem_getparam *args = data;
f935a16
+	int value=0, ret;
f935a16
+
f935a16
+	switch (args->param) {
f935a16
+	case VGEM_PARAM_IS_VGEM:
f935a16
+		value = 1;
f935a16
+	}
f935a16
+
f935a16
+	ret = copy_to_user(args->value, &value, sizeof(int));
f935a16
+	if (ret)
f935a16
+		return ret;
f935a16
+
f935a16
+	return 0;
f935a16
+}
f935a16
+
f935a16
+
f935a16
+static struct drm_ioctl_desc vgem_ioctls[] = {
f935a16
+	DRM_IOCTL_DEF_DRV(VGEM_GEM_CREATE, vgem_gem_create_ioctl,
f935a16
+			  DRM_UNLOCKED | DRM_AUTH),
f935a16
+	DRM_IOCTL_DEF_DRV(VGEM_GEM_MMAP, vgem_gem_mmap_ioctl,
f935a16
+			  DRM_UNLOCKED | DRM_AUTH),
f935a16
+	DRM_IOCTL_DEF_DRV(VGEM_GEM_GETPARAM, vgem_gem_getparam_ioctl,
f935a16
+			  DRM_UNLOCKED),
f935a16
+};
f935a16
+
f935a16
+static const struct file_operations vgem_driver_fops = {
f935a16
+	.owner		= THIS_MODULE,
f935a16
+	.open		= drm_open,
f935a16
+	.mmap		= drm_gem_mmap,
f935a16
+	.poll		= drm_poll,
f935a16
+	.read		= drm_read,
f935a16
+	.unlocked_ioctl = drm_ioctl,
f935a16
+	.release	= drm_release,
f935a16
+};
f935a16
+
f935a16
+static struct drm_driver vgem_driver = {
f935a16
+	.driver_features	= DRIVER_BUS_PLATFORM | DRIVER_GEM,
f935a16
+	.load			= vgem_load,
f935a16
+	.unload			= vgem_unload,
f935a16
+	.preclose		= vgem_preclose,
f935a16
+	.lastclose		= vgem_lastclose,
f935a16
+	.gem_init_object	= vgem_gem_init_object,
f935a16
+	.gem_free_object	= vgem_gem_free_object,
f935a16
+	.gem_vm_ops		= &vgem_gem_vm_ops,
f935a16
+	.ioctls			= vgem_ioctls,
f935a16
+	.fops			= &vgem_driver_fops,
f935a16
+	.name	= DRIVER_NAME,
f935a16
+	.desc	= DRIVER_DESC,
f935a16
+	.date	= DRIVER_DATE,
f935a16
+	.major	= DRIVER_MAJOR,
f935a16
+	.minor	= DRIVER_MINOR,
f935a16
+};
f935a16
+
f935a16
+static int vgem_platform_probe(struct platform_device *pdev)
f935a16
+{
f935a16
+	vgem_driver.num_ioctls = DRM_ARRAY_SIZE(vgem_ioctls);
f935a16
+
f935a16
+	return drm_platform_init(&vgem_driver, pdev);
f935a16
+}
f935a16
+
f935a16
+static int vgem_platform_remove(struct platform_device *pdev)
f935a16
+{
f935a16
+	drm_platform_exit(&vgem_driver, pdev);
f935a16
+
f935a16
+	return 0;
f935a16
+}
f935a16
+
f935a16
+static struct platform_driver vgem_platform_driver = {
f935a16
+	.probe		= vgem_platform_probe,
f935a16
+	.remove		= __devexit_p(vgem_platform_remove),
f935a16
+	.driver		= {
f935a16
+		.owner	= THIS_MODULE,
f935a16
+		.name	= DRIVER_NAME,
f935a16
+	},
f935a16
+};
f935a16
+
f935a16
+static struct platform_device *vgem_device;
f935a16
+
f935a16
+static int __init vgem_init(void)
f935a16
+{
f935a16
+	int ret;
f935a16
+
f935a16
+	if ((ret = platform_driver_register(&vgem_platform_driver)))
f935a16
+		return ret;
f935a16
+
f935a16
+	vgem_device = platform_device_alloc("vgem", -1);
f935a16
+	if (!vgem_device) {
f935a16
+		ret = -ENOMEM;
f935a16
+		goto out;
f935a16
+	}
f935a16
+
f935a16
+	ret = platform_device_add(vgem_device);
f935a16
+	if (!ret)
f935a16
+		return 0;
f935a16
+
f935a16
+out:
f935a16
+	platform_device_put(vgem_device);
f935a16
+	platform_driver_unregister(&vgem_platform_driver);
f935a16
+
f935a16
+	return ret;
f935a16
+}
f935a16
+
f935a16
+static void __exit vgem_exit(void)
f935a16
+{
f935a16
+	platform_device_unregister(vgem_device);
f935a16
+	platform_driver_unregister(&vgem_platform_driver);
f935a16
+}
f935a16
+
f935a16
+module_init(vgem_init);
f935a16
+module_exit(vgem_exit);
f935a16
+
f935a16
+MODULE_AUTHOR("Red Hat, Inc.");
f935a16
+MODULE_DESCRIPTION(DRIVER_DESC);
f935a16
+MODULE_LICENSE("GPL and additional rights");
f935a16
diff --git a/include/drm/vgem_drm.h b/include/drm/vgem_drm.h
f935a16
new file mode 100644
f935a16
index 0000000..df83503
f935a16
--- /dev/null
f935a16
+++ b/include/drm/vgem_drm.h
f935a16
@@ -0,0 +1,62 @@
f935a16
+/*
f935a16
+ * Copyright 2011 Red Hat, Inc.
f935a16
+ *
f935a16
+ * Permission is hereby granted, free of charge, to any person obtaining a
f935a16
+ * copy of this software and associated documentation files (the "Software")
f935a16
+ * to deal in the software without restriction, including without limitation
f935a16
+ * on the rights to use, copy, modify, merge, publish, distribute, sub
f935a16
+ * license, and/or sell copies of the Software, and to permit persons to whom
f935a16
+ * them Software is furnished to do so, subject to the following conditions:
f935a16
+ *
f935a16
+ * The above copyright notice and this permission notice (including the next
f935a16
+ * paragraph) shall be included in all copies or substantial portions of the
f935a16
+ * Software.
f935a16
+ *
f935a16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
f935a16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTIBILITY,
f935a16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
f935a16
+ * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER
f935a16
+ * IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM, OUT OF OR IN
f935a16
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
f935a16
+ */
f935a16
+
f935a16
+#ifndef VGEM_DRM_H
f935a16
+#define VGEM_DRM_H
f935a16
+
f935a16
+/* Bare API largely ripped off from exynos driver */
f935a16
+
f935a16
+struct vgem_gem_create {
f935a16
+	unsigned int size;
f935a16
+	unsigned int flags;
f935a16
+	unsigned int handle;
f935a16
+};
f935a16
+
f935a16
+struct vgem_gem_mmap {
f935a16
+	unsigned int handle;
f935a16
+	unsigned int size;
f935a16
+	uint64_t mapped;
f935a16
+};
f935a16
+
f935a16
+struct vgem_gem_getparam {
f935a16
+#define VGEM_PARAM_IS_VGEM 1
f935a16
+	unsigned int param;
f935a16
+	unsigned int *value;
f935a16
+};
f935a16
+
f935a16
+#define DRM_VGEM_GEM_CREATE	0x00
f935a16
+#define DRM_VGEM_GEM_MMAP	0x01
f935a16
+#define DRM_VGEM_GEM_GETPARAM	0x02
f935a16
+
f935a16
+#define DRM_IOCTL_VGEM_GEM_CREATE \
f935a16
+		DRM_IOWR(DRM_COMMAND_BASE + DRM_VGEM_GEM_CREATE, \
f935a16
+			 struct vgem_gem_create)
f935a16
+
f935a16
+#define DRM_IOCTL_VGEM_GEM_MMAP \
f935a16
+		DRM_IOWR(DRM_COMMAND_BASE + DRM_VGEM_GEM_MMAP, \
f935a16
+			 struct vgem_gem_mmap)
f935a16
+
f935a16
+#define DRM_IOCTL_VGEM_GEM_GETPARAM \
f935a16
+		DRM_IOWR(DRM_COMMAND_BASE + DRM_VGEM_GEM_GETPARAM, \
f935a16
+			 struct vgem_gem_getparam)
f935a16
+
f935a16
+#endif