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