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