vgem_drv.c 4.7 KB

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