blob: e2036673645673e2c6d5a150202a7e6610e41699 [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>
30#include <asm/cacheflush.h>
31#include <asm/hardware/gic.h>
32#include <asm/pgtable.h>
33#include <asm/pgalloc.h>
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -060034#include <asm/hardware/cache-l2x0.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070035#ifdef CONFIG_VFP
36#include <asm/vfp.h>
37#endif
38
39#include "acpuclock.h"
40#include "clock.h"
41#include "avs.h"
Abhijeet Dharmapurikarefaca4f2011-12-27 16:24:07 -080042#include <mach/cpuidle.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070043#include "idle.h"
Matt Wagantall7cca4642012-02-01 16:43:24 -080044#include "pm.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070045#include "scm-boot.h"
46#include "spm.h"
47#include "timer.h"
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -060048#include "pm-boot.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070049
50/******************************************************************************
51 * Debug Definitions
52 *****************************************************************************/
53
54enum {
55 MSM_PM_DEBUG_SUSPEND = BIT(0),
56 MSM_PM_DEBUG_POWER_COLLAPSE = BIT(1),
57 MSM_PM_DEBUG_SUSPEND_LIMITS = BIT(2),
58 MSM_PM_DEBUG_CLOCK = BIT(3),
59 MSM_PM_DEBUG_RESET_VECTOR = BIT(4),
Karthik Parsha6fb932d2012-01-24 18:04:12 -080060 MSM_PM_DEBUG_IDLE_CLK = BIT(5),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070061 MSM_PM_DEBUG_IDLE = BIT(6),
62 MSM_PM_DEBUG_IDLE_LIMITS = BIT(7),
63 MSM_PM_DEBUG_HOTPLUG = BIT(8),
64};
65
66static int msm_pm_debug_mask = 1;
67module_param_named(
68 debug_mask, msm_pm_debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP
69);
70
71
72/******************************************************************************
73 * Sleep Modes and Parameters
74 *****************************************************************************/
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070075enum {
76 MSM_PM_MODE_ATTR_SUSPEND,
77 MSM_PM_MODE_ATTR_IDLE,
78 MSM_PM_MODE_ATTR_NR,
79};
80
81static char *msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_NR] = {
82 [MSM_PM_MODE_ATTR_SUSPEND] = "suspend_enabled",
83 [MSM_PM_MODE_ATTR_IDLE] = "idle_enabled",
84};
85
86struct msm_pm_kobj_attribute {
87 unsigned int cpu;
88 struct kobj_attribute ka;
89};
90
91#define GET_CPU_OF_ATTR(attr) \
92 (container_of(attr, struct msm_pm_kobj_attribute, ka)->cpu)
93
94struct msm_pm_sysfs_sleep_mode {
95 struct kobject *kobj;
96 struct attribute_group attr_group;
97 struct attribute *attrs[MSM_PM_MODE_ATTR_NR + 1];
98 struct msm_pm_kobj_attribute kas[MSM_PM_MODE_ATTR_NR];
99};
100
101static char *msm_pm_sleep_mode_labels[MSM_PM_SLEEP_MODE_NR] = {
102 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE] = "power_collapse",
103 [MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT] = "wfi",
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600104 [MSM_PM_SLEEP_MODE_RETENTION] = "retention",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700105 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE] =
106 "standalone_power_collapse",
107};
108
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600109static struct msm_pm_sleep_ops pm_sleep_ops;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700110/*
111 * Write out the attribute.
112 */
113static ssize_t msm_pm_mode_attr_show(
114 struct kobject *kobj, struct kobj_attribute *attr, char *buf)
115{
116 int ret = -EINVAL;
117 int i;
118
119 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
120 struct kernel_param kp;
121 unsigned int cpu;
122 struct msm_pm_platform_data *mode;
123
124 if (msm_pm_sleep_mode_labels[i] == NULL)
125 continue;
126
127 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
128 continue;
129
130 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600131 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700132
133 if (!strcmp(attr->attr.name,
134 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
135 u32 arg = mode->suspend_enabled;
136 kp.arg = &arg;
137 ret = param_get_ulong(buf, &kp);
138 } else if (!strcmp(attr->attr.name,
139 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
140 u32 arg = mode->idle_enabled;
141 kp.arg = &arg;
142 ret = param_get_ulong(buf, &kp);
143 }
144
145 break;
146 }
147
148 if (ret > 0) {
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600149 strlcat(buf, "\n", PAGE_SIZE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700150 ret++;
151 }
152
153 return ret;
154}
155
156/*
157 * Read in the new attribute value.
158 */
159static ssize_t msm_pm_mode_attr_store(struct kobject *kobj,
160 struct kobj_attribute *attr, const char *buf, size_t count)
161{
162 int ret = -EINVAL;
163 int i;
164
165 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
166 struct kernel_param kp;
167 unsigned int cpu;
168 struct msm_pm_platform_data *mode;
169
170 if (msm_pm_sleep_mode_labels[i] == NULL)
171 continue;
172
173 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
174 continue;
175
176 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600177 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700178
179 if (!strcmp(attr->attr.name,
180 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
181 kp.arg = &mode->suspend_enabled;
182 ret = param_set_byte(buf, &kp);
183 } else if (!strcmp(attr->attr.name,
184 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
185 kp.arg = &mode->idle_enabled;
186 ret = param_set_byte(buf, &kp);
187 }
188
189 break;
190 }
191
192 return ret ? ret : count;
193}
194
195/*
196 * Add sysfs entries for one cpu.
197 */
198static int __init msm_pm_mode_sysfs_add_cpu(
199 unsigned int cpu, struct kobject *modes_kobj)
200{
201 char cpu_name[8];
202 struct kobject *cpu_kobj;
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600203 struct msm_pm_sysfs_sleep_mode *mode = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700204 int i, j, k;
205 int ret;
206
207 snprintf(cpu_name, sizeof(cpu_name), "cpu%u", cpu);
208 cpu_kobj = kobject_create_and_add(cpu_name, modes_kobj);
209 if (!cpu_kobj) {
210 pr_err("%s: cannot create %s kobject\n", __func__, cpu_name);
211 ret = -ENOMEM;
212 goto mode_sysfs_add_cpu_exit;
213 }
214
215 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
216 int idx = MSM_PM_MODE(cpu, i);
217
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600218 if ((!msm_pm_sleep_modes[idx].suspend_supported)
219 && (!msm_pm_sleep_modes[idx].idle_supported))
220 continue;
221
222 if (!msm_pm_sleep_mode_labels[i] ||
223 !msm_pm_sleep_mode_labels[i][0])
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700224 continue;
225
226 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
227 if (!mode) {
228 pr_err("%s: cannot allocate memory for attributes\n",
229 __func__);
230 ret = -ENOMEM;
231 goto mode_sysfs_add_cpu_exit;
232 }
233
234 mode->kobj = kobject_create_and_add(
235 msm_pm_sleep_mode_labels[i], cpu_kobj);
236 if (!mode->kobj) {
237 pr_err("%s: cannot create kobject\n", __func__);
238 ret = -ENOMEM;
239 goto mode_sysfs_add_cpu_exit;
240 }
241
242 for (k = 0, j = 0; k < MSM_PM_MODE_ATTR_NR; k++) {
243 if ((k == MSM_PM_MODE_ATTR_IDLE) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600244 !msm_pm_sleep_modes[idx].idle_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700245 continue;
246 if ((k == MSM_PM_MODE_ATTR_SUSPEND) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600247 !msm_pm_sleep_modes[idx].suspend_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700248 continue;
Stephen Boydd2059c32012-07-03 15:11:15 -0700249 sysfs_attr_init(&mode->kas[j].ka.attr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700250 mode->kas[j].cpu = cpu;
251 mode->kas[j].ka.attr.mode = 0644;
252 mode->kas[j].ka.show = msm_pm_mode_attr_show;
253 mode->kas[j].ka.store = msm_pm_mode_attr_store;
254 mode->kas[j].ka.attr.name = msm_pm_mode_attr_labels[k];
255 mode->attrs[j] = &mode->kas[j].ka.attr;
256 j++;
257 }
258 mode->attrs[j] = NULL;
259
260 mode->attr_group.attrs = mode->attrs;
261 ret = sysfs_create_group(mode->kobj, &mode->attr_group);
262 if (ret) {
263 pr_err("%s: cannot create kobject attribute group\n",
264 __func__);
265 goto mode_sysfs_add_cpu_exit;
266 }
267 }
268
269 ret = 0;
270
271mode_sysfs_add_cpu_exit:
Praveen Chidambaramd5ac2d32011-10-24 14:30:27 -0600272 if (ret) {
Praveen Chidambaram2cfda632011-10-11 16:58:09 -0600273 if (mode && mode->kobj)
274 kobject_del(mode->kobj);
275 kfree(mode);
276 }
277
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700278 return ret;
279}
280
281/*
282 * Add sysfs entries for the sleep modes.
283 */
284static int __init msm_pm_mode_sysfs_add(void)
285{
286 struct kobject *module_kobj;
287 struct kobject *modes_kobj;
288 unsigned int cpu;
289 int ret;
290
291 module_kobj = kset_find_obj(module_kset, KBUILD_MODNAME);
292 if (!module_kobj) {
293 pr_err("%s: cannot find kobject for module %s\n",
294 __func__, KBUILD_MODNAME);
295 ret = -ENOENT;
296 goto mode_sysfs_add_exit;
297 }
298
299 modes_kobj = kobject_create_and_add("modes", module_kobj);
300 if (!modes_kobj) {
301 pr_err("%s: cannot create modes kobject\n", __func__);
302 ret = -ENOMEM;
303 goto mode_sysfs_add_exit;
304 }
305
306 for_each_possible_cpu(cpu) {
307 ret = msm_pm_mode_sysfs_add_cpu(cpu, modes_kobj);
308 if (ret)
309 goto mode_sysfs_add_exit;
310 }
311
312 ret = 0;
313
314mode_sysfs_add_exit:
315 return ret;
316}
317
318/******************************************************************************
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700319 * Configure Hardware before/after Low Power Mode
320 *****************************************************************************/
321
322/*
323 * Configure hardware registers in preparation for Apps power down.
324 */
325static void msm_pm_config_hw_before_power_down(void)
326{
327 return;
328}
329
330/*
331 * Clear hardware registers after Apps powers up.
332 */
333static void msm_pm_config_hw_after_power_up(void)
334{
335 return;
336}
337
338/*
339 * Configure hardware registers in preparation for SWFI.
340 */
341static void msm_pm_config_hw_before_swfi(void)
342{
343 return;
344}
345
346
347/******************************************************************************
348 * Suspend Max Sleep Time
349 *****************************************************************************/
350
351#ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE
352static int msm_pm_sleep_time_override;
353module_param_named(sleep_time_override,
354 msm_pm_sleep_time_override, int, S_IRUGO | S_IWUSR | S_IWGRP);
355#endif
356
357#define SCLK_HZ (32768)
358#define MSM_PM_SLEEP_TICK_LIMIT (0x6DDD000)
359
360static uint32_t msm_pm_max_sleep_time;
361
362/*
363 * Convert time from nanoseconds to slow clock ticks, then cap it to the
364 * specified limit
365 */
366static int64_t msm_pm_convert_and_cap_time(int64_t time_ns, int64_t limit)
367{
368 do_div(time_ns, NSEC_PER_SEC / SCLK_HZ);
369 return (time_ns > limit) ? limit : time_ns;
370}
371
372/*
373 * Set the sleep time for suspend. 0 means infinite sleep time.
374 */
375void msm_pm_set_max_sleep_time(int64_t max_sleep_time_ns)
376{
377 if (max_sleep_time_ns == 0) {
378 msm_pm_max_sleep_time = 0;
379 } else {
380 msm_pm_max_sleep_time = (uint32_t)msm_pm_convert_and_cap_time(
381 max_sleep_time_ns, MSM_PM_SLEEP_TICK_LIMIT);
382
383 if (msm_pm_max_sleep_time == 0)
384 msm_pm_max_sleep_time = 1;
385 }
386
387 if (msm_pm_debug_mask & MSM_PM_DEBUG_SUSPEND)
388 pr_info("%s: Requested %lld ns Giving %u sclk ticks\n",
389 __func__, max_sleep_time_ns, msm_pm_max_sleep_time);
390}
391EXPORT_SYMBOL(msm_pm_set_max_sleep_time);
392
393
394/******************************************************************************
395 *
396 *****************************************************************************/
397
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600398static void *msm_pm_idle_rs_limits;
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600399static bool msm_pm_use_qtimer;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700400
401static void msm_pm_swfi(void)
402{
403 msm_pm_config_hw_before_swfi();
404 msm_arch_idle();
405}
406
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600407
408static void msm_pm_retention(void)
409{
410 int ret = 0;
411
412 msm_pm_config_hw_before_swfi();
413 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_POWER_RETENTION, false);
414 WARN_ON(ret);
415 msm_arch_idle();
416 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
417 WARN_ON(ret);
418}
419
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600420#ifdef CONFIG_CACHE_L2X0
421static inline bool msm_pm_l2x0_power_collapse(void)
422{
423 bool collapsed = 0;
424
Taniya Das38a8c6e2012-05-09 20:34:39 +0530425 l2cc_suspend();
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600426 collapsed = msm_pm_collapse();
Taniya Das38a8c6e2012-05-09 20:34:39 +0530427 l2cc_resume();
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600428
429 return collapsed;
430}
431#else
432static inline bool msm_pm_l2x0_power_collapse(void)
433{
434 return msm_pm_collapse();
435}
436#endif
437
Stephen Boydb29750d2012-02-21 01:21:32 -0800438static bool __ref msm_pm_spm_power_collapse(
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700439 unsigned int cpu, bool from_idle, bool notify_rpm)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700440{
441 void *entry;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600442 bool collapsed = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700443 int ret;
Rohit Vaswanie78dfb62012-02-21 10:29:29 -0800444 unsigned int saved_gic_cpu_ctrl;
445
446 saved_gic_cpu_ctrl = readl_relaxed(MSM_QGIC_CPU_BASE + GIC_CPU_CTRL);
447 mb();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700448
449 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
450 pr_info("CPU%u: %s: notify_rpm %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700451 cpu, __func__, (int) notify_rpm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700452
453 ret = msm_spm_set_low_power_mode(
454 MSM_SPM_MODE_POWER_COLLAPSE, notify_rpm);
455 WARN_ON(ret);
456
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700457 entry = (!cpu || from_idle) ?
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700458 msm_pm_collapse_exit : msm_secondary_startup;
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700459 msm_pm_boot_config_before_pc(cpu, virt_to_phys(entry));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700460
461 if (MSM_PM_DEBUG_RESET_VECTOR & msm_pm_debug_mask)
462 pr_info("CPU%u: %s: program vector to %p\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700463 cpu, __func__, entry);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700464
465#ifdef CONFIG_VFP
Steve Mucklef132c6c2012-06-06 18:30:57 -0700466 vfp_pm_suspend();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700467#endif
468
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600469 collapsed = msm_pm_l2x0_power_collapse();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700470
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700471 msm_pm_boot_config_after_pc(cpu);
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -0600472
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700473 if (collapsed) {
474#ifdef CONFIG_VFP
Steve Mucklef132c6c2012-06-06 18:30:57 -0700475 vfp_pm_resume();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700476#endif
477 cpu_init();
478 writel(0xF0, MSM_QGIC_CPU_BASE + GIC_CPU_PRIMASK);
Rohit Vaswanie78dfb62012-02-21 10:29:29 -0800479 writel_relaxed(saved_gic_cpu_ctrl,
480 MSM_QGIC_CPU_BASE + GIC_CPU_CTRL);
481 mb();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700482 local_fiq_enable();
483 }
484
485 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
486 pr_info("CPU%u: %s: msm_pm_collapse returned, collapsed %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700487 cpu, __func__, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700488
489 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
490 WARN_ON(ret);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600491 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700492}
493
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600494static bool msm_pm_power_collapse_standalone(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700495{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700496 unsigned int cpu = smp_processor_id();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700497 unsigned int avsdscr_setting;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600498 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700499
500 avsdscr_setting = avs_get_avsdscr();
501 avs_disable();
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700502 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700503 avs_reset_delays(avsdscr_setting);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600504 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700505}
506
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600507static bool msm_pm_power_collapse(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700508{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700509 unsigned int cpu = smp_processor_id();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700510 unsigned long saved_acpuclk_rate;
511 unsigned int avsdscr_setting;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600512 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700513
514 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
515 pr_info("CPU%u: %s: idle %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700516 cpu, __func__, (int)from_idle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700517
518 msm_pm_config_hw_before_power_down();
519 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700520 pr_info("CPU%u: %s: pre power down\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700521
522 avsdscr_setting = avs_get_avsdscr();
523 avs_disable();
524
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700525 if (cpu_online(cpu))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700526 saved_acpuclk_rate = acpuclk_power_collapse();
527 else
528 saved_acpuclk_rate = 0;
529
530 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
531 pr_info("CPU%u: %s: change clock rate (old rate = %lu)\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700532 cpu, __func__, saved_acpuclk_rate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700533
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700534 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700535
Girish Mahadevan4e025d92012-02-29 13:26:51 -0700536 if (cpu_online(cpu)) {
537 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
538 pr_info("CPU%u: %s: restore clock rate to %lu\n",
539 cpu, __func__, saved_acpuclk_rate);
540 if (acpuclk_set_rate(cpu, saved_acpuclk_rate, SETRATE_PC) < 0)
541 pr_err("CPU%u: %s: failed to restore clock rate(%lu)\n",
542 cpu, __func__, saved_acpuclk_rate);
543 } else {
544 unsigned int gic_dist_enabled;
545 unsigned int gic_dist_pending;
546 gic_dist_enabled = readl_relaxed(
547 MSM_QGIC_DIST_BASE + GIC_DIST_ENABLE_CLEAR);
548 gic_dist_pending = readl_relaxed(
549 MSM_QGIC_DIST_BASE + GIC_DIST_PENDING_SET);
550 mb();
551 gic_dist_pending &= gic_dist_enabled;
552
553 if (gic_dist_pending)
554 pr_err("CPU %d interrupted during hotplug.Pending int 0x%x\n",
555 cpu, gic_dist_pending);
556 }
557
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700558
559 avs_reset_delays(avsdscr_setting);
560 msm_pm_config_hw_after_power_up();
561 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700562 pr_info("CPU%u: %s: post power up\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700563
564 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700565 pr_info("CPU%u: %s: return\n", cpu, __func__);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600566 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700567}
568
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600569static void msm_pm_qtimer_available(void)
570{
Abhimanyu Kapur90ced6e2012-06-26 17:41:25 -0700571 if (machine_is_msm8974())
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600572 msm_pm_use_qtimer = true;
573}
574
575static int64_t msm_pm_timer_enter_idle(void)
576{
577 if (msm_pm_use_qtimer)
578 return ktime_to_ns(tick_nohz_get_sleep_length());
579
580 return msm_timer_enter_idle();
581}
582
583static void msm_pm_timer_exit_idle(bool timer_halted)
584{
585 if (msm_pm_use_qtimer)
586 return;
587
588 msm_timer_exit_idle((int) timer_halted);
589}
590
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530591static int64_t msm_pm_timer_enter_suspend(int64_t *period)
592{
Anji Jonnalac02367a2012-07-01 02:56:11 +0530593 int64_t time = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530594
595 if (msm_pm_use_qtimer)
596 return sched_clock();
597
598 time = msm_timer_get_sclk_time(period);
599 if (!time)
600 pr_err("%s: Unable to read sclk.\n", __func__);
601
602 return time;
603}
604
605static int64_t msm_pm_timer_exit_suspend(int64_t time, int64_t period)
606{
607 if (msm_pm_use_qtimer)
608 return sched_clock() - time;
609
610 if (time != 0) {
611 int64_t end_time = msm_timer_get_sclk_time(NULL);
612 if (end_time != 0) {
613 time = end_time - time;
614 if (time < 0)
615 time += period;
616 } else
617 time = 0;
618 }
619
620 return time;
621}
622
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700623/******************************************************************************
624 * External Idle/Suspend Functions
625 *****************************************************************************/
626
627void arch_idle(void)
628{
629 return;
630}
631
Steve Mucklef132c6c2012-06-06 18:30:57 -0700632int msm_pm_idle_prepare(struct cpuidle_device *dev,
633 struct cpuidle_driver *drv, int index)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700634{
635 uint32_t latency_us;
636 uint32_t sleep_us;
637 int i;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700638 unsigned int power_usage = -1;
639 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700640
641 latency_us = (uint32_t) pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
642 sleep_us = (uint32_t) ktime_to_ns(tick_nohz_get_sleep_length());
643 sleep_us = DIV_ROUND_UP(sleep_us, 1000);
644
645 for (i = 0; i < dev->state_count; i++) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700646 struct cpuidle_state *state = &drv->states[i];
647 struct cpuidle_state_usage *st_usage = &dev->states_usage[i];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700648 enum msm_pm_sleep_mode mode;
649 bool allow;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600650 void *rs_limits = NULL;
651 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700652 int idx;
653
Steve Mucklef132c6c2012-06-06 18:30:57 -0700654 mode = (enum msm_pm_sleep_mode) cpuidle_get_statedata(st_usage);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700655 idx = MSM_PM_MODE(dev->cpu, mode);
656
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600657 allow = msm_pm_sleep_modes[idx].idle_enabled &&
658 msm_pm_sleep_modes[idx].idle_supported;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700659
660 switch (mode) {
661 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
662 if (!allow)
663 break;
664
665 if (num_online_cpus() > 1) {
666 allow = false;
667 break;
668 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700669 /* fall through */
670
671 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
672 if (!allow)
673 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700674 /* fall through */
675
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600676 case MSM_PM_SLEEP_MODE_RETENTION:
677 if (!allow)
678 break;
679 /* fall through */
680
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700681 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
682 if (!allow)
683 break;
684
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600685 if (pm_sleep_ops.lowest_limits)
686 rs_limits = pm_sleep_ops.lowest_limits(true,
687 mode, latency_us, sleep_us,
688 &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700689
690 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
691 pr_info("CPU%u: %s: %s, latency %uus, "
692 "sleep %uus, limit %p\n",
693 dev->cpu, __func__, state->desc,
694 latency_us, sleep_us, rs_limits);
695
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700696 if (!rs_limits)
697 allow = false;
698 break;
699
700 default:
701 allow = false;
702 break;
703 }
704
705 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
706 pr_info("CPU%u: %s: allow %s: %d\n",
707 dev->cpu, __func__, state->desc, (int)allow);
708
709 if (allow) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700710 if (power < power_usage) {
711 power_usage = power;
712 ret = mode;
713 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700714
715 if (MSM_PM_SLEEP_MODE_POWER_COLLAPSE == mode)
716 msm_pm_idle_rs_limits = rs_limits;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700717 }
718 }
719
Steve Mucklef132c6c2012-06-06 18:30:57 -0700720 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700721}
722
723int msm_pm_idle_enter(enum msm_pm_sleep_mode sleep_mode)
724{
725 int64_t time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700726 int exit_stat;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700727
728 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
729 pr_info("CPU%u: %s: mode %d\n",
730 smp_processor_id(), __func__, sleep_mode);
731
732 time = ktime_to_ns(ktime_get());
733
734 switch (sleep_mode) {
735 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
736 msm_pm_swfi();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700737 exit_stat = MSM_PM_STAT_IDLE_WFI;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700738 break;
739
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600740 case MSM_PM_SLEEP_MODE_RETENTION:
741 msm_pm_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600742 exit_stat = MSM_PM_STAT_RETENTION;
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600743 break;
744
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700745 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
746 msm_pm_power_collapse_standalone(true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700747 exit_stat = MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700748 break;
749
750 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE: {
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600751 int64_t timer_expiration = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700752 bool timer_halted = false;
753 uint32_t sleep_delay;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600754 int ret = -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700755 int notify_rpm =
756 (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600757 int collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700758
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600759 timer_expiration = msm_pm_timer_enter_idle();
760
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700761 sleep_delay = (uint32_t) msm_pm_convert_and_cap_time(
762 timer_expiration, MSM_PM_SLEEP_TICK_LIMIT);
763 if (sleep_delay == 0) /* 0 would mean infinite time */
764 sleep_delay = 1;
765
Karthik Parsha6fb932d2012-01-24 18:04:12 -0800766 if (MSM_PM_DEBUG_IDLE_CLK & msm_pm_debug_mask)
767 clock_debug_print_enabled();
768
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600769 if (pm_sleep_ops.enter_sleep)
770 ret = pm_sleep_ops.enter_sleep(sleep_delay,
771 msm_pm_idle_rs_limits,
772 true, notify_rpm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700773 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600774 collapsed = msm_pm_power_collapse(true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700775 timer_halted = true;
776
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600777 if (pm_sleep_ops.exit_sleep)
778 pm_sleep_ops.exit_sleep(msm_pm_idle_rs_limits,
779 true, notify_rpm, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700780 }
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600781 msm_pm_timer_exit_idle(timer_halted);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700782 exit_stat = MSM_PM_STAT_IDLE_POWER_COLLAPSE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700783 break;
784 }
785
786 default:
787 __WARN();
788 goto cpuidle_enter_bail;
789 }
790
791 time = ktime_to_ns(ktime_get()) - time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700792 msm_pm_add_stat(exit_stat, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700793
794 do_div(time, 1000);
795 return (int) time;
796
797cpuidle_enter_bail:
798 return 0;
799}
800
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700801void msm_pm_cpu_enter_lowpower(unsigned int cpu)
802{
803 int i;
804 bool allow[MSM_PM_SLEEP_MODE_NR];
805
806 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
807 struct msm_pm_platform_data *mode;
808
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600809 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700810 allow[i] = mode->suspend_supported && mode->suspend_enabled;
811 }
812
813 if (MSM_PM_DEBUG_HOTPLUG & msm_pm_debug_mask)
814 pr_notice("CPU%u: %s: shutting down cpu\n", cpu, __func__);
815
Matt Wagantall5375aa72012-07-02 19:59:51 -0700816 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700817 msm_pm_power_collapse(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -0700818 else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700819 msm_pm_power_collapse_standalone(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -0700820 else if (allow[MSM_PM_SLEEP_MODE_RETENTION])
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600821 msm_pm_retention();
Matt Wagantall5375aa72012-07-02 19:59:51 -0700822 else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700823 msm_pm_swfi();
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700824}
825
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700826static int msm_pm_enter(suspend_state_t state)
827{
828 bool allow[MSM_PM_SLEEP_MODE_NR];
829 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700830 int64_t period = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530831 int64_t time = msm_pm_timer_enter_suspend(&period);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700832
833 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
834 pr_info("%s\n", __func__);
835
836 if (smp_processor_id()) {
837 __WARN();
838 goto enter_exit;
839 }
840
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(0, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700846 allow[i] = mode->suspend_supported && mode->suspend_enabled;
847 }
848
849 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600850 void *rs_limits = NULL;
851 int ret = -ENODEV;
852 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700853
854 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
855 pr_info("%s: power collapse\n", __func__);
856
857 clock_debug_print_enabled();
858
859#ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE
860 if (msm_pm_sleep_time_override > 0) {
861 int64_t ns = NSEC_PER_SEC *
862 (int64_t) msm_pm_sleep_time_override;
863 msm_pm_set_max_sleep_time(ns);
864 msm_pm_sleep_time_override = 0;
865 }
866#endif /* CONFIG_MSM_SLEEP_TIME_OVERRIDE */
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600867 if (pm_sleep_ops.lowest_limits)
868 rs_limits = pm_sleep_ops.lowest_limits(false,
869 MSM_PM_SLEEP_MODE_POWER_COLLAPSE, -1,
870 -1, &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700871
872 if (rs_limits) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600873 if (pm_sleep_ops.enter_sleep)
874 ret = pm_sleep_ops.enter_sleep(
875 msm_pm_max_sleep_time,
876 rs_limits, false, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700877 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600878 int collapsed = msm_pm_power_collapse(false);
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600879 if (pm_sleep_ops.exit_sleep) {
880 pm_sleep_ops.exit_sleep(rs_limits,
881 false, true, collapsed);
882 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700883 }
884 } else {
885 pr_err("%s: cannot find the lowest power limit\n",
886 __func__);
887 }
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530888 time = msm_pm_timer_exit_suspend(time, period);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700889 msm_pm_add_stat(MSM_PM_STAT_SUSPEND, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700890 } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
891 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
892 pr_info("%s: standalone power collapse\n", __func__);
893 msm_pm_power_collapse_standalone(false);
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600894 } else if (allow[MSM_PM_SLEEP_MODE_RETENTION]) {
895 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
896 pr_info("%s: retention\n", __func__);
897 msm_pm_retention();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700898 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
899 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
900 pr_info("%s: swfi\n", __func__);
901 msm_pm_swfi();
902 }
903
904
905enter_exit:
906 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
907 pr_info("%s: return\n", __func__);
908
909 return 0;
910}
911
912static struct platform_suspend_ops msm_pm_ops = {
913 .enter = msm_pm_enter,
914 .valid = suspend_valid_only_mem,
915};
916
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700917/******************************************************************************
918 * Initialization routine
919 *****************************************************************************/
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600920void msm_pm_set_sleep_ops(struct msm_pm_sleep_ops *ops)
921{
922 if (ops)
923 pm_sleep_ops = *ops;
924}
925
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700926static int __init msm_pm_init(void)
927{
928 pgd_t *pc_pgd;
929 pmd_t *pmd;
930 unsigned long pmdval;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530931 enum msm_pm_time_stats_id enable_stats[] = {
932 MSM_PM_STAT_IDLE_WFI,
933 MSM_PM_STAT_RETENTION,
934 MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE,
935 MSM_PM_STAT_IDLE_POWER_COLLAPSE,
936 MSM_PM_STAT_SUSPEND,
937 };
Steve Mucklef132c6c2012-06-06 18:30:57 -0700938 unsigned long exit_phys;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530939
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700940 /* Page table for cores to come back up safely. */
941 pc_pgd = pgd_alloc(&init_mm);
942 if (!pc_pgd)
943 return -ENOMEM;
944
Steve Mucklef132c6c2012-06-06 18:30:57 -0700945 exit_phys = virt_to_phys(msm_pm_collapse_exit);
946
947 pmd = pmd_offset(pud_offset(pc_pgd + pgd_index(exit_phys),exit_phys),
948 exit_phys);
949 pmdval = (exit_phys & PGDIR_MASK) |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700950 PMD_TYPE_SECT | PMD_SECT_AP_WRITE;
951 pmd[0] = __pmd(pmdval);
952 pmd[1] = __pmd(pmdval + (1 << (PGDIR_SHIFT - 1)));
953
Steve Mucklefcece052012-02-18 20:09:58 -0800954 msm_saved_state_phys =
955 allocate_contiguous_ebi_nomap(CPU_SAVED_STATE_SIZE *
956 num_possible_cpus(), 4);
957 if (!msm_saved_state_phys)
958 return -ENOMEM;
959 msm_saved_state = ioremap_nocache(msm_saved_state_phys,
960 CPU_SAVED_STATE_SIZE *
961 num_possible_cpus());
962 if (!msm_saved_state)
963 return -ENOMEM;
964
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700965 /* It is remotely possible that the code in msm_pm_collapse_exit()
966 * which turns on the MMU with this mapping is in the
967 * next even-numbered megabyte beyond the
968 * start of msm_pm_collapse_exit().
969 * Map this megabyte in as well.
970 */
971 pmd[2] = __pmd(pmdval + (2 << (PGDIR_SHIFT - 1)));
972 flush_pmd_entry(pmd);
973 msm_pm_pc_pgd = virt_to_phys(pc_pgd);
Steve Muckle730ad7a2012-02-21 15:26:37 -0800974 clean_caches((unsigned long)&msm_pm_pc_pgd, sizeof(msm_pm_pc_pgd),
975 virt_to_phys(&msm_pm_pc_pgd));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700976
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700977 msm_pm_mode_sysfs_add();
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530978 msm_pm_add_stats(enable_stats, ARRAY_SIZE(enable_stats));
979
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700980 suspend_set_ops(&msm_pm_ops);
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600981 msm_pm_qtimer_available();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700982 msm_cpuidle_init();
983
984 return 0;
985}
986
987late_initcall(msm_pm_init);