blob: 33ff076941a64611d8175caac49cdc8b18eb7246 [file] [log] [blame]
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001/* arch/arm/mach-msm/smd.c
2 *
3 * Copyright (C) 2007 Google, Inc.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004 * Copyright (c) 2008-2011, Code Aurora Forum. All rights reserved.
Brian Swetland2eb44eb2008-09-29 16:00:48 -07005 * Author: Brian Swetland <swetland@google.com>
6 *
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
18#include <linux/platform_device.h>
19#include <linux/module.h>
20#include <linux/fs.h>
21#include <linux/cdev.h>
22#include <linux/device.h>
23#include <linux/wait.h>
24#include <linux/interrupt.h>
25#include <linux/irq.h>
26#include <linux/list.h>
27#include <linux/slab.h>
Brian Swetland2eb44eb2008-09-29 16:00:48 -070028#include <linux/delay.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070029#include <linux/io.h>
30#include <linux/termios.h>
31#include <linux/ctype.h>
32#include <linux/remote_spinlock.h>
33#include <linux/uaccess.h>
Brian Swetland2eb44eb2008-09-29 16:00:48 -070034#include <mach/msm_smd.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070035#include <mach/msm_iomap.h>
Brian Swetland2eb44eb2008-09-29 16:00:48 -070036#include <mach/system.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070037#include <mach/subsystem_notif.h>
Brian Swetland2eb44eb2008-09-29 16:00:48 -070038
39#include "smd_private.h"
40#include "proc_comm.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070041#include "modem_notifier.h"
Brian Swetland2eb44eb2008-09-29 16:00:48 -070042
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070043#if defined(CONFIG_ARCH_QSD8X50) || defined(CONFIG_ARCH_MSM8X60) \
44 || defined(CONFIG_ARCH_MSM8960) || defined(CONFIG_ARCH_FSM9XXX)
Brian Swetland37521a32009-07-01 18:30:47 -070045#define CONFIG_QDSP6 1
46#endif
47
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070048#if defined(CONFIG_ARCH_MSM8X60) || defined(CONFIG_ARCH_MSM8960)
49#define CONFIG_DSPS 1
50#endif
51
52#if defined(CONFIG_ARCH_MSM8960)
53#define CONFIG_WCNSS 1
Jeff Hugo6a8057c2011-08-16 13:47:12 -060054#define CONFIG_DSPS_SMSM 1
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070055#endif
Brian Swetland2eb44eb2008-09-29 16:00:48 -070056
57#define MODULE_NAME "msm_smd"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070058#define SMEM_VERSION 0x000B
59#define SMD_VERSION 0x00020000
60
61uint32_t SMSM_NUM_ENTRIES = 8;
62uint32_t SMSM_NUM_HOSTS = 3;
Brian Swetland2eb44eb2008-09-29 16:00:48 -070063
64enum {
65 MSM_SMD_DEBUG = 1U << 0,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070066 MSM_SMSM_DEBUG = 1U << 1,
67 MSM_SMD_INFO = 1U << 2,
68 MSM_SMSM_INFO = 1U << 3,
69};
70
71struct smsm_shared_info {
72 uint32_t *state;
73 uint32_t *intr_mask;
74 uint32_t *intr_mux;
75};
76
77static struct smsm_shared_info smsm_info;
78
79struct smsm_size_info_type {
80 uint32_t num_hosts;
81 uint32_t num_entries;
82 uint32_t reserved0;
83 uint32_t reserved1;
84};
85
86struct smsm_state_cb_info {
87 struct list_head cb_list;
88 uint32_t mask;
89 void *data;
90 void (*notify)(void *data, uint32_t old_state, uint32_t new_state);
91};
92
93struct smsm_state_info {
94 struct list_head callbacks;
95 uint32_t last_value;
96};
97
98#define SMSM_STATE_ADDR(entry) (smsm_info.state + entry)
99#define SMSM_INTR_MASK_ADDR(entry, host) (smsm_info.intr_mask + \
100 entry * SMSM_NUM_HOSTS + host)
101#define SMSM_INTR_MUX_ADDR(entry) (smsm_info.intr_mux + entry)
102
103/* Internal definitions which are not exported in some targets */
104enum {
105 SMSM_APPS_DEM_I = 3,
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700106};
107
108static int msm_smd_debug_mask;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700109module_param_named(debug_mask, msm_smd_debug_mask,
110 int, S_IRUGO | S_IWUSR | S_IWGRP);
111
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700112#if defined(CONFIG_MSM_SMD_DEBUG)
113#define SMD_DBG(x...) do { \
114 if (msm_smd_debug_mask & MSM_SMD_DEBUG) \
115 printk(KERN_DEBUG x); \
116 } while (0)
117
118#define SMSM_DBG(x...) do { \
119 if (msm_smd_debug_mask & MSM_SMSM_DEBUG) \
120 printk(KERN_DEBUG x); \
121 } while (0)
122
123#define SMD_INFO(x...) do { \
124 if (msm_smd_debug_mask & MSM_SMD_INFO) \
125 printk(KERN_INFO x); \
126 } while (0)
127
128#define SMSM_INFO(x...) do { \
129 if (msm_smd_debug_mask & MSM_SMSM_INFO) \
130 printk(KERN_INFO x); \
131 } while (0)
132#else
133#define SMD_DBG(x...) do { } while (0)
134#define SMSM_DBG(x...) do { } while (0)
135#define SMD_INFO(x...) do { } while (0)
136#define SMSM_INFO(x...) do { } while (0)
137#endif
138
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700139static unsigned last_heap_free = 0xffffffff;
140
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700141static inline void smd_write_intr(unsigned int val,
142 const void __iomem *addr);
143
144#if defined(CONFIG_ARCH_MSM7X30)
145#define MSM_TRIG_A2M_SMD_INT \
146 (smd_write_intr(1 << 0, MSM_GCC_BASE + 0x8))
147#define MSM_TRIG_A2Q6_SMD_INT \
148 (smd_write_intr(1 << 8, MSM_GCC_BASE + 0x8))
149#define MSM_TRIG_A2M_SMSM_INT \
150 (smd_write_intr(1 << 5, MSM_GCC_BASE + 0x8))
151#define MSM_TRIG_A2Q6_SMSM_INT \
152 (smd_write_intr(1 << 8, MSM_GCC_BASE + 0x8))
153#define MSM_TRIG_A2DSPS_SMD_INT
Jeff Hugo6a8057c2011-08-16 13:47:12 -0600154#define MSM_TRIG_A2DSPS_SMSM_INT
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700155#define MSM_TRIG_A2WCNSS_SMD_INT
156#define MSM_TRIG_A2WCNSS_SMSM_INT
157#elif defined(CONFIG_ARCH_MSM8X60)
158#define MSM_TRIG_A2M_SMD_INT \
159 (smd_write_intr(1 << 3, MSM_GCC_BASE + 0x8))
160#define MSM_TRIG_A2Q6_SMD_INT \
161 (smd_write_intr(1 << 15, MSM_GCC_BASE + 0x8))
162#define MSM_TRIG_A2M_SMSM_INT \
163 (smd_write_intr(1 << 4, MSM_GCC_BASE + 0x8))
164#define MSM_TRIG_A2Q6_SMSM_INT \
165 (smd_write_intr(1 << 14, MSM_GCC_BASE + 0x8))
166#define MSM_TRIG_A2DSPS_SMD_INT \
167 (smd_write_intr(1, MSM_SIC_NON_SECURE_BASE + 0x4080))
Jeff Hugo6a8057c2011-08-16 13:47:12 -0600168#define MSM_TRIG_A2DSPS_SMSM_INT
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700169#define MSM_TRIG_A2WCNSS_SMD_INT
170#define MSM_TRIG_A2WCNSS_SMSM_INT
171#elif defined(CONFIG_ARCH_MSM8960)
172#define MSM_TRIG_A2M_SMD_INT \
173 (smd_write_intr(1 << 3, MSM_APCS_GCC_BASE + 0x8))
174#define MSM_TRIG_A2Q6_SMD_INT \
175 (smd_write_intr(1 << 15, MSM_APCS_GCC_BASE + 0x8))
176#define MSM_TRIG_A2M_SMSM_INT \
177 (smd_write_intr(1 << 4, MSM_APCS_GCC_BASE + 0x8))
178#define MSM_TRIG_A2Q6_SMSM_INT \
179 (smd_write_intr(1 << 14, MSM_APCS_GCC_BASE + 0x8))
180#define MSM_TRIG_A2DSPS_SMD_INT \
181 (smd_write_intr(1, MSM_SIC_NON_SECURE_BASE + 0x4080))
Jeff Hugo6a8057c2011-08-16 13:47:12 -0600182#define MSM_TRIG_A2DSPS_SMSM_INT \
183 (smd_write_intr(1, MSM_SIC_NON_SECURE_BASE + 0x4094))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700184#define MSM_TRIG_A2WCNSS_SMD_INT \
185 (smd_write_intr(1 << 25, MSM_APCS_GCC_BASE + 0x8))
186#define MSM_TRIG_A2WCNSS_SMSM_INT \
187 (smd_write_intr(1 << 23, MSM_APCS_GCC_BASE + 0x8))
188#elif defined(CONFIG_ARCH_FSM9XXX)
189#define MSM_TRIG_A2Q6_SMD_INT \
190 (smd_write_intr(1 << 10, MSM_GCC_BASE + 0x8))
191#define MSM_TRIG_A2Q6_SMSM_INT \
192 (smd_write_intr(1 << 10, MSM_GCC_BASE + 0x8))
193#define MSM_TRIG_A2M_SMD_INT \
194 (smd_write_intr(1 << 0, MSM_GCC_BASE + 0x8))
195#define MSM_TRIG_A2M_SMSM_INT \
196 (smd_write_intr(1 << 5, MSM_GCC_BASE + 0x8))
197#define MSM_TRIG_A2DSPS_SMD_INT
Jeff Hugo6a8057c2011-08-16 13:47:12 -0600198#define MSM_TRIG_A2DSPS_SMSM_INT
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700199#define MSM_TRIG_A2WCNSS_SMD_INT
200#define MSM_TRIG_A2WCNSS_SMSM_INT
201#else
202#define MSM_TRIG_A2M_SMD_INT \
203 (smd_write_intr(1, MSM_CSR_BASE + 0x400 + (0) * 4))
204#define MSM_TRIG_A2Q6_SMD_INT \
205 (smd_write_intr(1, MSM_CSR_BASE + 0x400 + (8) * 4))
206#define MSM_TRIG_A2M_SMSM_INT \
207 (smd_write_intr(1, MSM_CSR_BASE + 0x400 + (5) * 4))
208#define MSM_TRIG_A2Q6_SMSM_INT \
209 (smd_write_intr(1, MSM_CSR_BASE + 0x400 + (8) * 4))
210#define MSM_TRIG_A2DSPS_SMD_INT
Jeff Hugo6a8057c2011-08-16 13:47:12 -0600211#define MSM_TRIG_A2DSPS_SMSM_INT
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700212#define MSM_TRIG_A2WCNSS_SMD_INT
213#define MSM_TRIG_A2WCNSS_SMSM_INT
Brian Swetland37521a32009-07-01 18:30:47 -0700214#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700215
216#define SMD_LOOPBACK_CID 100
217
218static LIST_HEAD(smd_ch_list_loopback);
219static irqreturn_t smsm_irq_handler(int irq, void *data);
220static void smd_fake_irq_handler(unsigned long arg);
221
222static void notify_smsm_cb_clients_worker(struct work_struct *work);
223static DECLARE_WORK(smsm_cb_work, notify_smsm_cb_clients_worker);
224static DEFINE_SPINLOCK(smsm_lock);
225static struct smsm_state_info *smsm_states;
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +0530226static int spinlocks_initialized;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700227
228static inline void smd_write_intr(unsigned int val,
229 const void __iomem *addr)
230{
231 wmb();
232 __raw_writel(val, addr);
233}
234
235#ifdef CONFIG_WCNSS
236static inline void wakeup_v1_riva(void)
237{
238 /*
239 * workaround hack for RIVA v1 hardware bug
240 * trigger GPIO 40 to wake up RIVA from power collaspe
241 * not to be sent to customers
242 */
243 __raw_writel(0x0, MSM_TLMM_BASE + 0x1284);
244 __raw_writel(0x2, MSM_TLMM_BASE + 0x1284);
245 /* end workaround */
246}
247#else
248static inline void wakeup_v1_riva(void) {}
249#endif
250
251static void notify_other_smsm(uint32_t smsm_entry, uint32_t notify_mask)
252{
253 /* older protocol don't use smsm_intr_mask,
254 but still communicates with modem */
255 if (!smsm_info.intr_mask ||
256 (__raw_readl(SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_MODEM))
257 & notify_mask))
258 MSM_TRIG_A2M_SMSM_INT;
259
260 if (smsm_info.intr_mask &&
261 (__raw_readl(SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_Q6))
262 & notify_mask)) {
263#if !defined(CONFIG_ARCH_MSM8X60) && !defined(MCONFIG_ARCH_MSM8960)
264 uint32_t mux_val;
265
266 if (smsm_info.intr_mux) {
267 mux_val = __raw_readl(
268 SMSM_INTR_MUX_ADDR(SMEM_APPS_Q6_SMSM));
269 mux_val++;
270 __raw_writel(mux_val,
271 SMSM_INTR_MUX_ADDR(SMEM_APPS_Q6_SMSM));
272 }
273#endif
274 MSM_TRIG_A2Q6_SMSM_INT;
275 }
276
277 if (smsm_info.intr_mask &&
278 (__raw_readl(SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_WCNSS))
279 & notify_mask)) {
280 wakeup_v1_riva();
281 MSM_TRIG_A2WCNSS_SMSM_INT;
282 }
283
Jeff Hugo6a8057c2011-08-16 13:47:12 -0600284 if (smsm_info.intr_mask &&
285 (__raw_readl(SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_DSPS))
286 & notify_mask)) {
287 MSM_TRIG_A2DSPS_SMSM_INT;
288 }
289
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700290 schedule_work(&smsm_cb_work);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700291}
292
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700293static inline void notify_modem_smd(void)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700294{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700295 MSM_TRIG_A2M_SMD_INT;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700296}
297
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700298static inline void notify_dsp_smd(void)
299{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700300 MSM_TRIG_A2Q6_SMD_INT;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700301}
302
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700303static inline void notify_dsps_smd(void)
304{
305 MSM_TRIG_A2DSPS_SMD_INT;
306}
307
308static inline void notify_wcnss_smd(void)
309{
310 wakeup_v1_riva();
311 MSM_TRIG_A2WCNSS_SMD_INT;
312}
313
314void smd_diag(void)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700315{
316 char *x;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700317 int size;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700318
319 x = smem_find(ID_DIAG_ERR_MSG, SZ_DIAG_ERR_MSG);
320 if (x != 0) {
321 x[SZ_DIAG_ERR_MSG - 1] = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700322 SMD_INFO("smem: DIAG '%s'\n", x);
323 }
324
325 x = smem_get_entry(SMEM_ERR_CRASH_LOG, &size);
326 if (x != 0) {
327 x[size - 1] = 0;
328 pr_err("smem: CRASH LOG\n'%s'\n", x);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700329 }
330}
331
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700332
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700333static void handle_modem_crash(void)
334{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700335 pr_err("MODEM/AMSS has CRASHED\n");
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700336 smd_diag();
337
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700338 /* hard reboot if possible FIXME
339 if (msm_reset_hook)
340 msm_reset_hook();
341 */
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700342
343 /* in this case the modem or watchdog should reboot us */
344 for (;;)
345 ;
346}
347
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700348int smsm_check_for_modem_crash(void)
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700349{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700350 /* if the modem's not ready yet, we have to hope for the best */
351 if (!smsm_info.state)
352 return 0;
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700353
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700354 if (__raw_readl(SMSM_STATE_ADDR(SMSM_MODEM_STATE)) & SMSM_RESET) {
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700355 handle_modem_crash();
356 return -1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700357 }
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700358 return 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700359}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700360EXPORT_SYMBOL(smsm_check_for_modem_crash);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700361
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700362/* the spinlock is used to synchronize between the
Brian Swetland03e00cd2009-07-01 17:58:37 -0700363 * irq handler and code that mutates the channel
364 * list or fiddles with channel state
365 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700366static DEFINE_SPINLOCK(smd_lock);
Brian Swetland03e00cd2009-07-01 17:58:37 -0700367DEFINE_SPINLOCK(smem_lock);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700368
369/* the mutex is used during open() and close()
Brian Swetland03e00cd2009-07-01 17:58:37 -0700370 * operations to avoid races while creating or
371 * destroying smd_channel structures
372 */
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700373static DEFINE_MUTEX(smd_creation_mutex);
374
375static int smd_initialized;
376
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700377struct smd_shared_v1 {
378 struct smd_half_channel ch0;
379 unsigned char data0[SMD_BUF_SIZE];
380 struct smd_half_channel ch1;
381 unsigned char data1[SMD_BUF_SIZE];
382};
383
384struct smd_shared_v2 {
385 struct smd_half_channel ch0;
386 struct smd_half_channel ch1;
387};
388
389struct smd_channel {
390 volatile struct smd_half_channel *send;
391 volatile struct smd_half_channel *recv;
392 unsigned char *send_data;
393 unsigned char *recv_data;
394 unsigned fifo_size;
395 unsigned fifo_mask;
396 struct list_head ch_list;
397
398 unsigned current_packet;
399 unsigned n;
400 void *priv;
401 void (*notify)(void *priv, unsigned flags);
402
403 int (*read)(smd_channel_t *ch, void *data, int len, int user_buf);
404 int (*write)(smd_channel_t *ch, const void *data, int len,
405 int user_buf);
406 int (*read_avail)(smd_channel_t *ch);
407 int (*write_avail)(smd_channel_t *ch);
408 int (*read_from_cb)(smd_channel_t *ch, void *data, int len,
409 int user_buf);
410
411 void (*update_state)(smd_channel_t *ch);
412 unsigned last_state;
413 void (*notify_other_cpu)(void);
414
415 char name[20];
416 struct platform_device pdev;
417 unsigned type;
418
419 int pending_pkt_sz;
420
421 char is_pkt_ch;
422};
423
424struct edge_to_pid {
425 uint32_t local_pid;
426 uint32_t remote_pid;
427};
428
Eric Holmberg7dfd1972011-09-09 16:07:57 -0600429/*
430 * SMD Processor ID's.
431 *
432 * For all processors that have both SMSM and SMD clients,
433 * the SMSM Processor ID and the SMD Processor ID will
434 * be the same. In cases where a processor only supports
435 * SMD, the entry will only exist in this enum.
436 */
437enum {
438 SMD_APPS = SMSM_APPS,
439 SMD_MODEM = SMSM_MODEM,
440 SMD_Q6 = SMSM_Q6,
441 SMD_WCNSS = SMSM_WCNSS,
442 SMD_DSPS = SMSM_DSPS,
443 SMD_MODEM_Q6_FW,
444};
445
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700446/**
447 * Maps edge type to local and remote processor ID's.
448 */
449static struct edge_to_pid edge_to_pids[] = {
450 [SMD_APPS_MODEM] = {SMSM_APPS, SMSM_MODEM},
451 [SMD_APPS_QDSP] = {SMSM_APPS, SMSM_Q6},
452 [SMD_MODEM_QDSP] = {SMSM_MODEM, SMSM_Q6},
453 [SMD_APPS_DSPS] = {SMSM_APPS, SMSM_DSPS},
454 [SMD_MODEM_DSPS] = {SMSM_MODEM, SMSM_DSPS},
455 [SMD_QDSP_DSPS] = {SMSM_Q6, SMSM_DSPS},
456 [SMD_APPS_WCNSS] = {SMSM_APPS, SMSM_WCNSS},
457 [SMD_MODEM_WCNSS] = {SMSM_MODEM, SMSM_WCNSS},
458 [SMD_QDSP_WCNSS] = {SMSM_Q6, SMSM_WCNSS},
459 [SMD_DSPS_WCNSS] = {SMSM_DSPS, SMSM_WCNSS},
Eric Holmberg7dfd1972011-09-09 16:07:57 -0600460 [SMD_APPS_Q6FW] = {SMSM_APPS, SMD_MODEM_Q6_FW},
461 [SMD_MODEM_Q6FW] = {SMSM_MODEM, SMD_MODEM_Q6_FW},
462 [SMD_QDSP_Q6FW] = {SMSM_Q6, SMD_MODEM_Q6_FW},
463 [SMD_DSPS_Q6FW] = {SMSM_DSPS, SMD_MODEM_Q6_FW},
464 [SMD_WCNSS_Q6FW] = {SMSM_WCNSS, SMD_MODEM_Q6_FW},
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700465};
466
467struct restart_notifier_block {
468 unsigned processor;
469 char *name;
470 struct notifier_block nb;
471};
472
473static struct platform_device loopback_tty_pdev = {.name = "LOOPBACK_TTY"};
474
475static LIST_HEAD(smd_ch_closed_list);
476static LIST_HEAD(smd_ch_closing_list);
477static LIST_HEAD(smd_ch_to_close_list);
478static LIST_HEAD(smd_ch_list_modem);
479static LIST_HEAD(smd_ch_list_dsp);
480static LIST_HEAD(smd_ch_list_dsps);
481static LIST_HEAD(smd_ch_list_wcnss);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700482
483static unsigned char smd_ch_allocated[64];
484static struct work_struct probe_work;
485
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700486static void finalize_channel_close_fn(struct work_struct *work);
487static DECLARE_WORK(finalize_channel_close_work, finalize_channel_close_fn);
488static struct workqueue_struct *channel_close_wq;
489
490static int smd_alloc_channel(struct smd_alloc_elm *alloc_elm);
491
492/* on smp systems, the probe might get called from multiple cores,
493 hence use a lock */
494static DEFINE_MUTEX(smd_probe_lock);
495
496static void smd_channel_probe_worker(struct work_struct *work)
497{
498 struct smd_alloc_elm *shared;
499 unsigned n;
500 uint32_t type;
501
502 shared = smem_find(ID_CH_ALLOC_TBL, sizeof(*shared) * 64);
503
504 if (!shared) {
505 pr_err("%s: allocation table not initialized\n", __func__);
506 return;
507 }
508
509 mutex_lock(&smd_probe_lock);
510 for (n = 0; n < 64; n++) {
511 if (smd_ch_allocated[n])
512 continue;
513
514 /* channel should be allocated only if APPS
515 processor is involved */
516 type = SMD_CHANNEL_TYPE(shared[n].type);
517 if ((type != SMD_APPS_MODEM) && (type != SMD_APPS_QDSP) &&
518 (type != SMD_APPS_DSPS) && (type != SMD_APPS_WCNSS))
519 continue;
520 if (!shared[n].ref_count)
521 continue;
522 if (!shared[n].name[0])
523 continue;
524
525 if (!smd_alloc_channel(&shared[n]))
526 smd_ch_allocated[n] = 1;
527 else
528 SMD_INFO("Probe skipping ch %d, not allocated\n", n);
529 }
530 mutex_unlock(&smd_probe_lock);
531}
532
533/**
534 * Lookup processor ID and determine if it belongs to the proved edge
535 * type.
536 *
537 * @shared2: Pointer to v2 shared channel structure
538 * @type: Edge type
539 * @pid: Processor ID of processor on edge
540 * @local_ch: Channel that belongs to processor @pid
541 * @remote_ch: Other side of edge contained @pid
542 *
543 * Returns 0 for not on edge, 1 for found on edge
544 */
545static int pid_is_on_edge(struct smd_shared_v2 *shared2,
546 uint32_t type, uint32_t pid,
547 struct smd_half_channel **local_ch,
548 struct smd_half_channel **remote_ch
549 )
550{
551 int ret = 0;
552 struct edge_to_pid *edge;
553
554 *local_ch = 0;
555 *remote_ch = 0;
556
557 if (!shared2 || (type >= ARRAY_SIZE(edge_to_pids)))
558 return 0;
559
560 edge = &edge_to_pids[type];
561 if (edge->local_pid != edge->remote_pid) {
562 if (pid == edge->local_pid) {
563 *local_ch = &shared2->ch0;
564 *remote_ch = &shared2->ch1;
565 ret = 1;
566 } else if (pid == edge->remote_pid) {
567 *local_ch = &shared2->ch1;
568 *remote_ch = &shared2->ch0;
569 ret = 1;
570 }
571 }
572
573 return ret;
574}
575
576
577static void smd_channel_reset_state(struct smd_alloc_elm *shared,
578 unsigned new_state, unsigned pid)
579{
580 unsigned n;
581 struct smd_shared_v2 *shared2;
582 uint32_t type;
583 struct smd_half_channel *local_ch;
584 struct smd_half_channel *remote_ch;
585
586 for (n = 0; n < SMD_CHANNELS; n++) {
587 if (!shared[n].ref_count)
588 continue;
589 if (!shared[n].name[0])
590 continue;
591
592 type = SMD_CHANNEL_TYPE(shared[n].type);
593 shared2 = smem_alloc(SMEM_SMD_BASE_ID + n, sizeof(*shared2));
594 if (!shared2)
595 continue;
596
Eric Holmberg7dfd1972011-09-09 16:07:57 -0600597 if (pid_is_on_edge(shared2, type, pid, &local_ch, &remote_ch) ||
598 (pid == SMSM_MODEM &&
599 pid_is_on_edge(shared2, type, SMD_MODEM_Q6_FW,
600 &local_ch, &remote_ch))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700601
602 /* force remote state for processor being restarted */
603 if (local_ch->state != SMD_SS_CLOSED) {
604 local_ch->state = new_state;
605 local_ch->fDSR = 0;
606 local_ch->fCTS = 0;
607 local_ch->fCD = 0;
608 local_ch->fSTATE = 1;
609 }
610 }
611 }
612}
613
614
615void smd_channel_reset(uint32_t restart_pid)
616{
617 struct smd_alloc_elm *shared;
618 unsigned n;
619 uint32_t *smem_lock;
620 unsigned long flags;
621
622 SMD_DBG("%s: starting reset\n", __func__);
623 shared = smem_find(ID_CH_ALLOC_TBL, sizeof(*shared) * 64);
624 if (!shared) {
625 pr_err("%s: allocation table not initialized\n", __func__);
626 return;
627 }
628
629 smem_lock = smem_alloc(SMEM_SPINLOCK_ARRAY, 8 * sizeof(uint32_t));
630 if (smem_lock) {
631 SMD_DBG("%s: releasing locks\n", __func__);
632 for (n = 0; n < 8; n++) {
633 uint32_t pid = readl_relaxed(smem_lock);
634 if (pid == (restart_pid + 1))
635 writel_relaxed(0, smem_lock);
636 smem_lock++;
637 }
638 }
639
640 /* reset SMSM entry */
641 if (smsm_info.state) {
642 writel_relaxed(0, SMSM_STATE_ADDR(restart_pid));
643
644 /* clear apps SMSM to restart SMSM init handshake */
645 if (restart_pid == SMSM_MODEM)
646 writel_relaxed(0, SMSM_STATE_ADDR(SMSM_APPS));
647
648 /* notify SMSM processors */
649 smsm_irq_handler(0, 0);
650 MSM_TRIG_A2M_SMSM_INT;
651 MSM_TRIG_A2Q6_SMSM_INT;
Jeff Hugo6a8057c2011-08-16 13:47:12 -0600652 MSM_TRIG_A2DSPS_SMSM_INT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700653 }
654
655 /* change all remote states to CLOSING */
656 mutex_lock(&smd_probe_lock);
657 spin_lock_irqsave(&smd_lock, flags);
658 smd_channel_reset_state(shared, SMD_SS_CLOSING, restart_pid);
659 spin_unlock_irqrestore(&smd_lock, flags);
660 mutex_unlock(&smd_probe_lock);
661
662 /* notify SMD processors */
663 mb();
664 smd_fake_irq_handler(0);
665 notify_modem_smd();
666 notify_dsp_smd();
667 notify_dsps_smd();
668 notify_wcnss_smd();
669
670 /* change all remote states to CLOSED */
671 mutex_lock(&smd_probe_lock);
672 spin_lock_irqsave(&smd_lock, flags);
673 smd_channel_reset_state(shared, SMD_SS_CLOSED, restart_pid);
674 spin_unlock_irqrestore(&smd_lock, flags);
675 mutex_unlock(&smd_probe_lock);
676
677 /* notify SMD processors */
678 mb();
679 smd_fake_irq_handler(0);
680 notify_modem_smd();
681 notify_dsp_smd();
682 notify_dsps_smd();
683 notify_wcnss_smd();
684
685 SMD_DBG("%s: finished reset\n", __func__);
686}
687
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700688/* how many bytes are available for reading */
689static int smd_stream_read_avail(struct smd_channel *ch)
690{
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700691 return (ch->recv->head - ch->recv->tail) & ch->fifo_mask;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700692}
693
694/* how many bytes we are free to write */
695static int smd_stream_write_avail(struct smd_channel *ch)
696{
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700697 return ch->fifo_mask -
698 ((ch->send->head - ch->send->tail) & ch->fifo_mask);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700699}
700
701static int smd_packet_read_avail(struct smd_channel *ch)
702{
703 if (ch->current_packet) {
704 int n = smd_stream_read_avail(ch);
705 if (n > ch->current_packet)
706 n = ch->current_packet;
707 return n;
708 } else {
709 return 0;
710 }
711}
712
713static int smd_packet_write_avail(struct smd_channel *ch)
714{
715 int n = smd_stream_write_avail(ch);
716 return n > SMD_HEADER_SIZE ? n - SMD_HEADER_SIZE : 0;
717}
718
719static int ch_is_open(struct smd_channel *ch)
720{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700721 return (ch->recv->state == SMD_SS_OPENED ||
722 ch->recv->state == SMD_SS_FLUSHING)
723 && (ch->send->state == SMD_SS_OPENED);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700724}
725
726/* provide a pointer and length to readable data in the fifo */
727static unsigned ch_read_buffer(struct smd_channel *ch, void **ptr)
728{
729 unsigned head = ch->recv->head;
730 unsigned tail = ch->recv->tail;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700731 *ptr = (void *) (ch->recv_data + tail);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700732
733 if (tail <= head)
734 return head - tail;
735 else
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700736 return ch->fifo_size - tail;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700737}
738
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700739static int read_intr_blocked(struct smd_channel *ch)
740{
741 return ch->recv->fBLOCKREADINTR;
742}
743
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700744/* advance the fifo read pointer after data from ch_read_buffer is consumed */
745static void ch_read_done(struct smd_channel *ch, unsigned count)
746{
747 BUG_ON(count > smd_stream_read_avail(ch));
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700748 ch->recv->tail = (ch->recv->tail + count) & ch->fifo_mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700749 wmb();
Haley Teng7632fba2009-10-12 10:38:10 -0700750 ch->send->fTAIL = 1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700751}
752
753/* basic read interface to ch_read_{buffer,done} used
Brian Swetland03e00cd2009-07-01 17:58:37 -0700754 * by smd_*_read() and update_packet_state()
755 * will read-and-discard if the _data pointer is null
756 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700757static int ch_read(struct smd_channel *ch, void *_data, int len, int user_buf)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700758{
759 void *ptr;
760 unsigned n;
761 unsigned char *data = _data;
762 int orig_len = len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700763 int r = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700764
765 while (len > 0) {
766 n = ch_read_buffer(ch, &ptr);
767 if (n == 0)
768 break;
769
770 if (n > len)
771 n = len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700772 if (_data) {
773 if (user_buf) {
774 r = copy_to_user(data, ptr, n);
775 if (r > 0) {
776 pr_err("%s: "
777 "copy_to_user could not copy "
778 "%i bytes.\n",
779 __func__,
780 r);
781 }
782 } else
783 memcpy(data, ptr, n);
784 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700785
786 data += n;
787 len -= n;
788 ch_read_done(ch, n);
789 }
790
791 return orig_len - len;
792}
793
794static void update_stream_state(struct smd_channel *ch)
795{
796 /* streams have no special state requiring updating */
797}
798
799static void update_packet_state(struct smd_channel *ch)
800{
801 unsigned hdr[5];
802 int r;
803
804 /* can't do anything if we're in the middle of a packet */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700805 while (ch->current_packet == 0) {
806 /* discard 0 length packets if any */
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700807
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700808 /* don't bother unless we can get the full header */
809 if (smd_stream_read_avail(ch) < SMD_HEADER_SIZE)
810 return;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700811
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700812 r = ch_read(ch, hdr, SMD_HEADER_SIZE, 0);
813 BUG_ON(r != SMD_HEADER_SIZE);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700814
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700815 ch->current_packet = hdr[0];
816 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700817}
818
819/* provide a pointer and length to next free space in the fifo */
820static unsigned ch_write_buffer(struct smd_channel *ch, void **ptr)
821{
822 unsigned head = ch->send->head;
823 unsigned tail = ch->send->tail;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700824 *ptr = (void *) (ch->send_data + head);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700825
826 if (head < tail) {
827 return tail - head - 1;
828 } else {
829 if (tail == 0)
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700830 return ch->fifo_size - head - 1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700831 else
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700832 return ch->fifo_size - head;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700833 }
834}
835
836/* advace the fifo write pointer after freespace
837 * from ch_write_buffer is filled
838 */
839static void ch_write_done(struct smd_channel *ch, unsigned count)
840{
841 BUG_ON(count > smd_stream_write_avail(ch));
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700842 ch->send->head = (ch->send->head + count) & ch->fifo_mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700843 wmb();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700844 ch->send->fHEAD = 1;
845}
846
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700847static void ch_set_state(struct smd_channel *ch, unsigned n)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700848{
849 if (n == SMD_SS_OPENED) {
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700850 ch->send->fDSR = 1;
851 ch->send->fCTS = 1;
852 ch->send->fCD = 1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700853 } else {
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700854 ch->send->fDSR = 0;
855 ch->send->fCTS = 0;
856 ch->send->fCD = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700857 }
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700858 ch->send->state = n;
859 ch->send->fSTATE = 1;
860 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700861}
862
863static void do_smd_probe(void)
864{
865 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
866 if (shared->heap_info.free_offset != last_heap_free) {
867 last_heap_free = shared->heap_info.free_offset;
868 schedule_work(&probe_work);
869 }
870}
871
872static void smd_state_change(struct smd_channel *ch,
873 unsigned last, unsigned next)
874{
875 ch->last_state = next;
876
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700877 SMD_INFO("SMD: ch %d %d -> %d\n", ch->n, last, next);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700878
879 switch (next) {
880 case SMD_SS_OPENING:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700881 if (ch->send->state == SMD_SS_CLOSING ||
882 ch->send->state == SMD_SS_CLOSED) {
883 ch->recv->tail = 0;
884 ch->send->head = 0;
885 ch->send->fBLOCKREADINTR = 0;
886 ch_set_state(ch, SMD_SS_OPENING);
887 }
888 break;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700889 case SMD_SS_OPENED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700890 if (ch->send->state == SMD_SS_OPENING) {
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700891 ch_set_state(ch, SMD_SS_OPENED);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700892 ch->notify(ch->priv, SMD_EVENT_OPEN);
893 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700894 break;
895 case SMD_SS_FLUSHING:
896 case SMD_SS_RESET:
897 /* we should force them to close? */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700898 break;
899 case SMD_SS_CLOSED:
900 if (ch->send->state == SMD_SS_OPENED) {
901 ch_set_state(ch, SMD_SS_CLOSING);
902 ch->current_packet = 0;
903 ch->notify(ch->priv, SMD_EVENT_CLOSE);
904 }
905 break;
906 case SMD_SS_CLOSING:
907 if (ch->send->state == SMD_SS_CLOSED) {
908 list_move(&ch->ch_list,
909 &smd_ch_to_close_list);
910 queue_work(channel_close_wq,
911 &finalize_channel_close_work);
912 }
913 break;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700914 }
915}
916
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700917static void handle_smd_irq_closing_list(void)
918{
919 unsigned long flags;
920 struct smd_channel *ch;
921 struct smd_channel *index;
922 unsigned tmp;
923
924 spin_lock_irqsave(&smd_lock, flags);
925 list_for_each_entry_safe(ch, index, &smd_ch_closing_list, ch_list) {
926 if (ch->recv->fSTATE)
927 ch->recv->fSTATE = 0;
928 tmp = ch->recv->state;
929 if (tmp != ch->last_state)
930 smd_state_change(ch, ch->last_state, tmp);
931 }
932 spin_unlock_irqrestore(&smd_lock, flags);
933}
934
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700935static void handle_smd_irq(struct list_head *list, void (*notify)(void))
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700936{
937 unsigned long flags;
938 struct smd_channel *ch;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700939 unsigned ch_flags;
940 unsigned tmp;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700941 unsigned char state_change;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700942
943 spin_lock_irqsave(&smd_lock, flags);
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700944 list_for_each_entry(ch, list, ch_list) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700945 state_change = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700946 ch_flags = 0;
947 if (ch_is_open(ch)) {
948 if (ch->recv->fHEAD) {
949 ch->recv->fHEAD = 0;
950 ch_flags |= 1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700951 }
952 if (ch->recv->fTAIL) {
953 ch->recv->fTAIL = 0;
954 ch_flags |= 2;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700955 }
956 if (ch->recv->fSTATE) {
957 ch->recv->fSTATE = 0;
958 ch_flags |= 4;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700959 }
960 }
961 tmp = ch->recv->state;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700962 if (tmp != ch->last_state) {
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700963 smd_state_change(ch, ch->last_state, tmp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700964 state_change = 1;
965 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700966 if (ch_flags) {
967 ch->update_state(ch);
968 ch->notify(ch->priv, SMD_EVENT_DATA);
969 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700970 if (ch_flags & 0x4 && !state_change)
971 ch->notify(ch->priv, SMD_EVENT_STATUS);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700972 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700973 spin_unlock_irqrestore(&smd_lock, flags);
974 do_smd_probe();
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700975}
976
Brian Swetland37521a32009-07-01 18:30:47 -0700977static irqreturn_t smd_modem_irq_handler(int irq, void *data)
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700978{
Brian Swetland37521a32009-07-01 18:30:47 -0700979 handle_smd_irq(&smd_ch_list_modem, notify_modem_smd);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700980 handle_smd_irq_closing_list();
Brian Swetland37521a32009-07-01 18:30:47 -0700981 return IRQ_HANDLED;
982}
983
Daniel Walkerb13525c2010-03-18 10:10:30 -0700984#if defined(CONFIG_QDSP6)
Brian Swetland37521a32009-07-01 18:30:47 -0700985static irqreturn_t smd_dsp_irq_handler(int irq, void *data)
986{
987 handle_smd_irq(&smd_ch_list_dsp, notify_dsp_smd);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700988 handle_smd_irq_closing_list();
989 return IRQ_HANDLED;
990}
991#endif
992
993#if defined(CONFIG_DSPS)
994static irqreturn_t smd_dsps_irq_handler(int irq, void *data)
995{
996 handle_smd_irq(&smd_ch_list_dsps, notify_dsps_smd);
997 handle_smd_irq_closing_list();
998 return IRQ_HANDLED;
999}
1000#endif
1001
1002#if defined(CONFIG_WCNSS)
1003static irqreturn_t smd_wcnss_irq_handler(int irq, void *data)
1004{
1005 handle_smd_irq(&smd_ch_list_wcnss, notify_wcnss_smd);
1006 handle_smd_irq_closing_list();
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001007 return IRQ_HANDLED;
1008}
Daniel Walkerb13525c2010-03-18 10:10:30 -07001009#endif
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001010
1011static void smd_fake_irq_handler(unsigned long arg)
1012{
Brian Swetland37521a32009-07-01 18:30:47 -07001013 handle_smd_irq(&smd_ch_list_modem, notify_modem_smd);
1014 handle_smd_irq(&smd_ch_list_dsp, notify_dsp_smd);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001015 handle_smd_irq(&smd_ch_list_dsps, notify_dsps_smd);
1016 handle_smd_irq(&smd_ch_list_wcnss, notify_wcnss_smd);
1017 handle_smd_irq_closing_list();
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001018}
1019
1020static DECLARE_TASKLET(smd_fake_irq_tasklet, smd_fake_irq_handler, 0);
1021
Brian Swetland37521a32009-07-01 18:30:47 -07001022static inline int smd_need_int(struct smd_channel *ch)
1023{
1024 if (ch_is_open(ch)) {
1025 if (ch->recv->fHEAD || ch->recv->fTAIL || ch->recv->fSTATE)
1026 return 1;
1027 if (ch->recv->state != ch->last_state)
1028 return 1;
1029 }
1030 return 0;
1031}
1032
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001033void smd_sleep_exit(void)
1034{
1035 unsigned long flags;
1036 struct smd_channel *ch;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001037 int need_int = 0;
1038
1039 spin_lock_irqsave(&smd_lock, flags);
Brian Swetland37521a32009-07-01 18:30:47 -07001040 list_for_each_entry(ch, &smd_ch_list_modem, ch_list) {
1041 if (smd_need_int(ch)) {
1042 need_int = 1;
1043 break;
1044 }
1045 }
1046 list_for_each_entry(ch, &smd_ch_list_dsp, ch_list) {
1047 if (smd_need_int(ch)) {
1048 need_int = 1;
1049 break;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001050 }
1051 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001052 list_for_each_entry(ch, &smd_ch_list_dsps, ch_list) {
1053 if (smd_need_int(ch)) {
1054 need_int = 1;
1055 break;
1056 }
1057 }
1058 list_for_each_entry(ch, &smd_ch_list_wcnss, ch_list) {
1059 if (smd_need_int(ch)) {
1060 need_int = 1;
1061 break;
1062 }
1063 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001064 spin_unlock_irqrestore(&smd_lock, flags);
1065 do_smd_probe();
Brian Swetland37521a32009-07-01 18:30:47 -07001066
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001067 if (need_int) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001068 SMD_DBG("smd_sleep_exit need interrupt\n");
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001069 tasklet_schedule(&smd_fake_irq_tasklet);
1070 }
1071}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001072EXPORT_SYMBOL(smd_sleep_exit);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001073
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001074static int smd_is_packet(struct smd_alloc_elm *alloc_elm)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001075{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001076 if (SMD_XFER_TYPE(alloc_elm->type) == 1)
1077 return 0;
1078 else if (SMD_XFER_TYPE(alloc_elm->type) == 2)
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001079 return 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001080
1081 /* for cases where xfer type is 0 */
1082 if (!strncmp(alloc_elm->name, "DAL", 3))
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001083 return 0;
1084
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001085 /* for cases where xfer type is 0 */
1086 if (!strncmp(alloc_elm->name, "RPCCALL_QDSP", 12))
1087 return 0;
1088
1089 if (alloc_elm->cid > 4 || alloc_elm->cid == 1)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001090 return 1;
1091 else
1092 return 0;
1093}
1094
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001095static int smd_stream_write(smd_channel_t *ch, const void *_data, int len,
1096 int user_buf)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001097{
1098 void *ptr;
1099 const unsigned char *buf = _data;
1100 unsigned xfer;
1101 int orig_len = len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001102 int r = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001103
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001104 SMD_DBG("smd_stream_write() %d -> ch%d\n", len, ch->n);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001105 if (len < 0)
1106 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001107 else if (len == 0)
1108 return 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001109
1110 while ((xfer = ch_write_buffer(ch, &ptr)) != 0) {
1111 if (!ch_is_open(ch))
1112 break;
1113 if (xfer > len)
1114 xfer = len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001115 if (user_buf) {
1116 r = copy_from_user(ptr, buf, xfer);
1117 if (r > 0) {
1118 pr_err("%s: "
1119 "copy_from_user could not copy %i "
1120 "bytes.\n",
1121 __func__,
1122 r);
1123 }
1124 } else
1125 memcpy(ptr, buf, xfer);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001126 ch_write_done(ch, xfer);
1127 len -= xfer;
1128 buf += xfer;
1129 if (len == 0)
1130 break;
1131 }
1132
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001133 if (orig_len - len)
1134 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001135
1136 return orig_len - len;
1137}
1138
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001139static int smd_packet_write(smd_channel_t *ch, const void *_data, int len,
1140 int user_buf)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001141{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001142 int ret;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001143 unsigned hdr[5];
1144
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001145 SMD_DBG("smd_packet_write() %d -> ch%d\n", len, ch->n);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001146 if (len < 0)
1147 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001148 else if (len == 0)
1149 return 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001150
1151 if (smd_stream_write_avail(ch) < (len + SMD_HEADER_SIZE))
1152 return -ENOMEM;
1153
1154 hdr[0] = len;
1155 hdr[1] = hdr[2] = hdr[3] = hdr[4] = 0;
1156
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001157
1158 ret = smd_stream_write(ch, hdr, sizeof(hdr), 0);
1159 if (ret < 0 || ret != sizeof(hdr)) {
1160 SMD_DBG("%s failed to write pkt header: "
1161 "%d returned\n", __func__, ret);
1162 return -1;
1163 }
1164
1165
1166 ret = smd_stream_write(ch, _data, len, user_buf);
1167 if (ret < 0 || ret != len) {
1168 SMD_DBG("%s failed to write pkt data: "
1169 "%d returned\n", __func__, ret);
1170 return ret;
1171 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001172
1173 return len;
1174}
1175
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001176static int smd_stream_read(smd_channel_t *ch, void *data, int len, int user_buf)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001177{
1178 int r;
1179
1180 if (len < 0)
1181 return -EINVAL;
1182
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001183 r = ch_read(ch, data, len, user_buf);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001184 if (r > 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001185 if (!read_intr_blocked(ch))
1186 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001187
1188 return r;
1189}
1190
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001191static int smd_packet_read(smd_channel_t *ch, void *data, int len, int user_buf)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001192{
1193 unsigned long flags;
1194 int r;
1195
1196 if (len < 0)
1197 return -EINVAL;
1198
1199 if (len > ch->current_packet)
1200 len = ch->current_packet;
1201
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001202 r = ch_read(ch, data, len, user_buf);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001203 if (r > 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001204 if (!read_intr_blocked(ch))
1205 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001206
1207 spin_lock_irqsave(&smd_lock, flags);
1208 ch->current_packet -= r;
1209 update_packet_state(ch);
1210 spin_unlock_irqrestore(&smd_lock, flags);
1211
1212 return r;
1213}
1214
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001215static int smd_packet_read_from_cb(smd_channel_t *ch, void *data, int len,
1216 int user_buf)
1217{
1218 int r;
1219
1220 if (len < 0)
1221 return -EINVAL;
1222
1223 if (len > ch->current_packet)
1224 len = ch->current_packet;
1225
1226 r = ch_read(ch, data, len, user_buf);
1227 if (r > 0)
1228 if (!read_intr_blocked(ch))
1229 ch->notify_other_cpu();
1230
1231 ch->current_packet -= r;
1232 update_packet_state(ch);
1233
1234 return r;
1235}
1236
1237static int smd_alloc_v2(struct smd_channel *ch)
1238{
1239 struct smd_shared_v2 *shared2;
1240 void *buffer;
1241 unsigned buffer_sz;
1242
1243 shared2 = smem_alloc(SMEM_SMD_BASE_ID + ch->n, sizeof(*shared2));
1244 if (!shared2) {
1245 SMD_INFO("smem_alloc failed ch=%d\n", ch->n);
1246 return -1;
1247 }
1248 buffer = smem_get_entry(SMEM_SMD_FIFO_BASE_ID + ch->n, &buffer_sz);
1249 if (!buffer) {
1250 SMD_INFO("smem_get_entry failed \n");
1251 return -1;
1252 }
1253
1254 /* buffer must be a power-of-two size */
1255 if (buffer_sz & (buffer_sz - 1))
1256 return -1;
1257
1258 buffer_sz /= 2;
1259 ch->send = &shared2->ch0;
1260 ch->recv = &shared2->ch1;
1261 ch->send_data = buffer;
1262 ch->recv_data = buffer + buffer_sz;
1263 ch->fifo_size = buffer_sz;
1264 return 0;
1265}
1266
1267static int smd_alloc_v1(struct smd_channel *ch)
1268{
1269 struct smd_shared_v1 *shared1;
1270 shared1 = smem_alloc(ID_SMD_CHANNELS + ch->n, sizeof(*shared1));
1271 if (!shared1) {
1272 pr_err("smd_alloc_channel() cid %d does not exist\n", ch->n);
1273 return -1;
1274 }
1275 ch->send = &shared1->ch0;
1276 ch->recv = &shared1->ch1;
1277 ch->send_data = shared1->data0;
1278 ch->recv_data = shared1->data1;
1279 ch->fifo_size = SMD_BUF_SIZE;
1280 return 0;
1281}
1282
1283static int smd_alloc_channel(struct smd_alloc_elm *alloc_elm)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001284{
1285 struct smd_channel *ch;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001286
1287 ch = kzalloc(sizeof(struct smd_channel), GFP_KERNEL);
1288 if (ch == 0) {
1289 pr_err("smd_alloc_channel() out of memory\n");
Brian Swetland34f719b2009-10-30 16:22:05 -07001290 return -1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001291 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001292 ch->n = alloc_elm->cid;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001293
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001294 if (smd_alloc_v2(ch) && smd_alloc_v1(ch)) {
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001295 kfree(ch);
Brian Swetland34f719b2009-10-30 16:22:05 -07001296 return -1;
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001297 }
1298
1299 ch->fifo_mask = ch->fifo_size - 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001300 ch->type = SMD_CHANNEL_TYPE(alloc_elm->type);
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001301
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001302 if (ch->type == SMD_APPS_MODEM)
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001303 ch->notify_other_cpu = notify_modem_smd;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001304 else if (ch->type == SMD_APPS_QDSP)
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001305 ch->notify_other_cpu = notify_dsp_smd;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001306 else if (ch->type == SMD_APPS_DSPS)
1307 ch->notify_other_cpu = notify_dsps_smd;
1308 else
1309 ch->notify_other_cpu = notify_wcnss_smd;
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001310
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001311 if (smd_is_packet(alloc_elm)) {
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001312 ch->read = smd_packet_read;
1313 ch->write = smd_packet_write;
1314 ch->read_avail = smd_packet_read_avail;
1315 ch->write_avail = smd_packet_write_avail;
1316 ch->update_state = update_packet_state;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001317 ch->read_from_cb = smd_packet_read_from_cb;
1318 ch->is_pkt_ch = 1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001319 } else {
1320 ch->read = smd_stream_read;
1321 ch->write = smd_stream_write;
1322 ch->read_avail = smd_stream_read_avail;
1323 ch->write_avail = smd_stream_write_avail;
1324 ch->update_state = update_stream_state;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001325 ch->read_from_cb = smd_stream_read;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001326 }
1327
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001328 memcpy(ch->name, alloc_elm->name, SMD_MAX_CH_NAME_LEN);
1329 ch->name[SMD_MAX_CH_NAME_LEN-1] = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001330
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001331 ch->pdev.name = ch->name;
1332 ch->pdev.id = ch->type;
1333
1334 SMD_INFO("smd_alloc_channel() '%s' cid=%d\n",
1335 ch->name, ch->n);
1336
1337 mutex_lock(&smd_creation_mutex);
1338 list_add(&ch->ch_list, &smd_ch_closed_list);
1339 mutex_unlock(&smd_creation_mutex);
1340
1341 platform_device_register(&ch->pdev);
1342 if (!strncmp(ch->name, "LOOPBACK", 8) && ch->type == SMD_APPS_MODEM) {
1343 /* create a platform driver to be used by smd_tty driver
1344 * so that it can access the loopback port
1345 */
1346 loopback_tty_pdev.id = ch->type;
1347 platform_device_register(&loopback_tty_pdev);
1348 }
1349 return 0;
1350}
1351
1352static inline void notify_loopback_smd(void)
1353{
1354 unsigned long flags;
1355 struct smd_channel *ch;
1356
1357 spin_lock_irqsave(&smd_lock, flags);
1358 list_for_each_entry(ch, &smd_ch_list_loopback, ch_list) {
1359 ch->notify(ch->priv, SMD_EVENT_DATA);
1360 }
1361 spin_unlock_irqrestore(&smd_lock, flags);
1362}
1363
1364static int smd_alloc_loopback_channel(void)
1365{
1366 static struct smd_half_channel smd_loopback_ctl;
1367 static char smd_loopback_data[SMD_BUF_SIZE];
1368 struct smd_channel *ch;
1369
1370 ch = kzalloc(sizeof(struct smd_channel), GFP_KERNEL);
1371 if (ch == 0) {
1372 pr_err("%s: out of memory\n", __func__);
1373 return -1;
1374 }
1375 ch->n = SMD_LOOPBACK_CID;
1376
1377 ch->send = &smd_loopback_ctl;
1378 ch->recv = &smd_loopback_ctl;
1379 ch->send_data = smd_loopback_data;
1380 ch->recv_data = smd_loopback_data;
1381 ch->fifo_size = SMD_BUF_SIZE;
1382
1383 ch->fifo_mask = ch->fifo_size - 1;
1384 ch->type = SMD_LOOPBACK_TYPE;
1385 ch->notify_other_cpu = notify_loopback_smd;
1386
1387 ch->read = smd_stream_read;
1388 ch->write = smd_stream_write;
1389 ch->read_avail = smd_stream_read_avail;
1390 ch->write_avail = smd_stream_write_avail;
1391 ch->update_state = update_stream_state;
1392 ch->read_from_cb = smd_stream_read;
1393
1394 memset(ch->name, 0, 20);
1395 memcpy(ch->name, "local_loopback", 14);
1396
1397 ch->pdev.name = ch->name;
1398 ch->pdev.id = ch->type;
1399
1400 SMD_INFO("%s: '%s' cid=%d\n", __func__, ch->name, ch->n);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001401
1402 mutex_lock(&smd_creation_mutex);
1403 list_add(&ch->ch_list, &smd_ch_closed_list);
1404 mutex_unlock(&smd_creation_mutex);
1405
1406 platform_device_register(&ch->pdev);
Brian Swetland34f719b2009-10-30 16:22:05 -07001407 return 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001408}
1409
1410static void do_nothing_notify(void *priv, unsigned flags)
1411{
1412}
1413
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001414static void finalize_channel_close_fn(struct work_struct *work)
1415{
1416 unsigned long flags;
1417 struct smd_channel *ch;
1418 struct smd_channel *index;
1419
1420 spin_lock_irqsave(&smd_lock, flags);
1421 list_for_each_entry_safe(ch, index, &smd_ch_to_close_list, ch_list) {
1422 list_del(&ch->ch_list);
1423 spin_unlock_irqrestore(&smd_lock, flags);
1424 mutex_lock(&smd_creation_mutex);
1425 list_add(&ch->ch_list, &smd_ch_closed_list);
1426 mutex_unlock(&smd_creation_mutex);
1427 ch->notify(ch->priv, SMD_EVENT_REOPEN_READY);
1428 ch->notify = do_nothing_notify;
1429 spin_lock_irqsave(&smd_lock, flags);
1430 }
1431 spin_unlock_irqrestore(&smd_lock, flags);
1432}
1433
1434struct smd_channel *smd_get_channel(const char *name, uint32_t type)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001435{
1436 struct smd_channel *ch;
1437
1438 mutex_lock(&smd_creation_mutex);
1439 list_for_each_entry(ch, &smd_ch_closed_list, ch_list) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001440 if (!strcmp(name, ch->name) &&
1441 (type == ch->type)) {
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001442 list_del(&ch->ch_list);
1443 mutex_unlock(&smd_creation_mutex);
1444 return ch;
1445 }
1446 }
1447 mutex_unlock(&smd_creation_mutex);
1448
1449 return NULL;
1450}
1451
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001452int smd_named_open_on_edge(const char *name, uint32_t edge,
1453 smd_channel_t **_ch,
1454 void *priv, void (*notify)(void *, unsigned))
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001455{
1456 struct smd_channel *ch;
1457 unsigned long flags;
1458
1459 if (smd_initialized == 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001460 SMD_INFO("smd_open() before smd_init()\n");
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001461 return -ENODEV;
1462 }
1463
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001464 SMD_DBG("smd_open('%s', %p, %p)\n", name, priv, notify);
1465
1466 ch = smd_get_channel(name, edge);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001467 if (!ch)
1468 return -ENODEV;
1469
1470 if (notify == 0)
1471 notify = do_nothing_notify;
1472
1473 ch->notify = notify;
1474 ch->current_packet = 0;
1475 ch->last_state = SMD_SS_CLOSED;
1476 ch->priv = priv;
1477
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001478 if (edge == SMD_LOOPBACK_TYPE) {
1479 ch->last_state = SMD_SS_OPENED;
1480 ch->send->state = SMD_SS_OPENED;
1481 ch->send->fDSR = 1;
1482 ch->send->fCTS = 1;
1483 ch->send->fCD = 1;
1484 }
1485
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001486 *_ch = ch;
1487
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001488 SMD_DBG("smd_open: opening '%s'\n", ch->name);
1489
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001490 spin_lock_irqsave(&smd_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001491 if (SMD_CHANNEL_TYPE(ch->type) == SMD_APPS_MODEM)
Brian Swetland37521a32009-07-01 18:30:47 -07001492 list_add(&ch->ch_list, &smd_ch_list_modem);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001493 else if (SMD_CHANNEL_TYPE(ch->type) == SMD_APPS_QDSP)
Brian Swetland37521a32009-07-01 18:30:47 -07001494 list_add(&ch->ch_list, &smd_ch_list_dsp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001495 else if (SMD_CHANNEL_TYPE(ch->type) == SMD_APPS_DSPS)
1496 list_add(&ch->ch_list, &smd_ch_list_dsps);
1497 else if (SMD_CHANNEL_TYPE(ch->type) == SMD_APPS_WCNSS)
1498 list_add(&ch->ch_list, &smd_ch_list_wcnss);
1499 else
1500 list_add(&ch->ch_list, &smd_ch_list_loopback);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001501
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001502 SMD_DBG("%s: opening ch %d\n", __func__, ch->n);
1503
1504 if (edge != SMD_LOOPBACK_TYPE)
1505 smd_state_change(ch, ch->last_state, SMD_SS_OPENING);
1506
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001507 spin_unlock_irqrestore(&smd_lock, flags);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001508
1509 return 0;
1510}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001511EXPORT_SYMBOL(smd_named_open_on_edge);
1512
1513
1514int smd_open(const char *name, smd_channel_t **_ch,
1515 void *priv, void (*notify)(void *, unsigned))
1516{
1517 return smd_named_open_on_edge(name, SMD_APPS_MODEM, _ch, priv,
1518 notify);
1519}
1520EXPORT_SYMBOL(smd_open);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001521
1522int smd_close(smd_channel_t *ch)
1523{
1524 unsigned long flags;
1525
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001526 if (ch == 0)
1527 return -1;
1528
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001529 SMD_INFO("smd_close(%s)\n", ch->name);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001530
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001531 spin_lock_irqsave(&smd_lock, flags);
1532 list_del(&ch->ch_list);
1533 if (ch->n == SMD_LOOPBACK_CID) {
1534 ch->send->fDSR = 0;
1535 ch->send->fCTS = 0;
1536 ch->send->fCD = 0;
1537 ch->send->state = SMD_SS_CLOSED;
1538 } else
1539 ch_set_state(ch, SMD_SS_CLOSED);
1540
1541 if (ch->recv->state == SMD_SS_OPENED) {
1542 list_add(&ch->ch_list, &smd_ch_closing_list);
1543 spin_unlock_irqrestore(&smd_lock, flags);
1544 } else {
1545 spin_unlock_irqrestore(&smd_lock, flags);
1546 ch->notify = do_nothing_notify;
1547 mutex_lock(&smd_creation_mutex);
1548 list_add(&ch->ch_list, &smd_ch_closed_list);
1549 mutex_unlock(&smd_creation_mutex);
1550 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001551
1552 return 0;
1553}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001554EXPORT_SYMBOL(smd_close);
1555
1556int smd_write_start(smd_channel_t *ch, int len)
1557{
1558 int ret;
1559 unsigned hdr[5];
1560
1561 if (!ch) {
1562 pr_err("%s: Invalid channel specified\n", __func__);
1563 return -ENODEV;
1564 }
1565 if (!ch->is_pkt_ch) {
1566 pr_err("%s: non-packet channel specified\n", __func__);
1567 return -EACCES;
1568 }
1569 if (len < 1) {
1570 pr_err("%s: invalid length: %d\n", __func__, len);
1571 return -EINVAL;
1572 }
1573
1574 if (ch->pending_pkt_sz) {
1575 pr_err("%s: packet of size: %d in progress\n", __func__,
1576 ch->pending_pkt_sz);
1577 return -EBUSY;
1578 }
1579 ch->pending_pkt_sz = len;
1580
1581 if (smd_stream_write_avail(ch) < (SMD_HEADER_SIZE)) {
1582 ch->pending_pkt_sz = 0;
1583 SMD_DBG("%s: no space to write packet header\n", __func__);
1584 return -EAGAIN;
1585 }
1586
1587 hdr[0] = len;
1588 hdr[1] = hdr[2] = hdr[3] = hdr[4] = 0;
1589
1590
1591 ret = smd_stream_write(ch, hdr, sizeof(hdr), 0);
1592 if (ret < 0 || ret != sizeof(hdr)) {
1593 ch->pending_pkt_sz = 0;
1594 pr_err("%s: packet header failed to write\n", __func__);
1595 return -EPERM;
1596 }
1597 return 0;
1598}
1599EXPORT_SYMBOL(smd_write_start);
1600
1601int smd_write_segment(smd_channel_t *ch, void *data, int len, int user_buf)
1602{
1603 int bytes_written;
1604
1605 if (!ch) {
1606 pr_err("%s: Invalid channel specified\n", __func__);
1607 return -ENODEV;
1608 }
1609 if (len < 1) {
1610 pr_err("%s: invalid length: %d\n", __func__, len);
1611 return -EINVAL;
1612 }
1613
1614 if (!ch->pending_pkt_sz) {
1615 pr_err("%s: no transaction in progress\n", __func__);
1616 return -ENOEXEC;
1617 }
1618 if (ch->pending_pkt_sz - len < 0) {
1619 pr_err("%s: segment of size: %d will make packet go over "
1620 "length\n", __func__, len);
1621 return -EINVAL;
1622 }
1623
1624 bytes_written = smd_stream_write(ch, data, len, user_buf);
1625
1626 ch->pending_pkt_sz -= bytes_written;
1627
1628 return bytes_written;
1629}
1630EXPORT_SYMBOL(smd_write_segment);
1631
1632int smd_write_end(smd_channel_t *ch)
1633{
1634
1635 if (!ch) {
1636 pr_err("%s: Invalid channel specified\n", __func__);
1637 return -ENODEV;
1638 }
1639 if (ch->pending_pkt_sz) {
1640 pr_err("%s: current packet not completely written\n", __func__);
1641 return -E2BIG;
1642 }
1643
1644 return 0;
1645}
1646EXPORT_SYMBOL(smd_write_end);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001647
1648int smd_read(smd_channel_t *ch, void *data, int len)
1649{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001650 return ch->read(ch, data, len, 0);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001651}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001652EXPORT_SYMBOL(smd_read);
1653
1654int smd_read_user_buffer(smd_channel_t *ch, void *data, int len)
1655{
1656 return ch->read(ch, data, len, 1);
1657}
1658EXPORT_SYMBOL(smd_read_user_buffer);
1659
1660int smd_read_from_cb(smd_channel_t *ch, void *data, int len)
1661{
1662 return ch->read_from_cb(ch, data, len, 0);
1663}
1664EXPORT_SYMBOL(smd_read_from_cb);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001665
1666int smd_write(smd_channel_t *ch, const void *data, int len)
1667{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001668 return ch->pending_pkt_sz ? -EBUSY : ch->write(ch, data, len, 0);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001669}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001670EXPORT_SYMBOL(smd_write);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001671
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001672int smd_write_user_buffer(smd_channel_t *ch, const void *data, int len)
Brian Swetland636eb9c2009-12-07 15:28:08 -08001673{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001674 return ch->pending_pkt_sz ? -EBUSY : ch->write(ch, data, len, 1);
Brian Swetland636eb9c2009-12-07 15:28:08 -08001675}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001676EXPORT_SYMBOL(smd_write_user_buffer);
Brian Swetland636eb9c2009-12-07 15:28:08 -08001677
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001678int smd_read_avail(smd_channel_t *ch)
1679{
1680 return ch->read_avail(ch);
1681}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001682EXPORT_SYMBOL(smd_read_avail);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001683
1684int smd_write_avail(smd_channel_t *ch)
1685{
1686 return ch->write_avail(ch);
1687}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001688EXPORT_SYMBOL(smd_write_avail);
1689
1690void smd_enable_read_intr(smd_channel_t *ch)
1691{
1692 if (ch)
1693 ch->send->fBLOCKREADINTR = 0;
1694}
1695EXPORT_SYMBOL(smd_enable_read_intr);
1696
1697void smd_disable_read_intr(smd_channel_t *ch)
1698{
1699 if (ch)
1700 ch->send->fBLOCKREADINTR = 1;
1701}
1702EXPORT_SYMBOL(smd_disable_read_intr);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001703
1704int smd_wait_until_readable(smd_channel_t *ch, int bytes)
1705{
1706 return -1;
1707}
1708
1709int smd_wait_until_writable(smd_channel_t *ch, int bytes)
1710{
1711 return -1;
1712}
1713
1714int smd_cur_packet_size(smd_channel_t *ch)
1715{
1716 return ch->current_packet;
1717}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001718EXPORT_SYMBOL(smd_cur_packet_size);
1719
1720int smd_tiocmget(smd_channel_t *ch)
1721{
1722 return (ch->recv->fDSR ? TIOCM_DSR : 0) |
1723 (ch->recv->fCTS ? TIOCM_CTS : 0) |
1724 (ch->recv->fCD ? TIOCM_CD : 0) |
1725 (ch->recv->fRI ? TIOCM_RI : 0) |
1726 (ch->send->fCTS ? TIOCM_RTS : 0) |
1727 (ch->send->fDSR ? TIOCM_DTR : 0);
1728}
1729EXPORT_SYMBOL(smd_tiocmget);
1730
Vamsi Krishnacb12a102011-08-17 15:18:26 -07001731/* this api will be called while holding smd_lock */
1732int
1733smd_tiocmset_from_cb(smd_channel_t *ch, unsigned int set, unsigned int clear)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001734{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001735 if (set & TIOCM_DTR)
1736 ch->send->fDSR = 1;
1737
1738 if (set & TIOCM_RTS)
1739 ch->send->fCTS = 1;
1740
1741 if (clear & TIOCM_DTR)
1742 ch->send->fDSR = 0;
1743
1744 if (clear & TIOCM_RTS)
1745 ch->send->fCTS = 0;
1746
1747 ch->send->fSTATE = 1;
1748 barrier();
1749 ch->notify_other_cpu();
Vamsi Krishnacb12a102011-08-17 15:18:26 -07001750
1751 return 0;
1752}
1753EXPORT_SYMBOL(smd_tiocmset_from_cb);
1754
1755int smd_tiocmset(smd_channel_t *ch, unsigned int set, unsigned int clear)
1756{
1757 unsigned long flags;
1758
1759 spin_lock_irqsave(&smd_lock, flags);
1760 smd_tiocmset_from_cb(ch, set, clear);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001761 spin_unlock_irqrestore(&smd_lock, flags);
1762
1763 return 0;
1764}
1765EXPORT_SYMBOL(smd_tiocmset);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001766
1767
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001768/* -------------------------------------------------------------------------- */
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001769
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001770/* smem_alloc returns the pointer to smem item if it is already allocated.
1771 * Otherwise, it returns NULL.
1772 */
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001773void *smem_alloc(unsigned id, unsigned size)
1774{
1775 return smem_find(id, size);
1776}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001777EXPORT_SYMBOL(smem_alloc);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001778
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001779#define SMEM_SPINLOCK_SMEM_ALLOC "S:3"
1780static remote_spinlock_t remote_spinlock;
1781
1782/* smem_alloc2 returns the pointer to smem item. If it is not allocated,
1783 * it allocates it and then returns the pointer to it.
1784 */
Angshuman Sarkar4eade0d2011-08-17 14:06:23 +05301785void *smem_alloc2(unsigned id, unsigned size_in)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001786{
1787 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
1788 struct smem_heap_entry *toc = shared->heap_toc;
1789 unsigned long flags;
1790 void *ret = NULL;
1791
1792 if (!shared->heap_info.initialized) {
1793 pr_err("%s: smem heap info not initialized\n", __func__);
1794 return NULL;
1795 }
1796
1797 if (id >= SMEM_NUM_ITEMS)
1798 return NULL;
1799
1800 size_in = ALIGN(size_in, 8);
1801 remote_spin_lock_irqsave(&remote_spinlock, flags);
1802 if (toc[id].allocated) {
1803 SMD_DBG("%s: %u already allocated\n", __func__, id);
1804 if (size_in != toc[id].size)
1805 pr_err("%s: wrong size %u (expected %u)\n",
1806 __func__, toc[id].size, size_in);
1807 else
1808 ret = (void *)(MSM_SHARED_RAM_BASE + toc[id].offset);
1809 } else if (id > SMEM_FIXED_ITEM_LAST) {
1810 SMD_DBG("%s: allocating %u\n", __func__, id);
1811 if (shared->heap_info.heap_remaining >= size_in) {
1812 toc[id].offset = shared->heap_info.free_offset;
1813 toc[id].size = size_in;
1814 wmb();
1815 toc[id].allocated = 1;
1816
1817 shared->heap_info.free_offset += size_in;
1818 shared->heap_info.heap_remaining -= size_in;
1819 ret = (void *)(MSM_SHARED_RAM_BASE + toc[id].offset);
1820 } else
1821 pr_err("%s: not enough memory %u (required %u)\n",
1822 __func__, shared->heap_info.heap_remaining,
1823 size_in);
1824 }
1825 wmb();
1826 remote_spin_unlock_irqrestore(&remote_spinlock, flags);
1827 return ret;
1828}
Angshuman Sarkar4eade0d2011-08-17 14:06:23 +05301829EXPORT_SYMBOL(smem_alloc2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001830
1831void *smem_get_entry(unsigned id, unsigned *size)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001832{
1833 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
1834 struct smem_heap_entry *toc = shared->heap_toc;
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +05301835 int use_spinlocks = spinlocks_initialized;
1836 void *ret = 0;
1837 unsigned long flags = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001838
1839 if (id >= SMEM_NUM_ITEMS)
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +05301840 return ret;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001841
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +05301842 if (use_spinlocks)
1843 remote_spin_lock_irqsave(&remote_spinlock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001844 /* toc is in device memory and cannot be speculatively accessed */
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001845 if (toc[id].allocated) {
1846 *size = toc[id].size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001847 barrier();
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +05301848 ret = (void *) (MSM_SHARED_RAM_BASE + toc[id].offset);
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001849 } else {
1850 *size = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001851 }
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +05301852 if (use_spinlocks)
1853 remote_spin_unlock_irqrestore(&remote_spinlock, flags);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001854
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +05301855 return ret;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001856}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001857EXPORT_SYMBOL(smem_get_entry);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001858
1859void *smem_find(unsigned id, unsigned size_in)
1860{
1861 unsigned size;
1862 void *ptr;
1863
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001864 ptr = smem_get_entry(id, &size);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001865 if (!ptr)
1866 return 0;
1867
1868 size_in = ALIGN(size_in, 8);
1869 if (size_in != size) {
1870 pr_err("smem_find(%d, %d): wrong size %d\n",
1871 id, size_in, size);
1872 return 0;
1873 }
1874
1875 return ptr;
1876}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001877EXPORT_SYMBOL(smem_find);
1878
1879static int smsm_cb_init(void)
1880{
1881 unsigned long flags;
1882 struct smsm_state_info *state_info;
1883 int n;
1884 int ret = 0;
1885
1886 smsm_states = kmalloc(sizeof(struct smsm_state_info)*SMSM_NUM_ENTRIES,
1887 GFP_KERNEL);
1888
1889 if (!smsm_states) {
1890 pr_err("%s: SMSM init failed\n", __func__);
1891 return -ENOMEM;
1892 }
1893
1894 spin_lock_irqsave(&smsm_lock, flags);
1895 for (n = 0; n < SMSM_NUM_ENTRIES; n++) {
1896 state_info = &smsm_states[n];
1897 state_info->last_value = __raw_readl(SMSM_STATE_ADDR(n));
1898 INIT_LIST_HEAD(&state_info->callbacks);
1899 }
1900 spin_unlock_irqrestore(&smsm_lock, flags);
1901
1902 return ret;
1903}
1904
1905static int smsm_init(void)
1906{
1907 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
1908 int i;
1909 struct smsm_size_info_type *smsm_size_info;
1910
1911 i = remote_spin_lock_init(&remote_spinlock, SMEM_SPINLOCK_SMEM_ALLOC);
1912 if (i) {
1913 pr_err("%s: remote spinlock init failed %d\n", __func__, i);
1914 return i;
1915 }
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +05301916 spinlocks_initialized = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001917
1918 smsm_size_info = smem_alloc(SMEM_SMSM_SIZE_INFO,
1919 sizeof(struct smsm_size_info_type));
1920 if (smsm_size_info) {
1921 SMSM_NUM_ENTRIES = smsm_size_info->num_entries;
1922 SMSM_NUM_HOSTS = smsm_size_info->num_hosts;
1923 }
1924
1925 if (!smsm_info.state) {
1926 smsm_info.state = smem_alloc2(ID_SHARED_STATE,
1927 SMSM_NUM_ENTRIES *
1928 sizeof(uint32_t));
1929
1930 if (smsm_info.state) {
1931 __raw_writel(0, SMSM_STATE_ADDR(SMSM_APPS_STATE));
1932 if ((shared->version[VERSION_MODEM] >> 16) >= 0xB)
1933 __raw_writel(0, \
1934 SMSM_STATE_ADDR(SMSM_APPS_DEM_I));
1935 }
1936 }
1937
1938 if (!smsm_info.intr_mask) {
1939 smsm_info.intr_mask = smem_alloc2(SMEM_SMSM_CPU_INTR_MASK,
1940 SMSM_NUM_ENTRIES *
1941 SMSM_NUM_HOSTS *
1942 sizeof(uint32_t));
1943
1944 if (smsm_info.intr_mask)
1945 for (i = 0; i < SMSM_NUM_ENTRIES; i++)
1946 __raw_writel(0xffffffff,
1947 SMSM_INTR_MASK_ADDR(i, SMSM_APPS));
1948 }
1949
1950 if (!smsm_info.intr_mux)
1951 smsm_info.intr_mux = smem_alloc2(SMEM_SMD_SMSM_INTR_MUX,
1952 SMSM_NUM_INTR_MUX *
1953 sizeof(uint32_t));
1954
1955 i = smsm_cb_init();
1956 if (i)
1957 return i;
1958
1959 wmb();
1960 return 0;
1961}
1962
1963void smsm_reset_modem(unsigned mode)
1964{
1965 if (mode == SMSM_SYSTEM_DOWNLOAD) {
1966 mode = SMSM_RESET | SMSM_SYSTEM_DOWNLOAD;
1967 } else if (mode == SMSM_MODEM_WAIT) {
1968 mode = SMSM_RESET | SMSM_MODEM_WAIT;
1969 } else { /* reset_mode is SMSM_RESET or default */
1970 mode = SMSM_RESET;
1971 }
1972
1973 smsm_change_state(SMSM_APPS_STATE, mode, mode);
1974}
1975EXPORT_SYMBOL(smsm_reset_modem);
1976
1977void smsm_reset_modem_cont(void)
1978{
1979 unsigned long flags;
1980 uint32_t state;
1981
1982 if (!smsm_info.state)
1983 return;
1984
1985 spin_lock_irqsave(&smem_lock, flags);
1986 state = __raw_readl(SMSM_STATE_ADDR(SMSM_APPS_STATE)) \
1987 & ~SMSM_MODEM_WAIT;
1988 __raw_writel(state, SMSM_STATE_ADDR(SMSM_APPS_STATE));
1989 wmb();
1990 spin_unlock_irqrestore(&smem_lock, flags);
1991}
1992EXPORT_SYMBOL(smsm_reset_modem_cont);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001993
1994static irqreturn_t smsm_irq_handler(int irq, void *data)
1995{
1996 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001997
1998#if !defined(CONFIG_ARCH_MSM8X60)
1999 uint32_t mux_val;
2000 static uint32_t prev_smem_q6_apps_smsm;
2001
2002 if (irq == INT_ADSP_A11_SMSM) {
2003 if (!smsm_info.intr_mux)
2004 return IRQ_HANDLED;
2005 mux_val = __raw_readl(SMSM_INTR_MUX_ADDR(SMEM_Q6_APPS_SMSM));
2006 if (mux_val != prev_smem_q6_apps_smsm)
2007 prev_smem_q6_apps_smsm = mux_val;
2008 return IRQ_HANDLED;
2009 }
2010#else
2011 if (irq == INT_ADSP_A11_SMSM)
2012 return IRQ_HANDLED;
2013#endif
2014
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002015
2016 spin_lock_irqsave(&smem_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002017 if (!smsm_info.state) {
2018 SMSM_INFO("<SM NO STATE>\n");
2019 } else {
2020 unsigned old_apps, apps;
2021 unsigned modm = __raw_readl(SMSM_STATE_ADDR(SMSM_MODEM_STATE));
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002022
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002023 old_apps = apps = __raw_readl(SMSM_STATE_ADDR(SMSM_APPS_STATE));
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002024
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002025 SMSM_DBG("<SM %08x %08x>\n", apps, modm);
2026 if (apps & SMSM_RESET) {
2027 /* If we get an interrupt and the apps SMSM_RESET
2028 bit is already set, the modem is acking the
2029 app's reset ack. */
2030 apps &= ~SMSM_RESET;
Daniel Walker79848a22010-03-16 15:20:07 -07002031
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002032 /* Issue a fake irq to handle any
2033 * smd state changes during reset
2034 */
2035 smd_fake_irq_handler(0);
Brian Swetland5b0f5a32009-04-26 18:38:49 -07002036
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002037 /* queue modem restart notify chain */
2038 modem_queue_start_reset_notify();
2039
2040 } else if (modm & SMSM_RESET) {
2041 apps |= SMSM_RESET;
2042
2043 pr_err("\nSMSM: Modem SMSM state changed to SMSM_RESET.");
2044 modem_queue_start_reset_notify();
2045
2046 } else if (modm & SMSM_INIT) {
2047 if (!(apps & SMSM_INIT)) {
2048 apps |= SMSM_INIT;
2049 modem_queue_smsm_init_notify();
2050 }
2051
2052 if (modm & SMSM_SMDINIT)
2053 apps |= SMSM_SMDINIT;
2054 if ((apps & (SMSM_INIT | SMSM_SMDINIT | SMSM_RPCINIT)) ==
2055 (SMSM_INIT | SMSM_SMDINIT | SMSM_RPCINIT))
2056 apps |= SMSM_RUN;
2057 } else if (modm & SMSM_SYSTEM_DOWNLOAD) {
2058 pr_err("\nSMSM: Modem SMSM state changed to SMSM_SYSTEM_DOWNLOAD.");
2059 modem_queue_start_reset_notify();
2060 }
2061
2062 if (old_apps != apps) {
2063 SMSM_DBG("<SM %08x NOTIFY>\n", apps);
2064 __raw_writel(apps, SMSM_STATE_ADDR(SMSM_APPS_STATE));
2065 do_smd_probe();
2066 notify_other_smsm(SMSM_APPS_STATE, (old_apps ^ apps));
2067 }
2068
2069 schedule_work(&smsm_cb_work);
2070 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002071 spin_unlock_irqrestore(&smem_lock, flags);
2072 return IRQ_HANDLED;
2073}
2074
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002075int smsm_change_intr_mask(uint32_t smsm_entry,
2076 uint32_t clear_mask, uint32_t set_mask)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002077{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002078 uint32_t old_mask, new_mask;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002079 unsigned long flags;
Brian Swetland5b0f5a32009-04-26 18:38:49 -07002080
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002081 if (smsm_entry >= SMSM_NUM_ENTRIES) {
2082 pr_err("smsm_change_state: Invalid entry %d\n",
2083 smsm_entry);
2084 return -EINVAL;
2085 }
2086
2087 if (!smsm_info.intr_mask) {
2088 pr_err("smsm_change_intr_mask <SM NO STATE>\n");
Brian Swetland5b0f5a32009-04-26 18:38:49 -07002089 return -EIO;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002090 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002091
2092 spin_lock_irqsave(&smem_lock, flags);
2093
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002094 old_mask = __raw_readl(SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_APPS));
2095 new_mask = (old_mask & ~clear_mask) | set_mask;
2096 __raw_writel(new_mask, SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_APPS));
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002097
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002098 wmb();
2099 spin_unlock_irqrestore(&smem_lock, flags);
Brian Swetland5b0f5a32009-04-26 18:38:49 -07002100
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002101 return 0;
2102}
2103EXPORT_SYMBOL(smsm_change_intr_mask);
2104
2105int smsm_get_intr_mask(uint32_t smsm_entry, uint32_t *intr_mask)
2106{
2107 if (smsm_entry >= SMSM_NUM_ENTRIES) {
2108 pr_err("smsm_change_state: Invalid entry %d\n",
2109 smsm_entry);
2110 return -EINVAL;
2111 }
2112
2113 if (!smsm_info.intr_mask) {
2114 pr_err("smsm_change_intr_mask <SM NO STATE>\n");
2115 return -EIO;
2116 }
2117
2118 *intr_mask = __raw_readl(SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_APPS));
2119 return 0;
2120}
2121EXPORT_SYMBOL(smsm_get_intr_mask);
2122
2123int smsm_change_state(uint32_t smsm_entry,
2124 uint32_t clear_mask, uint32_t set_mask)
2125{
2126 unsigned long flags;
2127 uint32_t old_state, new_state;
2128
2129 if (smsm_entry >= SMSM_NUM_ENTRIES) {
2130 pr_err("smsm_change_state: Invalid entry %d",
2131 smsm_entry);
2132 return -EINVAL;
2133 }
2134
2135 if (!smsm_info.state) {
2136 pr_err("smsm_change_state <SM NO STATE>\n");
2137 return -EIO;
2138 }
2139 spin_lock_irqsave(&smem_lock, flags);
2140
2141 old_state = __raw_readl(SMSM_STATE_ADDR(smsm_entry));
2142 new_state = (old_state & ~clear_mask) | set_mask;
2143 __raw_writel(new_state, SMSM_STATE_ADDR(smsm_entry));
2144 SMSM_DBG("smsm_change_state %x\n", new_state);
2145 notify_other_smsm(SMSM_APPS_STATE, (old_state ^ new_state));
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002146
2147 spin_unlock_irqrestore(&smem_lock, flags);
2148
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002149 return 0;
2150}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002151EXPORT_SYMBOL(smsm_change_state);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002152
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002153uint32_t smsm_get_state(uint32_t smsm_entry)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002154{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002155 uint32_t rv = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002156
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002157 /* needs interface change to return error code */
2158 if (smsm_entry >= SMSM_NUM_ENTRIES) {
2159 pr_err("smsm_change_state: Invalid entry %d",
2160 smsm_entry);
2161 return 0;
2162 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002163
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002164 if (!smsm_info.state) {
2165 pr_err("smsm_get_state <SM NO STATE>\n");
2166 } else {
2167 rv = __raw_readl(SMSM_STATE_ADDR(smsm_entry));
2168 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002169
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002170 return rv;
2171}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002172EXPORT_SYMBOL(smsm_get_state);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002173
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002174/**
2175 * Performs SMSM callback client notifiction.
2176 */
2177void notify_smsm_cb_clients_worker(struct work_struct *work)
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -07002178{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002179 unsigned long flags;
2180 struct smsm_state_cb_info *cb_info;
2181 struct smsm_state_info *state_info;
2182 int n;
2183 uint32_t new_state;
2184 uint32_t state_changes;
Brian Swetland03e00cd2009-07-01 17:58:37 -07002185
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002186 spin_lock_irqsave(&smsm_lock, flags);
2187
2188 if (!smsm_states) {
2189 /* smsm not yet initialized */
2190 spin_unlock_irqrestore(&smsm_lock, flags);
2191 return;
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -07002192 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002193
2194 for (n = 0; n < SMSM_NUM_ENTRIES; n++) {
2195 state_info = &smsm_states[n];
2196 new_state = __raw_readl(SMSM_STATE_ADDR(n));
2197
2198 if (new_state != state_info->last_value) {
2199 state_changes = state_info->last_value ^ new_state;
2200
2201 list_for_each_entry(cb_info,
2202 &state_info->callbacks, cb_list) {
2203
2204 if (cb_info->mask & state_changes)
2205 cb_info->notify(cb_info->data,
2206 state_info->last_value,
2207 new_state);
2208 }
2209 state_info->last_value = new_state;
2210 }
2211 }
2212
2213 spin_unlock_irqrestore(&smsm_lock, flags);
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -07002214}
2215
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -07002216
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002217/**
2218 * Registers callback for SMSM state notifications when the specified
2219 * bits change.
2220 *
2221 * @smsm_entry Processor entry to deregister
2222 * @mask Bits to deregister (if result is 0, callback is removed)
2223 * @notify Notification function to deregister
2224 * @data Opaque data passed in to callback
2225 *
2226 * @returns Status code
2227 * <0 error code
2228 * 0 inserted new entry
2229 * 1 updated mask of existing entry
2230 */
2231int smsm_state_cb_register(uint32_t smsm_entry, uint32_t mask,
2232 void (*notify)(void *, uint32_t, uint32_t), void *data)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002233{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002234 unsigned long flags;
2235 struct smsm_state_cb_info *cb_info;
2236 struct smsm_state_cb_info *cb_found = 0;
2237 int ret = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002238
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002239 if (smsm_entry >= SMSM_NUM_ENTRIES)
2240 return -EINVAL;
2241
2242 spin_lock_irqsave(&smsm_lock, flags);
2243
2244 if (!smsm_states) {
2245 /* smsm not yet initialized */
2246 ret = -ENODEV;
2247 goto cleanup;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002248 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002249
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002250 list_for_each_entry(cb_info,
2251 &smsm_states[smsm_entry].callbacks, cb_list) {
2252 if ((cb_info->notify == notify) &&
2253 (cb_info->data == data)) {
2254 cb_info->mask |= mask;
2255 cb_found = cb_info;
2256 ret = 1;
Brian Swetland5b0f5a32009-04-26 18:38:49 -07002257 break;
2258 }
2259 }
2260
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002261 if (!cb_found) {
2262 cb_info = kmalloc(sizeof(struct smsm_state_cb_info),
2263 GFP_ATOMIC);
2264 if (!cb_info) {
2265 ret = -ENOMEM;
2266 goto cleanup;
2267 }
2268
2269 cb_info->mask = mask;
2270 cb_info->notify = notify;
2271 cb_info->data = data;
2272 INIT_LIST_HEAD(&cb_info->cb_list);
2273 list_add_tail(&cb_info->cb_list,
2274 &smsm_states[smsm_entry].callbacks);
2275 }
2276
2277cleanup:
2278 spin_unlock_irqrestore(&smsm_lock, flags);
2279 return ret;
2280}
2281EXPORT_SYMBOL(smsm_state_cb_register);
2282
2283
2284/**
2285 * Deregisters for SMSM state notifications for the specified bits.
2286 *
2287 * @smsm_entry Processor entry to deregister
2288 * @mask Bits to deregister (if result is 0, callback is removed)
2289 * @notify Notification function to deregister
2290 * @data Opaque data passed in to callback
2291 *
2292 * @returns Status code
2293 * <0 error code
2294 * 0 not found
2295 * 1 updated mask
2296 * 2 removed callback
2297 */
2298int smsm_state_cb_deregister(uint32_t smsm_entry, uint32_t mask,
2299 void (*notify)(void *, uint32_t, uint32_t), void *data)
2300{
2301 unsigned long flags;
2302 struct smsm_state_cb_info *cb_info;
2303 int ret = 0;
2304
2305 if (smsm_entry >= SMSM_NUM_ENTRIES)
2306 return -EINVAL;
2307
2308 spin_lock_irqsave(&smsm_lock, flags);
2309
2310 if (!smsm_states) {
2311 /* smsm not yet initialized */
2312 spin_unlock_irqrestore(&smsm_lock, flags);
2313 return -ENODEV;
2314 }
2315
2316 list_for_each_entry(cb_info,
2317 &smsm_states[smsm_entry].callbacks, cb_list) {
2318 if ((cb_info->notify == notify) &&
2319 (cb_info->data == data)) {
2320 cb_info->mask &= ~mask;
2321 ret = 1;
2322 if (!cb_info->mask) {
2323 /* no mask bits set, remove callback */
2324 list_del(&cb_info->cb_list);
2325 kfree(cb_info);
2326 ret = 2;
2327 }
2328 break;
2329 }
2330 }
2331
2332 spin_unlock_irqrestore(&smsm_lock, flags);
2333 return ret;
2334}
2335EXPORT_SYMBOL(smsm_state_cb_deregister);
2336
2337
2338int smd_core_init(void)
2339{
2340 int r;
2341 unsigned long flags = IRQF_TRIGGER_RISING;
2342 SMD_INFO("smd_core_init()\n");
Brian Swetland5b0f5a32009-04-26 18:38:49 -07002343
Brian Swetland37521a32009-07-01 18:30:47 -07002344 r = request_irq(INT_A9_M2A_0, smd_modem_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002345 flags, "smd_dev", 0);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002346 if (r < 0)
2347 return r;
2348 r = enable_irq_wake(INT_A9_M2A_0);
2349 if (r < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002350 pr_err("smd_core_init: "
2351 "enable_irq_wake failed for INT_A9_M2A_0\n");
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002352
2353 r = request_irq(INT_A9_M2A_5, smsm_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002354 flags, "smsm_dev", 0);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002355 if (r < 0) {
2356 free_irq(INT_A9_M2A_0, 0);
2357 return r;
2358 }
2359 r = enable_irq_wake(INT_A9_M2A_5);
2360 if (r < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002361 pr_err("smd_core_init: "
2362 "enable_irq_wake failed for INT_A9_M2A_5\n");
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002363
Brian Swetland37521a32009-07-01 18:30:47 -07002364#if defined(CONFIG_QDSP6)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002365#if (INT_ADSP_A11 == INT_ADSP_A11_SMSM)
2366 flags |= IRQF_SHARED;
2367#endif
Brian Swetland37521a32009-07-01 18:30:47 -07002368 r = request_irq(INT_ADSP_A11, smd_dsp_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002369 flags, "smd_dev", smd_dsp_irq_handler);
Brian Swetland37521a32009-07-01 18:30:47 -07002370 if (r < 0) {
2371 free_irq(INT_A9_M2A_0, 0);
2372 free_irq(INT_A9_M2A_5, 0);
2373 return r;
2374 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002375
2376 r = request_irq(INT_ADSP_A11_SMSM, smsm_irq_handler,
2377 flags, "smsm_dev", smsm_irq_handler);
2378 if (r < 0) {
2379 free_irq(INT_A9_M2A_0, 0);
2380 free_irq(INT_A9_M2A_5, 0);
2381 free_irq(INT_ADSP_A11, smd_dsp_irq_handler);
2382 return r;
2383 }
2384
2385 r = enable_irq_wake(INT_ADSP_A11);
2386 if (r < 0)
2387 pr_err("smd_core_init: "
2388 "enable_irq_wake failed for INT_ADSP_A11\n");
2389
2390#if (INT_ADSP_A11 != INT_ADSP_A11_SMSM)
2391 r = enable_irq_wake(INT_ADSP_A11_SMSM);
2392 if (r < 0)
2393 pr_err("smd_core_init: enable_irq_wake "
2394 "failed for INT_ADSP_A11_SMSM\n");
2395#endif
2396 flags &= ~IRQF_SHARED;
Brian Swetland37521a32009-07-01 18:30:47 -07002397#endif
2398
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002399#if defined(CONFIG_DSPS)
2400 r = request_irq(INT_DSPS_A11, smd_dsps_irq_handler,
2401 flags, "smd_dev", smd_dsps_irq_handler);
2402 if (r < 0) {
2403 free_irq(INT_A9_M2A_0, 0);
2404 free_irq(INT_A9_M2A_5, 0);
2405 free_irq(INT_ADSP_A11, smd_dsp_irq_handler);
2406 free_irq(INT_ADSP_A11_SMSM, smsm_irq_handler);
2407 return r;
2408 }
Brian Swetland5b0f5a32009-04-26 18:38:49 -07002409
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002410 r = enable_irq_wake(INT_DSPS_A11);
2411 if (r < 0)
2412 pr_err("smd_core_init: "
2413 "enable_irq_wake failed for INT_ADSP_A11\n");
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -07002414#endif
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002415
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002416#if defined(CONFIG_WCNSS)
2417 r = request_irq(INT_WCNSS_A11, smd_wcnss_irq_handler,
2418 flags, "smd_dev", smd_wcnss_irq_handler);
2419 if (r < 0) {
2420 free_irq(INT_A9_M2A_0, 0);
2421 free_irq(INT_A9_M2A_5, 0);
2422 free_irq(INT_ADSP_A11, smd_dsp_irq_handler);
2423 free_irq(INT_ADSP_A11_SMSM, smsm_irq_handler);
2424 free_irq(INT_DSPS_A11, smd_dsps_irq_handler);
2425 return r;
2426 }
2427
2428 r = enable_irq_wake(INT_WCNSS_A11);
2429 if (r < 0)
2430 pr_err("smd_core_init: "
2431 "enable_irq_wake failed for INT_WCNSS_A11\n");
2432
2433 r = request_irq(INT_WCNSS_A11_SMSM, smsm_irq_handler,
2434 flags, "smsm_dev", smsm_irq_handler);
2435 if (r < 0) {
2436 free_irq(INT_A9_M2A_0, 0);
2437 free_irq(INT_A9_M2A_5, 0);
2438 free_irq(INT_ADSP_A11, smd_dsp_irq_handler);
2439 free_irq(INT_ADSP_A11_SMSM, smsm_irq_handler);
2440 free_irq(INT_DSPS_A11, smd_dsps_irq_handler);
2441 free_irq(INT_WCNSS_A11, smd_wcnss_irq_handler);
2442 return r;
2443 }
2444
2445 r = enable_irq_wake(INT_WCNSS_A11_SMSM);
2446 if (r < 0)
2447 pr_err("smd_core_init: "
2448 "enable_irq_wake failed for INT_WCNSS_A11_SMSM\n");
2449#endif
2450
Jeff Hugo6a8057c2011-08-16 13:47:12 -06002451#if defined(CONFIG_DSPS_SMSM)
2452 r = request_irq(INT_DSPS_A11_SMSM, smsm_irq_handler,
2453 flags, "smsm_dev", smsm_irq_handler);
2454 if (r < 0) {
2455 free_irq(INT_A9_M2A_0, 0);
2456 free_irq(INT_A9_M2A_5, 0);
2457 free_irq(INT_ADSP_A11, smd_dsp_irq_handler);
2458 free_irq(INT_ADSP_A11_SMSM, smsm_irq_handler);
2459 free_irq(INT_DSPS_A11, smd_dsps_irq_handler);
2460 free_irq(INT_WCNSS_A11, smd_wcnss_irq_handler);
2461 free_irq(INT_WCNSS_A11_SMSM, smsm_irq_handler);
2462 return r;
2463 }
2464
2465 r = enable_irq_wake(INT_DSPS_A11_SMSM);
2466 if (r < 0)
2467 pr_err("smd_core_init: "
2468 "enable_irq_wake failed for INT_DSPS_A11_SMSM\n");
2469#endif
2470
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002471 /* we may have missed a signal while booting -- fake
2472 * an interrupt to make sure we process any existing
2473 * state
2474 */
2475 smsm_irq_handler(0, 0);
2476
2477 SMD_INFO("smd_core_init() done\n");
2478
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002479 return 0;
2480}
2481
Gregory Bean4416e9e2010-07-28 10:22:12 -07002482static int __devinit msm_smd_probe(struct platform_device *pdev)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002483{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002484 SMD_INFO("smd probe\n");
Daniel Walker0aec66d2010-03-18 12:31:08 -07002485
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002486 INIT_WORK(&probe_work, smd_channel_probe_worker);
2487
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002488 channel_close_wq = create_singlethread_workqueue("smd_channel_close");
2489 if (IS_ERR(channel_close_wq)) {
2490 pr_err("%s: create_singlethread_workqueue ENOMEM\n", __func__);
2491 return -ENOMEM;
2492 }
2493
2494 if (smsm_init()) {
2495 pr_err("smsm_init() failed\n");
2496 return -1;
2497 }
2498
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002499 if (smd_core_init()) {
2500 pr_err("smd_core_init() failed\n");
2501 return -1;
2502 }
2503
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002504 smd_initialized = 1;
2505
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002506 smd_alloc_loopback_channel();
2507
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002508 return 0;
2509}
2510
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002511static int restart_notifier_cb(struct notifier_block *this,
2512 unsigned long code,
2513 void *data);
2514
2515static struct restart_notifier_block restart_notifiers[] = {
2516 {SMSM_MODEM, "modem", .nb.notifier_call = restart_notifier_cb},
2517 {SMSM_Q6, "lpass", .nb.notifier_call = restart_notifier_cb},
2518};
2519
2520static int restart_notifier_cb(struct notifier_block *this,
2521 unsigned long code,
2522 void *data)
2523{
2524 if (code == SUBSYS_AFTER_SHUTDOWN) {
2525 struct restart_notifier_block *notifier;
2526
2527 notifier = container_of(this,
2528 struct restart_notifier_block, nb);
2529 SMD_INFO("%s: ssrestart for processor %d ('%s')\n",
2530 __func__, notifier->processor,
2531 notifier->name);
2532
2533 smd_channel_reset(notifier->processor);
2534 }
2535
2536 return NOTIFY_DONE;
2537}
2538
2539static __init int modem_restart_late_init(void)
2540{
2541 int i;
2542 void *handle;
2543 struct restart_notifier_block *nb;
2544
2545 for (i = 0; i < ARRAY_SIZE(restart_notifiers); i++) {
2546 nb = &restart_notifiers[i];
2547 handle = subsys_notif_register_notifier(nb->name, &nb->nb);
2548 SMD_DBG("%s: registering notif for '%s', handle=%p\n",
2549 __func__, nb->name, handle);
2550 }
2551 return 0;
2552}
2553late_initcall(modem_restart_late_init);
2554
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002555static struct platform_driver msm_smd_driver = {
2556 .probe = msm_smd_probe,
2557 .driver = {
2558 .name = MODULE_NAME,
2559 .owner = THIS_MODULE,
2560 },
2561};
2562
2563static int __init msm_smd_init(void)
2564{
2565 return platform_driver_register(&msm_smd_driver);
2566}
2567
2568module_init(msm_smd_init);
2569
2570MODULE_DESCRIPTION("MSM Shared Memory Core");
2571MODULE_AUTHOR("Brian Swetland <swetland@google.com>");
2572MODULE_LICENSE("GPL");