blob: 070e2c555c02d7e786ec4af3acdb3f39316feae1 [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 "rpm_resources.h"
48#include "scm-boot.h"
49#include "spm.h"
50#include "timer.h"
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -060051#include "pm-boot.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070052
53/******************************************************************************
54 * Debug Definitions
55 *****************************************************************************/
56
57enum {
58 MSM_PM_DEBUG_SUSPEND = BIT(0),
59 MSM_PM_DEBUG_POWER_COLLAPSE = BIT(1),
60 MSM_PM_DEBUG_SUSPEND_LIMITS = BIT(2),
61 MSM_PM_DEBUG_CLOCK = BIT(3),
62 MSM_PM_DEBUG_RESET_VECTOR = BIT(4),
Karthik Parsha6fb932d2012-01-24 18:04:12 -080063 MSM_PM_DEBUG_IDLE_CLK = BIT(5),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070064 MSM_PM_DEBUG_IDLE = BIT(6),
65 MSM_PM_DEBUG_IDLE_LIMITS = BIT(7),
66 MSM_PM_DEBUG_HOTPLUG = BIT(8),
67};
68
69static int msm_pm_debug_mask = 1;
70module_param_named(
71 debug_mask, msm_pm_debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP
72);
73
74
75/******************************************************************************
76 * Sleep Modes and Parameters
77 *****************************************************************************/
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070078enum {
79 MSM_PM_MODE_ATTR_SUSPEND,
80 MSM_PM_MODE_ATTR_IDLE,
81 MSM_PM_MODE_ATTR_NR,
82};
83
84static char *msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_NR] = {
85 [MSM_PM_MODE_ATTR_SUSPEND] = "suspend_enabled",
86 [MSM_PM_MODE_ATTR_IDLE] = "idle_enabled",
87};
88
89struct msm_pm_kobj_attribute {
90 unsigned int cpu;
91 struct kobj_attribute ka;
92};
93
94#define GET_CPU_OF_ATTR(attr) \
95 (container_of(attr, struct msm_pm_kobj_attribute, ka)->cpu)
96
97struct msm_pm_sysfs_sleep_mode {
98 struct kobject *kobj;
99 struct attribute_group attr_group;
100 struct attribute *attrs[MSM_PM_MODE_ATTR_NR + 1];
101 struct msm_pm_kobj_attribute kas[MSM_PM_MODE_ATTR_NR];
102};
103
104static char *msm_pm_sleep_mode_labels[MSM_PM_SLEEP_MODE_NR] = {
105 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE] = "power_collapse",
106 [MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT] = "wfi",
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600107 [MSM_PM_SLEEP_MODE_RETENTION] = "retention",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700108 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE] =
109 "standalone_power_collapse",
110};
111
112/*
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
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700399static struct msm_rpmrs_limits *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;
647 struct msm_rpmrs_limits *rs_limits = NULL;
648 int idx;
649
650 mode = (enum msm_pm_sleep_mode) state->driver_data;
651 idx = MSM_PM_MODE(dev->cpu, mode);
652
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600653 allow = msm_pm_sleep_modes[idx].idle_enabled &&
654 msm_pm_sleep_modes[idx].idle_supported;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700655
656 switch (mode) {
657 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
658 if (!allow)
659 break;
660
661 if (num_online_cpus() > 1) {
662 allow = false;
663 break;
664 }
665#ifdef CONFIG_HAS_WAKELOCK
666 if (has_wake_lock(WAKE_LOCK_IDLE)) {
667 allow = false;
668 break;
669 }
670#endif
671 /* fall through */
672
673 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
674 if (!allow)
675 break;
676
677 if (!dev->cpu &&
678 msm_rpm_local_request_is_outstanding()) {
679 allow = false;
680 break;
681 }
682 /* fall through */
683
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600684 case MSM_PM_SLEEP_MODE_RETENTION:
685 if (!allow)
686 break;
687 /* fall through */
688
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700689 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
690 if (!allow)
691 break;
692
693 rs_limits = msm_rpmrs_lowest_limits(true,
694 mode, latency_us, sleep_us);
695
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
702 if ((MSM_PM_DEBUG_IDLE_LIMITS & msm_pm_debug_mask) &&
703 rs_limits)
704 pr_info("CPU%u: %s: limit %p: "
705 "pxo %d, l2_cache %d, "
706 "vdd_mem %d, vdd_dig %d\n",
707 dev->cpu, __func__, rs_limits,
708 rs_limits->pxo,
709 rs_limits->l2_cache,
710 rs_limits->vdd_mem,
711 rs_limits->vdd_dig);
712
713 if (!rs_limits)
714 allow = false;
715 break;
716
717 default:
718 allow = false;
719 break;
720 }
721
722 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
723 pr_info("CPU%u: %s: allow %s: %d\n",
724 dev->cpu, __func__, state->desc, (int)allow);
725
726 if (allow) {
727 state->flags &= ~CPUIDLE_FLAG_IGNORE;
728 state->target_residency = 0;
729 state->exit_latency = 0;
730 state->power_usage = rs_limits->power[dev->cpu];
731
732 if (MSM_PM_SLEEP_MODE_POWER_COLLAPSE == mode)
733 msm_pm_idle_rs_limits = rs_limits;
734 } else {
735 state->flags |= CPUIDLE_FLAG_IGNORE;
736 }
737 }
738
739 return 0;
740}
741
742int msm_pm_idle_enter(enum msm_pm_sleep_mode sleep_mode)
743{
744 int64_t time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700745 int exit_stat;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700746
747 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
748 pr_info("CPU%u: %s: mode %d\n",
749 smp_processor_id(), __func__, sleep_mode);
750
751 time = ktime_to_ns(ktime_get());
752
753 switch (sleep_mode) {
754 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
755 msm_pm_swfi();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700756 exit_stat = MSM_PM_STAT_IDLE_WFI;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700757 break;
758
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600759 case MSM_PM_SLEEP_MODE_RETENTION:
760 msm_pm_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600761 exit_stat = MSM_PM_STAT_RETENTION;
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600762 break;
763
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700764 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
765 msm_pm_power_collapse_standalone(true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700766 exit_stat = MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700767 break;
768
769 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE: {
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600770 int64_t timer_expiration = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700771 bool timer_halted = false;
772 uint32_t sleep_delay;
773 int ret;
774 int notify_rpm =
775 (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600776 int collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700777
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600778 timer_expiration = msm_pm_timer_enter_idle();
779
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700780 sleep_delay = (uint32_t) msm_pm_convert_and_cap_time(
781 timer_expiration, MSM_PM_SLEEP_TICK_LIMIT);
782 if (sleep_delay == 0) /* 0 would mean infinite time */
783 sleep_delay = 1;
784
Karthik Parsha6fb932d2012-01-24 18:04:12 -0800785 if (MSM_PM_DEBUG_IDLE_CLK & msm_pm_debug_mask)
786 clock_debug_print_enabled();
787
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700788 ret = msm_rpmrs_enter_sleep(
789 sleep_delay, msm_pm_idle_rs_limits, true, notify_rpm);
790 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600791 collapsed = msm_pm_power_collapse(true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700792 timer_halted = true;
793
794 msm_rpmrs_exit_sleep(msm_pm_idle_rs_limits, true,
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600795 notify_rpm, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700796 }
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600797 msm_pm_timer_exit_idle(timer_halted);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700798 exit_stat = MSM_PM_STAT_IDLE_POWER_COLLAPSE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700799 break;
800 }
801
802 default:
803 __WARN();
804 goto cpuidle_enter_bail;
805 }
806
807 time = ktime_to_ns(ktime_get()) - time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700808 msm_pm_add_stat(exit_stat, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700809
810 do_div(time, 1000);
811 return (int) time;
812
813cpuidle_enter_bail:
814 return 0;
815}
816
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -0600817static struct msm_pm_sleep_status_data *msm_pm_slp_sts;
818
819static DEFINE_PER_CPU_SHARED_ALIGNED(enum msm_pm_sleep_mode,
820 msm_pm_last_slp_mode);
821
822bool msm_pm_verify_cpu_pc(unsigned int cpu)
823{
824 enum msm_pm_sleep_mode mode = per_cpu(msm_pm_last_slp_mode, cpu);
825
826 if (msm_pm_slp_sts)
827 if ((mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE) ||
828 (mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE))
829 return true;
830
831 return false;
832}
833
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700834void msm_pm_cpu_enter_lowpower(unsigned int cpu)
835{
836 int i;
837 bool allow[MSM_PM_SLEEP_MODE_NR];
838
839 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
840 struct msm_pm_platform_data *mode;
841
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600842 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700843 allow[i] = mode->suspend_supported && mode->suspend_enabled;
844 }
845
846 if (MSM_PM_DEBUG_HOTPLUG & msm_pm_debug_mask)
847 pr_notice("CPU%u: %s: shutting down cpu\n", cpu, __func__);
848
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -0600849 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) {
850 per_cpu(msm_pm_last_slp_mode, cpu)
851 = MSM_PM_SLEEP_MODE_POWER_COLLAPSE;
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700852 msm_pm_power_collapse(false);
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -0600853 } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
854 per_cpu(msm_pm_last_slp_mode, cpu)
855 = MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE;
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700856 msm_pm_power_collapse_standalone(false);
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600857 } else if (allow[MSM_PM_SLEEP_MODE_RETENTION]) {
858 per_cpu(msm_pm_last_slp_mode, cpu)
859 = MSM_PM_SLEEP_MODE_RETENTION;
860 msm_pm_retention();
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -0600861 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
862 per_cpu(msm_pm_last_slp_mode, cpu)
Girish Mahadevanc8127052012-04-25 16:23:24 -0600863 = MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT;
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700864 msm_pm_swfi();
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -0600865 } else
866 per_cpu(msm_pm_last_slp_mode, cpu) = MSM_PM_SLEEP_MODE_NR;
867}
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700868
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -0600869int msm_pm_wait_cpu_shutdown(unsigned int cpu)
870{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700871
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -0600872 int timeout = 10;
873
874 if (!msm_pm_slp_sts)
875 return 0;
876
877 while (timeout--) {
878
879 /*
880 * Check for the SPM of the core being hotplugged to set
881 * its sleep state.The SPM sleep state indicates that the
882 * core has been power collapsed.
883 */
884
885 int acc_sts = __raw_readl(msm_pm_slp_sts->base_addr
886 + cpu * msm_pm_slp_sts->cpu_offset);
887 mb();
888
889 if (acc_sts & msm_pm_slp_sts->mask)
890 return 0;
891
892 usleep(100);
893 }
894 pr_warn("%s(): Timed out waiting for CPU %u SPM to enter sleep state",
895 __func__, cpu);
896 return -EBUSY;
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700897}
898
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700899static int msm_pm_enter(suspend_state_t state)
900{
901 bool allow[MSM_PM_SLEEP_MODE_NR];
902 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700903 int64_t period = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530904 int64_t time = msm_pm_timer_enter_suspend(&period);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700905
906 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
907 pr_info("%s\n", __func__);
908
909 if (smp_processor_id()) {
910 __WARN();
911 goto enter_exit;
912 }
913
914
915 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
916 struct msm_pm_platform_data *mode;
917
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600918 mode = &msm_pm_sleep_modes[MSM_PM_MODE(0, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700919 allow[i] = mode->suspend_supported && mode->suspend_enabled;
920 }
921
922 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) {
923 struct msm_rpmrs_limits *rs_limits;
924 int ret;
925
926 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
927 pr_info("%s: power collapse\n", __func__);
928
929 clock_debug_print_enabled();
930
931#ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE
932 if (msm_pm_sleep_time_override > 0) {
933 int64_t ns = NSEC_PER_SEC *
934 (int64_t) msm_pm_sleep_time_override;
935 msm_pm_set_max_sleep_time(ns);
936 msm_pm_sleep_time_override = 0;
937 }
938#endif /* CONFIG_MSM_SLEEP_TIME_OVERRIDE */
939
940 if (MSM_PM_DEBUG_SUSPEND_LIMITS & msm_pm_debug_mask)
941 msm_rpmrs_show_resources();
942
943 rs_limits = msm_rpmrs_lowest_limits(false,
944 MSM_PM_SLEEP_MODE_POWER_COLLAPSE, -1, -1);
945
946 if ((MSM_PM_DEBUG_SUSPEND_LIMITS & msm_pm_debug_mask) &&
947 rs_limits)
948 pr_info("%s: limit %p: pxo %d, l2_cache %d, "
949 "vdd_mem %d, vdd_dig %d\n",
950 __func__, rs_limits,
951 rs_limits->pxo, rs_limits->l2_cache,
952 rs_limits->vdd_mem, rs_limits->vdd_dig);
953
954 if (rs_limits) {
955 ret = msm_rpmrs_enter_sleep(
956 msm_pm_max_sleep_time, rs_limits, false, true);
957 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600958 int collapsed = msm_pm_power_collapse(false);
959 msm_rpmrs_exit_sleep(rs_limits, false, true,
960 collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700961 }
962 } else {
963 pr_err("%s: cannot find the lowest power limit\n",
964 __func__);
965 }
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530966 time = msm_pm_timer_exit_suspend(time, period);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700967 msm_pm_add_stat(MSM_PM_STAT_SUSPEND, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700968 } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
969 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
970 pr_info("%s: standalone power collapse\n", __func__);
971 msm_pm_power_collapse_standalone(false);
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600972 } else if (allow[MSM_PM_SLEEP_MODE_RETENTION]) {
973 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
974 pr_info("%s: retention\n", __func__);
975 msm_pm_retention();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700976 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
977 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
978 pr_info("%s: swfi\n", __func__);
979 msm_pm_swfi();
980 }
981
982
983enter_exit:
984 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
985 pr_info("%s: return\n", __func__);
986
987 return 0;
988}
989
990static struct platform_suspend_ops msm_pm_ops = {
991 .enter = msm_pm_enter,
992 .valid = suspend_valid_only_mem,
993};
994
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700995/******************************************************************************
996 * Initialization routine
997 *****************************************************************************/
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -0600998void __init msm_pm_init_sleep_status_data(
999 struct msm_pm_sleep_status_data *data)
1000{
1001 msm_pm_slp_sts = data;
1002}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001003
1004static int __init msm_pm_init(void)
1005{
1006 pgd_t *pc_pgd;
1007 pmd_t *pmd;
1008 unsigned long pmdval;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301009 enum msm_pm_time_stats_id enable_stats[] = {
1010 MSM_PM_STAT_IDLE_WFI,
1011 MSM_PM_STAT_RETENTION,
1012 MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE,
1013 MSM_PM_STAT_IDLE_POWER_COLLAPSE,
1014 MSM_PM_STAT_SUSPEND,
1015 };
1016
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001017 /* Page table for cores to come back up safely. */
1018 pc_pgd = pgd_alloc(&init_mm);
1019 if (!pc_pgd)
1020 return -ENOMEM;
1021
1022 pmd = pmd_offset(pc_pgd +
1023 pgd_index(virt_to_phys(msm_pm_collapse_exit)),
1024 virt_to_phys(msm_pm_collapse_exit));
1025 pmdval = (virt_to_phys(msm_pm_collapse_exit) & PGDIR_MASK) |
1026 PMD_TYPE_SECT | PMD_SECT_AP_WRITE;
1027 pmd[0] = __pmd(pmdval);
1028 pmd[1] = __pmd(pmdval + (1 << (PGDIR_SHIFT - 1)));
1029
Steve Mucklefcece052012-02-18 20:09:58 -08001030 msm_saved_state_phys =
1031 allocate_contiguous_ebi_nomap(CPU_SAVED_STATE_SIZE *
1032 num_possible_cpus(), 4);
1033 if (!msm_saved_state_phys)
1034 return -ENOMEM;
1035 msm_saved_state = ioremap_nocache(msm_saved_state_phys,
1036 CPU_SAVED_STATE_SIZE *
1037 num_possible_cpus());
1038 if (!msm_saved_state)
1039 return -ENOMEM;
1040
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001041 /* It is remotely possible that the code in msm_pm_collapse_exit()
1042 * which turns on the MMU with this mapping is in the
1043 * next even-numbered megabyte beyond the
1044 * start of msm_pm_collapse_exit().
1045 * Map this megabyte in as well.
1046 */
1047 pmd[2] = __pmd(pmdval + (2 << (PGDIR_SHIFT - 1)));
1048 flush_pmd_entry(pmd);
1049 msm_pm_pc_pgd = virt_to_phys(pc_pgd);
Steve Muckle730ad7a2012-02-21 15:26:37 -08001050 clean_caches((unsigned long)&msm_pm_pc_pgd, sizeof(msm_pm_pc_pgd),
1051 virt_to_phys(&msm_pm_pc_pgd));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001052
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001053 msm_pm_mode_sysfs_add();
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301054 msm_pm_add_stats(enable_stats, ARRAY_SIZE(enable_stats));
1055
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001056 msm_spm_allow_x_cpu_set_vdd(false);
1057
1058 suspend_set_ops(&msm_pm_ops);
Praveen Chidambaram192979f2012-04-25 18:30:23 -06001059 msm_pm_qtimer_available();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001060 msm_cpuidle_init();
1061
1062 return 0;
1063}
1064
1065late_initcall(msm_pm_init);