coda-bit.c 65 KB

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