blob: b6966680a2d8a5e4e7ffe52312b58a6a8026cba7 [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
126extern int (*msm_check_for_modem_crash)(void);
127
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700128uint32_t raw_smsm_get_state(enum smsm_state_item item)
129{
130 return readl(smd_info.state + item * 4);
131}
132
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700133static int check_for_modem_crash(void)
134{
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700135 if (raw_smsm_get_state(SMSM_STATE_MODEM) & SMSM_RESET) {
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700136 handle_modem_crash();
137 return -1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700138 }
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700139 return 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700140}
141
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700142/* the spinlock is used to synchronize between the
Brian Swetland03e00cd2009-07-01 17:58:37 -0700143 * irq handler and code that mutates the channel
144 * list or fiddles with channel state
145 */
146DEFINE_SPINLOCK(smd_lock);
147DEFINE_SPINLOCK(smem_lock);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700148
149/* the mutex is used during open() and close()
Brian Swetland03e00cd2009-07-01 17:58:37 -0700150 * operations to avoid races while creating or
151 * destroying smd_channel structures
152 */
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700153static DEFINE_MUTEX(smd_creation_mutex);
154
155static int smd_initialized;
156
Brian Swetland03e00cd2009-07-01 17:58:37 -0700157LIST_HEAD(smd_ch_closed_list);
Brian Swetland37521a32009-07-01 18:30:47 -0700158LIST_HEAD(smd_ch_list_modem);
159LIST_HEAD(smd_ch_list_dsp);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700160
161static unsigned char smd_ch_allocated[64];
162static struct work_struct probe_work;
163
Brian Swetland34f719b2009-10-30 16:22:05 -0700164static int smd_alloc_channel(const char *name, uint32_t cid, uint32_t type);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700165
166static void smd_channel_probe_worker(struct work_struct *work)
167{
168 struct smd_alloc_elm *shared;
Brian Swetland37521a32009-07-01 18:30:47 -0700169 unsigned ctype;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700170 unsigned type;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700171 unsigned n;
172
173 shared = smem_find(ID_CH_ALLOC_TBL, sizeof(*shared) * 64);
Brian Swetland4d4fb262009-01-28 20:25:40 -0800174 if (!shared) {
175 pr_err("smd: cannot find allocation table\n");
176 return;
177 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700178 for (n = 0; n < 64; n++) {
179 if (smd_ch_allocated[n])
180 continue;
181 if (!shared[n].ref_count)
182 continue;
183 if (!shared[n].name[0])
184 continue;
Brian Swetland37521a32009-07-01 18:30:47 -0700185 ctype = shared[n].ctype;
186 type = ctype & SMD_TYPE_MASK;
187
188 /* DAL channels are stream but neither the modem,
189 * nor the DSP correctly indicate this. Fixup manually.
190 */
191 if (!memcmp(shared[n].name, "DAL", 3))
192 ctype = (ctype & (~SMD_KIND_MASK)) | SMD_KIND_STREAM;
193
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700194 type = shared[n].ctype & SMD_TYPE_MASK;
195 if ((type == SMD_TYPE_APPS_MODEM) ||
196 (type == SMD_TYPE_APPS_DSP))
Brian Swetland34f719b2009-10-30 16:22:05 -0700197 if (!smd_alloc_channel(shared[n].name, shared[n].cid, ctype))
198 smd_ch_allocated[n] = 1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700199 }
200}
201
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700202/* how many bytes are available for reading */
203static int smd_stream_read_avail(struct smd_channel *ch)
204{
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700205 return (ch->recv->head - ch->recv->tail) & ch->fifo_mask;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700206}
207
208/* how many bytes we are free to write */
209static int smd_stream_write_avail(struct smd_channel *ch)
210{
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700211 return ch->fifo_mask -
212 ((ch->send->head - ch->send->tail) & ch->fifo_mask);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700213}
214
215static int smd_packet_read_avail(struct smd_channel *ch)
216{
217 if (ch->current_packet) {
218 int n = smd_stream_read_avail(ch);
219 if (n > ch->current_packet)
220 n = ch->current_packet;
221 return n;
222 } else {
223 return 0;
224 }
225}
226
227static int smd_packet_write_avail(struct smd_channel *ch)
228{
229 int n = smd_stream_write_avail(ch);
230 return n > SMD_HEADER_SIZE ? n - SMD_HEADER_SIZE : 0;
231}
232
233static int ch_is_open(struct smd_channel *ch)
234{
235 return (ch->recv->state == SMD_SS_OPENED) &&
236 (ch->send->state == SMD_SS_OPENED);
237}
238
239/* provide a pointer and length to readable data in the fifo */
240static unsigned ch_read_buffer(struct smd_channel *ch, void **ptr)
241{
242 unsigned head = ch->recv->head;
243 unsigned tail = ch->recv->tail;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700244 *ptr = (void *) (ch->recv_data + tail);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700245
246 if (tail <= head)
247 return head - tail;
248 else
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700249 return ch->fifo_size - tail;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700250}
251
252/* advance the fifo read pointer after data from ch_read_buffer is consumed */
253static void ch_read_done(struct smd_channel *ch, unsigned count)
254{
255 BUG_ON(count > smd_stream_read_avail(ch));
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700256 ch->recv->tail = (ch->recv->tail + count) & ch->fifo_mask;
Haley Teng7632fba2009-10-12 10:38:10 -0700257 ch->send->fTAIL = 1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700258}
259
260/* basic read interface to ch_read_{buffer,done} used
Brian Swetland03e00cd2009-07-01 17:58:37 -0700261 * by smd_*_read() and update_packet_state()
262 * will read-and-discard if the _data pointer is null
263 */
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700264static int ch_read(struct smd_channel *ch, void *_data, int len)
265{
266 void *ptr;
267 unsigned n;
268 unsigned char *data = _data;
269 int orig_len = len;
270
271 while (len > 0) {
272 n = ch_read_buffer(ch, &ptr);
273 if (n == 0)
274 break;
275
276 if (n > len)
277 n = len;
278 if (_data)
279 memcpy(data, ptr, n);
280
281 data += n;
282 len -= n;
283 ch_read_done(ch, n);
284 }
285
286 return orig_len - len;
287}
288
289static void update_stream_state(struct smd_channel *ch)
290{
291 /* streams have no special state requiring updating */
292}
293
294static void update_packet_state(struct smd_channel *ch)
295{
296 unsigned hdr[5];
297 int r;
298
299 /* can't do anything if we're in the middle of a packet */
300 if (ch->current_packet != 0)
301 return;
302
303 /* don't bother unless we can get the full header */
304 if (smd_stream_read_avail(ch) < SMD_HEADER_SIZE)
305 return;
306
307 r = ch_read(ch, hdr, SMD_HEADER_SIZE);
308 BUG_ON(r != SMD_HEADER_SIZE);
309
310 ch->current_packet = hdr[0];
311}
312
313/* provide a pointer and length to next free space in the fifo */
314static unsigned ch_write_buffer(struct smd_channel *ch, void **ptr)
315{
316 unsigned head = ch->send->head;
317 unsigned tail = ch->send->tail;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700318 *ptr = (void *) (ch->send_data + head);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700319
320 if (head < tail) {
321 return tail - head - 1;
322 } else {
323 if (tail == 0)
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700324 return ch->fifo_size - head - 1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700325 else
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700326 return ch->fifo_size - head;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700327 }
328}
329
330/* advace the fifo write pointer after freespace
331 * from ch_write_buffer is filled
332 */
333static void ch_write_done(struct smd_channel *ch, unsigned count)
334{
335 BUG_ON(count > smd_stream_write_avail(ch));
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700336 ch->send->head = (ch->send->head + count) & ch->fifo_mask;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700337 ch->send->fHEAD = 1;
338}
339
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700340static void ch_set_state(struct smd_channel *ch, unsigned n)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700341{
342 if (n == SMD_SS_OPENED) {
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700343 ch->send->fDSR = 1;
344 ch->send->fCTS = 1;
345 ch->send->fCD = 1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700346 } else {
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700347 ch->send->fDSR = 0;
348 ch->send->fCTS = 0;
349 ch->send->fCD = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700350 }
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700351 ch->send->state = n;
352 ch->send->fSTATE = 1;
353 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700354}
355
356static void do_smd_probe(void)
357{
358 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
359 if (shared->heap_info.free_offset != last_heap_free) {
360 last_heap_free = shared->heap_info.free_offset;
361 schedule_work(&probe_work);
362 }
363}
364
365static void smd_state_change(struct smd_channel *ch,
366 unsigned last, unsigned next)
367{
368 ch->last_state = next;
369
Brian Swetland03e00cd2009-07-01 17:58:37 -0700370 pr_info("SMD: ch %d %d -> %d\n", ch->n, last, next);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700371
372 switch (next) {
373 case SMD_SS_OPENING:
374 ch->recv->tail = 0;
375 case SMD_SS_OPENED:
376 if (ch->send->state != SMD_SS_OPENED)
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700377 ch_set_state(ch, SMD_SS_OPENED);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700378 ch->notify(ch->priv, SMD_EVENT_OPEN);
379 break;
380 case SMD_SS_FLUSHING:
381 case SMD_SS_RESET:
382 /* we should force them to close? */
383 default:
384 ch->notify(ch->priv, SMD_EVENT_CLOSE);
385 }
386}
387
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700388static void handle_smd_irq(struct list_head *list, void (*notify)(void))
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700389{
390 unsigned long flags;
391 struct smd_channel *ch;
392 int do_notify = 0;
393 unsigned ch_flags;
394 unsigned tmp;
395
396 spin_lock_irqsave(&smd_lock, flags);
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700397 list_for_each_entry(ch, list, ch_list) {
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700398 ch_flags = 0;
399 if (ch_is_open(ch)) {
400 if (ch->recv->fHEAD) {
401 ch->recv->fHEAD = 0;
402 ch_flags |= 1;
403 do_notify |= 1;
404 }
405 if (ch->recv->fTAIL) {
406 ch->recv->fTAIL = 0;
407 ch_flags |= 2;
408 do_notify |= 1;
409 }
410 if (ch->recv->fSTATE) {
411 ch->recv->fSTATE = 0;
412 ch_flags |= 4;
413 do_notify |= 1;
414 }
415 }
416 tmp = ch->recv->state;
417 if (tmp != ch->last_state)
418 smd_state_change(ch, ch->last_state, tmp);
419 if (ch_flags) {
420 ch->update_state(ch);
421 ch->notify(ch->priv, SMD_EVENT_DATA);
422 }
423 }
424 if (do_notify)
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700425 notify();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700426 spin_unlock_irqrestore(&smd_lock, flags);
427 do_smd_probe();
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700428}
429
Brian Swetland37521a32009-07-01 18:30:47 -0700430static irqreturn_t smd_modem_irq_handler(int irq, void *data)
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700431{
Brian Swetland37521a32009-07-01 18:30:47 -0700432 handle_smd_irq(&smd_ch_list_modem, notify_modem_smd);
433 return IRQ_HANDLED;
434}
435
436static irqreturn_t smd_dsp_irq_handler(int irq, void *data)
437{
438 handle_smd_irq(&smd_ch_list_dsp, notify_dsp_smd);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700439 return IRQ_HANDLED;
440}
441
442static void smd_fake_irq_handler(unsigned long arg)
443{
Brian Swetland37521a32009-07-01 18:30:47 -0700444 handle_smd_irq(&smd_ch_list_modem, notify_modem_smd);
445 handle_smd_irq(&smd_ch_list_dsp, notify_dsp_smd);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700446}
447
448static DECLARE_TASKLET(smd_fake_irq_tasklet, smd_fake_irq_handler, 0);
449
Brian Swetland37521a32009-07-01 18:30:47 -0700450static inline int smd_need_int(struct smd_channel *ch)
451{
452 if (ch_is_open(ch)) {
453 if (ch->recv->fHEAD || ch->recv->fTAIL || ch->recv->fSTATE)
454 return 1;
455 if (ch->recv->state != ch->last_state)
456 return 1;
457 }
458 return 0;
459}
460
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700461void smd_sleep_exit(void)
462{
463 unsigned long flags;
464 struct smd_channel *ch;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700465 int need_int = 0;
466
467 spin_lock_irqsave(&smd_lock, flags);
Brian Swetland37521a32009-07-01 18:30:47 -0700468 list_for_each_entry(ch, &smd_ch_list_modem, ch_list) {
469 if (smd_need_int(ch)) {
470 need_int = 1;
471 break;
472 }
473 }
474 list_for_each_entry(ch, &smd_ch_list_dsp, ch_list) {
475 if (smd_need_int(ch)) {
476 need_int = 1;
477 break;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700478 }
479 }
480 spin_unlock_irqrestore(&smd_lock, flags);
481 do_smd_probe();
Brian Swetland37521a32009-07-01 18:30:47 -0700482
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700483 if (need_int) {
484 if (msm_smd_debug_mask & MSM_SMD_DEBUG)
485 pr_info("smd_sleep_exit need interrupt\n");
486 tasklet_schedule(&smd_fake_irq_tasklet);
487 }
488}
489
490
491void smd_kick(smd_channel_t *ch)
492{
493 unsigned long flags;
494 unsigned tmp;
495
496 spin_lock_irqsave(&smd_lock, flags);
497 ch->update_state(ch);
498 tmp = ch->recv->state;
499 if (tmp != ch->last_state) {
500 ch->last_state = tmp;
501 if (tmp == SMD_SS_OPENED)
502 ch->notify(ch->priv, SMD_EVENT_OPEN);
503 else
504 ch->notify(ch->priv, SMD_EVENT_CLOSE);
505 }
506 ch->notify(ch->priv, SMD_EVENT_DATA);
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700507 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700508 spin_unlock_irqrestore(&smd_lock, flags);
509}
510
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700511static int smd_is_packet(int chn, unsigned type)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700512{
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700513 type &= SMD_KIND_MASK;
514 if (type == SMD_KIND_PACKET)
515 return 1;
516 if (type == SMD_KIND_STREAM)
517 return 0;
518
519 /* older AMSS reports SMD_KIND_UNKNOWN always */
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700520 if ((chn > 4) || (chn == 1))
521 return 1;
522 else
523 return 0;
524}
525
526static int smd_stream_write(smd_channel_t *ch, const void *_data, int len)
527{
528 void *ptr;
529 const unsigned char *buf = _data;
530 unsigned xfer;
531 int orig_len = len;
532
533 if (len < 0)
534 return -EINVAL;
535
536 while ((xfer = ch_write_buffer(ch, &ptr)) != 0) {
537 if (!ch_is_open(ch))
538 break;
539 if (xfer > len)
540 xfer = len;
541 memcpy(ptr, buf, xfer);
542 ch_write_done(ch, xfer);
543 len -= xfer;
544 buf += xfer;
545 if (len == 0)
546 break;
547 }
548
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700549 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700550
551 return orig_len - len;
552}
553
554static int smd_packet_write(smd_channel_t *ch, const void *_data, int len)
555{
556 unsigned hdr[5];
557
558 if (len < 0)
559 return -EINVAL;
560
561 if (smd_stream_write_avail(ch) < (len + SMD_HEADER_SIZE))
562 return -ENOMEM;
563
564 hdr[0] = len;
565 hdr[1] = hdr[2] = hdr[3] = hdr[4] = 0;
566
567 smd_stream_write(ch, hdr, sizeof(hdr));
568 smd_stream_write(ch, _data, len);
569
570 return len;
571}
572
573static int smd_stream_read(smd_channel_t *ch, void *data, int len)
574{
575 int r;
576
577 if (len < 0)
578 return -EINVAL;
579
580 r = ch_read(ch, data, len);
581 if (r > 0)
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700582 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700583
584 return r;
585}
586
587static int smd_packet_read(smd_channel_t *ch, void *data, int len)
588{
589 unsigned long flags;
590 int r;
591
592 if (len < 0)
593 return -EINVAL;
594
595 if (len > ch->current_packet)
596 len = ch->current_packet;
597
598 r = ch_read(ch, data, len);
599 if (r > 0)
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700600 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700601
602 spin_lock_irqsave(&smd_lock, flags);
603 ch->current_packet -= r;
604 update_packet_state(ch);
605 spin_unlock_irqrestore(&smd_lock, flags);
606
607 return r;
608}
609
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700610static int smd_alloc_v2(struct smd_channel *ch)
611{
612 struct smd_shared_v2 *shared2;
613 void *buffer;
614 unsigned buffer_sz;
615
616 shared2 = smem_alloc(SMEM_SMD_BASE_ID + ch->n, sizeof(*shared2));
617 buffer = smem_item(SMEM_SMD_FIFO_BASE_ID + ch->n, &buffer_sz);
618
619 if (!buffer)
620 return -1;
621
622 /* buffer must be a power-of-two size */
623 if (buffer_sz & (buffer_sz - 1))
624 return -1;
625
626 buffer_sz /= 2;
627 ch->send = &shared2->ch0;
628 ch->recv = &shared2->ch1;
629 ch->send_data = buffer;
630 ch->recv_data = buffer + buffer_sz;
631 ch->fifo_size = buffer_sz;
632 return 0;
633}
634
635static int smd_alloc_v1(struct smd_channel *ch)
636{
637 struct smd_shared_v1 *shared1;
638 shared1 = smem_alloc(ID_SMD_CHANNELS + ch->n, sizeof(*shared1));
639 if (!shared1) {
640 pr_err("smd_alloc_channel() cid %d does not exist\n", ch->n);
641 return -1;
642 }
643 ch->send = &shared1->ch0;
644 ch->recv = &shared1->ch1;
645 ch->send_data = shared1->data0;
646 ch->recv_data = shared1->data1;
647 ch->fifo_size = SMD_BUF_SIZE;
648 return 0;
649}
650
651
Brian Swetland34f719b2009-10-30 16:22:05 -0700652static int smd_alloc_channel(const char *name, uint32_t cid, uint32_t type)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700653{
654 struct smd_channel *ch;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700655
656 ch = kzalloc(sizeof(struct smd_channel), GFP_KERNEL);
657 if (ch == 0) {
658 pr_err("smd_alloc_channel() out of memory\n");
Brian Swetland34f719b2009-10-30 16:22:05 -0700659 return -1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700660 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700661 ch->n = cid;
662
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700663 if (smd_alloc_v2(ch) && smd_alloc_v1(ch)) {
664 kfree(ch);
Brian Swetland34f719b2009-10-30 16:22:05 -0700665 return -1;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700666 }
667
668 ch->fifo_mask = ch->fifo_size - 1;
669 ch->type = type;
670
671 if ((type & SMD_TYPE_MASK) == SMD_TYPE_APPS_MODEM)
672 ch->notify_other_cpu = notify_modem_smd;
673 else
674 ch->notify_other_cpu = notify_dsp_smd;
675
676 if (smd_is_packet(cid, type)) {
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700677 ch->read = smd_packet_read;
678 ch->write = smd_packet_write;
679 ch->read_avail = smd_packet_read_avail;
680 ch->write_avail = smd_packet_write_avail;
681 ch->update_state = update_packet_state;
682 } else {
683 ch->read = smd_stream_read;
684 ch->write = smd_stream_write;
685 ch->read_avail = smd_stream_read_avail;
686 ch->write_avail = smd_stream_write_avail;
687 ch->update_state = update_stream_state;
688 }
689
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700690 if ((type & 0xff) == 0)
691 memcpy(ch->name, "SMD_", 4);
692 else
693 memcpy(ch->name, "DSP_", 4);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700694 memcpy(ch->name + 4, name, 20);
695 ch->name[23] = 0;
696 ch->pdev.name = ch->name;
697 ch->pdev.id = -1;
698
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700699 pr_info("smd_alloc_channel() cid=%02d size=%05d '%s'\n",
700 ch->n, ch->fifo_size, ch->name);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700701
702 mutex_lock(&smd_creation_mutex);
703 list_add(&ch->ch_list, &smd_ch_closed_list);
704 mutex_unlock(&smd_creation_mutex);
705
706 platform_device_register(&ch->pdev);
Brian Swetland34f719b2009-10-30 16:22:05 -0700707 return 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700708}
709
710static void do_nothing_notify(void *priv, unsigned flags)
711{
712}
713
714struct smd_channel *smd_get_channel(const char *name)
715{
716 struct smd_channel *ch;
717
718 mutex_lock(&smd_creation_mutex);
719 list_for_each_entry(ch, &smd_ch_closed_list, ch_list) {
720 if (!strcmp(name, ch->name)) {
721 list_del(&ch->ch_list);
722 mutex_unlock(&smd_creation_mutex);
723 return ch;
724 }
725 }
726 mutex_unlock(&smd_creation_mutex);
727
728 return NULL;
729}
730
731int smd_open(const char *name, smd_channel_t **_ch,
732 void *priv, void (*notify)(void *, unsigned))
733{
734 struct smd_channel *ch;
735 unsigned long flags;
736
737 if (smd_initialized == 0) {
738 pr_info("smd_open() before smd_init()\n");
739 return -ENODEV;
740 }
741
742 ch = smd_get_channel(name);
743 if (!ch)
744 return -ENODEV;
745
746 if (notify == 0)
747 notify = do_nothing_notify;
748
749 ch->notify = notify;
750 ch->current_packet = 0;
751 ch->last_state = SMD_SS_CLOSED;
752 ch->priv = priv;
753
754 *_ch = ch;
755
756 spin_lock_irqsave(&smd_lock, flags);
Brian Swetland37521a32009-07-01 18:30:47 -0700757
758 if ((ch->type & SMD_TYPE_MASK) == SMD_TYPE_APPS_MODEM)
759 list_add(&ch->ch_list, &smd_ch_list_modem);
760 else
761 list_add(&ch->ch_list, &smd_ch_list_dsp);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700762
763 /* If the remote side is CLOSING, we need to get it to
764 * move to OPENING (which we'll do by moving from CLOSED to
765 * OPENING) and then get it to move from OPENING to
766 * OPENED (by doing the same state change ourselves).
767 *
768 * Otherwise, it should be OPENING and we can move directly
769 * to OPENED so that it will follow.
770 */
771 if (ch->recv->state == SMD_SS_CLOSING) {
772 ch->send->head = 0;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700773 ch_set_state(ch, SMD_SS_OPENING);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700774 } else {
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700775 ch_set_state(ch, SMD_SS_OPENED);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700776 }
777 spin_unlock_irqrestore(&smd_lock, flags);
778 smd_kick(ch);
779
780 return 0;
781}
782
783int smd_close(smd_channel_t *ch)
784{
785 unsigned long flags;
786
787 pr_info("smd_close(%p)\n", ch);
788
789 if (ch == 0)
790 return -1;
791
792 spin_lock_irqsave(&smd_lock, flags);
793 ch->notify = do_nothing_notify;
794 list_del(&ch->ch_list);
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700795 ch_set_state(ch, SMD_SS_CLOSED);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700796 spin_unlock_irqrestore(&smd_lock, flags);
797
798 mutex_lock(&smd_creation_mutex);
799 list_add(&ch->ch_list, &smd_ch_closed_list);
800 mutex_unlock(&smd_creation_mutex);
801
802 return 0;
803}
804
805int smd_read(smd_channel_t *ch, void *data, int len)
806{
807 return ch->read(ch, data, len);
808}
809
810int smd_write(smd_channel_t *ch, const void *data, int len)
811{
812 return ch->write(ch, data, len);
813}
814
Brian Swetland636eb9c2009-12-07 15:28:08 -0800815int smd_write_atomic(smd_channel_t *ch, const void *data, int len)
816{
817 unsigned long flags;
818 int res;
819 spin_lock_irqsave(&smd_lock, flags);
820 res = ch->write(ch, data, len);
821 spin_unlock_irqrestore(&smd_lock, flags);
822 return res;
823}
824
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700825int smd_read_avail(smd_channel_t *ch)
826{
827 return ch->read_avail(ch);
828}
829
830int smd_write_avail(smd_channel_t *ch)
831{
832 return ch->write_avail(ch);
833}
834
835int smd_wait_until_readable(smd_channel_t *ch, int bytes)
836{
837 return -1;
838}
839
840int smd_wait_until_writable(smd_channel_t *ch, int bytes)
841{
842 return -1;
843}
844
845int smd_cur_packet_size(smd_channel_t *ch)
846{
847 return ch->current_packet;
848}
849
850
851/* ------------------------------------------------------------------------- */
852
853void *smem_alloc(unsigned id, unsigned size)
854{
855 return smem_find(id, size);
856}
857
Brian Swetland03e00cd2009-07-01 17:58:37 -0700858void *smem_item(unsigned id, unsigned *size)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700859{
860 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
861 struct smem_heap_entry *toc = shared->heap_toc;
862
863 if (id >= SMEM_NUM_ITEMS)
864 return 0;
865
866 if (toc[id].allocated) {
867 *size = toc[id].size;
868 return (void *) (MSM_SHARED_RAM_BASE + toc[id].offset);
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700869 } else {
870 *size = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700871 }
872
873 return 0;
874}
875
876void *smem_find(unsigned id, unsigned size_in)
877{
878 unsigned size;
879 void *ptr;
880
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700881 ptr = smem_item(id, &size);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700882 if (!ptr)
883 return 0;
884
885 size_in = ALIGN(size_in, 8);
886 if (size_in != size) {
887 pr_err("smem_find(%d, %d): wrong size %d\n",
888 id, size_in, size);
889 return 0;
890 }
891
892 return ptr;
893}
894
895static irqreturn_t smsm_irq_handler(int irq, void *data)
896{
897 unsigned long flags;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700898 unsigned apps, modm;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700899
900 spin_lock_irqsave(&smem_lock, flags);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700901
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700902 apps = raw_smsm_get_state(SMSM_STATE_APPS);
903 modm = raw_smsm_get_state(SMSM_STATE_MODEM);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700904
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700905 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
906 pr_info("<SM %08x %08x>\n", apps, modm);
907 if (modm & SMSM_RESET) {
908 handle_modem_crash();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700909 }
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700910 do_smd_probe();
911
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700912 spin_unlock_irqrestore(&smem_lock, flags);
913 return IRQ_HANDLED;
914}
915
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700916int smsm_change_state(enum smsm_state_item item,
917 uint32_t clear_mask, uint32_t set_mask)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700918{
919 unsigned long flags;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700920 unsigned state;
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700921 unsigned addr = smd_info.state + item * 4;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700922
923 if (!smd_info.ready)
924 return -EIO;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700925
926 spin_lock_irqsave(&smem_lock, flags);
927
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700928 if (raw_smsm_get_state(SMSM_STATE_MODEM) & SMSM_RESET)
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700929 handle_modem_crash();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700930
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700931 state = (readl(addr) & ~clear_mask) | set_mask;
932 writel(state, addr);
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700933
934 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700935 pr_info("smsm_change_state %d %x\n", item, state);
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700936 notify_other_smsm();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700937
938 spin_unlock_irqrestore(&smem_lock, flags);
939
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700940 return 0;
941}
942
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700943uint32_t smsm_get_state(enum smsm_state_item item)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700944{
945 unsigned long flags;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700946 uint32_t rv;
947
948 spin_lock_irqsave(&smem_lock, flags);
949
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700950 rv = readl(smd_info.state + item * 4);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700951
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700952 if (item == SMSM_STATE_MODEM && (rv & SMSM_RESET))
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700953 handle_modem_crash();
954
955 spin_unlock_irqrestore(&smem_lock, flags);
956
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700957 return rv;
958}
959
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -0700960#ifdef CONFIG_ARCH_MSM_SCORPION
961
962int smsm_set_sleep_duration(uint32_t delay)
963{
Brian Swetland03e00cd2009-07-01 17:58:37 -0700964 struct msm_dem_slave_data *ptr;
965
966 ptr = smem_find(SMEM_APPS_DEM_SLAVE_DATA, sizeof(*ptr));
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -0700967 if (ptr == NULL) {
968 pr_err("smsm_set_sleep_duration <SM NO APPS_DEM_SLAVE_DATA>\n");
969 return -EIO;
970 }
971 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
972 pr_info("smsm_set_sleep_duration %d -> %d\n",
973 ptr->sleep_time, delay);
974 ptr->sleep_time = delay;
975 return 0;
976}
977
978#else
979
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700980int smsm_set_sleep_duration(uint32_t delay)
981{
982 uint32_t *ptr;
983
Brian Swetland03e00cd2009-07-01 17:58:37 -0700984 ptr = smem_find(SMEM_SMSM_SLEEP_DELAY, sizeof(*ptr));
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700985 if (ptr == NULL) {
986 pr_err("smsm_set_sleep_duration <SM NO SLEEP_DELAY>\n");
987 return -EIO;
988 }
989 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
990 pr_info("smsm_set_sleep_duration %d -> %d\n",
991 *ptr, delay);
992 *ptr = delay;
993 return 0;
994}
995
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -0700996#endif
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700997
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700998int smd_core_init(void)
999{
1000 int r;
1001 pr_info("smd_core_init()\n");
1002
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001003 /* wait for essential items to be initialized */
1004 for (;;) {
1005 unsigned size;
1006 void *state;
1007 state = smem_item(SMEM_SMSM_SHARED_STATE, &size);
Arve Hjønnevåg28379412009-05-20 16:52:36 -07001008 if (size == SMSM_V1_SIZE || size == SMSM_V2_SIZE) {
1009 smd_info.state = (unsigned)state;
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001010 break;
1011 }
1012 }
1013
1014 smd_info.ready = 1;
1015
Brian Swetland37521a32009-07-01 18:30:47 -07001016 r = request_irq(INT_A9_M2A_0, smd_modem_irq_handler,
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001017 IRQF_TRIGGER_RISING, "smd_dev", 0);
1018 if (r < 0)
1019 return r;
1020 r = enable_irq_wake(INT_A9_M2A_0);
1021 if (r < 0)
1022 pr_err("smd_core_init: enable_irq_wake failed for A9_M2A_0\n");
1023
1024 r = request_irq(INT_A9_M2A_5, smsm_irq_handler,
1025 IRQF_TRIGGER_RISING, "smsm_dev", 0);
1026 if (r < 0) {
1027 free_irq(INT_A9_M2A_0, 0);
1028 return r;
1029 }
1030 r = enable_irq_wake(INT_A9_M2A_5);
1031 if (r < 0)
1032 pr_err("smd_core_init: enable_irq_wake failed for A9_M2A_5\n");
1033
Brian Swetland37521a32009-07-01 18:30:47 -07001034#if defined(CONFIG_QDSP6)
1035 r = request_irq(INT_ADSP_A11, smd_dsp_irq_handler,
1036 IRQF_TRIGGER_RISING, "smd_dsp", 0);
1037 if (r < 0) {
1038 free_irq(INT_A9_M2A_0, 0);
1039 free_irq(INT_A9_M2A_5, 0);
1040 return r;
1041 }
1042#endif
1043
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001044 /* check for any SMD channels that may already exist */
1045 do_smd_probe();
1046
1047 /* indicate that we're up and running */
Arve Hjønnevåg28379412009-05-20 16:52:36 -07001048 smsm_change_state(SMSM_STATE_APPS,
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -07001049 ~0, SMSM_INIT | SMSM_SMDINIT | SMSM_RPCINIT | SMSM_RUN);
1050#ifdef CONFIG_ARCH_MSM_SCORPION
1051 smsm_change_state(SMSM_STATE_APPS_DEM, ~0, 0);
1052#endif
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001053
1054 pr_info("smd_core_init() done\n");
1055
1056 return 0;
1057}
1058
Iliyan Malchev1207bab2009-11-15 18:16:43 -08001059extern void msm_init_last_radio_log(struct module *);
1060
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001061static int __init msm_smd_probe(struct platform_device *pdev)
1062{
1063 pr_info("smd_init()\n");
1064
1065 INIT_WORK(&probe_work, smd_channel_probe_worker);
1066
1067 if (smd_core_init()) {
1068 pr_err("smd_core_init() failed\n");
1069 return -1;
1070 }
1071
1072 do_smd_probe();
1073
1074 msm_check_for_modem_crash = check_for_modem_crash;
1075
Iliyan Malchev1207bab2009-11-15 18:16:43 -08001076 msm_init_last_radio_log(THIS_MODULE);
1077
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001078 smd_initialized = 1;
1079
1080 return 0;
1081}
1082
1083static struct platform_driver msm_smd_driver = {
1084 .probe = msm_smd_probe,
1085 .driver = {
1086 .name = MODULE_NAME,
1087 .owner = THIS_MODULE,
1088 },
1089};
1090
1091static int __init msm_smd_init(void)
1092{
1093 return platform_driver_register(&msm_smd_driver);
1094}
1095
1096module_init(msm_smd_init);
1097
1098MODULE_DESCRIPTION("MSM Shared Memory Core");
1099MODULE_AUTHOR("Brian Swetland <swetland@google.com>");
1100MODULE_LICENSE("GPL");