ipu-image-convert.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706
  1. /*
  2. * Copyright (C) 2012-2016 Mentor Graphics Inc.
  3. *
  4. * Queued image conversion support, with tiling and rotation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  13. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. * for more details.
  15. */
  16. #include <linux/interrupt.h>
  17. #include <linux/dma-mapping.h>
  18. #include <video/imx-ipu-image-convert.h>
  19. #include "ipu-prv.h"
  20. /*
  21. * The IC Resizer has a restriction that the output frame from the
  22. * resizer must be 1024 or less in both width (pixels) and height
  23. * (lines).
  24. *
  25. * The image converter attempts to split up a conversion when
  26. * the desired output (converted) frame resolution exceeds the
  27. * IC resizer limit of 1024 in either dimension.
  28. *
  29. * If either dimension of the output frame exceeds the limit, the
  30. * dimension is split into 1, 2, or 4 equal stripes, for a maximum
  31. * of 4*4 or 16 tiles. A conversion is then carried out for each
  32. * tile (but taking care to pass the full frame stride length to
  33. * the DMA channel's parameter memory!). IDMA double-buffering is used
  34. * to convert each tile back-to-back when possible (see note below
  35. * when double_buffering boolean is set).
  36. *
  37. * Note that the input frame must be split up into the same number
  38. * of tiles as the output frame.
  39. *
  40. * FIXME: at this point there is no attempt to deal with visible seams
  41. * at the tile boundaries when upscaling. The seams are caused by a reset
  42. * of the bilinear upscale interpolation when starting a new tile. The
  43. * seams are barely visible for small upscale factors, but become
  44. * increasingly visible as the upscale factor gets larger, since more
  45. * interpolated pixels get thrown out at the tile boundaries. A possilble
  46. * fix might be to overlap tiles of different sizes, but this must be done
  47. * while also maintaining the IDMAC dma buffer address alignment and 8x8 IRT
  48. * alignment restrictions of each tile.
  49. */
  50. #define MAX_STRIPES_W 4
  51. #define MAX_STRIPES_H 4
  52. #define MAX_TILES (MAX_STRIPES_W * MAX_STRIPES_H)
  53. #define MIN_W 16
  54. #define MIN_H 8
  55. #define MAX_W 4096
  56. #define MAX_H 4096
  57. enum ipu_image_convert_type {
  58. IMAGE_CONVERT_IN = 0,
  59. IMAGE_CONVERT_OUT,
  60. };
  61. struct ipu_image_convert_dma_buf {
  62. void *virt;
  63. dma_addr_t phys;
  64. unsigned long len;
  65. };
  66. struct ipu_image_convert_dma_chan {
  67. int in;
  68. int out;
  69. int rot_in;
  70. int rot_out;
  71. int vdi_in_p;
  72. int vdi_in;
  73. int vdi_in_n;
  74. };
  75. /* dimensions of one tile */
  76. struct ipu_image_tile {
  77. u32 width;
  78. u32 height;
  79. /* size and strides are in bytes */
  80. u32 size;
  81. u32 stride;
  82. u32 rot_stride;
  83. /* start Y or packed offset of this tile */
  84. u32 offset;
  85. /* offset from start to tile in U plane, for planar formats */
  86. u32 u_off;
  87. /* offset from start to tile in V plane, for planar formats */
  88. u32 v_off;
  89. };
  90. struct ipu_image_convert_image {
  91. struct ipu_image base;
  92. enum ipu_image_convert_type type;
  93. const struct ipu_image_pixfmt *fmt;
  94. unsigned int stride;
  95. /* # of rows (horizontal stripes) if dest height is > 1024 */
  96. unsigned int num_rows;
  97. /* # of columns (vertical stripes) if dest width is > 1024 */
  98. unsigned int num_cols;
  99. struct ipu_image_tile tile[MAX_TILES];
  100. };
  101. struct ipu_image_pixfmt {
  102. u32 fourcc; /* V4L2 fourcc */
  103. int bpp; /* total bpp */
  104. int uv_width_dec; /* decimation in width for U/V planes */
  105. int uv_height_dec; /* decimation in height for U/V planes */
  106. bool planar; /* planar format */
  107. bool uv_swapped; /* U and V planes are swapped */
  108. bool uv_packed; /* partial planar (U and V in same plane) */
  109. };
  110. struct ipu_image_convert_ctx;
  111. struct ipu_image_convert_chan;
  112. struct ipu_image_convert_priv;
  113. struct ipu_image_convert_ctx {
  114. struct ipu_image_convert_chan *chan;
  115. ipu_image_convert_cb_t complete;
  116. void *complete_context;
  117. /* Source/destination image data and rotation mode */
  118. struct ipu_image_convert_image in;
  119. struct ipu_image_convert_image out;
  120. enum ipu_rotate_mode rot_mode;
  121. /* intermediate buffer for rotation */
  122. struct ipu_image_convert_dma_buf rot_intermediate[2];
  123. /* current buffer number for double buffering */
  124. int cur_buf_num;
  125. bool aborting;
  126. struct completion aborted;
  127. /* can we use double-buffering for this conversion operation? */
  128. bool double_buffering;
  129. /* num_rows * num_cols */
  130. unsigned int num_tiles;
  131. /* next tile to process */
  132. unsigned int next_tile;
  133. /* where to place converted tile in dest image */
  134. unsigned int out_tile_map[MAX_TILES];
  135. struct list_head list;
  136. };
  137. struct ipu_image_convert_chan {
  138. struct ipu_image_convert_priv *priv;
  139. enum ipu_ic_task ic_task;
  140. const struct ipu_image_convert_dma_chan *dma_ch;
  141. struct ipu_ic *ic;
  142. struct ipuv3_channel *in_chan;
  143. struct ipuv3_channel *out_chan;
  144. struct ipuv3_channel *rotation_in_chan;
  145. struct ipuv3_channel *rotation_out_chan;
  146. /* the IPU end-of-frame irqs */
  147. int out_eof_irq;
  148. int rot_out_eof_irq;
  149. spinlock_t irqlock;
  150. /* list of convert contexts */
  151. struct list_head ctx_list;
  152. /* queue of conversion runs */
  153. struct list_head pending_q;
  154. /* queue of completed runs */
  155. struct list_head done_q;
  156. /* the current conversion run */
  157. struct ipu_image_convert_run *current_run;
  158. };
  159. struct ipu_image_convert_priv {
  160. struct ipu_image_convert_chan chan[IC_NUM_TASKS];
  161. struct ipu_soc *ipu;
  162. };
  163. static const struct ipu_image_convert_dma_chan
  164. image_convert_dma_chan[IC_NUM_TASKS] = {
  165. [IC_TASK_VIEWFINDER] = {
  166. .in = IPUV3_CHANNEL_MEM_IC_PRP_VF,
  167. .out = IPUV3_CHANNEL_IC_PRP_VF_MEM,
  168. .rot_in = IPUV3_CHANNEL_MEM_ROT_VF,
  169. .rot_out = IPUV3_CHANNEL_ROT_VF_MEM,
  170. .vdi_in_p = IPUV3_CHANNEL_MEM_VDI_PREV,
  171. .vdi_in = IPUV3_CHANNEL_MEM_VDI_CUR,
  172. .vdi_in_n = IPUV3_CHANNEL_MEM_VDI_NEXT,
  173. },
  174. [IC_TASK_POST_PROCESSOR] = {
  175. .in = IPUV3_CHANNEL_MEM_IC_PP,
  176. .out = IPUV3_CHANNEL_IC_PP_MEM,
  177. .rot_in = IPUV3_CHANNEL_MEM_ROT_PP,
  178. .rot_out = IPUV3_CHANNEL_ROT_PP_MEM,
  179. },
  180. };
  181. static const struct ipu_image_pixfmt image_convert_formats[] = {
  182. {
  183. .fourcc = V4L2_PIX_FMT_RGB565,
  184. .bpp = 16,
  185. }, {
  186. .fourcc = V4L2_PIX_FMT_RGB24,
  187. .bpp = 24,
  188. }, {
  189. .fourcc = V4L2_PIX_FMT_BGR24,
  190. .bpp = 24,
  191. }, {
  192. .fourcc = V4L2_PIX_FMT_RGB32,
  193. .bpp = 32,
  194. }, {
  195. .fourcc = V4L2_PIX_FMT_BGR32,
  196. .bpp = 32,
  197. }, {
  198. .fourcc = V4L2_PIX_FMT_XRGB32,
  199. .bpp = 32,
  200. }, {
  201. .fourcc = V4L2_PIX_FMT_XBGR32,
  202. .bpp = 32,
  203. }, {
  204. .fourcc = V4L2_PIX_FMT_YUYV,
  205. .bpp = 16,
  206. .uv_width_dec = 2,
  207. .uv_height_dec = 1,
  208. }, {
  209. .fourcc = V4L2_PIX_FMT_UYVY,
  210. .bpp = 16,
  211. .uv_width_dec = 2,
  212. .uv_height_dec = 1,
  213. }, {
  214. .fourcc = V4L2_PIX_FMT_YUV420,
  215. .bpp = 12,
  216. .planar = true,
  217. .uv_width_dec = 2,
  218. .uv_height_dec = 2,
  219. }, {
  220. .fourcc = V4L2_PIX_FMT_YVU420,
  221. .bpp = 12,
  222. .planar = true,
  223. .uv_width_dec = 2,
  224. .uv_height_dec = 2,
  225. .uv_swapped = true,
  226. }, {
  227. .fourcc = V4L2_PIX_FMT_NV12,
  228. .bpp = 12,
  229. .planar = true,
  230. .uv_width_dec = 2,
  231. .uv_height_dec = 2,
  232. .uv_packed = true,
  233. }, {
  234. .fourcc = V4L2_PIX_FMT_YUV422P,
  235. .bpp = 16,
  236. .planar = true,
  237. .uv_width_dec = 2,
  238. .uv_height_dec = 1,
  239. }, {
  240. .fourcc = V4L2_PIX_FMT_NV16,
  241. .bpp = 16,
  242. .planar = true,
  243. .uv_width_dec = 2,
  244. .uv_height_dec = 1,
  245. .uv_packed = true,
  246. },
  247. };
  248. static const struct ipu_image_pixfmt *get_format(u32 fourcc)
  249. {
  250. const struct ipu_image_pixfmt *ret = NULL;
  251. unsigned int i;
  252. for (i = 0; i < ARRAY_SIZE(image_convert_formats); i++) {
  253. if (image_convert_formats[i].fourcc == fourcc) {
  254. ret = &image_convert_formats[i];
  255. break;
  256. }
  257. }
  258. return ret;
  259. }
  260. static void dump_format(struct ipu_image_convert_ctx *ctx,
  261. struct ipu_image_convert_image *ic_image)
  262. {
  263. struct ipu_image_convert_chan *chan = ctx->chan;
  264. struct ipu_image_convert_priv *priv = chan->priv;
  265. dev_dbg(priv->ipu->dev,
  266. "task %u: ctx %p: %s format: %dx%d (%dx%d tiles of size %dx%d), %c%c%c%c\n",
  267. chan->ic_task, ctx,
  268. ic_image->type == IMAGE_CONVERT_OUT ? "Output" : "Input",
  269. ic_image->base.pix.width, ic_image->base.pix.height,
  270. ic_image->num_cols, ic_image->num_rows,
  271. ic_image->tile[0].width, ic_image->tile[0].height,
  272. ic_image->fmt->fourcc & 0xff,
  273. (ic_image->fmt->fourcc >> 8) & 0xff,
  274. (ic_image->fmt->fourcc >> 16) & 0xff,
  275. (ic_image->fmt->fourcc >> 24) & 0xff);
  276. }
  277. int ipu_image_convert_enum_format(int index, u32 *fourcc)
  278. {
  279. const struct ipu_image_pixfmt *fmt;
  280. if (index >= (int)ARRAY_SIZE(image_convert_formats))
  281. return -EINVAL;
  282. /* Format found */
  283. fmt = &image_convert_formats[index];
  284. *fourcc = fmt->fourcc;
  285. return 0;
  286. }
  287. EXPORT_SYMBOL_GPL(ipu_image_convert_enum_format);
  288. static void free_dma_buf(struct ipu_image_convert_priv *priv,
  289. struct ipu_image_convert_dma_buf *buf)
  290. {
  291. if (buf->virt)
  292. dma_free_coherent(priv->ipu->dev,
  293. buf->len, buf->virt, buf->phys);
  294. buf->virt = NULL;
  295. buf->phys = 0;
  296. }
  297. static int alloc_dma_buf(struct ipu_image_convert_priv *priv,
  298. struct ipu_image_convert_dma_buf *buf,
  299. int size)
  300. {
  301. buf->len = PAGE_ALIGN(size);
  302. buf->virt = dma_alloc_coherent(priv->ipu->dev, buf->len, &buf->phys,
  303. GFP_DMA | GFP_KERNEL);
  304. if (!buf->virt) {
  305. dev_err(priv->ipu->dev, "failed to alloc dma buffer\n");
  306. return -ENOMEM;
  307. }
  308. return 0;
  309. }
  310. static inline int num_stripes(int dim)
  311. {
  312. if (dim <= 1024)
  313. return 1;
  314. else if (dim <= 2048)
  315. return 2;
  316. else
  317. return 4;
  318. }
  319. static void calc_tile_dimensions(struct ipu_image_convert_ctx *ctx,
  320. struct ipu_image_convert_image *image)
  321. {
  322. int i;
  323. for (i = 0; i < ctx->num_tiles; i++) {
  324. struct ipu_image_tile *tile = &image->tile[i];
  325. tile->height = image->base.pix.height / image->num_rows;
  326. tile->width = image->base.pix.width / image->num_cols;
  327. tile->size = ((tile->height * image->fmt->bpp) >> 3) *
  328. tile->width;
  329. if (image->fmt->planar) {
  330. tile->stride = tile->width;
  331. tile->rot_stride = tile->height;
  332. } else {
  333. tile->stride =
  334. (image->fmt->bpp * tile->width) >> 3;
  335. tile->rot_stride =
  336. (image->fmt->bpp * tile->height) >> 3;
  337. }
  338. }
  339. }
  340. /*
  341. * Use the rotation transformation to find the tile coordinates
  342. * (row, col) of a tile in the destination frame that corresponds
  343. * to the given tile coordinates of a source frame. The destination
  344. * coordinate is then converted to a tile index.
  345. */
  346. static int transform_tile_index(struct ipu_image_convert_ctx *ctx,
  347. int src_row, int src_col)
  348. {
  349. struct ipu_image_convert_chan *chan = ctx->chan;
  350. struct ipu_image_convert_priv *priv = chan->priv;
  351. struct ipu_image_convert_image *s_image = &ctx->in;
  352. struct ipu_image_convert_image *d_image = &ctx->out;
  353. int dst_row, dst_col;
  354. /* with no rotation it's a 1:1 mapping */
  355. if (ctx->rot_mode == IPU_ROTATE_NONE)
  356. return src_row * s_image->num_cols + src_col;
  357. /*
  358. * before doing the transform, first we have to translate
  359. * source row,col for an origin in the center of s_image
  360. */
  361. src_row = src_row * 2 - (s_image->num_rows - 1);
  362. src_col = src_col * 2 - (s_image->num_cols - 1);
  363. /* do the rotation transform */
  364. if (ctx->rot_mode & IPU_ROT_BIT_90) {
  365. dst_col = -src_row;
  366. dst_row = src_col;
  367. } else {
  368. dst_col = src_col;
  369. dst_row = src_row;
  370. }
  371. /* apply flip */
  372. if (ctx->rot_mode & IPU_ROT_BIT_HFLIP)
  373. dst_col = -dst_col;
  374. if (ctx->rot_mode & IPU_ROT_BIT_VFLIP)
  375. dst_row = -dst_row;
  376. dev_dbg(priv->ipu->dev, "task %u: ctx %p: [%d,%d] --> [%d,%d]\n",
  377. chan->ic_task, ctx, src_col, src_row, dst_col, dst_row);
  378. /*
  379. * finally translate dest row,col using an origin in upper
  380. * left of d_image
  381. */
  382. dst_row += d_image->num_rows - 1;
  383. dst_col += d_image->num_cols - 1;
  384. dst_row /= 2;
  385. dst_col /= 2;
  386. return dst_row * d_image->num_cols + dst_col;
  387. }
  388. /*
  389. * Fill the out_tile_map[] with transformed destination tile indeces.
  390. */
  391. static void calc_out_tile_map(struct ipu_image_convert_ctx *ctx)
  392. {
  393. struct ipu_image_convert_image *s_image = &ctx->in;
  394. unsigned int row, col, tile = 0;
  395. for (row = 0; row < s_image->num_rows; row++) {
  396. for (col = 0; col < s_image->num_cols; col++) {
  397. ctx->out_tile_map[tile] =
  398. transform_tile_index(ctx, row, col);
  399. tile++;
  400. }
  401. }
  402. }
  403. static void calc_tile_offsets_planar(struct ipu_image_convert_ctx *ctx,
  404. struct ipu_image_convert_image *image)
  405. {
  406. struct ipu_image_convert_chan *chan = ctx->chan;
  407. struct ipu_image_convert_priv *priv = chan->priv;
  408. const struct ipu_image_pixfmt *fmt = image->fmt;
  409. unsigned int row, col, tile = 0;
  410. u32 H, w, h, y_stride, uv_stride;
  411. u32 uv_row_off, uv_col_off, uv_off, u_off, v_off, tmp;
  412. u32 y_row_off, y_col_off, y_off;
  413. u32 y_size, uv_size;
  414. /* setup some convenience vars */
  415. H = image->base.pix.height;
  416. y_stride = image->stride;
  417. uv_stride = y_stride / fmt->uv_width_dec;
  418. if (fmt->uv_packed)
  419. uv_stride *= 2;
  420. y_size = H * y_stride;
  421. uv_size = y_size / (fmt->uv_width_dec * fmt->uv_height_dec);
  422. for (row = 0; row < image->num_rows; row++) {
  423. w = image->tile[tile].width;
  424. h = image->tile[tile].height;
  425. y_row_off = row * h * y_stride;
  426. uv_row_off = (row * h * uv_stride) / fmt->uv_height_dec;
  427. for (col = 0; col < image->num_cols; col++) {
  428. y_col_off = col * w;
  429. uv_col_off = y_col_off / fmt->uv_width_dec;
  430. if (fmt->uv_packed)
  431. uv_col_off *= 2;
  432. y_off = y_row_off + y_col_off;
  433. uv_off = uv_row_off + uv_col_off;
  434. u_off = y_size - y_off + uv_off;
  435. v_off = (fmt->uv_packed) ? 0 : u_off + uv_size;
  436. if (fmt->uv_swapped) {
  437. tmp = u_off;
  438. u_off = v_off;
  439. v_off = tmp;
  440. }
  441. image->tile[tile].offset = y_off;
  442. image->tile[tile].u_off = u_off;
  443. image->tile[tile++].v_off = v_off;
  444. dev_dbg(priv->ipu->dev,
  445. "task %u: ctx %p: %s@[%d,%d]: y_off %08x, u_off %08x, v_off %08x\n",
  446. chan->ic_task, ctx,
  447. image->type == IMAGE_CONVERT_IN ?
  448. "Input" : "Output", row, col,
  449. y_off, u_off, v_off);
  450. }
  451. }
  452. }
  453. static void calc_tile_offsets_packed(struct ipu_image_convert_ctx *ctx,
  454. struct ipu_image_convert_image *image)
  455. {
  456. struct ipu_image_convert_chan *chan = ctx->chan;
  457. struct ipu_image_convert_priv *priv = chan->priv;
  458. const struct ipu_image_pixfmt *fmt = image->fmt;
  459. unsigned int row, col, tile = 0;
  460. u32 w, h, bpp, stride;
  461. u32 row_off, col_off;
  462. /* setup some convenience vars */
  463. stride = image->stride;
  464. bpp = fmt->bpp;
  465. for (row = 0; row < image->num_rows; row++) {
  466. w = image->tile[tile].width;
  467. h = image->tile[tile].height;
  468. row_off = row * h * stride;
  469. for (col = 0; col < image->num_cols; col++) {
  470. col_off = (col * w * bpp) >> 3;
  471. image->tile[tile].offset = row_off + col_off;
  472. image->tile[tile].u_off = 0;
  473. image->tile[tile++].v_off = 0;
  474. dev_dbg(priv->ipu->dev,
  475. "task %u: ctx %p: %s@[%d,%d]: phys %08x\n",
  476. chan->ic_task, ctx,
  477. image->type == IMAGE_CONVERT_IN ?
  478. "Input" : "Output", row, col,
  479. row_off + col_off);
  480. }
  481. }
  482. }
  483. static void calc_tile_offsets(struct ipu_image_convert_ctx *ctx,
  484. struct ipu_image_convert_image *image)
  485. {
  486. if (image->fmt->planar)
  487. calc_tile_offsets_planar(ctx, image);
  488. else
  489. calc_tile_offsets_packed(ctx, image);
  490. }
  491. /*
  492. * return the number of runs in given queue (pending_q or done_q)
  493. * for this context. hold irqlock when calling.
  494. */
  495. static int get_run_count(struct ipu_image_convert_ctx *ctx,
  496. struct list_head *q)
  497. {
  498. struct ipu_image_convert_run *run;
  499. int count = 0;
  500. lockdep_assert_held(&ctx->chan->irqlock);
  501. list_for_each_entry(run, q, list) {
  502. if (run->ctx == ctx)
  503. count++;
  504. }
  505. return count;
  506. }
  507. static void convert_stop(struct ipu_image_convert_run *run)
  508. {
  509. struct ipu_image_convert_ctx *ctx = run->ctx;
  510. struct ipu_image_convert_chan *chan = ctx->chan;
  511. struct ipu_image_convert_priv *priv = chan->priv;
  512. dev_dbg(priv->ipu->dev, "%s: task %u: stopping ctx %p run %p\n",
  513. __func__, chan->ic_task, ctx, run);
  514. /* disable IC tasks and the channels */
  515. ipu_ic_task_disable(chan->ic);
  516. ipu_idmac_disable_channel(chan->in_chan);
  517. ipu_idmac_disable_channel(chan->out_chan);
  518. if (ipu_rot_mode_is_irt(ctx->rot_mode)) {
  519. ipu_idmac_disable_channel(chan->rotation_in_chan);
  520. ipu_idmac_disable_channel(chan->rotation_out_chan);
  521. ipu_idmac_unlink(chan->out_chan, chan->rotation_in_chan);
  522. }
  523. ipu_ic_disable(chan->ic);
  524. }
  525. static void init_idmac_channel(struct ipu_image_convert_ctx *ctx,
  526. struct ipuv3_channel *channel,
  527. struct ipu_image_convert_image *image,
  528. enum ipu_rotate_mode rot_mode,
  529. bool rot_swap_width_height)
  530. {
  531. struct ipu_image_convert_chan *chan = ctx->chan;
  532. unsigned int burst_size;
  533. u32 width, height, stride;
  534. dma_addr_t addr0, addr1 = 0;
  535. struct ipu_image tile_image;
  536. unsigned int tile_idx[2];
  537. if (image->type == IMAGE_CONVERT_OUT) {
  538. tile_idx[0] = ctx->out_tile_map[0];
  539. tile_idx[1] = ctx->out_tile_map[1];
  540. } else {
  541. tile_idx[0] = 0;
  542. tile_idx[1] = 1;
  543. }
  544. if (rot_swap_width_height) {
  545. width = image->tile[0].height;
  546. height = image->tile[0].width;
  547. stride = image->tile[0].rot_stride;
  548. addr0 = ctx->rot_intermediate[0].phys;
  549. if (ctx->double_buffering)
  550. addr1 = ctx->rot_intermediate[1].phys;
  551. } else {
  552. width = image->tile[0].width;
  553. height = image->tile[0].height;
  554. stride = image->stride;
  555. addr0 = image->base.phys0 +
  556. image->tile[tile_idx[0]].offset;
  557. if (ctx->double_buffering)
  558. addr1 = image->base.phys0 +
  559. image->tile[tile_idx[1]].offset;
  560. }
  561. ipu_cpmem_zero(channel);
  562. memset(&tile_image, 0, sizeof(tile_image));
  563. tile_image.pix.width = tile_image.rect.width = width;
  564. tile_image.pix.height = tile_image.rect.height = height;
  565. tile_image.pix.bytesperline = stride;
  566. tile_image.pix.pixelformat = image->fmt->fourcc;
  567. tile_image.phys0 = addr0;
  568. tile_image.phys1 = addr1;
  569. ipu_cpmem_set_image(channel, &tile_image);
  570. if (image->fmt->planar && !rot_swap_width_height)
  571. ipu_cpmem_set_uv_offset(channel,
  572. image->tile[tile_idx[0]].u_off,
  573. image->tile[tile_idx[0]].v_off);
  574. if (rot_mode)
  575. ipu_cpmem_set_rotation(channel, rot_mode);
  576. if (channel == chan->rotation_in_chan ||
  577. channel == chan->rotation_out_chan) {
  578. burst_size = 8;
  579. ipu_cpmem_set_block_mode(channel);
  580. } else
  581. burst_size = (width % 16) ? 8 : 16;
  582. ipu_cpmem_set_burstsize(channel, burst_size);
  583. ipu_ic_task_idma_init(chan->ic, channel, width, height,
  584. burst_size, rot_mode);
  585. /*
  586. * Setting a non-zero AXI ID collides with the PRG AXI snooping, so
  587. * only do this when there is no PRG present.
  588. */
  589. if (!channel->ipu->prg_priv)
  590. ipu_cpmem_set_axi_id(channel, 1);
  591. ipu_idmac_set_double_buffer(channel, ctx->double_buffering);
  592. }
  593. static int convert_start(struct ipu_image_convert_run *run)
  594. {
  595. struct ipu_image_convert_ctx *ctx = run->ctx;
  596. struct ipu_image_convert_chan *chan = ctx->chan;
  597. struct ipu_image_convert_priv *priv = chan->priv;
  598. struct ipu_image_convert_image *s_image = &ctx->in;
  599. struct ipu_image_convert_image *d_image = &ctx->out;
  600. enum ipu_color_space src_cs, dest_cs;
  601. unsigned int dest_width, dest_height;
  602. int ret;
  603. dev_dbg(priv->ipu->dev, "%s: task %u: starting ctx %p run %p\n",
  604. __func__, chan->ic_task, ctx, run);
  605. src_cs = ipu_pixelformat_to_colorspace(s_image->fmt->fourcc);
  606. dest_cs = ipu_pixelformat_to_colorspace(d_image->fmt->fourcc);
  607. if (ipu_rot_mode_is_irt(ctx->rot_mode)) {
  608. /* swap width/height for resizer */
  609. dest_width = d_image->tile[0].height;
  610. dest_height = d_image->tile[0].width;
  611. } else {
  612. dest_width = d_image->tile[0].width;
  613. dest_height = d_image->tile[0].height;
  614. }
  615. /* setup the IC resizer and CSC */
  616. ret = ipu_ic_task_init(chan->ic,
  617. s_image->tile[0].width,
  618. s_image->tile[0].height,
  619. dest_width,
  620. dest_height,
  621. src_cs, dest_cs);
  622. if (ret) {
  623. dev_err(priv->ipu->dev, "ipu_ic_task_init failed, %d\n", ret);
  624. return ret;
  625. }
  626. /* init the source MEM-->IC PP IDMAC channel */
  627. init_idmac_channel(ctx, chan->in_chan, s_image,
  628. IPU_ROTATE_NONE, false);
  629. if (ipu_rot_mode_is_irt(ctx->rot_mode)) {
  630. /* init the IC PP-->MEM IDMAC channel */
  631. init_idmac_channel(ctx, chan->out_chan, d_image,
  632. IPU_ROTATE_NONE, true);
  633. /* init the MEM-->IC PP ROT IDMAC channel */
  634. init_idmac_channel(ctx, chan->rotation_in_chan, d_image,
  635. ctx->rot_mode, true);
  636. /* init the destination IC PP ROT-->MEM IDMAC channel */
  637. init_idmac_channel(ctx, chan->rotation_out_chan, d_image,
  638. IPU_ROTATE_NONE, false);
  639. /* now link IC PP-->MEM to MEM-->IC PP ROT */
  640. ipu_idmac_link(chan->out_chan, chan->rotation_in_chan);
  641. } else {
  642. /* init the destination IC PP-->MEM IDMAC channel */
  643. init_idmac_channel(ctx, chan->out_chan, d_image,
  644. ctx->rot_mode, false);
  645. }
  646. /* enable the IC */
  647. ipu_ic_enable(chan->ic);
  648. /* set buffers ready */
  649. ipu_idmac_select_buffer(chan->in_chan, 0);
  650. ipu_idmac_select_buffer(chan->out_chan, 0);
  651. if (ipu_rot_mode_is_irt(ctx->rot_mode))
  652. ipu_idmac_select_buffer(chan->rotation_out_chan, 0);
  653. if (ctx->double_buffering) {
  654. ipu_idmac_select_buffer(chan->in_chan, 1);
  655. ipu_idmac_select_buffer(chan->out_chan, 1);
  656. if (ipu_rot_mode_is_irt(ctx->rot_mode))
  657. ipu_idmac_select_buffer(chan->rotation_out_chan, 1);
  658. }
  659. /* enable the channels! */
  660. ipu_idmac_enable_channel(chan->in_chan);
  661. ipu_idmac_enable_channel(chan->out_chan);
  662. if (ipu_rot_mode_is_irt(ctx->rot_mode)) {
  663. ipu_idmac_enable_channel(chan->rotation_in_chan);
  664. ipu_idmac_enable_channel(chan->rotation_out_chan);
  665. }
  666. ipu_ic_task_enable(chan->ic);
  667. ipu_cpmem_dump(chan->in_chan);
  668. ipu_cpmem_dump(chan->out_chan);
  669. if (ipu_rot_mode_is_irt(ctx->rot_mode)) {
  670. ipu_cpmem_dump(chan->rotation_in_chan);
  671. ipu_cpmem_dump(chan->rotation_out_chan);
  672. }
  673. ipu_dump(priv->ipu);
  674. return 0;
  675. }
  676. /* hold irqlock when calling */
  677. static int do_run(struct ipu_image_convert_run *run)
  678. {
  679. struct ipu_image_convert_ctx *ctx = run->ctx;
  680. struct ipu_image_convert_chan *chan = ctx->chan;
  681. lockdep_assert_held(&chan->irqlock);
  682. ctx->in.base.phys0 = run->in_phys;
  683. ctx->out.base.phys0 = run->out_phys;
  684. ctx->cur_buf_num = 0;
  685. ctx->next_tile = 1;
  686. /* remove run from pending_q and set as current */
  687. list_del(&run->list);
  688. chan->current_run = run;
  689. return convert_start(run);
  690. }
  691. /* hold irqlock when calling */
  692. static void run_next(struct ipu_image_convert_chan *chan)
  693. {
  694. struct ipu_image_convert_priv *priv = chan->priv;
  695. struct ipu_image_convert_run *run, *tmp;
  696. int ret;
  697. lockdep_assert_held(&chan->irqlock);
  698. list_for_each_entry_safe(run, tmp, &chan->pending_q, list) {
  699. /* skip contexts that are aborting */
  700. if (run->ctx->aborting) {
  701. dev_dbg(priv->ipu->dev,
  702. "%s: task %u: skipping aborting ctx %p run %p\n",
  703. __func__, chan->ic_task, run->ctx, run);
  704. continue;
  705. }
  706. ret = do_run(run);
  707. if (!ret)
  708. break;
  709. /*
  710. * something went wrong with start, add the run
  711. * to done q and continue to the next run in the
  712. * pending q.
  713. */
  714. run->status = ret;
  715. list_add_tail(&run->list, &chan->done_q);
  716. chan->current_run = NULL;
  717. }
  718. }
  719. static void empty_done_q(struct ipu_image_convert_chan *chan)
  720. {
  721. struct ipu_image_convert_priv *priv = chan->priv;
  722. struct ipu_image_convert_run *run;
  723. unsigned long flags;
  724. spin_lock_irqsave(&chan->irqlock, flags);
  725. while (!list_empty(&chan->done_q)) {
  726. run = list_entry(chan->done_q.next,
  727. struct ipu_image_convert_run,
  728. list);
  729. list_del(&run->list);
  730. dev_dbg(priv->ipu->dev,
  731. "%s: task %u: completing ctx %p run %p with %d\n",
  732. __func__, chan->ic_task, run->ctx, run, run->status);
  733. /* call the completion callback and free the run */
  734. spin_unlock_irqrestore(&chan->irqlock, flags);
  735. run->ctx->complete(run, run->ctx->complete_context);
  736. spin_lock_irqsave(&chan->irqlock, flags);
  737. }
  738. spin_unlock_irqrestore(&chan->irqlock, flags);
  739. }
  740. /*
  741. * the bottom half thread clears out the done_q, calling the
  742. * completion handler for each.
  743. */
  744. static irqreturn_t do_bh(int irq, void *dev_id)
  745. {
  746. struct ipu_image_convert_chan *chan = dev_id;
  747. struct ipu_image_convert_priv *priv = chan->priv;
  748. struct ipu_image_convert_ctx *ctx;
  749. unsigned long flags;
  750. dev_dbg(priv->ipu->dev, "%s: task %u: enter\n", __func__,
  751. chan->ic_task);
  752. empty_done_q(chan);
  753. spin_lock_irqsave(&chan->irqlock, flags);
  754. /*
  755. * the done_q is cleared out, signal any contexts
  756. * that are aborting that abort can complete.
  757. */
  758. list_for_each_entry(ctx, &chan->ctx_list, list) {
  759. if (ctx->aborting) {
  760. dev_dbg(priv->ipu->dev,
  761. "%s: task %u: signaling abort for ctx %p\n",
  762. __func__, chan->ic_task, ctx);
  763. complete(&ctx->aborted);
  764. }
  765. }
  766. spin_unlock_irqrestore(&chan->irqlock, flags);
  767. dev_dbg(priv->ipu->dev, "%s: task %u: exit\n", __func__,
  768. chan->ic_task);
  769. return IRQ_HANDLED;
  770. }
  771. /* hold irqlock when calling */
  772. static irqreturn_t do_irq(struct ipu_image_convert_run *run)
  773. {
  774. struct ipu_image_convert_ctx *ctx = run->ctx;
  775. struct ipu_image_convert_chan *chan = ctx->chan;
  776. struct ipu_image_tile *src_tile, *dst_tile;
  777. struct ipu_image_convert_image *s_image = &ctx->in;
  778. struct ipu_image_convert_image *d_image = &ctx->out;
  779. struct ipuv3_channel *outch;
  780. unsigned int dst_idx;
  781. lockdep_assert_held(&chan->irqlock);
  782. outch = ipu_rot_mode_is_irt(ctx->rot_mode) ?
  783. chan->rotation_out_chan : chan->out_chan;
  784. /*
  785. * It is difficult to stop the channel DMA before the channels
  786. * enter the paused state. Without double-buffering the channels
  787. * are always in a paused state when the EOF irq occurs, so it
  788. * is safe to stop the channels now. For double-buffering we
  789. * just ignore the abort until the operation completes, when it
  790. * is safe to shut down.
  791. */
  792. if (ctx->aborting && !ctx->double_buffering) {
  793. convert_stop(run);
  794. run->status = -EIO;
  795. goto done;
  796. }
  797. if (ctx->next_tile == ctx->num_tiles) {
  798. /*
  799. * the conversion is complete
  800. */
  801. convert_stop(run);
  802. run->status = 0;
  803. goto done;
  804. }
  805. /*
  806. * not done, place the next tile buffers.
  807. */
  808. if (!ctx->double_buffering) {
  809. src_tile = &s_image->tile[ctx->next_tile];
  810. dst_idx = ctx->out_tile_map[ctx->next_tile];
  811. dst_tile = &d_image->tile[dst_idx];
  812. ipu_cpmem_set_buffer(chan->in_chan, 0,
  813. s_image->base.phys0 + src_tile->offset);
  814. ipu_cpmem_set_buffer(outch, 0,
  815. d_image->base.phys0 + dst_tile->offset);
  816. if (s_image->fmt->planar)
  817. ipu_cpmem_set_uv_offset(chan->in_chan,
  818. src_tile->u_off,
  819. src_tile->v_off);
  820. if (d_image->fmt->planar)
  821. ipu_cpmem_set_uv_offset(outch,
  822. dst_tile->u_off,
  823. dst_tile->v_off);
  824. ipu_idmac_select_buffer(chan->in_chan, 0);
  825. ipu_idmac_select_buffer(outch, 0);
  826. } else if (ctx->next_tile < ctx->num_tiles - 1) {
  827. src_tile = &s_image->tile[ctx->next_tile + 1];
  828. dst_idx = ctx->out_tile_map[ctx->next_tile + 1];
  829. dst_tile = &d_image->tile[dst_idx];
  830. ipu_cpmem_set_buffer(chan->in_chan, ctx->cur_buf_num,
  831. s_image->base.phys0 + src_tile->offset);
  832. ipu_cpmem_set_buffer(outch, ctx->cur_buf_num,
  833. d_image->base.phys0 + dst_tile->offset);
  834. ipu_idmac_select_buffer(chan->in_chan, ctx->cur_buf_num);
  835. ipu_idmac_select_buffer(outch, ctx->cur_buf_num);
  836. ctx->cur_buf_num ^= 1;
  837. }
  838. ctx->next_tile++;
  839. return IRQ_HANDLED;
  840. done:
  841. list_add_tail(&run->list, &chan->done_q);
  842. chan->current_run = NULL;
  843. run_next(chan);
  844. return IRQ_WAKE_THREAD;
  845. }
  846. static irqreturn_t eof_irq(int irq, void *data)
  847. {
  848. struct ipu_image_convert_chan *chan = data;
  849. struct ipu_image_convert_priv *priv = chan->priv;
  850. struct ipu_image_convert_ctx *ctx;
  851. struct ipu_image_convert_run *run;
  852. unsigned long flags;
  853. irqreturn_t ret;
  854. spin_lock_irqsave(&chan->irqlock, flags);
  855. /* get current run and its context */
  856. run = chan->current_run;
  857. if (!run) {
  858. ret = IRQ_NONE;
  859. goto out;
  860. }
  861. ctx = run->ctx;
  862. if (irq == chan->out_eof_irq) {
  863. if (ipu_rot_mode_is_irt(ctx->rot_mode)) {
  864. /* this is a rotation op, just ignore */
  865. ret = IRQ_HANDLED;
  866. goto out;
  867. }
  868. } else if (irq == chan->rot_out_eof_irq) {
  869. if (!ipu_rot_mode_is_irt(ctx->rot_mode)) {
  870. /* this was NOT a rotation op, shouldn't happen */
  871. dev_err(priv->ipu->dev,
  872. "Unexpected rotation interrupt\n");
  873. ret = IRQ_HANDLED;
  874. goto out;
  875. }
  876. } else {
  877. dev_err(priv->ipu->dev, "Received unknown irq %d\n", irq);
  878. ret = IRQ_NONE;
  879. goto out;
  880. }
  881. ret = do_irq(run);
  882. out:
  883. spin_unlock_irqrestore(&chan->irqlock, flags);
  884. return ret;
  885. }
  886. /*
  887. * try to force the completion of runs for this ctx. Called when
  888. * abort wait times out in ipu_image_convert_abort().
  889. */
  890. static void force_abort(struct ipu_image_convert_ctx *ctx)
  891. {
  892. struct ipu_image_convert_chan *chan = ctx->chan;
  893. struct ipu_image_convert_run *run;
  894. unsigned long flags;
  895. spin_lock_irqsave(&chan->irqlock, flags);
  896. run = chan->current_run;
  897. if (run && run->ctx == ctx) {
  898. convert_stop(run);
  899. run->status = -EIO;
  900. list_add_tail(&run->list, &chan->done_q);
  901. chan->current_run = NULL;
  902. run_next(chan);
  903. }
  904. spin_unlock_irqrestore(&chan->irqlock, flags);
  905. empty_done_q(chan);
  906. }
  907. static void release_ipu_resources(struct ipu_image_convert_chan *chan)
  908. {
  909. if (chan->out_eof_irq >= 0)
  910. free_irq(chan->out_eof_irq, chan);
  911. if (chan->rot_out_eof_irq >= 0)
  912. free_irq(chan->rot_out_eof_irq, chan);
  913. if (!IS_ERR_OR_NULL(chan->in_chan))
  914. ipu_idmac_put(chan->in_chan);
  915. if (!IS_ERR_OR_NULL(chan->out_chan))
  916. ipu_idmac_put(chan->out_chan);
  917. if (!IS_ERR_OR_NULL(chan->rotation_in_chan))
  918. ipu_idmac_put(chan->rotation_in_chan);
  919. if (!IS_ERR_OR_NULL(chan->rotation_out_chan))
  920. ipu_idmac_put(chan->rotation_out_chan);
  921. if (!IS_ERR_OR_NULL(chan->ic))
  922. ipu_ic_put(chan->ic);
  923. chan->in_chan = chan->out_chan = chan->rotation_in_chan =
  924. chan->rotation_out_chan = NULL;
  925. chan->out_eof_irq = chan->rot_out_eof_irq = -1;
  926. }
  927. static int get_ipu_resources(struct ipu_image_convert_chan *chan)
  928. {
  929. const struct ipu_image_convert_dma_chan *dma = chan->dma_ch;
  930. struct ipu_image_convert_priv *priv = chan->priv;
  931. int ret;
  932. /* get IC */
  933. chan->ic = ipu_ic_get(priv->ipu, chan->ic_task);
  934. if (IS_ERR(chan->ic)) {
  935. dev_err(priv->ipu->dev, "could not acquire IC\n");
  936. ret = PTR_ERR(chan->ic);
  937. goto err;
  938. }
  939. /* get IDMAC channels */
  940. chan->in_chan = ipu_idmac_get(priv->ipu, dma->in);
  941. chan->out_chan = ipu_idmac_get(priv->ipu, dma->out);
  942. if (IS_ERR(chan->in_chan) || IS_ERR(chan->out_chan)) {
  943. dev_err(priv->ipu->dev, "could not acquire idmac channels\n");
  944. ret = -EBUSY;
  945. goto err;
  946. }
  947. chan->rotation_in_chan = ipu_idmac_get(priv->ipu, dma->rot_in);
  948. chan->rotation_out_chan = ipu_idmac_get(priv->ipu, dma->rot_out);
  949. if (IS_ERR(chan->rotation_in_chan) || IS_ERR(chan->rotation_out_chan)) {
  950. dev_err(priv->ipu->dev,
  951. "could not acquire idmac rotation channels\n");
  952. ret = -EBUSY;
  953. goto err;
  954. }
  955. /* acquire the EOF interrupts */
  956. chan->out_eof_irq = ipu_idmac_channel_irq(priv->ipu,
  957. chan->out_chan,
  958. IPU_IRQ_EOF);
  959. ret = request_threaded_irq(chan->out_eof_irq, eof_irq, do_bh,
  960. 0, "ipu-ic", chan);
  961. if (ret < 0) {
  962. dev_err(priv->ipu->dev, "could not acquire irq %d\n",
  963. chan->out_eof_irq);
  964. chan->out_eof_irq = -1;
  965. goto err;
  966. }
  967. chan->rot_out_eof_irq = ipu_idmac_channel_irq(priv->ipu,
  968. chan->rotation_out_chan,
  969. IPU_IRQ_EOF);
  970. ret = request_threaded_irq(chan->rot_out_eof_irq, eof_irq, do_bh,
  971. 0, "ipu-ic", chan);
  972. if (ret < 0) {
  973. dev_err(priv->ipu->dev, "could not acquire irq %d\n",
  974. chan->rot_out_eof_irq);
  975. chan->rot_out_eof_irq = -1;
  976. goto err;
  977. }
  978. return 0;
  979. err:
  980. release_ipu_resources(chan);
  981. return ret;
  982. }
  983. static int fill_image(struct ipu_image_convert_ctx *ctx,
  984. struct ipu_image_convert_image *ic_image,
  985. struct ipu_image *image,
  986. enum ipu_image_convert_type type)
  987. {
  988. struct ipu_image_convert_priv *priv = ctx->chan->priv;
  989. ic_image->base = *image;
  990. ic_image->type = type;
  991. ic_image->fmt = get_format(image->pix.pixelformat);
  992. if (!ic_image->fmt) {
  993. dev_err(priv->ipu->dev, "pixelformat not supported for %s\n",
  994. type == IMAGE_CONVERT_OUT ? "Output" : "Input");
  995. return -EINVAL;
  996. }
  997. if (ic_image->fmt->planar)
  998. ic_image->stride = ic_image->base.pix.width;
  999. else
  1000. ic_image->stride = ic_image->base.pix.bytesperline;
  1001. calc_tile_dimensions(ctx, ic_image);
  1002. calc_tile_offsets(ctx, ic_image);
  1003. return 0;
  1004. }
  1005. /* borrowed from drivers/media/v4l2-core/v4l2-common.c */
  1006. static unsigned int clamp_align(unsigned int x, unsigned int min,
  1007. unsigned int max, unsigned int align)
  1008. {
  1009. /* Bits that must be zero to be aligned */
  1010. unsigned int mask = ~((1 << align) - 1);
  1011. /* Clamp to aligned min and max */
  1012. x = clamp(x, (min + ~mask) & mask, max & mask);
  1013. /* Round to nearest aligned value */
  1014. if (align)
  1015. x = (x + (1 << (align - 1))) & mask;
  1016. return x;
  1017. }
  1018. /*
  1019. * We have to adjust the tile width such that the tile physaddrs and
  1020. * U and V plane offsets are multiples of 8 bytes as required by
  1021. * the IPU DMA Controller. For the planar formats, this corresponds
  1022. * to a pixel alignment of 16 (but use a more formal equation since
  1023. * the variables are available). For all the packed formats, 8 is
  1024. * good enough.
  1025. */
  1026. static inline u32 tile_width_align(const struct ipu_image_pixfmt *fmt)
  1027. {
  1028. return fmt->planar ? 8 * fmt->uv_width_dec : 8;
  1029. }
  1030. /*
  1031. * For tile height alignment, we have to ensure that the output tile
  1032. * heights are multiples of 8 lines if the IRT is required by the
  1033. * given rotation mode (the IRT performs rotations on 8x8 blocks
  1034. * at a time). If the IRT is not used, or for input image tiles,
  1035. * 2 lines are good enough.
  1036. */
  1037. static inline u32 tile_height_align(enum ipu_image_convert_type type,
  1038. enum ipu_rotate_mode rot_mode)
  1039. {
  1040. return (type == IMAGE_CONVERT_OUT &&
  1041. ipu_rot_mode_is_irt(rot_mode)) ? 8 : 2;
  1042. }
  1043. /* Adjusts input/output images to IPU restrictions */
  1044. void ipu_image_convert_adjust(struct ipu_image *in, struct ipu_image *out,
  1045. enum ipu_rotate_mode rot_mode)
  1046. {
  1047. const struct ipu_image_pixfmt *infmt, *outfmt;
  1048. unsigned int num_in_rows, num_in_cols;
  1049. unsigned int num_out_rows, num_out_cols;
  1050. u32 w_align, h_align;
  1051. infmt = get_format(in->pix.pixelformat);
  1052. outfmt = get_format(out->pix.pixelformat);
  1053. /* set some default pixel formats if needed */
  1054. if (!infmt) {
  1055. in->pix.pixelformat = V4L2_PIX_FMT_RGB24;
  1056. infmt = get_format(V4L2_PIX_FMT_RGB24);
  1057. }
  1058. if (!outfmt) {
  1059. out->pix.pixelformat = V4L2_PIX_FMT_RGB24;
  1060. outfmt = get_format(V4L2_PIX_FMT_RGB24);
  1061. }
  1062. /* image converter does not handle fields */
  1063. in->pix.field = out->pix.field = V4L2_FIELD_NONE;
  1064. /* resizer cannot downsize more than 4:1 */
  1065. if (ipu_rot_mode_is_irt(rot_mode)) {
  1066. out->pix.height = max_t(__u32, out->pix.height,
  1067. in->pix.width / 4);
  1068. out->pix.width = max_t(__u32, out->pix.width,
  1069. in->pix.height / 4);
  1070. } else {
  1071. out->pix.width = max_t(__u32, out->pix.width,
  1072. in->pix.width / 4);
  1073. out->pix.height = max_t(__u32, out->pix.height,
  1074. in->pix.height / 4);
  1075. }
  1076. /* get tiling rows/cols from output format */
  1077. num_out_rows = num_stripes(out->pix.height);
  1078. num_out_cols = num_stripes(out->pix.width);
  1079. if (ipu_rot_mode_is_irt(rot_mode)) {
  1080. num_in_rows = num_out_cols;
  1081. num_in_cols = num_out_rows;
  1082. } else {
  1083. num_in_rows = num_out_rows;
  1084. num_in_cols = num_out_cols;
  1085. }
  1086. /* align input width/height */
  1087. w_align = ilog2(tile_width_align(infmt) * num_in_cols);
  1088. h_align = ilog2(tile_height_align(IMAGE_CONVERT_IN, rot_mode) *
  1089. num_in_rows);
  1090. in->pix.width = clamp_align(in->pix.width, MIN_W, MAX_W, w_align);
  1091. in->pix.height = clamp_align(in->pix.height, MIN_H, MAX_H, h_align);
  1092. /* align output width/height */
  1093. w_align = ilog2(tile_width_align(outfmt) * num_out_cols);
  1094. h_align = ilog2(tile_height_align(IMAGE_CONVERT_OUT, rot_mode) *
  1095. num_out_rows);
  1096. out->pix.width = clamp_align(out->pix.width, MIN_W, MAX_W, w_align);
  1097. out->pix.height = clamp_align(out->pix.height, MIN_H, MAX_H, h_align);
  1098. /* set input/output strides and image sizes */
  1099. in->pix.bytesperline = (in->pix.width * infmt->bpp) >> 3;
  1100. in->pix.sizeimage = in->pix.height * in->pix.bytesperline;
  1101. out->pix.bytesperline = (out->pix.width * outfmt->bpp) >> 3;
  1102. out->pix.sizeimage = out->pix.height * out->pix.bytesperline;
  1103. }
  1104. EXPORT_SYMBOL_GPL(ipu_image_convert_adjust);
  1105. /*
  1106. * this is used by ipu_image_convert_prepare() to verify set input and
  1107. * output images are valid before starting the conversion. Clients can
  1108. * also call it before calling ipu_image_convert_prepare().
  1109. */
  1110. int ipu_image_convert_verify(struct ipu_image *in, struct ipu_image *out,
  1111. enum ipu_rotate_mode rot_mode)
  1112. {
  1113. struct ipu_image testin, testout;
  1114. testin = *in;
  1115. testout = *out;
  1116. ipu_image_convert_adjust(&testin, &testout, rot_mode);
  1117. if (testin.pix.width != in->pix.width ||
  1118. testin.pix.height != in->pix.height ||
  1119. testout.pix.width != out->pix.width ||
  1120. testout.pix.height != out->pix.height)
  1121. return -EINVAL;
  1122. return 0;
  1123. }
  1124. EXPORT_SYMBOL_GPL(ipu_image_convert_verify);
  1125. /*
  1126. * Call ipu_image_convert_prepare() to prepare for the conversion of
  1127. * given images and rotation mode. Returns a new conversion context.
  1128. */
  1129. struct ipu_image_convert_ctx *
  1130. ipu_image_convert_prepare(struct ipu_soc *ipu, enum ipu_ic_task ic_task,
  1131. struct ipu_image *in, struct ipu_image *out,
  1132. enum ipu_rotate_mode rot_mode,
  1133. ipu_image_convert_cb_t complete,
  1134. void *complete_context)
  1135. {
  1136. struct ipu_image_convert_priv *priv = ipu->image_convert_priv;
  1137. struct ipu_image_convert_image *s_image, *d_image;
  1138. struct ipu_image_convert_chan *chan;
  1139. struct ipu_image_convert_ctx *ctx;
  1140. unsigned long flags;
  1141. bool get_res;
  1142. int ret;
  1143. if (!in || !out || !complete ||
  1144. (ic_task != IC_TASK_VIEWFINDER &&
  1145. ic_task != IC_TASK_POST_PROCESSOR))
  1146. return ERR_PTR(-EINVAL);
  1147. /* verify the in/out images before continuing */
  1148. ret = ipu_image_convert_verify(in, out, rot_mode);
  1149. if (ret) {
  1150. dev_err(priv->ipu->dev, "%s: in/out formats invalid\n",
  1151. __func__);
  1152. return ERR_PTR(ret);
  1153. }
  1154. chan = &priv->chan[ic_task];
  1155. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  1156. if (!ctx)
  1157. return ERR_PTR(-ENOMEM);
  1158. dev_dbg(priv->ipu->dev, "%s: task %u: ctx %p\n", __func__,
  1159. chan->ic_task, ctx);
  1160. ctx->chan = chan;
  1161. init_completion(&ctx->aborted);
  1162. s_image = &ctx->in;
  1163. d_image = &ctx->out;
  1164. /* set tiling and rotation */
  1165. d_image->num_rows = num_stripes(out->pix.height);
  1166. d_image->num_cols = num_stripes(out->pix.width);
  1167. if (ipu_rot_mode_is_irt(rot_mode)) {
  1168. s_image->num_rows = d_image->num_cols;
  1169. s_image->num_cols = d_image->num_rows;
  1170. } else {
  1171. s_image->num_rows = d_image->num_rows;
  1172. s_image->num_cols = d_image->num_cols;
  1173. }
  1174. ctx->num_tiles = d_image->num_cols * d_image->num_rows;
  1175. ctx->rot_mode = rot_mode;
  1176. ret = fill_image(ctx, s_image, in, IMAGE_CONVERT_IN);
  1177. if (ret)
  1178. goto out_free;
  1179. ret = fill_image(ctx, d_image, out, IMAGE_CONVERT_OUT);
  1180. if (ret)
  1181. goto out_free;
  1182. calc_out_tile_map(ctx);
  1183. dump_format(ctx, s_image);
  1184. dump_format(ctx, d_image);
  1185. ctx->complete = complete;
  1186. ctx->complete_context = complete_context;
  1187. /*
  1188. * Can we use double-buffering for this operation? If there is
  1189. * only one tile (the whole image can be converted in a single
  1190. * operation) there's no point in using double-buffering. Also,
  1191. * the IPU's IDMAC channels allow only a single U and V plane
  1192. * offset shared between both buffers, but these offsets change
  1193. * for every tile, and therefore would have to be updated for
  1194. * each buffer which is not possible. So double-buffering is
  1195. * impossible when either the source or destination images are
  1196. * a planar format (YUV420, YUV422P, etc.).
  1197. */
  1198. ctx->double_buffering = (ctx->num_tiles > 1 &&
  1199. !s_image->fmt->planar &&
  1200. !d_image->fmt->planar);
  1201. if (ipu_rot_mode_is_irt(ctx->rot_mode)) {
  1202. ret = alloc_dma_buf(priv, &ctx->rot_intermediate[0],
  1203. d_image->tile[0].size);
  1204. if (ret)
  1205. goto out_free;
  1206. if (ctx->double_buffering) {
  1207. ret = alloc_dma_buf(priv,
  1208. &ctx->rot_intermediate[1],
  1209. d_image->tile[0].size);
  1210. if (ret)
  1211. goto out_free_dmabuf0;
  1212. }
  1213. }
  1214. spin_lock_irqsave(&chan->irqlock, flags);
  1215. get_res = list_empty(&chan->ctx_list);
  1216. list_add_tail(&ctx->list, &chan->ctx_list);
  1217. spin_unlock_irqrestore(&chan->irqlock, flags);
  1218. if (get_res) {
  1219. ret = get_ipu_resources(chan);
  1220. if (ret)
  1221. goto out_free_dmabuf1;
  1222. }
  1223. return ctx;
  1224. out_free_dmabuf1:
  1225. free_dma_buf(priv, &ctx->rot_intermediate[1]);
  1226. spin_lock_irqsave(&chan->irqlock, flags);
  1227. list_del(&ctx->list);
  1228. spin_unlock_irqrestore(&chan->irqlock, flags);
  1229. out_free_dmabuf0:
  1230. free_dma_buf(priv, &ctx->rot_intermediate[0]);
  1231. out_free:
  1232. kfree(ctx);
  1233. return ERR_PTR(ret);
  1234. }
  1235. EXPORT_SYMBOL_GPL(ipu_image_convert_prepare);
  1236. /*
  1237. * Carry out a single image conversion run. Only the physaddr's of the input
  1238. * and output image buffers are needed. The conversion context must have
  1239. * been created previously with ipu_image_convert_prepare().
  1240. */
  1241. int ipu_image_convert_queue(struct ipu_image_convert_run *run)
  1242. {
  1243. struct ipu_image_convert_chan *chan;
  1244. struct ipu_image_convert_priv *priv;
  1245. struct ipu_image_convert_ctx *ctx;
  1246. unsigned long flags;
  1247. int ret = 0;
  1248. if (!run || !run->ctx || !run->in_phys || !run->out_phys)
  1249. return -EINVAL;
  1250. ctx = run->ctx;
  1251. chan = ctx->chan;
  1252. priv = chan->priv;
  1253. dev_dbg(priv->ipu->dev, "%s: task %u: ctx %p run %p\n", __func__,
  1254. chan->ic_task, ctx, run);
  1255. INIT_LIST_HEAD(&run->list);
  1256. spin_lock_irqsave(&chan->irqlock, flags);
  1257. if (ctx->aborting) {
  1258. ret = -EIO;
  1259. goto unlock;
  1260. }
  1261. list_add_tail(&run->list, &chan->pending_q);
  1262. if (!chan->current_run) {
  1263. ret = do_run(run);
  1264. if (ret)
  1265. chan->current_run = NULL;
  1266. }
  1267. unlock:
  1268. spin_unlock_irqrestore(&chan->irqlock, flags);
  1269. return ret;
  1270. }
  1271. EXPORT_SYMBOL_GPL(ipu_image_convert_queue);
  1272. /* Abort any active or pending conversions for this context */
  1273. static void __ipu_image_convert_abort(struct ipu_image_convert_ctx *ctx)
  1274. {
  1275. struct ipu_image_convert_chan *chan = ctx->chan;
  1276. struct ipu_image_convert_priv *priv = chan->priv;
  1277. struct ipu_image_convert_run *run, *active_run, *tmp;
  1278. unsigned long flags;
  1279. int run_count, ret;
  1280. bool need_abort;
  1281. reinit_completion(&ctx->aborted);
  1282. spin_lock_irqsave(&chan->irqlock, flags);
  1283. /* move all remaining pending runs in this context to done_q */
  1284. list_for_each_entry_safe(run, tmp, &chan->pending_q, list) {
  1285. if (run->ctx != ctx)
  1286. continue;
  1287. run->status = -EIO;
  1288. list_move_tail(&run->list, &chan->done_q);
  1289. }
  1290. run_count = get_run_count(ctx, &chan->done_q);
  1291. active_run = (chan->current_run && chan->current_run->ctx == ctx) ?
  1292. chan->current_run : NULL;
  1293. need_abort = (run_count || active_run);
  1294. ctx->aborting = true;
  1295. spin_unlock_irqrestore(&chan->irqlock, flags);
  1296. if (!need_abort) {
  1297. dev_dbg(priv->ipu->dev,
  1298. "%s: task %u: no abort needed for ctx %p\n",
  1299. __func__, chan->ic_task, ctx);
  1300. return;
  1301. }
  1302. dev_dbg(priv->ipu->dev,
  1303. "%s: task %u: wait for completion: %d runs, active run %p\n",
  1304. __func__, chan->ic_task, run_count, active_run);
  1305. ret = wait_for_completion_timeout(&ctx->aborted,
  1306. msecs_to_jiffies(10000));
  1307. if (ret == 0) {
  1308. dev_warn(priv->ipu->dev, "%s: timeout\n", __func__);
  1309. force_abort(ctx);
  1310. }
  1311. }
  1312. void ipu_image_convert_abort(struct ipu_image_convert_ctx *ctx)
  1313. {
  1314. __ipu_image_convert_abort(ctx);
  1315. ctx->aborting = false;
  1316. }
  1317. EXPORT_SYMBOL_GPL(ipu_image_convert_abort);
  1318. /* Unprepare image conversion context */
  1319. void ipu_image_convert_unprepare(struct ipu_image_convert_ctx *ctx)
  1320. {
  1321. struct ipu_image_convert_chan *chan = ctx->chan;
  1322. struct ipu_image_convert_priv *priv = chan->priv;
  1323. unsigned long flags;
  1324. bool put_res;
  1325. /* make sure no runs are hanging around */
  1326. __ipu_image_convert_abort(ctx);
  1327. dev_dbg(priv->ipu->dev, "%s: task %u: removing ctx %p\n", __func__,
  1328. chan->ic_task, ctx);
  1329. spin_lock_irqsave(&chan->irqlock, flags);
  1330. list_del(&ctx->list);
  1331. put_res = list_empty(&chan->ctx_list);
  1332. spin_unlock_irqrestore(&chan->irqlock, flags);
  1333. if (put_res)
  1334. release_ipu_resources(chan);
  1335. free_dma_buf(priv, &ctx->rot_intermediate[1]);
  1336. free_dma_buf(priv, &ctx->rot_intermediate[0]);
  1337. kfree(ctx);
  1338. }
  1339. EXPORT_SYMBOL_GPL(ipu_image_convert_unprepare);
  1340. /*
  1341. * "Canned" asynchronous single image conversion. Allocates and returns
  1342. * a new conversion run. On successful return the caller must free the
  1343. * run and call ipu_image_convert_unprepare() after conversion completes.
  1344. */
  1345. struct ipu_image_convert_run *
  1346. ipu_image_convert(struct ipu_soc *ipu, enum ipu_ic_task ic_task,
  1347. struct ipu_image *in, struct ipu_image *out,
  1348. enum ipu_rotate_mode rot_mode,
  1349. ipu_image_convert_cb_t complete,
  1350. void *complete_context)
  1351. {
  1352. struct ipu_image_convert_ctx *ctx;
  1353. struct ipu_image_convert_run *run;
  1354. int ret;
  1355. ctx = ipu_image_convert_prepare(ipu, ic_task, in, out, rot_mode,
  1356. complete, complete_context);
  1357. if (IS_ERR(ctx))
  1358. return ERR_CAST(ctx);
  1359. run = kzalloc(sizeof(*run), GFP_KERNEL);
  1360. if (!run) {
  1361. ipu_image_convert_unprepare(ctx);
  1362. return ERR_PTR(-ENOMEM);
  1363. }
  1364. run->ctx = ctx;
  1365. run->in_phys = in->phys0;
  1366. run->out_phys = out->phys0;
  1367. ret = ipu_image_convert_queue(run);
  1368. if (ret) {
  1369. ipu_image_convert_unprepare(ctx);
  1370. kfree(run);
  1371. return ERR_PTR(ret);
  1372. }
  1373. return run;
  1374. }
  1375. EXPORT_SYMBOL_GPL(ipu_image_convert);
  1376. /* "Canned" synchronous single image conversion */
  1377. static void image_convert_sync_complete(struct ipu_image_convert_run *run,
  1378. void *data)
  1379. {
  1380. struct completion *comp = data;
  1381. complete(comp);
  1382. }
  1383. int ipu_image_convert_sync(struct ipu_soc *ipu, enum ipu_ic_task ic_task,
  1384. struct ipu_image *in, struct ipu_image *out,
  1385. enum ipu_rotate_mode rot_mode)
  1386. {
  1387. struct ipu_image_convert_run *run;
  1388. struct completion comp;
  1389. int ret;
  1390. init_completion(&comp);
  1391. run = ipu_image_convert(ipu, ic_task, in, out, rot_mode,
  1392. image_convert_sync_complete, &comp);
  1393. if (IS_ERR(run))
  1394. return PTR_ERR(run);
  1395. ret = wait_for_completion_timeout(&comp, msecs_to_jiffies(10000));
  1396. ret = (ret == 0) ? -ETIMEDOUT : 0;
  1397. ipu_image_convert_unprepare(run->ctx);
  1398. kfree(run);
  1399. return ret;
  1400. }
  1401. EXPORT_SYMBOL_GPL(ipu_image_convert_sync);
  1402. int ipu_image_convert_init(struct ipu_soc *ipu, struct device *dev)
  1403. {
  1404. struct ipu_image_convert_priv *priv;
  1405. int i;
  1406. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  1407. if (!priv)
  1408. return -ENOMEM;
  1409. ipu->image_convert_priv = priv;
  1410. priv->ipu = ipu;
  1411. for (i = 0; i < IC_NUM_TASKS; i++) {
  1412. struct ipu_image_convert_chan *chan = &priv->chan[i];
  1413. chan->ic_task = i;
  1414. chan->priv = priv;
  1415. chan->dma_ch = &image_convert_dma_chan[i];
  1416. chan->out_eof_irq = -1;
  1417. chan->rot_out_eof_irq = -1;
  1418. spin_lock_init(&chan->irqlock);
  1419. INIT_LIST_HEAD(&chan->ctx_list);
  1420. INIT_LIST_HEAD(&chan->pending_q);
  1421. INIT_LIST_HEAD(&chan->done_q);
  1422. }
  1423. return 0;
  1424. }
  1425. void ipu_image_convert_exit(struct ipu_soc *ipu)
  1426. {
  1427. }