1 /* arch/arm/mach-msm/smd.c
3 * Copyright (C) 2007 Google, Inc.
4 * Author: Brian Swetland <swetland@google.com>
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.
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.
17 #include <linux/platform_device.h>
18 #include <linux/module.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>
31 #include <mach/msm_smd.h>
32 #include <mach/msm_iomap.h>
33 #include <mach/system.h>
35 #include "smd_private.h"
36 #include "proc_comm.h"
38 void (*msm_hw_reset_hook)(void);
40 #define MODULE_NAME "msm_smd"
43 MSM_SMD_DEBUG = 1U << 0,
44 MSM_SMSM_DEBUG = 1U << 0,
47 static int msm_smd_debug_mask;
56 static unsigned dummy_state_apps;
57 static unsigned dummy_state_modem;
59 static struct shared_info smd_info = {
60 .state_apps = (unsigned) &dummy_state_apps,
61 .state_modem = (unsigned) &dummy_state_modem,
64 module_param_named(debug_mask, msm_smd_debug_mask,
65 int, S_IRUGO | S_IWUSR | S_IWGRP);
67 void *smem_find(unsigned id, unsigned size);
68 static void *smem_item(unsigned id, unsigned *size);
69 static void smd_diag(void);
71 static unsigned last_heap_free = 0xffffffff;
73 #define MSM_A2M_INT(n) (MSM_CSR_BASE + 0x400 + (n) * 4)
75 static inline void notify_other_smsm(void)
77 writel(1, MSM_A2M_INT(5));
80 static inline void notify_modem_smd(void)
82 writel(1, MSM_A2M_INT(0));
85 static inline void notify_dsp_smd(void)
87 writel(1, MSM_A2M_INT(8));
90 static void smd_diag(void)
94 x = smem_find(ID_DIAG_ERR_MSG, SZ_DIAG_ERR_MSG);
96 x[SZ_DIAG_ERR_MSG - 1] = 0;
97 pr_info("smem: DIAG '%s'\n", x);
101 /* call when SMSM_RESET flag is set in the A9's smsm_state */
102 static void handle_modem_crash(void)
104 pr_err("ARM9 has CRASHED\n");
107 /* hard reboot if possible */
108 if (msm_hw_reset_hook)
111 /* in this case the modem or watchdog should reboot us */
116 extern int (*msm_check_for_modem_crash)(void);
118 static int check_for_modem_crash(void)
120 if (readl(smd_info.state_modem) & SMSM_RESET) {
121 handle_modem_crash();
127 #define SMD_SS_CLOSED 0x00000000
128 #define SMD_SS_OPENING 0x00000001
129 #define SMD_SS_OPENED 0x00000002
130 #define SMD_SS_FLUSHING 0x00000003
131 #define SMD_SS_CLOSING 0x00000004
132 #define SMD_SS_RESET 0x00000005
133 #define SMD_SS_RESET_OPENING 0x00000006
135 #define SMD_BUF_SIZE 8192
136 #define SMD_CHANNELS 64
138 #define SMD_HEADER_SIZE 20
141 /* the spinlock is used to synchronize between the
142 ** irq handler and code that mutates the channel
143 ** list or fiddles with channel state
145 static DEFINE_SPINLOCK(smd_lock);
146 static DEFINE_SPINLOCK(smem_lock);
148 /* the mutex is used during open() and close()
149 ** operations to avoid races while creating or
150 ** destroying smd_channel structures
152 static DEFINE_MUTEX(smd_creation_mutex);
154 static int smd_initialized;
156 struct smd_alloc_elm {
163 struct smd_half_channel {
171 unsigned char fSTATE;
172 unsigned char fUNUSED;
175 } __attribute__((packed));
177 struct smd_shared_v1 {
178 struct smd_half_channel ch0;
179 unsigned char data0[SMD_BUF_SIZE];
180 struct smd_half_channel ch1;
181 unsigned char data1[SMD_BUF_SIZE];
184 struct smd_shared_v2 {
185 struct smd_half_channel ch0;
186 struct smd_half_channel ch1;
190 volatile struct smd_half_channel *send;
191 volatile struct smd_half_channel *recv;
192 unsigned char *send_data;
193 unsigned char *recv_data;
197 unsigned current_packet;
200 struct list_head ch_list;
203 void (*notify)(void *priv, unsigned flags);
205 int (*read)(smd_channel_t *ch, void *data, int len);
206 int (*write)(smd_channel_t *ch, const void *data, int len);
207 int (*read_avail)(smd_channel_t *ch);
208 int (*write_avail)(smd_channel_t *ch);
210 void (*update_state)(smd_channel_t *ch);
212 void (*notify_other_cpu)(void);
216 struct platform_device pdev;
219 static LIST_HEAD(smd_ch_closed_list);
220 static LIST_HEAD(smd_ch_list); /* todo: per-target lists */
222 static unsigned char smd_ch_allocated[64];
223 static struct work_struct probe_work;
225 #define SMD_TYPE_MASK 0x0FF
226 #define SMD_TYPE_APPS_MODEM 0x000
227 #define SMD_TYPE_APPS_DSP 0x001
228 #define SMD_TYPE_MODEM_DSP 0x002
230 #define SMD_KIND_MASK 0xF00
231 #define SMD_KIND_UNKNOWN 0x000
232 #define SMD_KIND_STREAM 0x100
233 #define SMD_KIND_PACKET 0x200
235 static void smd_alloc_channel(const char *name, uint32_t cid, uint32_t type);
237 static void smd_channel_probe_worker(struct work_struct *work)
239 struct smd_alloc_elm *shared;
243 shared = smem_find(ID_CH_ALLOC_TBL, sizeof(*shared) * 64);
245 pr_err("smd: cannot find allocation table\n");
248 for (n = 0; n < 64; n++) {
249 if (smd_ch_allocated[n])
251 if (!shared[n].ref_count)
253 if (!shared[n].name[0])
255 type = shared[n].ctype & SMD_TYPE_MASK;
256 if ((type == SMD_TYPE_APPS_MODEM) ||
257 (type == SMD_TYPE_APPS_DSP))
258 smd_alloc_channel(shared[n].name,
261 smd_ch_allocated[n] = 1;
265 static char *chstate(unsigned n)
274 case SMD_SS_FLUSHING:
280 case SMD_SS_RESET_OPENING:
287 /* how many bytes are available for reading */
288 static int smd_stream_read_avail(struct smd_channel *ch)
290 return (ch->recv->head - ch->recv->tail) & ch->fifo_mask;
293 /* how many bytes we are free to write */
294 static int smd_stream_write_avail(struct smd_channel *ch)
296 return ch->fifo_mask -
297 ((ch->send->head - ch->send->tail) & ch->fifo_mask);
300 static int smd_packet_read_avail(struct smd_channel *ch)
302 if (ch->current_packet) {
303 int n = smd_stream_read_avail(ch);
304 if (n > ch->current_packet)
305 n = ch->current_packet;
312 static int smd_packet_write_avail(struct smd_channel *ch)
314 int n = smd_stream_write_avail(ch);
315 return n > SMD_HEADER_SIZE ? n - SMD_HEADER_SIZE : 0;
318 static int ch_is_open(struct smd_channel *ch)
320 return (ch->recv->state == SMD_SS_OPENED) &&
321 (ch->send->state == SMD_SS_OPENED);
324 /* provide a pointer and length to readable data in the fifo */
325 static unsigned ch_read_buffer(struct smd_channel *ch, void **ptr)
327 unsigned head = ch->recv->head;
328 unsigned tail = ch->recv->tail;
329 *ptr = (void *) (ch->recv_data + tail);
334 return ch->fifo_size - tail;
337 /* advance the fifo read pointer after data from ch_read_buffer is consumed */
338 static void ch_read_done(struct smd_channel *ch, unsigned count)
340 BUG_ON(count > smd_stream_read_avail(ch));
341 ch->recv->tail = (ch->recv->tail + count) & ch->fifo_mask;
345 /* basic read interface to ch_read_{buffer,done} used
346 ** by smd_*_read() and update_packet_state()
347 ** will read-and-discard if the _data pointer is null
349 static int ch_read(struct smd_channel *ch, void *_data, int len)
353 unsigned char *data = _data;
357 n = ch_read_buffer(ch, &ptr);
364 memcpy(data, ptr, n);
371 return orig_len - len;
374 static void update_stream_state(struct smd_channel *ch)
376 /* streams have no special state requiring updating */
379 static void update_packet_state(struct smd_channel *ch)
384 /* can't do anything if we're in the middle of a packet */
385 if (ch->current_packet != 0)
388 /* don't bother unless we can get the full header */
389 if (smd_stream_read_avail(ch) < SMD_HEADER_SIZE)
392 r = ch_read(ch, hdr, SMD_HEADER_SIZE);
393 BUG_ON(r != SMD_HEADER_SIZE);
395 ch->current_packet = hdr[0];
398 /* provide a pointer and length to next free space in the fifo */
399 static unsigned ch_write_buffer(struct smd_channel *ch, void **ptr)
401 unsigned head = ch->send->head;
402 unsigned tail = ch->send->tail;
403 *ptr = (void *) (ch->send_data + head);
406 return tail - head - 1;
409 return ch->fifo_size - head - 1;
411 return ch->fifo_size - head;
415 /* advace the fifo write pointer after freespace
416 * from ch_write_buffer is filled
418 static void ch_write_done(struct smd_channel *ch, unsigned count)
420 BUG_ON(count > smd_stream_write_avail(ch));
421 ch->send->head = (ch->send->head + count) & ch->fifo_mask;
425 static void ch_set_state(struct smd_channel *ch, unsigned n)
427 if (n == SMD_SS_OPENED) {
437 ch->send->fSTATE = 1;
438 ch->notify_other_cpu();
441 static void do_smd_probe(void)
443 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
444 if (shared->heap_info.free_offset != last_heap_free) {
445 last_heap_free = shared->heap_info.free_offset;
446 schedule_work(&probe_work);
450 static void smd_state_change(struct smd_channel *ch,
451 unsigned last, unsigned next)
453 ch->last_state = next;
455 pr_info("SMD: ch %d %s -> %s\n", ch->n,
456 chstate(last), chstate(next));
462 if (ch->send->state != SMD_SS_OPENED)
463 ch_set_state(ch, SMD_SS_OPENED);
464 ch->notify(ch->priv, SMD_EVENT_OPEN);
466 case SMD_SS_FLUSHING:
468 /* we should force them to close? */
470 ch->notify(ch->priv, SMD_EVENT_CLOSE);
474 static void handle_smd_irq(struct list_head *list, void (*notify)(void))
477 struct smd_channel *ch;
482 spin_lock_irqsave(&smd_lock, flags);
483 list_for_each_entry(ch, list, ch_list) {
485 if (ch_is_open(ch)) {
486 if (ch->recv->fHEAD) {
491 if (ch->recv->fTAIL) {
496 if (ch->recv->fSTATE) {
497 ch->recv->fSTATE = 0;
502 tmp = ch->recv->state;
503 if (tmp != ch->last_state)
504 smd_state_change(ch, ch->last_state, tmp);
506 ch->update_state(ch);
507 ch->notify(ch->priv, SMD_EVENT_DATA);
512 spin_unlock_irqrestore(&smd_lock, flags);
516 static irqreturn_t smd_irq_handler(int irq, void *data)
518 handle_smd_irq(&smd_ch_list, notify_modem_smd);
522 static void smd_fake_irq_handler(unsigned long arg)
524 smd_irq_handler(0, NULL);
527 static DECLARE_TASKLET(smd_fake_irq_tasklet, smd_fake_irq_handler, 0);
529 void smd_sleep_exit(void)
532 struct smd_channel *ch;
536 spin_lock_irqsave(&smd_lock, flags);
537 list_for_each_entry(ch, &smd_ch_list, ch_list) {
538 if (ch_is_open(ch)) {
539 if (ch->recv->fHEAD) {
540 if (msm_smd_debug_mask & MSM_SMD_DEBUG)
541 pr_info("smd_sleep_exit ch %d fHEAD "
543 ch->n, ch->recv->fHEAD,
544 ch->recv->head, ch->recv->tail);
548 if (ch->recv->fTAIL) {
549 if (msm_smd_debug_mask & MSM_SMD_DEBUG)
550 pr_info("smd_sleep_exit ch %d fTAIL "
552 ch->n, ch->recv->fTAIL,
553 ch->send->head, ch->send->tail);
557 if (ch->recv->fSTATE) {
558 if (msm_smd_debug_mask & MSM_SMD_DEBUG)
559 pr_info("smd_sleep_exit ch %d fSTATE %x"
560 "\n", ch->n, ch->recv->fSTATE);
564 tmp = ch->recv->state;
565 if (tmp != ch->last_state) {
566 if (msm_smd_debug_mask & MSM_SMD_DEBUG)
567 pr_info("smd_sleep_exit ch %d "
569 ch->n, tmp, ch->last_state);
575 spin_unlock_irqrestore(&smd_lock, flags);
578 if (msm_smd_debug_mask & MSM_SMD_DEBUG)
579 pr_info("smd_sleep_exit need interrupt\n");
580 tasklet_schedule(&smd_fake_irq_tasklet);
585 void smd_kick(smd_channel_t *ch)
590 spin_lock_irqsave(&smd_lock, flags);
591 ch->update_state(ch);
592 tmp = ch->recv->state;
593 if (tmp != ch->last_state) {
594 ch->last_state = tmp;
595 if (tmp == SMD_SS_OPENED)
596 ch->notify(ch->priv, SMD_EVENT_OPEN);
598 ch->notify(ch->priv, SMD_EVENT_CLOSE);
600 ch->notify(ch->priv, SMD_EVENT_DATA);
601 ch->notify_other_cpu();
602 spin_unlock_irqrestore(&smd_lock, flags);
605 static int smd_is_packet(int chn, unsigned type)
607 type &= SMD_KIND_MASK;
608 if (type == SMD_KIND_PACKET)
610 if (type == SMD_KIND_STREAM)
613 /* older AMSS reports SMD_KIND_UNKNOWN always */
614 if ((chn > 4) || (chn == 1))
620 static int smd_stream_write(smd_channel_t *ch, const void *_data, int len)
623 const unsigned char *buf = _data;
630 while ((xfer = ch_write_buffer(ch, &ptr)) != 0) {
635 memcpy(ptr, buf, xfer);
636 ch_write_done(ch, xfer);
643 ch->notify_other_cpu();
645 return orig_len - len;
648 static int smd_packet_write(smd_channel_t *ch, const void *_data, int len)
655 if (smd_stream_write_avail(ch) < (len + SMD_HEADER_SIZE))
659 hdr[1] = hdr[2] = hdr[3] = hdr[4] = 0;
661 smd_stream_write(ch, hdr, sizeof(hdr));
662 smd_stream_write(ch, _data, len);
667 static int smd_stream_read(smd_channel_t *ch, void *data, int len)
674 r = ch_read(ch, data, len);
676 ch->notify_other_cpu();
681 static int smd_packet_read(smd_channel_t *ch, void *data, int len)
689 if (len > ch->current_packet)
690 len = ch->current_packet;
692 r = ch_read(ch, data, len);
694 ch->notify_other_cpu();
696 spin_lock_irqsave(&smd_lock, flags);
697 ch->current_packet -= r;
698 update_packet_state(ch);
699 spin_unlock_irqrestore(&smd_lock, flags);
704 static int smd_alloc_v2(struct smd_channel *ch)
706 struct smd_shared_v2 *shared2;
710 shared2 = smem_alloc(SMEM_SMD_BASE_ID + ch->n, sizeof(*shared2));
711 buffer = smem_item(SMEM_SMD_FIFO_BASE_ID + ch->n, &buffer_sz);
716 /* buffer must be a power-of-two size */
717 if (buffer_sz & (buffer_sz - 1))
721 ch->send = &shared2->ch0;
722 ch->recv = &shared2->ch1;
723 ch->send_data = buffer;
724 ch->recv_data = buffer + buffer_sz;
725 ch->fifo_size = buffer_sz;
729 static int smd_alloc_v1(struct smd_channel *ch)
731 struct smd_shared_v1 *shared1;
732 shared1 = smem_alloc(ID_SMD_CHANNELS + ch->n, sizeof(*shared1));
734 pr_err("smd_alloc_channel() cid %d does not exist\n", ch->n);
737 ch->send = &shared1->ch0;
738 ch->recv = &shared1->ch1;
739 ch->send_data = shared1->data0;
740 ch->recv_data = shared1->data1;
741 ch->fifo_size = SMD_BUF_SIZE;
746 static void smd_alloc_channel(const char *name, uint32_t cid, uint32_t type)
748 struct smd_channel *ch;
750 ch = kzalloc(sizeof(struct smd_channel), GFP_KERNEL);
752 pr_err("smd_alloc_channel() out of memory\n");
757 if (smd_alloc_v2(ch) && smd_alloc_v1(ch)) {
762 ch->fifo_mask = ch->fifo_size - 1;
765 if ((type & SMD_TYPE_MASK) == SMD_TYPE_APPS_MODEM)
766 ch->notify_other_cpu = notify_modem_smd;
768 ch->notify_other_cpu = notify_dsp_smd;
770 if (smd_is_packet(cid, type)) {
771 ch->read = smd_packet_read;
772 ch->write = smd_packet_write;
773 ch->read_avail = smd_packet_read_avail;
774 ch->write_avail = smd_packet_write_avail;
775 ch->update_state = update_packet_state;
777 ch->read = smd_stream_read;
778 ch->write = smd_stream_write;
779 ch->read_avail = smd_stream_read_avail;
780 ch->write_avail = smd_stream_write_avail;
781 ch->update_state = update_stream_state;
784 if ((type & 0xff) == 0)
785 memcpy(ch->name, "SMD_", 4);
787 memcpy(ch->name, "DSP_", 4);
788 memcpy(ch->name + 4, name, 20);
790 ch->pdev.name = ch->name;
793 pr_info("smd_alloc_channel() cid=%02d size=%05d '%s'\n",
794 ch->n, ch->fifo_size, ch->name);
796 mutex_lock(&smd_creation_mutex);
797 list_add(&ch->ch_list, &smd_ch_closed_list);
798 mutex_unlock(&smd_creation_mutex);
800 platform_device_register(&ch->pdev);
803 static void do_nothing_notify(void *priv, unsigned flags)
807 struct smd_channel *smd_get_channel(const char *name)
809 struct smd_channel *ch;
811 mutex_lock(&smd_creation_mutex);
812 list_for_each_entry(ch, &smd_ch_closed_list, ch_list) {
813 if (!strcmp(name, ch->name)) {
814 list_del(&ch->ch_list);
815 mutex_unlock(&smd_creation_mutex);
819 mutex_unlock(&smd_creation_mutex);
824 int smd_open(const char *name, smd_channel_t **_ch,
825 void *priv, void (*notify)(void *, unsigned))
827 struct smd_channel *ch;
830 if (smd_initialized == 0) {
831 pr_info("smd_open() before smd_init()\n");
835 ch = smd_get_channel(name);
840 notify = do_nothing_notify;
843 ch->current_packet = 0;
844 ch->last_state = SMD_SS_CLOSED;
849 spin_lock_irqsave(&smd_lock, flags);
850 list_add(&ch->ch_list, &smd_ch_list);
852 /* If the remote side is CLOSING, we need to get it to
853 * move to OPENING (which we'll do by moving from CLOSED to
854 * OPENING) and then get it to move from OPENING to
855 * OPENED (by doing the same state change ourselves).
857 * Otherwise, it should be OPENING and we can move directly
858 * to OPENED so that it will follow.
860 if (ch->recv->state == SMD_SS_CLOSING) {
862 ch_set_state(ch, SMD_SS_OPENING);
864 ch_set_state(ch, SMD_SS_OPENED);
866 spin_unlock_irqrestore(&smd_lock, flags);
872 int smd_close(smd_channel_t *ch)
876 pr_info("smd_close(%p)\n", ch);
881 spin_lock_irqsave(&smd_lock, flags);
882 ch->notify = do_nothing_notify;
883 list_del(&ch->ch_list);
884 ch_set_state(ch, SMD_SS_CLOSED);
885 spin_unlock_irqrestore(&smd_lock, flags);
887 mutex_lock(&smd_creation_mutex);
888 list_add(&ch->ch_list, &smd_ch_closed_list);
889 mutex_unlock(&smd_creation_mutex);
894 int smd_read(smd_channel_t *ch, void *data, int len)
896 return ch->read(ch, data, len);
899 int smd_write(smd_channel_t *ch, const void *data, int len)
901 return ch->write(ch, data, len);
904 int smd_read_avail(smd_channel_t *ch)
906 return ch->read_avail(ch);
909 int smd_write_avail(smd_channel_t *ch)
911 return ch->write_avail(ch);
914 int smd_wait_until_readable(smd_channel_t *ch, int bytes)
919 int smd_wait_until_writable(smd_channel_t *ch, int bytes)
924 int smd_cur_packet_size(smd_channel_t *ch)
926 return ch->current_packet;
930 /* ------------------------------------------------------------------------- */
932 void *smem_alloc(unsigned id, unsigned size)
934 return smem_find(id, size);
937 static void *smem_item(unsigned id, unsigned *size)
939 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
940 struct smem_heap_entry *toc = shared->heap_toc;
942 if (id >= SMEM_NUM_ITEMS)
945 if (toc[id].allocated) {
946 *size = toc[id].size;
947 return (void *) (MSM_SHARED_RAM_BASE + toc[id].offset);
955 void *smem_find(unsigned id, unsigned size_in)
960 ptr = smem_item(id, &size);
964 size_in = ALIGN(size_in, 8);
965 if (size_in != size) {
966 pr_err("smem_find(%d, %d): wrong size %d\n",
974 static irqreturn_t smsm_irq_handler(int irq, void *data)
979 spin_lock_irqsave(&smem_lock, flags);
981 apps = readl(smd_info.state_apps);
982 modm = readl(smd_info.state_modem);
984 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
985 pr_info("<SM %08x %08x>\n", apps, modm);
986 if (modm & SMSM_RESET) {
987 handle_modem_crash();
991 spin_unlock_irqrestore(&smem_lock, flags);
995 int smsm_change_state(uint32_t clear_mask, uint32_t set_mask)
1000 if (!smd_info.ready)
1003 spin_lock_irqsave(&smem_lock, flags);
1005 if (readl(smd_info.state_modem) & SMSM_RESET)
1006 handle_modem_crash();
1008 state = (readl(smd_info.state_apps) & ~clear_mask) | set_mask;
1009 writel(state, smd_info.state_apps);
1011 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
1012 pr_info("smsm_change_state %x\n", state);
1013 notify_other_smsm();
1015 spin_unlock_irqrestore(&smem_lock, flags);
1020 uint32_t smsm_get_state(void)
1022 unsigned long flags;
1025 spin_lock_irqsave(&smem_lock, flags);
1027 rv = readl(smd_info.state_modem);
1029 if (rv & SMSM_RESET)
1030 handle_modem_crash();
1032 spin_unlock_irqrestore(&smem_lock, flags);
1037 int smsm_set_sleep_duration(uint32_t delay)
1041 ptr = smem_alloc(SMEM_SMSM_SLEEP_DELAY, sizeof(*ptr));
1043 pr_err("smsm_set_sleep_duration <SM NO SLEEP_DELAY>\n");
1046 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
1047 pr_info("smsm_set_sleep_duration %d -> %d\n",
1053 int smsm_set_interrupt_info(struct smsm_interrupt_info *info)
1055 struct smsm_interrupt_info *ptr;
1057 ptr = smem_alloc(SMEM_SMSM_INT_INFO, sizeof(*ptr));
1059 pr_err("smsm_set_sleep_duration <SM NO INT_INFO>\n");
1062 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
1063 pr_info("smsm_set_interrupt_info %x %x -> %x %x\n",
1064 ptr->aArm_en_mask, ptr->aArm_interrupts_pending,
1065 info->aArm_en_mask, info->aArm_interrupts_pending);
1070 #define MAX_NUM_SLEEP_CLIENTS 64
1071 #define MAX_SLEEP_NAME_LEN 8
1073 #define NUM_GPIO_INT_REGISTERS 6
1074 #define GPIO_SMEM_NUM_GROUPS 2
1075 #define GPIO_SMEM_MAX_PC_INTERRUPTS 8
1077 struct tramp_gpio_save {
1078 unsigned int enable;
1079 unsigned int detect;
1080 unsigned int polarity;
1083 struct tramp_gpio_smem {
1084 uint16_t num_fired[GPIO_SMEM_NUM_GROUPS];
1085 uint16_t fired[GPIO_SMEM_NUM_GROUPS][GPIO_SMEM_MAX_PC_INTERRUPTS];
1086 uint32_t enabled[NUM_GPIO_INT_REGISTERS];
1087 uint32_t detection[NUM_GPIO_INT_REGISTERS];
1088 uint32_t polarity[NUM_GPIO_INT_REGISTERS];
1092 void smsm_print_sleep_info(void)
1094 unsigned long flags;
1096 struct tramp_gpio_smem *gpio;
1097 struct smsm_interrupt_info *int_info;
1100 spin_lock_irqsave(&smem_lock, flags);
1102 ptr = smem_alloc(SMEM_SMSM_SLEEP_DELAY, sizeof(*ptr));
1104 pr_info("SMEM_SMSM_SLEEP_DELAY: %x\n", *ptr);
1106 ptr = smem_alloc(SMEM_SMSM_LIMIT_SLEEP, sizeof(*ptr));
1108 pr_info("SMEM_SMSM_LIMIT_SLEEP: %x\n", *ptr);
1110 ptr = smem_alloc(SMEM_SLEEP_POWER_COLLAPSE_DISABLED, sizeof(*ptr));
1112 pr_info("SMEM_SLEEP_POWER_COLLAPSE_DISABLED: %x\n", *ptr);
1114 int_info = smem_alloc(SMEM_SMSM_INT_INFO, sizeof(*int_info));
1116 pr_info("SMEM_SMSM_INT_INFO %x %x %x\n",
1117 int_info->aArm_en_mask,
1118 int_info->aArm_interrupts_pending,
1119 int_info->aArm_wakeup_reason);
1121 gpio = smem_alloc(SMEM_GPIO_INT, sizeof(*gpio));
1124 for (i = 0; i < NUM_GPIO_INT_REGISTERS; i++)
1125 pr_info("SMEM_GPIO_INT: %d: e %x d %x p %x\n",
1126 i, gpio->enabled[i], gpio->detection[i],
1129 for (i = 0; i < GPIO_SMEM_NUM_GROUPS; i++)
1130 pr_info("SMEM_GPIO_INT: %d: f %d: %d %d...\n",
1131 i, gpio->num_fired[i], gpio->fired[i][0],
1135 spin_unlock_irqrestore(&smem_lock, flags);
1138 int smd_core_init(void)
1141 pr_info("smd_core_init()\n");
1143 /* wait for essential items to be initialized */
1147 state = smem_item(SMEM_SMSM_SHARED_STATE, &size);
1148 if (size == SMSM_V1_SIZE) {
1149 smd_info.state_apps = state + SMSM_V1_STATE_APPS;
1150 smd_info.state_modem = state + SMSM_V1_STATE_MODEM;
1153 if (size == SMSM_V2_SIZE) {
1154 smd_info.state_apps = state + SMSM_V2_STATE_APPS;
1155 smd_info.state_modem = state + SMSM_V2_STATE_MODEM;
1162 r = request_irq(INT_A9_M2A_0, smd_irq_handler,
1163 IRQF_TRIGGER_RISING, "smd_dev", 0);
1166 r = enable_irq_wake(INT_A9_M2A_0);
1168 pr_err("smd_core_init: enable_irq_wake failed for A9_M2A_0\n");
1170 r = request_irq(INT_A9_M2A_5, smsm_irq_handler,
1171 IRQF_TRIGGER_RISING, "smsm_dev", 0);
1173 free_irq(INT_A9_M2A_0, 0);
1176 r = enable_irq_wake(INT_A9_M2A_5);
1178 pr_err("smd_core_init: enable_irq_wake failed for A9_M2A_5\n");
1180 /* check for any SMD channels that may already exist */
1183 /* indicate that we're up and running */
1184 writel(SMSM_INIT | SMSM_SMDINIT | SMSM_RPCINIT, smd_info.state_apps);
1185 notify_other_smsm();
1187 pr_info("smd_core_init() done\n");
1192 #if defined(CONFIG_DEBUG_FS)
1194 static int dump_ch(char *buf, int max, struct smd_channel *ch)
1196 volatile struct smd_half_channel *s = ch->send;
1197 volatile struct smd_half_channel *r = ch->recv;
1202 " %8s(%05d/%05d) %c%c%c%c%c%c%c <->"
1203 " %8s(%05d/%05d) %c%c%c%c%c%c%c\n", ch->n,
1204 chstate(s->state), s->tail, s->head,
1205 s->fDSR ? 'D' : 'd',
1206 s->fCTS ? 'C' : 'c',
1209 s->fHEAD ? 'W' : 'w',
1210 s->fTAIL ? 'R' : 'r',
1211 s->fSTATE ? 'S' : 's',
1212 chstate(r->state), r->tail, r->head,
1213 r->fDSR ? 'D' : 'd',
1214 r->fCTS ? 'R' : 'r',
1217 r->fHEAD ? 'W' : 'w',
1218 r->fTAIL ? 'R' : 'r',
1219 r->fSTATE ? 'S' : 's'
1223 static int debug_read_stat(char *buf, int max)
1228 msg = smem_find(ID_DIAG_ERR_MSG, SZ_DIAG_ERR_MSG);
1230 if (readl(smd_info.state_modem) & SMSM_RESET)
1231 i += scnprintf(buf + i, max - i,
1232 "smsm: ARM9 HAS CRASHED\n");
1234 i += scnprintf(buf + i, max - i, "smsm: a9: %08x a11: %08x\n",
1235 readl(smd_info.state_modem),
1236 readl(smd_info.state_apps));
1239 msg[SZ_DIAG_ERR_MSG - 1] = 0;
1240 i += scnprintf(buf + i, max - i, "diag: '%s'\n", msg);
1245 static int debug_read_mem(char *buf, int max)
1248 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
1249 struct smem_heap_entry *toc = shared->heap_toc;
1252 i += scnprintf(buf + i, max - i,
1253 "heap: init=%d free=%d remain=%d\n",
1254 shared->heap_info.initialized,
1255 shared->heap_info.free_offset,
1256 shared->heap_info.heap_remaining);
1258 for (n = 0; n < SMEM_NUM_ITEMS; n++) {
1259 if (toc[n].allocated == 0)
1261 i += scnprintf(buf + i, max - i,
1262 "%04d: offset %08x size %08x\n",
1263 n, toc[n].offset, toc[n].size);
1268 static int debug_read_ch(char *buf, int max)
1270 struct smd_channel *ch;
1271 unsigned long flags;
1274 spin_lock_irqsave(&smd_lock, flags);
1275 list_for_each_entry(ch, &smd_ch_list, ch_list)
1276 i += dump_ch(buf + i, max - i, ch);
1277 list_for_each_entry(ch, &smd_ch_closed_list, ch_list)
1278 i += dump_ch(buf + i, max - i, ch);
1279 spin_unlock_irqrestore(&smd_lock, flags);
1284 static int debug_read_version(char *buf, int max)
1286 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
1287 unsigned version = shared->version[VERSION_MODEM];
1288 return sprintf(buf, "%d.%d\n", version >> 16, version & 0xffff);
1291 static int debug_read_build_id(char *buf, int max)
1296 data = smem_item(SMEM_HW_SW_BUILD_ID, &size);
1302 memcpy(buf, data, size);
1307 static int debug_read_alloc_tbl(char *buf, int max)
1309 struct smd_alloc_elm *shared;
1312 shared = smem_find(ID_CH_ALLOC_TBL, sizeof(*shared) * 64);
1314 for (n = 0; n < 64; n++) {
1315 if (shared[n].ref_count == 0)
1317 i += scnprintf(buf + i, max - i,
1318 "%03d: %-20s cid=%02d type=%03d "
1319 "kind=%02d ref_count=%d\n",
1320 n, shared[n].name, shared[n].cid,
1321 shared[n].ctype & 0xff,
1322 (shared[n].ctype >> 8) & 0xf,
1323 shared[n].ref_count);
1329 static int debug_boom(char *buf, int max)
1332 msm_proc_comm(PCOM_RESET_MODEM, &ms, 0);
1336 #define DEBUG_BUFMAX 4096
1337 static char debug_buffer[DEBUG_BUFMAX];
1339 static ssize_t debug_read(struct file *file, char __user *buf,
1340 size_t count, loff_t *ppos)
1342 int (*fill)(char *buf, int max) = file->private_data;
1343 int bsize = fill(debug_buffer, DEBUG_BUFMAX);
1344 return simple_read_from_buffer(buf, count, ppos, debug_buffer, bsize);
1347 static int debug_open(struct inode *inode, struct file *file)
1349 file->private_data = inode->i_private;
1353 static const struct file_operations debug_ops = {
1358 static void debug_create(const char *name, mode_t mode,
1359 struct dentry *dent,
1360 int (*fill)(char *buf, int max))
1362 debugfs_create_file(name, mode, dent, fill, &debug_ops);
1365 static void smd_debugfs_init(void)
1367 struct dentry *dent;
1369 dent = debugfs_create_dir("smd", 0);
1373 debug_create("ch", 0444, dent, debug_read_ch);
1374 debug_create("stat", 0444, dent, debug_read_stat);
1375 debug_create("mem", 0444, dent, debug_read_mem);
1376 debug_create("version", 0444, dent, debug_read_version);
1377 debug_create("tbl", 0444, dent, debug_read_alloc_tbl);
1378 debug_create("build", 0444, dent, debug_read_build_id);
1379 debug_create("boom", 0444, dent, debug_boom);
1382 static void smd_debugfs_init(void) {}
1385 static int __init msm_smd_probe(struct platform_device *pdev)
1387 pr_info("smd_init()\n");
1389 INIT_WORK(&probe_work, smd_channel_probe_worker);
1391 if (smd_core_init()) {
1392 pr_err("smd_core_init() failed\n");
1398 msm_check_for_modem_crash = check_for_modem_crash;
1401 smd_initialized = 1;
1406 static struct platform_driver msm_smd_driver = {
1407 .probe = msm_smd_probe,
1409 .name = MODULE_NAME,
1410 .owner = THIS_MODULE,
1414 static int __init msm_smd_init(void)
1416 return platform_driver_register(&msm_smd_driver);
1419 module_init(msm_smd_init);
1421 MODULE_DESCRIPTION("MSM Shared Memory Core");
1422 MODULE_AUTHOR("Brian Swetland <swetland@google.com>");
1423 MODULE_LICENSE("GPL");