23 #include <asm/cacheflush.h>
31 #include <linux/sched.h>
33 #define OFFSETA(x) (((x) - dev->bank1) >> MFC_OFFSET_SHIFT)
34 #define OFFSETB(x) (((x) - dev->bank2) >> MFC_OFFSET_SHIFT)
47 mfc_err(
"Allocating DESC buffer failed\n");
50 ctx->
dsc.dma = s5p_mfc_mem_cookie(
58 mfc_err(
"Remapping DESC buffer failed\n");
80 unsigned int enc_ref_y_size = 0;
81 unsigned int enc_ref_c_size = 0;
82 unsigned int guard_width, guard_height;
85 mfc_debug(2,
"Luma size:%d Chroma size:%d MV size:%d\n",
98 enc_ref_c_size =
ALIGN(enc_ref_c_size,
105 enc_ref_c_size =
ALIGN(guard_width * guard_height,
108 mfc_debug(2,
"recon luma size: %d chroma size: %d\n",
109 enc_ref_y_size, enc_ref_c_size);
163 (enc_ref_c_size * 4) +
172 (enc_ref_c_size * 4);
179 (enc_ref_c_size * 4);
191 "Buf alloc for decoding failed (port A)\n");
204 mfc_err(
"Buf alloc for decoding failed (port B)\n");
244 if (IS_ERR(ctx->
ctx.alloc)) {
245 mfc_err(
"Allocating context buffer failed\n");
249 ctx->
ctx.dma = s5p_mfc_mem_cookie(
254 if (!ctx->
ctx.virt) {
255 mfc_err(
"Remapping instance buffer failed\n");
269 if (IS_ERR(ctx->
shm.alloc)) {
270 mfc_err(
"failed to allocate shared memory\n");
271 return PTR_ERR(ctx->
shm.alloc);
274 ctx->
shm.ofs = s5p_mfc_mem_cookie(
280 if (!ctx->
shm.virt) {
284 mfc_err(
"failed to virt addr of shared memory\n");
295 if (ctx->
ctx.alloc) {
302 if (ctx->
shm.alloc) {
329 static unsigned int s5p_mfc_read_info_v5(
struct s5p_mfc_ctx *ctx,
338 unsigned int guard_width, guard_height;
343 "SEQ Done: Movie dimensions %dx%d, buffer dimensions: %dx%d\n",
407 static void s5p_mfc_set_dec_desc_buffer(
struct s5p_mfc_ctx *ctx)
417 static void s5p_mfc_set_shared_buffer(
struct s5p_mfc_ctx *ctx)
425 unsigned int start_num_byte,
unsigned int buf_size)
439 unsigned int frame_size,
i;
440 unsigned int frame_size_ch, frame_size_mv;
443 size_t buf_addr1, buf_addr2;
444 int buf_size1, buf_size2;
454 s5p_mfc_set_shared_buffer(ctx);
523 mfc_err(
"Unknown codec for decoding (%x)\n",
531 mfc_debug(2,
"Frm size: %d ch: %d mv: %d\n", frame_size, frame_size_ch,
540 ctx->
dst_bufs[i].cookie.raw.chroma);
545 buf_addr2, buf_size2);
548 buf_addr2 += frame_size_mv;
549 buf_size2 -= frame_size_mv;
552 mfc_debug(2,
"Buf1: %u, buf_size1: %d\n", buf_addr1, buf_size1);
553 mfc_debug(2,
"Buf 1/2 size after: %d/%d (frames %d)\n",
555 if (buf_size1 < 0 || buf_size2 < 0) {
556 mfc_debug(2,
"Not enough memory has been allocated\n");
571 unsigned long addr,
unsigned int size)
581 unsigned long y_addr,
unsigned long c_addr)
590 unsigned long *y_addr,
unsigned long *c_addr)
604 size_t buf_addr1, buf_addr2;
605 size_t buf_size1, buf_size2;
606 unsigned int enc_ref_y_size, enc_ref_c_size;
607 unsigned int guard_width, guard_height;
626 enc_ref_c_size =
ALIGN(guard_width * guard_height,
629 mfc_debug(2,
"buf_size1: %d, buf_size2: %d\n", buf_size1, buf_size2);
632 for (i = 0; i < 2; i++) {
635 buf_addr1 += enc_ref_y_size;
636 buf_size1 -= enc_ref_y_size;
640 buf_addr2 += enc_ref_y_size;
641 buf_size2 -= enc_ref_y_size;
643 for (i = 0; i < 4; i++) {
646 buf_addr2 += enc_ref_c_size;
647 buf_size2 -= enc_ref_c_size;
668 mfc_debug(2,
"buf_size1: %d, buf_size2: %d\n",
669 buf_size1, buf_size2);
672 for (i = 0; i < 2; i++) {
675 buf_addr1 += enc_ref_y_size;
676 buf_size1 -= enc_ref_y_size;
679 buf_addr2 += enc_ref_y_size;
680 buf_size2 -= enc_ref_y_size;
682 for (i = 0; i < 4; i++) {
685 buf_addr2 += enc_ref_c_size;
686 buf_size2 -= enc_ref_c_size;
699 mfc_debug(2,
"buf_size1: %d, buf_size2: %d\n",
700 buf_size1, buf_size2);
703 for (i = 0; i < 2; i++) {
706 buf_addr1 += enc_ref_y_size;
707 buf_size1 -= enc_ref_y_size;
710 buf_addr2 += enc_ref_y_size;
711 buf_size2 -= enc_ref_y_size;
713 for (i = 0; i < 4; i++) {
716 buf_addr2 += enc_ref_c_size;
717 buf_size2 -= enc_ref_c_size;
725 mfc_debug(2,
"buf_size1: %d, buf_size2: %d\n",
726 buf_size1, buf_size2);
729 mfc_err(
"Unknown codec set for encoding: %d\n",
736 static int s5p_mfc_set_enc_params(
struct s5p_mfc_ctx *ctx)
778 reg &= ~(0xFF << 16);
819 static int s5p_mfc_set_enc_params_h264(
struct s5p_mfc_ctx *ctx)
827 s5p_mfc_set_enc_params(ctx);
838 reg |= (p_264->
level << 8);
888 reg |= (p->
rc_mb << 8);
947 shm &= ~(0xFFFFFFFF);
969 shm &= ~(0xFFFF << 16);
976 static int s5p_mfc_set_enc_params_mpeg4(
struct s5p_mfc_ctx *ctx)
983 unsigned int framerate;
985 s5p_mfc_set_enc_params(ctx);
996 reg |= (p_mpeg4->
level << 8);
1019 shm &= ~(0xFFFFFFFF);
1037 reg &= ~(0x3F << 8);
1048 shm &= ~(0xFFFF << 16);
1055 static int s5p_mfc_set_enc_params_h263(
struct s5p_mfc_ctx *ctx)
1063 s5p_mfc_set_enc_params(ctx);
1086 reg &= ~(0x3F << 8);
1097 shm &= ~(0xFFFF << 16);
1109 s5p_mfc_set_shared_buffer(ctx);
1126 static void s5p_mfc_set_flush(
struct s5p_mfc_ctx *ctx,
int flush)
1147 s5p_mfc_set_shared_buffer(ctx);
1151 switch (last_frame) {
1166 mfc_debug(2,
"Decoding a usual frame\n");
1175 s5p_mfc_set_enc_params_h264(ctx);
1177 s5p_mfc_set_enc_params_mpeg4(ctx);
1179 s5p_mfc_set_enc_params_h263(ctx);
1181 mfc_err(
"Unknown codec for encoding (%x)\n",
1185 s5p_mfc_set_shared_buffer(ctx);
1201 s5p_mfc_set_shared_buffer(ctx);
1213 static int s5p_mfc_get_new_ctx(
struct s5p_mfc_dev *dev)
1215 unsigned long flags;
1226 spin_unlock_irqrestore(&dev->
condlock, flags);
1230 spin_unlock_irqrestore(&dev->
condlock, flags);
1234 static void s5p_mfc_run_res_change(
struct s5p_mfc_ctx *ctx)
1244 static int s5p_mfc_run_dec_frame(
struct s5p_mfc_ctx *ctx,
int last_frame)
1248 unsigned long flags;
1255 spin_unlock_irqrestore(&dev->
irqlock, flags);
1262 vb2_dma_contig_plane_dma_addr(temp_vb->
b, 0),
1264 spin_unlock_irqrestore(&dev->
irqlock, flags);
1265 index = temp_vb->
b->v4l2_buf.index;
1268 if (temp_vb->
b->v4l2_planes[0].bytesused == 0) {
1270 mfc_debug(2,
"Setting ctx->state to FINISHING\n");
1277 static int s5p_mfc_run_enc_frame(
struct s5p_mfc_ctx *ctx)
1280 unsigned long flags;
1283 unsigned long src_y_addr, src_c_addr,
dst_addr;
1289 spin_unlock_irqrestore(&dev->
irqlock, flags);
1294 spin_unlock_irqrestore(&dev->
irqlock, flags);
1305 if (src_mb->
b->v4l2_planes[0].bytesused == 0) {
1311 src_y_addr = vb2_dma_contig_plane_dma_addr(src_mb->
b,
1313 src_c_addr = vb2_dma_contig_plane_dma_addr(src_mb->
b,
1323 dst_addr = vb2_dma_contig_plane_dma_addr(dst_mb->
b, 0);
1324 dst_size = vb2_plane_size(dst_mb->
b, 0);
1326 spin_unlock_irqrestore(&dev->
irqlock, flags);
1329 mfc_debug(2,
"encoding buffer with index=%d state=%d",
1330 src_mb ? src_mb->
b->v4l2_buf.index : -1, ctx->
state);
1335 static void s5p_mfc_run_init_dec(
struct s5p_mfc_ctx *ctx)
1338 unsigned long flags;
1343 mfc_debug(2,
"Preparing to init decoding\n");
1345 s5p_mfc_set_dec_desc_buffer(ctx);
1346 mfc_debug(2,
"Header size: %d\n", temp_vb->
b->v4l2_planes[0].bytesused);
1348 vb2_dma_contig_plane_dma_addr(temp_vb->
b, 0),
1349 0, temp_vb->
b->v4l2_planes[0].bytesused);
1350 spin_unlock_irqrestore(&dev->
irqlock, flags);
1356 static void s5p_mfc_run_init_enc(
struct s5p_mfc_ctx *ctx)
1359 unsigned long flags;
1367 dst_addr = vb2_dma_contig_plane_dma_addr(dst_mb->
b, 0);
1368 dst_size = vb2_plane_size(dst_mb->
b, 0);
1370 spin_unlock_irqrestore(&dev->
irqlock, flags);
1376 static int s5p_mfc_run_init_dec_buffers(
struct s5p_mfc_ctx *ctx)
1379 unsigned long flags;
1388 mfc_err(
"It seems that not all destionation buffers were "
1389 "mmaped\nMFC requires that all destination are mmaped "
1390 "before starting processing\n");
1395 mfc_err(
"Header has been deallocated in the middle of"
1396 " initialization\n");
1397 spin_unlock_irqrestore(&dev->
irqlock, flags);
1401 mfc_debug(2,
"Header size: %d\n", temp_vb->
b->v4l2_planes[0].bytesused);
1403 vb2_dma_contig_plane_dma_addr(temp_vb->
b, 0),
1404 0, temp_vb->
b->v4l2_planes[0].bytesused);
1405 spin_unlock_irqrestore(&dev->
irqlock, flags);
1410 mfc_err(
"Failed to alloc frame mem\n");
1421 unsigned int ret = 0;
1424 mfc_debug(1,
"Entering suspend so do not schedule any jobs\n");
1434 new_ctx = s5p_mfc_get_new_ctx(dev);
1438 mfc_err(
"Failed to unlock hardware\n");
1441 mfc_debug(1,
"No ctx is scheduled to be run\n");
1444 ctx = dev->
ctx[new_ctx];
1452 s5p_mfc_set_dec_desc_buffer(ctx);
1453 switch (ctx->
state) {
1471 s5p_mfc_run_init_dec(ctx);
1474 ret = s5p_mfc_run_init_dec_buffers(ctx);
1478 s5p_mfc_run_res_change(ctx);
1484 mfc_debug(2,
"Finished remaining frames after resolution change\n");
1486 mfc_debug(2,
"Will re-init the codec\n");
1487 s5p_mfc_run_init_dec(ctx);
1493 switch (ctx->
state) {
1496 ret = s5p_mfc_run_enc_frame(ctx);
1509 s5p_mfc_run_init_enc(ctx);
1522 mfc_err(
"Failed to unlock hardware\n");
1538 while (!list_empty(lh)) {
1540 for (i = 0; i < b->
b->num_planes; i++)
1541 vb2_set_plane_payload(b->
b, i, 0);
1760 .write_info = s5p_mfc_write_info_v5,
1761 .read_info = s5p_mfc_read_info_v5,
1793 return &s5p_mfc_ops_v5;