blob: 769d74068751adc71c108b3fd8540fae73ce3894 [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"
Jaeseong GIMfc880da2012-08-13 18:47:46 -070045#include "rpm_resources.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{
Anji Jonnalac02367a2012-07-01 02:56:11 +0530594 int64_t time = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530595
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;
Jaeseong GIMfc880da2012-08-13 18:47:46 -0700675
676 if (!dev->cpu && msm_rpm_local_request_is_outstanding()) {
677 allow = false;
678 break;
679 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700680 /* fall through */
681
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600682 case MSM_PM_SLEEP_MODE_RETENTION:
683 if (!allow)
684 break;
685 /* fall through */
686
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700687 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
688 if (!allow)
689 break;
690
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600691 if (pm_sleep_ops.lowest_limits)
692 rs_limits = pm_sleep_ops.lowest_limits(true,
693 mode, latency_us, sleep_us,
694 &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700695
696 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
697 pr_info("CPU%u: %s: %s, latency %uus, "
698 "sleep %uus, limit %p\n",
699 dev->cpu, __func__, state->desc,
700 latency_us, sleep_us, rs_limits);
701
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700702 if (!rs_limits)
703 allow = false;
704 break;
705
706 default:
707 allow = false;
708 break;
709 }
710
711 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
712 pr_info("CPU%u: %s: allow %s: %d\n",
713 dev->cpu, __func__, state->desc, (int)allow);
714
715 if (allow) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700716 if (power < power_usage) {
717 power_usage = power;
718 ret = mode;
719 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700720
721 if (MSM_PM_SLEEP_MODE_POWER_COLLAPSE == mode)
722 msm_pm_idle_rs_limits = rs_limits;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700723 }
724 }
725
Steve Mucklef132c6c2012-06-06 18:30:57 -0700726 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700727}
728
729int msm_pm_idle_enter(enum msm_pm_sleep_mode sleep_mode)
730{
731 int64_t time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700732 int exit_stat;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700733
734 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
735 pr_info("CPU%u: %s: mode %d\n",
736 smp_processor_id(), __func__, sleep_mode);
737
738 time = ktime_to_ns(ktime_get());
739
740 switch (sleep_mode) {
741 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
742 msm_pm_swfi();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700743 exit_stat = MSM_PM_STAT_IDLE_WFI;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700744 break;
745
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600746 case MSM_PM_SLEEP_MODE_RETENTION:
747 msm_pm_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600748 exit_stat = MSM_PM_STAT_RETENTION;
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600749 break;
750
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700751 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
752 msm_pm_power_collapse_standalone(true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700753 exit_stat = MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700754 break;
755
756 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE: {
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600757 int64_t timer_expiration = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700758 bool timer_halted = false;
759 uint32_t sleep_delay;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600760 int ret = -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700761 int notify_rpm =
762 (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600763 int collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700764
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600765 timer_expiration = msm_pm_timer_enter_idle();
766
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700767 sleep_delay = (uint32_t) msm_pm_convert_and_cap_time(
768 timer_expiration, MSM_PM_SLEEP_TICK_LIMIT);
769 if (sleep_delay == 0) /* 0 would mean infinite time */
770 sleep_delay = 1;
771
Karthik Parsha6fb932d2012-01-24 18:04:12 -0800772 if (MSM_PM_DEBUG_IDLE_CLK & msm_pm_debug_mask)
773 clock_debug_print_enabled();
774
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600775 if (pm_sleep_ops.enter_sleep)
776 ret = pm_sleep_ops.enter_sleep(sleep_delay,
777 msm_pm_idle_rs_limits,
778 true, notify_rpm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700779 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600780 collapsed = msm_pm_power_collapse(true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700781 timer_halted = true;
782
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600783 if (pm_sleep_ops.exit_sleep)
784 pm_sleep_ops.exit_sleep(msm_pm_idle_rs_limits,
785 true, notify_rpm, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700786 }
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600787 msm_pm_timer_exit_idle(timer_halted);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700788 exit_stat = MSM_PM_STAT_IDLE_POWER_COLLAPSE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700789 break;
790 }
791
792 default:
793 __WARN();
794 goto cpuidle_enter_bail;
795 }
796
797 time = ktime_to_ns(ktime_get()) - time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700798 msm_pm_add_stat(exit_stat, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700799
800 do_div(time, 1000);
801 return (int) time;
802
803cpuidle_enter_bail:
804 return 0;
805}
806
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700807void msm_pm_cpu_enter_lowpower(unsigned int cpu)
808{
809 int i;
810 bool allow[MSM_PM_SLEEP_MODE_NR];
811
812 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
813 struct msm_pm_platform_data *mode;
814
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600815 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700816 allow[i] = mode->suspend_supported && mode->suspend_enabled;
817 }
818
819 if (MSM_PM_DEBUG_HOTPLUG & msm_pm_debug_mask)
820 pr_notice("CPU%u: %s: shutting down cpu\n", cpu, __func__);
821
Matt Wagantall5375aa72012-07-02 19:59:51 -0700822 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700823 msm_pm_power_collapse(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -0700824 else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700825 msm_pm_power_collapse_standalone(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -0700826 else if (allow[MSM_PM_SLEEP_MODE_RETENTION])
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600827 msm_pm_retention();
Matt Wagantall5375aa72012-07-02 19:59:51 -0700828 else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700829 msm_pm_swfi();
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700830}
831
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700832static int msm_pm_enter(suspend_state_t state)
833{
834 bool allow[MSM_PM_SLEEP_MODE_NR];
835 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700836 int64_t period = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530837 int64_t time = msm_pm_timer_enter_suspend(&period);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700838
839 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
840 pr_info("%s\n", __func__);
841
842 if (smp_processor_id()) {
843 __WARN();
844 goto enter_exit;
845 }
846
847
848 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
849 struct msm_pm_platform_data *mode;
850
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600851 mode = &msm_pm_sleep_modes[MSM_PM_MODE(0, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700852 allow[i] = mode->suspend_supported && mode->suspend_enabled;
853 }
854
855 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600856 void *rs_limits = NULL;
857 int ret = -ENODEV;
858 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700859
860 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
861 pr_info("%s: power collapse\n", __func__);
862
863 clock_debug_print_enabled();
864
865#ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE
866 if (msm_pm_sleep_time_override > 0) {
867 int64_t ns = NSEC_PER_SEC *
868 (int64_t) msm_pm_sleep_time_override;
869 msm_pm_set_max_sleep_time(ns);
870 msm_pm_sleep_time_override = 0;
871 }
872#endif /* CONFIG_MSM_SLEEP_TIME_OVERRIDE */
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600873 if (pm_sleep_ops.lowest_limits)
874 rs_limits = pm_sleep_ops.lowest_limits(false,
875 MSM_PM_SLEEP_MODE_POWER_COLLAPSE, -1,
876 -1, &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700877
878 if (rs_limits) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600879 if (pm_sleep_ops.enter_sleep)
880 ret = pm_sleep_ops.enter_sleep(
881 msm_pm_max_sleep_time,
882 rs_limits, false, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700883 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600884 int collapsed = msm_pm_power_collapse(false);
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600885 if (pm_sleep_ops.exit_sleep) {
886 pm_sleep_ops.exit_sleep(rs_limits,
887 false, true, collapsed);
888 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700889 }
890 } else {
891 pr_err("%s: cannot find the lowest power limit\n",
892 __func__);
893 }
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530894 time = msm_pm_timer_exit_suspend(time, period);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700895 msm_pm_add_stat(MSM_PM_STAT_SUSPEND, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700896 } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
897 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
898 pr_info("%s: standalone power collapse\n", __func__);
899 msm_pm_power_collapse_standalone(false);
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600900 } else if (allow[MSM_PM_SLEEP_MODE_RETENTION]) {
901 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
902 pr_info("%s: retention\n", __func__);
903 msm_pm_retention();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700904 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
905 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
906 pr_info("%s: swfi\n", __func__);
907 msm_pm_swfi();
908 }
909
910
911enter_exit:
912 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
913 pr_info("%s: return\n", __func__);
914
915 return 0;
916}
917
918static struct platform_suspend_ops msm_pm_ops = {
919 .enter = msm_pm_enter,
920 .valid = suspend_valid_only_mem,
921};
922
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700923/******************************************************************************
924 * Initialization routine
925 *****************************************************************************/
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600926void msm_pm_set_sleep_ops(struct msm_pm_sleep_ops *ops)
927{
928 if (ops)
929 pm_sleep_ops = *ops;
930}
931
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700932static int __init msm_pm_init(void)
933{
934 pgd_t *pc_pgd;
935 pmd_t *pmd;
936 unsigned long pmdval;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530937 enum msm_pm_time_stats_id enable_stats[] = {
938 MSM_PM_STAT_IDLE_WFI,
939 MSM_PM_STAT_RETENTION,
940 MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE,
941 MSM_PM_STAT_IDLE_POWER_COLLAPSE,
942 MSM_PM_STAT_SUSPEND,
943 };
Steve Mucklef132c6c2012-06-06 18:30:57 -0700944 unsigned long exit_phys;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530945
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700946 /* Page table for cores to come back up safely. */
947 pc_pgd = pgd_alloc(&init_mm);
948 if (!pc_pgd)
949 return -ENOMEM;
950
Steve Mucklef132c6c2012-06-06 18:30:57 -0700951 exit_phys = virt_to_phys(msm_pm_collapse_exit);
952
953 pmd = pmd_offset(pud_offset(pc_pgd + pgd_index(exit_phys),exit_phys),
954 exit_phys);
955 pmdval = (exit_phys & PGDIR_MASK) |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700956 PMD_TYPE_SECT | PMD_SECT_AP_WRITE;
957 pmd[0] = __pmd(pmdval);
958 pmd[1] = __pmd(pmdval + (1 << (PGDIR_SHIFT - 1)));
959
Steve Mucklefcece052012-02-18 20:09:58 -0800960 msm_saved_state_phys =
961 allocate_contiguous_ebi_nomap(CPU_SAVED_STATE_SIZE *
962 num_possible_cpus(), 4);
963 if (!msm_saved_state_phys)
964 return -ENOMEM;
965 msm_saved_state = ioremap_nocache(msm_saved_state_phys,
966 CPU_SAVED_STATE_SIZE *
967 num_possible_cpus());
968 if (!msm_saved_state)
969 return -ENOMEM;
970
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700971 /* It is remotely possible that the code in msm_pm_collapse_exit()
972 * which turns on the MMU with this mapping is in the
973 * next even-numbered megabyte beyond the
974 * start of msm_pm_collapse_exit().
975 * Map this megabyte in as well.
976 */
977 pmd[2] = __pmd(pmdval + (2 << (PGDIR_SHIFT - 1)));
978 flush_pmd_entry(pmd);
979 msm_pm_pc_pgd = virt_to_phys(pc_pgd);
Steve Muckle730ad7a2012-02-21 15:26:37 -0800980 clean_caches((unsigned long)&msm_pm_pc_pgd, sizeof(msm_pm_pc_pgd),
981 virt_to_phys(&msm_pm_pc_pgd));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700982
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700983 msm_pm_mode_sysfs_add();
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530984 msm_pm_add_stats(enable_stats, ARRAY_SIZE(enable_stats));
985
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700986 suspend_set_ops(&msm_pm_ops);
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600987 msm_pm_qtimer_available();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700988 msm_cpuidle_init();
989
990 return 0;
991}
992
993late_initcall(msm_pm_init);