blob: 5d2baa9c1ba6bc09e670fafae5585f69fadae59e [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>
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();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700579 unsigned int avsdscr_setting;
Mahesh Sivasubramanianc1145072012-10-24 13:53:48 -0600580 unsigned int avscsr_enable;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600581 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700582
583 avsdscr_setting = avs_get_avsdscr();
Mahesh Sivasubramanianc1145072012-10-24 13:53:48 -0600584 avscsr_enable = avs_disable();
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700585 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, false);
Mahesh Sivasubramanianc1145072012-10-24 13:53:48 -0600586 avs_enable(avscsr_enable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700587 avs_reset_delays(avsdscr_setting);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600588 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700589}
590
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600591static bool msm_pm_power_collapse(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700592{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700593 unsigned int cpu = smp_processor_id();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700594 unsigned long saved_acpuclk_rate;
595 unsigned int avsdscr_setting;
Mahesh Sivasubramanianc1145072012-10-24 13:53:48 -0600596 unsigned int avscsr_enable;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600597 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700598
599 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
600 pr_info("CPU%u: %s: idle %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700601 cpu, __func__, (int)from_idle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700602
603 msm_pm_config_hw_before_power_down();
604 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700605 pr_info("CPU%u: %s: pre power down\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700606
607 avsdscr_setting = avs_get_avsdscr();
Mahesh Sivasubramanianc1145072012-10-24 13:53:48 -0600608 avscsr_enable = avs_disable();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700609
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700610 if (cpu_online(cpu))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700611 saved_acpuclk_rate = acpuclk_power_collapse();
612 else
613 saved_acpuclk_rate = 0;
614
615 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
616 pr_info("CPU%u: %s: change clock rate (old rate = %lu)\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700617 cpu, __func__, saved_acpuclk_rate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700618
Praveen Chidambaram79f3b4a2012-09-18 19:48:29 -0600619 if (msm_pm_save_cp15)
620 msm_pm_save_cpu_reg();
621
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700622 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700623
Praveen Chidambaram79f3b4a2012-09-18 19:48:29 -0600624 if (msm_pm_save_cp15)
625 msm_pm_restore_cpu_reg();
626
Girish Mahadevan4e025d92012-02-29 13:26:51 -0700627 if (cpu_online(cpu)) {
628 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
629 pr_info("CPU%u: %s: restore clock rate to %lu\n",
630 cpu, __func__, saved_acpuclk_rate);
631 if (acpuclk_set_rate(cpu, saved_acpuclk_rate, SETRATE_PC) < 0)
632 pr_err("CPU%u: %s: failed to restore clock rate(%lu)\n",
633 cpu, __func__, saved_acpuclk_rate);
634 } else {
635 unsigned int gic_dist_enabled;
636 unsigned int gic_dist_pending;
637 gic_dist_enabled = readl_relaxed(
638 MSM_QGIC_DIST_BASE + GIC_DIST_ENABLE_CLEAR);
639 gic_dist_pending = readl_relaxed(
640 MSM_QGIC_DIST_BASE + GIC_DIST_PENDING_SET);
641 mb();
642 gic_dist_pending &= gic_dist_enabled;
643
644 if (gic_dist_pending)
645 pr_err("CPU %d interrupted during hotplug.Pending int 0x%x\n",
646 cpu, gic_dist_pending);
647 }
648
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700649
Mahesh Sivasubramanianc1145072012-10-24 13:53:48 -0600650 avs_enable(avscsr_enable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700651 avs_reset_delays(avsdscr_setting);
652 msm_pm_config_hw_after_power_up();
653 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700654 pr_info("CPU%u: %s: post power up\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700655
656 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700657 pr_info("CPU%u: %s: return\n", cpu, __func__);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600658 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700659}
660
Praveen Chidambaram79f3b4a2012-09-18 19:48:29 -0600661static void msm_pm_target_init(void)
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600662{
Praveen Chidambaram79f3b4a2012-09-18 19:48:29 -0600663 if (cpu_is_apq8064())
664 msm_pm_save_cp15 = true;
665
666 if (cpu_is_msm8974())
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600667 msm_pm_use_qtimer = true;
668}
669
670static int64_t msm_pm_timer_enter_idle(void)
671{
672 if (msm_pm_use_qtimer)
673 return ktime_to_ns(tick_nohz_get_sleep_length());
674
675 return msm_timer_enter_idle();
676}
677
678static void msm_pm_timer_exit_idle(bool timer_halted)
679{
680 if (msm_pm_use_qtimer)
681 return;
682
683 msm_timer_exit_idle((int) timer_halted);
684}
685
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530686static int64_t msm_pm_timer_enter_suspend(int64_t *period)
687{
Anji Jonnalac02367a2012-07-01 02:56:11 +0530688 int64_t time = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530689
690 if (msm_pm_use_qtimer)
691 return sched_clock();
692
693 time = msm_timer_get_sclk_time(period);
694 if (!time)
695 pr_err("%s: Unable to read sclk.\n", __func__);
696
697 return time;
698}
699
700static int64_t msm_pm_timer_exit_suspend(int64_t time, int64_t period)
701{
702 if (msm_pm_use_qtimer)
703 return sched_clock() - time;
704
705 if (time != 0) {
706 int64_t end_time = msm_timer_get_sclk_time(NULL);
707 if (end_time != 0) {
708 time = end_time - time;
709 if (time < 0)
710 time += period;
711 } else
712 time = 0;
713 }
714
715 return time;
716}
717
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600718/**
719 * pm_hrtimer_cb() : Callback function for hrtimer created if the
720 * core needs to be awake to handle an event.
721 * @hrtimer : Pointer to hrtimer
722 */
723static enum hrtimer_restart pm_hrtimer_cb(struct hrtimer *hrtimer)
724{
725 return HRTIMER_NORESTART;
726}
727
728/**
729 * msm_pm_set_timer() : Set an hrtimer to wakeup the core in time
730 * to handle an event.
731 */
732static void msm_pm_set_timer(uint32_t modified_time_us)
733{
734 u64 modified_time_ns = modified_time_us * NSEC_PER_USEC;
735 ktime_t modified_ktime = ns_to_ktime(modified_time_ns);
736 pm_hrtimer.function = pm_hrtimer_cb;
737 hrtimer_start(&pm_hrtimer, modified_ktime, HRTIMER_MODE_ABS);
738}
739
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700740/******************************************************************************
741 * External Idle/Suspend Functions
742 *****************************************************************************/
743
744void arch_idle(void)
745{
746 return;
747}
748
Steve Mucklef132c6c2012-06-06 18:30:57 -0700749int msm_pm_idle_prepare(struct cpuidle_device *dev,
750 struct cpuidle_driver *drv, int index)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700751{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700752 int i;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700753 unsigned int power_usage = -1;
754 int ret = 0;
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600755 uint32_t modified_time_us = 0;
756 struct msm_pm_time_params time_param;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700757
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600758 time_param.latency_us =
759 (uint32_t) pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
760 time_param.sleep_us =
761 (uint32_t) (ktime_to_us(tick_nohz_get_sleep_length())
762 & UINT_MAX);
763 time_param.modified_time_us = 0;
764
765 if (!dev->cpu)
766 time_param.next_event_us =
767 (uint32_t) (ktime_to_us(get_next_event_time())
768 & UINT_MAX);
769 else
770 time_param.next_event_us = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700771
772 for (i = 0; i < dev->state_count; i++) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700773 struct cpuidle_state *state = &drv->states[i];
774 struct cpuidle_state_usage *st_usage = &dev->states_usage[i];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700775 enum msm_pm_sleep_mode mode;
776 bool allow;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600777 void *rs_limits = NULL;
778 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700779 int idx;
780
Steve Mucklef132c6c2012-06-06 18:30:57 -0700781 mode = (enum msm_pm_sleep_mode) cpuidle_get_statedata(st_usage);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700782 idx = MSM_PM_MODE(dev->cpu, mode);
783
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600784 allow = msm_pm_sleep_modes[idx].idle_enabled &&
785 msm_pm_sleep_modes[idx].idle_supported;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700786
787 switch (mode) {
788 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
Mahesh Sivasubramanian96c0aa42012-09-04 18:22:51 -0600789 case MSM_PM_SLEEP_MODE_RETENTION:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700790 if (!allow)
791 break;
792
793 if (num_online_cpus() > 1) {
794 allow = false;
795 break;
796 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700797 /* fall through */
798
799 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
800 if (!allow)
801 break;
Jaeseong GIMfc880da2012-08-13 18:47:46 -0700802
803 if (!dev->cpu && msm_rpm_local_request_is_outstanding()) {
804 allow = false;
805 break;
806 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700807 /* fall through */
808
809 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
810 if (!allow)
811 break;
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600812 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700813
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600814 if (pm_sleep_ops.lowest_limits)
815 rs_limits = pm_sleep_ops.lowest_limits(true,
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600816 mode, &time_param, &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700817
818 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
819 pr_info("CPU%u: %s: %s, latency %uus, "
820 "sleep %uus, limit %p\n",
821 dev->cpu, __func__, state->desc,
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600822 time_param.latency_us,
823 time_param.sleep_us, rs_limits);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700824
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700825 if (!rs_limits)
826 allow = false;
827 break;
828
829 default:
830 allow = false;
831 break;
832 }
833
834 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
835 pr_info("CPU%u: %s: allow %s: %d\n",
836 dev->cpu, __func__, state->desc, (int)allow);
837
838 if (allow) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700839 if (power < power_usage) {
840 power_usage = power;
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600841 modified_time_us = time_param.modified_time_us;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700842 ret = mode;
843 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700844
845 if (MSM_PM_SLEEP_MODE_POWER_COLLAPSE == mode)
846 msm_pm_idle_rs_limits = rs_limits;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700847 }
848 }
849
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600850 if (modified_time_us && !dev->cpu)
851 msm_pm_set_timer(modified_time_us);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700852 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700853}
854
855int msm_pm_idle_enter(enum msm_pm_sleep_mode sleep_mode)
856{
857 int64_t time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700858 int exit_stat;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700859
860 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
861 pr_info("CPU%u: %s: mode %d\n",
862 smp_processor_id(), __func__, sleep_mode);
863
864 time = ktime_to_ns(ktime_get());
865
866 switch (sleep_mode) {
867 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
868 msm_pm_swfi();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700869 exit_stat = MSM_PM_STAT_IDLE_WFI;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700870 break;
871
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600872 case MSM_PM_SLEEP_MODE_RETENTION:
873 msm_pm_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600874 exit_stat = MSM_PM_STAT_RETENTION;
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600875 break;
876
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700877 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
878 msm_pm_power_collapse_standalone(true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700879 exit_stat = MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700880 break;
881
882 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE: {
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600883 int64_t timer_expiration = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700884 bool timer_halted = false;
885 uint32_t sleep_delay;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600886 int ret = -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700887 int notify_rpm =
888 (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600889 int collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700890
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600891 timer_expiration = msm_pm_timer_enter_idle();
892
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700893 sleep_delay = (uint32_t) msm_pm_convert_and_cap_time(
894 timer_expiration, MSM_PM_SLEEP_TICK_LIMIT);
895 if (sleep_delay == 0) /* 0 would mean infinite time */
896 sleep_delay = 1;
897
Karthik Parsha6fb932d2012-01-24 18:04:12 -0800898 if (MSM_PM_DEBUG_IDLE_CLK & msm_pm_debug_mask)
899 clock_debug_print_enabled();
900
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600901 if (pm_sleep_ops.enter_sleep)
902 ret = pm_sleep_ops.enter_sleep(sleep_delay,
903 msm_pm_idle_rs_limits,
904 true, notify_rpm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700905 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600906 collapsed = msm_pm_power_collapse(true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700907 timer_halted = true;
908
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600909 if (pm_sleep_ops.exit_sleep)
910 pm_sleep_ops.exit_sleep(msm_pm_idle_rs_limits,
911 true, notify_rpm, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700912 }
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600913 msm_pm_timer_exit_idle(timer_halted);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700914 exit_stat = MSM_PM_STAT_IDLE_POWER_COLLAPSE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700915 break;
916 }
917
918 default:
919 __WARN();
920 goto cpuidle_enter_bail;
921 }
922
923 time = ktime_to_ns(ktime_get()) - time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700924 msm_pm_add_stat(exit_stat, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700925
926 do_div(time, 1000);
927 return (int) time;
928
929cpuidle_enter_bail:
930 return 0;
931}
932
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700933void msm_pm_cpu_enter_lowpower(unsigned int cpu)
934{
935 int i;
936 bool allow[MSM_PM_SLEEP_MODE_NR];
937
938 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
939 struct msm_pm_platform_data *mode;
940
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600941 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700942 allow[i] = mode->suspend_supported && mode->suspend_enabled;
943 }
944
945 if (MSM_PM_DEBUG_HOTPLUG & msm_pm_debug_mask)
946 pr_notice("CPU%u: %s: shutting down cpu\n", cpu, __func__);
947
Matt Wagantall5375aa72012-07-02 19:59:51 -0700948 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700949 msm_pm_power_collapse(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -0700950 else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700951 msm_pm_power_collapse_standalone(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -0700952 else if (allow[MSM_PM_SLEEP_MODE_RETENTION])
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600953 msm_pm_retention();
Stephen Boydbda74272012-08-09 14:01:27 -0700954 else
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700955 msm_pm_swfi();
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700956}
957
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700958static int msm_pm_enter(suspend_state_t state)
959{
960 bool allow[MSM_PM_SLEEP_MODE_NR];
961 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700962 int64_t period = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530963 int64_t time = msm_pm_timer_enter_suspend(&period);
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600964 struct msm_pm_time_params time_param;
965
966 time_param.latency_us = -1;
967 time_param.sleep_us = -1;
968 time_param.next_event_us = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700969
970 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
971 pr_info("%s\n", __func__);
972
973 if (smp_processor_id()) {
974 __WARN();
975 goto enter_exit;
976 }
977
978
979 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
980 struct msm_pm_platform_data *mode;
981
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600982 mode = &msm_pm_sleep_modes[MSM_PM_MODE(0, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700983 allow[i] = mode->suspend_supported && mode->suspend_enabled;
984 }
985
986 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600987 void *rs_limits = NULL;
988 int ret = -ENODEV;
989 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700990
991 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
992 pr_info("%s: power collapse\n", __func__);
993
994 clock_debug_print_enabled();
995
996#ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE
997 if (msm_pm_sleep_time_override > 0) {
998 int64_t ns = NSEC_PER_SEC *
999 (int64_t) msm_pm_sleep_time_override;
1000 msm_pm_set_max_sleep_time(ns);
1001 msm_pm_sleep_time_override = 0;
1002 }
1003#endif /* CONFIG_MSM_SLEEP_TIME_OVERRIDE */
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001004 if (pm_sleep_ops.lowest_limits)
1005 rs_limits = pm_sleep_ops.lowest_limits(false,
Girish Mahadevan12aaabe2012-08-17 16:48:05 -06001006 MSM_PM_SLEEP_MODE_POWER_COLLAPSE, &time_param, &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001007
1008 if (rs_limits) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001009 if (pm_sleep_ops.enter_sleep)
1010 ret = pm_sleep_ops.enter_sleep(
1011 msm_pm_max_sleep_time,
1012 rs_limits, false, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001013 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -06001014 int collapsed = msm_pm_power_collapse(false);
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001015 if (pm_sleep_ops.exit_sleep) {
1016 pm_sleep_ops.exit_sleep(rs_limits,
1017 false, true, collapsed);
1018 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001019 }
1020 } else {
1021 pr_err("%s: cannot find the lowest power limit\n",
1022 __func__);
1023 }
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301024 time = msm_pm_timer_exit_suspend(time, period);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001025 msm_pm_add_stat(MSM_PM_STAT_SUSPEND, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001026 } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
1027 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1028 pr_info("%s: standalone power collapse\n", __func__);
1029 msm_pm_power_collapse_standalone(false);
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -06001030 } else if (allow[MSM_PM_SLEEP_MODE_RETENTION]) {
1031 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1032 pr_info("%s: retention\n", __func__);
1033 msm_pm_retention();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001034 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
1035 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1036 pr_info("%s: swfi\n", __func__);
1037 msm_pm_swfi();
1038 }
1039
1040
1041enter_exit:
1042 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1043 pr_info("%s: return\n", __func__);
1044
1045 return 0;
1046}
1047
1048static struct platform_suspend_ops msm_pm_ops = {
1049 .enter = msm_pm_enter,
1050 .valid = suspend_valid_only_mem,
1051};
1052
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001053/******************************************************************************
1054 * Initialization routine
1055 *****************************************************************************/
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001056void msm_pm_set_sleep_ops(struct msm_pm_sleep_ops *ops)
1057{
1058 if (ops)
1059 pm_sleep_ops = *ops;
1060}
1061
Girish Mahadevan63b87262012-08-15 09:21:23 -06001062void __init msm_pm_set_tz_retention_flag(unsigned int flag)
1063{
1064 msm_pm_retention_tz_call = flag;
1065}
1066
Mahesh Sivasubramanian24b2cea2012-03-14 14:50:37 -06001067static int __devinit msm_pc_debug_probe(struct platform_device *pdev)
1068{
1069 struct resource *res = NULL;
1070 int i ;
1071
1072 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1073 if (!res)
1074 goto fail;
1075
1076 msm_pc_debug_counters_phys = res->start;
1077 WARN_ON(resource_size(res) < SZ_64);
1078 msm_pc_debug_counters = devm_ioremap(&pdev->dev, res->start,
1079 resource_size(res));
1080
1081 if (!msm_pc_debug_counters)
1082 goto fail;
1083
1084 for (i = 0; i < resource_size(res)/4; i++)
1085 __raw_writel(0, msm_pc_debug_counters + i * 4);
1086 return 0;
1087fail:
1088 msm_pc_debug_counters = 0;
1089 msm_pc_debug_counters_phys = 0;
1090 return -EFAULT;
1091}
1092
1093static struct of_device_id msm_pc_debug_table[] = {
1094 {.compatible = "qcom,pc-cntr"},
1095 {},
1096};
1097
1098static struct platform_driver msm_pc_counter_driver = {
1099 .probe = msm_pc_debug_probe,
1100 .driver = {
1101 .name = "pc-cntr",
1102 .owner = THIS_MODULE,
1103 .of_match_table = msm_pc_debug_table,
1104 },
1105};
1106
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001107static int __init msm_pm_init(void)
1108{
1109 pgd_t *pc_pgd;
1110 pmd_t *pmd;
1111 unsigned long pmdval;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301112 enum msm_pm_time_stats_id enable_stats[] = {
1113 MSM_PM_STAT_IDLE_WFI,
1114 MSM_PM_STAT_RETENTION,
1115 MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE,
1116 MSM_PM_STAT_IDLE_POWER_COLLAPSE,
1117 MSM_PM_STAT_SUSPEND,
1118 };
Steve Mucklef132c6c2012-06-06 18:30:57 -07001119 unsigned long exit_phys;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301120
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001121 /* Page table for cores to come back up safely. */
Mahesh Sivasubramanian24b2cea2012-03-14 14:50:37 -06001122
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001123 pc_pgd = pgd_alloc(&init_mm);
1124 if (!pc_pgd)
1125 return -ENOMEM;
1126
Steve Mucklef132c6c2012-06-06 18:30:57 -07001127 exit_phys = virt_to_phys(msm_pm_collapse_exit);
1128
1129 pmd = pmd_offset(pud_offset(pc_pgd + pgd_index(exit_phys),exit_phys),
1130 exit_phys);
1131 pmdval = (exit_phys & PGDIR_MASK) |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001132 PMD_TYPE_SECT | PMD_SECT_AP_WRITE;
1133 pmd[0] = __pmd(pmdval);
1134 pmd[1] = __pmd(pmdval + (1 << (PGDIR_SHIFT - 1)));
1135
Steve Mucklefcece052012-02-18 20:09:58 -08001136 msm_saved_state_phys =
1137 allocate_contiguous_ebi_nomap(CPU_SAVED_STATE_SIZE *
1138 num_possible_cpus(), 4);
1139 if (!msm_saved_state_phys)
1140 return -ENOMEM;
1141 msm_saved_state = ioremap_nocache(msm_saved_state_phys,
1142 CPU_SAVED_STATE_SIZE *
1143 num_possible_cpus());
1144 if (!msm_saved_state)
1145 return -ENOMEM;
1146
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001147 /* It is remotely possible that the code in msm_pm_collapse_exit()
1148 * which turns on the MMU with this mapping is in the
1149 * next even-numbered megabyte beyond the
1150 * start of msm_pm_collapse_exit().
1151 * Map this megabyte in as well.
1152 */
1153 pmd[2] = __pmd(pmdval + (2 << (PGDIR_SHIFT - 1)));
1154 flush_pmd_entry(pmd);
1155 msm_pm_pc_pgd = virt_to_phys(pc_pgd);
Steve Muckle730ad7a2012-02-21 15:26:37 -08001156 clean_caches((unsigned long)&msm_pm_pc_pgd, sizeof(msm_pm_pc_pgd),
1157 virt_to_phys(&msm_pm_pc_pgd));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001158
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001159 msm_pm_mode_sysfs_add();
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301160 msm_pm_add_stats(enable_stats, ARRAY_SIZE(enable_stats));
1161
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001162 suspend_set_ops(&msm_pm_ops);
Praveen Chidambaram79f3b4a2012-09-18 19:48:29 -06001163 msm_pm_target_init();
Girish Mahadevan12aaabe2012-08-17 16:48:05 -06001164 hrtimer_init(&pm_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001165 msm_cpuidle_init();
Mahesh Sivasubramanian24b2cea2012-03-14 14:50:37 -06001166 platform_driver_register(&msm_pc_counter_driver);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001167
1168 return 0;
1169}
1170
1171late_initcall(msm_pm_init);