blob: 4a8c64b2e2842581ac09907e6f44970d87ccc753 [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"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070052
53/******************************************************************************
54 * Debug Definitions
55 *****************************************************************************/
56
57enum {
58 MSM_PM_DEBUG_SUSPEND = BIT(0),
59 MSM_PM_DEBUG_POWER_COLLAPSE = BIT(1),
60 MSM_PM_DEBUG_SUSPEND_LIMITS = BIT(2),
61 MSM_PM_DEBUG_CLOCK = BIT(3),
62 MSM_PM_DEBUG_RESET_VECTOR = BIT(4),
Karthik Parsha6fb932d2012-01-24 18:04:12 -080063 MSM_PM_DEBUG_IDLE_CLK = BIT(5),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070064 MSM_PM_DEBUG_IDLE = BIT(6),
65 MSM_PM_DEBUG_IDLE_LIMITS = BIT(7),
66 MSM_PM_DEBUG_HOTPLUG = BIT(8),
67};
68
69static int msm_pm_debug_mask = 1;
70module_param_named(
71 debug_mask, msm_pm_debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP
72);
Girish Mahadevan63b87262012-08-15 09:21:23 -060073static int msm_pm_retention_tz_call;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070074
75
76/******************************************************************************
77 * Sleep Modes and Parameters
78 *****************************************************************************/
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070079enum {
80 MSM_PM_MODE_ATTR_SUSPEND,
81 MSM_PM_MODE_ATTR_IDLE,
82 MSM_PM_MODE_ATTR_NR,
83};
84
Girish Mahadevan63b87262012-08-15 09:21:23 -060085#define SCM_L2_RETENTION (0x2)
86#define SCM_CMD_TERMINATE_PC (0x2)
87
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070088static char *msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_NR] = {
89 [MSM_PM_MODE_ATTR_SUSPEND] = "suspend_enabled",
90 [MSM_PM_MODE_ATTR_IDLE] = "idle_enabled",
91};
92
93struct msm_pm_kobj_attribute {
94 unsigned int cpu;
95 struct kobj_attribute ka;
96};
97
98#define GET_CPU_OF_ATTR(attr) \
99 (container_of(attr, struct msm_pm_kobj_attribute, ka)->cpu)
100
101struct msm_pm_sysfs_sleep_mode {
102 struct kobject *kobj;
103 struct attribute_group attr_group;
104 struct attribute *attrs[MSM_PM_MODE_ATTR_NR + 1];
105 struct msm_pm_kobj_attribute kas[MSM_PM_MODE_ATTR_NR];
106};
107
108static char *msm_pm_sleep_mode_labels[MSM_PM_SLEEP_MODE_NR] = {
109 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE] = "power_collapse",
110 [MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT] = "wfi",
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600111 [MSM_PM_SLEEP_MODE_RETENTION] = "retention",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700112 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE] =
113 "standalone_power_collapse",
114};
115
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600116static struct msm_pm_sleep_ops pm_sleep_ops;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700117/*
118 * Write out the attribute.
119 */
120static ssize_t msm_pm_mode_attr_show(
121 struct kobject *kobj, struct kobj_attribute *attr, char *buf)
122{
123 int ret = -EINVAL;
124 int i;
125
126 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
127 struct kernel_param kp;
128 unsigned int cpu;
129 struct msm_pm_platform_data *mode;
130
131 if (msm_pm_sleep_mode_labels[i] == NULL)
132 continue;
133
134 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
135 continue;
136
137 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600138 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700139
140 if (!strcmp(attr->attr.name,
141 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
142 u32 arg = mode->suspend_enabled;
143 kp.arg = &arg;
144 ret = param_get_ulong(buf, &kp);
145 } else if (!strcmp(attr->attr.name,
146 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
147 u32 arg = mode->idle_enabled;
148 kp.arg = &arg;
149 ret = param_get_ulong(buf, &kp);
150 }
151
152 break;
153 }
154
155 if (ret > 0) {
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600156 strlcat(buf, "\n", PAGE_SIZE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700157 ret++;
158 }
159
160 return ret;
161}
162
163/*
164 * Read in the new attribute value.
165 */
166static ssize_t msm_pm_mode_attr_store(struct kobject *kobj,
167 struct kobj_attribute *attr, const char *buf, size_t count)
168{
169 int ret = -EINVAL;
170 int i;
171
172 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
173 struct kernel_param kp;
174 unsigned int cpu;
175 struct msm_pm_platform_data *mode;
176
177 if (msm_pm_sleep_mode_labels[i] == NULL)
178 continue;
179
180 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
181 continue;
182
183 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600184 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700185
186 if (!strcmp(attr->attr.name,
187 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
188 kp.arg = &mode->suspend_enabled;
189 ret = param_set_byte(buf, &kp);
190 } else if (!strcmp(attr->attr.name,
191 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
192 kp.arg = &mode->idle_enabled;
193 ret = param_set_byte(buf, &kp);
194 }
195
196 break;
197 }
198
199 return ret ? ret : count;
200}
201
202/*
203 * Add sysfs entries for one cpu.
204 */
205static int __init msm_pm_mode_sysfs_add_cpu(
206 unsigned int cpu, struct kobject *modes_kobj)
207{
208 char cpu_name[8];
209 struct kobject *cpu_kobj;
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600210 struct msm_pm_sysfs_sleep_mode *mode = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700211 int i, j, k;
212 int ret;
213
214 snprintf(cpu_name, sizeof(cpu_name), "cpu%u", cpu);
215 cpu_kobj = kobject_create_and_add(cpu_name, modes_kobj);
216 if (!cpu_kobj) {
217 pr_err("%s: cannot create %s kobject\n", __func__, cpu_name);
218 ret = -ENOMEM;
219 goto mode_sysfs_add_cpu_exit;
220 }
221
222 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
223 int idx = MSM_PM_MODE(cpu, i);
224
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600225 if ((!msm_pm_sleep_modes[idx].suspend_supported)
226 && (!msm_pm_sleep_modes[idx].idle_supported))
227 continue;
228
229 if (!msm_pm_sleep_mode_labels[i] ||
230 !msm_pm_sleep_mode_labels[i][0])
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700231 continue;
232
233 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
234 if (!mode) {
235 pr_err("%s: cannot allocate memory for attributes\n",
236 __func__);
237 ret = -ENOMEM;
238 goto mode_sysfs_add_cpu_exit;
239 }
240
241 mode->kobj = kobject_create_and_add(
242 msm_pm_sleep_mode_labels[i], cpu_kobj);
243 if (!mode->kobj) {
244 pr_err("%s: cannot create kobject\n", __func__);
245 ret = -ENOMEM;
246 goto mode_sysfs_add_cpu_exit;
247 }
248
249 for (k = 0, j = 0; k < MSM_PM_MODE_ATTR_NR; k++) {
250 if ((k == MSM_PM_MODE_ATTR_IDLE) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600251 !msm_pm_sleep_modes[idx].idle_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700252 continue;
253 if ((k == MSM_PM_MODE_ATTR_SUSPEND) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600254 !msm_pm_sleep_modes[idx].suspend_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700255 continue;
Stephen Boydd2059c32012-07-03 15:11:15 -0700256 sysfs_attr_init(&mode->kas[j].ka.attr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700257 mode->kas[j].cpu = cpu;
258 mode->kas[j].ka.attr.mode = 0644;
259 mode->kas[j].ka.show = msm_pm_mode_attr_show;
260 mode->kas[j].ka.store = msm_pm_mode_attr_store;
261 mode->kas[j].ka.attr.name = msm_pm_mode_attr_labels[k];
262 mode->attrs[j] = &mode->kas[j].ka.attr;
263 j++;
264 }
265 mode->attrs[j] = NULL;
266
267 mode->attr_group.attrs = mode->attrs;
268 ret = sysfs_create_group(mode->kobj, &mode->attr_group);
269 if (ret) {
270 pr_err("%s: cannot create kobject attribute group\n",
271 __func__);
272 goto mode_sysfs_add_cpu_exit;
273 }
274 }
275
276 ret = 0;
277
278mode_sysfs_add_cpu_exit:
Praveen Chidambaramd5ac2d32011-10-24 14:30:27 -0600279 if (ret) {
Praveen Chidambaram2cfda632011-10-11 16:58:09 -0600280 if (mode && mode->kobj)
281 kobject_del(mode->kobj);
282 kfree(mode);
283 }
284
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700285 return ret;
286}
287
288/*
289 * Add sysfs entries for the sleep modes.
290 */
291static int __init msm_pm_mode_sysfs_add(void)
292{
293 struct kobject *module_kobj;
294 struct kobject *modes_kobj;
295 unsigned int cpu;
296 int ret;
297
298 module_kobj = kset_find_obj(module_kset, KBUILD_MODNAME);
299 if (!module_kobj) {
300 pr_err("%s: cannot find kobject for module %s\n",
301 __func__, KBUILD_MODNAME);
302 ret = -ENOENT;
303 goto mode_sysfs_add_exit;
304 }
305
306 modes_kobj = kobject_create_and_add("modes", module_kobj);
307 if (!modes_kobj) {
308 pr_err("%s: cannot create modes kobject\n", __func__);
309 ret = -ENOMEM;
310 goto mode_sysfs_add_exit;
311 }
312
313 for_each_possible_cpu(cpu) {
314 ret = msm_pm_mode_sysfs_add_cpu(cpu, modes_kobj);
315 if (ret)
316 goto mode_sysfs_add_exit;
317 }
318
319 ret = 0;
320
321mode_sysfs_add_exit:
322 return ret;
323}
324
325/******************************************************************************
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700326 * Configure Hardware before/after Low Power Mode
327 *****************************************************************************/
328
329/*
330 * Configure hardware registers in preparation for Apps power down.
331 */
332static void msm_pm_config_hw_before_power_down(void)
333{
334 return;
335}
336
337/*
338 * Clear hardware registers after Apps powers up.
339 */
340static void msm_pm_config_hw_after_power_up(void)
341{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700342}
343
344/*
345 * Configure hardware registers in preparation for SWFI.
346 */
347static void msm_pm_config_hw_before_swfi(void)
348{
349 return;
350}
351
Girish Mahadevan63b87262012-08-15 09:21:23 -0600352/*
353 * Configure/Restore hardware registers in preparation for Retention.
354 */
355
356static void msm_pm_config_hw_after_retention(void)
357{
358 int ret;
359 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
360 WARN_ON(ret);
361}
362
363static void msm_pm_config_hw_before_retention(void)
364{
365 return;
366}
367
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700368
369/******************************************************************************
370 * Suspend Max Sleep Time
371 *****************************************************************************/
372
373#ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE
374static int msm_pm_sleep_time_override;
375module_param_named(sleep_time_override,
376 msm_pm_sleep_time_override, int, S_IRUGO | S_IWUSR | S_IWGRP);
377#endif
378
379#define SCLK_HZ (32768)
380#define MSM_PM_SLEEP_TICK_LIMIT (0x6DDD000)
381
382static uint32_t msm_pm_max_sleep_time;
383
384/*
385 * Convert time from nanoseconds to slow clock ticks, then cap it to the
386 * specified limit
387 */
388static int64_t msm_pm_convert_and_cap_time(int64_t time_ns, int64_t limit)
389{
390 do_div(time_ns, NSEC_PER_SEC / SCLK_HZ);
391 return (time_ns > limit) ? limit : time_ns;
392}
393
394/*
395 * Set the sleep time for suspend. 0 means infinite sleep time.
396 */
397void msm_pm_set_max_sleep_time(int64_t max_sleep_time_ns)
398{
399 if (max_sleep_time_ns == 0) {
400 msm_pm_max_sleep_time = 0;
401 } else {
402 msm_pm_max_sleep_time = (uint32_t)msm_pm_convert_and_cap_time(
403 max_sleep_time_ns, MSM_PM_SLEEP_TICK_LIMIT);
404
405 if (msm_pm_max_sleep_time == 0)
406 msm_pm_max_sleep_time = 1;
407 }
408
409 if (msm_pm_debug_mask & MSM_PM_DEBUG_SUSPEND)
410 pr_info("%s: Requested %lld ns Giving %u sclk ticks\n",
411 __func__, max_sleep_time_ns, msm_pm_max_sleep_time);
412}
413EXPORT_SYMBOL(msm_pm_set_max_sleep_time);
414
415
416/******************************************************************************
417 *
418 *****************************************************************************/
419
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600420static void *msm_pm_idle_rs_limits;
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600421static bool msm_pm_use_qtimer;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700422
423static void msm_pm_swfi(void)
424{
425 msm_pm_config_hw_before_swfi();
426 msm_arch_idle();
427}
428
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600429
430static void msm_pm_retention(void)
431{
432 int ret = 0;
433
Girish Mahadevan63b87262012-08-15 09:21:23 -0600434 msm_pm_config_hw_before_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600435 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_POWER_RETENTION, false);
436 WARN_ON(ret);
Girish Mahadevan63b87262012-08-15 09:21:23 -0600437
438 if (msm_pm_retention_tz_call)
439 scm_call_atomic1(SCM_SVC_BOOT, SCM_CMD_TERMINATE_PC,
440 SCM_L2_RETENTION);
441 else
442 msm_arch_idle();
443
444 msm_pm_config_hw_after_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600445}
446
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600447#ifdef CONFIG_CACHE_L2X0
448static inline bool msm_pm_l2x0_power_collapse(void)
449{
450 bool collapsed = 0;
451
Taniya Das38a8c6e2012-05-09 20:34:39 +0530452 l2cc_suspend();
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600453 collapsed = msm_pm_collapse();
Taniya Das38a8c6e2012-05-09 20:34:39 +0530454 l2cc_resume();
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600455
456 return collapsed;
457}
458#else
459static inline bool msm_pm_l2x0_power_collapse(void)
460{
461 return msm_pm_collapse();
462}
463#endif
464
Stephen Boydb29750d2012-02-21 01:21:32 -0800465static bool __ref msm_pm_spm_power_collapse(
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700466 unsigned int cpu, bool from_idle, bool notify_rpm)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700467{
468 void *entry;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600469 bool collapsed = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700470 int ret;
Rohit Vaswanie78dfb62012-02-21 10:29:29 -0800471 unsigned int saved_gic_cpu_ctrl;
472
473 saved_gic_cpu_ctrl = readl_relaxed(MSM_QGIC_CPU_BASE + GIC_CPU_CTRL);
474 mb();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700475
476 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
477 pr_info("CPU%u: %s: notify_rpm %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700478 cpu, __func__, (int) notify_rpm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700479
480 ret = msm_spm_set_low_power_mode(
481 MSM_SPM_MODE_POWER_COLLAPSE, notify_rpm);
482 WARN_ON(ret);
483
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700484 entry = (!cpu || from_idle) ?
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700485 msm_pm_collapse_exit : msm_secondary_startup;
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700486 msm_pm_boot_config_before_pc(cpu, virt_to_phys(entry));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700487
488 if (MSM_PM_DEBUG_RESET_VECTOR & msm_pm_debug_mask)
489 pr_info("CPU%u: %s: program vector to %p\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700490 cpu, __func__, entry);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700491
492#ifdef CONFIG_VFP
Steve Mucklef132c6c2012-06-06 18:30:57 -0700493 vfp_pm_suspend();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700494#endif
495
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600496 collapsed = msm_pm_l2x0_power_collapse();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700497
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700498 msm_pm_boot_config_after_pc(cpu);
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -0600499
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700500 if (collapsed) {
501#ifdef CONFIG_VFP
Steve Mucklef132c6c2012-06-06 18:30:57 -0700502 vfp_pm_resume();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700503#endif
504 cpu_init();
505 writel(0xF0, MSM_QGIC_CPU_BASE + GIC_CPU_PRIMASK);
Rohit Vaswanie78dfb62012-02-21 10:29:29 -0800506 writel_relaxed(saved_gic_cpu_ctrl,
507 MSM_QGIC_CPU_BASE + GIC_CPU_CTRL);
508 mb();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700509 local_fiq_enable();
510 }
511
512 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
513 pr_info("CPU%u: %s: msm_pm_collapse returned, collapsed %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700514 cpu, __func__, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700515
516 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
517 WARN_ON(ret);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600518 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700519}
520
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600521static bool msm_pm_power_collapse_standalone(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700522{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700523 unsigned int cpu = smp_processor_id();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700524 unsigned int avsdscr_setting;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600525 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700526
527 avsdscr_setting = avs_get_avsdscr();
528 avs_disable();
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700529 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700530 avs_reset_delays(avsdscr_setting);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600531 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700532}
533
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600534static bool msm_pm_power_collapse(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700535{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700536 unsigned int cpu = smp_processor_id();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700537 unsigned long saved_acpuclk_rate;
538 unsigned int avsdscr_setting;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600539 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700540
541 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
542 pr_info("CPU%u: %s: idle %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700543 cpu, __func__, (int)from_idle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700544
545 msm_pm_config_hw_before_power_down();
546 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700547 pr_info("CPU%u: %s: pre power down\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700548
549 avsdscr_setting = avs_get_avsdscr();
550 avs_disable();
551
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700552 if (cpu_online(cpu))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700553 saved_acpuclk_rate = acpuclk_power_collapse();
554 else
555 saved_acpuclk_rate = 0;
556
557 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
558 pr_info("CPU%u: %s: change clock rate (old rate = %lu)\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700559 cpu, __func__, saved_acpuclk_rate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700560
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700561 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700562
Girish Mahadevan4e025d92012-02-29 13:26:51 -0700563 if (cpu_online(cpu)) {
564 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
565 pr_info("CPU%u: %s: restore clock rate to %lu\n",
566 cpu, __func__, saved_acpuclk_rate);
567 if (acpuclk_set_rate(cpu, saved_acpuclk_rate, SETRATE_PC) < 0)
568 pr_err("CPU%u: %s: failed to restore clock rate(%lu)\n",
569 cpu, __func__, saved_acpuclk_rate);
570 } else {
571 unsigned int gic_dist_enabled;
572 unsigned int gic_dist_pending;
573 gic_dist_enabled = readl_relaxed(
574 MSM_QGIC_DIST_BASE + GIC_DIST_ENABLE_CLEAR);
575 gic_dist_pending = readl_relaxed(
576 MSM_QGIC_DIST_BASE + GIC_DIST_PENDING_SET);
577 mb();
578 gic_dist_pending &= gic_dist_enabled;
579
580 if (gic_dist_pending)
581 pr_err("CPU %d interrupted during hotplug.Pending int 0x%x\n",
582 cpu, gic_dist_pending);
583 }
584
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700585
586 avs_reset_delays(avsdscr_setting);
587 msm_pm_config_hw_after_power_up();
588 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700589 pr_info("CPU%u: %s: post power up\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700590
591 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700592 pr_info("CPU%u: %s: return\n", cpu, __func__);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600593 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700594}
595
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600596static void msm_pm_qtimer_available(void)
597{
Abhimanyu Kapur90ced6e2012-06-26 17:41:25 -0700598 if (machine_is_msm8974())
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600599 msm_pm_use_qtimer = true;
600}
601
602static int64_t msm_pm_timer_enter_idle(void)
603{
604 if (msm_pm_use_qtimer)
605 return ktime_to_ns(tick_nohz_get_sleep_length());
606
607 return msm_timer_enter_idle();
608}
609
610static void msm_pm_timer_exit_idle(bool timer_halted)
611{
612 if (msm_pm_use_qtimer)
613 return;
614
615 msm_timer_exit_idle((int) timer_halted);
616}
617
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530618static int64_t msm_pm_timer_enter_suspend(int64_t *period)
619{
Anji Jonnalac02367a2012-07-01 02:56:11 +0530620 int64_t time = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530621
622 if (msm_pm_use_qtimer)
623 return sched_clock();
624
625 time = msm_timer_get_sclk_time(period);
626 if (!time)
627 pr_err("%s: Unable to read sclk.\n", __func__);
628
629 return time;
630}
631
632static int64_t msm_pm_timer_exit_suspend(int64_t time, int64_t period)
633{
634 if (msm_pm_use_qtimer)
635 return sched_clock() - time;
636
637 if (time != 0) {
638 int64_t end_time = msm_timer_get_sclk_time(NULL);
639 if (end_time != 0) {
640 time = end_time - time;
641 if (time < 0)
642 time += period;
643 } else
644 time = 0;
645 }
646
647 return time;
648}
649
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700650/******************************************************************************
651 * External Idle/Suspend Functions
652 *****************************************************************************/
653
654void arch_idle(void)
655{
656 return;
657}
658
Steve Mucklef132c6c2012-06-06 18:30:57 -0700659int msm_pm_idle_prepare(struct cpuidle_device *dev,
660 struct cpuidle_driver *drv, int index)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700661{
662 uint32_t latency_us;
663 uint32_t sleep_us;
664 int i;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700665 unsigned int power_usage = -1;
666 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700667
668 latency_us = (uint32_t) pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
669 sleep_us = (uint32_t) ktime_to_ns(tick_nohz_get_sleep_length());
670 sleep_us = DIV_ROUND_UP(sleep_us, 1000);
671
672 for (i = 0; i < dev->state_count; i++) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700673 struct cpuidle_state *state = &drv->states[i];
674 struct cpuidle_state_usage *st_usage = &dev->states_usage[i];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700675 enum msm_pm_sleep_mode mode;
676 bool allow;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600677 void *rs_limits = NULL;
678 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700679 int idx;
680
Steve Mucklef132c6c2012-06-06 18:30:57 -0700681 mode = (enum msm_pm_sleep_mode) cpuidle_get_statedata(st_usage);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700682 idx = MSM_PM_MODE(dev->cpu, mode);
683
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600684 allow = msm_pm_sleep_modes[idx].idle_enabled &&
685 msm_pm_sleep_modes[idx].idle_supported;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700686
687 switch (mode) {
688 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
689 if (!allow)
690 break;
691
692 if (num_online_cpus() > 1) {
693 allow = false;
694 break;
695 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700696 /* fall through */
697
698 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
699 if (!allow)
700 break;
Jaeseong GIMfc880da2012-08-13 18:47:46 -0700701
702 if (!dev->cpu && msm_rpm_local_request_is_outstanding()) {
703 allow = false;
704 break;
705 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700706 /* fall through */
707
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600708 case MSM_PM_SLEEP_MODE_RETENTION:
709 if (!allow)
710 break;
Girish Mahadevan63b87262012-08-15 09:21:23 -0600711 if (num_online_cpus() > 1) {
712 allow = false;
713 break;
714 }
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600715 /* fall through */
716
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700717 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
718 if (!allow)
719 break;
720
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600721 if (pm_sleep_ops.lowest_limits)
722 rs_limits = pm_sleep_ops.lowest_limits(true,
723 mode, latency_us, sleep_us,
724 &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700725
726 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
727 pr_info("CPU%u: %s: %s, latency %uus, "
728 "sleep %uus, limit %p\n",
729 dev->cpu, __func__, state->desc,
730 latency_us, sleep_us, rs_limits);
731
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700732 if (!rs_limits)
733 allow = false;
734 break;
735
736 default:
737 allow = false;
738 break;
739 }
740
741 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
742 pr_info("CPU%u: %s: allow %s: %d\n",
743 dev->cpu, __func__, state->desc, (int)allow);
744
745 if (allow) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700746 if (power < power_usage) {
747 power_usage = power;
748 ret = mode;
749 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700750
751 if (MSM_PM_SLEEP_MODE_POWER_COLLAPSE == mode)
752 msm_pm_idle_rs_limits = rs_limits;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700753 }
754 }
755
Steve Mucklef132c6c2012-06-06 18:30:57 -0700756 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700757}
758
759int msm_pm_idle_enter(enum msm_pm_sleep_mode sleep_mode)
760{
761 int64_t time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700762 int exit_stat;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700763
764 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
765 pr_info("CPU%u: %s: mode %d\n",
766 smp_processor_id(), __func__, sleep_mode);
767
768 time = ktime_to_ns(ktime_get());
769
770 switch (sleep_mode) {
771 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
772 msm_pm_swfi();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700773 exit_stat = MSM_PM_STAT_IDLE_WFI;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700774 break;
775
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600776 case MSM_PM_SLEEP_MODE_RETENTION:
777 msm_pm_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600778 exit_stat = MSM_PM_STAT_RETENTION;
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600779 break;
780
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700781 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
782 msm_pm_power_collapse_standalone(true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700783 exit_stat = MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700784 break;
785
786 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE: {
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600787 int64_t timer_expiration = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700788 bool timer_halted = false;
789 uint32_t sleep_delay;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600790 int ret = -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700791 int notify_rpm =
792 (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600793 int collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700794
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600795 timer_expiration = msm_pm_timer_enter_idle();
796
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700797 sleep_delay = (uint32_t) msm_pm_convert_and_cap_time(
798 timer_expiration, MSM_PM_SLEEP_TICK_LIMIT);
799 if (sleep_delay == 0) /* 0 would mean infinite time */
800 sleep_delay = 1;
801
Karthik Parsha6fb932d2012-01-24 18:04:12 -0800802 if (MSM_PM_DEBUG_IDLE_CLK & msm_pm_debug_mask)
803 clock_debug_print_enabled();
804
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600805 if (pm_sleep_ops.enter_sleep)
806 ret = pm_sleep_ops.enter_sleep(sleep_delay,
807 msm_pm_idle_rs_limits,
808 true, notify_rpm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700809 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600810 collapsed = msm_pm_power_collapse(true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700811 timer_halted = true;
812
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600813 if (pm_sleep_ops.exit_sleep)
814 pm_sleep_ops.exit_sleep(msm_pm_idle_rs_limits,
815 true, notify_rpm, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700816 }
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600817 msm_pm_timer_exit_idle(timer_halted);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700818 exit_stat = MSM_PM_STAT_IDLE_POWER_COLLAPSE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700819 break;
820 }
821
822 default:
823 __WARN();
824 goto cpuidle_enter_bail;
825 }
826
827 time = ktime_to_ns(ktime_get()) - time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700828 msm_pm_add_stat(exit_stat, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700829
830 do_div(time, 1000);
831 return (int) time;
832
833cpuidle_enter_bail:
834 return 0;
835}
836
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700837void msm_pm_cpu_enter_lowpower(unsigned int cpu)
838{
839 int i;
840 bool allow[MSM_PM_SLEEP_MODE_NR];
841
842 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
843 struct msm_pm_platform_data *mode;
844
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600845 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700846 allow[i] = mode->suspend_supported && mode->suspend_enabled;
847 }
848
849 if (MSM_PM_DEBUG_HOTPLUG & msm_pm_debug_mask)
850 pr_notice("CPU%u: %s: shutting down cpu\n", cpu, __func__);
851
Matt Wagantall5375aa72012-07-02 19:59:51 -0700852 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700853 msm_pm_power_collapse(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -0700854 else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700855 msm_pm_power_collapse_standalone(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -0700856 else if (allow[MSM_PM_SLEEP_MODE_RETENTION])
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600857 msm_pm_retention();
Stephen Boydbda74272012-08-09 14:01:27 -0700858 else
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700859 msm_pm_swfi();
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700860}
861
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700862static int msm_pm_enter(suspend_state_t state)
863{
864 bool allow[MSM_PM_SLEEP_MODE_NR];
865 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700866 int64_t period = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530867 int64_t time = msm_pm_timer_enter_suspend(&period);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700868
869 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
870 pr_info("%s\n", __func__);
871
872 if (smp_processor_id()) {
873 __WARN();
874 goto enter_exit;
875 }
876
877
878 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
879 struct msm_pm_platform_data *mode;
880
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600881 mode = &msm_pm_sleep_modes[MSM_PM_MODE(0, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700882 allow[i] = mode->suspend_supported && mode->suspend_enabled;
883 }
884
885 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600886 void *rs_limits = NULL;
887 int ret = -ENODEV;
888 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700889
890 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
891 pr_info("%s: power collapse\n", __func__);
892
893 clock_debug_print_enabled();
894
895#ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE
896 if (msm_pm_sleep_time_override > 0) {
897 int64_t ns = NSEC_PER_SEC *
898 (int64_t) msm_pm_sleep_time_override;
899 msm_pm_set_max_sleep_time(ns);
900 msm_pm_sleep_time_override = 0;
901 }
902#endif /* CONFIG_MSM_SLEEP_TIME_OVERRIDE */
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600903 if (pm_sleep_ops.lowest_limits)
904 rs_limits = pm_sleep_ops.lowest_limits(false,
905 MSM_PM_SLEEP_MODE_POWER_COLLAPSE, -1,
906 -1, &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700907
908 if (rs_limits) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600909 if (pm_sleep_ops.enter_sleep)
910 ret = pm_sleep_ops.enter_sleep(
911 msm_pm_max_sleep_time,
912 rs_limits, false, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700913 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600914 int collapsed = msm_pm_power_collapse(false);
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600915 if (pm_sleep_ops.exit_sleep) {
916 pm_sleep_ops.exit_sleep(rs_limits,
917 false, true, collapsed);
918 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700919 }
920 } else {
921 pr_err("%s: cannot find the lowest power limit\n",
922 __func__);
923 }
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530924 time = msm_pm_timer_exit_suspend(time, period);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700925 msm_pm_add_stat(MSM_PM_STAT_SUSPEND, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700926 } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
927 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
928 pr_info("%s: standalone power collapse\n", __func__);
929 msm_pm_power_collapse_standalone(false);
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600930 } else if (allow[MSM_PM_SLEEP_MODE_RETENTION]) {
931 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
932 pr_info("%s: retention\n", __func__);
933 msm_pm_retention();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700934 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
935 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
936 pr_info("%s: swfi\n", __func__);
937 msm_pm_swfi();
938 }
939
940
941enter_exit:
942 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
943 pr_info("%s: return\n", __func__);
944
945 return 0;
946}
947
948static struct platform_suspend_ops msm_pm_ops = {
949 .enter = msm_pm_enter,
950 .valid = suspend_valid_only_mem,
951};
952
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700953/******************************************************************************
954 * Initialization routine
955 *****************************************************************************/
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600956void msm_pm_set_sleep_ops(struct msm_pm_sleep_ops *ops)
957{
958 if (ops)
959 pm_sleep_ops = *ops;
960}
961
Girish Mahadevan63b87262012-08-15 09:21:23 -0600962void __init msm_pm_set_tz_retention_flag(unsigned int flag)
963{
964 msm_pm_retention_tz_call = flag;
965}
966
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700967static int __init msm_pm_init(void)
968{
969 pgd_t *pc_pgd;
970 pmd_t *pmd;
971 unsigned long pmdval;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530972 enum msm_pm_time_stats_id enable_stats[] = {
973 MSM_PM_STAT_IDLE_WFI,
974 MSM_PM_STAT_RETENTION,
975 MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE,
976 MSM_PM_STAT_IDLE_POWER_COLLAPSE,
977 MSM_PM_STAT_SUSPEND,
978 };
Steve Mucklef132c6c2012-06-06 18:30:57 -0700979 unsigned long exit_phys;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530980
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700981 /* Page table for cores to come back up safely. */
982 pc_pgd = pgd_alloc(&init_mm);
983 if (!pc_pgd)
984 return -ENOMEM;
985
Steve Mucklef132c6c2012-06-06 18:30:57 -0700986 exit_phys = virt_to_phys(msm_pm_collapse_exit);
987
988 pmd = pmd_offset(pud_offset(pc_pgd + pgd_index(exit_phys),exit_phys),
989 exit_phys);
990 pmdval = (exit_phys & PGDIR_MASK) |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700991 PMD_TYPE_SECT | PMD_SECT_AP_WRITE;
992 pmd[0] = __pmd(pmdval);
993 pmd[1] = __pmd(pmdval + (1 << (PGDIR_SHIFT - 1)));
994
Steve Mucklefcece052012-02-18 20:09:58 -0800995 msm_saved_state_phys =
996 allocate_contiguous_ebi_nomap(CPU_SAVED_STATE_SIZE *
997 num_possible_cpus(), 4);
998 if (!msm_saved_state_phys)
999 return -ENOMEM;
1000 msm_saved_state = ioremap_nocache(msm_saved_state_phys,
1001 CPU_SAVED_STATE_SIZE *
1002 num_possible_cpus());
1003 if (!msm_saved_state)
1004 return -ENOMEM;
1005
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001006 /* It is remotely possible that the code in msm_pm_collapse_exit()
1007 * which turns on the MMU with this mapping is in the
1008 * next even-numbered megabyte beyond the
1009 * start of msm_pm_collapse_exit().
1010 * Map this megabyte in as well.
1011 */
1012 pmd[2] = __pmd(pmdval + (2 << (PGDIR_SHIFT - 1)));
1013 flush_pmd_entry(pmd);
1014 msm_pm_pc_pgd = virt_to_phys(pc_pgd);
Steve Muckle730ad7a2012-02-21 15:26:37 -08001015 clean_caches((unsigned long)&msm_pm_pc_pgd, sizeof(msm_pm_pc_pgd),
1016 virt_to_phys(&msm_pm_pc_pgd));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001017
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001018 msm_pm_mode_sysfs_add();
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301019 msm_pm_add_stats(enable_stats, ARRAY_SIZE(enable_stats));
1020
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001021 suspend_set_ops(&msm_pm_ops);
Praveen Chidambaram192979f2012-04-25 18:30:23 -06001022 msm_pm_qtimer_available();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001023 msm_cpuidle_init();
1024
1025 return 0;
1026}
1027
1028late_initcall(msm_pm_init);