2 * Copyright (C) 2012 Spreadtrum Communications Inc.
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
17 #include <linux/poll.h>
19 /* ****************************************************************** */
22 /* sipc processor ID definition */
24 SIPC_ID_AP = 0, /* Application Processor */
25 SIPC_ID_CPT, /* TD processor */
26 SIPC_ID_CPW, /* WCDMA processor */
27 SIPC_ID_WCN, /* Wireless Connectivity */
28 SIPC_ID_GGE, /* Gsm Gprs Edge processor */
29 SIPC_ID_LTE, /* LTE processor */
31 SIPC_ID_NR, /* total processor number */
34 /* share-mem ring buffer short message */
36 uint8_t channel; /* channel index */
37 uint8_t type; /* msg type */
38 uint16_t flag; /* msg flag */
39 uint32_t value; /* msg value */
42 /* smsg channel definition */
44 SMSG_CH_CTRL = 0, /* some emergency control */
45 SMSG_CH_COMM, /* general communication channel */
46 SMSG_CH_RPC_AP, /* RPC server channel in AP side */
47 SMSG_CH_RPC_CP, /* RPC server channel in CP side */
48 SMSG_CH_PIPE, /* general pipe channel */
49 SMSG_CH_PLOG, /* pipe for debug log/dump */
50 SMSG_CH_TTY, /* virtual serial for telephony */
51 SMSG_CH_DATA0, /* 2G/3G wirleless data */
52 SMSG_CH_DATA1, /* 2G/3G wirleless data */
53 SMSG_CH_DATA2, /* 2G/3G wirleless data */
54 SMSG_CH_VBC, /* audio conrol channel */
55 SMSG_CH_PLAYBACK, /* audio playback channel */
56 SMSG_CH_CAPTURE, /* audio capture channel */
57 SMSG_CH_MONITOR_AUDIO, /* audio monitor channel */
58 SMSG_CH_CTRL_VOIP, /* audio voip conrol channel */
59 SMSG_CH_PLAYBACK_VOIP, /* audio voip playback channel */
60 SMSG_CH_CAPTURE_VOIP, /* audio voip capture channel */
61 SMSG_CH_MONITOR_VOIP, /* audio voip monitor channel */
62 SMSG_CH_DATA3, /* 2G/3G wirleless data */
63 SMSG_CH_DATA4, /* 2G/3G wirleless data */
64 SMSG_CH_DATA5, /* 2G/3G wirleless data */
65 SMSG_CH_DIAG, /* pipe for debug log/dump */
66 SMSG_CH_NR, /* total channel number */
69 /* smsg type definition */
72 SMSG_TYPE_OPEN, /* first msg to open a channel */
73 SMSG_TYPE_CLOSE, /* last msg to close a channel */
74 SMSG_TYPE_DATA, /* data, value=addr, no ack */
75 SMSG_TYPE_EVENT, /* event with value, no ack */
76 SMSG_TYPE_CMD, /* command, value=cmd */
77 SMSG_TYPE_DONE, /* return of command */
78 SMSG_TYPE_SMEM_ALLOC, /* allocate smem, flag=order */
79 SMSG_TYPE_SMEM_FREE, /* free smem, flag=order, value=addr */
80 SMSG_TYPE_SMEM_DONE, /* return of alloc/free smem */
81 SMSG_TYPE_FUNC_CALL, /* RPC func, value=addr */
82 SMSG_TYPE_FUNC_RETURN, /* return of RPC func */
87 SMSG_TYPE_NR, /* total type number */
90 /* flag for OPEN/CLOSE msg type */
91 #define SMSG_OPEN_MAGIC 0xBEEE
92 #define SMSG_CLOSE_MAGIC 0xEDDD
95 * smsg_ch_open -- open a channel for smsg
97 * @dst: dest processor ID
98 * @channel: channel ID
99 * @timeout: milliseconds, 0 means no wait, -1 means unlimited
100 * @return: 0 on success, <0 on failue
102 int smsg_ch_open(uint8_t dst, uint8_t channel, int timeout);
105 * smsg_ch_close -- close a channel for smsg
107 * @dst: dest processor ID
108 * @channel: channel ID
109 * @timeout: milliseconds, 0 means no wait, -1 means unlimited
110 * @return: 0 on success, <0 on failue
112 int smsg_ch_close(uint8_t dst, uint8_t channel, int timeout);
115 * smsg_send -- send smsg
117 * @dst: dest processor ID
118 * @msg: smsg body to be sent
119 * @timeout: milliseconds, 0 means no wait, -1 means unlimited
120 * @return: 0 on success, <0 on failue
122 int smsg_send(uint8_t dst, struct smsg *msg, int timeout);
125 * smsg_recv -- poll and recv smsg
127 * @dst: dest processor ID
128 * @msg: smsg body to be received, channel should be filled as input
129 * @timeout: milliseconds, 0 means no wait, -1 means unlimited
130 * @return: 0 on success, <0 on failue
132 int smsg_recv(uint8_t dst, struct smsg *msg, int timeout);
134 /* quickly fill a smsg body */
135 static inline void smsg_set(struct smsg *msg, uint8_t channel,
136 uint8_t type, uint16_t flag, uint32_t value)
138 msg->channel = channel;
144 /* ack an open msg for modem recovery */
145 static inline void smsg_open_ack(uint8_t dst, uint16_t channel)
148 smsg_set(&mopen, channel, SMSG_TYPE_OPEN, SMSG_OPEN_MAGIC, 0);
149 smsg_send(dst, &mopen, -1);
152 /* ack an close msg for modem recovery */
153 static inline void smsg_close_ack(uint8_t dst, uint16_t channel)
156 smsg_set(&mclose, channel, SMSG_TYPE_CLOSE, SMSG_CLOSE_MAGIC, 0);
157 smsg_send(dst, &mclose, -1);
159 /* ****************************************************************** */
160 /* SMEM interfaces */
163 * smem_alloc -- allocate shared memory block
165 * @size: size to be allocated, page-aligned
166 * @return: phys addr or 0 if failed
168 uint32_t smem_alloc(uint32_t size);
171 * smem_free -- free shared memory block
173 * @addr: smem phys addr to be freed
174 * @order: size to be freed
176 void smem_free(uint32_t addr, uint32_t size);
178 /* ****************************************************************** */
179 /* SBUF Interfaces */
182 * sbuf_create -- create pipe ring buffers on a channel
184 * @dst: dest processor ID
185 * @channel: channel ID
186 * @txbufsize: tx buffer size
187 * @rxbufsize: rx buffer size
188 * @bufnum: how many buffers to be created
189 * @return: 0 on success, <0 on failue
191 int sbuf_create(uint8_t dst, uint8_t channel, uint32_t bufnum,
192 uint32_t txbufsize, uint32_t rxbufsize);
195 * sbuf_destroy -- destroy the pipe ring buffers on a channel
197 * @dst: dest processor ID
198 * @channel: channel ID
199 * @return: 0 on success, <0 on failue
201 void sbuf_destroy(uint8_t dst, uint8_t channel);
204 * sbuf_write -- write data to a sbuf
206 * @dst: dest processor ID
207 * @channel: channel ID
209 * @buf: data to be written
211 * @timeout: milliseconds, 0 means no wait, -1 means unlimited
212 * @return: written bytes on success, <0 on failue
214 int sbuf_write(uint8_t dst, uint8_t channel, uint32_t bufid,
215 void *buf, uint32_t len, int timeout);
218 * sbuf_read -- write data to a sbuf
220 * @dst: dest processor ID
221 * @channel: channel ID
223 * @buf: data to be written
225 * @timeout: milliseconds, 0 means no wait, -1 means unlimited
226 * @return: read bytes on success, <0 on failue
228 int sbuf_read(uint8_t dst, uint8_t channel, uint32_t bufid,
229 void *buf, uint32_t len, int timeout);
232 * sbuf_poll_wait -- poll sbuf read/write, used in spipe driver
234 * @dst: dest processor ID
235 * @channel: channel ID
237 * @file: struct file handler
239 * @return: POLLIN or POLLOUT
241 int sbuf_poll_wait(uint8_t dst, uint8_t channel, uint32_t bufid,
242 struct file *file, poll_table *wait);
245 * sbuf_status -- get sbuf status
247 * @dst: dest processor ID
248 * @channel: channel ID
249 * @return: 0 when ready, <0 when broken
251 int sbuf_status(uint8_t dst, uint8_t channel);
253 #define SBUF_NOTIFY_READ 0x01
254 #define SBUF_NOTIFY_WRITE 0x02
256 * sbuf_register_notifier -- register a callback that's called
257 * when a tx sbuf is available or a rx sbuf is received.
258 * non-blocked sbuf_read can be called.
260 * @dst: dest processor ID
261 * @channel: channel ID
263 * @handler: a callback handler
264 * @event: NOTIFY_READ, NOTIFY_WRITE, or both
265 * @data: opaque data passed to the receiver
266 * @return: 0 on success, <0 on failue
268 int sbuf_register_notifier(uint8_t dst, uint8_t channel, uint32_t bufid,
269 void (*handler)(int event, void *data), void *data);
271 /* ****************************************************************** */
272 /* SBLOCK interfaces */
274 /* sblock structure: addr is the uncached virtual address */
281 * sblock_create -- create sblock manager on a channel
283 * @dst: dest processor ID
284 * @channel: channel ID
285 * @txblocknum: tx block number
286 * @txblocksize: tx block size
287 * @rxblocknum: rx block number
288 * @rxblocksize: rx block size
289 * @return: 0 on success, <0 on failue
291 int sblock_create(uint8_t dst, uint8_t channel,
292 uint32_t txblocknum, uint32_t txblocksize,
293 uint32_t rxblocknum, uint32_t rxblocksize);
296 * sblock_destroy -- destroy sblock manager on a channel
298 * @dst: dest processor ID
299 * @channel: channel ID
301 void sblock_destroy(uint8_t dst, uint8_t channel);
303 #define SBLOCK_NOTIFY_GET 0x01
304 #define SBLOCK_NOTIFY_RECV 0x02
305 #define SBLOCK_NOTIFY_STATUS 0x04
306 #define SBLOCK_NOTIFY_OPEN 0x08
307 #define SBLOCK_NOTIFY_CLOSE 0x10
309 * sblock_register_notifier -- register a callback that's called
310 * when a tx sblock is available or a rx block is received.
311 * non-blocked sblock_get or sblock_receive can be called.
313 * @dst: dest processor ID
314 * @channel: channel ID
315 * @handler: a callback handler
316 * @event: SBLOCK_NOTIFY_GET, SBLOCK_NOTIFY_RECV, or both
317 * @data: opaque data passed to the receiver
318 * @return: 0 on success, <0 on failue
320 int sblock_register_notifier(uint8_t dst, uint8_t channel,
321 void (*handler)(int event, void *data), void *data);
324 * sblock_get -- get a free sblock for sender
326 * @dst: dest processor ID
327 * @channel: channel ID
328 * @blk: return a gotten sblock pointer
329 * @timeout: milliseconds, 0 means no wait, -1 means unlimited
330 * @return: 0 on success, <0 on failue
332 int sblock_get(uint8_t dst, uint8_t channel, struct sblock *blk, int timeout);
335 * sblock_send -- send a sblock with smsg, it should be from sblock_get
337 * @dst: dest processor ID
338 * @channel: channel ID
339 * @blk: the sblock to be sent
340 * @return: 0 on success, <0 on failue
342 int sblock_send(uint8_t dst, uint8_t channel, struct sblock *blk);
345 * sblock_send_prepare -- send a sblock without smsg, it should be from sblock_get
347 * @dst: dest processor ID
348 * @channel: channel ID
349 * @blk: the sblock to be sent
350 * @return: 0 on success, <0 on failue
352 int sblock_send_prepare(uint8_t dst, uint8_t channel, struct sblock *blk);
355 * sblock_send_finish -- trigger an smsg to notify that sblock has been sent
357 * @dst: dest processor ID
358 * @channel: channel ID
359 * @return: 0 on success, <0 on failue
361 int sblock_send_finish(uint8_t dst, uint8_t channel);
364 * sblock_receive -- receive a sblock, it should be released after it's handled
366 * @dst: dest processor ID
367 * @channel: channel ID
368 * @blk: return a received sblock pointer
369 * @timeout: milliseconds, 0 means no wait, -1 means unlimited
370 * @return: 0 on success, <0 on failue
372 int sblock_receive(uint8_t dst, uint8_t channel, struct sblock *blk, int timeout);
375 * sblock_release -- release a sblock from reveiver
377 * @dst: dest processor ID
378 * @channel: channel ID
379 * @return: 0 on success, <0 on failue
381 int sblock_release(uint8_t dst, uint8_t channel, struct sblock *blk);
384 * sblock_get_arrived_count -- get the count of sblock(s) arrived at AP (sblock_send on CP)
385 * but not received (sblock_receive on AP).
387 * @dst: dest processor ID
388 * @channel: channel ID
389 * @return: >=0 the count of blocks
391 int sblock_get_arrived_count(uint8_t dst, uint8_t channel);
396 * sblock_get_free_count -- get the count of available sblock(s) resident in sblock pool on AP.
398 * @dst: dest processor ID
399 * @channel: channel ID
400 * @return: >=0 the count of blocks
402 int sblock_get_free_count(uint8_t dst, uint8_t channel);
406 * sblock_put -- put a free sblock for sender
408 * @dst: dest processor ID
409 * @channel: channel ID
410 * @blk: sblock pointer
413 void sblock_put(uint8_t dst, uint8_t channel, struct sblock *blk);
415 /* ****************************************************************** */
416 /* TODO: SRPC interfaces */
434 int srpc_init(uint8_t dst);
435 int srpc_server_register(uint8_t dst, struct srpc_server *server);
436 int srpc_client_call(uint8_t dst, struct srpc_client *client);
437 int sctrl_send_sync(uint32_t type, uint32_t target_id, uint32_t value);
439 static inline unsigned long unalign_copy_to_user(void __user *to, const void *from, unsigned long n)
441 unsigned long rval = 0;
443 while (((unsigned long)from & 7) && n) {
444 rval |= copy_to_user(to++, from++, 1);
447 rval = copy_to_user(to, from, n);
451 static inline unsigned long unalign_copy_from_user(void *to, const void __user *from, unsigned long n)
453 unsigned long rval = 0;
455 while (((unsigned long)to & 7) && n) {
456 rval |= copy_from_user(to++, from++, 1);
459 rval = copy_from_user(to, from, n);
463 static inline void *unalign_memcpy(void *to, const void *from, size_t n)
465 if (((unsigned long)to & 7) == ((unsigned long)from & 7)) {
466 while (((unsigned long)from & 7) && n) {
467 *(char *)(to++) = *(char*)(from++);
473 *(char *)(to++) = *(char*)(from++);
479 static inline unsigned long unalign_copy_to_user(void __user *to, const void *from, unsigned long n)
481 return copy_to_user(to, from, n);
483 static inline unsigned long unalign_copy_from_user(void *to, const void __user *from, unsigned long n)
485 return copy_from_user(to, from, n);
487 static inline void *unalign_memcpy(void *to, const void *from, size_t n)
489 return memcpy(to, from, n);