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