blob: 60ee8f0921b8ea0dce954327b61c749464169d37 [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 Sivasubramaniancb396622012-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 Mahadevand27ca4a2012-08-15 09:21:23 -060031#include <mach/scm.h>
32#include <mach/socinfo.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"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070048#include "scm-boot.h"
49#include "spm.h"
50#include "timer.h"
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -060051#include "pm-boot.h"
Girish Mahadevandc318fd2012-08-17 16:48:05 -060052#include <mach/event_timer.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070053
54/******************************************************************************
55 * Debug Definitions
56 *****************************************************************************/
57
58enum {
59 MSM_PM_DEBUG_SUSPEND = BIT(0),
60 MSM_PM_DEBUG_POWER_COLLAPSE = BIT(1),
61 MSM_PM_DEBUG_SUSPEND_LIMITS = BIT(2),
62 MSM_PM_DEBUG_CLOCK = BIT(3),
63 MSM_PM_DEBUG_RESET_VECTOR = BIT(4),
Karthik Parsha6fb932d2012-01-24 18:04:12 -080064 MSM_PM_DEBUG_IDLE_CLK = BIT(5),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070065 MSM_PM_DEBUG_IDLE = BIT(6),
66 MSM_PM_DEBUG_IDLE_LIMITS = BIT(7),
67 MSM_PM_DEBUG_HOTPLUG = BIT(8),
68};
69
70static int msm_pm_debug_mask = 1;
71module_param_named(
72 debug_mask, msm_pm_debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP
73);
Girish Mahadevand27ca4a2012-08-15 09:21:23 -060074static int msm_pm_retention_tz_call;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070075
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070076/******************************************************************************
77 * Sleep Modes and Parameters
78 *****************************************************************************/
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070079enum {
80 MSM_PM_MODE_ATTR_SUSPEND,
81 MSM_PM_MODE_ATTR_IDLE,
82 MSM_PM_MODE_ATTR_NR,
83};
84
Girish Mahadevand27ca4a2012-08-15 09:21:23 -060085#define SCM_L2_RETENTION (0x2)
86#define SCM_CMD_TERMINATE_PC (0x2)
87
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070088static char *msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_NR] = {
89 [MSM_PM_MODE_ATTR_SUSPEND] = "suspend_enabled",
90 [MSM_PM_MODE_ATTR_IDLE] = "idle_enabled",
91};
92
93struct msm_pm_kobj_attribute {
94 unsigned int cpu;
95 struct kobj_attribute ka;
96};
97
98#define GET_CPU_OF_ATTR(attr) \
99 (container_of(attr, struct msm_pm_kobj_attribute, ka)->cpu)
100
101struct msm_pm_sysfs_sleep_mode {
102 struct kobject *kobj;
103 struct attribute_group attr_group;
104 struct attribute *attrs[MSM_PM_MODE_ATTR_NR + 1];
105 struct msm_pm_kobj_attribute kas[MSM_PM_MODE_ATTR_NR];
106};
107
108static char *msm_pm_sleep_mode_labels[MSM_PM_SLEEP_MODE_NR] = {
109 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE] = "power_collapse",
110 [MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT] = "wfi",
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600111 [MSM_PM_SLEEP_MODE_RETENTION] = "retention",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700112 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE] =
113 "standalone_power_collapse",
114};
115
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600116static struct hrtimer pm_hrtimer;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600117static struct msm_pm_sleep_ops pm_sleep_ops;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700118/*
119 * Write out the attribute.
120 */
121static ssize_t msm_pm_mode_attr_show(
122 struct kobject *kobj, struct kobj_attribute *attr, char *buf)
123{
124 int ret = -EINVAL;
125 int i;
126
127 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
128 struct kernel_param kp;
129 unsigned int cpu;
130 struct msm_pm_platform_data *mode;
131
132 if (msm_pm_sleep_mode_labels[i] == NULL)
133 continue;
134
135 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
136 continue;
137
138 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600139 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700140
141 if (!strcmp(attr->attr.name,
142 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
143 u32 arg = mode->suspend_enabled;
144 kp.arg = &arg;
145 ret = param_get_ulong(buf, &kp);
146 } else if (!strcmp(attr->attr.name,
147 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
148 u32 arg = mode->idle_enabled;
149 kp.arg = &arg;
150 ret = param_get_ulong(buf, &kp);
151 }
152
153 break;
154 }
155
156 if (ret > 0) {
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600157 strlcat(buf, "\n", PAGE_SIZE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700158 ret++;
159 }
160
161 return ret;
162}
163
164/*
165 * Read in the new attribute value.
166 */
167static ssize_t msm_pm_mode_attr_store(struct kobject *kobj,
168 struct kobj_attribute *attr, const char *buf, size_t count)
169{
170 int ret = -EINVAL;
171 int i;
172
173 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
174 struct kernel_param kp;
175 unsigned int cpu;
176 struct msm_pm_platform_data *mode;
177
178 if (msm_pm_sleep_mode_labels[i] == NULL)
179 continue;
180
181 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
182 continue;
183
184 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600185 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700186
187 if (!strcmp(attr->attr.name,
188 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
189 kp.arg = &mode->suspend_enabled;
190 ret = param_set_byte(buf, &kp);
191 } else if (!strcmp(attr->attr.name,
192 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
193 kp.arg = &mode->idle_enabled;
194 ret = param_set_byte(buf, &kp);
195 }
196
197 break;
198 }
199
200 return ret ? ret : count;
201}
202
203/*
204 * Add sysfs entries for one cpu.
205 */
206static int __init msm_pm_mode_sysfs_add_cpu(
207 unsigned int cpu, struct kobject *modes_kobj)
208{
209 char cpu_name[8];
210 struct kobject *cpu_kobj;
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600211 struct msm_pm_sysfs_sleep_mode *mode = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700212 int i, j, k;
213 int ret;
214
215 snprintf(cpu_name, sizeof(cpu_name), "cpu%u", cpu);
216 cpu_kobj = kobject_create_and_add(cpu_name, modes_kobj);
217 if (!cpu_kobj) {
218 pr_err("%s: cannot create %s kobject\n", __func__, cpu_name);
219 ret = -ENOMEM;
220 goto mode_sysfs_add_cpu_exit;
221 }
222
223 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
224 int idx = MSM_PM_MODE(cpu, i);
225
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600226 if ((!msm_pm_sleep_modes[idx].suspend_supported)
227 && (!msm_pm_sleep_modes[idx].idle_supported))
228 continue;
229
230 if (!msm_pm_sleep_mode_labels[i] ||
231 !msm_pm_sleep_mode_labels[i][0])
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700232 continue;
233
234 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
235 if (!mode) {
236 pr_err("%s: cannot allocate memory for attributes\n",
237 __func__);
238 ret = -ENOMEM;
239 goto mode_sysfs_add_cpu_exit;
240 }
241
242 mode->kobj = kobject_create_and_add(
243 msm_pm_sleep_mode_labels[i], cpu_kobj);
244 if (!mode->kobj) {
245 pr_err("%s: cannot create kobject\n", __func__);
246 ret = -ENOMEM;
247 goto mode_sysfs_add_cpu_exit;
248 }
249
250 for (k = 0, j = 0; k < MSM_PM_MODE_ATTR_NR; k++) {
251 if ((k == MSM_PM_MODE_ATTR_IDLE) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600252 !msm_pm_sleep_modes[idx].idle_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700253 continue;
254 if ((k == MSM_PM_MODE_ATTR_SUSPEND) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600255 !msm_pm_sleep_modes[idx].suspend_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700256 continue;
Stephen Boydd2059c32012-07-03 15:11:15 -0700257 sysfs_attr_init(&mode->kas[j].ka.attr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700258 mode->kas[j].cpu = cpu;
259 mode->kas[j].ka.attr.mode = 0644;
260 mode->kas[j].ka.show = msm_pm_mode_attr_show;
261 mode->kas[j].ka.store = msm_pm_mode_attr_store;
262 mode->kas[j].ka.attr.name = msm_pm_mode_attr_labels[k];
263 mode->attrs[j] = &mode->kas[j].ka.attr;
264 j++;
265 }
266 mode->attrs[j] = NULL;
267
268 mode->attr_group.attrs = mode->attrs;
269 ret = sysfs_create_group(mode->kobj, &mode->attr_group);
270 if (ret) {
271 pr_err("%s: cannot create kobject attribute group\n",
272 __func__);
273 goto mode_sysfs_add_cpu_exit;
274 }
275 }
276
277 ret = 0;
278
279mode_sysfs_add_cpu_exit:
Praveen Chidambaramd5ac2d32011-10-24 14:30:27 -0600280 if (ret) {
Praveen Chidambaram2cfda632011-10-11 16:58:09 -0600281 if (mode && mode->kobj)
282 kobject_del(mode->kobj);
283 kfree(mode);
284 }
285
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700286 return ret;
287}
288
289/*
290 * Add sysfs entries for the sleep modes.
291 */
292static int __init msm_pm_mode_sysfs_add(void)
293{
294 struct kobject *module_kobj;
295 struct kobject *modes_kobj;
296 unsigned int cpu;
297 int ret;
298
299 module_kobj = kset_find_obj(module_kset, KBUILD_MODNAME);
300 if (!module_kobj) {
301 pr_err("%s: cannot find kobject for module %s\n",
302 __func__, KBUILD_MODNAME);
303 ret = -ENOENT;
304 goto mode_sysfs_add_exit;
305 }
306
307 modes_kobj = kobject_create_and_add("modes", module_kobj);
308 if (!modes_kobj) {
309 pr_err("%s: cannot create modes kobject\n", __func__);
310 ret = -ENOMEM;
311 goto mode_sysfs_add_exit;
312 }
313
314 for_each_possible_cpu(cpu) {
315 ret = msm_pm_mode_sysfs_add_cpu(cpu, modes_kobj);
316 if (ret)
317 goto mode_sysfs_add_exit;
318 }
319
320 ret = 0;
321
322mode_sysfs_add_exit:
323 return ret;
324}
325
326/******************************************************************************
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700327 * Configure Hardware before/after Low Power Mode
328 *****************************************************************************/
329
330/*
331 * Configure hardware registers in preparation for Apps power down.
332 */
333static void msm_pm_config_hw_before_power_down(void)
334{
335 return;
336}
337
338/*
339 * Clear hardware registers after Apps powers up.
340 */
341static void msm_pm_config_hw_after_power_up(void)
342{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700343}
344
345/*
346 * Configure hardware registers in preparation for SWFI.
347 */
348static void msm_pm_config_hw_before_swfi(void)
349{
350 return;
351}
352
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600353/*
354 * Configure/Restore hardware registers in preparation for Retention.
355 */
356
357static void msm_pm_config_hw_after_retention(void)
358{
359 int ret;
360 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
361 WARN_ON(ret);
362}
363
364static void msm_pm_config_hw_before_retention(void)
365{
366 return;
367}
368
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700369
370/******************************************************************************
371 * Suspend Max Sleep Time
372 *****************************************************************************/
373
374#ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE
375static int msm_pm_sleep_time_override;
376module_param_named(sleep_time_override,
377 msm_pm_sleep_time_override, int, S_IRUGO | S_IWUSR | S_IWGRP);
378#endif
379
380#define SCLK_HZ (32768)
381#define MSM_PM_SLEEP_TICK_LIMIT (0x6DDD000)
382
383static uint32_t msm_pm_max_sleep_time;
384
385/*
386 * Convert time from nanoseconds to slow clock ticks, then cap it to the
387 * specified limit
388 */
389static int64_t msm_pm_convert_and_cap_time(int64_t time_ns, int64_t limit)
390{
391 do_div(time_ns, NSEC_PER_SEC / SCLK_HZ);
392 return (time_ns > limit) ? limit : time_ns;
393}
394
395/*
396 * Set the sleep time for suspend. 0 means infinite sleep time.
397 */
398void msm_pm_set_max_sleep_time(int64_t max_sleep_time_ns)
399{
400 if (max_sleep_time_ns == 0) {
401 msm_pm_max_sleep_time = 0;
402 } else {
403 msm_pm_max_sleep_time = (uint32_t)msm_pm_convert_and_cap_time(
404 max_sleep_time_ns, MSM_PM_SLEEP_TICK_LIMIT);
405
406 if (msm_pm_max_sleep_time == 0)
407 msm_pm_max_sleep_time = 1;
408 }
409
410 if (msm_pm_debug_mask & MSM_PM_DEBUG_SUSPEND)
411 pr_info("%s: Requested %lld ns Giving %u sclk ticks\n",
412 __func__, max_sleep_time_ns, msm_pm_max_sleep_time);
413}
414EXPORT_SYMBOL(msm_pm_set_max_sleep_time);
415
416
417/******************************************************************************
418 *
419 *****************************************************************************/
420
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600421static void *msm_pm_idle_rs_limits;
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600422static bool msm_pm_use_qtimer;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700423
424static void msm_pm_swfi(void)
425{
426 msm_pm_config_hw_before_swfi();
427 msm_arch_idle();
428}
429
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600430
431static void msm_pm_retention(void)
432{
433 int ret = 0;
434
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600435 msm_pm_config_hw_before_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600436 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_POWER_RETENTION, false);
437 WARN_ON(ret);
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600438
439 if (msm_pm_retention_tz_call)
440 scm_call_atomic1(SCM_SVC_BOOT, SCM_CMD_TERMINATE_PC,
441 SCM_L2_RETENTION);
442 else
443 msm_arch_idle();
444
445 msm_pm_config_hw_after_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600446}
447
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600448#ifdef CONFIG_CACHE_L2X0
449static inline bool msm_pm_l2x0_power_collapse(void)
450{
451 bool collapsed = 0;
452
Taniya Das38a8c6e2012-05-09 20:34:39 +0530453 l2cc_suspend();
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600454 collapsed = msm_pm_collapse();
Taniya Das38a8c6e2012-05-09 20:34:39 +0530455 l2cc_resume();
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600456
457 return collapsed;
458}
459#else
460static inline bool msm_pm_l2x0_power_collapse(void)
461{
462 return msm_pm_collapse();
463}
464#endif
465
Stephen Boydb29750d2012-02-21 01:21:32 -0800466static bool __ref msm_pm_spm_power_collapse(
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700467 unsigned int cpu, bool from_idle, bool notify_rpm)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700468{
469 void *entry;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600470 bool collapsed = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700471 int ret;
Rohit Vaswanie78dfb62012-02-21 10:29:29 -0800472 unsigned int saved_gic_cpu_ctrl;
473
474 saved_gic_cpu_ctrl = readl_relaxed(MSM_QGIC_CPU_BASE + GIC_CPU_CTRL);
475 mb();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700476
477 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
478 pr_info("CPU%u: %s: notify_rpm %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700479 cpu, __func__, (int) notify_rpm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700480
481 ret = msm_spm_set_low_power_mode(
482 MSM_SPM_MODE_POWER_COLLAPSE, notify_rpm);
483 WARN_ON(ret);
484
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700485 entry = (!cpu || from_idle) ?
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700486 msm_pm_collapse_exit : msm_secondary_startup;
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700487 msm_pm_boot_config_before_pc(cpu, virt_to_phys(entry));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700488
489 if (MSM_PM_DEBUG_RESET_VECTOR & msm_pm_debug_mask)
490 pr_info("CPU%u: %s: program vector to %p\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700491 cpu, __func__, entry);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700492
493#ifdef CONFIG_VFP
Steve Mucklef132c6c2012-06-06 18:30:57 -0700494 vfp_pm_suspend();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700495#endif
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600496 collapsed = msm_pm_l2x0_power_collapse();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700497
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700498 msm_pm_boot_config_after_pc(cpu);
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -0600499
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700500 if (collapsed) {
501#ifdef CONFIG_VFP
Steve Mucklef132c6c2012-06-06 18:30:57 -0700502 vfp_pm_resume();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700503#endif
504 cpu_init();
505 writel(0xF0, MSM_QGIC_CPU_BASE + GIC_CPU_PRIMASK);
Rohit Vaswanie78dfb62012-02-21 10:29:29 -0800506 writel_relaxed(saved_gic_cpu_ctrl,
507 MSM_QGIC_CPU_BASE + GIC_CPU_CTRL);
508 mb();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700509 local_fiq_enable();
510 }
511
512 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
513 pr_info("CPU%u: %s: msm_pm_collapse returned, collapsed %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700514 cpu, __func__, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700515
516 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
517 WARN_ON(ret);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600518 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700519}
520
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600521static bool msm_pm_power_collapse_standalone(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700522{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700523 unsigned int cpu = smp_processor_id();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700524 unsigned int avsdscr_setting;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600525 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700526
527 avsdscr_setting = avs_get_avsdscr();
528 avs_disable();
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700529 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700530 avs_reset_delays(avsdscr_setting);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600531 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700532}
533
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600534static bool msm_pm_power_collapse(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700535{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700536 unsigned int cpu = smp_processor_id();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700537 unsigned long saved_acpuclk_rate;
538 unsigned int avsdscr_setting;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600539 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700540
541 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
542 pr_info("CPU%u: %s: idle %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700543 cpu, __func__, (int)from_idle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700544
545 msm_pm_config_hw_before_power_down();
546 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700547 pr_info("CPU%u: %s: pre power down\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700548
549 avsdscr_setting = avs_get_avsdscr();
550 avs_disable();
551
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700552 if (cpu_online(cpu))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700553 saved_acpuclk_rate = acpuclk_power_collapse();
554 else
555 saved_acpuclk_rate = 0;
556
557 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
558 pr_info("CPU%u: %s: change clock rate (old rate = %lu)\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700559 cpu, __func__, saved_acpuclk_rate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700560
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700561 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700562
Girish Mahadevan4e025d92012-02-29 13:26:51 -0700563 if (cpu_online(cpu)) {
564 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
565 pr_info("CPU%u: %s: restore clock rate to %lu\n",
566 cpu, __func__, saved_acpuclk_rate);
567 if (acpuclk_set_rate(cpu, saved_acpuclk_rate, SETRATE_PC) < 0)
568 pr_err("CPU%u: %s: failed to restore clock rate(%lu)\n",
569 cpu, __func__, saved_acpuclk_rate);
570 } else {
571 unsigned int gic_dist_enabled;
572 unsigned int gic_dist_pending;
573 gic_dist_enabled = readl_relaxed(
574 MSM_QGIC_DIST_BASE + GIC_DIST_ENABLE_CLEAR);
575 gic_dist_pending = readl_relaxed(
576 MSM_QGIC_DIST_BASE + GIC_DIST_PENDING_SET);
577 mb();
578 gic_dist_pending &= gic_dist_enabled;
579
580 if (gic_dist_pending)
581 pr_err("CPU %d interrupted during hotplug.Pending int 0x%x\n",
582 cpu, gic_dist_pending);
583 }
584
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700585
586 avs_reset_delays(avsdscr_setting);
587 msm_pm_config_hw_after_power_up();
588 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700589 pr_info("CPU%u: %s: post power up\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700590
591 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700592 pr_info("CPU%u: %s: return\n", cpu, __func__);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600593 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700594}
595
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600596static void msm_pm_qtimer_available(void)
597{
Abhimanyu Kapur90ced6e2012-06-26 17:41:25 -0700598 if (machine_is_msm8974())
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600599 msm_pm_use_qtimer = true;
600}
601
602static int64_t msm_pm_timer_enter_idle(void)
603{
604 if (msm_pm_use_qtimer)
605 return ktime_to_ns(tick_nohz_get_sleep_length());
606
607 return msm_timer_enter_idle();
608}
609
610static void msm_pm_timer_exit_idle(bool timer_halted)
611{
612 if (msm_pm_use_qtimer)
613 return;
614
615 msm_timer_exit_idle((int) timer_halted);
616}
617
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530618static int64_t msm_pm_timer_enter_suspend(int64_t *period)
619{
Anji Jonnalac02367a2012-07-01 02:56:11 +0530620 int64_t time = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530621
622 if (msm_pm_use_qtimer)
623 return sched_clock();
624
625 time = msm_timer_get_sclk_time(period);
626 if (!time)
627 pr_err("%s: Unable to read sclk.\n", __func__);
628
629 return time;
630}
631
632static int64_t msm_pm_timer_exit_suspend(int64_t time, int64_t period)
633{
634 if (msm_pm_use_qtimer)
635 return sched_clock() - time;
636
637 if (time != 0) {
638 int64_t end_time = msm_timer_get_sclk_time(NULL);
639 if (end_time != 0) {
640 time = end_time - time;
641 if (time < 0)
642 time += period;
643 } else
644 time = 0;
645 }
646
647 return time;
648}
649
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600650/**
651 * pm_hrtimer_cb() : Callback function for hrtimer created if the
652 * core needs to be awake to handle an event.
653 * @hrtimer : Pointer to hrtimer
654 */
655static enum hrtimer_restart pm_hrtimer_cb(struct hrtimer *hrtimer)
656{
657 return HRTIMER_NORESTART;
658}
659
660/**
661 * msm_pm_set_timer() : Set an hrtimer to wakeup the core in time
662 * to handle an event.
663 */
664static void msm_pm_set_timer(uint32_t modified_time_us)
665{
666 u64 modified_time_ns = modified_time_us * NSEC_PER_USEC;
667 ktime_t modified_ktime = ns_to_ktime(modified_time_ns);
668 pm_hrtimer.function = pm_hrtimer_cb;
669 hrtimer_start(&pm_hrtimer, modified_ktime, HRTIMER_MODE_ABS);
670}
671
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700672/******************************************************************************
673 * External Idle/Suspend Functions
674 *****************************************************************************/
675
676void arch_idle(void)
677{
678 return;
679}
680
Steve Mucklef132c6c2012-06-06 18:30:57 -0700681int msm_pm_idle_prepare(struct cpuidle_device *dev,
682 struct cpuidle_driver *drv, int index)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700683{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700684 int i;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700685 unsigned int power_usage = -1;
686 int ret = 0;
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600687 uint32_t modified_time_us = 0;
688 struct msm_pm_time_params time_param;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700689
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600690 time_param.latency_us =
691 (uint32_t) pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
692 time_param.sleep_us =
693 (uint32_t) (ktime_to_us(tick_nohz_get_sleep_length())
694 & UINT_MAX);
695 time_param.modified_time_us = 0;
696
697 if (!dev->cpu)
698 time_param.next_event_us =
699 (uint32_t) (ktime_to_us(get_next_event_time())
700 & UINT_MAX);
701 else
702 time_param.next_event_us = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700703
704 for (i = 0; i < dev->state_count; i++) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700705 struct cpuidle_state *state = &drv->states[i];
706 struct cpuidle_state_usage *st_usage = &dev->states_usage[i];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700707 enum msm_pm_sleep_mode mode;
708 bool allow;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600709 void *rs_limits = NULL;
710 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700711 int idx;
712
Steve Mucklef132c6c2012-06-06 18:30:57 -0700713 mode = (enum msm_pm_sleep_mode) cpuidle_get_statedata(st_usage);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700714 idx = MSM_PM_MODE(dev->cpu, mode);
715
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600716 allow = msm_pm_sleep_modes[idx].idle_enabled &&
717 msm_pm_sleep_modes[idx].idle_supported;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700718
719 switch (mode) {
720 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
Mahesh Sivasubramanianba7e01c2012-09-04 18:22:51 -0600721 case MSM_PM_SLEEP_MODE_RETENTION:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700722 if (!allow)
723 break;
724
725 if (num_online_cpus() > 1) {
726 allow = false;
727 break;
728 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700729 /* fall through */
730
731 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
732 if (!allow)
733 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700734 /* fall through */
735
736 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
737 if (!allow)
738 break;
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600739 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700740
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600741 if (pm_sleep_ops.lowest_limits)
742 rs_limits = pm_sleep_ops.lowest_limits(true,
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600743 mode, &time_param, &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700744
745 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
746 pr_info("CPU%u: %s: %s, latency %uus, "
747 "sleep %uus, limit %p\n",
748 dev->cpu, __func__, state->desc,
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600749 time_param.latency_us,
750 time_param.sleep_us, rs_limits);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700751
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700752 if (!rs_limits)
753 allow = false;
754 break;
755
756 default:
757 allow = false;
758 break;
759 }
760
761 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
762 pr_info("CPU%u: %s: allow %s: %d\n",
763 dev->cpu, __func__, state->desc, (int)allow);
764
765 if (allow) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700766 if (power < power_usage) {
767 power_usage = power;
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600768 modified_time_us = time_param.modified_time_us;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700769 ret = mode;
770 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700771
772 if (MSM_PM_SLEEP_MODE_POWER_COLLAPSE == mode)
773 msm_pm_idle_rs_limits = rs_limits;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700774 }
775 }
776
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600777 if (modified_time_us && !dev->cpu)
778 msm_pm_set_timer(modified_time_us);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700779 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700780}
781
782int msm_pm_idle_enter(enum msm_pm_sleep_mode sleep_mode)
783{
784 int64_t time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700785 int exit_stat;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700786
787 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
788 pr_info("CPU%u: %s: mode %d\n",
789 smp_processor_id(), __func__, sleep_mode);
790
791 time = ktime_to_ns(ktime_get());
792
793 switch (sleep_mode) {
794 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
795 msm_pm_swfi();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700796 exit_stat = MSM_PM_STAT_IDLE_WFI;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700797 break;
798
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600799 case MSM_PM_SLEEP_MODE_RETENTION:
800 msm_pm_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600801 exit_stat = MSM_PM_STAT_RETENTION;
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600802 break;
803
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700804 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
805 msm_pm_power_collapse_standalone(true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700806 exit_stat = MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700807 break;
808
809 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE: {
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600810 int64_t timer_expiration = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700811 bool timer_halted = false;
812 uint32_t sleep_delay;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600813 int ret = -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700814 int notify_rpm =
815 (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600816 int collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700817
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600818 timer_expiration = msm_pm_timer_enter_idle();
819
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700820 sleep_delay = (uint32_t) msm_pm_convert_and_cap_time(
821 timer_expiration, MSM_PM_SLEEP_TICK_LIMIT);
822 if (sleep_delay == 0) /* 0 would mean infinite time */
823 sleep_delay = 1;
824
Karthik Parsha6fb932d2012-01-24 18:04:12 -0800825 if (MSM_PM_DEBUG_IDLE_CLK & msm_pm_debug_mask)
826 clock_debug_print_enabled();
827
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600828 if (pm_sleep_ops.enter_sleep)
829 ret = pm_sleep_ops.enter_sleep(sleep_delay,
830 msm_pm_idle_rs_limits,
831 true, notify_rpm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700832 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600833 collapsed = msm_pm_power_collapse(true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700834 timer_halted = true;
835
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600836 if (pm_sleep_ops.exit_sleep)
837 pm_sleep_ops.exit_sleep(msm_pm_idle_rs_limits,
838 true, notify_rpm, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700839 }
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600840 msm_pm_timer_exit_idle(timer_halted);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700841 exit_stat = MSM_PM_STAT_IDLE_POWER_COLLAPSE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700842 break;
843 }
844
845 default:
846 __WARN();
847 goto cpuidle_enter_bail;
848 }
849
850 time = ktime_to_ns(ktime_get()) - time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700851 msm_pm_add_stat(exit_stat, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700852
853 do_div(time, 1000);
854 return (int) time;
855
856cpuidle_enter_bail:
857 return 0;
858}
859
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700860void msm_pm_cpu_enter_lowpower(unsigned int cpu)
861{
862 int i;
863 bool allow[MSM_PM_SLEEP_MODE_NR];
864
865 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
866 struct msm_pm_platform_data *mode;
867
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600868 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700869 allow[i] = mode->suspend_supported && mode->suspend_enabled;
870 }
871
872 if (MSM_PM_DEBUG_HOTPLUG & msm_pm_debug_mask)
873 pr_notice("CPU%u: %s: shutting down cpu\n", cpu, __func__);
874
Matt Wagantall5375aa72012-07-02 19:59:51 -0700875 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700876 msm_pm_power_collapse(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -0700877 else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700878 msm_pm_power_collapse_standalone(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -0700879 else if (allow[MSM_PM_SLEEP_MODE_RETENTION])
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600880 msm_pm_retention();
Stephen Boydbda74272012-08-09 14:01:27 -0700881 else
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700882 msm_pm_swfi();
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700883}
884
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700885static int msm_pm_enter(suspend_state_t state)
886{
887 bool allow[MSM_PM_SLEEP_MODE_NR];
888 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700889 int64_t period = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530890 int64_t time = msm_pm_timer_enter_suspend(&period);
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600891 struct msm_pm_time_params time_param;
892
893 time_param.latency_us = -1;
894 time_param.sleep_us = -1;
895 time_param.next_event_us = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700896
897 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
898 pr_info("%s\n", __func__);
899
900 if (smp_processor_id()) {
901 __WARN();
902 goto enter_exit;
903 }
904
905
906 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
907 struct msm_pm_platform_data *mode;
908
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600909 mode = &msm_pm_sleep_modes[MSM_PM_MODE(0, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700910 allow[i] = mode->suspend_supported && mode->suspend_enabled;
911 }
912
913 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600914 void *rs_limits = NULL;
915 int ret = -ENODEV;
916 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700917
918 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
919 pr_info("%s: power collapse\n", __func__);
920
921 clock_debug_print_enabled();
922
923#ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE
924 if (msm_pm_sleep_time_override > 0) {
925 int64_t ns = NSEC_PER_SEC *
926 (int64_t) msm_pm_sleep_time_override;
927 msm_pm_set_max_sleep_time(ns);
928 msm_pm_sleep_time_override = 0;
929 }
930#endif /* CONFIG_MSM_SLEEP_TIME_OVERRIDE */
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600931 if (pm_sleep_ops.lowest_limits)
932 rs_limits = pm_sleep_ops.lowest_limits(false,
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600933 MSM_PM_SLEEP_MODE_POWER_COLLAPSE, &time_param, &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700934
935 if (rs_limits) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600936 if (pm_sleep_ops.enter_sleep)
937 ret = pm_sleep_ops.enter_sleep(
938 msm_pm_max_sleep_time,
939 rs_limits, false, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700940 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600941 int collapsed = msm_pm_power_collapse(false);
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600942 if (pm_sleep_ops.exit_sleep) {
943 pm_sleep_ops.exit_sleep(rs_limits,
944 false, true, collapsed);
945 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700946 }
947 } else {
948 pr_err("%s: cannot find the lowest power limit\n",
949 __func__);
950 }
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530951 time = msm_pm_timer_exit_suspend(time, period);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700952 msm_pm_add_stat(MSM_PM_STAT_SUSPEND, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700953 } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
954 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
955 pr_info("%s: standalone power collapse\n", __func__);
956 msm_pm_power_collapse_standalone(false);
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600957 } else if (allow[MSM_PM_SLEEP_MODE_RETENTION]) {
958 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
959 pr_info("%s: retention\n", __func__);
960 msm_pm_retention();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700961 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
962 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
963 pr_info("%s: swfi\n", __func__);
964 msm_pm_swfi();
965 }
966
967
968enter_exit:
969 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
970 pr_info("%s: return\n", __func__);
971
972 return 0;
973}
974
975static struct platform_suspend_ops msm_pm_ops = {
976 .enter = msm_pm_enter,
977 .valid = suspend_valid_only_mem,
978};
979
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700980/******************************************************************************
981 * Initialization routine
982 *****************************************************************************/
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600983void msm_pm_set_sleep_ops(struct msm_pm_sleep_ops *ops)
984{
985 if (ops)
986 pm_sleep_ops = *ops;
987}
988
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600989void __init msm_pm_set_tz_retention_flag(unsigned int flag)
990{
991 msm_pm_retention_tz_call = flag;
992}
993
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -0600994static int __devinit msm_pc_debug_probe(struct platform_device *pdev)
995{
996 struct resource *res = NULL;
997 int i ;
998
999 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1000 if (!res)
1001 goto fail;
1002
1003 msm_pc_debug_counters_phys = res->start;
1004 WARN_ON(resource_size(res) < SZ_64);
1005 msm_pc_debug_counters = devm_ioremap(&pdev->dev, res->start,
1006 resource_size(res));
1007
1008 if (!msm_pc_debug_counters)
1009 goto fail;
1010
1011 for (i = 0; i < resource_size(res)/4; i++)
1012 __raw_writel(0, msm_pc_debug_counters + i * 4);
1013 return 0;
1014fail:
1015 msm_pc_debug_counters = 0;
1016 msm_pc_debug_counters_phys = 0;
1017 return -EFAULT;
1018}
1019
1020static struct of_device_id msm_pc_debug_table[] = {
1021 {.compatible = "qcom,pc-cntr"},
1022 {},
1023};
1024
1025static struct platform_driver msm_pc_counter_driver = {
1026 .probe = msm_pc_debug_probe,
1027 .driver = {
1028 .name = "pc-cntr",
1029 .owner = THIS_MODULE,
1030 .of_match_table = msm_pc_debug_table,
1031 },
1032};
1033
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001034static int __init msm_pm_init(void)
1035{
1036 pgd_t *pc_pgd;
1037 pmd_t *pmd;
1038 unsigned long pmdval;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301039 enum msm_pm_time_stats_id enable_stats[] = {
1040 MSM_PM_STAT_IDLE_WFI,
1041 MSM_PM_STAT_RETENTION,
1042 MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE,
1043 MSM_PM_STAT_IDLE_POWER_COLLAPSE,
1044 MSM_PM_STAT_SUSPEND,
1045 };
Steve Mucklef132c6c2012-06-06 18:30:57 -07001046 unsigned long exit_phys;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301047
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001048 /* Page table for cores to come back up safely. */
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -06001049
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001050 pc_pgd = pgd_alloc(&init_mm);
1051 if (!pc_pgd)
1052 return -ENOMEM;
1053
Steve Mucklef132c6c2012-06-06 18:30:57 -07001054 exit_phys = virt_to_phys(msm_pm_collapse_exit);
1055
1056 pmd = pmd_offset(pud_offset(pc_pgd + pgd_index(exit_phys),exit_phys),
1057 exit_phys);
1058 pmdval = (exit_phys & PGDIR_MASK) |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001059 PMD_TYPE_SECT | PMD_SECT_AP_WRITE;
1060 pmd[0] = __pmd(pmdval);
1061 pmd[1] = __pmd(pmdval + (1 << (PGDIR_SHIFT - 1)));
1062
Steve Mucklefcece052012-02-18 20:09:58 -08001063 msm_saved_state_phys =
1064 allocate_contiguous_ebi_nomap(CPU_SAVED_STATE_SIZE *
1065 num_possible_cpus(), 4);
1066 if (!msm_saved_state_phys)
1067 return -ENOMEM;
1068 msm_saved_state = ioremap_nocache(msm_saved_state_phys,
1069 CPU_SAVED_STATE_SIZE *
1070 num_possible_cpus());
1071 if (!msm_saved_state)
1072 return -ENOMEM;
1073
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001074 /* It is remotely possible that the code in msm_pm_collapse_exit()
1075 * which turns on the MMU with this mapping is in the
1076 * next even-numbered megabyte beyond the
1077 * start of msm_pm_collapse_exit().
1078 * Map this megabyte in as well.
1079 */
1080 pmd[2] = __pmd(pmdval + (2 << (PGDIR_SHIFT - 1)));
1081 flush_pmd_entry(pmd);
1082 msm_pm_pc_pgd = virt_to_phys(pc_pgd);
Steve Muckle730ad7a2012-02-21 15:26:37 -08001083 clean_caches((unsigned long)&msm_pm_pc_pgd, sizeof(msm_pm_pc_pgd),
1084 virt_to_phys(&msm_pm_pc_pgd));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001085
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001086 msm_pm_mode_sysfs_add();
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301087 msm_pm_add_stats(enable_stats, ARRAY_SIZE(enable_stats));
1088
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001089 suspend_set_ops(&msm_pm_ops);
Praveen Chidambaram192979f2012-04-25 18:30:23 -06001090 msm_pm_qtimer_available();
Girish Mahadevandc318fd2012-08-17 16:48:05 -06001091 hrtimer_init(&pm_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001092 msm_cpuidle_init();
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -06001093 platform_driver_register(&msm_pc_counter_driver);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001094
1095 return 0;
1096}
1097
1098late_initcall(msm_pm_init);