blob: 163caa5d08da38e6673a0400b4358f6f2b84d71f [file] [log] [blame]
Karthik Parsha6fb932d2012-01-24 18:04:12 -08001/* Copyright (c) 2010-2012, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13
14#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/init.h>
17#include <linux/completion.h>
18#include <linux/cpuidle.h>
19#include <linux/interrupt.h>
20#include <linux/io.h>
21#include <linux/ktime.h>
22#include <linux/pm.h>
Steve Mucklef132c6c2012-06-06 18:30:57 -070023#include <linux/pm_qos.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070024#include <linux/smp.h>
25#include <linux/suspend.h>
26#include <linux/tick.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070027#include <linux/wakelock.h>
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -060028#include <linux/delay.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070029#include <mach/msm_iomap.h>
Praveen Chidambaram192979f2012-04-25 18:30:23 -060030#include <mach/socinfo.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070031#include <mach/system.h>
32#include <asm/cacheflush.h>
33#include <asm/hardware/gic.h>
34#include <asm/pgtable.h>
35#include <asm/pgalloc.h>
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -060036#include <asm/hardware/cache-l2x0.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070037#ifdef CONFIG_VFP
38#include <asm/vfp.h>
39#endif
40
41#include "acpuclock.h"
42#include "clock.h"
43#include "avs.h"
Abhijeet Dharmapurikarefaca4f2011-12-27 16:24:07 -080044#include <mach/cpuidle.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070045#include "idle.h"
Matt Wagantall7cca4642012-02-01 16:43:24 -080046#include "pm.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070047#include "scm-boot.h"
48#include "spm.h"
49#include "timer.h"
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -060050#include "pm-boot.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070051
52/******************************************************************************
53 * Debug Definitions
54 *****************************************************************************/
55
56enum {
57 MSM_PM_DEBUG_SUSPEND = BIT(0),
58 MSM_PM_DEBUG_POWER_COLLAPSE = BIT(1),
59 MSM_PM_DEBUG_SUSPEND_LIMITS = BIT(2),
60 MSM_PM_DEBUG_CLOCK = BIT(3),
61 MSM_PM_DEBUG_RESET_VECTOR = BIT(4),
Karthik Parsha6fb932d2012-01-24 18:04:12 -080062 MSM_PM_DEBUG_IDLE_CLK = BIT(5),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070063 MSM_PM_DEBUG_IDLE = BIT(6),
64 MSM_PM_DEBUG_IDLE_LIMITS = BIT(7),
65 MSM_PM_DEBUG_HOTPLUG = BIT(8),
66};
67
68static int msm_pm_debug_mask = 1;
69module_param_named(
70 debug_mask, msm_pm_debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP
71);
72
73
74/******************************************************************************
75 * Sleep Modes and Parameters
76 *****************************************************************************/
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070077enum {
78 MSM_PM_MODE_ATTR_SUSPEND,
79 MSM_PM_MODE_ATTR_IDLE,
80 MSM_PM_MODE_ATTR_NR,
81};
82
83static char *msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_NR] = {
84 [MSM_PM_MODE_ATTR_SUSPEND] = "suspend_enabled",
85 [MSM_PM_MODE_ATTR_IDLE] = "idle_enabled",
86};
87
88struct msm_pm_kobj_attribute {
89 unsigned int cpu;
90 struct kobj_attribute ka;
91};
92
93#define GET_CPU_OF_ATTR(attr) \
94 (container_of(attr, struct msm_pm_kobj_attribute, ka)->cpu)
95
96struct msm_pm_sysfs_sleep_mode {
97 struct kobject *kobj;
98 struct attribute_group attr_group;
99 struct attribute *attrs[MSM_PM_MODE_ATTR_NR + 1];
100 struct msm_pm_kobj_attribute kas[MSM_PM_MODE_ATTR_NR];
101};
102
103static char *msm_pm_sleep_mode_labels[MSM_PM_SLEEP_MODE_NR] = {
104 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE] = "power_collapse",
105 [MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT] = "wfi",
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600106 [MSM_PM_SLEEP_MODE_RETENTION] = "retention",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700107 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE] =
108 "standalone_power_collapse",
109};
110
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600111static struct msm_pm_sleep_ops pm_sleep_ops;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700112/*
113 * Write out the attribute.
114 */
115static ssize_t msm_pm_mode_attr_show(
116 struct kobject *kobj, struct kobj_attribute *attr, char *buf)
117{
118 int ret = -EINVAL;
119 int i;
120
121 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
122 struct kernel_param kp;
123 unsigned int cpu;
124 struct msm_pm_platform_data *mode;
125
126 if (msm_pm_sleep_mode_labels[i] == NULL)
127 continue;
128
129 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
130 continue;
131
132 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600133 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700134
135 if (!strcmp(attr->attr.name,
136 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
137 u32 arg = mode->suspend_enabled;
138 kp.arg = &arg;
139 ret = param_get_ulong(buf, &kp);
140 } else if (!strcmp(attr->attr.name,
141 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
142 u32 arg = mode->idle_enabled;
143 kp.arg = &arg;
144 ret = param_get_ulong(buf, &kp);
145 }
146
147 break;
148 }
149
150 if (ret > 0) {
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600151 strlcat(buf, "\n", PAGE_SIZE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700152 ret++;
153 }
154
155 return ret;
156}
157
158/*
159 * Read in the new attribute value.
160 */
161static ssize_t msm_pm_mode_attr_store(struct kobject *kobj,
162 struct kobj_attribute *attr, const char *buf, size_t count)
163{
164 int ret = -EINVAL;
165 int i;
166
167 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
168 struct kernel_param kp;
169 unsigned int cpu;
170 struct msm_pm_platform_data *mode;
171
172 if (msm_pm_sleep_mode_labels[i] == NULL)
173 continue;
174
175 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
176 continue;
177
178 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600179 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700180
181 if (!strcmp(attr->attr.name,
182 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
183 kp.arg = &mode->suspend_enabled;
184 ret = param_set_byte(buf, &kp);
185 } else if (!strcmp(attr->attr.name,
186 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
187 kp.arg = &mode->idle_enabled;
188 ret = param_set_byte(buf, &kp);
189 }
190
191 break;
192 }
193
194 return ret ? ret : count;
195}
196
197/*
198 * Add sysfs entries for one cpu.
199 */
200static int __init msm_pm_mode_sysfs_add_cpu(
201 unsigned int cpu, struct kobject *modes_kobj)
202{
203 char cpu_name[8];
204 struct kobject *cpu_kobj;
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600205 struct msm_pm_sysfs_sleep_mode *mode = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700206 int i, j, k;
207 int ret;
208
209 snprintf(cpu_name, sizeof(cpu_name), "cpu%u", cpu);
210 cpu_kobj = kobject_create_and_add(cpu_name, modes_kobj);
211 if (!cpu_kobj) {
212 pr_err("%s: cannot create %s kobject\n", __func__, cpu_name);
213 ret = -ENOMEM;
214 goto mode_sysfs_add_cpu_exit;
215 }
216
217 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
218 int idx = MSM_PM_MODE(cpu, i);
219
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600220 if ((!msm_pm_sleep_modes[idx].suspend_supported)
221 && (!msm_pm_sleep_modes[idx].idle_supported))
222 continue;
223
224 if (!msm_pm_sleep_mode_labels[i] ||
225 !msm_pm_sleep_mode_labels[i][0])
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700226 continue;
227
228 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
229 if (!mode) {
230 pr_err("%s: cannot allocate memory for attributes\n",
231 __func__);
232 ret = -ENOMEM;
233 goto mode_sysfs_add_cpu_exit;
234 }
235
236 mode->kobj = kobject_create_and_add(
237 msm_pm_sleep_mode_labels[i], cpu_kobj);
238 if (!mode->kobj) {
239 pr_err("%s: cannot create kobject\n", __func__);
240 ret = -ENOMEM;
241 goto mode_sysfs_add_cpu_exit;
242 }
243
244 for (k = 0, j = 0; k < MSM_PM_MODE_ATTR_NR; k++) {
245 if ((k == MSM_PM_MODE_ATTR_IDLE) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600246 !msm_pm_sleep_modes[idx].idle_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700247 continue;
248 if ((k == MSM_PM_MODE_ATTR_SUSPEND) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600249 !msm_pm_sleep_modes[idx].suspend_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700250 continue;
251 mode->kas[j].cpu = cpu;
252 mode->kas[j].ka.attr.mode = 0644;
253 mode->kas[j].ka.show = msm_pm_mode_attr_show;
254 mode->kas[j].ka.store = msm_pm_mode_attr_store;
255 mode->kas[j].ka.attr.name = msm_pm_mode_attr_labels[k];
256 mode->attrs[j] = &mode->kas[j].ka.attr;
257 j++;
258 }
259 mode->attrs[j] = NULL;
260
261 mode->attr_group.attrs = mode->attrs;
262 ret = sysfs_create_group(mode->kobj, &mode->attr_group);
263 if (ret) {
264 pr_err("%s: cannot create kobject attribute group\n",
265 __func__);
266 goto mode_sysfs_add_cpu_exit;
267 }
268 }
269
270 ret = 0;
271
272mode_sysfs_add_cpu_exit:
Praveen Chidambaramd5ac2d32011-10-24 14:30:27 -0600273 if (ret) {
Praveen Chidambaram2cfda632011-10-11 16:58:09 -0600274 if (mode && mode->kobj)
275 kobject_del(mode->kobj);
276 kfree(mode);
277 }
278
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700279 return ret;
280}
281
282/*
283 * Add sysfs entries for the sleep modes.
284 */
285static int __init msm_pm_mode_sysfs_add(void)
286{
287 struct kobject *module_kobj;
288 struct kobject *modes_kobj;
289 unsigned int cpu;
290 int ret;
291
292 module_kobj = kset_find_obj(module_kset, KBUILD_MODNAME);
293 if (!module_kobj) {
294 pr_err("%s: cannot find kobject for module %s\n",
295 __func__, KBUILD_MODNAME);
296 ret = -ENOENT;
297 goto mode_sysfs_add_exit;
298 }
299
300 modes_kobj = kobject_create_and_add("modes", module_kobj);
301 if (!modes_kobj) {
302 pr_err("%s: cannot create modes kobject\n", __func__);
303 ret = -ENOMEM;
304 goto mode_sysfs_add_exit;
305 }
306
307 for_each_possible_cpu(cpu) {
308 ret = msm_pm_mode_sysfs_add_cpu(cpu, modes_kobj);
309 if (ret)
310 goto mode_sysfs_add_exit;
311 }
312
313 ret = 0;
314
315mode_sysfs_add_exit:
316 return ret;
317}
318
319/******************************************************************************
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700320 * Configure Hardware before/after Low Power Mode
321 *****************************************************************************/
322
323/*
324 * Configure hardware registers in preparation for Apps power down.
325 */
326static void msm_pm_config_hw_before_power_down(void)
327{
328 return;
329}
330
331/*
332 * Clear hardware registers after Apps powers up.
333 */
334static void msm_pm_config_hw_after_power_up(void)
335{
336 return;
337}
338
339/*
340 * Configure hardware registers in preparation for SWFI.
341 */
342static void msm_pm_config_hw_before_swfi(void)
343{
344 return;
345}
346
347
348/******************************************************************************
349 * Suspend Max Sleep Time
350 *****************************************************************************/
351
352#ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE
353static int msm_pm_sleep_time_override;
354module_param_named(sleep_time_override,
355 msm_pm_sleep_time_override, int, S_IRUGO | S_IWUSR | S_IWGRP);
356#endif
357
358#define SCLK_HZ (32768)
359#define MSM_PM_SLEEP_TICK_LIMIT (0x6DDD000)
360
361static uint32_t msm_pm_max_sleep_time;
362
363/*
364 * Convert time from nanoseconds to slow clock ticks, then cap it to the
365 * specified limit
366 */
367static int64_t msm_pm_convert_and_cap_time(int64_t time_ns, int64_t limit)
368{
369 do_div(time_ns, NSEC_PER_SEC / SCLK_HZ);
370 return (time_ns > limit) ? limit : time_ns;
371}
372
373/*
374 * Set the sleep time for suspend. 0 means infinite sleep time.
375 */
376void msm_pm_set_max_sleep_time(int64_t max_sleep_time_ns)
377{
378 if (max_sleep_time_ns == 0) {
379 msm_pm_max_sleep_time = 0;
380 } else {
381 msm_pm_max_sleep_time = (uint32_t)msm_pm_convert_and_cap_time(
382 max_sleep_time_ns, MSM_PM_SLEEP_TICK_LIMIT);
383
384 if (msm_pm_max_sleep_time == 0)
385 msm_pm_max_sleep_time = 1;
386 }
387
388 if (msm_pm_debug_mask & MSM_PM_DEBUG_SUSPEND)
389 pr_info("%s: Requested %lld ns Giving %u sclk ticks\n",
390 __func__, max_sleep_time_ns, msm_pm_max_sleep_time);
391}
392EXPORT_SYMBOL(msm_pm_set_max_sleep_time);
393
394
395/******************************************************************************
396 *
397 *****************************************************************************/
398
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600399static void *msm_pm_idle_rs_limits;
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600400static bool msm_pm_use_qtimer;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700401
402static void msm_pm_swfi(void)
403{
404 msm_pm_config_hw_before_swfi();
405 msm_arch_idle();
406}
407
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600408
409static void msm_pm_retention(void)
410{
411 int ret = 0;
412
413 msm_pm_config_hw_before_swfi();
414 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_POWER_RETENTION, false);
415 WARN_ON(ret);
416 msm_arch_idle();
417 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
418 WARN_ON(ret);
419}
420
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600421#ifdef CONFIG_CACHE_L2X0
422static inline bool msm_pm_l2x0_power_collapse(void)
423{
424 bool collapsed = 0;
425
Taniya Das38a8c6e2012-05-09 20:34:39 +0530426 l2cc_suspend();
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600427 collapsed = msm_pm_collapse();
Taniya Das38a8c6e2012-05-09 20:34:39 +0530428 l2cc_resume();
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600429
430 return collapsed;
431}
432#else
433static inline bool msm_pm_l2x0_power_collapse(void)
434{
435 return msm_pm_collapse();
436}
437#endif
438
Stephen Boydb29750d2012-02-21 01:21:32 -0800439static bool __ref msm_pm_spm_power_collapse(
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700440 unsigned int cpu, bool from_idle, bool notify_rpm)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700441{
442 void *entry;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600443 bool collapsed = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700444 int ret;
Rohit Vaswanie78dfb62012-02-21 10:29:29 -0800445 unsigned int saved_gic_cpu_ctrl;
446
447 saved_gic_cpu_ctrl = readl_relaxed(MSM_QGIC_CPU_BASE + GIC_CPU_CTRL);
448 mb();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700449
450 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
451 pr_info("CPU%u: %s: notify_rpm %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700452 cpu, __func__, (int) notify_rpm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700453
454 ret = msm_spm_set_low_power_mode(
455 MSM_SPM_MODE_POWER_COLLAPSE, notify_rpm);
456 WARN_ON(ret);
457
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700458 entry = (!cpu || from_idle) ?
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700459 msm_pm_collapse_exit : msm_secondary_startup;
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700460 msm_pm_boot_config_before_pc(cpu, virt_to_phys(entry));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700461
462 if (MSM_PM_DEBUG_RESET_VECTOR & msm_pm_debug_mask)
463 pr_info("CPU%u: %s: program vector to %p\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700464 cpu, __func__, entry);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700465
466#ifdef CONFIG_VFP
Steve Mucklef132c6c2012-06-06 18:30:57 -0700467 vfp_pm_suspend();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700468#endif
469
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600470 collapsed = msm_pm_l2x0_power_collapse();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700471
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700472 msm_pm_boot_config_after_pc(cpu);
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -0600473
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700474 if (collapsed) {
475#ifdef CONFIG_VFP
Steve Mucklef132c6c2012-06-06 18:30:57 -0700476 vfp_pm_resume();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700477#endif
478 cpu_init();
479 writel(0xF0, MSM_QGIC_CPU_BASE + GIC_CPU_PRIMASK);
Rohit Vaswanie78dfb62012-02-21 10:29:29 -0800480 writel_relaxed(saved_gic_cpu_ctrl,
481 MSM_QGIC_CPU_BASE + GIC_CPU_CTRL);
482 mb();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700483 local_fiq_enable();
484 }
485
486 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
487 pr_info("CPU%u: %s: msm_pm_collapse returned, collapsed %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700488 cpu, __func__, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700489
490 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
491 WARN_ON(ret);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600492 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700493}
494
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600495static bool msm_pm_power_collapse_standalone(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700496{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700497 unsigned int cpu = smp_processor_id();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700498 unsigned int avsdscr_setting;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600499 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700500
501 avsdscr_setting = avs_get_avsdscr();
502 avs_disable();
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700503 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700504 avs_reset_delays(avsdscr_setting);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600505 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700506}
507
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600508static bool msm_pm_power_collapse(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700509{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700510 unsigned int cpu = smp_processor_id();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700511 unsigned long saved_acpuclk_rate;
512 unsigned int avsdscr_setting;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600513 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700514
515 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
516 pr_info("CPU%u: %s: idle %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700517 cpu, __func__, (int)from_idle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700518
519 msm_pm_config_hw_before_power_down();
520 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700521 pr_info("CPU%u: %s: pre power down\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700522
523 avsdscr_setting = avs_get_avsdscr();
524 avs_disable();
525
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700526 if (cpu_online(cpu))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700527 saved_acpuclk_rate = acpuclk_power_collapse();
528 else
529 saved_acpuclk_rate = 0;
530
531 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
532 pr_info("CPU%u: %s: change clock rate (old rate = %lu)\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700533 cpu, __func__, saved_acpuclk_rate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700534
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700535 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700536
Girish Mahadevan4e025d92012-02-29 13:26:51 -0700537 if (cpu_online(cpu)) {
538 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
539 pr_info("CPU%u: %s: restore clock rate to %lu\n",
540 cpu, __func__, saved_acpuclk_rate);
541 if (acpuclk_set_rate(cpu, saved_acpuclk_rate, SETRATE_PC) < 0)
542 pr_err("CPU%u: %s: failed to restore clock rate(%lu)\n",
543 cpu, __func__, saved_acpuclk_rate);
544 } else {
545 unsigned int gic_dist_enabled;
546 unsigned int gic_dist_pending;
547 gic_dist_enabled = readl_relaxed(
548 MSM_QGIC_DIST_BASE + GIC_DIST_ENABLE_CLEAR);
549 gic_dist_pending = readl_relaxed(
550 MSM_QGIC_DIST_BASE + GIC_DIST_PENDING_SET);
551 mb();
552 gic_dist_pending &= gic_dist_enabled;
553
554 if (gic_dist_pending)
555 pr_err("CPU %d interrupted during hotplug.Pending int 0x%x\n",
556 cpu, gic_dist_pending);
557 }
558
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700559
560 avs_reset_delays(avsdscr_setting);
561 msm_pm_config_hw_after_power_up();
562 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700563 pr_info("CPU%u: %s: post power up\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700564
565 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700566 pr_info("CPU%u: %s: return\n", cpu, __func__);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600567 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700568}
569
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600570static void msm_pm_qtimer_available(void)
571{
572 if (machine_is_copper())
573 msm_pm_use_qtimer = true;
574}
575
576static int64_t msm_pm_timer_enter_idle(void)
577{
578 if (msm_pm_use_qtimer)
579 return ktime_to_ns(tick_nohz_get_sleep_length());
580
581 return msm_timer_enter_idle();
582}
583
584static void msm_pm_timer_exit_idle(bool timer_halted)
585{
586 if (msm_pm_use_qtimer)
587 return;
588
589 msm_timer_exit_idle((int) timer_halted);
590}
591
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530592static int64_t msm_pm_timer_enter_suspend(int64_t *period)
593{
594 int time = 0;
595
596 if (msm_pm_use_qtimer)
597 return sched_clock();
598
599 time = msm_timer_get_sclk_time(period);
600 if (!time)
601 pr_err("%s: Unable to read sclk.\n", __func__);
602
603 return time;
604}
605
606static int64_t msm_pm_timer_exit_suspend(int64_t time, int64_t period)
607{
608 if (msm_pm_use_qtimer)
609 return sched_clock() - time;
610
611 if (time != 0) {
612 int64_t end_time = msm_timer_get_sclk_time(NULL);
613 if (end_time != 0) {
614 time = end_time - time;
615 if (time < 0)
616 time += period;
617 } else
618 time = 0;
619 }
620
621 return time;
622}
623
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700624/******************************************************************************
625 * External Idle/Suspend Functions
626 *****************************************************************************/
627
628void arch_idle(void)
629{
630 return;
631}
632
Steve Mucklef132c6c2012-06-06 18:30:57 -0700633int msm_pm_idle_prepare(struct cpuidle_device *dev,
634 struct cpuidle_driver *drv, int index)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700635{
636 uint32_t latency_us;
637 uint32_t sleep_us;
638 int i;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700639 unsigned int power_usage = -1;
640 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700641
642 latency_us = (uint32_t) pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
643 sleep_us = (uint32_t) ktime_to_ns(tick_nohz_get_sleep_length());
644 sleep_us = DIV_ROUND_UP(sleep_us, 1000);
645
646 for (i = 0; i < dev->state_count; i++) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700647 struct cpuidle_state *state = &drv->states[i];
648 struct cpuidle_state_usage *st_usage = &dev->states_usage[i];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700649 enum msm_pm_sleep_mode mode;
650 bool allow;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600651 void *rs_limits = NULL;
652 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700653 int idx;
654
Steve Mucklef132c6c2012-06-06 18:30:57 -0700655 mode = (enum msm_pm_sleep_mode) cpuidle_get_statedata(st_usage);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700656 idx = MSM_PM_MODE(dev->cpu, mode);
657
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600658 allow = msm_pm_sleep_modes[idx].idle_enabled &&
659 msm_pm_sleep_modes[idx].idle_supported;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700660
661 switch (mode) {
662 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
663 if (!allow)
664 break;
665
666 if (num_online_cpus() > 1) {
667 allow = false;
668 break;
669 }
670#ifdef CONFIG_HAS_WAKELOCK
671 if (has_wake_lock(WAKE_LOCK_IDLE)) {
672 allow = false;
673 break;
674 }
675#endif
676 /* fall through */
677
678 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
679 if (!allow)
680 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700681 /* fall through */
682
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600683 case MSM_PM_SLEEP_MODE_RETENTION:
684 if (!allow)
685 break;
686 /* fall through */
687
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700688 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
689 if (!allow)
690 break;
691
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600692 if (pm_sleep_ops.lowest_limits)
693 rs_limits = pm_sleep_ops.lowest_limits(true,
694 mode, latency_us, sleep_us,
695 &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700696
697 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
698 pr_info("CPU%u: %s: %s, latency %uus, "
699 "sleep %uus, limit %p\n",
700 dev->cpu, __func__, state->desc,
701 latency_us, sleep_us, rs_limits);
702
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700703 if (!rs_limits)
704 allow = false;
705 break;
706
707 default:
708 allow = false;
709 break;
710 }
711
712 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
713 pr_info("CPU%u: %s: allow %s: %d\n",
714 dev->cpu, __func__, state->desc, (int)allow);
715
716 if (allow) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700717 if (power < power_usage) {
718 power_usage = power;
719 ret = mode;
720 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700721
722 if (MSM_PM_SLEEP_MODE_POWER_COLLAPSE == mode)
723 msm_pm_idle_rs_limits = rs_limits;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700724 }
725 }
726
Steve Mucklef132c6c2012-06-06 18:30:57 -0700727 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700728}
729
730int msm_pm_idle_enter(enum msm_pm_sleep_mode sleep_mode)
731{
732 int64_t time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700733 int exit_stat;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700734
735 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
736 pr_info("CPU%u: %s: mode %d\n",
737 smp_processor_id(), __func__, sleep_mode);
738
739 time = ktime_to_ns(ktime_get());
740
741 switch (sleep_mode) {
742 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
743 msm_pm_swfi();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700744 exit_stat = MSM_PM_STAT_IDLE_WFI;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700745 break;
746
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600747 case MSM_PM_SLEEP_MODE_RETENTION:
748 msm_pm_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600749 exit_stat = MSM_PM_STAT_RETENTION;
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600750 break;
751
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700752 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
753 msm_pm_power_collapse_standalone(true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700754 exit_stat = MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700755 break;
756
757 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE: {
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600758 int64_t timer_expiration = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700759 bool timer_halted = false;
760 uint32_t sleep_delay;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600761 int ret = -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700762 int notify_rpm =
763 (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600764 int collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700765
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600766 timer_expiration = msm_pm_timer_enter_idle();
767
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700768 sleep_delay = (uint32_t) msm_pm_convert_and_cap_time(
769 timer_expiration, MSM_PM_SLEEP_TICK_LIMIT);
770 if (sleep_delay == 0) /* 0 would mean infinite time */
771 sleep_delay = 1;
772
Karthik Parsha6fb932d2012-01-24 18:04:12 -0800773 if (MSM_PM_DEBUG_IDLE_CLK & msm_pm_debug_mask)
774 clock_debug_print_enabled();
775
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600776 if (pm_sleep_ops.enter_sleep)
777 ret = pm_sleep_ops.enter_sleep(sleep_delay,
778 msm_pm_idle_rs_limits,
779 true, notify_rpm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700780 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600781 collapsed = msm_pm_power_collapse(true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700782 timer_halted = true;
783
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600784 if (pm_sleep_ops.exit_sleep)
785 pm_sleep_ops.exit_sleep(msm_pm_idle_rs_limits,
786 true, notify_rpm, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700787 }
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600788 msm_pm_timer_exit_idle(timer_halted);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700789 exit_stat = MSM_PM_STAT_IDLE_POWER_COLLAPSE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700790 break;
791 }
792
793 default:
794 __WARN();
795 goto cpuidle_enter_bail;
796 }
797
798 time = ktime_to_ns(ktime_get()) - time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700799 msm_pm_add_stat(exit_stat, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700800
801 do_div(time, 1000);
802 return (int) time;
803
804cpuidle_enter_bail:
805 return 0;
806}
807
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -0600808static struct msm_pm_sleep_status_data *msm_pm_slp_sts;
809
810static DEFINE_PER_CPU_SHARED_ALIGNED(enum msm_pm_sleep_mode,
811 msm_pm_last_slp_mode);
812
813bool msm_pm_verify_cpu_pc(unsigned int cpu)
814{
815 enum msm_pm_sleep_mode mode = per_cpu(msm_pm_last_slp_mode, cpu);
816
817 if (msm_pm_slp_sts)
818 if ((mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE) ||
819 (mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE))
820 return true;
821
822 return false;
823}
824
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700825void msm_pm_cpu_enter_lowpower(unsigned int cpu)
826{
827 int i;
828 bool allow[MSM_PM_SLEEP_MODE_NR];
829
830 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
831 struct msm_pm_platform_data *mode;
832
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600833 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700834 allow[i] = mode->suspend_supported && mode->suspend_enabled;
835 }
836
837 if (MSM_PM_DEBUG_HOTPLUG & msm_pm_debug_mask)
838 pr_notice("CPU%u: %s: shutting down cpu\n", cpu, __func__);
839
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -0600840 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) {
841 per_cpu(msm_pm_last_slp_mode, cpu)
842 = MSM_PM_SLEEP_MODE_POWER_COLLAPSE;
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700843 msm_pm_power_collapse(false);
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -0600844 } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
845 per_cpu(msm_pm_last_slp_mode, cpu)
846 = MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE;
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700847 msm_pm_power_collapse_standalone(false);
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600848 } else if (allow[MSM_PM_SLEEP_MODE_RETENTION]) {
849 per_cpu(msm_pm_last_slp_mode, cpu)
850 = MSM_PM_SLEEP_MODE_RETENTION;
851 msm_pm_retention();
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -0600852 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
853 per_cpu(msm_pm_last_slp_mode, cpu)
Girish Mahadevanc8127052012-04-25 16:23:24 -0600854 = MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT;
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700855 msm_pm_swfi();
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -0600856 } else
857 per_cpu(msm_pm_last_slp_mode, cpu) = MSM_PM_SLEEP_MODE_NR;
858}
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700859
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -0600860int msm_pm_wait_cpu_shutdown(unsigned int cpu)
861{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700862
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -0600863 int timeout = 10;
864
865 if (!msm_pm_slp_sts)
866 return 0;
867
868 while (timeout--) {
869
870 /*
871 * Check for the SPM of the core being hotplugged to set
872 * its sleep state.The SPM sleep state indicates that the
873 * core has been power collapsed.
874 */
875
876 int acc_sts = __raw_readl(msm_pm_slp_sts->base_addr
877 + cpu * msm_pm_slp_sts->cpu_offset);
878 mb();
879
880 if (acc_sts & msm_pm_slp_sts->mask)
881 return 0;
882
883 usleep(100);
884 }
885 pr_warn("%s(): Timed out waiting for CPU %u SPM to enter sleep state",
886 __func__, cpu);
887 return -EBUSY;
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700888}
889
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700890static int msm_pm_enter(suspend_state_t state)
891{
892 bool allow[MSM_PM_SLEEP_MODE_NR];
893 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700894 int64_t period = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530895 int64_t time = msm_pm_timer_enter_suspend(&period);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700896
897 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
898 pr_info("%s\n", __func__);
899
900 if (smp_processor_id()) {
901 __WARN();
902 goto enter_exit;
903 }
904
905
906 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
907 struct msm_pm_platform_data *mode;
908
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600909 mode = &msm_pm_sleep_modes[MSM_PM_MODE(0, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700910 allow[i] = mode->suspend_supported && mode->suspend_enabled;
911 }
912
913 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600914 void *rs_limits = NULL;
915 int ret = -ENODEV;
916 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700917
918 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
919 pr_info("%s: power collapse\n", __func__);
920
921 clock_debug_print_enabled();
922
923#ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE
924 if (msm_pm_sleep_time_override > 0) {
925 int64_t ns = NSEC_PER_SEC *
926 (int64_t) msm_pm_sleep_time_override;
927 msm_pm_set_max_sleep_time(ns);
928 msm_pm_sleep_time_override = 0;
929 }
930#endif /* CONFIG_MSM_SLEEP_TIME_OVERRIDE */
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600931 if (pm_sleep_ops.lowest_limits)
932 rs_limits = pm_sleep_ops.lowest_limits(false,
933 MSM_PM_SLEEP_MODE_POWER_COLLAPSE, -1,
934 -1, &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700935
936 if (rs_limits) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600937 if (pm_sleep_ops.enter_sleep)
938 ret = pm_sleep_ops.enter_sleep(
939 msm_pm_max_sleep_time,
940 rs_limits, false, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700941 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600942 int collapsed = msm_pm_power_collapse(false);
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600943 if (pm_sleep_ops.exit_sleep) {
944 pm_sleep_ops.exit_sleep(rs_limits,
945 false, true, collapsed);
946 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700947 }
948 } else {
949 pr_err("%s: cannot find the lowest power limit\n",
950 __func__);
951 }
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530952 time = msm_pm_timer_exit_suspend(time, period);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700953 msm_pm_add_stat(MSM_PM_STAT_SUSPEND, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700954 } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
955 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
956 pr_info("%s: standalone power collapse\n", __func__);
957 msm_pm_power_collapse_standalone(false);
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600958 } else if (allow[MSM_PM_SLEEP_MODE_RETENTION]) {
959 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
960 pr_info("%s: retention\n", __func__);
961 msm_pm_retention();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700962 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
963 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
964 pr_info("%s: swfi\n", __func__);
965 msm_pm_swfi();
966 }
967
968
969enter_exit:
970 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
971 pr_info("%s: return\n", __func__);
972
973 return 0;
974}
975
976static struct platform_suspend_ops msm_pm_ops = {
977 .enter = msm_pm_enter,
978 .valid = suspend_valid_only_mem,
979};
980
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700981/******************************************************************************
982 * Initialization routine
983 *****************************************************************************/
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -0600984void __init msm_pm_init_sleep_status_data(
985 struct msm_pm_sleep_status_data *data)
986{
987 msm_pm_slp_sts = data;
988}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700989
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600990void msm_pm_set_sleep_ops(struct msm_pm_sleep_ops *ops)
991{
992 if (ops)
993 pm_sleep_ops = *ops;
994}
995
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700996static int __init msm_pm_init(void)
997{
998 pgd_t *pc_pgd;
999 pmd_t *pmd;
1000 unsigned long pmdval;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301001 enum msm_pm_time_stats_id enable_stats[] = {
1002 MSM_PM_STAT_IDLE_WFI,
1003 MSM_PM_STAT_RETENTION,
1004 MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE,
1005 MSM_PM_STAT_IDLE_POWER_COLLAPSE,
1006 MSM_PM_STAT_SUSPEND,
1007 };
Steve Mucklef132c6c2012-06-06 18:30:57 -07001008 unsigned long exit_phys;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301009
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001010 /* Page table for cores to come back up safely. */
1011 pc_pgd = pgd_alloc(&init_mm);
1012 if (!pc_pgd)
1013 return -ENOMEM;
1014
Steve Mucklef132c6c2012-06-06 18:30:57 -07001015 exit_phys = virt_to_phys(msm_pm_collapse_exit);
1016
1017 pmd = pmd_offset(pud_offset(pc_pgd + pgd_index(exit_phys),exit_phys),
1018 exit_phys);
1019 pmdval = (exit_phys & PGDIR_MASK) |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001020 PMD_TYPE_SECT | PMD_SECT_AP_WRITE;
1021 pmd[0] = __pmd(pmdval);
1022 pmd[1] = __pmd(pmdval + (1 << (PGDIR_SHIFT - 1)));
1023
Steve Mucklefcece052012-02-18 20:09:58 -08001024 msm_saved_state_phys =
1025 allocate_contiguous_ebi_nomap(CPU_SAVED_STATE_SIZE *
1026 num_possible_cpus(), 4);
1027 if (!msm_saved_state_phys)
1028 return -ENOMEM;
1029 msm_saved_state = ioremap_nocache(msm_saved_state_phys,
1030 CPU_SAVED_STATE_SIZE *
1031 num_possible_cpus());
1032 if (!msm_saved_state)
1033 return -ENOMEM;
1034
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001035 /* It is remotely possible that the code in msm_pm_collapse_exit()
1036 * which turns on the MMU with this mapping is in the
1037 * next even-numbered megabyte beyond the
1038 * start of msm_pm_collapse_exit().
1039 * Map this megabyte in as well.
1040 */
1041 pmd[2] = __pmd(pmdval + (2 << (PGDIR_SHIFT - 1)));
1042 flush_pmd_entry(pmd);
1043 msm_pm_pc_pgd = virt_to_phys(pc_pgd);
Steve Muckle730ad7a2012-02-21 15:26:37 -08001044 clean_caches((unsigned long)&msm_pm_pc_pgd, sizeof(msm_pm_pc_pgd),
1045 virt_to_phys(&msm_pm_pc_pgd));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001046
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001047 msm_pm_mode_sysfs_add();
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301048 msm_pm_add_stats(enable_stats, ARRAY_SIZE(enable_stats));
1049
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001050 msm_spm_allow_x_cpu_set_vdd(false);
1051
1052 suspend_set_ops(&msm_pm_ops);
Praveen Chidambaram192979f2012-04-25 18:30:23 -06001053 msm_pm_qtimer_available();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001054 msm_cpuidle_init();
1055
1056 return 0;
1057}
1058
1059late_initcall(msm_pm_init);