File Index
Files and directories
Sign in
FILE

vdec_vpu_if.c

File preview with syntax highlighting.

linux-6.19.14/drivers/media/platform/mediatek/vcodec/decoder/vdec_vpu_if.c C
  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
 12static 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
 55static 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
 75static 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	unsigned long flags;
 79	int ret = false;
 80
 81	spin_lock_irqsave(&dec_dev->dev_ctx_lock, flags);
 82	list_for_each_entry(ctx, &dec_dev->ctx_list, list) {
 83		if (!IS_ERR_OR_NULL(ctx) && ctx->vpu_inst == vpu) {
 84			ret = true;
 85			break;
 86		}
 87	}
 88	spin_unlock_irqrestore(&dec_dev->dev_ctx_lock, flags);
 89
 90	return ret;
 91}
 92
 93/*
 94 * vpu_dec_ipi_handler - Handler for VPU ipi message.
 95 *
 96 * @data: ipi message
 97 * @len : length of ipi message
 98 * @priv: callback private data which is passed by decoder when register.
 99 *
100 * This function runs in interrupt context and it means there's an IPI MSG
101 * from VPU.
102 */
103static void vpu_dec_ipi_handler(void *data, unsigned int len, void *priv)
104{
105	struct mtk_vcodec_dec_dev *dec_dev;
106	const struct vdec_vpu_ipi_ack *msg = data;
107	struct vdec_vpu_inst *vpu;
108
109	dec_dev = (struct mtk_vcodec_dec_dev *)priv;
110	vpu = (struct vdec_vpu_inst *)(unsigned long)msg->ap_inst_addr;
111	if (!priv || !vpu) {
112		pr_err(MTK_DBG_V4L2_STR "ap_inst_addr is NULL, did the SCP hang or crash?");
113		return;
114	}
115
116	if (!vpu_dec_check_ap_inst(dec_dev, vpu) || msg->msg_id < VPU_IPIMSG_DEC_INIT_ACK ||
117	    msg->msg_id > VPU_IPIMSG_DEC_GET_PARAM_ACK) {
118		mtk_v4l2_vdec_err(vpu->ctx, "vdec msg id not correctly => 0x%x", msg->msg_id);
119		vpu->failure = -EINVAL;
120		goto error;
121	}
122
123	vpu->failure = msg->status;
124	if (msg->status != 0)
125		goto error;
126
127	switch (msg->msg_id) {
128	case VPU_IPIMSG_DEC_INIT_ACK:
129		handle_init_ack_msg(data);
130		break;
131
132	case VPU_IPIMSG_DEC_START_ACK:
133	case VPU_IPIMSG_DEC_END_ACK:
134	case VPU_IPIMSG_DEC_DEINIT_ACK:
135	case VPU_IPIMSG_DEC_RESET_ACK:
136	case VPU_IPIMSG_DEC_CORE_ACK:
137	case VPU_IPIMSG_DEC_CORE_END_ACK:
138		break;
139
140	case VPU_IPIMSG_DEC_GET_PARAM_ACK:
141		handle_get_param_msg_ack(data);
142		break;
143	default:
144		mtk_vdec_err(vpu->ctx, "invalid msg=%X", msg->msg_id);
145		break;
146	}
147
148error:
149	vpu->signaled = 1;
150}
151
152static int vcodec_vpu_send_msg(struct vdec_vpu_inst *vpu, void *msg, int len)
153{
154	int err, id, msgid;
155
156	msgid = *(uint32_t *)msg;
157	mtk_vdec_debug(vpu->ctx, "id=%X", msgid);
158
159	vpu->failure = 0;
160	vpu->signaled = 0;
161
162	if (vpu->ctx->dev->vdec_pdata->hw_arch == MTK_VDEC_LAT_SINGLE_CORE) {
163		if (msgid == AP_IPIMSG_DEC_CORE ||
164		    msgid == AP_IPIMSG_DEC_CORE_END)
165			id = vpu->core_id;
166		else
167			id = vpu->id;
168	} else {
169		id = vpu->id;
170	}
171
172	err = mtk_vcodec_fw_ipi_send(vpu->ctx->dev->fw_handler, id, msg,
173				     len, 2000);
174	if (err) {
175		mtk_vdec_err(vpu->ctx, "send fail vpu_id=%d msg_id=%X status=%d",
176			     id, msgid, err);
177		return err;
178	}
179
180	return vpu->failure;
181}
182
183static int vcodec_send_ap_ipi(struct vdec_vpu_inst *vpu, unsigned int msg_id)
184{
185	struct vdec_ap_ipi_cmd msg;
186	int err = 0;
187
188	mtk_vdec_debug(vpu->ctx, "+ id=%X", msg_id);
189
190	memset(&msg, 0, sizeof(msg));
191	msg.msg_id = msg_id;
192	if (vpu->fw_abi_version < 2)
193		msg.vpu_inst_addr = vpu->inst_addr;
194	else
195		msg.inst_id = vpu->inst_id;
196	msg.codec_type = vpu->codec_type;
197
198	err = vcodec_vpu_send_msg(vpu, &msg, sizeof(msg));
199	mtk_vdec_debug(vpu->ctx, "- id=%X ret=%d", msg_id, err);
200	return err;
201}
202
203int vpu_dec_init(struct vdec_vpu_inst *vpu)
204{
205	struct vdec_ap_ipi_init msg;
206	int err;
207
208	init_waitqueue_head(&vpu->wq);
209	vpu->handler = vpu_dec_ipi_handler;
210	vpu->ctx->vpu_inst = vpu;
211
212	err = mtk_vcodec_fw_ipi_register(vpu->ctx->dev->fw_handler, vpu->id,
213					 vpu->handler, "vdec", vpu->ctx->dev);
214	if (err) {
215		mtk_vdec_err(vpu->ctx, "vpu_ipi_register fail status=%d", err);
216		return err;
217	}
218
219	if (vpu->ctx->dev->vdec_pdata->hw_arch == MTK_VDEC_LAT_SINGLE_CORE) {
220		err = mtk_vcodec_fw_ipi_register(vpu->ctx->dev->fw_handler,
221						 vpu->core_id, vpu->handler,
222						 "vdec", vpu->ctx->dev);
223		if (err) {
224			mtk_vdec_err(vpu->ctx, "vpu_ipi_register core fail status=%d", err);
225			return err;
226		}
227	}
228
229	memset(&msg, 0, sizeof(msg));
230	msg.msg_id = AP_IPIMSG_DEC_INIT;
231	msg.ap_inst_addr = (unsigned long)vpu;
232	msg.codec_type = vpu->codec_type;
233
234	mtk_vdec_debug(vpu->ctx, "vdec_inst=%p", vpu);
235
236	err = vcodec_vpu_send_msg(vpu, (void *)&msg, sizeof(msg));
237
238	if (IS_ERR_OR_NULL(vpu->vsi)) {
239		mtk_vdec_err(vpu->ctx, "invalid vdec vsi, status=%d", err);
240		return -EINVAL;
241	}
242
243	mtk_vdec_debug(vpu->ctx, "- ret=%d", err);
244	return err;
245}
246
247int vpu_dec_start(struct vdec_vpu_inst *vpu, uint32_t *data, unsigned int len)
248{
249	struct vdec_ap_ipi_dec_start msg;
250	int i;
251	int err = 0;
252
253	if (len > ARRAY_SIZE(msg.data)) {
254		mtk_vdec_err(vpu->ctx, "invalid len = %d\n", len);
255		return -EINVAL;
256	}
257
258	memset(&msg, 0, sizeof(msg));
259	msg.msg_id = AP_IPIMSG_DEC_START;
260	if (vpu->fw_abi_version < 2)
261		msg.vpu_inst_addr = vpu->inst_addr;
262	else
263		msg.inst_id = vpu->inst_id;
264
265	for (i = 0; i < len; i++)
266		msg.data[i] = data[i];
267	msg.codec_type = vpu->codec_type;
268
269	err = vcodec_vpu_send_msg(vpu, (void *)&msg, sizeof(msg));
270	mtk_vdec_debug(vpu->ctx, "- ret=%d", err);
271	return err;
272}
273
274int vpu_dec_get_param(struct vdec_vpu_inst *vpu, uint32_t *data,
275		      unsigned int len, unsigned int param_type)
276{
277	struct vdec_ap_ipi_get_param msg;
278	int err;
279
280	if (len > ARRAY_SIZE(msg.data)) {
281		mtk_vdec_err(vpu->ctx, "invalid len = %d\n", len);
282		return -EINVAL;
283	}
284
285	memset(&msg, 0, sizeof(msg));
286	msg.msg_id = AP_IPIMSG_DEC_GET_PARAM;
287	msg.inst_id = vpu->inst_id;
288	memcpy(msg.data, data, sizeof(unsigned int) * len);
289	msg.param_type = param_type;
290	msg.codec_type = vpu->codec_type;
291
292	err = vcodec_vpu_send_msg(vpu, (void *)&msg, sizeof(msg));
293	mtk_vdec_debug(vpu->ctx, "- ret=%d", err);
294	return err;
295}
296
297int vpu_dec_core(struct vdec_vpu_inst *vpu)
298{
299	return vcodec_send_ap_ipi(vpu, AP_IPIMSG_DEC_CORE);
300}
301
302int vpu_dec_end(struct vdec_vpu_inst *vpu)
303{
304	return vcodec_send_ap_ipi(vpu, AP_IPIMSG_DEC_END);
305}
306
307int vpu_dec_core_end(struct vdec_vpu_inst *vpu)
308{
309	return vcodec_send_ap_ipi(vpu, AP_IPIMSG_DEC_CORE_END);
310}
311
312int vpu_dec_deinit(struct vdec_vpu_inst *vpu)
313{
314	return vcodec_send_ap_ipi(vpu, AP_IPIMSG_DEC_DEINIT);
315}
316
317int vpu_dec_reset(struct vdec_vpu_inst *vpu)
318{
319	return vcodec_send_ap_ipi(vpu, AP_IPIMSG_DEC_RESET);
320}
~/ Read-only file preview.