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 #if defined(CONFIG_ARCH_QSD8X50)
39 #define CONFIG_QDSP6 1
42 void (*msm_hw_reset_hook)(void);
44 #define MODULE_NAME "msm_smd"
47 MSM_SMD_DEBUG = 1U << 0,
48 MSM_SMSM_DEBUG = 1U << 0,
51 static int msm_smd_debug_mask;
58 static unsigned dummy_state[SMSM_STATE_COUNT];
60 static struct shared_info smd_info = {
61 .state = (unsigned) &dummy_state,
64 module_param_named(debug_mask, msm_smd_debug_mask,
65 int, S_IRUGO | S_IWUSR | S_IWGRP);
67 void *smem_item(unsigned id, unsigned *size);
68 static void smd_diag(void);
70 static unsigned last_heap_free = 0xffffffff;
72 static inline void msm_a2m_int(uint32_t irq)
74 #if defined(CONFIG_ARCH_MSM7X30)
75 writel(1 << irq, MSM_GCC_BASE + 0x8);
77 writel(1, MSM_CSR_BASE + 0x400 + (irq * 4));
82 static inline void notify_other_smsm(void)
90 static inline void notify_modem_smd(void)
95 static inline void notify_dsp_smd(void)
100 static void smd_diag(void)
104 x = smem_find(ID_DIAG_ERR_MSG, SZ_DIAG_ERR_MSG);
106 x[SZ_DIAG_ERR_MSG - 1] = 0;
107 pr_info("smem: DIAG '%s'\n", x);
111 /* call when SMSM_RESET flag is set in the A9's smsm_state */
112 static void handle_modem_crash(void)
114 pr_err("ARM9 has CRASHED\n");
117 /* hard reboot if possible */
118 if (msm_hw_reset_hook)
121 /* in this case the modem or watchdog should reboot us */
126 uint32_t raw_smsm_get_state(enum smsm_state_item item)
128 return readl(smd_info.state + item * 4);
131 static int check_for_modem_crash(void)
133 if (raw_smsm_get_state(SMSM_STATE_MODEM) & SMSM_RESET) {
134 handle_modem_crash();
140 /* the spinlock is used to synchronize between the
141 * irq handler and code that mutates the channel
142 * list or fiddles with channel state
144 DEFINE_SPINLOCK(smd_lock);
145 DEFINE_SPINLOCK(smem_lock);
147 /* the mutex is used during open() and close()
148 * operations to avoid races while creating or
149 * destroying smd_channel structures
151 static DEFINE_MUTEX(smd_creation_mutex);
153 static int smd_initialized;
155 LIST_HEAD(smd_ch_closed_list);
156 LIST_HEAD(smd_ch_list_modem);
157 LIST_HEAD(smd_ch_list_dsp);
159 static unsigned char smd_ch_allocated[64];
160 static struct work_struct probe_work;
162 static int smd_alloc_channel(const char *name, uint32_t cid, uint32_t type);
164 static void smd_channel_probe_worker(struct work_struct *work)
166 struct smd_alloc_elm *shared;
171 shared = smem_find(ID_CH_ALLOC_TBL, sizeof(*shared) * 64);
173 pr_err("smd: cannot find allocation table\n");
176 for (n = 0; n < 64; n++) {
177 if (smd_ch_allocated[n])
179 if (!shared[n].ref_count)
181 if (!shared[n].name[0])
183 ctype = shared[n].ctype;
184 type = ctype & SMD_TYPE_MASK;
186 /* DAL channels are stream but neither the modem,
187 * nor the DSP correctly indicate this. Fixup manually.
189 if (!memcmp(shared[n].name, "DAL", 3))
190 ctype = (ctype & (~SMD_KIND_MASK)) | SMD_KIND_STREAM;
192 type = shared[n].ctype & SMD_TYPE_MASK;
193 if ((type == SMD_TYPE_APPS_MODEM) ||
194 (type == SMD_TYPE_APPS_DSP))
195 if (!smd_alloc_channel(shared[n].name, shared[n].cid, ctype))
196 smd_ch_allocated[n] = 1;
200 /* how many bytes are available for reading */
201 static int smd_stream_read_avail(struct smd_channel *ch)
203 return (ch->recv->head - ch->recv->tail) & ch->fifo_mask;
206 /* how many bytes we are free to write */
207 static int smd_stream_write_avail(struct smd_channel *ch)
209 return ch->fifo_mask -
210 ((ch->send->head - ch->send->tail) & ch->fifo_mask);
213 static int smd_packet_read_avail(struct smd_channel *ch)
215 if (ch->current_packet) {
216 int n = smd_stream_read_avail(ch);
217 if (n > ch->current_packet)
218 n = ch->current_packet;
225 static int smd_packet_write_avail(struct smd_channel *ch)
227 int n = smd_stream_write_avail(ch);
228 return n > SMD_HEADER_SIZE ? n - SMD_HEADER_SIZE : 0;
231 static int ch_is_open(struct smd_channel *ch)
233 return (ch->recv->state == SMD_SS_OPENED) &&
234 (ch->send->state == SMD_SS_OPENED);
237 /* provide a pointer and length to readable data in the fifo */
238 static unsigned ch_read_buffer(struct smd_channel *ch, void **ptr)
240 unsigned head = ch->recv->head;
241 unsigned tail = ch->recv->tail;
242 *ptr = (void *) (ch->recv_data + tail);
247 return ch->fifo_size - tail;
250 /* advance the fifo read pointer after data from ch_read_buffer is consumed */
251 static void ch_read_done(struct smd_channel *ch, unsigned count)
253 BUG_ON(count > smd_stream_read_avail(ch));
254 ch->recv->tail = (ch->recv->tail + count) & ch->fifo_mask;
258 /* basic read interface to ch_read_{buffer,done} used
259 * by smd_*_read() and update_packet_state()
260 * will read-and-discard if the _data pointer is null
262 static int ch_read(struct smd_channel *ch, void *_data, int len)
266 unsigned char *data = _data;
270 n = ch_read_buffer(ch, &ptr);
277 memcpy(data, ptr, n);
284 return orig_len - len;
287 static void update_stream_state(struct smd_channel *ch)
289 /* streams have no special state requiring updating */
292 static void update_packet_state(struct smd_channel *ch)
297 /* can't do anything if we're in the middle of a packet */
298 if (ch->current_packet != 0)
301 /* don't bother unless we can get the full header */
302 if (smd_stream_read_avail(ch) < SMD_HEADER_SIZE)
305 r = ch_read(ch, hdr, SMD_HEADER_SIZE);
306 BUG_ON(r != SMD_HEADER_SIZE);
308 ch->current_packet = hdr[0];
311 /* provide a pointer and length to next free space in the fifo */
312 static unsigned ch_write_buffer(struct smd_channel *ch, void **ptr)
314 unsigned head = ch->send->head;
315 unsigned tail = ch->send->tail;
316 *ptr = (void *) (ch->send_data + head);
319 return tail - head - 1;
322 return ch->fifo_size - head - 1;
324 return ch->fifo_size - head;
328 /* advace the fifo write pointer after freespace
329 * from ch_write_buffer is filled
331 static void ch_write_done(struct smd_channel *ch, unsigned count)
333 BUG_ON(count > smd_stream_write_avail(ch));
334 ch->send->head = (ch->send->head + count) & ch->fifo_mask;
338 static void ch_set_state(struct smd_channel *ch, unsigned n)
340 if (n == SMD_SS_OPENED) {
350 ch->send->fSTATE = 1;
351 ch->notify_other_cpu();
354 static void do_smd_probe(void)
356 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
357 if (shared->heap_info.free_offset != last_heap_free) {
358 last_heap_free = shared->heap_info.free_offset;
359 schedule_work(&probe_work);
363 static void smd_state_change(struct smd_channel *ch,
364 unsigned last, unsigned next)
366 ch->last_state = next;
368 pr_info("SMD: ch %d %d -> %d\n", ch->n, last, next);
374 if (ch->send->state != SMD_SS_OPENED)
375 ch_set_state(ch, SMD_SS_OPENED);
376 ch->notify(ch->priv, SMD_EVENT_OPEN);
378 case SMD_SS_FLUSHING:
380 /* we should force them to close? */
382 ch->notify(ch->priv, SMD_EVENT_CLOSE);
386 static void handle_smd_irq(struct list_head *list, void (*notify)(void))
389 struct smd_channel *ch;
394 spin_lock_irqsave(&smd_lock, flags);
395 list_for_each_entry(ch, list, ch_list) {
397 if (ch_is_open(ch)) {
398 if (ch->recv->fHEAD) {
403 if (ch->recv->fTAIL) {
408 if (ch->recv->fSTATE) {
409 ch->recv->fSTATE = 0;
414 tmp = ch->recv->state;
415 if (tmp != ch->last_state)
416 smd_state_change(ch, ch->last_state, tmp);
418 ch->update_state(ch);
419 ch->notify(ch->priv, SMD_EVENT_DATA);
424 spin_unlock_irqrestore(&smd_lock, flags);
428 static irqreturn_t smd_modem_irq_handler(int irq, void *data)
430 handle_smd_irq(&smd_ch_list_modem, notify_modem_smd);
434 static irqreturn_t smd_dsp_irq_handler(int irq, void *data)
436 handle_smd_irq(&smd_ch_list_dsp, notify_dsp_smd);
440 static void smd_fake_irq_handler(unsigned long arg)
442 handle_smd_irq(&smd_ch_list_modem, notify_modem_smd);
443 handle_smd_irq(&smd_ch_list_dsp, notify_dsp_smd);
446 static DECLARE_TASKLET(smd_fake_irq_tasklet, smd_fake_irq_handler, 0);
448 static inline int smd_need_int(struct smd_channel *ch)
450 if (ch_is_open(ch)) {
451 if (ch->recv->fHEAD || ch->recv->fTAIL || ch->recv->fSTATE)
453 if (ch->recv->state != ch->last_state)
459 void smd_sleep_exit(void)
462 struct smd_channel *ch;
465 spin_lock_irqsave(&smd_lock, flags);
466 list_for_each_entry(ch, &smd_ch_list_modem, ch_list) {
467 if (smd_need_int(ch)) {
472 list_for_each_entry(ch, &smd_ch_list_dsp, ch_list) {
473 if (smd_need_int(ch)) {
478 spin_unlock_irqrestore(&smd_lock, flags);
482 if (msm_smd_debug_mask & MSM_SMD_DEBUG)
483 pr_info("smd_sleep_exit need interrupt\n");
484 tasklet_schedule(&smd_fake_irq_tasklet);
489 void smd_kick(smd_channel_t *ch)
494 spin_lock_irqsave(&smd_lock, flags);
495 ch->update_state(ch);
496 tmp = ch->recv->state;
497 if (tmp != ch->last_state) {
498 ch->last_state = tmp;
499 if (tmp == SMD_SS_OPENED)
500 ch->notify(ch->priv, SMD_EVENT_OPEN);
502 ch->notify(ch->priv, SMD_EVENT_CLOSE);
504 ch->notify(ch->priv, SMD_EVENT_DATA);
505 ch->notify_other_cpu();
506 spin_unlock_irqrestore(&smd_lock, flags);
509 static int smd_is_packet(int chn, unsigned type)
511 type &= SMD_KIND_MASK;
512 if (type == SMD_KIND_PACKET)
514 if (type == SMD_KIND_STREAM)
517 /* older AMSS reports SMD_KIND_UNKNOWN always */
518 if ((chn > 4) || (chn == 1))
524 static int smd_stream_write(smd_channel_t *ch, const void *_data, int len)
527 const unsigned char *buf = _data;
534 while ((xfer = ch_write_buffer(ch, &ptr)) != 0) {
539 memcpy(ptr, buf, xfer);
540 ch_write_done(ch, xfer);
547 ch->notify_other_cpu();
549 return orig_len - len;
552 static int smd_packet_write(smd_channel_t *ch, const void *_data, int len)
559 if (smd_stream_write_avail(ch) < (len + SMD_HEADER_SIZE))
563 hdr[1] = hdr[2] = hdr[3] = hdr[4] = 0;
565 smd_stream_write(ch, hdr, sizeof(hdr));
566 smd_stream_write(ch, _data, len);
571 static int smd_stream_read(smd_channel_t *ch, void *data, int len)
578 r = ch_read(ch, data, len);
580 ch->notify_other_cpu();
585 static int smd_packet_read(smd_channel_t *ch, void *data, int len)
593 if (len > ch->current_packet)
594 len = ch->current_packet;
596 r = ch_read(ch, data, len);
598 ch->notify_other_cpu();
600 spin_lock_irqsave(&smd_lock, flags);
601 ch->current_packet -= r;
602 update_packet_state(ch);
603 spin_unlock_irqrestore(&smd_lock, flags);
608 static int smd_alloc_channel(const char *name, uint32_t cid, uint32_t type)
610 struct smd_channel *ch;
612 ch = kzalloc(sizeof(struct smd_channel), GFP_KERNEL);
614 pr_err("smd_alloc_channel() out of memory\n");
619 if (_smd_alloc_channel(ch)) {
624 ch->fifo_mask = ch->fifo_size - 1;
627 if ((type & SMD_TYPE_MASK) == SMD_TYPE_APPS_MODEM)
628 ch->notify_other_cpu = notify_modem_smd;
630 ch->notify_other_cpu = notify_dsp_smd;
632 if (smd_is_packet(cid, type)) {
633 ch->read = smd_packet_read;
634 ch->write = smd_packet_write;
635 ch->read_avail = smd_packet_read_avail;
636 ch->write_avail = smd_packet_write_avail;
637 ch->update_state = update_packet_state;
639 ch->read = smd_stream_read;
640 ch->write = smd_stream_write;
641 ch->read_avail = smd_stream_read_avail;
642 ch->write_avail = smd_stream_write_avail;
643 ch->update_state = update_stream_state;
646 if ((type & 0xff) == 0)
647 memcpy(ch->name, "SMD_", 4);
649 memcpy(ch->name, "DSP_", 4);
650 memcpy(ch->name + 4, name, 20);
652 ch->pdev.name = ch->name;
655 pr_info("smd_alloc_channel() cid=%02d size=%05d '%s'\n",
656 ch->n, ch->fifo_size, ch->name);
658 mutex_lock(&smd_creation_mutex);
659 list_add(&ch->ch_list, &smd_ch_closed_list);
660 mutex_unlock(&smd_creation_mutex);
662 platform_device_register(&ch->pdev);
666 static void do_nothing_notify(void *priv, unsigned flags)
670 struct smd_channel *smd_get_channel(const char *name)
672 struct smd_channel *ch;
674 mutex_lock(&smd_creation_mutex);
675 list_for_each_entry(ch, &smd_ch_closed_list, ch_list) {
676 if (!strcmp(name, ch->name)) {
677 list_del(&ch->ch_list);
678 mutex_unlock(&smd_creation_mutex);
682 mutex_unlock(&smd_creation_mutex);
687 int smd_open(const char *name, smd_channel_t **_ch,
688 void *priv, void (*notify)(void *, unsigned))
690 struct smd_channel *ch;
693 if (smd_initialized == 0) {
694 pr_info("smd_open() before smd_init()\n");
698 ch = smd_get_channel(name);
703 notify = do_nothing_notify;
706 ch->current_packet = 0;
707 ch->last_state = SMD_SS_CLOSED;
712 spin_lock_irqsave(&smd_lock, flags);
714 if ((ch->type & SMD_TYPE_MASK) == SMD_TYPE_APPS_MODEM)
715 list_add(&ch->ch_list, &smd_ch_list_modem);
717 list_add(&ch->ch_list, &smd_ch_list_dsp);
719 /* If the remote side is CLOSING, we need to get it to
720 * move to OPENING (which we'll do by moving from CLOSED to
721 * OPENING) and then get it to move from OPENING to
722 * OPENED (by doing the same state change ourselves).
724 * Otherwise, it should be OPENING and we can move directly
725 * to OPENED so that it will follow.
727 if (ch->recv->state == SMD_SS_CLOSING) {
729 ch_set_state(ch, SMD_SS_OPENING);
731 ch_set_state(ch, SMD_SS_OPENED);
733 spin_unlock_irqrestore(&smd_lock, flags);
739 int smd_close(smd_channel_t *ch)
743 pr_info("smd_close(%p)\n", ch);
748 spin_lock_irqsave(&smd_lock, flags);
749 ch->notify = do_nothing_notify;
750 list_del(&ch->ch_list);
751 ch_set_state(ch, SMD_SS_CLOSED);
752 spin_unlock_irqrestore(&smd_lock, flags);
754 mutex_lock(&smd_creation_mutex);
755 list_add(&ch->ch_list, &smd_ch_closed_list);
756 mutex_unlock(&smd_creation_mutex);
761 int smd_read(smd_channel_t *ch, void *data, int len)
763 return ch->read(ch, data, len);
766 int smd_write(smd_channel_t *ch, const void *data, int len)
768 return ch->write(ch, data, len);
771 int smd_write_atomic(smd_channel_t *ch, const void *data, int len)
775 spin_lock_irqsave(&smd_lock, flags);
776 res = ch->write(ch, data, len);
777 spin_unlock_irqrestore(&smd_lock, flags);
781 int smd_read_avail(smd_channel_t *ch)
783 return ch->read_avail(ch);
786 int smd_write_avail(smd_channel_t *ch)
788 return ch->write_avail(ch);
791 int smd_wait_until_readable(smd_channel_t *ch, int bytes)
796 int smd_wait_until_writable(smd_channel_t *ch, int bytes)
801 int smd_cur_packet_size(smd_channel_t *ch)
803 return ch->current_packet;
807 /* ------------------------------------------------------------------------- */
809 void *smem_alloc(unsigned id, unsigned size)
811 return smem_find(id, size);
814 void *smem_item(unsigned id, unsigned *size)
816 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
817 struct smem_heap_entry *toc = shared->heap_toc;
819 if (id >= SMEM_NUM_ITEMS)
822 if (toc[id].allocated) {
823 *size = toc[id].size;
824 return (void *) (MSM_SHARED_RAM_BASE + toc[id].offset);
832 void *smem_find(unsigned id, unsigned size_in)
837 ptr = smem_item(id, &size);
841 size_in = ALIGN(size_in, 8);
842 if (size_in != size) {
843 pr_err("smem_find(%d, %d): wrong size %d\n",
851 static irqreturn_t smsm_irq_handler(int irq, void *data)
856 spin_lock_irqsave(&smem_lock, flags);
858 apps = raw_smsm_get_state(SMSM_STATE_APPS);
859 modm = raw_smsm_get_state(SMSM_STATE_MODEM);
861 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
862 pr_info("<SM %08x %08x>\n", apps, modm);
863 if (modm & SMSM_RESET)
864 handle_modem_crash();
868 spin_unlock_irqrestore(&smem_lock, flags);
872 int smsm_change_state(enum smsm_state_item item,
873 uint32_t clear_mask, uint32_t set_mask)
875 unsigned long addr = smd_info.state + item * 4;
882 spin_lock_irqsave(&smem_lock, flags);
884 if (raw_smsm_get_state(SMSM_STATE_MODEM) & SMSM_RESET)
885 handle_modem_crash();
887 state = (readl(addr) & ~clear_mask) | set_mask;
890 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
891 pr_info("smsm_change_state %d %x\n", item, state);
894 spin_unlock_irqrestore(&smem_lock, flags);
899 uint32_t smsm_get_state(enum smsm_state_item item)
904 spin_lock_irqsave(&smem_lock, flags);
906 rv = readl(smd_info.state + item * 4);
908 if (item == SMSM_STATE_MODEM && (rv & SMSM_RESET))
909 handle_modem_crash();
911 spin_unlock_irqrestore(&smem_lock, flags);
916 #ifdef CONFIG_ARCH_MSM_SCORPION
918 int smsm_set_sleep_duration(uint32_t delay)
920 struct msm_dem_slave_data *ptr;
922 ptr = smem_find(SMEM_APPS_DEM_SLAVE_DATA, sizeof(*ptr));
924 pr_err("smsm_set_sleep_duration <SM NO APPS_DEM_SLAVE_DATA>\n");
927 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
928 pr_info("smsm_set_sleep_duration %d -> %d\n",
929 ptr->sleep_time, delay);
930 ptr->sleep_time = delay;
936 int smsm_set_sleep_duration(uint32_t delay)
940 ptr = smem_find(SMEM_SMSM_SLEEP_DELAY, sizeof(*ptr));
942 pr_err("smsm_set_sleep_duration <SM NO SLEEP_DELAY>\n");
945 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
946 pr_info("smsm_set_sleep_duration %d -> %d\n",
954 int smd_core_init(void)
957 pr_info("smd_core_init()\n");
959 /* wait for essential items to be initialized */
963 state = smem_item(SMEM_SMSM_SHARED_STATE, &size);
964 if (size == SMSM_V1_SIZE || size == SMSM_V2_SIZE) {
965 smd_info.state = (unsigned)state;
972 r = request_irq(INT_A9_M2A_0, smd_modem_irq_handler,
973 IRQF_TRIGGER_RISING, "smd_dev", 0);
976 r = enable_irq_wake(INT_A9_M2A_0);
978 pr_err("smd_core_init: enable_irq_wake failed for A9_M2A_0\n");
980 r = request_irq(INT_A9_M2A_5, smsm_irq_handler,
981 IRQF_TRIGGER_RISING, "smsm_dev", 0);
983 free_irq(INT_A9_M2A_0, 0);
986 r = enable_irq_wake(INT_A9_M2A_5);
988 pr_err("smd_core_init: enable_irq_wake failed for A9_M2A_5\n");
990 #if defined(CONFIG_QDSP6)
991 r = request_irq(INT_ADSP_A11, smd_dsp_irq_handler,
992 IRQF_TRIGGER_RISING, "smd_dsp", 0);
994 free_irq(INT_A9_M2A_0, 0);
995 free_irq(INT_A9_M2A_5, 0);
1000 /* check for any SMD channels that may already exist */
1003 /* indicate that we're up and running */
1004 smsm_change_state(SMSM_STATE_APPS,
1005 ~0, SMSM_INIT | SMSM_SMDINIT | SMSM_RPCINIT | SMSM_RUN);
1006 #ifdef CONFIG_ARCH_MSM_SCORPION
1007 smsm_change_state(SMSM_STATE_APPS_DEM, ~0, 0);
1010 pr_info("smd_core_init() done\n");
1015 static int __init msm_smd_probe(struct platform_device *pdev)
1017 pr_info("smd_init()\n");
1019 INIT_WORK(&probe_work, smd_channel_probe_worker);
1021 if (smd_core_init()) {
1022 pr_err("smd_core_init() failed\n");
1028 msm_check_for_modem_crash = check_for_modem_crash;
1030 msm_init_last_radio_log(THIS_MODULE);
1032 smd_initialized = 1;
1037 static struct platform_driver msm_smd_driver = {
1038 .probe = msm_smd_probe,
1040 .name = MODULE_NAME,
1041 .owner = THIS_MODULE,
1045 static int __init msm_smd_init(void)
1047 return platform_driver_register(&msm_smd_driver);
1050 module_init(msm_smd_init);
1052 MODULE_DESCRIPTION("MSM Shared Memory Core");
1053 MODULE_AUTHOR("Brian Swetland <swetland@google.com>");
1054 MODULE_LICENSE("GPL");