123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888 |
- // SPDX-License-Identifier: GPL-2.0-only
- /*
- * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
- * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
- */
- #include <linux/types.h>
- #include <linux/hash.h>
- #include "msm_vidc_core.h"
- #include "msm_vidc_inst.h"
- #include "msm_vdec.h"
- #include "msm_venc.h"
- #include "msm_vidc_internal.h"
- #include "msm_vidc_driver.h"
- #include "msm_vidc_vb2.h"
- #include "msm_vidc_v4l2.h"
- #include "msm_vidc_debug.h"
- #include "msm_vidc_control.h"
- #include "msm_vidc_power.h"
- #include "msm_vidc_fence.h"
- #include "msm_vidc_memory.h"
- #include "venus_hfi_response.h"
- #include "msm_vidc.h"
- extern const char video_banner[];
- #define MSM_VIDC_DRV_NAME "msm_vidc_driver"
- #define MSM_VIDC_BUS_NAME "platform:msm_vidc_bus"
- /* kernel/msm-4.19 */
- #define MSM_VIDC_VERSION ((5 << 16) + (10 << 8) + 0)
- static inline bool valid_v4l2_buffer(struct v4l2_buffer *b,
- struct msm_vidc_inst *inst)
- {
- if (b->type == INPUT_MPLANE || b->type == OUTPUT_MPLANE)
- return b->length > 0;
- else if (b->type == INPUT_META_PLANE || b->type == OUTPUT_META_PLANE)
- return true;
- return false;
- }
- static int get_poll_flags(struct msm_vidc_inst *inst, u32 port)
- {
- int poll = 0;
- struct vb2_queue *q = NULL;
- struct vb2_buffer *vb = NULL;
- unsigned long flags = 0;
- if (port >= MAX_PORT) {
- d_vpr_e("%s: invalid params, inst %pK, port %d\n",
- __func__, inst, port);
- return poll;
- }
- q = inst->bufq[port].vb2q;
- spin_lock_irqsave(&q->done_lock, flags);
- if (!list_empty(&q->done_list))
- vb = list_first_entry(&q->done_list, struct vb2_buffer,
- done_entry);
- if (vb && (vb->state == VB2_BUF_STATE_DONE ||
- vb->state == VB2_BUF_STATE_ERROR)) {
- if (port == OUTPUT_PORT || port == OUTPUT_META_PORT)
- poll |= POLLIN | POLLRDNORM;
- else if (port == INPUT_PORT || port == INPUT_META_PORT)
- poll |= POLLOUT | POLLWRNORM;
- }
- spin_unlock_irqrestore(&q->done_lock, flags);
- return poll;
- }
- int msm_vidc_poll(struct msm_vidc_inst *inst, struct file *filp,
- struct poll_table_struct *wait)
- {
- int poll = 0;
- poll_wait(filp, &inst->fh.wait, wait);
- poll_wait(filp, &inst->bufq[INPUT_META_PORT].vb2q->done_wq, wait);
- poll_wait(filp, &inst->bufq[OUTPUT_META_PORT].vb2q->done_wq, wait);
- poll_wait(filp, &inst->bufq[INPUT_PORT].vb2q->done_wq, wait);
- poll_wait(filp, &inst->bufq[OUTPUT_PORT].vb2q->done_wq, wait);
- if (v4l2_event_pending(&inst->fh))
- poll |= POLLPRI;
- poll |= get_poll_flags(inst, INPUT_META_PORT);
- poll |= get_poll_flags(inst, OUTPUT_META_PORT);
- poll |= get_poll_flags(inst, INPUT_PORT);
- poll |= get_poll_flags(inst, OUTPUT_PORT);
- return poll;
- }
- int msm_vidc_querycap(struct msm_vidc_inst *inst, struct v4l2_capability *cap)
- {
- strlcpy(cap->driver, MSM_VIDC_DRV_NAME, sizeof(cap->driver));
- strlcpy(cap->bus_info, MSM_VIDC_BUS_NAME, sizeof(cap->bus_info));
- cap->version = MSM_VIDC_VERSION;
- memset(cap->reserved, 0, sizeof(cap->reserved));
- if (is_decode_session(inst))
- strlcpy(cap->card, "msm_vidc_decoder", sizeof(cap->card));
- else if (is_encode_session(inst))
- strlcpy(cap->card, "msm_vidc_encoder", sizeof(cap->card));
- else
- return -EINVAL;
- return 0;
- }
- int msm_vidc_enum_fmt(struct msm_vidc_inst *inst, struct v4l2_fmtdesc *f)
- {
- if (is_decode_session(inst))
- return msm_vdec_enum_fmt(inst, f);
- if (is_encode_session(inst))
- return msm_venc_enum_fmt(inst, f);
- return -EINVAL;
- }
- int msm_vidc_query_ctrl(struct msm_vidc_inst *inst, struct v4l2_queryctrl *q_ctrl)
- {
- int rc = 0;
- struct v4l2_ctrl *ctrl;
- ctrl = v4l2_ctrl_find(&inst->ctrl_handler, q_ctrl->id);
- if (!ctrl) {
- i_vpr_e(inst, "%s: get_ctrl failed for id %d\n",
- __func__, q_ctrl->id);
- return -EINVAL;
- }
- q_ctrl->minimum = ctrl->minimum;
- q_ctrl->maximum = ctrl->maximum;
- q_ctrl->default_value = ctrl->default_value;
- q_ctrl->flags = 0;
- q_ctrl->step = ctrl->step;
- i_vpr_h(inst,
- "query ctrl: %s: min %d, max %d, default %d step %d flags %#x\n",
- ctrl->name, q_ctrl->minimum, q_ctrl->maximum,
- q_ctrl->default_value, q_ctrl->step, q_ctrl->flags);
- return rc;
- }
- int msm_vidc_query_menu(struct msm_vidc_inst *inst, struct v4l2_querymenu *qmenu)
- {
- int rc = 0;
- struct v4l2_ctrl *ctrl;
- ctrl = v4l2_ctrl_find(&inst->ctrl_handler, qmenu->id);
- if (!ctrl) {
- i_vpr_e(inst, "%s: get_ctrl failed for id %d\n",
- __func__, qmenu->id);
- return -EINVAL;
- }
- if (ctrl->type != V4L2_CTRL_TYPE_MENU) {
- i_vpr_e(inst, "%s: ctrl: %s: type (%d) is not MENU type\n",
- __func__, ctrl->name, ctrl->type);
- return -EINVAL;
- }
- if (qmenu->index < ctrl->minimum || qmenu->index > ctrl->maximum)
- return -EINVAL;
- if (ctrl->menu_skip_mask & (1 << qmenu->index))
- rc = -EINVAL;
- i_vpr_h(inst,
- "%s: ctrl: %s: min %lld, max %lld, menu_skip_mask %lld, qmenu: id %u, index %d, %s\n",
- __func__, ctrl->name, ctrl->minimum, ctrl->maximum,
- ctrl->menu_skip_mask, qmenu->id, qmenu->index,
- rc ? "not supported" : "supported");
- return rc;
- }
- int msm_vidc_try_fmt(struct msm_vidc_inst *inst, struct v4l2_format *f)
- {
- int rc = 0;
- if (is_decode_session(inst))
- rc = msm_vdec_try_fmt(inst, f);
- if (is_encode_session(inst))
- rc = msm_venc_try_fmt(inst, f);
- if (rc)
- i_vpr_e(inst, "%s: try_fmt(%d) failed %d\n",
- __func__, f->type, rc);
- return rc;
- }
- int msm_vidc_s_fmt(struct msm_vidc_inst *inst, struct v4l2_format *f)
- {
- int rc = 0;
- if (is_decode_session(inst))
- rc = msm_vdec_s_fmt(inst, f);
- if (is_encode_session(inst))
- rc = msm_venc_s_fmt(inst, f);
- if (rc)
- i_vpr_e(inst, "%s: s_fmt(%d) failed %d\n",
- __func__, f->type, rc);
- return rc;
- }
- int msm_vidc_g_fmt(struct msm_vidc_inst *inst, struct v4l2_format *f)
- {
- int rc = 0;
- if (is_decode_session(inst))
- rc = msm_vdec_g_fmt(inst, f);
- if (is_encode_session(inst))
- rc = msm_venc_g_fmt(inst, f);
- if (rc)
- return rc;
- if (f->type == INPUT_MPLANE || f->type == OUTPUT_MPLANE)
- i_vpr_h(inst, "%s: type %s format %s width %d height %d size %d\n",
- __func__, v4l2_type_name(f->type),
- v4l2_pixelfmt_name(inst, f->fmt.pix_mp.pixelformat),
- f->fmt.pix_mp.width, f->fmt.pix_mp.height,
- f->fmt.pix_mp.plane_fmt[0].sizeimage);
- else if (f->type == INPUT_META_PLANE || f->type == OUTPUT_META_PLANE)
- i_vpr_h(inst, "%s: type %s size %d\n",
- __func__, v4l2_type_name(f->type), f->fmt.meta.buffersize);
- return 0;
- }
- int msm_vidc_s_selection(struct msm_vidc_inst *inst, struct v4l2_selection *s)
- {
- int rc = 0;
- if (is_decode_session(inst))
- rc = msm_vdec_s_selection(inst, s);
- if (is_encode_session(inst))
- rc = msm_venc_s_selection(inst, s);
- return rc;
- }
- int msm_vidc_g_selection(struct msm_vidc_inst *inst, struct v4l2_selection *s)
- {
- int rc = 0;
- if (is_decode_session(inst))
- rc = msm_vdec_g_selection(inst, s);
- if (is_encode_session(inst))
- rc = msm_venc_g_selection(inst, s);
- return rc;
- }
- int msm_vidc_s_param(struct msm_vidc_inst *inst, struct v4l2_streamparm *param)
- {
- int rc = 0;
- if (param->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
- param->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
- return -EINVAL;
- if (is_encode_session(inst)) {
- rc = msm_venc_s_param(inst, param);
- } else {
- i_vpr_e(inst, "%s: invalid domain %#x\n",
- __func__, inst->domain);
- return -EINVAL;
- }
- return rc;
- }
- int msm_vidc_g_param(struct msm_vidc_inst *inst, struct v4l2_streamparm *param)
- {
- int rc = 0;
- if (param->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
- param->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
- return -EINVAL;
- if (is_encode_session(inst)) {
- rc = msm_venc_g_param(inst, param);
- } else {
- i_vpr_e(inst, "%s: invalid domain %#x\n",
- __func__, inst->domain);
- return -EINVAL;
- }
- return rc;
- }
- int msm_vidc_reqbufs(struct msm_vidc_inst *inst, struct v4l2_requestbuffers *b)
- {
- int rc = 0;
- int port;
- port = v4l2_type_to_driver_port(inst, b->type, __func__);
- if (port < 0) {
- rc = -EINVAL;
- goto exit;
- }
- rc = vb2_reqbufs(inst->bufq[port].vb2q, b);
- if (rc) {
- i_vpr_e(inst, "%s: vb2_reqbufs(%d) failed, %d\n",
- __func__, b->type, rc);
- goto exit;
- }
- exit:
- return rc;
- }
- int msm_vidc_querybuf(struct msm_vidc_inst *inst, struct v4l2_buffer *b)
- {
- int rc = 0;
- int port;
- port = v4l2_type_to_driver_port(inst, b->type, __func__);
- if (port < 0) {
- rc = -EINVAL;
- goto exit;
- }
- rc = vb2_querybuf(inst->bufq[port].vb2q, b);
- if (rc) {
- i_vpr_e(inst, "%s: vb2_querybuf(%d) failed, %d\n",
- __func__, b->type, rc);
- goto exit;
- }
- exit:
- return rc;
- }
- int msm_vidc_create_bufs(struct msm_vidc_inst *inst, struct v4l2_create_buffers *b)
- {
- int rc = 0;
- int port;
- struct v4l2_format *f;
- f = &b->format;
- port = v4l2_type_to_driver_port(inst, f->type, __func__);
- if (port < 0) {
- rc = -EINVAL;
- goto exit;
- }
- rc = vb2_create_bufs(inst->bufq[port].vb2q, b);
- if (rc) {
- i_vpr_e(inst, "%s: vb2_create_bufs(%d) failed, %d\n",
- __func__, f->type, rc);
- goto exit;
- }
- exit:
- return rc;
- }
- int msm_vidc_prepare_buf(struct msm_vidc_inst *inst, struct media_device *mdev,
- struct v4l2_buffer *b)
- {
- int rc = 0;
- struct vb2_queue *q;
- if (!valid_v4l2_buffer(b, inst)) {
- d_vpr_e("%s: invalid params %pK %pK\n", __func__, inst, b);
- return -EINVAL;
- }
- q = msm_vidc_get_vb2q(inst, b->type, __func__);
- if (!q) {
- rc = -EINVAL;
- goto exit;
- }
- rc = vb2_prepare_buf(q, mdev, b);
- if (rc) {
- i_vpr_e(inst, "%s: failed with %d\n", __func__, rc);
- goto exit;
- }
- exit:
- return rc;
- }
- int msm_vidc_qbuf(struct msm_vidc_inst *inst, struct media_device *mdev,
- struct v4l2_buffer *b)
- {
- int rc = 0;
- struct vb2_queue *q;
- if (!valid_v4l2_buffer(b, inst)) {
- d_vpr_e("%s: invalid params %pK %pK\n", __func__, inst, b);
- return -EINVAL;
- }
- q = msm_vidc_get_vb2q(inst, b->type, __func__);
- if (!q) {
- rc = -EINVAL;
- goto exit;
- }
- rc = vb2_qbuf(q, mdev, b);
- if (rc)
- i_vpr_e(inst, "%s: failed with %d\n", __func__, rc);
- exit:
- return rc;
- }
- int msm_vidc_dqbuf(struct msm_vidc_inst *inst, struct v4l2_buffer *b)
- {
- int rc = 0;
- struct vb2_queue *q;
- if (!valid_v4l2_buffer(b, inst)) {
- d_vpr_e("%s: invalid params %pK %pK\n", __func__, inst, b);
- return -EINVAL;
- }
- q = msm_vidc_get_vb2q(inst, b->type, __func__);
- if (!q) {
- rc = -EINVAL;
- goto exit;
- }
- rc = vb2_dqbuf(q, b, true);
- if (rc == -EAGAIN) {
- goto exit;
- } else if (rc) {
- i_vpr_l(inst, "%s: failed with %d\n", __func__, rc);
- goto exit;
- }
- exit:
- return rc;
- }
- int msm_vidc_streamon(struct msm_vidc_inst *inst, enum v4l2_buf_type type)
- {
- int rc = 0;
- int port;
- port = v4l2_type_to_driver_port(inst, type, __func__);
- if (port < 0) {
- rc = -EINVAL;
- goto exit;
- }
- rc = vb2_streamon(inst->bufq[port].vb2q, type);
- if (rc) {
- i_vpr_e(inst, "%s: vb2_streamon(%d) failed, %d\n",
- __func__, type, rc);
- goto exit;
- }
- exit:
- return rc;
- }
- int msm_vidc_streamoff(struct msm_vidc_inst *inst, enum v4l2_buf_type type)
- {
- int rc = 0;
- int port;
- port = v4l2_type_to_driver_port(inst, type, __func__);
- if (port < 0) {
- rc = -EINVAL;
- goto exit;
- }
- rc = vb2_streamoff(inst->bufq[port].vb2q, type);
- if (rc) {
- i_vpr_e(inst, "%s: vb2_streamoff(%d) failed, %d\n",
- __func__, type, rc);
- goto exit;
- }
- exit:
- return rc;
- }
- int msm_vidc_try_cmd(struct msm_vidc_inst *inst, union msm_v4l2_cmd *cmd)
- {
- int rc = 0;
- struct v4l2_decoder_cmd *dec = NULL;
- struct v4l2_encoder_cmd *enc = NULL;
- if (is_decode_session(inst)) {
- dec = (struct v4l2_decoder_cmd *)cmd;
- i_vpr_h(inst, "%s: cmd %d\n", __func__, dec->cmd);
- if (dec->cmd != V4L2_DEC_CMD_STOP && dec->cmd != V4L2_DEC_CMD_START)
- return -EINVAL;
- dec->flags = 0;
- if (dec->cmd == V4L2_DEC_CMD_STOP) {
- dec->stop.pts = 0;
- } else if (dec->cmd == V4L2_DEC_CMD_START) {
- dec->start.speed = 0;
- dec->start.format = V4L2_DEC_START_FMT_NONE;
- }
- } else if (is_encode_session(inst)) {
- enc = (struct v4l2_encoder_cmd *)cmd;
- i_vpr_h(inst, "%s: cmd %d\n", __func__, enc->cmd);
- if (enc->cmd != V4L2_ENC_CMD_STOP && enc->cmd != V4L2_ENC_CMD_START)
- return -EINVAL;
- enc->flags = 0;
- }
- return rc;
- }
- int msm_vidc_start_cmd(struct msm_vidc_inst *inst)
- {
- int rc = 0;
- if (!is_decode_session(inst) && !is_encode_session(inst)) {
- i_vpr_e(inst, "%s: invalid session %d\n", __func__, inst->domain);
- return -EINVAL;
- }
- if (is_decode_session(inst)) {
- rc = msm_vdec_start_cmd(inst);
- if (rc)
- return rc;
- } else if (is_encode_session(inst)) {
- rc = msm_venc_start_cmd(inst);
- if (rc)
- return rc;
- }
- return rc;
- }
- int msm_vidc_stop_cmd(struct msm_vidc_inst *inst)
- {
- int rc = 0;
- if (!is_decode_session(inst) && !is_encode_session(inst)) {
- i_vpr_e(inst, "%s: invalid session %d\n", __func__, inst->domain);
- return -EINVAL;
- }
- if (is_decode_session(inst)) {
- rc = msm_vdec_stop_cmd(inst);
- if (rc)
- return rc;
- } else if (is_encode_session(inst)) {
- rc = msm_venc_stop_cmd(inst);
- if (rc)
- return rc;
- }
- return rc;
- }
- int msm_vidc_enum_framesizes(struct msm_vidc_inst *inst, struct v4l2_frmsizeenum *fsize)
- {
- enum msm_vidc_colorformat_type colorfmt;
- enum msm_vidc_codec_type codec;
- u32 meta_fmt;
- /* only index 0 allowed as per v4l2 spec */
- if (fsize->index)
- return -EINVAL;
- meta_fmt = v4l2_colorformat_from_driver(inst, MSM_VIDC_FMT_META, __func__);
- if (fsize->pixel_format != meta_fmt) {
- /* validate pixel format */
- codec = v4l2_codec_to_driver(inst, fsize->pixel_format, __func__);
- if (!codec) {
- colorfmt = v4l2_colorformat_to_driver(inst, fsize->pixel_format,
- __func__);
- if (colorfmt == MSM_VIDC_FMT_NONE) {
- i_vpr_e(inst, "%s: unsupported pix fmt %#x\n",
- __func__, fsize->pixel_format);
- return -EINVAL;
- }
- }
- }
- fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
- fsize->stepwise.min_width = inst->capabilities[FRAME_WIDTH].min;
- fsize->stepwise.max_width = inst->capabilities[FRAME_WIDTH].max;
- fsize->stepwise.step_width =
- inst->capabilities[FRAME_WIDTH].step_or_mask;
- fsize->stepwise.min_height = inst->capabilities[FRAME_HEIGHT].min;
- fsize->stepwise.max_height = inst->capabilities[FRAME_HEIGHT].max;
- fsize->stepwise.step_height =
- inst->capabilities[FRAME_HEIGHT].step_or_mask;
- return 0;
- }
- int msm_vidc_enum_frameintervals(struct msm_vidc_inst *inst, struct v4l2_frmivalenum *fival)
- {
- struct msm_vidc_core *core;
- enum msm_vidc_colorformat_type colorfmt;
- u32 fps, mbpf;
- u32 meta_fmt;
- if (is_decode_session(inst)) {
- i_vpr_e(inst, "%s: not supported by decoder\n", __func__);
- return -ENOTTY;
- }
- core = inst->core;
- /* only index 0 allowed as per v4l2 spec */
- if (fival->index)
- return -EINVAL;
- meta_fmt = v4l2_colorformat_from_driver(inst, MSM_VIDC_FMT_META, __func__);
- if (fival->pixel_format != meta_fmt) {
- /* validate pixel format */
- colorfmt = v4l2_colorformat_to_driver(inst, fival->pixel_format, __func__);
- if (colorfmt == MSM_VIDC_FMT_NONE) {
- i_vpr_e(inst, "%s: unsupported pix fmt %#x\n",
- __func__, fival->pixel_format);
- return -EINVAL;
- }
- }
- /* validate resolution */
- if (fival->width > inst->capabilities[FRAME_WIDTH].max ||
- fival->width < inst->capabilities[FRAME_WIDTH].min ||
- fival->height > inst->capabilities[FRAME_HEIGHT].max ||
- fival->height < inst->capabilities[FRAME_HEIGHT].min) {
- i_vpr_e(inst, "%s: unsupported resolution %u x %u\n", __func__,
- fival->width, fival->height);
- return -EINVAL;
- }
- /* calculate max supported fps for a given resolution */
- mbpf = NUM_MBS_PER_FRAME(fival->height, fival->width);
- fps = core->capabilities[MAX_MBPS].value / mbpf;
- fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
- fival->stepwise.min.numerator = 1;
- fival->stepwise.min.denominator =
- min_t(u32, fps, inst->capabilities[FRAME_RATE].max);
- fival->stepwise.max.numerator = 1;
- fival->stepwise.max.denominator = 1;
- fival->stepwise.step.numerator = 1;
- fival->stepwise.step.denominator = inst->capabilities[FRAME_RATE].max;
- return 0;
- }
- int msm_vidc_subscribe_event(struct msm_vidc_inst *inst,
- const struct v4l2_event_subscription *sub)
- {
- int rc = 0;
- i_vpr_h(inst, "%s: type %d id %d\n", __func__, sub->type, sub->id);
- if (is_decode_session(inst))
- rc = msm_vdec_subscribe_event(inst, sub);
- if (is_encode_session(inst))
- rc = msm_venc_subscribe_event(inst, sub);
- return rc;
- }
- int msm_vidc_unsubscribe_event(struct msm_vidc_inst *inst,
- const struct v4l2_event_subscription *sub)
- {
- int rc = 0;
- i_vpr_h(inst, "%s: type %d id %d\n", __func__, sub->type, sub->id);
- rc = v4l2_event_unsubscribe(&inst->fh, sub);
- if (rc)
- i_vpr_e(inst, "%s: failed, type %d id %d\n",
- __func__, sub->type, sub->id);
- return rc;
- }
- int msm_vidc_dqevent(struct msm_vidc_inst *inst, struct v4l2_event *event)
- {
- int rc = 0;
- rc = v4l2_event_dequeue(&inst->fh, event, false);
- if (rc)
- i_vpr_e(inst, "%s: fialed\n", __func__);
- return rc;
- }
- void *msm_vidc_open(struct msm_vidc_core *core, u32 session_type)
- {
- int rc = 0;
- struct msm_vidc_inst *inst = NULL;
- int i = 0;
- d_vpr_h("%s: %s\n", __func__, video_banner);
- if (session_type != MSM_VIDC_DECODER &&
- session_type != MSM_VIDC_ENCODER) {
- d_vpr_e("%s: invalid session_type %d\n",
- __func__, session_type);
- return NULL;
- }
- rc = msm_vidc_core_init(core);
- if (rc)
- return NULL;
- rc = msm_vidc_core_init_wait(core);
- if (rc)
- return NULL;
- rc = msm_vidc_vmem_alloc(sizeof(*inst), (void **)&inst, "inst memory");
- if (rc)
- return NULL;
- inst->core = core;
- inst->domain = session_type;
- inst->session_id = hash32_ptr(inst);
- msm_vidc_update_state(inst, MSM_VIDC_OPEN, __func__);
- inst->sub_state = MSM_VIDC_SUB_STATE_NONE;
- strlcpy(inst->sub_state_name, "SUB_STATE_NONE", sizeof(inst->sub_state_name));
- inst->active = true;
- inst->request = false;
- inst->ipsc_properties_set = false;
- inst->opsc_properties_set = false;
- inst->caps_list_prepared = false;
- inst->has_bframe = false;
- inst->iframe = false;
- inst->auto_framerate = DEFAULT_FPS << 16;
- inst->initial_time_us = ktime_get_ns() / 1000;
- kref_init(&inst->kref);
- mutex_init(&inst->lock);
- mutex_init(&inst->ctx_q_lock);
- mutex_init(&inst->client_lock);
- msm_vidc_update_debug_str(inst);
- i_vpr_h(inst, "Opening video instance: %d\n", session_type);
- rc = msm_vidc_add_session(inst);
- if (rc) {
- i_vpr_e(inst, "%s: failed to add session\n", __func__);
- goto fail_add_session;
- }
- rc = msm_vidc_pools_init(inst);
- if (rc) {
- i_vpr_e(inst, "%s: failed to init pool buffers\n", __func__);
- goto fail_pools_init;
- }
- INIT_LIST_HEAD(&inst->caps_list);
- INIT_LIST_HEAD(&inst->timestamps.list);
- INIT_LIST_HEAD(&inst->ts_reorder.list);
- INIT_LIST_HEAD(&inst->buffers.input.list);
- INIT_LIST_HEAD(&inst->buffers.input_meta.list);
- INIT_LIST_HEAD(&inst->buffers.output.list);
- INIT_LIST_HEAD(&inst->buffers.output_meta.list);
- INIT_LIST_HEAD(&inst->buffers.read_only.list);
- INIT_LIST_HEAD(&inst->buffers.bin.list);
- INIT_LIST_HEAD(&inst->buffers.arp.list);
- INIT_LIST_HEAD(&inst->buffers.comv.list);
- INIT_LIST_HEAD(&inst->buffers.non_comv.list);
- INIT_LIST_HEAD(&inst->buffers.line.list);
- INIT_LIST_HEAD(&inst->buffers.dpb.list);
- INIT_LIST_HEAD(&inst->buffers.persist.list);
- INIT_LIST_HEAD(&inst->buffers.vpss.list);
- INIT_LIST_HEAD(&inst->buffers.partial_data.list);
- INIT_LIST_HEAD(&inst->mem_info.bin.list);
- INIT_LIST_HEAD(&inst->mem_info.arp.list);
- INIT_LIST_HEAD(&inst->mem_info.comv.list);
- INIT_LIST_HEAD(&inst->mem_info.non_comv.list);
- INIT_LIST_HEAD(&inst->mem_info.line.list);
- INIT_LIST_HEAD(&inst->mem_info.dpb.list);
- INIT_LIST_HEAD(&inst->mem_info.persist.list);
- INIT_LIST_HEAD(&inst->mem_info.vpss.list);
- INIT_LIST_HEAD(&inst->mem_info.partial_data.list);
- INIT_LIST_HEAD(&inst->children_list);
- INIT_LIST_HEAD(&inst->firmware_list);
- INIT_LIST_HEAD(&inst->enc_input_crs);
- INIT_LIST_HEAD(&inst->dmabuf_tracker);
- INIT_LIST_HEAD(&inst->input_timer_list);
- INIT_LIST_HEAD(&inst->pending_pkts);
- INIT_LIST_HEAD(&inst->fence_list);
- INIT_LIST_HEAD(&inst->buffer_stats_list);
- for (i = 0; i < MAX_SIGNAL; i++)
- init_completion(&inst->completions[i]);
- inst->workq = create_singlethread_workqueue("workq");
- if (!inst->workq) {
- i_vpr_e(inst, "%s: create workq failed\n", __func__);
- goto fail_create_workq;
- }
- INIT_DELAYED_WORK(&inst->stats_work, msm_vidc_stats_handler);
- INIT_WORK(&inst->stability_work, msm_vidc_stability_handler);
- rc = msm_vidc_v4l2_fh_init(inst);
- if (rc)
- goto fail_eventq_init;
- rc = msm_vidc_vb2_queue_init(inst);
- if (rc)
- goto fail_vb2q_init;
- if (is_decode_session(inst))
- rc = msm_vdec_inst_init(inst);
- else if (is_encode_session(inst))
- rc = msm_venc_inst_init(inst);
- if (rc)
- goto fail_inst_init;
- rc = msm_vidc_fence_init(inst);
- if (rc)
- goto fail_fence_init;
- /* reset clock residency stats */
- msm_vidc_reset_residency_stats(core);
- msm_vidc_scale_power(inst, true);
- rc = msm_vidc_session_open(inst);
- if (rc) {
- msm_vidc_core_deinit(core, true);
- goto fail_session_open;
- }
- inst->debugfs_root =
- msm_vidc_debugfs_init_inst(inst, core->debugfs_root);
- if (!inst->debugfs_root)
- i_vpr_h(inst, "%s: debugfs not available\n", __func__);
- return inst;
- fail_session_open:
- msm_vidc_fence_deinit(inst);
- fail_fence_init:
- if (is_decode_session(inst))
- msm_vdec_inst_deinit(inst);
- else if (is_encode_session(inst))
- msm_venc_inst_deinit(inst);
- fail_inst_init:
- msm_vidc_vb2_queue_deinit(inst);
- fail_vb2q_init:
- msm_vidc_v4l2_fh_deinit(inst);
- fail_eventq_init:
- destroy_workqueue(inst->workq);
- fail_create_workq:
- msm_vidc_pools_deinit(inst);
- fail_pools_init:
- msm_vidc_remove_session(inst);
- msm_vidc_remove_dangling_session(inst);
- fail_add_session:
- mutex_destroy(&inst->client_lock);
- mutex_destroy(&inst->ctx_q_lock);
- mutex_destroy(&inst->lock);
- msm_vidc_vmem_free((void **)&inst);
- return NULL;
- }
- int msm_vidc_close(struct msm_vidc_inst *inst)
- {
- int rc = 0;
- struct msm_vidc_core *core;
- core = inst->core;
- i_vpr_h(inst, "%s()\n", __func__);
- client_lock(inst, __func__);
- inst_lock(inst, __func__);
- /* print final stats */
- msm_vidc_print_stats(inst);
- /* print internal buffer memory usage stats */
- msm_vidc_print_memory_stats(inst);
- msm_vidc_print_residency_stats(core);
- msm_vidc_session_close(inst);
- msm_vidc_change_state(inst, MSM_VIDC_CLOSE, __func__);
- inst->sub_state = MSM_VIDC_SUB_STATE_NONE;
- strscpy(inst->sub_state_name, "SUB_STATE_NONE", sizeof(inst->sub_state_name));
- inst_unlock(inst, __func__);
- client_unlock(inst, __func__);
- cancel_stability_work_sync(inst);
- cancel_stats_work_sync(inst);
- msm_vidc_show_stats(inst);
- put_inst(inst);
- msm_vidc_schedule_core_deinit(core);
- return rc;
- }
|