blob: 6f68c8b343ea24b93ecfb62638a9b85cfacdfafa [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>
23#include <linux/pm_qos_params.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070024#include <linux/smp.h>
25#include <linux/suspend.h>
26#include <linux/tick.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070027#include <linux/wakelock.h>
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -060028#include <linux/delay.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070029#include <mach/msm_iomap.h>
Praveen Chidambaram192979f2012-04-25 18:30:23 -060030#include <mach/socinfo.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070031#include <mach/system.h>
32#include <asm/cacheflush.h>
33#include <asm/hardware/gic.h>
34#include <asm/pgtable.h>
35#include <asm/pgalloc.h>
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -060036#include <asm/hardware/cache-l2x0.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070037#ifdef CONFIG_VFP
38#include <asm/vfp.h>
39#endif
40
41#include "acpuclock.h"
42#include "clock.h"
43#include "avs.h"
Abhijeet Dharmapurikarefaca4f2011-12-27 16:24:07 -080044#include <mach/cpuidle.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070045#include "idle.h"
Matt Wagantall7cca4642012-02-01 16:43:24 -080046#include "pm.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070047#include "scm-boot.h"
48#include "spm.h"
49#include "timer.h"
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -060050#include "pm-boot.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070051
52/******************************************************************************
53 * Debug Definitions
54 *****************************************************************************/
55
56enum {
57 MSM_PM_DEBUG_SUSPEND = BIT(0),
58 MSM_PM_DEBUG_POWER_COLLAPSE = BIT(1),
59 MSM_PM_DEBUG_SUSPEND_LIMITS = BIT(2),
60 MSM_PM_DEBUG_CLOCK = BIT(3),
61 MSM_PM_DEBUG_RESET_VECTOR = BIT(4),
Karthik Parsha6fb932d2012-01-24 18:04:12 -080062 MSM_PM_DEBUG_IDLE_CLK = BIT(5),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070063 MSM_PM_DEBUG_IDLE = BIT(6),
64 MSM_PM_DEBUG_IDLE_LIMITS = BIT(7),
65 MSM_PM_DEBUG_HOTPLUG = BIT(8),
66};
67
68static int msm_pm_debug_mask = 1;
69module_param_named(
70 debug_mask, msm_pm_debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP
71);
72
73
74/******************************************************************************
75 * Sleep Modes and Parameters
76 *****************************************************************************/
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070077enum {
78 MSM_PM_MODE_ATTR_SUSPEND,
79 MSM_PM_MODE_ATTR_IDLE,
80 MSM_PM_MODE_ATTR_NR,
81};
82
83static char *msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_NR] = {
84 [MSM_PM_MODE_ATTR_SUSPEND] = "suspend_enabled",
85 [MSM_PM_MODE_ATTR_IDLE] = "idle_enabled",
86};
87
88struct msm_pm_kobj_attribute {
89 unsigned int cpu;
90 struct kobj_attribute ka;
91};
92
93#define GET_CPU_OF_ATTR(attr) \
94 (container_of(attr, struct msm_pm_kobj_attribute, ka)->cpu)
95
96struct msm_pm_sysfs_sleep_mode {
97 struct kobject *kobj;
98 struct attribute_group attr_group;
99 struct attribute *attrs[MSM_PM_MODE_ATTR_NR + 1];
100 struct msm_pm_kobj_attribute kas[MSM_PM_MODE_ATTR_NR];
101};
102
103static char *msm_pm_sleep_mode_labels[MSM_PM_SLEEP_MODE_NR] = {
104 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE] = "power_collapse",
105 [MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT] = "wfi",
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600106 [MSM_PM_SLEEP_MODE_RETENTION] = "retention",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700107 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE] =
108 "standalone_power_collapse",
109};
110
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600111static struct msm_pm_sleep_ops pm_sleep_ops;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700112/*
113 * Write out the attribute.
114 */
115static ssize_t msm_pm_mode_attr_show(
116 struct kobject *kobj, struct kobj_attribute *attr, char *buf)
117{
118 int ret = -EINVAL;
119 int i;
120
121 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
122 struct kernel_param kp;
123 unsigned int cpu;
124 struct msm_pm_platform_data *mode;
125
126 if (msm_pm_sleep_mode_labels[i] == NULL)
127 continue;
128
129 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
130 continue;
131
132 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600133 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700134
135 if (!strcmp(attr->attr.name,
136 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
137 u32 arg = mode->suspend_enabled;
138 kp.arg = &arg;
139 ret = param_get_ulong(buf, &kp);
140 } else if (!strcmp(attr->attr.name,
141 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
142 u32 arg = mode->idle_enabled;
143 kp.arg = &arg;
144 ret = param_get_ulong(buf, &kp);
145 }
146
147 break;
148 }
149
150 if (ret > 0) {
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600151 strlcat(buf, "\n", PAGE_SIZE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700152 ret++;
153 }
154
155 return ret;
156}
157
158/*
159 * Read in the new attribute value.
160 */
161static ssize_t msm_pm_mode_attr_store(struct kobject *kobj,
162 struct kobj_attribute *attr, const char *buf, size_t count)
163{
164 int ret = -EINVAL;
165 int i;
166
167 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
168 struct kernel_param kp;
169 unsigned int cpu;
170 struct msm_pm_platform_data *mode;
171
172 if (msm_pm_sleep_mode_labels[i] == NULL)
173 continue;
174
175 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
176 continue;
177
178 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600179 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700180
181 if (!strcmp(attr->attr.name,
182 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
183 kp.arg = &mode->suspend_enabled;
184 ret = param_set_byte(buf, &kp);
185 } else if (!strcmp(attr->attr.name,
186 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
187 kp.arg = &mode->idle_enabled;
188 ret = param_set_byte(buf, &kp);
189 }
190
191 break;
192 }
193
194 return ret ? ret : count;
195}
196
197/*
198 * Add sysfs entries for one cpu.
199 */
200static int __init msm_pm_mode_sysfs_add_cpu(
201 unsigned int cpu, struct kobject *modes_kobj)
202{
203 char cpu_name[8];
204 struct kobject *cpu_kobj;
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600205 struct msm_pm_sysfs_sleep_mode *mode = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700206 int i, j, k;
207 int ret;
208
209 snprintf(cpu_name, sizeof(cpu_name), "cpu%u", cpu);
210 cpu_kobj = kobject_create_and_add(cpu_name, modes_kobj);
211 if (!cpu_kobj) {
212 pr_err("%s: cannot create %s kobject\n", __func__, cpu_name);
213 ret = -ENOMEM;
214 goto mode_sysfs_add_cpu_exit;
215 }
216
217 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
218 int idx = MSM_PM_MODE(cpu, i);
219
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600220 if ((!msm_pm_sleep_modes[idx].suspend_supported)
221 && (!msm_pm_sleep_modes[idx].idle_supported))
222 continue;
223
224 if (!msm_pm_sleep_mode_labels[i] ||
225 !msm_pm_sleep_mode_labels[i][0])
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700226 continue;
227
228 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
229 if (!mode) {
230 pr_err("%s: cannot allocate memory for attributes\n",
231 __func__);
232 ret = -ENOMEM;
233 goto mode_sysfs_add_cpu_exit;
234 }
235
236 mode->kobj = kobject_create_and_add(
237 msm_pm_sleep_mode_labels[i], cpu_kobj);
238 if (!mode->kobj) {
239 pr_err("%s: cannot create kobject\n", __func__);
240 ret = -ENOMEM;
241 goto mode_sysfs_add_cpu_exit;
242 }
243
244 for (k = 0, j = 0; k < MSM_PM_MODE_ATTR_NR; k++) {
245 if ((k == MSM_PM_MODE_ATTR_IDLE) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600246 !msm_pm_sleep_modes[idx].idle_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700247 continue;
248 if ((k == MSM_PM_MODE_ATTR_SUSPEND) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600249 !msm_pm_sleep_modes[idx].suspend_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700250 continue;
251 mode->kas[j].cpu = cpu;
252 mode->kas[j].ka.attr.mode = 0644;
253 mode->kas[j].ka.show = msm_pm_mode_attr_show;
254 mode->kas[j].ka.store = msm_pm_mode_attr_store;
255 mode->kas[j].ka.attr.name = msm_pm_mode_attr_labels[k];
256 mode->attrs[j] = &mode->kas[j].ka.attr;
257 j++;
258 }
259 mode->attrs[j] = NULL;
260
261 mode->attr_group.attrs = mode->attrs;
262 ret = sysfs_create_group(mode->kobj, &mode->attr_group);
263 if (ret) {
264 pr_err("%s: cannot create kobject attribute group\n",
265 __func__);
266 goto mode_sysfs_add_cpu_exit;
267 }
268 }
269
270 ret = 0;
271
272mode_sysfs_add_cpu_exit:
Praveen Chidambaramd5ac2d32011-10-24 14:30:27 -0600273 if (ret) {
Praveen Chidambaram2cfda632011-10-11 16:58:09 -0600274 if (mode && mode->kobj)
275 kobject_del(mode->kobj);
276 kfree(mode);
277 }
278
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700279 return ret;
280}
281
282/*
283 * Add sysfs entries for the sleep modes.
284 */
285static int __init msm_pm_mode_sysfs_add(void)
286{
287 struct kobject *module_kobj;
288 struct kobject *modes_kobj;
289 unsigned int cpu;
290 int ret;
291
292 module_kobj = kset_find_obj(module_kset, KBUILD_MODNAME);
293 if (!module_kobj) {
294 pr_err("%s: cannot find kobject for module %s\n",
295 __func__, KBUILD_MODNAME);
296 ret = -ENOENT;
297 goto mode_sysfs_add_exit;
298 }
299
300 modes_kobj = kobject_create_and_add("modes", module_kobj);
301 if (!modes_kobj) {
302 pr_err("%s: cannot create modes kobject\n", __func__);
303 ret = -ENOMEM;
304 goto mode_sysfs_add_exit;
305 }
306
307 for_each_possible_cpu(cpu) {
308 ret = msm_pm_mode_sysfs_add_cpu(cpu, modes_kobj);
309 if (ret)
310 goto mode_sysfs_add_exit;
311 }
312
313 ret = 0;
314
315mode_sysfs_add_exit:
316 return ret;
317}
318
319/******************************************************************************
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700320 * Configure Hardware before/after Low Power Mode
321 *****************************************************************************/
322
323/*
324 * Configure hardware registers in preparation for Apps power down.
325 */
326static void msm_pm_config_hw_before_power_down(void)
327{
328 return;
329}
330
331/*
332 * Clear hardware registers after Apps powers up.
333 */
334static void msm_pm_config_hw_after_power_up(void)
335{
336 return;
337}
338
339/*
340 * Configure hardware registers in preparation for SWFI.
341 */
342static void msm_pm_config_hw_before_swfi(void)
343{
344 return;
345}
346
347
348/******************************************************************************
349 * Suspend Max Sleep Time
350 *****************************************************************************/
351
352#ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE
353static int msm_pm_sleep_time_override;
354module_param_named(sleep_time_override,
355 msm_pm_sleep_time_override, int, S_IRUGO | S_IWUSR | S_IWGRP);
356#endif
357
358#define SCLK_HZ (32768)
359#define MSM_PM_SLEEP_TICK_LIMIT (0x6DDD000)
360
361static uint32_t msm_pm_max_sleep_time;
362
363/*
364 * Convert time from nanoseconds to slow clock ticks, then cap it to the
365 * specified limit
366 */
367static int64_t msm_pm_convert_and_cap_time(int64_t time_ns, int64_t limit)
368{
369 do_div(time_ns, NSEC_PER_SEC / SCLK_HZ);
370 return (time_ns > limit) ? limit : time_ns;
371}
372
373/*
374 * Set the sleep time for suspend. 0 means infinite sleep time.
375 */
376void msm_pm_set_max_sleep_time(int64_t max_sleep_time_ns)
377{
378 if (max_sleep_time_ns == 0) {
379 msm_pm_max_sleep_time = 0;
380 } else {
381 msm_pm_max_sleep_time = (uint32_t)msm_pm_convert_and_cap_time(
382 max_sleep_time_ns, MSM_PM_SLEEP_TICK_LIMIT);
383
384 if (msm_pm_max_sleep_time == 0)
385 msm_pm_max_sleep_time = 1;
386 }
387
388 if (msm_pm_debug_mask & MSM_PM_DEBUG_SUSPEND)
389 pr_info("%s: Requested %lld ns Giving %u sclk ticks\n",
390 __func__, max_sleep_time_ns, msm_pm_max_sleep_time);
391}
392EXPORT_SYMBOL(msm_pm_set_max_sleep_time);
393
394
395/******************************************************************************
396 *
397 *****************************************************************************/
398
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600399static void *msm_pm_idle_rs_limits;
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600400static bool msm_pm_use_qtimer;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700401
402static void msm_pm_swfi(void)
403{
404 msm_pm_config_hw_before_swfi();
405 msm_arch_idle();
406}
407
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600408
409static void msm_pm_retention(void)
410{
411 int ret = 0;
412
413 msm_pm_config_hw_before_swfi();
414 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_POWER_RETENTION, false);
415 WARN_ON(ret);
416 msm_arch_idle();
417 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
418 WARN_ON(ret);
419}
420
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600421#ifdef CONFIG_CACHE_L2X0
422static inline bool msm_pm_l2x0_power_collapse(void)
423{
424 bool collapsed = 0;
425
Taniya Das38a8c6e2012-05-09 20:34:39 +0530426 l2cc_suspend();
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600427 collapsed = msm_pm_collapse();
Taniya Das38a8c6e2012-05-09 20:34:39 +0530428 l2cc_resume();
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600429
430 return collapsed;
431}
432#else
433static inline bool msm_pm_l2x0_power_collapse(void)
434{
435 return msm_pm_collapse();
436}
437#endif
438
Stephen Boydb29750d2012-02-21 01:21:32 -0800439static bool __ref msm_pm_spm_power_collapse(
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700440 unsigned int cpu, bool from_idle, bool notify_rpm)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700441{
442 void *entry;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600443 bool collapsed = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700444 int ret;
Rohit Vaswanie78dfb62012-02-21 10:29:29 -0800445 unsigned int saved_gic_cpu_ctrl;
446
447 saved_gic_cpu_ctrl = readl_relaxed(MSM_QGIC_CPU_BASE + GIC_CPU_CTRL);
448 mb();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700449
450 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
451 pr_info("CPU%u: %s: notify_rpm %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700452 cpu, __func__, (int) notify_rpm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700453
454 ret = msm_spm_set_low_power_mode(
455 MSM_SPM_MODE_POWER_COLLAPSE, notify_rpm);
456 WARN_ON(ret);
457
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700458 entry = (!cpu || from_idle) ?
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700459 msm_pm_collapse_exit : msm_secondary_startup;
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700460 msm_pm_boot_config_before_pc(cpu, virt_to_phys(entry));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700461
462 if (MSM_PM_DEBUG_RESET_VECTOR & msm_pm_debug_mask)
463 pr_info("CPU%u: %s: program vector to %p\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700464 cpu, __func__, entry);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700465
466#ifdef CONFIG_VFP
467 vfp_flush_context();
468#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
476 vfp_reinit();
477#endif
478 cpu_init();
479 writel(0xF0, MSM_QGIC_CPU_BASE + GIC_CPU_PRIMASK);
Rohit Vaswanie78dfb62012-02-21 10:29:29 -0800480 writel_relaxed(saved_gic_cpu_ctrl,
481 MSM_QGIC_CPU_BASE + GIC_CPU_CTRL);
482 mb();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700483 local_fiq_enable();
484 }
485
486 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
487 pr_info("CPU%u: %s: msm_pm_collapse returned, collapsed %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700488 cpu, __func__, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700489
490 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
491 WARN_ON(ret);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600492 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700493}
494
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600495static bool msm_pm_power_collapse_standalone(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700496{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700497 unsigned int cpu = smp_processor_id();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700498 unsigned int avsdscr_setting;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600499 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700500
501 avsdscr_setting = avs_get_avsdscr();
502 avs_disable();
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700503 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700504 avs_reset_delays(avsdscr_setting);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600505 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700506}
507
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600508static bool msm_pm_power_collapse(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700509{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700510 unsigned int cpu = smp_processor_id();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700511 unsigned long saved_acpuclk_rate;
512 unsigned int avsdscr_setting;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600513 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700514
515 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
516 pr_info("CPU%u: %s: idle %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700517 cpu, __func__, (int)from_idle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700518
519 msm_pm_config_hw_before_power_down();
520 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700521 pr_info("CPU%u: %s: pre power down\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700522
523 avsdscr_setting = avs_get_avsdscr();
524 avs_disable();
525
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700526 if (cpu_online(cpu))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700527 saved_acpuclk_rate = acpuclk_power_collapse();
528 else
529 saved_acpuclk_rate = 0;
530
531 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
532 pr_info("CPU%u: %s: change clock rate (old rate = %lu)\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700533 cpu, __func__, saved_acpuclk_rate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700534
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700535 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700536
Girish Mahadevan4e025d92012-02-29 13:26:51 -0700537 if (cpu_online(cpu)) {
538 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
539 pr_info("CPU%u: %s: restore clock rate to %lu\n",
540 cpu, __func__, saved_acpuclk_rate);
541 if (acpuclk_set_rate(cpu, saved_acpuclk_rate, SETRATE_PC) < 0)
542 pr_err("CPU%u: %s: failed to restore clock rate(%lu)\n",
543 cpu, __func__, saved_acpuclk_rate);
544 } else {
545 unsigned int gic_dist_enabled;
546 unsigned int gic_dist_pending;
547 gic_dist_enabled = readl_relaxed(
548 MSM_QGIC_DIST_BASE + GIC_DIST_ENABLE_CLEAR);
549 gic_dist_pending = readl_relaxed(
550 MSM_QGIC_DIST_BASE + GIC_DIST_PENDING_SET);
551 mb();
552 gic_dist_pending &= gic_dist_enabled;
553
554 if (gic_dist_pending)
555 pr_err("CPU %d interrupted during hotplug.Pending int 0x%x\n",
556 cpu, gic_dist_pending);
557 }
558
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700559
560 avs_reset_delays(avsdscr_setting);
561 msm_pm_config_hw_after_power_up();
562 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700563 pr_info("CPU%u: %s: post power up\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700564
565 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700566 pr_info("CPU%u: %s: return\n", cpu, __func__);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600567 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700568}
569
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600570static void msm_pm_qtimer_available(void)
571{
572 if (machine_is_copper())
573 msm_pm_use_qtimer = true;
574}
575
576static int64_t msm_pm_timer_enter_idle(void)
577{
578 if (msm_pm_use_qtimer)
579 return ktime_to_ns(tick_nohz_get_sleep_length());
580
581 return msm_timer_enter_idle();
582}
583
584static void msm_pm_timer_exit_idle(bool timer_halted)
585{
586 if (msm_pm_use_qtimer)
587 return;
588
589 msm_timer_exit_idle((int) timer_halted);
590}
591
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530592static int64_t msm_pm_timer_enter_suspend(int64_t *period)
593{
594 int time = 0;
595
596 if (msm_pm_use_qtimer)
597 return sched_clock();
598
599 time = msm_timer_get_sclk_time(period);
600 if (!time)
601 pr_err("%s: Unable to read sclk.\n", __func__);
602
603 return time;
604}
605
606static int64_t msm_pm_timer_exit_suspend(int64_t time, int64_t period)
607{
608 if (msm_pm_use_qtimer)
609 return sched_clock() - time;
610
611 if (time != 0) {
612 int64_t end_time = msm_timer_get_sclk_time(NULL);
613 if (end_time != 0) {
614 time = end_time - time;
615 if (time < 0)
616 time += period;
617 } else
618 time = 0;
619 }
620
621 return time;
622}
623
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700624/******************************************************************************
625 * External Idle/Suspend Functions
626 *****************************************************************************/
627
628void arch_idle(void)
629{
630 return;
631}
632
633int msm_pm_idle_prepare(struct cpuidle_device *dev)
634{
635 uint32_t latency_us;
636 uint32_t sleep_us;
637 int i;
638
639 latency_us = (uint32_t) pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
640 sleep_us = (uint32_t) ktime_to_ns(tick_nohz_get_sleep_length());
641 sleep_us = DIV_ROUND_UP(sleep_us, 1000);
642
643 for (i = 0; i < dev->state_count; i++) {
644 struct cpuidle_state *state = &dev->states[i];
645 enum msm_pm_sleep_mode mode;
646 bool allow;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600647 void *rs_limits = NULL;
648 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700649 int idx;
650
651 mode = (enum msm_pm_sleep_mode) state->driver_data;
652 idx = MSM_PM_MODE(dev->cpu, mode);
653
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600654 allow = msm_pm_sleep_modes[idx].idle_enabled &&
655 msm_pm_sleep_modes[idx].idle_supported;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700656
657 switch (mode) {
658 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
659 if (!allow)
660 break;
661
662 if (num_online_cpus() > 1) {
663 allow = false;
664 break;
665 }
666#ifdef CONFIG_HAS_WAKELOCK
667 if (has_wake_lock(WAKE_LOCK_IDLE)) {
668 allow = false;
669 break;
670 }
671#endif
672 /* fall through */
673
674 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
675 if (!allow)
676 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700677 /* fall through */
678
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600679 case MSM_PM_SLEEP_MODE_RETENTION:
680 if (!allow)
681 break;
682 /* fall through */
683
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700684 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
685 if (!allow)
686 break;
687
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600688 if (pm_sleep_ops.lowest_limits)
689 rs_limits = pm_sleep_ops.lowest_limits(true,
690 mode, latency_us, sleep_us,
691 &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700692
693 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
694 pr_info("CPU%u: %s: %s, latency %uus, "
695 "sleep %uus, limit %p\n",
696 dev->cpu, __func__, state->desc,
697 latency_us, sleep_us, rs_limits);
698
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700699 if (!rs_limits)
700 allow = false;
701 break;
702
703 default:
704 allow = false;
705 break;
706 }
707
708 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
709 pr_info("CPU%u: %s: allow %s: %d\n",
710 dev->cpu, __func__, state->desc, (int)allow);
711
712 if (allow) {
713 state->flags &= ~CPUIDLE_FLAG_IGNORE;
714 state->target_residency = 0;
715 state->exit_latency = 0;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600716 state->power_usage = power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700717
718 if (MSM_PM_SLEEP_MODE_POWER_COLLAPSE == mode)
719 msm_pm_idle_rs_limits = rs_limits;
720 } else {
721 state->flags |= CPUIDLE_FLAG_IGNORE;
722 }
723 }
724
725 return 0;
726}
727
728int msm_pm_idle_enter(enum msm_pm_sleep_mode sleep_mode)
729{
730 int64_t time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700731 int exit_stat;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700732
733 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
734 pr_info("CPU%u: %s: mode %d\n",
735 smp_processor_id(), __func__, sleep_mode);
736
737 time = ktime_to_ns(ktime_get());
738
739 switch (sleep_mode) {
740 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
741 msm_pm_swfi();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700742 exit_stat = MSM_PM_STAT_IDLE_WFI;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700743 break;
744
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600745 case MSM_PM_SLEEP_MODE_RETENTION:
746 msm_pm_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600747 exit_stat = MSM_PM_STAT_RETENTION;
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600748 break;
749
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700750 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
751 msm_pm_power_collapse_standalone(true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700752 exit_stat = MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700753 break;
754
755 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE: {
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600756 int64_t timer_expiration = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700757 bool timer_halted = false;
758 uint32_t sleep_delay;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600759 int ret = -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700760 int notify_rpm =
761 (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600762 int collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700763
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600764 timer_expiration = msm_pm_timer_enter_idle();
765
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700766 sleep_delay = (uint32_t) msm_pm_convert_and_cap_time(
767 timer_expiration, MSM_PM_SLEEP_TICK_LIMIT);
768 if (sleep_delay == 0) /* 0 would mean infinite time */
769 sleep_delay = 1;
770
Karthik Parsha6fb932d2012-01-24 18:04:12 -0800771 if (MSM_PM_DEBUG_IDLE_CLK & msm_pm_debug_mask)
772 clock_debug_print_enabled();
773
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600774 if (pm_sleep_ops.enter_sleep)
775 ret = pm_sleep_ops.enter_sleep(sleep_delay,
776 msm_pm_idle_rs_limits,
777 true, notify_rpm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700778 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600779 collapsed = msm_pm_power_collapse(true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700780 timer_halted = true;
781
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600782 if (pm_sleep_ops.exit_sleep)
783 pm_sleep_ops.exit_sleep(msm_pm_idle_rs_limits,
784 true, notify_rpm, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700785 }
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600786 msm_pm_timer_exit_idle(timer_halted);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700787 exit_stat = MSM_PM_STAT_IDLE_POWER_COLLAPSE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700788 break;
789 }
790
791 default:
792 __WARN();
793 goto cpuidle_enter_bail;
794 }
795
796 time = ktime_to_ns(ktime_get()) - time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700797 msm_pm_add_stat(exit_stat, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700798
799 do_div(time, 1000);
800 return (int) time;
801
802cpuidle_enter_bail:
803 return 0;
804}
805
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -0600806static struct msm_pm_sleep_status_data *msm_pm_slp_sts;
807
808static DEFINE_PER_CPU_SHARED_ALIGNED(enum msm_pm_sleep_mode,
809 msm_pm_last_slp_mode);
810
811bool msm_pm_verify_cpu_pc(unsigned int cpu)
812{
813 enum msm_pm_sleep_mode mode = per_cpu(msm_pm_last_slp_mode, cpu);
814
815 if (msm_pm_slp_sts)
816 if ((mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE) ||
817 (mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE))
818 return true;
819
820 return false;
821}
822
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700823void msm_pm_cpu_enter_lowpower(unsigned int cpu)
824{
825 int i;
826 bool allow[MSM_PM_SLEEP_MODE_NR];
827
828 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
829 struct msm_pm_platform_data *mode;
830
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600831 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700832 allow[i] = mode->suspend_supported && mode->suspend_enabled;
833 }
834
835 if (MSM_PM_DEBUG_HOTPLUG & msm_pm_debug_mask)
836 pr_notice("CPU%u: %s: shutting down cpu\n", cpu, __func__);
837
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -0600838 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) {
839 per_cpu(msm_pm_last_slp_mode, cpu)
840 = MSM_PM_SLEEP_MODE_POWER_COLLAPSE;
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700841 msm_pm_power_collapse(false);
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -0600842 } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
843 per_cpu(msm_pm_last_slp_mode, cpu)
844 = MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE;
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700845 msm_pm_power_collapse_standalone(false);
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600846 } else if (allow[MSM_PM_SLEEP_MODE_RETENTION]) {
847 per_cpu(msm_pm_last_slp_mode, cpu)
848 = MSM_PM_SLEEP_MODE_RETENTION;
849 msm_pm_retention();
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -0600850 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
851 per_cpu(msm_pm_last_slp_mode, cpu)
Girish Mahadevanc8127052012-04-25 16:23:24 -0600852 = MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT;
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700853 msm_pm_swfi();
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -0600854 } else
855 per_cpu(msm_pm_last_slp_mode, cpu) = MSM_PM_SLEEP_MODE_NR;
856}
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700857
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -0600858int msm_pm_wait_cpu_shutdown(unsigned int cpu)
859{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700860
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -0600861 int timeout = 10;
862
863 if (!msm_pm_slp_sts)
864 return 0;
865
866 while (timeout--) {
867
868 /*
869 * Check for the SPM of the core being hotplugged to set
870 * its sleep state.The SPM sleep state indicates that the
871 * core has been power collapsed.
872 */
873
874 int acc_sts = __raw_readl(msm_pm_slp_sts->base_addr
875 + cpu * msm_pm_slp_sts->cpu_offset);
876 mb();
877
878 if (acc_sts & msm_pm_slp_sts->mask)
879 return 0;
880
881 usleep(100);
882 }
883 pr_warn("%s(): Timed out waiting for CPU %u SPM to enter sleep state",
884 __func__, cpu);
885 return -EBUSY;
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700886}
887
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700888static int msm_pm_enter(suspend_state_t state)
889{
890 bool allow[MSM_PM_SLEEP_MODE_NR];
891 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700892 int64_t period = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530893 int64_t time = msm_pm_timer_enter_suspend(&period);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700894
895 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
896 pr_info("%s\n", __func__);
897
898 if (smp_processor_id()) {
899 __WARN();
900 goto enter_exit;
901 }
902
903
904 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
905 struct msm_pm_platform_data *mode;
906
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600907 mode = &msm_pm_sleep_modes[MSM_PM_MODE(0, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700908 allow[i] = mode->suspend_supported && mode->suspend_enabled;
909 }
910
911 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600912 void *rs_limits = NULL;
913 int ret = -ENODEV;
914 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700915
916 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
917 pr_info("%s: power collapse\n", __func__);
918
919 clock_debug_print_enabled();
920
921#ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE
922 if (msm_pm_sleep_time_override > 0) {
923 int64_t ns = NSEC_PER_SEC *
924 (int64_t) msm_pm_sleep_time_override;
925 msm_pm_set_max_sleep_time(ns);
926 msm_pm_sleep_time_override = 0;
927 }
928#endif /* CONFIG_MSM_SLEEP_TIME_OVERRIDE */
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600929 if (pm_sleep_ops.lowest_limits)
930 rs_limits = pm_sleep_ops.lowest_limits(false,
931 MSM_PM_SLEEP_MODE_POWER_COLLAPSE, -1,
932 -1, &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700933
934 if (rs_limits) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600935 if (pm_sleep_ops.enter_sleep)
936 ret = pm_sleep_ops.enter_sleep(
937 msm_pm_max_sleep_time,
938 rs_limits, false, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700939 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600940 int collapsed = msm_pm_power_collapse(false);
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600941 if (pm_sleep_ops.exit_sleep) {
942 pm_sleep_ops.exit_sleep(rs_limits,
943 false, true, collapsed);
944 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700945 }
946 } else {
947 pr_err("%s: cannot find the lowest power limit\n",
948 __func__);
949 }
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530950 time = msm_pm_timer_exit_suspend(time, period);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700951 msm_pm_add_stat(MSM_PM_STAT_SUSPEND, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700952 } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
953 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
954 pr_info("%s: standalone power collapse\n", __func__);
955 msm_pm_power_collapse_standalone(false);
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600956 } else if (allow[MSM_PM_SLEEP_MODE_RETENTION]) {
957 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
958 pr_info("%s: retention\n", __func__);
959 msm_pm_retention();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700960 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
961 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
962 pr_info("%s: swfi\n", __func__);
963 msm_pm_swfi();
964 }
965
966
967enter_exit:
968 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
969 pr_info("%s: return\n", __func__);
970
971 return 0;
972}
973
974static struct platform_suspend_ops msm_pm_ops = {
975 .enter = msm_pm_enter,
976 .valid = suspend_valid_only_mem,
977};
978
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700979/******************************************************************************
980 * Initialization routine
981 *****************************************************************************/
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -0600982void __init msm_pm_init_sleep_status_data(
983 struct msm_pm_sleep_status_data *data)
984{
985 msm_pm_slp_sts = data;
986}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700987
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600988void msm_pm_set_sleep_ops(struct msm_pm_sleep_ops *ops)
989{
990 if (ops)
991 pm_sleep_ops = *ops;
992}
993
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700994static int __init msm_pm_init(void)
995{
996 pgd_t *pc_pgd;
997 pmd_t *pmd;
998 unsigned long pmdval;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530999 enum msm_pm_time_stats_id enable_stats[] = {
1000 MSM_PM_STAT_IDLE_WFI,
1001 MSM_PM_STAT_RETENTION,
1002 MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE,
1003 MSM_PM_STAT_IDLE_POWER_COLLAPSE,
1004 MSM_PM_STAT_SUSPEND,
1005 };
1006
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001007 /* Page table for cores to come back up safely. */
1008 pc_pgd = pgd_alloc(&init_mm);
1009 if (!pc_pgd)
1010 return -ENOMEM;
1011
1012 pmd = pmd_offset(pc_pgd +
1013 pgd_index(virt_to_phys(msm_pm_collapse_exit)),
1014 virt_to_phys(msm_pm_collapse_exit));
1015 pmdval = (virt_to_phys(msm_pm_collapse_exit) & PGDIR_MASK) |
1016 PMD_TYPE_SECT | PMD_SECT_AP_WRITE;
1017 pmd[0] = __pmd(pmdval);
1018 pmd[1] = __pmd(pmdval + (1 << (PGDIR_SHIFT - 1)));
1019
Steve Mucklefcece052012-02-18 20:09:58 -08001020 msm_saved_state_phys =
1021 allocate_contiguous_ebi_nomap(CPU_SAVED_STATE_SIZE *
1022 num_possible_cpus(), 4);
1023 if (!msm_saved_state_phys)
1024 return -ENOMEM;
1025 msm_saved_state = ioremap_nocache(msm_saved_state_phys,
1026 CPU_SAVED_STATE_SIZE *
1027 num_possible_cpus());
1028 if (!msm_saved_state)
1029 return -ENOMEM;
1030
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001031 /* It is remotely possible that the code in msm_pm_collapse_exit()
1032 * which turns on the MMU with this mapping is in the
1033 * next even-numbered megabyte beyond the
1034 * start of msm_pm_collapse_exit().
1035 * Map this megabyte in as well.
1036 */
1037 pmd[2] = __pmd(pmdval + (2 << (PGDIR_SHIFT - 1)));
1038 flush_pmd_entry(pmd);
1039 msm_pm_pc_pgd = virt_to_phys(pc_pgd);
Steve Muckle730ad7a2012-02-21 15:26:37 -08001040 clean_caches((unsigned long)&msm_pm_pc_pgd, sizeof(msm_pm_pc_pgd),
1041 virt_to_phys(&msm_pm_pc_pgd));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001042
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001043 msm_pm_mode_sysfs_add();
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301044 msm_pm_add_stats(enable_stats, ARRAY_SIZE(enable_stats));
1045
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001046 msm_spm_allow_x_cpu_set_vdd(false);
1047
1048 suspend_set_ops(&msm_pm_ops);
Praveen Chidambaram192979f2012-04-25 18:30:23 -06001049 msm_pm_qtimer_available();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001050 msm_cpuidle_init();
1051
1052 return 0;
1053}
1054
1055late_initcall(msm_pm_init);