ipu-image-convert.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2012-2016 Mentor Graphics Inc.
  4. *
  5. * Queued image conversion support, with tiling and rotation.
  6. */
  7. #include <linux/interrupt.h>
  8. #include <linux/dma-mapping.h>
  9. #include <linux/math.h>
  10. #include <video/imx-ipu-image-convert.h>
  11. #include "ipu-prv.h"
  12. /*
  13. * The IC Resizer has a restriction that the output frame from the
  14. * resizer must be 1024 or less in both width (pixels) and height
  15. * (lines).
  16. *
  17. * The image converter attempts to split up a conversion when
  18. * the desired output (converted) frame resolution exceeds the
  19. * IC resizer limit of 1024 in either dimension.
  20. *
  21. * If either dimension of the output frame exceeds the limit, the
  22. * dimension is split into 1, 2, or 4 equal stripes, for a maximum
  23. * of 4*4 or 16 tiles. A conversion is then carried out for each
  24. * tile (but taking care to pass the full frame stride length to
  25. * the DMA channel's parameter memory!). IDMA double-buffering is used
  26. * to convert each tile back-to-back when possible (see note below
  27. * when double_buffering boolean is set).
  28. *
  29. * Note that the input frame must be split up into the same number
  30. * of tiles as the output frame:
  31. *
  32. * +---------+-----+
  33. * +-----+---+ | A | B |
  34. * | A | B | | | |
  35. * +-----+---+ --> +---------+-----+
  36. * | C | D | | C | D |
  37. * +-----+---+ | | |
  38. * +---------+-----+
  39. *
  40. * Clockwise 90° rotations are handled by first rescaling into a
  41. * reusable temporary tile buffer and then rotating with the 8x8
  42. * block rotator, writing to the correct destination:
  43. *
  44. * +-----+-----+
  45. * | | |
  46. * +-----+---+ +---------+ | C | A |
  47. * | A | B | | A,B, | | | | |
  48. * +-----+---+ --> | C,D | | --> | | |
  49. * | C | D | +---------+ +-----+-----+
  50. * +-----+---+ | D | B |
  51. * | | |
  52. * +-----+-----+
  53. *
  54. * If the 8x8 block rotator is used, horizontal or vertical flipping
  55. * is done during the rotation step, otherwise flipping is done
  56. * during the scaling step.
  57. * With rotation or flipping, tile order changes between input and
  58. * output image. Tiles are numbered row major from top left to bottom
  59. * right for both input and output image.
  60. */
  61. #define MAX_STRIPES_W 4
  62. #define MAX_STRIPES_H 4
  63. #define MAX_TILES (MAX_STRIPES_W * MAX_STRIPES_H)
  64. #define MIN_W 16
  65. #define MIN_H 8
  66. #define MAX_W 4096
  67. #define MAX_H 4096
  68. enum ipu_image_convert_type {
  69. IMAGE_CONVERT_IN = 0,
  70. IMAGE_CONVERT_OUT,
  71. };
  72. struct ipu_image_convert_dma_buf {
  73. void *virt;
  74. dma_addr_t phys;
  75. unsigned long len;
  76. };
  77. struct ipu_image_convert_dma_chan {
  78. int in;
  79. int out;
  80. int rot_in;
  81. int rot_out;
  82. int vdi_in_p;
  83. int vdi_in;
  84. int vdi_in_n;
  85. };
  86. /* dimensions of one tile */
  87. struct ipu_image_tile {
  88. u32 width;
  89. u32 height;
  90. u32 left;
  91. u32 top;
  92. /* size and strides are in bytes */
  93. u32 size;
  94. u32 stride;
  95. u32 rot_stride;
  96. /* start Y or packed offset of this tile */
  97. u32 offset;
  98. /* offset from start to tile in U plane, for planar formats */
  99. u32 u_off;
  100. /* offset from start to tile in V plane, for planar formats */
  101. u32 v_off;
  102. };
  103. struct ipu_image_convert_image {
  104. struct ipu_image base;
  105. enum ipu_image_convert_type type;
  106. const struct ipu_image_pixfmt *fmt;
  107. unsigned int stride;
  108. /* # of rows (horizontal stripes) if dest height is > 1024 */
  109. unsigned int num_rows;
  110. /* # of columns (vertical stripes) if dest width is > 1024 */
  111. unsigned int num_cols;
  112. struct ipu_image_tile tile[MAX_TILES];
  113. };
  114. struct ipu_image_pixfmt {
  115. u32 fourcc; /* V4L2 fourcc */
  116. int bpp; /* total bpp */
  117. int uv_width_dec; /* decimation in width for U/V planes */
  118. int uv_height_dec; /* decimation in height for U/V planes */
  119. bool planar; /* planar format */
  120. bool uv_swapped; /* U and V planes are swapped */
  121. bool uv_packed; /* partial planar (U and V in same plane) */
  122. };
  123. struct ipu_image_convert_ctx;
  124. struct ipu_image_convert_chan;
  125. struct ipu_image_convert_priv;
  126. enum eof_irq_mask {
  127. EOF_IRQ_IN = BIT(0),
  128. EOF_IRQ_ROT_IN = BIT(1),
  129. EOF_IRQ_OUT = BIT(2),
  130. EOF_IRQ_ROT_OUT = BIT(3),
  131. };
  132. #define EOF_IRQ_COMPLETE (EOF_IRQ_IN | EOF_IRQ_OUT)
  133. #define EOF_IRQ_ROT_COMPLETE (EOF_IRQ_IN | EOF_IRQ_OUT | \
  134. EOF_IRQ_ROT_IN | EOF_IRQ_ROT_OUT)
  135. struct ipu_image_convert_ctx {
  136. struct ipu_image_convert_chan *chan;
  137. ipu_image_convert_cb_t complete;
  138. void *complete_context;
  139. /* Source/destination image data and rotation mode */
  140. struct ipu_image_convert_image in;
  141. struct ipu_image_convert_image out;
  142. struct ipu_ic_csc csc;
  143. enum ipu_rotate_mode rot_mode;
  144. u32 downsize_coeff_h;
  145. u32 downsize_coeff_v;
  146. u32 image_resize_coeff_h;
  147. u32 image_resize_coeff_v;
  148. u32 resize_coeffs_h[MAX_STRIPES_W];
  149. u32 resize_coeffs_v[MAX_STRIPES_H];
  150. /* intermediate buffer for rotation */
  151. struct ipu_image_convert_dma_buf rot_intermediate[2];
  152. /* current buffer number for double buffering */
  153. int cur_buf_num;
  154. bool aborting;
  155. struct completion aborted;
  156. /* can we use double-buffering for this conversion operation? */
  157. bool double_buffering;
  158. /* num_rows * num_cols */
  159. unsigned int num_tiles;
  160. /* next tile to process */
  161. unsigned int next_tile;
  162. /* where to place converted tile in dest image */
  163. unsigned int out_tile_map[MAX_TILES];
  164. /* mask of completed EOF irqs at every tile conversion */
  165. enum eof_irq_mask eof_mask;
  166. struct list_head list;
  167. };
  168. struct ipu_image_convert_chan {
  169. struct ipu_image_convert_priv *priv;
  170. enum ipu_ic_task ic_task;
  171. const struct ipu_image_convert_dma_chan *dma_ch;
  172. struct ipu_ic *ic;
  173. struct ipuv3_channel *in_chan;
  174. struct ipuv3_channel *out_chan;
  175. struct ipuv3_channel *rotation_in_chan;
  176. struct ipuv3_channel *rotation_out_chan;
  177. /* the IPU end-of-frame irqs */
  178. int in_eof_irq;
  179. int rot_in_eof_irq;
  180. int out_eof_irq;
  181. int rot_out_eof_irq;
  182. spinlock_t irqlock;
  183. /* list of convert contexts */
  184. struct list_head ctx_list;
  185. /* queue of conversion runs */
  186. struct list_head pending_q;
  187. /* queue of completed runs */
  188. struct list_head done_q;
  189. /* the current conversion run */
  190. struct ipu_image_convert_run *current_run;
  191. };
  192. struct ipu_image_convert_priv {
  193. struct ipu_image_convert_chan chan[IC_NUM_TASKS];
  194. struct ipu_soc *ipu;
  195. };
  196. static const struct ipu_image_convert_dma_chan
  197. image_convert_dma_chan[IC_NUM_TASKS] = {
  198. [IC_TASK_VIEWFINDER] = {
  199. .in = IPUV3_CHANNEL_MEM_IC_PRP_VF,
  200. .out = IPUV3_CHANNEL_IC_PRP_VF_MEM,
  201. .rot_in = IPUV3_CHANNEL_MEM_ROT_VF,
  202. .rot_out = IPUV3_CHANNEL_ROT_VF_MEM,
  203. .vdi_in_p = IPUV3_CHANNEL_MEM_VDI_PREV,
  204. .vdi_in = IPUV3_CHANNEL_MEM_VDI_CUR,
  205. .vdi_in_n = IPUV3_CHANNEL_MEM_VDI_NEXT,
  206. },
  207. [IC_TASK_POST_PROCESSOR] = {
  208. .in = IPUV3_CHANNEL_MEM_IC_PP,
  209. .out = IPUV3_CHANNEL_IC_PP_MEM,
  210. .rot_in = IPUV3_CHANNEL_MEM_ROT_PP,
  211. .rot_out = IPUV3_CHANNEL_ROT_PP_MEM,
  212. },
  213. };
  214. static const struct ipu_image_pixfmt image_convert_formats[] = {
  215. {
  216. .fourcc = V4L2_PIX_FMT_RGB565,
  217. .bpp = 16,
  218. }, {
  219. .fourcc = V4L2_PIX_FMT_RGB24,
  220. .bpp = 24,
  221. }, {
  222. .fourcc = V4L2_PIX_FMT_BGR24,
  223. .bpp = 24,
  224. }, {
  225. .fourcc = V4L2_PIX_FMT_RGB32,
  226. .bpp = 32,
  227. }, {
  228. .fourcc = V4L2_PIX_FMT_BGR32,
  229. .bpp = 32,
  230. }, {
  231. .fourcc = V4L2_PIX_FMT_XRGB32,
  232. .bpp = 32,
  233. }, {
  234. .fourcc = V4L2_PIX_FMT_XBGR32,
  235. .bpp = 32,
  236. }, {
  237. .fourcc = V4L2_PIX_FMT_BGRX32,
  238. .bpp = 32,
  239. }, {
  240. .fourcc = V4L2_PIX_FMT_RGBX32,
  241. .bpp = 32,
  242. }, {
  243. .fourcc = V4L2_PIX_FMT_YUYV,
  244. .bpp = 16,
  245. .uv_width_dec = 2,
  246. .uv_height_dec = 1,
  247. }, {
  248. .fourcc = V4L2_PIX_FMT_UYVY,
  249. .bpp = 16,
  250. .uv_width_dec = 2,
  251. .uv_height_dec = 1,
  252. }, {
  253. .fourcc = V4L2_PIX_FMT_YUV420,
  254. .bpp = 12,
  255. .planar = true,
  256. .uv_width_dec = 2,
  257. .uv_height_dec = 2,
  258. }, {
  259. .fourcc = V4L2_PIX_FMT_YVU420,
  260. .bpp = 12,
  261. .planar = true,
  262. .uv_width_dec = 2,
  263. .uv_height_dec = 2,
  264. .uv_swapped = true,
  265. }, {
  266. .fourcc = V4L2_PIX_FMT_NV12,
  267. .bpp = 12,
  268. .planar = true,
  269. .uv_width_dec = 2,
  270. .uv_height_dec = 2,
  271. .uv_packed = true,
  272. }, {
  273. .fourcc = V4L2_PIX_FMT_YUV422P,
  274. .bpp = 16,
  275. .planar = true,
  276. .uv_width_dec = 2,
  277. .uv_height_dec = 1,
  278. }, {
  279. .fourcc = V4L2_PIX_FMT_NV16,
  280. .bpp = 16,
  281. .planar = true,
  282. .uv_width_dec = 2,
  283. .uv_height_dec = 1,
  284. .uv_packed = true,
  285. },
  286. };
  287. static const struct ipu_image_pixfmt *get_format(u32 fourcc)
  288. {
  289. const struct ipu_image_pixfmt *ret = NULL;
  290. unsigned int i;
  291. for (i = 0; i < ARRAY_SIZE(image_convert_formats); i++) {
  292. if (image_convert_formats[i].fourcc == fourcc) {
  293. ret = &image_convert_formats[i];
  294. break;
  295. }
  296. }
  297. return ret;
  298. }
  299. static void dump_format(struct ipu_image_convert_ctx *ctx,
  300. struct ipu_image_convert_image *ic_image)
  301. {
  302. struct ipu_image_convert_chan *chan = ctx->chan;
  303. struct ipu_image_convert_priv *priv = chan->priv;
  304. dev_dbg(priv->ipu->dev,
  305. "task %u: ctx %p: %s format: %dx%d (%dx%d tiles), %c%c%c%c\n",
  306. chan->ic_task, ctx,
  307. ic_image->type == IMAGE_CONVERT_OUT ? "Output" : "Input",
  308. ic_image->base.pix.width, ic_image->base.pix.height,
  309. ic_image->num_cols, ic_image->num_rows,
  310. ic_image->fmt->fourcc & 0xff,
  311. (ic_image->fmt->fourcc >> 8) & 0xff,
  312. (ic_image->fmt->fourcc >> 16) & 0xff,
  313. (ic_image->fmt->fourcc >> 24) & 0xff);
  314. }
  315. static void free_dma_buf(struct ipu_image_convert_priv *priv,
  316. struct ipu_image_convert_dma_buf *buf)
  317. {
  318. if (buf->virt)
  319. dma_free_coherent(priv->ipu->dev,
  320. buf->len, buf->virt, buf->phys);
  321. buf->virt = NULL;
  322. buf->phys = 0;
  323. }
  324. static int alloc_dma_buf(struct ipu_image_convert_priv *priv,
  325. struct ipu_image_convert_dma_buf *buf,
  326. int size)
  327. {
  328. buf->len = PAGE_ALIGN(size);
  329. buf->virt = dma_alloc_coherent(priv->ipu->dev, buf->len, &buf->phys,
  330. GFP_DMA | GFP_KERNEL);
  331. if (!buf->virt) {
  332. dev_err(priv->ipu->dev, "failed to alloc dma buffer\n");
  333. return -ENOMEM;
  334. }
  335. return 0;
  336. }
  337. static inline int num_stripes(int dim)
  338. {
  339. return (dim - 1) / 1024 + 1;
  340. }
  341. /*
  342. * Calculate downsizing coefficients, which are the same for all tiles,
  343. * and initial bilinear resizing coefficients, which are used to find the
  344. * best seam positions.
  345. * Also determine the number of tiles necessary to guarantee that no tile
  346. * is larger than 1024 pixels in either dimension at the output and between
  347. * IC downsizing and main processing sections.
  348. */
  349. static int calc_image_resize_coefficients(struct ipu_image_convert_ctx *ctx,
  350. struct ipu_image *in,
  351. struct ipu_image *out)
  352. {
  353. u32 downsized_width = in->rect.width;
  354. u32 downsized_height = in->rect.height;
  355. u32 downsize_coeff_v = 0;
  356. u32 downsize_coeff_h = 0;
  357. u32 resized_width = out->rect.width;
  358. u32 resized_height = out->rect.height;
  359. u32 resize_coeff_h;
  360. u32 resize_coeff_v;
  361. u32 cols;
  362. u32 rows;
  363. if (ipu_rot_mode_is_irt(ctx->rot_mode)) {
  364. resized_width = out->rect.height;
  365. resized_height = out->rect.width;
  366. }
  367. /* Do not let invalid input lead to an endless loop below */
  368. if (WARN_ON(resized_width == 0 || resized_height == 0))
  369. return -EINVAL;
  370. while (downsized_width >= resized_width * 2) {
  371. downsized_width >>= 1;
  372. downsize_coeff_h++;
  373. }
  374. while (downsized_height >= resized_height * 2) {
  375. downsized_height >>= 1;
  376. downsize_coeff_v++;
  377. }
  378. /*
  379. * Calculate the bilinear resizing coefficients that could be used if
  380. * we were converting with a single tile. The bottom right output pixel
  381. * should sample as close as possible to the bottom right input pixel
  382. * out of the decimator, but not overshoot it:
  383. */
  384. resize_coeff_h = 8192 * (downsized_width - 1) / (resized_width - 1);
  385. resize_coeff_v = 8192 * (downsized_height - 1) / (resized_height - 1);
  386. /*
  387. * Both the output of the IC downsizing section before being passed to
  388. * the IC main processing section and the final output of the IC main
  389. * processing section must be <= 1024 pixels in both dimensions.
  390. */
  391. cols = num_stripes(max_t(u32, downsized_width, resized_width));
  392. rows = num_stripes(max_t(u32, downsized_height, resized_height));
  393. dev_dbg(ctx->chan->priv->ipu->dev,
  394. "%s: hscale: >>%u, *8192/%u vscale: >>%u, *8192/%u, %ux%u tiles\n",
  395. __func__, downsize_coeff_h, resize_coeff_h, downsize_coeff_v,
  396. resize_coeff_v, cols, rows);
  397. if (downsize_coeff_h > 2 || downsize_coeff_v > 2 ||
  398. resize_coeff_h > 0x3fff || resize_coeff_v > 0x3fff)
  399. return -EINVAL;
  400. ctx->downsize_coeff_h = downsize_coeff_h;
  401. ctx->downsize_coeff_v = downsize_coeff_v;
  402. ctx->image_resize_coeff_h = resize_coeff_h;
  403. ctx->image_resize_coeff_v = resize_coeff_v;
  404. ctx->in.num_cols = cols;
  405. ctx->in.num_rows = rows;
  406. return 0;
  407. }
  408. #define round_closest(x, y) round_down((x) + (y)/2, (y))
  409. /*
  410. * Find the best aligned seam position for the given column / row index.
  411. * Rotation and image offsets are out of scope.
  412. *
  413. * @index: column / row index, used to calculate valid interval
  414. * @in_edge: input right / bottom edge
  415. * @out_edge: output right / bottom edge
  416. * @in_align: input alignment, either horizontal 8-byte line start address
  417. * alignment, or pixel alignment due to image format
  418. * @out_align: output alignment, either horizontal 8-byte line start address
  419. * alignment, or pixel alignment due to image format or rotator
  420. * block size
  421. * @in_burst: horizontal input burst size in case of horizontal flip
  422. * @out_burst: horizontal output burst size or rotator block size
  423. * @downsize_coeff: downsizing section coefficient
  424. * @resize_coeff: main processing section resizing coefficient
  425. * @_in_seam: aligned input seam position return value
  426. * @_out_seam: aligned output seam position return value
  427. */
  428. static void find_best_seam(struct ipu_image_convert_ctx *ctx,
  429. unsigned int index,
  430. unsigned int in_edge,
  431. unsigned int out_edge,
  432. unsigned int in_align,
  433. unsigned int out_align,
  434. unsigned int in_burst,
  435. unsigned int out_burst,
  436. unsigned int downsize_coeff,
  437. unsigned int resize_coeff,
  438. u32 *_in_seam,
  439. u32 *_out_seam)
  440. {
  441. struct device *dev = ctx->chan->priv->ipu->dev;
  442. unsigned int out_pos;
  443. /* Input / output seam position candidates */
  444. unsigned int out_seam = 0;
  445. unsigned int in_seam = 0;
  446. unsigned int min_diff = UINT_MAX;
  447. unsigned int out_start;
  448. unsigned int out_end;
  449. unsigned int in_start;
  450. unsigned int in_end;
  451. /* Start within 1024 pixels of the right / bottom edge */
  452. out_start = max_t(int, index * out_align, out_edge - 1024);
  453. /* End before having to add more columns to the left / rows above */
  454. out_end = min_t(unsigned int, out_edge, index * 1024 + 1);
  455. /*
  456. * Limit input seam position to make sure that the downsized input tile
  457. * to the right or bottom does not exceed 1024 pixels.
  458. */
  459. in_start = max_t(int, index * in_align,
  460. in_edge - (1024 << downsize_coeff));
  461. in_end = min_t(unsigned int, in_edge,
  462. index * (1024 << downsize_coeff) + 1);
  463. /*
  464. * Output tiles must start at a multiple of 8 bytes horizontally and
  465. * possibly at an even line horizontally depending on the pixel format.
  466. * Only consider output aligned positions for the seam.
  467. */
  468. out_start = round_up(out_start, out_align);
  469. for (out_pos = out_start; out_pos < out_end; out_pos += out_align) {
  470. unsigned int in_pos;
  471. unsigned int in_pos_aligned;
  472. unsigned int in_pos_rounded;
  473. unsigned int diff;
  474. /*
  475. * Tiles in the right row / bottom column may not be allowed to
  476. * overshoot horizontally / vertically. out_burst may be the
  477. * actual DMA burst size, or the rotator block size.
  478. */
  479. if ((out_burst > 1) && (out_edge - out_pos) % out_burst)
  480. continue;
  481. /*
  482. * Input sample position, corresponding to out_pos, 19.13 fixed
  483. * point.
  484. */
  485. in_pos = (out_pos * resize_coeff) << downsize_coeff;
  486. /*
  487. * The closest input sample position that we could actually
  488. * start the input tile at, 19.13 fixed point.
  489. */
  490. in_pos_aligned = round_closest(in_pos, 8192U * in_align);
  491. /* Convert 19.13 fixed point to integer */
  492. in_pos_rounded = in_pos_aligned / 8192U;
  493. if (in_pos_rounded < in_start)
  494. continue;
  495. if (in_pos_rounded >= in_end)
  496. break;
  497. if ((in_burst > 1) &&
  498. (in_edge - in_pos_rounded) % in_burst)
  499. continue;
  500. diff = abs_diff(in_pos, in_pos_aligned);
  501. if (diff < min_diff) {
  502. in_seam = in_pos_rounded;
  503. out_seam = out_pos;
  504. min_diff = diff;
  505. }
  506. }
  507. *_out_seam = out_seam;
  508. *_in_seam = in_seam;
  509. dev_dbg(dev, "%s: out_seam %u(%u) in [%u, %u], in_seam %u(%u) in [%u, %u] diff %u.%03u\n",
  510. __func__, out_seam, out_align, out_start, out_end,
  511. in_seam, in_align, in_start, in_end, min_diff / 8192,
  512. DIV_ROUND_CLOSEST(min_diff % 8192 * 1000, 8192));
  513. }
  514. /*
  515. * Tile left edges are required to be aligned to multiples of 8 bytes
  516. * by the IDMAC.
  517. */
  518. static inline u32 tile_left_align(const struct ipu_image_pixfmt *fmt)
  519. {
  520. if (fmt->planar)
  521. return fmt->uv_packed ? 8 : 8 * fmt->uv_width_dec;
  522. else
  523. return fmt->bpp == 32 ? 2 : fmt->bpp == 16 ? 4 : 8;
  524. }
  525. /*
  526. * Tile top edge alignment is only limited by chroma subsampling.
  527. */
  528. static inline u32 tile_top_align(const struct ipu_image_pixfmt *fmt)
  529. {
  530. return fmt->uv_height_dec > 1 ? 2 : 1;
  531. }
  532. static inline u32 tile_width_align(enum ipu_image_convert_type type,
  533. const struct ipu_image_pixfmt *fmt,
  534. enum ipu_rotate_mode rot_mode)
  535. {
  536. if (type == IMAGE_CONVERT_IN) {
  537. /*
  538. * The IC burst reads 8 pixels at a time. Reading beyond the
  539. * end of the line is usually acceptable. Those pixels are
  540. * ignored, unless the IC has to write the scaled line in
  541. * reverse.
  542. */
  543. return (!ipu_rot_mode_is_irt(rot_mode) &&
  544. (rot_mode & IPU_ROT_BIT_HFLIP)) ? 8 : 2;
  545. }
  546. /*
  547. * Align to 16x16 pixel blocks for planar 4:2:0 chroma subsampled
  548. * formats to guarantee 8-byte aligned line start addresses in the
  549. * chroma planes when IRT is used. Align to 8x8 pixel IRT block size
  550. * for all other formats.
  551. */
  552. return (ipu_rot_mode_is_irt(rot_mode) &&
  553. fmt->planar && !fmt->uv_packed) ?
  554. 8 * fmt->uv_width_dec : 8;
  555. }
  556. static inline u32 tile_height_align(enum ipu_image_convert_type type,
  557. const struct ipu_image_pixfmt *fmt,
  558. enum ipu_rotate_mode rot_mode)
  559. {
  560. if (type == IMAGE_CONVERT_IN || !ipu_rot_mode_is_irt(rot_mode))
  561. return 2;
  562. /*
  563. * Align to 16x16 pixel blocks for planar 4:2:0 chroma subsampled
  564. * formats to guarantee 8-byte aligned line start addresses in the
  565. * chroma planes when IRT is used. Align to 8x8 pixel IRT block size
  566. * for all other formats.
  567. */
  568. return (fmt->planar && !fmt->uv_packed) ? 8 * fmt->uv_width_dec : 8;
  569. }
  570. /*
  571. * Fill in left position and width and for all tiles in an input column, and
  572. * for all corresponding output tiles. If the 90° rotator is used, the output
  573. * tiles are in a row, and output tile top position and height are set.
  574. */
  575. static void fill_tile_column(struct ipu_image_convert_ctx *ctx,
  576. unsigned int col,
  577. struct ipu_image_convert_image *in,
  578. unsigned int in_left, unsigned int in_width,
  579. struct ipu_image_convert_image *out,
  580. unsigned int out_left, unsigned int out_width)
  581. {
  582. unsigned int row, tile_idx;
  583. struct ipu_image_tile *in_tile, *out_tile;
  584. for (row = 0; row < in->num_rows; row++) {
  585. tile_idx = in->num_cols * row + col;
  586. in_tile = &in->tile[tile_idx];
  587. out_tile = &out->tile[ctx->out_tile_map[tile_idx]];
  588. in_tile->left = in_left;
  589. in_tile->width = in_width;
  590. if (ipu_rot_mode_is_irt(ctx->rot_mode)) {
  591. out_tile->top = out_left;
  592. out_tile->height = out_width;
  593. } else {
  594. out_tile->left = out_left;
  595. out_tile->width = out_width;
  596. }
  597. }
  598. }
  599. /*
  600. * Fill in top position and height and for all tiles in an input row, and
  601. * for all corresponding output tiles. If the 90° rotator is used, the output
  602. * tiles are in a column, and output tile left position and width are set.
  603. */
  604. static void fill_tile_row(struct ipu_image_convert_ctx *ctx, unsigned int row,
  605. struct ipu_image_convert_image *in,
  606. unsigned int in_top, unsigned int in_height,
  607. struct ipu_image_convert_image *out,
  608. unsigned int out_top, unsigned int out_height)
  609. {
  610. unsigned int col, tile_idx;
  611. struct ipu_image_tile *in_tile, *out_tile;
  612. for (col = 0; col < in->num_cols; col++) {
  613. tile_idx = in->num_cols * row + col;
  614. in_tile = &in->tile[tile_idx];
  615. out_tile = &out->tile[ctx->out_tile_map[tile_idx]];
  616. in_tile->top = in_top;
  617. in_tile->height = in_height;
  618. if (ipu_rot_mode_is_irt(ctx->rot_mode)) {
  619. out_tile->left = out_top;
  620. out_tile->width = out_height;
  621. } else {
  622. out_tile->top = out_top;
  623. out_tile->height = out_height;
  624. }
  625. }
  626. }
  627. /*
  628. * Find the best horizontal and vertical seam positions to split into tiles.
  629. * Minimize the fractional part of the input sampling position for the
  630. * top / left pixels of each tile.
  631. */
  632. static void find_seams(struct ipu_image_convert_ctx *ctx,
  633. struct ipu_image_convert_image *in,
  634. struct ipu_image_convert_image *out)
  635. {
  636. struct device *dev = ctx->chan->priv->ipu->dev;
  637. unsigned int resized_width = out->base.rect.width;
  638. unsigned int resized_height = out->base.rect.height;
  639. unsigned int col;
  640. unsigned int row;
  641. unsigned int in_left_align = tile_left_align(in->fmt);
  642. unsigned int in_top_align = tile_top_align(in->fmt);
  643. unsigned int out_left_align = tile_left_align(out->fmt);
  644. unsigned int out_top_align = tile_top_align(out->fmt);
  645. unsigned int out_width_align = tile_width_align(out->type, out->fmt,
  646. ctx->rot_mode);
  647. unsigned int out_height_align = tile_height_align(out->type, out->fmt,
  648. ctx->rot_mode);
  649. unsigned int in_right = in->base.rect.width;
  650. unsigned int in_bottom = in->base.rect.height;
  651. unsigned int out_right = out->base.rect.width;
  652. unsigned int out_bottom = out->base.rect.height;
  653. unsigned int flipped_out_left;
  654. unsigned int flipped_out_top;
  655. if (ipu_rot_mode_is_irt(ctx->rot_mode)) {
  656. /* Switch width/height and align top left to IRT block size */
  657. resized_width = out->base.rect.height;
  658. resized_height = out->base.rect.width;
  659. out_left_align = out_height_align;
  660. out_top_align = out_width_align;
  661. out_width_align = out_left_align;
  662. out_height_align = out_top_align;
  663. out_right = out->base.rect.height;
  664. out_bottom = out->base.rect.width;
  665. }
  666. for (col = in->num_cols - 1; col > 0; col--) {
  667. bool allow_in_overshoot = ipu_rot_mode_is_irt(ctx->rot_mode) ||
  668. !(ctx->rot_mode & IPU_ROT_BIT_HFLIP);
  669. bool allow_out_overshoot = (col < in->num_cols - 1) &&
  670. !(ctx->rot_mode & IPU_ROT_BIT_HFLIP);
  671. unsigned int in_left;
  672. unsigned int out_left;
  673. /*
  674. * Align input width to burst length if the scaling step flips
  675. * horizontally.
  676. */
  677. find_best_seam(ctx, col,
  678. in_right, out_right,
  679. in_left_align, out_left_align,
  680. allow_in_overshoot ? 1 : 8 /* burst length */,
  681. allow_out_overshoot ? 1 : out_width_align,
  682. ctx->downsize_coeff_h, ctx->image_resize_coeff_h,
  683. &in_left, &out_left);
  684. if (ctx->rot_mode & IPU_ROT_BIT_HFLIP)
  685. flipped_out_left = resized_width - out_right;
  686. else
  687. flipped_out_left = out_left;
  688. fill_tile_column(ctx, col, in, in_left, in_right - in_left,
  689. out, flipped_out_left, out_right - out_left);
  690. dev_dbg(dev, "%s: col %u: %u, %u -> %u, %u\n", __func__, col,
  691. in_left, in_right - in_left,
  692. flipped_out_left, out_right - out_left);
  693. in_right = in_left;
  694. out_right = out_left;
  695. }
  696. flipped_out_left = (ctx->rot_mode & IPU_ROT_BIT_HFLIP) ?
  697. resized_width - out_right : 0;
  698. fill_tile_column(ctx, 0, in, 0, in_right,
  699. out, flipped_out_left, out_right);
  700. dev_dbg(dev, "%s: col 0: 0, %u -> %u, %u\n", __func__,
  701. in_right, flipped_out_left, out_right);
  702. for (row = in->num_rows - 1; row > 0; row--) {
  703. bool allow_overshoot = row < in->num_rows - 1;
  704. unsigned int in_top;
  705. unsigned int out_top;
  706. find_best_seam(ctx, row,
  707. in_bottom, out_bottom,
  708. in_top_align, out_top_align,
  709. 1, allow_overshoot ? 1 : out_height_align,
  710. ctx->downsize_coeff_v, ctx->image_resize_coeff_v,
  711. &in_top, &out_top);
  712. if ((ctx->rot_mode & IPU_ROT_BIT_VFLIP) ^
  713. ipu_rot_mode_is_irt(ctx->rot_mode))
  714. flipped_out_top = resized_height - out_bottom;
  715. else
  716. flipped_out_top = out_top;
  717. fill_tile_row(ctx, row, in, in_top, in_bottom - in_top,
  718. out, flipped_out_top, out_bottom - out_top);
  719. dev_dbg(dev, "%s: row %u: %u, %u -> %u, %u\n", __func__, row,
  720. in_top, in_bottom - in_top,
  721. flipped_out_top, out_bottom - out_top);
  722. in_bottom = in_top;
  723. out_bottom = out_top;
  724. }
  725. if ((ctx->rot_mode & IPU_ROT_BIT_VFLIP) ^
  726. ipu_rot_mode_is_irt(ctx->rot_mode))
  727. flipped_out_top = resized_height - out_bottom;
  728. else
  729. flipped_out_top = 0;
  730. fill_tile_row(ctx, 0, in, 0, in_bottom,
  731. out, flipped_out_top, out_bottom);
  732. dev_dbg(dev, "%s: row 0: 0, %u -> %u, %u\n", __func__,
  733. in_bottom, flipped_out_top, out_bottom);
  734. }
  735. static int calc_tile_dimensions(struct ipu_image_convert_ctx *ctx,
  736. struct ipu_image_convert_image *image)
  737. {
  738. struct ipu_image_convert_chan *chan = ctx->chan;
  739. struct ipu_image_convert_priv *priv = chan->priv;
  740. unsigned int max_width = 1024;
  741. unsigned int max_height = 1024;
  742. unsigned int i;
  743. if (image->type == IMAGE_CONVERT_IN) {
  744. /* Up to 4096x4096 input tile size */
  745. max_width <<= ctx->downsize_coeff_h;
  746. max_height <<= ctx->downsize_coeff_v;
  747. }
  748. for (i = 0; i < ctx->num_tiles; i++) {
  749. struct ipu_image_tile *tile;
  750. const unsigned int row = i / image->num_cols;
  751. const unsigned int col = i % image->num_cols;
  752. if (image->type == IMAGE_CONVERT_OUT)
  753. tile = &image->tile[ctx->out_tile_map[i]];
  754. else
  755. tile = &image->tile[i];
  756. tile->size = ((tile->height * image->fmt->bpp) >> 3) *
  757. tile->width;
  758. if (image->fmt->planar) {
  759. tile->stride = tile->width;
  760. tile->rot_stride = tile->height;
  761. } else {
  762. tile->stride =
  763. (image->fmt->bpp * tile->width) >> 3;
  764. tile->rot_stride =
  765. (image->fmt->bpp * tile->height) >> 3;
  766. }
  767. dev_dbg(priv->ipu->dev,
  768. "task %u: ctx %p: %s@[%u,%u]: %ux%u@%u,%u\n",
  769. chan->ic_task, ctx,
  770. image->type == IMAGE_CONVERT_IN ? "Input" : "Output",
  771. row, col,
  772. tile->width, tile->height, tile->left, tile->top);
  773. if (!tile->width || tile->width > max_width ||
  774. !tile->height || tile->height > max_height) {
  775. dev_err(priv->ipu->dev, "invalid %s tile size: %ux%u\n",
  776. image->type == IMAGE_CONVERT_IN ? "input" :
  777. "output", tile->width, tile->height);
  778. return -EINVAL;
  779. }
  780. }
  781. return 0;
  782. }
  783. /*
  784. * Use the rotation transformation to find the tile coordinates
  785. * (row, col) of a tile in the destination frame that corresponds
  786. * to the given tile coordinates of a source frame. The destination
  787. * coordinate is then converted to a tile index.
  788. */
  789. static int transform_tile_index(struct ipu_image_convert_ctx *ctx,
  790. int src_row, int src_col)
  791. {
  792. struct ipu_image_convert_chan *chan = ctx->chan;
  793. struct ipu_image_convert_priv *priv = chan->priv;
  794. struct ipu_image_convert_image *s_image = &ctx->in;
  795. struct ipu_image_convert_image *d_image = &ctx->out;
  796. int dst_row, dst_col;
  797. /* with no rotation it's a 1:1 mapping */
  798. if (ctx->rot_mode == IPU_ROTATE_NONE)
  799. return src_row * s_image->num_cols + src_col;
  800. /*
  801. * before doing the transform, first we have to translate
  802. * source row,col for an origin in the center of s_image
  803. */
  804. src_row = src_row * 2 - (s_image->num_rows - 1);
  805. src_col = src_col * 2 - (s_image->num_cols - 1);
  806. /* do the rotation transform */
  807. if (ctx->rot_mode & IPU_ROT_BIT_90) {
  808. dst_col = -src_row;
  809. dst_row = src_col;
  810. } else {
  811. dst_col = src_col;
  812. dst_row = src_row;
  813. }
  814. /* apply flip */
  815. if (ctx->rot_mode & IPU_ROT_BIT_HFLIP)
  816. dst_col = -dst_col;
  817. if (ctx->rot_mode & IPU_ROT_BIT_VFLIP)
  818. dst_row = -dst_row;
  819. dev_dbg(priv->ipu->dev, "task %u: ctx %p: [%d,%d] --> [%d,%d]\n",
  820. chan->ic_task, ctx, src_col, src_row, dst_col, dst_row);
  821. /*
  822. * finally translate dest row,col using an origin in upper
  823. * left of d_image
  824. */
  825. dst_row += d_image->num_rows - 1;
  826. dst_col += d_image->num_cols - 1;
  827. dst_row /= 2;
  828. dst_col /= 2;
  829. return dst_row * d_image->num_cols + dst_col;
  830. }
  831. /*
  832. * Fill the out_tile_map[] with transformed destination tile indeces.
  833. */
  834. static void calc_out_tile_map(struct ipu_image_convert_ctx *ctx)
  835. {
  836. struct ipu_image_convert_image *s_image = &ctx->in;
  837. unsigned int row, col, tile = 0;
  838. for (row = 0; row < s_image->num_rows; row++) {
  839. for (col = 0; col < s_image->num_cols; col++) {
  840. ctx->out_tile_map[tile] =
  841. transform_tile_index(ctx, row, col);
  842. tile++;
  843. }
  844. }
  845. }
  846. static int calc_tile_offsets_planar(struct ipu_image_convert_ctx *ctx,
  847. struct ipu_image_convert_image *image)
  848. {
  849. struct ipu_image_convert_chan *chan = ctx->chan;
  850. struct ipu_image_convert_priv *priv = chan->priv;
  851. const struct ipu_image_pixfmt *fmt = image->fmt;
  852. unsigned int row, col, tile = 0;
  853. u32 H, top, y_stride, uv_stride;
  854. u32 uv_row_off, uv_col_off, uv_off, u_off, v_off;
  855. u32 y_row_off, y_col_off, y_off;
  856. u32 y_size, uv_size;
  857. /* setup some convenience vars */
  858. H = image->base.pix.height;
  859. y_stride = image->stride;
  860. uv_stride = y_stride / fmt->uv_width_dec;
  861. if (fmt->uv_packed)
  862. uv_stride *= 2;
  863. y_size = H * y_stride;
  864. uv_size = y_size / (fmt->uv_width_dec * fmt->uv_height_dec);
  865. for (row = 0; row < image->num_rows; row++) {
  866. top = image->tile[tile].top;
  867. y_row_off = top * y_stride;
  868. uv_row_off = (top * uv_stride) / fmt->uv_height_dec;
  869. for (col = 0; col < image->num_cols; col++) {
  870. y_col_off = image->tile[tile].left;
  871. uv_col_off = y_col_off / fmt->uv_width_dec;
  872. if (fmt->uv_packed)
  873. uv_col_off *= 2;
  874. y_off = y_row_off + y_col_off;
  875. uv_off = uv_row_off + uv_col_off;
  876. u_off = y_size - y_off + uv_off;
  877. v_off = (fmt->uv_packed) ? 0 : u_off + uv_size;
  878. if (fmt->uv_swapped)
  879. swap(u_off, v_off);
  880. image->tile[tile].offset = y_off;
  881. image->tile[tile].u_off = u_off;
  882. image->tile[tile++].v_off = v_off;
  883. if ((y_off & 0x7) || (u_off & 0x7) || (v_off & 0x7)) {
  884. dev_err(priv->ipu->dev,
  885. "task %u: ctx %p: %s@[%d,%d]: "
  886. "y_off %08x, u_off %08x, v_off %08x\n",
  887. chan->ic_task, ctx,
  888. image->type == IMAGE_CONVERT_IN ?
  889. "Input" : "Output", row, col,
  890. y_off, u_off, v_off);
  891. return -EINVAL;
  892. }
  893. }
  894. }
  895. return 0;
  896. }
  897. static int calc_tile_offsets_packed(struct ipu_image_convert_ctx *ctx,
  898. struct ipu_image_convert_image *image)
  899. {
  900. struct ipu_image_convert_chan *chan = ctx->chan;
  901. struct ipu_image_convert_priv *priv = chan->priv;
  902. const struct ipu_image_pixfmt *fmt = image->fmt;
  903. unsigned int row, col, tile = 0;
  904. u32 bpp, stride, offset;
  905. u32 row_off, col_off;
  906. /* setup some convenience vars */
  907. stride = image->stride;
  908. bpp = fmt->bpp;
  909. for (row = 0; row < image->num_rows; row++) {
  910. row_off = image->tile[tile].top * stride;
  911. for (col = 0; col < image->num_cols; col++) {
  912. col_off = (image->tile[tile].left * bpp) >> 3;
  913. offset = row_off + col_off;
  914. image->tile[tile].offset = offset;
  915. image->tile[tile].u_off = 0;
  916. image->tile[tile++].v_off = 0;
  917. if (offset & 0x7) {
  918. dev_err(priv->ipu->dev,
  919. "task %u: ctx %p: %s@[%d,%d]: "
  920. "phys %08x\n",
  921. chan->ic_task, ctx,
  922. image->type == IMAGE_CONVERT_IN ?
  923. "Input" : "Output", row, col,
  924. row_off + col_off);
  925. return -EINVAL;
  926. }
  927. }
  928. }
  929. return 0;
  930. }
  931. static int calc_tile_offsets(struct ipu_image_convert_ctx *ctx,
  932. struct ipu_image_convert_image *image)
  933. {
  934. if (image->fmt->planar)
  935. return calc_tile_offsets_planar(ctx, image);
  936. return calc_tile_offsets_packed(ctx, image);
  937. }
  938. /*
  939. * Calculate the resizing ratio for the IC main processing section given input
  940. * size, fixed downsizing coefficient, and output size.
  941. * Either round to closest for the next tile's first pixel to minimize seams
  942. * and distortion (for all but right column / bottom row), or round down to
  943. * avoid sampling beyond the edges of the input image for this tile's last
  944. * pixel.
  945. * Returns the resizing coefficient, resizing ratio is 8192.0 / resize_coeff.
  946. */
  947. static u32 calc_resize_coeff(u32 input_size, u32 downsize_coeff,
  948. u32 output_size, bool allow_overshoot)
  949. {
  950. u32 downsized = input_size >> downsize_coeff;
  951. if (allow_overshoot)
  952. return DIV_ROUND_CLOSEST(8192 * downsized, output_size);
  953. else
  954. return 8192 * (downsized - 1) / (output_size - 1);
  955. }
  956. /*
  957. * Slightly modify resize coefficients per tile to hide the bilinear
  958. * interpolator reset at tile borders, shifting the right / bottom edge
  959. * by up to a half input pixel. This removes noticeable seams between
  960. * tiles at higher upscaling factors.
  961. */
  962. static void calc_tile_resize_coefficients(struct ipu_image_convert_ctx *ctx)
  963. {
  964. struct ipu_image_convert_chan *chan = ctx->chan;
  965. struct ipu_image_convert_priv *priv = chan->priv;
  966. struct ipu_image_tile *in_tile, *out_tile;
  967. unsigned int col, row, tile_idx;
  968. unsigned int last_output;
  969. for (col = 0; col < ctx->in.num_cols; col++) {
  970. bool closest = (col < ctx->in.num_cols - 1) &&
  971. !(ctx->rot_mode & IPU_ROT_BIT_HFLIP);
  972. u32 resized_width;
  973. u32 resize_coeff_h;
  974. u32 in_width;
  975. tile_idx = col;
  976. in_tile = &ctx->in.tile[tile_idx];
  977. out_tile = &ctx->out.tile[ctx->out_tile_map[tile_idx]];
  978. if (ipu_rot_mode_is_irt(ctx->rot_mode))
  979. resized_width = out_tile->height;
  980. else
  981. resized_width = out_tile->width;
  982. resize_coeff_h = calc_resize_coeff(in_tile->width,
  983. ctx->downsize_coeff_h,
  984. resized_width, closest);
  985. dev_dbg(priv->ipu->dev, "%s: column %u hscale: *8192/%u\n",
  986. __func__, col, resize_coeff_h);
  987. /*
  988. * With the horizontal scaling factor known, round up resized
  989. * width (output width or height) to burst size.
  990. */
  991. resized_width = round_up(resized_width, 8);
  992. /*
  993. * Calculate input width from the last accessed input pixel
  994. * given resized width and scaling coefficients. Round up to
  995. * burst size.
  996. */
  997. last_output = resized_width - 1;
  998. if (closest && ((last_output * resize_coeff_h) % 8192))
  999. last_output++;
  1000. in_width = round_up(
  1001. (DIV_ROUND_UP(last_output * resize_coeff_h, 8192) + 1)
  1002. << ctx->downsize_coeff_h, 8);
  1003. for (row = 0; row < ctx->in.num_rows; row++) {
  1004. tile_idx = row * ctx->in.num_cols + col;
  1005. in_tile = &ctx->in.tile[tile_idx];
  1006. out_tile = &ctx->out.tile[ctx->out_tile_map[tile_idx]];
  1007. if (ipu_rot_mode_is_irt(ctx->rot_mode))
  1008. out_tile->height = resized_width;
  1009. else
  1010. out_tile->width = resized_width;
  1011. in_tile->width = in_width;
  1012. }
  1013. ctx->resize_coeffs_h[col] = resize_coeff_h;
  1014. }
  1015. for (row = 0; row < ctx->in.num_rows; row++) {
  1016. bool closest = (row < ctx->in.num_rows - 1) &&
  1017. !(ctx->rot_mode & IPU_ROT_BIT_VFLIP);
  1018. u32 resized_height;
  1019. u32 resize_coeff_v;
  1020. u32 in_height;
  1021. tile_idx = row * ctx->in.num_cols;
  1022. in_tile = &ctx->in.tile[tile_idx];
  1023. out_tile = &ctx->out.tile[ctx->out_tile_map[tile_idx]];
  1024. if (ipu_rot_mode_is_irt(ctx->rot_mode))
  1025. resized_height = out_tile->width;
  1026. else
  1027. resized_height = out_tile->height;
  1028. resize_coeff_v = calc_resize_coeff(in_tile->height,
  1029. ctx->downsize_coeff_v,
  1030. resized_height, closest);
  1031. dev_dbg(priv->ipu->dev, "%s: row %u vscale: *8192/%u\n",
  1032. __func__, row, resize_coeff_v);
  1033. /*
  1034. * With the vertical scaling factor known, round up resized
  1035. * height (output width or height) to IDMAC limitations.
  1036. */
  1037. resized_height = round_up(resized_height, 2);
  1038. /*
  1039. * Calculate input width from the last accessed input pixel
  1040. * given resized height and scaling coefficients. Align to
  1041. * IDMAC restrictions.
  1042. */
  1043. last_output = resized_height - 1;
  1044. if (closest && ((last_output * resize_coeff_v) % 8192))
  1045. last_output++;
  1046. in_height = round_up(
  1047. (DIV_ROUND_UP(last_output * resize_coeff_v, 8192) + 1)
  1048. << ctx->downsize_coeff_v, 2);
  1049. for (col = 0; col < ctx->in.num_cols; col++) {
  1050. tile_idx = row * ctx->in.num_cols + col;
  1051. in_tile = &ctx->in.tile[tile_idx];
  1052. out_tile = &ctx->out.tile[ctx->out_tile_map[tile_idx]];
  1053. if (ipu_rot_mode_is_irt(ctx->rot_mode))
  1054. out_tile->width = resized_height;
  1055. else
  1056. out_tile->height = resized_height;
  1057. in_tile->height = in_height;
  1058. }
  1059. ctx->resize_coeffs_v[row] = resize_coeff_v;
  1060. }
  1061. }
  1062. /*
  1063. * return the number of runs in given queue (pending_q or done_q)
  1064. * for this context. hold irqlock when calling.
  1065. */
  1066. static int get_run_count(struct ipu_image_convert_ctx *ctx,
  1067. struct list_head *q)
  1068. {
  1069. struct ipu_image_convert_run *run;
  1070. int count = 0;
  1071. lockdep_assert_held(&ctx->chan->irqlock);
  1072. list_for_each_entry(run, q, list) {
  1073. if (run->ctx == ctx)
  1074. count++;
  1075. }
  1076. return count;
  1077. }
  1078. static void convert_stop(struct ipu_image_convert_run *run)
  1079. {
  1080. struct ipu_image_convert_ctx *ctx = run->ctx;
  1081. struct ipu_image_convert_chan *chan = ctx->chan;
  1082. struct ipu_image_convert_priv *priv = chan->priv;
  1083. dev_dbg(priv->ipu->dev, "%s: task %u: stopping ctx %p run %p\n",
  1084. __func__, chan->ic_task, ctx, run);
  1085. /* disable IC tasks and the channels */
  1086. ipu_ic_task_disable(chan->ic);
  1087. ipu_idmac_disable_channel(chan->in_chan);
  1088. ipu_idmac_disable_channel(chan->out_chan);
  1089. if (ipu_rot_mode_is_irt(ctx->rot_mode)) {
  1090. ipu_idmac_disable_channel(chan->rotation_in_chan);
  1091. ipu_idmac_disable_channel(chan->rotation_out_chan);
  1092. ipu_idmac_unlink(chan->out_chan, chan->rotation_in_chan);
  1093. }
  1094. ipu_ic_disable(chan->ic);
  1095. }
  1096. static void init_idmac_channel(struct ipu_image_convert_ctx *ctx,
  1097. struct ipuv3_channel *channel,
  1098. struct ipu_image_convert_image *image,
  1099. enum ipu_rotate_mode rot_mode,
  1100. bool rot_swap_width_height,
  1101. unsigned int tile)
  1102. {
  1103. struct ipu_image_convert_chan *chan = ctx->chan;
  1104. unsigned int burst_size;
  1105. u32 width, height, stride;
  1106. dma_addr_t addr0, addr1 = 0;
  1107. struct ipu_image tile_image;
  1108. unsigned int tile_idx[2];
  1109. if (image->type == IMAGE_CONVERT_OUT) {
  1110. tile_idx[0] = ctx->out_tile_map[tile];
  1111. tile_idx[1] = ctx->out_tile_map[1];
  1112. } else {
  1113. tile_idx[0] = tile;
  1114. tile_idx[1] = 1;
  1115. }
  1116. if (rot_swap_width_height) {
  1117. width = image->tile[tile_idx[0]].height;
  1118. height = image->tile[tile_idx[0]].width;
  1119. stride = image->tile[tile_idx[0]].rot_stride;
  1120. addr0 = ctx->rot_intermediate[0].phys;
  1121. if (ctx->double_buffering)
  1122. addr1 = ctx->rot_intermediate[1].phys;
  1123. } else {
  1124. width = image->tile[tile_idx[0]].width;
  1125. height = image->tile[tile_idx[0]].height;
  1126. stride = image->stride;
  1127. addr0 = image->base.phys0 +
  1128. image->tile[tile_idx[0]].offset;
  1129. if (ctx->double_buffering)
  1130. addr1 = image->base.phys0 +
  1131. image->tile[tile_idx[1]].offset;
  1132. }
  1133. ipu_cpmem_zero(channel);
  1134. memset(&tile_image, 0, sizeof(tile_image));
  1135. tile_image.pix.width = tile_image.rect.width = width;
  1136. tile_image.pix.height = tile_image.rect.height = height;
  1137. tile_image.pix.bytesperline = stride;
  1138. tile_image.pix.pixelformat = image->fmt->fourcc;
  1139. tile_image.phys0 = addr0;
  1140. tile_image.phys1 = addr1;
  1141. if (image->fmt->planar && !rot_swap_width_height) {
  1142. tile_image.u_offset = image->tile[tile_idx[0]].u_off;
  1143. tile_image.v_offset = image->tile[tile_idx[0]].v_off;
  1144. }
  1145. ipu_cpmem_set_image(channel, &tile_image);
  1146. if (rot_mode)
  1147. ipu_cpmem_set_rotation(channel, rot_mode);
  1148. /*
  1149. * Skip writing U and V components to odd rows in the output
  1150. * channels for planar 4:2:0.
  1151. */
  1152. if ((channel == chan->out_chan ||
  1153. channel == chan->rotation_out_chan) &&
  1154. image->fmt->planar && image->fmt->uv_height_dec == 2)
  1155. ipu_cpmem_skip_odd_chroma_rows(channel);
  1156. if (channel == chan->rotation_in_chan ||
  1157. channel == chan->rotation_out_chan) {
  1158. burst_size = 8;
  1159. ipu_cpmem_set_block_mode(channel);
  1160. } else
  1161. burst_size = (width % 16) ? 8 : 16;
  1162. ipu_cpmem_set_burstsize(channel, burst_size);
  1163. ipu_ic_task_idma_init(chan->ic, channel, width, height,
  1164. burst_size, rot_mode);
  1165. /*
  1166. * Setting a non-zero AXI ID collides with the PRG AXI snooping, so
  1167. * only do this when there is no PRG present.
  1168. */
  1169. if (!channel->ipu->prg_priv)
  1170. ipu_cpmem_set_axi_id(channel, 1);
  1171. ipu_idmac_set_double_buffer(channel, ctx->double_buffering);
  1172. }
  1173. static int convert_start(struct ipu_image_convert_run *run, unsigned int tile)
  1174. {
  1175. struct ipu_image_convert_ctx *ctx = run->ctx;
  1176. struct ipu_image_convert_chan *chan = ctx->chan;
  1177. struct ipu_image_convert_priv *priv = chan->priv;
  1178. struct ipu_image_convert_image *s_image = &ctx->in;
  1179. struct ipu_image_convert_image *d_image = &ctx->out;
  1180. unsigned int dst_tile = ctx->out_tile_map[tile];
  1181. unsigned int dest_width, dest_height;
  1182. unsigned int col, row;
  1183. u32 rsc;
  1184. int ret;
  1185. dev_dbg(priv->ipu->dev, "%s: task %u: starting ctx %p run %p tile %u -> %u\n",
  1186. __func__, chan->ic_task, ctx, run, tile, dst_tile);
  1187. /* clear EOF irq mask */
  1188. ctx->eof_mask = 0;
  1189. if (ipu_rot_mode_is_irt(ctx->rot_mode)) {
  1190. /* swap width/height for resizer */
  1191. dest_width = d_image->tile[dst_tile].height;
  1192. dest_height = d_image->tile[dst_tile].width;
  1193. } else {
  1194. dest_width = d_image->tile[dst_tile].width;
  1195. dest_height = d_image->tile[dst_tile].height;
  1196. }
  1197. row = tile / s_image->num_cols;
  1198. col = tile % s_image->num_cols;
  1199. rsc = (ctx->downsize_coeff_v << 30) |
  1200. (ctx->resize_coeffs_v[row] << 16) |
  1201. (ctx->downsize_coeff_h << 14) |
  1202. (ctx->resize_coeffs_h[col]);
  1203. dev_dbg(priv->ipu->dev, "%s: %ux%u -> %ux%u (rsc = 0x%x)\n",
  1204. __func__, s_image->tile[tile].width,
  1205. s_image->tile[tile].height, dest_width, dest_height, rsc);
  1206. /* setup the IC resizer and CSC */
  1207. ret = ipu_ic_task_init_rsc(chan->ic, &ctx->csc,
  1208. s_image->tile[tile].width,
  1209. s_image->tile[tile].height,
  1210. dest_width,
  1211. dest_height,
  1212. rsc);
  1213. if (ret) {
  1214. dev_err(priv->ipu->dev, "ipu_ic_task_init failed, %d\n", ret);
  1215. return ret;
  1216. }
  1217. /* init the source MEM-->IC PP IDMAC channel */
  1218. init_idmac_channel(ctx, chan->in_chan, s_image,
  1219. IPU_ROTATE_NONE, false, tile);
  1220. if (ipu_rot_mode_is_irt(ctx->rot_mode)) {
  1221. /* init the IC PP-->MEM IDMAC channel */
  1222. init_idmac_channel(ctx, chan->out_chan, d_image,
  1223. IPU_ROTATE_NONE, true, tile);
  1224. /* init the MEM-->IC PP ROT IDMAC channel */
  1225. init_idmac_channel(ctx, chan->rotation_in_chan, d_image,
  1226. ctx->rot_mode, true, tile);
  1227. /* init the destination IC PP ROT-->MEM IDMAC channel */
  1228. init_idmac_channel(ctx, chan->rotation_out_chan, d_image,
  1229. IPU_ROTATE_NONE, false, tile);
  1230. /* now link IC PP-->MEM to MEM-->IC PP ROT */
  1231. ipu_idmac_link(chan->out_chan, chan->rotation_in_chan);
  1232. } else {
  1233. /* init the destination IC PP-->MEM IDMAC channel */
  1234. init_idmac_channel(ctx, chan->out_chan, d_image,
  1235. ctx->rot_mode, false, tile);
  1236. }
  1237. /* enable the IC */
  1238. ipu_ic_enable(chan->ic);
  1239. /* set buffers ready */
  1240. ipu_idmac_select_buffer(chan->in_chan, 0);
  1241. ipu_idmac_select_buffer(chan->out_chan, 0);
  1242. if (ipu_rot_mode_is_irt(ctx->rot_mode))
  1243. ipu_idmac_select_buffer(chan->rotation_out_chan, 0);
  1244. if (ctx->double_buffering) {
  1245. ipu_idmac_select_buffer(chan->in_chan, 1);
  1246. ipu_idmac_select_buffer(chan->out_chan, 1);
  1247. if (ipu_rot_mode_is_irt(ctx->rot_mode))
  1248. ipu_idmac_select_buffer(chan->rotation_out_chan, 1);
  1249. }
  1250. /* enable the channels! */
  1251. ipu_idmac_enable_channel(chan->in_chan);
  1252. ipu_idmac_enable_channel(chan->out_chan);
  1253. if (ipu_rot_mode_is_irt(ctx->rot_mode)) {
  1254. ipu_idmac_enable_channel(chan->rotation_in_chan);
  1255. ipu_idmac_enable_channel(chan->rotation_out_chan);
  1256. }
  1257. ipu_ic_task_enable(chan->ic);
  1258. ipu_cpmem_dump(chan->in_chan);
  1259. ipu_cpmem_dump(chan->out_chan);
  1260. if (ipu_rot_mode_is_irt(ctx->rot_mode)) {
  1261. ipu_cpmem_dump(chan->rotation_in_chan);
  1262. ipu_cpmem_dump(chan->rotation_out_chan);
  1263. }
  1264. ipu_dump(priv->ipu);
  1265. return 0;
  1266. }
  1267. /* hold irqlock when calling */
  1268. static int do_run(struct ipu_image_convert_run *run)
  1269. {
  1270. struct ipu_image_convert_ctx *ctx = run->ctx;
  1271. struct ipu_image_convert_chan *chan = ctx->chan;
  1272. lockdep_assert_held(&chan->irqlock);
  1273. ctx->in.base.phys0 = run->in_phys;
  1274. ctx->out.base.phys0 = run->out_phys;
  1275. ctx->cur_buf_num = 0;
  1276. ctx->next_tile = 1;
  1277. /* remove run from pending_q and set as current */
  1278. list_del(&run->list);
  1279. chan->current_run = run;
  1280. return convert_start(run, 0);
  1281. }
  1282. /* hold irqlock when calling */
  1283. static void run_next(struct ipu_image_convert_chan *chan)
  1284. {
  1285. struct ipu_image_convert_priv *priv = chan->priv;
  1286. struct ipu_image_convert_run *run, *tmp;
  1287. int ret;
  1288. lockdep_assert_held(&chan->irqlock);
  1289. list_for_each_entry_safe(run, tmp, &chan->pending_q, list) {
  1290. /* skip contexts that are aborting */
  1291. if (run->ctx->aborting) {
  1292. dev_dbg(priv->ipu->dev,
  1293. "%s: task %u: skipping aborting ctx %p run %p\n",
  1294. __func__, chan->ic_task, run->ctx, run);
  1295. continue;
  1296. }
  1297. ret = do_run(run);
  1298. if (!ret)
  1299. break;
  1300. /*
  1301. * something went wrong with start, add the run
  1302. * to done q and continue to the next run in the
  1303. * pending q.
  1304. */
  1305. run->status = ret;
  1306. list_add_tail(&run->list, &chan->done_q);
  1307. chan->current_run = NULL;
  1308. }
  1309. }
  1310. static void empty_done_q(struct ipu_image_convert_chan *chan)
  1311. {
  1312. struct ipu_image_convert_priv *priv = chan->priv;
  1313. struct ipu_image_convert_run *run;
  1314. unsigned long flags;
  1315. spin_lock_irqsave(&chan->irqlock, flags);
  1316. while (!list_empty(&chan->done_q)) {
  1317. run = list_entry(chan->done_q.next,
  1318. struct ipu_image_convert_run,
  1319. list);
  1320. list_del(&run->list);
  1321. dev_dbg(priv->ipu->dev,
  1322. "%s: task %u: completing ctx %p run %p with %d\n",
  1323. __func__, chan->ic_task, run->ctx, run, run->status);
  1324. /* call the completion callback and free the run */
  1325. spin_unlock_irqrestore(&chan->irqlock, flags);
  1326. run->ctx->complete(run, run->ctx->complete_context);
  1327. spin_lock_irqsave(&chan->irqlock, flags);
  1328. }
  1329. spin_unlock_irqrestore(&chan->irqlock, flags);
  1330. }
  1331. /*
  1332. * the bottom half thread clears out the done_q, calling the
  1333. * completion handler for each.
  1334. */
  1335. static irqreturn_t do_bh(int irq, void *dev_id)
  1336. {
  1337. struct ipu_image_convert_chan *chan = dev_id;
  1338. struct ipu_image_convert_priv *priv = chan->priv;
  1339. struct ipu_image_convert_ctx *ctx;
  1340. unsigned long flags;
  1341. dev_dbg(priv->ipu->dev, "%s: task %u: enter\n", __func__,
  1342. chan->ic_task);
  1343. empty_done_q(chan);
  1344. spin_lock_irqsave(&chan->irqlock, flags);
  1345. /*
  1346. * the done_q is cleared out, signal any contexts
  1347. * that are aborting that abort can complete.
  1348. */
  1349. list_for_each_entry(ctx, &chan->ctx_list, list) {
  1350. if (ctx->aborting) {
  1351. dev_dbg(priv->ipu->dev,
  1352. "%s: task %u: signaling abort for ctx %p\n",
  1353. __func__, chan->ic_task, ctx);
  1354. complete_all(&ctx->aborted);
  1355. }
  1356. }
  1357. spin_unlock_irqrestore(&chan->irqlock, flags);
  1358. dev_dbg(priv->ipu->dev, "%s: task %u: exit\n", __func__,
  1359. chan->ic_task);
  1360. return IRQ_HANDLED;
  1361. }
  1362. static bool ic_settings_changed(struct ipu_image_convert_ctx *ctx)
  1363. {
  1364. unsigned int cur_tile = ctx->next_tile - 1;
  1365. unsigned int next_tile = ctx->next_tile;
  1366. if (ctx->resize_coeffs_h[cur_tile % ctx->in.num_cols] !=
  1367. ctx->resize_coeffs_h[next_tile % ctx->in.num_cols] ||
  1368. ctx->resize_coeffs_v[cur_tile / ctx->in.num_cols] !=
  1369. ctx->resize_coeffs_v[next_tile / ctx->in.num_cols] ||
  1370. ctx->in.tile[cur_tile].width != ctx->in.tile[next_tile].width ||
  1371. ctx->in.tile[cur_tile].height != ctx->in.tile[next_tile].height ||
  1372. ctx->out.tile[cur_tile].width != ctx->out.tile[next_tile].width ||
  1373. ctx->out.tile[cur_tile].height != ctx->out.tile[next_tile].height)
  1374. return true;
  1375. return false;
  1376. }
  1377. /* hold irqlock when calling */
  1378. static irqreturn_t do_tile_complete(struct ipu_image_convert_run *run)
  1379. {
  1380. struct ipu_image_convert_ctx *ctx = run->ctx;
  1381. struct ipu_image_convert_chan *chan = ctx->chan;
  1382. struct ipu_image_tile *src_tile, *dst_tile;
  1383. struct ipu_image_convert_image *s_image = &ctx->in;
  1384. struct ipu_image_convert_image *d_image = &ctx->out;
  1385. struct ipuv3_channel *outch;
  1386. unsigned int dst_idx;
  1387. lockdep_assert_held(&chan->irqlock);
  1388. outch = ipu_rot_mode_is_irt(ctx->rot_mode) ?
  1389. chan->rotation_out_chan : chan->out_chan;
  1390. /*
  1391. * It is difficult to stop the channel DMA before the channels
  1392. * enter the paused state. Without double-buffering the channels
  1393. * are always in a paused state when the EOF irq occurs, so it
  1394. * is safe to stop the channels now. For double-buffering we
  1395. * just ignore the abort until the operation completes, when it
  1396. * is safe to shut down.
  1397. */
  1398. if (ctx->aborting && !ctx->double_buffering) {
  1399. convert_stop(run);
  1400. run->status = -EIO;
  1401. goto done;
  1402. }
  1403. if (ctx->next_tile == ctx->num_tiles) {
  1404. /*
  1405. * the conversion is complete
  1406. */
  1407. convert_stop(run);
  1408. run->status = 0;
  1409. goto done;
  1410. }
  1411. /*
  1412. * not done, place the next tile buffers.
  1413. */
  1414. if (!ctx->double_buffering) {
  1415. if (ic_settings_changed(ctx)) {
  1416. convert_stop(run);
  1417. convert_start(run, ctx->next_tile);
  1418. } else {
  1419. src_tile = &s_image->tile[ctx->next_tile];
  1420. dst_idx = ctx->out_tile_map[ctx->next_tile];
  1421. dst_tile = &d_image->tile[dst_idx];
  1422. ipu_cpmem_set_buffer(chan->in_chan, 0,
  1423. s_image->base.phys0 +
  1424. src_tile->offset);
  1425. ipu_cpmem_set_buffer(outch, 0,
  1426. d_image->base.phys0 +
  1427. dst_tile->offset);
  1428. if (s_image->fmt->planar)
  1429. ipu_cpmem_set_uv_offset(chan->in_chan,
  1430. src_tile->u_off,
  1431. src_tile->v_off);
  1432. if (d_image->fmt->planar)
  1433. ipu_cpmem_set_uv_offset(outch,
  1434. dst_tile->u_off,
  1435. dst_tile->v_off);
  1436. ipu_idmac_select_buffer(chan->in_chan, 0);
  1437. ipu_idmac_select_buffer(outch, 0);
  1438. }
  1439. } else if (ctx->next_tile < ctx->num_tiles - 1) {
  1440. src_tile = &s_image->tile[ctx->next_tile + 1];
  1441. dst_idx = ctx->out_tile_map[ctx->next_tile + 1];
  1442. dst_tile = &d_image->tile[dst_idx];
  1443. ipu_cpmem_set_buffer(chan->in_chan, ctx->cur_buf_num,
  1444. s_image->base.phys0 + src_tile->offset);
  1445. ipu_cpmem_set_buffer(outch, ctx->cur_buf_num,
  1446. d_image->base.phys0 + dst_tile->offset);
  1447. ipu_idmac_select_buffer(chan->in_chan, ctx->cur_buf_num);
  1448. ipu_idmac_select_buffer(outch, ctx->cur_buf_num);
  1449. ctx->cur_buf_num ^= 1;
  1450. }
  1451. ctx->eof_mask = 0; /* clear EOF irq mask for next tile */
  1452. ctx->next_tile++;
  1453. return IRQ_HANDLED;
  1454. done:
  1455. list_add_tail(&run->list, &chan->done_q);
  1456. chan->current_run = NULL;
  1457. run_next(chan);
  1458. return IRQ_WAKE_THREAD;
  1459. }
  1460. static irqreturn_t eof_irq(int irq, void *data)
  1461. {
  1462. struct ipu_image_convert_chan *chan = data;
  1463. struct ipu_image_convert_priv *priv = chan->priv;
  1464. struct ipu_image_convert_ctx *ctx;
  1465. struct ipu_image_convert_run *run;
  1466. irqreturn_t ret = IRQ_HANDLED;
  1467. bool tile_complete = false;
  1468. unsigned long flags;
  1469. spin_lock_irqsave(&chan->irqlock, flags);
  1470. /* get current run and its context */
  1471. run = chan->current_run;
  1472. if (!run) {
  1473. ret = IRQ_NONE;
  1474. goto out;
  1475. }
  1476. ctx = run->ctx;
  1477. if (irq == chan->in_eof_irq) {
  1478. ctx->eof_mask |= EOF_IRQ_IN;
  1479. } else if (irq == chan->out_eof_irq) {
  1480. ctx->eof_mask |= EOF_IRQ_OUT;
  1481. } else if (irq == chan->rot_in_eof_irq ||
  1482. irq == chan->rot_out_eof_irq) {
  1483. if (!ipu_rot_mode_is_irt(ctx->rot_mode)) {
  1484. /* this was NOT a rotation op, shouldn't happen */
  1485. dev_err(priv->ipu->dev,
  1486. "Unexpected rotation interrupt\n");
  1487. goto out;
  1488. }
  1489. ctx->eof_mask |= (irq == chan->rot_in_eof_irq) ?
  1490. EOF_IRQ_ROT_IN : EOF_IRQ_ROT_OUT;
  1491. } else {
  1492. dev_err(priv->ipu->dev, "Received unknown irq %d\n", irq);
  1493. ret = IRQ_NONE;
  1494. goto out;
  1495. }
  1496. if (ipu_rot_mode_is_irt(ctx->rot_mode))
  1497. tile_complete = (ctx->eof_mask == EOF_IRQ_ROT_COMPLETE);
  1498. else
  1499. tile_complete = (ctx->eof_mask == EOF_IRQ_COMPLETE);
  1500. if (tile_complete)
  1501. ret = do_tile_complete(run);
  1502. out:
  1503. spin_unlock_irqrestore(&chan->irqlock, flags);
  1504. return ret;
  1505. }
  1506. /*
  1507. * try to force the completion of runs for this ctx. Called when
  1508. * abort wait times out in ipu_image_convert_abort().
  1509. */
  1510. static void force_abort(struct ipu_image_convert_ctx *ctx)
  1511. {
  1512. struct ipu_image_convert_chan *chan = ctx->chan;
  1513. struct ipu_image_convert_run *run;
  1514. unsigned long flags;
  1515. spin_lock_irqsave(&chan->irqlock, flags);
  1516. run = chan->current_run;
  1517. if (run && run->ctx == ctx) {
  1518. convert_stop(run);
  1519. run->status = -EIO;
  1520. list_add_tail(&run->list, &chan->done_q);
  1521. chan->current_run = NULL;
  1522. run_next(chan);
  1523. }
  1524. spin_unlock_irqrestore(&chan->irqlock, flags);
  1525. empty_done_q(chan);
  1526. }
  1527. static void release_ipu_resources(struct ipu_image_convert_chan *chan)
  1528. {
  1529. if (chan->in_eof_irq >= 0)
  1530. free_irq(chan->in_eof_irq, chan);
  1531. if (chan->rot_in_eof_irq >= 0)
  1532. free_irq(chan->rot_in_eof_irq, chan);
  1533. if (chan->out_eof_irq >= 0)
  1534. free_irq(chan->out_eof_irq, chan);
  1535. if (chan->rot_out_eof_irq >= 0)
  1536. free_irq(chan->rot_out_eof_irq, chan);
  1537. if (!IS_ERR_OR_NULL(chan->in_chan))
  1538. ipu_idmac_put(chan->in_chan);
  1539. if (!IS_ERR_OR_NULL(chan->out_chan))
  1540. ipu_idmac_put(chan->out_chan);
  1541. if (!IS_ERR_OR_NULL(chan->rotation_in_chan))
  1542. ipu_idmac_put(chan->rotation_in_chan);
  1543. if (!IS_ERR_OR_NULL(chan->rotation_out_chan))
  1544. ipu_idmac_put(chan->rotation_out_chan);
  1545. if (!IS_ERR_OR_NULL(chan->ic))
  1546. ipu_ic_put(chan->ic);
  1547. chan->in_chan = chan->out_chan = chan->rotation_in_chan =
  1548. chan->rotation_out_chan = NULL;
  1549. chan->in_eof_irq = -1;
  1550. chan->rot_in_eof_irq = -1;
  1551. chan->out_eof_irq = -1;
  1552. chan->rot_out_eof_irq = -1;
  1553. }
  1554. static int get_eof_irq(struct ipu_image_convert_chan *chan,
  1555. struct ipuv3_channel *channel)
  1556. {
  1557. struct ipu_image_convert_priv *priv = chan->priv;
  1558. int ret, irq;
  1559. irq = ipu_idmac_channel_irq(priv->ipu, channel, IPU_IRQ_EOF);
  1560. ret = request_threaded_irq(irq, eof_irq, do_bh, 0, "ipu-ic", chan);
  1561. if (ret < 0) {
  1562. dev_err(priv->ipu->dev, "could not acquire irq %d\n", irq);
  1563. return ret;
  1564. }
  1565. return irq;
  1566. }
  1567. static int get_ipu_resources(struct ipu_image_convert_chan *chan)
  1568. {
  1569. const struct ipu_image_convert_dma_chan *dma = chan->dma_ch;
  1570. struct ipu_image_convert_priv *priv = chan->priv;
  1571. int ret;
  1572. /* get IC */
  1573. chan->ic = ipu_ic_get(priv->ipu, chan->ic_task);
  1574. if (IS_ERR(chan->ic)) {
  1575. dev_err(priv->ipu->dev, "could not acquire IC\n");
  1576. ret = PTR_ERR(chan->ic);
  1577. goto err;
  1578. }
  1579. /* get IDMAC channels */
  1580. chan->in_chan = ipu_idmac_get(priv->ipu, dma->in);
  1581. chan->out_chan = ipu_idmac_get(priv->ipu, dma->out);
  1582. if (IS_ERR(chan->in_chan) || IS_ERR(chan->out_chan)) {
  1583. dev_err(priv->ipu->dev, "could not acquire idmac channels\n");
  1584. ret = -EBUSY;
  1585. goto err;
  1586. }
  1587. chan->rotation_in_chan = ipu_idmac_get(priv->ipu, dma->rot_in);
  1588. chan->rotation_out_chan = ipu_idmac_get(priv->ipu, dma->rot_out);
  1589. if (IS_ERR(chan->rotation_in_chan) || IS_ERR(chan->rotation_out_chan)) {
  1590. dev_err(priv->ipu->dev,
  1591. "could not acquire idmac rotation channels\n");
  1592. ret = -EBUSY;
  1593. goto err;
  1594. }
  1595. /* acquire the EOF interrupts */
  1596. ret = get_eof_irq(chan, chan->in_chan);
  1597. if (ret < 0) {
  1598. chan->in_eof_irq = -1;
  1599. goto err;
  1600. }
  1601. chan->in_eof_irq = ret;
  1602. ret = get_eof_irq(chan, chan->rotation_in_chan);
  1603. if (ret < 0) {
  1604. chan->rot_in_eof_irq = -1;
  1605. goto err;
  1606. }
  1607. chan->rot_in_eof_irq = ret;
  1608. ret = get_eof_irq(chan, chan->out_chan);
  1609. if (ret < 0) {
  1610. chan->out_eof_irq = -1;
  1611. goto err;
  1612. }
  1613. chan->out_eof_irq = ret;
  1614. ret = get_eof_irq(chan, chan->rotation_out_chan);
  1615. if (ret < 0) {
  1616. chan->rot_out_eof_irq = -1;
  1617. goto err;
  1618. }
  1619. chan->rot_out_eof_irq = ret;
  1620. return 0;
  1621. err:
  1622. release_ipu_resources(chan);
  1623. return ret;
  1624. }
  1625. static int fill_image(struct ipu_image_convert_ctx *ctx,
  1626. struct ipu_image_convert_image *ic_image,
  1627. struct ipu_image *image,
  1628. enum ipu_image_convert_type type)
  1629. {
  1630. struct ipu_image_convert_priv *priv = ctx->chan->priv;
  1631. ic_image->base = *image;
  1632. ic_image->type = type;
  1633. ic_image->fmt = get_format(image->pix.pixelformat);
  1634. if (!ic_image->fmt) {
  1635. dev_err(priv->ipu->dev, "pixelformat not supported for %s\n",
  1636. type == IMAGE_CONVERT_OUT ? "Output" : "Input");
  1637. return -EINVAL;
  1638. }
  1639. if (ic_image->fmt->planar)
  1640. ic_image->stride = ic_image->base.pix.width;
  1641. else
  1642. ic_image->stride = ic_image->base.pix.bytesperline;
  1643. return 0;
  1644. }
  1645. /* borrowed from drivers/media/v4l2-core/v4l2-common.c */
  1646. static unsigned int clamp_align(unsigned int x, unsigned int min,
  1647. unsigned int max, unsigned int align)
  1648. {
  1649. /* Bits that must be zero to be aligned */
  1650. unsigned int mask = ~((1 << align) - 1);
  1651. /* Clamp to aligned min and max */
  1652. x = clamp(x, (min + ~mask) & mask, max & mask);
  1653. /* Round to nearest aligned value */
  1654. if (align)
  1655. x = (x + (1 << (align - 1))) & mask;
  1656. return x;
  1657. }
  1658. /* Adjusts input/output images to IPU restrictions */
  1659. void ipu_image_convert_adjust(struct ipu_image *in, struct ipu_image *out,
  1660. enum ipu_rotate_mode rot_mode)
  1661. {
  1662. const struct ipu_image_pixfmt *infmt, *outfmt;
  1663. u32 w_align_out, h_align_out;
  1664. u32 w_align_in, h_align_in;
  1665. infmt = get_format(in->pix.pixelformat);
  1666. outfmt = get_format(out->pix.pixelformat);
  1667. /* set some default pixel formats if needed */
  1668. if (!infmt) {
  1669. in->pix.pixelformat = V4L2_PIX_FMT_RGB24;
  1670. infmt = get_format(V4L2_PIX_FMT_RGB24);
  1671. }
  1672. if (!outfmt) {
  1673. out->pix.pixelformat = V4L2_PIX_FMT_RGB24;
  1674. outfmt = get_format(V4L2_PIX_FMT_RGB24);
  1675. }
  1676. /* image converter does not handle fields */
  1677. in->pix.field = out->pix.field = V4L2_FIELD_NONE;
  1678. /* resizer cannot downsize more than 4:1 */
  1679. if (ipu_rot_mode_is_irt(rot_mode)) {
  1680. out->pix.height = max_t(__u32, out->pix.height,
  1681. in->pix.width / 4);
  1682. out->pix.width = max_t(__u32, out->pix.width,
  1683. in->pix.height / 4);
  1684. } else {
  1685. out->pix.width = max_t(__u32, out->pix.width,
  1686. in->pix.width / 4);
  1687. out->pix.height = max_t(__u32, out->pix.height,
  1688. in->pix.height / 4);
  1689. }
  1690. /* align input width/height */
  1691. w_align_in = ilog2(tile_width_align(IMAGE_CONVERT_IN, infmt,
  1692. rot_mode));
  1693. h_align_in = ilog2(tile_height_align(IMAGE_CONVERT_IN, infmt,
  1694. rot_mode));
  1695. in->pix.width = clamp_align(in->pix.width, MIN_W, MAX_W,
  1696. w_align_in);
  1697. in->pix.height = clamp_align(in->pix.height, MIN_H, MAX_H,
  1698. h_align_in);
  1699. /* align output width/height */
  1700. w_align_out = ilog2(tile_width_align(IMAGE_CONVERT_OUT, outfmt,
  1701. rot_mode));
  1702. h_align_out = ilog2(tile_height_align(IMAGE_CONVERT_OUT, outfmt,
  1703. rot_mode));
  1704. out->pix.width = clamp_align(out->pix.width, MIN_W, MAX_W,
  1705. w_align_out);
  1706. out->pix.height = clamp_align(out->pix.height, MIN_H, MAX_H,
  1707. h_align_out);
  1708. /* set input/output strides and image sizes */
  1709. in->pix.bytesperline = infmt->planar ?
  1710. clamp_align(in->pix.width, 2 << w_align_in, MAX_W,
  1711. w_align_in) :
  1712. clamp_align((in->pix.width * infmt->bpp) >> 3,
  1713. ((2 << w_align_in) * infmt->bpp) >> 3,
  1714. (MAX_W * infmt->bpp) >> 3,
  1715. w_align_in);
  1716. in->pix.sizeimage = infmt->planar ?
  1717. (in->pix.height * in->pix.bytesperline * infmt->bpp) >> 3 :
  1718. in->pix.height * in->pix.bytesperline;
  1719. out->pix.bytesperline = outfmt->planar ? out->pix.width :
  1720. (out->pix.width * outfmt->bpp) >> 3;
  1721. out->pix.sizeimage = outfmt->planar ?
  1722. (out->pix.height * out->pix.bytesperline * outfmt->bpp) >> 3 :
  1723. out->pix.height * out->pix.bytesperline;
  1724. }
  1725. EXPORT_SYMBOL_GPL(ipu_image_convert_adjust);
  1726. /*
  1727. * this is used by ipu_image_convert_prepare() to verify set input and
  1728. * output images are valid before starting the conversion. Clients can
  1729. * also call it before calling ipu_image_convert_prepare().
  1730. */
  1731. int ipu_image_convert_verify(struct ipu_image *in, struct ipu_image *out,
  1732. enum ipu_rotate_mode rot_mode)
  1733. {
  1734. struct ipu_image testin, testout;
  1735. testin = *in;
  1736. testout = *out;
  1737. ipu_image_convert_adjust(&testin, &testout, rot_mode);
  1738. if (testin.pix.width != in->pix.width ||
  1739. testin.pix.height != in->pix.height ||
  1740. testout.pix.width != out->pix.width ||
  1741. testout.pix.height != out->pix.height)
  1742. return -EINVAL;
  1743. return 0;
  1744. }
  1745. EXPORT_SYMBOL_GPL(ipu_image_convert_verify);
  1746. /*
  1747. * Call ipu_image_convert_prepare() to prepare for the conversion of
  1748. * given images and rotation mode. Returns a new conversion context.
  1749. */
  1750. struct ipu_image_convert_ctx *
  1751. ipu_image_convert_prepare(struct ipu_soc *ipu, enum ipu_ic_task ic_task,
  1752. struct ipu_image *in, struct ipu_image *out,
  1753. enum ipu_rotate_mode rot_mode,
  1754. ipu_image_convert_cb_t complete,
  1755. void *complete_context)
  1756. {
  1757. struct ipu_image_convert_priv *priv = ipu->image_convert_priv;
  1758. struct ipu_image_convert_image *s_image, *d_image;
  1759. struct ipu_image_convert_chan *chan;
  1760. struct ipu_image_convert_ctx *ctx;
  1761. unsigned long flags;
  1762. unsigned int i;
  1763. bool get_res;
  1764. int ret;
  1765. if (!in || !out || !complete ||
  1766. (ic_task != IC_TASK_VIEWFINDER &&
  1767. ic_task != IC_TASK_POST_PROCESSOR))
  1768. return ERR_PTR(-EINVAL);
  1769. /* verify the in/out images before continuing */
  1770. ret = ipu_image_convert_verify(in, out, rot_mode);
  1771. if (ret) {
  1772. dev_err(priv->ipu->dev, "%s: in/out formats invalid\n",
  1773. __func__);
  1774. return ERR_PTR(ret);
  1775. }
  1776. chan = &priv->chan[ic_task];
  1777. ctx = kzalloc_obj(*ctx);
  1778. if (!ctx)
  1779. return ERR_PTR(-ENOMEM);
  1780. dev_dbg(priv->ipu->dev, "%s: task %u: ctx %p\n", __func__,
  1781. chan->ic_task, ctx);
  1782. ctx->chan = chan;
  1783. init_completion(&ctx->aborted);
  1784. ctx->rot_mode = rot_mode;
  1785. /* Sets ctx->in.num_rows/cols as well */
  1786. ret = calc_image_resize_coefficients(ctx, in, out);
  1787. if (ret)
  1788. goto out_free;
  1789. s_image = &ctx->in;
  1790. d_image = &ctx->out;
  1791. /* set tiling and rotation */
  1792. if (ipu_rot_mode_is_irt(rot_mode)) {
  1793. d_image->num_rows = s_image->num_cols;
  1794. d_image->num_cols = s_image->num_rows;
  1795. } else {
  1796. d_image->num_rows = s_image->num_rows;
  1797. d_image->num_cols = s_image->num_cols;
  1798. }
  1799. ctx->num_tiles = d_image->num_cols * d_image->num_rows;
  1800. ret = fill_image(ctx, s_image, in, IMAGE_CONVERT_IN);
  1801. if (ret)
  1802. goto out_free;
  1803. ret = fill_image(ctx, d_image, out, IMAGE_CONVERT_OUT);
  1804. if (ret)
  1805. goto out_free;
  1806. calc_out_tile_map(ctx);
  1807. find_seams(ctx, s_image, d_image);
  1808. ret = calc_tile_dimensions(ctx, s_image);
  1809. if (ret)
  1810. goto out_free;
  1811. ret = calc_tile_offsets(ctx, s_image);
  1812. if (ret)
  1813. goto out_free;
  1814. calc_tile_dimensions(ctx, d_image);
  1815. ret = calc_tile_offsets(ctx, d_image);
  1816. if (ret)
  1817. goto out_free;
  1818. calc_tile_resize_coefficients(ctx);
  1819. ret = ipu_ic_calc_csc(&ctx->csc,
  1820. s_image->base.pix.ycbcr_enc,
  1821. s_image->base.pix.quantization,
  1822. ipu_pixelformat_to_colorspace(s_image->fmt->fourcc),
  1823. d_image->base.pix.ycbcr_enc,
  1824. d_image->base.pix.quantization,
  1825. ipu_pixelformat_to_colorspace(d_image->fmt->fourcc));
  1826. if (ret)
  1827. goto out_free;
  1828. dump_format(ctx, s_image);
  1829. dump_format(ctx, d_image);
  1830. ctx->complete = complete;
  1831. ctx->complete_context = complete_context;
  1832. /*
  1833. * Can we use double-buffering for this operation? If there is
  1834. * only one tile (the whole image can be converted in a single
  1835. * operation) there's no point in using double-buffering. Also,
  1836. * the IPU's IDMAC channels allow only a single U and V plane
  1837. * offset shared between both buffers, but these offsets change
  1838. * for every tile, and therefore would have to be updated for
  1839. * each buffer which is not possible. So double-buffering is
  1840. * impossible when either the source or destination images are
  1841. * a planar format (YUV420, YUV422P, etc.). Further, differently
  1842. * sized tiles or different resizing coefficients per tile
  1843. * prevent double-buffering as well.
  1844. */
  1845. ctx->double_buffering = (ctx->num_tiles > 1 &&
  1846. !s_image->fmt->planar &&
  1847. !d_image->fmt->planar);
  1848. for (i = 1; i < ctx->num_tiles; i++) {
  1849. if (ctx->in.tile[i].width != ctx->in.tile[0].width ||
  1850. ctx->in.tile[i].height != ctx->in.tile[0].height ||
  1851. ctx->out.tile[i].width != ctx->out.tile[0].width ||
  1852. ctx->out.tile[i].height != ctx->out.tile[0].height) {
  1853. ctx->double_buffering = false;
  1854. break;
  1855. }
  1856. }
  1857. for (i = 1; i < ctx->in.num_cols; i++) {
  1858. if (ctx->resize_coeffs_h[i] != ctx->resize_coeffs_h[0]) {
  1859. ctx->double_buffering = false;
  1860. break;
  1861. }
  1862. }
  1863. for (i = 1; i < ctx->in.num_rows; i++) {
  1864. if (ctx->resize_coeffs_v[i] != ctx->resize_coeffs_v[0]) {
  1865. ctx->double_buffering = false;
  1866. break;
  1867. }
  1868. }
  1869. if (ipu_rot_mode_is_irt(ctx->rot_mode)) {
  1870. unsigned long intermediate_size = d_image->tile[0].size;
  1871. for (i = 1; i < ctx->num_tiles; i++) {
  1872. if (d_image->tile[i].size > intermediate_size)
  1873. intermediate_size = d_image->tile[i].size;
  1874. }
  1875. ret = alloc_dma_buf(priv, &ctx->rot_intermediate[0],
  1876. intermediate_size);
  1877. if (ret)
  1878. goto out_free;
  1879. if (ctx->double_buffering) {
  1880. ret = alloc_dma_buf(priv,
  1881. &ctx->rot_intermediate[1],
  1882. intermediate_size);
  1883. if (ret)
  1884. goto out_free_dmabuf0;
  1885. }
  1886. }
  1887. spin_lock_irqsave(&chan->irqlock, flags);
  1888. get_res = list_empty(&chan->ctx_list);
  1889. list_add_tail(&ctx->list, &chan->ctx_list);
  1890. spin_unlock_irqrestore(&chan->irqlock, flags);
  1891. if (get_res) {
  1892. ret = get_ipu_resources(chan);
  1893. if (ret)
  1894. goto out_free_dmabuf1;
  1895. }
  1896. return ctx;
  1897. out_free_dmabuf1:
  1898. free_dma_buf(priv, &ctx->rot_intermediate[1]);
  1899. spin_lock_irqsave(&chan->irqlock, flags);
  1900. list_del(&ctx->list);
  1901. spin_unlock_irqrestore(&chan->irqlock, flags);
  1902. out_free_dmabuf0:
  1903. free_dma_buf(priv, &ctx->rot_intermediate[0]);
  1904. out_free:
  1905. kfree(ctx);
  1906. return ERR_PTR(ret);
  1907. }
  1908. EXPORT_SYMBOL_GPL(ipu_image_convert_prepare);
  1909. /*
  1910. * Carry out a single image conversion run. Only the physaddr's of the input
  1911. * and output image buffers are needed. The conversion context must have
  1912. * been created previously with ipu_image_convert_prepare().
  1913. */
  1914. int ipu_image_convert_queue(struct ipu_image_convert_run *run)
  1915. {
  1916. struct ipu_image_convert_chan *chan;
  1917. struct ipu_image_convert_priv *priv;
  1918. struct ipu_image_convert_ctx *ctx;
  1919. unsigned long flags;
  1920. int ret = 0;
  1921. if (!run || !run->ctx || !run->in_phys || !run->out_phys)
  1922. return -EINVAL;
  1923. ctx = run->ctx;
  1924. chan = ctx->chan;
  1925. priv = chan->priv;
  1926. dev_dbg(priv->ipu->dev, "%s: task %u: ctx %p run %p\n", __func__,
  1927. chan->ic_task, ctx, run);
  1928. INIT_LIST_HEAD(&run->list);
  1929. spin_lock_irqsave(&chan->irqlock, flags);
  1930. if (ctx->aborting) {
  1931. ret = -EIO;
  1932. goto unlock;
  1933. }
  1934. list_add_tail(&run->list, &chan->pending_q);
  1935. if (!chan->current_run) {
  1936. ret = do_run(run);
  1937. if (ret)
  1938. chan->current_run = NULL;
  1939. }
  1940. unlock:
  1941. spin_unlock_irqrestore(&chan->irqlock, flags);
  1942. return ret;
  1943. }
  1944. EXPORT_SYMBOL_GPL(ipu_image_convert_queue);
  1945. /* Abort any active or pending conversions for this context */
  1946. static void __ipu_image_convert_abort(struct ipu_image_convert_ctx *ctx)
  1947. {
  1948. struct ipu_image_convert_chan *chan = ctx->chan;
  1949. struct ipu_image_convert_priv *priv = chan->priv;
  1950. struct ipu_image_convert_run *run, *active_run, *tmp;
  1951. unsigned long flags;
  1952. int run_count, ret;
  1953. spin_lock_irqsave(&chan->irqlock, flags);
  1954. /* move all remaining pending runs in this context to done_q */
  1955. list_for_each_entry_safe(run, tmp, &chan->pending_q, list) {
  1956. if (run->ctx != ctx)
  1957. continue;
  1958. run->status = -EIO;
  1959. list_move_tail(&run->list, &chan->done_q);
  1960. }
  1961. run_count = get_run_count(ctx, &chan->done_q);
  1962. active_run = (chan->current_run && chan->current_run->ctx == ctx) ?
  1963. chan->current_run : NULL;
  1964. if (active_run)
  1965. reinit_completion(&ctx->aborted);
  1966. ctx->aborting = true;
  1967. spin_unlock_irqrestore(&chan->irqlock, flags);
  1968. if (!run_count && !active_run) {
  1969. dev_dbg(priv->ipu->dev,
  1970. "%s: task %u: no abort needed for ctx %p\n",
  1971. __func__, chan->ic_task, ctx);
  1972. return;
  1973. }
  1974. if (!active_run) {
  1975. empty_done_q(chan);
  1976. return;
  1977. }
  1978. dev_dbg(priv->ipu->dev,
  1979. "%s: task %u: wait for completion: %d runs\n",
  1980. __func__, chan->ic_task, run_count);
  1981. ret = wait_for_completion_timeout(&ctx->aborted,
  1982. msecs_to_jiffies(10000));
  1983. if (ret == 0) {
  1984. dev_warn(priv->ipu->dev, "%s: timeout\n", __func__);
  1985. force_abort(ctx);
  1986. }
  1987. }
  1988. void ipu_image_convert_abort(struct ipu_image_convert_ctx *ctx)
  1989. {
  1990. __ipu_image_convert_abort(ctx);
  1991. ctx->aborting = false;
  1992. }
  1993. EXPORT_SYMBOL_GPL(ipu_image_convert_abort);
  1994. /* Unprepare image conversion context */
  1995. void ipu_image_convert_unprepare(struct ipu_image_convert_ctx *ctx)
  1996. {
  1997. struct ipu_image_convert_chan *chan = ctx->chan;
  1998. struct ipu_image_convert_priv *priv = chan->priv;
  1999. unsigned long flags;
  2000. bool put_res;
  2001. /* make sure no runs are hanging around */
  2002. __ipu_image_convert_abort(ctx);
  2003. dev_dbg(priv->ipu->dev, "%s: task %u: removing ctx %p\n", __func__,
  2004. chan->ic_task, ctx);
  2005. spin_lock_irqsave(&chan->irqlock, flags);
  2006. list_del(&ctx->list);
  2007. put_res = list_empty(&chan->ctx_list);
  2008. spin_unlock_irqrestore(&chan->irqlock, flags);
  2009. if (put_res)
  2010. release_ipu_resources(chan);
  2011. free_dma_buf(priv, &ctx->rot_intermediate[1]);
  2012. free_dma_buf(priv, &ctx->rot_intermediate[0]);
  2013. kfree(ctx);
  2014. }
  2015. EXPORT_SYMBOL_GPL(ipu_image_convert_unprepare);
  2016. /*
  2017. * "Canned" asynchronous single image conversion. Allocates and returns
  2018. * a new conversion run. On successful return the caller must free the
  2019. * run and call ipu_image_convert_unprepare() after conversion completes.
  2020. */
  2021. struct ipu_image_convert_run *
  2022. ipu_image_convert(struct ipu_soc *ipu, enum ipu_ic_task ic_task,
  2023. struct ipu_image *in, struct ipu_image *out,
  2024. enum ipu_rotate_mode rot_mode,
  2025. ipu_image_convert_cb_t complete,
  2026. void *complete_context)
  2027. {
  2028. struct ipu_image_convert_ctx *ctx;
  2029. struct ipu_image_convert_run *run;
  2030. int ret;
  2031. ctx = ipu_image_convert_prepare(ipu, ic_task, in, out, rot_mode,
  2032. complete, complete_context);
  2033. if (IS_ERR(ctx))
  2034. return ERR_CAST(ctx);
  2035. run = kzalloc_obj(*run);
  2036. if (!run) {
  2037. ipu_image_convert_unprepare(ctx);
  2038. return ERR_PTR(-ENOMEM);
  2039. }
  2040. run->ctx = ctx;
  2041. run->in_phys = in->phys0;
  2042. run->out_phys = out->phys0;
  2043. ret = ipu_image_convert_queue(run);
  2044. if (ret) {
  2045. ipu_image_convert_unprepare(ctx);
  2046. kfree(run);
  2047. return ERR_PTR(ret);
  2048. }
  2049. return run;
  2050. }
  2051. EXPORT_SYMBOL_GPL(ipu_image_convert);
  2052. int ipu_image_convert_init(struct ipu_soc *ipu, struct device *dev)
  2053. {
  2054. struct ipu_image_convert_priv *priv;
  2055. int i;
  2056. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  2057. if (!priv)
  2058. return -ENOMEM;
  2059. ipu->image_convert_priv = priv;
  2060. priv->ipu = ipu;
  2061. for (i = 0; i < IC_NUM_TASKS; i++) {
  2062. struct ipu_image_convert_chan *chan = &priv->chan[i];
  2063. chan->ic_task = i;
  2064. chan->priv = priv;
  2065. chan->dma_ch = &image_convert_dma_chan[i];
  2066. chan->in_eof_irq = -1;
  2067. chan->rot_in_eof_irq = -1;
  2068. chan->out_eof_irq = -1;
  2069. chan->rot_out_eof_irq = -1;
  2070. spin_lock_init(&chan->irqlock);
  2071. INIT_LIST_HEAD(&chan->ctx_list);
  2072. INIT_LIST_HEAD(&chan->pending_q);
  2073. INIT_LIST_HEAD(&chan->done_q);
  2074. }
  2075. return 0;
  2076. }
  2077. void ipu_image_convert_exit(struct ipu_soc *ipu)
  2078. {
  2079. }