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