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