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