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-mem2mem.h> 13 #include <media/videobuf2-dma-contig.h> 14 #include <media/v4l2-ioctl.h> 15 #include <media/v4l2-event.h> 16 #include <media/v4l2-ctrls.h> 17 18 #include "hfi_venus_io.h" 19 #include "hfi_parser.h" 20 #include "core.h" 21 #include "helpers.h" 22 #include "venc.h" 23 #include "pm_helpers.h" 24 25 #define NUM_B_FRAMES_MAX 4 26 27 /* 28 * Three resons to keep MPLANE formats (despite that the number of planes 29 * currently is one): 30 * - the MPLANE formats allow only one plane to be used 31 * - the downstream driver use MPLANE formats too 32 * - future firmware versions could add support for >1 planes 33 */ 34 static const struct venus_format venc_formats[] = { 35 [VENUS_FMT_NV12] = { 36 .pixfmt = V4L2_PIX_FMT_NV12, 37 .num_planes = 1, 38 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, 39 }, 40 [VENUS_FMT_H264] = { 41 .pixfmt = V4L2_PIX_FMT_H264, 42 .num_planes = 1, 43 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, 44 }, 45 [VENUS_FMT_VP8] = { 46 .pixfmt = V4L2_PIX_FMT_VP8, 47 .num_planes = 1, 48 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, 49 }, 50 [VENUS_FMT_HEVC] = { 51 .pixfmt = V4L2_PIX_FMT_HEVC, 52 .num_planes = 1, 53 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, 54 }, 55 [VENUS_FMT_MPEG4] = { 56 .pixfmt = V4L2_PIX_FMT_MPEG4, 57 .num_planes = 1, 58 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, 59 }, 60 [VENUS_FMT_H263] = { 61 .pixfmt = V4L2_PIX_FMT_H263, 62 .num_planes = 1, 63 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, 64 }, 65 }; 66 67 static const struct venus_format * 68 find_format(struct venus_inst *inst, u32 pixfmt, u32 type) 69 { 70 const struct venus_format *fmt = venc_formats; 71 unsigned int size = ARRAY_SIZE(venc_formats); 72 unsigned int i; 73 74 for (i = 0; i < size; i++) { 75 if (fmt[i].pixfmt == pixfmt) 76 break; 77 } 78 79 if (i == size || fmt[i].type != type) 80 return NULL; 81 82 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE && 83 !venus_helper_check_codec(inst, fmt[i].pixfmt)) 84 return NULL; 85 86 return &fmt[i]; 87 } 88 89 static const struct venus_format * 90 find_format_by_index(struct venus_inst *inst, unsigned int index, u32 type) 91 { 92 const struct venus_format *fmt = venc_formats; 93 unsigned int size = ARRAY_SIZE(venc_formats); 94 unsigned int i, k = 0; 95 96 if (index > size) 97 return NULL; 98 99 for (i = 0; i < size; i++) { 100 bool valid; 101 102 if (fmt[i].type != type) 103 continue; 104 valid = type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE || 105 venus_helper_check_codec(inst, fmt[i].pixfmt); 106 if (k == index && valid) 107 break; 108 if (valid) 109 k++; 110 } 111 112 if (i == size) 113 return NULL; 114 115 return &fmt[i]; 116 } 117 118 static int venc_v4l2_to_hfi(int id, int value) 119 { 120 switch (id) { 121 case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE: 122 switch (value) { 123 case V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC: 124 default: 125 return HFI_H264_ENTROPY_CAVLC; 126 case V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CABAC: 127 return HFI_H264_ENTROPY_CABAC; 128 } 129 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE: 130 switch (value) { 131 case V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED: 132 default: 133 return HFI_H264_DB_MODE_ALL_BOUNDARY; 134 case V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED: 135 return HFI_H264_DB_MODE_DISABLE; 136 case V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED_AT_SLICE_BOUNDARY: 137 return HFI_H264_DB_MODE_SKIP_SLICE_BOUNDARY; 138 } 139 } 140 141 return 0; 142 } 143 144 static int 145 venc_querycap(struct file *file, void *fh, struct v4l2_capability *cap) 146 { 147 strscpy(cap->driver, "qcom-venus", sizeof(cap->driver)); 148 strscpy(cap->card, "Qualcomm Venus video encoder", sizeof(cap->card)); 149 strscpy(cap->bus_info, "platform:qcom-venus", sizeof(cap->bus_info)); 150 151 return 0; 152 } 153 154 static int venc_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f) 155 { 156 struct venus_inst *inst = to_inst(file); 157 const struct venus_format *fmt; 158 159 fmt = find_format_by_index(inst, f->index, f->type); 160 161 memset(f->reserved, 0, sizeof(f->reserved)); 162 163 if (!fmt) 164 return -EINVAL; 165 166 f->pixelformat = fmt->pixfmt; 167 168 return 0; 169 } 170 171 static const struct venus_format * 172 venc_try_fmt_common(struct venus_inst *inst, struct v4l2_format *f) 173 { 174 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp; 175 struct v4l2_plane_pix_format *pfmt = pixmp->plane_fmt; 176 const struct venus_format *fmt; 177 u32 sizeimage; 178 179 memset(pfmt[0].reserved, 0, sizeof(pfmt[0].reserved)); 180 memset(pixmp->reserved, 0, sizeof(pixmp->reserved)); 181 182 fmt = find_format(inst, pixmp->pixelformat, f->type); 183 if (!fmt) { 184 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) 185 pixmp->pixelformat = V4L2_PIX_FMT_H264; 186 else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) 187 pixmp->pixelformat = V4L2_PIX_FMT_NV12; 188 else 189 return NULL; 190 fmt = find_format(inst, pixmp->pixelformat, f->type); 191 if (!fmt) 192 return NULL; 193 } 194 195 pixmp->width = clamp(pixmp->width, frame_width_min(inst), 196 frame_width_max(inst)); 197 pixmp->height = clamp(pixmp->height, frame_height_min(inst), 198 frame_height_max(inst)); 199 200 pixmp->width = ALIGN(pixmp->width, 128); 201 pixmp->height = ALIGN(pixmp->height, 32); 202 203 pixmp->width = ALIGN(pixmp->width, 2); 204 pixmp->height = ALIGN(pixmp->height, 2); 205 206 if (pixmp->field == V4L2_FIELD_ANY) 207 pixmp->field = V4L2_FIELD_NONE; 208 pixmp->num_planes = fmt->num_planes; 209 pixmp->flags = 0; 210 211 sizeimage = venus_helper_get_framesz(pixmp->pixelformat, 212 pixmp->width, 213 pixmp->height); 214 pfmt[0].sizeimage = max(ALIGN(pfmt[0].sizeimage, SZ_4K), sizeimage); 215 216 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) 217 pfmt[0].bytesperline = ALIGN(pixmp->width, 128); 218 else 219 pfmt[0].bytesperline = 0; 220 221 return fmt; 222 } 223 224 static int venc_try_fmt(struct file *file, void *fh, struct v4l2_format *f) 225 { 226 struct venus_inst *inst = to_inst(file); 227 228 venc_try_fmt_common(inst, f); 229 230 return 0; 231 } 232 233 static int venc_s_fmt(struct file *file, void *fh, struct v4l2_format *f) 234 { 235 struct venus_inst *inst = to_inst(file); 236 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp; 237 struct v4l2_pix_format_mplane orig_pixmp; 238 const struct venus_format *fmt; 239 struct v4l2_format format; 240 u32 pixfmt_out = 0, pixfmt_cap = 0; 241 struct vb2_queue *q; 242 243 q = v4l2_m2m_get_vq(inst->m2m_ctx, f->type); 244 if (!q) 245 return -EINVAL; 246 247 if (vb2_is_busy(q)) 248 return -EBUSY; 249 250 orig_pixmp = *pixmp; 251 252 fmt = venc_try_fmt_common(inst, f); 253 if (!fmt) 254 return -EINVAL; 255 256 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) { 257 pixfmt_out = pixmp->pixelformat; 258 pixfmt_cap = inst->fmt_cap->pixfmt; 259 } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) { 260 pixfmt_cap = pixmp->pixelformat; 261 pixfmt_out = inst->fmt_out->pixfmt; 262 } 263 264 memset(&format, 0, sizeof(format)); 265 266 format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; 267 format.fmt.pix_mp.pixelformat = pixfmt_out; 268 format.fmt.pix_mp.width = orig_pixmp.width; 269 format.fmt.pix_mp.height = orig_pixmp.height; 270 venc_try_fmt_common(inst, &format); 271 272 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) { 273 inst->out_width = format.fmt.pix_mp.width; 274 inst->out_height = format.fmt.pix_mp.height; 275 inst->colorspace = pixmp->colorspace; 276 inst->ycbcr_enc = pixmp->ycbcr_enc; 277 inst->quantization = pixmp->quantization; 278 inst->xfer_func = pixmp->xfer_func; 279 } 280 281 memset(&format, 0, sizeof(format)); 282 283 format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; 284 format.fmt.pix_mp.pixelformat = pixfmt_cap; 285 format.fmt.pix_mp.width = orig_pixmp.width; 286 format.fmt.pix_mp.height = orig_pixmp.height; 287 venc_try_fmt_common(inst, &format); 288 289 inst->width = format.fmt.pix_mp.width; 290 inst->height = format.fmt.pix_mp.height; 291 292 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) 293 inst->fmt_out = fmt; 294 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) { 295 inst->fmt_cap = fmt; 296 inst->output_buf_size = pixmp->plane_fmt[0].sizeimage; 297 } 298 299 return 0; 300 } 301 302 static int venc_g_fmt(struct file *file, void *fh, struct v4l2_format *f) 303 { 304 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp; 305 struct venus_inst *inst = to_inst(file); 306 const struct venus_format *fmt; 307 308 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) 309 fmt = inst->fmt_cap; 310 else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) 311 fmt = inst->fmt_out; 312 else 313 return -EINVAL; 314 315 pixmp->pixelformat = fmt->pixfmt; 316 317 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) { 318 pixmp->width = inst->width; 319 pixmp->height = inst->height; 320 pixmp->colorspace = inst->colorspace; 321 pixmp->ycbcr_enc = inst->ycbcr_enc; 322 pixmp->quantization = inst->quantization; 323 pixmp->xfer_func = inst->xfer_func; 324 } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) { 325 pixmp->width = inst->out_width; 326 pixmp->height = inst->out_height; 327 } 328 329 venc_try_fmt_common(inst, f); 330 331 return 0; 332 } 333 334 static int 335 venc_g_selection(struct file *file, void *fh, struct v4l2_selection *s) 336 { 337 struct venus_inst *inst = to_inst(file); 338 339 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) 340 return -EINVAL; 341 342 switch (s->target) { 343 case V4L2_SEL_TGT_CROP_DEFAULT: 344 case V4L2_SEL_TGT_CROP_BOUNDS: 345 s->r.width = inst->out_width; 346 s->r.height = inst->out_height; 347 break; 348 case V4L2_SEL_TGT_CROP: 349 s->r.width = inst->width; 350 s->r.height = inst->height; 351 break; 352 default: 353 return -EINVAL; 354 } 355 356 s->r.top = 0; 357 s->r.left = 0; 358 359 return 0; 360 } 361 362 static int 363 venc_s_selection(struct file *file, void *fh, struct v4l2_selection *s) 364 { 365 struct venus_inst *inst = to_inst(file); 366 367 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) 368 return -EINVAL; 369 370 if (s->r.width > inst->out_width || 371 s->r.height > inst->out_height) 372 return -EINVAL; 373 374 s->r.width = ALIGN(s->r.width, 2); 375 s->r.height = ALIGN(s->r.height, 2); 376 377 switch (s->target) { 378 case V4L2_SEL_TGT_CROP: 379 s->r.top = 0; 380 s->r.left = 0; 381 inst->width = s->r.width; 382 inst->height = s->r.height; 383 break; 384 default: 385 return -EINVAL; 386 } 387 388 return 0; 389 } 390 391 static int venc_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a) 392 { 393 struct venus_inst *inst = to_inst(file); 394 struct v4l2_outputparm *out = &a->parm.output; 395 struct v4l2_fract *timeperframe = &out->timeperframe; 396 u64 us_per_frame, fps; 397 398 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT && 399 a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) 400 return -EINVAL; 401 402 memset(out->reserved, 0, sizeof(out->reserved)); 403 404 if (!timeperframe->denominator) 405 timeperframe->denominator = inst->timeperframe.denominator; 406 if (!timeperframe->numerator) 407 timeperframe->numerator = inst->timeperframe.numerator; 408 409 out->capability = V4L2_CAP_TIMEPERFRAME; 410 411 us_per_frame = timeperframe->numerator * (u64)USEC_PER_SEC; 412 do_div(us_per_frame, timeperframe->denominator); 413 414 us_per_frame = clamp(us_per_frame, 1, USEC_PER_SEC); 415 fps = USEC_PER_SEC / (u32)us_per_frame; 416 fps = min(VENUS_MAX_FPS, fps); 417 418 inst->timeperframe = *timeperframe; 419 inst->fps = fps; 420 421 return 0; 422 } 423 424 static int venc_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a) 425 { 426 struct venus_inst *inst = to_inst(file); 427 428 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT && 429 a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) 430 return -EINVAL; 431 432 a->parm.output.capability |= V4L2_CAP_TIMEPERFRAME; 433 a->parm.output.timeperframe = inst->timeperframe; 434 435 return 0; 436 } 437 438 static int venc_enum_framesizes(struct file *file, void *fh, 439 struct v4l2_frmsizeenum *fsize) 440 { 441 struct venus_inst *inst = to_inst(file); 442 const struct venus_format *fmt; 443 444 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE; 445 446 fmt = find_format(inst, fsize->pixel_format, 447 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE); 448 if (!fmt) { 449 fmt = find_format(inst, fsize->pixel_format, 450 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE); 451 if (!fmt) 452 return -EINVAL; 453 } 454 455 if (fsize->index) 456 return -EINVAL; 457 458 fsize->stepwise.min_width = frame_width_min(inst); 459 fsize->stepwise.max_width = frame_width_max(inst); 460 fsize->stepwise.step_width = frame_width_step(inst); 461 fsize->stepwise.min_height = frame_height_min(inst); 462 fsize->stepwise.max_height = frame_height_max(inst); 463 fsize->stepwise.step_height = frame_height_step(inst); 464 465 return 0; 466 } 467 468 static int venc_enum_frameintervals(struct file *file, void *fh, 469 struct v4l2_frmivalenum *fival) 470 { 471 struct venus_inst *inst = to_inst(file); 472 const struct venus_format *fmt; 473 unsigned int framerate_factor = 1; 474 475 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE; 476 477 fmt = find_format(inst, fival->pixel_format, 478 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE); 479 if (!fmt) { 480 fmt = find_format(inst, fival->pixel_format, 481 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE); 482 if (!fmt) 483 return -EINVAL; 484 } 485 486 if (fival->index) 487 return -EINVAL; 488 489 if (!fival->width || !fival->height) 490 return -EINVAL; 491 492 if (fival->width > frame_width_max(inst) || 493 fival->width < frame_width_min(inst) || 494 fival->height > frame_height_max(inst) || 495 fival->height < frame_height_min(inst)) 496 return -EINVAL; 497 498 if (IS_V1(inst->core)) { 499 /* framerate is reported in 1/65535 fps unit */ 500 framerate_factor = (1 << 16); 501 } 502 503 fival->stepwise.min.numerator = 1; 504 fival->stepwise.min.denominator = frate_max(inst) / framerate_factor; 505 fival->stepwise.max.numerator = 1; 506 fival->stepwise.max.denominator = frate_min(inst) / framerate_factor; 507 fival->stepwise.step.numerator = 1; 508 fival->stepwise.step.denominator = frate_max(inst) / framerate_factor; 509 510 return 0; 511 } 512 513 static int venc_subscribe_event(struct v4l2_fh *fh, 514 const struct v4l2_event_subscription *sub) 515 { 516 switch (sub->type) { 517 case V4L2_EVENT_EOS: 518 return v4l2_event_subscribe(fh, sub, 2, NULL); 519 case V4L2_EVENT_CTRL: 520 return v4l2_ctrl_subscribe_event(fh, sub); 521 default: 522 return -EINVAL; 523 } 524 } 525 526 static int 527 venc_encoder_cmd(struct file *file, void *fh, struct v4l2_encoder_cmd *cmd) 528 { 529 struct venus_inst *inst = to_inst(file); 530 struct hfi_frame_data fdata = {0}; 531 int ret = 0; 532 533 ret = v4l2_m2m_ioctl_try_encoder_cmd(file, fh, cmd); 534 if (ret) 535 return ret; 536 537 mutex_lock(&inst->lock); 538 539 if (cmd->cmd == V4L2_ENC_CMD_STOP && 540 inst->enc_state == VENUS_ENC_STATE_ENCODING) { 541 /* 542 * Implement V4L2_ENC_CMD_STOP by enqueue an empty buffer on 543 * encoder input to signal EOS. 544 */ 545 if (!(inst->streamon_out && inst->streamon_cap)) 546 goto unlock; 547 548 fdata.buffer_type = HFI_BUFFER_INPUT; 549 fdata.flags |= HFI_BUFFERFLAG_EOS; 550 fdata.device_addr = 0xdeadb000; 551 552 ret = hfi_session_process_buf(inst, &fdata); 553 554 inst->enc_state = VENUS_ENC_STATE_DRAIN; 555 } else if (cmd->cmd == V4L2_ENC_CMD_START) { 556 if (inst->enc_state == VENUS_ENC_STATE_DRAIN) { 557 ret = -EBUSY; 558 goto unlock; 559 } 560 if (inst->enc_state == VENUS_ENC_STATE_STOPPED) { 561 vb2_clear_last_buffer_dequeued(&inst->fh.m2m_ctx->cap_q_ctx.q); 562 inst->enc_state = VENUS_ENC_STATE_ENCODING; 563 } 564 } 565 566 unlock: 567 mutex_unlock(&inst->lock); 568 return ret; 569 } 570 571 static const struct v4l2_ioctl_ops venc_ioctl_ops = { 572 .vidioc_querycap = venc_querycap, 573 .vidioc_enum_fmt_vid_cap = venc_enum_fmt, 574 .vidioc_enum_fmt_vid_out = venc_enum_fmt, 575 .vidioc_s_fmt_vid_cap_mplane = venc_s_fmt, 576 .vidioc_s_fmt_vid_out_mplane = venc_s_fmt, 577 .vidioc_g_fmt_vid_cap_mplane = venc_g_fmt, 578 .vidioc_g_fmt_vid_out_mplane = venc_g_fmt, 579 .vidioc_try_fmt_vid_cap_mplane = venc_try_fmt, 580 .vidioc_try_fmt_vid_out_mplane = venc_try_fmt, 581 .vidioc_g_selection = venc_g_selection, 582 .vidioc_s_selection = venc_s_selection, 583 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs, 584 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf, 585 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs, 586 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf, 587 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf, 588 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf, 589 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf, 590 .vidioc_streamon = v4l2_m2m_ioctl_streamon, 591 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff, 592 .vidioc_s_parm = venc_s_parm, 593 .vidioc_g_parm = venc_g_parm, 594 .vidioc_enum_framesizes = venc_enum_framesizes, 595 .vidioc_enum_frameintervals = venc_enum_frameintervals, 596 .vidioc_subscribe_event = venc_subscribe_event, 597 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 598 .vidioc_try_encoder_cmd = v4l2_m2m_ioctl_try_encoder_cmd, 599 .vidioc_encoder_cmd = venc_encoder_cmd, 600 }; 601 602 static int venc_pm_get(struct venus_inst *inst) 603 { 604 struct venus_core *core = inst->core; 605 struct device *dev = core->dev_enc; 606 int ret; 607 608 mutex_lock(&core->pm_lock); 609 ret = pm_runtime_resume_and_get(dev); 610 mutex_unlock(&core->pm_lock); 611 612 return ret < 0 ? ret : 0; 613 } 614 615 static int venc_pm_put(struct venus_inst *inst, bool autosuspend) 616 { 617 struct venus_core *core = inst->core; 618 struct device *dev = core->dev_enc; 619 int ret; 620 621 mutex_lock(&core->pm_lock); 622 623 if (autosuspend) 624 ret = pm_runtime_put_autosuspend(dev); 625 else 626 ret = pm_runtime_put_sync(dev); 627 628 mutex_unlock(&core->pm_lock); 629 630 return ret < 0 ? ret : 0; 631 } 632 633 static int venc_pm_get_put(struct venus_inst *inst) 634 { 635 struct venus_core *core = inst->core; 636 struct device *dev = core->dev_enc; 637 int ret = 0; 638 639 mutex_lock(&core->pm_lock); 640 641 if (pm_runtime_suspended(dev)) { 642 ret = pm_runtime_resume_and_get(dev); 643 if (ret < 0) 644 goto error; 645 646 ret = pm_runtime_put_autosuspend(dev); 647 } 648 649 error: 650 mutex_unlock(&core->pm_lock); 651 652 return ret < 0 ? ret : 0; 653 } 654 655 static void venc_pm_touch(struct venus_inst *inst) 656 { 657 pm_runtime_mark_last_busy(inst->core->dev_enc); 658 } 659 660 static int venc_set_properties(struct venus_inst *inst) 661 { 662 struct venc_controls *ctr = &inst->controls.enc; 663 struct hfi_intra_period intra_period; 664 struct hfi_framerate frate; 665 struct hfi_bitrate brate; 666 struct hfi_idr_period idrp; 667 struct hfi_quantization quant; 668 struct hfi_quantization_range quant_range; 669 struct hfi_quantization_range_v2 quant_range_v2; 670 struct hfi_enable en; 671 struct hfi_ltr_mode ltr_mode; 672 struct hfi_intra_refresh intra_refresh = {}; 673 u32 ptype, rate_control, bitrate; 674 u32 profile, level; 675 int ret; 676 677 ret = venus_helper_set_work_mode(inst); 678 if (ret) 679 return ret; 680 681 ptype = HFI_PROPERTY_CONFIG_FRAME_RATE; 682 frate.buffer_type = HFI_BUFFER_OUTPUT; 683 frate.framerate = inst->fps * (1 << 16); 684 685 ret = hfi_session_set_property(inst, ptype, &frate); 686 if (ret) 687 return ret; 688 689 if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_H264) { 690 struct hfi_h264_vui_timing_info info; 691 struct hfi_h264_entropy_control entropy; 692 struct hfi_h264_db_control deblock; 693 struct hfi_h264_8x8_transform h264_transform; 694 695 ptype = HFI_PROPERTY_PARAM_VENC_H264_VUI_TIMING_INFO; 696 info.enable = 1; 697 info.fixed_framerate = 1; 698 info.time_scale = NSEC_PER_SEC; 699 700 ret = hfi_session_set_property(inst, ptype, &info); 701 if (ret) 702 return ret; 703 704 ptype = HFI_PROPERTY_PARAM_VENC_H264_ENTROPY_CONTROL; 705 entropy.entropy_mode = venc_v4l2_to_hfi( 706 V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE, 707 ctr->h264_entropy_mode); 708 entropy.cabac_model = HFI_H264_CABAC_MODEL_0; 709 710 ret = hfi_session_set_property(inst, ptype, &entropy); 711 if (ret) 712 return ret; 713 714 ptype = HFI_PROPERTY_PARAM_VENC_H264_DEBLOCK_CONTROL; 715 deblock.mode = venc_v4l2_to_hfi( 716 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE, 717 ctr->h264_loop_filter_mode); 718 deblock.slice_alpha_offset = ctr->h264_loop_filter_alpha; 719 deblock.slice_beta_offset = ctr->h264_loop_filter_beta; 720 721 ret = hfi_session_set_property(inst, ptype, &deblock); 722 if (ret) 723 return ret; 724 725 ptype = HFI_PROPERTY_PARAM_VENC_H264_TRANSFORM_8X8; 726 h264_transform.enable_type = 0; 727 if (ctr->profile.h264 == V4L2_MPEG_VIDEO_H264_PROFILE_HIGH || 728 ctr->profile.h264 == V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_HIGH) 729 h264_transform.enable_type = ctr->h264_8x8_transform; 730 731 ret = hfi_session_set_property(inst, ptype, &h264_transform); 732 if (ret) 733 return ret; 734 735 if (ctr->layer_bitrate) { 736 unsigned int i; 737 738 ptype = HFI_PROPERTY_PARAM_VENC_HIER_P_MAX_NUM_ENH_LAYER; 739 ret = hfi_session_set_property(inst, ptype, &ctr->h264_hier_layers); 740 if (ret) 741 return ret; 742 743 ptype = HFI_PROPERTY_CONFIG_VENC_HIER_P_ENH_LAYER; 744 ret = hfi_session_set_property(inst, ptype, &ctr->layer_bitrate); 745 if (ret) 746 return ret; 747 748 for (i = 0; i < ctr->h264_hier_layers; ++i) { 749 ptype = HFI_PROPERTY_CONFIG_VENC_TARGET_BITRATE; 750 brate.bitrate = ctr->h264_hier_layer_bitrate[i]; 751 brate.layer_id = i; 752 753 ret = hfi_session_set_property(inst, ptype, &brate); 754 if (ret) 755 return ret; 756 } 757 } 758 } 759 760 if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_H264 || 761 inst->fmt_cap->pixfmt == V4L2_PIX_FMT_HEVC) { 762 /* IDR periodicity, n: 763 * n = 0 - only the first I-frame is IDR frame 764 * n = 1 - all I-frames will be IDR frames 765 * n > 1 - every n-th I-frame will be IDR frame 766 */ 767 ptype = HFI_PROPERTY_CONFIG_VENC_IDR_PERIOD; 768 idrp.idr_period = 0; 769 ret = hfi_session_set_property(inst, ptype, &idrp); 770 if (ret) 771 return ret; 772 } 773 774 if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_HEVC && 775 ctr->profile.hevc == V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_10) { 776 struct hfi_hdr10_pq_sei hdr10; 777 unsigned int c; 778 779 ptype = HFI_PROPERTY_PARAM_VENC_HDR10_PQ_SEI; 780 781 for (c = 0; c < 3; c++) { 782 hdr10.mastering.display_primaries_x[c] = 783 ctr->mastering.display_primaries_x[c]; 784 hdr10.mastering.display_primaries_y[c] = 785 ctr->mastering.display_primaries_y[c]; 786 } 787 788 hdr10.mastering.white_point_x = ctr->mastering.white_point_x; 789 hdr10.mastering.white_point_y = ctr->mastering.white_point_y; 790 hdr10.mastering.max_display_mastering_luminance = 791 ctr->mastering.max_display_mastering_luminance; 792 hdr10.mastering.min_display_mastering_luminance = 793 ctr->mastering.min_display_mastering_luminance; 794 795 hdr10.cll.max_content_light = ctr->cll.max_content_light_level; 796 hdr10.cll.max_pic_average_light = 797 ctr->cll.max_pic_average_light_level; 798 799 ret = hfi_session_set_property(inst, ptype, &hdr10); 800 if (ret) 801 return ret; 802 } 803 804 if (ctr->num_b_frames) { 805 u32 max_num_b_frames = NUM_B_FRAMES_MAX; 806 807 ptype = HFI_PROPERTY_PARAM_VENC_MAX_NUM_B_FRAMES; 808 ret = hfi_session_set_property(inst, ptype, &max_num_b_frames); 809 if (ret) 810 return ret; 811 } 812 813 ptype = HFI_PROPERTY_CONFIG_VENC_INTRA_PERIOD; 814 intra_period.pframes = ctr->num_p_frames; 815 intra_period.bframes = ctr->num_b_frames; 816 817 ret = hfi_session_set_property(inst, ptype, &intra_period); 818 if (ret) 819 return ret; 820 821 if (!ctr->rc_enable) 822 rate_control = HFI_RATE_CONTROL_OFF; 823 else if (ctr->bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) 824 rate_control = ctr->frame_skip_mode ? HFI_RATE_CONTROL_VBR_VFR : 825 HFI_RATE_CONTROL_VBR_CFR; 826 else if (ctr->bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR) 827 rate_control = ctr->frame_skip_mode ? HFI_RATE_CONTROL_CBR_VFR : 828 HFI_RATE_CONTROL_CBR_CFR; 829 else if (ctr->bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_CQ) 830 rate_control = HFI_RATE_CONTROL_CQ; 831 832 ptype = HFI_PROPERTY_PARAM_VENC_RATE_CONTROL; 833 ret = hfi_session_set_property(inst, ptype, &rate_control); 834 if (ret) 835 return ret; 836 837 if (rate_control == HFI_RATE_CONTROL_CQ && ctr->const_quality) { 838 struct hfi_heic_frame_quality quality = {}; 839 840 ptype = HFI_PROPERTY_CONFIG_HEIC_FRAME_QUALITY; 841 quality.frame_quality = ctr->const_quality; 842 ret = hfi_session_set_property(inst, ptype, &quality); 843 if (ret) 844 return ret; 845 } 846 847 if (!ctr->layer_bitrate) { 848 if (!ctr->bitrate) 849 bitrate = 64000; 850 else 851 bitrate = ctr->bitrate; 852 853 ptype = HFI_PROPERTY_CONFIG_VENC_TARGET_BITRATE; 854 brate.bitrate = bitrate; 855 brate.layer_id = 0; 856 857 ret = hfi_session_set_property(inst, ptype, &brate); 858 if (ret) 859 return ret; 860 861 if (!ctr->bitrate_peak) 862 bitrate *= 2; 863 else 864 bitrate = ctr->bitrate_peak; 865 866 ptype = HFI_PROPERTY_CONFIG_VENC_MAX_BITRATE; 867 brate.bitrate = bitrate; 868 brate.layer_id = 0; 869 870 ret = hfi_session_set_property(inst, ptype, &brate); 871 if (ret) 872 return ret; 873 } 874 875 if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_H264 || 876 inst->fmt_cap->pixfmt == V4L2_PIX_FMT_HEVC) { 877 ptype = HFI_PROPERTY_CONFIG_VENC_SYNC_FRAME_SEQUENCE_HEADER; 878 if (ctr->header_mode == V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE) 879 en.enable = 0; 880 else 881 en.enable = 1; 882 883 ret = hfi_session_set_property(inst, ptype, &en); 884 if (ret) 885 return ret; 886 } 887 888 ptype = HFI_PROPERTY_PARAM_VENC_SESSION_QP; 889 if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_HEVC) { 890 quant.qp_i = ctr->hevc_i_qp; 891 quant.qp_p = ctr->hevc_p_qp; 892 quant.qp_b = ctr->hevc_b_qp; 893 } else { 894 quant.qp_i = ctr->h264_i_qp; 895 quant.qp_p = ctr->h264_p_qp; 896 quant.qp_b = ctr->h264_b_qp; 897 } 898 quant.layer_id = 0; 899 ret = hfi_session_set_property(inst, ptype, &quant); 900 if (ret) 901 return ret; 902 903 if (inst->core->res->hfi_version == HFI_VERSION_4XX || 904 inst->core->res->hfi_version == HFI_VERSION_6XX) { 905 ptype = HFI_PROPERTY_PARAM_VENC_SESSION_QP_RANGE_V2; 906 907 if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_HEVC) { 908 quant_range_v2.min_qp.qp_packed = ctr->hevc_min_qp; 909 quant_range_v2.max_qp.qp_packed = ctr->hevc_max_qp; 910 } else if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_VP8) { 911 quant_range_v2.min_qp.qp_packed = ctr->vp8_min_qp; 912 quant_range_v2.max_qp.qp_packed = ctr->vp8_max_qp; 913 } else { 914 quant_range_v2.min_qp.qp_packed = ctr->h264_min_qp; 915 quant_range_v2.max_qp.qp_packed = ctr->h264_max_qp; 916 } 917 918 ret = hfi_session_set_property(inst, ptype, &quant_range_v2); 919 } else { 920 ptype = HFI_PROPERTY_PARAM_VENC_SESSION_QP_RANGE; 921 922 if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_HEVC) { 923 quant_range.min_qp = ctr->hevc_min_qp; 924 quant_range.max_qp = ctr->hevc_max_qp; 925 } else if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_VP8) { 926 quant_range.min_qp = ctr->vp8_min_qp; 927 quant_range.max_qp = ctr->vp8_max_qp; 928 } else { 929 quant_range.min_qp = ctr->h264_min_qp; 930 quant_range.max_qp = ctr->h264_max_qp; 931 } 932 933 quant_range.layer_id = 0; 934 ret = hfi_session_set_property(inst, ptype, &quant_range); 935 } 936 937 if (ret) 938 return ret; 939 940 ptype = HFI_PROPERTY_PARAM_VENC_LTRMODE; 941 ltr_mode.ltr_count = ctr->ltr_count; 942 ltr_mode.ltr_mode = HFI_LTR_MODE_MANUAL; 943 ltr_mode.trust_mode = 1; 944 ret = hfi_session_set_property(inst, ptype, <r_mode); 945 if (ret) 946 return ret; 947 948 switch (inst->hfi_codec) { 949 case HFI_VIDEO_CODEC_H264: 950 profile = ctr->profile.h264; 951 level = ctr->level.h264; 952 break; 953 case HFI_VIDEO_CODEC_MPEG4: 954 profile = ctr->profile.mpeg4; 955 level = ctr->level.mpeg4; 956 break; 957 case HFI_VIDEO_CODEC_VP8: 958 profile = ctr->profile.vp8; 959 level = 0; 960 break; 961 case HFI_VIDEO_CODEC_VP9: 962 profile = ctr->profile.vp9; 963 level = ctr->level.vp9; 964 break; 965 case HFI_VIDEO_CODEC_HEVC: 966 profile = ctr->profile.hevc; 967 level = ctr->level.hevc; 968 break; 969 case HFI_VIDEO_CODEC_MPEG2: 970 default: 971 profile = 0; 972 level = 0; 973 break; 974 } 975 976 ret = venus_helper_set_profile_level(inst, profile, level); 977 if (ret) 978 return ret; 979 980 if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_H264 || 981 inst->fmt_cap->pixfmt == V4L2_PIX_FMT_HEVC) { 982 struct hfi_enable en = {}; 983 984 ptype = HFI_PROPERTY_PARAM_VENC_H264_GENERATE_AUDNAL; 985 986 if (ctr->aud_enable) 987 en.enable = 1; 988 989 ret = hfi_session_set_property(inst, ptype, &en); 990 } 991 992 if ((inst->fmt_cap->pixfmt == V4L2_PIX_FMT_H264 || 993 inst->fmt_cap->pixfmt == V4L2_PIX_FMT_HEVC) && 994 (rate_control == HFI_RATE_CONTROL_CBR_VFR || 995 rate_control == HFI_RATE_CONTROL_CBR_CFR)) { 996 intra_refresh.mode = HFI_INTRA_REFRESH_NONE; 997 intra_refresh.cir_mbs = 0; 998 999 if (ctr->intra_refresh_period) { 1000 u32 mbs; 1001 1002 mbs = ALIGN(inst->width, 16) * ALIGN(inst->height, 16); 1003 mbs /= 16 * 16; 1004 if (mbs % ctr->intra_refresh_period) 1005 mbs++; 1006 mbs /= ctr->intra_refresh_period; 1007 1008 intra_refresh.cir_mbs = mbs; 1009 if (ctr->intra_refresh_type == 1010 V4L2_CID_MPEG_VIDEO_INTRA_REFRESH_PERIOD_TYPE_CYCLIC) 1011 intra_refresh.mode = HFI_INTRA_REFRESH_CYCLIC; 1012 else 1013 intra_refresh.mode = HFI_INTRA_REFRESH_RANDOM; 1014 } 1015 1016 ptype = HFI_PROPERTY_PARAM_VENC_INTRA_REFRESH; 1017 1018 ret = hfi_session_set_property(inst, ptype, &intra_refresh); 1019 if (ret) 1020 return ret; 1021 } 1022 1023 return 0; 1024 } 1025 1026 static int venc_init_session(struct venus_inst *inst) 1027 { 1028 int ret; 1029 1030 ret = venus_helper_session_init(inst); 1031 if (ret == -EALREADY) 1032 return 0; 1033 else if (ret) 1034 return ret; 1035 1036 ret = venus_helper_set_stride(inst, inst->out_width, 1037 inst->out_height); 1038 if (ret) 1039 goto deinit; 1040 1041 ret = venus_helper_set_input_resolution(inst, inst->width, 1042 inst->height); 1043 if (ret) 1044 goto deinit; 1045 1046 ret = venus_helper_set_output_resolution(inst, inst->width, 1047 inst->height, 1048 HFI_BUFFER_OUTPUT); 1049 if (ret) 1050 goto deinit; 1051 1052 ret = venus_helper_set_color_format(inst, inst->fmt_out->pixfmt); 1053 if (ret) 1054 goto deinit; 1055 1056 ret = venc_set_properties(inst); 1057 if (ret) 1058 goto deinit; 1059 1060 return 0; 1061 deinit: 1062 hfi_session_deinit(inst); 1063 return ret; 1064 } 1065 1066 static int venc_out_num_buffers(struct venus_inst *inst, unsigned int *num) 1067 { 1068 struct hfi_buffer_requirements bufreq; 1069 int ret; 1070 1071 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq); 1072 if (ret) 1073 return ret; 1074 1075 *num = bufreq.count_actual; 1076 1077 return 0; 1078 } 1079 1080 static int venc_queue_setup(struct vb2_queue *q, 1081 unsigned int *num_buffers, unsigned int *num_planes, 1082 unsigned int sizes[], struct device *alloc_devs[]) 1083 { 1084 struct venus_inst *inst = vb2_get_drv_priv(q); 1085 struct venus_core *core = inst->core; 1086 unsigned int num, min = 4; 1087 int ret; 1088 1089 if (*num_planes) { 1090 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE && 1091 *num_planes != inst->fmt_out->num_planes) 1092 return -EINVAL; 1093 1094 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE && 1095 *num_planes != inst->fmt_cap->num_planes) 1096 return -EINVAL; 1097 1098 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE && 1099 sizes[0] < inst->input_buf_size) 1100 return -EINVAL; 1101 1102 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE && 1103 sizes[0] < inst->output_buf_size) 1104 return -EINVAL; 1105 1106 return 0; 1107 } 1108 1109 if (test_bit(0, &core->sys_error)) { 1110 if (inst->nonblock) 1111 return -EAGAIN; 1112 1113 ret = wait_event_interruptible(core->sys_err_done, 1114 !test_bit(0, &core->sys_error)); 1115 if (ret) 1116 return ret; 1117 } 1118 1119 ret = venc_pm_get(inst); 1120 if (ret) 1121 return ret; 1122 1123 mutex_lock(&inst->lock); 1124 ret = venc_init_session(inst); 1125 mutex_unlock(&inst->lock); 1126 1127 if (ret) 1128 goto put_power; 1129 1130 ret = venc_pm_put(inst, false); 1131 if (ret) 1132 return ret; 1133 1134 switch (q->type) { 1135 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE: 1136 *num_planes = inst->fmt_out->num_planes; 1137 1138 ret = venc_out_num_buffers(inst, &num); 1139 if (ret) 1140 break; 1141 1142 num = max(num, min); 1143 *num_buffers = max(*num_buffers, num); 1144 inst->num_input_bufs = *num_buffers; 1145 1146 sizes[0] = venus_helper_get_framesz(inst->fmt_out->pixfmt, 1147 inst->out_width, 1148 inst->out_height); 1149 inst->input_buf_size = sizes[0]; 1150 break; 1151 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE: 1152 *num_planes = inst->fmt_cap->num_planes; 1153 *num_buffers = max(*num_buffers, min); 1154 inst->num_output_bufs = *num_buffers; 1155 sizes[0] = venus_helper_get_framesz(inst->fmt_cap->pixfmt, 1156 inst->width, 1157 inst->height); 1158 sizes[0] = max(sizes[0], inst->output_buf_size); 1159 inst->output_buf_size = sizes[0]; 1160 break; 1161 default: 1162 ret = -EINVAL; 1163 break; 1164 } 1165 1166 return ret; 1167 put_power: 1168 venc_pm_put(inst, false); 1169 return ret; 1170 } 1171 1172 static int venc_buf_init(struct vb2_buffer *vb) 1173 { 1174 struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue); 1175 1176 inst->buf_count++; 1177 1178 return venus_helper_vb2_buf_init(vb); 1179 } 1180 1181 static void venc_release_session(struct venus_inst *inst) 1182 { 1183 int ret; 1184 1185 venc_pm_get(inst); 1186 1187 mutex_lock(&inst->lock); 1188 1189 ret = hfi_session_deinit(inst); 1190 if (ret || inst->session_error) 1191 hfi_session_abort(inst); 1192 1193 mutex_unlock(&inst->lock); 1194 1195 venus_pm_load_scale(inst); 1196 INIT_LIST_HEAD(&inst->registeredbufs); 1197 venus_pm_release_core(inst); 1198 1199 venc_pm_put(inst, false); 1200 } 1201 1202 static void venc_buf_cleanup(struct vb2_buffer *vb) 1203 { 1204 struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue); 1205 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 1206 struct venus_buffer *buf = to_venus_buffer(vbuf); 1207 1208 mutex_lock(&inst->lock); 1209 if (vb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) 1210 if (!list_empty(&inst->registeredbufs)) 1211 list_del_init(&buf->reg_list); 1212 mutex_unlock(&inst->lock); 1213 1214 inst->buf_count--; 1215 if (!inst->buf_count) 1216 venc_release_session(inst); 1217 } 1218 1219 static int venc_verify_conf(struct venus_inst *inst) 1220 { 1221 enum hfi_version ver = inst->core->res->hfi_version; 1222 struct hfi_buffer_requirements bufreq; 1223 int ret; 1224 1225 if (!inst->num_input_bufs || !inst->num_output_bufs) 1226 return -EINVAL; 1227 1228 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq); 1229 if (ret) 1230 return ret; 1231 1232 if (inst->num_output_bufs < bufreq.count_actual || 1233 inst->num_output_bufs < hfi_bufreq_get_count_min(&bufreq, ver)) 1234 return -EINVAL; 1235 1236 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq); 1237 if (ret) 1238 return ret; 1239 1240 if (inst->num_input_bufs < bufreq.count_actual || 1241 inst->num_input_bufs < hfi_bufreq_get_count_min(&bufreq, ver)) 1242 return -EINVAL; 1243 1244 return 0; 1245 } 1246 1247 static int venc_start_streaming(struct vb2_queue *q, unsigned int count) 1248 { 1249 struct venus_inst *inst = vb2_get_drv_priv(q); 1250 int ret; 1251 1252 mutex_lock(&inst->lock); 1253 1254 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) 1255 inst->streamon_out = 1; 1256 else 1257 inst->streamon_cap = 1; 1258 1259 if (!(inst->streamon_out & inst->streamon_cap)) { 1260 mutex_unlock(&inst->lock); 1261 return 0; 1262 } 1263 1264 venus_helper_init_instance(inst); 1265 1266 inst->sequence_cap = 0; 1267 inst->sequence_out = 0; 1268 1269 ret = venc_pm_get(inst); 1270 if (ret) 1271 goto error; 1272 1273 ret = venus_pm_acquire_core(inst); 1274 if (ret) 1275 goto put_power; 1276 1277 ret = venc_pm_put(inst, true); 1278 if (ret) 1279 goto error; 1280 1281 ret = venc_set_properties(inst); 1282 if (ret) 1283 goto error; 1284 1285 ret = venc_verify_conf(inst); 1286 if (ret) 1287 goto error; 1288 1289 ret = venus_helper_set_num_bufs(inst, inst->num_input_bufs, 1290 inst->num_output_bufs, 0); 1291 if (ret) 1292 goto error; 1293 1294 ret = venus_helper_vb2_start_streaming(inst); 1295 if (ret) 1296 goto error; 1297 1298 inst->enc_state = VENUS_ENC_STATE_ENCODING; 1299 1300 mutex_unlock(&inst->lock); 1301 1302 return 0; 1303 1304 put_power: 1305 venc_pm_put(inst, false); 1306 error: 1307 venus_helper_buffers_done(inst, q->type, VB2_BUF_STATE_QUEUED); 1308 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) 1309 inst->streamon_out = 0; 1310 else 1311 inst->streamon_cap = 0; 1312 mutex_unlock(&inst->lock); 1313 return ret; 1314 } 1315 1316 static void venc_vb2_buf_queue(struct vb2_buffer *vb) 1317 { 1318 struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue); 1319 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 1320 1321 venc_pm_get_put(inst); 1322 1323 mutex_lock(&inst->lock); 1324 1325 if (inst->enc_state == VENUS_ENC_STATE_STOPPED) { 1326 vbuf->sequence = inst->sequence_cap++; 1327 vbuf->field = V4L2_FIELD_NONE; 1328 vb2_set_plane_payload(vb, 0, 0); 1329 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_DONE); 1330 mutex_unlock(&inst->lock); 1331 return; 1332 } 1333 1334 venus_helper_vb2_buf_queue(vb); 1335 mutex_unlock(&inst->lock); 1336 } 1337 1338 static const struct vb2_ops venc_vb2_ops = { 1339 .queue_setup = venc_queue_setup, 1340 .buf_init = venc_buf_init, 1341 .buf_cleanup = venc_buf_cleanup, 1342 .buf_prepare = venus_helper_vb2_buf_prepare, 1343 .start_streaming = venc_start_streaming, 1344 .stop_streaming = venus_helper_vb2_stop_streaming, 1345 .buf_queue = venc_vb2_buf_queue, 1346 }; 1347 1348 static void venc_buf_done(struct venus_inst *inst, unsigned int buf_type, 1349 u32 tag, u32 bytesused, u32 data_offset, u32 flags, 1350 u32 hfi_flags, u64 timestamp_us) 1351 { 1352 struct vb2_v4l2_buffer *vbuf; 1353 struct vb2_buffer *vb; 1354 unsigned int type; 1355 1356 venc_pm_touch(inst); 1357 1358 if (buf_type == HFI_BUFFER_INPUT) 1359 type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; 1360 else 1361 type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; 1362 1363 vbuf = venus_helper_find_buf(inst, type, tag); 1364 if (!vbuf) 1365 return; 1366 1367 vbuf->flags = flags; 1368 1369 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) { 1370 vb = &vbuf->vb2_buf; 1371 vb2_set_plane_payload(vb, 0, bytesused + data_offset); 1372 vb->planes[0].data_offset = data_offset; 1373 vb->timestamp = timestamp_us * NSEC_PER_USEC; 1374 vbuf->sequence = inst->sequence_cap++; 1375 if ((vbuf->flags & V4L2_BUF_FLAG_LAST) && 1376 inst->enc_state == VENUS_ENC_STATE_DRAIN) { 1377 inst->enc_state = VENUS_ENC_STATE_STOPPED; 1378 } 1379 } else { 1380 vbuf->sequence = inst->sequence_out++; 1381 } 1382 1383 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_DONE); 1384 } 1385 1386 static void venc_event_notify(struct venus_inst *inst, u32 event, 1387 struct hfi_event_data *data) 1388 { 1389 struct device *dev = inst->core->dev_enc; 1390 1391 venc_pm_touch(inst); 1392 1393 if (event == EVT_SESSION_ERROR) { 1394 inst->session_error = true; 1395 venus_helper_vb2_queue_error(inst); 1396 dev_err(dev, "enc: event session error %x\n", inst->error); 1397 } 1398 } 1399 1400 static const struct hfi_inst_ops venc_hfi_ops = { 1401 .buf_done = venc_buf_done, 1402 .event_notify = venc_event_notify, 1403 }; 1404 1405 static const struct v4l2_m2m_ops venc_m2m_ops = { 1406 .device_run = venus_helper_m2m_device_run, 1407 .job_abort = venus_helper_m2m_job_abort, 1408 }; 1409 1410 static int m2m_queue_init(void *priv, struct vb2_queue *src_vq, 1411 struct vb2_queue *dst_vq) 1412 { 1413 struct venus_inst *inst = priv; 1414 int ret; 1415 1416 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; 1417 src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF; 1418 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 1419 src_vq->ops = &venc_vb2_ops; 1420 src_vq->mem_ops = &vb2_dma_contig_memops; 1421 src_vq->drv_priv = inst; 1422 src_vq->buf_struct_size = sizeof(struct venus_buffer); 1423 src_vq->allow_zero_bytesused = 1; 1424 src_vq->min_queued_buffers = 1; 1425 src_vq->dev = inst->core->dev; 1426 src_vq->lock = &inst->ctx_q_lock; 1427 if (inst->core->res->hfi_version == HFI_VERSION_1XX) 1428 src_vq->bidirectional = 1; 1429 ret = vb2_queue_init(src_vq); 1430 if (ret) 1431 return ret; 1432 1433 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; 1434 dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF; 1435 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 1436 dst_vq->ops = &venc_vb2_ops; 1437 dst_vq->mem_ops = &vb2_dma_contig_memops; 1438 dst_vq->drv_priv = inst; 1439 dst_vq->buf_struct_size = sizeof(struct venus_buffer); 1440 dst_vq->allow_zero_bytesused = 1; 1441 dst_vq->min_queued_buffers = 1; 1442 dst_vq->dev = inst->core->dev; 1443 dst_vq->lock = &inst->ctx_q_lock; 1444 return vb2_queue_init(dst_vq); 1445 } 1446 1447 static void venc_inst_init(struct venus_inst *inst) 1448 { 1449 inst->fmt_cap = &venc_formats[VENUS_FMT_H264]; 1450 inst->fmt_out = &venc_formats[VENUS_FMT_NV12]; 1451 inst->width = 1280; 1452 inst->height = ALIGN(720, 32); 1453 inst->out_width = 1280; 1454 inst->out_height = 720; 1455 inst->fps = 15; 1456 inst->timeperframe.numerator = 1; 1457 inst->timeperframe.denominator = 15; 1458 inst->hfi_codec = HFI_VIDEO_CODEC_H264; 1459 } 1460 1461 static int venc_open(struct file *file) 1462 { 1463 struct venus_core *core = video_drvdata(file); 1464 struct venus_inst *inst; 1465 int ret; 1466 1467 inst = kzalloc(sizeof(*inst), GFP_KERNEL); 1468 if (!inst) 1469 return -ENOMEM; 1470 1471 INIT_LIST_HEAD(&inst->dpbbufs); 1472 INIT_LIST_HEAD(&inst->registeredbufs); 1473 INIT_LIST_HEAD(&inst->internalbufs); 1474 INIT_LIST_HEAD(&inst->list); 1475 mutex_init(&inst->lock); 1476 mutex_init(&inst->ctx_q_lock); 1477 1478 inst->core = core; 1479 inst->session_type = VIDC_SESSION_TYPE_ENC; 1480 inst->clk_data.core_id = VIDC_CORE_ID_DEFAULT; 1481 inst->core_acquired = false; 1482 inst->nonblock = file->f_flags & O_NONBLOCK; 1483 1484 if (inst->enc_state == VENUS_ENC_STATE_DEINIT) 1485 inst->enc_state = VENUS_ENC_STATE_INIT; 1486 1487 venus_helper_init_instance(inst); 1488 1489 ret = venc_ctrl_init(inst); 1490 if (ret) 1491 goto err_free; 1492 1493 venc_inst_init(inst); 1494 1495 /* 1496 * create m2m device for every instance, the m2m context scheduling 1497 * is made by firmware side so we do not need to care about. 1498 */ 1499 inst->m2m_dev = v4l2_m2m_init(&venc_m2m_ops); 1500 if (IS_ERR(inst->m2m_dev)) { 1501 ret = PTR_ERR(inst->m2m_dev); 1502 goto err_ctrl_deinit; 1503 } 1504 1505 inst->m2m_ctx = v4l2_m2m_ctx_init(inst->m2m_dev, inst, m2m_queue_init); 1506 if (IS_ERR(inst->m2m_ctx)) { 1507 ret = PTR_ERR(inst->m2m_ctx); 1508 goto err_m2m_dev_release; 1509 } 1510 1511 ret = hfi_session_create(inst, &venc_hfi_ops); 1512 if (ret) 1513 goto err_m2m_ctx_release; 1514 1515 v4l2_fh_init(&inst->fh, core->vdev_enc); 1516 1517 inst->fh.ctrl_handler = &inst->ctrl_handler; 1518 v4l2_fh_add(&inst->fh); 1519 inst->fh.m2m_ctx = inst->m2m_ctx; 1520 file->private_data = &inst->fh; 1521 1522 return 0; 1523 1524 err_m2m_ctx_release: 1525 v4l2_m2m_ctx_release(inst->m2m_ctx); 1526 err_m2m_dev_release: 1527 v4l2_m2m_release(inst->m2m_dev); 1528 err_ctrl_deinit: 1529 v4l2_ctrl_handler_free(&inst->ctrl_handler); 1530 err_free: 1531 kfree(inst); 1532 return ret; 1533 } 1534 1535 static int venc_close(struct file *file) 1536 { 1537 struct venus_inst *inst = to_inst(file); 1538 1539 venc_pm_get(inst); 1540 venus_close_common(inst); 1541 inst->enc_state = VENUS_ENC_STATE_DEINIT; 1542 venc_pm_put(inst, false); 1543 1544 kfree(inst); 1545 return 0; 1546 } 1547 1548 static const struct v4l2_file_operations venc_fops = { 1549 .owner = THIS_MODULE, 1550 .open = venc_open, 1551 .release = venc_close, 1552 .unlocked_ioctl = video_ioctl2, 1553 .poll = v4l2_m2m_fop_poll, 1554 .mmap = v4l2_m2m_fop_mmap, 1555 }; 1556 1557 static int venc_probe(struct platform_device *pdev) 1558 { 1559 struct device *dev = &pdev->dev; 1560 struct video_device *vdev; 1561 struct venus_core *core; 1562 int ret; 1563 1564 if (!dev->parent) 1565 return -EPROBE_DEFER; 1566 1567 core = dev_get_drvdata(dev->parent); 1568 if (!core) 1569 return -EPROBE_DEFER; 1570 1571 platform_set_drvdata(pdev, core); 1572 1573 if (core->pm_ops->venc_get) { 1574 ret = core->pm_ops->venc_get(dev); 1575 if (ret) 1576 return ret; 1577 } 1578 1579 vdev = video_device_alloc(); 1580 if (!vdev) 1581 return -ENOMEM; 1582 1583 strscpy(vdev->name, "qcom-venus-encoder", sizeof(vdev->name)); 1584 vdev->release = video_device_release; 1585 vdev->fops = &venc_fops; 1586 vdev->ioctl_ops = &venc_ioctl_ops; 1587 vdev->vfl_dir = VFL_DIR_M2M; 1588 vdev->v4l2_dev = &core->v4l2_dev; 1589 vdev->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING; 1590 1591 ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1); 1592 if (ret) 1593 goto err_vdev_release; 1594 1595 core->vdev_enc = vdev; 1596 core->dev_enc = dev; 1597 1598 video_set_drvdata(vdev, core); 1599 pm_runtime_set_autosuspend_delay(dev, 2000); 1600 pm_runtime_use_autosuspend(dev); 1601 pm_runtime_enable(dev); 1602 1603 return 0; 1604 1605 err_vdev_release: 1606 video_device_release(vdev); 1607 return ret; 1608 } 1609 1610 static void venc_remove(struct platform_device *pdev) 1611 { 1612 struct venus_core *core = dev_get_drvdata(pdev->dev.parent); 1613 1614 video_unregister_device(core->vdev_enc); 1615 pm_runtime_disable(core->dev_enc); 1616 1617 if (core->pm_ops->venc_put) 1618 core->pm_ops->venc_put(core->dev_enc); 1619 } 1620 1621 static __maybe_unused int venc_runtime_suspend(struct device *dev) 1622 { 1623 struct venus_core *core = dev_get_drvdata(dev); 1624 const struct venus_pm_ops *pm_ops = core->pm_ops; 1625 int ret = 0; 1626 1627 if (pm_ops->venc_power) 1628 ret = pm_ops->venc_power(dev, POWER_OFF); 1629 1630 return ret; 1631 } 1632 1633 static __maybe_unused int venc_runtime_resume(struct device *dev) 1634 { 1635 struct venus_core *core = dev_get_drvdata(dev); 1636 const struct venus_pm_ops *pm_ops = core->pm_ops; 1637 int ret = 0; 1638 1639 if (pm_ops->venc_power) 1640 ret = pm_ops->venc_power(dev, POWER_ON); 1641 1642 return ret; 1643 } 1644 1645 static const struct dev_pm_ops venc_pm_ops = { 1646 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, 1647 pm_runtime_force_resume) 1648 SET_RUNTIME_PM_OPS(venc_runtime_suspend, venc_runtime_resume, NULL) 1649 }; 1650 1651 static const struct of_device_id venc_dt_match[] = { 1652 { .compatible = "venus-encoder" }, 1653 { } 1654 }; 1655 MODULE_DEVICE_TABLE(of, venc_dt_match); 1656 1657 static struct platform_driver qcom_venus_enc_driver = { 1658 .probe = venc_probe, 1659 .remove = venc_remove, 1660 .driver = { 1661 .name = "qcom-venus-encoder", 1662 .of_match_table = venc_dt_match, 1663 .pm = &venc_pm_ops, 1664 }, 1665 }; 1666 module_platform_driver(qcom_venus_enc_driver); 1667 1668 MODULE_ALIAS("platform:qcom-venus-encoder"); 1669 MODULE_DESCRIPTION("Qualcomm Venus video encoder driver"); 1670 MODULE_LICENSE("GPL v2"); 1671