drm/gem: completely close gem_open vs. gem_close races
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / include / linux / sipc.h
1 /*
2  * Copyright (C) 2012 Spreadtrum Communications Inc.
3  *
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.
7  *
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.
12  */
13
14 #ifndef __SIPC_H
15 #define __SIPC_H
16
17 #include <linux/poll.h>
18
19 /* ****************************************************************** */
20 /* SMSG interfaces */
21
22 /* sipc processor ID definition */
23 enum {
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 */
30         SIPC_ID_PMIC,
31         SIPC_ID_NR,             /* total processor number */
32 };
33
34 /* share-mem ring buffer short message */
35 struct smsg {
36         uint8_t                 channel;        /* channel index */
37         uint8_t                 type;           /* msg type */
38         uint16_t                flag;           /* msg flag */
39         uint32_t                value;          /* msg value */
40 };
41
42 /* smsg channel definition */
43 enum {
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 */
67 };
68
69 /* smsg type definition */
70 enum {
71         SMSG_TYPE_NONE = 0,
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 */
83         SMSG_TYPE_DIE,
84         SMSG_TYPE_DFS,
85         SMSG_TYPE_DFS_RSP,
86         SMSG_TYPE_ASS_TRG,
87         SMSG_TYPE_NR,           /* total type number */
88 };
89
90 /* flag for OPEN/CLOSE msg type */
91 #define SMSG_OPEN_MAGIC         0xBEEE
92 #define SMSG_CLOSE_MAGIC        0xEDDD
93
94 /**
95  * smsg_ch_open -- open a channel for smsg
96  *
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
101  */
102 int smsg_ch_open(uint8_t dst, uint8_t channel, int timeout);
103
104 /**
105  * smsg_ch_close -- close a channel for smsg
106  *
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
111  */
112 int smsg_ch_close(uint8_t dst, uint8_t channel, int timeout);
113
114 /**
115  * smsg_send -- send smsg
116  *
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
121  */
122 int smsg_send(uint8_t dst, struct smsg *msg, int timeout);
123
124 /**
125  * smsg_recv -- poll and recv smsg
126  *
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
131  */
132 int smsg_recv(uint8_t dst, struct smsg *msg, int timeout);
133
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)
137 {
138         msg->channel = channel;
139         msg->type = type;
140         msg->flag = flag;
141         msg->value = value;
142 }
143
144 /* ack an open msg for modem recovery */
145 static inline void smsg_open_ack(uint8_t dst, uint16_t channel)
146 {
147         struct smsg mopen;
148         smsg_set(&mopen, channel, SMSG_TYPE_OPEN, SMSG_OPEN_MAGIC, 0);
149         smsg_send(dst, &mopen, -1);
150 }
151
152 /* ack an close msg for modem recovery */
153 static inline void smsg_close_ack(uint8_t dst, uint16_t channel)
154 {
155         struct smsg mclose;
156         smsg_set(&mclose, channel, SMSG_TYPE_CLOSE, SMSG_CLOSE_MAGIC, 0);
157         smsg_send(dst, &mclose, -1);
158 }
159 /* ****************************************************************** */
160 /* SMEM interfaces */
161
162 /**
163  * smem_alloc -- allocate shared memory block
164  *
165  * @size: size to be allocated, page-aligned
166  * @return: phys addr or 0 if failed
167  */
168 uint32_t smem_alloc(uint32_t size);
169
170 /**
171  * smem_free -- free shared memory block
172  *
173  * @addr: smem phys addr to be freed
174  * @order: size to be freed
175  */
176 void smem_free(uint32_t addr, uint32_t size);
177
178 /* ****************************************************************** */
179 /* SBUF Interfaces */
180
181 /**
182  * sbuf_create -- create pipe ring buffers on a channel
183  *
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
190  */
191 int sbuf_create(uint8_t dst, uint8_t channel, uint32_t bufnum,
192                 uint32_t txbufsize, uint32_t rxbufsize);
193
194 /**
195  * sbuf_destroy -- destroy the pipe ring buffers on a channel
196  *
197  * @dst: dest processor ID
198  * @channel: channel ID
199  * @return: 0 on success, <0 on failue
200  */
201 void sbuf_destroy(uint8_t dst, uint8_t channel);
202
203 /**
204  * sbuf_write -- write data to a sbuf
205  *
206  * @dst: dest processor ID
207  * @channel: channel ID
208  * @bufid: buffer ID
209  * @buf: data to be written
210  * @len: data length
211  * @timeout: milliseconds, 0 means no wait, -1 means unlimited
212  * @return: written bytes on success, <0 on failue
213  */
214 int sbuf_write(uint8_t dst, uint8_t channel, uint32_t bufid,
215                 void *buf, uint32_t len, int timeout);
216
217 /**
218  * sbuf_read -- write data to a sbuf
219  *
220  * @dst: dest processor ID
221  * @channel: channel ID
222  * @bufid: buffer ID
223  * @buf: data to be written
224  * @len: data length
225  * @timeout: milliseconds, 0 means no wait, -1 means unlimited
226  * @return: read bytes on success, <0 on failue
227  */
228 int sbuf_read(uint8_t dst, uint8_t channel, uint32_t bufid,
229                 void *buf, uint32_t len, int timeout);
230
231 /**
232  * sbuf_poll_wait -- poll sbuf read/write, used in spipe driver
233  *
234  * @dst: dest processor ID
235  * @channel: channel ID
236  * @bufid: buffer ID
237  * @file: struct file handler
238  * @wait: poll table
239  * @return: POLLIN or POLLOUT
240  */
241 int sbuf_poll_wait(uint8_t dst, uint8_t channel, uint32_t bufid,
242                 struct file *file, poll_table *wait);
243
244 /**
245  * sbuf_status -- get sbuf status
246  *
247  * @dst: dest processor ID
248  * @channel: channel ID
249  * @return: 0 when ready, <0 when broken
250  */
251 int sbuf_status(uint8_t dst, uint8_t channel);
252
253 #define SBUF_NOTIFY_READ        0x01
254 #define SBUF_NOTIFY_WRITE       0x02
255 /**
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.
259  *
260  * @dst: dest processor ID
261  * @channel: channel ID
262  * @bufid: buf 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
267  */
268 int sbuf_register_notifier(uint8_t dst, uint8_t channel, uint32_t bufid,
269                 void (*handler)(int event, void *data), void *data);
270
271 /* ****************************************************************** */
272 /* SBLOCK interfaces */
273
274 /* sblock structure: addr is the uncached virtual address */
275 struct sblock {
276         void            *addr;
277         uint32_t        length;
278 };
279
280 /**
281  * sblock_create -- create sblock manager on a channel
282  *
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
290  */
291 int sblock_create(uint8_t dst, uint8_t channel,
292                 uint32_t txblocknum, uint32_t txblocksize,
293                 uint32_t rxblocknum, uint32_t rxblocksize);
294
295 /**
296  * sblock_destroy -- destroy sblock manager on a channel
297  *
298  * @dst: dest processor ID
299  * @channel: channel ID
300  */
301 void sblock_destroy(uint8_t dst, uint8_t channel);
302
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
308 /**
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.
312  *
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
319  */
320 int sblock_register_notifier(uint8_t dst, uint8_t channel,
321                 void (*handler)(int event, void *data), void *data);
322
323 /**
324  * sblock_get  -- get a free sblock for sender
325  *
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
331  */
332 int sblock_get(uint8_t dst, uint8_t channel, struct sblock *blk, int timeout);
333
334 /**
335  * sblock_send  -- send a sblock with smsg, it should be from sblock_get
336  *
337  * @dst: dest processor ID
338  * @channel: channel ID
339  * @blk: the sblock to be sent
340  * @return: 0 on success, <0 on failue
341  */
342 int sblock_send(uint8_t dst, uint8_t channel, struct sblock *blk);
343
344 /**
345  * sblock_send_prepare  -- send a sblock without smsg, it should be from sblock_get
346  *
347  * @dst: dest processor ID
348  * @channel: channel ID
349  * @blk: the sblock to be sent
350  * @return: 0 on success, <0 on failue
351  */
352 int sblock_send_prepare(uint8_t dst, uint8_t channel, struct sblock *blk);
353
354 /**
355  * sblock_send_finish  -- trigger an smsg to notify that sblock has been sent
356  *
357  * @dst: dest processor ID
358  * @channel: channel ID
359  * @return: 0 on success, <0 on failue
360  */
361 int sblock_send_finish(uint8_t dst, uint8_t channel);
362
363 /**
364  * sblock_receive  -- receive a sblock, it should be released after it's handled
365  *
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
371  */
372 int sblock_receive(uint8_t dst, uint8_t channel, struct sblock *blk, int timeout);
373
374 /**
375  * sblock_release  -- release a sblock from reveiver
376  *
377  * @dst: dest processor ID
378  * @channel: channel ID
379  * @return: 0 on success, <0 on failue
380  */
381 int sblock_release(uint8_t dst, uint8_t channel, struct sblock *blk);
382
383 /**
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).
386  *
387  * @dst: dest processor ID
388  * @channel: channel ID
389  * @return: >=0  the count of blocks
390  */
391 int sblock_get_arrived_count(uint8_t dst, uint8_t channel);
392
393
394
395 /**
396  * sblock_get_free_count  -- get the count of available sblock(s) resident in sblock pool on AP.
397  *
398  * @dst: dest processor ID
399  * @channel: channel ID
400  * @return: >=0  the count of blocks
401  */
402 int sblock_get_free_count(uint8_t dst, uint8_t channel);
403
404
405 /**
406  * sblock_put  -- put a free sblock for sender
407  *
408  * @dst: dest processor ID
409  * @channel: channel ID
410  * @blk: sblock pointer
411  * @return: void
412  */
413 void sblock_put(uint8_t dst, uint8_t channel, struct sblock *blk);
414
415 /* ****************************************************************** */
416 /* TODO: SRPC interfaces */
417
418 enum {
419         SPRC_ID_NONE = 0,
420         SPRC_ID_XXX,
421         SPRC_ID_NR,
422 };
423
424 struct srpc_server {
425         uint32_t                id;
426         /* TODO */
427 };
428
429 struct srpc_client {
430         uint32_t                id;
431         /* TODO */
432 };
433
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);
438 #ifdef CONFIG_64BIT
439 static inline unsigned long unalign_copy_to_user(void __user *to, const void *from, unsigned long n)
440 {
441         unsigned long rval = 0;
442
443         while (((unsigned long)from & 7) && n) {
444                 rval |= copy_to_user(to++, from++, 1);
445                 n--;
446         }
447         rval = copy_to_user(to, from, n);
448
449         return rval;
450 }
451 static inline unsigned long unalign_copy_from_user(void *to, const void __user *from, unsigned long n)
452 {
453         unsigned long rval = 0;
454
455         while (((unsigned long)to & 7) && n) {
456                 rval |= copy_from_user(to++, from++, 1);
457                 n--;
458         }
459         rval = copy_from_user(to, from, n);
460
461         return rval;
462 }
463 static inline void *unalign_memcpy(void *to, const void *from, size_t n)
464 {
465         if (((unsigned long)to & 7) == ((unsigned long)from & 7)) {
466                 while (((unsigned long)from & 7) && n) {
467                         *(char *)(to++) = *(char*)(from++);
468                         n--;
469                 }
470                 memcpy(to, from, n);
471         } else {
472                 while (n) {
473                         *(char *)(to++) = *(char*)(from++);
474                         n--;
475                 }
476         }
477 }
478 #else
479 static inline unsigned long unalign_copy_to_user(void __user *to, const void *from, unsigned long n)
480 {
481         return copy_to_user(to, from, n);
482 }
483 static inline unsigned long unalign_copy_from_user(void *to, const void __user *from, unsigned long n)
484 {
485         return copy_from_user(to, from, n);
486 }
487 static inline void *unalign_memcpy(void *to, const void *from, size_t n)
488 {
489         return memcpy(to, from, n);
490 }
491 #endif
492
493
494 #endif