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_drv.h"
8 #include "mtk_vcodec_fw.h"
9 #include "venc_ipi_msg.h"
10 #include "venc_vpu_if.h"
12 static void handle_enc_init_msg(struct venc_vpu_inst
*vpu
, const void *data
)
14 const struct venc_vpu_ipi_msg_init
*msg
= data
;
16 vpu
->inst_addr
= msg
->vpu_inst_addr
;
17 vpu
->vsi
= mtk_vcodec_fw_map_dm_addr(vpu
->ctx
->dev
->fw_handler
,
20 /* Firmware version field value is unspecified on MT8173. */
21 if (vpu
->ctx
->dev
->venc_pdata
->chip
== MTK_MT8173
)
24 /* Check firmware version. */
25 mtk_vcodec_debug(vpu
, "firmware version: 0x%x\n",
26 msg
->venc_abi_version
);
27 switch (msg
->venc_abi_version
) {
31 mtk_vcodec_err(vpu
, "unhandled firmware version 0x%x\n",
32 msg
->venc_abi_version
);
38 static void handle_enc_encode_msg(struct venc_vpu_inst
*vpu
, const void *data
)
40 const struct venc_vpu_ipi_msg_enc
*msg
= data
;
42 vpu
->state
= msg
->state
;
43 vpu
->bs_size
= msg
->bs_size
;
44 vpu
->is_key_frm
= msg
->is_key_frm
;
47 static void vpu_enc_ipi_handler(void *data
, unsigned int len
, void *priv
)
49 const struct venc_vpu_ipi_msg_common
*msg
= data
;
50 struct venc_vpu_inst
*vpu
=
51 (struct venc_vpu_inst
*)(unsigned long)msg
->venc_inst
;
53 mtk_vcodec_debug(vpu
, "msg_id %x inst %p status %d",
54 msg
->msg_id
, vpu
, msg
->status
);
57 vpu
->failure
= (msg
->status
!= VENC_IPI_MSG_STATUS_OK
);
61 switch (msg
->msg_id
) {
62 case VPU_IPIMSG_ENC_INIT_DONE
:
63 handle_enc_init_msg(vpu
, data
);
65 case VPU_IPIMSG_ENC_SET_PARAM_DONE
:
67 case VPU_IPIMSG_ENC_ENCODE_DONE
:
68 handle_enc_encode_msg(vpu
, data
);
70 case VPU_IPIMSG_ENC_DEINIT_DONE
:
73 mtk_vcodec_err(vpu
, "unknown msg id %x", msg
->msg_id
);
78 mtk_vcodec_debug_leave(vpu
);
81 static int vpu_enc_send_msg(struct venc_vpu_inst
*vpu
, void *msg
,
86 mtk_vcodec_debug_enter(vpu
);
88 if (!vpu
->ctx
->dev
->fw_handler
) {
89 mtk_vcodec_err(vpu
, "inst dev is NULL");
93 status
= mtk_vcodec_fw_ipi_send(vpu
->ctx
->dev
->fw_handler
, vpu
->id
, msg
,
96 mtk_vcodec_err(vpu
, "vpu_ipi_send msg_id %x len %d fail %d",
97 *(uint32_t *)msg
, len
, status
);
103 mtk_vcodec_debug_leave(vpu
);
108 int vpu_enc_init(struct venc_vpu_inst
*vpu
)
111 struct venc_ap_ipi_msg_init out
;
113 mtk_vcodec_debug_enter(vpu
);
115 init_waitqueue_head(&vpu
->wq_hd
);
119 status
= mtk_vcodec_fw_ipi_register(vpu
->ctx
->dev
->fw_handler
, vpu
->id
,
120 vpu_enc_ipi_handler
, "venc", NULL
);
123 mtk_vcodec_err(vpu
, "vpu_ipi_register fail %d", status
);
127 memset(&out
, 0, sizeof(out
));
128 out
.msg_id
= AP_IPIMSG_ENC_INIT
;
129 out
.venc_inst
= (unsigned long)vpu
;
130 if (vpu_enc_send_msg(vpu
, &out
, sizeof(out
))) {
131 mtk_vcodec_err(vpu
, "AP_IPIMSG_ENC_INIT fail");
135 mtk_vcodec_debug_leave(vpu
);
140 static unsigned int venc_enc_param_crop_right(struct venc_vpu_inst
*vpu
,
141 struct venc_enc_param
*enc_prm
)
143 unsigned int img_crop_right
= enc_prm
->buf_width
- enc_prm
->width
;
145 return img_crop_right
% 16;
148 static unsigned int venc_enc_param_crop_bottom(struct venc_enc_param
*enc_prm
)
150 return round_up(enc_prm
->height
, 16) - enc_prm
->height
;
153 static unsigned int venc_enc_param_num_mb(struct venc_enc_param
*enc_prm
)
155 return DIV_ROUND_UP(enc_prm
->width
, 16) *
156 DIV_ROUND_UP(enc_prm
->height
, 16);
159 int vpu_enc_set_param(struct venc_vpu_inst
*vpu
,
160 enum venc_set_param_type id
,
161 struct venc_enc_param
*enc_param
)
163 const bool is_ext
= MTK_ENC_CTX_IS_EXT(vpu
->ctx
);
164 size_t msg_size
= is_ext
?
165 sizeof(struct venc_ap_ipi_msg_set_param_ext
) :
166 sizeof(struct venc_ap_ipi_msg_set_param
);
167 struct venc_ap_ipi_msg_set_param_ext out
;
169 mtk_vcodec_debug(vpu
, "id %d ->", id
);
171 memset(&out
, 0, sizeof(out
));
172 out
.base
.msg_id
= AP_IPIMSG_ENC_SET_PARAM
;
173 out
.base
.vpu_inst_addr
= vpu
->inst_addr
;
174 out
.base
.param_id
= id
;
176 case VENC_SET_PARAM_ENC
:
178 out
.base
.data_item
= 3;
180 venc_enc_param_crop_right(vpu
, enc_param
);
182 venc_enc_param_crop_bottom(enc_param
);
183 out
.base
.data
[2] = venc_enc_param_num_mb(enc_param
);
185 out
.base
.data_item
= 0;
188 case VENC_SET_PARAM_FORCE_INTRA
:
189 out
.base
.data_item
= 0;
191 case VENC_SET_PARAM_ADJUST_BITRATE
:
192 out
.base
.data_item
= 1;
193 out
.base
.data
[0] = enc_param
->bitrate
;
195 case VENC_SET_PARAM_ADJUST_FRAMERATE
:
196 out
.base
.data_item
= 1;
197 out
.base
.data
[0] = enc_param
->frm_rate
;
199 case VENC_SET_PARAM_GOP_SIZE
:
200 out
.base
.data_item
= 1;
201 out
.base
.data
[0] = enc_param
->gop_size
;
203 case VENC_SET_PARAM_INTRA_PERIOD
:
204 out
.base
.data_item
= 1;
205 out
.base
.data
[0] = enc_param
->intra_period
;
207 case VENC_SET_PARAM_SKIP_FRAME
:
208 out
.base
.data_item
= 0;
211 mtk_vcodec_err(vpu
, "id %d not supported", id
);
214 if (vpu_enc_send_msg(vpu
, &out
, msg_size
)) {
216 "AP_IPIMSG_ENC_SET_PARAM %d fail", id
);
220 mtk_vcodec_debug(vpu
, "id %d <-", id
);
225 int vpu_enc_encode(struct venc_vpu_inst
*vpu
, unsigned int bs_mode
,
226 struct venc_frm_buf
*frm_buf
,
227 struct mtk_vcodec_mem
*bs_buf
,
228 unsigned int *bs_size
,
229 struct venc_frame_info
*frame_info
)
231 const bool is_ext
= MTK_ENC_CTX_IS_EXT(vpu
->ctx
);
232 size_t msg_size
= is_ext
?
233 sizeof(struct venc_ap_ipi_msg_enc_ext
) :
234 sizeof(struct venc_ap_ipi_msg_enc
);
235 struct venc_ap_ipi_msg_enc_ext out
;
237 mtk_vcodec_debug(vpu
, "bs_mode %d ->", bs_mode
);
239 memset(&out
, 0, sizeof(out
));
240 out
.base
.msg_id
= AP_IPIMSG_ENC_ENCODE
;
241 out
.base
.vpu_inst_addr
= vpu
->inst_addr
;
242 out
.base
.bs_mode
= bs_mode
;
244 if ((frm_buf
->fb_addr
[0].dma_addr
% 16 == 0) &&
245 (frm_buf
->fb_addr
[1].dma_addr
% 16 == 0) &&
246 (frm_buf
->fb_addr
[2].dma_addr
% 16 == 0)) {
247 out
.base
.input_addr
[0] = frm_buf
->fb_addr
[0].dma_addr
;
248 out
.base
.input_addr
[1] = frm_buf
->fb_addr
[1].dma_addr
;
249 out
.base
.input_addr
[2] = frm_buf
->fb_addr
[2].dma_addr
;
251 mtk_vcodec_err(vpu
, "dma_addr not align to 16");
256 out
.base
.bs_addr
= bs_buf
->dma_addr
;
257 out
.base
.bs_size
= bs_buf
->size
;
259 if (is_ext
&& frame_info
) {
261 out
.data
[0] = frame_info
->frm_count
;
262 out
.data
[1] = frame_info
->skip_frm_count
;
263 out
.data
[2] = frame_info
->frm_type
;
265 if (vpu_enc_send_msg(vpu
, &out
, msg_size
)) {
266 mtk_vcodec_err(vpu
, "AP_IPIMSG_ENC_ENCODE %d fail",
271 mtk_vcodec_debug(vpu
, "bs_mode %d state %d size %d key_frm %d <-",
272 bs_mode
, vpu
->state
, vpu
->bs_size
, vpu
->is_key_frm
);
277 int vpu_enc_deinit(struct venc_vpu_inst
*vpu
)
279 struct venc_ap_ipi_msg_deinit out
;
281 mtk_vcodec_debug_enter(vpu
);
283 memset(&out
, 0, sizeof(out
));
284 out
.msg_id
= AP_IPIMSG_ENC_DEINIT
;
285 out
.vpu_inst_addr
= vpu
->inst_addr
;
286 if (vpu_enc_send_msg(vpu
, &out
, sizeof(out
))) {
287 mtk_vcodec_err(vpu
, "AP_IPIMSG_ENC_DEINIT fail");
291 mtk_vcodec_debug_leave(vpu
);