1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2016 MediaTek Inc.
4 * Author: PC Chen <pc.chen@mediatek.com>
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"
12 static void handle_init_ack_msg(const struct vdec_vpu_ipi_init_ack *msg)
14 struct vdec_vpu_inst *vpu = (struct vdec_vpu_inst *)
15 (unsigned long)msg->ap_inst_addr;
17 mtk_vdec_debug(vpu->ctx, "+ ap_inst_addr = 0x%llx", msg->ap_inst_addr);
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,
23 vpu->inst_addr = msg->vpu_inst_addr;
25 mtk_vdec_debug(vpu->ctx, "- vpu_inst_addr = 0x%x", vpu->inst_addr);
27 /* Set default ABI version if dealing with unversioned firmware. */
28 vpu->fw_abi_version = 0;
30 * Instance ID is only used if ABI version >= 2. Initialize it with
33 vpu->inst_id = 0xdeadbeef;
35 /* VPU firmware does not contain a version field. */
36 if (mtk_vcodec_fw_get_type(vpu->ctx->dev->fw_handler) == VPU)
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) {
46 vpu->inst_id = msg->inst_id;
49 mtk_vdec_err(vpu->ctx, "unhandled firmware version 0x%x\n", vpu->fw_abi_version);
55 static void handle_get_param_msg_ack(const struct vdec_vpu_ipi_get_param_ack *msg)
57 struct vdec_vpu_inst *vpu = (struct vdec_vpu_inst *)
58 (unsigned long)msg->ap_inst_addr;
60 mtk_vdec_debug(vpu->ctx, "+ ap_inst_addr = 0x%llx", msg->ap_inst_addr);
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];
69 mtk_vdec_err(vpu->ctx, "invalid get param type=%d", msg->param_type);
75 static bool vpu_dec_check_ap_inst(struct mtk_vcodec_dec_dev *dec_dev, struct vdec_vpu_inst *vpu)
77 struct mtk_vcodec_dec_ctx *ctx;
80 mutex_lock(&dec_dev->dev_ctx_lock);
81 list_for_each_entry(ctx, &dec_dev->ctx_list, list) {
82 if (!IS_ERR_OR_NULL(ctx) && ctx->vpu_inst == vpu) {
87 mutex_unlock(&dec_dev->dev_ctx_lock);
93 * vpu_dec_ipi_handler - Handler for VPU ipi message.
96 * @len : length of ipi message
97 * @priv: callback private data which is passed by decoder when register.
99 * This function runs in interrupt context and it means there's an IPI MSG
102 static void vpu_dec_ipi_handler(void *data, unsigned int len, void *priv)
104 struct mtk_vcodec_dec_dev *dec_dev;
105 const struct vdec_vpu_ipi_ack *msg = data;
106 struct vdec_vpu_inst *vpu;
108 dec_dev = (struct mtk_vcodec_dec_dev *)priv;
109 vpu = (struct vdec_vpu_inst *)(unsigned long)msg->ap_inst_addr;
111 pr_err(MTK_DBG_V4L2_STR "ap_inst_addr is NULL, did the SCP hang or crash?");
115 if (!vpu_dec_check_ap_inst(dec_dev, vpu) || msg->msg_id < VPU_IPIMSG_DEC_INIT_ACK ||
116 msg->msg_id > VPU_IPIMSG_DEC_GET_PARAM_ACK) {
117 mtk_v4l2_vdec_err(vpu->ctx, "vdec msg id not correctly => 0x%x", msg->msg_id);
118 vpu->failure = -EINVAL;
122 vpu->failure = msg->status;
123 if (msg->status != 0)
126 switch (msg->msg_id) {
127 case VPU_IPIMSG_DEC_INIT_ACK:
128 handle_init_ack_msg(data);
131 case VPU_IPIMSG_DEC_START_ACK:
132 case VPU_IPIMSG_DEC_END_ACK:
133 case VPU_IPIMSG_DEC_DEINIT_ACK:
134 case VPU_IPIMSG_DEC_RESET_ACK:
135 case VPU_IPIMSG_DEC_CORE_ACK:
136 case VPU_IPIMSG_DEC_CORE_END_ACK:
139 case VPU_IPIMSG_DEC_GET_PARAM_ACK:
140 handle_get_param_msg_ack(data);
143 mtk_vdec_err(vpu->ctx, "invalid msg=%X", msg->msg_id);
151 static int vcodec_vpu_send_msg(struct vdec_vpu_inst *vpu, void *msg, int len)
155 msgid = *(uint32_t *)msg;
156 mtk_vdec_debug(vpu->ctx, "id=%X", msgid);
161 if (vpu->ctx->dev->vdec_pdata->hw_arch == MTK_VDEC_LAT_SINGLE_CORE) {
162 if (msgid == AP_IPIMSG_DEC_CORE ||
163 msgid == AP_IPIMSG_DEC_CORE_END)
171 err = mtk_vcodec_fw_ipi_send(vpu->ctx->dev->fw_handler, id, msg,
174 mtk_vdec_err(vpu->ctx, "send fail vpu_id=%d msg_id=%X status=%d",
182 static int vcodec_send_ap_ipi(struct vdec_vpu_inst *vpu, unsigned int msg_id)
184 struct vdec_ap_ipi_cmd msg;
187 mtk_vdec_debug(vpu->ctx, "+ id=%X", msg_id);
189 memset(&msg, 0, sizeof(msg));
191 if (vpu->fw_abi_version < 2)
192 msg.vpu_inst_addr = vpu->inst_addr;
194 msg.inst_id = vpu->inst_id;
195 msg.codec_type = vpu->codec_type;
197 err = vcodec_vpu_send_msg(vpu, &msg, sizeof(msg));
198 mtk_vdec_debug(vpu->ctx, "- id=%X ret=%d", msg_id, err);
202 int vpu_dec_init(struct vdec_vpu_inst *vpu)
204 struct vdec_ap_ipi_init msg;
207 init_waitqueue_head(&vpu->wq);
208 vpu->handler = vpu_dec_ipi_handler;
209 vpu->ctx->vpu_inst = vpu;
211 err = mtk_vcodec_fw_ipi_register(vpu->ctx->dev->fw_handler, vpu->id,
212 vpu->handler, "vdec", vpu->ctx->dev);
214 mtk_vdec_err(vpu->ctx, "vpu_ipi_register fail status=%d", err);
218 if (vpu->ctx->dev->vdec_pdata->hw_arch == MTK_VDEC_LAT_SINGLE_CORE) {
219 err = mtk_vcodec_fw_ipi_register(vpu->ctx->dev->fw_handler,
220 vpu->core_id, vpu->handler,
221 "vdec", vpu->ctx->dev);
223 mtk_vdec_err(vpu->ctx, "vpu_ipi_register core fail status=%d", err);
228 memset(&msg, 0, sizeof(msg));
229 msg.msg_id = AP_IPIMSG_DEC_INIT;
230 msg.ap_inst_addr = (unsigned long)vpu;
231 msg.codec_type = vpu->codec_type;
233 mtk_vdec_debug(vpu->ctx, "vdec_inst=%p", vpu);
235 err = vcodec_vpu_send_msg(vpu, (void *)&msg, sizeof(msg));
236 mtk_vdec_debug(vpu->ctx, "- ret=%d", err);
240 int vpu_dec_start(struct vdec_vpu_inst *vpu, uint32_t *data, unsigned int len)
242 struct vdec_ap_ipi_dec_start msg;
246 if (len > ARRAY_SIZE(msg.data)) {
247 mtk_vdec_err(vpu->ctx, "invalid len = %d\n", len);
251 memset(&msg, 0, sizeof(msg));
252 msg.msg_id = AP_IPIMSG_DEC_START;
253 if (vpu->fw_abi_version < 2)
254 msg.vpu_inst_addr = vpu->inst_addr;
256 msg.inst_id = vpu->inst_id;
258 for (i = 0; i < len; i++)
259 msg.data[i] = data[i];
260 msg.codec_type = vpu->codec_type;
262 err = vcodec_vpu_send_msg(vpu, (void *)&msg, sizeof(msg));
263 mtk_vdec_debug(vpu->ctx, "- ret=%d", err);
267 int vpu_dec_get_param(struct vdec_vpu_inst *vpu, uint32_t *data,
268 unsigned int len, unsigned int param_type)
270 struct vdec_ap_ipi_get_param msg;
273 if (len > ARRAY_SIZE(msg.data)) {
274 mtk_vdec_err(vpu->ctx, "invalid len = %d\n", len);
278 memset(&msg, 0, sizeof(msg));
279 msg.msg_id = AP_IPIMSG_DEC_GET_PARAM;
280 msg.inst_id = vpu->inst_id;
281 memcpy(msg.data, data, sizeof(unsigned int) * len);
282 msg.param_type = param_type;
283 msg.codec_type = vpu->codec_type;
285 err = vcodec_vpu_send_msg(vpu, (void *)&msg, sizeof(msg));
286 mtk_vdec_debug(vpu->ctx, "- ret=%d", err);
290 int vpu_dec_core(struct vdec_vpu_inst *vpu)
292 return vcodec_send_ap_ipi(vpu, AP_IPIMSG_DEC_CORE);
295 int vpu_dec_end(struct vdec_vpu_inst *vpu)
297 return vcodec_send_ap_ipi(vpu, AP_IPIMSG_DEC_END);
300 int vpu_dec_core_end(struct vdec_vpu_inst *vpu)
302 return vcodec_send_ap_ipi(vpu, AP_IPIMSG_DEC_CORE_END);
305 int vpu_dec_deinit(struct vdec_vpu_inst *vpu)
307 return vcodec_send_ap_ipi(vpu, AP_IPIMSG_DEC_DEINIT);
310 int vpu_dec_reset(struct vdec_vpu_inst *vpu)
312 return vcodec_send_ap_ipi(vpu, AP_IPIMSG_DEC_RESET);