sw_sync.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * drivers/base/sw_sync.c
  3. *
  4. * Copyright (C) 2012 Google, Inc.
  5. *
  6. * This software is licensed under the terms of the GNU General Public
  7. * License version 2, as published by the Free Software Foundation, and
  8. * may be copied, distributed, and modified under those terms.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/export.h>
  18. #include <linux/file.h>
  19. #include <linux/fs.h>
  20. #include <linux/miscdevice.h>
  21. #include <linux/module.h>
  22. #include <linux/sw_sync.h>
  23. #include <linux/syscalls.h>
  24. #include <linux/uaccess.h>
  25. static int sw_sync_cmp(u32 a, u32 b)
  26. {
  27. if (a == b)
  28. return 0;
  29. return ((s32)a - (s32)b) < 0 ? -1 : 1;
  30. }
  31. struct sync_pt *sw_sync_pt_create(struct sw_sync_timeline *obj, u32 value)
  32. {
  33. struct sw_sync_pt *pt;
  34. pt = (struct sw_sync_pt *)
  35. sync_pt_create(&obj->obj, sizeof(struct sw_sync_pt));
  36. pt->value = value;
  37. return (struct sync_pt *)pt;
  38. }
  39. EXPORT_SYMBOL(sw_sync_pt_create);
  40. static struct sync_pt *sw_sync_pt_dup(struct sync_pt *sync_pt)
  41. {
  42. struct sw_sync_pt *pt = (struct sw_sync_pt *) sync_pt;
  43. struct sw_sync_timeline *obj =
  44. (struct sw_sync_timeline *)sync_pt->parent;
  45. return (struct sync_pt *) sw_sync_pt_create(obj, pt->value);
  46. }
  47. static int sw_sync_pt_has_signaled(struct sync_pt *sync_pt)
  48. {
  49. struct sw_sync_pt *pt = (struct sw_sync_pt *)sync_pt;
  50. struct sw_sync_timeline *obj =
  51. (struct sw_sync_timeline *)sync_pt->parent;
  52. return sw_sync_cmp(obj->value, pt->value) >= 0;
  53. }
  54. static int sw_sync_pt_compare(struct sync_pt *a, struct sync_pt *b)
  55. {
  56. struct sw_sync_pt *pt_a = (struct sw_sync_pt *)a;
  57. struct sw_sync_pt *pt_b = (struct sw_sync_pt *)b;
  58. return sw_sync_cmp(pt_a->value, pt_b->value);
  59. }
  60. static int sw_sync_fill_driver_data(struct sync_pt *sync_pt,
  61. void *data, int size)
  62. {
  63. struct sw_sync_pt *pt = (struct sw_sync_pt *)sync_pt;
  64. if (size < sizeof(pt->value))
  65. return -ENOMEM;
  66. memcpy(data, &pt->value, sizeof(pt->value));
  67. return sizeof(pt->value);
  68. }
  69. static void sw_sync_timeline_value_str(struct sync_timeline *sync_timeline,
  70. char *str, int size)
  71. {
  72. struct sw_sync_timeline *timeline =
  73. (struct sw_sync_timeline *)sync_timeline;
  74. snprintf(str, size, "%d", timeline->value);
  75. }
  76. static void sw_sync_pt_value_str(struct sync_pt *sync_pt,
  77. char *str, int size)
  78. {
  79. struct sw_sync_pt *pt = (struct sw_sync_pt *)sync_pt;
  80. snprintf(str, size, "%d", pt->value);
  81. }
  82. struct sync_timeline_ops sw_sync_timeline_ops = {
  83. .driver_name = "sw_sync",
  84. .dup = sw_sync_pt_dup,
  85. .has_signaled = sw_sync_pt_has_signaled,
  86. .compare = sw_sync_pt_compare,
  87. .fill_driver_data = sw_sync_fill_driver_data,
  88. .timeline_value_str = sw_sync_timeline_value_str,
  89. .pt_value_str = sw_sync_pt_value_str,
  90. };
  91. struct sw_sync_timeline *sw_sync_timeline_create(const char *name)
  92. {
  93. struct sw_sync_timeline *obj = (struct sw_sync_timeline *)
  94. sync_timeline_create(&sw_sync_timeline_ops,
  95. sizeof(struct sw_sync_timeline),
  96. name);
  97. return obj;
  98. }
  99. EXPORT_SYMBOL(sw_sync_timeline_create);
  100. void sw_sync_timeline_inc(struct sw_sync_timeline *obj, u32 inc)
  101. {
  102. obj->value += inc;
  103. sync_timeline_signal(&obj->obj);
  104. }
  105. EXPORT_SYMBOL(sw_sync_timeline_inc);
  106. #ifdef CONFIG_SW_SYNC_USER
  107. /* *WARNING*
  108. *
  109. * improper use of this can result in deadlocking kernel drivers from userspace.
  110. */
  111. /* opening sw_sync create a new sync obj */
  112. int sw_sync_open(struct inode *inode, struct file *file)
  113. {
  114. struct sw_sync_timeline *obj;
  115. char task_comm[TASK_COMM_LEN];
  116. get_task_comm(task_comm, current);
  117. obj = sw_sync_timeline_create(task_comm);
  118. if (obj == NULL)
  119. return -ENOMEM;
  120. file->private_data = obj;
  121. return 0;
  122. }
  123. int sw_sync_release(struct inode *inode, struct file *file)
  124. {
  125. struct sw_sync_timeline *obj = file->private_data;
  126. sync_timeline_destroy(&obj->obj);
  127. return 0;
  128. }
  129. long sw_sync_ioctl_create_fence(struct sw_sync_timeline *obj, unsigned long arg)
  130. {
  131. int fd = get_unused_fd();
  132. int err;
  133. struct sync_pt *pt;
  134. struct sync_fence *fence;
  135. struct sw_sync_create_fence_data data;
  136. if (fd < 0)
  137. return fd;
  138. if (copy_from_user(&data, (void __user *)arg, sizeof(data))) {
  139. err = -EFAULT;
  140. goto err;
  141. }
  142. pt = sw_sync_pt_create(obj, data.value);
  143. if (pt == NULL) {
  144. err = -ENOMEM;
  145. goto err;
  146. }
  147. data.name[sizeof(data.name) - 1] = '\0';
  148. fence = sync_fence_create(data.name, pt);
  149. if (fence == NULL) {
  150. sync_pt_free(pt);
  151. err = -ENOMEM;
  152. goto err;
  153. }
  154. data.fence = fd;
  155. if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
  156. sync_fence_put(fence);
  157. err = -EFAULT;
  158. goto err;
  159. }
  160. sync_fence_install(fence, fd);
  161. return 0;
  162. err:
  163. put_unused_fd(fd);
  164. return err;
  165. }
  166. long sw_sync_ioctl_inc(struct sw_sync_timeline *obj, unsigned long arg)
  167. {
  168. u32 value;
  169. if (copy_from_user(&value, (void __user *)arg, sizeof(value)))
  170. return -EFAULT;
  171. sw_sync_timeline_inc(obj, value);
  172. return 0;
  173. }
  174. long sw_sync_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  175. {
  176. struct sw_sync_timeline *obj = file->private_data;
  177. switch (cmd) {
  178. case SW_SYNC_IOC_CREATE_FENCE:
  179. return sw_sync_ioctl_create_fence(obj, arg);
  180. case SW_SYNC_IOC_INC:
  181. return sw_sync_ioctl_inc(obj, arg);
  182. default:
  183. return -ENOTTY;
  184. }
  185. }
  186. static const struct file_operations sw_sync_fops = {
  187. .owner = THIS_MODULE,
  188. .open = sw_sync_open,
  189. .release = sw_sync_release,
  190. .unlocked_ioctl = sw_sync_ioctl,
  191. };
  192. static struct miscdevice sw_sync_dev = {
  193. .minor = MISC_DYNAMIC_MINOR,
  194. .name = "sw_sync",
  195. .fops = &sw_sync_fops,
  196. };
  197. int __init sw_sync_device_init(void)
  198. {
  199. return misc_register(&sw_sync_dev);
  200. }
  201. void __exit sw_sync_device_remove(void)
  202. {
  203. misc_deregister(&sw_sync_dev);
  204. }
  205. module_init(sw_sync_device_init);
  206. module_exit(sw_sync_device_remove);
  207. #endif /* CONFIG_SW_SYNC_USER */