media: mediatek: vcodec: Fix encoder access NULL pointer
[platform/kernel/linux-rpi.git] / drivers / media / platform / mediatek / vcodec / encoder / venc_vpu_if.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2016 MediaTek Inc.
4  * Author: PoChun Lin <pochun.lin@mediatek.com>
5  */
6
7 #include "mtk_vcodec_enc_drv.h"
8 #include "venc_ipi_msg.h"
9 #include "venc_vpu_if.h"
10
11 static void handle_enc_init_msg(struct venc_vpu_inst *vpu, const void *data)
12 {
13         const struct venc_vpu_ipi_msg_init *msg = data;
14
15         vpu->inst_addr = msg->vpu_inst_addr;
16         vpu->vsi = mtk_vcodec_fw_map_dm_addr(vpu->ctx->dev->fw_handler,
17                                              msg->vpu_inst_addr);
18
19         /* Firmware version field value is unspecified on MT8173. */
20         if (mtk_vcodec_fw_get_type(vpu->ctx->dev->fw_handler) == VPU)
21                 return;
22
23         /* Check firmware version. */
24         mtk_venc_debug(vpu->ctx, "firmware version: 0x%x\n", msg->venc_abi_version);
25         switch (msg->venc_abi_version) {
26         case 1:
27                 break;
28         default:
29                 mtk_venc_err(vpu->ctx, "unhandled firmware version 0x%x\n",
30                              msg->venc_abi_version);
31                 vpu->failure = 1;
32                 break;
33         }
34 }
35
36 static void handle_enc_encode_msg(struct venc_vpu_inst *vpu, const void *data)
37 {
38         const struct venc_vpu_ipi_msg_enc *msg = data;
39
40         vpu->state = msg->state;
41         vpu->bs_size = msg->bs_size;
42         vpu->is_key_frm = msg->is_key_frm;
43 }
44
45 static bool vpu_enc_check_ap_inst(struct mtk_vcodec_enc_dev *enc_dev, struct venc_vpu_inst *vpu)
46 {
47         struct mtk_vcodec_enc_ctx *ctx;
48         int ret = false;
49
50         list_for_each_entry(ctx, &enc_dev->ctx_list, list) {
51                 if (!IS_ERR_OR_NULL(ctx) && ctx->vpu_inst == vpu) {
52                         ret = true;
53                         break;
54                 }
55         }
56
57         return ret;
58 }
59
60 static void vpu_enc_ipi_handler(void *data, unsigned int len, void *priv)
61 {
62         struct mtk_vcodec_enc_dev *enc_dev;
63         const struct venc_vpu_ipi_msg_common *msg = data;
64         struct venc_vpu_inst *vpu;
65
66         enc_dev = (struct mtk_vcodec_enc_dev *)priv;
67         vpu = (struct venc_vpu_inst *)(unsigned long)msg->venc_inst;
68         if (!priv || !vpu) {
69                 pr_err(MTK_DBG_V4L2_STR "venc_inst is NULL, did the SCP hang or crash?");
70                 return;
71         }
72
73         mtk_venc_debug(vpu->ctx, "msg_id %x inst %p status %d", msg->msg_id, vpu, msg->status);
74         if (!vpu_enc_check_ap_inst(enc_dev, vpu) || msg->msg_id < VPU_IPIMSG_ENC_INIT_DONE ||
75             msg->msg_id > VPU_IPIMSG_ENC_DEINIT_DONE) {
76                 mtk_v4l2_venc_err(vpu->ctx, "venc msg id not correctly => 0x%x", msg->msg_id);
77                 vpu->failure = -EINVAL;
78                 goto error;
79         }
80
81         vpu->failure = (msg->status != VENC_IPI_MSG_STATUS_OK);
82         if (vpu->failure) {
83                 mtk_venc_err(vpu->ctx, "vpu enc status failure %d", vpu->failure);
84                 goto error;
85         }
86
87         switch (msg->msg_id) {
88         case VPU_IPIMSG_ENC_INIT_DONE:
89                 handle_enc_init_msg(vpu, data);
90                 break;
91         case VPU_IPIMSG_ENC_SET_PARAM_DONE:
92                 break;
93         case VPU_IPIMSG_ENC_ENCODE_DONE:
94                 handle_enc_encode_msg(vpu, data);
95                 break;
96         case VPU_IPIMSG_ENC_DEINIT_DONE:
97                 break;
98         default:
99                 mtk_venc_err(vpu->ctx, "unknown msg id %x", msg->msg_id);
100                 break;
101         }
102
103 error:
104         vpu->signaled = 1;
105 }
106
107 static int vpu_enc_send_msg(struct venc_vpu_inst *vpu, void *msg,
108                             int len)
109 {
110         int status;
111
112         if (!vpu->ctx->dev->fw_handler) {
113                 mtk_venc_err(vpu->ctx, "inst dev is NULL");
114                 return -EINVAL;
115         }
116
117         status = mtk_vcodec_fw_ipi_send(vpu->ctx->dev->fw_handler, vpu->id, msg,
118                                         len, 2000);
119         if (status) {
120                 mtk_venc_err(vpu->ctx, "vpu_ipi_send msg_id %x len %d fail %d",
121                              *(uint32_t *)msg, len, status);
122                 return -EINVAL;
123         }
124         if (vpu->failure)
125                 return -EINVAL;
126
127         return 0;
128 }
129
130 int vpu_enc_init(struct venc_vpu_inst *vpu)
131 {
132         int status;
133         struct venc_ap_ipi_msg_init out;
134
135         init_waitqueue_head(&vpu->wq_hd);
136         vpu->signaled = 0;
137         vpu->failure = 0;
138         vpu->ctx->vpu_inst = vpu;
139
140         status = mtk_vcodec_fw_ipi_register(vpu->ctx->dev->fw_handler, vpu->id,
141                                             vpu_enc_ipi_handler, "venc",
142                                             vpu->ctx->dev);
143
144         if (status) {
145                 mtk_venc_err(vpu->ctx, "vpu_ipi_register fail %d", status);
146                 return -EINVAL;
147         }
148
149         memset(&out, 0, sizeof(out));
150         out.msg_id = AP_IPIMSG_ENC_INIT;
151         out.venc_inst = (unsigned long)vpu;
152         if (vpu_enc_send_msg(vpu, &out, sizeof(out))) {
153                 mtk_venc_err(vpu->ctx, "AP_IPIMSG_ENC_INIT fail");
154                 return -EINVAL;
155         }
156
157         return 0;
158 }
159
160 static unsigned int venc_enc_param_crop_right(struct venc_vpu_inst *vpu,
161                                               struct venc_enc_param *enc_prm)
162 {
163         unsigned int img_crop_right = enc_prm->buf_width - enc_prm->width;
164
165         return img_crop_right % 16;
166 }
167
168 static unsigned int venc_enc_param_crop_bottom(struct venc_enc_param *enc_prm)
169 {
170         return round_up(enc_prm->height, 16) - enc_prm->height;
171 }
172
173 static unsigned int venc_enc_param_num_mb(struct venc_enc_param *enc_prm)
174 {
175         return DIV_ROUND_UP(enc_prm->width, 16) *
176                DIV_ROUND_UP(enc_prm->height, 16);
177 }
178
179 int vpu_enc_set_param(struct venc_vpu_inst *vpu,
180                       enum venc_set_param_type id,
181                       struct venc_enc_param *enc_param)
182 {
183         const bool is_ext = MTK_ENC_CTX_IS_EXT(vpu->ctx);
184         size_t msg_size = is_ext ?
185                 sizeof(struct venc_ap_ipi_msg_set_param_ext) :
186                 sizeof(struct venc_ap_ipi_msg_set_param);
187         struct venc_ap_ipi_msg_set_param_ext out;
188
189         mtk_venc_debug(vpu->ctx, "id %d ->", id);
190
191         memset(&out, 0, sizeof(out));
192         out.base.msg_id = AP_IPIMSG_ENC_SET_PARAM;
193         out.base.vpu_inst_addr = vpu->inst_addr;
194         out.base.param_id = id;
195         switch (id) {
196         case VENC_SET_PARAM_ENC:
197                 if (is_ext) {
198                         out.base.data_item = 3;
199                         out.base.data[0] =
200                                 venc_enc_param_crop_right(vpu, enc_param);
201                         out.base.data[1] =
202                                 venc_enc_param_crop_bottom(enc_param);
203                         out.base.data[2] = venc_enc_param_num_mb(enc_param);
204                 } else {
205                         out.base.data_item = 0;
206                 }
207                 break;
208         case VENC_SET_PARAM_FORCE_INTRA:
209                 out.base.data_item = 0;
210                 break;
211         case VENC_SET_PARAM_ADJUST_BITRATE:
212                 out.base.data_item = 1;
213                 out.base.data[0] = enc_param->bitrate;
214                 break;
215         case VENC_SET_PARAM_ADJUST_FRAMERATE:
216                 out.base.data_item = 1;
217                 out.base.data[0] = enc_param->frm_rate;
218                 break;
219         case VENC_SET_PARAM_GOP_SIZE:
220                 out.base.data_item = 1;
221                 out.base.data[0] = enc_param->gop_size;
222                 break;
223         case VENC_SET_PARAM_INTRA_PERIOD:
224                 out.base.data_item = 1;
225                 out.base.data[0] = enc_param->intra_period;
226                 break;
227         case VENC_SET_PARAM_SKIP_FRAME:
228                 out.base.data_item = 0;
229                 break;
230         default:
231                 mtk_venc_err(vpu->ctx, "id %d not supported", id);
232                 return -EINVAL;
233         }
234         if (vpu_enc_send_msg(vpu, &out, msg_size)) {
235                 mtk_venc_err(vpu->ctx, "AP_IPIMSG_ENC_SET_PARAM %d fail", id);
236                 return -EINVAL;
237         }
238
239         mtk_venc_debug(vpu->ctx, "id %d <-", id);
240
241         return 0;
242 }
243
244 static int vpu_enc_encode_32bits(struct venc_vpu_inst *vpu,
245                                  unsigned int bs_mode,
246                                  struct venc_frm_buf *frm_buf,
247                                  struct mtk_vcodec_mem *bs_buf,
248                                  struct venc_frame_info *frame_info)
249 {
250         const bool is_ext = MTK_ENC_CTX_IS_EXT(vpu->ctx);
251         size_t msg_size = is_ext ?
252                 sizeof(struct venc_ap_ipi_msg_enc_ext) :
253                 sizeof(struct venc_ap_ipi_msg_enc);
254         struct venc_ap_ipi_msg_enc_ext out;
255
256         mtk_venc_debug(vpu->ctx, "bs_mode %d ->", bs_mode);
257
258         memset(&out, 0, sizeof(out));
259         out.base.msg_id = AP_IPIMSG_ENC_ENCODE;
260         out.base.vpu_inst_addr = vpu->inst_addr;
261         out.base.bs_mode = bs_mode;
262         if (frm_buf) {
263                 if ((frm_buf->fb_addr[0].dma_addr % 16 == 0) &&
264                     (frm_buf->fb_addr[1].dma_addr % 16 == 0) &&
265                     (frm_buf->fb_addr[2].dma_addr % 16 == 0)) {
266                         out.base.input_addr[0] = frm_buf->fb_addr[0].dma_addr;
267                         out.base.input_addr[1] = frm_buf->fb_addr[1].dma_addr;
268                         out.base.input_addr[2] = frm_buf->fb_addr[2].dma_addr;
269                 } else {
270                         mtk_venc_err(vpu->ctx, "dma_addr not align to 16");
271                         return -EINVAL;
272                 }
273         }
274         if (bs_buf) {
275                 out.base.bs_addr = bs_buf->dma_addr;
276                 out.base.bs_size = bs_buf->size;
277         }
278         if (is_ext && frame_info) {
279                 out.data_item = 3;
280                 out.data[0] = frame_info->frm_count;
281                 out.data[1] = frame_info->skip_frm_count;
282                 out.data[2] = frame_info->frm_type;
283         }
284         if (vpu_enc_send_msg(vpu, &out, msg_size)) {
285                 mtk_venc_err(vpu->ctx, "AP_IPIMSG_ENC_ENCODE %d fail", bs_mode);
286                 return -EINVAL;
287         }
288
289         return 0;
290 }
291
292 static int vpu_enc_encode_34bits(struct venc_vpu_inst *vpu,
293                                  unsigned int bs_mode,
294                                  struct venc_frm_buf *frm_buf,
295                                  struct mtk_vcodec_mem *bs_buf,
296                                  struct venc_frame_info *frame_info)
297 {
298         struct venc_ap_ipi_msg_enc_ext_34 out;
299         size_t msg_size = sizeof(struct venc_ap_ipi_msg_enc_ext_34);
300
301         mtk_venc_debug(vpu->ctx, "bs_mode %d ->", bs_mode);
302
303         memset(&out, 0, sizeof(out));
304         out.msg_id = AP_IPIMSG_ENC_ENCODE;
305         out.vpu_inst_addr = vpu->inst_addr;
306         out.bs_mode = bs_mode;
307
308         if (frm_buf) {
309                 if ((frm_buf->fb_addr[0].dma_addr % 16 == 0) &&
310                     (frm_buf->fb_addr[1].dma_addr % 16 == 0) &&
311                     (frm_buf->fb_addr[2].dma_addr % 16 == 0)) {
312                         out.input_addr[0] = frm_buf->fb_addr[0].dma_addr;
313                         out.input_addr[1] = frm_buf->fb_addr[1].dma_addr;
314                         out.input_addr[2] = frm_buf->fb_addr[2].dma_addr;
315                 } else {
316                         mtk_venc_err(vpu->ctx, "dma_addr not align to 16");
317                         return -EINVAL;
318                 }
319         }
320         if (bs_buf) {
321                 out.bs_addr = bs_buf->dma_addr;
322                 out.bs_size = bs_buf->size;
323         }
324         if (frame_info) {
325                 out.data_item = 3;
326                 out.data[0] = frame_info->frm_count;
327                 out.data[1] = frame_info->skip_frm_count;
328                 out.data[2] = frame_info->frm_type;
329         }
330         if (vpu_enc_send_msg(vpu, &out, msg_size)) {
331                 mtk_venc_err(vpu->ctx, "AP_IPIMSG_ENC_ENCODE %d fail", bs_mode);
332                 return -EINVAL;
333         }
334
335         return 0;
336 }
337
338 int vpu_enc_encode(struct venc_vpu_inst *vpu, unsigned int bs_mode,
339                    struct venc_frm_buf *frm_buf,
340                    struct mtk_vcodec_mem *bs_buf,
341                    struct venc_frame_info *frame_info)
342 {
343         int ret;
344
345         if (MTK_ENC_IOVA_IS_34BIT(vpu->ctx))
346                 ret = vpu_enc_encode_34bits(vpu, bs_mode,
347                                             frm_buf, bs_buf, frame_info);
348         else
349                 ret = vpu_enc_encode_32bits(vpu, bs_mode,
350                                             frm_buf, bs_buf, frame_info);
351
352         if (ret)
353                 return ret;
354
355         mtk_venc_debug(vpu->ctx, "bs_mode %d state %d size %d key_frm %d <-",
356                        bs_mode, vpu->state, vpu->bs_size, vpu->is_key_frm);
357
358         return 0;
359 }
360
361 int vpu_enc_deinit(struct venc_vpu_inst *vpu)
362 {
363         struct venc_ap_ipi_msg_deinit out;
364
365         memset(&out, 0, sizeof(out));
366         out.msg_id = AP_IPIMSG_ENC_DEINIT;
367         out.vpu_inst_addr = vpu->inst_addr;
368         if (vpu_enc_send_msg(vpu, &out, sizeof(out))) {
369                 mtk_venc_err(vpu->ctx, "AP_IPIMSG_ENC_DEINIT fail");
370                 return -EINVAL;
371         }
372
373         return 0;
374 }