1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * vsp1_wpf.c -- R-Car VSP1 Write Pixel Formatter 4 * 5 * Copyright (C) 2013-2014 Renesas Electronics Corporation 6 * 7 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com) 8 */ 9 10 #include <linux/device.h> 11 12 #include <media/v4l2-subdev.h> 13 14 #include "vsp1.h" 15 #include "vsp1_dl.h" 16 #include "vsp1_pipe.h" 17 #include "vsp1_rwpf.h" 18 #include "vsp1_video.h" 19 20 #define WPF_GEN2_MAX_WIDTH 2048U 21 #define WPF_GEN2_MAX_HEIGHT 2048U 22 #define WPF_GEN3_MAX_WIDTH 8190U 23 #define WPF_GEN3_MAX_HEIGHT 8190U 24 25 /* ----------------------------------------------------------------------------- 26 * Device Access 27 */ 28 29 static inline void vsp1_wpf_write(struct vsp1_rwpf *wpf, 30 struct vsp1_dl_body *dlb, u32 reg, u32 data) 31 { 32 vsp1_dl_body_write(dlb, reg + wpf->entity.index * VI6_WPF_OFFSET, data); 33 } 34 35 /* ----------------------------------------------------------------------------- 36 * Controls 37 */ 38 39 enum wpf_flip_ctrl { 40 WPF_CTRL_VFLIP = 0, 41 WPF_CTRL_HFLIP = 1, 42 }; 43 44 static int vsp1_wpf_set_rotation(struct vsp1_rwpf *wpf, unsigned int rotation) 45 { 46 struct vsp1_video *video = wpf->video; 47 struct v4l2_mbus_framefmt *sink_format; 48 struct v4l2_mbus_framefmt *source_format; 49 bool rotate; 50 int ret = 0; 51 52 /* 53 * Only consider the 0°/180° from/to 90°/270° modifications, the rest 54 * is taken care of by the flipping configuration. 55 */ 56 rotate = rotation == 90 || rotation == 270; 57 if (rotate == wpf->flip.rotate) 58 return 0; 59 60 /* Changing rotation isn't allowed when buffers are allocated. */ 61 mutex_lock(&video->lock); 62 63 if (vb2_is_busy(&video->queue)) { 64 ret = -EBUSY; 65 goto done; 66 } 67 68 sink_format = vsp1_entity_get_pad_format(&wpf->entity, 69 wpf->entity.config, 70 RWPF_PAD_SINK); 71 source_format = vsp1_entity_get_pad_format(&wpf->entity, 72 wpf->entity.config, 73 RWPF_PAD_SOURCE); 74 75 mutex_lock(&wpf->entity.lock); 76 77 if (rotate) { 78 source_format->width = sink_format->height; 79 source_format->height = sink_format->width; 80 } else { 81 source_format->width = sink_format->width; 82 source_format->height = sink_format->height; 83 } 84 85 wpf->flip.rotate = rotate; 86 87 mutex_unlock(&wpf->entity.lock); 88 89 done: 90 mutex_unlock(&video->lock); 91 return ret; 92 } 93 94 static int vsp1_wpf_s_ctrl(struct v4l2_ctrl *ctrl) 95 { 96 struct vsp1_rwpf *wpf = 97 container_of(ctrl->handler, struct vsp1_rwpf, ctrls); 98 unsigned int rotation; 99 u32 flip = 0; 100 int ret; 101 102 /* Update the rotation. */ 103 rotation = wpf->flip.ctrls.rotate ? wpf->flip.ctrls.rotate->val : 0; 104 ret = vsp1_wpf_set_rotation(wpf, rotation); 105 if (ret < 0) 106 return ret; 107 108 /* 109 * Compute the flip value resulting from all three controls, with 110 * rotation by 180° flipping the image in both directions. Store the 111 * result in the pending flip field for the next frame that will be 112 * processed. 113 */ 114 if (wpf->flip.ctrls.vflip->val) 115 flip |= BIT(WPF_CTRL_VFLIP); 116 117 if (wpf->flip.ctrls.hflip && wpf->flip.ctrls.hflip->val) 118 flip |= BIT(WPF_CTRL_HFLIP); 119 120 if (rotation == 180 || rotation == 270) 121 flip ^= BIT(WPF_CTRL_VFLIP) | BIT(WPF_CTRL_HFLIP); 122 123 spin_lock_irq(&wpf->flip.lock); 124 wpf->flip.pending = flip; 125 spin_unlock_irq(&wpf->flip.lock); 126 127 return 0; 128 } 129 130 static const struct v4l2_ctrl_ops vsp1_wpf_ctrl_ops = { 131 .s_ctrl = vsp1_wpf_s_ctrl, 132 }; 133 134 static int wpf_init_controls(struct vsp1_rwpf *wpf) 135 { 136 struct vsp1_device *vsp1 = wpf->entity.vsp1; 137 unsigned int num_flip_ctrls; 138 139 spin_lock_init(&wpf->flip.lock); 140 141 if (wpf->entity.index != 0) { 142 /* Only WPF0 supports flipping. */ 143 num_flip_ctrls = 0; 144 } else if (vsp1_feature(vsp1, VSP1_HAS_WPF_HFLIP)) { 145 /* 146 * When horizontal flip is supported the WPF implements three 147 * controls (horizontal flip, vertical flip and rotation). 148 */ 149 num_flip_ctrls = 3; 150 } else if (vsp1_feature(vsp1, VSP1_HAS_WPF_VFLIP)) { 151 /* 152 * When only vertical flip is supported the WPF implements a 153 * single control (vertical flip). 154 */ 155 num_flip_ctrls = 1; 156 } else { 157 /* Otherwise flipping is not supported. */ 158 num_flip_ctrls = 0; 159 } 160 161 vsp1_rwpf_init_ctrls(wpf, num_flip_ctrls); 162 163 if (num_flip_ctrls >= 1) { 164 wpf->flip.ctrls.vflip = 165 v4l2_ctrl_new_std(&wpf->ctrls, &vsp1_wpf_ctrl_ops, 166 V4L2_CID_VFLIP, 0, 1, 1, 0); 167 } 168 169 if (num_flip_ctrls == 3) { 170 wpf->flip.ctrls.hflip = 171 v4l2_ctrl_new_std(&wpf->ctrls, &vsp1_wpf_ctrl_ops, 172 V4L2_CID_HFLIP, 0, 1, 1, 0); 173 wpf->flip.ctrls.rotate = 174 v4l2_ctrl_new_std(&wpf->ctrls, &vsp1_wpf_ctrl_ops, 175 V4L2_CID_ROTATE, 0, 270, 90, 0); 176 v4l2_ctrl_cluster(3, &wpf->flip.ctrls.vflip); 177 } 178 179 if (wpf->ctrls.error) { 180 dev_err(vsp1->dev, "wpf%u: failed to initialize controls\n", 181 wpf->entity.index); 182 return wpf->ctrls.error; 183 } 184 185 return 0; 186 } 187 188 /* ----------------------------------------------------------------------------- 189 * VSP1 Entity Operations 190 */ 191 192 void vsp1_wpf_stop(struct vsp1_rwpf *wpf) 193 { 194 struct vsp1_device *vsp1 = wpf->entity.vsp1; 195 196 /* 197 * Write to registers directly when stopping the stream as there will be 198 * no pipeline run to apply the display list. 199 */ 200 vsp1_write(vsp1, VI6_WPF_IRQ_ENB(wpf->entity.index), 0); 201 vsp1_write(vsp1, wpf->entity.index * VI6_WPF_OFFSET + 202 VI6_WPF_SRCRPF, 0); 203 } 204 205 static void vsp1_wpf_destroy(struct vsp1_entity *entity) 206 { 207 struct vsp1_rwpf *wpf = entity_to_rwpf(entity); 208 209 vsp1_dlm_destroy(wpf->dlm); 210 } 211 212 static int wpf_configure_writeback_chain(struct vsp1_rwpf *wpf, 213 struct vsp1_dl_list *dl) 214 { 215 unsigned int index = wpf->entity.index; 216 struct vsp1_dl_list *dl_next; 217 struct vsp1_dl_body *dlb; 218 219 dl_next = vsp1_dl_list_get(wpf->dlm); 220 if (!dl_next) { 221 dev_err(wpf->entity.vsp1->dev, 222 "Failed to obtain a dl list, disabling writeback\n"); 223 return -ENOMEM; 224 } 225 226 dlb = vsp1_dl_list_get_body0(dl_next); 227 vsp1_dl_body_write(dlb, VI6_WPF_WRBCK_CTRL(index), 0); 228 vsp1_dl_list_add_chain(dl, dl_next); 229 230 return 0; 231 } 232 233 static void wpf_configure_stream(struct vsp1_entity *entity, 234 struct vsp1_pipeline *pipe, 235 struct vsp1_dl_list *dl, 236 struct vsp1_dl_body *dlb) 237 { 238 struct vsp1_rwpf *wpf = to_rwpf(&entity->subdev); 239 struct vsp1_device *vsp1 = wpf->entity.vsp1; 240 const struct v4l2_mbus_framefmt *source_format; 241 const struct v4l2_mbus_framefmt *sink_format; 242 unsigned int index = wpf->entity.index; 243 unsigned int i; 244 u32 outfmt = 0; 245 u32 srcrpf = 0; 246 int ret; 247 248 sink_format = vsp1_entity_get_pad_format(&wpf->entity, 249 wpf->entity.config, 250 RWPF_PAD_SINK); 251 source_format = vsp1_entity_get_pad_format(&wpf->entity, 252 wpf->entity.config, 253 RWPF_PAD_SOURCE); 254 255 /* Format */ 256 if (!pipe->lif || wpf->writeback) { 257 const struct v4l2_pix_format_mplane *format = &wpf->format; 258 const struct vsp1_format_info *fmtinfo = wpf->fmtinfo; 259 260 outfmt = fmtinfo->hwfmt << VI6_WPF_OUTFMT_WRFMT_SHIFT; 261 262 if (wpf->flip.rotate) 263 outfmt |= VI6_WPF_OUTFMT_ROT; 264 265 if (fmtinfo->alpha) 266 outfmt |= VI6_WPF_OUTFMT_PXA; 267 if (fmtinfo->swap_yc) 268 outfmt |= VI6_WPF_OUTFMT_SPYCS; 269 if (fmtinfo->swap_uv) 270 outfmt |= VI6_WPF_OUTFMT_SPUVS; 271 272 /* Destination stride and byte swapping. */ 273 vsp1_wpf_write(wpf, dlb, VI6_WPF_DSTM_STRIDE_Y, 274 format->plane_fmt[0].bytesperline); 275 if (format->num_planes > 1) 276 vsp1_wpf_write(wpf, dlb, VI6_WPF_DSTM_STRIDE_C, 277 format->plane_fmt[1].bytesperline); 278 279 vsp1_wpf_write(wpf, dlb, VI6_WPF_DSWAP, fmtinfo->swap); 280 281 if (vsp1_feature(vsp1, VSP1_HAS_WPF_HFLIP) && index == 0) 282 vsp1_wpf_write(wpf, dlb, VI6_WPF_ROT_CTRL, 283 VI6_WPF_ROT_CTRL_LN16 | 284 (256 << VI6_WPF_ROT_CTRL_LMEM_WD_SHIFT)); 285 } 286 287 if (sink_format->code != source_format->code) 288 outfmt |= VI6_WPF_OUTFMT_CSC; 289 290 wpf->outfmt = outfmt; 291 292 vsp1_dl_body_write(dlb, VI6_DPR_WPF_FPORCH(index), 293 VI6_DPR_WPF_FPORCH_FP_WPFN); 294 295 /* 296 * Sources. If the pipeline has a single input and BRx is not used, 297 * configure it as the master layer. Otherwise configure all 298 * inputs as sub-layers and select the virtual RPF as the master 299 * layer. 300 */ 301 for (i = 0; i < vsp1->info->rpf_count; ++i) { 302 struct vsp1_rwpf *input = pipe->inputs[i]; 303 304 if (!input) 305 continue; 306 307 srcrpf |= (!pipe->brx && pipe->num_inputs == 1) 308 ? VI6_WPF_SRCRPF_RPF_ACT_MST(input->entity.index) 309 : VI6_WPF_SRCRPF_RPF_ACT_SUB(input->entity.index); 310 } 311 312 if (pipe->brx) 313 srcrpf |= pipe->brx->type == VSP1_ENTITY_BRU 314 ? VI6_WPF_SRCRPF_VIRACT_MST 315 : VI6_WPF_SRCRPF_VIRACT2_MST; 316 317 vsp1_wpf_write(wpf, dlb, VI6_WPF_SRCRPF, srcrpf); 318 319 /* Enable interrupts. */ 320 vsp1_dl_body_write(dlb, VI6_WPF_IRQ_STA(index), 0); 321 vsp1_dl_body_write(dlb, VI6_WPF_IRQ_ENB(index), 322 VI6_WPF_IRQ_ENB_DFEE); 323 324 /* 325 * Configure writeback for display pipelines (the wpf writeback flag is 326 * never set for memory-to-memory pipelines). Start by adding a chained 327 * display list to disable writeback after a single frame, and process 328 * to enable writeback. If the display list allocation fails don't 329 * enable writeback as we wouldn't be able to safely disable it, 330 * resulting in possible memory corruption. 331 */ 332 if (wpf->writeback) { 333 ret = wpf_configure_writeback_chain(wpf, dl); 334 if (ret < 0) 335 wpf->writeback = false; 336 } 337 338 vsp1_dl_body_write(dlb, VI6_WPF_WRBCK_CTRL(index), 339 wpf->writeback ? VI6_WPF_WRBCK_CTRL_WBMD : 0); 340 } 341 342 static void wpf_configure_frame(struct vsp1_entity *entity, 343 struct vsp1_pipeline *pipe, 344 struct vsp1_dl_list *dl, 345 struct vsp1_dl_body *dlb) 346 { 347 const unsigned int mask = BIT(WPF_CTRL_VFLIP) 348 | BIT(WPF_CTRL_HFLIP); 349 struct vsp1_rwpf *wpf = to_rwpf(&entity->subdev); 350 unsigned long flags; 351 u32 outfmt; 352 353 spin_lock_irqsave(&wpf->flip.lock, flags); 354 wpf->flip.active = (wpf->flip.active & ~mask) 355 | (wpf->flip.pending & mask); 356 spin_unlock_irqrestore(&wpf->flip.lock, flags); 357 358 outfmt = (wpf->alpha << VI6_WPF_OUTFMT_PDV_SHIFT) | wpf->outfmt; 359 360 if (wpf->flip.active & BIT(WPF_CTRL_VFLIP)) 361 outfmt |= VI6_WPF_OUTFMT_FLP; 362 if (wpf->flip.active & BIT(WPF_CTRL_HFLIP)) 363 outfmt |= VI6_WPF_OUTFMT_HFLP; 364 365 vsp1_wpf_write(wpf, dlb, VI6_WPF_OUTFMT, outfmt); 366 } 367 368 static void wpf_configure_partition(struct vsp1_entity *entity, 369 struct vsp1_pipeline *pipe, 370 struct vsp1_dl_list *dl, 371 struct vsp1_dl_body *dlb) 372 { 373 struct vsp1_rwpf *wpf = to_rwpf(&entity->subdev); 374 struct vsp1_device *vsp1 = wpf->entity.vsp1; 375 struct vsp1_rwpf_memory mem = wpf->mem; 376 const struct v4l2_mbus_framefmt *sink_format; 377 const struct v4l2_pix_format_mplane *format = &wpf->format; 378 const struct vsp1_format_info *fmtinfo = wpf->fmtinfo; 379 unsigned int width; 380 unsigned int height; 381 unsigned int left; 382 unsigned int offset; 383 unsigned int flip; 384 unsigned int i; 385 386 sink_format = vsp1_entity_get_pad_format(&wpf->entity, 387 wpf->entity.config, 388 RWPF_PAD_SINK); 389 width = sink_format->width; 390 height = sink_format->height; 391 left = 0; 392 393 /* 394 * Cropping. The partition algorithm can split the image into 395 * multiple slices. 396 */ 397 if (pipe->partitions > 1) { 398 width = pipe->partition->wpf.width; 399 left = pipe->partition->wpf.left; 400 } 401 402 vsp1_wpf_write(wpf, dlb, VI6_WPF_HSZCLIP, VI6_WPF_SZCLIP_EN | 403 (0 << VI6_WPF_SZCLIP_OFST_SHIFT) | 404 (width << VI6_WPF_SZCLIP_SIZE_SHIFT)); 405 vsp1_wpf_write(wpf, dlb, VI6_WPF_VSZCLIP, VI6_WPF_SZCLIP_EN | 406 (0 << VI6_WPF_SZCLIP_OFST_SHIFT) | 407 (height << VI6_WPF_SZCLIP_SIZE_SHIFT)); 408 409 /* 410 * For display pipelines without writeback enabled there's no memory 411 * address to configure, return now. 412 */ 413 if (pipe->lif && !wpf->writeback) 414 return; 415 416 /* 417 * Update the memory offsets based on flipping configuration. 418 * The destination addresses point to the locations where the 419 * VSP starts writing to memory, which can be any corner of the 420 * image depending on the combination of flipping and rotation. 421 */ 422 423 /* 424 * First take the partition left coordinate into account. 425 * Compute the offset to order the partitions correctly on the 426 * output based on whether flipping is enabled. Consider 427 * horizontal flipping when rotation is disabled but vertical 428 * flipping when rotation is enabled, as rotating the image 429 * switches the horizontal and vertical directions. The offset 430 * is applied horizontally or vertically accordingly. 431 */ 432 flip = wpf->flip.active; 433 434 if (flip & BIT(WPF_CTRL_HFLIP) && !wpf->flip.rotate) 435 offset = format->width - left - width; 436 else if (flip & BIT(WPF_CTRL_VFLIP) && wpf->flip.rotate) 437 offset = format->height - left - width; 438 else 439 offset = left; 440 441 for (i = 0; i < format->num_planes; ++i) { 442 unsigned int hsub = i > 0 ? fmtinfo->hsub : 1; 443 unsigned int vsub = i > 0 ? fmtinfo->vsub : 1; 444 445 if (wpf->flip.rotate) 446 mem.addr[i] += offset / vsub 447 * format->plane_fmt[i].bytesperline; 448 else 449 mem.addr[i] += offset / hsub 450 * fmtinfo->bpp[i] / 8; 451 } 452 453 if (flip & BIT(WPF_CTRL_VFLIP)) { 454 /* 455 * When rotating the output (after rotation) image 456 * height is equal to the partition width (before 457 * rotation). Otherwise it is equal to the output 458 * image height. 459 */ 460 if (wpf->flip.rotate) 461 height = width; 462 else 463 height = format->height; 464 465 mem.addr[0] += (height - 1) 466 * format->plane_fmt[0].bytesperline; 467 468 if (format->num_planes > 1) { 469 offset = (height / fmtinfo->vsub - 1) 470 * format->plane_fmt[1].bytesperline; 471 mem.addr[1] += offset; 472 mem.addr[2] += offset; 473 } 474 } 475 476 if (wpf->flip.rotate && !(flip & BIT(WPF_CTRL_HFLIP))) { 477 unsigned int hoffset = max(0, (int)format->width - 16); 478 479 /* 480 * Compute the output coordinate. The partition 481 * horizontal (left) offset becomes a vertical offset. 482 */ 483 for (i = 0; i < format->num_planes; ++i) { 484 unsigned int hsub = i > 0 ? fmtinfo->hsub : 1; 485 486 mem.addr[i] += hoffset / hsub 487 * fmtinfo->bpp[i] / 8; 488 } 489 } 490 491 /* 492 * On Gen3+ hardware the SPUVS bit has no effect on 3-planar 493 * formats. Swap the U and V planes manually in that case. 494 */ 495 if (vsp1->info->gen >= 3 && format->num_planes == 3 && 496 fmtinfo->swap_uv) 497 swap(mem.addr[1], mem.addr[2]); 498 499 vsp1_wpf_write(wpf, dlb, VI6_WPF_DSTM_ADDR_Y, mem.addr[0]); 500 vsp1_wpf_write(wpf, dlb, VI6_WPF_DSTM_ADDR_C0, mem.addr[1]); 501 vsp1_wpf_write(wpf, dlb, VI6_WPF_DSTM_ADDR_C1, mem.addr[2]); 502 503 /* 504 * Writeback operates in single-shot mode and lasts for a single frame, 505 * reset the writeback flag to false for the next frame. 506 */ 507 wpf->writeback = false; 508 } 509 510 static unsigned int wpf_max_width(struct vsp1_entity *entity, 511 struct vsp1_pipeline *pipe) 512 { 513 struct vsp1_rwpf *wpf = to_rwpf(&entity->subdev); 514 515 return wpf->flip.rotate ? 256 : wpf->max_width; 516 } 517 518 static void wpf_partition(struct vsp1_entity *entity, 519 struct vsp1_pipeline *pipe, 520 struct vsp1_partition *partition, 521 unsigned int partition_idx, 522 struct vsp1_partition_window *window) 523 { 524 partition->wpf = *window; 525 } 526 527 static const struct vsp1_entity_operations wpf_entity_ops = { 528 .destroy = vsp1_wpf_destroy, 529 .configure_stream = wpf_configure_stream, 530 .configure_frame = wpf_configure_frame, 531 .configure_partition = wpf_configure_partition, 532 .max_width = wpf_max_width, 533 .partition = wpf_partition, 534 }; 535 536 /* ----------------------------------------------------------------------------- 537 * Initialization and Cleanup 538 */ 539 540 struct vsp1_rwpf *vsp1_wpf_create(struct vsp1_device *vsp1, unsigned int index) 541 { 542 struct vsp1_rwpf *wpf; 543 char name[6]; 544 int ret; 545 546 wpf = devm_kzalloc(vsp1->dev, sizeof(*wpf), GFP_KERNEL); 547 if (wpf == NULL) 548 return ERR_PTR(-ENOMEM); 549 550 if (vsp1->info->gen == 2) { 551 wpf->max_width = WPF_GEN2_MAX_WIDTH; 552 wpf->max_height = WPF_GEN2_MAX_HEIGHT; 553 } else { 554 wpf->max_width = WPF_GEN3_MAX_WIDTH; 555 wpf->max_height = WPF_GEN3_MAX_HEIGHT; 556 } 557 558 wpf->entity.ops = &wpf_entity_ops; 559 wpf->entity.type = VSP1_ENTITY_WPF; 560 wpf->entity.index = index; 561 562 sprintf(name, "wpf.%u", index); 563 ret = vsp1_entity_init(vsp1, &wpf->entity, name, 2, &vsp1_rwpf_subdev_ops, 564 MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER); 565 if (ret < 0) 566 return ERR_PTR(ret); 567 568 /* Initialize the display list manager. */ 569 wpf->dlm = vsp1_dlm_create(vsp1, index, 64); 570 if (!wpf->dlm) { 571 ret = -ENOMEM; 572 goto error; 573 } 574 575 /* Initialize the control handler. */ 576 ret = wpf_init_controls(wpf); 577 if (ret < 0) { 578 dev_err(vsp1->dev, "wpf%u: failed to initialize controls\n", 579 index); 580 goto error; 581 } 582 583 v4l2_ctrl_handler_setup(&wpf->ctrls); 584 585 return wpf; 586 587 error: 588 vsp1_entity_destroy(&wpf->entity); 589 return ERR_PTR(ret); 590 } 591