Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Coda multi-standard codec IP - BIT processor functions |
| 3 | * |
| 4 | * Copyright (C) 2012 Vista Silicon S.L. |
| 5 | * Javier Martin, <javier.martin@vista-silicon.com> |
| 6 | * Xavier Duret |
| 7 | * Copyright (C) 2012-2014 Philipp Zabel, Pengutronix |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/clk.h> |
| 16 | #include <linux/irqreturn.h> |
| 17 | #include <linux/kernel.h> |
Philipp Zabel | ad532d3 | 2015-03-24 14:30:52 -0300 | [diff] [blame] | 18 | #include <linux/log2.h> |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/reset.h> |
Fabio Estevam | c0aaf69 | 2014-09-19 08:32:30 -0300 | [diff] [blame] | 21 | #include <linux/slab.h> |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 22 | #include <linux/videodev2.h> |
| 23 | |
| 24 | #include <media/v4l2-common.h> |
| 25 | #include <media/v4l2-ctrls.h> |
| 26 | #include <media/v4l2-fh.h> |
| 27 | #include <media/v4l2-mem2mem.h> |
| 28 | #include <media/videobuf2-core.h> |
| 29 | #include <media/videobuf2-dma-contig.h> |
| 30 | #include <media/videobuf2-vmalloc.h> |
| 31 | |
| 32 | #include "coda.h" |
| 33 | |
Philipp Zabel | 650b939 | 2015-03-24 14:30:53 -0300 | [diff] [blame^] | 34 | #define CODA_PARA_BUF_SIZE (10 * 1024) |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 35 | #define CODA7_PS_BUF_SIZE 0x28000 |
| 36 | #define CODA9_PS_SAVE_SIZE (512 * 1024) |
| 37 | |
| 38 | #define CODA_DEFAULT_GAMMA 4096 |
| 39 | #define CODA9_DEFAULT_GAMMA 24576 /* 0.75 * 32768 */ |
| 40 | |
Philipp Zabel | ad532d3 | 2015-03-24 14:30:52 -0300 | [diff] [blame] | 41 | static void coda_free_bitstream_buffer(struct coda_ctx *ctx); |
| 42 | |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 43 | static inline int coda_is_initialized(struct coda_dev *dev) |
| 44 | { |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 45 | return coda_read(dev, CODA_REG_BIT_CUR_PC) != 0; |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | static inline unsigned long coda_isbusy(struct coda_dev *dev) |
| 49 | { |
| 50 | return coda_read(dev, CODA_REG_BIT_BUSY); |
| 51 | } |
| 52 | |
| 53 | static int coda_wait_timeout(struct coda_dev *dev) |
| 54 | { |
| 55 | unsigned long timeout = jiffies + msecs_to_jiffies(1000); |
| 56 | |
| 57 | while (coda_isbusy(dev)) { |
| 58 | if (time_after(jiffies, timeout)) |
| 59 | return -ETIMEDOUT; |
| 60 | } |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | static void coda_command_async(struct coda_ctx *ctx, int cmd) |
| 65 | { |
| 66 | struct coda_dev *dev = ctx->dev; |
| 67 | |
| 68 | if (dev->devtype->product == CODA_960 || |
| 69 | dev->devtype->product == CODA_7541) { |
| 70 | /* Restore context related registers to CODA */ |
| 71 | coda_write(dev, ctx->bit_stream_param, |
| 72 | CODA_REG_BIT_BIT_STREAM_PARAM); |
| 73 | coda_write(dev, ctx->frm_dis_flg, |
| 74 | CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx)); |
| 75 | coda_write(dev, ctx->frame_mem_ctrl, |
| 76 | CODA_REG_BIT_FRAME_MEM_CTRL); |
| 77 | coda_write(dev, ctx->workbuf.paddr, CODA_REG_BIT_WORK_BUF_ADDR); |
| 78 | } |
| 79 | |
| 80 | if (dev->devtype->product == CODA_960) { |
| 81 | coda_write(dev, 1, CODA9_GDI_WPROT_ERR_CLR); |
| 82 | coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN); |
| 83 | } |
| 84 | |
| 85 | coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY); |
| 86 | |
| 87 | coda_write(dev, ctx->idx, CODA_REG_BIT_RUN_INDEX); |
| 88 | coda_write(dev, ctx->params.codec_mode, CODA_REG_BIT_RUN_COD_STD); |
| 89 | coda_write(dev, ctx->params.codec_mode_aux, CODA7_REG_BIT_RUN_AUX_STD); |
| 90 | |
| 91 | coda_write(dev, cmd, CODA_REG_BIT_RUN_COMMAND); |
| 92 | } |
| 93 | |
| 94 | static int coda_command_sync(struct coda_ctx *ctx, int cmd) |
| 95 | { |
| 96 | struct coda_dev *dev = ctx->dev; |
| 97 | |
| 98 | coda_command_async(ctx, cmd); |
| 99 | return coda_wait_timeout(dev); |
| 100 | } |
| 101 | |
| 102 | int coda_hw_reset(struct coda_ctx *ctx) |
| 103 | { |
| 104 | struct coda_dev *dev = ctx->dev; |
| 105 | unsigned long timeout; |
| 106 | unsigned int idx; |
| 107 | int ret; |
| 108 | |
| 109 | if (!dev->rstc) |
| 110 | return -ENOENT; |
| 111 | |
| 112 | idx = coda_read(dev, CODA_REG_BIT_RUN_INDEX); |
| 113 | |
| 114 | if (dev->devtype->product == CODA_960) { |
| 115 | timeout = jiffies + msecs_to_jiffies(100); |
| 116 | coda_write(dev, 0x11, CODA9_GDI_BUS_CTRL); |
| 117 | while (coda_read(dev, CODA9_GDI_BUS_STATUS) != 0x77) { |
| 118 | if (time_after(jiffies, timeout)) |
| 119 | return -ETIME; |
| 120 | cpu_relax(); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | ret = reset_control_reset(dev->rstc); |
| 125 | if (ret < 0) |
| 126 | return ret; |
| 127 | |
| 128 | if (dev->devtype->product == CODA_960) |
| 129 | coda_write(dev, 0x00, CODA9_GDI_BUS_CTRL); |
| 130 | coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY); |
| 131 | coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN); |
| 132 | ret = coda_wait_timeout(dev); |
| 133 | coda_write(dev, idx, CODA_REG_BIT_RUN_INDEX); |
| 134 | |
| 135 | return ret; |
| 136 | } |
| 137 | |
| 138 | static void coda_kfifo_sync_from_device(struct coda_ctx *ctx) |
| 139 | { |
| 140 | struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo; |
| 141 | struct coda_dev *dev = ctx->dev; |
| 142 | u32 rd_ptr; |
| 143 | |
| 144 | rd_ptr = coda_read(dev, CODA_REG_BIT_RD_PTR(ctx->reg_idx)); |
| 145 | kfifo->out = (kfifo->in & ~kfifo->mask) | |
| 146 | (rd_ptr - ctx->bitstream.paddr); |
| 147 | if (kfifo->out > kfifo->in) |
| 148 | kfifo->out -= kfifo->mask + 1; |
| 149 | } |
| 150 | |
| 151 | static void coda_kfifo_sync_to_device_full(struct coda_ctx *ctx) |
| 152 | { |
| 153 | struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo; |
| 154 | struct coda_dev *dev = ctx->dev; |
| 155 | u32 rd_ptr, wr_ptr; |
| 156 | |
| 157 | rd_ptr = ctx->bitstream.paddr + (kfifo->out & kfifo->mask); |
| 158 | coda_write(dev, rd_ptr, CODA_REG_BIT_RD_PTR(ctx->reg_idx)); |
| 159 | wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask); |
| 160 | coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx)); |
| 161 | } |
| 162 | |
| 163 | static void coda_kfifo_sync_to_device_write(struct coda_ctx *ctx) |
| 164 | { |
| 165 | struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo; |
| 166 | struct coda_dev *dev = ctx->dev; |
| 167 | u32 wr_ptr; |
| 168 | |
| 169 | wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask); |
| 170 | coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx)); |
| 171 | } |
| 172 | |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 173 | static int coda_bitstream_queue(struct coda_ctx *ctx, |
| 174 | struct vb2_buffer *src_buf) |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 175 | { |
| 176 | u32 src_size = vb2_get_plane_payload(src_buf, 0); |
| 177 | u32 n; |
| 178 | |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 179 | n = kfifo_in(&ctx->bitstream_fifo, vb2_plane_vaddr(src_buf, 0), |
| 180 | src_size); |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 181 | if (n < src_size) |
| 182 | return -ENOSPC; |
| 183 | |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 184 | dma_sync_single_for_device(&ctx->dev->plat_dev->dev, |
| 185 | ctx->bitstream.paddr, ctx->bitstream.size, |
| 186 | DMA_TO_DEVICE); |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 187 | |
| 188 | src_buf->v4l2_buf.sequence = ctx->qsequence++; |
| 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | static bool coda_bitstream_try_queue(struct coda_ctx *ctx, |
| 194 | struct vb2_buffer *src_buf) |
| 195 | { |
| 196 | int ret; |
| 197 | |
| 198 | if (coda_get_bitstream_payload(ctx) + |
| 199 | vb2_get_plane_payload(src_buf, 0) + 512 >= ctx->bitstream.size) |
| 200 | return false; |
| 201 | |
| 202 | if (vb2_plane_vaddr(src_buf, 0) == NULL) { |
| 203 | v4l2_err(&ctx->dev->v4l2_dev, "trying to queue empty buffer\n"); |
| 204 | return true; |
| 205 | } |
| 206 | |
| 207 | ret = coda_bitstream_queue(ctx, src_buf); |
| 208 | if (ret < 0) { |
| 209 | v4l2_err(&ctx->dev->v4l2_dev, "bitstream buffer overflow\n"); |
| 210 | return false; |
| 211 | } |
| 212 | /* Sync read pointer to device */ |
| 213 | if (ctx == v4l2_m2m_get_curr_priv(ctx->dev->m2m_dev)) |
| 214 | coda_kfifo_sync_to_device_write(ctx); |
| 215 | |
| 216 | ctx->hold = false; |
| 217 | |
| 218 | return true; |
| 219 | } |
| 220 | |
| 221 | void coda_fill_bitstream(struct coda_ctx *ctx) |
| 222 | { |
| 223 | struct vb2_buffer *src_buf; |
Philipp Zabel | 7cbb105 | 2014-10-02 14:08:32 -0300 | [diff] [blame] | 224 | struct coda_buffer_meta *meta; |
| 225 | u32 start; |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 226 | |
| 227 | while (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) > 0) { |
Philipp Zabel | 619165628d | 2014-10-02 14:08:34 -0300 | [diff] [blame] | 228 | /* |
| 229 | * Only queue a single JPEG into the bitstream buffer, except |
| 230 | * to increase payload over 512 bytes or if in hold state. |
| 231 | */ |
| 232 | if (ctx->codec->src_fourcc == V4L2_PIX_FMT_JPEG && |
| 233 | (coda_get_bitstream_payload(ctx) >= 512) && !ctx->hold) |
| 234 | break; |
| 235 | |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 236 | src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); |
| 237 | |
Philipp Zabel | edc16cb | 2014-10-08 13:09:27 -0300 | [diff] [blame] | 238 | /* Drop frames that do not start/end with a SOI/EOI markers */ |
| 239 | if (ctx->codec->src_fourcc == V4L2_PIX_FMT_JPEG && |
| 240 | !coda_jpeg_check_buffer(ctx, src_buf)) { |
| 241 | v4l2_err(&ctx->dev->v4l2_dev, |
| 242 | "dropping invalid JPEG frame\n"); |
| 243 | src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx); |
| 244 | v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR); |
| 245 | continue; |
| 246 | } |
| 247 | |
Philipp Zabel | 7cbb105 | 2014-10-02 14:08:32 -0300 | [diff] [blame] | 248 | /* Buffer start position */ |
| 249 | start = ctx->bitstream_fifo.kfifo.in & |
| 250 | ctx->bitstream_fifo.kfifo.mask; |
| 251 | |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 252 | if (coda_bitstream_try_queue(ctx, src_buf)) { |
| 253 | /* |
| 254 | * Source buffer is queued in the bitstream ringbuffer; |
| 255 | * queue the timestamp and mark source buffer as done |
| 256 | */ |
| 257 | src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx); |
| 258 | |
Philipp Zabel | 7cbb105 | 2014-10-02 14:08:32 -0300 | [diff] [blame] | 259 | meta = kmalloc(sizeof(*meta), GFP_KERNEL); |
| 260 | if (meta) { |
| 261 | meta->sequence = src_buf->v4l2_buf.sequence; |
| 262 | meta->timecode = src_buf->v4l2_buf.timecode; |
| 263 | meta->timestamp = src_buf->v4l2_buf.timestamp; |
| 264 | meta->start = start; |
| 265 | meta->end = ctx->bitstream_fifo.kfifo.in & |
| 266 | ctx->bitstream_fifo.kfifo.mask; |
| 267 | list_add_tail(&meta->list, |
| 268 | &ctx->buffer_meta_list); |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE); |
| 272 | } else { |
| 273 | break; |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | void coda_bit_stream_end_flag(struct coda_ctx *ctx) |
| 279 | { |
| 280 | struct coda_dev *dev = ctx->dev; |
| 281 | |
| 282 | ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG; |
| 283 | |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 284 | /* If this context is currently running, update the hardware flag */ |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 285 | if ((dev->devtype->product == CODA_960) && |
| 286 | coda_isbusy(dev) && |
| 287 | (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) { |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 288 | coda_write(dev, ctx->bit_stream_param, |
| 289 | CODA_REG_BIT_BIT_STREAM_PARAM); |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
| 293 | static void coda_parabuf_write(struct coda_ctx *ctx, int index, u32 value) |
| 294 | { |
| 295 | struct coda_dev *dev = ctx->dev; |
| 296 | u32 *p = ctx->parabuf.vaddr; |
| 297 | |
| 298 | if (dev->devtype->product == CODA_DX6) |
| 299 | p[index] = value; |
| 300 | else |
| 301 | p[index ^ 1] = value; |
| 302 | } |
| 303 | |
Philipp Zabel | 650b939 | 2015-03-24 14:30:53 -0300 | [diff] [blame^] | 304 | static inline int coda_alloc_context_buf(struct coda_ctx *ctx, |
| 305 | struct coda_aux_buf *buf, size_t size, |
| 306 | const char *name) |
| 307 | { |
| 308 | return coda_alloc_aux_buf(ctx->dev, buf, size, name, ctx->debugfs_entry); |
| 309 | } |
| 310 | |
| 311 | |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 312 | static void coda_free_framebuffers(struct coda_ctx *ctx) |
| 313 | { |
| 314 | int i; |
| 315 | |
| 316 | for (i = 0; i < CODA_MAX_FRAMEBUFFERS; i++) |
| 317 | coda_free_aux_buf(ctx->dev, &ctx->internal_frames[i]); |
| 318 | } |
| 319 | |
| 320 | static int coda_alloc_framebuffers(struct coda_ctx *ctx, |
| 321 | struct coda_q_data *q_data, u32 fourcc) |
| 322 | { |
| 323 | struct coda_dev *dev = ctx->dev; |
| 324 | int width, height; |
| 325 | dma_addr_t paddr; |
| 326 | int ysize; |
| 327 | int ret; |
| 328 | int i; |
| 329 | |
| 330 | if (ctx->codec && (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 || |
| 331 | ctx->codec->dst_fourcc == V4L2_PIX_FMT_H264)) { |
| 332 | width = round_up(q_data->width, 16); |
| 333 | height = round_up(q_data->height, 16); |
| 334 | } else { |
| 335 | width = round_up(q_data->width, 8); |
| 336 | height = q_data->height; |
| 337 | } |
| 338 | ysize = width * height; |
| 339 | |
| 340 | /* Allocate frame buffers */ |
| 341 | for (i = 0; i < ctx->num_internal_frames; i++) { |
| 342 | size_t size; |
| 343 | char *name; |
| 344 | |
| 345 | size = ysize + ysize / 2; |
| 346 | if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 && |
| 347 | dev->devtype->product != CODA_DX6) |
| 348 | size += ysize / 4; |
| 349 | name = kasprintf(GFP_KERNEL, "fb%d", i); |
| 350 | ret = coda_alloc_context_buf(ctx, &ctx->internal_frames[i], |
| 351 | size, name); |
| 352 | kfree(name); |
| 353 | if (ret < 0) { |
| 354 | coda_free_framebuffers(ctx); |
| 355 | return ret; |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | /* Register frame buffers in the parameter buffer */ |
| 360 | for (i = 0; i < ctx->num_internal_frames; i++) { |
| 361 | paddr = ctx->internal_frames[i].paddr; |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 362 | /* Start addresses of Y, Cb, Cr planes */ |
| 363 | coda_parabuf_write(ctx, i * 3 + 0, paddr); |
| 364 | coda_parabuf_write(ctx, i * 3 + 1, paddr + ysize); |
| 365 | coda_parabuf_write(ctx, i * 3 + 2, paddr + ysize + ysize / 4); |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 366 | |
| 367 | /* mvcol buffer for h.264 */ |
| 368 | if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 && |
| 369 | dev->devtype->product != CODA_DX6) |
| 370 | coda_parabuf_write(ctx, 96 + i, |
| 371 | ctx->internal_frames[i].paddr + |
| 372 | ysize + ysize/4 + ysize/4); |
| 373 | } |
| 374 | |
| 375 | /* mvcol buffer for mpeg4 */ |
| 376 | if ((dev->devtype->product != CODA_DX6) && |
| 377 | (ctx->codec->src_fourcc == V4L2_PIX_FMT_MPEG4)) |
| 378 | coda_parabuf_write(ctx, 97, ctx->internal_frames[i].paddr + |
| 379 | ysize + ysize/4 + ysize/4); |
| 380 | |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | static void coda_free_context_buffers(struct coda_ctx *ctx) |
| 385 | { |
| 386 | struct coda_dev *dev = ctx->dev; |
| 387 | |
| 388 | coda_free_aux_buf(dev, &ctx->slicebuf); |
| 389 | coda_free_aux_buf(dev, &ctx->psbuf); |
| 390 | if (dev->devtype->product != CODA_DX6) |
| 391 | coda_free_aux_buf(dev, &ctx->workbuf); |
Philipp Zabel | 650b939 | 2015-03-24 14:30:53 -0300 | [diff] [blame^] | 392 | coda_free_aux_buf(dev, &ctx->parabuf); |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | static int coda_alloc_context_buffers(struct coda_ctx *ctx, |
| 396 | struct coda_q_data *q_data) |
| 397 | { |
| 398 | struct coda_dev *dev = ctx->dev; |
| 399 | size_t size; |
| 400 | int ret; |
| 401 | |
Philipp Zabel | 650b939 | 2015-03-24 14:30:53 -0300 | [diff] [blame^] | 402 | if (!ctx->parabuf.vaddr) { |
| 403 | ret = coda_alloc_context_buf(ctx, &ctx->parabuf, |
| 404 | CODA_PARA_BUF_SIZE, "parabuf"); |
| 405 | if (ret < 0) { |
| 406 | v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf"); |
| 407 | return ret; |
| 408 | } |
| 409 | } |
| 410 | |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 411 | if (dev->devtype->product == CODA_DX6) |
| 412 | return 0; |
| 413 | |
Philipp Zabel | ad532d3 | 2015-03-24 14:30:52 -0300 | [diff] [blame] | 414 | if (!ctx->slicebuf.vaddr && q_data->fourcc == V4L2_PIX_FMT_H264) { |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 415 | /* worst case slice size */ |
| 416 | size = (DIV_ROUND_UP(q_data->width, 16) * |
| 417 | DIV_ROUND_UP(q_data->height, 16)) * 3200 / 8 + 512; |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 418 | ret = coda_alloc_context_buf(ctx, &ctx->slicebuf, size, |
| 419 | "slicebuf"); |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 420 | if (ret < 0) { |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 421 | v4l2_err(&dev->v4l2_dev, |
| 422 | "failed to allocate %d byte slice buffer", |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 423 | ctx->slicebuf.size); |
Philipp Zabel | 650b939 | 2015-03-24 14:30:53 -0300 | [diff] [blame^] | 424 | goto err; |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
Philipp Zabel | ad532d3 | 2015-03-24 14:30:52 -0300 | [diff] [blame] | 428 | if (!ctx->psbuf.vaddr && dev->devtype->product == CODA_7541) { |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 429 | ret = coda_alloc_context_buf(ctx, &ctx->psbuf, |
| 430 | CODA7_PS_BUF_SIZE, "psbuf"); |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 431 | if (ret < 0) { |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 432 | v4l2_err(&dev->v4l2_dev, |
| 433 | "failed to allocate psmem buffer"); |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 434 | goto err; |
| 435 | } |
| 436 | } |
| 437 | |
Philipp Zabel | ad532d3 | 2015-03-24 14:30:52 -0300 | [diff] [blame] | 438 | if (!ctx->workbuf.vaddr) { |
| 439 | size = dev->devtype->workbuf_size; |
| 440 | if (dev->devtype->product == CODA_960 && |
| 441 | q_data->fourcc == V4L2_PIX_FMT_H264) |
| 442 | size += CODA9_PS_SAVE_SIZE; |
| 443 | ret = coda_alloc_context_buf(ctx, &ctx->workbuf, size, |
| 444 | "workbuf"); |
| 445 | if (ret < 0) { |
| 446 | v4l2_err(&dev->v4l2_dev, |
| 447 | "failed to allocate %d byte context buffer", |
| 448 | ctx->workbuf.size); |
| 449 | goto err; |
| 450 | } |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | return 0; |
| 454 | |
| 455 | err: |
| 456 | coda_free_context_buffers(ctx); |
| 457 | return ret; |
| 458 | } |
| 459 | |
| 460 | static int coda_encode_header(struct coda_ctx *ctx, struct vb2_buffer *buf, |
| 461 | int header_code, u8 *header, int *size) |
| 462 | { |
| 463 | struct coda_dev *dev = ctx->dev; |
| 464 | size_t bufsize; |
| 465 | int ret; |
| 466 | int i; |
| 467 | |
| 468 | if (dev->devtype->product == CODA_960) |
| 469 | memset(vb2_plane_vaddr(buf, 0), 0, 64); |
| 470 | |
| 471 | coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0), |
| 472 | CODA_CMD_ENC_HEADER_BB_START); |
| 473 | bufsize = vb2_plane_size(buf, 0); |
| 474 | if (dev->devtype->product == CODA_960) |
| 475 | bufsize /= 1024; |
| 476 | coda_write(dev, bufsize, CODA_CMD_ENC_HEADER_BB_SIZE); |
| 477 | coda_write(dev, header_code, CODA_CMD_ENC_HEADER_CODE); |
| 478 | ret = coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER); |
| 479 | if (ret < 0) { |
| 480 | v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n"); |
| 481 | return ret; |
| 482 | } |
| 483 | |
| 484 | if (dev->devtype->product == CODA_960) { |
| 485 | for (i = 63; i > 0; i--) |
| 486 | if (((char *)vb2_plane_vaddr(buf, 0))[i] != 0) |
| 487 | break; |
| 488 | *size = i + 1; |
| 489 | } else { |
| 490 | *size = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx)) - |
| 491 | coda_read(dev, CODA_CMD_ENC_HEADER_BB_START); |
| 492 | } |
| 493 | memcpy(header, vb2_plane_vaddr(buf, 0), *size); |
| 494 | |
| 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | static phys_addr_t coda_iram_alloc(struct coda_iram_info *iram, size_t size) |
| 499 | { |
| 500 | phys_addr_t ret; |
| 501 | |
| 502 | size = round_up(size, 1024); |
| 503 | if (size > iram->remaining) |
| 504 | return 0; |
| 505 | iram->remaining -= size; |
| 506 | |
| 507 | ret = iram->next_paddr; |
| 508 | iram->next_paddr += size; |
| 509 | |
| 510 | return ret; |
| 511 | } |
| 512 | |
| 513 | static void coda_setup_iram(struct coda_ctx *ctx) |
| 514 | { |
| 515 | struct coda_iram_info *iram_info = &ctx->iram_info; |
| 516 | struct coda_dev *dev = ctx->dev; |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 517 | int w64, w128; |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 518 | int mb_width; |
| 519 | int dbk_bits; |
| 520 | int bit_bits; |
| 521 | int ip_bits; |
| 522 | |
| 523 | memset(iram_info, 0, sizeof(*iram_info)); |
| 524 | iram_info->next_paddr = dev->iram.paddr; |
| 525 | iram_info->remaining = dev->iram.size; |
| 526 | |
Philipp Zabel | 8be31c8 | 2014-08-05 14:00:13 -0300 | [diff] [blame] | 527 | if (!dev->iram.vaddr) |
| 528 | return; |
| 529 | |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 530 | switch (dev->devtype->product) { |
| 531 | case CODA_7541: |
| 532 | dbk_bits = CODA7_USE_HOST_DBK_ENABLE | CODA7_USE_DBK_ENABLE; |
| 533 | bit_bits = CODA7_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE; |
| 534 | ip_bits = CODA7_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE; |
| 535 | break; |
| 536 | case CODA_960: |
| 537 | dbk_bits = CODA9_USE_HOST_DBK_ENABLE | CODA9_USE_DBK_ENABLE; |
| 538 | bit_bits = CODA9_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE; |
| 539 | ip_bits = CODA9_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE; |
| 540 | break; |
| 541 | default: /* CODA_DX6 */ |
| 542 | return; |
| 543 | } |
| 544 | |
| 545 | if (ctx->inst_type == CODA_INST_ENCODER) { |
| 546 | struct coda_q_data *q_data_src; |
| 547 | |
| 548 | q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); |
| 549 | mb_width = DIV_ROUND_UP(q_data_src->width, 16); |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 550 | w128 = mb_width * 128; |
| 551 | w64 = mb_width * 64; |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 552 | |
| 553 | /* Prioritize in case IRAM is too small for everything */ |
| 554 | if (dev->devtype->product == CODA_7541) { |
| 555 | iram_info->search_ram_size = round_up(mb_width * 16 * |
| 556 | 36 + 2048, 1024); |
| 557 | iram_info->search_ram_paddr = coda_iram_alloc(iram_info, |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 558 | iram_info->search_ram_size); |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 559 | if (!iram_info->search_ram_paddr) { |
| 560 | pr_err("IRAM is smaller than the search ram size\n"); |
| 561 | goto out; |
| 562 | } |
| 563 | iram_info->axi_sram_use |= CODA7_USE_HOST_ME_ENABLE | |
| 564 | CODA7_USE_ME_ENABLE; |
| 565 | } |
| 566 | |
| 567 | /* Only H.264BP and H.263P3 are considered */ |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 568 | iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, w64); |
| 569 | iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, w64); |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 570 | if (!iram_info->buf_dbk_c_use) |
| 571 | goto out; |
| 572 | iram_info->axi_sram_use |= dbk_bits; |
| 573 | |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 574 | iram_info->buf_bit_use = coda_iram_alloc(iram_info, w128); |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 575 | if (!iram_info->buf_bit_use) |
| 576 | goto out; |
| 577 | iram_info->axi_sram_use |= bit_bits; |
| 578 | |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 579 | iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, w128); |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 580 | if (!iram_info->buf_ip_ac_dc_use) |
| 581 | goto out; |
| 582 | iram_info->axi_sram_use |= ip_bits; |
| 583 | |
| 584 | /* OVL and BTP disabled for encoder */ |
| 585 | } else if (ctx->inst_type == CODA_INST_DECODER) { |
| 586 | struct coda_q_data *q_data_dst; |
| 587 | |
| 588 | q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE); |
| 589 | mb_width = DIV_ROUND_UP(q_data_dst->width, 16); |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 590 | w128 = mb_width * 128; |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 591 | |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 592 | iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, w128); |
| 593 | iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, w128); |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 594 | if (!iram_info->buf_dbk_c_use) |
| 595 | goto out; |
| 596 | iram_info->axi_sram_use |= dbk_bits; |
| 597 | |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 598 | iram_info->buf_bit_use = coda_iram_alloc(iram_info, w128); |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 599 | if (!iram_info->buf_bit_use) |
| 600 | goto out; |
| 601 | iram_info->axi_sram_use |= bit_bits; |
| 602 | |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 603 | iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, w128); |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 604 | if (!iram_info->buf_ip_ac_dc_use) |
| 605 | goto out; |
| 606 | iram_info->axi_sram_use |= ip_bits; |
| 607 | |
| 608 | /* OVL and BTP unused as there is no VC1 support yet */ |
| 609 | } |
| 610 | |
| 611 | out: |
| 612 | if (!(iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE)) |
| 613 | v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, |
| 614 | "IRAM smaller than needed\n"); |
| 615 | |
| 616 | if (dev->devtype->product == CODA_7541) { |
| 617 | /* TODO - Enabling these causes picture errors on CODA7541 */ |
| 618 | if (ctx->inst_type == CODA_INST_DECODER) { |
| 619 | /* fw 1.4.50 */ |
| 620 | iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE | |
| 621 | CODA7_USE_IP_ENABLE); |
| 622 | } else { |
| 623 | /* fw 13.4.29 */ |
| 624 | iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE | |
| 625 | CODA7_USE_HOST_DBK_ENABLE | |
| 626 | CODA7_USE_IP_ENABLE | |
| 627 | CODA7_USE_DBK_ENABLE); |
| 628 | } |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | static u32 coda_supported_firmwares[] = { |
| 633 | CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5), |
| 634 | CODA_FIRMWARE_VERNUM(CODA_7541, 1, 4, 50), |
| 635 | CODA_FIRMWARE_VERNUM(CODA_960, 2, 1, 5), |
| 636 | }; |
| 637 | |
| 638 | static bool coda_firmware_supported(u32 vernum) |
| 639 | { |
| 640 | int i; |
| 641 | |
| 642 | for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++) |
| 643 | if (vernum == coda_supported_firmwares[i]) |
| 644 | return true; |
| 645 | return false; |
| 646 | } |
| 647 | |
| 648 | int coda_check_firmware(struct coda_dev *dev) |
| 649 | { |
| 650 | u16 product, major, minor, release; |
| 651 | u32 data; |
| 652 | int ret; |
| 653 | |
| 654 | ret = clk_prepare_enable(dev->clk_per); |
| 655 | if (ret) |
| 656 | goto err_clk_per; |
| 657 | |
| 658 | ret = clk_prepare_enable(dev->clk_ahb); |
| 659 | if (ret) |
| 660 | goto err_clk_ahb; |
| 661 | |
| 662 | coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM); |
| 663 | coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY); |
| 664 | coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX); |
| 665 | coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD); |
| 666 | coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND); |
| 667 | if (coda_wait_timeout(dev)) { |
| 668 | v4l2_err(&dev->v4l2_dev, "firmware get command error\n"); |
| 669 | ret = -EIO; |
| 670 | goto err_run_cmd; |
| 671 | } |
| 672 | |
| 673 | if (dev->devtype->product == CODA_960) { |
| 674 | data = coda_read(dev, CODA9_CMD_FIRMWARE_CODE_REV); |
| 675 | v4l2_info(&dev->v4l2_dev, "Firmware code revision: %d\n", |
| 676 | data); |
| 677 | } |
| 678 | |
| 679 | /* Check we are compatible with the loaded firmware */ |
| 680 | data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM); |
| 681 | product = CODA_FIRMWARE_PRODUCT(data); |
| 682 | major = CODA_FIRMWARE_MAJOR(data); |
| 683 | minor = CODA_FIRMWARE_MINOR(data); |
| 684 | release = CODA_FIRMWARE_RELEASE(data); |
| 685 | |
| 686 | clk_disable_unprepare(dev->clk_per); |
| 687 | clk_disable_unprepare(dev->clk_ahb); |
| 688 | |
| 689 | if (product != dev->devtype->product) { |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 690 | v4l2_err(&dev->v4l2_dev, |
| 691 | "Wrong firmware. Hw: %s, Fw: %s, Version: %u.%u.%u\n", |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 692 | coda_product_name(dev->devtype->product), |
| 693 | coda_product_name(product), major, minor, release); |
| 694 | return -EINVAL; |
| 695 | } |
| 696 | |
| 697 | v4l2_info(&dev->v4l2_dev, "Initialized %s.\n", |
| 698 | coda_product_name(product)); |
| 699 | |
| 700 | if (coda_firmware_supported(data)) { |
| 701 | v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n", |
| 702 | major, minor, release); |
| 703 | } else { |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 704 | v4l2_warn(&dev->v4l2_dev, |
| 705 | "Unsupported firmware version: %u.%u.%u\n", |
| 706 | major, minor, release); |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | return 0; |
| 710 | |
| 711 | err_run_cmd: |
| 712 | clk_disable_unprepare(dev->clk_ahb); |
| 713 | err_clk_ahb: |
| 714 | clk_disable_unprepare(dev->clk_per); |
| 715 | err_clk_per: |
| 716 | return ret; |
| 717 | } |
| 718 | |
| 719 | /* |
| 720 | * Encoder context operations |
| 721 | */ |
| 722 | |
Philipp Zabel | 73751da | 2015-03-24 14:30:51 -0300 | [diff] [blame] | 723 | static int coda_encoder_reqbufs(struct coda_ctx *ctx, |
| 724 | struct v4l2_requestbuffers *rb) |
| 725 | { |
| 726 | struct coda_q_data *q_data_src; |
| 727 | int ret; |
| 728 | |
| 729 | if (rb->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) |
| 730 | return 0; |
| 731 | |
| 732 | if (rb->count) { |
| 733 | q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); |
| 734 | ret = coda_alloc_context_buffers(ctx, q_data_src); |
| 735 | if (ret < 0) |
| 736 | return ret; |
| 737 | } else { |
| 738 | coda_free_context_buffers(ctx); |
| 739 | } |
| 740 | |
| 741 | return 0; |
| 742 | } |
| 743 | |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 744 | static int coda_start_encoding(struct coda_ctx *ctx) |
| 745 | { |
| 746 | struct coda_dev *dev = ctx->dev; |
| 747 | struct v4l2_device *v4l2_dev = &dev->v4l2_dev; |
| 748 | struct coda_q_data *q_data_src, *q_data_dst; |
| 749 | u32 bitstream_buf, bitstream_size; |
| 750 | struct vb2_buffer *buf; |
| 751 | int gamma, ret, value; |
| 752 | u32 dst_fourcc; |
Philipp Zabel | e3cccff | 2015-01-23 13:51:28 -0300 | [diff] [blame] | 753 | int num_fb; |
Philipp Zabel | cb1d3a3 | 2014-10-02 14:08:31 -0300 | [diff] [blame] | 754 | u32 stride; |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 755 | |
| 756 | q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); |
| 757 | q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE); |
| 758 | dst_fourcc = q_data_dst->fourcc; |
| 759 | |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 760 | buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx); |
| 761 | bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0); |
| 762 | bitstream_size = q_data_dst->sizeimage; |
| 763 | |
| 764 | if (!coda_is_initialized(dev)) { |
| 765 | v4l2_err(v4l2_dev, "coda is not initialized.\n"); |
| 766 | return -EFAULT; |
| 767 | } |
| 768 | |
Philipp Zabel | cb1d3a3 | 2014-10-02 14:08:31 -0300 | [diff] [blame] | 769 | if (dst_fourcc == V4L2_PIX_FMT_JPEG) { |
| 770 | if (!ctx->params.jpeg_qmat_tab[0]) |
| 771 | ctx->params.jpeg_qmat_tab[0] = kmalloc(64, GFP_KERNEL); |
| 772 | if (!ctx->params.jpeg_qmat_tab[1]) |
| 773 | ctx->params.jpeg_qmat_tab[1] = kmalloc(64, GFP_KERNEL); |
| 774 | coda_set_jpeg_compression_quality(ctx, ctx->params.jpeg_quality); |
| 775 | } |
| 776 | |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 777 | mutex_lock(&dev->coda_mutex); |
| 778 | |
| 779 | coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR); |
| 780 | coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->reg_idx)); |
| 781 | coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->reg_idx)); |
| 782 | switch (dev->devtype->product) { |
| 783 | case CODA_DX6: |
| 784 | coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN | |
| 785 | CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL); |
| 786 | break; |
| 787 | case CODA_960: |
| 788 | coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN); |
| 789 | /* fallthrough */ |
| 790 | case CODA_7541: |
| 791 | coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN | |
| 792 | CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL); |
| 793 | break; |
| 794 | } |
| 795 | |
Philipp Zabel | 1cb12cf | 2014-09-29 09:53:47 -0300 | [diff] [blame] | 796 | ctx->frame_mem_ctrl &= ~CODA_FRAME_CHROMA_INTERLEAVE; |
| 797 | if (q_data_src->fourcc == V4L2_PIX_FMT_NV12) |
| 798 | ctx->frame_mem_ctrl |= CODA_FRAME_CHROMA_INTERLEAVE; |
Philipp Zabel | 2bf299c | 2014-09-29 09:53:46 -0300 | [diff] [blame] | 799 | coda_write(dev, ctx->frame_mem_ctrl, CODA_REG_BIT_FRAME_MEM_CTRL); |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 800 | |
| 801 | if (dev->devtype->product == CODA_DX6) { |
| 802 | /* Configure the coda */ |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 803 | coda_write(dev, dev->iram.paddr, |
| 804 | CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR); |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | /* Could set rotation here if needed */ |
Philipp Zabel | 2bf299c | 2014-09-29 09:53:46 -0300 | [diff] [blame] | 808 | value = 0; |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 809 | switch (dev->devtype->product) { |
| 810 | case CODA_DX6: |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 811 | value = (q_data_src->width & CODADX6_PICWIDTH_MASK) |
| 812 | << CODADX6_PICWIDTH_OFFSET; |
| 813 | value |= (q_data_src->height & CODADX6_PICHEIGHT_MASK) |
| 814 | << CODA_PICHEIGHT_OFFSET; |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 815 | break; |
| 816 | case CODA_7541: |
| 817 | if (dst_fourcc == V4L2_PIX_FMT_H264) { |
| 818 | value = (round_up(q_data_src->width, 16) & |
| 819 | CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET; |
| 820 | value |= (round_up(q_data_src->height, 16) & |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 821 | CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET; |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 822 | break; |
| 823 | } |
| 824 | /* fallthrough */ |
| 825 | case CODA_960: |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 826 | value = (q_data_src->width & CODA7_PICWIDTH_MASK) |
| 827 | << CODA7_PICWIDTH_OFFSET; |
| 828 | value |= (q_data_src->height & CODA7_PICHEIGHT_MASK) |
| 829 | << CODA_PICHEIGHT_OFFSET; |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 830 | } |
| 831 | coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE); |
Philipp Zabel | cb1d3a3 | 2014-10-02 14:08:31 -0300 | [diff] [blame] | 832 | if (dst_fourcc == V4L2_PIX_FMT_JPEG) |
| 833 | ctx->params.framerate = 0; |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 834 | coda_write(dev, ctx->params.framerate, |
| 835 | CODA_CMD_ENC_SEQ_SRC_F_RATE); |
| 836 | |
| 837 | ctx->params.codec_mode = ctx->codec->mode; |
| 838 | switch (dst_fourcc) { |
| 839 | case V4L2_PIX_FMT_MPEG4: |
| 840 | if (dev->devtype->product == CODA_960) |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 841 | coda_write(dev, CODA9_STD_MPEG4, |
| 842 | CODA_CMD_ENC_SEQ_COD_STD); |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 843 | else |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 844 | coda_write(dev, CODA_STD_MPEG4, |
| 845 | CODA_CMD_ENC_SEQ_COD_STD); |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 846 | coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA); |
| 847 | break; |
| 848 | case V4L2_PIX_FMT_H264: |
| 849 | if (dev->devtype->product == CODA_960) |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 850 | coda_write(dev, CODA9_STD_H264, |
| 851 | CODA_CMD_ENC_SEQ_COD_STD); |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 852 | else |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 853 | coda_write(dev, CODA_STD_H264, |
| 854 | CODA_CMD_ENC_SEQ_COD_STD); |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 855 | if (ctx->params.h264_deblk_enabled) { |
| 856 | value = ((ctx->params.h264_deblk_alpha & |
| 857 | CODA_264PARAM_DEBLKFILTEROFFSETALPHA_MASK) << |
| 858 | CODA_264PARAM_DEBLKFILTEROFFSETALPHA_OFFSET) | |
| 859 | ((ctx->params.h264_deblk_beta & |
| 860 | CODA_264PARAM_DEBLKFILTEROFFSETBETA_MASK) << |
| 861 | CODA_264PARAM_DEBLKFILTEROFFSETBETA_OFFSET); |
| 862 | } else { |
| 863 | value = 1 << CODA_264PARAM_DISABLEDEBLK_OFFSET; |
| 864 | } |
| 865 | coda_write(dev, value, CODA_CMD_ENC_SEQ_264_PARA); |
| 866 | break; |
Philipp Zabel | cb1d3a3 | 2014-10-02 14:08:31 -0300 | [diff] [blame] | 867 | case V4L2_PIX_FMT_JPEG: |
| 868 | coda_write(dev, 0, CODA_CMD_ENC_SEQ_JPG_PARA); |
| 869 | coda_write(dev, ctx->params.jpeg_restart_interval, |
| 870 | CODA_CMD_ENC_SEQ_JPG_RST_INTERVAL); |
| 871 | coda_write(dev, 0, CODA_CMD_ENC_SEQ_JPG_THUMB_EN); |
| 872 | coda_write(dev, 0, CODA_CMD_ENC_SEQ_JPG_THUMB_SIZE); |
| 873 | coda_write(dev, 0, CODA_CMD_ENC_SEQ_JPG_THUMB_OFFSET); |
| 874 | |
| 875 | coda_jpeg_write_tables(ctx); |
| 876 | break; |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 877 | default: |
| 878 | v4l2_err(v4l2_dev, |
| 879 | "dst format (0x%08x) invalid.\n", dst_fourcc); |
| 880 | ret = -EINVAL; |
| 881 | goto out; |
| 882 | } |
| 883 | |
Philipp Zabel | cb1d3a3 | 2014-10-02 14:08:31 -0300 | [diff] [blame] | 884 | /* |
| 885 | * slice mode and GOP size registers are used for thumb size/offset |
| 886 | * in JPEG mode |
| 887 | */ |
| 888 | if (dst_fourcc != V4L2_PIX_FMT_JPEG) { |
| 889 | switch (ctx->params.slice_mode) { |
| 890 | case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE: |
| 891 | value = 0; |
| 892 | break; |
| 893 | case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB: |
| 894 | value = (ctx->params.slice_max_mb & |
| 895 | CODA_SLICING_SIZE_MASK) |
| 896 | << CODA_SLICING_SIZE_OFFSET; |
| 897 | value |= (1 & CODA_SLICING_UNIT_MASK) |
| 898 | << CODA_SLICING_UNIT_OFFSET; |
| 899 | value |= 1 & CODA_SLICING_MODE_MASK; |
| 900 | break; |
| 901 | case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES: |
| 902 | value = (ctx->params.slice_max_bits & |
| 903 | CODA_SLICING_SIZE_MASK) |
| 904 | << CODA_SLICING_SIZE_OFFSET; |
| 905 | value |= (0 & CODA_SLICING_UNIT_MASK) |
| 906 | << CODA_SLICING_UNIT_OFFSET; |
| 907 | value |= 1 & CODA_SLICING_MODE_MASK; |
| 908 | break; |
| 909 | } |
| 910 | coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE); |
| 911 | value = ctx->params.gop_size & CODA_GOP_SIZE_MASK; |
| 912 | coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE); |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 913 | } |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 914 | |
| 915 | if (ctx->params.bitrate) { |
| 916 | /* Rate control enabled */ |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 917 | value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK) |
| 918 | << CODA_RATECONTROL_BITRATE_OFFSET; |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 919 | value |= 1 & CODA_RATECONTROL_ENABLE_MASK; |
| 920 | if (dev->devtype->product == CODA_960) |
| 921 | value |= BIT(31); /* disable autoskip */ |
| 922 | } else { |
| 923 | value = 0; |
| 924 | } |
| 925 | coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA); |
| 926 | |
| 927 | coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE); |
| 928 | coda_write(dev, ctx->params.intra_refresh, |
| 929 | CODA_CMD_ENC_SEQ_INTRA_REFRESH); |
| 930 | |
| 931 | coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START); |
| 932 | coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE); |
| 933 | |
| 934 | |
| 935 | value = 0; |
| 936 | if (dev->devtype->product == CODA_960) |
| 937 | gamma = CODA9_DEFAULT_GAMMA; |
| 938 | else |
| 939 | gamma = CODA_DEFAULT_GAMMA; |
| 940 | if (gamma > 0) { |
| 941 | coda_write(dev, (gamma & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET, |
| 942 | CODA_CMD_ENC_SEQ_RC_GAMMA); |
| 943 | } |
| 944 | |
| 945 | if (ctx->params.h264_min_qp || ctx->params.h264_max_qp) { |
| 946 | coda_write(dev, |
| 947 | ctx->params.h264_min_qp << CODA_QPMIN_OFFSET | |
| 948 | ctx->params.h264_max_qp << CODA_QPMAX_OFFSET, |
| 949 | CODA_CMD_ENC_SEQ_RC_QP_MIN_MAX); |
| 950 | } |
| 951 | if (dev->devtype->product == CODA_960) { |
| 952 | if (ctx->params.h264_max_qp) |
| 953 | value |= 1 << CODA9_OPTION_RCQPMAX_OFFSET; |
| 954 | if (CODA_DEFAULT_GAMMA > 0) |
| 955 | value |= 1 << CODA9_OPTION_GAMMA_OFFSET; |
| 956 | } else { |
| 957 | if (CODA_DEFAULT_GAMMA > 0) { |
| 958 | if (dev->devtype->product == CODA_DX6) |
| 959 | value |= 1 << CODADX6_OPTION_GAMMA_OFFSET; |
| 960 | else |
| 961 | value |= 1 << CODA7_OPTION_GAMMA_OFFSET; |
| 962 | } |
| 963 | if (ctx->params.h264_min_qp) |
| 964 | value |= 1 << CODA7_OPTION_RCQPMIN_OFFSET; |
| 965 | if (ctx->params.h264_max_qp) |
| 966 | value |= 1 << CODA7_OPTION_RCQPMAX_OFFSET; |
| 967 | } |
| 968 | coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION); |
| 969 | |
| 970 | coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_INTERVAL_MODE); |
| 971 | |
| 972 | coda_setup_iram(ctx); |
| 973 | |
| 974 | if (dst_fourcc == V4L2_PIX_FMT_H264) { |
| 975 | switch (dev->devtype->product) { |
| 976 | case CODA_DX6: |
| 977 | value = FMO_SLICE_SAVE_BUF_SIZE << 7; |
| 978 | coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO); |
| 979 | break; |
| 980 | case CODA_7541: |
| 981 | coda_write(dev, ctx->iram_info.search_ram_paddr, |
| 982 | CODA7_CMD_ENC_SEQ_SEARCH_BASE); |
| 983 | coda_write(dev, ctx->iram_info.search_ram_size, |
| 984 | CODA7_CMD_ENC_SEQ_SEARCH_SIZE); |
| 985 | break; |
| 986 | case CODA_960: |
| 987 | coda_write(dev, 0, CODA9_CMD_ENC_SEQ_ME_OPTION); |
| 988 | coda_write(dev, 0, CODA9_CMD_ENC_SEQ_INTRA_WEIGHT); |
| 989 | } |
| 990 | } |
| 991 | |
| 992 | ret = coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT); |
| 993 | if (ret < 0) { |
| 994 | v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n"); |
| 995 | goto out; |
| 996 | } |
| 997 | |
| 998 | if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0) { |
| 999 | v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT failed\n"); |
| 1000 | ret = -EFAULT; |
| 1001 | goto out; |
| 1002 | } |
| 1003 | |
Philipp Zabel | cb1d3a3 | 2014-10-02 14:08:31 -0300 | [diff] [blame] | 1004 | if (dst_fourcc != V4L2_PIX_FMT_JPEG) { |
| 1005 | if (dev->devtype->product == CODA_960) |
| 1006 | ctx->num_internal_frames = 4; |
| 1007 | else |
| 1008 | ctx->num_internal_frames = 2; |
| 1009 | ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc); |
| 1010 | if (ret < 0) { |
| 1011 | v4l2_err(v4l2_dev, "failed to allocate framebuffers\n"); |
| 1012 | goto out; |
| 1013 | } |
Philipp Zabel | e3cccff | 2015-01-23 13:51:28 -0300 | [diff] [blame] | 1014 | num_fb = 2; |
Philipp Zabel | cb1d3a3 | 2014-10-02 14:08:31 -0300 | [diff] [blame] | 1015 | stride = q_data_src->bytesperline; |
| 1016 | } else { |
| 1017 | ctx->num_internal_frames = 0; |
Philipp Zabel | e3cccff | 2015-01-23 13:51:28 -0300 | [diff] [blame] | 1018 | num_fb = 0; |
Philipp Zabel | cb1d3a3 | 2014-10-02 14:08:31 -0300 | [diff] [blame] | 1019 | stride = 0; |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1020 | } |
Philipp Zabel | e3cccff | 2015-01-23 13:51:28 -0300 | [diff] [blame] | 1021 | coda_write(dev, num_fb, CODA_CMD_SET_FRAME_BUF_NUM); |
Philipp Zabel | cb1d3a3 | 2014-10-02 14:08:31 -0300 | [diff] [blame] | 1022 | coda_write(dev, stride, CODA_CMD_SET_FRAME_BUF_STRIDE); |
| 1023 | |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1024 | if (dev->devtype->product == CODA_7541) { |
| 1025 | coda_write(dev, q_data_src->bytesperline, |
| 1026 | CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE); |
| 1027 | } |
| 1028 | if (dev->devtype->product != CODA_DX6) { |
| 1029 | coda_write(dev, ctx->iram_info.buf_bit_use, |
| 1030 | CODA7_CMD_SET_FRAME_AXI_BIT_ADDR); |
| 1031 | coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use, |
| 1032 | CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR); |
| 1033 | coda_write(dev, ctx->iram_info.buf_dbk_y_use, |
| 1034 | CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR); |
| 1035 | coda_write(dev, ctx->iram_info.buf_dbk_c_use, |
| 1036 | CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR); |
| 1037 | coda_write(dev, ctx->iram_info.buf_ovl_use, |
| 1038 | CODA7_CMD_SET_FRAME_AXI_OVL_ADDR); |
| 1039 | if (dev->devtype->product == CODA_960) { |
| 1040 | coda_write(dev, ctx->iram_info.buf_btp_use, |
| 1041 | CODA9_CMD_SET_FRAME_AXI_BTP_ADDR); |
| 1042 | |
| 1043 | /* FIXME */ |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 1044 | coda_write(dev, ctx->internal_frames[2].paddr, |
| 1045 | CODA9_CMD_SET_FRAME_SUBSAMP_A); |
| 1046 | coda_write(dev, ctx->internal_frames[3].paddr, |
| 1047 | CODA9_CMD_SET_FRAME_SUBSAMP_B); |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | ret = coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF); |
| 1052 | if (ret < 0) { |
| 1053 | v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n"); |
| 1054 | goto out; |
| 1055 | } |
| 1056 | |
| 1057 | /* Save stream headers */ |
| 1058 | buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx); |
| 1059 | switch (dst_fourcc) { |
| 1060 | case V4L2_PIX_FMT_H264: |
| 1061 | /* |
| 1062 | * Get SPS in the first frame and copy it to an |
| 1063 | * intermediate buffer. |
| 1064 | */ |
| 1065 | ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_SPS, |
| 1066 | &ctx->vpu_header[0][0], |
| 1067 | &ctx->vpu_header_size[0]); |
| 1068 | if (ret < 0) |
| 1069 | goto out; |
| 1070 | |
| 1071 | /* |
| 1072 | * Get PPS in the first frame and copy it to an |
| 1073 | * intermediate buffer. |
| 1074 | */ |
| 1075 | ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_PPS, |
| 1076 | &ctx->vpu_header[1][0], |
| 1077 | &ctx->vpu_header_size[1]); |
| 1078 | if (ret < 0) |
| 1079 | goto out; |
| 1080 | |
| 1081 | /* |
| 1082 | * Length of H.264 headers is variable and thus it might not be |
| 1083 | * aligned for the coda to append the encoded frame. In that is |
| 1084 | * the case a filler NAL must be added to header 2. |
| 1085 | */ |
| 1086 | ctx->vpu_header_size[2] = coda_h264_padding( |
| 1087 | (ctx->vpu_header_size[0] + |
| 1088 | ctx->vpu_header_size[1]), |
| 1089 | ctx->vpu_header[2]); |
| 1090 | break; |
| 1091 | case V4L2_PIX_FMT_MPEG4: |
| 1092 | /* |
| 1093 | * Get VOS in the first frame and copy it to an |
| 1094 | * intermediate buffer |
| 1095 | */ |
| 1096 | ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOS, |
| 1097 | &ctx->vpu_header[0][0], |
| 1098 | &ctx->vpu_header_size[0]); |
| 1099 | if (ret < 0) |
| 1100 | goto out; |
| 1101 | |
| 1102 | ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VIS, |
| 1103 | &ctx->vpu_header[1][0], |
| 1104 | &ctx->vpu_header_size[1]); |
| 1105 | if (ret < 0) |
| 1106 | goto out; |
| 1107 | |
| 1108 | ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOL, |
| 1109 | &ctx->vpu_header[2][0], |
| 1110 | &ctx->vpu_header_size[2]); |
| 1111 | if (ret < 0) |
| 1112 | goto out; |
| 1113 | break; |
| 1114 | default: |
| 1115 | /* No more formats need to save headers at the moment */ |
| 1116 | break; |
| 1117 | } |
| 1118 | |
| 1119 | out: |
| 1120 | mutex_unlock(&dev->coda_mutex); |
| 1121 | return ret; |
| 1122 | } |
| 1123 | |
| 1124 | static int coda_prepare_encode(struct coda_ctx *ctx) |
| 1125 | { |
| 1126 | struct coda_q_data *q_data_src, *q_data_dst; |
| 1127 | struct vb2_buffer *src_buf, *dst_buf; |
| 1128 | struct coda_dev *dev = ctx->dev; |
| 1129 | int force_ipicture; |
| 1130 | int quant_param = 0; |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1131 | u32 pic_stream_buffer_addr, pic_stream_buffer_size; |
Philipp Zabel | 59ebc2e | 2014-09-29 09:53:45 -0300 | [diff] [blame] | 1132 | u32 rot_mode = 0; |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1133 | u32 dst_fourcc; |
Philipp Zabel | 856d7d9 | 2014-09-29 09:53:44 -0300 | [diff] [blame] | 1134 | u32 reg; |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1135 | |
| 1136 | src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); |
| 1137 | dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx); |
| 1138 | q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); |
| 1139 | q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE); |
| 1140 | dst_fourcc = q_data_dst->fourcc; |
| 1141 | |
| 1142 | src_buf->v4l2_buf.sequence = ctx->osequence; |
| 1143 | dst_buf->v4l2_buf.sequence = ctx->osequence; |
| 1144 | ctx->osequence++; |
| 1145 | |
| 1146 | /* |
| 1147 | * Workaround coda firmware BUG that only marks the first |
| 1148 | * frame as IDR. This is a problem for some decoders that can't |
| 1149 | * recover when a frame is lost. |
| 1150 | */ |
| 1151 | if (src_buf->v4l2_buf.sequence % ctx->params.gop_size) { |
| 1152 | src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME; |
| 1153 | src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME; |
| 1154 | } else { |
| 1155 | src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME; |
| 1156 | src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME; |
| 1157 | } |
| 1158 | |
| 1159 | if (dev->devtype->product == CODA_960) |
| 1160 | coda_set_gdi_regs(ctx); |
| 1161 | |
| 1162 | /* |
| 1163 | * Copy headers at the beginning of the first frame for H.264 only. |
| 1164 | * In MPEG4 they are already copied by the coda. |
| 1165 | */ |
| 1166 | if (src_buf->v4l2_buf.sequence == 0) { |
| 1167 | pic_stream_buffer_addr = |
| 1168 | vb2_dma_contig_plane_dma_addr(dst_buf, 0) + |
| 1169 | ctx->vpu_header_size[0] + |
| 1170 | ctx->vpu_header_size[1] + |
| 1171 | ctx->vpu_header_size[2]; |
Philipp Zabel | d4c6a41 | 2014-10-02 14:08:35 -0300 | [diff] [blame] | 1172 | pic_stream_buffer_size = q_data_dst->sizeimage - |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1173 | ctx->vpu_header_size[0] - |
| 1174 | ctx->vpu_header_size[1] - |
| 1175 | ctx->vpu_header_size[2]; |
| 1176 | memcpy(vb2_plane_vaddr(dst_buf, 0), |
| 1177 | &ctx->vpu_header[0][0], ctx->vpu_header_size[0]); |
| 1178 | memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0], |
| 1179 | &ctx->vpu_header[1][0], ctx->vpu_header_size[1]); |
| 1180 | memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0] + |
| 1181 | ctx->vpu_header_size[1], &ctx->vpu_header[2][0], |
| 1182 | ctx->vpu_header_size[2]); |
| 1183 | } else { |
| 1184 | pic_stream_buffer_addr = |
| 1185 | vb2_dma_contig_plane_dma_addr(dst_buf, 0); |
Philipp Zabel | d4c6a41 | 2014-10-02 14:08:35 -0300 | [diff] [blame] | 1186 | pic_stream_buffer_size = q_data_dst->sizeimage; |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) { |
| 1190 | force_ipicture = 1; |
| 1191 | switch (dst_fourcc) { |
| 1192 | case V4L2_PIX_FMT_H264: |
| 1193 | quant_param = ctx->params.h264_intra_qp; |
| 1194 | break; |
| 1195 | case V4L2_PIX_FMT_MPEG4: |
| 1196 | quant_param = ctx->params.mpeg4_intra_qp; |
| 1197 | break; |
Philipp Zabel | cb1d3a3 | 2014-10-02 14:08:31 -0300 | [diff] [blame] | 1198 | case V4L2_PIX_FMT_JPEG: |
| 1199 | quant_param = 30; |
| 1200 | break; |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1201 | default: |
| 1202 | v4l2_warn(&ctx->dev->v4l2_dev, |
| 1203 | "cannot set intra qp, fmt not supported\n"); |
| 1204 | break; |
| 1205 | } |
| 1206 | } else { |
| 1207 | force_ipicture = 0; |
| 1208 | switch (dst_fourcc) { |
| 1209 | case V4L2_PIX_FMT_H264: |
| 1210 | quant_param = ctx->params.h264_inter_qp; |
| 1211 | break; |
| 1212 | case V4L2_PIX_FMT_MPEG4: |
| 1213 | quant_param = ctx->params.mpeg4_inter_qp; |
| 1214 | break; |
| 1215 | default: |
| 1216 | v4l2_warn(&ctx->dev->v4l2_dev, |
| 1217 | "cannot set inter qp, fmt not supported\n"); |
| 1218 | break; |
| 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | /* submit */ |
Philipp Zabel | 59ebc2e | 2014-09-29 09:53:45 -0300 | [diff] [blame] | 1223 | if (ctx->params.rot_mode) |
| 1224 | rot_mode = CODA_ROT_MIR_ENABLE | ctx->params.rot_mode; |
| 1225 | coda_write(dev, rot_mode, CODA_CMD_ENC_PIC_ROT_MODE); |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1226 | coda_write(dev, quant_param, CODA_CMD_ENC_PIC_QS); |
| 1227 | |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1228 | if (dev->devtype->product == CODA_960) { |
| 1229 | coda_write(dev, 4/*FIXME: 0*/, CODA9_CMD_ENC_PIC_SRC_INDEX); |
| 1230 | coda_write(dev, q_data_src->width, CODA9_CMD_ENC_PIC_SRC_STRIDE); |
| 1231 | coda_write(dev, 0, CODA9_CMD_ENC_PIC_SUB_FRAME_SYNC); |
| 1232 | |
Philipp Zabel | 856d7d9 | 2014-09-29 09:53:44 -0300 | [diff] [blame] | 1233 | reg = CODA9_CMD_ENC_PIC_SRC_ADDR_Y; |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1234 | } else { |
Philipp Zabel | 856d7d9 | 2014-09-29 09:53:44 -0300 | [diff] [blame] | 1235 | reg = CODA_CMD_ENC_PIC_SRC_ADDR_Y; |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1236 | } |
Philipp Zabel | 856d7d9 | 2014-09-29 09:53:44 -0300 | [diff] [blame] | 1237 | coda_write_base(ctx, q_data_src, src_buf, reg); |
| 1238 | |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1239 | coda_write(dev, force_ipicture << 1 & 0x2, |
| 1240 | CODA_CMD_ENC_PIC_OPTION); |
| 1241 | |
| 1242 | coda_write(dev, pic_stream_buffer_addr, CODA_CMD_ENC_PIC_BB_START); |
| 1243 | coda_write(dev, pic_stream_buffer_size / 1024, |
| 1244 | CODA_CMD_ENC_PIC_BB_SIZE); |
| 1245 | |
| 1246 | if (!ctx->streamon_out) { |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 1247 | /* After streamoff on the output side, set stream end flag */ |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1248 | ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG; |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 1249 | coda_write(dev, ctx->bit_stream_param, |
| 1250 | CODA_REG_BIT_BIT_STREAM_PARAM); |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | if (dev->devtype->product != CODA_DX6) |
| 1254 | coda_write(dev, ctx->iram_info.axi_sram_use, |
| 1255 | CODA7_REG_BIT_AXI_SRAM_USE); |
| 1256 | |
| 1257 | coda_command_async(ctx, CODA_COMMAND_PIC_RUN); |
| 1258 | |
| 1259 | return 0; |
| 1260 | } |
| 1261 | |
| 1262 | static void coda_finish_encode(struct coda_ctx *ctx) |
| 1263 | { |
| 1264 | struct vb2_buffer *src_buf, *dst_buf; |
| 1265 | struct coda_dev *dev = ctx->dev; |
| 1266 | u32 wr_ptr, start_ptr; |
| 1267 | |
| 1268 | src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx); |
| 1269 | dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx); |
| 1270 | |
| 1271 | /* Get results from the coda */ |
| 1272 | start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START); |
| 1273 | wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx)); |
| 1274 | |
| 1275 | /* Calculate bytesused field */ |
| 1276 | if (dst_buf->v4l2_buf.sequence == 0) { |
| 1277 | vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr + |
| 1278 | ctx->vpu_header_size[0] + |
| 1279 | ctx->vpu_header_size[1] + |
| 1280 | ctx->vpu_header_size[2]); |
| 1281 | } else { |
| 1282 | vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr); |
| 1283 | } |
| 1284 | |
| 1285 | v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n", |
| 1286 | wr_ptr - start_ptr); |
| 1287 | |
| 1288 | coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM); |
| 1289 | coda_read(dev, CODA_RET_ENC_PIC_FLAG); |
| 1290 | |
| 1291 | if (coda_read(dev, CODA_RET_ENC_PIC_TYPE) == 0) { |
| 1292 | dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME; |
| 1293 | dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME; |
| 1294 | } else { |
| 1295 | dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME; |
| 1296 | dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME; |
| 1297 | } |
| 1298 | |
| 1299 | dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp; |
| 1300 | dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK; |
| 1301 | dst_buf->v4l2_buf.flags |= |
| 1302 | src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK; |
| 1303 | dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode; |
| 1304 | |
| 1305 | v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE); |
| 1306 | |
| 1307 | dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx); |
| 1308 | v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE); |
| 1309 | |
| 1310 | ctx->gopcounter--; |
| 1311 | if (ctx->gopcounter < 0) |
| 1312 | ctx->gopcounter = ctx->params.gop_size - 1; |
| 1313 | |
| 1314 | v4l2_dbg(1, coda_debug, &dev->v4l2_dev, |
| 1315 | "job finished: encoding frame (%d) (%s)\n", |
| 1316 | dst_buf->v4l2_buf.sequence, |
| 1317 | (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ? |
| 1318 | "KEYFRAME" : "PFRAME"); |
| 1319 | } |
| 1320 | |
| 1321 | static void coda_seq_end_work(struct work_struct *work) |
| 1322 | { |
| 1323 | struct coda_ctx *ctx = container_of(work, struct coda_ctx, seq_end_work); |
| 1324 | struct coda_dev *dev = ctx->dev; |
| 1325 | |
| 1326 | mutex_lock(&ctx->buffer_mutex); |
| 1327 | mutex_lock(&dev->coda_mutex); |
| 1328 | |
| 1329 | v4l2_dbg(1, coda_debug, &dev->v4l2_dev, |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 1330 | "%d: %s: sent command 'SEQ_END' to coda\n", ctx->idx, |
| 1331 | __func__); |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1332 | if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) { |
| 1333 | v4l2_err(&dev->v4l2_dev, |
| 1334 | "CODA_COMMAND_SEQ_END failed\n"); |
| 1335 | } |
| 1336 | |
| 1337 | kfifo_init(&ctx->bitstream_fifo, |
| 1338 | ctx->bitstream.vaddr, ctx->bitstream.size); |
| 1339 | |
| 1340 | coda_free_framebuffers(ctx); |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1341 | |
| 1342 | mutex_unlock(&dev->coda_mutex); |
| 1343 | mutex_unlock(&ctx->buffer_mutex); |
| 1344 | } |
| 1345 | |
| 1346 | static void coda_bit_release(struct coda_ctx *ctx) |
| 1347 | { |
Philipp Zabel | 540d70d | 2015-01-23 13:51:32 -0300 | [diff] [blame] | 1348 | mutex_lock(&ctx->buffer_mutex); |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1349 | coda_free_framebuffers(ctx); |
| 1350 | coda_free_context_buffers(ctx); |
Philipp Zabel | ad532d3 | 2015-03-24 14:30:52 -0300 | [diff] [blame] | 1351 | coda_free_bitstream_buffer(ctx); |
Philipp Zabel | 540d70d | 2015-01-23 13:51:32 -0300 | [diff] [blame] | 1352 | mutex_unlock(&ctx->buffer_mutex); |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1353 | } |
| 1354 | |
| 1355 | const struct coda_context_ops coda_bit_encode_ops = { |
| 1356 | .queue_init = coda_encoder_queue_init, |
Philipp Zabel | 73751da | 2015-03-24 14:30:51 -0300 | [diff] [blame] | 1357 | .reqbufs = coda_encoder_reqbufs, |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1358 | .start_streaming = coda_start_encoding, |
| 1359 | .prepare_run = coda_prepare_encode, |
| 1360 | .finish_run = coda_finish_encode, |
| 1361 | .seq_end_work = coda_seq_end_work, |
| 1362 | .release = coda_bit_release, |
| 1363 | }; |
| 1364 | |
| 1365 | /* |
| 1366 | * Decoder context operations |
| 1367 | */ |
| 1368 | |
Philipp Zabel | ad532d3 | 2015-03-24 14:30:52 -0300 | [diff] [blame] | 1369 | static int coda_alloc_bitstream_buffer(struct coda_ctx *ctx, |
| 1370 | struct coda_q_data *q_data) |
| 1371 | { |
| 1372 | if (ctx->bitstream.vaddr) |
| 1373 | return 0; |
| 1374 | |
| 1375 | ctx->bitstream.size = roundup_pow_of_two(q_data->sizeimage * 2); |
| 1376 | ctx->bitstream.vaddr = dma_alloc_writecombine( |
| 1377 | &ctx->dev->plat_dev->dev, ctx->bitstream.size, |
| 1378 | &ctx->bitstream.paddr, GFP_KERNEL); |
| 1379 | if (!ctx->bitstream.vaddr) { |
| 1380 | v4l2_err(&ctx->dev->v4l2_dev, |
| 1381 | "failed to allocate bitstream ringbuffer"); |
| 1382 | return -ENOMEM; |
| 1383 | } |
| 1384 | kfifo_init(&ctx->bitstream_fifo, |
| 1385 | ctx->bitstream.vaddr, ctx->bitstream.size); |
| 1386 | |
| 1387 | return 0; |
| 1388 | } |
| 1389 | |
| 1390 | static void coda_free_bitstream_buffer(struct coda_ctx *ctx) |
| 1391 | { |
| 1392 | if (ctx->bitstream.vaddr == NULL) |
| 1393 | return; |
| 1394 | |
| 1395 | dma_free_writecombine(&ctx->dev->plat_dev->dev, ctx->bitstream.size, |
| 1396 | ctx->bitstream.vaddr, ctx->bitstream.paddr); |
| 1397 | ctx->bitstream.vaddr = NULL; |
| 1398 | kfifo_init(&ctx->bitstream_fifo, NULL, 0); |
| 1399 | } |
| 1400 | |
Philipp Zabel | 73751da | 2015-03-24 14:30:51 -0300 | [diff] [blame] | 1401 | static int coda_decoder_reqbufs(struct coda_ctx *ctx, |
| 1402 | struct v4l2_requestbuffers *rb) |
| 1403 | { |
| 1404 | struct coda_q_data *q_data_src; |
| 1405 | int ret; |
| 1406 | |
| 1407 | if (rb->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) |
| 1408 | return 0; |
| 1409 | |
| 1410 | if (rb->count) { |
| 1411 | q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); |
| 1412 | ret = coda_alloc_context_buffers(ctx, q_data_src); |
| 1413 | if (ret < 0) |
| 1414 | return ret; |
Philipp Zabel | ad532d3 | 2015-03-24 14:30:52 -0300 | [diff] [blame] | 1415 | ret = coda_alloc_bitstream_buffer(ctx, q_data_src); |
| 1416 | if (ret < 0) { |
| 1417 | coda_free_context_buffers(ctx); |
| 1418 | return ret; |
| 1419 | } |
Philipp Zabel | 73751da | 2015-03-24 14:30:51 -0300 | [diff] [blame] | 1420 | } else { |
Philipp Zabel | ad532d3 | 2015-03-24 14:30:52 -0300 | [diff] [blame] | 1421 | coda_free_bitstream_buffer(ctx); |
Philipp Zabel | 73751da | 2015-03-24 14:30:51 -0300 | [diff] [blame] | 1422 | coda_free_context_buffers(ctx); |
| 1423 | } |
| 1424 | |
| 1425 | return 0; |
| 1426 | } |
| 1427 | |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1428 | static int __coda_start_decoding(struct coda_ctx *ctx) |
| 1429 | { |
| 1430 | struct coda_q_data *q_data_src, *q_data_dst; |
| 1431 | u32 bitstream_buf, bitstream_size; |
| 1432 | struct coda_dev *dev = ctx->dev; |
| 1433 | int width, height; |
Philipp Zabel | 1cb12cf | 2014-09-29 09:53:47 -0300 | [diff] [blame] | 1434 | u32 src_fourcc, dst_fourcc; |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1435 | u32 val; |
| 1436 | int ret; |
| 1437 | |
| 1438 | /* Start decoding */ |
| 1439 | q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); |
| 1440 | q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE); |
| 1441 | bitstream_buf = ctx->bitstream.paddr; |
| 1442 | bitstream_size = ctx->bitstream.size; |
| 1443 | src_fourcc = q_data_src->fourcc; |
Philipp Zabel | 1cb12cf | 2014-09-29 09:53:47 -0300 | [diff] [blame] | 1444 | dst_fourcc = q_data_dst->fourcc; |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1445 | |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1446 | coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR); |
| 1447 | |
| 1448 | /* Update coda bitstream read and write pointers from kfifo */ |
| 1449 | coda_kfifo_sync_to_device_full(ctx); |
| 1450 | |
Philipp Zabel | 1cb12cf | 2014-09-29 09:53:47 -0300 | [diff] [blame] | 1451 | ctx->frame_mem_ctrl &= ~CODA_FRAME_CHROMA_INTERLEAVE; |
| 1452 | if (dst_fourcc == V4L2_PIX_FMT_NV12) |
| 1453 | ctx->frame_mem_ctrl |= CODA_FRAME_CHROMA_INTERLEAVE; |
Philipp Zabel | 2bf299c | 2014-09-29 09:53:46 -0300 | [diff] [blame] | 1454 | coda_write(dev, ctx->frame_mem_ctrl, CODA_REG_BIT_FRAME_MEM_CTRL); |
| 1455 | |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1456 | ctx->display_idx = -1; |
| 1457 | ctx->frm_dis_flg = 0; |
| 1458 | coda_write(dev, 0, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx)); |
| 1459 | |
| 1460 | coda_write(dev, CODA_BIT_DEC_SEQ_INIT_ESCAPE, |
| 1461 | CODA_REG_BIT_BIT_STREAM_PARAM); |
| 1462 | |
| 1463 | coda_write(dev, bitstream_buf, CODA_CMD_DEC_SEQ_BB_START); |
| 1464 | coda_write(dev, bitstream_size / 1024, CODA_CMD_DEC_SEQ_BB_SIZE); |
| 1465 | val = 0; |
| 1466 | if ((dev->devtype->product == CODA_7541) || |
| 1467 | (dev->devtype->product == CODA_960)) |
| 1468 | val |= CODA_REORDER_ENABLE; |
Philipp Zabel | cb1d3a3 | 2014-10-02 14:08:31 -0300 | [diff] [blame] | 1469 | if (ctx->codec->src_fourcc == V4L2_PIX_FMT_JPEG) |
| 1470 | val |= CODA_NO_INT_ENABLE; |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1471 | coda_write(dev, val, CODA_CMD_DEC_SEQ_OPTION); |
| 1472 | |
| 1473 | ctx->params.codec_mode = ctx->codec->mode; |
| 1474 | if (dev->devtype->product == CODA_960 && |
| 1475 | src_fourcc == V4L2_PIX_FMT_MPEG4) |
| 1476 | ctx->params.codec_mode_aux = CODA_MP4_AUX_MPEG4; |
| 1477 | else |
| 1478 | ctx->params.codec_mode_aux = 0; |
| 1479 | if (src_fourcc == V4L2_PIX_FMT_H264) { |
| 1480 | if (dev->devtype->product == CODA_7541) { |
| 1481 | coda_write(dev, ctx->psbuf.paddr, |
| 1482 | CODA_CMD_DEC_SEQ_PS_BB_START); |
| 1483 | coda_write(dev, (CODA7_PS_BUF_SIZE / 1024), |
| 1484 | CODA_CMD_DEC_SEQ_PS_BB_SIZE); |
| 1485 | } |
| 1486 | if (dev->devtype->product == CODA_960) { |
| 1487 | coda_write(dev, 0, CODA_CMD_DEC_SEQ_X264_MV_EN); |
| 1488 | coda_write(dev, 512, CODA_CMD_DEC_SEQ_SPP_CHUNK_SIZE); |
| 1489 | } |
| 1490 | } |
| 1491 | if (dev->devtype->product != CODA_960) |
| 1492 | coda_write(dev, 0, CODA_CMD_DEC_SEQ_SRC_SIZE); |
| 1493 | |
| 1494 | if (coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT)) { |
| 1495 | v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n"); |
| 1496 | coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM); |
| 1497 | return -ETIMEDOUT; |
| 1498 | } |
| 1499 | |
| 1500 | /* Update kfifo out pointer from coda bitstream read pointer */ |
| 1501 | coda_kfifo_sync_from_device(ctx); |
| 1502 | |
| 1503 | coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM); |
| 1504 | |
| 1505 | if (coda_read(dev, CODA_RET_DEC_SEQ_SUCCESS) == 0) { |
| 1506 | v4l2_err(&dev->v4l2_dev, |
| 1507 | "CODA_COMMAND_SEQ_INIT failed, error code = %d\n", |
| 1508 | coda_read(dev, CODA_RET_DEC_SEQ_ERR_REASON)); |
| 1509 | return -EAGAIN; |
| 1510 | } |
| 1511 | |
| 1512 | val = coda_read(dev, CODA_RET_DEC_SEQ_SRC_SIZE); |
| 1513 | if (dev->devtype->product == CODA_DX6) { |
| 1514 | width = (val >> CODADX6_PICWIDTH_OFFSET) & CODADX6_PICWIDTH_MASK; |
| 1515 | height = val & CODADX6_PICHEIGHT_MASK; |
| 1516 | } else { |
| 1517 | width = (val >> CODA7_PICWIDTH_OFFSET) & CODA7_PICWIDTH_MASK; |
| 1518 | height = val & CODA7_PICHEIGHT_MASK; |
| 1519 | } |
| 1520 | |
Markus Pargmann | 31db5ad | 2015-01-23 13:51:25 -0300 | [diff] [blame] | 1521 | if (width > q_data_dst->bytesperline || height > q_data_dst->height) { |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1522 | v4l2_err(&dev->v4l2_dev, "stream is %dx%d, not %dx%d\n", |
Markus Pargmann | 31db5ad | 2015-01-23 13:51:25 -0300 | [diff] [blame] | 1523 | width, height, q_data_dst->bytesperline, |
| 1524 | q_data_dst->height); |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1525 | return -EINVAL; |
| 1526 | } |
| 1527 | |
| 1528 | width = round_up(width, 16); |
| 1529 | height = round_up(height, 16); |
| 1530 | |
| 1531 | v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "%s instance %d now: %dx%d\n", |
| 1532 | __func__, ctx->idx, width, height); |
| 1533 | |
| 1534 | ctx->num_internal_frames = coda_read(dev, CODA_RET_DEC_SEQ_FRAME_NEED); |
| 1535 | if (ctx->num_internal_frames > CODA_MAX_FRAMEBUFFERS) { |
| 1536 | v4l2_err(&dev->v4l2_dev, |
| 1537 | "not enough framebuffers to decode (%d < %d)\n", |
| 1538 | CODA_MAX_FRAMEBUFFERS, ctx->num_internal_frames); |
| 1539 | return -EINVAL; |
| 1540 | } |
| 1541 | |
| 1542 | if (src_fourcc == V4L2_PIX_FMT_H264) { |
| 1543 | u32 left_right; |
| 1544 | u32 top_bottom; |
| 1545 | |
| 1546 | left_right = coda_read(dev, CODA_RET_DEC_SEQ_CROP_LEFT_RIGHT); |
| 1547 | top_bottom = coda_read(dev, CODA_RET_DEC_SEQ_CROP_TOP_BOTTOM); |
| 1548 | |
| 1549 | q_data_dst->rect.left = (left_right >> 10) & 0x3ff; |
| 1550 | q_data_dst->rect.top = (top_bottom >> 10) & 0x3ff; |
| 1551 | q_data_dst->rect.width = width - q_data_dst->rect.left - |
| 1552 | (left_right & 0x3ff); |
| 1553 | q_data_dst->rect.height = height - q_data_dst->rect.top - |
| 1554 | (top_bottom & 0x3ff); |
| 1555 | } |
| 1556 | |
| 1557 | ret = coda_alloc_framebuffers(ctx, q_data_dst, src_fourcc); |
Philipp Zabel | 68fc31c | 2014-08-05 14:00:16 -0300 | [diff] [blame] | 1558 | if (ret < 0) { |
| 1559 | v4l2_err(&dev->v4l2_dev, "failed to allocate framebuffers\n"); |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1560 | return ret; |
Philipp Zabel | 68fc31c | 2014-08-05 14:00:16 -0300 | [diff] [blame] | 1561 | } |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1562 | |
| 1563 | /* Tell the decoder how many frame buffers we allocated. */ |
| 1564 | coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM); |
| 1565 | coda_write(dev, width, CODA_CMD_SET_FRAME_BUF_STRIDE); |
| 1566 | |
| 1567 | if (dev->devtype->product != CODA_DX6) { |
| 1568 | /* Set secondary AXI IRAM */ |
| 1569 | coda_setup_iram(ctx); |
| 1570 | |
| 1571 | coda_write(dev, ctx->iram_info.buf_bit_use, |
| 1572 | CODA7_CMD_SET_FRAME_AXI_BIT_ADDR); |
| 1573 | coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use, |
| 1574 | CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR); |
| 1575 | coda_write(dev, ctx->iram_info.buf_dbk_y_use, |
| 1576 | CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR); |
| 1577 | coda_write(dev, ctx->iram_info.buf_dbk_c_use, |
| 1578 | CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR); |
| 1579 | coda_write(dev, ctx->iram_info.buf_ovl_use, |
| 1580 | CODA7_CMD_SET_FRAME_AXI_OVL_ADDR); |
| 1581 | if (dev->devtype->product == CODA_960) |
| 1582 | coda_write(dev, ctx->iram_info.buf_btp_use, |
| 1583 | CODA9_CMD_SET_FRAME_AXI_BTP_ADDR); |
| 1584 | } |
| 1585 | |
| 1586 | if (dev->devtype->product == CODA_960) { |
Philipp Zabel | 1cb12cf | 2014-09-29 09:53:47 -0300 | [diff] [blame] | 1587 | int cbb_size, crb_size; |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1588 | |
Philipp Zabel | 1cb12cf | 2014-09-29 09:53:47 -0300 | [diff] [blame] | 1589 | coda_write(dev, -1, CODA9_CMD_SET_FRAME_DELAY); |
| 1590 | /* Luma 2x0 page, 2x6 cache, chroma 2x0 page, 2x4 cache size */ |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1591 | coda_write(dev, 0x20262024, CODA9_CMD_SET_FRAME_CACHE_SIZE); |
Philipp Zabel | 1cb12cf | 2014-09-29 09:53:47 -0300 | [diff] [blame] | 1592 | |
| 1593 | if (dst_fourcc == V4L2_PIX_FMT_NV12) { |
| 1594 | cbb_size = 0; |
| 1595 | crb_size = 16; |
| 1596 | } else { |
| 1597 | cbb_size = 8; |
| 1598 | crb_size = 8; |
| 1599 | } |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1600 | coda_write(dev, 2 << CODA9_CACHE_PAGEMERGE_OFFSET | |
| 1601 | 32 << CODA9_CACHE_LUMA_BUFFER_SIZE_OFFSET | |
Philipp Zabel | 1cb12cf | 2014-09-29 09:53:47 -0300 | [diff] [blame] | 1602 | cbb_size << CODA9_CACHE_CB_BUFFER_SIZE_OFFSET | |
| 1603 | crb_size << CODA9_CACHE_CR_BUFFER_SIZE_OFFSET, |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1604 | CODA9_CMD_SET_FRAME_CACHE_CONFIG); |
| 1605 | } |
| 1606 | |
| 1607 | if (src_fourcc == V4L2_PIX_FMT_H264) { |
| 1608 | coda_write(dev, ctx->slicebuf.paddr, |
| 1609 | CODA_CMD_SET_FRAME_SLICE_BB_START); |
| 1610 | coda_write(dev, ctx->slicebuf.size / 1024, |
| 1611 | CODA_CMD_SET_FRAME_SLICE_BB_SIZE); |
| 1612 | } |
| 1613 | |
| 1614 | if (dev->devtype->product == CODA_7541) { |
| 1615 | int max_mb_x = 1920 / 16; |
| 1616 | int max_mb_y = 1088 / 16; |
| 1617 | int max_mb_num = max_mb_x * max_mb_y; |
| 1618 | |
| 1619 | coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y, |
| 1620 | CODA7_CMD_SET_FRAME_MAX_DEC_SIZE); |
| 1621 | } else if (dev->devtype->product == CODA_960) { |
| 1622 | int max_mb_x = 1920 / 16; |
| 1623 | int max_mb_y = 1088 / 16; |
| 1624 | int max_mb_num = max_mb_x * max_mb_y; |
| 1625 | |
| 1626 | coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y, |
| 1627 | CODA9_CMD_SET_FRAME_MAX_DEC_SIZE); |
| 1628 | } |
| 1629 | |
| 1630 | if (coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF)) { |
| 1631 | v4l2_err(&ctx->dev->v4l2_dev, |
| 1632 | "CODA_COMMAND_SET_FRAME_BUF timeout\n"); |
| 1633 | return -ETIMEDOUT; |
| 1634 | } |
| 1635 | |
| 1636 | return 0; |
| 1637 | } |
| 1638 | |
| 1639 | static int coda_start_decoding(struct coda_ctx *ctx) |
| 1640 | { |
| 1641 | struct coda_dev *dev = ctx->dev; |
| 1642 | int ret; |
| 1643 | |
| 1644 | mutex_lock(&dev->coda_mutex); |
| 1645 | ret = __coda_start_decoding(ctx); |
| 1646 | mutex_unlock(&dev->coda_mutex); |
| 1647 | |
| 1648 | return ret; |
| 1649 | } |
| 1650 | |
| 1651 | static int coda_prepare_decode(struct coda_ctx *ctx) |
| 1652 | { |
| 1653 | struct vb2_buffer *dst_buf; |
| 1654 | struct coda_dev *dev = ctx->dev; |
| 1655 | struct coda_q_data *q_data_dst; |
Philipp Zabel | 07ba277 | 2015-01-23 13:51:18 -0300 | [diff] [blame] | 1656 | struct coda_buffer_meta *meta; |
Philipp Zabel | 856d7d9 | 2014-09-29 09:53:44 -0300 | [diff] [blame] | 1657 | u32 reg_addr, reg_stride; |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1658 | |
| 1659 | dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx); |
| 1660 | q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE); |
| 1661 | |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1662 | /* Try to copy source buffer contents into the bitstream ringbuffer */ |
| 1663 | mutex_lock(&ctx->bitstream_mutex); |
| 1664 | coda_fill_bitstream(ctx); |
| 1665 | mutex_unlock(&ctx->bitstream_mutex); |
| 1666 | |
| 1667 | if (coda_get_bitstream_payload(ctx) < 512 && |
| 1668 | (!(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) { |
| 1669 | v4l2_dbg(1, coda_debug, &dev->v4l2_dev, |
| 1670 | "bitstream payload: %d, skipping\n", |
| 1671 | coda_get_bitstream_payload(ctx)); |
| 1672 | v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx); |
| 1673 | return -EAGAIN; |
| 1674 | } |
| 1675 | |
| 1676 | /* Run coda_start_decoding (again) if not yet initialized */ |
| 1677 | if (!ctx->initialized) { |
| 1678 | int ret = __coda_start_decoding(ctx); |
| 1679 | |
| 1680 | if (ret < 0) { |
| 1681 | v4l2_err(&dev->v4l2_dev, "failed to start decoding\n"); |
| 1682 | v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx); |
| 1683 | return -EAGAIN; |
| 1684 | } else { |
| 1685 | ctx->initialized = 1; |
| 1686 | } |
| 1687 | } |
| 1688 | |
| 1689 | if (dev->devtype->product == CODA_960) |
| 1690 | coda_set_gdi_regs(ctx); |
| 1691 | |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1692 | if (dev->devtype->product == CODA_960) { |
| 1693 | /* |
| 1694 | * The CODA960 seems to have an internal list of buffers with |
| 1695 | * 64 entries that includes the registered frame buffers as |
| 1696 | * well as the rotator buffer output. |
| 1697 | * ROT_INDEX needs to be < 0x40, but > ctx->num_internal_frames. |
| 1698 | */ |
| 1699 | coda_write(dev, CODA_MAX_FRAMEBUFFERS + dst_buf->v4l2_buf.index, |
| 1700 | CODA9_CMD_DEC_PIC_ROT_INDEX); |
Philipp Zabel | 856d7d9 | 2014-09-29 09:53:44 -0300 | [diff] [blame] | 1701 | |
| 1702 | reg_addr = CODA9_CMD_DEC_PIC_ROT_ADDR_Y; |
| 1703 | reg_stride = CODA9_CMD_DEC_PIC_ROT_STRIDE; |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1704 | } else { |
Philipp Zabel | 856d7d9 | 2014-09-29 09:53:44 -0300 | [diff] [blame] | 1705 | reg_addr = CODA_CMD_DEC_PIC_ROT_ADDR_Y; |
| 1706 | reg_stride = CODA_CMD_DEC_PIC_ROT_STRIDE; |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1707 | } |
Philipp Zabel | 856d7d9 | 2014-09-29 09:53:44 -0300 | [diff] [blame] | 1708 | coda_write_base(ctx, q_data_dst, dst_buf, reg_addr); |
| 1709 | coda_write(dev, q_data_dst->bytesperline, reg_stride); |
| 1710 | |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1711 | coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode, |
| 1712 | CODA_CMD_DEC_PIC_ROT_MODE); |
| 1713 | |
| 1714 | switch (dev->devtype->product) { |
| 1715 | case CODA_DX6: |
| 1716 | /* TBD */ |
| 1717 | case CODA_7541: |
| 1718 | coda_write(dev, CODA_PRE_SCAN_EN, CODA_CMD_DEC_PIC_OPTION); |
| 1719 | break; |
| 1720 | case CODA_960: |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 1721 | /* 'hardcode to use interrupt disable mode'? */ |
| 1722 | coda_write(dev, (1 << 10), CODA_CMD_DEC_PIC_OPTION); |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1723 | break; |
| 1724 | } |
| 1725 | |
| 1726 | coda_write(dev, 0, CODA_CMD_DEC_PIC_SKIP_NUM); |
| 1727 | |
| 1728 | coda_write(dev, 0, CODA_CMD_DEC_PIC_BB_START); |
| 1729 | coda_write(dev, 0, CODA_CMD_DEC_PIC_START_BYTE); |
| 1730 | |
| 1731 | if (dev->devtype->product != CODA_DX6) |
| 1732 | coda_write(dev, ctx->iram_info.axi_sram_use, |
| 1733 | CODA7_REG_BIT_AXI_SRAM_USE); |
| 1734 | |
Philipp Zabel | 07ba277 | 2015-01-23 13:51:18 -0300 | [diff] [blame] | 1735 | meta = list_first_entry_or_null(&ctx->buffer_meta_list, |
| 1736 | struct coda_buffer_meta, list); |
| 1737 | |
| 1738 | if (meta && ctx->codec->src_fourcc == V4L2_PIX_FMT_JPEG) { |
Philipp Zabel | 5269aed | 2014-10-02 14:08:33 -0300 | [diff] [blame] | 1739 | |
| 1740 | /* If this is the last buffer in the bitstream, add padding */ |
Philipp Zabel | 5269aed | 2014-10-02 14:08:33 -0300 | [diff] [blame] | 1741 | if (meta->end == (ctx->bitstream_fifo.kfifo.in & |
| 1742 | ctx->bitstream_fifo.kfifo.mask)) { |
| 1743 | static unsigned char buf[512]; |
| 1744 | unsigned int pad; |
| 1745 | |
| 1746 | /* Pad to multiple of 256 and then add 256 more */ |
| 1747 | pad = ((0 - meta->end) & 0xff) + 256; |
| 1748 | |
| 1749 | memset(buf, 0xff, sizeof(buf)); |
| 1750 | |
| 1751 | kfifo_in(&ctx->bitstream_fifo, buf, pad); |
| 1752 | } |
| 1753 | } |
| 1754 | |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1755 | coda_kfifo_sync_to_device_full(ctx); |
| 1756 | |
Philipp Zabel | 8c9ef6b | 2015-01-23 13:51:24 -0300 | [diff] [blame] | 1757 | /* Clear decode success flag */ |
| 1758 | coda_write(dev, 0, CODA_RET_DEC_PIC_SUCCESS); |
| 1759 | |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1760 | coda_command_async(ctx, CODA_COMMAND_PIC_RUN); |
| 1761 | |
| 1762 | return 0; |
| 1763 | } |
| 1764 | |
| 1765 | static void coda_finish_decode(struct coda_ctx *ctx) |
| 1766 | { |
| 1767 | struct coda_dev *dev = ctx->dev; |
| 1768 | struct coda_q_data *q_data_src; |
| 1769 | struct coda_q_data *q_data_dst; |
| 1770 | struct vb2_buffer *dst_buf; |
Philipp Zabel | 7cbb105 | 2014-10-02 14:08:32 -0300 | [diff] [blame] | 1771 | struct coda_buffer_meta *meta; |
Philipp Zabel | 4de6931 | 2014-10-02 14:08:26 -0300 | [diff] [blame] | 1772 | unsigned long payload; |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1773 | int width, height; |
| 1774 | int decoded_idx; |
| 1775 | int display_idx; |
| 1776 | u32 src_fourcc; |
| 1777 | int success; |
| 1778 | u32 err_mb; |
| 1779 | u32 val; |
| 1780 | |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1781 | /* Update kfifo out pointer from coda bitstream read pointer */ |
| 1782 | coda_kfifo_sync_from_device(ctx); |
| 1783 | |
| 1784 | /* |
| 1785 | * in stream-end mode, the read pointer can overshoot the write pointer |
| 1786 | * by up to 512 bytes |
| 1787 | */ |
| 1788 | if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) { |
Philipp Zabel | ad532d3 | 2015-03-24 14:30:52 -0300 | [diff] [blame] | 1789 | if (coda_get_bitstream_payload(ctx) >= ctx->bitstream.size - 512) |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1790 | kfifo_init(&ctx->bitstream_fifo, |
| 1791 | ctx->bitstream.vaddr, ctx->bitstream.size); |
| 1792 | } |
| 1793 | |
| 1794 | q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); |
| 1795 | src_fourcc = q_data_src->fourcc; |
| 1796 | |
| 1797 | val = coda_read(dev, CODA_RET_DEC_PIC_SUCCESS); |
| 1798 | if (val != 1) |
| 1799 | pr_err("DEC_PIC_SUCCESS = %d\n", val); |
| 1800 | |
| 1801 | success = val & 0x1; |
| 1802 | if (!success) |
| 1803 | v4l2_err(&dev->v4l2_dev, "decode failed\n"); |
| 1804 | |
| 1805 | if (src_fourcc == V4L2_PIX_FMT_H264) { |
| 1806 | if (val & (1 << 3)) |
| 1807 | v4l2_err(&dev->v4l2_dev, |
| 1808 | "insufficient PS buffer space (%d bytes)\n", |
| 1809 | ctx->psbuf.size); |
| 1810 | if (val & (1 << 2)) |
| 1811 | v4l2_err(&dev->v4l2_dev, |
| 1812 | "insufficient slice buffer space (%d bytes)\n", |
| 1813 | ctx->slicebuf.size); |
| 1814 | } |
| 1815 | |
| 1816 | val = coda_read(dev, CODA_RET_DEC_PIC_SIZE); |
| 1817 | width = (val >> 16) & 0xffff; |
| 1818 | height = val & 0xffff; |
| 1819 | |
| 1820 | q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE); |
| 1821 | |
| 1822 | /* frame crop information */ |
| 1823 | if (src_fourcc == V4L2_PIX_FMT_H264) { |
| 1824 | u32 left_right; |
| 1825 | u32 top_bottom; |
| 1826 | |
| 1827 | left_right = coda_read(dev, CODA_RET_DEC_PIC_CROP_LEFT_RIGHT); |
| 1828 | top_bottom = coda_read(dev, CODA_RET_DEC_PIC_CROP_TOP_BOTTOM); |
| 1829 | |
| 1830 | if (left_right == 0xffffffff && top_bottom == 0xffffffff) { |
| 1831 | /* Keep current crop information */ |
| 1832 | } else { |
| 1833 | struct v4l2_rect *rect = &q_data_dst->rect; |
| 1834 | |
| 1835 | rect->left = left_right >> 16 & 0xffff; |
| 1836 | rect->top = top_bottom >> 16 & 0xffff; |
| 1837 | rect->width = width - rect->left - |
| 1838 | (left_right & 0xffff); |
| 1839 | rect->height = height - rect->top - |
| 1840 | (top_bottom & 0xffff); |
| 1841 | } |
| 1842 | } else { |
| 1843 | /* no cropping */ |
| 1844 | } |
| 1845 | |
| 1846 | err_mb = coda_read(dev, CODA_RET_DEC_PIC_ERR_MB); |
| 1847 | if (err_mb > 0) |
| 1848 | v4l2_err(&dev->v4l2_dev, |
| 1849 | "errors in %d macroblocks\n", err_mb); |
| 1850 | |
| 1851 | if (dev->devtype->product == CODA_7541) { |
| 1852 | val = coda_read(dev, CODA_RET_DEC_PIC_OPTION); |
| 1853 | if (val == 0) { |
| 1854 | /* not enough bitstream data */ |
| 1855 | v4l2_dbg(1, coda_debug, &dev->v4l2_dev, |
| 1856 | "prescan failed: %d\n", val); |
| 1857 | ctx->hold = true; |
| 1858 | return; |
| 1859 | } |
| 1860 | } |
| 1861 | |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 1862 | ctx->frm_dis_flg = coda_read(dev, |
| 1863 | CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx)); |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1864 | |
| 1865 | /* |
| 1866 | * The previous display frame was copied out by the rotator, |
| 1867 | * now it can be overwritten again |
| 1868 | */ |
| 1869 | if (ctx->display_idx >= 0 && |
| 1870 | ctx->display_idx < ctx->num_internal_frames) { |
| 1871 | ctx->frm_dis_flg &= ~(1 << ctx->display_idx); |
| 1872 | coda_write(dev, ctx->frm_dis_flg, |
| 1873 | CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx)); |
| 1874 | } |
| 1875 | |
| 1876 | /* |
| 1877 | * The index of the last decoded frame, not necessarily in |
| 1878 | * display order, and the index of the next display frame. |
| 1879 | * The latter could have been decoded in a previous run. |
| 1880 | */ |
| 1881 | decoded_idx = coda_read(dev, CODA_RET_DEC_PIC_CUR_IDX); |
| 1882 | display_idx = coda_read(dev, CODA_RET_DEC_PIC_FRAME_IDX); |
| 1883 | |
| 1884 | if (decoded_idx == -1) { |
| 1885 | /* no frame was decoded, but we might have a display frame */ |
| 1886 | if (display_idx >= 0 && display_idx < ctx->num_internal_frames) |
| 1887 | ctx->sequence_offset++; |
| 1888 | else if (ctx->display_idx < 0) |
| 1889 | ctx->hold = true; |
| 1890 | } else if (decoded_idx == -2) { |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 1891 | /* no frame was decoded, we still return remaining buffers */ |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1892 | } else if (decoded_idx < 0 || decoded_idx >= ctx->num_internal_frames) { |
| 1893 | v4l2_err(&dev->v4l2_dev, |
| 1894 | "decoded frame index out of range: %d\n", decoded_idx); |
| 1895 | } else { |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1896 | val = coda_read(dev, CODA_RET_DEC_PIC_FRAME_NUM) - 1; |
| 1897 | val -= ctx->sequence_offset; |
Philipp Zabel | 18fd0cc | 2014-08-05 14:00:17 -0300 | [diff] [blame] | 1898 | mutex_lock(&ctx->bitstream_mutex); |
Philipp Zabel | 7cbb105 | 2014-10-02 14:08:32 -0300 | [diff] [blame] | 1899 | if (!list_empty(&ctx->buffer_meta_list)) { |
| 1900 | meta = list_first_entry(&ctx->buffer_meta_list, |
| 1901 | struct coda_buffer_meta, list); |
| 1902 | list_del(&meta->list); |
| 1903 | if (val != (meta->sequence & 0xffff)) { |
Philipp Zabel | 18fd0cc | 2014-08-05 14:00:17 -0300 | [diff] [blame] | 1904 | v4l2_err(&dev->v4l2_dev, |
| 1905 | "sequence number mismatch (%d(%d) != %d)\n", |
| 1906 | val, ctx->sequence_offset, |
Philipp Zabel | 7cbb105 | 2014-10-02 14:08:32 -0300 | [diff] [blame] | 1907 | meta->sequence); |
Philipp Zabel | 18fd0cc | 2014-08-05 14:00:17 -0300 | [diff] [blame] | 1908 | } |
Philipp Zabel | 7cbb105 | 2014-10-02 14:08:32 -0300 | [diff] [blame] | 1909 | ctx->frame_metas[decoded_idx] = *meta; |
| 1910 | kfree(meta); |
Philipp Zabel | 18fd0cc | 2014-08-05 14:00:17 -0300 | [diff] [blame] | 1911 | } else { |
| 1912 | v4l2_err(&dev->v4l2_dev, "empty timestamp list!\n"); |
Philipp Zabel | 7cbb105 | 2014-10-02 14:08:32 -0300 | [diff] [blame] | 1913 | memset(&ctx->frame_metas[decoded_idx], 0, |
| 1914 | sizeof(struct coda_buffer_meta)); |
| 1915 | ctx->frame_metas[decoded_idx].sequence = val; |
Lucas Stach | 83f31c8 | 2015-01-23 13:51:19 -0300 | [diff] [blame] | 1916 | ctx->sequence_offset++; |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1917 | } |
Philipp Zabel | 18fd0cc | 2014-08-05 14:00:17 -0300 | [diff] [blame] | 1918 | mutex_unlock(&ctx->bitstream_mutex); |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1919 | |
| 1920 | val = coda_read(dev, CODA_RET_DEC_PIC_TYPE) & 0x7; |
| 1921 | if (val == 0) |
| 1922 | ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_KEYFRAME; |
| 1923 | else if (val == 1) |
| 1924 | ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_PFRAME; |
| 1925 | else |
| 1926 | ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_BFRAME; |
| 1927 | |
| 1928 | ctx->frame_errors[decoded_idx] = err_mb; |
| 1929 | } |
| 1930 | |
| 1931 | if (display_idx == -1) { |
| 1932 | /* |
| 1933 | * no more frames to be decoded, but there could still |
| 1934 | * be rotator output to dequeue |
| 1935 | */ |
| 1936 | ctx->hold = true; |
| 1937 | } else if (display_idx == -3) { |
| 1938 | /* possibly prescan failure */ |
| 1939 | } else if (display_idx < 0 || display_idx >= ctx->num_internal_frames) { |
| 1940 | v4l2_err(&dev->v4l2_dev, |
| 1941 | "presentation frame index out of range: %d\n", |
| 1942 | display_idx); |
| 1943 | } |
| 1944 | |
| 1945 | /* If a frame was copied out, return it */ |
| 1946 | if (ctx->display_idx >= 0 && |
| 1947 | ctx->display_idx < ctx->num_internal_frames) { |
| 1948 | dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx); |
| 1949 | dst_buf->v4l2_buf.sequence = ctx->osequence++; |
| 1950 | |
| 1951 | dst_buf->v4l2_buf.flags &= ~(V4L2_BUF_FLAG_KEYFRAME | |
| 1952 | V4L2_BUF_FLAG_PFRAME | |
| 1953 | V4L2_BUF_FLAG_BFRAME); |
| 1954 | dst_buf->v4l2_buf.flags |= ctx->frame_types[ctx->display_idx]; |
Philipp Zabel | 7cbb105 | 2014-10-02 14:08:32 -0300 | [diff] [blame] | 1955 | meta = &ctx->frame_metas[ctx->display_idx]; |
| 1956 | dst_buf->v4l2_buf.timecode = meta->timecode; |
| 1957 | dst_buf->v4l2_buf.timestamp = meta->timestamp; |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1958 | |
Philipp Zabel | 4de6931 | 2014-10-02 14:08:26 -0300 | [diff] [blame] | 1959 | switch (q_data_dst->fourcc) { |
| 1960 | case V4L2_PIX_FMT_YUV420: |
| 1961 | case V4L2_PIX_FMT_YVU420: |
| 1962 | case V4L2_PIX_FMT_NV12: |
| 1963 | default: |
| 1964 | payload = width * height * 3 / 2; |
| 1965 | break; |
| 1966 | case V4L2_PIX_FMT_YUV422P: |
| 1967 | payload = width * height * 2; |
| 1968 | break; |
| 1969 | } |
| 1970 | vb2_set_plane_payload(dst_buf, 0, payload); |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1971 | |
| 1972 | v4l2_m2m_buf_done(dst_buf, ctx->frame_errors[display_idx] ? |
| 1973 | VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE); |
| 1974 | |
| 1975 | v4l2_dbg(1, coda_debug, &dev->v4l2_dev, |
| 1976 | "job finished: decoding frame (%d) (%s)\n", |
| 1977 | dst_buf->v4l2_buf.sequence, |
| 1978 | (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ? |
| 1979 | "KEYFRAME" : "PFRAME"); |
| 1980 | } else { |
| 1981 | v4l2_dbg(1, coda_debug, &dev->v4l2_dev, |
| 1982 | "job finished: no frame decoded\n"); |
| 1983 | } |
| 1984 | |
| 1985 | /* The rotator will copy the current display frame next time */ |
| 1986 | ctx->display_idx = display_idx; |
| 1987 | } |
| 1988 | |
| 1989 | const struct coda_context_ops coda_bit_decode_ops = { |
| 1990 | .queue_init = coda_decoder_queue_init, |
Philipp Zabel | 73751da | 2015-03-24 14:30:51 -0300 | [diff] [blame] | 1991 | .reqbufs = coda_decoder_reqbufs, |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 1992 | .start_streaming = coda_start_decoding, |
| 1993 | .prepare_run = coda_prepare_decode, |
| 1994 | .finish_run = coda_finish_decode, |
| 1995 | .seq_end_work = coda_seq_end_work, |
| 1996 | .release = coda_bit_release, |
| 1997 | }; |
| 1998 | |
| 1999 | irqreturn_t coda_irq_handler(int irq, void *data) |
| 2000 | { |
| 2001 | struct coda_dev *dev = data; |
| 2002 | struct coda_ctx *ctx; |
| 2003 | |
| 2004 | /* read status register to attend the IRQ */ |
| 2005 | coda_read(dev, CODA_REG_BIT_INT_STATUS); |
| 2006 | coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET, |
| 2007 | CODA_REG_BIT_INT_CLEAR); |
| 2008 | |
| 2009 | ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev); |
| 2010 | if (ctx == NULL) { |
Philipp Zabel | f23797b | 2014-08-06 08:02:23 -0300 | [diff] [blame] | 2011 | v4l2_err(&dev->v4l2_dev, |
| 2012 | "Instance released before the end of transaction\n"); |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 2013 | mutex_unlock(&dev->coda_mutex); |
| 2014 | return IRQ_HANDLED; |
| 2015 | } |
| 2016 | |
| 2017 | if (ctx->aborting) { |
| 2018 | v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, |
| 2019 | "task has been aborted\n"); |
| 2020 | } |
| 2021 | |
| 2022 | if (coda_isbusy(ctx->dev)) { |
| 2023 | v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, |
| 2024 | "coda is still busy!!!!\n"); |
| 2025 | return IRQ_NONE; |
| 2026 | } |
| 2027 | |
| 2028 | complete(&ctx->completion); |
| 2029 | |
| 2030 | return IRQ_HANDLED; |
| 2031 | } |