1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (c) 2022 MediaTek Inc.
* Author: Ping-Hsun Wu <ping-hsun.wu@mediatek.com>
*/
#include <linux/remoteproc.h>
#include <linux/remoteproc/mtk_scp.h>
#include "mtk-mdp3-vpu.h"
#include "mtk-mdp3-core.h"
#define MDP_VPU_MESSAGE_TIMEOUT 500U
static inline struct mdp_dev *vpu_to_mdp(struct mdp_vpu_dev *vpu)
{
return container_of(vpu, struct mdp_dev, vpu);
}
static int mdp_vpu_shared_mem_alloc(struct mdp_vpu_dev *vpu)
{
struct device *dev;
if (IS_ERR_OR_NULL(vpu))
goto err_return;
dev = scp_get_device(vpu->scp);
if (!vpu->param) {
vpu->param = dma_alloc_wc(dev, vpu->param_size,
&vpu->param_addr, GFP_KERNEL);
if (!vpu->param)
goto err_return;
}
if (!vpu->work) {
vpu->work = dma_alloc_wc(dev, vpu->work_size,
&vpu->work_addr, GFP_KERNEL);
if (!vpu->work)
goto err_free_param;
}
if (!vpu->config) {
vpu->config = dma_alloc_wc(dev, vpu->config_size,
&vpu->config_addr, GFP_KERNEL);
if (!vpu->config)
goto err_free_work;
}
return 0;
err_free_work:
dma_free_wc(dev, vpu->work_size, vpu->work, vpu->work_addr);
vpu->work = NULL;
err_free_param:
dma_free_wc(dev, vpu->param_size, vpu->param, vpu->param_addr);
vpu->param = NULL;
err_return:
return -ENOMEM;
}
void mdp_vpu_shared_mem_free(struct mdp_vpu_dev *vpu)
{
struct device *dev;
if (IS_ERR_OR_NULL(vpu))
return;
dev = scp_get_device(vpu->scp);
if (vpu->param && vpu->param_addr)
dma_free_wc(dev, vpu->param_size, vpu->param, vpu->param_addr);
if (vpu->work && vpu->work_addr)
dma_free_wc(dev, vpu->work_size, vpu->work, vpu->work_addr);
if (vpu->config && vpu->config_addr)
dma_free_wc(dev, vpu->config_size, vpu->config, vpu->config_addr);
}
static void mdp_vpu_ipi_handle_init_ack(void *data, unsigned int len,
void *priv)
{
struct mdp_ipi_init_msg *msg = (struct mdp_ipi_init_msg *)data;
struct mdp_vpu_dev *vpu =
(struct mdp_vpu_dev *)(unsigned long)msg->drv_data;
if (!vpu->work_size)
vpu->work_size = msg->work_size;
vpu->status = msg->status;
complete(&vpu->ipi_acked);
}
static void mdp_vpu_ipi_handle_deinit_ack(void *data, unsigned int len,
void *priv)
{
struct mdp_ipi_deinit_msg *msg = (struct mdp_ipi_deinit_msg *)data;
struct mdp_vpu_dev *vpu =
(struct mdp_vpu_dev *)(unsigned long)msg->drv_data;
vpu->status = msg->status;
complete(&vpu->ipi_acked);
}
static void mdp_vpu_ipi_handle_frame_ack(void *data, unsigned int len,
void *priv)
{
struct img_sw_addr *addr = (struct img_sw_addr *)data;
struct img_ipi_frameparam *param =
(struct img_ipi_frameparam *)(unsigned long)addr->va;
struct mdp_vpu_dev *vpu =
(struct mdp_vpu_dev *)(unsigned long)param->drv_data;
if (param->state) {
struct mdp_dev *mdp = vpu_to_mdp(vpu);
dev_err(&mdp->pdev->dev, "VPU MDP failure:%d\n", param->state);
}
vpu->status = param->state;
complete(&vpu->ipi_acked);
}
int mdp_vpu_register(struct mdp_dev *mdp)
{
int err;
struct mtk_scp *scp = mdp->scp;
struct device *dev = &mdp->pdev->dev;
err = scp_ipi_register(scp, SCP_IPI_MDP_INIT,
mdp_vpu_ipi_handle_init_ack, NULL);
if (err) {
dev_err(dev, "scp_ipi_register failed %d\n", err);
goto err_ipi_init;
}
err = scp_ipi_register(scp, SCP_IPI_MDP_DEINIT,
mdp_vpu_ipi_handle_deinit_ack, NULL);
if (err) {
dev_err(dev, "scp_ipi_register failed %d\n", err);
goto err_ipi_deinit;
}
err = scp_ipi_register(scp, SCP_IPI_MDP_FRAME,
mdp_vpu_ipi_handle_frame_ack, NULL);
if (err) {
dev_err(dev, "scp_ipi_register failed %d\n", err);
goto err_ipi_frame;
}
return 0;
err_ipi_frame:
scp_ipi_unregister(scp, SCP_IPI_MDP_DEINIT);
err_ipi_deinit:
scp_ipi_unregister(scp, SCP_IPI_MDP_INIT);
err_ipi_init:
return err;
}
void mdp_vpu_unregister(struct mdp_dev *mdp)
{
scp_ipi_unregister(mdp->scp, SCP_IPI_MDP_INIT);
scp_ipi_unregister(mdp->scp, SCP_IPI_MDP_DEINIT);
scp_ipi_unregister(mdp->scp, SCP_IPI_MDP_FRAME);
}
static int mdp_vpu_sendmsg(struct mdp_vpu_dev *vpu, enum scp_ipi_id id,
void *buf, unsigned int len)
{
struct mdp_dev *mdp = vpu_to_mdp(vpu);
unsigned int t = MDP_VPU_MESSAGE_TIMEOUT;
int ret;
if (!vpu->scp) {
dev_dbg(&mdp->pdev->dev, "vpu scp is NULL");
return -EINVAL;
}
ret = scp_ipi_send(vpu->scp, id, buf, len, 2000);
if (ret) {
dev_err(&mdp->pdev->dev, "scp_ipi_send failed %d\n", ret);
return -EPERM;
}
ret = wait_for_completion_timeout(&vpu->ipi_acked,
msecs_to_jiffies(t));
if (!ret)
ret = -ETIME;
else if (vpu->status)
ret = -EINVAL;
else
ret = 0;
return ret;
}
int mdp_vpu_dev_init(struct mdp_vpu_dev *vpu, struct mtk_scp *scp,
struct mutex *lock)
{
struct mdp_ipi_init_msg msg = {
.drv_data = (unsigned long)vpu,
};
struct mdp_dev *mdp = vpu_to_mdp(vpu);
int err;
u8 pp_num = mdp->mdp_data->pp_used;
init_completion(&vpu->ipi_acked);
vpu->scp = scp;
vpu->lock = lock;
vpu->work_size = 0;
err = mdp_vpu_sendmsg(vpu, SCP_IPI_MDP_INIT, &msg, sizeof(msg));
if (err)
goto err_work_size;
/* vpu work_size was set in mdp_vpu_ipi_handle_init_ack */
mutex_lock(vpu->lock);
vpu->work_size = ALIGN(vpu->work_size, 64);
vpu->param_size = ALIGN(sizeof(struct img_ipi_frameparam), 64);
vpu->config_size = ALIGN(sizeof(struct img_config) * pp_num, 64);
err = mdp_vpu_shared_mem_alloc(vpu);
mutex_unlock(vpu->lock);
if (err) {
dev_err(&mdp->pdev->dev, "VPU memory alloc fail!");
goto err_mem_alloc;
}
dev_dbg(&mdp->pdev->dev,
"VPU param:%pK pa:%pad sz:%zx, work:%pK pa:%pad sz:%zx, config:%pK pa:%pad sz:%zx",
vpu->param, &vpu->param_addr, vpu->param_size,
vpu->work, &vpu->work_addr, vpu->work_size,
vpu->config, &vpu->config_addr, vpu->config_size);
msg.work_addr = vpu->work_addr;
msg.work_size = vpu->work_size;
err = mdp_vpu_sendmsg(vpu, SCP_IPI_MDP_INIT, &msg, sizeof(msg));
if (err)
goto err_work_size;
return 0;
err_work_size:
switch (vpu->status) {
case -MDP_IPI_EBUSY:
err = -EBUSY;
break;
case -MDP_IPI_ENOMEM:
err = -ENOSPC; /* -ENOMEM */
break;
}
return err;
err_mem_alloc:
return err;
}
int mdp_vpu_dev_deinit(struct mdp_vpu_dev *vpu)
{
struct mdp_ipi_deinit_msg msg = {
.drv_data = (unsigned long)vpu,
.work_addr = vpu->work_addr,
};
return mdp_vpu_sendmsg(vpu, SCP_IPI_MDP_DEINIT, &msg, sizeof(msg));
}
int mdp_vpu_process(struct mdp_vpu_dev *vpu, struct img_ipi_frameparam *param)
{
struct mdp_dev *mdp = vpu_to_mdp(vpu);
struct img_sw_addr addr;
mutex_lock(vpu->lock);
if (mdp_vpu_shared_mem_alloc(vpu)) {
dev_err(&mdp->pdev->dev, "VPU memory alloc fail!");
mutex_unlock(vpu->lock);
return -ENOMEM;
}
memset(vpu->param, 0, vpu->param_size);
memset(vpu->work, 0, vpu->work_size);
memset(vpu->config, 0, vpu->config_size);
param->self_data.va = (unsigned long)vpu->work;
param->self_data.pa = vpu->work_addr;
param->config_data.va = (unsigned long)vpu->config;
param->config_data.pa = vpu->config_addr;
param->drv_data = (unsigned long)vpu;
memcpy(vpu->param, param, sizeof(*param));
addr.pa = vpu->param_addr;
addr.va = (unsigned long)vpu->param;
mutex_unlock(vpu->lock);
return mdp_vpu_sendmsg(vpu, SCP_IPI_MDP_FRAME, &addr, sizeof(addr));
}
|