blob: 0339c211141848eaba88b8f3ba83e87920d24546 [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>
Praveen Chidambaramc594a092012-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"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070049#include "scm-boot.h"
50#include "spm.h"
51#include "timer.h"
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -060052#include "pm-boot.h"
Girish Mahadevandc318fd2012-08-17 16:48:05 -060053#include <mach/event_timer.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070054
55/******************************************************************************
56 * Debug Definitions
57 *****************************************************************************/
58
59enum {
60 MSM_PM_DEBUG_SUSPEND = BIT(0),
61 MSM_PM_DEBUG_POWER_COLLAPSE = BIT(1),
62 MSM_PM_DEBUG_SUSPEND_LIMITS = BIT(2),
63 MSM_PM_DEBUG_CLOCK = BIT(3),
64 MSM_PM_DEBUG_RESET_VECTOR = BIT(4),
Karthik Parsha6fb932d2012-01-24 18:04:12 -080065 MSM_PM_DEBUG_IDLE_CLK = BIT(5),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070066 MSM_PM_DEBUG_IDLE = BIT(6),
67 MSM_PM_DEBUG_IDLE_LIMITS = BIT(7),
68 MSM_PM_DEBUG_HOTPLUG = BIT(8),
69};
70
71static int msm_pm_debug_mask = 1;
72module_param_named(
73 debug_mask, msm_pm_debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP
74);
Girish Mahadevand27ca4a2012-08-15 09:21:23 -060075static int msm_pm_retention_tz_call;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070076
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070077/******************************************************************************
78 * Sleep Modes and Parameters
79 *****************************************************************************/
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070080enum {
81 MSM_PM_MODE_ATTR_SUSPEND,
82 MSM_PM_MODE_ATTR_IDLE,
83 MSM_PM_MODE_ATTR_NR,
84};
85
Girish Mahadevand27ca4a2012-08-15 09:21:23 -060086#define SCM_L2_RETENTION (0x2)
87#define SCM_CMD_TERMINATE_PC (0x2)
88
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070089static char *msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_NR] = {
90 [MSM_PM_MODE_ATTR_SUSPEND] = "suspend_enabled",
91 [MSM_PM_MODE_ATTR_IDLE] = "idle_enabled",
92};
93
94struct msm_pm_kobj_attribute {
95 unsigned int cpu;
96 struct kobj_attribute ka;
97};
98
99#define GET_CPU_OF_ATTR(attr) \
100 (container_of(attr, struct msm_pm_kobj_attribute, ka)->cpu)
101
102struct msm_pm_sysfs_sleep_mode {
103 struct kobject *kobj;
104 struct attribute_group attr_group;
105 struct attribute *attrs[MSM_PM_MODE_ATTR_NR + 1];
106 struct msm_pm_kobj_attribute kas[MSM_PM_MODE_ATTR_NR];
107};
108
109static char *msm_pm_sleep_mode_labels[MSM_PM_SLEEP_MODE_NR] = {
110 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE] = "power_collapse",
111 [MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT] = "wfi",
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600112 [MSM_PM_SLEEP_MODE_RETENTION] = "retention",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700113 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE] =
114 "standalone_power_collapse",
115};
116
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600117static struct hrtimer pm_hrtimer;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600118static struct msm_pm_sleep_ops pm_sleep_ops;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700119/*
120 * Write out the attribute.
121 */
122static ssize_t msm_pm_mode_attr_show(
123 struct kobject *kobj, struct kobj_attribute *attr, char *buf)
124{
125 int ret = -EINVAL;
126 int i;
127
128 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
129 struct kernel_param kp;
130 unsigned int cpu;
131 struct msm_pm_platform_data *mode;
132
133 if (msm_pm_sleep_mode_labels[i] == NULL)
134 continue;
135
136 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
137 continue;
138
139 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600140 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700141
142 if (!strcmp(attr->attr.name,
143 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
144 u32 arg = mode->suspend_enabled;
145 kp.arg = &arg;
146 ret = param_get_ulong(buf, &kp);
147 } else if (!strcmp(attr->attr.name,
148 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
149 u32 arg = mode->idle_enabled;
150 kp.arg = &arg;
151 ret = param_get_ulong(buf, &kp);
152 }
153
154 break;
155 }
156
157 if (ret > 0) {
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600158 strlcat(buf, "\n", PAGE_SIZE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700159 ret++;
160 }
161
162 return ret;
163}
164
165/*
166 * Read in the new attribute value.
167 */
168static ssize_t msm_pm_mode_attr_store(struct kobject *kobj,
169 struct kobj_attribute *attr, const char *buf, size_t count)
170{
171 int ret = -EINVAL;
172 int i;
173
174 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
175 struct kernel_param kp;
176 unsigned int cpu;
177 struct msm_pm_platform_data *mode;
178
179 if (msm_pm_sleep_mode_labels[i] == NULL)
180 continue;
181
182 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
183 continue;
184
185 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600186 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700187
188 if (!strcmp(attr->attr.name,
189 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
190 kp.arg = &mode->suspend_enabled;
191 ret = param_set_byte(buf, &kp);
192 } else if (!strcmp(attr->attr.name,
193 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
194 kp.arg = &mode->idle_enabled;
195 ret = param_set_byte(buf, &kp);
196 }
197
198 break;
199 }
200
201 return ret ? ret : count;
202}
203
204/*
205 * Add sysfs entries for one cpu.
206 */
207static int __init msm_pm_mode_sysfs_add_cpu(
208 unsigned int cpu, struct kobject *modes_kobj)
209{
210 char cpu_name[8];
211 struct kobject *cpu_kobj;
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600212 struct msm_pm_sysfs_sleep_mode *mode = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700213 int i, j, k;
214 int ret;
215
216 snprintf(cpu_name, sizeof(cpu_name), "cpu%u", cpu);
217 cpu_kobj = kobject_create_and_add(cpu_name, modes_kobj);
218 if (!cpu_kobj) {
219 pr_err("%s: cannot create %s kobject\n", __func__, cpu_name);
220 ret = -ENOMEM;
221 goto mode_sysfs_add_cpu_exit;
222 }
223
224 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
225 int idx = MSM_PM_MODE(cpu, i);
226
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600227 if ((!msm_pm_sleep_modes[idx].suspend_supported)
228 && (!msm_pm_sleep_modes[idx].idle_supported))
229 continue;
230
231 if (!msm_pm_sleep_mode_labels[i] ||
232 !msm_pm_sleep_mode_labels[i][0])
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700233 continue;
234
235 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
236 if (!mode) {
237 pr_err("%s: cannot allocate memory for attributes\n",
238 __func__);
239 ret = -ENOMEM;
240 goto mode_sysfs_add_cpu_exit;
241 }
242
243 mode->kobj = kobject_create_and_add(
244 msm_pm_sleep_mode_labels[i], cpu_kobj);
245 if (!mode->kobj) {
246 pr_err("%s: cannot create kobject\n", __func__);
247 ret = -ENOMEM;
248 goto mode_sysfs_add_cpu_exit;
249 }
250
251 for (k = 0, j = 0; k < MSM_PM_MODE_ATTR_NR; k++) {
252 if ((k == MSM_PM_MODE_ATTR_IDLE) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600253 !msm_pm_sleep_modes[idx].idle_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700254 continue;
255 if ((k == MSM_PM_MODE_ATTR_SUSPEND) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600256 !msm_pm_sleep_modes[idx].suspend_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700257 continue;
Stephen Boydd2059c32012-07-03 15:11:15 -0700258 sysfs_attr_init(&mode->kas[j].ka.attr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700259 mode->kas[j].cpu = cpu;
260 mode->kas[j].ka.attr.mode = 0644;
261 mode->kas[j].ka.show = msm_pm_mode_attr_show;
262 mode->kas[j].ka.store = msm_pm_mode_attr_store;
263 mode->kas[j].ka.attr.name = msm_pm_mode_attr_labels[k];
264 mode->attrs[j] = &mode->kas[j].ka.attr;
265 j++;
266 }
267 mode->attrs[j] = NULL;
268
269 mode->attr_group.attrs = mode->attrs;
270 ret = sysfs_create_group(mode->kobj, &mode->attr_group);
271 if (ret) {
272 pr_err("%s: cannot create kobject attribute group\n",
273 __func__);
274 goto mode_sysfs_add_cpu_exit;
275 }
276 }
277
278 ret = 0;
279
280mode_sysfs_add_cpu_exit:
Praveen Chidambaramd5ac2d32011-10-24 14:30:27 -0600281 if (ret) {
Praveen Chidambaram2cfda632011-10-11 16:58:09 -0600282 if (mode && mode->kobj)
283 kobject_del(mode->kobj);
284 kfree(mode);
285 }
286
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700287 return ret;
288}
289
290/*
291 * Add sysfs entries for the sleep modes.
292 */
293static int __init msm_pm_mode_sysfs_add(void)
294{
295 struct kobject *module_kobj;
296 struct kobject *modes_kobj;
297 unsigned int cpu;
298 int ret;
299
300 module_kobj = kset_find_obj(module_kset, KBUILD_MODNAME);
301 if (!module_kobj) {
302 pr_err("%s: cannot find kobject for module %s\n",
303 __func__, KBUILD_MODNAME);
304 ret = -ENOENT;
305 goto mode_sysfs_add_exit;
306 }
307
308 modes_kobj = kobject_create_and_add("modes", module_kobj);
309 if (!modes_kobj) {
310 pr_err("%s: cannot create modes kobject\n", __func__);
311 ret = -ENOMEM;
312 goto mode_sysfs_add_exit;
313 }
314
315 for_each_possible_cpu(cpu) {
316 ret = msm_pm_mode_sysfs_add_cpu(cpu, modes_kobj);
317 if (ret)
318 goto mode_sysfs_add_exit;
319 }
320
321 ret = 0;
322
323mode_sysfs_add_exit:
324 return ret;
325}
326
327/******************************************************************************
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700328 * Configure Hardware before/after Low Power Mode
329 *****************************************************************************/
330
331/*
332 * Configure hardware registers in preparation for Apps power down.
333 */
334static void msm_pm_config_hw_before_power_down(void)
335{
336 return;
337}
338
339/*
340 * Clear hardware registers after Apps powers up.
341 */
342static void msm_pm_config_hw_after_power_up(void)
343{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700344}
345
346/*
347 * Configure hardware registers in preparation for SWFI.
348 */
349static void msm_pm_config_hw_before_swfi(void)
350{
351 return;
352}
353
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600354/*
355 * Configure/Restore hardware registers in preparation for Retention.
356 */
357
358static void msm_pm_config_hw_after_retention(void)
359{
360 int ret;
361 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
362 WARN_ON(ret);
363}
364
365static void msm_pm_config_hw_before_retention(void)
366{
367 return;
368}
369
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700370
371/******************************************************************************
372 * Suspend Max Sleep Time
373 *****************************************************************************/
374
375#ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE
376static int msm_pm_sleep_time_override;
377module_param_named(sleep_time_override,
378 msm_pm_sleep_time_override, int, S_IRUGO | S_IWUSR | S_IWGRP);
379#endif
380
381#define SCLK_HZ (32768)
382#define MSM_PM_SLEEP_TICK_LIMIT (0x6DDD000)
383
384static uint32_t msm_pm_max_sleep_time;
385
386/*
387 * Convert time from nanoseconds to slow clock ticks, then cap it to the
388 * specified limit
389 */
390static int64_t msm_pm_convert_and_cap_time(int64_t time_ns, int64_t limit)
391{
392 do_div(time_ns, NSEC_PER_SEC / SCLK_HZ);
393 return (time_ns > limit) ? limit : time_ns;
394}
395
396/*
397 * Set the sleep time for suspend. 0 means infinite sleep time.
398 */
399void msm_pm_set_max_sleep_time(int64_t max_sleep_time_ns)
400{
401 if (max_sleep_time_ns == 0) {
402 msm_pm_max_sleep_time = 0;
403 } else {
404 msm_pm_max_sleep_time = (uint32_t)msm_pm_convert_and_cap_time(
405 max_sleep_time_ns, MSM_PM_SLEEP_TICK_LIMIT);
406
407 if (msm_pm_max_sleep_time == 0)
408 msm_pm_max_sleep_time = 1;
409 }
410
411 if (msm_pm_debug_mask & MSM_PM_DEBUG_SUSPEND)
412 pr_info("%s: Requested %lld ns Giving %u sclk ticks\n",
413 __func__, max_sleep_time_ns, msm_pm_max_sleep_time);
414}
415EXPORT_SYMBOL(msm_pm_set_max_sleep_time);
416
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600417struct reg_data {
418 uint32_t reg;
419 uint32_t val;
420};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700421
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600422static struct reg_data reg_saved_state[] = {
423 { .reg = 0x4501, },
424 { .reg = 0x5501, },
425 { .reg = 0x6501, },
426 { .reg = 0x7501, },
427 { .reg = 0x0500, },
428};
429
430static unsigned int active_vdd;
431static bool msm_pm_save_cp15;
432static const unsigned int pc_vdd = 0x98;
433
434static void msm_pm_save_cpu_reg(void)
435{
436 int i;
437
438 /* Only on core0 */
439 if (smp_processor_id())
440 return;
441
442 /**
443 * On some targets, L2 PC will turn off may reset the core
444 * configuration for the mux and the default may not make the core
445 * happy when it resumes.
446 * Save the active vdd, and set the core vdd to QSB max vdd, so that
447 * when the core resumes, it is capable of supporting the current QSB
448 * rate. Then restore the active vdd before switching the acpuclk rate.
449 */
450 if (msm_pm_get_l2_flush_flag() == 1) {
451 active_vdd = msm_spm_get_vdd(0);
452 for (i = 0; i < ARRAY_SIZE(reg_saved_state); i++)
453 reg_saved_state[i].val =
454 get_l2_indirect_reg(reg_saved_state[i].reg);
455 msm_spm_set_vdd(0, pc_vdd);
456 }
457}
458
459static void msm_pm_restore_cpu_reg(void)
460{
461 int i;
462
463 /* Only on core0 */
464 if (smp_processor_id())
465 return;
466
467 if (msm_pm_get_l2_flush_flag() == 1) {
468 for (i = 0; i < ARRAY_SIZE(reg_saved_state); i++)
469 set_l2_indirect_reg(reg_saved_state[i].reg,
470 reg_saved_state[i].val);
471 msm_spm_set_vdd(0, active_vdd);
472 }
473}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700474
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600475static void *msm_pm_idle_rs_limits;
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600476static bool msm_pm_use_qtimer;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700477
478static void msm_pm_swfi(void)
479{
480 msm_pm_config_hw_before_swfi();
481 msm_arch_idle();
482}
483
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600484
485static void msm_pm_retention(void)
486{
487 int ret = 0;
488
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600489 msm_pm_config_hw_before_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600490 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_POWER_RETENTION, false);
491 WARN_ON(ret);
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600492
493 if (msm_pm_retention_tz_call)
494 scm_call_atomic1(SCM_SVC_BOOT, SCM_CMD_TERMINATE_PC,
495 SCM_L2_RETENTION);
496 else
497 msm_arch_idle();
498
499 msm_pm_config_hw_after_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600500}
501
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600502#ifdef CONFIG_CACHE_L2X0
503static inline bool msm_pm_l2x0_power_collapse(void)
504{
505 bool collapsed = 0;
506
Taniya Das38a8c6e2012-05-09 20:34:39 +0530507 l2cc_suspend();
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600508 collapsed = msm_pm_collapse();
Taniya Das38a8c6e2012-05-09 20:34:39 +0530509 l2cc_resume();
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600510
511 return collapsed;
512}
513#else
514static inline bool msm_pm_l2x0_power_collapse(void)
515{
516 return msm_pm_collapse();
517}
518#endif
519
Stephen Boydb29750d2012-02-21 01:21:32 -0800520static bool __ref msm_pm_spm_power_collapse(
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700521 unsigned int cpu, bool from_idle, bool notify_rpm)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700522{
523 void *entry;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600524 bool collapsed = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700525 int ret;
Rohit Vaswanie78dfb62012-02-21 10:29:29 -0800526 unsigned int saved_gic_cpu_ctrl;
527
528 saved_gic_cpu_ctrl = readl_relaxed(MSM_QGIC_CPU_BASE + GIC_CPU_CTRL);
529 mb();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700530
531 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
532 pr_info("CPU%u: %s: notify_rpm %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700533 cpu, __func__, (int) notify_rpm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700534
535 ret = msm_spm_set_low_power_mode(
536 MSM_SPM_MODE_POWER_COLLAPSE, notify_rpm);
537 WARN_ON(ret);
538
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700539 entry = (!cpu || from_idle) ?
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700540 msm_pm_collapse_exit : msm_secondary_startup;
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700541 msm_pm_boot_config_before_pc(cpu, virt_to_phys(entry));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700542
543 if (MSM_PM_DEBUG_RESET_VECTOR & msm_pm_debug_mask)
544 pr_info("CPU%u: %s: program vector to %p\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700545 cpu, __func__, entry);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700546
547#ifdef CONFIG_VFP
Steve Mucklef132c6c2012-06-06 18:30:57 -0700548 vfp_pm_suspend();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700549#endif
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600550 collapsed = msm_pm_l2x0_power_collapse();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700551
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700552 msm_pm_boot_config_after_pc(cpu);
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -0600553
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700554 if (collapsed) {
555#ifdef CONFIG_VFP
Steve Mucklef132c6c2012-06-06 18:30:57 -0700556 vfp_pm_resume();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700557#endif
558 cpu_init();
559 writel(0xF0, MSM_QGIC_CPU_BASE + GIC_CPU_PRIMASK);
Rohit Vaswanie78dfb62012-02-21 10:29:29 -0800560 writel_relaxed(saved_gic_cpu_ctrl,
561 MSM_QGIC_CPU_BASE + GIC_CPU_CTRL);
562 mb();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700563 local_fiq_enable();
564 }
565
566 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
567 pr_info("CPU%u: %s: msm_pm_collapse returned, collapsed %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700568 cpu, __func__, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700569
570 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
571 WARN_ON(ret);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600572 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700573}
574
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600575static bool msm_pm_power_collapse_standalone(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700576{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700577 unsigned int cpu = smp_processor_id();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700578 unsigned int avsdscr_setting;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600579 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700580
581 avsdscr_setting = avs_get_avsdscr();
582 avs_disable();
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700583 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700584 avs_reset_delays(avsdscr_setting);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600585 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700586}
587
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600588static bool msm_pm_power_collapse(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700589{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700590 unsigned int cpu = smp_processor_id();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700591 unsigned long saved_acpuclk_rate;
592 unsigned int avsdscr_setting;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600593 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700594
595 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
596 pr_info("CPU%u: %s: idle %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700597 cpu, __func__, (int)from_idle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700598
599 msm_pm_config_hw_before_power_down();
600 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700601 pr_info("CPU%u: %s: pre power down\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700602
603 avsdscr_setting = avs_get_avsdscr();
604 avs_disable();
605
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700606 if (cpu_online(cpu))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700607 saved_acpuclk_rate = acpuclk_power_collapse();
608 else
609 saved_acpuclk_rate = 0;
610
611 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
612 pr_info("CPU%u: %s: change clock rate (old rate = %lu)\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700613 cpu, __func__, saved_acpuclk_rate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700614
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600615 if (msm_pm_save_cp15)
616 msm_pm_save_cpu_reg();
617
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700618 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700619
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600620 if (msm_pm_save_cp15)
621 msm_pm_restore_cpu_reg();
622
Girish Mahadevan4e025d92012-02-29 13:26:51 -0700623 if (cpu_online(cpu)) {
624 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
625 pr_info("CPU%u: %s: restore clock rate to %lu\n",
626 cpu, __func__, saved_acpuclk_rate);
627 if (acpuclk_set_rate(cpu, saved_acpuclk_rate, SETRATE_PC) < 0)
628 pr_err("CPU%u: %s: failed to restore clock rate(%lu)\n",
629 cpu, __func__, saved_acpuclk_rate);
630 } else {
631 unsigned int gic_dist_enabled;
632 unsigned int gic_dist_pending;
633 gic_dist_enabled = readl_relaxed(
634 MSM_QGIC_DIST_BASE + GIC_DIST_ENABLE_CLEAR);
635 gic_dist_pending = readl_relaxed(
636 MSM_QGIC_DIST_BASE + GIC_DIST_PENDING_SET);
637 mb();
638 gic_dist_pending &= gic_dist_enabled;
639
640 if (gic_dist_pending)
641 pr_err("CPU %d interrupted during hotplug.Pending int 0x%x\n",
642 cpu, gic_dist_pending);
643 }
644
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700645
646 avs_reset_delays(avsdscr_setting);
647 msm_pm_config_hw_after_power_up();
648 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700649 pr_info("CPU%u: %s: post power up\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700650
651 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700652 pr_info("CPU%u: %s: return\n", cpu, __func__);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600653 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700654}
655
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600656static void msm_pm_target_init(void)
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600657{
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600658 if (cpu_is_apq8064())
659 msm_pm_save_cp15 = true;
660
661 if (cpu_is_msm8974())
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600662 msm_pm_use_qtimer = true;
663}
664
665static int64_t msm_pm_timer_enter_idle(void)
666{
667 if (msm_pm_use_qtimer)
668 return ktime_to_ns(tick_nohz_get_sleep_length());
669
670 return msm_timer_enter_idle();
671}
672
673static void msm_pm_timer_exit_idle(bool timer_halted)
674{
675 if (msm_pm_use_qtimer)
676 return;
677
678 msm_timer_exit_idle((int) timer_halted);
679}
680
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530681static int64_t msm_pm_timer_enter_suspend(int64_t *period)
682{
Anji Jonnalac02367a2012-07-01 02:56:11 +0530683 int64_t time = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530684
685 if (msm_pm_use_qtimer)
686 return sched_clock();
687
688 time = msm_timer_get_sclk_time(period);
689 if (!time)
690 pr_err("%s: Unable to read sclk.\n", __func__);
691
692 return time;
693}
694
695static int64_t msm_pm_timer_exit_suspend(int64_t time, int64_t period)
696{
697 if (msm_pm_use_qtimer)
698 return sched_clock() - time;
699
700 if (time != 0) {
701 int64_t end_time = msm_timer_get_sclk_time(NULL);
702 if (end_time != 0) {
703 time = end_time - time;
704 if (time < 0)
705 time += period;
706 } else
707 time = 0;
708 }
709
710 return time;
711}
712
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600713/**
714 * pm_hrtimer_cb() : Callback function for hrtimer created if the
715 * core needs to be awake to handle an event.
716 * @hrtimer : Pointer to hrtimer
717 */
718static enum hrtimer_restart pm_hrtimer_cb(struct hrtimer *hrtimer)
719{
720 return HRTIMER_NORESTART;
721}
722
723/**
724 * msm_pm_set_timer() : Set an hrtimer to wakeup the core in time
725 * to handle an event.
726 */
727static void msm_pm_set_timer(uint32_t modified_time_us)
728{
729 u64 modified_time_ns = modified_time_us * NSEC_PER_USEC;
730 ktime_t modified_ktime = ns_to_ktime(modified_time_ns);
731 pm_hrtimer.function = pm_hrtimer_cb;
732 hrtimer_start(&pm_hrtimer, modified_ktime, HRTIMER_MODE_ABS);
733}
734
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700735/******************************************************************************
736 * External Idle/Suspend Functions
737 *****************************************************************************/
738
739void arch_idle(void)
740{
741 return;
742}
743
Steve Mucklef132c6c2012-06-06 18:30:57 -0700744int msm_pm_idle_prepare(struct cpuidle_device *dev,
745 struct cpuidle_driver *drv, int index)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700746{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700747 int i;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700748 unsigned int power_usage = -1;
749 int ret = 0;
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600750 uint32_t modified_time_us = 0;
751 struct msm_pm_time_params time_param;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700752
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600753 time_param.latency_us =
754 (uint32_t) pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
755 time_param.sleep_us =
756 (uint32_t) (ktime_to_us(tick_nohz_get_sleep_length())
757 & UINT_MAX);
758 time_param.modified_time_us = 0;
759
760 if (!dev->cpu)
761 time_param.next_event_us =
762 (uint32_t) (ktime_to_us(get_next_event_time())
763 & UINT_MAX);
764 else
765 time_param.next_event_us = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700766
767 for (i = 0; i < dev->state_count; i++) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700768 struct cpuidle_state *state = &drv->states[i];
769 struct cpuidle_state_usage *st_usage = &dev->states_usage[i];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700770 enum msm_pm_sleep_mode mode;
771 bool allow;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600772 void *rs_limits = NULL;
773 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700774 int idx;
775
Steve Mucklef132c6c2012-06-06 18:30:57 -0700776 mode = (enum msm_pm_sleep_mode) cpuidle_get_statedata(st_usage);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700777 idx = MSM_PM_MODE(dev->cpu, mode);
778
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600779 allow = msm_pm_sleep_modes[idx].idle_enabled &&
780 msm_pm_sleep_modes[idx].idle_supported;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700781
782 switch (mode) {
783 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
Mahesh Sivasubramanianba7e01c2012-09-04 18:22:51 -0600784 case MSM_PM_SLEEP_MODE_RETENTION:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700785 if (!allow)
786 break;
787
788 if (num_online_cpus() > 1) {
789 allow = false;
790 break;
791 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700792 /* fall through */
793
794 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
795 if (!allow)
796 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700797 /* fall through */
798
799 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
800 if (!allow)
801 break;
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600802 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700803
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600804 if (pm_sleep_ops.lowest_limits)
805 rs_limits = pm_sleep_ops.lowest_limits(true,
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600806 mode, &time_param, &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700807
808 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
809 pr_info("CPU%u: %s: %s, latency %uus, "
810 "sleep %uus, limit %p\n",
811 dev->cpu, __func__, state->desc,
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600812 time_param.latency_us,
813 time_param.sleep_us, rs_limits);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700814
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700815 if (!rs_limits)
816 allow = false;
817 break;
818
819 default:
820 allow = false;
821 break;
822 }
823
824 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
825 pr_info("CPU%u: %s: allow %s: %d\n",
826 dev->cpu, __func__, state->desc, (int)allow);
827
828 if (allow) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700829 if (power < power_usage) {
830 power_usage = power;
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600831 modified_time_us = time_param.modified_time_us;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700832 ret = mode;
833 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700834
835 if (MSM_PM_SLEEP_MODE_POWER_COLLAPSE == mode)
836 msm_pm_idle_rs_limits = rs_limits;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700837 }
838 }
839
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600840 if (modified_time_us && !dev->cpu)
841 msm_pm_set_timer(modified_time_us);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700842 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700843}
844
845int msm_pm_idle_enter(enum msm_pm_sleep_mode sleep_mode)
846{
847 int64_t time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700848 int exit_stat;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700849
850 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
851 pr_info("CPU%u: %s: mode %d\n",
852 smp_processor_id(), __func__, sleep_mode);
853
854 time = ktime_to_ns(ktime_get());
855
856 switch (sleep_mode) {
857 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
858 msm_pm_swfi();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700859 exit_stat = MSM_PM_STAT_IDLE_WFI;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700860 break;
861
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600862 case MSM_PM_SLEEP_MODE_RETENTION:
863 msm_pm_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600864 exit_stat = MSM_PM_STAT_RETENTION;
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600865 break;
866
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700867 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
868 msm_pm_power_collapse_standalone(true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700869 exit_stat = MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700870 break;
871
872 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE: {
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600873 int64_t timer_expiration = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700874 bool timer_halted = false;
875 uint32_t sleep_delay;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600876 int ret = -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700877 int notify_rpm =
878 (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600879 int collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700880
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600881 timer_expiration = msm_pm_timer_enter_idle();
882
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700883 sleep_delay = (uint32_t) msm_pm_convert_and_cap_time(
884 timer_expiration, MSM_PM_SLEEP_TICK_LIMIT);
885 if (sleep_delay == 0) /* 0 would mean infinite time */
886 sleep_delay = 1;
887
Karthik Parsha6fb932d2012-01-24 18:04:12 -0800888 if (MSM_PM_DEBUG_IDLE_CLK & msm_pm_debug_mask)
889 clock_debug_print_enabled();
890
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600891 if (pm_sleep_ops.enter_sleep)
892 ret = pm_sleep_ops.enter_sleep(sleep_delay,
893 msm_pm_idle_rs_limits,
894 true, notify_rpm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700895 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600896 collapsed = msm_pm_power_collapse(true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700897 timer_halted = true;
898
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600899 if (pm_sleep_ops.exit_sleep)
900 pm_sleep_ops.exit_sleep(msm_pm_idle_rs_limits,
901 true, notify_rpm, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700902 }
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600903 msm_pm_timer_exit_idle(timer_halted);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700904 exit_stat = MSM_PM_STAT_IDLE_POWER_COLLAPSE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700905 break;
906 }
907
908 default:
909 __WARN();
910 goto cpuidle_enter_bail;
911 }
912
913 time = ktime_to_ns(ktime_get()) - time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700914 msm_pm_add_stat(exit_stat, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700915
916 do_div(time, 1000);
917 return (int) time;
918
919cpuidle_enter_bail:
920 return 0;
921}
922
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700923void msm_pm_cpu_enter_lowpower(unsigned int cpu)
924{
925 int i;
926 bool allow[MSM_PM_SLEEP_MODE_NR];
927
928 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
929 struct msm_pm_platform_data *mode;
930
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600931 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700932 allow[i] = mode->suspend_supported && mode->suspend_enabled;
933 }
934
935 if (MSM_PM_DEBUG_HOTPLUG & msm_pm_debug_mask)
936 pr_notice("CPU%u: %s: shutting down cpu\n", cpu, __func__);
937
Matt Wagantall5375aa72012-07-02 19:59:51 -0700938 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700939 msm_pm_power_collapse(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -0700940 else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700941 msm_pm_power_collapse_standalone(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -0700942 else if (allow[MSM_PM_SLEEP_MODE_RETENTION])
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600943 msm_pm_retention();
Stephen Boydbda74272012-08-09 14:01:27 -0700944 else
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700945 msm_pm_swfi();
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700946}
947
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700948static int msm_pm_enter(suspend_state_t state)
949{
950 bool allow[MSM_PM_SLEEP_MODE_NR];
951 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700952 int64_t period = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530953 int64_t time = msm_pm_timer_enter_suspend(&period);
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600954 struct msm_pm_time_params time_param;
955
956 time_param.latency_us = -1;
957 time_param.sleep_us = -1;
958 time_param.next_event_us = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700959
960 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
961 pr_info("%s\n", __func__);
962
963 if (smp_processor_id()) {
964 __WARN();
965 goto enter_exit;
966 }
967
968
969 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
970 struct msm_pm_platform_data *mode;
971
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600972 mode = &msm_pm_sleep_modes[MSM_PM_MODE(0, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700973 allow[i] = mode->suspend_supported && mode->suspend_enabled;
974 }
975
976 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600977 void *rs_limits = NULL;
978 int ret = -ENODEV;
979 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700980
981 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
982 pr_info("%s: power collapse\n", __func__);
983
984 clock_debug_print_enabled();
985
986#ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE
987 if (msm_pm_sleep_time_override > 0) {
988 int64_t ns = NSEC_PER_SEC *
989 (int64_t) msm_pm_sleep_time_override;
990 msm_pm_set_max_sleep_time(ns);
991 msm_pm_sleep_time_override = 0;
992 }
993#endif /* CONFIG_MSM_SLEEP_TIME_OVERRIDE */
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600994 if (pm_sleep_ops.lowest_limits)
995 rs_limits = pm_sleep_ops.lowest_limits(false,
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600996 MSM_PM_SLEEP_MODE_POWER_COLLAPSE, &time_param, &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700997
998 if (rs_limits) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600999 if (pm_sleep_ops.enter_sleep)
1000 ret = pm_sleep_ops.enter_sleep(
1001 msm_pm_max_sleep_time,
1002 rs_limits, false, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001003 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -06001004 int collapsed = msm_pm_power_collapse(false);
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001005 if (pm_sleep_ops.exit_sleep) {
1006 pm_sleep_ops.exit_sleep(rs_limits,
1007 false, true, collapsed);
1008 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001009 }
1010 } else {
1011 pr_err("%s: cannot find the lowest power limit\n",
1012 __func__);
1013 }
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301014 time = msm_pm_timer_exit_suspend(time, period);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001015 msm_pm_add_stat(MSM_PM_STAT_SUSPEND, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001016 } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
1017 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1018 pr_info("%s: standalone power collapse\n", __func__);
1019 msm_pm_power_collapse_standalone(false);
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -06001020 } else if (allow[MSM_PM_SLEEP_MODE_RETENTION]) {
1021 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1022 pr_info("%s: retention\n", __func__);
1023 msm_pm_retention();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001024 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
1025 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1026 pr_info("%s: swfi\n", __func__);
1027 msm_pm_swfi();
1028 }
1029
1030
1031enter_exit:
1032 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1033 pr_info("%s: return\n", __func__);
1034
1035 return 0;
1036}
1037
1038static struct platform_suspend_ops msm_pm_ops = {
1039 .enter = msm_pm_enter,
1040 .valid = suspend_valid_only_mem,
1041};
1042
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001043/******************************************************************************
1044 * Initialization routine
1045 *****************************************************************************/
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001046void msm_pm_set_sleep_ops(struct msm_pm_sleep_ops *ops)
1047{
1048 if (ops)
1049 pm_sleep_ops = *ops;
1050}
1051
Girish Mahadevand27ca4a2012-08-15 09:21:23 -06001052void __init msm_pm_set_tz_retention_flag(unsigned int flag)
1053{
1054 msm_pm_retention_tz_call = flag;
1055}
1056
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -06001057static int __devinit msm_pc_debug_probe(struct platform_device *pdev)
1058{
1059 struct resource *res = NULL;
1060 int i ;
1061
1062 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1063 if (!res)
1064 goto fail;
1065
1066 msm_pc_debug_counters_phys = res->start;
1067 WARN_ON(resource_size(res) < SZ_64);
1068 msm_pc_debug_counters = devm_ioremap(&pdev->dev, res->start,
1069 resource_size(res));
1070
1071 if (!msm_pc_debug_counters)
1072 goto fail;
1073
1074 for (i = 0; i < resource_size(res)/4; i++)
1075 __raw_writel(0, msm_pc_debug_counters + i * 4);
1076 return 0;
1077fail:
1078 msm_pc_debug_counters = 0;
1079 msm_pc_debug_counters_phys = 0;
1080 return -EFAULT;
1081}
1082
1083static struct of_device_id msm_pc_debug_table[] = {
1084 {.compatible = "qcom,pc-cntr"},
1085 {},
1086};
1087
1088static struct platform_driver msm_pc_counter_driver = {
1089 .probe = msm_pc_debug_probe,
1090 .driver = {
1091 .name = "pc-cntr",
1092 .owner = THIS_MODULE,
1093 .of_match_table = msm_pc_debug_table,
1094 },
1095};
1096
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001097static int __init msm_pm_init(void)
1098{
1099 pgd_t *pc_pgd;
1100 pmd_t *pmd;
1101 unsigned long pmdval;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301102 enum msm_pm_time_stats_id enable_stats[] = {
1103 MSM_PM_STAT_IDLE_WFI,
1104 MSM_PM_STAT_RETENTION,
1105 MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE,
1106 MSM_PM_STAT_IDLE_POWER_COLLAPSE,
1107 MSM_PM_STAT_SUSPEND,
1108 };
Steve Mucklef132c6c2012-06-06 18:30:57 -07001109 unsigned long exit_phys;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301110
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001111 /* Page table for cores to come back up safely. */
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -06001112
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001113 pc_pgd = pgd_alloc(&init_mm);
1114 if (!pc_pgd)
1115 return -ENOMEM;
1116
Steve Mucklef132c6c2012-06-06 18:30:57 -07001117 exit_phys = virt_to_phys(msm_pm_collapse_exit);
1118
1119 pmd = pmd_offset(pud_offset(pc_pgd + pgd_index(exit_phys),exit_phys),
1120 exit_phys);
1121 pmdval = (exit_phys & PGDIR_MASK) |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001122 PMD_TYPE_SECT | PMD_SECT_AP_WRITE;
1123 pmd[0] = __pmd(pmdval);
1124 pmd[1] = __pmd(pmdval + (1 << (PGDIR_SHIFT - 1)));
1125
Steve Mucklefcece052012-02-18 20:09:58 -08001126 msm_saved_state_phys =
1127 allocate_contiguous_ebi_nomap(CPU_SAVED_STATE_SIZE *
1128 num_possible_cpus(), 4);
1129 if (!msm_saved_state_phys)
1130 return -ENOMEM;
1131 msm_saved_state = ioremap_nocache(msm_saved_state_phys,
1132 CPU_SAVED_STATE_SIZE *
1133 num_possible_cpus());
1134 if (!msm_saved_state)
1135 return -ENOMEM;
1136
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001137 /* It is remotely possible that the code in msm_pm_collapse_exit()
1138 * which turns on the MMU with this mapping is in the
1139 * next even-numbered megabyte beyond the
1140 * start of msm_pm_collapse_exit().
1141 * Map this megabyte in as well.
1142 */
1143 pmd[2] = __pmd(pmdval + (2 << (PGDIR_SHIFT - 1)));
1144 flush_pmd_entry(pmd);
1145 msm_pm_pc_pgd = virt_to_phys(pc_pgd);
Steve Muckle730ad7a2012-02-21 15:26:37 -08001146 clean_caches((unsigned long)&msm_pm_pc_pgd, sizeof(msm_pm_pc_pgd),
1147 virt_to_phys(&msm_pm_pc_pgd));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001148
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001149 msm_pm_mode_sysfs_add();
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301150 msm_pm_add_stats(enable_stats, ARRAY_SIZE(enable_stats));
1151
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001152 suspend_set_ops(&msm_pm_ops);
Praveen Chidambaramc594a092012-09-18 19:48:29 -06001153 msm_pm_target_init();
Girish Mahadevandc318fd2012-08-17 16:48:05 -06001154 hrtimer_init(&pm_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001155 msm_cpuidle_init();
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -06001156 platform_driver_register(&msm_pc_counter_driver);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001157
1158 return 0;
1159}
1160
1161late_initcall(msm_pm_init);