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>
30 #include <mach/msm_smd.h>
31 #include <mach/system.h>
33 #include "smd_private.h"
34 #include "proc_comm.h"
36 #if defined(CONFIG_ARCH_QSD8X50)
37 #define CONFIG_QDSP6 1
40 void (*msm_hw_reset_hook)(void);
42 #define MODULE_NAME "msm_smd"
45 MSM_SMD_DEBUG = 1U << 0,
46 MSM_SMSM_DEBUG = 1U << 0,
49 static int msm_smd_debug_mask;
56 static unsigned dummy_state[SMSM_STATE_COUNT];
58 static struct shared_info smd_info = {
59 .state = (unsigned) &dummy_state,
62 module_param_named(debug_mask, msm_smd_debug_mask,
63 int, S_IRUGO | S_IWUSR | S_IWGRP);
65 void *smem_item(unsigned id, unsigned *size);
66 static void smd_diag(void);
68 static unsigned last_heap_free = 0xffffffff;
70 static inline void notify_other_smsm(void)
78 static inline void notify_modem_smd(void)
83 static inline void notify_dsp_smd(void)
88 static void smd_diag(void)
92 x = smem_find(ID_DIAG_ERR_MSG, SZ_DIAG_ERR_MSG);
94 x[SZ_DIAG_ERR_MSG - 1] = 0;
95 pr_info("smem: DIAG '%s'\n", x);
99 /* call when SMSM_RESET flag is set in the A9's smsm_state */
100 static void handle_modem_crash(void)
102 pr_err("ARM9 has CRASHED\n");
105 /* hard reboot if possible */
106 if (msm_hw_reset_hook)
109 /* in this case the modem or watchdog should reboot us */
114 uint32_t raw_smsm_get_state(enum smsm_state_item item)
116 return readl(smd_info.state + item * 4);
119 static int check_for_modem_crash(void)
121 if (raw_smsm_get_state(SMSM_STATE_MODEM) & SMSM_RESET) {
122 handle_modem_crash();
128 /* the spinlock is used to synchronize between the
129 * irq handler and code that mutates the channel
130 * list or fiddles with channel state
132 DEFINE_SPINLOCK(smd_lock);
133 DEFINE_SPINLOCK(smem_lock);
135 /* the mutex is used during open() and close()
136 * operations to avoid races while creating or
137 * destroying smd_channel structures
139 static DEFINE_MUTEX(smd_creation_mutex);
141 static int smd_initialized;
143 LIST_HEAD(smd_ch_closed_list);
144 LIST_HEAD(smd_ch_list_modem);
145 LIST_HEAD(smd_ch_list_dsp);
147 static unsigned char smd_ch_allocated[64];
148 static struct work_struct probe_work;
150 static int smd_alloc_channel(const char *name, uint32_t cid, uint32_t type);
152 static void smd_channel_probe_worker(struct work_struct *work)
154 struct smd_alloc_elm *shared;
159 shared = smem_find(ID_CH_ALLOC_TBL, sizeof(*shared) * 64);
161 pr_err("smd: cannot find allocation table\n");
164 for (n = 0; n < 64; n++) {
165 if (smd_ch_allocated[n])
167 if (!shared[n].ref_count)
169 if (!shared[n].name[0])
171 ctype = shared[n].ctype;
172 type = ctype & SMD_TYPE_MASK;
174 /* DAL channels are stream but neither the modem,
175 * nor the DSP correctly indicate this. Fixup manually.
177 if (!memcmp(shared[n].name, "DAL", 3))
178 ctype = (ctype & (~SMD_KIND_MASK)) | SMD_KIND_STREAM;
180 type = shared[n].ctype & SMD_TYPE_MASK;
181 if ((type == SMD_TYPE_APPS_MODEM) ||
182 (type == SMD_TYPE_APPS_DSP))
183 if (!smd_alloc_channel(shared[n].name, shared[n].cid, ctype))
184 smd_ch_allocated[n] = 1;
188 /* how many bytes are available for reading */
189 static int smd_stream_read_avail(struct smd_channel *ch)
191 return (ch->recv->head - ch->recv->tail) & ch->fifo_mask;
194 /* how many bytes we are free to write */
195 static int smd_stream_write_avail(struct smd_channel *ch)
197 return ch->fifo_mask -
198 ((ch->send->head - ch->send->tail) & ch->fifo_mask);
201 static int smd_packet_read_avail(struct smd_channel *ch)
203 if (ch->current_packet) {
204 int n = smd_stream_read_avail(ch);
205 if (n > ch->current_packet)
206 n = ch->current_packet;
213 static int smd_packet_write_avail(struct smd_channel *ch)
215 int n = smd_stream_write_avail(ch);
216 return n > SMD_HEADER_SIZE ? n - SMD_HEADER_SIZE : 0;
219 static int ch_is_open(struct smd_channel *ch)
221 return (ch->recv->state == SMD_SS_OPENED) &&
222 (ch->send->state == SMD_SS_OPENED);
225 /* provide a pointer and length to readable data in the fifo */
226 static unsigned ch_read_buffer(struct smd_channel *ch, void **ptr)
228 unsigned head = ch->recv->head;
229 unsigned tail = ch->recv->tail;
230 *ptr = (void *) (ch->recv_data + tail);
235 return ch->fifo_size - tail;
238 /* advance the fifo read pointer after data from ch_read_buffer is consumed */
239 static void ch_read_done(struct smd_channel *ch, unsigned count)
241 BUG_ON(count > smd_stream_read_avail(ch));
242 ch->recv->tail = (ch->recv->tail + count) & ch->fifo_mask;
246 /* basic read interface to ch_read_{buffer,done} used
247 * by smd_*_read() and update_packet_state()
248 * will read-and-discard if the _data pointer is null
250 static int ch_read(struct smd_channel *ch, void *_data, int len)
254 unsigned char *data = _data;
258 n = ch_read_buffer(ch, &ptr);
265 memcpy(data, ptr, n);
272 return orig_len - len;
275 static void update_stream_state(struct smd_channel *ch)
277 /* streams have no special state requiring updating */
280 static void update_packet_state(struct smd_channel *ch)
285 /* can't do anything if we're in the middle of a packet */
286 if (ch->current_packet != 0)
289 /* don't bother unless we can get the full header */
290 if (smd_stream_read_avail(ch) < SMD_HEADER_SIZE)
293 r = ch_read(ch, hdr, SMD_HEADER_SIZE);
294 BUG_ON(r != SMD_HEADER_SIZE);
296 ch->current_packet = hdr[0];
299 /* provide a pointer and length to next free space in the fifo */
300 static unsigned ch_write_buffer(struct smd_channel *ch, void **ptr)
302 unsigned head = ch->send->head;
303 unsigned tail = ch->send->tail;
304 *ptr = (void *) (ch->send_data + head);
307 return tail - head - 1;
310 return ch->fifo_size - head - 1;
312 return ch->fifo_size - head;
316 /* advace the fifo write pointer after freespace
317 * from ch_write_buffer is filled
319 static void ch_write_done(struct smd_channel *ch, unsigned count)
321 BUG_ON(count > smd_stream_write_avail(ch));
322 ch->send->head = (ch->send->head + count) & ch->fifo_mask;
326 static void ch_set_state(struct smd_channel *ch, unsigned n)
328 if (n == SMD_SS_OPENED) {
338 ch->send->fSTATE = 1;
339 ch->notify_other_cpu();
342 static void do_smd_probe(void)
344 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
345 if (shared->heap_info.free_offset != last_heap_free) {
346 last_heap_free = shared->heap_info.free_offset;
347 schedule_work(&probe_work);
351 static void smd_state_change(struct smd_channel *ch,
352 unsigned last, unsigned next)
354 ch->last_state = next;
356 pr_info("SMD: ch %d %d -> %d\n", ch->n, last, next);
362 if (ch->send->state != SMD_SS_OPENED)
363 ch_set_state(ch, SMD_SS_OPENED);
364 ch->notify(ch->priv, SMD_EVENT_OPEN);
366 case SMD_SS_FLUSHING:
368 /* we should force them to close? */
370 ch->notify(ch->priv, SMD_EVENT_CLOSE);
374 static void handle_smd_irq(struct list_head *list, void (*notify)(void))
377 struct smd_channel *ch;
382 spin_lock_irqsave(&smd_lock, flags);
383 list_for_each_entry(ch, list, ch_list) {
385 if (ch_is_open(ch)) {
386 if (ch->recv->fHEAD) {
391 if (ch->recv->fTAIL) {
396 if (ch->recv->fSTATE) {
397 ch->recv->fSTATE = 0;
402 tmp = ch->recv->state;
403 if (tmp != ch->last_state)
404 smd_state_change(ch, ch->last_state, tmp);
406 ch->update_state(ch);
407 ch->notify(ch->priv, SMD_EVENT_DATA);
412 spin_unlock_irqrestore(&smd_lock, flags);
416 static irqreturn_t smd_modem_irq_handler(int irq, void *data)
418 handle_smd_irq(&smd_ch_list_modem, notify_modem_smd);
422 static irqreturn_t smd_dsp_irq_handler(int irq, void *data)
424 handle_smd_irq(&smd_ch_list_dsp, notify_dsp_smd);
428 static void smd_fake_irq_handler(unsigned long arg)
430 handle_smd_irq(&smd_ch_list_modem, notify_modem_smd);
431 handle_smd_irq(&smd_ch_list_dsp, notify_dsp_smd);
434 static DECLARE_TASKLET(smd_fake_irq_tasklet, smd_fake_irq_handler, 0);
436 static inline int smd_need_int(struct smd_channel *ch)
438 if (ch_is_open(ch)) {
439 if (ch->recv->fHEAD || ch->recv->fTAIL || ch->recv->fSTATE)
441 if (ch->recv->state != ch->last_state)
447 void smd_sleep_exit(void)
450 struct smd_channel *ch;
453 spin_lock_irqsave(&smd_lock, flags);
454 list_for_each_entry(ch, &smd_ch_list_modem, ch_list) {
455 if (smd_need_int(ch)) {
460 list_for_each_entry(ch, &smd_ch_list_dsp, ch_list) {
461 if (smd_need_int(ch)) {
466 spin_unlock_irqrestore(&smd_lock, flags);
470 if (msm_smd_debug_mask & MSM_SMD_DEBUG)
471 pr_info("smd_sleep_exit need interrupt\n");
472 tasklet_schedule(&smd_fake_irq_tasklet);
477 void smd_kick(smd_channel_t *ch)
482 spin_lock_irqsave(&smd_lock, flags);
483 ch->update_state(ch);
484 tmp = ch->recv->state;
485 if (tmp != ch->last_state) {
486 ch->last_state = tmp;
487 if (tmp == SMD_SS_OPENED)
488 ch->notify(ch->priv, SMD_EVENT_OPEN);
490 ch->notify(ch->priv, SMD_EVENT_CLOSE);
492 ch->notify(ch->priv, SMD_EVENT_DATA);
493 ch->notify_other_cpu();
494 spin_unlock_irqrestore(&smd_lock, flags);
497 static int smd_is_packet(int chn, unsigned type)
499 type &= SMD_KIND_MASK;
500 if (type == SMD_KIND_PACKET)
502 if (type == SMD_KIND_STREAM)
505 /* older AMSS reports SMD_KIND_UNKNOWN always */
506 if ((chn > 4) || (chn == 1))
512 static int smd_stream_write(smd_channel_t *ch, const void *_data, int len)
515 const unsigned char *buf = _data;
522 while ((xfer = ch_write_buffer(ch, &ptr)) != 0) {
527 memcpy(ptr, buf, xfer);
528 ch_write_done(ch, xfer);
535 ch->notify_other_cpu();
537 return orig_len - len;
540 static int smd_packet_write(smd_channel_t *ch, const void *_data, int len)
547 if (smd_stream_write_avail(ch) < (len + SMD_HEADER_SIZE))
551 hdr[1] = hdr[2] = hdr[3] = hdr[4] = 0;
553 smd_stream_write(ch, hdr, sizeof(hdr));
554 smd_stream_write(ch, _data, len);
559 static int smd_stream_read(smd_channel_t *ch, void *data, int len)
566 r = ch_read(ch, data, len);
568 ch->notify_other_cpu();
573 static int smd_packet_read(smd_channel_t *ch, void *data, int len)
581 if (len > ch->current_packet)
582 len = ch->current_packet;
584 r = ch_read(ch, data, len);
586 ch->notify_other_cpu();
588 spin_lock_irqsave(&smd_lock, flags);
589 ch->current_packet -= r;
590 update_packet_state(ch);
591 spin_unlock_irqrestore(&smd_lock, flags);
596 static int smd_alloc_channel(const char *name, uint32_t cid, uint32_t type)
598 struct smd_channel *ch;
600 ch = kzalloc(sizeof(struct smd_channel), GFP_KERNEL);
602 pr_err("smd_alloc_channel() out of memory\n");
607 if (_smd_alloc_channel(ch)) {
612 ch->fifo_mask = ch->fifo_size - 1;
615 if ((type & SMD_TYPE_MASK) == SMD_TYPE_APPS_MODEM)
616 ch->notify_other_cpu = notify_modem_smd;
618 ch->notify_other_cpu = notify_dsp_smd;
620 if (smd_is_packet(cid, type)) {
621 ch->read = smd_packet_read;
622 ch->write = smd_packet_write;
623 ch->read_avail = smd_packet_read_avail;
624 ch->write_avail = smd_packet_write_avail;
625 ch->update_state = update_packet_state;
627 ch->read = smd_stream_read;
628 ch->write = smd_stream_write;
629 ch->read_avail = smd_stream_read_avail;
630 ch->write_avail = smd_stream_write_avail;
631 ch->update_state = update_stream_state;
634 if ((type & 0xff) == 0)
635 memcpy(ch->name, "SMD_", 4);
637 memcpy(ch->name, "DSP_", 4);
638 memcpy(ch->name + 4, name, 20);
640 ch->pdev.name = ch->name;
643 pr_info("smd_alloc_channel() cid=%02d size=%05d '%s'\n",
644 ch->n, ch->fifo_size, ch->name);
646 mutex_lock(&smd_creation_mutex);
647 list_add(&ch->ch_list, &smd_ch_closed_list);
648 mutex_unlock(&smd_creation_mutex);
650 platform_device_register(&ch->pdev);
654 static void do_nothing_notify(void *priv, unsigned flags)
658 struct smd_channel *smd_get_channel(const char *name)
660 struct smd_channel *ch;
662 mutex_lock(&smd_creation_mutex);
663 list_for_each_entry(ch, &smd_ch_closed_list, ch_list) {
664 if (!strcmp(name, ch->name)) {
665 list_del(&ch->ch_list);
666 mutex_unlock(&smd_creation_mutex);
670 mutex_unlock(&smd_creation_mutex);
675 int smd_open(const char *name, smd_channel_t **_ch,
676 void *priv, void (*notify)(void *, unsigned))
678 struct smd_channel *ch;
681 if (smd_initialized == 0) {
682 pr_info("smd_open() before smd_init()\n");
686 ch = smd_get_channel(name);
691 notify = do_nothing_notify;
694 ch->current_packet = 0;
695 ch->last_state = SMD_SS_CLOSED;
700 spin_lock_irqsave(&smd_lock, flags);
702 if ((ch->type & SMD_TYPE_MASK) == SMD_TYPE_APPS_MODEM)
703 list_add(&ch->ch_list, &smd_ch_list_modem);
705 list_add(&ch->ch_list, &smd_ch_list_dsp);
707 /* If the remote side is CLOSING, we need to get it to
708 * move to OPENING (which we'll do by moving from CLOSED to
709 * OPENING) and then get it to move from OPENING to
710 * OPENED (by doing the same state change ourselves).
712 * Otherwise, it should be OPENING and we can move directly
713 * to OPENED so that it will follow.
715 if (ch->recv->state == SMD_SS_CLOSING) {
717 ch_set_state(ch, SMD_SS_OPENING);
719 ch_set_state(ch, SMD_SS_OPENED);
721 spin_unlock_irqrestore(&smd_lock, flags);
727 int smd_close(smd_channel_t *ch)
731 pr_info("smd_close(%p)\n", ch);
736 spin_lock_irqsave(&smd_lock, flags);
737 ch->notify = do_nothing_notify;
738 list_del(&ch->ch_list);
739 ch_set_state(ch, SMD_SS_CLOSED);
740 spin_unlock_irqrestore(&smd_lock, flags);
742 mutex_lock(&smd_creation_mutex);
743 list_add(&ch->ch_list, &smd_ch_closed_list);
744 mutex_unlock(&smd_creation_mutex);
749 int smd_read(smd_channel_t *ch, void *data, int len)
751 return ch->read(ch, data, len);
754 int smd_write(smd_channel_t *ch, const void *data, int len)
756 return ch->write(ch, data, len);
759 int smd_write_atomic(smd_channel_t *ch, const void *data, int len)
763 spin_lock_irqsave(&smd_lock, flags);
764 res = ch->write(ch, data, len);
765 spin_unlock_irqrestore(&smd_lock, flags);
769 int smd_read_avail(smd_channel_t *ch)
771 return ch->read_avail(ch);
774 int smd_write_avail(smd_channel_t *ch)
776 return ch->write_avail(ch);
779 int smd_wait_until_readable(smd_channel_t *ch, int bytes)
784 int smd_wait_until_writable(smd_channel_t *ch, int bytes)
789 int smd_cur_packet_size(smd_channel_t *ch)
791 return ch->current_packet;
795 /* ------------------------------------------------------------------------- */
797 void *smem_alloc(unsigned id, unsigned size)
799 return smem_find(id, size);
802 void *smem_item(unsigned id, unsigned *size)
804 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
805 struct smem_heap_entry *toc = shared->heap_toc;
807 if (id >= SMEM_NUM_ITEMS)
810 if (toc[id].allocated) {
811 *size = toc[id].size;
812 return (void *) (MSM_SHARED_RAM_BASE + toc[id].offset);
820 void *smem_find(unsigned id, unsigned size_in)
825 ptr = smem_item(id, &size);
829 size_in = ALIGN(size_in, 8);
830 if (size_in != size) {
831 pr_err("smem_find(%d, %d): wrong size %d\n",
839 static irqreturn_t smsm_irq_handler(int irq, void *data)
844 spin_lock_irqsave(&smem_lock, flags);
846 apps = raw_smsm_get_state(SMSM_STATE_APPS);
847 modm = raw_smsm_get_state(SMSM_STATE_MODEM);
849 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
850 pr_info("<SM %08x %08x>\n", apps, modm);
851 if (modm & SMSM_RESET)
852 handle_modem_crash();
856 spin_unlock_irqrestore(&smem_lock, flags);
860 int smsm_change_state(enum smsm_state_item item,
861 uint32_t clear_mask, uint32_t set_mask)
863 unsigned long addr = smd_info.state + item * 4;
870 spin_lock_irqsave(&smem_lock, flags);
872 if (raw_smsm_get_state(SMSM_STATE_MODEM) & SMSM_RESET)
873 handle_modem_crash();
875 state = (readl(addr) & ~clear_mask) | set_mask;
878 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
879 pr_info("smsm_change_state %d %x\n", item, state);
882 spin_unlock_irqrestore(&smem_lock, flags);
887 uint32_t smsm_get_state(enum smsm_state_item item)
892 spin_lock_irqsave(&smem_lock, flags);
894 rv = readl(smd_info.state + item * 4);
896 if (item == SMSM_STATE_MODEM && (rv & SMSM_RESET))
897 handle_modem_crash();
899 spin_unlock_irqrestore(&smem_lock, flags);
904 #ifdef CONFIG_ARCH_MSM_SCORPION
906 int smsm_set_sleep_duration(uint32_t delay)
908 struct msm_dem_slave_data *ptr;
910 ptr = smem_find(SMEM_APPS_DEM_SLAVE_DATA, sizeof(*ptr));
912 pr_err("smsm_set_sleep_duration <SM NO APPS_DEM_SLAVE_DATA>\n");
915 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
916 pr_info("smsm_set_sleep_duration %d -> %d\n",
917 ptr->sleep_time, delay);
918 ptr->sleep_time = delay;
924 int smsm_set_sleep_duration(uint32_t delay)
928 ptr = smem_find(SMEM_SMSM_SLEEP_DELAY, sizeof(*ptr));
930 pr_err("smsm_set_sleep_duration <SM NO SLEEP_DELAY>\n");
933 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
934 pr_info("smsm_set_sleep_duration %d -> %d\n",
942 int smd_core_init(void)
945 pr_info("smd_core_init()\n");
947 /* wait for essential items to be initialized */
951 state = smem_item(SMEM_SMSM_SHARED_STATE, &size);
952 if (size == SMSM_V1_SIZE || size == SMSM_V2_SIZE) {
953 smd_info.state = (unsigned)state;
960 r = request_irq(INT_A9_M2A_0, smd_modem_irq_handler,
961 IRQF_TRIGGER_RISING, "smd_dev", 0);
964 r = enable_irq_wake(INT_A9_M2A_0);
966 pr_err("smd_core_init: enable_irq_wake failed for A9_M2A_0\n");
968 r = request_irq(INT_A9_M2A_5, smsm_irq_handler,
969 IRQF_TRIGGER_RISING, "smsm_dev", 0);
971 free_irq(INT_A9_M2A_0, 0);
974 r = enable_irq_wake(INT_A9_M2A_5);
976 pr_err("smd_core_init: enable_irq_wake failed for A9_M2A_5\n");
978 #if defined(CONFIG_QDSP6)
979 r = request_irq(INT_ADSP_A11, smd_dsp_irq_handler,
980 IRQF_TRIGGER_RISING, "smd_dsp", 0);
982 free_irq(INT_A9_M2A_0, 0);
983 free_irq(INT_A9_M2A_5, 0);
988 /* check for any SMD channels that may already exist */
991 /* indicate that we're up and running */
992 smsm_change_state(SMSM_STATE_APPS,
993 ~0, SMSM_INIT | SMSM_SMDINIT | SMSM_RPCINIT | SMSM_RUN);
994 #ifdef CONFIG_ARCH_MSM_SCORPION
995 smsm_change_state(SMSM_STATE_APPS_DEM, ~0, 0);
998 pr_info("smd_core_init() done\n");
1003 static int __init msm_smd_probe(struct platform_device *pdev)
1005 pr_info("smd_init()\n");
1007 INIT_WORK(&probe_work, smd_channel_probe_worker);
1009 if (smd_core_init()) {
1010 pr_err("smd_core_init() failed\n");
1016 msm_check_for_modem_crash = check_for_modem_crash;
1018 msm_init_last_radio_log(THIS_MODULE);
1020 smd_initialized = 1;
1025 static struct platform_driver msm_smd_driver = {
1026 .probe = msm_smd_probe,
1028 .name = MODULE_NAME,
1029 .owner = THIS_MODULE,
1033 static int __init msm_smd_init(void)
1035 return platform_driver_register(&msm_smd_driver);
1038 module_init(msm_smd_init);
1040 MODULE_DESCRIPTION("MSM Shared Memory Core");
1041 MODULE_AUTHOR("Brian Swetland <swetland@google.com>");
1042 MODULE_LICENSE("GPL");