cvp_vm_msgq.c 8.2 KB

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