sw_sync.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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/file.h>
  18. #include <linux/fs.h>
  19. #include <linux/miscdevice.h>
  20. #include <linux/module.h>
  21. #include <linux/sw_sync.h>
  22. #include <linux/syscalls.h>
  23. #include <linux/uaccess.h>
  24. static int sw_sync_cmp(u32 a, u32 b)
  25. {
  26. if (a == b)
  27. return 0;
  28. return ((s32)a - (s32)b) < 0 ? -1 : 1;
  29. }
  30. struct sync_pt *sw_sync_pt_create(struct sw_sync_timeline *obj, u32 value)
  31. {
  32. struct sw_sync_pt *pt;
  33. pt = (struct sw_sync_pt *)
  34. sync_pt_create(&obj->obj, sizeof(struct sw_sync_pt));
  35. pt->value = value;
  36. return (struct sync_pt *)pt;
  37. }
  38. static struct sync_pt *sw_sync_pt_dup(struct sync_pt *sync_pt)
  39. {
  40. struct sw_sync_pt *pt = (struct sw_sync_pt *) sync_pt;
  41. struct sw_sync_timeline *obj =
  42. (struct sw_sync_timeline *)sync_pt->parent;
  43. return (struct sync_pt *) sw_sync_pt_create(obj, pt->value);
  44. }
  45. static int sw_sync_pt_has_signaled(struct sync_pt *sync_pt)
  46. {
  47. struct sw_sync_pt *pt = (struct sw_sync_pt *)sync_pt;
  48. struct sw_sync_timeline *obj =
  49. (struct sw_sync_timeline *)sync_pt->parent;
  50. return sw_sync_cmp(obj->value, pt->value) >= 0;
  51. }
  52. static int sw_sync_pt_compare(struct sync_pt *a, struct sync_pt *b)
  53. {
  54. struct sw_sync_pt *pt_a = (struct sw_sync_pt *)a;
  55. struct sw_sync_pt *pt_b = (struct sw_sync_pt *)b;
  56. return sw_sync_cmp(pt_a->value, pt_b->value);
  57. }
  58. static void sw_sync_print_obj(struct seq_file *s,
  59. struct sync_timeline *sync_timeline)
  60. {
  61. struct sw_sync_timeline *obj = (struct sw_sync_timeline *)sync_timeline;
  62. seq_printf(s, "%d", obj->value);
  63. }
  64. static void sw_sync_print_pt(struct seq_file *s, struct sync_pt *sync_pt)
  65. {
  66. struct sw_sync_pt *pt = (struct sw_sync_pt *)sync_pt;
  67. struct sw_sync_timeline *obj =
  68. (struct sw_sync_timeline *)sync_pt->parent;
  69. seq_printf(s, "%d / %d", pt->value, obj->value);
  70. }
  71. static int sw_sync_fill_driver_data(struct sync_pt *sync_pt,
  72. void *data, int size)
  73. {
  74. struct sw_sync_pt *pt = (struct sw_sync_pt *)sync_pt;
  75. if (size < sizeof(pt->value))
  76. return -ENOMEM;
  77. memcpy(data, &pt->value, sizeof(pt->value));
  78. return sizeof(pt->value);
  79. }
  80. struct sync_timeline_ops sw_sync_timeline_ops = {
  81. .driver_name = "sw_sync",
  82. .dup = sw_sync_pt_dup,
  83. .has_signaled = sw_sync_pt_has_signaled,
  84. .compare = sw_sync_pt_compare,
  85. .print_obj = sw_sync_print_obj,
  86. .print_pt = sw_sync_print_pt,
  87. .fill_driver_data = sw_sync_fill_driver_data,
  88. };
  89. struct sw_sync_timeline *sw_sync_timeline_create(const char *name)
  90. {
  91. struct sw_sync_timeline *obj = (struct sw_sync_timeline *)
  92. sync_timeline_create(&sw_sync_timeline_ops,
  93. sizeof(struct sw_sync_timeline),
  94. name);
  95. return obj;
  96. }
  97. void sw_sync_timeline_inc(struct sw_sync_timeline *obj, u32 inc)
  98. {
  99. obj->value += inc;
  100. sync_timeline_signal(&obj->obj);
  101. }
  102. #ifdef CONFIG_SW_SYNC_USER
  103. /* *WARNING*
  104. *
  105. * improper use of this can result in deadlocking kernel drivers from userspace.
  106. */
  107. /* opening sw_sync create a new sync obj */
  108. int sw_sync_open(struct inode *inode, struct file *file)
  109. {
  110. struct sw_sync_timeline *obj;
  111. char task_comm[TASK_COMM_LEN];
  112. get_task_comm(task_comm, current);
  113. obj = sw_sync_timeline_create(task_comm);
  114. if (obj == NULL)
  115. return -ENOMEM;
  116. file->private_data = obj;
  117. return 0;
  118. }
  119. int sw_sync_release(struct inode *inode, struct file *file)
  120. {
  121. struct sw_sync_timeline *obj = file->private_data;
  122. sync_timeline_destroy(&obj->obj);
  123. return 0;
  124. }
  125. long sw_sync_ioctl_create_fence(struct sw_sync_timeline *obj, unsigned long arg)
  126. {
  127. int fd = get_unused_fd();
  128. int err;
  129. struct sync_pt *pt;
  130. struct sync_fence *fence;
  131. struct sw_sync_create_fence_data data;
  132. if (copy_from_user(&data, (void __user *)arg, sizeof(data)))
  133. return -EFAULT;
  134. pt = sw_sync_pt_create(obj, data.value);
  135. if (pt == NULL) {
  136. err = -ENOMEM;
  137. goto err;
  138. }
  139. data.name[sizeof(data.name) - 1] = '\0';
  140. fence = sync_fence_create(data.name, pt);
  141. if (fence == NULL) {
  142. sync_pt_free(pt);
  143. err = -ENOMEM;
  144. goto err;
  145. }
  146. data.fence = fd;
  147. if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
  148. sync_fence_put(fence);
  149. err = -EFAULT;
  150. goto err;
  151. }
  152. sync_fence_install(fence, fd);
  153. return 0;
  154. err:
  155. put_unused_fd(fd);
  156. return err;
  157. }
  158. long sw_sync_ioctl_inc(struct sw_sync_timeline *obj, unsigned long arg)
  159. {
  160. u32 value;
  161. if (copy_from_user(&value, (void __user *)arg, sizeof(value)))
  162. return -EFAULT;
  163. sw_sync_timeline_inc(obj, value);
  164. return 0;
  165. }
  166. long sw_sync_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  167. {
  168. struct sw_sync_timeline *obj = file->private_data;
  169. switch (cmd) {
  170. case SW_SYNC_IOC_CREATE_FENCE:
  171. return sw_sync_ioctl_create_fence(obj, arg);
  172. case SW_SYNC_IOC_INC:
  173. return sw_sync_ioctl_inc(obj, arg);
  174. default:
  175. return -ENOTTY;
  176. }
  177. }
  178. static const struct file_operations sw_sync_fops = {
  179. .owner = THIS_MODULE,
  180. .open = sw_sync_open,
  181. .release = sw_sync_release,
  182. .unlocked_ioctl = sw_sync_ioctl,
  183. };
  184. static struct miscdevice sw_sync_dev = {
  185. .minor = MISC_DYNAMIC_MINOR,
  186. .name = "sw_sync",
  187. .fops = &sw_sync_fops,
  188. };
  189. int __init sw_sync_device_init(void)
  190. {
  191. return misc_register(&sw_sync_dev);
  192. }
  193. void __exit sw_sync_device_remove(void)
  194. {
  195. misc_deregister(&sw_sync_dev);
  196. }
  197. module_init(sw_sync_device_init);
  198. module_exit(sw_sync_device_remove);
  199. #endif /* CONFIG_SW_SYNC_USER */