blob: b95c3aafdbe79195161589f51d86c7217c60b1ed [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,
75};
76
77struct smsm_shared_info {
78 uint32_t *state;
79 uint32_t *intr_mask;
80 uint32_t *intr_mux;
81};
82
83static struct smsm_shared_info smsm_info;
Eric Holmbergc7e8daf2011-12-28 11:49:21 -070084struct kfifo smsm_snapshot_fifo;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070085
86struct smsm_size_info_type {
87 uint32_t num_hosts;
88 uint32_t num_entries;
89 uint32_t reserved0;
90 uint32_t reserved1;
91};
92
93struct smsm_state_cb_info {
94 struct list_head cb_list;
95 uint32_t mask;
96 void *data;
97 void (*notify)(void *data, uint32_t old_state, uint32_t new_state);
98};
99
100struct smsm_state_info {
101 struct list_head callbacks;
102 uint32_t last_value;
103};
104
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530105struct interrupt_config_item {
106 /* must be initialized */
107 irqreturn_t (*irq_handler)(int req, void *data);
108 /* outgoing interrupt config (set from platform data) */
109 uint32_t out_bit_pos;
110 void __iomem *out_base;
111 uint32_t out_offset;
112};
113
114struct interrupt_config {
115 struct interrupt_config_item smd;
116 struct interrupt_config_item smsm;
117};
118
119static irqreturn_t smd_modem_irq_handler(int irq, void *data);
120static irqreturn_t smd_dsp_irq_handler(int irq, void *data);
121static irqreturn_t smd_dsps_irq_handler(int irq, void *data);
122static irqreturn_t smd_wcnss_irq_handler(int irq, void *data);
123static irqreturn_t smsm_irq_handler(int irq, void *data);
124
125static struct interrupt_config private_intr_config[NUM_SMD_SUBSYSTEMS] = {
126 [SMD_MODEM] = {
127 .smd.irq_handler = smd_modem_irq_handler,
128 .smsm.irq_handler = smsm_irq_handler,
129 },
130 [SMD_Q6] = {
131 .smd.irq_handler = smd_dsp_irq_handler,
132 .smsm.irq_handler = smsm_irq_handler,
133 },
134 [SMD_DSPS] = {
135 .smd.irq_handler = smd_dsps_irq_handler,
136 .smsm.irq_handler = smsm_irq_handler,
137 },
138 [SMD_WCNSS] = {
139 .smd.irq_handler = smd_wcnss_irq_handler,
140 .smsm.irq_handler = smsm_irq_handler,
141 },
142};
143
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700144#define SMSM_STATE_ADDR(entry) (smsm_info.state + entry)
145#define SMSM_INTR_MASK_ADDR(entry, host) (smsm_info.intr_mask + \
146 entry * SMSM_NUM_HOSTS + host)
147#define SMSM_INTR_MUX_ADDR(entry) (smsm_info.intr_mux + entry)
148
149/* Internal definitions which are not exported in some targets */
150enum {
151 SMSM_APPS_DEM_I = 3,
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700152};
153
154static int msm_smd_debug_mask;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700155module_param_named(debug_mask, msm_smd_debug_mask,
156 int, S_IRUGO | S_IWUSR | S_IWGRP);
157
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700158#if defined(CONFIG_MSM_SMD_DEBUG)
159#define SMD_DBG(x...) do { \
160 if (msm_smd_debug_mask & MSM_SMD_DEBUG) \
161 printk(KERN_DEBUG x); \
162 } while (0)
163
164#define SMSM_DBG(x...) do { \
165 if (msm_smd_debug_mask & MSM_SMSM_DEBUG) \
166 printk(KERN_DEBUG x); \
167 } while (0)
168
169#define SMD_INFO(x...) do { \
170 if (msm_smd_debug_mask & MSM_SMD_INFO) \
171 printk(KERN_INFO x); \
172 } while (0)
173
174#define SMSM_INFO(x...) do { \
175 if (msm_smd_debug_mask & MSM_SMSM_INFO) \
176 printk(KERN_INFO x); \
177 } while (0)
178#else
179#define SMD_DBG(x...) do { } while (0)
180#define SMSM_DBG(x...) do { } while (0)
181#define SMD_INFO(x...) do { } while (0)
182#define SMSM_INFO(x...) do { } while (0)
183#endif
184
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700185static unsigned last_heap_free = 0xffffffff;
186
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700187static inline void smd_write_intr(unsigned int val,
188 const void __iomem *addr);
189
190#if defined(CONFIG_ARCH_MSM7X30)
191#define MSM_TRIG_A2M_SMD_INT \
192 (smd_write_intr(1 << 0, MSM_GCC_BASE + 0x8))
193#define MSM_TRIG_A2Q6_SMD_INT \
194 (smd_write_intr(1 << 8, MSM_GCC_BASE + 0x8))
195#define MSM_TRIG_A2M_SMSM_INT \
196 (smd_write_intr(1 << 5, MSM_GCC_BASE + 0x8))
197#define MSM_TRIG_A2Q6_SMSM_INT \
198 (smd_write_intr(1 << 8, MSM_GCC_BASE + 0x8))
199#define MSM_TRIG_A2DSPS_SMD_INT
Jeff Hugo6a8057c2011-08-16 13:47:12 -0600200#define MSM_TRIG_A2DSPS_SMSM_INT
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700201#define MSM_TRIG_A2WCNSS_SMD_INT
202#define MSM_TRIG_A2WCNSS_SMSM_INT
203#elif defined(CONFIG_ARCH_MSM8X60)
204#define MSM_TRIG_A2M_SMD_INT \
205 (smd_write_intr(1 << 3, MSM_GCC_BASE + 0x8))
206#define MSM_TRIG_A2Q6_SMD_INT \
207 (smd_write_intr(1 << 15, MSM_GCC_BASE + 0x8))
208#define MSM_TRIG_A2M_SMSM_INT \
209 (smd_write_intr(1 << 4, MSM_GCC_BASE + 0x8))
210#define MSM_TRIG_A2Q6_SMSM_INT \
211 (smd_write_intr(1 << 14, MSM_GCC_BASE + 0x8))
212#define MSM_TRIG_A2DSPS_SMD_INT \
213 (smd_write_intr(1, MSM_SIC_NON_SECURE_BASE + 0x4080))
Jeff Hugo6a8057c2011-08-16 13:47:12 -0600214#define MSM_TRIG_A2DSPS_SMSM_INT
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700215#define MSM_TRIG_A2WCNSS_SMD_INT
216#define MSM_TRIG_A2WCNSS_SMSM_INT
Stepan Moskovchenko0dd0eba2011-10-20 18:20:59 -0700217#elif defined(CONFIG_ARCH_MSM8960) || defined(CONFIG_ARCH_MSM8930) || \
218 defined(CONFIG_ARCH_APQ8064)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700219#define MSM_TRIG_A2M_SMD_INT \
220 (smd_write_intr(1 << 3, MSM_APCS_GCC_BASE + 0x8))
221#define MSM_TRIG_A2Q6_SMD_INT \
222 (smd_write_intr(1 << 15, MSM_APCS_GCC_BASE + 0x8))
223#define MSM_TRIG_A2M_SMSM_INT \
224 (smd_write_intr(1 << 4, MSM_APCS_GCC_BASE + 0x8))
225#define MSM_TRIG_A2Q6_SMSM_INT \
226 (smd_write_intr(1 << 14, MSM_APCS_GCC_BASE + 0x8))
227#define MSM_TRIG_A2DSPS_SMD_INT \
228 (smd_write_intr(1, MSM_SIC_NON_SECURE_BASE + 0x4080))
Jeff Hugo6a8057c2011-08-16 13:47:12 -0600229#define MSM_TRIG_A2DSPS_SMSM_INT \
230 (smd_write_intr(1, MSM_SIC_NON_SECURE_BASE + 0x4094))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700231#define MSM_TRIG_A2WCNSS_SMD_INT \
232 (smd_write_intr(1 << 25, MSM_APCS_GCC_BASE + 0x8))
233#define MSM_TRIG_A2WCNSS_SMSM_INT \
234 (smd_write_intr(1 << 23, MSM_APCS_GCC_BASE + 0x8))
Jeff Hugo56b933a2011-09-28 14:42:05 -0600235#elif defined(CONFIG_ARCH_MSM9615)
236#define MSM_TRIG_A2M_SMD_INT \
237 (smd_write_intr(1 << 3, MSM_APCS_GCC_BASE + 0x8))
238#define MSM_TRIG_A2Q6_SMD_INT \
239 (smd_write_intr(1 << 15, MSM_APCS_GCC_BASE + 0x8))
240#define MSM_TRIG_A2M_SMSM_INT \
241 (smd_write_intr(1 << 4, MSM_APCS_GCC_BASE + 0x8))
242#define MSM_TRIG_A2Q6_SMSM_INT \
243 (smd_write_intr(1 << 14, MSM_APCS_GCC_BASE + 0x8))
244#define MSM_TRIG_A2DSPS_SMD_INT
245#define MSM_TRIG_A2DSPS_SMSM_INT
246#define MSM_TRIG_A2WCNSS_SMD_INT
247#define MSM_TRIG_A2WCNSS_SMSM_INT
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700248#elif defined(CONFIG_ARCH_FSM9XXX)
249#define MSM_TRIG_A2Q6_SMD_INT \
250 (smd_write_intr(1 << 10, MSM_GCC_BASE + 0x8))
251#define MSM_TRIG_A2Q6_SMSM_INT \
252 (smd_write_intr(1 << 10, MSM_GCC_BASE + 0x8))
253#define MSM_TRIG_A2M_SMD_INT \
254 (smd_write_intr(1 << 0, MSM_GCC_BASE + 0x8))
255#define MSM_TRIG_A2M_SMSM_INT \
256 (smd_write_intr(1 << 5, MSM_GCC_BASE + 0x8))
257#define MSM_TRIG_A2DSPS_SMD_INT
Jeff Hugo6a8057c2011-08-16 13:47:12 -0600258#define MSM_TRIG_A2DSPS_SMSM_INT
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700259#define MSM_TRIG_A2WCNSS_SMD_INT
260#define MSM_TRIG_A2WCNSS_SMSM_INT
261#else
262#define MSM_TRIG_A2M_SMD_INT \
263 (smd_write_intr(1, MSM_CSR_BASE + 0x400 + (0) * 4))
264#define MSM_TRIG_A2Q6_SMD_INT \
265 (smd_write_intr(1, MSM_CSR_BASE + 0x400 + (8) * 4))
266#define MSM_TRIG_A2M_SMSM_INT \
267 (smd_write_intr(1, MSM_CSR_BASE + 0x400 + (5) * 4))
268#define MSM_TRIG_A2Q6_SMSM_INT \
269 (smd_write_intr(1, MSM_CSR_BASE + 0x400 + (8) * 4))
270#define MSM_TRIG_A2DSPS_SMD_INT
Jeff Hugo6a8057c2011-08-16 13:47:12 -0600271#define MSM_TRIG_A2DSPS_SMSM_INT
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700272#define MSM_TRIG_A2WCNSS_SMD_INT
273#define MSM_TRIG_A2WCNSS_SMSM_INT
Brian Swetland37521a32009-07-01 18:30:47 -0700274#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700275
276#define SMD_LOOPBACK_CID 100
277
Eric Holmbergf6d7d1a2011-09-23 18:31:04 -0600278#define SMEM_SPINLOCK_SMEM_ALLOC "S:3"
279static remote_spinlock_t remote_spinlock;
280
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700281static LIST_HEAD(smd_ch_list_loopback);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700282static void smd_fake_irq_handler(unsigned long arg);
Eric Holmbergc7e8daf2011-12-28 11:49:21 -0700283static void smsm_cb_snapshot(void);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700284
285static void notify_smsm_cb_clients_worker(struct work_struct *work);
286static DECLARE_WORK(smsm_cb_work, notify_smsm_cb_clients_worker);
Eric Holmbergc8002902011-09-16 13:55:57 -0600287static DEFINE_MUTEX(smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700288static struct smsm_state_info *smsm_states;
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +0530289static int spinlocks_initialized;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700290
291static inline void smd_write_intr(unsigned int val,
292 const void __iomem *addr)
293{
294 wmb();
295 __raw_writel(val, addr);
296}
297
298#ifdef CONFIG_WCNSS
299static inline void wakeup_v1_riva(void)
300{
301 /*
302 * workaround hack for RIVA v1 hardware bug
303 * trigger GPIO 40 to wake up RIVA from power collaspe
304 * not to be sent to customers
305 */
Jeff Hugoafb8c4a2011-10-27 15:57:27 -0600306 if (SOCINFO_VERSION_MAJOR(socinfo_get_version()) == 1) {
307 __raw_writel(0x0, MSM_TLMM_BASE + 0x1284);
308 __raw_writel(0x2, MSM_TLMM_BASE + 0x1284);
309 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700310 /* end workaround */
311}
312#else
313static inline void wakeup_v1_riva(void) {}
314#endif
315
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530316static inline void notify_modem_smd(void)
317{
318 static const struct interrupt_config_item *intr
319 = &private_intr_config[SMD_MODEM].smd;
320 if (intr->out_base)
321 smd_write_intr(intr->out_bit_pos,
322 intr->out_base + intr->out_offset);
323 else
324 MSM_TRIG_A2M_SMD_INT;
325}
326
327static inline void notify_dsp_smd(void)
328{
329 static const struct interrupt_config_item *intr
330 = &private_intr_config[SMD_Q6].smd;
331 if (intr->out_base)
332 smd_write_intr(intr->out_bit_pos,
333 intr->out_base + intr->out_offset);
334 else
335 MSM_TRIG_A2Q6_SMD_INT;
336}
337
338static inline void notify_dsps_smd(void)
339{
340 static const struct interrupt_config_item *intr
341 = &private_intr_config[SMD_DSPS].smd;
342 if (intr->out_base)
343 smd_write_intr(intr->out_bit_pos,
344 intr->out_base + intr->out_offset);
345 else
346 MSM_TRIG_A2DSPS_SMD_INT;
347}
348
349static inline void notify_wcnss_smd(void)
350{
351 static const struct interrupt_config_item *intr
352 = &private_intr_config[SMD_WCNSS].smd;
353 wakeup_v1_riva();
354
355 if (intr->out_base)
356 smd_write_intr(intr->out_bit_pos,
357 intr->out_base + intr->out_offset);
358 else
359 MSM_TRIG_A2WCNSS_SMD_INT;
360}
361
362static inline void notify_modem_smsm(void)
363{
364 static const struct interrupt_config_item *intr
365 = &private_intr_config[SMD_MODEM].smsm;
366 if (intr->out_base)
367 smd_write_intr(intr->out_bit_pos,
368 intr->out_base + intr->out_offset);
369 else
370 MSM_TRIG_A2M_SMSM_INT;
371}
372
373static inline void notify_dsp_smsm(void)
374{
375 static const struct interrupt_config_item *intr
376 = &private_intr_config[SMD_Q6].smsm;
377 if (intr->out_base)
378 smd_write_intr(intr->out_bit_pos,
379 intr->out_base + intr->out_offset);
380 else
381 MSM_TRIG_A2Q6_SMSM_INT;
382}
383
384static inline void notify_dsps_smsm(void)
385{
386 static const struct interrupt_config_item *intr
387 = &private_intr_config[SMD_DSPS].smsm;
388 if (intr->out_base)
389 smd_write_intr(intr->out_bit_pos,
390 intr->out_base + intr->out_offset);
391 else
392 MSM_TRIG_A2DSPS_SMSM_INT;
393}
394
395static inline void notify_wcnss_smsm(void)
396{
397 static const struct interrupt_config_item *intr
398 = &private_intr_config[SMD_WCNSS].smsm;
399 wakeup_v1_riva();
400
401 if (intr->out_base)
402 smd_write_intr(intr->out_bit_pos,
403 intr->out_base + intr->out_offset);
404 else
405 MSM_TRIG_A2WCNSS_SMSM_INT;
406}
407
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700408static void notify_other_smsm(uint32_t smsm_entry, uint32_t notify_mask)
409{
410 /* older protocol don't use smsm_intr_mask,
411 but still communicates with modem */
412 if (!smsm_info.intr_mask ||
413 (__raw_readl(SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_MODEM))
414 & notify_mask))
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530415 notify_modem_smsm();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700416
417 if (smsm_info.intr_mask &&
418 (__raw_readl(SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_Q6))
419 & notify_mask)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700420 uint32_t mux_val;
421
Eric Holmberg6282c5d2011-10-27 17:30:57 -0600422 if (cpu_is_qsd8x50() && smsm_info.intr_mux) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700423 mux_val = __raw_readl(
424 SMSM_INTR_MUX_ADDR(SMEM_APPS_Q6_SMSM));
425 mux_val++;
426 __raw_writel(mux_val,
427 SMSM_INTR_MUX_ADDR(SMEM_APPS_Q6_SMSM));
428 }
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530429 notify_dsp_smsm();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700430 }
431
432 if (smsm_info.intr_mask &&
433 (__raw_readl(SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_WCNSS))
434 & notify_mask)) {
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530435 notify_wcnss_smsm();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700436 }
437
Jeff Hugo6a8057c2011-08-16 13:47:12 -0600438 if (smsm_info.intr_mask &&
439 (__raw_readl(SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_DSPS))
440 & notify_mask)) {
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530441 notify_dsps_smsm();
Jeff Hugo6a8057c2011-08-16 13:47:12 -0600442 }
443
Eric Holmbergc7e8daf2011-12-28 11:49:21 -0700444 smsm_cb_snapshot();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700445}
446
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700447void smd_diag(void)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700448{
449 char *x;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700450 int size;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700451
452 x = smem_find(ID_DIAG_ERR_MSG, SZ_DIAG_ERR_MSG);
453 if (x != 0) {
454 x[SZ_DIAG_ERR_MSG - 1] = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700455 SMD_INFO("smem: DIAG '%s'\n", x);
456 }
457
458 x = smem_get_entry(SMEM_ERR_CRASH_LOG, &size);
459 if (x != 0) {
460 x[size - 1] = 0;
461 pr_err("smem: CRASH LOG\n'%s'\n", x);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700462 }
463}
464
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700465
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700466static void handle_modem_crash(void)
467{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700468 pr_err("MODEM/AMSS has CRASHED\n");
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700469 smd_diag();
470
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700471 /* hard reboot if possible FIXME
472 if (msm_reset_hook)
473 msm_reset_hook();
474 */
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700475
476 /* in this case the modem or watchdog should reboot us */
477 for (;;)
478 ;
479}
480
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700481int smsm_check_for_modem_crash(void)
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700482{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700483 /* if the modem's not ready yet, we have to hope for the best */
484 if (!smsm_info.state)
485 return 0;
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700486
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700487 if (__raw_readl(SMSM_STATE_ADDR(SMSM_MODEM_STATE)) & SMSM_RESET) {
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700488 handle_modem_crash();
489 return -1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700490 }
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700491 return 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700492}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700493EXPORT_SYMBOL(smsm_check_for_modem_crash);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700494
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700495/* the spinlock is used to synchronize between the
Brian Swetland03e00cd2009-07-01 17:58:37 -0700496 * irq handler and code that mutates the channel
497 * list or fiddles with channel state
498 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700499static DEFINE_SPINLOCK(smd_lock);
Brian Swetland03e00cd2009-07-01 17:58:37 -0700500DEFINE_SPINLOCK(smem_lock);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700501
502/* the mutex is used during open() and close()
Brian Swetland03e00cd2009-07-01 17:58:37 -0700503 * operations to avoid races while creating or
504 * destroying smd_channel structures
505 */
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700506static DEFINE_MUTEX(smd_creation_mutex);
507
508static int smd_initialized;
509
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700510struct smd_shared_v1 {
511 struct smd_half_channel ch0;
512 unsigned char data0[SMD_BUF_SIZE];
513 struct smd_half_channel ch1;
514 unsigned char data1[SMD_BUF_SIZE];
515};
516
517struct smd_shared_v2 {
518 struct smd_half_channel ch0;
519 struct smd_half_channel ch1;
520};
521
522struct smd_channel {
523 volatile struct smd_half_channel *send;
524 volatile struct smd_half_channel *recv;
525 unsigned char *send_data;
526 unsigned char *recv_data;
527 unsigned fifo_size;
528 unsigned fifo_mask;
529 struct list_head ch_list;
530
531 unsigned current_packet;
532 unsigned n;
533 void *priv;
534 void (*notify)(void *priv, unsigned flags);
535
536 int (*read)(smd_channel_t *ch, void *data, int len, int user_buf);
537 int (*write)(smd_channel_t *ch, const void *data, int len,
538 int user_buf);
539 int (*read_avail)(smd_channel_t *ch);
540 int (*write_avail)(smd_channel_t *ch);
541 int (*read_from_cb)(smd_channel_t *ch, void *data, int len,
542 int user_buf);
543
544 void (*update_state)(smd_channel_t *ch);
545 unsigned last_state;
546 void (*notify_other_cpu)(void);
547
548 char name[20];
549 struct platform_device pdev;
550 unsigned type;
551
552 int pending_pkt_sz;
553
554 char is_pkt_ch;
555};
556
557struct edge_to_pid {
558 uint32_t local_pid;
559 uint32_t remote_pid;
560};
561
562/**
563 * Maps edge type to local and remote processor ID's.
564 */
565static struct edge_to_pid edge_to_pids[] = {
566 [SMD_APPS_MODEM] = {SMSM_APPS, SMSM_MODEM},
567 [SMD_APPS_QDSP] = {SMSM_APPS, SMSM_Q6},
568 [SMD_MODEM_QDSP] = {SMSM_MODEM, SMSM_Q6},
569 [SMD_APPS_DSPS] = {SMSM_APPS, SMSM_DSPS},
570 [SMD_MODEM_DSPS] = {SMSM_MODEM, SMSM_DSPS},
571 [SMD_QDSP_DSPS] = {SMSM_Q6, SMSM_DSPS},
572 [SMD_APPS_WCNSS] = {SMSM_APPS, SMSM_WCNSS},
573 [SMD_MODEM_WCNSS] = {SMSM_MODEM, SMSM_WCNSS},
574 [SMD_QDSP_WCNSS] = {SMSM_Q6, SMSM_WCNSS},
575 [SMD_DSPS_WCNSS] = {SMSM_DSPS, SMSM_WCNSS},
Eric Holmberg7dfd1972011-09-09 16:07:57 -0600576 [SMD_APPS_Q6FW] = {SMSM_APPS, SMD_MODEM_Q6_FW},
577 [SMD_MODEM_Q6FW] = {SMSM_MODEM, SMD_MODEM_Q6_FW},
578 [SMD_QDSP_Q6FW] = {SMSM_Q6, SMD_MODEM_Q6_FW},
579 [SMD_DSPS_Q6FW] = {SMSM_DSPS, SMD_MODEM_Q6_FW},
580 [SMD_WCNSS_Q6FW] = {SMSM_WCNSS, SMD_MODEM_Q6_FW},
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700581};
582
583struct restart_notifier_block {
584 unsigned processor;
585 char *name;
586 struct notifier_block nb;
587};
588
589static struct platform_device loopback_tty_pdev = {.name = "LOOPBACK_TTY"};
590
591static LIST_HEAD(smd_ch_closed_list);
592static LIST_HEAD(smd_ch_closing_list);
593static LIST_HEAD(smd_ch_to_close_list);
594static LIST_HEAD(smd_ch_list_modem);
595static LIST_HEAD(smd_ch_list_dsp);
596static LIST_HEAD(smd_ch_list_dsps);
597static LIST_HEAD(smd_ch_list_wcnss);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700598
599static unsigned char smd_ch_allocated[64];
600static struct work_struct probe_work;
601
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700602static void finalize_channel_close_fn(struct work_struct *work);
603static DECLARE_WORK(finalize_channel_close_work, finalize_channel_close_fn);
604static struct workqueue_struct *channel_close_wq;
605
606static int smd_alloc_channel(struct smd_alloc_elm *alloc_elm);
607
608/* on smp systems, the probe might get called from multiple cores,
609 hence use a lock */
610static DEFINE_MUTEX(smd_probe_lock);
611
612static void smd_channel_probe_worker(struct work_struct *work)
613{
614 struct smd_alloc_elm *shared;
615 unsigned n;
616 uint32_t type;
617
618 shared = smem_find(ID_CH_ALLOC_TBL, sizeof(*shared) * 64);
619
620 if (!shared) {
621 pr_err("%s: allocation table not initialized\n", __func__);
622 return;
623 }
624
625 mutex_lock(&smd_probe_lock);
626 for (n = 0; n < 64; n++) {
627 if (smd_ch_allocated[n])
628 continue;
629
630 /* channel should be allocated only if APPS
631 processor is involved */
632 type = SMD_CHANNEL_TYPE(shared[n].type);
633 if ((type != SMD_APPS_MODEM) && (type != SMD_APPS_QDSP) &&
634 (type != SMD_APPS_DSPS) && (type != SMD_APPS_WCNSS))
635 continue;
636 if (!shared[n].ref_count)
637 continue;
638 if (!shared[n].name[0])
639 continue;
640
641 if (!smd_alloc_channel(&shared[n]))
642 smd_ch_allocated[n] = 1;
643 else
644 SMD_INFO("Probe skipping ch %d, not allocated\n", n);
645 }
646 mutex_unlock(&smd_probe_lock);
647}
648
649/**
650 * Lookup processor ID and determine if it belongs to the proved edge
651 * type.
652 *
653 * @shared2: Pointer to v2 shared channel structure
654 * @type: Edge type
655 * @pid: Processor ID of processor on edge
656 * @local_ch: Channel that belongs to processor @pid
657 * @remote_ch: Other side of edge contained @pid
658 *
659 * Returns 0 for not on edge, 1 for found on edge
660 */
661static int pid_is_on_edge(struct smd_shared_v2 *shared2,
662 uint32_t type, uint32_t pid,
663 struct smd_half_channel **local_ch,
664 struct smd_half_channel **remote_ch
665 )
666{
667 int ret = 0;
668 struct edge_to_pid *edge;
669
670 *local_ch = 0;
671 *remote_ch = 0;
672
673 if (!shared2 || (type >= ARRAY_SIZE(edge_to_pids)))
674 return 0;
675
676 edge = &edge_to_pids[type];
677 if (edge->local_pid != edge->remote_pid) {
678 if (pid == edge->local_pid) {
679 *local_ch = &shared2->ch0;
680 *remote_ch = &shared2->ch1;
681 ret = 1;
682 } else if (pid == edge->remote_pid) {
683 *local_ch = &shared2->ch1;
684 *remote_ch = &shared2->ch0;
685 ret = 1;
686 }
687 }
688
689 return ret;
690}
691
Eric Holmberg2a563c32011-10-05 14:51:43 -0600692static void smd_reset_edge(struct smd_half_channel *ch, unsigned new_state)
693{
694 if (ch->state != SMD_SS_CLOSED) {
695 ch->state = new_state;
696 ch->fDSR = 0;
697 ch->fCTS = 0;
698 ch->fCD = 0;
699 ch->fSTATE = 1;
700 }
701}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700702
703static void smd_channel_reset_state(struct smd_alloc_elm *shared,
704 unsigned new_state, unsigned pid)
705{
706 unsigned n;
707 struct smd_shared_v2 *shared2;
708 uint32_t type;
709 struct smd_half_channel *local_ch;
710 struct smd_half_channel *remote_ch;
711
712 for (n = 0; n < SMD_CHANNELS; n++) {
713 if (!shared[n].ref_count)
714 continue;
715 if (!shared[n].name[0])
716 continue;
717
718 type = SMD_CHANNEL_TYPE(shared[n].type);
719 shared2 = smem_alloc(SMEM_SMD_BASE_ID + n, sizeof(*shared2));
720 if (!shared2)
721 continue;
722
Eric Holmberg2a563c32011-10-05 14:51:43 -0600723 if (pid_is_on_edge(shared2, type, pid, &local_ch, &remote_ch))
724 smd_reset_edge(local_ch, new_state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700725
Eric Holmberg2a563c32011-10-05 14:51:43 -0600726 /*
727 * ModemFW is in the same subsystem as ModemSW, but has
728 * separate SMD edges that need to be reset.
729 */
730 if (pid == SMSM_MODEM &&
731 pid_is_on_edge(shared2, type, SMD_MODEM_Q6_FW,
732 &local_ch, &remote_ch))
733 smd_reset_edge(local_ch, new_state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700734 }
735}
736
737
738void smd_channel_reset(uint32_t restart_pid)
739{
740 struct smd_alloc_elm *shared;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700741 unsigned long flags;
742
743 SMD_DBG("%s: starting reset\n", __func__);
744 shared = smem_find(ID_CH_ALLOC_TBL, sizeof(*shared) * 64);
745 if (!shared) {
746 pr_err("%s: allocation table not initialized\n", __func__);
747 return;
748 }
749
Eric Holmbergf6d7d1a2011-09-23 18:31:04 -0600750 /* release any held spinlocks */
751 remote_spin_release(&remote_spinlock, restart_pid);
752 remote_spin_release_all(restart_pid);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700753
754 /* reset SMSM entry */
755 if (smsm_info.state) {
756 writel_relaxed(0, SMSM_STATE_ADDR(restart_pid));
757
Eric Holmberg351a63c2011-12-02 17:49:43 -0700758 /* restart SMSM init handshake */
759 if (restart_pid == SMSM_MODEM) {
760 smsm_change_state(SMSM_APPS_STATE,
Eric Holmberg6b2f80e2012-01-09 12:22:52 -0700761 SMSM_INIT | SMSM_SMD_LOOPBACK | SMSM_RESET,
762 0);
Eric Holmberg351a63c2011-12-02 17:49:43 -0700763 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700764
765 /* notify SMSM processors */
766 smsm_irq_handler(0, 0);
767 MSM_TRIG_A2M_SMSM_INT;
768 MSM_TRIG_A2Q6_SMSM_INT;
Jeff Hugo6a8057c2011-08-16 13:47:12 -0600769 MSM_TRIG_A2DSPS_SMSM_INT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700770 }
771
772 /* change all remote states to CLOSING */
773 mutex_lock(&smd_probe_lock);
774 spin_lock_irqsave(&smd_lock, flags);
775 smd_channel_reset_state(shared, SMD_SS_CLOSING, restart_pid);
776 spin_unlock_irqrestore(&smd_lock, flags);
777 mutex_unlock(&smd_probe_lock);
778
779 /* notify SMD processors */
780 mb();
781 smd_fake_irq_handler(0);
782 notify_modem_smd();
783 notify_dsp_smd();
784 notify_dsps_smd();
785 notify_wcnss_smd();
786
787 /* change all remote states to CLOSED */
788 mutex_lock(&smd_probe_lock);
789 spin_lock_irqsave(&smd_lock, flags);
790 smd_channel_reset_state(shared, SMD_SS_CLOSED, restart_pid);
791 spin_unlock_irqrestore(&smd_lock, flags);
792 mutex_unlock(&smd_probe_lock);
793
794 /* notify SMD processors */
795 mb();
796 smd_fake_irq_handler(0);
797 notify_modem_smd();
798 notify_dsp_smd();
799 notify_dsps_smd();
800 notify_wcnss_smd();
801
802 SMD_DBG("%s: finished reset\n", __func__);
803}
804
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700805/* how many bytes are available for reading */
806static int smd_stream_read_avail(struct smd_channel *ch)
807{
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700808 return (ch->recv->head - ch->recv->tail) & ch->fifo_mask;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700809}
810
811/* how many bytes we are free to write */
812static int smd_stream_write_avail(struct smd_channel *ch)
813{
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700814 return ch->fifo_mask -
815 ((ch->send->head - ch->send->tail) & ch->fifo_mask);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700816}
817
818static int smd_packet_read_avail(struct smd_channel *ch)
819{
820 if (ch->current_packet) {
821 int n = smd_stream_read_avail(ch);
822 if (n > ch->current_packet)
823 n = ch->current_packet;
824 return n;
825 } else {
826 return 0;
827 }
828}
829
830static int smd_packet_write_avail(struct smd_channel *ch)
831{
832 int n = smd_stream_write_avail(ch);
833 return n > SMD_HEADER_SIZE ? n - SMD_HEADER_SIZE : 0;
834}
835
836static int ch_is_open(struct smd_channel *ch)
837{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700838 return (ch->recv->state == SMD_SS_OPENED ||
839 ch->recv->state == SMD_SS_FLUSHING)
840 && (ch->send->state == SMD_SS_OPENED);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700841}
842
843/* provide a pointer and length to readable data in the fifo */
844static unsigned ch_read_buffer(struct smd_channel *ch, void **ptr)
845{
846 unsigned head = ch->recv->head;
847 unsigned tail = ch->recv->tail;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700848 *ptr = (void *) (ch->recv_data + tail);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700849
850 if (tail <= head)
851 return head - tail;
852 else
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700853 return ch->fifo_size - tail;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700854}
855
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700856static int read_intr_blocked(struct smd_channel *ch)
857{
858 return ch->recv->fBLOCKREADINTR;
859}
860
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700861/* advance the fifo read pointer after data from ch_read_buffer is consumed */
862static void ch_read_done(struct smd_channel *ch, unsigned count)
863{
864 BUG_ON(count > smd_stream_read_avail(ch));
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700865 ch->recv->tail = (ch->recv->tail + count) & ch->fifo_mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700866 wmb();
Haley Teng7632fba2009-10-12 10:38:10 -0700867 ch->send->fTAIL = 1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700868}
869
870/* basic read interface to ch_read_{buffer,done} used
Brian Swetland03e00cd2009-07-01 17:58:37 -0700871 * by smd_*_read() and update_packet_state()
872 * will read-and-discard if the _data pointer is null
873 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700874static int ch_read(struct smd_channel *ch, void *_data, int len, int user_buf)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700875{
876 void *ptr;
877 unsigned n;
878 unsigned char *data = _data;
879 int orig_len = len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700880 int r = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700881
882 while (len > 0) {
883 n = ch_read_buffer(ch, &ptr);
884 if (n == 0)
885 break;
886
887 if (n > len)
888 n = len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700889 if (_data) {
890 if (user_buf) {
891 r = copy_to_user(data, ptr, n);
892 if (r > 0) {
893 pr_err("%s: "
894 "copy_to_user could not copy "
895 "%i bytes.\n",
896 __func__,
897 r);
898 }
899 } else
900 memcpy(data, ptr, n);
901 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700902
903 data += n;
904 len -= n;
905 ch_read_done(ch, n);
906 }
907
908 return orig_len - len;
909}
910
911static void update_stream_state(struct smd_channel *ch)
912{
913 /* streams have no special state requiring updating */
914}
915
916static void update_packet_state(struct smd_channel *ch)
917{
918 unsigned hdr[5];
919 int r;
920
921 /* can't do anything if we're in the middle of a packet */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700922 while (ch->current_packet == 0) {
923 /* discard 0 length packets if any */
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700924
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700925 /* don't bother unless we can get the full header */
926 if (smd_stream_read_avail(ch) < SMD_HEADER_SIZE)
927 return;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700928
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700929 r = ch_read(ch, hdr, SMD_HEADER_SIZE, 0);
930 BUG_ON(r != SMD_HEADER_SIZE);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700931
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700932 ch->current_packet = hdr[0];
933 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700934}
935
936/* provide a pointer and length to next free space in the fifo */
937static unsigned ch_write_buffer(struct smd_channel *ch, void **ptr)
938{
939 unsigned head = ch->send->head;
940 unsigned tail = ch->send->tail;
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700941 *ptr = (void *) (ch->send_data + head);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700942
943 if (head < tail) {
944 return tail - head - 1;
945 } else {
946 if (tail == 0)
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700947 return ch->fifo_size - head - 1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700948 else
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700949 return ch->fifo_size - head;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700950 }
951}
952
953/* advace the fifo write pointer after freespace
954 * from ch_write_buffer is filled
955 */
956static void ch_write_done(struct smd_channel *ch, unsigned count)
957{
958 BUG_ON(count > smd_stream_write_avail(ch));
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700959 ch->send->head = (ch->send->head + count) & ch->fifo_mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700960 wmb();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700961 ch->send->fHEAD = 1;
962}
963
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700964static void ch_set_state(struct smd_channel *ch, unsigned n)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700965{
966 if (n == SMD_SS_OPENED) {
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700967 ch->send->fDSR = 1;
968 ch->send->fCTS = 1;
969 ch->send->fCD = 1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700970 } else {
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700971 ch->send->fDSR = 0;
972 ch->send->fCTS = 0;
973 ch->send->fCD = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700974 }
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700975 ch->send->state = n;
976 ch->send->fSTATE = 1;
977 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700978}
979
980static void do_smd_probe(void)
981{
982 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
983 if (shared->heap_info.free_offset != last_heap_free) {
984 last_heap_free = shared->heap_info.free_offset;
985 schedule_work(&probe_work);
986 }
987}
988
989static void smd_state_change(struct smd_channel *ch,
990 unsigned last, unsigned next)
991{
992 ch->last_state = next;
993
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700994 SMD_INFO("SMD: ch %d %d -> %d\n", ch->n, last, next);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700995
996 switch (next) {
997 case SMD_SS_OPENING:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700998 if (ch->send->state == SMD_SS_CLOSING ||
999 ch->send->state == SMD_SS_CLOSED) {
1000 ch->recv->tail = 0;
1001 ch->send->head = 0;
1002 ch->send->fBLOCKREADINTR = 0;
1003 ch_set_state(ch, SMD_SS_OPENING);
1004 }
1005 break;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001006 case SMD_SS_OPENED:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001007 if (ch->send->state == SMD_SS_OPENING) {
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001008 ch_set_state(ch, SMD_SS_OPENED);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001009 ch->notify(ch->priv, SMD_EVENT_OPEN);
1010 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001011 break;
1012 case SMD_SS_FLUSHING:
1013 case SMD_SS_RESET:
1014 /* we should force them to close? */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001015 break;
1016 case SMD_SS_CLOSED:
1017 if (ch->send->state == SMD_SS_OPENED) {
1018 ch_set_state(ch, SMD_SS_CLOSING);
1019 ch->current_packet = 0;
Eric Holmbergad4fa8d2011-11-11 16:55:13 -07001020 ch->pending_pkt_sz = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001021 ch->notify(ch->priv, SMD_EVENT_CLOSE);
1022 }
1023 break;
1024 case SMD_SS_CLOSING:
1025 if (ch->send->state == SMD_SS_CLOSED) {
1026 list_move(&ch->ch_list,
1027 &smd_ch_to_close_list);
1028 queue_work(channel_close_wq,
1029 &finalize_channel_close_work);
1030 }
1031 break;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001032 }
1033}
1034
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001035static void handle_smd_irq_closing_list(void)
1036{
1037 unsigned long flags;
1038 struct smd_channel *ch;
1039 struct smd_channel *index;
1040 unsigned tmp;
1041
1042 spin_lock_irqsave(&smd_lock, flags);
1043 list_for_each_entry_safe(ch, index, &smd_ch_closing_list, ch_list) {
1044 if (ch->recv->fSTATE)
1045 ch->recv->fSTATE = 0;
1046 tmp = ch->recv->state;
1047 if (tmp != ch->last_state)
1048 smd_state_change(ch, ch->last_state, tmp);
1049 }
1050 spin_unlock_irqrestore(&smd_lock, flags);
1051}
1052
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001053static void handle_smd_irq(struct list_head *list, void (*notify)(void))
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001054{
1055 unsigned long flags;
1056 struct smd_channel *ch;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001057 unsigned ch_flags;
1058 unsigned tmp;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001059 unsigned char state_change;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001060
1061 spin_lock_irqsave(&smd_lock, flags);
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001062 list_for_each_entry(ch, list, ch_list) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001063 state_change = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001064 ch_flags = 0;
1065 if (ch_is_open(ch)) {
1066 if (ch->recv->fHEAD) {
1067 ch->recv->fHEAD = 0;
1068 ch_flags |= 1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001069 }
1070 if (ch->recv->fTAIL) {
1071 ch->recv->fTAIL = 0;
1072 ch_flags |= 2;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001073 }
1074 if (ch->recv->fSTATE) {
1075 ch->recv->fSTATE = 0;
1076 ch_flags |= 4;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001077 }
1078 }
1079 tmp = ch->recv->state;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001080 if (tmp != ch->last_state) {
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001081 smd_state_change(ch, ch->last_state, tmp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001082 state_change = 1;
1083 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001084 if (ch_flags) {
1085 ch->update_state(ch);
1086 ch->notify(ch->priv, SMD_EVENT_DATA);
1087 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001088 if (ch_flags & 0x4 && !state_change)
1089 ch->notify(ch->priv, SMD_EVENT_STATUS);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001090 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001091 spin_unlock_irqrestore(&smd_lock, flags);
1092 do_smd_probe();
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001093}
1094
Brian Swetland37521a32009-07-01 18:30:47 -07001095static irqreturn_t smd_modem_irq_handler(int irq, void *data)
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001096{
Brian Swetland37521a32009-07-01 18:30:47 -07001097 handle_smd_irq(&smd_ch_list_modem, notify_modem_smd);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001098 handle_smd_irq_closing_list();
Brian Swetland37521a32009-07-01 18:30:47 -07001099 return IRQ_HANDLED;
1100}
1101
1102static irqreturn_t smd_dsp_irq_handler(int irq, void *data)
1103{
1104 handle_smd_irq(&smd_ch_list_dsp, notify_dsp_smd);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001105 handle_smd_irq_closing_list();
1106 return IRQ_HANDLED;
1107}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001108
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001109static irqreturn_t smd_dsps_irq_handler(int irq, void *data)
1110{
1111 handle_smd_irq(&smd_ch_list_dsps, notify_dsps_smd);
1112 handle_smd_irq_closing_list();
1113 return IRQ_HANDLED;
1114}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001115
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001116static irqreturn_t smd_wcnss_irq_handler(int irq, void *data)
1117{
1118 handle_smd_irq(&smd_ch_list_wcnss, notify_wcnss_smd);
1119 handle_smd_irq_closing_list();
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001120 return IRQ_HANDLED;
1121}
1122
1123static void smd_fake_irq_handler(unsigned long arg)
1124{
Brian Swetland37521a32009-07-01 18:30:47 -07001125 handle_smd_irq(&smd_ch_list_modem, notify_modem_smd);
1126 handle_smd_irq(&smd_ch_list_dsp, notify_dsp_smd);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001127 handle_smd_irq(&smd_ch_list_dsps, notify_dsps_smd);
1128 handle_smd_irq(&smd_ch_list_wcnss, notify_wcnss_smd);
1129 handle_smd_irq_closing_list();
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001130}
1131
1132static DECLARE_TASKLET(smd_fake_irq_tasklet, smd_fake_irq_handler, 0);
1133
Brian Swetland37521a32009-07-01 18:30:47 -07001134static inline int smd_need_int(struct smd_channel *ch)
1135{
1136 if (ch_is_open(ch)) {
1137 if (ch->recv->fHEAD || ch->recv->fTAIL || ch->recv->fSTATE)
1138 return 1;
1139 if (ch->recv->state != ch->last_state)
1140 return 1;
1141 }
1142 return 0;
1143}
1144
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001145void smd_sleep_exit(void)
1146{
1147 unsigned long flags;
1148 struct smd_channel *ch;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001149 int need_int = 0;
1150
1151 spin_lock_irqsave(&smd_lock, flags);
Brian Swetland37521a32009-07-01 18:30:47 -07001152 list_for_each_entry(ch, &smd_ch_list_modem, ch_list) {
1153 if (smd_need_int(ch)) {
1154 need_int = 1;
1155 break;
1156 }
1157 }
1158 list_for_each_entry(ch, &smd_ch_list_dsp, ch_list) {
1159 if (smd_need_int(ch)) {
1160 need_int = 1;
1161 break;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001162 }
1163 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001164 list_for_each_entry(ch, &smd_ch_list_dsps, ch_list) {
1165 if (smd_need_int(ch)) {
1166 need_int = 1;
1167 break;
1168 }
1169 }
1170 list_for_each_entry(ch, &smd_ch_list_wcnss, ch_list) {
1171 if (smd_need_int(ch)) {
1172 need_int = 1;
1173 break;
1174 }
1175 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001176 spin_unlock_irqrestore(&smd_lock, flags);
1177 do_smd_probe();
Brian Swetland37521a32009-07-01 18:30:47 -07001178
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001179 if (need_int) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001180 SMD_DBG("smd_sleep_exit need interrupt\n");
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001181 tasklet_schedule(&smd_fake_irq_tasklet);
1182 }
1183}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001184EXPORT_SYMBOL(smd_sleep_exit);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001185
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001186static int smd_is_packet(struct smd_alloc_elm *alloc_elm)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001187{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001188 if (SMD_XFER_TYPE(alloc_elm->type) == 1)
1189 return 0;
1190 else if (SMD_XFER_TYPE(alloc_elm->type) == 2)
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001191 return 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001192
1193 /* for cases where xfer type is 0 */
1194 if (!strncmp(alloc_elm->name, "DAL", 3))
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001195 return 0;
1196
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001197 /* for cases where xfer type is 0 */
1198 if (!strncmp(alloc_elm->name, "RPCCALL_QDSP", 12))
1199 return 0;
1200
1201 if (alloc_elm->cid > 4 || alloc_elm->cid == 1)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001202 return 1;
1203 else
1204 return 0;
1205}
1206
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001207static int smd_stream_write(smd_channel_t *ch, const void *_data, int len,
1208 int user_buf)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001209{
1210 void *ptr;
1211 const unsigned char *buf = _data;
1212 unsigned xfer;
1213 int orig_len = len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001214 int r = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001215
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001216 SMD_DBG("smd_stream_write() %d -> ch%d\n", len, ch->n);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001217 if (len < 0)
1218 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001219 else if (len == 0)
1220 return 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001221
1222 while ((xfer = ch_write_buffer(ch, &ptr)) != 0) {
Eric Holmberg7a717872012-02-03 11:58:04 -07001223 if (!ch_is_open(ch)) {
1224 len = orig_len;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001225 break;
Eric Holmberg7a717872012-02-03 11:58:04 -07001226 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001227 if (xfer > len)
1228 xfer = len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001229 if (user_buf) {
1230 r = copy_from_user(ptr, buf, xfer);
1231 if (r > 0) {
1232 pr_err("%s: "
1233 "copy_from_user could not copy %i "
1234 "bytes.\n",
1235 __func__,
1236 r);
1237 }
1238 } else
1239 memcpy(ptr, buf, xfer);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001240 ch_write_done(ch, xfer);
1241 len -= xfer;
1242 buf += xfer;
1243 if (len == 0)
1244 break;
1245 }
1246
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001247 if (orig_len - len)
1248 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001249
1250 return orig_len - len;
1251}
1252
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001253static int smd_packet_write(smd_channel_t *ch, const void *_data, int len,
1254 int user_buf)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001255{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001256 int ret;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001257 unsigned hdr[5];
1258
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001259 SMD_DBG("smd_packet_write() %d -> ch%d\n", len, ch->n);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001260 if (len < 0)
1261 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001262 else if (len == 0)
1263 return 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001264
1265 if (smd_stream_write_avail(ch) < (len + SMD_HEADER_SIZE))
1266 return -ENOMEM;
1267
1268 hdr[0] = len;
1269 hdr[1] = hdr[2] = hdr[3] = hdr[4] = 0;
1270
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001271
1272 ret = smd_stream_write(ch, hdr, sizeof(hdr), 0);
1273 if (ret < 0 || ret != sizeof(hdr)) {
1274 SMD_DBG("%s failed to write pkt header: "
1275 "%d returned\n", __func__, ret);
1276 return -1;
1277 }
1278
1279
1280 ret = smd_stream_write(ch, _data, len, user_buf);
1281 if (ret < 0 || ret != len) {
1282 SMD_DBG("%s failed to write pkt data: "
1283 "%d returned\n", __func__, ret);
1284 return ret;
1285 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001286
1287 return len;
1288}
1289
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001290static int smd_stream_read(smd_channel_t *ch, void *data, int len, int user_buf)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001291{
1292 int r;
1293
1294 if (len < 0)
1295 return -EINVAL;
1296
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001297 r = ch_read(ch, data, len, user_buf);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001298 if (r > 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001299 if (!read_intr_blocked(ch))
1300 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001301
1302 return r;
1303}
1304
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001305static int smd_packet_read(smd_channel_t *ch, void *data, int len, int user_buf)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001306{
1307 unsigned long flags;
1308 int r;
1309
1310 if (len < 0)
1311 return -EINVAL;
1312
1313 if (len > ch->current_packet)
1314 len = ch->current_packet;
1315
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001316 r = ch_read(ch, data, len, user_buf);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001317 if (r > 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001318 if (!read_intr_blocked(ch))
1319 ch->notify_other_cpu();
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001320
1321 spin_lock_irqsave(&smd_lock, flags);
1322 ch->current_packet -= r;
1323 update_packet_state(ch);
1324 spin_unlock_irqrestore(&smd_lock, flags);
1325
1326 return r;
1327}
1328
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001329static int smd_packet_read_from_cb(smd_channel_t *ch, void *data, int len,
1330 int user_buf)
1331{
1332 int r;
1333
1334 if (len < 0)
1335 return -EINVAL;
1336
1337 if (len > ch->current_packet)
1338 len = ch->current_packet;
1339
1340 r = ch_read(ch, data, len, user_buf);
1341 if (r > 0)
1342 if (!read_intr_blocked(ch))
1343 ch->notify_other_cpu();
1344
1345 ch->current_packet -= r;
1346 update_packet_state(ch);
1347
1348 return r;
1349}
1350
Angshuman Sarkarac7d6252011-09-30 18:20:59 +05301351#if (defined(CONFIG_MSM_SMD_PKG4) || defined(CONFIG_MSM_SMD_PKG3))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001352static int smd_alloc_v2(struct smd_channel *ch)
1353{
1354 struct smd_shared_v2 *shared2;
1355 void *buffer;
1356 unsigned buffer_sz;
1357
1358 shared2 = smem_alloc(SMEM_SMD_BASE_ID + ch->n, sizeof(*shared2));
1359 if (!shared2) {
1360 SMD_INFO("smem_alloc failed ch=%d\n", ch->n);
Angshuman Sarkarac7d6252011-09-30 18:20:59 +05301361 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001362 }
1363 buffer = smem_get_entry(SMEM_SMD_FIFO_BASE_ID + ch->n, &buffer_sz);
1364 if (!buffer) {
Angshuman Sarkarac7d6252011-09-30 18:20:59 +05301365 SMD_INFO("smem_get_entry failed\n");
1366 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001367 }
1368
1369 /* buffer must be a power-of-two size */
Angshuman Sarkarac7d6252011-09-30 18:20:59 +05301370 if (buffer_sz & (buffer_sz - 1)) {
1371 SMD_INFO("Buffer size: %u not power of two\n", buffer_sz);
1372 return -EINVAL;
1373 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001374 buffer_sz /= 2;
1375 ch->send = &shared2->ch0;
1376 ch->recv = &shared2->ch1;
1377 ch->send_data = buffer;
1378 ch->recv_data = buffer + buffer_sz;
1379 ch->fifo_size = buffer_sz;
1380 return 0;
1381}
1382
1383static int smd_alloc_v1(struct smd_channel *ch)
1384{
Angshuman Sarkarac7d6252011-09-30 18:20:59 +05301385 return -EINVAL;
1386}
1387
1388#else /* define v1 for older targets */
1389static int smd_alloc_v2(struct smd_channel *ch)
1390{
1391 return -EINVAL;
1392}
1393
1394static int smd_alloc_v1(struct smd_channel *ch)
1395{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001396 struct smd_shared_v1 *shared1;
1397 shared1 = smem_alloc(ID_SMD_CHANNELS + ch->n, sizeof(*shared1));
1398 if (!shared1) {
1399 pr_err("smd_alloc_channel() cid %d does not exist\n", ch->n);
Angshuman Sarkarac7d6252011-09-30 18:20:59 +05301400 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001401 }
1402 ch->send = &shared1->ch0;
1403 ch->recv = &shared1->ch1;
1404 ch->send_data = shared1->data0;
1405 ch->recv_data = shared1->data1;
1406 ch->fifo_size = SMD_BUF_SIZE;
1407 return 0;
1408}
1409
Angshuman Sarkarac7d6252011-09-30 18:20:59 +05301410#endif
1411
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001412static int smd_alloc_channel(struct smd_alloc_elm *alloc_elm)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001413{
1414 struct smd_channel *ch;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001415
1416 ch = kzalloc(sizeof(struct smd_channel), GFP_KERNEL);
1417 if (ch == 0) {
1418 pr_err("smd_alloc_channel() out of memory\n");
Brian Swetland34f719b2009-10-30 16:22:05 -07001419 return -1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001420 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001421 ch->n = alloc_elm->cid;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001422
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001423 if (smd_alloc_v2(ch) && smd_alloc_v1(ch)) {
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001424 kfree(ch);
Brian Swetland34f719b2009-10-30 16:22:05 -07001425 return -1;
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001426 }
1427
1428 ch->fifo_mask = ch->fifo_size - 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001429 ch->type = SMD_CHANNEL_TYPE(alloc_elm->type);
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001430
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001431 if (ch->type == SMD_APPS_MODEM)
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001432 ch->notify_other_cpu = notify_modem_smd;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001433 else if (ch->type == SMD_APPS_QDSP)
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001434 ch->notify_other_cpu = notify_dsp_smd;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001435 else if (ch->type == SMD_APPS_DSPS)
1436 ch->notify_other_cpu = notify_dsps_smd;
1437 else
1438 ch->notify_other_cpu = notify_wcnss_smd;
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001439
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001440 if (smd_is_packet(alloc_elm)) {
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001441 ch->read = smd_packet_read;
1442 ch->write = smd_packet_write;
1443 ch->read_avail = smd_packet_read_avail;
1444 ch->write_avail = smd_packet_write_avail;
1445 ch->update_state = update_packet_state;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001446 ch->read_from_cb = smd_packet_read_from_cb;
1447 ch->is_pkt_ch = 1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001448 } else {
1449 ch->read = smd_stream_read;
1450 ch->write = smd_stream_write;
1451 ch->read_avail = smd_stream_read_avail;
1452 ch->write_avail = smd_stream_write_avail;
1453 ch->update_state = update_stream_state;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001454 ch->read_from_cb = smd_stream_read;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001455 }
1456
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001457 memcpy(ch->name, alloc_elm->name, SMD_MAX_CH_NAME_LEN);
1458 ch->name[SMD_MAX_CH_NAME_LEN-1] = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001459
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001460 ch->pdev.name = ch->name;
1461 ch->pdev.id = ch->type;
1462
1463 SMD_INFO("smd_alloc_channel() '%s' cid=%d\n",
1464 ch->name, ch->n);
1465
1466 mutex_lock(&smd_creation_mutex);
1467 list_add(&ch->ch_list, &smd_ch_closed_list);
1468 mutex_unlock(&smd_creation_mutex);
1469
1470 platform_device_register(&ch->pdev);
1471 if (!strncmp(ch->name, "LOOPBACK", 8) && ch->type == SMD_APPS_MODEM) {
1472 /* create a platform driver to be used by smd_tty driver
1473 * so that it can access the loopback port
1474 */
1475 loopback_tty_pdev.id = ch->type;
1476 platform_device_register(&loopback_tty_pdev);
1477 }
1478 return 0;
1479}
1480
1481static inline void notify_loopback_smd(void)
1482{
1483 unsigned long flags;
1484 struct smd_channel *ch;
1485
1486 spin_lock_irqsave(&smd_lock, flags);
1487 list_for_each_entry(ch, &smd_ch_list_loopback, ch_list) {
1488 ch->notify(ch->priv, SMD_EVENT_DATA);
1489 }
1490 spin_unlock_irqrestore(&smd_lock, flags);
1491}
1492
1493static int smd_alloc_loopback_channel(void)
1494{
1495 static struct smd_half_channel smd_loopback_ctl;
1496 static char smd_loopback_data[SMD_BUF_SIZE];
1497 struct smd_channel *ch;
1498
1499 ch = kzalloc(sizeof(struct smd_channel), GFP_KERNEL);
1500 if (ch == 0) {
1501 pr_err("%s: out of memory\n", __func__);
1502 return -1;
1503 }
1504 ch->n = SMD_LOOPBACK_CID;
1505
1506 ch->send = &smd_loopback_ctl;
1507 ch->recv = &smd_loopback_ctl;
1508 ch->send_data = smd_loopback_data;
1509 ch->recv_data = smd_loopback_data;
1510 ch->fifo_size = SMD_BUF_SIZE;
1511
1512 ch->fifo_mask = ch->fifo_size - 1;
1513 ch->type = SMD_LOOPBACK_TYPE;
1514 ch->notify_other_cpu = notify_loopback_smd;
1515
1516 ch->read = smd_stream_read;
1517 ch->write = smd_stream_write;
1518 ch->read_avail = smd_stream_read_avail;
1519 ch->write_avail = smd_stream_write_avail;
1520 ch->update_state = update_stream_state;
1521 ch->read_from_cb = smd_stream_read;
1522
1523 memset(ch->name, 0, 20);
1524 memcpy(ch->name, "local_loopback", 14);
1525
1526 ch->pdev.name = ch->name;
1527 ch->pdev.id = ch->type;
1528
1529 SMD_INFO("%s: '%s' cid=%d\n", __func__, ch->name, ch->n);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001530
1531 mutex_lock(&smd_creation_mutex);
1532 list_add(&ch->ch_list, &smd_ch_closed_list);
1533 mutex_unlock(&smd_creation_mutex);
1534
1535 platform_device_register(&ch->pdev);
Brian Swetland34f719b2009-10-30 16:22:05 -07001536 return 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001537}
1538
1539static void do_nothing_notify(void *priv, unsigned flags)
1540{
1541}
1542
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001543static void finalize_channel_close_fn(struct work_struct *work)
1544{
1545 unsigned long flags;
1546 struct smd_channel *ch;
1547 struct smd_channel *index;
1548
Eric Holmbergbb2b1fa2011-10-12 16:41:37 -06001549 mutex_lock(&smd_creation_mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001550 spin_lock_irqsave(&smd_lock, flags);
1551 list_for_each_entry_safe(ch, index, &smd_ch_to_close_list, ch_list) {
1552 list_del(&ch->ch_list);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001553 list_add(&ch->ch_list, &smd_ch_closed_list);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001554 ch->notify(ch->priv, SMD_EVENT_REOPEN_READY);
1555 ch->notify = do_nothing_notify;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001556 }
1557 spin_unlock_irqrestore(&smd_lock, flags);
Eric Holmbergbb2b1fa2011-10-12 16:41:37 -06001558 mutex_unlock(&smd_creation_mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001559}
1560
1561struct smd_channel *smd_get_channel(const char *name, uint32_t type)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001562{
1563 struct smd_channel *ch;
1564
1565 mutex_lock(&smd_creation_mutex);
1566 list_for_each_entry(ch, &smd_ch_closed_list, ch_list) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001567 if (!strcmp(name, ch->name) &&
1568 (type == ch->type)) {
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001569 list_del(&ch->ch_list);
1570 mutex_unlock(&smd_creation_mutex);
1571 return ch;
1572 }
1573 }
1574 mutex_unlock(&smd_creation_mutex);
1575
1576 return NULL;
1577}
1578
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001579int smd_named_open_on_edge(const char *name, uint32_t edge,
1580 smd_channel_t **_ch,
1581 void *priv, void (*notify)(void *, unsigned))
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001582{
1583 struct smd_channel *ch;
1584 unsigned long flags;
1585
1586 if (smd_initialized == 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001587 SMD_INFO("smd_open() before smd_init()\n");
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001588 return -ENODEV;
1589 }
1590
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001591 SMD_DBG("smd_open('%s', %p, %p)\n", name, priv, notify);
1592
1593 ch = smd_get_channel(name, edge);
Eric Holmbergbb2b1fa2011-10-12 16:41:37 -06001594 if (!ch) {
Eric Holmbergbb2b1fa2011-10-12 16:41:37 -06001595 /* check closing list for port */
1596 spin_lock_irqsave(&smd_lock, flags);
1597 list_for_each_entry(ch, &smd_ch_closing_list, ch_list) {
1598 if (!strncmp(name, ch->name, 20) &&
1599 (edge == ch->type)) {
1600 /* channel exists, but is being closed */
1601 spin_unlock_irqrestore(&smd_lock, flags);
1602 return -EAGAIN;
1603 }
1604 }
1605
1606 /* check closing workqueue list for port */
1607 list_for_each_entry(ch, &smd_ch_to_close_list, ch_list) {
1608 if (!strncmp(name, ch->name, 20) &&
1609 (edge == ch->type)) {
1610 /* channel exists, but is being closed */
1611 spin_unlock_irqrestore(&smd_lock, flags);
1612 return -EAGAIN;
1613 }
1614 }
1615 spin_unlock_irqrestore(&smd_lock, flags);
1616
1617 /* one final check to handle closing->closed race condition */
1618 ch = smd_get_channel(name, edge);
1619 if (!ch)
1620 return -ENODEV;
1621 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001622
1623 if (notify == 0)
1624 notify = do_nothing_notify;
1625
1626 ch->notify = notify;
1627 ch->current_packet = 0;
1628 ch->last_state = SMD_SS_CLOSED;
1629 ch->priv = priv;
1630
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001631 if (edge == SMD_LOOPBACK_TYPE) {
1632 ch->last_state = SMD_SS_OPENED;
1633 ch->send->state = SMD_SS_OPENED;
1634 ch->send->fDSR = 1;
1635 ch->send->fCTS = 1;
1636 ch->send->fCD = 1;
1637 }
1638
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001639 *_ch = ch;
1640
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001641 SMD_DBG("smd_open: opening '%s'\n", ch->name);
1642
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001643 spin_lock_irqsave(&smd_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001644 if (SMD_CHANNEL_TYPE(ch->type) == SMD_APPS_MODEM)
Brian Swetland37521a32009-07-01 18:30:47 -07001645 list_add(&ch->ch_list, &smd_ch_list_modem);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001646 else if (SMD_CHANNEL_TYPE(ch->type) == SMD_APPS_QDSP)
Brian Swetland37521a32009-07-01 18:30:47 -07001647 list_add(&ch->ch_list, &smd_ch_list_dsp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001648 else if (SMD_CHANNEL_TYPE(ch->type) == SMD_APPS_DSPS)
1649 list_add(&ch->ch_list, &smd_ch_list_dsps);
1650 else if (SMD_CHANNEL_TYPE(ch->type) == SMD_APPS_WCNSS)
1651 list_add(&ch->ch_list, &smd_ch_list_wcnss);
1652 else
1653 list_add(&ch->ch_list, &smd_ch_list_loopback);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001654
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001655 SMD_DBG("%s: opening ch %d\n", __func__, ch->n);
1656
1657 if (edge != SMD_LOOPBACK_TYPE)
1658 smd_state_change(ch, ch->last_state, SMD_SS_OPENING);
1659
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001660 spin_unlock_irqrestore(&smd_lock, flags);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001661
1662 return 0;
1663}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001664EXPORT_SYMBOL(smd_named_open_on_edge);
1665
1666
1667int smd_open(const char *name, smd_channel_t **_ch,
1668 void *priv, void (*notify)(void *, unsigned))
1669{
1670 return smd_named_open_on_edge(name, SMD_APPS_MODEM, _ch, priv,
1671 notify);
1672}
1673EXPORT_SYMBOL(smd_open);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001674
1675int smd_close(smd_channel_t *ch)
1676{
1677 unsigned long flags;
1678
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001679 if (ch == 0)
1680 return -1;
1681
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001682 SMD_INFO("smd_close(%s)\n", ch->name);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001683
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001684 spin_lock_irqsave(&smd_lock, flags);
1685 list_del(&ch->ch_list);
1686 if (ch->n == SMD_LOOPBACK_CID) {
1687 ch->send->fDSR = 0;
1688 ch->send->fCTS = 0;
1689 ch->send->fCD = 0;
1690 ch->send->state = SMD_SS_CLOSED;
1691 } else
1692 ch_set_state(ch, SMD_SS_CLOSED);
1693
1694 if (ch->recv->state == SMD_SS_OPENED) {
1695 list_add(&ch->ch_list, &smd_ch_closing_list);
1696 spin_unlock_irqrestore(&smd_lock, flags);
1697 } else {
1698 spin_unlock_irqrestore(&smd_lock, flags);
1699 ch->notify = do_nothing_notify;
1700 mutex_lock(&smd_creation_mutex);
1701 list_add(&ch->ch_list, &smd_ch_closed_list);
1702 mutex_unlock(&smd_creation_mutex);
1703 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001704
1705 return 0;
1706}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001707EXPORT_SYMBOL(smd_close);
1708
1709int smd_write_start(smd_channel_t *ch, int len)
1710{
1711 int ret;
1712 unsigned hdr[5];
1713
1714 if (!ch) {
1715 pr_err("%s: Invalid channel specified\n", __func__);
1716 return -ENODEV;
1717 }
1718 if (!ch->is_pkt_ch) {
1719 pr_err("%s: non-packet channel specified\n", __func__);
1720 return -EACCES;
1721 }
1722 if (len < 1) {
1723 pr_err("%s: invalid length: %d\n", __func__, len);
1724 return -EINVAL;
1725 }
1726
1727 if (ch->pending_pkt_sz) {
1728 pr_err("%s: packet of size: %d in progress\n", __func__,
1729 ch->pending_pkt_sz);
1730 return -EBUSY;
1731 }
1732 ch->pending_pkt_sz = len;
1733
1734 if (smd_stream_write_avail(ch) < (SMD_HEADER_SIZE)) {
1735 ch->pending_pkt_sz = 0;
1736 SMD_DBG("%s: no space to write packet header\n", __func__);
1737 return -EAGAIN;
1738 }
1739
1740 hdr[0] = len;
1741 hdr[1] = hdr[2] = hdr[3] = hdr[4] = 0;
1742
1743
1744 ret = smd_stream_write(ch, hdr, sizeof(hdr), 0);
1745 if (ret < 0 || ret != sizeof(hdr)) {
1746 ch->pending_pkt_sz = 0;
1747 pr_err("%s: packet header failed to write\n", __func__);
1748 return -EPERM;
1749 }
1750 return 0;
1751}
1752EXPORT_SYMBOL(smd_write_start);
1753
1754int smd_write_segment(smd_channel_t *ch, void *data, int len, int user_buf)
1755{
1756 int bytes_written;
1757
1758 if (!ch) {
1759 pr_err("%s: Invalid channel specified\n", __func__);
1760 return -ENODEV;
1761 }
1762 if (len < 1) {
1763 pr_err("%s: invalid length: %d\n", __func__, len);
1764 return -EINVAL;
1765 }
1766
1767 if (!ch->pending_pkt_sz) {
1768 pr_err("%s: no transaction in progress\n", __func__);
1769 return -ENOEXEC;
1770 }
1771 if (ch->pending_pkt_sz - len < 0) {
1772 pr_err("%s: segment of size: %d will make packet go over "
1773 "length\n", __func__, len);
1774 return -EINVAL;
1775 }
1776
1777 bytes_written = smd_stream_write(ch, data, len, user_buf);
1778
1779 ch->pending_pkt_sz -= bytes_written;
1780
1781 return bytes_written;
1782}
1783EXPORT_SYMBOL(smd_write_segment);
1784
1785int smd_write_end(smd_channel_t *ch)
1786{
1787
1788 if (!ch) {
1789 pr_err("%s: Invalid channel specified\n", __func__);
1790 return -ENODEV;
1791 }
1792 if (ch->pending_pkt_sz) {
1793 pr_err("%s: current packet not completely written\n", __func__);
1794 return -E2BIG;
1795 }
1796
1797 return 0;
1798}
1799EXPORT_SYMBOL(smd_write_end);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001800
1801int smd_read(smd_channel_t *ch, void *data, int len)
1802{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001803 return ch->read(ch, data, len, 0);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001804}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001805EXPORT_SYMBOL(smd_read);
1806
1807int smd_read_user_buffer(smd_channel_t *ch, void *data, int len)
1808{
1809 return ch->read(ch, data, len, 1);
1810}
1811EXPORT_SYMBOL(smd_read_user_buffer);
1812
1813int smd_read_from_cb(smd_channel_t *ch, void *data, int len)
1814{
1815 return ch->read_from_cb(ch, data, len, 0);
1816}
1817EXPORT_SYMBOL(smd_read_from_cb);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001818
1819int smd_write(smd_channel_t *ch, const void *data, int len)
1820{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001821 return ch->pending_pkt_sz ? -EBUSY : ch->write(ch, data, len, 0);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001822}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001823EXPORT_SYMBOL(smd_write);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001824
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001825int smd_write_user_buffer(smd_channel_t *ch, const void *data, int len)
Brian Swetland636eb9c2009-12-07 15:28:08 -08001826{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001827 return ch->pending_pkt_sz ? -EBUSY : ch->write(ch, data, len, 1);
Brian Swetland636eb9c2009-12-07 15:28:08 -08001828}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001829EXPORT_SYMBOL(smd_write_user_buffer);
Brian Swetland636eb9c2009-12-07 15:28:08 -08001830
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001831int smd_read_avail(smd_channel_t *ch)
1832{
1833 return ch->read_avail(ch);
1834}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001835EXPORT_SYMBOL(smd_read_avail);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001836
1837int smd_write_avail(smd_channel_t *ch)
1838{
1839 return ch->write_avail(ch);
1840}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001841EXPORT_SYMBOL(smd_write_avail);
1842
1843void smd_enable_read_intr(smd_channel_t *ch)
1844{
1845 if (ch)
1846 ch->send->fBLOCKREADINTR = 0;
1847}
1848EXPORT_SYMBOL(smd_enable_read_intr);
1849
1850void smd_disable_read_intr(smd_channel_t *ch)
1851{
1852 if (ch)
1853 ch->send->fBLOCKREADINTR = 1;
1854}
1855EXPORT_SYMBOL(smd_disable_read_intr);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001856
1857int smd_wait_until_readable(smd_channel_t *ch, int bytes)
1858{
1859 return -1;
1860}
1861
1862int smd_wait_until_writable(smd_channel_t *ch, int bytes)
1863{
1864 return -1;
1865}
1866
1867int smd_cur_packet_size(smd_channel_t *ch)
1868{
1869 return ch->current_packet;
1870}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001871EXPORT_SYMBOL(smd_cur_packet_size);
1872
1873int smd_tiocmget(smd_channel_t *ch)
1874{
1875 return (ch->recv->fDSR ? TIOCM_DSR : 0) |
1876 (ch->recv->fCTS ? TIOCM_CTS : 0) |
1877 (ch->recv->fCD ? TIOCM_CD : 0) |
1878 (ch->recv->fRI ? TIOCM_RI : 0) |
1879 (ch->send->fCTS ? TIOCM_RTS : 0) |
1880 (ch->send->fDSR ? TIOCM_DTR : 0);
1881}
1882EXPORT_SYMBOL(smd_tiocmget);
1883
Vamsi Krishnacb12a102011-08-17 15:18:26 -07001884/* this api will be called while holding smd_lock */
1885int
1886smd_tiocmset_from_cb(smd_channel_t *ch, unsigned int set, unsigned int clear)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001887{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001888 if (set & TIOCM_DTR)
1889 ch->send->fDSR = 1;
1890
1891 if (set & TIOCM_RTS)
1892 ch->send->fCTS = 1;
1893
1894 if (clear & TIOCM_DTR)
1895 ch->send->fDSR = 0;
1896
1897 if (clear & TIOCM_RTS)
1898 ch->send->fCTS = 0;
1899
1900 ch->send->fSTATE = 1;
1901 barrier();
1902 ch->notify_other_cpu();
Vamsi Krishnacb12a102011-08-17 15:18:26 -07001903
1904 return 0;
1905}
1906EXPORT_SYMBOL(smd_tiocmset_from_cb);
1907
1908int smd_tiocmset(smd_channel_t *ch, unsigned int set, unsigned int clear)
1909{
1910 unsigned long flags;
1911
1912 spin_lock_irqsave(&smd_lock, flags);
1913 smd_tiocmset_from_cb(ch, set, clear);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001914 spin_unlock_irqrestore(&smd_lock, flags);
1915
1916 return 0;
1917}
1918EXPORT_SYMBOL(smd_tiocmset);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001919
1920
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001921/* -------------------------------------------------------------------------- */
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001922
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001923/* smem_alloc returns the pointer to smem item if it is already allocated.
1924 * Otherwise, it returns NULL.
1925 */
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001926void *smem_alloc(unsigned id, unsigned size)
1927{
1928 return smem_find(id, size);
1929}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001930EXPORT_SYMBOL(smem_alloc);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001931
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001932/* smem_alloc2 returns the pointer to smem item. If it is not allocated,
1933 * it allocates it and then returns the pointer to it.
1934 */
Angshuman Sarkar4eade0d2011-08-17 14:06:23 +05301935void *smem_alloc2(unsigned id, unsigned size_in)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001936{
1937 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
1938 struct smem_heap_entry *toc = shared->heap_toc;
1939 unsigned long flags;
1940 void *ret = NULL;
1941
1942 if (!shared->heap_info.initialized) {
1943 pr_err("%s: smem heap info not initialized\n", __func__);
1944 return NULL;
1945 }
1946
1947 if (id >= SMEM_NUM_ITEMS)
1948 return NULL;
1949
1950 size_in = ALIGN(size_in, 8);
1951 remote_spin_lock_irqsave(&remote_spinlock, flags);
1952 if (toc[id].allocated) {
1953 SMD_DBG("%s: %u already allocated\n", __func__, id);
1954 if (size_in != toc[id].size)
1955 pr_err("%s: wrong size %u (expected %u)\n",
1956 __func__, toc[id].size, size_in);
1957 else
1958 ret = (void *)(MSM_SHARED_RAM_BASE + toc[id].offset);
1959 } else if (id > SMEM_FIXED_ITEM_LAST) {
1960 SMD_DBG("%s: allocating %u\n", __func__, id);
1961 if (shared->heap_info.heap_remaining >= size_in) {
1962 toc[id].offset = shared->heap_info.free_offset;
1963 toc[id].size = size_in;
1964 wmb();
1965 toc[id].allocated = 1;
1966
1967 shared->heap_info.free_offset += size_in;
1968 shared->heap_info.heap_remaining -= size_in;
1969 ret = (void *)(MSM_SHARED_RAM_BASE + toc[id].offset);
1970 } else
1971 pr_err("%s: not enough memory %u (required %u)\n",
1972 __func__, shared->heap_info.heap_remaining,
1973 size_in);
1974 }
1975 wmb();
1976 remote_spin_unlock_irqrestore(&remote_spinlock, flags);
1977 return ret;
1978}
Angshuman Sarkar4eade0d2011-08-17 14:06:23 +05301979EXPORT_SYMBOL(smem_alloc2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001980
1981void *smem_get_entry(unsigned id, unsigned *size)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001982{
1983 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
1984 struct smem_heap_entry *toc = shared->heap_toc;
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +05301985 int use_spinlocks = spinlocks_initialized;
1986 void *ret = 0;
1987 unsigned long flags = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001988
1989 if (id >= SMEM_NUM_ITEMS)
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +05301990 return ret;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001991
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +05301992 if (use_spinlocks)
1993 remote_spin_lock_irqsave(&remote_spinlock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001994 /* toc is in device memory and cannot be speculatively accessed */
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001995 if (toc[id].allocated) {
1996 *size = toc[id].size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001997 barrier();
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +05301998 ret = (void *) (MSM_SHARED_RAM_BASE + toc[id].offset);
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001999 } else {
2000 *size = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002001 }
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +05302002 if (use_spinlocks)
2003 remote_spin_unlock_irqrestore(&remote_spinlock, flags);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002004
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +05302005 return ret;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002006}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002007EXPORT_SYMBOL(smem_get_entry);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002008
2009void *smem_find(unsigned id, unsigned size_in)
2010{
2011 unsigned size;
2012 void *ptr;
2013
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002014 ptr = smem_get_entry(id, &size);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002015 if (!ptr)
2016 return 0;
2017
2018 size_in = ALIGN(size_in, 8);
2019 if (size_in != size) {
2020 pr_err("smem_find(%d, %d): wrong size %d\n",
2021 id, size_in, size);
2022 return 0;
2023 }
2024
2025 return ptr;
2026}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002027EXPORT_SYMBOL(smem_find);
2028
2029static int smsm_cb_init(void)
2030{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002031 struct smsm_state_info *state_info;
2032 int n;
2033 int ret = 0;
2034
2035 smsm_states = kmalloc(sizeof(struct smsm_state_info)*SMSM_NUM_ENTRIES,
2036 GFP_KERNEL);
2037
2038 if (!smsm_states) {
2039 pr_err("%s: SMSM init failed\n", __func__);
2040 return -ENOMEM;
2041 }
2042
Eric Holmbergc8002902011-09-16 13:55:57 -06002043 mutex_lock(&smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002044 for (n = 0; n < SMSM_NUM_ENTRIES; n++) {
2045 state_info = &smsm_states[n];
2046 state_info->last_value = __raw_readl(SMSM_STATE_ADDR(n));
2047 INIT_LIST_HEAD(&state_info->callbacks);
2048 }
Eric Holmbergc8002902011-09-16 13:55:57 -06002049 mutex_unlock(&smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002050
2051 return ret;
2052}
2053
2054static int smsm_init(void)
2055{
2056 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
2057 int i;
2058 struct smsm_size_info_type *smsm_size_info;
2059
2060 i = remote_spin_lock_init(&remote_spinlock, SMEM_SPINLOCK_SMEM_ALLOC);
2061 if (i) {
2062 pr_err("%s: remote spinlock init failed %d\n", __func__, i);
2063 return i;
2064 }
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +05302065 spinlocks_initialized = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002066
2067 smsm_size_info = smem_alloc(SMEM_SMSM_SIZE_INFO,
2068 sizeof(struct smsm_size_info_type));
2069 if (smsm_size_info) {
2070 SMSM_NUM_ENTRIES = smsm_size_info->num_entries;
2071 SMSM_NUM_HOSTS = smsm_size_info->num_hosts;
2072 }
2073
Eric Holmbergc7e8daf2011-12-28 11:49:21 -07002074 i = kfifo_alloc(&smsm_snapshot_fifo,
2075 sizeof(uint32_t) * SMSM_NUM_ENTRIES * SMSM_SNAPSHOT_CNT,
2076 GFP_KERNEL);
2077 if (i) {
2078 pr_err("%s: SMSM state fifo alloc failed %d\n", __func__, i);
2079 return i;
2080 }
2081
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002082 if (!smsm_info.state) {
2083 smsm_info.state = smem_alloc2(ID_SHARED_STATE,
2084 SMSM_NUM_ENTRIES *
2085 sizeof(uint32_t));
2086
2087 if (smsm_info.state) {
2088 __raw_writel(0, SMSM_STATE_ADDR(SMSM_APPS_STATE));
2089 if ((shared->version[VERSION_MODEM] >> 16) >= 0xB)
2090 __raw_writel(0, \
2091 SMSM_STATE_ADDR(SMSM_APPS_DEM_I));
2092 }
2093 }
2094
2095 if (!smsm_info.intr_mask) {
2096 smsm_info.intr_mask = smem_alloc2(SMEM_SMSM_CPU_INTR_MASK,
2097 SMSM_NUM_ENTRIES *
2098 SMSM_NUM_HOSTS *
2099 sizeof(uint32_t));
2100
2101 if (smsm_info.intr_mask)
2102 for (i = 0; i < SMSM_NUM_ENTRIES; i++)
2103 __raw_writel(0xffffffff,
2104 SMSM_INTR_MASK_ADDR(i, SMSM_APPS));
2105 }
2106
2107 if (!smsm_info.intr_mux)
2108 smsm_info.intr_mux = smem_alloc2(SMEM_SMD_SMSM_INTR_MUX,
2109 SMSM_NUM_INTR_MUX *
2110 sizeof(uint32_t));
2111
2112 i = smsm_cb_init();
2113 if (i)
2114 return i;
2115
2116 wmb();
2117 return 0;
2118}
2119
2120void smsm_reset_modem(unsigned mode)
2121{
2122 if (mode == SMSM_SYSTEM_DOWNLOAD) {
2123 mode = SMSM_RESET | SMSM_SYSTEM_DOWNLOAD;
2124 } else if (mode == SMSM_MODEM_WAIT) {
2125 mode = SMSM_RESET | SMSM_MODEM_WAIT;
2126 } else { /* reset_mode is SMSM_RESET or default */
2127 mode = SMSM_RESET;
2128 }
2129
2130 smsm_change_state(SMSM_APPS_STATE, mode, mode);
2131}
2132EXPORT_SYMBOL(smsm_reset_modem);
2133
2134void smsm_reset_modem_cont(void)
2135{
2136 unsigned long flags;
2137 uint32_t state;
2138
2139 if (!smsm_info.state)
2140 return;
2141
2142 spin_lock_irqsave(&smem_lock, flags);
2143 state = __raw_readl(SMSM_STATE_ADDR(SMSM_APPS_STATE)) \
2144 & ~SMSM_MODEM_WAIT;
2145 __raw_writel(state, SMSM_STATE_ADDR(SMSM_APPS_STATE));
2146 wmb();
2147 spin_unlock_irqrestore(&smem_lock, flags);
2148}
2149EXPORT_SYMBOL(smsm_reset_modem_cont);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002150
Eric Holmbergc7e8daf2011-12-28 11:49:21 -07002151static void smsm_cb_snapshot(void)
2152{
2153 int n;
2154 uint32_t new_state;
2155 int ret;
2156
2157 ret = kfifo_avail(&smsm_snapshot_fifo);
2158 if (ret < (SMSM_NUM_ENTRIES * 4)) {
2159 pr_err("%s: SMSM snapshot full %d\n", __func__, ret);
2160 return;
2161 }
2162
2163 for (n = 0; n < SMSM_NUM_ENTRIES; n++) {
2164 new_state = __raw_readl(SMSM_STATE_ADDR(n));
2165
2166 ret = kfifo_in(&smsm_snapshot_fifo,
2167 &new_state, sizeof(new_state));
2168 if (ret != sizeof(new_state)) {
2169 pr_err("%s: SMSM snapshot failure %d\n", __func__, ret);
2170 return;
2171 }
2172 }
2173 schedule_work(&smsm_cb_work);
2174}
2175
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002176static irqreturn_t smsm_irq_handler(int irq, void *data)
2177{
2178 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002179
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002180 if (irq == INT_ADSP_A11_SMSM) {
Eric Holmberg6282c5d2011-10-27 17:30:57 -06002181 uint32_t mux_val;
2182 static uint32_t prev_smem_q6_apps_smsm;
2183
2184 if (smsm_info.intr_mux && cpu_is_qsd8x50()) {
2185 mux_val = __raw_readl(
2186 SMSM_INTR_MUX_ADDR(SMEM_Q6_APPS_SMSM));
2187 if (mux_val != prev_smem_q6_apps_smsm)
2188 prev_smem_q6_apps_smsm = mux_val;
2189 }
2190
Eric Holmbergc7e8daf2011-12-28 11:49:21 -07002191 spin_lock_irqsave(&smem_lock, flags);
2192 smsm_cb_snapshot();
2193 spin_unlock_irqrestore(&smem_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002194 return IRQ_HANDLED;
2195 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002196
2197 spin_lock_irqsave(&smem_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002198 if (!smsm_info.state) {
2199 SMSM_INFO("<SM NO STATE>\n");
2200 } else {
2201 unsigned old_apps, apps;
2202 unsigned modm = __raw_readl(SMSM_STATE_ADDR(SMSM_MODEM_STATE));
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002203
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002204 old_apps = apps = __raw_readl(SMSM_STATE_ADDR(SMSM_APPS_STATE));
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002205
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002206 SMSM_DBG("<SM %08x %08x>\n", apps, modm);
2207 if (apps & SMSM_RESET) {
2208 /* If we get an interrupt and the apps SMSM_RESET
2209 bit is already set, the modem is acking the
2210 app's reset ack. */
Stepan Moskovchenkoa1ca7582011-10-25 14:45:09 -07002211 if (!cpu_is_msm8960() && !cpu_is_msm8930())
Angshuman Sarkaread67bd2011-09-21 20:13:12 +05302212 apps &= ~SMSM_RESET;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002213 /* Issue a fake irq to handle any
2214 * smd state changes during reset
2215 */
2216 smd_fake_irq_handler(0);
Brian Swetland5b0f5a32009-04-26 18:38:49 -07002217
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002218 /* queue modem restart notify chain */
2219 modem_queue_start_reset_notify();
2220
2221 } else if (modm & SMSM_RESET) {
Stepan Moskovchenkoa1ca7582011-10-25 14:45:09 -07002222 if (!cpu_is_msm8960() && !cpu_is_msm8930())
Angshuman Sarkaread67bd2011-09-21 20:13:12 +05302223 apps |= SMSM_RESET;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002224
2225 pr_err("\nSMSM: Modem SMSM state changed to SMSM_RESET.");
2226 modem_queue_start_reset_notify();
2227
2228 } else if (modm & SMSM_INIT) {
2229 if (!(apps & SMSM_INIT)) {
2230 apps |= SMSM_INIT;
2231 modem_queue_smsm_init_notify();
2232 }
2233
2234 if (modm & SMSM_SMDINIT)
2235 apps |= SMSM_SMDINIT;
2236 if ((apps & (SMSM_INIT | SMSM_SMDINIT | SMSM_RPCINIT)) ==
2237 (SMSM_INIT | SMSM_SMDINIT | SMSM_RPCINIT))
2238 apps |= SMSM_RUN;
2239 } else if (modm & SMSM_SYSTEM_DOWNLOAD) {
2240 pr_err("\nSMSM: Modem SMSM state changed to SMSM_SYSTEM_DOWNLOAD.");
2241 modem_queue_start_reset_notify();
2242 }
2243
2244 if (old_apps != apps) {
2245 SMSM_DBG("<SM %08x NOTIFY>\n", apps);
2246 __raw_writel(apps, SMSM_STATE_ADDR(SMSM_APPS_STATE));
2247 do_smd_probe();
2248 notify_other_smsm(SMSM_APPS_STATE, (old_apps ^ apps));
2249 }
2250
Eric Holmbergc7e8daf2011-12-28 11:49:21 -07002251 smsm_cb_snapshot();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002252 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002253 spin_unlock_irqrestore(&smem_lock, flags);
2254 return IRQ_HANDLED;
2255}
2256
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002257int smsm_change_intr_mask(uint32_t smsm_entry,
2258 uint32_t clear_mask, uint32_t set_mask)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002259{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002260 uint32_t old_mask, new_mask;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002261 unsigned long flags;
Brian Swetland5b0f5a32009-04-26 18:38:49 -07002262
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002263 if (smsm_entry >= SMSM_NUM_ENTRIES) {
2264 pr_err("smsm_change_state: Invalid entry %d\n",
2265 smsm_entry);
2266 return -EINVAL;
2267 }
2268
2269 if (!smsm_info.intr_mask) {
2270 pr_err("smsm_change_intr_mask <SM NO STATE>\n");
Brian Swetland5b0f5a32009-04-26 18:38:49 -07002271 return -EIO;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002272 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002273
2274 spin_lock_irqsave(&smem_lock, flags);
2275
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002276 old_mask = __raw_readl(SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_APPS));
2277 new_mask = (old_mask & ~clear_mask) | set_mask;
2278 __raw_writel(new_mask, SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_APPS));
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002279
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002280 wmb();
2281 spin_unlock_irqrestore(&smem_lock, flags);
Brian Swetland5b0f5a32009-04-26 18:38:49 -07002282
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002283 return 0;
2284}
2285EXPORT_SYMBOL(smsm_change_intr_mask);
2286
2287int smsm_get_intr_mask(uint32_t smsm_entry, uint32_t *intr_mask)
2288{
2289 if (smsm_entry >= SMSM_NUM_ENTRIES) {
2290 pr_err("smsm_change_state: Invalid entry %d\n",
2291 smsm_entry);
2292 return -EINVAL;
2293 }
2294
2295 if (!smsm_info.intr_mask) {
2296 pr_err("smsm_change_intr_mask <SM NO STATE>\n");
2297 return -EIO;
2298 }
2299
2300 *intr_mask = __raw_readl(SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_APPS));
2301 return 0;
2302}
2303EXPORT_SYMBOL(smsm_get_intr_mask);
2304
2305int smsm_change_state(uint32_t smsm_entry,
2306 uint32_t clear_mask, uint32_t set_mask)
2307{
2308 unsigned long flags;
2309 uint32_t old_state, new_state;
2310
2311 if (smsm_entry >= SMSM_NUM_ENTRIES) {
2312 pr_err("smsm_change_state: Invalid entry %d",
2313 smsm_entry);
2314 return -EINVAL;
2315 }
2316
2317 if (!smsm_info.state) {
2318 pr_err("smsm_change_state <SM NO STATE>\n");
2319 return -EIO;
2320 }
2321 spin_lock_irqsave(&smem_lock, flags);
2322
2323 old_state = __raw_readl(SMSM_STATE_ADDR(smsm_entry));
2324 new_state = (old_state & ~clear_mask) | set_mask;
2325 __raw_writel(new_state, SMSM_STATE_ADDR(smsm_entry));
2326 SMSM_DBG("smsm_change_state %x\n", new_state);
2327 notify_other_smsm(SMSM_APPS_STATE, (old_state ^ new_state));
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002328
2329 spin_unlock_irqrestore(&smem_lock, flags);
2330
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002331 return 0;
2332}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002333EXPORT_SYMBOL(smsm_change_state);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002334
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002335uint32_t smsm_get_state(uint32_t smsm_entry)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002336{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002337 uint32_t rv = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002338
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002339 /* needs interface change to return error code */
2340 if (smsm_entry >= SMSM_NUM_ENTRIES) {
2341 pr_err("smsm_change_state: Invalid entry %d",
2342 smsm_entry);
2343 return 0;
2344 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002345
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002346 if (!smsm_info.state) {
2347 pr_err("smsm_get_state <SM NO STATE>\n");
2348 } else {
2349 rv = __raw_readl(SMSM_STATE_ADDR(smsm_entry));
2350 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002351
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002352 return rv;
2353}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002354EXPORT_SYMBOL(smsm_get_state);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002355
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002356/**
2357 * Performs SMSM callback client notifiction.
2358 */
2359void notify_smsm_cb_clients_worker(struct work_struct *work)
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -07002360{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002361 struct smsm_state_cb_info *cb_info;
2362 struct smsm_state_info *state_info;
2363 int n;
2364 uint32_t new_state;
2365 uint32_t state_changes;
Eric Holmbergc7e8daf2011-12-28 11:49:21 -07002366 int ret;
2367 int snapshot_size = SMSM_NUM_ENTRIES * sizeof(uint32_t);
Brian Swetland03e00cd2009-07-01 17:58:37 -07002368
Eric Holmbergc7e8daf2011-12-28 11:49:21 -07002369 if (!smd_initialized)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002370 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002371
Eric Holmbergc7e8daf2011-12-28 11:49:21 -07002372 while (kfifo_len(&smsm_snapshot_fifo) >= snapshot_size) {
2373 mutex_lock(&smsm_lock);
2374 for (n = 0; n < SMSM_NUM_ENTRIES; n++) {
2375 state_info = &smsm_states[n];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002376
Eric Holmbergc7e8daf2011-12-28 11:49:21 -07002377 ret = kfifo_out(&smsm_snapshot_fifo, &new_state,
2378 sizeof(new_state));
2379 if (ret != sizeof(new_state)) {
2380 pr_err("%s: snapshot underflow %d\n",
2381 __func__, ret);
2382 mutex_unlock(&smsm_lock);
2383 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002384 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002385
Eric Holmbergc7e8daf2011-12-28 11:49:21 -07002386 state_changes = state_info->last_value ^ new_state;
2387 if (state_changes) {
2388 list_for_each_entry(cb_info,
2389 &state_info->callbacks, cb_list) {
2390
2391 if (cb_info->mask & state_changes)
2392 cb_info->notify(cb_info->data,
2393 state_info->last_value,
2394 new_state);
2395 }
2396 state_info->last_value = new_state;
2397 }
2398 }
2399 mutex_unlock(&smsm_lock);
2400 }
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -07002401}
2402
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -07002403
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002404/**
2405 * Registers callback for SMSM state notifications when the specified
2406 * bits change.
2407 *
2408 * @smsm_entry Processor entry to deregister
2409 * @mask Bits to deregister (if result is 0, callback is removed)
2410 * @notify Notification function to deregister
2411 * @data Opaque data passed in to callback
2412 *
2413 * @returns Status code
2414 * <0 error code
2415 * 0 inserted new entry
2416 * 1 updated mask of existing entry
2417 */
2418int smsm_state_cb_register(uint32_t smsm_entry, uint32_t mask,
2419 void (*notify)(void *, uint32_t, uint32_t), void *data)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002420{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002421 struct smsm_state_cb_info *cb_info;
2422 struct smsm_state_cb_info *cb_found = 0;
2423 int ret = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002424
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002425 if (smsm_entry >= SMSM_NUM_ENTRIES)
2426 return -EINVAL;
2427
Eric Holmbergc8002902011-09-16 13:55:57 -06002428 mutex_lock(&smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002429
2430 if (!smsm_states) {
2431 /* smsm not yet initialized */
2432 ret = -ENODEV;
2433 goto cleanup;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002434 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002435
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002436 list_for_each_entry(cb_info,
2437 &smsm_states[smsm_entry].callbacks, cb_list) {
2438 if ((cb_info->notify == notify) &&
2439 (cb_info->data == data)) {
2440 cb_info->mask |= mask;
2441 cb_found = cb_info;
2442 ret = 1;
Brian Swetland5b0f5a32009-04-26 18:38:49 -07002443 break;
2444 }
2445 }
2446
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002447 if (!cb_found) {
2448 cb_info = kmalloc(sizeof(struct smsm_state_cb_info),
2449 GFP_ATOMIC);
2450 if (!cb_info) {
2451 ret = -ENOMEM;
2452 goto cleanup;
2453 }
2454
2455 cb_info->mask = mask;
2456 cb_info->notify = notify;
2457 cb_info->data = data;
2458 INIT_LIST_HEAD(&cb_info->cb_list);
2459 list_add_tail(&cb_info->cb_list,
2460 &smsm_states[smsm_entry].callbacks);
2461 }
2462
2463cleanup:
Eric Holmbergc8002902011-09-16 13:55:57 -06002464 mutex_unlock(&smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002465 return ret;
2466}
2467EXPORT_SYMBOL(smsm_state_cb_register);
2468
2469
2470/**
2471 * Deregisters for SMSM state notifications for the specified bits.
2472 *
2473 * @smsm_entry Processor entry to deregister
2474 * @mask Bits to deregister (if result is 0, callback is removed)
2475 * @notify Notification function to deregister
2476 * @data Opaque data passed in to callback
2477 *
2478 * @returns Status code
2479 * <0 error code
2480 * 0 not found
2481 * 1 updated mask
2482 * 2 removed callback
2483 */
2484int smsm_state_cb_deregister(uint32_t smsm_entry, uint32_t mask,
2485 void (*notify)(void *, uint32_t, uint32_t), void *data)
2486{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002487 struct smsm_state_cb_info *cb_info;
2488 int ret = 0;
2489
2490 if (smsm_entry >= SMSM_NUM_ENTRIES)
2491 return -EINVAL;
2492
Eric Holmbergc8002902011-09-16 13:55:57 -06002493 mutex_lock(&smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002494
2495 if (!smsm_states) {
2496 /* smsm not yet initialized */
Eric Holmbergc8002902011-09-16 13:55:57 -06002497 mutex_unlock(&smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002498 return -ENODEV;
2499 }
2500
2501 list_for_each_entry(cb_info,
2502 &smsm_states[smsm_entry].callbacks, cb_list) {
2503 if ((cb_info->notify == notify) &&
2504 (cb_info->data == data)) {
2505 cb_info->mask &= ~mask;
2506 ret = 1;
2507 if (!cb_info->mask) {
2508 /* no mask bits set, remove callback */
2509 list_del(&cb_info->cb_list);
2510 kfree(cb_info);
2511 ret = 2;
2512 }
2513 break;
2514 }
2515 }
2516
Eric Holmbergc8002902011-09-16 13:55:57 -06002517 mutex_unlock(&smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002518 return ret;
2519}
2520EXPORT_SYMBOL(smsm_state_cb_deregister);
2521
2522
2523int smd_core_init(void)
2524{
2525 int r;
2526 unsigned long flags = IRQF_TRIGGER_RISING;
2527 SMD_INFO("smd_core_init()\n");
Brian Swetland5b0f5a32009-04-26 18:38:49 -07002528
Brian Swetland37521a32009-07-01 18:30:47 -07002529 r = request_irq(INT_A9_M2A_0, smd_modem_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002530 flags, "smd_dev", 0);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002531 if (r < 0)
2532 return r;
2533 r = enable_irq_wake(INT_A9_M2A_0);
2534 if (r < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002535 pr_err("smd_core_init: "
2536 "enable_irq_wake failed for INT_A9_M2A_0\n");
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002537
2538 r = request_irq(INT_A9_M2A_5, smsm_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002539 flags, "smsm_dev", 0);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002540 if (r < 0) {
2541 free_irq(INT_A9_M2A_0, 0);
2542 return r;
2543 }
2544 r = enable_irq_wake(INT_A9_M2A_5);
2545 if (r < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002546 pr_err("smd_core_init: "
2547 "enable_irq_wake failed for INT_A9_M2A_5\n");
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002548
Brian Swetland37521a32009-07-01 18:30:47 -07002549#if defined(CONFIG_QDSP6)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002550#if (INT_ADSP_A11 == INT_ADSP_A11_SMSM)
2551 flags |= IRQF_SHARED;
2552#endif
Brian Swetland37521a32009-07-01 18:30:47 -07002553 r = request_irq(INT_ADSP_A11, smd_dsp_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002554 flags, "smd_dev", smd_dsp_irq_handler);
Brian Swetland37521a32009-07-01 18:30:47 -07002555 if (r < 0) {
2556 free_irq(INT_A9_M2A_0, 0);
2557 free_irq(INT_A9_M2A_5, 0);
2558 return r;
2559 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002560
2561 r = request_irq(INT_ADSP_A11_SMSM, smsm_irq_handler,
2562 flags, "smsm_dev", smsm_irq_handler);
2563 if (r < 0) {
2564 free_irq(INT_A9_M2A_0, 0);
2565 free_irq(INT_A9_M2A_5, 0);
2566 free_irq(INT_ADSP_A11, smd_dsp_irq_handler);
2567 return r;
2568 }
2569
2570 r = enable_irq_wake(INT_ADSP_A11);
2571 if (r < 0)
2572 pr_err("smd_core_init: "
2573 "enable_irq_wake failed for INT_ADSP_A11\n");
2574
2575#if (INT_ADSP_A11 != INT_ADSP_A11_SMSM)
2576 r = enable_irq_wake(INT_ADSP_A11_SMSM);
2577 if (r < 0)
2578 pr_err("smd_core_init: enable_irq_wake "
2579 "failed for INT_ADSP_A11_SMSM\n");
2580#endif
2581 flags &= ~IRQF_SHARED;
Brian Swetland37521a32009-07-01 18:30:47 -07002582#endif
2583
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002584#if defined(CONFIG_DSPS)
2585 r = request_irq(INT_DSPS_A11, smd_dsps_irq_handler,
2586 flags, "smd_dev", smd_dsps_irq_handler);
2587 if (r < 0) {
2588 free_irq(INT_A9_M2A_0, 0);
2589 free_irq(INT_A9_M2A_5, 0);
2590 free_irq(INT_ADSP_A11, smd_dsp_irq_handler);
2591 free_irq(INT_ADSP_A11_SMSM, smsm_irq_handler);
2592 return r;
2593 }
Brian Swetland5b0f5a32009-04-26 18:38:49 -07002594
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002595 r = enable_irq_wake(INT_DSPS_A11);
2596 if (r < 0)
2597 pr_err("smd_core_init: "
2598 "enable_irq_wake failed for INT_ADSP_A11\n");
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -07002599#endif
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002600
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002601#if defined(CONFIG_WCNSS)
2602 r = request_irq(INT_WCNSS_A11, smd_wcnss_irq_handler,
2603 flags, "smd_dev", smd_wcnss_irq_handler);
2604 if (r < 0) {
2605 free_irq(INT_A9_M2A_0, 0);
2606 free_irq(INT_A9_M2A_5, 0);
2607 free_irq(INT_ADSP_A11, smd_dsp_irq_handler);
2608 free_irq(INT_ADSP_A11_SMSM, smsm_irq_handler);
2609 free_irq(INT_DSPS_A11, smd_dsps_irq_handler);
2610 return r;
2611 }
2612
2613 r = enable_irq_wake(INT_WCNSS_A11);
2614 if (r < 0)
2615 pr_err("smd_core_init: "
2616 "enable_irq_wake failed for INT_WCNSS_A11\n");
2617
2618 r = request_irq(INT_WCNSS_A11_SMSM, smsm_irq_handler,
2619 flags, "smsm_dev", smsm_irq_handler);
2620 if (r < 0) {
2621 free_irq(INT_A9_M2A_0, 0);
2622 free_irq(INT_A9_M2A_5, 0);
2623 free_irq(INT_ADSP_A11, smd_dsp_irq_handler);
2624 free_irq(INT_ADSP_A11_SMSM, smsm_irq_handler);
2625 free_irq(INT_DSPS_A11, smd_dsps_irq_handler);
2626 free_irq(INT_WCNSS_A11, smd_wcnss_irq_handler);
2627 return r;
2628 }
2629
2630 r = enable_irq_wake(INT_WCNSS_A11_SMSM);
2631 if (r < 0)
2632 pr_err("smd_core_init: "
2633 "enable_irq_wake failed for INT_WCNSS_A11_SMSM\n");
2634#endif
2635
Jeff Hugo6a8057c2011-08-16 13:47:12 -06002636#if defined(CONFIG_DSPS_SMSM)
2637 r = request_irq(INT_DSPS_A11_SMSM, smsm_irq_handler,
2638 flags, "smsm_dev", smsm_irq_handler);
2639 if (r < 0) {
2640 free_irq(INT_A9_M2A_0, 0);
2641 free_irq(INT_A9_M2A_5, 0);
2642 free_irq(INT_ADSP_A11, smd_dsp_irq_handler);
2643 free_irq(INT_ADSP_A11_SMSM, smsm_irq_handler);
2644 free_irq(INT_DSPS_A11, smd_dsps_irq_handler);
2645 free_irq(INT_WCNSS_A11, smd_wcnss_irq_handler);
2646 free_irq(INT_WCNSS_A11_SMSM, smsm_irq_handler);
2647 return r;
2648 }
2649
2650 r = enable_irq_wake(INT_DSPS_A11_SMSM);
2651 if (r < 0)
2652 pr_err("smd_core_init: "
2653 "enable_irq_wake failed for INT_DSPS_A11_SMSM\n");
2654#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002655 SMD_INFO("smd_core_init() done\n");
2656
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002657 return 0;
2658}
2659
Angshuman Sarkarbad32df2012-02-01 19:52:52 +05302660static int intr_init(struct interrupt_config_item *private_irq,
2661 struct smd_irq_config *platform_irq,
2662 struct platform_device *pdev
2663 )
2664{
2665 int irq_id;
2666 int ret;
2667 int ret_wake;
2668
2669 private_irq->out_bit_pos = platform_irq->out_bit_pos;
2670 private_irq->out_offset = platform_irq->out_offset;
2671 private_irq->out_base = platform_irq->out_base;
2672
2673 irq_id = platform_get_irq_byname(
2674 pdev,
2675 platform_irq->irq_name
2676 );
2677 SMD_DBG("smd: %s: register irq: %s id: %d\n", __func__,
2678 platform_irq->irq_name, irq_id);
2679 ret = request_irq(irq_id,
2680 private_irq->irq_handler,
2681 platform_irq->flags,
2682 platform_irq->device_name,
2683 (void *)platform_irq->dev_id
2684 );
2685 if (ret < 0) {
2686 platform_irq->irq_id = ret;
2687 } else {
2688 platform_irq->irq_id = irq_id;
2689 ret_wake = enable_irq_wake(irq_id);
2690 if (ret_wake < 0) {
2691 pr_err("smd: enable_irq_wake failed on %s",
2692 platform_irq->irq_name);
2693 }
2694 }
2695
2696 return ret;
2697}
2698
2699int smd_core_platform_init(struct platform_device *pdev)
2700{
2701 int i;
2702 int ret;
2703 uint32_t num_ss;
2704 struct smd_platform *smd_platform_data;
2705 struct smd_subsystem_config *smd_ss_config_list;
2706 struct smd_subsystem_config *cfg;
2707 int err_ret = 0;
2708
2709 smd_platform_data = pdev->dev.platform_data;
2710 num_ss = smd_platform_data->num_ss_configs;
2711 smd_ss_config_list = smd_platform_data->smd_ss_configs;
2712
2713 for (i = 0; i < num_ss; i++) {
2714 cfg = &smd_ss_config_list[i];
2715
2716 ret = intr_init(
2717 &private_intr_config[cfg->irq_config_id].smd,
2718 &cfg->smd_int,
2719 pdev
2720 );
2721
2722 if (ret < 0) {
2723 err_ret = ret;
2724 pr_err("smd: register irq failed on %s\n",
2725 cfg->smd_int.irq_name);
2726 break;
2727 }
2728
2729 ret = intr_init(
2730 &private_intr_config[cfg->irq_config_id].smsm,
2731 &cfg->smsm_int,
2732 pdev
2733 );
2734
2735 if (ret < 0) {
2736 err_ret = ret;
2737 pr_err("smd: register irq failed on %s\n",
2738 cfg->smsm_int.irq_name);
2739 break;
2740 }
2741 }
2742
2743 if (err_ret < 0) {
2744 pr_err("smd: deregistering IRQs\n");
2745 for (i = 0; i < num_ss; ++i) {
2746 cfg = &smd_ss_config_list[i];
2747
2748 if (cfg->smd_int.irq_id >= 0)
2749 free_irq(cfg->smd_int.irq_id,
2750 (void *)cfg->smd_int.dev_id
2751 );
2752 if (cfg->smsm_int.irq_id >= 0)
2753 free_irq(cfg->smsm_int.irq_id,
2754 (void *)cfg->smsm_int.dev_id
2755 );
2756 }
2757 return err_ret;
2758 }
2759
2760 SMD_INFO("smd_core_platform_init() done\n");
2761 return 0;
2762
2763}
2764
Gregory Bean4416e9e2010-07-28 10:22:12 -07002765static int __devinit msm_smd_probe(struct platform_device *pdev)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002766{
Angshuman Sarkarbad32df2012-02-01 19:52:52 +05302767 int ret;
Daniel Walker0aec66d2010-03-18 12:31:08 -07002768
Angshuman Sarkarbad32df2012-02-01 19:52:52 +05302769 SMD_INFO("smd probe\n");
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002770 INIT_WORK(&probe_work, smd_channel_probe_worker);
2771
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002772 channel_close_wq = create_singlethread_workqueue("smd_channel_close");
2773 if (IS_ERR(channel_close_wq)) {
2774 pr_err("%s: create_singlethread_workqueue ENOMEM\n", __func__);
2775 return -ENOMEM;
2776 }
2777
2778 if (smsm_init()) {
2779 pr_err("smsm_init() failed\n");
2780 return -1;
2781 }
2782
Angshuman Sarkarbad32df2012-02-01 19:52:52 +05302783 if (pdev) {
2784 if (pdev->dev.of_node) {
2785 pr_err("SMD: Device tree not currently supported\n");
2786 return -ENODEV;
2787 } else if (pdev->dev.platform_data) {
2788 ret = smd_core_platform_init(pdev);
2789 if (ret) {
2790 pr_err(
2791 "SMD: smd_core_platform_init() failed\n");
2792 return -ENODEV;
2793 }
2794 } else {
2795 ret = smd_core_init();
2796 if (ret) {
2797 pr_err("smd_core_init() failed\n");
2798 return -ENODEV;
2799 }
2800 }
2801 } else {
2802 pr_err("SMD: PDEV not found\n");
2803 return -ENODEV;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002804 }
2805
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002806 smd_initialized = 1;
2807
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002808 smd_alloc_loopback_channel();
Eric Holmbergc33d4ab2011-10-24 10:28:25 -06002809 smsm_irq_handler(0, 0);
2810 tasklet_schedule(&smd_fake_irq_tasklet);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002811
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002812 return 0;
2813}
2814
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002815static int restart_notifier_cb(struct notifier_block *this,
2816 unsigned long code,
2817 void *data);
2818
2819static struct restart_notifier_block restart_notifiers[] = {
Eric Holmbergca7ead22011-12-01 17:21:15 -07002820 {SMD_MODEM, "modem", .nb.notifier_call = restart_notifier_cb},
2821 {SMD_Q6, "lpass", .nb.notifier_call = restart_notifier_cb},
2822 {SMD_WCNSS, "riva", .nb.notifier_call = restart_notifier_cb},
2823 {SMD_DSPS, "dsps", .nb.notifier_call = restart_notifier_cb},
Eric Holmberg8b0e74f2012-02-08 09:56:17 -07002824 {SMD_MODEM, "gss", .nb.notifier_call = restart_notifier_cb},
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002825};
2826
2827static int restart_notifier_cb(struct notifier_block *this,
2828 unsigned long code,
2829 void *data)
2830{
2831 if (code == SUBSYS_AFTER_SHUTDOWN) {
2832 struct restart_notifier_block *notifier;
2833
2834 notifier = container_of(this,
2835 struct restart_notifier_block, nb);
2836 SMD_INFO("%s: ssrestart for processor %d ('%s')\n",
2837 __func__, notifier->processor,
2838 notifier->name);
2839
2840 smd_channel_reset(notifier->processor);
2841 }
2842
2843 return NOTIFY_DONE;
2844}
2845
2846static __init int modem_restart_late_init(void)
2847{
2848 int i;
2849 void *handle;
2850 struct restart_notifier_block *nb;
2851
2852 for (i = 0; i < ARRAY_SIZE(restart_notifiers); i++) {
2853 nb = &restart_notifiers[i];
2854 handle = subsys_notif_register_notifier(nb->name, &nb->nb);
2855 SMD_DBG("%s: registering notif for '%s', handle=%p\n",
2856 __func__, nb->name, handle);
2857 }
2858 return 0;
2859}
2860late_initcall(modem_restart_late_init);
2861
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002862static struct platform_driver msm_smd_driver = {
2863 .probe = msm_smd_probe,
2864 .driver = {
2865 .name = MODULE_NAME,
2866 .owner = THIS_MODULE,
2867 },
2868};
2869
2870static int __init msm_smd_init(void)
2871{
2872 return platform_driver_register(&msm_smd_driver);
2873}
2874
2875module_init(msm_smd_init);
2876
2877MODULE_DESCRIPTION("MSM Shared Memory Core");
2878MODULE_AUTHOR("Brian Swetland <swetland@google.com>");
2879MODULE_LICENSE("GPL");