blob: f4e475c493025d1403ba89af16ff4606222c18f9 [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
David Brown9be58f32010-11-12 13:49:27 -080017#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
Brian Swetland2eb44eb2008-09-29 16:00:48 -070019#include <linux/platform_device.h>
20#include <linux/module.h>
21#include <linux/fs.h>
22#include <linux/cdev.h>
23#include <linux/device.h>
24#include <linux/wait.h>
25#include <linux/interrupt.h>
26#include <linux/irq.h>
27#include <linux/list.h>
28#include <linux/slab.h>
29#include <linux/debugfs.h>
30#include <linux/delay.h>
Brian Swetland2eb44eb2008-09-29 16:00:48 -070031
32#include <mach/msm_smd.h>
Brian Swetland2eb44eb2008-09-29 16:00:48 -070033#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;
Arnd Bergmanna1b478e2012-09-14 20:20:43 +000055 void __iomem *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 = {
Arnd Bergmanna1b478e2012-09-14 20:20:43 +000061 /* FIXME: not a real __iomem pointer */
62 .state = &dummy_state,
Brian Swetland5b0f5a32009-04-26 18:38:49 -070063};
64
Brian Swetland2eb44eb2008-09-29 16:00:48 -070065module_param_named(debug_mask, msm_smd_debug_mask,
66 int, S_IRUGO | S_IWUSR | S_IWGRP);
67
Brian Swetland2eb44eb2008-09-29 16:00:48 -070068static unsigned last_heap_free = 0xffffffff;
69
Brian Swetland2eb44eb2008-09-29 16:00:48 -070070static inline void notify_other_smsm(void)
71{
Dima Zavinb42dc442010-01-29 11:43:42 -080072 msm_a2m_int(5);
Brian Swetland37521a32009-07-01 18:30:47 -070073#ifdef CONFIG_QDSP6
Dima Zavinb42dc442010-01-29 11:43:42 -080074 msm_a2m_int(8);
Brian Swetland37521a32009-07-01 18:30:47 -070075#endif
Brian Swetland2eb44eb2008-09-29 16:00:48 -070076}
77
Brian Swetland5b0f5a32009-04-26 18:38:49 -070078static inline void notify_modem_smd(void)
Brian Swetland2eb44eb2008-09-29 16:00:48 -070079{
Dima Zavinb42dc442010-01-29 11:43:42 -080080 msm_a2m_int(0);
Brian Swetland2eb44eb2008-09-29 16:00:48 -070081}
82
Brian Swetland5b0f5a32009-04-26 18:38:49 -070083static inline void notify_dsp_smd(void)
84{
Dima Zavinb42dc442010-01-29 11:43:42 -080085 msm_a2m_int(8);
Brian Swetland5b0f5a32009-04-26 18:38:49 -070086}
87
Brian Swetland2eb44eb2008-09-29 16:00:48 -070088static void smd_diag(void)
89{
90 char *x;
91
92 x = smem_find(ID_DIAG_ERR_MSG, SZ_DIAG_ERR_MSG);
93 if (x != 0) {
94 x[SZ_DIAG_ERR_MSG - 1] = 0;
David Brown9be58f32010-11-12 13:49:27 -080095 pr_debug("DIAG '%s'\n", x);
Brian Swetland2eb44eb2008-09-29 16:00:48 -070096 }
97}
98
99/* call when SMSM_RESET flag is set in the A9's smsm_state */
100static void handle_modem_crash(void)
101{
102 pr_err("ARM9 has CRASHED\n");
103 smd_diag();
104
105 /* hard reboot if possible */
106 if (msm_hw_reset_hook)
107 msm_hw_reset_hook();
108
109 /* in this case the modem or watchdog should reboot us */
110 for (;;)
111 ;
112}
113
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700114uint32_t raw_smsm_get_state(enum smsm_state_item item)
115{
116 return readl(smd_info.state + item * 4);
117}
118
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700119static int check_for_modem_crash(void)
120{
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700121 if (raw_smsm_get_state(SMSM_STATE_MODEM) & SMSM_RESET) {
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700122 handle_modem_crash();
123 return -1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700124 }
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700125 return 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700126}
127
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700128/* the spinlock is used to synchronize between the
Brian Swetland03e00cd2009-07-01 17:58:37 -0700129 * irq handler and code that mutates the channel
130 * list or fiddles with channel state
131 */
132DEFINE_SPINLOCK(smd_lock);
133DEFINE_SPINLOCK(smem_lock);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700134
135/* the mutex is used during open() and close()
Brian Swetland03e00cd2009-07-01 17:58:37 -0700136 * operations to avoid races while creating or
137 * destroying smd_channel structures
138 */
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700139static DEFINE_MUTEX(smd_creation_mutex);
140
141static int smd_initialized;
142
Brian Swetland03e00cd2009-07-01 17:58:37 -0700143LIST_HEAD(smd_ch_closed_list);
Brian Swetland37521a32009-07-01 18:30:47 -0700144LIST_HEAD(smd_ch_list_modem);
145LIST_HEAD(smd_ch_list_dsp);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700146
147static unsigned char smd_ch_allocated[64];
148static struct work_struct probe_work;
149
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700150/* how many bytes are available for reading */
151static int smd_stream_read_avail(struct smd_channel *ch)
152{
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700153 return (ch->recv->head - ch->recv->tail) & ch->fifo_mask;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700154}
155
156/* how many bytes we are free to write */
157static int smd_stream_write_avail(struct smd_channel *ch)
158{
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700159 return ch->fifo_mask -
160 ((ch->send->head - ch->send->tail) & ch->fifo_mask);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700161}
162
163static int smd_packet_read_avail(struct smd_channel *ch)
164{
165 if (ch->current_packet) {
166 int n = smd_stream_read_avail(ch);
167 if (n > ch->current_packet)
168 n = ch->current_packet;
169 return n;
170 } else {
171 return 0;
172 }
173}
174
175static int smd_packet_write_avail(struct smd_channel *ch)
176{
177 int n = smd_stream_write_avail(ch);
178 return n > SMD_HEADER_SIZE ? n - SMD_HEADER_SIZE : 0;
179}
180
181static int ch_is_open(struct smd_channel *ch)
182{
183 return (ch->recv->state == SMD_SS_OPENED) &&
184 (ch->send->state == SMD_SS_OPENED);
185}
186
187/* provide a pointer and length to readable data in the fifo */
188static unsigned ch_read_buffer(struct smd_channel *ch, void **ptr)
189{
190 unsigned head = ch->recv->head;
191 unsigned tail = ch->recv->tail;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700192 *ptr = (void *) (ch->recv_data + tail);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700193
194 if (tail <= head)
195 return head - tail;
196 else
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700197 return ch->fifo_size - tail;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700198}
199
200/* advance the fifo read pointer after data from ch_read_buffer is consumed */
201static void ch_read_done(struct smd_channel *ch, unsigned count)
202{
203 BUG_ON(count > smd_stream_read_avail(ch));
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700204 ch->recv->tail = (ch->recv->tail + count) & ch->fifo_mask;
Haley Teng7632fba2009-10-12 10:38:10 -0700205 ch->send->fTAIL = 1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700206}
207
208/* basic read interface to ch_read_{buffer,done} used
Brian Swetland03e00cd2009-07-01 17:58:37 -0700209 * by smd_*_read() and update_packet_state()
210 * will read-and-discard if the _data pointer is null
211 */
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700212static int ch_read(struct smd_channel *ch, void *_data, int len)
213{
214 void *ptr;
215 unsigned n;
216 unsigned char *data = _data;
217 int orig_len = len;
218
219 while (len > 0) {
220 n = ch_read_buffer(ch, &ptr);
221 if (n == 0)
222 break;
223
224 if (n > len)
225 n = len;
226 if (_data)
227 memcpy(data, ptr, n);
228
229 data += n;
230 len -= n;
231 ch_read_done(ch, n);
232 }
233
234 return orig_len - len;
235}
236
237static void update_stream_state(struct smd_channel *ch)
238{
239 /* streams have no special state requiring updating */
240}
241
242static void update_packet_state(struct smd_channel *ch)
243{
244 unsigned hdr[5];
245 int r;
246
247 /* can't do anything if we're in the middle of a packet */
248 if (ch->current_packet != 0)
249 return;
250
251 /* don't bother unless we can get the full header */
252 if (smd_stream_read_avail(ch) < SMD_HEADER_SIZE)
253 return;
254
255 r = ch_read(ch, hdr, SMD_HEADER_SIZE);
256 BUG_ON(r != SMD_HEADER_SIZE);
257
258 ch->current_packet = hdr[0];
259}
260
261/* provide a pointer and length to next free space in the fifo */
262static unsigned ch_write_buffer(struct smd_channel *ch, void **ptr)
263{
264 unsigned head = ch->send->head;
265 unsigned tail = ch->send->tail;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700266 *ptr = (void *) (ch->send_data + head);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700267
268 if (head < tail) {
269 return tail - head - 1;
270 } else {
271 if (tail == 0)
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700272 return ch->fifo_size - head - 1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700273 else
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700274 return ch->fifo_size - head;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700275 }
276}
277
278/* advace the fifo write pointer after freespace
279 * from ch_write_buffer is filled
280 */
281static void ch_write_done(struct smd_channel *ch, unsigned count)
282{
283 BUG_ON(count > smd_stream_write_avail(ch));
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700284 ch->send->head = (ch->send->head + count) & ch->fifo_mask;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700285 ch->send->fHEAD = 1;
286}
287
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700288static void ch_set_state(struct smd_channel *ch, unsigned n)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700289{
290 if (n == SMD_SS_OPENED) {
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700291 ch->send->fDSR = 1;
292 ch->send->fCTS = 1;
293 ch->send->fCD = 1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700294 } else {
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700295 ch->send->fDSR = 0;
296 ch->send->fCTS = 0;
297 ch->send->fCD = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700298 }
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700299 ch->send->state = n;
300 ch->send->fSTATE = 1;
301 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700302}
303
304static void do_smd_probe(void)
305{
306 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
307 if (shared->heap_info.free_offset != last_heap_free) {
308 last_heap_free = shared->heap_info.free_offset;
309 schedule_work(&probe_work);
310 }
311}
312
313static void smd_state_change(struct smd_channel *ch,
314 unsigned last, unsigned next)
315{
316 ch->last_state = next;
317
David Brown9be58f32010-11-12 13:49:27 -0800318 pr_debug("ch %d %d -> %d\n", ch->n, last, next);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700319
320 switch (next) {
321 case SMD_SS_OPENING:
322 ch->recv->tail = 0;
323 case SMD_SS_OPENED:
324 if (ch->send->state != SMD_SS_OPENED)
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700325 ch_set_state(ch, SMD_SS_OPENED);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700326 ch->notify(ch->priv, SMD_EVENT_OPEN);
327 break;
328 case SMD_SS_FLUSHING:
329 case SMD_SS_RESET:
330 /* we should force them to close? */
331 default:
332 ch->notify(ch->priv, SMD_EVENT_CLOSE);
333 }
334}
335
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700336static void handle_smd_irq(struct list_head *list, void (*notify)(void))
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700337{
338 unsigned long flags;
339 struct smd_channel *ch;
340 int do_notify = 0;
341 unsigned ch_flags;
342 unsigned tmp;
343
344 spin_lock_irqsave(&smd_lock, flags);
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700345 list_for_each_entry(ch, list, ch_list) {
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700346 ch_flags = 0;
347 if (ch_is_open(ch)) {
348 if (ch->recv->fHEAD) {
349 ch->recv->fHEAD = 0;
350 ch_flags |= 1;
351 do_notify |= 1;
352 }
353 if (ch->recv->fTAIL) {
354 ch->recv->fTAIL = 0;
355 ch_flags |= 2;
356 do_notify |= 1;
357 }
358 if (ch->recv->fSTATE) {
359 ch->recv->fSTATE = 0;
360 ch_flags |= 4;
361 do_notify |= 1;
362 }
363 }
364 tmp = ch->recv->state;
365 if (tmp != ch->last_state)
366 smd_state_change(ch, ch->last_state, tmp);
367 if (ch_flags) {
368 ch->update_state(ch);
369 ch->notify(ch->priv, SMD_EVENT_DATA);
370 }
371 }
372 if (do_notify)
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700373 notify();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700374 spin_unlock_irqrestore(&smd_lock, flags);
375 do_smd_probe();
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700376}
377
Brian Swetland37521a32009-07-01 18:30:47 -0700378static irqreturn_t smd_modem_irq_handler(int irq, void *data)
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700379{
Brian Swetland37521a32009-07-01 18:30:47 -0700380 handle_smd_irq(&smd_ch_list_modem, notify_modem_smd);
381 return IRQ_HANDLED;
382}
383
Daniel Walkerb13525c2010-03-18 10:10:30 -0700384#if defined(CONFIG_QDSP6)
Brian Swetland37521a32009-07-01 18:30:47 -0700385static irqreturn_t smd_dsp_irq_handler(int irq, void *data)
386{
387 handle_smd_irq(&smd_ch_list_dsp, notify_dsp_smd);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700388 return IRQ_HANDLED;
389}
Daniel Walkerb13525c2010-03-18 10:10:30 -0700390#endif
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700391
392static void smd_fake_irq_handler(unsigned long arg)
393{
Brian Swetland37521a32009-07-01 18:30:47 -0700394 handle_smd_irq(&smd_ch_list_modem, notify_modem_smd);
395 handle_smd_irq(&smd_ch_list_dsp, notify_dsp_smd);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700396}
397
398static DECLARE_TASKLET(smd_fake_irq_tasklet, smd_fake_irq_handler, 0);
399
Brian Swetland37521a32009-07-01 18:30:47 -0700400static inline int smd_need_int(struct smd_channel *ch)
401{
402 if (ch_is_open(ch)) {
403 if (ch->recv->fHEAD || ch->recv->fTAIL || ch->recv->fSTATE)
404 return 1;
405 if (ch->recv->state != ch->last_state)
406 return 1;
407 }
408 return 0;
409}
410
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700411void smd_sleep_exit(void)
412{
413 unsigned long flags;
414 struct smd_channel *ch;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700415 int need_int = 0;
416
417 spin_lock_irqsave(&smd_lock, flags);
Brian Swetland37521a32009-07-01 18:30:47 -0700418 list_for_each_entry(ch, &smd_ch_list_modem, ch_list) {
419 if (smd_need_int(ch)) {
420 need_int = 1;
421 break;
422 }
423 }
424 list_for_each_entry(ch, &smd_ch_list_dsp, ch_list) {
425 if (smd_need_int(ch)) {
426 need_int = 1;
427 break;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700428 }
429 }
430 spin_unlock_irqrestore(&smd_lock, flags);
431 do_smd_probe();
Brian Swetland37521a32009-07-01 18:30:47 -0700432
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700433 if (need_int) {
434 if (msm_smd_debug_mask & MSM_SMD_DEBUG)
435 pr_info("smd_sleep_exit need interrupt\n");
436 tasklet_schedule(&smd_fake_irq_tasklet);
437 }
438}
439
440
441void smd_kick(smd_channel_t *ch)
442{
443 unsigned long flags;
444 unsigned tmp;
445
446 spin_lock_irqsave(&smd_lock, flags);
447 ch->update_state(ch);
448 tmp = ch->recv->state;
449 if (tmp != ch->last_state) {
450 ch->last_state = tmp;
451 if (tmp == SMD_SS_OPENED)
452 ch->notify(ch->priv, SMD_EVENT_OPEN);
453 else
454 ch->notify(ch->priv, SMD_EVENT_CLOSE);
455 }
456 ch->notify(ch->priv, SMD_EVENT_DATA);
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700457 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700458 spin_unlock_irqrestore(&smd_lock, flags);
459}
460
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700461static int smd_is_packet(int chn, unsigned type)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700462{
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700463 type &= SMD_KIND_MASK;
464 if (type == SMD_KIND_PACKET)
465 return 1;
466 if (type == SMD_KIND_STREAM)
467 return 0;
468
469 /* older AMSS reports SMD_KIND_UNKNOWN always */
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700470 if ((chn > 4) || (chn == 1))
471 return 1;
472 else
473 return 0;
474}
475
476static int smd_stream_write(smd_channel_t *ch, const void *_data, int len)
477{
478 void *ptr;
479 const unsigned char *buf = _data;
480 unsigned xfer;
481 int orig_len = len;
482
483 if (len < 0)
484 return -EINVAL;
485
486 while ((xfer = ch_write_buffer(ch, &ptr)) != 0) {
487 if (!ch_is_open(ch))
488 break;
489 if (xfer > len)
490 xfer = len;
491 memcpy(ptr, buf, xfer);
492 ch_write_done(ch, xfer);
493 len -= xfer;
494 buf += xfer;
495 if (len == 0)
496 break;
497 }
498
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700499 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700500
501 return orig_len - len;
502}
503
504static int smd_packet_write(smd_channel_t *ch, const void *_data, int len)
505{
506 unsigned hdr[5];
507
508 if (len < 0)
509 return -EINVAL;
510
511 if (smd_stream_write_avail(ch) < (len + SMD_HEADER_SIZE))
512 return -ENOMEM;
513
514 hdr[0] = len;
515 hdr[1] = hdr[2] = hdr[3] = hdr[4] = 0;
516
517 smd_stream_write(ch, hdr, sizeof(hdr));
518 smd_stream_write(ch, _data, len);
519
520 return len;
521}
522
523static int smd_stream_read(smd_channel_t *ch, void *data, int len)
524{
525 int r;
526
527 if (len < 0)
528 return -EINVAL;
529
530 r = ch_read(ch, data, len);
531 if (r > 0)
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700532 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700533
534 return r;
535}
536
537static int smd_packet_read(smd_channel_t *ch, void *data, int len)
538{
539 unsigned long flags;
540 int r;
541
542 if (len < 0)
543 return -EINVAL;
544
545 if (len > ch->current_packet)
546 len = ch->current_packet;
547
548 r = ch_read(ch, data, len);
549 if (r > 0)
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700550 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700551
552 spin_lock_irqsave(&smd_lock, flags);
553 ch->current_packet -= r;
554 update_packet_state(ch);
555 spin_unlock_irqrestore(&smd_lock, flags);
556
557 return r;
558}
559
Brian Swetland34f719b2009-10-30 16:22:05 -0700560static int smd_alloc_channel(const char *name, uint32_t cid, uint32_t type)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700561{
562 struct smd_channel *ch;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700563
564 ch = kzalloc(sizeof(struct smd_channel), GFP_KERNEL);
565 if (ch == 0) {
566 pr_err("smd_alloc_channel() out of memory\n");
Brian Swetland34f719b2009-10-30 16:22:05 -0700567 return -1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700568 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700569 ch->n = cid;
570
Daniel Walkerbf83de42010-03-16 16:29:44 -0700571 if (_smd_alloc_channel(ch)) {
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700572 kfree(ch);
Brian Swetland34f719b2009-10-30 16:22:05 -0700573 return -1;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700574 }
575
576 ch->fifo_mask = ch->fifo_size - 1;
577 ch->type = type;
578
579 if ((type & SMD_TYPE_MASK) == SMD_TYPE_APPS_MODEM)
580 ch->notify_other_cpu = notify_modem_smd;
581 else
582 ch->notify_other_cpu = notify_dsp_smd;
583
584 if (smd_is_packet(cid, type)) {
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700585 ch->read = smd_packet_read;
586 ch->write = smd_packet_write;
587 ch->read_avail = smd_packet_read_avail;
588 ch->write_avail = smd_packet_write_avail;
589 ch->update_state = update_packet_state;
590 } else {
591 ch->read = smd_stream_read;
592 ch->write = smd_stream_write;
593 ch->read_avail = smd_stream_read_avail;
594 ch->write_avail = smd_stream_write_avail;
595 ch->update_state = update_stream_state;
596 }
597
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700598 if ((type & 0xff) == 0)
599 memcpy(ch->name, "SMD_", 4);
600 else
601 memcpy(ch->name, "DSP_", 4);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700602 memcpy(ch->name + 4, name, 20);
603 ch->name[23] = 0;
604 ch->pdev.name = ch->name;
605 ch->pdev.id = -1;
606
David Brown9be58f32010-11-12 13:49:27 -0800607 pr_debug("smd_alloc_channel() cid=%02d size=%05d '%s'\n",
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700608 ch->n, ch->fifo_size, ch->name);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700609
610 mutex_lock(&smd_creation_mutex);
611 list_add(&ch->ch_list, &smd_ch_closed_list);
612 mutex_unlock(&smd_creation_mutex);
613
614 platform_device_register(&ch->pdev);
Brian Swetland34f719b2009-10-30 16:22:05 -0700615 return 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700616}
617
Daniel Walker3843ac32010-03-17 11:10:40 -0700618static void smd_channel_probe_worker(struct work_struct *work)
619{
620 struct smd_alloc_elm *shared;
621 unsigned ctype;
622 unsigned type;
623 unsigned n;
624
625 shared = smem_find(ID_CH_ALLOC_TBL, sizeof(*shared) * 64);
626 if (!shared) {
David Brown9be58f32010-11-12 13:49:27 -0800627 pr_err("cannot find allocation table\n");
Daniel Walker3843ac32010-03-17 11:10:40 -0700628 return;
629 }
630 for (n = 0; n < 64; n++) {
631 if (smd_ch_allocated[n])
632 continue;
633 if (!shared[n].ref_count)
634 continue;
635 if (!shared[n].name[0])
636 continue;
637 ctype = shared[n].ctype;
638 type = ctype & SMD_TYPE_MASK;
639
640 /* DAL channels are stream but neither the modem,
641 * nor the DSP correctly indicate this. Fixup manually.
642 */
643 if (!memcmp(shared[n].name, "DAL", 3))
644 ctype = (ctype & (~SMD_KIND_MASK)) | SMD_KIND_STREAM;
645
646 type = shared[n].ctype & SMD_TYPE_MASK;
647 if ((type == SMD_TYPE_APPS_MODEM) ||
648 (type == SMD_TYPE_APPS_DSP))
649 if (!smd_alloc_channel(shared[n].name, shared[n].cid, ctype))
650 smd_ch_allocated[n] = 1;
651 }
652}
653
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700654static void do_nothing_notify(void *priv, unsigned flags)
655{
656}
657
658struct smd_channel *smd_get_channel(const char *name)
659{
660 struct smd_channel *ch;
661
662 mutex_lock(&smd_creation_mutex);
663 list_for_each_entry(ch, &smd_ch_closed_list, ch_list) {
664 if (!strcmp(name, ch->name)) {
665 list_del(&ch->ch_list);
666 mutex_unlock(&smd_creation_mutex);
667 return ch;
668 }
669 }
670 mutex_unlock(&smd_creation_mutex);
671
672 return NULL;
673}
674
675int smd_open(const char *name, smd_channel_t **_ch,
676 void *priv, void (*notify)(void *, unsigned))
677{
678 struct smd_channel *ch;
679 unsigned long flags;
680
681 if (smd_initialized == 0) {
682 pr_info("smd_open() before smd_init()\n");
683 return -ENODEV;
684 }
685
686 ch = smd_get_channel(name);
687 if (!ch)
688 return -ENODEV;
689
690 if (notify == 0)
691 notify = do_nothing_notify;
692
693 ch->notify = notify;
694 ch->current_packet = 0;
695 ch->last_state = SMD_SS_CLOSED;
696 ch->priv = priv;
697
698 *_ch = ch;
699
700 spin_lock_irqsave(&smd_lock, flags);
Brian Swetland37521a32009-07-01 18:30:47 -0700701
702 if ((ch->type & SMD_TYPE_MASK) == SMD_TYPE_APPS_MODEM)
703 list_add(&ch->ch_list, &smd_ch_list_modem);
704 else
705 list_add(&ch->ch_list, &smd_ch_list_dsp);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700706
707 /* If the remote side is CLOSING, we need to get it to
708 * move to OPENING (which we'll do by moving from CLOSED to
709 * OPENING) and then get it to move from OPENING to
710 * OPENED (by doing the same state change ourselves).
711 *
712 * Otherwise, it should be OPENING and we can move directly
713 * to OPENED so that it will follow.
714 */
715 if (ch->recv->state == SMD_SS_CLOSING) {
716 ch->send->head = 0;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700717 ch_set_state(ch, SMD_SS_OPENING);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700718 } else {
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700719 ch_set_state(ch, SMD_SS_OPENED);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700720 }
721 spin_unlock_irqrestore(&smd_lock, flags);
722 smd_kick(ch);
723
724 return 0;
725}
726
727int smd_close(smd_channel_t *ch)
728{
729 unsigned long flags;
730
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700731 if (ch == 0)
732 return -1;
733
734 spin_lock_irqsave(&smd_lock, flags);
735 ch->notify = do_nothing_notify;
736 list_del(&ch->ch_list);
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700737 ch_set_state(ch, SMD_SS_CLOSED);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700738 spin_unlock_irqrestore(&smd_lock, flags);
739
740 mutex_lock(&smd_creation_mutex);
741 list_add(&ch->ch_list, &smd_ch_closed_list);
742 mutex_unlock(&smd_creation_mutex);
743
744 return 0;
745}
746
747int smd_read(smd_channel_t *ch, void *data, int len)
748{
749 return ch->read(ch, data, len);
750}
751
752int smd_write(smd_channel_t *ch, const void *data, int len)
753{
754 return ch->write(ch, data, len);
755}
756
Brian Swetland636eb9c2009-12-07 15:28:08 -0800757int smd_write_atomic(smd_channel_t *ch, const void *data, int len)
758{
759 unsigned long flags;
760 int res;
761 spin_lock_irqsave(&smd_lock, flags);
762 res = ch->write(ch, data, len);
763 spin_unlock_irqrestore(&smd_lock, flags);
764 return res;
765}
766
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700767int smd_read_avail(smd_channel_t *ch)
768{
769 return ch->read_avail(ch);
770}
771
772int smd_write_avail(smd_channel_t *ch)
773{
774 return ch->write_avail(ch);
775}
776
777int smd_wait_until_readable(smd_channel_t *ch, int bytes)
778{
779 return -1;
780}
781
782int smd_wait_until_writable(smd_channel_t *ch, int bytes)
783{
784 return -1;
785}
786
787int smd_cur_packet_size(smd_channel_t *ch)
788{
789 return ch->current_packet;
790}
791
792
793/* ------------------------------------------------------------------------- */
794
795void *smem_alloc(unsigned id, unsigned size)
796{
797 return smem_find(id, size);
798}
799
Arnd Bergmanna1b478e2012-09-14 20:20:43 +0000800void __iomem *smem_item(unsigned id, unsigned *size)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700801{
802 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
803 struct smem_heap_entry *toc = shared->heap_toc;
804
805 if (id >= SMEM_NUM_ITEMS)
Arnd Bergmanna1b478e2012-09-14 20:20:43 +0000806 return NULL;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700807
808 if (toc[id].allocated) {
809 *size = toc[id].size;
Arnd Bergmanna1b478e2012-09-14 20:20:43 +0000810 return (MSM_SHARED_RAM_BASE + toc[id].offset);
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700811 } else {
812 *size = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700813 }
814
Arnd Bergmanna1b478e2012-09-14 20:20:43 +0000815 return NULL;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700816}
817
818void *smem_find(unsigned id, unsigned size_in)
819{
820 unsigned size;
821 void *ptr;
822
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700823 ptr = smem_item(id, &size);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700824 if (!ptr)
825 return 0;
826
827 size_in = ALIGN(size_in, 8);
828 if (size_in != size) {
829 pr_err("smem_find(%d, %d): wrong size %d\n",
830 id, size_in, size);
831 return 0;
832 }
833
834 return ptr;
835}
836
837static irqreturn_t smsm_irq_handler(int irq, void *data)
838{
839 unsigned long flags;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700840 unsigned apps, modm;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700841
842 spin_lock_irqsave(&smem_lock, flags);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700843
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700844 apps = raw_smsm_get_state(SMSM_STATE_APPS);
845 modm = raw_smsm_get_state(SMSM_STATE_MODEM);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700846
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700847 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
848 pr_info("<SM %08x %08x>\n", apps, modm);
Daniel Walker79848a22010-03-16 15:20:07 -0700849 if (modm & SMSM_RESET)
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700850 handle_modem_crash();
Daniel Walker79848a22010-03-16 15:20:07 -0700851
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700852 do_smd_probe();
853
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700854 spin_unlock_irqrestore(&smem_lock, flags);
855 return IRQ_HANDLED;
856}
857
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700858int smsm_change_state(enum smsm_state_item item,
859 uint32_t clear_mask, uint32_t set_mask)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700860{
Arnd Bergmanna1b478e2012-09-14 20:20:43 +0000861 void __iomem *addr = smd_info.state + item * 4;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700862 unsigned long flags;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700863 unsigned state;
864
865 if (!smd_info.ready)
866 return -EIO;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700867
868 spin_lock_irqsave(&smem_lock, flags);
869
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700870 if (raw_smsm_get_state(SMSM_STATE_MODEM) & SMSM_RESET)
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700871 handle_modem_crash();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700872
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700873 state = (readl(addr) & ~clear_mask) | set_mask;
874 writel(state, addr);
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700875
876 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700877 pr_info("smsm_change_state %d %x\n", item, state);
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700878 notify_other_smsm();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700879
880 spin_unlock_irqrestore(&smem_lock, flags);
881
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700882 return 0;
883}
884
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700885uint32_t smsm_get_state(enum smsm_state_item item)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700886{
887 unsigned long flags;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700888 uint32_t rv;
889
890 spin_lock_irqsave(&smem_lock, flags);
891
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700892 rv = readl(smd_info.state + item * 4);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700893
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700894 if (item == SMSM_STATE_MODEM && (rv & SMSM_RESET))
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700895 handle_modem_crash();
896
897 spin_unlock_irqrestore(&smem_lock, flags);
898
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700899 return rv;
900}
901
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -0700902#ifdef CONFIG_ARCH_MSM_SCORPION
903
904int smsm_set_sleep_duration(uint32_t delay)
905{
Brian Swetland03e00cd2009-07-01 17:58:37 -0700906 struct msm_dem_slave_data *ptr;
907
908 ptr = smem_find(SMEM_APPS_DEM_SLAVE_DATA, sizeof(*ptr));
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -0700909 if (ptr == NULL) {
910 pr_err("smsm_set_sleep_duration <SM NO APPS_DEM_SLAVE_DATA>\n");
911 return -EIO;
912 }
913 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
914 pr_info("smsm_set_sleep_duration %d -> %d\n",
915 ptr->sleep_time, delay);
916 ptr->sleep_time = delay;
917 return 0;
918}
919
920#else
921
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700922int smsm_set_sleep_duration(uint32_t delay)
923{
924 uint32_t *ptr;
925
Brian Swetland03e00cd2009-07-01 17:58:37 -0700926 ptr = smem_find(SMEM_SMSM_SLEEP_DELAY, sizeof(*ptr));
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700927 if (ptr == NULL) {
928 pr_err("smsm_set_sleep_duration <SM NO SLEEP_DELAY>\n");
929 return -EIO;
930 }
931 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
932 pr_info("smsm_set_sleep_duration %d -> %d\n",
933 *ptr, delay);
934 *ptr = delay;
935 return 0;
936}
937
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -0700938#endif
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700939
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700940int smd_core_init(void)
941{
942 int r;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700943
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700944 /* wait for essential items to be initialized */
945 for (;;) {
946 unsigned size;
Arnd Bergmanna1b478e2012-09-14 20:20:43 +0000947 void __iomem *state;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700948 state = smem_item(SMEM_SMSM_SHARED_STATE, &size);
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700949 if (size == SMSM_V1_SIZE || size == SMSM_V2_SIZE) {
Arnd Bergmanna1b478e2012-09-14 20:20:43 +0000950 smd_info.state = state;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700951 break;
952 }
953 }
954
955 smd_info.ready = 1;
956
Brian Swetland37521a32009-07-01 18:30:47 -0700957 r = request_irq(INT_A9_M2A_0, smd_modem_irq_handler,
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700958 IRQF_TRIGGER_RISING, "smd_dev", 0);
959 if (r < 0)
960 return r;
961 r = enable_irq_wake(INT_A9_M2A_0);
962 if (r < 0)
963 pr_err("smd_core_init: enable_irq_wake failed for A9_M2A_0\n");
964
965 r = request_irq(INT_A9_M2A_5, smsm_irq_handler,
966 IRQF_TRIGGER_RISING, "smsm_dev", 0);
967 if (r < 0) {
968 free_irq(INT_A9_M2A_0, 0);
969 return r;
970 }
971 r = enable_irq_wake(INT_A9_M2A_5);
972 if (r < 0)
973 pr_err("smd_core_init: enable_irq_wake failed for A9_M2A_5\n");
974
Brian Swetland37521a32009-07-01 18:30:47 -0700975#if defined(CONFIG_QDSP6)
976 r = request_irq(INT_ADSP_A11, smd_dsp_irq_handler,
977 IRQF_TRIGGER_RISING, "smd_dsp", 0);
978 if (r < 0) {
979 free_irq(INT_A9_M2A_0, 0);
980 free_irq(INT_A9_M2A_5, 0);
981 return r;
982 }
983#endif
984
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700985 /* check for any SMD channels that may already exist */
986 do_smd_probe();
987
988 /* indicate that we're up and running */
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700989 smsm_change_state(SMSM_STATE_APPS,
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -0700990 ~0, SMSM_INIT | SMSM_SMDINIT | SMSM_RPCINIT | SMSM_RUN);
991#ifdef CONFIG_ARCH_MSM_SCORPION
992 smsm_change_state(SMSM_STATE_APPS_DEM, ~0, 0);
993#endif
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700994
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700995 return 0;
996}
997
Gregory Bean4416e9e2010-07-28 10:22:12 -0700998static int __devinit msm_smd_probe(struct platform_device *pdev)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700999{
Daniel Walker0aec66d2010-03-18 12:31:08 -07001000 /*
1001 * If we haven't waited for the ARM9 to boot up till now,
1002 * then we need to wait here. Otherwise this should just
1003 * return immediately.
1004 */
1005 proc_comm_boot_wait();
1006
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001007 INIT_WORK(&probe_work, smd_channel_probe_worker);
1008
1009 if (smd_core_init()) {
1010 pr_err("smd_core_init() failed\n");
1011 return -1;
1012 }
1013
1014 do_smd_probe();
1015
1016 msm_check_for_modem_crash = check_for_modem_crash;
1017
Iliyan Malchev1207bab2009-11-15 18:16:43 -08001018 msm_init_last_radio_log(THIS_MODULE);
1019
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001020 smd_initialized = 1;
1021
1022 return 0;
1023}
1024
1025static struct platform_driver msm_smd_driver = {
1026 .probe = msm_smd_probe,
1027 .driver = {
1028 .name = MODULE_NAME,
1029 .owner = THIS_MODULE,
1030 },
1031};
1032
1033static int __init msm_smd_init(void)
1034{
1035 return platform_driver_register(&msm_smd_driver);
1036}
1037
1038module_init(msm_smd_init);
1039
1040MODULE_DESCRIPTION("MSM Shared Memory Core");
1041MODULE_AUTHOR("Brian Swetland <swetland@google.com>");
1042MODULE_LICENSE("GPL");