]>
Commit | Line | Data |
---|---|---|
2eb44eb9 BS |
1 | /* 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 | ||
37521a31 BS |
38 | #if defined(CONFIG_ARCH_QSD8X50) |
39 | #define CONFIG_QDSP6 1 | |
40 | #endif | |
41 | ||
2eb44eb9 BS |
42 | void (*msm_hw_reset_hook)(void); |
43 | ||
44 | #define MODULE_NAME "msm_smd" | |
45 | ||
46 | enum { | |
47 | MSM_SMD_DEBUG = 1U << 0, | |
48 | MSM_SMSM_DEBUG = 1U << 0, | |
49 | }; | |
50 | ||
51 | static int msm_smd_debug_mask; | |
52 | ||
03e00cd3 | 53 | struct shared_info { |
5b0f5a3f | 54 | int ready; |
28379410 | 55 | unsigned state; |
5b0f5a3f BS |
56 | }; |
57 | ||
28379410 | 58 | static unsigned dummy_state[SMSM_STATE_COUNT]; |
5b0f5a3f BS |
59 | |
60 | static struct shared_info smd_info = { | |
28379410 | 61 | .state = (unsigned) &dummy_state, |
5b0f5a3f BS |
62 | }; |
63 | ||
2eb44eb9 BS |
64 | module_param_named(debug_mask, msm_smd_debug_mask, |
65 | int, S_IRUGO | S_IWUSR | S_IWGRP); | |
66 | ||
03e00cd3 | 67 | void *smem_item(unsigned id, unsigned *size); |
2eb44eb9 BS |
68 | static void smd_diag(void); |
69 | ||
70 | static unsigned last_heap_free = 0xffffffff; | |
71 | ||
72 | #define MSM_A2M_INT(n) (MSM_CSR_BASE + 0x400 + (n) * 4) | |
73 | ||
74 | static inline void notify_other_smsm(void) | |
75 | { | |
76 | writel(1, MSM_A2M_INT(5)); | |
37521a31 BS |
77 | #ifdef CONFIG_QDSP6 |
78 | writel(1, MSM_A2M_INT(8)); | |
79 | #endif | |
2eb44eb9 BS |
80 | } |
81 | ||
5b0f5a3f | 82 | static inline void notify_modem_smd(void) |
2eb44eb9 BS |
83 | { |
84 | writel(1, MSM_A2M_INT(0)); | |
85 | } | |
86 | ||
5b0f5a3f BS |
87 | static inline void notify_dsp_smd(void) |
88 | { | |
89 | writel(1, MSM_A2M_INT(8)); | |
90 | } | |
91 | ||
2eb44eb9 BS |
92 | static void smd_diag(void) |
93 | { | |
94 | char *x; | |
95 | ||
96 | x = smem_find(ID_DIAG_ERR_MSG, SZ_DIAG_ERR_MSG); | |
97 | if (x != 0) { | |
98 | x[SZ_DIAG_ERR_MSG - 1] = 0; | |
99 | pr_info("smem: DIAG '%s'\n", x); | |
100 | } | |
101 | } | |
102 | ||
103 | /* call when SMSM_RESET flag is set in the A9's smsm_state */ | |
104 | static void handle_modem_crash(void) | |
105 | { | |
106 | pr_err("ARM9 has CRASHED\n"); | |
107 | smd_diag(); | |
108 | ||
109 | /* hard reboot if possible */ | |
110 | if (msm_hw_reset_hook) | |
111 | msm_hw_reset_hook(); | |
112 | ||
113 | /* in this case the modem or watchdog should reboot us */ | |
114 | for (;;) | |
115 | ; | |
116 | } | |
117 | ||
118 | extern int (*msm_check_for_modem_crash)(void); | |
119 | ||
28379410 AH |
120 | uint32_t raw_smsm_get_state(enum smsm_state_item item) |
121 | { | |
122 | return readl(smd_info.state + item * 4); | |
123 | } | |
124 | ||
2eb44eb9 BS |
125 | static int check_for_modem_crash(void) |
126 | { | |
28379410 | 127 | if (raw_smsm_get_state(SMSM_STATE_MODEM) & SMSM_RESET) { |
2eb44eb9 BS |
128 | handle_modem_crash(); |
129 | return -1; | |
2eb44eb9 | 130 | } |
5b0f5a3f | 131 | return 0; |
2eb44eb9 BS |
132 | } |
133 | ||
2eb44eb9 | 134 | /* the spinlock is used to synchronize between the |
03e00cd3 BS |
135 | * irq handler and code that mutates the channel |
136 | * list or fiddles with channel state | |
137 | */ | |
138 | DEFINE_SPINLOCK(smd_lock); | |
139 | DEFINE_SPINLOCK(smem_lock); | |
2eb44eb9 BS |
140 | |
141 | /* the mutex is used during open() and close() | |
03e00cd3 BS |
142 | * operations to avoid races while creating or |
143 | * destroying smd_channel structures | |
144 | */ | |
2eb44eb9 BS |
145 | static DEFINE_MUTEX(smd_creation_mutex); |
146 | ||
147 | static int smd_initialized; | |
148 | ||
03e00cd3 | 149 | LIST_HEAD(smd_ch_closed_list); |
37521a31 BS |
150 | LIST_HEAD(smd_ch_list_modem); |
151 | LIST_HEAD(smd_ch_list_dsp); | |
2eb44eb9 BS |
152 | |
153 | static unsigned char smd_ch_allocated[64]; | |
154 | static struct work_struct probe_work; | |
155 | ||
156 | static void smd_alloc_channel(const char *name, uint32_t cid, uint32_t type); | |
157 | ||
158 | static void smd_channel_probe_worker(struct work_struct *work) | |
159 | { | |
160 | struct smd_alloc_elm *shared; | |
37521a31 | 161 | unsigned ctype; |
5b0f5a3f | 162 | unsigned type; |
2eb44eb9 BS |
163 | unsigned n; |
164 | ||
165 | shared = smem_find(ID_CH_ALLOC_TBL, sizeof(*shared) * 64); | |
4d4fb266 BS |
166 | if (!shared) { |
167 | pr_err("smd: cannot find allocation table\n"); | |
168 | return; | |
169 | } | |
2eb44eb9 BS |
170 | for (n = 0; n < 64; n++) { |
171 | if (smd_ch_allocated[n]) | |
172 | continue; | |
173 | if (!shared[n].ref_count) | |
174 | continue; | |
175 | if (!shared[n].name[0]) | |
176 | continue; | |
37521a31 BS |
177 | ctype = shared[n].ctype; |
178 | type = ctype & SMD_TYPE_MASK; | |
179 | ||
180 | /* DAL channels are stream but neither the modem, | |
181 | * nor the DSP correctly indicate this. Fixup manually. | |
182 | */ | |
183 | if (!memcmp(shared[n].name, "DAL", 3)) | |
184 | ctype = (ctype & (~SMD_KIND_MASK)) | SMD_KIND_STREAM; | |
185 | ||
5b0f5a3f BS |
186 | type = shared[n].ctype & SMD_TYPE_MASK; |
187 | if ((type == SMD_TYPE_APPS_MODEM) || | |
188 | (type == SMD_TYPE_APPS_DSP)) | |
37521a31 | 189 | smd_alloc_channel(shared[n].name, shared[n].cid, ctype); |
2eb44eb9 BS |
190 | smd_ch_allocated[n] = 1; |
191 | } | |
192 | } | |
193 | ||
2eb44eb9 BS |
194 | /* how many bytes are available for reading */ |
195 | static int smd_stream_read_avail(struct smd_channel *ch) | |
196 | { | |
5b0f5a3f | 197 | return (ch->recv->head - ch->recv->tail) & ch->fifo_mask; |
2eb44eb9 BS |
198 | } |
199 | ||
200 | /* how many bytes we are free to write */ | |
201 | static int smd_stream_write_avail(struct smd_channel *ch) | |
202 | { | |
5b0f5a3f BS |
203 | return ch->fifo_mask - |
204 | ((ch->send->head - ch->send->tail) & ch->fifo_mask); | |
2eb44eb9 BS |
205 | } |
206 | ||
207 | static int smd_packet_read_avail(struct smd_channel *ch) | |
208 | { | |
209 | if (ch->current_packet) { | |
210 | int n = smd_stream_read_avail(ch); | |
211 | if (n > ch->current_packet) | |
212 | n = ch->current_packet; | |
213 | return n; | |
214 | } else { | |
215 | return 0; | |
216 | } | |
217 | } | |
218 | ||
219 | static int smd_packet_write_avail(struct smd_channel *ch) | |
220 | { | |
221 | int n = smd_stream_write_avail(ch); | |
222 | return n > SMD_HEADER_SIZE ? n - SMD_HEADER_SIZE : 0; | |
223 | } | |
224 | ||
225 | static int ch_is_open(struct smd_channel *ch) | |
226 | { | |
227 | return (ch->recv->state == SMD_SS_OPENED) && | |
228 | (ch->send->state == SMD_SS_OPENED); | |
229 | } | |
230 | ||
231 | /* provide a pointer and length to readable data in the fifo */ | |
232 | static unsigned ch_read_buffer(struct smd_channel *ch, void **ptr) | |
233 | { | |
234 | unsigned head = ch->recv->head; | |
235 | unsigned tail = ch->recv->tail; | |
5b0f5a3f | 236 | *ptr = (void *) (ch->recv_data + tail); |
2eb44eb9 BS |
237 | |
238 | if (tail <= head) | |
239 | return head - tail; | |
240 | else | |
5b0f5a3f | 241 | return ch->fifo_size - tail; |
2eb44eb9 BS |
242 | } |
243 | ||
244 | /* advance the fifo read pointer after data from ch_read_buffer is consumed */ | |
245 | static void ch_read_done(struct smd_channel *ch, unsigned count) | |
246 | { | |
247 | BUG_ON(count > smd_stream_read_avail(ch)); | |
5b0f5a3f | 248 | ch->recv->tail = (ch->recv->tail + count) & ch->fifo_mask; |
7632fba0 | 249 | ch->send->fTAIL = 1; |
2eb44eb9 BS |
250 | } |
251 | ||
252 | /* basic read interface to ch_read_{buffer,done} used | |
03e00cd3 BS |
253 | * by smd_*_read() and update_packet_state() |
254 | * will read-and-discard if the _data pointer is null | |
255 | */ | |
2eb44eb9 BS |
256 | static int ch_read(struct smd_channel *ch, void *_data, int len) |
257 | { | |
258 | void *ptr; | |
259 | unsigned n; | |
260 | unsigned char *data = _data; | |
261 | int orig_len = len; | |
262 | ||
263 | while (len > 0) { | |
264 | n = ch_read_buffer(ch, &ptr); | |
265 | if (n == 0) | |
266 | break; | |
267 | ||
268 | if (n > len) | |
269 | n = len; | |
270 | if (_data) | |
271 | memcpy(data, ptr, n); | |
272 | ||
273 | data += n; | |
274 | len -= n; | |
275 | ch_read_done(ch, n); | |
276 | } | |
277 | ||
278 | return orig_len - len; | |
279 | } | |
280 | ||
281 | static void update_stream_state(struct smd_channel *ch) | |
282 | { | |
283 | /* streams have no special state requiring updating */ | |
284 | } | |
285 | ||
286 | static void update_packet_state(struct smd_channel *ch) | |
287 | { | |
288 | unsigned hdr[5]; | |
289 | int r; | |
290 | ||
291 | /* can't do anything if we're in the middle of a packet */ | |
292 | if (ch->current_packet != 0) | |
293 | return; | |
294 | ||
295 | /* don't bother unless we can get the full header */ | |
296 | if (smd_stream_read_avail(ch) < SMD_HEADER_SIZE) | |
297 | return; | |
298 | ||
299 | r = ch_read(ch, hdr, SMD_HEADER_SIZE); | |
300 | BUG_ON(r != SMD_HEADER_SIZE); | |
301 | ||
302 | ch->current_packet = hdr[0]; | |
303 | } | |
304 | ||
305 | /* provide a pointer and length to next free space in the fifo */ | |
306 | static unsigned ch_write_buffer(struct smd_channel *ch, void **ptr) | |
307 | { | |
308 | unsigned head = ch->send->head; | |
309 | unsigned tail = ch->send->tail; | |
5b0f5a3f | 310 | *ptr = (void *) (ch->send_data + head); |
2eb44eb9 BS |
311 | |
312 | if (head < tail) { | |
313 | return tail - head - 1; | |
314 | } else { | |
315 | if (tail == 0) | |
5b0f5a3f | 316 | return ch->fifo_size - head - 1; |
2eb44eb9 | 317 | else |
5b0f5a3f | 318 | return ch->fifo_size - head; |
2eb44eb9 BS |
319 | } |
320 | } | |
321 | ||
322 | /* advace the fifo write pointer after freespace | |
323 | * from ch_write_buffer is filled | |
324 | */ | |
325 | static void ch_write_done(struct smd_channel *ch, unsigned count) | |
326 | { | |
327 | BUG_ON(count > smd_stream_write_avail(ch)); | |
5b0f5a3f | 328 | ch->send->head = (ch->send->head + count) & ch->fifo_mask; |
2eb44eb9 BS |
329 | ch->send->fHEAD = 1; |
330 | } | |
331 | ||
5b0f5a3f | 332 | static void ch_set_state(struct smd_channel *ch, unsigned n) |
2eb44eb9 BS |
333 | { |
334 | if (n == SMD_SS_OPENED) { | |
5b0f5a3f BS |
335 | ch->send->fDSR = 1; |
336 | ch->send->fCTS = 1; | |
337 | ch->send->fCD = 1; | |
2eb44eb9 | 338 | } else { |
5b0f5a3f BS |
339 | ch->send->fDSR = 0; |
340 | ch->send->fCTS = 0; | |
341 | ch->send->fCD = 0; | |
2eb44eb9 | 342 | } |
5b0f5a3f BS |
343 | ch->send->state = n; |
344 | ch->send->fSTATE = 1; | |
345 | ch->notify_other_cpu(); | |
2eb44eb9 BS |
346 | } |
347 | ||
348 | static void do_smd_probe(void) | |
349 | { | |
350 | struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE; | |
351 | if (shared->heap_info.free_offset != last_heap_free) { | |
352 | last_heap_free = shared->heap_info.free_offset; | |
353 | schedule_work(&probe_work); | |
354 | } | |
355 | } | |
356 | ||
357 | static void smd_state_change(struct smd_channel *ch, | |
358 | unsigned last, unsigned next) | |
359 | { | |
360 | ch->last_state = next; | |
361 | ||
03e00cd3 | 362 | pr_info("SMD: ch %d %d -> %d\n", ch->n, last, next); |
2eb44eb9 BS |
363 | |
364 | switch (next) { | |
365 | case SMD_SS_OPENING: | |
366 | ch->recv->tail = 0; | |
367 | case SMD_SS_OPENED: | |
368 | if (ch->send->state != SMD_SS_OPENED) | |
5b0f5a3f | 369 | ch_set_state(ch, SMD_SS_OPENED); |
2eb44eb9 BS |
370 | ch->notify(ch->priv, SMD_EVENT_OPEN); |
371 | break; | |
372 | case SMD_SS_FLUSHING: | |
373 | case SMD_SS_RESET: | |
374 | /* we should force them to close? */ | |
375 | default: | |
376 | ch->notify(ch->priv, SMD_EVENT_CLOSE); | |
377 | } | |
378 | } | |
379 | ||
5b0f5a3f | 380 | static void handle_smd_irq(struct list_head *list, void (*notify)(void)) |
2eb44eb9 BS |
381 | { |
382 | unsigned long flags; | |
383 | struct smd_channel *ch; | |
384 | int do_notify = 0; | |
385 | unsigned ch_flags; | |
386 | unsigned tmp; | |
387 | ||
388 | spin_lock_irqsave(&smd_lock, flags); | |
5b0f5a3f | 389 | list_for_each_entry(ch, list, ch_list) { |
2eb44eb9 BS |
390 | ch_flags = 0; |
391 | if (ch_is_open(ch)) { | |
392 | if (ch->recv->fHEAD) { | |
393 | ch->recv->fHEAD = 0; | |
394 | ch_flags |= 1; | |
395 | do_notify |= 1; | |
396 | } | |
397 | if (ch->recv->fTAIL) { | |
398 | ch->recv->fTAIL = 0; | |
399 | ch_flags |= 2; | |
400 | do_notify |= 1; | |
401 | } | |
402 | if (ch->recv->fSTATE) { | |
403 | ch->recv->fSTATE = 0; | |
404 | ch_flags |= 4; | |
405 | do_notify |= 1; | |
406 | } | |
407 | } | |
408 | tmp = ch->recv->state; | |
409 | if (tmp != ch->last_state) | |
410 | smd_state_change(ch, ch->last_state, tmp); | |
411 | if (ch_flags) { | |
412 | ch->update_state(ch); | |
413 | ch->notify(ch->priv, SMD_EVENT_DATA); | |
414 | } | |
415 | } | |
416 | if (do_notify) | |
5b0f5a3f | 417 | notify(); |
2eb44eb9 BS |
418 | spin_unlock_irqrestore(&smd_lock, flags); |
419 | do_smd_probe(); | |
5b0f5a3f BS |
420 | } |
421 | ||
37521a31 BS |
422 | static irqreturn_t smd_modem_irq_handler(int irq, void *data) |
423 | { | |
424 | handle_smd_irq(&smd_ch_list_modem, notify_modem_smd); | |
425 | return IRQ_HANDLED; | |
426 | } | |
427 | ||
428 | static irqreturn_t smd_dsp_irq_handler(int irq, void *data) | |
5b0f5a3f | 429 | { |
37521a31 | 430 | handle_smd_irq(&smd_ch_list_dsp, notify_dsp_smd); |
2eb44eb9 BS |
431 | return IRQ_HANDLED; |
432 | } | |
433 | ||
434 | static void smd_fake_irq_handler(unsigned long arg) | |
435 | { | |
37521a31 BS |
436 | handle_smd_irq(&smd_ch_list_modem, notify_modem_smd); |
437 | handle_smd_irq(&smd_ch_list_dsp, notify_dsp_smd); | |
2eb44eb9 BS |
438 | } |
439 | ||
440 | static DECLARE_TASKLET(smd_fake_irq_tasklet, smd_fake_irq_handler, 0); | |
441 | ||
37521a31 BS |
442 | static inline int smd_need_int(struct smd_channel *ch) |
443 | { | |
444 | if (ch_is_open(ch)) { | |
445 | if (ch->recv->fHEAD || ch->recv->fTAIL || ch->recv->fSTATE) | |
446 | return 1; | |
447 | if (ch->recv->state != ch->last_state) | |
448 | return 1; | |
449 | } | |
450 | return 0; | |
451 | } | |
452 | ||
2eb44eb9 BS |
453 | void smd_sleep_exit(void) |
454 | { | |
455 | unsigned long flags; | |
456 | struct smd_channel *ch; | |
2eb44eb9 BS |
457 | int need_int = 0; |
458 | ||
459 | spin_lock_irqsave(&smd_lock, flags); | |
37521a31 BS |
460 | list_for_each_entry(ch, &smd_ch_list_modem, ch_list) { |
461 | if (smd_need_int(ch)) { | |
462 | need_int = 1; | |
463 | break; | |
464 | } | |
465 | } | |
466 | list_for_each_entry(ch, &smd_ch_list_dsp, ch_list) { | |
467 | if (smd_need_int(ch)) { | |
468 | need_int = 1; | |
469 | break; | |
2eb44eb9 BS |
470 | } |
471 | } | |
472 | spin_unlock_irqrestore(&smd_lock, flags); | |
473 | do_smd_probe(); | |
37521a31 | 474 | |
2eb44eb9 BS |
475 | if (need_int) { |
476 | if (msm_smd_debug_mask & MSM_SMD_DEBUG) | |
477 | pr_info("smd_sleep_exit need interrupt\n"); | |
478 | tasklet_schedule(&smd_fake_irq_tasklet); | |
479 | } | |
480 | } | |
481 | ||
482 | ||
483 | void smd_kick(smd_channel_t *ch) | |
484 | { | |
485 | unsigned long flags; | |
486 | unsigned tmp; | |
487 | ||
488 | spin_lock_irqsave(&smd_lock, flags); | |
489 | ch->update_state(ch); | |
490 | tmp = ch->recv->state; | |
491 | if (tmp != ch->last_state) { | |
492 | ch->last_state = tmp; | |
493 | if (tmp == SMD_SS_OPENED) | |
494 | ch->notify(ch->priv, SMD_EVENT_OPEN); | |
495 | else | |
496 | ch->notify(ch->priv, SMD_EVENT_CLOSE); | |
497 | } | |
498 | ch->notify(ch->priv, SMD_EVENT_DATA); | |
5b0f5a3f | 499 | ch->notify_other_cpu(); |
2eb44eb9 BS |
500 | spin_unlock_irqrestore(&smd_lock, flags); |
501 | } | |
502 | ||
5b0f5a3f | 503 | static int smd_is_packet(int chn, unsigned type) |
2eb44eb9 | 504 | { |
5b0f5a3f BS |
505 | type &= SMD_KIND_MASK; |
506 | if (type == SMD_KIND_PACKET) | |
507 | return 1; | |
508 | if (type == SMD_KIND_STREAM) | |
509 | return 0; | |
510 | ||
511 | /* older AMSS reports SMD_KIND_UNKNOWN always */ | |
2eb44eb9 BS |
512 | if ((chn > 4) || (chn == 1)) |
513 | return 1; | |
514 | else | |
515 | return 0; | |
516 | } | |
517 | ||
518 | static int smd_stream_write(smd_channel_t *ch, const void *_data, int len) | |
519 | { | |
520 | void *ptr; | |
521 | const unsigned char *buf = _data; | |
522 | unsigned xfer; | |
523 | int orig_len = len; | |
524 | ||
525 | if (len < 0) | |
526 | return -EINVAL; | |
527 | ||
528 | while ((xfer = ch_write_buffer(ch, &ptr)) != 0) { | |
529 | if (!ch_is_open(ch)) | |
530 | break; | |
531 | if (xfer > len) | |
532 | xfer = len; | |
533 | memcpy(ptr, buf, xfer); | |
534 | ch_write_done(ch, xfer); | |
535 | len -= xfer; | |
536 | buf += xfer; | |
537 | if (len == 0) | |
538 | break; | |
539 | } | |
540 | ||
5b0f5a3f | 541 | ch->notify_other_cpu(); |
2eb44eb9 BS |
542 | |
543 | return orig_len - len; | |
544 | } | |
545 | ||
546 | static int smd_packet_write(smd_channel_t *ch, const void *_data, int len) | |
547 | { | |
548 | unsigned hdr[5]; | |
549 | ||
550 | if (len < 0) | |
551 | return -EINVAL; | |
552 | ||
553 | if (smd_stream_write_avail(ch) < (len + SMD_HEADER_SIZE)) | |
554 | return -ENOMEM; | |
555 | ||
556 | hdr[0] = len; | |
557 | hdr[1] = hdr[2] = hdr[3] = hdr[4] = 0; | |
558 | ||
559 | smd_stream_write(ch, hdr, sizeof(hdr)); | |
560 | smd_stream_write(ch, _data, len); | |
561 | ||
562 | return len; | |
563 | } | |
564 | ||
565 | static int smd_stream_read(smd_channel_t *ch, void *data, int len) | |
566 | { | |
567 | int r; | |
568 | ||
569 | if (len < 0) | |
570 | return -EINVAL; | |
571 | ||
572 | r = ch_read(ch, data, len); | |
573 | if (r > 0) | |
5b0f5a3f | 574 | ch->notify_other_cpu(); |
2eb44eb9 BS |
575 | |
576 | return r; | |
577 | } | |
578 | ||
579 | static int smd_packet_read(smd_channel_t *ch, void *data, int len) | |
580 | { | |
581 | unsigned long flags; | |
582 | int r; | |
583 | ||
584 | if (len < 0) | |
585 | return -EINVAL; | |
586 | ||
587 | if (len > ch->current_packet) | |
588 | len = ch->current_packet; | |
589 | ||
590 | r = ch_read(ch, data, len); | |
591 | if (r > 0) | |
5b0f5a3f | 592 | ch->notify_other_cpu(); |
2eb44eb9 BS |
593 | |
594 | spin_lock_irqsave(&smd_lock, flags); | |
595 | ch->current_packet -= r; | |
596 | update_packet_state(ch); | |
597 | spin_unlock_irqrestore(&smd_lock, flags); | |
598 | ||
599 | return r; | |
600 | } | |
601 | ||
5b0f5a3f | 602 | static int smd_alloc_v2(struct smd_channel *ch) |
2eb44eb9 | 603 | { |
5b0f5a3f BS |
604 | struct smd_shared_v2 *shared2; |
605 | void *buffer; | |
606 | unsigned buffer_sz; | |
2eb44eb9 | 607 | |
5b0f5a3f BS |
608 | shared2 = smem_alloc(SMEM_SMD_BASE_ID + ch->n, sizeof(*shared2)); |
609 | buffer = smem_item(SMEM_SMD_FIFO_BASE_ID + ch->n, &buffer_sz); | |
610 | ||
611 | if (!buffer) | |
612 | return -1; | |
613 | ||
614 | /* buffer must be a power-of-two size */ | |
615 | if (buffer_sz & (buffer_sz - 1)) | |
616 | return -1; | |
617 | ||
618 | buffer_sz /= 2; | |
619 | ch->send = &shared2->ch0; | |
620 | ch->recv = &shared2->ch1; | |
621 | ch->send_data = buffer; | |
622 | ch->recv_data = buffer + buffer_sz; | |
623 | ch->fifo_size = buffer_sz; | |
624 | return 0; | |
625 | } | |
626 | ||
627 | static int smd_alloc_v1(struct smd_channel *ch) | |
628 | { | |
629 | struct smd_shared_v1 *shared1; | |
630 | shared1 = smem_alloc(ID_SMD_CHANNELS + ch->n, sizeof(*shared1)); | |
631 | if (!shared1) { | |
632 | pr_err("smd_alloc_channel() cid %d does not exist\n", ch->n); | |
633 | return -1; | |
2eb44eb9 | 634 | } |
5b0f5a3f BS |
635 | ch->send = &shared1->ch0; |
636 | ch->recv = &shared1->ch1; | |
637 | ch->send_data = shared1->data0; | |
638 | ch->recv_data = shared1->data1; | |
639 | ch->fifo_size = SMD_BUF_SIZE; | |
640 | return 0; | |
641 | } | |
642 | ||
643 | ||
644 | static void smd_alloc_channel(const char *name, uint32_t cid, uint32_t type) | |
645 | { | |
646 | struct smd_channel *ch; | |
2eb44eb9 BS |
647 | |
648 | ch = kzalloc(sizeof(struct smd_channel), GFP_KERNEL); | |
649 | if (ch == 0) { | |
650 | pr_err("smd_alloc_channel() out of memory\n"); | |
651 | return; | |
652 | } | |
2eb44eb9 BS |
653 | ch->n = cid; |
654 | ||
5b0f5a3f BS |
655 | if (smd_alloc_v2(ch) && smd_alloc_v1(ch)) { |
656 | kfree(ch); | |
657 | return; | |
658 | } | |
659 | ||
660 | ch->fifo_mask = ch->fifo_size - 1; | |
661 | ch->type = type; | |
662 | ||
663 | if ((type & SMD_TYPE_MASK) == SMD_TYPE_APPS_MODEM) | |
664 | ch->notify_other_cpu = notify_modem_smd; | |
665 | else | |
666 | ch->notify_other_cpu = notify_dsp_smd; | |
667 | ||
668 | if (smd_is_packet(cid, type)) { | |
2eb44eb9 BS |
669 | ch->read = smd_packet_read; |
670 | ch->write = smd_packet_write; | |
671 | ch->read_avail = smd_packet_read_avail; | |
672 | ch->write_avail = smd_packet_write_avail; | |
673 | ch->update_state = update_packet_state; | |
674 | } else { | |
675 | ch->read = smd_stream_read; | |
676 | ch->write = smd_stream_write; | |
677 | ch->read_avail = smd_stream_read_avail; | |
678 | ch->write_avail = smd_stream_write_avail; | |
679 | ch->update_state = update_stream_state; | |
680 | } | |
681 | ||
5b0f5a3f BS |
682 | if ((type & 0xff) == 0) |
683 | memcpy(ch->name, "SMD_", 4); | |
684 | else | |
685 | memcpy(ch->name, "DSP_", 4); | |
2eb44eb9 BS |
686 | memcpy(ch->name + 4, name, 20); |
687 | ch->name[23] = 0; | |
688 | ch->pdev.name = ch->name; | |
689 | ch->pdev.id = -1; | |
690 | ||
5b0f5a3f BS |
691 | pr_info("smd_alloc_channel() cid=%02d size=%05d '%s'\n", |
692 | ch->n, ch->fifo_size, ch->name); | |
2eb44eb9 BS |
693 | |
694 | mutex_lock(&smd_creation_mutex); | |
695 | list_add(&ch->ch_list, &smd_ch_closed_list); | |
696 | mutex_unlock(&smd_creation_mutex); | |
697 | ||
698 | platform_device_register(&ch->pdev); | |
699 | } | |
700 | ||
701 | static void do_nothing_notify(void *priv, unsigned flags) | |
702 | { | |
703 | } | |
704 | ||
705 | struct smd_channel *smd_get_channel(const char *name) | |
706 | { | |
707 | struct smd_channel *ch; | |
708 | ||
709 | mutex_lock(&smd_creation_mutex); | |
710 | list_for_each_entry(ch, &smd_ch_closed_list, ch_list) { | |
711 | if (!strcmp(name, ch->name)) { | |
712 | list_del(&ch->ch_list); | |
713 | mutex_unlock(&smd_creation_mutex); | |
714 | return ch; | |
715 | } | |
716 | } | |
717 | mutex_unlock(&smd_creation_mutex); | |
718 | ||
719 | return NULL; | |
720 | } | |
721 | ||
722 | int smd_open(const char *name, smd_channel_t **_ch, | |
723 | void *priv, void (*notify)(void *, unsigned)) | |
724 | { | |
725 | struct smd_channel *ch; | |
726 | unsigned long flags; | |
727 | ||
728 | if (smd_initialized == 0) { | |
729 | pr_info("smd_open() before smd_init()\n"); | |
730 | return -ENODEV; | |
731 | } | |
732 | ||
733 | ch = smd_get_channel(name); | |
734 | if (!ch) | |
735 | return -ENODEV; | |
736 | ||
737 | if (notify == 0) | |
738 | notify = do_nothing_notify; | |
739 | ||
740 | ch->notify = notify; | |
741 | ch->current_packet = 0; | |
742 | ch->last_state = SMD_SS_CLOSED; | |
743 | ch->priv = priv; | |
744 | ||
745 | *_ch = ch; | |
746 | ||
747 | spin_lock_irqsave(&smd_lock, flags); | |
37521a31 BS |
748 | |
749 | if ((ch->type & SMD_TYPE_MASK) == SMD_TYPE_APPS_MODEM) | |
750 | list_add(&ch->ch_list, &smd_ch_list_modem); | |
751 | else | |
752 | list_add(&ch->ch_list, &smd_ch_list_dsp); | |
2eb44eb9 BS |
753 | |
754 | /* If the remote side is CLOSING, we need to get it to | |
755 | * move to OPENING (which we'll do by moving from CLOSED to | |
756 | * OPENING) and then get it to move from OPENING to | |
757 | * OPENED (by doing the same state change ourselves). | |
758 | * | |
759 | * Otherwise, it should be OPENING and we can move directly | |
760 | * to OPENED so that it will follow. | |
761 | */ | |
762 | if (ch->recv->state == SMD_SS_CLOSING) { | |
763 | ch->send->head = 0; | |
5b0f5a3f | 764 | ch_set_state(ch, SMD_SS_OPENING); |
2eb44eb9 | 765 | } else { |
5b0f5a3f | 766 | ch_set_state(ch, SMD_SS_OPENED); |
2eb44eb9 BS |
767 | } |
768 | spin_unlock_irqrestore(&smd_lock, flags); | |
769 | smd_kick(ch); | |
770 | ||
771 | return 0; | |
772 | } | |
773 | ||
774 | int smd_close(smd_channel_t *ch) | |
775 | { | |
776 | unsigned long flags; | |
777 | ||
778 | pr_info("smd_close(%p)\n", ch); | |
779 | ||
780 | if (ch == 0) | |
781 | return -1; | |
782 | ||
783 | spin_lock_irqsave(&smd_lock, flags); | |
784 | ch->notify = do_nothing_notify; | |
785 | list_del(&ch->ch_list); | |
5b0f5a3f | 786 | ch_set_state(ch, SMD_SS_CLOSED); |
2eb44eb9 BS |
787 | spin_unlock_irqrestore(&smd_lock, flags); |
788 | ||
789 | mutex_lock(&smd_creation_mutex); | |
790 | list_add(&ch->ch_list, &smd_ch_closed_list); | |
791 | mutex_unlock(&smd_creation_mutex); | |
792 | ||
793 | return 0; | |
794 | } | |
795 | ||
796 | int smd_read(smd_channel_t *ch, void *data, int len) | |
797 | { | |
798 | return ch->read(ch, data, len); | |
799 | } | |
800 | ||
801 | int smd_write(smd_channel_t *ch, const void *data, int len) | |
802 | { | |
803 | return ch->write(ch, data, len); | |
804 | } | |
805 | ||
806 | int smd_read_avail(smd_channel_t *ch) | |
807 | { | |
808 | return ch->read_avail(ch); | |
809 | } | |
810 | ||
811 | int smd_write_avail(smd_channel_t *ch) | |
812 | { | |
813 | return ch->write_avail(ch); | |
814 | } | |
815 | ||
816 | int smd_wait_until_readable(smd_channel_t *ch, int bytes) | |
817 | { | |
818 | return -1; | |
819 | } | |
820 | ||
821 | int smd_wait_until_writable(smd_channel_t *ch, int bytes) | |
822 | { | |
823 | return -1; | |
824 | } | |
825 | ||
826 | int smd_cur_packet_size(smd_channel_t *ch) | |
827 | { | |
828 | return ch->current_packet; | |
829 | } | |
830 | ||
831 | ||
832 | /* ------------------------------------------------------------------------- */ | |
833 | ||
834 | void *smem_alloc(unsigned id, unsigned size) | |
835 | { | |
836 | return smem_find(id, size); | |
837 | } | |
838 | ||
03e00cd3 | 839 | void *smem_item(unsigned id, unsigned *size) |
2eb44eb9 BS |
840 | { |
841 | struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE; | |
842 | struct smem_heap_entry *toc = shared->heap_toc; | |
843 | ||
844 | if (id >= SMEM_NUM_ITEMS) | |
845 | return 0; | |
846 | ||
847 | if (toc[id].allocated) { | |
848 | *size = toc[id].size; | |
849 | return (void *) (MSM_SHARED_RAM_BASE + toc[id].offset); | |
5b0f5a3f BS |
850 | } else { |
851 | *size = 0; | |
2eb44eb9 BS |
852 | } |
853 | ||
854 | return 0; | |
855 | } | |
856 | ||
857 | void *smem_find(unsigned id, unsigned size_in) | |
858 | { | |
859 | unsigned size; | |
860 | void *ptr; | |
861 | ||
5b0f5a3f | 862 | ptr = smem_item(id, &size); |
2eb44eb9 BS |
863 | if (!ptr) |
864 | return 0; | |
865 | ||
866 | size_in = ALIGN(size_in, 8); | |
867 | if (size_in != size) { | |
868 | pr_err("smem_find(%d, %d): wrong size %d\n", | |
869 | id, size_in, size); | |
870 | return 0; | |
871 | } | |
872 | ||
873 | return ptr; | |
874 | } | |
875 | ||
876 | static irqreturn_t smsm_irq_handler(int irq, void *data) | |
877 | { | |
878 | unsigned long flags; | |
5b0f5a3f | 879 | unsigned apps, modm; |
2eb44eb9 BS |
880 | |
881 | spin_lock_irqsave(&smem_lock, flags); | |
2eb44eb9 | 882 | |
28379410 AH |
883 | apps = raw_smsm_get_state(SMSM_STATE_APPS); |
884 | modm = raw_smsm_get_state(SMSM_STATE_MODEM); | |
2eb44eb9 | 885 | |
5b0f5a3f BS |
886 | if (msm_smd_debug_mask & MSM_SMSM_DEBUG) |
887 | pr_info("<SM %08x %08x>\n", apps, modm); | |
888 | if (modm & SMSM_RESET) { | |
889 | handle_modem_crash(); | |
2eb44eb9 | 890 | } |
5b0f5a3f BS |
891 | do_smd_probe(); |
892 | ||
2eb44eb9 BS |
893 | spin_unlock_irqrestore(&smem_lock, flags); |
894 | return IRQ_HANDLED; | |
895 | } | |
896 | ||
28379410 AH |
897 | int smsm_change_state(enum smsm_state_item item, |
898 | uint32_t clear_mask, uint32_t set_mask) | |
2eb44eb9 BS |
899 | { |
900 | unsigned long flags; | |
5b0f5a3f | 901 | unsigned state; |
28379410 | 902 | unsigned addr = smd_info.state + item * 4; |
5b0f5a3f BS |
903 | |
904 | if (!smd_info.ready) | |
905 | return -EIO; | |
2eb44eb9 BS |
906 | |
907 | spin_lock_irqsave(&smem_lock, flags); | |
908 | ||
28379410 | 909 | if (raw_smsm_get_state(SMSM_STATE_MODEM) & SMSM_RESET) |
5b0f5a3f BS |
910 | handle_modem_crash(); |
911 | ||
28379410 AH |
912 | state = (readl(addr) & ~clear_mask) | set_mask; |
913 | writel(state, addr); | |
5b0f5a3f BS |
914 | |
915 | if (msm_smd_debug_mask & MSM_SMSM_DEBUG) | |
28379410 | 916 | pr_info("smsm_change_state %d %x\n", item, state); |
5b0f5a3f | 917 | notify_other_smsm(); |
2eb44eb9 BS |
918 | |
919 | spin_unlock_irqrestore(&smem_lock, flags); | |
920 | ||
2eb44eb9 BS |
921 | return 0; |
922 | } | |
923 | ||
28379410 | 924 | uint32_t smsm_get_state(enum smsm_state_item item) |
2eb44eb9 BS |
925 | { |
926 | unsigned long flags; | |
2eb44eb9 BS |
927 | uint32_t rv; |
928 | ||
929 | spin_lock_irqsave(&smem_lock, flags); | |
930 | ||
28379410 | 931 | rv = readl(smd_info.state + item * 4); |
2eb44eb9 | 932 | |
28379410 | 933 | if (item == SMSM_STATE_MODEM && (rv & SMSM_RESET)) |
2eb44eb9 BS |
934 | handle_modem_crash(); |
935 | ||
936 | spin_unlock_irqrestore(&smem_lock, flags); | |
937 | ||
2eb44eb9 BS |
938 | return rv; |
939 | } | |
940 | ||
ec9d3d14 AH |
941 | #ifdef CONFIG_ARCH_MSM_SCORPION |
942 | ||
2eb44eb9 BS |
943 | int smsm_set_sleep_duration(uint32_t delay) |
944 | { | |
03e00cd3 BS |
945 | struct msm_dem_slave_data *ptr; |
946 | ||
947 | ptr = smem_find(SMEM_APPS_DEM_SLAVE_DATA, sizeof(*ptr)); | |
2eb44eb9 | 948 | if (ptr == NULL) { |
ec9d3d14 | 949 | pr_err("smsm_set_sleep_duration <SM NO APPS_DEM_SLAVE_DATA>\n"); |
2eb44eb9 BS |
950 | return -EIO; |
951 | } | |
952 | if (msm_smd_debug_mask & MSM_SMSM_DEBUG) | |
953 | pr_info("smsm_set_sleep_duration %d -> %d\n", | |
ec9d3d14 AH |
954 | ptr->sleep_time, delay); |
955 | ptr->sleep_time = delay; | |
2eb44eb9 BS |
956 | return 0; |
957 | } | |
958 | ||
ec9d3d14 AH |
959 | #else |
960 | ||
961 | int smsm_set_sleep_duration(uint32_t delay) | |
2eb44eb9 | 962 | { |
ec9d3d14 | 963 | uint32_t *ptr; |
2eb44eb9 | 964 | |
03e00cd3 | 965 | ptr = smem_find(SMEM_SMSM_SLEEP_DELAY, sizeof(*ptr)); |
2eb44eb9 | 966 | if (ptr == NULL) { |
ec9d3d14 | 967 | pr_err("smsm_set_sleep_duration <SM NO SLEEP_DELAY>\n"); |
2eb44eb9 BS |
968 | return -EIO; |
969 | } | |
970 | if (msm_smd_debug_mask & MSM_SMSM_DEBUG) | |
ec9d3d14 AH |
971 | pr_info("smsm_set_sleep_duration %d -> %d\n", |
972 | *ptr, delay); | |
973 | *ptr = delay; | |
2eb44eb9 BS |
974 | return 0; |
975 | } | |
976 | ||
ec9d3d14 AH |
977 | #endif |
978 | ||
2eb44eb9 BS |
979 | int smd_core_init(void) |
980 | { | |
981 | int r; | |
982 | pr_info("smd_core_init()\n"); | |
983 | ||
5b0f5a3f BS |
984 | /* wait for essential items to be initialized */ |
985 | for (;;) { | |
986 | unsigned size; | |
987 | void *state; | |
988 | state = smem_item(SMEM_SMSM_SHARED_STATE, &size); | |
28379410 AH |
989 | if (size == SMSM_V1_SIZE || size == SMSM_V2_SIZE) { |
990 | smd_info.state = (unsigned)state; | |
5b0f5a3f BS |
991 | break; |
992 | } | |
993 | } | |
994 | ||
995 | smd_info.ready = 1; | |
996 | ||
37521a31 | 997 | r = request_irq(INT_A9_M2A_0, smd_modem_irq_handler, |
2eb44eb9 BS |
998 | IRQF_TRIGGER_RISING, "smd_dev", 0); |
999 | if (r < 0) | |
1000 | return r; | |
1001 | r = enable_irq_wake(INT_A9_M2A_0); | |
1002 | if (r < 0) | |
1003 | pr_err("smd_core_init: enable_irq_wake failed for A9_M2A_0\n"); | |
1004 | ||
1005 | r = request_irq(INT_A9_M2A_5, smsm_irq_handler, | |
1006 | IRQF_TRIGGER_RISING, "smsm_dev", 0); | |
1007 | if (r < 0) { | |
1008 | free_irq(INT_A9_M2A_0, 0); | |
1009 | return r; | |
1010 | } | |
1011 | r = enable_irq_wake(INT_A9_M2A_5); | |
1012 | if (r < 0) | |
1013 | pr_err("smd_core_init: enable_irq_wake failed for A9_M2A_5\n"); | |
1014 | ||
37521a31 BS |
1015 | #if defined(CONFIG_QDSP6) |
1016 | r = request_irq(INT_ADSP_A11, smd_dsp_irq_handler, | |
1017 | IRQF_TRIGGER_RISING, "smd_dsp", 0); | |
1018 | if (r < 0) { | |
1019 | free_irq(INT_A9_M2A_0, 0); | |
1020 | free_irq(INT_A9_M2A_5, 0); | |
1021 | return r; | |
1022 | } | |
1023 | #endif | |
1024 | ||
5b0f5a3f BS |
1025 | /* check for any SMD channels that may already exist */ |
1026 | do_smd_probe(); | |
1027 | ||
1028 | /* indicate that we're up and running */ | |
28379410 | 1029 | smsm_change_state(SMSM_STATE_APPS, |
ec9d3d14 AH |
1030 | ~0, SMSM_INIT | SMSM_SMDINIT | SMSM_RPCINIT | SMSM_RUN); |
1031 | #ifdef CONFIG_ARCH_MSM_SCORPION | |
1032 | smsm_change_state(SMSM_STATE_APPS_DEM, ~0, 0); | |
1033 | #endif | |
2eb44eb9 BS |
1034 | |
1035 | pr_info("smd_core_init() done\n"); | |
1036 | ||
1037 | return 0; | |
1038 | } | |
1039 | ||
2eb44eb9 BS |
1040 | static int __init msm_smd_probe(struct platform_device *pdev) |
1041 | { | |
1042 | pr_info("smd_init()\n"); | |
1043 | ||
1044 | INIT_WORK(&probe_work, smd_channel_probe_worker); | |
1045 | ||
1046 | if (smd_core_init()) { | |
1047 | pr_err("smd_core_init() failed\n"); | |
1048 | return -1; | |
1049 | } | |
1050 | ||
1051 | do_smd_probe(); | |
1052 | ||
1053 | msm_check_for_modem_crash = check_for_modem_crash; | |
1054 | ||
2eb44eb9 BS |
1055 | smd_initialized = 1; |
1056 | ||
1057 | return 0; | |
1058 | } | |
1059 | ||
1060 | static struct platform_driver msm_smd_driver = { | |
1061 | .probe = msm_smd_probe, | |
1062 | .driver = { | |
1063 | .name = MODULE_NAME, | |
1064 | .owner = THIS_MODULE, | |
1065 | }, | |
1066 | }; | |
1067 | ||
1068 | static int __init msm_smd_init(void) | |
1069 | { | |
1070 | return platform_driver_register(&msm_smd_driver); | |
1071 | } | |
1072 | ||
1073 | module_init(msm_smd_init); | |
1074 | ||
1075 | MODULE_DESCRIPTION("MSM Shared Memory Core"); | |
1076 | MODULE_AUTHOR("Brian Swetland <swetland@google.com>"); | |
1077 | MODULE_LICENSE("GPL"); |