blob: dbb23d51c8f9e6954c8eeddbb285080163fecaee [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"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070052
53/******************************************************************************
54 * Debug Definitions
55 *****************************************************************************/
56
57enum {
58 MSM_PM_DEBUG_SUSPEND = BIT(0),
59 MSM_PM_DEBUG_POWER_COLLAPSE = BIT(1),
60 MSM_PM_DEBUG_SUSPEND_LIMITS = BIT(2),
61 MSM_PM_DEBUG_CLOCK = BIT(3),
62 MSM_PM_DEBUG_RESET_VECTOR = BIT(4),
Karthik Parsha6fb932d2012-01-24 18:04:12 -080063 MSM_PM_DEBUG_IDLE_CLK = BIT(5),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070064 MSM_PM_DEBUG_IDLE = BIT(6),
65 MSM_PM_DEBUG_IDLE_LIMITS = BIT(7),
66 MSM_PM_DEBUG_HOTPLUG = BIT(8),
67};
68
69static int msm_pm_debug_mask = 1;
70module_param_named(
71 debug_mask, msm_pm_debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP
72);
Girish Mahadevand27ca4a2012-08-15 09:21:23 -060073static int msm_pm_retention_tz_call;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070074
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070075/******************************************************************************
76 * Sleep Modes and Parameters
77 *****************************************************************************/
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070078enum {
79 MSM_PM_MODE_ATTR_SUSPEND,
80 MSM_PM_MODE_ATTR_IDLE,
81 MSM_PM_MODE_ATTR_NR,
82};
83
Girish Mahadevand27ca4a2012-08-15 09:21:23 -060084#define SCM_L2_RETENTION (0x2)
85#define SCM_CMD_TERMINATE_PC (0x2)
86
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070087static char *msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_NR] = {
88 [MSM_PM_MODE_ATTR_SUSPEND] = "suspend_enabled",
89 [MSM_PM_MODE_ATTR_IDLE] = "idle_enabled",
90};
91
92struct msm_pm_kobj_attribute {
93 unsigned int cpu;
94 struct kobj_attribute ka;
95};
96
97#define GET_CPU_OF_ATTR(attr) \
98 (container_of(attr, struct msm_pm_kobj_attribute, ka)->cpu)
99
100struct msm_pm_sysfs_sleep_mode {
101 struct kobject *kobj;
102 struct attribute_group attr_group;
103 struct attribute *attrs[MSM_PM_MODE_ATTR_NR + 1];
104 struct msm_pm_kobj_attribute kas[MSM_PM_MODE_ATTR_NR];
105};
106
107static char *msm_pm_sleep_mode_labels[MSM_PM_SLEEP_MODE_NR] = {
108 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE] = "power_collapse",
109 [MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT] = "wfi",
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600110 [MSM_PM_SLEEP_MODE_RETENTION] = "retention",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700111 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE] =
112 "standalone_power_collapse",
113};
114
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600115static struct msm_pm_sleep_ops pm_sleep_ops;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700116/*
117 * Write out the attribute.
118 */
119static ssize_t msm_pm_mode_attr_show(
120 struct kobject *kobj, struct kobj_attribute *attr, char *buf)
121{
122 int ret = -EINVAL;
123 int i;
124
125 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
126 struct kernel_param kp;
127 unsigned int cpu;
128 struct msm_pm_platform_data *mode;
129
130 if (msm_pm_sleep_mode_labels[i] == NULL)
131 continue;
132
133 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
134 continue;
135
136 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600137 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700138
139 if (!strcmp(attr->attr.name,
140 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
141 u32 arg = mode->suspend_enabled;
142 kp.arg = &arg;
143 ret = param_get_ulong(buf, &kp);
144 } else if (!strcmp(attr->attr.name,
145 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
146 u32 arg = mode->idle_enabled;
147 kp.arg = &arg;
148 ret = param_get_ulong(buf, &kp);
149 }
150
151 break;
152 }
153
154 if (ret > 0) {
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600155 strlcat(buf, "\n", PAGE_SIZE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700156 ret++;
157 }
158
159 return ret;
160}
161
162/*
163 * Read in the new attribute value.
164 */
165static ssize_t msm_pm_mode_attr_store(struct kobject *kobj,
166 struct kobj_attribute *attr, const char *buf, size_t count)
167{
168 int ret = -EINVAL;
169 int i;
170
171 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
172 struct kernel_param kp;
173 unsigned int cpu;
174 struct msm_pm_platform_data *mode;
175
176 if (msm_pm_sleep_mode_labels[i] == NULL)
177 continue;
178
179 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
180 continue;
181
182 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600183 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700184
185 if (!strcmp(attr->attr.name,
186 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
187 kp.arg = &mode->suspend_enabled;
188 ret = param_set_byte(buf, &kp);
189 } else if (!strcmp(attr->attr.name,
190 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
191 kp.arg = &mode->idle_enabled;
192 ret = param_set_byte(buf, &kp);
193 }
194
195 break;
196 }
197
198 return ret ? ret : count;
199}
200
201/*
202 * Add sysfs entries for one cpu.
203 */
204static int __init msm_pm_mode_sysfs_add_cpu(
205 unsigned int cpu, struct kobject *modes_kobj)
206{
207 char cpu_name[8];
208 struct kobject *cpu_kobj;
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600209 struct msm_pm_sysfs_sleep_mode *mode = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700210 int i, j, k;
211 int ret;
212
213 snprintf(cpu_name, sizeof(cpu_name), "cpu%u", cpu);
214 cpu_kobj = kobject_create_and_add(cpu_name, modes_kobj);
215 if (!cpu_kobj) {
216 pr_err("%s: cannot create %s kobject\n", __func__, cpu_name);
217 ret = -ENOMEM;
218 goto mode_sysfs_add_cpu_exit;
219 }
220
221 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
222 int idx = MSM_PM_MODE(cpu, i);
223
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600224 if ((!msm_pm_sleep_modes[idx].suspend_supported)
225 && (!msm_pm_sleep_modes[idx].idle_supported))
226 continue;
227
228 if (!msm_pm_sleep_mode_labels[i] ||
229 !msm_pm_sleep_mode_labels[i][0])
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700230 continue;
231
232 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
233 if (!mode) {
234 pr_err("%s: cannot allocate memory for attributes\n",
235 __func__);
236 ret = -ENOMEM;
237 goto mode_sysfs_add_cpu_exit;
238 }
239
240 mode->kobj = kobject_create_and_add(
241 msm_pm_sleep_mode_labels[i], cpu_kobj);
242 if (!mode->kobj) {
243 pr_err("%s: cannot create kobject\n", __func__);
244 ret = -ENOMEM;
245 goto mode_sysfs_add_cpu_exit;
246 }
247
248 for (k = 0, j = 0; k < MSM_PM_MODE_ATTR_NR; k++) {
249 if ((k == MSM_PM_MODE_ATTR_IDLE) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600250 !msm_pm_sleep_modes[idx].idle_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700251 continue;
252 if ((k == MSM_PM_MODE_ATTR_SUSPEND) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600253 !msm_pm_sleep_modes[idx].suspend_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700254 continue;
Stephen Boydd2059c32012-07-03 15:11:15 -0700255 sysfs_attr_init(&mode->kas[j].ka.attr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700256 mode->kas[j].cpu = cpu;
257 mode->kas[j].ka.attr.mode = 0644;
258 mode->kas[j].ka.show = msm_pm_mode_attr_show;
259 mode->kas[j].ka.store = msm_pm_mode_attr_store;
260 mode->kas[j].ka.attr.name = msm_pm_mode_attr_labels[k];
261 mode->attrs[j] = &mode->kas[j].ka.attr;
262 j++;
263 }
264 mode->attrs[j] = NULL;
265
266 mode->attr_group.attrs = mode->attrs;
267 ret = sysfs_create_group(mode->kobj, &mode->attr_group);
268 if (ret) {
269 pr_err("%s: cannot create kobject attribute group\n",
270 __func__);
271 goto mode_sysfs_add_cpu_exit;
272 }
273 }
274
275 ret = 0;
276
277mode_sysfs_add_cpu_exit:
Praveen Chidambaramd5ac2d32011-10-24 14:30:27 -0600278 if (ret) {
Praveen Chidambaram2cfda632011-10-11 16:58:09 -0600279 if (mode && mode->kobj)
280 kobject_del(mode->kobj);
281 kfree(mode);
282 }
283
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700284 return ret;
285}
286
287/*
288 * Add sysfs entries for the sleep modes.
289 */
290static int __init msm_pm_mode_sysfs_add(void)
291{
292 struct kobject *module_kobj;
293 struct kobject *modes_kobj;
294 unsigned int cpu;
295 int ret;
296
297 module_kobj = kset_find_obj(module_kset, KBUILD_MODNAME);
298 if (!module_kobj) {
299 pr_err("%s: cannot find kobject for module %s\n",
300 __func__, KBUILD_MODNAME);
301 ret = -ENOENT;
302 goto mode_sysfs_add_exit;
303 }
304
305 modes_kobj = kobject_create_and_add("modes", module_kobj);
306 if (!modes_kobj) {
307 pr_err("%s: cannot create modes kobject\n", __func__);
308 ret = -ENOMEM;
309 goto mode_sysfs_add_exit;
310 }
311
312 for_each_possible_cpu(cpu) {
313 ret = msm_pm_mode_sysfs_add_cpu(cpu, modes_kobj);
314 if (ret)
315 goto mode_sysfs_add_exit;
316 }
317
318 ret = 0;
319
320mode_sysfs_add_exit:
321 return ret;
322}
323
324/******************************************************************************
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700325 * Configure Hardware before/after Low Power Mode
326 *****************************************************************************/
327
328/*
329 * Configure hardware registers in preparation for Apps power down.
330 */
331static void msm_pm_config_hw_before_power_down(void)
332{
333 return;
334}
335
336/*
337 * Clear hardware registers after Apps powers up.
338 */
339static void msm_pm_config_hw_after_power_up(void)
340{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700341}
342
343/*
344 * Configure hardware registers in preparation for SWFI.
345 */
346static void msm_pm_config_hw_before_swfi(void)
347{
348 return;
349}
350
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600351/*
352 * Configure/Restore hardware registers in preparation for Retention.
353 */
354
355static void msm_pm_config_hw_after_retention(void)
356{
357 int ret;
358 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
359 WARN_ON(ret);
360}
361
362static void msm_pm_config_hw_before_retention(void)
363{
364 return;
365}
366
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700367
368/******************************************************************************
369 * Suspend Max Sleep Time
370 *****************************************************************************/
371
372#ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE
373static int msm_pm_sleep_time_override;
374module_param_named(sleep_time_override,
375 msm_pm_sleep_time_override, int, S_IRUGO | S_IWUSR | S_IWGRP);
376#endif
377
378#define SCLK_HZ (32768)
379#define MSM_PM_SLEEP_TICK_LIMIT (0x6DDD000)
380
381static uint32_t msm_pm_max_sleep_time;
382
383/*
384 * Convert time from nanoseconds to slow clock ticks, then cap it to the
385 * specified limit
386 */
387static int64_t msm_pm_convert_and_cap_time(int64_t time_ns, int64_t limit)
388{
389 do_div(time_ns, NSEC_PER_SEC / SCLK_HZ);
390 return (time_ns > limit) ? limit : time_ns;
391}
392
393/*
394 * Set the sleep time for suspend. 0 means infinite sleep time.
395 */
396void msm_pm_set_max_sleep_time(int64_t max_sleep_time_ns)
397{
398 if (max_sleep_time_ns == 0) {
399 msm_pm_max_sleep_time = 0;
400 } else {
401 msm_pm_max_sleep_time = (uint32_t)msm_pm_convert_and_cap_time(
402 max_sleep_time_ns, MSM_PM_SLEEP_TICK_LIMIT);
403
404 if (msm_pm_max_sleep_time == 0)
405 msm_pm_max_sleep_time = 1;
406 }
407
408 if (msm_pm_debug_mask & MSM_PM_DEBUG_SUSPEND)
409 pr_info("%s: Requested %lld ns Giving %u sclk ticks\n",
410 __func__, max_sleep_time_ns, msm_pm_max_sleep_time);
411}
412EXPORT_SYMBOL(msm_pm_set_max_sleep_time);
413
414
415/******************************************************************************
416 *
417 *****************************************************************************/
418
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600419static void *msm_pm_idle_rs_limits;
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600420static bool msm_pm_use_qtimer;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700421
422static void msm_pm_swfi(void)
423{
424 msm_pm_config_hw_before_swfi();
425 msm_arch_idle();
426}
427
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600428
429static void msm_pm_retention(void)
430{
431 int ret = 0;
432
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600433 msm_pm_config_hw_before_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600434 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_POWER_RETENTION, false);
435 WARN_ON(ret);
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600436
437 if (msm_pm_retention_tz_call)
438 scm_call_atomic1(SCM_SVC_BOOT, SCM_CMD_TERMINATE_PC,
439 SCM_L2_RETENTION);
440 else
441 msm_arch_idle();
442
443 msm_pm_config_hw_after_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600444}
445
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600446#ifdef CONFIG_CACHE_L2X0
447static inline bool msm_pm_l2x0_power_collapse(void)
448{
449 bool collapsed = 0;
450
Taniya Das38a8c6e2012-05-09 20:34:39 +0530451 l2cc_suspend();
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600452 collapsed = msm_pm_collapse();
Taniya Das38a8c6e2012-05-09 20:34:39 +0530453 l2cc_resume();
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600454
455 return collapsed;
456}
457#else
458static inline bool msm_pm_l2x0_power_collapse(void)
459{
460 return msm_pm_collapse();
461}
462#endif
463
Stephen Boydb29750d2012-02-21 01:21:32 -0800464static bool __ref msm_pm_spm_power_collapse(
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700465 unsigned int cpu, bool from_idle, bool notify_rpm)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700466{
467 void *entry;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600468 bool collapsed = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700469 int ret;
Rohit Vaswanie78dfb62012-02-21 10:29:29 -0800470 unsigned int saved_gic_cpu_ctrl;
471
472 saved_gic_cpu_ctrl = readl_relaxed(MSM_QGIC_CPU_BASE + GIC_CPU_CTRL);
473 mb();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700474
475 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
476 pr_info("CPU%u: %s: notify_rpm %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700477 cpu, __func__, (int) notify_rpm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700478
479 ret = msm_spm_set_low_power_mode(
480 MSM_SPM_MODE_POWER_COLLAPSE, notify_rpm);
481 WARN_ON(ret);
482
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700483 entry = (!cpu || from_idle) ?
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700484 msm_pm_collapse_exit : msm_secondary_startup;
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700485 msm_pm_boot_config_before_pc(cpu, virt_to_phys(entry));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700486
487 if (MSM_PM_DEBUG_RESET_VECTOR & msm_pm_debug_mask)
488 pr_info("CPU%u: %s: program vector to %p\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700489 cpu, __func__, entry);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700490
491#ifdef CONFIG_VFP
Steve Mucklef132c6c2012-06-06 18:30:57 -0700492 vfp_pm_suspend();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700493#endif
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600494 collapsed = msm_pm_l2x0_power_collapse();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700495
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700496 msm_pm_boot_config_after_pc(cpu);
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -0600497
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700498 if (collapsed) {
499#ifdef CONFIG_VFP
Steve Mucklef132c6c2012-06-06 18:30:57 -0700500 vfp_pm_resume();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700501#endif
502 cpu_init();
503 writel(0xF0, MSM_QGIC_CPU_BASE + GIC_CPU_PRIMASK);
Rohit Vaswanie78dfb62012-02-21 10:29:29 -0800504 writel_relaxed(saved_gic_cpu_ctrl,
505 MSM_QGIC_CPU_BASE + GIC_CPU_CTRL);
506 mb();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700507 local_fiq_enable();
508 }
509
510 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
511 pr_info("CPU%u: %s: msm_pm_collapse returned, collapsed %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700512 cpu, __func__, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700513
514 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
515 WARN_ON(ret);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600516 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700517}
518
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600519static bool msm_pm_power_collapse_standalone(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700520{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700521 unsigned int cpu = smp_processor_id();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700522 unsigned int avsdscr_setting;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600523 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700524
525 avsdscr_setting = avs_get_avsdscr();
526 avs_disable();
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700527 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700528 avs_reset_delays(avsdscr_setting);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600529 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700530}
531
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600532static bool msm_pm_power_collapse(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700533{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700534 unsigned int cpu = smp_processor_id();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700535 unsigned long saved_acpuclk_rate;
536 unsigned int avsdscr_setting;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600537 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700538
539 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
540 pr_info("CPU%u: %s: idle %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700541 cpu, __func__, (int)from_idle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700542
543 msm_pm_config_hw_before_power_down();
544 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700545 pr_info("CPU%u: %s: pre power down\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700546
547 avsdscr_setting = avs_get_avsdscr();
548 avs_disable();
549
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700550 if (cpu_online(cpu))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700551 saved_acpuclk_rate = acpuclk_power_collapse();
552 else
553 saved_acpuclk_rate = 0;
554
555 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
556 pr_info("CPU%u: %s: change clock rate (old rate = %lu)\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700557 cpu, __func__, saved_acpuclk_rate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700558
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700559 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700560
Girish Mahadevan4e025d92012-02-29 13:26:51 -0700561 if (cpu_online(cpu)) {
562 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
563 pr_info("CPU%u: %s: restore clock rate to %lu\n",
564 cpu, __func__, saved_acpuclk_rate);
565 if (acpuclk_set_rate(cpu, saved_acpuclk_rate, SETRATE_PC) < 0)
566 pr_err("CPU%u: %s: failed to restore clock rate(%lu)\n",
567 cpu, __func__, saved_acpuclk_rate);
568 } else {
569 unsigned int gic_dist_enabled;
570 unsigned int gic_dist_pending;
571 gic_dist_enabled = readl_relaxed(
572 MSM_QGIC_DIST_BASE + GIC_DIST_ENABLE_CLEAR);
573 gic_dist_pending = readl_relaxed(
574 MSM_QGIC_DIST_BASE + GIC_DIST_PENDING_SET);
575 mb();
576 gic_dist_pending &= gic_dist_enabled;
577
578 if (gic_dist_pending)
579 pr_err("CPU %d interrupted during hotplug.Pending int 0x%x\n",
580 cpu, gic_dist_pending);
581 }
582
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700583
584 avs_reset_delays(avsdscr_setting);
585 msm_pm_config_hw_after_power_up();
586 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700587 pr_info("CPU%u: %s: post power up\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700588
589 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700590 pr_info("CPU%u: %s: return\n", cpu, __func__);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600591 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700592}
593
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600594static void msm_pm_qtimer_available(void)
595{
Abhimanyu Kapur90ced6e2012-06-26 17:41:25 -0700596 if (machine_is_msm8974())
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600597 msm_pm_use_qtimer = true;
598}
599
600static int64_t msm_pm_timer_enter_idle(void)
601{
602 if (msm_pm_use_qtimer)
603 return ktime_to_ns(tick_nohz_get_sleep_length());
604
605 return msm_timer_enter_idle();
606}
607
608static void msm_pm_timer_exit_idle(bool timer_halted)
609{
610 if (msm_pm_use_qtimer)
611 return;
612
613 msm_timer_exit_idle((int) timer_halted);
614}
615
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530616static int64_t msm_pm_timer_enter_suspend(int64_t *period)
617{
Anji Jonnalac02367a2012-07-01 02:56:11 +0530618 int64_t time = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530619
620 if (msm_pm_use_qtimer)
621 return sched_clock();
622
623 time = msm_timer_get_sclk_time(period);
624 if (!time)
625 pr_err("%s: Unable to read sclk.\n", __func__);
626
627 return time;
628}
629
630static int64_t msm_pm_timer_exit_suspend(int64_t time, int64_t period)
631{
632 if (msm_pm_use_qtimer)
633 return sched_clock() - time;
634
635 if (time != 0) {
636 int64_t end_time = msm_timer_get_sclk_time(NULL);
637 if (end_time != 0) {
638 time = end_time - time;
639 if (time < 0)
640 time += period;
641 } else
642 time = 0;
643 }
644
645 return time;
646}
647
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700648/******************************************************************************
649 * External Idle/Suspend Functions
650 *****************************************************************************/
651
652void arch_idle(void)
653{
654 return;
655}
656
Steve Mucklef132c6c2012-06-06 18:30:57 -0700657int msm_pm_idle_prepare(struct cpuidle_device *dev,
658 struct cpuidle_driver *drv, int index)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700659{
660 uint32_t latency_us;
661 uint32_t sleep_us;
662 int i;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700663 unsigned int power_usage = -1;
664 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700665
666 latency_us = (uint32_t) pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
667 sleep_us = (uint32_t) ktime_to_ns(tick_nohz_get_sleep_length());
668 sleep_us = DIV_ROUND_UP(sleep_us, 1000);
669
670 for (i = 0; i < dev->state_count; i++) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700671 struct cpuidle_state *state = &drv->states[i];
672 struct cpuidle_state_usage *st_usage = &dev->states_usage[i];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700673 enum msm_pm_sleep_mode mode;
674 bool allow;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600675 void *rs_limits = NULL;
676 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700677 int idx;
678
Steve Mucklef132c6c2012-06-06 18:30:57 -0700679 mode = (enum msm_pm_sleep_mode) cpuidle_get_statedata(st_usage);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700680 idx = MSM_PM_MODE(dev->cpu, mode);
681
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600682 allow = msm_pm_sleep_modes[idx].idle_enabled &&
683 msm_pm_sleep_modes[idx].idle_supported;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700684
685 switch (mode) {
686 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
Mahesh Sivasubramanianba7e01c2012-09-04 18:22:51 -0600687 case MSM_PM_SLEEP_MODE_RETENTION:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700688 if (!allow)
689 break;
690
691 if (num_online_cpus() > 1) {
692 allow = false;
693 break;
694 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700695 /* fall through */
696
697 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
698 if (!allow)
699 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700700 /* fall through */
701
702 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
703 if (!allow)
704 break;
705
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600706 if (pm_sleep_ops.lowest_limits)
707 rs_limits = pm_sleep_ops.lowest_limits(true,
708 mode, latency_us, sleep_us,
709 &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700710
711 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
712 pr_info("CPU%u: %s: %s, latency %uus, "
713 "sleep %uus, limit %p\n",
714 dev->cpu, __func__, state->desc,
715 latency_us, sleep_us, rs_limits);
716
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700717 if (!rs_limits)
718 allow = false;
719 break;
720
721 default:
722 allow = false;
723 break;
724 }
725
726 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
727 pr_info("CPU%u: %s: allow %s: %d\n",
728 dev->cpu, __func__, state->desc, (int)allow);
729
730 if (allow) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700731 if (power < power_usage) {
732 power_usage = power;
733 ret = mode;
734 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700735
736 if (MSM_PM_SLEEP_MODE_POWER_COLLAPSE == mode)
737 msm_pm_idle_rs_limits = rs_limits;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700738 }
739 }
740
Steve Mucklef132c6c2012-06-06 18:30:57 -0700741 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700742}
743
744int msm_pm_idle_enter(enum msm_pm_sleep_mode sleep_mode)
745{
746 int64_t time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700747 int exit_stat;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700748
749 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
750 pr_info("CPU%u: %s: mode %d\n",
751 smp_processor_id(), __func__, sleep_mode);
752
753 time = ktime_to_ns(ktime_get());
754
755 switch (sleep_mode) {
756 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
757 msm_pm_swfi();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700758 exit_stat = MSM_PM_STAT_IDLE_WFI;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700759 break;
760
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600761 case MSM_PM_SLEEP_MODE_RETENTION:
762 msm_pm_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600763 exit_stat = MSM_PM_STAT_RETENTION;
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600764 break;
765
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700766 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
767 msm_pm_power_collapse_standalone(true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700768 exit_stat = MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700769 break;
770
771 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE: {
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600772 int64_t timer_expiration = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700773 bool timer_halted = false;
774 uint32_t sleep_delay;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600775 int ret = -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700776 int notify_rpm =
777 (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600778 int collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700779
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600780 timer_expiration = msm_pm_timer_enter_idle();
781
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700782 sleep_delay = (uint32_t) msm_pm_convert_and_cap_time(
783 timer_expiration, MSM_PM_SLEEP_TICK_LIMIT);
784 if (sleep_delay == 0) /* 0 would mean infinite time */
785 sleep_delay = 1;
786
Karthik Parsha6fb932d2012-01-24 18:04:12 -0800787 if (MSM_PM_DEBUG_IDLE_CLK & msm_pm_debug_mask)
788 clock_debug_print_enabled();
789
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600790 if (pm_sleep_ops.enter_sleep)
791 ret = pm_sleep_ops.enter_sleep(sleep_delay,
792 msm_pm_idle_rs_limits,
793 true, notify_rpm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700794 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600795 collapsed = msm_pm_power_collapse(true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700796 timer_halted = true;
797
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600798 if (pm_sleep_ops.exit_sleep)
799 pm_sleep_ops.exit_sleep(msm_pm_idle_rs_limits,
800 true, notify_rpm, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700801 }
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600802 msm_pm_timer_exit_idle(timer_halted);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700803 exit_stat = MSM_PM_STAT_IDLE_POWER_COLLAPSE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700804 break;
805 }
806
807 default:
808 __WARN();
809 goto cpuidle_enter_bail;
810 }
811
812 time = ktime_to_ns(ktime_get()) - time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700813 msm_pm_add_stat(exit_stat, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700814
815 do_div(time, 1000);
816 return (int) time;
817
818cpuidle_enter_bail:
819 return 0;
820}
821
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700822void msm_pm_cpu_enter_lowpower(unsigned int cpu)
823{
824 int i;
825 bool allow[MSM_PM_SLEEP_MODE_NR];
826
827 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
828 struct msm_pm_platform_data *mode;
829
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600830 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700831 allow[i] = mode->suspend_supported && mode->suspend_enabled;
832 }
833
834 if (MSM_PM_DEBUG_HOTPLUG & msm_pm_debug_mask)
835 pr_notice("CPU%u: %s: shutting down cpu\n", cpu, __func__);
836
Matt Wagantall5375aa72012-07-02 19:59:51 -0700837 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700838 msm_pm_power_collapse(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -0700839 else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700840 msm_pm_power_collapse_standalone(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -0700841 else if (allow[MSM_PM_SLEEP_MODE_RETENTION])
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600842 msm_pm_retention();
Stephen Boydbda74272012-08-09 14:01:27 -0700843 else
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700844 msm_pm_swfi();
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700845}
846
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700847static int msm_pm_enter(suspend_state_t state)
848{
849 bool allow[MSM_PM_SLEEP_MODE_NR];
850 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700851 int64_t period = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530852 int64_t time = msm_pm_timer_enter_suspend(&period);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700853
854 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
855 pr_info("%s\n", __func__);
856
857 if (smp_processor_id()) {
858 __WARN();
859 goto enter_exit;
860 }
861
862
863 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
864 struct msm_pm_platform_data *mode;
865
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600866 mode = &msm_pm_sleep_modes[MSM_PM_MODE(0, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700867 allow[i] = mode->suspend_supported && mode->suspend_enabled;
868 }
869
870 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600871 void *rs_limits = NULL;
872 int ret = -ENODEV;
873 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700874
875 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
876 pr_info("%s: power collapse\n", __func__);
877
878 clock_debug_print_enabled();
879
880#ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE
881 if (msm_pm_sleep_time_override > 0) {
882 int64_t ns = NSEC_PER_SEC *
883 (int64_t) msm_pm_sleep_time_override;
884 msm_pm_set_max_sleep_time(ns);
885 msm_pm_sleep_time_override = 0;
886 }
887#endif /* CONFIG_MSM_SLEEP_TIME_OVERRIDE */
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600888 if (pm_sleep_ops.lowest_limits)
889 rs_limits = pm_sleep_ops.lowest_limits(false,
890 MSM_PM_SLEEP_MODE_POWER_COLLAPSE, -1,
891 -1, &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700892
893 if (rs_limits) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600894 if (pm_sleep_ops.enter_sleep)
895 ret = pm_sleep_ops.enter_sleep(
896 msm_pm_max_sleep_time,
897 rs_limits, false, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700898 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600899 int collapsed = msm_pm_power_collapse(false);
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600900 if (pm_sleep_ops.exit_sleep) {
901 pm_sleep_ops.exit_sleep(rs_limits,
902 false, true, collapsed);
903 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700904 }
905 } else {
906 pr_err("%s: cannot find the lowest power limit\n",
907 __func__);
908 }
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530909 time = msm_pm_timer_exit_suspend(time, period);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700910 msm_pm_add_stat(MSM_PM_STAT_SUSPEND, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700911 } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
912 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
913 pr_info("%s: standalone power collapse\n", __func__);
914 msm_pm_power_collapse_standalone(false);
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600915 } else if (allow[MSM_PM_SLEEP_MODE_RETENTION]) {
916 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
917 pr_info("%s: retention\n", __func__);
918 msm_pm_retention();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700919 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
920 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
921 pr_info("%s: swfi\n", __func__);
922 msm_pm_swfi();
923 }
924
925
926enter_exit:
927 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
928 pr_info("%s: return\n", __func__);
929
930 return 0;
931}
932
933static struct platform_suspend_ops msm_pm_ops = {
934 .enter = msm_pm_enter,
935 .valid = suspend_valid_only_mem,
936};
937
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700938/******************************************************************************
939 * Initialization routine
940 *****************************************************************************/
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600941void msm_pm_set_sleep_ops(struct msm_pm_sleep_ops *ops)
942{
943 if (ops)
944 pm_sleep_ops = *ops;
945}
946
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600947void __init msm_pm_set_tz_retention_flag(unsigned int flag)
948{
949 msm_pm_retention_tz_call = flag;
950}
951
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -0600952static int __devinit msm_pc_debug_probe(struct platform_device *pdev)
953{
954 struct resource *res = NULL;
955 int i ;
956
957 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
958 if (!res)
959 goto fail;
960
961 msm_pc_debug_counters_phys = res->start;
962 WARN_ON(resource_size(res) < SZ_64);
963 msm_pc_debug_counters = devm_ioremap(&pdev->dev, res->start,
964 resource_size(res));
965
966 if (!msm_pc_debug_counters)
967 goto fail;
968
969 for (i = 0; i < resource_size(res)/4; i++)
970 __raw_writel(0, msm_pc_debug_counters + i * 4);
971 return 0;
972fail:
973 msm_pc_debug_counters = 0;
974 msm_pc_debug_counters_phys = 0;
975 return -EFAULT;
976}
977
978static struct of_device_id msm_pc_debug_table[] = {
979 {.compatible = "qcom,pc-cntr"},
980 {},
981};
982
983static struct platform_driver msm_pc_counter_driver = {
984 .probe = msm_pc_debug_probe,
985 .driver = {
986 .name = "pc-cntr",
987 .owner = THIS_MODULE,
988 .of_match_table = msm_pc_debug_table,
989 },
990};
991
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700992static int __init msm_pm_init(void)
993{
994 pgd_t *pc_pgd;
995 pmd_t *pmd;
996 unsigned long pmdval;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530997 enum msm_pm_time_stats_id enable_stats[] = {
998 MSM_PM_STAT_IDLE_WFI,
999 MSM_PM_STAT_RETENTION,
1000 MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE,
1001 MSM_PM_STAT_IDLE_POWER_COLLAPSE,
1002 MSM_PM_STAT_SUSPEND,
1003 };
Steve Mucklef132c6c2012-06-06 18:30:57 -07001004 unsigned long exit_phys;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301005
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001006 /* Page table for cores to come back up safely. */
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -06001007
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001008 pc_pgd = pgd_alloc(&init_mm);
1009 if (!pc_pgd)
1010 return -ENOMEM;
1011
Steve Mucklef132c6c2012-06-06 18:30:57 -07001012 exit_phys = virt_to_phys(msm_pm_collapse_exit);
1013
1014 pmd = pmd_offset(pud_offset(pc_pgd + pgd_index(exit_phys),exit_phys),
1015 exit_phys);
1016 pmdval = (exit_phys & PGDIR_MASK) |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001017 PMD_TYPE_SECT | PMD_SECT_AP_WRITE;
1018 pmd[0] = __pmd(pmdval);
1019 pmd[1] = __pmd(pmdval + (1 << (PGDIR_SHIFT - 1)));
1020
Steve Mucklefcece052012-02-18 20:09:58 -08001021 msm_saved_state_phys =
1022 allocate_contiguous_ebi_nomap(CPU_SAVED_STATE_SIZE *
1023 num_possible_cpus(), 4);
1024 if (!msm_saved_state_phys)
1025 return -ENOMEM;
1026 msm_saved_state = ioremap_nocache(msm_saved_state_phys,
1027 CPU_SAVED_STATE_SIZE *
1028 num_possible_cpus());
1029 if (!msm_saved_state)
1030 return -ENOMEM;
1031
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001032 /* It is remotely possible that the code in msm_pm_collapse_exit()
1033 * which turns on the MMU with this mapping is in the
1034 * next even-numbered megabyte beyond the
1035 * start of msm_pm_collapse_exit().
1036 * Map this megabyte in as well.
1037 */
1038 pmd[2] = __pmd(pmdval + (2 << (PGDIR_SHIFT - 1)));
1039 flush_pmd_entry(pmd);
1040 msm_pm_pc_pgd = virt_to_phys(pc_pgd);
Steve Muckle730ad7a2012-02-21 15:26:37 -08001041 clean_caches((unsigned long)&msm_pm_pc_pgd, sizeof(msm_pm_pc_pgd),
1042 virt_to_phys(&msm_pm_pc_pgd));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001043
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001044 msm_pm_mode_sysfs_add();
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301045 msm_pm_add_stats(enable_stats, ARRAY_SIZE(enable_stats));
1046
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001047 suspend_set_ops(&msm_pm_ops);
Praveen Chidambaram192979f2012-04-25 18:30:23 -06001048 msm_pm_qtimer_available();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001049 msm_cpuidle_init();
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -06001050 platform_driver_register(&msm_pc_counter_driver);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001051
1052 return 0;
1053}
1054
1055late_initcall(msm_pm_init);