mali_kbase_sync_user.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. *
  3. * (C) COPYRIGHT 2012-2015 ARM Limited. All rights reserved.
  4. *
  5. * This program is free software and is provided to you under the terms of the
  6. * GNU General Public License version 2 as published by the Free Software
  7. * Foundation, and any use by you of this program is subject to the terms
  8. * of such GNU licence.
  9. *
  10. * A copy of the licence is included with the program, and can also be obtained
  11. * from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  12. * Boston, MA 02110-1301, USA.
  13. *
  14. */
  15. /**
  16. * @file mali_kbase_sync_user.c
  17. *
  18. */
  19. #ifdef CONFIG_SYNC
  20. #include <linux/sched.h>
  21. #include <linux/fdtable.h>
  22. #include <linux/file.h>
  23. #include <linux/fs.h>
  24. #include <linux/module.h>
  25. #include <linux/anon_inodes.h>
  26. #include <linux/version.h>
  27. #include <linux/uaccess.h>
  28. #include <mali_kbase_sync.h>
  29. #include <mali_base_kernel_sync.h>
  30. static int kbase_stream_close(struct inode *inode, struct file *file)
  31. {
  32. struct sync_timeline *tl;
  33. tl = (struct sync_timeline *)file->private_data;
  34. BUG_ON(!tl);
  35. sync_timeline_destroy(tl);
  36. return 0;
  37. }
  38. static const struct file_operations stream_fops = {
  39. .owner = THIS_MODULE,
  40. .release = kbase_stream_close,
  41. };
  42. int kbase_stream_create(const char *name, int *const out_fd)
  43. {
  44. struct sync_timeline *tl;
  45. BUG_ON(!out_fd);
  46. tl = kbase_sync_timeline_alloc(name);
  47. if (!tl)
  48. return -EINVAL;
  49. *out_fd = anon_inode_getfd(name, &stream_fops, tl, O_RDONLY | O_CLOEXEC);
  50. if (*out_fd < 0) {
  51. sync_timeline_destroy(tl);
  52. return -EINVAL;
  53. }
  54. return 0;
  55. }
  56. int kbase_stream_create_fence(int tl_fd)
  57. {
  58. struct sync_timeline *tl;
  59. struct sync_pt *pt;
  60. struct sync_fence *fence;
  61. int fd;
  62. struct file *tl_file;
  63. tl_file = fget(tl_fd);
  64. if (tl_file == NULL)
  65. return -EBADF;
  66. if (tl_file->f_op != &stream_fops) {
  67. fd = -EBADF;
  68. goto out;
  69. }
  70. tl = tl_file->private_data;
  71. pt = kbase_sync_pt_alloc(tl);
  72. if (!pt) {
  73. fd = -EFAULT;
  74. goto out;
  75. }
  76. fence = sync_fence_create("mali_fence", pt);
  77. if (!fence) {
  78. sync_pt_free(pt);
  79. fd = -EFAULT;
  80. goto out;
  81. }
  82. /* from here the fence owns the sync_pt */
  83. /* create a fd representing the fence */
  84. fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
  85. if (fd < 0) {
  86. sync_fence_put(fence);
  87. goto out;
  88. }
  89. /* bind fence to the new fd */
  90. sync_fence_install(fence, fd);
  91. out:
  92. fput(tl_file);
  93. return fd;
  94. }
  95. int kbase_fence_validate(int fd)
  96. {
  97. struct sync_fence *fence;
  98. fence = sync_fence_fdget(fd);
  99. if (!fence)
  100. return -EINVAL;
  101. sync_fence_put(fence);
  102. return 0;
  103. }
  104. #endif /* CONFIG_SYNC */