venc_vpu_if.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. uint32_t msg_id = *(uint32_t *)msg;
  69. mtk_vcodec_err(vpu, "vpu_ipi_send msg_id %x len %d fail %d",
  70. msg_id, len, status);
  71. return -EINVAL;
  72. }
  73. if (vpu->failure)
  74. return -EINVAL;
  75. mtk_vcodec_debug_leave(vpu);
  76. return 0;
  77. }
  78. int vpu_enc_init(struct venc_vpu_inst *vpu)
  79. {
  80. int status;
  81. struct venc_ap_ipi_msg_init out;
  82. mtk_vcodec_debug_enter(vpu);
  83. init_waitqueue_head(&vpu->wq_hd);
  84. vpu->signaled = 0;
  85. vpu->failure = 0;
  86. status = vpu_ipi_register(vpu->dev, vpu->id, vpu_enc_ipi_handler,
  87. NULL, NULL);
  88. if (status) {
  89. mtk_vcodec_err(vpu, "vpu_ipi_register fail %d", status);
  90. return -EINVAL;
  91. }
  92. memset(&out, 0, sizeof(out));
  93. out.msg_id = AP_IPIMSG_ENC_INIT;
  94. out.venc_inst = (unsigned long)vpu;
  95. if (vpu_enc_send_msg(vpu, &out, sizeof(out))) {
  96. mtk_vcodec_err(vpu, "AP_IPIMSG_ENC_INIT fail");
  97. return -EINVAL;
  98. }
  99. mtk_vcodec_debug_leave(vpu);
  100. return 0;
  101. }
  102. int vpu_enc_set_param(struct venc_vpu_inst *vpu,
  103. enum venc_set_param_type id,
  104. struct venc_enc_param *enc_param)
  105. {
  106. struct venc_ap_ipi_msg_set_param out;
  107. mtk_vcodec_debug(vpu, "id %d ->", id);
  108. memset(&out, 0, sizeof(out));
  109. out.msg_id = AP_IPIMSG_ENC_SET_PARAM;
  110. out.vpu_inst_addr = vpu->inst_addr;
  111. out.param_id = id;
  112. switch (id) {
  113. case VENC_SET_PARAM_ENC:
  114. out.data_item = 0;
  115. break;
  116. case VENC_SET_PARAM_FORCE_INTRA:
  117. out.data_item = 0;
  118. break;
  119. case VENC_SET_PARAM_ADJUST_BITRATE:
  120. out.data_item = 1;
  121. out.data[0] = enc_param->bitrate;
  122. break;
  123. case VENC_SET_PARAM_ADJUST_FRAMERATE:
  124. out.data_item = 1;
  125. out.data[0] = enc_param->frm_rate;
  126. break;
  127. case VENC_SET_PARAM_GOP_SIZE:
  128. out.data_item = 1;
  129. out.data[0] = enc_param->gop_size;
  130. break;
  131. case VENC_SET_PARAM_INTRA_PERIOD:
  132. out.data_item = 1;
  133. out.data[0] = enc_param->intra_period;
  134. break;
  135. case VENC_SET_PARAM_SKIP_FRAME:
  136. out.data_item = 0;
  137. break;
  138. default:
  139. mtk_vcodec_err(vpu, "id %d not supported", id);
  140. return -EINVAL;
  141. }
  142. if (vpu_enc_send_msg(vpu, &out, sizeof(out))) {
  143. mtk_vcodec_err(vpu,
  144. "AP_IPIMSG_ENC_SET_PARAM %d fail", id);
  145. return -EINVAL;
  146. }
  147. mtk_vcodec_debug(vpu, "id %d <-", id);
  148. return 0;
  149. }
  150. int vpu_enc_encode(struct venc_vpu_inst *vpu, unsigned int bs_mode,
  151. struct venc_frm_buf *frm_buf,
  152. struct mtk_vcodec_mem *bs_buf,
  153. unsigned int *bs_size)
  154. {
  155. struct venc_ap_ipi_msg_enc out;
  156. mtk_vcodec_debug(vpu, "bs_mode %d ->", bs_mode);
  157. memset(&out, 0, sizeof(out));
  158. out.msg_id = AP_IPIMSG_ENC_ENCODE;
  159. out.vpu_inst_addr = vpu->inst_addr;
  160. out.bs_mode = bs_mode;
  161. if (frm_buf) {
  162. if ((frm_buf->fb_addr[0].dma_addr % 16 == 0) &&
  163. (frm_buf->fb_addr[1].dma_addr % 16 == 0) &&
  164. (frm_buf->fb_addr[2].dma_addr % 16 == 0)) {
  165. out.input_addr[0] = frm_buf->fb_addr[0].dma_addr;
  166. out.input_addr[1] = frm_buf->fb_addr[1].dma_addr;
  167. out.input_addr[2] = frm_buf->fb_addr[2].dma_addr;
  168. } else {
  169. mtk_vcodec_err(vpu, "dma_addr not align to 16");
  170. return -EINVAL;
  171. }
  172. }
  173. if (bs_buf) {
  174. out.bs_addr = bs_buf->dma_addr;
  175. out.bs_size = bs_buf->size;
  176. }
  177. if (vpu_enc_send_msg(vpu, &out, sizeof(out))) {
  178. mtk_vcodec_err(vpu, "AP_IPIMSG_ENC_ENCODE %d fail",
  179. bs_mode);
  180. return -EINVAL;
  181. }
  182. mtk_vcodec_debug(vpu, "bs_mode %d state %d size %d key_frm %d <-",
  183. bs_mode, vpu->state, vpu->bs_size, vpu->is_key_frm);
  184. return 0;
  185. }
  186. int vpu_enc_deinit(struct venc_vpu_inst *vpu)
  187. {
  188. struct venc_ap_ipi_msg_deinit out;
  189. mtk_vcodec_debug_enter(vpu);
  190. memset(&out, 0, sizeof(out));
  191. out.msg_id = AP_IPIMSG_ENC_DEINIT;
  192. out.vpu_inst_addr = vpu->inst_addr;
  193. if (vpu_enc_send_msg(vpu, &out, sizeof(out))) {
  194. mtk_vcodec_err(vpu, "AP_IPIMSG_ENC_DEINIT fail");
  195. return -EINVAL;
  196. }
  197. mtk_vcodec_debug_leave(vpu);
  198. return 0;
  199. }