coda-jpeg.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Coda multi-standard codec IP - JPEG support functions
  4. *
  5. * Copyright (C) 2014 Philipp Zabel, Pengutronix
  6. */
  7. #include <asm/unaligned.h>
  8. #include <linux/irqreturn.h>
  9. #include <linux/kernel.h>
  10. #include <linux/ktime.h>
  11. #include <linux/slab.h>
  12. #include <linux/swab.h>
  13. #include <linux/videodev2.h>
  14. #include <media/v4l2-common.h>
  15. #include <media/v4l2-fh.h>
  16. #include <media/v4l2-jpeg.h>
  17. #include <media/v4l2-mem2mem.h>
  18. #include <media/videobuf2-core.h>
  19. #include <media/videobuf2-dma-contig.h>
  20. #include "coda.h"
  21. #include "trace.h"
  22. #define SOI_MARKER 0xffd8
  23. #define APP9_MARKER 0xffe9
  24. #define DRI_MARKER 0xffdd
  25. #define DQT_MARKER 0xffdb
  26. #define DHT_MARKER 0xffc4
  27. #define SOF_MARKER 0xffc0
  28. #define SOS_MARKER 0xffda
  29. #define EOI_MARKER 0xffd9
  30. enum {
  31. CODA9_JPEG_FORMAT_420,
  32. CODA9_JPEG_FORMAT_422,
  33. CODA9_JPEG_FORMAT_224,
  34. CODA9_JPEG_FORMAT_444,
  35. CODA9_JPEG_FORMAT_400,
  36. };
  37. struct coda_huff_tab {
  38. u8 luma_dc[16 + 12];
  39. u8 chroma_dc[16 + 12];
  40. u8 luma_ac[16 + 162];
  41. u8 chroma_ac[16 + 162];
  42. /* DC Luma, DC Chroma, AC Luma, AC Chroma */
  43. s16 min[4 * 16];
  44. s16 max[4 * 16];
  45. s8 ptr[4 * 16];
  46. };
  47. #define CODA9_JPEG_ENC_HUFF_DATA_SIZE (256 + 256 + 16 + 16)
  48. /*
  49. * Typical Huffman tables for 8-bit precision luminance and
  50. * chrominance from JPEG ITU-T.81 (ISO/IEC 10918-1) Annex K.3
  51. */
  52. static const unsigned char luma_dc[16 + 12] = {
  53. /* bits */
  54. 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01,
  55. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  56. /* values */
  57. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  58. 0x08, 0x09, 0x0a, 0x0b,
  59. };
  60. static const unsigned char chroma_dc[16 + 12] = {
  61. /* bits */
  62. 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  63. 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  64. /* values */
  65. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  66. 0x08, 0x09, 0x0a, 0x0b,
  67. };
  68. static const unsigned char luma_ac[16 + 162 + 2] = {
  69. /* bits */
  70. 0x00, 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03,
  71. 0x05, 0x05, 0x04, 0x04, 0x00, 0x00, 0x01, 0x7d,
  72. /* values */
  73. 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
  74. 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
  75. 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
  76. 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
  77. 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
  78. 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
  79. 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
  80. 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
  81. 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
  82. 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
  83. 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
  84. 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
  85. 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
  86. 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  87. 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
  88. 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
  89. 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
  90. 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
  91. 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
  92. 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  93. 0xf9, 0xfa, /* padded to 32-bit */
  94. };
  95. static const unsigned char chroma_ac[16 + 162 + 2] = {
  96. /* bits */
  97. 0x00, 0x02, 0x01, 0x02, 0x04, 0x04, 0x03, 0x04,
  98. 0x07, 0x05, 0x04, 0x04, 0x00, 0x01, 0x02, 0x77,
  99. /* values */
  100. 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
  101. 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
  102. 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
  103. 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
  104. 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
  105. 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
  106. 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
  107. 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
  108. 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
  109. 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
  110. 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
  111. 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  112. 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
  113. 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
  114. 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
  115. 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
  116. 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
  117. 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
  118. 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
  119. 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  120. 0xf9, 0xfa, /* padded to 32-bit */
  121. };
  122. /*
  123. * Quantization tables for luminance and chrominance components in
  124. * zig-zag scan order from the Freescale i.MX VPU libraries
  125. */
  126. static unsigned char luma_q[64] = {
  127. 0x06, 0x04, 0x04, 0x04, 0x05, 0x04, 0x06, 0x05,
  128. 0x05, 0x06, 0x09, 0x06, 0x05, 0x06, 0x09, 0x0b,
  129. 0x08, 0x06, 0x06, 0x08, 0x0b, 0x0c, 0x0a, 0x0a,
  130. 0x0b, 0x0a, 0x0a, 0x0c, 0x10, 0x0c, 0x0c, 0x0c,
  131. 0x0c, 0x0c, 0x0c, 0x10, 0x0c, 0x0c, 0x0c, 0x0c,
  132. 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
  133. 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
  134. 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
  135. };
  136. static unsigned char chroma_q[64] = {
  137. 0x07, 0x07, 0x07, 0x0d, 0x0c, 0x0d, 0x18, 0x10,
  138. 0x10, 0x18, 0x14, 0x0e, 0x0e, 0x0e, 0x14, 0x14,
  139. 0x0e, 0x0e, 0x0e, 0x0e, 0x14, 0x11, 0x0c, 0x0c,
  140. 0x0c, 0x0c, 0x0c, 0x11, 0x11, 0x0c, 0x0c, 0x0c,
  141. 0x0c, 0x0c, 0x0c, 0x11, 0x0c, 0x0c, 0x0c, 0x0c,
  142. 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
  143. 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
  144. 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
  145. };
  146. static const unsigned char width_align[] = {
  147. [CODA9_JPEG_FORMAT_420] = 16,
  148. [CODA9_JPEG_FORMAT_422] = 16,
  149. [CODA9_JPEG_FORMAT_224] = 8,
  150. [CODA9_JPEG_FORMAT_444] = 8,
  151. [CODA9_JPEG_FORMAT_400] = 8,
  152. };
  153. static const unsigned char height_align[] = {
  154. [CODA9_JPEG_FORMAT_420] = 16,
  155. [CODA9_JPEG_FORMAT_422] = 8,
  156. [CODA9_JPEG_FORMAT_224] = 16,
  157. [CODA9_JPEG_FORMAT_444] = 8,
  158. [CODA9_JPEG_FORMAT_400] = 8,
  159. };
  160. static int coda9_jpeg_chroma_format(u32 pixfmt)
  161. {
  162. switch (pixfmt) {
  163. case V4L2_PIX_FMT_YUV420:
  164. case V4L2_PIX_FMT_NV12:
  165. return CODA9_JPEG_FORMAT_420;
  166. case V4L2_PIX_FMT_YUV422P:
  167. return CODA9_JPEG_FORMAT_422;
  168. case V4L2_PIX_FMT_YUV444:
  169. return CODA9_JPEG_FORMAT_444;
  170. case V4L2_PIX_FMT_GREY:
  171. return CODA9_JPEG_FORMAT_400;
  172. }
  173. return -EINVAL;
  174. }
  175. struct coda_memcpy_desc {
  176. int offset;
  177. const void *src;
  178. size_t len;
  179. };
  180. static void coda_memcpy_parabuf(void *parabuf,
  181. const struct coda_memcpy_desc *desc)
  182. {
  183. u32 *dst = parabuf + desc->offset;
  184. const u32 *src = desc->src;
  185. int len = desc->len / 4;
  186. int i;
  187. for (i = 0; i < len; i += 2) {
  188. dst[i + 1] = swab32(src[i]);
  189. dst[i] = swab32(src[i + 1]);
  190. }
  191. }
  192. int coda_jpeg_write_tables(struct coda_ctx *ctx)
  193. {
  194. int i;
  195. static const struct coda_memcpy_desc huff[8] = {
  196. { 0, luma_dc, sizeof(luma_dc) },
  197. { 32, luma_ac, sizeof(luma_ac) },
  198. { 216, chroma_dc, sizeof(chroma_dc) },
  199. { 248, chroma_ac, sizeof(chroma_ac) },
  200. };
  201. struct coda_memcpy_desc qmat[3] = {
  202. { 512, ctx->params.jpeg_qmat_tab[0], 64 },
  203. { 576, ctx->params.jpeg_qmat_tab[1], 64 },
  204. { 640, ctx->params.jpeg_qmat_tab[1], 64 },
  205. };
  206. /* Write huffman tables to parameter memory */
  207. for (i = 0; i < ARRAY_SIZE(huff); i++)
  208. coda_memcpy_parabuf(ctx->parabuf.vaddr, huff + i);
  209. /* Write Q-matrix to parameter memory */
  210. for (i = 0; i < ARRAY_SIZE(qmat); i++)
  211. coda_memcpy_parabuf(ctx->parabuf.vaddr, qmat + i);
  212. return 0;
  213. }
  214. bool coda_jpeg_check_buffer(struct coda_ctx *ctx, struct vb2_buffer *vb)
  215. {
  216. void *vaddr = vb2_plane_vaddr(vb, 0);
  217. u16 soi, eoi;
  218. int len, i;
  219. soi = be16_to_cpup((__be16 *)vaddr);
  220. if (soi != SOI_MARKER)
  221. return false;
  222. len = vb2_get_plane_payload(vb, 0);
  223. vaddr += len - 2;
  224. for (i = 0; i < 32; i++) {
  225. eoi = be16_to_cpup((__be16 *)(vaddr - i));
  226. if (eoi == EOI_MARKER) {
  227. if (i > 0)
  228. vb2_set_plane_payload(vb, 0, len - i);
  229. return true;
  230. }
  231. }
  232. return false;
  233. }
  234. static int coda9_jpeg_gen_dec_huff_tab(struct coda_ctx *ctx, int tab_num);
  235. int coda_jpeg_decode_header(struct coda_ctx *ctx, struct vb2_buffer *vb)
  236. {
  237. struct coda_dev *dev = ctx->dev;
  238. u8 *buf = vb2_plane_vaddr(vb, 0);
  239. size_t len = vb2_get_plane_payload(vb, 0);
  240. struct v4l2_jpeg_scan_header scan_header;
  241. struct v4l2_jpeg_reference quantization_tables[4] = { };
  242. struct v4l2_jpeg_reference huffman_tables[4] = { };
  243. struct v4l2_jpeg_header header = {
  244. .scan = &scan_header,
  245. .quantization_tables = quantization_tables,
  246. .huffman_tables = huffman_tables,
  247. };
  248. struct coda_q_data *q_data_src;
  249. struct coda_huff_tab *huff_tab;
  250. int i, j, ret;
  251. ret = v4l2_jpeg_parse_header(buf, len, &header);
  252. if (ret < 0) {
  253. v4l2_err(&dev->v4l2_dev, "failed to parse JPEG header: %pe\n",
  254. ERR_PTR(ret));
  255. return ret;
  256. }
  257. ctx->params.jpeg_restart_interval = header.restart_interval;
  258. /* check frame header */
  259. if (header.frame.height > ctx->codec->max_h ||
  260. header.frame.width > ctx->codec->max_w) {
  261. v4l2_err(&dev->v4l2_dev, "invalid dimensions: %dx%d\n",
  262. header.frame.width, header.frame.height);
  263. return -EINVAL;
  264. }
  265. q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  266. if (header.frame.height != q_data_src->height ||
  267. header.frame.width != q_data_src->width) {
  268. v4l2_err(&dev->v4l2_dev,
  269. "dimensions don't match format: %dx%d\n",
  270. header.frame.width, header.frame.height);
  271. return -EINVAL;
  272. }
  273. if (header.frame.num_components != 3) {
  274. v4l2_err(&dev->v4l2_dev,
  275. "unsupported number of components: %d\n",
  276. header.frame.num_components);
  277. return -EINVAL;
  278. }
  279. /* install quantization tables */
  280. if (quantization_tables[3].start) {
  281. v4l2_err(&dev->v4l2_dev,
  282. "only 3 quantization tables supported\n");
  283. return -EINVAL;
  284. }
  285. for (i = 0; i < 3; i++) {
  286. if (!quantization_tables[i].start)
  287. continue;
  288. if (quantization_tables[i].length != 64) {
  289. v4l2_err(&dev->v4l2_dev,
  290. "only 8-bit quantization tables supported\n");
  291. continue;
  292. }
  293. if (!ctx->params.jpeg_qmat_tab[i]) {
  294. ctx->params.jpeg_qmat_tab[i] = kmalloc(64, GFP_KERNEL);
  295. if (!ctx->params.jpeg_qmat_tab[i])
  296. return -ENOMEM;
  297. }
  298. memcpy(ctx->params.jpeg_qmat_tab[i],
  299. quantization_tables[i].start, 64);
  300. }
  301. /* install Huffman tables */
  302. for (i = 0; i < 4; i++) {
  303. if (!huffman_tables[i].start) {
  304. v4l2_err(&dev->v4l2_dev, "missing Huffman table\n");
  305. return -EINVAL;
  306. }
  307. /* AC tables should be between 17 -> 178, DC between 17 -> 28 */
  308. if (huffman_tables[i].length < 17 ||
  309. huffman_tables[i].length > 178 ||
  310. ((i & 2) == 0 && huffman_tables[i].length > 28)) {
  311. v4l2_err(&dev->v4l2_dev,
  312. "invalid Huffman table %d length: %zu\n",
  313. i, huffman_tables[i].length);
  314. return -EINVAL;
  315. }
  316. }
  317. huff_tab = ctx->params.jpeg_huff_tab;
  318. if (!huff_tab) {
  319. huff_tab = kzalloc(sizeof(struct coda_huff_tab), GFP_KERNEL);
  320. if (!huff_tab)
  321. return -ENOMEM;
  322. ctx->params.jpeg_huff_tab = huff_tab;
  323. }
  324. memset(huff_tab, 0, sizeof(*huff_tab));
  325. memcpy(huff_tab->luma_dc, huffman_tables[0].start, huffman_tables[0].length);
  326. memcpy(huff_tab->chroma_dc, huffman_tables[1].start, huffman_tables[1].length);
  327. memcpy(huff_tab->luma_ac, huffman_tables[2].start, huffman_tables[2].length);
  328. memcpy(huff_tab->chroma_ac, huffman_tables[3].start, huffman_tables[3].length);
  329. /* check scan header */
  330. for (i = 0; i < scan_header.num_components; i++) {
  331. struct v4l2_jpeg_scan_component_spec *scan_component;
  332. scan_component = &scan_header.component[i];
  333. for (j = 0; j < header.frame.num_components; j++) {
  334. if (header.frame.component[j].component_identifier ==
  335. scan_component->component_selector)
  336. break;
  337. }
  338. if (j == header.frame.num_components)
  339. continue;
  340. ctx->params.jpeg_huff_dc_index[j] =
  341. scan_component->dc_entropy_coding_table_selector;
  342. ctx->params.jpeg_huff_ac_index[j] =
  343. scan_component->ac_entropy_coding_table_selector;
  344. }
  345. /* Generate Huffman table information */
  346. for (i = 0; i < 4; i++)
  347. coda9_jpeg_gen_dec_huff_tab(ctx, i);
  348. /* start of entropy coded segment */
  349. ctx->jpeg_ecs_offset = header.ecs_offset;
  350. switch (header.frame.subsampling) {
  351. case V4L2_JPEG_CHROMA_SUBSAMPLING_420:
  352. case V4L2_JPEG_CHROMA_SUBSAMPLING_422:
  353. ctx->params.jpeg_chroma_subsampling = header.frame.subsampling;
  354. break;
  355. default:
  356. v4l2_err(&dev->v4l2_dev, "chroma subsampling not supported: %d",
  357. header.frame.subsampling);
  358. return -EINVAL;
  359. }
  360. return 0;
  361. }
  362. static inline void coda9_jpeg_write_huff_values(struct coda_dev *dev, u8 *bits,
  363. int num_values)
  364. {
  365. s8 *values = (s8 *)(bits + 16);
  366. int huff_length, i;
  367. for (huff_length = 0, i = 0; i < 16; i++)
  368. huff_length += bits[i];
  369. for (i = huff_length; i < num_values; i++)
  370. values[i] = -1;
  371. for (i = 0; i < num_values; i++)
  372. coda_write(dev, (s32)values[i], CODA9_REG_JPEG_HUFF_DATA);
  373. }
  374. static void coda9_jpeg_dec_huff_setup(struct coda_ctx *ctx)
  375. {
  376. struct coda_huff_tab *huff_tab = ctx->params.jpeg_huff_tab;
  377. struct coda_dev *dev = ctx->dev;
  378. s16 *huff_min = huff_tab->min;
  379. s16 *huff_max = huff_tab->max;
  380. s8 *huff_ptr = huff_tab->ptr;
  381. int i;
  382. /* MIN Tables */
  383. coda_write(dev, 0x003, CODA9_REG_JPEG_HUFF_CTRL);
  384. coda_write(dev, 0x000, CODA9_REG_JPEG_HUFF_ADDR);
  385. for (i = 0; i < 4 * 16; i++)
  386. coda_write(dev, (s32)huff_min[i], CODA9_REG_JPEG_HUFF_DATA);
  387. /* MAX Tables */
  388. coda_write(dev, 0x403, CODA9_REG_JPEG_HUFF_CTRL);
  389. coda_write(dev, 0x440, CODA9_REG_JPEG_HUFF_ADDR);
  390. for (i = 0; i < 4 * 16; i++)
  391. coda_write(dev, (s32)huff_max[i], CODA9_REG_JPEG_HUFF_DATA);
  392. /* PTR Tables */
  393. coda_write(dev, 0x803, CODA9_REG_JPEG_HUFF_CTRL);
  394. coda_write(dev, 0x880, CODA9_REG_JPEG_HUFF_ADDR);
  395. for (i = 0; i < 4 * 16; i++)
  396. coda_write(dev, (s32)huff_ptr[i], CODA9_REG_JPEG_HUFF_DATA);
  397. /* VAL Tables: DC Luma, DC Chroma, AC Luma, AC Chroma */
  398. coda_write(dev, 0xc03, CODA9_REG_JPEG_HUFF_CTRL);
  399. coda9_jpeg_write_huff_values(dev, huff_tab->luma_dc, 12);
  400. coda9_jpeg_write_huff_values(dev, huff_tab->chroma_dc, 12);
  401. coda9_jpeg_write_huff_values(dev, huff_tab->luma_ac, 162);
  402. coda9_jpeg_write_huff_values(dev, huff_tab->chroma_ac, 162);
  403. coda_write(dev, 0x000, CODA9_REG_JPEG_HUFF_CTRL);
  404. }
  405. static inline void coda9_jpeg_write_qmat_tab(struct coda_dev *dev,
  406. u8 *qmat, int index)
  407. {
  408. int i;
  409. coda_write(dev, index | 0x3, CODA9_REG_JPEG_QMAT_CTRL);
  410. for (i = 0; i < 64; i++)
  411. coda_write(dev, qmat[i], CODA9_REG_JPEG_QMAT_DATA);
  412. coda_write(dev, 0, CODA9_REG_JPEG_QMAT_CTRL);
  413. }
  414. static void coda9_jpeg_qmat_setup(struct coda_ctx *ctx)
  415. {
  416. struct coda_dev *dev = ctx->dev;
  417. int *qmat_index = ctx->params.jpeg_qmat_index;
  418. u8 **qmat_tab = ctx->params.jpeg_qmat_tab;
  419. coda9_jpeg_write_qmat_tab(dev, qmat_tab[qmat_index[0]], 0x00);
  420. coda9_jpeg_write_qmat_tab(dev, qmat_tab[qmat_index[1]], 0x40);
  421. coda9_jpeg_write_qmat_tab(dev, qmat_tab[qmat_index[2]], 0x80);
  422. }
  423. static void coda9_jpeg_dec_bbc_gbu_setup(struct coda_ctx *ctx,
  424. struct vb2_buffer *buf, u32 ecs_offset)
  425. {
  426. struct coda_dev *dev = ctx->dev;
  427. int page_ptr, word_ptr, bit_ptr;
  428. u32 bbc_base_addr, end_addr;
  429. int bbc_cur_pos;
  430. int ret, val;
  431. bbc_base_addr = vb2_dma_contig_plane_dma_addr(buf, 0);
  432. end_addr = bbc_base_addr + vb2_get_plane_payload(buf, 0);
  433. page_ptr = ecs_offset / 256;
  434. word_ptr = (ecs_offset % 256) / 4;
  435. if (page_ptr & 1)
  436. word_ptr += 64;
  437. bit_ptr = (ecs_offset % 4) * 8;
  438. if (word_ptr & 1)
  439. bit_ptr += 32;
  440. word_ptr &= ~0x1;
  441. coda_write(dev, end_addr, CODA9_REG_JPEG_BBC_WR_PTR);
  442. coda_write(dev, bbc_base_addr, CODA9_REG_JPEG_BBC_BAS_ADDR);
  443. /* Leave 3 256-byte page margin to avoid a BBC interrupt */
  444. coda_write(dev, end_addr + 256 * 3 + 256, CODA9_REG_JPEG_BBC_END_ADDR);
  445. val = DIV_ROUND_UP(vb2_plane_size(buf, 0), 256) + 3;
  446. coda_write(dev, BIT(31) | val, CODA9_REG_JPEG_BBC_STRM_CTRL);
  447. bbc_cur_pos = page_ptr;
  448. coda_write(dev, bbc_cur_pos, CODA9_REG_JPEG_BBC_CUR_POS);
  449. coda_write(dev, bbc_base_addr + (bbc_cur_pos << 8),
  450. CODA9_REG_JPEG_BBC_EXT_ADDR);
  451. coda_write(dev, (bbc_cur_pos & 1) << 6, CODA9_REG_JPEG_BBC_INT_ADDR);
  452. coda_write(dev, 64, CODA9_REG_JPEG_BBC_DATA_CNT);
  453. coda_write(dev, 0, CODA9_REG_JPEG_BBC_COMMAND);
  454. do {
  455. ret = coda_read(dev, CODA9_REG_JPEG_BBC_BUSY);
  456. } while (ret == 1);
  457. bbc_cur_pos++;
  458. coda_write(dev, bbc_cur_pos, CODA9_REG_JPEG_BBC_CUR_POS);
  459. coda_write(dev, bbc_base_addr + (bbc_cur_pos << 8),
  460. CODA9_REG_JPEG_BBC_EXT_ADDR);
  461. coda_write(dev, (bbc_cur_pos & 1) << 6, CODA9_REG_JPEG_BBC_INT_ADDR);
  462. coda_write(dev, 64, CODA9_REG_JPEG_BBC_DATA_CNT);
  463. coda_write(dev, 0, CODA9_REG_JPEG_BBC_COMMAND);
  464. do {
  465. ret = coda_read(dev, CODA9_REG_JPEG_BBC_BUSY);
  466. } while (ret == 1);
  467. bbc_cur_pos++;
  468. coda_write(dev, bbc_cur_pos, CODA9_REG_JPEG_BBC_CUR_POS);
  469. coda_write(dev, 1, CODA9_REG_JPEG_BBC_CTRL);
  470. coda_write(dev, 0, CODA9_REG_JPEG_GBU_TT_CNT);
  471. coda_write(dev, word_ptr, CODA9_REG_JPEG_GBU_WD_PTR);
  472. coda_write(dev, 0, CODA9_REG_JPEG_GBU_BBSR);
  473. coda_write(dev, 127, CODA9_REG_JPEG_GBU_BBER);
  474. if (page_ptr & 1) {
  475. coda_write(dev, 0, CODA9_REG_JPEG_GBU_BBIR);
  476. coda_write(dev, 0, CODA9_REG_JPEG_GBU_BBHR);
  477. } else {
  478. coda_write(dev, 64, CODA9_REG_JPEG_GBU_BBIR);
  479. coda_write(dev, 64, CODA9_REG_JPEG_GBU_BBHR);
  480. }
  481. coda_write(dev, 4, CODA9_REG_JPEG_GBU_CTRL);
  482. coda_write(dev, bit_ptr, CODA9_REG_JPEG_GBU_FF_RPTR);
  483. coda_write(dev, 3, CODA9_REG_JPEG_GBU_CTRL);
  484. }
  485. static const int bus_req_num[] = {
  486. [CODA9_JPEG_FORMAT_420] = 2,
  487. [CODA9_JPEG_FORMAT_422] = 3,
  488. [CODA9_JPEG_FORMAT_224] = 3,
  489. [CODA9_JPEG_FORMAT_444] = 4,
  490. [CODA9_JPEG_FORMAT_400] = 4,
  491. };
  492. #define MCU_INFO(mcu_block_num, comp_num, comp0_info, comp1_info, comp2_info) \
  493. (((mcu_block_num) << CODA9_JPEG_MCU_BLOCK_NUM_OFFSET) | \
  494. ((comp_num) << CODA9_JPEG_COMP_NUM_OFFSET) | \
  495. ((comp0_info) << CODA9_JPEG_COMP0_INFO_OFFSET) | \
  496. ((comp1_info) << CODA9_JPEG_COMP1_INFO_OFFSET) | \
  497. ((comp2_info) << CODA9_JPEG_COMP2_INFO_OFFSET))
  498. static const u32 mcu_info[] = {
  499. [CODA9_JPEG_FORMAT_420] = MCU_INFO(6, 3, 10, 5, 5),
  500. [CODA9_JPEG_FORMAT_422] = MCU_INFO(4, 3, 9, 5, 5),
  501. [CODA9_JPEG_FORMAT_224] = MCU_INFO(4, 3, 6, 5, 5),
  502. [CODA9_JPEG_FORMAT_444] = MCU_INFO(3, 3, 5, 5, 5),
  503. [CODA9_JPEG_FORMAT_400] = MCU_INFO(1, 1, 5, 0, 0),
  504. };
  505. /*
  506. * Convert Huffman table specifcations to tables of codes and code lengths.
  507. * For reference, see JPEG ITU-T.81 (ISO/IEC 10918-1) [1]
  508. *
  509. * [1] https://www.w3.org/Graphics/JPEG/itu-t81.pdf
  510. */
  511. static int coda9_jpeg_gen_enc_huff_tab(struct coda_ctx *ctx, int tab_num,
  512. int *ehufsi, int *ehufco)
  513. {
  514. int i, j, k, lastk, si, code, maxsymbol;
  515. const u8 *bits, *huffval;
  516. struct {
  517. int size[256];
  518. int code[256];
  519. } *huff;
  520. static const unsigned char *huff_tabs[4] = {
  521. luma_dc, luma_ac, chroma_dc, chroma_ac,
  522. };
  523. int ret = -EINVAL;
  524. huff = kzalloc(sizeof(*huff), GFP_KERNEL);
  525. if (!huff)
  526. return -ENOMEM;
  527. bits = huff_tabs[tab_num];
  528. huffval = huff_tabs[tab_num] + 16;
  529. maxsymbol = tab_num & 1 ? 256 : 16;
  530. /* Figure C.1 - Generation of table of Huffman code sizes */
  531. k = 0;
  532. for (i = 1; i <= 16; i++) {
  533. j = bits[i - 1];
  534. if (k + j > maxsymbol)
  535. goto out;
  536. while (j--)
  537. huff->size[k++] = i;
  538. }
  539. lastk = k;
  540. /* Figure C.2 - Generation of table of Huffman codes */
  541. k = 0;
  542. code = 0;
  543. si = huff->size[0];
  544. while (k < lastk) {
  545. while (huff->size[k] == si) {
  546. huff->code[k++] = code;
  547. code++;
  548. }
  549. if (code >= (1 << si))
  550. goto out;
  551. code <<= 1;
  552. si++;
  553. }
  554. /* Figure C.3 - Ordering procedure for encoding procedure code tables */
  555. for (k = 0; k < lastk; k++) {
  556. i = huffval[k];
  557. if (i >= maxsymbol || ehufsi[i])
  558. goto out;
  559. ehufco[i] = huff->code[k];
  560. ehufsi[i] = huff->size[k];
  561. }
  562. ret = 0;
  563. out:
  564. kfree(huff);
  565. return ret;
  566. }
  567. #define DC_TABLE_INDEX0 0
  568. #define AC_TABLE_INDEX0 1
  569. #define DC_TABLE_INDEX1 2
  570. #define AC_TABLE_INDEX1 3
  571. static u8 *coda9_jpeg_get_huff_bits(struct coda_ctx *ctx, int tab_num)
  572. {
  573. struct coda_huff_tab *huff_tab = ctx->params.jpeg_huff_tab;
  574. if (!huff_tab)
  575. return NULL;
  576. switch (tab_num) {
  577. case DC_TABLE_INDEX0: return huff_tab->luma_dc;
  578. case AC_TABLE_INDEX0: return huff_tab->luma_ac;
  579. case DC_TABLE_INDEX1: return huff_tab->chroma_dc;
  580. case AC_TABLE_INDEX1: return huff_tab->chroma_ac;
  581. }
  582. return NULL;
  583. }
  584. static int coda9_jpeg_gen_dec_huff_tab(struct coda_ctx *ctx, int tab_num)
  585. {
  586. int ptr_cnt = 0, huff_code = 0, zero_flag = 0, data_flag = 0;
  587. u8 *huff_bits;
  588. s16 *huff_max;
  589. s16 *huff_min;
  590. s8 *huff_ptr;
  591. int ofs;
  592. int i;
  593. huff_bits = coda9_jpeg_get_huff_bits(ctx, tab_num);
  594. if (!huff_bits)
  595. return -EINVAL;
  596. /* DC/AC Luma, DC/AC Chroma -> DC Luma/Chroma, AC Luma/Chroma */
  597. ofs = ((tab_num & 1) << 1) | ((tab_num >> 1) & 1);
  598. ofs *= 16;
  599. huff_ptr = ctx->params.jpeg_huff_tab->ptr + ofs;
  600. huff_max = ctx->params.jpeg_huff_tab->max + ofs;
  601. huff_min = ctx->params.jpeg_huff_tab->min + ofs;
  602. for (i = 0; i < 16; i++) {
  603. if (huff_bits[i]) {
  604. huff_ptr[i] = ptr_cnt;
  605. ptr_cnt += huff_bits[i];
  606. huff_min[i] = huff_code;
  607. huff_max[i] = huff_code + (huff_bits[i] - 1);
  608. data_flag = 1;
  609. zero_flag = 0;
  610. } else {
  611. huff_ptr[i] = -1;
  612. huff_min[i] = -1;
  613. huff_max[i] = -1;
  614. zero_flag = 1;
  615. }
  616. if (data_flag == 1) {
  617. if (zero_flag == 1)
  618. huff_code <<= 1;
  619. else
  620. huff_code = (huff_max[i] + 1) << 1;
  621. }
  622. }
  623. return 0;
  624. }
  625. static int coda9_jpeg_load_huff_tab(struct coda_ctx *ctx)
  626. {
  627. struct {
  628. int size[4][256];
  629. int code[4][256];
  630. } *huff;
  631. u32 *huff_data;
  632. int i, j;
  633. int ret;
  634. huff = kzalloc(sizeof(*huff), GFP_KERNEL);
  635. if (!huff)
  636. return -ENOMEM;
  637. /* Generate all four (luma/chroma DC/AC) code/size lookup tables */
  638. for (i = 0; i < 4; i++) {
  639. ret = coda9_jpeg_gen_enc_huff_tab(ctx, i, huff->size[i],
  640. huff->code[i]);
  641. if (ret)
  642. goto out;
  643. }
  644. if (!ctx->params.jpeg_huff_data) {
  645. ctx->params.jpeg_huff_data =
  646. kzalloc(sizeof(u32) * CODA9_JPEG_ENC_HUFF_DATA_SIZE,
  647. GFP_KERNEL);
  648. if (!ctx->params.jpeg_huff_data) {
  649. ret = -ENOMEM;
  650. goto out;
  651. }
  652. }
  653. huff_data = ctx->params.jpeg_huff_data;
  654. for (j = 0; j < 4; j++) {
  655. /* Store Huffman lookup tables in AC0, AC1, DC0, DC1 order */
  656. int t = (j == 0) ? AC_TABLE_INDEX0 :
  657. (j == 1) ? AC_TABLE_INDEX1 :
  658. (j == 2) ? DC_TABLE_INDEX0 :
  659. DC_TABLE_INDEX1;
  660. /* DC tables only have 16 entries */
  661. int len = (j < 2) ? 256 : 16;
  662. for (i = 0; i < len; i++) {
  663. if (huff->size[t][i] == 0 && huff->code[t][i] == 0)
  664. *(huff_data++) = 0;
  665. else
  666. *(huff_data++) =
  667. ((huff->size[t][i] - 1) << 16) |
  668. huff->code[t][i];
  669. }
  670. }
  671. ret = 0;
  672. out:
  673. kfree(huff);
  674. return ret;
  675. }
  676. static void coda9_jpeg_write_huff_tab(struct coda_ctx *ctx)
  677. {
  678. struct coda_dev *dev = ctx->dev;
  679. u32 *huff_data = ctx->params.jpeg_huff_data;
  680. int i;
  681. /* Write Huffman size/code lookup tables in AC0, AC1, DC0, DC1 order */
  682. coda_write(dev, 0x3, CODA9_REG_JPEG_HUFF_CTRL);
  683. for (i = 0; i < CODA9_JPEG_ENC_HUFF_DATA_SIZE; i++)
  684. coda_write(dev, *(huff_data++), CODA9_REG_JPEG_HUFF_DATA);
  685. coda_write(dev, 0x0, CODA9_REG_JPEG_HUFF_CTRL);
  686. }
  687. static inline void coda9_jpeg_write_qmat_quotients(struct coda_dev *dev,
  688. u8 *qmat, int index)
  689. {
  690. int i;
  691. coda_write(dev, index | 0x3, CODA9_REG_JPEG_QMAT_CTRL);
  692. for (i = 0; i < 64; i++)
  693. coda_write(dev, 0x80000 / qmat[i], CODA9_REG_JPEG_QMAT_DATA);
  694. coda_write(dev, index, CODA9_REG_JPEG_QMAT_CTRL);
  695. }
  696. static void coda9_jpeg_load_qmat_tab(struct coda_ctx *ctx)
  697. {
  698. struct coda_dev *dev = ctx->dev;
  699. u8 *luma_tab;
  700. u8 *chroma_tab;
  701. luma_tab = ctx->params.jpeg_qmat_tab[0];
  702. if (!luma_tab)
  703. luma_tab = luma_q;
  704. chroma_tab = ctx->params.jpeg_qmat_tab[1];
  705. if (!chroma_tab)
  706. chroma_tab = chroma_q;
  707. coda9_jpeg_write_qmat_quotients(dev, luma_tab, 0x00);
  708. coda9_jpeg_write_qmat_quotients(dev, chroma_tab, 0x40);
  709. coda9_jpeg_write_qmat_quotients(dev, chroma_tab, 0x80);
  710. }
  711. struct coda_jpeg_stream {
  712. u8 *curr;
  713. u8 *end;
  714. };
  715. static inline int coda_jpeg_put_byte(u8 byte, struct coda_jpeg_stream *stream)
  716. {
  717. if (stream->curr >= stream->end)
  718. return -EINVAL;
  719. *stream->curr++ = byte;
  720. return 0;
  721. }
  722. static inline int coda_jpeg_put_word(u16 word, struct coda_jpeg_stream *stream)
  723. {
  724. if (stream->curr + sizeof(__be16) > stream->end)
  725. return -EINVAL;
  726. put_unaligned_be16(word, stream->curr);
  727. stream->curr += sizeof(__be16);
  728. return 0;
  729. }
  730. static int coda_jpeg_put_table(u16 marker, u8 index, const u8 *table,
  731. size_t len, struct coda_jpeg_stream *stream)
  732. {
  733. int i, ret;
  734. ret = coda_jpeg_put_word(marker, stream);
  735. if (ret < 0)
  736. return ret;
  737. ret = coda_jpeg_put_word(3 + len, stream);
  738. if (ret < 0)
  739. return ret;
  740. ret = coda_jpeg_put_byte(index, stream);
  741. for (i = 0; i < len && ret == 0; i++)
  742. ret = coda_jpeg_put_byte(table[i], stream);
  743. return ret;
  744. }
  745. static int coda_jpeg_define_quantization_table(struct coda_ctx *ctx, u8 index,
  746. struct coda_jpeg_stream *stream)
  747. {
  748. return coda_jpeg_put_table(DQT_MARKER, index,
  749. ctx->params.jpeg_qmat_tab[index], 64,
  750. stream);
  751. }
  752. static int coda_jpeg_define_huffman_table(u8 index, const u8 *table, size_t len,
  753. struct coda_jpeg_stream *stream)
  754. {
  755. return coda_jpeg_put_table(DHT_MARKER, index, table, len, stream);
  756. }
  757. static int coda9_jpeg_encode_header(struct coda_ctx *ctx, int len, u8 *buf)
  758. {
  759. struct coda_jpeg_stream stream = { buf, buf + len };
  760. struct coda_q_data *q_data_src;
  761. int chroma_format, comp_num;
  762. int i, ret, pad;
  763. q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  764. chroma_format = coda9_jpeg_chroma_format(q_data_src->fourcc);
  765. if (chroma_format < 0)
  766. return 0;
  767. /* Start Of Image */
  768. ret = coda_jpeg_put_word(SOI_MARKER, &stream);
  769. if (ret < 0)
  770. return ret;
  771. /* Define Restart Interval */
  772. if (ctx->params.jpeg_restart_interval) {
  773. ret = coda_jpeg_put_word(DRI_MARKER, &stream);
  774. if (ret < 0)
  775. return ret;
  776. ret = coda_jpeg_put_word(4, &stream);
  777. if (ret < 0)
  778. return ret;
  779. ret = coda_jpeg_put_word(ctx->params.jpeg_restart_interval,
  780. &stream);
  781. if (ret < 0)
  782. return ret;
  783. }
  784. /* Define Quantization Tables */
  785. ret = coda_jpeg_define_quantization_table(ctx, 0x00, &stream);
  786. if (ret < 0)
  787. return ret;
  788. if (chroma_format != CODA9_JPEG_FORMAT_400) {
  789. ret = coda_jpeg_define_quantization_table(ctx, 0x01, &stream);
  790. if (ret < 0)
  791. return ret;
  792. }
  793. /* Define Huffman Tables */
  794. ret = coda_jpeg_define_huffman_table(0x00, luma_dc, 16 + 12, &stream);
  795. if (ret < 0)
  796. return ret;
  797. ret = coda_jpeg_define_huffman_table(0x10, luma_ac, 16 + 162, &stream);
  798. if (ret < 0)
  799. return ret;
  800. if (chroma_format != CODA9_JPEG_FORMAT_400) {
  801. ret = coda_jpeg_define_huffman_table(0x01, chroma_dc, 16 + 12,
  802. &stream);
  803. if (ret < 0)
  804. return ret;
  805. ret = coda_jpeg_define_huffman_table(0x11, chroma_ac, 16 + 162,
  806. &stream);
  807. if (ret < 0)
  808. return ret;
  809. }
  810. /* Start Of Frame */
  811. ret = coda_jpeg_put_word(SOF_MARKER, &stream);
  812. if (ret < 0)
  813. return ret;
  814. comp_num = (chroma_format == CODA9_JPEG_FORMAT_400) ? 1 : 3;
  815. ret = coda_jpeg_put_word(8 + comp_num * 3, &stream);
  816. if (ret < 0)
  817. return ret;
  818. ret = coda_jpeg_put_byte(0x08, &stream);
  819. if (ret < 0)
  820. return ret;
  821. ret = coda_jpeg_put_word(q_data_src->height, &stream);
  822. if (ret < 0)
  823. return ret;
  824. ret = coda_jpeg_put_word(q_data_src->width, &stream);
  825. if (ret < 0)
  826. return ret;
  827. ret = coda_jpeg_put_byte(comp_num, &stream);
  828. if (ret < 0)
  829. return ret;
  830. for (i = 0; i < comp_num; i++) {
  831. static unsigned char subsampling[5][3] = {
  832. [CODA9_JPEG_FORMAT_420] = { 0x22, 0x11, 0x11 },
  833. [CODA9_JPEG_FORMAT_422] = { 0x21, 0x11, 0x11 },
  834. [CODA9_JPEG_FORMAT_224] = { 0x12, 0x11, 0x11 },
  835. [CODA9_JPEG_FORMAT_444] = { 0x11, 0x11, 0x11 },
  836. [CODA9_JPEG_FORMAT_400] = { 0x11 },
  837. };
  838. /* Component identifier, matches SOS */
  839. ret = coda_jpeg_put_byte(i + 1, &stream);
  840. if (ret < 0)
  841. return ret;
  842. ret = coda_jpeg_put_byte(subsampling[chroma_format][i],
  843. &stream);
  844. if (ret < 0)
  845. return ret;
  846. /* Chroma table index */
  847. ret = coda_jpeg_put_byte((i == 0) ? 0 : 1, &stream);
  848. if (ret < 0)
  849. return ret;
  850. }
  851. /* Pad to multiple of 8 bytes */
  852. pad = (stream.curr - buf) % 8;
  853. if (pad) {
  854. pad = 8 - pad;
  855. while (pad--) {
  856. ret = coda_jpeg_put_byte(0x00, &stream);
  857. if (ret < 0)
  858. return ret;
  859. }
  860. }
  861. return stream.curr - buf;
  862. }
  863. /*
  864. * Scale quantization table using nonlinear scaling factor
  865. * u8 qtab[64], scale [50,190]
  866. */
  867. static void coda_scale_quant_table(u8 *q_tab, int scale)
  868. {
  869. unsigned int temp;
  870. int i;
  871. for (i = 0; i < 64; i++) {
  872. temp = DIV_ROUND_CLOSEST((unsigned int)q_tab[i] * scale, 100);
  873. if (temp <= 0)
  874. temp = 1;
  875. if (temp > 255)
  876. temp = 255;
  877. q_tab[i] = (unsigned char)temp;
  878. }
  879. }
  880. void coda_set_jpeg_compression_quality(struct coda_ctx *ctx, int quality)
  881. {
  882. unsigned int scale;
  883. ctx->params.jpeg_quality = quality;
  884. /* Clip quality setting to [5,100] interval */
  885. if (quality > 100)
  886. quality = 100;
  887. if (quality < 5)
  888. quality = 5;
  889. /*
  890. * Non-linear scaling factor:
  891. * [5,50] -> [1000..100], [51,100] -> [98..0]
  892. */
  893. if (quality < 50)
  894. scale = 5000 / quality;
  895. else
  896. scale = 200 - 2 * quality;
  897. if (ctx->params.jpeg_qmat_tab[0]) {
  898. memcpy(ctx->params.jpeg_qmat_tab[0], luma_q, 64);
  899. coda_scale_quant_table(ctx->params.jpeg_qmat_tab[0], scale);
  900. }
  901. if (ctx->params.jpeg_qmat_tab[1]) {
  902. memcpy(ctx->params.jpeg_qmat_tab[1], chroma_q, 64);
  903. coda_scale_quant_table(ctx->params.jpeg_qmat_tab[1], scale);
  904. }
  905. }
  906. /*
  907. * Encoder context operations
  908. */
  909. static int coda9_jpeg_start_encoding(struct coda_ctx *ctx)
  910. {
  911. struct coda_dev *dev = ctx->dev;
  912. int ret;
  913. ret = coda9_jpeg_load_huff_tab(ctx);
  914. if (ret < 0) {
  915. v4l2_err(&dev->v4l2_dev, "error loading Huffman tables\n");
  916. return ret;
  917. }
  918. if (!ctx->params.jpeg_qmat_tab[0]) {
  919. ctx->params.jpeg_qmat_tab[0] = kmalloc(64, GFP_KERNEL);
  920. if (!ctx->params.jpeg_qmat_tab[0])
  921. return -ENOMEM;
  922. }
  923. if (!ctx->params.jpeg_qmat_tab[1]) {
  924. ctx->params.jpeg_qmat_tab[1] = kmalloc(64, GFP_KERNEL);
  925. if (!ctx->params.jpeg_qmat_tab[1])
  926. return -ENOMEM;
  927. }
  928. coda_set_jpeg_compression_quality(ctx, ctx->params.jpeg_quality);
  929. return 0;
  930. }
  931. static int coda9_jpeg_prepare_encode(struct coda_ctx *ctx)
  932. {
  933. struct coda_q_data *q_data_src;
  934. struct vb2_v4l2_buffer *src_buf, *dst_buf;
  935. struct coda_dev *dev = ctx->dev;
  936. u32 start_addr, end_addr;
  937. u16 aligned_width, aligned_height;
  938. bool chroma_interleave;
  939. int chroma_format;
  940. int header_len;
  941. int ret;
  942. ktime_t timeout;
  943. src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
  944. dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
  945. q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  946. if (vb2_get_plane_payload(&src_buf->vb2_buf, 0) == 0)
  947. vb2_set_plane_payload(&src_buf->vb2_buf, 0,
  948. vb2_plane_size(&src_buf->vb2_buf, 0));
  949. src_buf->sequence = ctx->osequence;
  950. dst_buf->sequence = ctx->osequence;
  951. ctx->osequence++;
  952. src_buf->flags |= V4L2_BUF_FLAG_KEYFRAME;
  953. src_buf->flags &= ~V4L2_BUF_FLAG_PFRAME;
  954. coda_set_gdi_regs(ctx);
  955. start_addr = vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0);
  956. end_addr = start_addr + vb2_plane_size(&dst_buf->vb2_buf, 0);
  957. chroma_format = coda9_jpeg_chroma_format(q_data_src->fourcc);
  958. if (chroma_format < 0)
  959. return chroma_format;
  960. /* Round image dimensions to multiple of MCU size */
  961. aligned_width = round_up(q_data_src->width, width_align[chroma_format]);
  962. aligned_height = round_up(q_data_src->height,
  963. height_align[chroma_format]);
  964. if (aligned_width != q_data_src->bytesperline) {
  965. v4l2_err(&dev->v4l2_dev, "wrong stride: %d instead of %d\n",
  966. aligned_width, q_data_src->bytesperline);
  967. }
  968. header_len =
  969. coda9_jpeg_encode_header(ctx,
  970. vb2_plane_size(&dst_buf->vb2_buf, 0),
  971. vb2_plane_vaddr(&dst_buf->vb2_buf, 0));
  972. if (header_len < 0)
  973. return header_len;
  974. coda_write(dev, start_addr + header_len, CODA9_REG_JPEG_BBC_BAS_ADDR);
  975. coda_write(dev, end_addr, CODA9_REG_JPEG_BBC_END_ADDR);
  976. coda_write(dev, start_addr + header_len, CODA9_REG_JPEG_BBC_WR_PTR);
  977. coda_write(dev, start_addr + header_len, CODA9_REG_JPEG_BBC_RD_PTR);
  978. coda_write(dev, 0, CODA9_REG_JPEG_BBC_CUR_POS);
  979. /* 64 words per 256-byte page */
  980. coda_write(dev, 64, CODA9_REG_JPEG_BBC_DATA_CNT);
  981. coda_write(dev, start_addr, CODA9_REG_JPEG_BBC_EXT_ADDR);
  982. coda_write(dev, 0, CODA9_REG_JPEG_BBC_INT_ADDR);
  983. coda_write(dev, 0, CODA9_REG_JPEG_GBU_BT_PTR);
  984. coda_write(dev, 0, CODA9_REG_JPEG_GBU_WD_PTR);
  985. coda_write(dev, 0, CODA9_REG_JPEG_GBU_BBSR);
  986. coda_write(dev, BIT(31) | ((end_addr - start_addr - header_len) / 256),
  987. CODA9_REG_JPEG_BBC_STRM_CTRL);
  988. coda_write(dev, 0, CODA9_REG_JPEG_GBU_CTRL);
  989. coda_write(dev, 0, CODA9_REG_JPEG_GBU_FF_RPTR);
  990. coda_write(dev, 127, CODA9_REG_JPEG_GBU_BBER);
  991. coda_write(dev, 64, CODA9_REG_JPEG_GBU_BBIR);
  992. coda_write(dev, 64, CODA9_REG_JPEG_GBU_BBHR);
  993. chroma_interleave = (q_data_src->fourcc == V4L2_PIX_FMT_NV12);
  994. coda_write(dev, CODA9_JPEG_PIC_CTRL_TC_DIRECTION |
  995. CODA9_JPEG_PIC_CTRL_ENCODER_EN, CODA9_REG_JPEG_PIC_CTRL);
  996. coda_write(dev, 0, CODA9_REG_JPEG_SCL_INFO);
  997. coda_write(dev, chroma_interleave, CODA9_REG_JPEG_DPB_CONFIG);
  998. coda_write(dev, ctx->params.jpeg_restart_interval,
  999. CODA9_REG_JPEG_RST_INTVAL);
  1000. coda_write(dev, 1, CODA9_REG_JPEG_BBC_CTRL);
  1001. coda_write(dev, bus_req_num[chroma_format], CODA9_REG_JPEG_OP_INFO);
  1002. coda9_jpeg_write_huff_tab(ctx);
  1003. coda9_jpeg_load_qmat_tab(ctx);
  1004. if (ctx->params.rot_mode & CODA_ROT_90) {
  1005. aligned_width = aligned_height;
  1006. aligned_height = q_data_src->bytesperline;
  1007. if (chroma_format == CODA9_JPEG_FORMAT_422)
  1008. chroma_format = CODA9_JPEG_FORMAT_224;
  1009. else if (chroma_format == CODA9_JPEG_FORMAT_224)
  1010. chroma_format = CODA9_JPEG_FORMAT_422;
  1011. }
  1012. /* These need to be multiples of MCU size */
  1013. coda_write(dev, aligned_width << 16 | aligned_height,
  1014. CODA9_REG_JPEG_PIC_SIZE);
  1015. coda_write(dev, ctx->params.rot_mode ?
  1016. (CODA_ROT_MIR_ENABLE | ctx->params.rot_mode) : 0,
  1017. CODA9_REG_JPEG_ROT_INFO);
  1018. coda_write(dev, mcu_info[chroma_format], CODA9_REG_JPEG_MCU_INFO);
  1019. coda_write(dev, 1, CODA9_GDI_CONTROL);
  1020. timeout = ktime_add_us(ktime_get(), 100000);
  1021. do {
  1022. ret = coda_read(dev, CODA9_GDI_STATUS);
  1023. if (ktime_compare(ktime_get(), timeout) > 0) {
  1024. v4l2_err(&dev->v4l2_dev, "timeout waiting for GDI\n");
  1025. return -ETIMEDOUT;
  1026. }
  1027. } while (!ret);
  1028. coda_write(dev, (chroma_format << 17) | (chroma_interleave << 16) |
  1029. q_data_src->bytesperline, CODA9_GDI_INFO_CONTROL);
  1030. /* The content of this register seems to be irrelevant: */
  1031. coda_write(dev, aligned_width << 16 | aligned_height,
  1032. CODA9_GDI_INFO_PIC_SIZE);
  1033. coda_write_base(ctx, q_data_src, src_buf, CODA9_GDI_INFO_BASE_Y);
  1034. coda_write(dev, 0, CODA9_REG_JPEG_DPB_BASE00);
  1035. coda_write(dev, 0, CODA9_GDI_CONTROL);
  1036. coda_write(dev, 1, CODA9_GDI_PIC_INIT_HOST);
  1037. coda_write(dev, 1, CODA9_GDI_WPROT_ERR_CLR);
  1038. coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
  1039. trace_coda_jpeg_run(ctx, src_buf);
  1040. coda_write(dev, 1, CODA9_REG_JPEG_PIC_START);
  1041. return 0;
  1042. }
  1043. static void coda9_jpeg_finish_encode(struct coda_ctx *ctx)
  1044. {
  1045. struct vb2_v4l2_buffer *src_buf, *dst_buf;
  1046. struct coda_dev *dev = ctx->dev;
  1047. u32 wr_ptr, start_ptr;
  1048. u32 err_mb;
  1049. if (ctx->aborting) {
  1050. coda_write(ctx->dev, 0, CODA9_REG_JPEG_BBC_FLUSH_CMD);
  1051. return;
  1052. }
  1053. /*
  1054. * Lock to make sure that an encoder stop command running in parallel
  1055. * will either already have marked src_buf as last, or it will wake up
  1056. * the capture queue after the buffers are returned.
  1057. */
  1058. mutex_lock(&ctx->wakeup_mutex);
  1059. src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
  1060. dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
  1061. trace_coda_jpeg_done(ctx, dst_buf);
  1062. /*
  1063. * Set plane payload to the number of bytes written out
  1064. * by the JPEG processing unit
  1065. */
  1066. start_ptr = vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0);
  1067. wr_ptr = coda_read(dev, CODA9_REG_JPEG_BBC_WR_PTR);
  1068. vb2_set_plane_payload(&dst_buf->vb2_buf, 0, wr_ptr - start_ptr);
  1069. err_mb = coda_read(dev, CODA9_REG_JPEG_PIC_ERRMB);
  1070. if (err_mb)
  1071. coda_dbg(1, ctx, "ERRMB: 0x%x\n", err_mb);
  1072. coda_write(dev, 0, CODA9_REG_JPEG_BBC_FLUSH_CMD);
  1073. dst_buf->flags &= ~(V4L2_BUF_FLAG_PFRAME | V4L2_BUF_FLAG_LAST);
  1074. dst_buf->flags |= V4L2_BUF_FLAG_KEYFRAME;
  1075. dst_buf->flags |= src_buf->flags & V4L2_BUF_FLAG_LAST;
  1076. v4l2_m2m_buf_copy_metadata(src_buf, dst_buf, false);
  1077. v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
  1078. coda_m2m_buf_done(ctx, dst_buf, err_mb ? VB2_BUF_STATE_ERROR :
  1079. VB2_BUF_STATE_DONE);
  1080. mutex_unlock(&ctx->wakeup_mutex);
  1081. coda_dbg(1, ctx, "job finished: encoded frame (%u)%s\n",
  1082. dst_buf->sequence,
  1083. (dst_buf->flags & V4L2_BUF_FLAG_LAST) ? " (last)" : "");
  1084. /*
  1085. * Reset JPEG processing unit after each encode run to work
  1086. * around hangups when switching context between encoder and
  1087. * decoder.
  1088. */
  1089. coda_hw_reset(ctx);
  1090. }
  1091. static void coda9_jpeg_encode_timeout(struct coda_ctx *ctx)
  1092. {
  1093. struct coda_dev *dev = ctx->dev;
  1094. u32 end_addr, wr_ptr;
  1095. /* Handle missing BBC overflow interrupt via timeout */
  1096. end_addr = coda_read(dev, CODA9_REG_JPEG_BBC_END_ADDR);
  1097. wr_ptr = coda_read(dev, CODA9_REG_JPEG_BBC_WR_PTR);
  1098. if (wr_ptr >= end_addr - 256) {
  1099. v4l2_err(&dev->v4l2_dev, "JPEG too large for capture buffer\n");
  1100. coda9_jpeg_finish_encode(ctx);
  1101. return;
  1102. }
  1103. coda_hw_reset(ctx);
  1104. }
  1105. static void coda9_jpeg_release(struct coda_ctx *ctx)
  1106. {
  1107. int i;
  1108. if (ctx->params.jpeg_qmat_tab[0] == luma_q)
  1109. ctx->params.jpeg_qmat_tab[0] = NULL;
  1110. if (ctx->params.jpeg_qmat_tab[1] == chroma_q)
  1111. ctx->params.jpeg_qmat_tab[1] = NULL;
  1112. for (i = 0; i < 3; i++)
  1113. kfree(ctx->params.jpeg_qmat_tab[i]);
  1114. kfree(ctx->params.jpeg_huff_data);
  1115. kfree(ctx->params.jpeg_huff_tab);
  1116. }
  1117. const struct coda_context_ops coda9_jpeg_encode_ops = {
  1118. .queue_init = coda_encoder_queue_init,
  1119. .start_streaming = coda9_jpeg_start_encoding,
  1120. .prepare_run = coda9_jpeg_prepare_encode,
  1121. .finish_run = coda9_jpeg_finish_encode,
  1122. .run_timeout = coda9_jpeg_encode_timeout,
  1123. .release = coda9_jpeg_release,
  1124. };
  1125. /*
  1126. * Decoder context operations
  1127. */
  1128. static int coda9_jpeg_start_decoding(struct coda_ctx *ctx)
  1129. {
  1130. ctx->params.jpeg_qmat_index[0] = 0;
  1131. ctx->params.jpeg_qmat_index[1] = 1;
  1132. ctx->params.jpeg_qmat_index[2] = 1;
  1133. ctx->params.jpeg_qmat_tab[0] = luma_q;
  1134. ctx->params.jpeg_qmat_tab[1] = chroma_q;
  1135. /* nothing more to do here */
  1136. /* TODO: we could already scan the first header to get the chroma
  1137. * format.
  1138. */
  1139. return 0;
  1140. }
  1141. static int coda9_jpeg_prepare_decode(struct coda_ctx *ctx)
  1142. {
  1143. struct coda_dev *dev = ctx->dev;
  1144. int aligned_width, aligned_height;
  1145. int chroma_format;
  1146. int ret;
  1147. u32 val, dst_fourcc;
  1148. struct coda_q_data *q_data_src, *q_data_dst;
  1149. struct vb2_v4l2_buffer *src_buf, *dst_buf;
  1150. int chroma_interleave;
  1151. int scl_hor_mode, scl_ver_mode;
  1152. src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
  1153. dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
  1154. q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  1155. q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  1156. dst_fourcc = q_data_dst->fourcc;
  1157. scl_hor_mode = coda_jpeg_scale(q_data_src->width, q_data_dst->width);
  1158. scl_ver_mode = coda_jpeg_scale(q_data_src->height, q_data_dst->height);
  1159. if (vb2_get_plane_payload(&src_buf->vb2_buf, 0) == 0)
  1160. vb2_set_plane_payload(&src_buf->vb2_buf, 0,
  1161. vb2_plane_size(&src_buf->vb2_buf, 0));
  1162. chroma_format = coda9_jpeg_chroma_format(q_data_dst->fourcc);
  1163. if (chroma_format < 0)
  1164. return chroma_format;
  1165. ret = coda_jpeg_decode_header(ctx, &src_buf->vb2_buf);
  1166. if (ret < 0) {
  1167. src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
  1168. dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
  1169. v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
  1170. v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
  1171. return ret;
  1172. }
  1173. /* Round image dimensions to multiple of MCU size */
  1174. aligned_width = round_up(q_data_src->width, width_align[chroma_format]);
  1175. aligned_height = round_up(q_data_src->height, height_align[chroma_format]);
  1176. if (aligned_width != q_data_dst->bytesperline) {
  1177. v4l2_err(&dev->v4l2_dev, "stride mismatch: %d != %d\n",
  1178. aligned_width, q_data_dst->bytesperline);
  1179. }
  1180. coda_set_gdi_regs(ctx);
  1181. val = ctx->params.jpeg_huff_ac_index[0] << 12 |
  1182. ctx->params.jpeg_huff_ac_index[1] << 11 |
  1183. ctx->params.jpeg_huff_ac_index[2] << 10 |
  1184. ctx->params.jpeg_huff_dc_index[0] << 9 |
  1185. ctx->params.jpeg_huff_dc_index[1] << 8 |
  1186. ctx->params.jpeg_huff_dc_index[2] << 7;
  1187. if (ctx->params.jpeg_huff_tab)
  1188. val |= CODA9_JPEG_PIC_CTRL_USER_HUFFMAN_EN;
  1189. coda_write(dev, val, CODA9_REG_JPEG_PIC_CTRL);
  1190. coda_write(dev, aligned_width << 16 | aligned_height,
  1191. CODA9_REG_JPEG_PIC_SIZE);
  1192. chroma_interleave = (dst_fourcc == V4L2_PIX_FMT_NV12);
  1193. coda_write(dev, 0, CODA9_REG_JPEG_ROT_INFO);
  1194. coda_write(dev, bus_req_num[chroma_format], CODA9_REG_JPEG_OP_INFO);
  1195. coda_write(dev, mcu_info[chroma_format], CODA9_REG_JPEG_MCU_INFO);
  1196. if (scl_hor_mode || scl_ver_mode)
  1197. val = CODA9_JPEG_SCL_ENABLE | (scl_hor_mode << 2) | scl_ver_mode;
  1198. else
  1199. val = 0;
  1200. coda_write(dev, val, CODA9_REG_JPEG_SCL_INFO);
  1201. coda_write(dev, chroma_interleave, CODA9_REG_JPEG_DPB_CONFIG);
  1202. coda_write(dev, ctx->params.jpeg_restart_interval,
  1203. CODA9_REG_JPEG_RST_INTVAL);
  1204. if (ctx->params.jpeg_huff_tab)
  1205. coda9_jpeg_dec_huff_setup(ctx);
  1206. coda9_jpeg_qmat_setup(ctx);
  1207. coda9_jpeg_dec_bbc_gbu_setup(ctx, &src_buf->vb2_buf,
  1208. ctx->jpeg_ecs_offset);
  1209. coda_write(dev, 0, CODA9_REG_JPEG_RST_INDEX);
  1210. coda_write(dev, 0, CODA9_REG_JPEG_RST_COUNT);
  1211. coda_write(dev, 0, CODA9_REG_JPEG_DPCM_DIFF_Y);
  1212. coda_write(dev, 0, CODA9_REG_JPEG_DPCM_DIFF_CB);
  1213. coda_write(dev, 0, CODA9_REG_JPEG_DPCM_DIFF_CR);
  1214. coda_write(dev, 0, CODA9_REG_JPEG_ROT_INFO);
  1215. coda_write(dev, 1, CODA9_GDI_CONTROL);
  1216. do {
  1217. ret = coda_read(dev, CODA9_GDI_STATUS);
  1218. } while (!ret);
  1219. val = (chroma_format << 17) | (chroma_interleave << 16) |
  1220. q_data_dst->bytesperline;
  1221. if (ctx->tiled_map_type == GDI_TILED_FRAME_MB_RASTER_MAP)
  1222. val |= 3 << 20;
  1223. coda_write(dev, val, CODA9_GDI_INFO_CONTROL);
  1224. coda_write(dev, aligned_width << 16 | aligned_height,
  1225. CODA9_GDI_INFO_PIC_SIZE);
  1226. coda_write_base(ctx, q_data_dst, dst_buf, CODA9_GDI_INFO_BASE_Y);
  1227. coda_write(dev, 0, CODA9_REG_JPEG_DPB_BASE00);
  1228. coda_write(dev, 0, CODA9_GDI_CONTROL);
  1229. coda_write(dev, 1, CODA9_GDI_PIC_INIT_HOST);
  1230. trace_coda_jpeg_run(ctx, src_buf);
  1231. coda_write(dev, 1, CODA9_REG_JPEG_PIC_START);
  1232. return 0;
  1233. }
  1234. static void coda9_jpeg_finish_decode(struct coda_ctx *ctx)
  1235. {
  1236. struct coda_dev *dev = ctx->dev;
  1237. struct vb2_v4l2_buffer *dst_buf, *src_buf;
  1238. struct coda_q_data *q_data_dst;
  1239. u32 err_mb;
  1240. err_mb = coda_read(dev, CODA9_REG_JPEG_PIC_ERRMB);
  1241. if (err_mb)
  1242. v4l2_err(&dev->v4l2_dev, "ERRMB: 0x%x\n", err_mb);
  1243. coda_write(dev, 0, CODA9_REG_JPEG_BBC_FLUSH_CMD);
  1244. /*
  1245. * Lock to make sure that a decoder stop command running in parallel
  1246. * will either already have marked src_buf as last, or it will wake up
  1247. * the capture queue after the buffers are returned.
  1248. */
  1249. mutex_lock(&ctx->wakeup_mutex);
  1250. src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
  1251. dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
  1252. dst_buf->sequence = ctx->osequence++;
  1253. trace_coda_jpeg_done(ctx, dst_buf);
  1254. dst_buf->flags &= ~(V4L2_BUF_FLAG_PFRAME | V4L2_BUF_FLAG_LAST);
  1255. dst_buf->flags |= V4L2_BUF_FLAG_KEYFRAME;
  1256. dst_buf->flags |= src_buf->flags & V4L2_BUF_FLAG_LAST;
  1257. v4l2_m2m_buf_copy_metadata(src_buf, dst_buf, false);
  1258. q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
  1259. vb2_set_plane_payload(&dst_buf->vb2_buf, 0, q_data_dst->sizeimage);
  1260. v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
  1261. coda_m2m_buf_done(ctx, dst_buf, err_mb ? VB2_BUF_STATE_ERROR :
  1262. VB2_BUF_STATE_DONE);
  1263. mutex_unlock(&ctx->wakeup_mutex);
  1264. coda_dbg(1, ctx, "job finished: decoded frame (%u)%s\n",
  1265. dst_buf->sequence,
  1266. (dst_buf->flags & V4L2_BUF_FLAG_LAST) ? " (last)" : "");
  1267. /*
  1268. * Reset JPEG processing unit after each decode run to work
  1269. * around hangups when switching context between encoder and
  1270. * decoder.
  1271. */
  1272. coda_hw_reset(ctx);
  1273. }
  1274. const struct coda_context_ops coda9_jpeg_decode_ops = {
  1275. .queue_init = coda_encoder_queue_init, /* non-bitstream operation */
  1276. .start_streaming = coda9_jpeg_start_decoding,
  1277. .prepare_run = coda9_jpeg_prepare_decode,
  1278. .finish_run = coda9_jpeg_finish_decode,
  1279. .release = coda9_jpeg_release,
  1280. };
  1281. irqreturn_t coda9_jpeg_irq_handler(int irq, void *data)
  1282. {
  1283. struct coda_dev *dev = data;
  1284. struct coda_ctx *ctx;
  1285. int status;
  1286. int err_mb;
  1287. status = coda_read(dev, CODA9_REG_JPEG_PIC_STATUS);
  1288. if (status == 0)
  1289. return IRQ_HANDLED;
  1290. coda_write(dev, status, CODA9_REG_JPEG_PIC_STATUS);
  1291. if (status & CODA9_JPEG_STATUS_OVERFLOW)
  1292. v4l2_err(&dev->v4l2_dev, "JPEG overflow\n");
  1293. if (status & CODA9_JPEG_STATUS_BBC_INT)
  1294. v4l2_err(&dev->v4l2_dev, "JPEG BBC interrupt\n");
  1295. if (status & CODA9_JPEG_STATUS_ERROR) {
  1296. v4l2_err(&dev->v4l2_dev, "JPEG error\n");
  1297. err_mb = coda_read(dev, CODA9_REG_JPEG_PIC_ERRMB);
  1298. if (err_mb) {
  1299. v4l2_err(&dev->v4l2_dev,
  1300. "ERRMB: 0x%x: rst idx %d, mcu pos (%d,%d)\n",
  1301. err_mb, err_mb >> 24, (err_mb >> 12) & 0xfff,
  1302. err_mb & 0xfff);
  1303. }
  1304. }
  1305. ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
  1306. if (!ctx) {
  1307. v4l2_err(&dev->v4l2_dev,
  1308. "Instance released before the end of transaction\n");
  1309. mutex_unlock(&dev->coda_mutex);
  1310. return IRQ_HANDLED;
  1311. }
  1312. complete(&ctx->completion);
  1313. return IRQ_HANDLED;
  1314. }