1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2012-2016, The Linux Foundation. All rights reserved. 4 * Copyright (C) 2017 Linaro Ltd. 5 */ 6 #include <linux/clk.h> 7 #include <linux/module.h> 8 #include <linux/mod_devicetable.h> 9 #include <linux/platform_device.h> 10 #include <linux/pm_runtime.h> 11 #include <linux/slab.h> 12 #include <media/v4l2-ioctl.h> 13 #include <media/v4l2-event.h> 14 #include <media/v4l2-ctrls.h> 15 #include <media/v4l2-mem2mem.h> 16 #include <media/videobuf2-dma-contig.h> 17 18 #include "hfi_venus_io.h" 19 #include "hfi_parser.h" 20 #include "core.h" 21 #include "helpers.h" 22 #include "vdec.h" 23 #include "pm_helpers.h" 24 25 /* 26 * Three resons to keep MPLANE formats (despite that the number of planes 27 * currently is one): 28 * - the MPLANE formats allow only one plane to be used 29 * - the downstream driver use MPLANE formats too 30 * - future firmware versions could add support for >1 planes 31 */ 32 static const struct venus_format vdec_formats[] = { 33 [VENUS_FMT_NV12] = { 34 .pixfmt = V4L2_PIX_FMT_NV12, 35 .num_planes = 1, 36 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, 37 }, 38 [VENUS_FMT_QC08C] = { 39 .pixfmt = V4L2_PIX_FMT_QC08C, 40 .num_planes = 1, 41 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, 42 }, 43 [VENUS_FMT_QC10C] = { 44 .pixfmt = V4L2_PIX_FMT_QC10C, 45 .num_planes = 1, 46 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, 47 }, 48 [VENUS_FMT_P010] = { 49 .pixfmt = V4L2_PIX_FMT_P010, 50 .num_planes = 1, 51 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, 52 }, 53 [VENUS_FMT_H264] = { 54 .pixfmt = V4L2_PIX_FMT_H264, 55 .num_planes = 1, 56 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, 57 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION, 58 }, 59 [VENUS_FMT_VP8] = { 60 .pixfmt = V4L2_PIX_FMT_VP8, 61 .num_planes = 1, 62 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, 63 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION, 64 }, 65 [VENUS_FMT_VP9] = { 66 .pixfmt = V4L2_PIX_FMT_VP9, 67 .num_planes = 1, 68 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, 69 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION, 70 }, 71 [VENUS_FMT_HEVC] = { 72 .pixfmt = V4L2_PIX_FMT_HEVC, 73 .num_planes = 1, 74 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, 75 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION, 76 }, 77 [VENUS_FMT_VC1_ANNEX_G] = { 78 .pixfmt = V4L2_PIX_FMT_VC1_ANNEX_G, 79 .num_planes = 1, 80 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, 81 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION, 82 }, 83 [VENUS_FMT_VC1_ANNEX_L] = { 84 .pixfmt = V4L2_PIX_FMT_VC1_ANNEX_L, 85 .num_planes = 1, 86 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, 87 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION, 88 }, 89 [VENUS_FMT_MPEG4] = { 90 .pixfmt = V4L2_PIX_FMT_MPEG4, 91 .num_planes = 1, 92 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, 93 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION, 94 }, 95 [VENUS_FMT_MPEG2] = { 96 .pixfmt = V4L2_PIX_FMT_MPEG2, 97 .num_planes = 1, 98 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, 99 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION, 100 }, 101 [VENUS_FMT_H263] = { 102 .pixfmt = V4L2_PIX_FMT_H263, 103 .num_planes = 1, 104 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, 105 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION, 106 }, 107 [VENUS_FMT_XVID] = { 108 .pixfmt = V4L2_PIX_FMT_XVID, 109 .num_planes = 1, 110 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, 111 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION, 112 }, 113 114 }; 115 116 static const struct venus_format * 117 find_format(struct venus_inst *inst, u32 pixfmt, u32 type) 118 { 119 const struct venus_format *fmt = vdec_formats; 120 unsigned int size = ARRAY_SIZE(vdec_formats); 121 unsigned int i; 122 123 for (i = 0; i < size; i++) { 124 if (fmt[i].pixfmt == pixfmt) 125 break; 126 } 127 128 if (i == size || fmt[i].type != type) 129 return NULL; 130 131 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE && 132 !venus_helper_check_codec(inst, fmt[i].pixfmt)) 133 return NULL; 134 135 if (V4L2_TYPE_IS_CAPTURE(type) && 136 !venus_helper_check_format(inst, fmt[i].pixfmt)) 137 return NULL; 138 139 if (V4L2_TYPE_IS_CAPTURE(type) && fmt[i].pixfmt == V4L2_PIX_FMT_QC10C && 140 !(inst->bit_depth == VIDC_BITDEPTH_10)) 141 return NULL; 142 143 return &fmt[i]; 144 } 145 146 static const struct venus_format * 147 find_format_by_index(struct venus_inst *inst, unsigned int index, u32 type) 148 { 149 const struct venus_format *fmt = vdec_formats; 150 unsigned int size = ARRAY_SIZE(vdec_formats); 151 unsigned int i, k = 0; 152 153 if (index > size) 154 return NULL; 155 156 for (i = 0; i < size; i++) { 157 bool valid = false; 158 159 if (fmt[i].type != type) 160 continue; 161 162 if (V4L2_TYPE_IS_OUTPUT(type)) { 163 valid = venus_helper_check_codec(inst, fmt[i].pixfmt); 164 } else { 165 valid = venus_helper_check_format(inst, fmt[i].pixfmt); 166 167 if (fmt[i].pixfmt == V4L2_PIX_FMT_QC10C && 168 !(inst->bit_depth == VIDC_BITDEPTH_10)) 169 valid = false; 170 } 171 172 if (k == index && valid) 173 break; 174 if (valid) 175 k++; 176 } 177 178 if (i == size) 179 return NULL; 180 181 return &fmt[i]; 182 } 183 184 static const struct venus_format * 185 vdec_try_fmt_common(struct venus_inst *inst, struct v4l2_format *f) 186 { 187 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp; 188 struct v4l2_plane_pix_format *pfmt = pixmp->plane_fmt; 189 const struct venus_format *fmt; 190 u32 szimage; 191 192 memset(pfmt[0].reserved, 0, sizeof(pfmt[0].reserved)); 193 memset(pixmp->reserved, 0, sizeof(pixmp->reserved)); 194 195 fmt = find_format(inst, pixmp->pixelformat, f->type); 196 if (!fmt) { 197 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) 198 pixmp->pixelformat = V4L2_PIX_FMT_NV12; 199 else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) 200 pixmp->pixelformat = V4L2_PIX_FMT_H264; 201 else 202 return NULL; 203 fmt = find_format(inst, pixmp->pixelformat, f->type); 204 if (!fmt) 205 return NULL; 206 } 207 208 pixmp->width = clamp(pixmp->width, frame_width_min(inst), 209 frame_width_max(inst)); 210 pixmp->height = clamp(pixmp->height, frame_height_min(inst), 211 frame_height_max(inst)); 212 213 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) 214 pixmp->height = ALIGN(pixmp->height, 32); 215 216 if (pixmp->field == V4L2_FIELD_ANY) 217 pixmp->field = V4L2_FIELD_NONE; 218 pixmp->num_planes = fmt->num_planes; 219 pixmp->flags = 0; 220 221 szimage = venus_helper_get_framesz(pixmp->pixelformat, pixmp->width, 222 pixmp->height); 223 224 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) { 225 unsigned int stride = pixmp->width; 226 227 if (pixmp->pixelformat == V4L2_PIX_FMT_P010) 228 stride *= 2; 229 230 pfmt[0].sizeimage = szimage; 231 pfmt[0].bytesperline = ALIGN(stride, 128); 232 } else { 233 pfmt[0].sizeimage = clamp_t(u32, pfmt[0].sizeimage, 0, SZ_8M); 234 pfmt[0].sizeimage = max(pfmt[0].sizeimage, szimage); 235 pfmt[0].bytesperline = 0; 236 } 237 238 return fmt; 239 } 240 241 static int vdec_try_fmt(struct file *file, void *fh, struct v4l2_format *f) 242 { 243 struct venus_inst *inst = to_inst(file); 244 245 vdec_try_fmt_common(inst, f); 246 247 return 0; 248 } 249 250 static int vdec_check_src_change(struct venus_inst *inst) 251 { 252 int ret; 253 254 if (inst->subscriptions & V4L2_EVENT_SOURCE_CHANGE && 255 inst->codec_state == VENUS_DEC_STATE_INIT && 256 !inst->reconfig) 257 return -EINVAL; 258 259 if (inst->subscriptions & V4L2_EVENT_SOURCE_CHANGE) 260 return 0; 261 262 /* 263 * The code snippet below is a workaround for backward compatibility 264 * with applications which doesn't support V4L2 events. It will be 265 * dropped in future once those applications are fixed. 266 */ 267 268 if (inst->codec_state != VENUS_DEC_STATE_INIT) 269 goto done; 270 271 ret = wait_event_timeout(inst->reconf_wait, inst->reconfig, 272 msecs_to_jiffies(100)); 273 if (!ret) 274 return -EINVAL; 275 276 if (!(inst->codec_state == VENUS_DEC_STATE_CAPTURE_SETUP) || 277 !inst->reconfig) 278 dev_dbg(inst->core->dev, VDBGH "wrong state\n"); 279 280 done: 281 return 0; 282 } 283 284 static int vdec_g_fmt(struct file *file, void *fh, struct v4l2_format *f) 285 { 286 struct venus_inst *inst = to_inst(file); 287 const struct venus_format *fmt = NULL; 288 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp; 289 int ret; 290 291 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) 292 fmt = inst->fmt_cap; 293 else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) 294 fmt = inst->fmt_out; 295 296 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) { 297 ret = vdec_check_src_change(inst); 298 if (ret) 299 return ret; 300 } 301 302 pixmp->pixelformat = fmt->pixfmt; 303 304 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) { 305 pixmp->width = inst->width; 306 pixmp->height = inst->height; 307 pixmp->colorspace = inst->colorspace; 308 pixmp->ycbcr_enc = inst->ycbcr_enc; 309 pixmp->quantization = inst->quantization; 310 pixmp->xfer_func = inst->xfer_func; 311 } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) { 312 pixmp->width = inst->out_width; 313 pixmp->height = inst->out_height; 314 } 315 316 vdec_try_fmt_common(inst, f); 317 318 return 0; 319 } 320 321 static int vdec_s_fmt(struct file *file, void *fh, struct v4l2_format *f) 322 { 323 struct venus_inst *inst = to_inst(file); 324 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp; 325 struct v4l2_pix_format_mplane orig_pixmp; 326 const struct venus_format *fmt; 327 struct v4l2_format format; 328 u32 pixfmt_out = 0, pixfmt_cap = 0; 329 struct vb2_queue *q; 330 331 q = v4l2_m2m_get_vq(inst->m2m_ctx, f->type); 332 333 if (vb2_is_busy(q)) 334 return -EBUSY; 335 336 orig_pixmp = *pixmp; 337 338 fmt = vdec_try_fmt_common(inst, f); 339 340 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) { 341 pixfmt_out = pixmp->pixelformat; 342 pixfmt_cap = inst->fmt_cap->pixfmt; 343 } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) { 344 pixfmt_cap = pixmp->pixelformat; 345 pixfmt_out = inst->fmt_out->pixfmt; 346 } 347 348 memset(&format, 0, sizeof(format)); 349 350 format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; 351 format.fmt.pix_mp.pixelformat = pixfmt_out; 352 format.fmt.pix_mp.width = orig_pixmp.width; 353 format.fmt.pix_mp.height = orig_pixmp.height; 354 vdec_try_fmt_common(inst, &format); 355 356 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) { 357 inst->out_width = format.fmt.pix_mp.width; 358 inst->out_height = format.fmt.pix_mp.height; 359 inst->colorspace = pixmp->colorspace; 360 inst->ycbcr_enc = pixmp->ycbcr_enc; 361 inst->quantization = pixmp->quantization; 362 inst->xfer_func = pixmp->xfer_func; 363 inst->input_buf_size = pixmp->plane_fmt[0].sizeimage; 364 } 365 366 memset(&format, 0, sizeof(format)); 367 368 format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; 369 format.fmt.pix_mp.pixelformat = pixfmt_cap; 370 format.fmt.pix_mp.width = orig_pixmp.width; 371 format.fmt.pix_mp.height = orig_pixmp.height; 372 vdec_try_fmt_common(inst, &format); 373 374 inst->width = format.fmt.pix_mp.width; 375 inst->height = format.fmt.pix_mp.height; 376 inst->crop.top = 0; 377 inst->crop.left = 0; 378 inst->crop.width = inst->width; 379 inst->crop.height = inst->height; 380 381 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) 382 inst->fmt_out = fmt; 383 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) { 384 inst->fmt_cap = fmt; 385 inst->output2_buf_size = 386 venus_helper_get_framesz(pixfmt_cap, orig_pixmp.width, orig_pixmp.height); 387 } 388 389 return 0; 390 } 391 392 static int 393 vdec_g_selection(struct file *file, void *fh, struct v4l2_selection *s) 394 { 395 struct venus_inst *inst = to_inst(file); 396 397 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE && 398 s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) 399 return -EINVAL; 400 401 s->r.top = 0; 402 s->r.left = 0; 403 404 switch (s->target) { 405 case V4L2_SEL_TGT_CROP_BOUNDS: 406 case V4L2_SEL_TGT_CROP_DEFAULT: 407 case V4L2_SEL_TGT_CROP: 408 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) 409 return -EINVAL; 410 s->r.width = inst->out_width; 411 s->r.height = inst->out_height; 412 break; 413 case V4L2_SEL_TGT_COMPOSE_BOUNDS: 414 case V4L2_SEL_TGT_COMPOSE_PADDED: 415 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 416 return -EINVAL; 417 s->r.width = inst->width; 418 s->r.height = inst->height; 419 break; 420 case V4L2_SEL_TGT_COMPOSE_DEFAULT: 421 case V4L2_SEL_TGT_COMPOSE: 422 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 423 return -EINVAL; 424 s->r = inst->crop; 425 break; 426 default: 427 return -EINVAL; 428 } 429 430 return 0; 431 } 432 433 static int 434 vdec_querycap(struct file *file, void *fh, struct v4l2_capability *cap) 435 { 436 strscpy(cap->driver, "qcom-venus", sizeof(cap->driver)); 437 strscpy(cap->card, "Qualcomm Venus video decoder", sizeof(cap->card)); 438 strscpy(cap->bus_info, "platform:qcom-venus", sizeof(cap->bus_info)); 439 440 return 0; 441 } 442 443 static int vdec_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f) 444 { 445 struct venus_inst *inst = to_inst(file); 446 const struct venus_format *fmt; 447 448 memset(f->reserved, 0, sizeof(f->reserved)); 449 450 fmt = find_format_by_index(inst, f->index, f->type); 451 if (!fmt) 452 return -EINVAL; 453 454 f->pixelformat = fmt->pixfmt; 455 f->flags = fmt->flags; 456 457 return 0; 458 } 459 460 static int vdec_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a) 461 { 462 struct venus_inst *inst = to_inst(file); 463 struct v4l2_captureparm *cap = &a->parm.capture; 464 struct v4l2_fract *timeperframe = &cap->timeperframe; 465 u64 us_per_frame, fps; 466 467 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE && 468 a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) 469 return -EINVAL; 470 471 memset(cap->reserved, 0, sizeof(cap->reserved)); 472 if (!timeperframe->denominator) 473 timeperframe->denominator = inst->timeperframe.denominator; 474 if (!timeperframe->numerator) 475 timeperframe->numerator = inst->timeperframe.numerator; 476 cap->readbuffers = 0; 477 cap->extendedmode = 0; 478 cap->capability = V4L2_CAP_TIMEPERFRAME; 479 us_per_frame = timeperframe->numerator * (u64)USEC_PER_SEC; 480 do_div(us_per_frame, timeperframe->denominator); 481 482 us_per_frame = clamp(us_per_frame, 1, USEC_PER_SEC); 483 fps = USEC_PER_SEC / (u32)us_per_frame; 484 fps = min(VENUS_MAX_FPS, fps); 485 486 inst->fps = fps; 487 inst->timeperframe = *timeperframe; 488 489 return 0; 490 } 491 492 static int vdec_enum_framesizes(struct file *file, void *fh, 493 struct v4l2_frmsizeenum *fsize) 494 { 495 struct venus_inst *inst = to_inst(file); 496 const struct venus_format *fmt; 497 498 fmt = find_format(inst, fsize->pixel_format, 499 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE); 500 if (!fmt) { 501 fmt = find_format(inst, fsize->pixel_format, 502 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE); 503 if (!fmt) 504 return -EINVAL; 505 } 506 507 if (fsize->index) 508 return -EINVAL; 509 510 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE; 511 512 fsize->stepwise.min_width = frame_width_min(inst); 513 fsize->stepwise.max_width = frame_width_max(inst); 514 fsize->stepwise.step_width = frame_width_step(inst); 515 fsize->stepwise.min_height = frame_height_min(inst); 516 fsize->stepwise.max_height = frame_height_max(inst); 517 fsize->stepwise.step_height = frame_height_step(inst); 518 519 return 0; 520 } 521 522 static int vdec_subscribe_event(struct v4l2_fh *fh, 523 const struct v4l2_event_subscription *sub) 524 { 525 struct venus_inst *inst = container_of(fh, struct venus_inst, fh); 526 int ret; 527 528 switch (sub->type) { 529 case V4L2_EVENT_EOS: 530 return v4l2_event_subscribe(fh, sub, 2, NULL); 531 case V4L2_EVENT_SOURCE_CHANGE: 532 ret = v4l2_src_change_event_subscribe(fh, sub); 533 if (ret) 534 return ret; 535 inst->subscriptions |= V4L2_EVENT_SOURCE_CHANGE; 536 return 0; 537 case V4L2_EVENT_CTRL: 538 return v4l2_ctrl_subscribe_event(fh, sub); 539 default: 540 return -EINVAL; 541 } 542 } 543 544 static int 545 vdec_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *cmd) 546 { 547 struct venus_inst *inst = to_inst(file); 548 struct vb2_queue *dst_vq; 549 struct hfi_frame_data fdata = {0}; 550 int ret; 551 552 ret = v4l2_m2m_ioctl_try_decoder_cmd(file, fh, cmd); 553 if (ret) 554 return ret; 555 556 mutex_lock(&inst->lock); 557 558 if (cmd->cmd == V4L2_DEC_CMD_STOP) { 559 /* 560 * Implement V4L2_DEC_CMD_STOP by enqueue an empty buffer on 561 * decoder input to signal EOS. 562 */ 563 if (!(inst->streamon_out && inst->streamon_cap)) 564 goto unlock; 565 566 fdata.buffer_type = HFI_BUFFER_INPUT; 567 fdata.flags |= HFI_BUFFERFLAG_EOS; 568 if (IS_V6(inst->core) && is_fw_rev_or_older(inst->core, 1, 0, 87)) 569 fdata.device_addr = 0; 570 else 571 fdata.device_addr = 0xdeadb000; 572 573 ret = hfi_session_process_buf(inst, &fdata); 574 575 if (!ret && inst->codec_state == VENUS_DEC_STATE_DECODING) { 576 inst->codec_state = VENUS_DEC_STATE_DRAIN; 577 inst->drain_active = true; 578 } 579 } else if (cmd->cmd == V4L2_DEC_CMD_START && 580 inst->codec_state == VENUS_DEC_STATE_STOPPED) { 581 dst_vq = v4l2_m2m_get_vq(inst->fh.m2m_ctx, 582 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE); 583 vb2_clear_last_buffer_dequeued(dst_vq); 584 585 inst->codec_state = VENUS_DEC_STATE_DECODING; 586 } 587 588 unlock: 589 mutex_unlock(&inst->lock); 590 return ret; 591 } 592 593 static const struct v4l2_ioctl_ops vdec_ioctl_ops = { 594 .vidioc_querycap = vdec_querycap, 595 .vidioc_enum_fmt_vid_cap = vdec_enum_fmt, 596 .vidioc_enum_fmt_vid_out = vdec_enum_fmt, 597 .vidioc_s_fmt_vid_cap_mplane = vdec_s_fmt, 598 .vidioc_s_fmt_vid_out_mplane = vdec_s_fmt, 599 .vidioc_g_fmt_vid_cap_mplane = vdec_g_fmt, 600 .vidioc_g_fmt_vid_out_mplane = vdec_g_fmt, 601 .vidioc_try_fmt_vid_cap_mplane = vdec_try_fmt, 602 .vidioc_try_fmt_vid_out_mplane = vdec_try_fmt, 603 .vidioc_g_selection = vdec_g_selection, 604 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs, 605 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf, 606 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs, 607 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf, 608 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf, 609 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf, 610 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf, 611 .vidioc_streamon = v4l2_m2m_ioctl_streamon, 612 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff, 613 .vidioc_s_parm = vdec_s_parm, 614 .vidioc_enum_framesizes = vdec_enum_framesizes, 615 .vidioc_subscribe_event = vdec_subscribe_event, 616 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 617 .vidioc_try_decoder_cmd = v4l2_m2m_ioctl_try_decoder_cmd, 618 .vidioc_decoder_cmd = vdec_decoder_cmd, 619 }; 620 621 static int vdec_pm_get(struct venus_inst *inst) 622 { 623 struct venus_core *core = inst->core; 624 struct device *dev = core->dev_dec; 625 int ret; 626 627 mutex_lock(&core->pm_lock); 628 ret = pm_runtime_resume_and_get(dev); 629 mutex_unlock(&core->pm_lock); 630 631 return ret; 632 } 633 634 static int vdec_pm_put(struct venus_inst *inst, bool autosuspend) 635 { 636 struct venus_core *core = inst->core; 637 struct device *dev = core->dev_dec; 638 int ret; 639 640 mutex_lock(&core->pm_lock); 641 642 if (autosuspend) 643 ret = pm_runtime_put_autosuspend(dev); 644 else 645 ret = pm_runtime_put_sync(dev); 646 647 mutex_unlock(&core->pm_lock); 648 649 return ret < 0 ? ret : 0; 650 } 651 652 static int vdec_pm_get_put(struct venus_inst *inst) 653 { 654 struct venus_core *core = inst->core; 655 struct device *dev = core->dev_dec; 656 int ret = 0; 657 658 mutex_lock(&core->pm_lock); 659 660 if (pm_runtime_suspended(dev)) { 661 ret = pm_runtime_resume_and_get(dev); 662 if (ret < 0) 663 goto error; 664 665 ret = pm_runtime_put_autosuspend(dev); 666 } 667 668 error: 669 mutex_unlock(&core->pm_lock); 670 671 return ret < 0 ? ret : 0; 672 } 673 674 static void vdec_pm_touch(struct venus_inst *inst) 675 { 676 pm_runtime_mark_last_busy(inst->core->dev_dec); 677 } 678 679 static int vdec_set_properties(struct venus_inst *inst) 680 { 681 struct vdec_controls *ctr = &inst->controls.dec; 682 struct hfi_enable en = { .enable = 1 }; 683 u32 ptype, decode_order, conceal; 684 int ret; 685 686 if (ctr->post_loop_deb_mode) { 687 ptype = HFI_PROPERTY_CONFIG_VDEC_POST_LOOP_DEBLOCKER; 688 ret = hfi_session_set_property(inst, ptype, &en); 689 if (ret) 690 return ret; 691 } 692 693 if (ctr->display_delay_enable && ctr->display_delay == 0) { 694 ptype = HFI_PROPERTY_PARAM_VDEC_OUTPUT_ORDER; 695 decode_order = HFI_OUTPUT_ORDER_DECODE; 696 ret = hfi_session_set_property(inst, ptype, &decode_order); 697 if (ret) 698 return ret; 699 } 700 701 /* Enabling sufficient sequence change support for VP9 */ 702 if (is_fw_rev_or_newer(inst->core, 5, 4, 51)) { 703 ptype = HFI_PROPERTY_PARAM_VDEC_ENABLE_SUFFICIENT_SEQCHANGE_EVENT; 704 ret = hfi_session_set_property(inst, ptype, &en); 705 if (ret) 706 return ret; 707 } 708 709 ptype = HFI_PROPERTY_PARAM_VDEC_CONCEAL_COLOR; 710 conceal = ctr->conceal_color & 0xffff; 711 conceal |= ((ctr->conceal_color >> 16) & 0xffff) << 10; 712 conceal |= ((ctr->conceal_color >> 32) & 0xffff) << 20; 713 714 ret = hfi_session_set_property(inst, ptype, &conceal); 715 if (ret) 716 return ret; 717 718 return 0; 719 } 720 721 static int vdec_set_work_route(struct venus_inst *inst) 722 { 723 u32 ptype = HFI_PROPERTY_PARAM_WORK_ROUTE; 724 struct hfi_video_work_route wr; 725 726 if (!(IS_IRIS2(inst->core) || IS_IRIS2_1(inst->core))) 727 return 0; 728 729 wr.video_work_route = inst->core->res->num_vpp_pipes; 730 731 return hfi_session_set_property(inst, ptype, &wr); 732 } 733 734 #define is_ubwc_fmt(fmt) (!!((fmt) & HFI_COLOR_FORMAT_UBWC_BASE)) 735 #define is_10bit_ubwc_fmt(fmt) (!!((fmt) & HFI_COLOR_FORMAT_10_BIT_BASE & \ 736 HFI_COLOR_FORMAT_UBWC_BASE)) 737 738 739 static int vdec_output_conf(struct venus_inst *inst) 740 { 741 struct venus_core *core = inst->core; 742 struct hfi_enable en = { .enable = 1 }; 743 struct hfi_buffer_requirements bufreq; 744 u32 width = inst->width; 745 u32 height = inst->height; 746 u32 out_fmt, out2_fmt; 747 bool ubwc = false; 748 u32 ptype; 749 int ret; 750 751 ret = venus_helper_set_work_mode(inst); 752 if (ret) 753 return ret; 754 755 if (core->res->hfi_version == HFI_VERSION_1XX) { 756 ptype = HFI_PROPERTY_PARAM_VDEC_CONTINUE_DATA_TRANSFER; 757 ret = hfi_session_set_property(inst, ptype, &en); 758 if (ret) 759 return ret; 760 } 761 762 /* Force searching UBWC formats for bigger then HD resolutions */ 763 if (width > 1920 && height > ALIGN(1080, 32)) 764 ubwc = true; 765 766 /* For Venus v4/v6 UBWC format is mandatory */ 767 if (IS_V4(core) || IS_V6(core)) 768 ubwc = true; 769 770 ret = venus_helper_get_out_fmts(inst, inst->fmt_cap->pixfmt, &out_fmt, 771 &out2_fmt, ubwc); 772 if (ret) 773 return ret; 774 775 inst->output_buf_size = 776 venus_helper_get_framesz_raw(out_fmt, width, height); 777 inst->output2_buf_size = 778 venus_helper_get_framesz_raw(out2_fmt, width, height); 779 780 if (is_ubwc_fmt(out_fmt)) { 781 inst->opb_buftype = HFI_BUFFER_OUTPUT2; 782 inst->opb_fmt = out2_fmt; 783 inst->dpb_buftype = HFI_BUFFER_OUTPUT; 784 inst->dpb_fmt = out_fmt; 785 } else if (is_ubwc_fmt(out2_fmt) || is_10bit_ubwc_fmt(out_fmt)) { 786 inst->opb_buftype = HFI_BUFFER_OUTPUT; 787 inst->opb_fmt = out_fmt; 788 inst->dpb_buftype = HFI_BUFFER_OUTPUT2; 789 inst->dpb_fmt = out2_fmt; 790 } else { 791 inst->opb_buftype = HFI_BUFFER_OUTPUT; 792 inst->opb_fmt = out_fmt; 793 inst->dpb_buftype = 0; 794 inst->dpb_fmt = 0; 795 } 796 797 ret = venus_helper_set_raw_format(inst, inst->opb_fmt, 798 inst->opb_buftype); 799 if (ret) 800 return ret; 801 802 ret = venus_helper_set_format_constraints(inst); 803 if (ret) 804 return ret; 805 806 if (inst->dpb_fmt) { 807 ret = venus_helper_set_multistream(inst, false, true); 808 if (ret) 809 return ret; 810 811 ret = venus_helper_set_raw_format(inst, inst->dpb_fmt, 812 inst->dpb_buftype); 813 if (ret) 814 return ret; 815 816 ret = venus_helper_set_output_resolution(inst, width, height, 817 HFI_BUFFER_OUTPUT2); 818 if (ret) 819 return ret; 820 } 821 822 if (IS_V3(core) || IS_V4(core) || IS_V6(core)) { 823 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq); 824 if (ret) 825 return ret; 826 827 if (bufreq.size > inst->output_buf_size) 828 return -EINVAL; 829 830 if (inst->dpb_fmt) { 831 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT2, 832 &bufreq); 833 if (ret) 834 return ret; 835 836 if (bufreq.size > inst->output2_buf_size) 837 return -EINVAL; 838 } 839 840 if (inst->output2_buf_size) { 841 ret = venus_helper_set_bufsize(inst, 842 inst->output2_buf_size, 843 HFI_BUFFER_OUTPUT2); 844 if (ret) 845 return ret; 846 } 847 848 if (inst->output_buf_size) { 849 ret = venus_helper_set_bufsize(inst, 850 inst->output_buf_size, 851 HFI_BUFFER_OUTPUT); 852 if (ret) 853 return ret; 854 } 855 } 856 857 ret = venus_helper_set_dyn_bufmode(inst); 858 if (ret) 859 return ret; 860 861 return 0; 862 } 863 864 static int vdec_session_init(struct venus_inst *inst) 865 { 866 int ret; 867 868 ret = venus_helper_session_init(inst); 869 if (ret == -EALREADY) 870 return 0; 871 else if (ret) 872 return ret; 873 874 ret = venus_helper_set_input_resolution(inst, frame_width_min(inst), 875 frame_height_min(inst)); 876 if (ret) 877 goto deinit; 878 879 return 0; 880 deinit: 881 hfi_session_deinit(inst); 882 return ret; 883 } 884 885 static int vdec_num_buffers(struct venus_inst *inst, unsigned int *in_num, 886 unsigned int *out_num) 887 { 888 enum hfi_version ver = inst->core->res->hfi_version; 889 struct hfi_buffer_requirements bufreq; 890 int ret; 891 892 *in_num = *out_num = 0; 893 894 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq); 895 if (ret) 896 return ret; 897 898 *in_num = hfi_bufreq_get_count_min(&bufreq, ver); 899 900 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq); 901 if (ret) 902 return ret; 903 904 *out_num = hfi_bufreq_get_count_min(&bufreq, ver); 905 906 return 0; 907 } 908 909 static int vdec_queue_setup(struct vb2_queue *q, 910 unsigned int *num_buffers, unsigned int *num_planes, 911 unsigned int sizes[], struct device *alloc_devs[]) 912 { 913 struct venus_inst *inst = vb2_get_drv_priv(q); 914 struct venus_core *core = inst->core; 915 unsigned int in_num, out_num; 916 int ret = 0; 917 918 if (*num_planes) { 919 unsigned int output_buf_size = venus_helper_get_opb_size(inst); 920 921 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE && 922 *num_planes != inst->fmt_out->num_planes) 923 return -EINVAL; 924 925 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE && 926 *num_planes != inst->fmt_cap->num_planes) 927 return -EINVAL; 928 929 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE && 930 sizes[0] < inst->input_buf_size) 931 return -EINVAL; 932 933 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE && 934 sizes[0] < output_buf_size) 935 return -EINVAL; 936 937 return 0; 938 } 939 940 if (test_bit(0, &core->sys_error)) { 941 if (inst->nonblock) 942 return -EAGAIN; 943 944 ret = wait_event_interruptible(core->sys_err_done, 945 !test_bit(0, &core->sys_error)); 946 if (ret) 947 return ret; 948 } 949 950 ret = vdec_pm_get(inst); 951 if (ret) 952 return ret; 953 954 ret = vdec_session_init(inst); 955 if (ret) 956 goto put_power; 957 958 ret = vdec_num_buffers(inst, &in_num, &out_num); 959 if (ret) 960 goto put_power; 961 962 ret = vdec_pm_put(inst, false); 963 if (ret) 964 return ret; 965 966 switch (q->type) { 967 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE: 968 *num_planes = inst->fmt_out->num_planes; 969 sizes[0] = venus_helper_get_framesz(inst->fmt_out->pixfmt, 970 inst->out_width, 971 inst->out_height); 972 sizes[0] = max(sizes[0], inst->input_buf_size); 973 inst->input_buf_size = sizes[0]; 974 *num_buffers = max(*num_buffers, in_num); 975 inst->num_input_bufs = *num_buffers; 976 inst->num_output_bufs = out_num; 977 break; 978 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE: 979 *num_planes = inst->fmt_cap->num_planes; 980 sizes[0] = venus_helper_get_framesz(inst->fmt_cap->pixfmt, 981 inst->width, 982 inst->height); 983 inst->output_buf_size = sizes[0]; 984 *num_buffers = max(*num_buffers, out_num); 985 inst->num_output_bufs = *num_buffers; 986 987 mutex_lock(&inst->lock); 988 if (inst->codec_state == VENUS_DEC_STATE_CAPTURE_SETUP) 989 inst->codec_state = VENUS_DEC_STATE_STOPPED; 990 mutex_unlock(&inst->lock); 991 break; 992 default: 993 ret = -EINVAL; 994 break; 995 } 996 997 return ret; 998 999 put_power: 1000 vdec_pm_put(inst, false); 1001 return ret; 1002 } 1003 1004 static int vdec_verify_conf(struct venus_inst *inst) 1005 { 1006 enum hfi_version ver = inst->core->res->hfi_version; 1007 struct hfi_buffer_requirements bufreq; 1008 int ret; 1009 1010 if (!inst->num_input_bufs || !inst->num_output_bufs) 1011 return -EINVAL; 1012 1013 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq); 1014 if (ret) 1015 return ret; 1016 1017 if (inst->num_output_bufs < bufreq.count_actual || 1018 inst->num_output_bufs < hfi_bufreq_get_count_min(&bufreq, ver)) 1019 return -EINVAL; 1020 1021 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq); 1022 if (ret) 1023 return ret; 1024 1025 if (inst->num_input_bufs < hfi_bufreq_get_count_min(&bufreq, ver)) 1026 return -EINVAL; 1027 1028 return 0; 1029 } 1030 1031 static int vdec_start_capture(struct venus_inst *inst) 1032 { 1033 int ret; 1034 1035 if (!inst->streamon_out) 1036 return 0; 1037 1038 if (inst->codec_state == VENUS_DEC_STATE_DECODING) { 1039 if (inst->reconfig) 1040 goto reconfigure; 1041 1042 venus_helper_queue_dpb_bufs(inst); 1043 venus_helper_process_initial_cap_bufs(inst); 1044 inst->streamon_cap = 1; 1045 return 0; 1046 } 1047 1048 if (inst->codec_state != VENUS_DEC_STATE_STOPPED) 1049 return 0; 1050 1051 reconfigure: 1052 ret = vdec_output_conf(inst); 1053 if (ret) 1054 return ret; 1055 1056 ret = venus_helper_set_num_bufs(inst, inst->num_input_bufs, 1057 VB2_MAX_FRAME, VB2_MAX_FRAME); 1058 if (ret) 1059 return ret; 1060 1061 ret = venus_helper_intbufs_realloc(inst); 1062 if (ret) 1063 goto err; 1064 1065 venus_pm_load_scale(inst); 1066 1067 inst->next_buf_last = false; 1068 1069 ret = venus_helper_alloc_dpb_bufs(inst); 1070 if (ret) 1071 goto err; 1072 1073 ret = hfi_session_continue(inst); 1074 if (ret) 1075 goto free_dpb_bufs; 1076 1077 ret = venus_helper_queue_dpb_bufs(inst); 1078 if (ret) 1079 goto free_dpb_bufs; 1080 1081 ret = venus_helper_process_initial_cap_bufs(inst); 1082 if (ret) 1083 goto free_dpb_bufs; 1084 1085 inst->codec_state = VENUS_DEC_STATE_DECODING; 1086 1087 if (inst->drain_active) 1088 inst->codec_state = VENUS_DEC_STATE_DRAIN; 1089 1090 inst->streamon_cap = 1; 1091 inst->sequence_cap = 0; 1092 inst->reconfig = false; 1093 inst->drain_active = false; 1094 1095 return 0; 1096 1097 free_dpb_bufs: 1098 venus_helper_free_dpb_bufs(inst); 1099 err: 1100 return ret; 1101 } 1102 1103 static int vdec_start_output(struct venus_inst *inst) 1104 { 1105 int ret; 1106 1107 if (inst->codec_state == VENUS_DEC_STATE_SEEK) { 1108 ret = venus_helper_process_initial_out_bufs(inst); 1109 if (ret) 1110 return ret; 1111 1112 if (inst->next_buf_last) { 1113 inst->codec_state = VENUS_DEC_STATE_DRC; 1114 } else { 1115 inst->codec_state = VENUS_DEC_STATE_DECODING; 1116 1117 if (inst->streamon_cap) { 1118 ret = venus_helper_queue_dpb_bufs(inst); 1119 if (ret) 1120 return ret; 1121 } 1122 } 1123 goto done; 1124 } 1125 1126 if (inst->codec_state == VENUS_DEC_STATE_INIT || 1127 inst->codec_state == VENUS_DEC_STATE_CAPTURE_SETUP) { 1128 ret = venus_helper_process_initial_out_bufs(inst); 1129 goto done; 1130 } 1131 1132 if (inst->codec_state != VENUS_DEC_STATE_DEINIT) 1133 return -EINVAL; 1134 1135 venus_helper_init_instance(inst); 1136 inst->sequence_out = 0; 1137 inst->reconfig = false; 1138 inst->next_buf_last = false; 1139 1140 ret = vdec_set_properties(inst); 1141 if (ret) 1142 return ret; 1143 1144 ret = vdec_set_work_route(inst); 1145 if (ret) 1146 return ret; 1147 1148 ret = vdec_output_conf(inst); 1149 if (ret) 1150 return ret; 1151 1152 ret = vdec_verify_conf(inst); 1153 if (ret) 1154 return ret; 1155 1156 ret = venus_helper_set_num_bufs(inst, inst->num_input_bufs, 1157 VB2_MAX_FRAME, VB2_MAX_FRAME); 1158 if (ret) 1159 return ret; 1160 1161 ret = venus_helper_vb2_start_streaming(inst); 1162 if (ret) 1163 return ret; 1164 1165 ret = venus_helper_process_initial_out_bufs(inst); 1166 if (ret) 1167 return ret; 1168 1169 inst->codec_state = VENUS_DEC_STATE_INIT; 1170 1171 done: 1172 inst->streamon_out = 1; 1173 return ret; 1174 } 1175 1176 static int vdec_start_streaming(struct vb2_queue *q, unsigned int count) 1177 { 1178 struct venus_inst *inst = vb2_get_drv_priv(q); 1179 int ret; 1180 1181 mutex_lock(&inst->lock); 1182 1183 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) { 1184 ret = vdec_start_capture(inst); 1185 } else { 1186 ret = vdec_pm_get(inst); 1187 if (ret) 1188 goto error; 1189 1190 ret = venus_pm_acquire_core(inst); 1191 if (ret) 1192 goto put_power; 1193 1194 ret = vdec_pm_put(inst, true); 1195 if (ret) 1196 goto error; 1197 1198 ret = vdec_start_output(inst); 1199 } 1200 1201 if (ret) 1202 goto error; 1203 1204 mutex_unlock(&inst->lock); 1205 return 0; 1206 1207 put_power: 1208 vdec_pm_put(inst, false); 1209 error: 1210 venus_helper_buffers_done(inst, q->type, VB2_BUF_STATE_QUEUED); 1211 mutex_unlock(&inst->lock); 1212 return ret; 1213 } 1214 1215 static void vdec_cancel_dst_buffers(struct venus_inst *inst) 1216 { 1217 struct vb2_v4l2_buffer *buf; 1218 1219 while ((buf = v4l2_m2m_dst_buf_remove(inst->m2m_ctx))) 1220 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR); 1221 } 1222 1223 static int vdec_stop_capture(struct venus_inst *inst) 1224 { 1225 int ret = 0; 1226 1227 switch (inst->codec_state) { 1228 case VENUS_DEC_STATE_DECODING: 1229 ret = hfi_session_flush(inst, HFI_FLUSH_ALL, true); 1230 fallthrough; 1231 case VENUS_DEC_STATE_DRAIN: 1232 inst->codec_state = VENUS_DEC_STATE_STOPPED; 1233 inst->drain_active = false; 1234 fallthrough; 1235 case VENUS_DEC_STATE_SEEK: 1236 vdec_cancel_dst_buffers(inst); 1237 break; 1238 case VENUS_DEC_STATE_DRC: 1239 ret = hfi_session_flush(inst, HFI_FLUSH_OUTPUT, true); 1240 inst->codec_state = VENUS_DEC_STATE_CAPTURE_SETUP; 1241 venus_helper_free_dpb_bufs(inst); 1242 break; 1243 default: 1244 break; 1245 } 1246 1247 return ret; 1248 } 1249 1250 static int vdec_stop_output(struct venus_inst *inst) 1251 { 1252 int ret = 0; 1253 1254 switch (inst->codec_state) { 1255 case VENUS_DEC_STATE_DECODING: 1256 case VENUS_DEC_STATE_DRAIN: 1257 case VENUS_DEC_STATE_STOPPED: 1258 case VENUS_DEC_STATE_DRC: 1259 ret = hfi_session_flush(inst, HFI_FLUSH_ALL, true); 1260 inst->codec_state = VENUS_DEC_STATE_SEEK; 1261 break; 1262 case VENUS_DEC_STATE_INIT: 1263 case VENUS_DEC_STATE_CAPTURE_SETUP: 1264 ret = hfi_session_flush(inst, HFI_FLUSH_ALL, true); 1265 break; 1266 default: 1267 break; 1268 } 1269 1270 return ret; 1271 } 1272 1273 static void vdec_stop_streaming(struct vb2_queue *q) 1274 { 1275 struct venus_inst *inst = vb2_get_drv_priv(q); 1276 int ret = -EINVAL; 1277 1278 vdec_pm_get_put(inst); 1279 1280 mutex_lock(&inst->lock); 1281 1282 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) 1283 ret = vdec_stop_capture(inst); 1284 else 1285 ret = vdec_stop_output(inst); 1286 1287 venus_helper_buffers_done(inst, q->type, VB2_BUF_STATE_ERROR); 1288 1289 inst->session_error = 0; 1290 1291 if (ret) 1292 goto unlock; 1293 1294 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) 1295 inst->streamon_out = 0; 1296 else 1297 inst->streamon_cap = 0; 1298 1299 unlock: 1300 mutex_unlock(&inst->lock); 1301 } 1302 1303 static void vdec_session_release(struct venus_inst *inst) 1304 { 1305 struct venus_core *core = inst->core; 1306 int ret, abort = 0; 1307 1308 vdec_pm_get(inst); 1309 1310 mutex_lock(&inst->lock); 1311 inst->codec_state = VENUS_DEC_STATE_DEINIT; 1312 1313 ret = hfi_session_stop(inst); 1314 abort = (ret && ret != -EINVAL) ? 1 : 0; 1315 ret = hfi_session_unload_res(inst); 1316 abort = (ret && ret != -EINVAL) ? 1 : 0; 1317 ret = venus_helper_unregister_bufs(inst); 1318 abort = (ret && ret != -EINVAL) ? 1 : 0; 1319 ret = venus_helper_intbufs_free(inst); 1320 abort = (ret && ret != -EINVAL) ? 1 : 0; 1321 ret = hfi_session_deinit(inst); 1322 abort = (ret && ret != -EINVAL) ? 1 : 0; 1323 1324 if (inst->session_error || test_bit(0, &core->sys_error)) 1325 abort = 1; 1326 1327 if (abort) 1328 hfi_session_abort(inst); 1329 1330 venus_helper_free_dpb_bufs(inst); 1331 venus_pm_load_scale(inst); 1332 INIT_LIST_HEAD(&inst->registeredbufs); 1333 mutex_unlock(&inst->lock); 1334 1335 venus_pm_release_core(inst); 1336 vdec_pm_put(inst, false); 1337 } 1338 1339 static int vdec_buf_init(struct vb2_buffer *vb) 1340 { 1341 struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue); 1342 1343 inst->buf_count++; 1344 1345 return venus_helper_vb2_buf_init(vb); 1346 } 1347 1348 static void vdec_buf_cleanup(struct vb2_buffer *vb) 1349 { 1350 struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue); 1351 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 1352 struct venus_buffer *buf = to_venus_buffer(vbuf); 1353 1354 mutex_lock(&inst->lock); 1355 if (vb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) 1356 if (!list_empty(&inst->registeredbufs)) 1357 list_del_init(&buf->reg_list); 1358 mutex_unlock(&inst->lock); 1359 1360 inst->buf_count--; 1361 if (!inst->buf_count) 1362 vdec_session_release(inst); 1363 } 1364 1365 static void vdec_vb2_buf_queue(struct vb2_buffer *vb) 1366 { 1367 struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue); 1368 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 1369 static const struct v4l2_event eos = { .type = V4L2_EVENT_EOS }; 1370 1371 vdec_pm_get_put(inst); 1372 1373 mutex_lock(&inst->lock); 1374 1375 if (inst->next_buf_last && V4L2_TYPE_IS_CAPTURE(vb->vb2_queue->type) && 1376 inst->codec_state == VENUS_DEC_STATE_DRC) { 1377 vbuf->flags |= V4L2_BUF_FLAG_LAST; 1378 vbuf->sequence = inst->sequence_cap++; 1379 vbuf->field = V4L2_FIELD_NONE; 1380 vb2_set_plane_payload(vb, 0, 0); 1381 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_DONE); 1382 v4l2_event_queue_fh(&inst->fh, &eos); 1383 inst->next_buf_last = false; 1384 mutex_unlock(&inst->lock); 1385 return; 1386 } 1387 1388 venus_helper_vb2_buf_queue(vb); 1389 mutex_unlock(&inst->lock); 1390 } 1391 1392 static const struct vb2_ops vdec_vb2_ops = { 1393 .queue_setup = vdec_queue_setup, 1394 .buf_init = vdec_buf_init, 1395 .buf_cleanup = vdec_buf_cleanup, 1396 .buf_prepare = venus_helper_vb2_buf_prepare, 1397 .start_streaming = vdec_start_streaming, 1398 .stop_streaming = vdec_stop_streaming, 1399 .buf_queue = vdec_vb2_buf_queue, 1400 }; 1401 1402 static void vdec_buf_done(struct venus_inst *inst, unsigned int buf_type, 1403 u32 tag, u32 bytesused, u32 data_offset, u32 flags, 1404 u32 hfi_flags, u64 timestamp_us) 1405 { 1406 enum vb2_buffer_state state = VB2_BUF_STATE_DONE; 1407 struct vb2_v4l2_buffer *vbuf; 1408 struct vb2_buffer *vb; 1409 unsigned int type; 1410 1411 vdec_pm_touch(inst); 1412 1413 if (buf_type == HFI_BUFFER_INPUT) 1414 type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; 1415 else 1416 type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; 1417 1418 vbuf = venus_helper_find_buf(inst, type, tag); 1419 if (!vbuf) { 1420 venus_helper_change_dpb_owner(inst, vbuf, type, buf_type, tag); 1421 return; 1422 } 1423 1424 vbuf->flags = flags; 1425 vbuf->field = V4L2_FIELD_NONE; 1426 vb = &vbuf->vb2_buf; 1427 1428 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) { 1429 vb2_set_plane_payload(vb, 0, bytesused); 1430 vb->planes[0].data_offset = data_offset; 1431 vb->timestamp = timestamp_us * NSEC_PER_USEC; 1432 vbuf->sequence = inst->sequence_cap++; 1433 1434 if (vbuf->flags & V4L2_BUF_FLAG_LAST) { 1435 const struct v4l2_event ev = { .type = V4L2_EVENT_EOS }; 1436 1437 v4l2_event_queue_fh(&inst->fh, &ev); 1438 1439 if (inst->codec_state == VENUS_DEC_STATE_DRAIN) { 1440 inst->drain_active = false; 1441 inst->codec_state = VENUS_DEC_STATE_STOPPED; 1442 } 1443 } 1444 1445 if (!bytesused) 1446 state = VB2_BUF_STATE_ERROR; 1447 } else { 1448 vbuf->sequence = inst->sequence_out++; 1449 } 1450 1451 venus_helper_get_ts_metadata(inst, timestamp_us, vbuf); 1452 1453 if (hfi_flags & HFI_BUFFERFLAG_READONLY) 1454 venus_helper_acquire_buf_ref(vbuf); 1455 1456 if (hfi_flags & HFI_BUFFERFLAG_DATACORRUPT) 1457 state = VB2_BUF_STATE_ERROR; 1458 1459 if (hfi_flags & HFI_BUFFERFLAG_DROP_FRAME) { 1460 state = VB2_BUF_STATE_ERROR; 1461 vb2_set_plane_payload(vb, 0, 0); 1462 vb->timestamp = 0; 1463 } 1464 1465 v4l2_m2m_buf_done(vbuf, state); 1466 } 1467 1468 static void vdec_event_change(struct venus_inst *inst, 1469 struct hfi_event_data *ev_data, bool sufficient) 1470 { 1471 static const struct v4l2_event ev = { 1472 .type = V4L2_EVENT_SOURCE_CHANGE, 1473 .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION }; 1474 struct device *dev = inst->core->dev_dec; 1475 struct v4l2_format format = {}; 1476 1477 mutex_lock(&inst->lock); 1478 1479 format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; 1480 format.fmt.pix_mp.pixelformat = inst->fmt_cap->pixfmt; 1481 format.fmt.pix_mp.width = ev_data->width; 1482 format.fmt.pix_mp.height = ev_data->height; 1483 1484 vdec_try_fmt_common(inst, &format); 1485 1486 inst->width = format.fmt.pix_mp.width; 1487 inst->height = format.fmt.pix_mp.height; 1488 /* 1489 * Some versions of the firmware do not report crop information for 1490 * all codecs. For these cases, set the crop to the coded resolution. 1491 */ 1492 if (ev_data->input_crop.width > 0 && ev_data->input_crop.height > 0) { 1493 inst->crop.left = ev_data->input_crop.left; 1494 inst->crop.top = ev_data->input_crop.top; 1495 inst->crop.width = ev_data->input_crop.width; 1496 inst->crop.height = ev_data->input_crop.height; 1497 } else { 1498 inst->crop.left = 0; 1499 inst->crop.top = 0; 1500 inst->crop.width = ev_data->width; 1501 inst->crop.height = ev_data->height; 1502 } 1503 1504 inst->fw_min_cnt = ev_data->buf_count; 1505 /* overwriting this to 11 for vp9 due to fw bug */ 1506 if (inst->hfi_codec == HFI_VIDEO_CODEC_VP9) 1507 inst->fw_min_cnt = 11; 1508 1509 inst->out_width = ev_data->width; 1510 inst->out_height = ev_data->height; 1511 1512 if (inst->bit_depth != ev_data->bit_depth) { 1513 inst->bit_depth = ev_data->bit_depth; 1514 if (inst->bit_depth == VIDC_BITDEPTH_10) 1515 inst->fmt_cap = &vdec_formats[VENUS_FMT_P010]; 1516 else 1517 inst->fmt_cap = &vdec_formats[VENUS_FMT_NV12]; 1518 } 1519 1520 if (inst->pic_struct != ev_data->pic_struct) 1521 inst->pic_struct = ev_data->pic_struct; 1522 1523 dev_dbg(dev, VDBGM "event %s sufficient resources (%ux%u)\n", 1524 sufficient ? "" : "not", ev_data->width, ev_data->height); 1525 1526 switch (inst->codec_state) { 1527 case VENUS_DEC_STATE_INIT: 1528 inst->codec_state = VENUS_DEC_STATE_CAPTURE_SETUP; 1529 break; 1530 case VENUS_DEC_STATE_DECODING: 1531 case VENUS_DEC_STATE_DRAIN: 1532 inst->codec_state = VENUS_DEC_STATE_DRC; 1533 break; 1534 default: 1535 break; 1536 } 1537 1538 /* 1539 * The assumption is that the firmware have to return the last buffer 1540 * before this event is received in the v4l2 driver. Also the firmware 1541 * itself doesn't mark the last decoder output buffer with HFI EOS flag. 1542 */ 1543 1544 if (inst->codec_state == VENUS_DEC_STATE_DRC) { 1545 int ret; 1546 1547 inst->next_buf_last = true; 1548 1549 ret = hfi_session_flush(inst, HFI_FLUSH_OUTPUT, false); 1550 if (ret) 1551 dev_dbg(dev, VDBGH "flush output error %d\n", ret); 1552 } 1553 1554 inst->next_buf_last = true; 1555 inst->reconfig = true; 1556 v4l2_event_queue_fh(&inst->fh, &ev); 1557 wake_up(&inst->reconf_wait); 1558 1559 mutex_unlock(&inst->lock); 1560 } 1561 1562 static void vdec_event_notify(struct venus_inst *inst, u32 event, 1563 struct hfi_event_data *data) 1564 { 1565 struct venus_core *core = inst->core; 1566 struct device *dev = core->dev_dec; 1567 1568 vdec_pm_touch(inst); 1569 1570 switch (event) { 1571 case EVT_SESSION_ERROR: 1572 inst->session_error = true; 1573 venus_helper_vb2_queue_error(inst); 1574 dev_err(dev, "dec: event session error %x\n", inst->error); 1575 break; 1576 case EVT_SYS_EVENT_CHANGE: 1577 switch (data->event_type) { 1578 case HFI_EVENT_DATA_SEQUENCE_CHANGED_SUFFICIENT_BUF_RESOURCES: 1579 vdec_event_change(inst, data, true); 1580 break; 1581 case HFI_EVENT_DATA_SEQUENCE_CHANGED_INSUFFICIENT_BUF_RESOURCES: 1582 vdec_event_change(inst, data, false); 1583 break; 1584 case HFI_EVENT_RELEASE_BUFFER_REFERENCE: 1585 venus_helper_release_buf_ref(inst, data->tag); 1586 break; 1587 default: 1588 break; 1589 } 1590 break; 1591 default: 1592 break; 1593 } 1594 } 1595 1596 static void vdec_flush_done(struct venus_inst *inst) 1597 { 1598 dev_dbg(inst->core->dev_dec, VDBGH "flush done\n"); 1599 } 1600 1601 static const struct hfi_inst_ops vdec_hfi_ops = { 1602 .buf_done = vdec_buf_done, 1603 .event_notify = vdec_event_notify, 1604 .flush_done = vdec_flush_done, 1605 }; 1606 1607 static void vdec_inst_init(struct venus_inst *inst) 1608 { 1609 inst->hfi_codec = HFI_VIDEO_CODEC_H264; 1610 inst->fmt_out = &vdec_formats[VENUS_FMT_H264]; 1611 inst->fmt_cap = &vdec_formats[VENUS_FMT_NV12]; 1612 inst->width = frame_width_min(inst); 1613 inst->height = ALIGN(frame_height_min(inst), 32); 1614 inst->crop.left = 0; 1615 inst->crop.top = 0; 1616 inst->crop.width = inst->width; 1617 inst->crop.height = inst->height; 1618 inst->fw_min_cnt = 8; 1619 inst->out_width = frame_width_min(inst); 1620 inst->out_height = frame_height_min(inst); 1621 inst->fps = 30; 1622 inst->timeperframe.numerator = 1; 1623 inst->timeperframe.denominator = 30; 1624 inst->opb_buftype = HFI_BUFFER_OUTPUT; 1625 } 1626 1627 static void vdec_m2m_device_run(void *priv) 1628 { 1629 } 1630 1631 static const struct v4l2_m2m_ops vdec_m2m_ops = { 1632 .device_run = vdec_m2m_device_run, 1633 .job_abort = venus_helper_m2m_job_abort, 1634 }; 1635 1636 static int m2m_queue_init(void *priv, struct vb2_queue *src_vq, 1637 struct vb2_queue *dst_vq) 1638 { 1639 struct venus_inst *inst = priv; 1640 int ret; 1641 1642 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; 1643 src_vq->io_modes = VB2_MMAP | VB2_DMABUF; 1644 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 1645 src_vq->ops = &vdec_vb2_ops; 1646 src_vq->mem_ops = &vb2_dma_contig_memops; 1647 src_vq->drv_priv = inst; 1648 src_vq->buf_struct_size = sizeof(struct venus_buffer); 1649 src_vq->allow_zero_bytesused = 1; 1650 src_vq->min_queued_buffers = 0; 1651 src_vq->dev = inst->core->dev; 1652 src_vq->lock = &inst->ctx_q_lock; 1653 ret = vb2_queue_init(src_vq); 1654 if (ret) 1655 return ret; 1656 1657 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; 1658 dst_vq->io_modes = VB2_MMAP | VB2_DMABUF; 1659 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 1660 dst_vq->ops = &vdec_vb2_ops; 1661 dst_vq->mem_ops = &vb2_dma_contig_memops; 1662 dst_vq->drv_priv = inst; 1663 dst_vq->buf_struct_size = sizeof(struct venus_buffer); 1664 dst_vq->allow_zero_bytesused = 1; 1665 dst_vq->min_queued_buffers = 0; 1666 dst_vq->dev = inst->core->dev; 1667 dst_vq->lock = &inst->ctx_q_lock; 1668 return vb2_queue_init(dst_vq); 1669 } 1670 1671 static int vdec_open(struct file *file) 1672 { 1673 struct venus_core *core = video_drvdata(file); 1674 struct venus_inst *inst; 1675 int ret; 1676 1677 inst = kzalloc(sizeof(*inst), GFP_KERNEL); 1678 if (!inst) 1679 return -ENOMEM; 1680 1681 INIT_LIST_HEAD(&inst->dpbbufs); 1682 INIT_LIST_HEAD(&inst->registeredbufs); 1683 INIT_LIST_HEAD(&inst->internalbufs); 1684 INIT_LIST_HEAD(&inst->list); 1685 mutex_init(&inst->lock); 1686 mutex_init(&inst->ctx_q_lock); 1687 1688 inst->core = core; 1689 inst->session_type = VIDC_SESSION_TYPE_DEC; 1690 inst->num_output_bufs = 1; 1691 inst->codec_state = VENUS_DEC_STATE_DEINIT; 1692 inst->buf_count = 0; 1693 inst->clk_data.core_id = VIDC_CORE_ID_DEFAULT; 1694 inst->core_acquired = false; 1695 inst->bit_depth = VIDC_BITDEPTH_8; 1696 inst->pic_struct = HFI_INTERLACE_FRAME_PROGRESSIVE; 1697 init_waitqueue_head(&inst->reconf_wait); 1698 inst->nonblock = file->f_flags & O_NONBLOCK; 1699 1700 venus_helper_init_instance(inst); 1701 1702 ret = vdec_ctrl_init(inst); 1703 if (ret) 1704 goto err_free; 1705 1706 vdec_inst_init(inst); 1707 1708 ida_init(&inst->dpb_ids); 1709 1710 /* 1711 * create m2m device for every instance, the m2m context scheduling 1712 * is made by firmware side so we do not need to care about. 1713 */ 1714 inst->m2m_dev = v4l2_m2m_init(&vdec_m2m_ops); 1715 if (IS_ERR(inst->m2m_dev)) { 1716 ret = PTR_ERR(inst->m2m_dev); 1717 goto err_ctrl_deinit; 1718 } 1719 1720 inst->m2m_ctx = v4l2_m2m_ctx_init(inst->m2m_dev, inst, m2m_queue_init); 1721 if (IS_ERR(inst->m2m_ctx)) { 1722 ret = PTR_ERR(inst->m2m_ctx); 1723 goto err_m2m_dev_release; 1724 } 1725 1726 ret = hfi_session_create(inst, &vdec_hfi_ops); 1727 if (ret) 1728 goto err_m2m_ctx_release; 1729 1730 v4l2_fh_init(&inst->fh, core->vdev_dec); 1731 1732 inst->fh.ctrl_handler = &inst->ctrl_handler; 1733 v4l2_fh_add(&inst->fh, file); 1734 inst->fh.m2m_ctx = inst->m2m_ctx; 1735 1736 return 0; 1737 1738 err_m2m_ctx_release: 1739 v4l2_m2m_ctx_release(inst->m2m_ctx); 1740 err_m2m_dev_release: 1741 v4l2_m2m_release(inst->m2m_dev); 1742 err_ctrl_deinit: 1743 v4l2_ctrl_handler_free(&inst->ctrl_handler); 1744 err_free: 1745 kfree(inst); 1746 return ret; 1747 } 1748 1749 static int vdec_close(struct file *file) 1750 { 1751 struct venus_inst *inst = to_inst(file); 1752 1753 vdec_pm_get(inst); 1754 cancel_work_sync(&inst->delayed_process_work); 1755 venus_close_common(inst, file); 1756 ida_destroy(&inst->dpb_ids); 1757 vdec_pm_put(inst, false); 1758 1759 kfree(inst); 1760 return 0; 1761 } 1762 1763 static const struct v4l2_file_operations vdec_fops = { 1764 .owner = THIS_MODULE, 1765 .open = vdec_open, 1766 .release = vdec_close, 1767 .unlocked_ioctl = video_ioctl2, 1768 .poll = v4l2_m2m_fop_poll, 1769 .mmap = v4l2_m2m_fop_mmap, 1770 }; 1771 1772 static int vdec_probe(struct platform_device *pdev) 1773 { 1774 struct device *dev = &pdev->dev; 1775 struct video_device *vdev; 1776 struct venus_core *core; 1777 int ret; 1778 1779 core = dev_get_drvdata(dev->parent); 1780 if (!core) 1781 return -EINVAL; 1782 1783 platform_set_drvdata(pdev, core); 1784 1785 if (core->pm_ops->vdec_get) { 1786 ret = core->pm_ops->vdec_get(dev); 1787 if (ret) 1788 return ret; 1789 } 1790 1791 vdev = video_device_alloc(); 1792 if (!vdev) 1793 return -ENOMEM; 1794 1795 strscpy(vdev->name, "qcom-venus-decoder", sizeof(vdev->name)); 1796 vdev->release = video_device_release; 1797 vdev->fops = &vdec_fops; 1798 vdev->ioctl_ops = &vdec_ioctl_ops; 1799 vdev->vfl_dir = VFL_DIR_M2M; 1800 vdev->v4l2_dev = &core->v4l2_dev; 1801 vdev->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING; 1802 1803 ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1); 1804 if (ret) 1805 goto err_vdev_release; 1806 1807 core->vdev_dec = vdev; 1808 core->dev_dec = dev; 1809 1810 video_set_drvdata(vdev, core); 1811 pm_runtime_set_autosuspend_delay(dev, 2000); 1812 pm_runtime_use_autosuspend(dev); 1813 pm_runtime_enable(dev); 1814 1815 return 0; 1816 1817 err_vdev_release: 1818 video_device_release(vdev); 1819 return ret; 1820 } 1821 1822 static void vdec_remove(struct platform_device *pdev) 1823 { 1824 struct venus_core *core = dev_get_drvdata(pdev->dev.parent); 1825 1826 video_unregister_device(core->vdev_dec); 1827 pm_runtime_disable(core->dev_dec); 1828 1829 if (core->pm_ops->vdec_put) 1830 core->pm_ops->vdec_put(core->dev_dec); 1831 } 1832 1833 static __maybe_unused int vdec_runtime_suspend(struct device *dev) 1834 { 1835 struct venus_core *core = dev_get_drvdata(dev); 1836 const struct venus_pm_ops *pm_ops = core->pm_ops; 1837 int ret = 0; 1838 1839 if (pm_ops->vdec_power) 1840 ret = pm_ops->vdec_power(dev, POWER_OFF); 1841 1842 return ret; 1843 } 1844 1845 static __maybe_unused int vdec_runtime_resume(struct device *dev) 1846 { 1847 struct venus_core *core = dev_get_drvdata(dev); 1848 const struct venus_pm_ops *pm_ops = core->pm_ops; 1849 int ret = 0; 1850 1851 if (pm_ops->vdec_power) 1852 ret = pm_ops->vdec_power(dev, POWER_ON); 1853 1854 return ret; 1855 } 1856 1857 static const struct dev_pm_ops vdec_pm_ops = { 1858 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, 1859 pm_runtime_force_resume) 1860 SET_RUNTIME_PM_OPS(vdec_runtime_suspend, vdec_runtime_resume, NULL) 1861 }; 1862 1863 static const struct of_device_id vdec_dt_match[] = { 1864 { .compatible = "venus-decoder" }, 1865 { } 1866 }; 1867 MODULE_DEVICE_TABLE(of, vdec_dt_match); 1868 1869 static struct platform_driver qcom_venus_dec_driver = { 1870 .probe = vdec_probe, 1871 .remove = vdec_remove, 1872 .driver = { 1873 .name = "qcom-venus-decoder", 1874 .of_match_table = vdec_dt_match, 1875 .pm = &vdec_pm_ops, 1876 }, 1877 }; 1878 module_platform_driver(qcom_venus_dec_driver); 1879 1880 MODULE_DESCRIPTION("Qualcomm Venus video decoder driver"); 1881 MODULE_LICENSE("GPL v2"); 1882