venc_vpu_if.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright (c) 2016 MediaTek Inc.
  3. * Author: PoChun Lin <pochun.lin@mediatek.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include "mtk_vpu.h"
  16. #include "venc_ipi_msg.h"
  17. #include "venc_vpu_if.h"
  18. static void handle_enc_init_msg(struct venc_vpu_inst *vpu, void *data)
  19. {
  20. struct venc_vpu_ipi_msg_init *msg = data;
  21. vpu->inst_addr = msg->vpu_inst_addr;
  22. vpu->vsi = vpu_mapping_dm_addr(vpu->dev, msg->vpu_inst_addr);
  23. }
  24. static void handle_enc_encode_msg(struct venc_vpu_inst *vpu, void *data)
  25. {
  26. struct venc_vpu_ipi_msg_enc *msg = data;
  27. vpu->state = msg->state;
  28. vpu->bs_size = msg->bs_size;
  29. vpu->is_key_frm = msg->is_key_frm;
  30. }
  31. static void vpu_enc_ipi_handler(void *data, unsigned int len, void *priv)
  32. {
  33. struct venc_vpu_ipi_msg_common *msg = data;
  34. struct venc_vpu_inst *vpu =
  35. (struct venc_vpu_inst *)(unsigned long)msg->venc_inst;
  36. mtk_vcodec_debug(vpu, "msg_id %x inst %p status %d",
  37. msg->msg_id, vpu, msg->status);
  38. switch (msg->msg_id) {
  39. case VPU_IPIMSG_ENC_INIT_DONE:
  40. handle_enc_init_msg(vpu, data);
  41. break;
  42. case VPU_IPIMSG_ENC_SET_PARAM_DONE:
  43. break;
  44. case VPU_IPIMSG_ENC_ENCODE_DONE:
  45. handle_enc_encode_msg(vpu, data);
  46. break;
  47. case VPU_IPIMSG_ENC_DEINIT_DONE:
  48. break;
  49. default:
  50. mtk_vcodec_err(vpu, "unknown msg id %x", msg->msg_id);
  51. break;
  52. }
  53. vpu->signaled = 1;
  54. vpu->failure = (msg->status != VENC_IPI_MSG_STATUS_OK);
  55. mtk_vcodec_debug_leave(vpu);
  56. }
  57. static int vpu_enc_send_msg(struct venc_vpu_inst *vpu, void *msg,
  58. int len)
  59. {
  60. int status;
  61. mtk_vcodec_debug_enter(vpu);
  62. if (!vpu->dev) {
  63. mtk_vcodec_err(vpu, "inst dev is NULL");
  64. return -EINVAL;
  65. }
  66. status = vpu_ipi_send(vpu->dev, vpu->id, msg, len);
  67. if (status) {
  68. mtk_vcodec_err(vpu, "vpu_ipi_send msg_id %x len %d fail %d",
  69. *(uint32_t *)msg, len, status);
  70. return -EINVAL;
  71. }
  72. if (vpu->failure)
  73. return -EINVAL;
  74. mtk_vcodec_debug_leave(vpu);
  75. return 0;
  76. }
  77. int vpu_enc_init(struct venc_vpu_inst *vpu)
  78. {
  79. int status;
  80. struct venc_ap_ipi_msg_init out;
  81. mtk_vcodec_debug_enter(vpu);
  82. init_waitqueue_head(&vpu->wq_hd);
  83. vpu->signaled = 0;
  84. vpu->failure = 0;
  85. status = vpu_ipi_register(vpu->dev, vpu->id, vpu_enc_ipi_handler,
  86. NULL, NULL);
  87. if (status) {
  88. mtk_vcodec_err(vpu, "vpu_ipi_register fail %d", status);
  89. return -EINVAL;
  90. }
  91. memset(&out, 0, sizeof(out));
  92. out.msg_id = AP_IPIMSG_ENC_INIT;
  93. out.venc_inst = (unsigned long)vpu;
  94. if (vpu_enc_send_msg(vpu, &out, sizeof(out))) {
  95. mtk_vcodec_err(vpu, "AP_IPIMSG_ENC_INIT fail");
  96. return -EINVAL;
  97. }
  98. mtk_vcodec_debug_leave(vpu);
  99. return 0;
  100. }
  101. int vpu_enc_set_param(struct venc_vpu_inst *vpu,
  102. enum venc_set_param_type id,
  103. struct venc_enc_param *enc_param)
  104. {
  105. struct venc_ap_ipi_msg_set_param out;
  106. mtk_vcodec_debug(vpu, "id %d ->", id);
  107. memset(&out, 0, sizeof(out));
  108. out.msg_id = AP_IPIMSG_ENC_SET_PARAM;
  109. out.vpu_inst_addr = vpu->inst_addr;
  110. out.param_id = id;
  111. switch (id) {
  112. case VENC_SET_PARAM_ENC:
  113. out.data_item = 0;
  114. break;
  115. case VENC_SET_PARAM_FORCE_INTRA:
  116. out.data_item = 0;
  117. break;
  118. case VENC_SET_PARAM_ADJUST_BITRATE:
  119. out.data_item = 1;
  120. out.data[0] = enc_param->bitrate;
  121. break;
  122. case VENC_SET_PARAM_ADJUST_FRAMERATE:
  123. out.data_item = 1;
  124. out.data[0] = enc_param->frm_rate;
  125. break;
  126. case VENC_SET_PARAM_GOP_SIZE:
  127. out.data_item = 1;
  128. out.data[0] = enc_param->gop_size;
  129. break;
  130. case VENC_SET_PARAM_INTRA_PERIOD:
  131. out.data_item = 1;
  132. out.data[0] = enc_param->intra_period;
  133. break;
  134. case VENC_SET_PARAM_SKIP_FRAME:
  135. out.data_item = 0;
  136. break;
  137. default:
  138. mtk_vcodec_err(vpu, "id %d not supported", id);
  139. return -EINVAL;
  140. }
  141. if (vpu_enc_send_msg(vpu, &out, sizeof(out))) {
  142. mtk_vcodec_err(vpu,
  143. "AP_IPIMSG_ENC_SET_PARAM %d fail", id);
  144. return -EINVAL;
  145. }
  146. mtk_vcodec_debug(vpu, "id %d <-", id);
  147. return 0;
  148. }
  149. int vpu_enc_encode(struct venc_vpu_inst *vpu, unsigned int bs_mode,
  150. struct venc_frm_buf *frm_buf,
  151. struct mtk_vcodec_mem *bs_buf,
  152. unsigned int *bs_size)
  153. {
  154. struct venc_ap_ipi_msg_enc out;
  155. mtk_vcodec_debug(vpu, "bs_mode %d ->", bs_mode);
  156. memset(&out, 0, sizeof(out));
  157. out.msg_id = AP_IPIMSG_ENC_ENCODE;
  158. out.vpu_inst_addr = vpu->inst_addr;
  159. out.bs_mode = bs_mode;
  160. if (frm_buf) {
  161. if ((frm_buf->fb_addr[0].dma_addr % 16 == 0) &&
  162. (frm_buf->fb_addr[1].dma_addr % 16 == 0) &&
  163. (frm_buf->fb_addr[2].dma_addr % 16 == 0)) {
  164. out.input_addr[0] = frm_buf->fb_addr[0].dma_addr;
  165. out.input_addr[1] = frm_buf->fb_addr[1].dma_addr;
  166. out.input_addr[2] = frm_buf->fb_addr[2].dma_addr;
  167. } else {
  168. mtk_vcodec_err(vpu, "dma_addr not align to 16");
  169. return -EINVAL;
  170. }
  171. }
  172. if (bs_buf) {
  173. out.bs_addr = bs_buf->dma_addr;
  174. out.bs_size = bs_buf->size;
  175. }
  176. if (vpu_enc_send_msg(vpu, &out, sizeof(out))) {
  177. mtk_vcodec_err(vpu, "AP_IPIMSG_ENC_ENCODE %d fail",
  178. bs_mode);
  179. return -EINVAL;
  180. }
  181. mtk_vcodec_debug(vpu, "bs_mode %d state %d size %d key_frm %d <-",
  182. bs_mode, vpu->state, vpu->bs_size, vpu->is_key_frm);
  183. return 0;
  184. }
  185. int vpu_enc_deinit(struct venc_vpu_inst *vpu)
  186. {
  187. struct venc_ap_ipi_msg_deinit out;
  188. mtk_vcodec_debug_enter(vpu);
  189. memset(&out, 0, sizeof(out));
  190. out.msg_id = AP_IPIMSG_ENC_DEINIT;
  191. out.vpu_inst_addr = vpu->inst_addr;
  192. if (vpu_enc_send_msg(vpu, &out, sizeof(out))) {
  193. mtk_vcodec_err(vpu, "AP_IPIMSG_ENC_DEINIT fail");
  194. return -EINVAL;
  195. }
  196. mtk_vcodec_debug_leave(vpu);
  197. return 0;
  198. }