coda-bit.c 64 KB

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