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