blob: 2e32d9ab3609a273149c93c5cdbd72afc54fa92c [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
Brian Swetland37521a32009-07-01 18:30:47 -070038#if defined(CONFIG_ARCH_QSD8X50)
39#define CONFIG_QDSP6 1
40#endif
41
Brian Swetland2eb44eb2008-09-29 16:00:48 -070042void (*msm_hw_reset_hook)(void);
43
44#define MODULE_NAME "msm_smd"
45
46enum {
47 MSM_SMD_DEBUG = 1U << 0,
48 MSM_SMSM_DEBUG = 1U << 0,
49};
50
51static int msm_smd_debug_mask;
52
Brian Swetland03e00cd2009-07-01 17:58:37 -070053struct shared_info {
Brian Swetland5b0f5a32009-04-26 18:38:49 -070054 int ready;
Arve Hjønnevåg28379412009-05-20 16:52:36 -070055 unsigned state;
Brian Swetland5b0f5a32009-04-26 18:38:49 -070056};
57
Arve Hjønnevåg28379412009-05-20 16:52:36 -070058static unsigned dummy_state[SMSM_STATE_COUNT];
Brian Swetland5b0f5a32009-04-26 18:38:49 -070059
60static struct shared_info smd_info = {
Arve Hjønnevåg28379412009-05-20 16:52:36 -070061 .state = (unsigned) &dummy_state,
Brian Swetland5b0f5a32009-04-26 18:38:49 -070062};
63
Brian Swetland2eb44eb2008-09-29 16:00:48 -070064module_param_named(debug_mask, msm_smd_debug_mask,
65 int, S_IRUGO | S_IWUSR | S_IWGRP);
66
Brian Swetland03e00cd2009-07-01 17:58:37 -070067void *smem_item(unsigned id, unsigned *size);
Brian Swetland2eb44eb2008-09-29 16:00:48 -070068static void smd_diag(void);
69
70static unsigned last_heap_free = 0xffffffff;
71
Dima Zavinb42dc442010-01-29 11:43:42 -080072static inline void msm_a2m_int(uint32_t irq)
73{
74#if defined(CONFIG_ARCH_MSM7X30)
75 writel(1 << irq, MSM_GCC_BASE + 0x8);
76#else
77 writel(1, MSM_CSR_BASE + 0x400 + (irq * 4));
78#endif
79}
80
Brian Swetland2eb44eb2008-09-29 16:00:48 -070081
82static inline void notify_other_smsm(void)
83{
Dima Zavinb42dc442010-01-29 11:43:42 -080084 msm_a2m_int(5);
Brian Swetland37521a32009-07-01 18:30:47 -070085#ifdef CONFIG_QDSP6
Dima Zavinb42dc442010-01-29 11:43:42 -080086 msm_a2m_int(8);
Brian Swetland37521a32009-07-01 18:30:47 -070087#endif
Brian Swetland2eb44eb2008-09-29 16:00:48 -070088}
89
Brian Swetland5b0f5a32009-04-26 18:38:49 -070090static inline void notify_modem_smd(void)
Brian Swetland2eb44eb2008-09-29 16:00:48 -070091{
Dima Zavinb42dc442010-01-29 11:43:42 -080092 msm_a2m_int(0);
Brian Swetland2eb44eb2008-09-29 16:00:48 -070093}
94
Brian Swetland5b0f5a32009-04-26 18:38:49 -070095static inline void notify_dsp_smd(void)
96{
Dima Zavinb42dc442010-01-29 11:43:42 -080097 msm_a2m_int(8);
Brian Swetland5b0f5a32009-04-26 18:38:49 -070098}
99
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700100static void smd_diag(void)
101{
102 char *x;
103
104 x = smem_find(ID_DIAG_ERR_MSG, SZ_DIAG_ERR_MSG);
105 if (x != 0) {
106 x[SZ_DIAG_ERR_MSG - 1] = 0;
107 pr_info("smem: DIAG '%s'\n", x);
108 }
109}
110
111/* call when SMSM_RESET flag is set in the A9's smsm_state */
112static void handle_modem_crash(void)
113{
114 pr_err("ARM9 has CRASHED\n");
115 smd_diag();
116
117 /* hard reboot if possible */
118 if (msm_hw_reset_hook)
119 msm_hw_reset_hook();
120
121 /* in this case the modem or watchdog should reboot us */
122 for (;;)
123 ;
124}
125
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700126uint32_t raw_smsm_get_state(enum smsm_state_item item)
127{
128 return readl(smd_info.state + item * 4);
129}
130
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700131static int check_for_modem_crash(void)
132{
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700133 if (raw_smsm_get_state(SMSM_STATE_MODEM) & SMSM_RESET) {
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700134 handle_modem_crash();
135 return -1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700136 }
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700137 return 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700138}
139
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700140/* the spinlock is used to synchronize between the
Brian Swetland03e00cd2009-07-01 17:58:37 -0700141 * irq handler and code that mutates the channel
142 * list or fiddles with channel state
143 */
144DEFINE_SPINLOCK(smd_lock);
145DEFINE_SPINLOCK(smem_lock);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700146
147/* the mutex is used during open() and close()
Brian Swetland03e00cd2009-07-01 17:58:37 -0700148 * operations to avoid races while creating or
149 * destroying smd_channel structures
150 */
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700151static DEFINE_MUTEX(smd_creation_mutex);
152
153static int smd_initialized;
154
Brian Swetland03e00cd2009-07-01 17:58:37 -0700155LIST_HEAD(smd_ch_closed_list);
Brian Swetland37521a32009-07-01 18:30:47 -0700156LIST_HEAD(smd_ch_list_modem);
157LIST_HEAD(smd_ch_list_dsp);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700158
159static unsigned char smd_ch_allocated[64];
160static struct work_struct probe_work;
161
Brian Swetland34f719b2009-10-30 16:22:05 -0700162static int smd_alloc_channel(const char *name, uint32_t cid, uint32_t type);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700163
164static void smd_channel_probe_worker(struct work_struct *work)
165{
166 struct smd_alloc_elm *shared;
Brian Swetland37521a32009-07-01 18:30:47 -0700167 unsigned ctype;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700168 unsigned type;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700169 unsigned n;
170
171 shared = smem_find(ID_CH_ALLOC_TBL, sizeof(*shared) * 64);
Brian Swetland4d4fb262009-01-28 20:25:40 -0800172 if (!shared) {
173 pr_err("smd: cannot find allocation table\n");
174 return;
175 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700176 for (n = 0; n < 64; n++) {
177 if (smd_ch_allocated[n])
178 continue;
179 if (!shared[n].ref_count)
180 continue;
181 if (!shared[n].name[0])
182 continue;
Brian Swetland37521a32009-07-01 18:30:47 -0700183 ctype = shared[n].ctype;
184 type = ctype & SMD_TYPE_MASK;
185
186 /* DAL channels are stream but neither the modem,
187 * nor the DSP correctly indicate this. Fixup manually.
188 */
189 if (!memcmp(shared[n].name, "DAL", 3))
190 ctype = (ctype & (~SMD_KIND_MASK)) | SMD_KIND_STREAM;
191
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700192 type = shared[n].ctype & SMD_TYPE_MASK;
193 if ((type == SMD_TYPE_APPS_MODEM) ||
194 (type == SMD_TYPE_APPS_DSP))
Brian Swetland34f719b2009-10-30 16:22:05 -0700195 if (!smd_alloc_channel(shared[n].name, shared[n].cid, ctype))
196 smd_ch_allocated[n] = 1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700197 }
198}
199
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700200/* how many bytes are available for reading */
201static int smd_stream_read_avail(struct smd_channel *ch)
202{
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700203 return (ch->recv->head - ch->recv->tail) & ch->fifo_mask;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700204}
205
206/* how many bytes we are free to write */
207static int smd_stream_write_avail(struct smd_channel *ch)
208{
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700209 return ch->fifo_mask -
210 ((ch->send->head - ch->send->tail) & ch->fifo_mask);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700211}
212
213static int smd_packet_read_avail(struct smd_channel *ch)
214{
215 if (ch->current_packet) {
216 int n = smd_stream_read_avail(ch);
217 if (n > ch->current_packet)
218 n = ch->current_packet;
219 return n;
220 } else {
221 return 0;
222 }
223}
224
225static int smd_packet_write_avail(struct smd_channel *ch)
226{
227 int n = smd_stream_write_avail(ch);
228 return n > SMD_HEADER_SIZE ? n - SMD_HEADER_SIZE : 0;
229}
230
231static int ch_is_open(struct smd_channel *ch)
232{
233 return (ch->recv->state == SMD_SS_OPENED) &&
234 (ch->send->state == SMD_SS_OPENED);
235}
236
237/* provide a pointer and length to readable data in the fifo */
238static unsigned ch_read_buffer(struct smd_channel *ch, void **ptr)
239{
240 unsigned head = ch->recv->head;
241 unsigned tail = ch->recv->tail;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700242 *ptr = (void *) (ch->recv_data + tail);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700243
244 if (tail <= head)
245 return head - tail;
246 else
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700247 return ch->fifo_size - tail;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700248}
249
250/* advance the fifo read pointer after data from ch_read_buffer is consumed */
251static void ch_read_done(struct smd_channel *ch, unsigned count)
252{
253 BUG_ON(count > smd_stream_read_avail(ch));
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700254 ch->recv->tail = (ch->recv->tail + count) & ch->fifo_mask;
Haley Teng7632fba2009-10-12 10:38:10 -0700255 ch->send->fTAIL = 1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700256}
257
258/* basic read interface to ch_read_{buffer,done} used
Brian Swetland03e00cd2009-07-01 17:58:37 -0700259 * by smd_*_read() and update_packet_state()
260 * will read-and-discard if the _data pointer is null
261 */
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700262static int ch_read(struct smd_channel *ch, void *_data, int len)
263{
264 void *ptr;
265 unsigned n;
266 unsigned char *data = _data;
267 int orig_len = len;
268
269 while (len > 0) {
270 n = ch_read_buffer(ch, &ptr);
271 if (n == 0)
272 break;
273
274 if (n > len)
275 n = len;
276 if (_data)
277 memcpy(data, ptr, n);
278
279 data += n;
280 len -= n;
281 ch_read_done(ch, n);
282 }
283
284 return orig_len - len;
285}
286
287static void update_stream_state(struct smd_channel *ch)
288{
289 /* streams have no special state requiring updating */
290}
291
292static void update_packet_state(struct smd_channel *ch)
293{
294 unsigned hdr[5];
295 int r;
296
297 /* can't do anything if we're in the middle of a packet */
298 if (ch->current_packet != 0)
299 return;
300
301 /* don't bother unless we can get the full header */
302 if (smd_stream_read_avail(ch) < SMD_HEADER_SIZE)
303 return;
304
305 r = ch_read(ch, hdr, SMD_HEADER_SIZE);
306 BUG_ON(r != SMD_HEADER_SIZE);
307
308 ch->current_packet = hdr[0];
309}
310
311/* provide a pointer and length to next free space in the fifo */
312static unsigned ch_write_buffer(struct smd_channel *ch, void **ptr)
313{
314 unsigned head = ch->send->head;
315 unsigned tail = ch->send->tail;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700316 *ptr = (void *) (ch->send_data + head);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700317
318 if (head < tail) {
319 return tail - head - 1;
320 } else {
321 if (tail == 0)
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700322 return ch->fifo_size - head - 1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700323 else
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700324 return ch->fifo_size - head;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700325 }
326}
327
328/* advace the fifo write pointer after freespace
329 * from ch_write_buffer is filled
330 */
331static void ch_write_done(struct smd_channel *ch, unsigned count)
332{
333 BUG_ON(count > smd_stream_write_avail(ch));
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700334 ch->send->head = (ch->send->head + count) & ch->fifo_mask;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700335 ch->send->fHEAD = 1;
336}
337
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700338static void ch_set_state(struct smd_channel *ch, unsigned n)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700339{
340 if (n == SMD_SS_OPENED) {
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700341 ch->send->fDSR = 1;
342 ch->send->fCTS = 1;
343 ch->send->fCD = 1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700344 } else {
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700345 ch->send->fDSR = 0;
346 ch->send->fCTS = 0;
347 ch->send->fCD = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700348 }
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700349 ch->send->state = n;
350 ch->send->fSTATE = 1;
351 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700352}
353
354static void do_smd_probe(void)
355{
356 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
357 if (shared->heap_info.free_offset != last_heap_free) {
358 last_heap_free = shared->heap_info.free_offset;
359 schedule_work(&probe_work);
360 }
361}
362
363static void smd_state_change(struct smd_channel *ch,
364 unsigned last, unsigned next)
365{
366 ch->last_state = next;
367
Brian Swetland03e00cd2009-07-01 17:58:37 -0700368 pr_info("SMD: ch %d %d -> %d\n", ch->n, last, next);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700369
370 switch (next) {
371 case SMD_SS_OPENING:
372 ch->recv->tail = 0;
373 case SMD_SS_OPENED:
374 if (ch->send->state != SMD_SS_OPENED)
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700375 ch_set_state(ch, SMD_SS_OPENED);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700376 ch->notify(ch->priv, SMD_EVENT_OPEN);
377 break;
378 case SMD_SS_FLUSHING:
379 case SMD_SS_RESET:
380 /* we should force them to close? */
381 default:
382 ch->notify(ch->priv, SMD_EVENT_CLOSE);
383 }
384}
385
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700386static void handle_smd_irq(struct list_head *list, void (*notify)(void))
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700387{
388 unsigned long flags;
389 struct smd_channel *ch;
390 int do_notify = 0;
391 unsigned ch_flags;
392 unsigned tmp;
393
394 spin_lock_irqsave(&smd_lock, flags);
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700395 list_for_each_entry(ch, list, ch_list) {
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700396 ch_flags = 0;
397 if (ch_is_open(ch)) {
398 if (ch->recv->fHEAD) {
399 ch->recv->fHEAD = 0;
400 ch_flags |= 1;
401 do_notify |= 1;
402 }
403 if (ch->recv->fTAIL) {
404 ch->recv->fTAIL = 0;
405 ch_flags |= 2;
406 do_notify |= 1;
407 }
408 if (ch->recv->fSTATE) {
409 ch->recv->fSTATE = 0;
410 ch_flags |= 4;
411 do_notify |= 1;
412 }
413 }
414 tmp = ch->recv->state;
415 if (tmp != ch->last_state)
416 smd_state_change(ch, ch->last_state, tmp);
417 if (ch_flags) {
418 ch->update_state(ch);
419 ch->notify(ch->priv, SMD_EVENT_DATA);
420 }
421 }
422 if (do_notify)
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700423 notify();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700424 spin_unlock_irqrestore(&smd_lock, flags);
425 do_smd_probe();
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700426}
427
Brian Swetland37521a32009-07-01 18:30:47 -0700428static irqreturn_t smd_modem_irq_handler(int irq, void *data)
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700429{
Brian Swetland37521a32009-07-01 18:30:47 -0700430 handle_smd_irq(&smd_ch_list_modem, notify_modem_smd);
431 return IRQ_HANDLED;
432}
433
434static irqreturn_t smd_dsp_irq_handler(int irq, void *data)
435{
436 handle_smd_irq(&smd_ch_list_dsp, notify_dsp_smd);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700437 return IRQ_HANDLED;
438}
439
440static void smd_fake_irq_handler(unsigned long arg)
441{
Brian Swetland37521a32009-07-01 18:30:47 -0700442 handle_smd_irq(&smd_ch_list_modem, notify_modem_smd);
443 handle_smd_irq(&smd_ch_list_dsp, notify_dsp_smd);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700444}
445
446static DECLARE_TASKLET(smd_fake_irq_tasklet, smd_fake_irq_handler, 0);
447
Brian Swetland37521a32009-07-01 18:30:47 -0700448static inline int smd_need_int(struct smd_channel *ch)
449{
450 if (ch_is_open(ch)) {
451 if (ch->recv->fHEAD || ch->recv->fTAIL || ch->recv->fSTATE)
452 return 1;
453 if (ch->recv->state != ch->last_state)
454 return 1;
455 }
456 return 0;
457}
458
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700459void smd_sleep_exit(void)
460{
461 unsigned long flags;
462 struct smd_channel *ch;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700463 int need_int = 0;
464
465 spin_lock_irqsave(&smd_lock, flags);
Brian Swetland37521a32009-07-01 18:30:47 -0700466 list_for_each_entry(ch, &smd_ch_list_modem, ch_list) {
467 if (smd_need_int(ch)) {
468 need_int = 1;
469 break;
470 }
471 }
472 list_for_each_entry(ch, &smd_ch_list_dsp, ch_list) {
473 if (smd_need_int(ch)) {
474 need_int = 1;
475 break;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700476 }
477 }
478 spin_unlock_irqrestore(&smd_lock, flags);
479 do_smd_probe();
Brian Swetland37521a32009-07-01 18:30:47 -0700480
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700481 if (need_int) {
482 if (msm_smd_debug_mask & MSM_SMD_DEBUG)
483 pr_info("smd_sleep_exit need interrupt\n");
484 tasklet_schedule(&smd_fake_irq_tasklet);
485 }
486}
487
488
489void smd_kick(smd_channel_t *ch)
490{
491 unsigned long flags;
492 unsigned tmp;
493
494 spin_lock_irqsave(&smd_lock, flags);
495 ch->update_state(ch);
496 tmp = ch->recv->state;
497 if (tmp != ch->last_state) {
498 ch->last_state = tmp;
499 if (tmp == SMD_SS_OPENED)
500 ch->notify(ch->priv, SMD_EVENT_OPEN);
501 else
502 ch->notify(ch->priv, SMD_EVENT_CLOSE);
503 }
504 ch->notify(ch->priv, SMD_EVENT_DATA);
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700505 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700506 spin_unlock_irqrestore(&smd_lock, flags);
507}
508
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700509static int smd_is_packet(int chn, unsigned type)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700510{
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700511 type &= SMD_KIND_MASK;
512 if (type == SMD_KIND_PACKET)
513 return 1;
514 if (type == SMD_KIND_STREAM)
515 return 0;
516
517 /* older AMSS reports SMD_KIND_UNKNOWN always */
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700518 if ((chn > 4) || (chn == 1))
519 return 1;
520 else
521 return 0;
522}
523
524static int smd_stream_write(smd_channel_t *ch, const void *_data, int len)
525{
526 void *ptr;
527 const unsigned char *buf = _data;
528 unsigned xfer;
529 int orig_len = len;
530
531 if (len < 0)
532 return -EINVAL;
533
534 while ((xfer = ch_write_buffer(ch, &ptr)) != 0) {
535 if (!ch_is_open(ch))
536 break;
537 if (xfer > len)
538 xfer = len;
539 memcpy(ptr, buf, xfer);
540 ch_write_done(ch, xfer);
541 len -= xfer;
542 buf += xfer;
543 if (len == 0)
544 break;
545 }
546
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700547 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700548
549 return orig_len - len;
550}
551
552static int smd_packet_write(smd_channel_t *ch, const void *_data, int len)
553{
554 unsigned hdr[5];
555
556 if (len < 0)
557 return -EINVAL;
558
559 if (smd_stream_write_avail(ch) < (len + SMD_HEADER_SIZE))
560 return -ENOMEM;
561
562 hdr[0] = len;
563 hdr[1] = hdr[2] = hdr[3] = hdr[4] = 0;
564
565 smd_stream_write(ch, hdr, sizeof(hdr));
566 smd_stream_write(ch, _data, len);
567
568 return len;
569}
570
571static int smd_stream_read(smd_channel_t *ch, void *data, int len)
572{
573 int r;
574
575 if (len < 0)
576 return -EINVAL;
577
578 r = ch_read(ch, data, len);
579 if (r > 0)
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700580 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700581
582 return r;
583}
584
585static int smd_packet_read(smd_channel_t *ch, void *data, int len)
586{
587 unsigned long flags;
588 int r;
589
590 if (len < 0)
591 return -EINVAL;
592
593 if (len > ch->current_packet)
594 len = ch->current_packet;
595
596 r = ch_read(ch, data, len);
597 if (r > 0)
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700598 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700599
600 spin_lock_irqsave(&smd_lock, flags);
601 ch->current_packet -= r;
602 update_packet_state(ch);
603 spin_unlock_irqrestore(&smd_lock, flags);
604
605 return r;
606}
607
Brian Swetland34f719b2009-10-30 16:22:05 -0700608static int smd_alloc_channel(const char *name, uint32_t cid, uint32_t type)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700609{
610 struct smd_channel *ch;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700611
612 ch = kzalloc(sizeof(struct smd_channel), GFP_KERNEL);
613 if (ch == 0) {
614 pr_err("smd_alloc_channel() out of memory\n");
Brian Swetland34f719b2009-10-30 16:22:05 -0700615 return -1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700616 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700617 ch->n = cid;
618
Daniel Walkerbf83de42010-03-16 16:29:44 -0700619 if (_smd_alloc_channel(ch)) {
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700620 kfree(ch);
Brian Swetland34f719b2009-10-30 16:22:05 -0700621 return -1;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700622 }
623
624 ch->fifo_mask = ch->fifo_size - 1;
625 ch->type = type;
626
627 if ((type & SMD_TYPE_MASK) == SMD_TYPE_APPS_MODEM)
628 ch->notify_other_cpu = notify_modem_smd;
629 else
630 ch->notify_other_cpu = notify_dsp_smd;
631
632 if (smd_is_packet(cid, type)) {
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700633 ch->read = smd_packet_read;
634 ch->write = smd_packet_write;
635 ch->read_avail = smd_packet_read_avail;
636 ch->write_avail = smd_packet_write_avail;
637 ch->update_state = update_packet_state;
638 } else {
639 ch->read = smd_stream_read;
640 ch->write = smd_stream_write;
641 ch->read_avail = smd_stream_read_avail;
642 ch->write_avail = smd_stream_write_avail;
643 ch->update_state = update_stream_state;
644 }
645
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700646 if ((type & 0xff) == 0)
647 memcpy(ch->name, "SMD_", 4);
648 else
649 memcpy(ch->name, "DSP_", 4);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700650 memcpy(ch->name + 4, name, 20);
651 ch->name[23] = 0;
652 ch->pdev.name = ch->name;
653 ch->pdev.id = -1;
654
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700655 pr_info("smd_alloc_channel() cid=%02d size=%05d '%s'\n",
656 ch->n, ch->fifo_size, ch->name);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700657
658 mutex_lock(&smd_creation_mutex);
659 list_add(&ch->ch_list, &smd_ch_closed_list);
660 mutex_unlock(&smd_creation_mutex);
661
662 platform_device_register(&ch->pdev);
Brian Swetland34f719b2009-10-30 16:22:05 -0700663 return 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700664}
665
666static void do_nothing_notify(void *priv, unsigned flags)
667{
668}
669
670struct smd_channel *smd_get_channel(const char *name)
671{
672 struct smd_channel *ch;
673
674 mutex_lock(&smd_creation_mutex);
675 list_for_each_entry(ch, &smd_ch_closed_list, ch_list) {
676 if (!strcmp(name, ch->name)) {
677 list_del(&ch->ch_list);
678 mutex_unlock(&smd_creation_mutex);
679 return ch;
680 }
681 }
682 mutex_unlock(&smd_creation_mutex);
683
684 return NULL;
685}
686
687int smd_open(const char *name, smd_channel_t **_ch,
688 void *priv, void (*notify)(void *, unsigned))
689{
690 struct smd_channel *ch;
691 unsigned long flags;
692
693 if (smd_initialized == 0) {
694 pr_info("smd_open() before smd_init()\n");
695 return -ENODEV;
696 }
697
698 ch = smd_get_channel(name);
699 if (!ch)
700 return -ENODEV;
701
702 if (notify == 0)
703 notify = do_nothing_notify;
704
705 ch->notify = notify;
706 ch->current_packet = 0;
707 ch->last_state = SMD_SS_CLOSED;
708 ch->priv = priv;
709
710 *_ch = ch;
711
712 spin_lock_irqsave(&smd_lock, flags);
Brian Swetland37521a32009-07-01 18:30:47 -0700713
714 if ((ch->type & SMD_TYPE_MASK) == SMD_TYPE_APPS_MODEM)
715 list_add(&ch->ch_list, &smd_ch_list_modem);
716 else
717 list_add(&ch->ch_list, &smd_ch_list_dsp);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700718
719 /* If the remote side is CLOSING, we need to get it to
720 * move to OPENING (which we'll do by moving from CLOSED to
721 * OPENING) and then get it to move from OPENING to
722 * OPENED (by doing the same state change ourselves).
723 *
724 * Otherwise, it should be OPENING and we can move directly
725 * to OPENED so that it will follow.
726 */
727 if (ch->recv->state == SMD_SS_CLOSING) {
728 ch->send->head = 0;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700729 ch_set_state(ch, SMD_SS_OPENING);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700730 } else {
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700731 ch_set_state(ch, SMD_SS_OPENED);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700732 }
733 spin_unlock_irqrestore(&smd_lock, flags);
734 smd_kick(ch);
735
736 return 0;
737}
738
739int smd_close(smd_channel_t *ch)
740{
741 unsigned long flags;
742
743 pr_info("smd_close(%p)\n", ch);
744
745 if (ch == 0)
746 return -1;
747
748 spin_lock_irqsave(&smd_lock, flags);
749 ch->notify = do_nothing_notify;
750 list_del(&ch->ch_list);
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700751 ch_set_state(ch, SMD_SS_CLOSED);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700752 spin_unlock_irqrestore(&smd_lock, flags);
753
754 mutex_lock(&smd_creation_mutex);
755 list_add(&ch->ch_list, &smd_ch_closed_list);
756 mutex_unlock(&smd_creation_mutex);
757
758 return 0;
759}
760
761int smd_read(smd_channel_t *ch, void *data, int len)
762{
763 return ch->read(ch, data, len);
764}
765
766int smd_write(smd_channel_t *ch, const void *data, int len)
767{
768 return ch->write(ch, data, len);
769}
770
Brian Swetland636eb9c2009-12-07 15:28:08 -0800771int smd_write_atomic(smd_channel_t *ch, const void *data, int len)
772{
773 unsigned long flags;
774 int res;
775 spin_lock_irqsave(&smd_lock, flags);
776 res = ch->write(ch, data, len);
777 spin_unlock_irqrestore(&smd_lock, flags);
778 return res;
779}
780
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700781int smd_read_avail(smd_channel_t *ch)
782{
783 return ch->read_avail(ch);
784}
785
786int smd_write_avail(smd_channel_t *ch)
787{
788 return ch->write_avail(ch);
789}
790
791int smd_wait_until_readable(smd_channel_t *ch, int bytes)
792{
793 return -1;
794}
795
796int smd_wait_until_writable(smd_channel_t *ch, int bytes)
797{
798 return -1;
799}
800
801int smd_cur_packet_size(smd_channel_t *ch)
802{
803 return ch->current_packet;
804}
805
806
807/* ------------------------------------------------------------------------- */
808
809void *smem_alloc(unsigned id, unsigned size)
810{
811 return smem_find(id, size);
812}
813
Brian Swetland03e00cd2009-07-01 17:58:37 -0700814void *smem_item(unsigned id, unsigned *size)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700815{
816 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
817 struct smem_heap_entry *toc = shared->heap_toc;
818
819 if (id >= SMEM_NUM_ITEMS)
820 return 0;
821
822 if (toc[id].allocated) {
823 *size = toc[id].size;
824 return (void *) (MSM_SHARED_RAM_BASE + toc[id].offset);
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700825 } else {
826 *size = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700827 }
828
829 return 0;
830}
831
832void *smem_find(unsigned id, unsigned size_in)
833{
834 unsigned size;
835 void *ptr;
836
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700837 ptr = smem_item(id, &size);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700838 if (!ptr)
839 return 0;
840
841 size_in = ALIGN(size_in, 8);
842 if (size_in != size) {
843 pr_err("smem_find(%d, %d): wrong size %d\n",
844 id, size_in, size);
845 return 0;
846 }
847
848 return ptr;
849}
850
851static irqreturn_t smsm_irq_handler(int irq, void *data)
852{
853 unsigned long flags;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700854 unsigned apps, modm;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700855
856 spin_lock_irqsave(&smem_lock, flags);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700857
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700858 apps = raw_smsm_get_state(SMSM_STATE_APPS);
859 modm = raw_smsm_get_state(SMSM_STATE_MODEM);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700860
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700861 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
862 pr_info("<SM %08x %08x>\n", apps, modm);
Daniel Walker79848a22010-03-16 15:20:07 -0700863 if (modm & SMSM_RESET)
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700864 handle_modem_crash();
Daniel Walker79848a22010-03-16 15:20:07 -0700865
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700866 do_smd_probe();
867
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700868 spin_unlock_irqrestore(&smem_lock, flags);
869 return IRQ_HANDLED;
870}
871
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700872int smsm_change_state(enum smsm_state_item item,
873 uint32_t clear_mask, uint32_t set_mask)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700874{
Daniel Walker1a86fbc2010-03-17 09:58:29 -0700875 unsigned long addr = smd_info.state + item * 4;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700876 unsigned long flags;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700877 unsigned state;
878
879 if (!smd_info.ready)
880 return -EIO;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700881
882 spin_lock_irqsave(&smem_lock, flags);
883
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700884 if (raw_smsm_get_state(SMSM_STATE_MODEM) & SMSM_RESET)
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700885 handle_modem_crash();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700886
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700887 state = (readl(addr) & ~clear_mask) | set_mask;
888 writel(state, addr);
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700889
890 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700891 pr_info("smsm_change_state %d %x\n", item, state);
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700892 notify_other_smsm();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700893
894 spin_unlock_irqrestore(&smem_lock, flags);
895
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700896 return 0;
897}
898
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700899uint32_t smsm_get_state(enum smsm_state_item item)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700900{
901 unsigned long flags;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700902 uint32_t rv;
903
904 spin_lock_irqsave(&smem_lock, flags);
905
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700906 rv = readl(smd_info.state + item * 4);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700907
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700908 if (item == SMSM_STATE_MODEM && (rv & SMSM_RESET))
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700909 handle_modem_crash();
910
911 spin_unlock_irqrestore(&smem_lock, flags);
912
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700913 return rv;
914}
915
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -0700916#ifdef CONFIG_ARCH_MSM_SCORPION
917
918int smsm_set_sleep_duration(uint32_t delay)
919{
Brian Swetland03e00cd2009-07-01 17:58:37 -0700920 struct msm_dem_slave_data *ptr;
921
922 ptr = smem_find(SMEM_APPS_DEM_SLAVE_DATA, sizeof(*ptr));
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -0700923 if (ptr == NULL) {
924 pr_err("smsm_set_sleep_duration <SM NO APPS_DEM_SLAVE_DATA>\n");
925 return -EIO;
926 }
927 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
928 pr_info("smsm_set_sleep_duration %d -> %d\n",
929 ptr->sleep_time, delay);
930 ptr->sleep_time = delay;
931 return 0;
932}
933
934#else
935
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700936int smsm_set_sleep_duration(uint32_t delay)
937{
938 uint32_t *ptr;
939
Brian Swetland03e00cd2009-07-01 17:58:37 -0700940 ptr = smem_find(SMEM_SMSM_SLEEP_DELAY, sizeof(*ptr));
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700941 if (ptr == NULL) {
942 pr_err("smsm_set_sleep_duration <SM NO SLEEP_DELAY>\n");
943 return -EIO;
944 }
945 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
946 pr_info("smsm_set_sleep_duration %d -> %d\n",
947 *ptr, delay);
948 *ptr = delay;
949 return 0;
950}
951
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -0700952#endif
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700953
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700954int smd_core_init(void)
955{
956 int r;
957 pr_info("smd_core_init()\n");
958
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700959 /* wait for essential items to be initialized */
960 for (;;) {
961 unsigned size;
962 void *state;
963 state = smem_item(SMEM_SMSM_SHARED_STATE, &size);
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700964 if (size == SMSM_V1_SIZE || size == SMSM_V2_SIZE) {
965 smd_info.state = (unsigned)state;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700966 break;
967 }
968 }
969
970 smd_info.ready = 1;
971
Brian Swetland37521a32009-07-01 18:30:47 -0700972 r = request_irq(INT_A9_M2A_0, smd_modem_irq_handler,
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700973 IRQF_TRIGGER_RISING, "smd_dev", 0);
974 if (r < 0)
975 return r;
976 r = enable_irq_wake(INT_A9_M2A_0);
977 if (r < 0)
978 pr_err("smd_core_init: enable_irq_wake failed for A9_M2A_0\n");
979
980 r = request_irq(INT_A9_M2A_5, smsm_irq_handler,
981 IRQF_TRIGGER_RISING, "smsm_dev", 0);
982 if (r < 0) {
983 free_irq(INT_A9_M2A_0, 0);
984 return r;
985 }
986 r = enable_irq_wake(INT_A9_M2A_5);
987 if (r < 0)
988 pr_err("smd_core_init: enable_irq_wake failed for A9_M2A_5\n");
989
Brian Swetland37521a32009-07-01 18:30:47 -0700990#if defined(CONFIG_QDSP6)
991 r = request_irq(INT_ADSP_A11, smd_dsp_irq_handler,
992 IRQF_TRIGGER_RISING, "smd_dsp", 0);
993 if (r < 0) {
994 free_irq(INT_A9_M2A_0, 0);
995 free_irq(INT_A9_M2A_5, 0);
996 return r;
997 }
998#endif
999
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001000 /* check for any SMD channels that may already exist */
1001 do_smd_probe();
1002
1003 /* indicate that we're up and running */
Arve Hjønnevåg28379412009-05-20 16:52:36 -07001004 smsm_change_state(SMSM_STATE_APPS,
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -07001005 ~0, SMSM_INIT | SMSM_SMDINIT | SMSM_RPCINIT | SMSM_RUN);
1006#ifdef CONFIG_ARCH_MSM_SCORPION
1007 smsm_change_state(SMSM_STATE_APPS_DEM, ~0, 0);
1008#endif
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001009
1010 pr_info("smd_core_init() done\n");
1011
1012 return 0;
1013}
1014
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001015static int __init msm_smd_probe(struct platform_device *pdev)
1016{
1017 pr_info("smd_init()\n");
1018
1019 INIT_WORK(&probe_work, smd_channel_probe_worker);
1020
1021 if (smd_core_init()) {
1022 pr_err("smd_core_init() failed\n");
1023 return -1;
1024 }
1025
1026 do_smd_probe();
1027
1028 msm_check_for_modem_crash = check_for_modem_crash;
1029
Iliyan Malchev1207bab2009-11-15 18:16:43 -08001030 msm_init_last_radio_log(THIS_MODULE);
1031
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001032 smd_initialized = 1;
1033
1034 return 0;
1035}
1036
1037static struct platform_driver msm_smd_driver = {
1038 .probe = msm_smd_probe,
1039 .driver = {
1040 .name = MODULE_NAME,
1041 .owner = THIS_MODULE,
1042 },
1043};
1044
1045static int __init msm_smd_init(void)
1046{
1047 return platform_driver_register(&msm_smd_driver);
1048}
1049
1050module_init(msm_smd_init);
1051
1052MODULE_DESCRIPTION("MSM Shared Memory Core");
1053MODULE_AUTHOR("Brian Swetland <swetland@google.com>");
1054MODULE_LICENSE("GPL");