blob: 931248dc60f916690619a8b13bcd7ae71b61d335 [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;
220 struct coda_timestamp *ts;
221
222 while (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) > 0) {
223 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
224
225 if (coda_bitstream_try_queue(ctx, src_buf)) {
226 /*
227 * Source buffer is queued in the bitstream ringbuffer;
228 * queue the timestamp and mark source buffer as done
229 */
230 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
231
232 ts = kmalloc(sizeof(*ts), GFP_KERNEL);
233 if (ts) {
234 ts->sequence = src_buf->v4l2_buf.sequence;
235 ts->timecode = src_buf->v4l2_buf.timecode;
236 ts->timestamp = src_buf->v4l2_buf.timestamp;
237 list_add_tail(&ts->list, &ctx->timestamp_list);
238 }
239
240 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
241 } else {
242 break;
243 }
244 }
245}
246
247void coda_bit_stream_end_flag(struct coda_ctx *ctx)
248{
249 struct coda_dev *dev = ctx->dev;
250
251 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
252
Philipp Zabelf23797b2014-08-06 08:02:23 -0300253 /* If this context is currently running, update the hardware flag */
Philipp Zabel79924ca2014-07-23 12:28:45 -0300254 if ((dev->devtype->product == CODA_960) &&
255 coda_isbusy(dev) &&
256 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
Philipp Zabelf23797b2014-08-06 08:02:23 -0300257 coda_write(dev, ctx->bit_stream_param,
258 CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel79924ca2014-07-23 12:28:45 -0300259 }
260}
261
262static void coda_parabuf_write(struct coda_ctx *ctx, int index, u32 value)
263{
264 struct coda_dev *dev = ctx->dev;
265 u32 *p = ctx->parabuf.vaddr;
266
267 if (dev->devtype->product == CODA_DX6)
268 p[index] = value;
269 else
270 p[index ^ 1] = value;
271}
272
273static void coda_free_framebuffers(struct coda_ctx *ctx)
274{
275 int i;
276
277 for (i = 0; i < CODA_MAX_FRAMEBUFFERS; i++)
278 coda_free_aux_buf(ctx->dev, &ctx->internal_frames[i]);
279}
280
281static int coda_alloc_framebuffers(struct coda_ctx *ctx,
282 struct coda_q_data *q_data, u32 fourcc)
283{
284 struct coda_dev *dev = ctx->dev;
285 int width, height;
286 dma_addr_t paddr;
287 int ysize;
288 int ret;
289 int i;
290
291 if (ctx->codec && (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 ||
292 ctx->codec->dst_fourcc == V4L2_PIX_FMT_H264)) {
293 width = round_up(q_data->width, 16);
294 height = round_up(q_data->height, 16);
295 } else {
296 width = round_up(q_data->width, 8);
297 height = q_data->height;
298 }
299 ysize = width * height;
300
301 /* Allocate frame buffers */
302 for (i = 0; i < ctx->num_internal_frames; i++) {
303 size_t size;
304 char *name;
305
306 size = ysize + ysize / 2;
307 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
308 dev->devtype->product != CODA_DX6)
309 size += ysize / 4;
310 name = kasprintf(GFP_KERNEL, "fb%d", i);
311 ret = coda_alloc_context_buf(ctx, &ctx->internal_frames[i],
312 size, name);
313 kfree(name);
314 if (ret < 0) {
315 coda_free_framebuffers(ctx);
316 return ret;
317 }
318 }
319
320 /* Register frame buffers in the parameter buffer */
321 for (i = 0; i < ctx->num_internal_frames; i++) {
322 paddr = ctx->internal_frames[i].paddr;
Philipp Zabelf23797b2014-08-06 08:02:23 -0300323 /* Start addresses of Y, Cb, Cr planes */
324 coda_parabuf_write(ctx, i * 3 + 0, paddr);
325 coda_parabuf_write(ctx, i * 3 + 1, paddr + ysize);
326 coda_parabuf_write(ctx, i * 3 + 2, paddr + ysize + ysize / 4);
Philipp Zabel79924ca2014-07-23 12:28:45 -0300327
328 /* mvcol buffer for h.264 */
329 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
330 dev->devtype->product != CODA_DX6)
331 coda_parabuf_write(ctx, 96 + i,
332 ctx->internal_frames[i].paddr +
333 ysize + ysize/4 + ysize/4);
334 }
335
336 /* mvcol buffer for mpeg4 */
337 if ((dev->devtype->product != CODA_DX6) &&
338 (ctx->codec->src_fourcc == V4L2_PIX_FMT_MPEG4))
339 coda_parabuf_write(ctx, 97, ctx->internal_frames[i].paddr +
340 ysize + ysize/4 + ysize/4);
341
342 return 0;
343}
344
345static void coda_free_context_buffers(struct coda_ctx *ctx)
346{
347 struct coda_dev *dev = ctx->dev;
348
349 coda_free_aux_buf(dev, &ctx->slicebuf);
350 coda_free_aux_buf(dev, &ctx->psbuf);
351 if (dev->devtype->product != CODA_DX6)
352 coda_free_aux_buf(dev, &ctx->workbuf);
353}
354
355static int coda_alloc_context_buffers(struct coda_ctx *ctx,
356 struct coda_q_data *q_data)
357{
358 struct coda_dev *dev = ctx->dev;
359 size_t size;
360 int ret;
361
362 if (dev->devtype->product == CODA_DX6)
363 return 0;
364
365 if (ctx->psbuf.vaddr) {
366 v4l2_err(&dev->v4l2_dev, "psmembuf still allocated\n");
367 return -EBUSY;
368 }
369 if (ctx->slicebuf.vaddr) {
370 v4l2_err(&dev->v4l2_dev, "slicebuf still allocated\n");
371 return -EBUSY;
372 }
373 if (ctx->workbuf.vaddr) {
374 v4l2_err(&dev->v4l2_dev, "context buffer still allocated\n");
375 ret = -EBUSY;
376 return -ENOMEM;
377 }
378
379 if (q_data->fourcc == V4L2_PIX_FMT_H264) {
380 /* worst case slice size */
381 size = (DIV_ROUND_UP(q_data->width, 16) *
382 DIV_ROUND_UP(q_data->height, 16)) * 3200 / 8 + 512;
Philipp Zabelf23797b2014-08-06 08:02:23 -0300383 ret = coda_alloc_context_buf(ctx, &ctx->slicebuf, size,
384 "slicebuf");
Philipp Zabel79924ca2014-07-23 12:28:45 -0300385 if (ret < 0) {
Philipp Zabelf23797b2014-08-06 08:02:23 -0300386 v4l2_err(&dev->v4l2_dev,
387 "failed to allocate %d byte slice buffer",
Philipp Zabel79924ca2014-07-23 12:28:45 -0300388 ctx->slicebuf.size);
389 return ret;
390 }
391 }
392
393 if (dev->devtype->product == CODA_7541) {
Philipp Zabelf23797b2014-08-06 08:02:23 -0300394 ret = coda_alloc_context_buf(ctx, &ctx->psbuf,
395 CODA7_PS_BUF_SIZE, "psbuf");
Philipp Zabel79924ca2014-07-23 12:28:45 -0300396 if (ret < 0) {
Philipp Zabelf23797b2014-08-06 08:02:23 -0300397 v4l2_err(&dev->v4l2_dev,
398 "failed to allocate psmem buffer");
Philipp Zabel79924ca2014-07-23 12:28:45 -0300399 goto err;
400 }
401 }
402
403 size = dev->devtype->workbuf_size;
404 if (dev->devtype->product == CODA_960 &&
405 q_data->fourcc == V4L2_PIX_FMT_H264)
406 size += CODA9_PS_SAVE_SIZE;
407 ret = coda_alloc_context_buf(ctx, &ctx->workbuf, size, "workbuf");
408 if (ret < 0) {
Philipp Zabelf23797b2014-08-06 08:02:23 -0300409 v4l2_err(&dev->v4l2_dev,
410 "failed to allocate %d byte context buffer",
Philipp Zabel79924ca2014-07-23 12:28:45 -0300411 ctx->workbuf.size);
412 goto err;
413 }
414
415 return 0;
416
417err:
418 coda_free_context_buffers(ctx);
419 return ret;
420}
421
422static int coda_encode_header(struct coda_ctx *ctx, struct vb2_buffer *buf,
423 int header_code, u8 *header, int *size)
424{
425 struct coda_dev *dev = ctx->dev;
426 size_t bufsize;
427 int ret;
428 int i;
429
430 if (dev->devtype->product == CODA_960)
431 memset(vb2_plane_vaddr(buf, 0), 0, 64);
432
433 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0),
434 CODA_CMD_ENC_HEADER_BB_START);
435 bufsize = vb2_plane_size(buf, 0);
436 if (dev->devtype->product == CODA_960)
437 bufsize /= 1024;
438 coda_write(dev, bufsize, CODA_CMD_ENC_HEADER_BB_SIZE);
439 coda_write(dev, header_code, CODA_CMD_ENC_HEADER_CODE);
440 ret = coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER);
441 if (ret < 0) {
442 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
443 return ret;
444 }
445
446 if (dev->devtype->product == CODA_960) {
447 for (i = 63; i > 0; i--)
448 if (((char *)vb2_plane_vaddr(buf, 0))[i] != 0)
449 break;
450 *size = i + 1;
451 } else {
452 *size = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx)) -
453 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
454 }
455 memcpy(header, vb2_plane_vaddr(buf, 0), *size);
456
457 return 0;
458}
459
460static phys_addr_t coda_iram_alloc(struct coda_iram_info *iram, size_t size)
461{
462 phys_addr_t ret;
463
464 size = round_up(size, 1024);
465 if (size > iram->remaining)
466 return 0;
467 iram->remaining -= size;
468
469 ret = iram->next_paddr;
470 iram->next_paddr += size;
471
472 return ret;
473}
474
475static void coda_setup_iram(struct coda_ctx *ctx)
476{
477 struct coda_iram_info *iram_info = &ctx->iram_info;
478 struct coda_dev *dev = ctx->dev;
Philipp Zabelf23797b2014-08-06 08:02:23 -0300479 int w64, w128;
Philipp Zabel79924ca2014-07-23 12:28:45 -0300480 int mb_width;
481 int dbk_bits;
482 int bit_bits;
483 int ip_bits;
484
485 memset(iram_info, 0, sizeof(*iram_info));
486 iram_info->next_paddr = dev->iram.paddr;
487 iram_info->remaining = dev->iram.size;
488
Philipp Zabel8be31c82014-08-05 14:00:13 -0300489 if (!dev->iram.vaddr)
490 return;
491
Philipp Zabel79924ca2014-07-23 12:28:45 -0300492 switch (dev->devtype->product) {
493 case CODA_7541:
494 dbk_bits = CODA7_USE_HOST_DBK_ENABLE | CODA7_USE_DBK_ENABLE;
495 bit_bits = CODA7_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
496 ip_bits = CODA7_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
497 break;
498 case CODA_960:
499 dbk_bits = CODA9_USE_HOST_DBK_ENABLE | CODA9_USE_DBK_ENABLE;
500 bit_bits = CODA9_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
501 ip_bits = CODA9_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
502 break;
503 default: /* CODA_DX6 */
504 return;
505 }
506
507 if (ctx->inst_type == CODA_INST_ENCODER) {
508 struct coda_q_data *q_data_src;
509
510 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
511 mb_width = DIV_ROUND_UP(q_data_src->width, 16);
Philipp Zabelf23797b2014-08-06 08:02:23 -0300512 w128 = mb_width * 128;
513 w64 = mb_width * 64;
Philipp Zabel79924ca2014-07-23 12:28:45 -0300514
515 /* Prioritize in case IRAM is too small for everything */
516 if (dev->devtype->product == CODA_7541) {
517 iram_info->search_ram_size = round_up(mb_width * 16 *
518 36 + 2048, 1024);
519 iram_info->search_ram_paddr = coda_iram_alloc(iram_info,
Philipp Zabelf23797b2014-08-06 08:02:23 -0300520 iram_info->search_ram_size);
Philipp Zabel79924ca2014-07-23 12:28:45 -0300521 if (!iram_info->search_ram_paddr) {
522 pr_err("IRAM is smaller than the search ram size\n");
523 goto out;
524 }
525 iram_info->axi_sram_use |= CODA7_USE_HOST_ME_ENABLE |
526 CODA7_USE_ME_ENABLE;
527 }
528
529 /* Only H.264BP and H.263P3 are considered */
Philipp Zabelf23797b2014-08-06 08:02:23 -0300530 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, w64);
531 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, w64);
Philipp Zabel79924ca2014-07-23 12:28:45 -0300532 if (!iram_info->buf_dbk_c_use)
533 goto out;
534 iram_info->axi_sram_use |= dbk_bits;
535
Philipp Zabelf23797b2014-08-06 08:02:23 -0300536 iram_info->buf_bit_use = coda_iram_alloc(iram_info, w128);
Philipp Zabel79924ca2014-07-23 12:28:45 -0300537 if (!iram_info->buf_bit_use)
538 goto out;
539 iram_info->axi_sram_use |= bit_bits;
540
Philipp Zabelf23797b2014-08-06 08:02:23 -0300541 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, w128);
Philipp Zabel79924ca2014-07-23 12:28:45 -0300542 if (!iram_info->buf_ip_ac_dc_use)
543 goto out;
544 iram_info->axi_sram_use |= ip_bits;
545
546 /* OVL and BTP disabled for encoder */
547 } else if (ctx->inst_type == CODA_INST_DECODER) {
548 struct coda_q_data *q_data_dst;
549
550 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
551 mb_width = DIV_ROUND_UP(q_data_dst->width, 16);
Philipp Zabelf23797b2014-08-06 08:02:23 -0300552 w128 = mb_width * 128;
Philipp Zabel79924ca2014-07-23 12:28:45 -0300553
Philipp Zabelf23797b2014-08-06 08:02:23 -0300554 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, w128);
555 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, w128);
Philipp Zabel79924ca2014-07-23 12:28:45 -0300556 if (!iram_info->buf_dbk_c_use)
557 goto out;
558 iram_info->axi_sram_use |= dbk_bits;
559
Philipp Zabelf23797b2014-08-06 08:02:23 -0300560 iram_info->buf_bit_use = coda_iram_alloc(iram_info, w128);
Philipp Zabel79924ca2014-07-23 12:28:45 -0300561 if (!iram_info->buf_bit_use)
562 goto out;
563 iram_info->axi_sram_use |= bit_bits;
564
Philipp Zabelf23797b2014-08-06 08:02:23 -0300565 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, w128);
Philipp Zabel79924ca2014-07-23 12:28:45 -0300566 if (!iram_info->buf_ip_ac_dc_use)
567 goto out;
568 iram_info->axi_sram_use |= ip_bits;
569
570 /* OVL and BTP unused as there is no VC1 support yet */
571 }
572
573out:
574 if (!(iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE))
575 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
576 "IRAM smaller than needed\n");
577
578 if (dev->devtype->product == CODA_7541) {
579 /* TODO - Enabling these causes picture errors on CODA7541 */
580 if (ctx->inst_type == CODA_INST_DECODER) {
581 /* fw 1.4.50 */
582 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
583 CODA7_USE_IP_ENABLE);
584 } else {
585 /* fw 13.4.29 */
586 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
587 CODA7_USE_HOST_DBK_ENABLE |
588 CODA7_USE_IP_ENABLE |
589 CODA7_USE_DBK_ENABLE);
590 }
591 }
592}
593
594static u32 coda_supported_firmwares[] = {
595 CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5),
596 CODA_FIRMWARE_VERNUM(CODA_7541, 1, 4, 50),
597 CODA_FIRMWARE_VERNUM(CODA_960, 2, 1, 5),
598};
599
600static bool coda_firmware_supported(u32 vernum)
601{
602 int i;
603
604 for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++)
605 if (vernum == coda_supported_firmwares[i])
606 return true;
607 return false;
608}
609
610int coda_check_firmware(struct coda_dev *dev)
611{
612 u16 product, major, minor, release;
613 u32 data;
614 int ret;
615
616 ret = clk_prepare_enable(dev->clk_per);
617 if (ret)
618 goto err_clk_per;
619
620 ret = clk_prepare_enable(dev->clk_ahb);
621 if (ret)
622 goto err_clk_ahb;
623
624 coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM);
625 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
626 coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX);
627 coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD);
628 coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND);
629 if (coda_wait_timeout(dev)) {
630 v4l2_err(&dev->v4l2_dev, "firmware get command error\n");
631 ret = -EIO;
632 goto err_run_cmd;
633 }
634
635 if (dev->devtype->product == CODA_960) {
636 data = coda_read(dev, CODA9_CMD_FIRMWARE_CODE_REV);
637 v4l2_info(&dev->v4l2_dev, "Firmware code revision: %d\n",
638 data);
639 }
640
641 /* Check we are compatible with the loaded firmware */
642 data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM);
643 product = CODA_FIRMWARE_PRODUCT(data);
644 major = CODA_FIRMWARE_MAJOR(data);
645 minor = CODA_FIRMWARE_MINOR(data);
646 release = CODA_FIRMWARE_RELEASE(data);
647
648 clk_disable_unprepare(dev->clk_per);
649 clk_disable_unprepare(dev->clk_ahb);
650
651 if (product != dev->devtype->product) {
Philipp Zabelf23797b2014-08-06 08:02:23 -0300652 v4l2_err(&dev->v4l2_dev,
653 "Wrong firmware. Hw: %s, Fw: %s, Version: %u.%u.%u\n",
Philipp Zabel79924ca2014-07-23 12:28:45 -0300654 coda_product_name(dev->devtype->product),
655 coda_product_name(product), major, minor, release);
656 return -EINVAL;
657 }
658
659 v4l2_info(&dev->v4l2_dev, "Initialized %s.\n",
660 coda_product_name(product));
661
662 if (coda_firmware_supported(data)) {
663 v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n",
664 major, minor, release);
665 } else {
Philipp Zabelf23797b2014-08-06 08:02:23 -0300666 v4l2_warn(&dev->v4l2_dev,
667 "Unsupported firmware version: %u.%u.%u\n",
668 major, minor, release);
Philipp Zabel79924ca2014-07-23 12:28:45 -0300669 }
670
671 return 0;
672
673err_run_cmd:
674 clk_disable_unprepare(dev->clk_ahb);
675err_clk_ahb:
676 clk_disable_unprepare(dev->clk_per);
677err_clk_per:
678 return ret;
679}
680
681/*
682 * Encoder context operations
683 */
684
685static int coda_start_encoding(struct coda_ctx *ctx)
686{
687 struct coda_dev *dev = ctx->dev;
688 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
689 struct coda_q_data *q_data_src, *q_data_dst;
690 u32 bitstream_buf, bitstream_size;
691 struct vb2_buffer *buf;
692 int gamma, ret, value;
693 u32 dst_fourcc;
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300694 u32 stride;
Philipp Zabel79924ca2014-07-23 12:28:45 -0300695
696 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
697 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
698 dst_fourcc = q_data_dst->fourcc;
699
700 /* Allocate per-instance buffers */
701 ret = coda_alloc_context_buffers(ctx, q_data_src);
702 if (ret < 0)
703 return ret;
704
705 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
706 bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0);
707 bitstream_size = q_data_dst->sizeimage;
708
709 if (!coda_is_initialized(dev)) {
710 v4l2_err(v4l2_dev, "coda is not initialized.\n");
711 return -EFAULT;
712 }
713
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300714 if (dst_fourcc == V4L2_PIX_FMT_JPEG) {
715 if (!ctx->params.jpeg_qmat_tab[0])
716 ctx->params.jpeg_qmat_tab[0] = kmalloc(64, GFP_KERNEL);
717 if (!ctx->params.jpeg_qmat_tab[1])
718 ctx->params.jpeg_qmat_tab[1] = kmalloc(64, GFP_KERNEL);
719 coda_set_jpeg_compression_quality(ctx, ctx->params.jpeg_quality);
720 }
721
Philipp Zabel79924ca2014-07-23 12:28:45 -0300722 mutex_lock(&dev->coda_mutex);
723
724 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
725 coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
726 coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
727 switch (dev->devtype->product) {
728 case CODA_DX6:
729 coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN |
730 CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
731 break;
732 case CODA_960:
733 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
734 /* fallthrough */
735 case CODA_7541:
736 coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN |
737 CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
738 break;
739 }
740
Philipp Zabel1cb12cf2014-09-29 09:53:47 -0300741 ctx->frame_mem_ctrl &= ~CODA_FRAME_CHROMA_INTERLEAVE;
742 if (q_data_src->fourcc == V4L2_PIX_FMT_NV12)
743 ctx->frame_mem_ctrl |= CODA_FRAME_CHROMA_INTERLEAVE;
Philipp Zabel2bf299c2014-09-29 09:53:46 -0300744 coda_write(dev, ctx->frame_mem_ctrl, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel79924ca2014-07-23 12:28:45 -0300745
746 if (dev->devtype->product == CODA_DX6) {
747 /* Configure the coda */
Philipp Zabelf23797b2014-08-06 08:02:23 -0300748 coda_write(dev, dev->iram.paddr,
749 CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR);
Philipp Zabel79924ca2014-07-23 12:28:45 -0300750 }
751
752 /* Could set rotation here if needed */
Philipp Zabel2bf299c2014-09-29 09:53:46 -0300753 value = 0;
Philipp Zabel79924ca2014-07-23 12:28:45 -0300754 switch (dev->devtype->product) {
755 case CODA_DX6:
Philipp Zabelf23797b2014-08-06 08:02:23 -0300756 value = (q_data_src->width & CODADX6_PICWIDTH_MASK)
757 << CODADX6_PICWIDTH_OFFSET;
758 value |= (q_data_src->height & CODADX6_PICHEIGHT_MASK)
759 << CODA_PICHEIGHT_OFFSET;
Philipp Zabel79924ca2014-07-23 12:28:45 -0300760 break;
761 case CODA_7541:
762 if (dst_fourcc == V4L2_PIX_FMT_H264) {
763 value = (round_up(q_data_src->width, 16) &
764 CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
765 value |= (round_up(q_data_src->height, 16) &
Philipp Zabelf23797b2014-08-06 08:02:23 -0300766 CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Philipp Zabel79924ca2014-07-23 12:28:45 -0300767 break;
768 }
769 /* fallthrough */
770 case CODA_960:
Philipp Zabelf23797b2014-08-06 08:02:23 -0300771 value = (q_data_src->width & CODA7_PICWIDTH_MASK)
772 << CODA7_PICWIDTH_OFFSET;
773 value |= (q_data_src->height & CODA7_PICHEIGHT_MASK)
774 << CODA_PICHEIGHT_OFFSET;
Philipp Zabel79924ca2014-07-23 12:28:45 -0300775 }
776 coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE);
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300777 if (dst_fourcc == V4L2_PIX_FMT_JPEG)
778 ctx->params.framerate = 0;
Philipp Zabel79924ca2014-07-23 12:28:45 -0300779 coda_write(dev, ctx->params.framerate,
780 CODA_CMD_ENC_SEQ_SRC_F_RATE);
781
782 ctx->params.codec_mode = ctx->codec->mode;
783 switch (dst_fourcc) {
784 case V4L2_PIX_FMT_MPEG4:
785 if (dev->devtype->product == CODA_960)
Philipp Zabelf23797b2014-08-06 08:02:23 -0300786 coda_write(dev, CODA9_STD_MPEG4,
787 CODA_CMD_ENC_SEQ_COD_STD);
Philipp Zabel79924ca2014-07-23 12:28:45 -0300788 else
Philipp Zabelf23797b2014-08-06 08:02:23 -0300789 coda_write(dev, CODA_STD_MPEG4,
790 CODA_CMD_ENC_SEQ_COD_STD);
Philipp Zabel79924ca2014-07-23 12:28:45 -0300791 coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA);
792 break;
793 case V4L2_PIX_FMT_H264:
794 if (dev->devtype->product == CODA_960)
Philipp Zabelf23797b2014-08-06 08:02:23 -0300795 coda_write(dev, CODA9_STD_H264,
796 CODA_CMD_ENC_SEQ_COD_STD);
Philipp Zabel79924ca2014-07-23 12:28:45 -0300797 else
Philipp Zabelf23797b2014-08-06 08:02:23 -0300798 coda_write(dev, CODA_STD_H264,
799 CODA_CMD_ENC_SEQ_COD_STD);
Philipp Zabel79924ca2014-07-23 12:28:45 -0300800 if (ctx->params.h264_deblk_enabled) {
801 value = ((ctx->params.h264_deblk_alpha &
802 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_MASK) <<
803 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_OFFSET) |
804 ((ctx->params.h264_deblk_beta &
805 CODA_264PARAM_DEBLKFILTEROFFSETBETA_MASK) <<
806 CODA_264PARAM_DEBLKFILTEROFFSETBETA_OFFSET);
807 } else {
808 value = 1 << CODA_264PARAM_DISABLEDEBLK_OFFSET;
809 }
810 coda_write(dev, value, CODA_CMD_ENC_SEQ_264_PARA);
811 break;
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300812 case V4L2_PIX_FMT_JPEG:
813 coda_write(dev, 0, CODA_CMD_ENC_SEQ_JPG_PARA);
814 coda_write(dev, ctx->params.jpeg_restart_interval,
815 CODA_CMD_ENC_SEQ_JPG_RST_INTERVAL);
816 coda_write(dev, 0, CODA_CMD_ENC_SEQ_JPG_THUMB_EN);
817 coda_write(dev, 0, CODA_CMD_ENC_SEQ_JPG_THUMB_SIZE);
818 coda_write(dev, 0, CODA_CMD_ENC_SEQ_JPG_THUMB_OFFSET);
819
820 coda_jpeg_write_tables(ctx);
821 break;
Philipp Zabel79924ca2014-07-23 12:28:45 -0300822 default:
823 v4l2_err(v4l2_dev,
824 "dst format (0x%08x) invalid.\n", dst_fourcc);
825 ret = -EINVAL;
826 goto out;
827 }
828
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300829 /*
830 * slice mode and GOP size registers are used for thumb size/offset
831 * in JPEG mode
832 */
833 if (dst_fourcc != V4L2_PIX_FMT_JPEG) {
834 switch (ctx->params.slice_mode) {
835 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE:
836 value = 0;
837 break;
838 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB:
839 value = (ctx->params.slice_max_mb &
840 CODA_SLICING_SIZE_MASK)
841 << CODA_SLICING_SIZE_OFFSET;
842 value |= (1 & CODA_SLICING_UNIT_MASK)
843 << CODA_SLICING_UNIT_OFFSET;
844 value |= 1 & CODA_SLICING_MODE_MASK;
845 break;
846 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES:
847 value = (ctx->params.slice_max_bits &
848 CODA_SLICING_SIZE_MASK)
849 << CODA_SLICING_SIZE_OFFSET;
850 value |= (0 & CODA_SLICING_UNIT_MASK)
851 << CODA_SLICING_UNIT_OFFSET;
852 value |= 1 & CODA_SLICING_MODE_MASK;
853 break;
854 }
855 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
856 value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
857 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
Philipp Zabel79924ca2014-07-23 12:28:45 -0300858 }
Philipp Zabel79924ca2014-07-23 12:28:45 -0300859
860 if (ctx->params.bitrate) {
861 /* Rate control enabled */
Philipp Zabelf23797b2014-08-06 08:02:23 -0300862 value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK)
863 << CODA_RATECONTROL_BITRATE_OFFSET;
Philipp Zabel79924ca2014-07-23 12:28:45 -0300864 value |= 1 & CODA_RATECONTROL_ENABLE_MASK;
865 if (dev->devtype->product == CODA_960)
866 value |= BIT(31); /* disable autoskip */
867 } else {
868 value = 0;
869 }
870 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA);
871
872 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE);
873 coda_write(dev, ctx->params.intra_refresh,
874 CODA_CMD_ENC_SEQ_INTRA_REFRESH);
875
876 coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START);
877 coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE);
878
879
880 value = 0;
881 if (dev->devtype->product == CODA_960)
882 gamma = CODA9_DEFAULT_GAMMA;
883 else
884 gamma = CODA_DEFAULT_GAMMA;
885 if (gamma > 0) {
886 coda_write(dev, (gamma & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET,
887 CODA_CMD_ENC_SEQ_RC_GAMMA);
888 }
889
890 if (ctx->params.h264_min_qp || ctx->params.h264_max_qp) {
891 coda_write(dev,
892 ctx->params.h264_min_qp << CODA_QPMIN_OFFSET |
893 ctx->params.h264_max_qp << CODA_QPMAX_OFFSET,
894 CODA_CMD_ENC_SEQ_RC_QP_MIN_MAX);
895 }
896 if (dev->devtype->product == CODA_960) {
897 if (ctx->params.h264_max_qp)
898 value |= 1 << CODA9_OPTION_RCQPMAX_OFFSET;
899 if (CODA_DEFAULT_GAMMA > 0)
900 value |= 1 << CODA9_OPTION_GAMMA_OFFSET;
901 } else {
902 if (CODA_DEFAULT_GAMMA > 0) {
903 if (dev->devtype->product == CODA_DX6)
904 value |= 1 << CODADX6_OPTION_GAMMA_OFFSET;
905 else
906 value |= 1 << CODA7_OPTION_GAMMA_OFFSET;
907 }
908 if (ctx->params.h264_min_qp)
909 value |= 1 << CODA7_OPTION_RCQPMIN_OFFSET;
910 if (ctx->params.h264_max_qp)
911 value |= 1 << CODA7_OPTION_RCQPMAX_OFFSET;
912 }
913 coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION);
914
915 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_INTERVAL_MODE);
916
917 coda_setup_iram(ctx);
918
919 if (dst_fourcc == V4L2_PIX_FMT_H264) {
920 switch (dev->devtype->product) {
921 case CODA_DX6:
922 value = FMO_SLICE_SAVE_BUF_SIZE << 7;
923 coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO);
924 break;
925 case CODA_7541:
926 coda_write(dev, ctx->iram_info.search_ram_paddr,
927 CODA7_CMD_ENC_SEQ_SEARCH_BASE);
928 coda_write(dev, ctx->iram_info.search_ram_size,
929 CODA7_CMD_ENC_SEQ_SEARCH_SIZE);
930 break;
931 case CODA_960:
932 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_ME_OPTION);
933 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_INTRA_WEIGHT);
934 }
935 }
936
937 ret = coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT);
938 if (ret < 0) {
939 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
940 goto out;
941 }
942
943 if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0) {
944 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT failed\n");
945 ret = -EFAULT;
946 goto out;
947 }
948
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300949 if (dst_fourcc != V4L2_PIX_FMT_JPEG) {
950 if (dev->devtype->product == CODA_960)
951 ctx->num_internal_frames = 4;
952 else
953 ctx->num_internal_frames = 2;
954 ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc);
955 if (ret < 0) {
956 v4l2_err(v4l2_dev, "failed to allocate framebuffers\n");
957 goto out;
958 }
959 stride = q_data_src->bytesperline;
960 } else {
961 ctx->num_internal_frames = 0;
962 stride = 0;
Philipp Zabel79924ca2014-07-23 12:28:45 -0300963 }
Philipp Zabel79924ca2014-07-23 12:28:45 -0300964 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300965 coda_write(dev, stride, CODA_CMD_SET_FRAME_BUF_STRIDE);
966
Philipp Zabel79924ca2014-07-23 12:28:45 -0300967 if (dev->devtype->product == CODA_7541) {
968 coda_write(dev, q_data_src->bytesperline,
969 CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE);
970 }
971 if (dev->devtype->product != CODA_DX6) {
972 coda_write(dev, ctx->iram_info.buf_bit_use,
973 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
974 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
975 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
976 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
977 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
978 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
979 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
980 coda_write(dev, ctx->iram_info.buf_ovl_use,
981 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
982 if (dev->devtype->product == CODA_960) {
983 coda_write(dev, ctx->iram_info.buf_btp_use,
984 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
985
986 /* FIXME */
Philipp Zabelf23797b2014-08-06 08:02:23 -0300987 coda_write(dev, ctx->internal_frames[2].paddr,
988 CODA9_CMD_SET_FRAME_SUBSAMP_A);
989 coda_write(dev, ctx->internal_frames[3].paddr,
990 CODA9_CMD_SET_FRAME_SUBSAMP_B);
Philipp Zabel79924ca2014-07-23 12:28:45 -0300991 }
992 }
993
994 ret = coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF);
995 if (ret < 0) {
996 v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
997 goto out;
998 }
999
1000 /* Save stream headers */
1001 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
1002 switch (dst_fourcc) {
1003 case V4L2_PIX_FMT_H264:
1004 /*
1005 * Get SPS in the first frame and copy it to an
1006 * intermediate buffer.
1007 */
1008 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_SPS,
1009 &ctx->vpu_header[0][0],
1010 &ctx->vpu_header_size[0]);
1011 if (ret < 0)
1012 goto out;
1013
1014 /*
1015 * Get PPS in the first frame and copy it to an
1016 * intermediate buffer.
1017 */
1018 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_PPS,
1019 &ctx->vpu_header[1][0],
1020 &ctx->vpu_header_size[1]);
1021 if (ret < 0)
1022 goto out;
1023
1024 /*
1025 * Length of H.264 headers is variable and thus it might not be
1026 * aligned for the coda to append the encoded frame. In that is
1027 * the case a filler NAL must be added to header 2.
1028 */
1029 ctx->vpu_header_size[2] = coda_h264_padding(
1030 (ctx->vpu_header_size[0] +
1031 ctx->vpu_header_size[1]),
1032 ctx->vpu_header[2]);
1033 break;
1034 case V4L2_PIX_FMT_MPEG4:
1035 /*
1036 * Get VOS in the first frame and copy it to an
1037 * intermediate buffer
1038 */
1039 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOS,
1040 &ctx->vpu_header[0][0],
1041 &ctx->vpu_header_size[0]);
1042 if (ret < 0)
1043 goto out;
1044
1045 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VIS,
1046 &ctx->vpu_header[1][0],
1047 &ctx->vpu_header_size[1]);
1048 if (ret < 0)
1049 goto out;
1050
1051 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOL,
1052 &ctx->vpu_header[2][0],
1053 &ctx->vpu_header_size[2]);
1054 if (ret < 0)
1055 goto out;
1056 break;
1057 default:
1058 /* No more formats need to save headers at the moment */
1059 break;
1060 }
1061
1062out:
1063 mutex_unlock(&dev->coda_mutex);
1064 return ret;
1065}
1066
1067static int coda_prepare_encode(struct coda_ctx *ctx)
1068{
1069 struct coda_q_data *q_data_src, *q_data_dst;
1070 struct vb2_buffer *src_buf, *dst_buf;
1071 struct coda_dev *dev = ctx->dev;
1072 int force_ipicture;
1073 int quant_param = 0;
Philipp Zabel79924ca2014-07-23 12:28:45 -03001074 u32 pic_stream_buffer_addr, pic_stream_buffer_size;
Philipp Zabel59ebc2e2014-09-29 09:53:45 -03001075 u32 rot_mode = 0;
Philipp Zabel79924ca2014-07-23 12:28:45 -03001076 u32 dst_fourcc;
Philipp Zabel856d7d92014-09-29 09:53:44 -03001077 u32 reg;
Philipp Zabel79924ca2014-07-23 12:28:45 -03001078
1079 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
1080 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
1081 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1082 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1083 dst_fourcc = q_data_dst->fourcc;
1084
1085 src_buf->v4l2_buf.sequence = ctx->osequence;
1086 dst_buf->v4l2_buf.sequence = ctx->osequence;
1087 ctx->osequence++;
1088
1089 /*
1090 * Workaround coda firmware BUG that only marks the first
1091 * frame as IDR. This is a problem for some decoders that can't
1092 * recover when a frame is lost.
1093 */
1094 if (src_buf->v4l2_buf.sequence % ctx->params.gop_size) {
1095 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
1096 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
1097 } else {
1098 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
1099 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
1100 }
1101
1102 if (dev->devtype->product == CODA_960)
1103 coda_set_gdi_regs(ctx);
1104
1105 /*
1106 * Copy headers at the beginning of the first frame for H.264 only.
1107 * In MPEG4 they are already copied by the coda.
1108 */
1109 if (src_buf->v4l2_buf.sequence == 0) {
1110 pic_stream_buffer_addr =
1111 vb2_dma_contig_plane_dma_addr(dst_buf, 0) +
1112 ctx->vpu_header_size[0] +
1113 ctx->vpu_header_size[1] +
1114 ctx->vpu_header_size[2];
1115 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE -
1116 ctx->vpu_header_size[0] -
1117 ctx->vpu_header_size[1] -
1118 ctx->vpu_header_size[2];
1119 memcpy(vb2_plane_vaddr(dst_buf, 0),
1120 &ctx->vpu_header[0][0], ctx->vpu_header_size[0]);
1121 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0],
1122 &ctx->vpu_header[1][0], ctx->vpu_header_size[1]);
1123 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0] +
1124 ctx->vpu_header_size[1], &ctx->vpu_header[2][0],
1125 ctx->vpu_header_size[2]);
1126 } else {
1127 pic_stream_buffer_addr =
1128 vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1129 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE;
1130 }
1131
1132 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
1133 force_ipicture = 1;
1134 switch (dst_fourcc) {
1135 case V4L2_PIX_FMT_H264:
1136 quant_param = ctx->params.h264_intra_qp;
1137 break;
1138 case V4L2_PIX_FMT_MPEG4:
1139 quant_param = ctx->params.mpeg4_intra_qp;
1140 break;
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001141 case V4L2_PIX_FMT_JPEG:
1142 quant_param = 30;
1143 break;
Philipp Zabel79924ca2014-07-23 12:28:45 -03001144 default:
1145 v4l2_warn(&ctx->dev->v4l2_dev,
1146 "cannot set intra qp, fmt not supported\n");
1147 break;
1148 }
1149 } else {
1150 force_ipicture = 0;
1151 switch (dst_fourcc) {
1152 case V4L2_PIX_FMT_H264:
1153 quant_param = ctx->params.h264_inter_qp;
1154 break;
1155 case V4L2_PIX_FMT_MPEG4:
1156 quant_param = ctx->params.mpeg4_inter_qp;
1157 break;
1158 default:
1159 v4l2_warn(&ctx->dev->v4l2_dev,
1160 "cannot set inter qp, fmt not supported\n");
1161 break;
1162 }
1163 }
1164
1165 /* submit */
Philipp Zabel59ebc2e2014-09-29 09:53:45 -03001166 if (ctx->params.rot_mode)
1167 rot_mode = CODA_ROT_MIR_ENABLE | ctx->params.rot_mode;
1168 coda_write(dev, rot_mode, CODA_CMD_ENC_PIC_ROT_MODE);
Philipp Zabel79924ca2014-07-23 12:28:45 -03001169 coda_write(dev, quant_param, CODA_CMD_ENC_PIC_QS);
1170
Philipp Zabel79924ca2014-07-23 12:28:45 -03001171 if (dev->devtype->product == CODA_960) {
1172 coda_write(dev, 4/*FIXME: 0*/, CODA9_CMD_ENC_PIC_SRC_INDEX);
1173 coda_write(dev, q_data_src->width, CODA9_CMD_ENC_PIC_SRC_STRIDE);
1174 coda_write(dev, 0, CODA9_CMD_ENC_PIC_SUB_FRAME_SYNC);
1175
Philipp Zabel856d7d92014-09-29 09:53:44 -03001176 reg = CODA9_CMD_ENC_PIC_SRC_ADDR_Y;
Philipp Zabel79924ca2014-07-23 12:28:45 -03001177 } else {
Philipp Zabel856d7d92014-09-29 09:53:44 -03001178 reg = CODA_CMD_ENC_PIC_SRC_ADDR_Y;
Philipp Zabel79924ca2014-07-23 12:28:45 -03001179 }
Philipp Zabel856d7d92014-09-29 09:53:44 -03001180 coda_write_base(ctx, q_data_src, src_buf, reg);
1181
Philipp Zabel79924ca2014-07-23 12:28:45 -03001182 coda_write(dev, force_ipicture << 1 & 0x2,
1183 CODA_CMD_ENC_PIC_OPTION);
1184
1185 coda_write(dev, pic_stream_buffer_addr, CODA_CMD_ENC_PIC_BB_START);
1186 coda_write(dev, pic_stream_buffer_size / 1024,
1187 CODA_CMD_ENC_PIC_BB_SIZE);
1188
1189 if (!ctx->streamon_out) {
Philipp Zabelf23797b2014-08-06 08:02:23 -03001190 /* After streamoff on the output side, set stream end flag */
Philipp Zabel79924ca2014-07-23 12:28:45 -03001191 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
Philipp Zabelf23797b2014-08-06 08:02:23 -03001192 coda_write(dev, ctx->bit_stream_param,
1193 CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel79924ca2014-07-23 12:28:45 -03001194 }
1195
1196 if (dev->devtype->product != CODA_DX6)
1197 coda_write(dev, ctx->iram_info.axi_sram_use,
1198 CODA7_REG_BIT_AXI_SRAM_USE);
1199
1200 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
1201
1202 return 0;
1203}
1204
1205static void coda_finish_encode(struct coda_ctx *ctx)
1206{
1207 struct vb2_buffer *src_buf, *dst_buf;
1208 struct coda_dev *dev = ctx->dev;
1209 u32 wr_ptr, start_ptr;
1210
1211 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1212 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
1213
1214 /* Get results from the coda */
1215 start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START);
1216 wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
1217
1218 /* Calculate bytesused field */
1219 if (dst_buf->v4l2_buf.sequence == 0) {
1220 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr +
1221 ctx->vpu_header_size[0] +
1222 ctx->vpu_header_size[1] +
1223 ctx->vpu_header_size[2]);
1224 } else {
1225 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr);
1226 }
1227
1228 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n",
1229 wr_ptr - start_ptr);
1230
1231 coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM);
1232 coda_read(dev, CODA_RET_ENC_PIC_FLAG);
1233
1234 if (coda_read(dev, CODA_RET_ENC_PIC_TYPE) == 0) {
1235 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
1236 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
1237 } else {
1238 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
1239 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
1240 }
1241
1242 dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp;
1243 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
1244 dst_buf->v4l2_buf.flags |=
1245 src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
1246 dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode;
1247
1248 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1249
1250 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1251 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
1252
1253 ctx->gopcounter--;
1254 if (ctx->gopcounter < 0)
1255 ctx->gopcounter = ctx->params.gop_size - 1;
1256
1257 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1258 "job finished: encoding frame (%d) (%s)\n",
1259 dst_buf->v4l2_buf.sequence,
1260 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
1261 "KEYFRAME" : "PFRAME");
1262}
1263
1264static void coda_seq_end_work(struct work_struct *work)
1265{
1266 struct coda_ctx *ctx = container_of(work, struct coda_ctx, seq_end_work);
1267 struct coda_dev *dev = ctx->dev;
1268
1269 mutex_lock(&ctx->buffer_mutex);
1270 mutex_lock(&dev->coda_mutex);
1271
1272 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Philipp Zabelf23797b2014-08-06 08:02:23 -03001273 "%d: %s: sent command 'SEQ_END' to coda\n", ctx->idx,
1274 __func__);
Philipp Zabel79924ca2014-07-23 12:28:45 -03001275 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
1276 v4l2_err(&dev->v4l2_dev,
1277 "CODA_COMMAND_SEQ_END failed\n");
1278 }
1279
1280 kfifo_init(&ctx->bitstream_fifo,
1281 ctx->bitstream.vaddr, ctx->bitstream.size);
1282
1283 coda_free_framebuffers(ctx);
1284 coda_free_context_buffers(ctx);
1285
1286 mutex_unlock(&dev->coda_mutex);
1287 mutex_unlock(&ctx->buffer_mutex);
1288}
1289
1290static void coda_bit_release(struct coda_ctx *ctx)
1291{
1292 coda_free_framebuffers(ctx);
1293 coda_free_context_buffers(ctx);
1294}
1295
1296const struct coda_context_ops coda_bit_encode_ops = {
1297 .queue_init = coda_encoder_queue_init,
1298 .start_streaming = coda_start_encoding,
1299 .prepare_run = coda_prepare_encode,
1300 .finish_run = coda_finish_encode,
1301 .seq_end_work = coda_seq_end_work,
1302 .release = coda_bit_release,
1303};
1304
1305/*
1306 * Decoder context operations
1307 */
1308
1309static int __coda_start_decoding(struct coda_ctx *ctx)
1310{
1311 struct coda_q_data *q_data_src, *q_data_dst;
1312 u32 bitstream_buf, bitstream_size;
1313 struct coda_dev *dev = ctx->dev;
1314 int width, height;
Philipp Zabel1cb12cf2014-09-29 09:53:47 -03001315 u32 src_fourcc, dst_fourcc;
Philipp Zabel79924ca2014-07-23 12:28:45 -03001316 u32 val;
1317 int ret;
1318
1319 /* Start decoding */
1320 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1321 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1322 bitstream_buf = ctx->bitstream.paddr;
1323 bitstream_size = ctx->bitstream.size;
1324 src_fourcc = q_data_src->fourcc;
Philipp Zabel1cb12cf2014-09-29 09:53:47 -03001325 dst_fourcc = q_data_dst->fourcc;
Philipp Zabel79924ca2014-07-23 12:28:45 -03001326
1327 /* Allocate per-instance buffers */
1328 ret = coda_alloc_context_buffers(ctx, q_data_src);
1329 if (ret < 0)
1330 return ret;
1331
1332 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
1333
1334 /* Update coda bitstream read and write pointers from kfifo */
1335 coda_kfifo_sync_to_device_full(ctx);
1336
Philipp Zabel1cb12cf2014-09-29 09:53:47 -03001337 ctx->frame_mem_ctrl &= ~CODA_FRAME_CHROMA_INTERLEAVE;
1338 if (dst_fourcc == V4L2_PIX_FMT_NV12)
1339 ctx->frame_mem_ctrl |= CODA_FRAME_CHROMA_INTERLEAVE;
Philipp Zabel2bf299c2014-09-29 09:53:46 -03001340 coda_write(dev, ctx->frame_mem_ctrl, CODA_REG_BIT_FRAME_MEM_CTRL);
1341
Philipp Zabel79924ca2014-07-23 12:28:45 -03001342 ctx->display_idx = -1;
1343 ctx->frm_dis_flg = 0;
1344 coda_write(dev, 0, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
1345
1346 coda_write(dev, CODA_BIT_DEC_SEQ_INIT_ESCAPE,
1347 CODA_REG_BIT_BIT_STREAM_PARAM);
1348
1349 coda_write(dev, bitstream_buf, CODA_CMD_DEC_SEQ_BB_START);
1350 coda_write(dev, bitstream_size / 1024, CODA_CMD_DEC_SEQ_BB_SIZE);
1351 val = 0;
1352 if ((dev->devtype->product == CODA_7541) ||
1353 (dev->devtype->product == CODA_960))
1354 val |= CODA_REORDER_ENABLE;
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001355 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_JPEG)
1356 val |= CODA_NO_INT_ENABLE;
Philipp Zabel79924ca2014-07-23 12:28:45 -03001357 coda_write(dev, val, CODA_CMD_DEC_SEQ_OPTION);
1358
1359 ctx->params.codec_mode = ctx->codec->mode;
1360 if (dev->devtype->product == CODA_960 &&
1361 src_fourcc == V4L2_PIX_FMT_MPEG4)
1362 ctx->params.codec_mode_aux = CODA_MP4_AUX_MPEG4;
1363 else
1364 ctx->params.codec_mode_aux = 0;
1365 if (src_fourcc == V4L2_PIX_FMT_H264) {
1366 if (dev->devtype->product == CODA_7541) {
1367 coda_write(dev, ctx->psbuf.paddr,
1368 CODA_CMD_DEC_SEQ_PS_BB_START);
1369 coda_write(dev, (CODA7_PS_BUF_SIZE / 1024),
1370 CODA_CMD_DEC_SEQ_PS_BB_SIZE);
1371 }
1372 if (dev->devtype->product == CODA_960) {
1373 coda_write(dev, 0, CODA_CMD_DEC_SEQ_X264_MV_EN);
1374 coda_write(dev, 512, CODA_CMD_DEC_SEQ_SPP_CHUNK_SIZE);
1375 }
1376 }
1377 if (dev->devtype->product != CODA_960)
1378 coda_write(dev, 0, CODA_CMD_DEC_SEQ_SRC_SIZE);
1379
1380 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT)) {
1381 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
1382 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
1383 return -ETIMEDOUT;
1384 }
1385
1386 /* Update kfifo out pointer from coda bitstream read pointer */
1387 coda_kfifo_sync_from_device(ctx);
1388
1389 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
1390
1391 if (coda_read(dev, CODA_RET_DEC_SEQ_SUCCESS) == 0) {
1392 v4l2_err(&dev->v4l2_dev,
1393 "CODA_COMMAND_SEQ_INIT failed, error code = %d\n",
1394 coda_read(dev, CODA_RET_DEC_SEQ_ERR_REASON));
1395 return -EAGAIN;
1396 }
1397
1398 val = coda_read(dev, CODA_RET_DEC_SEQ_SRC_SIZE);
1399 if (dev->devtype->product == CODA_DX6) {
1400 width = (val >> CODADX6_PICWIDTH_OFFSET) & CODADX6_PICWIDTH_MASK;
1401 height = val & CODADX6_PICHEIGHT_MASK;
1402 } else {
1403 width = (val >> CODA7_PICWIDTH_OFFSET) & CODA7_PICWIDTH_MASK;
1404 height = val & CODA7_PICHEIGHT_MASK;
1405 }
1406
1407 if (width > q_data_dst->width || height > q_data_dst->height) {
1408 v4l2_err(&dev->v4l2_dev, "stream is %dx%d, not %dx%d\n",
1409 width, height, q_data_dst->width, q_data_dst->height);
1410 return -EINVAL;
1411 }
1412
1413 width = round_up(width, 16);
1414 height = round_up(height, 16);
1415
1416 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "%s instance %d now: %dx%d\n",
1417 __func__, ctx->idx, width, height);
1418
1419 ctx->num_internal_frames = coda_read(dev, CODA_RET_DEC_SEQ_FRAME_NEED);
1420 if (ctx->num_internal_frames > CODA_MAX_FRAMEBUFFERS) {
1421 v4l2_err(&dev->v4l2_dev,
1422 "not enough framebuffers to decode (%d < %d)\n",
1423 CODA_MAX_FRAMEBUFFERS, ctx->num_internal_frames);
1424 return -EINVAL;
1425 }
1426
1427 if (src_fourcc == V4L2_PIX_FMT_H264) {
1428 u32 left_right;
1429 u32 top_bottom;
1430
1431 left_right = coda_read(dev, CODA_RET_DEC_SEQ_CROP_LEFT_RIGHT);
1432 top_bottom = coda_read(dev, CODA_RET_DEC_SEQ_CROP_TOP_BOTTOM);
1433
1434 q_data_dst->rect.left = (left_right >> 10) & 0x3ff;
1435 q_data_dst->rect.top = (top_bottom >> 10) & 0x3ff;
1436 q_data_dst->rect.width = width - q_data_dst->rect.left -
1437 (left_right & 0x3ff);
1438 q_data_dst->rect.height = height - q_data_dst->rect.top -
1439 (top_bottom & 0x3ff);
1440 }
1441
1442 ret = coda_alloc_framebuffers(ctx, q_data_dst, src_fourcc);
Philipp Zabel68fc31c2014-08-05 14:00:16 -03001443 if (ret < 0) {
1444 v4l2_err(&dev->v4l2_dev, "failed to allocate framebuffers\n");
Philipp Zabel79924ca2014-07-23 12:28:45 -03001445 return ret;
Philipp Zabel68fc31c2014-08-05 14:00:16 -03001446 }
Philipp Zabel79924ca2014-07-23 12:28:45 -03001447
1448 /* Tell the decoder how many frame buffers we allocated. */
1449 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
1450 coda_write(dev, width, CODA_CMD_SET_FRAME_BUF_STRIDE);
1451
1452 if (dev->devtype->product != CODA_DX6) {
1453 /* Set secondary AXI IRAM */
1454 coda_setup_iram(ctx);
1455
1456 coda_write(dev, ctx->iram_info.buf_bit_use,
1457 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
1458 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
1459 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
1460 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
1461 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
1462 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
1463 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
1464 coda_write(dev, ctx->iram_info.buf_ovl_use,
1465 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
1466 if (dev->devtype->product == CODA_960)
1467 coda_write(dev, ctx->iram_info.buf_btp_use,
1468 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
1469 }
1470
1471 if (dev->devtype->product == CODA_960) {
Philipp Zabel1cb12cf2014-09-29 09:53:47 -03001472 int cbb_size, crb_size;
Philipp Zabel79924ca2014-07-23 12:28:45 -03001473
Philipp Zabel1cb12cf2014-09-29 09:53:47 -03001474 coda_write(dev, -1, CODA9_CMD_SET_FRAME_DELAY);
1475 /* Luma 2x0 page, 2x6 cache, chroma 2x0 page, 2x4 cache size */
Philipp Zabel79924ca2014-07-23 12:28:45 -03001476 coda_write(dev, 0x20262024, CODA9_CMD_SET_FRAME_CACHE_SIZE);
Philipp Zabel1cb12cf2014-09-29 09:53:47 -03001477
1478 if (dst_fourcc == V4L2_PIX_FMT_NV12) {
1479 cbb_size = 0;
1480 crb_size = 16;
1481 } else {
1482 cbb_size = 8;
1483 crb_size = 8;
1484 }
Philipp Zabel79924ca2014-07-23 12:28:45 -03001485 coda_write(dev, 2 << CODA9_CACHE_PAGEMERGE_OFFSET |
1486 32 << CODA9_CACHE_LUMA_BUFFER_SIZE_OFFSET |
Philipp Zabel1cb12cf2014-09-29 09:53:47 -03001487 cbb_size << CODA9_CACHE_CB_BUFFER_SIZE_OFFSET |
1488 crb_size << CODA9_CACHE_CR_BUFFER_SIZE_OFFSET,
Philipp Zabel79924ca2014-07-23 12:28:45 -03001489 CODA9_CMD_SET_FRAME_CACHE_CONFIG);
1490 }
1491
1492 if (src_fourcc == V4L2_PIX_FMT_H264) {
1493 coda_write(dev, ctx->slicebuf.paddr,
1494 CODA_CMD_SET_FRAME_SLICE_BB_START);
1495 coda_write(dev, ctx->slicebuf.size / 1024,
1496 CODA_CMD_SET_FRAME_SLICE_BB_SIZE);
1497 }
1498
1499 if (dev->devtype->product == CODA_7541) {
1500 int max_mb_x = 1920 / 16;
1501 int max_mb_y = 1088 / 16;
1502 int max_mb_num = max_mb_x * max_mb_y;
1503
1504 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
1505 CODA7_CMD_SET_FRAME_MAX_DEC_SIZE);
1506 } else if (dev->devtype->product == CODA_960) {
1507 int max_mb_x = 1920 / 16;
1508 int max_mb_y = 1088 / 16;
1509 int max_mb_num = max_mb_x * max_mb_y;
1510
1511 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
1512 CODA9_CMD_SET_FRAME_MAX_DEC_SIZE);
1513 }
1514
1515 if (coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF)) {
1516 v4l2_err(&ctx->dev->v4l2_dev,
1517 "CODA_COMMAND_SET_FRAME_BUF timeout\n");
1518 return -ETIMEDOUT;
1519 }
1520
1521 return 0;
1522}
1523
1524static int coda_start_decoding(struct coda_ctx *ctx)
1525{
1526 struct coda_dev *dev = ctx->dev;
1527 int ret;
1528
1529 mutex_lock(&dev->coda_mutex);
1530 ret = __coda_start_decoding(ctx);
1531 mutex_unlock(&dev->coda_mutex);
1532
1533 return ret;
1534}
1535
1536static int coda_prepare_decode(struct coda_ctx *ctx)
1537{
1538 struct vb2_buffer *dst_buf;
1539 struct coda_dev *dev = ctx->dev;
1540 struct coda_q_data *q_data_dst;
Philipp Zabel856d7d92014-09-29 09:53:44 -03001541 u32 reg_addr, reg_stride;
Philipp Zabel79924ca2014-07-23 12:28:45 -03001542
1543 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
1544 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1545
Philipp Zabel79924ca2014-07-23 12:28:45 -03001546 /* Try to copy source buffer contents into the bitstream ringbuffer */
1547 mutex_lock(&ctx->bitstream_mutex);
1548 coda_fill_bitstream(ctx);
1549 mutex_unlock(&ctx->bitstream_mutex);
1550
1551 if (coda_get_bitstream_payload(ctx) < 512 &&
1552 (!(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1553 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1554 "bitstream payload: %d, skipping\n",
1555 coda_get_bitstream_payload(ctx));
1556 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
1557 return -EAGAIN;
1558 }
1559
1560 /* Run coda_start_decoding (again) if not yet initialized */
1561 if (!ctx->initialized) {
1562 int ret = __coda_start_decoding(ctx);
1563
1564 if (ret < 0) {
1565 v4l2_err(&dev->v4l2_dev, "failed to start decoding\n");
1566 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
1567 return -EAGAIN;
1568 } else {
1569 ctx->initialized = 1;
1570 }
1571 }
1572
1573 if (dev->devtype->product == CODA_960)
1574 coda_set_gdi_regs(ctx);
1575
Philipp Zabel79924ca2014-07-23 12:28:45 -03001576 if (dev->devtype->product == CODA_960) {
1577 /*
1578 * The CODA960 seems to have an internal list of buffers with
1579 * 64 entries that includes the registered frame buffers as
1580 * well as the rotator buffer output.
1581 * ROT_INDEX needs to be < 0x40, but > ctx->num_internal_frames.
1582 */
1583 coda_write(dev, CODA_MAX_FRAMEBUFFERS + dst_buf->v4l2_buf.index,
1584 CODA9_CMD_DEC_PIC_ROT_INDEX);
Philipp Zabel856d7d92014-09-29 09:53:44 -03001585
1586 reg_addr = CODA9_CMD_DEC_PIC_ROT_ADDR_Y;
1587 reg_stride = CODA9_CMD_DEC_PIC_ROT_STRIDE;
Philipp Zabel79924ca2014-07-23 12:28:45 -03001588 } else {
Philipp Zabel856d7d92014-09-29 09:53:44 -03001589 reg_addr = CODA_CMD_DEC_PIC_ROT_ADDR_Y;
1590 reg_stride = CODA_CMD_DEC_PIC_ROT_STRIDE;
Philipp Zabel79924ca2014-07-23 12:28:45 -03001591 }
Philipp Zabel856d7d92014-09-29 09:53:44 -03001592 coda_write_base(ctx, q_data_dst, dst_buf, reg_addr);
1593 coda_write(dev, q_data_dst->bytesperline, reg_stride);
1594
Philipp Zabel79924ca2014-07-23 12:28:45 -03001595 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode,
1596 CODA_CMD_DEC_PIC_ROT_MODE);
1597
1598 switch (dev->devtype->product) {
1599 case CODA_DX6:
1600 /* TBD */
1601 case CODA_7541:
1602 coda_write(dev, CODA_PRE_SCAN_EN, CODA_CMD_DEC_PIC_OPTION);
1603 break;
1604 case CODA_960:
Philipp Zabelf23797b2014-08-06 08:02:23 -03001605 /* 'hardcode to use interrupt disable mode'? */
1606 coda_write(dev, (1 << 10), CODA_CMD_DEC_PIC_OPTION);
Philipp Zabel79924ca2014-07-23 12:28:45 -03001607 break;
1608 }
1609
1610 coda_write(dev, 0, CODA_CMD_DEC_PIC_SKIP_NUM);
1611
1612 coda_write(dev, 0, CODA_CMD_DEC_PIC_BB_START);
1613 coda_write(dev, 0, CODA_CMD_DEC_PIC_START_BYTE);
1614
1615 if (dev->devtype->product != CODA_DX6)
1616 coda_write(dev, ctx->iram_info.axi_sram_use,
1617 CODA7_REG_BIT_AXI_SRAM_USE);
1618
1619 coda_kfifo_sync_to_device_full(ctx);
1620
1621 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
1622
1623 return 0;
1624}
1625
1626static void coda_finish_decode(struct coda_ctx *ctx)
1627{
1628 struct coda_dev *dev = ctx->dev;
1629 struct coda_q_data *q_data_src;
1630 struct coda_q_data *q_data_dst;
1631 struct vb2_buffer *dst_buf;
1632 struct coda_timestamp *ts;
Philipp Zabel4de69312014-10-02 14:08:26 -03001633 unsigned long payload;
Philipp Zabel79924ca2014-07-23 12:28:45 -03001634 int width, height;
1635 int decoded_idx;
1636 int display_idx;
1637 u32 src_fourcc;
1638 int success;
1639 u32 err_mb;
1640 u32 val;
1641
Philipp Zabel79924ca2014-07-23 12:28:45 -03001642 /* Update kfifo out pointer from coda bitstream read pointer */
1643 coda_kfifo_sync_from_device(ctx);
1644
1645 /*
1646 * in stream-end mode, the read pointer can overshoot the write pointer
1647 * by up to 512 bytes
1648 */
1649 if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) {
1650 if (coda_get_bitstream_payload(ctx) >= CODA_MAX_FRAME_SIZE - 512)
1651 kfifo_init(&ctx->bitstream_fifo,
1652 ctx->bitstream.vaddr, ctx->bitstream.size);
1653 }
1654
1655 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1656 src_fourcc = q_data_src->fourcc;
1657
1658 val = coda_read(dev, CODA_RET_DEC_PIC_SUCCESS);
1659 if (val != 1)
1660 pr_err("DEC_PIC_SUCCESS = %d\n", val);
1661
1662 success = val & 0x1;
1663 if (!success)
1664 v4l2_err(&dev->v4l2_dev, "decode failed\n");
1665
1666 if (src_fourcc == V4L2_PIX_FMT_H264) {
1667 if (val & (1 << 3))
1668 v4l2_err(&dev->v4l2_dev,
1669 "insufficient PS buffer space (%d bytes)\n",
1670 ctx->psbuf.size);
1671 if (val & (1 << 2))
1672 v4l2_err(&dev->v4l2_dev,
1673 "insufficient slice buffer space (%d bytes)\n",
1674 ctx->slicebuf.size);
1675 }
1676
1677 val = coda_read(dev, CODA_RET_DEC_PIC_SIZE);
1678 width = (val >> 16) & 0xffff;
1679 height = val & 0xffff;
1680
1681 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1682
1683 /* frame crop information */
1684 if (src_fourcc == V4L2_PIX_FMT_H264) {
1685 u32 left_right;
1686 u32 top_bottom;
1687
1688 left_right = coda_read(dev, CODA_RET_DEC_PIC_CROP_LEFT_RIGHT);
1689 top_bottom = coda_read(dev, CODA_RET_DEC_PIC_CROP_TOP_BOTTOM);
1690
1691 if (left_right == 0xffffffff && top_bottom == 0xffffffff) {
1692 /* Keep current crop information */
1693 } else {
1694 struct v4l2_rect *rect = &q_data_dst->rect;
1695
1696 rect->left = left_right >> 16 & 0xffff;
1697 rect->top = top_bottom >> 16 & 0xffff;
1698 rect->width = width - rect->left -
1699 (left_right & 0xffff);
1700 rect->height = height - rect->top -
1701 (top_bottom & 0xffff);
1702 }
1703 } else {
1704 /* no cropping */
1705 }
1706
1707 err_mb = coda_read(dev, CODA_RET_DEC_PIC_ERR_MB);
1708 if (err_mb > 0)
1709 v4l2_err(&dev->v4l2_dev,
1710 "errors in %d macroblocks\n", err_mb);
1711
1712 if (dev->devtype->product == CODA_7541) {
1713 val = coda_read(dev, CODA_RET_DEC_PIC_OPTION);
1714 if (val == 0) {
1715 /* not enough bitstream data */
1716 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1717 "prescan failed: %d\n", val);
1718 ctx->hold = true;
1719 return;
1720 }
1721 }
1722
Philipp Zabelf23797b2014-08-06 08:02:23 -03001723 ctx->frm_dis_flg = coda_read(dev,
1724 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
Philipp Zabel79924ca2014-07-23 12:28:45 -03001725
1726 /*
1727 * The previous display frame was copied out by the rotator,
1728 * now it can be overwritten again
1729 */
1730 if (ctx->display_idx >= 0 &&
1731 ctx->display_idx < ctx->num_internal_frames) {
1732 ctx->frm_dis_flg &= ~(1 << ctx->display_idx);
1733 coda_write(dev, ctx->frm_dis_flg,
1734 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
1735 }
1736
1737 /*
1738 * The index of the last decoded frame, not necessarily in
1739 * display order, and the index of the next display frame.
1740 * The latter could have been decoded in a previous run.
1741 */
1742 decoded_idx = coda_read(dev, CODA_RET_DEC_PIC_CUR_IDX);
1743 display_idx = coda_read(dev, CODA_RET_DEC_PIC_FRAME_IDX);
1744
1745 if (decoded_idx == -1) {
1746 /* no frame was decoded, but we might have a display frame */
1747 if (display_idx >= 0 && display_idx < ctx->num_internal_frames)
1748 ctx->sequence_offset++;
1749 else if (ctx->display_idx < 0)
1750 ctx->hold = true;
1751 } else if (decoded_idx == -2) {
Philipp Zabelf23797b2014-08-06 08:02:23 -03001752 /* no frame was decoded, we still return remaining buffers */
Philipp Zabel79924ca2014-07-23 12:28:45 -03001753 } else if (decoded_idx < 0 || decoded_idx >= ctx->num_internal_frames) {
1754 v4l2_err(&dev->v4l2_dev,
1755 "decoded frame index out of range: %d\n", decoded_idx);
1756 } else {
Philipp Zabel79924ca2014-07-23 12:28:45 -03001757 val = coda_read(dev, CODA_RET_DEC_PIC_FRAME_NUM) - 1;
1758 val -= ctx->sequence_offset;
Philipp Zabel18fd0cc2014-08-05 14:00:17 -03001759 mutex_lock(&ctx->bitstream_mutex);
1760 if (!list_empty(&ctx->timestamp_list)) {
1761 ts = list_first_entry(&ctx->timestamp_list,
1762 struct coda_timestamp, list);
1763 list_del(&ts->list);
1764 if (val != (ts->sequence & 0xffff)) {
1765 v4l2_err(&dev->v4l2_dev,
1766 "sequence number mismatch (%d(%d) != %d)\n",
1767 val, ctx->sequence_offset,
1768 ts->sequence);
1769 }
1770 ctx->frame_timestamps[decoded_idx] = *ts;
1771 kfree(ts);
1772 } else {
1773 v4l2_err(&dev->v4l2_dev, "empty timestamp list!\n");
1774 memset(&ctx->frame_timestamps[decoded_idx], 0,
1775 sizeof(struct coda_timestamp));
1776 ctx->frame_timestamps[decoded_idx].sequence = val;
Philipp Zabel79924ca2014-07-23 12:28:45 -03001777 }
Philipp Zabel18fd0cc2014-08-05 14:00:17 -03001778 mutex_unlock(&ctx->bitstream_mutex);
Philipp Zabel79924ca2014-07-23 12:28:45 -03001779
1780 val = coda_read(dev, CODA_RET_DEC_PIC_TYPE) & 0x7;
1781 if (val == 0)
1782 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_KEYFRAME;
1783 else if (val == 1)
1784 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_PFRAME;
1785 else
1786 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_BFRAME;
1787
1788 ctx->frame_errors[decoded_idx] = err_mb;
1789 }
1790
1791 if (display_idx == -1) {
1792 /*
1793 * no more frames to be decoded, but there could still
1794 * be rotator output to dequeue
1795 */
1796 ctx->hold = true;
1797 } else if (display_idx == -3) {
1798 /* possibly prescan failure */
1799 } else if (display_idx < 0 || display_idx >= ctx->num_internal_frames) {
1800 v4l2_err(&dev->v4l2_dev,
1801 "presentation frame index out of range: %d\n",
1802 display_idx);
1803 }
1804
1805 /* If a frame was copied out, return it */
1806 if (ctx->display_idx >= 0 &&
1807 ctx->display_idx < ctx->num_internal_frames) {
1808 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1809 dst_buf->v4l2_buf.sequence = ctx->osequence++;
1810
1811 dst_buf->v4l2_buf.flags &= ~(V4L2_BUF_FLAG_KEYFRAME |
1812 V4L2_BUF_FLAG_PFRAME |
1813 V4L2_BUF_FLAG_BFRAME);
1814 dst_buf->v4l2_buf.flags |= ctx->frame_types[ctx->display_idx];
1815 ts = &ctx->frame_timestamps[ctx->display_idx];
1816 dst_buf->v4l2_buf.timecode = ts->timecode;
1817 dst_buf->v4l2_buf.timestamp = ts->timestamp;
1818
Philipp Zabel4de69312014-10-02 14:08:26 -03001819 switch (q_data_dst->fourcc) {
1820 case V4L2_PIX_FMT_YUV420:
1821 case V4L2_PIX_FMT_YVU420:
1822 case V4L2_PIX_FMT_NV12:
1823 default:
1824 payload = width * height * 3 / 2;
1825 break;
1826 case V4L2_PIX_FMT_YUV422P:
1827 payload = width * height * 2;
1828 break;
1829 }
1830 vb2_set_plane_payload(dst_buf, 0, payload);
Philipp Zabel79924ca2014-07-23 12:28:45 -03001831
1832 v4l2_m2m_buf_done(dst_buf, ctx->frame_errors[display_idx] ?
1833 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
1834
1835 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1836 "job finished: decoding frame (%d) (%s)\n",
1837 dst_buf->v4l2_buf.sequence,
1838 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
1839 "KEYFRAME" : "PFRAME");
1840 } else {
1841 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1842 "job finished: no frame decoded\n");
1843 }
1844
1845 /* The rotator will copy the current display frame next time */
1846 ctx->display_idx = display_idx;
1847}
1848
1849const struct coda_context_ops coda_bit_decode_ops = {
1850 .queue_init = coda_decoder_queue_init,
1851 .start_streaming = coda_start_decoding,
1852 .prepare_run = coda_prepare_decode,
1853 .finish_run = coda_finish_decode,
1854 .seq_end_work = coda_seq_end_work,
1855 .release = coda_bit_release,
1856};
1857
1858irqreturn_t coda_irq_handler(int irq, void *data)
1859{
1860 struct coda_dev *dev = data;
1861 struct coda_ctx *ctx;
1862
1863 /* read status register to attend the IRQ */
1864 coda_read(dev, CODA_REG_BIT_INT_STATUS);
1865 coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET,
1866 CODA_REG_BIT_INT_CLEAR);
1867
1868 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
1869 if (ctx == NULL) {
Philipp Zabelf23797b2014-08-06 08:02:23 -03001870 v4l2_err(&dev->v4l2_dev,
1871 "Instance released before the end of transaction\n");
Philipp Zabel79924ca2014-07-23 12:28:45 -03001872 mutex_unlock(&dev->coda_mutex);
1873 return IRQ_HANDLED;
1874 }
1875
1876 if (ctx->aborting) {
1877 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1878 "task has been aborted\n");
1879 }
1880
1881 if (coda_isbusy(ctx->dev)) {
1882 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1883 "coda is still busy!!!!\n");
1884 return IRQ_NONE;
1885 }
1886
1887 complete(&ctx->completion);
1888
1889 return IRQ_HANDLED;
1890}