blob: e33fd029fa37288719ea7dbebdaef7d0da2428a9 [file] [log] [blame]
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001/* 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
Brian Swetland5b0f5a32009-04-26 18:38:49 -070049struct shared_info
50{
51 int ready;
Arve Hjønnevåg28379412009-05-20 16:52:36 -070052 unsigned state;
Brian Swetland5b0f5a32009-04-26 18:38:49 -070053};
54
Arve Hjønnevåg28379412009-05-20 16:52:36 -070055static unsigned dummy_state[SMSM_STATE_COUNT];
Brian Swetland5b0f5a32009-04-26 18:38:49 -070056
57static struct shared_info smd_info = {
Arve Hjønnevåg28379412009-05-20 16:52:36 -070058 .state = (unsigned) &dummy_state,
Brian Swetland5b0f5a32009-04-26 18:38:49 -070059};
60
Brian Swetland2eb44eb2008-09-29 16:00:48 -070061module_param_named(debug_mask, msm_smd_debug_mask,
62 int, S_IRUGO | S_IWUSR | S_IWGRP);
63
64void *smem_find(unsigned id, unsigned size);
Brian Swetland5b0f5a32009-04-26 18:38:49 -070065static void *smem_item(unsigned id, unsigned *size);
Brian Swetland2eb44eb2008-09-29 16:00:48 -070066static void smd_diag(void);
67
68static unsigned last_heap_free = 0xffffffff;
69
70#define MSM_A2M_INT(n) (MSM_CSR_BASE + 0x400 + (n) * 4)
71
72static inline void notify_other_smsm(void)
73{
74 writel(1, MSM_A2M_INT(5));
75}
76
Brian Swetland5b0f5a32009-04-26 18:38:49 -070077static inline void notify_modem_smd(void)
Brian Swetland2eb44eb2008-09-29 16:00:48 -070078{
79 writel(1, MSM_A2M_INT(0));
80}
81
Brian Swetland5b0f5a32009-04-26 18:38:49 -070082static inline void notify_dsp_smd(void)
83{
84 writel(1, MSM_A2M_INT(8));
85}
86
Brian Swetland2eb44eb2008-09-29 16:00:48 -070087static void smd_diag(void)
88{
89 char *x;
90
91 x = smem_find(ID_DIAG_ERR_MSG, SZ_DIAG_ERR_MSG);
92 if (x != 0) {
93 x[SZ_DIAG_ERR_MSG - 1] = 0;
94 pr_info("smem: DIAG '%s'\n", x);
95 }
96}
97
98/* call when SMSM_RESET flag is set in the A9's smsm_state */
99static void handle_modem_crash(void)
100{
101 pr_err("ARM9 has CRASHED\n");
102 smd_diag();
103
104 /* hard reboot if possible */
105 if (msm_hw_reset_hook)
106 msm_hw_reset_hook();
107
108 /* in this case the modem or watchdog should reboot us */
109 for (;;)
110 ;
111}
112
113extern int (*msm_check_for_modem_crash)(void);
114
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700115uint32_t raw_smsm_get_state(enum smsm_state_item item)
116{
117 return readl(smd_info.state + item * 4);
118}
119
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700120static int check_for_modem_crash(void)
121{
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700122 if (raw_smsm_get_state(SMSM_STATE_MODEM) & SMSM_RESET) {
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700123 handle_modem_crash();
124 return -1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700125 }
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700126 return 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700127}
128
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700129#define SMD_SS_CLOSED 0x00000000
130#define SMD_SS_OPENING 0x00000001
131#define SMD_SS_OPENED 0x00000002
132#define SMD_SS_FLUSHING 0x00000003
133#define SMD_SS_CLOSING 0x00000004
134#define SMD_SS_RESET 0x00000005
135#define SMD_SS_RESET_OPENING 0x00000006
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700136
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700137#define SMD_BUF_SIZE 8192
138#define SMD_CHANNELS 64
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700139
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700140#define SMD_HEADER_SIZE 20
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700141
142
143/* the spinlock is used to synchronize between the
144** irq handler and code that mutates the channel
145** list or fiddles with channel state
146*/
147static DEFINE_SPINLOCK(smd_lock);
148static DEFINE_SPINLOCK(smem_lock);
149
150/* the mutex is used during open() and close()
151** operations to avoid races while creating or
152** destroying smd_channel structures
153*/
154static DEFINE_MUTEX(smd_creation_mutex);
155
156static int smd_initialized;
157
158struct smd_alloc_elm {
159 char name[20];
160 uint32_t cid;
161 uint32_t ctype;
162 uint32_t ref_count;
163};
164
165struct smd_half_channel {
166 unsigned state;
167 unsigned char fDSR;
168 unsigned char fCTS;
169 unsigned char fCD;
170 unsigned char fRI;
171 unsigned char fHEAD;
172 unsigned char fTAIL;
173 unsigned char fSTATE;
174 unsigned char fUNUSED;
175 unsigned tail;
176 unsigned head;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700177} __attribute__((packed));
178
179struct smd_shared_v1 {
180 struct smd_half_channel ch0;
181 unsigned char data0[SMD_BUF_SIZE];
182 struct smd_half_channel ch1;
183 unsigned char data1[SMD_BUF_SIZE];
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700184};
185
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700186struct smd_shared_v2 {
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700187 struct smd_half_channel ch0;
188 struct smd_half_channel ch1;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700189};
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700190
191struct smd_channel {
192 volatile struct smd_half_channel *send;
193 volatile struct smd_half_channel *recv;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700194 unsigned char *send_data;
195 unsigned char *recv_data;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700196
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700197 unsigned fifo_mask;
198 unsigned fifo_size;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700199 unsigned current_packet;
200 unsigned n;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700201
202 struct list_head ch_list;
203
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700204 void *priv;
205 void (*notify)(void *priv, unsigned flags);
206
207 int (*read)(smd_channel_t *ch, void *data, int len);
208 int (*write)(smd_channel_t *ch, const void *data, int len);
209 int (*read_avail)(smd_channel_t *ch);
210 int (*write_avail)(smd_channel_t *ch);
211
212 void (*update_state)(smd_channel_t *ch);
213 unsigned last_state;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700214 void (*notify_other_cpu)(void);
215 unsigned type;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700216
217 char name[32];
218 struct platform_device pdev;
219};
220
221static LIST_HEAD(smd_ch_closed_list);
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700222static LIST_HEAD(smd_ch_list); /* todo: per-target lists */
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700223
224static unsigned char smd_ch_allocated[64];
225static struct work_struct probe_work;
226
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700227#define SMD_TYPE_MASK 0x0FF
228#define SMD_TYPE_APPS_MODEM 0x000
229#define SMD_TYPE_APPS_DSP 0x001
230#define SMD_TYPE_MODEM_DSP 0x002
231
232#define SMD_KIND_MASK 0xF00
233#define SMD_KIND_UNKNOWN 0x000
234#define SMD_KIND_STREAM 0x100
235#define SMD_KIND_PACKET 0x200
236
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700237static void smd_alloc_channel(const char *name, uint32_t cid, uint32_t type);
238
239static void smd_channel_probe_worker(struct work_struct *work)
240{
241 struct smd_alloc_elm *shared;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700242 unsigned type;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700243 unsigned n;
244
245 shared = smem_find(ID_CH_ALLOC_TBL, sizeof(*shared) * 64);
Brian Swetland4d4fb262009-01-28 20:25:40 -0800246 if (!shared) {
247 pr_err("smd: cannot find allocation table\n");
248 return;
249 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700250 for (n = 0; n < 64; n++) {
251 if (smd_ch_allocated[n])
252 continue;
253 if (!shared[n].ref_count)
254 continue;
255 if (!shared[n].name[0])
256 continue;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700257 type = shared[n].ctype & SMD_TYPE_MASK;
258 if ((type == SMD_TYPE_APPS_MODEM) ||
259 (type == SMD_TYPE_APPS_DSP))
260 smd_alloc_channel(shared[n].name,
261 shared[n].cid,
262 shared[n].ctype);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700263 smd_ch_allocated[n] = 1;
264 }
265}
266
267static char *chstate(unsigned n)
268{
269 switch (n) {
270 case SMD_SS_CLOSED:
271 return "CLOSED";
272 case SMD_SS_OPENING:
273 return "OPENING";
274 case SMD_SS_OPENED:
275 return "OPENED";
276 case SMD_SS_FLUSHING:
277 return "FLUSHING";
278 case SMD_SS_CLOSING:
279 return "CLOSING";
280 case SMD_SS_RESET:
281 return "RESET";
282 case SMD_SS_RESET_OPENING:
283 return "ROPENING";
284 default:
285 return "UNKNOWN";
286 }
287}
288
289/* how many bytes are available for reading */
290static int smd_stream_read_avail(struct smd_channel *ch)
291{
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700292 return (ch->recv->head - ch->recv->tail) & ch->fifo_mask;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700293}
294
295/* how many bytes we are free to write */
296static int smd_stream_write_avail(struct smd_channel *ch)
297{
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700298 return ch->fifo_mask -
299 ((ch->send->head - ch->send->tail) & ch->fifo_mask);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700300}
301
302static int smd_packet_read_avail(struct smd_channel *ch)
303{
304 if (ch->current_packet) {
305 int n = smd_stream_read_avail(ch);
306 if (n > ch->current_packet)
307 n = ch->current_packet;
308 return n;
309 } else {
310 return 0;
311 }
312}
313
314static int smd_packet_write_avail(struct smd_channel *ch)
315{
316 int n = smd_stream_write_avail(ch);
317 return n > SMD_HEADER_SIZE ? n - SMD_HEADER_SIZE : 0;
318}
319
320static int ch_is_open(struct smd_channel *ch)
321{
322 return (ch->recv->state == SMD_SS_OPENED) &&
323 (ch->send->state == SMD_SS_OPENED);
324}
325
326/* provide a pointer and length to readable data in the fifo */
327static unsigned ch_read_buffer(struct smd_channel *ch, void **ptr)
328{
329 unsigned head = ch->recv->head;
330 unsigned tail = ch->recv->tail;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700331 *ptr = (void *) (ch->recv_data + tail);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700332
333 if (tail <= head)
334 return head - tail;
335 else
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700336 return ch->fifo_size - tail;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700337}
338
339/* advance the fifo read pointer after data from ch_read_buffer is consumed */
340static void ch_read_done(struct smd_channel *ch, unsigned count)
341{
342 BUG_ON(count > smd_stream_read_avail(ch));
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700343 ch->recv->tail = (ch->recv->tail + count) & ch->fifo_mask;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700344 ch->recv->fTAIL = 1;
345}
346
347/* basic read interface to ch_read_{buffer,done} used
348** by smd_*_read() and update_packet_state()
349** will read-and-discard if the _data pointer is null
350*/
351static int ch_read(struct smd_channel *ch, void *_data, int len)
352{
353 void *ptr;
354 unsigned n;
355 unsigned char *data = _data;
356 int orig_len = len;
357
358 while (len > 0) {
359 n = ch_read_buffer(ch, &ptr);
360 if (n == 0)
361 break;
362
363 if (n > len)
364 n = len;
365 if (_data)
366 memcpy(data, ptr, n);
367
368 data += n;
369 len -= n;
370 ch_read_done(ch, n);
371 }
372
373 return orig_len - len;
374}
375
376static void update_stream_state(struct smd_channel *ch)
377{
378 /* streams have no special state requiring updating */
379}
380
381static void update_packet_state(struct smd_channel *ch)
382{
383 unsigned hdr[5];
384 int r;
385
386 /* can't do anything if we're in the middle of a packet */
387 if (ch->current_packet != 0)
388 return;
389
390 /* don't bother unless we can get the full header */
391 if (smd_stream_read_avail(ch) < SMD_HEADER_SIZE)
392 return;
393
394 r = ch_read(ch, hdr, SMD_HEADER_SIZE);
395 BUG_ON(r != SMD_HEADER_SIZE);
396
397 ch->current_packet = hdr[0];
398}
399
400/* provide a pointer and length to next free space in the fifo */
401static unsigned ch_write_buffer(struct smd_channel *ch, void **ptr)
402{
403 unsigned head = ch->send->head;
404 unsigned tail = ch->send->tail;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700405 *ptr = (void *) (ch->send_data + head);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700406
407 if (head < tail) {
408 return tail - head - 1;
409 } else {
410 if (tail == 0)
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700411 return ch->fifo_size - head - 1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700412 else
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700413 return ch->fifo_size - head;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700414 }
415}
416
417/* advace the fifo write pointer after freespace
418 * from ch_write_buffer is filled
419 */
420static void ch_write_done(struct smd_channel *ch, unsigned count)
421{
422 BUG_ON(count > smd_stream_write_avail(ch));
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700423 ch->send->head = (ch->send->head + count) & ch->fifo_mask;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700424 ch->send->fHEAD = 1;
425}
426
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700427static void ch_set_state(struct smd_channel *ch, unsigned n)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700428{
429 if (n == SMD_SS_OPENED) {
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700430 ch->send->fDSR = 1;
431 ch->send->fCTS = 1;
432 ch->send->fCD = 1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700433 } else {
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700434 ch->send->fDSR = 0;
435 ch->send->fCTS = 0;
436 ch->send->fCD = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700437 }
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700438 ch->send->state = n;
439 ch->send->fSTATE = 1;
440 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700441}
442
443static void do_smd_probe(void)
444{
445 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
446 if (shared->heap_info.free_offset != last_heap_free) {
447 last_heap_free = shared->heap_info.free_offset;
448 schedule_work(&probe_work);
449 }
450}
451
452static void smd_state_change(struct smd_channel *ch,
453 unsigned last, unsigned next)
454{
455 ch->last_state = next;
456
457 pr_info("SMD: ch %d %s -> %s\n", ch->n,
458 chstate(last), chstate(next));
459
460 switch (next) {
461 case SMD_SS_OPENING:
462 ch->recv->tail = 0;
463 case SMD_SS_OPENED:
464 if (ch->send->state != SMD_SS_OPENED)
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700465 ch_set_state(ch, SMD_SS_OPENED);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700466 ch->notify(ch->priv, SMD_EVENT_OPEN);
467 break;
468 case SMD_SS_FLUSHING:
469 case SMD_SS_RESET:
470 /* we should force them to close? */
471 default:
472 ch->notify(ch->priv, SMD_EVENT_CLOSE);
473 }
474}
475
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700476static void handle_smd_irq(struct list_head *list, void (*notify)(void))
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700477{
478 unsigned long flags;
479 struct smd_channel *ch;
480 int do_notify = 0;
481 unsigned ch_flags;
482 unsigned tmp;
483
484 spin_lock_irqsave(&smd_lock, flags);
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700485 list_for_each_entry(ch, list, ch_list) {
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700486 ch_flags = 0;
487 if (ch_is_open(ch)) {
488 if (ch->recv->fHEAD) {
489 ch->recv->fHEAD = 0;
490 ch_flags |= 1;
491 do_notify |= 1;
492 }
493 if (ch->recv->fTAIL) {
494 ch->recv->fTAIL = 0;
495 ch_flags |= 2;
496 do_notify |= 1;
497 }
498 if (ch->recv->fSTATE) {
499 ch->recv->fSTATE = 0;
500 ch_flags |= 4;
501 do_notify |= 1;
502 }
503 }
504 tmp = ch->recv->state;
505 if (tmp != ch->last_state)
506 smd_state_change(ch, ch->last_state, tmp);
507 if (ch_flags) {
508 ch->update_state(ch);
509 ch->notify(ch->priv, SMD_EVENT_DATA);
510 }
511 }
512 if (do_notify)
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700513 notify();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700514 spin_unlock_irqrestore(&smd_lock, flags);
515 do_smd_probe();
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700516}
517
518static irqreturn_t smd_irq_handler(int irq, void *data)
519{
520 handle_smd_irq(&smd_ch_list, notify_modem_smd);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700521 return IRQ_HANDLED;
522}
523
524static void smd_fake_irq_handler(unsigned long arg)
525{
526 smd_irq_handler(0, NULL);
527}
528
529static DECLARE_TASKLET(smd_fake_irq_tasklet, smd_fake_irq_handler, 0);
530
531void smd_sleep_exit(void)
532{
533 unsigned long flags;
534 struct smd_channel *ch;
535 unsigned tmp;
536 int need_int = 0;
537
538 spin_lock_irqsave(&smd_lock, flags);
539 list_for_each_entry(ch, &smd_ch_list, ch_list) {
540 if (ch_is_open(ch)) {
541 if (ch->recv->fHEAD) {
542 if (msm_smd_debug_mask & MSM_SMD_DEBUG)
543 pr_info("smd_sleep_exit ch %d fHEAD "
544 "%x %x %x\n",
545 ch->n, ch->recv->fHEAD,
546 ch->recv->head, ch->recv->tail);
547 need_int = 1;
548 break;
549 }
550 if (ch->recv->fTAIL) {
551 if (msm_smd_debug_mask & MSM_SMD_DEBUG)
552 pr_info("smd_sleep_exit ch %d fTAIL "
553 "%x %x %x\n",
554 ch->n, ch->recv->fTAIL,
555 ch->send->head, ch->send->tail);
556 need_int = 1;
557 break;
558 }
559 if (ch->recv->fSTATE) {
560 if (msm_smd_debug_mask & MSM_SMD_DEBUG)
561 pr_info("smd_sleep_exit ch %d fSTATE %x"
562 "\n", ch->n, ch->recv->fSTATE);
563 need_int = 1;
564 break;
565 }
566 tmp = ch->recv->state;
567 if (tmp != ch->last_state) {
568 if (msm_smd_debug_mask & MSM_SMD_DEBUG)
569 pr_info("smd_sleep_exit ch %d "
570 "state %x != %x\n",
571 ch->n, tmp, ch->last_state);
572 need_int = 1;
573 break;
574 }
575 }
576 }
577 spin_unlock_irqrestore(&smd_lock, flags);
578 do_smd_probe();
579 if (need_int) {
580 if (msm_smd_debug_mask & MSM_SMD_DEBUG)
581 pr_info("smd_sleep_exit need interrupt\n");
582 tasklet_schedule(&smd_fake_irq_tasklet);
583 }
584}
585
586
587void smd_kick(smd_channel_t *ch)
588{
589 unsigned long flags;
590 unsigned tmp;
591
592 spin_lock_irqsave(&smd_lock, flags);
593 ch->update_state(ch);
594 tmp = ch->recv->state;
595 if (tmp != ch->last_state) {
596 ch->last_state = tmp;
597 if (tmp == SMD_SS_OPENED)
598 ch->notify(ch->priv, SMD_EVENT_OPEN);
599 else
600 ch->notify(ch->priv, SMD_EVENT_CLOSE);
601 }
602 ch->notify(ch->priv, SMD_EVENT_DATA);
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700603 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700604 spin_unlock_irqrestore(&smd_lock, flags);
605}
606
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700607static int smd_is_packet(int chn, unsigned type)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700608{
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700609 type &= SMD_KIND_MASK;
610 if (type == SMD_KIND_PACKET)
611 return 1;
612 if (type == SMD_KIND_STREAM)
613 return 0;
614
615 /* older AMSS reports SMD_KIND_UNKNOWN always */
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700616 if ((chn > 4) || (chn == 1))
617 return 1;
618 else
619 return 0;
620}
621
622static int smd_stream_write(smd_channel_t *ch, const void *_data, int len)
623{
624 void *ptr;
625 const unsigned char *buf = _data;
626 unsigned xfer;
627 int orig_len = len;
628
629 if (len < 0)
630 return -EINVAL;
631
632 while ((xfer = ch_write_buffer(ch, &ptr)) != 0) {
633 if (!ch_is_open(ch))
634 break;
635 if (xfer > len)
636 xfer = len;
637 memcpy(ptr, buf, xfer);
638 ch_write_done(ch, xfer);
639 len -= xfer;
640 buf += xfer;
641 if (len == 0)
642 break;
643 }
644
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700645 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700646
647 return orig_len - len;
648}
649
650static int smd_packet_write(smd_channel_t *ch, const void *_data, int len)
651{
652 unsigned hdr[5];
653
654 if (len < 0)
655 return -EINVAL;
656
657 if (smd_stream_write_avail(ch) < (len + SMD_HEADER_SIZE))
658 return -ENOMEM;
659
660 hdr[0] = len;
661 hdr[1] = hdr[2] = hdr[3] = hdr[4] = 0;
662
663 smd_stream_write(ch, hdr, sizeof(hdr));
664 smd_stream_write(ch, _data, len);
665
666 return len;
667}
668
669static int smd_stream_read(smd_channel_t *ch, void *data, int len)
670{
671 int r;
672
673 if (len < 0)
674 return -EINVAL;
675
676 r = ch_read(ch, data, len);
677 if (r > 0)
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700678 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700679
680 return r;
681}
682
683static int smd_packet_read(smd_channel_t *ch, void *data, int len)
684{
685 unsigned long flags;
686 int r;
687
688 if (len < 0)
689 return -EINVAL;
690
691 if (len > ch->current_packet)
692 len = ch->current_packet;
693
694 r = ch_read(ch, data, len);
695 if (r > 0)
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700696 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700697
698 spin_lock_irqsave(&smd_lock, flags);
699 ch->current_packet -= r;
700 update_packet_state(ch);
701 spin_unlock_irqrestore(&smd_lock, flags);
702
703 return r;
704}
705
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700706static int smd_alloc_v2(struct smd_channel *ch)
707{
708 struct smd_shared_v2 *shared2;
709 void *buffer;
710 unsigned buffer_sz;
711
712 shared2 = smem_alloc(SMEM_SMD_BASE_ID + ch->n, sizeof(*shared2));
713 buffer = smem_item(SMEM_SMD_FIFO_BASE_ID + ch->n, &buffer_sz);
714
715 if (!buffer)
716 return -1;
717
718 /* buffer must be a power-of-two size */
719 if (buffer_sz & (buffer_sz - 1))
720 return -1;
721
722 buffer_sz /= 2;
723 ch->send = &shared2->ch0;
724 ch->recv = &shared2->ch1;
725 ch->send_data = buffer;
726 ch->recv_data = buffer + buffer_sz;
727 ch->fifo_size = buffer_sz;
728 return 0;
729}
730
731static int smd_alloc_v1(struct smd_channel *ch)
732{
733 struct smd_shared_v1 *shared1;
734 shared1 = smem_alloc(ID_SMD_CHANNELS + ch->n, sizeof(*shared1));
735 if (!shared1) {
736 pr_err("smd_alloc_channel() cid %d does not exist\n", ch->n);
737 return -1;
738 }
739 ch->send = &shared1->ch0;
740 ch->recv = &shared1->ch1;
741 ch->send_data = shared1->data0;
742 ch->recv_data = shared1->data1;
743 ch->fifo_size = SMD_BUF_SIZE;
744 return 0;
745}
746
747
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700748static void smd_alloc_channel(const char *name, uint32_t cid, uint32_t type)
749{
750 struct smd_channel *ch;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700751
752 ch = kzalloc(sizeof(struct smd_channel), GFP_KERNEL);
753 if (ch == 0) {
754 pr_err("smd_alloc_channel() out of memory\n");
755 return;
756 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700757 ch->n = cid;
758
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700759 if (smd_alloc_v2(ch) && smd_alloc_v1(ch)) {
760 kfree(ch);
761 return;
762 }
763
764 ch->fifo_mask = ch->fifo_size - 1;
765 ch->type = type;
766
767 if ((type & SMD_TYPE_MASK) == SMD_TYPE_APPS_MODEM)
768 ch->notify_other_cpu = notify_modem_smd;
769 else
770 ch->notify_other_cpu = notify_dsp_smd;
771
772 if (smd_is_packet(cid, type)) {
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700773 ch->read = smd_packet_read;
774 ch->write = smd_packet_write;
775 ch->read_avail = smd_packet_read_avail;
776 ch->write_avail = smd_packet_write_avail;
777 ch->update_state = update_packet_state;
778 } else {
779 ch->read = smd_stream_read;
780 ch->write = smd_stream_write;
781 ch->read_avail = smd_stream_read_avail;
782 ch->write_avail = smd_stream_write_avail;
783 ch->update_state = update_stream_state;
784 }
785
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700786 if ((type & 0xff) == 0)
787 memcpy(ch->name, "SMD_", 4);
788 else
789 memcpy(ch->name, "DSP_", 4);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700790 memcpy(ch->name + 4, name, 20);
791 ch->name[23] = 0;
792 ch->pdev.name = ch->name;
793 ch->pdev.id = -1;
794
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700795 pr_info("smd_alloc_channel() cid=%02d size=%05d '%s'\n",
796 ch->n, ch->fifo_size, ch->name);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700797
798 mutex_lock(&smd_creation_mutex);
799 list_add(&ch->ch_list, &smd_ch_closed_list);
800 mutex_unlock(&smd_creation_mutex);
801
802 platform_device_register(&ch->pdev);
803}
804
805static void do_nothing_notify(void *priv, unsigned flags)
806{
807}
808
809struct smd_channel *smd_get_channel(const char *name)
810{
811 struct smd_channel *ch;
812
813 mutex_lock(&smd_creation_mutex);
814 list_for_each_entry(ch, &smd_ch_closed_list, ch_list) {
815 if (!strcmp(name, ch->name)) {
816 list_del(&ch->ch_list);
817 mutex_unlock(&smd_creation_mutex);
818 return ch;
819 }
820 }
821 mutex_unlock(&smd_creation_mutex);
822
823 return NULL;
824}
825
826int smd_open(const char *name, smd_channel_t **_ch,
827 void *priv, void (*notify)(void *, unsigned))
828{
829 struct smd_channel *ch;
830 unsigned long flags;
831
832 if (smd_initialized == 0) {
833 pr_info("smd_open() before smd_init()\n");
834 return -ENODEV;
835 }
836
837 ch = smd_get_channel(name);
838 if (!ch)
839 return -ENODEV;
840
841 if (notify == 0)
842 notify = do_nothing_notify;
843
844 ch->notify = notify;
845 ch->current_packet = 0;
846 ch->last_state = SMD_SS_CLOSED;
847 ch->priv = priv;
848
849 *_ch = ch;
850
851 spin_lock_irqsave(&smd_lock, flags);
852 list_add(&ch->ch_list, &smd_ch_list);
853
854 /* If the remote side is CLOSING, we need to get it to
855 * move to OPENING (which we'll do by moving from CLOSED to
856 * OPENING) and then get it to move from OPENING to
857 * OPENED (by doing the same state change ourselves).
858 *
859 * Otherwise, it should be OPENING and we can move directly
860 * to OPENED so that it will follow.
861 */
862 if (ch->recv->state == SMD_SS_CLOSING) {
863 ch->send->head = 0;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700864 ch_set_state(ch, SMD_SS_OPENING);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700865 } else {
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700866 ch_set_state(ch, SMD_SS_OPENED);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700867 }
868 spin_unlock_irqrestore(&smd_lock, flags);
869 smd_kick(ch);
870
871 return 0;
872}
873
874int smd_close(smd_channel_t *ch)
875{
876 unsigned long flags;
877
878 pr_info("smd_close(%p)\n", ch);
879
880 if (ch == 0)
881 return -1;
882
883 spin_lock_irqsave(&smd_lock, flags);
884 ch->notify = do_nothing_notify;
885 list_del(&ch->ch_list);
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700886 ch_set_state(ch, SMD_SS_CLOSED);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700887 spin_unlock_irqrestore(&smd_lock, flags);
888
889 mutex_lock(&smd_creation_mutex);
890 list_add(&ch->ch_list, &smd_ch_closed_list);
891 mutex_unlock(&smd_creation_mutex);
892
893 return 0;
894}
895
896int smd_read(smd_channel_t *ch, void *data, int len)
897{
898 return ch->read(ch, data, len);
899}
900
901int smd_write(smd_channel_t *ch, const void *data, int len)
902{
903 return ch->write(ch, data, len);
904}
905
906int smd_read_avail(smd_channel_t *ch)
907{
908 return ch->read_avail(ch);
909}
910
911int smd_write_avail(smd_channel_t *ch)
912{
913 return ch->write_avail(ch);
914}
915
916int smd_wait_until_readable(smd_channel_t *ch, int bytes)
917{
918 return -1;
919}
920
921int smd_wait_until_writable(smd_channel_t *ch, int bytes)
922{
923 return -1;
924}
925
926int smd_cur_packet_size(smd_channel_t *ch)
927{
928 return ch->current_packet;
929}
930
931
932/* ------------------------------------------------------------------------- */
933
934void *smem_alloc(unsigned id, unsigned size)
935{
936 return smem_find(id, size);
937}
938
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700939static void *smem_item(unsigned id, unsigned *size)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700940{
941 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
942 struct smem_heap_entry *toc = shared->heap_toc;
943
944 if (id >= SMEM_NUM_ITEMS)
945 return 0;
946
947 if (toc[id].allocated) {
948 *size = toc[id].size;
949 return (void *) (MSM_SHARED_RAM_BASE + toc[id].offset);
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700950 } else {
951 *size = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700952 }
953
954 return 0;
955}
956
957void *smem_find(unsigned id, unsigned size_in)
958{
959 unsigned size;
960 void *ptr;
961
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700962 ptr = smem_item(id, &size);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700963 if (!ptr)
964 return 0;
965
966 size_in = ALIGN(size_in, 8);
967 if (size_in != size) {
968 pr_err("smem_find(%d, %d): wrong size %d\n",
969 id, size_in, size);
970 return 0;
971 }
972
973 return ptr;
974}
975
976static irqreturn_t smsm_irq_handler(int irq, void *data)
977{
978 unsigned long flags;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700979 unsigned apps, modm;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700980
981 spin_lock_irqsave(&smem_lock, flags);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700982
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700983 apps = raw_smsm_get_state(SMSM_STATE_APPS);
984 modm = raw_smsm_get_state(SMSM_STATE_MODEM);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700985
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700986 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
987 pr_info("<SM %08x %08x>\n", apps, modm);
988 if (modm & SMSM_RESET) {
989 handle_modem_crash();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700990 }
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700991 do_smd_probe();
992
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700993 spin_unlock_irqrestore(&smem_lock, flags);
994 return IRQ_HANDLED;
995}
996
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700997int smsm_change_state(enum smsm_state_item item,
998 uint32_t clear_mask, uint32_t set_mask)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700999{
1000 unsigned long flags;
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001001 unsigned state;
Arve Hjønnevåg28379412009-05-20 16:52:36 -07001002 unsigned addr = smd_info.state + item * 4;
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001003
1004 if (!smd_info.ready)
1005 return -EIO;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001006
1007 spin_lock_irqsave(&smem_lock, flags);
1008
Arve Hjønnevåg28379412009-05-20 16:52:36 -07001009 if (raw_smsm_get_state(SMSM_STATE_MODEM) & SMSM_RESET)
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001010 handle_modem_crash();
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001011
Arve Hjønnevåg28379412009-05-20 16:52:36 -07001012 state = (readl(addr) & ~clear_mask) | set_mask;
1013 writel(state, addr);
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001014
1015 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
Arve Hjønnevåg28379412009-05-20 16:52:36 -07001016 pr_info("smsm_change_state %d %x\n", item, state);
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001017 notify_other_smsm();
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001018
1019 spin_unlock_irqrestore(&smem_lock, flags);
1020
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001021 return 0;
1022}
1023
Arve Hjønnevåg28379412009-05-20 16:52:36 -07001024uint32_t smsm_get_state(enum smsm_state_item item)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001025{
1026 unsigned long flags;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001027 uint32_t rv;
1028
1029 spin_lock_irqsave(&smem_lock, flags);
1030
Arve Hjønnevåg28379412009-05-20 16:52:36 -07001031 rv = readl(smd_info.state + item * 4);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001032
Arve Hjønnevåg28379412009-05-20 16:52:36 -07001033 if (item == SMSM_STATE_MODEM && (rv & SMSM_RESET))
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001034 handle_modem_crash();
1035
1036 spin_unlock_irqrestore(&smem_lock, flags);
1037
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001038 return rv;
1039}
1040
1041int smsm_set_sleep_duration(uint32_t delay)
1042{
1043 uint32_t *ptr;
1044
1045 ptr = smem_alloc(SMEM_SMSM_SLEEP_DELAY, sizeof(*ptr));
1046 if (ptr == NULL) {
1047 pr_err("smsm_set_sleep_duration <SM NO SLEEP_DELAY>\n");
1048 return -EIO;
1049 }
1050 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
1051 pr_info("smsm_set_sleep_duration %d -> %d\n",
1052 *ptr, delay);
1053 *ptr = delay;
1054 return 0;
1055}
1056
1057int smsm_set_interrupt_info(struct smsm_interrupt_info *info)
1058{
1059 struct smsm_interrupt_info *ptr;
1060
1061 ptr = smem_alloc(SMEM_SMSM_INT_INFO, sizeof(*ptr));
1062 if (ptr == NULL) {
1063 pr_err("smsm_set_sleep_duration <SM NO INT_INFO>\n");
1064 return -EIO;
1065 }
1066 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
1067 pr_info("smsm_set_interrupt_info %x %x -> %x %x\n",
1068 ptr->aArm_en_mask, ptr->aArm_interrupts_pending,
1069 info->aArm_en_mask, info->aArm_interrupts_pending);
1070 *ptr = *info;
1071 return 0;
1072}
1073
1074#define MAX_NUM_SLEEP_CLIENTS 64
1075#define MAX_SLEEP_NAME_LEN 8
1076
1077#define NUM_GPIO_INT_REGISTERS 6
1078#define GPIO_SMEM_NUM_GROUPS 2
1079#define GPIO_SMEM_MAX_PC_INTERRUPTS 8
1080
1081struct tramp_gpio_save {
1082 unsigned int enable;
1083 unsigned int detect;
1084 unsigned int polarity;
1085};
1086
1087struct tramp_gpio_smem {
1088 uint16_t num_fired[GPIO_SMEM_NUM_GROUPS];
1089 uint16_t fired[GPIO_SMEM_NUM_GROUPS][GPIO_SMEM_MAX_PC_INTERRUPTS];
1090 uint32_t enabled[NUM_GPIO_INT_REGISTERS];
1091 uint32_t detection[NUM_GPIO_INT_REGISTERS];
1092 uint32_t polarity[NUM_GPIO_INT_REGISTERS];
1093};
1094
1095
1096void smsm_print_sleep_info(void)
1097{
1098 unsigned long flags;
1099 uint32_t *ptr;
1100 struct tramp_gpio_smem *gpio;
1101 struct smsm_interrupt_info *int_info;
1102
1103
1104 spin_lock_irqsave(&smem_lock, flags);
1105
1106 ptr = smem_alloc(SMEM_SMSM_SLEEP_DELAY, sizeof(*ptr));
1107 if (ptr)
1108 pr_info("SMEM_SMSM_SLEEP_DELAY: %x\n", *ptr);
1109
1110 ptr = smem_alloc(SMEM_SMSM_LIMIT_SLEEP, sizeof(*ptr));
1111 if (ptr)
1112 pr_info("SMEM_SMSM_LIMIT_SLEEP: %x\n", *ptr);
1113
1114 ptr = smem_alloc(SMEM_SLEEP_POWER_COLLAPSE_DISABLED, sizeof(*ptr));
1115 if (ptr)
1116 pr_info("SMEM_SLEEP_POWER_COLLAPSE_DISABLED: %x\n", *ptr);
1117
1118 int_info = smem_alloc(SMEM_SMSM_INT_INFO, sizeof(*int_info));
1119 if (int_info)
1120 pr_info("SMEM_SMSM_INT_INFO %x %x %x\n",
1121 int_info->aArm_en_mask,
1122 int_info->aArm_interrupts_pending,
1123 int_info->aArm_wakeup_reason);
1124
1125 gpio = smem_alloc(SMEM_GPIO_INT, sizeof(*gpio));
1126 if (gpio) {
1127 int i;
1128 for (i = 0; i < NUM_GPIO_INT_REGISTERS; i++)
1129 pr_info("SMEM_GPIO_INT: %d: e %x d %x p %x\n",
1130 i, gpio->enabled[i], gpio->detection[i],
1131 gpio->polarity[i]);
1132
1133 for (i = 0; i < GPIO_SMEM_NUM_GROUPS; i++)
1134 pr_info("SMEM_GPIO_INT: %d: f %d: %d %d...\n",
1135 i, gpio->num_fired[i], gpio->fired[i][0],
1136 gpio->fired[i][1]);
1137 }
1138
1139 spin_unlock_irqrestore(&smem_lock, flags);
1140}
1141
1142int smd_core_init(void)
1143{
1144 int r;
1145 pr_info("smd_core_init()\n");
1146
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001147 /* wait for essential items to be initialized */
1148 for (;;) {
1149 unsigned size;
1150 void *state;
1151 state = smem_item(SMEM_SMSM_SHARED_STATE, &size);
Arve Hjønnevåg28379412009-05-20 16:52:36 -07001152 if (size == SMSM_V1_SIZE || size == SMSM_V2_SIZE) {
1153 smd_info.state = (unsigned)state;
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001154 break;
1155 }
1156 }
1157
1158 smd_info.ready = 1;
1159
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001160 r = request_irq(INT_A9_M2A_0, smd_irq_handler,
1161 IRQF_TRIGGER_RISING, "smd_dev", 0);
1162 if (r < 0)
1163 return r;
1164 r = enable_irq_wake(INT_A9_M2A_0);
1165 if (r < 0)
1166 pr_err("smd_core_init: enable_irq_wake failed for A9_M2A_0\n");
1167
1168 r = request_irq(INT_A9_M2A_5, smsm_irq_handler,
1169 IRQF_TRIGGER_RISING, "smsm_dev", 0);
1170 if (r < 0) {
1171 free_irq(INT_A9_M2A_0, 0);
1172 return r;
1173 }
1174 r = enable_irq_wake(INT_A9_M2A_5);
1175 if (r < 0)
1176 pr_err("smd_core_init: enable_irq_wake failed for A9_M2A_5\n");
1177
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001178 /* check for any SMD channels that may already exist */
1179 do_smd_probe();
1180
1181 /* indicate that we're up and running */
Arve Hjønnevåg28379412009-05-20 16:52:36 -07001182 smsm_change_state(SMSM_STATE_APPS,
1183 ~0, SMSM_INIT | SMSM_SMDINIT | SMSM_RPCINIT);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001184
1185 pr_info("smd_core_init() done\n");
1186
1187 return 0;
1188}
1189
1190#if defined(CONFIG_DEBUG_FS)
1191
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001192static int dump_ch(char *buf, int max, struct smd_channel *ch)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001193{
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001194 volatile struct smd_half_channel *s = ch->send;
1195 volatile struct smd_half_channel *r = ch->recv;
1196
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001197 return scnprintf(
1198 buf, max,
1199 "ch%02d:"
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001200 " %8s(%05d/%05d) %c%c%c%c%c%c%c <->"
1201 " %8s(%05d/%05d) %c%c%c%c%c%c%c\n", ch->n,
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001202 chstate(s->state), s->tail, s->head,
1203 s->fDSR ? 'D' : 'd',
1204 s->fCTS ? 'C' : 'c',
1205 s->fCD ? 'C' : 'c',
1206 s->fRI ? 'I' : 'i',
1207 s->fHEAD ? 'W' : 'w',
1208 s->fTAIL ? 'R' : 'r',
1209 s->fSTATE ? 'S' : 's',
1210 chstate(r->state), r->tail, r->head,
1211 r->fDSR ? 'D' : 'd',
1212 r->fCTS ? 'R' : 'r',
1213 r->fCD ? 'C' : 'c',
1214 r->fRI ? 'I' : 'i',
1215 r->fHEAD ? 'W' : 'w',
1216 r->fTAIL ? 'R' : 'r',
1217 r->fSTATE ? 'S' : 's'
1218 );
1219}
1220
1221static int debug_read_stat(char *buf, int max)
1222{
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001223 char *msg;
1224 int i = 0;
1225
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001226 msg = smem_find(ID_DIAG_ERR_MSG, SZ_DIAG_ERR_MSG);
1227
Arve Hjønnevåg28379412009-05-20 16:52:36 -07001228 if (raw_smsm_get_state(SMSM_STATE_MODEM) & SMSM_RESET)
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001229 i += scnprintf(buf + i, max - i,
1230 "smsm: ARM9 HAS CRASHED\n");
1231
1232 i += scnprintf(buf + i, max - i, "smsm: a9: %08x a11: %08x\n",
Arve Hjønnevåg28379412009-05-20 16:52:36 -07001233 raw_smsm_get_state(SMSM_STATE_MODEM),
1234 raw_smsm_get_state(SMSM_STATE_APPS));
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001235
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001236 if (msg) {
1237 msg[SZ_DIAG_ERR_MSG - 1] = 0;
1238 i += scnprintf(buf + i, max - i, "diag: '%s'\n", msg);
1239 }
1240 return i;
1241}
1242
1243static int debug_read_mem(char *buf, int max)
1244{
1245 unsigned n;
1246 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
1247 struct smem_heap_entry *toc = shared->heap_toc;
1248 int i = 0;
1249
1250 i += scnprintf(buf + i, max - i,
1251 "heap: init=%d free=%d remain=%d\n",
1252 shared->heap_info.initialized,
1253 shared->heap_info.free_offset,
1254 shared->heap_info.heap_remaining);
1255
1256 for (n = 0; n < SMEM_NUM_ITEMS; n++) {
1257 if (toc[n].allocated == 0)
1258 continue;
1259 i += scnprintf(buf + i, max - i,
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001260 "%04d: offset %08x size %08x\n",
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001261 n, toc[n].offset, toc[n].size);
1262 }
1263 return i;
1264}
1265
1266static int debug_read_ch(char *buf, int max)
1267{
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001268 struct smd_channel *ch;
1269 unsigned long flags;
1270 int i = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001271
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001272 spin_lock_irqsave(&smd_lock, flags);
1273 list_for_each_entry(ch, &smd_ch_list, ch_list)
1274 i += dump_ch(buf + i, max - i, ch);
1275 list_for_each_entry(ch, &smd_ch_closed_list, ch_list)
1276 i += dump_ch(buf + i, max - i, ch);
1277 spin_unlock_irqrestore(&smd_lock, flags);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001278
1279 return i;
1280}
1281
1282static int debug_read_version(char *buf, int max)
1283{
1284 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
1285 unsigned version = shared->version[VERSION_MODEM];
1286 return sprintf(buf, "%d.%d\n", version >> 16, version & 0xffff);
1287}
1288
1289static int debug_read_build_id(char *buf, int max)
1290{
1291 unsigned size;
1292 void *data;
1293
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001294 data = smem_item(SMEM_HW_SW_BUILD_ID, &size);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001295 if (!data)
1296 return 0;
1297
1298 if (size >= max)
1299 size = max;
1300 memcpy(buf, data, size);
1301
1302 return size;
1303}
1304
1305static int debug_read_alloc_tbl(char *buf, int max)
1306{
1307 struct smd_alloc_elm *shared;
1308 int n, i = 0;
1309
1310 shared = smem_find(ID_CH_ALLOC_TBL, sizeof(*shared) * 64);
1311
1312 for (n = 0; n < 64; n++) {
1313 if (shared[n].ref_count == 0)
1314 continue;
1315 i += scnprintf(buf + i, max - i,
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001316 "%03d: %-20s cid=%02d type=%03d "
1317 "kind=%02d ref_count=%d\n",
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001318 n, shared[n].name, shared[n].cid,
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001319 shared[n].ctype & 0xff,
1320 (shared[n].ctype >> 8) & 0xf,
1321 shared[n].ref_count);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001322 }
1323
1324 return i;
1325}
1326
1327static int debug_boom(char *buf, int max)
1328{
1329 unsigned ms = 5000;
1330 msm_proc_comm(PCOM_RESET_MODEM, &ms, 0);
1331 return 0;
1332}
1333
1334#define DEBUG_BUFMAX 4096
1335static char debug_buffer[DEBUG_BUFMAX];
1336
1337static ssize_t debug_read(struct file *file, char __user *buf,
1338 size_t count, loff_t *ppos)
1339{
1340 int (*fill)(char *buf, int max) = file->private_data;
1341 int bsize = fill(debug_buffer, DEBUG_BUFMAX);
1342 return simple_read_from_buffer(buf, count, ppos, debug_buffer, bsize);
1343}
1344
1345static int debug_open(struct inode *inode, struct file *file)
1346{
1347 file->private_data = inode->i_private;
1348 return 0;
1349}
1350
1351static const struct file_operations debug_ops = {
1352 .read = debug_read,
1353 .open = debug_open,
1354};
1355
1356static void debug_create(const char *name, mode_t mode,
1357 struct dentry *dent,
1358 int (*fill)(char *buf, int max))
1359{
1360 debugfs_create_file(name, mode, dent, fill, &debug_ops);
1361}
1362
1363static void smd_debugfs_init(void)
1364{
1365 struct dentry *dent;
1366
1367 dent = debugfs_create_dir("smd", 0);
1368 if (IS_ERR(dent))
1369 return;
1370
1371 debug_create("ch", 0444, dent, debug_read_ch);
1372 debug_create("stat", 0444, dent, debug_read_stat);
1373 debug_create("mem", 0444, dent, debug_read_mem);
1374 debug_create("version", 0444, dent, debug_read_version);
1375 debug_create("tbl", 0444, dent, debug_read_alloc_tbl);
1376 debug_create("build", 0444, dent, debug_read_build_id);
1377 debug_create("boom", 0444, dent, debug_boom);
1378}
1379#else
1380static void smd_debugfs_init(void) {}
1381#endif
1382
1383static int __init msm_smd_probe(struct platform_device *pdev)
1384{
1385 pr_info("smd_init()\n");
1386
1387 INIT_WORK(&probe_work, smd_channel_probe_worker);
1388
1389 if (smd_core_init()) {
1390 pr_err("smd_core_init() failed\n");
1391 return -1;
1392 }
1393
1394 do_smd_probe();
1395
1396 msm_check_for_modem_crash = check_for_modem_crash;
1397
1398 smd_debugfs_init();
1399 smd_initialized = 1;
1400
1401 return 0;
1402}
1403
1404static struct platform_driver msm_smd_driver = {
1405 .probe = msm_smd_probe,
1406 .driver = {
1407 .name = MODULE_NAME,
1408 .owner = THIS_MODULE,
1409 },
1410};
1411
1412static int __init msm_smd_init(void)
1413{
1414 return platform_driver_register(&msm_smd_driver);
1415}
1416
1417module_init(msm_smd_init);
1418
1419MODULE_DESCRIPTION("MSM Shared Memory Core");
1420MODULE_AUTHOR("Brian Swetland <swetland@google.com>");
1421MODULE_LICENSE("GPL");