blob: f3558f504709d116133d29734797db9345e1af44 [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>
Brian Swetland2eb44eb2008-09-29 16:00:48 -070029
30#include <mach/msm_smd.h>
Brian Swetland2eb44eb2008-09-29 16:00:48 -070031#include <mach/system.h>
32
33#include "smd_private.h"
34#include "proc_comm.h"
35
Brian Swetland37521a32009-07-01 18:30:47 -070036#if defined(CONFIG_ARCH_QSD8X50)
37#define CONFIG_QDSP6 1
38#endif
39
Brian Swetland2eb44eb2008-09-29 16:00:48 -070040void (*msm_hw_reset_hook)(void);
41
42#define MODULE_NAME "msm_smd"
43
44enum {
45 MSM_SMD_DEBUG = 1U << 0,
46 MSM_SMSM_DEBUG = 1U << 0,
47};
48
49static int msm_smd_debug_mask;
50
Brian Swetland03e00cd2009-07-01 17:58:37 -070051struct shared_info {
Brian Swetland5b0f5a32009-04-26 18:38:49 -070052 int ready;
Arve Hjønnevåg28379412009-05-20 16:52:36 -070053 unsigned state;
Brian Swetland5b0f5a32009-04-26 18:38:49 -070054};
55
Arve Hjønnevåg28379412009-05-20 16:52:36 -070056static unsigned dummy_state[SMSM_STATE_COUNT];
Brian Swetland5b0f5a32009-04-26 18:38:49 -070057
58static struct shared_info smd_info = {
Arve Hjønnevåg28379412009-05-20 16:52:36 -070059 .state = (unsigned) &dummy_state,
Brian Swetland5b0f5a32009-04-26 18:38:49 -070060};
61
Brian Swetland2eb44eb2008-09-29 16:00:48 -070062module_param_named(debug_mask, msm_smd_debug_mask,
63 int, S_IRUGO | S_IWUSR | S_IWGRP);
64
Brian Swetland2eb44eb2008-09-29 16:00:48 -070065static unsigned last_heap_free = 0xffffffff;
66
Brian Swetland2eb44eb2008-09-29 16:00:48 -070067static inline void notify_other_smsm(void)
68{
Dima Zavinb42dc442010-01-29 11:43:42 -080069 msm_a2m_int(5);
Brian Swetland37521a32009-07-01 18:30:47 -070070#ifdef CONFIG_QDSP6
Dima Zavinb42dc442010-01-29 11:43:42 -080071 msm_a2m_int(8);
Brian Swetland37521a32009-07-01 18:30:47 -070072#endif
Brian Swetland2eb44eb2008-09-29 16:00:48 -070073}
74
Brian Swetland5b0f5a32009-04-26 18:38:49 -070075static inline void notify_modem_smd(void)
Brian Swetland2eb44eb2008-09-29 16:00:48 -070076{
Dima Zavinb42dc442010-01-29 11:43:42 -080077 msm_a2m_int(0);
Brian Swetland2eb44eb2008-09-29 16:00:48 -070078}
79
Brian Swetland5b0f5a32009-04-26 18:38:49 -070080static inline void notify_dsp_smd(void)
81{
Dima Zavinb42dc442010-01-29 11:43:42 -080082 msm_a2m_int(8);
Brian Swetland5b0f5a32009-04-26 18:38:49 -070083}
84
Brian Swetland2eb44eb2008-09-29 16:00:48 -070085static void smd_diag(void)
86{
87 char *x;
88
89 x = smem_find(ID_DIAG_ERR_MSG, SZ_DIAG_ERR_MSG);
90 if (x != 0) {
91 x[SZ_DIAG_ERR_MSG - 1] = 0;
92 pr_info("smem: DIAG '%s'\n", x);
93 }
94}
95
96/* call when SMSM_RESET flag is set in the A9's smsm_state */
97static void handle_modem_crash(void)
98{
99 pr_err("ARM9 has CRASHED\n");
100 smd_diag();
101
102 /* hard reboot if possible */
103 if (msm_hw_reset_hook)
104 msm_hw_reset_hook();
105
106 /* in this case the modem or watchdog should reboot us */
107 for (;;)
108 ;
109}
110
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700111uint32_t raw_smsm_get_state(enum smsm_state_item item)
112{
113 return readl(smd_info.state + item * 4);
114}
115
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700116static int check_for_modem_crash(void)
117{
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700118 if (raw_smsm_get_state(SMSM_STATE_MODEM) & SMSM_RESET) {
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700119 handle_modem_crash();
120 return -1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700121 }
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700122 return 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700123}
124
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700125/* the spinlock is used to synchronize between the
Brian Swetland03e00cd2009-07-01 17:58:37 -0700126 * irq handler and code that mutates the channel
127 * list or fiddles with channel state
128 */
129DEFINE_SPINLOCK(smd_lock);
130DEFINE_SPINLOCK(smem_lock);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700131
132/* the mutex is used during open() and close()
Brian Swetland03e00cd2009-07-01 17:58:37 -0700133 * operations to avoid races while creating or
134 * destroying smd_channel structures
135 */
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700136static DEFINE_MUTEX(smd_creation_mutex);
137
138static int smd_initialized;
139
Brian Swetland03e00cd2009-07-01 17:58:37 -0700140LIST_HEAD(smd_ch_closed_list);
Brian Swetland37521a32009-07-01 18:30:47 -0700141LIST_HEAD(smd_ch_list_modem);
142LIST_HEAD(smd_ch_list_dsp);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700143
144static unsigned char smd_ch_allocated[64];
145static struct work_struct probe_work;
146
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700147/* how many bytes are available for reading */
148static int smd_stream_read_avail(struct smd_channel *ch)
149{
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700150 return (ch->recv->head - ch->recv->tail) & ch->fifo_mask;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700151}
152
153/* how many bytes we are free to write */
154static int smd_stream_write_avail(struct smd_channel *ch)
155{
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700156 return ch->fifo_mask -
157 ((ch->send->head - ch->send->tail) & ch->fifo_mask);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700158}
159
160static int smd_packet_read_avail(struct smd_channel *ch)
161{
162 if (ch->current_packet) {
163 int n = smd_stream_read_avail(ch);
164 if (n > ch->current_packet)
165 n = ch->current_packet;
166 return n;
167 } else {
168 return 0;
169 }
170}
171
172static int smd_packet_write_avail(struct smd_channel *ch)
173{
174 int n = smd_stream_write_avail(ch);
175 return n > SMD_HEADER_SIZE ? n - SMD_HEADER_SIZE : 0;
176}
177
178static int ch_is_open(struct smd_channel *ch)
179{
180 return (ch->recv->state == SMD_SS_OPENED) &&
181 (ch->send->state == SMD_SS_OPENED);
182}
183
184/* provide a pointer and length to readable data in the fifo */
185static unsigned ch_read_buffer(struct smd_channel *ch, void **ptr)
186{
187 unsigned head = ch->recv->head;
188 unsigned tail = ch->recv->tail;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700189 *ptr = (void *) (ch->recv_data + tail);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700190
191 if (tail <= head)
192 return head - tail;
193 else
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700194 return ch->fifo_size - tail;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700195}
196
197/* advance the fifo read pointer after data from ch_read_buffer is consumed */
198static void ch_read_done(struct smd_channel *ch, unsigned count)
199{
200 BUG_ON(count > smd_stream_read_avail(ch));
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700201 ch->recv->tail = (ch->recv->tail + count) & ch->fifo_mask;
Haley Teng7632fba2009-10-12 10:38:10 -0700202 ch->send->fTAIL = 1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700203}
204
205/* basic read interface to ch_read_{buffer,done} used
Brian Swetland03e00cd2009-07-01 17:58:37 -0700206 * by smd_*_read() and update_packet_state()
207 * will read-and-discard if the _data pointer is null
208 */
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700209static int ch_read(struct smd_channel *ch, void *_data, int len)
210{
211 void *ptr;
212 unsigned n;
213 unsigned char *data = _data;
214 int orig_len = len;
215
216 while (len > 0) {
217 n = ch_read_buffer(ch, &ptr);
218 if (n == 0)
219 break;
220
221 if (n > len)
222 n = len;
223 if (_data)
224 memcpy(data, ptr, n);
225
226 data += n;
227 len -= n;
228 ch_read_done(ch, n);
229 }
230
231 return orig_len - len;
232}
233
234static void update_stream_state(struct smd_channel *ch)
235{
236 /* streams have no special state requiring updating */
237}
238
239static void update_packet_state(struct smd_channel *ch)
240{
241 unsigned hdr[5];
242 int r;
243
244 /* can't do anything if we're in the middle of a packet */
245 if (ch->current_packet != 0)
246 return;
247
248 /* don't bother unless we can get the full header */
249 if (smd_stream_read_avail(ch) < SMD_HEADER_SIZE)
250 return;
251
252 r = ch_read(ch, hdr, SMD_HEADER_SIZE);
253 BUG_ON(r != SMD_HEADER_SIZE);
254
255 ch->current_packet = hdr[0];
256}
257
258/* provide a pointer and length to next free space in the fifo */
259static unsigned ch_write_buffer(struct smd_channel *ch, void **ptr)
260{
261 unsigned head = ch->send->head;
262 unsigned tail = ch->send->tail;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700263 *ptr = (void *) (ch->send_data + head);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700264
265 if (head < tail) {
266 return tail - head - 1;
267 } else {
268 if (tail == 0)
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700269 return ch->fifo_size - head - 1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700270 else
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700271 return ch->fifo_size - head;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700272 }
273}
274
275/* advace the fifo write pointer after freespace
276 * from ch_write_buffer is filled
277 */
278static void ch_write_done(struct smd_channel *ch, unsigned count)
279{
280 BUG_ON(count > smd_stream_write_avail(ch));
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700281 ch->send->head = (ch->send->head + count) & ch->fifo_mask;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700282 ch->send->fHEAD = 1;
283}
284
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700285static void ch_set_state(struct smd_channel *ch, unsigned n)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700286{
287 if (n == SMD_SS_OPENED) {
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700288 ch->send->fDSR = 1;
289 ch->send->fCTS = 1;
290 ch->send->fCD = 1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700291 } else {
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700292 ch->send->fDSR = 0;
293 ch->send->fCTS = 0;
294 ch->send->fCD = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700295 }
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700296 ch->send->state = n;
297 ch->send->fSTATE = 1;
298 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700299}
300
301static void do_smd_probe(void)
302{
303 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
304 if (shared->heap_info.free_offset != last_heap_free) {
305 last_heap_free = shared->heap_info.free_offset;
306 schedule_work(&probe_work);
307 }
308}
309
310static void smd_state_change(struct smd_channel *ch,
311 unsigned last, unsigned next)
312{
313 ch->last_state = next;
314
Brian Swetland03e00cd2009-07-01 17:58:37 -0700315 pr_info("SMD: ch %d %d -> %d\n", ch->n, last, next);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700316
317 switch (next) {
318 case SMD_SS_OPENING:
319 ch->recv->tail = 0;
320 case SMD_SS_OPENED:
321 if (ch->send->state != SMD_SS_OPENED)
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700322 ch_set_state(ch, SMD_SS_OPENED);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700323 ch->notify(ch->priv, SMD_EVENT_OPEN);
324 break;
325 case SMD_SS_FLUSHING:
326 case SMD_SS_RESET:
327 /* we should force them to close? */
328 default:
329 ch->notify(ch->priv, SMD_EVENT_CLOSE);
330 }
331}
332
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700333static void handle_smd_irq(struct list_head *list, void (*notify)(void))
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700334{
335 unsigned long flags;
336 struct smd_channel *ch;
337 int do_notify = 0;
338 unsigned ch_flags;
339 unsigned tmp;
340
341 spin_lock_irqsave(&smd_lock, flags);
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700342 list_for_each_entry(ch, list, ch_list) {
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700343 ch_flags = 0;
344 if (ch_is_open(ch)) {
345 if (ch->recv->fHEAD) {
346 ch->recv->fHEAD = 0;
347 ch_flags |= 1;
348 do_notify |= 1;
349 }
350 if (ch->recv->fTAIL) {
351 ch->recv->fTAIL = 0;
352 ch_flags |= 2;
353 do_notify |= 1;
354 }
355 if (ch->recv->fSTATE) {
356 ch->recv->fSTATE = 0;
357 ch_flags |= 4;
358 do_notify |= 1;
359 }
360 }
361 tmp = ch->recv->state;
362 if (tmp != ch->last_state)
363 smd_state_change(ch, ch->last_state, tmp);
364 if (ch_flags) {
365 ch->update_state(ch);
366 ch->notify(ch->priv, SMD_EVENT_DATA);
367 }
368 }
369 if (do_notify)
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700370 notify();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700371 spin_unlock_irqrestore(&smd_lock, flags);
372 do_smd_probe();
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700373}
374
Brian Swetland37521a32009-07-01 18:30:47 -0700375static irqreturn_t smd_modem_irq_handler(int irq, void *data)
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700376{
Brian Swetland37521a32009-07-01 18:30:47 -0700377 handle_smd_irq(&smd_ch_list_modem, notify_modem_smd);
378 return IRQ_HANDLED;
379}
380
381static irqreturn_t smd_dsp_irq_handler(int irq, void *data)
382{
383 handle_smd_irq(&smd_ch_list_dsp, notify_dsp_smd);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700384 return IRQ_HANDLED;
385}
386
387static void smd_fake_irq_handler(unsigned long arg)
388{
Brian Swetland37521a32009-07-01 18:30:47 -0700389 handle_smd_irq(&smd_ch_list_modem, notify_modem_smd);
390 handle_smd_irq(&smd_ch_list_dsp, notify_dsp_smd);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700391}
392
393static DECLARE_TASKLET(smd_fake_irq_tasklet, smd_fake_irq_handler, 0);
394
Brian Swetland37521a32009-07-01 18:30:47 -0700395static inline int smd_need_int(struct smd_channel *ch)
396{
397 if (ch_is_open(ch)) {
398 if (ch->recv->fHEAD || ch->recv->fTAIL || ch->recv->fSTATE)
399 return 1;
400 if (ch->recv->state != ch->last_state)
401 return 1;
402 }
403 return 0;
404}
405
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700406void smd_sleep_exit(void)
407{
408 unsigned long flags;
409 struct smd_channel *ch;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700410 int need_int = 0;
411
412 spin_lock_irqsave(&smd_lock, flags);
Brian Swetland37521a32009-07-01 18:30:47 -0700413 list_for_each_entry(ch, &smd_ch_list_modem, ch_list) {
414 if (smd_need_int(ch)) {
415 need_int = 1;
416 break;
417 }
418 }
419 list_for_each_entry(ch, &smd_ch_list_dsp, ch_list) {
420 if (smd_need_int(ch)) {
421 need_int = 1;
422 break;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700423 }
424 }
425 spin_unlock_irqrestore(&smd_lock, flags);
426 do_smd_probe();
Brian Swetland37521a32009-07-01 18:30:47 -0700427
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700428 if (need_int) {
429 if (msm_smd_debug_mask & MSM_SMD_DEBUG)
430 pr_info("smd_sleep_exit need interrupt\n");
431 tasklet_schedule(&smd_fake_irq_tasklet);
432 }
433}
434
435
436void smd_kick(smd_channel_t *ch)
437{
438 unsigned long flags;
439 unsigned tmp;
440
441 spin_lock_irqsave(&smd_lock, flags);
442 ch->update_state(ch);
443 tmp = ch->recv->state;
444 if (tmp != ch->last_state) {
445 ch->last_state = tmp;
446 if (tmp == SMD_SS_OPENED)
447 ch->notify(ch->priv, SMD_EVENT_OPEN);
448 else
449 ch->notify(ch->priv, SMD_EVENT_CLOSE);
450 }
451 ch->notify(ch->priv, SMD_EVENT_DATA);
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700452 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700453 spin_unlock_irqrestore(&smd_lock, flags);
454}
455
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700456static int smd_is_packet(int chn, unsigned type)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700457{
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700458 type &= SMD_KIND_MASK;
459 if (type == SMD_KIND_PACKET)
460 return 1;
461 if (type == SMD_KIND_STREAM)
462 return 0;
463
464 /* older AMSS reports SMD_KIND_UNKNOWN always */
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700465 if ((chn > 4) || (chn == 1))
466 return 1;
467 else
468 return 0;
469}
470
471static int smd_stream_write(smd_channel_t *ch, const void *_data, int len)
472{
473 void *ptr;
474 const unsigned char *buf = _data;
475 unsigned xfer;
476 int orig_len = len;
477
478 if (len < 0)
479 return -EINVAL;
480
481 while ((xfer = ch_write_buffer(ch, &ptr)) != 0) {
482 if (!ch_is_open(ch))
483 break;
484 if (xfer > len)
485 xfer = len;
486 memcpy(ptr, buf, xfer);
487 ch_write_done(ch, xfer);
488 len -= xfer;
489 buf += xfer;
490 if (len == 0)
491 break;
492 }
493
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700494 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700495
496 return orig_len - len;
497}
498
499static int smd_packet_write(smd_channel_t *ch, const void *_data, int len)
500{
501 unsigned hdr[5];
502
503 if (len < 0)
504 return -EINVAL;
505
506 if (smd_stream_write_avail(ch) < (len + SMD_HEADER_SIZE))
507 return -ENOMEM;
508
509 hdr[0] = len;
510 hdr[1] = hdr[2] = hdr[3] = hdr[4] = 0;
511
512 smd_stream_write(ch, hdr, sizeof(hdr));
513 smd_stream_write(ch, _data, len);
514
515 return len;
516}
517
518static int smd_stream_read(smd_channel_t *ch, void *data, int len)
519{
520 int r;
521
522 if (len < 0)
523 return -EINVAL;
524
525 r = ch_read(ch, data, len);
526 if (r > 0)
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700527 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700528
529 return r;
530}
531
532static int smd_packet_read(smd_channel_t *ch, void *data, int len)
533{
534 unsigned long flags;
535 int r;
536
537 if (len < 0)
538 return -EINVAL;
539
540 if (len > ch->current_packet)
541 len = ch->current_packet;
542
543 r = ch_read(ch, data, len);
544 if (r > 0)
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700545 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700546
547 spin_lock_irqsave(&smd_lock, flags);
548 ch->current_packet -= r;
549 update_packet_state(ch);
550 spin_unlock_irqrestore(&smd_lock, flags);
551
552 return r;
553}
554
Brian Swetland34f719b2009-10-30 16:22:05 -0700555static int smd_alloc_channel(const char *name, uint32_t cid, uint32_t type)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700556{
557 struct smd_channel *ch;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700558
559 ch = kzalloc(sizeof(struct smd_channel), GFP_KERNEL);
560 if (ch == 0) {
561 pr_err("smd_alloc_channel() out of memory\n");
Brian Swetland34f719b2009-10-30 16:22:05 -0700562 return -1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700563 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700564 ch->n = cid;
565
Daniel Walkerbf83de42010-03-16 16:29:44 -0700566 if (_smd_alloc_channel(ch)) {
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700567 kfree(ch);
Brian Swetland34f719b2009-10-30 16:22:05 -0700568 return -1;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700569 }
570
571 ch->fifo_mask = ch->fifo_size - 1;
572 ch->type = type;
573
574 if ((type & SMD_TYPE_MASK) == SMD_TYPE_APPS_MODEM)
575 ch->notify_other_cpu = notify_modem_smd;
576 else
577 ch->notify_other_cpu = notify_dsp_smd;
578
579 if (smd_is_packet(cid, type)) {
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700580 ch->read = smd_packet_read;
581 ch->write = smd_packet_write;
582 ch->read_avail = smd_packet_read_avail;
583 ch->write_avail = smd_packet_write_avail;
584 ch->update_state = update_packet_state;
585 } else {
586 ch->read = smd_stream_read;
587 ch->write = smd_stream_write;
588 ch->read_avail = smd_stream_read_avail;
589 ch->write_avail = smd_stream_write_avail;
590 ch->update_state = update_stream_state;
591 }
592
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700593 if ((type & 0xff) == 0)
594 memcpy(ch->name, "SMD_", 4);
595 else
596 memcpy(ch->name, "DSP_", 4);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700597 memcpy(ch->name + 4, name, 20);
598 ch->name[23] = 0;
599 ch->pdev.name = ch->name;
600 ch->pdev.id = -1;
601
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700602 pr_info("smd_alloc_channel() cid=%02d size=%05d '%s'\n",
603 ch->n, ch->fifo_size, ch->name);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700604
605 mutex_lock(&smd_creation_mutex);
606 list_add(&ch->ch_list, &smd_ch_closed_list);
607 mutex_unlock(&smd_creation_mutex);
608
609 platform_device_register(&ch->pdev);
Brian Swetland34f719b2009-10-30 16:22:05 -0700610 return 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700611}
612
Daniel Walker3843ac32010-03-17 11:10:40 -0700613static void smd_channel_probe_worker(struct work_struct *work)
614{
615 struct smd_alloc_elm *shared;
616 unsigned ctype;
617 unsigned type;
618 unsigned n;
619
620 shared = smem_find(ID_CH_ALLOC_TBL, sizeof(*shared) * 64);
621 if (!shared) {
622 pr_err("smd: cannot find allocation table\n");
623 return;
624 }
625 for (n = 0; n < 64; n++) {
626 if (smd_ch_allocated[n])
627 continue;
628 if (!shared[n].ref_count)
629 continue;
630 if (!shared[n].name[0])
631 continue;
632 ctype = shared[n].ctype;
633 type = ctype & SMD_TYPE_MASK;
634
635 /* DAL channels are stream but neither the modem,
636 * nor the DSP correctly indicate this. Fixup manually.
637 */
638 if (!memcmp(shared[n].name, "DAL", 3))
639 ctype = (ctype & (~SMD_KIND_MASK)) | SMD_KIND_STREAM;
640
641 type = shared[n].ctype & SMD_TYPE_MASK;
642 if ((type == SMD_TYPE_APPS_MODEM) ||
643 (type == SMD_TYPE_APPS_DSP))
644 if (!smd_alloc_channel(shared[n].name, shared[n].cid, ctype))
645 smd_ch_allocated[n] = 1;
646 }
647}
648
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700649static void do_nothing_notify(void *priv, unsigned flags)
650{
651}
652
653struct smd_channel *smd_get_channel(const char *name)
654{
655 struct smd_channel *ch;
656
657 mutex_lock(&smd_creation_mutex);
658 list_for_each_entry(ch, &smd_ch_closed_list, ch_list) {
659 if (!strcmp(name, ch->name)) {
660 list_del(&ch->ch_list);
661 mutex_unlock(&smd_creation_mutex);
662 return ch;
663 }
664 }
665 mutex_unlock(&smd_creation_mutex);
666
667 return NULL;
668}
669
670int smd_open(const char *name, smd_channel_t **_ch,
671 void *priv, void (*notify)(void *, unsigned))
672{
673 struct smd_channel *ch;
674 unsigned long flags;
675
676 if (smd_initialized == 0) {
677 pr_info("smd_open() before smd_init()\n");
678 return -ENODEV;
679 }
680
681 ch = smd_get_channel(name);
682 if (!ch)
683 return -ENODEV;
684
685 if (notify == 0)
686 notify = do_nothing_notify;
687
688 ch->notify = notify;
689 ch->current_packet = 0;
690 ch->last_state = SMD_SS_CLOSED;
691 ch->priv = priv;
692
693 *_ch = ch;
694
695 spin_lock_irqsave(&smd_lock, flags);
Brian Swetland37521a32009-07-01 18:30:47 -0700696
697 if ((ch->type & SMD_TYPE_MASK) == SMD_TYPE_APPS_MODEM)
698 list_add(&ch->ch_list, &smd_ch_list_modem);
699 else
700 list_add(&ch->ch_list, &smd_ch_list_dsp);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700701
702 /* If the remote side is CLOSING, we need to get it to
703 * move to OPENING (which we'll do by moving from CLOSED to
704 * OPENING) and then get it to move from OPENING to
705 * OPENED (by doing the same state change ourselves).
706 *
707 * Otherwise, it should be OPENING and we can move directly
708 * to OPENED so that it will follow.
709 */
710 if (ch->recv->state == SMD_SS_CLOSING) {
711 ch->send->head = 0;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700712 ch_set_state(ch, SMD_SS_OPENING);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700713 } else {
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700714 ch_set_state(ch, SMD_SS_OPENED);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700715 }
716 spin_unlock_irqrestore(&smd_lock, flags);
717 smd_kick(ch);
718
719 return 0;
720}
721
722int smd_close(smd_channel_t *ch)
723{
724 unsigned long flags;
725
726 pr_info("smd_close(%p)\n", ch);
727
728 if (ch == 0)
729 return -1;
730
731 spin_lock_irqsave(&smd_lock, flags);
732 ch->notify = do_nothing_notify;
733 list_del(&ch->ch_list);
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700734 ch_set_state(ch, SMD_SS_CLOSED);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700735 spin_unlock_irqrestore(&smd_lock, flags);
736
737 mutex_lock(&smd_creation_mutex);
738 list_add(&ch->ch_list, &smd_ch_closed_list);
739 mutex_unlock(&smd_creation_mutex);
740
741 return 0;
742}
743
744int smd_read(smd_channel_t *ch, void *data, int len)
745{
746 return ch->read(ch, data, len);
747}
748
749int smd_write(smd_channel_t *ch, const void *data, int len)
750{
751 return ch->write(ch, data, len);
752}
753
Brian Swetland636eb9c2009-12-07 15:28:08 -0800754int smd_write_atomic(smd_channel_t *ch, const void *data, int len)
755{
756 unsigned long flags;
757 int res;
758 spin_lock_irqsave(&smd_lock, flags);
759 res = ch->write(ch, data, len);
760 spin_unlock_irqrestore(&smd_lock, flags);
761 return res;
762}
763
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700764int smd_read_avail(smd_channel_t *ch)
765{
766 return ch->read_avail(ch);
767}
768
769int smd_write_avail(smd_channel_t *ch)
770{
771 return ch->write_avail(ch);
772}
773
774int smd_wait_until_readable(smd_channel_t *ch, int bytes)
775{
776 return -1;
777}
778
779int smd_wait_until_writable(smd_channel_t *ch, int bytes)
780{
781 return -1;
782}
783
784int smd_cur_packet_size(smd_channel_t *ch)
785{
786 return ch->current_packet;
787}
788
789
790/* ------------------------------------------------------------------------- */
791
792void *smem_alloc(unsigned id, unsigned size)
793{
794 return smem_find(id, size);
795}
796
Brian Swetland03e00cd2009-07-01 17:58:37 -0700797void *smem_item(unsigned id, unsigned *size)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700798{
799 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
800 struct smem_heap_entry *toc = shared->heap_toc;
801
802 if (id >= SMEM_NUM_ITEMS)
803 return 0;
804
805 if (toc[id].allocated) {
806 *size = toc[id].size;
807 return (void *) (MSM_SHARED_RAM_BASE + toc[id].offset);
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700808 } else {
809 *size = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700810 }
811
812 return 0;
813}
814
815void *smem_find(unsigned id, unsigned size_in)
816{
817 unsigned size;
818 void *ptr;
819
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700820 ptr = smem_item(id, &size);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700821 if (!ptr)
822 return 0;
823
824 size_in = ALIGN(size_in, 8);
825 if (size_in != size) {
826 pr_err("smem_find(%d, %d): wrong size %d\n",
827 id, size_in, size);
828 return 0;
829 }
830
831 return ptr;
832}
833
834static irqreturn_t smsm_irq_handler(int irq, void *data)
835{
836 unsigned long flags;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700837 unsigned apps, modm;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700838
839 spin_lock_irqsave(&smem_lock, flags);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700840
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700841 apps = raw_smsm_get_state(SMSM_STATE_APPS);
842 modm = raw_smsm_get_state(SMSM_STATE_MODEM);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700843
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700844 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
845 pr_info("<SM %08x %08x>\n", apps, modm);
Daniel Walker79848a22010-03-16 15:20:07 -0700846 if (modm & SMSM_RESET)
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700847 handle_modem_crash();
Daniel Walker79848a22010-03-16 15:20:07 -0700848
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700849 do_smd_probe();
850
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700851 spin_unlock_irqrestore(&smem_lock, flags);
852 return IRQ_HANDLED;
853}
854
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700855int smsm_change_state(enum smsm_state_item item,
856 uint32_t clear_mask, uint32_t set_mask)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700857{
Daniel Walker1a86fbc2010-03-17 09:58:29 -0700858 unsigned long addr = smd_info.state + item * 4;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700859 unsigned long flags;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700860 unsigned state;
861
862 if (!smd_info.ready)
863 return -EIO;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700864
865 spin_lock_irqsave(&smem_lock, flags);
866
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700867 if (raw_smsm_get_state(SMSM_STATE_MODEM) & SMSM_RESET)
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700868 handle_modem_crash();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700869
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700870 state = (readl(addr) & ~clear_mask) | set_mask;
871 writel(state, addr);
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700872
873 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700874 pr_info("smsm_change_state %d %x\n", item, state);
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700875 notify_other_smsm();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700876
877 spin_unlock_irqrestore(&smem_lock, flags);
878
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700879 return 0;
880}
881
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700882uint32_t smsm_get_state(enum smsm_state_item item)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700883{
884 unsigned long flags;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700885 uint32_t rv;
886
887 spin_lock_irqsave(&smem_lock, flags);
888
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700889 rv = readl(smd_info.state + item * 4);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700890
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700891 if (item == SMSM_STATE_MODEM && (rv & SMSM_RESET))
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700892 handle_modem_crash();
893
894 spin_unlock_irqrestore(&smem_lock, flags);
895
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700896 return rv;
897}
898
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -0700899#ifdef CONFIG_ARCH_MSM_SCORPION
900
901int smsm_set_sleep_duration(uint32_t delay)
902{
Brian Swetland03e00cd2009-07-01 17:58:37 -0700903 struct msm_dem_slave_data *ptr;
904
905 ptr = smem_find(SMEM_APPS_DEM_SLAVE_DATA, sizeof(*ptr));
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -0700906 if (ptr == NULL) {
907 pr_err("smsm_set_sleep_duration <SM NO APPS_DEM_SLAVE_DATA>\n");
908 return -EIO;
909 }
910 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
911 pr_info("smsm_set_sleep_duration %d -> %d\n",
912 ptr->sleep_time, delay);
913 ptr->sleep_time = delay;
914 return 0;
915}
916
917#else
918
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700919int smsm_set_sleep_duration(uint32_t delay)
920{
921 uint32_t *ptr;
922
Brian Swetland03e00cd2009-07-01 17:58:37 -0700923 ptr = smem_find(SMEM_SMSM_SLEEP_DELAY, sizeof(*ptr));
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700924 if (ptr == NULL) {
925 pr_err("smsm_set_sleep_duration <SM NO SLEEP_DELAY>\n");
926 return -EIO;
927 }
928 if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
929 pr_info("smsm_set_sleep_duration %d -> %d\n",
930 *ptr, delay);
931 *ptr = delay;
932 return 0;
933}
934
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -0700935#endif
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700936
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700937int smd_core_init(void)
938{
939 int r;
940 pr_info("smd_core_init()\n");
941
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700942 /* wait for essential items to be initialized */
943 for (;;) {
944 unsigned size;
945 void *state;
946 state = smem_item(SMEM_SMSM_SHARED_STATE, &size);
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700947 if (size == SMSM_V1_SIZE || size == SMSM_V2_SIZE) {
948 smd_info.state = (unsigned)state;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700949 break;
950 }
951 }
952
953 smd_info.ready = 1;
954
Brian Swetland37521a32009-07-01 18:30:47 -0700955 r = request_irq(INT_A9_M2A_0, smd_modem_irq_handler,
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700956 IRQF_TRIGGER_RISING, "smd_dev", 0);
957 if (r < 0)
958 return r;
959 r = enable_irq_wake(INT_A9_M2A_0);
960 if (r < 0)
961 pr_err("smd_core_init: enable_irq_wake failed for A9_M2A_0\n");
962
963 r = request_irq(INT_A9_M2A_5, smsm_irq_handler,
964 IRQF_TRIGGER_RISING, "smsm_dev", 0);
965 if (r < 0) {
966 free_irq(INT_A9_M2A_0, 0);
967 return r;
968 }
969 r = enable_irq_wake(INT_A9_M2A_5);
970 if (r < 0)
971 pr_err("smd_core_init: enable_irq_wake failed for A9_M2A_5\n");
972
Brian Swetland37521a32009-07-01 18:30:47 -0700973#if defined(CONFIG_QDSP6)
974 r = request_irq(INT_ADSP_A11, smd_dsp_irq_handler,
975 IRQF_TRIGGER_RISING, "smd_dsp", 0);
976 if (r < 0) {
977 free_irq(INT_A9_M2A_0, 0);
978 free_irq(INT_A9_M2A_5, 0);
979 return r;
980 }
981#endif
982
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700983 /* check for any SMD channels that may already exist */
984 do_smd_probe();
985
986 /* indicate that we're up and running */
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700987 smsm_change_state(SMSM_STATE_APPS,
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -0700988 ~0, SMSM_INIT | SMSM_SMDINIT | SMSM_RPCINIT | SMSM_RUN);
989#ifdef CONFIG_ARCH_MSM_SCORPION
990 smsm_change_state(SMSM_STATE_APPS_DEM, ~0, 0);
991#endif
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700992
993 pr_info("smd_core_init() done\n");
994
995 return 0;
996}
997
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700998static int __init msm_smd_probe(struct platform_device *pdev)
999{
1000 pr_info("smd_init()\n");
1001
1002 INIT_WORK(&probe_work, smd_channel_probe_worker);
1003
1004 if (smd_core_init()) {
1005 pr_err("smd_core_init() failed\n");
1006 return -1;
1007 }
1008
1009 do_smd_probe();
1010
1011 msm_check_for_modem_crash = check_for_modem_crash;
1012
Iliyan Malchev1207bab2009-11-15 18:16:43 -08001013 msm_init_last_radio_log(THIS_MODULE);
1014
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001015 smd_initialized = 1;
1016
1017 return 0;
1018}
1019
1020static struct platform_driver msm_smd_driver = {
1021 .probe = msm_smd_probe,
1022 .driver = {
1023 .name = MODULE_NAME,
1024 .owner = THIS_MODULE,
1025 },
1026};
1027
1028static int __init msm_smd_init(void)
1029{
1030 return platform_driver_register(&msm_smd_driver);
1031}
1032
1033module_init(msm_smd_init);
1034
1035MODULE_DESCRIPTION("MSM Shared Memory Core");
1036MODULE_AUTHOR("Brian Swetland <swetland@google.com>");
1037MODULE_LICENSE("GPL");