blob: 89b8c92669fb11c439e6df57f0df9a6994329af4 [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>
Praveen Chidambaram79f3b4a2012-09-18 19:48:29 -060032#include <mach/msm-krait-l2-accessors.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070033#include <asm/cacheflush.h>
34#include <asm/hardware/gic.h>
35#include <asm/pgtable.h>
36#include <asm/pgalloc.h>
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -060037#include <asm/hardware/cache-l2x0.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070038#ifdef CONFIG_VFP
39#include <asm/vfp.h>
40#endif
41
42#include "acpuclock.h"
43#include "clock.h"
44#include "avs.h"
Abhijeet Dharmapurikarefaca4f2011-12-27 16:24:07 -080045#include <mach/cpuidle.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070046#include "idle.h"
Matt Wagantall7cca4642012-02-01 16:43:24 -080047#include "pm.h"
Jaeseong GIMfc880da2012-08-13 18:47:46 -070048#include "rpm_resources.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070049#include "scm-boot.h"
50#include "spm.h"
51#include "timer.h"
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -060052#include "pm-boot.h"
Girish Mahadevan12aaabe2012-08-17 16:48:05 -060053#include <mach/event_timer.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);
Girish Mahadevan63b87262012-08-15 09:21:23 -060075static int msm_pm_retention_tz_call;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070076
77
78/******************************************************************************
79 * Sleep Modes and Parameters
80 *****************************************************************************/
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070081enum {
82 MSM_PM_MODE_ATTR_SUSPEND,
83 MSM_PM_MODE_ATTR_IDLE,
84 MSM_PM_MODE_ATTR_NR,
85};
86
Girish Mahadevan63b87262012-08-15 09:21:23 -060087#define SCM_L2_RETENTION (0x2)
88#define SCM_CMD_TERMINATE_PC (0x2)
89
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070090static char *msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_NR] = {
91 [MSM_PM_MODE_ATTR_SUSPEND] = "suspend_enabled",
92 [MSM_PM_MODE_ATTR_IDLE] = "idle_enabled",
93};
94
95struct msm_pm_kobj_attribute {
96 unsigned int cpu;
97 struct kobj_attribute ka;
98};
99
100#define GET_CPU_OF_ATTR(attr) \
101 (container_of(attr, struct msm_pm_kobj_attribute, ka)->cpu)
102
103struct msm_pm_sysfs_sleep_mode {
104 struct kobject *kobj;
105 struct attribute_group attr_group;
106 struct attribute *attrs[MSM_PM_MODE_ATTR_NR + 1];
107 struct msm_pm_kobj_attribute kas[MSM_PM_MODE_ATTR_NR];
108};
109
110static char *msm_pm_sleep_mode_labels[MSM_PM_SLEEP_MODE_NR] = {
111 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE] = "power_collapse",
112 [MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT] = "wfi",
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600113 [MSM_PM_SLEEP_MODE_RETENTION] = "retention",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700114 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE] =
115 "standalone_power_collapse",
116};
117
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600118static struct hrtimer pm_hrtimer;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600119static struct msm_pm_sleep_ops pm_sleep_ops;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700120/*
121 * Write out the attribute.
122 */
123static ssize_t msm_pm_mode_attr_show(
124 struct kobject *kobj, struct kobj_attribute *attr, char *buf)
125{
126 int ret = -EINVAL;
127 int i;
128
129 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
130 struct kernel_param kp;
131 unsigned int cpu;
132 struct msm_pm_platform_data *mode;
133
134 if (msm_pm_sleep_mode_labels[i] == NULL)
135 continue;
136
137 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
138 continue;
139
140 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600141 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700142
143 if (!strcmp(attr->attr.name,
144 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
145 u32 arg = mode->suspend_enabled;
146 kp.arg = &arg;
147 ret = param_get_ulong(buf, &kp);
148 } else if (!strcmp(attr->attr.name,
149 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
150 u32 arg = mode->idle_enabled;
151 kp.arg = &arg;
152 ret = param_get_ulong(buf, &kp);
153 }
154
155 break;
156 }
157
158 if (ret > 0) {
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600159 strlcat(buf, "\n", PAGE_SIZE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700160 ret++;
161 }
162
163 return ret;
164}
165
166/*
167 * Read in the new attribute value.
168 */
169static ssize_t msm_pm_mode_attr_store(struct kobject *kobj,
170 struct kobj_attribute *attr, const char *buf, size_t count)
171{
172 int ret = -EINVAL;
173 int i;
174
175 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
176 struct kernel_param kp;
177 unsigned int cpu;
178 struct msm_pm_platform_data *mode;
179
180 if (msm_pm_sleep_mode_labels[i] == NULL)
181 continue;
182
183 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
184 continue;
185
186 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600187 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700188
189 if (!strcmp(attr->attr.name,
190 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
191 kp.arg = &mode->suspend_enabled;
192 ret = param_set_byte(buf, &kp);
193 } else if (!strcmp(attr->attr.name,
194 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
195 kp.arg = &mode->idle_enabled;
196 ret = param_set_byte(buf, &kp);
197 }
198
199 break;
200 }
201
202 return ret ? ret : count;
203}
204
205/*
206 * Add sysfs entries for one cpu.
207 */
208static int __init msm_pm_mode_sysfs_add_cpu(
209 unsigned int cpu, struct kobject *modes_kobj)
210{
211 char cpu_name[8];
212 struct kobject *cpu_kobj;
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600213 struct msm_pm_sysfs_sleep_mode *mode = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700214 int i, j, k;
215 int ret;
216
217 snprintf(cpu_name, sizeof(cpu_name), "cpu%u", cpu);
218 cpu_kobj = kobject_create_and_add(cpu_name, modes_kobj);
219 if (!cpu_kobj) {
220 pr_err("%s: cannot create %s kobject\n", __func__, cpu_name);
221 ret = -ENOMEM;
222 goto mode_sysfs_add_cpu_exit;
223 }
224
225 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
226 int idx = MSM_PM_MODE(cpu, i);
227
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600228 if ((!msm_pm_sleep_modes[idx].suspend_supported)
229 && (!msm_pm_sleep_modes[idx].idle_supported))
230 continue;
231
232 if (!msm_pm_sleep_mode_labels[i] ||
233 !msm_pm_sleep_mode_labels[i][0])
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700234 continue;
235
236 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
237 if (!mode) {
238 pr_err("%s: cannot allocate memory for attributes\n",
239 __func__);
240 ret = -ENOMEM;
241 goto mode_sysfs_add_cpu_exit;
242 }
243
244 mode->kobj = kobject_create_and_add(
245 msm_pm_sleep_mode_labels[i], cpu_kobj);
246 if (!mode->kobj) {
247 pr_err("%s: cannot create kobject\n", __func__);
248 ret = -ENOMEM;
249 goto mode_sysfs_add_cpu_exit;
250 }
251
252 for (k = 0, j = 0; k < MSM_PM_MODE_ATTR_NR; k++) {
253 if ((k == MSM_PM_MODE_ATTR_IDLE) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600254 !msm_pm_sleep_modes[idx].idle_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700255 continue;
256 if ((k == MSM_PM_MODE_ATTR_SUSPEND) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600257 !msm_pm_sleep_modes[idx].suspend_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700258 continue;
Stephen Boydd2059c32012-07-03 15:11:15 -0700259 sysfs_attr_init(&mode->kas[j].ka.attr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700260 mode->kas[j].cpu = cpu;
261 mode->kas[j].ka.attr.mode = 0644;
262 mode->kas[j].ka.show = msm_pm_mode_attr_show;
263 mode->kas[j].ka.store = msm_pm_mode_attr_store;
264 mode->kas[j].ka.attr.name = msm_pm_mode_attr_labels[k];
265 mode->attrs[j] = &mode->kas[j].ka.attr;
266 j++;
267 }
268 mode->attrs[j] = NULL;
269
270 mode->attr_group.attrs = mode->attrs;
271 ret = sysfs_create_group(mode->kobj, &mode->attr_group);
272 if (ret) {
273 pr_err("%s: cannot create kobject attribute group\n",
274 __func__);
275 goto mode_sysfs_add_cpu_exit;
276 }
277 }
278
279 ret = 0;
280
281mode_sysfs_add_cpu_exit:
Praveen Chidambaramd5ac2d32011-10-24 14:30:27 -0600282 if (ret) {
Praveen Chidambaram2cfda632011-10-11 16:58:09 -0600283 if (mode && mode->kobj)
284 kobject_del(mode->kobj);
285 kfree(mode);
286 }
287
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700288 return ret;
289}
290
291/*
292 * Add sysfs entries for the sleep modes.
293 */
294static int __init msm_pm_mode_sysfs_add(void)
295{
296 struct kobject *module_kobj;
297 struct kobject *modes_kobj;
298 unsigned int cpu;
299 int ret;
300
301 module_kobj = kset_find_obj(module_kset, KBUILD_MODNAME);
302 if (!module_kobj) {
303 pr_err("%s: cannot find kobject for module %s\n",
304 __func__, KBUILD_MODNAME);
305 ret = -ENOENT;
306 goto mode_sysfs_add_exit;
307 }
308
309 modes_kobj = kobject_create_and_add("modes", module_kobj);
310 if (!modes_kobj) {
311 pr_err("%s: cannot create modes kobject\n", __func__);
312 ret = -ENOMEM;
313 goto mode_sysfs_add_exit;
314 }
315
316 for_each_possible_cpu(cpu) {
317 ret = msm_pm_mode_sysfs_add_cpu(cpu, modes_kobj);
318 if (ret)
319 goto mode_sysfs_add_exit;
320 }
321
322 ret = 0;
323
324mode_sysfs_add_exit:
325 return ret;
326}
327
328/******************************************************************************
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700329 * Configure Hardware before/after Low Power Mode
330 *****************************************************************************/
331
332/*
333 * Configure hardware registers in preparation for Apps power down.
334 */
335static void msm_pm_config_hw_before_power_down(void)
336{
337 return;
338}
339
340/*
341 * Clear hardware registers after Apps powers up.
342 */
343static void msm_pm_config_hw_after_power_up(void)
344{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700345}
346
347/*
348 * Configure hardware registers in preparation for SWFI.
349 */
350static void msm_pm_config_hw_before_swfi(void)
351{
352 return;
353}
354
Girish Mahadevan63b87262012-08-15 09:21:23 -0600355/*
356 * Configure/Restore hardware registers in preparation for Retention.
357 */
358
359static void msm_pm_config_hw_after_retention(void)
360{
361 int ret;
362 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
363 WARN_ON(ret);
364}
365
366static void msm_pm_config_hw_before_retention(void)
367{
368 return;
369}
370
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700371
372/******************************************************************************
373 * Suspend Max Sleep Time
374 *****************************************************************************/
375
376#ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE
377static int msm_pm_sleep_time_override;
378module_param_named(sleep_time_override,
379 msm_pm_sleep_time_override, int, S_IRUGO | S_IWUSR | S_IWGRP);
380#endif
381
382#define SCLK_HZ (32768)
383#define MSM_PM_SLEEP_TICK_LIMIT (0x6DDD000)
384
385static uint32_t msm_pm_max_sleep_time;
386
387/*
388 * Convert time from nanoseconds to slow clock ticks, then cap it to the
389 * specified limit
390 */
391static int64_t msm_pm_convert_and_cap_time(int64_t time_ns, int64_t limit)
392{
393 do_div(time_ns, NSEC_PER_SEC / SCLK_HZ);
394 return (time_ns > limit) ? limit : time_ns;
395}
396
397/*
398 * Set the sleep time for suspend. 0 means infinite sleep time.
399 */
400void msm_pm_set_max_sleep_time(int64_t max_sleep_time_ns)
401{
402 if (max_sleep_time_ns == 0) {
403 msm_pm_max_sleep_time = 0;
404 } else {
405 msm_pm_max_sleep_time = (uint32_t)msm_pm_convert_and_cap_time(
406 max_sleep_time_ns, MSM_PM_SLEEP_TICK_LIMIT);
407
408 if (msm_pm_max_sleep_time == 0)
409 msm_pm_max_sleep_time = 1;
410 }
411
412 if (msm_pm_debug_mask & MSM_PM_DEBUG_SUSPEND)
413 pr_info("%s: Requested %lld ns Giving %u sclk ticks\n",
414 __func__, max_sleep_time_ns, msm_pm_max_sleep_time);
415}
416EXPORT_SYMBOL(msm_pm_set_max_sleep_time);
417
Praveen Chidambaram79f3b4a2012-09-18 19:48:29 -0600418struct reg_data {
419 uint32_t reg;
420 uint32_t val;
421};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700422
Praveen Chidambaram79f3b4a2012-09-18 19:48:29 -0600423static struct reg_data reg_saved_state[] = {
424 { .reg = 0x4501, },
425 { .reg = 0x5501, },
426 { .reg = 0x6501, },
427 { .reg = 0x7501, },
428 { .reg = 0x0500, },
429};
430
431static unsigned int active_vdd;
432static bool msm_pm_save_cp15;
433static const unsigned int pc_vdd = 0x98;
434
435static void msm_pm_save_cpu_reg(void)
436{
437 int i;
438
439 /* Only on core0 */
440 if (smp_processor_id())
441 return;
442
443 /**
444 * On some targets, L2 PC will turn off may reset the core
445 * configuration for the mux and the default may not make the core
446 * happy when it resumes.
447 * Save the active vdd, and set the core vdd to QSB max vdd, so that
448 * when the core resumes, it is capable of supporting the current QSB
449 * rate. Then restore the active vdd before switching the acpuclk rate.
450 */
451 if (msm_pm_get_l2_flush_flag() == 1) {
452 active_vdd = msm_spm_get_vdd(0);
453 for (i = 0; i < ARRAY_SIZE(reg_saved_state); i++)
454 reg_saved_state[i].val =
455 get_l2_indirect_reg(reg_saved_state[i].reg);
456 msm_spm_set_vdd(0, pc_vdd);
457 }
458}
459
460static void msm_pm_restore_cpu_reg(void)
461{
462 int i;
463
464 /* Only on core0 */
465 if (smp_processor_id())
466 return;
467
468 if (msm_pm_get_l2_flush_flag() == 1) {
469 for (i = 0; i < ARRAY_SIZE(reg_saved_state); i++)
470 set_l2_indirect_reg(reg_saved_state[i].reg,
471 reg_saved_state[i].val);
472 msm_spm_set_vdd(0, active_vdd);
473 }
474}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700475
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600476static void *msm_pm_idle_rs_limits;
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600477static bool msm_pm_use_qtimer;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700478
479static void msm_pm_swfi(void)
480{
481 msm_pm_config_hw_before_swfi();
482 msm_arch_idle();
483}
484
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600485
486static void msm_pm_retention(void)
487{
488 int ret = 0;
489
Girish Mahadevan63b87262012-08-15 09:21:23 -0600490 msm_pm_config_hw_before_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600491 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_POWER_RETENTION, false);
492 WARN_ON(ret);
Girish Mahadevan63b87262012-08-15 09:21:23 -0600493
494 if (msm_pm_retention_tz_call)
495 scm_call_atomic1(SCM_SVC_BOOT, SCM_CMD_TERMINATE_PC,
496 SCM_L2_RETENTION);
497 else
498 msm_arch_idle();
499
500 msm_pm_config_hw_after_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600501}
502
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600503#ifdef CONFIG_CACHE_L2X0
504static inline bool msm_pm_l2x0_power_collapse(void)
505{
506 bool collapsed = 0;
507
Taniya Das38a8c6e2012-05-09 20:34:39 +0530508 l2cc_suspend();
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600509 collapsed = msm_pm_collapse();
Taniya Das38a8c6e2012-05-09 20:34:39 +0530510 l2cc_resume();
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600511
512 return collapsed;
513}
514#else
515static inline bool msm_pm_l2x0_power_collapse(void)
516{
517 return msm_pm_collapse();
518}
519#endif
520
Stephen Boydb29750d2012-02-21 01:21:32 -0800521static bool __ref msm_pm_spm_power_collapse(
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700522 unsigned int cpu, bool from_idle, bool notify_rpm)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700523{
524 void *entry;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600525 bool collapsed = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700526 int ret;
Rohit Vaswanie78dfb62012-02-21 10:29:29 -0800527 unsigned int saved_gic_cpu_ctrl;
528
529 saved_gic_cpu_ctrl = readl_relaxed(MSM_QGIC_CPU_BASE + GIC_CPU_CTRL);
530 mb();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700531
532 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
533 pr_info("CPU%u: %s: notify_rpm %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700534 cpu, __func__, (int) notify_rpm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700535
536 ret = msm_spm_set_low_power_mode(
537 MSM_SPM_MODE_POWER_COLLAPSE, notify_rpm);
538 WARN_ON(ret);
539
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700540 entry = (!cpu || from_idle) ?
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700541 msm_pm_collapse_exit : msm_secondary_startup;
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700542 msm_pm_boot_config_before_pc(cpu, virt_to_phys(entry));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700543
544 if (MSM_PM_DEBUG_RESET_VECTOR & msm_pm_debug_mask)
545 pr_info("CPU%u: %s: program vector to %p\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700546 cpu, __func__, entry);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700547
548#ifdef CONFIG_VFP
Steve Mucklef132c6c2012-06-06 18:30:57 -0700549 vfp_pm_suspend();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700550#endif
551
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600552 collapsed = msm_pm_l2x0_power_collapse();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700553
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700554 msm_pm_boot_config_after_pc(cpu);
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -0600555
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700556 if (collapsed) {
557#ifdef CONFIG_VFP
Steve Mucklef132c6c2012-06-06 18:30:57 -0700558 vfp_pm_resume();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700559#endif
560 cpu_init();
561 writel(0xF0, MSM_QGIC_CPU_BASE + GIC_CPU_PRIMASK);
Rohit Vaswanie78dfb62012-02-21 10:29:29 -0800562 writel_relaxed(saved_gic_cpu_ctrl,
563 MSM_QGIC_CPU_BASE + GIC_CPU_CTRL);
564 mb();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700565 local_fiq_enable();
566 }
567
568 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
569 pr_info("CPU%u: %s: msm_pm_collapse returned, collapsed %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700570 cpu, __func__, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700571
572 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
573 WARN_ON(ret);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600574 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700575}
576
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600577static bool msm_pm_power_collapse_standalone(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700578{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700579 unsigned int cpu = smp_processor_id();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700580 unsigned int avsdscr_setting;
Mahesh Sivasubramanianc1145072012-10-24 13:53:48 -0600581 unsigned int avscsr_enable;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600582 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700583
584 avsdscr_setting = avs_get_avsdscr();
Mahesh Sivasubramanianc1145072012-10-24 13:53:48 -0600585 avscsr_enable = avs_disable();
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700586 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, false);
Mahesh Sivasubramanianc1145072012-10-24 13:53:48 -0600587 avs_enable(avscsr_enable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700588 avs_reset_delays(avsdscr_setting);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600589 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700590}
591
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600592static bool msm_pm_power_collapse(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700593{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700594 unsigned int cpu = smp_processor_id();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700595 unsigned long saved_acpuclk_rate;
596 unsigned int avsdscr_setting;
Mahesh Sivasubramanianc1145072012-10-24 13:53:48 -0600597 unsigned int avscsr_enable;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600598 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700599
600 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
601 pr_info("CPU%u: %s: idle %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700602 cpu, __func__, (int)from_idle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700603
604 msm_pm_config_hw_before_power_down();
605 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700606 pr_info("CPU%u: %s: pre power down\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700607
608 avsdscr_setting = avs_get_avsdscr();
Mahesh Sivasubramanianc1145072012-10-24 13:53:48 -0600609 avscsr_enable = avs_disable();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700610
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700611 if (cpu_online(cpu))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700612 saved_acpuclk_rate = acpuclk_power_collapse();
613 else
614 saved_acpuclk_rate = 0;
615
616 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
617 pr_info("CPU%u: %s: change clock rate (old rate = %lu)\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700618 cpu, __func__, saved_acpuclk_rate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700619
Praveen Chidambaram79f3b4a2012-09-18 19:48:29 -0600620 if (msm_pm_save_cp15)
621 msm_pm_save_cpu_reg();
622
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700623 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700624
Praveen Chidambaram79f3b4a2012-09-18 19:48:29 -0600625 if (msm_pm_save_cp15)
626 msm_pm_restore_cpu_reg();
627
Girish Mahadevan4e025d92012-02-29 13:26:51 -0700628 if (cpu_online(cpu)) {
629 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
630 pr_info("CPU%u: %s: restore clock rate to %lu\n",
631 cpu, __func__, saved_acpuclk_rate);
632 if (acpuclk_set_rate(cpu, saved_acpuclk_rate, SETRATE_PC) < 0)
633 pr_err("CPU%u: %s: failed to restore clock rate(%lu)\n",
634 cpu, __func__, saved_acpuclk_rate);
635 } else {
636 unsigned int gic_dist_enabled;
637 unsigned int gic_dist_pending;
638 gic_dist_enabled = readl_relaxed(
639 MSM_QGIC_DIST_BASE + GIC_DIST_ENABLE_CLEAR);
640 gic_dist_pending = readl_relaxed(
641 MSM_QGIC_DIST_BASE + GIC_DIST_PENDING_SET);
642 mb();
643 gic_dist_pending &= gic_dist_enabled;
644
645 if (gic_dist_pending)
646 pr_err("CPU %d interrupted during hotplug.Pending int 0x%x\n",
647 cpu, gic_dist_pending);
648 }
649
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700650
Mahesh Sivasubramanianc1145072012-10-24 13:53:48 -0600651 avs_enable(avscsr_enable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700652 avs_reset_delays(avsdscr_setting);
653 msm_pm_config_hw_after_power_up();
654 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700655 pr_info("CPU%u: %s: post power up\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700656
657 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700658 pr_info("CPU%u: %s: return\n", cpu, __func__);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600659 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700660}
661
Praveen Chidambaram79f3b4a2012-09-18 19:48:29 -0600662static void msm_pm_target_init(void)
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600663{
Praveen Chidambaram79f3b4a2012-09-18 19:48:29 -0600664 if (cpu_is_apq8064())
665 msm_pm_save_cp15 = true;
666
667 if (cpu_is_msm8974())
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600668 msm_pm_use_qtimer = true;
669}
670
671static int64_t msm_pm_timer_enter_idle(void)
672{
673 if (msm_pm_use_qtimer)
674 return ktime_to_ns(tick_nohz_get_sleep_length());
675
676 return msm_timer_enter_idle();
677}
678
679static void msm_pm_timer_exit_idle(bool timer_halted)
680{
681 if (msm_pm_use_qtimer)
682 return;
683
684 msm_timer_exit_idle((int) timer_halted);
685}
686
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530687static int64_t msm_pm_timer_enter_suspend(int64_t *period)
688{
Anji Jonnalac02367a2012-07-01 02:56:11 +0530689 int64_t time = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530690
691 if (msm_pm_use_qtimer)
692 return sched_clock();
693
694 time = msm_timer_get_sclk_time(period);
695 if (!time)
696 pr_err("%s: Unable to read sclk.\n", __func__);
697
698 return time;
699}
700
701static int64_t msm_pm_timer_exit_suspend(int64_t time, int64_t period)
702{
703 if (msm_pm_use_qtimer)
704 return sched_clock() - time;
705
706 if (time != 0) {
707 int64_t end_time = msm_timer_get_sclk_time(NULL);
708 if (end_time != 0) {
709 time = end_time - time;
710 if (time < 0)
711 time += period;
712 } else
713 time = 0;
714 }
715
716 return time;
717}
718
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600719/**
720 * pm_hrtimer_cb() : Callback function for hrtimer created if the
721 * core needs to be awake to handle an event.
722 * @hrtimer : Pointer to hrtimer
723 */
724static enum hrtimer_restart pm_hrtimer_cb(struct hrtimer *hrtimer)
725{
726 return HRTIMER_NORESTART;
727}
728
729/**
730 * msm_pm_set_timer() : Set an hrtimer to wakeup the core in time
731 * to handle an event.
732 */
733static void msm_pm_set_timer(uint32_t modified_time_us)
734{
735 u64 modified_time_ns = modified_time_us * NSEC_PER_USEC;
736 ktime_t modified_ktime = ns_to_ktime(modified_time_ns);
737 pm_hrtimer.function = pm_hrtimer_cb;
738 hrtimer_start(&pm_hrtimer, modified_ktime, HRTIMER_MODE_ABS);
739}
740
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700741/******************************************************************************
742 * External Idle/Suspend Functions
743 *****************************************************************************/
744
745void arch_idle(void)
746{
747 return;
748}
749
Steve Mucklef132c6c2012-06-06 18:30:57 -0700750int msm_pm_idle_prepare(struct cpuidle_device *dev,
751 struct cpuidle_driver *drv, int index)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700752{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700753 int i;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700754 unsigned int power_usage = -1;
755 int ret = 0;
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600756 uint32_t modified_time_us = 0;
757 struct msm_pm_time_params time_param;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700758
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600759 time_param.latency_us =
760 (uint32_t) pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
761 time_param.sleep_us =
762 (uint32_t) (ktime_to_us(tick_nohz_get_sleep_length())
763 & UINT_MAX);
764 time_param.modified_time_us = 0;
765
766 if (!dev->cpu)
767 time_param.next_event_us =
768 (uint32_t) (ktime_to_us(get_next_event_time())
769 & UINT_MAX);
770 else
771 time_param.next_event_us = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700772
773 for (i = 0; i < dev->state_count; i++) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700774 struct cpuidle_state *state = &drv->states[i];
775 struct cpuidle_state_usage *st_usage = &dev->states_usage[i];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700776 enum msm_pm_sleep_mode mode;
777 bool allow;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600778 void *rs_limits = NULL;
779 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700780 int idx;
781
Steve Mucklef132c6c2012-06-06 18:30:57 -0700782 mode = (enum msm_pm_sleep_mode) cpuidle_get_statedata(st_usage);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700783 idx = MSM_PM_MODE(dev->cpu, mode);
784
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600785 allow = msm_pm_sleep_modes[idx].idle_enabled &&
786 msm_pm_sleep_modes[idx].idle_supported;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700787
788 switch (mode) {
789 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
Mahesh Sivasubramanian96c0aa42012-09-04 18:22:51 -0600790 case MSM_PM_SLEEP_MODE_RETENTION:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700791 if (!allow)
792 break;
793
794 if (num_online_cpus() > 1) {
795 allow = false;
796 break;
797 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700798 /* fall through */
799
800 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
801 if (!allow)
802 break;
Jaeseong GIMfc880da2012-08-13 18:47:46 -0700803
804 if (!dev->cpu && msm_rpm_local_request_is_outstanding()) {
805 allow = false;
806 break;
807 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700808 /* fall through */
809
810 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
811 if (!allow)
812 break;
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600813 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700814
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600815 if (pm_sleep_ops.lowest_limits)
816 rs_limits = pm_sleep_ops.lowest_limits(true,
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600817 mode, &time_param, &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700818
819 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
820 pr_info("CPU%u: %s: %s, latency %uus, "
821 "sleep %uus, limit %p\n",
822 dev->cpu, __func__, state->desc,
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600823 time_param.latency_us,
824 time_param.sleep_us, rs_limits);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700825
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700826 if (!rs_limits)
827 allow = false;
828 break;
829
830 default:
831 allow = false;
832 break;
833 }
834
835 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
836 pr_info("CPU%u: %s: allow %s: %d\n",
837 dev->cpu, __func__, state->desc, (int)allow);
838
839 if (allow) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700840 if (power < power_usage) {
841 power_usage = power;
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600842 modified_time_us = time_param.modified_time_us;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700843 ret = mode;
844 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700845
846 if (MSM_PM_SLEEP_MODE_POWER_COLLAPSE == mode)
847 msm_pm_idle_rs_limits = rs_limits;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700848 }
849 }
850
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600851 if (modified_time_us && !dev->cpu)
852 msm_pm_set_timer(modified_time_us);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700853 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700854}
855
856int msm_pm_idle_enter(enum msm_pm_sleep_mode sleep_mode)
857{
858 int64_t time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700859 int exit_stat;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700860
861 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
862 pr_info("CPU%u: %s: mode %d\n",
863 smp_processor_id(), __func__, sleep_mode);
864
865 time = ktime_to_ns(ktime_get());
866
867 switch (sleep_mode) {
868 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
869 msm_pm_swfi();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700870 exit_stat = MSM_PM_STAT_IDLE_WFI;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700871 break;
872
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600873 case MSM_PM_SLEEP_MODE_RETENTION:
874 msm_pm_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600875 exit_stat = MSM_PM_STAT_RETENTION;
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600876 break;
877
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700878 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
879 msm_pm_power_collapse_standalone(true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700880 exit_stat = MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700881 break;
882
883 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE: {
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600884 int64_t timer_expiration = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700885 bool timer_halted = false;
886 uint32_t sleep_delay;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600887 int ret = -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700888 int notify_rpm =
889 (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600890 int collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700891
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600892 timer_expiration = msm_pm_timer_enter_idle();
893
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700894 sleep_delay = (uint32_t) msm_pm_convert_and_cap_time(
895 timer_expiration, MSM_PM_SLEEP_TICK_LIMIT);
896 if (sleep_delay == 0) /* 0 would mean infinite time */
897 sleep_delay = 1;
898
Karthik Parsha6fb932d2012-01-24 18:04:12 -0800899 if (MSM_PM_DEBUG_IDLE_CLK & msm_pm_debug_mask)
900 clock_debug_print_enabled();
901
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600902 if (pm_sleep_ops.enter_sleep)
903 ret = pm_sleep_ops.enter_sleep(sleep_delay,
904 msm_pm_idle_rs_limits,
905 true, notify_rpm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700906 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600907 collapsed = msm_pm_power_collapse(true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700908 timer_halted = true;
909
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600910 if (pm_sleep_ops.exit_sleep)
911 pm_sleep_ops.exit_sleep(msm_pm_idle_rs_limits,
912 true, notify_rpm, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700913 }
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600914 msm_pm_timer_exit_idle(timer_halted);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700915 exit_stat = MSM_PM_STAT_IDLE_POWER_COLLAPSE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700916 break;
917 }
918
919 default:
920 __WARN();
921 goto cpuidle_enter_bail;
922 }
923
924 time = ktime_to_ns(ktime_get()) - time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700925 msm_pm_add_stat(exit_stat, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700926
927 do_div(time, 1000);
928 return (int) time;
929
930cpuidle_enter_bail:
931 return 0;
932}
933
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700934void msm_pm_cpu_enter_lowpower(unsigned int cpu)
935{
936 int i;
937 bool allow[MSM_PM_SLEEP_MODE_NR];
938
939 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
940 struct msm_pm_platform_data *mode;
941
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600942 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700943 allow[i] = mode->suspend_supported && mode->suspend_enabled;
944 }
945
946 if (MSM_PM_DEBUG_HOTPLUG & msm_pm_debug_mask)
947 pr_notice("CPU%u: %s: shutting down cpu\n", cpu, __func__);
948
Matt Wagantall5375aa72012-07-02 19:59:51 -0700949 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700950 msm_pm_power_collapse(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -0700951 else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700952 msm_pm_power_collapse_standalone(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -0700953 else if (allow[MSM_PM_SLEEP_MODE_RETENTION])
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600954 msm_pm_retention();
Stephen Boydbda74272012-08-09 14:01:27 -0700955 else
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700956 msm_pm_swfi();
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700957}
958
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700959static int msm_pm_enter(suspend_state_t state)
960{
961 bool allow[MSM_PM_SLEEP_MODE_NR];
962 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700963 int64_t period = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530964 int64_t time = msm_pm_timer_enter_suspend(&period);
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600965 struct msm_pm_time_params time_param;
966
967 time_param.latency_us = -1;
968 time_param.sleep_us = -1;
969 time_param.next_event_us = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700970
971 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
972 pr_info("%s\n", __func__);
973
974 if (smp_processor_id()) {
975 __WARN();
976 goto enter_exit;
977 }
978
979
980 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
981 struct msm_pm_platform_data *mode;
982
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600983 mode = &msm_pm_sleep_modes[MSM_PM_MODE(0, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700984 allow[i] = mode->suspend_supported && mode->suspend_enabled;
985 }
986
987 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600988 void *rs_limits = NULL;
989 int ret = -ENODEV;
990 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700991
992 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
993 pr_info("%s: power collapse\n", __func__);
994
995 clock_debug_print_enabled();
996
997#ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE
998 if (msm_pm_sleep_time_override > 0) {
999 int64_t ns = NSEC_PER_SEC *
1000 (int64_t) msm_pm_sleep_time_override;
1001 msm_pm_set_max_sleep_time(ns);
1002 msm_pm_sleep_time_override = 0;
1003 }
1004#endif /* CONFIG_MSM_SLEEP_TIME_OVERRIDE */
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001005 if (pm_sleep_ops.lowest_limits)
1006 rs_limits = pm_sleep_ops.lowest_limits(false,
Girish Mahadevan12aaabe2012-08-17 16:48:05 -06001007 MSM_PM_SLEEP_MODE_POWER_COLLAPSE, &time_param, &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001008
1009 if (rs_limits) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001010 if (pm_sleep_ops.enter_sleep)
1011 ret = pm_sleep_ops.enter_sleep(
1012 msm_pm_max_sleep_time,
1013 rs_limits, false, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001014 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -06001015 int collapsed = msm_pm_power_collapse(false);
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001016 if (pm_sleep_ops.exit_sleep) {
1017 pm_sleep_ops.exit_sleep(rs_limits,
1018 false, true, collapsed);
1019 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001020 }
1021 } else {
1022 pr_err("%s: cannot find the lowest power limit\n",
1023 __func__);
1024 }
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301025 time = msm_pm_timer_exit_suspend(time, period);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001026 msm_pm_add_stat(MSM_PM_STAT_SUSPEND, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001027 } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
1028 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1029 pr_info("%s: standalone power collapse\n", __func__);
1030 msm_pm_power_collapse_standalone(false);
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -06001031 } else if (allow[MSM_PM_SLEEP_MODE_RETENTION]) {
1032 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1033 pr_info("%s: retention\n", __func__);
1034 msm_pm_retention();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001035 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
1036 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1037 pr_info("%s: swfi\n", __func__);
1038 msm_pm_swfi();
1039 }
1040
1041
1042enter_exit:
1043 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1044 pr_info("%s: return\n", __func__);
1045
1046 return 0;
1047}
1048
1049static struct platform_suspend_ops msm_pm_ops = {
1050 .enter = msm_pm_enter,
1051 .valid = suspend_valid_only_mem,
1052};
1053
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001054/******************************************************************************
1055 * Initialization routine
1056 *****************************************************************************/
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001057void msm_pm_set_sleep_ops(struct msm_pm_sleep_ops *ops)
1058{
1059 if (ops)
1060 pm_sleep_ops = *ops;
1061}
1062
Girish Mahadevan63b87262012-08-15 09:21:23 -06001063void __init msm_pm_set_tz_retention_flag(unsigned int flag)
1064{
1065 msm_pm_retention_tz_call = flag;
1066}
1067
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001068static int __init msm_pm_init(void)
1069{
1070 pgd_t *pc_pgd;
1071 pmd_t *pmd;
1072 unsigned long pmdval;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301073 enum msm_pm_time_stats_id enable_stats[] = {
1074 MSM_PM_STAT_IDLE_WFI,
1075 MSM_PM_STAT_RETENTION,
1076 MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE,
1077 MSM_PM_STAT_IDLE_POWER_COLLAPSE,
1078 MSM_PM_STAT_SUSPEND,
1079 };
Steve Mucklef132c6c2012-06-06 18:30:57 -07001080 unsigned long exit_phys;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301081
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001082 /* Page table for cores to come back up safely. */
1083 pc_pgd = pgd_alloc(&init_mm);
1084 if (!pc_pgd)
1085 return -ENOMEM;
1086
Steve Mucklef132c6c2012-06-06 18:30:57 -07001087 exit_phys = virt_to_phys(msm_pm_collapse_exit);
1088
1089 pmd = pmd_offset(pud_offset(pc_pgd + pgd_index(exit_phys),exit_phys),
1090 exit_phys);
1091 pmdval = (exit_phys & PGDIR_MASK) |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001092 PMD_TYPE_SECT | PMD_SECT_AP_WRITE;
1093 pmd[0] = __pmd(pmdval);
1094 pmd[1] = __pmd(pmdval + (1 << (PGDIR_SHIFT - 1)));
1095
Steve Mucklefcece052012-02-18 20:09:58 -08001096 msm_saved_state_phys =
1097 allocate_contiguous_ebi_nomap(CPU_SAVED_STATE_SIZE *
1098 num_possible_cpus(), 4);
1099 if (!msm_saved_state_phys)
1100 return -ENOMEM;
1101 msm_saved_state = ioremap_nocache(msm_saved_state_phys,
1102 CPU_SAVED_STATE_SIZE *
1103 num_possible_cpus());
1104 if (!msm_saved_state)
1105 return -ENOMEM;
1106
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001107 /* It is remotely possible that the code in msm_pm_collapse_exit()
1108 * which turns on the MMU with this mapping is in the
1109 * next even-numbered megabyte beyond the
1110 * start of msm_pm_collapse_exit().
1111 * Map this megabyte in as well.
1112 */
1113 pmd[2] = __pmd(pmdval + (2 << (PGDIR_SHIFT - 1)));
1114 flush_pmd_entry(pmd);
1115 msm_pm_pc_pgd = virt_to_phys(pc_pgd);
Steve Muckle730ad7a2012-02-21 15:26:37 -08001116 clean_caches((unsigned long)&msm_pm_pc_pgd, sizeof(msm_pm_pc_pgd),
1117 virt_to_phys(&msm_pm_pc_pgd));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001118
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001119 msm_pm_mode_sysfs_add();
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301120 msm_pm_add_stats(enable_stats, ARRAY_SIZE(enable_stats));
1121
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001122 suspend_set_ops(&msm_pm_ops);
Praveen Chidambaram79f3b4a2012-09-18 19:48:29 -06001123 msm_pm_target_init();
Girish Mahadevan12aaabe2012-08-17 16:48:05 -06001124 hrtimer_init(&pm_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001125 msm_cpuidle_init();
1126
1127 return 0;
1128}
1129
1130late_initcall(msm_pm_init);