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