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