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