vgem_drv.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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/dma-buf.h>
  32. #include <linux/module.h>
  33. #include <linux/device/faux.h>
  34. #include <linux/shmem_fs.h>
  35. #include <linux/vmalloc.h>
  36. #include <drm/drm_drv.h>
  37. #include <drm/drm_file.h>
  38. #include <drm/drm_gem_shmem_helper.h>
  39. #include <drm/drm_ioctl.h>
  40. #include <drm/drm_managed.h>
  41. #include <drm/drm_prime.h>
  42. #include "vgem_drv.h"
  43. #define DRIVER_NAME "vgem"
  44. #define DRIVER_DESC "Virtual GEM provider"
  45. #define DRIVER_MAJOR 1
  46. #define DRIVER_MINOR 0
  47. static struct vgem_device {
  48. struct drm_device drm;
  49. struct faux_device *faux_dev;
  50. } *vgem_device;
  51. static int vgem_open(struct drm_device *dev, struct drm_file *file)
  52. {
  53. struct vgem_file *vfile;
  54. int ret;
  55. vfile = kzalloc_obj(*vfile);
  56. if (!vfile)
  57. return -ENOMEM;
  58. file->driver_priv = vfile;
  59. ret = vgem_fence_open(vfile);
  60. if (ret) {
  61. kfree(vfile);
  62. return ret;
  63. }
  64. return 0;
  65. }
  66. static void vgem_postclose(struct drm_device *dev, struct drm_file *file)
  67. {
  68. struct vgem_file *vfile = file->driver_priv;
  69. vgem_fence_close(vfile);
  70. kfree(vfile);
  71. }
  72. static struct drm_ioctl_desc vgem_ioctls[] = {
  73. DRM_IOCTL_DEF_DRV(VGEM_FENCE_ATTACH, vgem_fence_attach_ioctl, DRM_RENDER_ALLOW),
  74. DRM_IOCTL_DEF_DRV(VGEM_FENCE_SIGNAL, vgem_fence_signal_ioctl, DRM_RENDER_ALLOW),
  75. };
  76. DEFINE_DRM_GEM_FOPS(vgem_driver_fops);
  77. static struct drm_gem_object *vgem_gem_create_object(struct drm_device *dev, size_t size)
  78. {
  79. struct drm_gem_shmem_object *obj;
  80. obj = kzalloc_obj(*obj);
  81. if (!obj)
  82. return ERR_PTR(-ENOMEM);
  83. /*
  84. * vgem doesn't have any begin/end cpu access ioctls, therefore must use
  85. * coherent memory or dma-buf sharing just wont work.
  86. */
  87. obj->map_wc = true;
  88. return &obj->base;
  89. }
  90. static const struct drm_driver vgem_driver = {
  91. .driver_features = DRIVER_GEM | DRIVER_RENDER,
  92. .open = vgem_open,
  93. .postclose = vgem_postclose,
  94. .ioctls = vgem_ioctls,
  95. .num_ioctls = ARRAY_SIZE(vgem_ioctls),
  96. .fops = &vgem_driver_fops,
  97. DRM_GEM_SHMEM_DRIVER_OPS,
  98. .gem_create_object = vgem_gem_create_object,
  99. .name = DRIVER_NAME,
  100. .desc = DRIVER_DESC,
  101. .major = DRIVER_MAJOR,
  102. .minor = DRIVER_MINOR,
  103. };
  104. static int __init vgem_init(void)
  105. {
  106. int ret;
  107. struct faux_device *fdev;
  108. fdev = faux_device_create("vgem", NULL, NULL);
  109. if (!fdev)
  110. return -ENODEV;
  111. if (!devres_open_group(&fdev->dev, NULL, GFP_KERNEL)) {
  112. ret = -ENOMEM;
  113. goto out_unregister;
  114. }
  115. dma_coerce_mask_and_coherent(&fdev->dev,
  116. DMA_BIT_MASK(64));
  117. vgem_device = devm_drm_dev_alloc(&fdev->dev, &vgem_driver,
  118. struct vgem_device, drm);
  119. if (IS_ERR(vgem_device)) {
  120. ret = PTR_ERR(vgem_device);
  121. goto out_devres;
  122. }
  123. vgem_device->faux_dev = fdev;
  124. /* Final step: expose the device/driver to userspace */
  125. ret = drm_dev_register(&vgem_device->drm, 0);
  126. if (ret)
  127. goto out_devres;
  128. return 0;
  129. out_devres:
  130. devres_release_group(&fdev->dev, NULL);
  131. out_unregister:
  132. faux_device_destroy(fdev);
  133. return ret;
  134. }
  135. static void __exit vgem_exit(void)
  136. {
  137. struct faux_device *fdev = vgem_device->faux_dev;
  138. drm_dev_unregister(&vgem_device->drm);
  139. devres_release_group(&fdev->dev, NULL);
  140. faux_device_destroy(fdev);
  141. }
  142. module_init(vgem_init);
  143. module_exit(vgem_exit);
  144. MODULE_AUTHOR("Red Hat, Inc.");
  145. MODULE_AUTHOR("Intel Corporation");
  146. MODULE_DESCRIPTION(DRIVER_DESC);
  147. MODULE_LICENSE("GPL and additional rights");