venc_vpu_if.c (7377B)
1// SPDX-License-Identifier: GPL-2.0 2/* 3 * Copyright (c) 2016 MediaTek Inc. 4 * Author: PoChun Lin <pochun.lin@mediatek.com> 5 */ 6 7#include "mtk_vcodec_drv.h" 8#include "mtk_vcodec_fw.h" 9#include "venc_ipi_msg.h" 10#include "venc_vpu_if.h" 11 12static void handle_enc_init_msg(struct venc_vpu_inst *vpu, const void *data) 13{ 14 const struct venc_vpu_ipi_msg_init *msg = data; 15 16 vpu->inst_addr = msg->vpu_inst_addr; 17 vpu->vsi = mtk_vcodec_fw_map_dm_addr(vpu->ctx->dev->fw_handler, 18 msg->vpu_inst_addr); 19 20 /* Firmware version field value is unspecified on MT8173. */ 21 if (mtk_vcodec_fw_get_type(vpu->ctx->dev->fw_handler) == VPU) 22 return; 23 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) { 28 case 1: 29 break; 30 default: 31 mtk_vcodec_err(vpu, "unhandled firmware version 0x%x\n", 32 msg->venc_abi_version); 33 vpu->failure = 1; 34 break; 35 } 36} 37 38static void handle_enc_encode_msg(struct venc_vpu_inst *vpu, const void *data) 39{ 40 const struct venc_vpu_ipi_msg_enc *msg = data; 41 42 vpu->state = msg->state; 43 vpu->bs_size = msg->bs_size; 44 vpu->is_key_frm = msg->is_key_frm; 45} 46 47static void vpu_enc_ipi_handler(void *data, unsigned int len, void *priv) 48{ 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; 52 53 mtk_vcodec_debug(vpu, "msg_id %x inst %p status %d", 54 msg->msg_id, vpu, msg->status); 55 56 vpu->signaled = 1; 57 vpu->failure = (msg->status != VENC_IPI_MSG_STATUS_OK); 58 if (vpu->failure) 59 goto failure; 60 61 switch (msg->msg_id) { 62 case VPU_IPIMSG_ENC_INIT_DONE: 63 handle_enc_init_msg(vpu, data); 64 break; 65 case VPU_IPIMSG_ENC_SET_PARAM_DONE: 66 break; 67 case VPU_IPIMSG_ENC_ENCODE_DONE: 68 handle_enc_encode_msg(vpu, data); 69 break; 70 case VPU_IPIMSG_ENC_DEINIT_DONE: 71 break; 72 default: 73 mtk_vcodec_err(vpu, "unknown msg id %x", msg->msg_id); 74 break; 75 } 76 77failure: 78 mtk_vcodec_debug_leave(vpu); 79} 80 81static int vpu_enc_send_msg(struct venc_vpu_inst *vpu, void *msg, 82 int len) 83{ 84 int status; 85 86 mtk_vcodec_debug_enter(vpu); 87 88 if (!vpu->ctx->dev->fw_handler) { 89 mtk_vcodec_err(vpu, "inst dev is NULL"); 90 return -EINVAL; 91 } 92 93 status = mtk_vcodec_fw_ipi_send(vpu->ctx->dev->fw_handler, vpu->id, msg, 94 len, 2000); 95 if (status) { 96 mtk_vcodec_err(vpu, "vpu_ipi_send msg_id %x len %d fail %d", 97 *(uint32_t *)msg, len, status); 98 return -EINVAL; 99 } 100 if (vpu->failure) 101 return -EINVAL; 102 103 mtk_vcodec_debug_leave(vpu); 104 105 return 0; 106} 107 108int vpu_enc_init(struct venc_vpu_inst *vpu) 109{ 110 int status; 111 struct venc_ap_ipi_msg_init out; 112 113 mtk_vcodec_debug_enter(vpu); 114 115 init_waitqueue_head(&vpu->wq_hd); 116 vpu->signaled = 0; 117 vpu->failure = 0; 118 119 status = mtk_vcodec_fw_ipi_register(vpu->ctx->dev->fw_handler, vpu->id, 120 vpu_enc_ipi_handler, "venc", NULL); 121 122 if (status) { 123 mtk_vcodec_err(vpu, "vpu_ipi_register fail %d", status); 124 return -EINVAL; 125 } 126 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"); 132 return -EINVAL; 133 } 134 135 mtk_vcodec_debug_leave(vpu); 136 137 return 0; 138} 139 140static unsigned int venc_enc_param_crop_right(struct venc_vpu_inst *vpu, 141 struct venc_enc_param *enc_prm) 142{ 143 unsigned int img_crop_right = enc_prm->buf_width - enc_prm->width; 144 145 return img_crop_right % 16; 146} 147 148static unsigned int venc_enc_param_crop_bottom(struct venc_enc_param *enc_prm) 149{ 150 return round_up(enc_prm->height, 16) - enc_prm->height; 151} 152 153static unsigned int venc_enc_param_num_mb(struct venc_enc_param *enc_prm) 154{ 155 return DIV_ROUND_UP(enc_prm->width, 16) * 156 DIV_ROUND_UP(enc_prm->height, 16); 157} 158 159int vpu_enc_set_param(struct venc_vpu_inst *vpu, 160 enum venc_set_param_type id, 161 struct venc_enc_param *enc_param) 162{ 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; 168 169 mtk_vcodec_debug(vpu, "id %d ->", id); 170 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; 175 switch (id) { 176 case VENC_SET_PARAM_ENC: 177 if (is_ext) { 178 out.base.data_item = 3; 179 out.base.data[0] = 180 venc_enc_param_crop_right(vpu, enc_param); 181 out.base.data[1] = 182 venc_enc_param_crop_bottom(enc_param); 183 out.base.data[2] = venc_enc_param_num_mb(enc_param); 184 } else { 185 out.base.data_item = 0; 186 } 187 break; 188 case VENC_SET_PARAM_FORCE_INTRA: 189 out.base.data_item = 0; 190 break; 191 case VENC_SET_PARAM_ADJUST_BITRATE: 192 out.base.data_item = 1; 193 out.base.data[0] = enc_param->bitrate; 194 break; 195 case VENC_SET_PARAM_ADJUST_FRAMERATE: 196 out.base.data_item = 1; 197 out.base.data[0] = enc_param->frm_rate; 198 break; 199 case VENC_SET_PARAM_GOP_SIZE: 200 out.base.data_item = 1; 201 out.base.data[0] = enc_param->gop_size; 202 break; 203 case VENC_SET_PARAM_INTRA_PERIOD: 204 out.base.data_item = 1; 205 out.base.data[0] = enc_param->intra_period; 206 break; 207 case VENC_SET_PARAM_SKIP_FRAME: 208 out.base.data_item = 0; 209 break; 210 default: 211 mtk_vcodec_err(vpu, "id %d not supported", id); 212 return -EINVAL; 213 } 214 if (vpu_enc_send_msg(vpu, &out, msg_size)) { 215 mtk_vcodec_err(vpu, 216 "AP_IPIMSG_ENC_SET_PARAM %d fail", id); 217 return -EINVAL; 218 } 219 220 mtk_vcodec_debug(vpu, "id %d <-", id); 221 222 return 0; 223} 224 225int 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 struct venc_frame_info *frame_info) 229{ 230 const bool is_ext = MTK_ENC_CTX_IS_EXT(vpu->ctx); 231 size_t msg_size = is_ext ? 232 sizeof(struct venc_ap_ipi_msg_enc_ext) : 233 sizeof(struct venc_ap_ipi_msg_enc); 234 struct venc_ap_ipi_msg_enc_ext out; 235 236 mtk_vcodec_debug(vpu, "bs_mode %d ->", bs_mode); 237 238 memset(&out, 0, sizeof(out)); 239 out.base.msg_id = AP_IPIMSG_ENC_ENCODE; 240 out.base.vpu_inst_addr = vpu->inst_addr; 241 out.base.bs_mode = bs_mode; 242 if (frm_buf) { 243 if ((frm_buf->fb_addr[0].dma_addr % 16 == 0) && 244 (frm_buf->fb_addr[1].dma_addr % 16 == 0) && 245 (frm_buf->fb_addr[2].dma_addr % 16 == 0)) { 246 out.base.input_addr[0] = frm_buf->fb_addr[0].dma_addr; 247 out.base.input_addr[1] = frm_buf->fb_addr[1].dma_addr; 248 out.base.input_addr[2] = frm_buf->fb_addr[2].dma_addr; 249 } else { 250 mtk_vcodec_err(vpu, "dma_addr not align to 16"); 251 return -EINVAL; 252 } 253 } 254 if (bs_buf) { 255 out.base.bs_addr = bs_buf->dma_addr; 256 out.base.bs_size = bs_buf->size; 257 } 258 if (is_ext && frame_info) { 259 out.data_item = 3; 260 out.data[0] = frame_info->frm_count; 261 out.data[1] = frame_info->skip_frm_count; 262 out.data[2] = frame_info->frm_type; 263 } 264 if (vpu_enc_send_msg(vpu, &out, msg_size)) { 265 mtk_vcodec_err(vpu, "AP_IPIMSG_ENC_ENCODE %d fail", 266 bs_mode); 267 return -EINVAL; 268 } 269 270 mtk_vcodec_debug(vpu, "bs_mode %d state %d size %d key_frm %d <-", 271 bs_mode, vpu->state, vpu->bs_size, vpu->is_key_frm); 272 273 return 0; 274} 275 276int vpu_enc_deinit(struct venc_vpu_inst *vpu) 277{ 278 struct venc_ap_ipi_msg_deinit out; 279 280 mtk_vcodec_debug_enter(vpu); 281 282 memset(&out, 0, sizeof(out)); 283 out.msg_id = AP_IPIMSG_ENC_DEINIT; 284 out.vpu_inst_addr = vpu->inst_addr; 285 if (vpu_enc_send_msg(vpu, &out, sizeof(out))) { 286 mtk_vcodec_err(vpu, "AP_IPIMSG_ENC_DEINIT fail"); 287 return -EINVAL; 288 } 289 290 mtk_vcodec_debug_leave(vpu); 291 292 return 0; 293}