blob: 691dad290f96ebc819cf836f243978bb6c351fe7 [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) \
Jeff Hugo0c0f5e92011-09-28 13:55:45 -060046 || defined(CONFIG_ARCH_MSM9615) || defined(CONFIG_ARCH_APQ8064)
Brian Swetland37521a32009-07-01 18:30:47 -070047#define CONFIG_QDSP6 1
48#endif
49
Jeff Hugo0c0f5e92011-09-28 13:55:45 -060050#if defined(CONFIG_ARCH_MSM8X60) || defined(CONFIG_ARCH_MSM8960) \
51 || defined(CONFIG_ARCH_APQ8064)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070052#define CONFIG_DSPS 1
53#endif
54
Jeff Hugo0c0f5e92011-09-28 13:55:45 -060055#if defined(CONFIG_ARCH_MSM8960) \
56 || defined(CONFIG_ARCH_APQ8064)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070057#define CONFIG_WCNSS 1
Jeff Hugo6a8057c2011-08-16 13:47:12 -060058#define CONFIG_DSPS_SMSM 1
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070059#endif
Brian Swetland2eb44eb2008-09-29 16:00:48 -070060
61#define MODULE_NAME "msm_smd"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070062#define SMEM_VERSION 0x000B
63#define SMD_VERSION 0x00020000
64
65uint32_t SMSM_NUM_ENTRIES = 8;
66uint32_t SMSM_NUM_HOSTS = 3;
Brian Swetland2eb44eb2008-09-29 16:00:48 -070067
68enum {
69 MSM_SMD_DEBUG = 1U << 0,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070070 MSM_SMSM_DEBUG = 1U << 1,
71 MSM_SMD_INFO = 1U << 2,
72 MSM_SMSM_INFO = 1U << 3,
73};
74
75struct smsm_shared_info {
76 uint32_t *state;
77 uint32_t *intr_mask;
78 uint32_t *intr_mux;
79};
80
81static struct smsm_shared_info smsm_info;
82
83struct smsm_size_info_type {
84 uint32_t num_hosts;
85 uint32_t num_entries;
86 uint32_t reserved0;
87 uint32_t reserved1;
88};
89
90struct smsm_state_cb_info {
91 struct list_head cb_list;
92 uint32_t mask;
93 void *data;
94 void (*notify)(void *data, uint32_t old_state, uint32_t new_state);
95};
96
97struct smsm_state_info {
98 struct list_head callbacks;
99 uint32_t last_value;
100};
101
102#define SMSM_STATE_ADDR(entry) (smsm_info.state + entry)
103#define SMSM_INTR_MASK_ADDR(entry, host) (smsm_info.intr_mask + \
104 entry * SMSM_NUM_HOSTS + host)
105#define SMSM_INTR_MUX_ADDR(entry) (smsm_info.intr_mux + entry)
106
107/* Internal definitions which are not exported in some targets */
108enum {
109 SMSM_APPS_DEM_I = 3,
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700110};
111
112static int msm_smd_debug_mask;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700113module_param_named(debug_mask, msm_smd_debug_mask,
114 int, S_IRUGO | S_IWUSR | S_IWGRP);
115
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700116#if defined(CONFIG_MSM_SMD_DEBUG)
117#define SMD_DBG(x...) do { \
118 if (msm_smd_debug_mask & MSM_SMD_DEBUG) \
119 printk(KERN_DEBUG x); \
120 } while (0)
121
122#define SMSM_DBG(x...) do { \
123 if (msm_smd_debug_mask & MSM_SMSM_DEBUG) \
124 printk(KERN_DEBUG x); \
125 } while (0)
126
127#define SMD_INFO(x...) do { \
128 if (msm_smd_debug_mask & MSM_SMD_INFO) \
129 printk(KERN_INFO x); \
130 } while (0)
131
132#define SMSM_INFO(x...) do { \
133 if (msm_smd_debug_mask & MSM_SMSM_INFO) \
134 printk(KERN_INFO x); \
135 } while (0)
136#else
137#define SMD_DBG(x...) do { } while (0)
138#define SMSM_DBG(x...) do { } while (0)
139#define SMD_INFO(x...) do { } while (0)
140#define SMSM_INFO(x...) do { } while (0)
141#endif
142
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700143static unsigned last_heap_free = 0xffffffff;
144
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700145static inline void smd_write_intr(unsigned int val,
146 const void __iomem *addr);
147
148#if defined(CONFIG_ARCH_MSM7X30)
149#define MSM_TRIG_A2M_SMD_INT \
150 (smd_write_intr(1 << 0, MSM_GCC_BASE + 0x8))
151#define MSM_TRIG_A2Q6_SMD_INT \
152 (smd_write_intr(1 << 8, MSM_GCC_BASE + 0x8))
153#define MSM_TRIG_A2M_SMSM_INT \
154 (smd_write_intr(1 << 5, MSM_GCC_BASE + 0x8))
155#define MSM_TRIG_A2Q6_SMSM_INT \
156 (smd_write_intr(1 << 8, MSM_GCC_BASE + 0x8))
157#define MSM_TRIG_A2DSPS_SMD_INT
Jeff Hugo6a8057c2011-08-16 13:47:12 -0600158#define MSM_TRIG_A2DSPS_SMSM_INT
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700159#define MSM_TRIG_A2WCNSS_SMD_INT
160#define MSM_TRIG_A2WCNSS_SMSM_INT
161#elif defined(CONFIG_ARCH_MSM8X60)
162#define MSM_TRIG_A2M_SMD_INT \
163 (smd_write_intr(1 << 3, MSM_GCC_BASE + 0x8))
164#define MSM_TRIG_A2Q6_SMD_INT \
165 (smd_write_intr(1 << 15, MSM_GCC_BASE + 0x8))
166#define MSM_TRIG_A2M_SMSM_INT \
167 (smd_write_intr(1 << 4, MSM_GCC_BASE + 0x8))
168#define MSM_TRIG_A2Q6_SMSM_INT \
169 (smd_write_intr(1 << 14, MSM_GCC_BASE + 0x8))
170#define MSM_TRIG_A2DSPS_SMD_INT \
171 (smd_write_intr(1, MSM_SIC_NON_SECURE_BASE + 0x4080))
Jeff Hugo6a8057c2011-08-16 13:47:12 -0600172#define MSM_TRIG_A2DSPS_SMSM_INT
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700173#define MSM_TRIG_A2WCNSS_SMD_INT
174#define MSM_TRIG_A2WCNSS_SMSM_INT
Stepan Moskovchenko0dd0eba2011-10-20 18:20:59 -0700175#elif defined(CONFIG_ARCH_MSM8960) || defined(CONFIG_ARCH_MSM8930) || \
176 defined(CONFIG_ARCH_APQ8064)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700177#define MSM_TRIG_A2M_SMD_INT \
178 (smd_write_intr(1 << 3, MSM_APCS_GCC_BASE + 0x8))
179#define MSM_TRIG_A2Q6_SMD_INT \
180 (smd_write_intr(1 << 15, MSM_APCS_GCC_BASE + 0x8))
181#define MSM_TRIG_A2M_SMSM_INT \
182 (smd_write_intr(1 << 4, MSM_APCS_GCC_BASE + 0x8))
183#define MSM_TRIG_A2Q6_SMSM_INT \
184 (smd_write_intr(1 << 14, MSM_APCS_GCC_BASE + 0x8))
185#define MSM_TRIG_A2DSPS_SMD_INT \
186 (smd_write_intr(1, MSM_SIC_NON_SECURE_BASE + 0x4080))
Jeff Hugo6a8057c2011-08-16 13:47:12 -0600187#define MSM_TRIG_A2DSPS_SMSM_INT \
188 (smd_write_intr(1, MSM_SIC_NON_SECURE_BASE + 0x4094))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700189#define MSM_TRIG_A2WCNSS_SMD_INT \
190 (smd_write_intr(1 << 25, MSM_APCS_GCC_BASE + 0x8))
191#define MSM_TRIG_A2WCNSS_SMSM_INT \
192 (smd_write_intr(1 << 23, MSM_APCS_GCC_BASE + 0x8))
Jeff Hugo56b933a2011-09-28 14:42:05 -0600193#elif defined(CONFIG_ARCH_MSM9615)
194#define MSM_TRIG_A2M_SMD_INT \
195 (smd_write_intr(1 << 3, MSM_APCS_GCC_BASE + 0x8))
196#define MSM_TRIG_A2Q6_SMD_INT \
197 (smd_write_intr(1 << 15, MSM_APCS_GCC_BASE + 0x8))
198#define MSM_TRIG_A2M_SMSM_INT \
199 (smd_write_intr(1 << 4, MSM_APCS_GCC_BASE + 0x8))
200#define MSM_TRIG_A2Q6_SMSM_INT \
201 (smd_write_intr(1 << 14, MSM_APCS_GCC_BASE + 0x8))
202#define MSM_TRIG_A2DSPS_SMD_INT
203#define MSM_TRIG_A2DSPS_SMSM_INT
204#define MSM_TRIG_A2WCNSS_SMD_INT
205#define MSM_TRIG_A2WCNSS_SMSM_INT
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700206#elif defined(CONFIG_ARCH_FSM9XXX)
207#define MSM_TRIG_A2Q6_SMD_INT \
208 (smd_write_intr(1 << 10, MSM_GCC_BASE + 0x8))
209#define MSM_TRIG_A2Q6_SMSM_INT \
210 (smd_write_intr(1 << 10, MSM_GCC_BASE + 0x8))
211#define MSM_TRIG_A2M_SMD_INT \
212 (smd_write_intr(1 << 0, MSM_GCC_BASE + 0x8))
213#define MSM_TRIG_A2M_SMSM_INT \
214 (smd_write_intr(1 << 5, MSM_GCC_BASE + 0x8))
215#define MSM_TRIG_A2DSPS_SMD_INT
Jeff Hugo6a8057c2011-08-16 13:47:12 -0600216#define MSM_TRIG_A2DSPS_SMSM_INT
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700217#define MSM_TRIG_A2WCNSS_SMD_INT
218#define MSM_TRIG_A2WCNSS_SMSM_INT
219#else
220#define MSM_TRIG_A2M_SMD_INT \
221 (smd_write_intr(1, MSM_CSR_BASE + 0x400 + (0) * 4))
222#define MSM_TRIG_A2Q6_SMD_INT \
223 (smd_write_intr(1, MSM_CSR_BASE + 0x400 + (8) * 4))
224#define MSM_TRIG_A2M_SMSM_INT \
225 (smd_write_intr(1, MSM_CSR_BASE + 0x400 + (5) * 4))
226#define MSM_TRIG_A2Q6_SMSM_INT \
227 (smd_write_intr(1, MSM_CSR_BASE + 0x400 + (8) * 4))
228#define MSM_TRIG_A2DSPS_SMD_INT
Jeff Hugo6a8057c2011-08-16 13:47:12 -0600229#define MSM_TRIG_A2DSPS_SMSM_INT
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700230#define MSM_TRIG_A2WCNSS_SMD_INT
231#define MSM_TRIG_A2WCNSS_SMSM_INT
Brian Swetland37521a32009-07-01 18:30:47 -0700232#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700233
234#define SMD_LOOPBACK_CID 100
235
Eric Holmbergf6d7d1a2011-09-23 18:31:04 -0600236#define SMEM_SPINLOCK_SMEM_ALLOC "S:3"
237static remote_spinlock_t remote_spinlock;
238
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700239static LIST_HEAD(smd_ch_list_loopback);
240static irqreturn_t smsm_irq_handler(int irq, void *data);
241static void smd_fake_irq_handler(unsigned long arg);
242
243static void notify_smsm_cb_clients_worker(struct work_struct *work);
244static DECLARE_WORK(smsm_cb_work, notify_smsm_cb_clients_worker);
Eric Holmbergc8002902011-09-16 13:55:57 -0600245static DEFINE_MUTEX(smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700246static struct smsm_state_info *smsm_states;
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +0530247static int spinlocks_initialized;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700248
249static inline void smd_write_intr(unsigned int val,
250 const void __iomem *addr)
251{
252 wmb();
253 __raw_writel(val, addr);
254}
255
256#ifdef CONFIG_WCNSS
257static inline void wakeup_v1_riva(void)
258{
259 /*
260 * workaround hack for RIVA v1 hardware bug
261 * trigger GPIO 40 to wake up RIVA from power collaspe
262 * not to be sent to customers
263 */
Jeff Hugoafb8c4a2011-10-27 15:57:27 -0600264 if (SOCINFO_VERSION_MAJOR(socinfo_get_version()) == 1) {
265 __raw_writel(0x0, MSM_TLMM_BASE + 0x1284);
266 __raw_writel(0x2, MSM_TLMM_BASE + 0x1284);
267 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700268 /* end workaround */
269}
270#else
271static inline void wakeup_v1_riva(void) {}
272#endif
273
274static void notify_other_smsm(uint32_t smsm_entry, uint32_t notify_mask)
275{
276 /* older protocol don't use smsm_intr_mask,
277 but still communicates with modem */
278 if (!smsm_info.intr_mask ||
279 (__raw_readl(SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_MODEM))
280 & notify_mask))
281 MSM_TRIG_A2M_SMSM_INT;
282
283 if (smsm_info.intr_mask &&
284 (__raw_readl(SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_Q6))
285 & notify_mask)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700286 uint32_t mux_val;
287
Eric Holmberg6282c5d2011-10-27 17:30:57 -0600288 if (cpu_is_qsd8x50() && smsm_info.intr_mux) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700289 mux_val = __raw_readl(
290 SMSM_INTR_MUX_ADDR(SMEM_APPS_Q6_SMSM));
291 mux_val++;
292 __raw_writel(mux_val,
293 SMSM_INTR_MUX_ADDR(SMEM_APPS_Q6_SMSM));
294 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700295 MSM_TRIG_A2Q6_SMSM_INT;
296 }
297
298 if (smsm_info.intr_mask &&
299 (__raw_readl(SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_WCNSS))
300 & notify_mask)) {
301 wakeup_v1_riva();
302 MSM_TRIG_A2WCNSS_SMSM_INT;
303 }
304
Jeff Hugo6a8057c2011-08-16 13:47:12 -0600305 if (smsm_info.intr_mask &&
306 (__raw_readl(SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_DSPS))
307 & notify_mask)) {
308 MSM_TRIG_A2DSPS_SMSM_INT;
309 }
310
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700311 schedule_work(&smsm_cb_work);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700312}
313
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700314static inline void notify_modem_smd(void)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700315{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700316 MSM_TRIG_A2M_SMD_INT;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700317}
318
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700319static inline void notify_dsp_smd(void)
320{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700321 MSM_TRIG_A2Q6_SMD_INT;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700322}
323
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700324static inline void notify_dsps_smd(void)
325{
326 MSM_TRIG_A2DSPS_SMD_INT;
327}
328
329static inline void notify_wcnss_smd(void)
330{
331 wakeup_v1_riva();
332 MSM_TRIG_A2WCNSS_SMD_INT;
333}
334
335void smd_diag(void)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700336{
337 char *x;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700338 int size;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700339
340 x = smem_find(ID_DIAG_ERR_MSG, SZ_DIAG_ERR_MSG);
341 if (x != 0) {
342 x[SZ_DIAG_ERR_MSG - 1] = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700343 SMD_INFO("smem: DIAG '%s'\n", x);
344 }
345
346 x = smem_get_entry(SMEM_ERR_CRASH_LOG, &size);
347 if (x != 0) {
348 x[size - 1] = 0;
349 pr_err("smem: CRASH LOG\n'%s'\n", x);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700350 }
351}
352
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700353
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700354static void handle_modem_crash(void)
355{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700356 pr_err("MODEM/AMSS has CRASHED\n");
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700357 smd_diag();
358
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700359 /* hard reboot if possible FIXME
360 if (msm_reset_hook)
361 msm_reset_hook();
362 */
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700363
364 /* in this case the modem or watchdog should reboot us */
365 for (;;)
366 ;
367}
368
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700369int smsm_check_for_modem_crash(void)
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700370{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700371 /* if the modem's not ready yet, we have to hope for the best */
372 if (!smsm_info.state)
373 return 0;
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700374
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700375 if (__raw_readl(SMSM_STATE_ADDR(SMSM_MODEM_STATE)) & SMSM_RESET) {
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700376 handle_modem_crash();
377 return -1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700378 }
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700379 return 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700380}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700381EXPORT_SYMBOL(smsm_check_for_modem_crash);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700382
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700383/* the spinlock is used to synchronize between the
Brian Swetland03e00cd2009-07-01 17:58:37 -0700384 * irq handler and code that mutates the channel
385 * list or fiddles with channel state
386 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700387static DEFINE_SPINLOCK(smd_lock);
Brian Swetland03e00cd2009-07-01 17:58:37 -0700388DEFINE_SPINLOCK(smem_lock);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700389
390/* the mutex is used during open() and close()
Brian Swetland03e00cd2009-07-01 17:58:37 -0700391 * operations to avoid races while creating or
392 * destroying smd_channel structures
393 */
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700394static DEFINE_MUTEX(smd_creation_mutex);
395
396static int smd_initialized;
397
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700398struct smd_shared_v1 {
399 struct smd_half_channel ch0;
400 unsigned char data0[SMD_BUF_SIZE];
401 struct smd_half_channel ch1;
402 unsigned char data1[SMD_BUF_SIZE];
403};
404
405struct smd_shared_v2 {
406 struct smd_half_channel ch0;
407 struct smd_half_channel ch1;
408};
409
410struct smd_channel {
411 volatile struct smd_half_channel *send;
412 volatile struct smd_half_channel *recv;
413 unsigned char *send_data;
414 unsigned char *recv_data;
415 unsigned fifo_size;
416 unsigned fifo_mask;
417 struct list_head ch_list;
418
419 unsigned current_packet;
420 unsigned n;
421 void *priv;
422 void (*notify)(void *priv, unsigned flags);
423
424 int (*read)(smd_channel_t *ch, void *data, int len, int user_buf);
425 int (*write)(smd_channel_t *ch, const void *data, int len,
426 int user_buf);
427 int (*read_avail)(smd_channel_t *ch);
428 int (*write_avail)(smd_channel_t *ch);
429 int (*read_from_cb)(smd_channel_t *ch, void *data, int len,
430 int user_buf);
431
432 void (*update_state)(smd_channel_t *ch);
433 unsigned last_state;
434 void (*notify_other_cpu)(void);
435
436 char name[20];
437 struct platform_device pdev;
438 unsigned type;
439
440 int pending_pkt_sz;
441
442 char is_pkt_ch;
443};
444
445struct edge_to_pid {
446 uint32_t local_pid;
447 uint32_t remote_pid;
448};
449
Eric Holmberg7dfd1972011-09-09 16:07:57 -0600450/*
451 * SMD Processor ID's.
452 *
453 * For all processors that have both SMSM and SMD clients,
454 * the SMSM Processor ID and the SMD Processor ID will
455 * be the same. In cases where a processor only supports
456 * SMD, the entry will only exist in this enum.
457 */
458enum {
459 SMD_APPS = SMSM_APPS,
460 SMD_MODEM = SMSM_MODEM,
461 SMD_Q6 = SMSM_Q6,
462 SMD_WCNSS = SMSM_WCNSS,
463 SMD_DSPS = SMSM_DSPS,
464 SMD_MODEM_Q6_FW,
465};
466
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700467/**
468 * Maps edge type to local and remote processor ID's.
469 */
470static struct edge_to_pid edge_to_pids[] = {
471 [SMD_APPS_MODEM] = {SMSM_APPS, SMSM_MODEM},
472 [SMD_APPS_QDSP] = {SMSM_APPS, SMSM_Q6},
473 [SMD_MODEM_QDSP] = {SMSM_MODEM, SMSM_Q6},
474 [SMD_APPS_DSPS] = {SMSM_APPS, SMSM_DSPS},
475 [SMD_MODEM_DSPS] = {SMSM_MODEM, SMSM_DSPS},
476 [SMD_QDSP_DSPS] = {SMSM_Q6, SMSM_DSPS},
477 [SMD_APPS_WCNSS] = {SMSM_APPS, SMSM_WCNSS},
478 [SMD_MODEM_WCNSS] = {SMSM_MODEM, SMSM_WCNSS},
479 [SMD_QDSP_WCNSS] = {SMSM_Q6, SMSM_WCNSS},
480 [SMD_DSPS_WCNSS] = {SMSM_DSPS, SMSM_WCNSS},
Eric Holmberg7dfd1972011-09-09 16:07:57 -0600481 [SMD_APPS_Q6FW] = {SMSM_APPS, SMD_MODEM_Q6_FW},
482 [SMD_MODEM_Q6FW] = {SMSM_MODEM, SMD_MODEM_Q6_FW},
483 [SMD_QDSP_Q6FW] = {SMSM_Q6, SMD_MODEM_Q6_FW},
484 [SMD_DSPS_Q6FW] = {SMSM_DSPS, SMD_MODEM_Q6_FW},
485 [SMD_WCNSS_Q6FW] = {SMSM_WCNSS, SMD_MODEM_Q6_FW},
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700486};
487
488struct restart_notifier_block {
489 unsigned processor;
490 char *name;
491 struct notifier_block nb;
492};
493
494static struct platform_device loopback_tty_pdev = {.name = "LOOPBACK_TTY"};
495
496static LIST_HEAD(smd_ch_closed_list);
497static LIST_HEAD(smd_ch_closing_list);
498static LIST_HEAD(smd_ch_to_close_list);
499static LIST_HEAD(smd_ch_list_modem);
500static LIST_HEAD(smd_ch_list_dsp);
501static LIST_HEAD(smd_ch_list_dsps);
502static LIST_HEAD(smd_ch_list_wcnss);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700503
504static unsigned char smd_ch_allocated[64];
505static struct work_struct probe_work;
506
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700507static void finalize_channel_close_fn(struct work_struct *work);
508static DECLARE_WORK(finalize_channel_close_work, finalize_channel_close_fn);
509static struct workqueue_struct *channel_close_wq;
510
511static int smd_alloc_channel(struct smd_alloc_elm *alloc_elm);
512
513/* on smp systems, the probe might get called from multiple cores,
514 hence use a lock */
515static DEFINE_MUTEX(smd_probe_lock);
516
517static void smd_channel_probe_worker(struct work_struct *work)
518{
519 struct smd_alloc_elm *shared;
520 unsigned n;
521 uint32_t type;
522
523 shared = smem_find(ID_CH_ALLOC_TBL, sizeof(*shared) * 64);
524
525 if (!shared) {
526 pr_err("%s: allocation table not initialized\n", __func__);
527 return;
528 }
529
530 mutex_lock(&smd_probe_lock);
531 for (n = 0; n < 64; n++) {
532 if (smd_ch_allocated[n])
533 continue;
534
535 /* channel should be allocated only if APPS
536 processor is involved */
537 type = SMD_CHANNEL_TYPE(shared[n].type);
538 if ((type != SMD_APPS_MODEM) && (type != SMD_APPS_QDSP) &&
539 (type != SMD_APPS_DSPS) && (type != SMD_APPS_WCNSS))
540 continue;
541 if (!shared[n].ref_count)
542 continue;
543 if (!shared[n].name[0])
544 continue;
545
546 if (!smd_alloc_channel(&shared[n]))
547 smd_ch_allocated[n] = 1;
548 else
549 SMD_INFO("Probe skipping ch %d, not allocated\n", n);
550 }
551 mutex_unlock(&smd_probe_lock);
552}
553
554/**
555 * Lookup processor ID and determine if it belongs to the proved edge
556 * type.
557 *
558 * @shared2: Pointer to v2 shared channel structure
559 * @type: Edge type
560 * @pid: Processor ID of processor on edge
561 * @local_ch: Channel that belongs to processor @pid
562 * @remote_ch: Other side of edge contained @pid
563 *
564 * Returns 0 for not on edge, 1 for found on edge
565 */
566static int pid_is_on_edge(struct smd_shared_v2 *shared2,
567 uint32_t type, uint32_t pid,
568 struct smd_half_channel **local_ch,
569 struct smd_half_channel **remote_ch
570 )
571{
572 int ret = 0;
573 struct edge_to_pid *edge;
574
575 *local_ch = 0;
576 *remote_ch = 0;
577
578 if (!shared2 || (type >= ARRAY_SIZE(edge_to_pids)))
579 return 0;
580
581 edge = &edge_to_pids[type];
582 if (edge->local_pid != edge->remote_pid) {
583 if (pid == edge->local_pid) {
584 *local_ch = &shared2->ch0;
585 *remote_ch = &shared2->ch1;
586 ret = 1;
587 } else if (pid == edge->remote_pid) {
588 *local_ch = &shared2->ch1;
589 *remote_ch = &shared2->ch0;
590 ret = 1;
591 }
592 }
593
594 return ret;
595}
596
Eric Holmberg2a563c32011-10-05 14:51:43 -0600597static void smd_reset_edge(struct smd_half_channel *ch, unsigned new_state)
598{
599 if (ch->state != SMD_SS_CLOSED) {
600 ch->state = new_state;
601 ch->fDSR = 0;
602 ch->fCTS = 0;
603 ch->fCD = 0;
604 ch->fSTATE = 1;
605 }
606}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700607
608static void smd_channel_reset_state(struct smd_alloc_elm *shared,
609 unsigned new_state, unsigned pid)
610{
611 unsigned n;
612 struct smd_shared_v2 *shared2;
613 uint32_t type;
614 struct smd_half_channel *local_ch;
615 struct smd_half_channel *remote_ch;
616
617 for (n = 0; n < SMD_CHANNELS; n++) {
618 if (!shared[n].ref_count)
619 continue;
620 if (!shared[n].name[0])
621 continue;
622
623 type = SMD_CHANNEL_TYPE(shared[n].type);
624 shared2 = smem_alloc(SMEM_SMD_BASE_ID + n, sizeof(*shared2));
625 if (!shared2)
626 continue;
627
Eric Holmberg2a563c32011-10-05 14:51:43 -0600628 if (pid_is_on_edge(shared2, type, pid, &local_ch, &remote_ch))
629 smd_reset_edge(local_ch, new_state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700630
Eric Holmberg2a563c32011-10-05 14:51:43 -0600631 /*
632 * ModemFW is in the same subsystem as ModemSW, but has
633 * separate SMD edges that need to be reset.
634 */
635 if (pid == SMSM_MODEM &&
636 pid_is_on_edge(shared2, type, SMD_MODEM_Q6_FW,
637 &local_ch, &remote_ch))
638 smd_reset_edge(local_ch, new_state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700639 }
640}
641
642
643void smd_channel_reset(uint32_t restart_pid)
644{
645 struct smd_alloc_elm *shared;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700646 unsigned long flags;
647
648 SMD_DBG("%s: starting reset\n", __func__);
649 shared = smem_find(ID_CH_ALLOC_TBL, sizeof(*shared) * 64);
650 if (!shared) {
651 pr_err("%s: allocation table not initialized\n", __func__);
652 return;
653 }
654
Eric Holmbergf6d7d1a2011-09-23 18:31:04 -0600655 /* release any held spinlocks */
656 remote_spin_release(&remote_spinlock, restart_pid);
657 remote_spin_release_all(restart_pid);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700658
659 /* reset SMSM entry */
660 if (smsm_info.state) {
661 writel_relaxed(0, SMSM_STATE_ADDR(restart_pid));
662
Eric Holmberg351a63c2011-12-02 17:49:43 -0700663 /* restart SMSM init handshake */
664 if (restart_pid == SMSM_MODEM) {
665 smsm_change_state(SMSM_APPS_STATE,
666 SMSM_INIT | SMSM_SMD_LOOPBACK, 0);
667 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700668
669 /* notify SMSM processors */
670 smsm_irq_handler(0, 0);
671 MSM_TRIG_A2M_SMSM_INT;
672 MSM_TRIG_A2Q6_SMSM_INT;
Jeff Hugo6a8057c2011-08-16 13:47:12 -0600673 MSM_TRIG_A2DSPS_SMSM_INT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700674 }
675
676 /* change all remote states to CLOSING */
677 mutex_lock(&smd_probe_lock);
678 spin_lock_irqsave(&smd_lock, flags);
679 smd_channel_reset_state(shared, SMD_SS_CLOSING, restart_pid);
680 spin_unlock_irqrestore(&smd_lock, flags);
681 mutex_unlock(&smd_probe_lock);
682
683 /* notify SMD processors */
684 mb();
685 smd_fake_irq_handler(0);
686 notify_modem_smd();
687 notify_dsp_smd();
688 notify_dsps_smd();
689 notify_wcnss_smd();
690
691 /* change all remote states to CLOSED */
692 mutex_lock(&smd_probe_lock);
693 spin_lock_irqsave(&smd_lock, flags);
694 smd_channel_reset_state(shared, SMD_SS_CLOSED, restart_pid);
695 spin_unlock_irqrestore(&smd_lock, flags);
696 mutex_unlock(&smd_probe_lock);
697
698 /* notify SMD processors */
699 mb();
700 smd_fake_irq_handler(0);
701 notify_modem_smd();
702 notify_dsp_smd();
703 notify_dsps_smd();
704 notify_wcnss_smd();
705
706 SMD_DBG("%s: finished reset\n", __func__);
707}
708
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700709/* how many bytes are available for reading */
710static int smd_stream_read_avail(struct smd_channel *ch)
711{
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700712 return (ch->recv->head - ch->recv->tail) & ch->fifo_mask;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700713}
714
715/* how many bytes we are free to write */
716static int smd_stream_write_avail(struct smd_channel *ch)
717{
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700718 return ch->fifo_mask -
719 ((ch->send->head - ch->send->tail) & ch->fifo_mask);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700720}
721
722static int smd_packet_read_avail(struct smd_channel *ch)
723{
724 if (ch->current_packet) {
725 int n = smd_stream_read_avail(ch);
726 if (n > ch->current_packet)
727 n = ch->current_packet;
728 return n;
729 } else {
730 return 0;
731 }
732}
733
734static int smd_packet_write_avail(struct smd_channel *ch)
735{
736 int n = smd_stream_write_avail(ch);
737 return n > SMD_HEADER_SIZE ? n - SMD_HEADER_SIZE : 0;
738}
739
740static int ch_is_open(struct smd_channel *ch)
741{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700742 return (ch->recv->state == SMD_SS_OPENED ||
743 ch->recv->state == SMD_SS_FLUSHING)
744 && (ch->send->state == SMD_SS_OPENED);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700745}
746
747/* provide a pointer and length to readable data in the fifo */
748static unsigned ch_read_buffer(struct smd_channel *ch, void **ptr)
749{
750 unsigned head = ch->recv->head;
751 unsigned tail = ch->recv->tail;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700752 *ptr = (void *) (ch->recv_data + tail);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700753
754 if (tail <= head)
755 return head - tail;
756 else
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700757 return ch->fifo_size - tail;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700758}
759
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700760static int read_intr_blocked(struct smd_channel *ch)
761{
762 return ch->recv->fBLOCKREADINTR;
763}
764
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700765/* advance the fifo read pointer after data from ch_read_buffer is consumed */
766static void ch_read_done(struct smd_channel *ch, unsigned count)
767{
768 BUG_ON(count > smd_stream_read_avail(ch));
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700769 ch->recv->tail = (ch->recv->tail + count) & ch->fifo_mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700770 wmb();
Haley Teng7632fba2009-10-12 10:38:10 -0700771 ch->send->fTAIL = 1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700772}
773
774/* basic read interface to ch_read_{buffer,done} used
Brian Swetland03e00cd2009-07-01 17:58:37 -0700775 * by smd_*_read() and update_packet_state()
776 * will read-and-discard if the _data pointer is null
777 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700778static int ch_read(struct smd_channel *ch, void *_data, int len, int user_buf)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700779{
780 void *ptr;
781 unsigned n;
782 unsigned char *data = _data;
783 int orig_len = len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700784 int r = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700785
786 while (len > 0) {
787 n = ch_read_buffer(ch, &ptr);
788 if (n == 0)
789 break;
790
791 if (n > len)
792 n = len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700793 if (_data) {
794 if (user_buf) {
795 r = copy_to_user(data, ptr, n);
796 if (r > 0) {
797 pr_err("%s: "
798 "copy_to_user could not copy "
799 "%i bytes.\n",
800 __func__,
801 r);
802 }
803 } else
804 memcpy(data, ptr, n);
805 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700806
807 data += n;
808 len -= n;
809 ch_read_done(ch, n);
810 }
811
812 return orig_len - len;
813}
814
815static void update_stream_state(struct smd_channel *ch)
816{
817 /* streams have no special state requiring updating */
818}
819
820static void update_packet_state(struct smd_channel *ch)
821{
822 unsigned hdr[5];
823 int r;
824
825 /* can't do anything if we're in the middle of a packet */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700826 while (ch->current_packet == 0) {
827 /* discard 0 length packets if any */
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700828
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700829 /* don't bother unless we can get the full header */
830 if (smd_stream_read_avail(ch) < SMD_HEADER_SIZE)
831 return;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700832
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700833 r = ch_read(ch, hdr, SMD_HEADER_SIZE, 0);
834 BUG_ON(r != SMD_HEADER_SIZE);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700835
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700836 ch->current_packet = hdr[0];
837 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700838}
839
840/* provide a pointer and length to next free space in the fifo */
841static unsigned ch_write_buffer(struct smd_channel *ch, void **ptr)
842{
843 unsigned head = ch->send->head;
844 unsigned tail = ch->send->tail;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700845 *ptr = (void *) (ch->send_data + head);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700846
847 if (head < tail) {
848 return tail - head - 1;
849 } else {
850 if (tail == 0)
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700851 return ch->fifo_size - head - 1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700852 else
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700853 return ch->fifo_size - head;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700854 }
855}
856
857/* advace the fifo write pointer after freespace
858 * from ch_write_buffer is filled
859 */
860static void ch_write_done(struct smd_channel *ch, unsigned count)
861{
862 BUG_ON(count > smd_stream_write_avail(ch));
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700863 ch->send->head = (ch->send->head + count) & ch->fifo_mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700864 wmb();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700865 ch->send->fHEAD = 1;
866}
867
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700868static void ch_set_state(struct smd_channel *ch, unsigned n)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700869{
870 if (n == SMD_SS_OPENED) {
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700871 ch->send->fDSR = 1;
872 ch->send->fCTS = 1;
873 ch->send->fCD = 1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700874 } else {
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700875 ch->send->fDSR = 0;
876 ch->send->fCTS = 0;
877 ch->send->fCD = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700878 }
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700879 ch->send->state = n;
880 ch->send->fSTATE = 1;
881 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700882}
883
884static void do_smd_probe(void)
885{
886 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
887 if (shared->heap_info.free_offset != last_heap_free) {
888 last_heap_free = shared->heap_info.free_offset;
889 schedule_work(&probe_work);
890 }
891}
892
893static void smd_state_change(struct smd_channel *ch,
894 unsigned last, unsigned next)
895{
896 ch->last_state = next;
897
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700898 SMD_INFO("SMD: ch %d %d -> %d\n", ch->n, last, next);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700899
900 switch (next) {
901 case SMD_SS_OPENING:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700902 if (ch->send->state == SMD_SS_CLOSING ||
903 ch->send->state == SMD_SS_CLOSED) {
904 ch->recv->tail = 0;
905 ch->send->head = 0;
906 ch->send->fBLOCKREADINTR = 0;
907 ch_set_state(ch, SMD_SS_OPENING);
908 }
909 break;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700910 case SMD_SS_OPENED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700911 if (ch->send->state == SMD_SS_OPENING) {
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700912 ch_set_state(ch, SMD_SS_OPENED);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700913 ch->notify(ch->priv, SMD_EVENT_OPEN);
914 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700915 break;
916 case SMD_SS_FLUSHING:
917 case SMD_SS_RESET:
918 /* we should force them to close? */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700919 break;
920 case SMD_SS_CLOSED:
921 if (ch->send->state == SMD_SS_OPENED) {
922 ch_set_state(ch, SMD_SS_CLOSING);
923 ch->current_packet = 0;
Eric Holmbergad4fa8d2011-11-11 16:55:13 -0700924 ch->pending_pkt_sz = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700925 ch->notify(ch->priv, SMD_EVENT_CLOSE);
926 }
927 break;
928 case SMD_SS_CLOSING:
929 if (ch->send->state == SMD_SS_CLOSED) {
930 list_move(&ch->ch_list,
931 &smd_ch_to_close_list);
932 queue_work(channel_close_wq,
933 &finalize_channel_close_work);
934 }
935 break;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700936 }
937}
938
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700939static void handle_smd_irq_closing_list(void)
940{
941 unsigned long flags;
942 struct smd_channel *ch;
943 struct smd_channel *index;
944 unsigned tmp;
945
946 spin_lock_irqsave(&smd_lock, flags);
947 list_for_each_entry_safe(ch, index, &smd_ch_closing_list, ch_list) {
948 if (ch->recv->fSTATE)
949 ch->recv->fSTATE = 0;
950 tmp = ch->recv->state;
951 if (tmp != ch->last_state)
952 smd_state_change(ch, ch->last_state, tmp);
953 }
954 spin_unlock_irqrestore(&smd_lock, flags);
955}
956
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700957static void handle_smd_irq(struct list_head *list, void (*notify)(void))
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700958{
959 unsigned long flags;
960 struct smd_channel *ch;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700961 unsigned ch_flags;
962 unsigned tmp;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700963 unsigned char state_change;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700964
965 spin_lock_irqsave(&smd_lock, flags);
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700966 list_for_each_entry(ch, list, ch_list) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700967 state_change = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700968 ch_flags = 0;
969 if (ch_is_open(ch)) {
970 if (ch->recv->fHEAD) {
971 ch->recv->fHEAD = 0;
972 ch_flags |= 1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700973 }
974 if (ch->recv->fTAIL) {
975 ch->recv->fTAIL = 0;
976 ch_flags |= 2;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700977 }
978 if (ch->recv->fSTATE) {
979 ch->recv->fSTATE = 0;
980 ch_flags |= 4;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700981 }
982 }
983 tmp = ch->recv->state;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700984 if (tmp != ch->last_state) {
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700985 smd_state_change(ch, ch->last_state, tmp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700986 state_change = 1;
987 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700988 if (ch_flags) {
989 ch->update_state(ch);
990 ch->notify(ch->priv, SMD_EVENT_DATA);
991 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700992 if (ch_flags & 0x4 && !state_change)
993 ch->notify(ch->priv, SMD_EVENT_STATUS);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700994 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700995 spin_unlock_irqrestore(&smd_lock, flags);
996 do_smd_probe();
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700997}
998
Brian Swetland37521a32009-07-01 18:30:47 -0700999static irqreturn_t smd_modem_irq_handler(int irq, void *data)
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001000{
Brian Swetland37521a32009-07-01 18:30:47 -07001001 handle_smd_irq(&smd_ch_list_modem, notify_modem_smd);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001002 handle_smd_irq_closing_list();
Brian Swetland37521a32009-07-01 18:30:47 -07001003 return IRQ_HANDLED;
1004}
1005
Daniel Walkerb13525c2010-03-18 10:10:30 -07001006#if defined(CONFIG_QDSP6)
Brian Swetland37521a32009-07-01 18:30:47 -07001007static irqreturn_t smd_dsp_irq_handler(int irq, void *data)
1008{
1009 handle_smd_irq(&smd_ch_list_dsp, notify_dsp_smd);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001010 handle_smd_irq_closing_list();
1011 return IRQ_HANDLED;
1012}
1013#endif
1014
1015#if defined(CONFIG_DSPS)
1016static irqreturn_t smd_dsps_irq_handler(int irq, void *data)
1017{
1018 handle_smd_irq(&smd_ch_list_dsps, notify_dsps_smd);
1019 handle_smd_irq_closing_list();
1020 return IRQ_HANDLED;
1021}
1022#endif
1023
1024#if defined(CONFIG_WCNSS)
1025static irqreturn_t smd_wcnss_irq_handler(int irq, void *data)
1026{
1027 handle_smd_irq(&smd_ch_list_wcnss, notify_wcnss_smd);
1028 handle_smd_irq_closing_list();
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001029 return IRQ_HANDLED;
1030}
Daniel Walkerb13525c2010-03-18 10:10:30 -07001031#endif
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001032
1033static void smd_fake_irq_handler(unsigned long arg)
1034{
Brian Swetland37521a32009-07-01 18:30:47 -07001035 handle_smd_irq(&smd_ch_list_modem, notify_modem_smd);
1036 handle_smd_irq(&smd_ch_list_dsp, notify_dsp_smd);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001037 handle_smd_irq(&smd_ch_list_dsps, notify_dsps_smd);
1038 handle_smd_irq(&smd_ch_list_wcnss, notify_wcnss_smd);
1039 handle_smd_irq_closing_list();
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001040}
1041
1042static DECLARE_TASKLET(smd_fake_irq_tasklet, smd_fake_irq_handler, 0);
1043
Brian Swetland37521a32009-07-01 18:30:47 -07001044static inline int smd_need_int(struct smd_channel *ch)
1045{
1046 if (ch_is_open(ch)) {
1047 if (ch->recv->fHEAD || ch->recv->fTAIL || ch->recv->fSTATE)
1048 return 1;
1049 if (ch->recv->state != ch->last_state)
1050 return 1;
1051 }
1052 return 0;
1053}
1054
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001055void smd_sleep_exit(void)
1056{
1057 unsigned long flags;
1058 struct smd_channel *ch;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001059 int need_int = 0;
1060
1061 spin_lock_irqsave(&smd_lock, flags);
Brian Swetland37521a32009-07-01 18:30:47 -07001062 list_for_each_entry(ch, &smd_ch_list_modem, ch_list) {
1063 if (smd_need_int(ch)) {
1064 need_int = 1;
1065 break;
1066 }
1067 }
1068 list_for_each_entry(ch, &smd_ch_list_dsp, ch_list) {
1069 if (smd_need_int(ch)) {
1070 need_int = 1;
1071 break;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001072 }
1073 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001074 list_for_each_entry(ch, &smd_ch_list_dsps, ch_list) {
1075 if (smd_need_int(ch)) {
1076 need_int = 1;
1077 break;
1078 }
1079 }
1080 list_for_each_entry(ch, &smd_ch_list_wcnss, ch_list) {
1081 if (smd_need_int(ch)) {
1082 need_int = 1;
1083 break;
1084 }
1085 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001086 spin_unlock_irqrestore(&smd_lock, flags);
1087 do_smd_probe();
Brian Swetland37521a32009-07-01 18:30:47 -07001088
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001089 if (need_int) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001090 SMD_DBG("smd_sleep_exit need interrupt\n");
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001091 tasklet_schedule(&smd_fake_irq_tasklet);
1092 }
1093}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001094EXPORT_SYMBOL(smd_sleep_exit);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001095
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001096static int smd_is_packet(struct smd_alloc_elm *alloc_elm)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001097{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001098 if (SMD_XFER_TYPE(alloc_elm->type) == 1)
1099 return 0;
1100 else if (SMD_XFER_TYPE(alloc_elm->type) == 2)
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001101 return 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001102
1103 /* for cases where xfer type is 0 */
1104 if (!strncmp(alloc_elm->name, "DAL", 3))
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001105 return 0;
1106
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001107 /* for cases where xfer type is 0 */
1108 if (!strncmp(alloc_elm->name, "RPCCALL_QDSP", 12))
1109 return 0;
1110
1111 if (alloc_elm->cid > 4 || alloc_elm->cid == 1)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001112 return 1;
1113 else
1114 return 0;
1115}
1116
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001117static int smd_stream_write(smd_channel_t *ch, const void *_data, int len,
1118 int user_buf)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001119{
1120 void *ptr;
1121 const unsigned char *buf = _data;
1122 unsigned xfer;
1123 int orig_len = len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001124 int r = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001125
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001126 SMD_DBG("smd_stream_write() %d -> ch%d\n", len, ch->n);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001127 if (len < 0)
1128 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001129 else if (len == 0)
1130 return 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001131
1132 while ((xfer = ch_write_buffer(ch, &ptr)) != 0) {
1133 if (!ch_is_open(ch))
1134 break;
1135 if (xfer > len)
1136 xfer = len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001137 if (user_buf) {
1138 r = copy_from_user(ptr, buf, xfer);
1139 if (r > 0) {
1140 pr_err("%s: "
1141 "copy_from_user could not copy %i "
1142 "bytes.\n",
1143 __func__,
1144 r);
1145 }
1146 } else
1147 memcpy(ptr, buf, xfer);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001148 ch_write_done(ch, xfer);
1149 len -= xfer;
1150 buf += xfer;
1151 if (len == 0)
1152 break;
1153 }
1154
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001155 if (orig_len - len)
1156 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001157
1158 return orig_len - len;
1159}
1160
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001161static int smd_packet_write(smd_channel_t *ch, const void *_data, int len,
1162 int user_buf)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001163{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001164 int ret;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001165 unsigned hdr[5];
1166
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001167 SMD_DBG("smd_packet_write() %d -> ch%d\n", len, ch->n);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001168 if (len < 0)
1169 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001170 else if (len == 0)
1171 return 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001172
1173 if (smd_stream_write_avail(ch) < (len + SMD_HEADER_SIZE))
1174 return -ENOMEM;
1175
1176 hdr[0] = len;
1177 hdr[1] = hdr[2] = hdr[3] = hdr[4] = 0;
1178
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001179
1180 ret = smd_stream_write(ch, hdr, sizeof(hdr), 0);
1181 if (ret < 0 || ret != sizeof(hdr)) {
1182 SMD_DBG("%s failed to write pkt header: "
1183 "%d returned\n", __func__, ret);
1184 return -1;
1185 }
1186
1187
1188 ret = smd_stream_write(ch, _data, len, user_buf);
1189 if (ret < 0 || ret != len) {
1190 SMD_DBG("%s failed to write pkt data: "
1191 "%d returned\n", __func__, ret);
1192 return ret;
1193 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001194
1195 return len;
1196}
1197
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001198static int smd_stream_read(smd_channel_t *ch, void *data, int len, int user_buf)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001199{
1200 int r;
1201
1202 if (len < 0)
1203 return -EINVAL;
1204
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001205 r = ch_read(ch, data, len, user_buf);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001206 if (r > 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001207 if (!read_intr_blocked(ch))
1208 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001209
1210 return r;
1211}
1212
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001213static int smd_packet_read(smd_channel_t *ch, void *data, int len, int user_buf)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001214{
1215 unsigned long flags;
1216 int r;
1217
1218 if (len < 0)
1219 return -EINVAL;
1220
1221 if (len > ch->current_packet)
1222 len = ch->current_packet;
1223
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001224 r = ch_read(ch, data, len, user_buf);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001225 if (r > 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001226 if (!read_intr_blocked(ch))
1227 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001228
1229 spin_lock_irqsave(&smd_lock, flags);
1230 ch->current_packet -= r;
1231 update_packet_state(ch);
1232 spin_unlock_irqrestore(&smd_lock, flags);
1233
1234 return r;
1235}
1236
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001237static int smd_packet_read_from_cb(smd_channel_t *ch, void *data, int len,
1238 int user_buf)
1239{
1240 int r;
1241
1242 if (len < 0)
1243 return -EINVAL;
1244
1245 if (len > ch->current_packet)
1246 len = ch->current_packet;
1247
1248 r = ch_read(ch, data, len, user_buf);
1249 if (r > 0)
1250 if (!read_intr_blocked(ch))
1251 ch->notify_other_cpu();
1252
1253 ch->current_packet -= r;
1254 update_packet_state(ch);
1255
1256 return r;
1257}
1258
Angshuman Sarkarac7d6252011-09-30 18:20:59 +05301259#if (defined(CONFIG_MSM_SMD_PKG4) || defined(CONFIG_MSM_SMD_PKG3))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001260static int smd_alloc_v2(struct smd_channel *ch)
1261{
1262 struct smd_shared_v2 *shared2;
1263 void *buffer;
1264 unsigned buffer_sz;
1265
1266 shared2 = smem_alloc(SMEM_SMD_BASE_ID + ch->n, sizeof(*shared2));
1267 if (!shared2) {
1268 SMD_INFO("smem_alloc failed ch=%d\n", ch->n);
Angshuman Sarkarac7d6252011-09-30 18:20:59 +05301269 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001270 }
1271 buffer = smem_get_entry(SMEM_SMD_FIFO_BASE_ID + ch->n, &buffer_sz);
1272 if (!buffer) {
Angshuman Sarkarac7d6252011-09-30 18:20:59 +05301273 SMD_INFO("smem_get_entry failed\n");
1274 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001275 }
1276
1277 /* buffer must be a power-of-two size */
Angshuman Sarkarac7d6252011-09-30 18:20:59 +05301278 if (buffer_sz & (buffer_sz - 1)) {
1279 SMD_INFO("Buffer size: %u not power of two\n", buffer_sz);
1280 return -EINVAL;
1281 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001282 buffer_sz /= 2;
1283 ch->send = &shared2->ch0;
1284 ch->recv = &shared2->ch1;
1285 ch->send_data = buffer;
1286 ch->recv_data = buffer + buffer_sz;
1287 ch->fifo_size = buffer_sz;
1288 return 0;
1289}
1290
1291static int smd_alloc_v1(struct smd_channel *ch)
1292{
Angshuman Sarkarac7d6252011-09-30 18:20:59 +05301293 return -EINVAL;
1294}
1295
1296#else /* define v1 for older targets */
1297static int smd_alloc_v2(struct smd_channel *ch)
1298{
1299 return -EINVAL;
1300}
1301
1302static int smd_alloc_v1(struct smd_channel *ch)
1303{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001304 struct smd_shared_v1 *shared1;
1305 shared1 = smem_alloc(ID_SMD_CHANNELS + ch->n, sizeof(*shared1));
1306 if (!shared1) {
1307 pr_err("smd_alloc_channel() cid %d does not exist\n", ch->n);
Angshuman Sarkarac7d6252011-09-30 18:20:59 +05301308 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001309 }
1310 ch->send = &shared1->ch0;
1311 ch->recv = &shared1->ch1;
1312 ch->send_data = shared1->data0;
1313 ch->recv_data = shared1->data1;
1314 ch->fifo_size = SMD_BUF_SIZE;
1315 return 0;
1316}
1317
Angshuman Sarkarac7d6252011-09-30 18:20:59 +05301318#endif
1319
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001320static int smd_alloc_channel(struct smd_alloc_elm *alloc_elm)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001321{
1322 struct smd_channel *ch;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001323
1324 ch = kzalloc(sizeof(struct smd_channel), GFP_KERNEL);
1325 if (ch == 0) {
1326 pr_err("smd_alloc_channel() out of memory\n");
Brian Swetland34f719b2009-10-30 16:22:05 -07001327 return -1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001328 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001329 ch->n = alloc_elm->cid;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001330
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001331 if (smd_alloc_v2(ch) && smd_alloc_v1(ch)) {
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001332 kfree(ch);
Brian Swetland34f719b2009-10-30 16:22:05 -07001333 return -1;
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001334 }
1335
1336 ch->fifo_mask = ch->fifo_size - 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001337 ch->type = SMD_CHANNEL_TYPE(alloc_elm->type);
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001338
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001339 if (ch->type == SMD_APPS_MODEM)
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001340 ch->notify_other_cpu = notify_modem_smd;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001341 else if (ch->type == SMD_APPS_QDSP)
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001342 ch->notify_other_cpu = notify_dsp_smd;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001343 else if (ch->type == SMD_APPS_DSPS)
1344 ch->notify_other_cpu = notify_dsps_smd;
1345 else
1346 ch->notify_other_cpu = notify_wcnss_smd;
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001347
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001348 if (smd_is_packet(alloc_elm)) {
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001349 ch->read = smd_packet_read;
1350 ch->write = smd_packet_write;
1351 ch->read_avail = smd_packet_read_avail;
1352 ch->write_avail = smd_packet_write_avail;
1353 ch->update_state = update_packet_state;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001354 ch->read_from_cb = smd_packet_read_from_cb;
1355 ch->is_pkt_ch = 1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001356 } else {
1357 ch->read = smd_stream_read;
1358 ch->write = smd_stream_write;
1359 ch->read_avail = smd_stream_read_avail;
1360 ch->write_avail = smd_stream_write_avail;
1361 ch->update_state = update_stream_state;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001362 ch->read_from_cb = smd_stream_read;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001363 }
1364
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001365 memcpy(ch->name, alloc_elm->name, SMD_MAX_CH_NAME_LEN);
1366 ch->name[SMD_MAX_CH_NAME_LEN-1] = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001367
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001368 ch->pdev.name = ch->name;
1369 ch->pdev.id = ch->type;
1370
1371 SMD_INFO("smd_alloc_channel() '%s' cid=%d\n",
1372 ch->name, ch->n);
1373
1374 mutex_lock(&smd_creation_mutex);
1375 list_add(&ch->ch_list, &smd_ch_closed_list);
1376 mutex_unlock(&smd_creation_mutex);
1377
1378 platform_device_register(&ch->pdev);
1379 if (!strncmp(ch->name, "LOOPBACK", 8) && ch->type == SMD_APPS_MODEM) {
1380 /* create a platform driver to be used by smd_tty driver
1381 * so that it can access the loopback port
1382 */
1383 loopback_tty_pdev.id = ch->type;
1384 platform_device_register(&loopback_tty_pdev);
1385 }
1386 return 0;
1387}
1388
1389static inline void notify_loopback_smd(void)
1390{
1391 unsigned long flags;
1392 struct smd_channel *ch;
1393
1394 spin_lock_irqsave(&smd_lock, flags);
1395 list_for_each_entry(ch, &smd_ch_list_loopback, ch_list) {
1396 ch->notify(ch->priv, SMD_EVENT_DATA);
1397 }
1398 spin_unlock_irqrestore(&smd_lock, flags);
1399}
1400
1401static int smd_alloc_loopback_channel(void)
1402{
1403 static struct smd_half_channel smd_loopback_ctl;
1404 static char smd_loopback_data[SMD_BUF_SIZE];
1405 struct smd_channel *ch;
1406
1407 ch = kzalloc(sizeof(struct smd_channel), GFP_KERNEL);
1408 if (ch == 0) {
1409 pr_err("%s: out of memory\n", __func__);
1410 return -1;
1411 }
1412 ch->n = SMD_LOOPBACK_CID;
1413
1414 ch->send = &smd_loopback_ctl;
1415 ch->recv = &smd_loopback_ctl;
1416 ch->send_data = smd_loopback_data;
1417 ch->recv_data = smd_loopback_data;
1418 ch->fifo_size = SMD_BUF_SIZE;
1419
1420 ch->fifo_mask = ch->fifo_size - 1;
1421 ch->type = SMD_LOOPBACK_TYPE;
1422 ch->notify_other_cpu = notify_loopback_smd;
1423
1424 ch->read = smd_stream_read;
1425 ch->write = smd_stream_write;
1426 ch->read_avail = smd_stream_read_avail;
1427 ch->write_avail = smd_stream_write_avail;
1428 ch->update_state = update_stream_state;
1429 ch->read_from_cb = smd_stream_read;
1430
1431 memset(ch->name, 0, 20);
1432 memcpy(ch->name, "local_loopback", 14);
1433
1434 ch->pdev.name = ch->name;
1435 ch->pdev.id = ch->type;
1436
1437 SMD_INFO("%s: '%s' cid=%d\n", __func__, ch->name, ch->n);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001438
1439 mutex_lock(&smd_creation_mutex);
1440 list_add(&ch->ch_list, &smd_ch_closed_list);
1441 mutex_unlock(&smd_creation_mutex);
1442
1443 platform_device_register(&ch->pdev);
Brian Swetland34f719b2009-10-30 16:22:05 -07001444 return 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001445}
1446
1447static void do_nothing_notify(void *priv, unsigned flags)
1448{
1449}
1450
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001451static void finalize_channel_close_fn(struct work_struct *work)
1452{
1453 unsigned long flags;
1454 struct smd_channel *ch;
1455 struct smd_channel *index;
1456
1457 spin_lock_irqsave(&smd_lock, flags);
1458 list_for_each_entry_safe(ch, index, &smd_ch_to_close_list, ch_list) {
1459 list_del(&ch->ch_list);
1460 spin_unlock_irqrestore(&smd_lock, flags);
1461 mutex_lock(&smd_creation_mutex);
1462 list_add(&ch->ch_list, &smd_ch_closed_list);
1463 mutex_unlock(&smd_creation_mutex);
1464 ch->notify(ch->priv, SMD_EVENT_REOPEN_READY);
1465 ch->notify = do_nothing_notify;
1466 spin_lock_irqsave(&smd_lock, flags);
1467 }
1468 spin_unlock_irqrestore(&smd_lock, flags);
1469}
1470
1471struct smd_channel *smd_get_channel(const char *name, uint32_t type)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001472{
1473 struct smd_channel *ch;
1474
1475 mutex_lock(&smd_creation_mutex);
1476 list_for_each_entry(ch, &smd_ch_closed_list, ch_list) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001477 if (!strcmp(name, ch->name) &&
1478 (type == ch->type)) {
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001479 list_del(&ch->ch_list);
1480 mutex_unlock(&smd_creation_mutex);
1481 return ch;
1482 }
1483 }
1484 mutex_unlock(&smd_creation_mutex);
1485
1486 return NULL;
1487}
1488
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001489int smd_named_open_on_edge(const char *name, uint32_t edge,
1490 smd_channel_t **_ch,
1491 void *priv, void (*notify)(void *, unsigned))
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001492{
1493 struct smd_channel *ch;
1494 unsigned long flags;
1495
1496 if (smd_initialized == 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001497 SMD_INFO("smd_open() before smd_init()\n");
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001498 return -ENODEV;
1499 }
1500
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001501 SMD_DBG("smd_open('%s', %p, %p)\n", name, priv, notify);
1502
1503 ch = smd_get_channel(name, edge);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001504 if (!ch)
1505 return -ENODEV;
1506
1507 if (notify == 0)
1508 notify = do_nothing_notify;
1509
1510 ch->notify = notify;
1511 ch->current_packet = 0;
1512 ch->last_state = SMD_SS_CLOSED;
1513 ch->priv = priv;
1514
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001515 if (edge == SMD_LOOPBACK_TYPE) {
1516 ch->last_state = SMD_SS_OPENED;
1517 ch->send->state = SMD_SS_OPENED;
1518 ch->send->fDSR = 1;
1519 ch->send->fCTS = 1;
1520 ch->send->fCD = 1;
1521 }
1522
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001523 *_ch = ch;
1524
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001525 SMD_DBG("smd_open: opening '%s'\n", ch->name);
1526
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001527 spin_lock_irqsave(&smd_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001528 if (SMD_CHANNEL_TYPE(ch->type) == SMD_APPS_MODEM)
Brian Swetland37521a32009-07-01 18:30:47 -07001529 list_add(&ch->ch_list, &smd_ch_list_modem);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001530 else if (SMD_CHANNEL_TYPE(ch->type) == SMD_APPS_QDSP)
Brian Swetland37521a32009-07-01 18:30:47 -07001531 list_add(&ch->ch_list, &smd_ch_list_dsp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001532 else if (SMD_CHANNEL_TYPE(ch->type) == SMD_APPS_DSPS)
1533 list_add(&ch->ch_list, &smd_ch_list_dsps);
1534 else if (SMD_CHANNEL_TYPE(ch->type) == SMD_APPS_WCNSS)
1535 list_add(&ch->ch_list, &smd_ch_list_wcnss);
1536 else
1537 list_add(&ch->ch_list, &smd_ch_list_loopback);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001538
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001539 SMD_DBG("%s: opening ch %d\n", __func__, ch->n);
1540
1541 if (edge != SMD_LOOPBACK_TYPE)
1542 smd_state_change(ch, ch->last_state, SMD_SS_OPENING);
1543
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001544 spin_unlock_irqrestore(&smd_lock, flags);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001545
1546 return 0;
1547}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001548EXPORT_SYMBOL(smd_named_open_on_edge);
1549
1550
1551int smd_open(const char *name, smd_channel_t **_ch,
1552 void *priv, void (*notify)(void *, unsigned))
1553{
1554 return smd_named_open_on_edge(name, SMD_APPS_MODEM, _ch, priv,
1555 notify);
1556}
1557EXPORT_SYMBOL(smd_open);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001558
1559int smd_close(smd_channel_t *ch)
1560{
1561 unsigned long flags;
1562
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001563 if (ch == 0)
1564 return -1;
1565
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001566 SMD_INFO("smd_close(%s)\n", ch->name);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001567
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001568 spin_lock_irqsave(&smd_lock, flags);
1569 list_del(&ch->ch_list);
1570 if (ch->n == SMD_LOOPBACK_CID) {
1571 ch->send->fDSR = 0;
1572 ch->send->fCTS = 0;
1573 ch->send->fCD = 0;
1574 ch->send->state = SMD_SS_CLOSED;
1575 } else
1576 ch_set_state(ch, SMD_SS_CLOSED);
1577
1578 if (ch->recv->state == SMD_SS_OPENED) {
1579 list_add(&ch->ch_list, &smd_ch_closing_list);
1580 spin_unlock_irqrestore(&smd_lock, flags);
1581 } else {
1582 spin_unlock_irqrestore(&smd_lock, flags);
1583 ch->notify = do_nothing_notify;
1584 mutex_lock(&smd_creation_mutex);
1585 list_add(&ch->ch_list, &smd_ch_closed_list);
1586 mutex_unlock(&smd_creation_mutex);
1587 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001588
1589 return 0;
1590}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001591EXPORT_SYMBOL(smd_close);
1592
1593int smd_write_start(smd_channel_t *ch, int len)
1594{
1595 int ret;
1596 unsigned hdr[5];
1597
1598 if (!ch) {
1599 pr_err("%s: Invalid channel specified\n", __func__);
1600 return -ENODEV;
1601 }
1602 if (!ch->is_pkt_ch) {
1603 pr_err("%s: non-packet channel specified\n", __func__);
1604 return -EACCES;
1605 }
1606 if (len < 1) {
1607 pr_err("%s: invalid length: %d\n", __func__, len);
1608 return -EINVAL;
1609 }
1610
1611 if (ch->pending_pkt_sz) {
1612 pr_err("%s: packet of size: %d in progress\n", __func__,
1613 ch->pending_pkt_sz);
1614 return -EBUSY;
1615 }
1616 ch->pending_pkt_sz = len;
1617
1618 if (smd_stream_write_avail(ch) < (SMD_HEADER_SIZE)) {
1619 ch->pending_pkt_sz = 0;
1620 SMD_DBG("%s: no space to write packet header\n", __func__);
1621 return -EAGAIN;
1622 }
1623
1624 hdr[0] = len;
1625 hdr[1] = hdr[2] = hdr[3] = hdr[4] = 0;
1626
1627
1628 ret = smd_stream_write(ch, hdr, sizeof(hdr), 0);
1629 if (ret < 0 || ret != sizeof(hdr)) {
1630 ch->pending_pkt_sz = 0;
1631 pr_err("%s: packet header failed to write\n", __func__);
1632 return -EPERM;
1633 }
1634 return 0;
1635}
1636EXPORT_SYMBOL(smd_write_start);
1637
1638int smd_write_segment(smd_channel_t *ch, void *data, int len, int user_buf)
1639{
1640 int bytes_written;
1641
1642 if (!ch) {
1643 pr_err("%s: Invalid channel specified\n", __func__);
1644 return -ENODEV;
1645 }
1646 if (len < 1) {
1647 pr_err("%s: invalid length: %d\n", __func__, len);
1648 return -EINVAL;
1649 }
1650
1651 if (!ch->pending_pkt_sz) {
1652 pr_err("%s: no transaction in progress\n", __func__);
1653 return -ENOEXEC;
1654 }
1655 if (ch->pending_pkt_sz - len < 0) {
1656 pr_err("%s: segment of size: %d will make packet go over "
1657 "length\n", __func__, len);
1658 return -EINVAL;
1659 }
1660
1661 bytes_written = smd_stream_write(ch, data, len, user_buf);
1662
1663 ch->pending_pkt_sz -= bytes_written;
1664
1665 return bytes_written;
1666}
1667EXPORT_SYMBOL(smd_write_segment);
1668
1669int smd_write_end(smd_channel_t *ch)
1670{
1671
1672 if (!ch) {
1673 pr_err("%s: Invalid channel specified\n", __func__);
1674 return -ENODEV;
1675 }
1676 if (ch->pending_pkt_sz) {
1677 pr_err("%s: current packet not completely written\n", __func__);
1678 return -E2BIG;
1679 }
1680
1681 return 0;
1682}
1683EXPORT_SYMBOL(smd_write_end);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001684
1685int smd_read(smd_channel_t *ch, void *data, int len)
1686{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001687 return ch->read(ch, data, len, 0);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001688}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001689EXPORT_SYMBOL(smd_read);
1690
1691int smd_read_user_buffer(smd_channel_t *ch, void *data, int len)
1692{
1693 return ch->read(ch, data, len, 1);
1694}
1695EXPORT_SYMBOL(smd_read_user_buffer);
1696
1697int smd_read_from_cb(smd_channel_t *ch, void *data, int len)
1698{
1699 return ch->read_from_cb(ch, data, len, 0);
1700}
1701EXPORT_SYMBOL(smd_read_from_cb);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001702
1703int smd_write(smd_channel_t *ch, const void *data, int len)
1704{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001705 return ch->pending_pkt_sz ? -EBUSY : ch->write(ch, data, len, 0);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001706}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001707EXPORT_SYMBOL(smd_write);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001708
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001709int smd_write_user_buffer(smd_channel_t *ch, const void *data, int len)
Brian Swetland636eb9c2009-12-07 15:28:08 -08001710{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001711 return ch->pending_pkt_sz ? -EBUSY : ch->write(ch, data, len, 1);
Brian Swetland636eb9c2009-12-07 15:28:08 -08001712}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001713EXPORT_SYMBOL(smd_write_user_buffer);
Brian Swetland636eb9c2009-12-07 15:28:08 -08001714
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001715int smd_read_avail(smd_channel_t *ch)
1716{
1717 return ch->read_avail(ch);
1718}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001719EXPORT_SYMBOL(smd_read_avail);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001720
1721int smd_write_avail(smd_channel_t *ch)
1722{
1723 return ch->write_avail(ch);
1724}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001725EXPORT_SYMBOL(smd_write_avail);
1726
1727void smd_enable_read_intr(smd_channel_t *ch)
1728{
1729 if (ch)
1730 ch->send->fBLOCKREADINTR = 0;
1731}
1732EXPORT_SYMBOL(smd_enable_read_intr);
1733
1734void smd_disable_read_intr(smd_channel_t *ch)
1735{
1736 if (ch)
1737 ch->send->fBLOCKREADINTR = 1;
1738}
1739EXPORT_SYMBOL(smd_disable_read_intr);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001740
1741int smd_wait_until_readable(smd_channel_t *ch, int bytes)
1742{
1743 return -1;
1744}
1745
1746int smd_wait_until_writable(smd_channel_t *ch, int bytes)
1747{
1748 return -1;
1749}
1750
1751int smd_cur_packet_size(smd_channel_t *ch)
1752{
1753 return ch->current_packet;
1754}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001755EXPORT_SYMBOL(smd_cur_packet_size);
1756
1757int smd_tiocmget(smd_channel_t *ch)
1758{
1759 return (ch->recv->fDSR ? TIOCM_DSR : 0) |
1760 (ch->recv->fCTS ? TIOCM_CTS : 0) |
1761 (ch->recv->fCD ? TIOCM_CD : 0) |
1762 (ch->recv->fRI ? TIOCM_RI : 0) |
1763 (ch->send->fCTS ? TIOCM_RTS : 0) |
1764 (ch->send->fDSR ? TIOCM_DTR : 0);
1765}
1766EXPORT_SYMBOL(smd_tiocmget);
1767
Vamsi Krishnacb12a102011-08-17 15:18:26 -07001768/* this api will be called while holding smd_lock */
1769int
1770smd_tiocmset_from_cb(smd_channel_t *ch, unsigned int set, unsigned int clear)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001771{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001772 if (set & TIOCM_DTR)
1773 ch->send->fDSR = 1;
1774
1775 if (set & TIOCM_RTS)
1776 ch->send->fCTS = 1;
1777
1778 if (clear & TIOCM_DTR)
1779 ch->send->fDSR = 0;
1780
1781 if (clear & TIOCM_RTS)
1782 ch->send->fCTS = 0;
1783
1784 ch->send->fSTATE = 1;
1785 barrier();
1786 ch->notify_other_cpu();
Vamsi Krishnacb12a102011-08-17 15:18:26 -07001787
1788 return 0;
1789}
1790EXPORT_SYMBOL(smd_tiocmset_from_cb);
1791
1792int smd_tiocmset(smd_channel_t *ch, unsigned int set, unsigned int clear)
1793{
1794 unsigned long flags;
1795
1796 spin_lock_irqsave(&smd_lock, flags);
1797 smd_tiocmset_from_cb(ch, set, clear);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001798 spin_unlock_irqrestore(&smd_lock, flags);
1799
1800 return 0;
1801}
1802EXPORT_SYMBOL(smd_tiocmset);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001803
1804
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001805/* -------------------------------------------------------------------------- */
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001806
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001807/* smem_alloc returns the pointer to smem item if it is already allocated.
1808 * Otherwise, it returns NULL.
1809 */
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001810void *smem_alloc(unsigned id, unsigned size)
1811{
1812 return smem_find(id, size);
1813}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001814EXPORT_SYMBOL(smem_alloc);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001815
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001816/* smem_alloc2 returns the pointer to smem item. If it is not allocated,
1817 * it allocates it and then returns the pointer to it.
1818 */
Angshuman Sarkar4eade0d2011-08-17 14:06:23 +05301819void *smem_alloc2(unsigned id, unsigned size_in)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001820{
1821 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
1822 struct smem_heap_entry *toc = shared->heap_toc;
1823 unsigned long flags;
1824 void *ret = NULL;
1825
1826 if (!shared->heap_info.initialized) {
1827 pr_err("%s: smem heap info not initialized\n", __func__);
1828 return NULL;
1829 }
1830
1831 if (id >= SMEM_NUM_ITEMS)
1832 return NULL;
1833
1834 size_in = ALIGN(size_in, 8);
1835 remote_spin_lock_irqsave(&remote_spinlock, flags);
1836 if (toc[id].allocated) {
1837 SMD_DBG("%s: %u already allocated\n", __func__, id);
1838 if (size_in != toc[id].size)
1839 pr_err("%s: wrong size %u (expected %u)\n",
1840 __func__, toc[id].size, size_in);
1841 else
1842 ret = (void *)(MSM_SHARED_RAM_BASE + toc[id].offset);
1843 } else if (id > SMEM_FIXED_ITEM_LAST) {
1844 SMD_DBG("%s: allocating %u\n", __func__, id);
1845 if (shared->heap_info.heap_remaining >= size_in) {
1846 toc[id].offset = shared->heap_info.free_offset;
1847 toc[id].size = size_in;
1848 wmb();
1849 toc[id].allocated = 1;
1850
1851 shared->heap_info.free_offset += size_in;
1852 shared->heap_info.heap_remaining -= size_in;
1853 ret = (void *)(MSM_SHARED_RAM_BASE + toc[id].offset);
1854 } else
1855 pr_err("%s: not enough memory %u (required %u)\n",
1856 __func__, shared->heap_info.heap_remaining,
1857 size_in);
1858 }
1859 wmb();
1860 remote_spin_unlock_irqrestore(&remote_spinlock, flags);
1861 return ret;
1862}
Angshuman Sarkar4eade0d2011-08-17 14:06:23 +05301863EXPORT_SYMBOL(smem_alloc2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001864
1865void *smem_get_entry(unsigned id, unsigned *size)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001866{
1867 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
1868 struct smem_heap_entry *toc = shared->heap_toc;
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +05301869 int use_spinlocks = spinlocks_initialized;
1870 void *ret = 0;
1871 unsigned long flags = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001872
1873 if (id >= SMEM_NUM_ITEMS)
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +05301874 return ret;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001875
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +05301876 if (use_spinlocks)
1877 remote_spin_lock_irqsave(&remote_spinlock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001878 /* toc is in device memory and cannot be speculatively accessed */
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001879 if (toc[id].allocated) {
1880 *size = toc[id].size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001881 barrier();
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +05301882 ret = (void *) (MSM_SHARED_RAM_BASE + toc[id].offset);
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001883 } else {
1884 *size = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001885 }
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +05301886 if (use_spinlocks)
1887 remote_spin_unlock_irqrestore(&remote_spinlock, flags);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001888
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +05301889 return ret;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001890}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001891EXPORT_SYMBOL(smem_get_entry);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001892
1893void *smem_find(unsigned id, unsigned size_in)
1894{
1895 unsigned size;
1896 void *ptr;
1897
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001898 ptr = smem_get_entry(id, &size);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001899 if (!ptr)
1900 return 0;
1901
1902 size_in = ALIGN(size_in, 8);
1903 if (size_in != size) {
1904 pr_err("smem_find(%d, %d): wrong size %d\n",
1905 id, size_in, size);
1906 return 0;
1907 }
1908
1909 return ptr;
1910}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001911EXPORT_SYMBOL(smem_find);
1912
1913static int smsm_cb_init(void)
1914{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001915 struct smsm_state_info *state_info;
1916 int n;
1917 int ret = 0;
1918
1919 smsm_states = kmalloc(sizeof(struct smsm_state_info)*SMSM_NUM_ENTRIES,
1920 GFP_KERNEL);
1921
1922 if (!smsm_states) {
1923 pr_err("%s: SMSM init failed\n", __func__);
1924 return -ENOMEM;
1925 }
1926
Eric Holmbergc8002902011-09-16 13:55:57 -06001927 mutex_lock(&smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001928 for (n = 0; n < SMSM_NUM_ENTRIES; n++) {
1929 state_info = &smsm_states[n];
1930 state_info->last_value = __raw_readl(SMSM_STATE_ADDR(n));
1931 INIT_LIST_HEAD(&state_info->callbacks);
1932 }
Eric Holmbergc8002902011-09-16 13:55:57 -06001933 mutex_unlock(&smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001934
1935 return ret;
1936}
1937
1938static int smsm_init(void)
1939{
1940 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
1941 int i;
1942 struct smsm_size_info_type *smsm_size_info;
1943
1944 i = remote_spin_lock_init(&remote_spinlock, SMEM_SPINLOCK_SMEM_ALLOC);
1945 if (i) {
1946 pr_err("%s: remote spinlock init failed %d\n", __func__, i);
1947 return i;
1948 }
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +05301949 spinlocks_initialized = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001950
1951 smsm_size_info = smem_alloc(SMEM_SMSM_SIZE_INFO,
1952 sizeof(struct smsm_size_info_type));
1953 if (smsm_size_info) {
1954 SMSM_NUM_ENTRIES = smsm_size_info->num_entries;
1955 SMSM_NUM_HOSTS = smsm_size_info->num_hosts;
1956 }
1957
1958 if (!smsm_info.state) {
1959 smsm_info.state = smem_alloc2(ID_SHARED_STATE,
1960 SMSM_NUM_ENTRIES *
1961 sizeof(uint32_t));
1962
1963 if (smsm_info.state) {
1964 __raw_writel(0, SMSM_STATE_ADDR(SMSM_APPS_STATE));
1965 if ((shared->version[VERSION_MODEM] >> 16) >= 0xB)
1966 __raw_writel(0, \
1967 SMSM_STATE_ADDR(SMSM_APPS_DEM_I));
1968 }
1969 }
1970
1971 if (!smsm_info.intr_mask) {
1972 smsm_info.intr_mask = smem_alloc2(SMEM_SMSM_CPU_INTR_MASK,
1973 SMSM_NUM_ENTRIES *
1974 SMSM_NUM_HOSTS *
1975 sizeof(uint32_t));
1976
1977 if (smsm_info.intr_mask)
1978 for (i = 0; i < SMSM_NUM_ENTRIES; i++)
1979 __raw_writel(0xffffffff,
1980 SMSM_INTR_MASK_ADDR(i, SMSM_APPS));
1981 }
1982
1983 if (!smsm_info.intr_mux)
1984 smsm_info.intr_mux = smem_alloc2(SMEM_SMD_SMSM_INTR_MUX,
1985 SMSM_NUM_INTR_MUX *
1986 sizeof(uint32_t));
1987
1988 i = smsm_cb_init();
1989 if (i)
1990 return i;
1991
1992 wmb();
1993 return 0;
1994}
1995
1996void smsm_reset_modem(unsigned mode)
1997{
1998 if (mode == SMSM_SYSTEM_DOWNLOAD) {
1999 mode = SMSM_RESET | SMSM_SYSTEM_DOWNLOAD;
2000 } else if (mode == SMSM_MODEM_WAIT) {
2001 mode = SMSM_RESET | SMSM_MODEM_WAIT;
2002 } else { /* reset_mode is SMSM_RESET or default */
2003 mode = SMSM_RESET;
2004 }
2005
2006 smsm_change_state(SMSM_APPS_STATE, mode, mode);
2007}
2008EXPORT_SYMBOL(smsm_reset_modem);
2009
2010void smsm_reset_modem_cont(void)
2011{
2012 unsigned long flags;
2013 uint32_t state;
2014
2015 if (!smsm_info.state)
2016 return;
2017
2018 spin_lock_irqsave(&smem_lock, flags);
2019 state = __raw_readl(SMSM_STATE_ADDR(SMSM_APPS_STATE)) \
2020 & ~SMSM_MODEM_WAIT;
2021 __raw_writel(state, SMSM_STATE_ADDR(SMSM_APPS_STATE));
2022 wmb();
2023 spin_unlock_irqrestore(&smem_lock, flags);
2024}
2025EXPORT_SYMBOL(smsm_reset_modem_cont);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002026
2027static irqreturn_t smsm_irq_handler(int irq, void *data)
2028{
2029 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002030
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002031 if (irq == INT_ADSP_A11_SMSM) {
Eric Holmberg6282c5d2011-10-27 17:30:57 -06002032 uint32_t mux_val;
2033 static uint32_t prev_smem_q6_apps_smsm;
2034
2035 if (smsm_info.intr_mux && cpu_is_qsd8x50()) {
2036 mux_val = __raw_readl(
2037 SMSM_INTR_MUX_ADDR(SMEM_Q6_APPS_SMSM));
2038 if (mux_val != prev_smem_q6_apps_smsm)
2039 prev_smem_q6_apps_smsm = mux_val;
2040 }
2041
2042 schedule_work(&smsm_cb_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002043 return IRQ_HANDLED;
2044 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002045
2046 spin_lock_irqsave(&smem_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002047 if (!smsm_info.state) {
2048 SMSM_INFO("<SM NO STATE>\n");
2049 } else {
2050 unsigned old_apps, apps;
2051 unsigned modm = __raw_readl(SMSM_STATE_ADDR(SMSM_MODEM_STATE));
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002052
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002053 old_apps = apps = __raw_readl(SMSM_STATE_ADDR(SMSM_APPS_STATE));
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002054
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002055 SMSM_DBG("<SM %08x %08x>\n", apps, modm);
2056 if (apps & SMSM_RESET) {
2057 /* If we get an interrupt and the apps SMSM_RESET
2058 bit is already set, the modem is acking the
2059 app's reset ack. */
Stepan Moskovchenkoa1ca7582011-10-25 14:45:09 -07002060 if (!cpu_is_msm8960() && !cpu_is_msm8930())
Angshuman Sarkaread67bd2011-09-21 20:13:12 +05302061 apps &= ~SMSM_RESET;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002062 /* Issue a fake irq to handle any
2063 * smd state changes during reset
2064 */
2065 smd_fake_irq_handler(0);
Brian Swetland5b0f5a32009-04-26 18:38:49 -07002066
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002067 /* queue modem restart notify chain */
2068 modem_queue_start_reset_notify();
2069
2070 } else if (modm & SMSM_RESET) {
Stepan Moskovchenkoa1ca7582011-10-25 14:45:09 -07002071 if (!cpu_is_msm8960() && !cpu_is_msm8930())
Angshuman Sarkaread67bd2011-09-21 20:13:12 +05302072 apps |= SMSM_RESET;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002073
2074 pr_err("\nSMSM: Modem SMSM state changed to SMSM_RESET.");
2075 modem_queue_start_reset_notify();
2076
2077 } else if (modm & SMSM_INIT) {
2078 if (!(apps & SMSM_INIT)) {
2079 apps |= SMSM_INIT;
2080 modem_queue_smsm_init_notify();
2081 }
2082
2083 if (modm & SMSM_SMDINIT)
2084 apps |= SMSM_SMDINIT;
2085 if ((apps & (SMSM_INIT | SMSM_SMDINIT | SMSM_RPCINIT)) ==
2086 (SMSM_INIT | SMSM_SMDINIT | SMSM_RPCINIT))
2087 apps |= SMSM_RUN;
2088 } else if (modm & SMSM_SYSTEM_DOWNLOAD) {
2089 pr_err("\nSMSM: Modem SMSM state changed to SMSM_SYSTEM_DOWNLOAD.");
2090 modem_queue_start_reset_notify();
2091 }
2092
2093 if (old_apps != apps) {
2094 SMSM_DBG("<SM %08x NOTIFY>\n", apps);
2095 __raw_writel(apps, SMSM_STATE_ADDR(SMSM_APPS_STATE));
2096 do_smd_probe();
2097 notify_other_smsm(SMSM_APPS_STATE, (old_apps ^ apps));
2098 }
2099
2100 schedule_work(&smsm_cb_work);
2101 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002102 spin_unlock_irqrestore(&smem_lock, flags);
2103 return IRQ_HANDLED;
2104}
2105
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002106int smsm_change_intr_mask(uint32_t smsm_entry,
2107 uint32_t clear_mask, uint32_t set_mask)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002108{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002109 uint32_t old_mask, new_mask;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002110 unsigned long flags;
Brian Swetland5b0f5a32009-04-26 18:38:49 -07002111
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002112 if (smsm_entry >= SMSM_NUM_ENTRIES) {
2113 pr_err("smsm_change_state: Invalid entry %d\n",
2114 smsm_entry);
2115 return -EINVAL;
2116 }
2117
2118 if (!smsm_info.intr_mask) {
2119 pr_err("smsm_change_intr_mask <SM NO STATE>\n");
Brian Swetland5b0f5a32009-04-26 18:38:49 -07002120 return -EIO;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002121 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002122
2123 spin_lock_irqsave(&smem_lock, flags);
2124
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002125 old_mask = __raw_readl(SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_APPS));
2126 new_mask = (old_mask & ~clear_mask) | set_mask;
2127 __raw_writel(new_mask, SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_APPS));
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002128
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002129 wmb();
2130 spin_unlock_irqrestore(&smem_lock, flags);
Brian Swetland5b0f5a32009-04-26 18:38:49 -07002131
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002132 return 0;
2133}
2134EXPORT_SYMBOL(smsm_change_intr_mask);
2135
2136int smsm_get_intr_mask(uint32_t smsm_entry, uint32_t *intr_mask)
2137{
2138 if (smsm_entry >= SMSM_NUM_ENTRIES) {
2139 pr_err("smsm_change_state: Invalid entry %d\n",
2140 smsm_entry);
2141 return -EINVAL;
2142 }
2143
2144 if (!smsm_info.intr_mask) {
2145 pr_err("smsm_change_intr_mask <SM NO STATE>\n");
2146 return -EIO;
2147 }
2148
2149 *intr_mask = __raw_readl(SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_APPS));
2150 return 0;
2151}
2152EXPORT_SYMBOL(smsm_get_intr_mask);
2153
2154int smsm_change_state(uint32_t smsm_entry,
2155 uint32_t clear_mask, uint32_t set_mask)
2156{
2157 unsigned long flags;
2158 uint32_t old_state, new_state;
2159
2160 if (smsm_entry >= SMSM_NUM_ENTRIES) {
2161 pr_err("smsm_change_state: Invalid entry %d",
2162 smsm_entry);
2163 return -EINVAL;
2164 }
2165
2166 if (!smsm_info.state) {
2167 pr_err("smsm_change_state <SM NO STATE>\n");
2168 return -EIO;
2169 }
2170 spin_lock_irqsave(&smem_lock, flags);
2171
2172 old_state = __raw_readl(SMSM_STATE_ADDR(smsm_entry));
2173 new_state = (old_state & ~clear_mask) | set_mask;
2174 __raw_writel(new_state, SMSM_STATE_ADDR(smsm_entry));
2175 SMSM_DBG("smsm_change_state %x\n", new_state);
2176 notify_other_smsm(SMSM_APPS_STATE, (old_state ^ new_state));
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002177
2178 spin_unlock_irqrestore(&smem_lock, flags);
2179
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002180 return 0;
2181}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002182EXPORT_SYMBOL(smsm_change_state);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002183
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002184uint32_t smsm_get_state(uint32_t smsm_entry)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002185{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002186 uint32_t rv = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002187
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002188 /* needs interface change to return error code */
2189 if (smsm_entry >= SMSM_NUM_ENTRIES) {
2190 pr_err("smsm_change_state: Invalid entry %d",
2191 smsm_entry);
2192 return 0;
2193 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002194
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002195 if (!smsm_info.state) {
2196 pr_err("smsm_get_state <SM NO STATE>\n");
2197 } else {
2198 rv = __raw_readl(SMSM_STATE_ADDR(smsm_entry));
2199 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002200
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002201 return rv;
2202}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002203EXPORT_SYMBOL(smsm_get_state);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002204
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002205/**
2206 * Performs SMSM callback client notifiction.
2207 */
2208void notify_smsm_cb_clients_worker(struct work_struct *work)
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -07002209{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002210 struct smsm_state_cb_info *cb_info;
2211 struct smsm_state_info *state_info;
2212 int n;
2213 uint32_t new_state;
2214 uint32_t state_changes;
Brian Swetland03e00cd2009-07-01 17:58:37 -07002215
Eric Holmbergc8002902011-09-16 13:55:57 -06002216 mutex_lock(&smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002217
2218 if (!smsm_states) {
2219 /* smsm not yet initialized */
Eric Holmbergc8002902011-09-16 13:55:57 -06002220 mutex_unlock(&smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002221 return;
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -07002222 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002223
2224 for (n = 0; n < SMSM_NUM_ENTRIES; n++) {
2225 state_info = &smsm_states[n];
2226 new_state = __raw_readl(SMSM_STATE_ADDR(n));
2227
2228 if (new_state != state_info->last_value) {
2229 state_changes = state_info->last_value ^ new_state;
2230
2231 list_for_each_entry(cb_info,
2232 &state_info->callbacks, cb_list) {
2233
2234 if (cb_info->mask & state_changes)
2235 cb_info->notify(cb_info->data,
2236 state_info->last_value,
2237 new_state);
2238 }
2239 state_info->last_value = new_state;
2240 }
2241 }
2242
Eric Holmbergc8002902011-09-16 13:55:57 -06002243 mutex_unlock(&smsm_lock);
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -07002244}
2245
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -07002246
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002247/**
2248 * Registers callback for SMSM state notifications when the specified
2249 * bits change.
2250 *
2251 * @smsm_entry Processor entry to deregister
2252 * @mask Bits to deregister (if result is 0, callback is removed)
2253 * @notify Notification function to deregister
2254 * @data Opaque data passed in to callback
2255 *
2256 * @returns Status code
2257 * <0 error code
2258 * 0 inserted new entry
2259 * 1 updated mask of existing entry
2260 */
2261int smsm_state_cb_register(uint32_t smsm_entry, uint32_t mask,
2262 void (*notify)(void *, uint32_t, uint32_t), void *data)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002263{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002264 struct smsm_state_cb_info *cb_info;
2265 struct smsm_state_cb_info *cb_found = 0;
2266 int ret = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002267
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002268 if (smsm_entry >= SMSM_NUM_ENTRIES)
2269 return -EINVAL;
2270
Eric Holmbergc8002902011-09-16 13:55:57 -06002271 mutex_lock(&smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002272
2273 if (!smsm_states) {
2274 /* smsm not yet initialized */
2275 ret = -ENODEV;
2276 goto cleanup;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002277 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002278
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002279 list_for_each_entry(cb_info,
2280 &smsm_states[smsm_entry].callbacks, cb_list) {
2281 if ((cb_info->notify == notify) &&
2282 (cb_info->data == data)) {
2283 cb_info->mask |= mask;
2284 cb_found = cb_info;
2285 ret = 1;
Brian Swetland5b0f5a32009-04-26 18:38:49 -07002286 break;
2287 }
2288 }
2289
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002290 if (!cb_found) {
2291 cb_info = kmalloc(sizeof(struct smsm_state_cb_info),
2292 GFP_ATOMIC);
2293 if (!cb_info) {
2294 ret = -ENOMEM;
2295 goto cleanup;
2296 }
2297
2298 cb_info->mask = mask;
2299 cb_info->notify = notify;
2300 cb_info->data = data;
2301 INIT_LIST_HEAD(&cb_info->cb_list);
2302 list_add_tail(&cb_info->cb_list,
2303 &smsm_states[smsm_entry].callbacks);
2304 }
2305
2306cleanup:
Eric Holmbergc8002902011-09-16 13:55:57 -06002307 mutex_unlock(&smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002308 return ret;
2309}
2310EXPORT_SYMBOL(smsm_state_cb_register);
2311
2312
2313/**
2314 * Deregisters for SMSM state notifications for the specified bits.
2315 *
2316 * @smsm_entry Processor entry to deregister
2317 * @mask Bits to deregister (if result is 0, callback is removed)
2318 * @notify Notification function to deregister
2319 * @data Opaque data passed in to callback
2320 *
2321 * @returns Status code
2322 * <0 error code
2323 * 0 not found
2324 * 1 updated mask
2325 * 2 removed callback
2326 */
2327int smsm_state_cb_deregister(uint32_t smsm_entry, uint32_t mask,
2328 void (*notify)(void *, uint32_t, uint32_t), void *data)
2329{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002330 struct smsm_state_cb_info *cb_info;
2331 int ret = 0;
2332
2333 if (smsm_entry >= SMSM_NUM_ENTRIES)
2334 return -EINVAL;
2335
Eric Holmbergc8002902011-09-16 13:55:57 -06002336 mutex_lock(&smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002337
2338 if (!smsm_states) {
2339 /* smsm not yet initialized */
Eric Holmbergc8002902011-09-16 13:55:57 -06002340 mutex_unlock(&smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002341 return -ENODEV;
2342 }
2343
2344 list_for_each_entry(cb_info,
2345 &smsm_states[smsm_entry].callbacks, cb_list) {
2346 if ((cb_info->notify == notify) &&
2347 (cb_info->data == data)) {
2348 cb_info->mask &= ~mask;
2349 ret = 1;
2350 if (!cb_info->mask) {
2351 /* no mask bits set, remove callback */
2352 list_del(&cb_info->cb_list);
2353 kfree(cb_info);
2354 ret = 2;
2355 }
2356 break;
2357 }
2358 }
2359
Eric Holmbergc8002902011-09-16 13:55:57 -06002360 mutex_unlock(&smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002361 return ret;
2362}
2363EXPORT_SYMBOL(smsm_state_cb_deregister);
2364
2365
2366int smd_core_init(void)
2367{
2368 int r;
2369 unsigned long flags = IRQF_TRIGGER_RISING;
2370 SMD_INFO("smd_core_init()\n");
Brian Swetland5b0f5a32009-04-26 18:38:49 -07002371
Brian Swetland37521a32009-07-01 18:30:47 -07002372 r = request_irq(INT_A9_M2A_0, smd_modem_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002373 flags, "smd_dev", 0);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002374 if (r < 0)
2375 return r;
2376 r = enable_irq_wake(INT_A9_M2A_0);
2377 if (r < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002378 pr_err("smd_core_init: "
2379 "enable_irq_wake failed for INT_A9_M2A_0\n");
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002380
2381 r = request_irq(INT_A9_M2A_5, smsm_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002382 flags, "smsm_dev", 0);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002383 if (r < 0) {
2384 free_irq(INT_A9_M2A_0, 0);
2385 return r;
2386 }
2387 r = enable_irq_wake(INT_A9_M2A_5);
2388 if (r < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002389 pr_err("smd_core_init: "
2390 "enable_irq_wake failed for INT_A9_M2A_5\n");
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002391
Brian Swetland37521a32009-07-01 18:30:47 -07002392#if defined(CONFIG_QDSP6)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002393#if (INT_ADSP_A11 == INT_ADSP_A11_SMSM)
2394 flags |= IRQF_SHARED;
2395#endif
Brian Swetland37521a32009-07-01 18:30:47 -07002396 r = request_irq(INT_ADSP_A11, smd_dsp_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002397 flags, "smd_dev", smd_dsp_irq_handler);
Brian Swetland37521a32009-07-01 18:30:47 -07002398 if (r < 0) {
2399 free_irq(INT_A9_M2A_0, 0);
2400 free_irq(INT_A9_M2A_5, 0);
2401 return r;
2402 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002403
2404 r = request_irq(INT_ADSP_A11_SMSM, smsm_irq_handler,
2405 flags, "smsm_dev", smsm_irq_handler);
2406 if (r < 0) {
2407 free_irq(INT_A9_M2A_0, 0);
2408 free_irq(INT_A9_M2A_5, 0);
2409 free_irq(INT_ADSP_A11, smd_dsp_irq_handler);
2410 return r;
2411 }
2412
2413 r = enable_irq_wake(INT_ADSP_A11);
2414 if (r < 0)
2415 pr_err("smd_core_init: "
2416 "enable_irq_wake failed for INT_ADSP_A11\n");
2417
2418#if (INT_ADSP_A11 != INT_ADSP_A11_SMSM)
2419 r = enable_irq_wake(INT_ADSP_A11_SMSM);
2420 if (r < 0)
2421 pr_err("smd_core_init: enable_irq_wake "
2422 "failed for INT_ADSP_A11_SMSM\n");
2423#endif
2424 flags &= ~IRQF_SHARED;
Brian Swetland37521a32009-07-01 18:30:47 -07002425#endif
2426
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002427#if defined(CONFIG_DSPS)
2428 r = request_irq(INT_DSPS_A11, smd_dsps_irq_handler,
2429 flags, "smd_dev", smd_dsps_irq_handler);
2430 if (r < 0) {
2431 free_irq(INT_A9_M2A_0, 0);
2432 free_irq(INT_A9_M2A_5, 0);
2433 free_irq(INT_ADSP_A11, smd_dsp_irq_handler);
2434 free_irq(INT_ADSP_A11_SMSM, smsm_irq_handler);
2435 return r;
2436 }
Brian Swetland5b0f5a32009-04-26 18:38:49 -07002437
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002438 r = enable_irq_wake(INT_DSPS_A11);
2439 if (r < 0)
2440 pr_err("smd_core_init: "
2441 "enable_irq_wake failed for INT_ADSP_A11\n");
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -07002442#endif
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002443
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002444#if defined(CONFIG_WCNSS)
2445 r = request_irq(INT_WCNSS_A11, smd_wcnss_irq_handler,
2446 flags, "smd_dev", smd_wcnss_irq_handler);
2447 if (r < 0) {
2448 free_irq(INT_A9_M2A_0, 0);
2449 free_irq(INT_A9_M2A_5, 0);
2450 free_irq(INT_ADSP_A11, smd_dsp_irq_handler);
2451 free_irq(INT_ADSP_A11_SMSM, smsm_irq_handler);
2452 free_irq(INT_DSPS_A11, smd_dsps_irq_handler);
2453 return r;
2454 }
2455
2456 r = enable_irq_wake(INT_WCNSS_A11);
2457 if (r < 0)
2458 pr_err("smd_core_init: "
2459 "enable_irq_wake failed for INT_WCNSS_A11\n");
2460
2461 r = request_irq(INT_WCNSS_A11_SMSM, smsm_irq_handler,
2462 flags, "smsm_dev", smsm_irq_handler);
2463 if (r < 0) {
2464 free_irq(INT_A9_M2A_0, 0);
2465 free_irq(INT_A9_M2A_5, 0);
2466 free_irq(INT_ADSP_A11, smd_dsp_irq_handler);
2467 free_irq(INT_ADSP_A11_SMSM, smsm_irq_handler);
2468 free_irq(INT_DSPS_A11, smd_dsps_irq_handler);
2469 free_irq(INT_WCNSS_A11, smd_wcnss_irq_handler);
2470 return r;
2471 }
2472
2473 r = enable_irq_wake(INT_WCNSS_A11_SMSM);
2474 if (r < 0)
2475 pr_err("smd_core_init: "
2476 "enable_irq_wake failed for INT_WCNSS_A11_SMSM\n");
2477#endif
2478
Jeff Hugo6a8057c2011-08-16 13:47:12 -06002479#if defined(CONFIG_DSPS_SMSM)
2480 r = request_irq(INT_DSPS_A11_SMSM, smsm_irq_handler,
2481 flags, "smsm_dev", smsm_irq_handler);
2482 if (r < 0) {
2483 free_irq(INT_A9_M2A_0, 0);
2484 free_irq(INT_A9_M2A_5, 0);
2485 free_irq(INT_ADSP_A11, smd_dsp_irq_handler);
2486 free_irq(INT_ADSP_A11_SMSM, smsm_irq_handler);
2487 free_irq(INT_DSPS_A11, smd_dsps_irq_handler);
2488 free_irq(INT_WCNSS_A11, smd_wcnss_irq_handler);
2489 free_irq(INT_WCNSS_A11_SMSM, smsm_irq_handler);
2490 return r;
2491 }
2492
2493 r = enable_irq_wake(INT_DSPS_A11_SMSM);
2494 if (r < 0)
2495 pr_err("smd_core_init: "
2496 "enable_irq_wake failed for INT_DSPS_A11_SMSM\n");
2497#endif
2498
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002499 /* we may have missed a signal while booting -- fake
2500 * an interrupt to make sure we process any existing
2501 * state
2502 */
2503 smsm_irq_handler(0, 0);
2504
2505 SMD_INFO("smd_core_init() done\n");
2506
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002507 return 0;
2508}
2509
Gregory Bean4416e9e2010-07-28 10:22:12 -07002510static int __devinit msm_smd_probe(struct platform_device *pdev)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002511{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002512 SMD_INFO("smd probe\n");
Daniel Walker0aec66d2010-03-18 12:31:08 -07002513
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002514 INIT_WORK(&probe_work, smd_channel_probe_worker);
2515
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002516 channel_close_wq = create_singlethread_workqueue("smd_channel_close");
2517 if (IS_ERR(channel_close_wq)) {
2518 pr_err("%s: create_singlethread_workqueue ENOMEM\n", __func__);
2519 return -ENOMEM;
2520 }
2521
2522 if (smsm_init()) {
2523 pr_err("smsm_init() failed\n");
2524 return -1;
2525 }
2526
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002527 if (smd_core_init()) {
2528 pr_err("smd_core_init() failed\n");
2529 return -1;
2530 }
2531
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002532 smd_initialized = 1;
2533
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002534 smd_alloc_loopback_channel();
Eric Holmbergc33d4ab2011-10-24 10:28:25 -06002535 smsm_irq_handler(0, 0);
2536 tasklet_schedule(&smd_fake_irq_tasklet);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002537
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002538 return 0;
2539}
2540
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002541static int restart_notifier_cb(struct notifier_block *this,
2542 unsigned long code,
2543 void *data);
2544
2545static struct restart_notifier_block restart_notifiers[] = {
Eric Holmbergca7ead22011-12-01 17:21:15 -07002546 {SMD_MODEM, "modem", .nb.notifier_call = restart_notifier_cb},
2547 {SMD_Q6, "lpass", .nb.notifier_call = restart_notifier_cb},
2548 {SMD_WCNSS, "riva", .nb.notifier_call = restart_notifier_cb},
2549 {SMD_DSPS, "dsps", .nb.notifier_call = restart_notifier_cb},
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002550};
2551
2552static int restart_notifier_cb(struct notifier_block *this,
2553 unsigned long code,
2554 void *data)
2555{
2556 if (code == SUBSYS_AFTER_SHUTDOWN) {
2557 struct restart_notifier_block *notifier;
2558
2559 notifier = container_of(this,
2560 struct restart_notifier_block, nb);
2561 SMD_INFO("%s: ssrestart for processor %d ('%s')\n",
2562 __func__, notifier->processor,
2563 notifier->name);
2564
2565 smd_channel_reset(notifier->processor);
2566 }
2567
2568 return NOTIFY_DONE;
2569}
2570
2571static __init int modem_restart_late_init(void)
2572{
2573 int i;
2574 void *handle;
2575 struct restart_notifier_block *nb;
2576
2577 for (i = 0; i < ARRAY_SIZE(restart_notifiers); i++) {
2578 nb = &restart_notifiers[i];
2579 handle = subsys_notif_register_notifier(nb->name, &nb->nb);
2580 SMD_DBG("%s: registering notif for '%s', handle=%p\n",
2581 __func__, nb->name, handle);
2582 }
2583 return 0;
2584}
2585late_initcall(modem_restart_late_init);
2586
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002587static struct platform_driver msm_smd_driver = {
2588 .probe = msm_smd_probe,
2589 .driver = {
2590 .name = MODULE_NAME,
2591 .owner = THIS_MODULE,
2592 },
2593};
2594
2595static int __init msm_smd_init(void)
2596{
2597 return platform_driver_register(&msm_smd_driver);
2598}
2599
2600module_init(msm_smd_init);
2601
2602MODULE_DESCRIPTION("MSM Shared Memory Core");
2603MODULE_AUTHOR("Brian Swetland <swetland@google.com>");
2604MODULE_LICENSE("GPL");