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