[ARM] msm: cleanup smd, separate debugfs support
[deliverable/linux.git] / arch / arm / mach-msm / smd.c
CommitLineData
2eb44eb9
BS
1/* arch/arm/mach-msm/smd.c
2 *
3 * Copyright (C) 2007 Google, Inc.
4 * Author: Brian Swetland <swetland@google.com>
5 *
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.
9 *
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.
14 *
15 */
16
17#include <linux/platform_device.h>
18#include <linux/module.h>
19#include <linux/fs.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>
29#include <linux/io.h>
30
31#include <mach/msm_smd.h>
32#include <mach/msm_iomap.h>
33#include <mach/system.h>
34
35#include "smd_private.h"
36#include "proc_comm.h"
37
38void (*msm_hw_reset_hook)(void);
39
40#define MODULE_NAME "msm_smd"
41
42enum {
43 MSM_SMD_DEBUG = 1U << 0,
44 MSM_SMSM_DEBUG = 1U << 0,
45};
46
47static int msm_smd_debug_mask;
48
03e00cd3 49struct shared_info {
5b0f5a3f 50 int ready;
28379410 51 unsigned state;
5b0f5a3f
BS
52};
53
28379410 54static unsigned dummy_state[SMSM_STATE_COUNT];
5b0f5a3f
BS
55
56static struct shared_info smd_info = {
28379410 57 .state = (unsigned) &dummy_state,
5b0f5a3f
BS
58};
59
2eb44eb9
BS
60module_param_named(debug_mask, msm_smd_debug_mask,
61 int, S_IRUGO | S_IWUSR | S_IWGRP);
62
03e00cd3 63void *smem_item(unsigned id, unsigned *size);
2eb44eb9
BS
64static void smd_diag(void);
65
66static unsigned last_heap_free = 0xffffffff;
67
68#define MSM_A2M_INT(n) (MSM_CSR_BASE + 0x400 + (n) * 4)
69
70static inline void notify_other_smsm(void)
71{
72 writel(1, MSM_A2M_INT(5));
73}
74
5b0f5a3f 75static inline void notify_modem_smd(void)
2eb44eb9
BS
76{
77 writel(1, MSM_A2M_INT(0));
78}
79
5b0f5a3f
BS
80static inline void notify_dsp_smd(void)
81{
82 writel(1, MSM_A2M_INT(8));
83}
84
2eb44eb9
BS
85static void smd_diag(void)
86{
87 char *x;
88
89 x = smem_find(ID_DIAG_ERR_MSG, SZ_DIAG_ERR_MSG);
90 if (x != 0) {
91 x[SZ_DIAG_ERR_MSG - 1] = 0;
92 pr_info("smem: DIAG '%s'\n", x);
93 }
94}
95
96/* call when SMSM_RESET flag is set in the A9's smsm_state */
97static void handle_modem_crash(void)
98{
99 pr_err("ARM9 has CRASHED\n");
100 smd_diag();
101
102 /* hard reboot if possible */
103 if (msm_hw_reset_hook)
104 msm_hw_reset_hook();
105
106 /* in this case the modem or watchdog should reboot us */
107 for (;;)
108 ;
109}
110
111extern int (*msm_check_for_modem_crash)(void);
112
28379410
AH
113uint32_t raw_smsm_get_state(enum smsm_state_item item)
114{
115 return readl(smd_info.state + item * 4);
116}
117
2eb44eb9
BS
118static int check_for_modem_crash(void)
119{
28379410 120 if (raw_smsm_get_state(SMSM_STATE_MODEM) & SMSM_RESET) {
2eb44eb9
BS
121 handle_modem_crash();
122 return -1;
2eb44eb9 123 }
5b0f5a3f 124 return 0;
2eb44eb9
BS
125}
126
2eb44eb9 127/* the spinlock is used to synchronize between the
03e00cd3
BS
128 * irq handler and code that mutates the channel
129 * list or fiddles with channel state
130 */
131DEFINE_SPINLOCK(smd_lock);
132DEFINE_SPINLOCK(smem_lock);
2eb44eb9
BS
133
134/* the mutex is used during open() and close()
03e00cd3
BS
135 * operations to avoid races while creating or
136 * destroying smd_channel structures
137 */
2eb44eb9
BS
138static DEFINE_MUTEX(smd_creation_mutex);
139
140static int smd_initialized;
141
03e00cd3
BS
142LIST_HEAD(smd_ch_closed_list);
143LIST_HEAD(smd_ch_list); /* todo: per-target lists */
2eb44eb9
BS
144
145static unsigned char smd_ch_allocated[64];
146static struct work_struct probe_work;
147
148static void smd_alloc_channel(const char *name, uint32_t cid, uint32_t type);
149
150static void smd_channel_probe_worker(struct work_struct *work)
151{
152 struct smd_alloc_elm *shared;
5b0f5a3f 153 unsigned type;
2eb44eb9
BS
154 unsigned n;
155
156 shared = smem_find(ID_CH_ALLOC_TBL, sizeof(*shared) * 64);
4d4fb266
BS
157 if (!shared) {
158 pr_err("smd: cannot find allocation table\n");
159 return;
160 }
2eb44eb9
BS
161 for (n = 0; n < 64; n++) {
162 if (smd_ch_allocated[n])
163 continue;
164 if (!shared[n].ref_count)
165 continue;
166 if (!shared[n].name[0])
167 continue;
5b0f5a3f
BS
168 type = shared[n].ctype & SMD_TYPE_MASK;
169 if ((type == SMD_TYPE_APPS_MODEM) ||
170 (type == SMD_TYPE_APPS_DSP))
171 smd_alloc_channel(shared[n].name,
172 shared[n].cid,
173 shared[n].ctype);
2eb44eb9
BS
174 smd_ch_allocated[n] = 1;
175 }
176}
177
2eb44eb9
BS
178/* how many bytes are available for reading */
179static int smd_stream_read_avail(struct smd_channel *ch)
180{
5b0f5a3f 181 return (ch->recv->head - ch->recv->tail) & ch->fifo_mask;
2eb44eb9
BS
182}
183
184/* how many bytes we are free to write */
185static int smd_stream_write_avail(struct smd_channel *ch)
186{
5b0f5a3f
BS
187 return ch->fifo_mask -
188 ((ch->send->head - ch->send->tail) & ch->fifo_mask);
2eb44eb9
BS
189}
190
191static int smd_packet_read_avail(struct smd_channel *ch)
192{
193 if (ch->current_packet) {
194 int n = smd_stream_read_avail(ch);
195 if (n > ch->current_packet)
196 n = ch->current_packet;
197 return n;
198 } else {
199 return 0;
200 }
201}
202
203static int smd_packet_write_avail(struct smd_channel *ch)
204{
205 int n = smd_stream_write_avail(ch);
206 return n > SMD_HEADER_SIZE ? n - SMD_HEADER_SIZE : 0;
207}
208
209static int ch_is_open(struct smd_channel *ch)
210{
211 return (ch->recv->state == SMD_SS_OPENED) &&
212 (ch->send->state == SMD_SS_OPENED);
213}
214
215/* provide a pointer and length to readable data in the fifo */
216static unsigned ch_read_buffer(struct smd_channel *ch, void **ptr)
217{
218 unsigned head = ch->recv->head;
219 unsigned tail = ch->recv->tail;
5b0f5a3f 220 *ptr = (void *) (ch->recv_data + tail);
2eb44eb9
BS
221
222 if (tail <= head)
223 return head - tail;
224 else
5b0f5a3f 225 return ch->fifo_size - tail;
2eb44eb9
BS
226}
227
228/* advance the fifo read pointer after data from ch_read_buffer is consumed */
229static void ch_read_done(struct smd_channel *ch, unsigned count)
230{
231 BUG_ON(count > smd_stream_read_avail(ch));
5b0f5a3f 232 ch->recv->tail = (ch->recv->tail + count) & ch->fifo_mask;
2eb44eb9
BS
233 ch->recv->fTAIL = 1;
234}
235
236/* basic read interface to ch_read_{buffer,done} used
03e00cd3
BS
237 * by smd_*_read() and update_packet_state()
238 * will read-and-discard if the _data pointer is null
239 */
2eb44eb9
BS
240static int ch_read(struct smd_channel *ch, void *_data, int len)
241{
242 void *ptr;
243 unsigned n;
244 unsigned char *data = _data;
245 int orig_len = len;
246
247 while (len > 0) {
248 n = ch_read_buffer(ch, &ptr);
249 if (n == 0)
250 break;
251
252 if (n > len)
253 n = len;
254 if (_data)
255 memcpy(data, ptr, n);
256
257 data += n;
258 len -= n;
259 ch_read_done(ch, n);
260 }
261
262 return orig_len - len;
263}
264
265static void update_stream_state(struct smd_channel *ch)
266{
267 /* streams have no special state requiring updating */
268}
269
270static void update_packet_state(struct smd_channel *ch)
271{
272 unsigned hdr[5];
273 int r;
274
275 /* can't do anything if we're in the middle of a packet */
276 if (ch->current_packet != 0)
277 return;
278
279 /* don't bother unless we can get the full header */
280 if (smd_stream_read_avail(ch) < SMD_HEADER_SIZE)
281 return;
282
283 r = ch_read(ch, hdr, SMD_HEADER_SIZE);
284 BUG_ON(r != SMD_HEADER_SIZE);
285
286 ch->current_packet = hdr[0];
287}
288
289/* provide a pointer and length to next free space in the fifo */
290static unsigned ch_write_buffer(struct smd_channel *ch, void **ptr)
291{
292 unsigned head = ch->send->head;
293 unsigned tail = ch->send->tail;
5b0f5a3f 294 *ptr = (void *) (ch->send_data + head);
2eb44eb9
BS
295
296 if (head < tail) {
297 return tail - head - 1;
298 } else {
299 if (tail == 0)
5b0f5a3f 300 return ch->fifo_size - head - 1;
2eb44eb9 301 else
5b0f5a3f 302 return ch->fifo_size - head;
2eb44eb9
BS
303 }
304}
305
306/* advace the fifo write pointer after freespace
307 * from ch_write_buffer is filled
308 */
309static void ch_write_done(struct smd_channel *ch, unsigned count)
310{
311 BUG_ON(count > smd_stream_write_avail(ch));
5b0f5a3f 312 ch->send->head = (ch->send->head + count) & ch->fifo_mask;
2eb44eb9
BS
313 ch->send->fHEAD = 1;
314}
315
5b0f5a3f 316static void ch_set_state(struct smd_channel *ch, unsigned n)
2eb44eb9
BS
317{
318 if (n == SMD_SS_OPENED) {
5b0f5a3f
BS
319 ch->send->fDSR = 1;
320 ch->send->fCTS = 1;
321 ch->send->fCD = 1;
2eb44eb9 322 } else {
5b0f5a3f
BS
323 ch->send->fDSR = 0;
324 ch->send->fCTS = 0;
325 ch->send->fCD = 0;
2eb44eb9 326 }
5b0f5a3f
BS
327 ch->send->state = n;
328 ch->send->fSTATE = 1;
329 ch->notify_other_cpu();
2eb44eb9
BS
330}
331
332static void do_smd_probe(void)
333{
334 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
335 if (shared->heap_info.free_offset != last_heap_free) {
336 last_heap_free = shared->heap_info.free_offset;
337 schedule_work(&probe_work);
338 }
339}
340
341static void smd_state_change(struct smd_channel *ch,
342 unsigned last, unsigned next)
343{
344 ch->last_state = next;
345
03e00cd3 346 pr_info("SMD: ch %d %d -> %d\n", ch->n, last, next);
2eb44eb9
BS
347
348 switch (next) {
349 case SMD_SS_OPENING:
350 ch->recv->tail = 0;
351 case SMD_SS_OPENED:
352 if (ch->send->state != SMD_SS_OPENED)
5b0f5a3f 353 ch_set_state(ch, SMD_SS_OPENED);
2eb44eb9
BS
354 ch->notify(ch->priv, SMD_EVENT_OPEN);
355 break;
356 case SMD_SS_FLUSHING:
357 case SMD_SS_RESET:
358 /* we should force them to close? */
359 default:
360 ch->notify(ch->priv, SMD_EVENT_CLOSE);
361 }
362}
363
5b0f5a3f 364static void handle_smd_irq(struct list_head *list, void (*notify)(void))
2eb44eb9
BS
365{
366 unsigned long flags;
367 struct smd_channel *ch;
368 int do_notify = 0;
369 unsigned ch_flags;
370 unsigned tmp;
371
372 spin_lock_irqsave(&smd_lock, flags);
5b0f5a3f 373 list_for_each_entry(ch, list, ch_list) {
2eb44eb9
BS
374 ch_flags = 0;
375 if (ch_is_open(ch)) {
376 if (ch->recv->fHEAD) {
377 ch->recv->fHEAD = 0;
378 ch_flags |= 1;
379 do_notify |= 1;
380 }
381 if (ch->recv->fTAIL) {
382 ch->recv->fTAIL = 0;
383 ch_flags |= 2;
384 do_notify |= 1;
385 }
386 if (ch->recv->fSTATE) {
387 ch->recv->fSTATE = 0;
388 ch_flags |= 4;
389 do_notify |= 1;
390 }
391 }
392 tmp = ch->recv->state;
393 if (tmp != ch->last_state)
394 smd_state_change(ch, ch->last_state, tmp);
395 if (ch_flags) {
396 ch->update_state(ch);
397 ch->notify(ch->priv, SMD_EVENT_DATA);
398 }
399 }
400 if (do_notify)
5b0f5a3f 401 notify();
2eb44eb9
BS
402 spin_unlock_irqrestore(&smd_lock, flags);
403 do_smd_probe();
5b0f5a3f
BS
404}
405
406static irqreturn_t smd_irq_handler(int irq, void *data)
407{
408 handle_smd_irq(&smd_ch_list, notify_modem_smd);
2eb44eb9
BS
409 return IRQ_HANDLED;
410}
411
412static void smd_fake_irq_handler(unsigned long arg)
413{
414 smd_irq_handler(0, NULL);
415}
416
417static DECLARE_TASKLET(smd_fake_irq_tasklet, smd_fake_irq_handler, 0);
418
419void smd_sleep_exit(void)
420{
421 unsigned long flags;
422 struct smd_channel *ch;
423 unsigned tmp;
424 int need_int = 0;
425
426 spin_lock_irqsave(&smd_lock, flags);
427 list_for_each_entry(ch, &smd_ch_list, ch_list) {
428 if (ch_is_open(ch)) {
429 if (ch->recv->fHEAD) {
430 if (msm_smd_debug_mask & MSM_SMD_DEBUG)
431 pr_info("smd_sleep_exit ch %d fHEAD "
432 "%x %x %x\n",
433 ch->n, ch->recv->fHEAD,
434 ch->recv->head, ch->recv->tail);
435 need_int = 1;
436 break;
437 }
438 if (ch->recv->fTAIL) {
439 if (msm_smd_debug_mask & MSM_SMD_DEBUG)
440 pr_info("smd_sleep_exit ch %d fTAIL "
441 "%x %x %x\n",
442 ch->n, ch->recv->fTAIL,
443 ch->send->head, ch->send->tail);
444 need_int = 1;
445 break;
446 }
447 if (ch->recv->fSTATE) {
448 if (msm_smd_debug_mask & MSM_SMD_DEBUG)
449 pr_info("smd_sleep_exit ch %d fSTATE %x"
450 "\n", ch->n, ch->recv->fSTATE);
451 need_int = 1;
452 break;
453 }
454 tmp = ch->recv->state;
455 if (tmp != ch->last_state) {
456 if (msm_smd_debug_mask & MSM_SMD_DEBUG)
457 pr_info("smd_sleep_exit ch %d "
458 "state %x != %x\n",
459 ch->n, tmp, ch->last_state);
460 need_int = 1;
461 break;
462 }
463 }
464 }
465 spin_unlock_irqrestore(&smd_lock, flags);
466 do_smd_probe();
467 if (need_int) {
468 if (msm_smd_debug_mask & MSM_SMD_DEBUG)
469 pr_info("smd_sleep_exit need interrupt\n");
470 tasklet_schedule(&smd_fake_irq_tasklet);
471 }
472}
473
474
475void smd_kick(smd_channel_t *ch)
476{
477 unsigned long flags;
478 unsigned tmp;
479
480 spin_lock_irqsave(&smd_lock, flags);
481 ch->update_state(ch);
482 tmp = ch->recv->state;
483 if (tmp != ch->last_state) {
484 ch->last_state = tmp;
485 if (tmp == SMD_SS_OPENED)
486 ch->notify(ch->priv, SMD_EVENT_OPEN);
487 else
488 ch->notify(ch->priv, SMD_EVENT_CLOSE);
489 }
490 ch->notify(ch->priv, SMD_EVENT_DATA);
5b0f5a3f 491 ch->notify_other_cpu();
2eb44eb9
BS
492 spin_unlock_irqrestore(&smd_lock, flags);
493}
494
5b0f5a3f 495static int smd_is_packet(int chn, unsigned type)
2eb44eb9 496{
5b0f5a3f
BS
497 type &= SMD_KIND_MASK;
498 if (type == SMD_KIND_PACKET)
499 return 1;
500 if (type == SMD_KIND_STREAM)
501 return 0;
502
503 /* older AMSS reports SMD_KIND_UNKNOWN always */
2eb44eb9
BS
504 if ((chn > 4) || (chn == 1))
505 return 1;
506 else
507 return 0;
508}
509
510static int smd_stream_write(smd_channel_t *ch, const void *_data, int len)
511{
512 void *ptr;
513 const unsigned char *buf = _data;
514 unsigned xfer;
515 int orig_len = len;
516
517 if (len < 0)
518 return -EINVAL;
519
520 while ((xfer = ch_write_buffer(ch, &ptr)) != 0) {
521 if (!ch_is_open(ch))
522 break;
523 if (xfer > len)
524 xfer = len;
525 memcpy(ptr, buf, xfer);
526 ch_write_done(ch, xfer);
527 len -= xfer;
528 buf += xfer;
529 if (len == 0)
530 break;
531 }
532
5b0f5a3f 533 ch->notify_other_cpu();
2eb44eb9
BS
534
535 return orig_len - len;
536}
537
538static int smd_packet_write(smd_channel_t *ch, const void *_data, int len)
539{
540 unsigned hdr[5];
541
542 if (len < 0)
543 return -EINVAL;
544
545 if (smd_stream_write_avail(ch) < (len + SMD_HEADER_SIZE))
546 return -ENOMEM;
547
548 hdr[0] = len;
549 hdr[1] = hdr[2] = hdr[3] = hdr[4] = 0;
550
551 smd_stream_write(ch, hdr, sizeof(hdr));
552 smd_stream_write(ch, _data, len);
553
554 return len;
555}
556
557static int smd_stream_read(smd_channel_t *ch, void *data, int len)
558{
559 int r;
560
561 if (len < 0)
562 return -EINVAL;
563
564 r = ch_read(ch, data, len);
565 if (r > 0)
5b0f5a3f 566 ch->notify_other_cpu();
2eb44eb9
BS
567
568 return r;
569}
570
571static int smd_packet_read(smd_channel_t *ch, void *data, int len)
572{
573 unsigned long flags;
574 int r;
575
576 if (len < 0)
577 return -EINVAL;
578
579 if (len > ch->current_packet)
580 len = ch->current_packet;
581
582 r = ch_read(ch, data, len);
583 if (r > 0)
5b0f5a3f 584 ch->notify_other_cpu();
2eb44eb9
BS
585
586 spin_lock_irqsave(&smd_lock, flags);
587 ch->current_packet -= r;
588 update_packet_state(ch);
589 spin_unlock_irqrestore(&smd_lock, flags);
590
591 return r;
592}
593
5b0f5a3f 594static int smd_alloc_v2(struct smd_channel *ch)
2eb44eb9 595{
5b0f5a3f
BS
596 struct smd_shared_v2 *shared2;
597 void *buffer;
598 unsigned buffer_sz;
2eb44eb9 599
5b0f5a3f
BS
600 shared2 = smem_alloc(SMEM_SMD_BASE_ID + ch->n, sizeof(*shared2));
601 buffer = smem_item(SMEM_SMD_FIFO_BASE_ID + ch->n, &buffer_sz);
602
603 if (!buffer)
604 return -1;
605
606 /* buffer must be a power-of-two size */
607 if (buffer_sz & (buffer_sz - 1))
608 return -1;
609
610 buffer_sz /= 2;
611 ch->send = &shared2->ch0;
612 ch->recv = &shared2->ch1;
613 ch->send_data = buffer;
614 ch->recv_data = buffer + buffer_sz;
615 ch->fifo_size = buffer_sz;
616 return 0;
617}
618
619static int smd_alloc_v1(struct smd_channel *ch)
620{
621 struct smd_shared_v1 *shared1;
622 shared1 = smem_alloc(ID_SMD_CHANNELS + ch->n, sizeof(*shared1));
623 if (!shared1) {
624 pr_err("smd_alloc_channel() cid %d does not exist\n", ch->n);
625 return -1;
2eb44eb9 626 }
5b0f5a3f
BS
627 ch->send = &shared1->ch0;
628 ch->recv = &shared1->ch1;
629 ch->send_data = shared1->data0;
630 ch->recv_data = shared1->data1;
631 ch->fifo_size = SMD_BUF_SIZE;
632 return 0;
633}
634
635
636static void smd_alloc_channel(const char *name, uint32_t cid, uint32_t type)
637{
638 struct smd_channel *ch;
2eb44eb9
BS
639
640 ch = kzalloc(sizeof(struct smd_channel), GFP_KERNEL);
641 if (ch == 0) {
642 pr_err("smd_alloc_channel() out of memory\n");
643 return;
644 }
2eb44eb9
BS
645 ch->n = cid;
646
5b0f5a3f
BS
647 if (smd_alloc_v2(ch) && smd_alloc_v1(ch)) {
648 kfree(ch);
649 return;
650 }
651
652 ch->fifo_mask = ch->fifo_size - 1;
653 ch->type = type;
654
655 if ((type & SMD_TYPE_MASK) == SMD_TYPE_APPS_MODEM)
656 ch->notify_other_cpu = notify_modem_smd;
657 else
658 ch->notify_other_cpu = notify_dsp_smd;
659
660 if (smd_is_packet(cid, type)) {
2eb44eb9
BS
661 ch->read = smd_packet_read;
662 ch->write = smd_packet_write;
663 ch->read_avail = smd_packet_read_avail;
664 ch->write_avail = smd_packet_write_avail;
665 ch->update_state = update_packet_state;
666 } else {
667 ch->read = smd_stream_read;
668 ch->write = smd_stream_write;
669 ch->read_avail = smd_stream_read_avail;
670 ch->write_avail = smd_stream_write_avail;
671 ch->update_state = update_stream_state;
672 }
673
5b0f5a3f
BS
674 if ((type & 0xff) == 0)
675 memcpy(ch->name, "SMD_", 4);
676 else
677 memcpy(ch->name, "DSP_", 4);
2eb44eb9
BS
678 memcpy(ch->name + 4, name, 20);
679 ch->name[23] = 0;
680 ch->pdev.name = ch->name;
681 ch->pdev.id = -1;
682
5b0f5a3f
BS
683 pr_info("smd_alloc_channel() cid=%02d size=%05d '%s'\n",
684 ch->n, ch->fifo_size, ch->name);
2eb44eb9
BS
685
686 mutex_lock(&smd_creation_mutex);
687 list_add(&ch->ch_list, &smd_ch_closed_list);
688 mutex_unlock(&smd_creation_mutex);
689
690 platform_device_register(&ch->pdev);
691}
692
693static void do_nothing_notify(void *priv, unsigned flags)
694{
695}
696
697struct smd_channel *smd_get_channel(const char *name)
698{
699 struct smd_channel *ch;
700
701 mutex_lock(&smd_creation_mutex);
702 list_for_each_entry(ch, &smd_ch_closed_list, ch_list) {
703 if (!strcmp(name, ch->name)) {
704 list_del(&ch->ch_list);
705 mutex_unlock(&smd_creation_mutex);
706 return ch;
707 }
708 }
709 mutex_unlock(&smd_creation_mutex);
710
711 return NULL;
712}
713
714int smd_open(const char *name, smd_channel_t **_ch,
715 void *priv, void (*notify)(void *, unsigned))
716{
717 struct smd_channel *ch;
718 unsigned long flags;
719
720 if (smd_initialized == 0) {
721 pr_info("smd_open() before smd_init()\n");
722 return -ENODEV;
723 }
724
725 ch = smd_get_channel(name);
726 if (!ch)
727 return -ENODEV;
728
729 if (notify == 0)
730 notify = do_nothing_notify;
731
732 ch->notify = notify;
733 ch->current_packet = 0;
734 ch->last_state = SMD_SS_CLOSED;
735 ch->priv = priv;
736
737 *_ch = ch;
738
739 spin_lock_irqsave(&smd_lock, flags);
740 list_add(&ch->ch_list, &smd_ch_list);
741
742 /* If the remote side is CLOSING, we need to get it to
743 * move to OPENING (which we'll do by moving from CLOSED to
744 * OPENING) and then get it to move from OPENING to
745 * OPENED (by doing the same state change ourselves).
746 *
747 * Otherwise, it should be OPENING and we can move directly
748 * to OPENED so that it will follow.
749 */
750 if (ch->recv->state == SMD_SS_CLOSING) {
751 ch->send->head = 0;
5b0f5a3f 752 ch_set_state(ch, SMD_SS_OPENING);
2eb44eb9 753 } else {
5b0f5a3f 754 ch_set_state(ch, SMD_SS_OPENED);
2eb44eb9
BS
755 }
756 spin_unlock_irqrestore(&smd_lock, flags);
757 smd_kick(ch);
758
759 return 0;
760}
761
762int smd_close(smd_channel_t *ch)
763{
764 unsigned long flags;
765
766 pr_info("smd_close(%p)\n", ch);
767
768 if (ch == 0)
769 return -1;
770
771 spin_lock_irqsave(&smd_lock, flags);
772 ch->notify = do_nothing_notify;
773 list_del(&ch->ch_list);
5b0f5a3f 774 ch_set_state(ch, SMD_SS_CLOSED);
2eb44eb9
BS
775 spin_unlock_irqrestore(&smd_lock, flags);
776
777 mutex_lock(&smd_creation_mutex);
778 list_add(&ch->ch_list, &smd_ch_closed_list);
779 mutex_unlock(&smd_creation_mutex);
780
781 return 0;
782}
783
784int smd_read(smd_channel_t *ch, void *data, int len)
785{
786 return ch->read(ch, data, len);
787}
788
789int smd_write(smd_channel_t *ch, const void *data, int len)
790{
791 return ch->write(ch, data, len);
792}
793
794int smd_read_avail(smd_channel_t *ch)
795{
796 return ch->read_avail(ch);
797}
798
799int smd_write_avail(smd_channel_t *ch)
800{
801 return ch->write_avail(ch);
802}
803
804int smd_wait_until_readable(smd_channel_t *ch, int bytes)
805{
806 return -1;
807}
808
809int smd_wait_until_writable(smd_channel_t *ch, int bytes)
810{
811 return -1;
812}
813
814int smd_cur_packet_size(smd_channel_t *ch)
815{
816 return ch->current_packet;
817}
818
819
820/* ------------------------------------------------------------------------- */
821
822void *smem_alloc(unsigned id, unsigned size)
823{
824 return smem_find(id, size);
825}
826
03e00cd3 827void *smem_item(unsigned id, unsigned *size)
2eb44eb9
BS
828{
829 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
830 struct smem_heap_entry *toc = shared->heap_toc;
831
832 if (id >= SMEM_NUM_ITEMS)
833 return 0;
834
835 if (toc[id].allocated) {
836 *size = toc[id].size;
837 return (void *) (MSM_SHARED_RAM_BASE + toc[id].offset);
5b0f5a3f
BS
838 } else {
839 *size = 0;
2eb44eb9
BS
840 }
841
842 return 0;
843}
844
845void *smem_find(unsigned id, unsigned size_in)
846{
847 unsigned size;
848 void *ptr;
849
5b0f5a3f 850 ptr = smem_item(id, &size);
2eb44eb9
BS
851 if (!ptr)
852 return 0;
853
854 size_in = ALIGN(size_in, 8);
855 if (size_in != size) {
856 pr_err("smem_find(%d, %d): wrong size %d\n",
857 id, size_in, size);
858 return 0;
859 }
860
861 return ptr;
862}
863
864static irqreturn_t smsm_irq_handler(int irq, void *data)
865{
866 unsigned long flags;
5b0f5a3f 867 unsigned apps, modm;
2eb44eb9
BS
868
869 spin_lock_irqsave(&smem_lock, flags);
2eb44eb9 870
28379410
AH
871 apps = raw_smsm_get_state(SMSM_STATE_APPS);
872 modm = raw_smsm_get_state(SMSM_STATE_MODEM);
2eb44eb9 873
5b0f5a3f
BS
874 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
875 pr_info("<SM %08x %08x>\n", apps, modm);
876 if (modm & SMSM_RESET) {
877 handle_modem_crash();
2eb44eb9 878 }
5b0f5a3f
BS
879 do_smd_probe();
880
2eb44eb9
BS
881 spin_unlock_irqrestore(&smem_lock, flags);
882 return IRQ_HANDLED;
883}
884
28379410
AH
885int smsm_change_state(enum smsm_state_item item,
886 uint32_t clear_mask, uint32_t set_mask)
2eb44eb9
BS
887{
888 unsigned long flags;
5b0f5a3f 889 unsigned state;
28379410 890 unsigned addr = smd_info.state + item * 4;
5b0f5a3f
BS
891
892 if (!smd_info.ready)
893 return -EIO;
2eb44eb9
BS
894
895 spin_lock_irqsave(&smem_lock, flags);
896
28379410 897 if (raw_smsm_get_state(SMSM_STATE_MODEM) & SMSM_RESET)
5b0f5a3f
BS
898 handle_modem_crash();
899
28379410
AH
900 state = (readl(addr) & ~clear_mask) | set_mask;
901 writel(state, addr);
5b0f5a3f
BS
902
903 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
28379410 904 pr_info("smsm_change_state %d %x\n", item, state);
5b0f5a3f 905 notify_other_smsm();
2eb44eb9
BS
906
907 spin_unlock_irqrestore(&smem_lock, flags);
908
2eb44eb9
BS
909 return 0;
910}
911
28379410 912uint32_t smsm_get_state(enum smsm_state_item item)
2eb44eb9
BS
913{
914 unsigned long flags;
2eb44eb9
BS
915 uint32_t rv;
916
917 spin_lock_irqsave(&smem_lock, flags);
918
28379410 919 rv = readl(smd_info.state + item * 4);
2eb44eb9 920
28379410 921 if (item == SMSM_STATE_MODEM && (rv & SMSM_RESET))
2eb44eb9
BS
922 handle_modem_crash();
923
924 spin_unlock_irqrestore(&smem_lock, flags);
925
2eb44eb9
BS
926 return rv;
927}
928
ec9d3d14
AH
929#ifdef CONFIG_ARCH_MSM_SCORPION
930
2eb44eb9
BS
931int smsm_set_sleep_duration(uint32_t delay)
932{
03e00cd3
BS
933 struct msm_dem_slave_data *ptr;
934
935 ptr = smem_find(SMEM_APPS_DEM_SLAVE_DATA, sizeof(*ptr));
2eb44eb9 936 if (ptr == NULL) {
ec9d3d14 937 pr_err("smsm_set_sleep_duration <SM NO APPS_DEM_SLAVE_DATA>\n");
2eb44eb9
BS
938 return -EIO;
939 }
940 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
941 pr_info("smsm_set_sleep_duration %d -> %d\n",
ec9d3d14
AH
942 ptr->sleep_time, delay);
943 ptr->sleep_time = delay;
2eb44eb9
BS
944 return 0;
945}
946
ec9d3d14
AH
947#else
948
949int smsm_set_sleep_duration(uint32_t delay)
2eb44eb9 950{
ec9d3d14 951 uint32_t *ptr;
2eb44eb9 952
03e00cd3 953 ptr = smem_find(SMEM_SMSM_SLEEP_DELAY, sizeof(*ptr));
2eb44eb9 954 if (ptr == NULL) {
ec9d3d14 955 pr_err("smsm_set_sleep_duration <SM NO SLEEP_DELAY>\n");
2eb44eb9
BS
956 return -EIO;
957 }
958 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
ec9d3d14
AH
959 pr_info("smsm_set_sleep_duration %d -> %d\n",
960 *ptr, delay);
961 *ptr = delay;
2eb44eb9
BS
962 return 0;
963}
964
ec9d3d14
AH
965#endif
966
2eb44eb9
BS
967int smd_core_init(void)
968{
969 int r;
970 pr_info("smd_core_init()\n");
971
5b0f5a3f
BS
972 /* wait for essential items to be initialized */
973 for (;;) {
974 unsigned size;
975 void *state;
976 state = smem_item(SMEM_SMSM_SHARED_STATE, &size);
28379410
AH
977 if (size == SMSM_V1_SIZE || size == SMSM_V2_SIZE) {
978 smd_info.state = (unsigned)state;
5b0f5a3f
BS
979 break;
980 }
981 }
982
983 smd_info.ready = 1;
984
2eb44eb9
BS
985 r = request_irq(INT_A9_M2A_0, smd_irq_handler,
986 IRQF_TRIGGER_RISING, "smd_dev", 0);
987 if (r < 0)
988 return r;
989 r = enable_irq_wake(INT_A9_M2A_0);
990 if (r < 0)
991 pr_err("smd_core_init: enable_irq_wake failed for A9_M2A_0\n");
992
993 r = request_irq(INT_A9_M2A_5, smsm_irq_handler,
994 IRQF_TRIGGER_RISING, "smsm_dev", 0);
995 if (r < 0) {
996 free_irq(INT_A9_M2A_0, 0);
997 return r;
998 }
999 r = enable_irq_wake(INT_A9_M2A_5);
1000 if (r < 0)
1001 pr_err("smd_core_init: enable_irq_wake failed for A9_M2A_5\n");
1002
5b0f5a3f
BS
1003 /* check for any SMD channels that may already exist */
1004 do_smd_probe();
1005
1006 /* indicate that we're up and running */
28379410 1007 smsm_change_state(SMSM_STATE_APPS,
ec9d3d14
AH
1008 ~0, SMSM_INIT | SMSM_SMDINIT | SMSM_RPCINIT | SMSM_RUN);
1009#ifdef CONFIG_ARCH_MSM_SCORPION
1010 smsm_change_state(SMSM_STATE_APPS_DEM, ~0, 0);
1011#endif
2eb44eb9
BS
1012
1013 pr_info("smd_core_init() done\n");
1014
1015 return 0;
1016}
1017
2eb44eb9
BS
1018static int __init msm_smd_probe(struct platform_device *pdev)
1019{
1020 pr_info("smd_init()\n");
1021
1022 INIT_WORK(&probe_work, smd_channel_probe_worker);
1023
1024 if (smd_core_init()) {
1025 pr_err("smd_core_init() failed\n");
1026 return -1;
1027 }
1028
1029 do_smd_probe();
1030
1031 msm_check_for_modem_crash = check_for_modem_crash;
1032
2eb44eb9
BS
1033 smd_initialized = 1;
1034
1035 return 0;
1036}
1037
1038static struct platform_driver msm_smd_driver = {
1039 .probe = msm_smd_probe,
1040 .driver = {
1041 .name = MODULE_NAME,
1042 .owner = THIS_MODULE,
1043 },
1044};
1045
1046static int __init msm_smd_init(void)
1047{
1048 return platform_driver_register(&msm_smd_driver);
1049}
1050
1051module_init(msm_smd_init);
1052
1053MODULE_DESCRIPTION("MSM Shared Memory Core");
1054MODULE_AUTHOR("Brian Swetland <swetland@google.com>");
1055MODULE_LICENSE("GPL");
This page took 0.095394 seconds and 5 git commands to generate.