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