linux/drivers/media/platform/mtk-vcodec/vdec_vpu_if.c
<<
>>
Prefs
   1/*
   2 * Copyright (c) 2016 MediaTek Inc.
   3 * Author: PC Chen <pc.chen@mediatek.com>
   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 as
   7 * 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 "mtk_vcodec_drv.h"
  16#include "mtk_vcodec_util.h"
  17#include "vdec_ipi_msg.h"
  18#include "vdec_vpu_if.h"
  19
  20static void handle_init_ack_msg(struct vdec_vpu_ipi_init_ack *msg)
  21{
  22        struct vdec_vpu_inst *vpu = (struct vdec_vpu_inst *)
  23                                        (unsigned long)msg->ap_inst_addr;
  24
  25        mtk_vcodec_debug(vpu, "+ ap_inst_addr = 0x%llx", msg->ap_inst_addr);
  26
  27        /* mapping VPU address to kernel virtual address */
  28        /* the content in vsi is initialized to 0 in VPU */
  29        vpu->vsi = vpu_mapping_dm_addr(vpu->dev, msg->vpu_inst_addr);
  30        vpu->inst_addr = msg->vpu_inst_addr;
  31
  32        mtk_vcodec_debug(vpu, "- vpu_inst_addr = 0x%x", vpu->inst_addr);
  33}
  34
  35/*
  36 * This function runs in interrupt context and it means there's an IPI MSG
  37 * from VPU.
  38 */
  39void vpu_dec_ipi_handler(void *data, unsigned int len, void *priv)
  40{
  41        struct vdec_vpu_ipi_ack *msg = data;
  42        struct vdec_vpu_inst *vpu = (struct vdec_vpu_inst *)
  43                                        (unsigned long)msg->ap_inst_addr;
  44
  45        mtk_vcodec_debug(vpu, "+ id=%X", msg->msg_id);
  46
  47        if (msg->status == 0) {
  48                switch (msg->msg_id) {
  49                case VPU_IPIMSG_DEC_INIT_ACK:
  50                        handle_init_ack_msg(data);
  51                        break;
  52
  53                case VPU_IPIMSG_DEC_START_ACK:
  54                case VPU_IPIMSG_DEC_END_ACK:
  55                case VPU_IPIMSG_DEC_DEINIT_ACK:
  56                case VPU_IPIMSG_DEC_RESET_ACK:
  57                        break;
  58
  59                default:
  60                        mtk_vcodec_err(vpu, "invalid msg=%X", msg->msg_id);
  61                        break;
  62                }
  63        }
  64
  65        mtk_vcodec_debug(vpu, "- id=%X", msg->msg_id);
  66        vpu->failure = msg->status;
  67        vpu->signaled = 1;
  68}
  69
  70static int vcodec_vpu_send_msg(struct vdec_vpu_inst *vpu, void *msg, int len)
  71{
  72        int err;
  73        uint32_t msg_id = *(uint32_t *)msg;
  74
  75        mtk_vcodec_debug(vpu, "id=%X", msg_id);
  76
  77        vpu->failure = 0;
  78        vpu->signaled = 0;
  79
  80        err = vpu_ipi_send(vpu->dev, vpu->id, msg, len);
  81        if (err) {
  82                mtk_vcodec_err(vpu, "send fail vpu_id=%d msg_id=%X status=%d",
  83                               vpu->id, msg_id, err);
  84                return err;
  85        }
  86
  87        return vpu->failure;
  88}
  89
  90static int vcodec_send_ap_ipi(struct vdec_vpu_inst *vpu, unsigned int msg_id)
  91{
  92        struct vdec_ap_ipi_cmd msg;
  93        int err = 0;
  94
  95        mtk_vcodec_debug(vpu, "+ id=%X", msg_id);
  96
  97        memset(&msg, 0, sizeof(msg));
  98        msg.msg_id = msg_id;
  99        msg.vpu_inst_addr = vpu->inst_addr;
 100
 101        err = vcodec_vpu_send_msg(vpu, &msg, sizeof(msg));
 102        mtk_vcodec_debug(vpu, "- id=%X ret=%d", msg_id, err);
 103        return err;
 104}
 105
 106int vpu_dec_init(struct vdec_vpu_inst *vpu)
 107{
 108        struct vdec_ap_ipi_init msg;
 109        int err;
 110
 111        mtk_vcodec_debug_enter(vpu);
 112
 113        init_waitqueue_head(&vpu->wq);
 114
 115        err = vpu_ipi_register(vpu->dev, vpu->id, vpu->handler, "vdec", NULL);
 116        if (err != 0) {
 117                mtk_vcodec_err(vpu, "vpu_ipi_register fail status=%d", err);
 118                return err;
 119        }
 120
 121        memset(&msg, 0, sizeof(msg));
 122        msg.msg_id = AP_IPIMSG_DEC_INIT;
 123        msg.ap_inst_addr = (unsigned long)vpu;
 124
 125        mtk_vcodec_debug(vpu, "vdec_inst=%p", vpu);
 126
 127        err = vcodec_vpu_send_msg(vpu, (void *)&msg, sizeof(msg));
 128        mtk_vcodec_debug(vpu, "- ret=%d", err);
 129        return err;
 130}
 131
 132int vpu_dec_start(struct vdec_vpu_inst *vpu, uint32_t *data, unsigned int len)
 133{
 134        struct vdec_ap_ipi_dec_start msg;
 135        int i;
 136        int err = 0;
 137
 138        mtk_vcodec_debug_enter(vpu);
 139
 140        if (len > ARRAY_SIZE(msg.data)) {
 141                mtk_vcodec_err(vpu, "invalid len = %d\n", len);
 142                return -EINVAL;
 143        }
 144
 145        memset(&msg, 0, sizeof(msg));
 146        msg.msg_id = AP_IPIMSG_DEC_START;
 147        msg.vpu_inst_addr = vpu->inst_addr;
 148
 149        for (i = 0; i < len; i++)
 150                msg.data[i] = data[i];
 151
 152        err = vcodec_vpu_send_msg(vpu, (void *)&msg, sizeof(msg));
 153        mtk_vcodec_debug(vpu, "- ret=%d", err);
 154        return err;
 155}
 156
 157int vpu_dec_end(struct vdec_vpu_inst *vpu)
 158{
 159        return vcodec_send_ap_ipi(vpu, AP_IPIMSG_DEC_END);
 160}
 161
 162int vpu_dec_deinit(struct vdec_vpu_inst *vpu)
 163{
 164        return vcodec_send_ap_ipi(vpu, AP_IPIMSG_DEC_DEINIT);
 165}
 166
 167int vpu_dec_reset(struct vdec_vpu_inst *vpu)
 168{
 169        return vcodec_send_ap_ipi(vpu, AP_IPIMSG_DEC_RESET);
 170}
 171