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_drv.h"
8 #include "mtk_vcodec_util.h"
9 #include "vdec_ipi_msg.h"
10 #include "vdec_vpu_if.h"
11 #include "mtk_vcodec_fw.h"
12 
handle_init_ack_msg(const struct vdec_vpu_ipi_init_ack * msg)13 static void handle_init_ack_msg(const struct vdec_vpu_ipi_init_ack *msg)
14 {
15 	struct vdec_vpu_inst *vpu = (struct vdec_vpu_inst *)
16 					(unsigned long)msg->ap_inst_addr;
17 
18 	mtk_vcodec_debug(vpu, "+ ap_inst_addr = 0x%llx", msg->ap_inst_addr);
19 
20 	/* mapping VPU address to kernel virtual address */
21 	/* the content in vsi is initialized to 0 in VPU */
22 	vpu->vsi = mtk_vcodec_fw_map_dm_addr(vpu->ctx->dev->fw_handler,
23 					     msg->vpu_inst_addr);
24 	vpu->inst_addr = msg->vpu_inst_addr;
25 
26 	mtk_vcodec_debug(vpu, "- vpu_inst_addr = 0x%x", vpu->inst_addr);
27 
28 	/* Set default ABI version if dealing with unversioned firmware. */
29 	vpu->fw_abi_version = 0;
30 	/*
31 	 * Instance ID is only used if ABI version >= 2. Initialize it with
32 	 * garbage by default.
33 	 */
34 	vpu->inst_id = 0xdeadbeef;
35 
36 	/* Firmware version field does not exist on MT8173. */
37 	if (vpu->ctx->dev->vdec_pdata->chip == MTK_MT8173)
38 		return;
39 
40 	/* Check firmware version. */
41 	vpu->fw_abi_version = msg->vdec_abi_version;
42 	mtk_vcodec_debug(vpu, "firmware version 0x%x\n", vpu->fw_abi_version);
43 	switch (vpu->fw_abi_version) {
44 	case 1:
45 		break;
46 	case 2:
47 		vpu->inst_id = msg->inst_id;
48 		break;
49 	default:
50 		mtk_vcodec_err(vpu, "unhandled firmware version 0x%x\n",
51 			       vpu->fw_abi_version);
52 		vpu->failure = 1;
53 		break;
54 	}
55 }
56 
57 /*
58  * vpu_dec_ipi_handler - Handler for VPU ipi message.
59  *
60  * @data: ipi message
61  * @len : length of ipi message
62  * @priv: callback private data which is passed by decoder when register.
63  *
64  * This function runs in interrupt context and it means there's an IPI MSG
65  * from VPU.
66  */
vpu_dec_ipi_handler(void * data,unsigned int len,void * priv)67 static void vpu_dec_ipi_handler(void *data, unsigned int len, void *priv)
68 {
69 	const struct vdec_vpu_ipi_ack *msg = data;
70 	struct vdec_vpu_inst *vpu = (struct vdec_vpu_inst *)
71 					(unsigned long)msg->ap_inst_addr;
72 
73 	mtk_vcodec_debug(vpu, "+ id=%X", msg->msg_id);
74 
75 	vpu->failure = msg->status;
76 	vpu->signaled = 1;
77 
78 	if (msg->status == 0) {
79 		switch (msg->msg_id) {
80 		case VPU_IPIMSG_DEC_INIT_ACK:
81 			handle_init_ack_msg(data);
82 			break;
83 
84 		case VPU_IPIMSG_DEC_START_ACK:
85 		case VPU_IPIMSG_DEC_END_ACK:
86 		case VPU_IPIMSG_DEC_DEINIT_ACK:
87 		case VPU_IPIMSG_DEC_RESET_ACK:
88 			break;
89 
90 		default:
91 			mtk_vcodec_err(vpu, "invalid msg=%X", msg->msg_id);
92 			break;
93 		}
94 	}
95 
96 	mtk_vcodec_debug(vpu, "- id=%X", msg->msg_id);
97 }
98 
vcodec_vpu_send_msg(struct vdec_vpu_inst * vpu,void * msg,int len)99 static int vcodec_vpu_send_msg(struct vdec_vpu_inst *vpu, void *msg, int len)
100 {
101 	int err;
102 
103 	mtk_vcodec_debug(vpu, "id=%X", *(uint32_t *)msg);
104 
105 	vpu->failure = 0;
106 	vpu->signaled = 0;
107 
108 	err = mtk_vcodec_fw_ipi_send(vpu->ctx->dev->fw_handler, vpu->id, msg,
109 				     len, 2000);
110 	if (err) {
111 		mtk_vcodec_err(vpu, "send fail vpu_id=%d msg_id=%X status=%d",
112 			       vpu->id, *(uint32_t *)msg, err);
113 		return err;
114 	}
115 
116 	return vpu->failure;
117 }
118 
vcodec_send_ap_ipi(struct vdec_vpu_inst * vpu,unsigned int msg_id)119 static int vcodec_send_ap_ipi(struct vdec_vpu_inst *vpu, unsigned int msg_id)
120 {
121 	struct vdec_ap_ipi_cmd msg;
122 	int err = 0;
123 
124 	mtk_vcodec_debug(vpu, "+ id=%X", msg_id);
125 
126 	memset(&msg, 0, sizeof(msg));
127 	msg.msg_id = msg_id;
128 	if (vpu->fw_abi_version < 2)
129 		msg.vpu_inst_addr = vpu->inst_addr;
130 	else
131 		msg.inst_id = vpu->inst_id;
132 
133 	err = vcodec_vpu_send_msg(vpu, &msg, sizeof(msg));
134 	mtk_vcodec_debug(vpu, "- id=%X ret=%d", msg_id, err);
135 	return err;
136 }
137 
vpu_dec_init(struct vdec_vpu_inst * vpu)138 int vpu_dec_init(struct vdec_vpu_inst *vpu)
139 {
140 	struct vdec_ap_ipi_init msg;
141 	int err;
142 
143 	mtk_vcodec_debug_enter(vpu);
144 
145 	init_waitqueue_head(&vpu->wq);
146 	vpu->handler = vpu_dec_ipi_handler;
147 
148 	err = mtk_vcodec_fw_ipi_register(vpu->ctx->dev->fw_handler, vpu->id,
149 					 vpu->handler, "vdec", NULL);
150 	if (err != 0) {
151 		mtk_vcodec_err(vpu, "vpu_ipi_register fail status=%d", err);
152 		return err;
153 	}
154 
155 	memset(&msg, 0, sizeof(msg));
156 	msg.msg_id = AP_IPIMSG_DEC_INIT;
157 	msg.ap_inst_addr = (unsigned long)vpu;
158 
159 	mtk_vcodec_debug(vpu, "vdec_inst=%p", vpu);
160 
161 	err = vcodec_vpu_send_msg(vpu, (void *)&msg, sizeof(msg));
162 	mtk_vcodec_debug(vpu, "- ret=%d", err);
163 	return err;
164 }
165 
vpu_dec_start(struct vdec_vpu_inst * vpu,uint32_t * data,unsigned int len)166 int vpu_dec_start(struct vdec_vpu_inst *vpu, uint32_t *data, unsigned int len)
167 {
168 	struct vdec_ap_ipi_dec_start msg;
169 	int i;
170 	int err = 0;
171 
172 	mtk_vcodec_debug_enter(vpu);
173 
174 	if (len > ARRAY_SIZE(msg.data)) {
175 		mtk_vcodec_err(vpu, "invalid len = %d\n", len);
176 		return -EINVAL;
177 	}
178 
179 	memset(&msg, 0, sizeof(msg));
180 	msg.msg_id = AP_IPIMSG_DEC_START;
181 	if (vpu->fw_abi_version < 2)
182 		msg.vpu_inst_addr = vpu->inst_addr;
183 	else
184 		msg.inst_id = vpu->inst_id;
185 
186 	for (i = 0; i < len; i++)
187 		msg.data[i] = data[i];
188 
189 	err = vcodec_vpu_send_msg(vpu, (void *)&msg, sizeof(msg));
190 	mtk_vcodec_debug(vpu, "- ret=%d", err);
191 	return err;
192 }
193 
vpu_dec_end(struct vdec_vpu_inst * vpu)194 int vpu_dec_end(struct vdec_vpu_inst *vpu)
195 {
196 	return vcodec_send_ap_ipi(vpu, AP_IPIMSG_DEC_END);
197 }
198 
vpu_dec_deinit(struct vdec_vpu_inst * vpu)199 int vpu_dec_deinit(struct vdec_vpu_inst *vpu)
200 {
201 	return vcodec_send_ap_ipi(vpu, AP_IPIMSG_DEC_DEINIT);
202 }
203 
vpu_dec_reset(struct vdec_vpu_inst * vpu)204 int vpu_dec_reset(struct vdec_vpu_inst *vpu)
205 {
206 	return vcodec_send_ap_ipi(vpu, AP_IPIMSG_DEC_RESET);
207 }
208