blob: 60e64cd59c7b9e43c2fa462e5bf9a10ed8dd9fc4 [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>
24#include <linux/proc_fs.h>
25#include <linux/smp.h>
26#include <linux/suspend.h>
27#include <linux/tick.h>
28#include <linux/uaccess.h>
29#include <linux/wakelock.h>
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -060030#include <linux/delay.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070031#include <mach/msm_iomap.h>
Praveen Chidambaram192979f2012-04-25 18:30:23 -060032#include <mach/socinfo.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070033#include <mach/system.h>
34#include <asm/cacheflush.h>
35#include <asm/hardware/gic.h>
36#include <asm/pgtable.h>
37#include <asm/pgalloc.h>
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -060038#include <asm/hardware/cache-l2x0.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070039#ifdef CONFIG_VFP
40#include <asm/vfp.h>
41#endif
42
43#include "acpuclock.h"
44#include "clock.h"
45#include "avs.h"
Abhijeet Dharmapurikarefaca4f2011-12-27 16:24:07 -080046#include <mach/cpuidle.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070047#include "idle.h"
Matt Wagantall7cca4642012-02-01 16:43:24 -080048#include "pm.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070049#include "rpm_resources.h"
50#include "scm-boot.h"
51#include "spm.h"
52#include "timer.h"
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -060053#include "pm-boot.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070054
55/******************************************************************************
56 * Debug Definitions
57 *****************************************************************************/
58
59enum {
60 MSM_PM_DEBUG_SUSPEND = BIT(0),
61 MSM_PM_DEBUG_POWER_COLLAPSE = BIT(1),
62 MSM_PM_DEBUG_SUSPEND_LIMITS = BIT(2),
63 MSM_PM_DEBUG_CLOCK = BIT(3),
64 MSM_PM_DEBUG_RESET_VECTOR = BIT(4),
Karthik Parsha6fb932d2012-01-24 18:04:12 -080065 MSM_PM_DEBUG_IDLE_CLK = BIT(5),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070066 MSM_PM_DEBUG_IDLE = BIT(6),
67 MSM_PM_DEBUG_IDLE_LIMITS = BIT(7),
68 MSM_PM_DEBUG_HOTPLUG = BIT(8),
69};
70
71static int msm_pm_debug_mask = 1;
72module_param_named(
73 debug_mask, msm_pm_debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP
74);
75
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
86static char *msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_NR] = {
87 [MSM_PM_MODE_ATTR_SUSPEND] = "suspend_enabled",
88 [MSM_PM_MODE_ATTR_IDLE] = "idle_enabled",
89};
90
91struct msm_pm_kobj_attribute {
92 unsigned int cpu;
93 struct kobj_attribute ka;
94};
95
96#define GET_CPU_OF_ATTR(attr) \
97 (container_of(attr, struct msm_pm_kobj_attribute, ka)->cpu)
98
99struct msm_pm_sysfs_sleep_mode {
100 struct kobject *kobj;
101 struct attribute_group attr_group;
102 struct attribute *attrs[MSM_PM_MODE_ATTR_NR + 1];
103 struct msm_pm_kobj_attribute kas[MSM_PM_MODE_ATTR_NR];
104};
105
106static char *msm_pm_sleep_mode_labels[MSM_PM_SLEEP_MODE_NR] = {
107 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE] = "power_collapse",
108 [MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT] = "wfi",
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600109 [MSM_PM_SLEEP_MODE_RETENTION] = "retention",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700110 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE] =
111 "standalone_power_collapse",
112};
113
114/*
115 * Write out the attribute.
116 */
117static ssize_t msm_pm_mode_attr_show(
118 struct kobject *kobj, struct kobj_attribute *attr, char *buf)
119{
120 int ret = -EINVAL;
121 int i;
122
123 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
124 struct kernel_param kp;
125 unsigned int cpu;
126 struct msm_pm_platform_data *mode;
127
128 if (msm_pm_sleep_mode_labels[i] == NULL)
129 continue;
130
131 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
132 continue;
133
134 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600135 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700136
137 if (!strcmp(attr->attr.name,
138 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
139 u32 arg = mode->suspend_enabled;
140 kp.arg = &arg;
141 ret = param_get_ulong(buf, &kp);
142 } else if (!strcmp(attr->attr.name,
143 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
144 u32 arg = mode->idle_enabled;
145 kp.arg = &arg;
146 ret = param_get_ulong(buf, &kp);
147 }
148
149 break;
150 }
151
152 if (ret > 0) {
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600153 strlcat(buf, "\n", PAGE_SIZE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700154 ret++;
155 }
156
157 return ret;
158}
159
160/*
161 * Read in the new attribute value.
162 */
163static ssize_t msm_pm_mode_attr_store(struct kobject *kobj,
164 struct kobj_attribute *attr, const char *buf, size_t count)
165{
166 int ret = -EINVAL;
167 int i;
168
169 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
170 struct kernel_param kp;
171 unsigned int cpu;
172 struct msm_pm_platform_data *mode;
173
174 if (msm_pm_sleep_mode_labels[i] == NULL)
175 continue;
176
177 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
178 continue;
179
180 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600181 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700182
183 if (!strcmp(attr->attr.name,
184 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
185 kp.arg = &mode->suspend_enabled;
186 ret = param_set_byte(buf, &kp);
187 } else if (!strcmp(attr->attr.name,
188 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
189 kp.arg = &mode->idle_enabled;
190 ret = param_set_byte(buf, &kp);
191 }
192
193 break;
194 }
195
196 return ret ? ret : count;
197}
198
199/*
200 * Add sysfs entries for one cpu.
201 */
202static int __init msm_pm_mode_sysfs_add_cpu(
203 unsigned int cpu, struct kobject *modes_kobj)
204{
205 char cpu_name[8];
206 struct kobject *cpu_kobj;
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600207 struct msm_pm_sysfs_sleep_mode *mode = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700208 int i, j, k;
209 int ret;
210
211 snprintf(cpu_name, sizeof(cpu_name), "cpu%u", cpu);
212 cpu_kobj = kobject_create_and_add(cpu_name, modes_kobj);
213 if (!cpu_kobj) {
214 pr_err("%s: cannot create %s kobject\n", __func__, cpu_name);
215 ret = -ENOMEM;
216 goto mode_sysfs_add_cpu_exit;
217 }
218
219 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
220 int idx = MSM_PM_MODE(cpu, i);
221
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600222 if ((!msm_pm_sleep_modes[idx].suspend_supported)
223 && (!msm_pm_sleep_modes[idx].idle_supported))
224 continue;
225
226 if (!msm_pm_sleep_mode_labels[i] ||
227 !msm_pm_sleep_mode_labels[i][0])
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700228 continue;
229
230 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
231 if (!mode) {
232 pr_err("%s: cannot allocate memory for attributes\n",
233 __func__);
234 ret = -ENOMEM;
235 goto mode_sysfs_add_cpu_exit;
236 }
237
238 mode->kobj = kobject_create_and_add(
239 msm_pm_sleep_mode_labels[i], cpu_kobj);
240 if (!mode->kobj) {
241 pr_err("%s: cannot create kobject\n", __func__);
242 ret = -ENOMEM;
243 goto mode_sysfs_add_cpu_exit;
244 }
245
246 for (k = 0, j = 0; k < MSM_PM_MODE_ATTR_NR; k++) {
247 if ((k == MSM_PM_MODE_ATTR_IDLE) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600248 !msm_pm_sleep_modes[idx].idle_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700249 continue;
250 if ((k == MSM_PM_MODE_ATTR_SUSPEND) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600251 !msm_pm_sleep_modes[idx].suspend_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700252 continue;
253 mode->kas[j].cpu = cpu;
254 mode->kas[j].ka.attr.mode = 0644;
255 mode->kas[j].ka.show = msm_pm_mode_attr_show;
256 mode->kas[j].ka.store = msm_pm_mode_attr_store;
257 mode->kas[j].ka.attr.name = msm_pm_mode_attr_labels[k];
258 mode->attrs[j] = &mode->kas[j].ka.attr;
259 j++;
260 }
261 mode->attrs[j] = NULL;
262
263 mode->attr_group.attrs = mode->attrs;
264 ret = sysfs_create_group(mode->kobj, &mode->attr_group);
265 if (ret) {
266 pr_err("%s: cannot create kobject attribute group\n",
267 __func__);
268 goto mode_sysfs_add_cpu_exit;
269 }
270 }
271
272 ret = 0;
273
274mode_sysfs_add_cpu_exit:
Praveen Chidambaramd5ac2d32011-10-24 14:30:27 -0600275 if (ret) {
Praveen Chidambaram2cfda632011-10-11 16:58:09 -0600276 if (mode && mode->kobj)
277 kobject_del(mode->kobj);
278 kfree(mode);
279 }
280
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700281 return ret;
282}
283
284/*
285 * Add sysfs entries for the sleep modes.
286 */
287static int __init msm_pm_mode_sysfs_add(void)
288{
289 struct kobject *module_kobj;
290 struct kobject *modes_kobj;
291 unsigned int cpu;
292 int ret;
293
294 module_kobj = kset_find_obj(module_kset, KBUILD_MODNAME);
295 if (!module_kobj) {
296 pr_err("%s: cannot find kobject for module %s\n",
297 __func__, KBUILD_MODNAME);
298 ret = -ENOENT;
299 goto mode_sysfs_add_exit;
300 }
301
302 modes_kobj = kobject_create_and_add("modes", module_kobj);
303 if (!modes_kobj) {
304 pr_err("%s: cannot create modes kobject\n", __func__);
305 ret = -ENOMEM;
306 goto mode_sysfs_add_exit;
307 }
308
309 for_each_possible_cpu(cpu) {
310 ret = msm_pm_mode_sysfs_add_cpu(cpu, modes_kobj);
311 if (ret)
312 goto mode_sysfs_add_exit;
313 }
314
315 ret = 0;
316
317mode_sysfs_add_exit:
318 return ret;
319}
320
321/******************************************************************************
322 * CONFIG_MSM_IDLE_STATS
323 *****************************************************************************/
324
325#ifdef CONFIG_MSM_IDLE_STATS
326enum msm_pm_time_stats_id {
327 MSM_PM_STAT_REQUESTED_IDLE,
328 MSM_PM_STAT_IDLE_WFI,
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600329 MSM_PM_STAT_RETENTION,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700330 MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE,
331 MSM_PM_STAT_IDLE_POWER_COLLAPSE,
332 MSM_PM_STAT_SUSPEND,
333 MSM_PM_STAT_COUNT
334};
335
336struct msm_pm_time_stats {
337 const char *name;
338 int64_t first_bucket_time;
339 int bucket[CONFIG_MSM_IDLE_STATS_BUCKET_COUNT];
340 int64_t min_time[CONFIG_MSM_IDLE_STATS_BUCKET_COUNT];
341 int64_t max_time[CONFIG_MSM_IDLE_STATS_BUCKET_COUNT];
342 int count;
343 int64_t total_time;
344};
345
346struct msm_pm_cpu_time_stats {
347 struct msm_pm_time_stats stats[MSM_PM_STAT_COUNT];
348};
349
350static DEFINE_SPINLOCK(msm_pm_stats_lock);
351static DEFINE_PER_CPU_SHARED_ALIGNED(
352 struct msm_pm_cpu_time_stats, msm_pm_stats);
353
354/*
355 * Add the given time data to the statistics collection.
356 */
357static void msm_pm_add_stat(enum msm_pm_time_stats_id id, int64_t t)
358{
359 unsigned long flags;
360 struct msm_pm_time_stats *stats;
361 int64_t bt;
362 int i;
363
364 spin_lock_irqsave(&msm_pm_stats_lock, flags);
365 stats = __get_cpu_var(msm_pm_stats).stats;
366
367 stats[id].total_time += t;
368 stats[id].count++;
369
370 bt = t;
371 do_div(bt, stats[id].first_bucket_time);
372
373 if (bt < 1ULL << (CONFIG_MSM_IDLE_STATS_BUCKET_SHIFT *
374 (CONFIG_MSM_IDLE_STATS_BUCKET_COUNT - 1)))
375 i = DIV_ROUND_UP(fls((uint32_t)bt),
376 CONFIG_MSM_IDLE_STATS_BUCKET_SHIFT);
377 else
378 i = CONFIG_MSM_IDLE_STATS_BUCKET_COUNT - 1;
379
Praveen Chidambaramfdaef162011-09-28 08:40:05 -0600380 if (i >= CONFIG_MSM_IDLE_STATS_BUCKET_COUNT)
381 i = CONFIG_MSM_IDLE_STATS_BUCKET_COUNT - 1;
382
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700383 stats[id].bucket[i]++;
384
385 if (t < stats[id].min_time[i] || !stats[id].max_time[i])
386 stats[id].min_time[i] = t;
387 if (t > stats[id].max_time[i])
388 stats[id].max_time[i] = t;
389
390 spin_unlock_irqrestore(&msm_pm_stats_lock, flags);
391}
392
393/*
394 * Helper function of snprintf where buf is auto-incremented, size is auto-
395 * decremented, and there is no return value.
396 *
397 * NOTE: buf and size must be l-values (e.g. variables)
398 */
399#define SNPRINTF(buf, size, format, ...) \
400 do { \
401 if (size > 0) { \
402 int ret; \
403 ret = snprintf(buf, size, format, ## __VA_ARGS__); \
404 if (ret > size) { \
405 buf += size; \
406 size = 0; \
407 } else { \
408 buf += ret; \
409 size -= ret; \
410 } \
411 } \
412 } while (0)
413
414/*
415 * Write out the power management statistics.
416 */
417static int msm_pm_read_proc
418 (char *page, char **start, off_t off, int count, int *eof, void *data)
419{
420 unsigned int cpu = off / MSM_PM_STAT_COUNT;
421 int id = off % MSM_PM_STAT_COUNT;
422 char *p = page;
423
424 if (count < 1024) {
425 *start = (char *) 0;
426 *eof = 0;
427 return 0;
428 }
429
430 if (cpu < num_possible_cpus()) {
431 unsigned long flags;
432 struct msm_pm_time_stats *stats;
433 int i;
434 int64_t bucket_time;
435 int64_t s;
436 uint32_t ns;
437
438 spin_lock_irqsave(&msm_pm_stats_lock, flags);
439 stats = per_cpu(msm_pm_stats, cpu).stats;
440
441 s = stats[id].total_time;
442 ns = do_div(s, NSEC_PER_SEC);
443 SNPRINTF(p, count,
444 "[cpu %u] %s:\n"
445 " count: %7d\n"
446 " total_time: %lld.%09u\n",
447 cpu, stats[id].name,
448 stats[id].count,
449 s, ns);
450
451 bucket_time = stats[id].first_bucket_time;
452 for (i = 0; i < CONFIG_MSM_IDLE_STATS_BUCKET_COUNT - 1; i++) {
453 s = bucket_time;
454 ns = do_div(s, NSEC_PER_SEC);
455 SNPRINTF(p, count,
456 " <%6lld.%09u: %7d (%lld-%lld)\n",
457 s, ns, stats[id].bucket[i],
458 stats[id].min_time[i],
459 stats[id].max_time[i]);
460
461 bucket_time <<= CONFIG_MSM_IDLE_STATS_BUCKET_SHIFT;
462 }
463
464 SNPRINTF(p, count, " >=%6lld.%09u: %7d (%lld-%lld)\n",
465 s, ns, stats[id].bucket[i],
466 stats[id].min_time[i],
467 stats[id].max_time[i]);
468
469 *start = (char *) 1;
470 *eof = (off + 1 >= MSM_PM_STAT_COUNT * num_possible_cpus());
471
472 spin_unlock_irqrestore(&msm_pm_stats_lock, flags);
473 }
474
475 return p - page;
476}
477#undef SNPRINTF
478
479#define MSM_PM_STATS_RESET "reset"
480
481/*
482 * Reset the power management statistics values.
483 */
484static int msm_pm_write_proc(struct file *file, const char __user *buffer,
485 unsigned long count, void *data)
486{
487 char buf[sizeof(MSM_PM_STATS_RESET)];
488 int ret;
489 unsigned long flags;
490 unsigned int cpu;
491
492 if (count < strlen(MSM_PM_STATS_RESET)) {
493 ret = -EINVAL;
494 goto write_proc_failed;
495 }
496
497 if (copy_from_user(buf, buffer, strlen(MSM_PM_STATS_RESET))) {
498 ret = -EFAULT;
499 goto write_proc_failed;
500 }
501
502 if (memcmp(buf, MSM_PM_STATS_RESET, strlen(MSM_PM_STATS_RESET))) {
503 ret = -EINVAL;
504 goto write_proc_failed;
505 }
506
507 spin_lock_irqsave(&msm_pm_stats_lock, flags);
508 for_each_possible_cpu(cpu) {
509 struct msm_pm_time_stats *stats;
510 int i;
511
512 stats = per_cpu(msm_pm_stats, cpu).stats;
513 for (i = 0; i < MSM_PM_STAT_COUNT; i++) {
514 memset(stats[i].bucket,
515 0, sizeof(stats[i].bucket));
516 memset(stats[i].min_time,
517 0, sizeof(stats[i].min_time));
518 memset(stats[i].max_time,
519 0, sizeof(stats[i].max_time));
520 stats[i].count = 0;
521 stats[i].total_time = 0;
522 }
523 }
524
525 spin_unlock_irqrestore(&msm_pm_stats_lock, flags);
526 return count;
527
528write_proc_failed:
529 return ret;
530}
531#undef MSM_PM_STATS_RESET
532#endif /* CONFIG_MSM_IDLE_STATS */
533
534
535/******************************************************************************
536 * Configure Hardware before/after Low Power Mode
537 *****************************************************************************/
538
539/*
540 * Configure hardware registers in preparation for Apps power down.
541 */
542static void msm_pm_config_hw_before_power_down(void)
543{
544 return;
545}
546
547/*
548 * Clear hardware registers after Apps powers up.
549 */
550static void msm_pm_config_hw_after_power_up(void)
551{
552 return;
553}
554
555/*
556 * Configure hardware registers in preparation for SWFI.
557 */
558static void msm_pm_config_hw_before_swfi(void)
559{
560 return;
561}
562
563
564/******************************************************************************
565 * Suspend Max Sleep Time
566 *****************************************************************************/
567
568#ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE
569static int msm_pm_sleep_time_override;
570module_param_named(sleep_time_override,
571 msm_pm_sleep_time_override, int, S_IRUGO | S_IWUSR | S_IWGRP);
572#endif
573
574#define SCLK_HZ (32768)
575#define MSM_PM_SLEEP_TICK_LIMIT (0x6DDD000)
576
577static uint32_t msm_pm_max_sleep_time;
578
579/*
580 * Convert time from nanoseconds to slow clock ticks, then cap it to the
581 * specified limit
582 */
583static int64_t msm_pm_convert_and_cap_time(int64_t time_ns, int64_t limit)
584{
585 do_div(time_ns, NSEC_PER_SEC / SCLK_HZ);
586 return (time_ns > limit) ? limit : time_ns;
587}
588
589/*
590 * Set the sleep time for suspend. 0 means infinite sleep time.
591 */
592void msm_pm_set_max_sleep_time(int64_t max_sleep_time_ns)
593{
594 if (max_sleep_time_ns == 0) {
595 msm_pm_max_sleep_time = 0;
596 } else {
597 msm_pm_max_sleep_time = (uint32_t)msm_pm_convert_and_cap_time(
598 max_sleep_time_ns, MSM_PM_SLEEP_TICK_LIMIT);
599
600 if (msm_pm_max_sleep_time == 0)
601 msm_pm_max_sleep_time = 1;
602 }
603
604 if (msm_pm_debug_mask & MSM_PM_DEBUG_SUSPEND)
605 pr_info("%s: Requested %lld ns Giving %u sclk ticks\n",
606 __func__, max_sleep_time_ns, msm_pm_max_sleep_time);
607}
608EXPORT_SYMBOL(msm_pm_set_max_sleep_time);
609
610
611/******************************************************************************
612 *
613 *****************************************************************************/
614
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700615static struct msm_rpmrs_limits *msm_pm_idle_rs_limits;
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600616static bool msm_pm_use_qtimer;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700617
618static void msm_pm_swfi(void)
619{
620 msm_pm_config_hw_before_swfi();
621 msm_arch_idle();
622}
623
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600624
625static void msm_pm_retention(void)
626{
627 int ret = 0;
628
629 msm_pm_config_hw_before_swfi();
630 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_POWER_RETENTION, false);
631 WARN_ON(ret);
632 msm_arch_idle();
633 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
634 WARN_ON(ret);
635}
636
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600637#ifdef CONFIG_CACHE_L2X0
638static inline bool msm_pm_l2x0_power_collapse(void)
639{
640 bool collapsed = 0;
641
Sridhar Parasurama0222902012-04-27 11:18:02 -0700642 l2x0_suspend();
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600643 collapsed = msm_pm_collapse();
Sridhar Parasurama0222902012-04-27 11:18:02 -0700644 l2x0_resume(collapsed);
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600645
646 return collapsed;
647}
648#else
649static inline bool msm_pm_l2x0_power_collapse(void)
650{
651 return msm_pm_collapse();
652}
653#endif
654
Stephen Boydb29750d2012-02-21 01:21:32 -0800655static bool __ref msm_pm_spm_power_collapse(
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700656 unsigned int cpu, bool from_idle, bool notify_rpm)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700657{
658 void *entry;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600659 bool collapsed = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700660 int ret;
Rohit Vaswanie78dfb62012-02-21 10:29:29 -0800661 unsigned int saved_gic_cpu_ctrl;
662
663 saved_gic_cpu_ctrl = readl_relaxed(MSM_QGIC_CPU_BASE + GIC_CPU_CTRL);
664 mb();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700665
666 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
667 pr_info("CPU%u: %s: notify_rpm %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700668 cpu, __func__, (int) notify_rpm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700669
670 ret = msm_spm_set_low_power_mode(
671 MSM_SPM_MODE_POWER_COLLAPSE, notify_rpm);
672 WARN_ON(ret);
673
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700674 entry = (!cpu || from_idle) ?
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700675 msm_pm_collapse_exit : msm_secondary_startup;
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700676 msm_pm_boot_config_before_pc(cpu, virt_to_phys(entry));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700677
678 if (MSM_PM_DEBUG_RESET_VECTOR & msm_pm_debug_mask)
679 pr_info("CPU%u: %s: program vector to %p\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700680 cpu, __func__, entry);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700681
682#ifdef CONFIG_VFP
683 vfp_flush_context();
684#endif
685
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600686 collapsed = msm_pm_l2x0_power_collapse();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700687
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700688 msm_pm_boot_config_after_pc(cpu);
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -0600689
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700690 if (collapsed) {
691#ifdef CONFIG_VFP
692 vfp_reinit();
693#endif
694 cpu_init();
695 writel(0xF0, MSM_QGIC_CPU_BASE + GIC_CPU_PRIMASK);
Rohit Vaswanie78dfb62012-02-21 10:29:29 -0800696 writel_relaxed(saved_gic_cpu_ctrl,
697 MSM_QGIC_CPU_BASE + GIC_CPU_CTRL);
698 mb();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700699 local_fiq_enable();
700 }
701
702 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
703 pr_info("CPU%u: %s: msm_pm_collapse returned, collapsed %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700704 cpu, __func__, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700705
706 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
707 WARN_ON(ret);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600708 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700709}
710
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600711static bool msm_pm_power_collapse_standalone(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700712{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700713 unsigned int cpu = smp_processor_id();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700714 unsigned int avsdscr_setting;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600715 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700716
717 avsdscr_setting = avs_get_avsdscr();
718 avs_disable();
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700719 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700720 avs_reset_delays(avsdscr_setting);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600721 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700722}
723
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600724static bool msm_pm_power_collapse(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700725{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700726 unsigned int cpu = smp_processor_id();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700727 unsigned long saved_acpuclk_rate;
728 unsigned int avsdscr_setting;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600729 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700730
731 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
732 pr_info("CPU%u: %s: idle %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700733 cpu, __func__, (int)from_idle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700734
735 msm_pm_config_hw_before_power_down();
736 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700737 pr_info("CPU%u: %s: pre power down\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700738
739 avsdscr_setting = avs_get_avsdscr();
740 avs_disable();
741
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700742 if (cpu_online(cpu))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700743 saved_acpuclk_rate = acpuclk_power_collapse();
744 else
745 saved_acpuclk_rate = 0;
746
747 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
748 pr_info("CPU%u: %s: change clock rate (old rate = %lu)\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700749 cpu, __func__, saved_acpuclk_rate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700750
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700751 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700752
Girish Mahadevan4e025d92012-02-29 13:26:51 -0700753 if (cpu_online(cpu)) {
754 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
755 pr_info("CPU%u: %s: restore clock rate to %lu\n",
756 cpu, __func__, saved_acpuclk_rate);
757 if (acpuclk_set_rate(cpu, saved_acpuclk_rate, SETRATE_PC) < 0)
758 pr_err("CPU%u: %s: failed to restore clock rate(%lu)\n",
759 cpu, __func__, saved_acpuclk_rate);
760 } else {
761 unsigned int gic_dist_enabled;
762 unsigned int gic_dist_pending;
763 gic_dist_enabled = readl_relaxed(
764 MSM_QGIC_DIST_BASE + GIC_DIST_ENABLE_CLEAR);
765 gic_dist_pending = readl_relaxed(
766 MSM_QGIC_DIST_BASE + GIC_DIST_PENDING_SET);
767 mb();
768 gic_dist_pending &= gic_dist_enabled;
769
770 if (gic_dist_pending)
771 pr_err("CPU %d interrupted during hotplug.Pending int 0x%x\n",
772 cpu, gic_dist_pending);
773 }
774
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700775
776 avs_reset_delays(avsdscr_setting);
777 msm_pm_config_hw_after_power_up();
778 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700779 pr_info("CPU%u: %s: post power up\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700780
781 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700782 pr_info("CPU%u: %s: return\n", cpu, __func__);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600783 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700784}
785
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600786static void msm_pm_qtimer_available(void)
787{
788 if (machine_is_copper())
789 msm_pm_use_qtimer = true;
790}
791
792static int64_t msm_pm_timer_enter_idle(void)
793{
794 if (msm_pm_use_qtimer)
795 return ktime_to_ns(tick_nohz_get_sleep_length());
796
797 return msm_timer_enter_idle();
798}
799
800static void msm_pm_timer_exit_idle(bool timer_halted)
801{
802 if (msm_pm_use_qtimer)
803 return;
804
805 msm_timer_exit_idle((int) timer_halted);
806}
807
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700808/******************************************************************************
809 * External Idle/Suspend Functions
810 *****************************************************************************/
811
812void arch_idle(void)
813{
814 return;
815}
816
817int msm_pm_idle_prepare(struct cpuidle_device *dev)
818{
819 uint32_t latency_us;
820 uint32_t sleep_us;
821 int i;
822
823 latency_us = (uint32_t) pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
824 sleep_us = (uint32_t) ktime_to_ns(tick_nohz_get_sleep_length());
825 sleep_us = DIV_ROUND_UP(sleep_us, 1000);
826
827 for (i = 0; i < dev->state_count; i++) {
828 struct cpuidle_state *state = &dev->states[i];
829 enum msm_pm_sleep_mode mode;
830 bool allow;
831 struct msm_rpmrs_limits *rs_limits = NULL;
832 int idx;
833
834 mode = (enum msm_pm_sleep_mode) state->driver_data;
835 idx = MSM_PM_MODE(dev->cpu, mode);
836
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600837 allow = msm_pm_sleep_modes[idx].idle_enabled &&
838 msm_pm_sleep_modes[idx].idle_supported;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700839
840 switch (mode) {
841 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
842 if (!allow)
843 break;
844
845 if (num_online_cpus() > 1) {
846 allow = false;
847 break;
848 }
849#ifdef CONFIG_HAS_WAKELOCK
850 if (has_wake_lock(WAKE_LOCK_IDLE)) {
851 allow = false;
852 break;
853 }
854#endif
855 /* fall through */
856
857 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
858 if (!allow)
859 break;
860
861 if (!dev->cpu &&
862 msm_rpm_local_request_is_outstanding()) {
863 allow = false;
864 break;
865 }
866 /* fall through */
867
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600868 case MSM_PM_SLEEP_MODE_RETENTION:
869 if (!allow)
870 break;
871 /* fall through */
872
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700873 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
874 if (!allow)
875 break;
876
877 rs_limits = msm_rpmrs_lowest_limits(true,
878 mode, latency_us, sleep_us);
879
880 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
881 pr_info("CPU%u: %s: %s, latency %uus, "
882 "sleep %uus, limit %p\n",
883 dev->cpu, __func__, state->desc,
884 latency_us, sleep_us, rs_limits);
885
886 if ((MSM_PM_DEBUG_IDLE_LIMITS & msm_pm_debug_mask) &&
887 rs_limits)
888 pr_info("CPU%u: %s: limit %p: "
889 "pxo %d, l2_cache %d, "
890 "vdd_mem %d, vdd_dig %d\n",
891 dev->cpu, __func__, rs_limits,
892 rs_limits->pxo,
893 rs_limits->l2_cache,
894 rs_limits->vdd_mem,
895 rs_limits->vdd_dig);
896
897 if (!rs_limits)
898 allow = false;
899 break;
900
901 default:
902 allow = false;
903 break;
904 }
905
906 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
907 pr_info("CPU%u: %s: allow %s: %d\n",
908 dev->cpu, __func__, state->desc, (int)allow);
909
910 if (allow) {
911 state->flags &= ~CPUIDLE_FLAG_IGNORE;
912 state->target_residency = 0;
913 state->exit_latency = 0;
914 state->power_usage = rs_limits->power[dev->cpu];
915
916 if (MSM_PM_SLEEP_MODE_POWER_COLLAPSE == mode)
917 msm_pm_idle_rs_limits = rs_limits;
918 } else {
919 state->flags |= CPUIDLE_FLAG_IGNORE;
920 }
921 }
922
923 return 0;
924}
925
926int msm_pm_idle_enter(enum msm_pm_sleep_mode sleep_mode)
927{
928 int64_t time;
929#ifdef CONFIG_MSM_IDLE_STATS
930 int exit_stat;
931#endif
932
933 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
934 pr_info("CPU%u: %s: mode %d\n",
935 smp_processor_id(), __func__, sleep_mode);
936
937 time = ktime_to_ns(ktime_get());
938
939 switch (sleep_mode) {
940 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
941 msm_pm_swfi();
942#ifdef CONFIG_MSM_IDLE_STATS
943 exit_stat = MSM_PM_STAT_IDLE_WFI;
944#endif
945 break;
946
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600947 case MSM_PM_SLEEP_MODE_RETENTION:
948 msm_pm_retention();
949#ifdef CONFIG_MSM_IDLE_STATS
950 exit_stat = MSM_PM_STAT_RETENTION;
951#endif
952 break;
953
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700954 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
955 msm_pm_power_collapse_standalone(true);
956#ifdef CONFIG_MSM_IDLE_STATS
957 exit_stat = MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE;
958#endif
959 break;
960
961 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE: {
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600962 int64_t timer_expiration = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700963 bool timer_halted = false;
964 uint32_t sleep_delay;
965 int ret;
966 int notify_rpm =
967 (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600968 int collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700969
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600970 timer_expiration = msm_pm_timer_enter_idle();
971
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700972 sleep_delay = (uint32_t) msm_pm_convert_and_cap_time(
973 timer_expiration, MSM_PM_SLEEP_TICK_LIMIT);
974 if (sleep_delay == 0) /* 0 would mean infinite time */
975 sleep_delay = 1;
976
Karthik Parsha6fb932d2012-01-24 18:04:12 -0800977 if (MSM_PM_DEBUG_IDLE_CLK & msm_pm_debug_mask)
978 clock_debug_print_enabled();
979
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700980 ret = msm_rpmrs_enter_sleep(
981 sleep_delay, msm_pm_idle_rs_limits, true, notify_rpm);
982 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600983 collapsed = msm_pm_power_collapse(true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700984 timer_halted = true;
985
986 msm_rpmrs_exit_sleep(msm_pm_idle_rs_limits, true,
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600987 notify_rpm, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700988 }
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600989 msm_pm_timer_exit_idle(timer_halted);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700990
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700991#ifdef CONFIG_MSM_IDLE_STATS
992 exit_stat = MSM_PM_STAT_IDLE_POWER_COLLAPSE;
993#endif
994 break;
995 }
996
997 default:
998 __WARN();
999 goto cpuidle_enter_bail;
1000 }
1001
1002 time = ktime_to_ns(ktime_get()) - time;
1003#ifdef CONFIG_MSM_IDLE_STATS
1004 msm_pm_add_stat(exit_stat, time);
1005#endif
1006
1007 do_div(time, 1000);
1008 return (int) time;
1009
1010cpuidle_enter_bail:
1011 return 0;
1012}
1013
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -06001014static struct msm_pm_sleep_status_data *msm_pm_slp_sts;
1015
1016static DEFINE_PER_CPU_SHARED_ALIGNED(enum msm_pm_sleep_mode,
1017 msm_pm_last_slp_mode);
1018
1019bool msm_pm_verify_cpu_pc(unsigned int cpu)
1020{
1021 enum msm_pm_sleep_mode mode = per_cpu(msm_pm_last_slp_mode, cpu);
1022
1023 if (msm_pm_slp_sts)
1024 if ((mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE) ||
1025 (mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE))
1026 return true;
1027
1028 return false;
1029}
1030
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -07001031void msm_pm_cpu_enter_lowpower(unsigned int cpu)
1032{
1033 int i;
1034 bool allow[MSM_PM_SLEEP_MODE_NR];
1035
1036 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
1037 struct msm_pm_platform_data *mode;
1038
Praveen Chidambaram42da9d22012-03-30 12:16:34 -06001039 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -07001040 allow[i] = mode->suspend_supported && mode->suspend_enabled;
1041 }
1042
1043 if (MSM_PM_DEBUG_HOTPLUG & msm_pm_debug_mask)
1044 pr_notice("CPU%u: %s: shutting down cpu\n", cpu, __func__);
1045
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -06001046 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) {
1047 per_cpu(msm_pm_last_slp_mode, cpu)
1048 = MSM_PM_SLEEP_MODE_POWER_COLLAPSE;
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -07001049 msm_pm_power_collapse(false);
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -06001050 } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
1051 per_cpu(msm_pm_last_slp_mode, cpu)
1052 = MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE;
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -07001053 msm_pm_power_collapse_standalone(false);
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -06001054 } else if (allow[MSM_PM_SLEEP_MODE_RETENTION]) {
1055 per_cpu(msm_pm_last_slp_mode, cpu)
1056 = MSM_PM_SLEEP_MODE_RETENTION;
1057 msm_pm_retention();
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -06001058 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
1059 per_cpu(msm_pm_last_slp_mode, cpu)
1060 = MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE;
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -07001061 msm_pm_swfi();
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -06001062 } else
1063 per_cpu(msm_pm_last_slp_mode, cpu) = MSM_PM_SLEEP_MODE_NR;
1064}
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -07001065
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -06001066int msm_pm_wait_cpu_shutdown(unsigned int cpu)
1067{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -07001068
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -06001069 int timeout = 10;
1070
1071 if (!msm_pm_slp_sts)
1072 return 0;
1073
1074 while (timeout--) {
1075
1076 /*
1077 * Check for the SPM of the core being hotplugged to set
1078 * its sleep state.The SPM sleep state indicates that the
1079 * core has been power collapsed.
1080 */
1081
1082 int acc_sts = __raw_readl(msm_pm_slp_sts->base_addr
1083 + cpu * msm_pm_slp_sts->cpu_offset);
1084 mb();
1085
1086 if (acc_sts & msm_pm_slp_sts->mask)
1087 return 0;
1088
1089 usleep(100);
1090 }
1091 pr_warn("%s(): Timed out waiting for CPU %u SPM to enter sleep state",
1092 __func__, cpu);
1093 return -EBUSY;
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -07001094}
1095
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001096static int msm_pm_enter(suspend_state_t state)
1097{
1098 bool allow[MSM_PM_SLEEP_MODE_NR];
1099 int i;
1100
1101#ifdef CONFIG_MSM_IDLE_STATS
1102 int64_t period = 0;
1103 int64_t time = msm_timer_get_sclk_time(&period);
1104#endif
1105
1106 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1107 pr_info("%s\n", __func__);
1108
1109 if (smp_processor_id()) {
1110 __WARN();
1111 goto enter_exit;
1112 }
1113
1114
1115 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
1116 struct msm_pm_platform_data *mode;
1117
Praveen Chidambaram42da9d22012-03-30 12:16:34 -06001118 mode = &msm_pm_sleep_modes[MSM_PM_MODE(0, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001119 allow[i] = mode->suspend_supported && mode->suspend_enabled;
1120 }
1121
1122 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) {
1123 struct msm_rpmrs_limits *rs_limits;
1124 int ret;
1125
1126 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1127 pr_info("%s: power collapse\n", __func__);
1128
1129 clock_debug_print_enabled();
1130
1131#ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE
1132 if (msm_pm_sleep_time_override > 0) {
1133 int64_t ns = NSEC_PER_SEC *
1134 (int64_t) msm_pm_sleep_time_override;
1135 msm_pm_set_max_sleep_time(ns);
1136 msm_pm_sleep_time_override = 0;
1137 }
1138#endif /* CONFIG_MSM_SLEEP_TIME_OVERRIDE */
1139
1140 if (MSM_PM_DEBUG_SUSPEND_LIMITS & msm_pm_debug_mask)
1141 msm_rpmrs_show_resources();
1142
1143 rs_limits = msm_rpmrs_lowest_limits(false,
1144 MSM_PM_SLEEP_MODE_POWER_COLLAPSE, -1, -1);
1145
1146 if ((MSM_PM_DEBUG_SUSPEND_LIMITS & msm_pm_debug_mask) &&
1147 rs_limits)
1148 pr_info("%s: limit %p: pxo %d, l2_cache %d, "
1149 "vdd_mem %d, vdd_dig %d\n",
1150 __func__, rs_limits,
1151 rs_limits->pxo, rs_limits->l2_cache,
1152 rs_limits->vdd_mem, rs_limits->vdd_dig);
1153
1154 if (rs_limits) {
1155 ret = msm_rpmrs_enter_sleep(
1156 msm_pm_max_sleep_time, rs_limits, false, true);
1157 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -06001158 int collapsed = msm_pm_power_collapse(false);
1159 msm_rpmrs_exit_sleep(rs_limits, false, true,
1160 collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001161 }
1162 } else {
1163 pr_err("%s: cannot find the lowest power limit\n",
1164 __func__);
1165 }
1166
1167#ifdef CONFIG_MSM_IDLE_STATS
1168 if (time != 0) {
1169 int64_t end_time = msm_timer_get_sclk_time(NULL);
1170 if (end_time != 0) {
1171 time = end_time - time;
1172 if (time < 0)
1173 time += period;
1174 } else
1175 time = 0;
1176 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001177 msm_pm_add_stat(MSM_PM_STAT_SUSPEND, time);
1178#endif /* CONFIG_MSM_IDLE_STATS */
1179 } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
1180 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1181 pr_info("%s: standalone power collapse\n", __func__);
1182 msm_pm_power_collapse_standalone(false);
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -06001183 } else if (allow[MSM_PM_SLEEP_MODE_RETENTION]) {
1184 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1185 pr_info("%s: retention\n", __func__);
1186 msm_pm_retention();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001187 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
1188 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1189 pr_info("%s: swfi\n", __func__);
1190 msm_pm_swfi();
1191 }
1192
1193
1194enter_exit:
1195 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1196 pr_info("%s: return\n", __func__);
1197
1198 return 0;
1199}
1200
1201static struct platform_suspend_ops msm_pm_ops = {
1202 .enter = msm_pm_enter,
1203 .valid = suspend_valid_only_mem,
1204};
1205
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001206/******************************************************************************
1207 * Initialization routine
1208 *****************************************************************************/
Maheshkumar Sivasubramanian6866b1c2011-06-07 14:20:33 -06001209void __init msm_pm_init_sleep_status_data(
1210 struct msm_pm_sleep_status_data *data)
1211{
1212 msm_pm_slp_sts = data;
1213}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001214
1215static int __init msm_pm_init(void)
1216{
1217 pgd_t *pc_pgd;
1218 pmd_t *pmd;
1219 unsigned long pmdval;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001220#ifdef CONFIG_MSM_IDLE_STATS
Praveen Chidambaram192979f2012-04-25 18:30:23 -06001221 unsigned int cpu;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001222 struct proc_dir_entry *d_entry;
1223#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001224 /* Page table for cores to come back up safely. */
1225 pc_pgd = pgd_alloc(&init_mm);
1226 if (!pc_pgd)
1227 return -ENOMEM;
1228
1229 pmd = pmd_offset(pc_pgd +
1230 pgd_index(virt_to_phys(msm_pm_collapse_exit)),
1231 virt_to_phys(msm_pm_collapse_exit));
1232 pmdval = (virt_to_phys(msm_pm_collapse_exit) & PGDIR_MASK) |
1233 PMD_TYPE_SECT | PMD_SECT_AP_WRITE;
1234 pmd[0] = __pmd(pmdval);
1235 pmd[1] = __pmd(pmdval + (1 << (PGDIR_SHIFT - 1)));
1236
Steve Mucklefcece052012-02-18 20:09:58 -08001237 msm_saved_state_phys =
1238 allocate_contiguous_ebi_nomap(CPU_SAVED_STATE_SIZE *
1239 num_possible_cpus(), 4);
1240 if (!msm_saved_state_phys)
1241 return -ENOMEM;
1242 msm_saved_state = ioremap_nocache(msm_saved_state_phys,
1243 CPU_SAVED_STATE_SIZE *
1244 num_possible_cpus());
1245 if (!msm_saved_state)
1246 return -ENOMEM;
1247
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001248 /* It is remotely possible that the code in msm_pm_collapse_exit()
1249 * which turns on the MMU with this mapping is in the
1250 * next even-numbered megabyte beyond the
1251 * start of msm_pm_collapse_exit().
1252 * Map this megabyte in as well.
1253 */
1254 pmd[2] = __pmd(pmdval + (2 << (PGDIR_SHIFT - 1)));
1255 flush_pmd_entry(pmd);
1256 msm_pm_pc_pgd = virt_to_phys(pc_pgd);
Steve Muckle730ad7a2012-02-21 15:26:37 -08001257 clean_caches((unsigned long)&msm_pm_pc_pgd, sizeof(msm_pm_pc_pgd),
1258 virt_to_phys(&msm_pm_pc_pgd));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001259
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001260#ifdef CONFIG_MSM_IDLE_STATS
1261 for_each_possible_cpu(cpu) {
1262 struct msm_pm_time_stats *stats =
1263 per_cpu(msm_pm_stats, cpu).stats;
1264
1265 stats[MSM_PM_STAT_REQUESTED_IDLE].name = "idle-request";
1266 stats[MSM_PM_STAT_REQUESTED_IDLE].first_bucket_time =
1267 CONFIG_MSM_IDLE_STATS_FIRST_BUCKET;
1268
1269 stats[MSM_PM_STAT_IDLE_WFI].name = "idle-wfi";
1270 stats[MSM_PM_STAT_IDLE_WFI].first_bucket_time =
1271 CONFIG_MSM_IDLE_STATS_FIRST_BUCKET;
1272
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -06001273 stats[MSM_PM_STAT_RETENTION].name = "retention";
1274 stats[MSM_PM_STAT_RETENTION].first_bucket_time =
1275 CONFIG_MSM_IDLE_STATS_FIRST_BUCKET;
1276
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001277 stats[MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE].name =
1278 "idle-standalone-power-collapse";
1279 stats[MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE].
1280 first_bucket_time = CONFIG_MSM_IDLE_STATS_FIRST_BUCKET;
1281
1282 stats[MSM_PM_STAT_IDLE_POWER_COLLAPSE].name =
1283 "idle-power-collapse";
1284 stats[MSM_PM_STAT_IDLE_POWER_COLLAPSE].first_bucket_time =
1285 CONFIG_MSM_IDLE_STATS_FIRST_BUCKET;
1286
1287 stats[MSM_PM_STAT_SUSPEND].name = "suspend";
1288 stats[MSM_PM_STAT_SUSPEND].first_bucket_time =
1289 CONFIG_MSM_SUSPEND_STATS_FIRST_BUCKET;
1290 }
1291
1292 d_entry = create_proc_entry("msm_pm_stats",
1293 S_IRUGO | S_IWUSR | S_IWGRP, NULL);
1294 if (d_entry) {
1295 d_entry->read_proc = msm_pm_read_proc;
1296 d_entry->write_proc = msm_pm_write_proc;
1297 d_entry->data = NULL;
1298 }
1299#endif /* CONFIG_MSM_IDLE_STATS */
1300
Praveen Chidambaram192979f2012-04-25 18:30:23 -06001301
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001302 msm_pm_mode_sysfs_add();
1303 msm_spm_allow_x_cpu_set_vdd(false);
1304
1305 suspend_set_ops(&msm_pm_ops);
Praveen Chidambaram192979f2012-04-25 18:30:23 -06001306 msm_pm_qtimer_available();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001307 msm_cpuidle_init();
1308
1309 return 0;
1310}
1311
1312late_initcall(msm_pm_init);