1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2016 MediaTek Inc. 4 * Author: PC Chen <pc.chen@mediatek.com> 5 */ 6 7 #include "mtk_vcodec_dec_drv.h" 8 #include "vdec_drv_if.h" 9 #include "vdec_ipi_msg.h" 10 #include "vdec_vpu_if.h" 11 12 static void handle_init_ack_msg(const struct vdec_vpu_ipi_init_ack *msg) 13 { 14 struct vdec_vpu_inst *vpu = (struct vdec_vpu_inst *) 15 (unsigned long)msg->ap_inst_addr; 16 17 mtk_vdec_debug(vpu->ctx, "+ ap_inst_addr = 0x%llx", msg->ap_inst_addr); 18 19 /* mapping VPU address to kernel virtual address */ 20 /* the content in vsi is initialized to 0 in VPU */ 21 vpu->vsi = mtk_vcodec_fw_map_dm_addr(vpu->ctx->dev->fw_handler, 22 msg->vpu_inst_addr); 23 vpu->inst_addr = msg->vpu_inst_addr; 24 25 mtk_vdec_debug(vpu->ctx, "- vpu_inst_addr = 0x%x", vpu->inst_addr); 26 27 /* Set default ABI version if dealing with unversioned firmware. */ 28 vpu->fw_abi_version = 0; 29 /* 30 * Instance ID is only used if ABI version >= 2. Initialize it with 31 * garbage by default. 32 */ 33 vpu->inst_id = 0xdeadbeef; 34 35 /* VPU firmware does not contain a version field. */ 36 if (mtk_vcodec_fw_get_type(vpu->ctx->dev->fw_handler) == VPU) 37 return; 38 39 /* Check firmware version. */ 40 vpu->fw_abi_version = msg->vdec_abi_version; 41 mtk_vdec_debug(vpu->ctx, "firmware version 0x%x\n", vpu->fw_abi_version); 42 switch (vpu->fw_abi_version) { 43 case 1: 44 break; 45 case 2: 46 vpu->inst_id = msg->inst_id; 47 break; 48 default: 49 mtk_vdec_err(vpu->ctx, "unhandled firmware version 0x%x\n", vpu->fw_abi_version); 50 vpu->failure = 1; 51 break; 52 } 53 } 54 55 static void handle_get_param_msg_ack(const struct vdec_vpu_ipi_get_param_ack *msg) 56 { 57 struct vdec_vpu_inst *vpu = (struct vdec_vpu_inst *) 58 (unsigned long)msg->ap_inst_addr; 59 60 mtk_vdec_debug(vpu->ctx, "+ ap_inst_addr = 0x%llx", msg->ap_inst_addr); 61 62 /* param_type is enum vdec_get_param_type */ 63 switch (msg->param_type) { 64 case GET_PARAM_PIC_INFO: 65 vpu->fb_sz[0] = msg->data[0]; 66 vpu->fb_sz[1] = msg->data[1]; 67 break; 68 default: 69 mtk_vdec_err(vpu->ctx, "invalid get param type=%d", msg->param_type); 70 vpu->failure = 1; 71 break; 72 } 73 } 74 75 static bool vpu_dec_check_ap_inst(struct mtk_vcodec_dec_dev *dec_dev, struct vdec_vpu_inst *vpu) 76 { 77 struct mtk_vcodec_dec_ctx *ctx; 78 int ret = false; 79 80 list_for_each_entry(ctx, &dec_dev->ctx_list, list) { 81 if (!IS_ERR_OR_NULL(ctx) && ctx->vpu_inst == vpu) { 82 ret = true; 83 break; 84 } 85 } 86 87 return ret; 88 } 89 90 /* 91 * vpu_dec_ipi_handler - Handler for VPU ipi message. 92 * 93 * @data: ipi message 94 * @len : length of ipi message 95 * @priv: callback private data which is passed by decoder when register. 96 * 97 * This function runs in interrupt context and it means there's an IPI MSG 98 * from VPU. 99 */ 100 static void vpu_dec_ipi_handler(void *data, unsigned int len, void *priv) 101 { 102 struct mtk_vcodec_dec_dev *dec_dev; 103 const struct vdec_vpu_ipi_ack *msg = data; 104 struct vdec_vpu_inst *vpu; 105 106 dec_dev = (struct mtk_vcodec_dec_dev *)priv; 107 vpu = (struct vdec_vpu_inst *)(unsigned long)msg->ap_inst_addr; 108 if (!priv || !vpu) { 109 pr_err(MTK_DBG_V4L2_STR "ap_inst_addr is NULL, did the SCP hang or crash?"); 110 return; 111 } 112 113 if (!vpu_dec_check_ap_inst(dec_dev, vpu) || msg->msg_id < VPU_IPIMSG_DEC_INIT_ACK || 114 msg->msg_id > VPU_IPIMSG_DEC_GET_PARAM_ACK) { 115 mtk_v4l2_vdec_err(vpu->ctx, "vdec msg id not correctly => 0x%x", msg->msg_id); 116 vpu->failure = -EINVAL; 117 goto error; 118 } 119 120 vpu->failure = msg->status; 121 if (msg->status != 0) 122 goto error; 123 124 switch (msg->msg_id) { 125 case VPU_IPIMSG_DEC_INIT_ACK: 126 handle_init_ack_msg(data); 127 break; 128 129 case VPU_IPIMSG_DEC_START_ACK: 130 case VPU_IPIMSG_DEC_END_ACK: 131 case VPU_IPIMSG_DEC_DEINIT_ACK: 132 case VPU_IPIMSG_DEC_RESET_ACK: 133 case VPU_IPIMSG_DEC_CORE_ACK: 134 case VPU_IPIMSG_DEC_CORE_END_ACK: 135 break; 136 137 case VPU_IPIMSG_DEC_GET_PARAM_ACK: 138 handle_get_param_msg_ack(data); 139 break; 140 default: 141 mtk_vdec_err(vpu->ctx, "invalid msg=%X", msg->msg_id); 142 break; 143 } 144 145 error: 146 vpu->signaled = 1; 147 } 148 149 static int vcodec_vpu_send_msg(struct vdec_vpu_inst *vpu, void *msg, int len) 150 { 151 int err, id, msgid; 152 153 msgid = *(uint32_t *)msg; 154 mtk_vdec_debug(vpu->ctx, "id=%X", msgid); 155 156 vpu->failure = 0; 157 vpu->signaled = 0; 158 159 if (vpu->ctx->dev->vdec_pdata->hw_arch == MTK_VDEC_LAT_SINGLE_CORE) { 160 if (msgid == AP_IPIMSG_DEC_CORE || 161 msgid == AP_IPIMSG_DEC_CORE_END) 162 id = vpu->core_id; 163 else 164 id = vpu->id; 165 } else { 166 id = vpu->id; 167 } 168 169 err = mtk_vcodec_fw_ipi_send(vpu->ctx->dev->fw_handler, id, msg, 170 len, 2000); 171 if (err) { 172 mtk_vdec_err(vpu->ctx, "send fail vpu_id=%d msg_id=%X status=%d", 173 id, msgid, err); 174 return err; 175 } 176 177 return vpu->failure; 178 } 179 180 static int vcodec_send_ap_ipi(struct vdec_vpu_inst *vpu, unsigned int msg_id) 181 { 182 struct vdec_ap_ipi_cmd msg; 183 int err = 0; 184 185 mtk_vdec_debug(vpu->ctx, "+ id=%X", msg_id); 186 187 memset(&msg, 0, sizeof(msg)); 188 msg.msg_id = msg_id; 189 if (vpu->fw_abi_version < 2) 190 msg.vpu_inst_addr = vpu->inst_addr; 191 else 192 msg.inst_id = vpu->inst_id; 193 msg.codec_type = vpu->codec_type; 194 195 err = vcodec_vpu_send_msg(vpu, &msg, sizeof(msg)); 196 mtk_vdec_debug(vpu->ctx, "- id=%X ret=%d", msg_id, err); 197 return err; 198 } 199 200 int vpu_dec_init(struct vdec_vpu_inst *vpu) 201 { 202 struct vdec_ap_ipi_init msg; 203 int err; 204 205 init_waitqueue_head(&vpu->wq); 206 vpu->handler = vpu_dec_ipi_handler; 207 vpu->ctx->vpu_inst = vpu; 208 209 err = mtk_vcodec_fw_ipi_register(vpu->ctx->dev->fw_handler, vpu->id, 210 vpu->handler, "vdec", vpu->ctx->dev); 211 if (err) { 212 mtk_vdec_err(vpu->ctx, "vpu_ipi_register fail status=%d", err); 213 return err; 214 } 215 216 if (vpu->ctx->dev->vdec_pdata->hw_arch == MTK_VDEC_LAT_SINGLE_CORE) { 217 err = mtk_vcodec_fw_ipi_register(vpu->ctx->dev->fw_handler, 218 vpu->core_id, vpu->handler, 219 "vdec", vpu->ctx->dev); 220 if (err) { 221 mtk_vdec_err(vpu->ctx, "vpu_ipi_register core fail status=%d", err); 222 return err; 223 } 224 } 225 226 memset(&msg, 0, sizeof(msg)); 227 msg.msg_id = AP_IPIMSG_DEC_INIT; 228 msg.ap_inst_addr = (unsigned long)vpu; 229 msg.codec_type = vpu->codec_type; 230 231 mtk_vdec_debug(vpu->ctx, "vdec_inst=%p", vpu); 232 233 err = vcodec_vpu_send_msg(vpu, (void *)&msg, sizeof(msg)); 234 mtk_vdec_debug(vpu->ctx, "- ret=%d", err); 235 return err; 236 } 237 238 int vpu_dec_start(struct vdec_vpu_inst *vpu, uint32_t *data, unsigned int len) 239 { 240 struct vdec_ap_ipi_dec_start msg; 241 int i; 242 int err = 0; 243 244 if (len > ARRAY_SIZE(msg.data)) { 245 mtk_vdec_err(vpu->ctx, "invalid len = %d\n", len); 246 return -EINVAL; 247 } 248 249 memset(&msg, 0, sizeof(msg)); 250 msg.msg_id = AP_IPIMSG_DEC_START; 251 if (vpu->fw_abi_version < 2) 252 msg.vpu_inst_addr = vpu->inst_addr; 253 else 254 msg.inst_id = vpu->inst_id; 255 256 for (i = 0; i < len; i++) 257 msg.data[i] = data[i]; 258 msg.codec_type = vpu->codec_type; 259 260 err = vcodec_vpu_send_msg(vpu, (void *)&msg, sizeof(msg)); 261 mtk_vdec_debug(vpu->ctx, "- ret=%d", err); 262 return err; 263 } 264 265 int vpu_dec_get_param(struct vdec_vpu_inst *vpu, uint32_t *data, 266 unsigned int len, unsigned int param_type) 267 { 268 struct vdec_ap_ipi_get_param msg; 269 int err; 270 271 if (len > ARRAY_SIZE(msg.data)) { 272 mtk_vdec_err(vpu->ctx, "invalid len = %d\n", len); 273 return -EINVAL; 274 } 275 276 memset(&msg, 0, sizeof(msg)); 277 msg.msg_id = AP_IPIMSG_DEC_GET_PARAM; 278 msg.inst_id = vpu->inst_id; 279 memcpy(msg.data, data, sizeof(unsigned int) * len); 280 msg.param_type = param_type; 281 msg.codec_type = vpu->codec_type; 282 283 err = vcodec_vpu_send_msg(vpu, (void *)&msg, sizeof(msg)); 284 mtk_vdec_debug(vpu->ctx, "- ret=%d", err); 285 return err; 286 } 287 288 int vpu_dec_core(struct vdec_vpu_inst *vpu) 289 { 290 return vcodec_send_ap_ipi(vpu, AP_IPIMSG_DEC_CORE); 291 } 292 293 int vpu_dec_end(struct vdec_vpu_inst *vpu) 294 { 295 return vcodec_send_ap_ipi(vpu, AP_IPIMSG_DEC_END); 296 } 297 298 int vpu_dec_core_end(struct vdec_vpu_inst *vpu) 299 { 300 return vcodec_send_ap_ipi(vpu, AP_IPIMSG_DEC_CORE_END); 301 } 302 303 int vpu_dec_deinit(struct vdec_vpu_inst *vpu) 304 { 305 return vcodec_send_ap_ipi(vpu, AP_IPIMSG_DEC_DEINIT); 306 } 307 308 int vpu_dec_reset(struct vdec_vpu_inst *vpu) 309 { 310 return vcodec_send_ap_ipi(vpu, AP_IPIMSG_DEC_RESET); 311 } 312