blob: d7f2623e5129c7706562e297d4c221237ff9c417 [file] [log] [blame]
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001/* arch/arm/mach-msm/smd.c
2 *
3 * Copyright (C) 2007 Google, Inc.
Eric Holmbergc7e8daf2011-12-28 11:49:21 -07004 * Copyright (c) 2008-2012, 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>
Eric Holmbergc7e8daf2011-12-28 11:49:21 -070034#include <linux/kfifo.h>
Brian Swetland2eb44eb2008-09-29 16:00:48 -070035#include <mach/msm_smd.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070036#include <mach/msm_iomap.h>
Brian Swetland2eb44eb2008-09-29 16:00:48 -070037#include <mach/system.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070038#include <mach/subsystem_notif.h>
Angshuman Sarkaread67bd2011-09-21 20:13:12 +053039#include <mach/socinfo.h>
Brian Swetland2eb44eb2008-09-29 16:00:48 -070040
41#include "smd_private.h"
42#include "proc_comm.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070043#include "modem_notifier.h"
Brian Swetland2eb44eb2008-09-29 16:00:48 -070044
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070045#if defined(CONFIG_ARCH_QSD8X50) || defined(CONFIG_ARCH_MSM8X60) \
Jeff Hugo56b933a2011-09-28 14:42:05 -060046 || defined(CONFIG_ARCH_MSM8960) || defined(CONFIG_ARCH_FSM9XXX) \
Jeff Hugo0c0f5e92011-09-28 13:55:45 -060047 || defined(CONFIG_ARCH_MSM9615) || defined(CONFIG_ARCH_APQ8064)
Brian Swetland37521a32009-07-01 18:30:47 -070048#define CONFIG_QDSP6 1
49#endif
50
Jeff Hugo0c0f5e92011-09-28 13:55:45 -060051#if defined(CONFIG_ARCH_MSM8X60) || defined(CONFIG_ARCH_MSM8960) \
52 || defined(CONFIG_ARCH_APQ8064)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070053#define CONFIG_DSPS 1
54#endif
55
Jeff Hugo0c0f5e92011-09-28 13:55:45 -060056#if defined(CONFIG_ARCH_MSM8960) \
57 || defined(CONFIG_ARCH_APQ8064)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070058#define CONFIG_WCNSS 1
Jeff Hugo6a8057c2011-08-16 13:47:12 -060059#define CONFIG_DSPS_SMSM 1
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070060#endif
Brian Swetland2eb44eb2008-09-29 16:00:48 -070061
62#define MODULE_NAME "msm_smd"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070063#define SMEM_VERSION 0x000B
64#define SMD_VERSION 0x00020000
Eric Holmbergc7e8daf2011-12-28 11:49:21 -070065#define SMSM_SNAPSHOT_CNT 64
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070066
67uint32_t SMSM_NUM_ENTRIES = 8;
68uint32_t SMSM_NUM_HOSTS = 3;
Brian Swetland2eb44eb2008-09-29 16:00:48 -070069
70enum {
71 MSM_SMD_DEBUG = 1U << 0,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070072 MSM_SMSM_DEBUG = 1U << 1,
73 MSM_SMD_INFO = 1U << 2,
74 MSM_SMSM_INFO = 1U << 3,
Eric Holmberg98c6c642012-02-24 11:29:35 -070075 MSM_SMx_POWER_INFO = 1U << 4,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070076};
77
78struct smsm_shared_info {
79 uint32_t *state;
80 uint32_t *intr_mask;
81 uint32_t *intr_mux;
82};
83
84static struct smsm_shared_info smsm_info;
Eric Holmbergc7e8daf2011-12-28 11:49:21 -070085struct kfifo smsm_snapshot_fifo;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070086
87struct smsm_size_info_type {
88 uint32_t num_hosts;
89 uint32_t num_entries;
90 uint32_t reserved0;
91 uint32_t reserved1;
92};
93
94struct smsm_state_cb_info {
95 struct list_head cb_list;
96 uint32_t mask;
97 void *data;
98 void (*notify)(void *data, uint32_t old_state, uint32_t new_state);
99};
100
101struct smsm_state_info {
102 struct list_head callbacks;
103 uint32_t last_value;
104};
105
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530106struct interrupt_config_item {
107 /* must be initialized */
108 irqreturn_t (*irq_handler)(int req, void *data);
109 /* outgoing interrupt config (set from platform data) */
110 uint32_t out_bit_pos;
111 void __iomem *out_base;
112 uint32_t out_offset;
113};
114
115struct interrupt_config {
116 struct interrupt_config_item smd;
117 struct interrupt_config_item smsm;
118};
119
120static irqreturn_t smd_modem_irq_handler(int irq, void *data);
Eric Holmberg98c6c642012-02-24 11:29:35 -0700121static irqreturn_t smsm_modem_irq_handler(int irq, void *data);
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530122static irqreturn_t smd_dsp_irq_handler(int irq, void *data);
Eric Holmberg98c6c642012-02-24 11:29:35 -0700123static irqreturn_t smsm_dsp_irq_handler(int irq, void *data);
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530124static irqreturn_t smd_dsps_irq_handler(int irq, void *data);
Eric Holmberg98c6c642012-02-24 11:29:35 -0700125static irqreturn_t smsm_dsps_irq_handler(int irq, void *data);
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530126static irqreturn_t smd_wcnss_irq_handler(int irq, void *data);
Eric Holmberg98c6c642012-02-24 11:29:35 -0700127static irqreturn_t smsm_wcnss_irq_handler(int irq, void *data);
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530128static irqreturn_t smsm_irq_handler(int irq, void *data);
129
130static struct interrupt_config private_intr_config[NUM_SMD_SUBSYSTEMS] = {
131 [SMD_MODEM] = {
132 .smd.irq_handler = smd_modem_irq_handler,
Eric Holmberg98c6c642012-02-24 11:29:35 -0700133 .smsm.irq_handler = smsm_modem_irq_handler,
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530134 },
135 [SMD_Q6] = {
136 .smd.irq_handler = smd_dsp_irq_handler,
Eric Holmberg98c6c642012-02-24 11:29:35 -0700137 .smsm.irq_handler = smsm_dsp_irq_handler,
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530138 },
139 [SMD_DSPS] = {
140 .smd.irq_handler = smd_dsps_irq_handler,
Eric Holmberg98c6c642012-02-24 11:29:35 -0700141 .smsm.irq_handler = smsm_dsps_irq_handler,
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530142 },
143 [SMD_WCNSS] = {
144 .smd.irq_handler = smd_wcnss_irq_handler,
Eric Holmberg98c6c642012-02-24 11:29:35 -0700145 .smsm.irq_handler = smsm_wcnss_irq_handler,
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530146 },
147};
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700148struct interrupt_stat interrupt_stats[NUM_SMD_SUBSYSTEMS];
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530149
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700150#define SMSM_STATE_ADDR(entry) (smsm_info.state + entry)
151#define SMSM_INTR_MASK_ADDR(entry, host) (smsm_info.intr_mask + \
152 entry * SMSM_NUM_HOSTS + host)
153#define SMSM_INTR_MUX_ADDR(entry) (smsm_info.intr_mux + entry)
154
155/* Internal definitions which are not exported in some targets */
156enum {
157 SMSM_APPS_DEM_I = 3,
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700158};
159
160static int msm_smd_debug_mask;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700161module_param_named(debug_mask, msm_smd_debug_mask,
162 int, S_IRUGO | S_IWUSR | S_IWGRP);
163
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700164#if defined(CONFIG_MSM_SMD_DEBUG)
165#define SMD_DBG(x...) do { \
166 if (msm_smd_debug_mask & MSM_SMD_DEBUG) \
167 printk(KERN_DEBUG x); \
168 } while (0)
169
170#define SMSM_DBG(x...) do { \
171 if (msm_smd_debug_mask & MSM_SMSM_DEBUG) \
172 printk(KERN_DEBUG x); \
173 } while (0)
174
175#define SMD_INFO(x...) do { \
176 if (msm_smd_debug_mask & MSM_SMD_INFO) \
177 printk(KERN_INFO x); \
178 } while (0)
179
180#define SMSM_INFO(x...) do { \
181 if (msm_smd_debug_mask & MSM_SMSM_INFO) \
182 printk(KERN_INFO x); \
183 } while (0)
Eric Holmberg98c6c642012-02-24 11:29:35 -0700184#define SMx_POWER_INFO(x...) do { \
185 if (msm_smd_debug_mask & MSM_SMx_POWER_INFO) \
186 printk(KERN_INFO x); \
187 } while (0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700188#else
189#define SMD_DBG(x...) do { } while (0)
190#define SMSM_DBG(x...) do { } while (0)
191#define SMD_INFO(x...) do { } while (0)
192#define SMSM_INFO(x...) do { } while (0)
Eric Holmberg98c6c642012-02-24 11:29:35 -0700193#define SMx_POWER_INFO(x...) do { } while (0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700194#endif
195
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700196static unsigned last_heap_free = 0xffffffff;
197
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700198static inline void smd_write_intr(unsigned int val,
199 const void __iomem *addr);
200
201#if defined(CONFIG_ARCH_MSM7X30)
202#define MSM_TRIG_A2M_SMD_INT \
203 (smd_write_intr(1 << 0, MSM_GCC_BASE + 0x8))
204#define MSM_TRIG_A2Q6_SMD_INT \
205 (smd_write_intr(1 << 8, MSM_GCC_BASE + 0x8))
206#define MSM_TRIG_A2M_SMSM_INT \
207 (smd_write_intr(1 << 5, MSM_GCC_BASE + 0x8))
208#define MSM_TRIG_A2Q6_SMSM_INT \
209 (smd_write_intr(1 << 8, MSM_GCC_BASE + 0x8))
210#define MSM_TRIG_A2DSPS_SMD_INT
Jeff Hugo6a8057c2011-08-16 13:47:12 -0600211#define MSM_TRIG_A2DSPS_SMSM_INT
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700212#define MSM_TRIG_A2WCNSS_SMD_INT
213#define MSM_TRIG_A2WCNSS_SMSM_INT
214#elif defined(CONFIG_ARCH_MSM8X60)
215#define MSM_TRIG_A2M_SMD_INT \
216 (smd_write_intr(1 << 3, MSM_GCC_BASE + 0x8))
217#define MSM_TRIG_A2Q6_SMD_INT \
218 (smd_write_intr(1 << 15, MSM_GCC_BASE + 0x8))
219#define MSM_TRIG_A2M_SMSM_INT \
220 (smd_write_intr(1 << 4, MSM_GCC_BASE + 0x8))
221#define MSM_TRIG_A2Q6_SMSM_INT \
222 (smd_write_intr(1 << 14, MSM_GCC_BASE + 0x8))
223#define MSM_TRIG_A2DSPS_SMD_INT \
224 (smd_write_intr(1, MSM_SIC_NON_SECURE_BASE + 0x4080))
Jeff Hugo6a8057c2011-08-16 13:47:12 -0600225#define MSM_TRIG_A2DSPS_SMSM_INT
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700226#define MSM_TRIG_A2WCNSS_SMD_INT
227#define MSM_TRIG_A2WCNSS_SMSM_INT
Jeff Hugo56b933a2011-09-28 14:42:05 -0600228#elif defined(CONFIG_ARCH_MSM9615)
229#define MSM_TRIG_A2M_SMD_INT \
230 (smd_write_intr(1 << 3, MSM_APCS_GCC_BASE + 0x8))
231#define MSM_TRIG_A2Q6_SMD_INT \
232 (smd_write_intr(1 << 15, MSM_APCS_GCC_BASE + 0x8))
233#define MSM_TRIG_A2M_SMSM_INT \
234 (smd_write_intr(1 << 4, MSM_APCS_GCC_BASE + 0x8))
235#define MSM_TRIG_A2Q6_SMSM_INT \
236 (smd_write_intr(1 << 14, MSM_APCS_GCC_BASE + 0x8))
237#define MSM_TRIG_A2DSPS_SMD_INT
238#define MSM_TRIG_A2DSPS_SMSM_INT
239#define MSM_TRIG_A2WCNSS_SMD_INT
240#define MSM_TRIG_A2WCNSS_SMSM_INT
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700241#elif defined(CONFIG_ARCH_FSM9XXX)
242#define MSM_TRIG_A2Q6_SMD_INT \
243 (smd_write_intr(1 << 10, MSM_GCC_BASE + 0x8))
244#define MSM_TRIG_A2Q6_SMSM_INT \
245 (smd_write_intr(1 << 10, MSM_GCC_BASE + 0x8))
246#define MSM_TRIG_A2M_SMD_INT \
247 (smd_write_intr(1 << 0, MSM_GCC_BASE + 0x8))
248#define MSM_TRIG_A2M_SMSM_INT \
249 (smd_write_intr(1 << 5, MSM_GCC_BASE + 0x8))
250#define MSM_TRIG_A2DSPS_SMD_INT
Jeff Hugo6a8057c2011-08-16 13:47:12 -0600251#define MSM_TRIG_A2DSPS_SMSM_INT
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700252#define MSM_TRIG_A2WCNSS_SMD_INT
253#define MSM_TRIG_A2WCNSS_SMSM_INT
Eric Holmberg73d45462012-02-28 10:41:31 -0700254#elif defined(CONFIG_ARCH_MSM7X01A) || defined(CONFIG_ARCH_MSM7x25)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700255#define MSM_TRIG_A2M_SMD_INT \
256 (smd_write_intr(1, MSM_CSR_BASE + 0x400 + (0) * 4))
Eric Holmberg73d45462012-02-28 10:41:31 -0700257#define MSM_TRIG_A2Q6_SMD_INT
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700258#define MSM_TRIG_A2M_SMSM_INT \
259 (smd_write_intr(1, MSM_CSR_BASE + 0x400 + (5) * 4))
Eric Holmberg73d45462012-02-28 10:41:31 -0700260#define MSM_TRIG_A2Q6_SMSM_INT
261#define MSM_TRIG_A2DSPS_SMD_INT
262#define MSM_TRIG_A2DSPS_SMSM_INT
263#define MSM_TRIG_A2WCNSS_SMD_INT
264#define MSM_TRIG_A2WCNSS_SMSM_INT
265#elif defined(CONFIG_ARCH_MSM7X27) || defined(CONFIG_ARCH_MSM7X27A)
266#define MSM_TRIG_A2M_SMD_INT \
267 (smd_write_intr(1, MSM_CSR_BASE + 0x400 + (0) * 4))
268#define MSM_TRIG_A2Q6_SMD_INT
269#define MSM_TRIG_A2M_SMSM_INT \
270 (smd_write_intr(1, MSM_CSR_BASE + 0x400 + (5) * 4))
271#define MSM_TRIG_A2Q6_SMSM_INT
272#define MSM_TRIG_A2DSPS_SMD_INT
273#define MSM_TRIG_A2DSPS_SMSM_INT
274#define MSM_TRIG_A2WCNSS_SMD_INT
275#define MSM_TRIG_A2WCNSS_SMSM_INT
276#else /* use platform device / device tree configuration */
277#define MSM_TRIG_A2M_SMD_INT
278#define MSM_TRIG_A2Q6_SMD_INT
279#define MSM_TRIG_A2M_SMSM_INT
280#define MSM_TRIG_A2Q6_SMSM_INT
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700281#define MSM_TRIG_A2DSPS_SMD_INT
Jeff Hugo6a8057c2011-08-16 13:47:12 -0600282#define MSM_TRIG_A2DSPS_SMSM_INT
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700283#define MSM_TRIG_A2WCNSS_SMD_INT
284#define MSM_TRIG_A2WCNSS_SMSM_INT
Brian Swetland37521a32009-07-01 18:30:47 -0700285#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700286
Jeff Hugoee40b152012-02-09 17:39:47 -0700287/*
288 * stub out legacy macros if they are not being used so that the legacy
289 * code compiles even though it is not used
290 *
291 * these definitions should not be used in active code and will cause
292 * an early failure
293 */
294#ifndef INT_A9_M2A_0
295#define INT_A9_M2A_0 -1
296#endif
297#ifndef INT_A9_M2A_5
298#define INT_A9_M2A_5 -1
299#endif
300#ifndef INT_ADSP_A11
301#define INT_ADSP_A11 -1
302#endif
303#ifndef INT_ADSP_A11_SMSM
304#define INT_ADSP_A11_SMSM -1
305#endif
306#ifndef INT_DSPS_A11
307#define INT_DSPS_A11 -1
308#endif
309#ifndef INT_DSPS_A11_SMSM
310#define INT_DSPS_A11_SMSM -1
311#endif
312#ifndef INT_WCNSS_A11
313#define INT_WCNSS_A11 -1
314#endif
315#ifndef INT_WCNSS_A11_SMSM
316#define INT_WCNSS_A11_SMSM -1
317#endif
318
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700319#define SMD_LOOPBACK_CID 100
320
Eric Holmbergf6d7d1a2011-09-23 18:31:04 -0600321#define SMEM_SPINLOCK_SMEM_ALLOC "S:3"
322static remote_spinlock_t remote_spinlock;
323
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700324static LIST_HEAD(smd_ch_list_loopback);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700325static void smd_fake_irq_handler(unsigned long arg);
Eric Holmbergc7e8daf2011-12-28 11:49:21 -0700326static void smsm_cb_snapshot(void);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700327
328static void notify_smsm_cb_clients_worker(struct work_struct *work);
329static DECLARE_WORK(smsm_cb_work, notify_smsm_cb_clients_worker);
Eric Holmbergc8002902011-09-16 13:55:57 -0600330static DEFINE_MUTEX(smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700331static struct smsm_state_info *smsm_states;
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +0530332static int spinlocks_initialized;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700333
334static inline void smd_write_intr(unsigned int val,
335 const void __iomem *addr)
336{
337 wmb();
338 __raw_writel(val, addr);
339}
340
341#ifdef CONFIG_WCNSS
342static inline void wakeup_v1_riva(void)
343{
344 /*
345 * workaround hack for RIVA v1 hardware bug
346 * trigger GPIO 40 to wake up RIVA from power collaspe
347 * not to be sent to customers
348 */
Jeff Hugoafb8c4a2011-10-27 15:57:27 -0600349 if (SOCINFO_VERSION_MAJOR(socinfo_get_version()) == 1) {
350 __raw_writel(0x0, MSM_TLMM_BASE + 0x1284);
351 __raw_writel(0x2, MSM_TLMM_BASE + 0x1284);
352 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700353 /* end workaround */
354}
355#else
356static inline void wakeup_v1_riva(void) {}
357#endif
358
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530359static inline void notify_modem_smd(void)
360{
361 static const struct interrupt_config_item *intr
362 = &private_intr_config[SMD_MODEM].smd;
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700363 if (intr->out_base) {
364 ++interrupt_stats[SMD_MODEM].smd_out_config_count;
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530365 smd_write_intr(intr->out_bit_pos,
366 intr->out_base + intr->out_offset);
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700367 } else {
368 ++interrupt_stats[SMD_MODEM].smd_out_hardcode_count;
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530369 MSM_TRIG_A2M_SMD_INT;
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700370 }
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530371}
372
373static inline void notify_dsp_smd(void)
374{
375 static const struct interrupt_config_item *intr
376 = &private_intr_config[SMD_Q6].smd;
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700377 if (intr->out_base) {
378 ++interrupt_stats[SMD_Q6].smd_out_config_count;
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530379 smd_write_intr(intr->out_bit_pos,
380 intr->out_base + intr->out_offset);
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700381 } else {
382 ++interrupt_stats[SMD_Q6].smd_out_hardcode_count;
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530383 MSM_TRIG_A2Q6_SMD_INT;
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700384 }
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530385}
386
387static inline void notify_dsps_smd(void)
388{
389 static const struct interrupt_config_item *intr
390 = &private_intr_config[SMD_DSPS].smd;
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700391 if (intr->out_base) {
392 ++interrupt_stats[SMD_DSPS].smd_out_config_count;
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530393 smd_write_intr(intr->out_bit_pos,
394 intr->out_base + intr->out_offset);
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700395 } else {
396 ++interrupt_stats[SMD_DSPS].smd_out_hardcode_count;
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530397 MSM_TRIG_A2DSPS_SMD_INT;
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700398 }
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530399}
400
401static inline void notify_wcnss_smd(void)
402{
403 static const struct interrupt_config_item *intr
404 = &private_intr_config[SMD_WCNSS].smd;
405 wakeup_v1_riva();
406
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700407 if (intr->out_base) {
408 ++interrupt_stats[SMD_WCNSS].smd_out_config_count;
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530409 smd_write_intr(intr->out_bit_pos,
410 intr->out_base + intr->out_offset);
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700411 } else {
412 ++interrupt_stats[SMD_WCNSS].smd_out_hardcode_count;
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530413 MSM_TRIG_A2WCNSS_SMD_INT;
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700414 }
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530415}
416
417static inline void notify_modem_smsm(void)
418{
419 static const struct interrupt_config_item *intr
420 = &private_intr_config[SMD_MODEM].smsm;
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700421 if (intr->out_base) {
422 ++interrupt_stats[SMD_MODEM].smsm_out_config_count;
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530423 smd_write_intr(intr->out_bit_pos,
424 intr->out_base + intr->out_offset);
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700425 } else {
426 ++interrupt_stats[SMD_MODEM].smsm_out_hardcode_count;
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530427 MSM_TRIG_A2M_SMSM_INT;
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700428 }
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530429}
430
431static inline void notify_dsp_smsm(void)
432{
433 static const struct interrupt_config_item *intr
434 = &private_intr_config[SMD_Q6].smsm;
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700435 if (intr->out_base) {
436 ++interrupt_stats[SMD_Q6].smsm_out_config_count;
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530437 smd_write_intr(intr->out_bit_pos,
438 intr->out_base + intr->out_offset);
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700439 } else {
440 ++interrupt_stats[SMD_Q6].smsm_out_hardcode_count;
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530441 MSM_TRIG_A2Q6_SMSM_INT;
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700442 }
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530443}
444
445static inline void notify_dsps_smsm(void)
446{
447 static const struct interrupt_config_item *intr
448 = &private_intr_config[SMD_DSPS].smsm;
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700449 if (intr->out_base) {
450 ++interrupt_stats[SMD_DSPS].smsm_out_config_count;
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530451 smd_write_intr(intr->out_bit_pos,
452 intr->out_base + intr->out_offset);
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700453 } else {
454 ++interrupt_stats[SMD_DSPS].smsm_out_hardcode_count;
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530455 MSM_TRIG_A2DSPS_SMSM_INT;
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700456 }
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530457}
458
459static inline void notify_wcnss_smsm(void)
460{
461 static const struct interrupt_config_item *intr
462 = &private_intr_config[SMD_WCNSS].smsm;
463 wakeup_v1_riva();
464
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700465 if (intr->out_base) {
466 ++interrupt_stats[SMD_WCNSS].smsm_out_config_count;
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530467 smd_write_intr(intr->out_bit_pos,
468 intr->out_base + intr->out_offset);
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700469 } else {
470 ++interrupt_stats[SMD_WCNSS].smsm_out_hardcode_count;
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530471 MSM_TRIG_A2WCNSS_SMSM_INT;
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700472 }
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530473}
474
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700475static void notify_other_smsm(uint32_t smsm_entry, uint32_t notify_mask)
476{
477 /* older protocol don't use smsm_intr_mask,
478 but still communicates with modem */
479 if (!smsm_info.intr_mask ||
480 (__raw_readl(SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_MODEM))
481 & notify_mask))
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530482 notify_modem_smsm();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700483
484 if (smsm_info.intr_mask &&
485 (__raw_readl(SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_Q6))
486 & notify_mask)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700487 uint32_t mux_val;
488
Eric Holmberg6282c5d2011-10-27 17:30:57 -0600489 if (cpu_is_qsd8x50() && smsm_info.intr_mux) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700490 mux_val = __raw_readl(
491 SMSM_INTR_MUX_ADDR(SMEM_APPS_Q6_SMSM));
492 mux_val++;
493 __raw_writel(mux_val,
494 SMSM_INTR_MUX_ADDR(SMEM_APPS_Q6_SMSM));
495 }
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530496 notify_dsp_smsm();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700497 }
498
499 if (smsm_info.intr_mask &&
500 (__raw_readl(SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_WCNSS))
501 & notify_mask)) {
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530502 notify_wcnss_smsm();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700503 }
504
Jeff Hugo6a8057c2011-08-16 13:47:12 -0600505 if (smsm_info.intr_mask &&
506 (__raw_readl(SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_DSPS))
507 & notify_mask)) {
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530508 notify_dsps_smsm();
Jeff Hugo6a8057c2011-08-16 13:47:12 -0600509 }
510
Eric Holmbergc7e8daf2011-12-28 11:49:21 -0700511 smsm_cb_snapshot();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700512}
513
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700514void smd_diag(void)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700515{
516 char *x;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700517 int size;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700518
519 x = smem_find(ID_DIAG_ERR_MSG, SZ_DIAG_ERR_MSG);
520 if (x != 0) {
521 x[SZ_DIAG_ERR_MSG - 1] = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700522 SMD_INFO("smem: DIAG '%s'\n", x);
523 }
524
525 x = smem_get_entry(SMEM_ERR_CRASH_LOG, &size);
526 if (x != 0) {
527 x[size - 1] = 0;
528 pr_err("smem: CRASH LOG\n'%s'\n", x);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700529 }
530}
531
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700532
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700533static void handle_modem_crash(void)
534{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700535 pr_err("MODEM/AMSS has CRASHED\n");
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700536 smd_diag();
537
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700538 /* hard reboot if possible FIXME
539 if (msm_reset_hook)
540 msm_reset_hook();
541 */
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700542
543 /* in this case the modem or watchdog should reboot us */
544 for (;;)
545 ;
546}
547
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700548int smsm_check_for_modem_crash(void)
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700549{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700550 /* if the modem's not ready yet, we have to hope for the best */
551 if (!smsm_info.state)
552 return 0;
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700553
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700554 if (__raw_readl(SMSM_STATE_ADDR(SMSM_MODEM_STATE)) & SMSM_RESET) {
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700555 handle_modem_crash();
556 return -1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700557 }
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700558 return 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700559}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700560EXPORT_SYMBOL(smsm_check_for_modem_crash);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700561
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700562/* the spinlock is used to synchronize between the
Brian Swetland03e00cd2009-07-01 17:58:37 -0700563 * irq handler and code that mutates the channel
564 * list or fiddles with channel state
565 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700566static DEFINE_SPINLOCK(smd_lock);
Brian Swetland03e00cd2009-07-01 17:58:37 -0700567DEFINE_SPINLOCK(smem_lock);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700568
569/* the mutex is used during open() and close()
Brian Swetland03e00cd2009-07-01 17:58:37 -0700570 * operations to avoid races while creating or
571 * destroying smd_channel structures
572 */
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700573static DEFINE_MUTEX(smd_creation_mutex);
574
575static int smd_initialized;
576
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700577struct smd_shared_v1 {
578 struct smd_half_channel ch0;
579 unsigned char data0[SMD_BUF_SIZE];
580 struct smd_half_channel ch1;
581 unsigned char data1[SMD_BUF_SIZE];
582};
583
584struct smd_shared_v2 {
585 struct smd_half_channel ch0;
586 struct smd_half_channel ch1;
587};
588
589struct smd_channel {
590 volatile struct smd_half_channel *send;
591 volatile struct smd_half_channel *recv;
592 unsigned char *send_data;
593 unsigned char *recv_data;
594 unsigned fifo_size;
595 unsigned fifo_mask;
596 struct list_head ch_list;
597
598 unsigned current_packet;
599 unsigned n;
600 void *priv;
601 void (*notify)(void *priv, unsigned flags);
602
603 int (*read)(smd_channel_t *ch, void *data, int len, int user_buf);
604 int (*write)(smd_channel_t *ch, const void *data, int len,
605 int user_buf);
606 int (*read_avail)(smd_channel_t *ch);
607 int (*write_avail)(smd_channel_t *ch);
608 int (*read_from_cb)(smd_channel_t *ch, void *data, int len,
609 int user_buf);
610
611 void (*update_state)(smd_channel_t *ch);
612 unsigned last_state;
613 void (*notify_other_cpu)(void);
614
615 char name[20];
616 struct platform_device pdev;
617 unsigned type;
618
619 int pending_pkt_sz;
620
621 char is_pkt_ch;
622};
623
624struct edge_to_pid {
625 uint32_t local_pid;
626 uint32_t remote_pid;
Eric Holmberg17992c12012-02-29 12:54:44 -0700627 char subsys_name[SMD_MAX_CH_NAME_LEN];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700628};
629
630/**
631 * Maps edge type to local and remote processor ID's.
632 */
633static struct edge_to_pid edge_to_pids[] = {
Eric Holmberg5a55b4a2012-03-01 14:41:54 -0700634 [SMD_APPS_MODEM] = {SMD_APPS, SMD_MODEM, "modem"},
635 [SMD_APPS_QDSP] = {SMD_APPS, SMD_Q6, "q6"},
636 [SMD_MODEM_QDSP] = {SMD_MODEM, SMD_Q6},
637 [SMD_APPS_DSPS] = {SMD_APPS, SMD_DSPS, "dsps"},
638 [SMD_MODEM_DSPS] = {SMD_MODEM, SMD_DSPS},
639 [SMD_QDSP_DSPS] = {SMD_Q6, SMD_DSPS},
640 [SMD_APPS_WCNSS] = {SMD_APPS, SMD_WCNSS, "wcnss"},
641 [SMD_MODEM_WCNSS] = {SMD_MODEM, SMD_WCNSS},
642 [SMD_QDSP_WCNSS] = {SMD_Q6, SMD_WCNSS},
643 [SMD_DSPS_WCNSS] = {SMD_DSPS, SMD_WCNSS},
644 [SMD_APPS_Q6FW] = {SMD_APPS, SMD_MODEM_Q6_FW},
645 [SMD_MODEM_Q6FW] = {SMD_MODEM, SMD_MODEM_Q6_FW},
646 [SMD_QDSP_Q6FW] = {SMD_Q6, SMD_MODEM_Q6_FW},
647 [SMD_DSPS_Q6FW] = {SMD_DSPS, SMD_MODEM_Q6_FW},
648 [SMD_WCNSS_Q6FW] = {SMD_WCNSS, SMD_MODEM_Q6_FW},
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700649};
650
651struct restart_notifier_block {
652 unsigned processor;
653 char *name;
654 struct notifier_block nb;
655};
656
Eric Holmberg2bb6ccd2012-03-13 13:05:14 -0600657static int disable_smsm_reset_handshake;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700658static struct platform_device loopback_tty_pdev = {.name = "LOOPBACK_TTY"};
659
660static LIST_HEAD(smd_ch_closed_list);
661static LIST_HEAD(smd_ch_closing_list);
662static LIST_HEAD(smd_ch_to_close_list);
663static LIST_HEAD(smd_ch_list_modem);
664static LIST_HEAD(smd_ch_list_dsp);
665static LIST_HEAD(smd_ch_list_dsps);
666static LIST_HEAD(smd_ch_list_wcnss);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700667
668static unsigned char smd_ch_allocated[64];
669static struct work_struct probe_work;
670
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700671static void finalize_channel_close_fn(struct work_struct *work);
672static DECLARE_WORK(finalize_channel_close_work, finalize_channel_close_fn);
673static struct workqueue_struct *channel_close_wq;
674
675static int smd_alloc_channel(struct smd_alloc_elm *alloc_elm);
676
677/* on smp systems, the probe might get called from multiple cores,
678 hence use a lock */
679static DEFINE_MUTEX(smd_probe_lock);
680
681static void smd_channel_probe_worker(struct work_struct *work)
682{
683 struct smd_alloc_elm *shared;
684 unsigned n;
685 uint32_t type;
686
687 shared = smem_find(ID_CH_ALLOC_TBL, sizeof(*shared) * 64);
688
689 if (!shared) {
690 pr_err("%s: allocation table not initialized\n", __func__);
691 return;
692 }
693
694 mutex_lock(&smd_probe_lock);
695 for (n = 0; n < 64; n++) {
696 if (smd_ch_allocated[n])
697 continue;
698
699 /* channel should be allocated only if APPS
700 processor is involved */
701 type = SMD_CHANNEL_TYPE(shared[n].type);
702 if ((type != SMD_APPS_MODEM) && (type != SMD_APPS_QDSP) &&
703 (type != SMD_APPS_DSPS) && (type != SMD_APPS_WCNSS))
704 continue;
705 if (!shared[n].ref_count)
706 continue;
707 if (!shared[n].name[0])
708 continue;
709
710 if (!smd_alloc_channel(&shared[n]))
711 smd_ch_allocated[n] = 1;
712 else
713 SMD_INFO("Probe skipping ch %d, not allocated\n", n);
714 }
715 mutex_unlock(&smd_probe_lock);
716}
717
718/**
719 * Lookup processor ID and determine if it belongs to the proved edge
720 * type.
721 *
722 * @shared2: Pointer to v2 shared channel structure
723 * @type: Edge type
724 * @pid: Processor ID of processor on edge
725 * @local_ch: Channel that belongs to processor @pid
726 * @remote_ch: Other side of edge contained @pid
727 *
728 * Returns 0 for not on edge, 1 for found on edge
729 */
730static int pid_is_on_edge(struct smd_shared_v2 *shared2,
731 uint32_t type, uint32_t pid,
732 struct smd_half_channel **local_ch,
733 struct smd_half_channel **remote_ch
734 )
735{
736 int ret = 0;
737 struct edge_to_pid *edge;
738
739 *local_ch = 0;
740 *remote_ch = 0;
741
742 if (!shared2 || (type >= ARRAY_SIZE(edge_to_pids)))
743 return 0;
744
745 edge = &edge_to_pids[type];
746 if (edge->local_pid != edge->remote_pid) {
747 if (pid == edge->local_pid) {
748 *local_ch = &shared2->ch0;
749 *remote_ch = &shared2->ch1;
750 ret = 1;
751 } else if (pid == edge->remote_pid) {
752 *local_ch = &shared2->ch1;
753 *remote_ch = &shared2->ch0;
754 ret = 1;
755 }
756 }
757
758 return ret;
759}
760
Eric Holmberg17992c12012-02-29 12:54:44 -0700761/*
762 * Returns a pointer to the subsystem name or NULL if no
763 * subsystem name is available.
764 *
765 * @type - Edge definition
766 */
767const char *smd_edge_to_subsystem(uint32_t type)
768{
769 const char *subsys = NULL;
770
771 if (type < ARRAY_SIZE(edge_to_pids)) {
772 subsys = edge_to_pids[type].subsys_name;
773 if (subsys[0] == 0x0)
774 subsys = NULL;
775 }
776 return subsys;
777}
778EXPORT_SYMBOL(smd_edge_to_subsystem);
779
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700780/*
781 * Returns a pointer to the subsystem name given the
782 * remote processor ID.
783 *
784 * @pid Remote processor ID
785 * @returns Pointer to subsystem name or NULL if not found
786 */
787const char *smd_pid_to_subsystem(uint32_t pid)
788{
789 const char *subsys = NULL;
790 int i;
791
792 for (i = 0; i < ARRAY_SIZE(edge_to_pids); ++i) {
793 if (pid == edge_to_pids[i].remote_pid &&
794 edge_to_pids[i].subsys_name[0] != 0x0
795 ) {
796 subsys = edge_to_pids[i].subsys_name;
797 break;
798 }
799 }
800
801 return subsys;
802}
803EXPORT_SYMBOL(smd_pid_to_subsystem);
Eric Holmberg17992c12012-02-29 12:54:44 -0700804
Eric Holmberg2a563c32011-10-05 14:51:43 -0600805static void smd_reset_edge(struct smd_half_channel *ch, unsigned new_state)
806{
807 if (ch->state != SMD_SS_CLOSED) {
808 ch->state = new_state;
809 ch->fDSR = 0;
810 ch->fCTS = 0;
811 ch->fCD = 0;
812 ch->fSTATE = 1;
813 }
814}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700815
816static void smd_channel_reset_state(struct smd_alloc_elm *shared,
817 unsigned new_state, unsigned pid)
818{
819 unsigned n;
820 struct smd_shared_v2 *shared2;
821 uint32_t type;
822 struct smd_half_channel *local_ch;
823 struct smd_half_channel *remote_ch;
824
825 for (n = 0; n < SMD_CHANNELS; n++) {
826 if (!shared[n].ref_count)
827 continue;
828 if (!shared[n].name[0])
829 continue;
830
831 type = SMD_CHANNEL_TYPE(shared[n].type);
832 shared2 = smem_alloc(SMEM_SMD_BASE_ID + n, sizeof(*shared2));
833 if (!shared2)
834 continue;
835
Eric Holmberg2a563c32011-10-05 14:51:43 -0600836 if (pid_is_on_edge(shared2, type, pid, &local_ch, &remote_ch))
837 smd_reset_edge(local_ch, new_state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700838
Eric Holmberg2a563c32011-10-05 14:51:43 -0600839 /*
840 * ModemFW is in the same subsystem as ModemSW, but has
841 * separate SMD edges that need to be reset.
842 */
843 if (pid == SMSM_MODEM &&
844 pid_is_on_edge(shared2, type, SMD_MODEM_Q6_FW,
845 &local_ch, &remote_ch))
846 smd_reset_edge(local_ch, new_state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700847 }
848}
849
850
851void smd_channel_reset(uint32_t restart_pid)
852{
853 struct smd_alloc_elm *shared;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700854 unsigned long flags;
855
856 SMD_DBG("%s: starting reset\n", __func__);
857 shared = smem_find(ID_CH_ALLOC_TBL, sizeof(*shared) * 64);
858 if (!shared) {
859 pr_err("%s: allocation table not initialized\n", __func__);
860 return;
861 }
862
Eric Holmbergf6d7d1a2011-09-23 18:31:04 -0600863 /* release any held spinlocks */
864 remote_spin_release(&remote_spinlock, restart_pid);
865 remote_spin_release_all(restart_pid);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700866
867 /* reset SMSM entry */
868 if (smsm_info.state) {
869 writel_relaxed(0, SMSM_STATE_ADDR(restart_pid));
870
Eric Holmberg351a63c2011-12-02 17:49:43 -0700871 /* restart SMSM init handshake */
872 if (restart_pid == SMSM_MODEM) {
873 smsm_change_state(SMSM_APPS_STATE,
Eric Holmberg6b2f80e2012-01-09 12:22:52 -0700874 SMSM_INIT | SMSM_SMD_LOOPBACK | SMSM_RESET,
875 0);
Eric Holmberg351a63c2011-12-02 17:49:43 -0700876 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700877
878 /* notify SMSM processors */
879 smsm_irq_handler(0, 0);
Eric Holmbergb7726442012-03-01 15:31:56 -0700880 notify_modem_smsm();
881 notify_dsp_smsm();
882 notify_dsps_smsm();
883 notify_wcnss_smsm();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700884 }
885
886 /* change all remote states to CLOSING */
887 mutex_lock(&smd_probe_lock);
888 spin_lock_irqsave(&smd_lock, flags);
889 smd_channel_reset_state(shared, SMD_SS_CLOSING, restart_pid);
890 spin_unlock_irqrestore(&smd_lock, flags);
891 mutex_unlock(&smd_probe_lock);
892
893 /* notify SMD processors */
894 mb();
895 smd_fake_irq_handler(0);
896 notify_modem_smd();
897 notify_dsp_smd();
898 notify_dsps_smd();
899 notify_wcnss_smd();
900
901 /* change all remote states to CLOSED */
902 mutex_lock(&smd_probe_lock);
903 spin_lock_irqsave(&smd_lock, flags);
904 smd_channel_reset_state(shared, SMD_SS_CLOSED, restart_pid);
905 spin_unlock_irqrestore(&smd_lock, flags);
906 mutex_unlock(&smd_probe_lock);
907
908 /* notify SMD processors */
909 mb();
910 smd_fake_irq_handler(0);
911 notify_modem_smd();
912 notify_dsp_smd();
913 notify_dsps_smd();
914 notify_wcnss_smd();
915
916 SMD_DBG("%s: finished reset\n", __func__);
917}
918
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700919/* how many bytes are available for reading */
920static int smd_stream_read_avail(struct smd_channel *ch)
921{
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700922 return (ch->recv->head - ch->recv->tail) & ch->fifo_mask;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700923}
924
925/* how many bytes we are free to write */
926static int smd_stream_write_avail(struct smd_channel *ch)
927{
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700928 return ch->fifo_mask -
929 ((ch->send->head - ch->send->tail) & ch->fifo_mask);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700930}
931
932static int smd_packet_read_avail(struct smd_channel *ch)
933{
934 if (ch->current_packet) {
935 int n = smd_stream_read_avail(ch);
936 if (n > ch->current_packet)
937 n = ch->current_packet;
938 return n;
939 } else {
940 return 0;
941 }
942}
943
944static int smd_packet_write_avail(struct smd_channel *ch)
945{
946 int n = smd_stream_write_avail(ch);
947 return n > SMD_HEADER_SIZE ? n - SMD_HEADER_SIZE : 0;
948}
949
950static int ch_is_open(struct smd_channel *ch)
951{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700952 return (ch->recv->state == SMD_SS_OPENED ||
953 ch->recv->state == SMD_SS_FLUSHING)
954 && (ch->send->state == SMD_SS_OPENED);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700955}
956
957/* provide a pointer and length to readable data in the fifo */
958static unsigned ch_read_buffer(struct smd_channel *ch, void **ptr)
959{
960 unsigned head = ch->recv->head;
961 unsigned tail = ch->recv->tail;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700962 *ptr = (void *) (ch->recv_data + tail);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700963
964 if (tail <= head)
965 return head - tail;
966 else
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700967 return ch->fifo_size - tail;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700968}
969
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700970static int read_intr_blocked(struct smd_channel *ch)
971{
972 return ch->recv->fBLOCKREADINTR;
973}
974
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700975/* advance the fifo read pointer after data from ch_read_buffer is consumed */
976static void ch_read_done(struct smd_channel *ch, unsigned count)
977{
978 BUG_ON(count > smd_stream_read_avail(ch));
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700979 ch->recv->tail = (ch->recv->tail + count) & ch->fifo_mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700980 wmb();
Haley Teng7632fba2009-10-12 10:38:10 -0700981 ch->send->fTAIL = 1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700982}
983
984/* basic read interface to ch_read_{buffer,done} used
Brian Swetland03e00cd2009-07-01 17:58:37 -0700985 * by smd_*_read() and update_packet_state()
986 * will read-and-discard if the _data pointer is null
987 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700988static int ch_read(struct smd_channel *ch, void *_data, int len, int user_buf)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700989{
990 void *ptr;
991 unsigned n;
992 unsigned char *data = _data;
993 int orig_len = len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700994 int r = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700995
996 while (len > 0) {
997 n = ch_read_buffer(ch, &ptr);
998 if (n == 0)
999 break;
1000
1001 if (n > len)
1002 n = len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001003 if (_data) {
1004 if (user_buf) {
1005 r = copy_to_user(data, ptr, n);
1006 if (r > 0) {
1007 pr_err("%s: "
1008 "copy_to_user could not copy "
1009 "%i bytes.\n",
1010 __func__,
1011 r);
1012 }
1013 } else
1014 memcpy(data, ptr, n);
1015 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001016
1017 data += n;
1018 len -= n;
1019 ch_read_done(ch, n);
1020 }
1021
1022 return orig_len - len;
1023}
1024
1025static void update_stream_state(struct smd_channel *ch)
1026{
1027 /* streams have no special state requiring updating */
1028}
1029
1030static void update_packet_state(struct smd_channel *ch)
1031{
1032 unsigned hdr[5];
1033 int r;
1034
1035 /* can't do anything if we're in the middle of a packet */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001036 while (ch->current_packet == 0) {
1037 /* discard 0 length packets if any */
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001038
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001039 /* don't bother unless we can get the full header */
1040 if (smd_stream_read_avail(ch) < SMD_HEADER_SIZE)
1041 return;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001042
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001043 r = ch_read(ch, hdr, SMD_HEADER_SIZE, 0);
1044 BUG_ON(r != SMD_HEADER_SIZE);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001045
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001046 ch->current_packet = hdr[0];
1047 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001048}
1049
1050/* provide a pointer and length to next free space in the fifo */
1051static unsigned ch_write_buffer(struct smd_channel *ch, void **ptr)
1052{
1053 unsigned head = ch->send->head;
1054 unsigned tail = ch->send->tail;
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001055 *ptr = (void *) (ch->send_data + head);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001056
1057 if (head < tail) {
1058 return tail - head - 1;
1059 } else {
1060 if (tail == 0)
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001061 return ch->fifo_size - head - 1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001062 else
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001063 return ch->fifo_size - head;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001064 }
1065}
1066
1067/* advace the fifo write pointer after freespace
1068 * from ch_write_buffer is filled
1069 */
1070static void ch_write_done(struct smd_channel *ch, unsigned count)
1071{
1072 BUG_ON(count > smd_stream_write_avail(ch));
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001073 ch->send->head = (ch->send->head + count) & ch->fifo_mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001074 wmb();
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001075 ch->send->fHEAD = 1;
1076}
1077
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001078static void ch_set_state(struct smd_channel *ch, unsigned n)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001079{
1080 if (n == SMD_SS_OPENED) {
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001081 ch->send->fDSR = 1;
1082 ch->send->fCTS = 1;
1083 ch->send->fCD = 1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001084 } else {
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001085 ch->send->fDSR = 0;
1086 ch->send->fCTS = 0;
1087 ch->send->fCD = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001088 }
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001089 ch->send->state = n;
1090 ch->send->fSTATE = 1;
1091 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001092}
1093
1094static void do_smd_probe(void)
1095{
1096 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
1097 if (shared->heap_info.free_offset != last_heap_free) {
1098 last_heap_free = shared->heap_info.free_offset;
1099 schedule_work(&probe_work);
1100 }
1101}
1102
1103static void smd_state_change(struct smd_channel *ch,
1104 unsigned last, unsigned next)
1105{
1106 ch->last_state = next;
1107
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001108 SMD_INFO("SMD: ch %d %d -> %d\n", ch->n, last, next);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001109
1110 switch (next) {
1111 case SMD_SS_OPENING:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001112 if (ch->send->state == SMD_SS_CLOSING ||
1113 ch->send->state == SMD_SS_CLOSED) {
1114 ch->recv->tail = 0;
1115 ch->send->head = 0;
1116 ch->send->fBLOCKREADINTR = 0;
1117 ch_set_state(ch, SMD_SS_OPENING);
1118 }
1119 break;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001120 case SMD_SS_OPENED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001121 if (ch->send->state == SMD_SS_OPENING) {
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001122 ch_set_state(ch, SMD_SS_OPENED);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001123 ch->notify(ch->priv, SMD_EVENT_OPEN);
1124 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001125 break;
1126 case SMD_SS_FLUSHING:
1127 case SMD_SS_RESET:
1128 /* we should force them to close? */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001129 break;
1130 case SMD_SS_CLOSED:
1131 if (ch->send->state == SMD_SS_OPENED) {
1132 ch_set_state(ch, SMD_SS_CLOSING);
1133 ch->current_packet = 0;
Eric Holmbergad4fa8d2011-11-11 16:55:13 -07001134 ch->pending_pkt_sz = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001135 ch->notify(ch->priv, SMD_EVENT_CLOSE);
1136 }
1137 break;
1138 case SMD_SS_CLOSING:
1139 if (ch->send->state == SMD_SS_CLOSED) {
1140 list_move(&ch->ch_list,
1141 &smd_ch_to_close_list);
1142 queue_work(channel_close_wq,
1143 &finalize_channel_close_work);
1144 }
1145 break;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001146 }
1147}
1148
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001149static void handle_smd_irq_closing_list(void)
1150{
1151 unsigned long flags;
1152 struct smd_channel *ch;
1153 struct smd_channel *index;
1154 unsigned tmp;
1155
1156 spin_lock_irqsave(&smd_lock, flags);
1157 list_for_each_entry_safe(ch, index, &smd_ch_closing_list, ch_list) {
1158 if (ch->recv->fSTATE)
1159 ch->recv->fSTATE = 0;
1160 tmp = ch->recv->state;
1161 if (tmp != ch->last_state)
1162 smd_state_change(ch, ch->last_state, tmp);
1163 }
1164 spin_unlock_irqrestore(&smd_lock, flags);
1165}
1166
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001167static void handle_smd_irq(struct list_head *list, void (*notify)(void))
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001168{
1169 unsigned long flags;
1170 struct smd_channel *ch;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001171 unsigned ch_flags;
1172 unsigned tmp;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001173 unsigned char state_change;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001174
1175 spin_lock_irqsave(&smd_lock, flags);
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001176 list_for_each_entry(ch, list, ch_list) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001177 state_change = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001178 ch_flags = 0;
1179 if (ch_is_open(ch)) {
1180 if (ch->recv->fHEAD) {
1181 ch->recv->fHEAD = 0;
1182 ch_flags |= 1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001183 }
1184 if (ch->recv->fTAIL) {
1185 ch->recv->fTAIL = 0;
1186 ch_flags |= 2;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001187 }
1188 if (ch->recv->fSTATE) {
1189 ch->recv->fSTATE = 0;
1190 ch_flags |= 4;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001191 }
1192 }
1193 tmp = ch->recv->state;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001194 if (tmp != ch->last_state) {
Eric Holmberg98c6c642012-02-24 11:29:35 -07001195 SMx_POWER_INFO("SMD ch%d '%s' State change %d->%d\n",
1196 ch->n, ch->name, ch->last_state, tmp);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001197 smd_state_change(ch, ch->last_state, tmp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001198 state_change = 1;
1199 }
Eric Holmberg65a7d432012-02-24 11:28:56 -07001200 if (ch_flags & 0x3) {
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001201 ch->update_state(ch);
Eric Holmberg98c6c642012-02-24 11:29:35 -07001202 SMx_POWER_INFO("SMD ch%d '%s' Data event r%d/w%d\n",
1203 ch->n, ch->name,
1204 ch->read_avail(ch),
1205 ch->fifo_size - ch->write_avail(ch));
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001206 ch->notify(ch->priv, SMD_EVENT_DATA);
1207 }
Eric Holmberg98c6c642012-02-24 11:29:35 -07001208 if (ch_flags & 0x4 && !state_change) {
1209 SMx_POWER_INFO("SMD ch%d '%s' State update\n",
1210 ch->n, ch->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001211 ch->notify(ch->priv, SMD_EVENT_STATUS);
Eric Holmberg98c6c642012-02-24 11:29:35 -07001212 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001213 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001214 spin_unlock_irqrestore(&smd_lock, flags);
1215 do_smd_probe();
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001216}
1217
Brian Swetland37521a32009-07-01 18:30:47 -07001218static irqreturn_t smd_modem_irq_handler(int irq, void *data)
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001219{
Eric Holmberg98c6c642012-02-24 11:29:35 -07001220 SMx_POWER_INFO("SMD Int Modem->Apps\n");
Eric Holmberg7ad623a2012-03-01 14:41:10 -07001221 ++interrupt_stats[SMD_MODEM].smd_in_count;
Brian Swetland37521a32009-07-01 18:30:47 -07001222 handle_smd_irq(&smd_ch_list_modem, notify_modem_smd);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001223 handle_smd_irq_closing_list();
Brian Swetland37521a32009-07-01 18:30:47 -07001224 return IRQ_HANDLED;
1225}
1226
1227static irqreturn_t smd_dsp_irq_handler(int irq, void *data)
1228{
Eric Holmberg98c6c642012-02-24 11:29:35 -07001229 SMx_POWER_INFO("SMD Int LPASS->Apps\n");
Eric Holmberg7ad623a2012-03-01 14:41:10 -07001230 ++interrupt_stats[SMD_Q6].smd_in_count;
Brian Swetland37521a32009-07-01 18:30:47 -07001231 handle_smd_irq(&smd_ch_list_dsp, notify_dsp_smd);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001232 handle_smd_irq_closing_list();
1233 return IRQ_HANDLED;
1234}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001235
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001236static irqreturn_t smd_dsps_irq_handler(int irq, void *data)
1237{
Eric Holmberg98c6c642012-02-24 11:29:35 -07001238 SMx_POWER_INFO("SMD Int DSPS->Apps\n");
Eric Holmberg7ad623a2012-03-01 14:41:10 -07001239 ++interrupt_stats[SMD_DSPS].smd_in_count;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001240 handle_smd_irq(&smd_ch_list_dsps, notify_dsps_smd);
1241 handle_smd_irq_closing_list();
1242 return IRQ_HANDLED;
1243}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001244
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001245static irqreturn_t smd_wcnss_irq_handler(int irq, void *data)
1246{
Eric Holmberg98c6c642012-02-24 11:29:35 -07001247 SMx_POWER_INFO("SMD Int WCNSS->Apps\n");
Eric Holmberg7ad623a2012-03-01 14:41:10 -07001248 ++interrupt_stats[SMD_WCNSS].smd_in_count;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001249 handle_smd_irq(&smd_ch_list_wcnss, notify_wcnss_smd);
1250 handle_smd_irq_closing_list();
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001251 return IRQ_HANDLED;
1252}
1253
1254static void smd_fake_irq_handler(unsigned long arg)
1255{
Brian Swetland37521a32009-07-01 18:30:47 -07001256 handle_smd_irq(&smd_ch_list_modem, notify_modem_smd);
1257 handle_smd_irq(&smd_ch_list_dsp, notify_dsp_smd);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001258 handle_smd_irq(&smd_ch_list_dsps, notify_dsps_smd);
1259 handle_smd_irq(&smd_ch_list_wcnss, notify_wcnss_smd);
1260 handle_smd_irq_closing_list();
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001261}
1262
1263static DECLARE_TASKLET(smd_fake_irq_tasklet, smd_fake_irq_handler, 0);
1264
Brian Swetland37521a32009-07-01 18:30:47 -07001265static inline int smd_need_int(struct smd_channel *ch)
1266{
1267 if (ch_is_open(ch)) {
1268 if (ch->recv->fHEAD || ch->recv->fTAIL || ch->recv->fSTATE)
1269 return 1;
1270 if (ch->recv->state != ch->last_state)
1271 return 1;
1272 }
1273 return 0;
1274}
1275
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001276void smd_sleep_exit(void)
1277{
1278 unsigned long flags;
1279 struct smd_channel *ch;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001280 int need_int = 0;
1281
1282 spin_lock_irqsave(&smd_lock, flags);
Brian Swetland37521a32009-07-01 18:30:47 -07001283 list_for_each_entry(ch, &smd_ch_list_modem, ch_list) {
1284 if (smd_need_int(ch)) {
1285 need_int = 1;
1286 break;
1287 }
1288 }
1289 list_for_each_entry(ch, &smd_ch_list_dsp, ch_list) {
1290 if (smd_need_int(ch)) {
1291 need_int = 1;
1292 break;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001293 }
1294 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001295 list_for_each_entry(ch, &smd_ch_list_dsps, ch_list) {
1296 if (smd_need_int(ch)) {
1297 need_int = 1;
1298 break;
1299 }
1300 }
1301 list_for_each_entry(ch, &smd_ch_list_wcnss, ch_list) {
1302 if (smd_need_int(ch)) {
1303 need_int = 1;
1304 break;
1305 }
1306 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001307 spin_unlock_irqrestore(&smd_lock, flags);
1308 do_smd_probe();
Brian Swetland37521a32009-07-01 18:30:47 -07001309
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001310 if (need_int) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001311 SMD_DBG("smd_sleep_exit need interrupt\n");
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001312 tasklet_schedule(&smd_fake_irq_tasklet);
1313 }
1314}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001315EXPORT_SYMBOL(smd_sleep_exit);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001316
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001317static int smd_is_packet(struct smd_alloc_elm *alloc_elm)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001318{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001319 if (SMD_XFER_TYPE(alloc_elm->type) == 1)
1320 return 0;
1321 else if (SMD_XFER_TYPE(alloc_elm->type) == 2)
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001322 return 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001323
1324 /* for cases where xfer type is 0 */
1325 if (!strncmp(alloc_elm->name, "DAL", 3))
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001326 return 0;
1327
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001328 /* for cases where xfer type is 0 */
1329 if (!strncmp(alloc_elm->name, "RPCCALL_QDSP", 12))
1330 return 0;
1331
1332 if (alloc_elm->cid > 4 || alloc_elm->cid == 1)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001333 return 1;
1334 else
1335 return 0;
1336}
1337
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001338static int smd_stream_write(smd_channel_t *ch, const void *_data, int len,
1339 int user_buf)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001340{
1341 void *ptr;
1342 const unsigned char *buf = _data;
1343 unsigned xfer;
1344 int orig_len = len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001345 int r = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001346
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001347 SMD_DBG("smd_stream_write() %d -> ch%d\n", len, ch->n);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001348 if (len < 0)
1349 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001350 else if (len == 0)
1351 return 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001352
1353 while ((xfer = ch_write_buffer(ch, &ptr)) != 0) {
Eric Holmberg7a717872012-02-03 11:58:04 -07001354 if (!ch_is_open(ch)) {
1355 len = orig_len;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001356 break;
Eric Holmberg7a717872012-02-03 11:58:04 -07001357 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001358 if (xfer > len)
1359 xfer = len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001360 if (user_buf) {
1361 r = copy_from_user(ptr, buf, xfer);
1362 if (r > 0) {
1363 pr_err("%s: "
1364 "copy_from_user could not copy %i "
1365 "bytes.\n",
1366 __func__,
1367 r);
1368 }
1369 } else
1370 memcpy(ptr, buf, xfer);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001371 ch_write_done(ch, xfer);
1372 len -= xfer;
1373 buf += xfer;
1374 if (len == 0)
1375 break;
1376 }
1377
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001378 if (orig_len - len)
1379 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001380
1381 return orig_len - len;
1382}
1383
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001384static int smd_packet_write(smd_channel_t *ch, const void *_data, int len,
1385 int user_buf)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001386{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001387 int ret;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001388 unsigned hdr[5];
1389
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001390 SMD_DBG("smd_packet_write() %d -> ch%d\n", len, ch->n);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001391 if (len < 0)
1392 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001393 else if (len == 0)
1394 return 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001395
1396 if (smd_stream_write_avail(ch) < (len + SMD_HEADER_SIZE))
1397 return -ENOMEM;
1398
1399 hdr[0] = len;
1400 hdr[1] = hdr[2] = hdr[3] = hdr[4] = 0;
1401
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001402
1403 ret = smd_stream_write(ch, hdr, sizeof(hdr), 0);
1404 if (ret < 0 || ret != sizeof(hdr)) {
1405 SMD_DBG("%s failed to write pkt header: "
1406 "%d returned\n", __func__, ret);
1407 return -1;
1408 }
1409
1410
1411 ret = smd_stream_write(ch, _data, len, user_buf);
1412 if (ret < 0 || ret != len) {
1413 SMD_DBG("%s failed to write pkt data: "
1414 "%d returned\n", __func__, ret);
1415 return ret;
1416 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001417
1418 return len;
1419}
1420
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001421static int smd_stream_read(smd_channel_t *ch, void *data, int len, int user_buf)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001422{
1423 int r;
1424
1425 if (len < 0)
1426 return -EINVAL;
1427
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001428 r = ch_read(ch, data, len, user_buf);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001429 if (r > 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001430 if (!read_intr_blocked(ch))
1431 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001432
1433 return r;
1434}
1435
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001436static int smd_packet_read(smd_channel_t *ch, void *data, int len, int user_buf)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001437{
1438 unsigned long flags;
1439 int r;
1440
1441 if (len < 0)
1442 return -EINVAL;
1443
1444 if (len > ch->current_packet)
1445 len = ch->current_packet;
1446
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001447 r = ch_read(ch, data, len, user_buf);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001448 if (r > 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001449 if (!read_intr_blocked(ch))
1450 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001451
1452 spin_lock_irqsave(&smd_lock, flags);
1453 ch->current_packet -= r;
1454 update_packet_state(ch);
1455 spin_unlock_irqrestore(&smd_lock, flags);
1456
1457 return r;
1458}
1459
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001460static int smd_packet_read_from_cb(smd_channel_t *ch, void *data, int len,
1461 int user_buf)
1462{
1463 int r;
1464
1465 if (len < 0)
1466 return -EINVAL;
1467
1468 if (len > ch->current_packet)
1469 len = ch->current_packet;
1470
1471 r = ch_read(ch, data, len, user_buf);
1472 if (r > 0)
1473 if (!read_intr_blocked(ch))
1474 ch->notify_other_cpu();
1475
1476 ch->current_packet -= r;
1477 update_packet_state(ch);
1478
1479 return r;
1480}
1481
Angshuman Sarkarac7d6252011-09-30 18:20:59 +05301482#if (defined(CONFIG_MSM_SMD_PKG4) || defined(CONFIG_MSM_SMD_PKG3))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001483static int smd_alloc_v2(struct smd_channel *ch)
1484{
1485 struct smd_shared_v2 *shared2;
1486 void *buffer;
1487 unsigned buffer_sz;
1488
1489 shared2 = smem_alloc(SMEM_SMD_BASE_ID + ch->n, sizeof(*shared2));
1490 if (!shared2) {
1491 SMD_INFO("smem_alloc failed ch=%d\n", ch->n);
Angshuman Sarkarac7d6252011-09-30 18:20:59 +05301492 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001493 }
1494 buffer = smem_get_entry(SMEM_SMD_FIFO_BASE_ID + ch->n, &buffer_sz);
1495 if (!buffer) {
Angshuman Sarkarac7d6252011-09-30 18:20:59 +05301496 SMD_INFO("smem_get_entry failed\n");
1497 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001498 }
1499
1500 /* buffer must be a power-of-two size */
Angshuman Sarkarac7d6252011-09-30 18:20:59 +05301501 if (buffer_sz & (buffer_sz - 1)) {
1502 SMD_INFO("Buffer size: %u not power of two\n", buffer_sz);
1503 return -EINVAL;
1504 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001505 buffer_sz /= 2;
1506 ch->send = &shared2->ch0;
1507 ch->recv = &shared2->ch1;
1508 ch->send_data = buffer;
1509 ch->recv_data = buffer + buffer_sz;
1510 ch->fifo_size = buffer_sz;
1511 return 0;
1512}
1513
1514static int smd_alloc_v1(struct smd_channel *ch)
1515{
Angshuman Sarkarac7d6252011-09-30 18:20:59 +05301516 return -EINVAL;
1517}
1518
1519#else /* define v1 for older targets */
1520static int smd_alloc_v2(struct smd_channel *ch)
1521{
1522 return -EINVAL;
1523}
1524
1525static int smd_alloc_v1(struct smd_channel *ch)
1526{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001527 struct smd_shared_v1 *shared1;
1528 shared1 = smem_alloc(ID_SMD_CHANNELS + ch->n, sizeof(*shared1));
1529 if (!shared1) {
1530 pr_err("smd_alloc_channel() cid %d does not exist\n", ch->n);
Angshuman Sarkarac7d6252011-09-30 18:20:59 +05301531 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001532 }
1533 ch->send = &shared1->ch0;
1534 ch->recv = &shared1->ch1;
1535 ch->send_data = shared1->data0;
1536 ch->recv_data = shared1->data1;
1537 ch->fifo_size = SMD_BUF_SIZE;
1538 return 0;
1539}
1540
Angshuman Sarkarac7d6252011-09-30 18:20:59 +05301541#endif
1542
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001543static int smd_alloc_channel(struct smd_alloc_elm *alloc_elm)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001544{
1545 struct smd_channel *ch;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001546
1547 ch = kzalloc(sizeof(struct smd_channel), GFP_KERNEL);
1548 if (ch == 0) {
1549 pr_err("smd_alloc_channel() out of memory\n");
Brian Swetland34f719b2009-10-30 16:22:05 -07001550 return -1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001551 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001552 ch->n = alloc_elm->cid;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001553
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001554 if (smd_alloc_v2(ch) && smd_alloc_v1(ch)) {
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001555 kfree(ch);
Brian Swetland34f719b2009-10-30 16:22:05 -07001556 return -1;
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001557 }
1558
1559 ch->fifo_mask = ch->fifo_size - 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001560 ch->type = SMD_CHANNEL_TYPE(alloc_elm->type);
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001561
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001562 if (ch->type == SMD_APPS_MODEM)
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001563 ch->notify_other_cpu = notify_modem_smd;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001564 else if (ch->type == SMD_APPS_QDSP)
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001565 ch->notify_other_cpu = notify_dsp_smd;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001566 else if (ch->type == SMD_APPS_DSPS)
1567 ch->notify_other_cpu = notify_dsps_smd;
1568 else
1569 ch->notify_other_cpu = notify_wcnss_smd;
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001570
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001571 if (smd_is_packet(alloc_elm)) {
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001572 ch->read = smd_packet_read;
1573 ch->write = smd_packet_write;
1574 ch->read_avail = smd_packet_read_avail;
1575 ch->write_avail = smd_packet_write_avail;
1576 ch->update_state = update_packet_state;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001577 ch->read_from_cb = smd_packet_read_from_cb;
1578 ch->is_pkt_ch = 1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001579 } else {
1580 ch->read = smd_stream_read;
1581 ch->write = smd_stream_write;
1582 ch->read_avail = smd_stream_read_avail;
1583 ch->write_avail = smd_stream_write_avail;
1584 ch->update_state = update_stream_state;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001585 ch->read_from_cb = smd_stream_read;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001586 }
1587
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001588 memcpy(ch->name, alloc_elm->name, SMD_MAX_CH_NAME_LEN);
1589 ch->name[SMD_MAX_CH_NAME_LEN-1] = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001590
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001591 ch->pdev.name = ch->name;
1592 ch->pdev.id = ch->type;
1593
1594 SMD_INFO("smd_alloc_channel() '%s' cid=%d\n",
1595 ch->name, ch->n);
1596
1597 mutex_lock(&smd_creation_mutex);
1598 list_add(&ch->ch_list, &smd_ch_closed_list);
1599 mutex_unlock(&smd_creation_mutex);
1600
1601 platform_device_register(&ch->pdev);
1602 if (!strncmp(ch->name, "LOOPBACK", 8) && ch->type == SMD_APPS_MODEM) {
1603 /* create a platform driver to be used by smd_tty driver
1604 * so that it can access the loopback port
1605 */
1606 loopback_tty_pdev.id = ch->type;
1607 platform_device_register(&loopback_tty_pdev);
1608 }
1609 return 0;
1610}
1611
1612static inline void notify_loopback_smd(void)
1613{
1614 unsigned long flags;
1615 struct smd_channel *ch;
1616
1617 spin_lock_irqsave(&smd_lock, flags);
1618 list_for_each_entry(ch, &smd_ch_list_loopback, ch_list) {
1619 ch->notify(ch->priv, SMD_EVENT_DATA);
1620 }
1621 spin_unlock_irqrestore(&smd_lock, flags);
1622}
1623
1624static int smd_alloc_loopback_channel(void)
1625{
1626 static struct smd_half_channel smd_loopback_ctl;
1627 static char smd_loopback_data[SMD_BUF_SIZE];
1628 struct smd_channel *ch;
1629
1630 ch = kzalloc(sizeof(struct smd_channel), GFP_KERNEL);
1631 if (ch == 0) {
1632 pr_err("%s: out of memory\n", __func__);
1633 return -1;
1634 }
1635 ch->n = SMD_LOOPBACK_CID;
1636
1637 ch->send = &smd_loopback_ctl;
1638 ch->recv = &smd_loopback_ctl;
1639 ch->send_data = smd_loopback_data;
1640 ch->recv_data = smd_loopback_data;
1641 ch->fifo_size = SMD_BUF_SIZE;
1642
1643 ch->fifo_mask = ch->fifo_size - 1;
1644 ch->type = SMD_LOOPBACK_TYPE;
1645 ch->notify_other_cpu = notify_loopback_smd;
1646
1647 ch->read = smd_stream_read;
1648 ch->write = smd_stream_write;
1649 ch->read_avail = smd_stream_read_avail;
1650 ch->write_avail = smd_stream_write_avail;
1651 ch->update_state = update_stream_state;
1652 ch->read_from_cb = smd_stream_read;
1653
1654 memset(ch->name, 0, 20);
1655 memcpy(ch->name, "local_loopback", 14);
1656
1657 ch->pdev.name = ch->name;
1658 ch->pdev.id = ch->type;
1659
1660 SMD_INFO("%s: '%s' cid=%d\n", __func__, ch->name, ch->n);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001661
1662 mutex_lock(&smd_creation_mutex);
1663 list_add(&ch->ch_list, &smd_ch_closed_list);
1664 mutex_unlock(&smd_creation_mutex);
1665
1666 platform_device_register(&ch->pdev);
Brian Swetland34f719b2009-10-30 16:22:05 -07001667 return 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001668}
1669
1670static void do_nothing_notify(void *priv, unsigned flags)
1671{
1672}
1673
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001674static void finalize_channel_close_fn(struct work_struct *work)
1675{
1676 unsigned long flags;
1677 struct smd_channel *ch;
1678 struct smd_channel *index;
1679
Eric Holmbergbb2b1fa2011-10-12 16:41:37 -06001680 mutex_lock(&smd_creation_mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001681 spin_lock_irqsave(&smd_lock, flags);
1682 list_for_each_entry_safe(ch, index, &smd_ch_to_close_list, ch_list) {
1683 list_del(&ch->ch_list);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001684 list_add(&ch->ch_list, &smd_ch_closed_list);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001685 ch->notify(ch->priv, SMD_EVENT_REOPEN_READY);
1686 ch->notify = do_nothing_notify;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001687 }
1688 spin_unlock_irqrestore(&smd_lock, flags);
Eric Holmbergbb2b1fa2011-10-12 16:41:37 -06001689 mutex_unlock(&smd_creation_mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001690}
1691
1692struct smd_channel *smd_get_channel(const char *name, uint32_t type)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001693{
1694 struct smd_channel *ch;
1695
1696 mutex_lock(&smd_creation_mutex);
1697 list_for_each_entry(ch, &smd_ch_closed_list, ch_list) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001698 if (!strcmp(name, ch->name) &&
1699 (type == ch->type)) {
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001700 list_del(&ch->ch_list);
1701 mutex_unlock(&smd_creation_mutex);
1702 return ch;
1703 }
1704 }
1705 mutex_unlock(&smd_creation_mutex);
1706
1707 return NULL;
1708}
1709
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001710int smd_named_open_on_edge(const char *name, uint32_t edge,
1711 smd_channel_t **_ch,
1712 void *priv, void (*notify)(void *, unsigned))
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001713{
1714 struct smd_channel *ch;
1715 unsigned long flags;
1716
1717 if (smd_initialized == 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001718 SMD_INFO("smd_open() before smd_init()\n");
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001719 return -ENODEV;
1720 }
1721
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001722 SMD_DBG("smd_open('%s', %p, %p)\n", name, priv, notify);
1723
1724 ch = smd_get_channel(name, edge);
Eric Holmbergbb2b1fa2011-10-12 16:41:37 -06001725 if (!ch) {
Eric Holmbergbb2b1fa2011-10-12 16:41:37 -06001726 /* check closing list for port */
1727 spin_lock_irqsave(&smd_lock, flags);
1728 list_for_each_entry(ch, &smd_ch_closing_list, ch_list) {
1729 if (!strncmp(name, ch->name, 20) &&
1730 (edge == ch->type)) {
1731 /* channel exists, but is being closed */
1732 spin_unlock_irqrestore(&smd_lock, flags);
1733 return -EAGAIN;
1734 }
1735 }
1736
1737 /* check closing workqueue list for port */
1738 list_for_each_entry(ch, &smd_ch_to_close_list, ch_list) {
1739 if (!strncmp(name, ch->name, 20) &&
1740 (edge == ch->type)) {
1741 /* channel exists, but is being closed */
1742 spin_unlock_irqrestore(&smd_lock, flags);
1743 return -EAGAIN;
1744 }
1745 }
1746 spin_unlock_irqrestore(&smd_lock, flags);
1747
1748 /* one final check to handle closing->closed race condition */
1749 ch = smd_get_channel(name, edge);
1750 if (!ch)
1751 return -ENODEV;
1752 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001753
1754 if (notify == 0)
1755 notify = do_nothing_notify;
1756
1757 ch->notify = notify;
1758 ch->current_packet = 0;
1759 ch->last_state = SMD_SS_CLOSED;
1760 ch->priv = priv;
1761
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001762 if (edge == SMD_LOOPBACK_TYPE) {
1763 ch->last_state = SMD_SS_OPENED;
1764 ch->send->state = SMD_SS_OPENED;
1765 ch->send->fDSR = 1;
1766 ch->send->fCTS = 1;
1767 ch->send->fCD = 1;
1768 }
1769
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001770 *_ch = ch;
1771
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001772 SMD_DBG("smd_open: opening '%s'\n", ch->name);
1773
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001774 spin_lock_irqsave(&smd_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001775 if (SMD_CHANNEL_TYPE(ch->type) == SMD_APPS_MODEM)
Brian Swetland37521a32009-07-01 18:30:47 -07001776 list_add(&ch->ch_list, &smd_ch_list_modem);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001777 else if (SMD_CHANNEL_TYPE(ch->type) == SMD_APPS_QDSP)
Brian Swetland37521a32009-07-01 18:30:47 -07001778 list_add(&ch->ch_list, &smd_ch_list_dsp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001779 else if (SMD_CHANNEL_TYPE(ch->type) == SMD_APPS_DSPS)
1780 list_add(&ch->ch_list, &smd_ch_list_dsps);
1781 else if (SMD_CHANNEL_TYPE(ch->type) == SMD_APPS_WCNSS)
1782 list_add(&ch->ch_list, &smd_ch_list_wcnss);
1783 else
1784 list_add(&ch->ch_list, &smd_ch_list_loopback);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001785
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001786 SMD_DBG("%s: opening ch %d\n", __func__, ch->n);
1787
1788 if (edge != SMD_LOOPBACK_TYPE)
1789 smd_state_change(ch, ch->last_state, SMD_SS_OPENING);
1790
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001791 spin_unlock_irqrestore(&smd_lock, flags);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001792
1793 return 0;
1794}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001795EXPORT_SYMBOL(smd_named_open_on_edge);
1796
1797
1798int smd_open(const char *name, smd_channel_t **_ch,
1799 void *priv, void (*notify)(void *, unsigned))
1800{
1801 return smd_named_open_on_edge(name, SMD_APPS_MODEM, _ch, priv,
1802 notify);
1803}
1804EXPORT_SYMBOL(smd_open);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001805
1806int smd_close(smd_channel_t *ch)
1807{
1808 unsigned long flags;
1809
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001810 if (ch == 0)
1811 return -1;
1812
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001813 SMD_INFO("smd_close(%s)\n", ch->name);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001814
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001815 spin_lock_irqsave(&smd_lock, flags);
1816 list_del(&ch->ch_list);
1817 if (ch->n == SMD_LOOPBACK_CID) {
1818 ch->send->fDSR = 0;
1819 ch->send->fCTS = 0;
1820 ch->send->fCD = 0;
1821 ch->send->state = SMD_SS_CLOSED;
1822 } else
1823 ch_set_state(ch, SMD_SS_CLOSED);
1824
1825 if (ch->recv->state == SMD_SS_OPENED) {
1826 list_add(&ch->ch_list, &smd_ch_closing_list);
1827 spin_unlock_irqrestore(&smd_lock, flags);
1828 } else {
1829 spin_unlock_irqrestore(&smd_lock, flags);
1830 ch->notify = do_nothing_notify;
1831 mutex_lock(&smd_creation_mutex);
1832 list_add(&ch->ch_list, &smd_ch_closed_list);
1833 mutex_unlock(&smd_creation_mutex);
1834 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001835
1836 return 0;
1837}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001838EXPORT_SYMBOL(smd_close);
1839
1840int smd_write_start(smd_channel_t *ch, int len)
1841{
1842 int ret;
1843 unsigned hdr[5];
1844
1845 if (!ch) {
1846 pr_err("%s: Invalid channel specified\n", __func__);
1847 return -ENODEV;
1848 }
1849 if (!ch->is_pkt_ch) {
1850 pr_err("%s: non-packet channel specified\n", __func__);
1851 return -EACCES;
1852 }
1853 if (len < 1) {
1854 pr_err("%s: invalid length: %d\n", __func__, len);
1855 return -EINVAL;
1856 }
1857
1858 if (ch->pending_pkt_sz) {
1859 pr_err("%s: packet of size: %d in progress\n", __func__,
1860 ch->pending_pkt_sz);
1861 return -EBUSY;
1862 }
1863 ch->pending_pkt_sz = len;
1864
1865 if (smd_stream_write_avail(ch) < (SMD_HEADER_SIZE)) {
1866 ch->pending_pkt_sz = 0;
1867 SMD_DBG("%s: no space to write packet header\n", __func__);
1868 return -EAGAIN;
1869 }
1870
1871 hdr[0] = len;
1872 hdr[1] = hdr[2] = hdr[3] = hdr[4] = 0;
1873
1874
1875 ret = smd_stream_write(ch, hdr, sizeof(hdr), 0);
1876 if (ret < 0 || ret != sizeof(hdr)) {
1877 ch->pending_pkt_sz = 0;
1878 pr_err("%s: packet header failed to write\n", __func__);
1879 return -EPERM;
1880 }
1881 return 0;
1882}
1883EXPORT_SYMBOL(smd_write_start);
1884
1885int smd_write_segment(smd_channel_t *ch, void *data, int len, int user_buf)
1886{
1887 int bytes_written;
1888
1889 if (!ch) {
1890 pr_err("%s: Invalid channel specified\n", __func__);
1891 return -ENODEV;
1892 }
1893 if (len < 1) {
1894 pr_err("%s: invalid length: %d\n", __func__, len);
1895 return -EINVAL;
1896 }
1897
1898 if (!ch->pending_pkt_sz) {
1899 pr_err("%s: no transaction in progress\n", __func__);
1900 return -ENOEXEC;
1901 }
1902 if (ch->pending_pkt_sz - len < 0) {
1903 pr_err("%s: segment of size: %d will make packet go over "
1904 "length\n", __func__, len);
1905 return -EINVAL;
1906 }
1907
1908 bytes_written = smd_stream_write(ch, data, len, user_buf);
1909
1910 ch->pending_pkt_sz -= bytes_written;
1911
1912 return bytes_written;
1913}
1914EXPORT_SYMBOL(smd_write_segment);
1915
1916int smd_write_end(smd_channel_t *ch)
1917{
1918
1919 if (!ch) {
1920 pr_err("%s: Invalid channel specified\n", __func__);
1921 return -ENODEV;
1922 }
1923 if (ch->pending_pkt_sz) {
1924 pr_err("%s: current packet not completely written\n", __func__);
1925 return -E2BIG;
1926 }
1927
1928 return 0;
1929}
1930EXPORT_SYMBOL(smd_write_end);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001931
1932int smd_read(smd_channel_t *ch, void *data, int len)
1933{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001934 return ch->read(ch, data, len, 0);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001935}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001936EXPORT_SYMBOL(smd_read);
1937
1938int smd_read_user_buffer(smd_channel_t *ch, void *data, int len)
1939{
1940 return ch->read(ch, data, len, 1);
1941}
1942EXPORT_SYMBOL(smd_read_user_buffer);
1943
1944int smd_read_from_cb(smd_channel_t *ch, void *data, int len)
1945{
1946 return ch->read_from_cb(ch, data, len, 0);
1947}
1948EXPORT_SYMBOL(smd_read_from_cb);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001949
1950int smd_write(smd_channel_t *ch, const void *data, int len)
1951{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001952 return ch->pending_pkt_sz ? -EBUSY : ch->write(ch, data, len, 0);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001953}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001954EXPORT_SYMBOL(smd_write);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001955
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001956int smd_write_user_buffer(smd_channel_t *ch, const void *data, int len)
Brian Swetland636eb9c2009-12-07 15:28:08 -08001957{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001958 return ch->pending_pkt_sz ? -EBUSY : ch->write(ch, data, len, 1);
Brian Swetland636eb9c2009-12-07 15:28:08 -08001959}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001960EXPORT_SYMBOL(smd_write_user_buffer);
Brian Swetland636eb9c2009-12-07 15:28:08 -08001961
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001962int smd_read_avail(smd_channel_t *ch)
1963{
1964 return ch->read_avail(ch);
1965}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001966EXPORT_SYMBOL(smd_read_avail);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001967
1968int smd_write_avail(smd_channel_t *ch)
1969{
1970 return ch->write_avail(ch);
1971}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001972EXPORT_SYMBOL(smd_write_avail);
1973
1974void smd_enable_read_intr(smd_channel_t *ch)
1975{
1976 if (ch)
1977 ch->send->fBLOCKREADINTR = 0;
1978}
1979EXPORT_SYMBOL(smd_enable_read_intr);
1980
1981void smd_disable_read_intr(smd_channel_t *ch)
1982{
1983 if (ch)
1984 ch->send->fBLOCKREADINTR = 1;
1985}
1986EXPORT_SYMBOL(smd_disable_read_intr);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001987
1988int smd_wait_until_readable(smd_channel_t *ch, int bytes)
1989{
1990 return -1;
1991}
1992
1993int smd_wait_until_writable(smd_channel_t *ch, int bytes)
1994{
1995 return -1;
1996}
1997
1998int smd_cur_packet_size(smd_channel_t *ch)
1999{
2000 return ch->current_packet;
2001}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002002EXPORT_SYMBOL(smd_cur_packet_size);
2003
2004int smd_tiocmget(smd_channel_t *ch)
2005{
2006 return (ch->recv->fDSR ? TIOCM_DSR : 0) |
2007 (ch->recv->fCTS ? TIOCM_CTS : 0) |
2008 (ch->recv->fCD ? TIOCM_CD : 0) |
2009 (ch->recv->fRI ? TIOCM_RI : 0) |
2010 (ch->send->fCTS ? TIOCM_RTS : 0) |
2011 (ch->send->fDSR ? TIOCM_DTR : 0);
2012}
2013EXPORT_SYMBOL(smd_tiocmget);
2014
Vamsi Krishnacb12a102011-08-17 15:18:26 -07002015/* this api will be called while holding smd_lock */
2016int
2017smd_tiocmset_from_cb(smd_channel_t *ch, unsigned int set, unsigned int clear)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002018{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002019 if (set & TIOCM_DTR)
2020 ch->send->fDSR = 1;
2021
2022 if (set & TIOCM_RTS)
2023 ch->send->fCTS = 1;
2024
2025 if (clear & TIOCM_DTR)
2026 ch->send->fDSR = 0;
2027
2028 if (clear & TIOCM_RTS)
2029 ch->send->fCTS = 0;
2030
2031 ch->send->fSTATE = 1;
2032 barrier();
2033 ch->notify_other_cpu();
Vamsi Krishnacb12a102011-08-17 15:18:26 -07002034
2035 return 0;
2036}
2037EXPORT_SYMBOL(smd_tiocmset_from_cb);
2038
2039int smd_tiocmset(smd_channel_t *ch, unsigned int set, unsigned int clear)
2040{
2041 unsigned long flags;
2042
2043 spin_lock_irqsave(&smd_lock, flags);
2044 smd_tiocmset_from_cb(ch, set, clear);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002045 spin_unlock_irqrestore(&smd_lock, flags);
2046
2047 return 0;
2048}
2049EXPORT_SYMBOL(smd_tiocmset);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002050
2051
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002052/* -------------------------------------------------------------------------- */
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002053
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002054/* smem_alloc returns the pointer to smem item if it is already allocated.
2055 * Otherwise, it returns NULL.
2056 */
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002057void *smem_alloc(unsigned id, unsigned size)
2058{
2059 return smem_find(id, size);
2060}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002061EXPORT_SYMBOL(smem_alloc);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002062
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002063/* smem_alloc2 returns the pointer to smem item. If it is not allocated,
2064 * it allocates it and then returns the pointer to it.
2065 */
Angshuman Sarkar4eade0d2011-08-17 14:06:23 +05302066void *smem_alloc2(unsigned id, unsigned size_in)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002067{
2068 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
2069 struct smem_heap_entry *toc = shared->heap_toc;
2070 unsigned long flags;
2071 void *ret = NULL;
2072
2073 if (!shared->heap_info.initialized) {
2074 pr_err("%s: smem heap info not initialized\n", __func__);
2075 return NULL;
2076 }
2077
2078 if (id >= SMEM_NUM_ITEMS)
2079 return NULL;
2080
2081 size_in = ALIGN(size_in, 8);
2082 remote_spin_lock_irqsave(&remote_spinlock, flags);
2083 if (toc[id].allocated) {
2084 SMD_DBG("%s: %u already allocated\n", __func__, id);
2085 if (size_in != toc[id].size)
2086 pr_err("%s: wrong size %u (expected %u)\n",
2087 __func__, toc[id].size, size_in);
2088 else
2089 ret = (void *)(MSM_SHARED_RAM_BASE + toc[id].offset);
2090 } else if (id > SMEM_FIXED_ITEM_LAST) {
2091 SMD_DBG("%s: allocating %u\n", __func__, id);
2092 if (shared->heap_info.heap_remaining >= size_in) {
2093 toc[id].offset = shared->heap_info.free_offset;
2094 toc[id].size = size_in;
2095 wmb();
2096 toc[id].allocated = 1;
2097
2098 shared->heap_info.free_offset += size_in;
2099 shared->heap_info.heap_remaining -= size_in;
2100 ret = (void *)(MSM_SHARED_RAM_BASE + toc[id].offset);
2101 } else
2102 pr_err("%s: not enough memory %u (required %u)\n",
2103 __func__, shared->heap_info.heap_remaining,
2104 size_in);
2105 }
2106 wmb();
2107 remote_spin_unlock_irqrestore(&remote_spinlock, flags);
2108 return ret;
2109}
Angshuman Sarkar4eade0d2011-08-17 14:06:23 +05302110EXPORT_SYMBOL(smem_alloc2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002111
2112void *smem_get_entry(unsigned id, unsigned *size)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002113{
2114 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
2115 struct smem_heap_entry *toc = shared->heap_toc;
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +05302116 int use_spinlocks = spinlocks_initialized;
2117 void *ret = 0;
2118 unsigned long flags = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002119
2120 if (id >= SMEM_NUM_ITEMS)
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +05302121 return ret;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002122
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +05302123 if (use_spinlocks)
2124 remote_spin_lock_irqsave(&remote_spinlock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002125 /* toc is in device memory and cannot be speculatively accessed */
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002126 if (toc[id].allocated) {
2127 *size = toc[id].size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002128 barrier();
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +05302129 ret = (void *) (MSM_SHARED_RAM_BASE + toc[id].offset);
Brian Swetland5b0f5a32009-04-26 18:38:49 -07002130 } else {
2131 *size = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002132 }
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +05302133 if (use_spinlocks)
2134 remote_spin_unlock_irqrestore(&remote_spinlock, flags);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002135
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +05302136 return ret;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002137}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002138EXPORT_SYMBOL(smem_get_entry);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002139
2140void *smem_find(unsigned id, unsigned size_in)
2141{
2142 unsigned size;
2143 void *ptr;
2144
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002145 ptr = smem_get_entry(id, &size);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002146 if (!ptr)
2147 return 0;
2148
2149 size_in = ALIGN(size_in, 8);
2150 if (size_in != size) {
2151 pr_err("smem_find(%d, %d): wrong size %d\n",
2152 id, size_in, size);
2153 return 0;
2154 }
2155
2156 return ptr;
2157}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002158EXPORT_SYMBOL(smem_find);
2159
2160static int smsm_cb_init(void)
2161{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002162 struct smsm_state_info *state_info;
2163 int n;
2164 int ret = 0;
2165
2166 smsm_states = kmalloc(sizeof(struct smsm_state_info)*SMSM_NUM_ENTRIES,
2167 GFP_KERNEL);
2168
2169 if (!smsm_states) {
2170 pr_err("%s: SMSM init failed\n", __func__);
2171 return -ENOMEM;
2172 }
2173
Eric Holmbergc8002902011-09-16 13:55:57 -06002174 mutex_lock(&smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002175 for (n = 0; n < SMSM_NUM_ENTRIES; n++) {
2176 state_info = &smsm_states[n];
2177 state_info->last_value = __raw_readl(SMSM_STATE_ADDR(n));
2178 INIT_LIST_HEAD(&state_info->callbacks);
2179 }
Eric Holmbergc8002902011-09-16 13:55:57 -06002180 mutex_unlock(&smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002181
2182 return ret;
2183}
2184
2185static int smsm_init(void)
2186{
2187 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
2188 int i;
2189 struct smsm_size_info_type *smsm_size_info;
2190
2191 i = remote_spin_lock_init(&remote_spinlock, SMEM_SPINLOCK_SMEM_ALLOC);
2192 if (i) {
2193 pr_err("%s: remote spinlock init failed %d\n", __func__, i);
2194 return i;
2195 }
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +05302196 spinlocks_initialized = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002197
2198 smsm_size_info = smem_alloc(SMEM_SMSM_SIZE_INFO,
2199 sizeof(struct smsm_size_info_type));
2200 if (smsm_size_info) {
2201 SMSM_NUM_ENTRIES = smsm_size_info->num_entries;
2202 SMSM_NUM_HOSTS = smsm_size_info->num_hosts;
2203 }
2204
Eric Holmbergc7e8daf2011-12-28 11:49:21 -07002205 i = kfifo_alloc(&smsm_snapshot_fifo,
2206 sizeof(uint32_t) * SMSM_NUM_ENTRIES * SMSM_SNAPSHOT_CNT,
2207 GFP_KERNEL);
2208 if (i) {
2209 pr_err("%s: SMSM state fifo alloc failed %d\n", __func__, i);
2210 return i;
2211 }
2212
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002213 if (!smsm_info.state) {
2214 smsm_info.state = smem_alloc2(ID_SHARED_STATE,
2215 SMSM_NUM_ENTRIES *
2216 sizeof(uint32_t));
2217
2218 if (smsm_info.state) {
2219 __raw_writel(0, SMSM_STATE_ADDR(SMSM_APPS_STATE));
2220 if ((shared->version[VERSION_MODEM] >> 16) >= 0xB)
2221 __raw_writel(0, \
2222 SMSM_STATE_ADDR(SMSM_APPS_DEM_I));
2223 }
2224 }
2225
2226 if (!smsm_info.intr_mask) {
2227 smsm_info.intr_mask = smem_alloc2(SMEM_SMSM_CPU_INTR_MASK,
2228 SMSM_NUM_ENTRIES *
2229 SMSM_NUM_HOSTS *
2230 sizeof(uint32_t));
2231
2232 if (smsm_info.intr_mask)
2233 for (i = 0; i < SMSM_NUM_ENTRIES; i++)
2234 __raw_writel(0xffffffff,
2235 SMSM_INTR_MASK_ADDR(i, SMSM_APPS));
2236 }
2237
2238 if (!smsm_info.intr_mux)
2239 smsm_info.intr_mux = smem_alloc2(SMEM_SMD_SMSM_INTR_MUX,
2240 SMSM_NUM_INTR_MUX *
2241 sizeof(uint32_t));
2242
2243 i = smsm_cb_init();
2244 if (i)
2245 return i;
2246
2247 wmb();
2248 return 0;
2249}
2250
2251void smsm_reset_modem(unsigned mode)
2252{
2253 if (mode == SMSM_SYSTEM_DOWNLOAD) {
2254 mode = SMSM_RESET | SMSM_SYSTEM_DOWNLOAD;
2255 } else if (mode == SMSM_MODEM_WAIT) {
2256 mode = SMSM_RESET | SMSM_MODEM_WAIT;
2257 } else { /* reset_mode is SMSM_RESET or default */
2258 mode = SMSM_RESET;
2259 }
2260
2261 smsm_change_state(SMSM_APPS_STATE, mode, mode);
2262}
2263EXPORT_SYMBOL(smsm_reset_modem);
2264
2265void smsm_reset_modem_cont(void)
2266{
2267 unsigned long flags;
2268 uint32_t state;
2269
2270 if (!smsm_info.state)
2271 return;
2272
2273 spin_lock_irqsave(&smem_lock, flags);
2274 state = __raw_readl(SMSM_STATE_ADDR(SMSM_APPS_STATE)) \
2275 & ~SMSM_MODEM_WAIT;
2276 __raw_writel(state, SMSM_STATE_ADDR(SMSM_APPS_STATE));
2277 wmb();
2278 spin_unlock_irqrestore(&smem_lock, flags);
2279}
2280EXPORT_SYMBOL(smsm_reset_modem_cont);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002281
Eric Holmbergc7e8daf2011-12-28 11:49:21 -07002282static void smsm_cb_snapshot(void)
2283{
2284 int n;
2285 uint32_t new_state;
2286 int ret;
2287
2288 ret = kfifo_avail(&smsm_snapshot_fifo);
2289 if (ret < (SMSM_NUM_ENTRIES * 4)) {
2290 pr_err("%s: SMSM snapshot full %d\n", __func__, ret);
2291 return;
2292 }
2293
2294 for (n = 0; n < SMSM_NUM_ENTRIES; n++) {
2295 new_state = __raw_readl(SMSM_STATE_ADDR(n));
2296
2297 ret = kfifo_in(&smsm_snapshot_fifo,
2298 &new_state, sizeof(new_state));
2299 if (ret != sizeof(new_state)) {
2300 pr_err("%s: SMSM snapshot failure %d\n", __func__, ret);
2301 return;
2302 }
2303 }
2304 schedule_work(&smsm_cb_work);
2305}
2306
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002307static irqreturn_t smsm_irq_handler(int irq, void *data)
2308{
2309 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002310
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002311 if (irq == INT_ADSP_A11_SMSM) {
Eric Holmberg6282c5d2011-10-27 17:30:57 -06002312 uint32_t mux_val;
2313 static uint32_t prev_smem_q6_apps_smsm;
2314
2315 if (smsm_info.intr_mux && cpu_is_qsd8x50()) {
2316 mux_val = __raw_readl(
2317 SMSM_INTR_MUX_ADDR(SMEM_Q6_APPS_SMSM));
2318 if (mux_val != prev_smem_q6_apps_smsm)
2319 prev_smem_q6_apps_smsm = mux_val;
2320 }
2321
Eric Holmbergc7e8daf2011-12-28 11:49:21 -07002322 spin_lock_irqsave(&smem_lock, flags);
2323 smsm_cb_snapshot();
2324 spin_unlock_irqrestore(&smem_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002325 return IRQ_HANDLED;
2326 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002327
2328 spin_lock_irqsave(&smem_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002329 if (!smsm_info.state) {
2330 SMSM_INFO("<SM NO STATE>\n");
2331 } else {
2332 unsigned old_apps, apps;
2333 unsigned modm = __raw_readl(SMSM_STATE_ADDR(SMSM_MODEM_STATE));
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002334
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002335 old_apps = apps = __raw_readl(SMSM_STATE_ADDR(SMSM_APPS_STATE));
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002336
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002337 SMSM_DBG("<SM %08x %08x>\n", apps, modm);
2338 if (apps & SMSM_RESET) {
2339 /* If we get an interrupt and the apps SMSM_RESET
2340 bit is already set, the modem is acking the
2341 app's reset ack. */
Eric Holmberg2bb6ccd2012-03-13 13:05:14 -06002342 if (!disable_smsm_reset_handshake)
Angshuman Sarkaread67bd2011-09-21 20:13:12 +05302343 apps &= ~SMSM_RESET;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002344 /* Issue a fake irq to handle any
2345 * smd state changes during reset
2346 */
2347 smd_fake_irq_handler(0);
Brian Swetland5b0f5a32009-04-26 18:38:49 -07002348
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002349 /* queue modem restart notify chain */
2350 modem_queue_start_reset_notify();
2351
2352 } else if (modm & SMSM_RESET) {
Eric Holmberg2bb6ccd2012-03-13 13:05:14 -06002353 if (!disable_smsm_reset_handshake)
Angshuman Sarkaread67bd2011-09-21 20:13:12 +05302354 apps |= SMSM_RESET;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002355
2356 pr_err("\nSMSM: Modem SMSM state changed to SMSM_RESET.");
2357 modem_queue_start_reset_notify();
2358
2359 } else if (modm & SMSM_INIT) {
2360 if (!(apps & SMSM_INIT)) {
2361 apps |= SMSM_INIT;
2362 modem_queue_smsm_init_notify();
2363 }
2364
2365 if (modm & SMSM_SMDINIT)
2366 apps |= SMSM_SMDINIT;
2367 if ((apps & (SMSM_INIT | SMSM_SMDINIT | SMSM_RPCINIT)) ==
2368 (SMSM_INIT | SMSM_SMDINIT | SMSM_RPCINIT))
2369 apps |= SMSM_RUN;
2370 } else if (modm & SMSM_SYSTEM_DOWNLOAD) {
2371 pr_err("\nSMSM: Modem SMSM state changed to SMSM_SYSTEM_DOWNLOAD.");
2372 modem_queue_start_reset_notify();
2373 }
2374
2375 if (old_apps != apps) {
2376 SMSM_DBG("<SM %08x NOTIFY>\n", apps);
2377 __raw_writel(apps, SMSM_STATE_ADDR(SMSM_APPS_STATE));
2378 do_smd_probe();
2379 notify_other_smsm(SMSM_APPS_STATE, (old_apps ^ apps));
2380 }
2381
Eric Holmbergc7e8daf2011-12-28 11:49:21 -07002382 smsm_cb_snapshot();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002383 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002384 spin_unlock_irqrestore(&smem_lock, flags);
2385 return IRQ_HANDLED;
2386}
2387
Eric Holmberg98c6c642012-02-24 11:29:35 -07002388static irqreturn_t smsm_modem_irq_handler(int irq, void *data)
2389{
2390 SMx_POWER_INFO("SMSM Int Modem->Apps\n");
Eric Holmberg7ad623a2012-03-01 14:41:10 -07002391 ++interrupt_stats[SMD_MODEM].smsm_in_count;
Eric Holmberg98c6c642012-02-24 11:29:35 -07002392 return smsm_irq_handler(irq, data);
2393}
2394
2395static irqreturn_t smsm_dsp_irq_handler(int irq, void *data)
2396{
2397 SMx_POWER_INFO("SMSM Int LPASS->Apps\n");
Eric Holmberg7ad623a2012-03-01 14:41:10 -07002398 ++interrupt_stats[SMD_Q6].smsm_in_count;
Eric Holmberg98c6c642012-02-24 11:29:35 -07002399 return smsm_irq_handler(irq, data);
2400}
2401
2402static irqreturn_t smsm_dsps_irq_handler(int irq, void *data)
2403{
2404 SMx_POWER_INFO("SMSM Int DSPS->Apps\n");
Eric Holmberg7ad623a2012-03-01 14:41:10 -07002405 ++interrupt_stats[SMD_DSPS].smsm_in_count;
Eric Holmberg98c6c642012-02-24 11:29:35 -07002406 return smsm_irq_handler(irq, data);
2407}
2408
2409static irqreturn_t smsm_wcnss_irq_handler(int irq, void *data)
2410{
2411 SMx_POWER_INFO("SMSM Int WCNSS->Apps\n");
Eric Holmberg7ad623a2012-03-01 14:41:10 -07002412 ++interrupt_stats[SMD_WCNSS].smsm_in_count;
Eric Holmberg98c6c642012-02-24 11:29:35 -07002413 return smsm_irq_handler(irq, data);
2414}
2415
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002416int smsm_change_intr_mask(uint32_t smsm_entry,
2417 uint32_t clear_mask, uint32_t set_mask)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002418{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002419 uint32_t old_mask, new_mask;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002420 unsigned long flags;
Brian Swetland5b0f5a32009-04-26 18:38:49 -07002421
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002422 if (smsm_entry >= SMSM_NUM_ENTRIES) {
2423 pr_err("smsm_change_state: Invalid entry %d\n",
2424 smsm_entry);
2425 return -EINVAL;
2426 }
2427
2428 if (!smsm_info.intr_mask) {
2429 pr_err("smsm_change_intr_mask <SM NO STATE>\n");
Brian Swetland5b0f5a32009-04-26 18:38:49 -07002430 return -EIO;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002431 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002432
2433 spin_lock_irqsave(&smem_lock, flags);
2434
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002435 old_mask = __raw_readl(SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_APPS));
2436 new_mask = (old_mask & ~clear_mask) | set_mask;
2437 __raw_writel(new_mask, SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_APPS));
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002438
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002439 wmb();
2440 spin_unlock_irqrestore(&smem_lock, flags);
Brian Swetland5b0f5a32009-04-26 18:38:49 -07002441
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002442 return 0;
2443}
2444EXPORT_SYMBOL(smsm_change_intr_mask);
2445
2446int smsm_get_intr_mask(uint32_t smsm_entry, uint32_t *intr_mask)
2447{
2448 if (smsm_entry >= SMSM_NUM_ENTRIES) {
2449 pr_err("smsm_change_state: Invalid entry %d\n",
2450 smsm_entry);
2451 return -EINVAL;
2452 }
2453
2454 if (!smsm_info.intr_mask) {
2455 pr_err("smsm_change_intr_mask <SM NO STATE>\n");
2456 return -EIO;
2457 }
2458
2459 *intr_mask = __raw_readl(SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_APPS));
2460 return 0;
2461}
2462EXPORT_SYMBOL(smsm_get_intr_mask);
2463
2464int smsm_change_state(uint32_t smsm_entry,
2465 uint32_t clear_mask, uint32_t set_mask)
2466{
2467 unsigned long flags;
2468 uint32_t old_state, new_state;
2469
2470 if (smsm_entry >= SMSM_NUM_ENTRIES) {
2471 pr_err("smsm_change_state: Invalid entry %d",
2472 smsm_entry);
2473 return -EINVAL;
2474 }
2475
2476 if (!smsm_info.state) {
2477 pr_err("smsm_change_state <SM NO STATE>\n");
2478 return -EIO;
2479 }
2480 spin_lock_irqsave(&smem_lock, flags);
2481
2482 old_state = __raw_readl(SMSM_STATE_ADDR(smsm_entry));
2483 new_state = (old_state & ~clear_mask) | set_mask;
2484 __raw_writel(new_state, SMSM_STATE_ADDR(smsm_entry));
2485 SMSM_DBG("smsm_change_state %x\n", new_state);
2486 notify_other_smsm(SMSM_APPS_STATE, (old_state ^ new_state));
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002487
2488 spin_unlock_irqrestore(&smem_lock, flags);
2489
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002490 return 0;
2491}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002492EXPORT_SYMBOL(smsm_change_state);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002493
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002494uint32_t smsm_get_state(uint32_t smsm_entry)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002495{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002496 uint32_t rv = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002497
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002498 /* needs interface change to return error code */
2499 if (smsm_entry >= SMSM_NUM_ENTRIES) {
2500 pr_err("smsm_change_state: Invalid entry %d",
2501 smsm_entry);
2502 return 0;
2503 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002504
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002505 if (!smsm_info.state) {
2506 pr_err("smsm_get_state <SM NO STATE>\n");
2507 } else {
2508 rv = __raw_readl(SMSM_STATE_ADDR(smsm_entry));
2509 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002510
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002511 return rv;
2512}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002513EXPORT_SYMBOL(smsm_get_state);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002514
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002515/**
2516 * Performs SMSM callback client notifiction.
2517 */
2518void notify_smsm_cb_clients_worker(struct work_struct *work)
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -07002519{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002520 struct smsm_state_cb_info *cb_info;
2521 struct smsm_state_info *state_info;
2522 int n;
2523 uint32_t new_state;
2524 uint32_t state_changes;
Eric Holmbergc7e8daf2011-12-28 11:49:21 -07002525 int ret;
2526 int snapshot_size = SMSM_NUM_ENTRIES * sizeof(uint32_t);
Brian Swetland03e00cd2009-07-01 17:58:37 -07002527
Eric Holmbergc7e8daf2011-12-28 11:49:21 -07002528 if (!smd_initialized)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002529 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002530
Eric Holmbergc7e8daf2011-12-28 11:49:21 -07002531 while (kfifo_len(&smsm_snapshot_fifo) >= snapshot_size) {
2532 mutex_lock(&smsm_lock);
2533 for (n = 0; n < SMSM_NUM_ENTRIES; n++) {
2534 state_info = &smsm_states[n];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002535
Eric Holmbergc7e8daf2011-12-28 11:49:21 -07002536 ret = kfifo_out(&smsm_snapshot_fifo, &new_state,
2537 sizeof(new_state));
2538 if (ret != sizeof(new_state)) {
2539 pr_err("%s: snapshot underflow %d\n",
2540 __func__, ret);
2541 mutex_unlock(&smsm_lock);
2542 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002543 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002544
Eric Holmbergc7e8daf2011-12-28 11:49:21 -07002545 state_changes = state_info->last_value ^ new_state;
2546 if (state_changes) {
Eric Holmberg98c6c642012-02-24 11:29:35 -07002547 SMx_POWER_INFO("SMSM Change %d: %08x->%08x\n",
2548 n, state_info->last_value,
2549 new_state);
Eric Holmbergc7e8daf2011-12-28 11:49:21 -07002550 list_for_each_entry(cb_info,
2551 &state_info->callbacks, cb_list) {
2552
2553 if (cb_info->mask & state_changes)
2554 cb_info->notify(cb_info->data,
2555 state_info->last_value,
2556 new_state);
2557 }
2558 state_info->last_value = new_state;
2559 }
2560 }
2561 mutex_unlock(&smsm_lock);
2562 }
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -07002563}
2564
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -07002565
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002566/**
2567 * Registers callback for SMSM state notifications when the specified
2568 * bits change.
2569 *
2570 * @smsm_entry Processor entry to deregister
2571 * @mask Bits to deregister (if result is 0, callback is removed)
2572 * @notify Notification function to deregister
2573 * @data Opaque data passed in to callback
2574 *
2575 * @returns Status code
2576 * <0 error code
2577 * 0 inserted new entry
2578 * 1 updated mask of existing entry
2579 */
2580int smsm_state_cb_register(uint32_t smsm_entry, uint32_t mask,
2581 void (*notify)(void *, uint32_t, uint32_t), void *data)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002582{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002583 struct smsm_state_cb_info *cb_info;
2584 struct smsm_state_cb_info *cb_found = 0;
2585 int ret = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002586
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002587 if (smsm_entry >= SMSM_NUM_ENTRIES)
2588 return -EINVAL;
2589
Eric Holmbergc8002902011-09-16 13:55:57 -06002590 mutex_lock(&smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002591
2592 if (!smsm_states) {
2593 /* smsm not yet initialized */
2594 ret = -ENODEV;
2595 goto cleanup;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002596 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002597
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002598 list_for_each_entry(cb_info,
2599 &smsm_states[smsm_entry].callbacks, cb_list) {
2600 if ((cb_info->notify == notify) &&
2601 (cb_info->data == data)) {
2602 cb_info->mask |= mask;
2603 cb_found = cb_info;
2604 ret = 1;
Brian Swetland5b0f5a32009-04-26 18:38:49 -07002605 break;
2606 }
2607 }
2608
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002609 if (!cb_found) {
2610 cb_info = kmalloc(sizeof(struct smsm_state_cb_info),
2611 GFP_ATOMIC);
2612 if (!cb_info) {
2613 ret = -ENOMEM;
2614 goto cleanup;
2615 }
2616
2617 cb_info->mask = mask;
2618 cb_info->notify = notify;
2619 cb_info->data = data;
2620 INIT_LIST_HEAD(&cb_info->cb_list);
2621 list_add_tail(&cb_info->cb_list,
2622 &smsm_states[smsm_entry].callbacks);
2623 }
2624
2625cleanup:
Eric Holmbergc8002902011-09-16 13:55:57 -06002626 mutex_unlock(&smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002627 return ret;
2628}
2629EXPORT_SYMBOL(smsm_state_cb_register);
2630
2631
2632/**
2633 * Deregisters for SMSM state notifications for the specified bits.
2634 *
2635 * @smsm_entry Processor entry to deregister
2636 * @mask Bits to deregister (if result is 0, callback is removed)
2637 * @notify Notification function to deregister
2638 * @data Opaque data passed in to callback
2639 *
2640 * @returns Status code
2641 * <0 error code
2642 * 0 not found
2643 * 1 updated mask
2644 * 2 removed callback
2645 */
2646int smsm_state_cb_deregister(uint32_t smsm_entry, uint32_t mask,
2647 void (*notify)(void *, uint32_t, uint32_t), void *data)
2648{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002649 struct smsm_state_cb_info *cb_info;
2650 int ret = 0;
2651
2652 if (smsm_entry >= SMSM_NUM_ENTRIES)
2653 return -EINVAL;
2654
Eric Holmbergc8002902011-09-16 13:55:57 -06002655 mutex_lock(&smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002656
2657 if (!smsm_states) {
2658 /* smsm not yet initialized */
Eric Holmbergc8002902011-09-16 13:55:57 -06002659 mutex_unlock(&smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002660 return -ENODEV;
2661 }
2662
2663 list_for_each_entry(cb_info,
2664 &smsm_states[smsm_entry].callbacks, cb_list) {
2665 if ((cb_info->notify == notify) &&
2666 (cb_info->data == data)) {
2667 cb_info->mask &= ~mask;
2668 ret = 1;
2669 if (!cb_info->mask) {
2670 /* no mask bits set, remove callback */
2671 list_del(&cb_info->cb_list);
2672 kfree(cb_info);
2673 ret = 2;
2674 }
2675 break;
2676 }
2677 }
2678
Eric Holmbergc8002902011-09-16 13:55:57 -06002679 mutex_unlock(&smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002680 return ret;
2681}
2682EXPORT_SYMBOL(smsm_state_cb_deregister);
2683
2684
2685int smd_core_init(void)
2686{
2687 int r;
2688 unsigned long flags = IRQF_TRIGGER_RISING;
2689 SMD_INFO("smd_core_init()\n");
Brian Swetland5b0f5a32009-04-26 18:38:49 -07002690
Brian Swetland37521a32009-07-01 18:30:47 -07002691 r = request_irq(INT_A9_M2A_0, smd_modem_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002692 flags, "smd_dev", 0);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002693 if (r < 0)
2694 return r;
2695 r = enable_irq_wake(INT_A9_M2A_0);
2696 if (r < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002697 pr_err("smd_core_init: "
2698 "enable_irq_wake failed for INT_A9_M2A_0\n");
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002699
Eric Holmberg98c6c642012-02-24 11:29:35 -07002700 r = request_irq(INT_A9_M2A_5, smsm_modem_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002701 flags, "smsm_dev", 0);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002702 if (r < 0) {
2703 free_irq(INT_A9_M2A_0, 0);
2704 return r;
2705 }
2706 r = enable_irq_wake(INT_A9_M2A_5);
2707 if (r < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002708 pr_err("smd_core_init: "
2709 "enable_irq_wake failed for INT_A9_M2A_5\n");
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002710
Brian Swetland37521a32009-07-01 18:30:47 -07002711#if defined(CONFIG_QDSP6)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002712#if (INT_ADSP_A11 == INT_ADSP_A11_SMSM)
2713 flags |= IRQF_SHARED;
2714#endif
Brian Swetland37521a32009-07-01 18:30:47 -07002715 r = request_irq(INT_ADSP_A11, smd_dsp_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002716 flags, "smd_dev", smd_dsp_irq_handler);
Brian Swetland37521a32009-07-01 18:30:47 -07002717 if (r < 0) {
2718 free_irq(INT_A9_M2A_0, 0);
2719 free_irq(INT_A9_M2A_5, 0);
2720 return r;
2721 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002722
Eric Holmberg98c6c642012-02-24 11:29:35 -07002723 r = request_irq(INT_ADSP_A11_SMSM, smsm_dsp_irq_handler,
2724 flags, "smsm_dev", smsm_dsp_irq_handler);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002725 if (r < 0) {
2726 free_irq(INT_A9_M2A_0, 0);
2727 free_irq(INT_A9_M2A_5, 0);
2728 free_irq(INT_ADSP_A11, smd_dsp_irq_handler);
2729 return r;
2730 }
2731
2732 r = enable_irq_wake(INT_ADSP_A11);
2733 if (r < 0)
2734 pr_err("smd_core_init: "
2735 "enable_irq_wake failed for INT_ADSP_A11\n");
2736
2737#if (INT_ADSP_A11 != INT_ADSP_A11_SMSM)
2738 r = enable_irq_wake(INT_ADSP_A11_SMSM);
2739 if (r < 0)
2740 pr_err("smd_core_init: enable_irq_wake "
2741 "failed for INT_ADSP_A11_SMSM\n");
2742#endif
2743 flags &= ~IRQF_SHARED;
Brian Swetland37521a32009-07-01 18:30:47 -07002744#endif
2745
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002746#if defined(CONFIG_DSPS)
2747 r = request_irq(INT_DSPS_A11, smd_dsps_irq_handler,
2748 flags, "smd_dev", smd_dsps_irq_handler);
2749 if (r < 0) {
2750 free_irq(INT_A9_M2A_0, 0);
2751 free_irq(INT_A9_M2A_5, 0);
2752 free_irq(INT_ADSP_A11, smd_dsp_irq_handler);
Eric Holmberg98c6c642012-02-24 11:29:35 -07002753 free_irq(INT_ADSP_A11_SMSM, smsm_dsp_irq_handler);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002754 return r;
2755 }
Brian Swetland5b0f5a32009-04-26 18:38:49 -07002756
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002757 r = enable_irq_wake(INT_DSPS_A11);
2758 if (r < 0)
2759 pr_err("smd_core_init: "
2760 "enable_irq_wake failed for INT_ADSP_A11\n");
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -07002761#endif
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002762
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002763#if defined(CONFIG_WCNSS)
2764 r = request_irq(INT_WCNSS_A11, smd_wcnss_irq_handler,
2765 flags, "smd_dev", smd_wcnss_irq_handler);
2766 if (r < 0) {
2767 free_irq(INT_A9_M2A_0, 0);
2768 free_irq(INT_A9_M2A_5, 0);
2769 free_irq(INT_ADSP_A11, smd_dsp_irq_handler);
Eric Holmberg98c6c642012-02-24 11:29:35 -07002770 free_irq(INT_ADSP_A11_SMSM, smsm_dsp_irq_handler);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002771 free_irq(INT_DSPS_A11, smd_dsps_irq_handler);
2772 return r;
2773 }
2774
2775 r = enable_irq_wake(INT_WCNSS_A11);
2776 if (r < 0)
2777 pr_err("smd_core_init: "
2778 "enable_irq_wake failed for INT_WCNSS_A11\n");
2779
Eric Holmberg98c6c642012-02-24 11:29:35 -07002780 r = request_irq(INT_WCNSS_A11_SMSM, smsm_wcnss_irq_handler,
2781 flags, "smsm_dev", smsm_wcnss_irq_handler);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002782 if (r < 0) {
2783 free_irq(INT_A9_M2A_0, 0);
2784 free_irq(INT_A9_M2A_5, 0);
2785 free_irq(INT_ADSP_A11, smd_dsp_irq_handler);
Eric Holmberg98c6c642012-02-24 11:29:35 -07002786 free_irq(INT_ADSP_A11_SMSM, smsm_dsp_irq_handler);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002787 free_irq(INT_DSPS_A11, smd_dsps_irq_handler);
2788 free_irq(INT_WCNSS_A11, smd_wcnss_irq_handler);
2789 return r;
2790 }
2791
2792 r = enable_irq_wake(INT_WCNSS_A11_SMSM);
2793 if (r < 0)
2794 pr_err("smd_core_init: "
2795 "enable_irq_wake failed for INT_WCNSS_A11_SMSM\n");
2796#endif
2797
Jeff Hugo6a8057c2011-08-16 13:47:12 -06002798#if defined(CONFIG_DSPS_SMSM)
Eric Holmberg98c6c642012-02-24 11:29:35 -07002799 r = request_irq(INT_DSPS_A11_SMSM, smsm_dsps_irq_handler,
2800 flags, "smsm_dev", smsm_dsps_irq_handler);
Jeff Hugo6a8057c2011-08-16 13:47:12 -06002801 if (r < 0) {
2802 free_irq(INT_A9_M2A_0, 0);
2803 free_irq(INT_A9_M2A_5, 0);
2804 free_irq(INT_ADSP_A11, smd_dsp_irq_handler);
Eric Holmberg98c6c642012-02-24 11:29:35 -07002805 free_irq(INT_ADSP_A11_SMSM, smsm_dsp_irq_handler);
Jeff Hugo6a8057c2011-08-16 13:47:12 -06002806 free_irq(INT_DSPS_A11, smd_dsps_irq_handler);
2807 free_irq(INT_WCNSS_A11, smd_wcnss_irq_handler);
Eric Holmberg98c6c642012-02-24 11:29:35 -07002808 free_irq(INT_WCNSS_A11_SMSM, smsm_wcnss_irq_handler);
Jeff Hugo6a8057c2011-08-16 13:47:12 -06002809 return r;
2810 }
2811
2812 r = enable_irq_wake(INT_DSPS_A11_SMSM);
2813 if (r < 0)
2814 pr_err("smd_core_init: "
2815 "enable_irq_wake failed for INT_DSPS_A11_SMSM\n");
2816#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002817 SMD_INFO("smd_core_init() done\n");
2818
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002819 return 0;
2820}
2821
Angshuman Sarkarbad32df2012-02-01 19:52:52 +05302822static int intr_init(struct interrupt_config_item *private_irq,
2823 struct smd_irq_config *platform_irq,
2824 struct platform_device *pdev
2825 )
2826{
2827 int irq_id;
2828 int ret;
2829 int ret_wake;
2830
2831 private_irq->out_bit_pos = platform_irq->out_bit_pos;
2832 private_irq->out_offset = platform_irq->out_offset;
2833 private_irq->out_base = platform_irq->out_base;
2834
2835 irq_id = platform_get_irq_byname(
2836 pdev,
2837 platform_irq->irq_name
2838 );
2839 SMD_DBG("smd: %s: register irq: %s id: %d\n", __func__,
2840 platform_irq->irq_name, irq_id);
2841 ret = request_irq(irq_id,
2842 private_irq->irq_handler,
2843 platform_irq->flags,
2844 platform_irq->device_name,
2845 (void *)platform_irq->dev_id
2846 );
2847 if (ret < 0) {
2848 platform_irq->irq_id = ret;
2849 } else {
2850 platform_irq->irq_id = irq_id;
2851 ret_wake = enable_irq_wake(irq_id);
2852 if (ret_wake < 0) {
2853 pr_err("smd: enable_irq_wake failed on %s",
2854 platform_irq->irq_name);
2855 }
2856 }
2857
2858 return ret;
2859}
2860
2861int smd_core_platform_init(struct platform_device *pdev)
2862{
2863 int i;
2864 int ret;
2865 uint32_t num_ss;
2866 struct smd_platform *smd_platform_data;
2867 struct smd_subsystem_config *smd_ss_config_list;
2868 struct smd_subsystem_config *cfg;
2869 int err_ret = 0;
2870
2871 smd_platform_data = pdev->dev.platform_data;
2872 num_ss = smd_platform_data->num_ss_configs;
2873 smd_ss_config_list = smd_platform_data->smd_ss_configs;
2874
Eric Holmberg2bb6ccd2012-03-13 13:05:14 -06002875 if (smd_platform_data->smd_ssr_config)
2876 disable_smsm_reset_handshake = smd_platform_data->
2877 smd_ssr_config->disable_smsm_reset_handshake;
2878
Angshuman Sarkarbad32df2012-02-01 19:52:52 +05302879 for (i = 0; i < num_ss; i++) {
2880 cfg = &smd_ss_config_list[i];
2881
2882 ret = intr_init(
2883 &private_intr_config[cfg->irq_config_id].smd,
2884 &cfg->smd_int,
2885 pdev
2886 );
2887
2888 if (ret < 0) {
2889 err_ret = ret;
2890 pr_err("smd: register irq failed on %s\n",
2891 cfg->smd_int.irq_name);
2892 break;
2893 }
2894
2895 ret = intr_init(
2896 &private_intr_config[cfg->irq_config_id].smsm,
2897 &cfg->smsm_int,
2898 pdev
2899 );
2900
2901 if (ret < 0) {
2902 err_ret = ret;
2903 pr_err("smd: register irq failed on %s\n",
2904 cfg->smsm_int.irq_name);
2905 break;
2906 }
Eric Holmberg17992c12012-02-29 12:54:44 -07002907
2908 strncpy(edge_to_pids[cfg->edge].subsys_name,
2909 cfg->subsys_name, SMD_MAX_CH_NAME_LEN);
Angshuman Sarkarbad32df2012-02-01 19:52:52 +05302910 }
2911
2912 if (err_ret < 0) {
2913 pr_err("smd: deregistering IRQs\n");
2914 for (i = 0; i < num_ss; ++i) {
2915 cfg = &smd_ss_config_list[i];
2916
2917 if (cfg->smd_int.irq_id >= 0)
2918 free_irq(cfg->smd_int.irq_id,
2919 (void *)cfg->smd_int.dev_id
2920 );
2921 if (cfg->smsm_int.irq_id >= 0)
2922 free_irq(cfg->smsm_int.irq_id,
2923 (void *)cfg->smsm_int.dev_id
2924 );
2925 }
2926 return err_ret;
2927 }
2928
2929 SMD_INFO("smd_core_platform_init() done\n");
2930 return 0;
2931
2932}
2933
Gregory Bean4416e9e2010-07-28 10:22:12 -07002934static int __devinit msm_smd_probe(struct platform_device *pdev)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002935{
Angshuman Sarkarbad32df2012-02-01 19:52:52 +05302936 int ret;
Daniel Walker0aec66d2010-03-18 12:31:08 -07002937
Angshuman Sarkarbad32df2012-02-01 19:52:52 +05302938 SMD_INFO("smd probe\n");
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002939 INIT_WORK(&probe_work, smd_channel_probe_worker);
2940
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002941 channel_close_wq = create_singlethread_workqueue("smd_channel_close");
2942 if (IS_ERR(channel_close_wq)) {
2943 pr_err("%s: create_singlethread_workqueue ENOMEM\n", __func__);
2944 return -ENOMEM;
2945 }
2946
2947 if (smsm_init()) {
2948 pr_err("smsm_init() failed\n");
2949 return -1;
2950 }
2951
Angshuman Sarkarbad32df2012-02-01 19:52:52 +05302952 if (pdev) {
2953 if (pdev->dev.of_node) {
2954 pr_err("SMD: Device tree not currently supported\n");
2955 return -ENODEV;
2956 } else if (pdev->dev.platform_data) {
2957 ret = smd_core_platform_init(pdev);
2958 if (ret) {
2959 pr_err(
2960 "SMD: smd_core_platform_init() failed\n");
2961 return -ENODEV;
2962 }
2963 } else {
2964 ret = smd_core_init();
2965 if (ret) {
2966 pr_err("smd_core_init() failed\n");
2967 return -ENODEV;
2968 }
2969 }
2970 } else {
2971 pr_err("SMD: PDEV not found\n");
2972 return -ENODEV;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002973 }
2974
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002975 smd_initialized = 1;
2976
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002977 smd_alloc_loopback_channel();
Eric Holmbergc33d4ab2011-10-24 10:28:25 -06002978 smsm_irq_handler(0, 0);
2979 tasklet_schedule(&smd_fake_irq_tasklet);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002980
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002981 return 0;
2982}
2983
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002984static int restart_notifier_cb(struct notifier_block *this,
2985 unsigned long code,
2986 void *data);
2987
2988static struct restart_notifier_block restart_notifiers[] = {
Eric Holmbergca7ead22011-12-01 17:21:15 -07002989 {SMD_MODEM, "modem", .nb.notifier_call = restart_notifier_cb},
2990 {SMD_Q6, "lpass", .nb.notifier_call = restart_notifier_cb},
2991 {SMD_WCNSS, "riva", .nb.notifier_call = restart_notifier_cb},
2992 {SMD_DSPS, "dsps", .nb.notifier_call = restart_notifier_cb},
Eric Holmberg8b0e74f2012-02-08 09:56:17 -07002993 {SMD_MODEM, "gss", .nb.notifier_call = restart_notifier_cb},
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002994};
2995
2996static int restart_notifier_cb(struct notifier_block *this,
2997 unsigned long code,
2998 void *data)
2999{
3000 if (code == SUBSYS_AFTER_SHUTDOWN) {
3001 struct restart_notifier_block *notifier;
3002
3003 notifier = container_of(this,
3004 struct restart_notifier_block, nb);
3005 SMD_INFO("%s: ssrestart for processor %d ('%s')\n",
3006 __func__, notifier->processor,
3007 notifier->name);
3008
3009 smd_channel_reset(notifier->processor);
3010 }
3011
3012 return NOTIFY_DONE;
3013}
3014
3015static __init int modem_restart_late_init(void)
3016{
3017 int i;
3018 void *handle;
3019 struct restart_notifier_block *nb;
3020
3021 for (i = 0; i < ARRAY_SIZE(restart_notifiers); i++) {
3022 nb = &restart_notifiers[i];
3023 handle = subsys_notif_register_notifier(nb->name, &nb->nb);
3024 SMD_DBG("%s: registering notif for '%s', handle=%p\n",
3025 __func__, nb->name, handle);
3026 }
3027 return 0;
3028}
3029late_initcall(modem_restart_late_init);
3030
Brian Swetland2eb44eb2008-09-29 16:00:48 -07003031static struct platform_driver msm_smd_driver = {
3032 .probe = msm_smd_probe,
3033 .driver = {
3034 .name = MODULE_NAME,
3035 .owner = THIS_MODULE,
3036 },
3037};
3038
3039static int __init msm_smd_init(void)
3040{
3041 return platform_driver_register(&msm_smd_driver);
3042}
3043
3044module_init(msm_smd_init);
3045
3046MODULE_DESCRIPTION("MSM Shared Memory Core");
3047MODULE_AUTHOR("Brian Swetland <swetland@google.com>");
3048MODULE_LICENSE("GPL");