GNU Linux-libre 4.14.254-gnu1
[releases.git] / drivers / media / platform / mtk-vcodec / vdec_vpu_if.c
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
20 static 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  */
39 void 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
70 static int vcodec_vpu_send_msg(struct vdec_vpu_inst *vpu, void *msg, int len)
71 {
72         int err;
73
74         mtk_vcodec_debug(vpu, "id=%X", *(uint32_t *)msg);
75
76         vpu->failure = 0;
77         vpu->signaled = 0;
78
79         err = vpu_ipi_send(vpu->dev, vpu->id, msg, len);
80         if (err) {
81                 mtk_vcodec_err(vpu, "send fail vpu_id=%d msg_id=%X status=%d",
82                                vpu->id, *(uint32_t *)msg, err);
83                 return err;
84         }
85
86         return vpu->failure;
87 }
88
89 static int vcodec_send_ap_ipi(struct vdec_vpu_inst *vpu, unsigned int msg_id)
90 {
91         struct vdec_ap_ipi_cmd msg;
92         int err = 0;
93
94         mtk_vcodec_debug(vpu, "+ id=%X", msg_id);
95
96         memset(&msg, 0, sizeof(msg));
97         msg.msg_id = msg_id;
98         msg.vpu_inst_addr = vpu->inst_addr;
99
100         err = vcodec_vpu_send_msg(vpu, &msg, sizeof(msg));
101         mtk_vcodec_debug(vpu, "- id=%X ret=%d", msg_id, err);
102         return err;
103 }
104
105 int vpu_dec_init(struct vdec_vpu_inst *vpu)
106 {
107         struct vdec_ap_ipi_init msg;
108         int err;
109
110         mtk_vcodec_debug_enter(vpu);
111
112         init_waitqueue_head(&vpu->wq);
113
114         err = vpu_ipi_register(vpu->dev, vpu->id, vpu->handler, "vdec", NULL);
115         if (err != 0) {
116                 mtk_vcodec_err(vpu, "vpu_ipi_register fail status=%d", err);
117                 return err;
118         }
119
120         memset(&msg, 0, sizeof(msg));
121         msg.msg_id = AP_IPIMSG_DEC_INIT;
122         msg.ap_inst_addr = (unsigned long)vpu;
123
124         mtk_vcodec_debug(vpu, "vdec_inst=%p", vpu);
125
126         err = vcodec_vpu_send_msg(vpu, (void *)&msg, sizeof(msg));
127         mtk_vcodec_debug(vpu, "- ret=%d", err);
128         return err;
129 }
130
131 int vpu_dec_start(struct vdec_vpu_inst *vpu, uint32_t *data, unsigned int len)
132 {
133         struct vdec_ap_ipi_dec_start msg;
134         int i;
135         int err = 0;
136
137         mtk_vcodec_debug_enter(vpu);
138
139         if (len > ARRAY_SIZE(msg.data)) {
140                 mtk_vcodec_err(vpu, "invalid len = %d\n", len);
141                 return -EINVAL;
142         }
143
144         memset(&msg, 0, sizeof(msg));
145         msg.msg_id = AP_IPIMSG_DEC_START;
146         msg.vpu_inst_addr = vpu->inst_addr;
147
148         for (i = 0; i < len; i++)
149                 msg.data[i] = data[i];
150
151         err = vcodec_vpu_send_msg(vpu, (void *)&msg, sizeof(msg));
152         mtk_vcodec_debug(vpu, "- ret=%d", err);
153         return err;
154 }
155
156 int vpu_dec_end(struct vdec_vpu_inst *vpu)
157 {
158         return vcodec_send_ap_ipi(vpu, AP_IPIMSG_DEC_END);
159 }
160
161 int vpu_dec_deinit(struct vdec_vpu_inst *vpu)
162 {
163         return vcodec_send_ap_ipi(vpu, AP_IPIMSG_DEC_DEINIT);
164 }
165
166 int vpu_dec_reset(struct vdec_vpu_inst *vpu)
167 {
168         return vcodec_send_ap_ipi(vpu, AP_IPIMSG_DEC_RESET);
169 }