blob: 6fb49016f89c1815e995b6e681a6af0ab3c4353d [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 <mach/msm_iomap.h>
Praveen Chidambaram192979f2012-04-25 18:30:23 -060028#include <mach/socinfo.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070029#include <mach/system.h>
Girish Mahadevan63b87262012-08-15 09:21:23 -060030#include <mach/scm.h>
31#include <mach/socinfo.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070032#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"
Jaeseong GIMfc880da2012-08-13 18:47:46 -070047#include "rpm_resources.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070048#include "scm-boot.h"
49#include "spm.h"
50#include "timer.h"
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -060051#include "pm-boot.h"
Girish Mahadevan12aaabe2012-08-17 16:48:05 -060052#include <mach/event_timer.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070053
54/******************************************************************************
55 * Debug Definitions
56 *****************************************************************************/
57
58enum {
59 MSM_PM_DEBUG_SUSPEND = BIT(0),
60 MSM_PM_DEBUG_POWER_COLLAPSE = BIT(1),
61 MSM_PM_DEBUG_SUSPEND_LIMITS = BIT(2),
62 MSM_PM_DEBUG_CLOCK = BIT(3),
63 MSM_PM_DEBUG_RESET_VECTOR = BIT(4),
Karthik Parsha6fb932d2012-01-24 18:04:12 -080064 MSM_PM_DEBUG_IDLE_CLK = BIT(5),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070065 MSM_PM_DEBUG_IDLE = BIT(6),
66 MSM_PM_DEBUG_IDLE_LIMITS = BIT(7),
67 MSM_PM_DEBUG_HOTPLUG = BIT(8),
68};
69
70static int msm_pm_debug_mask = 1;
71module_param_named(
72 debug_mask, msm_pm_debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP
73);
Girish Mahadevan63b87262012-08-15 09:21:23 -060074static int msm_pm_retention_tz_call;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070075
76
77/******************************************************************************
78 * Sleep Modes and Parameters
79 *****************************************************************************/
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070080enum {
81 MSM_PM_MODE_ATTR_SUSPEND,
82 MSM_PM_MODE_ATTR_IDLE,
83 MSM_PM_MODE_ATTR_NR,
84};
85
Girish Mahadevan63b87262012-08-15 09:21:23 -060086#define SCM_L2_RETENTION (0x2)
87#define SCM_CMD_TERMINATE_PC (0x2)
88
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070089static char *msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_NR] = {
90 [MSM_PM_MODE_ATTR_SUSPEND] = "suspend_enabled",
91 [MSM_PM_MODE_ATTR_IDLE] = "idle_enabled",
92};
93
94struct msm_pm_kobj_attribute {
95 unsigned int cpu;
96 struct kobj_attribute ka;
97};
98
99#define GET_CPU_OF_ATTR(attr) \
100 (container_of(attr, struct msm_pm_kobj_attribute, ka)->cpu)
101
102struct msm_pm_sysfs_sleep_mode {
103 struct kobject *kobj;
104 struct attribute_group attr_group;
105 struct attribute *attrs[MSM_PM_MODE_ATTR_NR + 1];
106 struct msm_pm_kobj_attribute kas[MSM_PM_MODE_ATTR_NR];
107};
108
109static char *msm_pm_sleep_mode_labels[MSM_PM_SLEEP_MODE_NR] = {
110 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE] = "power_collapse",
111 [MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT] = "wfi",
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600112 [MSM_PM_SLEEP_MODE_RETENTION] = "retention",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700113 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE] =
114 "standalone_power_collapse",
115};
116
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600117static struct hrtimer pm_hrtimer;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600118static struct msm_pm_sleep_ops pm_sleep_ops;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700119/*
120 * Write out the attribute.
121 */
122static ssize_t msm_pm_mode_attr_show(
123 struct kobject *kobj, struct kobj_attribute *attr, char *buf)
124{
125 int ret = -EINVAL;
126 int i;
127
128 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
129 struct kernel_param kp;
130 unsigned int cpu;
131 struct msm_pm_platform_data *mode;
132
133 if (msm_pm_sleep_mode_labels[i] == NULL)
134 continue;
135
136 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
137 continue;
138
139 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600140 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700141
142 if (!strcmp(attr->attr.name,
143 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
144 u32 arg = mode->suspend_enabled;
145 kp.arg = &arg;
146 ret = param_get_ulong(buf, &kp);
147 } else if (!strcmp(attr->attr.name,
148 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
149 u32 arg = mode->idle_enabled;
150 kp.arg = &arg;
151 ret = param_get_ulong(buf, &kp);
152 }
153
154 break;
155 }
156
157 if (ret > 0) {
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600158 strlcat(buf, "\n", PAGE_SIZE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700159 ret++;
160 }
161
162 return ret;
163}
164
165/*
166 * Read in the new attribute value.
167 */
168static ssize_t msm_pm_mode_attr_store(struct kobject *kobj,
169 struct kobj_attribute *attr, const char *buf, size_t count)
170{
171 int ret = -EINVAL;
172 int i;
173
174 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
175 struct kernel_param kp;
176 unsigned int cpu;
177 struct msm_pm_platform_data *mode;
178
179 if (msm_pm_sleep_mode_labels[i] == NULL)
180 continue;
181
182 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
183 continue;
184
185 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600186 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700187
188 if (!strcmp(attr->attr.name,
189 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
190 kp.arg = &mode->suspend_enabled;
191 ret = param_set_byte(buf, &kp);
192 } else if (!strcmp(attr->attr.name,
193 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
194 kp.arg = &mode->idle_enabled;
195 ret = param_set_byte(buf, &kp);
196 }
197
198 break;
199 }
200
201 return ret ? ret : count;
202}
203
204/*
205 * Add sysfs entries for one cpu.
206 */
207static int __init msm_pm_mode_sysfs_add_cpu(
208 unsigned int cpu, struct kobject *modes_kobj)
209{
210 char cpu_name[8];
211 struct kobject *cpu_kobj;
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600212 struct msm_pm_sysfs_sleep_mode *mode = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700213 int i, j, k;
214 int ret;
215
216 snprintf(cpu_name, sizeof(cpu_name), "cpu%u", cpu);
217 cpu_kobj = kobject_create_and_add(cpu_name, modes_kobj);
218 if (!cpu_kobj) {
219 pr_err("%s: cannot create %s kobject\n", __func__, cpu_name);
220 ret = -ENOMEM;
221 goto mode_sysfs_add_cpu_exit;
222 }
223
224 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
225 int idx = MSM_PM_MODE(cpu, i);
226
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600227 if ((!msm_pm_sleep_modes[idx].suspend_supported)
228 && (!msm_pm_sleep_modes[idx].idle_supported))
229 continue;
230
231 if (!msm_pm_sleep_mode_labels[i] ||
232 !msm_pm_sleep_mode_labels[i][0])
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700233 continue;
234
235 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
236 if (!mode) {
237 pr_err("%s: cannot allocate memory for attributes\n",
238 __func__);
239 ret = -ENOMEM;
240 goto mode_sysfs_add_cpu_exit;
241 }
242
243 mode->kobj = kobject_create_and_add(
244 msm_pm_sleep_mode_labels[i], cpu_kobj);
245 if (!mode->kobj) {
246 pr_err("%s: cannot create kobject\n", __func__);
247 ret = -ENOMEM;
248 goto mode_sysfs_add_cpu_exit;
249 }
250
251 for (k = 0, j = 0; k < MSM_PM_MODE_ATTR_NR; k++) {
252 if ((k == MSM_PM_MODE_ATTR_IDLE) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600253 !msm_pm_sleep_modes[idx].idle_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700254 continue;
255 if ((k == MSM_PM_MODE_ATTR_SUSPEND) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600256 !msm_pm_sleep_modes[idx].suspend_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700257 continue;
Stephen Boydd2059c32012-07-03 15:11:15 -0700258 sysfs_attr_init(&mode->kas[j].ka.attr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700259 mode->kas[j].cpu = cpu;
260 mode->kas[j].ka.attr.mode = 0644;
261 mode->kas[j].ka.show = msm_pm_mode_attr_show;
262 mode->kas[j].ka.store = msm_pm_mode_attr_store;
263 mode->kas[j].ka.attr.name = msm_pm_mode_attr_labels[k];
264 mode->attrs[j] = &mode->kas[j].ka.attr;
265 j++;
266 }
267 mode->attrs[j] = NULL;
268
269 mode->attr_group.attrs = mode->attrs;
270 ret = sysfs_create_group(mode->kobj, &mode->attr_group);
271 if (ret) {
272 pr_err("%s: cannot create kobject attribute group\n",
273 __func__);
274 goto mode_sysfs_add_cpu_exit;
275 }
276 }
277
278 ret = 0;
279
280mode_sysfs_add_cpu_exit:
Praveen Chidambaramd5ac2d32011-10-24 14:30:27 -0600281 if (ret) {
Praveen Chidambaram2cfda632011-10-11 16:58:09 -0600282 if (mode && mode->kobj)
283 kobject_del(mode->kobj);
284 kfree(mode);
285 }
286
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700287 return ret;
288}
289
290/*
291 * Add sysfs entries for the sleep modes.
292 */
293static int __init msm_pm_mode_sysfs_add(void)
294{
295 struct kobject *module_kobj;
296 struct kobject *modes_kobj;
297 unsigned int cpu;
298 int ret;
299
300 module_kobj = kset_find_obj(module_kset, KBUILD_MODNAME);
301 if (!module_kobj) {
302 pr_err("%s: cannot find kobject for module %s\n",
303 __func__, KBUILD_MODNAME);
304 ret = -ENOENT;
305 goto mode_sysfs_add_exit;
306 }
307
308 modes_kobj = kobject_create_and_add("modes", module_kobj);
309 if (!modes_kobj) {
310 pr_err("%s: cannot create modes kobject\n", __func__);
311 ret = -ENOMEM;
312 goto mode_sysfs_add_exit;
313 }
314
315 for_each_possible_cpu(cpu) {
316 ret = msm_pm_mode_sysfs_add_cpu(cpu, modes_kobj);
317 if (ret)
318 goto mode_sysfs_add_exit;
319 }
320
321 ret = 0;
322
323mode_sysfs_add_exit:
324 return ret;
325}
326
327/******************************************************************************
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700328 * Configure Hardware before/after Low Power Mode
329 *****************************************************************************/
330
331/*
332 * Configure hardware registers in preparation for Apps power down.
333 */
334static void msm_pm_config_hw_before_power_down(void)
335{
336 return;
337}
338
339/*
340 * Clear hardware registers after Apps powers up.
341 */
342static void msm_pm_config_hw_after_power_up(void)
343{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700344}
345
346/*
347 * Configure hardware registers in preparation for SWFI.
348 */
349static void msm_pm_config_hw_before_swfi(void)
350{
351 return;
352}
353
Girish Mahadevan63b87262012-08-15 09:21:23 -0600354/*
355 * Configure/Restore hardware registers in preparation for Retention.
356 */
357
358static void msm_pm_config_hw_after_retention(void)
359{
360 int ret;
361 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
362 WARN_ON(ret);
363}
364
365static void msm_pm_config_hw_before_retention(void)
366{
367 return;
368}
369
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700370
371/******************************************************************************
372 * Suspend Max Sleep Time
373 *****************************************************************************/
374
375#ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE
376static int msm_pm_sleep_time_override;
377module_param_named(sleep_time_override,
378 msm_pm_sleep_time_override, int, S_IRUGO | S_IWUSR | S_IWGRP);
379#endif
380
381#define SCLK_HZ (32768)
382#define MSM_PM_SLEEP_TICK_LIMIT (0x6DDD000)
383
384static uint32_t msm_pm_max_sleep_time;
385
386/*
387 * Convert time from nanoseconds to slow clock ticks, then cap it to the
388 * specified limit
389 */
390static int64_t msm_pm_convert_and_cap_time(int64_t time_ns, int64_t limit)
391{
392 do_div(time_ns, NSEC_PER_SEC / SCLK_HZ);
393 return (time_ns > limit) ? limit : time_ns;
394}
395
396/*
397 * Set the sleep time for suspend. 0 means infinite sleep time.
398 */
399void msm_pm_set_max_sleep_time(int64_t max_sleep_time_ns)
400{
401 if (max_sleep_time_ns == 0) {
402 msm_pm_max_sleep_time = 0;
403 } else {
404 msm_pm_max_sleep_time = (uint32_t)msm_pm_convert_and_cap_time(
405 max_sleep_time_ns, MSM_PM_SLEEP_TICK_LIMIT);
406
407 if (msm_pm_max_sleep_time == 0)
408 msm_pm_max_sleep_time = 1;
409 }
410
411 if (msm_pm_debug_mask & MSM_PM_DEBUG_SUSPEND)
412 pr_info("%s: Requested %lld ns Giving %u sclk ticks\n",
413 __func__, max_sleep_time_ns, msm_pm_max_sleep_time);
414}
415EXPORT_SYMBOL(msm_pm_set_max_sleep_time);
416
417
418/******************************************************************************
419 *
420 *****************************************************************************/
421
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600422static void *msm_pm_idle_rs_limits;
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600423static bool msm_pm_use_qtimer;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700424
425static void msm_pm_swfi(void)
426{
427 msm_pm_config_hw_before_swfi();
428 msm_arch_idle();
429}
430
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600431
432static void msm_pm_retention(void)
433{
434 int ret = 0;
435
Girish Mahadevan63b87262012-08-15 09:21:23 -0600436 msm_pm_config_hw_before_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600437 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_POWER_RETENTION, false);
438 WARN_ON(ret);
Girish Mahadevan63b87262012-08-15 09:21:23 -0600439
440 if (msm_pm_retention_tz_call)
441 scm_call_atomic1(SCM_SVC_BOOT, SCM_CMD_TERMINATE_PC,
442 SCM_L2_RETENTION);
443 else
444 msm_arch_idle();
445
446 msm_pm_config_hw_after_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600447}
448
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600449#ifdef CONFIG_CACHE_L2X0
450static inline bool msm_pm_l2x0_power_collapse(void)
451{
452 bool collapsed = 0;
453
Taniya Das38a8c6e2012-05-09 20:34:39 +0530454 l2cc_suspend();
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600455 collapsed = msm_pm_collapse();
Taniya Das38a8c6e2012-05-09 20:34:39 +0530456 l2cc_resume();
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600457
458 return collapsed;
459}
460#else
461static inline bool msm_pm_l2x0_power_collapse(void)
462{
463 return msm_pm_collapse();
464}
465#endif
466
Stephen Boydb29750d2012-02-21 01:21:32 -0800467static bool __ref msm_pm_spm_power_collapse(
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700468 unsigned int cpu, bool from_idle, bool notify_rpm)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700469{
470 void *entry;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600471 bool collapsed = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700472 int ret;
Rohit Vaswanie78dfb62012-02-21 10:29:29 -0800473 unsigned int saved_gic_cpu_ctrl;
474
475 saved_gic_cpu_ctrl = readl_relaxed(MSM_QGIC_CPU_BASE + GIC_CPU_CTRL);
476 mb();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700477
478 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
479 pr_info("CPU%u: %s: notify_rpm %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700480 cpu, __func__, (int) notify_rpm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700481
482 ret = msm_spm_set_low_power_mode(
483 MSM_SPM_MODE_POWER_COLLAPSE, notify_rpm);
484 WARN_ON(ret);
485
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700486 entry = (!cpu || from_idle) ?
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700487 msm_pm_collapse_exit : msm_secondary_startup;
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700488 msm_pm_boot_config_before_pc(cpu, virt_to_phys(entry));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700489
490 if (MSM_PM_DEBUG_RESET_VECTOR & msm_pm_debug_mask)
491 pr_info("CPU%u: %s: program vector to %p\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700492 cpu, __func__, entry);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700493
494#ifdef CONFIG_VFP
Steve Mucklef132c6c2012-06-06 18:30:57 -0700495 vfp_pm_suspend();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700496#endif
497
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600498 collapsed = msm_pm_l2x0_power_collapse();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700499
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700500 msm_pm_boot_config_after_pc(cpu);
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -0600501
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700502 if (collapsed) {
503#ifdef CONFIG_VFP
Steve Mucklef132c6c2012-06-06 18:30:57 -0700504 vfp_pm_resume();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700505#endif
506 cpu_init();
507 writel(0xF0, MSM_QGIC_CPU_BASE + GIC_CPU_PRIMASK);
Rohit Vaswanie78dfb62012-02-21 10:29:29 -0800508 writel_relaxed(saved_gic_cpu_ctrl,
509 MSM_QGIC_CPU_BASE + GIC_CPU_CTRL);
510 mb();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700511 local_fiq_enable();
512 }
513
514 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
515 pr_info("CPU%u: %s: msm_pm_collapse returned, collapsed %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700516 cpu, __func__, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700517
518 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
519 WARN_ON(ret);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600520 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700521}
522
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600523static bool msm_pm_power_collapse_standalone(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700524{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700525 unsigned int cpu = smp_processor_id();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700526 unsigned int avsdscr_setting;
Mahesh Sivasubramanianc1145072012-10-24 13:53:48 -0600527 unsigned int avscsr_enable;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600528 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700529
530 avsdscr_setting = avs_get_avsdscr();
Mahesh Sivasubramanianc1145072012-10-24 13:53:48 -0600531 avscsr_enable = avs_disable();
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700532 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, false);
Mahesh Sivasubramanianc1145072012-10-24 13:53:48 -0600533 avs_enable(avscsr_enable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700534 avs_reset_delays(avsdscr_setting);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600535 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700536}
537
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600538static bool msm_pm_power_collapse(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700539{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700540 unsigned int cpu = smp_processor_id();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700541 unsigned long saved_acpuclk_rate;
542 unsigned int avsdscr_setting;
Mahesh Sivasubramanianc1145072012-10-24 13:53:48 -0600543 unsigned int avscsr_enable;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600544 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700545
546 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
547 pr_info("CPU%u: %s: idle %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700548 cpu, __func__, (int)from_idle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700549
550 msm_pm_config_hw_before_power_down();
551 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700552 pr_info("CPU%u: %s: pre power down\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700553
554 avsdscr_setting = avs_get_avsdscr();
Mahesh Sivasubramanianc1145072012-10-24 13:53:48 -0600555 avscsr_enable = avs_disable();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700556
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700557 if (cpu_online(cpu))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700558 saved_acpuclk_rate = acpuclk_power_collapse();
559 else
560 saved_acpuclk_rate = 0;
561
562 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
563 pr_info("CPU%u: %s: change clock rate (old rate = %lu)\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700564 cpu, __func__, saved_acpuclk_rate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700565
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700566 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700567
Girish Mahadevan4e025d92012-02-29 13:26:51 -0700568 if (cpu_online(cpu)) {
569 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
570 pr_info("CPU%u: %s: restore clock rate to %lu\n",
571 cpu, __func__, saved_acpuclk_rate);
572 if (acpuclk_set_rate(cpu, saved_acpuclk_rate, SETRATE_PC) < 0)
573 pr_err("CPU%u: %s: failed to restore clock rate(%lu)\n",
574 cpu, __func__, saved_acpuclk_rate);
575 } else {
576 unsigned int gic_dist_enabled;
577 unsigned int gic_dist_pending;
578 gic_dist_enabled = readl_relaxed(
579 MSM_QGIC_DIST_BASE + GIC_DIST_ENABLE_CLEAR);
580 gic_dist_pending = readl_relaxed(
581 MSM_QGIC_DIST_BASE + GIC_DIST_PENDING_SET);
582 mb();
583 gic_dist_pending &= gic_dist_enabled;
584
585 if (gic_dist_pending)
586 pr_err("CPU %d interrupted during hotplug.Pending int 0x%x\n",
587 cpu, gic_dist_pending);
588 }
589
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700590
Mahesh Sivasubramanianc1145072012-10-24 13:53:48 -0600591 avs_enable(avscsr_enable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700592 avs_reset_delays(avsdscr_setting);
593 msm_pm_config_hw_after_power_up();
594 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700595 pr_info("CPU%u: %s: post power up\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700596
597 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700598 pr_info("CPU%u: %s: return\n", cpu, __func__);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600599 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700600}
601
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600602static void msm_pm_qtimer_available(void)
603{
Abhimanyu Kapur90ced6e2012-06-26 17:41:25 -0700604 if (machine_is_msm8974())
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600605 msm_pm_use_qtimer = true;
606}
607
608static int64_t msm_pm_timer_enter_idle(void)
609{
610 if (msm_pm_use_qtimer)
611 return ktime_to_ns(tick_nohz_get_sleep_length());
612
613 return msm_timer_enter_idle();
614}
615
616static void msm_pm_timer_exit_idle(bool timer_halted)
617{
618 if (msm_pm_use_qtimer)
619 return;
620
621 msm_timer_exit_idle((int) timer_halted);
622}
623
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530624static int64_t msm_pm_timer_enter_suspend(int64_t *period)
625{
Anji Jonnalac02367a2012-07-01 02:56:11 +0530626 int64_t time = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530627
628 if (msm_pm_use_qtimer)
629 return sched_clock();
630
631 time = msm_timer_get_sclk_time(period);
632 if (!time)
633 pr_err("%s: Unable to read sclk.\n", __func__);
634
635 return time;
636}
637
638static int64_t msm_pm_timer_exit_suspend(int64_t time, int64_t period)
639{
640 if (msm_pm_use_qtimer)
641 return sched_clock() - time;
642
643 if (time != 0) {
644 int64_t end_time = msm_timer_get_sclk_time(NULL);
645 if (end_time != 0) {
646 time = end_time - time;
647 if (time < 0)
648 time += period;
649 } else
650 time = 0;
651 }
652
653 return time;
654}
655
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600656/**
657 * pm_hrtimer_cb() : Callback function for hrtimer created if the
658 * core needs to be awake to handle an event.
659 * @hrtimer : Pointer to hrtimer
660 */
661static enum hrtimer_restart pm_hrtimer_cb(struct hrtimer *hrtimer)
662{
663 return HRTIMER_NORESTART;
664}
665
666/**
667 * msm_pm_set_timer() : Set an hrtimer to wakeup the core in time
668 * to handle an event.
669 */
670static void msm_pm_set_timer(uint32_t modified_time_us)
671{
672 u64 modified_time_ns = modified_time_us * NSEC_PER_USEC;
673 ktime_t modified_ktime = ns_to_ktime(modified_time_ns);
674 pm_hrtimer.function = pm_hrtimer_cb;
675 hrtimer_start(&pm_hrtimer, modified_ktime, HRTIMER_MODE_ABS);
676}
677
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700678/******************************************************************************
679 * External Idle/Suspend Functions
680 *****************************************************************************/
681
682void arch_idle(void)
683{
684 return;
685}
686
Steve Mucklef132c6c2012-06-06 18:30:57 -0700687int msm_pm_idle_prepare(struct cpuidle_device *dev,
688 struct cpuidle_driver *drv, int index)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700689{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700690 int i;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700691 unsigned int power_usage = -1;
692 int ret = 0;
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600693 uint32_t modified_time_us = 0;
694 struct msm_pm_time_params time_param;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700695
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600696 time_param.latency_us =
697 (uint32_t) pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
698 time_param.sleep_us =
699 (uint32_t) (ktime_to_us(tick_nohz_get_sleep_length())
700 & UINT_MAX);
701 time_param.modified_time_us = 0;
702
703 if (!dev->cpu)
704 time_param.next_event_us =
705 (uint32_t) (ktime_to_us(get_next_event_time())
706 & UINT_MAX);
707 else
708 time_param.next_event_us = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700709
710 for (i = 0; i < dev->state_count; i++) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700711 struct cpuidle_state *state = &drv->states[i];
712 struct cpuidle_state_usage *st_usage = &dev->states_usage[i];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700713 enum msm_pm_sleep_mode mode;
714 bool allow;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600715 void *rs_limits = NULL;
716 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700717 int idx;
718
Steve Mucklef132c6c2012-06-06 18:30:57 -0700719 mode = (enum msm_pm_sleep_mode) cpuidle_get_statedata(st_usage);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700720 idx = MSM_PM_MODE(dev->cpu, mode);
721
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600722 allow = msm_pm_sleep_modes[idx].idle_enabled &&
723 msm_pm_sleep_modes[idx].idle_supported;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700724
725 switch (mode) {
726 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
727 if (!allow)
728 break;
729
730 if (num_online_cpus() > 1) {
731 allow = false;
732 break;
733 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700734 /* fall through */
735
736 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
737 if (!allow)
738 break;
Jaeseong GIMfc880da2012-08-13 18:47:46 -0700739
740 if (!dev->cpu && msm_rpm_local_request_is_outstanding()) {
741 allow = false;
742 break;
743 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700744 /* fall through */
745
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600746 case MSM_PM_SLEEP_MODE_RETENTION:
747 if (!allow)
748 break;
Girish Mahadevan63b87262012-08-15 09:21:23 -0600749 if (num_online_cpus() > 1) {
750 allow = false;
751 break;
752 }
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600753 /* fall through */
754
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700755 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
756 if (!allow)
757 break;
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600758 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700759
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600760 if (pm_sleep_ops.lowest_limits)
761 rs_limits = pm_sleep_ops.lowest_limits(true,
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600762 mode, &time_param, &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700763
764 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
765 pr_info("CPU%u: %s: %s, latency %uus, "
766 "sleep %uus, limit %p\n",
767 dev->cpu, __func__, state->desc,
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600768 time_param.latency_us,
769 time_param.sleep_us, rs_limits);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700770
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700771 if (!rs_limits)
772 allow = false;
773 break;
774
775 default:
776 allow = false;
777 break;
778 }
779
780 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
781 pr_info("CPU%u: %s: allow %s: %d\n",
782 dev->cpu, __func__, state->desc, (int)allow);
783
784 if (allow) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700785 if (power < power_usage) {
786 power_usage = power;
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600787 modified_time_us = time_param.modified_time_us;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700788 ret = mode;
789 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700790
791 if (MSM_PM_SLEEP_MODE_POWER_COLLAPSE == mode)
792 msm_pm_idle_rs_limits = rs_limits;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700793 }
794 }
795
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600796 if (modified_time_us && !dev->cpu)
797 msm_pm_set_timer(modified_time_us);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700798 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700799}
800
801int msm_pm_idle_enter(enum msm_pm_sleep_mode sleep_mode)
802{
803 int64_t time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700804 int exit_stat;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700805
806 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
807 pr_info("CPU%u: %s: mode %d\n",
808 smp_processor_id(), __func__, sleep_mode);
809
810 time = ktime_to_ns(ktime_get());
811
812 switch (sleep_mode) {
813 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
814 msm_pm_swfi();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700815 exit_stat = MSM_PM_STAT_IDLE_WFI;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700816 break;
817
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600818 case MSM_PM_SLEEP_MODE_RETENTION:
819 msm_pm_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600820 exit_stat = MSM_PM_STAT_RETENTION;
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600821 break;
822
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700823 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
824 msm_pm_power_collapse_standalone(true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700825 exit_stat = MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700826 break;
827
828 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE: {
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600829 int64_t timer_expiration = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700830 bool timer_halted = false;
831 uint32_t sleep_delay;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600832 int ret = -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700833 int notify_rpm =
834 (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600835 int collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700836
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600837 timer_expiration = msm_pm_timer_enter_idle();
838
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700839 sleep_delay = (uint32_t) msm_pm_convert_and_cap_time(
840 timer_expiration, MSM_PM_SLEEP_TICK_LIMIT);
841 if (sleep_delay == 0) /* 0 would mean infinite time */
842 sleep_delay = 1;
843
Karthik Parsha6fb932d2012-01-24 18:04:12 -0800844 if (MSM_PM_DEBUG_IDLE_CLK & msm_pm_debug_mask)
845 clock_debug_print_enabled();
846
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600847 if (pm_sleep_ops.enter_sleep)
848 ret = pm_sleep_ops.enter_sleep(sleep_delay,
849 msm_pm_idle_rs_limits,
850 true, notify_rpm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700851 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600852 collapsed = msm_pm_power_collapse(true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700853 timer_halted = true;
854
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600855 if (pm_sleep_ops.exit_sleep)
856 pm_sleep_ops.exit_sleep(msm_pm_idle_rs_limits,
857 true, notify_rpm, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700858 }
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600859 msm_pm_timer_exit_idle(timer_halted);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700860 exit_stat = MSM_PM_STAT_IDLE_POWER_COLLAPSE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700861 break;
862 }
863
864 default:
865 __WARN();
866 goto cpuidle_enter_bail;
867 }
868
869 time = ktime_to_ns(ktime_get()) - time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700870 msm_pm_add_stat(exit_stat, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700871
872 do_div(time, 1000);
873 return (int) time;
874
875cpuidle_enter_bail:
876 return 0;
877}
878
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700879void msm_pm_cpu_enter_lowpower(unsigned int cpu)
880{
881 int i;
882 bool allow[MSM_PM_SLEEP_MODE_NR];
883
884 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
885 struct msm_pm_platform_data *mode;
886
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600887 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700888 allow[i] = mode->suspend_supported && mode->suspend_enabled;
889 }
890
891 if (MSM_PM_DEBUG_HOTPLUG & msm_pm_debug_mask)
892 pr_notice("CPU%u: %s: shutting down cpu\n", cpu, __func__);
893
Matt Wagantall5375aa72012-07-02 19:59:51 -0700894 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700895 msm_pm_power_collapse(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -0700896 else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700897 msm_pm_power_collapse_standalone(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -0700898 else if (allow[MSM_PM_SLEEP_MODE_RETENTION])
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600899 msm_pm_retention();
Stephen Boydbda74272012-08-09 14:01:27 -0700900 else
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700901 msm_pm_swfi();
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700902}
903
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700904static int msm_pm_enter(suspend_state_t state)
905{
906 bool allow[MSM_PM_SLEEP_MODE_NR];
907 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700908 int64_t period = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530909 int64_t time = msm_pm_timer_enter_suspend(&period);
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600910 struct msm_pm_time_params time_param;
911
912 time_param.latency_us = -1;
913 time_param.sleep_us = -1;
914 time_param.next_event_us = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700915
916 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
917 pr_info("%s\n", __func__);
918
919 if (smp_processor_id()) {
920 __WARN();
921 goto enter_exit;
922 }
923
924
925 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
926 struct msm_pm_platform_data *mode;
927
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600928 mode = &msm_pm_sleep_modes[MSM_PM_MODE(0, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700929 allow[i] = mode->suspend_supported && mode->suspend_enabled;
930 }
931
932 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600933 void *rs_limits = NULL;
934 int ret = -ENODEV;
935 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700936
937 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
938 pr_info("%s: power collapse\n", __func__);
939
940 clock_debug_print_enabled();
941
942#ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE
943 if (msm_pm_sleep_time_override > 0) {
944 int64_t ns = NSEC_PER_SEC *
945 (int64_t) msm_pm_sleep_time_override;
946 msm_pm_set_max_sleep_time(ns);
947 msm_pm_sleep_time_override = 0;
948 }
949#endif /* CONFIG_MSM_SLEEP_TIME_OVERRIDE */
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600950 if (pm_sleep_ops.lowest_limits)
951 rs_limits = pm_sleep_ops.lowest_limits(false,
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600952 MSM_PM_SLEEP_MODE_POWER_COLLAPSE, &time_param, &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700953
954 if (rs_limits) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600955 if (pm_sleep_ops.enter_sleep)
956 ret = pm_sleep_ops.enter_sleep(
957 msm_pm_max_sleep_time,
958 rs_limits, false, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700959 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600960 int collapsed = msm_pm_power_collapse(false);
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600961 if (pm_sleep_ops.exit_sleep) {
962 pm_sleep_ops.exit_sleep(rs_limits,
963 false, true, collapsed);
964 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700965 }
966 } else {
967 pr_err("%s: cannot find the lowest power limit\n",
968 __func__);
969 }
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530970 time = msm_pm_timer_exit_suspend(time, period);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700971 msm_pm_add_stat(MSM_PM_STAT_SUSPEND, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700972 } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
973 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
974 pr_info("%s: standalone power collapse\n", __func__);
975 msm_pm_power_collapse_standalone(false);
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600976 } else if (allow[MSM_PM_SLEEP_MODE_RETENTION]) {
977 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
978 pr_info("%s: retention\n", __func__);
979 msm_pm_retention();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700980 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
981 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
982 pr_info("%s: swfi\n", __func__);
983 msm_pm_swfi();
984 }
985
986
987enter_exit:
988 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
989 pr_info("%s: return\n", __func__);
990
991 return 0;
992}
993
994static struct platform_suspend_ops msm_pm_ops = {
995 .enter = msm_pm_enter,
996 .valid = suspend_valid_only_mem,
997};
998
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700999/******************************************************************************
1000 * Initialization routine
1001 *****************************************************************************/
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001002void msm_pm_set_sleep_ops(struct msm_pm_sleep_ops *ops)
1003{
1004 if (ops)
1005 pm_sleep_ops = *ops;
1006}
1007
Girish Mahadevan63b87262012-08-15 09:21:23 -06001008void __init msm_pm_set_tz_retention_flag(unsigned int flag)
1009{
1010 msm_pm_retention_tz_call = flag;
1011}
1012
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001013static int __init msm_pm_init(void)
1014{
1015 pgd_t *pc_pgd;
1016 pmd_t *pmd;
1017 unsigned long pmdval;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301018 enum msm_pm_time_stats_id enable_stats[] = {
1019 MSM_PM_STAT_IDLE_WFI,
1020 MSM_PM_STAT_RETENTION,
1021 MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE,
1022 MSM_PM_STAT_IDLE_POWER_COLLAPSE,
1023 MSM_PM_STAT_SUSPEND,
1024 };
Steve Mucklef132c6c2012-06-06 18:30:57 -07001025 unsigned long exit_phys;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301026
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001027 /* Page table for cores to come back up safely. */
1028 pc_pgd = pgd_alloc(&init_mm);
1029 if (!pc_pgd)
1030 return -ENOMEM;
1031
Steve Mucklef132c6c2012-06-06 18:30:57 -07001032 exit_phys = virt_to_phys(msm_pm_collapse_exit);
1033
1034 pmd = pmd_offset(pud_offset(pc_pgd + pgd_index(exit_phys),exit_phys),
1035 exit_phys);
1036 pmdval = (exit_phys & PGDIR_MASK) |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001037 PMD_TYPE_SECT | PMD_SECT_AP_WRITE;
1038 pmd[0] = __pmd(pmdval);
1039 pmd[1] = __pmd(pmdval + (1 << (PGDIR_SHIFT - 1)));
1040
Steve Mucklefcece052012-02-18 20:09:58 -08001041 msm_saved_state_phys =
1042 allocate_contiguous_ebi_nomap(CPU_SAVED_STATE_SIZE *
1043 num_possible_cpus(), 4);
1044 if (!msm_saved_state_phys)
1045 return -ENOMEM;
1046 msm_saved_state = ioremap_nocache(msm_saved_state_phys,
1047 CPU_SAVED_STATE_SIZE *
1048 num_possible_cpus());
1049 if (!msm_saved_state)
1050 return -ENOMEM;
1051
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001052 /* It is remotely possible that the code in msm_pm_collapse_exit()
1053 * which turns on the MMU with this mapping is in the
1054 * next even-numbered megabyte beyond the
1055 * start of msm_pm_collapse_exit().
1056 * Map this megabyte in as well.
1057 */
1058 pmd[2] = __pmd(pmdval + (2 << (PGDIR_SHIFT - 1)));
1059 flush_pmd_entry(pmd);
1060 msm_pm_pc_pgd = virt_to_phys(pc_pgd);
Steve Muckle730ad7a2012-02-21 15:26:37 -08001061 clean_caches((unsigned long)&msm_pm_pc_pgd, sizeof(msm_pm_pc_pgd),
1062 virt_to_phys(&msm_pm_pc_pgd));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001063
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001064 msm_pm_mode_sysfs_add();
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301065 msm_pm_add_stats(enable_stats, ARRAY_SIZE(enable_stats));
1066
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001067 suspend_set_ops(&msm_pm_ops);
Praveen Chidambaram192979f2012-04-25 18:30:23 -06001068 msm_pm_qtimer_available();
Girish Mahadevan12aaabe2012-08-17 16:48:05 -06001069 hrtimer_init(&pm_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001070 msm_cpuidle_init();
1071
1072 return 0;
1073}
1074
1075late_initcall(msm_pm_init);