forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-11 6778948f9de86c3cfaf36725a7c87dcff9ba247f
kernel/drivers/media/platform/mtk-vcodec/venc_vpu_if.c
....@@ -1,33 +1,43 @@
1
+// SPDX-License-Identifier: GPL-2.0
12 /*
23 * Copyright (c) 2016 MediaTek Inc.
34 * Author: PoChun Lin <pochun.lin@mediatek.com>
4
- *
5
- * This program is free software; you can redistribute it and/or
6
- * modify
7
- * it under the terms of the GNU General Public License version 2 as
8
- * published by the Free Software Foundation.
9
- *
10
- * This program is distributed in the hope that it will be useful,
11
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
- * GNU General Public License for more details.
145 */
156
16
-#include "mtk_vpu.h"
7
+#include "mtk_vcodec_drv.h"
8
+#include "mtk_vcodec_fw.h"
179 #include "venc_ipi_msg.h"
1810 #include "venc_vpu_if.h"
1911
20
-static void handle_enc_init_msg(struct venc_vpu_inst *vpu, void *data)
12
+static void handle_enc_init_msg(struct venc_vpu_inst *vpu, const void *data)
2113 {
22
- struct venc_vpu_ipi_msg_init *msg = data;
14
+ const struct venc_vpu_ipi_msg_init *msg = data;
2315
2416 vpu->inst_addr = msg->vpu_inst_addr;
25
- vpu->vsi = vpu_mapping_dm_addr(vpu->dev, msg->vpu_inst_addr);
17
+ vpu->vsi = mtk_vcodec_fw_map_dm_addr(vpu->ctx->dev->fw_handler,
18
+ msg->vpu_inst_addr);
19
+
20
+ /* Firmware version field value is unspecified on MT8173. */
21
+ if (vpu->ctx->dev->venc_pdata->chip == MTK_MT8173)
22
+ return;
23
+
24
+ /* Check firmware version. */
25
+ mtk_vcodec_debug(vpu, "firmware version: 0x%x\n",
26
+ msg->venc_abi_version);
27
+ switch (msg->venc_abi_version) {
28
+ case 1:
29
+ break;
30
+ default:
31
+ mtk_vcodec_err(vpu, "unhandled firmware version 0x%x\n",
32
+ msg->venc_abi_version);
33
+ vpu->failure = 1;
34
+ break;
35
+ }
2636 }
2737
28
-static void handle_enc_encode_msg(struct venc_vpu_inst *vpu, void *data)
38
+static void handle_enc_encode_msg(struct venc_vpu_inst *vpu, const void *data)
2939 {
30
- struct venc_vpu_ipi_msg_enc *msg = data;
40
+ const struct venc_vpu_ipi_msg_enc *msg = data;
3141
3242 vpu->state = msg->state;
3343 vpu->bs_size = msg->bs_size;
....@@ -36,12 +46,17 @@
3646
3747 static void vpu_enc_ipi_handler(void *data, unsigned int len, void *priv)
3848 {
39
- struct venc_vpu_ipi_msg_common *msg = data;
49
+ const struct venc_vpu_ipi_msg_common *msg = data;
4050 struct venc_vpu_inst *vpu =
4151 (struct venc_vpu_inst *)(unsigned long)msg->venc_inst;
4252
4353 mtk_vcodec_debug(vpu, "msg_id %x inst %p status %d",
4454 msg->msg_id, vpu, msg->status);
55
+
56
+ vpu->signaled = 1;
57
+ vpu->failure = (msg->status != VENC_IPI_MSG_STATUS_OK);
58
+ if (vpu->failure)
59
+ goto failure;
4560
4661 switch (msg->msg_id) {
4762 case VPU_IPIMSG_ENC_INIT_DONE:
....@@ -59,9 +74,7 @@
5974 break;
6075 }
6176
62
- vpu->signaled = 1;
63
- vpu->failure = (msg->status != VENC_IPI_MSG_STATUS_OK);
64
-
77
+failure:
6578 mtk_vcodec_debug_leave(vpu);
6679 }
6780
....@@ -72,12 +85,13 @@
7285
7386 mtk_vcodec_debug_enter(vpu);
7487
75
- if (!vpu->dev) {
88
+ if (!vpu->ctx->dev->fw_handler) {
7689 mtk_vcodec_err(vpu, "inst dev is NULL");
7790 return -EINVAL;
7891 }
7992
80
- status = vpu_ipi_send(vpu->dev, vpu->id, msg, len);
93
+ status = mtk_vcodec_fw_ipi_send(vpu->ctx->dev->fw_handler, vpu->id, msg,
94
+ len, 2000);
8195 if (status) {
8296 mtk_vcodec_err(vpu, "vpu_ipi_send msg_id %x len %d fail %d",
8397 *(uint32_t *)msg, len, status);
....@@ -102,8 +116,9 @@
102116 vpu->signaled = 0;
103117 vpu->failure = 0;
104118
105
- status = vpu_ipi_register(vpu->dev, vpu->id, vpu_enc_ipi_handler,
106
- NULL, NULL);
119
+ status = mtk_vcodec_fw_ipi_register(vpu->ctx->dev->fw_handler, vpu->id,
120
+ vpu_enc_ipi_handler, "venc", NULL);
121
+
107122 if (status) {
108123 mtk_vcodec_err(vpu, "vpu_ipi_register fail %d", status);
109124 return -EINVAL;
....@@ -122,49 +137,81 @@
122137 return 0;
123138 }
124139
140
+static unsigned int venc_enc_param_crop_right(struct venc_vpu_inst *vpu,
141
+ struct venc_enc_param *enc_prm)
142
+{
143
+ unsigned int img_crop_right = enc_prm->buf_width - enc_prm->width;
144
+
145
+ return img_crop_right % 16;
146
+}
147
+
148
+static unsigned int venc_enc_param_crop_bottom(struct venc_enc_param *enc_prm)
149
+{
150
+ return round_up(enc_prm->height, 16) - enc_prm->height;
151
+}
152
+
153
+static unsigned int venc_enc_param_num_mb(struct venc_enc_param *enc_prm)
154
+{
155
+ return DIV_ROUND_UP(enc_prm->width, 16) *
156
+ DIV_ROUND_UP(enc_prm->height, 16);
157
+}
158
+
125159 int vpu_enc_set_param(struct venc_vpu_inst *vpu,
126160 enum venc_set_param_type id,
127161 struct venc_enc_param *enc_param)
128162 {
129
- struct venc_ap_ipi_msg_set_param out;
163
+ const bool is_ext = MTK_ENC_CTX_IS_EXT(vpu->ctx);
164
+ size_t msg_size = is_ext ?
165
+ sizeof(struct venc_ap_ipi_msg_set_param_ext) :
166
+ sizeof(struct venc_ap_ipi_msg_set_param);
167
+ struct venc_ap_ipi_msg_set_param_ext out;
130168
131169 mtk_vcodec_debug(vpu, "id %d ->", id);
132170
133171 memset(&out, 0, sizeof(out));
134
- out.msg_id = AP_IPIMSG_ENC_SET_PARAM;
135
- out.vpu_inst_addr = vpu->inst_addr;
136
- out.param_id = id;
172
+ out.base.msg_id = AP_IPIMSG_ENC_SET_PARAM;
173
+ out.base.vpu_inst_addr = vpu->inst_addr;
174
+ out.base.param_id = id;
137175 switch (id) {
138176 case VENC_SET_PARAM_ENC:
139
- out.data_item = 0;
177
+ if (is_ext) {
178
+ out.base.data_item = 3;
179
+ out.base.data[0] =
180
+ venc_enc_param_crop_right(vpu, enc_param);
181
+ out.base.data[1] =
182
+ venc_enc_param_crop_bottom(enc_param);
183
+ out.base.data[2] = venc_enc_param_num_mb(enc_param);
184
+ } else {
185
+ out.base.data_item = 0;
186
+ }
140187 break;
141188 case VENC_SET_PARAM_FORCE_INTRA:
142
- out.data_item = 0;
189
+ out.base.data_item = 0;
143190 break;
144191 case VENC_SET_PARAM_ADJUST_BITRATE:
145
- out.data_item = 1;
146
- out.data[0] = enc_param->bitrate;
192
+ out.base.data_item = 1;
193
+ out.base.data[0] = enc_param->bitrate;
147194 break;
148195 case VENC_SET_PARAM_ADJUST_FRAMERATE:
149
- out.data_item = 1;
150
- out.data[0] = enc_param->frm_rate;
196
+ out.base.data_item = 1;
197
+ out.base.data[0] = enc_param->frm_rate;
151198 break;
152199 case VENC_SET_PARAM_GOP_SIZE:
153
- out.data_item = 1;
154
- out.data[0] = enc_param->gop_size;
200
+ out.base.data_item = 1;
201
+ out.base.data[0] = enc_param->gop_size;
155202 break;
156203 case VENC_SET_PARAM_INTRA_PERIOD:
157
- out.data_item = 1;
158
- out.data[0] = enc_param->intra_period;
204
+ out.base.data_item = 1;
205
+ out.base.data[0] = enc_param->intra_period;
159206 break;
160207 case VENC_SET_PARAM_SKIP_FRAME:
161
- out.data_item = 0;
208
+ out.base.data_item = 0;
162209 break;
163210 default:
164211 mtk_vcodec_err(vpu, "id %d not supported", id);
165212 return -EINVAL;
166213 }
167
- if (vpu_enc_send_msg(vpu, &out, sizeof(out))) {
214
+ if (vpu_enc_send_msg(vpu, &out, msg_size)) {
168215 mtk_vcodec_err(vpu,
169216 "AP_IPIMSG_ENC_SET_PARAM %d fail", id);
170217 return -EINVAL;
....@@ -178,33 +225,44 @@
178225 int vpu_enc_encode(struct venc_vpu_inst *vpu, unsigned int bs_mode,
179226 struct venc_frm_buf *frm_buf,
180227 struct mtk_vcodec_mem *bs_buf,
181
- unsigned int *bs_size)
228
+ unsigned int *bs_size,
229
+ struct venc_frame_info *frame_info)
182230 {
183
- struct venc_ap_ipi_msg_enc out;
231
+ const bool is_ext = MTK_ENC_CTX_IS_EXT(vpu->ctx);
232
+ size_t msg_size = is_ext ?
233
+ sizeof(struct venc_ap_ipi_msg_enc_ext) :
234
+ sizeof(struct venc_ap_ipi_msg_enc);
235
+ struct venc_ap_ipi_msg_enc_ext out;
184236
185237 mtk_vcodec_debug(vpu, "bs_mode %d ->", bs_mode);
186238
187239 memset(&out, 0, sizeof(out));
188
- out.msg_id = AP_IPIMSG_ENC_ENCODE;
189
- out.vpu_inst_addr = vpu->inst_addr;
190
- out.bs_mode = bs_mode;
240
+ out.base.msg_id = AP_IPIMSG_ENC_ENCODE;
241
+ out.base.vpu_inst_addr = vpu->inst_addr;
242
+ out.base.bs_mode = bs_mode;
191243 if (frm_buf) {
192244 if ((frm_buf->fb_addr[0].dma_addr % 16 == 0) &&
193245 (frm_buf->fb_addr[1].dma_addr % 16 == 0) &&
194246 (frm_buf->fb_addr[2].dma_addr % 16 == 0)) {
195
- out.input_addr[0] = frm_buf->fb_addr[0].dma_addr;
196
- out.input_addr[1] = frm_buf->fb_addr[1].dma_addr;
197
- out.input_addr[2] = frm_buf->fb_addr[2].dma_addr;
247
+ out.base.input_addr[0] = frm_buf->fb_addr[0].dma_addr;
248
+ out.base.input_addr[1] = frm_buf->fb_addr[1].dma_addr;
249
+ out.base.input_addr[2] = frm_buf->fb_addr[2].dma_addr;
198250 } else {
199251 mtk_vcodec_err(vpu, "dma_addr not align to 16");
200252 return -EINVAL;
201253 }
202254 }
203255 if (bs_buf) {
204
- out.bs_addr = bs_buf->dma_addr;
205
- out.bs_size = bs_buf->size;
256
+ out.base.bs_addr = bs_buf->dma_addr;
257
+ out.base.bs_size = bs_buf->size;
206258 }
207
- if (vpu_enc_send_msg(vpu, &out, sizeof(out))) {
259
+ if (is_ext && frame_info) {
260
+ out.data_item = 3;
261
+ out.data[0] = frame_info->frm_count;
262
+ out.data[1] = frame_info->skip_frm_count;
263
+ out.data[2] = frame_info->frm_type;
264
+ }
265
+ if (vpu_enc_send_msg(vpu, &out, msg_size)) {
208266 mtk_vcodec_err(vpu, "AP_IPIMSG_ENC_ENCODE %d fail",
209267 bs_mode);
210268 return -EINVAL;