blob: b42ad94750eeb62f900ffde5706e29a449518ed9 [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();
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700561 unsigned int avsdscr;
562 unsigned int avscsr;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600563 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700564
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700565 avsdscr = avs_get_avsdscr();
566 avscsr = avs_get_avscsr();
567 avs_set_avscsr(0); /* Disable AVS */
568
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700569 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, false);
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700570
571 avs_set_avsdscr(avsdscr);
572 avs_set_avscsr(avscsr);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600573 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700574}
575
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600576static bool msm_pm_power_collapse(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700577{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700578 unsigned int cpu = smp_processor_id();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700579 unsigned long saved_acpuclk_rate;
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700580 unsigned int avsdscr;
581 unsigned int avscsr;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600582 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700583
584 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
585 pr_info("CPU%u: %s: idle %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700586 cpu, __func__, (int)from_idle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700587
588 msm_pm_config_hw_before_power_down();
589 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700590 pr_info("CPU%u: %s: pre power down\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700591
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700592 avsdscr = avs_get_avsdscr();
593 avscsr = avs_get_avscsr();
594 avs_set_avscsr(0); /* Disable AVS */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700595
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700596 if (cpu_online(cpu))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700597 saved_acpuclk_rate = acpuclk_power_collapse();
598 else
599 saved_acpuclk_rate = 0;
600
601 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
602 pr_info("CPU%u: %s: change clock rate (old rate = %lu)\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700603 cpu, __func__, saved_acpuclk_rate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700604
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600605 if (msm_pm_save_cp15)
606 msm_pm_save_cpu_reg();
607
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700608 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700609
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600610 if (msm_pm_save_cp15)
611 msm_pm_restore_cpu_reg();
612
Girish Mahadevan4e025d92012-02-29 13:26:51 -0700613 if (cpu_online(cpu)) {
614 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
615 pr_info("CPU%u: %s: restore clock rate to %lu\n",
616 cpu, __func__, saved_acpuclk_rate);
617 if (acpuclk_set_rate(cpu, saved_acpuclk_rate, SETRATE_PC) < 0)
618 pr_err("CPU%u: %s: failed to restore clock rate(%lu)\n",
619 cpu, __func__, saved_acpuclk_rate);
620 } else {
621 unsigned int gic_dist_enabled;
622 unsigned int gic_dist_pending;
623 gic_dist_enabled = readl_relaxed(
624 MSM_QGIC_DIST_BASE + GIC_DIST_ENABLE_CLEAR);
625 gic_dist_pending = readl_relaxed(
626 MSM_QGIC_DIST_BASE + GIC_DIST_PENDING_SET);
627 mb();
628 gic_dist_pending &= gic_dist_enabled;
629
630 if (gic_dist_pending)
631 pr_err("CPU %d interrupted during hotplug.Pending int 0x%x\n",
632 cpu, gic_dist_pending);
633 }
634
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700635
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700636 avs_set_avsdscr(avsdscr);
637 avs_set_avscsr(avscsr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700638 msm_pm_config_hw_after_power_up();
639 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700640 pr_info("CPU%u: %s: post power up\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700641
642 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700643 pr_info("CPU%u: %s: return\n", cpu, __func__);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600644 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700645}
646
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600647static void msm_pm_target_init(void)
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600648{
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600649 if (cpu_is_apq8064())
650 msm_pm_save_cp15 = true;
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600651}
652
653static int64_t msm_pm_timer_enter_idle(void)
654{
Girish Mahadevan55944992012-10-26 11:03:07 -0600655 if (msm_pm_init_data.use_sync_timer)
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600656 return ktime_to_ns(tick_nohz_get_sleep_length());
657
658 return msm_timer_enter_idle();
659}
660
661static void msm_pm_timer_exit_idle(bool timer_halted)
662{
Girish Mahadevan55944992012-10-26 11:03:07 -0600663 if (msm_pm_init_data.use_sync_timer)
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600664 return;
665
666 msm_timer_exit_idle((int) timer_halted);
667}
668
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530669static int64_t msm_pm_timer_enter_suspend(int64_t *period)
670{
Anji Jonnalac02367a2012-07-01 02:56:11 +0530671 int64_t time = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530672
Girish Mahadevan55944992012-10-26 11:03:07 -0600673 if (msm_pm_init_data.use_sync_timer)
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530674 return sched_clock();
675
676 time = msm_timer_get_sclk_time(period);
677 if (!time)
678 pr_err("%s: Unable to read sclk.\n", __func__);
679
680 return time;
681}
682
683static int64_t msm_pm_timer_exit_suspend(int64_t time, int64_t period)
684{
Girish Mahadevan55944992012-10-26 11:03:07 -0600685 if (msm_pm_init_data.use_sync_timer)
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530686 return sched_clock() - time;
687
688 if (time != 0) {
689 int64_t end_time = msm_timer_get_sclk_time(NULL);
690 if (end_time != 0) {
691 time = end_time - time;
692 if (time < 0)
693 time += period;
694 } else
695 time = 0;
696 }
697
698 return time;
699}
700
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600701/**
702 * pm_hrtimer_cb() : Callback function for hrtimer created if the
703 * core needs to be awake to handle an event.
704 * @hrtimer : Pointer to hrtimer
705 */
706static enum hrtimer_restart pm_hrtimer_cb(struct hrtimer *hrtimer)
707{
708 return HRTIMER_NORESTART;
709}
710
711/**
712 * msm_pm_set_timer() : Set an hrtimer to wakeup the core in time
713 * to handle an event.
714 */
715static void msm_pm_set_timer(uint32_t modified_time_us)
716{
717 u64 modified_time_ns = modified_time_us * NSEC_PER_USEC;
718 ktime_t modified_ktime = ns_to_ktime(modified_time_ns);
719 pm_hrtimer.function = pm_hrtimer_cb;
720 hrtimer_start(&pm_hrtimer, modified_ktime, HRTIMER_MODE_ABS);
721}
722
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700723/******************************************************************************
724 * External Idle/Suspend Functions
725 *****************************************************************************/
726
727void arch_idle(void)
728{
729 return;
730}
731
Priyanka Mathur26b4a4b2012-11-05 13:45:45 -0800732static inline void msm_pm_ftrace_lpm_enter(unsigned int cpu,
733 uint32_t latency, uint32_t sleep_us,
734 uint32_t wake_up,
735 enum msm_pm_sleep_mode mode)
736{
737 switch (mode) {
738 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
739 trace_msm_pm_enter_wfi(cpu, latency, sleep_us, wake_up);
740 break;
741 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
742 trace_msm_pm_enter_spc(cpu, latency, sleep_us, wake_up);
743 break;
744 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
745 trace_msm_pm_enter_pc(cpu, latency, sleep_us, wake_up);
746 break;
747 case MSM_PM_SLEEP_MODE_RETENTION:
748 trace_msm_pm_enter_ret(cpu, latency, sleep_us, wake_up);
749 break;
750 default:
751 break;
752 }
753}
754
755static inline void msm_pm_ftrace_lpm_exit(unsigned int cpu,
756 enum msm_pm_sleep_mode mode,
757 bool success)
758{
759 switch (mode) {
760 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
761 trace_msm_pm_exit_wfi(cpu, success);
762 break;
763 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
764 trace_msm_pm_exit_spc(cpu, success);
765 break;
766 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
767 trace_msm_pm_exit_pc(cpu, success);
768 break;
769 case MSM_PM_SLEEP_MODE_RETENTION:
770 trace_msm_pm_exit_ret(cpu, success);
771 break;
772 default:
773 break;
774 }
775}
776
Steve Mucklef132c6c2012-06-06 18:30:57 -0700777int msm_pm_idle_prepare(struct cpuidle_device *dev,
778 struct cpuidle_driver *drv, int index)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700779{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700780 int i;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700781 unsigned int power_usage = -1;
782 int ret = 0;
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600783 uint32_t modified_time_us = 0;
784 struct msm_pm_time_params time_param;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700785
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600786 time_param.latency_us =
787 (uint32_t) pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
788 time_param.sleep_us =
789 (uint32_t) (ktime_to_us(tick_nohz_get_sleep_length())
790 & UINT_MAX);
791 time_param.modified_time_us = 0;
792
793 if (!dev->cpu)
794 time_param.next_event_us =
795 (uint32_t) (ktime_to_us(get_next_event_time())
796 & UINT_MAX);
797 else
798 time_param.next_event_us = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700799
800 for (i = 0; i < dev->state_count; i++) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700801 struct cpuidle_state *state = &drv->states[i];
802 struct cpuidle_state_usage *st_usage = &dev->states_usage[i];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700803 enum msm_pm_sleep_mode mode;
804 bool allow;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600805 void *rs_limits = NULL;
806 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700807 int idx;
808
Steve Mucklef132c6c2012-06-06 18:30:57 -0700809 mode = (enum msm_pm_sleep_mode) cpuidle_get_statedata(st_usage);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700810 idx = MSM_PM_MODE(dev->cpu, mode);
811
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600812 allow = msm_pm_sleep_modes[idx].idle_enabled &&
813 msm_pm_sleep_modes[idx].idle_supported;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700814
815 switch (mode) {
816 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
Priyanka Mathur68a9bc52012-10-03 15:15:01 -0700817 if (num_online_cpus() > 1) {
818 allow = false;
819 break;
820 }
821 /* fall through */
Mahesh Sivasubramanianba7e01c2012-09-04 18:22:51 -0600822 case MSM_PM_SLEEP_MODE_RETENTION:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700823 if (!allow)
824 break;
825
Priyanka Mathur68a9bc52012-10-03 15:15:01 -0700826 if (msm_pm_retention_tz_call &&
827 num_online_cpus() > 1) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700828 allow = false;
829 break;
830 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700831 /* fall through */
832
833 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
834 if (!allow)
835 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700836 /* fall through */
837
838 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
839 if (!allow)
840 break;
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600841 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700842
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600843 if (pm_sleep_ops.lowest_limits)
844 rs_limits = pm_sleep_ops.lowest_limits(true,
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600845 mode, &time_param, &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700846
847 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
848 pr_info("CPU%u: %s: %s, latency %uus, "
849 "sleep %uus, limit %p\n",
850 dev->cpu, __func__, state->desc,
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600851 time_param.latency_us,
852 time_param.sleep_us, rs_limits);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700853
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700854 if (!rs_limits)
855 allow = false;
856 break;
857
858 default:
859 allow = false;
860 break;
861 }
862
863 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
864 pr_info("CPU%u: %s: allow %s: %d\n",
865 dev->cpu, __func__, state->desc, (int)allow);
866
867 if (allow) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700868 if (power < power_usage) {
869 power_usage = power;
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600870 modified_time_us = time_param.modified_time_us;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700871 ret = mode;
872 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700873
874 if (MSM_PM_SLEEP_MODE_POWER_COLLAPSE == mode)
875 msm_pm_idle_rs_limits = rs_limits;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700876 }
877 }
878
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600879 if (modified_time_us && !dev->cpu)
880 msm_pm_set_timer(modified_time_us);
Priyanka Mathur26b4a4b2012-11-05 13:45:45 -0800881
882 msm_pm_ftrace_lpm_enter(dev->cpu, time_param.latency_us,
883 time_param.sleep_us, time_param.next_event_us,
884 ret);
885
Steve Mucklef132c6c2012-06-06 18:30:57 -0700886 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700887}
888
889int msm_pm_idle_enter(enum msm_pm_sleep_mode sleep_mode)
890{
891 int64_t time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700892 int exit_stat;
Priyanka Mathur26b4a4b2012-11-05 13:45:45 -0800893 bool collapsed = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700894
895 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
896 pr_info("CPU%u: %s: mode %d\n",
897 smp_processor_id(), __func__, sleep_mode);
898
899 time = ktime_to_ns(ktime_get());
900
901 switch (sleep_mode) {
902 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
903 msm_pm_swfi();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700904 exit_stat = MSM_PM_STAT_IDLE_WFI;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700905 break;
906
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600907 case MSM_PM_SLEEP_MODE_RETENTION:
908 msm_pm_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600909 exit_stat = MSM_PM_STAT_RETENTION;
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600910 break;
911
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700912 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
Priyanka Mathur26b4a4b2012-11-05 13:45:45 -0800913 collapsed = msm_pm_power_collapse_standalone(true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700914 exit_stat = MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700915 break;
916
917 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE: {
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600918 int64_t timer_expiration = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700919 bool timer_halted = false;
920 uint32_t sleep_delay;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600921 int ret = -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700922 int notify_rpm =
923 (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE);
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600924 timer_expiration = msm_pm_timer_enter_idle();
925
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700926 sleep_delay = (uint32_t) msm_pm_convert_and_cap_time(
927 timer_expiration, MSM_PM_SLEEP_TICK_LIMIT);
928 if (sleep_delay == 0) /* 0 would mean infinite time */
929 sleep_delay = 1;
930
Karthik Parsha6fb932d2012-01-24 18:04:12 -0800931 if (MSM_PM_DEBUG_IDLE_CLK & msm_pm_debug_mask)
932 clock_debug_print_enabled();
933
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600934 if (pm_sleep_ops.enter_sleep)
935 ret = pm_sleep_ops.enter_sleep(sleep_delay,
936 msm_pm_idle_rs_limits,
937 true, notify_rpm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700938 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600939 collapsed = msm_pm_power_collapse(true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700940 timer_halted = true;
941
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600942 if (pm_sleep_ops.exit_sleep)
943 pm_sleep_ops.exit_sleep(msm_pm_idle_rs_limits,
944 true, notify_rpm, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700945 }
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600946 msm_pm_timer_exit_idle(timer_halted);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700947 exit_stat = MSM_PM_STAT_IDLE_POWER_COLLAPSE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700948 break;
949 }
950
951 default:
952 __WARN();
953 goto cpuidle_enter_bail;
954 }
955
956 time = ktime_to_ns(ktime_get()) - time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700957 msm_pm_add_stat(exit_stat, time);
Priyanka Mathur26b4a4b2012-11-05 13:45:45 -0800958 msm_pm_ftrace_lpm_exit(smp_processor_id(), sleep_mode,
959 collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700960
961 do_div(time, 1000);
962 return (int) time;
963
964cpuidle_enter_bail:
965 return 0;
966}
967
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700968void msm_pm_cpu_enter_lowpower(unsigned int cpu)
969{
970 int i;
971 bool allow[MSM_PM_SLEEP_MODE_NR];
972
973 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
974 struct msm_pm_platform_data *mode;
975
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600976 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700977 allow[i] = mode->suspend_supported && mode->suspend_enabled;
978 }
979
980 if (MSM_PM_DEBUG_HOTPLUG & msm_pm_debug_mask)
981 pr_notice("CPU%u: %s: shutting down cpu\n", cpu, __func__);
982
Matt Wagantall5375aa72012-07-02 19:59:51 -0700983 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700984 msm_pm_power_collapse(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -0700985 else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700986 msm_pm_power_collapse_standalone(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -0700987 else if (allow[MSM_PM_SLEEP_MODE_RETENTION])
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600988 msm_pm_retention();
Stephen Boydbda74272012-08-09 14:01:27 -0700989 else
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700990 msm_pm_swfi();
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700991}
992
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700993static int msm_pm_enter(suspend_state_t state)
994{
995 bool allow[MSM_PM_SLEEP_MODE_NR];
996 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700997 int64_t period = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530998 int64_t time = msm_pm_timer_enter_suspend(&period);
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600999 struct msm_pm_time_params time_param;
1000
1001 time_param.latency_us = -1;
1002 time_param.sleep_us = -1;
1003 time_param.next_event_us = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001004
1005 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1006 pr_info("%s\n", __func__);
1007
1008 if (smp_processor_id()) {
1009 __WARN();
1010 goto enter_exit;
1011 }
1012
1013
1014 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
1015 struct msm_pm_platform_data *mode;
1016
Praveen Chidambaram42da9d22012-03-30 12:16:34 -06001017 mode = &msm_pm_sleep_modes[MSM_PM_MODE(0, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001018 allow[i] = mode->suspend_supported && mode->suspend_enabled;
1019 }
1020
1021 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001022 void *rs_limits = NULL;
1023 int ret = -ENODEV;
1024 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001025
1026 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1027 pr_info("%s: power collapse\n", __func__);
1028
1029 clock_debug_print_enabled();
1030
1031#ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE
1032 if (msm_pm_sleep_time_override > 0) {
1033 int64_t ns = NSEC_PER_SEC *
1034 (int64_t) msm_pm_sleep_time_override;
1035 msm_pm_set_max_sleep_time(ns);
1036 msm_pm_sleep_time_override = 0;
1037 }
1038#endif /* CONFIG_MSM_SLEEP_TIME_OVERRIDE */
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001039 if (pm_sleep_ops.lowest_limits)
1040 rs_limits = pm_sleep_ops.lowest_limits(false,
Girish Mahadevandc318fd2012-08-17 16:48:05 -06001041 MSM_PM_SLEEP_MODE_POWER_COLLAPSE, &time_param, &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001042
1043 if (rs_limits) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001044 if (pm_sleep_ops.enter_sleep)
1045 ret = pm_sleep_ops.enter_sleep(
1046 msm_pm_max_sleep_time,
1047 rs_limits, false, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001048 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -06001049 int collapsed = msm_pm_power_collapse(false);
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001050 if (pm_sleep_ops.exit_sleep) {
1051 pm_sleep_ops.exit_sleep(rs_limits,
1052 false, true, collapsed);
1053 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001054 }
1055 } else {
1056 pr_err("%s: cannot find the lowest power limit\n",
1057 __func__);
1058 }
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301059 time = msm_pm_timer_exit_suspend(time, period);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001060 msm_pm_add_stat(MSM_PM_STAT_SUSPEND, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001061 } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
1062 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1063 pr_info("%s: standalone power collapse\n", __func__);
1064 msm_pm_power_collapse_standalone(false);
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -06001065 } else if (allow[MSM_PM_SLEEP_MODE_RETENTION]) {
1066 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1067 pr_info("%s: retention\n", __func__);
1068 msm_pm_retention();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001069 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
1070 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1071 pr_info("%s: swfi\n", __func__);
1072 msm_pm_swfi();
1073 }
1074
1075
1076enter_exit:
1077 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1078 pr_info("%s: return\n", __func__);
1079
1080 return 0;
1081}
1082
1083static struct platform_suspend_ops msm_pm_ops = {
1084 .enter = msm_pm_enter,
1085 .valid = suspend_valid_only_mem,
1086};
1087
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001088/******************************************************************************
1089 * Initialization routine
1090 *****************************************************************************/
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001091void msm_pm_set_sleep_ops(struct msm_pm_sleep_ops *ops)
1092{
1093 if (ops)
1094 pm_sleep_ops = *ops;
1095}
1096
Girish Mahadevand27ca4a2012-08-15 09:21:23 -06001097void __init msm_pm_set_tz_retention_flag(unsigned int flag)
1098{
1099 msm_pm_retention_tz_call = flag;
1100}
1101
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -06001102static int __devinit msm_pc_debug_probe(struct platform_device *pdev)
1103{
1104 struct resource *res = NULL;
1105 int i ;
1106
1107 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1108 if (!res)
1109 goto fail;
1110
1111 msm_pc_debug_counters_phys = res->start;
1112 WARN_ON(resource_size(res) < SZ_64);
Mahesh Sivasubramaniane6d33992012-10-17 13:39:40 -06001113 msm_pc_debug_counters = devm_ioremap_nocache(&pdev->dev, res->start,
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -06001114 resource_size(res));
1115
1116 if (!msm_pc_debug_counters)
1117 goto fail;
1118
1119 for (i = 0; i < resource_size(res)/4; i++)
1120 __raw_writel(0, msm_pc_debug_counters + i * 4);
1121 return 0;
1122fail:
1123 msm_pc_debug_counters = 0;
1124 msm_pc_debug_counters_phys = 0;
1125 return -EFAULT;
1126}
1127
1128static struct of_device_id msm_pc_debug_table[] = {
1129 {.compatible = "qcom,pc-cntr"},
1130 {},
1131};
1132
1133static struct platform_driver msm_pc_counter_driver = {
1134 .probe = msm_pc_debug_probe,
1135 .driver = {
1136 .name = "pc-cntr",
1137 .owner = THIS_MODULE,
1138 .of_match_table = msm_pc_debug_table,
1139 },
1140};
1141
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001142static int __init msm_pm_init(void)
1143{
1144 pgd_t *pc_pgd;
1145 pmd_t *pmd;
1146 unsigned long pmdval;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301147 enum msm_pm_time_stats_id enable_stats[] = {
1148 MSM_PM_STAT_IDLE_WFI,
1149 MSM_PM_STAT_RETENTION,
1150 MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE,
1151 MSM_PM_STAT_IDLE_POWER_COLLAPSE,
1152 MSM_PM_STAT_SUSPEND,
1153 };
Steve Mucklef132c6c2012-06-06 18:30:57 -07001154 unsigned long exit_phys;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301155
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001156 /* Page table for cores to come back up safely. */
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -06001157
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001158 pc_pgd = pgd_alloc(&init_mm);
1159 if (!pc_pgd)
1160 return -ENOMEM;
1161
Steve Mucklef132c6c2012-06-06 18:30:57 -07001162 exit_phys = virt_to_phys(msm_pm_collapse_exit);
1163
1164 pmd = pmd_offset(pud_offset(pc_pgd + pgd_index(exit_phys),exit_phys),
1165 exit_phys);
1166 pmdval = (exit_phys & PGDIR_MASK) |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001167 PMD_TYPE_SECT | PMD_SECT_AP_WRITE;
1168 pmd[0] = __pmd(pmdval);
1169 pmd[1] = __pmd(pmdval + (1 << (PGDIR_SHIFT - 1)));
1170
Steve Mucklefcece052012-02-18 20:09:58 -08001171 msm_saved_state_phys =
1172 allocate_contiguous_ebi_nomap(CPU_SAVED_STATE_SIZE *
1173 num_possible_cpus(), 4);
1174 if (!msm_saved_state_phys)
1175 return -ENOMEM;
1176 msm_saved_state = ioremap_nocache(msm_saved_state_phys,
1177 CPU_SAVED_STATE_SIZE *
1178 num_possible_cpus());
1179 if (!msm_saved_state)
1180 return -ENOMEM;
1181
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001182 /* It is remotely possible that the code in msm_pm_collapse_exit()
1183 * which turns on the MMU with this mapping is in the
1184 * next even-numbered megabyte beyond the
1185 * start of msm_pm_collapse_exit().
1186 * Map this megabyte in as well.
1187 */
1188 pmd[2] = __pmd(pmdval + (2 << (PGDIR_SHIFT - 1)));
1189 flush_pmd_entry(pmd);
1190 msm_pm_pc_pgd = virt_to_phys(pc_pgd);
Steve Muckle730ad7a2012-02-21 15:26:37 -08001191 clean_caches((unsigned long)&msm_pm_pc_pgd, sizeof(msm_pm_pc_pgd),
1192 virt_to_phys(&msm_pm_pc_pgd));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001193
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001194 msm_pm_mode_sysfs_add();
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301195 msm_pm_add_stats(enable_stats, ARRAY_SIZE(enable_stats));
1196
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001197 suspend_set_ops(&msm_pm_ops);
Praveen Chidambaramc594a092012-09-18 19:48:29 -06001198 msm_pm_target_init();
Girish Mahadevandc318fd2012-08-17 16:48:05 -06001199 hrtimer_init(&pm_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001200 msm_cpuidle_init();
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -06001201 platform_driver_register(&msm_pc_counter_driver);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001202
1203 return 0;
1204}
1205
1206late_initcall(msm_pm_init);
Girish Mahadevan55944992012-10-26 11:03:07 -06001207
1208static void __devinit msm_pm_set_flush_fn(uint32_t pc_mode)
1209{
1210 msm_pm_disable_l2_fn = NULL;
1211 msm_pm_enable_l2_fn = NULL;
1212 msm_pm_flush_l2_fn = outer_flush_all;
1213
1214 if (pc_mode == MSM_PM_PC_NOTZ_L2_EXT) {
1215 msm_pm_disable_l2_fn = outer_disable;
1216 msm_pm_enable_l2_fn = outer_resume;
1217 }
1218}
1219
1220static int __devinit msm_pm_8x60_probe(struct platform_device *pdev)
1221{
1222 char *key = NULL;
1223 uint32_t val = 0;
1224 int ret = 0;
1225
1226 if (!pdev->dev.of_node) {
1227 struct msm_pm_init_data_type *d = pdev->dev.platform_data;
1228
1229 if (!d)
1230 goto pm_8x60_probe_done;
1231
1232 msm_pm_init_data.pc_mode = d->pc_mode;
1233 msm_pm_set_flush_fn(msm_pm_init_data.pc_mode);
1234 msm_pm_init_data.use_sync_timer = d->use_sync_timer;
1235 } else {
1236 key = "qcom,pc-mode";
1237 ret = of_property_read_u32(pdev->dev.of_node, key, &val);
1238
1239 if (ret) {
1240 pr_debug("%s: Cannot read %s,defaulting to 0",
1241 __func__, key);
1242 val = MSM_PM_PC_TZ_L2_INT;
1243 ret = 0;
1244 }
1245
1246 msm_pm_init_data.pc_mode = val;
1247 msm_pm_set_flush_fn(msm_pm_init_data.pc_mode);
1248
1249 key = "qcom,use-sync-timer";
1250 msm_pm_init_data.use_sync_timer =
1251 of_property_read_bool(pdev->dev.of_node, key);
1252 }
1253
1254pm_8x60_probe_done:
1255 return ret;
1256}
1257
1258static struct of_device_id msm_pm_8x60_table[] = {
1259 {.compatible = "qcom,pm-8x60"},
1260 {},
1261};
1262
1263static struct platform_driver msm_pm_8x60_driver = {
1264 .probe = msm_pm_8x60_probe,
1265 .driver = {
1266 .name = "pm-8x60",
1267 .owner = THIS_MODULE,
1268 .of_match_table = msm_pm_8x60_table,
1269 },
1270};
1271
1272static int __init msm_pm_8x60_init(void)
1273{
1274 return platform_driver_register(&msm_pm_8x60_driver);
1275}
1276module_init(msm_pm_8x60_init);