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