1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2016 MediaTek Inc.
4 * Author: PoChun Lin <pochun.lin@mediatek.com>
7 #include "mtk_vcodec_enc_drv.h"
8 #include "venc_ipi_msg.h"
9 #include "venc_vpu_if.h"
11 static void handle_enc_init_msg(struct venc_vpu_inst *vpu, const void *data)
13 const struct venc_vpu_ipi_msg_init *msg = data;
15 vpu->inst_addr = msg->vpu_inst_addr;
16 vpu->vsi = mtk_vcodec_fw_map_dm_addr(vpu->ctx->dev->fw_handler,
19 /* Firmware version field value is unspecified on MT8173. */
20 if (mtk_vcodec_fw_get_type(vpu->ctx->dev->fw_handler) == VPU)
23 /* Check firmware version. */
24 mtk_venc_debug(vpu->ctx, "firmware version: 0x%x\n", msg->venc_abi_version);
25 switch (msg->venc_abi_version) {
29 mtk_venc_err(vpu->ctx, "unhandled firmware version 0x%x\n",
30 msg->venc_abi_version);
36 static void handle_enc_encode_msg(struct venc_vpu_inst *vpu, const void *data)
38 const struct venc_vpu_ipi_msg_enc *msg = data;
40 vpu->state = msg->state;
41 vpu->bs_size = msg->bs_size;
42 vpu->is_key_frm = msg->is_key_frm;
45 static bool vpu_enc_check_ap_inst(struct mtk_vcodec_enc_dev *enc_dev, struct venc_vpu_inst *vpu)
47 struct mtk_vcodec_enc_ctx *ctx;
50 list_for_each_entry(ctx, &enc_dev->ctx_list, list) {
51 if (!IS_ERR_OR_NULL(ctx) && ctx->vpu_inst == vpu) {
60 static void vpu_enc_ipi_handler(void *data, unsigned int len, void *priv)
62 struct mtk_vcodec_enc_dev *enc_dev;
63 const struct venc_vpu_ipi_msg_common *msg = data;
64 struct venc_vpu_inst *vpu;
66 enc_dev = (struct mtk_vcodec_enc_dev *)priv;
67 vpu = (struct venc_vpu_inst *)(unsigned long)msg->venc_inst;
69 pr_err(MTK_DBG_V4L2_STR "venc_inst is NULL, did the SCP hang or crash?");
73 mtk_venc_debug(vpu->ctx, "msg_id %x inst %p status %d", msg->msg_id, vpu, msg->status);
74 if (!vpu_enc_check_ap_inst(enc_dev, vpu) || msg->msg_id < VPU_IPIMSG_ENC_INIT_DONE ||
75 msg->msg_id > VPU_IPIMSG_ENC_DEINIT_DONE) {
76 mtk_v4l2_venc_err(vpu->ctx, "venc msg id not correctly => 0x%x", msg->msg_id);
77 vpu->failure = -EINVAL;
81 vpu->failure = (msg->status != VENC_IPI_MSG_STATUS_OK);
83 mtk_venc_err(vpu->ctx, "vpu enc status failure %d", vpu->failure);
87 switch (msg->msg_id) {
88 case VPU_IPIMSG_ENC_INIT_DONE:
89 handle_enc_init_msg(vpu, data);
91 case VPU_IPIMSG_ENC_SET_PARAM_DONE:
93 case VPU_IPIMSG_ENC_ENCODE_DONE:
94 handle_enc_encode_msg(vpu, data);
96 case VPU_IPIMSG_ENC_DEINIT_DONE:
99 mtk_venc_err(vpu->ctx, "unknown msg id %x", msg->msg_id);
107 static int vpu_enc_send_msg(struct venc_vpu_inst *vpu, void *msg,
112 if (!vpu->ctx->dev->fw_handler) {
113 mtk_venc_err(vpu->ctx, "inst dev is NULL");
117 status = mtk_vcodec_fw_ipi_send(vpu->ctx->dev->fw_handler, vpu->id, msg,
120 mtk_venc_err(vpu->ctx, "vpu_ipi_send msg_id %x len %d fail %d",
121 *(uint32_t *)msg, len, status);
130 int vpu_enc_init(struct venc_vpu_inst *vpu)
133 struct venc_ap_ipi_msg_init out;
135 init_waitqueue_head(&vpu->wq_hd);
138 vpu->ctx->vpu_inst = vpu;
140 status = mtk_vcodec_fw_ipi_register(vpu->ctx->dev->fw_handler, vpu->id,
141 vpu_enc_ipi_handler, "venc", NULL);
144 mtk_venc_err(vpu->ctx, "vpu_ipi_register fail %d", status);
148 memset(&out, 0, sizeof(out));
149 out.msg_id = AP_IPIMSG_ENC_INIT;
150 out.venc_inst = (unsigned long)vpu;
151 if (vpu_enc_send_msg(vpu, &out, sizeof(out))) {
152 mtk_venc_err(vpu->ctx, "AP_IPIMSG_ENC_INIT fail");
159 static unsigned int venc_enc_param_crop_right(struct venc_vpu_inst *vpu,
160 struct venc_enc_param *enc_prm)
162 unsigned int img_crop_right = enc_prm->buf_width - enc_prm->width;
164 return img_crop_right % 16;
167 static unsigned int venc_enc_param_crop_bottom(struct venc_enc_param *enc_prm)
169 return round_up(enc_prm->height, 16) - enc_prm->height;
172 static unsigned int venc_enc_param_num_mb(struct venc_enc_param *enc_prm)
174 return DIV_ROUND_UP(enc_prm->width, 16) *
175 DIV_ROUND_UP(enc_prm->height, 16);
178 int vpu_enc_set_param(struct venc_vpu_inst *vpu,
179 enum venc_set_param_type id,
180 struct venc_enc_param *enc_param)
182 const bool is_ext = MTK_ENC_CTX_IS_EXT(vpu->ctx);
183 size_t msg_size = is_ext ?
184 sizeof(struct venc_ap_ipi_msg_set_param_ext) :
185 sizeof(struct venc_ap_ipi_msg_set_param);
186 struct venc_ap_ipi_msg_set_param_ext out;
188 mtk_venc_debug(vpu->ctx, "id %d ->", id);
190 memset(&out, 0, sizeof(out));
191 out.base.msg_id = AP_IPIMSG_ENC_SET_PARAM;
192 out.base.vpu_inst_addr = vpu->inst_addr;
193 out.base.param_id = id;
195 case VENC_SET_PARAM_ENC:
197 out.base.data_item = 3;
199 venc_enc_param_crop_right(vpu, enc_param);
201 venc_enc_param_crop_bottom(enc_param);
202 out.base.data[2] = venc_enc_param_num_mb(enc_param);
204 out.base.data_item = 0;
207 case VENC_SET_PARAM_FORCE_INTRA:
208 out.base.data_item = 0;
210 case VENC_SET_PARAM_ADJUST_BITRATE:
211 out.base.data_item = 1;
212 out.base.data[0] = enc_param->bitrate;
214 case VENC_SET_PARAM_ADJUST_FRAMERATE:
215 out.base.data_item = 1;
216 out.base.data[0] = enc_param->frm_rate;
218 case VENC_SET_PARAM_GOP_SIZE:
219 out.base.data_item = 1;
220 out.base.data[0] = enc_param->gop_size;
222 case VENC_SET_PARAM_INTRA_PERIOD:
223 out.base.data_item = 1;
224 out.base.data[0] = enc_param->intra_period;
226 case VENC_SET_PARAM_SKIP_FRAME:
227 out.base.data_item = 0;
230 mtk_venc_err(vpu->ctx, "id %d not supported", id);
233 if (vpu_enc_send_msg(vpu, &out, msg_size)) {
234 mtk_venc_err(vpu->ctx, "AP_IPIMSG_ENC_SET_PARAM %d fail", id);
238 mtk_venc_debug(vpu->ctx, "id %d <-", id);
243 static int vpu_enc_encode_32bits(struct venc_vpu_inst *vpu,
244 unsigned int bs_mode,
245 struct venc_frm_buf *frm_buf,
246 struct mtk_vcodec_mem *bs_buf,
247 struct venc_frame_info *frame_info)
249 const bool is_ext = MTK_ENC_CTX_IS_EXT(vpu->ctx);
250 size_t msg_size = is_ext ?
251 sizeof(struct venc_ap_ipi_msg_enc_ext) :
252 sizeof(struct venc_ap_ipi_msg_enc);
253 struct venc_ap_ipi_msg_enc_ext out;
255 mtk_venc_debug(vpu->ctx, "bs_mode %d ->", bs_mode);
257 memset(&out, 0, sizeof(out));
258 out.base.msg_id = AP_IPIMSG_ENC_ENCODE;
259 out.base.vpu_inst_addr = vpu->inst_addr;
260 out.base.bs_mode = bs_mode;
262 if ((frm_buf->fb_addr[0].dma_addr % 16 == 0) &&
263 (frm_buf->fb_addr[1].dma_addr % 16 == 0) &&
264 (frm_buf->fb_addr[2].dma_addr % 16 == 0)) {
265 out.base.input_addr[0] = frm_buf->fb_addr[0].dma_addr;
266 out.base.input_addr[1] = frm_buf->fb_addr[1].dma_addr;
267 out.base.input_addr[2] = frm_buf->fb_addr[2].dma_addr;
269 mtk_venc_err(vpu->ctx, "dma_addr not align to 16");
274 out.base.bs_addr = bs_buf->dma_addr;
275 out.base.bs_size = bs_buf->size;
277 if (is_ext && frame_info) {
279 out.data[0] = frame_info->frm_count;
280 out.data[1] = frame_info->skip_frm_count;
281 out.data[2] = frame_info->frm_type;
283 if (vpu_enc_send_msg(vpu, &out, msg_size)) {
284 mtk_venc_err(vpu->ctx, "AP_IPIMSG_ENC_ENCODE %d fail", bs_mode);
291 static int vpu_enc_encode_34bits(struct venc_vpu_inst *vpu,
292 unsigned int bs_mode,
293 struct venc_frm_buf *frm_buf,
294 struct mtk_vcodec_mem *bs_buf,
295 struct venc_frame_info *frame_info)
297 struct venc_ap_ipi_msg_enc_ext_34 out;
298 size_t msg_size = sizeof(struct venc_ap_ipi_msg_enc_ext_34);
300 mtk_venc_debug(vpu->ctx, "bs_mode %d ->", bs_mode);
302 memset(&out, 0, sizeof(out));
303 out.msg_id = AP_IPIMSG_ENC_ENCODE;
304 out.vpu_inst_addr = vpu->inst_addr;
305 out.bs_mode = bs_mode;
308 if ((frm_buf->fb_addr[0].dma_addr % 16 == 0) &&
309 (frm_buf->fb_addr[1].dma_addr % 16 == 0) &&
310 (frm_buf->fb_addr[2].dma_addr % 16 == 0)) {
311 out.input_addr[0] = frm_buf->fb_addr[0].dma_addr;
312 out.input_addr[1] = frm_buf->fb_addr[1].dma_addr;
313 out.input_addr[2] = frm_buf->fb_addr[2].dma_addr;
315 mtk_venc_err(vpu->ctx, "dma_addr not align to 16");
320 out.bs_addr = bs_buf->dma_addr;
321 out.bs_size = bs_buf->size;
325 out.data[0] = frame_info->frm_count;
326 out.data[1] = frame_info->skip_frm_count;
327 out.data[2] = frame_info->frm_type;
329 if (vpu_enc_send_msg(vpu, &out, msg_size)) {
330 mtk_venc_err(vpu->ctx, "AP_IPIMSG_ENC_ENCODE %d fail", bs_mode);
337 int vpu_enc_encode(struct venc_vpu_inst *vpu, unsigned int bs_mode,
338 struct venc_frm_buf *frm_buf,
339 struct mtk_vcodec_mem *bs_buf,
340 struct venc_frame_info *frame_info)
344 if (MTK_ENC_IOVA_IS_34BIT(vpu->ctx))
345 ret = vpu_enc_encode_34bits(vpu, bs_mode,
346 frm_buf, bs_buf, frame_info);
348 ret = vpu_enc_encode_32bits(vpu, bs_mode,
349 frm_buf, bs_buf, frame_info);
354 mtk_venc_debug(vpu->ctx, "bs_mode %d state %d size %d key_frm %d <-",
355 bs_mode, vpu->state, vpu->bs_size, vpu->is_key_frm);
360 int vpu_enc_deinit(struct venc_vpu_inst *vpu)
362 struct venc_ap_ipi_msg_deinit out;
364 memset(&out, 0, sizeof(out));
365 out.msg_id = AP_IPIMSG_ENC_DEINIT;
366 out.vpu_inst_addr = vpu->inst_addr;
367 if (vpu_enc_send_msg(vpu, &out, sizeof(out))) {
368 mtk_venc_err(vpu->ctx, "AP_IPIMSG_ENC_DEINIT fail");