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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/platform_device.h>
20 #include <linux/module.h>
22 #include <linux/cdev.h>
23 #include <linux/device.h>
24 #include <linux/wait.h>
25 #include <linux/interrupt.h>
26 #include <linux/irq.h>
27 #include <linux/list.h>
28 #include <linux/slab.h>
29 #include <linux/debugfs.h>
30 #include <linux/delay.h>
32 #include <mach/msm_smd.h>
34 #include "smd_private.h"
35 #include "proc_comm.h"
37 #if defined(CONFIG_ARCH_QSD8X50)
38 #define CONFIG_QDSP6 1
41 #define MODULE_NAME "msm_smd"
44 MSM_SMD_DEBUG = 1U << 0,
45 MSM_SMSM_DEBUG = 1U << 0,
48 static int msm_smd_debug_mask;
55 static unsigned dummy_state[SMSM_STATE_COUNT];
57 static struct shared_info smd_info = {
58 .state = (unsigned) &dummy_state,
61 module_param_named(debug_mask, msm_smd_debug_mask,
62 int, S_IRUGO | S_IWUSR | S_IWGRP);
64 static unsigned last_heap_free = 0xffffffff;
66 static inline void notify_other_smsm(void)
74 static inline void notify_modem_smd(void)
79 static inline void notify_dsp_smd(void)
84 static void smd_diag(void)
88 x = smem_find(ID_DIAG_ERR_MSG, SZ_DIAG_ERR_MSG);
90 x[SZ_DIAG_ERR_MSG - 1] = 0;
91 pr_debug("DIAG '%s'\n", x);
95 /* call when SMSM_RESET flag is set in the A9's smsm_state */
96 static void handle_modem_crash(void)
98 pr_err("ARM9 has CRASHED\n");
101 /* in this case the modem or watchdog should reboot us */
106 uint32_t raw_smsm_get_state(enum smsm_state_item item)
108 return readl(smd_info.state + item * 4);
111 static int check_for_modem_crash(void)
113 if (raw_smsm_get_state(SMSM_STATE_MODEM) & SMSM_RESET) {
114 handle_modem_crash();
120 /* the spinlock is used to synchronize between the
121 * irq handler and code that mutates the channel
122 * list or fiddles with channel state
124 DEFINE_SPINLOCK(smd_lock);
125 DEFINE_SPINLOCK(smem_lock);
127 /* the mutex is used during open() and close()
128 * operations to avoid races while creating or
129 * destroying smd_channel structures
131 static DEFINE_MUTEX(smd_creation_mutex);
133 static int smd_initialized;
135 LIST_HEAD(smd_ch_closed_list);
136 LIST_HEAD(smd_ch_list_modem);
137 LIST_HEAD(smd_ch_list_dsp);
139 static unsigned char smd_ch_allocated[64];
140 static struct work_struct probe_work;
142 /* how many bytes are available for reading */
143 static int smd_stream_read_avail(struct smd_channel *ch)
145 return (ch->recv->head - ch->recv->tail) & ch->fifo_mask;
148 /* how many bytes we are free to write */
149 static int smd_stream_write_avail(struct smd_channel *ch)
151 return ch->fifo_mask -
152 ((ch->send->head - ch->send->tail) & ch->fifo_mask);
155 static int smd_packet_read_avail(struct smd_channel *ch)
157 if (ch->current_packet) {
158 int n = smd_stream_read_avail(ch);
159 if (n > ch->current_packet)
160 n = ch->current_packet;
167 static int smd_packet_write_avail(struct smd_channel *ch)
169 int n = smd_stream_write_avail(ch);
170 return n > SMD_HEADER_SIZE ? n - SMD_HEADER_SIZE : 0;
173 static int ch_is_open(struct smd_channel *ch)
175 return (ch->recv->state == SMD_SS_OPENED) &&
176 (ch->send->state == SMD_SS_OPENED);
179 /* provide a pointer and length to readable data in the fifo */
180 static unsigned ch_read_buffer(struct smd_channel *ch, void **ptr)
182 unsigned head = ch->recv->head;
183 unsigned tail = ch->recv->tail;
184 *ptr = (void *) (ch->recv_data + tail);
189 return ch->fifo_size - tail;
192 /* advance the fifo read pointer after data from ch_read_buffer is consumed */
193 static void ch_read_done(struct smd_channel *ch, unsigned count)
195 BUG_ON(count > smd_stream_read_avail(ch));
196 ch->recv->tail = (ch->recv->tail + count) & ch->fifo_mask;
200 /* basic read interface to ch_read_{buffer,done} used
201 * by smd_*_read() and update_packet_state()
202 * will read-and-discard if the _data pointer is null
204 static int ch_read(struct smd_channel *ch, void *_data, int len)
208 unsigned char *data = _data;
212 n = ch_read_buffer(ch, &ptr);
219 memcpy(data, ptr, n);
226 return orig_len - len;
229 static void update_stream_state(struct smd_channel *ch)
231 /* streams have no special state requiring updating */
234 static void update_packet_state(struct smd_channel *ch)
239 /* can't do anything if we're in the middle of a packet */
240 if (ch->current_packet != 0)
243 /* don't bother unless we can get the full header */
244 if (smd_stream_read_avail(ch) < SMD_HEADER_SIZE)
247 r = ch_read(ch, hdr, SMD_HEADER_SIZE);
248 BUG_ON(r != SMD_HEADER_SIZE);
250 ch->current_packet = hdr[0];
253 /* provide a pointer and length to next free space in the fifo */
254 static unsigned ch_write_buffer(struct smd_channel *ch, void **ptr)
256 unsigned head = ch->send->head;
257 unsigned tail = ch->send->tail;
258 *ptr = (void *) (ch->send_data + head);
261 return tail - head - 1;
264 return ch->fifo_size - head - 1;
266 return ch->fifo_size - head;
270 /* advace the fifo write pointer after freespace
271 * from ch_write_buffer is filled
273 static void ch_write_done(struct smd_channel *ch, unsigned count)
275 BUG_ON(count > smd_stream_write_avail(ch));
276 ch->send->head = (ch->send->head + count) & ch->fifo_mask;
280 static void ch_set_state(struct smd_channel *ch, unsigned n)
282 if (n == SMD_SS_OPENED) {
292 ch->send->fSTATE = 1;
293 ch->notify_other_cpu();
296 static void do_smd_probe(void)
298 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
299 if (shared->heap_info.free_offset != last_heap_free) {
300 last_heap_free = shared->heap_info.free_offset;
301 schedule_work(&probe_work);
305 static void smd_state_change(struct smd_channel *ch,
306 unsigned last, unsigned next)
308 ch->last_state = next;
310 pr_debug("ch %d %d -> %d\n", ch->n, last, next);
316 if (ch->send->state != SMD_SS_OPENED)
317 ch_set_state(ch, SMD_SS_OPENED);
318 ch->notify(ch->priv, SMD_EVENT_OPEN);
320 case SMD_SS_FLUSHING:
322 /* we should force them to close? */
324 ch->notify(ch->priv, SMD_EVENT_CLOSE);
328 static void handle_smd_irq(struct list_head *list, void (*notify)(void))
331 struct smd_channel *ch;
336 spin_lock_irqsave(&smd_lock, flags);
337 list_for_each_entry(ch, list, ch_list) {
339 if (ch_is_open(ch)) {
340 if (ch->recv->fHEAD) {
345 if (ch->recv->fTAIL) {
350 if (ch->recv->fSTATE) {
351 ch->recv->fSTATE = 0;
356 tmp = ch->recv->state;
357 if (tmp != ch->last_state)
358 smd_state_change(ch, ch->last_state, tmp);
360 ch->update_state(ch);
361 ch->notify(ch->priv, SMD_EVENT_DATA);
366 spin_unlock_irqrestore(&smd_lock, flags);
370 static irqreturn_t smd_modem_irq_handler(int irq, void *data)
372 handle_smd_irq(&smd_ch_list_modem, notify_modem_smd);
376 #if defined(CONFIG_QDSP6)
377 static irqreturn_t smd_dsp_irq_handler(int irq, void *data)
379 handle_smd_irq(&smd_ch_list_dsp, notify_dsp_smd);
384 static void smd_fake_irq_handler(unsigned long arg)
386 handle_smd_irq(&smd_ch_list_modem, notify_modem_smd);
387 handle_smd_irq(&smd_ch_list_dsp, notify_dsp_smd);
390 static DECLARE_TASKLET(smd_fake_irq_tasklet, smd_fake_irq_handler, 0);
392 static inline int smd_need_int(struct smd_channel *ch)
394 if (ch_is_open(ch)) {
395 if (ch->recv->fHEAD || ch->recv->fTAIL || ch->recv->fSTATE)
397 if (ch->recv->state != ch->last_state)
403 void smd_sleep_exit(void)
406 struct smd_channel *ch;
409 spin_lock_irqsave(&smd_lock, flags);
410 list_for_each_entry(ch, &smd_ch_list_modem, ch_list) {
411 if (smd_need_int(ch)) {
416 list_for_each_entry(ch, &smd_ch_list_dsp, ch_list) {
417 if (smd_need_int(ch)) {
422 spin_unlock_irqrestore(&smd_lock, flags);
426 if (msm_smd_debug_mask & MSM_SMD_DEBUG)
427 pr_info("smd_sleep_exit need interrupt\n");
428 tasklet_schedule(&smd_fake_irq_tasklet);
433 void smd_kick(smd_channel_t *ch)
438 spin_lock_irqsave(&smd_lock, flags);
439 ch->update_state(ch);
440 tmp = ch->recv->state;
441 if (tmp != ch->last_state) {
442 ch->last_state = tmp;
443 if (tmp == SMD_SS_OPENED)
444 ch->notify(ch->priv, SMD_EVENT_OPEN);
446 ch->notify(ch->priv, SMD_EVENT_CLOSE);
448 ch->notify(ch->priv, SMD_EVENT_DATA);
449 ch->notify_other_cpu();
450 spin_unlock_irqrestore(&smd_lock, flags);
453 static int smd_is_packet(int chn, unsigned type)
455 type &= SMD_KIND_MASK;
456 if (type == SMD_KIND_PACKET)
458 if (type == SMD_KIND_STREAM)
461 /* older AMSS reports SMD_KIND_UNKNOWN always */
462 if ((chn > 4) || (chn == 1))
468 static int smd_stream_write(smd_channel_t *ch, const void *_data, int len)
471 const unsigned char *buf = _data;
478 while ((xfer = ch_write_buffer(ch, &ptr)) != 0) {
483 memcpy(ptr, buf, xfer);
484 ch_write_done(ch, xfer);
491 ch->notify_other_cpu();
493 return orig_len - len;
496 static int smd_packet_write(smd_channel_t *ch, const void *_data, int len)
503 if (smd_stream_write_avail(ch) < (len + SMD_HEADER_SIZE))
507 hdr[1] = hdr[2] = hdr[3] = hdr[4] = 0;
509 smd_stream_write(ch, hdr, sizeof(hdr));
510 smd_stream_write(ch, _data, len);
515 static int smd_stream_read(smd_channel_t *ch, void *data, int len)
522 r = ch_read(ch, data, len);
524 ch->notify_other_cpu();
529 static int smd_packet_read(smd_channel_t *ch, void *data, int len)
537 if (len > ch->current_packet)
538 len = ch->current_packet;
540 r = ch_read(ch, data, len);
542 ch->notify_other_cpu();
544 spin_lock_irqsave(&smd_lock, flags);
545 ch->current_packet -= r;
546 update_packet_state(ch);
547 spin_unlock_irqrestore(&smd_lock, flags);
552 static int smd_alloc_channel(const char *name, uint32_t cid, uint32_t type)
554 struct smd_channel *ch;
556 ch = kzalloc(sizeof(struct smd_channel), GFP_KERNEL);
558 pr_err("smd_alloc_channel() out of memory\n");
563 if (_smd_alloc_channel(ch)) {
568 ch->fifo_mask = ch->fifo_size - 1;
571 if ((type & SMD_TYPE_MASK) == SMD_TYPE_APPS_MODEM)
572 ch->notify_other_cpu = notify_modem_smd;
574 ch->notify_other_cpu = notify_dsp_smd;
576 if (smd_is_packet(cid, type)) {
577 ch->read = smd_packet_read;
578 ch->write = smd_packet_write;
579 ch->read_avail = smd_packet_read_avail;
580 ch->write_avail = smd_packet_write_avail;
581 ch->update_state = update_packet_state;
583 ch->read = smd_stream_read;
584 ch->write = smd_stream_write;
585 ch->read_avail = smd_stream_read_avail;
586 ch->write_avail = smd_stream_write_avail;
587 ch->update_state = update_stream_state;
590 if ((type & 0xff) == 0)
591 memcpy(ch->name, "SMD_", 4);
593 memcpy(ch->name, "DSP_", 4);
594 memcpy(ch->name + 4, name, 20);
596 ch->pdev.name = ch->name;
599 pr_debug("smd_alloc_channel() cid=%02d size=%05d '%s'\n",
600 ch->n, ch->fifo_size, ch->name);
602 mutex_lock(&smd_creation_mutex);
603 list_add(&ch->ch_list, &smd_ch_closed_list);
604 mutex_unlock(&smd_creation_mutex);
606 platform_device_register(&ch->pdev);
610 static void smd_channel_probe_worker(struct work_struct *work)
612 struct smd_alloc_elm *shared;
617 shared = smem_find(ID_CH_ALLOC_TBL, sizeof(*shared) * 64);
619 pr_err("cannot find allocation table\n");
622 for (n = 0; n < 64; n++) {
623 if (smd_ch_allocated[n])
625 if (!shared[n].ref_count)
627 if (!shared[n].name[0])
629 ctype = shared[n].ctype;
630 type = ctype & SMD_TYPE_MASK;
632 /* DAL channels are stream but neither the modem,
633 * nor the DSP correctly indicate this. Fixup manually.
635 if (!memcmp(shared[n].name, "DAL", 3))
636 ctype = (ctype & (~SMD_KIND_MASK)) | SMD_KIND_STREAM;
638 type = shared[n].ctype & SMD_TYPE_MASK;
639 if ((type == SMD_TYPE_APPS_MODEM) ||
640 (type == SMD_TYPE_APPS_DSP))
641 if (!smd_alloc_channel(shared[n].name, shared[n].cid, ctype))
642 smd_ch_allocated[n] = 1;
646 static void do_nothing_notify(void *priv, unsigned flags)
650 struct smd_channel *smd_get_channel(const char *name)
652 struct smd_channel *ch;
654 mutex_lock(&smd_creation_mutex);
655 list_for_each_entry(ch, &smd_ch_closed_list, ch_list) {
656 if (!strcmp(name, ch->name)) {
657 list_del(&ch->ch_list);
658 mutex_unlock(&smd_creation_mutex);
662 mutex_unlock(&smd_creation_mutex);
667 int smd_open(const char *name, smd_channel_t **_ch,
668 void *priv, void (*notify)(void *, unsigned))
670 struct smd_channel *ch;
673 if (smd_initialized == 0) {
674 pr_info("smd_open() before smd_init()\n");
678 ch = smd_get_channel(name);
683 notify = do_nothing_notify;
686 ch->current_packet = 0;
687 ch->last_state = SMD_SS_CLOSED;
692 spin_lock_irqsave(&smd_lock, flags);
694 if ((ch->type & SMD_TYPE_MASK) == SMD_TYPE_APPS_MODEM)
695 list_add(&ch->ch_list, &smd_ch_list_modem);
697 list_add(&ch->ch_list, &smd_ch_list_dsp);
699 /* If the remote side is CLOSING, we need to get it to
700 * move to OPENING (which we'll do by moving from CLOSED to
701 * OPENING) and then get it to move from OPENING to
702 * OPENED (by doing the same state change ourselves).
704 * Otherwise, it should be OPENING and we can move directly
705 * to OPENED so that it will follow.
707 if (ch->recv->state == SMD_SS_CLOSING) {
709 ch_set_state(ch, SMD_SS_OPENING);
711 ch_set_state(ch, SMD_SS_OPENED);
713 spin_unlock_irqrestore(&smd_lock, flags);
719 int smd_close(smd_channel_t *ch)
726 spin_lock_irqsave(&smd_lock, flags);
727 ch->notify = do_nothing_notify;
728 list_del(&ch->ch_list);
729 ch_set_state(ch, SMD_SS_CLOSED);
730 spin_unlock_irqrestore(&smd_lock, flags);
732 mutex_lock(&smd_creation_mutex);
733 list_add(&ch->ch_list, &smd_ch_closed_list);
734 mutex_unlock(&smd_creation_mutex);
739 int smd_read(smd_channel_t *ch, void *data, int len)
741 return ch->read(ch, data, len);
744 int smd_write(smd_channel_t *ch, const void *data, int len)
746 return ch->write(ch, data, len);
749 int smd_write_atomic(smd_channel_t *ch, const void *data, int len)
753 spin_lock_irqsave(&smd_lock, flags);
754 res = ch->write(ch, data, len);
755 spin_unlock_irqrestore(&smd_lock, flags);
759 int smd_read_avail(smd_channel_t *ch)
761 return ch->read_avail(ch);
764 int smd_write_avail(smd_channel_t *ch)
766 return ch->write_avail(ch);
769 int smd_wait_until_readable(smd_channel_t *ch, int bytes)
774 int smd_wait_until_writable(smd_channel_t *ch, int bytes)
779 int smd_cur_packet_size(smd_channel_t *ch)
781 return ch->current_packet;
785 /* ------------------------------------------------------------------------- */
787 void *smem_alloc(unsigned id, unsigned size)
789 return smem_find(id, size);
792 void *smem_item(unsigned id, unsigned *size)
794 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
795 struct smem_heap_entry *toc = shared->heap_toc;
797 if (id >= SMEM_NUM_ITEMS)
800 if (toc[id].allocated) {
801 *size = toc[id].size;
802 return (void *) (MSM_SHARED_RAM_BASE + toc[id].offset);
810 void *smem_find(unsigned id, unsigned size_in)
815 ptr = smem_item(id, &size);
819 size_in = ALIGN(size_in, 8);
820 if (size_in != size) {
821 pr_err("smem_find(%d, %d): wrong size %d\n",
829 static irqreturn_t smsm_irq_handler(int irq, void *data)
834 spin_lock_irqsave(&smem_lock, flags);
836 apps = raw_smsm_get_state(SMSM_STATE_APPS);
837 modm = raw_smsm_get_state(SMSM_STATE_MODEM);
839 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
840 pr_info("<SM %08x %08x>\n", apps, modm);
841 if (modm & SMSM_RESET)
842 handle_modem_crash();
846 spin_unlock_irqrestore(&smem_lock, flags);
850 int smsm_change_state(enum smsm_state_item item,
851 uint32_t clear_mask, uint32_t set_mask)
853 unsigned long addr = smd_info.state + item * 4;
860 spin_lock_irqsave(&smem_lock, flags);
862 if (raw_smsm_get_state(SMSM_STATE_MODEM) & SMSM_RESET)
863 handle_modem_crash();
865 state = (readl(addr) & ~clear_mask) | set_mask;
868 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
869 pr_info("smsm_change_state %d %x\n", item, state);
872 spin_unlock_irqrestore(&smem_lock, flags);
877 uint32_t smsm_get_state(enum smsm_state_item item)
882 spin_lock_irqsave(&smem_lock, flags);
884 rv = readl(smd_info.state + item * 4);
886 if (item == SMSM_STATE_MODEM && (rv & SMSM_RESET))
887 handle_modem_crash();
889 spin_unlock_irqrestore(&smem_lock, flags);
894 #ifdef CONFIG_ARCH_MSM_SCORPION
896 int smsm_set_sleep_duration(uint32_t delay)
898 struct msm_dem_slave_data *ptr;
900 ptr = smem_find(SMEM_APPS_DEM_SLAVE_DATA, sizeof(*ptr));
902 pr_err("smsm_set_sleep_duration <SM NO APPS_DEM_SLAVE_DATA>\n");
905 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
906 pr_info("smsm_set_sleep_duration %d -> %d\n",
907 ptr->sleep_time, delay);
908 ptr->sleep_time = delay;
914 int smsm_set_sleep_duration(uint32_t delay)
918 ptr = smem_find(SMEM_SMSM_SLEEP_DELAY, sizeof(*ptr));
920 pr_err("smsm_set_sleep_duration <SM NO SLEEP_DELAY>\n");
923 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
924 pr_info("smsm_set_sleep_duration %d -> %d\n",
932 int smd_core_init(void)
936 /* wait for essential items to be initialized */
940 state = smem_item(SMEM_SMSM_SHARED_STATE, &size);
941 if (size == SMSM_V1_SIZE || size == SMSM_V2_SIZE) {
942 smd_info.state = (unsigned)state;
949 r = request_irq(INT_A9_M2A_0, smd_modem_irq_handler,
950 IRQF_TRIGGER_RISING, "smd_dev", 0);
953 r = enable_irq_wake(INT_A9_M2A_0);
955 pr_err("smd_core_init: enable_irq_wake failed for A9_M2A_0\n");
957 r = request_irq(INT_A9_M2A_5, smsm_irq_handler,
958 IRQF_TRIGGER_RISING, "smsm_dev", 0);
960 free_irq(INT_A9_M2A_0, 0);
963 r = enable_irq_wake(INT_A9_M2A_5);
965 pr_err("smd_core_init: enable_irq_wake failed for A9_M2A_5\n");
967 #if defined(CONFIG_QDSP6)
968 r = request_irq(INT_ADSP_A11, smd_dsp_irq_handler,
969 IRQF_TRIGGER_RISING, "smd_dsp", 0);
971 free_irq(INT_A9_M2A_0, 0);
972 free_irq(INT_A9_M2A_5, 0);
977 /* check for any SMD channels that may already exist */
980 /* indicate that we're up and running */
981 smsm_change_state(SMSM_STATE_APPS,
982 ~0, SMSM_INIT | SMSM_SMDINIT | SMSM_RPCINIT | SMSM_RUN);
983 #ifdef CONFIG_ARCH_MSM_SCORPION
984 smsm_change_state(SMSM_STATE_APPS_DEM, ~0, 0);
990 static int __devinit msm_smd_probe(struct platform_device *pdev)
993 * If we haven't waited for the ARM9 to boot up till now,
994 * then we need to wait here. Otherwise this should just
995 * return immediately.
997 proc_comm_boot_wait();
999 INIT_WORK(&probe_work, smd_channel_probe_worker);
1001 if (smd_core_init()) {
1002 pr_err("smd_core_init() failed\n");
1008 msm_check_for_modem_crash = check_for_modem_crash;
1010 msm_init_last_radio_log(THIS_MODULE);
1012 smd_initialized = 1;
1017 static struct platform_driver msm_smd_driver = {
1018 .probe = msm_smd_probe,
1020 .name = MODULE_NAME,
1021 .owner = THIS_MODULE,
1025 static int __init msm_smd_init(void)
1027 return platform_driver_register(&msm_smd_driver);
1030 module_init(msm_smd_init);
1032 MODULE_DESCRIPTION("MSM Shared Memory Core");
1033 MODULE_AUTHOR("Brian Swetland <swetland@google.com>");
1034 MODULE_LICENSE("GPL");