vgem_drv.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Copyright 2011 Red Hat, Inc.
  3. * Copyright © 2014 The Chromium OS Authors
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software")
  7. * to deal in the software without restriction, including without limitation
  8. * on the rights to use, copy, modify, merge, publish, distribute, sub
  9. * license, and/or sell copies of the Software, and to permit persons to whom
  10. * them Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the next
  13. * paragraph) shall be included in all copies or substantial portions of the
  14. * Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTIBILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER
  20. * IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Adam Jackson <ajax@redhat.com>
  25. * Ben Widawsky <ben@bwidawsk.net>
  26. */
  27. /**
  28. * This is vgem, a (non-hardware-backed) GEM service. This is used by Mesa's
  29. * software renderer and the X server for efficient buffer sharing.
  30. */
  31. #include <linux/module.h>
  32. #include <linux/ramfs.h>
  33. #include <linux/shmem_fs.h>
  34. #include <linux/dma-buf.h>
  35. #include "vgem_drv.h"
  36. #define DRIVER_NAME "vgem"
  37. #define DRIVER_DESC "Virtual GEM provider"
  38. #define DRIVER_DATE "20120112"
  39. #define DRIVER_MAJOR 1
  40. #define DRIVER_MINOR 0
  41. void vgem_gem_put_pages(struct drm_vgem_gem_object *obj)
  42. {
  43. drm_gem_put_pages(&obj->base, obj->pages, false, false);
  44. obj->pages = NULL;
  45. }
  46. static void vgem_gem_free_object(struct drm_gem_object *obj)
  47. {
  48. struct drm_vgem_gem_object *vgem_obj = to_vgem_bo(obj);
  49. drm_gem_free_mmap_offset(obj);
  50. if (vgem_obj->use_dma_buf && obj->dma_buf) {
  51. dma_buf_put(obj->dma_buf);
  52. obj->dma_buf = NULL;
  53. }
  54. drm_gem_object_release(obj);
  55. if (vgem_obj->pages)
  56. vgem_gem_put_pages(vgem_obj);
  57. vgem_obj->pages = NULL;
  58. kfree(vgem_obj);
  59. }
  60. int vgem_gem_get_pages(struct drm_vgem_gem_object *obj)
  61. {
  62. struct page **pages;
  63. if (obj->pages || obj->use_dma_buf)
  64. return 0;
  65. pages = drm_gem_get_pages(&obj->base);
  66. if (IS_ERR(pages)) {
  67. return PTR_ERR(pages);
  68. }
  69. obj->pages = pages;
  70. return 0;
  71. }
  72. static int vgem_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  73. {
  74. struct drm_vgem_gem_object *obj = vma->vm_private_data;
  75. struct drm_device *dev = obj->base.dev;
  76. loff_t num_pages;
  77. pgoff_t page_offset;
  78. int ret;
  79. /* We don't use vmf->pgoff since that has the fake offset */
  80. page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
  81. PAGE_SHIFT;
  82. num_pages = DIV_ROUND_UP(obj->base.size, PAGE_SIZE);
  83. if (page_offset > num_pages)
  84. return VM_FAULT_SIGBUS;
  85. mutex_lock(&dev->struct_mutex);
  86. ret = vm_insert_page(vma, (unsigned long)vmf->virtual_address,
  87. obj->pages[page_offset]);
  88. mutex_unlock(&dev->struct_mutex);
  89. switch (ret) {
  90. case 0:
  91. return VM_FAULT_NOPAGE;
  92. case -ENOMEM:
  93. return VM_FAULT_OOM;
  94. case -EBUSY:
  95. return VM_FAULT_RETRY;
  96. case -EFAULT:
  97. case -EINVAL:
  98. return VM_FAULT_SIGBUS;
  99. default:
  100. WARN_ON(1);
  101. return VM_FAULT_SIGBUS;
  102. }
  103. }
  104. static const struct vm_operations_struct vgem_gem_vm_ops = {
  105. .fault = vgem_gem_fault,
  106. .open = drm_gem_vm_open,
  107. .close = drm_gem_vm_close,
  108. };
  109. /* ioctls */
  110. static struct drm_gem_object *vgem_gem_create(struct drm_device *dev,
  111. struct drm_file *file,
  112. unsigned int *handle,
  113. unsigned long size)
  114. {
  115. struct drm_vgem_gem_object *obj;
  116. struct drm_gem_object *gem_object;
  117. int err;
  118. size = roundup(size, PAGE_SIZE);
  119. obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  120. if (!obj)
  121. return ERR_PTR(-ENOMEM);
  122. gem_object = &obj->base;
  123. err = drm_gem_object_init(dev, gem_object, size);
  124. if (err)
  125. goto out;
  126. err = drm_gem_handle_create(file, gem_object, handle);
  127. if (err)
  128. goto handle_out;
  129. drm_gem_object_unreference_unlocked(gem_object);
  130. return gem_object;
  131. handle_out:
  132. drm_gem_object_release(gem_object);
  133. out:
  134. kfree(obj);
  135. return ERR_PTR(err);
  136. }
  137. static int vgem_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
  138. struct drm_mode_create_dumb *args)
  139. {
  140. struct drm_gem_object *gem_object;
  141. uint64_t size;
  142. uint64_t pitch = args->width * DIV_ROUND_UP(args->bpp, 8);
  143. size = args->height * pitch;
  144. if (size == 0)
  145. return -EINVAL;
  146. gem_object = vgem_gem_create(dev, file, &args->handle, size);
  147. if (IS_ERR(gem_object)) {
  148. DRM_DEBUG_DRIVER("object creation failed\n");
  149. return PTR_ERR(gem_object);
  150. }
  151. args->size = gem_object->size;
  152. args->pitch = pitch;
  153. DRM_DEBUG_DRIVER("Created object of size %lld\n", size);
  154. return 0;
  155. }
  156. int vgem_gem_dumb_map(struct drm_file *file, struct drm_device *dev,
  157. uint32_t handle, uint64_t *offset)
  158. {
  159. int ret = 0;
  160. struct drm_gem_object *obj;
  161. mutex_lock(&dev->struct_mutex);
  162. obj = drm_gem_object_lookup(dev, file, handle);
  163. if (!obj) {
  164. ret = -ENOENT;
  165. goto unlock;
  166. }
  167. if (!drm_vma_node_has_offset(&obj->vma_node)) {
  168. ret = drm_gem_create_mmap_offset(obj);
  169. if (ret)
  170. goto unref;
  171. }
  172. BUG_ON(!obj->filp);
  173. obj->filp->private_data = obj;
  174. ret = vgem_gem_get_pages(to_vgem_bo(obj));
  175. if (ret)
  176. goto fail_get_pages;
  177. *offset = drm_vma_node_offset_addr(&obj->vma_node);
  178. goto unref;
  179. fail_get_pages:
  180. drm_gem_free_mmap_offset(obj);
  181. unref:
  182. drm_gem_object_unreference(obj);
  183. unlock:
  184. mutex_unlock(&dev->struct_mutex);
  185. return ret;
  186. }
  187. static struct drm_ioctl_desc vgem_ioctls[] = {
  188. };
  189. static const struct file_operations vgem_driver_fops = {
  190. .owner = THIS_MODULE,
  191. .open = drm_open,
  192. .mmap = drm_gem_mmap,
  193. .poll = drm_poll,
  194. .read = drm_read,
  195. .unlocked_ioctl = drm_ioctl,
  196. .release = drm_release,
  197. };
  198. static struct drm_driver vgem_driver = {
  199. .driver_features = DRIVER_GEM,
  200. .gem_free_object = vgem_gem_free_object,
  201. .gem_vm_ops = &vgem_gem_vm_ops,
  202. .ioctls = vgem_ioctls,
  203. .fops = &vgem_driver_fops,
  204. .dumb_create = vgem_gem_dumb_create,
  205. .dumb_map_offset = vgem_gem_dumb_map,
  206. .name = DRIVER_NAME,
  207. .desc = DRIVER_DESC,
  208. .date = DRIVER_DATE,
  209. .major = DRIVER_MAJOR,
  210. .minor = DRIVER_MINOR,
  211. };
  212. struct drm_device *vgem_device;
  213. static int __init vgem_init(void)
  214. {
  215. int ret;
  216. vgem_device = drm_dev_alloc(&vgem_driver, NULL);
  217. if (!vgem_device) {
  218. ret = -ENOMEM;
  219. goto out;
  220. }
  221. drm_dev_set_unique(vgem_device, "vgem");
  222. ret = drm_dev_register(vgem_device, 0);
  223. if (ret)
  224. goto out_unref;
  225. return 0;
  226. out_unref:
  227. drm_dev_unref(vgem_device);
  228. out:
  229. return ret;
  230. }
  231. static void __exit vgem_exit(void)
  232. {
  233. drm_dev_unregister(vgem_device);
  234. drm_dev_unref(vgem_device);
  235. }
  236. module_init(vgem_init);
  237. module_exit(vgem_exit);
  238. MODULE_AUTHOR("Red Hat, Inc.");
  239. MODULE_DESCRIPTION(DRIVER_DESC);
  240. MODULE_LICENSE("GPL and additional rights");