sync.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * sync / sw_sync abstraction
  3. * Copyright 2015-2016 Collabora Ltd.
  4. *
  5. * Based on the implementation from the Android Open Source Project,
  6. *
  7. * Copyright 2012 Google, Inc
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a
  10. * copy of this software and associated documentation files (the "Software"),
  11. * to deal in the Software without restriction, including without limitation
  12. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13. * and/or sell copies of the Software, and to permit persons to whom the
  14. * Software is furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  22. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  23. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  24. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  25. * OTHER DEALINGS IN THE SOFTWARE.
  26. */
  27. #include <fcntl.h>
  28. #include <malloc.h>
  29. #include <poll.h>
  30. #include <stdint.h>
  31. #include <string.h>
  32. #include <unistd.h>
  33. #include <sys/ioctl.h>
  34. #include <sys/stat.h>
  35. #include <sys/types.h>
  36. #include "sync.h"
  37. #include "sw_sync.h"
  38. #include <linux/sync_file.h>
  39. /* SW_SYNC ioctls */
  40. struct sw_sync_create_fence_data {
  41. __u32 value;
  42. char name[32];
  43. __s32 fence;
  44. };
  45. #define SW_SYNC_IOC_MAGIC 'W'
  46. #define SW_SYNC_IOC_CREATE_FENCE _IOWR(SW_SYNC_IOC_MAGIC, 0,\
  47. struct sw_sync_create_fence_data)
  48. #define SW_SYNC_IOC_INC _IOW(SW_SYNC_IOC_MAGIC, 1, __u32)
  49. int sync_wait(int fd, int timeout)
  50. {
  51. struct pollfd fds;
  52. fds.fd = fd;
  53. fds.events = POLLIN | POLLERR;
  54. return poll(&fds, 1, timeout);
  55. }
  56. int sync_merge(const char *name, int fd1, int fd2)
  57. {
  58. struct sync_merge_data data = {};
  59. int err;
  60. data.fd2 = fd2;
  61. strncpy(data.name, name, sizeof(data.name) - 1);
  62. data.name[sizeof(data.name) - 1] = '\0';
  63. err = ioctl(fd1, SYNC_IOC_MERGE, &data);
  64. if (err < 0)
  65. return err;
  66. return data.fence;
  67. }
  68. static struct sync_file_info *sync_file_info(int fd)
  69. {
  70. struct sync_file_info *info;
  71. struct sync_fence_info *fence_info;
  72. int err, num_fences;
  73. info = calloc(1, sizeof(*info));
  74. if (info == NULL)
  75. return NULL;
  76. err = ioctl(fd, SYNC_IOC_FILE_INFO, info);
  77. if (err < 0) {
  78. free(info);
  79. return NULL;
  80. }
  81. num_fences = info->num_fences;
  82. if (num_fences) {
  83. info->flags = 0;
  84. info->num_fences = num_fences;
  85. fence_info = calloc(num_fences, sizeof(*fence_info));
  86. if (!fence_info) {
  87. free(info);
  88. return NULL;
  89. }
  90. info->sync_fence_info = (uint64_t)fence_info;
  91. err = ioctl(fd, SYNC_IOC_FILE_INFO, info);
  92. if (err < 0) {
  93. free(fence_info);
  94. free(info);
  95. return NULL;
  96. }
  97. }
  98. return info;
  99. }
  100. static void sync_file_info_free(struct sync_file_info *info)
  101. {
  102. free((void *)info->sync_fence_info);
  103. free(info);
  104. }
  105. int sync_fence_size(int fd)
  106. {
  107. int count;
  108. struct sync_file_info *info = sync_file_info(fd);
  109. if (!info)
  110. return 0;
  111. count = info->num_fences;
  112. sync_file_info_free(info);
  113. return count;
  114. }
  115. int sync_fence_count_with_status(int fd, int status)
  116. {
  117. unsigned int i, count = 0;
  118. struct sync_fence_info *fence_info = NULL;
  119. struct sync_file_info *info = sync_file_info(fd);
  120. if (!info)
  121. return -1;
  122. fence_info = (struct sync_fence_info *)info->sync_fence_info;
  123. for (i = 0 ; i < info->num_fences ; i++) {
  124. if (fence_info[i].status == status)
  125. count++;
  126. }
  127. sync_file_info_free(info);
  128. return count;
  129. }
  130. int sw_sync_timeline_create(void)
  131. {
  132. return open("/sys/kernel/debug/sync/sw_sync", O_RDWR);
  133. }
  134. int sw_sync_timeline_inc(int fd, unsigned int count)
  135. {
  136. __u32 arg = count;
  137. return ioctl(fd, SW_SYNC_IOC_INC, &arg);
  138. }
  139. int sw_sync_timeline_is_valid(int fd)
  140. {
  141. int status;
  142. if (fd == -1)
  143. return 0;
  144. status = fcntl(fd, F_GETFD, 0);
  145. return (status >= 0);
  146. }
  147. void sw_sync_timeline_destroy(int fd)
  148. {
  149. if (sw_sync_timeline_is_valid(fd))
  150. close(fd);
  151. }
  152. int sw_sync_fence_create(int fd, const char *name, unsigned int value)
  153. {
  154. struct sw_sync_create_fence_data data = {};
  155. int err;
  156. data.value = value;
  157. strncpy(data.name, name, sizeof(data.name) - 1);
  158. data.name[sizeof(data.name) - 1] = '\0';
  159. err = ioctl(fd, SW_SYNC_IOC_CREATE_FENCE, &data);
  160. if (err < 0)
  161. return err;
  162. return data.fence;
  163. }
  164. int sw_sync_fence_is_valid(int fd)
  165. {
  166. /* Same code! */
  167. return sw_sync_timeline_is_valid(fd);
  168. }
  169. void sw_sync_fence_destroy(int fd)
  170. {
  171. if (sw_sync_fence_is_valid(fd))
  172. close(fd);
  173. }