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