blob: 7977d22e7bcf53215e1e4369e6eb8e0478a68a3a [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
51
52#include "smd_private.h"
53#include "smd_rpcrouter.h"
54#include "acpuclock.h"
55#include "clock.h"
56#include "proc_comm.h"
57#include "idle.h"
58#include "irq.h"
59#include "gpio.h"
60#include "timer.h"
Matt Wagantall7cca4642012-02-01 16:43:24 -080061#include "pm.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070062#include "spm.h"
63#include "sirc.h"
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -060064#include "pm-boot.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070065
66/******************************************************************************
67 * Debug Definitions
68 *****************************************************************************/
69
70enum {
71 MSM_PM_DEBUG_SUSPEND = 1U << 0,
72 MSM_PM_DEBUG_POWER_COLLAPSE = 1U << 1,
73 MSM_PM_DEBUG_STATE = 1U << 2,
74 MSM_PM_DEBUG_CLOCK = 1U << 3,
75 MSM_PM_DEBUG_RESET_VECTOR = 1U << 4,
76 MSM_PM_DEBUG_SMSM_STATE = 1U << 5,
77 MSM_PM_DEBUG_IDLE = 1U << 6,
78};
79
80static int msm_pm_debug_mask;
81module_param_named(
82 debug_mask, msm_pm_debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP
83);
84
85#define MSM_PM_DPRINTK(mask, level, message, ...) \
86 do { \
87 if ((mask) & msm_pm_debug_mask) \
88 printk(level message, ## __VA_ARGS__); \
89 } while (0)
90
91#define MSM_PM_DEBUG_PRINT_STATE(tag) \
92 do { \
93 MSM_PM_DPRINTK(MSM_PM_DEBUG_STATE, \
94 KERN_INFO, "%s: " \
95 "APPS_CLK_SLEEP_EN %x, APPS_PWRDOWN %x, " \
96 "SMSM_POWER_MASTER_DEM %x, SMSM_MODEM_STATE %x, " \
97 "SMSM_APPS_DEM %x\n", \
98 tag, \
99 __raw_readl(APPS_CLK_SLEEP_EN), \
100 __raw_readl(APPS_PWRDOWN), \
101 smsm_get_state(SMSM_POWER_MASTER_DEM), \
102 smsm_get_state(SMSM_MODEM_STATE), \
103 smsm_get_state(SMSM_APPS_DEM)); \
104 } while (0)
105
106#define MSM_PM_DEBUG_PRINT_SLEEP_INFO() \
107 do { \
108 if (msm_pm_debug_mask & MSM_PM_DEBUG_SMSM_STATE) \
109 smsm_print_sleep_info(msm_pm_smem_data->sleep_time, \
110 msm_pm_smem_data->resources_used, \
111 msm_pm_smem_data->irq_mask, \
112 msm_pm_smem_data->wakeup_reason, \
113 msm_pm_smem_data->pending_irqs); \
114 } while (0)
115
116
117/******************************************************************************
118 * Sleep Modes and Parameters
119 *****************************************************************************/
120
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700121static int msm_pm_idle_sleep_min_time = CONFIG_MSM7X00A_IDLE_SLEEP_MIN_TIME;
122module_param_named(
123 idle_sleep_min_time, msm_pm_idle_sleep_min_time,
124 int, S_IRUGO | S_IWUSR | S_IWGRP
125);
126
127enum {
128 MSM_PM_MODE_ATTR_SUSPEND,
129 MSM_PM_MODE_ATTR_IDLE,
130 MSM_PM_MODE_ATTR_LATENCY,
131 MSM_PM_MODE_ATTR_RESIDENCY,
132 MSM_PM_MODE_ATTR_NR,
133};
134
135static char *msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_NR] = {
136 [MSM_PM_MODE_ATTR_SUSPEND] = "suspend_enabled",
137 [MSM_PM_MODE_ATTR_IDLE] = "idle_enabled",
138 [MSM_PM_MODE_ATTR_LATENCY] = "latency",
139 [MSM_PM_MODE_ATTR_RESIDENCY] = "residency",
140};
141
142static char *msm_pm_sleep_mode_labels[MSM_PM_SLEEP_MODE_NR] = {
143 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE_SUSPEND] = " ",
144 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE] = "power_collapse",
145 [MSM_PM_SLEEP_MODE_APPS_SLEEP] = "apps_sleep",
146 [MSM_PM_SLEEP_MODE_RAMP_DOWN_AND_WAIT_FOR_INTERRUPT] =
147 "ramp_down_and_wfi",
148 [MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT] = "wfi",
149 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE_NO_XO_SHUTDOWN] =
150 "power_collapse_no_xo_shutdown",
151 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE] =
152 "standalone_power_collapse",
153};
154
155static struct msm_pm_platform_data *msm_pm_modes;
156
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530157struct msm_pm_kobj_attribute {
158 unsigned int cpu;
159 struct kobj_attribute ka;
160};
161
162#define GET_CPU_OF_ATTR(attr) \
163 (container_of(attr, struct msm_pm_kobj_attribute, ka)->cpu)
164
165struct msm_pm_sysfs_sleep_mode {
166 struct kobject *kobj;
167 struct attribute_group attr_group;
168 struct attribute *attrs[MSM_PM_MODE_ATTR_NR + 1];
169 struct msm_pm_kobj_attribute kas[MSM_PM_MODE_ATTR_NR];
170};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700171
172/*
173 * Write out the attribute.
174 */
175static ssize_t msm_pm_mode_attr_show(
176 struct kobject *kobj, struct kobj_attribute *attr, char *buf)
177{
178 int ret = -EINVAL;
179 int i;
180
181 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
182 struct kernel_param kp;
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530183 unsigned int cpu;
184 struct msm_pm_platform_data *mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700185
186 if (msm_pm_sleep_mode_labels[i] == NULL)
187 continue;
188
189 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
190 continue;
191
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530192 cpu = GET_CPU_OF_ATTR(attr);
193 mode = &msm_pm_modes[MSM_PM_MODE(cpu, i)];
194
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700195 if (!strcmp(attr->attr.name,
196 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530197 u32 arg = mode->suspend_enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700198 kp.arg = &arg;
199 ret = param_get_ulong(buf, &kp);
200 } else if (!strcmp(attr->attr.name,
201 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530202 u32 arg = mode->idle_enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700203 kp.arg = &arg;
204 ret = param_get_ulong(buf, &kp);
205 } else if (!strcmp(attr->attr.name,
206 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_LATENCY])) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530207 u32 arg = mode->latency;
208 kp.arg = &arg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700209 ret = param_get_ulong(buf, &kp);
210 } else if (!strcmp(attr->attr.name,
211 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_RESIDENCY])) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530212 u32 arg = mode->residency;
213 kp.arg = &arg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700214 ret = param_get_ulong(buf, &kp);
215 }
216
217 break;
218 }
219
220 if (ret > 0) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530221 strlcat(buf, "\n", PAGE_SIZE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700222 ret++;
223 }
224
225 return ret;
226}
227
228/*
229 * Read in the new attribute value.
230 */
231static ssize_t msm_pm_mode_attr_store(struct kobject *kobj,
232 struct kobj_attribute *attr, const char *buf, size_t count)
233{
234 int ret = -EINVAL;
235 int i;
236
237 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
238 struct kernel_param kp;
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530239 unsigned int cpu;
240 struct msm_pm_platform_data *mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700241
242 if (msm_pm_sleep_mode_labels[i] == NULL)
243 continue;
244
245 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
246 continue;
247
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530248 cpu = GET_CPU_OF_ATTR(attr);
249 mode = &msm_pm_modes[MSM_PM_MODE(cpu, i)];
250
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700251 if (!strcmp(attr->attr.name,
252 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530253 kp.arg = &mode->suspend_enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700254 ret = param_set_byte(buf, &kp);
255 } else if (!strcmp(attr->attr.name,
256 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530257 kp.arg = &mode->idle_enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700258 ret = param_set_byte(buf, &kp);
259 } else if (!strcmp(attr->attr.name,
260 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_LATENCY])) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530261 kp.arg = &mode->latency;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700262 ret = param_set_ulong(buf, &kp);
263 } else if (!strcmp(attr->attr.name,
264 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_RESIDENCY])) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530265 kp.arg = &mode->residency;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700266 ret = param_set_ulong(buf, &kp);
267 }
268
269 break;
270 }
271
272 return ret ? ret : count;
273}
274
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530275 /* Add sysfs entries for one cpu. */
276static int __init msm_pm_mode_sysfs_add_cpu(
277 unsigned int cpu, struct kobject *modes_kobj)
278{
279 char cpu_name[8];
280 struct kobject *cpu_kobj;
281 struct msm_pm_sysfs_sleep_mode *mode = NULL;
282 int i, j, k;
283 int ret;
284
285 snprintf(cpu_name, sizeof(cpu_name), "cpu%u", cpu);
286 cpu_kobj = kobject_create_and_add(cpu_name, modes_kobj);
287 if (!cpu_kobj) {
288 pr_err("%s: cannot create %s kobject\n", __func__, cpu_name);
289 ret = -ENOMEM;
290 goto mode_sysfs_add_cpu_exit;
291 }
292
293 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
294 int idx = MSM_PM_MODE(cpu, i);
295
296 if ((!msm_pm_modes[idx].suspend_supported) &&
297 (!msm_pm_modes[idx].idle_supported))
298 continue;
299
300 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
301 if (!mode) {
302 pr_err("%s: cannot allocate memory for attributes\n",
303 __func__);
304 ret = -ENOMEM;
305 goto mode_sysfs_add_cpu_exit;
306 }
307
308 mode->kobj = kobject_create_and_add(
309 msm_pm_sleep_mode_labels[i], cpu_kobj);
310 if (!mode->kobj) {
311 pr_err("%s: cannot create kobject\n", __func__);
312 ret = -ENOMEM;
313 goto mode_sysfs_add_cpu_exit;
314 }
315
316 for (k = 0, j = 0; k < MSM_PM_MODE_ATTR_NR; k++) {
317 if ((k == MSM_PM_MODE_ATTR_IDLE) &&
318 !msm_pm_modes[idx].idle_supported)
319 continue;
320 if ((k == MSM_PM_MODE_ATTR_SUSPEND) &&
321 !msm_pm_modes[idx].suspend_supported)
322 continue;
323 mode->kas[j].cpu = cpu;
324 mode->kas[j].ka.attr.mode = 0644;
325 mode->kas[j].ka.show = msm_pm_mode_attr_show;
326 mode->kas[j].ka.store = msm_pm_mode_attr_store;
327 mode->kas[j].ka.attr.name = msm_pm_mode_attr_labels[k];
328 mode->attrs[j] = &mode->kas[j].ka.attr;
329 j++;
330 }
331 mode->attrs[j] = NULL;
332
333 mode->attr_group.attrs = mode->attrs;
334 ret = sysfs_create_group(mode->kobj, &mode->attr_group);
335 if (ret) {
336 printk(KERN_ERR
337 "%s: cannot create kobject attribute group\n",
338 __func__);
339 goto mode_sysfs_add_cpu_exit;
340 }
341 }
342
343 ret = 0;
344
345mode_sysfs_add_cpu_exit:
346 if (ret) {
347 if (mode && mode->kobj)
348 kobject_del(mode->kobj);
349 kfree(mode);
350 }
351
352 return ret;
353}
354
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700355/*
356 * Add sysfs entries for the sleep modes.
357 */
358static int __init msm_pm_mode_sysfs_add(void)
359{
360 struct kobject *module_kobj = NULL;
361 struct kobject *modes_kobj = NULL;
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530362 unsigned int cpu;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700363 int ret;
364
365 module_kobj = kset_find_obj(module_kset, KBUILD_MODNAME);
366 if (!module_kobj) {
367 printk(KERN_ERR "%s: cannot find kobject for module %s\n",
368 __func__, KBUILD_MODNAME);
369 ret = -ENOENT;
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530370 goto mode_sysfs_add_exit;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700371 }
372
373 modes_kobj = kobject_create_and_add("modes", module_kobj);
374 if (!modes_kobj) {
375 printk(KERN_ERR "%s: cannot create modes kobject\n", __func__);
376 ret = -ENOMEM;
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530377 goto mode_sysfs_add_exit;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700378 }
379
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530380 for_each_possible_cpu(cpu) {
381 ret = msm_pm_mode_sysfs_add_cpu(cpu, modes_kobj);
382 if (ret)
383 goto mode_sysfs_add_exit;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700384 }
385
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530386 ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700387
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530388mode_sysfs_add_exit:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700389 return ret;
390}
391
392void __init msm_pm_set_platform_data(
393 struct msm_pm_platform_data *data, int count)
394{
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530395 BUG_ON(MSM_PM_SLEEP_MODE_NR * num_possible_cpus() > count);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700396 msm_pm_modes = data;
397}
398
399
400/******************************************************************************
401 * Sleep Limitations
402 *****************************************************************************/
403enum {
404 SLEEP_LIMIT_NONE = 0,
405 SLEEP_LIMIT_NO_TCXO_SHUTDOWN = 2,
406 SLEEP_LIMIT_MASK = 0x03,
407};
408
409#ifdef CONFIG_MSM_MEMORY_LOW_POWER_MODE
410enum {
411 SLEEP_RESOURCE_MEMORY_BIT0 = 0x0200,
412 SLEEP_RESOURCE_MEMORY_BIT1 = 0x0010,
413};
414#endif
415
416
417/******************************************************************************
418 * Configure Hardware for Power Down/Up
419 *****************************************************************************/
420
421#if defined(CONFIG_ARCH_MSM7X30)
422#define APPS_CLK_SLEEP_EN (MSM_GCC_BASE + 0x020)
423#define APPS_PWRDOWN (MSM_ACC_BASE + 0x01c)
424#define APPS_SECOP (MSM_TCSR_BASE + 0x038)
425#else /* defined(CONFIG_ARCH_MSM7X30) */
426#define APPS_CLK_SLEEP_EN (MSM_CSR_BASE + 0x11c)
427#define APPS_PWRDOWN (MSM_CSR_BASE + 0x440)
428#define APPS_STANDBY_CTL (MSM_CSR_BASE + 0x108)
429#endif /* defined(CONFIG_ARCH_MSM7X30) */
430
431/*
432 * Configure hardware registers in preparation for Apps power down.
433 */
434static void msm_pm_config_hw_before_power_down(void)
435{
436#if defined(CONFIG_ARCH_MSM7X30)
437 __raw_writel(1, APPS_PWRDOWN);
438 mb();
439 __raw_writel(4, APPS_SECOP);
440 mb();
441#elif defined(CONFIG_ARCH_MSM7X27)
442 __raw_writel(0x1f, APPS_CLK_SLEEP_EN);
443 mb();
444 __raw_writel(1, APPS_PWRDOWN);
445 mb();
446#elif defined(CONFIG_ARCH_MSM7x27A)
447 __raw_writel(0x7, APPS_CLK_SLEEP_EN);
448 mb();
449 __raw_writel(1, APPS_PWRDOWN);
450 mb();
451#else
452 __raw_writel(0x1f, APPS_CLK_SLEEP_EN);
453 mb();
454 __raw_writel(1, APPS_PWRDOWN);
455 mb();
456 __raw_writel(0, APPS_STANDBY_CTL);
457 mb();
458#endif
459}
460
461/*
462 * Clear hardware registers after Apps powers up.
463 */
464static void msm_pm_config_hw_after_power_up(void)
465{
466#if defined(CONFIG_ARCH_MSM7X30)
467 __raw_writel(0, APPS_SECOP);
468 mb();
469 __raw_writel(0, APPS_PWRDOWN);
470 mb();
471 msm_spm_reinit();
472#elif defined(CONFIG_ARCH_MSM7x27A)
473 __raw_writel(0, APPS_PWRDOWN);
474 mb();
475 __raw_writel(0, APPS_CLK_SLEEP_EN);
476 mb();
477#else
478 __raw_writel(0, APPS_PWRDOWN);
479 mb();
480 __raw_writel(0, APPS_CLK_SLEEP_EN);
481 mb();
482#endif
483}
484
485/*
486 * Configure hardware registers in preparation for SWFI.
487 */
488static void msm_pm_config_hw_before_swfi(void)
489{
490#if defined(CONFIG_ARCH_QSD8X50)
491 __raw_writel(0x1f, APPS_CLK_SLEEP_EN);
492 mb();
493#elif defined(CONFIG_ARCH_MSM7X27)
494 __raw_writel(0x0f, APPS_CLK_SLEEP_EN);
495 mb();
496#elif defined(CONFIG_ARCH_MSM7X27A)
497 __raw_writel(0x7, APPS_CLK_SLEEP_EN);
498 mb();
499#endif
500}
501
502/*
503 * Respond to timing out waiting for Modem
504 *
505 * NOTE: The function never returns.
506 */
507static void msm_pm_timeout(void)
508{
509#if defined(CONFIG_MSM_PM_TIMEOUT_RESET_CHIP)
510 printk(KERN_EMERG "%s(): resetting chip\n", __func__);
511 msm_proc_comm(PCOM_RESET_CHIP_IMM, NULL, NULL);
512#elif defined(CONFIG_MSM_PM_TIMEOUT_RESET_MODEM)
513 printk(KERN_EMERG "%s(): resetting modem\n", __func__);
514 msm_proc_comm_reset_modem_now();
515#elif defined(CONFIG_MSM_PM_TIMEOUT_HALT)
516 printk(KERN_EMERG "%s(): halting\n", __func__);
517#endif
518 for (;;)
519 ;
520}
521
522
523/******************************************************************************
524 * State Polling Definitions
525 *****************************************************************************/
526
527struct msm_pm_polled_group {
528 uint32_t group_id;
529
530 uint32_t bits_all_set;
531 uint32_t bits_all_clear;
532 uint32_t bits_any_set;
533 uint32_t bits_any_clear;
534
535 uint32_t value_read;
536};
537
538/*
539 * Return true if all bits indicated by flag are set in source.
540 */
541static inline bool msm_pm_all_set(uint32_t source, uint32_t flag)
542{
543 return (source & flag) == flag;
544}
545
546/*
547 * Return true if any bit indicated by flag are set in source.
548 */
549static inline bool msm_pm_any_set(uint32_t source, uint32_t flag)
550{
551 return !flag || (source & flag);
552}
553
554/*
555 * Return true if all bits indicated by flag are cleared in source.
556 */
557static inline bool msm_pm_all_clear(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 cleared in source.
564 */
565static inline bool msm_pm_any_clear(uint32_t source, uint32_t flag)
566{
567 return !flag || (~source & flag);
568}
569
570/*
571 * Poll the shared memory states as indicated by the poll groups.
572 *
573 * nr_grps: number of groups in the array
574 * grps: array of groups
575 *
576 * The function returns when conditions specified by any of the poll
577 * groups become true. The conditions specified by a poll group are
578 * deemed true when 1) at least one bit from bits_any_set is set OR one
579 * bit from bits_any_clear is cleared; and 2) all bits in bits_all_set
580 * are set; and 3) all bits in bits_all_clear are cleared.
581 *
582 * Return value:
583 * >=0: index of the poll group whose conditions have become true
584 * -ETIMEDOUT: timed out
585 */
586static int msm_pm_poll_state(int nr_grps, struct msm_pm_polled_group *grps)
587{
588 int i, k;
589
590 for (i = 0; i < 50000; i++) {
591 for (k = 0; k < nr_grps; k++) {
592 bool all_set, all_clear;
593 bool any_set, any_clear;
594
595 grps[k].value_read = smsm_get_state(grps[k].group_id);
596
597 all_set = msm_pm_all_set(grps[k].value_read,
598 grps[k].bits_all_set);
599 all_clear = msm_pm_all_clear(grps[k].value_read,
600 grps[k].bits_all_clear);
601 any_set = msm_pm_any_set(grps[k].value_read,
602 grps[k].bits_any_set);
603 any_clear = msm_pm_any_clear(grps[k].value_read,
604 grps[k].bits_any_clear);
605
606 if (all_set && all_clear && (any_set || any_clear))
607 return k;
608 }
609 udelay(50);
610 }
611
612 printk(KERN_ERR "%s failed:\n", __func__);
613 for (k = 0; k < nr_grps; k++)
614 printk(KERN_ERR "(%x, %x, %x, %x) %x\n",
615 grps[k].bits_all_set, grps[k].bits_all_clear,
616 grps[k].bits_any_set, grps[k].bits_any_clear,
617 grps[k].value_read);
618
619 return -ETIMEDOUT;
620}
621
622
623/******************************************************************************
624 * Suspend Max Sleep Time
625 *****************************************************************************/
626
627#define SCLK_HZ (32768)
628#define MSM_PM_SLEEP_TICK_LIMIT (0x6DDD000)
629
630#ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE
631static int msm_pm_sleep_time_override;
632module_param_named(sleep_time_override,
633 msm_pm_sleep_time_override, int, S_IRUGO | S_IWUSR | S_IWGRP);
634#endif
635
636static uint32_t msm_pm_max_sleep_time;
637
638/*
639 * Convert time from nanoseconds to slow clock ticks, then cap it to the
640 * specified limit
641 */
642static int64_t msm_pm_convert_and_cap_time(int64_t time_ns, int64_t limit)
643{
644 do_div(time_ns, NSEC_PER_SEC / SCLK_HZ);
645 return (time_ns > limit) ? limit : time_ns;
646}
647
648/*
649 * Set the sleep time for suspend. 0 means infinite sleep time.
650 */
651void msm_pm_set_max_sleep_time(int64_t max_sleep_time_ns)
652{
653 unsigned long flags;
654
655 local_irq_save(flags);
656 if (max_sleep_time_ns == 0) {
657 msm_pm_max_sleep_time = 0;
658 } else {
659 msm_pm_max_sleep_time = (uint32_t)msm_pm_convert_and_cap_time(
660 max_sleep_time_ns, MSM_PM_SLEEP_TICK_LIMIT);
661
662 if (msm_pm_max_sleep_time == 0)
663 msm_pm_max_sleep_time = 1;
664 }
665
666 MSM_PM_DPRINTK(MSM_PM_DEBUG_SUSPEND, KERN_INFO,
667 "%s(): Requested %lld ns Giving %u sclk ticks\n", __func__,
668 max_sleep_time_ns, msm_pm_max_sleep_time);
669 local_irq_restore(flags);
670}
671EXPORT_SYMBOL(msm_pm_set_max_sleep_time);
672
673
674/******************************************************************************
675 * CONFIG_MSM_IDLE_STATS
676 *****************************************************************************/
677
678#ifdef CONFIG_MSM_IDLE_STATS
679enum msm_pm_time_stats_id {
680 MSM_PM_STAT_REQUESTED_IDLE,
681 MSM_PM_STAT_IDLE_SPIN,
682 MSM_PM_STAT_IDLE_WFI,
683 MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE,
684 MSM_PM_STAT_IDLE_FAILED_STANDALONE_POWER_COLLAPSE,
685 MSM_PM_STAT_IDLE_SLEEP,
686 MSM_PM_STAT_IDLE_FAILED_SLEEP,
687 MSM_PM_STAT_IDLE_POWER_COLLAPSE,
688 MSM_PM_STAT_IDLE_FAILED_POWER_COLLAPSE,
689 MSM_PM_STAT_SUSPEND,
690 MSM_PM_STAT_FAILED_SUSPEND,
691 MSM_PM_STAT_NOT_IDLE,
692 MSM_PM_STAT_COUNT
693};
694
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530695struct msm_pm_time_stats {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700696 const char *name;
697 int64_t first_bucket_time;
698 int bucket[CONFIG_MSM_IDLE_STATS_BUCKET_COUNT];
699 int64_t min_time[CONFIG_MSM_IDLE_STATS_BUCKET_COUNT];
700 int64_t max_time[CONFIG_MSM_IDLE_STATS_BUCKET_COUNT];
701 int count;
702 int64_t total_time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700703};
704
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530705struct msm_pm_cpu_time_stats {
706 struct msm_pm_time_stats stats[MSM_PM_STAT_COUNT];
707};
708
709static DEFINE_PER_CPU_SHARED_ALIGNED(
710 struct msm_pm_cpu_time_stats, msm_pm_stats);
711
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700712static uint32_t msm_pm_sleep_limit = SLEEP_LIMIT_NONE;
713
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530714static DEFINE_SPINLOCK(msm_pm_stats_lock);
715
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700716/*
717 * Add the given time data to the statistics collection.
718 */
719static void msm_pm_add_stat(enum msm_pm_time_stats_id id, int64_t t)
720{
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530721 unsigned long flags;
722 struct msm_pm_time_stats *stats;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700723 int i;
724 int64_t bt;
725
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530726 spin_lock_irqsave(&msm_pm_stats_lock, flags);
727 stats = __get_cpu_var(msm_pm_stats).stats;
728
729 stats[id].total_time += t;
730 stats[id].count++;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700731
732 bt = t;
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530733 do_div(bt, stats[id].first_bucket_time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700734
735 if (bt < 1ULL << (CONFIG_MSM_IDLE_STATS_BUCKET_SHIFT *
736 (CONFIG_MSM_IDLE_STATS_BUCKET_COUNT - 1)))
737 i = DIV_ROUND_UP(fls((uint32_t)bt),
738 CONFIG_MSM_IDLE_STATS_BUCKET_SHIFT);
739 else
740 i = CONFIG_MSM_IDLE_STATS_BUCKET_COUNT - 1;
741
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530742 if (i >= CONFIG_MSM_IDLE_STATS_BUCKET_COUNT)
743 i = CONFIG_MSM_IDLE_STATS_BUCKET_COUNT - 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700744
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530745 stats[id].bucket[i]++;
746
747 if (t < stats[id].min_time[i] || !stats[id].max_time[i])
748 stats[id].min_time[i] = t;
749 if (t > stats[id].max_time[i])
750 stats[id].max_time[i] = t;
751
752 spin_unlock_irqrestore(&msm_pm_stats_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700753}
754
755/*
756 * Helper function of snprintf where buf is auto-incremented, size is auto-
757 * decremented, and there is no return value.
758 *
759 * NOTE: buf and size must be l-values (e.g. variables)
760 */
761#define SNPRINTF(buf, size, format, ...) \
762 do { \
763 if (size > 0) { \
764 int ret; \
765 ret = snprintf(buf, size, format, ## __VA_ARGS__); \
766 if (ret > size) { \
767 buf += size; \
768 size = 0; \
769 } else { \
770 buf += ret; \
771 size -= ret; \
772 } \
773 } \
774 } while (0)
775
776/*
777 * Write out the power management statistics.
778 */
779static int msm_pm_read_proc
780 (char *page, char **start, off_t off, int count, int *eof, void *data)
781{
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530782 unsigned int cpu = off / MSM_PM_STAT_COUNT;
783 int id = off % MSM_PM_STAT_COUNT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700784 char *p = page;
785
786 if (count < 1024) {
787 *start = (char *) 0;
788 *eof = 0;
789 return 0;
790 }
791
792 if (!off) {
793 SNPRINTF(p, count, "Last power collapse voted ");
794 if ((msm_pm_sleep_limit & SLEEP_LIMIT_MASK) ==
795 SLEEP_LIMIT_NONE)
796 SNPRINTF(p, count, "for TCXO shutdown\n\n");
797 else
798 SNPRINTF(p, count, "against TCXO shutdown\n\n");
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530799 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700800
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530801 if (cpu < num_possible_cpus()) {
802 unsigned long flags;
803 struct msm_pm_time_stats *stats;
804 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700805 int64_t bucket_time;
806 int64_t s;
807 uint32_t ns;
808
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530809 spin_lock_irqsave(&msm_pm_stats_lock, flags);
810 stats = per_cpu(msm_pm_stats, cpu).stats;
811
812 s = stats[id].total_time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700813 ns = do_div(s, NSEC_PER_SEC);
814 SNPRINTF(p, count,
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530815 "[cpu %u] %s:\n"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700816 " count: %7d\n"
817 " total_time: %lld.%09u\n",
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530818 cpu, stats[id].name,
819 stats[id].count,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700820 s, ns);
821
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530822 bucket_time = stats[id].first_bucket_time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700823 for (i = 0; i < CONFIG_MSM_IDLE_STATS_BUCKET_COUNT - 1; i++) {
824 s = bucket_time;
825 ns = do_div(s, NSEC_PER_SEC);
826 SNPRINTF(p, count,
827 " <%6lld.%09u: %7d (%lld-%lld)\n",
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530828 s, ns, stats[id].bucket[i],
829 stats[id].min_time[i],
830 stats[id].max_time[i]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700831
832 bucket_time <<= CONFIG_MSM_IDLE_STATS_BUCKET_SHIFT;
833 }
834
835 SNPRINTF(p, count, " >=%6lld.%09u: %7d (%lld-%lld)\n",
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530836 s, ns, stats[id].bucket[i],
837 stats[id].min_time[i],
838 stats[id].max_time[i]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700839
840 *start = (char *) 1;
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530841 *eof = (off + 1 >= MSM_PM_STAT_COUNT * num_possible_cpus());
842
843 spin_unlock_irqrestore(&msm_pm_stats_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700844 }
845
846 return p - page;
847}
848#undef SNPRINTF
849
850#define MSM_PM_STATS_RESET "reset"
851
852/*
853 * Reset the power management statistics values.
854 */
855static int msm_pm_write_proc(struct file *file, const char __user *buffer,
856 unsigned long count, void *data)
857{
858 char buf[sizeof(MSM_PM_STATS_RESET)];
859 int ret;
860 unsigned long flags;
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530861 unsigned int cpu;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700862
863 if (count < strlen(MSM_PM_STATS_RESET)) {
864 ret = -EINVAL;
865 goto write_proc_failed;
866 }
867
868 if (copy_from_user(buf, buffer, strlen(MSM_PM_STATS_RESET))) {
869 ret = -EFAULT;
870 goto write_proc_failed;
871 }
872
873 if (memcmp(buf, MSM_PM_STATS_RESET, strlen(MSM_PM_STATS_RESET))) {
874 ret = -EINVAL;
875 goto write_proc_failed;
876 }
877
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530878 spin_lock_irqsave(&msm_pm_stats_lock, flags);
879 for_each_possible_cpu(cpu) {
880 struct msm_pm_time_stats *stats;
881 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700882
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530883 stats = per_cpu(msm_pm_stats, cpu).stats;
884 for (i = 0; i < MSM_PM_STAT_COUNT; i++) {
885 memset(stats[i].bucket,
886 0, sizeof(stats[i].bucket));
887 memset(stats[i].min_time,
888 0, sizeof(stats[i].min_time));
889 memset(stats[i].max_time,
890 0, sizeof(stats[i].max_time));
891 stats[i].count = 0;
892 stats[i].total_time = 0;
893 }
894 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700895 msm_pm_sleep_limit = SLEEP_LIMIT_NONE;
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530896 spin_unlock_irqrestore(&msm_pm_stats_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700897
898 return count;
899
900write_proc_failed:
901 return ret;
902}
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530903
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700904#undef MSM_PM_STATS_RESET
905#endif /* CONFIG_MSM_IDLE_STATS */
906
907
908/******************************************************************************
909 * Shared Memory Bits
910 *****************************************************************************/
911
912#define DEM_MASTER_BITS_PER_CPU 6
913
914/* Power Master State Bits - Per CPU */
915#define DEM_MASTER_SMSM_RUN \
916 (0x01UL << (DEM_MASTER_BITS_PER_CPU * SMSM_APPS_STATE))
917#define DEM_MASTER_SMSM_RSA \
918 (0x02UL << (DEM_MASTER_BITS_PER_CPU * SMSM_APPS_STATE))
919#define DEM_MASTER_SMSM_PWRC_EARLY_EXIT \
920 (0x04UL << (DEM_MASTER_BITS_PER_CPU * SMSM_APPS_STATE))
921#define DEM_MASTER_SMSM_SLEEP_EXIT \
922 (0x08UL << (DEM_MASTER_BITS_PER_CPU * SMSM_APPS_STATE))
923#define DEM_MASTER_SMSM_READY \
924 (0x10UL << (DEM_MASTER_BITS_PER_CPU * SMSM_APPS_STATE))
925#define DEM_MASTER_SMSM_SLEEP \
926 (0x20UL << (DEM_MASTER_BITS_PER_CPU * SMSM_APPS_STATE))
927
928/* Power Slave State Bits */
929#define DEM_SLAVE_SMSM_RUN (0x0001)
930#define DEM_SLAVE_SMSM_PWRC (0x0002)
931#define DEM_SLAVE_SMSM_PWRC_DELAY (0x0004)
932#define DEM_SLAVE_SMSM_PWRC_EARLY_EXIT (0x0008)
933#define DEM_SLAVE_SMSM_WFPI (0x0010)
934#define DEM_SLAVE_SMSM_SLEEP (0x0020)
935#define DEM_SLAVE_SMSM_SLEEP_EXIT (0x0040)
936#define DEM_SLAVE_SMSM_MSGS_REDUCED (0x0080)
937#define DEM_SLAVE_SMSM_RESET (0x0100)
938#define DEM_SLAVE_SMSM_PWRC_SUSPEND (0x0200)
939
940
941/******************************************************************************
942 * Shared Memory Data
943 *****************************************************************************/
944
945#define DEM_MAX_PORT_NAME_LEN (20)
946
947struct msm_pm_smem_t {
948 uint32_t sleep_time;
949 uint32_t irq_mask;
950 uint32_t resources_used;
951 uint32_t reserved1;
952
953 uint32_t wakeup_reason;
954 uint32_t pending_irqs;
955 uint32_t rpc_prog;
956 uint32_t rpc_proc;
957 char smd_port_name[DEM_MAX_PORT_NAME_LEN];
958 uint32_t reserved2;
959};
960
961
962/******************************************************************************
963 *
964 *****************************************************************************/
965static struct msm_pm_smem_t *msm_pm_smem_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700966static atomic_t msm_pm_init_done = ATOMIC_INIT(0);
967
968static int msm_pm_modem_busy(void)
969{
970 if (!(smsm_get_state(SMSM_POWER_MASTER_DEM) & DEM_MASTER_SMSM_READY)) {
971 MSM_PM_DPRINTK(MSM_PM_DEBUG_POWER_COLLAPSE,
972 KERN_INFO, "%s(): master not ready\n", __func__);
973 return -EBUSY;
974 }
975
976 return 0;
977}
978
979/*
980 * Power collapse the Apps processor. This function executes the handshake
981 * protocol with Modem.
982 *
983 * Return value:
984 * -EAGAIN: modem reset occurred or early exit from power collapse
985 * -EBUSY: modem not ready for our power collapse -- no power loss
986 * -ETIMEDOUT: timed out waiting for modem's handshake -- no power loss
987 * 0: success
988 */
989static int msm_pm_power_collapse
990 (bool from_idle, uint32_t sleep_delay, uint32_t sleep_limit)
991{
992 struct msm_pm_polled_group state_grps[2];
993 unsigned long saved_acpuclk_rate;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700994 int collapsed = 0;
995 int ret;
996
997 MSM_PM_DPRINTK(MSM_PM_DEBUG_SUSPEND|MSM_PM_DEBUG_POWER_COLLAPSE,
998 KERN_INFO, "%s(): idle %d, delay %u, limit %u\n", __func__,
999 (int)from_idle, sleep_delay, sleep_limit);
1000
1001 if (!(smsm_get_state(SMSM_POWER_MASTER_DEM) & DEM_MASTER_SMSM_READY)) {
1002 MSM_PM_DPRINTK(
1003 MSM_PM_DEBUG_SUSPEND | MSM_PM_DEBUG_POWER_COLLAPSE,
1004 KERN_INFO, "%s(): master not ready\n", __func__);
1005 ret = -EBUSY;
1006 goto power_collapse_bail;
1007 }
1008
1009 memset(msm_pm_smem_data, 0, sizeof(*msm_pm_smem_data));
1010
1011 msm_irq_enter_sleep1(true, from_idle, &msm_pm_smem_data->irq_mask);
1012 msm_sirc_enter_sleep();
1013 msm_gpio_enter_sleep(from_idle);
1014
1015 msm_pm_smem_data->sleep_time = sleep_delay;
1016 msm_pm_smem_data->resources_used = sleep_limit;
1017
1018 /* Enter PWRC/PWRC_SUSPEND */
1019
1020 if (from_idle)
1021 smsm_change_state(SMSM_APPS_DEM, DEM_SLAVE_SMSM_RUN,
1022 DEM_SLAVE_SMSM_PWRC);
1023 else
1024 smsm_change_state(SMSM_APPS_DEM, DEM_SLAVE_SMSM_RUN,
1025 DEM_SLAVE_SMSM_PWRC | DEM_SLAVE_SMSM_PWRC_SUSPEND);
1026
1027 MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): PWRC");
1028 MSM_PM_DEBUG_PRINT_SLEEP_INFO();
1029
1030 memset(state_grps, 0, sizeof(state_grps));
1031 state_grps[0].group_id = SMSM_POWER_MASTER_DEM;
1032 state_grps[0].bits_all_set = DEM_MASTER_SMSM_RSA;
1033 state_grps[1].group_id = SMSM_MODEM_STATE;
1034 state_grps[1].bits_all_set = SMSM_RESET;
1035
1036 ret = msm_pm_poll_state(ARRAY_SIZE(state_grps), state_grps);
1037
1038 if (ret < 0) {
1039 printk(KERN_EMERG "%s(): power collapse entry "
1040 "timed out waiting for Modem's response\n", __func__);
1041 msm_pm_timeout();
1042 }
1043
1044 if (ret == 1) {
1045 MSM_PM_DPRINTK(
1046 MSM_PM_DEBUG_SUSPEND|MSM_PM_DEBUG_POWER_COLLAPSE,
1047 KERN_INFO,
1048 "%s(): msm_pm_poll_state detected Modem reset\n",
1049 __func__);
1050 goto power_collapse_early_exit;
1051 }
1052
1053 /* DEM Master in RSA */
1054
1055 MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): PWRC RSA");
1056
1057 ret = msm_irq_enter_sleep2(true, from_idle);
1058 if (ret < 0) {
1059 MSM_PM_DPRINTK(
1060 MSM_PM_DEBUG_SUSPEND|MSM_PM_DEBUG_POWER_COLLAPSE,
1061 KERN_INFO,
1062 "%s(): msm_irq_enter_sleep2 aborted, %d\n", __func__,
1063 ret);
1064 goto power_collapse_early_exit;
1065 }
1066
1067 msm_pm_config_hw_before_power_down();
1068 MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): pre power down");
1069
1070 saved_acpuclk_rate = acpuclk_power_collapse();
1071 MSM_PM_DPRINTK(MSM_PM_DEBUG_CLOCK, KERN_INFO,
1072 "%s(): change clock rate (old rate = %lu)\n", __func__,
1073 saved_acpuclk_rate);
1074
1075 if (saved_acpuclk_rate == 0) {
1076 msm_pm_config_hw_after_power_up();
1077 goto power_collapse_early_exit;
1078 }
1079
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -06001080 msm_pm_boot_config_before_pc(smp_processor_id(),
1081 virt_to_phys(msm_pm_collapse_exit));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001082
1083#ifdef CONFIG_VFP
1084 if (from_idle)
1085 vfp_flush_context();
1086#endif
1087
1088#ifdef CONFIG_CACHE_L2X0
1089 l2x0_suspend();
1090#endif
1091
1092 collapsed = msm_pm_collapse();
1093
1094#ifdef CONFIG_CACHE_L2X0
1095 l2x0_resume(collapsed);
1096#endif
1097
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -06001098 msm_pm_boot_config_after_pc(smp_processor_id());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001099
1100 if (collapsed) {
1101#ifdef CONFIG_VFP
1102 if (from_idle)
1103 vfp_reinit();
1104#endif
1105 cpu_init();
1106 local_fiq_enable();
1107 }
1108
1109 MSM_PM_DPRINTK(MSM_PM_DEBUG_SUSPEND | MSM_PM_DEBUG_POWER_COLLAPSE,
1110 KERN_INFO,
1111 "%s(): msm_pm_collapse returned %d\n", __func__, collapsed);
1112
1113 MSM_PM_DPRINTK(MSM_PM_DEBUG_CLOCK, KERN_INFO,
1114 "%s(): restore clock rate to %lu\n", __func__,
1115 saved_acpuclk_rate);
1116 if (acpuclk_set_rate(smp_processor_id(), saved_acpuclk_rate,
1117 SETRATE_PC) < 0)
1118 printk(KERN_ERR "%s(): failed to restore clock rate(%lu)\n",
1119 __func__, saved_acpuclk_rate);
1120
1121 msm_irq_exit_sleep1(msm_pm_smem_data->irq_mask,
1122 msm_pm_smem_data->wakeup_reason,
1123 msm_pm_smem_data->pending_irqs);
1124
1125 msm_pm_config_hw_after_power_up();
1126 MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): post power up");
1127
1128 memset(state_grps, 0, sizeof(state_grps));
1129 state_grps[0].group_id = SMSM_POWER_MASTER_DEM;
1130 state_grps[0].bits_any_set =
1131 DEM_MASTER_SMSM_RSA | DEM_MASTER_SMSM_PWRC_EARLY_EXIT;
1132 state_grps[1].group_id = SMSM_MODEM_STATE;
1133 state_grps[1].bits_all_set = SMSM_RESET;
1134
1135 ret = msm_pm_poll_state(ARRAY_SIZE(state_grps), state_grps);
1136
1137 if (ret < 0) {
1138 printk(KERN_EMERG "%s(): power collapse exit "
1139 "timed out waiting for Modem's response\n", __func__);
1140 msm_pm_timeout();
1141 }
1142
1143 if (ret == 1) {
1144 MSM_PM_DPRINTK(
1145 MSM_PM_DEBUG_SUSPEND|MSM_PM_DEBUG_POWER_COLLAPSE,
1146 KERN_INFO,
1147 "%s(): msm_pm_poll_state detected Modem reset\n",
1148 __func__);
1149 goto power_collapse_early_exit;
1150 }
1151
1152 /* Sanity check */
1153 if (collapsed) {
1154 BUG_ON(!(state_grps[0].value_read & DEM_MASTER_SMSM_RSA));
1155 } else {
1156 BUG_ON(!(state_grps[0].value_read &
1157 DEM_MASTER_SMSM_PWRC_EARLY_EXIT));
1158 goto power_collapse_early_exit;
1159 }
1160
1161 /* Enter WFPI */
1162
1163 smsm_change_state(SMSM_APPS_DEM,
1164 DEM_SLAVE_SMSM_PWRC | DEM_SLAVE_SMSM_PWRC_SUSPEND,
1165 DEM_SLAVE_SMSM_WFPI);
1166
1167 MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): WFPI");
1168
1169 memset(state_grps, 0, sizeof(state_grps));
1170 state_grps[0].group_id = SMSM_POWER_MASTER_DEM;
1171 state_grps[0].bits_all_set = DEM_MASTER_SMSM_RUN;
1172 state_grps[1].group_id = SMSM_MODEM_STATE;
1173 state_grps[1].bits_all_set = SMSM_RESET;
1174
1175 ret = msm_pm_poll_state(ARRAY_SIZE(state_grps), state_grps);
1176
1177 if (ret < 0) {
1178 printk(KERN_EMERG "%s(): power collapse WFPI "
1179 "timed out waiting for Modem's response\n", __func__);
1180 msm_pm_timeout();
1181 }
1182
1183 if (ret == 1) {
1184 MSM_PM_DPRINTK(
1185 MSM_PM_DEBUG_SUSPEND|MSM_PM_DEBUG_POWER_COLLAPSE,
1186 KERN_INFO,
1187 "%s(): msm_pm_poll_state detected Modem reset\n",
1188 __func__);
1189 ret = -EAGAIN;
1190 goto power_collapse_restore_gpio_bail;
1191 }
1192
1193 /* DEM Master == RUN */
1194
1195 MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): WFPI RUN");
1196 MSM_PM_DEBUG_PRINT_SLEEP_INFO();
1197
1198 msm_irq_exit_sleep2(msm_pm_smem_data->irq_mask,
1199 msm_pm_smem_data->wakeup_reason,
1200 msm_pm_smem_data->pending_irqs);
1201 msm_irq_exit_sleep3(msm_pm_smem_data->irq_mask,
1202 msm_pm_smem_data->wakeup_reason,
1203 msm_pm_smem_data->pending_irqs);
1204 msm_gpio_exit_sleep();
1205 msm_sirc_exit_sleep();
1206
1207 smsm_change_state(SMSM_APPS_DEM,
1208 DEM_SLAVE_SMSM_WFPI, DEM_SLAVE_SMSM_RUN);
1209
1210 MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): RUN");
1211
1212 smd_sleep_exit();
1213 return 0;
1214
1215power_collapse_early_exit:
1216 /* Enter PWRC_EARLY_EXIT */
1217
1218 smsm_change_state(SMSM_APPS_DEM,
1219 DEM_SLAVE_SMSM_PWRC | DEM_SLAVE_SMSM_PWRC_SUSPEND,
1220 DEM_SLAVE_SMSM_PWRC_EARLY_EXIT);
1221
1222 MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): EARLY_EXIT");
1223
1224 memset(state_grps, 0, sizeof(state_grps));
1225 state_grps[0].group_id = SMSM_POWER_MASTER_DEM;
1226 state_grps[0].bits_all_set = DEM_MASTER_SMSM_PWRC_EARLY_EXIT;
1227 state_grps[1].group_id = SMSM_MODEM_STATE;
1228 state_grps[1].bits_all_set = SMSM_RESET;
1229
1230 ret = msm_pm_poll_state(ARRAY_SIZE(state_grps), state_grps);
1231 MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): EARLY_EXIT EE");
1232
1233 if (ret < 0) {
1234 printk(KERN_EMERG "%s(): power collapse EARLY_EXIT "
1235 "timed out waiting for Modem's response\n", __func__);
1236 msm_pm_timeout();
1237 }
1238
1239 if (ret == 1) {
1240 MSM_PM_DPRINTK(
1241 MSM_PM_DEBUG_SUSPEND|MSM_PM_DEBUG_POWER_COLLAPSE,
1242 KERN_INFO,
1243 "%s(): msm_pm_poll_state detected Modem reset\n",
1244 __func__);
1245 }
1246
1247 /* DEM Master == RESET or PWRC_EARLY_EXIT */
1248
1249 ret = -EAGAIN;
1250
1251power_collapse_restore_gpio_bail:
1252 msm_gpio_exit_sleep();
1253 msm_sirc_exit_sleep();
1254
1255 /* Enter RUN */
1256 smsm_change_state(SMSM_APPS_DEM,
1257 DEM_SLAVE_SMSM_PWRC | DEM_SLAVE_SMSM_PWRC_SUSPEND |
1258 DEM_SLAVE_SMSM_PWRC_EARLY_EXIT, DEM_SLAVE_SMSM_RUN);
1259
1260 MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): RUN");
1261
1262 if (collapsed)
1263 smd_sleep_exit();
1264
1265power_collapse_bail:
1266 return ret;
1267}
1268
1269/*
1270 * Power collapse the Apps processor without involving Modem.
1271 *
1272 * Return value:
1273 * 0: success
1274 */
1275static int msm_pm_power_collapse_standalone(void)
1276{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001277 int collapsed = 0;
1278 int ret;
1279
1280 MSM_PM_DPRINTK(MSM_PM_DEBUG_SUSPEND|MSM_PM_DEBUG_POWER_COLLAPSE,
1281 KERN_INFO, "%s()\n", __func__);
1282
1283 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_POWER_COLLAPSE, false);
1284 WARN_ON(ret);
1285
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -06001286 msm_pm_boot_config_before_pc(smp_processor_id(),
1287 virt_to_phys(msm_pm_collapse_exit));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001288
1289#ifdef CONFIG_VFP
1290 vfp_flush_context();
1291#endif
1292
1293#ifdef CONFIG_CACHE_L2X0
1294 l2x0_suspend();
1295#endif
1296
1297 collapsed = msm_pm_collapse();
1298
1299#ifdef CONFIG_CACHE_L2X0
1300 l2x0_resume(collapsed);
1301#endif
1302
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -06001303 msm_pm_boot_config_after_pc(smp_processor_id());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001304
1305 if (collapsed) {
1306#ifdef CONFIG_VFP
1307 vfp_reinit();
1308#endif
1309 cpu_init();
1310 local_fiq_enable();
1311 }
1312
1313 MSM_PM_DPRINTK(MSM_PM_DEBUG_SUSPEND | MSM_PM_DEBUG_POWER_COLLAPSE,
1314 KERN_INFO,
1315 "%s(): msm_pm_collapse returned %d\n", __func__, collapsed);
1316
1317 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
1318 WARN_ON(ret);
1319
1320 return 0;
1321}
1322
1323/*
1324 * Apps-sleep the Apps processor. This function execute the handshake
1325 * protocol with Modem.
1326 *
1327 * Return value:
1328 * -ENOSYS: function not implemented yet
1329 */
1330static int msm_pm_apps_sleep(uint32_t sleep_delay, uint32_t sleep_limit)
1331{
1332 return -ENOSYS;
1333}
1334
1335/*
1336 * Bring the Apps processor to SWFI.
1337 *
1338 * Return value:
1339 * -EIO: could not ramp Apps processor clock
1340 * 0: success
1341 */
1342static int msm_pm_swfi(bool ramp_acpu)
1343{
1344 unsigned long saved_acpuclk_rate = 0;
1345
1346 if (ramp_acpu) {
1347 saved_acpuclk_rate = acpuclk_wait_for_irq();
1348 MSM_PM_DPRINTK(MSM_PM_DEBUG_CLOCK, KERN_INFO,
1349 "%s(): change clock rate (old rate = %lu)\n", __func__,
1350 saved_acpuclk_rate);
1351
1352 if (!saved_acpuclk_rate)
1353 return -EIO;
1354 }
1355
1356 msm_pm_config_hw_before_swfi();
1357 msm_arch_idle();
1358
1359 if (ramp_acpu) {
1360 MSM_PM_DPRINTK(MSM_PM_DEBUG_CLOCK, KERN_INFO,
1361 "%s(): restore clock rate to %lu\n", __func__,
1362 saved_acpuclk_rate);
1363 if (acpuclk_set_rate(smp_processor_id(), saved_acpuclk_rate,
1364 SETRATE_SWFI) < 0)
1365 printk(KERN_ERR
1366 "%s(): failed to restore clock rate(%lu)\n",
1367 __func__, saved_acpuclk_rate);
1368 }
1369
1370 return 0;
1371}
1372
1373
1374/******************************************************************************
1375 * External Idle/Suspend Functions
1376 *****************************************************************************/
1377
1378/*
1379 * Put CPU in low power mode.
1380 */
1381void arch_idle(void)
1382{
1383 bool allow[MSM_PM_SLEEP_MODE_NR];
1384 uint32_t sleep_limit = SLEEP_LIMIT_NONE;
1385
1386 int latency_qos;
1387 int64_t timer_expiration;
1388
1389 int low_power;
1390 int ret;
1391 int i;
Murali Nalajala0df9fee2012-01-12 15:26:09 +05301392 unsigned int cpu;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001393
1394#ifdef CONFIG_MSM_IDLE_STATS
1395 int64_t t1;
1396 static int64_t t2;
1397 int exit_stat;
Murali Nalajala7744d162012-01-13 13:06:03 +05301398 #endif /* CONFIG_MSM_IDLE_STATS */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001399
1400 if (!atomic_read(&msm_pm_init_done))
1401 return;
1402
Murali Nalajala0df9fee2012-01-12 15:26:09 +05301403 cpu = smp_processor_id();
1404
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001405 latency_qos = pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
1406 timer_expiration = msm_timer_enter_idle();
1407
1408#ifdef CONFIG_MSM_IDLE_STATS
1409 t1 = ktime_to_ns(ktime_get());
1410 msm_pm_add_stat(MSM_PM_STAT_NOT_IDLE, t1 - t2);
1411 msm_pm_add_stat(MSM_PM_STAT_REQUESTED_IDLE, timer_expiration);
Murali Nalajala7744d162012-01-13 13:06:03 +05301412
1413 exit_stat = MSM_PM_STAT_IDLE_SPIN;
1414 low_power = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001415#endif /* CONFIG_MSM_IDLE_STATS */
1416
1417 for (i = 0; i < ARRAY_SIZE(allow); i++)
1418 allow[i] = true;
1419
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001420 if ((timer_expiration < msm_pm_idle_sleep_min_time) ||
1421#ifdef CONFIG_HAS_WAKELOCK
1422 has_wake_lock(WAKE_LOCK_IDLE) ||
1423#endif
1424 !msm_irq_idle_sleep_allowed()) {
1425 allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE] = false;
1426 allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_NO_XO_SHUTDOWN] = false;
1427 allow[MSM_PM_SLEEP_MODE_APPS_SLEEP] = false;
1428 }
1429
1430 for (i = 0; i < ARRAY_SIZE(allow); i++) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +05301431 struct msm_pm_platform_data *mode =
1432 &msm_pm_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001433 if (!mode->idle_supported || !mode->idle_enabled ||
1434 mode->latency >= latency_qos ||
1435 mode->residency * 1000ULL >= timer_expiration)
1436 allow[i] = false;
1437 }
1438
1439 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE] ||
1440 allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_NO_XO_SHUTDOWN]) {
1441 uint32_t wait_us = CONFIG_MSM_IDLE_WAIT_ON_MODEM;
1442 while (msm_pm_modem_busy() && wait_us) {
1443 if (wait_us > 100) {
1444 udelay(100);
1445 wait_us -= 100;
1446 } else {
1447 udelay(wait_us);
1448 wait_us = 0;
1449 }
1450 }
1451
1452 if (msm_pm_modem_busy()) {
1453 allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE] = false;
1454 allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_NO_XO_SHUTDOWN]
1455 = false;
1456 }
1457 }
1458
1459 MSM_PM_DPRINTK(MSM_PM_DEBUG_IDLE, KERN_INFO,
1460 "%s(): latency qos %d, next timer %lld, sleep limit %u\n",
1461 __func__, latency_qos, timer_expiration, sleep_limit);
1462
1463 for (i = 0; i < ARRAY_SIZE(allow); i++)
1464 MSM_PM_DPRINTK(MSM_PM_DEBUG_IDLE, KERN_INFO,
1465 "%s(): allow %s: %d\n", __func__,
1466 msm_pm_sleep_mode_labels[i], (int)allow[i]);
1467
1468 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE] ||
1469 allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_NO_XO_SHUTDOWN]) {
1470 uint32_t sleep_delay;
1471
1472 sleep_delay = (uint32_t) msm_pm_convert_and_cap_time(
1473 timer_expiration, MSM_PM_SLEEP_TICK_LIMIT);
1474 if (sleep_delay == 0) /* 0 would mean infinite time */
1475 sleep_delay = 1;
1476
1477 if (!allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE])
1478 sleep_limit = SLEEP_LIMIT_NO_TCXO_SHUTDOWN;
1479
1480#if defined(CONFIG_MSM_MEMORY_LOW_POWER_MODE_IDLE_ACTIVE)
1481 sleep_limit |= SLEEP_RESOURCE_MEMORY_BIT1;
1482#elif defined(CONFIG_MSM_MEMORY_LOW_POWER_MODE_IDLE_RETENTION)
1483 sleep_limit |= SLEEP_RESOURCE_MEMORY_BIT0;
1484#endif
1485
1486 ret = msm_pm_power_collapse(true, sleep_delay, sleep_limit);
1487 low_power = (ret != -EBUSY && ret != -ETIMEDOUT);
1488
1489#ifdef CONFIG_MSM_IDLE_STATS
1490 if (ret)
1491 exit_stat = MSM_PM_STAT_IDLE_FAILED_POWER_COLLAPSE;
1492 else {
1493 exit_stat = MSM_PM_STAT_IDLE_POWER_COLLAPSE;
1494 msm_pm_sleep_limit = sleep_limit;
1495 }
1496#endif /* CONFIG_MSM_IDLE_STATS */
1497 } else if (allow[MSM_PM_SLEEP_MODE_APPS_SLEEP]) {
1498 uint32_t sleep_delay;
1499
1500 sleep_delay = (uint32_t) msm_pm_convert_and_cap_time(
1501 timer_expiration, MSM_PM_SLEEP_TICK_LIMIT);
1502 if (sleep_delay == 0) /* 0 would mean infinite time */
1503 sleep_delay = 1;
1504
1505 ret = msm_pm_apps_sleep(sleep_delay, sleep_limit);
1506 low_power = 0;
1507
1508#ifdef CONFIG_MSM_IDLE_STATS
1509 if (ret)
1510 exit_stat = MSM_PM_STAT_IDLE_FAILED_SLEEP;
1511 else
1512 exit_stat = MSM_PM_STAT_IDLE_SLEEP;
1513#endif /* CONFIG_MSM_IDLE_STATS */
1514 } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
1515 ret = msm_pm_power_collapse_standalone();
1516 low_power = 0;
1517#ifdef CONFIG_MSM_IDLE_STATS
1518 exit_stat = ret ?
1519 MSM_PM_STAT_IDLE_FAILED_STANDALONE_POWER_COLLAPSE :
1520 MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE;
1521#endif /* CONFIG_MSM_IDLE_STATS */
1522 } else if (allow[MSM_PM_SLEEP_MODE_RAMP_DOWN_AND_WAIT_FOR_INTERRUPT]) {
1523 ret = msm_pm_swfi(true);
1524 if (ret)
1525 while (!msm_irq_pending())
1526 udelay(1);
1527 low_power = 0;
1528#ifdef CONFIG_MSM_IDLE_STATS
1529 exit_stat = ret ? MSM_PM_STAT_IDLE_SPIN : MSM_PM_STAT_IDLE_WFI;
1530#endif /* CONFIG_MSM_IDLE_STATS */
1531 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
1532 msm_pm_swfi(false);
1533 low_power = 0;
1534#ifdef CONFIG_MSM_IDLE_STATS
1535 exit_stat = MSM_PM_STAT_IDLE_WFI;
1536#endif /* CONFIG_MSM_IDLE_STATS */
1537 } else {
1538 while (!msm_irq_pending())
1539 udelay(1);
1540 low_power = 0;
1541#ifdef CONFIG_MSM_IDLE_STATS
1542 exit_stat = MSM_PM_STAT_IDLE_SPIN;
1543#endif /* CONFIG_MSM_IDLE_STATS */
1544 }
1545
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001546 msm_timer_exit_idle(low_power);
1547
1548#ifdef CONFIG_MSM_IDLE_STATS
1549 t2 = ktime_to_ns(ktime_get());
1550 msm_pm_add_stat(exit_stat, t2 - t1);
1551#endif /* CONFIG_MSM_IDLE_STATS */
1552}
1553
1554/*
1555 * Suspend the Apps processor.
1556 *
1557 * Return value:
1558 * -EAGAIN: modem reset occurred or early exit from suspend
1559 * -EBUSY: modem not ready for our suspend
1560 * -EINVAL: invalid sleep mode
1561 * -EIO: could not ramp Apps processor clock
1562 * -ETIMEDOUT: timed out waiting for modem's handshake
1563 * 0: success
1564 */
1565static int msm_pm_enter(suspend_state_t state)
1566{
1567 bool allow[MSM_PM_SLEEP_MODE_NR];
1568 uint32_t sleep_limit = SLEEP_LIMIT_NONE;
1569 int ret;
1570 int i;
1571
1572#ifdef CONFIG_MSM_IDLE_STATS
1573 int64_t period = 0;
1574 int64_t time = 0;
1575
1576 time = msm_timer_get_sclk_time(&period);
1577#endif
1578
1579 MSM_PM_DPRINTK(MSM_PM_DEBUG_SUSPEND, KERN_INFO,
1580 "%s(): sleep limit %u\n", __func__, sleep_limit);
1581
1582 for (i = 0; i < ARRAY_SIZE(allow); i++)
1583 allow[i] = true;
1584
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001585 for (i = 0; i < ARRAY_SIZE(allow); i++) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +05301586 struct msm_pm_platform_data *mode;
1587 mode = &msm_pm_modes[MSM_PM_MODE(0, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001588 if (!mode->suspend_supported || !mode->suspend_enabled)
1589 allow[i] = false;
1590 }
1591
1592 ret = 0;
1593
1594 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE] ||
1595 allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_NO_XO_SHUTDOWN]) {
1596#ifdef CONFIG_MSM_IDLE_STATS
1597 enum msm_pm_time_stats_id id;
1598 int64_t end_time;
1599#endif
1600
1601 clock_debug_print_enabled();
1602
1603#ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE
1604 if (msm_pm_sleep_time_override > 0) {
1605 int64_t ns;
1606 ns = NSEC_PER_SEC * (int64_t)msm_pm_sleep_time_override;
1607 msm_pm_set_max_sleep_time(ns);
1608 msm_pm_sleep_time_override = 0;
1609 }
1610#endif
1611 if (!allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE])
1612 sleep_limit = SLEEP_LIMIT_NO_TCXO_SHUTDOWN;
1613
1614#if defined(CONFIG_MSM_MEMORY_LOW_POWER_MODE_SUSPEND_ACTIVE)
1615 sleep_limit |= SLEEP_RESOURCE_MEMORY_BIT1;
1616#elif defined(CONFIG_MSM_MEMORY_LOW_POWER_MODE_SUSPEND_RETENTION)
1617 sleep_limit |= SLEEP_RESOURCE_MEMORY_BIT0;
1618#elif defined(CONFIG_MSM_MEMORY_LOW_POWER_MODE_SUSPEND_DEEP_POWER_DOWN)
1619 if (get_msm_migrate_pages_status() != MEM_OFFLINE)
1620 sleep_limit |= SLEEP_RESOURCE_MEMORY_BIT0;
1621#endif
1622
1623 for (i = 0; i < 30 && msm_pm_modem_busy(); i++)
1624 udelay(500);
1625
1626 ret = msm_pm_power_collapse(
1627 false, msm_pm_max_sleep_time, sleep_limit);
1628
1629#ifdef CONFIG_MSM_IDLE_STATS
1630 if (ret)
1631 id = MSM_PM_STAT_FAILED_SUSPEND;
1632 else {
1633 id = MSM_PM_STAT_SUSPEND;
1634 msm_pm_sleep_limit = sleep_limit;
1635 }
1636
1637 if (time != 0) {
1638 end_time = msm_timer_get_sclk_time(NULL);
1639 if (end_time != 0) {
1640 time = end_time - time;
1641 if (time < 0)
1642 time += period;
1643 } else
1644 time = 0;
1645 }
1646
1647 msm_pm_add_stat(id, time);
1648#endif
1649 } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
1650 ret = msm_pm_power_collapse_standalone();
1651 } else if (allow[MSM_PM_SLEEP_MODE_RAMP_DOWN_AND_WAIT_FOR_INTERRUPT]) {
1652 ret = msm_pm_swfi(true);
1653 if (ret)
1654 while (!msm_irq_pending())
1655 udelay(1);
1656 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
1657 msm_pm_swfi(false);
1658 }
1659
1660 MSM_PM_DPRINTK(MSM_PM_DEBUG_SUSPEND, KERN_INFO,
1661 "%s(): return %d\n", __func__, ret);
1662
1663 return ret;
1664}
1665
1666static struct platform_suspend_ops msm_pm_ops = {
1667 .enter = msm_pm_enter,
1668 .valid = suspend_valid_only_mem,
1669};
1670
Murali Nalajalac89f2f32012-02-07 19:23:52 +05301671/* Hotplug the "non boot" CPU's and put
1672 * the cores into low power mode
1673 */
1674void msm_pm_cpu_enter_lowpower(unsigned int cpu)
1675{
1676 return;
1677}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001678
1679/******************************************************************************
1680 * Restart Definitions
1681 *****************************************************************************/
1682
1683static uint32_t restart_reason = 0x776655AA;
1684
1685static void msm_pm_power_off(void)
1686{
1687 msm_rpcrouter_close();
1688 msm_proc_comm(PCOM_POWER_DOWN, 0, 0);
1689 for (;;)
1690 ;
1691}
1692
1693static void msm_pm_restart(char str, const char *cmd)
1694{
1695 msm_rpcrouter_close();
1696 msm_proc_comm(PCOM_RESET_CHIP, &restart_reason, 0);
1697
1698 for (;;)
1699 ;
1700}
1701
1702static int msm_reboot_call
1703 (struct notifier_block *this, unsigned long code, void *_cmd)
1704{
1705 if ((code == SYS_RESTART) && _cmd) {
1706 char *cmd = _cmd;
1707 if (!strcmp(cmd, "bootloader")) {
1708 restart_reason = 0x77665500;
1709 } else if (!strcmp(cmd, "recovery")) {
1710 restart_reason = 0x77665502;
1711 } else if (!strcmp(cmd, "eraseflash")) {
1712 restart_reason = 0x776655EF;
1713 } else if (!strncmp(cmd, "oem-", 4)) {
1714 unsigned code = simple_strtoul(cmd + 4, 0, 16) & 0xff;
1715 restart_reason = 0x6f656d00 | code;
1716 } else {
1717 restart_reason = 0x77665501;
1718 }
1719 }
1720 return NOTIFY_DONE;
1721}
1722
1723static struct notifier_block msm_reboot_notifier = {
1724 .notifier_call = msm_reboot_call,
1725};
1726
1727
1728/******************************************************************************
1729 *
1730 *****************************************************************************/
1731
1732/*
1733 * Initialize the power management subsystem.
1734 *
1735 * Return value:
1736 * -ENODEV: initialization failed
1737 * 0: success
1738 */
1739static int __init msm_pm_init(void)
1740{
1741#ifdef CONFIG_MSM_IDLE_STATS
1742 struct proc_dir_entry *d_entry;
Murali Nalajala0df9fee2012-01-12 15:26:09 +05301743 unsigned int cpu;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001744#endif
1745 int ret;
1746#ifdef CONFIG_CPU_V7
1747 pgd_t *pc_pgd;
1748 pmd_t *pmd;
1749 unsigned long pmdval;
1750
1751 /* Page table for cores to come back up safely. */
1752 pc_pgd = pgd_alloc(&init_mm);
1753 if (!pc_pgd)
1754 return -ENOMEM;
1755 pmd = pmd_offset(pc_pgd +
1756 pgd_index(virt_to_phys(msm_pm_collapse_exit)),
1757 virt_to_phys(msm_pm_collapse_exit));
1758 pmdval = (virt_to_phys(msm_pm_collapse_exit) & PGDIR_MASK) |
1759 PMD_TYPE_SECT | PMD_SECT_AP_WRITE;
1760 pmd[0] = __pmd(pmdval);
1761 pmd[1] = __pmd(pmdval + (1 << (PGDIR_SHIFT - 1)));
1762
1763 /* It is remotely possible that the code in msm_pm_collapse_exit()
1764 * which turns on the MMU with this mapping is in the
1765 * next even-numbered megabyte beyond the
1766 * start of msm_pm_collapse_exit().
1767 * Map this megabyte in as well.
1768 */
1769 pmd[2] = __pmd(pmdval + (2 << (PGDIR_SHIFT - 1)));
1770 flush_pmd_entry(pmd);
1771 msm_pm_pc_pgd = virt_to_phys(pc_pgd);
1772#endif
1773
1774 pm_power_off = msm_pm_power_off;
1775 arm_pm_restart = msm_pm_restart;
1776 register_reboot_notifier(&msm_reboot_notifier);
1777
1778 msm_pm_smem_data = smem_alloc(SMEM_APPS_DEM_SLAVE_DATA,
1779 sizeof(*msm_pm_smem_data));
1780 if (msm_pm_smem_data == NULL) {
1781 printk(KERN_ERR "%s: failed to get smsm_data\n", __func__);
1782 return -ENODEV;
1783 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001784
1785 ret = msm_timer_init_time_sync(msm_pm_timeout);
1786 if (ret)
1787 return ret;
1788
1789 ret = smsm_change_intr_mask(SMSM_POWER_MASTER_DEM, 0xFFFFFFFF, 0);
1790 if (ret) {
1791 printk(KERN_ERR "%s: failed to clear interrupt mask, %d\n",
1792 __func__, ret);
1793 return ret;
1794 }
1795
1796#ifdef CONFIG_MSM_MEMORY_LOW_POWER_MODE
1797 /* The wakeup_reason field is overloaded during initialization time
1798 to signal Modem that Apps will control the low power modes of
1799 the memory.
1800 */
1801 msm_pm_smem_data->wakeup_reason = 1;
1802 smsm_change_state(SMSM_APPS_DEM, 0, DEM_SLAVE_SMSM_RUN);
1803#endif
1804
1805 BUG_ON(msm_pm_modes == NULL);
1806
1807 atomic_set(&msm_pm_init_done, 1);
1808 suspend_set_ops(&msm_pm_ops);
1809
1810 msm_pm_mode_sysfs_add();
1811#ifdef CONFIG_MSM_IDLE_STATS
Murali Nalajala0df9fee2012-01-12 15:26:09 +05301812 for_each_possible_cpu(cpu) {
1813 struct msm_pm_time_stats *stats =
1814 per_cpu(msm_pm_stats, cpu).stats;
1815
1816 stats[MSM_PM_STAT_REQUESTED_IDLE].name = "idle-request";
1817 stats[MSM_PM_STAT_REQUESTED_IDLE].first_bucket_time =
1818 CONFIG_MSM_IDLE_STATS_FIRST_BUCKET;
1819
1820 stats[MSM_PM_STAT_IDLE_SPIN].name = "idle-spin";
1821 stats[MSM_PM_STAT_IDLE_SPIN].first_bucket_time =
1822 CONFIG_MSM_IDLE_STATS_FIRST_BUCKET;
1823
1824 stats[MSM_PM_STAT_IDLE_WFI].name = "idle-wfi";
1825 stats[MSM_PM_STAT_IDLE_WFI].first_bucket_time =
1826 CONFIG_MSM_IDLE_STATS_FIRST_BUCKET;
1827
1828 stats[MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE].name =
1829 "idle-standalone-power-collapse";
1830 stats[MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE].
1831 first_bucket_time =
1832 CONFIG_MSM_IDLE_STATS_FIRST_BUCKET;
1833
1834 stats[MSM_PM_STAT_IDLE_FAILED_STANDALONE_POWER_COLLAPSE].name =
1835 "idle-failed-standalone-power-collapse";
1836 stats[MSM_PM_STAT_IDLE_FAILED_STANDALONE_POWER_COLLAPSE].
1837 first_bucket_time =
1838 CONFIG_MSM_IDLE_STATS_FIRST_BUCKET;
1839
1840 stats[MSM_PM_STAT_IDLE_SLEEP].name = "idle-sleep";
1841 stats[MSM_PM_STAT_IDLE_SLEEP].first_bucket_time =
1842 CONFIG_MSM_IDLE_STATS_FIRST_BUCKET;
1843
1844 stats[MSM_PM_STAT_IDLE_FAILED_SLEEP].name =
1845 "idle-failed-sleep";
1846 stats[MSM_PM_STAT_IDLE_FAILED_SLEEP].first_bucket_time =
1847 CONFIG_MSM_IDLE_STATS_FIRST_BUCKET;
1848
1849 stats[MSM_PM_STAT_IDLE_POWER_COLLAPSE].name =
1850 "idle-power-collapse";
1851 stats[MSM_PM_STAT_IDLE_POWER_COLLAPSE].first_bucket_time =
1852 CONFIG_MSM_IDLE_STATS_FIRST_BUCKET;
1853
1854 stats[MSM_PM_STAT_IDLE_FAILED_POWER_COLLAPSE].name =
1855 "idle-failed-power-collapse";
1856 stats[MSM_PM_STAT_IDLE_FAILED_POWER_COLLAPSE].
1857 first_bucket_time =
1858 CONFIG_MSM_IDLE_STATS_FIRST_BUCKET;
1859
1860 stats[MSM_PM_STAT_SUSPEND].name = "suspend";
1861 stats[MSM_PM_STAT_SUSPEND].first_bucket_time =
1862 CONFIG_MSM_SUSPEND_STATS_FIRST_BUCKET;
1863
1864 stats[MSM_PM_STAT_FAILED_SUSPEND].name = "failed-suspend";
1865 stats[MSM_PM_STAT_FAILED_SUSPEND].first_bucket_time =
1866 CONFIG_MSM_IDLE_STATS_FIRST_BUCKET;
1867
1868 stats[MSM_PM_STAT_NOT_IDLE].name = "not-idle";
1869 stats[MSM_PM_STAT_NOT_IDLE].first_bucket_time =
1870 CONFIG_MSM_IDLE_STATS_FIRST_BUCKET;
1871 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001872 d_entry = create_proc_entry("msm_pm_stats",
1873 S_IRUGO | S_IWUSR | S_IWGRP, NULL);
1874 if (d_entry) {
1875 d_entry->read_proc = msm_pm_read_proc;
1876 d_entry->write_proc = msm_pm_write_proc;
1877 d_entry->data = NULL;
1878 }
1879#endif
1880
1881 return 0;
1882}
1883
1884late_initcall_sync(msm_pm_init);