blob: 6e86baaf45682c427da19217f81baf20247b6c30 [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>
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -060027#include <linux/delay.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070028#include <mach/msm_iomap.h>
Praveen Chidambaram192979f2012-04-25 18:30:23 -060029#include <mach/socinfo.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070030#include <mach/system.h>
31#include <asm/cacheflush.h>
32#include <asm/hardware/gic.h>
33#include <asm/pgtable.h>
34#include <asm/pgalloc.h>
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -060035#include <asm/hardware/cache-l2x0.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070036#ifdef CONFIG_VFP
37#include <asm/vfp.h>
38#endif
39
40#include "acpuclock.h"
41#include "clock.h"
42#include "avs.h"
Abhijeet Dharmapurikarefaca4f2011-12-27 16:24:07 -080043#include <mach/cpuidle.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070044#include "idle.h"
Matt Wagantall7cca4642012-02-01 16:43:24 -080045#include "pm.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070046#include "scm-boot.h"
47#include "spm.h"
48#include "timer.h"
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -060049#include "pm-boot.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070050
51/******************************************************************************
52 * Debug Definitions
53 *****************************************************************************/
54
55enum {
56 MSM_PM_DEBUG_SUSPEND = BIT(0),
57 MSM_PM_DEBUG_POWER_COLLAPSE = BIT(1),
58 MSM_PM_DEBUG_SUSPEND_LIMITS = BIT(2),
59 MSM_PM_DEBUG_CLOCK = BIT(3),
60 MSM_PM_DEBUG_RESET_VECTOR = BIT(4),
Karthik Parsha6fb932d2012-01-24 18:04:12 -080061 MSM_PM_DEBUG_IDLE_CLK = BIT(5),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070062 MSM_PM_DEBUG_IDLE = BIT(6),
63 MSM_PM_DEBUG_IDLE_LIMITS = BIT(7),
64 MSM_PM_DEBUG_HOTPLUG = BIT(8),
65};
66
67static int msm_pm_debug_mask = 1;
68module_param_named(
69 debug_mask, msm_pm_debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP
70);
71
72
73/******************************************************************************
74 * Sleep Modes and Parameters
75 *****************************************************************************/
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070076enum {
77 MSM_PM_MODE_ATTR_SUSPEND,
78 MSM_PM_MODE_ATTR_IDLE,
79 MSM_PM_MODE_ATTR_NR,
80};
81
82static char *msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_NR] = {
83 [MSM_PM_MODE_ATTR_SUSPEND] = "suspend_enabled",
84 [MSM_PM_MODE_ATTR_IDLE] = "idle_enabled",
85};
86
87struct msm_pm_kobj_attribute {
88 unsigned int cpu;
89 struct kobj_attribute ka;
90};
91
92#define GET_CPU_OF_ATTR(attr) \
93 (container_of(attr, struct msm_pm_kobj_attribute, ka)->cpu)
94
95struct msm_pm_sysfs_sleep_mode {
96 struct kobject *kobj;
97 struct attribute_group attr_group;
98 struct attribute *attrs[MSM_PM_MODE_ATTR_NR + 1];
99 struct msm_pm_kobj_attribute kas[MSM_PM_MODE_ATTR_NR];
100};
101
102static char *msm_pm_sleep_mode_labels[MSM_PM_SLEEP_MODE_NR] = {
103 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE] = "power_collapse",
104 [MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT] = "wfi",
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600105 [MSM_PM_SLEEP_MODE_RETENTION] = "retention",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700106 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE] =
107 "standalone_power_collapse",
108};
109
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600110static struct msm_pm_sleep_ops pm_sleep_ops;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700111/*
112 * Write out the attribute.
113 */
114static ssize_t msm_pm_mode_attr_show(
115 struct kobject *kobj, struct kobj_attribute *attr, char *buf)
116{
117 int ret = -EINVAL;
118 int i;
119
120 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
121 struct kernel_param kp;
122 unsigned int cpu;
123 struct msm_pm_platform_data *mode;
124
125 if (msm_pm_sleep_mode_labels[i] == NULL)
126 continue;
127
128 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
129 continue;
130
131 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600132 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700133
134 if (!strcmp(attr->attr.name,
135 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
136 u32 arg = mode->suspend_enabled;
137 kp.arg = &arg;
138 ret = param_get_ulong(buf, &kp);
139 } else if (!strcmp(attr->attr.name,
140 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
141 u32 arg = mode->idle_enabled;
142 kp.arg = &arg;
143 ret = param_get_ulong(buf, &kp);
144 }
145
146 break;
147 }
148
149 if (ret > 0) {
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600150 strlcat(buf, "\n", PAGE_SIZE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700151 ret++;
152 }
153
154 return ret;
155}
156
157/*
158 * Read in the new attribute value.
159 */
160static ssize_t msm_pm_mode_attr_store(struct kobject *kobj,
161 struct kobj_attribute *attr, const char *buf, size_t count)
162{
163 int ret = -EINVAL;
164 int i;
165
166 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
167 struct kernel_param kp;
168 unsigned int cpu;
169 struct msm_pm_platform_data *mode;
170
171 if (msm_pm_sleep_mode_labels[i] == NULL)
172 continue;
173
174 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
175 continue;
176
177 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600178 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700179
180 if (!strcmp(attr->attr.name,
181 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
182 kp.arg = &mode->suspend_enabled;
183 ret = param_set_byte(buf, &kp);
184 } else if (!strcmp(attr->attr.name,
185 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
186 kp.arg = &mode->idle_enabled;
187 ret = param_set_byte(buf, &kp);
188 }
189
190 break;
191 }
192
193 return ret ? ret : count;
194}
195
196/*
197 * Add sysfs entries for one cpu.
198 */
199static int __init msm_pm_mode_sysfs_add_cpu(
200 unsigned int cpu, struct kobject *modes_kobj)
201{
202 char cpu_name[8];
203 struct kobject *cpu_kobj;
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600204 struct msm_pm_sysfs_sleep_mode *mode = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700205 int i, j, k;
206 int ret;
207
208 snprintf(cpu_name, sizeof(cpu_name), "cpu%u", cpu);
209 cpu_kobj = kobject_create_and_add(cpu_name, modes_kobj);
210 if (!cpu_kobj) {
211 pr_err("%s: cannot create %s kobject\n", __func__, cpu_name);
212 ret = -ENOMEM;
213 goto mode_sysfs_add_cpu_exit;
214 }
215
216 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
217 int idx = MSM_PM_MODE(cpu, i);
218
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600219 if ((!msm_pm_sleep_modes[idx].suspend_supported)
220 && (!msm_pm_sleep_modes[idx].idle_supported))
221 continue;
222
223 if (!msm_pm_sleep_mode_labels[i] ||
224 !msm_pm_sleep_mode_labels[i][0])
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700225 continue;
226
227 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
228 if (!mode) {
229 pr_err("%s: cannot allocate memory for attributes\n",
230 __func__);
231 ret = -ENOMEM;
232 goto mode_sysfs_add_cpu_exit;
233 }
234
235 mode->kobj = kobject_create_and_add(
236 msm_pm_sleep_mode_labels[i], cpu_kobj);
237 if (!mode->kobj) {
238 pr_err("%s: cannot create kobject\n", __func__);
239 ret = -ENOMEM;
240 goto mode_sysfs_add_cpu_exit;
241 }
242
243 for (k = 0, j = 0; k < MSM_PM_MODE_ATTR_NR; k++) {
244 if ((k == MSM_PM_MODE_ATTR_IDLE) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600245 !msm_pm_sleep_modes[idx].idle_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700246 continue;
247 if ((k == MSM_PM_MODE_ATTR_SUSPEND) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600248 !msm_pm_sleep_modes[idx].suspend_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700249 continue;
Stephen Boydd2059c32012-07-03 15:11:15 -0700250 sysfs_attr_init(&mode->kas[j].ka.attr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700251 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{
Abhimanyu Kapur90ced6e2012-06-26 17:41:25 -0700572 if (machine_is_msm8974())
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600573 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 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700670 /* fall through */
671
672 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
673 if (!allow)
674 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700675 /* fall through */
676
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600677 case MSM_PM_SLEEP_MODE_RETENTION:
678 if (!allow)
679 break;
680 /* fall through */
681
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700682 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
683 if (!allow)
684 break;
685
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600686 if (pm_sleep_ops.lowest_limits)
687 rs_limits = pm_sleep_ops.lowest_limits(true,
688 mode, latency_us, sleep_us,
689 &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700690
691 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
692 pr_info("CPU%u: %s: %s, latency %uus, "
693 "sleep %uus, limit %p\n",
694 dev->cpu, __func__, state->desc,
695 latency_us, sleep_us, rs_limits);
696
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700697 if (!rs_limits)
698 allow = false;
699 break;
700
701 default:
702 allow = false;
703 break;
704 }
705
706 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
707 pr_info("CPU%u: %s: allow %s: %d\n",
708 dev->cpu, __func__, state->desc, (int)allow);
709
710 if (allow) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700711 if (power < power_usage) {
712 power_usage = power;
713 ret = mode;
714 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700715
716 if (MSM_PM_SLEEP_MODE_POWER_COLLAPSE == mode)
717 msm_pm_idle_rs_limits = rs_limits;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700718 }
719 }
720
Steve Mucklef132c6c2012-06-06 18:30:57 -0700721 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700722}
723
724int msm_pm_idle_enter(enum msm_pm_sleep_mode sleep_mode)
725{
726 int64_t time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700727 int exit_stat;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700728
729 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
730 pr_info("CPU%u: %s: mode %d\n",
731 smp_processor_id(), __func__, sleep_mode);
732
733 time = ktime_to_ns(ktime_get());
734
735 switch (sleep_mode) {
736 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
737 msm_pm_swfi();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700738 exit_stat = MSM_PM_STAT_IDLE_WFI;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700739 break;
740
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600741 case MSM_PM_SLEEP_MODE_RETENTION:
742 msm_pm_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600743 exit_stat = MSM_PM_STAT_RETENTION;
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600744 break;
745
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700746 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
747 msm_pm_power_collapse_standalone(true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700748 exit_stat = MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700749 break;
750
751 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE: {
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600752 int64_t timer_expiration = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700753 bool timer_halted = false;
754 uint32_t sleep_delay;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600755 int ret = -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700756 int notify_rpm =
757 (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600758 int collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700759
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600760 timer_expiration = msm_pm_timer_enter_idle();
761
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700762 sleep_delay = (uint32_t) msm_pm_convert_and_cap_time(
763 timer_expiration, MSM_PM_SLEEP_TICK_LIMIT);
764 if (sleep_delay == 0) /* 0 would mean infinite time */
765 sleep_delay = 1;
766
Karthik Parsha6fb932d2012-01-24 18:04:12 -0800767 if (MSM_PM_DEBUG_IDLE_CLK & msm_pm_debug_mask)
768 clock_debug_print_enabled();
769
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600770 if (pm_sleep_ops.enter_sleep)
771 ret = pm_sleep_ops.enter_sleep(sleep_delay,
772 msm_pm_idle_rs_limits,
773 true, notify_rpm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700774 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600775 collapsed = msm_pm_power_collapse(true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700776 timer_halted = true;
777
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600778 if (pm_sleep_ops.exit_sleep)
779 pm_sleep_ops.exit_sleep(msm_pm_idle_rs_limits,
780 true, notify_rpm, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700781 }
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600782 msm_pm_timer_exit_idle(timer_halted);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700783 exit_stat = MSM_PM_STAT_IDLE_POWER_COLLAPSE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700784 break;
785 }
786
787 default:
788 __WARN();
789 goto cpuidle_enter_bail;
790 }
791
792 time = ktime_to_ns(ktime_get()) - time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700793 msm_pm_add_stat(exit_stat, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700794
795 do_div(time, 1000);
796 return (int) time;
797
798cpuidle_enter_bail:
799 return 0;
800}
801
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -0600802static struct msm_pm_sleep_status_data *msm_pm_slp_sts;
803
804static DEFINE_PER_CPU_SHARED_ALIGNED(enum msm_pm_sleep_mode,
805 msm_pm_last_slp_mode);
806
807bool msm_pm_verify_cpu_pc(unsigned int cpu)
808{
809 enum msm_pm_sleep_mode mode = per_cpu(msm_pm_last_slp_mode, cpu);
810
Mahesh Sivasubramanian05d68512012-07-06 10:44:52 -0600811 if (msm_pm_slp_sts) {
812 int acc_sts = __raw_readl(msm_pm_slp_sts->base_addr
813 + cpu * msm_pm_slp_sts->cpu_offset);
814
815 if ((acc_sts & msm_pm_slp_sts->mask) &&
816 ((mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE) ||
817 (mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE)))
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -0600818 return true;
Mahesh Sivasubramanian05d68512012-07-06 10:44:52 -0600819 }
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -0600820
821 return false;
822}
823
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700824void msm_pm_cpu_enter_lowpower(unsigned int cpu)
825{
826 int i;
827 bool allow[MSM_PM_SLEEP_MODE_NR];
828
829 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
830 struct msm_pm_platform_data *mode;
831
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600832 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700833 allow[i] = mode->suspend_supported && mode->suspend_enabled;
834 }
835
836 if (MSM_PM_DEBUG_HOTPLUG & msm_pm_debug_mask)
837 pr_notice("CPU%u: %s: shutting down cpu\n", cpu, __func__);
838
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -0600839 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) {
840 per_cpu(msm_pm_last_slp_mode, cpu)
841 = MSM_PM_SLEEP_MODE_POWER_COLLAPSE;
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700842 msm_pm_power_collapse(false);
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -0600843 } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
844 per_cpu(msm_pm_last_slp_mode, cpu)
845 = MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE;
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700846 msm_pm_power_collapse_standalone(false);
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600847 } else if (allow[MSM_PM_SLEEP_MODE_RETENTION]) {
848 per_cpu(msm_pm_last_slp_mode, cpu)
849 = MSM_PM_SLEEP_MODE_RETENTION;
850 msm_pm_retention();
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -0600851 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
852 per_cpu(msm_pm_last_slp_mode, cpu)
Girish Mahadevanc8127052012-04-25 16:23:24 -0600853 = MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT;
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700854 msm_pm_swfi();
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -0600855 } else
856 per_cpu(msm_pm_last_slp_mode, cpu) = MSM_PM_SLEEP_MODE_NR;
857}
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700858
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -0600859int msm_pm_wait_cpu_shutdown(unsigned int cpu)
860{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700861
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -0600862 int timeout = 10;
863
864 if (!msm_pm_slp_sts)
865 return 0;
866
867 while (timeout--) {
868
869 /*
870 * Check for the SPM of the core being hotplugged to set
871 * its sleep state.The SPM sleep state indicates that the
872 * core has been power collapsed.
873 */
874
875 int acc_sts = __raw_readl(msm_pm_slp_sts->base_addr
876 + cpu * msm_pm_slp_sts->cpu_offset);
877 mb();
878
879 if (acc_sts & msm_pm_slp_sts->mask)
880 return 0;
881
882 usleep(100);
883 }
884 pr_warn("%s(): Timed out waiting for CPU %u SPM to enter sleep state",
885 __func__, cpu);
886 return -EBUSY;
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700887}
888
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700889static int msm_pm_enter(suspend_state_t state)
890{
891 bool allow[MSM_PM_SLEEP_MODE_NR];
892 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700893 int64_t period = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530894 int64_t time = msm_pm_timer_enter_suspend(&period);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700895
896 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
897 pr_info("%s\n", __func__);
898
899 if (smp_processor_id()) {
900 __WARN();
901 goto enter_exit;
902 }
903
904
905 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
906 struct msm_pm_platform_data *mode;
907
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600908 mode = &msm_pm_sleep_modes[MSM_PM_MODE(0, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700909 allow[i] = mode->suspend_supported && mode->suspend_enabled;
910 }
911
912 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600913 void *rs_limits = NULL;
914 int ret = -ENODEV;
915 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700916
917 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
918 pr_info("%s: power collapse\n", __func__);
919
920 clock_debug_print_enabled();
921
922#ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE
923 if (msm_pm_sleep_time_override > 0) {
924 int64_t ns = NSEC_PER_SEC *
925 (int64_t) msm_pm_sleep_time_override;
926 msm_pm_set_max_sleep_time(ns);
927 msm_pm_sleep_time_override = 0;
928 }
929#endif /* CONFIG_MSM_SLEEP_TIME_OVERRIDE */
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600930 if (pm_sleep_ops.lowest_limits)
931 rs_limits = pm_sleep_ops.lowest_limits(false,
932 MSM_PM_SLEEP_MODE_POWER_COLLAPSE, -1,
933 -1, &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700934
935 if (rs_limits) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600936 if (pm_sleep_ops.enter_sleep)
937 ret = pm_sleep_ops.enter_sleep(
938 msm_pm_max_sleep_time,
939 rs_limits, false, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700940 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600941 int collapsed = msm_pm_power_collapse(false);
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600942 if (pm_sleep_ops.exit_sleep) {
943 pm_sleep_ops.exit_sleep(rs_limits,
944 false, true, collapsed);
945 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700946 }
947 } else {
948 pr_err("%s: cannot find the lowest power limit\n",
949 __func__);
950 }
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530951 time = msm_pm_timer_exit_suspend(time, period);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700952 msm_pm_add_stat(MSM_PM_STAT_SUSPEND, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700953 } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
954 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
955 pr_info("%s: standalone power collapse\n", __func__);
956 msm_pm_power_collapse_standalone(false);
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600957 } else if (allow[MSM_PM_SLEEP_MODE_RETENTION]) {
958 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
959 pr_info("%s: retention\n", __func__);
960 msm_pm_retention();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700961 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
962 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
963 pr_info("%s: swfi\n", __func__);
964 msm_pm_swfi();
965 }
966
967
968enter_exit:
969 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
970 pr_info("%s: return\n", __func__);
971
972 return 0;
973}
974
975static struct platform_suspend_ops msm_pm_ops = {
976 .enter = msm_pm_enter,
977 .valid = suspend_valid_only_mem,
978};
979
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700980/******************************************************************************
981 * Initialization routine
982 *****************************************************************************/
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -0600983void __init msm_pm_init_sleep_status_data(
984 struct msm_pm_sleep_status_data *data)
985{
986 msm_pm_slp_sts = data;
987}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700988
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600989void msm_pm_set_sleep_ops(struct msm_pm_sleep_ops *ops)
990{
991 if (ops)
992 pm_sleep_ops = *ops;
993}
994
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700995static int __init msm_pm_init(void)
996{
997 pgd_t *pc_pgd;
998 pmd_t *pmd;
999 unsigned long pmdval;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301000 enum msm_pm_time_stats_id enable_stats[] = {
1001 MSM_PM_STAT_IDLE_WFI,
1002 MSM_PM_STAT_RETENTION,
1003 MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE,
1004 MSM_PM_STAT_IDLE_POWER_COLLAPSE,
1005 MSM_PM_STAT_SUSPEND,
1006 };
Steve Mucklef132c6c2012-06-06 18:30:57 -07001007 unsigned long exit_phys;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301008
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001009 /* Page table for cores to come back up safely. */
1010 pc_pgd = pgd_alloc(&init_mm);
1011 if (!pc_pgd)
1012 return -ENOMEM;
1013
Steve Mucklef132c6c2012-06-06 18:30:57 -07001014 exit_phys = virt_to_phys(msm_pm_collapse_exit);
1015
1016 pmd = pmd_offset(pud_offset(pc_pgd + pgd_index(exit_phys),exit_phys),
1017 exit_phys);
1018 pmdval = (exit_phys & PGDIR_MASK) |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001019 PMD_TYPE_SECT | PMD_SECT_AP_WRITE;
1020 pmd[0] = __pmd(pmdval);
1021 pmd[1] = __pmd(pmdval + (1 << (PGDIR_SHIFT - 1)));
1022
Steve Mucklefcece052012-02-18 20:09:58 -08001023 msm_saved_state_phys =
1024 allocate_contiguous_ebi_nomap(CPU_SAVED_STATE_SIZE *
1025 num_possible_cpus(), 4);
1026 if (!msm_saved_state_phys)
1027 return -ENOMEM;
1028 msm_saved_state = ioremap_nocache(msm_saved_state_phys,
1029 CPU_SAVED_STATE_SIZE *
1030 num_possible_cpus());
1031 if (!msm_saved_state)
1032 return -ENOMEM;
1033
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001034 /* It is remotely possible that the code in msm_pm_collapse_exit()
1035 * which turns on the MMU with this mapping is in the
1036 * next even-numbered megabyte beyond the
1037 * start of msm_pm_collapse_exit().
1038 * Map this megabyte in as well.
1039 */
1040 pmd[2] = __pmd(pmdval + (2 << (PGDIR_SHIFT - 1)));
1041 flush_pmd_entry(pmd);
1042 msm_pm_pc_pgd = virt_to_phys(pc_pgd);
Steve Muckle730ad7a2012-02-21 15:26:37 -08001043 clean_caches((unsigned long)&msm_pm_pc_pgd, sizeof(msm_pm_pc_pgd),
1044 virt_to_phys(&msm_pm_pc_pgd));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001045
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001046 msm_pm_mode_sysfs_add();
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301047 msm_pm_add_stats(enable_stats, ARRAY_SIZE(enable_stats));
1048
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001049 suspend_set_ops(&msm_pm_ops);
Praveen Chidambaram192979f2012-04-25 18:30:23 -06001050 msm_pm_qtimer_available();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001051 msm_cpuidle_init();
1052
1053 return 0;
1054}
1055
1056late_initcall(msm_pm_init);