blob: 78031c0a6db3012dda0c9f7677c46a857129315f [file] [log] [blame]
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001/* arch/arm/mach-msm/smd.c
2 *
3 * Copyright (C) 2007 Google, Inc.
4 * Author: Brian Swetland <swetland@google.com>
5 *
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 */
16
17#include <linux/platform_device.h>
18#include <linux/module.h>
19#include <linux/fs.h>
20#include <linux/cdev.h>
21#include <linux/device.h>
22#include <linux/wait.h>
23#include <linux/interrupt.h>
24#include <linux/irq.h>
25#include <linux/list.h>
26#include <linux/slab.h>
27#include <linux/debugfs.h>
28#include <linux/delay.h>
29#include <linux/io.h>
30
31#include <mach/msm_smd.h>
32#include <mach/msm_iomap.h>
33#include <mach/system.h>
34
35#include "smd_private.h"
36#include "proc_comm.h"
37
38void (*msm_hw_reset_hook)(void);
39
40#define MODULE_NAME "msm_smd"
41
42enum {
43 MSM_SMD_DEBUG = 1U << 0,
44 MSM_SMSM_DEBUG = 1U << 0,
45};
46
47static int msm_smd_debug_mask;
48
49module_param_named(debug_mask, msm_smd_debug_mask,
50 int, S_IRUGO | S_IWUSR | S_IWGRP);
51
52void *smem_find(unsigned id, unsigned size);
53static void smd_diag(void);
54
55static unsigned last_heap_free = 0xffffffff;
56
57#define MSM_A2M_INT(n) (MSM_CSR_BASE + 0x400 + (n) * 4)
58
59static inline void notify_other_smsm(void)
60{
61 writel(1, MSM_A2M_INT(5));
62}
63
64static inline void notify_other_smd(void)
65{
66 writel(1, MSM_A2M_INT(0));
67}
68
69static void smd_diag(void)
70{
71 char *x;
72
73 x = smem_find(ID_DIAG_ERR_MSG, SZ_DIAG_ERR_MSG);
74 if (x != 0) {
75 x[SZ_DIAG_ERR_MSG - 1] = 0;
76 pr_info("smem: DIAG '%s'\n", x);
77 }
78}
79
80/* call when SMSM_RESET flag is set in the A9's smsm_state */
81static void handle_modem_crash(void)
82{
83 pr_err("ARM9 has CRASHED\n");
84 smd_diag();
85
86 /* hard reboot if possible */
87 if (msm_hw_reset_hook)
88 msm_hw_reset_hook();
89
90 /* in this case the modem or watchdog should reboot us */
91 for (;;)
92 ;
93}
94
95extern int (*msm_check_for_modem_crash)(void);
96
97static int check_for_modem_crash(void)
98{
99 struct smsm_shared *smsm;
100
101 smsm = smem_find(ID_SHARED_STATE, 2 * sizeof(struct smsm_shared));
102
103 /* if the modem's not ready yet, we have to hope for the best */
104 if (!smsm)
105 return 0;
106
107 if (smsm[1].state & SMSM_RESET) {
108 handle_modem_crash();
109 return -1;
110 } else {
111 return 0;
112 }
113}
114
115#define SMD_SS_CLOSED 0x00000000
116#define SMD_SS_OPENING 0x00000001
117#define SMD_SS_OPENED 0x00000002
118#define SMD_SS_FLUSHING 0x00000003
119#define SMD_SS_CLOSING 0x00000004
120#define SMD_SS_RESET 0x00000005
121#define SMD_SS_RESET_OPENING 0x00000006
122
123#define SMD_BUF_SIZE 8192
124#define SMD_CHANNELS 64
125
126#define SMD_HEADER_SIZE 20
127
128
129/* the spinlock is used to synchronize between the
130** irq handler and code that mutates the channel
131** list or fiddles with channel state
132*/
133static DEFINE_SPINLOCK(smd_lock);
134static DEFINE_SPINLOCK(smem_lock);
135
136/* the mutex is used during open() and close()
137** operations to avoid races while creating or
138** destroying smd_channel structures
139*/
140static DEFINE_MUTEX(smd_creation_mutex);
141
142static int smd_initialized;
143
144struct smd_alloc_elm {
145 char name[20];
146 uint32_t cid;
147 uint32_t ctype;
148 uint32_t ref_count;
149};
150
151struct smd_half_channel {
152 unsigned state;
153 unsigned char fDSR;
154 unsigned char fCTS;
155 unsigned char fCD;
156 unsigned char fRI;
157 unsigned char fHEAD;
158 unsigned char fTAIL;
159 unsigned char fSTATE;
160 unsigned char fUNUSED;
161 unsigned tail;
162 unsigned head;
163 unsigned char data[SMD_BUF_SIZE];
164};
165
166struct smd_shared {
167 struct smd_half_channel ch0;
168 struct smd_half_channel ch1;
169};
170
171struct smd_channel {
172 volatile struct smd_half_channel *send;
173 volatile struct smd_half_channel *recv;
174 struct list_head ch_list;
175
176 unsigned current_packet;
177 unsigned n;
178 void *priv;
179 void (*notify)(void *priv, unsigned flags);
180
181 int (*read)(smd_channel_t *ch, void *data, int len);
182 int (*write)(smd_channel_t *ch, const void *data, int len);
183 int (*read_avail)(smd_channel_t *ch);
184 int (*write_avail)(smd_channel_t *ch);
185
186 void (*update_state)(smd_channel_t *ch);
187 unsigned last_state;
188
189 char name[32];
190 struct platform_device pdev;
191};
192
193static LIST_HEAD(smd_ch_closed_list);
194static LIST_HEAD(smd_ch_list);
195
196static unsigned char smd_ch_allocated[64];
197static struct work_struct probe_work;
198
199static void smd_alloc_channel(const char *name, uint32_t cid, uint32_t type);
200
201static void smd_channel_probe_worker(struct work_struct *work)
202{
203 struct smd_alloc_elm *shared;
204 unsigned n;
205
206 shared = smem_find(ID_CH_ALLOC_TBL, sizeof(*shared) * 64);
Brian Swetland4d4fb262009-01-28 20:25:40 -0800207 if (!shared) {
208 pr_err("smd: cannot find allocation table\n");
209 return;
210 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700211 for (n = 0; n < 64; n++) {
212 if (smd_ch_allocated[n])
213 continue;
214 if (!shared[n].ref_count)
215 continue;
216 if (!shared[n].name[0])
217 continue;
218 smd_alloc_channel(shared[n].name,
219 shared[n].cid,
220 shared[n].ctype);
221 smd_ch_allocated[n] = 1;
222 }
223}
224
225static char *chstate(unsigned n)
226{
227 switch (n) {
228 case SMD_SS_CLOSED:
229 return "CLOSED";
230 case SMD_SS_OPENING:
231 return "OPENING";
232 case SMD_SS_OPENED:
233 return "OPENED";
234 case SMD_SS_FLUSHING:
235 return "FLUSHING";
236 case SMD_SS_CLOSING:
237 return "CLOSING";
238 case SMD_SS_RESET:
239 return "RESET";
240 case SMD_SS_RESET_OPENING:
241 return "ROPENING";
242 default:
243 return "UNKNOWN";
244 }
245}
246
247/* how many bytes are available for reading */
248static int smd_stream_read_avail(struct smd_channel *ch)
249{
250 return (ch->recv->head - ch->recv->tail) & (SMD_BUF_SIZE - 1);
251}
252
253/* how many bytes we are free to write */
254static int smd_stream_write_avail(struct smd_channel *ch)
255{
256 return (SMD_BUF_SIZE - 1) -
257 ((ch->send->head - ch->send->tail) & (SMD_BUF_SIZE - 1));
258}
259
260static int smd_packet_read_avail(struct smd_channel *ch)
261{
262 if (ch->current_packet) {
263 int n = smd_stream_read_avail(ch);
264 if (n > ch->current_packet)
265 n = ch->current_packet;
266 return n;
267 } else {
268 return 0;
269 }
270}
271
272static int smd_packet_write_avail(struct smd_channel *ch)
273{
274 int n = smd_stream_write_avail(ch);
275 return n > SMD_HEADER_SIZE ? n - SMD_HEADER_SIZE : 0;
276}
277
278static int ch_is_open(struct smd_channel *ch)
279{
280 return (ch->recv->state == SMD_SS_OPENED) &&
281 (ch->send->state == SMD_SS_OPENED);
282}
283
284/* provide a pointer and length to readable data in the fifo */
285static unsigned ch_read_buffer(struct smd_channel *ch, void **ptr)
286{
287 unsigned head = ch->recv->head;
288 unsigned tail = ch->recv->tail;
289 *ptr = (void *) (ch->recv->data + tail);
290
291 if (tail <= head)
292 return head - tail;
293 else
294 return SMD_BUF_SIZE - tail;
295}
296
297/* advance the fifo read pointer after data from ch_read_buffer is consumed */
298static void ch_read_done(struct smd_channel *ch, unsigned count)
299{
300 BUG_ON(count > smd_stream_read_avail(ch));
301 ch->recv->tail = (ch->recv->tail + count) & (SMD_BUF_SIZE - 1);
302 ch->recv->fTAIL = 1;
303}
304
305/* basic read interface to ch_read_{buffer,done} used
306** by smd_*_read() and update_packet_state()
307** will read-and-discard if the _data pointer is null
308*/
309static int ch_read(struct smd_channel *ch, void *_data, int len)
310{
311 void *ptr;
312 unsigned n;
313 unsigned char *data = _data;
314 int orig_len = len;
315
316 while (len > 0) {
317 n = ch_read_buffer(ch, &ptr);
318 if (n == 0)
319 break;
320
321 if (n > len)
322 n = len;
323 if (_data)
324 memcpy(data, ptr, n);
325
326 data += n;
327 len -= n;
328 ch_read_done(ch, n);
329 }
330
331 return orig_len - len;
332}
333
334static void update_stream_state(struct smd_channel *ch)
335{
336 /* streams have no special state requiring updating */
337}
338
339static void update_packet_state(struct smd_channel *ch)
340{
341 unsigned hdr[5];
342 int r;
343
344 /* can't do anything if we're in the middle of a packet */
345 if (ch->current_packet != 0)
346 return;
347
348 /* don't bother unless we can get the full header */
349 if (smd_stream_read_avail(ch) < SMD_HEADER_SIZE)
350 return;
351
352 r = ch_read(ch, hdr, SMD_HEADER_SIZE);
353 BUG_ON(r != SMD_HEADER_SIZE);
354
355 ch->current_packet = hdr[0];
356}
357
358/* provide a pointer and length to next free space in the fifo */
359static unsigned ch_write_buffer(struct smd_channel *ch, void **ptr)
360{
361 unsigned head = ch->send->head;
362 unsigned tail = ch->send->tail;
363 *ptr = (void *) (ch->send->data + head);
364
365 if (head < tail) {
366 return tail - head - 1;
367 } else {
368 if (tail == 0)
369 return SMD_BUF_SIZE - head - 1;
370 else
371 return SMD_BUF_SIZE - head;
372 }
373}
374
375/* advace the fifo write pointer after freespace
376 * from ch_write_buffer is filled
377 */
378static void ch_write_done(struct smd_channel *ch, unsigned count)
379{
380 BUG_ON(count > smd_stream_write_avail(ch));
381 ch->send->head = (ch->send->head + count) & (SMD_BUF_SIZE - 1);
382 ch->send->fHEAD = 1;
383}
384
385static void hc_set_state(volatile struct smd_half_channel *hc, unsigned n)
386{
387 if (n == SMD_SS_OPENED) {
388 hc->fDSR = 1;
389 hc->fCTS = 1;
390 hc->fCD = 1;
391 } else {
392 hc->fDSR = 0;
393 hc->fCTS = 0;
394 hc->fCD = 0;
395 }
396 hc->state = n;
397 hc->fSTATE = 1;
398 notify_other_smd();
399}
400
401static void do_smd_probe(void)
402{
403 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
404 if (shared->heap_info.free_offset != last_heap_free) {
405 last_heap_free = shared->heap_info.free_offset;
406 schedule_work(&probe_work);
407 }
408}
409
410static void smd_state_change(struct smd_channel *ch,
411 unsigned last, unsigned next)
412{
413 ch->last_state = next;
414
415 pr_info("SMD: ch %d %s -> %s\n", ch->n,
416 chstate(last), chstate(next));
417
418 switch (next) {
419 case SMD_SS_OPENING:
420 ch->recv->tail = 0;
421 case SMD_SS_OPENED:
422 if (ch->send->state != SMD_SS_OPENED)
423 hc_set_state(ch->send, SMD_SS_OPENED);
424 ch->notify(ch->priv, SMD_EVENT_OPEN);
425 break;
426 case SMD_SS_FLUSHING:
427 case SMD_SS_RESET:
428 /* we should force them to close? */
429 default:
430 ch->notify(ch->priv, SMD_EVENT_CLOSE);
431 }
432}
433
434static irqreturn_t smd_irq_handler(int irq, void *data)
435{
436 unsigned long flags;
437 struct smd_channel *ch;
438 int do_notify = 0;
439 unsigned ch_flags;
440 unsigned tmp;
441
442 spin_lock_irqsave(&smd_lock, flags);
443 list_for_each_entry(ch, &smd_ch_list, ch_list) {
444 ch_flags = 0;
445 if (ch_is_open(ch)) {
446 if (ch->recv->fHEAD) {
447 ch->recv->fHEAD = 0;
448 ch_flags |= 1;
449 do_notify |= 1;
450 }
451 if (ch->recv->fTAIL) {
452 ch->recv->fTAIL = 0;
453 ch_flags |= 2;
454 do_notify |= 1;
455 }
456 if (ch->recv->fSTATE) {
457 ch->recv->fSTATE = 0;
458 ch_flags |= 4;
459 do_notify |= 1;
460 }
461 }
462 tmp = ch->recv->state;
463 if (tmp != ch->last_state)
464 smd_state_change(ch, ch->last_state, tmp);
465 if (ch_flags) {
466 ch->update_state(ch);
467 ch->notify(ch->priv, SMD_EVENT_DATA);
468 }
469 }
470 if (do_notify)
471 notify_other_smd();
472 spin_unlock_irqrestore(&smd_lock, flags);
473 do_smd_probe();
474 return IRQ_HANDLED;
475}
476
477static void smd_fake_irq_handler(unsigned long arg)
478{
479 smd_irq_handler(0, NULL);
480}
481
482static DECLARE_TASKLET(smd_fake_irq_tasklet, smd_fake_irq_handler, 0);
483
484void smd_sleep_exit(void)
485{
486 unsigned long flags;
487 struct smd_channel *ch;
488 unsigned tmp;
489 int need_int = 0;
490
491 spin_lock_irqsave(&smd_lock, flags);
492 list_for_each_entry(ch, &smd_ch_list, ch_list) {
493 if (ch_is_open(ch)) {
494 if (ch->recv->fHEAD) {
495 if (msm_smd_debug_mask & MSM_SMD_DEBUG)
496 pr_info("smd_sleep_exit ch %d fHEAD "
497 "%x %x %x\n",
498 ch->n, ch->recv->fHEAD,
499 ch->recv->head, ch->recv->tail);
500 need_int = 1;
501 break;
502 }
503 if (ch->recv->fTAIL) {
504 if (msm_smd_debug_mask & MSM_SMD_DEBUG)
505 pr_info("smd_sleep_exit ch %d fTAIL "
506 "%x %x %x\n",
507 ch->n, ch->recv->fTAIL,
508 ch->send->head, ch->send->tail);
509 need_int = 1;
510 break;
511 }
512 if (ch->recv->fSTATE) {
513 if (msm_smd_debug_mask & MSM_SMD_DEBUG)
514 pr_info("smd_sleep_exit ch %d fSTATE %x"
515 "\n", ch->n, ch->recv->fSTATE);
516 need_int = 1;
517 break;
518 }
519 tmp = ch->recv->state;
520 if (tmp != ch->last_state) {
521 if (msm_smd_debug_mask & MSM_SMD_DEBUG)
522 pr_info("smd_sleep_exit ch %d "
523 "state %x != %x\n",
524 ch->n, tmp, ch->last_state);
525 need_int = 1;
526 break;
527 }
528 }
529 }
530 spin_unlock_irqrestore(&smd_lock, flags);
531 do_smd_probe();
532 if (need_int) {
533 if (msm_smd_debug_mask & MSM_SMD_DEBUG)
534 pr_info("smd_sleep_exit need interrupt\n");
535 tasklet_schedule(&smd_fake_irq_tasklet);
536 }
537}
538
539
540void smd_kick(smd_channel_t *ch)
541{
542 unsigned long flags;
543 unsigned tmp;
544
545 spin_lock_irqsave(&smd_lock, flags);
546 ch->update_state(ch);
547 tmp = ch->recv->state;
548 if (tmp != ch->last_state) {
549 ch->last_state = tmp;
550 if (tmp == SMD_SS_OPENED)
551 ch->notify(ch->priv, SMD_EVENT_OPEN);
552 else
553 ch->notify(ch->priv, SMD_EVENT_CLOSE);
554 }
555 ch->notify(ch->priv, SMD_EVENT_DATA);
556 notify_other_smd();
557 spin_unlock_irqrestore(&smd_lock, flags);
558}
559
560static int smd_is_packet(int chn)
561{
562 if ((chn > 4) || (chn == 1))
563 return 1;
564 else
565 return 0;
566}
567
568static int smd_stream_write(smd_channel_t *ch, const void *_data, int len)
569{
570 void *ptr;
571 const unsigned char *buf = _data;
572 unsigned xfer;
573 int orig_len = len;
574
575 if (len < 0)
576 return -EINVAL;
577
578 while ((xfer = ch_write_buffer(ch, &ptr)) != 0) {
579 if (!ch_is_open(ch))
580 break;
581 if (xfer > len)
582 xfer = len;
583 memcpy(ptr, buf, xfer);
584 ch_write_done(ch, xfer);
585 len -= xfer;
586 buf += xfer;
587 if (len == 0)
588 break;
589 }
590
591 notify_other_smd();
592
593 return orig_len - len;
594}
595
596static int smd_packet_write(smd_channel_t *ch, const void *_data, int len)
597{
598 unsigned hdr[5];
599
600 if (len < 0)
601 return -EINVAL;
602
603 if (smd_stream_write_avail(ch) < (len + SMD_HEADER_SIZE))
604 return -ENOMEM;
605
606 hdr[0] = len;
607 hdr[1] = hdr[2] = hdr[3] = hdr[4] = 0;
608
609 smd_stream_write(ch, hdr, sizeof(hdr));
610 smd_stream_write(ch, _data, len);
611
612 return len;
613}
614
615static int smd_stream_read(smd_channel_t *ch, void *data, int len)
616{
617 int r;
618
619 if (len < 0)
620 return -EINVAL;
621
622 r = ch_read(ch, data, len);
623 if (r > 0)
624 notify_other_smd();
625
626 return r;
627}
628
629static int smd_packet_read(smd_channel_t *ch, void *data, int len)
630{
631 unsigned long flags;
632 int r;
633
634 if (len < 0)
635 return -EINVAL;
636
637 if (len > ch->current_packet)
638 len = ch->current_packet;
639
640 r = ch_read(ch, data, len);
641 if (r > 0)
642 notify_other_smd();
643
644 spin_lock_irqsave(&smd_lock, flags);
645 ch->current_packet -= r;
646 update_packet_state(ch);
647 spin_unlock_irqrestore(&smd_lock, flags);
648
649 return r;
650}
651
652static void smd_alloc_channel(const char *name, uint32_t cid, uint32_t type)
653{
654 struct smd_channel *ch;
655 struct smd_shared *shared;
656
657 shared = smem_alloc(ID_SMD_CHANNELS + cid, sizeof(*shared));
658 if (!shared) {
659 pr_err("smd_alloc_channel() cid %d does not exist\n", cid);
660 return;
661 }
662
663 ch = kzalloc(sizeof(struct smd_channel), GFP_KERNEL);
664 if (ch == 0) {
665 pr_err("smd_alloc_channel() out of memory\n");
666 return;
667 }
668
669 ch->send = &shared->ch0;
670 ch->recv = &shared->ch1;
671 ch->n = cid;
672
673 if (smd_is_packet(cid)) {
674 ch->read = smd_packet_read;
675 ch->write = smd_packet_write;
676 ch->read_avail = smd_packet_read_avail;
677 ch->write_avail = smd_packet_write_avail;
678 ch->update_state = update_packet_state;
679 } else {
680 ch->read = smd_stream_read;
681 ch->write = smd_stream_write;
682 ch->read_avail = smd_stream_read_avail;
683 ch->write_avail = smd_stream_write_avail;
684 ch->update_state = update_stream_state;
685 }
686
687 memcpy(ch->name, "SMD_", 4);
688 memcpy(ch->name + 4, name, 20);
689 ch->name[23] = 0;
690 ch->pdev.name = ch->name;
691 ch->pdev.id = -1;
692
693 pr_info("smd_alloc_channel() '%s' cid=%d, shared=%p\n",
694 ch->name, ch->n, shared);
695
696 mutex_lock(&smd_creation_mutex);
697 list_add(&ch->ch_list, &smd_ch_closed_list);
698 mutex_unlock(&smd_creation_mutex);
699
700 platform_device_register(&ch->pdev);
701}
702
703static void do_nothing_notify(void *priv, unsigned flags)
704{
705}
706
707struct smd_channel *smd_get_channel(const char *name)
708{
709 struct smd_channel *ch;
710
711 mutex_lock(&smd_creation_mutex);
712 list_for_each_entry(ch, &smd_ch_closed_list, ch_list) {
713 if (!strcmp(name, ch->name)) {
714 list_del(&ch->ch_list);
715 mutex_unlock(&smd_creation_mutex);
716 return ch;
717 }
718 }
719 mutex_unlock(&smd_creation_mutex);
720
721 return NULL;
722}
723
724int smd_open(const char *name, smd_channel_t **_ch,
725 void *priv, void (*notify)(void *, unsigned))
726{
727 struct smd_channel *ch;
728 unsigned long flags;
729
730 if (smd_initialized == 0) {
731 pr_info("smd_open() before smd_init()\n");
732 return -ENODEV;
733 }
734
735 ch = smd_get_channel(name);
736 if (!ch)
737 return -ENODEV;
738
739 if (notify == 0)
740 notify = do_nothing_notify;
741
742 ch->notify = notify;
743 ch->current_packet = 0;
744 ch->last_state = SMD_SS_CLOSED;
745 ch->priv = priv;
746
747 *_ch = ch;
748
749 spin_lock_irqsave(&smd_lock, flags);
750 list_add(&ch->ch_list, &smd_ch_list);
751
752 /* If the remote side is CLOSING, we need to get it to
753 * move to OPENING (which we'll do by moving from CLOSED to
754 * OPENING) and then get it to move from OPENING to
755 * OPENED (by doing the same state change ourselves).
756 *
757 * Otherwise, it should be OPENING and we can move directly
758 * to OPENED so that it will follow.
759 */
760 if (ch->recv->state == SMD_SS_CLOSING) {
761 ch->send->head = 0;
762 hc_set_state(ch->send, SMD_SS_OPENING);
763 } else {
764 hc_set_state(ch->send, SMD_SS_OPENED);
765 }
766 spin_unlock_irqrestore(&smd_lock, flags);
767 smd_kick(ch);
768
769 return 0;
770}
771
772int smd_close(smd_channel_t *ch)
773{
774 unsigned long flags;
775
776 pr_info("smd_close(%p)\n", ch);
777
778 if (ch == 0)
779 return -1;
780
781 spin_lock_irqsave(&smd_lock, flags);
782 ch->notify = do_nothing_notify;
783 list_del(&ch->ch_list);
784 hc_set_state(ch->send, SMD_SS_CLOSED);
785 spin_unlock_irqrestore(&smd_lock, flags);
786
787 mutex_lock(&smd_creation_mutex);
788 list_add(&ch->ch_list, &smd_ch_closed_list);
789 mutex_unlock(&smd_creation_mutex);
790
791 return 0;
792}
793
794int smd_read(smd_channel_t *ch, void *data, int len)
795{
796 return ch->read(ch, data, len);
797}
798
799int smd_write(smd_channel_t *ch, const void *data, int len)
800{
801 return ch->write(ch, data, len);
802}
803
804int smd_read_avail(smd_channel_t *ch)
805{
806 return ch->read_avail(ch);
807}
808
809int smd_write_avail(smd_channel_t *ch)
810{
811 return ch->write_avail(ch);
812}
813
814int smd_wait_until_readable(smd_channel_t *ch, int bytes)
815{
816 return -1;
817}
818
819int smd_wait_until_writable(smd_channel_t *ch, int bytes)
820{
821 return -1;
822}
823
824int smd_cur_packet_size(smd_channel_t *ch)
825{
826 return ch->current_packet;
827}
828
829
830/* ------------------------------------------------------------------------- */
831
832void *smem_alloc(unsigned id, unsigned size)
833{
834 return smem_find(id, size);
835}
836
837static void *_smem_find(unsigned id, unsigned *size)
838{
839 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
840 struct smem_heap_entry *toc = shared->heap_toc;
841
842 if (id >= SMEM_NUM_ITEMS)
843 return 0;
844
845 if (toc[id].allocated) {
846 *size = toc[id].size;
847 return (void *) (MSM_SHARED_RAM_BASE + toc[id].offset);
848 }
849
850 return 0;
851}
852
853void *smem_find(unsigned id, unsigned size_in)
854{
855 unsigned size;
856 void *ptr;
857
858 ptr = _smem_find(id, &size);
859 if (!ptr)
860 return 0;
861
862 size_in = ALIGN(size_in, 8);
863 if (size_in != size) {
864 pr_err("smem_find(%d, %d): wrong size %d\n",
865 id, size_in, size);
866 return 0;
867 }
868
869 return ptr;
870}
871
872static irqreturn_t smsm_irq_handler(int irq, void *data)
873{
874 unsigned long flags;
875 struct smsm_shared *smsm;
876
877 spin_lock_irqsave(&smem_lock, flags);
878 smsm = smem_alloc(ID_SHARED_STATE,
879 2 * sizeof(struct smsm_shared));
880
881 if (smsm == 0) {
882 pr_info("<SM NO STATE>\n");
883 } else {
884 unsigned apps = smsm[0].state;
885 unsigned modm = smsm[1].state;
886
887 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
888 pr_info("<SM %08x %08x>\n", apps, modm);
889 if (modm & SMSM_RESET) {
890 handle_modem_crash();
891 } else {
892 apps |= SMSM_INIT;
893 if (modm & SMSM_SMDINIT)
894 apps |= SMSM_SMDINIT;
895 if (modm & SMSM_RPCINIT)
896 apps |= SMSM_RPCINIT;
897 }
898
899 if (smsm[0].state != apps) {
900 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
901 pr_info("<SM %08x NOTIFY>\n", apps);
902 smsm[0].state = apps;
903 do_smd_probe();
904 notify_other_smsm();
905 }
906 }
907 spin_unlock_irqrestore(&smem_lock, flags);
908 return IRQ_HANDLED;
909}
910
911int smsm_change_state(uint32_t clear_mask, uint32_t set_mask)
912{
913 unsigned long flags;
914 struct smsm_shared *smsm;
915
916 spin_lock_irqsave(&smem_lock, flags);
917
918 smsm = smem_alloc(ID_SHARED_STATE,
919 2 * sizeof(struct smsm_shared));
920
921 if (smsm) {
922 if (smsm[1].state & SMSM_RESET)
923 handle_modem_crash();
924 smsm[0].state = (smsm[0].state & ~clear_mask) | set_mask;
925 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
926 pr_info("smsm_change_state %x\n",
927 smsm[0].state);
928 notify_other_smsm();
929 }
930
931 spin_unlock_irqrestore(&smem_lock, flags);
932
933 if (smsm == NULL) {
934 pr_err("smsm_change_state <SM NO STATE>\n");
935 return -EIO;
936 }
937 return 0;
938}
939
940uint32_t smsm_get_state(void)
941{
942 unsigned long flags;
943 struct smsm_shared *smsm;
944 uint32_t rv;
945
946 spin_lock_irqsave(&smem_lock, flags);
947
948 smsm = smem_alloc(ID_SHARED_STATE,
949 2 * sizeof(struct smsm_shared));
950
951 if (smsm)
952 rv = smsm[1].state;
953 else
954 rv = 0;
955
956 if (rv & SMSM_RESET)
957 handle_modem_crash();
958
959 spin_unlock_irqrestore(&smem_lock, flags);
960
961 if (smsm == NULL)
962 pr_err("smsm_get_state <SM NO STATE>\n");
963 return rv;
964}
965
966int smsm_set_sleep_duration(uint32_t delay)
967{
968 uint32_t *ptr;
969
970 ptr = smem_alloc(SMEM_SMSM_SLEEP_DELAY, sizeof(*ptr));
971 if (ptr == NULL) {
972 pr_err("smsm_set_sleep_duration <SM NO SLEEP_DELAY>\n");
973 return -EIO;
974 }
975 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
976 pr_info("smsm_set_sleep_duration %d -> %d\n",
977 *ptr, delay);
978 *ptr = delay;
979 return 0;
980}
981
982int smsm_set_interrupt_info(struct smsm_interrupt_info *info)
983{
984 struct smsm_interrupt_info *ptr;
985
986 ptr = smem_alloc(SMEM_SMSM_INT_INFO, sizeof(*ptr));
987 if (ptr == NULL) {
988 pr_err("smsm_set_sleep_duration <SM NO INT_INFO>\n");
989 return -EIO;
990 }
991 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
992 pr_info("smsm_set_interrupt_info %x %x -> %x %x\n",
993 ptr->aArm_en_mask, ptr->aArm_interrupts_pending,
994 info->aArm_en_mask, info->aArm_interrupts_pending);
995 *ptr = *info;
996 return 0;
997}
998
999#define MAX_NUM_SLEEP_CLIENTS 64
1000#define MAX_SLEEP_NAME_LEN 8
1001
1002#define NUM_GPIO_INT_REGISTERS 6
1003#define GPIO_SMEM_NUM_GROUPS 2
1004#define GPIO_SMEM_MAX_PC_INTERRUPTS 8
1005
1006struct tramp_gpio_save {
1007 unsigned int enable;
1008 unsigned int detect;
1009 unsigned int polarity;
1010};
1011
1012struct tramp_gpio_smem {
1013 uint16_t num_fired[GPIO_SMEM_NUM_GROUPS];
1014 uint16_t fired[GPIO_SMEM_NUM_GROUPS][GPIO_SMEM_MAX_PC_INTERRUPTS];
1015 uint32_t enabled[NUM_GPIO_INT_REGISTERS];
1016 uint32_t detection[NUM_GPIO_INT_REGISTERS];
1017 uint32_t polarity[NUM_GPIO_INT_REGISTERS];
1018};
1019
1020
1021void smsm_print_sleep_info(void)
1022{
1023 unsigned long flags;
1024 uint32_t *ptr;
1025 struct tramp_gpio_smem *gpio;
1026 struct smsm_interrupt_info *int_info;
1027
1028
1029 spin_lock_irqsave(&smem_lock, flags);
1030
1031 ptr = smem_alloc(SMEM_SMSM_SLEEP_DELAY, sizeof(*ptr));
1032 if (ptr)
1033 pr_info("SMEM_SMSM_SLEEP_DELAY: %x\n", *ptr);
1034
1035 ptr = smem_alloc(SMEM_SMSM_LIMIT_SLEEP, sizeof(*ptr));
1036 if (ptr)
1037 pr_info("SMEM_SMSM_LIMIT_SLEEP: %x\n", *ptr);
1038
1039 ptr = smem_alloc(SMEM_SLEEP_POWER_COLLAPSE_DISABLED, sizeof(*ptr));
1040 if (ptr)
1041 pr_info("SMEM_SLEEP_POWER_COLLAPSE_DISABLED: %x\n", *ptr);
1042
1043 int_info = smem_alloc(SMEM_SMSM_INT_INFO, sizeof(*int_info));
1044 if (int_info)
1045 pr_info("SMEM_SMSM_INT_INFO %x %x %x\n",
1046 int_info->aArm_en_mask,
1047 int_info->aArm_interrupts_pending,
1048 int_info->aArm_wakeup_reason);
1049
1050 gpio = smem_alloc(SMEM_GPIO_INT, sizeof(*gpio));
1051 if (gpio) {
1052 int i;
1053 for (i = 0; i < NUM_GPIO_INT_REGISTERS; i++)
1054 pr_info("SMEM_GPIO_INT: %d: e %x d %x p %x\n",
1055 i, gpio->enabled[i], gpio->detection[i],
1056 gpio->polarity[i]);
1057
1058 for (i = 0; i < GPIO_SMEM_NUM_GROUPS; i++)
1059 pr_info("SMEM_GPIO_INT: %d: f %d: %d %d...\n",
1060 i, gpio->num_fired[i], gpio->fired[i][0],
1061 gpio->fired[i][1]);
1062 }
1063
1064 spin_unlock_irqrestore(&smem_lock, flags);
1065}
1066
1067int smd_core_init(void)
1068{
1069 int r;
1070 pr_info("smd_core_init()\n");
1071
1072 r = request_irq(INT_A9_M2A_0, smd_irq_handler,
1073 IRQF_TRIGGER_RISING, "smd_dev", 0);
1074 if (r < 0)
1075 return r;
1076 r = enable_irq_wake(INT_A9_M2A_0);
1077 if (r < 0)
1078 pr_err("smd_core_init: enable_irq_wake failed for A9_M2A_0\n");
1079
1080 r = request_irq(INT_A9_M2A_5, smsm_irq_handler,
1081 IRQF_TRIGGER_RISING, "smsm_dev", 0);
1082 if (r < 0) {
1083 free_irq(INT_A9_M2A_0, 0);
1084 return r;
1085 }
1086 r = enable_irq_wake(INT_A9_M2A_5);
1087 if (r < 0)
1088 pr_err("smd_core_init: enable_irq_wake failed for A9_M2A_5\n");
1089
1090 /* we may have missed a signal while booting -- fake
1091 * an interrupt to make sure we process any existing
1092 * state
1093 */
1094 smsm_irq_handler(0, 0);
1095
1096 pr_info("smd_core_init() done\n");
1097
1098 return 0;
1099}
1100
1101#if defined(CONFIG_DEBUG_FS)
1102
1103static int dump_ch(char *buf, int max, int n,
1104 struct smd_half_channel *s,
1105 struct smd_half_channel *r)
1106{
1107 return scnprintf(
1108 buf, max,
1109 "ch%02d:"
1110 " %8s(%04d/%04d) %c%c%c%c%c%c%c <->"
1111 " %8s(%04d/%04d) %c%c%c%c%c%c%c\n", n,
1112 chstate(s->state), s->tail, s->head,
1113 s->fDSR ? 'D' : 'd',
1114 s->fCTS ? 'C' : 'c',
1115 s->fCD ? 'C' : 'c',
1116 s->fRI ? 'I' : 'i',
1117 s->fHEAD ? 'W' : 'w',
1118 s->fTAIL ? 'R' : 'r',
1119 s->fSTATE ? 'S' : 's',
1120 chstate(r->state), r->tail, r->head,
1121 r->fDSR ? 'D' : 'd',
1122 r->fCTS ? 'R' : 'r',
1123 r->fCD ? 'C' : 'c',
1124 r->fRI ? 'I' : 'i',
1125 r->fHEAD ? 'W' : 'w',
1126 r->fTAIL ? 'R' : 'r',
1127 r->fSTATE ? 'S' : 's'
1128 );
1129}
1130
1131static int debug_read_stat(char *buf, int max)
1132{
1133 struct smsm_shared *smsm;
1134 char *msg;
1135 int i = 0;
1136
1137 smsm = smem_find(ID_SHARED_STATE,
1138 2 * sizeof(struct smsm_shared));
1139
1140 msg = smem_find(ID_DIAG_ERR_MSG, SZ_DIAG_ERR_MSG);
1141
1142 if (smsm) {
1143 if (smsm[1].state & SMSM_RESET)
1144 i += scnprintf(buf + i, max - i,
1145 "smsm: ARM9 HAS CRASHED\n");
1146 i += scnprintf(buf + i, max - i, "smsm: a9: %08x a11: %08x\n",
1147 smsm[0].state, smsm[1].state);
1148 } else {
1149 i += scnprintf(buf + i, max - i, "smsm: cannot find\n");
1150 }
1151 if (msg) {
1152 msg[SZ_DIAG_ERR_MSG - 1] = 0;
1153 i += scnprintf(buf + i, max - i, "diag: '%s'\n", msg);
1154 }
1155 return i;
1156}
1157
1158static int debug_read_mem(char *buf, int max)
1159{
1160 unsigned n;
1161 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
1162 struct smem_heap_entry *toc = shared->heap_toc;
1163 int i = 0;
1164
1165 i += scnprintf(buf + i, max - i,
1166 "heap: init=%d free=%d remain=%d\n",
1167 shared->heap_info.initialized,
1168 shared->heap_info.free_offset,
1169 shared->heap_info.heap_remaining);
1170
1171 for (n = 0; n < SMEM_NUM_ITEMS; n++) {
1172 if (toc[n].allocated == 0)
1173 continue;
1174 i += scnprintf(buf + i, max - i,
1175 "%04d: offsed %08x size %08x\n",
1176 n, toc[n].offset, toc[n].size);
1177 }
1178 return i;
1179}
1180
1181static int debug_read_ch(char *buf, int max)
1182{
1183 struct smd_shared *shared;
1184 int n, i = 0;
1185
1186 for (n = 0; n < SMD_CHANNELS; n++) {
1187 shared = smem_find(ID_SMD_CHANNELS + n,
1188 sizeof(struct smd_shared));
1189 if (shared == 0)
1190 continue;
1191 i += dump_ch(buf + i, max - i, n, &shared->ch0, &shared->ch1);
1192 }
1193
1194 return i;
1195}
1196
1197static int debug_read_version(char *buf, int max)
1198{
1199 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
1200 unsigned version = shared->version[VERSION_MODEM];
1201 return sprintf(buf, "%d.%d\n", version >> 16, version & 0xffff);
1202}
1203
1204static int debug_read_build_id(char *buf, int max)
1205{
1206 unsigned size;
1207 void *data;
1208
1209 data = _smem_find(SMEM_HW_SW_BUILD_ID, &size);
1210 if (!data)
1211 return 0;
1212
1213 if (size >= max)
1214 size = max;
1215 memcpy(buf, data, size);
1216
1217 return size;
1218}
1219
1220static int debug_read_alloc_tbl(char *buf, int max)
1221{
1222 struct smd_alloc_elm *shared;
1223 int n, i = 0;
1224
1225 shared = smem_find(ID_CH_ALLOC_TBL, sizeof(*shared) * 64);
1226
1227 for (n = 0; n < 64; n++) {
1228 if (shared[n].ref_count == 0)
1229 continue;
1230 i += scnprintf(buf + i, max - i,
1231 "%03d: %20s cid=%02d ctype=%d ref_count=%d\n",
1232 n, shared[n].name, shared[n].cid,
1233 shared[n].ctype, shared[n].ref_count);
1234 }
1235
1236 return i;
1237}
1238
1239static int debug_boom(char *buf, int max)
1240{
1241 unsigned ms = 5000;
1242 msm_proc_comm(PCOM_RESET_MODEM, &ms, 0);
1243 return 0;
1244}
1245
1246#define DEBUG_BUFMAX 4096
1247static char debug_buffer[DEBUG_BUFMAX];
1248
1249static ssize_t debug_read(struct file *file, char __user *buf,
1250 size_t count, loff_t *ppos)
1251{
1252 int (*fill)(char *buf, int max) = file->private_data;
1253 int bsize = fill(debug_buffer, DEBUG_BUFMAX);
1254 return simple_read_from_buffer(buf, count, ppos, debug_buffer, bsize);
1255}
1256
1257static int debug_open(struct inode *inode, struct file *file)
1258{
1259 file->private_data = inode->i_private;
1260 return 0;
1261}
1262
1263static const struct file_operations debug_ops = {
1264 .read = debug_read,
1265 .open = debug_open,
1266};
1267
1268static void debug_create(const char *name, mode_t mode,
1269 struct dentry *dent,
1270 int (*fill)(char *buf, int max))
1271{
1272 debugfs_create_file(name, mode, dent, fill, &debug_ops);
1273}
1274
1275static void smd_debugfs_init(void)
1276{
1277 struct dentry *dent;
1278
1279 dent = debugfs_create_dir("smd", 0);
1280 if (IS_ERR(dent))
1281 return;
1282
1283 debug_create("ch", 0444, dent, debug_read_ch);
1284 debug_create("stat", 0444, dent, debug_read_stat);
1285 debug_create("mem", 0444, dent, debug_read_mem);
1286 debug_create("version", 0444, dent, debug_read_version);
1287 debug_create("tbl", 0444, dent, debug_read_alloc_tbl);
1288 debug_create("build", 0444, dent, debug_read_build_id);
1289 debug_create("boom", 0444, dent, debug_boom);
1290}
1291#else
1292static void smd_debugfs_init(void) {}
1293#endif
1294
1295static int __init msm_smd_probe(struct platform_device *pdev)
1296{
1297 pr_info("smd_init()\n");
1298
1299 INIT_WORK(&probe_work, smd_channel_probe_worker);
1300
1301 if (smd_core_init()) {
1302 pr_err("smd_core_init() failed\n");
1303 return -1;
1304 }
1305
1306 do_smd_probe();
1307
1308 msm_check_for_modem_crash = check_for_modem_crash;
1309
1310 smd_debugfs_init();
1311 smd_initialized = 1;
1312
1313 return 0;
1314}
1315
1316static struct platform_driver msm_smd_driver = {
1317 .probe = msm_smd_probe,
1318 .driver = {
1319 .name = MODULE_NAME,
1320 .owner = THIS_MODULE,
1321 },
1322};
1323
1324static int __init msm_smd_init(void)
1325{
1326 return platform_driver_register(&msm_smd_driver);
1327}
1328
1329module_init(msm_smd_init);
1330
1331MODULE_DESCRIPTION("MSM Shared Memory Core");
1332MODULE_AUTHOR("Brian Swetland <swetland@google.com>");
1333MODULE_LICENSE("GPL");