162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2012-2016 Mentor Graphics Inc.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Queued image conversion support, with tiling and rotation.
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include <linux/interrupt.h>
962306a36Sopenharmony_ci#include <linux/dma-mapping.h>
1062306a36Sopenharmony_ci#include <linux/math.h>
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci#include <video/imx-ipu-image-convert.h>
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci#include "ipu-prv.h"
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci/*
1762306a36Sopenharmony_ci * The IC Resizer has a restriction that the output frame from the
1862306a36Sopenharmony_ci * resizer must be 1024 or less in both width (pixels) and height
1962306a36Sopenharmony_ci * (lines).
2062306a36Sopenharmony_ci *
2162306a36Sopenharmony_ci * The image converter attempts to split up a conversion when
2262306a36Sopenharmony_ci * the desired output (converted) frame resolution exceeds the
2362306a36Sopenharmony_ci * IC resizer limit of 1024 in either dimension.
2462306a36Sopenharmony_ci *
2562306a36Sopenharmony_ci * If either dimension of the output frame exceeds the limit, the
2662306a36Sopenharmony_ci * dimension is split into 1, 2, or 4 equal stripes, for a maximum
2762306a36Sopenharmony_ci * of 4*4 or 16 tiles. A conversion is then carried out for each
2862306a36Sopenharmony_ci * tile (but taking care to pass the full frame stride length to
2962306a36Sopenharmony_ci * the DMA channel's parameter memory!). IDMA double-buffering is used
3062306a36Sopenharmony_ci * to convert each tile back-to-back when possible (see note below
3162306a36Sopenharmony_ci * when double_buffering boolean is set).
3262306a36Sopenharmony_ci *
3362306a36Sopenharmony_ci * Note that the input frame must be split up into the same number
3462306a36Sopenharmony_ci * of tiles as the output frame:
3562306a36Sopenharmony_ci *
3662306a36Sopenharmony_ci *                       +---------+-----+
3762306a36Sopenharmony_ci *   +-----+---+         |  A      | B   |
3862306a36Sopenharmony_ci *   | A   | B |         |         |     |
3962306a36Sopenharmony_ci *   +-----+---+   -->   +---------+-----+
4062306a36Sopenharmony_ci *   | C   | D |         |  C      | D   |
4162306a36Sopenharmony_ci *   +-----+---+         |         |     |
4262306a36Sopenharmony_ci *                       +---------+-----+
4362306a36Sopenharmony_ci *
4462306a36Sopenharmony_ci * Clockwise 90° rotations are handled by first rescaling into a
4562306a36Sopenharmony_ci * reusable temporary tile buffer and then rotating with the 8x8
4662306a36Sopenharmony_ci * block rotator, writing to the correct destination:
4762306a36Sopenharmony_ci *
4862306a36Sopenharmony_ci *                                         +-----+-----+
4962306a36Sopenharmony_ci *                                         |     |     |
5062306a36Sopenharmony_ci *   +-----+---+         +---------+       | C   | A   |
5162306a36Sopenharmony_ci *   | A   | B |         | A,B, |  |       |     |     |
5262306a36Sopenharmony_ci *   +-----+---+   -->   | C,D  |  |  -->  |     |     |
5362306a36Sopenharmony_ci *   | C   | D |         +---------+       +-----+-----+
5462306a36Sopenharmony_ci *   +-----+---+                           | D   | B   |
5562306a36Sopenharmony_ci *                                         |     |     |
5662306a36Sopenharmony_ci *                                         +-----+-----+
5762306a36Sopenharmony_ci *
5862306a36Sopenharmony_ci * If the 8x8 block rotator is used, horizontal or vertical flipping
5962306a36Sopenharmony_ci * is done during the rotation step, otherwise flipping is done
6062306a36Sopenharmony_ci * during the scaling step.
6162306a36Sopenharmony_ci * With rotation or flipping, tile order changes between input and
6262306a36Sopenharmony_ci * output image. Tiles are numbered row major from top left to bottom
6362306a36Sopenharmony_ci * right for both input and output image.
6462306a36Sopenharmony_ci */
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci#define MAX_STRIPES_W    4
6762306a36Sopenharmony_ci#define MAX_STRIPES_H    4
6862306a36Sopenharmony_ci#define MAX_TILES (MAX_STRIPES_W * MAX_STRIPES_H)
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci#define MIN_W     16
7162306a36Sopenharmony_ci#define MIN_H     8
7262306a36Sopenharmony_ci#define MAX_W     4096
7362306a36Sopenharmony_ci#define MAX_H     4096
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_cienum ipu_image_convert_type {
7662306a36Sopenharmony_ci	IMAGE_CONVERT_IN = 0,
7762306a36Sopenharmony_ci	IMAGE_CONVERT_OUT,
7862306a36Sopenharmony_ci};
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_cistruct ipu_image_convert_dma_buf {
8162306a36Sopenharmony_ci	void          *virt;
8262306a36Sopenharmony_ci	dma_addr_t    phys;
8362306a36Sopenharmony_ci	unsigned long len;
8462306a36Sopenharmony_ci};
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_cistruct ipu_image_convert_dma_chan {
8762306a36Sopenharmony_ci	int in;
8862306a36Sopenharmony_ci	int out;
8962306a36Sopenharmony_ci	int rot_in;
9062306a36Sopenharmony_ci	int rot_out;
9162306a36Sopenharmony_ci	int vdi_in_p;
9262306a36Sopenharmony_ci	int vdi_in;
9362306a36Sopenharmony_ci	int vdi_in_n;
9462306a36Sopenharmony_ci};
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci/* dimensions of one tile */
9762306a36Sopenharmony_cistruct ipu_image_tile {
9862306a36Sopenharmony_ci	u32 width;
9962306a36Sopenharmony_ci	u32 height;
10062306a36Sopenharmony_ci	u32 left;
10162306a36Sopenharmony_ci	u32 top;
10262306a36Sopenharmony_ci	/* size and strides are in bytes */
10362306a36Sopenharmony_ci	u32 size;
10462306a36Sopenharmony_ci	u32 stride;
10562306a36Sopenharmony_ci	u32 rot_stride;
10662306a36Sopenharmony_ci	/* start Y or packed offset of this tile */
10762306a36Sopenharmony_ci	u32 offset;
10862306a36Sopenharmony_ci	/* offset from start to tile in U plane, for planar formats */
10962306a36Sopenharmony_ci	u32 u_off;
11062306a36Sopenharmony_ci	/* offset from start to tile in V plane, for planar formats */
11162306a36Sopenharmony_ci	u32 v_off;
11262306a36Sopenharmony_ci};
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_cistruct ipu_image_convert_image {
11562306a36Sopenharmony_ci	struct ipu_image base;
11662306a36Sopenharmony_ci	enum ipu_image_convert_type type;
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ci	const struct ipu_image_pixfmt *fmt;
11962306a36Sopenharmony_ci	unsigned int stride;
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci	/* # of rows (horizontal stripes) if dest height is > 1024 */
12262306a36Sopenharmony_ci	unsigned int num_rows;
12362306a36Sopenharmony_ci	/* # of columns (vertical stripes) if dest width is > 1024 */
12462306a36Sopenharmony_ci	unsigned int num_cols;
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_ci	struct ipu_image_tile tile[MAX_TILES];
12762306a36Sopenharmony_ci};
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_cistruct ipu_image_pixfmt {
13062306a36Sopenharmony_ci	u32	fourcc;        /* V4L2 fourcc */
13162306a36Sopenharmony_ci	int     bpp;           /* total bpp */
13262306a36Sopenharmony_ci	int     uv_width_dec;  /* decimation in width for U/V planes */
13362306a36Sopenharmony_ci	int     uv_height_dec; /* decimation in height for U/V planes */
13462306a36Sopenharmony_ci	bool    planar;        /* planar format */
13562306a36Sopenharmony_ci	bool    uv_swapped;    /* U and V planes are swapped */
13662306a36Sopenharmony_ci	bool    uv_packed;     /* partial planar (U and V in same plane) */
13762306a36Sopenharmony_ci};
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_cistruct ipu_image_convert_ctx;
14062306a36Sopenharmony_cistruct ipu_image_convert_chan;
14162306a36Sopenharmony_cistruct ipu_image_convert_priv;
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_cienum eof_irq_mask {
14462306a36Sopenharmony_ci	EOF_IRQ_IN      = BIT(0),
14562306a36Sopenharmony_ci	EOF_IRQ_ROT_IN  = BIT(1),
14662306a36Sopenharmony_ci	EOF_IRQ_OUT     = BIT(2),
14762306a36Sopenharmony_ci	EOF_IRQ_ROT_OUT = BIT(3),
14862306a36Sopenharmony_ci};
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ci#define EOF_IRQ_COMPLETE (EOF_IRQ_IN | EOF_IRQ_OUT)
15162306a36Sopenharmony_ci#define EOF_IRQ_ROT_COMPLETE (EOF_IRQ_IN | EOF_IRQ_OUT |	\
15262306a36Sopenharmony_ci			      EOF_IRQ_ROT_IN | EOF_IRQ_ROT_OUT)
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_cistruct ipu_image_convert_ctx {
15562306a36Sopenharmony_ci	struct ipu_image_convert_chan *chan;
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ci	ipu_image_convert_cb_t complete;
15862306a36Sopenharmony_ci	void *complete_context;
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ci	/* Source/destination image data and rotation mode */
16162306a36Sopenharmony_ci	struct ipu_image_convert_image in;
16262306a36Sopenharmony_ci	struct ipu_image_convert_image out;
16362306a36Sopenharmony_ci	struct ipu_ic_csc csc;
16462306a36Sopenharmony_ci	enum ipu_rotate_mode rot_mode;
16562306a36Sopenharmony_ci	u32 downsize_coeff_h;
16662306a36Sopenharmony_ci	u32 downsize_coeff_v;
16762306a36Sopenharmony_ci	u32 image_resize_coeff_h;
16862306a36Sopenharmony_ci	u32 image_resize_coeff_v;
16962306a36Sopenharmony_ci	u32 resize_coeffs_h[MAX_STRIPES_W];
17062306a36Sopenharmony_ci	u32 resize_coeffs_v[MAX_STRIPES_H];
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci	/* intermediate buffer for rotation */
17362306a36Sopenharmony_ci	struct ipu_image_convert_dma_buf rot_intermediate[2];
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ci	/* current buffer number for double buffering */
17662306a36Sopenharmony_ci	int cur_buf_num;
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ci	bool aborting;
17962306a36Sopenharmony_ci	struct completion aborted;
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_ci	/* can we use double-buffering for this conversion operation? */
18262306a36Sopenharmony_ci	bool double_buffering;
18362306a36Sopenharmony_ci	/* num_rows * num_cols */
18462306a36Sopenharmony_ci	unsigned int num_tiles;
18562306a36Sopenharmony_ci	/* next tile to process */
18662306a36Sopenharmony_ci	unsigned int next_tile;
18762306a36Sopenharmony_ci	/* where to place converted tile in dest image */
18862306a36Sopenharmony_ci	unsigned int out_tile_map[MAX_TILES];
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ci	/* mask of completed EOF irqs at every tile conversion */
19162306a36Sopenharmony_ci	enum eof_irq_mask eof_mask;
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_ci	struct list_head list;
19462306a36Sopenharmony_ci};
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_cistruct ipu_image_convert_chan {
19762306a36Sopenharmony_ci	struct ipu_image_convert_priv *priv;
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_ci	enum ipu_ic_task ic_task;
20062306a36Sopenharmony_ci	const struct ipu_image_convert_dma_chan *dma_ch;
20162306a36Sopenharmony_ci
20262306a36Sopenharmony_ci	struct ipu_ic *ic;
20362306a36Sopenharmony_ci	struct ipuv3_channel *in_chan;
20462306a36Sopenharmony_ci	struct ipuv3_channel *out_chan;
20562306a36Sopenharmony_ci	struct ipuv3_channel *rotation_in_chan;
20662306a36Sopenharmony_ci	struct ipuv3_channel *rotation_out_chan;
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_ci	/* the IPU end-of-frame irqs */
20962306a36Sopenharmony_ci	int in_eof_irq;
21062306a36Sopenharmony_ci	int rot_in_eof_irq;
21162306a36Sopenharmony_ci	int out_eof_irq;
21262306a36Sopenharmony_ci	int rot_out_eof_irq;
21362306a36Sopenharmony_ci
21462306a36Sopenharmony_ci	spinlock_t irqlock;
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_ci	/* list of convert contexts */
21762306a36Sopenharmony_ci	struct list_head ctx_list;
21862306a36Sopenharmony_ci	/* queue of conversion runs */
21962306a36Sopenharmony_ci	struct list_head pending_q;
22062306a36Sopenharmony_ci	/* queue of completed runs */
22162306a36Sopenharmony_ci	struct list_head done_q;
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ci	/* the current conversion run */
22462306a36Sopenharmony_ci	struct ipu_image_convert_run *current_run;
22562306a36Sopenharmony_ci};
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_cistruct ipu_image_convert_priv {
22862306a36Sopenharmony_ci	struct ipu_image_convert_chan chan[IC_NUM_TASKS];
22962306a36Sopenharmony_ci	struct ipu_soc *ipu;
23062306a36Sopenharmony_ci};
23162306a36Sopenharmony_ci
23262306a36Sopenharmony_cistatic const struct ipu_image_convert_dma_chan
23362306a36Sopenharmony_ciimage_convert_dma_chan[IC_NUM_TASKS] = {
23462306a36Sopenharmony_ci	[IC_TASK_VIEWFINDER] = {
23562306a36Sopenharmony_ci		.in = IPUV3_CHANNEL_MEM_IC_PRP_VF,
23662306a36Sopenharmony_ci		.out = IPUV3_CHANNEL_IC_PRP_VF_MEM,
23762306a36Sopenharmony_ci		.rot_in = IPUV3_CHANNEL_MEM_ROT_VF,
23862306a36Sopenharmony_ci		.rot_out = IPUV3_CHANNEL_ROT_VF_MEM,
23962306a36Sopenharmony_ci		.vdi_in_p = IPUV3_CHANNEL_MEM_VDI_PREV,
24062306a36Sopenharmony_ci		.vdi_in = IPUV3_CHANNEL_MEM_VDI_CUR,
24162306a36Sopenharmony_ci		.vdi_in_n = IPUV3_CHANNEL_MEM_VDI_NEXT,
24262306a36Sopenharmony_ci	},
24362306a36Sopenharmony_ci	[IC_TASK_POST_PROCESSOR] = {
24462306a36Sopenharmony_ci		.in = IPUV3_CHANNEL_MEM_IC_PP,
24562306a36Sopenharmony_ci		.out = IPUV3_CHANNEL_IC_PP_MEM,
24662306a36Sopenharmony_ci		.rot_in = IPUV3_CHANNEL_MEM_ROT_PP,
24762306a36Sopenharmony_ci		.rot_out = IPUV3_CHANNEL_ROT_PP_MEM,
24862306a36Sopenharmony_ci	},
24962306a36Sopenharmony_ci};
25062306a36Sopenharmony_ci
25162306a36Sopenharmony_cistatic const struct ipu_image_pixfmt image_convert_formats[] = {
25262306a36Sopenharmony_ci	{
25362306a36Sopenharmony_ci		.fourcc	= V4L2_PIX_FMT_RGB565,
25462306a36Sopenharmony_ci		.bpp    = 16,
25562306a36Sopenharmony_ci	}, {
25662306a36Sopenharmony_ci		.fourcc	= V4L2_PIX_FMT_RGB24,
25762306a36Sopenharmony_ci		.bpp    = 24,
25862306a36Sopenharmony_ci	}, {
25962306a36Sopenharmony_ci		.fourcc	= V4L2_PIX_FMT_BGR24,
26062306a36Sopenharmony_ci		.bpp    = 24,
26162306a36Sopenharmony_ci	}, {
26262306a36Sopenharmony_ci		.fourcc	= V4L2_PIX_FMT_RGB32,
26362306a36Sopenharmony_ci		.bpp    = 32,
26462306a36Sopenharmony_ci	}, {
26562306a36Sopenharmony_ci		.fourcc	= V4L2_PIX_FMT_BGR32,
26662306a36Sopenharmony_ci		.bpp    = 32,
26762306a36Sopenharmony_ci	}, {
26862306a36Sopenharmony_ci		.fourcc	= V4L2_PIX_FMT_XRGB32,
26962306a36Sopenharmony_ci		.bpp    = 32,
27062306a36Sopenharmony_ci	}, {
27162306a36Sopenharmony_ci		.fourcc	= V4L2_PIX_FMT_XBGR32,
27262306a36Sopenharmony_ci		.bpp    = 32,
27362306a36Sopenharmony_ci	}, {
27462306a36Sopenharmony_ci		.fourcc	= V4L2_PIX_FMT_BGRX32,
27562306a36Sopenharmony_ci		.bpp    = 32,
27662306a36Sopenharmony_ci	}, {
27762306a36Sopenharmony_ci		.fourcc	= V4L2_PIX_FMT_RGBX32,
27862306a36Sopenharmony_ci		.bpp    = 32,
27962306a36Sopenharmony_ci	}, {
28062306a36Sopenharmony_ci		.fourcc	= V4L2_PIX_FMT_YUYV,
28162306a36Sopenharmony_ci		.bpp    = 16,
28262306a36Sopenharmony_ci		.uv_width_dec = 2,
28362306a36Sopenharmony_ci		.uv_height_dec = 1,
28462306a36Sopenharmony_ci	}, {
28562306a36Sopenharmony_ci		.fourcc	= V4L2_PIX_FMT_UYVY,
28662306a36Sopenharmony_ci		.bpp    = 16,
28762306a36Sopenharmony_ci		.uv_width_dec = 2,
28862306a36Sopenharmony_ci		.uv_height_dec = 1,
28962306a36Sopenharmony_ci	}, {
29062306a36Sopenharmony_ci		.fourcc	= V4L2_PIX_FMT_YUV420,
29162306a36Sopenharmony_ci		.bpp    = 12,
29262306a36Sopenharmony_ci		.planar = true,
29362306a36Sopenharmony_ci		.uv_width_dec = 2,
29462306a36Sopenharmony_ci		.uv_height_dec = 2,
29562306a36Sopenharmony_ci	}, {
29662306a36Sopenharmony_ci		.fourcc	= V4L2_PIX_FMT_YVU420,
29762306a36Sopenharmony_ci		.bpp    = 12,
29862306a36Sopenharmony_ci		.planar = true,
29962306a36Sopenharmony_ci		.uv_width_dec = 2,
30062306a36Sopenharmony_ci		.uv_height_dec = 2,
30162306a36Sopenharmony_ci		.uv_swapped = true,
30262306a36Sopenharmony_ci	}, {
30362306a36Sopenharmony_ci		.fourcc = V4L2_PIX_FMT_NV12,
30462306a36Sopenharmony_ci		.bpp    = 12,
30562306a36Sopenharmony_ci		.planar = true,
30662306a36Sopenharmony_ci		.uv_width_dec = 2,
30762306a36Sopenharmony_ci		.uv_height_dec = 2,
30862306a36Sopenharmony_ci		.uv_packed = true,
30962306a36Sopenharmony_ci	}, {
31062306a36Sopenharmony_ci		.fourcc = V4L2_PIX_FMT_YUV422P,
31162306a36Sopenharmony_ci		.bpp    = 16,
31262306a36Sopenharmony_ci		.planar = true,
31362306a36Sopenharmony_ci		.uv_width_dec = 2,
31462306a36Sopenharmony_ci		.uv_height_dec = 1,
31562306a36Sopenharmony_ci	}, {
31662306a36Sopenharmony_ci		.fourcc = V4L2_PIX_FMT_NV16,
31762306a36Sopenharmony_ci		.bpp    = 16,
31862306a36Sopenharmony_ci		.planar = true,
31962306a36Sopenharmony_ci		.uv_width_dec = 2,
32062306a36Sopenharmony_ci		.uv_height_dec = 1,
32162306a36Sopenharmony_ci		.uv_packed = true,
32262306a36Sopenharmony_ci	},
32362306a36Sopenharmony_ci};
32462306a36Sopenharmony_ci
32562306a36Sopenharmony_cistatic const struct ipu_image_pixfmt *get_format(u32 fourcc)
32662306a36Sopenharmony_ci{
32762306a36Sopenharmony_ci	const struct ipu_image_pixfmt *ret = NULL;
32862306a36Sopenharmony_ci	unsigned int i;
32962306a36Sopenharmony_ci
33062306a36Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(image_convert_formats); i++) {
33162306a36Sopenharmony_ci		if (image_convert_formats[i].fourcc == fourcc) {
33262306a36Sopenharmony_ci			ret = &image_convert_formats[i];
33362306a36Sopenharmony_ci			break;
33462306a36Sopenharmony_ci		}
33562306a36Sopenharmony_ci	}
33662306a36Sopenharmony_ci
33762306a36Sopenharmony_ci	return ret;
33862306a36Sopenharmony_ci}
33962306a36Sopenharmony_ci
34062306a36Sopenharmony_cistatic void dump_format(struct ipu_image_convert_ctx *ctx,
34162306a36Sopenharmony_ci			struct ipu_image_convert_image *ic_image)
34262306a36Sopenharmony_ci{
34362306a36Sopenharmony_ci	struct ipu_image_convert_chan *chan = ctx->chan;
34462306a36Sopenharmony_ci	struct ipu_image_convert_priv *priv = chan->priv;
34562306a36Sopenharmony_ci
34662306a36Sopenharmony_ci	dev_dbg(priv->ipu->dev,
34762306a36Sopenharmony_ci		"task %u: ctx %p: %s format: %dx%d (%dx%d tiles), %c%c%c%c\n",
34862306a36Sopenharmony_ci		chan->ic_task, ctx,
34962306a36Sopenharmony_ci		ic_image->type == IMAGE_CONVERT_OUT ? "Output" : "Input",
35062306a36Sopenharmony_ci		ic_image->base.pix.width, ic_image->base.pix.height,
35162306a36Sopenharmony_ci		ic_image->num_cols, ic_image->num_rows,
35262306a36Sopenharmony_ci		ic_image->fmt->fourcc & 0xff,
35362306a36Sopenharmony_ci		(ic_image->fmt->fourcc >> 8) & 0xff,
35462306a36Sopenharmony_ci		(ic_image->fmt->fourcc >> 16) & 0xff,
35562306a36Sopenharmony_ci		(ic_image->fmt->fourcc >> 24) & 0xff);
35662306a36Sopenharmony_ci}
35762306a36Sopenharmony_ci
35862306a36Sopenharmony_ciint ipu_image_convert_enum_format(int index, u32 *fourcc)
35962306a36Sopenharmony_ci{
36062306a36Sopenharmony_ci	const struct ipu_image_pixfmt *fmt;
36162306a36Sopenharmony_ci
36262306a36Sopenharmony_ci	if (index >= (int)ARRAY_SIZE(image_convert_formats))
36362306a36Sopenharmony_ci		return -EINVAL;
36462306a36Sopenharmony_ci
36562306a36Sopenharmony_ci	/* Format found */
36662306a36Sopenharmony_ci	fmt = &image_convert_formats[index];
36762306a36Sopenharmony_ci	*fourcc = fmt->fourcc;
36862306a36Sopenharmony_ci	return 0;
36962306a36Sopenharmony_ci}
37062306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(ipu_image_convert_enum_format);
37162306a36Sopenharmony_ci
37262306a36Sopenharmony_cistatic void free_dma_buf(struct ipu_image_convert_priv *priv,
37362306a36Sopenharmony_ci			 struct ipu_image_convert_dma_buf *buf)
37462306a36Sopenharmony_ci{
37562306a36Sopenharmony_ci	if (buf->virt)
37662306a36Sopenharmony_ci		dma_free_coherent(priv->ipu->dev,
37762306a36Sopenharmony_ci				  buf->len, buf->virt, buf->phys);
37862306a36Sopenharmony_ci	buf->virt = NULL;
37962306a36Sopenharmony_ci	buf->phys = 0;
38062306a36Sopenharmony_ci}
38162306a36Sopenharmony_ci
38262306a36Sopenharmony_cistatic int alloc_dma_buf(struct ipu_image_convert_priv *priv,
38362306a36Sopenharmony_ci			 struct ipu_image_convert_dma_buf *buf,
38462306a36Sopenharmony_ci			 int size)
38562306a36Sopenharmony_ci{
38662306a36Sopenharmony_ci	buf->len = PAGE_ALIGN(size);
38762306a36Sopenharmony_ci	buf->virt = dma_alloc_coherent(priv->ipu->dev, buf->len, &buf->phys,
38862306a36Sopenharmony_ci				       GFP_DMA | GFP_KERNEL);
38962306a36Sopenharmony_ci	if (!buf->virt) {
39062306a36Sopenharmony_ci		dev_err(priv->ipu->dev, "failed to alloc dma buffer\n");
39162306a36Sopenharmony_ci		return -ENOMEM;
39262306a36Sopenharmony_ci	}
39362306a36Sopenharmony_ci
39462306a36Sopenharmony_ci	return 0;
39562306a36Sopenharmony_ci}
39662306a36Sopenharmony_ci
39762306a36Sopenharmony_cistatic inline int num_stripes(int dim)
39862306a36Sopenharmony_ci{
39962306a36Sopenharmony_ci	return (dim - 1) / 1024 + 1;
40062306a36Sopenharmony_ci}
40162306a36Sopenharmony_ci
40262306a36Sopenharmony_ci/*
40362306a36Sopenharmony_ci * Calculate downsizing coefficients, which are the same for all tiles,
40462306a36Sopenharmony_ci * and initial bilinear resizing coefficients, which are used to find the
40562306a36Sopenharmony_ci * best seam positions.
40662306a36Sopenharmony_ci * Also determine the number of tiles necessary to guarantee that no tile
40762306a36Sopenharmony_ci * is larger than 1024 pixels in either dimension at the output and between
40862306a36Sopenharmony_ci * IC downsizing and main processing sections.
40962306a36Sopenharmony_ci */
41062306a36Sopenharmony_cistatic int calc_image_resize_coefficients(struct ipu_image_convert_ctx *ctx,
41162306a36Sopenharmony_ci					  struct ipu_image *in,
41262306a36Sopenharmony_ci					  struct ipu_image *out)
41362306a36Sopenharmony_ci{
41462306a36Sopenharmony_ci	u32 downsized_width = in->rect.width;
41562306a36Sopenharmony_ci	u32 downsized_height = in->rect.height;
41662306a36Sopenharmony_ci	u32 downsize_coeff_v = 0;
41762306a36Sopenharmony_ci	u32 downsize_coeff_h = 0;
41862306a36Sopenharmony_ci	u32 resized_width = out->rect.width;
41962306a36Sopenharmony_ci	u32 resized_height = out->rect.height;
42062306a36Sopenharmony_ci	u32 resize_coeff_h;
42162306a36Sopenharmony_ci	u32 resize_coeff_v;
42262306a36Sopenharmony_ci	u32 cols;
42362306a36Sopenharmony_ci	u32 rows;
42462306a36Sopenharmony_ci
42562306a36Sopenharmony_ci	if (ipu_rot_mode_is_irt(ctx->rot_mode)) {
42662306a36Sopenharmony_ci		resized_width = out->rect.height;
42762306a36Sopenharmony_ci		resized_height = out->rect.width;
42862306a36Sopenharmony_ci	}
42962306a36Sopenharmony_ci
43062306a36Sopenharmony_ci	/* Do not let invalid input lead to an endless loop below */
43162306a36Sopenharmony_ci	if (WARN_ON(resized_width == 0 || resized_height == 0))
43262306a36Sopenharmony_ci		return -EINVAL;
43362306a36Sopenharmony_ci
43462306a36Sopenharmony_ci	while (downsized_width >= resized_width * 2) {
43562306a36Sopenharmony_ci		downsized_width >>= 1;
43662306a36Sopenharmony_ci		downsize_coeff_h++;
43762306a36Sopenharmony_ci	}
43862306a36Sopenharmony_ci
43962306a36Sopenharmony_ci	while (downsized_height >= resized_height * 2) {
44062306a36Sopenharmony_ci		downsized_height >>= 1;
44162306a36Sopenharmony_ci		downsize_coeff_v++;
44262306a36Sopenharmony_ci	}
44362306a36Sopenharmony_ci
44462306a36Sopenharmony_ci	/*
44562306a36Sopenharmony_ci	 * Calculate the bilinear resizing coefficients that could be used if
44662306a36Sopenharmony_ci	 * we were converting with a single tile. The bottom right output pixel
44762306a36Sopenharmony_ci	 * should sample as close as possible to the bottom right input pixel
44862306a36Sopenharmony_ci	 * out of the decimator, but not overshoot it:
44962306a36Sopenharmony_ci	 */
45062306a36Sopenharmony_ci	resize_coeff_h = 8192 * (downsized_width - 1) / (resized_width - 1);
45162306a36Sopenharmony_ci	resize_coeff_v = 8192 * (downsized_height - 1) / (resized_height - 1);
45262306a36Sopenharmony_ci
45362306a36Sopenharmony_ci	/*
45462306a36Sopenharmony_ci	 * Both the output of the IC downsizing section before being passed to
45562306a36Sopenharmony_ci	 * the IC main processing section and the final output of the IC main
45662306a36Sopenharmony_ci	 * processing section must be <= 1024 pixels in both dimensions.
45762306a36Sopenharmony_ci	 */
45862306a36Sopenharmony_ci	cols = num_stripes(max_t(u32, downsized_width, resized_width));
45962306a36Sopenharmony_ci	rows = num_stripes(max_t(u32, downsized_height, resized_height));
46062306a36Sopenharmony_ci
46162306a36Sopenharmony_ci	dev_dbg(ctx->chan->priv->ipu->dev,
46262306a36Sopenharmony_ci		"%s: hscale: >>%u, *8192/%u vscale: >>%u, *8192/%u, %ux%u tiles\n",
46362306a36Sopenharmony_ci		__func__, downsize_coeff_h, resize_coeff_h, downsize_coeff_v,
46462306a36Sopenharmony_ci		resize_coeff_v, cols, rows);
46562306a36Sopenharmony_ci
46662306a36Sopenharmony_ci	if (downsize_coeff_h > 2 || downsize_coeff_v  > 2 ||
46762306a36Sopenharmony_ci	    resize_coeff_h > 0x3fff || resize_coeff_v > 0x3fff)
46862306a36Sopenharmony_ci		return -EINVAL;
46962306a36Sopenharmony_ci
47062306a36Sopenharmony_ci	ctx->downsize_coeff_h = downsize_coeff_h;
47162306a36Sopenharmony_ci	ctx->downsize_coeff_v = downsize_coeff_v;
47262306a36Sopenharmony_ci	ctx->image_resize_coeff_h = resize_coeff_h;
47362306a36Sopenharmony_ci	ctx->image_resize_coeff_v = resize_coeff_v;
47462306a36Sopenharmony_ci	ctx->in.num_cols = cols;
47562306a36Sopenharmony_ci	ctx->in.num_rows = rows;
47662306a36Sopenharmony_ci
47762306a36Sopenharmony_ci	return 0;
47862306a36Sopenharmony_ci}
47962306a36Sopenharmony_ci
48062306a36Sopenharmony_ci#define round_closest(x, y) round_down((x) + (y)/2, (y))
48162306a36Sopenharmony_ci
48262306a36Sopenharmony_ci/*
48362306a36Sopenharmony_ci * Find the best aligned seam position for the given column / row index.
48462306a36Sopenharmony_ci * Rotation and image offsets are out of scope.
48562306a36Sopenharmony_ci *
48662306a36Sopenharmony_ci * @index: column / row index, used to calculate valid interval
48762306a36Sopenharmony_ci * @in_edge: input right / bottom edge
48862306a36Sopenharmony_ci * @out_edge: output right / bottom edge
48962306a36Sopenharmony_ci * @in_align: input alignment, either horizontal 8-byte line start address
49062306a36Sopenharmony_ci *            alignment, or pixel alignment due to image format
49162306a36Sopenharmony_ci * @out_align: output alignment, either horizontal 8-byte line start address
49262306a36Sopenharmony_ci *             alignment, or pixel alignment due to image format or rotator
49362306a36Sopenharmony_ci *             block size
49462306a36Sopenharmony_ci * @in_burst: horizontal input burst size in case of horizontal flip
49562306a36Sopenharmony_ci * @out_burst: horizontal output burst size or rotator block size
49662306a36Sopenharmony_ci * @downsize_coeff: downsizing section coefficient
49762306a36Sopenharmony_ci * @resize_coeff: main processing section resizing coefficient
49862306a36Sopenharmony_ci * @_in_seam: aligned input seam position return value
49962306a36Sopenharmony_ci * @_out_seam: aligned output seam position return value
50062306a36Sopenharmony_ci */
50162306a36Sopenharmony_cistatic void find_best_seam(struct ipu_image_convert_ctx *ctx,
50262306a36Sopenharmony_ci			   unsigned int index,
50362306a36Sopenharmony_ci			   unsigned int in_edge,
50462306a36Sopenharmony_ci			   unsigned int out_edge,
50562306a36Sopenharmony_ci			   unsigned int in_align,
50662306a36Sopenharmony_ci			   unsigned int out_align,
50762306a36Sopenharmony_ci			   unsigned int in_burst,
50862306a36Sopenharmony_ci			   unsigned int out_burst,
50962306a36Sopenharmony_ci			   unsigned int downsize_coeff,
51062306a36Sopenharmony_ci			   unsigned int resize_coeff,
51162306a36Sopenharmony_ci			   u32 *_in_seam,
51262306a36Sopenharmony_ci			   u32 *_out_seam)
51362306a36Sopenharmony_ci{
51462306a36Sopenharmony_ci	struct device *dev = ctx->chan->priv->ipu->dev;
51562306a36Sopenharmony_ci	unsigned int out_pos;
51662306a36Sopenharmony_ci	/* Input / output seam position candidates */
51762306a36Sopenharmony_ci	unsigned int out_seam = 0;
51862306a36Sopenharmony_ci	unsigned int in_seam = 0;
51962306a36Sopenharmony_ci	unsigned int min_diff = UINT_MAX;
52062306a36Sopenharmony_ci	unsigned int out_start;
52162306a36Sopenharmony_ci	unsigned int out_end;
52262306a36Sopenharmony_ci	unsigned int in_start;
52362306a36Sopenharmony_ci	unsigned int in_end;
52462306a36Sopenharmony_ci
52562306a36Sopenharmony_ci	/* Start within 1024 pixels of the right / bottom edge */
52662306a36Sopenharmony_ci	out_start = max_t(int, index * out_align, out_edge - 1024);
52762306a36Sopenharmony_ci	/* End before having to add more columns to the left / rows above */
52862306a36Sopenharmony_ci	out_end = min_t(unsigned int, out_edge, index * 1024 + 1);
52962306a36Sopenharmony_ci
53062306a36Sopenharmony_ci	/*
53162306a36Sopenharmony_ci	 * Limit input seam position to make sure that the downsized input tile
53262306a36Sopenharmony_ci	 * to the right or bottom does not exceed 1024 pixels.
53362306a36Sopenharmony_ci	 */
53462306a36Sopenharmony_ci	in_start = max_t(int, index * in_align,
53562306a36Sopenharmony_ci			 in_edge - (1024 << downsize_coeff));
53662306a36Sopenharmony_ci	in_end = min_t(unsigned int, in_edge,
53762306a36Sopenharmony_ci		       index * (1024 << downsize_coeff) + 1);
53862306a36Sopenharmony_ci
53962306a36Sopenharmony_ci	/*
54062306a36Sopenharmony_ci	 * Output tiles must start at a multiple of 8 bytes horizontally and
54162306a36Sopenharmony_ci	 * possibly at an even line horizontally depending on the pixel format.
54262306a36Sopenharmony_ci	 * Only consider output aligned positions for the seam.
54362306a36Sopenharmony_ci	 */
54462306a36Sopenharmony_ci	out_start = round_up(out_start, out_align);
54562306a36Sopenharmony_ci	for (out_pos = out_start; out_pos < out_end; out_pos += out_align) {
54662306a36Sopenharmony_ci		unsigned int in_pos;
54762306a36Sopenharmony_ci		unsigned int in_pos_aligned;
54862306a36Sopenharmony_ci		unsigned int in_pos_rounded;
54962306a36Sopenharmony_ci		unsigned int diff;
55062306a36Sopenharmony_ci
55162306a36Sopenharmony_ci		/*
55262306a36Sopenharmony_ci		 * Tiles in the right row / bottom column may not be allowed to
55362306a36Sopenharmony_ci		 * overshoot horizontally / vertically. out_burst may be the
55462306a36Sopenharmony_ci		 * actual DMA burst size, or the rotator block size.
55562306a36Sopenharmony_ci		 */
55662306a36Sopenharmony_ci		if ((out_burst > 1) && (out_edge - out_pos) % out_burst)
55762306a36Sopenharmony_ci			continue;
55862306a36Sopenharmony_ci
55962306a36Sopenharmony_ci		/*
56062306a36Sopenharmony_ci		 * Input sample position, corresponding to out_pos, 19.13 fixed
56162306a36Sopenharmony_ci		 * point.
56262306a36Sopenharmony_ci		 */
56362306a36Sopenharmony_ci		in_pos = (out_pos * resize_coeff) << downsize_coeff;
56462306a36Sopenharmony_ci		/*
56562306a36Sopenharmony_ci		 * The closest input sample position that we could actually
56662306a36Sopenharmony_ci		 * start the input tile at, 19.13 fixed point.
56762306a36Sopenharmony_ci		 */
56862306a36Sopenharmony_ci		in_pos_aligned = round_closest(in_pos, 8192U * in_align);
56962306a36Sopenharmony_ci		/* Convert 19.13 fixed point to integer */
57062306a36Sopenharmony_ci		in_pos_rounded = in_pos_aligned / 8192U;
57162306a36Sopenharmony_ci
57262306a36Sopenharmony_ci		if (in_pos_rounded < in_start)
57362306a36Sopenharmony_ci			continue;
57462306a36Sopenharmony_ci		if (in_pos_rounded >= in_end)
57562306a36Sopenharmony_ci			break;
57662306a36Sopenharmony_ci
57762306a36Sopenharmony_ci		if ((in_burst > 1) &&
57862306a36Sopenharmony_ci		    (in_edge - in_pos_rounded) % in_burst)
57962306a36Sopenharmony_ci			continue;
58062306a36Sopenharmony_ci
58162306a36Sopenharmony_ci		diff = abs_diff(in_pos, in_pos_aligned);
58262306a36Sopenharmony_ci		if (diff < min_diff) {
58362306a36Sopenharmony_ci			in_seam = in_pos_rounded;
58462306a36Sopenharmony_ci			out_seam = out_pos;
58562306a36Sopenharmony_ci			min_diff = diff;
58662306a36Sopenharmony_ci		}
58762306a36Sopenharmony_ci	}
58862306a36Sopenharmony_ci
58962306a36Sopenharmony_ci	*_out_seam = out_seam;
59062306a36Sopenharmony_ci	*_in_seam = in_seam;
59162306a36Sopenharmony_ci
59262306a36Sopenharmony_ci	dev_dbg(dev, "%s: out_seam %u(%u) in [%u, %u], in_seam %u(%u) in [%u, %u] diff %u.%03u\n",
59362306a36Sopenharmony_ci		__func__, out_seam, out_align, out_start, out_end,
59462306a36Sopenharmony_ci		in_seam, in_align, in_start, in_end, min_diff / 8192,
59562306a36Sopenharmony_ci		DIV_ROUND_CLOSEST(min_diff % 8192 * 1000, 8192));
59662306a36Sopenharmony_ci}
59762306a36Sopenharmony_ci
59862306a36Sopenharmony_ci/*
59962306a36Sopenharmony_ci * Tile left edges are required to be aligned to multiples of 8 bytes
60062306a36Sopenharmony_ci * by the IDMAC.
60162306a36Sopenharmony_ci */
60262306a36Sopenharmony_cistatic inline u32 tile_left_align(const struct ipu_image_pixfmt *fmt)
60362306a36Sopenharmony_ci{
60462306a36Sopenharmony_ci	if (fmt->planar)
60562306a36Sopenharmony_ci		return fmt->uv_packed ? 8 : 8 * fmt->uv_width_dec;
60662306a36Sopenharmony_ci	else
60762306a36Sopenharmony_ci		return fmt->bpp == 32 ? 2 : fmt->bpp == 16 ? 4 : 8;
60862306a36Sopenharmony_ci}
60962306a36Sopenharmony_ci
61062306a36Sopenharmony_ci/*
61162306a36Sopenharmony_ci * Tile top edge alignment is only limited by chroma subsampling.
61262306a36Sopenharmony_ci */
61362306a36Sopenharmony_cistatic inline u32 tile_top_align(const struct ipu_image_pixfmt *fmt)
61462306a36Sopenharmony_ci{
61562306a36Sopenharmony_ci	return fmt->uv_height_dec > 1 ? 2 : 1;
61662306a36Sopenharmony_ci}
61762306a36Sopenharmony_ci
61862306a36Sopenharmony_cistatic inline u32 tile_width_align(enum ipu_image_convert_type type,
61962306a36Sopenharmony_ci				   const struct ipu_image_pixfmt *fmt,
62062306a36Sopenharmony_ci				   enum ipu_rotate_mode rot_mode)
62162306a36Sopenharmony_ci{
62262306a36Sopenharmony_ci	if (type == IMAGE_CONVERT_IN) {
62362306a36Sopenharmony_ci		/*
62462306a36Sopenharmony_ci		 * The IC burst reads 8 pixels at a time. Reading beyond the
62562306a36Sopenharmony_ci		 * end of the line is usually acceptable. Those pixels are
62662306a36Sopenharmony_ci		 * ignored, unless the IC has to write the scaled line in
62762306a36Sopenharmony_ci		 * reverse.
62862306a36Sopenharmony_ci		 */
62962306a36Sopenharmony_ci		return (!ipu_rot_mode_is_irt(rot_mode) &&
63062306a36Sopenharmony_ci			(rot_mode & IPU_ROT_BIT_HFLIP)) ? 8 : 2;
63162306a36Sopenharmony_ci	}
63262306a36Sopenharmony_ci
63362306a36Sopenharmony_ci	/*
63462306a36Sopenharmony_ci	 * Align to 16x16 pixel blocks for planar 4:2:0 chroma subsampled
63562306a36Sopenharmony_ci	 * formats to guarantee 8-byte aligned line start addresses in the
63662306a36Sopenharmony_ci	 * chroma planes when IRT is used. Align to 8x8 pixel IRT block size
63762306a36Sopenharmony_ci	 * for all other formats.
63862306a36Sopenharmony_ci	 */
63962306a36Sopenharmony_ci	return (ipu_rot_mode_is_irt(rot_mode) &&
64062306a36Sopenharmony_ci		fmt->planar && !fmt->uv_packed) ?
64162306a36Sopenharmony_ci		8 * fmt->uv_width_dec : 8;
64262306a36Sopenharmony_ci}
64362306a36Sopenharmony_ci
64462306a36Sopenharmony_cistatic inline u32 tile_height_align(enum ipu_image_convert_type type,
64562306a36Sopenharmony_ci				    const struct ipu_image_pixfmt *fmt,
64662306a36Sopenharmony_ci				    enum ipu_rotate_mode rot_mode)
64762306a36Sopenharmony_ci{
64862306a36Sopenharmony_ci	if (type == IMAGE_CONVERT_IN || !ipu_rot_mode_is_irt(rot_mode))
64962306a36Sopenharmony_ci		return 2;
65062306a36Sopenharmony_ci
65162306a36Sopenharmony_ci	/*
65262306a36Sopenharmony_ci	 * Align to 16x16 pixel blocks for planar 4:2:0 chroma subsampled
65362306a36Sopenharmony_ci	 * formats to guarantee 8-byte aligned line start addresses in the
65462306a36Sopenharmony_ci	 * chroma planes when IRT is used. Align to 8x8 pixel IRT block size
65562306a36Sopenharmony_ci	 * for all other formats.
65662306a36Sopenharmony_ci	 */
65762306a36Sopenharmony_ci	return (fmt->planar && !fmt->uv_packed) ? 8 * fmt->uv_width_dec : 8;
65862306a36Sopenharmony_ci}
65962306a36Sopenharmony_ci
66062306a36Sopenharmony_ci/*
66162306a36Sopenharmony_ci * Fill in left position and width and for all tiles in an input column, and
66262306a36Sopenharmony_ci * for all corresponding output tiles. If the 90° rotator is used, the output
66362306a36Sopenharmony_ci * tiles are in a row, and output tile top position and height are set.
66462306a36Sopenharmony_ci */
66562306a36Sopenharmony_cistatic void fill_tile_column(struct ipu_image_convert_ctx *ctx,
66662306a36Sopenharmony_ci			     unsigned int col,
66762306a36Sopenharmony_ci			     struct ipu_image_convert_image *in,
66862306a36Sopenharmony_ci			     unsigned int in_left, unsigned int in_width,
66962306a36Sopenharmony_ci			     struct ipu_image_convert_image *out,
67062306a36Sopenharmony_ci			     unsigned int out_left, unsigned int out_width)
67162306a36Sopenharmony_ci{
67262306a36Sopenharmony_ci	unsigned int row, tile_idx;
67362306a36Sopenharmony_ci	struct ipu_image_tile *in_tile, *out_tile;
67462306a36Sopenharmony_ci
67562306a36Sopenharmony_ci	for (row = 0; row < in->num_rows; row++) {
67662306a36Sopenharmony_ci		tile_idx = in->num_cols * row + col;
67762306a36Sopenharmony_ci		in_tile = &in->tile[tile_idx];
67862306a36Sopenharmony_ci		out_tile = &out->tile[ctx->out_tile_map[tile_idx]];
67962306a36Sopenharmony_ci
68062306a36Sopenharmony_ci		in_tile->left = in_left;
68162306a36Sopenharmony_ci		in_tile->width = in_width;
68262306a36Sopenharmony_ci
68362306a36Sopenharmony_ci		if (ipu_rot_mode_is_irt(ctx->rot_mode)) {
68462306a36Sopenharmony_ci			out_tile->top = out_left;
68562306a36Sopenharmony_ci			out_tile->height = out_width;
68662306a36Sopenharmony_ci		} else {
68762306a36Sopenharmony_ci			out_tile->left = out_left;
68862306a36Sopenharmony_ci			out_tile->width = out_width;
68962306a36Sopenharmony_ci		}
69062306a36Sopenharmony_ci	}
69162306a36Sopenharmony_ci}
69262306a36Sopenharmony_ci
69362306a36Sopenharmony_ci/*
69462306a36Sopenharmony_ci * Fill in top position and height and for all tiles in an input row, and
69562306a36Sopenharmony_ci * for all corresponding output tiles. If the 90° rotator is used, the output
69662306a36Sopenharmony_ci * tiles are in a column, and output tile left position and width are set.
69762306a36Sopenharmony_ci */
69862306a36Sopenharmony_cistatic void fill_tile_row(struct ipu_image_convert_ctx *ctx, unsigned int row,
69962306a36Sopenharmony_ci			  struct ipu_image_convert_image *in,
70062306a36Sopenharmony_ci			  unsigned int in_top, unsigned int in_height,
70162306a36Sopenharmony_ci			  struct ipu_image_convert_image *out,
70262306a36Sopenharmony_ci			  unsigned int out_top, unsigned int out_height)
70362306a36Sopenharmony_ci{
70462306a36Sopenharmony_ci	unsigned int col, tile_idx;
70562306a36Sopenharmony_ci	struct ipu_image_tile *in_tile, *out_tile;
70662306a36Sopenharmony_ci
70762306a36Sopenharmony_ci	for (col = 0; col < in->num_cols; col++) {
70862306a36Sopenharmony_ci		tile_idx = in->num_cols * row + col;
70962306a36Sopenharmony_ci		in_tile = &in->tile[tile_idx];
71062306a36Sopenharmony_ci		out_tile = &out->tile[ctx->out_tile_map[tile_idx]];
71162306a36Sopenharmony_ci
71262306a36Sopenharmony_ci		in_tile->top = in_top;
71362306a36Sopenharmony_ci		in_tile->height = in_height;
71462306a36Sopenharmony_ci
71562306a36Sopenharmony_ci		if (ipu_rot_mode_is_irt(ctx->rot_mode)) {
71662306a36Sopenharmony_ci			out_tile->left = out_top;
71762306a36Sopenharmony_ci			out_tile->width = out_height;
71862306a36Sopenharmony_ci		} else {
71962306a36Sopenharmony_ci			out_tile->top = out_top;
72062306a36Sopenharmony_ci			out_tile->height = out_height;
72162306a36Sopenharmony_ci		}
72262306a36Sopenharmony_ci	}
72362306a36Sopenharmony_ci}
72462306a36Sopenharmony_ci
72562306a36Sopenharmony_ci/*
72662306a36Sopenharmony_ci * Find the best horizontal and vertical seam positions to split into tiles.
72762306a36Sopenharmony_ci * Minimize the fractional part of the input sampling position for the
72862306a36Sopenharmony_ci * top / left pixels of each tile.
72962306a36Sopenharmony_ci */
73062306a36Sopenharmony_cistatic void find_seams(struct ipu_image_convert_ctx *ctx,
73162306a36Sopenharmony_ci		       struct ipu_image_convert_image *in,
73262306a36Sopenharmony_ci		       struct ipu_image_convert_image *out)
73362306a36Sopenharmony_ci{
73462306a36Sopenharmony_ci	struct device *dev = ctx->chan->priv->ipu->dev;
73562306a36Sopenharmony_ci	unsigned int resized_width = out->base.rect.width;
73662306a36Sopenharmony_ci	unsigned int resized_height = out->base.rect.height;
73762306a36Sopenharmony_ci	unsigned int col;
73862306a36Sopenharmony_ci	unsigned int row;
73962306a36Sopenharmony_ci	unsigned int in_left_align = tile_left_align(in->fmt);
74062306a36Sopenharmony_ci	unsigned int in_top_align = tile_top_align(in->fmt);
74162306a36Sopenharmony_ci	unsigned int out_left_align = tile_left_align(out->fmt);
74262306a36Sopenharmony_ci	unsigned int out_top_align = tile_top_align(out->fmt);
74362306a36Sopenharmony_ci	unsigned int out_width_align = tile_width_align(out->type, out->fmt,
74462306a36Sopenharmony_ci							ctx->rot_mode);
74562306a36Sopenharmony_ci	unsigned int out_height_align = tile_height_align(out->type, out->fmt,
74662306a36Sopenharmony_ci							  ctx->rot_mode);
74762306a36Sopenharmony_ci	unsigned int in_right = in->base.rect.width;
74862306a36Sopenharmony_ci	unsigned int in_bottom = in->base.rect.height;
74962306a36Sopenharmony_ci	unsigned int out_right = out->base.rect.width;
75062306a36Sopenharmony_ci	unsigned int out_bottom = out->base.rect.height;
75162306a36Sopenharmony_ci	unsigned int flipped_out_left;
75262306a36Sopenharmony_ci	unsigned int flipped_out_top;
75362306a36Sopenharmony_ci
75462306a36Sopenharmony_ci	if (ipu_rot_mode_is_irt(ctx->rot_mode)) {
75562306a36Sopenharmony_ci		/* Switch width/height and align top left to IRT block size */
75662306a36Sopenharmony_ci		resized_width = out->base.rect.height;
75762306a36Sopenharmony_ci		resized_height = out->base.rect.width;
75862306a36Sopenharmony_ci		out_left_align = out_height_align;
75962306a36Sopenharmony_ci		out_top_align = out_width_align;
76062306a36Sopenharmony_ci		out_width_align = out_left_align;
76162306a36Sopenharmony_ci		out_height_align = out_top_align;
76262306a36Sopenharmony_ci		out_right = out->base.rect.height;
76362306a36Sopenharmony_ci		out_bottom = out->base.rect.width;
76462306a36Sopenharmony_ci	}
76562306a36Sopenharmony_ci
76662306a36Sopenharmony_ci	for (col = in->num_cols - 1; col > 0; col--) {
76762306a36Sopenharmony_ci		bool allow_in_overshoot = ipu_rot_mode_is_irt(ctx->rot_mode) ||
76862306a36Sopenharmony_ci					  !(ctx->rot_mode & IPU_ROT_BIT_HFLIP);
76962306a36Sopenharmony_ci		bool allow_out_overshoot = (col < in->num_cols - 1) &&
77062306a36Sopenharmony_ci					   !(ctx->rot_mode & IPU_ROT_BIT_HFLIP);
77162306a36Sopenharmony_ci		unsigned int in_left;
77262306a36Sopenharmony_ci		unsigned int out_left;
77362306a36Sopenharmony_ci
77462306a36Sopenharmony_ci		/*
77562306a36Sopenharmony_ci		 * Align input width to burst length if the scaling step flips
77662306a36Sopenharmony_ci		 * horizontally.
77762306a36Sopenharmony_ci		 */
77862306a36Sopenharmony_ci
77962306a36Sopenharmony_ci		find_best_seam(ctx, col,
78062306a36Sopenharmony_ci			       in_right, out_right,
78162306a36Sopenharmony_ci			       in_left_align, out_left_align,
78262306a36Sopenharmony_ci			       allow_in_overshoot ? 1 : 8 /* burst length */,
78362306a36Sopenharmony_ci			       allow_out_overshoot ? 1 : out_width_align,
78462306a36Sopenharmony_ci			       ctx->downsize_coeff_h, ctx->image_resize_coeff_h,
78562306a36Sopenharmony_ci			       &in_left, &out_left);
78662306a36Sopenharmony_ci
78762306a36Sopenharmony_ci		if (ctx->rot_mode & IPU_ROT_BIT_HFLIP)
78862306a36Sopenharmony_ci			flipped_out_left = resized_width - out_right;
78962306a36Sopenharmony_ci		else
79062306a36Sopenharmony_ci			flipped_out_left = out_left;
79162306a36Sopenharmony_ci
79262306a36Sopenharmony_ci		fill_tile_column(ctx, col, in, in_left, in_right - in_left,
79362306a36Sopenharmony_ci				 out, flipped_out_left, out_right - out_left);
79462306a36Sopenharmony_ci
79562306a36Sopenharmony_ci		dev_dbg(dev, "%s: col %u: %u, %u -> %u, %u\n", __func__, col,
79662306a36Sopenharmony_ci			in_left, in_right - in_left,
79762306a36Sopenharmony_ci			flipped_out_left, out_right - out_left);
79862306a36Sopenharmony_ci
79962306a36Sopenharmony_ci		in_right = in_left;
80062306a36Sopenharmony_ci		out_right = out_left;
80162306a36Sopenharmony_ci	}
80262306a36Sopenharmony_ci
80362306a36Sopenharmony_ci	flipped_out_left = (ctx->rot_mode & IPU_ROT_BIT_HFLIP) ?
80462306a36Sopenharmony_ci			   resized_width - out_right : 0;
80562306a36Sopenharmony_ci
80662306a36Sopenharmony_ci	fill_tile_column(ctx, 0, in, 0, in_right,
80762306a36Sopenharmony_ci			 out, flipped_out_left, out_right);
80862306a36Sopenharmony_ci
80962306a36Sopenharmony_ci	dev_dbg(dev, "%s: col 0: 0, %u -> %u, %u\n", __func__,
81062306a36Sopenharmony_ci		in_right, flipped_out_left, out_right);
81162306a36Sopenharmony_ci
81262306a36Sopenharmony_ci	for (row = in->num_rows - 1; row > 0; row--) {
81362306a36Sopenharmony_ci		bool allow_overshoot = row < in->num_rows - 1;
81462306a36Sopenharmony_ci		unsigned int in_top;
81562306a36Sopenharmony_ci		unsigned int out_top;
81662306a36Sopenharmony_ci
81762306a36Sopenharmony_ci		find_best_seam(ctx, row,
81862306a36Sopenharmony_ci			       in_bottom, out_bottom,
81962306a36Sopenharmony_ci			       in_top_align, out_top_align,
82062306a36Sopenharmony_ci			       1, allow_overshoot ? 1 : out_height_align,
82162306a36Sopenharmony_ci			       ctx->downsize_coeff_v, ctx->image_resize_coeff_v,
82262306a36Sopenharmony_ci			       &in_top, &out_top);
82362306a36Sopenharmony_ci
82462306a36Sopenharmony_ci		if ((ctx->rot_mode & IPU_ROT_BIT_VFLIP) ^
82562306a36Sopenharmony_ci		    ipu_rot_mode_is_irt(ctx->rot_mode))
82662306a36Sopenharmony_ci			flipped_out_top = resized_height - out_bottom;
82762306a36Sopenharmony_ci		else
82862306a36Sopenharmony_ci			flipped_out_top = out_top;
82962306a36Sopenharmony_ci
83062306a36Sopenharmony_ci		fill_tile_row(ctx, row, in, in_top, in_bottom - in_top,
83162306a36Sopenharmony_ci			      out, flipped_out_top, out_bottom - out_top);
83262306a36Sopenharmony_ci
83362306a36Sopenharmony_ci		dev_dbg(dev, "%s: row %u: %u, %u -> %u, %u\n", __func__, row,
83462306a36Sopenharmony_ci			in_top, in_bottom - in_top,
83562306a36Sopenharmony_ci			flipped_out_top, out_bottom - out_top);
83662306a36Sopenharmony_ci
83762306a36Sopenharmony_ci		in_bottom = in_top;
83862306a36Sopenharmony_ci		out_bottom = out_top;
83962306a36Sopenharmony_ci	}
84062306a36Sopenharmony_ci
84162306a36Sopenharmony_ci	if ((ctx->rot_mode & IPU_ROT_BIT_VFLIP) ^
84262306a36Sopenharmony_ci	    ipu_rot_mode_is_irt(ctx->rot_mode))
84362306a36Sopenharmony_ci		flipped_out_top = resized_height - out_bottom;
84462306a36Sopenharmony_ci	else
84562306a36Sopenharmony_ci		flipped_out_top = 0;
84662306a36Sopenharmony_ci
84762306a36Sopenharmony_ci	fill_tile_row(ctx, 0, in, 0, in_bottom,
84862306a36Sopenharmony_ci		      out, flipped_out_top, out_bottom);
84962306a36Sopenharmony_ci
85062306a36Sopenharmony_ci	dev_dbg(dev, "%s: row 0: 0, %u -> %u, %u\n", __func__,
85162306a36Sopenharmony_ci		in_bottom, flipped_out_top, out_bottom);
85262306a36Sopenharmony_ci}
85362306a36Sopenharmony_ci
85462306a36Sopenharmony_cistatic int calc_tile_dimensions(struct ipu_image_convert_ctx *ctx,
85562306a36Sopenharmony_ci				struct ipu_image_convert_image *image)
85662306a36Sopenharmony_ci{
85762306a36Sopenharmony_ci	struct ipu_image_convert_chan *chan = ctx->chan;
85862306a36Sopenharmony_ci	struct ipu_image_convert_priv *priv = chan->priv;
85962306a36Sopenharmony_ci	unsigned int max_width = 1024;
86062306a36Sopenharmony_ci	unsigned int max_height = 1024;
86162306a36Sopenharmony_ci	unsigned int i;
86262306a36Sopenharmony_ci
86362306a36Sopenharmony_ci	if (image->type == IMAGE_CONVERT_IN) {
86462306a36Sopenharmony_ci		/* Up to 4096x4096 input tile size */
86562306a36Sopenharmony_ci		max_width <<= ctx->downsize_coeff_h;
86662306a36Sopenharmony_ci		max_height <<= ctx->downsize_coeff_v;
86762306a36Sopenharmony_ci	}
86862306a36Sopenharmony_ci
86962306a36Sopenharmony_ci	for (i = 0; i < ctx->num_tiles; i++) {
87062306a36Sopenharmony_ci		struct ipu_image_tile *tile;
87162306a36Sopenharmony_ci		const unsigned int row = i / image->num_cols;
87262306a36Sopenharmony_ci		const unsigned int col = i % image->num_cols;
87362306a36Sopenharmony_ci
87462306a36Sopenharmony_ci		if (image->type == IMAGE_CONVERT_OUT)
87562306a36Sopenharmony_ci			tile = &image->tile[ctx->out_tile_map[i]];
87662306a36Sopenharmony_ci		else
87762306a36Sopenharmony_ci			tile = &image->tile[i];
87862306a36Sopenharmony_ci
87962306a36Sopenharmony_ci		tile->size = ((tile->height * image->fmt->bpp) >> 3) *
88062306a36Sopenharmony_ci			tile->width;
88162306a36Sopenharmony_ci
88262306a36Sopenharmony_ci		if (image->fmt->planar) {
88362306a36Sopenharmony_ci			tile->stride = tile->width;
88462306a36Sopenharmony_ci			tile->rot_stride = tile->height;
88562306a36Sopenharmony_ci		} else {
88662306a36Sopenharmony_ci			tile->stride =
88762306a36Sopenharmony_ci				(image->fmt->bpp * tile->width) >> 3;
88862306a36Sopenharmony_ci			tile->rot_stride =
88962306a36Sopenharmony_ci				(image->fmt->bpp * tile->height) >> 3;
89062306a36Sopenharmony_ci		}
89162306a36Sopenharmony_ci
89262306a36Sopenharmony_ci		dev_dbg(priv->ipu->dev,
89362306a36Sopenharmony_ci			"task %u: ctx %p: %s@[%u,%u]: %ux%u@%u,%u\n",
89462306a36Sopenharmony_ci			chan->ic_task, ctx,
89562306a36Sopenharmony_ci			image->type == IMAGE_CONVERT_IN ? "Input" : "Output",
89662306a36Sopenharmony_ci			row, col,
89762306a36Sopenharmony_ci			tile->width, tile->height, tile->left, tile->top);
89862306a36Sopenharmony_ci
89962306a36Sopenharmony_ci		if (!tile->width || tile->width > max_width ||
90062306a36Sopenharmony_ci		    !tile->height || tile->height > max_height) {
90162306a36Sopenharmony_ci			dev_err(priv->ipu->dev, "invalid %s tile size: %ux%u\n",
90262306a36Sopenharmony_ci				image->type == IMAGE_CONVERT_IN ? "input" :
90362306a36Sopenharmony_ci				"output", tile->width, tile->height);
90462306a36Sopenharmony_ci			return -EINVAL;
90562306a36Sopenharmony_ci		}
90662306a36Sopenharmony_ci	}
90762306a36Sopenharmony_ci
90862306a36Sopenharmony_ci	return 0;
90962306a36Sopenharmony_ci}
91062306a36Sopenharmony_ci
91162306a36Sopenharmony_ci/*
91262306a36Sopenharmony_ci * Use the rotation transformation to find the tile coordinates
91362306a36Sopenharmony_ci * (row, col) of a tile in the destination frame that corresponds
91462306a36Sopenharmony_ci * to the given tile coordinates of a source frame. The destination
91562306a36Sopenharmony_ci * coordinate is then converted to a tile index.
91662306a36Sopenharmony_ci */
91762306a36Sopenharmony_cistatic int transform_tile_index(struct ipu_image_convert_ctx *ctx,
91862306a36Sopenharmony_ci				int src_row, int src_col)
91962306a36Sopenharmony_ci{
92062306a36Sopenharmony_ci	struct ipu_image_convert_chan *chan = ctx->chan;
92162306a36Sopenharmony_ci	struct ipu_image_convert_priv *priv = chan->priv;
92262306a36Sopenharmony_ci	struct ipu_image_convert_image *s_image = &ctx->in;
92362306a36Sopenharmony_ci	struct ipu_image_convert_image *d_image = &ctx->out;
92462306a36Sopenharmony_ci	int dst_row, dst_col;
92562306a36Sopenharmony_ci
92662306a36Sopenharmony_ci	/* with no rotation it's a 1:1 mapping */
92762306a36Sopenharmony_ci	if (ctx->rot_mode == IPU_ROTATE_NONE)
92862306a36Sopenharmony_ci		return src_row * s_image->num_cols + src_col;
92962306a36Sopenharmony_ci
93062306a36Sopenharmony_ci	/*
93162306a36Sopenharmony_ci	 * before doing the transform, first we have to translate
93262306a36Sopenharmony_ci	 * source row,col for an origin in the center of s_image
93362306a36Sopenharmony_ci	 */
93462306a36Sopenharmony_ci	src_row = src_row * 2 - (s_image->num_rows - 1);
93562306a36Sopenharmony_ci	src_col = src_col * 2 - (s_image->num_cols - 1);
93662306a36Sopenharmony_ci
93762306a36Sopenharmony_ci	/* do the rotation transform */
93862306a36Sopenharmony_ci	if (ctx->rot_mode & IPU_ROT_BIT_90) {
93962306a36Sopenharmony_ci		dst_col = -src_row;
94062306a36Sopenharmony_ci		dst_row = src_col;
94162306a36Sopenharmony_ci	} else {
94262306a36Sopenharmony_ci		dst_col = src_col;
94362306a36Sopenharmony_ci		dst_row = src_row;
94462306a36Sopenharmony_ci	}
94562306a36Sopenharmony_ci
94662306a36Sopenharmony_ci	/* apply flip */
94762306a36Sopenharmony_ci	if (ctx->rot_mode & IPU_ROT_BIT_HFLIP)
94862306a36Sopenharmony_ci		dst_col = -dst_col;
94962306a36Sopenharmony_ci	if (ctx->rot_mode & IPU_ROT_BIT_VFLIP)
95062306a36Sopenharmony_ci		dst_row = -dst_row;
95162306a36Sopenharmony_ci
95262306a36Sopenharmony_ci	dev_dbg(priv->ipu->dev, "task %u: ctx %p: [%d,%d] --> [%d,%d]\n",
95362306a36Sopenharmony_ci		chan->ic_task, ctx, src_col, src_row, dst_col, dst_row);
95462306a36Sopenharmony_ci
95562306a36Sopenharmony_ci	/*
95662306a36Sopenharmony_ci	 * finally translate dest row,col using an origin in upper
95762306a36Sopenharmony_ci	 * left of d_image
95862306a36Sopenharmony_ci	 */
95962306a36Sopenharmony_ci	dst_row += d_image->num_rows - 1;
96062306a36Sopenharmony_ci	dst_col += d_image->num_cols - 1;
96162306a36Sopenharmony_ci	dst_row /= 2;
96262306a36Sopenharmony_ci	dst_col /= 2;
96362306a36Sopenharmony_ci
96462306a36Sopenharmony_ci	return dst_row * d_image->num_cols + dst_col;
96562306a36Sopenharmony_ci}
96662306a36Sopenharmony_ci
96762306a36Sopenharmony_ci/*
96862306a36Sopenharmony_ci * Fill the out_tile_map[] with transformed destination tile indeces.
96962306a36Sopenharmony_ci */
97062306a36Sopenharmony_cistatic void calc_out_tile_map(struct ipu_image_convert_ctx *ctx)
97162306a36Sopenharmony_ci{
97262306a36Sopenharmony_ci	struct ipu_image_convert_image *s_image = &ctx->in;
97362306a36Sopenharmony_ci	unsigned int row, col, tile = 0;
97462306a36Sopenharmony_ci
97562306a36Sopenharmony_ci	for (row = 0; row < s_image->num_rows; row++) {
97662306a36Sopenharmony_ci		for (col = 0; col < s_image->num_cols; col++) {
97762306a36Sopenharmony_ci			ctx->out_tile_map[tile] =
97862306a36Sopenharmony_ci				transform_tile_index(ctx, row, col);
97962306a36Sopenharmony_ci			tile++;
98062306a36Sopenharmony_ci		}
98162306a36Sopenharmony_ci	}
98262306a36Sopenharmony_ci}
98362306a36Sopenharmony_ci
98462306a36Sopenharmony_cistatic int calc_tile_offsets_planar(struct ipu_image_convert_ctx *ctx,
98562306a36Sopenharmony_ci				    struct ipu_image_convert_image *image)
98662306a36Sopenharmony_ci{
98762306a36Sopenharmony_ci	struct ipu_image_convert_chan *chan = ctx->chan;
98862306a36Sopenharmony_ci	struct ipu_image_convert_priv *priv = chan->priv;
98962306a36Sopenharmony_ci	const struct ipu_image_pixfmt *fmt = image->fmt;
99062306a36Sopenharmony_ci	unsigned int row, col, tile = 0;
99162306a36Sopenharmony_ci	u32 H, top, y_stride, uv_stride;
99262306a36Sopenharmony_ci	u32 uv_row_off, uv_col_off, uv_off, u_off, v_off;
99362306a36Sopenharmony_ci	u32 y_row_off, y_col_off, y_off;
99462306a36Sopenharmony_ci	u32 y_size, uv_size;
99562306a36Sopenharmony_ci
99662306a36Sopenharmony_ci	/* setup some convenience vars */
99762306a36Sopenharmony_ci	H = image->base.pix.height;
99862306a36Sopenharmony_ci
99962306a36Sopenharmony_ci	y_stride = image->stride;
100062306a36Sopenharmony_ci	uv_stride = y_stride / fmt->uv_width_dec;
100162306a36Sopenharmony_ci	if (fmt->uv_packed)
100262306a36Sopenharmony_ci		uv_stride *= 2;
100362306a36Sopenharmony_ci
100462306a36Sopenharmony_ci	y_size = H * y_stride;
100562306a36Sopenharmony_ci	uv_size = y_size / (fmt->uv_width_dec * fmt->uv_height_dec);
100662306a36Sopenharmony_ci
100762306a36Sopenharmony_ci	for (row = 0; row < image->num_rows; row++) {
100862306a36Sopenharmony_ci		top = image->tile[tile].top;
100962306a36Sopenharmony_ci		y_row_off = top * y_stride;
101062306a36Sopenharmony_ci		uv_row_off = (top * uv_stride) / fmt->uv_height_dec;
101162306a36Sopenharmony_ci
101262306a36Sopenharmony_ci		for (col = 0; col < image->num_cols; col++) {
101362306a36Sopenharmony_ci			y_col_off = image->tile[tile].left;
101462306a36Sopenharmony_ci			uv_col_off = y_col_off / fmt->uv_width_dec;
101562306a36Sopenharmony_ci			if (fmt->uv_packed)
101662306a36Sopenharmony_ci				uv_col_off *= 2;
101762306a36Sopenharmony_ci
101862306a36Sopenharmony_ci			y_off = y_row_off + y_col_off;
101962306a36Sopenharmony_ci			uv_off = uv_row_off + uv_col_off;
102062306a36Sopenharmony_ci
102162306a36Sopenharmony_ci			u_off = y_size - y_off + uv_off;
102262306a36Sopenharmony_ci			v_off = (fmt->uv_packed) ? 0 : u_off + uv_size;
102362306a36Sopenharmony_ci			if (fmt->uv_swapped)
102462306a36Sopenharmony_ci				swap(u_off, v_off);
102562306a36Sopenharmony_ci
102662306a36Sopenharmony_ci			image->tile[tile].offset = y_off;
102762306a36Sopenharmony_ci			image->tile[tile].u_off = u_off;
102862306a36Sopenharmony_ci			image->tile[tile++].v_off = v_off;
102962306a36Sopenharmony_ci
103062306a36Sopenharmony_ci			if ((y_off & 0x7) || (u_off & 0x7) || (v_off & 0x7)) {
103162306a36Sopenharmony_ci				dev_err(priv->ipu->dev,
103262306a36Sopenharmony_ci					"task %u: ctx %p: %s@[%d,%d]: "
103362306a36Sopenharmony_ci					"y_off %08x, u_off %08x, v_off %08x\n",
103462306a36Sopenharmony_ci					chan->ic_task, ctx,
103562306a36Sopenharmony_ci					image->type == IMAGE_CONVERT_IN ?
103662306a36Sopenharmony_ci					"Input" : "Output", row, col,
103762306a36Sopenharmony_ci					y_off, u_off, v_off);
103862306a36Sopenharmony_ci				return -EINVAL;
103962306a36Sopenharmony_ci			}
104062306a36Sopenharmony_ci		}
104162306a36Sopenharmony_ci	}
104262306a36Sopenharmony_ci
104362306a36Sopenharmony_ci	return 0;
104462306a36Sopenharmony_ci}
104562306a36Sopenharmony_ci
104662306a36Sopenharmony_cistatic int calc_tile_offsets_packed(struct ipu_image_convert_ctx *ctx,
104762306a36Sopenharmony_ci				    struct ipu_image_convert_image *image)
104862306a36Sopenharmony_ci{
104962306a36Sopenharmony_ci	struct ipu_image_convert_chan *chan = ctx->chan;
105062306a36Sopenharmony_ci	struct ipu_image_convert_priv *priv = chan->priv;
105162306a36Sopenharmony_ci	const struct ipu_image_pixfmt *fmt = image->fmt;
105262306a36Sopenharmony_ci	unsigned int row, col, tile = 0;
105362306a36Sopenharmony_ci	u32 bpp, stride, offset;
105462306a36Sopenharmony_ci	u32 row_off, col_off;
105562306a36Sopenharmony_ci
105662306a36Sopenharmony_ci	/* setup some convenience vars */
105762306a36Sopenharmony_ci	stride = image->stride;
105862306a36Sopenharmony_ci	bpp = fmt->bpp;
105962306a36Sopenharmony_ci
106062306a36Sopenharmony_ci	for (row = 0; row < image->num_rows; row++) {
106162306a36Sopenharmony_ci		row_off = image->tile[tile].top * stride;
106262306a36Sopenharmony_ci
106362306a36Sopenharmony_ci		for (col = 0; col < image->num_cols; col++) {
106462306a36Sopenharmony_ci			col_off = (image->tile[tile].left * bpp) >> 3;
106562306a36Sopenharmony_ci
106662306a36Sopenharmony_ci			offset = row_off + col_off;
106762306a36Sopenharmony_ci
106862306a36Sopenharmony_ci			image->tile[tile].offset = offset;
106962306a36Sopenharmony_ci			image->tile[tile].u_off = 0;
107062306a36Sopenharmony_ci			image->tile[tile++].v_off = 0;
107162306a36Sopenharmony_ci
107262306a36Sopenharmony_ci			if (offset & 0x7) {
107362306a36Sopenharmony_ci				dev_err(priv->ipu->dev,
107462306a36Sopenharmony_ci					"task %u: ctx %p: %s@[%d,%d]: "
107562306a36Sopenharmony_ci					"phys %08x\n",
107662306a36Sopenharmony_ci					chan->ic_task, ctx,
107762306a36Sopenharmony_ci					image->type == IMAGE_CONVERT_IN ?
107862306a36Sopenharmony_ci					"Input" : "Output", row, col,
107962306a36Sopenharmony_ci					row_off + col_off);
108062306a36Sopenharmony_ci				return -EINVAL;
108162306a36Sopenharmony_ci			}
108262306a36Sopenharmony_ci		}
108362306a36Sopenharmony_ci	}
108462306a36Sopenharmony_ci
108562306a36Sopenharmony_ci	return 0;
108662306a36Sopenharmony_ci}
108762306a36Sopenharmony_ci
108862306a36Sopenharmony_cistatic int calc_tile_offsets(struct ipu_image_convert_ctx *ctx,
108962306a36Sopenharmony_ci			      struct ipu_image_convert_image *image)
109062306a36Sopenharmony_ci{
109162306a36Sopenharmony_ci	if (image->fmt->planar)
109262306a36Sopenharmony_ci		return calc_tile_offsets_planar(ctx, image);
109362306a36Sopenharmony_ci
109462306a36Sopenharmony_ci	return calc_tile_offsets_packed(ctx, image);
109562306a36Sopenharmony_ci}
109662306a36Sopenharmony_ci
109762306a36Sopenharmony_ci/*
109862306a36Sopenharmony_ci * Calculate the resizing ratio for the IC main processing section given input
109962306a36Sopenharmony_ci * size, fixed downsizing coefficient, and output size.
110062306a36Sopenharmony_ci * Either round to closest for the next tile's first pixel to minimize seams
110162306a36Sopenharmony_ci * and distortion (for all but right column / bottom row), or round down to
110262306a36Sopenharmony_ci * avoid sampling beyond the edges of the input image for this tile's last
110362306a36Sopenharmony_ci * pixel.
110462306a36Sopenharmony_ci * Returns the resizing coefficient, resizing ratio is 8192.0 / resize_coeff.
110562306a36Sopenharmony_ci */
110662306a36Sopenharmony_cistatic u32 calc_resize_coeff(u32 input_size, u32 downsize_coeff,
110762306a36Sopenharmony_ci			     u32 output_size, bool allow_overshoot)
110862306a36Sopenharmony_ci{
110962306a36Sopenharmony_ci	u32 downsized = input_size >> downsize_coeff;
111062306a36Sopenharmony_ci
111162306a36Sopenharmony_ci	if (allow_overshoot)
111262306a36Sopenharmony_ci		return DIV_ROUND_CLOSEST(8192 * downsized, output_size);
111362306a36Sopenharmony_ci	else
111462306a36Sopenharmony_ci		return 8192 * (downsized - 1) / (output_size - 1);
111562306a36Sopenharmony_ci}
111662306a36Sopenharmony_ci
111762306a36Sopenharmony_ci/*
111862306a36Sopenharmony_ci * Slightly modify resize coefficients per tile to hide the bilinear
111962306a36Sopenharmony_ci * interpolator reset at tile borders, shifting the right / bottom edge
112062306a36Sopenharmony_ci * by up to a half input pixel. This removes noticeable seams between
112162306a36Sopenharmony_ci * tiles at higher upscaling factors.
112262306a36Sopenharmony_ci */
112362306a36Sopenharmony_cistatic void calc_tile_resize_coefficients(struct ipu_image_convert_ctx *ctx)
112462306a36Sopenharmony_ci{
112562306a36Sopenharmony_ci	struct ipu_image_convert_chan *chan = ctx->chan;
112662306a36Sopenharmony_ci	struct ipu_image_convert_priv *priv = chan->priv;
112762306a36Sopenharmony_ci	struct ipu_image_tile *in_tile, *out_tile;
112862306a36Sopenharmony_ci	unsigned int col, row, tile_idx;
112962306a36Sopenharmony_ci	unsigned int last_output;
113062306a36Sopenharmony_ci
113162306a36Sopenharmony_ci	for (col = 0; col < ctx->in.num_cols; col++) {
113262306a36Sopenharmony_ci		bool closest = (col < ctx->in.num_cols - 1) &&
113362306a36Sopenharmony_ci			       !(ctx->rot_mode & IPU_ROT_BIT_HFLIP);
113462306a36Sopenharmony_ci		u32 resized_width;
113562306a36Sopenharmony_ci		u32 resize_coeff_h;
113662306a36Sopenharmony_ci		u32 in_width;
113762306a36Sopenharmony_ci
113862306a36Sopenharmony_ci		tile_idx = col;
113962306a36Sopenharmony_ci		in_tile = &ctx->in.tile[tile_idx];
114062306a36Sopenharmony_ci		out_tile = &ctx->out.tile[ctx->out_tile_map[tile_idx]];
114162306a36Sopenharmony_ci
114262306a36Sopenharmony_ci		if (ipu_rot_mode_is_irt(ctx->rot_mode))
114362306a36Sopenharmony_ci			resized_width = out_tile->height;
114462306a36Sopenharmony_ci		else
114562306a36Sopenharmony_ci			resized_width = out_tile->width;
114662306a36Sopenharmony_ci
114762306a36Sopenharmony_ci		resize_coeff_h = calc_resize_coeff(in_tile->width,
114862306a36Sopenharmony_ci						   ctx->downsize_coeff_h,
114962306a36Sopenharmony_ci						   resized_width, closest);
115062306a36Sopenharmony_ci
115162306a36Sopenharmony_ci		dev_dbg(priv->ipu->dev, "%s: column %u hscale: *8192/%u\n",
115262306a36Sopenharmony_ci			__func__, col, resize_coeff_h);
115362306a36Sopenharmony_ci
115462306a36Sopenharmony_ci		/*
115562306a36Sopenharmony_ci		 * With the horizontal scaling factor known, round up resized
115662306a36Sopenharmony_ci		 * width (output width or height) to burst size.
115762306a36Sopenharmony_ci		 */
115862306a36Sopenharmony_ci		resized_width = round_up(resized_width, 8);
115962306a36Sopenharmony_ci
116062306a36Sopenharmony_ci		/*
116162306a36Sopenharmony_ci		 * Calculate input width from the last accessed input pixel
116262306a36Sopenharmony_ci		 * given resized width and scaling coefficients. Round up to
116362306a36Sopenharmony_ci		 * burst size.
116462306a36Sopenharmony_ci		 */
116562306a36Sopenharmony_ci		last_output = resized_width - 1;
116662306a36Sopenharmony_ci		if (closest && ((last_output * resize_coeff_h) % 8192))
116762306a36Sopenharmony_ci			last_output++;
116862306a36Sopenharmony_ci		in_width = round_up(
116962306a36Sopenharmony_ci			(DIV_ROUND_UP(last_output * resize_coeff_h, 8192) + 1)
117062306a36Sopenharmony_ci			<< ctx->downsize_coeff_h, 8);
117162306a36Sopenharmony_ci
117262306a36Sopenharmony_ci		for (row = 0; row < ctx->in.num_rows; row++) {
117362306a36Sopenharmony_ci			tile_idx = row * ctx->in.num_cols + col;
117462306a36Sopenharmony_ci			in_tile = &ctx->in.tile[tile_idx];
117562306a36Sopenharmony_ci			out_tile = &ctx->out.tile[ctx->out_tile_map[tile_idx]];
117662306a36Sopenharmony_ci
117762306a36Sopenharmony_ci			if (ipu_rot_mode_is_irt(ctx->rot_mode))
117862306a36Sopenharmony_ci				out_tile->height = resized_width;
117962306a36Sopenharmony_ci			else
118062306a36Sopenharmony_ci				out_tile->width = resized_width;
118162306a36Sopenharmony_ci
118262306a36Sopenharmony_ci			in_tile->width = in_width;
118362306a36Sopenharmony_ci		}
118462306a36Sopenharmony_ci
118562306a36Sopenharmony_ci		ctx->resize_coeffs_h[col] = resize_coeff_h;
118662306a36Sopenharmony_ci	}
118762306a36Sopenharmony_ci
118862306a36Sopenharmony_ci	for (row = 0; row < ctx->in.num_rows; row++) {
118962306a36Sopenharmony_ci		bool closest = (row < ctx->in.num_rows - 1) &&
119062306a36Sopenharmony_ci			       !(ctx->rot_mode & IPU_ROT_BIT_VFLIP);
119162306a36Sopenharmony_ci		u32 resized_height;
119262306a36Sopenharmony_ci		u32 resize_coeff_v;
119362306a36Sopenharmony_ci		u32 in_height;
119462306a36Sopenharmony_ci
119562306a36Sopenharmony_ci		tile_idx = row * ctx->in.num_cols;
119662306a36Sopenharmony_ci		in_tile = &ctx->in.tile[tile_idx];
119762306a36Sopenharmony_ci		out_tile = &ctx->out.tile[ctx->out_tile_map[tile_idx]];
119862306a36Sopenharmony_ci
119962306a36Sopenharmony_ci		if (ipu_rot_mode_is_irt(ctx->rot_mode))
120062306a36Sopenharmony_ci			resized_height = out_tile->width;
120162306a36Sopenharmony_ci		else
120262306a36Sopenharmony_ci			resized_height = out_tile->height;
120362306a36Sopenharmony_ci
120462306a36Sopenharmony_ci		resize_coeff_v = calc_resize_coeff(in_tile->height,
120562306a36Sopenharmony_ci						   ctx->downsize_coeff_v,
120662306a36Sopenharmony_ci						   resized_height, closest);
120762306a36Sopenharmony_ci
120862306a36Sopenharmony_ci		dev_dbg(priv->ipu->dev, "%s: row %u vscale: *8192/%u\n",
120962306a36Sopenharmony_ci			__func__, row, resize_coeff_v);
121062306a36Sopenharmony_ci
121162306a36Sopenharmony_ci		/*
121262306a36Sopenharmony_ci		 * With the vertical scaling factor known, round up resized
121362306a36Sopenharmony_ci		 * height (output width or height) to IDMAC limitations.
121462306a36Sopenharmony_ci		 */
121562306a36Sopenharmony_ci		resized_height = round_up(resized_height, 2);
121662306a36Sopenharmony_ci
121762306a36Sopenharmony_ci		/*
121862306a36Sopenharmony_ci		 * Calculate input width from the last accessed input pixel
121962306a36Sopenharmony_ci		 * given resized height and scaling coefficients. Align to
122062306a36Sopenharmony_ci		 * IDMAC restrictions.
122162306a36Sopenharmony_ci		 */
122262306a36Sopenharmony_ci		last_output = resized_height - 1;
122362306a36Sopenharmony_ci		if (closest && ((last_output * resize_coeff_v) % 8192))
122462306a36Sopenharmony_ci			last_output++;
122562306a36Sopenharmony_ci		in_height = round_up(
122662306a36Sopenharmony_ci			(DIV_ROUND_UP(last_output * resize_coeff_v, 8192) + 1)
122762306a36Sopenharmony_ci			<< ctx->downsize_coeff_v, 2);
122862306a36Sopenharmony_ci
122962306a36Sopenharmony_ci		for (col = 0; col < ctx->in.num_cols; col++) {
123062306a36Sopenharmony_ci			tile_idx = row * ctx->in.num_cols + col;
123162306a36Sopenharmony_ci			in_tile = &ctx->in.tile[tile_idx];
123262306a36Sopenharmony_ci			out_tile = &ctx->out.tile[ctx->out_tile_map[tile_idx]];
123362306a36Sopenharmony_ci
123462306a36Sopenharmony_ci			if (ipu_rot_mode_is_irt(ctx->rot_mode))
123562306a36Sopenharmony_ci				out_tile->width = resized_height;
123662306a36Sopenharmony_ci			else
123762306a36Sopenharmony_ci				out_tile->height = resized_height;
123862306a36Sopenharmony_ci
123962306a36Sopenharmony_ci			in_tile->height = in_height;
124062306a36Sopenharmony_ci		}
124162306a36Sopenharmony_ci
124262306a36Sopenharmony_ci		ctx->resize_coeffs_v[row] = resize_coeff_v;
124362306a36Sopenharmony_ci	}
124462306a36Sopenharmony_ci}
124562306a36Sopenharmony_ci
124662306a36Sopenharmony_ci/*
124762306a36Sopenharmony_ci * return the number of runs in given queue (pending_q or done_q)
124862306a36Sopenharmony_ci * for this context. hold irqlock when calling.
124962306a36Sopenharmony_ci */
125062306a36Sopenharmony_cistatic int get_run_count(struct ipu_image_convert_ctx *ctx,
125162306a36Sopenharmony_ci			 struct list_head *q)
125262306a36Sopenharmony_ci{
125362306a36Sopenharmony_ci	struct ipu_image_convert_run *run;
125462306a36Sopenharmony_ci	int count = 0;
125562306a36Sopenharmony_ci
125662306a36Sopenharmony_ci	lockdep_assert_held(&ctx->chan->irqlock);
125762306a36Sopenharmony_ci
125862306a36Sopenharmony_ci	list_for_each_entry(run, q, list) {
125962306a36Sopenharmony_ci		if (run->ctx == ctx)
126062306a36Sopenharmony_ci			count++;
126162306a36Sopenharmony_ci	}
126262306a36Sopenharmony_ci
126362306a36Sopenharmony_ci	return count;
126462306a36Sopenharmony_ci}
126562306a36Sopenharmony_ci
126662306a36Sopenharmony_cistatic void convert_stop(struct ipu_image_convert_run *run)
126762306a36Sopenharmony_ci{
126862306a36Sopenharmony_ci	struct ipu_image_convert_ctx *ctx = run->ctx;
126962306a36Sopenharmony_ci	struct ipu_image_convert_chan *chan = ctx->chan;
127062306a36Sopenharmony_ci	struct ipu_image_convert_priv *priv = chan->priv;
127162306a36Sopenharmony_ci
127262306a36Sopenharmony_ci	dev_dbg(priv->ipu->dev, "%s: task %u: stopping ctx %p run %p\n",
127362306a36Sopenharmony_ci		__func__, chan->ic_task, ctx, run);
127462306a36Sopenharmony_ci
127562306a36Sopenharmony_ci	/* disable IC tasks and the channels */
127662306a36Sopenharmony_ci	ipu_ic_task_disable(chan->ic);
127762306a36Sopenharmony_ci	ipu_idmac_disable_channel(chan->in_chan);
127862306a36Sopenharmony_ci	ipu_idmac_disable_channel(chan->out_chan);
127962306a36Sopenharmony_ci
128062306a36Sopenharmony_ci	if (ipu_rot_mode_is_irt(ctx->rot_mode)) {
128162306a36Sopenharmony_ci		ipu_idmac_disable_channel(chan->rotation_in_chan);
128262306a36Sopenharmony_ci		ipu_idmac_disable_channel(chan->rotation_out_chan);
128362306a36Sopenharmony_ci		ipu_idmac_unlink(chan->out_chan, chan->rotation_in_chan);
128462306a36Sopenharmony_ci	}
128562306a36Sopenharmony_ci
128662306a36Sopenharmony_ci	ipu_ic_disable(chan->ic);
128762306a36Sopenharmony_ci}
128862306a36Sopenharmony_ci
128962306a36Sopenharmony_cistatic void init_idmac_channel(struct ipu_image_convert_ctx *ctx,
129062306a36Sopenharmony_ci			       struct ipuv3_channel *channel,
129162306a36Sopenharmony_ci			       struct ipu_image_convert_image *image,
129262306a36Sopenharmony_ci			       enum ipu_rotate_mode rot_mode,
129362306a36Sopenharmony_ci			       bool rot_swap_width_height,
129462306a36Sopenharmony_ci			       unsigned int tile)
129562306a36Sopenharmony_ci{
129662306a36Sopenharmony_ci	struct ipu_image_convert_chan *chan = ctx->chan;
129762306a36Sopenharmony_ci	unsigned int burst_size;
129862306a36Sopenharmony_ci	u32 width, height, stride;
129962306a36Sopenharmony_ci	dma_addr_t addr0, addr1 = 0;
130062306a36Sopenharmony_ci	struct ipu_image tile_image;
130162306a36Sopenharmony_ci	unsigned int tile_idx[2];
130262306a36Sopenharmony_ci
130362306a36Sopenharmony_ci	if (image->type == IMAGE_CONVERT_OUT) {
130462306a36Sopenharmony_ci		tile_idx[0] = ctx->out_tile_map[tile];
130562306a36Sopenharmony_ci		tile_idx[1] = ctx->out_tile_map[1];
130662306a36Sopenharmony_ci	} else {
130762306a36Sopenharmony_ci		tile_idx[0] = tile;
130862306a36Sopenharmony_ci		tile_idx[1] = 1;
130962306a36Sopenharmony_ci	}
131062306a36Sopenharmony_ci
131162306a36Sopenharmony_ci	if (rot_swap_width_height) {
131262306a36Sopenharmony_ci		width = image->tile[tile_idx[0]].height;
131362306a36Sopenharmony_ci		height = image->tile[tile_idx[0]].width;
131462306a36Sopenharmony_ci		stride = image->tile[tile_idx[0]].rot_stride;
131562306a36Sopenharmony_ci		addr0 = ctx->rot_intermediate[0].phys;
131662306a36Sopenharmony_ci		if (ctx->double_buffering)
131762306a36Sopenharmony_ci			addr1 = ctx->rot_intermediate[1].phys;
131862306a36Sopenharmony_ci	} else {
131962306a36Sopenharmony_ci		width = image->tile[tile_idx[0]].width;
132062306a36Sopenharmony_ci		height = image->tile[tile_idx[0]].height;
132162306a36Sopenharmony_ci		stride = image->stride;
132262306a36Sopenharmony_ci		addr0 = image->base.phys0 +
132362306a36Sopenharmony_ci			image->tile[tile_idx[0]].offset;
132462306a36Sopenharmony_ci		if (ctx->double_buffering)
132562306a36Sopenharmony_ci			addr1 = image->base.phys0 +
132662306a36Sopenharmony_ci				image->tile[tile_idx[1]].offset;
132762306a36Sopenharmony_ci	}
132862306a36Sopenharmony_ci
132962306a36Sopenharmony_ci	ipu_cpmem_zero(channel);
133062306a36Sopenharmony_ci
133162306a36Sopenharmony_ci	memset(&tile_image, 0, sizeof(tile_image));
133262306a36Sopenharmony_ci	tile_image.pix.width = tile_image.rect.width = width;
133362306a36Sopenharmony_ci	tile_image.pix.height = tile_image.rect.height = height;
133462306a36Sopenharmony_ci	tile_image.pix.bytesperline = stride;
133562306a36Sopenharmony_ci	tile_image.pix.pixelformat =  image->fmt->fourcc;
133662306a36Sopenharmony_ci	tile_image.phys0 = addr0;
133762306a36Sopenharmony_ci	tile_image.phys1 = addr1;
133862306a36Sopenharmony_ci	if (image->fmt->planar && !rot_swap_width_height) {
133962306a36Sopenharmony_ci		tile_image.u_offset = image->tile[tile_idx[0]].u_off;
134062306a36Sopenharmony_ci		tile_image.v_offset = image->tile[tile_idx[0]].v_off;
134162306a36Sopenharmony_ci	}
134262306a36Sopenharmony_ci
134362306a36Sopenharmony_ci	ipu_cpmem_set_image(channel, &tile_image);
134462306a36Sopenharmony_ci
134562306a36Sopenharmony_ci	if (rot_mode)
134662306a36Sopenharmony_ci		ipu_cpmem_set_rotation(channel, rot_mode);
134762306a36Sopenharmony_ci
134862306a36Sopenharmony_ci	/*
134962306a36Sopenharmony_ci	 * Skip writing U and V components to odd rows in the output
135062306a36Sopenharmony_ci	 * channels for planar 4:2:0.
135162306a36Sopenharmony_ci	 */
135262306a36Sopenharmony_ci	if ((channel == chan->out_chan ||
135362306a36Sopenharmony_ci	     channel == chan->rotation_out_chan) &&
135462306a36Sopenharmony_ci	    image->fmt->planar && image->fmt->uv_height_dec == 2)
135562306a36Sopenharmony_ci		ipu_cpmem_skip_odd_chroma_rows(channel);
135662306a36Sopenharmony_ci
135762306a36Sopenharmony_ci	if (channel == chan->rotation_in_chan ||
135862306a36Sopenharmony_ci	    channel == chan->rotation_out_chan) {
135962306a36Sopenharmony_ci		burst_size = 8;
136062306a36Sopenharmony_ci		ipu_cpmem_set_block_mode(channel);
136162306a36Sopenharmony_ci	} else
136262306a36Sopenharmony_ci		burst_size = (width % 16) ? 8 : 16;
136362306a36Sopenharmony_ci
136462306a36Sopenharmony_ci	ipu_cpmem_set_burstsize(channel, burst_size);
136562306a36Sopenharmony_ci
136662306a36Sopenharmony_ci	ipu_ic_task_idma_init(chan->ic, channel, width, height,
136762306a36Sopenharmony_ci			      burst_size, rot_mode);
136862306a36Sopenharmony_ci
136962306a36Sopenharmony_ci	/*
137062306a36Sopenharmony_ci	 * Setting a non-zero AXI ID collides with the PRG AXI snooping, so
137162306a36Sopenharmony_ci	 * only do this when there is no PRG present.
137262306a36Sopenharmony_ci	 */
137362306a36Sopenharmony_ci	if (!channel->ipu->prg_priv)
137462306a36Sopenharmony_ci		ipu_cpmem_set_axi_id(channel, 1);
137562306a36Sopenharmony_ci
137662306a36Sopenharmony_ci	ipu_idmac_set_double_buffer(channel, ctx->double_buffering);
137762306a36Sopenharmony_ci}
137862306a36Sopenharmony_ci
137962306a36Sopenharmony_cistatic int convert_start(struct ipu_image_convert_run *run, unsigned int tile)
138062306a36Sopenharmony_ci{
138162306a36Sopenharmony_ci	struct ipu_image_convert_ctx *ctx = run->ctx;
138262306a36Sopenharmony_ci	struct ipu_image_convert_chan *chan = ctx->chan;
138362306a36Sopenharmony_ci	struct ipu_image_convert_priv *priv = chan->priv;
138462306a36Sopenharmony_ci	struct ipu_image_convert_image *s_image = &ctx->in;
138562306a36Sopenharmony_ci	struct ipu_image_convert_image *d_image = &ctx->out;
138662306a36Sopenharmony_ci	unsigned int dst_tile = ctx->out_tile_map[tile];
138762306a36Sopenharmony_ci	unsigned int dest_width, dest_height;
138862306a36Sopenharmony_ci	unsigned int col, row;
138962306a36Sopenharmony_ci	u32 rsc;
139062306a36Sopenharmony_ci	int ret;
139162306a36Sopenharmony_ci
139262306a36Sopenharmony_ci	dev_dbg(priv->ipu->dev, "%s: task %u: starting ctx %p run %p tile %u -> %u\n",
139362306a36Sopenharmony_ci		__func__, chan->ic_task, ctx, run, tile, dst_tile);
139462306a36Sopenharmony_ci
139562306a36Sopenharmony_ci	/* clear EOF irq mask */
139662306a36Sopenharmony_ci	ctx->eof_mask = 0;
139762306a36Sopenharmony_ci
139862306a36Sopenharmony_ci	if (ipu_rot_mode_is_irt(ctx->rot_mode)) {
139962306a36Sopenharmony_ci		/* swap width/height for resizer */
140062306a36Sopenharmony_ci		dest_width = d_image->tile[dst_tile].height;
140162306a36Sopenharmony_ci		dest_height = d_image->tile[dst_tile].width;
140262306a36Sopenharmony_ci	} else {
140362306a36Sopenharmony_ci		dest_width = d_image->tile[dst_tile].width;
140462306a36Sopenharmony_ci		dest_height = d_image->tile[dst_tile].height;
140562306a36Sopenharmony_ci	}
140662306a36Sopenharmony_ci
140762306a36Sopenharmony_ci	row = tile / s_image->num_cols;
140862306a36Sopenharmony_ci	col = tile % s_image->num_cols;
140962306a36Sopenharmony_ci
141062306a36Sopenharmony_ci	rsc =  (ctx->downsize_coeff_v << 30) |
141162306a36Sopenharmony_ci	       (ctx->resize_coeffs_v[row] << 16) |
141262306a36Sopenharmony_ci	       (ctx->downsize_coeff_h << 14) |
141362306a36Sopenharmony_ci	       (ctx->resize_coeffs_h[col]);
141462306a36Sopenharmony_ci
141562306a36Sopenharmony_ci	dev_dbg(priv->ipu->dev, "%s: %ux%u -> %ux%u (rsc = 0x%x)\n",
141662306a36Sopenharmony_ci		__func__, s_image->tile[tile].width,
141762306a36Sopenharmony_ci		s_image->tile[tile].height, dest_width, dest_height, rsc);
141862306a36Sopenharmony_ci
141962306a36Sopenharmony_ci	/* setup the IC resizer and CSC */
142062306a36Sopenharmony_ci	ret = ipu_ic_task_init_rsc(chan->ic, &ctx->csc,
142162306a36Sopenharmony_ci				   s_image->tile[tile].width,
142262306a36Sopenharmony_ci				   s_image->tile[tile].height,
142362306a36Sopenharmony_ci				   dest_width,
142462306a36Sopenharmony_ci				   dest_height,
142562306a36Sopenharmony_ci				   rsc);
142662306a36Sopenharmony_ci	if (ret) {
142762306a36Sopenharmony_ci		dev_err(priv->ipu->dev, "ipu_ic_task_init failed, %d\n", ret);
142862306a36Sopenharmony_ci		return ret;
142962306a36Sopenharmony_ci	}
143062306a36Sopenharmony_ci
143162306a36Sopenharmony_ci	/* init the source MEM-->IC PP IDMAC channel */
143262306a36Sopenharmony_ci	init_idmac_channel(ctx, chan->in_chan, s_image,
143362306a36Sopenharmony_ci			   IPU_ROTATE_NONE, false, tile);
143462306a36Sopenharmony_ci
143562306a36Sopenharmony_ci	if (ipu_rot_mode_is_irt(ctx->rot_mode)) {
143662306a36Sopenharmony_ci		/* init the IC PP-->MEM IDMAC channel */
143762306a36Sopenharmony_ci		init_idmac_channel(ctx, chan->out_chan, d_image,
143862306a36Sopenharmony_ci				   IPU_ROTATE_NONE, true, tile);
143962306a36Sopenharmony_ci
144062306a36Sopenharmony_ci		/* init the MEM-->IC PP ROT IDMAC channel */
144162306a36Sopenharmony_ci		init_idmac_channel(ctx, chan->rotation_in_chan, d_image,
144262306a36Sopenharmony_ci				   ctx->rot_mode, true, tile);
144362306a36Sopenharmony_ci
144462306a36Sopenharmony_ci		/* init the destination IC PP ROT-->MEM IDMAC channel */
144562306a36Sopenharmony_ci		init_idmac_channel(ctx, chan->rotation_out_chan, d_image,
144662306a36Sopenharmony_ci				   IPU_ROTATE_NONE, false, tile);
144762306a36Sopenharmony_ci
144862306a36Sopenharmony_ci		/* now link IC PP-->MEM to MEM-->IC PP ROT */
144962306a36Sopenharmony_ci		ipu_idmac_link(chan->out_chan, chan->rotation_in_chan);
145062306a36Sopenharmony_ci	} else {
145162306a36Sopenharmony_ci		/* init the destination IC PP-->MEM IDMAC channel */
145262306a36Sopenharmony_ci		init_idmac_channel(ctx, chan->out_chan, d_image,
145362306a36Sopenharmony_ci				   ctx->rot_mode, false, tile);
145462306a36Sopenharmony_ci	}
145562306a36Sopenharmony_ci
145662306a36Sopenharmony_ci	/* enable the IC */
145762306a36Sopenharmony_ci	ipu_ic_enable(chan->ic);
145862306a36Sopenharmony_ci
145962306a36Sopenharmony_ci	/* set buffers ready */
146062306a36Sopenharmony_ci	ipu_idmac_select_buffer(chan->in_chan, 0);
146162306a36Sopenharmony_ci	ipu_idmac_select_buffer(chan->out_chan, 0);
146262306a36Sopenharmony_ci	if (ipu_rot_mode_is_irt(ctx->rot_mode))
146362306a36Sopenharmony_ci		ipu_idmac_select_buffer(chan->rotation_out_chan, 0);
146462306a36Sopenharmony_ci	if (ctx->double_buffering) {
146562306a36Sopenharmony_ci		ipu_idmac_select_buffer(chan->in_chan, 1);
146662306a36Sopenharmony_ci		ipu_idmac_select_buffer(chan->out_chan, 1);
146762306a36Sopenharmony_ci		if (ipu_rot_mode_is_irt(ctx->rot_mode))
146862306a36Sopenharmony_ci			ipu_idmac_select_buffer(chan->rotation_out_chan, 1);
146962306a36Sopenharmony_ci	}
147062306a36Sopenharmony_ci
147162306a36Sopenharmony_ci	/* enable the channels! */
147262306a36Sopenharmony_ci	ipu_idmac_enable_channel(chan->in_chan);
147362306a36Sopenharmony_ci	ipu_idmac_enable_channel(chan->out_chan);
147462306a36Sopenharmony_ci	if (ipu_rot_mode_is_irt(ctx->rot_mode)) {
147562306a36Sopenharmony_ci		ipu_idmac_enable_channel(chan->rotation_in_chan);
147662306a36Sopenharmony_ci		ipu_idmac_enable_channel(chan->rotation_out_chan);
147762306a36Sopenharmony_ci	}
147862306a36Sopenharmony_ci
147962306a36Sopenharmony_ci	ipu_ic_task_enable(chan->ic);
148062306a36Sopenharmony_ci
148162306a36Sopenharmony_ci	ipu_cpmem_dump(chan->in_chan);
148262306a36Sopenharmony_ci	ipu_cpmem_dump(chan->out_chan);
148362306a36Sopenharmony_ci	if (ipu_rot_mode_is_irt(ctx->rot_mode)) {
148462306a36Sopenharmony_ci		ipu_cpmem_dump(chan->rotation_in_chan);
148562306a36Sopenharmony_ci		ipu_cpmem_dump(chan->rotation_out_chan);
148662306a36Sopenharmony_ci	}
148762306a36Sopenharmony_ci
148862306a36Sopenharmony_ci	ipu_dump(priv->ipu);
148962306a36Sopenharmony_ci
149062306a36Sopenharmony_ci	return 0;
149162306a36Sopenharmony_ci}
149262306a36Sopenharmony_ci
149362306a36Sopenharmony_ci/* hold irqlock when calling */
149462306a36Sopenharmony_cistatic int do_run(struct ipu_image_convert_run *run)
149562306a36Sopenharmony_ci{
149662306a36Sopenharmony_ci	struct ipu_image_convert_ctx *ctx = run->ctx;
149762306a36Sopenharmony_ci	struct ipu_image_convert_chan *chan = ctx->chan;
149862306a36Sopenharmony_ci
149962306a36Sopenharmony_ci	lockdep_assert_held(&chan->irqlock);
150062306a36Sopenharmony_ci
150162306a36Sopenharmony_ci	ctx->in.base.phys0 = run->in_phys;
150262306a36Sopenharmony_ci	ctx->out.base.phys0 = run->out_phys;
150362306a36Sopenharmony_ci
150462306a36Sopenharmony_ci	ctx->cur_buf_num = 0;
150562306a36Sopenharmony_ci	ctx->next_tile = 1;
150662306a36Sopenharmony_ci
150762306a36Sopenharmony_ci	/* remove run from pending_q and set as current */
150862306a36Sopenharmony_ci	list_del(&run->list);
150962306a36Sopenharmony_ci	chan->current_run = run;
151062306a36Sopenharmony_ci
151162306a36Sopenharmony_ci	return convert_start(run, 0);
151262306a36Sopenharmony_ci}
151362306a36Sopenharmony_ci
151462306a36Sopenharmony_ci/* hold irqlock when calling */
151562306a36Sopenharmony_cistatic void run_next(struct ipu_image_convert_chan *chan)
151662306a36Sopenharmony_ci{
151762306a36Sopenharmony_ci	struct ipu_image_convert_priv *priv = chan->priv;
151862306a36Sopenharmony_ci	struct ipu_image_convert_run *run, *tmp;
151962306a36Sopenharmony_ci	int ret;
152062306a36Sopenharmony_ci
152162306a36Sopenharmony_ci	lockdep_assert_held(&chan->irqlock);
152262306a36Sopenharmony_ci
152362306a36Sopenharmony_ci	list_for_each_entry_safe(run, tmp, &chan->pending_q, list) {
152462306a36Sopenharmony_ci		/* skip contexts that are aborting */
152562306a36Sopenharmony_ci		if (run->ctx->aborting) {
152662306a36Sopenharmony_ci			dev_dbg(priv->ipu->dev,
152762306a36Sopenharmony_ci				"%s: task %u: skipping aborting ctx %p run %p\n",
152862306a36Sopenharmony_ci				__func__, chan->ic_task, run->ctx, run);
152962306a36Sopenharmony_ci			continue;
153062306a36Sopenharmony_ci		}
153162306a36Sopenharmony_ci
153262306a36Sopenharmony_ci		ret = do_run(run);
153362306a36Sopenharmony_ci		if (!ret)
153462306a36Sopenharmony_ci			break;
153562306a36Sopenharmony_ci
153662306a36Sopenharmony_ci		/*
153762306a36Sopenharmony_ci		 * something went wrong with start, add the run
153862306a36Sopenharmony_ci		 * to done q and continue to the next run in the
153962306a36Sopenharmony_ci		 * pending q.
154062306a36Sopenharmony_ci		 */
154162306a36Sopenharmony_ci		run->status = ret;
154262306a36Sopenharmony_ci		list_add_tail(&run->list, &chan->done_q);
154362306a36Sopenharmony_ci		chan->current_run = NULL;
154462306a36Sopenharmony_ci	}
154562306a36Sopenharmony_ci}
154662306a36Sopenharmony_ci
154762306a36Sopenharmony_cistatic void empty_done_q(struct ipu_image_convert_chan *chan)
154862306a36Sopenharmony_ci{
154962306a36Sopenharmony_ci	struct ipu_image_convert_priv *priv = chan->priv;
155062306a36Sopenharmony_ci	struct ipu_image_convert_run *run;
155162306a36Sopenharmony_ci	unsigned long flags;
155262306a36Sopenharmony_ci
155362306a36Sopenharmony_ci	spin_lock_irqsave(&chan->irqlock, flags);
155462306a36Sopenharmony_ci
155562306a36Sopenharmony_ci	while (!list_empty(&chan->done_q)) {
155662306a36Sopenharmony_ci		run = list_entry(chan->done_q.next,
155762306a36Sopenharmony_ci				 struct ipu_image_convert_run,
155862306a36Sopenharmony_ci				 list);
155962306a36Sopenharmony_ci
156062306a36Sopenharmony_ci		list_del(&run->list);
156162306a36Sopenharmony_ci
156262306a36Sopenharmony_ci		dev_dbg(priv->ipu->dev,
156362306a36Sopenharmony_ci			"%s: task %u: completing ctx %p run %p with %d\n",
156462306a36Sopenharmony_ci			__func__, chan->ic_task, run->ctx, run, run->status);
156562306a36Sopenharmony_ci
156662306a36Sopenharmony_ci		/* call the completion callback and free the run */
156762306a36Sopenharmony_ci		spin_unlock_irqrestore(&chan->irqlock, flags);
156862306a36Sopenharmony_ci		run->ctx->complete(run, run->ctx->complete_context);
156962306a36Sopenharmony_ci		spin_lock_irqsave(&chan->irqlock, flags);
157062306a36Sopenharmony_ci	}
157162306a36Sopenharmony_ci
157262306a36Sopenharmony_ci	spin_unlock_irqrestore(&chan->irqlock, flags);
157362306a36Sopenharmony_ci}
157462306a36Sopenharmony_ci
157562306a36Sopenharmony_ci/*
157662306a36Sopenharmony_ci * the bottom half thread clears out the done_q, calling the
157762306a36Sopenharmony_ci * completion handler for each.
157862306a36Sopenharmony_ci */
157962306a36Sopenharmony_cistatic irqreturn_t do_bh(int irq, void *dev_id)
158062306a36Sopenharmony_ci{
158162306a36Sopenharmony_ci	struct ipu_image_convert_chan *chan = dev_id;
158262306a36Sopenharmony_ci	struct ipu_image_convert_priv *priv = chan->priv;
158362306a36Sopenharmony_ci	struct ipu_image_convert_ctx *ctx;
158462306a36Sopenharmony_ci	unsigned long flags;
158562306a36Sopenharmony_ci
158662306a36Sopenharmony_ci	dev_dbg(priv->ipu->dev, "%s: task %u: enter\n", __func__,
158762306a36Sopenharmony_ci		chan->ic_task);
158862306a36Sopenharmony_ci
158962306a36Sopenharmony_ci	empty_done_q(chan);
159062306a36Sopenharmony_ci
159162306a36Sopenharmony_ci	spin_lock_irqsave(&chan->irqlock, flags);
159262306a36Sopenharmony_ci
159362306a36Sopenharmony_ci	/*
159462306a36Sopenharmony_ci	 * the done_q is cleared out, signal any contexts
159562306a36Sopenharmony_ci	 * that are aborting that abort can complete.
159662306a36Sopenharmony_ci	 */
159762306a36Sopenharmony_ci	list_for_each_entry(ctx, &chan->ctx_list, list) {
159862306a36Sopenharmony_ci		if (ctx->aborting) {
159962306a36Sopenharmony_ci			dev_dbg(priv->ipu->dev,
160062306a36Sopenharmony_ci				"%s: task %u: signaling abort for ctx %p\n",
160162306a36Sopenharmony_ci				__func__, chan->ic_task, ctx);
160262306a36Sopenharmony_ci			complete_all(&ctx->aborted);
160362306a36Sopenharmony_ci		}
160462306a36Sopenharmony_ci	}
160562306a36Sopenharmony_ci
160662306a36Sopenharmony_ci	spin_unlock_irqrestore(&chan->irqlock, flags);
160762306a36Sopenharmony_ci
160862306a36Sopenharmony_ci	dev_dbg(priv->ipu->dev, "%s: task %u: exit\n", __func__,
160962306a36Sopenharmony_ci		chan->ic_task);
161062306a36Sopenharmony_ci
161162306a36Sopenharmony_ci	return IRQ_HANDLED;
161262306a36Sopenharmony_ci}
161362306a36Sopenharmony_ci
161462306a36Sopenharmony_cistatic bool ic_settings_changed(struct ipu_image_convert_ctx *ctx)
161562306a36Sopenharmony_ci{
161662306a36Sopenharmony_ci	unsigned int cur_tile = ctx->next_tile - 1;
161762306a36Sopenharmony_ci	unsigned int next_tile = ctx->next_tile;
161862306a36Sopenharmony_ci
161962306a36Sopenharmony_ci	if (ctx->resize_coeffs_h[cur_tile % ctx->in.num_cols] !=
162062306a36Sopenharmony_ci	    ctx->resize_coeffs_h[next_tile % ctx->in.num_cols] ||
162162306a36Sopenharmony_ci	    ctx->resize_coeffs_v[cur_tile / ctx->in.num_cols] !=
162262306a36Sopenharmony_ci	    ctx->resize_coeffs_v[next_tile / ctx->in.num_cols] ||
162362306a36Sopenharmony_ci	    ctx->in.tile[cur_tile].width != ctx->in.tile[next_tile].width ||
162462306a36Sopenharmony_ci	    ctx->in.tile[cur_tile].height != ctx->in.tile[next_tile].height ||
162562306a36Sopenharmony_ci	    ctx->out.tile[cur_tile].width != ctx->out.tile[next_tile].width ||
162662306a36Sopenharmony_ci	    ctx->out.tile[cur_tile].height != ctx->out.tile[next_tile].height)
162762306a36Sopenharmony_ci		return true;
162862306a36Sopenharmony_ci
162962306a36Sopenharmony_ci	return false;
163062306a36Sopenharmony_ci}
163162306a36Sopenharmony_ci
163262306a36Sopenharmony_ci/* hold irqlock when calling */
163362306a36Sopenharmony_cistatic irqreturn_t do_tile_complete(struct ipu_image_convert_run *run)
163462306a36Sopenharmony_ci{
163562306a36Sopenharmony_ci	struct ipu_image_convert_ctx *ctx = run->ctx;
163662306a36Sopenharmony_ci	struct ipu_image_convert_chan *chan = ctx->chan;
163762306a36Sopenharmony_ci	struct ipu_image_tile *src_tile, *dst_tile;
163862306a36Sopenharmony_ci	struct ipu_image_convert_image *s_image = &ctx->in;
163962306a36Sopenharmony_ci	struct ipu_image_convert_image *d_image = &ctx->out;
164062306a36Sopenharmony_ci	struct ipuv3_channel *outch;
164162306a36Sopenharmony_ci	unsigned int dst_idx;
164262306a36Sopenharmony_ci
164362306a36Sopenharmony_ci	lockdep_assert_held(&chan->irqlock);
164462306a36Sopenharmony_ci
164562306a36Sopenharmony_ci	outch = ipu_rot_mode_is_irt(ctx->rot_mode) ?
164662306a36Sopenharmony_ci		chan->rotation_out_chan : chan->out_chan;
164762306a36Sopenharmony_ci
164862306a36Sopenharmony_ci	/*
164962306a36Sopenharmony_ci	 * It is difficult to stop the channel DMA before the channels
165062306a36Sopenharmony_ci	 * enter the paused state. Without double-buffering the channels
165162306a36Sopenharmony_ci	 * are always in a paused state when the EOF irq occurs, so it
165262306a36Sopenharmony_ci	 * is safe to stop the channels now. For double-buffering we
165362306a36Sopenharmony_ci	 * just ignore the abort until the operation completes, when it
165462306a36Sopenharmony_ci	 * is safe to shut down.
165562306a36Sopenharmony_ci	 */
165662306a36Sopenharmony_ci	if (ctx->aborting && !ctx->double_buffering) {
165762306a36Sopenharmony_ci		convert_stop(run);
165862306a36Sopenharmony_ci		run->status = -EIO;
165962306a36Sopenharmony_ci		goto done;
166062306a36Sopenharmony_ci	}
166162306a36Sopenharmony_ci
166262306a36Sopenharmony_ci	if (ctx->next_tile == ctx->num_tiles) {
166362306a36Sopenharmony_ci		/*
166462306a36Sopenharmony_ci		 * the conversion is complete
166562306a36Sopenharmony_ci		 */
166662306a36Sopenharmony_ci		convert_stop(run);
166762306a36Sopenharmony_ci		run->status = 0;
166862306a36Sopenharmony_ci		goto done;
166962306a36Sopenharmony_ci	}
167062306a36Sopenharmony_ci
167162306a36Sopenharmony_ci	/*
167262306a36Sopenharmony_ci	 * not done, place the next tile buffers.
167362306a36Sopenharmony_ci	 */
167462306a36Sopenharmony_ci	if (!ctx->double_buffering) {
167562306a36Sopenharmony_ci		if (ic_settings_changed(ctx)) {
167662306a36Sopenharmony_ci			convert_stop(run);
167762306a36Sopenharmony_ci			convert_start(run, ctx->next_tile);
167862306a36Sopenharmony_ci		} else {
167962306a36Sopenharmony_ci			src_tile = &s_image->tile[ctx->next_tile];
168062306a36Sopenharmony_ci			dst_idx = ctx->out_tile_map[ctx->next_tile];
168162306a36Sopenharmony_ci			dst_tile = &d_image->tile[dst_idx];
168262306a36Sopenharmony_ci
168362306a36Sopenharmony_ci			ipu_cpmem_set_buffer(chan->in_chan, 0,
168462306a36Sopenharmony_ci					     s_image->base.phys0 +
168562306a36Sopenharmony_ci					     src_tile->offset);
168662306a36Sopenharmony_ci			ipu_cpmem_set_buffer(outch, 0,
168762306a36Sopenharmony_ci					     d_image->base.phys0 +
168862306a36Sopenharmony_ci					     dst_tile->offset);
168962306a36Sopenharmony_ci			if (s_image->fmt->planar)
169062306a36Sopenharmony_ci				ipu_cpmem_set_uv_offset(chan->in_chan,
169162306a36Sopenharmony_ci							src_tile->u_off,
169262306a36Sopenharmony_ci							src_tile->v_off);
169362306a36Sopenharmony_ci			if (d_image->fmt->planar)
169462306a36Sopenharmony_ci				ipu_cpmem_set_uv_offset(outch,
169562306a36Sopenharmony_ci							dst_tile->u_off,
169662306a36Sopenharmony_ci							dst_tile->v_off);
169762306a36Sopenharmony_ci
169862306a36Sopenharmony_ci			ipu_idmac_select_buffer(chan->in_chan, 0);
169962306a36Sopenharmony_ci			ipu_idmac_select_buffer(outch, 0);
170062306a36Sopenharmony_ci		}
170162306a36Sopenharmony_ci	} else if (ctx->next_tile < ctx->num_tiles - 1) {
170262306a36Sopenharmony_ci
170362306a36Sopenharmony_ci		src_tile = &s_image->tile[ctx->next_tile + 1];
170462306a36Sopenharmony_ci		dst_idx = ctx->out_tile_map[ctx->next_tile + 1];
170562306a36Sopenharmony_ci		dst_tile = &d_image->tile[dst_idx];
170662306a36Sopenharmony_ci
170762306a36Sopenharmony_ci		ipu_cpmem_set_buffer(chan->in_chan, ctx->cur_buf_num,
170862306a36Sopenharmony_ci				     s_image->base.phys0 + src_tile->offset);
170962306a36Sopenharmony_ci		ipu_cpmem_set_buffer(outch, ctx->cur_buf_num,
171062306a36Sopenharmony_ci				     d_image->base.phys0 + dst_tile->offset);
171162306a36Sopenharmony_ci
171262306a36Sopenharmony_ci		ipu_idmac_select_buffer(chan->in_chan, ctx->cur_buf_num);
171362306a36Sopenharmony_ci		ipu_idmac_select_buffer(outch, ctx->cur_buf_num);
171462306a36Sopenharmony_ci
171562306a36Sopenharmony_ci		ctx->cur_buf_num ^= 1;
171662306a36Sopenharmony_ci	}
171762306a36Sopenharmony_ci
171862306a36Sopenharmony_ci	ctx->eof_mask = 0; /* clear EOF irq mask for next tile */
171962306a36Sopenharmony_ci	ctx->next_tile++;
172062306a36Sopenharmony_ci	return IRQ_HANDLED;
172162306a36Sopenharmony_cidone:
172262306a36Sopenharmony_ci	list_add_tail(&run->list, &chan->done_q);
172362306a36Sopenharmony_ci	chan->current_run = NULL;
172462306a36Sopenharmony_ci	run_next(chan);
172562306a36Sopenharmony_ci	return IRQ_WAKE_THREAD;
172662306a36Sopenharmony_ci}
172762306a36Sopenharmony_ci
172862306a36Sopenharmony_cistatic irqreturn_t eof_irq(int irq, void *data)
172962306a36Sopenharmony_ci{
173062306a36Sopenharmony_ci	struct ipu_image_convert_chan *chan = data;
173162306a36Sopenharmony_ci	struct ipu_image_convert_priv *priv = chan->priv;
173262306a36Sopenharmony_ci	struct ipu_image_convert_ctx *ctx;
173362306a36Sopenharmony_ci	struct ipu_image_convert_run *run;
173462306a36Sopenharmony_ci	irqreturn_t ret = IRQ_HANDLED;
173562306a36Sopenharmony_ci	bool tile_complete = false;
173662306a36Sopenharmony_ci	unsigned long flags;
173762306a36Sopenharmony_ci
173862306a36Sopenharmony_ci	spin_lock_irqsave(&chan->irqlock, flags);
173962306a36Sopenharmony_ci
174062306a36Sopenharmony_ci	/* get current run and its context */
174162306a36Sopenharmony_ci	run = chan->current_run;
174262306a36Sopenharmony_ci	if (!run) {
174362306a36Sopenharmony_ci		ret = IRQ_NONE;
174462306a36Sopenharmony_ci		goto out;
174562306a36Sopenharmony_ci	}
174662306a36Sopenharmony_ci
174762306a36Sopenharmony_ci	ctx = run->ctx;
174862306a36Sopenharmony_ci
174962306a36Sopenharmony_ci	if (irq == chan->in_eof_irq) {
175062306a36Sopenharmony_ci		ctx->eof_mask |= EOF_IRQ_IN;
175162306a36Sopenharmony_ci	} else if (irq == chan->out_eof_irq) {
175262306a36Sopenharmony_ci		ctx->eof_mask |= EOF_IRQ_OUT;
175362306a36Sopenharmony_ci	} else if (irq == chan->rot_in_eof_irq ||
175462306a36Sopenharmony_ci		   irq == chan->rot_out_eof_irq) {
175562306a36Sopenharmony_ci		if (!ipu_rot_mode_is_irt(ctx->rot_mode)) {
175662306a36Sopenharmony_ci			/* this was NOT a rotation op, shouldn't happen */
175762306a36Sopenharmony_ci			dev_err(priv->ipu->dev,
175862306a36Sopenharmony_ci				"Unexpected rotation interrupt\n");
175962306a36Sopenharmony_ci			goto out;
176062306a36Sopenharmony_ci		}
176162306a36Sopenharmony_ci		ctx->eof_mask |= (irq == chan->rot_in_eof_irq) ?
176262306a36Sopenharmony_ci			EOF_IRQ_ROT_IN : EOF_IRQ_ROT_OUT;
176362306a36Sopenharmony_ci	} else {
176462306a36Sopenharmony_ci		dev_err(priv->ipu->dev, "Received unknown irq %d\n", irq);
176562306a36Sopenharmony_ci		ret = IRQ_NONE;
176662306a36Sopenharmony_ci		goto out;
176762306a36Sopenharmony_ci	}
176862306a36Sopenharmony_ci
176962306a36Sopenharmony_ci	if (ipu_rot_mode_is_irt(ctx->rot_mode))
177062306a36Sopenharmony_ci		tile_complete =	(ctx->eof_mask == EOF_IRQ_ROT_COMPLETE);
177162306a36Sopenharmony_ci	else
177262306a36Sopenharmony_ci		tile_complete = (ctx->eof_mask == EOF_IRQ_COMPLETE);
177362306a36Sopenharmony_ci
177462306a36Sopenharmony_ci	if (tile_complete)
177562306a36Sopenharmony_ci		ret = do_tile_complete(run);
177662306a36Sopenharmony_ciout:
177762306a36Sopenharmony_ci	spin_unlock_irqrestore(&chan->irqlock, flags);
177862306a36Sopenharmony_ci	return ret;
177962306a36Sopenharmony_ci}
178062306a36Sopenharmony_ci
178162306a36Sopenharmony_ci/*
178262306a36Sopenharmony_ci * try to force the completion of runs for this ctx. Called when
178362306a36Sopenharmony_ci * abort wait times out in ipu_image_convert_abort().
178462306a36Sopenharmony_ci */
178562306a36Sopenharmony_cistatic void force_abort(struct ipu_image_convert_ctx *ctx)
178662306a36Sopenharmony_ci{
178762306a36Sopenharmony_ci	struct ipu_image_convert_chan *chan = ctx->chan;
178862306a36Sopenharmony_ci	struct ipu_image_convert_run *run;
178962306a36Sopenharmony_ci	unsigned long flags;
179062306a36Sopenharmony_ci
179162306a36Sopenharmony_ci	spin_lock_irqsave(&chan->irqlock, flags);
179262306a36Sopenharmony_ci
179362306a36Sopenharmony_ci	run = chan->current_run;
179462306a36Sopenharmony_ci	if (run && run->ctx == ctx) {
179562306a36Sopenharmony_ci		convert_stop(run);
179662306a36Sopenharmony_ci		run->status = -EIO;
179762306a36Sopenharmony_ci		list_add_tail(&run->list, &chan->done_q);
179862306a36Sopenharmony_ci		chan->current_run = NULL;
179962306a36Sopenharmony_ci		run_next(chan);
180062306a36Sopenharmony_ci	}
180162306a36Sopenharmony_ci
180262306a36Sopenharmony_ci	spin_unlock_irqrestore(&chan->irqlock, flags);
180362306a36Sopenharmony_ci
180462306a36Sopenharmony_ci	empty_done_q(chan);
180562306a36Sopenharmony_ci}
180662306a36Sopenharmony_ci
180762306a36Sopenharmony_cistatic void release_ipu_resources(struct ipu_image_convert_chan *chan)
180862306a36Sopenharmony_ci{
180962306a36Sopenharmony_ci	if (chan->in_eof_irq >= 0)
181062306a36Sopenharmony_ci		free_irq(chan->in_eof_irq, chan);
181162306a36Sopenharmony_ci	if (chan->rot_in_eof_irq >= 0)
181262306a36Sopenharmony_ci		free_irq(chan->rot_in_eof_irq, chan);
181362306a36Sopenharmony_ci	if (chan->out_eof_irq >= 0)
181462306a36Sopenharmony_ci		free_irq(chan->out_eof_irq, chan);
181562306a36Sopenharmony_ci	if (chan->rot_out_eof_irq >= 0)
181662306a36Sopenharmony_ci		free_irq(chan->rot_out_eof_irq, chan);
181762306a36Sopenharmony_ci
181862306a36Sopenharmony_ci	if (!IS_ERR_OR_NULL(chan->in_chan))
181962306a36Sopenharmony_ci		ipu_idmac_put(chan->in_chan);
182062306a36Sopenharmony_ci	if (!IS_ERR_OR_NULL(chan->out_chan))
182162306a36Sopenharmony_ci		ipu_idmac_put(chan->out_chan);
182262306a36Sopenharmony_ci	if (!IS_ERR_OR_NULL(chan->rotation_in_chan))
182362306a36Sopenharmony_ci		ipu_idmac_put(chan->rotation_in_chan);
182462306a36Sopenharmony_ci	if (!IS_ERR_OR_NULL(chan->rotation_out_chan))
182562306a36Sopenharmony_ci		ipu_idmac_put(chan->rotation_out_chan);
182662306a36Sopenharmony_ci	if (!IS_ERR_OR_NULL(chan->ic))
182762306a36Sopenharmony_ci		ipu_ic_put(chan->ic);
182862306a36Sopenharmony_ci
182962306a36Sopenharmony_ci	chan->in_chan = chan->out_chan = chan->rotation_in_chan =
183062306a36Sopenharmony_ci		chan->rotation_out_chan = NULL;
183162306a36Sopenharmony_ci	chan->in_eof_irq = -1;
183262306a36Sopenharmony_ci	chan->rot_in_eof_irq = -1;
183362306a36Sopenharmony_ci	chan->out_eof_irq = -1;
183462306a36Sopenharmony_ci	chan->rot_out_eof_irq = -1;
183562306a36Sopenharmony_ci}
183662306a36Sopenharmony_ci
183762306a36Sopenharmony_cistatic int get_eof_irq(struct ipu_image_convert_chan *chan,
183862306a36Sopenharmony_ci		       struct ipuv3_channel *channel)
183962306a36Sopenharmony_ci{
184062306a36Sopenharmony_ci	struct ipu_image_convert_priv *priv = chan->priv;
184162306a36Sopenharmony_ci	int ret, irq;
184262306a36Sopenharmony_ci
184362306a36Sopenharmony_ci	irq = ipu_idmac_channel_irq(priv->ipu, channel, IPU_IRQ_EOF);
184462306a36Sopenharmony_ci
184562306a36Sopenharmony_ci	ret = request_threaded_irq(irq, eof_irq, do_bh, 0, "ipu-ic", chan);
184662306a36Sopenharmony_ci	if (ret < 0) {
184762306a36Sopenharmony_ci		dev_err(priv->ipu->dev, "could not acquire irq %d\n", irq);
184862306a36Sopenharmony_ci		return ret;
184962306a36Sopenharmony_ci	}
185062306a36Sopenharmony_ci
185162306a36Sopenharmony_ci	return irq;
185262306a36Sopenharmony_ci}
185362306a36Sopenharmony_ci
185462306a36Sopenharmony_cistatic int get_ipu_resources(struct ipu_image_convert_chan *chan)
185562306a36Sopenharmony_ci{
185662306a36Sopenharmony_ci	const struct ipu_image_convert_dma_chan *dma = chan->dma_ch;
185762306a36Sopenharmony_ci	struct ipu_image_convert_priv *priv = chan->priv;
185862306a36Sopenharmony_ci	int ret;
185962306a36Sopenharmony_ci
186062306a36Sopenharmony_ci	/* get IC */
186162306a36Sopenharmony_ci	chan->ic = ipu_ic_get(priv->ipu, chan->ic_task);
186262306a36Sopenharmony_ci	if (IS_ERR(chan->ic)) {
186362306a36Sopenharmony_ci		dev_err(priv->ipu->dev, "could not acquire IC\n");
186462306a36Sopenharmony_ci		ret = PTR_ERR(chan->ic);
186562306a36Sopenharmony_ci		goto err;
186662306a36Sopenharmony_ci	}
186762306a36Sopenharmony_ci
186862306a36Sopenharmony_ci	/* get IDMAC channels */
186962306a36Sopenharmony_ci	chan->in_chan = ipu_idmac_get(priv->ipu, dma->in);
187062306a36Sopenharmony_ci	chan->out_chan = ipu_idmac_get(priv->ipu, dma->out);
187162306a36Sopenharmony_ci	if (IS_ERR(chan->in_chan) || IS_ERR(chan->out_chan)) {
187262306a36Sopenharmony_ci		dev_err(priv->ipu->dev, "could not acquire idmac channels\n");
187362306a36Sopenharmony_ci		ret = -EBUSY;
187462306a36Sopenharmony_ci		goto err;
187562306a36Sopenharmony_ci	}
187662306a36Sopenharmony_ci
187762306a36Sopenharmony_ci	chan->rotation_in_chan = ipu_idmac_get(priv->ipu, dma->rot_in);
187862306a36Sopenharmony_ci	chan->rotation_out_chan = ipu_idmac_get(priv->ipu, dma->rot_out);
187962306a36Sopenharmony_ci	if (IS_ERR(chan->rotation_in_chan) || IS_ERR(chan->rotation_out_chan)) {
188062306a36Sopenharmony_ci		dev_err(priv->ipu->dev,
188162306a36Sopenharmony_ci			"could not acquire idmac rotation channels\n");
188262306a36Sopenharmony_ci		ret = -EBUSY;
188362306a36Sopenharmony_ci		goto err;
188462306a36Sopenharmony_ci	}
188562306a36Sopenharmony_ci
188662306a36Sopenharmony_ci	/* acquire the EOF interrupts */
188762306a36Sopenharmony_ci	ret = get_eof_irq(chan, chan->in_chan);
188862306a36Sopenharmony_ci	if (ret < 0) {
188962306a36Sopenharmony_ci		chan->in_eof_irq = -1;
189062306a36Sopenharmony_ci		goto err;
189162306a36Sopenharmony_ci	}
189262306a36Sopenharmony_ci	chan->in_eof_irq = ret;
189362306a36Sopenharmony_ci
189462306a36Sopenharmony_ci	ret = get_eof_irq(chan, chan->rotation_in_chan);
189562306a36Sopenharmony_ci	if (ret < 0) {
189662306a36Sopenharmony_ci		chan->rot_in_eof_irq = -1;
189762306a36Sopenharmony_ci		goto err;
189862306a36Sopenharmony_ci	}
189962306a36Sopenharmony_ci	chan->rot_in_eof_irq = ret;
190062306a36Sopenharmony_ci
190162306a36Sopenharmony_ci	ret = get_eof_irq(chan, chan->out_chan);
190262306a36Sopenharmony_ci	if (ret < 0) {
190362306a36Sopenharmony_ci		chan->out_eof_irq = -1;
190462306a36Sopenharmony_ci		goto err;
190562306a36Sopenharmony_ci	}
190662306a36Sopenharmony_ci	chan->out_eof_irq = ret;
190762306a36Sopenharmony_ci
190862306a36Sopenharmony_ci	ret = get_eof_irq(chan, chan->rotation_out_chan);
190962306a36Sopenharmony_ci	if (ret < 0) {
191062306a36Sopenharmony_ci		chan->rot_out_eof_irq = -1;
191162306a36Sopenharmony_ci		goto err;
191262306a36Sopenharmony_ci	}
191362306a36Sopenharmony_ci	chan->rot_out_eof_irq = ret;
191462306a36Sopenharmony_ci
191562306a36Sopenharmony_ci	return 0;
191662306a36Sopenharmony_cierr:
191762306a36Sopenharmony_ci	release_ipu_resources(chan);
191862306a36Sopenharmony_ci	return ret;
191962306a36Sopenharmony_ci}
192062306a36Sopenharmony_ci
192162306a36Sopenharmony_cistatic int fill_image(struct ipu_image_convert_ctx *ctx,
192262306a36Sopenharmony_ci		      struct ipu_image_convert_image *ic_image,
192362306a36Sopenharmony_ci		      struct ipu_image *image,
192462306a36Sopenharmony_ci		      enum ipu_image_convert_type type)
192562306a36Sopenharmony_ci{
192662306a36Sopenharmony_ci	struct ipu_image_convert_priv *priv = ctx->chan->priv;
192762306a36Sopenharmony_ci
192862306a36Sopenharmony_ci	ic_image->base = *image;
192962306a36Sopenharmony_ci	ic_image->type = type;
193062306a36Sopenharmony_ci
193162306a36Sopenharmony_ci	ic_image->fmt = get_format(image->pix.pixelformat);
193262306a36Sopenharmony_ci	if (!ic_image->fmt) {
193362306a36Sopenharmony_ci		dev_err(priv->ipu->dev, "pixelformat not supported for %s\n",
193462306a36Sopenharmony_ci			type == IMAGE_CONVERT_OUT ? "Output" : "Input");
193562306a36Sopenharmony_ci		return -EINVAL;
193662306a36Sopenharmony_ci	}
193762306a36Sopenharmony_ci
193862306a36Sopenharmony_ci	if (ic_image->fmt->planar)
193962306a36Sopenharmony_ci		ic_image->stride = ic_image->base.pix.width;
194062306a36Sopenharmony_ci	else
194162306a36Sopenharmony_ci		ic_image->stride  = ic_image->base.pix.bytesperline;
194262306a36Sopenharmony_ci
194362306a36Sopenharmony_ci	return 0;
194462306a36Sopenharmony_ci}
194562306a36Sopenharmony_ci
194662306a36Sopenharmony_ci/* borrowed from drivers/media/v4l2-core/v4l2-common.c */
194762306a36Sopenharmony_cistatic unsigned int clamp_align(unsigned int x, unsigned int min,
194862306a36Sopenharmony_ci				unsigned int max, unsigned int align)
194962306a36Sopenharmony_ci{
195062306a36Sopenharmony_ci	/* Bits that must be zero to be aligned */
195162306a36Sopenharmony_ci	unsigned int mask = ~((1 << align) - 1);
195262306a36Sopenharmony_ci
195362306a36Sopenharmony_ci	/* Clamp to aligned min and max */
195462306a36Sopenharmony_ci	x = clamp(x, (min + ~mask) & mask, max & mask);
195562306a36Sopenharmony_ci
195662306a36Sopenharmony_ci	/* Round to nearest aligned value */
195762306a36Sopenharmony_ci	if (align)
195862306a36Sopenharmony_ci		x = (x + (1 << (align - 1))) & mask;
195962306a36Sopenharmony_ci
196062306a36Sopenharmony_ci	return x;
196162306a36Sopenharmony_ci}
196262306a36Sopenharmony_ci
196362306a36Sopenharmony_ci/* Adjusts input/output images to IPU restrictions */
196462306a36Sopenharmony_civoid ipu_image_convert_adjust(struct ipu_image *in, struct ipu_image *out,
196562306a36Sopenharmony_ci			      enum ipu_rotate_mode rot_mode)
196662306a36Sopenharmony_ci{
196762306a36Sopenharmony_ci	const struct ipu_image_pixfmt *infmt, *outfmt;
196862306a36Sopenharmony_ci	u32 w_align_out, h_align_out;
196962306a36Sopenharmony_ci	u32 w_align_in, h_align_in;
197062306a36Sopenharmony_ci
197162306a36Sopenharmony_ci	infmt = get_format(in->pix.pixelformat);
197262306a36Sopenharmony_ci	outfmt = get_format(out->pix.pixelformat);
197362306a36Sopenharmony_ci
197462306a36Sopenharmony_ci	/* set some default pixel formats if needed */
197562306a36Sopenharmony_ci	if (!infmt) {
197662306a36Sopenharmony_ci		in->pix.pixelformat = V4L2_PIX_FMT_RGB24;
197762306a36Sopenharmony_ci		infmt = get_format(V4L2_PIX_FMT_RGB24);
197862306a36Sopenharmony_ci	}
197962306a36Sopenharmony_ci	if (!outfmt) {
198062306a36Sopenharmony_ci		out->pix.pixelformat = V4L2_PIX_FMT_RGB24;
198162306a36Sopenharmony_ci		outfmt = get_format(V4L2_PIX_FMT_RGB24);
198262306a36Sopenharmony_ci	}
198362306a36Sopenharmony_ci
198462306a36Sopenharmony_ci	/* image converter does not handle fields */
198562306a36Sopenharmony_ci	in->pix.field = out->pix.field = V4L2_FIELD_NONE;
198662306a36Sopenharmony_ci
198762306a36Sopenharmony_ci	/* resizer cannot downsize more than 4:1 */
198862306a36Sopenharmony_ci	if (ipu_rot_mode_is_irt(rot_mode)) {
198962306a36Sopenharmony_ci		out->pix.height = max_t(__u32, out->pix.height,
199062306a36Sopenharmony_ci					in->pix.width / 4);
199162306a36Sopenharmony_ci		out->pix.width = max_t(__u32, out->pix.width,
199262306a36Sopenharmony_ci				       in->pix.height / 4);
199362306a36Sopenharmony_ci	} else {
199462306a36Sopenharmony_ci		out->pix.width = max_t(__u32, out->pix.width,
199562306a36Sopenharmony_ci				       in->pix.width / 4);
199662306a36Sopenharmony_ci		out->pix.height = max_t(__u32, out->pix.height,
199762306a36Sopenharmony_ci					in->pix.height / 4);
199862306a36Sopenharmony_ci	}
199962306a36Sopenharmony_ci
200062306a36Sopenharmony_ci	/* align input width/height */
200162306a36Sopenharmony_ci	w_align_in = ilog2(tile_width_align(IMAGE_CONVERT_IN, infmt,
200262306a36Sopenharmony_ci					    rot_mode));
200362306a36Sopenharmony_ci	h_align_in = ilog2(tile_height_align(IMAGE_CONVERT_IN, infmt,
200462306a36Sopenharmony_ci					     rot_mode));
200562306a36Sopenharmony_ci	in->pix.width = clamp_align(in->pix.width, MIN_W, MAX_W,
200662306a36Sopenharmony_ci				    w_align_in);
200762306a36Sopenharmony_ci	in->pix.height = clamp_align(in->pix.height, MIN_H, MAX_H,
200862306a36Sopenharmony_ci				     h_align_in);
200962306a36Sopenharmony_ci
201062306a36Sopenharmony_ci	/* align output width/height */
201162306a36Sopenharmony_ci	w_align_out = ilog2(tile_width_align(IMAGE_CONVERT_OUT, outfmt,
201262306a36Sopenharmony_ci					     rot_mode));
201362306a36Sopenharmony_ci	h_align_out = ilog2(tile_height_align(IMAGE_CONVERT_OUT, outfmt,
201462306a36Sopenharmony_ci					      rot_mode));
201562306a36Sopenharmony_ci	out->pix.width = clamp_align(out->pix.width, MIN_W, MAX_W,
201662306a36Sopenharmony_ci				     w_align_out);
201762306a36Sopenharmony_ci	out->pix.height = clamp_align(out->pix.height, MIN_H, MAX_H,
201862306a36Sopenharmony_ci				      h_align_out);
201962306a36Sopenharmony_ci
202062306a36Sopenharmony_ci	/* set input/output strides and image sizes */
202162306a36Sopenharmony_ci	in->pix.bytesperline = infmt->planar ?
202262306a36Sopenharmony_ci		clamp_align(in->pix.width, 2 << w_align_in, MAX_W,
202362306a36Sopenharmony_ci			    w_align_in) :
202462306a36Sopenharmony_ci		clamp_align((in->pix.width * infmt->bpp) >> 3,
202562306a36Sopenharmony_ci			    ((2 << w_align_in) * infmt->bpp) >> 3,
202662306a36Sopenharmony_ci			    (MAX_W * infmt->bpp) >> 3,
202762306a36Sopenharmony_ci			    w_align_in);
202862306a36Sopenharmony_ci	in->pix.sizeimage = infmt->planar ?
202962306a36Sopenharmony_ci		(in->pix.height * in->pix.bytesperline * infmt->bpp) >> 3 :
203062306a36Sopenharmony_ci		in->pix.height * in->pix.bytesperline;
203162306a36Sopenharmony_ci	out->pix.bytesperline = outfmt->planar ? out->pix.width :
203262306a36Sopenharmony_ci		(out->pix.width * outfmt->bpp) >> 3;
203362306a36Sopenharmony_ci	out->pix.sizeimage = outfmt->planar ?
203462306a36Sopenharmony_ci		(out->pix.height * out->pix.bytesperline * outfmt->bpp) >> 3 :
203562306a36Sopenharmony_ci		out->pix.height * out->pix.bytesperline;
203662306a36Sopenharmony_ci}
203762306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(ipu_image_convert_adjust);
203862306a36Sopenharmony_ci
203962306a36Sopenharmony_ci/*
204062306a36Sopenharmony_ci * this is used by ipu_image_convert_prepare() to verify set input and
204162306a36Sopenharmony_ci * output images are valid before starting the conversion. Clients can
204262306a36Sopenharmony_ci * also call it before calling ipu_image_convert_prepare().
204362306a36Sopenharmony_ci */
204462306a36Sopenharmony_ciint ipu_image_convert_verify(struct ipu_image *in, struct ipu_image *out,
204562306a36Sopenharmony_ci			     enum ipu_rotate_mode rot_mode)
204662306a36Sopenharmony_ci{
204762306a36Sopenharmony_ci	struct ipu_image testin, testout;
204862306a36Sopenharmony_ci
204962306a36Sopenharmony_ci	testin = *in;
205062306a36Sopenharmony_ci	testout = *out;
205162306a36Sopenharmony_ci
205262306a36Sopenharmony_ci	ipu_image_convert_adjust(&testin, &testout, rot_mode);
205362306a36Sopenharmony_ci
205462306a36Sopenharmony_ci	if (testin.pix.width != in->pix.width ||
205562306a36Sopenharmony_ci	    testin.pix.height != in->pix.height ||
205662306a36Sopenharmony_ci	    testout.pix.width != out->pix.width ||
205762306a36Sopenharmony_ci	    testout.pix.height != out->pix.height)
205862306a36Sopenharmony_ci		return -EINVAL;
205962306a36Sopenharmony_ci
206062306a36Sopenharmony_ci	return 0;
206162306a36Sopenharmony_ci}
206262306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(ipu_image_convert_verify);
206362306a36Sopenharmony_ci
206462306a36Sopenharmony_ci/*
206562306a36Sopenharmony_ci * Call ipu_image_convert_prepare() to prepare for the conversion of
206662306a36Sopenharmony_ci * given images and rotation mode. Returns a new conversion context.
206762306a36Sopenharmony_ci */
206862306a36Sopenharmony_cistruct ipu_image_convert_ctx *
206962306a36Sopenharmony_ciipu_image_convert_prepare(struct ipu_soc *ipu, enum ipu_ic_task ic_task,
207062306a36Sopenharmony_ci			  struct ipu_image *in, struct ipu_image *out,
207162306a36Sopenharmony_ci			  enum ipu_rotate_mode rot_mode,
207262306a36Sopenharmony_ci			  ipu_image_convert_cb_t complete,
207362306a36Sopenharmony_ci			  void *complete_context)
207462306a36Sopenharmony_ci{
207562306a36Sopenharmony_ci	struct ipu_image_convert_priv *priv = ipu->image_convert_priv;
207662306a36Sopenharmony_ci	struct ipu_image_convert_image *s_image, *d_image;
207762306a36Sopenharmony_ci	struct ipu_image_convert_chan *chan;
207862306a36Sopenharmony_ci	struct ipu_image_convert_ctx *ctx;
207962306a36Sopenharmony_ci	unsigned long flags;
208062306a36Sopenharmony_ci	unsigned int i;
208162306a36Sopenharmony_ci	bool get_res;
208262306a36Sopenharmony_ci	int ret;
208362306a36Sopenharmony_ci
208462306a36Sopenharmony_ci	if (!in || !out || !complete ||
208562306a36Sopenharmony_ci	    (ic_task != IC_TASK_VIEWFINDER &&
208662306a36Sopenharmony_ci	     ic_task != IC_TASK_POST_PROCESSOR))
208762306a36Sopenharmony_ci		return ERR_PTR(-EINVAL);
208862306a36Sopenharmony_ci
208962306a36Sopenharmony_ci	/* verify the in/out images before continuing */
209062306a36Sopenharmony_ci	ret = ipu_image_convert_verify(in, out, rot_mode);
209162306a36Sopenharmony_ci	if (ret) {
209262306a36Sopenharmony_ci		dev_err(priv->ipu->dev, "%s: in/out formats invalid\n",
209362306a36Sopenharmony_ci			__func__);
209462306a36Sopenharmony_ci		return ERR_PTR(ret);
209562306a36Sopenharmony_ci	}
209662306a36Sopenharmony_ci
209762306a36Sopenharmony_ci	chan = &priv->chan[ic_task];
209862306a36Sopenharmony_ci
209962306a36Sopenharmony_ci	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
210062306a36Sopenharmony_ci	if (!ctx)
210162306a36Sopenharmony_ci		return ERR_PTR(-ENOMEM);
210262306a36Sopenharmony_ci
210362306a36Sopenharmony_ci	dev_dbg(priv->ipu->dev, "%s: task %u: ctx %p\n", __func__,
210462306a36Sopenharmony_ci		chan->ic_task, ctx);
210562306a36Sopenharmony_ci
210662306a36Sopenharmony_ci	ctx->chan = chan;
210762306a36Sopenharmony_ci	init_completion(&ctx->aborted);
210862306a36Sopenharmony_ci
210962306a36Sopenharmony_ci	ctx->rot_mode = rot_mode;
211062306a36Sopenharmony_ci
211162306a36Sopenharmony_ci	/* Sets ctx->in.num_rows/cols as well */
211262306a36Sopenharmony_ci	ret = calc_image_resize_coefficients(ctx, in, out);
211362306a36Sopenharmony_ci	if (ret)
211462306a36Sopenharmony_ci		goto out_free;
211562306a36Sopenharmony_ci
211662306a36Sopenharmony_ci	s_image = &ctx->in;
211762306a36Sopenharmony_ci	d_image = &ctx->out;
211862306a36Sopenharmony_ci
211962306a36Sopenharmony_ci	/* set tiling and rotation */
212062306a36Sopenharmony_ci	if (ipu_rot_mode_is_irt(rot_mode)) {
212162306a36Sopenharmony_ci		d_image->num_rows = s_image->num_cols;
212262306a36Sopenharmony_ci		d_image->num_cols = s_image->num_rows;
212362306a36Sopenharmony_ci	} else {
212462306a36Sopenharmony_ci		d_image->num_rows = s_image->num_rows;
212562306a36Sopenharmony_ci		d_image->num_cols = s_image->num_cols;
212662306a36Sopenharmony_ci	}
212762306a36Sopenharmony_ci
212862306a36Sopenharmony_ci	ctx->num_tiles = d_image->num_cols * d_image->num_rows;
212962306a36Sopenharmony_ci
213062306a36Sopenharmony_ci	ret = fill_image(ctx, s_image, in, IMAGE_CONVERT_IN);
213162306a36Sopenharmony_ci	if (ret)
213262306a36Sopenharmony_ci		goto out_free;
213362306a36Sopenharmony_ci	ret = fill_image(ctx, d_image, out, IMAGE_CONVERT_OUT);
213462306a36Sopenharmony_ci	if (ret)
213562306a36Sopenharmony_ci		goto out_free;
213662306a36Sopenharmony_ci
213762306a36Sopenharmony_ci	calc_out_tile_map(ctx);
213862306a36Sopenharmony_ci
213962306a36Sopenharmony_ci	find_seams(ctx, s_image, d_image);
214062306a36Sopenharmony_ci
214162306a36Sopenharmony_ci	ret = calc_tile_dimensions(ctx, s_image);
214262306a36Sopenharmony_ci	if (ret)
214362306a36Sopenharmony_ci		goto out_free;
214462306a36Sopenharmony_ci
214562306a36Sopenharmony_ci	ret = calc_tile_offsets(ctx, s_image);
214662306a36Sopenharmony_ci	if (ret)
214762306a36Sopenharmony_ci		goto out_free;
214862306a36Sopenharmony_ci
214962306a36Sopenharmony_ci	calc_tile_dimensions(ctx, d_image);
215062306a36Sopenharmony_ci	ret = calc_tile_offsets(ctx, d_image);
215162306a36Sopenharmony_ci	if (ret)
215262306a36Sopenharmony_ci		goto out_free;
215362306a36Sopenharmony_ci
215462306a36Sopenharmony_ci	calc_tile_resize_coefficients(ctx);
215562306a36Sopenharmony_ci
215662306a36Sopenharmony_ci	ret = ipu_ic_calc_csc(&ctx->csc,
215762306a36Sopenharmony_ci			s_image->base.pix.ycbcr_enc,
215862306a36Sopenharmony_ci			s_image->base.pix.quantization,
215962306a36Sopenharmony_ci			ipu_pixelformat_to_colorspace(s_image->fmt->fourcc),
216062306a36Sopenharmony_ci			d_image->base.pix.ycbcr_enc,
216162306a36Sopenharmony_ci			d_image->base.pix.quantization,
216262306a36Sopenharmony_ci			ipu_pixelformat_to_colorspace(d_image->fmt->fourcc));
216362306a36Sopenharmony_ci	if (ret)
216462306a36Sopenharmony_ci		goto out_free;
216562306a36Sopenharmony_ci
216662306a36Sopenharmony_ci	dump_format(ctx, s_image);
216762306a36Sopenharmony_ci	dump_format(ctx, d_image);
216862306a36Sopenharmony_ci
216962306a36Sopenharmony_ci	ctx->complete = complete;
217062306a36Sopenharmony_ci	ctx->complete_context = complete_context;
217162306a36Sopenharmony_ci
217262306a36Sopenharmony_ci	/*
217362306a36Sopenharmony_ci	 * Can we use double-buffering for this operation? If there is
217462306a36Sopenharmony_ci	 * only one tile (the whole image can be converted in a single
217562306a36Sopenharmony_ci	 * operation) there's no point in using double-buffering. Also,
217662306a36Sopenharmony_ci	 * the IPU's IDMAC channels allow only a single U and V plane
217762306a36Sopenharmony_ci	 * offset shared between both buffers, but these offsets change
217862306a36Sopenharmony_ci	 * for every tile, and therefore would have to be updated for
217962306a36Sopenharmony_ci	 * each buffer which is not possible. So double-buffering is
218062306a36Sopenharmony_ci	 * impossible when either the source or destination images are
218162306a36Sopenharmony_ci	 * a planar format (YUV420, YUV422P, etc.). Further, differently
218262306a36Sopenharmony_ci	 * sized tiles or different resizing coefficients per tile
218362306a36Sopenharmony_ci	 * prevent double-buffering as well.
218462306a36Sopenharmony_ci	 */
218562306a36Sopenharmony_ci	ctx->double_buffering = (ctx->num_tiles > 1 &&
218662306a36Sopenharmony_ci				 !s_image->fmt->planar &&
218762306a36Sopenharmony_ci				 !d_image->fmt->planar);
218862306a36Sopenharmony_ci	for (i = 1; i < ctx->num_tiles; i++) {
218962306a36Sopenharmony_ci		if (ctx->in.tile[i].width != ctx->in.tile[0].width ||
219062306a36Sopenharmony_ci		    ctx->in.tile[i].height != ctx->in.tile[0].height ||
219162306a36Sopenharmony_ci		    ctx->out.tile[i].width != ctx->out.tile[0].width ||
219262306a36Sopenharmony_ci		    ctx->out.tile[i].height != ctx->out.tile[0].height) {
219362306a36Sopenharmony_ci			ctx->double_buffering = false;
219462306a36Sopenharmony_ci			break;
219562306a36Sopenharmony_ci		}
219662306a36Sopenharmony_ci	}
219762306a36Sopenharmony_ci	for (i = 1; i < ctx->in.num_cols; i++) {
219862306a36Sopenharmony_ci		if (ctx->resize_coeffs_h[i] != ctx->resize_coeffs_h[0]) {
219962306a36Sopenharmony_ci			ctx->double_buffering = false;
220062306a36Sopenharmony_ci			break;
220162306a36Sopenharmony_ci		}
220262306a36Sopenharmony_ci	}
220362306a36Sopenharmony_ci	for (i = 1; i < ctx->in.num_rows; i++) {
220462306a36Sopenharmony_ci		if (ctx->resize_coeffs_v[i] != ctx->resize_coeffs_v[0]) {
220562306a36Sopenharmony_ci			ctx->double_buffering = false;
220662306a36Sopenharmony_ci			break;
220762306a36Sopenharmony_ci		}
220862306a36Sopenharmony_ci	}
220962306a36Sopenharmony_ci
221062306a36Sopenharmony_ci	if (ipu_rot_mode_is_irt(ctx->rot_mode)) {
221162306a36Sopenharmony_ci		unsigned long intermediate_size = d_image->tile[0].size;
221262306a36Sopenharmony_ci
221362306a36Sopenharmony_ci		for (i = 1; i < ctx->num_tiles; i++) {
221462306a36Sopenharmony_ci			if (d_image->tile[i].size > intermediate_size)
221562306a36Sopenharmony_ci				intermediate_size = d_image->tile[i].size;
221662306a36Sopenharmony_ci		}
221762306a36Sopenharmony_ci
221862306a36Sopenharmony_ci		ret = alloc_dma_buf(priv, &ctx->rot_intermediate[0],
221962306a36Sopenharmony_ci				    intermediate_size);
222062306a36Sopenharmony_ci		if (ret)
222162306a36Sopenharmony_ci			goto out_free;
222262306a36Sopenharmony_ci		if (ctx->double_buffering) {
222362306a36Sopenharmony_ci			ret = alloc_dma_buf(priv,
222462306a36Sopenharmony_ci					    &ctx->rot_intermediate[1],
222562306a36Sopenharmony_ci					    intermediate_size);
222662306a36Sopenharmony_ci			if (ret)
222762306a36Sopenharmony_ci				goto out_free_dmabuf0;
222862306a36Sopenharmony_ci		}
222962306a36Sopenharmony_ci	}
223062306a36Sopenharmony_ci
223162306a36Sopenharmony_ci	spin_lock_irqsave(&chan->irqlock, flags);
223262306a36Sopenharmony_ci
223362306a36Sopenharmony_ci	get_res = list_empty(&chan->ctx_list);
223462306a36Sopenharmony_ci
223562306a36Sopenharmony_ci	list_add_tail(&ctx->list, &chan->ctx_list);
223662306a36Sopenharmony_ci
223762306a36Sopenharmony_ci	spin_unlock_irqrestore(&chan->irqlock, flags);
223862306a36Sopenharmony_ci
223962306a36Sopenharmony_ci	if (get_res) {
224062306a36Sopenharmony_ci		ret = get_ipu_resources(chan);
224162306a36Sopenharmony_ci		if (ret)
224262306a36Sopenharmony_ci			goto out_free_dmabuf1;
224362306a36Sopenharmony_ci	}
224462306a36Sopenharmony_ci
224562306a36Sopenharmony_ci	return ctx;
224662306a36Sopenharmony_ci
224762306a36Sopenharmony_ciout_free_dmabuf1:
224862306a36Sopenharmony_ci	free_dma_buf(priv, &ctx->rot_intermediate[1]);
224962306a36Sopenharmony_ci	spin_lock_irqsave(&chan->irqlock, flags);
225062306a36Sopenharmony_ci	list_del(&ctx->list);
225162306a36Sopenharmony_ci	spin_unlock_irqrestore(&chan->irqlock, flags);
225262306a36Sopenharmony_ciout_free_dmabuf0:
225362306a36Sopenharmony_ci	free_dma_buf(priv, &ctx->rot_intermediate[0]);
225462306a36Sopenharmony_ciout_free:
225562306a36Sopenharmony_ci	kfree(ctx);
225662306a36Sopenharmony_ci	return ERR_PTR(ret);
225762306a36Sopenharmony_ci}
225862306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(ipu_image_convert_prepare);
225962306a36Sopenharmony_ci
226062306a36Sopenharmony_ci/*
226162306a36Sopenharmony_ci * Carry out a single image conversion run. Only the physaddr's of the input
226262306a36Sopenharmony_ci * and output image buffers are needed. The conversion context must have
226362306a36Sopenharmony_ci * been created previously with ipu_image_convert_prepare().
226462306a36Sopenharmony_ci */
226562306a36Sopenharmony_ciint ipu_image_convert_queue(struct ipu_image_convert_run *run)
226662306a36Sopenharmony_ci{
226762306a36Sopenharmony_ci	struct ipu_image_convert_chan *chan;
226862306a36Sopenharmony_ci	struct ipu_image_convert_priv *priv;
226962306a36Sopenharmony_ci	struct ipu_image_convert_ctx *ctx;
227062306a36Sopenharmony_ci	unsigned long flags;
227162306a36Sopenharmony_ci	int ret = 0;
227262306a36Sopenharmony_ci
227362306a36Sopenharmony_ci	if (!run || !run->ctx || !run->in_phys || !run->out_phys)
227462306a36Sopenharmony_ci		return -EINVAL;
227562306a36Sopenharmony_ci
227662306a36Sopenharmony_ci	ctx = run->ctx;
227762306a36Sopenharmony_ci	chan = ctx->chan;
227862306a36Sopenharmony_ci	priv = chan->priv;
227962306a36Sopenharmony_ci
228062306a36Sopenharmony_ci	dev_dbg(priv->ipu->dev, "%s: task %u: ctx %p run %p\n", __func__,
228162306a36Sopenharmony_ci		chan->ic_task, ctx, run);
228262306a36Sopenharmony_ci
228362306a36Sopenharmony_ci	INIT_LIST_HEAD(&run->list);
228462306a36Sopenharmony_ci
228562306a36Sopenharmony_ci	spin_lock_irqsave(&chan->irqlock, flags);
228662306a36Sopenharmony_ci
228762306a36Sopenharmony_ci	if (ctx->aborting) {
228862306a36Sopenharmony_ci		ret = -EIO;
228962306a36Sopenharmony_ci		goto unlock;
229062306a36Sopenharmony_ci	}
229162306a36Sopenharmony_ci
229262306a36Sopenharmony_ci	list_add_tail(&run->list, &chan->pending_q);
229362306a36Sopenharmony_ci
229462306a36Sopenharmony_ci	if (!chan->current_run) {
229562306a36Sopenharmony_ci		ret = do_run(run);
229662306a36Sopenharmony_ci		if (ret)
229762306a36Sopenharmony_ci			chan->current_run = NULL;
229862306a36Sopenharmony_ci	}
229962306a36Sopenharmony_ciunlock:
230062306a36Sopenharmony_ci	spin_unlock_irqrestore(&chan->irqlock, flags);
230162306a36Sopenharmony_ci	return ret;
230262306a36Sopenharmony_ci}
230362306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(ipu_image_convert_queue);
230462306a36Sopenharmony_ci
230562306a36Sopenharmony_ci/* Abort any active or pending conversions for this context */
230662306a36Sopenharmony_cistatic void __ipu_image_convert_abort(struct ipu_image_convert_ctx *ctx)
230762306a36Sopenharmony_ci{
230862306a36Sopenharmony_ci	struct ipu_image_convert_chan *chan = ctx->chan;
230962306a36Sopenharmony_ci	struct ipu_image_convert_priv *priv = chan->priv;
231062306a36Sopenharmony_ci	struct ipu_image_convert_run *run, *active_run, *tmp;
231162306a36Sopenharmony_ci	unsigned long flags;
231262306a36Sopenharmony_ci	int run_count, ret;
231362306a36Sopenharmony_ci
231462306a36Sopenharmony_ci	spin_lock_irqsave(&chan->irqlock, flags);
231562306a36Sopenharmony_ci
231662306a36Sopenharmony_ci	/* move all remaining pending runs in this context to done_q */
231762306a36Sopenharmony_ci	list_for_each_entry_safe(run, tmp, &chan->pending_q, list) {
231862306a36Sopenharmony_ci		if (run->ctx != ctx)
231962306a36Sopenharmony_ci			continue;
232062306a36Sopenharmony_ci		run->status = -EIO;
232162306a36Sopenharmony_ci		list_move_tail(&run->list, &chan->done_q);
232262306a36Sopenharmony_ci	}
232362306a36Sopenharmony_ci
232462306a36Sopenharmony_ci	run_count = get_run_count(ctx, &chan->done_q);
232562306a36Sopenharmony_ci	active_run = (chan->current_run && chan->current_run->ctx == ctx) ?
232662306a36Sopenharmony_ci		chan->current_run : NULL;
232762306a36Sopenharmony_ci
232862306a36Sopenharmony_ci	if (active_run)
232962306a36Sopenharmony_ci		reinit_completion(&ctx->aborted);
233062306a36Sopenharmony_ci
233162306a36Sopenharmony_ci	ctx->aborting = true;
233262306a36Sopenharmony_ci
233362306a36Sopenharmony_ci	spin_unlock_irqrestore(&chan->irqlock, flags);
233462306a36Sopenharmony_ci
233562306a36Sopenharmony_ci	if (!run_count && !active_run) {
233662306a36Sopenharmony_ci		dev_dbg(priv->ipu->dev,
233762306a36Sopenharmony_ci			"%s: task %u: no abort needed for ctx %p\n",
233862306a36Sopenharmony_ci			__func__, chan->ic_task, ctx);
233962306a36Sopenharmony_ci		return;
234062306a36Sopenharmony_ci	}
234162306a36Sopenharmony_ci
234262306a36Sopenharmony_ci	if (!active_run) {
234362306a36Sopenharmony_ci		empty_done_q(chan);
234462306a36Sopenharmony_ci		return;
234562306a36Sopenharmony_ci	}
234662306a36Sopenharmony_ci
234762306a36Sopenharmony_ci	dev_dbg(priv->ipu->dev,
234862306a36Sopenharmony_ci		"%s: task %u: wait for completion: %d runs\n",
234962306a36Sopenharmony_ci		__func__, chan->ic_task, run_count);
235062306a36Sopenharmony_ci
235162306a36Sopenharmony_ci	ret = wait_for_completion_timeout(&ctx->aborted,
235262306a36Sopenharmony_ci					  msecs_to_jiffies(10000));
235362306a36Sopenharmony_ci	if (ret == 0) {
235462306a36Sopenharmony_ci		dev_warn(priv->ipu->dev, "%s: timeout\n", __func__);
235562306a36Sopenharmony_ci		force_abort(ctx);
235662306a36Sopenharmony_ci	}
235762306a36Sopenharmony_ci}
235862306a36Sopenharmony_ci
235962306a36Sopenharmony_civoid ipu_image_convert_abort(struct ipu_image_convert_ctx *ctx)
236062306a36Sopenharmony_ci{
236162306a36Sopenharmony_ci	__ipu_image_convert_abort(ctx);
236262306a36Sopenharmony_ci	ctx->aborting = false;
236362306a36Sopenharmony_ci}
236462306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(ipu_image_convert_abort);
236562306a36Sopenharmony_ci
236662306a36Sopenharmony_ci/* Unprepare image conversion context */
236762306a36Sopenharmony_civoid ipu_image_convert_unprepare(struct ipu_image_convert_ctx *ctx)
236862306a36Sopenharmony_ci{
236962306a36Sopenharmony_ci	struct ipu_image_convert_chan *chan = ctx->chan;
237062306a36Sopenharmony_ci	struct ipu_image_convert_priv *priv = chan->priv;
237162306a36Sopenharmony_ci	unsigned long flags;
237262306a36Sopenharmony_ci	bool put_res;
237362306a36Sopenharmony_ci
237462306a36Sopenharmony_ci	/* make sure no runs are hanging around */
237562306a36Sopenharmony_ci	__ipu_image_convert_abort(ctx);
237662306a36Sopenharmony_ci
237762306a36Sopenharmony_ci	dev_dbg(priv->ipu->dev, "%s: task %u: removing ctx %p\n", __func__,
237862306a36Sopenharmony_ci		chan->ic_task, ctx);
237962306a36Sopenharmony_ci
238062306a36Sopenharmony_ci	spin_lock_irqsave(&chan->irqlock, flags);
238162306a36Sopenharmony_ci
238262306a36Sopenharmony_ci	list_del(&ctx->list);
238362306a36Sopenharmony_ci
238462306a36Sopenharmony_ci	put_res = list_empty(&chan->ctx_list);
238562306a36Sopenharmony_ci
238662306a36Sopenharmony_ci	spin_unlock_irqrestore(&chan->irqlock, flags);
238762306a36Sopenharmony_ci
238862306a36Sopenharmony_ci	if (put_res)
238962306a36Sopenharmony_ci		release_ipu_resources(chan);
239062306a36Sopenharmony_ci
239162306a36Sopenharmony_ci	free_dma_buf(priv, &ctx->rot_intermediate[1]);
239262306a36Sopenharmony_ci	free_dma_buf(priv, &ctx->rot_intermediate[0]);
239362306a36Sopenharmony_ci
239462306a36Sopenharmony_ci	kfree(ctx);
239562306a36Sopenharmony_ci}
239662306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(ipu_image_convert_unprepare);
239762306a36Sopenharmony_ci
239862306a36Sopenharmony_ci/*
239962306a36Sopenharmony_ci * "Canned" asynchronous single image conversion. Allocates and returns
240062306a36Sopenharmony_ci * a new conversion run.  On successful return the caller must free the
240162306a36Sopenharmony_ci * run and call ipu_image_convert_unprepare() after conversion completes.
240262306a36Sopenharmony_ci */
240362306a36Sopenharmony_cistruct ipu_image_convert_run *
240462306a36Sopenharmony_ciipu_image_convert(struct ipu_soc *ipu, enum ipu_ic_task ic_task,
240562306a36Sopenharmony_ci		  struct ipu_image *in, struct ipu_image *out,
240662306a36Sopenharmony_ci		  enum ipu_rotate_mode rot_mode,
240762306a36Sopenharmony_ci		  ipu_image_convert_cb_t complete,
240862306a36Sopenharmony_ci		  void *complete_context)
240962306a36Sopenharmony_ci{
241062306a36Sopenharmony_ci	struct ipu_image_convert_ctx *ctx;
241162306a36Sopenharmony_ci	struct ipu_image_convert_run *run;
241262306a36Sopenharmony_ci	int ret;
241362306a36Sopenharmony_ci
241462306a36Sopenharmony_ci	ctx = ipu_image_convert_prepare(ipu, ic_task, in, out, rot_mode,
241562306a36Sopenharmony_ci					complete, complete_context);
241662306a36Sopenharmony_ci	if (IS_ERR(ctx))
241762306a36Sopenharmony_ci		return ERR_CAST(ctx);
241862306a36Sopenharmony_ci
241962306a36Sopenharmony_ci	run = kzalloc(sizeof(*run), GFP_KERNEL);
242062306a36Sopenharmony_ci	if (!run) {
242162306a36Sopenharmony_ci		ipu_image_convert_unprepare(ctx);
242262306a36Sopenharmony_ci		return ERR_PTR(-ENOMEM);
242362306a36Sopenharmony_ci	}
242462306a36Sopenharmony_ci
242562306a36Sopenharmony_ci	run->ctx = ctx;
242662306a36Sopenharmony_ci	run->in_phys = in->phys0;
242762306a36Sopenharmony_ci	run->out_phys = out->phys0;
242862306a36Sopenharmony_ci
242962306a36Sopenharmony_ci	ret = ipu_image_convert_queue(run);
243062306a36Sopenharmony_ci	if (ret) {
243162306a36Sopenharmony_ci		ipu_image_convert_unprepare(ctx);
243262306a36Sopenharmony_ci		kfree(run);
243362306a36Sopenharmony_ci		return ERR_PTR(ret);
243462306a36Sopenharmony_ci	}
243562306a36Sopenharmony_ci
243662306a36Sopenharmony_ci	return run;
243762306a36Sopenharmony_ci}
243862306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(ipu_image_convert);
243962306a36Sopenharmony_ci
244062306a36Sopenharmony_ci/* "Canned" synchronous single image conversion */
244162306a36Sopenharmony_cistatic void image_convert_sync_complete(struct ipu_image_convert_run *run,
244262306a36Sopenharmony_ci					void *data)
244362306a36Sopenharmony_ci{
244462306a36Sopenharmony_ci	struct completion *comp = data;
244562306a36Sopenharmony_ci
244662306a36Sopenharmony_ci	complete(comp);
244762306a36Sopenharmony_ci}
244862306a36Sopenharmony_ci
244962306a36Sopenharmony_ciint ipu_image_convert_sync(struct ipu_soc *ipu, enum ipu_ic_task ic_task,
245062306a36Sopenharmony_ci			   struct ipu_image *in, struct ipu_image *out,
245162306a36Sopenharmony_ci			   enum ipu_rotate_mode rot_mode)
245262306a36Sopenharmony_ci{
245362306a36Sopenharmony_ci	struct ipu_image_convert_run *run;
245462306a36Sopenharmony_ci	struct completion comp;
245562306a36Sopenharmony_ci	int ret;
245662306a36Sopenharmony_ci
245762306a36Sopenharmony_ci	init_completion(&comp);
245862306a36Sopenharmony_ci
245962306a36Sopenharmony_ci	run = ipu_image_convert(ipu, ic_task, in, out, rot_mode,
246062306a36Sopenharmony_ci				image_convert_sync_complete, &comp);
246162306a36Sopenharmony_ci	if (IS_ERR(run))
246262306a36Sopenharmony_ci		return PTR_ERR(run);
246362306a36Sopenharmony_ci
246462306a36Sopenharmony_ci	ret = wait_for_completion_timeout(&comp, msecs_to_jiffies(10000));
246562306a36Sopenharmony_ci	ret = (ret == 0) ? -ETIMEDOUT : 0;
246662306a36Sopenharmony_ci
246762306a36Sopenharmony_ci	ipu_image_convert_unprepare(run->ctx);
246862306a36Sopenharmony_ci	kfree(run);
246962306a36Sopenharmony_ci
247062306a36Sopenharmony_ci	return ret;
247162306a36Sopenharmony_ci}
247262306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(ipu_image_convert_sync);
247362306a36Sopenharmony_ci
247462306a36Sopenharmony_ciint ipu_image_convert_init(struct ipu_soc *ipu, struct device *dev)
247562306a36Sopenharmony_ci{
247662306a36Sopenharmony_ci	struct ipu_image_convert_priv *priv;
247762306a36Sopenharmony_ci	int i;
247862306a36Sopenharmony_ci
247962306a36Sopenharmony_ci	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
248062306a36Sopenharmony_ci	if (!priv)
248162306a36Sopenharmony_ci		return -ENOMEM;
248262306a36Sopenharmony_ci
248362306a36Sopenharmony_ci	ipu->image_convert_priv = priv;
248462306a36Sopenharmony_ci	priv->ipu = ipu;
248562306a36Sopenharmony_ci
248662306a36Sopenharmony_ci	for (i = 0; i < IC_NUM_TASKS; i++) {
248762306a36Sopenharmony_ci		struct ipu_image_convert_chan *chan = &priv->chan[i];
248862306a36Sopenharmony_ci
248962306a36Sopenharmony_ci		chan->ic_task = i;
249062306a36Sopenharmony_ci		chan->priv = priv;
249162306a36Sopenharmony_ci		chan->dma_ch = &image_convert_dma_chan[i];
249262306a36Sopenharmony_ci		chan->in_eof_irq = -1;
249362306a36Sopenharmony_ci		chan->rot_in_eof_irq = -1;
249462306a36Sopenharmony_ci		chan->out_eof_irq = -1;
249562306a36Sopenharmony_ci		chan->rot_out_eof_irq = -1;
249662306a36Sopenharmony_ci
249762306a36Sopenharmony_ci		spin_lock_init(&chan->irqlock);
249862306a36Sopenharmony_ci		INIT_LIST_HEAD(&chan->ctx_list);
249962306a36Sopenharmony_ci		INIT_LIST_HEAD(&chan->pending_q);
250062306a36Sopenharmony_ci		INIT_LIST_HEAD(&chan->done_q);
250162306a36Sopenharmony_ci	}
250262306a36Sopenharmony_ci
250362306a36Sopenharmony_ci	return 0;
250462306a36Sopenharmony_ci}
250562306a36Sopenharmony_ci
250662306a36Sopenharmony_civoid ipu_image_convert_exit(struct ipu_soc *ipu)
250762306a36Sopenharmony_ci{
250862306a36Sopenharmony_ci}
2509