msm_vidc_synx.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
  4. */
  5. #include <msm_hw_fence_synx_translation.h>
  6. #include "msm_vidc_core.h"
  7. #include "msm_vidc_internal.h"
  8. #include "msm_vidc_fence.h"
  9. #include "msm_vidc_debug.h"
  10. #include <synx_api.h>
  11. #define MSM_VIDC_SYNX_FENCE_CLIENT_ID SYNX_CLIENT_HW_FENCE_VID_CTX0
  12. #define MSM_VIDC_SYNX_CREATE_DMA_FENCE SYNX_CREATE_DMA_FENCE
  13. #define MAX_SYNX_FENCE_SESSION_NAME 64
  14. static const char *msm_vidc_synx_dma_fence_get_driver_name(struct dma_fence *df)
  15. {
  16. struct msm_vidc_fence *fence;
  17. if (df) {
  18. fence = container_of(df, struct msm_vidc_fence, dma_fence);
  19. return fence->name;
  20. }
  21. return "msm_vidc_synx_dma_fence_get_driver_name: invalid fence";
  22. }
  23. static const char *msm_vidc_synx_dma_fence_get_timeline_name(struct dma_fence *df)
  24. {
  25. struct msm_vidc_fence *fence;
  26. if (df) {
  27. fence = container_of(df, struct msm_vidc_fence, dma_fence);
  28. return fence->name;
  29. }
  30. return "msm_vidc_synx_dma_fence_get_timeline_name: invalid fence";
  31. }
  32. static void msm_vidc_synx_fence_release(struct dma_fence *df)
  33. {
  34. struct msm_vidc_fence *fence;
  35. int rc = 0;
  36. if (!df) {
  37. d_vpr_e("%s: invalid dma fence\n", __func__);
  38. return;
  39. }
  40. fence = container_of(df, struct msm_vidc_fence, dma_fence);
  41. if (!fence) {
  42. d_vpr_e("%s: invalid fence\n", __func__);
  43. return;
  44. }
  45. d_vpr_l("%s: name %s\n", __func__, fence->name);
  46. /* destroy associated synx fence */
  47. if (fence->session) {
  48. rc = synx_hwfence_release((struct synx_session *)fence->session,
  49. (u32)fence->fence_id);
  50. if (rc)
  51. d_vpr_e("%s: failed to destroy synx fence for %s\n",
  52. __func__, fence->name);
  53. }
  54. msm_vidc_vmem_free((void **)&fence);
  55. return;
  56. }
  57. static const struct dma_fence_ops msm_vidc_synx_dma_fence_ops = {
  58. .get_driver_name = msm_vidc_synx_dma_fence_get_driver_name,
  59. .get_timeline_name = msm_vidc_synx_dma_fence_get_timeline_name,
  60. .release = msm_vidc_synx_fence_release,
  61. };
  62. static struct msm_vidc_fence *msm_vidc_get_synx_fence_from_id(
  63. struct msm_vidc_inst *inst, u64 fence_id)
  64. {
  65. struct msm_vidc_fence *fence, *dummy_fence;
  66. bool found = false;
  67. if (!inst) {
  68. d_vpr_e("%s: invalid params\n", __func__);
  69. return NULL;
  70. }
  71. list_for_each_entry_safe(fence, dummy_fence, &inst->fence_list, list) {
  72. if (fence->fence_id == fence_id) {
  73. found = true;
  74. break;
  75. }
  76. }
  77. if (!found) {
  78. i_vpr_l(inst, "%s: no fence available for id: %u\n",
  79. __func__, fence_id);
  80. return NULL;
  81. }
  82. return fence;
  83. }
  84. static void msm_vidc_synx_fence_destroy(struct msm_vidc_inst *inst, u64 fence_id)
  85. {
  86. struct msm_vidc_fence *fence;
  87. if (!inst || !inst->core) {
  88. d_vpr_e("%s: invalid params\n", __func__);
  89. return;
  90. }
  91. fence = msm_vidc_get_synx_fence_from_id(inst, fence_id);
  92. if (!fence) {
  93. return;
  94. }
  95. i_vpr_e(inst, "%s: fence %s\n", __func__, fence->name);
  96. list_del_init(&fence->list);
  97. dma_fence_set_error(&fence->dma_fence, -EINVAL);
  98. dma_fence_signal(&fence->dma_fence);
  99. dma_fence_put(&fence->dma_fence);
  100. }
  101. static int msm_vidc_synx_fence_register(struct msm_vidc_core *core)
  102. {
  103. struct synx_initialization_params params;
  104. struct synx_session *session = NULL;
  105. char synx_session_name[MAX_SYNX_FENCE_SESSION_NAME];
  106. struct synx_queue_desc queue_desc;
  107. if (!core && !core->capabilities) {
  108. d_vpr_e("%s: invalid params\n", __func__);
  109. return -EINVAL;
  110. }
  111. if (!core->capabilities[SUPPORTS_SYNX_FENCE].value)
  112. return 0;
  113. /* fill synx_initialization_params */
  114. memset(&params, 0, sizeof(struct synx_initialization_params));
  115. memset(&queue_desc, 0, sizeof(struct synx_queue_desc));
  116. params.id = (enum synx_client_id)MSM_VIDC_SYNX_FENCE_CLIENT_ID;
  117. snprintf(synx_session_name, MAX_SYNX_FENCE_SESSION_NAME,
  118. "video synx fence");
  119. params.name = synx_session_name;
  120. params.ptr = &queue_desc;
  121. params.flags = SYNX_INIT_MAX; /* unused */
  122. session =
  123. (struct synx_session *)synx_hwfence_initialize(&params);
  124. if (IS_ERR_OR_NULL(session)) {
  125. d_vpr_e("%s: invalid synx fence session\n", __func__);
  126. return -EINVAL;
  127. }
  128. /* fill core synx fence data */
  129. core->synx_fence_data.client_id = (u32)params.id;
  130. core->synx_fence_data.client_flags = (u32)params.flags;
  131. core->synx_fence_data.session = (void *)session;
  132. core->synx_fence_data.queue.size = (u32)queue_desc.size;
  133. core->synx_fence_data.queue.kvaddr = queue_desc.vaddr;
  134. core->synx_fence_data.queue.phys_addr = (phys_addr_t)queue_desc.dev_addr;
  135. core->synx_fence_data.queue.type = MSM_VIDC_BUF_INTERFACE_QUEUE;
  136. core->synx_fence_data.queue.region = MSM_VIDC_NON_SECURE;
  137. core->synx_fence_data.queue.direction = DMA_BIDIRECTIONAL;
  138. d_vpr_h("%s: successfully registered synx fence\n", __func__);
  139. return 0;
  140. }
  141. static int msm_vidc_synx_fence_deregister(struct msm_vidc_core *core)
  142. {
  143. int rc = 0;
  144. if (!core || !core->capabilities) {
  145. d_vpr_e("%s: invalid params\n", __func__);
  146. return -EINVAL;
  147. }
  148. if (!core->capabilities[SUPPORTS_SYNX_FENCE].value)
  149. return 0;
  150. rc = synx_hwfence_uninitialize(
  151. (struct synx_session *)core->synx_fence_data.session);
  152. if (rc) {
  153. d_vpr_e("%s: failed to deregister synx fence\n", __func__);
  154. /* ignore error */
  155. rc = 0;
  156. } else {
  157. d_vpr_l("%s: successfully deregistered synx fence\n", __func__);
  158. }
  159. return rc;
  160. }
  161. static struct msm_vidc_fence *msm_vidc_synx_dma_fence_create(struct msm_vidc_inst *inst)
  162. {
  163. struct msm_vidc_fence *fence = NULL;
  164. int rc = 0;
  165. if (!inst) {
  166. d_vpr_e("%s: invalid params\n", __func__);
  167. return NULL;
  168. }
  169. rc = msm_vidc_vmem_alloc(sizeof(*fence), (void **)&fence, __func__);
  170. if (rc)
  171. return NULL;
  172. fence->fd = INVALID_FD;
  173. spin_lock_init(&fence->lock);
  174. dma_fence_init(&fence->dma_fence, &msm_vidc_synx_dma_fence_ops,
  175. &fence->lock, inst->fence_context.ctx_num,
  176. ++inst->fence_context.seq_num);
  177. snprintf(fence->name, sizeof(fence->name), "synx %s: %llu",
  178. inst->fence_context.name, inst->fence_context.seq_num);
  179. fence->fence_id = fence->dma_fence.seqno;
  180. INIT_LIST_HEAD(&fence->list);
  181. list_add_tail(&fence->list, &inst->fence_list);
  182. i_vpr_l(inst, "%s: created %s\n", __func__, fence->name);
  183. return fence;
  184. }
  185. static struct msm_vidc_fence *msm_vidc_synx_fence_create(struct msm_vidc_inst *inst)
  186. {
  187. int rc = 0;
  188. struct msm_vidc_fence *fence = NULL;
  189. struct msm_vidc_core *core = NULL;
  190. struct synx_create_params params;
  191. u32 fence_id = 0;
  192. if (!inst || !inst->core || !fence) {
  193. d_vpr_e("%s: invalid params\n", __func__);
  194. return NULL;
  195. }
  196. core = inst->core;
  197. if (!core->capabilities) {
  198. d_vpr_e("%s: invalid core caps\n", __func__);
  199. return NULL;
  200. }
  201. /* return if synx fence is not supported */
  202. if (!core->capabilities[SUPPORTS_SYNX_FENCE].value)
  203. return NULL;
  204. /* create dma fence */
  205. fence = msm_vidc_synx_dma_fence_create(inst);
  206. if (!fence) {
  207. i_vpr_e(inst, "%s: failed to create dma fence\n", __func__);
  208. return NULL;
  209. }
  210. if (!core->synx_fence_data.session) {
  211. i_vpr_e(inst, "%s: invalid synx fence session\n", __func__);
  212. goto destroy_dma_fence;
  213. }
  214. /* fill synx fence params structure */
  215. memset(&params, 0, sizeof(struct synx_create_params));
  216. params.name = fence->name;
  217. params.fence = (void *)&fence->dma_fence;
  218. params.h_synx = &fence_id;
  219. params.flags = MSM_VIDC_SYNX_CREATE_DMA_FENCE;
  220. /* create hw fence */
  221. rc = synx_hwfence_create(
  222. (struct synx_session *)core->synx_fence_data.session,
  223. &params);
  224. if (rc) {
  225. i_vpr_e(inst, "%s: failed to create hw fence for %s",
  226. __func__, fence->name);
  227. goto destroy_dma_fence;
  228. }
  229. fence->fence_id = (u64)(*(params.h_synx));
  230. /* this copy of hw fence client handle is req. to destroy synx fence */
  231. fence->session = core->synx_fence_data.session;
  232. i_vpr_l(inst, "%s: successfully created synx fence with id: %llu",
  233. __func__, fence->fence_id);
  234. return fence;
  235. destroy_dma_fence:
  236. msm_vidc_synx_fence_destroy(inst, fence->fence_id);
  237. return NULL;
  238. }
  239. int msm_vidc_synx_fence_create_fd(struct msm_vidc_inst *inst,
  240. struct msm_vidc_fence *fence)
  241. {
  242. int rc = 0;
  243. if (!inst || !fence) {
  244. d_vpr_e("%s: invalid params\n", __func__);
  245. return -EINVAL;
  246. }
  247. fence->fd = get_unused_fd_flags(0);
  248. if (fence->fd < 0) {
  249. i_vpr_e(inst, "%s: getting fd (%d) failed\n", __func__,
  250. fence->fd);
  251. rc = -EINVAL;
  252. goto err_fd;
  253. }
  254. fence->sync_file = sync_file_create(&fence->dma_fence);
  255. if (!fence->sync_file) {
  256. i_vpr_e(inst, "%s: sync_file_create failed\n", __func__);
  257. rc = -EINVAL;
  258. goto err_sync_file;
  259. }
  260. fd_install(fence->fd, fence->sync_file->file);
  261. i_vpr_l(inst, "%s: created fd %d for fence %s\n", __func__,
  262. fence->fd, fence->name);
  263. return 0;
  264. err_sync_file:
  265. put_unused_fd(fence->fd);
  266. err_fd:
  267. return rc;
  268. }
  269. static int msm_vidc_synx_fence_signal(struct msm_vidc_inst *inst, u64 fence_id)
  270. {
  271. int rc = 0;
  272. struct msm_vidc_fence *fence;
  273. struct msm_vidc_core *core;
  274. if (!inst || !inst->core) {
  275. d_vpr_e("%s: invalid params\n", __func__);
  276. return -EINVAL;
  277. }
  278. core = inst->core;
  279. if (!core->capabilities) {
  280. d_vpr_e("%s: invalid core caps\n", __func__);
  281. return -EINVAL;
  282. }
  283. fence = msm_vidc_get_synx_fence_from_id(inst, fence_id);
  284. if (!fence) {
  285. i_vpr_e(inst, "%s: no fence available to signal with id: %u\n",
  286. __func__, fence_id);
  287. rc = -EINVAL;
  288. goto exit;
  289. }
  290. i_vpr_l(inst, "%s: fence %s\n", __func__, fence->name);
  291. list_del_init(&fence->list);
  292. dma_fence_signal(&fence->dma_fence);
  293. dma_fence_put(&fence->dma_fence);
  294. exit:
  295. return rc;
  296. }
  297. static void msm_vidc_synx_fence_recover(struct msm_vidc_core *core)
  298. {
  299. int rc = 0;
  300. if (!core) {
  301. d_vpr_e("%s: invalid paras\n", __func__);
  302. return;
  303. }
  304. rc = synx_hwfence_recover(
  305. (enum synx_client_id)core->synx_fence_data.client_id);
  306. if (rc)
  307. d_vpr_e("%s: failed to recover synx fences for client id: %d",
  308. __func__,
  309. (enum synx_client_id)core->synx_fence_data.client_id);
  310. return;
  311. }
  312. const struct msm_vidc_fence_ops *get_synx_fence_ops(void)
  313. {
  314. static struct msm_vidc_fence_ops synx_ops;
  315. synx_ops.fence_register = msm_vidc_synx_fence_register;
  316. synx_ops.fence_deregister = msm_vidc_synx_fence_deregister;
  317. synx_ops.fence_create = msm_vidc_synx_fence_create;
  318. synx_ops.fence_create_fd = msm_vidc_synx_fence_create_fd;
  319. synx_ops.fence_destroy = msm_vidc_synx_fence_destroy;
  320. synx_ops.fence_signal = msm_vidc_synx_fence_signal;
  321. synx_ops.fence_recover = msm_vidc_synx_fence_recover;
  322. return &synx_ops;
  323. }