1 /* 2 * Copyright (c) 2012-2016, The Linux Foundation. All rights reserved. 3 * Copyright (C) 2017 Linaro Ltd. 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License version 2 and 7 * only version 2 as published by the Free Software Foundation. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 */ 15 #include <linux/clk.h> 16 #include <linux/module.h> 17 #include <linux/mod_devicetable.h> 18 #include <linux/platform_device.h> 19 #include <linux/pm_runtime.h> 20 #include <linux/slab.h> 21 #include <media/v4l2-ioctl.h> 22 #include <media/v4l2-event.h> 23 #include <media/v4l2-ctrls.h> 24 #include <media/v4l2-mem2mem.h> 25 #include <media/videobuf2-dma-sg.h> 26 27 #include "hfi_venus_io.h" 28 #include "hfi_parser.h" 29 #include "core.h" 30 #include "helpers.h" 31 #include "vdec.h" 32 33 /* 34 * Three resons to keep MPLANE formats (despite that the number of planes 35 * currently is one): 36 * - the MPLANE formats allow only one plane to be used 37 * - the downstream driver use MPLANE formats too 38 * - future firmware versions could add support for >1 planes 39 */ 40 static const struct venus_format vdec_formats[] = { 41 { 42 .pixfmt = V4L2_PIX_FMT_NV12, 43 .num_planes = 1, 44 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, 45 }, { 46 .pixfmt = V4L2_PIX_FMT_MPEG4, 47 .num_planes = 1, 48 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, 49 }, { 50 .pixfmt = V4L2_PIX_FMT_MPEG2, 51 .num_planes = 1, 52 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, 53 }, { 54 .pixfmt = V4L2_PIX_FMT_H263, 55 .num_planes = 1, 56 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, 57 }, { 58 .pixfmt = V4L2_PIX_FMT_VC1_ANNEX_G, 59 .num_planes = 1, 60 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, 61 }, { 62 .pixfmt = V4L2_PIX_FMT_VC1_ANNEX_L, 63 .num_planes = 1, 64 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, 65 }, { 66 .pixfmt = V4L2_PIX_FMT_H264, 67 .num_planes = 1, 68 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, 69 }, { 70 .pixfmt = V4L2_PIX_FMT_VP8, 71 .num_planes = 1, 72 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, 73 }, { 74 .pixfmt = V4L2_PIX_FMT_VP9, 75 .num_planes = 1, 76 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, 77 }, { 78 .pixfmt = V4L2_PIX_FMT_XVID, 79 .num_planes = 1, 80 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, 81 }, { 82 .pixfmt = V4L2_PIX_FMT_HEVC, 83 .num_planes = 1, 84 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, 85 }, 86 }; 87 88 static const struct venus_format * 89 find_format(struct venus_inst *inst, u32 pixfmt, u32 type) 90 { 91 const struct venus_format *fmt = vdec_formats; 92 unsigned int size = ARRAY_SIZE(vdec_formats); 93 unsigned int i; 94 95 for (i = 0; i < size; i++) { 96 if (fmt[i].pixfmt == pixfmt) 97 break; 98 } 99 100 if (i == size || fmt[i].type != type) 101 return NULL; 102 103 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE && 104 !venus_helper_check_codec(inst, fmt[i].pixfmt)) 105 return NULL; 106 107 return &fmt[i]; 108 } 109 110 static const struct venus_format * 111 find_format_by_index(struct venus_inst *inst, unsigned int index, u32 type) 112 { 113 const struct venus_format *fmt = vdec_formats; 114 unsigned int size = ARRAY_SIZE(vdec_formats); 115 unsigned int i, k = 0; 116 117 if (index > size) 118 return NULL; 119 120 for (i = 0; i < size; i++) { 121 bool valid; 122 123 if (fmt[i].type != type) 124 continue; 125 valid = type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE || 126 venus_helper_check_codec(inst, fmt[i].pixfmt); 127 if (k == index && valid) 128 break; 129 if (valid) 130 k++; 131 } 132 133 if (i == size) 134 return NULL; 135 136 return &fmt[i]; 137 } 138 139 static const struct venus_format * 140 vdec_try_fmt_common(struct venus_inst *inst, struct v4l2_format *f) 141 { 142 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp; 143 struct v4l2_plane_pix_format *pfmt = pixmp->plane_fmt; 144 const struct venus_format *fmt; 145 146 memset(pfmt[0].reserved, 0, sizeof(pfmt[0].reserved)); 147 memset(pixmp->reserved, 0, sizeof(pixmp->reserved)); 148 149 fmt = find_format(inst, pixmp->pixelformat, f->type); 150 if (!fmt) { 151 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) 152 pixmp->pixelformat = V4L2_PIX_FMT_NV12; 153 else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) 154 pixmp->pixelformat = V4L2_PIX_FMT_H264; 155 else 156 return NULL; 157 fmt = find_format(inst, pixmp->pixelformat, f->type); 158 } 159 160 pixmp->width = clamp(pixmp->width, frame_width_min(inst), 161 frame_width_max(inst)); 162 pixmp->height = clamp(pixmp->height, frame_height_min(inst), 163 frame_height_max(inst)); 164 165 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) 166 pixmp->height = ALIGN(pixmp->height, 32); 167 168 if (pixmp->field == V4L2_FIELD_ANY) 169 pixmp->field = V4L2_FIELD_NONE; 170 pixmp->num_planes = fmt->num_planes; 171 pixmp->flags = 0; 172 173 pfmt[0].sizeimage = venus_helper_get_framesz(pixmp->pixelformat, 174 pixmp->width, 175 pixmp->height); 176 177 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) 178 pfmt[0].bytesperline = ALIGN(pixmp->width, 128); 179 else 180 pfmt[0].bytesperline = 0; 181 182 return fmt; 183 } 184 185 static int vdec_try_fmt(struct file *file, void *fh, struct v4l2_format *f) 186 { 187 struct venus_inst *inst = to_inst(file); 188 189 vdec_try_fmt_common(inst, f); 190 191 return 0; 192 } 193 194 static int vdec_g_fmt(struct file *file, void *fh, struct v4l2_format *f) 195 { 196 struct venus_inst *inst = to_inst(file); 197 const struct venus_format *fmt = NULL; 198 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp; 199 200 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) 201 fmt = inst->fmt_cap; 202 else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) 203 fmt = inst->fmt_out; 204 205 if (inst->reconfig) { 206 struct v4l2_format format = {}; 207 208 inst->out_width = inst->reconfig_width; 209 inst->out_height = inst->reconfig_height; 210 inst->reconfig = false; 211 212 format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; 213 format.fmt.pix_mp.pixelformat = inst->fmt_cap->pixfmt; 214 format.fmt.pix_mp.width = inst->out_width; 215 format.fmt.pix_mp.height = inst->out_height; 216 217 vdec_try_fmt_common(inst, &format); 218 219 inst->width = format.fmt.pix_mp.width; 220 inst->height = format.fmt.pix_mp.height; 221 } 222 223 pixmp->pixelformat = fmt->pixfmt; 224 225 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) { 226 pixmp->width = inst->width; 227 pixmp->height = inst->height; 228 pixmp->colorspace = inst->colorspace; 229 pixmp->ycbcr_enc = inst->ycbcr_enc; 230 pixmp->quantization = inst->quantization; 231 pixmp->xfer_func = inst->xfer_func; 232 } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) { 233 pixmp->width = inst->out_width; 234 pixmp->height = inst->out_height; 235 } 236 237 vdec_try_fmt_common(inst, f); 238 239 return 0; 240 } 241 242 static int vdec_s_fmt(struct file *file, void *fh, struct v4l2_format *f) 243 { 244 struct venus_inst *inst = to_inst(file); 245 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp; 246 struct v4l2_pix_format_mplane orig_pixmp; 247 const struct venus_format *fmt; 248 struct v4l2_format format; 249 u32 pixfmt_out = 0, pixfmt_cap = 0; 250 251 orig_pixmp = *pixmp; 252 253 fmt = vdec_try_fmt_common(inst, f); 254 255 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) { 256 pixfmt_out = pixmp->pixelformat; 257 pixfmt_cap = inst->fmt_cap->pixfmt; 258 } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) { 259 pixfmt_cap = pixmp->pixelformat; 260 pixfmt_out = inst->fmt_out->pixfmt; 261 } 262 263 memset(&format, 0, sizeof(format)); 264 265 format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; 266 format.fmt.pix_mp.pixelformat = pixfmt_out; 267 format.fmt.pix_mp.width = orig_pixmp.width; 268 format.fmt.pix_mp.height = orig_pixmp.height; 269 vdec_try_fmt_common(inst, &format); 270 271 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) { 272 inst->out_width = format.fmt.pix_mp.width; 273 inst->out_height = format.fmt.pix_mp.height; 274 inst->colorspace = pixmp->colorspace; 275 inst->ycbcr_enc = pixmp->ycbcr_enc; 276 inst->quantization = pixmp->quantization; 277 inst->xfer_func = pixmp->xfer_func; 278 } 279 280 memset(&format, 0, sizeof(format)); 281 282 format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; 283 format.fmt.pix_mp.pixelformat = pixfmt_cap; 284 format.fmt.pix_mp.width = orig_pixmp.width; 285 format.fmt.pix_mp.height = orig_pixmp.height; 286 vdec_try_fmt_common(inst, &format); 287 288 inst->width = format.fmt.pix_mp.width; 289 inst->height = format.fmt.pix_mp.height; 290 291 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) 292 inst->fmt_out = fmt; 293 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) 294 inst->fmt_cap = fmt; 295 296 return 0; 297 } 298 299 static int 300 vdec_g_selection(struct file *file, void *fh, struct v4l2_selection *s) 301 { 302 struct venus_inst *inst = to_inst(file); 303 304 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE && 305 s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) 306 return -EINVAL; 307 308 switch (s->target) { 309 case V4L2_SEL_TGT_CROP_BOUNDS: 310 case V4L2_SEL_TGT_CROP_DEFAULT: 311 case V4L2_SEL_TGT_CROP: 312 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) 313 return -EINVAL; 314 s->r.width = inst->out_width; 315 s->r.height = inst->out_height; 316 break; 317 case V4L2_SEL_TGT_COMPOSE_BOUNDS: 318 case V4L2_SEL_TGT_COMPOSE_PADDED: 319 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 320 return -EINVAL; 321 s->r.width = inst->width; 322 s->r.height = inst->height; 323 break; 324 case V4L2_SEL_TGT_COMPOSE_DEFAULT: 325 case V4L2_SEL_TGT_COMPOSE: 326 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 327 return -EINVAL; 328 s->r.width = inst->out_width; 329 s->r.height = inst->out_height; 330 break; 331 default: 332 return -EINVAL; 333 } 334 335 s->r.top = 0; 336 s->r.left = 0; 337 338 return 0; 339 } 340 341 static int 342 vdec_querycap(struct file *file, void *fh, struct v4l2_capability *cap) 343 { 344 strlcpy(cap->driver, "qcom-venus", sizeof(cap->driver)); 345 strlcpy(cap->card, "Qualcomm Venus video decoder", sizeof(cap->card)); 346 strlcpy(cap->bus_info, "platform:qcom-venus", sizeof(cap->bus_info)); 347 348 return 0; 349 } 350 351 static int vdec_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f) 352 { 353 struct venus_inst *inst = to_inst(file); 354 const struct venus_format *fmt; 355 356 memset(f->reserved, 0, sizeof(f->reserved)); 357 358 fmt = find_format_by_index(inst, f->index, f->type); 359 if (!fmt) 360 return -EINVAL; 361 362 f->pixelformat = fmt->pixfmt; 363 364 return 0; 365 } 366 367 static int vdec_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a) 368 { 369 struct venus_inst *inst = to_inst(file); 370 struct v4l2_captureparm *cap = &a->parm.capture; 371 struct v4l2_fract *timeperframe = &cap->timeperframe; 372 u64 us_per_frame, fps; 373 374 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE && 375 a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) 376 return -EINVAL; 377 378 memset(cap->reserved, 0, sizeof(cap->reserved)); 379 if (!timeperframe->denominator) 380 timeperframe->denominator = inst->timeperframe.denominator; 381 if (!timeperframe->numerator) 382 timeperframe->numerator = inst->timeperframe.numerator; 383 cap->readbuffers = 0; 384 cap->extendedmode = 0; 385 cap->capability = V4L2_CAP_TIMEPERFRAME; 386 us_per_frame = timeperframe->numerator * (u64)USEC_PER_SEC; 387 do_div(us_per_frame, timeperframe->denominator); 388 389 if (!us_per_frame) 390 return -EINVAL; 391 392 fps = (u64)USEC_PER_SEC; 393 do_div(fps, us_per_frame); 394 395 inst->fps = fps; 396 inst->timeperframe = *timeperframe; 397 398 return 0; 399 } 400 401 static int vdec_enum_framesizes(struct file *file, void *fh, 402 struct v4l2_frmsizeenum *fsize) 403 { 404 struct venus_inst *inst = to_inst(file); 405 const struct venus_format *fmt; 406 407 fmt = find_format(inst, fsize->pixel_format, 408 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE); 409 if (!fmt) { 410 fmt = find_format(inst, fsize->pixel_format, 411 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE); 412 if (!fmt) 413 return -EINVAL; 414 } 415 416 if (fsize->index) 417 return -EINVAL; 418 419 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE; 420 421 fsize->stepwise.min_width = frame_width_min(inst); 422 fsize->stepwise.max_width = frame_width_max(inst); 423 fsize->stepwise.step_width = frame_width_step(inst); 424 fsize->stepwise.min_height = frame_height_min(inst); 425 fsize->stepwise.max_height = frame_height_max(inst); 426 fsize->stepwise.step_height = frame_height_step(inst); 427 428 return 0; 429 } 430 431 static int vdec_subscribe_event(struct v4l2_fh *fh, 432 const struct v4l2_event_subscription *sub) 433 { 434 switch (sub->type) { 435 case V4L2_EVENT_EOS: 436 return v4l2_event_subscribe(fh, sub, 2, NULL); 437 case V4L2_EVENT_SOURCE_CHANGE: 438 return v4l2_src_change_event_subscribe(fh, sub); 439 case V4L2_EVENT_CTRL: 440 return v4l2_ctrl_subscribe_event(fh, sub); 441 default: 442 return -EINVAL; 443 } 444 } 445 446 static int 447 vdec_try_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *cmd) 448 { 449 switch (cmd->cmd) { 450 case V4L2_DEC_CMD_STOP: 451 if (cmd->flags & V4L2_DEC_CMD_STOP_TO_BLACK) 452 return -EINVAL; 453 break; 454 default: 455 return -EINVAL; 456 } 457 458 return 0; 459 } 460 461 static int 462 vdec_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *cmd) 463 { 464 struct venus_inst *inst = to_inst(file); 465 struct hfi_frame_data fdata = {0}; 466 int ret; 467 468 ret = vdec_try_decoder_cmd(file, fh, cmd); 469 if (ret) 470 return ret; 471 472 mutex_lock(&inst->lock); 473 474 /* 475 * Implement V4L2_DEC_CMD_STOP by enqueue an empty buffer on decoder 476 * input to signal EOS. 477 */ 478 if (!(inst->streamon_out & inst->streamon_cap)) 479 goto unlock; 480 481 fdata.buffer_type = HFI_BUFFER_INPUT; 482 fdata.flags |= HFI_BUFFERFLAG_EOS; 483 fdata.device_addr = 0xdeadbeef; 484 485 ret = hfi_session_process_buf(inst, &fdata); 486 487 unlock: 488 mutex_unlock(&inst->lock); 489 return ret; 490 } 491 492 static const struct v4l2_ioctl_ops vdec_ioctl_ops = { 493 .vidioc_querycap = vdec_querycap, 494 .vidioc_enum_fmt_vid_cap_mplane = vdec_enum_fmt, 495 .vidioc_enum_fmt_vid_out_mplane = vdec_enum_fmt, 496 .vidioc_s_fmt_vid_cap_mplane = vdec_s_fmt, 497 .vidioc_s_fmt_vid_out_mplane = vdec_s_fmt, 498 .vidioc_g_fmt_vid_cap_mplane = vdec_g_fmt, 499 .vidioc_g_fmt_vid_out_mplane = vdec_g_fmt, 500 .vidioc_try_fmt_vid_cap_mplane = vdec_try_fmt, 501 .vidioc_try_fmt_vid_out_mplane = vdec_try_fmt, 502 .vidioc_g_selection = vdec_g_selection, 503 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs, 504 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf, 505 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs, 506 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf, 507 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf, 508 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf, 509 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf, 510 .vidioc_streamon = v4l2_m2m_ioctl_streamon, 511 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff, 512 .vidioc_s_parm = vdec_s_parm, 513 .vidioc_enum_framesizes = vdec_enum_framesizes, 514 .vidioc_subscribe_event = vdec_subscribe_event, 515 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 516 .vidioc_try_decoder_cmd = vdec_try_decoder_cmd, 517 .vidioc_decoder_cmd = vdec_decoder_cmd, 518 }; 519 520 static int vdec_set_properties(struct venus_inst *inst) 521 { 522 struct vdec_controls *ctr = &inst->controls.dec; 523 struct hfi_enable en = { .enable = 1 }; 524 u32 ptype; 525 int ret; 526 527 if (ctr->post_loop_deb_mode) { 528 ptype = HFI_PROPERTY_CONFIG_VDEC_POST_LOOP_DEBLOCKER; 529 ret = hfi_session_set_property(inst, ptype, &en); 530 if (ret) 531 return ret; 532 } 533 534 return 0; 535 } 536 537 #define is_ubwc_fmt(fmt) (!!((fmt) & HFI_COLOR_FORMAT_UBWC_BASE)) 538 539 static int vdec_output_conf(struct venus_inst *inst) 540 { 541 struct venus_core *core = inst->core; 542 struct hfi_enable en = { .enable = 1 }; 543 u32 width = inst->out_width; 544 u32 height = inst->out_height; 545 u32 out_fmt, out2_fmt; 546 bool ubwc = false; 547 u32 ptype; 548 int ret; 549 550 ret = venus_helper_set_work_mode(inst, VIDC_WORK_MODE_2); 551 if (ret) 552 return ret; 553 554 ret = venus_helper_set_core_usage(inst, VIDC_CORE_ID_1); 555 if (ret) 556 return ret; 557 558 if (core->res->hfi_version == HFI_VERSION_1XX) { 559 ptype = HFI_PROPERTY_PARAM_VDEC_CONTINUE_DATA_TRANSFER; 560 ret = hfi_session_set_property(inst, ptype, &en); 561 if (ret) 562 return ret; 563 } 564 565 /* Force searching UBWC formats for bigger then HD resolutions */ 566 if (width > 1920 && height > ALIGN(1080, 32)) 567 ubwc = true; 568 569 /* For Venus v4 UBWC format is mandatory */ 570 if (IS_V4(core)) 571 ubwc = true; 572 573 ret = venus_helper_get_out_fmts(inst, inst->fmt_cap->pixfmt, &out_fmt, 574 &out2_fmt, ubwc); 575 if (ret) 576 return ret; 577 578 inst->output_buf_size = 579 venus_helper_get_framesz_raw(out_fmt, width, height); 580 inst->output2_buf_size = 581 venus_helper_get_framesz_raw(out2_fmt, width, height); 582 583 if (is_ubwc_fmt(out_fmt)) { 584 inst->opb_buftype = HFI_BUFFER_OUTPUT2; 585 inst->opb_fmt = out2_fmt; 586 inst->dpb_buftype = HFI_BUFFER_OUTPUT; 587 inst->dpb_fmt = out_fmt; 588 } else if (is_ubwc_fmt(out2_fmt)) { 589 inst->opb_buftype = HFI_BUFFER_OUTPUT; 590 inst->opb_fmt = out_fmt; 591 inst->dpb_buftype = HFI_BUFFER_OUTPUT2; 592 inst->dpb_fmt = out2_fmt; 593 } else { 594 inst->opb_buftype = HFI_BUFFER_OUTPUT; 595 inst->opb_fmt = out_fmt; 596 inst->dpb_buftype = 0; 597 inst->dpb_fmt = 0; 598 } 599 600 ret = venus_helper_set_raw_format(inst, inst->opb_fmt, 601 inst->opb_buftype); 602 if (ret) 603 return ret; 604 605 if (inst->dpb_fmt) { 606 ret = venus_helper_set_multistream(inst, false, true); 607 if (ret) 608 return ret; 609 610 ret = venus_helper_set_raw_format(inst, inst->dpb_fmt, 611 inst->dpb_buftype); 612 if (ret) 613 return ret; 614 615 ret = venus_helper_set_output_resolution(inst, width, height, 616 HFI_BUFFER_OUTPUT2); 617 if (ret) 618 return ret; 619 } 620 621 if (IS_V3(core) || IS_V4(core)) { 622 if (inst->output2_buf_size) { 623 ret = venus_helper_set_bufsize(inst, 624 inst->output2_buf_size, 625 HFI_BUFFER_OUTPUT2); 626 if (ret) 627 return ret; 628 } 629 630 if (inst->output_buf_size) { 631 ret = venus_helper_set_bufsize(inst, 632 inst->output_buf_size, 633 HFI_BUFFER_OUTPUT); 634 if (ret) 635 return ret; 636 } 637 } 638 639 ret = venus_helper_set_dyn_bufmode(inst); 640 if (ret) 641 return ret; 642 643 return 0; 644 } 645 646 static int vdec_init_session(struct venus_inst *inst) 647 { 648 int ret; 649 650 ret = hfi_session_init(inst, inst->fmt_out->pixfmt); 651 if (ret) 652 return ret; 653 654 ret = venus_helper_set_input_resolution(inst, inst->out_width, 655 inst->out_height); 656 if (ret) 657 goto deinit; 658 659 ret = venus_helper_set_color_format(inst, inst->fmt_cap->pixfmt); 660 if (ret) 661 goto deinit; 662 663 return 0; 664 deinit: 665 hfi_session_deinit(inst); 666 return ret; 667 } 668 669 static int vdec_num_buffers(struct venus_inst *inst, unsigned int *in_num, 670 unsigned int *out_num) 671 { 672 enum hfi_version ver = inst->core->res->hfi_version; 673 struct hfi_buffer_requirements bufreq; 674 int ret; 675 676 *in_num = *out_num = 0; 677 678 ret = vdec_init_session(inst); 679 if (ret) 680 return ret; 681 682 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq); 683 if (ret) 684 goto deinit; 685 686 *in_num = HFI_BUFREQ_COUNT_MIN(&bufreq, ver); 687 688 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq); 689 if (ret) 690 goto deinit; 691 692 *out_num = HFI_BUFREQ_COUNT_MIN(&bufreq, ver); 693 694 deinit: 695 hfi_session_deinit(inst); 696 697 return ret; 698 } 699 700 static int vdec_queue_setup(struct vb2_queue *q, 701 unsigned int *num_buffers, unsigned int *num_planes, 702 unsigned int sizes[], struct device *alloc_devs[]) 703 { 704 struct venus_inst *inst = vb2_get_drv_priv(q); 705 unsigned int in_num, out_num; 706 int ret = 0; 707 708 if (*num_planes) { 709 unsigned int output_buf_size = venus_helper_get_opb_size(inst); 710 711 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE && 712 *num_planes != inst->fmt_out->num_planes) 713 return -EINVAL; 714 715 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE && 716 *num_planes != inst->fmt_cap->num_planes) 717 return -EINVAL; 718 719 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE && 720 sizes[0] < inst->input_buf_size) 721 return -EINVAL; 722 723 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE && 724 sizes[0] < output_buf_size) 725 return -EINVAL; 726 727 return 0; 728 } 729 730 ret = vdec_num_buffers(inst, &in_num, &out_num); 731 if (ret) 732 return ret; 733 734 switch (q->type) { 735 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE: 736 *num_planes = inst->fmt_out->num_planes; 737 sizes[0] = venus_helper_get_framesz(inst->fmt_out->pixfmt, 738 inst->out_width, 739 inst->out_height); 740 inst->input_buf_size = sizes[0]; 741 *num_buffers = max(*num_buffers, in_num); 742 inst->num_input_bufs = *num_buffers; 743 inst->num_output_bufs = out_num; 744 break; 745 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE: 746 *num_planes = inst->fmt_cap->num_planes; 747 sizes[0] = venus_helper_get_framesz(inst->fmt_cap->pixfmt, 748 inst->width, 749 inst->height); 750 inst->output_buf_size = sizes[0]; 751 *num_buffers = max(*num_buffers, out_num); 752 inst->num_output_bufs = *num_buffers; 753 break; 754 default: 755 ret = -EINVAL; 756 break; 757 } 758 759 return ret; 760 } 761 762 static int vdec_verify_conf(struct venus_inst *inst) 763 { 764 enum hfi_version ver = inst->core->res->hfi_version; 765 struct hfi_buffer_requirements bufreq; 766 int ret; 767 768 if (!inst->num_input_bufs || !inst->num_output_bufs) 769 return -EINVAL; 770 771 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq); 772 if (ret) 773 return ret; 774 775 if (inst->num_output_bufs < bufreq.count_actual || 776 inst->num_output_bufs < HFI_BUFREQ_COUNT_MIN(&bufreq, ver)) 777 return -EINVAL; 778 779 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq); 780 if (ret) 781 return ret; 782 783 if (inst->num_input_bufs < HFI_BUFREQ_COUNT_MIN(&bufreq, ver)) 784 return -EINVAL; 785 786 return 0; 787 } 788 789 static int vdec_start_streaming(struct vb2_queue *q, unsigned int count) 790 { 791 struct venus_inst *inst = vb2_get_drv_priv(q); 792 int ret; 793 794 mutex_lock(&inst->lock); 795 796 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) 797 inst->streamon_out = 1; 798 else 799 inst->streamon_cap = 1; 800 801 if (!(inst->streamon_out & inst->streamon_cap)) { 802 mutex_unlock(&inst->lock); 803 return 0; 804 } 805 806 venus_helper_init_instance(inst); 807 808 inst->reconfig = false; 809 inst->sequence_cap = 0; 810 inst->sequence_out = 0; 811 812 ret = vdec_init_session(inst); 813 if (ret) 814 goto bufs_done; 815 816 ret = vdec_set_properties(inst); 817 if (ret) 818 goto deinit_sess; 819 820 ret = vdec_output_conf(inst); 821 if (ret) 822 goto deinit_sess; 823 824 ret = vdec_verify_conf(inst); 825 if (ret) 826 goto deinit_sess; 827 828 ret = venus_helper_set_num_bufs(inst, inst->num_input_bufs, 829 VB2_MAX_FRAME, VB2_MAX_FRAME); 830 if (ret) 831 goto deinit_sess; 832 833 ret = venus_helper_alloc_dpb_bufs(inst); 834 if (ret) 835 goto deinit_sess; 836 837 ret = venus_helper_vb2_start_streaming(inst); 838 if (ret) 839 goto deinit_sess; 840 841 mutex_unlock(&inst->lock); 842 843 return 0; 844 845 deinit_sess: 846 hfi_session_deinit(inst); 847 bufs_done: 848 venus_helper_buffers_done(inst, VB2_BUF_STATE_QUEUED); 849 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) 850 inst->streamon_out = 0; 851 else 852 inst->streamon_cap = 0; 853 mutex_unlock(&inst->lock); 854 return ret; 855 } 856 857 static const struct vb2_ops vdec_vb2_ops = { 858 .queue_setup = vdec_queue_setup, 859 .buf_init = venus_helper_vb2_buf_init, 860 .buf_prepare = venus_helper_vb2_buf_prepare, 861 .start_streaming = vdec_start_streaming, 862 .stop_streaming = venus_helper_vb2_stop_streaming, 863 .buf_queue = venus_helper_vb2_buf_queue, 864 }; 865 866 static void vdec_buf_done(struct venus_inst *inst, unsigned int buf_type, 867 u32 tag, u32 bytesused, u32 data_offset, u32 flags, 868 u32 hfi_flags, u64 timestamp_us) 869 { 870 enum vb2_buffer_state state = VB2_BUF_STATE_DONE; 871 struct vb2_v4l2_buffer *vbuf; 872 struct vb2_buffer *vb; 873 unsigned int type; 874 875 if (buf_type == HFI_BUFFER_INPUT) 876 type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; 877 else 878 type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; 879 880 vbuf = venus_helper_find_buf(inst, type, tag); 881 if (!vbuf) 882 return; 883 884 vbuf->flags = flags; 885 vbuf->field = V4L2_FIELD_NONE; 886 887 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) { 888 unsigned int opb_sz = venus_helper_get_opb_size(inst); 889 890 vb = &vbuf->vb2_buf; 891 vb->planes[0].bytesused = 892 max_t(unsigned int, opb_sz, bytesused); 893 vb->planes[0].data_offset = data_offset; 894 vb->timestamp = timestamp_us * NSEC_PER_USEC; 895 vbuf->sequence = inst->sequence_cap++; 896 897 if (vbuf->flags & V4L2_BUF_FLAG_LAST) { 898 const struct v4l2_event ev = { .type = V4L2_EVENT_EOS }; 899 900 v4l2_event_queue_fh(&inst->fh, &ev); 901 } 902 } else { 903 vbuf->sequence = inst->sequence_out++; 904 } 905 906 if (hfi_flags & HFI_BUFFERFLAG_READONLY) 907 venus_helper_acquire_buf_ref(vbuf); 908 909 if (hfi_flags & HFI_BUFFERFLAG_DATACORRUPT) 910 state = VB2_BUF_STATE_ERROR; 911 912 v4l2_m2m_buf_done(vbuf, state); 913 } 914 915 static void vdec_event_notify(struct venus_inst *inst, u32 event, 916 struct hfi_event_data *data) 917 { 918 struct venus_core *core = inst->core; 919 struct device *dev = core->dev_dec; 920 static const struct v4l2_event ev = { 921 .type = V4L2_EVENT_SOURCE_CHANGE, 922 .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION }; 923 924 switch (event) { 925 case EVT_SESSION_ERROR: 926 inst->session_error = true; 927 dev_err(dev, "dec: event session error %x\n", inst->error); 928 break; 929 case EVT_SYS_EVENT_CHANGE: 930 switch (data->event_type) { 931 case HFI_EVENT_DATA_SEQUENCE_CHANGED_SUFFICIENT_BUF_RESOURCES: 932 hfi_session_continue(inst); 933 dev_dbg(dev, "event sufficient resources\n"); 934 break; 935 case HFI_EVENT_DATA_SEQUENCE_CHANGED_INSUFFICIENT_BUF_RESOURCES: 936 inst->reconfig_height = data->height; 937 inst->reconfig_width = data->width; 938 inst->reconfig = true; 939 940 v4l2_event_queue_fh(&inst->fh, &ev); 941 942 dev_dbg(dev, "event not sufficient resources (%ux%u)\n", 943 data->width, data->height); 944 break; 945 case HFI_EVENT_RELEASE_BUFFER_REFERENCE: 946 venus_helper_release_buf_ref(inst, data->tag); 947 break; 948 default: 949 break; 950 } 951 break; 952 default: 953 break; 954 } 955 } 956 957 static const struct hfi_inst_ops vdec_hfi_ops = { 958 .buf_done = vdec_buf_done, 959 .event_notify = vdec_event_notify, 960 }; 961 962 static void vdec_inst_init(struct venus_inst *inst) 963 { 964 inst->fmt_out = &vdec_formats[6]; 965 inst->fmt_cap = &vdec_formats[0]; 966 inst->width = 1280; 967 inst->height = ALIGN(720, 32); 968 inst->out_width = 1280; 969 inst->out_height = 720; 970 inst->fps = 30; 971 inst->timeperframe.numerator = 1; 972 inst->timeperframe.denominator = 30; 973 inst->hfi_codec = HFI_VIDEO_CODEC_H264; 974 } 975 976 static const struct v4l2_m2m_ops vdec_m2m_ops = { 977 .device_run = venus_helper_m2m_device_run, 978 .job_abort = venus_helper_m2m_job_abort, 979 }; 980 981 static int m2m_queue_init(void *priv, struct vb2_queue *src_vq, 982 struct vb2_queue *dst_vq) 983 { 984 struct venus_inst *inst = priv; 985 int ret; 986 987 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; 988 src_vq->io_modes = VB2_MMAP | VB2_DMABUF; 989 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 990 src_vq->ops = &vdec_vb2_ops; 991 src_vq->mem_ops = &vb2_dma_sg_memops; 992 src_vq->drv_priv = inst; 993 src_vq->buf_struct_size = sizeof(struct venus_buffer); 994 src_vq->allow_zero_bytesused = 1; 995 src_vq->min_buffers_needed = 1; 996 src_vq->dev = inst->core->dev; 997 ret = vb2_queue_init(src_vq); 998 if (ret) 999 return ret; 1000 1001 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; 1002 dst_vq->io_modes = VB2_MMAP | VB2_DMABUF; 1003 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 1004 dst_vq->ops = &vdec_vb2_ops; 1005 dst_vq->mem_ops = &vb2_dma_sg_memops; 1006 dst_vq->drv_priv = inst; 1007 dst_vq->buf_struct_size = sizeof(struct venus_buffer); 1008 dst_vq->allow_zero_bytesused = 1; 1009 dst_vq->min_buffers_needed = 1; 1010 dst_vq->dev = inst->core->dev; 1011 ret = vb2_queue_init(dst_vq); 1012 if (ret) { 1013 vb2_queue_release(src_vq); 1014 return ret; 1015 } 1016 1017 return 0; 1018 } 1019 1020 static int vdec_open(struct file *file) 1021 { 1022 struct venus_core *core = video_drvdata(file); 1023 struct venus_inst *inst; 1024 int ret; 1025 1026 inst = kzalloc(sizeof(*inst), GFP_KERNEL); 1027 if (!inst) 1028 return -ENOMEM; 1029 1030 INIT_LIST_HEAD(&inst->dpbbufs); 1031 INIT_LIST_HEAD(&inst->registeredbufs); 1032 INIT_LIST_HEAD(&inst->internalbufs); 1033 INIT_LIST_HEAD(&inst->list); 1034 mutex_init(&inst->lock); 1035 1036 inst->core = core; 1037 inst->session_type = VIDC_SESSION_TYPE_DEC; 1038 inst->num_output_bufs = 1; 1039 1040 venus_helper_init_instance(inst); 1041 1042 ret = pm_runtime_get_sync(core->dev_dec); 1043 if (ret < 0) 1044 goto err_free_inst; 1045 1046 ret = vdec_ctrl_init(inst); 1047 if (ret) 1048 goto err_put_sync; 1049 1050 ret = hfi_session_create(inst, &vdec_hfi_ops); 1051 if (ret) 1052 goto err_ctrl_deinit; 1053 1054 vdec_inst_init(inst); 1055 1056 /* 1057 * create m2m device for every instance, the m2m context scheduling 1058 * is made by firmware side so we do not need to care about. 1059 */ 1060 inst->m2m_dev = v4l2_m2m_init(&vdec_m2m_ops); 1061 if (IS_ERR(inst->m2m_dev)) { 1062 ret = PTR_ERR(inst->m2m_dev); 1063 goto err_session_destroy; 1064 } 1065 1066 inst->m2m_ctx = v4l2_m2m_ctx_init(inst->m2m_dev, inst, m2m_queue_init); 1067 if (IS_ERR(inst->m2m_ctx)) { 1068 ret = PTR_ERR(inst->m2m_ctx); 1069 goto err_m2m_release; 1070 } 1071 1072 v4l2_fh_init(&inst->fh, core->vdev_dec); 1073 1074 inst->fh.ctrl_handler = &inst->ctrl_handler; 1075 v4l2_fh_add(&inst->fh); 1076 inst->fh.m2m_ctx = inst->m2m_ctx; 1077 file->private_data = &inst->fh; 1078 1079 return 0; 1080 1081 err_m2m_release: 1082 v4l2_m2m_release(inst->m2m_dev); 1083 err_session_destroy: 1084 hfi_session_destroy(inst); 1085 err_ctrl_deinit: 1086 vdec_ctrl_deinit(inst); 1087 err_put_sync: 1088 pm_runtime_put_sync(core->dev_dec); 1089 err_free_inst: 1090 kfree(inst); 1091 return ret; 1092 } 1093 1094 static int vdec_close(struct file *file) 1095 { 1096 struct venus_inst *inst = to_inst(file); 1097 1098 v4l2_m2m_ctx_release(inst->m2m_ctx); 1099 v4l2_m2m_release(inst->m2m_dev); 1100 vdec_ctrl_deinit(inst); 1101 hfi_session_destroy(inst); 1102 mutex_destroy(&inst->lock); 1103 v4l2_fh_del(&inst->fh); 1104 v4l2_fh_exit(&inst->fh); 1105 1106 pm_runtime_put_sync(inst->core->dev_dec); 1107 1108 kfree(inst); 1109 return 0; 1110 } 1111 1112 static const struct v4l2_file_operations vdec_fops = { 1113 .owner = THIS_MODULE, 1114 .open = vdec_open, 1115 .release = vdec_close, 1116 .unlocked_ioctl = video_ioctl2, 1117 .poll = v4l2_m2m_fop_poll, 1118 .mmap = v4l2_m2m_fop_mmap, 1119 #ifdef CONFIG_COMPAT 1120 .compat_ioctl32 = v4l2_compat_ioctl32, 1121 #endif 1122 }; 1123 1124 static int vdec_probe(struct platform_device *pdev) 1125 { 1126 struct device *dev = &pdev->dev; 1127 struct video_device *vdev; 1128 struct venus_core *core; 1129 int ret; 1130 1131 if (!dev->parent) 1132 return -EPROBE_DEFER; 1133 1134 core = dev_get_drvdata(dev->parent); 1135 if (!core) 1136 return -EPROBE_DEFER; 1137 1138 if (IS_V3(core) || IS_V4(core)) { 1139 core->core0_clk = devm_clk_get(dev, "core"); 1140 if (IS_ERR(core->core0_clk)) 1141 return PTR_ERR(core->core0_clk); 1142 } 1143 1144 if (IS_V4(core)) { 1145 core->core0_bus_clk = devm_clk_get(dev, "bus"); 1146 if (IS_ERR(core->core0_bus_clk)) 1147 return PTR_ERR(core->core0_bus_clk); 1148 } 1149 1150 platform_set_drvdata(pdev, core); 1151 1152 vdev = video_device_alloc(); 1153 if (!vdev) 1154 return -ENOMEM; 1155 1156 strlcpy(vdev->name, "qcom-venus-decoder", sizeof(vdev->name)); 1157 vdev->release = video_device_release; 1158 vdev->fops = &vdec_fops; 1159 vdev->ioctl_ops = &vdec_ioctl_ops; 1160 vdev->vfl_dir = VFL_DIR_M2M; 1161 vdev->v4l2_dev = &core->v4l2_dev; 1162 vdev->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING; 1163 1164 ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1); 1165 if (ret) 1166 goto err_vdev_release; 1167 1168 core->vdev_dec = vdev; 1169 core->dev_dec = dev; 1170 1171 video_set_drvdata(vdev, core); 1172 pm_runtime_enable(dev); 1173 1174 return 0; 1175 1176 err_vdev_release: 1177 video_device_release(vdev); 1178 return ret; 1179 } 1180 1181 static int vdec_remove(struct platform_device *pdev) 1182 { 1183 struct venus_core *core = dev_get_drvdata(pdev->dev.parent); 1184 1185 video_unregister_device(core->vdev_dec); 1186 pm_runtime_disable(core->dev_dec); 1187 1188 return 0; 1189 } 1190 1191 static __maybe_unused int vdec_runtime_suspend(struct device *dev) 1192 { 1193 struct venus_core *core = dev_get_drvdata(dev); 1194 int ret; 1195 1196 if (IS_V1(core)) 1197 return 0; 1198 1199 ret = venus_helper_power_enable(core, VIDC_SESSION_TYPE_DEC, true); 1200 if (ret) 1201 return ret; 1202 1203 if (IS_V4(core)) 1204 clk_disable_unprepare(core->core0_bus_clk); 1205 1206 clk_disable_unprepare(core->core0_clk); 1207 1208 return venus_helper_power_enable(core, VIDC_SESSION_TYPE_DEC, false); 1209 } 1210 1211 static __maybe_unused int vdec_runtime_resume(struct device *dev) 1212 { 1213 struct venus_core *core = dev_get_drvdata(dev); 1214 int ret; 1215 1216 if (IS_V1(core)) 1217 return 0; 1218 1219 ret = venus_helper_power_enable(core, VIDC_SESSION_TYPE_DEC, true); 1220 if (ret) 1221 return ret; 1222 1223 ret = clk_prepare_enable(core->core0_clk); 1224 if (ret) 1225 goto err_power_disable; 1226 1227 if (IS_V4(core)) 1228 ret = clk_prepare_enable(core->core0_bus_clk); 1229 1230 if (ret) 1231 goto err_unprepare_core0; 1232 1233 return venus_helper_power_enable(core, VIDC_SESSION_TYPE_DEC, false); 1234 1235 err_unprepare_core0: 1236 clk_disable_unprepare(core->core0_clk); 1237 err_power_disable: 1238 venus_helper_power_enable(core, VIDC_SESSION_TYPE_DEC, false); 1239 return ret; 1240 } 1241 1242 static const struct dev_pm_ops vdec_pm_ops = { 1243 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, 1244 pm_runtime_force_resume) 1245 SET_RUNTIME_PM_OPS(vdec_runtime_suspend, vdec_runtime_resume, NULL) 1246 }; 1247 1248 static const struct of_device_id vdec_dt_match[] = { 1249 { .compatible = "venus-decoder" }, 1250 { } 1251 }; 1252 MODULE_DEVICE_TABLE(of, vdec_dt_match); 1253 1254 static struct platform_driver qcom_venus_dec_driver = { 1255 .probe = vdec_probe, 1256 .remove = vdec_remove, 1257 .driver = { 1258 .name = "qcom-venus-decoder", 1259 .of_match_table = vdec_dt_match, 1260 .pm = &vdec_pm_ops, 1261 }, 1262 }; 1263 module_platform_driver(qcom_venus_dec_driver); 1264 1265 MODULE_ALIAS("platform:qcom-venus-decoder"); 1266 MODULE_DESCRIPTION("Qualcomm Venus video decoder driver"); 1267 MODULE_LICENSE("GPL v2"); 1268