Merge tag 'media/v6.7-1' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab...
[linux-2.6-microblaze.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         if (IS_ERR_OR_NULL(vpu->vsi)) {
158                 mtk_venc_err(vpu->ctx, "invalid venc vsi");
159                 return -EINVAL;
160         }
161
162         return 0;
163 }
164
165 static unsigned int venc_enc_param_crop_right(struct venc_vpu_inst *vpu,
166                                               struct venc_enc_param *enc_prm)
167 {
168         unsigned int img_crop_right = enc_prm->buf_width - enc_prm->width;
169
170         return img_crop_right % 16;
171 }
172
173 static unsigned int venc_enc_param_crop_bottom(struct venc_enc_param *enc_prm)
174 {
175         return round_up(enc_prm->height, 16) - enc_prm->height;
176 }
177
178 static unsigned int venc_enc_param_num_mb(struct venc_enc_param *enc_prm)
179 {
180         return DIV_ROUND_UP(enc_prm->width, 16) *
181                DIV_ROUND_UP(enc_prm->height, 16);
182 }
183
184 int vpu_enc_set_param(struct venc_vpu_inst *vpu,
185                       enum venc_set_param_type id,
186                       struct venc_enc_param *enc_param)
187 {
188         const bool is_ext = MTK_ENC_CTX_IS_EXT(vpu->ctx);
189         size_t msg_size = is_ext ?
190                 sizeof(struct venc_ap_ipi_msg_set_param_ext) :
191                 sizeof(struct venc_ap_ipi_msg_set_param);
192         struct venc_ap_ipi_msg_set_param_ext out;
193
194         mtk_venc_debug(vpu->ctx, "id %d ->", id);
195
196         memset(&out, 0, sizeof(out));
197         out.base.msg_id = AP_IPIMSG_ENC_SET_PARAM;
198         out.base.vpu_inst_addr = vpu->inst_addr;
199         out.base.param_id = id;
200         switch (id) {
201         case VENC_SET_PARAM_ENC:
202                 if (is_ext) {
203                         out.base.data_item = 3;
204                         out.base.data[0] =
205                                 venc_enc_param_crop_right(vpu, enc_param);
206                         out.base.data[1] =
207                                 venc_enc_param_crop_bottom(enc_param);
208                         out.base.data[2] = venc_enc_param_num_mb(enc_param);
209                 } else {
210                         out.base.data_item = 0;
211                 }
212                 break;
213         case VENC_SET_PARAM_FORCE_INTRA:
214                 out.base.data_item = 0;
215                 break;
216         case VENC_SET_PARAM_ADJUST_BITRATE:
217                 out.base.data_item = 1;
218                 out.base.data[0] = enc_param->bitrate;
219                 break;
220         case VENC_SET_PARAM_ADJUST_FRAMERATE:
221                 out.base.data_item = 1;
222                 out.base.data[0] = enc_param->frm_rate;
223                 break;
224         case VENC_SET_PARAM_GOP_SIZE:
225                 out.base.data_item = 1;
226                 out.base.data[0] = enc_param->gop_size;
227                 break;
228         case VENC_SET_PARAM_INTRA_PERIOD:
229                 out.base.data_item = 1;
230                 out.base.data[0] = enc_param->intra_period;
231                 break;
232         case VENC_SET_PARAM_SKIP_FRAME:
233                 out.base.data_item = 0;
234                 break;
235         default:
236                 mtk_venc_err(vpu->ctx, "id %d not supported", id);
237                 return -EINVAL;
238         }
239         if (vpu_enc_send_msg(vpu, &out, msg_size)) {
240                 mtk_venc_err(vpu->ctx, "AP_IPIMSG_ENC_SET_PARAM %d fail", id);
241                 return -EINVAL;
242         }
243
244         mtk_venc_debug(vpu->ctx, "id %d <-", id);
245
246         return 0;
247 }
248
249 static int vpu_enc_encode_32bits(struct venc_vpu_inst *vpu,
250                                  unsigned int bs_mode,
251                                  struct venc_frm_buf *frm_buf,
252                                  struct mtk_vcodec_mem *bs_buf,
253                                  struct venc_frame_info *frame_info)
254 {
255         const bool is_ext = MTK_ENC_CTX_IS_EXT(vpu->ctx);
256         size_t msg_size = is_ext ?
257                 sizeof(struct venc_ap_ipi_msg_enc_ext) :
258                 sizeof(struct venc_ap_ipi_msg_enc);
259         struct venc_ap_ipi_msg_enc_ext out;
260
261         mtk_venc_debug(vpu->ctx, "bs_mode %d ->", bs_mode);
262
263         memset(&out, 0, sizeof(out));
264         out.base.msg_id = AP_IPIMSG_ENC_ENCODE;
265         out.base.vpu_inst_addr = vpu->inst_addr;
266         out.base.bs_mode = bs_mode;
267         if (frm_buf) {
268                 if ((frm_buf->fb_addr[0].dma_addr % 16 == 0) &&
269                     (frm_buf->fb_addr[1].dma_addr % 16 == 0) &&
270                     (frm_buf->fb_addr[2].dma_addr % 16 == 0)) {
271                         out.base.input_addr[0] = frm_buf->fb_addr[0].dma_addr;
272                         out.base.input_addr[1] = frm_buf->fb_addr[1].dma_addr;
273                         out.base.input_addr[2] = frm_buf->fb_addr[2].dma_addr;
274                 } else {
275                         mtk_venc_err(vpu->ctx, "dma_addr not align to 16");
276                         return -EINVAL;
277                 }
278         }
279         if (bs_buf) {
280                 out.base.bs_addr = bs_buf->dma_addr;
281                 out.base.bs_size = bs_buf->size;
282         }
283         if (is_ext && frame_info) {
284                 out.data_item = 3;
285                 out.data[0] = frame_info->frm_count;
286                 out.data[1] = frame_info->skip_frm_count;
287                 out.data[2] = frame_info->frm_type;
288         }
289         if (vpu_enc_send_msg(vpu, &out, msg_size)) {
290                 mtk_venc_err(vpu->ctx, "AP_IPIMSG_ENC_ENCODE %d fail", bs_mode);
291                 return -EINVAL;
292         }
293
294         return 0;
295 }
296
297 static int vpu_enc_encode_34bits(struct venc_vpu_inst *vpu,
298                                  unsigned int bs_mode,
299                                  struct venc_frm_buf *frm_buf,
300                                  struct mtk_vcodec_mem *bs_buf,
301                                  struct venc_frame_info *frame_info)
302 {
303         struct venc_ap_ipi_msg_enc_ext_34 out;
304         size_t msg_size = sizeof(struct venc_ap_ipi_msg_enc_ext_34);
305
306         mtk_venc_debug(vpu->ctx, "bs_mode %d ->", bs_mode);
307
308         memset(&out, 0, sizeof(out));
309         out.msg_id = AP_IPIMSG_ENC_ENCODE;
310         out.vpu_inst_addr = vpu->inst_addr;
311         out.bs_mode = bs_mode;
312
313         if (frm_buf) {
314                 if ((frm_buf->fb_addr[0].dma_addr % 16 == 0) &&
315                     (frm_buf->fb_addr[1].dma_addr % 16 == 0) &&
316                     (frm_buf->fb_addr[2].dma_addr % 16 == 0)) {
317                         out.input_addr[0] = frm_buf->fb_addr[0].dma_addr;
318                         out.input_addr[1] = frm_buf->fb_addr[1].dma_addr;
319                         out.input_addr[2] = frm_buf->fb_addr[2].dma_addr;
320                 } else {
321                         mtk_venc_err(vpu->ctx, "dma_addr not align to 16");
322                         return -EINVAL;
323                 }
324         }
325         if (bs_buf) {
326                 out.bs_addr = bs_buf->dma_addr;
327                 out.bs_size = bs_buf->size;
328         }
329         if (frame_info) {
330                 out.data_item = 3;
331                 out.data[0] = frame_info->frm_count;
332                 out.data[1] = frame_info->skip_frm_count;
333                 out.data[2] = frame_info->frm_type;
334         }
335         if (vpu_enc_send_msg(vpu, &out, msg_size)) {
336                 mtk_venc_err(vpu->ctx, "AP_IPIMSG_ENC_ENCODE %d fail", bs_mode);
337                 return -EINVAL;
338         }
339
340         return 0;
341 }
342
343 int vpu_enc_encode(struct venc_vpu_inst *vpu, unsigned int bs_mode,
344                    struct venc_frm_buf *frm_buf,
345                    struct mtk_vcodec_mem *bs_buf,
346                    struct venc_frame_info *frame_info)
347 {
348         int ret;
349
350         if (MTK_ENC_IOVA_IS_34BIT(vpu->ctx))
351                 ret = vpu_enc_encode_34bits(vpu, bs_mode,
352                                             frm_buf, bs_buf, frame_info);
353         else
354                 ret = vpu_enc_encode_32bits(vpu, bs_mode,
355                                             frm_buf, bs_buf, frame_info);
356
357         if (ret)
358                 return ret;
359
360         mtk_venc_debug(vpu->ctx, "bs_mode %d state %d size %d key_frm %d <-",
361                        bs_mode, vpu->state, vpu->bs_size, vpu->is_key_frm);
362
363         return 0;
364 }
365
366 int vpu_enc_deinit(struct venc_vpu_inst *vpu)
367 {
368         struct venc_ap_ipi_msg_deinit out;
369
370         memset(&out, 0, sizeof(out));
371         out.msg_id = AP_IPIMSG_ENC_DEINIT;
372         out.vpu_inst_addr = vpu->inst_addr;
373         if (vpu_enc_send_msg(vpu, &out, sizeof(out))) {
374                 mtk_venc_err(vpu->ctx, "AP_IPIMSG_ENC_DEINIT fail");
375                 return -EINVAL;
376         }
377
378         return 0;
379 }