cvp_vm_msgq.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /* SPDX-License-Identifier: GPL-2.0-only
  2. *
  3. * Copyright (c) 2022, Qualcomm Innovation Center, Inc. All rights reserved.
  4. * Copyright (c) 2023-2024 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/kthread.h>
  7. #include "cvp_vm_msgq.h"
  8. #include "msm_cvp_debug.h"
  9. /**
  10. * cvp_msgq_receiver - thread function that receive msg from gunyah msgq
  11. * data: cvp_msgq_drv pointer
  12. *
  13. * Note: single thread. If the sub-function or global data used in this
  14. * function is also used somehwere else, please add rx_lock.
  15. */
  16. static int cvp_msgq_receiver(void *data)
  17. {
  18. struct cvp_msgq_drv *msgq_drv = data;
  19. struct cvp_ipc_msg *msg_ptr;
  20. size_t size;
  21. bool is_resp;
  22. /**
  23. * true: response received from remote VM, cmd initiated from LOCAL VM;
  24. * false: cmd initiated from REMOTE VM;
  25. */
  26. int rc = -1;
  27. if (IS_ERR_OR_NULL(msgq_drv))
  28. return -EINVAL;
  29. msg_ptr = kzalloc(sizeof(*msg_ptr), GFP_KERNEL);
  30. if (!msg_ptr) {
  31. dprintk(CVP_ERR, "%s: fail to allocate mem\n", __func__);
  32. return -ENOMEM;
  33. }
  34. while (true) {
  35. rc = gh_msgq_recv(msgq_drv->config.handle, msg_ptr,
  36. sizeof(*msg_ptr), &size, 0);
  37. if (rc != 0 ) {
  38. dprintk(CVP_ERR,
  39. "%s: gh_msgq_recv fail rc=%d handle=%#x msg_ptr=%#x\n",
  40. __func__, rc, msgq_drv->config.handle, msg_ptr);
  41. if (rc != -EAGAIN) {
  42. kfree(msg_ptr);
  43. return rc;
  44. }
  45. continue;
  46. }
  47. is_resp = (msg_ptr->type &
  48. CVP_IPC_MSG_TYPE_DIR_CHECK) ? true : false;
  49. if (is_resp == false) {
  50. dprintk(CVP_VM,
  51. "%s: gh_msgq_recv cmd from remote VM\n",
  52. __func__);
  53. if (msgq_drv->pending_local_cmd.type == 0) {
  54. /* copy ipc message to local cmd */
  55. memcpy(&msgq_drv->pending_local_cmd,
  56. msg_ptr, sizeof(struct cvp_ipc_msg));
  57. /* toggle the direction bit*/
  58. msgq_drv->pending_local_cmd.type ^=
  59. CVP_IPC_MSG_TYPE_DIR_CHECK;
  60. /* TODO: call client function ptr to process */
  61. memcpy(msg_ptr, &msgq_drv->pending_local_cmd,
  62. sizeof(struct cvp_ipc_msg));
  63. /* 4: elements before actual data in cvp_ipc_msg*/
  64. size = (4 + msgq_drv->pending_local_cmd.len)<<2;
  65. /* sanity check on size information */
  66. if (size > GH_MSGQ_MAX_MSG_SIZE_BYTES) {
  67. dprintk(CVP_ERR,
  68. "%s: msg size %d exceed max size supported %d \n",
  69. __func__, size, GH_MSGQ_MAX_MSG_SIZE_BYTES);
  70. rc = -E2BIG;
  71. msgq_drv->pending_local_cmd.type = 0;
  72. continue;
  73. }
  74. /* send it back to the remote VM as response */
  75. rc = gh_msgq_send(msgq_drv->config.handle,
  76. msg_ptr, size, GH_MSGQ_TX_PUSH);
  77. if (rc < 0) {
  78. dprintk(CVP_ERR,
  79. "%s: failed gh_msgq_send rc %d \n",
  80. __func__, rc);
  81. }
  82. /* flag the source is released */
  83. msgq_drv->pending_local_cmd.type = 0;
  84. }
  85. else {
  86. dprintk(CVP_ERR,
  87. "%s: Msg rejected, local cmd in use type %d\n",
  88. __func__, msgq_drv->pending_local_cmd.type);
  89. }
  90. }
  91. else {
  92. dprintk(CVP_VM,
  93. "%s: gh_msgq_recv respond type from remote VM\n",
  94. __func__);
  95. if ((msg_ptr->type & CVP_IPC_MSG_TYPE_ACT_CHECK) !=
  96. msgq_drv->pending_remote_rsp.type) {
  97. dprintk(CVP_ERR,
  98. "%s: Msg disgard,recv type %d, pend local %d\n",
  99. __func__, msg_ptr->type,
  100. msgq_drv->pending_remote_rsp.type);
  101. }
  102. else {
  103. /* memcpy received data to pending_remote_rsp */
  104. memcpy(&msgq_drv->pending_remote_rsp, msg_ptr,
  105. sizeof(struct cvp_ipc_msg));
  106. /* clear direction bit of pending_remote_rsp */
  107. msgq_drv->pending_remote_rsp.type &=
  108. (~CVP_IPC_MSG_TYPE_DIR_CHECK);
  109. /* complete for cmd initiated from local VM */
  110. complete(&msgq_drv->completions[
  111. msgq_drv->pending_remote_rsp.type - 1]);
  112. }
  113. }
  114. }
  115. return 0;
  116. }
  117. static int cvp_complete_msgq_init(struct cvp_msgq_drv *msgq_drv)
  118. {
  119. int i;
  120. msgq_drv->receiver_thread = kthread_run(
  121. cvp_msgq_receiver,
  122. (void *)msgq_drv,
  123. "CVP msgq receiver");
  124. if (IS_ERR_OR_NULL(msgq_drv->receiver_thread)) {
  125. dprintk(CVP_ERR, "Failed to start msgq receiver thread\n");
  126. return -EINVAL;
  127. }
  128. mutex_init(&msgq_drv->ipc_lock);
  129. for (i = 0; i <= (CVP_MAX_IPC_CMD - 1); i++)
  130. init_completion(&msgq_drv->completions[i]);
  131. return 0;
  132. }
  133. #ifndef CONFIG_EVA_TVM
  134. static int cvp_msgq_cb(struct notifier_block *nb,
  135. unsigned long cmd, void *data)
  136. {
  137. struct gh_rm_notif_vm_status_payload *vm_status_payload;
  138. struct cvp_gh_msgq_config *msgq_config;
  139. struct cvp_msgq_drv *msgq_drv;
  140. gh_vmid_t peer_vmid;
  141. gh_vmid_t self_vmid;
  142. int rc;
  143. if (IS_ERR_OR_NULL(nb))
  144. return -EINVAL;
  145. msgq_config = container_of(nb, struct cvp_gh_msgq_config, rm_nb);
  146. msgq_drv = container_of(msgq_config, struct cvp_msgq_drv, config);
  147. if (cmd != GH_RM_NOTIF_VM_STATUS)
  148. return NOTIFY_DONE;
  149. /**
  150. * Check VM status, only GH_TRUSTED_VM notification activate
  151. * Gunyah msgq registration
  152. */
  153. vm_status_payload = (struct gh_rm_notif_vm_status_payload *)data;
  154. if (vm_status_payload->vm_status != GH_RM_VM_STATUS_READY)
  155. return -12;
  156. if (ghd_rm_get_vmid(msgq_config->peer_id, &peer_vmid))
  157. return -13;
  158. if (ghd_rm_get_vmid(GH_PRIMARY_VM, &self_vmid))
  159. return -14;
  160. if (peer_vmid != vm_status_payload->vmid)
  161. return NOTIFY_DONE;
  162. dprintk(CVP_VM, "%s: vmid=%d, peer_vmid=%d\n",
  163. __func__, vm_status_payload->vmid, peer_vmid);
  164. if (msgq_config->handle)
  165. return -15;
  166. msgq_config->handle = gh_msgq_register(GH_MSGQ_LABEL_EVA);
  167. if (IS_ERR(msgq_config->handle)) {
  168. rc = PTR_ERR(msgq_drv->config.handle);
  169. dprintk(CVP_ERR, "PVM failed to register msgq %d\n", rc);
  170. return rc;
  171. }
  172. dprintk(CVP_VM, "%s: gh_msgq_register handle: %x\n",
  173. __func__, msgq_config->handle);
  174. rc = cvp_complete_msgq_init(msgq_drv);
  175. return rc;
  176. }
  177. #endif
  178. static int cvp_msgq_init(struct cvp_msgq_drv *msgq_drv)
  179. {
  180. int rc = 0;
  181. msgq_drv->config.label = GH_MSGQ_LABEL_EVA;
  182. msgq_drv->config.handle = NULL;
  183. #ifndef CONFIG_EVA_TVM
  184. /* PVM init */
  185. msgq_drv->config.peer_id = GH_TRUSTED_VM;
  186. msgq_drv->config.rm_nb.notifier_call = cvp_msgq_cb;
  187. rc = gh_rm_register_notifier(&msgq_drv->config.rm_nb);
  188. if (rc) {
  189. dprintk(CVP_ERR, "PVM Fail register msgq notifier %d\n", rc);
  190. return rc;
  191. }
  192. dprintk(CVP_VM, "%s: gh_rm_register_notifier\n", __func__);
  193. #else
  194. /* TVM init */
  195. msgq_drv->config.handle = gh_msgq_register(GH_MSGQ_LABEL_EVA);
  196. if (IS_ERR(msgq_drv->config.handle)) {
  197. rc = PTR_ERR(msgq_drv->config.handle);
  198. dprintk(CVP_ERR, "TVM failed to register msgq %d\n", rc);
  199. return rc;
  200. }
  201. rc = cvp_complete_msgq_init(msgq_drv);
  202. #endif
  203. return rc;
  204. }
  205. static int cvp_msgq_deinit(struct cvp_msgq_drv *msgq_drv)
  206. {
  207. if (msgq_drv->receiver_thread)
  208. kthread_stop(msgq_drv->receiver_thread);
  209. return 0;
  210. }
  211. static int cvp_msgq_send_cmd(struct cvp_msgq_drv *msgq_drv,
  212. void *msg, size_t msg_size)
  213. {
  214. int rc = -1;
  215. struct cvp_ipc_msg *msg_ptr = msg;
  216. if (!msgq_drv->config.handle) {
  217. dprintk(CVP_ERR, "%s: Invalid msgq handle\n", __func__);
  218. rc = -EINVAL;
  219. goto err_param_check;
  220. }
  221. if (msg_size > GH_MSGQ_MAX_MSG_SIZE_BYTES) {
  222. dprintk(CVP_ERR,
  223. "%s: msg size %d exceed max size supported %d \n",
  224. __func__, msg_size, GH_MSGQ_MAX_MSG_SIZE_BYTES);
  225. rc = -E2BIG;
  226. goto err_param_check;
  227. }
  228. mutex_lock(&msgq_drv->ipc_lock);
  229. /* init case: only allow sending msg sequentially */
  230. if (msgq_drv->pending_remote_rsp.type &
  231. CVP_IPC_MSG_TYPE_ACT_CHECK) {
  232. rc = -EPERM;
  233. dprintk(CVP_ERR,
  234. "%s: Msg rejected, local rsp occupied.\n",
  235. __func__);
  236. goto err_valid_check;
  237. }
  238. /* book keeping type bits in pending_remote_rsp */
  239. msgq_drv->pending_remote_rsp.type = msg_ptr->type;
  240. rc = gh_msgq_send(msgq_drv->config.handle,
  241. msg_ptr, msg_size, GH_MSGQ_TX_PUSH);
  242. if (rc < 0) {
  243. dprintk(CVP_ERR,
  244. "%s: failed with gh_msgq_send with rc %d \n",
  245. __func__, rc);
  246. goto err_gh_send;
  247. }
  248. /* wait for completion */
  249. if (!wait_for_completion_timeout(
  250. &msgq_drv->completions[msgq_drv->pending_remote_rsp.type - 1],
  251. msecs_to_jiffies(CVP_VM_RESPONSE_TIMEOUT))) {
  252. dprintk(CVP_ERR, "%s cvp ipc msg type %d timeout\n",
  253. __func__, msgq_drv->pending_remote_rsp.type-1);
  254. rc = -ETIMEDOUT;
  255. }
  256. /* copy pending_remote_rsp content to msg (inout param)*/
  257. memcpy(msg, &msgq_drv->pending_remote_rsp,
  258. sizeof(struct cvp_ipc_msg));
  259. /* clear type bits to indicate resource is avaialbel */
  260. msgq_drv->pending_remote_rsp.type = 0;
  261. mutex_unlock(&msgq_drv->ipc_lock);
  262. return rc;
  263. err_gh_send:
  264. err_valid_check:
  265. mutex_unlock(&msgq_drv->ipc_lock);
  266. err_param_check:
  267. return rc;
  268. }
  269. static struct cvp_msgq_ops msgq_ops = {
  270. .msgq_init = cvp_msgq_init,
  271. .msgq_deinit = cvp_msgq_deinit,
  272. .msgq_send = cvp_msgq_send_cmd,
  273. .msgq_receive = NULL,
  274. };
  275. struct cvp_msgq_drv cvp_ipc_msgq = {
  276. .ops = &msgq_ops,
  277. };