msm_vidc_fence.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  4. */
  5. #include "msm_vidc_fence.h"
  6. #include "msm_vidc_debug.h"
  7. #include "msm_vidc_driver.h"
  8. extern struct msm_vidc_core *g_core;
  9. static const char *msm_vidc_dma_fence_get_driver_name(struct dma_fence *df)
  10. {
  11. struct msm_vidc_fence *fence;
  12. if (df) {
  13. fence = container_of(df, struct msm_vidc_fence, dma_fence);
  14. return fence->name;
  15. }
  16. return "msm_vidc_dma_fence_get_driver_name: invalid fence";
  17. }
  18. static const char *msm_vidc_dma_fence_get_timeline_name(struct dma_fence *df)
  19. {
  20. struct msm_vidc_fence *fence;
  21. if (df) {
  22. fence = container_of(df, struct msm_vidc_fence, dma_fence);
  23. return fence->name;
  24. }
  25. return "msm_vidc_dma_fence_get_timeline_name: invalid fence";
  26. }
  27. static void msm_vidc_dma_fence_release(struct dma_fence *df)
  28. {
  29. struct msm_vidc_fence *fence;
  30. if (df) {
  31. fence = container_of(df, struct msm_vidc_fence, dma_fence);
  32. d_vpr_l("%s: name %s\n", __func__, fence->name);
  33. msm_vidc_vmem_free((void **)&fence);
  34. } else {
  35. d_vpr_e("%s: invalid fence\n", __func__);
  36. }
  37. }
  38. static const struct dma_fence_ops msm_vidc_dma_fence_ops = {
  39. .get_driver_name = msm_vidc_dma_fence_get_driver_name,
  40. .get_timeline_name = msm_vidc_dma_fence_get_timeline_name,
  41. .release = msm_vidc_dma_fence_release,
  42. };
  43. struct msm_vidc_fence *msm_vidc_fence_create(struct msm_vidc_inst *inst)
  44. {
  45. struct msm_vidc_fence *fence = NULL;
  46. int rc = 0;
  47. if (!inst) {
  48. d_vpr_e("%s: invalid params\n", __func__);
  49. return NULL;
  50. }
  51. rc = msm_vidc_vmem_alloc(sizeof(*fence), (void **)&fence, __func__);
  52. if (rc)
  53. return NULL;
  54. fence->fd = INVALID_FD;
  55. dma_fence_init(&fence->dma_fence, &msm_vidc_dma_fence_ops,
  56. &inst->fence_context.lock, inst->fence_context.ctx_num,
  57. ++inst->fence_context.seq_num);
  58. snprintf(fence->name, sizeof(fence->name), "%s: %llu",
  59. inst->fence_context.name, inst->fence_context.seq_num);
  60. /* reset seqno to avoid going beyond INT_MAX */
  61. if (inst->fence_context.seq_num >= INT_MAX)
  62. inst->fence_context.seq_num = 0;
  63. INIT_LIST_HEAD(&fence->list);
  64. list_add_tail(&fence->list, &inst->fence_list);
  65. i_vpr_l(inst, "%s: created %s\n", __func__, fence->name);
  66. return fence;
  67. }
  68. int msm_vidc_create_fence_fd(struct msm_vidc_inst *inst,
  69. struct msm_vidc_fence *fence)
  70. {
  71. int rc = 0;
  72. if (!inst || !fence) {
  73. d_vpr_e("%s: invalid params\n", __func__);
  74. return -EINVAL;
  75. }
  76. fence->fd = get_unused_fd_flags(0);
  77. if (fence->fd < 0) {
  78. i_vpr_e(inst, "%s: getting fd (%d) failed\n", __func__,
  79. fence->fd);
  80. rc = -EINVAL;
  81. goto err_fd;
  82. }
  83. fence->sync_file = sync_file_create(&fence->dma_fence);
  84. if (!fence->sync_file) {
  85. i_vpr_e(inst, "%s: sync_file_create failed\n", __func__);
  86. rc = -EINVAL;
  87. goto err_sync_file;
  88. }
  89. fd_install(fence->fd, fence->sync_file->file);
  90. i_vpr_l(inst, "%s: created fd %d for fence %s\n", __func__,
  91. fence->fd, fence->name);
  92. return 0;
  93. err_sync_file:
  94. put_unused_fd(fence->fd);
  95. err_fd:
  96. return rc;
  97. }
  98. struct msm_vidc_fence *msm_vidc_get_fence_from_id(
  99. struct msm_vidc_inst *inst, u32 fence_id)
  100. {
  101. struct msm_vidc_fence *fence, *dummy_fence;
  102. bool found = false;
  103. if (!inst) {
  104. d_vpr_e("%s: invalid params\n", __func__);
  105. return NULL;
  106. }
  107. list_for_each_entry_safe(fence, dummy_fence, &inst->fence_list, list) {
  108. if (fence->dma_fence.seqno == (u64)fence_id) {
  109. found = true;
  110. break;
  111. }
  112. }
  113. if (!found)
  114. return NULL;
  115. return fence;
  116. }
  117. int msm_vidc_fence_signal(struct msm_vidc_inst *inst, u32 fence_id)
  118. {
  119. int rc = 0;
  120. struct msm_vidc_fence *fence;
  121. if (!inst) {
  122. d_vpr_e("%s: invalid params\n", __func__);
  123. return -EINVAL;
  124. }
  125. fence = msm_vidc_get_fence_from_id(inst, fence_id);
  126. if (!fence) {
  127. i_vpr_e(inst, "%s: no fence available to signal with id: %u\n",
  128. __func__, fence_id);
  129. rc = -EINVAL;
  130. goto exit;
  131. }
  132. i_vpr_l(inst, "%s: fence %s\n", __func__, fence->name);
  133. list_del_init(&fence->list);
  134. dma_fence_signal(&fence->dma_fence);
  135. dma_fence_put(&fence->dma_fence);
  136. exit:
  137. return rc;
  138. }
  139. void msm_vidc_fence_destroy(struct msm_vidc_inst *inst, u32 fence_id)
  140. {
  141. struct msm_vidc_fence *fence;
  142. if (!inst) {
  143. d_vpr_e("%s: invalid params\n", __func__);
  144. return;
  145. }
  146. fence = msm_vidc_get_fence_from_id(inst, fence_id);
  147. if (!fence) {
  148. return;
  149. }
  150. i_vpr_e(inst, "%s: fence %s\n", __func__, fence->name);
  151. list_del_init(&fence->list);
  152. dma_fence_set_error(&fence->dma_fence, -EINVAL);
  153. dma_fence_signal(&fence->dma_fence);
  154. dma_fence_put(&fence->dma_fence);
  155. }
  156. int msm_vidc_fence_init(struct msm_vidc_inst *inst)
  157. {
  158. int rc = 0;
  159. if (!inst) {
  160. d_vpr_e("%s: invalid params\n", __func__);
  161. return -EINVAL;
  162. }
  163. inst->fence_context.ctx_num = dma_fence_context_alloc(1);
  164. spin_lock_init(&inst->fence_context.lock);
  165. snprintf(inst->fence_context.name, sizeof(inst->fence_context.name),
  166. "msm_vidc_fence: %s: %llu", inst->debug_str,
  167. inst->fence_context.ctx_num);
  168. i_vpr_h(inst, "%s: %s\n", __func__, inst->fence_context.name);
  169. return rc;
  170. }
  171. void msm_vidc_fence_deinit(struct msm_vidc_inst *inst)
  172. {
  173. if (!inst) {
  174. d_vpr_e("%s: invalid params\n", __func__);
  175. return;
  176. }
  177. i_vpr_h(inst, "%s: %s\n", __func__, inst->fence_context.name);
  178. inst->fence_context.ctx_num = 0;
  179. snprintf(inst->fence_context.name, sizeof(inst->fence_context.name),
  180. "%s", "");
  181. }