ipu-image-convert.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720
  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 norotate_irq(int irq, void *data)
  847. {
  848. struct ipu_image_convert_chan *chan = data;
  849. struct ipu_image_convert_ctx *ctx;
  850. struct ipu_image_convert_run *run;
  851. unsigned long flags;
  852. irqreturn_t ret;
  853. spin_lock_irqsave(&chan->irqlock, flags);
  854. /* get current run and its context */
  855. run = chan->current_run;
  856. if (!run) {
  857. ret = IRQ_NONE;
  858. goto out;
  859. }
  860. ctx = run->ctx;
  861. if (ipu_rot_mode_is_irt(ctx->rot_mode)) {
  862. /* this is a rotation operation, just ignore */
  863. spin_unlock_irqrestore(&chan->irqlock, flags);
  864. return IRQ_HANDLED;
  865. }
  866. ret = do_irq(run);
  867. out:
  868. spin_unlock_irqrestore(&chan->irqlock, flags);
  869. return ret;
  870. }
  871. static irqreturn_t rotate_irq(int irq, void *data)
  872. {
  873. struct ipu_image_convert_chan *chan = data;
  874. struct ipu_image_convert_priv *priv = chan->priv;
  875. struct ipu_image_convert_ctx *ctx;
  876. struct ipu_image_convert_run *run;
  877. unsigned long flags;
  878. irqreturn_t ret;
  879. spin_lock_irqsave(&chan->irqlock, flags);
  880. /* get current run and its context */
  881. run = chan->current_run;
  882. if (!run) {
  883. ret = IRQ_NONE;
  884. goto out;
  885. }
  886. ctx = run->ctx;
  887. if (!ipu_rot_mode_is_irt(ctx->rot_mode)) {
  888. /* this was NOT a rotation operation, shouldn't happen */
  889. dev_err(priv->ipu->dev, "Unexpected rotation interrupt\n");
  890. spin_unlock_irqrestore(&chan->irqlock, flags);
  891. return IRQ_HANDLED;
  892. }
  893. ret = do_irq(run);
  894. out:
  895. spin_unlock_irqrestore(&chan->irqlock, flags);
  896. return ret;
  897. }
  898. /*
  899. * try to force the completion of runs for this ctx. Called when
  900. * abort wait times out in ipu_image_convert_abort().
  901. */
  902. static void force_abort(struct ipu_image_convert_ctx *ctx)
  903. {
  904. struct ipu_image_convert_chan *chan = ctx->chan;
  905. struct ipu_image_convert_run *run;
  906. unsigned long flags;
  907. spin_lock_irqsave(&chan->irqlock, flags);
  908. run = chan->current_run;
  909. if (run && run->ctx == ctx) {
  910. convert_stop(run);
  911. run->status = -EIO;
  912. list_add_tail(&run->list, &chan->done_q);
  913. chan->current_run = NULL;
  914. run_next(chan);
  915. }
  916. spin_unlock_irqrestore(&chan->irqlock, flags);
  917. empty_done_q(chan);
  918. }
  919. static void release_ipu_resources(struct ipu_image_convert_chan *chan)
  920. {
  921. if (chan->out_eof_irq >= 0)
  922. free_irq(chan->out_eof_irq, chan);
  923. if (chan->rot_out_eof_irq >= 0)
  924. free_irq(chan->rot_out_eof_irq, chan);
  925. if (!IS_ERR_OR_NULL(chan->in_chan))
  926. ipu_idmac_put(chan->in_chan);
  927. if (!IS_ERR_OR_NULL(chan->out_chan))
  928. ipu_idmac_put(chan->out_chan);
  929. if (!IS_ERR_OR_NULL(chan->rotation_in_chan))
  930. ipu_idmac_put(chan->rotation_in_chan);
  931. if (!IS_ERR_OR_NULL(chan->rotation_out_chan))
  932. ipu_idmac_put(chan->rotation_out_chan);
  933. if (!IS_ERR_OR_NULL(chan->ic))
  934. ipu_ic_put(chan->ic);
  935. chan->in_chan = chan->out_chan = chan->rotation_in_chan =
  936. chan->rotation_out_chan = NULL;
  937. chan->out_eof_irq = chan->rot_out_eof_irq = -1;
  938. }
  939. static int get_ipu_resources(struct ipu_image_convert_chan *chan)
  940. {
  941. const struct ipu_image_convert_dma_chan *dma = chan->dma_ch;
  942. struct ipu_image_convert_priv *priv = chan->priv;
  943. int ret;
  944. /* get IC */
  945. chan->ic = ipu_ic_get(priv->ipu, chan->ic_task);
  946. if (IS_ERR(chan->ic)) {
  947. dev_err(priv->ipu->dev, "could not acquire IC\n");
  948. ret = PTR_ERR(chan->ic);
  949. goto err;
  950. }
  951. /* get IDMAC channels */
  952. chan->in_chan = ipu_idmac_get(priv->ipu, dma->in);
  953. chan->out_chan = ipu_idmac_get(priv->ipu, dma->out);
  954. if (IS_ERR(chan->in_chan) || IS_ERR(chan->out_chan)) {
  955. dev_err(priv->ipu->dev, "could not acquire idmac channels\n");
  956. ret = -EBUSY;
  957. goto err;
  958. }
  959. chan->rotation_in_chan = ipu_idmac_get(priv->ipu, dma->rot_in);
  960. chan->rotation_out_chan = ipu_idmac_get(priv->ipu, dma->rot_out);
  961. if (IS_ERR(chan->rotation_in_chan) || IS_ERR(chan->rotation_out_chan)) {
  962. dev_err(priv->ipu->dev,
  963. "could not acquire idmac rotation channels\n");
  964. ret = -EBUSY;
  965. goto err;
  966. }
  967. /* acquire the EOF interrupts */
  968. chan->out_eof_irq = ipu_idmac_channel_irq(priv->ipu,
  969. chan->out_chan,
  970. IPU_IRQ_EOF);
  971. ret = request_threaded_irq(chan->out_eof_irq, norotate_irq, do_bh,
  972. 0, "ipu-ic", chan);
  973. if (ret < 0) {
  974. dev_err(priv->ipu->dev, "could not acquire irq %d\n",
  975. chan->out_eof_irq);
  976. chan->out_eof_irq = -1;
  977. goto err;
  978. }
  979. chan->rot_out_eof_irq = ipu_idmac_channel_irq(priv->ipu,
  980. chan->rotation_out_chan,
  981. IPU_IRQ_EOF);
  982. ret = request_threaded_irq(chan->rot_out_eof_irq, rotate_irq, do_bh,
  983. 0, "ipu-ic", chan);
  984. if (ret < 0) {
  985. dev_err(priv->ipu->dev, "could not acquire irq %d\n",
  986. chan->rot_out_eof_irq);
  987. chan->rot_out_eof_irq = -1;
  988. goto err;
  989. }
  990. return 0;
  991. err:
  992. release_ipu_resources(chan);
  993. return ret;
  994. }
  995. static int fill_image(struct ipu_image_convert_ctx *ctx,
  996. struct ipu_image_convert_image *ic_image,
  997. struct ipu_image *image,
  998. enum ipu_image_convert_type type)
  999. {
  1000. struct ipu_image_convert_priv *priv = ctx->chan->priv;
  1001. ic_image->base = *image;
  1002. ic_image->type = type;
  1003. ic_image->fmt = get_format(image->pix.pixelformat);
  1004. if (!ic_image->fmt) {
  1005. dev_err(priv->ipu->dev, "pixelformat not supported for %s\n",
  1006. type == IMAGE_CONVERT_OUT ? "Output" : "Input");
  1007. return -EINVAL;
  1008. }
  1009. if (ic_image->fmt->planar)
  1010. ic_image->stride = ic_image->base.pix.width;
  1011. else
  1012. ic_image->stride = ic_image->base.pix.bytesperline;
  1013. calc_tile_dimensions(ctx, ic_image);
  1014. calc_tile_offsets(ctx, ic_image);
  1015. return 0;
  1016. }
  1017. /* borrowed from drivers/media/v4l2-core/v4l2-common.c */
  1018. static unsigned int clamp_align(unsigned int x, unsigned int min,
  1019. unsigned int max, unsigned int align)
  1020. {
  1021. /* Bits that must be zero to be aligned */
  1022. unsigned int mask = ~((1 << align) - 1);
  1023. /* Clamp to aligned min and max */
  1024. x = clamp(x, (min + ~mask) & mask, max & mask);
  1025. /* Round to nearest aligned value */
  1026. if (align)
  1027. x = (x + (1 << (align - 1))) & mask;
  1028. return x;
  1029. }
  1030. /*
  1031. * We have to adjust the tile width such that the tile physaddrs and
  1032. * U and V plane offsets are multiples of 8 bytes as required by
  1033. * the IPU DMA Controller. For the planar formats, this corresponds
  1034. * to a pixel alignment of 16 (but use a more formal equation since
  1035. * the variables are available). For all the packed formats, 8 is
  1036. * good enough.
  1037. */
  1038. static inline u32 tile_width_align(const struct ipu_image_pixfmt *fmt)
  1039. {
  1040. return fmt->planar ? 8 * fmt->uv_width_dec : 8;
  1041. }
  1042. /*
  1043. * For tile height alignment, we have to ensure that the output tile
  1044. * heights are multiples of 8 lines if the IRT is required by the
  1045. * given rotation mode (the IRT performs rotations on 8x8 blocks
  1046. * at a time). If the IRT is not used, or for input image tiles,
  1047. * 2 lines are good enough.
  1048. */
  1049. static inline u32 tile_height_align(enum ipu_image_convert_type type,
  1050. enum ipu_rotate_mode rot_mode)
  1051. {
  1052. return (type == IMAGE_CONVERT_OUT &&
  1053. ipu_rot_mode_is_irt(rot_mode)) ? 8 : 2;
  1054. }
  1055. /* Adjusts input/output images to IPU restrictions */
  1056. void ipu_image_convert_adjust(struct ipu_image *in, struct ipu_image *out,
  1057. enum ipu_rotate_mode rot_mode)
  1058. {
  1059. const struct ipu_image_pixfmt *infmt, *outfmt;
  1060. unsigned int num_in_rows, num_in_cols;
  1061. unsigned int num_out_rows, num_out_cols;
  1062. u32 w_align, h_align;
  1063. infmt = get_format(in->pix.pixelformat);
  1064. outfmt = get_format(out->pix.pixelformat);
  1065. /* set some default pixel formats if needed */
  1066. if (!infmt) {
  1067. in->pix.pixelformat = V4L2_PIX_FMT_RGB24;
  1068. infmt = get_format(V4L2_PIX_FMT_RGB24);
  1069. }
  1070. if (!outfmt) {
  1071. out->pix.pixelformat = V4L2_PIX_FMT_RGB24;
  1072. outfmt = get_format(V4L2_PIX_FMT_RGB24);
  1073. }
  1074. /* image converter does not handle fields */
  1075. in->pix.field = out->pix.field = V4L2_FIELD_NONE;
  1076. /* resizer cannot downsize more than 4:1 */
  1077. if (ipu_rot_mode_is_irt(rot_mode)) {
  1078. out->pix.height = max_t(__u32, out->pix.height,
  1079. in->pix.width / 4);
  1080. out->pix.width = max_t(__u32, out->pix.width,
  1081. in->pix.height / 4);
  1082. } else {
  1083. out->pix.width = max_t(__u32, out->pix.width,
  1084. in->pix.width / 4);
  1085. out->pix.height = max_t(__u32, out->pix.height,
  1086. in->pix.height / 4);
  1087. }
  1088. /* get tiling rows/cols from output format */
  1089. num_out_rows = num_stripes(out->pix.height);
  1090. num_out_cols = num_stripes(out->pix.width);
  1091. if (ipu_rot_mode_is_irt(rot_mode)) {
  1092. num_in_rows = num_out_cols;
  1093. num_in_cols = num_out_rows;
  1094. } else {
  1095. num_in_rows = num_out_rows;
  1096. num_in_cols = num_out_cols;
  1097. }
  1098. /* align input width/height */
  1099. w_align = ilog2(tile_width_align(infmt) * num_in_cols);
  1100. h_align = ilog2(tile_height_align(IMAGE_CONVERT_IN, rot_mode) *
  1101. num_in_rows);
  1102. in->pix.width = clamp_align(in->pix.width, MIN_W, MAX_W, w_align);
  1103. in->pix.height = clamp_align(in->pix.height, MIN_H, MAX_H, h_align);
  1104. /* align output width/height */
  1105. w_align = ilog2(tile_width_align(outfmt) * num_out_cols);
  1106. h_align = ilog2(tile_height_align(IMAGE_CONVERT_OUT, rot_mode) *
  1107. num_out_rows);
  1108. out->pix.width = clamp_align(out->pix.width, MIN_W, MAX_W, w_align);
  1109. out->pix.height = clamp_align(out->pix.height, MIN_H, MAX_H, h_align);
  1110. /* set input/output strides and image sizes */
  1111. in->pix.bytesperline = (in->pix.width * infmt->bpp) >> 3;
  1112. in->pix.sizeimage = in->pix.height * in->pix.bytesperline;
  1113. out->pix.bytesperline = (out->pix.width * outfmt->bpp) >> 3;
  1114. out->pix.sizeimage = out->pix.height * out->pix.bytesperline;
  1115. }
  1116. EXPORT_SYMBOL_GPL(ipu_image_convert_adjust);
  1117. /*
  1118. * this is used by ipu_image_convert_prepare() to verify set input and
  1119. * output images are valid before starting the conversion. Clients can
  1120. * also call it before calling ipu_image_convert_prepare().
  1121. */
  1122. int ipu_image_convert_verify(struct ipu_image *in, struct ipu_image *out,
  1123. enum ipu_rotate_mode rot_mode)
  1124. {
  1125. struct ipu_image testin, testout;
  1126. testin = *in;
  1127. testout = *out;
  1128. ipu_image_convert_adjust(&testin, &testout, rot_mode);
  1129. if (testin.pix.width != in->pix.width ||
  1130. testin.pix.height != in->pix.height ||
  1131. testout.pix.width != out->pix.width ||
  1132. testout.pix.height != out->pix.height)
  1133. return -EINVAL;
  1134. return 0;
  1135. }
  1136. EXPORT_SYMBOL_GPL(ipu_image_convert_verify);
  1137. /*
  1138. * Call ipu_image_convert_prepare() to prepare for the conversion of
  1139. * given images and rotation mode. Returns a new conversion context.
  1140. */
  1141. struct ipu_image_convert_ctx *
  1142. ipu_image_convert_prepare(struct ipu_soc *ipu, enum ipu_ic_task ic_task,
  1143. struct ipu_image *in, struct ipu_image *out,
  1144. enum ipu_rotate_mode rot_mode,
  1145. ipu_image_convert_cb_t complete,
  1146. void *complete_context)
  1147. {
  1148. struct ipu_image_convert_priv *priv = ipu->image_convert_priv;
  1149. struct ipu_image_convert_image *s_image, *d_image;
  1150. struct ipu_image_convert_chan *chan;
  1151. struct ipu_image_convert_ctx *ctx;
  1152. unsigned long flags;
  1153. bool get_res;
  1154. int ret;
  1155. if (!in || !out || !complete ||
  1156. (ic_task != IC_TASK_VIEWFINDER &&
  1157. ic_task != IC_TASK_POST_PROCESSOR))
  1158. return ERR_PTR(-EINVAL);
  1159. /* verify the in/out images before continuing */
  1160. ret = ipu_image_convert_verify(in, out, rot_mode);
  1161. if (ret) {
  1162. dev_err(priv->ipu->dev, "%s: in/out formats invalid\n",
  1163. __func__);
  1164. return ERR_PTR(ret);
  1165. }
  1166. chan = &priv->chan[ic_task];
  1167. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  1168. if (!ctx)
  1169. return ERR_PTR(-ENOMEM);
  1170. dev_dbg(priv->ipu->dev, "%s: task %u: ctx %p\n", __func__,
  1171. chan->ic_task, ctx);
  1172. ctx->chan = chan;
  1173. init_completion(&ctx->aborted);
  1174. s_image = &ctx->in;
  1175. d_image = &ctx->out;
  1176. /* set tiling and rotation */
  1177. d_image->num_rows = num_stripes(out->pix.height);
  1178. d_image->num_cols = num_stripes(out->pix.width);
  1179. if (ipu_rot_mode_is_irt(rot_mode)) {
  1180. s_image->num_rows = d_image->num_cols;
  1181. s_image->num_cols = d_image->num_rows;
  1182. } else {
  1183. s_image->num_rows = d_image->num_rows;
  1184. s_image->num_cols = d_image->num_cols;
  1185. }
  1186. ctx->num_tiles = d_image->num_cols * d_image->num_rows;
  1187. ctx->rot_mode = rot_mode;
  1188. ret = fill_image(ctx, s_image, in, IMAGE_CONVERT_IN);
  1189. if (ret)
  1190. goto out_free;
  1191. ret = fill_image(ctx, d_image, out, IMAGE_CONVERT_OUT);
  1192. if (ret)
  1193. goto out_free;
  1194. calc_out_tile_map(ctx);
  1195. dump_format(ctx, s_image);
  1196. dump_format(ctx, d_image);
  1197. ctx->complete = complete;
  1198. ctx->complete_context = complete_context;
  1199. /*
  1200. * Can we use double-buffering for this operation? If there is
  1201. * only one tile (the whole image can be converted in a single
  1202. * operation) there's no point in using double-buffering. Also,
  1203. * the IPU's IDMAC channels allow only a single U and V plane
  1204. * offset shared between both buffers, but these offsets change
  1205. * for every tile, and therefore would have to be updated for
  1206. * each buffer which is not possible. So double-buffering is
  1207. * impossible when either the source or destination images are
  1208. * a planar format (YUV420, YUV422P, etc.).
  1209. */
  1210. ctx->double_buffering = (ctx->num_tiles > 1 &&
  1211. !s_image->fmt->planar &&
  1212. !d_image->fmt->planar);
  1213. if (ipu_rot_mode_is_irt(ctx->rot_mode)) {
  1214. ret = alloc_dma_buf(priv, &ctx->rot_intermediate[0],
  1215. d_image->tile[0].size);
  1216. if (ret)
  1217. goto out_free;
  1218. if (ctx->double_buffering) {
  1219. ret = alloc_dma_buf(priv,
  1220. &ctx->rot_intermediate[1],
  1221. d_image->tile[0].size);
  1222. if (ret)
  1223. goto out_free_dmabuf0;
  1224. }
  1225. }
  1226. spin_lock_irqsave(&chan->irqlock, flags);
  1227. get_res = list_empty(&chan->ctx_list);
  1228. list_add_tail(&ctx->list, &chan->ctx_list);
  1229. spin_unlock_irqrestore(&chan->irqlock, flags);
  1230. if (get_res) {
  1231. ret = get_ipu_resources(chan);
  1232. if (ret)
  1233. goto out_free_dmabuf1;
  1234. }
  1235. return ctx;
  1236. out_free_dmabuf1:
  1237. free_dma_buf(priv, &ctx->rot_intermediate[1]);
  1238. spin_lock_irqsave(&chan->irqlock, flags);
  1239. list_del(&ctx->list);
  1240. spin_unlock_irqrestore(&chan->irqlock, flags);
  1241. out_free_dmabuf0:
  1242. free_dma_buf(priv, &ctx->rot_intermediate[0]);
  1243. out_free:
  1244. kfree(ctx);
  1245. return ERR_PTR(ret);
  1246. }
  1247. EXPORT_SYMBOL_GPL(ipu_image_convert_prepare);
  1248. /*
  1249. * Carry out a single image conversion run. Only the physaddr's of the input
  1250. * and output image buffers are needed. The conversion context must have
  1251. * been created previously with ipu_image_convert_prepare().
  1252. */
  1253. int ipu_image_convert_queue(struct ipu_image_convert_run *run)
  1254. {
  1255. struct ipu_image_convert_chan *chan;
  1256. struct ipu_image_convert_priv *priv;
  1257. struct ipu_image_convert_ctx *ctx;
  1258. unsigned long flags;
  1259. int ret = 0;
  1260. if (!run || !run->ctx || !run->in_phys || !run->out_phys)
  1261. return -EINVAL;
  1262. ctx = run->ctx;
  1263. chan = ctx->chan;
  1264. priv = chan->priv;
  1265. dev_dbg(priv->ipu->dev, "%s: task %u: ctx %p run %p\n", __func__,
  1266. chan->ic_task, ctx, run);
  1267. INIT_LIST_HEAD(&run->list);
  1268. spin_lock_irqsave(&chan->irqlock, flags);
  1269. if (ctx->aborting) {
  1270. ret = -EIO;
  1271. goto unlock;
  1272. }
  1273. list_add_tail(&run->list, &chan->pending_q);
  1274. if (!chan->current_run) {
  1275. ret = do_run(run);
  1276. if (ret)
  1277. chan->current_run = NULL;
  1278. }
  1279. unlock:
  1280. spin_unlock_irqrestore(&chan->irqlock, flags);
  1281. return ret;
  1282. }
  1283. EXPORT_SYMBOL_GPL(ipu_image_convert_queue);
  1284. /* Abort any active or pending conversions for this context */
  1285. void ipu_image_convert_abort(struct ipu_image_convert_ctx *ctx)
  1286. {
  1287. struct ipu_image_convert_chan *chan = ctx->chan;
  1288. struct ipu_image_convert_priv *priv = chan->priv;
  1289. struct ipu_image_convert_run *run, *active_run, *tmp;
  1290. unsigned long flags;
  1291. int run_count, ret;
  1292. bool need_abort;
  1293. reinit_completion(&ctx->aborted);
  1294. spin_lock_irqsave(&chan->irqlock, flags);
  1295. /* move all remaining pending runs in this context to done_q */
  1296. list_for_each_entry_safe(run, tmp, &chan->pending_q, list) {
  1297. if (run->ctx != ctx)
  1298. continue;
  1299. run->status = -EIO;
  1300. list_move_tail(&run->list, &chan->done_q);
  1301. }
  1302. run_count = get_run_count(ctx, &chan->done_q);
  1303. active_run = (chan->current_run && chan->current_run->ctx == ctx) ?
  1304. chan->current_run : NULL;
  1305. need_abort = (run_count || active_run);
  1306. ctx->aborting = need_abort;
  1307. spin_unlock_irqrestore(&chan->irqlock, flags);
  1308. if (!need_abort) {
  1309. dev_dbg(priv->ipu->dev,
  1310. "%s: task %u: no abort needed for ctx %p\n",
  1311. __func__, chan->ic_task, ctx);
  1312. return;
  1313. }
  1314. dev_dbg(priv->ipu->dev,
  1315. "%s: task %u: wait for completion: %d runs, active run %p\n",
  1316. __func__, chan->ic_task, run_count, active_run);
  1317. ret = wait_for_completion_timeout(&ctx->aborted,
  1318. msecs_to_jiffies(10000));
  1319. if (ret == 0) {
  1320. dev_warn(priv->ipu->dev, "%s: timeout\n", __func__);
  1321. force_abort(ctx);
  1322. }
  1323. ctx->aborting = false;
  1324. }
  1325. EXPORT_SYMBOL_GPL(ipu_image_convert_abort);
  1326. /* Unprepare image conversion context */
  1327. void ipu_image_convert_unprepare(struct ipu_image_convert_ctx *ctx)
  1328. {
  1329. struct ipu_image_convert_chan *chan = ctx->chan;
  1330. struct ipu_image_convert_priv *priv = chan->priv;
  1331. unsigned long flags;
  1332. bool put_res;
  1333. /* make sure no runs are hanging around */
  1334. ipu_image_convert_abort(ctx);
  1335. dev_dbg(priv->ipu->dev, "%s: task %u: removing ctx %p\n", __func__,
  1336. chan->ic_task, ctx);
  1337. spin_lock_irqsave(&chan->irqlock, flags);
  1338. list_del(&ctx->list);
  1339. put_res = list_empty(&chan->ctx_list);
  1340. spin_unlock_irqrestore(&chan->irqlock, flags);
  1341. if (put_res)
  1342. release_ipu_resources(chan);
  1343. free_dma_buf(priv, &ctx->rot_intermediate[1]);
  1344. free_dma_buf(priv, &ctx->rot_intermediate[0]);
  1345. kfree(ctx);
  1346. }
  1347. EXPORT_SYMBOL_GPL(ipu_image_convert_unprepare);
  1348. /*
  1349. * "Canned" asynchronous single image conversion. Allocates and returns
  1350. * a new conversion run. On successful return the caller must free the
  1351. * run and call ipu_image_convert_unprepare() after conversion completes.
  1352. */
  1353. struct ipu_image_convert_run *
  1354. ipu_image_convert(struct ipu_soc *ipu, enum ipu_ic_task ic_task,
  1355. struct ipu_image *in, struct ipu_image *out,
  1356. enum ipu_rotate_mode rot_mode,
  1357. ipu_image_convert_cb_t complete,
  1358. void *complete_context)
  1359. {
  1360. struct ipu_image_convert_ctx *ctx;
  1361. struct ipu_image_convert_run *run;
  1362. int ret;
  1363. ctx = ipu_image_convert_prepare(ipu, ic_task, in, out, rot_mode,
  1364. complete, complete_context);
  1365. if (IS_ERR(ctx))
  1366. return ERR_CAST(ctx);
  1367. run = kzalloc(sizeof(*run), GFP_KERNEL);
  1368. if (!run) {
  1369. ipu_image_convert_unprepare(ctx);
  1370. return ERR_PTR(-ENOMEM);
  1371. }
  1372. run->ctx = ctx;
  1373. run->in_phys = in->phys0;
  1374. run->out_phys = out->phys0;
  1375. ret = ipu_image_convert_queue(run);
  1376. if (ret) {
  1377. ipu_image_convert_unprepare(ctx);
  1378. kfree(run);
  1379. return ERR_PTR(ret);
  1380. }
  1381. return run;
  1382. }
  1383. EXPORT_SYMBOL_GPL(ipu_image_convert);
  1384. /* "Canned" synchronous single image conversion */
  1385. static void image_convert_sync_complete(struct ipu_image_convert_run *run,
  1386. void *data)
  1387. {
  1388. struct completion *comp = data;
  1389. complete(comp);
  1390. }
  1391. int ipu_image_convert_sync(struct ipu_soc *ipu, enum ipu_ic_task ic_task,
  1392. struct ipu_image *in, struct ipu_image *out,
  1393. enum ipu_rotate_mode rot_mode)
  1394. {
  1395. struct ipu_image_convert_run *run;
  1396. struct completion comp;
  1397. int ret;
  1398. init_completion(&comp);
  1399. run = ipu_image_convert(ipu, ic_task, in, out, rot_mode,
  1400. image_convert_sync_complete, &comp);
  1401. if (IS_ERR(run))
  1402. return PTR_ERR(run);
  1403. ret = wait_for_completion_timeout(&comp, msecs_to_jiffies(10000));
  1404. ret = (ret == 0) ? -ETIMEDOUT : 0;
  1405. ipu_image_convert_unprepare(run->ctx);
  1406. kfree(run);
  1407. return ret;
  1408. }
  1409. EXPORT_SYMBOL_GPL(ipu_image_convert_sync);
  1410. int ipu_image_convert_init(struct ipu_soc *ipu, struct device *dev)
  1411. {
  1412. struct ipu_image_convert_priv *priv;
  1413. int i;
  1414. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  1415. if (!priv)
  1416. return -ENOMEM;
  1417. ipu->image_convert_priv = priv;
  1418. priv->ipu = ipu;
  1419. for (i = 0; i < IC_NUM_TASKS; i++) {
  1420. struct ipu_image_convert_chan *chan = &priv->chan[i];
  1421. chan->ic_task = i;
  1422. chan->priv = priv;
  1423. chan->dma_ch = &image_convert_dma_chan[i];
  1424. chan->out_eof_irq = -1;
  1425. chan->rot_out_eof_irq = -1;
  1426. spin_lock_init(&chan->irqlock);
  1427. INIT_LIST_HEAD(&chan->ctx_list);
  1428. INIT_LIST_HEAD(&chan->pending_q);
  1429. INIT_LIST_HEAD(&chan->done_q);
  1430. }
  1431. return 0;
  1432. }
  1433. void ipu_image_convert_exit(struct ipu_soc *ipu)
  1434. {
  1435. }