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",
145 mtk_venc_err(vpu
->ctx
, "vpu_ipi_register fail %d", status
);
149 memset(&out
, 0, sizeof(out
));
150 out
.msg_id
= AP_IPIMSG_ENC_INIT
;
151 out
.venc_inst
= (unsigned long)vpu
;
152 if (vpu_enc_send_msg(vpu
, &out
, sizeof(out
))) {
153 mtk_venc_err(vpu
->ctx
, "AP_IPIMSG_ENC_INIT fail");
157 if (IS_ERR_OR_NULL(vpu
->vsi
)) {
158 mtk_venc_err(vpu
->ctx
, "invalid venc vsi");
165 static unsigned int venc_enc_param_crop_right(struct venc_vpu_inst
*vpu
,
166 struct venc_enc_param
*enc_prm
)
168 unsigned int img_crop_right
= enc_prm
->buf_width
- enc_prm
->width
;
170 return img_crop_right
% 16;
173 static unsigned int venc_enc_param_crop_bottom(struct venc_enc_param
*enc_prm
)
175 return round_up(enc_prm
->height
, 16) - enc_prm
->height
;
178 static unsigned int venc_enc_param_num_mb(struct venc_enc_param
*enc_prm
)
180 return DIV_ROUND_UP(enc_prm
->width
, 16) *
181 DIV_ROUND_UP(enc_prm
->height
, 16);
184 int vpu_enc_set_param(struct venc_vpu_inst
*vpu
,
185 enum venc_set_param_type id
,
186 struct venc_enc_param
*enc_param
)
188 const bool is_ext
= MTK_ENC_CTX_IS_EXT(vpu
->ctx
);
189 size_t msg_size
= is_ext
?
190 sizeof(struct venc_ap_ipi_msg_set_param_ext
) :
191 sizeof(struct venc_ap_ipi_msg_set_param
);
192 struct venc_ap_ipi_msg_set_param_ext out
;
194 mtk_venc_debug(vpu
->ctx
, "id %d ->", id
);
196 memset(&out
, 0, sizeof(out
));
197 out
.base
.msg_id
= AP_IPIMSG_ENC_SET_PARAM
;
198 out
.base
.vpu_inst_addr
= vpu
->inst_addr
;
199 out
.base
.param_id
= id
;
201 case VENC_SET_PARAM_ENC
:
203 out
.base
.data_item
= 3;
205 venc_enc_param_crop_right(vpu
, enc_param
);
207 venc_enc_param_crop_bottom(enc_param
);
208 out
.base
.data
[2] = venc_enc_param_num_mb(enc_param
);
210 out
.base
.data_item
= 0;
213 case VENC_SET_PARAM_FORCE_INTRA
:
214 out
.base
.data_item
= 0;
216 case VENC_SET_PARAM_ADJUST_BITRATE
:
217 out
.base
.data_item
= 1;
218 out
.base
.data
[0] = enc_param
->bitrate
;
220 case VENC_SET_PARAM_ADJUST_FRAMERATE
:
221 out
.base
.data_item
= 1;
222 out
.base
.data
[0] = enc_param
->frm_rate
;
224 case VENC_SET_PARAM_GOP_SIZE
:
225 out
.base
.data_item
= 1;
226 out
.base
.data
[0] = enc_param
->gop_size
;
228 case VENC_SET_PARAM_INTRA_PERIOD
:
229 out
.base
.data_item
= 1;
230 out
.base
.data
[0] = enc_param
->intra_period
;
232 case VENC_SET_PARAM_SKIP_FRAME
:
233 out
.base
.data_item
= 0;
236 mtk_venc_err(vpu
->ctx
, "id %d not supported", id
);
239 if (vpu_enc_send_msg(vpu
, &out
, msg_size
)) {
240 mtk_venc_err(vpu
->ctx
, "AP_IPIMSG_ENC_SET_PARAM %d fail", id
);
244 mtk_venc_debug(vpu
->ctx
, "id %d <-", id
);
249 static int vpu_enc_encode_32bits(struct venc_vpu_inst
*vpu
,
250 unsigned int bs_mode
,
251 struct venc_frm_buf
*frm_buf
,
252 struct mtk_vcodec_mem
*bs_buf
,
253 struct venc_frame_info
*frame_info
)
255 const bool is_ext
= MTK_ENC_CTX_IS_EXT(vpu
->ctx
);
256 size_t msg_size
= is_ext
?
257 sizeof(struct venc_ap_ipi_msg_enc_ext
) :
258 sizeof(struct venc_ap_ipi_msg_enc
);
259 struct venc_ap_ipi_msg_enc_ext out
;
261 mtk_venc_debug(vpu
->ctx
, "bs_mode %d ->", bs_mode
);
263 memset(&out
, 0, sizeof(out
));
264 out
.base
.msg_id
= AP_IPIMSG_ENC_ENCODE
;
265 out
.base
.vpu_inst_addr
= vpu
->inst_addr
;
266 out
.base
.bs_mode
= bs_mode
;
268 if ((frm_buf
->fb_addr
[0].dma_addr
% 16 == 0) &&
269 (frm_buf
->fb_addr
[1].dma_addr
% 16 == 0) &&
270 (frm_buf
->fb_addr
[2].dma_addr
% 16 == 0)) {
271 out
.base
.input_addr
[0] = frm_buf
->fb_addr
[0].dma_addr
;
272 out
.base
.input_addr
[1] = frm_buf
->fb_addr
[1].dma_addr
;
273 out
.base
.input_addr
[2] = frm_buf
->fb_addr
[2].dma_addr
;
275 mtk_venc_err(vpu
->ctx
, "dma_addr not align to 16");
280 out
.base
.bs_addr
= bs_buf
->dma_addr
;
281 out
.base
.bs_size
= bs_buf
->size
;
283 if (is_ext
&& frame_info
) {
285 out
.data
[0] = frame_info
->frm_count
;
286 out
.data
[1] = frame_info
->skip_frm_count
;
287 out
.data
[2] = frame_info
->frm_type
;
289 if (vpu_enc_send_msg(vpu
, &out
, msg_size
)) {
290 mtk_venc_err(vpu
->ctx
, "AP_IPIMSG_ENC_ENCODE %d fail", bs_mode
);
297 static int vpu_enc_encode_34bits(struct venc_vpu_inst
*vpu
,
298 unsigned int bs_mode
,
299 struct venc_frm_buf
*frm_buf
,
300 struct mtk_vcodec_mem
*bs_buf
,
301 struct venc_frame_info
*frame_info
)
303 struct venc_ap_ipi_msg_enc_ext_34 out
;
304 size_t msg_size
= sizeof(struct venc_ap_ipi_msg_enc_ext_34
);
306 mtk_venc_debug(vpu
->ctx
, "bs_mode %d ->", bs_mode
);
308 memset(&out
, 0, sizeof(out
));
309 out
.msg_id
= AP_IPIMSG_ENC_ENCODE
;
310 out
.vpu_inst_addr
= vpu
->inst_addr
;
311 out
.bs_mode
= bs_mode
;
314 if ((frm_buf
->fb_addr
[0].dma_addr
% 16 == 0) &&
315 (frm_buf
->fb_addr
[1].dma_addr
% 16 == 0) &&
316 (frm_buf
->fb_addr
[2].dma_addr
% 16 == 0)) {
317 out
.input_addr
[0] = frm_buf
->fb_addr
[0].dma_addr
;
318 out
.input_addr
[1] = frm_buf
->fb_addr
[1].dma_addr
;
319 out
.input_addr
[2] = frm_buf
->fb_addr
[2].dma_addr
;
321 mtk_venc_err(vpu
->ctx
, "dma_addr not align to 16");
326 out
.bs_addr
= bs_buf
->dma_addr
;
327 out
.bs_size
= bs_buf
->size
;
331 out
.data
[0] = frame_info
->frm_count
;
332 out
.data
[1] = frame_info
->skip_frm_count
;
333 out
.data
[2] = frame_info
->frm_type
;
335 if (vpu_enc_send_msg(vpu
, &out
, msg_size
)) {
336 mtk_venc_err(vpu
->ctx
, "AP_IPIMSG_ENC_ENCODE %d fail", bs_mode
);
343 int vpu_enc_encode(struct venc_vpu_inst
*vpu
, unsigned int bs_mode
,
344 struct venc_frm_buf
*frm_buf
,
345 struct mtk_vcodec_mem
*bs_buf
,
346 struct venc_frame_info
*frame_info
)
350 if (MTK_ENC_IOVA_IS_34BIT(vpu
->ctx
))
351 ret
= vpu_enc_encode_34bits(vpu
, bs_mode
,
352 frm_buf
, bs_buf
, frame_info
);
354 ret
= vpu_enc_encode_32bits(vpu
, bs_mode
,
355 frm_buf
, bs_buf
, frame_info
);
360 mtk_venc_debug(vpu
->ctx
, "bs_mode %d state %d size %d key_frm %d <-",
361 bs_mode
, vpu
->state
, vpu
->bs_size
, vpu
->is_key_frm
);
366 int vpu_enc_deinit(struct venc_vpu_inst
*vpu
)
368 struct venc_ap_ipi_msg_deinit out
;
370 memset(&out
, 0, sizeof(out
));
371 out
.msg_id
= AP_IPIMSG_ENC_DEINIT
;
372 out
.vpu_inst_addr
= vpu
->inst_addr
;
373 if (vpu_enc_send_msg(vpu
, &out
, sizeof(out
))) {
374 mtk_venc_err(vpu
->ctx
, "AP_IPIMSG_ENC_DEINIT fail");