blob: 17419cd85ae165b12f82720308f7b9a079a50fdf [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>
Girish Mahadevan63b87262012-08-15 09:21:23 -060030#include <mach/scm.h>
31#include <mach/socinfo.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070032#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"
Jaeseong GIMfc880da2012-08-13 18:47:46 -070047#include "rpm_resources.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070048#include "scm-boot.h"
49#include "spm.h"
50#include "timer.h"
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -060051#include "pm-boot.h"
Girish Mahadevan12aaabe2012-08-17 16:48:05 -060052#include <mach/event_timer.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070053
54/******************************************************************************
55 * Debug Definitions
56 *****************************************************************************/
57
58enum {
59 MSM_PM_DEBUG_SUSPEND = BIT(0),
60 MSM_PM_DEBUG_POWER_COLLAPSE = BIT(1),
61 MSM_PM_DEBUG_SUSPEND_LIMITS = BIT(2),
62 MSM_PM_DEBUG_CLOCK = BIT(3),
63 MSM_PM_DEBUG_RESET_VECTOR = BIT(4),
Karthik Parsha6fb932d2012-01-24 18:04:12 -080064 MSM_PM_DEBUG_IDLE_CLK = BIT(5),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070065 MSM_PM_DEBUG_IDLE = BIT(6),
66 MSM_PM_DEBUG_IDLE_LIMITS = BIT(7),
67 MSM_PM_DEBUG_HOTPLUG = BIT(8),
68};
69
70static int msm_pm_debug_mask = 1;
71module_param_named(
72 debug_mask, msm_pm_debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP
73);
Girish Mahadevan63b87262012-08-15 09:21:23 -060074static int msm_pm_retention_tz_call;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070075
76
77/******************************************************************************
78 * Sleep Modes and Parameters
79 *****************************************************************************/
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070080enum {
81 MSM_PM_MODE_ATTR_SUSPEND,
82 MSM_PM_MODE_ATTR_IDLE,
83 MSM_PM_MODE_ATTR_NR,
84};
85
Girish Mahadevan63b87262012-08-15 09:21:23 -060086#define SCM_L2_RETENTION (0x2)
87#define SCM_CMD_TERMINATE_PC (0x2)
88
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070089static char *msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_NR] = {
90 [MSM_PM_MODE_ATTR_SUSPEND] = "suspend_enabled",
91 [MSM_PM_MODE_ATTR_IDLE] = "idle_enabled",
92};
93
94struct msm_pm_kobj_attribute {
95 unsigned int cpu;
96 struct kobj_attribute ka;
97};
98
99#define GET_CPU_OF_ATTR(attr) \
100 (container_of(attr, struct msm_pm_kobj_attribute, ka)->cpu)
101
102struct msm_pm_sysfs_sleep_mode {
103 struct kobject *kobj;
104 struct attribute_group attr_group;
105 struct attribute *attrs[MSM_PM_MODE_ATTR_NR + 1];
106 struct msm_pm_kobj_attribute kas[MSM_PM_MODE_ATTR_NR];
107};
108
109static char *msm_pm_sleep_mode_labels[MSM_PM_SLEEP_MODE_NR] = {
110 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE] = "power_collapse",
111 [MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT] = "wfi",
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600112 [MSM_PM_SLEEP_MODE_RETENTION] = "retention",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700113 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE] =
114 "standalone_power_collapse",
115};
116
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600117static struct hrtimer pm_hrtimer;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600118static struct msm_pm_sleep_ops pm_sleep_ops;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700119/*
120 * Write out the attribute.
121 */
122static ssize_t msm_pm_mode_attr_show(
123 struct kobject *kobj, struct kobj_attribute *attr, char *buf)
124{
125 int ret = -EINVAL;
126 int i;
127
128 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
129 struct kernel_param kp;
130 unsigned int cpu;
131 struct msm_pm_platform_data *mode;
132
133 if (msm_pm_sleep_mode_labels[i] == NULL)
134 continue;
135
136 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
137 continue;
138
139 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600140 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700141
142 if (!strcmp(attr->attr.name,
143 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
144 u32 arg = mode->suspend_enabled;
145 kp.arg = &arg;
146 ret = param_get_ulong(buf, &kp);
147 } else if (!strcmp(attr->attr.name,
148 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
149 u32 arg = mode->idle_enabled;
150 kp.arg = &arg;
151 ret = param_get_ulong(buf, &kp);
152 }
153
154 break;
155 }
156
157 if (ret > 0) {
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600158 strlcat(buf, "\n", PAGE_SIZE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700159 ret++;
160 }
161
162 return ret;
163}
164
165/*
166 * Read in the new attribute value.
167 */
168static ssize_t msm_pm_mode_attr_store(struct kobject *kobj,
169 struct kobj_attribute *attr, const char *buf, size_t count)
170{
171 int ret = -EINVAL;
172 int i;
173
174 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
175 struct kernel_param kp;
176 unsigned int cpu;
177 struct msm_pm_platform_data *mode;
178
179 if (msm_pm_sleep_mode_labels[i] == NULL)
180 continue;
181
182 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
183 continue;
184
185 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600186 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700187
188 if (!strcmp(attr->attr.name,
189 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
190 kp.arg = &mode->suspend_enabled;
191 ret = param_set_byte(buf, &kp);
192 } else if (!strcmp(attr->attr.name,
193 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
194 kp.arg = &mode->idle_enabled;
195 ret = param_set_byte(buf, &kp);
196 }
197
198 break;
199 }
200
201 return ret ? ret : count;
202}
203
204/*
205 * Add sysfs entries for one cpu.
206 */
207static int __init msm_pm_mode_sysfs_add_cpu(
208 unsigned int cpu, struct kobject *modes_kobj)
209{
210 char cpu_name[8];
211 struct kobject *cpu_kobj;
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600212 struct msm_pm_sysfs_sleep_mode *mode = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700213 int i, j, k;
214 int ret;
215
216 snprintf(cpu_name, sizeof(cpu_name), "cpu%u", cpu);
217 cpu_kobj = kobject_create_and_add(cpu_name, modes_kobj);
218 if (!cpu_kobj) {
219 pr_err("%s: cannot create %s kobject\n", __func__, cpu_name);
220 ret = -ENOMEM;
221 goto mode_sysfs_add_cpu_exit;
222 }
223
224 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
225 int idx = MSM_PM_MODE(cpu, i);
226
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600227 if ((!msm_pm_sleep_modes[idx].suspend_supported)
228 && (!msm_pm_sleep_modes[idx].idle_supported))
229 continue;
230
231 if (!msm_pm_sleep_mode_labels[i] ||
232 !msm_pm_sleep_mode_labels[i][0])
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700233 continue;
234
235 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
236 if (!mode) {
237 pr_err("%s: cannot allocate memory for attributes\n",
238 __func__);
239 ret = -ENOMEM;
240 goto mode_sysfs_add_cpu_exit;
241 }
242
243 mode->kobj = kobject_create_and_add(
244 msm_pm_sleep_mode_labels[i], cpu_kobj);
245 if (!mode->kobj) {
246 pr_err("%s: cannot create kobject\n", __func__);
247 ret = -ENOMEM;
248 goto mode_sysfs_add_cpu_exit;
249 }
250
251 for (k = 0, j = 0; k < MSM_PM_MODE_ATTR_NR; k++) {
252 if ((k == MSM_PM_MODE_ATTR_IDLE) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600253 !msm_pm_sleep_modes[idx].idle_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700254 continue;
255 if ((k == MSM_PM_MODE_ATTR_SUSPEND) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600256 !msm_pm_sleep_modes[idx].suspend_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700257 continue;
Stephen Boydd2059c32012-07-03 15:11:15 -0700258 sysfs_attr_init(&mode->kas[j].ka.attr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700259 mode->kas[j].cpu = cpu;
260 mode->kas[j].ka.attr.mode = 0644;
261 mode->kas[j].ka.show = msm_pm_mode_attr_show;
262 mode->kas[j].ka.store = msm_pm_mode_attr_store;
263 mode->kas[j].ka.attr.name = msm_pm_mode_attr_labels[k];
264 mode->attrs[j] = &mode->kas[j].ka.attr;
265 j++;
266 }
267 mode->attrs[j] = NULL;
268
269 mode->attr_group.attrs = mode->attrs;
270 ret = sysfs_create_group(mode->kobj, &mode->attr_group);
271 if (ret) {
272 pr_err("%s: cannot create kobject attribute group\n",
273 __func__);
274 goto mode_sysfs_add_cpu_exit;
275 }
276 }
277
278 ret = 0;
279
280mode_sysfs_add_cpu_exit:
Praveen Chidambaramd5ac2d32011-10-24 14:30:27 -0600281 if (ret) {
Praveen Chidambaram2cfda632011-10-11 16:58:09 -0600282 if (mode && mode->kobj)
283 kobject_del(mode->kobj);
284 kfree(mode);
285 }
286
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700287 return ret;
288}
289
290/*
291 * Add sysfs entries for the sleep modes.
292 */
293static int __init msm_pm_mode_sysfs_add(void)
294{
295 struct kobject *module_kobj;
296 struct kobject *modes_kobj;
297 unsigned int cpu;
298 int ret;
299
300 module_kobj = kset_find_obj(module_kset, KBUILD_MODNAME);
301 if (!module_kobj) {
302 pr_err("%s: cannot find kobject for module %s\n",
303 __func__, KBUILD_MODNAME);
304 ret = -ENOENT;
305 goto mode_sysfs_add_exit;
306 }
307
308 modes_kobj = kobject_create_and_add("modes", module_kobj);
309 if (!modes_kobj) {
310 pr_err("%s: cannot create modes kobject\n", __func__);
311 ret = -ENOMEM;
312 goto mode_sysfs_add_exit;
313 }
314
315 for_each_possible_cpu(cpu) {
316 ret = msm_pm_mode_sysfs_add_cpu(cpu, modes_kobj);
317 if (ret)
318 goto mode_sysfs_add_exit;
319 }
320
321 ret = 0;
322
323mode_sysfs_add_exit:
324 return ret;
325}
326
327/******************************************************************************
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700328 * Configure Hardware before/after Low Power Mode
329 *****************************************************************************/
330
331/*
332 * Configure hardware registers in preparation for Apps power down.
333 */
334static void msm_pm_config_hw_before_power_down(void)
335{
336 return;
337}
338
339/*
340 * Clear hardware registers after Apps powers up.
341 */
342static void msm_pm_config_hw_after_power_up(void)
343{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700344}
345
346/*
347 * Configure hardware registers in preparation for SWFI.
348 */
349static void msm_pm_config_hw_before_swfi(void)
350{
351 return;
352}
353
Girish Mahadevan63b87262012-08-15 09:21:23 -0600354/*
355 * Configure/Restore hardware registers in preparation for Retention.
356 */
357
358static void msm_pm_config_hw_after_retention(void)
359{
360 int ret;
361 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
362 WARN_ON(ret);
363}
364
365static void msm_pm_config_hw_before_retention(void)
366{
367 return;
368}
369
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700370
371/******************************************************************************
372 * Suspend Max Sleep Time
373 *****************************************************************************/
374
375#ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE
376static int msm_pm_sleep_time_override;
377module_param_named(sleep_time_override,
378 msm_pm_sleep_time_override, int, S_IRUGO | S_IWUSR | S_IWGRP);
379#endif
380
381#define SCLK_HZ (32768)
382#define MSM_PM_SLEEP_TICK_LIMIT (0x6DDD000)
383
384static uint32_t msm_pm_max_sleep_time;
385
386/*
387 * Convert time from nanoseconds to slow clock ticks, then cap it to the
388 * specified limit
389 */
390static int64_t msm_pm_convert_and_cap_time(int64_t time_ns, int64_t limit)
391{
392 do_div(time_ns, NSEC_PER_SEC / SCLK_HZ);
393 return (time_ns > limit) ? limit : time_ns;
394}
395
396/*
397 * Set the sleep time for suspend. 0 means infinite sleep time.
398 */
399void msm_pm_set_max_sleep_time(int64_t max_sleep_time_ns)
400{
401 if (max_sleep_time_ns == 0) {
402 msm_pm_max_sleep_time = 0;
403 } else {
404 msm_pm_max_sleep_time = (uint32_t)msm_pm_convert_and_cap_time(
405 max_sleep_time_ns, MSM_PM_SLEEP_TICK_LIMIT);
406
407 if (msm_pm_max_sleep_time == 0)
408 msm_pm_max_sleep_time = 1;
409 }
410
411 if (msm_pm_debug_mask & MSM_PM_DEBUG_SUSPEND)
412 pr_info("%s: Requested %lld ns Giving %u sclk ticks\n",
413 __func__, max_sleep_time_ns, msm_pm_max_sleep_time);
414}
415EXPORT_SYMBOL(msm_pm_set_max_sleep_time);
416
417
418/******************************************************************************
419 *
420 *****************************************************************************/
421
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600422static void *msm_pm_idle_rs_limits;
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600423static bool msm_pm_use_qtimer;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700424
425static void msm_pm_swfi(void)
426{
427 msm_pm_config_hw_before_swfi();
428 msm_arch_idle();
429}
430
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600431
432static void msm_pm_retention(void)
433{
434 int ret = 0;
435
Girish Mahadevan63b87262012-08-15 09:21:23 -0600436 msm_pm_config_hw_before_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600437 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_POWER_RETENTION, false);
438 WARN_ON(ret);
Girish Mahadevan63b87262012-08-15 09:21:23 -0600439
440 if (msm_pm_retention_tz_call)
441 scm_call_atomic1(SCM_SVC_BOOT, SCM_CMD_TERMINATE_PC,
442 SCM_L2_RETENTION);
443 else
444 msm_arch_idle();
445
446 msm_pm_config_hw_after_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600447}
448
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600449#ifdef CONFIG_CACHE_L2X0
450static inline bool msm_pm_l2x0_power_collapse(void)
451{
452 bool collapsed = 0;
453
Taniya Das38a8c6e2012-05-09 20:34:39 +0530454 l2cc_suspend();
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600455 collapsed = msm_pm_collapse();
Taniya Das38a8c6e2012-05-09 20:34:39 +0530456 l2cc_resume();
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600457
458 return collapsed;
459}
460#else
461static inline bool msm_pm_l2x0_power_collapse(void)
462{
463 return msm_pm_collapse();
464}
465#endif
466
Stephen Boydb29750d2012-02-21 01:21:32 -0800467static bool __ref msm_pm_spm_power_collapse(
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700468 unsigned int cpu, bool from_idle, bool notify_rpm)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700469{
470 void *entry;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600471 bool collapsed = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700472 int ret;
Rohit Vaswanie78dfb62012-02-21 10:29:29 -0800473 unsigned int saved_gic_cpu_ctrl;
474
475 saved_gic_cpu_ctrl = readl_relaxed(MSM_QGIC_CPU_BASE + GIC_CPU_CTRL);
476 mb();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700477
478 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
479 pr_info("CPU%u: %s: notify_rpm %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700480 cpu, __func__, (int) notify_rpm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700481
482 ret = msm_spm_set_low_power_mode(
483 MSM_SPM_MODE_POWER_COLLAPSE, notify_rpm);
484 WARN_ON(ret);
485
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700486 entry = (!cpu || from_idle) ?
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700487 msm_pm_collapse_exit : msm_secondary_startup;
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700488 msm_pm_boot_config_before_pc(cpu, virt_to_phys(entry));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700489
490 if (MSM_PM_DEBUG_RESET_VECTOR & msm_pm_debug_mask)
491 pr_info("CPU%u: %s: program vector to %p\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700492 cpu, __func__, entry);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700493
494#ifdef CONFIG_VFP
Steve Mucklef132c6c2012-06-06 18:30:57 -0700495 vfp_pm_suspend();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700496#endif
497
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600498 collapsed = msm_pm_l2x0_power_collapse();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700499
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700500 msm_pm_boot_config_after_pc(cpu);
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -0600501
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700502 if (collapsed) {
503#ifdef CONFIG_VFP
Steve Mucklef132c6c2012-06-06 18:30:57 -0700504 vfp_pm_resume();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700505#endif
506 cpu_init();
507 writel(0xF0, MSM_QGIC_CPU_BASE + GIC_CPU_PRIMASK);
Rohit Vaswanie78dfb62012-02-21 10:29:29 -0800508 writel_relaxed(saved_gic_cpu_ctrl,
509 MSM_QGIC_CPU_BASE + GIC_CPU_CTRL);
510 mb();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700511 local_fiq_enable();
512 }
513
514 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
515 pr_info("CPU%u: %s: msm_pm_collapse returned, collapsed %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700516 cpu, __func__, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700517
518 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
519 WARN_ON(ret);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600520 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700521}
522
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600523static bool msm_pm_power_collapse_standalone(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700524{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700525 unsigned int cpu = smp_processor_id();
Praveen Chidambaram2a618992012-11-08 17:53:34 -0700526 unsigned int avsdscr;
527 unsigned int avscsr;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600528 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700529
Praveen Chidambaram2a618992012-11-08 17:53:34 -0700530 avsdscr = avs_get_avsdscr();
531 avscsr = avs_get_avscsr();
532 avs_set_avscsr(0); /* Disable AVS */
533
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700534 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, false);
Praveen Chidambaram2a618992012-11-08 17:53:34 -0700535
536 avs_set_avsdscr(avsdscr);
537 avs_set_avscsr(avscsr);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600538 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700539}
540
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600541static bool msm_pm_power_collapse(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700542{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700543 unsigned int cpu = smp_processor_id();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700544 unsigned long saved_acpuclk_rate;
Praveen Chidambaram2a618992012-11-08 17:53:34 -0700545 unsigned int avsdscr;
546 unsigned int avscsr;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600547 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700548
549 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
550 pr_info("CPU%u: %s: idle %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700551 cpu, __func__, (int)from_idle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700552
553 msm_pm_config_hw_before_power_down();
554 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700555 pr_info("CPU%u: %s: pre power down\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700556
Praveen Chidambaram2a618992012-11-08 17:53:34 -0700557 avsdscr = avs_get_avsdscr();
558 avscsr = avs_get_avscsr();
559 avs_set_avscsr(0); /* Disable AVS */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700560
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700561 if (cpu_online(cpu))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700562 saved_acpuclk_rate = acpuclk_power_collapse();
563 else
564 saved_acpuclk_rate = 0;
565
566 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
567 pr_info("CPU%u: %s: change clock rate (old rate = %lu)\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700568 cpu, __func__, saved_acpuclk_rate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700569
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700570 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700571
Girish Mahadevan4e025d92012-02-29 13:26:51 -0700572 if (cpu_online(cpu)) {
573 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
574 pr_info("CPU%u: %s: restore clock rate to %lu\n",
575 cpu, __func__, saved_acpuclk_rate);
576 if (acpuclk_set_rate(cpu, saved_acpuclk_rate, SETRATE_PC) < 0)
577 pr_err("CPU%u: %s: failed to restore clock rate(%lu)\n",
578 cpu, __func__, saved_acpuclk_rate);
579 } else {
580 unsigned int gic_dist_enabled;
581 unsigned int gic_dist_pending;
582 gic_dist_enabled = readl_relaxed(
583 MSM_QGIC_DIST_BASE + GIC_DIST_ENABLE_CLEAR);
584 gic_dist_pending = readl_relaxed(
585 MSM_QGIC_DIST_BASE + GIC_DIST_PENDING_SET);
586 mb();
587 gic_dist_pending &= gic_dist_enabled;
588
589 if (gic_dist_pending)
590 pr_err("CPU %d interrupted during hotplug.Pending int 0x%x\n",
591 cpu, gic_dist_pending);
592 }
593
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700594
Praveen Chidambaram2a618992012-11-08 17:53:34 -0700595 avs_set_avsdscr(avsdscr);
596 avs_set_avscsr(avscsr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700597 msm_pm_config_hw_after_power_up();
598 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700599 pr_info("CPU%u: %s: post power up\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700600
601 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700602 pr_info("CPU%u: %s: return\n", cpu, __func__);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600603 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700604}
605
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600606static void msm_pm_qtimer_available(void)
607{
Abhimanyu Kapur90ced6e2012-06-26 17:41:25 -0700608 if (machine_is_msm8974())
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600609 msm_pm_use_qtimer = true;
610}
611
612static int64_t msm_pm_timer_enter_idle(void)
613{
614 if (msm_pm_use_qtimer)
615 return ktime_to_ns(tick_nohz_get_sleep_length());
616
617 return msm_timer_enter_idle();
618}
619
620static void msm_pm_timer_exit_idle(bool timer_halted)
621{
622 if (msm_pm_use_qtimer)
623 return;
624
625 msm_timer_exit_idle((int) timer_halted);
626}
627
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530628static int64_t msm_pm_timer_enter_suspend(int64_t *period)
629{
Anji Jonnalac02367a2012-07-01 02:56:11 +0530630 int64_t time = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530631
632 if (msm_pm_use_qtimer)
633 return sched_clock();
634
635 time = msm_timer_get_sclk_time(period);
636 if (!time)
637 pr_err("%s: Unable to read sclk.\n", __func__);
638
639 return time;
640}
641
642static int64_t msm_pm_timer_exit_suspend(int64_t time, int64_t period)
643{
644 if (msm_pm_use_qtimer)
645 return sched_clock() - time;
646
647 if (time != 0) {
648 int64_t end_time = msm_timer_get_sclk_time(NULL);
649 if (end_time != 0) {
650 time = end_time - time;
651 if (time < 0)
652 time += period;
653 } else
654 time = 0;
655 }
656
657 return time;
658}
659
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600660/**
661 * pm_hrtimer_cb() : Callback function for hrtimer created if the
662 * core needs to be awake to handle an event.
663 * @hrtimer : Pointer to hrtimer
664 */
665static enum hrtimer_restart pm_hrtimer_cb(struct hrtimer *hrtimer)
666{
667 return HRTIMER_NORESTART;
668}
669
670/**
671 * msm_pm_set_timer() : Set an hrtimer to wakeup the core in time
672 * to handle an event.
673 */
674static void msm_pm_set_timer(uint32_t modified_time_us)
675{
676 u64 modified_time_ns = modified_time_us * NSEC_PER_USEC;
677 ktime_t modified_ktime = ns_to_ktime(modified_time_ns);
678 pm_hrtimer.function = pm_hrtimer_cb;
679 hrtimer_start(&pm_hrtimer, modified_ktime, HRTIMER_MODE_ABS);
680}
681
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700682/******************************************************************************
683 * External Idle/Suspend Functions
684 *****************************************************************************/
685
686void arch_idle(void)
687{
688 return;
689}
690
Steve Mucklef132c6c2012-06-06 18:30:57 -0700691int msm_pm_idle_prepare(struct cpuidle_device *dev,
692 struct cpuidle_driver *drv, int index)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700693{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700694 int i;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700695 unsigned int power_usage = -1;
696 int ret = 0;
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600697 uint32_t modified_time_us = 0;
698 struct msm_pm_time_params time_param;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700699
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600700 time_param.latency_us =
701 (uint32_t) pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
702 time_param.sleep_us =
703 (uint32_t) (ktime_to_us(tick_nohz_get_sleep_length())
704 & UINT_MAX);
705 time_param.modified_time_us = 0;
706
707 if (!dev->cpu)
708 time_param.next_event_us =
709 (uint32_t) (ktime_to_us(get_next_event_time())
710 & UINT_MAX);
711 else
712 time_param.next_event_us = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700713
714 for (i = 0; i < dev->state_count; i++) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700715 struct cpuidle_state *state = &drv->states[i];
716 struct cpuidle_state_usage *st_usage = &dev->states_usage[i];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700717 enum msm_pm_sleep_mode mode;
718 bool allow;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600719 void *rs_limits = NULL;
720 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700721 int idx;
722
Steve Mucklef132c6c2012-06-06 18:30:57 -0700723 mode = (enum msm_pm_sleep_mode) cpuidle_get_statedata(st_usage);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700724 idx = MSM_PM_MODE(dev->cpu, mode);
725
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600726 allow = msm_pm_sleep_modes[idx].idle_enabled &&
727 msm_pm_sleep_modes[idx].idle_supported;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700728
729 switch (mode) {
730 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
731 if (!allow)
732 break;
733
734 if (num_online_cpus() > 1) {
735 allow = false;
736 break;
737 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700738 /* fall through */
739
740 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
741 if (!allow)
742 break;
Jaeseong GIMfc880da2012-08-13 18:47:46 -0700743
744 if (!dev->cpu && msm_rpm_local_request_is_outstanding()) {
745 allow = false;
746 break;
747 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700748 /* fall through */
749
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600750 case MSM_PM_SLEEP_MODE_RETENTION:
751 if (!allow)
752 break;
Girish Mahadevan63b87262012-08-15 09:21:23 -0600753 if (num_online_cpus() > 1) {
754 allow = false;
755 break;
756 }
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600757 /* fall through */
758
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700759 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
760 if (!allow)
761 break;
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600762 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700763
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600764 if (pm_sleep_ops.lowest_limits)
765 rs_limits = pm_sleep_ops.lowest_limits(true,
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600766 mode, &time_param, &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700767
768 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
769 pr_info("CPU%u: %s: %s, latency %uus, "
770 "sleep %uus, limit %p\n",
771 dev->cpu, __func__, state->desc,
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600772 time_param.latency_us,
773 time_param.sleep_us, rs_limits);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700774
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700775 if (!rs_limits)
776 allow = false;
777 break;
778
779 default:
780 allow = false;
781 break;
782 }
783
784 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
785 pr_info("CPU%u: %s: allow %s: %d\n",
786 dev->cpu, __func__, state->desc, (int)allow);
787
788 if (allow) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700789 if (power < power_usage) {
790 power_usage = power;
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600791 modified_time_us = time_param.modified_time_us;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700792 ret = mode;
793 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700794
795 if (MSM_PM_SLEEP_MODE_POWER_COLLAPSE == mode)
796 msm_pm_idle_rs_limits = rs_limits;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700797 }
798 }
799
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600800 if (modified_time_us && !dev->cpu)
801 msm_pm_set_timer(modified_time_us);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700802 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700803}
804
805int msm_pm_idle_enter(enum msm_pm_sleep_mode sleep_mode)
806{
807 int64_t time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700808 int exit_stat;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700809
810 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
811 pr_info("CPU%u: %s: mode %d\n",
812 smp_processor_id(), __func__, sleep_mode);
813
814 time = ktime_to_ns(ktime_get());
815
816 switch (sleep_mode) {
817 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
818 msm_pm_swfi();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700819 exit_stat = MSM_PM_STAT_IDLE_WFI;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700820 break;
821
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600822 case MSM_PM_SLEEP_MODE_RETENTION:
823 msm_pm_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600824 exit_stat = MSM_PM_STAT_RETENTION;
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600825 break;
826
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700827 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
828 msm_pm_power_collapse_standalone(true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700829 exit_stat = MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700830 break;
831
832 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE: {
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600833 int64_t timer_expiration = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700834 bool timer_halted = false;
835 uint32_t sleep_delay;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600836 int ret = -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700837 int notify_rpm =
838 (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600839 int collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700840
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600841 timer_expiration = msm_pm_timer_enter_idle();
842
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700843 sleep_delay = (uint32_t) msm_pm_convert_and_cap_time(
844 timer_expiration, MSM_PM_SLEEP_TICK_LIMIT);
845 if (sleep_delay == 0) /* 0 would mean infinite time */
846 sleep_delay = 1;
847
Karthik Parsha6fb932d2012-01-24 18:04:12 -0800848 if (MSM_PM_DEBUG_IDLE_CLK & msm_pm_debug_mask)
849 clock_debug_print_enabled();
850
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600851 if (pm_sleep_ops.enter_sleep)
852 ret = pm_sleep_ops.enter_sleep(sleep_delay,
853 msm_pm_idle_rs_limits,
854 true, notify_rpm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700855 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600856 collapsed = msm_pm_power_collapse(true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700857 timer_halted = true;
858
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600859 if (pm_sleep_ops.exit_sleep)
860 pm_sleep_ops.exit_sleep(msm_pm_idle_rs_limits,
861 true, notify_rpm, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700862 }
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600863 msm_pm_timer_exit_idle(timer_halted);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700864 exit_stat = MSM_PM_STAT_IDLE_POWER_COLLAPSE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700865 break;
866 }
867
868 default:
869 __WARN();
870 goto cpuidle_enter_bail;
871 }
872
873 time = ktime_to_ns(ktime_get()) - time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700874 msm_pm_add_stat(exit_stat, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700875
876 do_div(time, 1000);
877 return (int) time;
878
879cpuidle_enter_bail:
880 return 0;
881}
882
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700883void msm_pm_cpu_enter_lowpower(unsigned int cpu)
884{
885 int i;
886 bool allow[MSM_PM_SLEEP_MODE_NR];
887
888 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
889 struct msm_pm_platform_data *mode;
890
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600891 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700892 allow[i] = mode->suspend_supported && mode->suspend_enabled;
893 }
894
895 if (MSM_PM_DEBUG_HOTPLUG & msm_pm_debug_mask)
896 pr_notice("CPU%u: %s: shutting down cpu\n", cpu, __func__);
897
Matt Wagantall5375aa72012-07-02 19:59:51 -0700898 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700899 msm_pm_power_collapse(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -0700900 else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700901 msm_pm_power_collapse_standalone(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -0700902 else if (allow[MSM_PM_SLEEP_MODE_RETENTION])
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600903 msm_pm_retention();
Stephen Boydbda74272012-08-09 14:01:27 -0700904 else
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700905 msm_pm_swfi();
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700906}
907
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700908static int msm_pm_enter(suspend_state_t state)
909{
910 bool allow[MSM_PM_SLEEP_MODE_NR];
911 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700912 int64_t period = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530913 int64_t time = msm_pm_timer_enter_suspend(&period);
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600914 struct msm_pm_time_params time_param;
915
916 time_param.latency_us = -1;
917 time_param.sleep_us = -1;
918 time_param.next_event_us = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700919
920 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
921 pr_info("%s\n", __func__);
922
923 if (smp_processor_id()) {
924 __WARN();
925 goto enter_exit;
926 }
927
928
929 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
930 struct msm_pm_platform_data *mode;
931
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600932 mode = &msm_pm_sleep_modes[MSM_PM_MODE(0, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700933 allow[i] = mode->suspend_supported && mode->suspend_enabled;
934 }
935
936 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600937 void *rs_limits = NULL;
938 int ret = -ENODEV;
939 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700940
941 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
942 pr_info("%s: power collapse\n", __func__);
943
944 clock_debug_print_enabled();
945
946#ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE
947 if (msm_pm_sleep_time_override > 0) {
948 int64_t ns = NSEC_PER_SEC *
949 (int64_t) msm_pm_sleep_time_override;
950 msm_pm_set_max_sleep_time(ns);
951 msm_pm_sleep_time_override = 0;
952 }
953#endif /* CONFIG_MSM_SLEEP_TIME_OVERRIDE */
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600954 if (pm_sleep_ops.lowest_limits)
955 rs_limits = pm_sleep_ops.lowest_limits(false,
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600956 MSM_PM_SLEEP_MODE_POWER_COLLAPSE, &time_param, &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700957
958 if (rs_limits) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600959 if (pm_sleep_ops.enter_sleep)
960 ret = pm_sleep_ops.enter_sleep(
961 msm_pm_max_sleep_time,
962 rs_limits, false, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700963 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600964 int collapsed = msm_pm_power_collapse(false);
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600965 if (pm_sleep_ops.exit_sleep) {
966 pm_sleep_ops.exit_sleep(rs_limits,
967 false, true, collapsed);
968 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700969 }
970 } else {
971 pr_err("%s: cannot find the lowest power limit\n",
972 __func__);
973 }
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530974 time = msm_pm_timer_exit_suspend(time, period);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700975 msm_pm_add_stat(MSM_PM_STAT_SUSPEND, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700976 } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
977 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
978 pr_info("%s: standalone power collapse\n", __func__);
979 msm_pm_power_collapse_standalone(false);
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600980 } else if (allow[MSM_PM_SLEEP_MODE_RETENTION]) {
981 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
982 pr_info("%s: retention\n", __func__);
983 msm_pm_retention();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700984 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
985 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
986 pr_info("%s: swfi\n", __func__);
987 msm_pm_swfi();
988 }
989
990
991enter_exit:
992 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
993 pr_info("%s: return\n", __func__);
994
995 return 0;
996}
997
998static struct platform_suspend_ops msm_pm_ops = {
999 .enter = msm_pm_enter,
1000 .valid = suspend_valid_only_mem,
1001};
1002
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001003/******************************************************************************
1004 * Initialization routine
1005 *****************************************************************************/
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001006void msm_pm_set_sleep_ops(struct msm_pm_sleep_ops *ops)
1007{
1008 if (ops)
1009 pm_sleep_ops = *ops;
1010}
1011
Girish Mahadevan63b87262012-08-15 09:21:23 -06001012void __init msm_pm_set_tz_retention_flag(unsigned int flag)
1013{
1014 msm_pm_retention_tz_call = flag;
1015}
1016
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001017static int __init msm_pm_init(void)
1018{
1019 pgd_t *pc_pgd;
1020 pmd_t *pmd;
1021 unsigned long pmdval;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301022 enum msm_pm_time_stats_id enable_stats[] = {
1023 MSM_PM_STAT_IDLE_WFI,
1024 MSM_PM_STAT_RETENTION,
1025 MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE,
1026 MSM_PM_STAT_IDLE_POWER_COLLAPSE,
1027 MSM_PM_STAT_SUSPEND,
1028 };
Steve Mucklef132c6c2012-06-06 18:30:57 -07001029 unsigned long exit_phys;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301030
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001031 /* Page table for cores to come back up safely. */
1032 pc_pgd = pgd_alloc(&init_mm);
1033 if (!pc_pgd)
1034 return -ENOMEM;
1035
Steve Mucklef132c6c2012-06-06 18:30:57 -07001036 exit_phys = virt_to_phys(msm_pm_collapse_exit);
1037
1038 pmd = pmd_offset(pud_offset(pc_pgd + pgd_index(exit_phys),exit_phys),
1039 exit_phys);
1040 pmdval = (exit_phys & PGDIR_MASK) |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001041 PMD_TYPE_SECT | PMD_SECT_AP_WRITE;
1042 pmd[0] = __pmd(pmdval);
1043 pmd[1] = __pmd(pmdval + (1 << (PGDIR_SHIFT - 1)));
1044
Steve Mucklefcece052012-02-18 20:09:58 -08001045 msm_saved_state_phys =
1046 allocate_contiguous_ebi_nomap(CPU_SAVED_STATE_SIZE *
1047 num_possible_cpus(), 4);
1048 if (!msm_saved_state_phys)
1049 return -ENOMEM;
1050 msm_saved_state = ioremap_nocache(msm_saved_state_phys,
1051 CPU_SAVED_STATE_SIZE *
1052 num_possible_cpus());
1053 if (!msm_saved_state)
1054 return -ENOMEM;
1055
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001056 /* It is remotely possible that the code in msm_pm_collapse_exit()
1057 * which turns on the MMU with this mapping is in the
1058 * next even-numbered megabyte beyond the
1059 * start of msm_pm_collapse_exit().
1060 * Map this megabyte in as well.
1061 */
1062 pmd[2] = __pmd(pmdval + (2 << (PGDIR_SHIFT - 1)));
1063 flush_pmd_entry(pmd);
1064 msm_pm_pc_pgd = virt_to_phys(pc_pgd);
Steve Muckle730ad7a2012-02-21 15:26:37 -08001065 clean_caches((unsigned long)&msm_pm_pc_pgd, sizeof(msm_pm_pc_pgd),
1066 virt_to_phys(&msm_pm_pc_pgd));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001067
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001068 msm_pm_mode_sysfs_add();
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301069 msm_pm_add_stats(enable_stats, ARRAY_SIZE(enable_stats));
1070
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001071 suspend_set_ops(&msm_pm_ops);
Praveen Chidambaram192979f2012-04-25 18:30:23 -06001072 msm_pm_qtimer_available();
Girish Mahadevan12aaabe2012-08-17 16:48:05 -06001073 hrtimer_init(&pm_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001074 msm_cpuidle_init();
1075
1076 return 0;
1077}
1078
1079late_initcall(msm_pm_init);