1*418b791dSBob Badour /**
2*418b791dSBob Badour * Copyright (c) 2019, The Linux Foundation. All rights reserved.
3*418b791dSBob Badour *
4*418b791dSBob Badour * Redistribution and use in source and binary forms, with or without
5*418b791dSBob Badour * modification, are permitted provided that the following conditions are
6*418b791dSBob Badour * met:
7*418b791dSBob Badour * * Redistributions of source code must retain the above copyright
8*418b791dSBob Badour * notice, this list of conditions and the following disclaimer.
9*418b791dSBob Badour * * Redistributions in binary form must reproduce the above
10*418b791dSBob Badour * copyright notice, this list of conditions and the following
11*418b791dSBob Badour * disclaimer in the documentation and/or other materials provided
12*418b791dSBob Badour * with the distribution.
13*418b791dSBob Badour * * Neither the name of The Linux Foundation nor the names of its
14*418b791dSBob Badour * contributors may be used to endorse or promote products derived
15*418b791dSBob Badour * from this software without specific prior written permission.
16*418b791dSBob Badour *
17*418b791dSBob Badour * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18*418b791dSBob Badour * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19*418b791dSBob Badour * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20*418b791dSBob Badour * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21*418b791dSBob Badour * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22*418b791dSBob Badour * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23*418b791dSBob Badour * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24*418b791dSBob Badour * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25*418b791dSBob Badour * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26*418b791dSBob Badour * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27*418b791dSBob Badour * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*418b791dSBob Badour */
29*418b791dSBob Badour
30*418b791dSBob Badour #ifndef FASTRPC_INTERNAL_H
31*418b791dSBob Badour #define FASTRPC_INTERNAL_H
32*418b791dSBob Badour
33*418b791dSBob Badour #include <linux/types.h>
34*418b791dSBob Badour #include "remote64.h"
35*418b791dSBob Badour #include "verify.h"
36*418b791dSBob Badour #include "AEEstd.h"
37*418b791dSBob Badour
38*418b791dSBob Badour #define FASTRPC_IOCTL_ALLOC_DMA_BUFF _IOWR('R', 1, struct fastrpc_alloc_dma_buf)
39*418b791dSBob Badour #define FASTRPC_IOCTL_FREE_DMA_BUFF _IOWR('R', 2, uint32_t)
40*418b791dSBob Badour #define FASTRPC_IOCTL_INVOKE _IOWR('R', 3, struct fastrpc_invoke)
41*418b791dSBob Badour #define FASTRPC_IOCTL_INIT_ATTACH _IO('R', 4)
42*418b791dSBob Badour #define FASTRPC_IOCTL_INIT_CREATE _IOWR('R', 5, struct fastrpc_init_create)
43*418b791dSBob Badour #define FASTRPC_IOCTL_MMAP _IOWR('R', 6, struct fastrpc_ioctl_mmap)
44*418b791dSBob Badour #define FASTRPC_IOCTL_MUNMAP _IOWR('R', 7, struct fastrpc_ioctl_munmap)
45*418b791dSBob Badour
46*418b791dSBob Badour
47*418b791dSBob Badour #define DEVICE_NAME "adsprpc-smd"
48*418b791dSBob Badour
49*418b791dSBob Badour #if !(defined __qdsp6__) && !(defined __hexagon__)
Q6_R_cl0_R(uint32 num)50*418b791dSBob Badour static __inline uint32 Q6_R_cl0_R(uint32 num) {
51*418b791dSBob Badour int ii;
52*418b791dSBob Badour for(ii = 31; ii >= 0; --ii) {
53*418b791dSBob Badour if(num & (1 << ii)) {
54*418b791dSBob Badour return 31 - ii;
55*418b791dSBob Badour }
56*418b791dSBob Badour }
57*418b791dSBob Badour return 0;
58*418b791dSBob Badour }
59*418b791dSBob Badour #else
60*418b791dSBob Badour #include "hexagon_protos.h"
61*418b791dSBob Badour #include <types.h>
62*418b791dSBob Badour #endif
63*418b791dSBob Badour
64*418b791dSBob Badour #define FASTRPC_INFO_SMMU (1 << 0)
65*418b791dSBob Badour
66*418b791dSBob Badour /* struct fastrpc_invoke_args {
67*418b791dSBob Badour __u64 ptr;
68*418b791dSBob Badour __u64 length;
69*418b791dSBob Badour __s32 fd;
70*418b791dSBob Badour __u32 attrs;
71*418b791dSBob Badour __u32 crc;
72*418b791dSBob Badour }; */
73*418b791dSBob Badour
74*418b791dSBob Badour struct fastrpc_invoke_args {
75*418b791dSBob Badour __u64 ptr;
76*418b791dSBob Badour __u64 length;
77*418b791dSBob Badour __s32 fd;
78*418b791dSBob Badour __u32 reserved;
79*418b791dSBob Badour };
80*418b791dSBob Badour
81*418b791dSBob Badour struct fastrpc_invoke {
82*418b791dSBob Badour __u32 handle;
83*418b791dSBob Badour __u32 sc;
84*418b791dSBob Badour __u64 args;
85*418b791dSBob Badour };
86*418b791dSBob Badour
87*418b791dSBob Badour #define FASTRPC_ATTR_NOVA (1)
88*418b791dSBob Badour #define FASTRPC_ATTR_NOMAP (16)
89*418b791dSBob Badour
90*418b791dSBob Badour #define GUEST_OS 0
91*418b791dSBob Badour #define USER_PD -1
92*418b791dSBob Badour #define STATIC_USER_PD 1
93*418b791dSBob Badour #define ATTACH_SENSORS_PD 2
94*418b791dSBob Badour #define GUEST_OS_SHARED 3
95*418b791dSBob Badour
96*418b791dSBob Badour struct fastrpc_init_create {
97*418b791dSBob Badour __u32 filelen; /* elf file length */
98*418b791dSBob Badour __s32 filefd; /* fd for the file */
99*418b791dSBob Badour __u32 attrs;
100*418b791dSBob Badour __u32 siglen;
101*418b791dSBob Badour __u64 file; /* pointer to elf file */
102*418b791dSBob Badour };
103*418b791dSBob Badour
104*418b791dSBob Badour #define FASTRPC_ATTR_DEBUG_PROCESS (1)
105*418b791dSBob Badour
106*418b791dSBob Badour struct fastrpc_alloc_dma_buf {
107*418b791dSBob Badour __s32 fd; /* fd */
108*418b791dSBob Badour __u32 flags; /* flags to map with */
109*418b791dSBob Badour __u64 size; /* size */
110*418b791dSBob Badour };
111*418b791dSBob Badour
112*418b791dSBob Badour struct fastrpc_ioctl_mmap {
113*418b791dSBob Badour __s32 fd; /* fd */
114*418b791dSBob Badour __u32 flags; /* flags for dsp to map with */
115*418b791dSBob Badour __u64 vaddrin; /* optional virtual address */
116*418b791dSBob Badour __u64 size; /* size */
117*418b791dSBob Badour __u64 vaddrout; /* dsps virtual address */
118*418b791dSBob Badour };
119*418b791dSBob Badour
120*418b791dSBob Badour struct fastrpc_ioctl_munmap {
121*418b791dSBob Badour __u64 vaddrout; /* address to unmap */
122*418b791dSBob Badour __u64 size; /* size */
123*418b791dSBob Badour };
124*418b791dSBob Badour
125*418b791dSBob Badour #define FASTRPC_CONTROL_LATENCY (1)
126*418b791dSBob Badour struct fastrpc_ctrl_latency {
127*418b791dSBob Badour uint32_t enable; //!latency control enable
128*418b791dSBob Badour uint32_t level; //!level of control
129*418b791dSBob Badour };
130*418b791dSBob Badour #define FASTRPC_CONTROL_SMMU (2)
131*418b791dSBob Badour struct fastrpc_ctrl_smmu {
132*418b791dSBob Badour uint32_t sharedcb;
133*418b791dSBob Badour };
134*418b791dSBob Badour
135*418b791dSBob Badour #define FASTRPC_CONTROL_KALLOC (3)
136*418b791dSBob Badour struct fastrpc_ctrl_kalloc {
137*418b791dSBob Badour uint32_t kalloc_support;
138*418b791dSBob Badour };
139*418b791dSBob Badour
140*418b791dSBob Badour struct fastrpc_ioctl_control {
141*418b791dSBob Badour uint32_t req;
142*418b791dSBob Badour union {
143*418b791dSBob Badour struct fastrpc_ctrl_latency lp;
144*418b791dSBob Badour struct fastrpc_ctrl_smmu smmu;
145*418b791dSBob Badour struct fastrpc_ctrl_kalloc kalloc;
146*418b791dSBob Badour };
147*418b791dSBob Badour };
148*418b791dSBob Badour
149*418b791dSBob Badour #define FASTRPC_SMD_GUID "fastrpcsmd-apps-dsp"
150*418b791dSBob Badour
151*418b791dSBob Badour struct smq_null_invoke32 {
152*418b791dSBob Badour uint32_t ctx; //! invoke caller context
153*418b791dSBob Badour remote_handle handle; //! handle to invoke
154*418b791dSBob Badour uint32_t sc; //! scalars structure describing the rest of the data
155*418b791dSBob Badour };
156*418b791dSBob Badour
157*418b791dSBob Badour struct smq_null_invoke {
158*418b791dSBob Badour uint64_t ctx; //! invoke caller context
159*418b791dSBob Badour remote_handle handle; //! handle to invoke
160*418b791dSBob Badour uint32_t sc; //! scalars structure describing the rest of the data
161*418b791dSBob Badour };
162*418b791dSBob Badour
163*418b791dSBob Badour typedef uint32_t smq_invoke_buf_phy_addr;
164*418b791dSBob Badour
165*418b791dSBob Badour struct smq_phy_page {
166*418b791dSBob Badour uint64_t addr; //! physical address
167*418b791dSBob Badour int64_t size; //! size
168*418b791dSBob Badour };
169*418b791dSBob Badour
170*418b791dSBob Badour struct smq_phy_page32 {
171*418b791dSBob Badour uint32_t addr; //! physical address
172*418b791dSBob Badour uint32_t size; //! size
173*418b791dSBob Badour };
174*418b791dSBob Badour
175*418b791dSBob Badour struct smq_invoke_buf {
176*418b791dSBob Badour int num;
177*418b791dSBob Badour int pgidx;
178*418b791dSBob Badour };
179*418b791dSBob Badour
180*418b791dSBob Badour struct smq_invoke32 {
181*418b791dSBob Badour struct smq_null_invoke32 header;
182*418b791dSBob Badour struct smq_phy_page32 page; //! remote arg and list of pages address
183*418b791dSBob Badour };
184*418b791dSBob Badour
185*418b791dSBob Badour struct smq_invoke {
186*418b791dSBob Badour struct smq_null_invoke header;
187*418b791dSBob Badour struct smq_phy_page page; //! remote arg and list of pages address
188*418b791dSBob Badour };
189*418b791dSBob Badour
190*418b791dSBob Badour struct smq_msg32 {
191*418b791dSBob Badour uint32_t pid;
192*418b791dSBob Badour uint32_t tid;
193*418b791dSBob Badour struct smq_invoke32 invoke;
194*418b791dSBob Badour };
195*418b791dSBob Badour
196*418b791dSBob Badour struct smq_msg {
197*418b791dSBob Badour uint32_t pid;
198*418b791dSBob Badour uint32_t tid;
199*418b791dSBob Badour struct smq_invoke invoke;
200*418b791dSBob Badour };
201*418b791dSBob Badour
202*418b791dSBob Badour struct smq_msg_u {
203*418b791dSBob Badour union {
204*418b791dSBob Badour struct smq_msg32 msg32;
205*418b791dSBob Badour struct smq_msg msg64;
206*418b791dSBob Badour } msg;
207*418b791dSBob Badour int size;
208*418b791dSBob Badour };
209*418b791dSBob Badour
210*418b791dSBob Badour struct smq_invoke_rsp32 {
211*418b791dSBob Badour uint32_t ctx; //! invoke caller context
212*418b791dSBob Badour int nRetVal; //! invoke return value
213*418b791dSBob Badour };
214*418b791dSBob Badour
215*418b791dSBob Badour struct smq_invoke_rsp {
216*418b791dSBob Badour uint64_t ctx; //! invoke caller context
217*418b791dSBob Badour int nRetVal; //! invoke return value
218*418b791dSBob Badour };
219*418b791dSBob Badour
220*418b791dSBob Badour struct smq_invoke_rsp_u {
221*418b791dSBob Badour union {
222*418b791dSBob Badour struct smq_invoke_rsp32 rsp32;
223*418b791dSBob Badour struct smq_invoke_rsp rsp64;
224*418b791dSBob Badour } rsp;
225*418b791dSBob Badour int size;
226*418b791dSBob Badour };
227*418b791dSBob Badour
to_smq_msg(uint32 mode,struct smq_msg_u * msg,struct smq_msg * msg64)228*418b791dSBob Badour static __inline void to_smq_msg(uint32 mode, struct smq_msg_u* msg, struct smq_msg* msg64) {
229*418b791dSBob Badour if(0 == mode) {
230*418b791dSBob Badour msg64->pid = msg->msg.msg32.pid;
231*418b791dSBob Badour msg64->tid = msg->msg.msg32.tid;
232*418b791dSBob Badour msg64->invoke.header.ctx = msg->msg.msg32.invoke.header.ctx;
233*418b791dSBob Badour msg64->invoke.header.handle = msg->msg.msg32.invoke.header.handle;
234*418b791dSBob Badour msg64->invoke.header.sc = msg->msg.msg32.invoke.header.sc;
235*418b791dSBob Badour msg64->invoke.page.addr = msg->msg.msg32.invoke.page.addr;
236*418b791dSBob Badour msg64->invoke.page.size = msg->msg.msg32.invoke.page.size;
237*418b791dSBob Badour } else {
238*418b791dSBob Badour std_memmove(msg64, &msg->msg.msg64, sizeof(*msg64));
239*418b791dSBob Badour }
240*418b791dSBob Badour }
241*418b791dSBob Badour
to_smq_invoke_rsp(uint32 mode,uint64 ctx,int nRetVal,struct smq_invoke_rsp_u * rsp)242*418b791dSBob Badour static __inline void to_smq_invoke_rsp(uint32 mode, uint64 ctx, int nRetVal, struct smq_invoke_rsp_u* rsp) {
243*418b791dSBob Badour if (0 == mode) {
244*418b791dSBob Badour rsp->rsp.rsp32.ctx = (uint32)ctx;
245*418b791dSBob Badour rsp->rsp.rsp32.nRetVal = nRetVal;
246*418b791dSBob Badour rsp->size = sizeof(rsp->rsp.rsp32);
247*418b791dSBob Badour } else {
248*418b791dSBob Badour rsp->rsp.rsp64.ctx = ctx;
249*418b791dSBob Badour rsp->rsp.rsp64.nRetVal = nRetVal;
250*418b791dSBob Badour rsp->size = sizeof(rsp->rsp.rsp64);
251*418b791dSBob Badour }
252*418b791dSBob Badour }
253*418b791dSBob Badour
to_smq_invoke_buf_start(uint32 mode,void * virt,uint32 sc)254*418b791dSBob Badour static __inline struct smq_invoke_buf* to_smq_invoke_buf_start(uint32 mode, void* virt, uint32 sc) {
255*418b791dSBob Badour struct smq_invoke_buf* buf;
256*418b791dSBob Badour int len = REMOTE_SCALARS_LENGTH(sc);
257*418b791dSBob Badour if(0 == mode) {
258*418b791dSBob Badour remote_arg* pra = (remote_arg*)virt;
259*418b791dSBob Badour buf = (struct smq_invoke_buf*)(&pra[len]);
260*418b791dSBob Badour } else {
261*418b791dSBob Badour remote_arg64* pra = (remote_arg64*)virt;
262*418b791dSBob Badour buf = (struct smq_invoke_buf*)(&pra[len]);
263*418b791dSBob Badour }
264*418b791dSBob Badour return buf;
265*418b791dSBob Badour }
266*418b791dSBob Badour
smq_invoke_buf_start(remote_arg64 * pra,uint32 sc)267*418b791dSBob Badour static __inline struct smq_invoke_buf* smq_invoke_buf_start(remote_arg64 *pra, uint32 sc) {
268*418b791dSBob Badour int len = REMOTE_SCALARS_LENGTH(sc);
269*418b791dSBob Badour return (struct smq_invoke_buf*)(&pra[len]);
270*418b791dSBob Badour }
271*418b791dSBob Badour
smq_phy_page_start(uint32 sc,struct smq_invoke_buf * buf)272*418b791dSBob Badour static __inline struct smq_phy_page* smq_phy_page_start(uint32 sc, struct smq_invoke_buf* buf) {
273*418b791dSBob Badour int nTotal = REMOTE_SCALARS_INBUFS(sc) + REMOTE_SCALARS_OUTBUFS(sc);
274*418b791dSBob Badour return (struct smq_phy_page*)(&buf[nTotal]);
275*418b791dSBob Badour }
276*418b791dSBob Badour
277*418b791dSBob Badour //! size of the out of band data
smq_data_size(uint32 sc,int nPages)278*418b791dSBob Badour static __inline int smq_data_size(uint32 sc, int nPages) {
279*418b791dSBob Badour struct smq_invoke_buf* buf = smq_invoke_buf_start(0, sc);
280*418b791dSBob Badour struct smq_phy_page* page = smq_phy_page_start(sc, buf);
281*418b791dSBob Badour return (int)(uintptr_t)(&(page[nPages]));
282*418b791dSBob Badour }
283*418b791dSBob Badour
to_smq_data(uint32 mode,uint32 sc,int nPages,void * pv,remote_arg64 * rpra)284*418b791dSBob Badour static __inline void to_smq_data(uint32 mode, uint32 sc, int nPages, void* pv, remote_arg64* rpra) {
285*418b791dSBob Badour if(0 == mode) {
286*418b791dSBob Badour struct smq_phy_page* page;
287*418b791dSBob Badour struct smq_phy_page32* page32;
288*418b791dSBob Badour remote_arg *pra = (remote_arg*)pv;
289*418b791dSBob Badour int ii, len;
290*418b791dSBob Badour len = REMOTE_SCALARS_LENGTH(sc);
291*418b791dSBob Badour for(ii = 0; ii < len; ++ii) {
292*418b791dSBob Badour rpra[ii].buf.pv = (uint64)(uintptr_t)pra[ii].buf.pv;
293*418b791dSBob Badour rpra[ii].buf.nLen = pra[ii].buf.nLen;
294*418b791dSBob Badour }
295*418b791dSBob Badour len = REMOTE_SCALARS_INBUFS(sc) + REMOTE_SCALARS_OUTBUFS(sc);
296*418b791dSBob Badour std_memmove(&rpra[ii], &pra[ii], len * sizeof(struct smq_invoke_buf));
297*418b791dSBob Badour page = (struct smq_phy_page*)((struct smq_invoke_buf*)&rpra[ii] + len);
298*418b791dSBob Badour page32 = (struct smq_phy_page32*)((struct smq_invoke_buf*)&pra[ii] + len);
299*418b791dSBob Badour for(ii = 0; ii < nPages; ++ii) {
300*418b791dSBob Badour page[ii].addr = page32[ii].addr;
301*418b791dSBob Badour page[ii].size = page32[ii].size;
302*418b791dSBob Badour }
303*418b791dSBob Badour } else {
304*418b791dSBob Badour std_memmove(rpra, pv, smq_data_size(sc, nPages));
305*418b791dSBob Badour }
306*418b791dSBob Badour }
307*418b791dSBob Badour
308*418b791dSBob Badour #endif // FASTRPC_INTERNAL_H
309