blob: 6dc18596d976421e1306ec725f0c67e6c468c1de [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* arch/arm/mach-msm/pm2.c
2 *
3 * MSM Power Management Routines
4 *
5 * Copyright (C) 2007 Google, Inc.
Murali Nalajala0df9fee2012-01-12 15:26:09 +05306 * Copyright (c) 2008-2012 Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07007 *
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 */
18
19#include <linux/module.h>
20#include <linux/kernel.h>
21#include <linux/clk.h>
22#include <linux/delay.h>
23#include <linux/init.h>
24#include <linux/pm.h>
25#include <linux/pm_qos_params.h>
26#include <linux/proc_fs.h>
27#include <linux/suspend.h>
28#include <linux/reboot.h>
29#include <linux/uaccess.h>
30#include <linux/io.h>
31#include <linux/memory.h>
32#ifdef CONFIG_HAS_WAKELOCK
33#include <linux/wakelock.h>
34#endif
35#include <mach/msm_iomap.h>
36#include <mach/system.h>
37#ifdef CONFIG_CPU_V7
38#include <asm/pgtable.h>
39#include <asm/pgalloc.h>
40#endif
41#ifdef CONFIG_CACHE_L2X0
42#include <asm/hardware/cache-l2x0.h>
43#endif
44#ifdef CONFIG_VFP
45#include <asm/vfp.h>
46#endif
47
48#ifdef CONFIG_MSM_MEMORY_LOW_POWER_MODE_SUSPEND_DEEP_POWER_DOWN
49#include <mach/msm_migrate_pages.h>
50#endif
Murali Nalajala41786ab2012-03-06 10:47:32 +053051#include <mach/socinfo.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070052
53#include "smd_private.h"
54#include "smd_rpcrouter.h"
55#include "acpuclock.h"
56#include "clock.h"
57#include "proc_comm.h"
58#include "idle.h"
59#include "irq.h"
60#include "gpio.h"
61#include "timer.h"
Matt Wagantall7cca4642012-02-01 16:43:24 -080062#include "pm.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070063#include "spm.h"
64#include "sirc.h"
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -060065#include "pm-boot.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070066
67/******************************************************************************
68 * Debug Definitions
69 *****************************************************************************/
70
71enum {
Murali Nalajalaa7efba12012-02-23 18:13:52 +053072 MSM_PM_DEBUG_SUSPEND = BIT(0),
73 MSM_PM_DEBUG_POWER_COLLAPSE = BIT(1),
74 MSM_PM_DEBUG_STATE = BIT(2),
75 MSM_PM_DEBUG_CLOCK = BIT(3),
76 MSM_PM_DEBUG_RESET_VECTOR = BIT(4),
77 MSM_PM_DEBUG_SMSM_STATE = BIT(5),
78 MSM_PM_DEBUG_IDLE = BIT(6),
79 MSM_PM_DEBUG_HOTPLUG = BIT(7),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070080};
81
82static int msm_pm_debug_mask;
Taniya Dase30a6b22012-03-20 11:37:45 +053083int power_collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070084module_param_named(
85 debug_mask, msm_pm_debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP
86);
87
88#define MSM_PM_DPRINTK(mask, level, message, ...) \
89 do { \
90 if ((mask) & msm_pm_debug_mask) \
91 printk(level message, ## __VA_ARGS__); \
92 } while (0)
93
94#define MSM_PM_DEBUG_PRINT_STATE(tag) \
95 do { \
96 MSM_PM_DPRINTK(MSM_PM_DEBUG_STATE, \
97 KERN_INFO, "%s: " \
98 "APPS_CLK_SLEEP_EN %x, APPS_PWRDOWN %x, " \
99 "SMSM_POWER_MASTER_DEM %x, SMSM_MODEM_STATE %x, " \
100 "SMSM_APPS_DEM %x\n", \
101 tag, \
102 __raw_readl(APPS_CLK_SLEEP_EN), \
103 __raw_readl(APPS_PWRDOWN), \
104 smsm_get_state(SMSM_POWER_MASTER_DEM), \
105 smsm_get_state(SMSM_MODEM_STATE), \
106 smsm_get_state(SMSM_APPS_DEM)); \
107 } while (0)
108
109#define MSM_PM_DEBUG_PRINT_SLEEP_INFO() \
110 do { \
111 if (msm_pm_debug_mask & MSM_PM_DEBUG_SMSM_STATE) \
112 smsm_print_sleep_info(msm_pm_smem_data->sleep_time, \
113 msm_pm_smem_data->resources_used, \
114 msm_pm_smem_data->irq_mask, \
115 msm_pm_smem_data->wakeup_reason, \
116 msm_pm_smem_data->pending_irqs); \
117 } while (0)
118
119
120/******************************************************************************
121 * Sleep Modes and Parameters
122 *****************************************************************************/
123
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700124static int msm_pm_idle_sleep_min_time = CONFIG_MSM7X00A_IDLE_SLEEP_MIN_TIME;
125module_param_named(
126 idle_sleep_min_time, msm_pm_idle_sleep_min_time,
127 int, S_IRUGO | S_IWUSR | S_IWGRP
128);
129
130enum {
131 MSM_PM_MODE_ATTR_SUSPEND,
132 MSM_PM_MODE_ATTR_IDLE,
133 MSM_PM_MODE_ATTR_LATENCY,
134 MSM_PM_MODE_ATTR_RESIDENCY,
135 MSM_PM_MODE_ATTR_NR,
136};
137
138static char *msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_NR] = {
139 [MSM_PM_MODE_ATTR_SUSPEND] = "suspend_enabled",
140 [MSM_PM_MODE_ATTR_IDLE] = "idle_enabled",
141 [MSM_PM_MODE_ATTR_LATENCY] = "latency",
142 [MSM_PM_MODE_ATTR_RESIDENCY] = "residency",
143};
144
145static char *msm_pm_sleep_mode_labels[MSM_PM_SLEEP_MODE_NR] = {
146 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE_SUSPEND] = " ",
147 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE] = "power_collapse",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700148 [MSM_PM_SLEEP_MODE_RAMP_DOWN_AND_WAIT_FOR_INTERRUPT] =
149 "ramp_down_and_wfi",
150 [MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT] = "wfi",
151 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE_NO_XO_SHUTDOWN] =
152 "power_collapse_no_xo_shutdown",
153 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE] =
154 "standalone_power_collapse",
155};
156
157static struct msm_pm_platform_data *msm_pm_modes;
Murali Nalajala2a0bbda2012-03-28 12:12:54 +0530158static struct msm_pm_irq_calls *msm_pm_irq_extns;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700159
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530160struct msm_pm_kobj_attribute {
161 unsigned int cpu;
162 struct kobj_attribute ka;
163};
164
165#define GET_CPU_OF_ATTR(attr) \
166 (container_of(attr, struct msm_pm_kobj_attribute, ka)->cpu)
167
168struct msm_pm_sysfs_sleep_mode {
169 struct kobject *kobj;
170 struct attribute_group attr_group;
171 struct attribute *attrs[MSM_PM_MODE_ATTR_NR + 1];
172 struct msm_pm_kobj_attribute kas[MSM_PM_MODE_ATTR_NR];
173};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700174
175/*
176 * Write out the attribute.
177 */
178static ssize_t msm_pm_mode_attr_show(
179 struct kobject *kobj, struct kobj_attribute *attr, char *buf)
180{
181 int ret = -EINVAL;
182 int i;
183
184 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
185 struct kernel_param kp;
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530186 unsigned int cpu;
187 struct msm_pm_platform_data *mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700188
189 if (msm_pm_sleep_mode_labels[i] == NULL)
190 continue;
191
192 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
193 continue;
194
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530195 cpu = GET_CPU_OF_ATTR(attr);
196 mode = &msm_pm_modes[MSM_PM_MODE(cpu, i)];
197
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700198 if (!strcmp(attr->attr.name,
199 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530200 u32 arg = mode->suspend_enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700201 kp.arg = &arg;
202 ret = param_get_ulong(buf, &kp);
203 } else if (!strcmp(attr->attr.name,
204 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530205 u32 arg = mode->idle_enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700206 kp.arg = &arg;
207 ret = param_get_ulong(buf, &kp);
208 } else if (!strcmp(attr->attr.name,
209 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_LATENCY])) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530210 u32 arg = mode->latency;
211 kp.arg = &arg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700212 ret = param_get_ulong(buf, &kp);
213 } else if (!strcmp(attr->attr.name,
214 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_RESIDENCY])) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530215 u32 arg = mode->residency;
216 kp.arg = &arg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700217 ret = param_get_ulong(buf, &kp);
218 }
219
220 break;
221 }
222
223 if (ret > 0) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530224 strlcat(buf, "\n", PAGE_SIZE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700225 ret++;
226 }
227
228 return ret;
229}
230
231/*
232 * Read in the new attribute value.
233 */
234static ssize_t msm_pm_mode_attr_store(struct kobject *kobj,
235 struct kobj_attribute *attr, const char *buf, size_t count)
236{
237 int ret = -EINVAL;
238 int i;
239
240 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
241 struct kernel_param kp;
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530242 unsigned int cpu;
243 struct msm_pm_platform_data *mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700244
245 if (msm_pm_sleep_mode_labels[i] == NULL)
246 continue;
247
248 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
249 continue;
250
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530251 cpu = GET_CPU_OF_ATTR(attr);
252 mode = &msm_pm_modes[MSM_PM_MODE(cpu, i)];
253
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700254 if (!strcmp(attr->attr.name,
255 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530256 kp.arg = &mode->suspend_enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700257 ret = param_set_byte(buf, &kp);
258 } else if (!strcmp(attr->attr.name,
259 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530260 kp.arg = &mode->idle_enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700261 ret = param_set_byte(buf, &kp);
262 } else if (!strcmp(attr->attr.name,
263 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_LATENCY])) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530264 kp.arg = &mode->latency;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700265 ret = param_set_ulong(buf, &kp);
266 } else if (!strcmp(attr->attr.name,
267 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_RESIDENCY])) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530268 kp.arg = &mode->residency;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700269 ret = param_set_ulong(buf, &kp);
270 }
271
272 break;
273 }
274
275 return ret ? ret : count;
276}
277
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530278 /* Add sysfs entries for one cpu. */
279static int __init msm_pm_mode_sysfs_add_cpu(
280 unsigned int cpu, struct kobject *modes_kobj)
281{
282 char cpu_name[8];
283 struct kobject *cpu_kobj;
284 struct msm_pm_sysfs_sleep_mode *mode = NULL;
285 int i, j, k;
286 int ret;
287
288 snprintf(cpu_name, sizeof(cpu_name), "cpu%u", cpu);
289 cpu_kobj = kobject_create_and_add(cpu_name, modes_kobj);
290 if (!cpu_kobj) {
291 pr_err("%s: cannot create %s kobject\n", __func__, cpu_name);
292 ret = -ENOMEM;
293 goto mode_sysfs_add_cpu_exit;
294 }
295
296 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
297 int idx = MSM_PM_MODE(cpu, i);
298
299 if ((!msm_pm_modes[idx].suspend_supported) &&
300 (!msm_pm_modes[idx].idle_supported))
301 continue;
302
303 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
304 if (!mode) {
305 pr_err("%s: cannot allocate memory for attributes\n",
306 __func__);
307 ret = -ENOMEM;
308 goto mode_sysfs_add_cpu_exit;
309 }
310
311 mode->kobj = kobject_create_and_add(
312 msm_pm_sleep_mode_labels[i], cpu_kobj);
313 if (!mode->kobj) {
314 pr_err("%s: cannot create kobject\n", __func__);
315 ret = -ENOMEM;
316 goto mode_sysfs_add_cpu_exit;
317 }
318
319 for (k = 0, j = 0; k < MSM_PM_MODE_ATTR_NR; k++) {
320 if ((k == MSM_PM_MODE_ATTR_IDLE) &&
321 !msm_pm_modes[idx].idle_supported)
322 continue;
323 if ((k == MSM_PM_MODE_ATTR_SUSPEND) &&
324 !msm_pm_modes[idx].suspend_supported)
325 continue;
326 mode->kas[j].cpu = cpu;
327 mode->kas[j].ka.attr.mode = 0644;
328 mode->kas[j].ka.show = msm_pm_mode_attr_show;
329 mode->kas[j].ka.store = msm_pm_mode_attr_store;
330 mode->kas[j].ka.attr.name = msm_pm_mode_attr_labels[k];
331 mode->attrs[j] = &mode->kas[j].ka.attr;
332 j++;
333 }
334 mode->attrs[j] = NULL;
335
336 mode->attr_group.attrs = mode->attrs;
337 ret = sysfs_create_group(mode->kobj, &mode->attr_group);
338 if (ret) {
339 printk(KERN_ERR
340 "%s: cannot create kobject attribute group\n",
341 __func__);
342 goto mode_sysfs_add_cpu_exit;
343 }
344 }
345
346 ret = 0;
347
348mode_sysfs_add_cpu_exit:
349 if (ret) {
350 if (mode && mode->kobj)
351 kobject_del(mode->kobj);
352 kfree(mode);
353 }
354
355 return ret;
356}
357
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700358/*
359 * Add sysfs entries for the sleep modes.
360 */
361static int __init msm_pm_mode_sysfs_add(void)
362{
363 struct kobject *module_kobj = NULL;
364 struct kobject *modes_kobj = NULL;
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530365 unsigned int cpu;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700366 int ret;
367
368 module_kobj = kset_find_obj(module_kset, KBUILD_MODNAME);
369 if (!module_kobj) {
370 printk(KERN_ERR "%s: cannot find kobject for module %s\n",
371 __func__, KBUILD_MODNAME);
372 ret = -ENOENT;
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530373 goto mode_sysfs_add_exit;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700374 }
375
376 modes_kobj = kobject_create_and_add("modes", module_kobj);
377 if (!modes_kobj) {
378 printk(KERN_ERR "%s: cannot create modes kobject\n", __func__);
379 ret = -ENOMEM;
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530380 goto mode_sysfs_add_exit;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700381 }
382
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530383 for_each_possible_cpu(cpu) {
384 ret = msm_pm_mode_sysfs_add_cpu(cpu, modes_kobj);
385 if (ret)
386 goto mode_sysfs_add_exit;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700387 }
388
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530389 ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700390
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530391mode_sysfs_add_exit:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700392 return ret;
393}
394
395void __init msm_pm_set_platform_data(
396 struct msm_pm_platform_data *data, int count)
397{
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530398 BUG_ON(MSM_PM_SLEEP_MODE_NR * num_possible_cpus() > count);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700399 msm_pm_modes = data;
400}
401
Murali Nalajala2a0bbda2012-03-28 12:12:54 +0530402void __init msm_pm_set_irq_extns(struct msm_pm_irq_calls *irq_calls)
403{
404 /* sanity check */
405 BUG_ON(irq_calls == NULL || irq_calls->irq_pending == NULL ||
406 irq_calls->idle_sleep_allowed == NULL ||
407 irq_calls->enter_sleep1 == NULL ||
408 irq_calls->enter_sleep2 == NULL ||
409 irq_calls->exit_sleep1 == NULL ||
410 irq_calls->exit_sleep2 == NULL ||
411 irq_calls->exit_sleep3 == NULL);
412
413 msm_pm_irq_extns = irq_calls;
414}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700415
416/******************************************************************************
417 * Sleep Limitations
418 *****************************************************************************/
419enum {
420 SLEEP_LIMIT_NONE = 0,
421 SLEEP_LIMIT_NO_TCXO_SHUTDOWN = 2,
422 SLEEP_LIMIT_MASK = 0x03,
423};
424
425#ifdef CONFIG_MSM_MEMORY_LOW_POWER_MODE
426enum {
427 SLEEP_RESOURCE_MEMORY_BIT0 = 0x0200,
428 SLEEP_RESOURCE_MEMORY_BIT1 = 0x0010,
429};
430#endif
431
432
433/******************************************************************************
434 * Configure Hardware for Power Down/Up
435 *****************************************************************************/
436
437#if defined(CONFIG_ARCH_MSM7X30)
Taniya Das298de8c2012-02-16 11:45:31 +0530438#define APPS_CLK_SLEEP_EN (MSM_APCS_GCC_BASE + 0x020)
439#define APPS_PWRDOWN (MSM_ACC0_BASE + 0x01c)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700440#define APPS_SECOP (MSM_TCSR_BASE + 0x038)
441#else /* defined(CONFIG_ARCH_MSM7X30) */
442#define APPS_CLK_SLEEP_EN (MSM_CSR_BASE + 0x11c)
443#define APPS_PWRDOWN (MSM_CSR_BASE + 0x440)
444#define APPS_STANDBY_CTL (MSM_CSR_BASE + 0x108)
445#endif /* defined(CONFIG_ARCH_MSM7X30) */
446
447/*
448 * Configure hardware registers in preparation for Apps power down.
449 */
450static void msm_pm_config_hw_before_power_down(void)
451{
452#if defined(CONFIG_ARCH_MSM7X30)
453 __raw_writel(1, APPS_PWRDOWN);
454 mb();
455 __raw_writel(4, APPS_SECOP);
456 mb();
457#elif defined(CONFIG_ARCH_MSM7X27)
458 __raw_writel(0x1f, APPS_CLK_SLEEP_EN);
459 mb();
460 __raw_writel(1, APPS_PWRDOWN);
461 mb();
462#elif defined(CONFIG_ARCH_MSM7x27A)
463 __raw_writel(0x7, APPS_CLK_SLEEP_EN);
464 mb();
465 __raw_writel(1, APPS_PWRDOWN);
466 mb();
467#else
468 __raw_writel(0x1f, APPS_CLK_SLEEP_EN);
469 mb();
470 __raw_writel(1, APPS_PWRDOWN);
471 mb();
472 __raw_writel(0, APPS_STANDBY_CTL);
473 mb();
474#endif
475}
476
477/*
478 * Clear hardware registers after Apps powers up.
479 */
480static void msm_pm_config_hw_after_power_up(void)
481{
482#if defined(CONFIG_ARCH_MSM7X30)
483 __raw_writel(0, APPS_SECOP);
484 mb();
485 __raw_writel(0, APPS_PWRDOWN);
486 mb();
487 msm_spm_reinit();
488#elif defined(CONFIG_ARCH_MSM7x27A)
489 __raw_writel(0, APPS_PWRDOWN);
490 mb();
491 __raw_writel(0, APPS_CLK_SLEEP_EN);
492 mb();
493#else
494 __raw_writel(0, APPS_PWRDOWN);
495 mb();
496 __raw_writel(0, APPS_CLK_SLEEP_EN);
497 mb();
498#endif
499}
500
501/*
502 * Configure hardware registers in preparation for SWFI.
503 */
504static void msm_pm_config_hw_before_swfi(void)
505{
506#if defined(CONFIG_ARCH_QSD8X50)
507 __raw_writel(0x1f, APPS_CLK_SLEEP_EN);
508 mb();
509#elif defined(CONFIG_ARCH_MSM7X27)
510 __raw_writel(0x0f, APPS_CLK_SLEEP_EN);
511 mb();
512#elif defined(CONFIG_ARCH_MSM7X27A)
513 __raw_writel(0x7, APPS_CLK_SLEEP_EN);
514 mb();
515#endif
516}
517
518/*
519 * Respond to timing out waiting for Modem
520 *
521 * NOTE: The function never returns.
522 */
523static void msm_pm_timeout(void)
524{
525#if defined(CONFIG_MSM_PM_TIMEOUT_RESET_CHIP)
526 printk(KERN_EMERG "%s(): resetting chip\n", __func__);
527 msm_proc_comm(PCOM_RESET_CHIP_IMM, NULL, NULL);
528#elif defined(CONFIG_MSM_PM_TIMEOUT_RESET_MODEM)
529 printk(KERN_EMERG "%s(): resetting modem\n", __func__);
530 msm_proc_comm_reset_modem_now();
531#elif defined(CONFIG_MSM_PM_TIMEOUT_HALT)
532 printk(KERN_EMERG "%s(): halting\n", __func__);
533#endif
534 for (;;)
535 ;
536}
537
538
539/******************************************************************************
540 * State Polling Definitions
541 *****************************************************************************/
542
543struct msm_pm_polled_group {
544 uint32_t group_id;
545
546 uint32_t bits_all_set;
547 uint32_t bits_all_clear;
548 uint32_t bits_any_set;
549 uint32_t bits_any_clear;
550
551 uint32_t value_read;
552};
553
554/*
555 * Return true if all bits indicated by flag are set in source.
556 */
557static inline bool msm_pm_all_set(uint32_t source, uint32_t flag)
558{
559 return (source & flag) == flag;
560}
561
562/*
563 * Return true if any bit indicated by flag are set in source.
564 */
565static inline bool msm_pm_any_set(uint32_t source, uint32_t flag)
566{
567 return !flag || (source & flag);
568}
569
570/*
571 * Return true if all bits indicated by flag are cleared in source.
572 */
573static inline bool msm_pm_all_clear(uint32_t source, uint32_t flag)
574{
575 return (~source & flag) == flag;
576}
577
578/*
579 * Return true if any bit indicated by flag are cleared in source.
580 */
581static inline bool msm_pm_any_clear(uint32_t source, uint32_t flag)
582{
583 return !flag || (~source & flag);
584}
585
586/*
587 * Poll the shared memory states as indicated by the poll groups.
588 *
589 * nr_grps: number of groups in the array
590 * grps: array of groups
591 *
592 * The function returns when conditions specified by any of the poll
593 * groups become true. The conditions specified by a poll group are
594 * deemed true when 1) at least one bit from bits_any_set is set OR one
595 * bit from bits_any_clear is cleared; and 2) all bits in bits_all_set
596 * are set; and 3) all bits in bits_all_clear are cleared.
597 *
598 * Return value:
599 * >=0: index of the poll group whose conditions have become true
600 * -ETIMEDOUT: timed out
601 */
602static int msm_pm_poll_state(int nr_grps, struct msm_pm_polled_group *grps)
603{
604 int i, k;
605
606 for (i = 0; i < 50000; i++) {
607 for (k = 0; k < nr_grps; k++) {
608 bool all_set, all_clear;
609 bool any_set, any_clear;
610
611 grps[k].value_read = smsm_get_state(grps[k].group_id);
612
613 all_set = msm_pm_all_set(grps[k].value_read,
614 grps[k].bits_all_set);
615 all_clear = msm_pm_all_clear(grps[k].value_read,
616 grps[k].bits_all_clear);
617 any_set = msm_pm_any_set(grps[k].value_read,
618 grps[k].bits_any_set);
619 any_clear = msm_pm_any_clear(grps[k].value_read,
620 grps[k].bits_any_clear);
621
622 if (all_set && all_clear && (any_set || any_clear))
623 return k;
624 }
625 udelay(50);
626 }
627
628 printk(KERN_ERR "%s failed:\n", __func__);
629 for (k = 0; k < nr_grps; k++)
630 printk(KERN_ERR "(%x, %x, %x, %x) %x\n",
631 grps[k].bits_all_set, grps[k].bits_all_clear,
632 grps[k].bits_any_set, grps[k].bits_any_clear,
633 grps[k].value_read);
634
635 return -ETIMEDOUT;
636}
637
638
639/******************************************************************************
640 * Suspend Max Sleep Time
641 *****************************************************************************/
642
643#define SCLK_HZ (32768)
644#define MSM_PM_SLEEP_TICK_LIMIT (0x6DDD000)
645
646#ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE
647static int msm_pm_sleep_time_override;
648module_param_named(sleep_time_override,
649 msm_pm_sleep_time_override, int, S_IRUGO | S_IWUSR | S_IWGRP);
650#endif
651
652static uint32_t msm_pm_max_sleep_time;
653
654/*
655 * Convert time from nanoseconds to slow clock ticks, then cap it to the
656 * specified limit
657 */
658static int64_t msm_pm_convert_and_cap_time(int64_t time_ns, int64_t limit)
659{
660 do_div(time_ns, NSEC_PER_SEC / SCLK_HZ);
661 return (time_ns > limit) ? limit : time_ns;
662}
663
664/*
665 * Set the sleep time for suspend. 0 means infinite sleep time.
666 */
667void msm_pm_set_max_sleep_time(int64_t max_sleep_time_ns)
668{
669 unsigned long flags;
670
671 local_irq_save(flags);
672 if (max_sleep_time_ns == 0) {
673 msm_pm_max_sleep_time = 0;
674 } else {
675 msm_pm_max_sleep_time = (uint32_t)msm_pm_convert_and_cap_time(
676 max_sleep_time_ns, MSM_PM_SLEEP_TICK_LIMIT);
677
678 if (msm_pm_max_sleep_time == 0)
679 msm_pm_max_sleep_time = 1;
680 }
681
682 MSM_PM_DPRINTK(MSM_PM_DEBUG_SUSPEND, KERN_INFO,
683 "%s(): Requested %lld ns Giving %u sclk ticks\n", __func__,
684 max_sleep_time_ns, msm_pm_max_sleep_time);
685 local_irq_restore(flags);
686}
687EXPORT_SYMBOL(msm_pm_set_max_sleep_time);
688
689
690/******************************************************************************
691 * CONFIG_MSM_IDLE_STATS
692 *****************************************************************************/
693
694#ifdef CONFIG_MSM_IDLE_STATS
695enum msm_pm_time_stats_id {
696 MSM_PM_STAT_REQUESTED_IDLE,
697 MSM_PM_STAT_IDLE_SPIN,
698 MSM_PM_STAT_IDLE_WFI,
699 MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE,
700 MSM_PM_STAT_IDLE_FAILED_STANDALONE_POWER_COLLAPSE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700701 MSM_PM_STAT_IDLE_POWER_COLLAPSE,
702 MSM_PM_STAT_IDLE_FAILED_POWER_COLLAPSE,
703 MSM_PM_STAT_SUSPEND,
704 MSM_PM_STAT_FAILED_SUSPEND,
705 MSM_PM_STAT_NOT_IDLE,
706 MSM_PM_STAT_COUNT
707};
708
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530709struct msm_pm_time_stats {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700710 const char *name;
711 int64_t first_bucket_time;
712 int bucket[CONFIG_MSM_IDLE_STATS_BUCKET_COUNT];
713 int64_t min_time[CONFIG_MSM_IDLE_STATS_BUCKET_COUNT];
714 int64_t max_time[CONFIG_MSM_IDLE_STATS_BUCKET_COUNT];
715 int count;
716 int64_t total_time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700717};
718
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530719struct msm_pm_cpu_time_stats {
720 struct msm_pm_time_stats stats[MSM_PM_STAT_COUNT];
721};
722
723static DEFINE_PER_CPU_SHARED_ALIGNED(
724 struct msm_pm_cpu_time_stats, msm_pm_stats);
725
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700726static uint32_t msm_pm_sleep_limit = SLEEP_LIMIT_NONE;
727
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530728static DEFINE_SPINLOCK(msm_pm_stats_lock);
729
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700730/*
731 * Add the given time data to the statistics collection.
732 */
733static void msm_pm_add_stat(enum msm_pm_time_stats_id id, int64_t t)
734{
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530735 unsigned long flags;
736 struct msm_pm_time_stats *stats;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700737 int i;
738 int64_t bt;
739
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530740 spin_lock_irqsave(&msm_pm_stats_lock, flags);
741 stats = __get_cpu_var(msm_pm_stats).stats;
742
743 stats[id].total_time += t;
744 stats[id].count++;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700745
746 bt = t;
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530747 do_div(bt, stats[id].first_bucket_time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700748
749 if (bt < 1ULL << (CONFIG_MSM_IDLE_STATS_BUCKET_SHIFT *
750 (CONFIG_MSM_IDLE_STATS_BUCKET_COUNT - 1)))
751 i = DIV_ROUND_UP(fls((uint32_t)bt),
752 CONFIG_MSM_IDLE_STATS_BUCKET_SHIFT);
753 else
754 i = CONFIG_MSM_IDLE_STATS_BUCKET_COUNT - 1;
755
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530756 if (i >= CONFIG_MSM_IDLE_STATS_BUCKET_COUNT)
757 i = CONFIG_MSM_IDLE_STATS_BUCKET_COUNT - 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700758
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530759 stats[id].bucket[i]++;
760
761 if (t < stats[id].min_time[i] || !stats[id].max_time[i])
762 stats[id].min_time[i] = t;
763 if (t > stats[id].max_time[i])
764 stats[id].max_time[i] = t;
765
766 spin_unlock_irqrestore(&msm_pm_stats_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700767}
768
769/*
770 * Helper function of snprintf where buf is auto-incremented, size is auto-
771 * decremented, and there is no return value.
772 *
773 * NOTE: buf and size must be l-values (e.g. variables)
774 */
775#define SNPRINTF(buf, size, format, ...) \
776 do { \
777 if (size > 0) { \
778 int ret; \
779 ret = snprintf(buf, size, format, ## __VA_ARGS__); \
780 if (ret > size) { \
781 buf += size; \
782 size = 0; \
783 } else { \
784 buf += ret; \
785 size -= ret; \
786 } \
787 } \
788 } while (0)
789
790/*
791 * Write out the power management statistics.
792 */
793static int msm_pm_read_proc
794 (char *page, char **start, off_t off, int count, int *eof, void *data)
795{
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530796 unsigned int cpu = off / MSM_PM_STAT_COUNT;
797 int id = off % MSM_PM_STAT_COUNT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700798 char *p = page;
799
800 if (count < 1024) {
801 *start = (char *) 0;
802 *eof = 0;
803 return 0;
804 }
805
806 if (!off) {
807 SNPRINTF(p, count, "Last power collapse voted ");
808 if ((msm_pm_sleep_limit & SLEEP_LIMIT_MASK) ==
809 SLEEP_LIMIT_NONE)
810 SNPRINTF(p, count, "for TCXO shutdown\n\n");
811 else
812 SNPRINTF(p, count, "against TCXO shutdown\n\n");
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530813 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700814
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530815 if (cpu < num_possible_cpus()) {
816 unsigned long flags;
817 struct msm_pm_time_stats *stats;
818 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700819 int64_t bucket_time;
820 int64_t s;
821 uint32_t ns;
822
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530823 spin_lock_irqsave(&msm_pm_stats_lock, flags);
824 stats = per_cpu(msm_pm_stats, cpu).stats;
825
826 s = stats[id].total_time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700827 ns = do_div(s, NSEC_PER_SEC);
828 SNPRINTF(p, count,
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530829 "[cpu %u] %s:\n"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700830 " count: %7d\n"
831 " total_time: %lld.%09u\n",
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530832 cpu, stats[id].name,
833 stats[id].count,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700834 s, ns);
835
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530836 bucket_time = stats[id].first_bucket_time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700837 for (i = 0; i < CONFIG_MSM_IDLE_STATS_BUCKET_COUNT - 1; i++) {
838 s = bucket_time;
839 ns = do_div(s, NSEC_PER_SEC);
840 SNPRINTF(p, count,
841 " <%6lld.%09u: %7d (%lld-%lld)\n",
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530842 s, ns, stats[id].bucket[i],
843 stats[id].min_time[i],
844 stats[id].max_time[i]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700845
846 bucket_time <<= CONFIG_MSM_IDLE_STATS_BUCKET_SHIFT;
847 }
848
849 SNPRINTF(p, count, " >=%6lld.%09u: %7d (%lld-%lld)\n",
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530850 s, ns, stats[id].bucket[i],
851 stats[id].min_time[i],
852 stats[id].max_time[i]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700853
854 *start = (char *) 1;
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530855 *eof = (off + 1 >= MSM_PM_STAT_COUNT * num_possible_cpus());
856
857 spin_unlock_irqrestore(&msm_pm_stats_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700858 }
859
860 return p - page;
861}
862#undef SNPRINTF
863
864#define MSM_PM_STATS_RESET "reset"
865
866/*
867 * Reset the power management statistics values.
868 */
869static int msm_pm_write_proc(struct file *file, const char __user *buffer,
870 unsigned long count, void *data)
871{
872 char buf[sizeof(MSM_PM_STATS_RESET)];
873 int ret;
874 unsigned long flags;
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530875 unsigned int cpu;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700876
877 if (count < strlen(MSM_PM_STATS_RESET)) {
878 ret = -EINVAL;
879 goto write_proc_failed;
880 }
881
882 if (copy_from_user(buf, buffer, strlen(MSM_PM_STATS_RESET))) {
883 ret = -EFAULT;
884 goto write_proc_failed;
885 }
886
887 if (memcmp(buf, MSM_PM_STATS_RESET, strlen(MSM_PM_STATS_RESET))) {
888 ret = -EINVAL;
889 goto write_proc_failed;
890 }
891
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530892 spin_lock_irqsave(&msm_pm_stats_lock, flags);
893 for_each_possible_cpu(cpu) {
894 struct msm_pm_time_stats *stats;
895 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700896
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530897 stats = per_cpu(msm_pm_stats, cpu).stats;
898 for (i = 0; i < MSM_PM_STAT_COUNT; i++) {
899 memset(stats[i].bucket,
900 0, sizeof(stats[i].bucket));
901 memset(stats[i].min_time,
902 0, sizeof(stats[i].min_time));
903 memset(stats[i].max_time,
904 0, sizeof(stats[i].max_time));
905 stats[i].count = 0;
906 stats[i].total_time = 0;
907 }
908 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700909 msm_pm_sleep_limit = SLEEP_LIMIT_NONE;
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530910 spin_unlock_irqrestore(&msm_pm_stats_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700911
912 return count;
913
914write_proc_failed:
915 return ret;
916}
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530917
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700918#undef MSM_PM_STATS_RESET
919#endif /* CONFIG_MSM_IDLE_STATS */
920
921
922/******************************************************************************
923 * Shared Memory Bits
924 *****************************************************************************/
925
926#define DEM_MASTER_BITS_PER_CPU 6
927
928/* Power Master State Bits - Per CPU */
929#define DEM_MASTER_SMSM_RUN \
930 (0x01UL << (DEM_MASTER_BITS_PER_CPU * SMSM_APPS_STATE))
931#define DEM_MASTER_SMSM_RSA \
932 (0x02UL << (DEM_MASTER_BITS_PER_CPU * SMSM_APPS_STATE))
933#define DEM_MASTER_SMSM_PWRC_EARLY_EXIT \
934 (0x04UL << (DEM_MASTER_BITS_PER_CPU * SMSM_APPS_STATE))
935#define DEM_MASTER_SMSM_SLEEP_EXIT \
936 (0x08UL << (DEM_MASTER_BITS_PER_CPU * SMSM_APPS_STATE))
937#define DEM_MASTER_SMSM_READY \
938 (0x10UL << (DEM_MASTER_BITS_PER_CPU * SMSM_APPS_STATE))
939#define DEM_MASTER_SMSM_SLEEP \
940 (0x20UL << (DEM_MASTER_BITS_PER_CPU * SMSM_APPS_STATE))
941
942/* Power Slave State Bits */
943#define DEM_SLAVE_SMSM_RUN (0x0001)
944#define DEM_SLAVE_SMSM_PWRC (0x0002)
945#define DEM_SLAVE_SMSM_PWRC_DELAY (0x0004)
946#define DEM_SLAVE_SMSM_PWRC_EARLY_EXIT (0x0008)
947#define DEM_SLAVE_SMSM_WFPI (0x0010)
948#define DEM_SLAVE_SMSM_SLEEP (0x0020)
949#define DEM_SLAVE_SMSM_SLEEP_EXIT (0x0040)
950#define DEM_SLAVE_SMSM_MSGS_REDUCED (0x0080)
951#define DEM_SLAVE_SMSM_RESET (0x0100)
952#define DEM_SLAVE_SMSM_PWRC_SUSPEND (0x0200)
953
954
955/******************************************************************************
956 * Shared Memory Data
957 *****************************************************************************/
958
959#define DEM_MAX_PORT_NAME_LEN (20)
960
961struct msm_pm_smem_t {
962 uint32_t sleep_time;
963 uint32_t irq_mask;
964 uint32_t resources_used;
965 uint32_t reserved1;
966
967 uint32_t wakeup_reason;
968 uint32_t pending_irqs;
969 uint32_t rpc_prog;
970 uint32_t rpc_proc;
971 char smd_port_name[DEM_MAX_PORT_NAME_LEN];
972 uint32_t reserved2;
973};
974
975
976/******************************************************************************
977 *
978 *****************************************************************************/
979static struct msm_pm_smem_t *msm_pm_smem_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700980static atomic_t msm_pm_init_done = ATOMIC_INIT(0);
981
982static int msm_pm_modem_busy(void)
983{
984 if (!(smsm_get_state(SMSM_POWER_MASTER_DEM) & DEM_MASTER_SMSM_READY)) {
985 MSM_PM_DPRINTK(MSM_PM_DEBUG_POWER_COLLAPSE,
986 KERN_INFO, "%s(): master not ready\n", __func__);
987 return -EBUSY;
988 }
989
990 return 0;
991}
992
993/*
994 * Power collapse the Apps processor. This function executes the handshake
995 * protocol with Modem.
996 *
997 * Return value:
998 * -EAGAIN: modem reset occurred or early exit from power collapse
999 * -EBUSY: modem not ready for our power collapse -- no power loss
1000 * -ETIMEDOUT: timed out waiting for modem's handshake -- no power loss
1001 * 0: success
1002 */
1003static int msm_pm_power_collapse
1004 (bool from_idle, uint32_t sleep_delay, uint32_t sleep_limit)
1005{
1006 struct msm_pm_polled_group state_grps[2];
1007 unsigned long saved_acpuclk_rate;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001008 int collapsed = 0;
1009 int ret;
1010
1011 MSM_PM_DPRINTK(MSM_PM_DEBUG_SUSPEND|MSM_PM_DEBUG_POWER_COLLAPSE,
1012 KERN_INFO, "%s(): idle %d, delay %u, limit %u\n", __func__,
1013 (int)from_idle, sleep_delay, sleep_limit);
1014
1015 if (!(smsm_get_state(SMSM_POWER_MASTER_DEM) & DEM_MASTER_SMSM_READY)) {
1016 MSM_PM_DPRINTK(
1017 MSM_PM_DEBUG_SUSPEND | MSM_PM_DEBUG_POWER_COLLAPSE,
1018 KERN_INFO, "%s(): master not ready\n", __func__);
1019 ret = -EBUSY;
1020 goto power_collapse_bail;
1021 }
1022
1023 memset(msm_pm_smem_data, 0, sizeof(*msm_pm_smem_data));
1024
Murali Nalajala41786ab2012-03-06 10:47:32 +05301025 if (cpu_is_msm8625()) {
1026 /* Program the SPM */
1027 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_POWER_COLLAPSE,
1028 false);
1029 WARN_ON(ret);
1030 }
1031
Murali Nalajala2a0bbda2012-03-28 12:12:54 +05301032 msm_pm_irq_extns->enter_sleep1(true, from_idle,
1033 &msm_pm_smem_data->irq_mask);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001034 msm_sirc_enter_sleep();
1035 msm_gpio_enter_sleep(from_idle);
1036
1037 msm_pm_smem_data->sleep_time = sleep_delay;
1038 msm_pm_smem_data->resources_used = sleep_limit;
1039
1040 /* Enter PWRC/PWRC_SUSPEND */
1041
1042 if (from_idle)
1043 smsm_change_state(SMSM_APPS_DEM, DEM_SLAVE_SMSM_RUN,
1044 DEM_SLAVE_SMSM_PWRC);
1045 else
1046 smsm_change_state(SMSM_APPS_DEM, DEM_SLAVE_SMSM_RUN,
1047 DEM_SLAVE_SMSM_PWRC | DEM_SLAVE_SMSM_PWRC_SUSPEND);
1048
1049 MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): PWRC");
1050 MSM_PM_DEBUG_PRINT_SLEEP_INFO();
1051
1052 memset(state_grps, 0, sizeof(state_grps));
1053 state_grps[0].group_id = SMSM_POWER_MASTER_DEM;
1054 state_grps[0].bits_all_set = DEM_MASTER_SMSM_RSA;
1055 state_grps[1].group_id = SMSM_MODEM_STATE;
1056 state_grps[1].bits_all_set = SMSM_RESET;
1057
1058 ret = msm_pm_poll_state(ARRAY_SIZE(state_grps), state_grps);
1059
1060 if (ret < 0) {
1061 printk(KERN_EMERG "%s(): power collapse entry "
1062 "timed out waiting for Modem's response\n", __func__);
1063 msm_pm_timeout();
1064 }
1065
1066 if (ret == 1) {
1067 MSM_PM_DPRINTK(
1068 MSM_PM_DEBUG_SUSPEND|MSM_PM_DEBUG_POWER_COLLAPSE,
1069 KERN_INFO,
1070 "%s(): msm_pm_poll_state detected Modem reset\n",
1071 __func__);
1072 goto power_collapse_early_exit;
1073 }
1074
1075 /* DEM Master in RSA */
1076
1077 MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): PWRC RSA");
1078
Murali Nalajala2a0bbda2012-03-28 12:12:54 +05301079 ret = msm_pm_irq_extns->enter_sleep2(true, from_idle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001080 if (ret < 0) {
1081 MSM_PM_DPRINTK(
1082 MSM_PM_DEBUG_SUSPEND|MSM_PM_DEBUG_POWER_COLLAPSE,
1083 KERN_INFO,
1084 "%s(): msm_irq_enter_sleep2 aborted, %d\n", __func__,
1085 ret);
1086 goto power_collapse_early_exit;
1087 }
1088
1089 msm_pm_config_hw_before_power_down();
1090 MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): pre power down");
1091
1092 saved_acpuclk_rate = acpuclk_power_collapse();
1093 MSM_PM_DPRINTK(MSM_PM_DEBUG_CLOCK, KERN_INFO,
1094 "%s(): change clock rate (old rate = %lu)\n", __func__,
1095 saved_acpuclk_rate);
1096
1097 if (saved_acpuclk_rate == 0) {
1098 msm_pm_config_hw_after_power_up();
1099 goto power_collapse_early_exit;
1100 }
1101
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -06001102 msm_pm_boot_config_before_pc(smp_processor_id(),
1103 virt_to_phys(msm_pm_collapse_exit));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001104
1105#ifdef CONFIG_VFP
1106 if (from_idle)
1107 vfp_flush_context();
1108#endif
1109
1110#ifdef CONFIG_CACHE_L2X0
1111 l2x0_suspend();
1112#endif
1113
1114 collapsed = msm_pm_collapse();
1115
1116#ifdef CONFIG_CACHE_L2X0
1117 l2x0_resume(collapsed);
1118#endif
1119
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -06001120 msm_pm_boot_config_after_pc(smp_processor_id());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001121
1122 if (collapsed) {
1123#ifdef CONFIG_VFP
1124 if (from_idle)
1125 vfp_reinit();
1126#endif
1127 cpu_init();
1128 local_fiq_enable();
1129 }
1130
1131 MSM_PM_DPRINTK(MSM_PM_DEBUG_SUSPEND | MSM_PM_DEBUG_POWER_COLLAPSE,
1132 KERN_INFO,
1133 "%s(): msm_pm_collapse returned %d\n", __func__, collapsed);
1134
1135 MSM_PM_DPRINTK(MSM_PM_DEBUG_CLOCK, KERN_INFO,
1136 "%s(): restore clock rate to %lu\n", __func__,
1137 saved_acpuclk_rate);
1138 if (acpuclk_set_rate(smp_processor_id(), saved_acpuclk_rate,
1139 SETRATE_PC) < 0)
1140 printk(KERN_ERR "%s(): failed to restore clock rate(%lu)\n",
1141 __func__, saved_acpuclk_rate);
1142
Murali Nalajala2a0bbda2012-03-28 12:12:54 +05301143 msm_pm_irq_extns->exit_sleep1(msm_pm_smem_data->irq_mask,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001144 msm_pm_smem_data->wakeup_reason,
1145 msm_pm_smem_data->pending_irqs);
1146
1147 msm_pm_config_hw_after_power_up();
1148 MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): post power up");
1149
1150 memset(state_grps, 0, sizeof(state_grps));
1151 state_grps[0].group_id = SMSM_POWER_MASTER_DEM;
1152 state_grps[0].bits_any_set =
1153 DEM_MASTER_SMSM_RSA | DEM_MASTER_SMSM_PWRC_EARLY_EXIT;
1154 state_grps[1].group_id = SMSM_MODEM_STATE;
1155 state_grps[1].bits_all_set = SMSM_RESET;
1156
1157 ret = msm_pm_poll_state(ARRAY_SIZE(state_grps), state_grps);
1158
1159 if (ret < 0) {
1160 printk(KERN_EMERG "%s(): power collapse exit "
1161 "timed out waiting for Modem's response\n", __func__);
1162 msm_pm_timeout();
1163 }
1164
1165 if (ret == 1) {
1166 MSM_PM_DPRINTK(
1167 MSM_PM_DEBUG_SUSPEND|MSM_PM_DEBUG_POWER_COLLAPSE,
1168 KERN_INFO,
1169 "%s(): msm_pm_poll_state detected Modem reset\n",
1170 __func__);
1171 goto power_collapse_early_exit;
1172 }
1173
1174 /* Sanity check */
1175 if (collapsed) {
1176 BUG_ON(!(state_grps[0].value_read & DEM_MASTER_SMSM_RSA));
1177 } else {
1178 BUG_ON(!(state_grps[0].value_read &
1179 DEM_MASTER_SMSM_PWRC_EARLY_EXIT));
1180 goto power_collapse_early_exit;
1181 }
1182
1183 /* Enter WFPI */
1184
1185 smsm_change_state(SMSM_APPS_DEM,
1186 DEM_SLAVE_SMSM_PWRC | DEM_SLAVE_SMSM_PWRC_SUSPEND,
1187 DEM_SLAVE_SMSM_WFPI);
1188
1189 MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): WFPI");
1190
1191 memset(state_grps, 0, sizeof(state_grps));
1192 state_grps[0].group_id = SMSM_POWER_MASTER_DEM;
1193 state_grps[0].bits_all_set = DEM_MASTER_SMSM_RUN;
1194 state_grps[1].group_id = SMSM_MODEM_STATE;
1195 state_grps[1].bits_all_set = SMSM_RESET;
1196
1197 ret = msm_pm_poll_state(ARRAY_SIZE(state_grps), state_grps);
1198
1199 if (ret < 0) {
1200 printk(KERN_EMERG "%s(): power collapse WFPI "
1201 "timed out waiting for Modem's response\n", __func__);
1202 msm_pm_timeout();
1203 }
1204
1205 if (ret == 1) {
1206 MSM_PM_DPRINTK(
1207 MSM_PM_DEBUG_SUSPEND|MSM_PM_DEBUG_POWER_COLLAPSE,
1208 KERN_INFO,
1209 "%s(): msm_pm_poll_state detected Modem reset\n",
1210 __func__);
1211 ret = -EAGAIN;
1212 goto power_collapse_restore_gpio_bail;
1213 }
1214
1215 /* DEM Master == RUN */
1216
1217 MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): WFPI RUN");
1218 MSM_PM_DEBUG_PRINT_SLEEP_INFO();
1219
Murali Nalajala2a0bbda2012-03-28 12:12:54 +05301220 msm_pm_irq_extns->exit_sleep2(msm_pm_smem_data->irq_mask,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001221 msm_pm_smem_data->wakeup_reason,
1222 msm_pm_smem_data->pending_irqs);
Murali Nalajala2a0bbda2012-03-28 12:12:54 +05301223 msm_pm_irq_extns->exit_sleep3(msm_pm_smem_data->irq_mask,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001224 msm_pm_smem_data->wakeup_reason,
1225 msm_pm_smem_data->pending_irqs);
1226 msm_gpio_exit_sleep();
1227 msm_sirc_exit_sleep();
1228
1229 smsm_change_state(SMSM_APPS_DEM,
1230 DEM_SLAVE_SMSM_WFPI, DEM_SLAVE_SMSM_RUN);
1231
1232 MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): RUN");
1233
1234 smd_sleep_exit();
Murali Nalajala41786ab2012-03-06 10:47:32 +05301235
1236 if (cpu_is_msm8625()) {
1237 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING,
1238 false);
1239 WARN_ON(ret);
1240 }
1241
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001242 return 0;
1243
1244power_collapse_early_exit:
1245 /* Enter PWRC_EARLY_EXIT */
1246
1247 smsm_change_state(SMSM_APPS_DEM,
1248 DEM_SLAVE_SMSM_PWRC | DEM_SLAVE_SMSM_PWRC_SUSPEND,
1249 DEM_SLAVE_SMSM_PWRC_EARLY_EXIT);
1250
1251 MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): EARLY_EXIT");
1252
1253 memset(state_grps, 0, sizeof(state_grps));
1254 state_grps[0].group_id = SMSM_POWER_MASTER_DEM;
1255 state_grps[0].bits_all_set = DEM_MASTER_SMSM_PWRC_EARLY_EXIT;
1256 state_grps[1].group_id = SMSM_MODEM_STATE;
1257 state_grps[1].bits_all_set = SMSM_RESET;
1258
1259 ret = msm_pm_poll_state(ARRAY_SIZE(state_grps), state_grps);
1260 MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): EARLY_EXIT EE");
1261
1262 if (ret < 0) {
1263 printk(KERN_EMERG "%s(): power collapse EARLY_EXIT "
1264 "timed out waiting for Modem's response\n", __func__);
1265 msm_pm_timeout();
1266 }
1267
1268 if (ret == 1) {
1269 MSM_PM_DPRINTK(
1270 MSM_PM_DEBUG_SUSPEND|MSM_PM_DEBUG_POWER_COLLAPSE,
1271 KERN_INFO,
1272 "%s(): msm_pm_poll_state detected Modem reset\n",
1273 __func__);
1274 }
1275
1276 /* DEM Master == RESET or PWRC_EARLY_EXIT */
1277
1278 ret = -EAGAIN;
1279
1280power_collapse_restore_gpio_bail:
1281 msm_gpio_exit_sleep();
1282 msm_sirc_exit_sleep();
1283
1284 /* Enter RUN */
1285 smsm_change_state(SMSM_APPS_DEM,
1286 DEM_SLAVE_SMSM_PWRC | DEM_SLAVE_SMSM_PWRC_SUSPEND |
1287 DEM_SLAVE_SMSM_PWRC_EARLY_EXIT, DEM_SLAVE_SMSM_RUN);
1288
1289 MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): RUN");
1290
1291 if (collapsed)
1292 smd_sleep_exit();
1293
1294power_collapse_bail:
Murali Nalajala41786ab2012-03-06 10:47:32 +05301295 if (cpu_is_msm8625()) {
1296 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING,
1297 false);
1298 WARN_ON(ret);
1299 }
1300
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001301 return ret;
1302}
1303
1304/*
1305 * Power collapse the Apps processor without involving Modem.
1306 *
1307 * Return value:
1308 * 0: success
1309 */
Murali Nalajala41786ab2012-03-06 10:47:32 +05301310static int msm_pm_power_collapse_standalone(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001311{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001312 int collapsed = 0;
1313 int ret;
Murali Nalajala41786ab2012-03-06 10:47:32 +05301314 void *entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001315
1316 MSM_PM_DPRINTK(MSM_PM_DEBUG_SUSPEND|MSM_PM_DEBUG_POWER_COLLAPSE,
1317 KERN_INFO, "%s()\n", __func__);
1318
1319 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_POWER_COLLAPSE, false);
1320 WARN_ON(ret);
1321
Murali Nalajala41786ab2012-03-06 10:47:32 +05301322 entry = (!smp_processor_id() || from_idle) ?
1323 msm_pm_collapse_exit : msm_secondary_startup;
1324
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -06001325 msm_pm_boot_config_before_pc(smp_processor_id(),
Murali Nalajala41786ab2012-03-06 10:47:32 +05301326 virt_to_phys(entry));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001327
1328#ifdef CONFIG_VFP
1329 vfp_flush_context();
1330#endif
1331
1332#ifdef CONFIG_CACHE_L2X0
Murali Nalajala41786ab2012-03-06 10:47:32 +05301333 if (!cpu_is_msm8625())
1334 l2x0_suspend();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001335#endif
1336
1337 collapsed = msm_pm_collapse();
1338
1339#ifdef CONFIG_CACHE_L2X0
Murali Nalajala41786ab2012-03-06 10:47:32 +05301340 if (!cpu_is_msm8625())
1341 l2x0_resume(collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001342#endif
1343
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -06001344 msm_pm_boot_config_after_pc(smp_processor_id());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001345
1346 if (collapsed) {
1347#ifdef CONFIG_VFP
1348 vfp_reinit();
1349#endif
1350 cpu_init();
1351 local_fiq_enable();
1352 }
1353
1354 MSM_PM_DPRINTK(MSM_PM_DEBUG_SUSPEND | MSM_PM_DEBUG_POWER_COLLAPSE,
1355 KERN_INFO,
1356 "%s(): msm_pm_collapse returned %d\n", __func__, collapsed);
1357
1358 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
1359 WARN_ON(ret);
1360
1361 return 0;
1362}
1363
1364/*
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001365 * Bring the Apps processor to SWFI.
1366 *
1367 * Return value:
1368 * -EIO: could not ramp Apps processor clock
1369 * 0: success
1370 */
1371static int msm_pm_swfi(bool ramp_acpu)
1372{
1373 unsigned long saved_acpuclk_rate = 0;
1374
1375 if (ramp_acpu) {
1376 saved_acpuclk_rate = acpuclk_wait_for_irq();
1377 MSM_PM_DPRINTK(MSM_PM_DEBUG_CLOCK, KERN_INFO,
1378 "%s(): change clock rate (old rate = %lu)\n", __func__,
1379 saved_acpuclk_rate);
1380
1381 if (!saved_acpuclk_rate)
1382 return -EIO;
1383 }
1384
Murali Nalajala41786ab2012-03-06 10:47:32 +05301385 if (!cpu_is_msm8625())
1386 msm_pm_config_hw_before_swfi();
1387
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001388 msm_arch_idle();
1389
1390 if (ramp_acpu) {
1391 MSM_PM_DPRINTK(MSM_PM_DEBUG_CLOCK, KERN_INFO,
1392 "%s(): restore clock rate to %lu\n", __func__,
1393 saved_acpuclk_rate);
1394 if (acpuclk_set_rate(smp_processor_id(), saved_acpuclk_rate,
1395 SETRATE_SWFI) < 0)
1396 printk(KERN_ERR
1397 "%s(): failed to restore clock rate(%lu)\n",
1398 __func__, saved_acpuclk_rate);
1399 }
1400
1401 return 0;
1402}
1403
1404
1405/******************************************************************************
1406 * External Idle/Suspend Functions
1407 *****************************************************************************/
1408
1409/*
1410 * Put CPU in low power mode.
1411 */
1412void arch_idle(void)
1413{
1414 bool allow[MSM_PM_SLEEP_MODE_NR];
1415 uint32_t sleep_limit = SLEEP_LIMIT_NONE;
1416
1417 int latency_qos;
1418 int64_t timer_expiration;
1419
1420 int low_power;
1421 int ret;
1422 int i;
Murali Nalajala0df9fee2012-01-12 15:26:09 +05301423 unsigned int cpu;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001424
1425#ifdef CONFIG_MSM_IDLE_STATS
1426 int64_t t1;
1427 static int64_t t2;
1428 int exit_stat;
Murali Nalajala41786ab2012-03-06 10:47:32 +05301429 #endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001430
1431 if (!atomic_read(&msm_pm_init_done))
1432 return;
1433
Murali Nalajala0df9fee2012-01-12 15:26:09 +05301434 cpu = smp_processor_id();
1435
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001436 latency_qos = pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
1437 timer_expiration = msm_timer_enter_idle();
1438
1439#ifdef CONFIG_MSM_IDLE_STATS
1440 t1 = ktime_to_ns(ktime_get());
1441 msm_pm_add_stat(MSM_PM_STAT_NOT_IDLE, t1 - t2);
1442 msm_pm_add_stat(MSM_PM_STAT_REQUESTED_IDLE, timer_expiration);
Murali Nalajala7744d162012-01-13 13:06:03 +05301443
1444 exit_stat = MSM_PM_STAT_IDLE_SPIN;
1445 low_power = 0;
Murali Nalajala41786ab2012-03-06 10:47:32 +05301446#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001447
1448 for (i = 0; i < ARRAY_SIZE(allow); i++)
1449 allow[i] = true;
1450
Murali Nalajala41786ab2012-03-06 10:47:32 +05301451 if (num_online_cpus() > 1 ||
1452 (timer_expiration < msm_pm_idle_sleep_min_time) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001453#ifdef CONFIG_HAS_WAKELOCK
1454 has_wake_lock(WAKE_LOCK_IDLE) ||
1455#endif
Murali Nalajala2a0bbda2012-03-28 12:12:54 +05301456 !msm_pm_irq_extns->idle_sleep_allowed()) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001457 allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE] = false;
1458 allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_NO_XO_SHUTDOWN] = false;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001459 }
1460
1461 for (i = 0; i < ARRAY_SIZE(allow); i++) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +05301462 struct msm_pm_platform_data *mode =
1463 &msm_pm_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001464 if (!mode->idle_supported || !mode->idle_enabled ||
1465 mode->latency >= latency_qos ||
1466 mode->residency * 1000ULL >= timer_expiration)
1467 allow[i] = false;
1468 }
1469
1470 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE] ||
1471 allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_NO_XO_SHUTDOWN]) {
1472 uint32_t wait_us = CONFIG_MSM_IDLE_WAIT_ON_MODEM;
1473 while (msm_pm_modem_busy() && wait_us) {
1474 if (wait_us > 100) {
1475 udelay(100);
1476 wait_us -= 100;
1477 } else {
1478 udelay(wait_us);
1479 wait_us = 0;
1480 }
1481 }
1482
1483 if (msm_pm_modem_busy()) {
1484 allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE] = false;
1485 allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_NO_XO_SHUTDOWN]
1486 = false;
1487 }
1488 }
1489
1490 MSM_PM_DPRINTK(MSM_PM_DEBUG_IDLE, KERN_INFO,
1491 "%s(): latency qos %d, next timer %lld, sleep limit %u\n",
1492 __func__, latency_qos, timer_expiration, sleep_limit);
1493
1494 for (i = 0; i < ARRAY_SIZE(allow); i++)
1495 MSM_PM_DPRINTK(MSM_PM_DEBUG_IDLE, KERN_INFO,
1496 "%s(): allow %s: %d\n", __func__,
1497 msm_pm_sleep_mode_labels[i], (int)allow[i]);
1498
1499 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE] ||
1500 allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_NO_XO_SHUTDOWN]) {
1501 uint32_t sleep_delay;
1502
1503 sleep_delay = (uint32_t) msm_pm_convert_and_cap_time(
1504 timer_expiration, MSM_PM_SLEEP_TICK_LIMIT);
1505 if (sleep_delay == 0) /* 0 would mean infinite time */
1506 sleep_delay = 1;
1507
1508 if (!allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE])
1509 sleep_limit = SLEEP_LIMIT_NO_TCXO_SHUTDOWN;
1510
1511#if defined(CONFIG_MSM_MEMORY_LOW_POWER_MODE_IDLE_ACTIVE)
1512 sleep_limit |= SLEEP_RESOURCE_MEMORY_BIT1;
1513#elif defined(CONFIG_MSM_MEMORY_LOW_POWER_MODE_IDLE_RETENTION)
1514 sleep_limit |= SLEEP_RESOURCE_MEMORY_BIT0;
1515#endif
1516
1517 ret = msm_pm_power_collapse(true, sleep_delay, sleep_limit);
1518 low_power = (ret != -EBUSY && ret != -ETIMEDOUT);
1519
1520#ifdef CONFIG_MSM_IDLE_STATS
1521 if (ret)
1522 exit_stat = MSM_PM_STAT_IDLE_FAILED_POWER_COLLAPSE;
1523 else {
1524 exit_stat = MSM_PM_STAT_IDLE_POWER_COLLAPSE;
1525 msm_pm_sleep_limit = sleep_limit;
1526 }
Murali Nalajala41786ab2012-03-06 10:47:32 +05301527#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001528 } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
Murali Nalajala41786ab2012-03-06 10:47:32 +05301529 ret = msm_pm_power_collapse_standalone(true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001530 low_power = 0;
1531#ifdef CONFIG_MSM_IDLE_STATS
1532 exit_stat = ret ?
1533 MSM_PM_STAT_IDLE_FAILED_STANDALONE_POWER_COLLAPSE :
1534 MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE;
Murali Nalajala41786ab2012-03-06 10:47:32 +05301535#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001536 } else if (allow[MSM_PM_SLEEP_MODE_RAMP_DOWN_AND_WAIT_FOR_INTERRUPT]) {
1537 ret = msm_pm_swfi(true);
1538 if (ret)
Murali Nalajala2a0bbda2012-03-28 12:12:54 +05301539 while (!msm_pm_irq_extns->irq_pending())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001540 udelay(1);
1541 low_power = 0;
1542#ifdef CONFIG_MSM_IDLE_STATS
1543 exit_stat = ret ? MSM_PM_STAT_IDLE_SPIN : MSM_PM_STAT_IDLE_WFI;
Murali Nalajala41786ab2012-03-06 10:47:32 +05301544#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001545 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
1546 msm_pm_swfi(false);
1547 low_power = 0;
1548#ifdef CONFIG_MSM_IDLE_STATS
1549 exit_stat = MSM_PM_STAT_IDLE_WFI;
Murali Nalajala41786ab2012-03-06 10:47:32 +05301550#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001551 } else {
Murali Nalajala2a0bbda2012-03-28 12:12:54 +05301552 while (!msm_pm_irq_extns->irq_pending())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001553 udelay(1);
1554 low_power = 0;
1555#ifdef CONFIG_MSM_IDLE_STATS
1556 exit_stat = MSM_PM_STAT_IDLE_SPIN;
Murali Nalajala41786ab2012-03-06 10:47:32 +05301557#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001558 }
1559
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001560 msm_timer_exit_idle(low_power);
1561
1562#ifdef CONFIG_MSM_IDLE_STATS
1563 t2 = ktime_to_ns(ktime_get());
1564 msm_pm_add_stat(exit_stat, t2 - t1);
Murali Nalajala41786ab2012-03-06 10:47:32 +05301565#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001566}
1567
1568/*
1569 * Suspend the Apps processor.
1570 *
1571 * Return value:
Murali Nalajala41786ab2012-03-06 10:47:32 +05301572 * -EPERM: Suspend happened by a not permitted core
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001573 * -EAGAIN: modem reset occurred or early exit from suspend
1574 * -EBUSY: modem not ready for our suspend
1575 * -EINVAL: invalid sleep mode
1576 * -EIO: could not ramp Apps processor clock
1577 * -ETIMEDOUT: timed out waiting for modem's handshake
1578 * 0: success
1579 */
1580static int msm_pm_enter(suspend_state_t state)
1581{
1582 bool allow[MSM_PM_SLEEP_MODE_NR];
1583 uint32_t sleep_limit = SLEEP_LIMIT_NONE;
Murali Nalajala41786ab2012-03-06 10:47:32 +05301584 int ret = -EPERM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001585 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001586#ifdef CONFIG_MSM_IDLE_STATS
1587 int64_t period = 0;
1588 int64_t time = 0;
Murali Nalajala41786ab2012-03-06 10:47:32 +05301589#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001590
Murali Nalajala41786ab2012-03-06 10:47:32 +05301591 /* Must executed by CORE0 */
1592 if (smp_processor_id()) {
1593 __WARN();
1594 goto suspend_exit;
1595 }
1596
1597#ifdef CONFIG_MSM_IDLE_STATS
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001598 time = msm_timer_get_sclk_time(&period);
1599#endif
1600
1601 MSM_PM_DPRINTK(MSM_PM_DEBUG_SUSPEND, KERN_INFO,
1602 "%s(): sleep limit %u\n", __func__, sleep_limit);
1603
1604 for (i = 0; i < ARRAY_SIZE(allow); i++)
1605 allow[i] = true;
1606
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001607 for (i = 0; i < ARRAY_SIZE(allow); i++) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +05301608 struct msm_pm_platform_data *mode;
1609 mode = &msm_pm_modes[MSM_PM_MODE(0, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001610 if (!mode->suspend_supported || !mode->suspend_enabled)
1611 allow[i] = false;
1612 }
1613
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001614 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE] ||
1615 allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_NO_XO_SHUTDOWN]) {
1616#ifdef CONFIG_MSM_IDLE_STATS
1617 enum msm_pm_time_stats_id id;
1618 int64_t end_time;
1619#endif
1620
1621 clock_debug_print_enabled();
1622
1623#ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE
1624 if (msm_pm_sleep_time_override > 0) {
1625 int64_t ns;
1626 ns = NSEC_PER_SEC * (int64_t)msm_pm_sleep_time_override;
1627 msm_pm_set_max_sleep_time(ns);
1628 msm_pm_sleep_time_override = 0;
1629 }
1630#endif
1631 if (!allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE])
1632 sleep_limit = SLEEP_LIMIT_NO_TCXO_SHUTDOWN;
1633
1634#if defined(CONFIG_MSM_MEMORY_LOW_POWER_MODE_SUSPEND_ACTIVE)
1635 sleep_limit |= SLEEP_RESOURCE_MEMORY_BIT1;
1636#elif defined(CONFIG_MSM_MEMORY_LOW_POWER_MODE_SUSPEND_RETENTION)
1637 sleep_limit |= SLEEP_RESOURCE_MEMORY_BIT0;
1638#elif defined(CONFIG_MSM_MEMORY_LOW_POWER_MODE_SUSPEND_DEEP_POWER_DOWN)
1639 if (get_msm_migrate_pages_status() != MEM_OFFLINE)
1640 sleep_limit |= SLEEP_RESOURCE_MEMORY_BIT0;
1641#endif
1642
1643 for (i = 0; i < 30 && msm_pm_modem_busy(); i++)
1644 udelay(500);
1645
1646 ret = msm_pm_power_collapse(
1647 false, msm_pm_max_sleep_time, sleep_limit);
1648
1649#ifdef CONFIG_MSM_IDLE_STATS
1650 if (ret)
1651 id = MSM_PM_STAT_FAILED_SUSPEND;
1652 else {
1653 id = MSM_PM_STAT_SUSPEND;
1654 msm_pm_sleep_limit = sleep_limit;
1655 }
1656
1657 if (time != 0) {
1658 end_time = msm_timer_get_sclk_time(NULL);
1659 if (end_time != 0) {
1660 time = end_time - time;
1661 if (time < 0)
1662 time += period;
1663 } else
1664 time = 0;
1665 }
1666
1667 msm_pm_add_stat(id, time);
1668#endif
1669 } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
Murali Nalajala41786ab2012-03-06 10:47:32 +05301670 ret = msm_pm_power_collapse_standalone(false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001671 } else if (allow[MSM_PM_SLEEP_MODE_RAMP_DOWN_AND_WAIT_FOR_INTERRUPT]) {
1672 ret = msm_pm_swfi(true);
1673 if (ret)
Murali Nalajala2a0bbda2012-03-28 12:12:54 +05301674 while (!msm_pm_irq_extns->irq_pending())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001675 udelay(1);
1676 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
1677 msm_pm_swfi(false);
1678 }
1679
Murali Nalajala41786ab2012-03-06 10:47:32 +05301680suspend_exit:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001681 MSM_PM_DPRINTK(MSM_PM_DEBUG_SUSPEND, KERN_INFO,
1682 "%s(): return %d\n", __func__, ret);
1683
1684 return ret;
1685}
1686
1687static struct platform_suspend_ops msm_pm_ops = {
1688 .enter = msm_pm_enter,
1689 .valid = suspend_valid_only_mem,
1690};
1691
Murali Nalajalac89f2f32012-02-07 19:23:52 +05301692/* Hotplug the "non boot" CPU's and put
1693 * the cores into low power mode
1694 */
1695void msm_pm_cpu_enter_lowpower(unsigned int cpu)
1696{
Murali Nalajalaa7efba12012-02-23 18:13:52 +05301697 bool allow[MSM_PM_SLEEP_MODE_NR];
1698 int i;
1699
1700 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
1701 struct msm_pm_platform_data *mode;
1702
1703 mode = &msm_pm_modes[MSM_PM_MODE(cpu, i)];
1704 allow[i] = mode->suspend_supported && mode->suspend_enabled;
1705 }
1706
1707 MSM_PM_DPRINTK(MSM_PM_DEBUG_HOTPLUG, KERN_INFO,
1708 "CPU%u: %s: shutting down cpu\n", cpu, __func__);
1709
1710 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
1711 msm_pm_power_collapse_standalone(false);
1712 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
1713 msm_pm_swfi(false);
1714 } else {
1715 MSM_PM_DPRINTK(MSM_PM_DEBUG_HOTPLUG, KERN_INFO,
1716 "CPU%u: %s: shutting down failed!!!\n", cpu, __func__);
1717 }
Murali Nalajalac89f2f32012-02-07 19:23:52 +05301718}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001719
1720/******************************************************************************
1721 * Restart Definitions
1722 *****************************************************************************/
1723
1724static uint32_t restart_reason = 0x776655AA;
1725
1726static void msm_pm_power_off(void)
1727{
1728 msm_rpcrouter_close();
1729 msm_proc_comm(PCOM_POWER_DOWN, 0, 0);
1730 for (;;)
1731 ;
1732}
1733
1734static void msm_pm_restart(char str, const char *cmd)
1735{
1736 msm_rpcrouter_close();
1737 msm_proc_comm(PCOM_RESET_CHIP, &restart_reason, 0);
1738
1739 for (;;)
1740 ;
1741}
1742
1743static int msm_reboot_call
1744 (struct notifier_block *this, unsigned long code, void *_cmd)
1745{
1746 if ((code == SYS_RESTART) && _cmd) {
1747 char *cmd = _cmd;
1748 if (!strcmp(cmd, "bootloader")) {
1749 restart_reason = 0x77665500;
1750 } else if (!strcmp(cmd, "recovery")) {
1751 restart_reason = 0x77665502;
1752 } else if (!strcmp(cmd, "eraseflash")) {
1753 restart_reason = 0x776655EF;
1754 } else if (!strncmp(cmd, "oem-", 4)) {
1755 unsigned code = simple_strtoul(cmd + 4, 0, 16) & 0xff;
1756 restart_reason = 0x6f656d00 | code;
1757 } else {
1758 restart_reason = 0x77665501;
1759 }
1760 }
1761 return NOTIFY_DONE;
1762}
1763
1764static struct notifier_block msm_reboot_notifier = {
1765 .notifier_call = msm_reboot_call,
1766};
1767
1768
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001769/*
1770 * Initialize the power management subsystem.
1771 *
1772 * Return value:
1773 * -ENODEV: initialization failed
1774 * 0: success
1775 */
1776static int __init msm_pm_init(void)
1777{
1778#ifdef CONFIG_MSM_IDLE_STATS
1779 struct proc_dir_entry *d_entry;
Murali Nalajala0df9fee2012-01-12 15:26:09 +05301780 unsigned int cpu;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001781#endif
1782 int ret;
1783#ifdef CONFIG_CPU_V7
1784 pgd_t *pc_pgd;
1785 pmd_t *pmd;
1786 unsigned long pmdval;
1787
Aparna Mallavarapuced47282012-03-08 12:29:41 +05301788 if (cpu_is_msm8625())
1789 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001790 /* Page table for cores to come back up safely. */
1791 pc_pgd = pgd_alloc(&init_mm);
1792 if (!pc_pgd)
1793 return -ENOMEM;
1794 pmd = pmd_offset(pc_pgd +
1795 pgd_index(virt_to_phys(msm_pm_collapse_exit)),
1796 virt_to_phys(msm_pm_collapse_exit));
1797 pmdval = (virt_to_phys(msm_pm_collapse_exit) & PGDIR_MASK) |
1798 PMD_TYPE_SECT | PMD_SECT_AP_WRITE;
1799 pmd[0] = __pmd(pmdval);
1800 pmd[1] = __pmd(pmdval + (1 << (PGDIR_SHIFT - 1)));
1801
1802 /* It is remotely possible that the code in msm_pm_collapse_exit()
1803 * which turns on the MMU with this mapping is in the
1804 * next even-numbered megabyte beyond the
1805 * start of msm_pm_collapse_exit().
1806 * Map this megabyte in as well.
1807 */
1808 pmd[2] = __pmd(pmdval + (2 << (PGDIR_SHIFT - 1)));
1809 flush_pmd_entry(pmd);
1810 msm_pm_pc_pgd = virt_to_phys(pc_pgd);
1811#endif
1812
1813 pm_power_off = msm_pm_power_off;
1814 arm_pm_restart = msm_pm_restart;
1815 register_reboot_notifier(&msm_reboot_notifier);
1816
1817 msm_pm_smem_data = smem_alloc(SMEM_APPS_DEM_SLAVE_DATA,
1818 sizeof(*msm_pm_smem_data));
1819 if (msm_pm_smem_data == NULL) {
1820 printk(KERN_ERR "%s: failed to get smsm_data\n", __func__);
1821 return -ENODEV;
1822 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001823
1824 ret = msm_timer_init_time_sync(msm_pm_timeout);
1825 if (ret)
1826 return ret;
1827
1828 ret = smsm_change_intr_mask(SMSM_POWER_MASTER_DEM, 0xFFFFFFFF, 0);
1829 if (ret) {
1830 printk(KERN_ERR "%s: failed to clear interrupt mask, %d\n",
1831 __func__, ret);
1832 return ret;
1833 }
1834
1835#ifdef CONFIG_MSM_MEMORY_LOW_POWER_MODE
1836 /* The wakeup_reason field is overloaded during initialization time
1837 to signal Modem that Apps will control the low power modes of
1838 the memory.
1839 */
1840 msm_pm_smem_data->wakeup_reason = 1;
1841 smsm_change_state(SMSM_APPS_DEM, 0, DEM_SLAVE_SMSM_RUN);
1842#endif
1843
1844 BUG_ON(msm_pm_modes == NULL);
1845
1846 atomic_set(&msm_pm_init_done, 1);
1847 suspend_set_ops(&msm_pm_ops);
1848
1849 msm_pm_mode_sysfs_add();
1850#ifdef CONFIG_MSM_IDLE_STATS
Murali Nalajala0df9fee2012-01-12 15:26:09 +05301851 for_each_possible_cpu(cpu) {
1852 struct msm_pm_time_stats *stats =
1853 per_cpu(msm_pm_stats, cpu).stats;
1854
1855 stats[MSM_PM_STAT_REQUESTED_IDLE].name = "idle-request";
1856 stats[MSM_PM_STAT_REQUESTED_IDLE].first_bucket_time =
1857 CONFIG_MSM_IDLE_STATS_FIRST_BUCKET;
1858
1859 stats[MSM_PM_STAT_IDLE_SPIN].name = "idle-spin";
1860 stats[MSM_PM_STAT_IDLE_SPIN].first_bucket_time =
1861 CONFIG_MSM_IDLE_STATS_FIRST_BUCKET;
1862
1863 stats[MSM_PM_STAT_IDLE_WFI].name = "idle-wfi";
1864 stats[MSM_PM_STAT_IDLE_WFI].first_bucket_time =
1865 CONFIG_MSM_IDLE_STATS_FIRST_BUCKET;
1866
1867 stats[MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE].name =
1868 "idle-standalone-power-collapse";
1869 stats[MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE].
1870 first_bucket_time =
1871 CONFIG_MSM_IDLE_STATS_FIRST_BUCKET;
1872
1873 stats[MSM_PM_STAT_IDLE_FAILED_STANDALONE_POWER_COLLAPSE].name =
1874 "idle-failed-standalone-power-collapse";
1875 stats[MSM_PM_STAT_IDLE_FAILED_STANDALONE_POWER_COLLAPSE].
1876 first_bucket_time =
1877 CONFIG_MSM_IDLE_STATS_FIRST_BUCKET;
1878
Murali Nalajala0df9fee2012-01-12 15:26:09 +05301879 stats[MSM_PM_STAT_IDLE_POWER_COLLAPSE].name =
1880 "idle-power-collapse";
1881 stats[MSM_PM_STAT_IDLE_POWER_COLLAPSE].first_bucket_time =
1882 CONFIG_MSM_IDLE_STATS_FIRST_BUCKET;
1883
1884 stats[MSM_PM_STAT_IDLE_FAILED_POWER_COLLAPSE].name =
1885 "idle-failed-power-collapse";
1886 stats[MSM_PM_STAT_IDLE_FAILED_POWER_COLLAPSE].
1887 first_bucket_time =
1888 CONFIG_MSM_IDLE_STATS_FIRST_BUCKET;
1889
1890 stats[MSM_PM_STAT_SUSPEND].name = "suspend";
1891 stats[MSM_PM_STAT_SUSPEND].first_bucket_time =
1892 CONFIG_MSM_SUSPEND_STATS_FIRST_BUCKET;
1893
1894 stats[MSM_PM_STAT_FAILED_SUSPEND].name = "failed-suspend";
1895 stats[MSM_PM_STAT_FAILED_SUSPEND].first_bucket_time =
1896 CONFIG_MSM_IDLE_STATS_FIRST_BUCKET;
1897
1898 stats[MSM_PM_STAT_NOT_IDLE].name = "not-idle";
1899 stats[MSM_PM_STAT_NOT_IDLE].first_bucket_time =
1900 CONFIG_MSM_IDLE_STATS_FIRST_BUCKET;
1901 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001902 d_entry = create_proc_entry("msm_pm_stats",
1903 S_IRUGO | S_IWUSR | S_IWGRP, NULL);
1904 if (d_entry) {
1905 d_entry->read_proc = msm_pm_read_proc;
1906 d_entry->write_proc = msm_pm_write_proc;
1907 d_entry->data = NULL;
1908 }
1909#endif
1910
1911 return 0;
1912}
1913
1914late_initcall_sync(msm_pm_init);