vpu_cmds.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2020-2021 NXP
  4. */
  5. #include <linux/init.h>
  6. #include <linux/interconnect.h>
  7. #include <linux/ioctl.h>
  8. #include <linux/list.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/slab.h>
  13. #include <linux/types.h>
  14. #include <linux/delay.h>
  15. #include "vpu.h"
  16. #include "vpu_defs.h"
  17. #include "vpu_cmds.h"
  18. #include "vpu_rpc.h"
  19. #include "vpu_mbox.h"
  20. struct vpu_cmd_request {
  21. u32 request;
  22. u32 response;
  23. u32 handled;
  24. };
  25. struct vpu_cmd_t {
  26. struct list_head list;
  27. u32 id;
  28. struct vpu_cmd_request *request;
  29. struct vpu_rpc_event *pkt;
  30. unsigned long key;
  31. atomic_long_t *last_response_cmd;
  32. };
  33. static struct vpu_cmd_request vpu_cmd_requests[] = {
  34. {
  35. .request = VPU_CMD_ID_CONFIGURE_CODEC,
  36. .response = VPU_MSG_ID_MEM_REQUEST,
  37. .handled = 1,
  38. },
  39. {
  40. .request = VPU_CMD_ID_START,
  41. .response = VPU_MSG_ID_START_DONE,
  42. .handled = 0,
  43. },
  44. {
  45. .request = VPU_CMD_ID_STOP,
  46. .response = VPU_MSG_ID_STOP_DONE,
  47. .handled = 0,
  48. },
  49. {
  50. .request = VPU_CMD_ID_ABORT,
  51. .response = VPU_MSG_ID_ABORT_DONE,
  52. .handled = 0,
  53. },
  54. {
  55. .request = VPU_CMD_ID_RST_BUF,
  56. .response = VPU_MSG_ID_BUF_RST,
  57. .handled = 1,
  58. },
  59. };
  60. static int vpu_cmd_send(struct vpu_core *core, struct vpu_rpc_event *pkt)
  61. {
  62. int ret = 0;
  63. ret = vpu_iface_send_cmd(core, pkt);
  64. if (ret)
  65. return ret;
  66. /*write cmd data to cmd buffer before trigger a cmd interrupt*/
  67. mb();
  68. vpu_mbox_send_type(core, COMMAND);
  69. return ret;
  70. }
  71. static struct vpu_cmd_t *vpu_alloc_cmd(struct vpu_inst *inst, u32 id, void *data)
  72. {
  73. struct vpu_cmd_t *cmd;
  74. int i;
  75. int ret;
  76. cmd = kzalloc_obj(*cmd);
  77. if (!cmd)
  78. return NULL;
  79. cmd->pkt = kzalloc_obj(*cmd->pkt);
  80. if (!cmd->pkt) {
  81. kfree(cmd);
  82. return NULL;
  83. }
  84. cmd->id = id;
  85. ret = vpu_iface_pack_cmd(inst->core, cmd->pkt, inst->id, id, data);
  86. if (ret) {
  87. dev_err(inst->dev, "iface pack cmd %s fail\n", vpu_id_name(id));
  88. kfree(cmd->pkt);
  89. kfree(cmd);
  90. return NULL;
  91. }
  92. for (i = 0; i < ARRAY_SIZE(vpu_cmd_requests); i++) {
  93. if (vpu_cmd_requests[i].request == id) {
  94. cmd->request = &vpu_cmd_requests[i];
  95. break;
  96. }
  97. }
  98. return cmd;
  99. }
  100. static void vpu_free_cmd(struct vpu_cmd_t *cmd)
  101. {
  102. if (!cmd)
  103. return;
  104. if (cmd->last_response_cmd)
  105. atomic_long_set(cmd->last_response_cmd, cmd->key);
  106. kfree(cmd->pkt);
  107. kfree(cmd);
  108. }
  109. static int vpu_session_process_cmd(struct vpu_inst *inst, struct vpu_cmd_t *cmd)
  110. {
  111. int ret;
  112. dev_dbg(inst->dev, "[%d]send cmd %s\n", inst->id, vpu_id_name(cmd->id));
  113. vpu_iface_pre_send_cmd(inst);
  114. ret = vpu_cmd_send(inst->core, cmd->pkt);
  115. if (!ret) {
  116. vpu_iface_post_send_cmd(inst);
  117. vpu_inst_record_flow(inst, cmd->id);
  118. } else {
  119. dev_err(inst->dev, "[%d] iface send cmd %s fail\n", inst->id, vpu_id_name(cmd->id));
  120. }
  121. return ret;
  122. }
  123. static void vpu_process_cmd_request(struct vpu_inst *inst)
  124. {
  125. struct vpu_cmd_t *cmd;
  126. struct vpu_cmd_t *tmp;
  127. if (!inst || inst->pending)
  128. return;
  129. list_for_each_entry_safe(cmd, tmp, &inst->cmd_q, list) {
  130. list_del_init(&cmd->list);
  131. if (vpu_session_process_cmd(inst, cmd))
  132. dev_err(inst->dev, "[%d] process cmd %s fail\n",
  133. inst->id, vpu_id_name(cmd->id));
  134. if (cmd->request) {
  135. inst->pending = (void *)cmd;
  136. break;
  137. }
  138. vpu_free_cmd(cmd);
  139. }
  140. }
  141. static int vpu_request_cmd(struct vpu_inst *inst, u32 id, void *data,
  142. unsigned long *key, int *sync)
  143. {
  144. struct vpu_core *core;
  145. struct vpu_cmd_t *cmd;
  146. if (!inst || !inst->core)
  147. return -EINVAL;
  148. core = inst->core;
  149. cmd = vpu_alloc_cmd(inst, id, data);
  150. if (!cmd)
  151. return -ENOMEM;
  152. mutex_lock(&core->cmd_lock);
  153. cmd->key = ++inst->cmd_seq;
  154. cmd->last_response_cmd = &inst->last_response_cmd;
  155. if (key)
  156. *key = cmd->key;
  157. if (sync)
  158. *sync = cmd->request ? true : false;
  159. list_add_tail(&cmd->list, &inst->cmd_q);
  160. vpu_process_cmd_request(inst);
  161. mutex_unlock(&core->cmd_lock);
  162. return 0;
  163. }
  164. static void vpu_clear_pending(struct vpu_inst *inst)
  165. {
  166. if (!inst || !inst->pending)
  167. return;
  168. vpu_free_cmd(inst->pending);
  169. wake_up_all(&inst->core->ack_wq);
  170. inst->pending = NULL;
  171. }
  172. static bool vpu_check_response(struct vpu_cmd_t *cmd, u32 response, u32 handled)
  173. {
  174. struct vpu_cmd_request *request;
  175. if (!cmd || !cmd->request)
  176. return false;
  177. request = cmd->request;
  178. if (request->response != response)
  179. return false;
  180. if (request->handled != handled)
  181. return false;
  182. return true;
  183. }
  184. int vpu_response_cmd(struct vpu_inst *inst, u32 response, u32 handled)
  185. {
  186. struct vpu_core *core;
  187. if (!inst || !inst->core)
  188. return -EINVAL;
  189. core = inst->core;
  190. mutex_lock(&core->cmd_lock);
  191. if (vpu_check_response(inst->pending, response, handled))
  192. vpu_clear_pending(inst);
  193. vpu_process_cmd_request(inst);
  194. mutex_unlock(&core->cmd_lock);
  195. return 0;
  196. }
  197. void vpu_clear_request(struct vpu_inst *inst)
  198. {
  199. struct vpu_cmd_t *cmd;
  200. struct vpu_cmd_t *tmp;
  201. mutex_lock(&inst->core->cmd_lock);
  202. if (inst->pending)
  203. vpu_clear_pending(inst);
  204. list_for_each_entry_safe(cmd, tmp, &inst->cmd_q, list) {
  205. list_del_init(&cmd->list);
  206. vpu_free_cmd(cmd);
  207. }
  208. mutex_unlock(&inst->core->cmd_lock);
  209. }
  210. static bool check_is_responsed(struct vpu_inst *inst, unsigned long key)
  211. {
  212. unsigned long last_response = atomic_long_read(&inst->last_response_cmd);
  213. if (key <= last_response && (last_response - key) < (ULONG_MAX >> 1))
  214. return true;
  215. return false;
  216. }
  217. static int sync_session_response(struct vpu_inst *inst, unsigned long key, long timeout, int try)
  218. {
  219. struct vpu_core *core;
  220. if (!inst || !inst->core)
  221. return -EINVAL;
  222. core = inst->core;
  223. call_void_vop(inst, wait_prepare);
  224. wait_event_timeout(core->ack_wq, check_is_responsed(inst, key), timeout);
  225. call_void_vop(inst, wait_finish);
  226. if (!check_is_responsed(inst, key)) {
  227. if (try)
  228. return -EINVAL;
  229. dev_err(inst->dev, "[%d] sync session timeout\n", inst->id);
  230. set_bit(inst->id, &core->hang_mask);
  231. mutex_lock(&inst->core->cmd_lock);
  232. vpu_clear_pending(inst);
  233. mutex_unlock(&inst->core->cmd_lock);
  234. return -EINVAL;
  235. }
  236. return 0;
  237. }
  238. static void vpu_core_keep_active(struct vpu_core *core)
  239. {
  240. struct vpu_rpc_event pkt;
  241. memset(&pkt, 0, sizeof(pkt));
  242. vpu_iface_pack_cmd(core, &pkt, 0, VPU_CMD_ID_NOOP, NULL);
  243. dev_dbg(core->dev, "try to wake up\n");
  244. mutex_lock(&core->cmd_lock);
  245. if (vpu_cmd_send(core, &pkt))
  246. dev_err(core->dev, "fail to keep active\n");
  247. mutex_unlock(&core->cmd_lock);
  248. }
  249. static int vpu_session_send_cmd(struct vpu_inst *inst, u32 id, void *data)
  250. {
  251. unsigned long key;
  252. int sync = false;
  253. int ret;
  254. if (inst->id < 0)
  255. return -EINVAL;
  256. ret = vpu_request_cmd(inst, id, data, &key, &sync);
  257. if (ret)
  258. goto exit;
  259. /* workaround for a firmware issue,
  260. * firmware should be waked up by start or configure command,
  261. * but there is a very small change that firmware failed to wakeup.
  262. * in such case, try to wakeup firmware again by sending a noop command
  263. */
  264. if (sync && (id == VPU_CMD_ID_CONFIGURE_CODEC || id == VPU_CMD_ID_START)) {
  265. if (sync_session_response(inst, key, VPU_TIMEOUT_WAKEUP, 1))
  266. vpu_core_keep_active(inst->core);
  267. else
  268. goto exit;
  269. }
  270. if (sync)
  271. ret = sync_session_response(inst, key, VPU_TIMEOUT, 0);
  272. exit:
  273. if (ret)
  274. dev_err(inst->dev, "[%d] send cmd %s fail\n", inst->id, vpu_id_name(id));
  275. return ret;
  276. }
  277. int vpu_session_configure_codec(struct vpu_inst *inst)
  278. {
  279. return vpu_session_send_cmd(inst, VPU_CMD_ID_CONFIGURE_CODEC, NULL);
  280. }
  281. int vpu_session_start(struct vpu_inst *inst)
  282. {
  283. vpu_trace(inst->dev, "[%d]\n", inst->id);
  284. return vpu_session_send_cmd(inst, VPU_CMD_ID_START, NULL);
  285. }
  286. int vpu_session_stop(struct vpu_inst *inst)
  287. {
  288. int ret;
  289. vpu_trace(inst->dev, "[%d]\n", inst->id);
  290. ret = vpu_session_send_cmd(inst, VPU_CMD_ID_STOP, NULL);
  291. /* workaround for a firmware bug,
  292. * if the next command is too close after stop cmd,
  293. * the firmware may enter wfi wrongly.
  294. */
  295. usleep_range(3000, 5000);
  296. return ret;
  297. }
  298. int vpu_session_encode_frame(struct vpu_inst *inst, s64 timestamp)
  299. {
  300. return vpu_session_send_cmd(inst, VPU_CMD_ID_FRAME_ENCODE, &timestamp);
  301. }
  302. int vpu_session_alloc_fs(struct vpu_inst *inst, struct vpu_fs_info *fs)
  303. {
  304. return vpu_session_send_cmd(inst, VPU_CMD_ID_FS_ALLOC, fs);
  305. }
  306. int vpu_session_release_fs(struct vpu_inst *inst, struct vpu_fs_info *fs)
  307. {
  308. return vpu_session_send_cmd(inst, VPU_CMD_ID_FS_RELEASE, fs);
  309. }
  310. int vpu_session_abort(struct vpu_inst *inst)
  311. {
  312. return vpu_session_send_cmd(inst, VPU_CMD_ID_ABORT, NULL);
  313. }
  314. int vpu_session_rst_buf(struct vpu_inst *inst)
  315. {
  316. return vpu_session_send_cmd(inst, VPU_CMD_ID_RST_BUF, NULL);
  317. }
  318. int vpu_session_fill_timestamp(struct vpu_inst *inst, struct vpu_ts_info *info)
  319. {
  320. return vpu_session_send_cmd(inst, VPU_CMD_ID_TIMESTAMP, info);
  321. }
  322. int vpu_session_update_parameters(struct vpu_inst *inst, void *arg)
  323. {
  324. if (inst->type & VPU_CORE_TYPE_DEC)
  325. vpu_iface_set_decode_params(inst, arg, 1);
  326. else
  327. vpu_iface_set_encode_params(inst, arg, 1);
  328. return vpu_session_send_cmd(inst, VPU_CMD_ID_UPDATE_PARAMETER, arg);
  329. }
  330. int vpu_session_debug(struct vpu_inst *inst)
  331. {
  332. return vpu_session_send_cmd(inst, VPU_CMD_ID_DEBUG, NULL);
  333. }
  334. int vpu_core_snapshot(struct vpu_core *core)
  335. {
  336. struct vpu_inst *inst;
  337. int ret;
  338. if (!core || list_empty(&core->instances))
  339. return 0;
  340. inst = list_first_entry(&core->instances, struct vpu_inst, list);
  341. reinit_completion(&core->cmp);
  342. ret = vpu_session_send_cmd(inst, VPU_CMD_ID_SNAPSHOT, NULL);
  343. if (ret)
  344. return ret;
  345. ret = wait_for_completion_timeout(&core->cmp, VPU_TIMEOUT);
  346. if (!ret) {
  347. dev_err(core->dev, "snapshot timeout\n");
  348. return -EINVAL;
  349. }
  350. return 0;
  351. }
  352. int vpu_core_sw_reset(struct vpu_core *core)
  353. {
  354. struct vpu_rpc_event pkt;
  355. int ret;
  356. memset(&pkt, 0, sizeof(pkt));
  357. vpu_iface_pack_cmd(core, &pkt, 0, VPU_CMD_ID_FIRM_RESET, NULL);
  358. reinit_completion(&core->cmp);
  359. mutex_lock(&core->cmd_lock);
  360. ret = vpu_cmd_send(core, &pkt);
  361. mutex_unlock(&core->cmd_lock);
  362. if (ret)
  363. return ret;
  364. ret = wait_for_completion_timeout(&core->cmp, VPU_TIMEOUT);
  365. if (!ret) {
  366. dev_err(core->dev, "sw reset timeout\n");
  367. return -EINVAL;
  368. }
  369. return 0;
  370. }