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