1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2015-2016 MediaTek Inc.
4  * Author: Houlong Wei <houlong.wei@mediatek.com>
5  *         Ming Hsiu Tsai <minghsiu.tsai@mediatek.com>
6  */
7 
8 #include "mtk_mdp_core.h"
9 #include "mtk_mdp_vpu.h"
10 #include "mtk_vpu.h"
11 
12 
vpu_to_ctx(struct mtk_mdp_vpu * vpu)13 static inline struct mtk_mdp_ctx *vpu_to_ctx(struct mtk_mdp_vpu *vpu)
14 {
15 	return container_of(vpu, struct mtk_mdp_ctx, vpu);
16 }
17 
mtk_mdp_vpu_handle_init_ack(struct mdp_ipi_comm_ack * msg)18 static void mtk_mdp_vpu_handle_init_ack(struct mdp_ipi_comm_ack *msg)
19 {
20 	struct mtk_mdp_vpu *vpu = (struct mtk_mdp_vpu *)
21 					(unsigned long)msg->ap_inst;
22 
23 	/* mapping VPU address to kernel virtual address */
24 	vpu->vsi = (struct mdp_process_vsi *)
25 			vpu_mapping_dm_addr(vpu->pdev, msg->vpu_inst_addr);
26 	vpu->inst_addr = msg->vpu_inst_addr;
27 }
28 
mtk_mdp_vpu_ipi_handler(void * data,unsigned int len,void * priv)29 static void mtk_mdp_vpu_ipi_handler(void *data, unsigned int len, void *priv)
30 {
31 	unsigned int msg_id = *(unsigned int *)data;
32 	struct mdp_ipi_comm_ack *msg = (struct mdp_ipi_comm_ack *)data;
33 	struct mtk_mdp_vpu *vpu = (struct mtk_mdp_vpu *)
34 					(unsigned long)msg->ap_inst;
35 	struct mtk_mdp_ctx *ctx;
36 
37 	vpu->failure = msg->status;
38 	if (!vpu->failure) {
39 		switch (msg_id) {
40 		case VPU_MDP_INIT_ACK:
41 			mtk_mdp_vpu_handle_init_ack(data);
42 			break;
43 		case VPU_MDP_DEINIT_ACK:
44 		case VPU_MDP_PROCESS_ACK:
45 			break;
46 		default:
47 			ctx = vpu_to_ctx(vpu);
48 			dev_err(&ctx->mdp_dev->pdev->dev,
49 				"handle unknown ipi msg:0x%x\n",
50 				msg_id);
51 			break;
52 		}
53 	} else {
54 		ctx = vpu_to_ctx(vpu);
55 		mtk_mdp_dbg(0, "[%d]:msg 0x%x, failure:%d", ctx->id,
56 			    msg_id, vpu->failure);
57 	}
58 }
59 
mtk_mdp_vpu_register(struct platform_device * pdev)60 int mtk_mdp_vpu_register(struct platform_device *pdev)
61 {
62 	struct mtk_mdp_dev *mdp = platform_get_drvdata(pdev);
63 	int err;
64 
65 	err = vpu_ipi_register(mdp->vpu_dev, IPI_MDP,
66 			       mtk_mdp_vpu_ipi_handler, "mdp_vpu", NULL);
67 	if (err)
68 		dev_err(&mdp->pdev->dev,
69 			"vpu_ipi_registration fail status=%d\n", err);
70 
71 	return err;
72 }
73 
mtk_mdp_vpu_send_msg(void * msg,int len,struct mtk_mdp_vpu * vpu,int id)74 static int mtk_mdp_vpu_send_msg(void *msg, int len, struct mtk_mdp_vpu *vpu,
75 				int id)
76 {
77 	struct mtk_mdp_ctx *ctx = vpu_to_ctx(vpu);
78 	int err;
79 
80 	if (!vpu->pdev) {
81 		mtk_mdp_dbg(1, "[%d]:vpu pdev is NULL", ctx->id);
82 		return -EINVAL;
83 	}
84 
85 	mutex_lock(&ctx->mdp_dev->vpulock);
86 	err = vpu_ipi_send(vpu->pdev, (enum ipi_id)id, msg, len);
87 	if (err)
88 		dev_err(&ctx->mdp_dev->pdev->dev,
89 			"vpu_ipi_send fail status %d\n", err);
90 	mutex_unlock(&ctx->mdp_dev->vpulock);
91 
92 	return err;
93 }
94 
mtk_mdp_vpu_send_ap_ipi(struct mtk_mdp_vpu * vpu,uint32_t msg_id)95 static int mtk_mdp_vpu_send_ap_ipi(struct mtk_mdp_vpu *vpu, uint32_t msg_id)
96 {
97 	int err;
98 	struct mdp_ipi_comm msg;
99 
100 	msg.msg_id = msg_id;
101 	msg.ipi_id = IPI_MDP;
102 	msg.vpu_inst_addr = vpu->inst_addr;
103 	msg.ap_inst = (unsigned long)vpu;
104 	err = mtk_mdp_vpu_send_msg((void *)&msg, sizeof(msg), vpu, IPI_MDP);
105 	if (!err && vpu->failure)
106 		err = -EINVAL;
107 
108 	return err;
109 }
110 
mtk_mdp_vpu_init(struct mtk_mdp_vpu * vpu)111 int mtk_mdp_vpu_init(struct mtk_mdp_vpu *vpu)
112 {
113 	int err;
114 	struct mdp_ipi_init msg;
115 	struct mtk_mdp_ctx *ctx = vpu_to_ctx(vpu);
116 
117 	vpu->pdev = ctx->mdp_dev->vpu_dev;
118 
119 	msg.msg_id = AP_MDP_INIT;
120 	msg.ipi_id = IPI_MDP;
121 	msg.ap_inst = (unsigned long)vpu;
122 	err = mtk_mdp_vpu_send_msg((void *)&msg, sizeof(msg), vpu, IPI_MDP);
123 	if (!err && vpu->failure)
124 		err = -EINVAL;
125 
126 	return err;
127 }
128 
mtk_mdp_vpu_deinit(struct mtk_mdp_vpu * vpu)129 int mtk_mdp_vpu_deinit(struct mtk_mdp_vpu *vpu)
130 {
131 	return mtk_mdp_vpu_send_ap_ipi(vpu, AP_MDP_DEINIT);
132 }
133 
mtk_mdp_vpu_process(struct mtk_mdp_vpu * vpu)134 int mtk_mdp_vpu_process(struct mtk_mdp_vpu *vpu)
135 {
136 	return mtk_mdp_vpu_send_ap_ipi(vpu, AP_MDP_PROCESS);
137 }
138