blob: c931ac7c7cd7582f08c7285d90135ca9a642fd09 [file] [log] [blame]
Praveen Chidambaram0ed2feb2013-02-26 21:05:41 -07001/* Copyright (c) 2010-2013, The Linux Foundation. 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>
Mahesh Sivasubramanian24b2cea2012-03-14 14:50:37 -060027#include <linux/platform_device.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070028#include <mach/msm_iomap.h>
Praveen Chidambaram192979f2012-04-25 18:30:23 -060029#include <mach/socinfo.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070030#include <mach/system.h>
Girish Mahadevan63b87262012-08-15 09:21:23 -060031#include <mach/scm.h>
32#include <mach/socinfo.h>
Praveen Chidambaram79f3b4a2012-09-18 19:48:29 -060033#include <mach/msm-krait-l2-accessors.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070034#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"
Jaeseong GIMfc880da2012-08-13 18:47:46 -070049#include "rpm_resources.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070050#include "scm-boot.h"
51#include "spm.h"
52#include "timer.h"
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -060053#include "pm-boot.h"
Girish Mahadevan12aaabe2012-08-17 16:48:05 -060054#include <mach/event_timer.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070055
56/******************************************************************************
57 * Debug Definitions
58 *****************************************************************************/
59
60enum {
61 MSM_PM_DEBUG_SUSPEND = BIT(0),
62 MSM_PM_DEBUG_POWER_COLLAPSE = BIT(1),
63 MSM_PM_DEBUG_SUSPEND_LIMITS = BIT(2),
64 MSM_PM_DEBUG_CLOCK = BIT(3),
65 MSM_PM_DEBUG_RESET_VECTOR = BIT(4),
Karthik Parsha6fb932d2012-01-24 18:04:12 -080066 MSM_PM_DEBUG_IDLE_CLK = BIT(5),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070067 MSM_PM_DEBUG_IDLE = BIT(6),
68 MSM_PM_DEBUG_IDLE_LIMITS = BIT(7),
69 MSM_PM_DEBUG_HOTPLUG = BIT(8),
70};
71
72static int msm_pm_debug_mask = 1;
73module_param_named(
74 debug_mask, msm_pm_debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP
75);
Girish Mahadevan63b87262012-08-15 09:21:23 -060076static int msm_pm_retention_tz_call;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070077
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070078/******************************************************************************
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
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600551 collapsed = msm_pm_l2x0_power_collapse();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700552
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700553 msm_pm_boot_config_after_pc(cpu);
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -0600554
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700555 if (collapsed) {
556#ifdef CONFIG_VFP
Steve Mucklef132c6c2012-06-06 18:30:57 -0700557 vfp_pm_resume();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700558#endif
559 cpu_init();
560 writel(0xF0, MSM_QGIC_CPU_BASE + GIC_CPU_PRIMASK);
Rohit Vaswanie78dfb62012-02-21 10:29:29 -0800561 writel_relaxed(saved_gic_cpu_ctrl,
562 MSM_QGIC_CPU_BASE + GIC_CPU_CTRL);
563 mb();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700564 local_fiq_enable();
565 }
566
567 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
568 pr_info("CPU%u: %s: msm_pm_collapse returned, collapsed %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700569 cpu, __func__, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700570
571 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
572 WARN_ON(ret);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600573 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700574}
575
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600576static bool msm_pm_power_collapse_standalone(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700577{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700578 unsigned int cpu = smp_processor_id();
Praveen Chidambaram2a618992012-11-08 17:53:34 -0700579 unsigned int avsdscr;
580 unsigned int avscsr;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600581 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700582
Praveen Chidambaram2a618992012-11-08 17:53:34 -0700583 avsdscr = avs_get_avsdscr();
584 avscsr = avs_get_avscsr();
585 avs_set_avscsr(0); /* Disable AVS */
586
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700587 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, false);
Praveen Chidambaram2a618992012-11-08 17:53:34 -0700588
589 avs_set_avsdscr(avsdscr);
590 avs_set_avscsr(avscsr);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600591 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700592}
593
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600594static bool msm_pm_power_collapse(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700595{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700596 unsigned int cpu = smp_processor_id();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700597 unsigned long saved_acpuclk_rate;
Praveen Chidambaram2a618992012-11-08 17:53:34 -0700598 unsigned int avsdscr;
599 unsigned int avscsr;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600600 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700601
602 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
603 pr_info("CPU%u: %s: idle %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700604 cpu, __func__, (int)from_idle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700605
606 msm_pm_config_hw_before_power_down();
607 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700608 pr_info("CPU%u: %s: pre power down\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700609
Praveen Chidambaram2a618992012-11-08 17:53:34 -0700610 avsdscr = avs_get_avsdscr();
611 avscsr = avs_get_avscsr();
612 avs_set_avscsr(0); /* Disable AVS */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700613
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700614 if (cpu_online(cpu))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700615 saved_acpuclk_rate = acpuclk_power_collapse();
616 else
617 saved_acpuclk_rate = 0;
618
619 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
620 pr_info("CPU%u: %s: change clock rate (old rate = %lu)\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700621 cpu, __func__, saved_acpuclk_rate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700622
Praveen Chidambaram79f3b4a2012-09-18 19:48:29 -0600623 if (msm_pm_save_cp15)
624 msm_pm_save_cpu_reg();
625
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700626 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700627
Praveen Chidambaram79f3b4a2012-09-18 19:48:29 -0600628 if (msm_pm_save_cp15)
629 msm_pm_restore_cpu_reg();
630
Girish Mahadevan4e025d92012-02-29 13:26:51 -0700631 if (cpu_online(cpu)) {
632 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
633 pr_info("CPU%u: %s: restore clock rate to %lu\n",
634 cpu, __func__, saved_acpuclk_rate);
635 if (acpuclk_set_rate(cpu, saved_acpuclk_rate, SETRATE_PC) < 0)
636 pr_err("CPU%u: %s: failed to restore clock rate(%lu)\n",
637 cpu, __func__, saved_acpuclk_rate);
638 } else {
639 unsigned int gic_dist_enabled;
640 unsigned int gic_dist_pending;
641 gic_dist_enabled = readl_relaxed(
642 MSM_QGIC_DIST_BASE + GIC_DIST_ENABLE_CLEAR);
643 gic_dist_pending = readl_relaxed(
644 MSM_QGIC_DIST_BASE + GIC_DIST_PENDING_SET);
645 mb();
646 gic_dist_pending &= gic_dist_enabled;
647
648 if (gic_dist_pending)
649 pr_err("CPU %d interrupted during hotplug.Pending int 0x%x\n",
650 cpu, gic_dist_pending);
651 }
652
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700653
Praveen Chidambaram2a618992012-11-08 17:53:34 -0700654 avs_set_avsdscr(avsdscr);
655 avs_set_avscsr(avscsr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700656 msm_pm_config_hw_after_power_up();
657 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700658 pr_info("CPU%u: %s: post power up\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700659
660 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700661 pr_info("CPU%u: %s: return\n", cpu, __func__);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600662 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700663}
664
Praveen Chidambaram79f3b4a2012-09-18 19:48:29 -0600665static void msm_pm_target_init(void)
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600666{
Praveen Chidambaram79f3b4a2012-09-18 19:48:29 -0600667 if (cpu_is_apq8064())
668 msm_pm_save_cp15 = true;
669
670 if (cpu_is_msm8974())
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600671 msm_pm_use_qtimer = true;
672}
673
674static int64_t msm_pm_timer_enter_idle(void)
675{
676 if (msm_pm_use_qtimer)
677 return ktime_to_ns(tick_nohz_get_sleep_length());
678
679 return msm_timer_enter_idle();
680}
681
682static void msm_pm_timer_exit_idle(bool timer_halted)
683{
684 if (msm_pm_use_qtimer)
685 return;
686
687 msm_timer_exit_idle((int) timer_halted);
688}
689
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530690static int64_t msm_pm_timer_enter_suspend(int64_t *period)
691{
Anji Jonnalac02367a2012-07-01 02:56:11 +0530692 int64_t time = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530693
694 if (msm_pm_use_qtimer)
695 return sched_clock();
696
697 time = msm_timer_get_sclk_time(period);
698 if (!time)
699 pr_err("%s: Unable to read sclk.\n", __func__);
700
701 return time;
702}
703
704static int64_t msm_pm_timer_exit_suspend(int64_t time, int64_t period)
705{
706 if (msm_pm_use_qtimer)
707 return sched_clock() - time;
708
709 if (time != 0) {
710 int64_t end_time = msm_timer_get_sclk_time(NULL);
711 if (end_time != 0) {
712 time = end_time - time;
713 if (time < 0)
714 time += period;
715 } else
716 time = 0;
717 }
718
719 return time;
720}
721
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600722/**
723 * pm_hrtimer_cb() : Callback function for hrtimer created if the
724 * core needs to be awake to handle an event.
725 * @hrtimer : Pointer to hrtimer
726 */
727static enum hrtimer_restart pm_hrtimer_cb(struct hrtimer *hrtimer)
728{
729 return HRTIMER_NORESTART;
730}
731
732/**
733 * msm_pm_set_timer() : Set an hrtimer to wakeup the core in time
734 * to handle an event.
735 */
736static void msm_pm_set_timer(uint32_t modified_time_us)
737{
738 u64 modified_time_ns = modified_time_us * NSEC_PER_USEC;
739 ktime_t modified_ktime = ns_to_ktime(modified_time_ns);
740 pm_hrtimer.function = pm_hrtimer_cb;
741 hrtimer_start(&pm_hrtimer, modified_ktime, HRTIMER_MODE_ABS);
742}
743
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700744/******************************************************************************
745 * External Idle/Suspend Functions
746 *****************************************************************************/
747
748void arch_idle(void)
749{
750 return;
751}
752
Steve Mucklef132c6c2012-06-06 18:30:57 -0700753int msm_pm_idle_prepare(struct cpuidle_device *dev,
754 struct cpuidle_driver *drv, int index)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700755{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700756 int i;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700757 unsigned int power_usage = -1;
758 int ret = 0;
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600759 uint32_t modified_time_us = 0;
760 struct msm_pm_time_params time_param;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700761
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600762 time_param.latency_us =
763 (uint32_t) pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
764 time_param.sleep_us =
765 (uint32_t) (ktime_to_us(tick_nohz_get_sleep_length())
766 & UINT_MAX);
767 time_param.modified_time_us = 0;
768
769 if (!dev->cpu)
770 time_param.next_event_us =
771 (uint32_t) (ktime_to_us(get_next_event_time())
772 & UINT_MAX);
773 else
774 time_param.next_event_us = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700775
776 for (i = 0; i < dev->state_count; i++) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700777 struct cpuidle_state *state = &drv->states[i];
778 struct cpuidle_state_usage *st_usage = &dev->states_usage[i];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700779 enum msm_pm_sleep_mode mode;
780 bool allow;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600781 void *rs_limits = NULL;
782 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700783 int idx;
784
Steve Mucklef132c6c2012-06-06 18:30:57 -0700785 mode = (enum msm_pm_sleep_mode) cpuidle_get_statedata(st_usage);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700786 idx = MSM_PM_MODE(dev->cpu, mode);
787
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600788 allow = msm_pm_sleep_modes[idx].idle_enabled &&
789 msm_pm_sleep_modes[idx].idle_supported;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700790
791 switch (mode) {
792 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
Priyanka Mathur5effc7c2012-10-03 15:15:01 -0700793 if (num_online_cpus() > 1) {
794 allow = false;
795 break;
796 }
797 /* fall through */
Mahesh Sivasubramanian96c0aa42012-09-04 18:22:51 -0600798 case MSM_PM_SLEEP_MODE_RETENTION:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700799 if (!allow)
800 break;
801
Priyanka Mathur5effc7c2012-10-03 15:15:01 -0700802 if (msm_pm_retention_tz_call &&
803 num_online_cpus() > 1) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700804 allow = false;
805 break;
806 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700807 /* fall through */
808
809 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
810 if (!allow)
811 break;
Jaeseong GIMfc880da2012-08-13 18:47:46 -0700812
813 if (!dev->cpu && msm_rpm_local_request_is_outstanding()) {
814 allow = false;
815 break;
816 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700817 /* fall through */
818
819 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
820 if (!allow)
821 break;
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600822 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700823
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600824 if (pm_sleep_ops.lowest_limits)
825 rs_limits = pm_sleep_ops.lowest_limits(true,
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600826 mode, &time_param, &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700827
828 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
829 pr_info("CPU%u: %s: %s, latency %uus, "
830 "sleep %uus, limit %p\n",
831 dev->cpu, __func__, state->desc,
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600832 time_param.latency_us,
833 time_param.sleep_us, rs_limits);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700834
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700835 if (!rs_limits)
836 allow = false;
837 break;
838
839 default:
840 allow = false;
841 break;
842 }
843
844 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
845 pr_info("CPU%u: %s: allow %s: %d\n",
846 dev->cpu, __func__, state->desc, (int)allow);
847
848 if (allow) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700849 if (power < power_usage) {
850 power_usage = power;
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600851 modified_time_us = time_param.modified_time_us;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700852 ret = mode;
853 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700854
855 if (MSM_PM_SLEEP_MODE_POWER_COLLAPSE == mode)
856 msm_pm_idle_rs_limits = rs_limits;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700857 }
858 }
859
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600860 if (modified_time_us && !dev->cpu)
861 msm_pm_set_timer(modified_time_us);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700862 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700863}
864
865int msm_pm_idle_enter(enum msm_pm_sleep_mode sleep_mode)
866{
867 int64_t time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700868 int exit_stat;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700869
870 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
871 pr_info("CPU%u: %s: mode %d\n",
872 smp_processor_id(), __func__, sleep_mode);
873
874 time = ktime_to_ns(ktime_get());
875
876 switch (sleep_mode) {
877 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
878 msm_pm_swfi();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700879 exit_stat = MSM_PM_STAT_IDLE_WFI;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700880 break;
881
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600882 case MSM_PM_SLEEP_MODE_RETENTION:
883 msm_pm_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600884 exit_stat = MSM_PM_STAT_RETENTION;
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600885 break;
886
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700887 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
888 msm_pm_power_collapse_standalone(true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700889 exit_stat = MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700890 break;
891
892 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE: {
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600893 int64_t timer_expiration = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700894 bool timer_halted = false;
895 uint32_t sleep_delay;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600896 int ret = -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700897 int notify_rpm =
898 (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600899 int collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700900
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600901 timer_expiration = msm_pm_timer_enter_idle();
902
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700903 sleep_delay = (uint32_t) msm_pm_convert_and_cap_time(
904 timer_expiration, MSM_PM_SLEEP_TICK_LIMIT);
905 if (sleep_delay == 0) /* 0 would mean infinite time */
906 sleep_delay = 1;
907
Karthik Parsha6fb932d2012-01-24 18:04:12 -0800908 if (MSM_PM_DEBUG_IDLE_CLK & msm_pm_debug_mask)
909 clock_debug_print_enabled();
910
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600911 if (pm_sleep_ops.enter_sleep)
912 ret = pm_sleep_ops.enter_sleep(sleep_delay,
913 msm_pm_idle_rs_limits,
914 true, notify_rpm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700915 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600916 collapsed = msm_pm_power_collapse(true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700917 timer_halted = true;
918
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600919 if (pm_sleep_ops.exit_sleep)
920 pm_sleep_ops.exit_sleep(msm_pm_idle_rs_limits,
921 true, notify_rpm, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700922 }
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600923 msm_pm_timer_exit_idle(timer_halted);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700924 exit_stat = MSM_PM_STAT_IDLE_POWER_COLLAPSE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700925 break;
926 }
927
928 default:
929 __WARN();
930 goto cpuidle_enter_bail;
931 }
932
933 time = ktime_to_ns(ktime_get()) - time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700934 msm_pm_add_stat(exit_stat, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700935
936 do_div(time, 1000);
937 return (int) time;
938
939cpuidle_enter_bail:
940 return 0;
941}
942
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700943void msm_pm_cpu_enter_lowpower(unsigned int cpu)
944{
945 int i;
946 bool allow[MSM_PM_SLEEP_MODE_NR];
947
948 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
949 struct msm_pm_platform_data *mode;
950
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600951 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700952 allow[i] = mode->suspend_supported && mode->suspend_enabled;
953 }
954
955 if (MSM_PM_DEBUG_HOTPLUG & msm_pm_debug_mask)
956 pr_notice("CPU%u: %s: shutting down cpu\n", cpu, __func__);
957
Matt Wagantall5375aa72012-07-02 19:59:51 -0700958 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700959 msm_pm_power_collapse(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -0700960 else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700961 msm_pm_power_collapse_standalone(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -0700962 else if (allow[MSM_PM_SLEEP_MODE_RETENTION])
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600963 msm_pm_retention();
Stephen Boydbda74272012-08-09 14:01:27 -0700964 else
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700965 msm_pm_swfi();
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700966}
967
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700968static int msm_pm_enter(suspend_state_t state)
969{
970 bool allow[MSM_PM_SLEEP_MODE_NR];
971 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700972 int64_t period = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530973 int64_t time = msm_pm_timer_enter_suspend(&period);
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600974 struct msm_pm_time_params time_param;
975
976 time_param.latency_us = -1;
977 time_param.sleep_us = -1;
978 time_param.next_event_us = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700979
980 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
981 pr_info("%s\n", __func__);
982
983 if (smp_processor_id()) {
984 __WARN();
985 goto enter_exit;
986 }
987
988
989 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
990 struct msm_pm_platform_data *mode;
991
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600992 mode = &msm_pm_sleep_modes[MSM_PM_MODE(0, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700993 allow[i] = mode->suspend_supported && mode->suspend_enabled;
994 }
995
996 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600997 void *rs_limits = NULL;
998 int ret = -ENODEV;
999 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001000
1001 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1002 pr_info("%s: power collapse\n", __func__);
1003
1004 clock_debug_print_enabled();
1005
1006#ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE
1007 if (msm_pm_sleep_time_override > 0) {
1008 int64_t ns = NSEC_PER_SEC *
1009 (int64_t) msm_pm_sleep_time_override;
1010 msm_pm_set_max_sleep_time(ns);
1011 msm_pm_sleep_time_override = 0;
1012 }
1013#endif /* CONFIG_MSM_SLEEP_TIME_OVERRIDE */
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001014 if (pm_sleep_ops.lowest_limits)
1015 rs_limits = pm_sleep_ops.lowest_limits(false,
Girish Mahadevan12aaabe2012-08-17 16:48:05 -06001016 MSM_PM_SLEEP_MODE_POWER_COLLAPSE, &time_param, &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001017
1018 if (rs_limits) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001019 if (pm_sleep_ops.enter_sleep)
1020 ret = pm_sleep_ops.enter_sleep(
1021 msm_pm_max_sleep_time,
1022 rs_limits, false, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001023 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -06001024 int collapsed = msm_pm_power_collapse(false);
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001025 if (pm_sleep_ops.exit_sleep) {
1026 pm_sleep_ops.exit_sleep(rs_limits,
1027 false, true, collapsed);
1028 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001029 }
1030 } else {
1031 pr_err("%s: cannot find the lowest power limit\n",
1032 __func__);
1033 }
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301034 time = msm_pm_timer_exit_suspend(time, period);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001035 msm_pm_add_stat(MSM_PM_STAT_SUSPEND, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001036 } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
1037 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1038 pr_info("%s: standalone power collapse\n", __func__);
1039 msm_pm_power_collapse_standalone(false);
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -06001040 } else if (allow[MSM_PM_SLEEP_MODE_RETENTION]) {
1041 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1042 pr_info("%s: retention\n", __func__);
1043 msm_pm_retention();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001044 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
1045 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1046 pr_info("%s: swfi\n", __func__);
1047 msm_pm_swfi();
1048 }
1049
1050
1051enter_exit:
1052 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1053 pr_info("%s: return\n", __func__);
1054
1055 return 0;
1056}
1057
1058static struct platform_suspend_ops msm_pm_ops = {
1059 .enter = msm_pm_enter,
1060 .valid = suspend_valid_only_mem,
1061};
1062
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001063/******************************************************************************
1064 * Initialization routine
1065 *****************************************************************************/
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001066void msm_pm_set_sleep_ops(struct msm_pm_sleep_ops *ops)
1067{
1068 if (ops)
1069 pm_sleep_ops = *ops;
1070}
1071
Girish Mahadevan63b87262012-08-15 09:21:23 -06001072void __init msm_pm_set_tz_retention_flag(unsigned int flag)
1073{
1074 msm_pm_retention_tz_call = flag;
1075}
1076
Mahesh Sivasubramanian24b2cea2012-03-14 14:50:37 -06001077static int __devinit msm_pc_debug_probe(struct platform_device *pdev)
1078{
1079 struct resource *res = NULL;
1080 int i ;
1081
1082 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1083 if (!res)
1084 goto fail;
1085
1086 msm_pc_debug_counters_phys = res->start;
1087 WARN_ON(resource_size(res) < SZ_64);
Mahesh Sivasubramanian7683d0a2012-10-17 13:39:40 -06001088 msm_pc_debug_counters = devm_ioremap_nocache(&pdev->dev, res->start,
Mahesh Sivasubramanian24b2cea2012-03-14 14:50:37 -06001089 resource_size(res));
1090
1091 if (!msm_pc_debug_counters)
1092 goto fail;
1093
1094 for (i = 0; i < resource_size(res)/4; i++)
1095 __raw_writel(0, msm_pc_debug_counters + i * 4);
1096 return 0;
1097fail:
1098 msm_pc_debug_counters = 0;
1099 msm_pc_debug_counters_phys = 0;
1100 return -EFAULT;
1101}
1102
1103static struct of_device_id msm_pc_debug_table[] = {
1104 {.compatible = "qcom,pc-cntr"},
1105 {},
1106};
1107
1108static struct platform_driver msm_pc_counter_driver = {
1109 .probe = msm_pc_debug_probe,
1110 .driver = {
1111 .name = "pc-cntr",
1112 .owner = THIS_MODULE,
1113 .of_match_table = msm_pc_debug_table,
1114 },
1115};
1116
Praveen Chidambaram0ed2feb2013-02-26 21:05:41 -07001117static int __init msm_pm_setup_saved_state(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001118{
1119 pgd_t *pc_pgd;
1120 pmd_t *pmd;
1121 unsigned long pmdval;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001122 unsigned long exit_phys;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301123
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001124 /* Page table for cores to come back up safely. */
Mahesh Sivasubramanian24b2cea2012-03-14 14:50:37 -06001125
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001126 pc_pgd = pgd_alloc(&init_mm);
1127 if (!pc_pgd)
1128 return -ENOMEM;
1129
Steve Mucklef132c6c2012-06-06 18:30:57 -07001130 exit_phys = virt_to_phys(msm_pm_collapse_exit);
1131
1132 pmd = pmd_offset(pud_offset(pc_pgd + pgd_index(exit_phys),exit_phys),
1133 exit_phys);
1134 pmdval = (exit_phys & PGDIR_MASK) |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001135 PMD_TYPE_SECT | PMD_SECT_AP_WRITE;
1136 pmd[0] = __pmd(pmdval);
1137 pmd[1] = __pmd(pmdval + (1 << (PGDIR_SHIFT - 1)));
1138
Steve Mucklefcece052012-02-18 20:09:58 -08001139 msm_saved_state_phys =
1140 allocate_contiguous_ebi_nomap(CPU_SAVED_STATE_SIZE *
1141 num_possible_cpus(), 4);
1142 if (!msm_saved_state_phys)
1143 return -ENOMEM;
1144 msm_saved_state = ioremap_nocache(msm_saved_state_phys,
1145 CPU_SAVED_STATE_SIZE *
1146 num_possible_cpus());
1147 if (!msm_saved_state)
1148 return -ENOMEM;
1149
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001150 /* It is remotely possible that the code in msm_pm_collapse_exit()
1151 * which turns on the MMU with this mapping is in the
1152 * next even-numbered megabyte beyond the
1153 * start of msm_pm_collapse_exit().
1154 * Map this megabyte in as well.
1155 */
1156 pmd[2] = __pmd(pmdval + (2 << (PGDIR_SHIFT - 1)));
1157 flush_pmd_entry(pmd);
1158 msm_pm_pc_pgd = virt_to_phys(pc_pgd);
Steve Muckle730ad7a2012-02-21 15:26:37 -08001159 clean_caches((unsigned long)&msm_pm_pc_pgd, sizeof(msm_pm_pc_pgd),
1160 virt_to_phys(&msm_pm_pc_pgd));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001161
Praveen Chidambaram0ed2feb2013-02-26 21:05:41 -07001162 return 0;
1163}
1164core_initcall(msm_pm_setup_saved_state);
1165
1166static int __init msm_pm_init(void)
1167{
1168 enum msm_pm_time_stats_id enable_stats[] = {
1169 MSM_PM_STAT_IDLE_WFI,
1170 MSM_PM_STAT_RETENTION,
1171 MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE,
1172 MSM_PM_STAT_IDLE_POWER_COLLAPSE,
1173 MSM_PM_STAT_SUSPEND,
1174 };
1175
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001176 msm_pm_mode_sysfs_add();
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301177 msm_pm_add_stats(enable_stats, ARRAY_SIZE(enable_stats));
1178
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001179 suspend_set_ops(&msm_pm_ops);
Praveen Chidambaram79f3b4a2012-09-18 19:48:29 -06001180 msm_pm_target_init();
Girish Mahadevan12aaabe2012-08-17 16:48:05 -06001181 hrtimer_init(&pm_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001182 msm_cpuidle_init();
Mahesh Sivasubramanian24b2cea2012-03-14 14:50:37 -06001183 platform_driver_register(&msm_pc_counter_driver);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001184
1185 return 0;
1186}
1187
1188late_initcall(msm_pm_init);