cam_lrme_context.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-2020, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/module.h>
  6. #include <linux/kernel.h>
  7. #include "cam_debug_util.h"
  8. #include "cam_lrme_context.h"
  9. static const char lrme_dev_name[] = "cam-lrme";
  10. static int __cam_lrme_ctx_acquire_dev_in_available(struct cam_context *ctx,
  11. struct cam_acquire_dev_cmd *cmd)
  12. {
  13. int rc = 0;
  14. uintptr_t ctxt_to_hw_map = (uintptr_t)ctx->ctxt_to_hw_map;
  15. struct cam_lrme_context *lrme_ctx = ctx->ctx_priv;
  16. CAM_DBG(CAM_LRME, "Enter ctx %d", ctx->ctx_id);
  17. rc = cam_context_acquire_dev_to_hw(ctx, cmd);
  18. if (rc) {
  19. CAM_ERR(CAM_LRME, "Failed to acquire");
  20. return rc;
  21. }
  22. ctxt_to_hw_map |= (lrme_ctx->index << CAM_LRME_CTX_INDEX_SHIFT);
  23. ctx->ctxt_to_hw_map = (void *)ctxt_to_hw_map;
  24. ctx->state = CAM_CTX_ACQUIRED;
  25. return rc;
  26. }
  27. static int __cam_lrme_ctx_release_dev_in_acquired(struct cam_context *ctx,
  28. struct cam_release_dev_cmd *cmd)
  29. {
  30. int rc = 0;
  31. CAM_DBG(CAM_LRME, "Enter ctx %d", ctx->ctx_id);
  32. rc = cam_context_release_dev_to_hw(ctx, cmd);
  33. if (rc) {
  34. CAM_ERR(CAM_LRME, "Failed to release");
  35. return rc;
  36. }
  37. ctx->state = CAM_CTX_AVAILABLE;
  38. return rc;
  39. }
  40. static int __cam_lrme_ctx_start_dev_in_acquired(struct cam_context *ctx,
  41. struct cam_start_stop_dev_cmd *cmd)
  42. {
  43. int rc = 0;
  44. CAM_DBG(CAM_LRME, "Enter ctx %d", ctx->ctx_id);
  45. rc = cam_context_start_dev_to_hw(ctx, cmd);
  46. if (rc) {
  47. CAM_ERR(CAM_LRME, "Failed to start");
  48. return rc;
  49. }
  50. ctx->state = CAM_CTX_ACTIVATED;
  51. return rc;
  52. }
  53. static int __cam_lrme_ctx_config_dev_in_activated(struct cam_context *ctx,
  54. struct cam_config_dev_cmd *cmd)
  55. {
  56. int rc;
  57. CAM_DBG(CAM_LRME, "Enter ctx %d", ctx->ctx_id);
  58. rc = cam_context_prepare_dev_to_hw(ctx, cmd);
  59. if (rc) {
  60. CAM_ERR(CAM_LRME, "Failed to config");
  61. return rc;
  62. }
  63. return rc;
  64. }
  65. static int __cam_lrme_ctx_dump_dev_in_activated(
  66. struct cam_context *ctx,
  67. struct cam_dump_req_cmd *cmd)
  68. {
  69. int rc = 0;
  70. CAM_DBG(CAM_LRME, "Enter ctx %d", ctx->ctx_id);
  71. rc = cam_context_dump_dev_to_hw(ctx, cmd);
  72. if (rc)
  73. CAM_ERR(CAM_LRME, "Failed to dump device");
  74. return rc;
  75. }
  76. static int __cam_lrme_ctx_flush_dev_in_activated(struct cam_context *ctx,
  77. struct cam_flush_dev_cmd *cmd)
  78. {
  79. int rc;
  80. CAM_DBG(CAM_LRME, "Enter ctx %d", ctx->ctx_id);
  81. rc = cam_context_flush_dev_to_hw(ctx, cmd);
  82. if (rc)
  83. CAM_ERR(CAM_LRME, "Failed to flush device");
  84. return rc;
  85. }
  86. static int __cam_lrme_ctx_stop_dev_in_activated(struct cam_context *ctx,
  87. struct cam_start_stop_dev_cmd *cmd)
  88. {
  89. int rc = 0;
  90. CAM_DBG(CAM_LRME, "Enter ctx %d", ctx->ctx_id);
  91. rc = cam_context_stop_dev_to_hw(ctx);
  92. if (rc) {
  93. CAM_ERR(CAM_LRME, "Failed to stop dev");
  94. return rc;
  95. }
  96. ctx->state = CAM_CTX_ACQUIRED;
  97. return rc;
  98. }
  99. static int __cam_lrme_ctx_release_dev_in_activated(struct cam_context *ctx,
  100. struct cam_release_dev_cmd *cmd)
  101. {
  102. int rc = 0;
  103. CAM_DBG(CAM_LRME, "Enter ctx %d", ctx->ctx_id);
  104. rc = __cam_lrme_ctx_stop_dev_in_activated(ctx, NULL);
  105. if (rc) {
  106. CAM_ERR(CAM_LRME, "Failed to stop");
  107. return rc;
  108. }
  109. rc = cam_context_release_dev_to_hw(ctx, cmd);
  110. if (rc) {
  111. CAM_ERR(CAM_LRME, "Failed to release");
  112. return rc;
  113. }
  114. ctx->state = CAM_CTX_AVAILABLE;
  115. return rc;
  116. }
  117. static int __cam_lrme_ctx_handle_irq_in_activated(void *context,
  118. uint32_t evt_id, void *evt_data)
  119. {
  120. int rc;
  121. CAM_DBG(CAM_LRME, "Enter");
  122. rc = cam_context_buf_done_from_hw(context, evt_data, evt_id);
  123. if (rc) {
  124. CAM_ERR(CAM_LRME, "Failed in buf done, rc=%d", rc);
  125. return rc;
  126. }
  127. return rc;
  128. }
  129. /* top state machine */
  130. static struct cam_ctx_ops
  131. cam_lrme_ctx_state_machine[CAM_CTX_STATE_MAX] = {
  132. /* Uninit */
  133. {
  134. .ioctl_ops = {},
  135. .crm_ops = {},
  136. .irq_ops = NULL,
  137. },
  138. /* Available */
  139. {
  140. .ioctl_ops = {
  141. .acquire_dev = __cam_lrme_ctx_acquire_dev_in_available,
  142. },
  143. .crm_ops = {},
  144. .irq_ops = NULL,
  145. },
  146. /* Acquired */
  147. {
  148. .ioctl_ops = {
  149. .config_dev = __cam_lrme_ctx_config_dev_in_activated,
  150. .release_dev = __cam_lrme_ctx_release_dev_in_acquired,
  151. .start_dev = __cam_lrme_ctx_start_dev_in_acquired,
  152. },
  153. .crm_ops = {},
  154. .irq_ops = NULL,
  155. },
  156. /* Ready */
  157. {
  158. .ioctl_ops = {},
  159. .crm_ops = {},
  160. .irq_ops = NULL,
  161. },
  162. /* Flushed */
  163. {},
  164. /* Activate */
  165. {
  166. .ioctl_ops = {
  167. .config_dev = __cam_lrme_ctx_config_dev_in_activated,
  168. .release_dev = __cam_lrme_ctx_release_dev_in_activated,
  169. .stop_dev = __cam_lrme_ctx_stop_dev_in_activated,
  170. .flush_dev = __cam_lrme_ctx_flush_dev_in_activated,
  171. .dump_dev = __cam_lrme_ctx_dump_dev_in_activated,
  172. },
  173. .crm_ops = {},
  174. .irq_ops = __cam_lrme_ctx_handle_irq_in_activated,
  175. },
  176. };
  177. int cam_lrme_context_init(struct cam_lrme_context *lrme_ctx,
  178. struct cam_context *base_ctx,
  179. struct cam_hw_mgr_intf *hw_intf,
  180. uint32_t index)
  181. {
  182. int rc = 0;
  183. CAM_DBG(CAM_LRME, "Enter");
  184. if (!base_ctx || !lrme_ctx) {
  185. CAM_ERR(CAM_LRME, "Invalid input");
  186. return -EINVAL;
  187. }
  188. memset(lrme_ctx, 0, sizeof(*lrme_ctx));
  189. rc = cam_context_init(base_ctx, lrme_dev_name, CAM_LRME, index,
  190. NULL, hw_intf, lrme_ctx->req_base, CAM_CTX_REQ_MAX);
  191. if (rc) {
  192. CAM_ERR(CAM_LRME, "Failed to init context");
  193. return rc;
  194. }
  195. lrme_ctx->base = base_ctx;
  196. lrme_ctx->index = index;
  197. base_ctx->ctx_priv = lrme_ctx;
  198. base_ctx->state_machine = cam_lrme_ctx_state_machine;
  199. base_ctx->max_hw_update_entries = CAM_CTX_CFG_MAX;
  200. base_ctx->max_in_map_entries = CAM_CTX_CFG_MAX;
  201. base_ctx->max_out_map_entries = CAM_CTX_CFG_MAX;
  202. return rc;
  203. }
  204. int cam_lrme_context_deinit(struct cam_lrme_context *lrme_ctx)
  205. {
  206. int rc = 0;
  207. CAM_DBG(CAM_LRME, "Enter");
  208. if (!lrme_ctx) {
  209. CAM_ERR(CAM_LRME, "No ctx to deinit");
  210. return -EINVAL;
  211. }
  212. rc = cam_context_deinit(lrme_ctx->base);
  213. memset(lrme_ctx, 0, sizeof(*lrme_ctx));
  214. return rc;
  215. }