blob: 550bb562715adbb6e3569c61512e7ad2a8c9d909 [file] [log] [blame]
Priyanka Mathur26b4a4b2012-11-05 13:45:45 -08001/* Copyright (c) 2010-2012, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13
14#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/init.h>
17#include <linux/completion.h>
18#include <linux/cpuidle.h>
19#include <linux/interrupt.h>
20#include <linux/io.h>
21#include <linux/ktime.h>
22#include <linux/pm.h>
Steve Mucklef132c6c2012-06-06 18:30:57 -070023#include <linux/pm_qos.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070024#include <linux/smp.h>
25#include <linux/suspend.h>
26#include <linux/tick.h>
Mahesh 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>
Girish Mahadevan55944992012-10-26 11:03:07 -060038#include <asm/outercache.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>
Priyanka Mathur26b4a4b2012-11-05 13:45:45 -080054#define CREATE_TRACE_POINTS
55#include "trace_msm_low_power.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070056/******************************************************************************
57 * Debug Definitions
58 *****************************************************************************/
59
60enum {
61 MSM_PM_DEBUG_SUSPEND = BIT(0),
62 MSM_PM_DEBUG_POWER_COLLAPSE = BIT(1),
63 MSM_PM_DEBUG_SUSPEND_LIMITS = BIT(2),
64 MSM_PM_DEBUG_CLOCK = BIT(3),
65 MSM_PM_DEBUG_RESET_VECTOR = BIT(4),
Karthik Parsha6fb932d2012-01-24 18:04:12 -080066 MSM_PM_DEBUG_IDLE_CLK = BIT(5),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070067 MSM_PM_DEBUG_IDLE = BIT(6),
68 MSM_PM_DEBUG_IDLE_LIMITS = BIT(7),
69 MSM_PM_DEBUG_HOTPLUG = BIT(8),
70};
71
72static int msm_pm_debug_mask = 1;
73module_param_named(
74 debug_mask, msm_pm_debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP
75);
Girish Mahadevand27ca4a2012-08-15 09:21:23 -060076static int msm_pm_retention_tz_call;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070077
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070078/******************************************************************************
79 * Sleep Modes and Parameters
80 *****************************************************************************/
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070081enum {
82 MSM_PM_MODE_ATTR_SUSPEND,
83 MSM_PM_MODE_ATTR_IDLE,
84 MSM_PM_MODE_ATTR_NR,
85};
86
Girish Mahadevand27ca4a2012-08-15 09:21:23 -060087#define SCM_L2_RETENTION (0x2)
88#define SCM_CMD_TERMINATE_PC (0x2)
89
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070090static char *msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_NR] = {
91 [MSM_PM_MODE_ATTR_SUSPEND] = "suspend_enabled",
92 [MSM_PM_MODE_ATTR_IDLE] = "idle_enabled",
93};
94
95struct msm_pm_kobj_attribute {
96 unsigned int cpu;
97 struct kobj_attribute ka;
98};
99
100#define GET_CPU_OF_ATTR(attr) \
101 (container_of(attr, struct msm_pm_kobj_attribute, ka)->cpu)
102
103struct msm_pm_sysfs_sleep_mode {
104 struct kobject *kobj;
105 struct attribute_group attr_group;
106 struct attribute *attrs[MSM_PM_MODE_ATTR_NR + 1];
107 struct msm_pm_kobj_attribute kas[MSM_PM_MODE_ATTR_NR];
108};
109
110static char *msm_pm_sleep_mode_labels[MSM_PM_SLEEP_MODE_NR] = {
111 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE] = "power_collapse",
112 [MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT] = "wfi",
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600113 [MSM_PM_SLEEP_MODE_RETENTION] = "retention",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700114 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE] =
115 "standalone_power_collapse",
116};
117
Girish Mahadevan55944992012-10-26 11:03:07 -0600118static struct msm_pm_init_data_type msm_pm_init_data;
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600119static struct hrtimer pm_hrtimer;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600120static struct msm_pm_sleep_ops pm_sleep_ops;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700121/*
122 * Write out the attribute.
123 */
124static ssize_t msm_pm_mode_attr_show(
125 struct kobject *kobj, struct kobj_attribute *attr, char *buf)
126{
127 int ret = -EINVAL;
128 int i;
129
130 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
131 struct kernel_param kp;
132 unsigned int cpu;
133 struct msm_pm_platform_data *mode;
134
135 if (msm_pm_sleep_mode_labels[i] == NULL)
136 continue;
137
138 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
139 continue;
140
141 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600142 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700143
144 if (!strcmp(attr->attr.name,
145 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
146 u32 arg = mode->suspend_enabled;
147 kp.arg = &arg;
148 ret = param_get_ulong(buf, &kp);
149 } else if (!strcmp(attr->attr.name,
150 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
151 u32 arg = mode->idle_enabled;
152 kp.arg = &arg;
153 ret = param_get_ulong(buf, &kp);
154 }
155
156 break;
157 }
158
159 if (ret > 0) {
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600160 strlcat(buf, "\n", PAGE_SIZE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700161 ret++;
162 }
163
164 return ret;
165}
166
167/*
168 * Read in the new attribute value.
169 */
170static ssize_t msm_pm_mode_attr_store(struct kobject *kobj,
171 struct kobj_attribute *attr, const char *buf, size_t count)
172{
173 int ret = -EINVAL;
174 int i;
175
176 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
177 struct kernel_param kp;
178 unsigned int cpu;
179 struct msm_pm_platform_data *mode;
180
181 if (msm_pm_sleep_mode_labels[i] == NULL)
182 continue;
183
184 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
185 continue;
186
187 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600188 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700189
190 if (!strcmp(attr->attr.name,
191 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
192 kp.arg = &mode->suspend_enabled;
193 ret = param_set_byte(buf, &kp);
194 } else if (!strcmp(attr->attr.name,
195 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
196 kp.arg = &mode->idle_enabled;
197 ret = param_set_byte(buf, &kp);
198 }
199
200 break;
201 }
202
203 return ret ? ret : count;
204}
205
206/*
207 * Add sysfs entries for one cpu.
208 */
209static int __init msm_pm_mode_sysfs_add_cpu(
210 unsigned int cpu, struct kobject *modes_kobj)
211{
212 char cpu_name[8];
213 struct kobject *cpu_kobj;
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600214 struct msm_pm_sysfs_sleep_mode *mode = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700215 int i, j, k;
216 int ret;
217
218 snprintf(cpu_name, sizeof(cpu_name), "cpu%u", cpu);
219 cpu_kobj = kobject_create_and_add(cpu_name, modes_kobj);
220 if (!cpu_kobj) {
221 pr_err("%s: cannot create %s kobject\n", __func__, cpu_name);
222 ret = -ENOMEM;
223 goto mode_sysfs_add_cpu_exit;
224 }
225
226 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
227 int idx = MSM_PM_MODE(cpu, i);
228
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600229 if ((!msm_pm_sleep_modes[idx].suspend_supported)
230 && (!msm_pm_sleep_modes[idx].idle_supported))
231 continue;
232
233 if (!msm_pm_sleep_mode_labels[i] ||
234 !msm_pm_sleep_mode_labels[i][0])
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700235 continue;
236
237 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
238 if (!mode) {
239 pr_err("%s: cannot allocate memory for attributes\n",
240 __func__);
241 ret = -ENOMEM;
242 goto mode_sysfs_add_cpu_exit;
243 }
244
245 mode->kobj = kobject_create_and_add(
246 msm_pm_sleep_mode_labels[i], cpu_kobj);
247 if (!mode->kobj) {
248 pr_err("%s: cannot create kobject\n", __func__);
249 ret = -ENOMEM;
250 goto mode_sysfs_add_cpu_exit;
251 }
252
253 for (k = 0, j = 0; k < MSM_PM_MODE_ATTR_NR; k++) {
254 if ((k == MSM_PM_MODE_ATTR_IDLE) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600255 !msm_pm_sleep_modes[idx].idle_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700256 continue;
257 if ((k == MSM_PM_MODE_ATTR_SUSPEND) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600258 !msm_pm_sleep_modes[idx].suspend_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700259 continue;
Stephen Boydd2059c32012-07-03 15:11:15 -0700260 sysfs_attr_init(&mode->kas[j].ka.attr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700261 mode->kas[j].cpu = cpu;
262 mode->kas[j].ka.attr.mode = 0644;
263 mode->kas[j].ka.show = msm_pm_mode_attr_show;
264 mode->kas[j].ka.store = msm_pm_mode_attr_store;
265 mode->kas[j].ka.attr.name = msm_pm_mode_attr_labels[k];
266 mode->attrs[j] = &mode->kas[j].ka.attr;
267 j++;
268 }
269 mode->attrs[j] = NULL;
270
271 mode->attr_group.attrs = mode->attrs;
272 ret = sysfs_create_group(mode->kobj, &mode->attr_group);
273 if (ret) {
274 pr_err("%s: cannot create kobject attribute group\n",
275 __func__);
276 goto mode_sysfs_add_cpu_exit;
277 }
278 }
279
280 ret = 0;
281
282mode_sysfs_add_cpu_exit:
Praveen Chidambaramd5ac2d32011-10-24 14:30:27 -0600283 if (ret) {
Praveen Chidambaram2cfda632011-10-11 16:58:09 -0600284 if (mode && mode->kobj)
285 kobject_del(mode->kobj);
286 kfree(mode);
287 }
288
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700289 return ret;
290}
291
292/*
293 * Add sysfs entries for the sleep modes.
294 */
295static int __init msm_pm_mode_sysfs_add(void)
296{
297 struct kobject *module_kobj;
298 struct kobject *modes_kobj;
299 unsigned int cpu;
300 int ret;
301
302 module_kobj = kset_find_obj(module_kset, KBUILD_MODNAME);
303 if (!module_kobj) {
304 pr_err("%s: cannot find kobject for module %s\n",
305 __func__, KBUILD_MODNAME);
306 ret = -ENOENT;
307 goto mode_sysfs_add_exit;
308 }
309
310 modes_kobj = kobject_create_and_add("modes", module_kobj);
311 if (!modes_kobj) {
312 pr_err("%s: cannot create modes kobject\n", __func__);
313 ret = -ENOMEM;
314 goto mode_sysfs_add_exit;
315 }
316
317 for_each_possible_cpu(cpu) {
318 ret = msm_pm_mode_sysfs_add_cpu(cpu, modes_kobj);
319 if (ret)
320 goto mode_sysfs_add_exit;
321 }
322
323 ret = 0;
324
325mode_sysfs_add_exit:
326 return ret;
327}
328
329/******************************************************************************
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700330 * Configure Hardware before/after Low Power Mode
331 *****************************************************************************/
332
333/*
334 * Configure hardware registers in preparation for Apps power down.
335 */
336static void msm_pm_config_hw_before_power_down(void)
337{
338 return;
339}
340
341/*
342 * Clear hardware registers after Apps powers up.
343 */
344static void msm_pm_config_hw_after_power_up(void)
345{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700346}
347
348/*
349 * Configure hardware registers in preparation for SWFI.
350 */
351static void msm_pm_config_hw_before_swfi(void)
352{
353 return;
354}
355
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600356/*
357 * Configure/Restore hardware registers in preparation for Retention.
358 */
359
360static void msm_pm_config_hw_after_retention(void)
361{
362 int ret;
363 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
364 WARN_ON(ret);
365}
366
367static void msm_pm_config_hw_before_retention(void)
368{
369 return;
370}
371
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700372
373/******************************************************************************
374 * Suspend Max Sleep Time
375 *****************************************************************************/
376
377#ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE
378static int msm_pm_sleep_time_override;
379module_param_named(sleep_time_override,
380 msm_pm_sleep_time_override, int, S_IRUGO | S_IWUSR | S_IWGRP);
381#endif
382
383#define SCLK_HZ (32768)
384#define MSM_PM_SLEEP_TICK_LIMIT (0x6DDD000)
385
386static uint32_t msm_pm_max_sleep_time;
387
388/*
389 * Convert time from nanoseconds to slow clock ticks, then cap it to the
390 * specified limit
391 */
392static int64_t msm_pm_convert_and_cap_time(int64_t time_ns, int64_t limit)
393{
394 do_div(time_ns, NSEC_PER_SEC / SCLK_HZ);
395 return (time_ns > limit) ? limit : time_ns;
396}
397
398/*
399 * Set the sleep time for suspend. 0 means infinite sleep time.
400 */
401void msm_pm_set_max_sleep_time(int64_t max_sleep_time_ns)
402{
403 if (max_sleep_time_ns == 0) {
404 msm_pm_max_sleep_time = 0;
405 } else {
406 msm_pm_max_sleep_time = (uint32_t)msm_pm_convert_and_cap_time(
407 max_sleep_time_ns, MSM_PM_SLEEP_TICK_LIMIT);
408
409 if (msm_pm_max_sleep_time == 0)
410 msm_pm_max_sleep_time = 1;
411 }
412
413 if (msm_pm_debug_mask & MSM_PM_DEBUG_SUSPEND)
414 pr_info("%s: Requested %lld ns Giving %u sclk ticks\n",
415 __func__, max_sleep_time_ns, msm_pm_max_sleep_time);
416}
417EXPORT_SYMBOL(msm_pm_set_max_sleep_time);
418
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600419struct reg_data {
420 uint32_t reg;
421 uint32_t val;
422};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700423
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600424static struct reg_data reg_saved_state[] = {
425 { .reg = 0x4501, },
426 { .reg = 0x5501, },
427 { .reg = 0x6501, },
428 { .reg = 0x7501, },
429 { .reg = 0x0500, },
430};
431
432static unsigned int active_vdd;
433static bool msm_pm_save_cp15;
434static const unsigned int pc_vdd = 0x98;
435
436static void msm_pm_save_cpu_reg(void)
437{
438 int i;
439
440 /* Only on core0 */
441 if (smp_processor_id())
442 return;
443
444 /**
445 * On some targets, L2 PC will turn off may reset the core
446 * configuration for the mux and the default may not make the core
447 * happy when it resumes.
448 * Save the active vdd, and set the core vdd to QSB max vdd, so that
449 * when the core resumes, it is capable of supporting the current QSB
450 * rate. Then restore the active vdd before switching the acpuclk rate.
451 */
452 if (msm_pm_get_l2_flush_flag() == 1) {
453 active_vdd = msm_spm_get_vdd(0);
454 for (i = 0; i < ARRAY_SIZE(reg_saved_state); i++)
455 reg_saved_state[i].val =
456 get_l2_indirect_reg(reg_saved_state[i].reg);
457 msm_spm_set_vdd(0, pc_vdd);
458 }
459}
460
461static void msm_pm_restore_cpu_reg(void)
462{
463 int i;
464
465 /* Only on core0 */
466 if (smp_processor_id())
467 return;
468
469 if (msm_pm_get_l2_flush_flag() == 1) {
470 for (i = 0; i < ARRAY_SIZE(reg_saved_state); i++)
471 set_l2_indirect_reg(reg_saved_state[i].reg,
472 reg_saved_state[i].val);
473 msm_spm_set_vdd(0, active_vdd);
474 }
475}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700476
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600477static void *msm_pm_idle_rs_limits;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700478
479static void msm_pm_swfi(void)
480{
481 msm_pm_config_hw_before_swfi();
482 msm_arch_idle();
483}
484
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600485
486static void msm_pm_retention(void)
487{
488 int ret = 0;
489
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600490 msm_pm_config_hw_before_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600491 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_POWER_RETENTION, false);
492 WARN_ON(ret);
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600493
494 if (msm_pm_retention_tz_call)
495 scm_call_atomic1(SCM_SVC_BOOT, SCM_CMD_TERMINATE_PC,
496 SCM_L2_RETENTION);
497 else
498 msm_arch_idle();
499
500 msm_pm_config_hw_after_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600501}
502
Stephen Boydb29750d2012-02-21 01:21:32 -0800503static bool __ref msm_pm_spm_power_collapse(
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700504 unsigned int cpu, bool from_idle, bool notify_rpm)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700505{
506 void *entry;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600507 bool collapsed = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700508 int ret;
Rohit Vaswanie78dfb62012-02-21 10:29:29 -0800509 unsigned int saved_gic_cpu_ctrl;
510
511 saved_gic_cpu_ctrl = readl_relaxed(MSM_QGIC_CPU_BASE + GIC_CPU_CTRL);
512 mb();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700513
514 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
515 pr_info("CPU%u: %s: notify_rpm %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700516 cpu, __func__, (int) notify_rpm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700517
518 ret = msm_spm_set_low_power_mode(
519 MSM_SPM_MODE_POWER_COLLAPSE, notify_rpm);
520 WARN_ON(ret);
521
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700522 entry = (!cpu || from_idle) ?
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700523 msm_pm_collapse_exit : msm_secondary_startup;
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700524 msm_pm_boot_config_before_pc(cpu, virt_to_phys(entry));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700525
526 if (MSM_PM_DEBUG_RESET_VECTOR & msm_pm_debug_mask)
527 pr_info("CPU%u: %s: program vector to %p\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700528 cpu, __func__, entry);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700529
530#ifdef CONFIG_VFP
Steve Mucklef132c6c2012-06-06 18:30:57 -0700531 vfp_pm_suspend();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700532#endif
Girish Mahadevan55944992012-10-26 11:03:07 -0600533 collapsed = msm_pm_collapse();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700534
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700535 msm_pm_boot_config_after_pc(cpu);
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -0600536
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700537 if (collapsed) {
538#ifdef CONFIG_VFP
Steve Mucklef132c6c2012-06-06 18:30:57 -0700539 vfp_pm_resume();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700540#endif
541 cpu_init();
542 writel(0xF0, MSM_QGIC_CPU_BASE + GIC_CPU_PRIMASK);
Rohit Vaswanie78dfb62012-02-21 10:29:29 -0800543 writel_relaxed(saved_gic_cpu_ctrl,
544 MSM_QGIC_CPU_BASE + GIC_CPU_CTRL);
545 mb();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700546 local_fiq_enable();
547 }
548
549 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
550 pr_info("CPU%u: %s: msm_pm_collapse returned, collapsed %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700551 cpu, __func__, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700552
553 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
554 WARN_ON(ret);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600555 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700556}
557
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600558static bool msm_pm_power_collapse_standalone(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700559{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700560 unsigned int cpu = smp_processor_id();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700561 unsigned int avsdscr_setting;
Mahesh Sivasubramanianad138da2012-10-24 13:53:48 -0600562 unsigned int avscsr_enable;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600563 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700564
565 avsdscr_setting = avs_get_avsdscr();
Mahesh Sivasubramanianad138da2012-10-24 13:53:48 -0600566 avscsr_enable = avs_disable();
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700567 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, false);
Mahesh Sivasubramanianad138da2012-10-24 13:53:48 -0600568 avs_enable(avscsr_enable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700569 avs_reset_delays(avsdscr_setting);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600570 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700571}
572
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600573static bool msm_pm_power_collapse(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700574{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700575 unsigned int cpu = smp_processor_id();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700576 unsigned long saved_acpuclk_rate;
577 unsigned int avsdscr_setting;
Mahesh Sivasubramanianad138da2012-10-24 13:53:48 -0600578 unsigned int avscsr_enable;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600579 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700580
581 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
582 pr_info("CPU%u: %s: idle %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700583 cpu, __func__, (int)from_idle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700584
585 msm_pm_config_hw_before_power_down();
586 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700587 pr_info("CPU%u: %s: pre power down\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700588
589 avsdscr_setting = avs_get_avsdscr();
Mahesh Sivasubramanianad138da2012-10-24 13:53:48 -0600590 avscsr_enable = avs_disable();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700591
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700592 if (cpu_online(cpu))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700593 saved_acpuclk_rate = acpuclk_power_collapse();
594 else
595 saved_acpuclk_rate = 0;
596
597 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
598 pr_info("CPU%u: %s: change clock rate (old rate = %lu)\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700599 cpu, __func__, saved_acpuclk_rate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700600
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600601 if (msm_pm_save_cp15)
602 msm_pm_save_cpu_reg();
603
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700604 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700605
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600606 if (msm_pm_save_cp15)
607 msm_pm_restore_cpu_reg();
608
Girish Mahadevan4e025d92012-02-29 13:26:51 -0700609 if (cpu_online(cpu)) {
610 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
611 pr_info("CPU%u: %s: restore clock rate to %lu\n",
612 cpu, __func__, saved_acpuclk_rate);
613 if (acpuclk_set_rate(cpu, saved_acpuclk_rate, SETRATE_PC) < 0)
614 pr_err("CPU%u: %s: failed to restore clock rate(%lu)\n",
615 cpu, __func__, saved_acpuclk_rate);
616 } else {
617 unsigned int gic_dist_enabled;
618 unsigned int gic_dist_pending;
619 gic_dist_enabled = readl_relaxed(
620 MSM_QGIC_DIST_BASE + GIC_DIST_ENABLE_CLEAR);
621 gic_dist_pending = readl_relaxed(
622 MSM_QGIC_DIST_BASE + GIC_DIST_PENDING_SET);
623 mb();
624 gic_dist_pending &= gic_dist_enabled;
625
626 if (gic_dist_pending)
627 pr_err("CPU %d interrupted during hotplug.Pending int 0x%x\n",
628 cpu, gic_dist_pending);
629 }
630
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700631
Mahesh Sivasubramanianad138da2012-10-24 13:53:48 -0600632 avs_enable(avscsr_enable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700633 avs_reset_delays(avsdscr_setting);
634 msm_pm_config_hw_after_power_up();
635 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700636 pr_info("CPU%u: %s: post power up\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700637
638 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700639 pr_info("CPU%u: %s: return\n", cpu, __func__);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600640 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700641}
642
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600643static void msm_pm_target_init(void)
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600644{
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600645 if (cpu_is_apq8064())
646 msm_pm_save_cp15 = true;
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600647}
648
649static int64_t msm_pm_timer_enter_idle(void)
650{
Girish Mahadevan55944992012-10-26 11:03:07 -0600651 if (msm_pm_init_data.use_sync_timer)
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600652 return ktime_to_ns(tick_nohz_get_sleep_length());
653
654 return msm_timer_enter_idle();
655}
656
657static void msm_pm_timer_exit_idle(bool timer_halted)
658{
Girish Mahadevan55944992012-10-26 11:03:07 -0600659 if (msm_pm_init_data.use_sync_timer)
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600660 return;
661
662 msm_timer_exit_idle((int) timer_halted);
663}
664
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530665static int64_t msm_pm_timer_enter_suspend(int64_t *period)
666{
Anji Jonnalac02367a2012-07-01 02:56:11 +0530667 int64_t time = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530668
Girish Mahadevan55944992012-10-26 11:03:07 -0600669 if (msm_pm_init_data.use_sync_timer)
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530670 return sched_clock();
671
672 time = msm_timer_get_sclk_time(period);
673 if (!time)
674 pr_err("%s: Unable to read sclk.\n", __func__);
675
676 return time;
677}
678
679static int64_t msm_pm_timer_exit_suspend(int64_t time, int64_t period)
680{
Girish Mahadevan55944992012-10-26 11:03:07 -0600681 if (msm_pm_init_data.use_sync_timer)
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530682 return sched_clock() - time;
683
684 if (time != 0) {
685 int64_t end_time = msm_timer_get_sclk_time(NULL);
686 if (end_time != 0) {
687 time = end_time - time;
688 if (time < 0)
689 time += period;
690 } else
691 time = 0;
692 }
693
694 return time;
695}
696
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600697/**
698 * pm_hrtimer_cb() : Callback function for hrtimer created if the
699 * core needs to be awake to handle an event.
700 * @hrtimer : Pointer to hrtimer
701 */
702static enum hrtimer_restart pm_hrtimer_cb(struct hrtimer *hrtimer)
703{
704 return HRTIMER_NORESTART;
705}
706
707/**
708 * msm_pm_set_timer() : Set an hrtimer to wakeup the core in time
709 * to handle an event.
710 */
711static void msm_pm_set_timer(uint32_t modified_time_us)
712{
713 u64 modified_time_ns = modified_time_us * NSEC_PER_USEC;
714 ktime_t modified_ktime = ns_to_ktime(modified_time_ns);
715 pm_hrtimer.function = pm_hrtimer_cb;
716 hrtimer_start(&pm_hrtimer, modified_ktime, HRTIMER_MODE_ABS);
717}
718
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700719/******************************************************************************
720 * External Idle/Suspend Functions
721 *****************************************************************************/
722
723void arch_idle(void)
724{
725 return;
726}
727
Priyanka Mathur26b4a4b2012-11-05 13:45:45 -0800728static inline void msm_pm_ftrace_lpm_enter(unsigned int cpu,
729 uint32_t latency, uint32_t sleep_us,
730 uint32_t wake_up,
731 enum msm_pm_sleep_mode mode)
732{
733 switch (mode) {
734 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
735 trace_msm_pm_enter_wfi(cpu, latency, sleep_us, wake_up);
736 break;
737 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
738 trace_msm_pm_enter_spc(cpu, latency, sleep_us, wake_up);
739 break;
740 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
741 trace_msm_pm_enter_pc(cpu, latency, sleep_us, wake_up);
742 break;
743 case MSM_PM_SLEEP_MODE_RETENTION:
744 trace_msm_pm_enter_ret(cpu, latency, sleep_us, wake_up);
745 break;
746 default:
747 break;
748 }
749}
750
751static inline void msm_pm_ftrace_lpm_exit(unsigned int cpu,
752 enum msm_pm_sleep_mode mode,
753 bool success)
754{
755 switch (mode) {
756 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
757 trace_msm_pm_exit_wfi(cpu, success);
758 break;
759 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
760 trace_msm_pm_exit_spc(cpu, success);
761 break;
762 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
763 trace_msm_pm_exit_pc(cpu, success);
764 break;
765 case MSM_PM_SLEEP_MODE_RETENTION:
766 trace_msm_pm_exit_ret(cpu, success);
767 break;
768 default:
769 break;
770 }
771}
772
Steve Mucklef132c6c2012-06-06 18:30:57 -0700773int msm_pm_idle_prepare(struct cpuidle_device *dev,
774 struct cpuidle_driver *drv, int index)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700775{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700776 int i;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700777 unsigned int power_usage = -1;
778 int ret = 0;
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600779 uint32_t modified_time_us = 0;
780 struct msm_pm_time_params time_param;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700781
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600782 time_param.latency_us =
783 (uint32_t) pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
784 time_param.sleep_us =
785 (uint32_t) (ktime_to_us(tick_nohz_get_sleep_length())
786 & UINT_MAX);
787 time_param.modified_time_us = 0;
788
789 if (!dev->cpu)
790 time_param.next_event_us =
791 (uint32_t) (ktime_to_us(get_next_event_time())
792 & UINT_MAX);
793 else
794 time_param.next_event_us = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700795
796 for (i = 0; i < dev->state_count; i++) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700797 struct cpuidle_state *state = &drv->states[i];
798 struct cpuidle_state_usage *st_usage = &dev->states_usage[i];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700799 enum msm_pm_sleep_mode mode;
800 bool allow;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600801 void *rs_limits = NULL;
802 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700803 int idx;
804
Steve Mucklef132c6c2012-06-06 18:30:57 -0700805 mode = (enum msm_pm_sleep_mode) cpuidle_get_statedata(st_usage);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700806 idx = MSM_PM_MODE(dev->cpu, mode);
807
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600808 allow = msm_pm_sleep_modes[idx].idle_enabled &&
809 msm_pm_sleep_modes[idx].idle_supported;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700810
811 switch (mode) {
812 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
Priyanka Mathur68a9bc52012-10-03 15:15:01 -0700813 if (num_online_cpus() > 1) {
814 allow = false;
815 break;
816 }
817 /* fall through */
Mahesh Sivasubramanianba7e01c2012-09-04 18:22:51 -0600818 case MSM_PM_SLEEP_MODE_RETENTION:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700819 if (!allow)
820 break;
821
Priyanka Mathur68a9bc52012-10-03 15:15:01 -0700822 if (msm_pm_retention_tz_call &&
823 num_online_cpus() > 1) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700824 allow = false;
825 break;
826 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700827 /* fall through */
828
829 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
830 if (!allow)
831 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700832 /* fall through */
833
834 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
835 if (!allow)
836 break;
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600837 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700838
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600839 if (pm_sleep_ops.lowest_limits)
840 rs_limits = pm_sleep_ops.lowest_limits(true,
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600841 mode, &time_param, &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700842
843 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
844 pr_info("CPU%u: %s: %s, latency %uus, "
845 "sleep %uus, limit %p\n",
846 dev->cpu, __func__, state->desc,
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600847 time_param.latency_us,
848 time_param.sleep_us, rs_limits);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700849
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700850 if (!rs_limits)
851 allow = false;
852 break;
853
854 default:
855 allow = false;
856 break;
857 }
858
859 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
860 pr_info("CPU%u: %s: allow %s: %d\n",
861 dev->cpu, __func__, state->desc, (int)allow);
862
863 if (allow) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700864 if (power < power_usage) {
865 power_usage = power;
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600866 modified_time_us = time_param.modified_time_us;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700867 ret = mode;
868 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700869
870 if (MSM_PM_SLEEP_MODE_POWER_COLLAPSE == mode)
871 msm_pm_idle_rs_limits = rs_limits;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700872 }
873 }
874
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600875 if (modified_time_us && !dev->cpu)
876 msm_pm_set_timer(modified_time_us);
Priyanka Mathur26b4a4b2012-11-05 13:45:45 -0800877
878 msm_pm_ftrace_lpm_enter(dev->cpu, time_param.latency_us,
879 time_param.sleep_us, time_param.next_event_us,
880 ret);
881
Steve Mucklef132c6c2012-06-06 18:30:57 -0700882 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700883}
884
885int msm_pm_idle_enter(enum msm_pm_sleep_mode sleep_mode)
886{
887 int64_t time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700888 int exit_stat;
Priyanka Mathur26b4a4b2012-11-05 13:45:45 -0800889 bool collapsed = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700890
891 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
892 pr_info("CPU%u: %s: mode %d\n",
893 smp_processor_id(), __func__, sleep_mode);
894
895 time = ktime_to_ns(ktime_get());
896
897 switch (sleep_mode) {
898 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
899 msm_pm_swfi();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700900 exit_stat = MSM_PM_STAT_IDLE_WFI;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700901 break;
902
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600903 case MSM_PM_SLEEP_MODE_RETENTION:
904 msm_pm_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600905 exit_stat = MSM_PM_STAT_RETENTION;
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600906 break;
907
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700908 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
Priyanka Mathur26b4a4b2012-11-05 13:45:45 -0800909 collapsed = msm_pm_power_collapse_standalone(true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700910 exit_stat = MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700911 break;
912
913 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE: {
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600914 int64_t timer_expiration = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700915 bool timer_halted = false;
916 uint32_t sleep_delay;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600917 int ret = -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700918 int notify_rpm =
919 (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE);
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600920 timer_expiration = msm_pm_timer_enter_idle();
921
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700922 sleep_delay = (uint32_t) msm_pm_convert_and_cap_time(
923 timer_expiration, MSM_PM_SLEEP_TICK_LIMIT);
924 if (sleep_delay == 0) /* 0 would mean infinite time */
925 sleep_delay = 1;
926
Karthik Parsha6fb932d2012-01-24 18:04:12 -0800927 if (MSM_PM_DEBUG_IDLE_CLK & msm_pm_debug_mask)
928 clock_debug_print_enabled();
929
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600930 if (pm_sleep_ops.enter_sleep)
931 ret = pm_sleep_ops.enter_sleep(sleep_delay,
932 msm_pm_idle_rs_limits,
933 true, notify_rpm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700934 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600935 collapsed = msm_pm_power_collapse(true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700936 timer_halted = true;
937
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600938 if (pm_sleep_ops.exit_sleep)
939 pm_sleep_ops.exit_sleep(msm_pm_idle_rs_limits,
940 true, notify_rpm, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700941 }
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600942 msm_pm_timer_exit_idle(timer_halted);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700943 exit_stat = MSM_PM_STAT_IDLE_POWER_COLLAPSE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700944 break;
945 }
946
947 default:
948 __WARN();
949 goto cpuidle_enter_bail;
950 }
951
952 time = ktime_to_ns(ktime_get()) - time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700953 msm_pm_add_stat(exit_stat, time);
Priyanka Mathur26b4a4b2012-11-05 13:45:45 -0800954 msm_pm_ftrace_lpm_exit(smp_processor_id(), sleep_mode,
955 collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700956
957 do_div(time, 1000);
958 return (int) time;
959
960cpuidle_enter_bail:
961 return 0;
962}
963
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700964void msm_pm_cpu_enter_lowpower(unsigned int cpu)
965{
966 int i;
967 bool allow[MSM_PM_SLEEP_MODE_NR];
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(cpu, i)];
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700973 allow[i] = mode->suspend_supported && mode->suspend_enabled;
974 }
975
976 if (MSM_PM_DEBUG_HOTPLUG & msm_pm_debug_mask)
977 pr_notice("CPU%u: %s: shutting down cpu\n", cpu, __func__);
978
Matt Wagantall5375aa72012-07-02 19:59:51 -0700979 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700980 msm_pm_power_collapse(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -0700981 else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700982 msm_pm_power_collapse_standalone(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -0700983 else if (allow[MSM_PM_SLEEP_MODE_RETENTION])
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600984 msm_pm_retention();
Stephen Boydbda74272012-08-09 14:01:27 -0700985 else
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700986 msm_pm_swfi();
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700987}
988
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700989static int msm_pm_enter(suspend_state_t state)
990{
991 bool allow[MSM_PM_SLEEP_MODE_NR];
992 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700993 int64_t period = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530994 int64_t time = msm_pm_timer_enter_suspend(&period);
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600995 struct msm_pm_time_params time_param;
996
997 time_param.latency_us = -1;
998 time_param.sleep_us = -1;
999 time_param.next_event_us = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001000
1001 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1002 pr_info("%s\n", __func__);
1003
1004 if (smp_processor_id()) {
1005 __WARN();
1006 goto enter_exit;
1007 }
1008
1009
1010 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
1011 struct msm_pm_platform_data *mode;
1012
Praveen Chidambaram42da9d22012-03-30 12:16:34 -06001013 mode = &msm_pm_sleep_modes[MSM_PM_MODE(0, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001014 allow[i] = mode->suspend_supported && mode->suspend_enabled;
1015 }
1016
1017 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001018 void *rs_limits = NULL;
1019 int ret = -ENODEV;
1020 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001021
1022 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1023 pr_info("%s: power collapse\n", __func__);
1024
1025 clock_debug_print_enabled();
1026
1027#ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE
1028 if (msm_pm_sleep_time_override > 0) {
1029 int64_t ns = NSEC_PER_SEC *
1030 (int64_t) msm_pm_sleep_time_override;
1031 msm_pm_set_max_sleep_time(ns);
1032 msm_pm_sleep_time_override = 0;
1033 }
1034#endif /* CONFIG_MSM_SLEEP_TIME_OVERRIDE */
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001035 if (pm_sleep_ops.lowest_limits)
1036 rs_limits = pm_sleep_ops.lowest_limits(false,
Girish Mahadevandc318fd2012-08-17 16:48:05 -06001037 MSM_PM_SLEEP_MODE_POWER_COLLAPSE, &time_param, &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001038
1039 if (rs_limits) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001040 if (pm_sleep_ops.enter_sleep)
1041 ret = pm_sleep_ops.enter_sleep(
1042 msm_pm_max_sleep_time,
1043 rs_limits, false, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001044 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -06001045 int collapsed = msm_pm_power_collapse(false);
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001046 if (pm_sleep_ops.exit_sleep) {
1047 pm_sleep_ops.exit_sleep(rs_limits,
1048 false, true, collapsed);
1049 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001050 }
1051 } else {
1052 pr_err("%s: cannot find the lowest power limit\n",
1053 __func__);
1054 }
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301055 time = msm_pm_timer_exit_suspend(time, period);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001056 msm_pm_add_stat(MSM_PM_STAT_SUSPEND, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001057 } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
1058 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1059 pr_info("%s: standalone power collapse\n", __func__);
1060 msm_pm_power_collapse_standalone(false);
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -06001061 } else if (allow[MSM_PM_SLEEP_MODE_RETENTION]) {
1062 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1063 pr_info("%s: retention\n", __func__);
1064 msm_pm_retention();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001065 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
1066 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1067 pr_info("%s: swfi\n", __func__);
1068 msm_pm_swfi();
1069 }
1070
1071
1072enter_exit:
1073 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1074 pr_info("%s: return\n", __func__);
1075
1076 return 0;
1077}
1078
1079static struct platform_suspend_ops msm_pm_ops = {
1080 .enter = msm_pm_enter,
1081 .valid = suspend_valid_only_mem,
1082};
1083
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001084/******************************************************************************
1085 * Initialization routine
1086 *****************************************************************************/
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001087void msm_pm_set_sleep_ops(struct msm_pm_sleep_ops *ops)
1088{
1089 if (ops)
1090 pm_sleep_ops = *ops;
1091}
1092
Girish Mahadevand27ca4a2012-08-15 09:21:23 -06001093void __init msm_pm_set_tz_retention_flag(unsigned int flag)
1094{
1095 msm_pm_retention_tz_call = flag;
1096}
1097
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -06001098static int __devinit msm_pc_debug_probe(struct platform_device *pdev)
1099{
1100 struct resource *res = NULL;
1101 int i ;
1102
1103 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1104 if (!res)
1105 goto fail;
1106
1107 msm_pc_debug_counters_phys = res->start;
1108 WARN_ON(resource_size(res) < SZ_64);
Mahesh Sivasubramaniane6d33992012-10-17 13:39:40 -06001109 msm_pc_debug_counters = devm_ioremap_nocache(&pdev->dev, res->start,
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -06001110 resource_size(res));
1111
1112 if (!msm_pc_debug_counters)
1113 goto fail;
1114
1115 for (i = 0; i < resource_size(res)/4; i++)
1116 __raw_writel(0, msm_pc_debug_counters + i * 4);
1117 return 0;
1118fail:
1119 msm_pc_debug_counters = 0;
1120 msm_pc_debug_counters_phys = 0;
1121 return -EFAULT;
1122}
1123
1124static struct of_device_id msm_pc_debug_table[] = {
1125 {.compatible = "qcom,pc-cntr"},
1126 {},
1127};
1128
1129static struct platform_driver msm_pc_counter_driver = {
1130 .probe = msm_pc_debug_probe,
1131 .driver = {
1132 .name = "pc-cntr",
1133 .owner = THIS_MODULE,
1134 .of_match_table = msm_pc_debug_table,
1135 },
1136};
1137
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001138static int __init msm_pm_init(void)
1139{
1140 pgd_t *pc_pgd;
1141 pmd_t *pmd;
1142 unsigned long pmdval;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301143 enum msm_pm_time_stats_id enable_stats[] = {
1144 MSM_PM_STAT_IDLE_WFI,
1145 MSM_PM_STAT_RETENTION,
1146 MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE,
1147 MSM_PM_STAT_IDLE_POWER_COLLAPSE,
1148 MSM_PM_STAT_SUSPEND,
1149 };
Steve Mucklef132c6c2012-06-06 18:30:57 -07001150 unsigned long exit_phys;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301151
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001152 /* Page table for cores to come back up safely. */
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -06001153
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001154 pc_pgd = pgd_alloc(&init_mm);
1155 if (!pc_pgd)
1156 return -ENOMEM;
1157
Steve Mucklef132c6c2012-06-06 18:30:57 -07001158 exit_phys = virt_to_phys(msm_pm_collapse_exit);
1159
1160 pmd = pmd_offset(pud_offset(pc_pgd + pgd_index(exit_phys),exit_phys),
1161 exit_phys);
1162 pmdval = (exit_phys & PGDIR_MASK) |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001163 PMD_TYPE_SECT | PMD_SECT_AP_WRITE;
1164 pmd[0] = __pmd(pmdval);
1165 pmd[1] = __pmd(pmdval + (1 << (PGDIR_SHIFT - 1)));
1166
Steve Mucklefcece052012-02-18 20:09:58 -08001167 msm_saved_state_phys =
1168 allocate_contiguous_ebi_nomap(CPU_SAVED_STATE_SIZE *
1169 num_possible_cpus(), 4);
1170 if (!msm_saved_state_phys)
1171 return -ENOMEM;
1172 msm_saved_state = ioremap_nocache(msm_saved_state_phys,
1173 CPU_SAVED_STATE_SIZE *
1174 num_possible_cpus());
1175 if (!msm_saved_state)
1176 return -ENOMEM;
1177
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001178 /* It is remotely possible that the code in msm_pm_collapse_exit()
1179 * which turns on the MMU with this mapping is in the
1180 * next even-numbered megabyte beyond the
1181 * start of msm_pm_collapse_exit().
1182 * Map this megabyte in as well.
1183 */
1184 pmd[2] = __pmd(pmdval + (2 << (PGDIR_SHIFT - 1)));
1185 flush_pmd_entry(pmd);
1186 msm_pm_pc_pgd = virt_to_phys(pc_pgd);
Steve Muckle730ad7a2012-02-21 15:26:37 -08001187 clean_caches((unsigned long)&msm_pm_pc_pgd, sizeof(msm_pm_pc_pgd),
1188 virt_to_phys(&msm_pm_pc_pgd));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001189
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001190 msm_pm_mode_sysfs_add();
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301191 msm_pm_add_stats(enable_stats, ARRAY_SIZE(enable_stats));
1192
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001193 suspend_set_ops(&msm_pm_ops);
Praveen Chidambaramc594a092012-09-18 19:48:29 -06001194 msm_pm_target_init();
Girish Mahadevandc318fd2012-08-17 16:48:05 -06001195 hrtimer_init(&pm_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001196 msm_cpuidle_init();
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -06001197 platform_driver_register(&msm_pc_counter_driver);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001198
1199 return 0;
1200}
1201
1202late_initcall(msm_pm_init);
Girish Mahadevan55944992012-10-26 11:03:07 -06001203
1204static void __devinit msm_pm_set_flush_fn(uint32_t pc_mode)
1205{
1206 msm_pm_disable_l2_fn = NULL;
1207 msm_pm_enable_l2_fn = NULL;
1208 msm_pm_flush_l2_fn = outer_flush_all;
1209
1210 if (pc_mode == MSM_PM_PC_NOTZ_L2_EXT) {
1211 msm_pm_disable_l2_fn = outer_disable;
1212 msm_pm_enable_l2_fn = outer_resume;
1213 }
1214}
1215
1216static int __devinit msm_pm_8x60_probe(struct platform_device *pdev)
1217{
1218 char *key = NULL;
1219 uint32_t val = 0;
1220 int ret = 0;
1221
1222 if (!pdev->dev.of_node) {
1223 struct msm_pm_init_data_type *d = pdev->dev.platform_data;
1224
1225 if (!d)
1226 goto pm_8x60_probe_done;
1227
1228 msm_pm_init_data.pc_mode = d->pc_mode;
1229 msm_pm_set_flush_fn(msm_pm_init_data.pc_mode);
1230 msm_pm_init_data.use_sync_timer = d->use_sync_timer;
1231 } else {
1232 key = "qcom,pc-mode";
1233 ret = of_property_read_u32(pdev->dev.of_node, key, &val);
1234
1235 if (ret) {
1236 pr_debug("%s: Cannot read %s,defaulting to 0",
1237 __func__, key);
1238 val = MSM_PM_PC_TZ_L2_INT;
1239 ret = 0;
1240 }
1241
1242 msm_pm_init_data.pc_mode = val;
1243 msm_pm_set_flush_fn(msm_pm_init_data.pc_mode);
1244
1245 key = "qcom,use-sync-timer";
1246 msm_pm_init_data.use_sync_timer =
1247 of_property_read_bool(pdev->dev.of_node, key);
1248 }
1249
1250pm_8x60_probe_done:
1251 return ret;
1252}
1253
1254static struct of_device_id msm_pm_8x60_table[] = {
1255 {.compatible = "qcom,pm-8x60"},
1256 {},
1257};
1258
1259static struct platform_driver msm_pm_8x60_driver = {
1260 .probe = msm_pm_8x60_probe,
1261 .driver = {
1262 .name = "pm-8x60",
1263 .owner = THIS_MODULE,
1264 .of_match_table = msm_pm_8x60_table,
1265 },
1266};
1267
1268static int __init msm_pm_8x60_init(void)
1269{
1270 return platform_driver_register(&msm_pm_8x60_driver);
1271}
1272module_init(msm_pm_8x60_init);