17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 #include <linux/module.h>
23 #include <linux/device.h>
24 #include <linux/wait.h>
27 #include <linux/list.h>
28 #include <linux/slab.h>
37 #if defined(CONFIG_ARCH_QSD8X50)
38 #define CONFIG_QDSP6 1
41 #define MODULE_NAME "msm_smd"
48 static int msm_smd_debug_mask;
59 .state = &dummy_state,
65 static unsigned last_heap_free = 0xffffffff;
67 static inline void notify_other_smsm(
void)
75 static inline void notify_modem_smd(
void)
80 static inline void notify_dsp_smd(
void)
85 static void smd_diag(
void)
97 static void handle_modem_crash(
void)
99 pr_err(
"ARM9 has CRASHED\n");
112 static int check_for_modem_crash(
void)
115 handle_modem_crash();
134 static int smd_initialized;
140 static unsigned char smd_ch_allocated[64];
144 static int smd_stream_read_avail(
struct smd_channel *ch)
150 static int smd_stream_write_avail(
struct smd_channel *ch)
156 static int smd_packet_read_avail(
struct smd_channel *ch)
159 int n = smd_stream_read_avail(ch);
168 static int smd_packet_write_avail(
struct smd_channel *ch)
170 int n = smd_stream_write_avail(ch);
181 static unsigned ch_read_buffer(
struct smd_channel *ch,
void **
ptr)
196 BUG_ON(count > smd_stream_read_avail(ch));
205 static int ch_read(
struct smd_channel *ch,
void *_data,
int len)
209 unsigned char *
data = _data;
213 n = ch_read_buffer(ch, &ptr);
227 return orig_len - len;
230 static void update_stream_state(
struct smd_channel *ch)
235 static void update_packet_state(
struct smd_channel *ch)
255 static unsigned ch_write_buffer(
struct smd_channel *ch,
void **ptr)
257 unsigned head = ch->
send->head;
258 unsigned tail = ch->
send->tail;
262 return tail - head - 1;
274 static void ch_write_done(
struct smd_channel *ch,
unsigned count)
276 BUG_ON(count > smd_stream_write_avail(ch));
281 static void ch_set_state(
struct smd_channel *ch,
unsigned n)
293 ch->
send->fSTATE = 1;
297 static void do_smd_probe(
void)
300 if (shared->
heap_info.free_offset != last_heap_free) {
301 last_heap_free = shared->
heap_info.free_offset;
306 static void smd_state_change(
struct smd_channel *ch,
307 unsigned last,
unsigned next)
311 pr_debug(
"ch %d %d -> %d\n", ch->
n, last, next);
340 if (ch_is_open(ch)) {
341 if (ch->
recv->fHEAD) {
346 if (ch->
recv->fTAIL) {
351 if (ch->
recv->fSTATE) {
352 ch->
recv->fSTATE = 0;
357 tmp = ch->
recv->state;
367 spin_unlock_irqrestore(&smd_lock, flags);
371 static irqreturn_t smd_modem_irq_handler(
int irq,
void *data)
373 handle_smd_irq(&smd_ch_list_modem, notify_modem_smd);
377 #if defined(CONFIG_QDSP6)
378 static irqreturn_t smd_dsp_irq_handler(
int irq,
void *data)
380 handle_smd_irq(&smd_ch_list_dsp, notify_dsp_smd);
385 static void smd_fake_irq_handler(
unsigned long arg)
387 handle_smd_irq(&smd_ch_list_modem, notify_modem_smd);
388 handle_smd_irq(&smd_ch_list_dsp, notify_dsp_smd);
393 static inline int smd_need_int(
struct smd_channel *ch)
395 if (ch_is_open(ch)) {
396 if (ch->
recv->fHEAD || ch->
recv->fTAIL || ch->
recv->fSTATE)
412 if (smd_need_int(ch)) {
418 if (smd_need_int(ch)) {
423 spin_unlock_irqrestore(&
smd_lock, flags);
428 pr_info(
"smd_sleep_exit need interrupt\n");
429 tasklet_schedule(&smd_fake_irq_tasklet);
441 tmp = ch->
recv->state;
451 spin_unlock_irqrestore(&
smd_lock, flags);
454 static int smd_is_packet(
int chn,
unsigned type)
463 if ((chn > 4) || (chn == 1))
469 static int smd_stream_write(
smd_channel_t *ch,
const void *_data,
int len)
472 const unsigned char *
buf = _data;
479 while ((xfer = ch_write_buffer(ch, &ptr)) != 0) {
485 ch_write_done(ch, xfer);
494 return orig_len - len;
497 static int smd_packet_write(
smd_channel_t *ch,
const void *_data,
int len)
508 hdr[1] = hdr[2] = hdr[3] = hdr[4] = 0;
510 smd_stream_write(ch, hdr,
sizeof(hdr));
511 smd_stream_write(ch, _data, len);
516 static int smd_stream_read(
smd_channel_t *ch,
void *data,
int len)
523 r = ch_read(ch, data, len);
530 static int smd_packet_read(
smd_channel_t *ch,
void *data,
int len)
541 r = ch_read(ch, data, len);
547 update_packet_state(ch);
548 spin_unlock_irqrestore(&smd_lock, flags);
559 pr_err(
"smd_alloc_channel() out of memory\n");
564 if (_smd_alloc_channel(ch)) {
577 if (smd_is_packet(cid, type)) {
578 ch->
read = smd_packet_read;
579 ch->
write = smd_packet_write;
584 ch->
read = smd_stream_read;
585 ch->
write = smd_stream_write;
591 if ((type & 0xff) == 0)
600 pr_debug(
"smd_alloc_channel() cid=%02d size=%05d '%s'\n",
604 list_add(&ch->
ch_list, &smd_ch_closed_list);
620 pr_err(
"cannot find allocation table\n");
623 for (n = 0; n < 64; n++) {
624 if (smd_ch_allocated[n])
628 if (!shared[n].name[0])
636 if (!
memcmp(shared[n].name,
"DAL", 3))
642 if (!smd_alloc_channel(shared[n].name, shared[n].cid, ctype))
643 smd_ch_allocated[n] = 1;
647 static void do_nothing_notify(
void *
priv,
unsigned flags)
674 if (smd_initialized == 0) {
675 pr_info(
"smd_open() before smd_init()\n");
684 notify = do_nothing_notify;
714 spin_unlock_irqrestore(&
smd_lock, flags);
728 ch->
notify = do_nothing_notify;
731 spin_unlock_irqrestore(&
smd_lock, flags);
742 return ch->
read(ch, data, len);
747 return ch->
write(ch, data, len);
755 res = ch->
write(ch, data, len);
756 spin_unlock_irqrestore(&
smd_lock, flags);
820 size_in =
ALIGN(size_in, 8);
821 if (size_in != size) {
822 pr_err(
"smem_find(%d, %d): wrong size %d\n",
830 static irqreturn_t smsm_irq_handler(
int irq,
void *data)
841 pr_info(
"<SM %08x %08x>\n", apps, modm);
843 handle_modem_crash();
847 spin_unlock_irqrestore(&smem_lock, flags);
864 handle_modem_crash();
866 state = (
readl(addr) & ~clear_mask) | set_mask;
869 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
870 pr_info(
"smsm_change_state %d %x\n", item, state);
873 spin_unlock_irqrestore(&
smem_lock, flags);
888 handle_modem_crash();
890 spin_unlock_irqrestore(&
smem_lock, flags);
895 #ifdef CONFIG_ARCH_MSM_SCORPION
903 pr_err(
"smsm_set_sleep_duration <SM NO APPS_DEM_SLAVE_DATA>\n");
906 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
907 pr_info(
"smsm_set_sleep_duration %d -> %d\n",
921 pr_err(
"smsm_set_sleep_duration <SM NO SLEEP_DELAY>\n");
924 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
925 pr_info(
"smsm_set_sleep_duration %d -> %d\n",
956 pr_err(
"smd_core_init: enable_irq_wake failed for A9_M2A_0\n");
966 pr_err(
"smd_core_init: enable_irq_wake failed for A9_M2A_5\n");
968 #if defined(CONFIG_QDSP6)
984 #ifdef CONFIG_ARCH_MSM_SCORPION
1000 INIT_WORK(&probe_work, smd_channel_probe_worker);
1003 pr_err(
"smd_core_init() failed\n");
1013 smd_initialized = 1;
1019 .probe = msm_smd_probe,
1026 static int __init msm_smd_init(
void)