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