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