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