blob: 3590e6b4884a3259d1649bec29f1593c733936c9 [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 Holmberg6275b602012-11-19 13:05:04 -07004 * Copyright (c) 2008-2013, The Linux Foundation. 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>
Eric Holmberg59a9f9412012-03-19 10:04:22 -060035#include <linux/wakelock.h>
Karthikeyan Ramasubramanian7069c482012-03-22 09:21:20 -060036#include <linux/notifier.h>
Eric Holmberg144c2de2012-10-04 13:37:28 -060037#include <linux/suspend.h>
Jeff Hugo412356e2012-09-27 17:14:23 -060038#include <linux/of.h>
39#include <linux/of_irq.h>
Seemanta Dutta4e2d49c2013-04-05 16:28:11 -070040
Brian Swetland2eb44eb2008-09-29 16:00:48 -070041#include <mach/msm_smd.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070042#include <mach/msm_iomap.h>
Brian Swetland2eb44eb2008-09-29 16:00:48 -070043#include <mach/system.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070044#include <mach/subsystem_notif.h>
Angshuman Sarkaread67bd2011-09-21 20:13:12 +053045#include <mach/socinfo.h>
Steve Mucklef132c6c2012-06-06 18:30:57 -070046#include <mach/proc_comm.h>
Arun Kumar Neelakantam7cffb332013-01-28 15:43:35 +053047#include <mach/msm_ipc_logging.h>
Seemanta Dutta4e2d49c2013-04-05 16:28:11 -070048#include <mach/ramdump.h>
Eric Holmberg51edef72013-04-11 14:28:33 -060049#include <mach/board.h>
Seemanta Dutta4e2d49c2013-04-05 16:28:11 -070050
Ram Somani8b9589f2012-04-03 12:07:18 +053051#include <asm/cacheflush.h>
Brian Swetland2eb44eb2008-09-29 16:00:48 -070052
53#include "smd_private.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070054#include "modem_notifier.h"
Brian Swetland2eb44eb2008-09-29 16:00:48 -070055
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070056#if defined(CONFIG_ARCH_QSD8X50) || defined(CONFIG_ARCH_MSM8X60) \
Jeff Hugo56b933a2011-09-28 14:42:05 -060057 || defined(CONFIG_ARCH_MSM8960) || defined(CONFIG_ARCH_FSM9XXX) \
Jeff Hugo0c0f5e92011-09-28 13:55:45 -060058 || defined(CONFIG_ARCH_MSM9615) || defined(CONFIG_ARCH_APQ8064)
Brian Swetland37521a32009-07-01 18:30:47 -070059#define CONFIG_QDSP6 1
60#endif
61
Jeff Hugo0c0f5e92011-09-28 13:55:45 -060062#if defined(CONFIG_ARCH_MSM8X60) || defined(CONFIG_ARCH_MSM8960) \
63 || defined(CONFIG_ARCH_APQ8064)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070064#define CONFIG_DSPS 1
65#endif
66
Jeff Hugo0c0f5e92011-09-28 13:55:45 -060067#if defined(CONFIG_ARCH_MSM8960) \
68 || defined(CONFIG_ARCH_APQ8064)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070069#define CONFIG_WCNSS 1
Jeff Hugo6a8057c2011-08-16 13:47:12 -060070#define CONFIG_DSPS_SMSM 1
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070071#endif
Brian Swetland2eb44eb2008-09-29 16:00:48 -070072
73#define MODULE_NAME "msm_smd"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070074#define SMEM_VERSION 0x000B
75#define SMD_VERSION 0x00020000
Eric Holmbergc7e8daf2011-12-28 11:49:21 -070076#define SMSM_SNAPSHOT_CNT 64
Eric Holmbergda31d042012-03-28 14:01:02 -060077#define SMSM_SNAPSHOT_SIZE ((SMSM_NUM_ENTRIES + 1) * 4)
Eric Holmberge5266d32013-02-25 18:29:27 -070078#define RSPIN_INIT_WAIT_MS 1000
Eric Holmberg424d9552013-04-05 15:23:25 -060079#define SMD_FIFO_FULL_RESERVE 4
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070080
81uint32_t SMSM_NUM_ENTRIES = 8;
82uint32_t SMSM_NUM_HOSTS = 3;
Brian Swetland2eb44eb2008-09-29 16:00:48 -070083
Eric Holmberge8a39322012-04-03 15:14:02 -060084/* Legacy SMSM interrupt notifications */
85#define LEGACY_MODEM_SMSM_MASK (SMSM_RESET | SMSM_INIT | SMSM_SMDINIT \
86 | SMSM_RUN | SMSM_SYSTEM_DOWNLOAD)
Brian Swetland2eb44eb2008-09-29 16:00:48 -070087
88enum {
89 MSM_SMD_DEBUG = 1U << 0,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070090 MSM_SMSM_DEBUG = 1U << 1,
91 MSM_SMD_INFO = 1U << 2,
92 MSM_SMSM_INFO = 1U << 3,
Eric Holmberg98c6c642012-02-24 11:29:35 -070093 MSM_SMx_POWER_INFO = 1U << 4,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070094};
95
96struct smsm_shared_info {
97 uint32_t *state;
98 uint32_t *intr_mask;
99 uint32_t *intr_mux;
100};
101
102static struct smsm_shared_info smsm_info;
Eric Holmberg59a9f9412012-03-19 10:04:22 -0600103static struct kfifo smsm_snapshot_fifo;
104static struct wake_lock smsm_snapshot_wakelock;
105static int smsm_snapshot_count;
106static DEFINE_SPINLOCK(smsm_snapshot_count_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700107
108struct smsm_size_info_type {
109 uint32_t num_hosts;
110 uint32_t num_entries;
111 uint32_t reserved0;
112 uint32_t reserved1;
113};
114
115struct smsm_state_cb_info {
116 struct list_head cb_list;
117 uint32_t mask;
118 void *data;
119 void (*notify)(void *data, uint32_t old_state, uint32_t new_state);
120};
121
122struct smsm_state_info {
123 struct list_head callbacks;
124 uint32_t last_value;
Eric Holmberge8a39322012-04-03 15:14:02 -0600125 uint32_t intr_mask_set;
126 uint32_t intr_mask_clear;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700127};
128
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530129struct interrupt_config_item {
130 /* must be initialized */
131 irqreturn_t (*irq_handler)(int req, void *data);
132 /* outgoing interrupt config (set from platform data) */
133 uint32_t out_bit_pos;
134 void __iomem *out_base;
135 uint32_t out_offset;
Eric Holmbergdeace152012-07-25 12:17:11 -0600136 int irq_id;
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530137};
138
139struct interrupt_config {
140 struct interrupt_config_item smd;
141 struct interrupt_config_item smsm;
142};
143
144static irqreturn_t smd_modem_irq_handler(int irq, void *data);
Eric Holmberg98c6c642012-02-24 11:29:35 -0700145static irqreturn_t smsm_modem_irq_handler(int irq, void *data);
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530146static irqreturn_t smd_dsp_irq_handler(int irq, void *data);
Eric Holmberg98c6c642012-02-24 11:29:35 -0700147static irqreturn_t smsm_dsp_irq_handler(int irq, void *data);
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530148static irqreturn_t smd_dsps_irq_handler(int irq, void *data);
Eric Holmberg98c6c642012-02-24 11:29:35 -0700149static irqreturn_t smsm_dsps_irq_handler(int irq, void *data);
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530150static irqreturn_t smd_wcnss_irq_handler(int irq, void *data);
Eric Holmberg98c6c642012-02-24 11:29:35 -0700151static irqreturn_t smsm_wcnss_irq_handler(int irq, void *data);
Jeff Hugo918b2dc2012-03-21 13:42:09 -0600152static irqreturn_t smd_rpm_irq_handler(int irq, void *data);
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530153static irqreturn_t smsm_irq_handler(int irq, void *data);
154
155static struct interrupt_config private_intr_config[NUM_SMD_SUBSYSTEMS] = {
156 [SMD_MODEM] = {
157 .smd.irq_handler = smd_modem_irq_handler,
Eric Holmberg98c6c642012-02-24 11:29:35 -0700158 .smsm.irq_handler = smsm_modem_irq_handler,
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530159 },
160 [SMD_Q6] = {
161 .smd.irq_handler = smd_dsp_irq_handler,
Eric Holmberg98c6c642012-02-24 11:29:35 -0700162 .smsm.irq_handler = smsm_dsp_irq_handler,
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530163 },
164 [SMD_DSPS] = {
165 .smd.irq_handler = smd_dsps_irq_handler,
Eric Holmberg98c6c642012-02-24 11:29:35 -0700166 .smsm.irq_handler = smsm_dsps_irq_handler,
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530167 },
168 [SMD_WCNSS] = {
169 .smd.irq_handler = smd_wcnss_irq_handler,
Eric Holmberg98c6c642012-02-24 11:29:35 -0700170 .smsm.irq_handler = smsm_wcnss_irq_handler,
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530171 },
Jeff Hugo918b2dc2012-03-21 13:42:09 -0600172 [SMD_RPM] = {
173 .smd.irq_handler = smd_rpm_irq_handler,
174 .smsm.irq_handler = NULL, /* does not support smsm */
175 },
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530176};
Jeff Hugobdc734d2012-03-26 16:05:39 -0600177
178struct smem_area {
Stepan Moskovchenkod6ee8262013-02-06 11:26:05 -0800179 phys_addr_t phys_addr;
180 resource_size_t size;
Jeff Hugobdc734d2012-03-26 16:05:39 -0600181 void __iomem *virt_addr;
182};
183static uint32_t num_smem_areas;
184static struct smem_area *smem_areas;
Eric Holmbergcfbc1d52013-03-13 18:30:19 -0600185static struct ramdump_segment *smem_ramdump_segments;
186static void *smem_ramdump_dev;
Eric Holmberg51edef72013-04-11 14:28:33 -0600187static void *smem_phys_to_virt(phys_addr_t base, unsigned offset);
Eric Holmbergcfbc1d52013-03-13 18:30:19 -0600188static void *smd_dev;
Jeff Hugobdc734d2012-03-26 16:05:39 -0600189
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700190struct interrupt_stat interrupt_stats[NUM_SMD_SUBSYSTEMS];
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530191
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700192#define SMSM_STATE_ADDR(entry) (smsm_info.state + entry)
193#define SMSM_INTR_MASK_ADDR(entry, host) (smsm_info.intr_mask + \
194 entry * SMSM_NUM_HOSTS + host)
195#define SMSM_INTR_MUX_ADDR(entry) (smsm_info.intr_mux + entry)
196
197/* Internal definitions which are not exported in some targets */
198enum {
199 SMSM_APPS_DEM_I = 3,
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700200};
201
Arun Kumar Neelakantam7cffb332013-01-28 15:43:35 +0530202static int msm_smd_debug_mask = MSM_SMx_POWER_INFO;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700203module_param_named(debug_mask, msm_smd_debug_mask,
204 int, S_IRUGO | S_IWUSR | S_IWGRP);
Arun Kumar Neelakantam7cffb332013-01-28 15:43:35 +0530205static void *smd_log_ctx;
206#define NUM_LOG_PAGES 4
207
208#define IPC_LOG(level, x...) do { \
209 if (smd_log_ctx) \
210 ipc_log_string(smd_log_ctx, x); \
211 else \
212 printk(level x); \
213 } while (0)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700214
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700215#if defined(CONFIG_MSM_SMD_DEBUG)
216#define SMD_DBG(x...) do { \
217 if (msm_smd_debug_mask & MSM_SMD_DEBUG) \
Arun Kumar Neelakantam7cffb332013-01-28 15:43:35 +0530218 IPC_LOG(KERN_DEBUG, x); \
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700219 } while (0)
220
221#define SMSM_DBG(x...) do { \
222 if (msm_smd_debug_mask & MSM_SMSM_DEBUG) \
Arun Kumar Neelakantam7cffb332013-01-28 15:43:35 +0530223 IPC_LOG(KERN_DEBUG, x); \
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700224 } while (0)
225
226#define SMD_INFO(x...) do { \
227 if (msm_smd_debug_mask & MSM_SMD_INFO) \
Arun Kumar Neelakantam7cffb332013-01-28 15:43:35 +0530228 IPC_LOG(KERN_INFO, x); \
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700229 } while (0)
230
231#define SMSM_INFO(x...) do { \
232 if (msm_smd_debug_mask & MSM_SMSM_INFO) \
Arun Kumar Neelakantam7cffb332013-01-28 15:43:35 +0530233 IPC_LOG(KERN_INFO, x); \
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700234 } while (0)
Eric Holmberg98c6c642012-02-24 11:29:35 -0700235#define SMx_POWER_INFO(x...) do { \
236 if (msm_smd_debug_mask & MSM_SMx_POWER_INFO) \
Arun Kumar Neelakantam7cffb332013-01-28 15:43:35 +0530237 IPC_LOG(KERN_INFO, x); \
Eric Holmberg98c6c642012-02-24 11:29:35 -0700238 } while (0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700239#else
240#define SMD_DBG(x...) do { } while (0)
241#define SMSM_DBG(x...) do { } while (0)
242#define SMD_INFO(x...) do { } while (0)
243#define SMSM_INFO(x...) do { } while (0)
Eric Holmberg98c6c642012-02-24 11:29:35 -0700244#define SMx_POWER_INFO(x...) do { } while (0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700245#endif
246
Eric Holmberg51edef72013-04-11 14:28:33 -0600247/**
248 * OVERFLOW_ADD_UNSIGNED() - check for unsigned overflow
249 *
250 * @type: type to check for overflow
251 * @a: left value to use
252 * @b: right value to use
253 * @returns: true if a + b will result in overflow; false otherwise
254 */
255#define OVERFLOW_ADD_UNSIGNED(type, a, b) \
256 (((type)~0 - (a)) < (b) ? true : false)
257
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700258static unsigned last_heap_free = 0xffffffff;
259
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700260static inline void smd_write_intr(unsigned int val,
261 const void __iomem *addr);
262
263#if defined(CONFIG_ARCH_MSM7X30)
264#define MSM_TRIG_A2M_SMD_INT \
Taniya Das298de8c2012-02-16 11:45:31 +0530265 (smd_write_intr(1 << 0, MSM_APCS_GCC_BASE + 0x8))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700266#define MSM_TRIG_A2Q6_SMD_INT \
Taniya Das298de8c2012-02-16 11:45:31 +0530267 (smd_write_intr(1 << 8, MSM_APCS_GCC_BASE + 0x8))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700268#define MSM_TRIG_A2M_SMSM_INT \
Taniya Das298de8c2012-02-16 11:45:31 +0530269 (smd_write_intr(1 << 5, MSM_APCS_GCC_BASE + 0x8))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700270#define MSM_TRIG_A2Q6_SMSM_INT \
Taniya Das298de8c2012-02-16 11:45:31 +0530271 (smd_write_intr(1 << 8, MSM_APCS_GCC_BASE + 0x8))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700272#define MSM_TRIG_A2DSPS_SMD_INT
Jeff Hugo6a8057c2011-08-16 13:47:12 -0600273#define MSM_TRIG_A2DSPS_SMSM_INT
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700274#define MSM_TRIG_A2WCNSS_SMD_INT
275#define MSM_TRIG_A2WCNSS_SMSM_INT
276#elif defined(CONFIG_ARCH_MSM8X60)
277#define MSM_TRIG_A2M_SMD_INT \
278 (smd_write_intr(1 << 3, MSM_GCC_BASE + 0x8))
279#define MSM_TRIG_A2Q6_SMD_INT \
280 (smd_write_intr(1 << 15, MSM_GCC_BASE + 0x8))
281#define MSM_TRIG_A2M_SMSM_INT \
282 (smd_write_intr(1 << 4, MSM_GCC_BASE + 0x8))
283#define MSM_TRIG_A2Q6_SMSM_INT \
284 (smd_write_intr(1 << 14, MSM_GCC_BASE + 0x8))
285#define MSM_TRIG_A2DSPS_SMD_INT \
286 (smd_write_intr(1, MSM_SIC_NON_SECURE_BASE + 0x4080))
Jeff Hugo6a8057c2011-08-16 13:47:12 -0600287#define MSM_TRIG_A2DSPS_SMSM_INT
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700288#define MSM_TRIG_A2WCNSS_SMD_INT
289#define MSM_TRIG_A2WCNSS_SMSM_INT
Jeff Hugo56b933a2011-09-28 14:42:05 -0600290#elif defined(CONFIG_ARCH_MSM9615)
291#define MSM_TRIG_A2M_SMD_INT \
292 (smd_write_intr(1 << 3, MSM_APCS_GCC_BASE + 0x8))
293#define MSM_TRIG_A2Q6_SMD_INT \
294 (smd_write_intr(1 << 15, MSM_APCS_GCC_BASE + 0x8))
295#define MSM_TRIG_A2M_SMSM_INT \
296 (smd_write_intr(1 << 4, MSM_APCS_GCC_BASE + 0x8))
297#define MSM_TRIG_A2Q6_SMSM_INT \
298 (smd_write_intr(1 << 14, MSM_APCS_GCC_BASE + 0x8))
299#define MSM_TRIG_A2DSPS_SMD_INT
300#define MSM_TRIG_A2DSPS_SMSM_INT
301#define MSM_TRIG_A2WCNSS_SMD_INT
302#define MSM_TRIG_A2WCNSS_SMSM_INT
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700303#elif defined(CONFIG_ARCH_FSM9XXX)
304#define MSM_TRIG_A2Q6_SMD_INT \
305 (smd_write_intr(1 << 10, MSM_GCC_BASE + 0x8))
306#define MSM_TRIG_A2Q6_SMSM_INT \
307 (smd_write_intr(1 << 10, MSM_GCC_BASE + 0x8))
308#define MSM_TRIG_A2M_SMD_INT \
309 (smd_write_intr(1 << 0, MSM_GCC_BASE + 0x8))
310#define MSM_TRIG_A2M_SMSM_INT \
311 (smd_write_intr(1 << 5, MSM_GCC_BASE + 0x8))
312#define MSM_TRIG_A2DSPS_SMD_INT
Jeff Hugo6a8057c2011-08-16 13:47:12 -0600313#define MSM_TRIG_A2DSPS_SMSM_INT
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700314#define MSM_TRIG_A2WCNSS_SMD_INT
315#define MSM_TRIG_A2WCNSS_SMSM_INT
Eric Holmberg73d45462012-02-28 10:41:31 -0700316#elif defined(CONFIG_ARCH_MSM7X01A) || defined(CONFIG_ARCH_MSM7x25)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700317#define MSM_TRIG_A2M_SMD_INT \
318 (smd_write_intr(1, MSM_CSR_BASE + 0x400 + (0) * 4))
Eric Holmberg73d45462012-02-28 10:41:31 -0700319#define MSM_TRIG_A2Q6_SMD_INT
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700320#define MSM_TRIG_A2M_SMSM_INT \
321 (smd_write_intr(1, MSM_CSR_BASE + 0x400 + (5) * 4))
Eric Holmberg73d45462012-02-28 10:41:31 -0700322#define MSM_TRIG_A2Q6_SMSM_INT
323#define MSM_TRIG_A2DSPS_SMD_INT
324#define MSM_TRIG_A2DSPS_SMSM_INT
325#define MSM_TRIG_A2WCNSS_SMD_INT
326#define MSM_TRIG_A2WCNSS_SMSM_INT
327#elif defined(CONFIG_ARCH_MSM7X27) || defined(CONFIG_ARCH_MSM7X27A)
328#define MSM_TRIG_A2M_SMD_INT \
329 (smd_write_intr(1, MSM_CSR_BASE + 0x400 + (0) * 4))
330#define MSM_TRIG_A2Q6_SMD_INT
331#define MSM_TRIG_A2M_SMSM_INT \
332 (smd_write_intr(1, MSM_CSR_BASE + 0x400 + (5) * 4))
333#define MSM_TRIG_A2Q6_SMSM_INT
334#define MSM_TRIG_A2DSPS_SMD_INT
335#define MSM_TRIG_A2DSPS_SMSM_INT
336#define MSM_TRIG_A2WCNSS_SMD_INT
337#define MSM_TRIG_A2WCNSS_SMSM_INT
338#else /* use platform device / device tree configuration */
339#define MSM_TRIG_A2M_SMD_INT
340#define MSM_TRIG_A2Q6_SMD_INT
341#define MSM_TRIG_A2M_SMSM_INT
342#define MSM_TRIG_A2Q6_SMSM_INT
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700343#define MSM_TRIG_A2DSPS_SMD_INT
Jeff Hugo6a8057c2011-08-16 13:47:12 -0600344#define MSM_TRIG_A2DSPS_SMSM_INT
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700345#define MSM_TRIG_A2WCNSS_SMD_INT
346#define MSM_TRIG_A2WCNSS_SMSM_INT
Brian Swetland37521a32009-07-01 18:30:47 -0700347#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700348
Jeff Hugoee40b152012-02-09 17:39:47 -0700349/*
350 * stub out legacy macros if they are not being used so that the legacy
351 * code compiles even though it is not used
352 *
353 * these definitions should not be used in active code and will cause
354 * an early failure
355 */
356#ifndef INT_A9_M2A_0
357#define INT_A9_M2A_0 -1
358#endif
359#ifndef INT_A9_M2A_5
360#define INT_A9_M2A_5 -1
361#endif
362#ifndef INT_ADSP_A11
363#define INT_ADSP_A11 -1
364#endif
365#ifndef INT_ADSP_A11_SMSM
366#define INT_ADSP_A11_SMSM -1
367#endif
368#ifndef INT_DSPS_A11
369#define INT_DSPS_A11 -1
370#endif
371#ifndef INT_DSPS_A11_SMSM
372#define INT_DSPS_A11_SMSM -1
373#endif
374#ifndef INT_WCNSS_A11
375#define INT_WCNSS_A11 -1
376#endif
377#ifndef INT_WCNSS_A11_SMSM
378#define INT_WCNSS_A11_SMSM -1
379#endif
380
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700381#define SMD_LOOPBACK_CID 100
382
Eric Holmbergf6d7d1a2011-09-23 18:31:04 -0600383#define SMEM_SPINLOCK_SMEM_ALLOC "S:3"
384static remote_spinlock_t remote_spinlock;
385
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700386static LIST_HEAD(smd_ch_list_loopback);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700387static void smd_fake_irq_handler(unsigned long arg);
Eric Holmbergda31d042012-03-28 14:01:02 -0600388static void smsm_cb_snapshot(uint32_t use_wakelock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700389
Karthikeyan Ramasubramanian3249a222012-04-18 17:16:49 -0600390static struct workqueue_struct *smsm_cb_wq;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700391static void notify_smsm_cb_clients_worker(struct work_struct *work);
392static DECLARE_WORK(smsm_cb_work, notify_smsm_cb_clients_worker);
Eric Holmbergc8002902011-09-16 13:55:57 -0600393static DEFINE_MUTEX(smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700394static struct smsm_state_info *smsm_states;
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +0530395static int spinlocks_initialized;
Karthikeyan Ramasubramanianfa44cd72012-08-22 18:08:14 -0600396
397/**
398 * Variables to indicate smd module initialization.
399 * Dependents to register for smd module init notifier.
400 */
401static int smd_module_inited;
402static RAW_NOTIFIER_HEAD(smd_module_init_notifier_list);
403static DEFINE_MUTEX(smd_module_init_notifier_lock);
404static void smd_module_init_notify(uint32_t state, void *data);
Arun Kumar Neelakantam7cffb332013-01-28 15:43:35 +0530405static int smd_stream_write_avail(struct smd_channel *ch);
406static int smd_stream_read_avail(struct smd_channel *ch);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700407
408static inline void smd_write_intr(unsigned int val,
409 const void __iomem *addr)
410{
411 wmb();
412 __raw_writel(val, addr);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700413}
414
Arun Kumar Neelakantam7cffb332013-01-28 15:43:35 +0530415static inline void log_notify(uint32_t subsystem, smd_channel_t *ch)
416{
417 const char *subsys = smd_edge_to_subsystem(subsystem);
418
Jay Chokshi83b4f6132013-02-14 16:20:56 -0800419 (void) subsys;
420
Arun Kumar Neelakantam7cffb332013-01-28 15:43:35 +0530421 if (!ch)
422 SMx_POWER_INFO("Apps->%s\n", subsys);
423 else
424 SMx_POWER_INFO(
425 "Apps->%s ch%d '%s': tx%d/rx%d %dr/%dw : %dr/%dw\n",
426 subsys, ch->n, ch->name,
427 ch->fifo_size -
428 (smd_stream_write_avail(ch) + 1),
429 smd_stream_read_avail(ch),
430 ch->half_ch->get_tail(ch->send),
431 ch->half_ch->get_head(ch->send),
432 ch->half_ch->get_tail(ch->recv),
433 ch->half_ch->get_head(ch->recv)
434 );
435}
436
437static inline void notify_modem_smd(smd_channel_t *ch)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700438{
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530439 static const struct interrupt_config_item *intr
440 = &private_intr_config[SMD_MODEM].smd;
Arun Kumar Neelakantam7cffb332013-01-28 15:43:35 +0530441
442 log_notify(SMD_APPS_MODEM, ch);
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700443 if (intr->out_base) {
444 ++interrupt_stats[SMD_MODEM].smd_out_config_count;
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530445 smd_write_intr(intr->out_bit_pos,
446 intr->out_base + intr->out_offset);
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700447 } else {
448 ++interrupt_stats[SMD_MODEM].smd_out_hardcode_count;
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530449 MSM_TRIG_A2M_SMD_INT;
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700450 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700451}
452
Arun Kumar Neelakantam7cffb332013-01-28 15:43:35 +0530453static inline void notify_dsp_smd(smd_channel_t *ch)
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700454{
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530455 static const struct interrupt_config_item *intr
456 = &private_intr_config[SMD_Q6].smd;
Arun Kumar Neelakantam7cffb332013-01-28 15:43:35 +0530457
458 log_notify(SMD_APPS_QDSP, ch);
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700459 if (intr->out_base) {
460 ++interrupt_stats[SMD_Q6].smd_out_config_count;
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530461 smd_write_intr(intr->out_bit_pos,
462 intr->out_base + intr->out_offset);
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700463 } else {
464 ++interrupt_stats[SMD_Q6].smd_out_hardcode_count;
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530465 MSM_TRIG_A2Q6_SMD_INT;
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700466 }
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700467}
468
Arun Kumar Neelakantam7cffb332013-01-28 15:43:35 +0530469static inline void notify_dsps_smd(smd_channel_t *ch)
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530470{
471 static const struct interrupt_config_item *intr
472 = &private_intr_config[SMD_DSPS].smd;
Arun Kumar Neelakantam7cffb332013-01-28 15:43:35 +0530473
474 log_notify(SMD_APPS_DSPS, ch);
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700475 if (intr->out_base) {
476 ++interrupt_stats[SMD_DSPS].smd_out_config_count;
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530477 smd_write_intr(intr->out_bit_pos,
478 intr->out_base + intr->out_offset);
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700479 } else {
480 ++interrupt_stats[SMD_DSPS].smd_out_hardcode_count;
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530481 MSM_TRIG_A2DSPS_SMD_INT;
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700482 }
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530483}
484
Arun Kumar Neelakantam7cffb332013-01-28 15:43:35 +0530485static inline void notify_wcnss_smd(struct smd_channel *ch)
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530486{
487 static const struct interrupt_config_item *intr
488 = &private_intr_config[SMD_WCNSS].smd;
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530489
Arun Kumar Neelakantam7cffb332013-01-28 15:43:35 +0530490 log_notify(SMD_APPS_WCNSS, ch);
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700491 if (intr->out_base) {
492 ++interrupt_stats[SMD_WCNSS].smd_out_config_count;
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530493 smd_write_intr(intr->out_bit_pos,
494 intr->out_base + intr->out_offset);
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700495 } else {
496 ++interrupt_stats[SMD_WCNSS].smd_out_hardcode_count;
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530497 MSM_TRIG_A2WCNSS_SMD_INT;
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700498 }
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530499}
500
Arun Kumar Neelakantam7cffb332013-01-28 15:43:35 +0530501static inline void notify_rpm_smd(smd_channel_t *ch)
Jeff Hugo918b2dc2012-03-21 13:42:09 -0600502{
503 static const struct interrupt_config_item *intr
504 = &private_intr_config[SMD_RPM].smd;
505
506 if (intr->out_base) {
Arun Kumar Neelakantam7cffb332013-01-28 15:43:35 +0530507 log_notify(SMD_APPS_RPM, ch);
Jeff Hugo918b2dc2012-03-21 13:42:09 -0600508 ++interrupt_stats[SMD_RPM].smd_out_config_count;
509 smd_write_intr(intr->out_bit_pos,
510 intr->out_base + intr->out_offset);
511 }
512}
513
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530514static inline void notify_modem_smsm(void)
515{
516 static const struct interrupt_config_item *intr
517 = &private_intr_config[SMD_MODEM].smsm;
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700518 if (intr->out_base) {
519 ++interrupt_stats[SMD_MODEM].smsm_out_config_count;
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530520 smd_write_intr(intr->out_bit_pos,
521 intr->out_base + intr->out_offset);
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700522 } else {
523 ++interrupt_stats[SMD_MODEM].smsm_out_hardcode_count;
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530524 MSM_TRIG_A2M_SMSM_INT;
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700525 }
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530526}
527
528static inline void notify_dsp_smsm(void)
529{
530 static const struct interrupt_config_item *intr
531 = &private_intr_config[SMD_Q6].smsm;
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700532 if (intr->out_base) {
533 ++interrupt_stats[SMD_Q6].smsm_out_config_count;
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530534 smd_write_intr(intr->out_bit_pos,
535 intr->out_base + intr->out_offset);
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700536 } else {
537 ++interrupt_stats[SMD_Q6].smsm_out_hardcode_count;
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530538 MSM_TRIG_A2Q6_SMSM_INT;
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700539 }
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530540}
541
542static inline void notify_dsps_smsm(void)
543{
544 static const struct interrupt_config_item *intr
545 = &private_intr_config[SMD_DSPS].smsm;
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700546 if (intr->out_base) {
547 ++interrupt_stats[SMD_DSPS].smsm_out_config_count;
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530548 smd_write_intr(intr->out_bit_pos,
549 intr->out_base + intr->out_offset);
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700550 } else {
551 ++interrupt_stats[SMD_DSPS].smsm_out_hardcode_count;
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530552 MSM_TRIG_A2DSPS_SMSM_INT;
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700553 }
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530554}
555
556static inline void notify_wcnss_smsm(void)
557{
558 static const struct interrupt_config_item *intr
559 = &private_intr_config[SMD_WCNSS].smsm;
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530560
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700561 if (intr->out_base) {
562 ++interrupt_stats[SMD_WCNSS].smsm_out_config_count;
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530563 smd_write_intr(intr->out_bit_pos,
564 intr->out_base + intr->out_offset);
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700565 } else {
566 ++interrupt_stats[SMD_WCNSS].smsm_out_hardcode_count;
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530567 MSM_TRIG_A2WCNSS_SMSM_INT;
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700568 }
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530569}
570
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700571static void notify_other_smsm(uint32_t smsm_entry, uint32_t notify_mask)
572{
573 /* older protocol don't use smsm_intr_mask,
574 but still communicates with modem */
575 if (!smsm_info.intr_mask ||
576 (__raw_readl(SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_MODEM))
577 & notify_mask))
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530578 notify_modem_smsm();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700579
580 if (smsm_info.intr_mask &&
581 (__raw_readl(SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_Q6))
582 & notify_mask)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700583 uint32_t mux_val;
584
Eric Holmberg6282c5d2011-10-27 17:30:57 -0600585 if (cpu_is_qsd8x50() && smsm_info.intr_mux) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700586 mux_val = __raw_readl(
587 SMSM_INTR_MUX_ADDR(SMEM_APPS_Q6_SMSM));
588 mux_val++;
589 __raw_writel(mux_val,
590 SMSM_INTR_MUX_ADDR(SMEM_APPS_Q6_SMSM));
591 }
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530592 notify_dsp_smsm();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700593 }
594
595 if (smsm_info.intr_mask &&
596 (__raw_readl(SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_WCNSS))
597 & notify_mask)) {
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530598 notify_wcnss_smsm();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700599 }
600
Jeff Hugo6a8057c2011-08-16 13:47:12 -0600601 if (smsm_info.intr_mask &&
602 (__raw_readl(SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_DSPS))
603 & notify_mask)) {
Angshuman Sarkarbad32df2012-02-01 19:52:52 +0530604 notify_dsps_smsm();
Jeff Hugo6a8057c2011-08-16 13:47:12 -0600605 }
606
Eric Holmbergda31d042012-03-28 14:01:02 -0600607 /*
608 * Notify local SMSM callback clients without wakelock since this
609 * code is used by power management during power-down/-up sequencing
610 * on DEM-based targets. Grabbing a wakelock in this case will
611 * abort the power-down sequencing.
612 */
Eric Holmberg51676a12012-07-10 18:45:23 -0600613 if (smsm_info.intr_mask &&
614 (__raw_readl(SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_APPS))
615 & notify_mask)) {
616 smsm_cb_snapshot(0);
617 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700618}
619
Eric Holmberg144c2de2012-10-04 13:37:28 -0600620static int smsm_pm_notifier(struct notifier_block *nb,
621 unsigned long event, void *unused)
622{
623 switch (event) {
624 case PM_SUSPEND_PREPARE:
625 smsm_change_state(SMSM_APPS_STATE, SMSM_PROC_AWAKE, 0);
626 break;
627
628 case PM_POST_SUSPEND:
629 smsm_change_state(SMSM_APPS_STATE, 0, SMSM_PROC_AWAKE);
630 break;
631 }
632 return NOTIFY_DONE;
633}
634
635static struct notifier_block smsm_pm_nb = {
636 .notifier_call = smsm_pm_notifier,
637 .priority = 0,
638};
639
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700640void smd_diag(void)
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700641{
642 char *x;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700643 int size;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700644
645 x = smem_find(ID_DIAG_ERR_MSG, SZ_DIAG_ERR_MSG);
646 if (x != 0) {
647 x[SZ_DIAG_ERR_MSG - 1] = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700648 SMD_INFO("smem: DIAG '%s'\n", x);
649 }
650
651 x = smem_get_entry(SMEM_ERR_CRASH_LOG, &size);
652 if (x != 0) {
653 x[size - 1] = 0;
654 pr_err("smem: CRASH LOG\n'%s'\n", x);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700655 }
656}
657
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700658
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700659static void handle_modem_crash(void)
660{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700661 pr_err("MODEM/AMSS has CRASHED\n");
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700662 smd_diag();
663
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700664 /* hard reboot if possible FIXME
665 if (msm_reset_hook)
666 msm_reset_hook();
667 */
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700668
669 /* in this case the modem or watchdog should reboot us */
670 for (;;)
671 ;
672}
673
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700674int smsm_check_for_modem_crash(void)
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700675{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700676 /* if the modem's not ready yet, we have to hope for the best */
677 if (!smsm_info.state)
678 return 0;
Arve Hjønnevåg28379412009-05-20 16:52:36 -0700679
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700680 if (__raw_readl(SMSM_STATE_ADDR(SMSM_MODEM_STATE)) & SMSM_RESET) {
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700681 handle_modem_crash();
682 return -1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700683 }
Brian Swetland5b0f5a32009-04-26 18:38:49 -0700684 return 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700685}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700686EXPORT_SYMBOL(smsm_check_for_modem_crash);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700687
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700688/* the spinlock is used to synchronize between the
Brian Swetland03e00cd2009-07-01 17:58:37 -0700689 * irq handler and code that mutates the channel
690 * list or fiddles with channel state
691 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700692static DEFINE_SPINLOCK(smd_lock);
Brian Swetland03e00cd2009-07-01 17:58:37 -0700693DEFINE_SPINLOCK(smem_lock);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700694
695/* the mutex is used during open() and close()
Brian Swetland03e00cd2009-07-01 17:58:37 -0700696 * operations to avoid races while creating or
697 * destroying smd_channel structures
698 */
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700699static DEFINE_MUTEX(smd_creation_mutex);
700
701static int smd_initialized;
702
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700703struct smd_shared_v1 {
704 struct smd_half_channel ch0;
705 unsigned char data0[SMD_BUF_SIZE];
706 struct smd_half_channel ch1;
707 unsigned char data1[SMD_BUF_SIZE];
708};
709
710struct smd_shared_v2 {
711 struct smd_half_channel ch0;
712 struct smd_half_channel ch1;
713};
714
Jeff Hugo918b2dc2012-03-21 13:42:09 -0600715struct smd_shared_v2_word_access {
716 struct smd_half_channel_word_access ch0;
717 struct smd_half_channel_word_access ch1;
718};
719
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700720struct edge_to_pid {
721 uint32_t local_pid;
722 uint32_t remote_pid;
Eric Holmberg17992c12012-02-29 12:54:44 -0700723 char subsys_name[SMD_MAX_CH_NAME_LEN];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700724};
725
726/**
727 * Maps edge type to local and remote processor ID's.
728 */
729static struct edge_to_pid edge_to_pids[] = {
Eric Holmberg5a55b4a2012-03-01 14:41:54 -0700730 [SMD_APPS_MODEM] = {SMD_APPS, SMD_MODEM, "modem"},
Stephen Boyd77db8bb2012-06-27 15:15:16 -0700731 [SMD_APPS_QDSP] = {SMD_APPS, SMD_Q6, "adsp"},
Eric Holmberg5a55b4a2012-03-01 14:41:54 -0700732 [SMD_MODEM_QDSP] = {SMD_MODEM, SMD_Q6},
733 [SMD_APPS_DSPS] = {SMD_APPS, SMD_DSPS, "dsps"},
734 [SMD_MODEM_DSPS] = {SMD_MODEM, SMD_DSPS},
735 [SMD_QDSP_DSPS] = {SMD_Q6, SMD_DSPS},
736 [SMD_APPS_WCNSS] = {SMD_APPS, SMD_WCNSS, "wcnss"},
737 [SMD_MODEM_WCNSS] = {SMD_MODEM, SMD_WCNSS},
738 [SMD_QDSP_WCNSS] = {SMD_Q6, SMD_WCNSS},
739 [SMD_DSPS_WCNSS] = {SMD_DSPS, SMD_WCNSS},
740 [SMD_APPS_Q6FW] = {SMD_APPS, SMD_MODEM_Q6_FW},
741 [SMD_MODEM_Q6FW] = {SMD_MODEM, SMD_MODEM_Q6_FW},
742 [SMD_QDSP_Q6FW] = {SMD_Q6, SMD_MODEM_Q6_FW},
743 [SMD_DSPS_Q6FW] = {SMD_DSPS, SMD_MODEM_Q6_FW},
744 [SMD_WCNSS_Q6FW] = {SMD_WCNSS, SMD_MODEM_Q6_FW},
Jeff Hugo918b2dc2012-03-21 13:42:09 -0600745 [SMD_APPS_RPM] = {SMD_APPS, SMD_RPM},
746 [SMD_MODEM_RPM] = {SMD_MODEM, SMD_RPM},
747 [SMD_QDSP_RPM] = {SMD_Q6, SMD_RPM},
748 [SMD_WCNSS_RPM] = {SMD_WCNSS, SMD_RPM},
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700749};
750
751struct restart_notifier_block {
752 unsigned processor;
753 char *name;
754 struct notifier_block nb;
755};
756
Eric Holmberg2bb6ccd2012-03-13 13:05:14 -0600757static int disable_smsm_reset_handshake;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700758static struct platform_device loopback_tty_pdev = {.name = "LOOPBACK_TTY"};
759
760static LIST_HEAD(smd_ch_closed_list);
761static LIST_HEAD(smd_ch_closing_list);
762static LIST_HEAD(smd_ch_to_close_list);
763static LIST_HEAD(smd_ch_list_modem);
764static LIST_HEAD(smd_ch_list_dsp);
765static LIST_HEAD(smd_ch_list_dsps);
766static LIST_HEAD(smd_ch_list_wcnss);
Jeff Hugo918b2dc2012-03-21 13:42:09 -0600767static LIST_HEAD(smd_ch_list_rpm);
Brian Swetland2eb44eb2008-09-29 16:00:48 -0700768
769static unsigned char smd_ch_allocated[64];
770static struct work_struct probe_work;
771
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700772static void finalize_channel_close_fn(struct work_struct *work);
773static DECLARE_WORK(finalize_channel_close_work, finalize_channel_close_fn);
774static struct workqueue_struct *channel_close_wq;
775
776static int smd_alloc_channel(struct smd_alloc_elm *alloc_elm);
777
778/* on smp systems, the probe might get called from multiple cores,
779 hence use a lock */
780static DEFINE_MUTEX(smd_probe_lock);
781
782static void smd_channel_probe_worker(struct work_struct *work)
783{
784 struct smd_alloc_elm *shared;
785 unsigned n;
786 uint32_t type;
787
788 shared = smem_find(ID_CH_ALLOC_TBL, sizeof(*shared) * 64);
789
790 if (!shared) {
791 pr_err("%s: allocation table not initialized\n", __func__);
792 return;
793 }
794
795 mutex_lock(&smd_probe_lock);
796 for (n = 0; n < 64; n++) {
797 if (smd_ch_allocated[n])
798 continue;
799
800 /* channel should be allocated only if APPS
801 processor is involved */
802 type = SMD_CHANNEL_TYPE(shared[n].type);
Jeff Hugo918b2dc2012-03-21 13:42:09 -0600803 if (type >= ARRAY_SIZE(edge_to_pids) ||
804 edge_to_pids[type].local_pid != SMD_APPS)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700805 continue;
806 if (!shared[n].ref_count)
807 continue;
808 if (!shared[n].name[0])
809 continue;
810
811 if (!smd_alloc_channel(&shared[n]))
812 smd_ch_allocated[n] = 1;
813 else
814 SMD_INFO("Probe skipping ch %d, not allocated\n", n);
815 }
816 mutex_unlock(&smd_probe_lock);
817}
818
819/**
820 * Lookup processor ID and determine if it belongs to the proved edge
821 * type.
822 *
823 * @shared2: Pointer to v2 shared channel structure
824 * @type: Edge type
825 * @pid: Processor ID of processor on edge
826 * @local_ch: Channel that belongs to processor @pid
827 * @remote_ch: Other side of edge contained @pid
Jeff Hugo00be6282012-09-07 11:24:32 -0600828 * @is_word_access_ch: Bool, is this a word aligned access channel
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700829 *
830 * Returns 0 for not on edge, 1 for found on edge
831 */
Jeff Hugo00be6282012-09-07 11:24:32 -0600832static int pid_is_on_edge(void *shared2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700833 uint32_t type, uint32_t pid,
Jeff Hugo00be6282012-09-07 11:24:32 -0600834 void **local_ch,
835 void **remote_ch,
836 int is_word_access_ch
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700837 )
838{
839 int ret = 0;
840 struct edge_to_pid *edge;
Jeff Hugo00be6282012-09-07 11:24:32 -0600841 void *ch0;
842 void *ch1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700843
844 *local_ch = 0;
845 *remote_ch = 0;
846
847 if (!shared2 || (type >= ARRAY_SIZE(edge_to_pids)))
848 return 0;
849
Jeff Hugo00be6282012-09-07 11:24:32 -0600850 if (is_word_access_ch) {
851 ch0 = &((struct smd_shared_v2_word_access *)(shared2))->ch0;
852 ch1 = &((struct smd_shared_v2_word_access *)(shared2))->ch1;
853 } else {
854 ch0 = &((struct smd_shared_v2 *)(shared2))->ch0;
855 ch1 = &((struct smd_shared_v2 *)(shared2))->ch1;
856 }
857
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700858 edge = &edge_to_pids[type];
859 if (edge->local_pid != edge->remote_pid) {
860 if (pid == edge->local_pid) {
Jeff Hugo00be6282012-09-07 11:24:32 -0600861 *local_ch = ch0;
862 *remote_ch = ch1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700863 ret = 1;
864 } else if (pid == edge->remote_pid) {
Jeff Hugo00be6282012-09-07 11:24:32 -0600865 *local_ch = ch1;
866 *remote_ch = ch0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700867 ret = 1;
868 }
869 }
870
871 return ret;
872}
873
Eric Holmberg17992c12012-02-29 12:54:44 -0700874/*
875 * Returns a pointer to the subsystem name or NULL if no
876 * subsystem name is available.
877 *
878 * @type - Edge definition
879 */
880const char *smd_edge_to_subsystem(uint32_t type)
881{
882 const char *subsys = NULL;
883
884 if (type < ARRAY_SIZE(edge_to_pids)) {
885 subsys = edge_to_pids[type].subsys_name;
886 if (subsys[0] == 0x0)
887 subsys = NULL;
888 }
889 return subsys;
890}
891EXPORT_SYMBOL(smd_edge_to_subsystem);
892
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700893/*
894 * Returns a pointer to the subsystem name given the
895 * remote processor ID.
Arun Kumar Neelakantama35c7a72012-10-18 15:45:15 +0530896 * subsystem is not necessarily PIL-loadable
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700897 *
898 * @pid Remote processor ID
899 * @returns Pointer to subsystem name or NULL if not found
900 */
901const char *smd_pid_to_subsystem(uint32_t pid)
902{
903 const char *subsys = NULL;
904 int i;
905
906 for (i = 0; i < ARRAY_SIZE(edge_to_pids); ++i) {
Arun Kumar Neelakantama35c7a72012-10-18 15:45:15 +0530907 if (pid == edge_to_pids[i].remote_pid) {
908 if (edge_to_pids[i].subsys_name[0] != 0x0) {
909 subsys = edge_to_pids[i].subsys_name;
910 break;
911 } else if (pid == SMD_RPM) {
912 subsys = "rpm";
913 break;
914 }
Eric Holmberg7ad623a2012-03-01 14:41:10 -0700915 }
916 }
917
918 return subsys;
919}
920EXPORT_SYMBOL(smd_pid_to_subsystem);
Eric Holmberg17992c12012-02-29 12:54:44 -0700921
Jeff Hugo00be6282012-09-07 11:24:32 -0600922static void smd_reset_edge(void *void_ch, unsigned new_state,
923 int is_word_access_ch)
Eric Holmberg2a563c32011-10-05 14:51:43 -0600924{
Jeff Hugo00be6282012-09-07 11:24:32 -0600925 if (is_word_access_ch) {
926 struct smd_half_channel_word_access *ch =
927 (struct smd_half_channel_word_access *)(void_ch);
928 if (ch->state != SMD_SS_CLOSED) {
929 ch->state = new_state;
930 ch->fDSR = 0;
931 ch->fCTS = 0;
932 ch->fCD = 0;
933 ch->fSTATE = 1;
934 }
935 } else {
936 struct smd_half_channel *ch =
937 (struct smd_half_channel *)(void_ch);
938 if (ch->state != SMD_SS_CLOSED) {
939 ch->state = new_state;
940 ch->fDSR = 0;
941 ch->fCTS = 0;
942 ch->fCD = 0;
943 ch->fSTATE = 1;
944 }
Eric Holmberg2a563c32011-10-05 14:51:43 -0600945 }
946}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700947
948static void smd_channel_reset_state(struct smd_alloc_elm *shared,
949 unsigned new_state, unsigned pid)
950{
951 unsigned n;
Jeff Hugo00be6282012-09-07 11:24:32 -0600952 void *shared2;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700953 uint32_t type;
Jeff Hugo00be6282012-09-07 11:24:32 -0600954 void *local_ch;
955 void *remote_ch;
956 int is_word_access;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700957
958 for (n = 0; n < SMD_CHANNELS; n++) {
959 if (!shared[n].ref_count)
960 continue;
961 if (!shared[n].name[0])
962 continue;
963
964 type = SMD_CHANNEL_TYPE(shared[n].type);
Jeff Hugo00be6282012-09-07 11:24:32 -0600965 is_word_access = is_word_access_ch(type);
966 if (is_word_access)
967 shared2 = smem_alloc(SMEM_SMD_BASE_ID + n,
968 sizeof(struct smd_shared_v2_word_access));
969 else
970 shared2 = smem_alloc(SMEM_SMD_BASE_ID + n,
971 sizeof(struct smd_shared_v2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700972 if (!shared2)
973 continue;
974
Jeff Hugo00be6282012-09-07 11:24:32 -0600975 if (pid_is_on_edge(shared2, type, pid, &local_ch, &remote_ch,
976 is_word_access))
977 smd_reset_edge(local_ch, new_state, is_word_access);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700978
Eric Holmberg2a563c32011-10-05 14:51:43 -0600979 /*
980 * ModemFW is in the same subsystem as ModemSW, but has
981 * separate SMD edges that need to be reset.
982 */
983 if (pid == SMSM_MODEM &&
984 pid_is_on_edge(shared2, type, SMD_MODEM_Q6_FW,
Jeff Hugo00be6282012-09-07 11:24:32 -0600985 &local_ch, &remote_ch, is_word_access))
986 smd_reset_edge(local_ch, new_state, is_word_access);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700987 }
988}
989
990
991void smd_channel_reset(uint32_t restart_pid)
992{
993 struct smd_alloc_elm *shared;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700994 unsigned long flags;
995
Arun Kumar Neelakantam7cffb332013-01-28 15:43:35 +0530996 SMx_POWER_INFO("%s: starting reset\n", __func__);
Eric Holmberg50ad86f2012-09-07 13:54:31 -0600997
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700998 shared = smem_find(ID_CH_ALLOC_TBL, sizeof(*shared) * 64);
999 if (!shared) {
1000 pr_err("%s: allocation table not initialized\n", __func__);
1001 return;
1002 }
1003
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001004 /* reset SMSM entry */
1005 if (smsm_info.state) {
1006 writel_relaxed(0, SMSM_STATE_ADDR(restart_pid));
1007
Eric Holmberg351a63c2011-12-02 17:49:43 -07001008 /* restart SMSM init handshake */
1009 if (restart_pid == SMSM_MODEM) {
1010 smsm_change_state(SMSM_APPS_STATE,
Eric Holmberg6b2f80e2012-01-09 12:22:52 -07001011 SMSM_INIT | SMSM_SMD_LOOPBACK | SMSM_RESET,
1012 0);
Eric Holmberg351a63c2011-12-02 17:49:43 -07001013 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001014
1015 /* notify SMSM processors */
1016 smsm_irq_handler(0, 0);
Eric Holmbergb7726442012-03-01 15:31:56 -07001017 notify_modem_smsm();
1018 notify_dsp_smsm();
1019 notify_dsps_smsm();
1020 notify_wcnss_smsm();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001021 }
1022
1023 /* change all remote states to CLOSING */
1024 mutex_lock(&smd_probe_lock);
1025 spin_lock_irqsave(&smd_lock, flags);
1026 smd_channel_reset_state(shared, SMD_SS_CLOSING, restart_pid);
1027 spin_unlock_irqrestore(&smd_lock, flags);
1028 mutex_unlock(&smd_probe_lock);
1029
1030 /* notify SMD processors */
1031 mb();
1032 smd_fake_irq_handler(0);
Arun Kumar Neelakantam7cffb332013-01-28 15:43:35 +05301033 notify_modem_smd(NULL);
1034 notify_dsp_smd(NULL);
1035 notify_dsps_smd(NULL);
1036 notify_wcnss_smd(NULL);
1037 notify_rpm_smd(NULL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001038
1039 /* change all remote states to CLOSED */
1040 mutex_lock(&smd_probe_lock);
1041 spin_lock_irqsave(&smd_lock, flags);
1042 smd_channel_reset_state(shared, SMD_SS_CLOSED, restart_pid);
1043 spin_unlock_irqrestore(&smd_lock, flags);
1044 mutex_unlock(&smd_probe_lock);
1045
1046 /* notify SMD processors */
1047 mb();
1048 smd_fake_irq_handler(0);
Arun Kumar Neelakantam7cffb332013-01-28 15:43:35 +05301049 notify_modem_smd(NULL);
1050 notify_dsp_smd(NULL);
1051 notify_dsps_smd(NULL);
1052 notify_wcnss_smd(NULL);
1053 notify_rpm_smd(NULL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001054
Arun Kumar Neelakantam7cffb332013-01-28 15:43:35 +05301055 SMx_POWER_INFO("%s: finished reset\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001056}
1057
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001058/* how many bytes are available for reading */
1059static int smd_stream_read_avail(struct smd_channel *ch)
1060{
Jeff Hugo918b2dc2012-03-21 13:42:09 -06001061 return (ch->half_ch->get_head(ch->recv) -
1062 ch->half_ch->get_tail(ch->recv)) & ch->fifo_mask;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001063}
1064
1065/* how many bytes we are free to write */
1066static int smd_stream_write_avail(struct smd_channel *ch)
1067{
Eric Holmberg424d9552013-04-05 15:23:25 -06001068 int bytes_avail;
1069
1070 bytes_avail = ch->fifo_mask - ((ch->half_ch->get_head(ch->send) -
1071 ch->half_ch->get_tail(ch->send)) & ch->fifo_mask) + 1;
1072
1073 if (bytes_avail < SMD_FIFO_FULL_RESERVE)
1074 bytes_avail = 0;
1075 else
1076 bytes_avail -= SMD_FIFO_FULL_RESERVE;
1077 return bytes_avail;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001078}
1079
1080static int smd_packet_read_avail(struct smd_channel *ch)
1081{
1082 if (ch->current_packet) {
1083 int n = smd_stream_read_avail(ch);
1084 if (n > ch->current_packet)
1085 n = ch->current_packet;
1086 return n;
1087 } else {
1088 return 0;
1089 }
1090}
1091
1092static int smd_packet_write_avail(struct smd_channel *ch)
1093{
1094 int n = smd_stream_write_avail(ch);
1095 return n > SMD_HEADER_SIZE ? n - SMD_HEADER_SIZE : 0;
1096}
1097
1098static int ch_is_open(struct smd_channel *ch)
1099{
Jeff Hugo918b2dc2012-03-21 13:42:09 -06001100 return (ch->half_ch->get_state(ch->recv) == SMD_SS_OPENED ||
1101 ch->half_ch->get_state(ch->recv) == SMD_SS_FLUSHING)
1102 && (ch->half_ch->get_state(ch->send) == SMD_SS_OPENED);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001103}
1104
1105/* provide a pointer and length to readable data in the fifo */
1106static unsigned ch_read_buffer(struct smd_channel *ch, void **ptr)
1107{
Jeff Hugo918b2dc2012-03-21 13:42:09 -06001108 unsigned head = ch->half_ch->get_head(ch->recv);
1109 unsigned tail = ch->half_ch->get_tail(ch->recv);
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001110 *ptr = (void *) (ch->recv_data + tail);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001111
1112 if (tail <= head)
1113 return head - tail;
1114 else
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001115 return ch->fifo_size - tail;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001116}
1117
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001118static int read_intr_blocked(struct smd_channel *ch)
1119{
Jeff Hugo918b2dc2012-03-21 13:42:09 -06001120 return ch->half_ch->get_fBLOCKREADINTR(ch->recv);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001121}
1122
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001123/* advance the fifo read pointer after data from ch_read_buffer is consumed */
1124static void ch_read_done(struct smd_channel *ch, unsigned count)
1125{
1126 BUG_ON(count > smd_stream_read_avail(ch));
Jeff Hugo918b2dc2012-03-21 13:42:09 -06001127 ch->half_ch->set_tail(ch->recv,
1128 (ch->half_ch->get_tail(ch->recv) + count) & ch->fifo_mask);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001129 wmb();
Jeff Hugo918b2dc2012-03-21 13:42:09 -06001130 ch->half_ch->set_fTAIL(ch->send, 1);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001131}
1132
1133/* basic read interface to ch_read_{buffer,done} used
Brian Swetland03e00cd2009-07-01 17:58:37 -07001134 * by smd_*_read() and update_packet_state()
1135 * will read-and-discard if the _data pointer is null
1136 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001137static int ch_read(struct smd_channel *ch, void *_data, int len, int user_buf)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001138{
1139 void *ptr;
1140 unsigned n;
1141 unsigned char *data = _data;
1142 int orig_len = len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001143 int r = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001144
1145 while (len > 0) {
1146 n = ch_read_buffer(ch, &ptr);
1147 if (n == 0)
1148 break;
1149
1150 if (n > len)
1151 n = len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001152 if (_data) {
1153 if (user_buf) {
1154 r = copy_to_user(data, ptr, n);
1155 if (r > 0) {
1156 pr_err("%s: "
1157 "copy_to_user could not copy "
1158 "%i bytes.\n",
1159 __func__,
1160 r);
1161 }
1162 } else
1163 memcpy(data, ptr, n);
1164 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001165
1166 data += n;
1167 len -= n;
1168 ch_read_done(ch, n);
1169 }
1170
1171 return orig_len - len;
1172}
1173
1174static void update_stream_state(struct smd_channel *ch)
1175{
1176 /* streams have no special state requiring updating */
1177}
1178
1179static void update_packet_state(struct smd_channel *ch)
1180{
1181 unsigned hdr[5];
1182 int r;
1183
1184 /* can't do anything if we're in the middle of a packet */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001185 while (ch->current_packet == 0) {
1186 /* discard 0 length packets if any */
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001187
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001188 /* don't bother unless we can get the full header */
1189 if (smd_stream_read_avail(ch) < SMD_HEADER_SIZE)
1190 return;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001191
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001192 r = ch_read(ch, hdr, SMD_HEADER_SIZE, 0);
1193 BUG_ON(r != SMD_HEADER_SIZE);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001194
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001195 ch->current_packet = hdr[0];
1196 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001197}
1198
Eric Holmberg424d9552013-04-05 15:23:25 -06001199/**
1200 * ch_write_buffer() - Provide a pointer and length for the next segment of
1201 * free space in the FIFO.
1202 * @ch: channel
1203 * @ptr: Address to pointer for the next segment write
1204 * @returns: Maximum size that can be written until the FIFO is either full
1205 * or the end of the FIFO has been reached.
1206 *
1207 * The returned pointer and length are passed to memcpy, so the next segment is
1208 * defined as either the space available between the read index (tail) and the
1209 * write index (head) or the space available to the end of the FIFO.
1210 */
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001211static unsigned ch_write_buffer(struct smd_channel *ch, void **ptr)
1212{
Jeff Hugo918b2dc2012-03-21 13:42:09 -06001213 unsigned head = ch->half_ch->get_head(ch->send);
1214 unsigned tail = ch->half_ch->get_tail(ch->send);
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001215 *ptr = (void *) (ch->send_data + head);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001216
1217 if (head < tail) {
Eric Holmberg424d9552013-04-05 15:23:25 -06001218 return tail - head - SMD_FIFO_FULL_RESERVE;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001219 } else {
Eric Holmberg424d9552013-04-05 15:23:25 -06001220 if (tail < SMD_FIFO_FULL_RESERVE)
1221 return ch->fifo_size + tail - head
1222 - SMD_FIFO_FULL_RESERVE;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001223 else
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001224 return ch->fifo_size - head;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001225 }
1226}
1227
1228/* advace the fifo write pointer after freespace
1229 * from ch_write_buffer is filled
1230 */
1231static void ch_write_done(struct smd_channel *ch, unsigned count)
1232{
1233 BUG_ON(count > smd_stream_write_avail(ch));
Jeff Hugo918b2dc2012-03-21 13:42:09 -06001234 ch->half_ch->set_head(ch->send,
1235 (ch->half_ch->get_head(ch->send) + count) & ch->fifo_mask);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001236 wmb();
Jeff Hugo918b2dc2012-03-21 13:42:09 -06001237 ch->half_ch->set_fHEAD(ch->send, 1);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001238}
1239
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001240static void ch_set_state(struct smd_channel *ch, unsigned n)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001241{
1242 if (n == SMD_SS_OPENED) {
Jeff Hugo918b2dc2012-03-21 13:42:09 -06001243 ch->half_ch->set_fDSR(ch->send, 1);
1244 ch->half_ch->set_fCTS(ch->send, 1);
1245 ch->half_ch->set_fCD(ch->send, 1);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001246 } else {
Jeff Hugo918b2dc2012-03-21 13:42:09 -06001247 ch->half_ch->set_fDSR(ch->send, 0);
1248 ch->half_ch->set_fCTS(ch->send, 0);
1249 ch->half_ch->set_fCD(ch->send, 0);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001250 }
Jeff Hugo918b2dc2012-03-21 13:42:09 -06001251 ch->half_ch->set_state(ch->send, n);
1252 ch->half_ch->set_fSTATE(ch->send, 1);
Arun Kumar Neelakantam7cffb332013-01-28 15:43:35 +05301253 ch->notify_other_cpu(ch);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001254}
1255
1256static void do_smd_probe(void)
1257{
1258 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
1259 if (shared->heap_info.free_offset != last_heap_free) {
1260 last_heap_free = shared->heap_info.free_offset;
1261 schedule_work(&probe_work);
1262 }
1263}
1264
1265static void smd_state_change(struct smd_channel *ch,
1266 unsigned last, unsigned next)
1267{
1268 ch->last_state = next;
1269
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001270 SMD_INFO("SMD: ch %d %d -> %d\n", ch->n, last, next);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001271
1272 switch (next) {
1273 case SMD_SS_OPENING:
Jeff Hugo918b2dc2012-03-21 13:42:09 -06001274 if (ch->half_ch->get_state(ch->send) == SMD_SS_CLOSING ||
1275 ch->half_ch->get_state(ch->send) == SMD_SS_CLOSED) {
1276 ch->half_ch->set_tail(ch->recv, 0);
1277 ch->half_ch->set_head(ch->send, 0);
1278 ch->half_ch->set_fBLOCKREADINTR(ch->send, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001279 ch_set_state(ch, SMD_SS_OPENING);
1280 }
1281 break;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001282 case SMD_SS_OPENED:
Jeff Hugo918b2dc2012-03-21 13:42:09 -06001283 if (ch->half_ch->get_state(ch->send) == SMD_SS_OPENING) {
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001284 ch_set_state(ch, SMD_SS_OPENED);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001285 ch->notify(ch->priv, SMD_EVENT_OPEN);
1286 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001287 break;
1288 case SMD_SS_FLUSHING:
1289 case SMD_SS_RESET:
1290 /* we should force them to close? */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001291 break;
1292 case SMD_SS_CLOSED:
Jeff Hugo918b2dc2012-03-21 13:42:09 -06001293 if (ch->half_ch->get_state(ch->send) == SMD_SS_OPENED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001294 ch_set_state(ch, SMD_SS_CLOSING);
1295 ch->current_packet = 0;
Eric Holmbergad4fa8d2011-11-11 16:55:13 -07001296 ch->pending_pkt_sz = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001297 ch->notify(ch->priv, SMD_EVENT_CLOSE);
1298 }
1299 break;
1300 case SMD_SS_CLOSING:
Jeff Hugo918b2dc2012-03-21 13:42:09 -06001301 if (ch->half_ch->get_state(ch->send) == SMD_SS_CLOSED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001302 list_move(&ch->ch_list,
1303 &smd_ch_to_close_list);
1304 queue_work(channel_close_wq,
1305 &finalize_channel_close_work);
1306 }
1307 break;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001308 }
1309}
1310
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001311static void handle_smd_irq_closing_list(void)
1312{
1313 unsigned long flags;
1314 struct smd_channel *ch;
1315 struct smd_channel *index;
1316 unsigned tmp;
1317
1318 spin_lock_irqsave(&smd_lock, flags);
1319 list_for_each_entry_safe(ch, index, &smd_ch_closing_list, ch_list) {
Jeff Hugo918b2dc2012-03-21 13:42:09 -06001320 if (ch->half_ch->get_fSTATE(ch->recv))
1321 ch->half_ch->set_fSTATE(ch->recv, 0);
1322 tmp = ch->half_ch->get_state(ch->recv);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001323 if (tmp != ch->last_state)
1324 smd_state_change(ch, ch->last_state, tmp);
1325 }
1326 spin_unlock_irqrestore(&smd_lock, flags);
1327}
1328
Arun Kumar Neelakantam7cffb332013-01-28 15:43:35 +05301329static void handle_smd_irq(struct list_head *list,
1330 void (*notify)(smd_channel_t *ch))
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001331{
1332 unsigned long flags;
1333 struct smd_channel *ch;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001334 unsigned ch_flags;
1335 unsigned tmp;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001336 unsigned char state_change;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001337
1338 spin_lock_irqsave(&smd_lock, flags);
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001339 list_for_each_entry(ch, list, ch_list) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001340 state_change = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001341 ch_flags = 0;
1342 if (ch_is_open(ch)) {
Jeff Hugo918b2dc2012-03-21 13:42:09 -06001343 if (ch->half_ch->get_fHEAD(ch->recv)) {
1344 ch->half_ch->set_fHEAD(ch->recv, 0);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001345 ch_flags |= 1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001346 }
Jeff Hugo918b2dc2012-03-21 13:42:09 -06001347 if (ch->half_ch->get_fTAIL(ch->recv)) {
1348 ch->half_ch->set_fTAIL(ch->recv, 0);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001349 ch_flags |= 2;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001350 }
Jeff Hugo918b2dc2012-03-21 13:42:09 -06001351 if (ch->half_ch->get_fSTATE(ch->recv)) {
1352 ch->half_ch->set_fSTATE(ch->recv, 0);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001353 ch_flags |= 4;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001354 }
1355 }
Jeff Hugo918b2dc2012-03-21 13:42:09 -06001356 tmp = ch->half_ch->get_state(ch->recv);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001357 if (tmp != ch->last_state) {
Eric Holmberg98c6c642012-02-24 11:29:35 -07001358 SMx_POWER_INFO("SMD ch%d '%s' State change %d->%d\n",
1359 ch->n, ch->name, ch->last_state, tmp);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001360 smd_state_change(ch, ch->last_state, tmp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001361 state_change = 1;
1362 }
Eric Holmberg65a7d432012-02-24 11:28:56 -07001363 if (ch_flags & 0x3) {
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001364 ch->update_state(ch);
Arun Kumar Neelakantam7cffb332013-01-28 15:43:35 +05301365 SMx_POWER_INFO(
1366 "SMD ch%d '%s' Data event 0x%x tx%d/rx%d %dr/%dw : %dr/%dw\n",
1367 ch->n, ch->name,
1368 ch_flags,
1369 ch->fifo_size -
1370 (smd_stream_write_avail(ch) + 1),
1371 smd_stream_read_avail(ch),
1372 ch->half_ch->get_tail(ch->send),
1373 ch->half_ch->get_head(ch->send),
1374 ch->half_ch->get_tail(ch->recv),
1375 ch->half_ch->get_head(ch->recv)
1376 );
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001377 ch->notify(ch->priv, SMD_EVENT_DATA);
1378 }
Eric Holmberg98c6c642012-02-24 11:29:35 -07001379 if (ch_flags & 0x4 && !state_change) {
1380 SMx_POWER_INFO("SMD ch%d '%s' State update\n",
1381 ch->n, ch->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001382 ch->notify(ch->priv, SMD_EVENT_STATUS);
Eric Holmberg98c6c642012-02-24 11:29:35 -07001383 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001384 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001385 spin_unlock_irqrestore(&smd_lock, flags);
1386 do_smd_probe();
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001387}
1388
Arun Kumar Neelakantam7cffb332013-01-28 15:43:35 +05301389static inline void log_irq(uint32_t subsystem)
1390{
1391 const char *subsys = smd_edge_to_subsystem(subsystem);
1392
Jay Chokshi83b4f6132013-02-14 16:20:56 -08001393 (void) subsys;
1394
Arun Kumar Neelakantam7cffb332013-01-28 15:43:35 +05301395 SMx_POWER_INFO("SMD Int %s->Apps\n", subsys);
1396}
1397
Brian Swetland37521a32009-07-01 18:30:47 -07001398static irqreturn_t smd_modem_irq_handler(int irq, void *data)
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001399{
Arun Kumar Neelakantam7cffb332013-01-28 15:43:35 +05301400 log_irq(SMD_APPS_MODEM);
Eric Holmberg7ad623a2012-03-01 14:41:10 -07001401 ++interrupt_stats[SMD_MODEM].smd_in_count;
Brian Swetland37521a32009-07-01 18:30:47 -07001402 handle_smd_irq(&smd_ch_list_modem, notify_modem_smd);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001403 handle_smd_irq_closing_list();
Brian Swetland37521a32009-07-01 18:30:47 -07001404 return IRQ_HANDLED;
1405}
1406
1407static irqreturn_t smd_dsp_irq_handler(int irq, void *data)
1408{
Arun Kumar Neelakantam7cffb332013-01-28 15:43:35 +05301409 log_irq(SMD_APPS_QDSP);
Eric Holmberg7ad623a2012-03-01 14:41:10 -07001410 ++interrupt_stats[SMD_Q6].smd_in_count;
Brian Swetland37521a32009-07-01 18:30:47 -07001411 handle_smd_irq(&smd_ch_list_dsp, notify_dsp_smd);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001412 handle_smd_irq_closing_list();
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001413 return IRQ_HANDLED;
1414}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001415
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001416static irqreturn_t smd_dsps_irq_handler(int irq, void *data)
1417{
Arun Kumar Neelakantam7cffb332013-01-28 15:43:35 +05301418 log_irq(SMD_APPS_DSPS);
Eric Holmberg7ad623a2012-03-01 14:41:10 -07001419 ++interrupt_stats[SMD_DSPS].smd_in_count;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001420 handle_smd_irq(&smd_ch_list_dsps, notify_dsps_smd);
1421 handle_smd_irq_closing_list();
1422 return IRQ_HANDLED;
1423}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001424
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001425static irqreturn_t smd_wcnss_irq_handler(int irq, void *data)
1426{
Arun Kumar Neelakantam7cffb332013-01-28 15:43:35 +05301427 log_irq(SMD_APPS_WCNSS);
Eric Holmberg7ad623a2012-03-01 14:41:10 -07001428 ++interrupt_stats[SMD_WCNSS].smd_in_count;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001429 handle_smd_irq(&smd_ch_list_wcnss, notify_wcnss_smd);
1430 handle_smd_irq_closing_list();
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001431 return IRQ_HANDLED;
1432}
1433
Jeff Hugo918b2dc2012-03-21 13:42:09 -06001434static irqreturn_t smd_rpm_irq_handler(int irq, void *data)
1435{
Arun Kumar Neelakantam7cffb332013-01-28 15:43:35 +05301436 log_irq(SMD_APPS_RPM);
Jeff Hugo918b2dc2012-03-21 13:42:09 -06001437 ++interrupt_stats[SMD_RPM].smd_in_count;
1438 handle_smd_irq(&smd_ch_list_rpm, notify_rpm_smd);
1439 handle_smd_irq_closing_list();
1440 return IRQ_HANDLED;
1441}
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001442
1443static void smd_fake_irq_handler(unsigned long arg)
1444{
Brian Swetland37521a32009-07-01 18:30:47 -07001445 handle_smd_irq(&smd_ch_list_modem, notify_modem_smd);
1446 handle_smd_irq(&smd_ch_list_dsp, notify_dsp_smd);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001447 handle_smd_irq(&smd_ch_list_dsps, notify_dsps_smd);
1448 handle_smd_irq(&smd_ch_list_wcnss, notify_wcnss_smd);
Jeff Hugo918b2dc2012-03-21 13:42:09 -06001449 handle_smd_irq(&smd_ch_list_rpm, notify_rpm_smd);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001450 handle_smd_irq_closing_list();
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001451}
1452
1453static DECLARE_TASKLET(smd_fake_irq_tasklet, smd_fake_irq_handler, 0);
1454
Brian Swetland37521a32009-07-01 18:30:47 -07001455static inline int smd_need_int(struct smd_channel *ch)
1456{
1457 if (ch_is_open(ch)) {
Jeff Hugo918b2dc2012-03-21 13:42:09 -06001458 if (ch->half_ch->get_fHEAD(ch->recv) ||
1459 ch->half_ch->get_fTAIL(ch->recv) ||
1460 ch->half_ch->get_fSTATE(ch->recv))
Brian Swetland37521a32009-07-01 18:30:47 -07001461 return 1;
Jeff Hugo918b2dc2012-03-21 13:42:09 -06001462 if (ch->half_ch->get_state(ch->recv) != ch->last_state)
Brian Swetland37521a32009-07-01 18:30:47 -07001463 return 1;
1464 }
1465 return 0;
1466}
1467
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001468void smd_sleep_exit(void)
1469{
1470 unsigned long flags;
1471 struct smd_channel *ch;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001472 int need_int = 0;
1473
1474 spin_lock_irqsave(&smd_lock, flags);
Brian Swetland37521a32009-07-01 18:30:47 -07001475 list_for_each_entry(ch, &smd_ch_list_modem, ch_list) {
1476 if (smd_need_int(ch)) {
1477 need_int = 1;
1478 break;
1479 }
1480 }
1481 list_for_each_entry(ch, &smd_ch_list_dsp, ch_list) {
1482 if (smd_need_int(ch)) {
1483 need_int = 1;
1484 break;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001485 }
1486 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001487 list_for_each_entry(ch, &smd_ch_list_dsps, ch_list) {
1488 if (smd_need_int(ch)) {
1489 need_int = 1;
1490 break;
1491 }
1492 }
1493 list_for_each_entry(ch, &smd_ch_list_wcnss, ch_list) {
1494 if (smd_need_int(ch)) {
1495 need_int = 1;
1496 break;
1497 }
1498 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001499 spin_unlock_irqrestore(&smd_lock, flags);
1500 do_smd_probe();
Brian Swetland37521a32009-07-01 18:30:47 -07001501
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001502 if (need_int) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001503 SMD_DBG("smd_sleep_exit need interrupt\n");
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001504 tasklet_schedule(&smd_fake_irq_tasklet);
1505 }
1506}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001507EXPORT_SYMBOL(smd_sleep_exit);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001508
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001509static int smd_is_packet(struct smd_alloc_elm *alloc_elm)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001510{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001511 if (SMD_XFER_TYPE(alloc_elm->type) == 1)
1512 return 0;
1513 else if (SMD_XFER_TYPE(alloc_elm->type) == 2)
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001514 return 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001515
1516 /* for cases where xfer type is 0 */
1517 if (!strncmp(alloc_elm->name, "DAL", 3))
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001518 return 0;
1519
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001520 /* for cases where xfer type is 0 */
1521 if (!strncmp(alloc_elm->name, "RPCCALL_QDSP", 12))
1522 return 0;
1523
1524 if (alloc_elm->cid > 4 || alloc_elm->cid == 1)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001525 return 1;
1526 else
1527 return 0;
1528}
1529
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001530static int smd_stream_write(smd_channel_t *ch, const void *_data, int len,
1531 int user_buf)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001532{
1533 void *ptr;
1534 const unsigned char *buf = _data;
1535 unsigned xfer;
1536 int orig_len = len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001537 int r = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001538
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001539 SMD_DBG("smd_stream_write() %d -> ch%d\n", len, ch->n);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001540 if (len < 0)
1541 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001542 else if (len == 0)
1543 return 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001544
1545 while ((xfer = ch_write_buffer(ch, &ptr)) != 0) {
Eric Holmberg7a717872012-02-03 11:58:04 -07001546 if (!ch_is_open(ch)) {
1547 len = orig_len;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001548 break;
Eric Holmberg7a717872012-02-03 11:58:04 -07001549 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001550 if (xfer > len)
1551 xfer = len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001552 if (user_buf) {
1553 r = copy_from_user(ptr, buf, xfer);
1554 if (r > 0) {
1555 pr_err("%s: "
1556 "copy_from_user could not copy %i "
1557 "bytes.\n",
1558 __func__,
1559 r);
1560 }
1561 } else
1562 memcpy(ptr, buf, xfer);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001563 ch_write_done(ch, xfer);
1564 len -= xfer;
1565 buf += xfer;
1566 if (len == 0)
1567 break;
1568 }
1569
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001570 if (orig_len - len)
Arun Kumar Neelakantam7cffb332013-01-28 15:43:35 +05301571 ch->notify_other_cpu(ch);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001572
1573 return orig_len - len;
1574}
1575
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001576static int smd_packet_write(smd_channel_t *ch, const void *_data, int len,
1577 int user_buf)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001578{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001579 int ret;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001580 unsigned hdr[5];
1581
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001582 SMD_DBG("smd_packet_write() %d -> ch%d\n", len, ch->n);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001583 if (len < 0)
1584 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001585 else if (len == 0)
1586 return 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001587
1588 if (smd_stream_write_avail(ch) < (len + SMD_HEADER_SIZE))
1589 return -ENOMEM;
1590
1591 hdr[0] = len;
1592 hdr[1] = hdr[2] = hdr[3] = hdr[4] = 0;
1593
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001594
1595 ret = smd_stream_write(ch, hdr, sizeof(hdr), 0);
1596 if (ret < 0 || ret != sizeof(hdr)) {
1597 SMD_DBG("%s failed to write pkt header: "
1598 "%d returned\n", __func__, ret);
1599 return -1;
1600 }
1601
1602
1603 ret = smd_stream_write(ch, _data, len, user_buf);
1604 if (ret < 0 || ret != len) {
1605 SMD_DBG("%s failed to write pkt data: "
1606 "%d returned\n", __func__, ret);
1607 return ret;
1608 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001609
1610 return len;
1611}
1612
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001613static int smd_stream_read(smd_channel_t *ch, void *data, int len, int user_buf)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001614{
1615 int r;
1616
1617 if (len < 0)
1618 return -EINVAL;
1619
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001620 r = ch_read(ch, data, len, user_buf);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001621 if (r > 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001622 if (!read_intr_blocked(ch))
Arun Kumar Neelakantam7cffb332013-01-28 15:43:35 +05301623 ch->notify_other_cpu(ch);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001624
1625 return r;
1626}
1627
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001628static int smd_packet_read(smd_channel_t *ch, void *data, int len, int user_buf)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001629{
1630 unsigned long flags;
1631 int r;
1632
1633 if (len < 0)
1634 return -EINVAL;
1635
1636 if (len > ch->current_packet)
1637 len = ch->current_packet;
1638
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001639 r = ch_read(ch, data, len, user_buf);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001640 if (r > 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001641 if (!read_intr_blocked(ch))
Arun Kumar Neelakantam7cffb332013-01-28 15:43:35 +05301642 ch->notify_other_cpu(ch);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001643
1644 spin_lock_irqsave(&smd_lock, flags);
1645 ch->current_packet -= r;
1646 update_packet_state(ch);
1647 spin_unlock_irqrestore(&smd_lock, flags);
1648
1649 return r;
1650}
1651
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001652static int smd_packet_read_from_cb(smd_channel_t *ch, void *data, int len,
1653 int user_buf)
1654{
1655 int r;
1656
1657 if (len < 0)
1658 return -EINVAL;
1659
1660 if (len > ch->current_packet)
1661 len = ch->current_packet;
1662
1663 r = ch_read(ch, data, len, user_buf);
1664 if (r > 0)
1665 if (!read_intr_blocked(ch))
Arun Kumar Neelakantam7cffb332013-01-28 15:43:35 +05301666 ch->notify_other_cpu(ch);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001667
1668 ch->current_packet -= r;
1669 update_packet_state(ch);
1670
1671 return r;
1672}
1673
Angshuman Sarkarac7d6252011-09-30 18:20:59 +05301674#if (defined(CONFIG_MSM_SMD_PKG4) || defined(CONFIG_MSM_SMD_PKG3))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001675static int smd_alloc_v2(struct smd_channel *ch)
1676{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001677 void *buffer;
1678 unsigned buffer_sz;
1679
Jeff Hugo918b2dc2012-03-21 13:42:09 -06001680 if (is_word_access_ch(ch->type)) {
1681 struct smd_shared_v2_word_access *shared2;
1682 shared2 = smem_alloc(SMEM_SMD_BASE_ID + ch->n,
1683 sizeof(*shared2));
1684 if (!shared2) {
1685 SMD_INFO("smem_alloc failed ch=%d\n", ch->n);
1686 return -EINVAL;
1687 }
1688 ch->send = &shared2->ch0;
1689 ch->recv = &shared2->ch1;
1690 } else {
1691 struct smd_shared_v2 *shared2;
1692 shared2 = smem_alloc(SMEM_SMD_BASE_ID + ch->n,
1693 sizeof(*shared2));
1694 if (!shared2) {
1695 SMD_INFO("smem_alloc failed ch=%d\n", ch->n);
1696 return -EINVAL;
1697 }
1698 ch->send = &shared2->ch0;
1699 ch->recv = &shared2->ch1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001700 }
Jeff Hugo918b2dc2012-03-21 13:42:09 -06001701 ch->half_ch = get_half_ch_funcs(ch->type);
1702
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001703 buffer = smem_get_entry(SMEM_SMD_FIFO_BASE_ID + ch->n, &buffer_sz);
1704 if (!buffer) {
Angshuman Sarkarac7d6252011-09-30 18:20:59 +05301705 SMD_INFO("smem_get_entry failed\n");
1706 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001707 }
1708
1709 /* buffer must be a power-of-two size */
Angshuman Sarkarac7d6252011-09-30 18:20:59 +05301710 if (buffer_sz & (buffer_sz - 1)) {
1711 SMD_INFO("Buffer size: %u not power of two\n", buffer_sz);
1712 return -EINVAL;
1713 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001714 buffer_sz /= 2;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001715 ch->send_data = buffer;
1716 ch->recv_data = buffer + buffer_sz;
1717 ch->fifo_size = buffer_sz;
Jeff Hugo918b2dc2012-03-21 13:42:09 -06001718
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001719 return 0;
1720}
1721
1722static int smd_alloc_v1(struct smd_channel *ch)
1723{
Angshuman Sarkarac7d6252011-09-30 18:20:59 +05301724 return -EINVAL;
1725}
1726
1727#else /* define v1 for older targets */
1728static int smd_alloc_v2(struct smd_channel *ch)
1729{
1730 return -EINVAL;
1731}
1732
1733static int smd_alloc_v1(struct smd_channel *ch)
1734{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001735 struct smd_shared_v1 *shared1;
1736 shared1 = smem_alloc(ID_SMD_CHANNELS + ch->n, sizeof(*shared1));
1737 if (!shared1) {
1738 pr_err("smd_alloc_channel() cid %d does not exist\n", ch->n);
Angshuman Sarkarac7d6252011-09-30 18:20:59 +05301739 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001740 }
1741 ch->send = &shared1->ch0;
1742 ch->recv = &shared1->ch1;
1743 ch->send_data = shared1->data0;
1744 ch->recv_data = shared1->data1;
1745 ch->fifo_size = SMD_BUF_SIZE;
Jeff Hugo918b2dc2012-03-21 13:42:09 -06001746 ch->half_ch = get_half_ch_funcs(ch->type);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001747 return 0;
1748}
1749
Angshuman Sarkarac7d6252011-09-30 18:20:59 +05301750#endif
1751
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001752static int smd_alloc_channel(struct smd_alloc_elm *alloc_elm)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001753{
1754 struct smd_channel *ch;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001755
1756 ch = kzalloc(sizeof(struct smd_channel), GFP_KERNEL);
1757 if (ch == 0) {
1758 pr_err("smd_alloc_channel() out of memory\n");
Brian Swetland34f719b2009-10-30 16:22:05 -07001759 return -1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001760 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001761 ch->n = alloc_elm->cid;
Jeff Hugo918b2dc2012-03-21 13:42:09 -06001762 ch->type = SMD_CHANNEL_TYPE(alloc_elm->type);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001763
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001764 if (smd_alloc_v2(ch) && smd_alloc_v1(ch)) {
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001765 kfree(ch);
Brian Swetland34f719b2009-10-30 16:22:05 -07001766 return -1;
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001767 }
1768
1769 ch->fifo_mask = ch->fifo_size - 1;
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001770
Jeff Hugo918b2dc2012-03-21 13:42:09 -06001771 /* probe_worker guarentees ch->type will be a valid type */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001772 if (ch->type == SMD_APPS_MODEM)
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001773 ch->notify_other_cpu = notify_modem_smd;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001774 else if (ch->type == SMD_APPS_QDSP)
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001775 ch->notify_other_cpu = notify_dsp_smd;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001776 else if (ch->type == SMD_APPS_DSPS)
1777 ch->notify_other_cpu = notify_dsps_smd;
Jeff Hugo918b2dc2012-03-21 13:42:09 -06001778 else if (ch->type == SMD_APPS_WCNSS)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001779 ch->notify_other_cpu = notify_wcnss_smd;
Jeff Hugo918b2dc2012-03-21 13:42:09 -06001780 else if (ch->type == SMD_APPS_RPM)
1781 ch->notify_other_cpu = notify_rpm_smd;
Brian Swetland5b0f5a32009-04-26 18:38:49 -07001782
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001783 if (smd_is_packet(alloc_elm)) {
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001784 ch->read = smd_packet_read;
1785 ch->write = smd_packet_write;
1786 ch->read_avail = smd_packet_read_avail;
1787 ch->write_avail = smd_packet_write_avail;
1788 ch->update_state = update_packet_state;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001789 ch->read_from_cb = smd_packet_read_from_cb;
1790 ch->is_pkt_ch = 1;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001791 } else {
1792 ch->read = smd_stream_read;
1793 ch->write = smd_stream_write;
1794 ch->read_avail = smd_stream_read_avail;
1795 ch->write_avail = smd_stream_write_avail;
1796 ch->update_state = update_stream_state;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001797 ch->read_from_cb = smd_stream_read;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001798 }
1799
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001800 memcpy(ch->name, alloc_elm->name, SMD_MAX_CH_NAME_LEN);
1801 ch->name[SMD_MAX_CH_NAME_LEN-1] = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001802
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001803 ch->pdev.name = ch->name;
1804 ch->pdev.id = ch->type;
1805
1806 SMD_INFO("smd_alloc_channel() '%s' cid=%d\n",
1807 ch->name, ch->n);
1808
1809 mutex_lock(&smd_creation_mutex);
1810 list_add(&ch->ch_list, &smd_ch_closed_list);
1811 mutex_unlock(&smd_creation_mutex);
1812
1813 platform_device_register(&ch->pdev);
1814 if (!strncmp(ch->name, "LOOPBACK", 8) && ch->type == SMD_APPS_MODEM) {
1815 /* create a platform driver to be used by smd_tty driver
1816 * so that it can access the loopback port
1817 */
1818 loopback_tty_pdev.id = ch->type;
1819 platform_device_register(&loopback_tty_pdev);
1820 }
1821 return 0;
1822}
1823
Arun Kumar Neelakantam7cffb332013-01-28 15:43:35 +05301824static inline void notify_loopback_smd(smd_channel_t *ch_notif)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001825{
1826 unsigned long flags;
1827 struct smd_channel *ch;
1828
1829 spin_lock_irqsave(&smd_lock, flags);
1830 list_for_each_entry(ch, &smd_ch_list_loopback, ch_list) {
1831 ch->notify(ch->priv, SMD_EVENT_DATA);
1832 }
1833 spin_unlock_irqrestore(&smd_lock, flags);
1834}
1835
1836static int smd_alloc_loopback_channel(void)
1837{
1838 static struct smd_half_channel smd_loopback_ctl;
1839 static char smd_loopback_data[SMD_BUF_SIZE];
1840 struct smd_channel *ch;
1841
1842 ch = kzalloc(sizeof(struct smd_channel), GFP_KERNEL);
1843 if (ch == 0) {
1844 pr_err("%s: out of memory\n", __func__);
1845 return -1;
1846 }
1847 ch->n = SMD_LOOPBACK_CID;
1848
1849 ch->send = &smd_loopback_ctl;
1850 ch->recv = &smd_loopback_ctl;
1851 ch->send_data = smd_loopback_data;
1852 ch->recv_data = smd_loopback_data;
1853 ch->fifo_size = SMD_BUF_SIZE;
1854
1855 ch->fifo_mask = ch->fifo_size - 1;
1856 ch->type = SMD_LOOPBACK_TYPE;
1857 ch->notify_other_cpu = notify_loopback_smd;
1858
1859 ch->read = smd_stream_read;
1860 ch->write = smd_stream_write;
1861 ch->read_avail = smd_stream_read_avail;
1862 ch->write_avail = smd_stream_write_avail;
1863 ch->update_state = update_stream_state;
1864 ch->read_from_cb = smd_stream_read;
1865
1866 memset(ch->name, 0, 20);
1867 memcpy(ch->name, "local_loopback", 14);
1868
1869 ch->pdev.name = ch->name;
1870 ch->pdev.id = ch->type;
1871
1872 SMD_INFO("%s: '%s' cid=%d\n", __func__, ch->name, ch->n);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001873
1874 mutex_lock(&smd_creation_mutex);
1875 list_add(&ch->ch_list, &smd_ch_closed_list);
1876 mutex_unlock(&smd_creation_mutex);
1877
1878 platform_device_register(&ch->pdev);
Brian Swetland34f719b2009-10-30 16:22:05 -07001879 return 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001880}
1881
1882static void do_nothing_notify(void *priv, unsigned flags)
1883{
1884}
1885
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001886static void finalize_channel_close_fn(struct work_struct *work)
1887{
1888 unsigned long flags;
1889 struct smd_channel *ch;
1890 struct smd_channel *index;
1891
Eric Holmbergbb2b1fa2011-10-12 16:41:37 -06001892 mutex_lock(&smd_creation_mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001893 spin_lock_irqsave(&smd_lock, flags);
1894 list_for_each_entry_safe(ch, index, &smd_ch_to_close_list, ch_list) {
1895 list_del(&ch->ch_list);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001896 list_add(&ch->ch_list, &smd_ch_closed_list);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001897 ch->notify(ch->priv, SMD_EVENT_REOPEN_READY);
1898 ch->notify = do_nothing_notify;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001899 }
1900 spin_unlock_irqrestore(&smd_lock, flags);
Eric Holmbergbb2b1fa2011-10-12 16:41:37 -06001901 mutex_unlock(&smd_creation_mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001902}
1903
1904struct smd_channel *smd_get_channel(const char *name, uint32_t type)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001905{
1906 struct smd_channel *ch;
1907
1908 mutex_lock(&smd_creation_mutex);
1909 list_for_each_entry(ch, &smd_ch_closed_list, ch_list) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001910 if (!strcmp(name, ch->name) &&
1911 (type == ch->type)) {
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001912 list_del(&ch->ch_list);
1913 mutex_unlock(&smd_creation_mutex);
1914 return ch;
1915 }
1916 }
1917 mutex_unlock(&smd_creation_mutex);
1918
1919 return NULL;
1920}
1921
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001922int smd_named_open_on_edge(const char *name, uint32_t edge,
1923 smd_channel_t **_ch,
1924 void *priv, void (*notify)(void *, unsigned))
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001925{
1926 struct smd_channel *ch;
1927 unsigned long flags;
1928
1929 if (smd_initialized == 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001930 SMD_INFO("smd_open() before smd_init()\n");
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001931 return -ENODEV;
1932 }
1933
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001934 SMD_DBG("smd_open('%s', %p, %p)\n", name, priv, notify);
1935
1936 ch = smd_get_channel(name, edge);
Eric Holmbergbb2b1fa2011-10-12 16:41:37 -06001937 if (!ch) {
Eric Holmbergbb2b1fa2011-10-12 16:41:37 -06001938 /* check closing list for port */
1939 spin_lock_irqsave(&smd_lock, flags);
1940 list_for_each_entry(ch, &smd_ch_closing_list, ch_list) {
1941 if (!strncmp(name, ch->name, 20) &&
1942 (edge == ch->type)) {
1943 /* channel exists, but is being closed */
1944 spin_unlock_irqrestore(&smd_lock, flags);
1945 return -EAGAIN;
1946 }
1947 }
1948
1949 /* check closing workqueue list for port */
1950 list_for_each_entry(ch, &smd_ch_to_close_list, ch_list) {
1951 if (!strncmp(name, ch->name, 20) &&
1952 (edge == ch->type)) {
1953 /* channel exists, but is being closed */
1954 spin_unlock_irqrestore(&smd_lock, flags);
1955 return -EAGAIN;
1956 }
1957 }
1958 spin_unlock_irqrestore(&smd_lock, flags);
1959
1960 /* one final check to handle closing->closed race condition */
1961 ch = smd_get_channel(name, edge);
1962 if (!ch)
1963 return -ENODEV;
1964 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001965
1966 if (notify == 0)
1967 notify = do_nothing_notify;
1968
1969 ch->notify = notify;
1970 ch->current_packet = 0;
1971 ch->last_state = SMD_SS_CLOSED;
1972 ch->priv = priv;
1973
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001974 if (edge == SMD_LOOPBACK_TYPE) {
1975 ch->last_state = SMD_SS_OPENED;
Jeff Hugo918b2dc2012-03-21 13:42:09 -06001976 ch->half_ch->set_state(ch->send, SMD_SS_OPENED);
1977 ch->half_ch->set_fDSR(ch->send, 1);
1978 ch->half_ch->set_fCTS(ch->send, 1);
1979 ch->half_ch->set_fCD(ch->send, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001980 }
1981
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001982 *_ch = ch;
1983
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001984 SMD_DBG("smd_open: opening '%s'\n", ch->name);
1985
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001986 spin_lock_irqsave(&smd_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001987 if (SMD_CHANNEL_TYPE(ch->type) == SMD_APPS_MODEM)
Brian Swetland37521a32009-07-01 18:30:47 -07001988 list_add(&ch->ch_list, &smd_ch_list_modem);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001989 else if (SMD_CHANNEL_TYPE(ch->type) == SMD_APPS_QDSP)
Brian Swetland37521a32009-07-01 18:30:47 -07001990 list_add(&ch->ch_list, &smd_ch_list_dsp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001991 else if (SMD_CHANNEL_TYPE(ch->type) == SMD_APPS_DSPS)
1992 list_add(&ch->ch_list, &smd_ch_list_dsps);
1993 else if (SMD_CHANNEL_TYPE(ch->type) == SMD_APPS_WCNSS)
1994 list_add(&ch->ch_list, &smd_ch_list_wcnss);
Jeff Hugo918b2dc2012-03-21 13:42:09 -06001995 else if (SMD_CHANNEL_TYPE(ch->type) == SMD_APPS_RPM)
1996 list_add(&ch->ch_list, &smd_ch_list_rpm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001997 else
1998 list_add(&ch->ch_list, &smd_ch_list_loopback);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07001999
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002000 SMD_DBG("%s: opening ch %d\n", __func__, ch->n);
2001
2002 if (edge != SMD_LOOPBACK_TYPE)
2003 smd_state_change(ch, ch->last_state, SMD_SS_OPENING);
2004
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002005 spin_unlock_irqrestore(&smd_lock, flags);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002006
2007 return 0;
2008}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002009EXPORT_SYMBOL(smd_named_open_on_edge);
2010
2011
2012int smd_open(const char *name, smd_channel_t **_ch,
2013 void *priv, void (*notify)(void *, unsigned))
2014{
2015 return smd_named_open_on_edge(name, SMD_APPS_MODEM, _ch, priv,
2016 notify);
2017}
2018EXPORT_SYMBOL(smd_open);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002019
2020int smd_close(smd_channel_t *ch)
2021{
2022 unsigned long flags;
2023
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002024 if (ch == 0)
2025 return -1;
2026
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002027 SMD_INFO("smd_close(%s)\n", ch->name);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002028
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002029 spin_lock_irqsave(&smd_lock, flags);
2030 list_del(&ch->ch_list);
2031 if (ch->n == SMD_LOOPBACK_CID) {
Jeff Hugo918b2dc2012-03-21 13:42:09 -06002032 ch->half_ch->set_fDSR(ch->send, 0);
2033 ch->half_ch->set_fCTS(ch->send, 0);
2034 ch->half_ch->set_fCD(ch->send, 0);
2035 ch->half_ch->set_state(ch->send, SMD_SS_CLOSED);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002036 } else
2037 ch_set_state(ch, SMD_SS_CLOSED);
2038
Jeff Hugo918b2dc2012-03-21 13:42:09 -06002039 if (ch->half_ch->get_state(ch->recv) == SMD_SS_OPENED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002040 list_add(&ch->ch_list, &smd_ch_closing_list);
2041 spin_unlock_irqrestore(&smd_lock, flags);
2042 } else {
2043 spin_unlock_irqrestore(&smd_lock, flags);
2044 ch->notify = do_nothing_notify;
2045 mutex_lock(&smd_creation_mutex);
2046 list_add(&ch->ch_list, &smd_ch_closed_list);
2047 mutex_unlock(&smd_creation_mutex);
2048 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002049
2050 return 0;
2051}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002052EXPORT_SYMBOL(smd_close);
2053
2054int smd_write_start(smd_channel_t *ch, int len)
2055{
2056 int ret;
2057 unsigned hdr[5];
2058
2059 if (!ch) {
2060 pr_err("%s: Invalid channel specified\n", __func__);
2061 return -ENODEV;
2062 }
2063 if (!ch->is_pkt_ch) {
2064 pr_err("%s: non-packet channel specified\n", __func__);
2065 return -EACCES;
2066 }
2067 if (len < 1) {
2068 pr_err("%s: invalid length: %d\n", __func__, len);
2069 return -EINVAL;
2070 }
2071
2072 if (ch->pending_pkt_sz) {
2073 pr_err("%s: packet of size: %d in progress\n", __func__,
2074 ch->pending_pkt_sz);
2075 return -EBUSY;
2076 }
2077 ch->pending_pkt_sz = len;
2078
2079 if (smd_stream_write_avail(ch) < (SMD_HEADER_SIZE)) {
2080 ch->pending_pkt_sz = 0;
2081 SMD_DBG("%s: no space to write packet header\n", __func__);
2082 return -EAGAIN;
2083 }
2084
2085 hdr[0] = len;
2086 hdr[1] = hdr[2] = hdr[3] = hdr[4] = 0;
2087
2088
2089 ret = smd_stream_write(ch, hdr, sizeof(hdr), 0);
2090 if (ret < 0 || ret != sizeof(hdr)) {
2091 ch->pending_pkt_sz = 0;
2092 pr_err("%s: packet header failed to write\n", __func__);
2093 return -EPERM;
2094 }
2095 return 0;
2096}
2097EXPORT_SYMBOL(smd_write_start);
2098
2099int smd_write_segment(smd_channel_t *ch, void *data, int len, int user_buf)
2100{
2101 int bytes_written;
2102
2103 if (!ch) {
2104 pr_err("%s: Invalid channel specified\n", __func__);
2105 return -ENODEV;
2106 }
2107 if (len < 1) {
2108 pr_err("%s: invalid length: %d\n", __func__, len);
2109 return -EINVAL;
2110 }
2111
2112 if (!ch->pending_pkt_sz) {
2113 pr_err("%s: no transaction in progress\n", __func__);
2114 return -ENOEXEC;
2115 }
2116 if (ch->pending_pkt_sz - len < 0) {
2117 pr_err("%s: segment of size: %d will make packet go over "
2118 "length\n", __func__, len);
2119 return -EINVAL;
2120 }
2121
2122 bytes_written = smd_stream_write(ch, data, len, user_buf);
2123
2124 ch->pending_pkt_sz -= bytes_written;
2125
2126 return bytes_written;
2127}
2128EXPORT_SYMBOL(smd_write_segment);
2129
2130int smd_write_end(smd_channel_t *ch)
2131{
2132
2133 if (!ch) {
2134 pr_err("%s: Invalid channel specified\n", __func__);
2135 return -ENODEV;
2136 }
2137 if (ch->pending_pkt_sz) {
2138 pr_err("%s: current packet not completely written\n", __func__);
2139 return -E2BIG;
2140 }
2141
2142 return 0;
2143}
2144EXPORT_SYMBOL(smd_write_end);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002145
Jeff Hugo44fd9832013-04-04 15:56:21 -06002146int smd_write_segment_avail(smd_channel_t *ch)
2147{
2148 int n;
2149
2150 if (!ch) {
2151 pr_err("%s: Invalid channel specified\n", __func__);
2152 return -ENODEV;
2153 }
2154 if (!ch->is_pkt_ch) {
2155 pr_err("%s: non-packet channel specified\n", __func__);
2156 return -ENODEV;
2157 }
2158
2159 n = smd_stream_write_avail(ch);
2160
2161 /* pkt hdr already written, no need to reserve space for it */
2162 if (ch->pending_pkt_sz)
2163 return n;
2164
2165 return n > SMD_HEADER_SIZE ? n - SMD_HEADER_SIZE : 0;
2166}
2167EXPORT_SYMBOL(smd_write_segment_avail);
2168
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002169int smd_read(smd_channel_t *ch, void *data, int len)
2170{
Jack Pham1b236d12012-03-19 15:27:18 -07002171 if (!ch) {
2172 pr_err("%s: Invalid channel specified\n", __func__);
2173 return -ENODEV;
2174 }
2175
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002176 return ch->read(ch, data, len, 0);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002177}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002178EXPORT_SYMBOL(smd_read);
2179
2180int smd_read_user_buffer(smd_channel_t *ch, void *data, int len)
2181{
Jack Pham1b236d12012-03-19 15:27:18 -07002182 if (!ch) {
2183 pr_err("%s: Invalid channel specified\n", __func__);
2184 return -ENODEV;
2185 }
2186
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002187 return ch->read(ch, data, len, 1);
2188}
2189EXPORT_SYMBOL(smd_read_user_buffer);
2190
2191int smd_read_from_cb(smd_channel_t *ch, void *data, int len)
2192{
Jack Pham1b236d12012-03-19 15:27:18 -07002193 if (!ch) {
2194 pr_err("%s: Invalid channel specified\n", __func__);
2195 return -ENODEV;
2196 }
2197
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002198 return ch->read_from_cb(ch, data, len, 0);
2199}
2200EXPORT_SYMBOL(smd_read_from_cb);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002201
2202int smd_write(smd_channel_t *ch, const void *data, int len)
2203{
Jack Pham1b236d12012-03-19 15:27:18 -07002204 if (!ch) {
2205 pr_err("%s: Invalid channel specified\n", __func__);
2206 return -ENODEV;
2207 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002208
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002209 return ch->pending_pkt_sz ? -EBUSY : ch->write(ch, data, len, 0);
Brian Swetland636eb9c2009-12-07 15:28:08 -08002210}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002211EXPORT_SYMBOL(smd_write);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002212
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002213int smd_write_user_buffer(smd_channel_t *ch, const void *data, int len)
Brian Swetland636eb9c2009-12-07 15:28:08 -08002214{
Jack Pham1b236d12012-03-19 15:27:18 -07002215 if (!ch) {
2216 pr_err("%s: Invalid channel specified\n", __func__);
2217 return -ENODEV;
2218 }
2219
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002220 return ch->pending_pkt_sz ? -EBUSY : ch->write(ch, data, len, 1);
Brian Swetland636eb9c2009-12-07 15:28:08 -08002221}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002222EXPORT_SYMBOL(smd_write_user_buffer);
Brian Swetland636eb9c2009-12-07 15:28:08 -08002223
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002224int smd_read_avail(smd_channel_t *ch)
2225{
Jack Pham1b236d12012-03-19 15:27:18 -07002226 if (!ch) {
2227 pr_err("%s: Invalid channel specified\n", __func__);
2228 return -ENODEV;
2229 }
2230
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002231 return ch->read_avail(ch);
2232}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002233EXPORT_SYMBOL(smd_read_avail);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002234
2235int smd_write_avail(smd_channel_t *ch)
2236{
Jack Pham1b236d12012-03-19 15:27:18 -07002237 if (!ch) {
2238 pr_err("%s: Invalid channel specified\n", __func__);
2239 return -ENODEV;
2240 }
2241
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002242 return ch->write_avail(ch);
2243}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002244EXPORT_SYMBOL(smd_write_avail);
2245
2246void smd_enable_read_intr(smd_channel_t *ch)
2247{
2248 if (ch)
Jeff Hugo918b2dc2012-03-21 13:42:09 -06002249 ch->half_ch->set_fBLOCKREADINTR(ch->send, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002250}
2251EXPORT_SYMBOL(smd_enable_read_intr);
2252
2253void smd_disable_read_intr(smd_channel_t *ch)
2254{
2255 if (ch)
Jeff Hugo918b2dc2012-03-21 13:42:09 -06002256 ch->half_ch->set_fBLOCKREADINTR(ch->send, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002257}
2258EXPORT_SYMBOL(smd_disable_read_intr);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002259
Eric Holmbergdeace152012-07-25 12:17:11 -06002260/**
2261 * Enable/disable receive interrupts for the remote processor used by a
2262 * particular channel.
2263 * @ch: open channel handle to use for the edge
2264 * @mask: 1 = mask interrupts; 0 = unmask interrupts
2265 * @returns: 0 for success; < 0 for failure
2266 *
2267 * Note that this enables/disables all interrupts from the remote subsystem for
2268 * all channels. As such, it should be used with care and only for specific
2269 * use cases such as power-collapse sequencing.
2270 */
2271int smd_mask_receive_interrupt(smd_channel_t *ch, bool mask)
2272{
2273 struct irq_chip *irq_chip;
2274 struct irq_data *irq_data;
2275 struct interrupt_config_item *int_cfg;
2276
2277 if (!ch)
2278 return -EINVAL;
2279
2280 if (ch->type >= ARRAY_SIZE(edge_to_pids))
2281 return -ENODEV;
2282
2283 int_cfg = &private_intr_config[edge_to_pids[ch->type].remote_pid].smd;
2284
2285 if (int_cfg->irq_id < 0)
2286 return -ENODEV;
2287
2288 irq_chip = irq_get_chip(int_cfg->irq_id);
2289 if (!irq_chip)
2290 return -ENODEV;
2291
2292 irq_data = irq_get_irq_data(int_cfg->irq_id);
2293 if (!irq_data)
2294 return -ENODEV;
2295
2296 if (mask) {
2297 SMx_POWER_INFO("SMD Masking interrupts from %s\n",
2298 edge_to_pids[ch->type].subsys_name);
2299 irq_chip->irq_mask(irq_data);
2300 } else {
2301 SMx_POWER_INFO("SMD Unmasking interrupts from %s\n",
2302 edge_to_pids[ch->type].subsys_name);
2303 irq_chip->irq_unmask(irq_data);
2304 }
2305
2306 return 0;
2307}
2308EXPORT_SYMBOL(smd_mask_receive_interrupt);
2309
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002310int smd_wait_until_readable(smd_channel_t *ch, int bytes)
2311{
2312 return -1;
2313}
2314
2315int smd_wait_until_writable(smd_channel_t *ch, int bytes)
2316{
2317 return -1;
2318}
2319
2320int smd_cur_packet_size(smd_channel_t *ch)
2321{
Jack Pham1b236d12012-03-19 15:27:18 -07002322 if (!ch) {
2323 pr_err("%s: Invalid channel specified\n", __func__);
2324 return -ENODEV;
2325 }
2326
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002327 return ch->current_packet;
2328}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002329EXPORT_SYMBOL(smd_cur_packet_size);
2330
2331int smd_tiocmget(smd_channel_t *ch)
2332{
Jack Pham1b236d12012-03-19 15:27:18 -07002333 if (!ch) {
2334 pr_err("%s: Invalid channel specified\n", __func__);
2335 return -ENODEV;
2336 }
2337
Jeff Hugo918b2dc2012-03-21 13:42:09 -06002338 return (ch->half_ch->get_fDSR(ch->recv) ? TIOCM_DSR : 0) |
2339 (ch->half_ch->get_fCTS(ch->recv) ? TIOCM_CTS : 0) |
2340 (ch->half_ch->get_fCD(ch->recv) ? TIOCM_CD : 0) |
2341 (ch->half_ch->get_fRI(ch->recv) ? TIOCM_RI : 0) |
2342 (ch->half_ch->get_fCTS(ch->send) ? TIOCM_RTS : 0) |
2343 (ch->half_ch->get_fDSR(ch->send) ? TIOCM_DTR : 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002344}
2345EXPORT_SYMBOL(smd_tiocmget);
2346
Vamsi Krishnacb12a102011-08-17 15:18:26 -07002347/* this api will be called while holding smd_lock */
2348int
2349smd_tiocmset_from_cb(smd_channel_t *ch, unsigned int set, unsigned int clear)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002350{
Jack Pham1b236d12012-03-19 15:27:18 -07002351 if (!ch) {
2352 pr_err("%s: Invalid channel specified\n", __func__);
2353 return -ENODEV;
2354 }
2355
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002356 if (set & TIOCM_DTR)
Jeff Hugo918b2dc2012-03-21 13:42:09 -06002357 ch->half_ch->set_fDSR(ch->send, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002358
2359 if (set & TIOCM_RTS)
Jeff Hugo918b2dc2012-03-21 13:42:09 -06002360 ch->half_ch->set_fCTS(ch->send, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002361
2362 if (clear & TIOCM_DTR)
Jeff Hugo918b2dc2012-03-21 13:42:09 -06002363 ch->half_ch->set_fDSR(ch->send, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002364
2365 if (clear & TIOCM_RTS)
Jeff Hugo918b2dc2012-03-21 13:42:09 -06002366 ch->half_ch->set_fCTS(ch->send, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002367
Jeff Hugo918b2dc2012-03-21 13:42:09 -06002368 ch->half_ch->set_fSTATE(ch->send, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002369 barrier();
Arun Kumar Neelakantam7cffb332013-01-28 15:43:35 +05302370 ch->notify_other_cpu(ch);
Vamsi Krishnacb12a102011-08-17 15:18:26 -07002371
2372 return 0;
2373}
2374EXPORT_SYMBOL(smd_tiocmset_from_cb);
2375
2376int smd_tiocmset(smd_channel_t *ch, unsigned int set, unsigned int clear)
2377{
2378 unsigned long flags;
2379
Jack Pham1b236d12012-03-19 15:27:18 -07002380 if (!ch) {
2381 pr_err("%s: Invalid channel specified\n", __func__);
2382 return -ENODEV;
2383 }
2384
Vamsi Krishnacb12a102011-08-17 15:18:26 -07002385 spin_lock_irqsave(&smd_lock, flags);
2386 smd_tiocmset_from_cb(ch, set, clear);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002387 spin_unlock_irqrestore(&smd_lock, flags);
2388
2389 return 0;
2390}
2391EXPORT_SYMBOL(smd_tiocmset);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002392
Jeff Hugod71d6ac2012-04-11 16:48:07 -06002393int smd_is_pkt_avail(smd_channel_t *ch)
2394{
Jeff Hugoa8549f12012-08-13 20:36:18 -06002395 unsigned long flags;
2396
Jeff Hugod71d6ac2012-04-11 16:48:07 -06002397 if (!ch || !ch->is_pkt_ch)
2398 return -EINVAL;
2399
2400 if (ch->current_packet)
2401 return 1;
2402
Jeff Hugoa8549f12012-08-13 20:36:18 -06002403 spin_lock_irqsave(&smd_lock, flags);
Jeff Hugod71d6ac2012-04-11 16:48:07 -06002404 update_packet_state(ch);
Jeff Hugoa8549f12012-08-13 20:36:18 -06002405 spin_unlock_irqrestore(&smd_lock, flags);
Jeff Hugod71d6ac2012-04-11 16:48:07 -06002406
2407 return ch->current_packet ? 1 : 0;
2408}
2409EXPORT_SYMBOL(smd_is_pkt_avail);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002410
2411
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002412/* -------------------------------------------------------------------------- */
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002413
Eric Holmberg51edef72013-04-11 14:28:33 -06002414/**
2415 * smem_phys_to_virt() - Convert a physical base and offset to virtual address
Jeff Hugobdc734d2012-03-26 16:05:39 -06002416 *
2417 * @base: physical base address to check
2418 * @offset: offset from the base to get the final address
Eric Holmberg51edef72013-04-11 14:28:33 -06002419 * @returns: virtual SMEM address; NULL for failure
2420 *
2421 * Takes a physical address and an offset and checks if the resulting physical
2422 * address would fit into one of the smem regions. If so, returns the
2423 * corresponding virtual address. Otherwise returns NULL.
Jeff Hugobdc734d2012-03-26 16:05:39 -06002424 */
Eric Holmberg51edef72013-04-11 14:28:33 -06002425static void *smem_phys_to_virt(phys_addr_t base, unsigned offset)
Jeff Hugobdc734d2012-03-26 16:05:39 -06002426{
2427 int i;
Stepan Moskovchenkod6ee8262013-02-06 11:26:05 -08002428 phys_addr_t phys_addr;
2429 resource_size_t size;
Jeff Hugobdc734d2012-03-26 16:05:39 -06002430
Eric Holmberg51edef72013-04-11 14:28:33 -06002431 if (OVERFLOW_ADD_UNSIGNED(phys_addr_t, base, offset))
2432 return NULL;
2433
2434 if (!smem_areas) {
2435 /*
2436 * Early boot - no area configuration yet, so default
2437 * to using the main memory region.
2438 *
2439 * To remove the MSM_SHARED_RAM_BASE and the static
2440 * mapping of SMEM in the future, add dump_stack()
2441 * to identify the early callers of smem_get_entry()
2442 * (which calls this function) and replace those calls
2443 * with a new function that knows how to lookup the
2444 * SMEM base address before SMEM has been probed.
2445 */
2446 phys_addr = msm_shared_ram_phys;
2447 size = MSM_SHARED_RAM_SIZE;
2448
2449 if (base >= phys_addr && base + offset < phys_addr + size) {
2450 if (OVERFLOW_ADD_UNSIGNED(uintptr_t,
2451 (uintptr_t)MSM_SHARED_RAM_BASE, offset)) {
2452 pr_err("%s: overflow %p %x\n", __func__,
2453 MSM_SHARED_RAM_BASE, offset);
2454 return NULL;
2455 }
2456
2457 return MSM_SHARED_RAM_BASE + offset;
2458 } else {
2459 return NULL;
2460 }
2461 }
Jeff Hugobdc734d2012-03-26 16:05:39 -06002462 for (i = 0; i < num_smem_areas; ++i) {
2463 phys_addr = smem_areas[i].phys_addr;
2464 size = smem_areas[i].size;
Eric Holmberg51edef72013-04-11 14:28:33 -06002465
2466 if (base < phys_addr || base + offset >= phys_addr + size)
Jeff Hugobdc734d2012-03-26 16:05:39 -06002467 continue;
Eric Holmberg51edef72013-04-11 14:28:33 -06002468
2469 if (OVERFLOW_ADD_UNSIGNED(uintptr_t,
2470 (uintptr_t)smem_areas[i].virt_addr, offset)) {
2471 pr_err("%s: overflow %p %x\n", __func__,
2472 smem_areas[i].virt_addr, offset);
2473 return NULL;
2474 }
2475
2476 return smem_areas[i].virt_addr + offset;
Jeff Hugobdc734d2012-03-26 16:05:39 -06002477 }
2478
2479 return NULL;
2480}
2481
Eric Holmberg51edef72013-04-11 14:28:33 -06002482/**
2483 * smem_virt_to_phys() - Convert SMEM address to physical address.
2484 *
2485 * @smem_address: Address of SMEM item (returned by smem_alloc(), etc)
2486 * @returns: Physical address (or NULL if there is a failure)
2487 *
2488 * This function should only be used if an SMEM item needs to be handed
2489 * off to a DMA engine.
2490 */
2491phys_addr_t smem_virt_to_phys(void *smem_address)
2492{
2493 phys_addr_t phys_addr = 0;
2494 int i;
2495 void *vend;
2496
2497 if (!smem_areas)
2498 return phys_addr;
2499
2500 for (i = 0; i < num_smem_areas; ++i) {
2501 vend = (void *)(smem_areas[i].virt_addr + smem_areas[i].size);
2502
2503 if (smem_address >= smem_areas[i].virt_addr &&
2504 smem_address < vend) {
2505 phys_addr = smem_address - smem_areas[i].virt_addr;
2506 phys_addr += smem_areas[i].phys_addr;
2507 break;
2508 }
2509 }
2510
2511 return phys_addr;
2512}
2513EXPORT_SYMBOL(smem_virt_to_phys);
2514
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002515/* smem_alloc returns the pointer to smem item if it is already allocated.
2516 * Otherwise, it returns NULL.
2517 */
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002518void *smem_alloc(unsigned id, unsigned size)
2519{
2520 return smem_find(id, size);
2521}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002522EXPORT_SYMBOL(smem_alloc);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002523
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002524/* smem_alloc2 returns the pointer to smem item. If it is not allocated,
2525 * it allocates it and then returns the pointer to it.
2526 */
Angshuman Sarkar4eade0d2011-08-17 14:06:23 +05302527void *smem_alloc2(unsigned id, unsigned size_in)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002528{
2529 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
2530 struct smem_heap_entry *toc = shared->heap_toc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002531 unsigned long flags;
2532 void *ret = NULL;
2533
2534 if (!shared->heap_info.initialized) {
2535 pr_err("%s: smem heap info not initialized\n", __func__);
2536 return NULL;
2537 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002538
2539 if (id >= SMEM_NUM_ITEMS)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002540 return NULL;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002541
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002542 size_in = ALIGN(size_in, 8);
2543 remote_spin_lock_irqsave(&remote_spinlock, flags);
2544 if (toc[id].allocated) {
2545 SMD_DBG("%s: %u already allocated\n", __func__, id);
2546 if (size_in != toc[id].size)
2547 pr_err("%s: wrong size %u (expected %u)\n",
2548 __func__, toc[id].size, size_in);
2549 else
2550 ret = (void *)(MSM_SHARED_RAM_BASE + toc[id].offset);
2551 } else if (id > SMEM_FIXED_ITEM_LAST) {
2552 SMD_DBG("%s: allocating %u\n", __func__, id);
2553 if (shared->heap_info.heap_remaining >= size_in) {
2554 toc[id].offset = shared->heap_info.free_offset;
2555 toc[id].size = size_in;
2556 wmb();
2557 toc[id].allocated = 1;
2558
2559 shared->heap_info.free_offset += size_in;
2560 shared->heap_info.heap_remaining -= size_in;
2561 ret = (void *)(MSM_SHARED_RAM_BASE + toc[id].offset);
2562 } else
2563 pr_err("%s: not enough memory %u (required %u)\n",
2564 __func__, shared->heap_info.heap_remaining,
2565 size_in);
2566 }
2567 wmb();
2568 remote_spin_unlock_irqrestore(&remote_spinlock, flags);
2569 return ret;
2570}
Angshuman Sarkar4eade0d2011-08-17 14:06:23 +05302571EXPORT_SYMBOL(smem_alloc2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002572
2573void *smem_get_entry(unsigned id, unsigned *size)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002574{
2575 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
2576 struct smem_heap_entry *toc = shared->heap_toc;
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +05302577 int use_spinlocks = spinlocks_initialized;
2578 void *ret = 0;
2579 unsigned long flags = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002580
2581 if (id >= SMEM_NUM_ITEMS)
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +05302582 return ret;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002583
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +05302584 if (use_spinlocks)
2585 remote_spin_lock_irqsave(&remote_spinlock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002586 /* toc is in device memory and cannot be speculatively accessed */
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002587 if (toc[id].allocated) {
Eric Holmberg51edef72013-04-11 14:28:33 -06002588 phys_addr_t phys_base;
2589
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002590 *size = toc[id].size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002591 barrier();
Eric Holmberg51edef72013-04-11 14:28:33 -06002592
2593 phys_base = toc[id].reserved & BASE_ADDR_MASK;
2594 if (!phys_base)
2595 phys_base = (phys_addr_t)msm_shared_ram_phys;
2596 ret = smem_phys_to_virt(phys_base, toc[id].offset);
Brian Swetland5b0f5a32009-04-26 18:38:49 -07002597 } else {
2598 *size = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002599 }
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +05302600 if (use_spinlocks)
2601 remote_spin_unlock_irqrestore(&remote_spinlock, flags);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002602
Angshuman Sarkar7ee0dca2011-08-22 21:37:34 +05302603 return ret;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002604}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002605EXPORT_SYMBOL(smem_get_entry);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002606
2607void *smem_find(unsigned id, unsigned size_in)
2608{
2609 unsigned size;
2610 void *ptr;
2611
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002612 ptr = smem_get_entry(id, &size);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002613 if (!ptr)
2614 return 0;
2615
2616 size_in = ALIGN(size_in, 8);
2617 if (size_in != size) {
2618 pr_err("smem_find(%d, %d): wrong size %d\n",
2619 id, size_in, size);
2620 return 0;
2621 }
2622
2623 return ptr;
2624}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002625EXPORT_SYMBOL(smem_find);
2626
2627static int smsm_cb_init(void)
2628{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002629 struct smsm_state_info *state_info;
2630 int n;
2631 int ret = 0;
2632
2633 smsm_states = kmalloc(sizeof(struct smsm_state_info)*SMSM_NUM_ENTRIES,
2634 GFP_KERNEL);
2635
2636 if (!smsm_states) {
2637 pr_err("%s: SMSM init failed\n", __func__);
2638 return -ENOMEM;
2639 }
2640
Karthikeyan Ramasubramanian3249a222012-04-18 17:16:49 -06002641 smsm_cb_wq = create_singlethread_workqueue("smsm_cb_wq");
2642 if (!smsm_cb_wq) {
2643 pr_err("%s: smsm_cb_wq creation failed\n", __func__);
2644 kfree(smsm_states);
2645 return -EFAULT;
2646 }
2647
Eric Holmbergc8002902011-09-16 13:55:57 -06002648 mutex_lock(&smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002649 for (n = 0; n < SMSM_NUM_ENTRIES; n++) {
2650 state_info = &smsm_states[n];
2651 state_info->last_value = __raw_readl(SMSM_STATE_ADDR(n));
Eric Holmberge8a39322012-04-03 15:14:02 -06002652 state_info->intr_mask_set = 0x0;
2653 state_info->intr_mask_clear = 0x0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002654 INIT_LIST_HEAD(&state_info->callbacks);
2655 }
Eric Holmbergc8002902011-09-16 13:55:57 -06002656 mutex_unlock(&smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002657
2658 return ret;
2659}
2660
2661static int smsm_init(void)
2662{
2663 struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
2664 int i;
2665 struct smsm_size_info_type *smsm_size_info;
Eric Holmberge5266d32013-02-25 18:29:27 -07002666 unsigned long flags;
2667 unsigned long j_start;
2668
2669 /* Verify that remote spinlock is not deadlocked */
2670 j_start = jiffies;
2671 while (!remote_spin_trylock_irqsave(&remote_spinlock, flags)) {
2672 if (jiffies_to_msecs(jiffies - j_start) > RSPIN_INIT_WAIT_MS) {
2673 panic("%s: Remote processor %d will not release spinlock\n",
2674 __func__, remote_spin_owner(&remote_spinlock));
2675 }
2676 }
2677 remote_spin_unlock_irqrestore(&remote_spinlock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002678
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002679 smsm_size_info = smem_alloc(SMEM_SMSM_SIZE_INFO,
2680 sizeof(struct smsm_size_info_type));
2681 if (smsm_size_info) {
2682 SMSM_NUM_ENTRIES = smsm_size_info->num_entries;
2683 SMSM_NUM_HOSTS = smsm_size_info->num_hosts;
2684 }
2685
Eric Holmbergc7e8daf2011-12-28 11:49:21 -07002686 i = kfifo_alloc(&smsm_snapshot_fifo,
2687 sizeof(uint32_t) * SMSM_NUM_ENTRIES * SMSM_SNAPSHOT_CNT,
2688 GFP_KERNEL);
2689 if (i) {
2690 pr_err("%s: SMSM state fifo alloc failed %d\n", __func__, i);
2691 return i;
2692 }
Eric Holmberg59a9f9412012-03-19 10:04:22 -06002693 wake_lock_init(&smsm_snapshot_wakelock, WAKE_LOCK_SUSPEND,
2694 "smsm_snapshot");
Eric Holmbergc7e8daf2011-12-28 11:49:21 -07002695
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002696 if (!smsm_info.state) {
2697 smsm_info.state = smem_alloc2(ID_SHARED_STATE,
2698 SMSM_NUM_ENTRIES *
2699 sizeof(uint32_t));
2700
2701 if (smsm_info.state) {
2702 __raw_writel(0, SMSM_STATE_ADDR(SMSM_APPS_STATE));
2703 if ((shared->version[VERSION_MODEM] >> 16) >= 0xB)
2704 __raw_writel(0, \
2705 SMSM_STATE_ADDR(SMSM_APPS_DEM_I));
2706 }
2707 }
2708
2709 if (!smsm_info.intr_mask) {
2710 smsm_info.intr_mask = smem_alloc2(SMEM_SMSM_CPU_INTR_MASK,
2711 SMSM_NUM_ENTRIES *
2712 SMSM_NUM_HOSTS *
2713 sizeof(uint32_t));
2714
Eric Holmberge8a39322012-04-03 15:14:02 -06002715 if (smsm_info.intr_mask) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002716 for (i = 0; i < SMSM_NUM_ENTRIES; i++)
Eric Holmberge8a39322012-04-03 15:14:02 -06002717 __raw_writel(0x0,
2718 SMSM_INTR_MASK_ADDR(i, SMSM_APPS));
2719
2720 /* Configure legacy modem bits */
2721 __raw_writel(LEGACY_MODEM_SMSM_MASK,
2722 SMSM_INTR_MASK_ADDR(SMSM_MODEM_STATE,
2723 SMSM_APPS));
2724 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002725 }
2726
2727 if (!smsm_info.intr_mux)
2728 smsm_info.intr_mux = smem_alloc2(SMEM_SMD_SMSM_INTR_MUX,
2729 SMSM_NUM_INTR_MUX *
2730 sizeof(uint32_t));
2731
2732 i = smsm_cb_init();
2733 if (i)
2734 return i;
2735
2736 wmb();
Eric Holmberg144c2de2012-10-04 13:37:28 -06002737
2738 smsm_pm_notifier(&smsm_pm_nb, PM_POST_SUSPEND, NULL);
2739 i = register_pm_notifier(&smsm_pm_nb);
2740 if (i)
2741 pr_err("%s: power state notif error %d\n", __func__, i);
2742
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002743 return 0;
2744}
2745
2746void smsm_reset_modem(unsigned mode)
2747{
2748 if (mode == SMSM_SYSTEM_DOWNLOAD) {
2749 mode = SMSM_RESET | SMSM_SYSTEM_DOWNLOAD;
2750 } else if (mode == SMSM_MODEM_WAIT) {
2751 mode = SMSM_RESET | SMSM_MODEM_WAIT;
2752 } else { /* reset_mode is SMSM_RESET or default */
2753 mode = SMSM_RESET;
2754 }
2755
2756 smsm_change_state(SMSM_APPS_STATE, mode, mode);
2757}
2758EXPORT_SYMBOL(smsm_reset_modem);
2759
2760void smsm_reset_modem_cont(void)
2761{
2762 unsigned long flags;
2763 uint32_t state;
2764
2765 if (!smsm_info.state)
2766 return;
2767
2768 spin_lock_irqsave(&smem_lock, flags);
2769 state = __raw_readl(SMSM_STATE_ADDR(SMSM_APPS_STATE)) \
2770 & ~SMSM_MODEM_WAIT;
2771 __raw_writel(state, SMSM_STATE_ADDR(SMSM_APPS_STATE));
2772 wmb();
2773 spin_unlock_irqrestore(&smem_lock, flags);
2774}
2775EXPORT_SYMBOL(smsm_reset_modem_cont);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002776
Eric Holmbergda31d042012-03-28 14:01:02 -06002777static void smsm_cb_snapshot(uint32_t use_wakelock)
Eric Holmbergc7e8daf2011-12-28 11:49:21 -07002778{
2779 int n;
2780 uint32_t new_state;
Eric Holmberg59a9f9412012-03-19 10:04:22 -06002781 unsigned long flags;
Eric Holmbergc7e8daf2011-12-28 11:49:21 -07002782 int ret;
2783
2784 ret = kfifo_avail(&smsm_snapshot_fifo);
Eric Holmbergda31d042012-03-28 14:01:02 -06002785 if (ret < SMSM_SNAPSHOT_SIZE) {
Eric Holmbergc7e8daf2011-12-28 11:49:21 -07002786 pr_err("%s: SMSM snapshot full %d\n", __func__, ret);
2787 return;
2788 }
2789
Eric Holmberg96b55f62012-04-03 19:10:46 -06002790 /*
2791 * To avoid a race condition with notify_smsm_cb_clients_worker, the
2792 * following sequence must be followed:
2793 * 1) increment snapshot count
2794 * 2) insert data into FIFO
2795 *
2796 * Potentially in parallel, the worker:
2797 * a) verifies >= 1 snapshots are in FIFO
2798 * b) processes snapshot
2799 * c) decrements reference count
2800 *
2801 * This order ensures that 1 will always occur before abc.
2802 */
Eric Holmbergda31d042012-03-28 14:01:02 -06002803 if (use_wakelock) {
2804 spin_lock_irqsave(&smsm_snapshot_count_lock, flags);
2805 if (smsm_snapshot_count == 0) {
2806 SMx_POWER_INFO("SMSM snapshot wake lock\n");
2807 wake_lock(&smsm_snapshot_wakelock);
2808 }
2809 ++smsm_snapshot_count;
2810 spin_unlock_irqrestore(&smsm_snapshot_count_lock, flags);
2811 }
Eric Holmberg96b55f62012-04-03 19:10:46 -06002812
2813 /* queue state entries */
2814 for (n = 0; n < SMSM_NUM_ENTRIES; n++) {
2815 new_state = __raw_readl(SMSM_STATE_ADDR(n));
2816
2817 ret = kfifo_in(&smsm_snapshot_fifo,
2818 &new_state, sizeof(new_state));
2819 if (ret != sizeof(new_state)) {
2820 pr_err("%s: SMSM snapshot failure %d\n", __func__, ret);
2821 goto restore_snapshot_count;
2822 }
2823 }
2824
2825 /* queue wakelock usage flag */
2826 ret = kfifo_in(&smsm_snapshot_fifo,
2827 &use_wakelock, sizeof(use_wakelock));
2828 if (ret != sizeof(use_wakelock)) {
2829 pr_err("%s: SMSM snapshot failure %d\n", __func__, ret);
2830 goto restore_snapshot_count;
2831 }
2832
Karthikeyan Ramasubramanian3249a222012-04-18 17:16:49 -06002833 queue_work(smsm_cb_wq, &smsm_cb_work);
Eric Holmberg96b55f62012-04-03 19:10:46 -06002834 return;
2835
2836restore_snapshot_count:
2837 if (use_wakelock) {
2838 spin_lock_irqsave(&smsm_snapshot_count_lock, flags);
2839 if (smsm_snapshot_count) {
2840 --smsm_snapshot_count;
2841 if (smsm_snapshot_count == 0) {
2842 SMx_POWER_INFO("SMSM snapshot wake unlock\n");
2843 wake_unlock(&smsm_snapshot_wakelock);
2844 }
2845 } else {
2846 pr_err("%s: invalid snapshot count\n", __func__);
2847 }
2848 spin_unlock_irqrestore(&smsm_snapshot_count_lock, flags);
2849 }
Eric Holmbergc7e8daf2011-12-28 11:49:21 -07002850}
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002851
2852static irqreturn_t smsm_irq_handler(int irq, void *data)
2853{
2854 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002855
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002856 if (irq == INT_ADSP_A11_SMSM) {
Eric Holmberg6282c5d2011-10-27 17:30:57 -06002857 uint32_t mux_val;
2858 static uint32_t prev_smem_q6_apps_smsm;
2859
2860 if (smsm_info.intr_mux && cpu_is_qsd8x50()) {
2861 mux_val = __raw_readl(
2862 SMSM_INTR_MUX_ADDR(SMEM_Q6_APPS_SMSM));
2863 if (mux_val != prev_smem_q6_apps_smsm)
2864 prev_smem_q6_apps_smsm = mux_val;
2865 }
2866
Eric Holmbergc7e8daf2011-12-28 11:49:21 -07002867 spin_lock_irqsave(&smem_lock, flags);
Eric Holmbergda31d042012-03-28 14:01:02 -06002868 smsm_cb_snapshot(1);
Eric Holmbergc7e8daf2011-12-28 11:49:21 -07002869 spin_unlock_irqrestore(&smem_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002870 return IRQ_HANDLED;
2871 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002872
2873 spin_lock_irqsave(&smem_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002874 if (!smsm_info.state) {
2875 SMSM_INFO("<SM NO STATE>\n");
2876 } else {
2877 unsigned old_apps, apps;
2878 unsigned modm = __raw_readl(SMSM_STATE_ADDR(SMSM_MODEM_STATE));
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002879
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002880 old_apps = apps = __raw_readl(SMSM_STATE_ADDR(SMSM_APPS_STATE));
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002881
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002882 SMSM_DBG("<SM %08x %08x>\n", apps, modm);
2883 if (apps & SMSM_RESET) {
2884 /* If we get an interrupt and the apps SMSM_RESET
2885 bit is already set, the modem is acking the
2886 app's reset ack. */
Eric Holmberg2bb6ccd2012-03-13 13:05:14 -06002887 if (!disable_smsm_reset_handshake)
Angshuman Sarkaread67bd2011-09-21 20:13:12 +05302888 apps &= ~SMSM_RESET;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002889 /* Issue a fake irq to handle any
2890 * smd state changes during reset
2891 */
2892 smd_fake_irq_handler(0);
Daniel Walker79848a22010-03-16 15:20:07 -07002893
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002894 /* queue modem restart notify chain */
2895 modem_queue_start_reset_notify();
Brian Swetland5b0f5a32009-04-26 18:38:49 -07002896
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002897 } else if (modm & SMSM_RESET) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002898 pr_err("\nSMSM: Modem SMSM state changed to SMSM_RESET.");
Ram Somani8b9589f2012-04-03 12:07:18 +05302899 if (!disable_smsm_reset_handshake) {
2900 apps |= SMSM_RESET;
2901 flush_cache_all();
2902 outer_flush_all();
2903 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002904 modem_queue_start_reset_notify();
2905
2906 } else if (modm & SMSM_INIT) {
2907 if (!(apps & SMSM_INIT)) {
2908 apps |= SMSM_INIT;
2909 modem_queue_smsm_init_notify();
2910 }
2911
2912 if (modm & SMSM_SMDINIT)
2913 apps |= SMSM_SMDINIT;
2914 if ((apps & (SMSM_INIT | SMSM_SMDINIT | SMSM_RPCINIT)) ==
2915 (SMSM_INIT | SMSM_SMDINIT | SMSM_RPCINIT))
2916 apps |= SMSM_RUN;
2917 } else if (modm & SMSM_SYSTEM_DOWNLOAD) {
2918 pr_err("\nSMSM: Modem SMSM state changed to SMSM_SYSTEM_DOWNLOAD.");
2919 modem_queue_start_reset_notify();
2920 }
2921
2922 if (old_apps != apps) {
2923 SMSM_DBG("<SM %08x NOTIFY>\n", apps);
2924 __raw_writel(apps, SMSM_STATE_ADDR(SMSM_APPS_STATE));
2925 do_smd_probe();
2926 notify_other_smsm(SMSM_APPS_STATE, (old_apps ^ apps));
2927 }
2928
Eric Holmbergda31d042012-03-28 14:01:02 -06002929 smsm_cb_snapshot(1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002930 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002931 spin_unlock_irqrestore(&smem_lock, flags);
2932 return IRQ_HANDLED;
2933}
2934
Eric Holmberg98c6c642012-02-24 11:29:35 -07002935static irqreturn_t smsm_modem_irq_handler(int irq, void *data)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002936{
Eric Holmberg98c6c642012-02-24 11:29:35 -07002937 SMx_POWER_INFO("SMSM Int Modem->Apps\n");
Eric Holmberg7ad623a2012-03-01 14:41:10 -07002938 ++interrupt_stats[SMD_MODEM].smsm_in_count;
Eric Holmberg98c6c642012-02-24 11:29:35 -07002939 return smsm_irq_handler(irq, data);
2940}
Brian Swetland5b0f5a32009-04-26 18:38:49 -07002941
Eric Holmberg98c6c642012-02-24 11:29:35 -07002942static irqreturn_t smsm_dsp_irq_handler(int irq, void *data)
2943{
2944 SMx_POWER_INFO("SMSM Int LPASS->Apps\n");
Eric Holmberg7ad623a2012-03-01 14:41:10 -07002945 ++interrupt_stats[SMD_Q6].smsm_in_count;
Eric Holmberg98c6c642012-02-24 11:29:35 -07002946 return smsm_irq_handler(irq, data);
2947}
2948
2949static irqreturn_t smsm_dsps_irq_handler(int irq, void *data)
2950{
2951 SMx_POWER_INFO("SMSM Int DSPS->Apps\n");
Eric Holmberg7ad623a2012-03-01 14:41:10 -07002952 ++interrupt_stats[SMD_DSPS].smsm_in_count;
Eric Holmberg98c6c642012-02-24 11:29:35 -07002953 return smsm_irq_handler(irq, data);
2954}
2955
2956static irqreturn_t smsm_wcnss_irq_handler(int irq, void *data)
2957{
2958 SMx_POWER_INFO("SMSM Int WCNSS->Apps\n");
Eric Holmberg7ad623a2012-03-01 14:41:10 -07002959 ++interrupt_stats[SMD_WCNSS].smsm_in_count;
Eric Holmberg98c6c642012-02-24 11:29:35 -07002960 return smsm_irq_handler(irq, data);
2961}
2962
Eric Holmberge8a39322012-04-03 15:14:02 -06002963/*
2964 * Changes the global interrupt mask. The set and clear masks are re-applied
2965 * every time the global interrupt mask is updated for callback registration
2966 * and de-registration.
2967 *
2968 * The clear mask is applied first, so if a bit is set to 1 in both the clear
2969 * mask and the set mask, the result will be that the interrupt is set.
2970 *
2971 * @smsm_entry SMSM entry to change
2972 * @clear_mask 1 = clear bit, 0 = no-op
2973 * @set_mask 1 = set bit, 0 = no-op
2974 *
2975 * @returns 0 for success, < 0 for error
2976 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002977int smsm_change_intr_mask(uint32_t smsm_entry,
2978 uint32_t clear_mask, uint32_t set_mask)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002979{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002980 uint32_t old_mask, new_mask;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002981 unsigned long flags;
Brian Swetland5b0f5a32009-04-26 18:38:49 -07002982
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002983 if (smsm_entry >= SMSM_NUM_ENTRIES) {
2984 pr_err("smsm_change_state: Invalid entry %d\n",
2985 smsm_entry);
2986 return -EINVAL;
2987 }
2988
2989 if (!smsm_info.intr_mask) {
2990 pr_err("smsm_change_intr_mask <SM NO STATE>\n");
Brian Swetland5b0f5a32009-04-26 18:38:49 -07002991 return -EIO;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002992 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002993
2994 spin_lock_irqsave(&smem_lock, flags);
Eric Holmberge8a39322012-04-03 15:14:02 -06002995 smsm_states[smsm_entry].intr_mask_clear = clear_mask;
2996 smsm_states[smsm_entry].intr_mask_set = set_mask;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07002997
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002998 old_mask = __raw_readl(SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_APPS));
2999 new_mask = (old_mask & ~clear_mask) | set_mask;
3000 __raw_writel(new_mask, SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_APPS));
Brian Swetland2eb44eb2008-09-29 16:00:48 -07003001
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003002 wmb();
3003 spin_unlock_irqrestore(&smem_lock, flags);
Brian Swetland5b0f5a32009-04-26 18:38:49 -07003004
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003005 return 0;
3006}
3007EXPORT_SYMBOL(smsm_change_intr_mask);
3008
3009int smsm_get_intr_mask(uint32_t smsm_entry, uint32_t *intr_mask)
3010{
3011 if (smsm_entry >= SMSM_NUM_ENTRIES) {
3012 pr_err("smsm_change_state: Invalid entry %d\n",
3013 smsm_entry);
3014 return -EINVAL;
3015 }
3016
3017 if (!smsm_info.intr_mask) {
3018 pr_err("smsm_change_intr_mask <SM NO STATE>\n");
3019 return -EIO;
3020 }
3021
3022 *intr_mask = __raw_readl(SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_APPS));
3023 return 0;
3024}
3025EXPORT_SYMBOL(smsm_get_intr_mask);
3026
3027int smsm_change_state(uint32_t smsm_entry,
3028 uint32_t clear_mask, uint32_t set_mask)
3029{
3030 unsigned long flags;
3031 uint32_t old_state, new_state;
3032
3033 if (smsm_entry >= SMSM_NUM_ENTRIES) {
3034 pr_err("smsm_change_state: Invalid entry %d",
3035 smsm_entry);
3036 return -EINVAL;
3037 }
3038
3039 if (!smsm_info.state) {
3040 pr_err("smsm_change_state <SM NO STATE>\n");
3041 return -EIO;
3042 }
3043 spin_lock_irqsave(&smem_lock, flags);
3044
3045 old_state = __raw_readl(SMSM_STATE_ADDR(smsm_entry));
3046 new_state = (old_state & ~clear_mask) | set_mask;
3047 __raw_writel(new_state, SMSM_STATE_ADDR(smsm_entry));
3048 SMSM_DBG("smsm_change_state %x\n", new_state);
3049 notify_other_smsm(SMSM_APPS_STATE, (old_state ^ new_state));
Brian Swetland2eb44eb2008-09-29 16:00:48 -07003050
3051 spin_unlock_irqrestore(&smem_lock, flags);
3052
Brian Swetland2eb44eb2008-09-29 16:00:48 -07003053 return 0;
3054}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003055EXPORT_SYMBOL(smsm_change_state);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07003056
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003057uint32_t smsm_get_state(uint32_t smsm_entry)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07003058{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003059 uint32_t rv = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07003060
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003061 /* needs interface change to return error code */
3062 if (smsm_entry >= SMSM_NUM_ENTRIES) {
3063 pr_err("smsm_change_state: Invalid entry %d",
3064 smsm_entry);
3065 return 0;
3066 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07003067
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003068 if (!smsm_info.state) {
3069 pr_err("smsm_get_state <SM NO STATE>\n");
3070 } else {
3071 rv = __raw_readl(SMSM_STATE_ADDR(smsm_entry));
3072 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07003073
Brian Swetland2eb44eb2008-09-29 16:00:48 -07003074 return rv;
3075}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003076EXPORT_SYMBOL(smsm_get_state);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07003077
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003078/**
3079 * Performs SMSM callback client notifiction.
3080 */
3081void notify_smsm_cb_clients_worker(struct work_struct *work)
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -07003082{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003083 struct smsm_state_cb_info *cb_info;
3084 struct smsm_state_info *state_info;
3085 int n;
3086 uint32_t new_state;
3087 uint32_t state_changes;
Eric Holmbergda31d042012-03-28 14:01:02 -06003088 uint32_t use_wakelock;
Eric Holmbergc7e8daf2011-12-28 11:49:21 -07003089 int ret;
Eric Holmberg59a9f9412012-03-19 10:04:22 -06003090 unsigned long flags;
Brian Swetland03e00cd2009-07-01 17:58:37 -07003091
Eric Holmbergc7e8daf2011-12-28 11:49:21 -07003092 if (!smd_initialized)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003093 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003094
Eric Holmbergda31d042012-03-28 14:01:02 -06003095 while (kfifo_len(&smsm_snapshot_fifo) >= SMSM_SNAPSHOT_SIZE) {
Eric Holmbergc7e8daf2011-12-28 11:49:21 -07003096 mutex_lock(&smsm_lock);
3097 for (n = 0; n < SMSM_NUM_ENTRIES; n++) {
3098 state_info = &smsm_states[n];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003099
Eric Holmbergc7e8daf2011-12-28 11:49:21 -07003100 ret = kfifo_out(&smsm_snapshot_fifo, &new_state,
3101 sizeof(new_state));
3102 if (ret != sizeof(new_state)) {
3103 pr_err("%s: snapshot underflow %d\n",
3104 __func__, ret);
3105 mutex_unlock(&smsm_lock);
3106 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003107 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003108
Eric Holmbergc7e8daf2011-12-28 11:49:21 -07003109 state_changes = state_info->last_value ^ new_state;
3110 if (state_changes) {
Eric Holmberg98c6c642012-02-24 11:29:35 -07003111 SMx_POWER_INFO("SMSM Change %d: %08x->%08x\n",
3112 n, state_info->last_value,
3113 new_state);
Eric Holmbergc7e8daf2011-12-28 11:49:21 -07003114 list_for_each_entry(cb_info,
3115 &state_info->callbacks, cb_list) {
3116
3117 if (cb_info->mask & state_changes)
3118 cb_info->notify(cb_info->data,
3119 state_info->last_value,
3120 new_state);
3121 }
3122 state_info->last_value = new_state;
3123 }
3124 }
Eric Holmberg59a9f9412012-03-19 10:04:22 -06003125
Eric Holmbergda31d042012-03-28 14:01:02 -06003126 /* read wakelock flag */
3127 ret = kfifo_out(&smsm_snapshot_fifo, &use_wakelock,
3128 sizeof(use_wakelock));
3129 if (ret != sizeof(use_wakelock)) {
3130 pr_err("%s: snapshot underflow %d\n",
3131 __func__, ret);
Karthikeyan Ramasubramanian3249a222012-04-18 17:16:49 -06003132 mutex_unlock(&smsm_lock);
Eric Holmbergda31d042012-03-28 14:01:02 -06003133 return;
Eric Holmberg59a9f9412012-03-19 10:04:22 -06003134 }
Karthikeyan Ramasubramanian3249a222012-04-18 17:16:49 -06003135 mutex_unlock(&smsm_lock);
Eric Holmbergda31d042012-03-28 14:01:02 -06003136
3137 if (use_wakelock) {
3138 spin_lock_irqsave(&smsm_snapshot_count_lock, flags);
3139 if (smsm_snapshot_count) {
3140 --smsm_snapshot_count;
3141 if (smsm_snapshot_count == 0) {
3142 SMx_POWER_INFO("SMSM snapshot"
3143 " wake unlock\n");
3144 wake_unlock(&smsm_snapshot_wakelock);
3145 }
3146 } else {
3147 pr_err("%s: invalid snapshot count\n",
3148 __func__);
3149 }
3150 spin_unlock_irqrestore(&smsm_snapshot_count_lock,
3151 flags);
3152 }
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -07003153 }
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -07003154}
3155
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -07003156
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003157/**
3158 * Registers callback for SMSM state notifications when the specified
3159 * bits change.
3160 *
3161 * @smsm_entry Processor entry to deregister
3162 * @mask Bits to deregister (if result is 0, callback is removed)
3163 * @notify Notification function to deregister
3164 * @data Opaque data passed in to callback
3165 *
3166 * @returns Status code
3167 * <0 error code
3168 * 0 inserted new entry
3169 * 1 updated mask of existing entry
3170 */
3171int smsm_state_cb_register(uint32_t smsm_entry, uint32_t mask,
3172 void (*notify)(void *, uint32_t, uint32_t), void *data)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07003173{
Eric Holmberge8a39322012-04-03 15:14:02 -06003174 struct smsm_state_info *state;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003175 struct smsm_state_cb_info *cb_info;
3176 struct smsm_state_cb_info *cb_found = 0;
Eric Holmberge8a39322012-04-03 15:14:02 -06003177 uint32_t new_mask = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003178 int ret = 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07003179
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003180 if (smsm_entry >= SMSM_NUM_ENTRIES)
3181 return -EINVAL;
3182
Eric Holmbergc8002902011-09-16 13:55:57 -06003183 mutex_lock(&smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003184
3185 if (!smsm_states) {
3186 /* smsm not yet initialized */
3187 ret = -ENODEV;
3188 goto cleanup;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07003189 }
Brian Swetland2eb44eb2008-09-29 16:00:48 -07003190
Eric Holmberge8a39322012-04-03 15:14:02 -06003191 state = &smsm_states[smsm_entry];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003192 list_for_each_entry(cb_info,
Eric Holmberge8a39322012-04-03 15:14:02 -06003193 &state->callbacks, cb_list) {
3194 if (!ret && (cb_info->notify == notify) &&
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003195 (cb_info->data == data)) {
3196 cb_info->mask |= mask;
3197 cb_found = cb_info;
3198 ret = 1;
Brian Swetland5b0f5a32009-04-26 18:38:49 -07003199 }
Eric Holmberge8a39322012-04-03 15:14:02 -06003200 new_mask |= cb_info->mask;
Brian Swetland5b0f5a32009-04-26 18:38:49 -07003201 }
3202
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003203 if (!cb_found) {
3204 cb_info = kmalloc(sizeof(struct smsm_state_cb_info),
3205 GFP_ATOMIC);
3206 if (!cb_info) {
3207 ret = -ENOMEM;
3208 goto cleanup;
3209 }
3210
3211 cb_info->mask = mask;
3212 cb_info->notify = notify;
3213 cb_info->data = data;
3214 INIT_LIST_HEAD(&cb_info->cb_list);
3215 list_add_tail(&cb_info->cb_list,
Eric Holmberge8a39322012-04-03 15:14:02 -06003216 &state->callbacks);
3217 new_mask |= mask;
3218 }
3219
3220 /* update interrupt notification mask */
3221 if (smsm_entry == SMSM_MODEM_STATE)
3222 new_mask |= LEGACY_MODEM_SMSM_MASK;
3223
3224 if (smsm_info.intr_mask) {
3225 unsigned long flags;
3226
3227 spin_lock_irqsave(&smem_lock, flags);
3228 new_mask = (new_mask & ~state->intr_mask_clear)
3229 | state->intr_mask_set;
3230 __raw_writel(new_mask,
3231 SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_APPS));
3232 wmb();
3233 spin_unlock_irqrestore(&smem_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003234 }
3235
3236cleanup:
Eric Holmbergc8002902011-09-16 13:55:57 -06003237 mutex_unlock(&smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003238 return ret;
3239}
3240EXPORT_SYMBOL(smsm_state_cb_register);
3241
3242
3243/**
3244 * Deregisters for SMSM state notifications for the specified bits.
3245 *
3246 * @smsm_entry Processor entry to deregister
3247 * @mask Bits to deregister (if result is 0, callback is removed)
3248 * @notify Notification function to deregister
3249 * @data Opaque data passed in to callback
3250 *
3251 * @returns Status code
3252 * <0 error code
3253 * 0 not found
3254 * 1 updated mask
3255 * 2 removed callback
3256 */
3257int smsm_state_cb_deregister(uint32_t smsm_entry, uint32_t mask,
3258 void (*notify)(void *, uint32_t, uint32_t), void *data)
3259{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003260 struct smsm_state_cb_info *cb_info;
Eric Holmberge8a39322012-04-03 15:14:02 -06003261 struct smsm_state_cb_info *cb_tmp;
3262 struct smsm_state_info *state;
3263 uint32_t new_mask = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003264 int ret = 0;
3265
3266 if (smsm_entry >= SMSM_NUM_ENTRIES)
3267 return -EINVAL;
3268
Eric Holmbergc8002902011-09-16 13:55:57 -06003269 mutex_lock(&smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003270
3271 if (!smsm_states) {
3272 /* smsm not yet initialized */
Eric Holmbergc8002902011-09-16 13:55:57 -06003273 mutex_unlock(&smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003274 return -ENODEV;
3275 }
3276
Eric Holmberge8a39322012-04-03 15:14:02 -06003277 state = &smsm_states[smsm_entry];
3278 list_for_each_entry_safe(cb_info, cb_tmp,
3279 &state->callbacks, cb_list) {
3280 if (!ret && (cb_info->notify == notify) &&
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003281 (cb_info->data == data)) {
3282 cb_info->mask &= ~mask;
3283 ret = 1;
3284 if (!cb_info->mask) {
3285 /* no mask bits set, remove callback */
3286 list_del(&cb_info->cb_list);
3287 kfree(cb_info);
3288 ret = 2;
Eric Holmberge8a39322012-04-03 15:14:02 -06003289 continue;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003290 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003291 }
Eric Holmberge8a39322012-04-03 15:14:02 -06003292 new_mask |= cb_info->mask;
3293 }
3294
3295 /* update interrupt notification mask */
3296 if (smsm_entry == SMSM_MODEM_STATE)
3297 new_mask |= LEGACY_MODEM_SMSM_MASK;
3298
3299 if (smsm_info.intr_mask) {
3300 unsigned long flags;
3301
3302 spin_lock_irqsave(&smem_lock, flags);
3303 new_mask = (new_mask & ~state->intr_mask_clear)
3304 | state->intr_mask_set;
3305 __raw_writel(new_mask,
3306 SMSM_INTR_MASK_ADDR(smsm_entry, SMSM_APPS));
3307 wmb();
3308 spin_unlock_irqrestore(&smem_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003309 }
3310
Eric Holmbergc8002902011-09-16 13:55:57 -06003311 mutex_unlock(&smsm_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003312 return ret;
3313}
3314EXPORT_SYMBOL(smsm_state_cb_deregister);
3315
Eric Holmberg6275b602012-11-19 13:05:04 -07003316/**
3317 * smem_get_remote_spinlock - Remote spinlock pointer for unit testing.
3318 *
3319 * @returns: pointer to SMEM remote spinlock
3320 */
3321remote_spinlock_t *smem_get_remote_spinlock(void)
3322{
3323 return &remote_spinlock;
3324}
3325EXPORT_SYMBOL(smem_get_remote_spinlock);
3326
Karthikeyan Ramasubramanianfa44cd72012-08-22 18:08:14 -06003327int smd_module_init_notifier_register(struct notifier_block *nb)
Karthikeyan Ramasubramanian7069c482012-03-22 09:21:20 -06003328{
3329 int ret;
3330 if (!nb)
3331 return -EINVAL;
Karthikeyan Ramasubramanianfa44cd72012-08-22 18:08:14 -06003332 mutex_lock(&smd_module_init_notifier_lock);
3333 ret = raw_notifier_chain_register(&smd_module_init_notifier_list, nb);
3334 if (smd_module_inited)
3335 nb->notifier_call(nb, 0, NULL);
3336 mutex_unlock(&smd_module_init_notifier_lock);
Karthikeyan Ramasubramanian7069c482012-03-22 09:21:20 -06003337 return ret;
3338}
Karthikeyan Ramasubramanianfa44cd72012-08-22 18:08:14 -06003339EXPORT_SYMBOL(smd_module_init_notifier_register);
Karthikeyan Ramasubramanian7069c482012-03-22 09:21:20 -06003340
Karthikeyan Ramasubramanianfa44cd72012-08-22 18:08:14 -06003341int smd_module_init_notifier_unregister(struct notifier_block *nb)
Karthikeyan Ramasubramanian7069c482012-03-22 09:21:20 -06003342{
3343 int ret;
3344 if (!nb)
3345 return -EINVAL;
Karthikeyan Ramasubramanianfa44cd72012-08-22 18:08:14 -06003346 mutex_lock(&smd_module_init_notifier_lock);
3347 ret = raw_notifier_chain_unregister(&smd_module_init_notifier_list,
Karthikeyan Ramasubramanian7069c482012-03-22 09:21:20 -06003348 nb);
Karthikeyan Ramasubramanianfa44cd72012-08-22 18:08:14 -06003349 mutex_unlock(&smd_module_init_notifier_lock);
Karthikeyan Ramasubramanian7069c482012-03-22 09:21:20 -06003350 return ret;
3351}
Karthikeyan Ramasubramanianfa44cd72012-08-22 18:08:14 -06003352EXPORT_SYMBOL(smd_module_init_notifier_unregister);
Karthikeyan Ramasubramanian7069c482012-03-22 09:21:20 -06003353
Karthikeyan Ramasubramanianfa44cd72012-08-22 18:08:14 -06003354static void smd_module_init_notify(uint32_t state, void *data)
Karthikeyan Ramasubramanian7069c482012-03-22 09:21:20 -06003355{
Karthikeyan Ramasubramanianfa44cd72012-08-22 18:08:14 -06003356 mutex_lock(&smd_module_init_notifier_lock);
3357 smd_module_inited = 1;
3358 raw_notifier_call_chain(&smd_module_init_notifier_list,
Karthikeyan Ramasubramanian7069c482012-03-22 09:21:20 -06003359 state, data);
Karthikeyan Ramasubramanianfa44cd72012-08-22 18:08:14 -06003360 mutex_unlock(&smd_module_init_notifier_lock);
Karthikeyan Ramasubramanian7069c482012-03-22 09:21:20 -06003361}
Brian Swetland5b0f5a32009-04-26 18:38:49 -07003362
3363int smd_core_init(void)
3364{
3365 int r;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003366 unsigned long flags = IRQF_TRIGGER_RISING;
3367 SMD_INFO("smd_core_init()\n");
Brian Swetland5b0f5a32009-04-26 18:38:49 -07003368
Brian Swetland37521a32009-07-01 18:30:47 -07003369 r = request_irq(INT_A9_M2A_0, smd_modem_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003370 flags, "smd_dev", 0);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07003371 if (r < 0)
3372 return r;
Arun Kumar Neelakantamac7c02d2012-10-16 22:17:55 +05303373 interrupt_stats[SMD_MODEM].smd_interrupt_id = INT_A9_M2A_0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07003374 r = enable_irq_wake(INT_A9_M2A_0);
3375 if (r < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003376 pr_err("smd_core_init: "
3377 "enable_irq_wake failed for INT_A9_M2A_0\n");
Brian Swetland2eb44eb2008-09-29 16:00:48 -07003378
Eric Holmberg98c6c642012-02-24 11:29:35 -07003379 r = request_irq(INT_A9_M2A_5, smsm_modem_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003380 flags, "smsm_dev", 0);
Brian Swetland2eb44eb2008-09-29 16:00:48 -07003381 if (r < 0) {
3382 free_irq(INT_A9_M2A_0, 0);
3383 return r;
3384 }
Arun Kumar Neelakantamac7c02d2012-10-16 22:17:55 +05303385 interrupt_stats[SMD_MODEM].smsm_interrupt_id = INT_A9_M2A_5;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07003386 r = enable_irq_wake(INT_A9_M2A_5);
3387 if (r < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003388 pr_err("smd_core_init: "
3389 "enable_irq_wake failed for INT_A9_M2A_5\n");
Brian Swetland2eb44eb2008-09-29 16:00:48 -07003390
Brian Swetland37521a32009-07-01 18:30:47 -07003391#if defined(CONFIG_QDSP6)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003392#if (INT_ADSP_A11 == INT_ADSP_A11_SMSM)
3393 flags |= IRQF_SHARED;
3394#endif
Brian Swetland37521a32009-07-01 18:30:47 -07003395 r = request_irq(INT_ADSP_A11, smd_dsp_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003396 flags, "smd_dev", smd_dsp_irq_handler);
Brian Swetland37521a32009-07-01 18:30:47 -07003397 if (r < 0) {
3398 free_irq(INT_A9_M2A_0, 0);
3399 free_irq(INT_A9_M2A_5, 0);
3400 return r;
3401 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003402
Arun Kumar Neelakantamac7c02d2012-10-16 22:17:55 +05303403 interrupt_stats[SMD_Q6].smd_interrupt_id = INT_ADSP_A11;
Eric Holmberg98c6c642012-02-24 11:29:35 -07003404 r = request_irq(INT_ADSP_A11_SMSM, smsm_dsp_irq_handler,
3405 flags, "smsm_dev", smsm_dsp_irq_handler);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003406 if (r < 0) {
3407 free_irq(INT_A9_M2A_0, 0);
3408 free_irq(INT_A9_M2A_5, 0);
3409 free_irq(INT_ADSP_A11, smd_dsp_irq_handler);
3410 return r;
3411 }
3412
Arun Kumar Neelakantamac7c02d2012-10-16 22:17:55 +05303413 interrupt_stats[SMD_Q6].smsm_interrupt_id = INT_ADSP_A11_SMSM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003414 r = enable_irq_wake(INT_ADSP_A11);
3415 if (r < 0)
3416 pr_err("smd_core_init: "
3417 "enable_irq_wake failed for INT_ADSP_A11\n");
3418
3419#if (INT_ADSP_A11 != INT_ADSP_A11_SMSM)
3420 r = enable_irq_wake(INT_ADSP_A11_SMSM);
3421 if (r < 0)
3422 pr_err("smd_core_init: enable_irq_wake "
3423 "failed for INT_ADSP_A11_SMSM\n");
3424#endif
3425 flags &= ~IRQF_SHARED;
Brian Swetland37521a32009-07-01 18:30:47 -07003426#endif
3427
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003428#if defined(CONFIG_DSPS)
3429 r = request_irq(INT_DSPS_A11, smd_dsps_irq_handler,
3430 flags, "smd_dev", smd_dsps_irq_handler);
3431 if (r < 0) {
3432 free_irq(INT_A9_M2A_0, 0);
3433 free_irq(INT_A9_M2A_5, 0);
3434 free_irq(INT_ADSP_A11, smd_dsp_irq_handler);
Eric Holmberg98c6c642012-02-24 11:29:35 -07003435 free_irq(INT_ADSP_A11_SMSM, smsm_dsp_irq_handler);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003436 return r;
3437 }
Brian Swetland5b0f5a32009-04-26 18:38:49 -07003438
Arun Kumar Neelakantamac7c02d2012-10-16 22:17:55 +05303439 interrupt_stats[SMD_DSPS].smd_interrupt_id = INT_DSPS_A11;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003440 r = enable_irq_wake(INT_DSPS_A11);
3441 if (r < 0)
3442 pr_err("smd_core_init: "
3443 "enable_irq_wake failed for INT_ADSP_A11\n");
Arve Hjønnevågec9d3d12009-06-16 14:48:21 -07003444#endif
Brian Swetland2eb44eb2008-09-29 16:00:48 -07003445
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003446#if defined(CONFIG_WCNSS)
3447 r = request_irq(INT_WCNSS_A11, smd_wcnss_irq_handler,
3448 flags, "smd_dev", smd_wcnss_irq_handler);
3449 if (r < 0) {
3450 free_irq(INT_A9_M2A_0, 0);
3451 free_irq(INT_A9_M2A_5, 0);
3452 free_irq(INT_ADSP_A11, smd_dsp_irq_handler);
Eric Holmberg98c6c642012-02-24 11:29:35 -07003453 free_irq(INT_ADSP_A11_SMSM, smsm_dsp_irq_handler);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003454 free_irq(INT_DSPS_A11, smd_dsps_irq_handler);
3455 return r;
3456 }
3457
Arun Kumar Neelakantamac7c02d2012-10-16 22:17:55 +05303458 interrupt_stats[SMD_WCNSS].smd_interrupt_id = INT_WCNSS_A11;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003459 r = enable_irq_wake(INT_WCNSS_A11);
3460 if (r < 0)
3461 pr_err("smd_core_init: "
3462 "enable_irq_wake failed for INT_WCNSS_A11\n");
3463
Eric Holmberg98c6c642012-02-24 11:29:35 -07003464 r = request_irq(INT_WCNSS_A11_SMSM, smsm_wcnss_irq_handler,
3465 flags, "smsm_dev", smsm_wcnss_irq_handler);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003466 if (r < 0) {
3467 free_irq(INT_A9_M2A_0, 0);
3468 free_irq(INT_A9_M2A_5, 0);
3469 free_irq(INT_ADSP_A11, smd_dsp_irq_handler);
Eric Holmberg98c6c642012-02-24 11:29:35 -07003470 free_irq(INT_ADSP_A11_SMSM, smsm_dsp_irq_handler);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003471 free_irq(INT_DSPS_A11, smd_dsps_irq_handler);
3472 free_irq(INT_WCNSS_A11, smd_wcnss_irq_handler);
3473 return r;
3474 }
3475
Arun Kumar Neelakantamac7c02d2012-10-16 22:17:55 +05303476 interrupt_stats[SMD_WCNSS].smsm_interrupt_id = INT_WCNSS_A11_SMSM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003477 r = enable_irq_wake(INT_WCNSS_A11_SMSM);
3478 if (r < 0)
3479 pr_err("smd_core_init: "
3480 "enable_irq_wake failed for INT_WCNSS_A11_SMSM\n");
3481#endif
3482
Jeff Hugo6a8057c2011-08-16 13:47:12 -06003483#if defined(CONFIG_DSPS_SMSM)
Eric Holmberg98c6c642012-02-24 11:29:35 -07003484 r = request_irq(INT_DSPS_A11_SMSM, smsm_dsps_irq_handler,
3485 flags, "smsm_dev", smsm_dsps_irq_handler);
Jeff Hugo6a8057c2011-08-16 13:47:12 -06003486 if (r < 0) {
3487 free_irq(INT_A9_M2A_0, 0);
3488 free_irq(INT_A9_M2A_5, 0);
3489 free_irq(INT_ADSP_A11, smd_dsp_irq_handler);
Eric Holmberg98c6c642012-02-24 11:29:35 -07003490 free_irq(INT_ADSP_A11_SMSM, smsm_dsp_irq_handler);
Jeff Hugo6a8057c2011-08-16 13:47:12 -06003491 free_irq(INT_DSPS_A11, smd_dsps_irq_handler);
3492 free_irq(INT_WCNSS_A11, smd_wcnss_irq_handler);
Eric Holmberg98c6c642012-02-24 11:29:35 -07003493 free_irq(INT_WCNSS_A11_SMSM, smsm_wcnss_irq_handler);
Jeff Hugo6a8057c2011-08-16 13:47:12 -06003494 return r;
3495 }
3496
Arun Kumar Neelakantamac7c02d2012-10-16 22:17:55 +05303497 interrupt_stats[SMD_DSPS].smsm_interrupt_id = INT_DSPS_A11_SMSM;
Jeff Hugo6a8057c2011-08-16 13:47:12 -06003498 r = enable_irq_wake(INT_DSPS_A11_SMSM);
3499 if (r < 0)
3500 pr_err("smd_core_init: "
3501 "enable_irq_wake failed for INT_DSPS_A11_SMSM\n");
3502#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003503 SMD_INFO("smd_core_init() done\n");
3504
Brian Swetland2eb44eb2008-09-29 16:00:48 -07003505 return 0;
3506}
3507
Angshuman Sarkarbad32df2012-02-01 19:52:52 +05303508static int intr_init(struct interrupt_config_item *private_irq,
3509 struct smd_irq_config *platform_irq,
3510 struct platform_device *pdev
3511 )
3512{
3513 int irq_id;
3514 int ret;
3515 int ret_wake;
3516
3517 private_irq->out_bit_pos = platform_irq->out_bit_pos;
3518 private_irq->out_offset = platform_irq->out_offset;
3519 private_irq->out_base = platform_irq->out_base;
3520
3521 irq_id = platform_get_irq_byname(
3522 pdev,
3523 platform_irq->irq_name
3524 );
3525 SMD_DBG("smd: %s: register irq: %s id: %d\n", __func__,
3526 platform_irq->irq_name, irq_id);
3527 ret = request_irq(irq_id,
3528 private_irq->irq_handler,
3529 platform_irq->flags,
3530 platform_irq->device_name,
3531 (void *)platform_irq->dev_id
3532 );
3533 if (ret < 0) {
3534 platform_irq->irq_id = ret;
Eric Holmbergdeace152012-07-25 12:17:11 -06003535 private_irq->irq_id = ret;
Angshuman Sarkarbad32df2012-02-01 19:52:52 +05303536 } else {
3537 platform_irq->irq_id = irq_id;
Eric Holmbergdeace152012-07-25 12:17:11 -06003538 private_irq->irq_id = irq_id;
Angshuman Sarkarbad32df2012-02-01 19:52:52 +05303539 ret_wake = enable_irq_wake(irq_id);
3540 if (ret_wake < 0) {
3541 pr_err("smd: enable_irq_wake failed on %s",
3542 platform_irq->irq_name);
3543 }
3544 }
3545
3546 return ret;
3547}
3548
3549int smd_core_platform_init(struct platform_device *pdev)
3550{
3551 int i;
3552 int ret;
3553 uint32_t num_ss;
3554 struct smd_platform *smd_platform_data;
3555 struct smd_subsystem_config *smd_ss_config_list;
3556 struct smd_subsystem_config *cfg;
3557 int err_ret = 0;
Jeff Hugobdc734d2012-03-26 16:05:39 -06003558 struct smd_smem_regions *smd_smem_areas;
Eric Holmberg51edef72013-04-11 14:28:33 -06003559 struct smem_area *smem_areas_tmp = NULL;
3560 int smem_idx;
Angshuman Sarkarbad32df2012-02-01 19:52:52 +05303561
3562 smd_platform_data = pdev->dev.platform_data;
3563 num_ss = smd_platform_data->num_ss_configs;
3564 smd_ss_config_list = smd_platform_data->smd_ss_configs;
3565
Eric Holmberg2bb6ccd2012-03-13 13:05:14 -06003566 if (smd_platform_data->smd_ssr_config)
3567 disable_smsm_reset_handshake = smd_platform_data->
3568 smd_ssr_config->disable_smsm_reset_handshake;
3569
Jeff Hugobdc734d2012-03-26 16:05:39 -06003570 smd_smem_areas = smd_platform_data->smd_smem_areas;
Eric Holmberg51edef72013-04-11 14:28:33 -06003571 num_smem_areas = smd_platform_data->num_smem_areas + 1;
3572
3573 /* Initialize main SMEM region */
3574 smem_areas_tmp = kmalloc_array(num_smem_areas, sizeof(struct smem_area),
3575 GFP_KERNEL);
3576 if (!smem_areas_tmp) {
3577 pr_err("%s: smem_areas kmalloc failed\n", __func__);
3578 err_ret = -ENOMEM;
3579 goto smem_areas_alloc_fail;
3580 }
3581
3582 smem_areas_tmp[0].phys_addr = msm_shared_ram_phys;
3583 smem_areas_tmp[0].size = MSM_SHARED_RAM_SIZE;
3584 smem_areas_tmp[0].virt_addr = MSM_SHARED_RAM_BASE;
3585
3586 /* Configure auxiliary SMEM regions */
3587 for (smem_idx = 1; smem_idx < num_smem_areas; ++smem_idx) {
3588 smem_areas_tmp[smem_idx].phys_addr =
3589 smd_smem_areas[smem_idx].phys_addr;
3590 smem_areas_tmp[smem_idx].size =
3591 smd_smem_areas[smem_idx].size;
3592 smem_areas_tmp[smem_idx].virt_addr = ioremap_nocache(
3593 (unsigned long)(smem_areas_tmp[smem_idx].phys_addr),
3594 smem_areas_tmp[smem_idx].size);
3595 if (!smem_areas_tmp[smem_idx].virt_addr) {
3596 pr_err("%s: ioremap_nocache() of addr: %pa size: %pa\n",
3597 __func__,
3598 &smem_areas_tmp[smem_idx].phys_addr,
3599 &smem_areas_tmp[smem_idx].size);
Jeff Hugobdc734d2012-03-26 16:05:39 -06003600 err_ret = -ENOMEM;
Eric Holmberg51edef72013-04-11 14:28:33 -06003601 goto smem_failed;
Jeff Hugobdc734d2012-03-26 16:05:39 -06003602 }
3603
Eric Holmberg51edef72013-04-11 14:28:33 -06003604 if (OVERFLOW_ADD_UNSIGNED(uintptr_t,
3605 (uintptr_t)smem_areas_tmp[smem_idx].virt_addr,
3606 smem_areas_tmp[smem_idx].size)) {
3607 pr_err("%s: invalid virtual address block %i: %p:%pa\n",
3608 __func__, smem_idx,
3609 smem_areas_tmp[smem_idx].virt_addr,
3610 &smem_areas_tmp[smem_idx].size);
3611 ++smem_idx;
3612 err_ret = -EINVAL;
3613 goto smem_failed;
Jeff Hugobdc734d2012-03-26 16:05:39 -06003614 }
Eric Holmberg51edef72013-04-11 14:28:33 -06003615
3616 SMD_DBG("%s: %d = %pa %pa", __func__, smem_idx,
3617 &smd_smem_areas[smem_idx].phys_addr,
3618 &smd_smem_areas[smem_idx].size);
Jeff Hugobdc734d2012-03-26 16:05:39 -06003619 }
3620
Angshuman Sarkarbad32df2012-02-01 19:52:52 +05303621 for (i = 0; i < num_ss; i++) {
3622 cfg = &smd_ss_config_list[i];
3623
3624 ret = intr_init(
3625 &private_intr_config[cfg->irq_config_id].smd,
3626 &cfg->smd_int,
3627 pdev
3628 );
3629
3630 if (ret < 0) {
3631 err_ret = ret;
3632 pr_err("smd: register irq failed on %s\n",
3633 cfg->smd_int.irq_name);
Jeff Hugobdc734d2012-03-26 16:05:39 -06003634 goto intr_failed;
Angshuman Sarkarbad32df2012-02-01 19:52:52 +05303635 }
3636
Arun Kumar Neelakantamac7c02d2012-10-16 22:17:55 +05303637 interrupt_stats[cfg->irq_config_id].smd_interrupt_id
3638 = cfg->smd_int.irq_id;
Jeff Hugo918b2dc2012-03-21 13:42:09 -06003639 /* only init smsm structs if this edge supports smsm */
3640 if (cfg->smsm_int.irq_id)
3641 ret = intr_init(
3642 &private_intr_config[cfg->irq_config_id].smsm,
3643 &cfg->smsm_int,
3644 pdev
3645 );
Angshuman Sarkarbad32df2012-02-01 19:52:52 +05303646
3647 if (ret < 0) {
3648 err_ret = ret;
3649 pr_err("smd: register irq failed on %s\n",
3650 cfg->smsm_int.irq_name);
Jeff Hugobdc734d2012-03-26 16:05:39 -06003651 goto intr_failed;
Angshuman Sarkarbad32df2012-02-01 19:52:52 +05303652 }
Eric Holmberg17992c12012-02-29 12:54:44 -07003653
Arun Kumar Neelakantamac7c02d2012-10-16 22:17:55 +05303654 if (cfg->smsm_int.irq_id)
3655 interrupt_stats[cfg->irq_config_id].smsm_interrupt_id
3656 = cfg->smsm_int.irq_id;
Jeff Hugo918b2dc2012-03-21 13:42:09 -06003657 if (cfg->subsys_name)
3658 strlcpy(edge_to_pids[cfg->edge].subsys_name,
Eric Holmberg17992c12012-02-29 12:54:44 -07003659 cfg->subsys_name, SMD_MAX_CH_NAME_LEN);
Angshuman Sarkarbad32df2012-02-01 19:52:52 +05303660 }
3661
Angshuman Sarkarbad32df2012-02-01 19:52:52 +05303662 SMD_INFO("smd_core_platform_init() done\n");
Eric Holmberg51edef72013-04-11 14:28:33 -06003663
3664 smem_areas = smem_areas_tmp;
Angshuman Sarkarbad32df2012-02-01 19:52:52 +05303665 return 0;
3666
Jeff Hugobdc734d2012-03-26 16:05:39 -06003667intr_failed:
3668 pr_err("smd: deregistering IRQs\n");
3669 for (i = 0; i < num_ss; ++i) {
3670 cfg = &smd_ss_config_list[i];
3671
3672 if (cfg->smd_int.irq_id >= 0)
3673 free_irq(cfg->smd_int.irq_id,
3674 (void *)cfg->smd_int.dev_id
3675 );
3676 if (cfg->smsm_int.irq_id >= 0)
3677 free_irq(cfg->smsm_int.irq_id,
3678 (void *)cfg->smsm_int.dev_id
3679 );
3680 }
3681smem_failed:
Eric Holmberg51edef72013-04-11 14:28:33 -06003682 for (smem_idx = smem_idx - 1; smem_idx >= 1; --smem_idx)
3683 iounmap(smem_areas_tmp[smem_idx].virt_addr);
3684
3685 num_smem_areas = 0;
3686 kfree(smem_areas_tmp);
3687
Jeff Hugobdc734d2012-03-26 16:05:39 -06003688smem_areas_alloc_fail:
3689 return err_ret;
Angshuman Sarkarbad32df2012-02-01 19:52:52 +05303690}
3691
Jeff Hugo412356e2012-09-27 17:14:23 -06003692static int __devinit parse_smd_devicetree(struct device_node *node,
3693 void *irq_out_base)
3694{
3695 uint32_t edge;
3696 char *key;
3697 int ret;
3698 uint32_t irq_offset;
3699 uint32_t irq_bitmask;
3700 uint32_t irq_line;
3701 unsigned long irq_flags = IRQF_TRIGGER_RISING;
3702 const char *pilstr;
3703 struct interrupt_config_item *private_irq;
3704
3705 key = "qcom,smd-edge";
3706 ret = of_property_read_u32(node, key, &edge);
3707 if (ret)
3708 goto missing_key;
3709 SMD_DBG("%s: %s = %d", __func__, key, edge);
3710
3711 key = "qcom,smd-irq-offset";
3712 ret = of_property_read_u32(node, key, &irq_offset);
3713 if (ret)
3714 goto missing_key;
3715 SMD_DBG("%s: %s = %x", __func__, key, irq_offset);
3716
3717 key = "qcom,smd-irq-bitmask";
3718 ret = of_property_read_u32(node, key, &irq_bitmask);
3719 if (ret)
3720 goto missing_key;
3721 SMD_DBG("%s: %s = %x", __func__, key, irq_bitmask);
3722
3723 key = "interrupts";
3724 irq_line = irq_of_parse_and_map(node, 0);
3725 if (!irq_line)
3726 goto missing_key;
3727 SMD_DBG("%s: %s = %d", __func__, key, irq_line);
3728
3729 key = "qcom,pil-string";
3730 pilstr = of_get_property(node, key, NULL);
3731 if (pilstr)
3732 SMD_DBG("%s: %s = %s", __func__, key, pilstr);
3733
3734 key = "qcom,irq-no-suspend";
3735 ret = of_property_read_bool(node, key);
3736 if (ret)
3737 irq_flags |= IRQF_NO_SUSPEND;
3738
3739 private_irq = &private_intr_config[edge_to_pids[edge].remote_pid].smd;
3740 private_irq->out_bit_pos = irq_bitmask;
3741 private_irq->out_offset = irq_offset;
3742 private_irq->out_base = irq_out_base;
3743 private_irq->irq_id = irq_line;
3744
3745 ret = request_irq(irq_line,
3746 private_irq->irq_handler,
3747 irq_flags,
3748 "smd_dev",
3749 NULL);
3750 if (ret < 0) {
3751 pr_err("%s: request_irq() failed on %d\n", __func__, irq_line);
3752 return ret;
3753 } else {
3754 ret = enable_irq_wake(irq_line);
3755 if (ret < 0)
3756 pr_err("%s: enable_irq_wake() failed on %d\n", __func__,
3757 irq_line);
3758 }
3759
3760 if (pilstr)
3761 strlcpy(edge_to_pids[edge].subsys_name, pilstr,
3762 SMD_MAX_CH_NAME_LEN);
3763
3764 return 0;
3765
3766missing_key:
3767 pr_err("%s: missing key: %s", __func__, key);
3768 return -ENODEV;
3769}
3770
3771static int __devinit parse_smsm_devicetree(struct device_node *node,
3772 void *irq_out_base)
3773{
3774 uint32_t edge;
3775 char *key;
3776 int ret;
3777 uint32_t irq_offset;
3778 uint32_t irq_bitmask;
3779 uint32_t irq_line;
3780 struct interrupt_config_item *private_irq;
3781
3782 key = "qcom,smsm-edge";
3783 ret = of_property_read_u32(node, key, &edge);
3784 if (ret)
3785 goto missing_key;
3786 SMD_DBG("%s: %s = %d", __func__, key, edge);
3787
3788 key = "qcom,smsm-irq-offset";
3789 ret = of_property_read_u32(node, key, &irq_offset);
3790 if (ret)
3791 goto missing_key;
3792 SMD_DBG("%s: %s = %x", __func__, key, irq_offset);
3793
3794 key = "qcom,smsm-irq-bitmask";
3795 ret = of_property_read_u32(node, key, &irq_bitmask);
3796 if (ret)
3797 goto missing_key;
3798 SMD_DBG("%s: %s = %x", __func__, key, irq_bitmask);
3799
3800 key = "interrupts";
3801 irq_line = irq_of_parse_and_map(node, 0);
3802 if (!irq_line)
3803 goto missing_key;
3804 SMD_DBG("%s: %s = %d", __func__, key, irq_line);
3805
3806 private_irq = &private_intr_config[edge_to_pids[edge].remote_pid].smsm;
3807 private_irq->out_bit_pos = irq_bitmask;
3808 private_irq->out_offset = irq_offset;
3809 private_irq->out_base = irq_out_base;
3810 private_irq->irq_id = irq_line;
3811
3812 ret = request_irq(irq_line,
3813 private_irq->irq_handler,
3814 IRQF_TRIGGER_RISING,
3815 "smsm_dev",
3816 NULL);
3817 if (ret < 0) {
3818 pr_err("%s: request_irq() failed on %d\n", __func__, irq_line);
3819 return ret;
3820 } else {
3821 ret = enable_irq_wake(irq_line);
3822 if (ret < 0)
3823 pr_err("%s: enable_irq_wake() failed on %d\n", __func__,
3824 irq_line);
3825 }
3826
3827 return 0;
3828
3829missing_key:
3830 pr_err("%s: missing key: %s", __func__, key);
3831 return -ENODEV;
3832}
3833
3834static void __devinit unparse_smd_devicetree(struct device_node *node)
3835{
3836 uint32_t irq_line;
3837
3838 irq_line = irq_of_parse_and_map(node, 0);
3839
3840 free_irq(irq_line, NULL);
3841}
3842
3843static void __devinit unparse_smsm_devicetree(struct device_node *node)
3844{
3845 uint32_t irq_line;
3846
3847 irq_line = irq_of_parse_and_map(node, 0);
3848
3849 free_irq(irq_line, NULL);
3850}
3851
3852static int __devinit smd_core_devicetree_init(struct platform_device *pdev)
3853{
3854 char *key;
3855 struct resource *r;
3856 void *irq_out_base;
Stepan Moskovchenkod6ee8262013-02-06 11:26:05 -08003857 phys_addr_t aux_mem_base;
3858 resource_size_t aux_mem_size;
Jeff Hugo412356e2012-09-27 17:14:23 -06003859 int temp_string_size = 11; /* max 3 digit count */
3860 char temp_string[temp_string_size];
Jeff Hugo412356e2012-09-27 17:14:23 -06003861 struct device_node *node;
3862 int ret;
3863 const char *compatible;
Eric Holmberg51edef72013-04-11 14:28:33 -06003864 struct ramdump_segment *ramdump_segments_tmp = NULL;
3865 struct smem_area *smem_areas_tmp = NULL;
3866 int smem_idx = 0;
Jeff Hugo412356e2012-09-27 17:14:23 -06003867 int subnode_num = 0;
Eric Holmberg51edef72013-04-11 14:28:33 -06003868 int i;
Stepan Moskovchenkod6ee8262013-02-06 11:26:05 -08003869 resource_size_t irq_out_size;
Jeff Hugo412356e2012-09-27 17:14:23 -06003870
3871 disable_smsm_reset_handshake = 1;
3872
3873 key = "irq-reg-base";
3874 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, key);
3875 if (!r) {
3876 pr_err("%s: missing '%s'\n", __func__, key);
3877 return -ENODEV;
3878 }
Stepan Moskovchenkod6ee8262013-02-06 11:26:05 -08003879 irq_out_size = resource_size(r);
3880 irq_out_base = ioremap_nocache(r->start, irq_out_size);
3881 if (!irq_out_base) {
3882 pr_err("%s: ioremap_nocache() of irq_out_base addr:%pr size:%pr\n",
3883 __func__, &r->start, &irq_out_size);
3884 return -ENOMEM;
3885 }
Jeff Hugo412356e2012-09-27 17:14:23 -06003886 SMD_DBG("%s: %s = %p", __func__, key, irq_out_base);
3887
Eric Holmberg51edef72013-04-11 14:28:33 -06003888 num_smem_areas = 1;
Jeff Hugo412356e2012-09-27 17:14:23 -06003889 while (1) {
Eric Holmberg51edef72013-04-11 14:28:33 -06003890 scnprintf(temp_string, temp_string_size, "aux-mem%d",
3891 num_smem_areas);
Jeff Hugo412356e2012-09-27 17:14:23 -06003892 r = platform_get_resource_byname(pdev, IORESOURCE_MEM,
3893 temp_string);
3894 if (!r)
3895 break;
3896
3897 ++num_smem_areas;
Eric Holmberg51edef72013-04-11 14:28:33 -06003898 if (num_smem_areas > 999) {
Jeff Hugo412356e2012-09-27 17:14:23 -06003899 pr_err("%s: max num aux mem regions reached\n",
3900 __func__);
3901 break;
3902 }
3903 }
3904
Eric Holmberg51edef72013-04-11 14:28:33 -06003905 /* Initialize main SMEM region and SSR ramdump region */
Eric Holmbergcfbc1d52013-03-13 18:30:19 -06003906 key = "smem";
3907 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, key);
3908 if (!r) {
3909 pr_err("%s: missing '%s'\n", __func__, key);
3910 return -ENODEV;
3911 }
Eric Holmbergcfbc1d52013-03-13 18:30:19 -06003912
Eric Holmberg51edef72013-04-11 14:28:33 -06003913 smem_areas_tmp = kmalloc_array(num_smem_areas, sizeof(struct smem_area),
3914 GFP_KERNEL);
3915 if (!smem_areas_tmp) {
3916 pr_err("%s: smem areas kmalloc failed\n", __func__);
3917 ret = -ENOMEM;
3918 goto free_smem_areas;
3919 }
3920
3921 ramdump_segments_tmp = kmalloc_array(num_smem_areas,
3922 sizeof(struct ramdump_segment), GFP_KERNEL);
Eric Holmbergcfbc1d52013-03-13 18:30:19 -06003923 if (!ramdump_segments_tmp) {
3924 pr_err("%s: ramdump segment kmalloc failed\n", __func__);
3925 ret = -ENOMEM;
3926 goto free_smem_areas;
3927 }
Eric Holmbergcfbc1d52013-03-13 18:30:19 -06003928
Eric Holmberg51edef72013-04-11 14:28:33 -06003929 smem_areas_tmp[smem_idx].phys_addr = r->start;
3930 smem_areas_tmp[smem_idx].size = resource_size(r);
3931 smem_areas_tmp[smem_idx].virt_addr = MSM_SHARED_RAM_BASE;
Eric Holmbergcfbc1d52013-03-13 18:30:19 -06003932
Eric Holmberg51edef72013-04-11 14:28:33 -06003933 ramdump_segments_tmp[smem_idx].address = r->start;
3934 ramdump_segments_tmp[smem_idx].size = resource_size(r);
3935 ++smem_idx;
Eric Holmbergcfbc1d52013-03-13 18:30:19 -06003936
Eric Holmberg51edef72013-04-11 14:28:33 -06003937 /* Configure auxiliary SMEM regions */
3938 while (1) {
3939 scnprintf(temp_string, temp_string_size, "aux-mem%d",
3940 smem_idx);
3941 r = platform_get_resource_byname(pdev, IORESOURCE_MEM,
3942 temp_string);
3943 if (!r)
3944 break;
3945 aux_mem_base = r->start;
3946 aux_mem_size = resource_size(r);
3947
3948 ramdump_segments_tmp[smem_idx].address = aux_mem_base;
3949 ramdump_segments_tmp[smem_idx].size = aux_mem_size;
3950
3951 smem_areas_tmp[smem_idx].phys_addr = aux_mem_base;
3952 smem_areas_tmp[smem_idx].size = aux_mem_size;
3953 smem_areas_tmp[smem_idx].virt_addr = ioremap_nocache(
3954 (unsigned long)(smem_areas_tmp[smem_idx].phys_addr),
3955 smem_areas_tmp[smem_idx].size);
3956 SMD_DBG("%s: %s = %pa %pa -> %p", __func__, temp_string,
3957 &aux_mem_base, &aux_mem_size,
3958 smem_areas_tmp[smem_idx].virt_addr);
3959
3960 if (!smem_areas_tmp[smem_idx].virt_addr) {
3961 pr_err("%s: ioremap_nocache() of addr:%pa size: %pa\n",
3962 __func__,
3963 &smem_areas_tmp[smem_idx].phys_addr,
3964 &smem_areas_tmp[smem_idx].size);
Eric Holmbergcfbc1d52013-03-13 18:30:19 -06003965 ret = -ENOMEM;
3966 goto free_smem_areas;
Jeff Hugo412356e2012-09-27 17:14:23 -06003967 }
Eric Holmbergcfbc1d52013-03-13 18:30:19 -06003968
Eric Holmberg51edef72013-04-11 14:28:33 -06003969 if (OVERFLOW_ADD_UNSIGNED(uintptr_t,
3970 (uintptr_t)smem_areas_tmp[smem_idx].virt_addr,
3971 smem_areas_tmp[smem_idx].size)) {
3972 pr_err("%s: invalid virtual address block %i: %p:%pa\n",
3973 __func__, smem_idx,
3974 smem_areas_tmp[smem_idx].virt_addr,
3975 &smem_areas_tmp[smem_idx].size);
3976 ++smem_idx;
3977 ret = -EINVAL;
3978 goto free_smem_areas;
Jeff Hugo412356e2012-09-27 17:14:23 -06003979 }
Eric Holmberg51edef72013-04-11 14:28:33 -06003980
3981 ++smem_idx;
3982 if (smem_idx > 999) {
3983 pr_err("%s: max num aux mem regions reached\n",
3984 __func__);
3985 break;
3986 }
Jeff Hugo412356e2012-09-27 17:14:23 -06003987 }
3988
3989 for_each_child_of_node(pdev->dev.of_node, node) {
3990 compatible = of_get_property(node, "compatible", NULL);
Brent Hronikf4442e12013-04-17 15:13:11 -06003991 if (!compatible) {
3992 pr_err("%s: invalid child node: compatible null\n",
3993 __func__);
3994 ret = -ENODEV;
3995 goto rollback_subnodes;
3996 }
Jeff Hugo412356e2012-09-27 17:14:23 -06003997 if (!strcmp(compatible, "qcom,smd")) {
3998 ret = parse_smd_devicetree(node, irq_out_base);
3999 if (ret)
4000 goto rollback_subnodes;
4001 } else if (!strcmp(compatible, "qcom,smsm")) {
4002 ret = parse_smsm_devicetree(node, irq_out_base);
4003 if (ret)
4004 goto rollback_subnodes;
4005 } else {
4006 pr_err("%s: invalid child node named: %s\n", __func__,
4007 compatible);
4008 ret = -ENODEV;
4009 goto rollback_subnodes;
4010 }
4011 ++subnode_num;
4012 }
4013
Eric Holmberg51edef72013-04-11 14:28:33 -06004014 smem_areas = smem_areas_tmp;
Eric Holmbergcfbc1d52013-03-13 18:30:19 -06004015 smem_ramdump_segments = ramdump_segments_tmp;
Jeff Hugo412356e2012-09-27 17:14:23 -06004016 return 0;
4017
4018rollback_subnodes:
Eric Holmberg51edef72013-04-11 14:28:33 -06004019 i = 0;
Jeff Hugo412356e2012-09-27 17:14:23 -06004020 for_each_child_of_node(pdev->dev.of_node, node) {
Eric Holmberg51edef72013-04-11 14:28:33 -06004021 if (i >= subnode_num)
Jeff Hugo412356e2012-09-27 17:14:23 -06004022 break;
Eric Holmberg51edef72013-04-11 14:28:33 -06004023 ++i;
Jeff Hugo412356e2012-09-27 17:14:23 -06004024 compatible = of_get_property(node, "compatible", NULL);
4025 if (!strcmp(compatible, "qcom,smd"))
4026 unparse_smd_devicetree(node);
4027 else
4028 unparse_smsm_devicetree(node);
4029 }
4030free_smem_areas:
Eric Holmberg51edef72013-04-11 14:28:33 -06004031 for (smem_idx = smem_idx - 1; smem_idx >= 1; --smem_idx)
4032 iounmap(smem_areas_tmp[smem_idx].virt_addr);
4033
Jeff Hugo412356e2012-09-27 17:14:23 -06004034 num_smem_areas = 0;
Eric Holmbergcfbc1d52013-03-13 18:30:19 -06004035 kfree(ramdump_segments_tmp);
Eric Holmberg51edef72013-04-11 14:28:33 -06004036 kfree(smem_areas_tmp);
Jeff Hugo412356e2012-09-27 17:14:23 -06004037 return ret;
4038}
4039
Gregory Bean4416e9e2010-07-28 10:22:12 -07004040static int __devinit msm_smd_probe(struct platform_device *pdev)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07004041{
Angshuman Sarkarbad32df2012-02-01 19:52:52 +05304042 int ret;
Daniel Walker0aec66d2010-03-18 12:31:08 -07004043
Angshuman Sarkarbad32df2012-02-01 19:52:52 +05304044 SMD_INFO("smd probe\n");
Brian Swetland2eb44eb2008-09-29 16:00:48 -07004045 INIT_WORK(&probe_work, smd_channel_probe_worker);
4046
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004047 channel_close_wq = create_singlethread_workqueue("smd_channel_close");
4048 if (IS_ERR(channel_close_wq)) {
4049 pr_err("%s: create_singlethread_workqueue ENOMEM\n", __func__);
4050 return -ENOMEM;
4051 }
4052
4053 if (smsm_init()) {
4054 pr_err("smsm_init() failed\n");
Brian Swetland2eb44eb2008-09-29 16:00:48 -07004055 return -1;
4056 }
4057
Angshuman Sarkarbad32df2012-02-01 19:52:52 +05304058 if (pdev) {
4059 if (pdev->dev.of_node) {
Jeff Hugo412356e2012-09-27 17:14:23 -06004060 ret = smd_core_devicetree_init(pdev);
4061 if (ret) {
4062 pr_err("%s: device tree init failed\n",
4063 __func__);
4064 return ret;
4065 }
Eric Holmbergcfbc1d52013-03-13 18:30:19 -06004066 smd_dev = &pdev->dev;
Angshuman Sarkarbad32df2012-02-01 19:52:52 +05304067 } else if (pdev->dev.platform_data) {
4068 ret = smd_core_platform_init(pdev);
4069 if (ret) {
4070 pr_err(
4071 "SMD: smd_core_platform_init() failed\n");
4072 return -ENODEV;
4073 }
4074 } else {
4075 ret = smd_core_init();
4076 if (ret) {
4077 pr_err("smd_core_init() failed\n");
4078 return -ENODEV;
4079 }
4080 }
4081 } else {
4082 pr_err("SMD: PDEV not found\n");
4083 return -ENODEV;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07004084 }
Iliyan Malchev1207bab2009-11-15 18:16:43 -08004085
Brian Swetland2eb44eb2008-09-29 16:00:48 -07004086 smd_initialized = 1;
4087
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004088 smd_alloc_loopback_channel();
Eric Holmbergc33d4ab2011-10-24 10:28:25 -06004089 smsm_irq_handler(0, 0);
4090 tasklet_schedule(&smd_fake_irq_tasklet);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004091
Brian Swetland2eb44eb2008-09-29 16:00:48 -07004092 return 0;
4093}
4094
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004095static int restart_notifier_cb(struct notifier_block *this,
4096 unsigned long code,
4097 void *data);
4098
4099static struct restart_notifier_block restart_notifiers[] = {
Eric Holmbergca7ead22011-12-01 17:21:15 -07004100 {SMD_MODEM, "modem", .nb.notifier_call = restart_notifier_cb},
4101 {SMD_Q6, "lpass", .nb.notifier_call = restart_notifier_cb},
Sameer Thalappil52381142012-10-04 17:22:24 -07004102 {SMD_WCNSS, "wcnss", .nb.notifier_call = restart_notifier_cb},
Eric Holmbergca7ead22011-12-01 17:21:15 -07004103 {SMD_DSPS, "dsps", .nb.notifier_call = restart_notifier_cb},
Eric Holmberg8b0e74f2012-02-08 09:56:17 -07004104 {SMD_MODEM, "gss", .nb.notifier_call = restart_notifier_cb},
Jeff Hugod3cf6ec2012-09-26 15:30:10 -06004105 {SMD_Q6, "adsp", .nb.notifier_call = restart_notifier_cb},
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004106};
4107
4108static int restart_notifier_cb(struct notifier_block *this,
4109 unsigned long code,
4110 void *data)
4111{
Jeff Hugo73f356f2012-12-14 17:56:19 -07004112 /*
4113 * Some SMD or SMSM clients assume SMD/SMSM SSR handling will be
4114 * done in the AFTER_SHUTDOWN level. If this ever changes, extra
4115 * care should be taken to verify no clients are broken.
4116 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004117 if (code == SUBSYS_AFTER_SHUTDOWN) {
4118 struct restart_notifier_block *notifier;
4119
4120 notifier = container_of(this,
4121 struct restart_notifier_block, nb);
4122 SMD_INFO("%s: ssrestart for processor %d ('%s')\n",
4123 __func__, notifier->processor,
4124 notifier->name);
4125
Eric Holmbergcfbc1d52013-03-13 18:30:19 -06004126 remote_spin_release(&remote_spinlock, notifier->processor);
4127 remote_spin_release_all(notifier->processor);
4128
4129 if (smem_ramdump_dev) {
4130 int ret;
4131
4132 SMD_INFO("%s: saving ramdump\n", __func__);
4133 /*
4134 * XPU protection does not currently allow the
4135 * auxiliary memory regions to be dumped. If this
4136 * changes, then num_smem_areas + 1 should be passed
4137 * into do_elf_ramdump() to dump all regions.
4138 */
4139 ret = do_elf_ramdump(smem_ramdump_dev,
4140 smem_ramdump_segments, 1);
4141 if (ret < 0)
4142 pr_err("%s: unable to dump smem %d\n", __func__,
4143 ret);
4144 }
4145
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004146 smd_channel_reset(notifier->processor);
4147 }
4148
4149 return NOTIFY_DONE;
4150}
4151
4152static __init int modem_restart_late_init(void)
4153{
4154 int i;
4155 void *handle;
4156 struct restart_notifier_block *nb;
4157
Eric Holmbergcfbc1d52013-03-13 18:30:19 -06004158 smem_ramdump_dev = create_ramdump_device("smem-smd", smd_dev);
4159 if (IS_ERR_OR_NULL(smem_ramdump_dev)) {
4160 pr_err("%s: Unable to create smem ramdump device.\n",
4161 __func__);
4162 smem_ramdump_dev = NULL;
4163 }
4164
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004165 for (i = 0; i < ARRAY_SIZE(restart_notifiers); i++) {
4166 nb = &restart_notifiers[i];
4167 handle = subsys_notif_register_notifier(nb->name, &nb->nb);
4168 SMD_DBG("%s: registering notif for '%s', handle=%p\n",
4169 __func__, nb->name, handle);
4170 }
Eric Holmbergcfbc1d52013-03-13 18:30:19 -06004171
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004172 return 0;
4173}
4174late_initcall(modem_restart_late_init);
4175
Jeff Hugo412356e2012-09-27 17:14:23 -06004176static struct of_device_id msm_smem_match_table[] = {
4177 { .compatible = "qcom,smem" },
4178 {},
4179};
4180
Brian Swetland2eb44eb2008-09-29 16:00:48 -07004181static struct platform_driver msm_smd_driver = {
4182 .probe = msm_smd_probe,
4183 .driver = {
4184 .name = MODULE_NAME,
4185 .owner = THIS_MODULE,
Jeff Hugo412356e2012-09-27 17:14:23 -06004186 .of_match_table = msm_smem_match_table,
Brian Swetland2eb44eb2008-09-29 16:00:48 -07004187 },
4188};
4189
Mahesh Sivasubramaniand9041b02012-05-09 12:57:23 -06004190int __init msm_smd_init(void)
Brian Swetland2eb44eb2008-09-29 16:00:48 -07004191{
Mahesh Sivasubramaniand9041b02012-05-09 12:57:23 -06004192 static bool registered;
Karthikeyan Ramasubramanianfa44cd72012-08-22 18:08:14 -06004193 int rc;
Mahesh Sivasubramaniand9041b02012-05-09 12:57:23 -06004194
4195 if (registered)
4196 return 0;
4197
Arun Kumar Neelakantam7cffb332013-01-28 15:43:35 +05304198 smd_log_ctx = ipc_log_context_create(NUM_LOG_PAGES, "smd");
4199 if (!smd_log_ctx) {
4200 pr_err("%s: unable to create logging context\n", __func__);
4201 msm_smd_debug_mask = 0;
4202 }
4203
Mahesh Sivasubramaniand9041b02012-05-09 12:57:23 -06004204 registered = true;
Karthikeyan Ramasubramanianfa44cd72012-08-22 18:08:14 -06004205 rc = remote_spin_lock_init(&remote_spinlock, SMEM_SPINLOCK_SMEM_ALLOC);
4206 if (rc) {
4207 pr_err("%s: remote spinlock init failed %d\n", __func__, rc);
4208 return rc;
4209 }
4210 spinlocks_initialized = 1;
4211
4212 rc = platform_driver_register(&msm_smd_driver);
4213 if (rc) {
4214 pr_err("%s: msm_smd_driver register failed %d\n",
4215 __func__, rc);
4216 return rc;
4217 }
4218
4219 smd_module_init_notify(0, NULL);
4220
4221 return 0;
Brian Swetland2eb44eb2008-09-29 16:00:48 -07004222}
4223
4224module_init(msm_smd_init);
4225
4226MODULE_DESCRIPTION("MSM Shared Memory Core");
4227MODULE_AUTHOR("Brian Swetland <swetland@google.com>");
4228MODULE_LICENSE("GPL");