blob: 12b93867e6657b3eb21572f22a7834d0c2af1639 [file] [log] [blame]
Philipp Zabel79924ca2014-07-23 12:28:45 -03001/*
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 Estevamc0aaf692014-09-19 08:32:30 -030020#include <linux/slab.h>
Philipp Zabel79924ca2014-07-23 12:28:45 -030021#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
39static inline int coda_is_initialized(struct coda_dev *dev)
40{
Philipp Zabelf23797b2014-08-06 08:02:23 -030041 return coda_read(dev, CODA_REG_BIT_CUR_PC) != 0;
Philipp Zabel79924ca2014-07-23 12:28:45 -030042}
43
44static inline unsigned long coda_isbusy(struct coda_dev *dev)
45{
46 return coda_read(dev, CODA_REG_BIT_BUSY);
47}
48
49static 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
60static 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
90static 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
98int 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
134static 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
147static 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
159static 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 Zabelf23797b2014-08-06 08:02:23 -0300169static int coda_bitstream_queue(struct coda_ctx *ctx,
170 struct vb2_buffer *src_buf)
Philipp Zabel79924ca2014-07-23 12:28:45 -0300171{
172 u32 src_size = vb2_get_plane_payload(src_buf, 0);
173 u32 n;
174
Philipp Zabelf23797b2014-08-06 08:02:23 -0300175 n = kfifo_in(&ctx->bitstream_fifo, vb2_plane_vaddr(src_buf, 0),
176 src_size);
Philipp Zabel79924ca2014-07-23 12:28:45 -0300177 if (n < src_size)
178 return -ENOSPC;
179
Philipp Zabelf23797b2014-08-06 08:02:23 -0300180 dma_sync_single_for_device(&ctx->dev->plat_dev->dev,
181 ctx->bitstream.paddr, ctx->bitstream.size,
182 DMA_TO_DEVICE);
Philipp Zabel79924ca2014-07-23 12:28:45 -0300183
184 src_buf->v4l2_buf.sequence = ctx->qsequence++;
185
186 return 0;
187}
188
189static 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
217void coda_fill_bitstream(struct coda_ctx *ctx)
218{
219 struct vb2_buffer *src_buf;
Philipp Zabel7cbb1052014-10-02 14:08:32 -0300220 struct coda_buffer_meta *meta;
221 u32 start;
Philipp Zabel79924ca2014-07-23 12:28:45 -0300222
223 while (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) > 0) {
Philipp Zabel619165628d2014-10-02 14:08:34 -0300224 /*
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 Zabel79924ca2014-07-23 12:28:45 -0300232 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
233
Philipp Zabeledc16cb2014-10-08 13:09:27 -0300234 /* Drop frames that do not start/end with a SOI/EOI markers */
235 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_JPEG &&
236 !coda_jpeg_check_buffer(ctx, src_buf)) {
237 v4l2_err(&ctx->dev->v4l2_dev,
238 "dropping invalid JPEG frame\n");
239 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
240 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
241 continue;
242 }
243
Philipp Zabel7cbb1052014-10-02 14:08:32 -0300244 /* Buffer start position */
245 start = ctx->bitstream_fifo.kfifo.in &
246 ctx->bitstream_fifo.kfifo.mask;
247
Philipp Zabel79924ca2014-07-23 12:28:45 -0300248 if (coda_bitstream_try_queue(ctx, src_buf)) {
249 /*
250 * Source buffer is queued in the bitstream ringbuffer;
251 * queue the timestamp and mark source buffer as done
252 */
253 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
254
Philipp Zabel7cbb1052014-10-02 14:08:32 -0300255 meta = kmalloc(sizeof(*meta), GFP_KERNEL);
256 if (meta) {
257 meta->sequence = src_buf->v4l2_buf.sequence;
258 meta->timecode = src_buf->v4l2_buf.timecode;
259 meta->timestamp = src_buf->v4l2_buf.timestamp;
260 meta->start = start;
261 meta->end = ctx->bitstream_fifo.kfifo.in &
262 ctx->bitstream_fifo.kfifo.mask;
263 list_add_tail(&meta->list,
264 &ctx->buffer_meta_list);
Philipp Zabel79924ca2014-07-23 12:28:45 -0300265 }
266
267 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
268 } else {
269 break;
270 }
271 }
272}
273
274void coda_bit_stream_end_flag(struct coda_ctx *ctx)
275{
276 struct coda_dev *dev = ctx->dev;
277
278 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
279
Philipp Zabelf23797b2014-08-06 08:02:23 -0300280 /* If this context is currently running, update the hardware flag */
Philipp Zabel79924ca2014-07-23 12:28:45 -0300281 if ((dev->devtype->product == CODA_960) &&
282 coda_isbusy(dev) &&
283 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
Philipp Zabelf23797b2014-08-06 08:02:23 -0300284 coda_write(dev, ctx->bit_stream_param,
285 CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel79924ca2014-07-23 12:28:45 -0300286 }
287}
288
289static void coda_parabuf_write(struct coda_ctx *ctx, int index, u32 value)
290{
291 struct coda_dev *dev = ctx->dev;
292 u32 *p = ctx->parabuf.vaddr;
293
294 if (dev->devtype->product == CODA_DX6)
295 p[index] = value;
296 else
297 p[index ^ 1] = value;
298}
299
300static void coda_free_framebuffers(struct coda_ctx *ctx)
301{
302 int i;
303
304 for (i = 0; i < CODA_MAX_FRAMEBUFFERS; i++)
305 coda_free_aux_buf(ctx->dev, &ctx->internal_frames[i]);
306}
307
308static int coda_alloc_framebuffers(struct coda_ctx *ctx,
309 struct coda_q_data *q_data, u32 fourcc)
310{
311 struct coda_dev *dev = ctx->dev;
312 int width, height;
313 dma_addr_t paddr;
314 int ysize;
315 int ret;
316 int i;
317
318 if (ctx->codec && (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 ||
319 ctx->codec->dst_fourcc == V4L2_PIX_FMT_H264)) {
320 width = round_up(q_data->width, 16);
321 height = round_up(q_data->height, 16);
322 } else {
323 width = round_up(q_data->width, 8);
324 height = q_data->height;
325 }
326 ysize = width * height;
327
328 /* Allocate frame buffers */
329 for (i = 0; i < ctx->num_internal_frames; i++) {
330 size_t size;
331 char *name;
332
333 size = ysize + ysize / 2;
334 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
335 dev->devtype->product != CODA_DX6)
336 size += ysize / 4;
337 name = kasprintf(GFP_KERNEL, "fb%d", i);
338 ret = coda_alloc_context_buf(ctx, &ctx->internal_frames[i],
339 size, name);
340 kfree(name);
341 if (ret < 0) {
342 coda_free_framebuffers(ctx);
343 return ret;
344 }
345 }
346
347 /* Register frame buffers in the parameter buffer */
348 for (i = 0; i < ctx->num_internal_frames; i++) {
349 paddr = ctx->internal_frames[i].paddr;
Philipp Zabelf23797b2014-08-06 08:02:23 -0300350 /* Start addresses of Y, Cb, Cr planes */
351 coda_parabuf_write(ctx, i * 3 + 0, paddr);
352 coda_parabuf_write(ctx, i * 3 + 1, paddr + ysize);
353 coda_parabuf_write(ctx, i * 3 + 2, paddr + ysize + ysize / 4);
Philipp Zabel79924ca2014-07-23 12:28:45 -0300354
355 /* mvcol buffer for h.264 */
356 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
357 dev->devtype->product != CODA_DX6)
358 coda_parabuf_write(ctx, 96 + i,
359 ctx->internal_frames[i].paddr +
360 ysize + ysize/4 + ysize/4);
361 }
362
363 /* mvcol buffer for mpeg4 */
364 if ((dev->devtype->product != CODA_DX6) &&
365 (ctx->codec->src_fourcc == V4L2_PIX_FMT_MPEG4))
366 coda_parabuf_write(ctx, 97, ctx->internal_frames[i].paddr +
367 ysize + ysize/4 + ysize/4);
368
369 return 0;
370}
371
372static void coda_free_context_buffers(struct coda_ctx *ctx)
373{
374 struct coda_dev *dev = ctx->dev;
375
376 coda_free_aux_buf(dev, &ctx->slicebuf);
377 coda_free_aux_buf(dev, &ctx->psbuf);
378 if (dev->devtype->product != CODA_DX6)
379 coda_free_aux_buf(dev, &ctx->workbuf);
380}
381
382static int coda_alloc_context_buffers(struct coda_ctx *ctx,
383 struct coda_q_data *q_data)
384{
385 struct coda_dev *dev = ctx->dev;
386 size_t size;
387 int ret;
388
389 if (dev->devtype->product == CODA_DX6)
390 return 0;
391
392 if (ctx->psbuf.vaddr) {
393 v4l2_err(&dev->v4l2_dev, "psmembuf still allocated\n");
394 return -EBUSY;
395 }
396 if (ctx->slicebuf.vaddr) {
397 v4l2_err(&dev->v4l2_dev, "slicebuf still allocated\n");
398 return -EBUSY;
399 }
400 if (ctx->workbuf.vaddr) {
401 v4l2_err(&dev->v4l2_dev, "context buffer still allocated\n");
402 ret = -EBUSY;
403 return -ENOMEM;
404 }
405
406 if (q_data->fourcc == V4L2_PIX_FMT_H264) {
407 /* worst case slice size */
408 size = (DIV_ROUND_UP(q_data->width, 16) *
409 DIV_ROUND_UP(q_data->height, 16)) * 3200 / 8 + 512;
Philipp Zabelf23797b2014-08-06 08:02:23 -0300410 ret = coda_alloc_context_buf(ctx, &ctx->slicebuf, size,
411 "slicebuf");
Philipp Zabel79924ca2014-07-23 12:28:45 -0300412 if (ret < 0) {
Philipp Zabelf23797b2014-08-06 08:02:23 -0300413 v4l2_err(&dev->v4l2_dev,
414 "failed to allocate %d byte slice buffer",
Philipp Zabel79924ca2014-07-23 12:28:45 -0300415 ctx->slicebuf.size);
416 return ret;
417 }
418 }
419
420 if (dev->devtype->product == CODA_7541) {
Philipp Zabelf23797b2014-08-06 08:02:23 -0300421 ret = coda_alloc_context_buf(ctx, &ctx->psbuf,
422 CODA7_PS_BUF_SIZE, "psbuf");
Philipp Zabel79924ca2014-07-23 12:28:45 -0300423 if (ret < 0) {
Philipp Zabelf23797b2014-08-06 08:02:23 -0300424 v4l2_err(&dev->v4l2_dev,
425 "failed to allocate psmem buffer");
Philipp Zabel79924ca2014-07-23 12:28:45 -0300426 goto err;
427 }
428 }
429
430 size = dev->devtype->workbuf_size;
431 if (dev->devtype->product == CODA_960 &&
432 q_data->fourcc == V4L2_PIX_FMT_H264)
433 size += CODA9_PS_SAVE_SIZE;
434 ret = coda_alloc_context_buf(ctx, &ctx->workbuf, size, "workbuf");
435 if (ret < 0) {
Philipp Zabelf23797b2014-08-06 08:02:23 -0300436 v4l2_err(&dev->v4l2_dev,
437 "failed to allocate %d byte context buffer",
Philipp Zabel79924ca2014-07-23 12:28:45 -0300438 ctx->workbuf.size);
439 goto err;
440 }
441
442 return 0;
443
444err:
445 coda_free_context_buffers(ctx);
446 return ret;
447}
448
449static int coda_encode_header(struct coda_ctx *ctx, struct vb2_buffer *buf,
450 int header_code, u8 *header, int *size)
451{
452 struct coda_dev *dev = ctx->dev;
453 size_t bufsize;
454 int ret;
455 int i;
456
457 if (dev->devtype->product == CODA_960)
458 memset(vb2_plane_vaddr(buf, 0), 0, 64);
459
460 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0),
461 CODA_CMD_ENC_HEADER_BB_START);
462 bufsize = vb2_plane_size(buf, 0);
463 if (dev->devtype->product == CODA_960)
464 bufsize /= 1024;
465 coda_write(dev, bufsize, CODA_CMD_ENC_HEADER_BB_SIZE);
466 coda_write(dev, header_code, CODA_CMD_ENC_HEADER_CODE);
467 ret = coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER);
468 if (ret < 0) {
469 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
470 return ret;
471 }
472
473 if (dev->devtype->product == CODA_960) {
474 for (i = 63; i > 0; i--)
475 if (((char *)vb2_plane_vaddr(buf, 0))[i] != 0)
476 break;
477 *size = i + 1;
478 } else {
479 *size = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx)) -
480 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
481 }
482 memcpy(header, vb2_plane_vaddr(buf, 0), *size);
483
484 return 0;
485}
486
487static phys_addr_t coda_iram_alloc(struct coda_iram_info *iram, size_t size)
488{
489 phys_addr_t ret;
490
491 size = round_up(size, 1024);
492 if (size > iram->remaining)
493 return 0;
494 iram->remaining -= size;
495
496 ret = iram->next_paddr;
497 iram->next_paddr += size;
498
499 return ret;
500}
501
502static void coda_setup_iram(struct coda_ctx *ctx)
503{
504 struct coda_iram_info *iram_info = &ctx->iram_info;
505 struct coda_dev *dev = ctx->dev;
Philipp Zabelf23797b2014-08-06 08:02:23 -0300506 int w64, w128;
Philipp Zabel79924ca2014-07-23 12:28:45 -0300507 int mb_width;
508 int dbk_bits;
509 int bit_bits;
510 int ip_bits;
511
512 memset(iram_info, 0, sizeof(*iram_info));
513 iram_info->next_paddr = dev->iram.paddr;
514 iram_info->remaining = dev->iram.size;
515
Philipp Zabel8be31c82014-08-05 14:00:13 -0300516 if (!dev->iram.vaddr)
517 return;
518
Philipp Zabel79924ca2014-07-23 12:28:45 -0300519 switch (dev->devtype->product) {
520 case CODA_7541:
521 dbk_bits = CODA7_USE_HOST_DBK_ENABLE | CODA7_USE_DBK_ENABLE;
522 bit_bits = CODA7_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
523 ip_bits = CODA7_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
524 break;
525 case CODA_960:
526 dbk_bits = CODA9_USE_HOST_DBK_ENABLE | CODA9_USE_DBK_ENABLE;
527 bit_bits = CODA9_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
528 ip_bits = CODA9_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
529 break;
530 default: /* CODA_DX6 */
531 return;
532 }
533
534 if (ctx->inst_type == CODA_INST_ENCODER) {
535 struct coda_q_data *q_data_src;
536
537 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
538 mb_width = DIV_ROUND_UP(q_data_src->width, 16);
Philipp Zabelf23797b2014-08-06 08:02:23 -0300539 w128 = mb_width * 128;
540 w64 = mb_width * 64;
Philipp Zabel79924ca2014-07-23 12:28:45 -0300541
542 /* Prioritize in case IRAM is too small for everything */
543 if (dev->devtype->product == CODA_7541) {
544 iram_info->search_ram_size = round_up(mb_width * 16 *
545 36 + 2048, 1024);
546 iram_info->search_ram_paddr = coda_iram_alloc(iram_info,
Philipp Zabelf23797b2014-08-06 08:02:23 -0300547 iram_info->search_ram_size);
Philipp Zabel79924ca2014-07-23 12:28:45 -0300548 if (!iram_info->search_ram_paddr) {
549 pr_err("IRAM is smaller than the search ram size\n");
550 goto out;
551 }
552 iram_info->axi_sram_use |= CODA7_USE_HOST_ME_ENABLE |
553 CODA7_USE_ME_ENABLE;
554 }
555
556 /* Only H.264BP and H.263P3 are considered */
Philipp Zabelf23797b2014-08-06 08:02:23 -0300557 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, w64);
558 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, w64);
Philipp Zabel79924ca2014-07-23 12:28:45 -0300559 if (!iram_info->buf_dbk_c_use)
560 goto out;
561 iram_info->axi_sram_use |= dbk_bits;
562
Philipp Zabelf23797b2014-08-06 08:02:23 -0300563 iram_info->buf_bit_use = coda_iram_alloc(iram_info, w128);
Philipp Zabel79924ca2014-07-23 12:28:45 -0300564 if (!iram_info->buf_bit_use)
565 goto out;
566 iram_info->axi_sram_use |= bit_bits;
567
Philipp Zabelf23797b2014-08-06 08:02:23 -0300568 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, w128);
Philipp Zabel79924ca2014-07-23 12:28:45 -0300569 if (!iram_info->buf_ip_ac_dc_use)
570 goto out;
571 iram_info->axi_sram_use |= ip_bits;
572
573 /* OVL and BTP disabled for encoder */
574 } else if (ctx->inst_type == CODA_INST_DECODER) {
575 struct coda_q_data *q_data_dst;
576
577 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
578 mb_width = DIV_ROUND_UP(q_data_dst->width, 16);
Philipp Zabelf23797b2014-08-06 08:02:23 -0300579 w128 = mb_width * 128;
Philipp Zabel79924ca2014-07-23 12:28:45 -0300580
Philipp Zabelf23797b2014-08-06 08:02:23 -0300581 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, w128);
582 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, w128);
Philipp Zabel79924ca2014-07-23 12:28:45 -0300583 if (!iram_info->buf_dbk_c_use)
584 goto out;
585 iram_info->axi_sram_use |= dbk_bits;
586
Philipp Zabelf23797b2014-08-06 08:02:23 -0300587 iram_info->buf_bit_use = coda_iram_alloc(iram_info, w128);
Philipp Zabel79924ca2014-07-23 12:28:45 -0300588 if (!iram_info->buf_bit_use)
589 goto out;
590 iram_info->axi_sram_use |= bit_bits;
591
Philipp Zabelf23797b2014-08-06 08:02:23 -0300592 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, w128);
Philipp Zabel79924ca2014-07-23 12:28:45 -0300593 if (!iram_info->buf_ip_ac_dc_use)
594 goto out;
595 iram_info->axi_sram_use |= ip_bits;
596
597 /* OVL and BTP unused as there is no VC1 support yet */
598 }
599
600out:
601 if (!(iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE))
602 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
603 "IRAM smaller than needed\n");
604
605 if (dev->devtype->product == CODA_7541) {
606 /* TODO - Enabling these causes picture errors on CODA7541 */
607 if (ctx->inst_type == CODA_INST_DECODER) {
608 /* fw 1.4.50 */
609 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
610 CODA7_USE_IP_ENABLE);
611 } else {
612 /* fw 13.4.29 */
613 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
614 CODA7_USE_HOST_DBK_ENABLE |
615 CODA7_USE_IP_ENABLE |
616 CODA7_USE_DBK_ENABLE);
617 }
618 }
619}
620
621static u32 coda_supported_firmwares[] = {
622 CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5),
623 CODA_FIRMWARE_VERNUM(CODA_7541, 1, 4, 50),
624 CODA_FIRMWARE_VERNUM(CODA_960, 2, 1, 5),
625};
626
627static bool coda_firmware_supported(u32 vernum)
628{
629 int i;
630
631 for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++)
632 if (vernum == coda_supported_firmwares[i])
633 return true;
634 return false;
635}
636
637int coda_check_firmware(struct coda_dev *dev)
638{
639 u16 product, major, minor, release;
640 u32 data;
641 int ret;
642
643 ret = clk_prepare_enable(dev->clk_per);
644 if (ret)
645 goto err_clk_per;
646
647 ret = clk_prepare_enable(dev->clk_ahb);
648 if (ret)
649 goto err_clk_ahb;
650
651 coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM);
652 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
653 coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX);
654 coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD);
655 coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND);
656 if (coda_wait_timeout(dev)) {
657 v4l2_err(&dev->v4l2_dev, "firmware get command error\n");
658 ret = -EIO;
659 goto err_run_cmd;
660 }
661
662 if (dev->devtype->product == CODA_960) {
663 data = coda_read(dev, CODA9_CMD_FIRMWARE_CODE_REV);
664 v4l2_info(&dev->v4l2_dev, "Firmware code revision: %d\n",
665 data);
666 }
667
668 /* Check we are compatible with the loaded firmware */
669 data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM);
670 product = CODA_FIRMWARE_PRODUCT(data);
671 major = CODA_FIRMWARE_MAJOR(data);
672 minor = CODA_FIRMWARE_MINOR(data);
673 release = CODA_FIRMWARE_RELEASE(data);
674
675 clk_disable_unprepare(dev->clk_per);
676 clk_disable_unprepare(dev->clk_ahb);
677
678 if (product != dev->devtype->product) {
Philipp Zabelf23797b2014-08-06 08:02:23 -0300679 v4l2_err(&dev->v4l2_dev,
680 "Wrong firmware. Hw: %s, Fw: %s, Version: %u.%u.%u\n",
Philipp Zabel79924ca2014-07-23 12:28:45 -0300681 coda_product_name(dev->devtype->product),
682 coda_product_name(product), major, minor, release);
683 return -EINVAL;
684 }
685
686 v4l2_info(&dev->v4l2_dev, "Initialized %s.\n",
687 coda_product_name(product));
688
689 if (coda_firmware_supported(data)) {
690 v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n",
691 major, minor, release);
692 } else {
Philipp Zabelf23797b2014-08-06 08:02:23 -0300693 v4l2_warn(&dev->v4l2_dev,
694 "Unsupported firmware version: %u.%u.%u\n",
695 major, minor, release);
Philipp Zabel79924ca2014-07-23 12:28:45 -0300696 }
697
698 return 0;
699
700err_run_cmd:
701 clk_disable_unprepare(dev->clk_ahb);
702err_clk_ahb:
703 clk_disable_unprepare(dev->clk_per);
704err_clk_per:
705 return ret;
706}
707
708/*
709 * Encoder context operations
710 */
711
Philipp Zabel73751da2015-03-24 14:30:51 -0300712static int coda_encoder_reqbufs(struct coda_ctx *ctx,
713 struct v4l2_requestbuffers *rb)
714{
715 struct coda_q_data *q_data_src;
716 int ret;
717
718 if (rb->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
719 return 0;
720
721 if (rb->count) {
722 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
723 ret = coda_alloc_context_buffers(ctx, q_data_src);
724 if (ret < 0)
725 return ret;
726 } else {
727 coda_free_context_buffers(ctx);
728 }
729
730 return 0;
731}
732
Philipp Zabel79924ca2014-07-23 12:28:45 -0300733static int coda_start_encoding(struct coda_ctx *ctx)
734{
735 struct coda_dev *dev = ctx->dev;
736 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
737 struct coda_q_data *q_data_src, *q_data_dst;
738 u32 bitstream_buf, bitstream_size;
739 struct vb2_buffer *buf;
740 int gamma, ret, value;
741 u32 dst_fourcc;
Philipp Zabele3cccff2015-01-23 13:51:28 -0300742 int num_fb;
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300743 u32 stride;
Philipp Zabel79924ca2014-07-23 12:28:45 -0300744
745 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
746 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
747 dst_fourcc = q_data_dst->fourcc;
748
Philipp Zabel79924ca2014-07-23 12:28:45 -0300749 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
750 bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0);
751 bitstream_size = q_data_dst->sizeimage;
752
753 if (!coda_is_initialized(dev)) {
754 v4l2_err(v4l2_dev, "coda is not initialized.\n");
755 return -EFAULT;
756 }
757
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300758 if (dst_fourcc == V4L2_PIX_FMT_JPEG) {
759 if (!ctx->params.jpeg_qmat_tab[0])
760 ctx->params.jpeg_qmat_tab[0] = kmalloc(64, GFP_KERNEL);
761 if (!ctx->params.jpeg_qmat_tab[1])
762 ctx->params.jpeg_qmat_tab[1] = kmalloc(64, GFP_KERNEL);
763 coda_set_jpeg_compression_quality(ctx, ctx->params.jpeg_quality);
764 }
765
Philipp Zabel79924ca2014-07-23 12:28:45 -0300766 mutex_lock(&dev->coda_mutex);
767
768 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
769 coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
770 coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
771 switch (dev->devtype->product) {
772 case CODA_DX6:
773 coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN |
774 CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
775 break;
776 case CODA_960:
777 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
778 /* fallthrough */
779 case CODA_7541:
780 coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN |
781 CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
782 break;
783 }
784
Philipp Zabel1cb12cf2014-09-29 09:53:47 -0300785 ctx->frame_mem_ctrl &= ~CODA_FRAME_CHROMA_INTERLEAVE;
786 if (q_data_src->fourcc == V4L2_PIX_FMT_NV12)
787 ctx->frame_mem_ctrl |= CODA_FRAME_CHROMA_INTERLEAVE;
Philipp Zabel2bf299c2014-09-29 09:53:46 -0300788 coda_write(dev, ctx->frame_mem_ctrl, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel79924ca2014-07-23 12:28:45 -0300789
790 if (dev->devtype->product == CODA_DX6) {
791 /* Configure the coda */
Philipp Zabelf23797b2014-08-06 08:02:23 -0300792 coda_write(dev, dev->iram.paddr,
793 CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR);
Philipp Zabel79924ca2014-07-23 12:28:45 -0300794 }
795
796 /* Could set rotation here if needed */
Philipp Zabel2bf299c2014-09-29 09:53:46 -0300797 value = 0;
Philipp Zabel79924ca2014-07-23 12:28:45 -0300798 switch (dev->devtype->product) {
799 case CODA_DX6:
Philipp Zabelf23797b2014-08-06 08:02:23 -0300800 value = (q_data_src->width & CODADX6_PICWIDTH_MASK)
801 << CODADX6_PICWIDTH_OFFSET;
802 value |= (q_data_src->height & CODADX6_PICHEIGHT_MASK)
803 << CODA_PICHEIGHT_OFFSET;
Philipp Zabel79924ca2014-07-23 12:28:45 -0300804 break;
805 case CODA_7541:
806 if (dst_fourcc == V4L2_PIX_FMT_H264) {
807 value = (round_up(q_data_src->width, 16) &
808 CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
809 value |= (round_up(q_data_src->height, 16) &
Philipp Zabelf23797b2014-08-06 08:02:23 -0300810 CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Philipp Zabel79924ca2014-07-23 12:28:45 -0300811 break;
812 }
813 /* fallthrough */
814 case CODA_960:
Philipp Zabelf23797b2014-08-06 08:02:23 -0300815 value = (q_data_src->width & CODA7_PICWIDTH_MASK)
816 << CODA7_PICWIDTH_OFFSET;
817 value |= (q_data_src->height & CODA7_PICHEIGHT_MASK)
818 << CODA_PICHEIGHT_OFFSET;
Philipp Zabel79924ca2014-07-23 12:28:45 -0300819 }
820 coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE);
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300821 if (dst_fourcc == V4L2_PIX_FMT_JPEG)
822 ctx->params.framerate = 0;
Philipp Zabel79924ca2014-07-23 12:28:45 -0300823 coda_write(dev, ctx->params.framerate,
824 CODA_CMD_ENC_SEQ_SRC_F_RATE);
825
826 ctx->params.codec_mode = ctx->codec->mode;
827 switch (dst_fourcc) {
828 case V4L2_PIX_FMT_MPEG4:
829 if (dev->devtype->product == CODA_960)
Philipp Zabelf23797b2014-08-06 08:02:23 -0300830 coda_write(dev, CODA9_STD_MPEG4,
831 CODA_CMD_ENC_SEQ_COD_STD);
Philipp Zabel79924ca2014-07-23 12:28:45 -0300832 else
Philipp Zabelf23797b2014-08-06 08:02:23 -0300833 coda_write(dev, CODA_STD_MPEG4,
834 CODA_CMD_ENC_SEQ_COD_STD);
Philipp Zabel79924ca2014-07-23 12:28:45 -0300835 coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA);
836 break;
837 case V4L2_PIX_FMT_H264:
838 if (dev->devtype->product == CODA_960)
Philipp Zabelf23797b2014-08-06 08:02:23 -0300839 coda_write(dev, CODA9_STD_H264,
840 CODA_CMD_ENC_SEQ_COD_STD);
Philipp Zabel79924ca2014-07-23 12:28:45 -0300841 else
Philipp Zabelf23797b2014-08-06 08:02:23 -0300842 coda_write(dev, CODA_STD_H264,
843 CODA_CMD_ENC_SEQ_COD_STD);
Philipp Zabel79924ca2014-07-23 12:28:45 -0300844 if (ctx->params.h264_deblk_enabled) {
845 value = ((ctx->params.h264_deblk_alpha &
846 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_MASK) <<
847 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_OFFSET) |
848 ((ctx->params.h264_deblk_beta &
849 CODA_264PARAM_DEBLKFILTEROFFSETBETA_MASK) <<
850 CODA_264PARAM_DEBLKFILTEROFFSETBETA_OFFSET);
851 } else {
852 value = 1 << CODA_264PARAM_DISABLEDEBLK_OFFSET;
853 }
854 coda_write(dev, value, CODA_CMD_ENC_SEQ_264_PARA);
855 break;
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300856 case V4L2_PIX_FMT_JPEG:
857 coda_write(dev, 0, CODA_CMD_ENC_SEQ_JPG_PARA);
858 coda_write(dev, ctx->params.jpeg_restart_interval,
859 CODA_CMD_ENC_SEQ_JPG_RST_INTERVAL);
860 coda_write(dev, 0, CODA_CMD_ENC_SEQ_JPG_THUMB_EN);
861 coda_write(dev, 0, CODA_CMD_ENC_SEQ_JPG_THUMB_SIZE);
862 coda_write(dev, 0, CODA_CMD_ENC_SEQ_JPG_THUMB_OFFSET);
863
864 coda_jpeg_write_tables(ctx);
865 break;
Philipp Zabel79924ca2014-07-23 12:28:45 -0300866 default:
867 v4l2_err(v4l2_dev,
868 "dst format (0x%08x) invalid.\n", dst_fourcc);
869 ret = -EINVAL;
870 goto out;
871 }
872
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300873 /*
874 * slice mode and GOP size registers are used for thumb size/offset
875 * in JPEG mode
876 */
877 if (dst_fourcc != V4L2_PIX_FMT_JPEG) {
878 switch (ctx->params.slice_mode) {
879 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE:
880 value = 0;
881 break;
882 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB:
883 value = (ctx->params.slice_max_mb &
884 CODA_SLICING_SIZE_MASK)
885 << CODA_SLICING_SIZE_OFFSET;
886 value |= (1 & CODA_SLICING_UNIT_MASK)
887 << CODA_SLICING_UNIT_OFFSET;
888 value |= 1 & CODA_SLICING_MODE_MASK;
889 break;
890 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES:
891 value = (ctx->params.slice_max_bits &
892 CODA_SLICING_SIZE_MASK)
893 << CODA_SLICING_SIZE_OFFSET;
894 value |= (0 & CODA_SLICING_UNIT_MASK)
895 << CODA_SLICING_UNIT_OFFSET;
896 value |= 1 & CODA_SLICING_MODE_MASK;
897 break;
898 }
899 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
900 value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
901 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
Philipp Zabel79924ca2014-07-23 12:28:45 -0300902 }
Philipp Zabel79924ca2014-07-23 12:28:45 -0300903
904 if (ctx->params.bitrate) {
905 /* Rate control enabled */
Philipp Zabelf23797b2014-08-06 08:02:23 -0300906 value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK)
907 << CODA_RATECONTROL_BITRATE_OFFSET;
Philipp Zabel79924ca2014-07-23 12:28:45 -0300908 value |= 1 & CODA_RATECONTROL_ENABLE_MASK;
909 if (dev->devtype->product == CODA_960)
910 value |= BIT(31); /* disable autoskip */
911 } else {
912 value = 0;
913 }
914 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA);
915
916 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE);
917 coda_write(dev, ctx->params.intra_refresh,
918 CODA_CMD_ENC_SEQ_INTRA_REFRESH);
919
920 coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START);
921 coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE);
922
923
924 value = 0;
925 if (dev->devtype->product == CODA_960)
926 gamma = CODA9_DEFAULT_GAMMA;
927 else
928 gamma = CODA_DEFAULT_GAMMA;
929 if (gamma > 0) {
930 coda_write(dev, (gamma & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET,
931 CODA_CMD_ENC_SEQ_RC_GAMMA);
932 }
933
934 if (ctx->params.h264_min_qp || ctx->params.h264_max_qp) {
935 coda_write(dev,
936 ctx->params.h264_min_qp << CODA_QPMIN_OFFSET |
937 ctx->params.h264_max_qp << CODA_QPMAX_OFFSET,
938 CODA_CMD_ENC_SEQ_RC_QP_MIN_MAX);
939 }
940 if (dev->devtype->product == CODA_960) {
941 if (ctx->params.h264_max_qp)
942 value |= 1 << CODA9_OPTION_RCQPMAX_OFFSET;
943 if (CODA_DEFAULT_GAMMA > 0)
944 value |= 1 << CODA9_OPTION_GAMMA_OFFSET;
945 } else {
946 if (CODA_DEFAULT_GAMMA > 0) {
947 if (dev->devtype->product == CODA_DX6)
948 value |= 1 << CODADX6_OPTION_GAMMA_OFFSET;
949 else
950 value |= 1 << CODA7_OPTION_GAMMA_OFFSET;
951 }
952 if (ctx->params.h264_min_qp)
953 value |= 1 << CODA7_OPTION_RCQPMIN_OFFSET;
954 if (ctx->params.h264_max_qp)
955 value |= 1 << CODA7_OPTION_RCQPMAX_OFFSET;
956 }
957 coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION);
958
959 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_INTERVAL_MODE);
960
961 coda_setup_iram(ctx);
962
963 if (dst_fourcc == V4L2_PIX_FMT_H264) {
964 switch (dev->devtype->product) {
965 case CODA_DX6:
966 value = FMO_SLICE_SAVE_BUF_SIZE << 7;
967 coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO);
968 break;
969 case CODA_7541:
970 coda_write(dev, ctx->iram_info.search_ram_paddr,
971 CODA7_CMD_ENC_SEQ_SEARCH_BASE);
972 coda_write(dev, ctx->iram_info.search_ram_size,
973 CODA7_CMD_ENC_SEQ_SEARCH_SIZE);
974 break;
975 case CODA_960:
976 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_ME_OPTION);
977 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_INTRA_WEIGHT);
978 }
979 }
980
981 ret = coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT);
982 if (ret < 0) {
983 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
984 goto out;
985 }
986
987 if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0) {
988 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT failed\n");
989 ret = -EFAULT;
990 goto out;
991 }
992
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300993 if (dst_fourcc != V4L2_PIX_FMT_JPEG) {
994 if (dev->devtype->product == CODA_960)
995 ctx->num_internal_frames = 4;
996 else
997 ctx->num_internal_frames = 2;
998 ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc);
999 if (ret < 0) {
1000 v4l2_err(v4l2_dev, "failed to allocate framebuffers\n");
1001 goto out;
1002 }
Philipp Zabele3cccff2015-01-23 13:51:28 -03001003 num_fb = 2;
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001004 stride = q_data_src->bytesperline;
1005 } else {
1006 ctx->num_internal_frames = 0;
Philipp Zabele3cccff2015-01-23 13:51:28 -03001007 num_fb = 0;
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001008 stride = 0;
Philipp Zabel79924ca2014-07-23 12:28:45 -03001009 }
Philipp Zabele3cccff2015-01-23 13:51:28 -03001010 coda_write(dev, num_fb, CODA_CMD_SET_FRAME_BUF_NUM);
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001011 coda_write(dev, stride, CODA_CMD_SET_FRAME_BUF_STRIDE);
1012
Philipp Zabel79924ca2014-07-23 12:28:45 -03001013 if (dev->devtype->product == CODA_7541) {
1014 coda_write(dev, q_data_src->bytesperline,
1015 CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE);
1016 }
1017 if (dev->devtype->product != CODA_DX6) {
1018 coda_write(dev, ctx->iram_info.buf_bit_use,
1019 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
1020 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
1021 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
1022 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
1023 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
1024 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
1025 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
1026 coda_write(dev, ctx->iram_info.buf_ovl_use,
1027 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
1028 if (dev->devtype->product == CODA_960) {
1029 coda_write(dev, ctx->iram_info.buf_btp_use,
1030 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
1031
1032 /* FIXME */
Philipp Zabelf23797b2014-08-06 08:02:23 -03001033 coda_write(dev, ctx->internal_frames[2].paddr,
1034 CODA9_CMD_SET_FRAME_SUBSAMP_A);
1035 coda_write(dev, ctx->internal_frames[3].paddr,
1036 CODA9_CMD_SET_FRAME_SUBSAMP_B);
Philipp Zabel79924ca2014-07-23 12:28:45 -03001037 }
1038 }
1039
1040 ret = coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF);
1041 if (ret < 0) {
1042 v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
1043 goto out;
1044 }
1045
1046 /* Save stream headers */
1047 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
1048 switch (dst_fourcc) {
1049 case V4L2_PIX_FMT_H264:
1050 /*
1051 * Get SPS in the first frame and copy it to an
1052 * intermediate buffer.
1053 */
1054 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_SPS,
1055 &ctx->vpu_header[0][0],
1056 &ctx->vpu_header_size[0]);
1057 if (ret < 0)
1058 goto out;
1059
1060 /*
1061 * Get PPS in the first frame and copy it to an
1062 * intermediate buffer.
1063 */
1064 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_PPS,
1065 &ctx->vpu_header[1][0],
1066 &ctx->vpu_header_size[1]);
1067 if (ret < 0)
1068 goto out;
1069
1070 /*
1071 * Length of H.264 headers is variable and thus it might not be
1072 * aligned for the coda to append the encoded frame. In that is
1073 * the case a filler NAL must be added to header 2.
1074 */
1075 ctx->vpu_header_size[2] = coda_h264_padding(
1076 (ctx->vpu_header_size[0] +
1077 ctx->vpu_header_size[1]),
1078 ctx->vpu_header[2]);
1079 break;
1080 case V4L2_PIX_FMT_MPEG4:
1081 /*
1082 * Get VOS in the first frame and copy it to an
1083 * intermediate buffer
1084 */
1085 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOS,
1086 &ctx->vpu_header[0][0],
1087 &ctx->vpu_header_size[0]);
1088 if (ret < 0)
1089 goto out;
1090
1091 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VIS,
1092 &ctx->vpu_header[1][0],
1093 &ctx->vpu_header_size[1]);
1094 if (ret < 0)
1095 goto out;
1096
1097 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOL,
1098 &ctx->vpu_header[2][0],
1099 &ctx->vpu_header_size[2]);
1100 if (ret < 0)
1101 goto out;
1102 break;
1103 default:
1104 /* No more formats need to save headers at the moment */
1105 break;
1106 }
1107
1108out:
1109 mutex_unlock(&dev->coda_mutex);
1110 return ret;
1111}
1112
1113static int coda_prepare_encode(struct coda_ctx *ctx)
1114{
1115 struct coda_q_data *q_data_src, *q_data_dst;
1116 struct vb2_buffer *src_buf, *dst_buf;
1117 struct coda_dev *dev = ctx->dev;
1118 int force_ipicture;
1119 int quant_param = 0;
Philipp Zabel79924ca2014-07-23 12:28:45 -03001120 u32 pic_stream_buffer_addr, pic_stream_buffer_size;
Philipp Zabel59ebc2e2014-09-29 09:53:45 -03001121 u32 rot_mode = 0;
Philipp Zabel79924ca2014-07-23 12:28:45 -03001122 u32 dst_fourcc;
Philipp Zabel856d7d92014-09-29 09:53:44 -03001123 u32 reg;
Philipp Zabel79924ca2014-07-23 12:28:45 -03001124
1125 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
1126 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
1127 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1128 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1129 dst_fourcc = q_data_dst->fourcc;
1130
1131 src_buf->v4l2_buf.sequence = ctx->osequence;
1132 dst_buf->v4l2_buf.sequence = ctx->osequence;
1133 ctx->osequence++;
1134
1135 /*
1136 * Workaround coda firmware BUG that only marks the first
1137 * frame as IDR. This is a problem for some decoders that can't
1138 * recover when a frame is lost.
1139 */
1140 if (src_buf->v4l2_buf.sequence % ctx->params.gop_size) {
1141 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
1142 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
1143 } else {
1144 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
1145 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
1146 }
1147
1148 if (dev->devtype->product == CODA_960)
1149 coda_set_gdi_regs(ctx);
1150
1151 /*
1152 * Copy headers at the beginning of the first frame for H.264 only.
1153 * In MPEG4 they are already copied by the coda.
1154 */
1155 if (src_buf->v4l2_buf.sequence == 0) {
1156 pic_stream_buffer_addr =
1157 vb2_dma_contig_plane_dma_addr(dst_buf, 0) +
1158 ctx->vpu_header_size[0] +
1159 ctx->vpu_header_size[1] +
1160 ctx->vpu_header_size[2];
Philipp Zabeld4c6a412014-10-02 14:08:35 -03001161 pic_stream_buffer_size = q_data_dst->sizeimage -
Philipp Zabel79924ca2014-07-23 12:28:45 -03001162 ctx->vpu_header_size[0] -
1163 ctx->vpu_header_size[1] -
1164 ctx->vpu_header_size[2];
1165 memcpy(vb2_plane_vaddr(dst_buf, 0),
1166 &ctx->vpu_header[0][0], ctx->vpu_header_size[0]);
1167 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0],
1168 &ctx->vpu_header[1][0], ctx->vpu_header_size[1]);
1169 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0] +
1170 ctx->vpu_header_size[1], &ctx->vpu_header[2][0],
1171 ctx->vpu_header_size[2]);
1172 } else {
1173 pic_stream_buffer_addr =
1174 vb2_dma_contig_plane_dma_addr(dst_buf, 0);
Philipp Zabeld4c6a412014-10-02 14:08:35 -03001175 pic_stream_buffer_size = q_data_dst->sizeimage;
Philipp Zabel79924ca2014-07-23 12:28:45 -03001176 }
1177
1178 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
1179 force_ipicture = 1;
1180 switch (dst_fourcc) {
1181 case V4L2_PIX_FMT_H264:
1182 quant_param = ctx->params.h264_intra_qp;
1183 break;
1184 case V4L2_PIX_FMT_MPEG4:
1185 quant_param = ctx->params.mpeg4_intra_qp;
1186 break;
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001187 case V4L2_PIX_FMT_JPEG:
1188 quant_param = 30;
1189 break;
Philipp Zabel79924ca2014-07-23 12:28:45 -03001190 default:
1191 v4l2_warn(&ctx->dev->v4l2_dev,
1192 "cannot set intra qp, fmt not supported\n");
1193 break;
1194 }
1195 } else {
1196 force_ipicture = 0;
1197 switch (dst_fourcc) {
1198 case V4L2_PIX_FMT_H264:
1199 quant_param = ctx->params.h264_inter_qp;
1200 break;
1201 case V4L2_PIX_FMT_MPEG4:
1202 quant_param = ctx->params.mpeg4_inter_qp;
1203 break;
1204 default:
1205 v4l2_warn(&ctx->dev->v4l2_dev,
1206 "cannot set inter qp, fmt not supported\n");
1207 break;
1208 }
1209 }
1210
1211 /* submit */
Philipp Zabel59ebc2e2014-09-29 09:53:45 -03001212 if (ctx->params.rot_mode)
1213 rot_mode = CODA_ROT_MIR_ENABLE | ctx->params.rot_mode;
1214 coda_write(dev, rot_mode, CODA_CMD_ENC_PIC_ROT_MODE);
Philipp Zabel79924ca2014-07-23 12:28:45 -03001215 coda_write(dev, quant_param, CODA_CMD_ENC_PIC_QS);
1216
Philipp Zabel79924ca2014-07-23 12:28:45 -03001217 if (dev->devtype->product == CODA_960) {
1218 coda_write(dev, 4/*FIXME: 0*/, CODA9_CMD_ENC_PIC_SRC_INDEX);
1219 coda_write(dev, q_data_src->width, CODA9_CMD_ENC_PIC_SRC_STRIDE);
1220 coda_write(dev, 0, CODA9_CMD_ENC_PIC_SUB_FRAME_SYNC);
1221
Philipp Zabel856d7d92014-09-29 09:53:44 -03001222 reg = CODA9_CMD_ENC_PIC_SRC_ADDR_Y;
Philipp Zabel79924ca2014-07-23 12:28:45 -03001223 } else {
Philipp Zabel856d7d92014-09-29 09:53:44 -03001224 reg = CODA_CMD_ENC_PIC_SRC_ADDR_Y;
Philipp Zabel79924ca2014-07-23 12:28:45 -03001225 }
Philipp Zabel856d7d92014-09-29 09:53:44 -03001226 coda_write_base(ctx, q_data_src, src_buf, reg);
1227
Philipp Zabel79924ca2014-07-23 12:28:45 -03001228 coda_write(dev, force_ipicture << 1 & 0x2,
1229 CODA_CMD_ENC_PIC_OPTION);
1230
1231 coda_write(dev, pic_stream_buffer_addr, CODA_CMD_ENC_PIC_BB_START);
1232 coda_write(dev, pic_stream_buffer_size / 1024,
1233 CODA_CMD_ENC_PIC_BB_SIZE);
1234
1235 if (!ctx->streamon_out) {
Philipp Zabelf23797b2014-08-06 08:02:23 -03001236 /* After streamoff on the output side, set stream end flag */
Philipp Zabel79924ca2014-07-23 12:28:45 -03001237 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
Philipp Zabelf23797b2014-08-06 08:02:23 -03001238 coda_write(dev, ctx->bit_stream_param,
1239 CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel79924ca2014-07-23 12:28:45 -03001240 }
1241
1242 if (dev->devtype->product != CODA_DX6)
1243 coda_write(dev, ctx->iram_info.axi_sram_use,
1244 CODA7_REG_BIT_AXI_SRAM_USE);
1245
1246 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
1247
1248 return 0;
1249}
1250
1251static void coda_finish_encode(struct coda_ctx *ctx)
1252{
1253 struct vb2_buffer *src_buf, *dst_buf;
1254 struct coda_dev *dev = ctx->dev;
1255 u32 wr_ptr, start_ptr;
1256
1257 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1258 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
1259
1260 /* Get results from the coda */
1261 start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START);
1262 wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
1263
1264 /* Calculate bytesused field */
1265 if (dst_buf->v4l2_buf.sequence == 0) {
1266 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr +
1267 ctx->vpu_header_size[0] +
1268 ctx->vpu_header_size[1] +
1269 ctx->vpu_header_size[2]);
1270 } else {
1271 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr);
1272 }
1273
1274 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n",
1275 wr_ptr - start_ptr);
1276
1277 coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM);
1278 coda_read(dev, CODA_RET_ENC_PIC_FLAG);
1279
1280 if (coda_read(dev, CODA_RET_ENC_PIC_TYPE) == 0) {
1281 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
1282 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
1283 } else {
1284 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
1285 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
1286 }
1287
1288 dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp;
1289 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
1290 dst_buf->v4l2_buf.flags |=
1291 src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
1292 dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode;
1293
1294 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1295
1296 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1297 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
1298
1299 ctx->gopcounter--;
1300 if (ctx->gopcounter < 0)
1301 ctx->gopcounter = ctx->params.gop_size - 1;
1302
1303 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1304 "job finished: encoding frame (%d) (%s)\n",
1305 dst_buf->v4l2_buf.sequence,
1306 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
1307 "KEYFRAME" : "PFRAME");
1308}
1309
1310static void coda_seq_end_work(struct work_struct *work)
1311{
1312 struct coda_ctx *ctx = container_of(work, struct coda_ctx, seq_end_work);
1313 struct coda_dev *dev = ctx->dev;
1314
1315 mutex_lock(&ctx->buffer_mutex);
1316 mutex_lock(&dev->coda_mutex);
1317
1318 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Philipp Zabelf23797b2014-08-06 08:02:23 -03001319 "%d: %s: sent command 'SEQ_END' to coda\n", ctx->idx,
1320 __func__);
Philipp Zabel79924ca2014-07-23 12:28:45 -03001321 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
1322 v4l2_err(&dev->v4l2_dev,
1323 "CODA_COMMAND_SEQ_END failed\n");
1324 }
1325
1326 kfifo_init(&ctx->bitstream_fifo,
1327 ctx->bitstream.vaddr, ctx->bitstream.size);
1328
1329 coda_free_framebuffers(ctx);
Philipp Zabel79924ca2014-07-23 12:28:45 -03001330
1331 mutex_unlock(&dev->coda_mutex);
1332 mutex_unlock(&ctx->buffer_mutex);
1333}
1334
1335static void coda_bit_release(struct coda_ctx *ctx)
1336{
Philipp Zabel540d70d2015-01-23 13:51:32 -03001337 mutex_lock(&ctx->buffer_mutex);
Philipp Zabel79924ca2014-07-23 12:28:45 -03001338 coda_free_framebuffers(ctx);
1339 coda_free_context_buffers(ctx);
Philipp Zabel540d70d2015-01-23 13:51:32 -03001340 mutex_unlock(&ctx->buffer_mutex);
Philipp Zabel79924ca2014-07-23 12:28:45 -03001341}
1342
1343const struct coda_context_ops coda_bit_encode_ops = {
1344 .queue_init = coda_encoder_queue_init,
Philipp Zabel73751da2015-03-24 14:30:51 -03001345 .reqbufs = coda_encoder_reqbufs,
Philipp Zabel79924ca2014-07-23 12:28:45 -03001346 .start_streaming = coda_start_encoding,
1347 .prepare_run = coda_prepare_encode,
1348 .finish_run = coda_finish_encode,
1349 .seq_end_work = coda_seq_end_work,
1350 .release = coda_bit_release,
1351};
1352
1353/*
1354 * Decoder context operations
1355 */
1356
Philipp Zabel73751da2015-03-24 14:30:51 -03001357static int coda_decoder_reqbufs(struct coda_ctx *ctx,
1358 struct v4l2_requestbuffers *rb)
1359{
1360 struct coda_q_data *q_data_src;
1361 int ret;
1362
1363 if (rb->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1364 return 0;
1365
1366 if (rb->count) {
1367 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1368 ret = coda_alloc_context_buffers(ctx, q_data_src);
1369 if (ret < 0)
1370 return ret;
1371 } else {
1372 coda_free_context_buffers(ctx);
1373 }
1374
1375 return 0;
1376}
1377
Philipp Zabel79924ca2014-07-23 12:28:45 -03001378static int __coda_start_decoding(struct coda_ctx *ctx)
1379{
1380 struct coda_q_data *q_data_src, *q_data_dst;
1381 u32 bitstream_buf, bitstream_size;
1382 struct coda_dev *dev = ctx->dev;
1383 int width, height;
Philipp Zabel1cb12cf2014-09-29 09:53:47 -03001384 u32 src_fourcc, dst_fourcc;
Philipp Zabel79924ca2014-07-23 12:28:45 -03001385 u32 val;
1386 int ret;
1387
1388 /* Start decoding */
1389 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1390 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1391 bitstream_buf = ctx->bitstream.paddr;
1392 bitstream_size = ctx->bitstream.size;
1393 src_fourcc = q_data_src->fourcc;
Philipp Zabel1cb12cf2014-09-29 09:53:47 -03001394 dst_fourcc = q_data_dst->fourcc;
Philipp Zabel79924ca2014-07-23 12:28:45 -03001395
Philipp Zabel79924ca2014-07-23 12:28:45 -03001396 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
1397
1398 /* Update coda bitstream read and write pointers from kfifo */
1399 coda_kfifo_sync_to_device_full(ctx);
1400
Philipp Zabel1cb12cf2014-09-29 09:53:47 -03001401 ctx->frame_mem_ctrl &= ~CODA_FRAME_CHROMA_INTERLEAVE;
1402 if (dst_fourcc == V4L2_PIX_FMT_NV12)
1403 ctx->frame_mem_ctrl |= CODA_FRAME_CHROMA_INTERLEAVE;
Philipp Zabel2bf299c2014-09-29 09:53:46 -03001404 coda_write(dev, ctx->frame_mem_ctrl, CODA_REG_BIT_FRAME_MEM_CTRL);
1405
Philipp Zabel79924ca2014-07-23 12:28:45 -03001406 ctx->display_idx = -1;
1407 ctx->frm_dis_flg = 0;
1408 coda_write(dev, 0, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
1409
1410 coda_write(dev, CODA_BIT_DEC_SEQ_INIT_ESCAPE,
1411 CODA_REG_BIT_BIT_STREAM_PARAM);
1412
1413 coda_write(dev, bitstream_buf, CODA_CMD_DEC_SEQ_BB_START);
1414 coda_write(dev, bitstream_size / 1024, CODA_CMD_DEC_SEQ_BB_SIZE);
1415 val = 0;
1416 if ((dev->devtype->product == CODA_7541) ||
1417 (dev->devtype->product == CODA_960))
1418 val |= CODA_REORDER_ENABLE;
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001419 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_JPEG)
1420 val |= CODA_NO_INT_ENABLE;
Philipp Zabel79924ca2014-07-23 12:28:45 -03001421 coda_write(dev, val, CODA_CMD_DEC_SEQ_OPTION);
1422
1423 ctx->params.codec_mode = ctx->codec->mode;
1424 if (dev->devtype->product == CODA_960 &&
1425 src_fourcc == V4L2_PIX_FMT_MPEG4)
1426 ctx->params.codec_mode_aux = CODA_MP4_AUX_MPEG4;
1427 else
1428 ctx->params.codec_mode_aux = 0;
1429 if (src_fourcc == V4L2_PIX_FMT_H264) {
1430 if (dev->devtype->product == CODA_7541) {
1431 coda_write(dev, ctx->psbuf.paddr,
1432 CODA_CMD_DEC_SEQ_PS_BB_START);
1433 coda_write(dev, (CODA7_PS_BUF_SIZE / 1024),
1434 CODA_CMD_DEC_SEQ_PS_BB_SIZE);
1435 }
1436 if (dev->devtype->product == CODA_960) {
1437 coda_write(dev, 0, CODA_CMD_DEC_SEQ_X264_MV_EN);
1438 coda_write(dev, 512, CODA_CMD_DEC_SEQ_SPP_CHUNK_SIZE);
1439 }
1440 }
1441 if (dev->devtype->product != CODA_960)
1442 coda_write(dev, 0, CODA_CMD_DEC_SEQ_SRC_SIZE);
1443
1444 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT)) {
1445 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
1446 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
1447 return -ETIMEDOUT;
1448 }
1449
1450 /* Update kfifo out pointer from coda bitstream read pointer */
1451 coda_kfifo_sync_from_device(ctx);
1452
1453 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
1454
1455 if (coda_read(dev, CODA_RET_DEC_SEQ_SUCCESS) == 0) {
1456 v4l2_err(&dev->v4l2_dev,
1457 "CODA_COMMAND_SEQ_INIT failed, error code = %d\n",
1458 coda_read(dev, CODA_RET_DEC_SEQ_ERR_REASON));
1459 return -EAGAIN;
1460 }
1461
1462 val = coda_read(dev, CODA_RET_DEC_SEQ_SRC_SIZE);
1463 if (dev->devtype->product == CODA_DX6) {
1464 width = (val >> CODADX6_PICWIDTH_OFFSET) & CODADX6_PICWIDTH_MASK;
1465 height = val & CODADX6_PICHEIGHT_MASK;
1466 } else {
1467 width = (val >> CODA7_PICWIDTH_OFFSET) & CODA7_PICWIDTH_MASK;
1468 height = val & CODA7_PICHEIGHT_MASK;
1469 }
1470
Markus Pargmann31db5ad2015-01-23 13:51:25 -03001471 if (width > q_data_dst->bytesperline || height > q_data_dst->height) {
Philipp Zabel79924ca2014-07-23 12:28:45 -03001472 v4l2_err(&dev->v4l2_dev, "stream is %dx%d, not %dx%d\n",
Markus Pargmann31db5ad2015-01-23 13:51:25 -03001473 width, height, q_data_dst->bytesperline,
1474 q_data_dst->height);
Philipp Zabel79924ca2014-07-23 12:28:45 -03001475 return -EINVAL;
1476 }
1477
1478 width = round_up(width, 16);
1479 height = round_up(height, 16);
1480
1481 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "%s instance %d now: %dx%d\n",
1482 __func__, ctx->idx, width, height);
1483
1484 ctx->num_internal_frames = coda_read(dev, CODA_RET_DEC_SEQ_FRAME_NEED);
1485 if (ctx->num_internal_frames > CODA_MAX_FRAMEBUFFERS) {
1486 v4l2_err(&dev->v4l2_dev,
1487 "not enough framebuffers to decode (%d < %d)\n",
1488 CODA_MAX_FRAMEBUFFERS, ctx->num_internal_frames);
1489 return -EINVAL;
1490 }
1491
1492 if (src_fourcc == V4L2_PIX_FMT_H264) {
1493 u32 left_right;
1494 u32 top_bottom;
1495
1496 left_right = coda_read(dev, CODA_RET_DEC_SEQ_CROP_LEFT_RIGHT);
1497 top_bottom = coda_read(dev, CODA_RET_DEC_SEQ_CROP_TOP_BOTTOM);
1498
1499 q_data_dst->rect.left = (left_right >> 10) & 0x3ff;
1500 q_data_dst->rect.top = (top_bottom >> 10) & 0x3ff;
1501 q_data_dst->rect.width = width - q_data_dst->rect.left -
1502 (left_right & 0x3ff);
1503 q_data_dst->rect.height = height - q_data_dst->rect.top -
1504 (top_bottom & 0x3ff);
1505 }
1506
1507 ret = coda_alloc_framebuffers(ctx, q_data_dst, src_fourcc);
Philipp Zabel68fc31c2014-08-05 14:00:16 -03001508 if (ret < 0) {
1509 v4l2_err(&dev->v4l2_dev, "failed to allocate framebuffers\n");
Philipp Zabel79924ca2014-07-23 12:28:45 -03001510 return ret;
Philipp Zabel68fc31c2014-08-05 14:00:16 -03001511 }
Philipp Zabel79924ca2014-07-23 12:28:45 -03001512
1513 /* Tell the decoder how many frame buffers we allocated. */
1514 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
1515 coda_write(dev, width, CODA_CMD_SET_FRAME_BUF_STRIDE);
1516
1517 if (dev->devtype->product != CODA_DX6) {
1518 /* Set secondary AXI IRAM */
1519 coda_setup_iram(ctx);
1520
1521 coda_write(dev, ctx->iram_info.buf_bit_use,
1522 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
1523 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
1524 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
1525 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
1526 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
1527 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
1528 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
1529 coda_write(dev, ctx->iram_info.buf_ovl_use,
1530 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
1531 if (dev->devtype->product == CODA_960)
1532 coda_write(dev, ctx->iram_info.buf_btp_use,
1533 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
1534 }
1535
1536 if (dev->devtype->product == CODA_960) {
Philipp Zabel1cb12cf2014-09-29 09:53:47 -03001537 int cbb_size, crb_size;
Philipp Zabel79924ca2014-07-23 12:28:45 -03001538
Philipp Zabel1cb12cf2014-09-29 09:53:47 -03001539 coda_write(dev, -1, CODA9_CMD_SET_FRAME_DELAY);
1540 /* Luma 2x0 page, 2x6 cache, chroma 2x0 page, 2x4 cache size */
Philipp Zabel79924ca2014-07-23 12:28:45 -03001541 coda_write(dev, 0x20262024, CODA9_CMD_SET_FRAME_CACHE_SIZE);
Philipp Zabel1cb12cf2014-09-29 09:53:47 -03001542
1543 if (dst_fourcc == V4L2_PIX_FMT_NV12) {
1544 cbb_size = 0;
1545 crb_size = 16;
1546 } else {
1547 cbb_size = 8;
1548 crb_size = 8;
1549 }
Philipp Zabel79924ca2014-07-23 12:28:45 -03001550 coda_write(dev, 2 << CODA9_CACHE_PAGEMERGE_OFFSET |
1551 32 << CODA9_CACHE_LUMA_BUFFER_SIZE_OFFSET |
Philipp Zabel1cb12cf2014-09-29 09:53:47 -03001552 cbb_size << CODA9_CACHE_CB_BUFFER_SIZE_OFFSET |
1553 crb_size << CODA9_CACHE_CR_BUFFER_SIZE_OFFSET,
Philipp Zabel79924ca2014-07-23 12:28:45 -03001554 CODA9_CMD_SET_FRAME_CACHE_CONFIG);
1555 }
1556
1557 if (src_fourcc == V4L2_PIX_FMT_H264) {
1558 coda_write(dev, ctx->slicebuf.paddr,
1559 CODA_CMD_SET_FRAME_SLICE_BB_START);
1560 coda_write(dev, ctx->slicebuf.size / 1024,
1561 CODA_CMD_SET_FRAME_SLICE_BB_SIZE);
1562 }
1563
1564 if (dev->devtype->product == CODA_7541) {
1565 int max_mb_x = 1920 / 16;
1566 int max_mb_y = 1088 / 16;
1567 int max_mb_num = max_mb_x * max_mb_y;
1568
1569 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
1570 CODA7_CMD_SET_FRAME_MAX_DEC_SIZE);
1571 } else if (dev->devtype->product == CODA_960) {
1572 int max_mb_x = 1920 / 16;
1573 int max_mb_y = 1088 / 16;
1574 int max_mb_num = max_mb_x * max_mb_y;
1575
1576 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
1577 CODA9_CMD_SET_FRAME_MAX_DEC_SIZE);
1578 }
1579
1580 if (coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF)) {
1581 v4l2_err(&ctx->dev->v4l2_dev,
1582 "CODA_COMMAND_SET_FRAME_BUF timeout\n");
1583 return -ETIMEDOUT;
1584 }
1585
1586 return 0;
1587}
1588
1589static int coda_start_decoding(struct coda_ctx *ctx)
1590{
1591 struct coda_dev *dev = ctx->dev;
1592 int ret;
1593
1594 mutex_lock(&dev->coda_mutex);
1595 ret = __coda_start_decoding(ctx);
1596 mutex_unlock(&dev->coda_mutex);
1597
1598 return ret;
1599}
1600
1601static int coda_prepare_decode(struct coda_ctx *ctx)
1602{
1603 struct vb2_buffer *dst_buf;
1604 struct coda_dev *dev = ctx->dev;
1605 struct coda_q_data *q_data_dst;
Philipp Zabel07ba2772015-01-23 13:51:18 -03001606 struct coda_buffer_meta *meta;
Philipp Zabel856d7d92014-09-29 09:53:44 -03001607 u32 reg_addr, reg_stride;
Philipp Zabel79924ca2014-07-23 12:28:45 -03001608
1609 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
1610 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1611
Philipp Zabel79924ca2014-07-23 12:28:45 -03001612 /* Try to copy source buffer contents into the bitstream ringbuffer */
1613 mutex_lock(&ctx->bitstream_mutex);
1614 coda_fill_bitstream(ctx);
1615 mutex_unlock(&ctx->bitstream_mutex);
1616
1617 if (coda_get_bitstream_payload(ctx) < 512 &&
1618 (!(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1619 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1620 "bitstream payload: %d, skipping\n",
1621 coda_get_bitstream_payload(ctx));
1622 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
1623 return -EAGAIN;
1624 }
1625
1626 /* Run coda_start_decoding (again) if not yet initialized */
1627 if (!ctx->initialized) {
1628 int ret = __coda_start_decoding(ctx);
1629
1630 if (ret < 0) {
1631 v4l2_err(&dev->v4l2_dev, "failed to start decoding\n");
1632 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
1633 return -EAGAIN;
1634 } else {
1635 ctx->initialized = 1;
1636 }
1637 }
1638
1639 if (dev->devtype->product == CODA_960)
1640 coda_set_gdi_regs(ctx);
1641
Philipp Zabel79924ca2014-07-23 12:28:45 -03001642 if (dev->devtype->product == CODA_960) {
1643 /*
1644 * The CODA960 seems to have an internal list of buffers with
1645 * 64 entries that includes the registered frame buffers as
1646 * well as the rotator buffer output.
1647 * ROT_INDEX needs to be < 0x40, but > ctx->num_internal_frames.
1648 */
1649 coda_write(dev, CODA_MAX_FRAMEBUFFERS + dst_buf->v4l2_buf.index,
1650 CODA9_CMD_DEC_PIC_ROT_INDEX);
Philipp Zabel856d7d92014-09-29 09:53:44 -03001651
1652 reg_addr = CODA9_CMD_DEC_PIC_ROT_ADDR_Y;
1653 reg_stride = CODA9_CMD_DEC_PIC_ROT_STRIDE;
Philipp Zabel79924ca2014-07-23 12:28:45 -03001654 } else {
Philipp Zabel856d7d92014-09-29 09:53:44 -03001655 reg_addr = CODA_CMD_DEC_PIC_ROT_ADDR_Y;
1656 reg_stride = CODA_CMD_DEC_PIC_ROT_STRIDE;
Philipp Zabel79924ca2014-07-23 12:28:45 -03001657 }
Philipp Zabel856d7d92014-09-29 09:53:44 -03001658 coda_write_base(ctx, q_data_dst, dst_buf, reg_addr);
1659 coda_write(dev, q_data_dst->bytesperline, reg_stride);
1660
Philipp Zabel79924ca2014-07-23 12:28:45 -03001661 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode,
1662 CODA_CMD_DEC_PIC_ROT_MODE);
1663
1664 switch (dev->devtype->product) {
1665 case CODA_DX6:
1666 /* TBD */
1667 case CODA_7541:
1668 coda_write(dev, CODA_PRE_SCAN_EN, CODA_CMD_DEC_PIC_OPTION);
1669 break;
1670 case CODA_960:
Philipp Zabelf23797b2014-08-06 08:02:23 -03001671 /* 'hardcode to use interrupt disable mode'? */
1672 coda_write(dev, (1 << 10), CODA_CMD_DEC_PIC_OPTION);
Philipp Zabel79924ca2014-07-23 12:28:45 -03001673 break;
1674 }
1675
1676 coda_write(dev, 0, CODA_CMD_DEC_PIC_SKIP_NUM);
1677
1678 coda_write(dev, 0, CODA_CMD_DEC_PIC_BB_START);
1679 coda_write(dev, 0, CODA_CMD_DEC_PIC_START_BYTE);
1680
1681 if (dev->devtype->product != CODA_DX6)
1682 coda_write(dev, ctx->iram_info.axi_sram_use,
1683 CODA7_REG_BIT_AXI_SRAM_USE);
1684
Philipp Zabel07ba2772015-01-23 13:51:18 -03001685 meta = list_first_entry_or_null(&ctx->buffer_meta_list,
1686 struct coda_buffer_meta, list);
1687
1688 if (meta && ctx->codec->src_fourcc == V4L2_PIX_FMT_JPEG) {
Philipp Zabel5269aed2014-10-02 14:08:33 -03001689
1690 /* If this is the last buffer in the bitstream, add padding */
Philipp Zabel5269aed2014-10-02 14:08:33 -03001691 if (meta->end == (ctx->bitstream_fifo.kfifo.in &
1692 ctx->bitstream_fifo.kfifo.mask)) {
1693 static unsigned char buf[512];
1694 unsigned int pad;
1695
1696 /* Pad to multiple of 256 and then add 256 more */
1697 pad = ((0 - meta->end) & 0xff) + 256;
1698
1699 memset(buf, 0xff, sizeof(buf));
1700
1701 kfifo_in(&ctx->bitstream_fifo, buf, pad);
1702 }
1703 }
1704
Philipp Zabel79924ca2014-07-23 12:28:45 -03001705 coda_kfifo_sync_to_device_full(ctx);
1706
Philipp Zabel8c9ef6b2015-01-23 13:51:24 -03001707 /* Clear decode success flag */
1708 coda_write(dev, 0, CODA_RET_DEC_PIC_SUCCESS);
1709
Philipp Zabel79924ca2014-07-23 12:28:45 -03001710 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
1711
1712 return 0;
1713}
1714
1715static void coda_finish_decode(struct coda_ctx *ctx)
1716{
1717 struct coda_dev *dev = ctx->dev;
1718 struct coda_q_data *q_data_src;
1719 struct coda_q_data *q_data_dst;
1720 struct vb2_buffer *dst_buf;
Philipp Zabel7cbb1052014-10-02 14:08:32 -03001721 struct coda_buffer_meta *meta;
Philipp Zabel4de69312014-10-02 14:08:26 -03001722 unsigned long payload;
Philipp Zabel79924ca2014-07-23 12:28:45 -03001723 int width, height;
1724 int decoded_idx;
1725 int display_idx;
1726 u32 src_fourcc;
1727 int success;
1728 u32 err_mb;
1729 u32 val;
1730
Philipp Zabel79924ca2014-07-23 12:28:45 -03001731 /* Update kfifo out pointer from coda bitstream read pointer */
1732 coda_kfifo_sync_from_device(ctx);
1733
1734 /*
1735 * in stream-end mode, the read pointer can overshoot the write pointer
1736 * by up to 512 bytes
1737 */
1738 if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) {
1739 if (coda_get_bitstream_payload(ctx) >= CODA_MAX_FRAME_SIZE - 512)
1740 kfifo_init(&ctx->bitstream_fifo,
1741 ctx->bitstream.vaddr, ctx->bitstream.size);
1742 }
1743
1744 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1745 src_fourcc = q_data_src->fourcc;
1746
1747 val = coda_read(dev, CODA_RET_DEC_PIC_SUCCESS);
1748 if (val != 1)
1749 pr_err("DEC_PIC_SUCCESS = %d\n", val);
1750
1751 success = val & 0x1;
1752 if (!success)
1753 v4l2_err(&dev->v4l2_dev, "decode failed\n");
1754
1755 if (src_fourcc == V4L2_PIX_FMT_H264) {
1756 if (val & (1 << 3))
1757 v4l2_err(&dev->v4l2_dev,
1758 "insufficient PS buffer space (%d bytes)\n",
1759 ctx->psbuf.size);
1760 if (val & (1 << 2))
1761 v4l2_err(&dev->v4l2_dev,
1762 "insufficient slice buffer space (%d bytes)\n",
1763 ctx->slicebuf.size);
1764 }
1765
1766 val = coda_read(dev, CODA_RET_DEC_PIC_SIZE);
1767 width = (val >> 16) & 0xffff;
1768 height = val & 0xffff;
1769
1770 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1771
1772 /* frame crop information */
1773 if (src_fourcc == V4L2_PIX_FMT_H264) {
1774 u32 left_right;
1775 u32 top_bottom;
1776
1777 left_right = coda_read(dev, CODA_RET_DEC_PIC_CROP_LEFT_RIGHT);
1778 top_bottom = coda_read(dev, CODA_RET_DEC_PIC_CROP_TOP_BOTTOM);
1779
1780 if (left_right == 0xffffffff && top_bottom == 0xffffffff) {
1781 /* Keep current crop information */
1782 } else {
1783 struct v4l2_rect *rect = &q_data_dst->rect;
1784
1785 rect->left = left_right >> 16 & 0xffff;
1786 rect->top = top_bottom >> 16 & 0xffff;
1787 rect->width = width - rect->left -
1788 (left_right & 0xffff);
1789 rect->height = height - rect->top -
1790 (top_bottom & 0xffff);
1791 }
1792 } else {
1793 /* no cropping */
1794 }
1795
1796 err_mb = coda_read(dev, CODA_RET_DEC_PIC_ERR_MB);
1797 if (err_mb > 0)
1798 v4l2_err(&dev->v4l2_dev,
1799 "errors in %d macroblocks\n", err_mb);
1800
1801 if (dev->devtype->product == CODA_7541) {
1802 val = coda_read(dev, CODA_RET_DEC_PIC_OPTION);
1803 if (val == 0) {
1804 /* not enough bitstream data */
1805 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1806 "prescan failed: %d\n", val);
1807 ctx->hold = true;
1808 return;
1809 }
1810 }
1811
Philipp Zabelf23797b2014-08-06 08:02:23 -03001812 ctx->frm_dis_flg = coda_read(dev,
1813 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
Philipp Zabel79924ca2014-07-23 12:28:45 -03001814
1815 /*
1816 * The previous display frame was copied out by the rotator,
1817 * now it can be overwritten again
1818 */
1819 if (ctx->display_idx >= 0 &&
1820 ctx->display_idx < ctx->num_internal_frames) {
1821 ctx->frm_dis_flg &= ~(1 << ctx->display_idx);
1822 coda_write(dev, ctx->frm_dis_flg,
1823 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
1824 }
1825
1826 /*
1827 * The index of the last decoded frame, not necessarily in
1828 * display order, and the index of the next display frame.
1829 * The latter could have been decoded in a previous run.
1830 */
1831 decoded_idx = coda_read(dev, CODA_RET_DEC_PIC_CUR_IDX);
1832 display_idx = coda_read(dev, CODA_RET_DEC_PIC_FRAME_IDX);
1833
1834 if (decoded_idx == -1) {
1835 /* no frame was decoded, but we might have a display frame */
1836 if (display_idx >= 0 && display_idx < ctx->num_internal_frames)
1837 ctx->sequence_offset++;
1838 else if (ctx->display_idx < 0)
1839 ctx->hold = true;
1840 } else if (decoded_idx == -2) {
Philipp Zabelf23797b2014-08-06 08:02:23 -03001841 /* no frame was decoded, we still return remaining buffers */
Philipp Zabel79924ca2014-07-23 12:28:45 -03001842 } else if (decoded_idx < 0 || decoded_idx >= ctx->num_internal_frames) {
1843 v4l2_err(&dev->v4l2_dev,
1844 "decoded frame index out of range: %d\n", decoded_idx);
1845 } else {
Philipp Zabel79924ca2014-07-23 12:28:45 -03001846 val = coda_read(dev, CODA_RET_DEC_PIC_FRAME_NUM) - 1;
1847 val -= ctx->sequence_offset;
Philipp Zabel18fd0cc2014-08-05 14:00:17 -03001848 mutex_lock(&ctx->bitstream_mutex);
Philipp Zabel7cbb1052014-10-02 14:08:32 -03001849 if (!list_empty(&ctx->buffer_meta_list)) {
1850 meta = list_first_entry(&ctx->buffer_meta_list,
1851 struct coda_buffer_meta, list);
1852 list_del(&meta->list);
1853 if (val != (meta->sequence & 0xffff)) {
Philipp Zabel18fd0cc2014-08-05 14:00:17 -03001854 v4l2_err(&dev->v4l2_dev,
1855 "sequence number mismatch (%d(%d) != %d)\n",
1856 val, ctx->sequence_offset,
Philipp Zabel7cbb1052014-10-02 14:08:32 -03001857 meta->sequence);
Philipp Zabel18fd0cc2014-08-05 14:00:17 -03001858 }
Philipp Zabel7cbb1052014-10-02 14:08:32 -03001859 ctx->frame_metas[decoded_idx] = *meta;
1860 kfree(meta);
Philipp Zabel18fd0cc2014-08-05 14:00:17 -03001861 } else {
1862 v4l2_err(&dev->v4l2_dev, "empty timestamp list!\n");
Philipp Zabel7cbb1052014-10-02 14:08:32 -03001863 memset(&ctx->frame_metas[decoded_idx], 0,
1864 sizeof(struct coda_buffer_meta));
1865 ctx->frame_metas[decoded_idx].sequence = val;
Lucas Stach83f31c82015-01-23 13:51:19 -03001866 ctx->sequence_offset++;
Philipp Zabel79924ca2014-07-23 12:28:45 -03001867 }
Philipp Zabel18fd0cc2014-08-05 14:00:17 -03001868 mutex_unlock(&ctx->bitstream_mutex);
Philipp Zabel79924ca2014-07-23 12:28:45 -03001869
1870 val = coda_read(dev, CODA_RET_DEC_PIC_TYPE) & 0x7;
1871 if (val == 0)
1872 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_KEYFRAME;
1873 else if (val == 1)
1874 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_PFRAME;
1875 else
1876 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_BFRAME;
1877
1878 ctx->frame_errors[decoded_idx] = err_mb;
1879 }
1880
1881 if (display_idx == -1) {
1882 /*
1883 * no more frames to be decoded, but there could still
1884 * be rotator output to dequeue
1885 */
1886 ctx->hold = true;
1887 } else if (display_idx == -3) {
1888 /* possibly prescan failure */
1889 } else if (display_idx < 0 || display_idx >= ctx->num_internal_frames) {
1890 v4l2_err(&dev->v4l2_dev,
1891 "presentation frame index out of range: %d\n",
1892 display_idx);
1893 }
1894
1895 /* If a frame was copied out, return it */
1896 if (ctx->display_idx >= 0 &&
1897 ctx->display_idx < ctx->num_internal_frames) {
1898 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1899 dst_buf->v4l2_buf.sequence = ctx->osequence++;
1900
1901 dst_buf->v4l2_buf.flags &= ~(V4L2_BUF_FLAG_KEYFRAME |
1902 V4L2_BUF_FLAG_PFRAME |
1903 V4L2_BUF_FLAG_BFRAME);
1904 dst_buf->v4l2_buf.flags |= ctx->frame_types[ctx->display_idx];
Philipp Zabel7cbb1052014-10-02 14:08:32 -03001905 meta = &ctx->frame_metas[ctx->display_idx];
1906 dst_buf->v4l2_buf.timecode = meta->timecode;
1907 dst_buf->v4l2_buf.timestamp = meta->timestamp;
Philipp Zabel79924ca2014-07-23 12:28:45 -03001908
Philipp Zabel4de69312014-10-02 14:08:26 -03001909 switch (q_data_dst->fourcc) {
1910 case V4L2_PIX_FMT_YUV420:
1911 case V4L2_PIX_FMT_YVU420:
1912 case V4L2_PIX_FMT_NV12:
1913 default:
1914 payload = width * height * 3 / 2;
1915 break;
1916 case V4L2_PIX_FMT_YUV422P:
1917 payload = width * height * 2;
1918 break;
1919 }
1920 vb2_set_plane_payload(dst_buf, 0, payload);
Philipp Zabel79924ca2014-07-23 12:28:45 -03001921
1922 v4l2_m2m_buf_done(dst_buf, ctx->frame_errors[display_idx] ?
1923 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
1924
1925 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1926 "job finished: decoding frame (%d) (%s)\n",
1927 dst_buf->v4l2_buf.sequence,
1928 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
1929 "KEYFRAME" : "PFRAME");
1930 } else {
1931 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1932 "job finished: no frame decoded\n");
1933 }
1934
1935 /* The rotator will copy the current display frame next time */
1936 ctx->display_idx = display_idx;
1937}
1938
1939const struct coda_context_ops coda_bit_decode_ops = {
1940 .queue_init = coda_decoder_queue_init,
Philipp Zabel73751da2015-03-24 14:30:51 -03001941 .reqbufs = coda_decoder_reqbufs,
Philipp Zabel79924ca2014-07-23 12:28:45 -03001942 .start_streaming = coda_start_decoding,
1943 .prepare_run = coda_prepare_decode,
1944 .finish_run = coda_finish_decode,
1945 .seq_end_work = coda_seq_end_work,
1946 .release = coda_bit_release,
1947};
1948
1949irqreturn_t coda_irq_handler(int irq, void *data)
1950{
1951 struct coda_dev *dev = data;
1952 struct coda_ctx *ctx;
1953
1954 /* read status register to attend the IRQ */
1955 coda_read(dev, CODA_REG_BIT_INT_STATUS);
1956 coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET,
1957 CODA_REG_BIT_INT_CLEAR);
1958
1959 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
1960 if (ctx == NULL) {
Philipp Zabelf23797b2014-08-06 08:02:23 -03001961 v4l2_err(&dev->v4l2_dev,
1962 "Instance released before the end of transaction\n");
Philipp Zabel79924ca2014-07-23 12:28:45 -03001963 mutex_unlock(&dev->coda_mutex);
1964 return IRQ_HANDLED;
1965 }
1966
1967 if (ctx->aborting) {
1968 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1969 "task has been aborted\n");
1970 }
1971
1972 if (coda_isbusy(ctx->dev)) {
1973 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1974 "coda is still busy!!!!\n");
1975 return IRQ_NONE;
1976 }
1977
1978 complete(&ctx->completion);
1979
1980 return IRQ_HANDLED;
1981}