blob: 7dec32ae417786b1b64ca9e0b1ae05fb8d13a069 [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>
Angshuman Sarkaread67bd2011-09-21 20:13:12 +053038#include <mach/socinfo.h>
Brian Swetland2eb44eb2008-09-29 16:00:48 -070039
40#include "smd_private.h"
41#include "proc_comm.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070042#include "modem_notifier.h"
Brian Swetland2eb44eb2008-09-29 16:00:48 -070043
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070044#if defined(CONFIG_ARCH_QSD8X50) || defined(CONFIG_ARCH_MSM8X60) \
Jeff Hugo56b933a2011-09-28 14:42:05 -060045 || defined(CONFIG_ARCH_MSM8960) || defined(CONFIG_ARCH_FSM9XXX) \
46 || defined(CONFIG_ARCH_MSM9615)
Brian Swetland37521a32009-07-01 18:30:47 -070047#define CONFIG_QDSP6 1
48#endif
49
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070050#if defined(CONFIG_ARCH_MSM8X60) || defined(CONFIG_ARCH_MSM8960)
51#define CONFIG_DSPS 1
52#endif
53
54#if defined(CONFIG_ARCH_MSM8960)
55#define CONFIG_WCNSS 1
Jeff Hugo6a8057c2011-08-16 13:47:12 -060056#define CONFIG_DSPS_SMSM 1
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070057#endif
Brian Swetland2eb44eb2008-09-29 16:00:48 -070058
59#define MODULE_NAME "msm_smd"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070060#define SMEM_VERSION 0x000B
61#define SMD_VERSION 0x00020000
62
63uint32_t SMSM_NUM_ENTRIES = 8;
64uint32_t SMSM_NUM_HOSTS = 3;
Brian Swetland2eb44eb2008-09-29 16:00:48 -070065
66enum {
67 MSM_SMD_DEBUG = 1U << 0,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070068 MSM_SMSM_DEBUG = 1U << 1,
69 MSM_SMD_INFO = 1U << 2,
70 MSM_SMSM_INFO = 1U << 3,
71};
72
73struct smsm_shared_info {
74 uint32_t *state;
75 uint32_t *intr_mask;
76 uint32_t *intr_mux;
77};
78
79static struct smsm_shared_info smsm_info;
80
81struct smsm_size_info_type {
82 uint32_t num_hosts;
83 uint32_t num_entries;
84 uint32_t reserved0;
85 uint32_t reserved1;
86};
87
88struct smsm_state_cb_info {
89 struct list_head cb_list;
90 uint32_t mask;
91 void *data;
92 void (*notify)(void *data, uint32_t old_state, uint32_t new_state);
93};
94
95struct smsm_state_info {
96 struct list_head callbacks;
97 uint32_t last_value;
98};
99
100#define SMSM_STATE_ADDR(entry) (smsm_info.state + entry)
101#define SMSM_INTR_MASK_ADDR(entry, host) (smsm_info.intr_mask + \
102 entry * SMSM_NUM_HOSTS + host)
103#define SMSM_INTR_MUX_ADDR(entry) (smsm_info.intr_mux + entry)
104
105/* Internal definitions which are not exported in some targets */
106enum {
107 SMSM_APPS_DEM_I = 3,
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700108};
109
110static int msm_smd_debug_mask;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700111module_param_named(debug_mask, msm_smd_debug_mask,
112 int, S_IRUGO | S_IWUSR | S_IWGRP);
113
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700114#if defined(CONFIG_MSM_SMD_DEBUG)
115#define SMD_DBG(x...) do { \
116 if (msm_smd_debug_mask & MSM_SMD_DEBUG) \
117 printk(KERN_DEBUG x); \
118 } while (0)
119
120#define SMSM_DBG(x...) do { \
121 if (msm_smd_debug_mask & MSM_SMSM_DEBUG) \
122 printk(KERN_DEBUG x); \
123 } while (0)
124
125#define SMD_INFO(x...) do { \
126 if (msm_smd_debug_mask & MSM_SMD_INFO) \
127 printk(KERN_INFO x); \
128 } while (0)
129
130#define SMSM_INFO(x...) do { \
131 if (msm_smd_debug_mask & MSM_SMSM_INFO) \
132 printk(KERN_INFO x); \
133 } while (0)
134#else
135#define SMD_DBG(x...) do { } while (0)
136#define SMSM_DBG(x...) do { } while (0)
137#define SMD_INFO(x...) do { } while (0)
138#define SMSM_INFO(x...) do { } while (0)
139#endif
140
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700141static unsigned last_heap_free = 0xffffffff;
142
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700143static inline void smd_write_intr(unsigned int val,
144 const void __iomem *addr);
145
146#if defined(CONFIG_ARCH_MSM7X30)
147#define MSM_TRIG_A2M_SMD_INT \
148 (smd_write_intr(1 << 0, MSM_GCC_BASE + 0x8))
149#define MSM_TRIG_A2Q6_SMD_INT \
150 (smd_write_intr(1 << 8, MSM_GCC_BASE + 0x8))
151#define MSM_TRIG_A2M_SMSM_INT \
152 (smd_write_intr(1 << 5, MSM_GCC_BASE + 0x8))
153#define MSM_TRIG_A2Q6_SMSM_INT \
154 (smd_write_intr(1 << 8, MSM_GCC_BASE + 0x8))
155#define MSM_TRIG_A2DSPS_SMD_INT
Jeff Hugo6a8057c2011-08-16 13:47:12 -0600156#define MSM_TRIG_A2DSPS_SMSM_INT
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700157#define MSM_TRIG_A2WCNSS_SMD_INT
158#define MSM_TRIG_A2WCNSS_SMSM_INT
159#elif defined(CONFIG_ARCH_MSM8X60)
160#define MSM_TRIG_A2M_SMD_INT \
161 (smd_write_intr(1 << 3, MSM_GCC_BASE + 0x8))
162#define MSM_TRIG_A2Q6_SMD_INT \
163 (smd_write_intr(1 << 15, MSM_GCC_BASE + 0x8))
164#define MSM_TRIG_A2M_SMSM_INT \
165 (smd_write_intr(1 << 4, MSM_GCC_BASE + 0x8))
166#define MSM_TRIG_A2Q6_SMSM_INT \
167 (smd_write_intr(1 << 14, MSM_GCC_BASE + 0x8))
168#define MSM_TRIG_A2DSPS_SMD_INT \
169 (smd_write_intr(1, MSM_SIC_NON_SECURE_BASE + 0x4080))
Jeff Hugo6a8057c2011-08-16 13:47:12 -0600170#define MSM_TRIG_A2DSPS_SMSM_INT
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700171#define MSM_TRIG_A2WCNSS_SMD_INT
172#define MSM_TRIG_A2WCNSS_SMSM_INT
173#elif defined(CONFIG_ARCH_MSM8960)
174#define MSM_TRIG_A2M_SMD_INT \
175 (smd_write_intr(1 << 3, MSM_APCS_GCC_BASE + 0x8))
176#define MSM_TRIG_A2Q6_SMD_INT \
177 (smd_write_intr(1 << 15, MSM_APCS_GCC_BASE + 0x8))
178#define MSM_TRIG_A2M_SMSM_INT \
179 (smd_write_intr(1 << 4, MSM_APCS_GCC_BASE + 0x8))
180#define MSM_TRIG_A2Q6_SMSM_INT \
181 (smd_write_intr(1 << 14, MSM_APCS_GCC_BASE + 0x8))
182#define MSM_TRIG_A2DSPS_SMD_INT \
183 (smd_write_intr(1, MSM_SIC_NON_SECURE_BASE + 0x4080))
Jeff Hugo6a8057c2011-08-16 13:47:12 -0600184#define MSM_TRIG_A2DSPS_SMSM_INT \
185 (smd_write_intr(1, MSM_SIC_NON_SECURE_BASE + 0x4094))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700186#define MSM_TRIG_A2WCNSS_SMD_INT \
187 (smd_write_intr(1 << 25, MSM_APCS_GCC_BASE + 0x8))
188#define MSM_TRIG_A2WCNSS_SMSM_INT \
189 (smd_write_intr(1 << 23, MSM_APCS_GCC_BASE + 0x8))
Jeff Hugo56b933a2011-09-28 14:42:05 -0600190#elif defined(CONFIG_ARCH_MSM9615)
191#define MSM_TRIG_A2M_SMD_INT \
192 (smd_write_intr(1 << 3, MSM_APCS_GCC_BASE + 0x8))
193#define MSM_TRIG_A2Q6_SMD_INT \
194 (smd_write_intr(1 << 15, MSM_APCS_GCC_BASE + 0x8))
195#define MSM_TRIG_A2M_SMSM_INT \
196 (smd_write_intr(1 << 4, MSM_APCS_GCC_BASE + 0x8))
197#define MSM_TRIG_A2Q6_SMSM_INT \
198 (smd_write_intr(1 << 14, MSM_APCS_GCC_BASE + 0x8))
199#define MSM_TRIG_A2DSPS_SMD_INT
200#define MSM_TRIG_A2DSPS_SMSM_INT
201#define MSM_TRIG_A2WCNSS_SMD_INT
202#define MSM_TRIG_A2WCNSS_SMSM_INT
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700203#elif defined(CONFIG_ARCH_FSM9XXX)
204#define MSM_TRIG_A2Q6_SMD_INT \
205 (smd_write_intr(1 << 10, MSM_GCC_BASE + 0x8))
206#define MSM_TRIG_A2Q6_SMSM_INT \
207 (smd_write_intr(1 << 10, MSM_GCC_BASE + 0x8))
208#define MSM_TRIG_A2M_SMD_INT \
209 (smd_write_intr(1 << 0, MSM_GCC_BASE + 0x8))
210#define MSM_TRIG_A2M_SMSM_INT \
211 (smd_write_intr(1 << 5, MSM_GCC_BASE + 0x8))
212#define MSM_TRIG_A2DSPS_SMD_INT
Jeff Hugo6a8057c2011-08-16 13:47:12 -0600213#define MSM_TRIG_A2DSPS_SMSM_INT
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700214#define MSM_TRIG_A2WCNSS_SMD_INT
215#define MSM_TRIG_A2WCNSS_SMSM_INT
216#else
217#define MSM_TRIG_A2M_SMD_INT \
218 (smd_write_intr(1, MSM_CSR_BASE + 0x400 + (0) * 4))
219#define MSM_TRIG_A2Q6_SMD_INT \
220 (smd_write_intr(1, MSM_CSR_BASE + 0x400 + (8) * 4))
221#define MSM_TRIG_A2M_SMSM_INT \
222 (smd_write_intr(1, MSM_CSR_BASE + 0x400 + (5) * 4))
223#define MSM_TRIG_A2Q6_SMSM_INT \
224 (smd_write_intr(1, MSM_CSR_BASE + 0x400 + (8) * 4))
225#define MSM_TRIG_A2DSPS_SMD_INT
Jeff Hugo6a8057c2011-08-16 13:47:12 -0600226#define MSM_TRIG_A2DSPS_SMSM_INT
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700227#define MSM_TRIG_A2WCNSS_SMD_INT
228#define MSM_TRIG_A2WCNSS_SMSM_INT
Brian Swetland37521a32009-07-01 18:30:47 -0700229#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700230
231#define SMD_LOOPBACK_CID 100
232
Eric Holmbergf6d7d1a2011-09-23 18:31:04 -0600233#define SMEM_SPINLOCK_SMEM_ALLOC "S:3"
234static remote_spinlock_t remote_spinlock;
235
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700236static LIST_HEAD(smd_ch_list_loopback);
237static irqreturn_t smsm_irq_handler(int irq, void *data);
238static void smd_fake_irq_handler(unsigned long arg);
239
240static void notify_smsm_cb_clients_worker(struct work_struct *work);
241static DECLARE_WORK(smsm_cb_work, notify_smsm_cb_clients_worker);
Eric Holmbergc8002902011-09-16 13:55:57 -0600242static DEFINE_MUTEX(smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700243static struct smsm_state_info *smsm_states;
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +0530244static int spinlocks_initialized;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700245
246static inline void smd_write_intr(unsigned int val,
247 const void __iomem *addr)
248{
249 wmb();
250 __raw_writel(val, addr);
251}
252
253#ifdef CONFIG_WCNSS
254static inline void wakeup_v1_riva(void)
255{
256 /*
257 * workaround hack for RIVA v1 hardware bug
258 * trigger GPIO 40 to wake up RIVA from power collaspe
259 * not to be sent to customers
260 */
261 __raw_writel(0x0, MSM_TLMM_BASE + 0x1284);
262 __raw_writel(0x2, MSM_TLMM_BASE + 0x1284);
263 /* end workaround */
264}
265#else
266static inline void wakeup_v1_riva(void) {}
267#endif
268
269static void notify_other_smsm(uint32_t smsm_entry, uint32_t notify_mask)
270{
271 /* older protocol don't use smsm_intr_mask,
272 but still communicates with modem */
273 if (!smsm_info.intr_mask ||
274 (__raw_readl(SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_MODEM))
275 & notify_mask))
276 MSM_TRIG_A2M_SMSM_INT;
277
278 if (smsm_info.intr_mask &&
279 (__raw_readl(SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_Q6))
280 & notify_mask)) {
281#if !defined(CONFIG_ARCH_MSM8X60) && !defined(MCONFIG_ARCH_MSM8960)
282 uint32_t mux_val;
283
284 if (smsm_info.intr_mux) {
285 mux_val = __raw_readl(
286 SMSM_INTR_MUX_ADDR(SMEM_APPS_Q6_SMSM));
287 mux_val++;
288 __raw_writel(mux_val,
289 SMSM_INTR_MUX_ADDR(SMEM_APPS_Q6_SMSM));
290 }
291#endif
292 MSM_TRIG_A2Q6_SMSM_INT;
293 }
294
295 if (smsm_info.intr_mask &&
296 (__raw_readl(SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_WCNSS))
297 & notify_mask)) {
298 wakeup_v1_riva();
299 MSM_TRIG_A2WCNSS_SMSM_INT;
300 }
301
Jeff Hugo6a8057c2011-08-16 13:47:12 -0600302 if (smsm_info.intr_mask &&
303 (__raw_readl(SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_DSPS))
304 & notify_mask)) {
305 MSM_TRIG_A2DSPS_SMSM_INT;
306 }
307
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700308 schedule_work(&smsm_cb_work);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700309}
310
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700311static inline void notify_modem_smd(void)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700312{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700313 MSM_TRIG_A2M_SMD_INT;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700314}
315
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700316static inline void notify_dsp_smd(void)
317{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700318 MSM_TRIG_A2Q6_SMD_INT;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700319}
320
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700321static inline void notify_dsps_smd(void)
322{
323 MSM_TRIG_A2DSPS_SMD_INT;
324}
325
326static inline void notify_wcnss_smd(void)
327{
328 wakeup_v1_riva();
329 MSM_TRIG_A2WCNSS_SMD_INT;
330}
331
332void smd_diag(void)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700333{
334 char *x;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700335 int size;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700336
337 x = smem_find(ID_DIAG_ERR_MSG, SZ_DIAG_ERR_MSG);
338 if (x != 0) {
339 x[SZ_DIAG_ERR_MSG - 1] = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700340 SMD_INFO("smem: DIAG '%s'\n", x);
341 }
342
343 x = smem_get_entry(SMEM_ERR_CRASH_LOG, &size);
344 if (x != 0) {
345 x[size - 1] = 0;
346 pr_err("smem: CRASH LOG\n'%s'\n", x);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700347 }
348}
349
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700350
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700351static void handle_modem_crash(void)
352{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700353 pr_err("MODEM/AMSS has CRASHED\n");
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700354 smd_diag();
355
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700356 /* hard reboot if possible FIXME
357 if (msm_reset_hook)
358 msm_reset_hook();
359 */
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700360
361 /* in this case the modem or watchdog should reboot us */
362 for (;;)
363 ;
364}
365
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700366int smsm_check_for_modem_crash(void)
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700367{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700368 /* if the modem's not ready yet, we have to hope for the best */
369 if (!smsm_info.state)
370 return 0;
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700371
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700372 if (__raw_readl(SMSM_STATE_ADDR(SMSM_MODEM_STATE)) & SMSM_RESET) {
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700373 handle_modem_crash();
374 return -1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700375 }
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700376 return 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700377}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700378EXPORT_SYMBOL(smsm_check_for_modem_crash);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700379
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700380/* the spinlock is used to synchronize between the
Brian Swetland03e00cd2009-07-01 17:58:37 -0700381 * irq handler and code that mutates the channel
382 * list or fiddles with channel state
383 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700384static DEFINE_SPINLOCK(smd_lock);
Brian Swetland03e00cd2009-07-01 17:58:37 -0700385DEFINE_SPINLOCK(smem_lock);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700386
387/* the mutex is used during open() and close()
Brian Swetland03e00cd2009-07-01 17:58:37 -0700388 * operations to avoid races while creating or
389 * destroying smd_channel structures
390 */
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700391static DEFINE_MUTEX(smd_creation_mutex);
392
393static int smd_initialized;
394
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700395struct smd_shared_v1 {
396 struct smd_half_channel ch0;
397 unsigned char data0[SMD_BUF_SIZE];
398 struct smd_half_channel ch1;
399 unsigned char data1[SMD_BUF_SIZE];
400};
401
402struct smd_shared_v2 {
403 struct smd_half_channel ch0;
404 struct smd_half_channel ch1;
405};
406
407struct smd_channel {
408 volatile struct smd_half_channel *send;
409 volatile struct smd_half_channel *recv;
410 unsigned char *send_data;
411 unsigned char *recv_data;
412 unsigned fifo_size;
413 unsigned fifo_mask;
414 struct list_head ch_list;
415
416 unsigned current_packet;
417 unsigned n;
418 void *priv;
419 void (*notify)(void *priv, unsigned flags);
420
421 int (*read)(smd_channel_t *ch, void *data, int len, int user_buf);
422 int (*write)(smd_channel_t *ch, const void *data, int len,
423 int user_buf);
424 int (*read_avail)(smd_channel_t *ch);
425 int (*write_avail)(smd_channel_t *ch);
426 int (*read_from_cb)(smd_channel_t *ch, void *data, int len,
427 int user_buf);
428
429 void (*update_state)(smd_channel_t *ch);
430 unsigned last_state;
431 void (*notify_other_cpu)(void);
432
433 char name[20];
434 struct platform_device pdev;
435 unsigned type;
436
437 int pending_pkt_sz;
438
439 char is_pkt_ch;
440};
441
442struct edge_to_pid {
443 uint32_t local_pid;
444 uint32_t remote_pid;
445};
446
Eric Holmberg7dfd1972011-09-09 16:07:57 -0600447/*
448 * SMD Processor ID's.
449 *
450 * For all processors that have both SMSM and SMD clients,
451 * the SMSM Processor ID and the SMD Processor ID will
452 * be the same. In cases where a processor only supports
453 * SMD, the entry will only exist in this enum.
454 */
455enum {
456 SMD_APPS = SMSM_APPS,
457 SMD_MODEM = SMSM_MODEM,
458 SMD_Q6 = SMSM_Q6,
459 SMD_WCNSS = SMSM_WCNSS,
460 SMD_DSPS = SMSM_DSPS,
461 SMD_MODEM_Q6_FW,
462};
463
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700464/**
465 * Maps edge type to local and remote processor ID's.
466 */
467static struct edge_to_pid edge_to_pids[] = {
468 [SMD_APPS_MODEM] = {SMSM_APPS, SMSM_MODEM},
469 [SMD_APPS_QDSP] = {SMSM_APPS, SMSM_Q6},
470 [SMD_MODEM_QDSP] = {SMSM_MODEM, SMSM_Q6},
471 [SMD_APPS_DSPS] = {SMSM_APPS, SMSM_DSPS},
472 [SMD_MODEM_DSPS] = {SMSM_MODEM, SMSM_DSPS},
473 [SMD_QDSP_DSPS] = {SMSM_Q6, SMSM_DSPS},
474 [SMD_APPS_WCNSS] = {SMSM_APPS, SMSM_WCNSS},
475 [SMD_MODEM_WCNSS] = {SMSM_MODEM, SMSM_WCNSS},
476 [SMD_QDSP_WCNSS] = {SMSM_Q6, SMSM_WCNSS},
477 [SMD_DSPS_WCNSS] = {SMSM_DSPS, SMSM_WCNSS},
Eric Holmberg7dfd1972011-09-09 16:07:57 -0600478 [SMD_APPS_Q6FW] = {SMSM_APPS, SMD_MODEM_Q6_FW},
479 [SMD_MODEM_Q6FW] = {SMSM_MODEM, SMD_MODEM_Q6_FW},
480 [SMD_QDSP_Q6FW] = {SMSM_Q6, SMD_MODEM_Q6_FW},
481 [SMD_DSPS_Q6FW] = {SMSM_DSPS, SMD_MODEM_Q6_FW},
482 [SMD_WCNSS_Q6FW] = {SMSM_WCNSS, SMD_MODEM_Q6_FW},
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700483};
484
485struct restart_notifier_block {
486 unsigned processor;
487 char *name;
488 struct notifier_block nb;
489};
490
491static struct platform_device loopback_tty_pdev = {.name = "LOOPBACK_TTY"};
492
493static LIST_HEAD(smd_ch_closed_list);
494static LIST_HEAD(smd_ch_closing_list);
495static LIST_HEAD(smd_ch_to_close_list);
496static LIST_HEAD(smd_ch_list_modem);
497static LIST_HEAD(smd_ch_list_dsp);
498static LIST_HEAD(smd_ch_list_dsps);
499static LIST_HEAD(smd_ch_list_wcnss);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700500
501static unsigned char smd_ch_allocated[64];
502static struct work_struct probe_work;
503
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700504static void finalize_channel_close_fn(struct work_struct *work);
505static DECLARE_WORK(finalize_channel_close_work, finalize_channel_close_fn);
506static struct workqueue_struct *channel_close_wq;
507
508static int smd_alloc_channel(struct smd_alloc_elm *alloc_elm);
509
510/* on smp systems, the probe might get called from multiple cores,
511 hence use a lock */
512static DEFINE_MUTEX(smd_probe_lock);
513
514static void smd_channel_probe_worker(struct work_struct *work)
515{
516 struct smd_alloc_elm *shared;
517 unsigned n;
518 uint32_t type;
519
520 shared = smem_find(ID_CH_ALLOC_TBL, sizeof(*shared) * 64);
521
522 if (!shared) {
523 pr_err("%s: allocation table not initialized\n", __func__);
524 return;
525 }
526
527 mutex_lock(&smd_probe_lock);
528 for (n = 0; n < 64; n++) {
529 if (smd_ch_allocated[n])
530 continue;
531
532 /* channel should be allocated only if APPS
533 processor is involved */
534 type = SMD_CHANNEL_TYPE(shared[n].type);
535 if ((type != SMD_APPS_MODEM) && (type != SMD_APPS_QDSP) &&
536 (type != SMD_APPS_DSPS) && (type != SMD_APPS_WCNSS))
537 continue;
538 if (!shared[n].ref_count)
539 continue;
540 if (!shared[n].name[0])
541 continue;
542
543 if (!smd_alloc_channel(&shared[n]))
544 smd_ch_allocated[n] = 1;
545 else
546 SMD_INFO("Probe skipping ch %d, not allocated\n", n);
547 }
548 mutex_unlock(&smd_probe_lock);
549}
550
551/**
552 * Lookup processor ID and determine if it belongs to the proved edge
553 * type.
554 *
555 * @shared2: Pointer to v2 shared channel structure
556 * @type: Edge type
557 * @pid: Processor ID of processor on edge
558 * @local_ch: Channel that belongs to processor @pid
559 * @remote_ch: Other side of edge contained @pid
560 *
561 * Returns 0 for not on edge, 1 for found on edge
562 */
563static int pid_is_on_edge(struct smd_shared_v2 *shared2,
564 uint32_t type, uint32_t pid,
565 struct smd_half_channel **local_ch,
566 struct smd_half_channel **remote_ch
567 )
568{
569 int ret = 0;
570 struct edge_to_pid *edge;
571
572 *local_ch = 0;
573 *remote_ch = 0;
574
575 if (!shared2 || (type >= ARRAY_SIZE(edge_to_pids)))
576 return 0;
577
578 edge = &edge_to_pids[type];
579 if (edge->local_pid != edge->remote_pid) {
580 if (pid == edge->local_pid) {
581 *local_ch = &shared2->ch0;
582 *remote_ch = &shared2->ch1;
583 ret = 1;
584 } else if (pid == edge->remote_pid) {
585 *local_ch = &shared2->ch1;
586 *remote_ch = &shared2->ch0;
587 ret = 1;
588 }
589 }
590
591 return ret;
592}
593
Eric Holmberg2a563c32011-10-05 14:51:43 -0600594static void smd_reset_edge(struct smd_half_channel *ch, unsigned new_state)
595{
596 if (ch->state != SMD_SS_CLOSED) {
597 ch->state = new_state;
598 ch->fDSR = 0;
599 ch->fCTS = 0;
600 ch->fCD = 0;
601 ch->fSTATE = 1;
602 }
603}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700604
605static void smd_channel_reset_state(struct smd_alloc_elm *shared,
606 unsigned new_state, unsigned pid)
607{
608 unsigned n;
609 struct smd_shared_v2 *shared2;
610 uint32_t type;
611 struct smd_half_channel *local_ch;
612 struct smd_half_channel *remote_ch;
613
614 for (n = 0; n < SMD_CHANNELS; n++) {
615 if (!shared[n].ref_count)
616 continue;
617 if (!shared[n].name[0])
618 continue;
619
620 type = SMD_CHANNEL_TYPE(shared[n].type);
621 shared2 = smem_alloc(SMEM_SMD_BASE_ID + n, sizeof(*shared2));
622 if (!shared2)
623 continue;
624
Eric Holmberg2a563c32011-10-05 14:51:43 -0600625 if (pid_is_on_edge(shared2, type, pid, &local_ch, &remote_ch))
626 smd_reset_edge(local_ch, new_state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700627
Eric Holmberg2a563c32011-10-05 14:51:43 -0600628 /*
629 * ModemFW is in the same subsystem as ModemSW, but has
630 * separate SMD edges that need to be reset.
631 */
632 if (pid == SMSM_MODEM &&
633 pid_is_on_edge(shared2, type, SMD_MODEM_Q6_FW,
634 &local_ch, &remote_ch))
635 smd_reset_edge(local_ch, new_state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700636 }
637}
638
639
640void smd_channel_reset(uint32_t restart_pid)
641{
642 struct smd_alloc_elm *shared;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700643 unsigned long flags;
644
645 SMD_DBG("%s: starting reset\n", __func__);
646 shared = smem_find(ID_CH_ALLOC_TBL, sizeof(*shared) * 64);
647 if (!shared) {
648 pr_err("%s: allocation table not initialized\n", __func__);
649 return;
650 }
651
Eric Holmbergf6d7d1a2011-09-23 18:31:04 -0600652 /* release any held spinlocks */
653 remote_spin_release(&remote_spinlock, restart_pid);
654 remote_spin_release_all(restart_pid);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700655
656 /* reset SMSM entry */
657 if (smsm_info.state) {
658 writel_relaxed(0, SMSM_STATE_ADDR(restart_pid));
659
660 /* clear apps SMSM to restart SMSM init handshake */
661 if (restart_pid == SMSM_MODEM)
662 writel_relaxed(0, SMSM_STATE_ADDR(SMSM_APPS));
663
664 /* notify SMSM processors */
665 smsm_irq_handler(0, 0);
666 MSM_TRIG_A2M_SMSM_INT;
667 MSM_TRIG_A2Q6_SMSM_INT;
Jeff Hugo6a8057c2011-08-16 13:47:12 -0600668 MSM_TRIG_A2DSPS_SMSM_INT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700669 }
670
671 /* change all remote states to CLOSING */
672 mutex_lock(&smd_probe_lock);
673 spin_lock_irqsave(&smd_lock, flags);
674 smd_channel_reset_state(shared, SMD_SS_CLOSING, restart_pid);
675 spin_unlock_irqrestore(&smd_lock, flags);
676 mutex_unlock(&smd_probe_lock);
677
678 /* notify SMD processors */
679 mb();
680 smd_fake_irq_handler(0);
681 notify_modem_smd();
682 notify_dsp_smd();
683 notify_dsps_smd();
684 notify_wcnss_smd();
685
686 /* change all remote states to CLOSED */
687 mutex_lock(&smd_probe_lock);
688 spin_lock_irqsave(&smd_lock, flags);
689 smd_channel_reset_state(shared, SMD_SS_CLOSED, restart_pid);
690 spin_unlock_irqrestore(&smd_lock, flags);
691 mutex_unlock(&smd_probe_lock);
692
693 /* notify SMD processors */
694 mb();
695 smd_fake_irq_handler(0);
696 notify_modem_smd();
697 notify_dsp_smd();
698 notify_dsps_smd();
699 notify_wcnss_smd();
700
701 SMD_DBG("%s: finished reset\n", __func__);
702}
703
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700704/* how many bytes are available for reading */
705static int smd_stream_read_avail(struct smd_channel *ch)
706{
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700707 return (ch->recv->head - ch->recv->tail) & ch->fifo_mask;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700708}
709
710/* how many bytes we are free to write */
711static int smd_stream_write_avail(struct smd_channel *ch)
712{
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700713 return ch->fifo_mask -
714 ((ch->send->head - ch->send->tail) & ch->fifo_mask);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700715}
716
717static int smd_packet_read_avail(struct smd_channel *ch)
718{
719 if (ch->current_packet) {
720 int n = smd_stream_read_avail(ch);
721 if (n > ch->current_packet)
722 n = ch->current_packet;
723 return n;
724 } else {
725 return 0;
726 }
727}
728
729static int smd_packet_write_avail(struct smd_channel *ch)
730{
731 int n = smd_stream_write_avail(ch);
732 return n > SMD_HEADER_SIZE ? n - SMD_HEADER_SIZE : 0;
733}
734
735static int ch_is_open(struct smd_channel *ch)
736{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700737 return (ch->recv->state == SMD_SS_OPENED ||
738 ch->recv->state == SMD_SS_FLUSHING)
739 && (ch->send->state == SMD_SS_OPENED);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700740}
741
742/* provide a pointer and length to readable data in the fifo */
743static unsigned ch_read_buffer(struct smd_channel *ch, void **ptr)
744{
745 unsigned head = ch->recv->head;
746 unsigned tail = ch->recv->tail;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700747 *ptr = (void *) (ch->recv_data + tail);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700748
749 if (tail <= head)
750 return head - tail;
751 else
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700752 return ch->fifo_size - tail;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700753}
754
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700755static int read_intr_blocked(struct smd_channel *ch)
756{
757 return ch->recv->fBLOCKREADINTR;
758}
759
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700760/* advance the fifo read pointer after data from ch_read_buffer is consumed */
761static void ch_read_done(struct smd_channel *ch, unsigned count)
762{
763 BUG_ON(count > smd_stream_read_avail(ch));
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700764 ch->recv->tail = (ch->recv->tail + count) & ch->fifo_mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700765 wmb();
Haley Teng7632fba2009-10-12 10:38:10 -0700766 ch->send->fTAIL = 1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700767}
768
769/* basic read interface to ch_read_{buffer,done} used
Brian Swetland03e00cd2009-07-01 17:58:37 -0700770 * by smd_*_read() and update_packet_state()
771 * will read-and-discard if the _data pointer is null
772 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700773static int ch_read(struct smd_channel *ch, void *_data, int len, int user_buf)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700774{
775 void *ptr;
776 unsigned n;
777 unsigned char *data = _data;
778 int orig_len = len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700779 int r = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700780
781 while (len > 0) {
782 n = ch_read_buffer(ch, &ptr);
783 if (n == 0)
784 break;
785
786 if (n > len)
787 n = len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700788 if (_data) {
789 if (user_buf) {
790 r = copy_to_user(data, ptr, n);
791 if (r > 0) {
792 pr_err("%s: "
793 "copy_to_user could not copy "
794 "%i bytes.\n",
795 __func__,
796 r);
797 }
798 } else
799 memcpy(data, ptr, n);
800 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700801
802 data += n;
803 len -= n;
804 ch_read_done(ch, n);
805 }
806
807 return orig_len - len;
808}
809
810static void update_stream_state(struct smd_channel *ch)
811{
812 /* streams have no special state requiring updating */
813}
814
815static void update_packet_state(struct smd_channel *ch)
816{
817 unsigned hdr[5];
818 int r;
819
820 /* can't do anything if we're in the middle of a packet */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700821 while (ch->current_packet == 0) {
822 /* discard 0 length packets if any */
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700823
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700824 /* don't bother unless we can get the full header */
825 if (smd_stream_read_avail(ch) < SMD_HEADER_SIZE)
826 return;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700827
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700828 r = ch_read(ch, hdr, SMD_HEADER_SIZE, 0);
829 BUG_ON(r != SMD_HEADER_SIZE);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700830
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700831 ch->current_packet = hdr[0];
832 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700833}
834
835/* provide a pointer and length to next free space in the fifo */
836static unsigned ch_write_buffer(struct smd_channel *ch, void **ptr)
837{
838 unsigned head = ch->send->head;
839 unsigned tail = ch->send->tail;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700840 *ptr = (void *) (ch->send_data + head);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700841
842 if (head < tail) {
843 return tail - head - 1;
844 } else {
845 if (tail == 0)
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700846 return ch->fifo_size - head - 1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700847 else
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700848 return ch->fifo_size - head;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700849 }
850}
851
852/* advace the fifo write pointer after freespace
853 * from ch_write_buffer is filled
854 */
855static void ch_write_done(struct smd_channel *ch, unsigned count)
856{
857 BUG_ON(count > smd_stream_write_avail(ch));
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700858 ch->send->head = (ch->send->head + count) & ch->fifo_mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700859 wmb();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700860 ch->send->fHEAD = 1;
861}
862
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700863static void ch_set_state(struct smd_channel *ch, unsigned n)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700864{
865 if (n == SMD_SS_OPENED) {
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700866 ch->send->fDSR = 1;
867 ch->send->fCTS = 1;
868 ch->send->fCD = 1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700869 } else {
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700870 ch->send->fDSR = 0;
871 ch->send->fCTS = 0;
872 ch->send->fCD = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700873 }
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700874 ch->send->state = n;
875 ch->send->fSTATE = 1;
876 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700877}
878
879static void do_smd_probe(void)
880{
881 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
882 if (shared->heap_info.free_offset != last_heap_free) {
883 last_heap_free = shared->heap_info.free_offset;
884 schedule_work(&probe_work);
885 }
886}
887
888static void smd_state_change(struct smd_channel *ch,
889 unsigned last, unsigned next)
890{
891 ch->last_state = next;
892
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700893 SMD_INFO("SMD: ch %d %d -> %d\n", ch->n, last, next);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700894
895 switch (next) {
896 case SMD_SS_OPENING:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700897 if (ch->send->state == SMD_SS_CLOSING ||
898 ch->send->state == SMD_SS_CLOSED) {
899 ch->recv->tail = 0;
900 ch->send->head = 0;
901 ch->send->fBLOCKREADINTR = 0;
902 ch_set_state(ch, SMD_SS_OPENING);
903 }
904 break;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700905 case SMD_SS_OPENED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700906 if (ch->send->state == SMD_SS_OPENING) {
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700907 ch_set_state(ch, SMD_SS_OPENED);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700908 ch->notify(ch->priv, SMD_EVENT_OPEN);
909 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700910 break;
911 case SMD_SS_FLUSHING:
912 case SMD_SS_RESET:
913 /* we should force them to close? */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700914 break;
915 case SMD_SS_CLOSED:
916 if (ch->send->state == SMD_SS_OPENED) {
917 ch_set_state(ch, SMD_SS_CLOSING);
918 ch->current_packet = 0;
919 ch->notify(ch->priv, SMD_EVENT_CLOSE);
920 }
921 break;
922 case SMD_SS_CLOSING:
923 if (ch->send->state == SMD_SS_CLOSED) {
924 list_move(&ch->ch_list,
925 &smd_ch_to_close_list);
926 queue_work(channel_close_wq,
927 &finalize_channel_close_work);
928 }
929 break;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700930 }
931}
932
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700933static void handle_smd_irq_closing_list(void)
934{
935 unsigned long flags;
936 struct smd_channel *ch;
937 struct smd_channel *index;
938 unsigned tmp;
939
940 spin_lock_irqsave(&smd_lock, flags);
941 list_for_each_entry_safe(ch, index, &smd_ch_closing_list, ch_list) {
942 if (ch->recv->fSTATE)
943 ch->recv->fSTATE = 0;
944 tmp = ch->recv->state;
945 if (tmp != ch->last_state)
946 smd_state_change(ch, ch->last_state, tmp);
947 }
948 spin_unlock_irqrestore(&smd_lock, flags);
949}
950
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700951static void handle_smd_irq(struct list_head *list, void (*notify)(void))
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700952{
953 unsigned long flags;
954 struct smd_channel *ch;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700955 unsigned ch_flags;
956 unsigned tmp;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700957 unsigned char state_change;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700958
959 spin_lock_irqsave(&smd_lock, flags);
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700960 list_for_each_entry(ch, list, ch_list) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700961 state_change = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700962 ch_flags = 0;
963 if (ch_is_open(ch)) {
964 if (ch->recv->fHEAD) {
965 ch->recv->fHEAD = 0;
966 ch_flags |= 1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700967 }
968 if (ch->recv->fTAIL) {
969 ch->recv->fTAIL = 0;
970 ch_flags |= 2;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700971 }
972 if (ch->recv->fSTATE) {
973 ch->recv->fSTATE = 0;
974 ch_flags |= 4;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700975 }
976 }
977 tmp = ch->recv->state;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700978 if (tmp != ch->last_state) {
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700979 smd_state_change(ch, ch->last_state, tmp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700980 state_change = 1;
981 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700982 if (ch_flags) {
983 ch->update_state(ch);
984 ch->notify(ch->priv, SMD_EVENT_DATA);
985 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700986 if (ch_flags & 0x4 && !state_change)
987 ch->notify(ch->priv, SMD_EVENT_STATUS);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700988 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700989 spin_unlock_irqrestore(&smd_lock, flags);
990 do_smd_probe();
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700991}
992
Brian Swetland37521a32009-07-01 18:30:47 -0700993static irqreturn_t smd_modem_irq_handler(int irq, void *data)
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700994{
Brian Swetland37521a32009-07-01 18:30:47 -0700995 handle_smd_irq(&smd_ch_list_modem, notify_modem_smd);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700996 handle_smd_irq_closing_list();
Brian Swetland37521a32009-07-01 18:30:47 -0700997 return IRQ_HANDLED;
998}
999
Daniel Walkerb13525c2010-03-18 10:10:30 -07001000#if defined(CONFIG_QDSP6)
Brian Swetland37521a32009-07-01 18:30:47 -07001001static irqreturn_t smd_dsp_irq_handler(int irq, void *data)
1002{
1003 handle_smd_irq(&smd_ch_list_dsp, notify_dsp_smd);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001004 handle_smd_irq_closing_list();
1005 return IRQ_HANDLED;
1006}
1007#endif
1008
1009#if defined(CONFIG_DSPS)
1010static irqreturn_t smd_dsps_irq_handler(int irq, void *data)
1011{
1012 handle_smd_irq(&smd_ch_list_dsps, notify_dsps_smd);
1013 handle_smd_irq_closing_list();
1014 return IRQ_HANDLED;
1015}
1016#endif
1017
1018#if defined(CONFIG_WCNSS)
1019static irqreturn_t smd_wcnss_irq_handler(int irq, void *data)
1020{
1021 handle_smd_irq(&smd_ch_list_wcnss, notify_wcnss_smd);
1022 handle_smd_irq_closing_list();
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001023 return IRQ_HANDLED;
1024}
Daniel Walkerb13525c2010-03-18 10:10:30 -07001025#endif
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001026
1027static void smd_fake_irq_handler(unsigned long arg)
1028{
Brian Swetland37521a32009-07-01 18:30:47 -07001029 handle_smd_irq(&smd_ch_list_modem, notify_modem_smd);
1030 handle_smd_irq(&smd_ch_list_dsp, notify_dsp_smd);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001031 handle_smd_irq(&smd_ch_list_dsps, notify_dsps_smd);
1032 handle_smd_irq(&smd_ch_list_wcnss, notify_wcnss_smd);
1033 handle_smd_irq_closing_list();
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001034}
1035
1036static DECLARE_TASKLET(smd_fake_irq_tasklet, smd_fake_irq_handler, 0);
1037
Brian Swetland37521a32009-07-01 18:30:47 -07001038static inline int smd_need_int(struct smd_channel *ch)
1039{
1040 if (ch_is_open(ch)) {
1041 if (ch->recv->fHEAD || ch->recv->fTAIL || ch->recv->fSTATE)
1042 return 1;
1043 if (ch->recv->state != ch->last_state)
1044 return 1;
1045 }
1046 return 0;
1047}
1048
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001049void smd_sleep_exit(void)
1050{
1051 unsigned long flags;
1052 struct smd_channel *ch;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001053 int need_int = 0;
1054
1055 spin_lock_irqsave(&smd_lock, flags);
Brian Swetland37521a32009-07-01 18:30:47 -07001056 list_for_each_entry(ch, &smd_ch_list_modem, ch_list) {
1057 if (smd_need_int(ch)) {
1058 need_int = 1;
1059 break;
1060 }
1061 }
1062 list_for_each_entry(ch, &smd_ch_list_dsp, ch_list) {
1063 if (smd_need_int(ch)) {
1064 need_int = 1;
1065 break;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001066 }
1067 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001068 list_for_each_entry(ch, &smd_ch_list_dsps, ch_list) {
1069 if (smd_need_int(ch)) {
1070 need_int = 1;
1071 break;
1072 }
1073 }
1074 list_for_each_entry(ch, &smd_ch_list_wcnss, ch_list) {
1075 if (smd_need_int(ch)) {
1076 need_int = 1;
1077 break;
1078 }
1079 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001080 spin_unlock_irqrestore(&smd_lock, flags);
1081 do_smd_probe();
Brian Swetland37521a32009-07-01 18:30:47 -07001082
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001083 if (need_int) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001084 SMD_DBG("smd_sleep_exit need interrupt\n");
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001085 tasklet_schedule(&smd_fake_irq_tasklet);
1086 }
1087}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001088EXPORT_SYMBOL(smd_sleep_exit);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001089
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001090static int smd_is_packet(struct smd_alloc_elm *alloc_elm)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001091{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001092 if (SMD_XFER_TYPE(alloc_elm->type) == 1)
1093 return 0;
1094 else if (SMD_XFER_TYPE(alloc_elm->type) == 2)
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001095 return 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001096
1097 /* for cases where xfer type is 0 */
1098 if (!strncmp(alloc_elm->name, "DAL", 3))
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001099 return 0;
1100
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001101 /* for cases where xfer type is 0 */
1102 if (!strncmp(alloc_elm->name, "RPCCALL_QDSP", 12))
1103 return 0;
1104
1105 if (alloc_elm->cid > 4 || alloc_elm->cid == 1)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001106 return 1;
1107 else
1108 return 0;
1109}
1110
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001111static int smd_stream_write(smd_channel_t *ch, const void *_data, int len,
1112 int user_buf)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001113{
1114 void *ptr;
1115 const unsigned char *buf = _data;
1116 unsigned xfer;
1117 int orig_len = len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001118 int r = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001119
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001120 SMD_DBG("smd_stream_write() %d -> ch%d\n", len, ch->n);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001121 if (len < 0)
1122 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001123 else if (len == 0)
1124 return 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001125
1126 while ((xfer = ch_write_buffer(ch, &ptr)) != 0) {
1127 if (!ch_is_open(ch))
1128 break;
1129 if (xfer > len)
1130 xfer = len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001131 if (user_buf) {
1132 r = copy_from_user(ptr, buf, xfer);
1133 if (r > 0) {
1134 pr_err("%s: "
1135 "copy_from_user could not copy %i "
1136 "bytes.\n",
1137 __func__,
1138 r);
1139 }
1140 } else
1141 memcpy(ptr, buf, xfer);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001142 ch_write_done(ch, xfer);
1143 len -= xfer;
1144 buf += xfer;
1145 if (len == 0)
1146 break;
1147 }
1148
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001149 if (orig_len - len)
1150 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001151
1152 return orig_len - len;
1153}
1154
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001155static int smd_packet_write(smd_channel_t *ch, const void *_data, int len,
1156 int user_buf)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001157{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001158 int ret;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001159 unsigned hdr[5];
1160
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001161 SMD_DBG("smd_packet_write() %d -> ch%d\n", len, ch->n);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001162 if (len < 0)
1163 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001164 else if (len == 0)
1165 return 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001166
1167 if (smd_stream_write_avail(ch) < (len + SMD_HEADER_SIZE))
1168 return -ENOMEM;
1169
1170 hdr[0] = len;
1171 hdr[1] = hdr[2] = hdr[3] = hdr[4] = 0;
1172
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001173
1174 ret = smd_stream_write(ch, hdr, sizeof(hdr), 0);
1175 if (ret < 0 || ret != sizeof(hdr)) {
1176 SMD_DBG("%s failed to write pkt header: "
1177 "%d returned\n", __func__, ret);
1178 return -1;
1179 }
1180
1181
1182 ret = smd_stream_write(ch, _data, len, user_buf);
1183 if (ret < 0 || ret != len) {
1184 SMD_DBG("%s failed to write pkt data: "
1185 "%d returned\n", __func__, ret);
1186 return ret;
1187 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001188
1189 return len;
1190}
1191
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001192static int smd_stream_read(smd_channel_t *ch, void *data, int len, int user_buf)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001193{
1194 int r;
1195
1196 if (len < 0)
1197 return -EINVAL;
1198
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001199 r = ch_read(ch, data, len, user_buf);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001200 if (r > 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001201 if (!read_intr_blocked(ch))
1202 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001203
1204 return r;
1205}
1206
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001207static int smd_packet_read(smd_channel_t *ch, void *data, int len, int user_buf)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001208{
1209 unsigned long flags;
1210 int r;
1211
1212 if (len < 0)
1213 return -EINVAL;
1214
1215 if (len > ch->current_packet)
1216 len = ch->current_packet;
1217
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001218 r = ch_read(ch, data, len, user_buf);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001219 if (r > 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001220 if (!read_intr_blocked(ch))
1221 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001222
1223 spin_lock_irqsave(&smd_lock, flags);
1224 ch->current_packet -= r;
1225 update_packet_state(ch);
1226 spin_unlock_irqrestore(&smd_lock, flags);
1227
1228 return r;
1229}
1230
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001231static int smd_packet_read_from_cb(smd_channel_t *ch, void *data, int len,
1232 int user_buf)
1233{
1234 int r;
1235
1236 if (len < 0)
1237 return -EINVAL;
1238
1239 if (len > ch->current_packet)
1240 len = ch->current_packet;
1241
1242 r = ch_read(ch, data, len, user_buf);
1243 if (r > 0)
1244 if (!read_intr_blocked(ch))
1245 ch->notify_other_cpu();
1246
1247 ch->current_packet -= r;
1248 update_packet_state(ch);
1249
1250 return r;
1251}
1252
1253static int smd_alloc_v2(struct smd_channel *ch)
1254{
1255 struct smd_shared_v2 *shared2;
1256 void *buffer;
1257 unsigned buffer_sz;
1258
1259 shared2 = smem_alloc(SMEM_SMD_BASE_ID + ch->n, sizeof(*shared2));
1260 if (!shared2) {
1261 SMD_INFO("smem_alloc failed ch=%d\n", ch->n);
1262 return -1;
1263 }
1264 buffer = smem_get_entry(SMEM_SMD_FIFO_BASE_ID + ch->n, &buffer_sz);
1265 if (!buffer) {
1266 SMD_INFO("smem_get_entry failed \n");
1267 return -1;
1268 }
1269
1270 /* buffer must be a power-of-two size */
1271 if (buffer_sz & (buffer_sz - 1))
1272 return -1;
1273
1274 buffer_sz /= 2;
1275 ch->send = &shared2->ch0;
1276 ch->recv = &shared2->ch1;
1277 ch->send_data = buffer;
1278 ch->recv_data = buffer + buffer_sz;
1279 ch->fifo_size = buffer_sz;
1280 return 0;
1281}
1282
1283static int smd_alloc_v1(struct smd_channel *ch)
1284{
1285 struct smd_shared_v1 *shared1;
1286 shared1 = smem_alloc(ID_SMD_CHANNELS + ch->n, sizeof(*shared1));
1287 if (!shared1) {
1288 pr_err("smd_alloc_channel() cid %d does not exist\n", ch->n);
1289 return -1;
1290 }
1291 ch->send = &shared1->ch0;
1292 ch->recv = &shared1->ch1;
1293 ch->send_data = shared1->data0;
1294 ch->recv_data = shared1->data1;
1295 ch->fifo_size = SMD_BUF_SIZE;
1296 return 0;
1297}
1298
1299static int smd_alloc_channel(struct smd_alloc_elm *alloc_elm)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001300{
1301 struct smd_channel *ch;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001302
1303 ch = kzalloc(sizeof(struct smd_channel), GFP_KERNEL);
1304 if (ch == 0) {
1305 pr_err("smd_alloc_channel() out of memory\n");
Brian Swetland34f719b2009-10-30 16:22:05 -07001306 return -1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001307 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001308 ch->n = alloc_elm->cid;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001309
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001310 if (smd_alloc_v2(ch) && smd_alloc_v1(ch)) {
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001311 kfree(ch);
Brian Swetland34f719b2009-10-30 16:22:05 -07001312 return -1;
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001313 }
1314
1315 ch->fifo_mask = ch->fifo_size - 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001316 ch->type = SMD_CHANNEL_TYPE(alloc_elm->type);
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001317
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001318 if (ch->type == SMD_APPS_MODEM)
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001319 ch->notify_other_cpu = notify_modem_smd;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001320 else if (ch->type == SMD_APPS_QDSP)
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001321 ch->notify_other_cpu = notify_dsp_smd;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001322 else if (ch->type == SMD_APPS_DSPS)
1323 ch->notify_other_cpu = notify_dsps_smd;
1324 else
1325 ch->notify_other_cpu = notify_wcnss_smd;
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001326
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001327 if (smd_is_packet(alloc_elm)) {
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001328 ch->read = smd_packet_read;
1329 ch->write = smd_packet_write;
1330 ch->read_avail = smd_packet_read_avail;
1331 ch->write_avail = smd_packet_write_avail;
1332 ch->update_state = update_packet_state;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001333 ch->read_from_cb = smd_packet_read_from_cb;
1334 ch->is_pkt_ch = 1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001335 } else {
1336 ch->read = smd_stream_read;
1337 ch->write = smd_stream_write;
1338 ch->read_avail = smd_stream_read_avail;
1339 ch->write_avail = smd_stream_write_avail;
1340 ch->update_state = update_stream_state;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001341 ch->read_from_cb = smd_stream_read;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001342 }
1343
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001344 memcpy(ch->name, alloc_elm->name, SMD_MAX_CH_NAME_LEN);
1345 ch->name[SMD_MAX_CH_NAME_LEN-1] = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001346
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001347 ch->pdev.name = ch->name;
1348 ch->pdev.id = ch->type;
1349
1350 SMD_INFO("smd_alloc_channel() '%s' cid=%d\n",
1351 ch->name, ch->n);
1352
1353 mutex_lock(&smd_creation_mutex);
1354 list_add(&ch->ch_list, &smd_ch_closed_list);
1355 mutex_unlock(&smd_creation_mutex);
1356
1357 platform_device_register(&ch->pdev);
1358 if (!strncmp(ch->name, "LOOPBACK", 8) && ch->type == SMD_APPS_MODEM) {
1359 /* create a platform driver to be used by smd_tty driver
1360 * so that it can access the loopback port
1361 */
1362 loopback_tty_pdev.id = ch->type;
1363 platform_device_register(&loopback_tty_pdev);
1364 }
1365 return 0;
1366}
1367
1368static inline void notify_loopback_smd(void)
1369{
1370 unsigned long flags;
1371 struct smd_channel *ch;
1372
1373 spin_lock_irqsave(&smd_lock, flags);
1374 list_for_each_entry(ch, &smd_ch_list_loopback, ch_list) {
1375 ch->notify(ch->priv, SMD_EVENT_DATA);
1376 }
1377 spin_unlock_irqrestore(&smd_lock, flags);
1378}
1379
1380static int smd_alloc_loopback_channel(void)
1381{
1382 static struct smd_half_channel smd_loopback_ctl;
1383 static char smd_loopback_data[SMD_BUF_SIZE];
1384 struct smd_channel *ch;
1385
1386 ch = kzalloc(sizeof(struct smd_channel), GFP_KERNEL);
1387 if (ch == 0) {
1388 pr_err("%s: out of memory\n", __func__);
1389 return -1;
1390 }
1391 ch->n = SMD_LOOPBACK_CID;
1392
1393 ch->send = &smd_loopback_ctl;
1394 ch->recv = &smd_loopback_ctl;
1395 ch->send_data = smd_loopback_data;
1396 ch->recv_data = smd_loopback_data;
1397 ch->fifo_size = SMD_BUF_SIZE;
1398
1399 ch->fifo_mask = ch->fifo_size - 1;
1400 ch->type = SMD_LOOPBACK_TYPE;
1401 ch->notify_other_cpu = notify_loopback_smd;
1402
1403 ch->read = smd_stream_read;
1404 ch->write = smd_stream_write;
1405 ch->read_avail = smd_stream_read_avail;
1406 ch->write_avail = smd_stream_write_avail;
1407 ch->update_state = update_stream_state;
1408 ch->read_from_cb = smd_stream_read;
1409
1410 memset(ch->name, 0, 20);
1411 memcpy(ch->name, "local_loopback", 14);
1412
1413 ch->pdev.name = ch->name;
1414 ch->pdev.id = ch->type;
1415
1416 SMD_INFO("%s: '%s' cid=%d\n", __func__, ch->name, ch->n);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001417
1418 mutex_lock(&smd_creation_mutex);
1419 list_add(&ch->ch_list, &smd_ch_closed_list);
1420 mutex_unlock(&smd_creation_mutex);
1421
1422 platform_device_register(&ch->pdev);
Brian Swetland34f719b2009-10-30 16:22:05 -07001423 return 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001424}
1425
1426static void do_nothing_notify(void *priv, unsigned flags)
1427{
1428}
1429
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001430static void finalize_channel_close_fn(struct work_struct *work)
1431{
1432 unsigned long flags;
1433 struct smd_channel *ch;
1434 struct smd_channel *index;
1435
1436 spin_lock_irqsave(&smd_lock, flags);
1437 list_for_each_entry_safe(ch, index, &smd_ch_to_close_list, ch_list) {
1438 list_del(&ch->ch_list);
1439 spin_unlock_irqrestore(&smd_lock, flags);
1440 mutex_lock(&smd_creation_mutex);
1441 list_add(&ch->ch_list, &smd_ch_closed_list);
1442 mutex_unlock(&smd_creation_mutex);
1443 ch->notify(ch->priv, SMD_EVENT_REOPEN_READY);
1444 ch->notify = do_nothing_notify;
1445 spin_lock_irqsave(&smd_lock, flags);
1446 }
1447 spin_unlock_irqrestore(&smd_lock, flags);
1448}
1449
1450struct smd_channel *smd_get_channel(const char *name, uint32_t type)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001451{
1452 struct smd_channel *ch;
1453
1454 mutex_lock(&smd_creation_mutex);
1455 list_for_each_entry(ch, &smd_ch_closed_list, ch_list) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001456 if (!strcmp(name, ch->name) &&
1457 (type == ch->type)) {
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001458 list_del(&ch->ch_list);
1459 mutex_unlock(&smd_creation_mutex);
1460 return ch;
1461 }
1462 }
1463 mutex_unlock(&smd_creation_mutex);
1464
1465 return NULL;
1466}
1467
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001468int smd_named_open_on_edge(const char *name, uint32_t edge,
1469 smd_channel_t **_ch,
1470 void *priv, void (*notify)(void *, unsigned))
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001471{
1472 struct smd_channel *ch;
1473 unsigned long flags;
1474
1475 if (smd_initialized == 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001476 SMD_INFO("smd_open() before smd_init()\n");
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001477 return -ENODEV;
1478 }
1479
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001480 SMD_DBG("smd_open('%s', %p, %p)\n", name, priv, notify);
1481
1482 ch = smd_get_channel(name, edge);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001483 if (!ch)
1484 return -ENODEV;
1485
1486 if (notify == 0)
1487 notify = do_nothing_notify;
1488
1489 ch->notify = notify;
1490 ch->current_packet = 0;
1491 ch->last_state = SMD_SS_CLOSED;
1492 ch->priv = priv;
1493
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001494 if (edge == SMD_LOOPBACK_TYPE) {
1495 ch->last_state = SMD_SS_OPENED;
1496 ch->send->state = SMD_SS_OPENED;
1497 ch->send->fDSR = 1;
1498 ch->send->fCTS = 1;
1499 ch->send->fCD = 1;
1500 }
1501
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001502 *_ch = ch;
1503
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001504 SMD_DBG("smd_open: opening '%s'\n", ch->name);
1505
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001506 spin_lock_irqsave(&smd_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001507 if (SMD_CHANNEL_TYPE(ch->type) == SMD_APPS_MODEM)
Brian Swetland37521a32009-07-01 18:30:47 -07001508 list_add(&ch->ch_list, &smd_ch_list_modem);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001509 else if (SMD_CHANNEL_TYPE(ch->type) == SMD_APPS_QDSP)
Brian Swetland37521a32009-07-01 18:30:47 -07001510 list_add(&ch->ch_list, &smd_ch_list_dsp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001511 else if (SMD_CHANNEL_TYPE(ch->type) == SMD_APPS_DSPS)
1512 list_add(&ch->ch_list, &smd_ch_list_dsps);
1513 else if (SMD_CHANNEL_TYPE(ch->type) == SMD_APPS_WCNSS)
1514 list_add(&ch->ch_list, &smd_ch_list_wcnss);
1515 else
1516 list_add(&ch->ch_list, &smd_ch_list_loopback);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001517
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001518 SMD_DBG("%s: opening ch %d\n", __func__, ch->n);
1519
1520 if (edge != SMD_LOOPBACK_TYPE)
1521 smd_state_change(ch, ch->last_state, SMD_SS_OPENING);
1522
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001523 spin_unlock_irqrestore(&smd_lock, flags);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001524
1525 return 0;
1526}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001527EXPORT_SYMBOL(smd_named_open_on_edge);
1528
1529
1530int smd_open(const char *name, smd_channel_t **_ch,
1531 void *priv, void (*notify)(void *, unsigned))
1532{
1533 return smd_named_open_on_edge(name, SMD_APPS_MODEM, _ch, priv,
1534 notify);
1535}
1536EXPORT_SYMBOL(smd_open);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001537
1538int smd_close(smd_channel_t *ch)
1539{
1540 unsigned long flags;
1541
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001542 if (ch == 0)
1543 return -1;
1544
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001545 SMD_INFO("smd_close(%s)\n", ch->name);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001546
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001547 spin_lock_irqsave(&smd_lock, flags);
1548 list_del(&ch->ch_list);
1549 if (ch->n == SMD_LOOPBACK_CID) {
1550 ch->send->fDSR = 0;
1551 ch->send->fCTS = 0;
1552 ch->send->fCD = 0;
1553 ch->send->state = SMD_SS_CLOSED;
1554 } else
1555 ch_set_state(ch, SMD_SS_CLOSED);
1556
1557 if (ch->recv->state == SMD_SS_OPENED) {
1558 list_add(&ch->ch_list, &smd_ch_closing_list);
1559 spin_unlock_irqrestore(&smd_lock, flags);
1560 } else {
1561 spin_unlock_irqrestore(&smd_lock, flags);
1562 ch->notify = do_nothing_notify;
1563 mutex_lock(&smd_creation_mutex);
1564 list_add(&ch->ch_list, &smd_ch_closed_list);
1565 mutex_unlock(&smd_creation_mutex);
1566 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001567
1568 return 0;
1569}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001570EXPORT_SYMBOL(smd_close);
1571
1572int smd_write_start(smd_channel_t *ch, int len)
1573{
1574 int ret;
1575 unsigned hdr[5];
1576
1577 if (!ch) {
1578 pr_err("%s: Invalid channel specified\n", __func__);
1579 return -ENODEV;
1580 }
1581 if (!ch->is_pkt_ch) {
1582 pr_err("%s: non-packet channel specified\n", __func__);
1583 return -EACCES;
1584 }
1585 if (len < 1) {
1586 pr_err("%s: invalid length: %d\n", __func__, len);
1587 return -EINVAL;
1588 }
1589
1590 if (ch->pending_pkt_sz) {
1591 pr_err("%s: packet of size: %d in progress\n", __func__,
1592 ch->pending_pkt_sz);
1593 return -EBUSY;
1594 }
1595 ch->pending_pkt_sz = len;
1596
1597 if (smd_stream_write_avail(ch) < (SMD_HEADER_SIZE)) {
1598 ch->pending_pkt_sz = 0;
1599 SMD_DBG("%s: no space to write packet header\n", __func__);
1600 return -EAGAIN;
1601 }
1602
1603 hdr[0] = len;
1604 hdr[1] = hdr[2] = hdr[3] = hdr[4] = 0;
1605
1606
1607 ret = smd_stream_write(ch, hdr, sizeof(hdr), 0);
1608 if (ret < 0 || ret != sizeof(hdr)) {
1609 ch->pending_pkt_sz = 0;
1610 pr_err("%s: packet header failed to write\n", __func__);
1611 return -EPERM;
1612 }
1613 return 0;
1614}
1615EXPORT_SYMBOL(smd_write_start);
1616
1617int smd_write_segment(smd_channel_t *ch, void *data, int len, int user_buf)
1618{
1619 int bytes_written;
1620
1621 if (!ch) {
1622 pr_err("%s: Invalid channel specified\n", __func__);
1623 return -ENODEV;
1624 }
1625 if (len < 1) {
1626 pr_err("%s: invalid length: %d\n", __func__, len);
1627 return -EINVAL;
1628 }
1629
1630 if (!ch->pending_pkt_sz) {
1631 pr_err("%s: no transaction in progress\n", __func__);
1632 return -ENOEXEC;
1633 }
1634 if (ch->pending_pkt_sz - len < 0) {
1635 pr_err("%s: segment of size: %d will make packet go over "
1636 "length\n", __func__, len);
1637 return -EINVAL;
1638 }
1639
1640 bytes_written = smd_stream_write(ch, data, len, user_buf);
1641
1642 ch->pending_pkt_sz -= bytes_written;
1643
1644 return bytes_written;
1645}
1646EXPORT_SYMBOL(smd_write_segment);
1647
1648int smd_write_end(smd_channel_t *ch)
1649{
1650
1651 if (!ch) {
1652 pr_err("%s: Invalid channel specified\n", __func__);
1653 return -ENODEV;
1654 }
1655 if (ch->pending_pkt_sz) {
1656 pr_err("%s: current packet not completely written\n", __func__);
1657 return -E2BIG;
1658 }
1659
1660 return 0;
1661}
1662EXPORT_SYMBOL(smd_write_end);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001663
1664int smd_read(smd_channel_t *ch, void *data, int len)
1665{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001666 return ch->read(ch, data, len, 0);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001667}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001668EXPORT_SYMBOL(smd_read);
1669
1670int smd_read_user_buffer(smd_channel_t *ch, void *data, int len)
1671{
1672 return ch->read(ch, data, len, 1);
1673}
1674EXPORT_SYMBOL(smd_read_user_buffer);
1675
1676int smd_read_from_cb(smd_channel_t *ch, void *data, int len)
1677{
1678 return ch->read_from_cb(ch, data, len, 0);
1679}
1680EXPORT_SYMBOL(smd_read_from_cb);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001681
1682int smd_write(smd_channel_t *ch, const void *data, int len)
1683{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001684 return ch->pending_pkt_sz ? -EBUSY : ch->write(ch, data, len, 0);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001685}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001686EXPORT_SYMBOL(smd_write);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001687
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001688int smd_write_user_buffer(smd_channel_t *ch, const void *data, int len)
Brian Swetland636eb9c2009-12-07 15:28:08 -08001689{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001690 return ch->pending_pkt_sz ? -EBUSY : ch->write(ch, data, len, 1);
Brian Swetland636eb9c2009-12-07 15:28:08 -08001691}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001692EXPORT_SYMBOL(smd_write_user_buffer);
Brian Swetland636eb9c2009-12-07 15:28:08 -08001693
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001694int smd_read_avail(smd_channel_t *ch)
1695{
1696 return ch->read_avail(ch);
1697}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001698EXPORT_SYMBOL(smd_read_avail);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001699
1700int smd_write_avail(smd_channel_t *ch)
1701{
1702 return ch->write_avail(ch);
1703}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001704EXPORT_SYMBOL(smd_write_avail);
1705
1706void smd_enable_read_intr(smd_channel_t *ch)
1707{
1708 if (ch)
1709 ch->send->fBLOCKREADINTR = 0;
1710}
1711EXPORT_SYMBOL(smd_enable_read_intr);
1712
1713void smd_disable_read_intr(smd_channel_t *ch)
1714{
1715 if (ch)
1716 ch->send->fBLOCKREADINTR = 1;
1717}
1718EXPORT_SYMBOL(smd_disable_read_intr);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001719
1720int smd_wait_until_readable(smd_channel_t *ch, int bytes)
1721{
1722 return -1;
1723}
1724
1725int smd_wait_until_writable(smd_channel_t *ch, int bytes)
1726{
1727 return -1;
1728}
1729
1730int smd_cur_packet_size(smd_channel_t *ch)
1731{
1732 return ch->current_packet;
1733}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001734EXPORT_SYMBOL(smd_cur_packet_size);
1735
1736int smd_tiocmget(smd_channel_t *ch)
1737{
1738 return (ch->recv->fDSR ? TIOCM_DSR : 0) |
1739 (ch->recv->fCTS ? TIOCM_CTS : 0) |
1740 (ch->recv->fCD ? TIOCM_CD : 0) |
1741 (ch->recv->fRI ? TIOCM_RI : 0) |
1742 (ch->send->fCTS ? TIOCM_RTS : 0) |
1743 (ch->send->fDSR ? TIOCM_DTR : 0);
1744}
1745EXPORT_SYMBOL(smd_tiocmget);
1746
Vamsi Krishnacb12a102011-08-17 15:18:26 -07001747/* this api will be called while holding smd_lock */
1748int
1749smd_tiocmset_from_cb(smd_channel_t *ch, unsigned int set, unsigned int clear)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001750{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001751 if (set & TIOCM_DTR)
1752 ch->send->fDSR = 1;
1753
1754 if (set & TIOCM_RTS)
1755 ch->send->fCTS = 1;
1756
1757 if (clear & TIOCM_DTR)
1758 ch->send->fDSR = 0;
1759
1760 if (clear & TIOCM_RTS)
1761 ch->send->fCTS = 0;
1762
1763 ch->send->fSTATE = 1;
1764 barrier();
1765 ch->notify_other_cpu();
Vamsi Krishnacb12a102011-08-17 15:18:26 -07001766
1767 return 0;
1768}
1769EXPORT_SYMBOL(smd_tiocmset_from_cb);
1770
1771int smd_tiocmset(smd_channel_t *ch, unsigned int set, unsigned int clear)
1772{
1773 unsigned long flags;
1774
1775 spin_lock_irqsave(&smd_lock, flags);
1776 smd_tiocmset_from_cb(ch, set, clear);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001777 spin_unlock_irqrestore(&smd_lock, flags);
1778
1779 return 0;
1780}
1781EXPORT_SYMBOL(smd_tiocmset);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001782
1783
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001784/* -------------------------------------------------------------------------- */
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001785
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001786/* smem_alloc returns the pointer to smem item if it is already allocated.
1787 * Otherwise, it returns NULL.
1788 */
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001789void *smem_alloc(unsigned id, unsigned size)
1790{
1791 return smem_find(id, size);
1792}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001793EXPORT_SYMBOL(smem_alloc);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001794
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001795/* smem_alloc2 returns the pointer to smem item. If it is not allocated,
1796 * it allocates it and then returns the pointer to it.
1797 */
Angshuman Sarkar4eade0d2011-08-17 14:06:23 +05301798void *smem_alloc2(unsigned id, unsigned size_in)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001799{
1800 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
1801 struct smem_heap_entry *toc = shared->heap_toc;
1802 unsigned long flags;
1803 void *ret = NULL;
1804
1805 if (!shared->heap_info.initialized) {
1806 pr_err("%s: smem heap info not initialized\n", __func__);
1807 return NULL;
1808 }
1809
1810 if (id >= SMEM_NUM_ITEMS)
1811 return NULL;
1812
1813 size_in = ALIGN(size_in, 8);
1814 remote_spin_lock_irqsave(&remote_spinlock, flags);
1815 if (toc[id].allocated) {
1816 SMD_DBG("%s: %u already allocated\n", __func__, id);
1817 if (size_in != toc[id].size)
1818 pr_err("%s: wrong size %u (expected %u)\n",
1819 __func__, toc[id].size, size_in);
1820 else
1821 ret = (void *)(MSM_SHARED_RAM_BASE + toc[id].offset);
1822 } else if (id > SMEM_FIXED_ITEM_LAST) {
1823 SMD_DBG("%s: allocating %u\n", __func__, id);
1824 if (shared->heap_info.heap_remaining >= size_in) {
1825 toc[id].offset = shared->heap_info.free_offset;
1826 toc[id].size = size_in;
1827 wmb();
1828 toc[id].allocated = 1;
1829
1830 shared->heap_info.free_offset += size_in;
1831 shared->heap_info.heap_remaining -= size_in;
1832 ret = (void *)(MSM_SHARED_RAM_BASE + toc[id].offset);
1833 } else
1834 pr_err("%s: not enough memory %u (required %u)\n",
1835 __func__, shared->heap_info.heap_remaining,
1836 size_in);
1837 }
1838 wmb();
1839 remote_spin_unlock_irqrestore(&remote_spinlock, flags);
1840 return ret;
1841}
Angshuman Sarkar4eade0d2011-08-17 14:06:23 +05301842EXPORT_SYMBOL(smem_alloc2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001843
1844void *smem_get_entry(unsigned id, unsigned *size)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001845{
1846 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
1847 struct smem_heap_entry *toc = shared->heap_toc;
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +05301848 int use_spinlocks = spinlocks_initialized;
1849 void *ret = 0;
1850 unsigned long flags = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001851
1852 if (id >= SMEM_NUM_ITEMS)
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +05301853 return ret;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001854
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +05301855 if (use_spinlocks)
1856 remote_spin_lock_irqsave(&remote_spinlock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001857 /* toc is in device memory and cannot be speculatively accessed */
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001858 if (toc[id].allocated) {
1859 *size = toc[id].size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001860 barrier();
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +05301861 ret = (void *) (MSM_SHARED_RAM_BASE + toc[id].offset);
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001862 } else {
1863 *size = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001864 }
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +05301865 if (use_spinlocks)
1866 remote_spin_unlock_irqrestore(&remote_spinlock, flags);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001867
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +05301868 return ret;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001869}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001870EXPORT_SYMBOL(smem_get_entry);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001871
1872void *smem_find(unsigned id, unsigned size_in)
1873{
1874 unsigned size;
1875 void *ptr;
1876
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001877 ptr = smem_get_entry(id, &size);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001878 if (!ptr)
1879 return 0;
1880
1881 size_in = ALIGN(size_in, 8);
1882 if (size_in != size) {
1883 pr_err("smem_find(%d, %d): wrong size %d\n",
1884 id, size_in, size);
1885 return 0;
1886 }
1887
1888 return ptr;
1889}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001890EXPORT_SYMBOL(smem_find);
1891
1892static int smsm_cb_init(void)
1893{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001894 struct smsm_state_info *state_info;
1895 int n;
1896 int ret = 0;
1897
1898 smsm_states = kmalloc(sizeof(struct smsm_state_info)*SMSM_NUM_ENTRIES,
1899 GFP_KERNEL);
1900
1901 if (!smsm_states) {
1902 pr_err("%s: SMSM init failed\n", __func__);
1903 return -ENOMEM;
1904 }
1905
Eric Holmbergc8002902011-09-16 13:55:57 -06001906 mutex_lock(&smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001907 for (n = 0; n < SMSM_NUM_ENTRIES; n++) {
1908 state_info = &smsm_states[n];
1909 state_info->last_value = __raw_readl(SMSM_STATE_ADDR(n));
1910 INIT_LIST_HEAD(&state_info->callbacks);
1911 }
Eric Holmbergc8002902011-09-16 13:55:57 -06001912 mutex_unlock(&smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001913
1914 return ret;
1915}
1916
1917static int smsm_init(void)
1918{
1919 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
1920 int i;
1921 struct smsm_size_info_type *smsm_size_info;
1922
1923 i = remote_spin_lock_init(&remote_spinlock, SMEM_SPINLOCK_SMEM_ALLOC);
1924 if (i) {
1925 pr_err("%s: remote spinlock init failed %d\n", __func__, i);
1926 return i;
1927 }
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +05301928 spinlocks_initialized = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001929
1930 smsm_size_info = smem_alloc(SMEM_SMSM_SIZE_INFO,
1931 sizeof(struct smsm_size_info_type));
1932 if (smsm_size_info) {
1933 SMSM_NUM_ENTRIES = smsm_size_info->num_entries;
1934 SMSM_NUM_HOSTS = smsm_size_info->num_hosts;
1935 }
1936
1937 if (!smsm_info.state) {
1938 smsm_info.state = smem_alloc2(ID_SHARED_STATE,
1939 SMSM_NUM_ENTRIES *
1940 sizeof(uint32_t));
1941
1942 if (smsm_info.state) {
1943 __raw_writel(0, SMSM_STATE_ADDR(SMSM_APPS_STATE));
1944 if ((shared->version[VERSION_MODEM] >> 16) >= 0xB)
1945 __raw_writel(0, \
1946 SMSM_STATE_ADDR(SMSM_APPS_DEM_I));
1947 }
1948 }
1949
1950 if (!smsm_info.intr_mask) {
1951 smsm_info.intr_mask = smem_alloc2(SMEM_SMSM_CPU_INTR_MASK,
1952 SMSM_NUM_ENTRIES *
1953 SMSM_NUM_HOSTS *
1954 sizeof(uint32_t));
1955
1956 if (smsm_info.intr_mask)
1957 for (i = 0; i < SMSM_NUM_ENTRIES; i++)
1958 __raw_writel(0xffffffff,
1959 SMSM_INTR_MASK_ADDR(i, SMSM_APPS));
1960 }
1961
1962 if (!smsm_info.intr_mux)
1963 smsm_info.intr_mux = smem_alloc2(SMEM_SMD_SMSM_INTR_MUX,
1964 SMSM_NUM_INTR_MUX *
1965 sizeof(uint32_t));
1966
1967 i = smsm_cb_init();
1968 if (i)
1969 return i;
1970
1971 wmb();
1972 return 0;
1973}
1974
1975void smsm_reset_modem(unsigned mode)
1976{
1977 if (mode == SMSM_SYSTEM_DOWNLOAD) {
1978 mode = SMSM_RESET | SMSM_SYSTEM_DOWNLOAD;
1979 } else if (mode == SMSM_MODEM_WAIT) {
1980 mode = SMSM_RESET | SMSM_MODEM_WAIT;
1981 } else { /* reset_mode is SMSM_RESET or default */
1982 mode = SMSM_RESET;
1983 }
1984
1985 smsm_change_state(SMSM_APPS_STATE, mode, mode);
1986}
1987EXPORT_SYMBOL(smsm_reset_modem);
1988
1989void smsm_reset_modem_cont(void)
1990{
1991 unsigned long flags;
1992 uint32_t state;
1993
1994 if (!smsm_info.state)
1995 return;
1996
1997 spin_lock_irqsave(&smem_lock, flags);
1998 state = __raw_readl(SMSM_STATE_ADDR(SMSM_APPS_STATE)) \
1999 & ~SMSM_MODEM_WAIT;
2000 __raw_writel(state, SMSM_STATE_ADDR(SMSM_APPS_STATE));
2001 wmb();
2002 spin_unlock_irqrestore(&smem_lock, flags);
2003}
2004EXPORT_SYMBOL(smsm_reset_modem_cont);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002005
2006static irqreturn_t smsm_irq_handler(int irq, void *data)
2007{
2008 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002009
2010#if !defined(CONFIG_ARCH_MSM8X60)
2011 uint32_t mux_val;
2012 static uint32_t prev_smem_q6_apps_smsm;
2013
2014 if (irq == INT_ADSP_A11_SMSM) {
2015 if (!smsm_info.intr_mux)
2016 return IRQ_HANDLED;
2017 mux_val = __raw_readl(SMSM_INTR_MUX_ADDR(SMEM_Q6_APPS_SMSM));
2018 if (mux_val != prev_smem_q6_apps_smsm)
2019 prev_smem_q6_apps_smsm = mux_val;
2020 return IRQ_HANDLED;
2021 }
2022#else
2023 if (irq == INT_ADSP_A11_SMSM)
2024 return IRQ_HANDLED;
2025#endif
2026
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002027
2028 spin_lock_irqsave(&smem_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002029 if (!smsm_info.state) {
2030 SMSM_INFO("<SM NO STATE>\n");
2031 } else {
2032 unsigned old_apps, apps;
2033 unsigned modm = __raw_readl(SMSM_STATE_ADDR(SMSM_MODEM_STATE));
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002034
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002035 old_apps = apps = __raw_readl(SMSM_STATE_ADDR(SMSM_APPS_STATE));
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002036
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002037 SMSM_DBG("<SM %08x %08x>\n", apps, modm);
2038 if (apps & SMSM_RESET) {
2039 /* If we get an interrupt and the apps SMSM_RESET
2040 bit is already set, the modem is acking the
2041 app's reset ack. */
Angshuman Sarkaread67bd2011-09-21 20:13:12 +05302042 if (!cpu_is_msm8960())
2043 apps &= ~SMSM_RESET;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002044 /* Issue a fake irq to handle any
2045 * smd state changes during reset
2046 */
2047 smd_fake_irq_handler(0);
Brian Swetland5b0f5a32009-04-26 18:38:49 -07002048
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002049 /* queue modem restart notify chain */
2050 modem_queue_start_reset_notify();
2051
2052 } else if (modm & SMSM_RESET) {
Angshuman Sarkaread67bd2011-09-21 20:13:12 +05302053 if (!cpu_is_msm8960())
2054 apps |= SMSM_RESET;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002055
2056 pr_err("\nSMSM: Modem SMSM state changed to SMSM_RESET.");
2057 modem_queue_start_reset_notify();
2058
2059 } else if (modm & SMSM_INIT) {
2060 if (!(apps & SMSM_INIT)) {
2061 apps |= SMSM_INIT;
2062 modem_queue_smsm_init_notify();
2063 }
2064
2065 if (modm & SMSM_SMDINIT)
2066 apps |= SMSM_SMDINIT;
2067 if ((apps & (SMSM_INIT | SMSM_SMDINIT | SMSM_RPCINIT)) ==
2068 (SMSM_INIT | SMSM_SMDINIT | SMSM_RPCINIT))
2069 apps |= SMSM_RUN;
2070 } else if (modm & SMSM_SYSTEM_DOWNLOAD) {
2071 pr_err("\nSMSM: Modem SMSM state changed to SMSM_SYSTEM_DOWNLOAD.");
2072 modem_queue_start_reset_notify();
2073 }
2074
2075 if (old_apps != apps) {
2076 SMSM_DBG("<SM %08x NOTIFY>\n", apps);
2077 __raw_writel(apps, SMSM_STATE_ADDR(SMSM_APPS_STATE));
2078 do_smd_probe();
2079 notify_other_smsm(SMSM_APPS_STATE, (old_apps ^ apps));
2080 }
2081
2082 schedule_work(&smsm_cb_work);
2083 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002084 spin_unlock_irqrestore(&smem_lock, flags);
2085 return IRQ_HANDLED;
2086}
2087
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002088int smsm_change_intr_mask(uint32_t smsm_entry,
2089 uint32_t clear_mask, uint32_t set_mask)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002090{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002091 uint32_t old_mask, new_mask;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002092 unsigned long flags;
Brian Swetland5b0f5a32009-04-26 18:38:49 -07002093
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002094 if (smsm_entry >= SMSM_NUM_ENTRIES) {
2095 pr_err("smsm_change_state: Invalid entry %d\n",
2096 smsm_entry);
2097 return -EINVAL;
2098 }
2099
2100 if (!smsm_info.intr_mask) {
2101 pr_err("smsm_change_intr_mask <SM NO STATE>\n");
Brian Swetland5b0f5a32009-04-26 18:38:49 -07002102 return -EIO;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002103 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002104
2105 spin_lock_irqsave(&smem_lock, flags);
2106
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002107 old_mask = __raw_readl(SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_APPS));
2108 new_mask = (old_mask & ~clear_mask) | set_mask;
2109 __raw_writel(new_mask, SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_APPS));
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002110
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002111 wmb();
2112 spin_unlock_irqrestore(&smem_lock, flags);
Brian Swetland5b0f5a32009-04-26 18:38:49 -07002113
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002114 return 0;
2115}
2116EXPORT_SYMBOL(smsm_change_intr_mask);
2117
2118int smsm_get_intr_mask(uint32_t smsm_entry, uint32_t *intr_mask)
2119{
2120 if (smsm_entry >= SMSM_NUM_ENTRIES) {
2121 pr_err("smsm_change_state: Invalid entry %d\n",
2122 smsm_entry);
2123 return -EINVAL;
2124 }
2125
2126 if (!smsm_info.intr_mask) {
2127 pr_err("smsm_change_intr_mask <SM NO STATE>\n");
2128 return -EIO;
2129 }
2130
2131 *intr_mask = __raw_readl(SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_APPS));
2132 return 0;
2133}
2134EXPORT_SYMBOL(smsm_get_intr_mask);
2135
2136int smsm_change_state(uint32_t smsm_entry,
2137 uint32_t clear_mask, uint32_t set_mask)
2138{
2139 unsigned long flags;
2140 uint32_t old_state, new_state;
2141
2142 if (smsm_entry >= SMSM_NUM_ENTRIES) {
2143 pr_err("smsm_change_state: Invalid entry %d",
2144 smsm_entry);
2145 return -EINVAL;
2146 }
2147
2148 if (!smsm_info.state) {
2149 pr_err("smsm_change_state <SM NO STATE>\n");
2150 return -EIO;
2151 }
2152 spin_lock_irqsave(&smem_lock, flags);
2153
2154 old_state = __raw_readl(SMSM_STATE_ADDR(smsm_entry));
2155 new_state = (old_state & ~clear_mask) | set_mask;
2156 __raw_writel(new_state, SMSM_STATE_ADDR(smsm_entry));
2157 SMSM_DBG("smsm_change_state %x\n", new_state);
2158 notify_other_smsm(SMSM_APPS_STATE, (old_state ^ new_state));
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002159
2160 spin_unlock_irqrestore(&smem_lock, flags);
2161
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002162 return 0;
2163}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002164EXPORT_SYMBOL(smsm_change_state);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002165
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002166uint32_t smsm_get_state(uint32_t smsm_entry)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002167{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002168 uint32_t rv = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002169
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002170 /* needs interface change to return error code */
2171 if (smsm_entry >= SMSM_NUM_ENTRIES) {
2172 pr_err("smsm_change_state: Invalid entry %d",
2173 smsm_entry);
2174 return 0;
2175 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002176
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002177 if (!smsm_info.state) {
2178 pr_err("smsm_get_state <SM NO STATE>\n");
2179 } else {
2180 rv = __raw_readl(SMSM_STATE_ADDR(smsm_entry));
2181 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002182
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002183 return rv;
2184}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002185EXPORT_SYMBOL(smsm_get_state);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002186
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002187/**
2188 * Performs SMSM callback client notifiction.
2189 */
2190void notify_smsm_cb_clients_worker(struct work_struct *work)
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -07002191{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002192 struct smsm_state_cb_info *cb_info;
2193 struct smsm_state_info *state_info;
2194 int n;
2195 uint32_t new_state;
2196 uint32_t state_changes;
Brian Swetland03e00cd2009-07-01 17:58:37 -07002197
Eric Holmbergc8002902011-09-16 13:55:57 -06002198 mutex_lock(&smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002199
2200 if (!smsm_states) {
2201 /* smsm not yet initialized */
Eric Holmbergc8002902011-09-16 13:55:57 -06002202 mutex_unlock(&smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002203 return;
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -07002204 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002205
2206 for (n = 0; n < SMSM_NUM_ENTRIES; n++) {
2207 state_info = &smsm_states[n];
2208 new_state = __raw_readl(SMSM_STATE_ADDR(n));
2209
2210 if (new_state != state_info->last_value) {
2211 state_changes = state_info->last_value ^ new_state;
2212
2213 list_for_each_entry(cb_info,
2214 &state_info->callbacks, cb_list) {
2215
2216 if (cb_info->mask & state_changes)
2217 cb_info->notify(cb_info->data,
2218 state_info->last_value,
2219 new_state);
2220 }
2221 state_info->last_value = new_state;
2222 }
2223 }
2224
Eric Holmbergc8002902011-09-16 13:55:57 -06002225 mutex_unlock(&smsm_lock);
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -07002226}
2227
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -07002228
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002229/**
2230 * Registers callback for SMSM state notifications when the specified
2231 * bits change.
2232 *
2233 * @smsm_entry Processor entry to deregister
2234 * @mask Bits to deregister (if result is 0, callback is removed)
2235 * @notify Notification function to deregister
2236 * @data Opaque data passed in to callback
2237 *
2238 * @returns Status code
2239 * <0 error code
2240 * 0 inserted new entry
2241 * 1 updated mask of existing entry
2242 */
2243int smsm_state_cb_register(uint32_t smsm_entry, uint32_t mask,
2244 void (*notify)(void *, uint32_t, uint32_t), void *data)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002245{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002246 struct smsm_state_cb_info *cb_info;
2247 struct smsm_state_cb_info *cb_found = 0;
2248 int ret = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002249
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002250 if (smsm_entry >= SMSM_NUM_ENTRIES)
2251 return -EINVAL;
2252
Eric Holmbergc8002902011-09-16 13:55:57 -06002253 mutex_lock(&smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002254
2255 if (!smsm_states) {
2256 /* smsm not yet initialized */
2257 ret = -ENODEV;
2258 goto cleanup;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002259 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002260
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002261 list_for_each_entry(cb_info,
2262 &smsm_states[smsm_entry].callbacks, cb_list) {
2263 if ((cb_info->notify == notify) &&
2264 (cb_info->data == data)) {
2265 cb_info->mask |= mask;
2266 cb_found = cb_info;
2267 ret = 1;
Brian Swetland5b0f5a32009-04-26 18:38:49 -07002268 break;
2269 }
2270 }
2271
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002272 if (!cb_found) {
2273 cb_info = kmalloc(sizeof(struct smsm_state_cb_info),
2274 GFP_ATOMIC);
2275 if (!cb_info) {
2276 ret = -ENOMEM;
2277 goto cleanup;
2278 }
2279
2280 cb_info->mask = mask;
2281 cb_info->notify = notify;
2282 cb_info->data = data;
2283 INIT_LIST_HEAD(&cb_info->cb_list);
2284 list_add_tail(&cb_info->cb_list,
2285 &smsm_states[smsm_entry].callbacks);
2286 }
2287
2288cleanup:
Eric Holmbergc8002902011-09-16 13:55:57 -06002289 mutex_unlock(&smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002290 return ret;
2291}
2292EXPORT_SYMBOL(smsm_state_cb_register);
2293
2294
2295/**
2296 * Deregisters for SMSM state notifications for the specified bits.
2297 *
2298 * @smsm_entry Processor entry to deregister
2299 * @mask Bits to deregister (if result is 0, callback is removed)
2300 * @notify Notification function to deregister
2301 * @data Opaque data passed in to callback
2302 *
2303 * @returns Status code
2304 * <0 error code
2305 * 0 not found
2306 * 1 updated mask
2307 * 2 removed callback
2308 */
2309int smsm_state_cb_deregister(uint32_t smsm_entry, uint32_t mask,
2310 void (*notify)(void *, uint32_t, uint32_t), void *data)
2311{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002312 struct smsm_state_cb_info *cb_info;
2313 int ret = 0;
2314
2315 if (smsm_entry >= SMSM_NUM_ENTRIES)
2316 return -EINVAL;
2317
Eric Holmbergc8002902011-09-16 13:55:57 -06002318 mutex_lock(&smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002319
2320 if (!smsm_states) {
2321 /* smsm not yet initialized */
Eric Holmbergc8002902011-09-16 13:55:57 -06002322 mutex_unlock(&smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002323 return -ENODEV;
2324 }
2325
2326 list_for_each_entry(cb_info,
2327 &smsm_states[smsm_entry].callbacks, cb_list) {
2328 if ((cb_info->notify == notify) &&
2329 (cb_info->data == data)) {
2330 cb_info->mask &= ~mask;
2331 ret = 1;
2332 if (!cb_info->mask) {
2333 /* no mask bits set, remove callback */
2334 list_del(&cb_info->cb_list);
2335 kfree(cb_info);
2336 ret = 2;
2337 }
2338 break;
2339 }
2340 }
2341
Eric Holmbergc8002902011-09-16 13:55:57 -06002342 mutex_unlock(&smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002343 return ret;
2344}
2345EXPORT_SYMBOL(smsm_state_cb_deregister);
2346
2347
2348int smd_core_init(void)
2349{
2350 int r;
2351 unsigned long flags = IRQF_TRIGGER_RISING;
2352 SMD_INFO("smd_core_init()\n");
Brian Swetland5b0f5a32009-04-26 18:38:49 -07002353
Brian Swetland37521a32009-07-01 18:30:47 -07002354 r = request_irq(INT_A9_M2A_0, smd_modem_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002355 flags, "smd_dev", 0);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002356 if (r < 0)
2357 return r;
2358 r = enable_irq_wake(INT_A9_M2A_0);
2359 if (r < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002360 pr_err("smd_core_init: "
2361 "enable_irq_wake failed for INT_A9_M2A_0\n");
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002362
2363 r = request_irq(INT_A9_M2A_5, smsm_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002364 flags, "smsm_dev", 0);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002365 if (r < 0) {
2366 free_irq(INT_A9_M2A_0, 0);
2367 return r;
2368 }
2369 r = enable_irq_wake(INT_A9_M2A_5);
2370 if (r < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002371 pr_err("smd_core_init: "
2372 "enable_irq_wake failed for INT_A9_M2A_5\n");
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002373
Brian Swetland37521a32009-07-01 18:30:47 -07002374#if defined(CONFIG_QDSP6)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002375#if (INT_ADSP_A11 == INT_ADSP_A11_SMSM)
2376 flags |= IRQF_SHARED;
2377#endif
Brian Swetland37521a32009-07-01 18:30:47 -07002378 r = request_irq(INT_ADSP_A11, smd_dsp_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002379 flags, "smd_dev", smd_dsp_irq_handler);
Brian Swetland37521a32009-07-01 18:30:47 -07002380 if (r < 0) {
2381 free_irq(INT_A9_M2A_0, 0);
2382 free_irq(INT_A9_M2A_5, 0);
2383 return r;
2384 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002385
2386 r = request_irq(INT_ADSP_A11_SMSM, smsm_irq_handler,
2387 flags, "smsm_dev", smsm_irq_handler);
2388 if (r < 0) {
2389 free_irq(INT_A9_M2A_0, 0);
2390 free_irq(INT_A9_M2A_5, 0);
2391 free_irq(INT_ADSP_A11, smd_dsp_irq_handler);
2392 return r;
2393 }
2394
2395 r = enable_irq_wake(INT_ADSP_A11);
2396 if (r < 0)
2397 pr_err("smd_core_init: "
2398 "enable_irq_wake failed for INT_ADSP_A11\n");
2399
2400#if (INT_ADSP_A11 != INT_ADSP_A11_SMSM)
2401 r = enable_irq_wake(INT_ADSP_A11_SMSM);
2402 if (r < 0)
2403 pr_err("smd_core_init: enable_irq_wake "
2404 "failed for INT_ADSP_A11_SMSM\n");
2405#endif
2406 flags &= ~IRQF_SHARED;
Brian Swetland37521a32009-07-01 18:30:47 -07002407#endif
2408
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002409#if defined(CONFIG_DSPS)
2410 r = request_irq(INT_DSPS_A11, smd_dsps_irq_handler,
2411 flags, "smd_dev", smd_dsps_irq_handler);
2412 if (r < 0) {
2413 free_irq(INT_A9_M2A_0, 0);
2414 free_irq(INT_A9_M2A_5, 0);
2415 free_irq(INT_ADSP_A11, smd_dsp_irq_handler);
2416 free_irq(INT_ADSP_A11_SMSM, smsm_irq_handler);
2417 return r;
2418 }
Brian Swetland5b0f5a32009-04-26 18:38:49 -07002419
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002420 r = enable_irq_wake(INT_DSPS_A11);
2421 if (r < 0)
2422 pr_err("smd_core_init: "
2423 "enable_irq_wake failed for INT_ADSP_A11\n");
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -07002424#endif
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002425
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002426#if defined(CONFIG_WCNSS)
2427 r = request_irq(INT_WCNSS_A11, smd_wcnss_irq_handler,
2428 flags, "smd_dev", smd_wcnss_irq_handler);
2429 if (r < 0) {
2430 free_irq(INT_A9_M2A_0, 0);
2431 free_irq(INT_A9_M2A_5, 0);
2432 free_irq(INT_ADSP_A11, smd_dsp_irq_handler);
2433 free_irq(INT_ADSP_A11_SMSM, smsm_irq_handler);
2434 free_irq(INT_DSPS_A11, smd_dsps_irq_handler);
2435 return r;
2436 }
2437
2438 r = enable_irq_wake(INT_WCNSS_A11);
2439 if (r < 0)
2440 pr_err("smd_core_init: "
2441 "enable_irq_wake failed for INT_WCNSS_A11\n");
2442
2443 r = request_irq(INT_WCNSS_A11_SMSM, smsm_irq_handler,
2444 flags, "smsm_dev", smsm_irq_handler);
2445 if (r < 0) {
2446 free_irq(INT_A9_M2A_0, 0);
2447 free_irq(INT_A9_M2A_5, 0);
2448 free_irq(INT_ADSP_A11, smd_dsp_irq_handler);
2449 free_irq(INT_ADSP_A11_SMSM, smsm_irq_handler);
2450 free_irq(INT_DSPS_A11, smd_dsps_irq_handler);
2451 free_irq(INT_WCNSS_A11, smd_wcnss_irq_handler);
2452 return r;
2453 }
2454
2455 r = enable_irq_wake(INT_WCNSS_A11_SMSM);
2456 if (r < 0)
2457 pr_err("smd_core_init: "
2458 "enable_irq_wake failed for INT_WCNSS_A11_SMSM\n");
2459#endif
2460
Jeff Hugo6a8057c2011-08-16 13:47:12 -06002461#if defined(CONFIG_DSPS_SMSM)
2462 r = request_irq(INT_DSPS_A11_SMSM, smsm_irq_handler,
2463 flags, "smsm_dev", smsm_irq_handler);
2464 if (r < 0) {
2465 free_irq(INT_A9_M2A_0, 0);
2466 free_irq(INT_A9_M2A_5, 0);
2467 free_irq(INT_ADSP_A11, smd_dsp_irq_handler);
2468 free_irq(INT_ADSP_A11_SMSM, smsm_irq_handler);
2469 free_irq(INT_DSPS_A11, smd_dsps_irq_handler);
2470 free_irq(INT_WCNSS_A11, smd_wcnss_irq_handler);
2471 free_irq(INT_WCNSS_A11_SMSM, smsm_irq_handler);
2472 return r;
2473 }
2474
2475 r = enable_irq_wake(INT_DSPS_A11_SMSM);
2476 if (r < 0)
2477 pr_err("smd_core_init: "
2478 "enable_irq_wake failed for INT_DSPS_A11_SMSM\n");
2479#endif
2480
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002481 /* we may have missed a signal while booting -- fake
2482 * an interrupt to make sure we process any existing
2483 * state
2484 */
2485 smsm_irq_handler(0, 0);
2486
2487 SMD_INFO("smd_core_init() done\n");
2488
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002489 return 0;
2490}
2491
Gregory Bean4416e9e2010-07-28 10:22:12 -07002492static int __devinit msm_smd_probe(struct platform_device *pdev)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002493{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002494 SMD_INFO("smd probe\n");
Daniel Walker0aec66d2010-03-18 12:31:08 -07002495
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002496 INIT_WORK(&probe_work, smd_channel_probe_worker);
2497
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002498 channel_close_wq = create_singlethread_workqueue("smd_channel_close");
2499 if (IS_ERR(channel_close_wq)) {
2500 pr_err("%s: create_singlethread_workqueue ENOMEM\n", __func__);
2501 return -ENOMEM;
2502 }
2503
2504 if (smsm_init()) {
2505 pr_err("smsm_init() failed\n");
2506 return -1;
2507 }
2508
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002509 if (smd_core_init()) {
2510 pr_err("smd_core_init() failed\n");
2511 return -1;
2512 }
2513
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002514 smd_initialized = 1;
2515
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002516 smd_alloc_loopback_channel();
2517
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002518 return 0;
2519}
2520
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002521static int restart_notifier_cb(struct notifier_block *this,
2522 unsigned long code,
2523 void *data);
2524
2525static struct restart_notifier_block restart_notifiers[] = {
2526 {SMSM_MODEM, "modem", .nb.notifier_call = restart_notifier_cb},
2527 {SMSM_Q6, "lpass", .nb.notifier_call = restart_notifier_cb},
2528};
2529
2530static int restart_notifier_cb(struct notifier_block *this,
2531 unsigned long code,
2532 void *data)
2533{
2534 if (code == SUBSYS_AFTER_SHUTDOWN) {
2535 struct restart_notifier_block *notifier;
2536
2537 notifier = container_of(this,
2538 struct restart_notifier_block, nb);
2539 SMD_INFO("%s: ssrestart for processor %d ('%s')\n",
2540 __func__, notifier->processor,
2541 notifier->name);
2542
2543 smd_channel_reset(notifier->processor);
2544 }
2545
2546 return NOTIFY_DONE;
2547}
2548
2549static __init int modem_restart_late_init(void)
2550{
2551 int i;
2552 void *handle;
2553 struct restart_notifier_block *nb;
2554
2555 for (i = 0; i < ARRAY_SIZE(restart_notifiers); i++) {
2556 nb = &restart_notifiers[i];
2557 handle = subsys_notif_register_notifier(nb->name, &nb->nb);
2558 SMD_DBG("%s: registering notif for '%s', handle=%p\n",
2559 __func__, nb->name, handle);
2560 }
2561 return 0;
2562}
2563late_initcall(modem_restart_late_init);
2564
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002565static struct platform_driver msm_smd_driver = {
2566 .probe = msm_smd_probe,
2567 .driver = {
2568 .name = MODULE_NAME,
2569 .owner = THIS_MODULE,
2570 },
2571};
2572
2573static int __init msm_smd_init(void)
2574{
2575 return platform_driver_register(&msm_smd_driver);
2576}
2577
2578module_init(msm_smd_init);
2579
2580MODULE_DESCRIPTION("MSM Shared Memory Core");
2581MODULE_AUTHOR("Brian Swetland <swetland@google.com>");
2582MODULE_LICENSE("GPL");