23 #include <linux/sched.h>
26 #include <asm/cacheflush.h>
37 #ifdef S5P_MFC_DEBUG_REGWRITE
39 #define writel(v, r) \
41 pr_err("MFCWRITE(%p): %08x\n", r, (unsigned int)v); \
46 #define READL(offset) readl(dev->regs_base + (offset))
47 #define WRITEL(data, offset) writel((data), dev->regs_base + (offset))
48 #define OFFSETA(x) (((x) - dev->port_a) >> S5P_FIMV_MEM_OFFSET)
49 #define OFFSETB(x) (((x) - dev->port_b) >> S5P_FIMV_MEM_OFFSET)
75 unsigned int mb_width, mb_height;
81 mfc_debug(2,
"Luma size:%d Chroma size:%d MV size:%d\n",
99 mfc_debug(2,
"recon luma size: %d chroma size: %d\n",
197 pr_err(
"Buf alloc for decoding failed (port A)\n");
255 if (IS_ERR(ctx->
ctx.alloc)) {
256 mfc_err(
"Allocating context buffer failed.\n");
257 return PTR_ERR(ctx->
ctx.alloc);
260 ctx->
ctx.dma = s5p_mfc_mem_cookie(
264 if (!ctx->
ctx.virt) {
270 mfc_err(
"Remapping context buffer failed.\n");
287 if (ctx->
ctx.alloc) {
306 if (IS_ERR(dev->
ctx_buf.alloc)) {
307 mfc_err(
"Allocating DESC buffer failed.\n");
308 return PTR_ERR(dev->
ctx_buf.alloc);
311 dev->
ctx_buf.dma = s5p_mfc_mem_cookie(
321 mfc_err(
"Remapping DESC buffer failed.\n");
352 mbY = (mbY + 1) / 2 * 2;
362 mfc_debug(2,
"SEQ Done: Movie dimensions %dx%d,\n"
363 "buffer dimensions: %dx%d\n", ctx->
img_width,
380 unsigned int mb_width, mb_height;
392 unsigned int start_num_byte,
unsigned int strm_size)
398 mfc_debug(2,
"inst_no: %d, buf_addr: 0x%08x,\n"
399 "buf_size: 0x%08x (%d)\n",
400 ctx->
inst_no, buf_addr, strm_size, strm_size);
413 unsigned int frame_size,
i;
414 unsigned int frame_size_ch, frame_size_mv;
423 mfc_debug(2,
"Buf1: %p (%d)\n", (
void *)buf_addr1, buf_size1);
445 mfc_debug(2,
"Frame size: %d ch: %d mv: %d\n",
446 frame_size, frame_size_ch, frame_size_mv);
455 ctx->
dst_bufs[i].cookie.raw.chroma);
461 for (i = 0; i < ctx->
mv_count; i++) {
463 align_gap = buf_addr1;
464 buf_addr1 =
ALIGN(buf_addr1, 16);
465 align_gap = buf_addr1 - align_gap;
466 buf_size1 -= align_gap;
469 buf_addr1, buf_size1);
471 buf_addr1 += frame_size_mv;
472 buf_size1 -= frame_size_mv;
476 mfc_debug(2,
"Buf1: %u, buf_size1: %d (frames %d)\n",
479 mfc_debug(2,
"Not enough memory has been allocated.\n");
487 mfc_debug(2,
"After setting buffers.\n");
493 unsigned long addr,
unsigned int size)
500 mfc_debug(2,
"stream buf addr: 0x%08lx, size: 0x%d",
507 unsigned long y_addr,
unsigned long c_addr)
514 mfc_debug(2,
"enc src y buf addr: 0x%08lx", y_addr);
515 mfc_debug(2,
"enc src c buf addr: 0x%08lx", c_addr);
519 unsigned long *y_addr,
unsigned long *c_addr)
522 unsigned long enc_recon_y_addr, enc_recon_c_addr;
530 mfc_debug(2,
"recon y addr: 0x%08lx", enc_recon_y_addr);
531 mfc_debug(2,
"recon c addr: 0x%08lx", enc_recon_c_addr);
538 size_t buf_addr1, buf_size1;
546 mfc_debug(2,
"Buf1: %p (%d)\n", (
void *)buf_addr1, buf_size1);
570 mfc_debug(2,
"Buf1: %u, buf_size1: %d (ref frames %d)\n",
573 mfc_debug(2,
"Not enough memory has been allocated.\n");
606 static int s5p_mfc_set_enc_params(
struct s5p_mfc_ctx *ctx)
610 unsigned int reg = 0;
648 s5p_mfc_set_slice_mode(ctx);
701 reg |= ((p->
pad_cr & 0xFF) << 16);
703 reg |= ((p->
pad_cb & 0xFF) << 8);
774 static int s5p_mfc_set_enc_params_h264(
struct s5p_mfc_ctx *ctx)
779 unsigned int reg = 0;
784 s5p_mfc_set_enc_params(ctx);
795 reg |= ((p_h264->
level & 0xFF) << 8);
804 reg |= ((p->
rc_mb & 0x1) << 8);
814 reg |= ((p_h264->
rc_max_qp & 0x3F) << 8);
923 reg |= ((p_h264->
vui_sar & 0x1) << 5);
946 reg |= ((p_h264->
open_gop & 0x1) << 4);
969 reg |= ((p_h264->
aso & 0x1) << 6);
975 reg |= ((p_h264->
open_gop & 0x1) << 8);
1030 mfc_err(
"Unsupported map type for FMO: %d\n",
1050 static int s5p_mfc_set_enc_params_mpeg4(
struct s5p_mfc_ctx *ctx)
1055 unsigned int reg = 0;
1059 s5p_mfc_set_enc_params(ctx);
1063 reg &= ~(0x3 << 16);
1070 reg |= ((p_mpeg4->
level & 0xFF) << 8);
1072 reg |= p_mpeg4->
profile & 0x3F;
1079 reg |= ((p->
rc_mb & 0x1) << 8);
1089 reg |= ((p_mpeg4->
rc_max_qp & 0x3F) << 8);
1130 static int s5p_mfc_set_enc_params_h263(
struct s5p_mfc_ctx *ctx)
1135 unsigned int reg = 0;
1139 s5p_mfc_set_enc_params(ctx);
1151 reg |= ((p->
rc_mb & 0x1) << 8);
1161 reg |= ((p_h263->
rc_max_qp & 0x3F) << 8);
1202 unsigned int reg = 0;
1203 int fmo_aso_ctrl = 0;
1226 mfc_debug(2,
"Set loop filter to: %d\n",
1253 static inline void s5p_mfc_set_flush(
struct s5p_mfc_ctx *ctx,
int flush)
1276 switch (last_frame) {
1286 mfc_err(
"Unsupported last frame arg.\n");
1290 mfc_debug(2,
"Decoding a usual frame.\n");
1299 s5p_mfc_set_enc_params_h264(ctx);
1301 s5p_mfc_set_enc_params_mpeg4(ctx);
1303 s5p_mfc_set_enc_params_h263(ctx);
1305 mfc_err(
"Unknown codec for encoding (%x).\n",
1325 for (i = 0; i < 8; i++)
1344 s5p_mfc_set_slice_mode(ctx);
1355 static inline int s5p_mfc_get_new_ctx(
struct s5p_mfc_dev *dev)
1357 unsigned long flags;
1371 spin_unlock_irqrestore(&dev->
condlock, flags);
1375 spin_unlock_irqrestore(&dev->
condlock, flags);
1379 static inline void s5p_mfc_run_dec_last_frames(
struct s5p_mfc_ctx *ctx)
1383 unsigned long flags;
1390 spin_unlock_irqrestore(&dev->
irqlock, flags);
1397 vb2_dma_contig_plane_dma_addr(temp_vb->
b, 0), 0, 0);
1398 spin_unlock_irqrestore(&dev->
irqlock, flags);
1405 static inline int s5p_mfc_run_dec_frame(
struct s5p_mfc_ctx *ctx)
1409 unsigned long flags;
1418 spin_unlock_irqrestore(&dev->
irqlock, flags);
1425 vb2_dma_contig_plane_dma_addr(temp_vb->
b, 0),
1427 temp_vb->
b->v4l2_planes[0].bytesused);
1428 spin_unlock_irqrestore(&dev->
irqlock, flags);
1430 index = temp_vb->
b->v4l2_buf.index;
1434 if (temp_vb->
b->v4l2_planes[0].bytesused == 0) {
1436 mfc_debug(2,
"Setting ctx->state to FINISHING\n");
1444 static inline int s5p_mfc_run_enc_frame(
struct s5p_mfc_ctx *ctx)
1447 unsigned long flags;
1450 unsigned long src_y_addr, src_c_addr,
dst_addr;
1461 spin_unlock_irqrestore(&dev->
irqlock, flags);
1467 spin_unlock_irqrestore(&dev->
irqlock, flags);
1473 src_y_addr = vb2_dma_contig_plane_dma_addr(src_mb->
b, 0);
1474 src_c_addr = vb2_dma_contig_plane_dma_addr(src_mb->
b, 1);
1476 mfc_debug(2,
"enc src y addr: 0x%08lx", src_y_addr);
1477 mfc_debug(2,
"enc src c addr: 0x%08lx", src_c_addr);
1483 dst_addr = vb2_dma_contig_plane_dma_addr(dst_mb->
b, 0);
1484 dst_size = vb2_plane_size(dst_mb->
b, 0);
1488 spin_unlock_irqrestore(&dev->
irqlock, flags);
1490 index = src_mb->
b->v4l2_buf.index;
1499 static inline void s5p_mfc_run_init_dec(
struct s5p_mfc_ctx *ctx)
1502 unsigned long flags;
1507 mfc_debug(2,
"Preparing to init decoding.\n");
1509 mfc_debug(2,
"Header size: %d\n", temp_vb->
b->v4l2_planes[0].bytesused);
1511 vb2_dma_contig_plane_dma_addr(temp_vb->
b, 0), 0,
1512 temp_vb->
b->v4l2_planes[0].bytesused);
1513 spin_unlock_irqrestore(&dev->
irqlock, flags);
1519 static inline void s5p_mfc_run_init_enc(
struct s5p_mfc_ctx *ctx)
1522 unsigned long flags;
1530 dst_addr = vb2_dma_contig_plane_dma_addr(dst_mb->
b, 0);
1531 dst_size = vb2_plane_size(dst_mb->
b, 0);
1533 spin_unlock_irqrestore(&dev->
irqlock, flags);
1539 static inline int s5p_mfc_run_init_dec_buffers(
struct s5p_mfc_ctx *ctx)
1548 mfc_err(
"It seems that not all destionation buffers were\n"
1549 "mmaped.MFC requires that all destination are mmaped\n"
1550 "before starting processing.\n");
1558 mfc_err(
"Failed to alloc frame mem.\n");
1564 static inline int s5p_mfc_run_init_enc_buffers(
struct s5p_mfc_ctx *ctx)
1571 mfc_err(
"Failed to allocate encoding buffers.\n");
1579 mfc_err(
"It seems that destionation buffers were not\n"
1580 "requested.MFC requires that header should be generated\n"
1581 "before allocating codec buffer.\n");
1589 mfc_err(
"Failed to alloc frame mem.\n");
1600 unsigned int ret = 0;
1612 new_ctx = s5p_mfc_get_new_ctx(dev);
1616 mfc_err(
"Failed to unlock hardware.\n");
1620 mfc_debug(1,
"No ctx is scheduled to be run.\n");
1624 mfc_debug(1,
"New context: %d\n", new_ctx);
1625 ctx = dev->
ctx[new_ctx];
1626 mfc_debug(1,
"Seting new context to %p\n", ctx);
1628 mfc_debug(1,
"ctx->dst_queue_cnt=%d ctx->dpb_count=%d ctx->src_queue_cnt=%d\n",
1636 switch (ctx->
state) {
1638 s5p_mfc_run_dec_last_frames(ctx);
1641 ret = s5p_mfc_run_dec_frame(ctx);
1654 s5p_mfc_run_init_dec(ctx);
1657 ret = s5p_mfc_run_init_dec_buffers(ctx);
1660 s5p_mfc_run_dec_last_frames(ctx);
1663 s5p_mfc_run_dec_last_frames(ctx);
1666 mfc_debug(2,
"Finished remaining frames after resolution change.\n");
1668 mfc_debug(2,
"Will re-init the codec`.\n");
1669 s5p_mfc_run_init_dec(ctx);
1675 switch (ctx->
state) {
1678 ret = s5p_mfc_run_enc_frame(ctx);
1689 s5p_mfc_run_init_enc(ctx);
1692 ret = s5p_mfc_run_init_enc_buffers(ctx);
1705 mfc_err(
"Failed to unlock hardware.\n");
1721 while (!list_empty(lh)) {
1723 for (i = 0; i < b->
b->num_planes; i++)
1724 vb2_set_plane_payload(b->
b, i, 0);
1904 .alloc_dev_context_buffer =
1906 .release_dev_context_buffer =
1955 return &s5p_mfc_ops_v6;