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 ret = venus_helper_init_codec_freq_data(inst); 689 if (ret) 690 goto deinit; 691 692 return 0; 693 deinit: 694 hfi_session_deinit(inst); 695 return ret; 696 } 697 698 static int vdec_num_buffers(struct venus_inst *inst, unsigned int *in_num, 699 unsigned int *out_num) 700 { 701 enum hfi_version ver = inst->core->res->hfi_version; 702 struct hfi_buffer_requirements bufreq; 703 int ret; 704 705 *in_num = *out_num = 0; 706 707 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq); 708 if (ret) 709 return ret; 710 711 *in_num = HFI_BUFREQ_COUNT_MIN(&bufreq, ver); 712 713 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq); 714 if (ret) 715 return ret; 716 717 *out_num = HFI_BUFREQ_COUNT_MIN(&bufreq, ver); 718 719 return 0; 720 } 721 722 static int vdec_queue_setup(struct vb2_queue *q, 723 unsigned int *num_buffers, unsigned int *num_planes, 724 unsigned int sizes[], struct device *alloc_devs[]) 725 { 726 struct venus_inst *inst = vb2_get_drv_priv(q); 727 unsigned int in_num, out_num; 728 int ret = 0; 729 730 if (*num_planes) { 731 unsigned int output_buf_size = venus_helper_get_opb_size(inst); 732 733 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE && 734 *num_planes != inst->fmt_out->num_planes) 735 return -EINVAL; 736 737 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE && 738 *num_planes != inst->fmt_cap->num_planes) 739 return -EINVAL; 740 741 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE && 742 sizes[0] < inst->input_buf_size) 743 return -EINVAL; 744 745 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE && 746 sizes[0] < output_buf_size) 747 return -EINVAL; 748 749 return 0; 750 } 751 752 ret = vdec_session_init(inst); 753 if (ret) 754 return ret; 755 756 ret = vdec_num_buffers(inst, &in_num, &out_num); 757 if (ret) 758 return ret; 759 760 switch (q->type) { 761 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE: 762 *num_planes = inst->fmt_out->num_planes; 763 sizes[0] = venus_helper_get_framesz(inst->fmt_out->pixfmt, 764 inst->out_width, 765 inst->out_height); 766 sizes[0] = max(sizes[0], inst->input_buf_size); 767 inst->input_buf_size = sizes[0]; 768 *num_buffers = max(*num_buffers, in_num); 769 inst->num_input_bufs = *num_buffers; 770 inst->num_output_bufs = out_num; 771 break; 772 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE: 773 *num_planes = inst->fmt_cap->num_planes; 774 sizes[0] = venus_helper_get_framesz(inst->fmt_cap->pixfmt, 775 inst->width, 776 inst->height); 777 inst->output_buf_size = sizes[0]; 778 *num_buffers = max(*num_buffers, out_num); 779 inst->num_output_bufs = *num_buffers; 780 781 mutex_lock(&inst->lock); 782 if (inst->codec_state == VENUS_DEC_STATE_CAPTURE_SETUP) 783 inst->codec_state = VENUS_DEC_STATE_STOPPED; 784 mutex_unlock(&inst->lock); 785 break; 786 default: 787 ret = -EINVAL; 788 break; 789 } 790 791 return ret; 792 } 793 794 static int vdec_verify_conf(struct venus_inst *inst) 795 { 796 enum hfi_version ver = inst->core->res->hfi_version; 797 struct hfi_buffer_requirements bufreq; 798 int ret; 799 800 if (!inst->num_input_bufs || !inst->num_output_bufs) 801 return -EINVAL; 802 803 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq); 804 if (ret) 805 return ret; 806 807 if (inst->num_output_bufs < bufreq.count_actual || 808 inst->num_output_bufs < HFI_BUFREQ_COUNT_MIN(&bufreq, ver)) 809 return -EINVAL; 810 811 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq); 812 if (ret) 813 return ret; 814 815 if (inst->num_input_bufs < HFI_BUFREQ_COUNT_MIN(&bufreq, ver)) 816 return -EINVAL; 817 818 return 0; 819 } 820 821 static int vdec_start_capture(struct venus_inst *inst) 822 { 823 int ret; 824 825 if (!inst->streamon_out) 826 return 0; 827 828 if (inst->codec_state == VENUS_DEC_STATE_DECODING) { 829 if (inst->reconfig) 830 goto reconfigure; 831 832 venus_helper_queue_dpb_bufs(inst); 833 venus_helper_process_initial_cap_bufs(inst); 834 inst->streamon_cap = 1; 835 return 0; 836 } 837 838 if (inst->codec_state != VENUS_DEC_STATE_STOPPED) 839 return 0; 840 841 reconfigure: 842 ret = hfi_session_flush(inst, HFI_FLUSH_OUTPUT); 843 if (ret) 844 return ret; 845 846 ret = vdec_output_conf(inst); 847 if (ret) 848 return ret; 849 850 ret = venus_helper_set_num_bufs(inst, inst->num_input_bufs, 851 VB2_MAX_FRAME, VB2_MAX_FRAME); 852 if (ret) 853 return ret; 854 855 ret = venus_helper_intbufs_realloc(inst); 856 if (ret) 857 goto err; 858 859 ret = venus_helper_alloc_dpb_bufs(inst); 860 if (ret) 861 goto err; 862 863 ret = venus_helper_queue_dpb_bufs(inst); 864 if (ret) 865 goto free_dpb_bufs; 866 867 ret = venus_helper_process_initial_cap_bufs(inst); 868 if (ret) 869 goto free_dpb_bufs; 870 871 venus_helper_load_scale_clocks(inst); 872 873 ret = hfi_session_continue(inst); 874 if (ret) 875 goto free_dpb_bufs; 876 877 inst->codec_state = VENUS_DEC_STATE_DECODING; 878 879 inst->streamon_cap = 1; 880 inst->sequence_cap = 0; 881 inst->reconfig = false; 882 883 return 0; 884 885 free_dpb_bufs: 886 venus_helper_free_dpb_bufs(inst); 887 err: 888 return ret; 889 } 890 891 static int vdec_start_output(struct venus_inst *inst) 892 { 893 int ret; 894 895 if (inst->codec_state == VENUS_DEC_STATE_SEEK) { 896 ret = venus_helper_process_initial_out_bufs(inst); 897 inst->codec_state = VENUS_DEC_STATE_DECODING; 898 goto done; 899 } 900 901 if (inst->codec_state == VENUS_DEC_STATE_INIT || 902 inst->codec_state == VENUS_DEC_STATE_CAPTURE_SETUP) { 903 ret = venus_helper_process_initial_out_bufs(inst); 904 goto done; 905 } 906 907 if (inst->codec_state != VENUS_DEC_STATE_DEINIT) 908 return -EINVAL; 909 910 venus_helper_init_instance(inst); 911 inst->sequence_out = 0; 912 inst->reconfig = false; 913 914 ret = vdec_set_properties(inst); 915 if (ret) 916 return ret; 917 918 ret = vdec_output_conf(inst); 919 if (ret) 920 return ret; 921 922 ret = vdec_verify_conf(inst); 923 if (ret) 924 return ret; 925 926 ret = venus_helper_set_num_bufs(inst, inst->num_input_bufs, 927 VB2_MAX_FRAME, VB2_MAX_FRAME); 928 if (ret) 929 return ret; 930 931 ret = venus_helper_vb2_start_streaming(inst); 932 if (ret) 933 return ret; 934 935 ret = venus_helper_process_initial_out_bufs(inst); 936 if (ret) 937 return ret; 938 939 inst->codec_state = VENUS_DEC_STATE_INIT; 940 941 done: 942 inst->streamon_out = 1; 943 return ret; 944 } 945 946 static int vdec_start_streaming(struct vb2_queue *q, unsigned int count) 947 { 948 struct venus_inst *inst = vb2_get_drv_priv(q); 949 int ret; 950 951 mutex_lock(&inst->lock); 952 953 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) 954 ret = vdec_start_capture(inst); 955 else 956 ret = vdec_start_output(inst); 957 958 if (ret) 959 goto error; 960 961 mutex_unlock(&inst->lock); 962 return 0; 963 964 error: 965 venus_helper_buffers_done(inst, VB2_BUF_STATE_QUEUED); 966 mutex_unlock(&inst->lock); 967 return ret; 968 } 969 970 static void vdec_cancel_dst_buffers(struct venus_inst *inst) 971 { 972 struct vb2_v4l2_buffer *buf; 973 974 while ((buf = v4l2_m2m_dst_buf_remove(inst->m2m_ctx))) 975 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR); 976 } 977 978 static int vdec_stop_capture(struct venus_inst *inst) 979 { 980 int ret = 0; 981 982 switch (inst->codec_state) { 983 case VENUS_DEC_STATE_DECODING: 984 ret = hfi_session_flush(inst, HFI_FLUSH_ALL); 985 /* fallthrough */ 986 case VENUS_DEC_STATE_DRAIN: 987 vdec_cancel_dst_buffers(inst); 988 inst->codec_state = VENUS_DEC_STATE_STOPPED; 989 break; 990 case VENUS_DEC_STATE_DRC: 991 ret = hfi_session_flush(inst, HFI_FLUSH_OUTPUT); 992 vdec_cancel_dst_buffers(inst); 993 inst->codec_state = VENUS_DEC_STATE_CAPTURE_SETUP; 994 INIT_LIST_HEAD(&inst->registeredbufs); 995 venus_helper_free_dpb_bufs(inst); 996 break; 997 default: 998 return 0; 999 } 1000 1001 return ret; 1002 } 1003 1004 static int vdec_stop_output(struct venus_inst *inst) 1005 { 1006 int ret = 0; 1007 1008 switch (inst->codec_state) { 1009 case VENUS_DEC_STATE_DECODING: 1010 case VENUS_DEC_STATE_DRAIN: 1011 case VENUS_DEC_STATE_STOPPED: 1012 ret = hfi_session_flush(inst, HFI_FLUSH_ALL); 1013 inst->codec_state = VENUS_DEC_STATE_SEEK; 1014 break; 1015 case VENUS_DEC_STATE_INIT: 1016 case VENUS_DEC_STATE_CAPTURE_SETUP: 1017 ret = hfi_session_flush(inst, HFI_FLUSH_INPUT); 1018 break; 1019 default: 1020 break; 1021 } 1022 1023 return ret; 1024 } 1025 1026 static void vdec_stop_streaming(struct vb2_queue *q) 1027 { 1028 struct venus_inst *inst = vb2_get_drv_priv(q); 1029 int ret = -EINVAL; 1030 1031 mutex_lock(&inst->lock); 1032 1033 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) 1034 ret = vdec_stop_capture(inst); 1035 else 1036 ret = vdec_stop_output(inst); 1037 1038 venus_helper_buffers_done(inst, VB2_BUF_STATE_ERROR); 1039 1040 if (ret) 1041 goto unlock; 1042 1043 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) 1044 inst->streamon_out = 0; 1045 else 1046 inst->streamon_cap = 0; 1047 1048 unlock: 1049 mutex_unlock(&inst->lock); 1050 } 1051 1052 static void vdec_session_release(struct venus_inst *inst) 1053 { 1054 struct venus_core *core = inst->core; 1055 int ret, abort = 0; 1056 1057 mutex_lock(&inst->lock); 1058 1059 inst->codec_state = VENUS_DEC_STATE_DEINIT; 1060 1061 ret = hfi_session_stop(inst); 1062 abort = (ret && ret != -EINVAL) ? 1 : 0; 1063 ret = hfi_session_unload_res(inst); 1064 abort = (ret && ret != -EINVAL) ? 1 : 0; 1065 ret = venus_helper_unregister_bufs(inst); 1066 abort = (ret && ret != -EINVAL) ? 1 : 0; 1067 ret = venus_helper_intbufs_free(inst); 1068 abort = (ret && ret != -EINVAL) ? 1 : 0; 1069 ret = hfi_session_deinit(inst); 1070 abort = (ret && ret != -EINVAL) ? 1 : 0; 1071 1072 if (inst->session_error || core->sys_error) 1073 abort = 1; 1074 1075 if (abort) 1076 hfi_session_abort(inst); 1077 1078 venus_helper_free_dpb_bufs(inst); 1079 venus_helper_load_scale_clocks(inst); 1080 INIT_LIST_HEAD(&inst->registeredbufs); 1081 1082 mutex_unlock(&inst->lock); 1083 } 1084 1085 static int vdec_buf_init(struct vb2_buffer *vb) 1086 { 1087 struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue); 1088 1089 inst->buf_count++; 1090 1091 return venus_helper_vb2_buf_init(vb); 1092 } 1093 1094 static void vdec_buf_cleanup(struct vb2_buffer *vb) 1095 { 1096 struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue); 1097 1098 inst->buf_count--; 1099 if (!inst->buf_count) 1100 vdec_session_release(inst); 1101 } 1102 1103 static const struct vb2_ops vdec_vb2_ops = { 1104 .queue_setup = vdec_queue_setup, 1105 .buf_init = vdec_buf_init, 1106 .buf_cleanup = vdec_buf_cleanup, 1107 .buf_prepare = venus_helper_vb2_buf_prepare, 1108 .start_streaming = vdec_start_streaming, 1109 .stop_streaming = vdec_stop_streaming, 1110 .buf_queue = venus_helper_vb2_buf_queue, 1111 }; 1112 1113 static void vdec_buf_done(struct venus_inst *inst, unsigned int buf_type, 1114 u32 tag, u32 bytesused, u32 data_offset, u32 flags, 1115 u32 hfi_flags, u64 timestamp_us) 1116 { 1117 enum vb2_buffer_state state = VB2_BUF_STATE_DONE; 1118 struct vb2_v4l2_buffer *vbuf; 1119 struct vb2_buffer *vb; 1120 unsigned int type; 1121 1122 if (buf_type == HFI_BUFFER_INPUT) 1123 type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; 1124 else 1125 type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; 1126 1127 vbuf = venus_helper_find_buf(inst, type, tag); 1128 if (!vbuf) 1129 return; 1130 1131 vbuf->flags = flags; 1132 vbuf->field = V4L2_FIELD_NONE; 1133 vb = &vbuf->vb2_buf; 1134 1135 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) { 1136 vb2_set_plane_payload(vb, 0, bytesused); 1137 vb->planes[0].data_offset = data_offset; 1138 vb->timestamp = timestamp_us * NSEC_PER_USEC; 1139 vbuf->sequence = inst->sequence_cap++; 1140 1141 if (vbuf->flags & V4L2_BUF_FLAG_LAST) { 1142 const struct v4l2_event ev = { .type = V4L2_EVENT_EOS }; 1143 1144 v4l2_event_queue_fh(&inst->fh, &ev); 1145 1146 if (inst->codec_state == VENUS_DEC_STATE_DRAIN) 1147 inst->codec_state = VENUS_DEC_STATE_STOPPED; 1148 } 1149 } else { 1150 vbuf->sequence = inst->sequence_out++; 1151 } 1152 1153 venus_helper_get_ts_metadata(inst, timestamp_us, vbuf); 1154 1155 if (hfi_flags & HFI_BUFFERFLAG_READONLY) 1156 venus_helper_acquire_buf_ref(vbuf); 1157 1158 if (hfi_flags & HFI_BUFFERFLAG_DATACORRUPT) 1159 state = VB2_BUF_STATE_ERROR; 1160 1161 if (hfi_flags & HFI_BUFFERFLAG_DROP_FRAME) { 1162 state = VB2_BUF_STATE_ERROR; 1163 vb2_set_plane_payload(vb, 0, 0); 1164 vb->timestamp = 0; 1165 } 1166 1167 v4l2_m2m_buf_done(vbuf, state); 1168 } 1169 1170 static void vdec_event_change(struct venus_inst *inst, 1171 struct hfi_event_data *ev_data, bool sufficient) 1172 { 1173 static const struct v4l2_event ev = { 1174 .type = V4L2_EVENT_SOURCE_CHANGE, 1175 .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION }; 1176 struct device *dev = inst->core->dev_dec; 1177 struct v4l2_format format = {}; 1178 1179 mutex_lock(&inst->lock); 1180 1181 format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; 1182 format.fmt.pix_mp.pixelformat = inst->fmt_cap->pixfmt; 1183 format.fmt.pix_mp.width = ev_data->width; 1184 format.fmt.pix_mp.height = ev_data->height; 1185 1186 vdec_try_fmt_common(inst, &format); 1187 1188 inst->width = format.fmt.pix_mp.width; 1189 inst->height = format.fmt.pix_mp.height; 1190 1191 inst->out_width = ev_data->width; 1192 inst->out_height = ev_data->height; 1193 1194 dev_dbg(dev, "event %s sufficient resources (%ux%u)\n", 1195 sufficient ? "" : "not", ev_data->width, ev_data->height); 1196 1197 if (sufficient) { 1198 hfi_session_continue(inst); 1199 } else { 1200 switch (inst->codec_state) { 1201 case VENUS_DEC_STATE_INIT: 1202 inst->codec_state = VENUS_DEC_STATE_CAPTURE_SETUP; 1203 break; 1204 case VENUS_DEC_STATE_DECODING: 1205 inst->codec_state = VENUS_DEC_STATE_DRC; 1206 break; 1207 default: 1208 break; 1209 } 1210 } 1211 1212 inst->reconfig = true; 1213 v4l2_event_queue_fh(&inst->fh, &ev); 1214 wake_up(&inst->reconf_wait); 1215 1216 mutex_unlock(&inst->lock); 1217 } 1218 1219 static void vdec_event_notify(struct venus_inst *inst, u32 event, 1220 struct hfi_event_data *data) 1221 { 1222 struct venus_core *core = inst->core; 1223 struct device *dev = core->dev_dec; 1224 1225 switch (event) { 1226 case EVT_SESSION_ERROR: 1227 inst->session_error = true; 1228 dev_err(dev, "dec: event session error %x\n", inst->error); 1229 break; 1230 case EVT_SYS_EVENT_CHANGE: 1231 switch (data->event_type) { 1232 case HFI_EVENT_DATA_SEQUENCE_CHANGED_SUFFICIENT_BUF_RESOURCES: 1233 vdec_event_change(inst, data, true); 1234 break; 1235 case HFI_EVENT_DATA_SEQUENCE_CHANGED_INSUFFICIENT_BUF_RESOURCES: 1236 vdec_event_change(inst, data, false); 1237 break; 1238 case HFI_EVENT_RELEASE_BUFFER_REFERENCE: 1239 venus_helper_release_buf_ref(inst, data->tag); 1240 break; 1241 default: 1242 break; 1243 } 1244 break; 1245 default: 1246 break; 1247 } 1248 } 1249 1250 static const struct hfi_inst_ops vdec_hfi_ops = { 1251 .buf_done = vdec_buf_done, 1252 .event_notify = vdec_event_notify, 1253 }; 1254 1255 static void vdec_inst_init(struct venus_inst *inst) 1256 { 1257 inst->hfi_codec = HFI_VIDEO_CODEC_H264; 1258 inst->fmt_out = &vdec_formats[6]; 1259 inst->fmt_cap = &vdec_formats[0]; 1260 inst->width = frame_width_min(inst); 1261 inst->height = ALIGN(frame_height_min(inst), 32); 1262 inst->out_width = frame_width_min(inst); 1263 inst->out_height = frame_height_min(inst); 1264 inst->fps = 30; 1265 inst->timeperframe.numerator = 1; 1266 inst->timeperframe.denominator = 30; 1267 inst->opb_buftype = HFI_BUFFER_OUTPUT; 1268 } 1269 1270 static void vdec_m2m_device_run(void *priv) 1271 { 1272 } 1273 1274 static const struct v4l2_m2m_ops vdec_m2m_ops = { 1275 .device_run = vdec_m2m_device_run, 1276 .job_abort = venus_helper_m2m_job_abort, 1277 }; 1278 1279 static int m2m_queue_init(void *priv, struct vb2_queue *src_vq, 1280 struct vb2_queue *dst_vq) 1281 { 1282 struct venus_inst *inst = priv; 1283 int ret; 1284 1285 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; 1286 src_vq->io_modes = VB2_MMAP | VB2_DMABUF; 1287 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 1288 src_vq->ops = &vdec_vb2_ops; 1289 src_vq->mem_ops = &vb2_dma_sg_memops; 1290 src_vq->drv_priv = inst; 1291 src_vq->buf_struct_size = sizeof(struct venus_buffer); 1292 src_vq->allow_zero_bytesused = 1; 1293 src_vq->min_buffers_needed = 0; 1294 src_vq->dev = inst->core->dev; 1295 ret = vb2_queue_init(src_vq); 1296 if (ret) 1297 return ret; 1298 1299 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; 1300 dst_vq->io_modes = VB2_MMAP | VB2_DMABUF; 1301 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 1302 dst_vq->ops = &vdec_vb2_ops; 1303 dst_vq->mem_ops = &vb2_dma_sg_memops; 1304 dst_vq->drv_priv = inst; 1305 dst_vq->buf_struct_size = sizeof(struct venus_buffer); 1306 dst_vq->allow_zero_bytesused = 1; 1307 dst_vq->min_buffers_needed = 0; 1308 dst_vq->dev = inst->core->dev; 1309 ret = vb2_queue_init(dst_vq); 1310 if (ret) { 1311 vb2_queue_release(src_vq); 1312 return ret; 1313 } 1314 1315 return 0; 1316 } 1317 1318 static int vdec_open(struct file *file) 1319 { 1320 struct venus_core *core = video_drvdata(file); 1321 struct venus_inst *inst; 1322 int ret; 1323 1324 inst = kzalloc(sizeof(*inst), GFP_KERNEL); 1325 if (!inst) 1326 return -ENOMEM; 1327 1328 INIT_LIST_HEAD(&inst->dpbbufs); 1329 INIT_LIST_HEAD(&inst->registeredbufs); 1330 INIT_LIST_HEAD(&inst->internalbufs); 1331 INIT_LIST_HEAD(&inst->list); 1332 mutex_init(&inst->lock); 1333 1334 inst->core = core; 1335 inst->session_type = VIDC_SESSION_TYPE_DEC; 1336 inst->num_output_bufs = 1; 1337 inst->codec_state = VENUS_DEC_STATE_DEINIT; 1338 inst->buf_count = 0; 1339 init_waitqueue_head(&inst->reconf_wait); 1340 venus_helper_init_instance(inst); 1341 1342 ret = pm_runtime_get_sync(core->dev_dec); 1343 if (ret < 0) 1344 goto err_free_inst; 1345 1346 ret = vdec_ctrl_init(inst); 1347 if (ret) 1348 goto err_put_sync; 1349 1350 ret = hfi_session_create(inst, &vdec_hfi_ops); 1351 if (ret) 1352 goto err_ctrl_deinit; 1353 1354 vdec_inst_init(inst); 1355 1356 /* 1357 * create m2m device for every instance, the m2m context scheduling 1358 * is made by firmware side so we do not need to care about. 1359 */ 1360 inst->m2m_dev = v4l2_m2m_init(&vdec_m2m_ops); 1361 if (IS_ERR(inst->m2m_dev)) { 1362 ret = PTR_ERR(inst->m2m_dev); 1363 goto err_session_destroy; 1364 } 1365 1366 inst->m2m_ctx = v4l2_m2m_ctx_init(inst->m2m_dev, inst, m2m_queue_init); 1367 if (IS_ERR(inst->m2m_ctx)) { 1368 ret = PTR_ERR(inst->m2m_ctx); 1369 goto err_m2m_release; 1370 } 1371 1372 v4l2_fh_init(&inst->fh, core->vdev_dec); 1373 1374 inst->fh.ctrl_handler = &inst->ctrl_handler; 1375 v4l2_fh_add(&inst->fh); 1376 inst->fh.m2m_ctx = inst->m2m_ctx; 1377 file->private_data = &inst->fh; 1378 1379 return 0; 1380 1381 err_m2m_release: 1382 v4l2_m2m_release(inst->m2m_dev); 1383 err_session_destroy: 1384 hfi_session_destroy(inst); 1385 err_ctrl_deinit: 1386 vdec_ctrl_deinit(inst); 1387 err_put_sync: 1388 pm_runtime_put_sync(core->dev_dec); 1389 err_free_inst: 1390 kfree(inst); 1391 return ret; 1392 } 1393 1394 static int vdec_close(struct file *file) 1395 { 1396 struct venus_inst *inst = to_inst(file); 1397 1398 v4l2_m2m_ctx_release(inst->m2m_ctx); 1399 v4l2_m2m_release(inst->m2m_dev); 1400 vdec_ctrl_deinit(inst); 1401 hfi_session_destroy(inst); 1402 mutex_destroy(&inst->lock); 1403 v4l2_fh_del(&inst->fh); 1404 v4l2_fh_exit(&inst->fh); 1405 1406 pm_runtime_put_sync(inst->core->dev_dec); 1407 1408 kfree(inst); 1409 return 0; 1410 } 1411 1412 static const struct v4l2_file_operations vdec_fops = { 1413 .owner = THIS_MODULE, 1414 .open = vdec_open, 1415 .release = vdec_close, 1416 .unlocked_ioctl = video_ioctl2, 1417 .poll = v4l2_m2m_fop_poll, 1418 .mmap = v4l2_m2m_fop_mmap, 1419 }; 1420 1421 static int vdec_probe(struct platform_device *pdev) 1422 { 1423 struct device *dev = &pdev->dev; 1424 struct video_device *vdev; 1425 struct venus_core *core; 1426 int ret; 1427 1428 if (!dev->parent) 1429 return -EPROBE_DEFER; 1430 1431 core = dev_get_drvdata(dev->parent); 1432 if (!core) 1433 return -EPROBE_DEFER; 1434 1435 if (IS_V3(core) || IS_V4(core)) { 1436 core->core0_clk = devm_clk_get(dev, "core"); 1437 if (IS_ERR(core->core0_clk)) 1438 return PTR_ERR(core->core0_clk); 1439 } 1440 1441 if (IS_V4(core)) { 1442 core->core0_bus_clk = devm_clk_get(dev, "bus"); 1443 if (IS_ERR(core->core0_bus_clk)) 1444 return PTR_ERR(core->core0_bus_clk); 1445 } 1446 1447 platform_set_drvdata(pdev, core); 1448 1449 vdev = video_device_alloc(); 1450 if (!vdev) 1451 return -ENOMEM; 1452 1453 strscpy(vdev->name, "qcom-venus-decoder", sizeof(vdev->name)); 1454 vdev->release = video_device_release; 1455 vdev->fops = &vdec_fops; 1456 vdev->ioctl_ops = &vdec_ioctl_ops; 1457 vdev->vfl_dir = VFL_DIR_M2M; 1458 vdev->v4l2_dev = &core->v4l2_dev; 1459 vdev->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING; 1460 1461 ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1); 1462 if (ret) 1463 goto err_vdev_release; 1464 1465 core->vdev_dec = vdev; 1466 core->dev_dec = dev; 1467 1468 video_set_drvdata(vdev, core); 1469 pm_runtime_enable(dev); 1470 1471 return 0; 1472 1473 err_vdev_release: 1474 video_device_release(vdev); 1475 return ret; 1476 } 1477 1478 static int vdec_remove(struct platform_device *pdev) 1479 { 1480 struct venus_core *core = dev_get_drvdata(pdev->dev.parent); 1481 1482 video_unregister_device(core->vdev_dec); 1483 pm_runtime_disable(core->dev_dec); 1484 1485 return 0; 1486 } 1487 1488 static __maybe_unused int vdec_runtime_suspend(struct device *dev) 1489 { 1490 struct venus_core *core = dev_get_drvdata(dev); 1491 int ret; 1492 1493 if (IS_V1(core)) 1494 return 0; 1495 1496 ret = venus_helper_power_enable(core, VIDC_SESSION_TYPE_DEC, true); 1497 if (ret) 1498 return ret; 1499 1500 if (IS_V4(core)) 1501 clk_disable_unprepare(core->core0_bus_clk); 1502 1503 clk_disable_unprepare(core->core0_clk); 1504 1505 return venus_helper_power_enable(core, VIDC_SESSION_TYPE_DEC, false); 1506 } 1507 1508 static __maybe_unused int vdec_runtime_resume(struct device *dev) 1509 { 1510 struct venus_core *core = dev_get_drvdata(dev); 1511 int ret; 1512 1513 if (IS_V1(core)) 1514 return 0; 1515 1516 ret = venus_helper_power_enable(core, VIDC_SESSION_TYPE_DEC, true); 1517 if (ret) 1518 return ret; 1519 1520 ret = clk_prepare_enable(core->core0_clk); 1521 if (ret) 1522 goto err_power_disable; 1523 1524 if (IS_V4(core)) 1525 ret = clk_prepare_enable(core->core0_bus_clk); 1526 1527 if (ret) 1528 goto err_unprepare_core0; 1529 1530 return venus_helper_power_enable(core, VIDC_SESSION_TYPE_DEC, false); 1531 1532 err_unprepare_core0: 1533 clk_disable_unprepare(core->core0_clk); 1534 err_power_disable: 1535 venus_helper_power_enable(core, VIDC_SESSION_TYPE_DEC, false); 1536 return ret; 1537 } 1538 1539 static const struct dev_pm_ops vdec_pm_ops = { 1540 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, 1541 pm_runtime_force_resume) 1542 SET_RUNTIME_PM_OPS(vdec_runtime_suspend, vdec_runtime_resume, NULL) 1543 }; 1544 1545 static const struct of_device_id vdec_dt_match[] = { 1546 { .compatible = "venus-decoder" }, 1547 { } 1548 }; 1549 MODULE_DEVICE_TABLE(of, vdec_dt_match); 1550 1551 static struct platform_driver qcom_venus_dec_driver = { 1552 .probe = vdec_probe, 1553 .remove = vdec_remove, 1554 .driver = { 1555 .name = "qcom-venus-decoder", 1556 .of_match_table = vdec_dt_match, 1557 .pm = &vdec_pm_ops, 1558 }, 1559 }; 1560 module_platform_driver(qcom_venus_dec_driver); 1561 1562 MODULE_ALIAS("platform:qcom-venus-decoder"); 1563 MODULE_DESCRIPTION("Qualcomm Venus video decoder driver"); 1564 MODULE_LICENSE("GPL v2"); 1565