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