blob: f918399b48638d12bd759a1be9364d0aa96e0883 [file] [log] [blame]
Praveen Chidambaram0ed2feb2013-02-26 21:05:41 -07001/* Copyright (c) 2010-2013, 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>
Anji Jonnalae3406bc2013-04-22 10:01:59 +053027#include <linux/delay.h>
Mahesh Sivasubramanian24b2cea2012-03-14 14:50:37 -060028#include <linux/platform_device.h>
Anji Jonnalae3406bc2013-04-22 10:01:59 +053029#include <linux/of_platform.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070030#include <mach/msm_iomap.h>
Praveen Chidambaram192979f2012-04-25 18:30:23 -060031#include <mach/socinfo.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070032#include <mach/system.h>
Girish Mahadevan63b87262012-08-15 09:21:23 -060033#include <mach/scm.h>
34#include <mach/socinfo.h>
Praveen Chidambaram79f3b4a2012-09-18 19:48:29 -060035#include <mach/msm-krait-l2-accessors.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070036#include <asm/cacheflush.h>
37#include <asm/hardware/gic.h>
38#include <asm/pgtable.h>
39#include <asm/pgalloc.h>
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -060040#include <asm/hardware/cache-l2x0.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070041#ifdef CONFIG_VFP
42#include <asm/vfp.h>
43#endif
44
45#include "acpuclock.h"
46#include "clock.h"
47#include "avs.h"
Abhijeet Dharmapurikarefaca4f2011-12-27 16:24:07 -080048#include <mach/cpuidle.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070049#include "idle.h"
Matt Wagantall7cca4642012-02-01 16:43:24 -080050#include "pm.h"
Jaeseong GIMfc880da2012-08-13 18:47:46 -070051#include "rpm_resources.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070052#include "scm-boot.h"
53#include "spm.h"
54#include "timer.h"
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -060055#include "pm-boot.h"
Girish Mahadevan12aaabe2012-08-17 16:48:05 -060056#include <mach/event_timer.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070057
58/******************************************************************************
59 * Debug Definitions
60 *****************************************************************************/
61
62enum {
63 MSM_PM_DEBUG_SUSPEND = BIT(0),
64 MSM_PM_DEBUG_POWER_COLLAPSE = BIT(1),
65 MSM_PM_DEBUG_SUSPEND_LIMITS = BIT(2),
66 MSM_PM_DEBUG_CLOCK = BIT(3),
67 MSM_PM_DEBUG_RESET_VECTOR = BIT(4),
Karthik Parsha6fb932d2012-01-24 18:04:12 -080068 MSM_PM_DEBUG_IDLE_CLK = BIT(5),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070069 MSM_PM_DEBUG_IDLE = BIT(6),
70 MSM_PM_DEBUG_IDLE_LIMITS = BIT(7),
71 MSM_PM_DEBUG_HOTPLUG = BIT(8),
72};
73
74static int msm_pm_debug_mask = 1;
75module_param_named(
76 debug_mask, msm_pm_debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP
77);
Girish Mahadevan63b87262012-08-15 09:21:23 -060078static int msm_pm_retention_tz_call;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070079
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070080/******************************************************************************
81 * Sleep Modes and Parameters
82 *****************************************************************************/
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070083enum {
84 MSM_PM_MODE_ATTR_SUSPEND,
85 MSM_PM_MODE_ATTR_IDLE,
86 MSM_PM_MODE_ATTR_NR,
87};
88
Girish Mahadevan63b87262012-08-15 09:21:23 -060089#define SCM_L2_RETENTION (0x2)
90#define SCM_CMD_TERMINATE_PC (0x2)
91
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070092static char *msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_NR] = {
93 [MSM_PM_MODE_ATTR_SUSPEND] = "suspend_enabled",
94 [MSM_PM_MODE_ATTR_IDLE] = "idle_enabled",
95};
96
97struct msm_pm_kobj_attribute {
98 unsigned int cpu;
99 struct kobj_attribute ka;
100};
101
102#define GET_CPU_OF_ATTR(attr) \
103 (container_of(attr, struct msm_pm_kobj_attribute, ka)->cpu)
104
105struct msm_pm_sysfs_sleep_mode {
106 struct kobject *kobj;
107 struct attribute_group attr_group;
108 struct attribute *attrs[MSM_PM_MODE_ATTR_NR + 1];
109 struct msm_pm_kobj_attribute kas[MSM_PM_MODE_ATTR_NR];
110};
111
112static char *msm_pm_sleep_mode_labels[MSM_PM_SLEEP_MODE_NR] = {
113 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE] = "power_collapse",
114 [MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT] = "wfi",
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600115 [MSM_PM_SLEEP_MODE_RETENTION] = "retention",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700116 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE] =
117 "standalone_power_collapse",
118};
119
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600120static struct hrtimer pm_hrtimer;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600121static struct msm_pm_sleep_ops pm_sleep_ops;
Anji Jonnalae3406bc2013-04-22 10:01:59 +0530122static struct msm_pm_sleep_status_data *msm_pm_slp_sts;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700123/*
124 * Write out the attribute.
125 */
126static ssize_t msm_pm_mode_attr_show(
127 struct kobject *kobj, struct kobj_attribute *attr, char *buf)
128{
129 int ret = -EINVAL;
130 int i;
131
132 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
133 struct kernel_param kp;
134 unsigned int cpu;
135 struct msm_pm_platform_data *mode;
136
137 if (msm_pm_sleep_mode_labels[i] == NULL)
138 continue;
139
140 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
141 continue;
142
143 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600144 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700145
146 if (!strcmp(attr->attr.name,
147 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
148 u32 arg = mode->suspend_enabled;
149 kp.arg = &arg;
150 ret = param_get_ulong(buf, &kp);
151 } else if (!strcmp(attr->attr.name,
152 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
153 u32 arg = mode->idle_enabled;
154 kp.arg = &arg;
155 ret = param_get_ulong(buf, &kp);
156 }
157
158 break;
159 }
160
161 if (ret > 0) {
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600162 strlcat(buf, "\n", PAGE_SIZE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700163 ret++;
164 }
165
166 return ret;
167}
168
169/*
170 * Read in the new attribute value.
171 */
172static ssize_t msm_pm_mode_attr_store(struct kobject *kobj,
173 struct kobj_attribute *attr, const char *buf, size_t count)
174{
175 int ret = -EINVAL;
176 int i;
177
178 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
179 struct kernel_param kp;
180 unsigned int cpu;
181 struct msm_pm_platform_data *mode;
182
183 if (msm_pm_sleep_mode_labels[i] == NULL)
184 continue;
185
186 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
187 continue;
188
189 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600190 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700191
192 if (!strcmp(attr->attr.name,
193 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
194 kp.arg = &mode->suspend_enabled;
195 ret = param_set_byte(buf, &kp);
196 } else if (!strcmp(attr->attr.name,
197 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
198 kp.arg = &mode->idle_enabled;
199 ret = param_set_byte(buf, &kp);
200 }
201
202 break;
203 }
204
205 return ret ? ret : count;
206}
207
208/*
209 * Add sysfs entries for one cpu.
210 */
211static int __init msm_pm_mode_sysfs_add_cpu(
212 unsigned int cpu, struct kobject *modes_kobj)
213{
214 char cpu_name[8];
215 struct kobject *cpu_kobj;
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600216 struct msm_pm_sysfs_sleep_mode *mode = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700217 int i, j, k;
218 int ret;
219
220 snprintf(cpu_name, sizeof(cpu_name), "cpu%u", cpu);
221 cpu_kobj = kobject_create_and_add(cpu_name, modes_kobj);
222 if (!cpu_kobj) {
223 pr_err("%s: cannot create %s kobject\n", __func__, cpu_name);
224 ret = -ENOMEM;
225 goto mode_sysfs_add_cpu_exit;
226 }
227
228 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
229 int idx = MSM_PM_MODE(cpu, i);
230
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600231 if ((!msm_pm_sleep_modes[idx].suspend_supported)
232 && (!msm_pm_sleep_modes[idx].idle_supported))
233 continue;
234
235 if (!msm_pm_sleep_mode_labels[i] ||
236 !msm_pm_sleep_mode_labels[i][0])
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700237 continue;
238
239 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
240 if (!mode) {
241 pr_err("%s: cannot allocate memory for attributes\n",
242 __func__);
243 ret = -ENOMEM;
244 goto mode_sysfs_add_cpu_exit;
245 }
246
247 mode->kobj = kobject_create_and_add(
248 msm_pm_sleep_mode_labels[i], cpu_kobj);
249 if (!mode->kobj) {
250 pr_err("%s: cannot create kobject\n", __func__);
251 ret = -ENOMEM;
252 goto mode_sysfs_add_cpu_exit;
253 }
254
255 for (k = 0, j = 0; k < MSM_PM_MODE_ATTR_NR; k++) {
256 if ((k == MSM_PM_MODE_ATTR_IDLE) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600257 !msm_pm_sleep_modes[idx].idle_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700258 continue;
259 if ((k == MSM_PM_MODE_ATTR_SUSPEND) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600260 !msm_pm_sleep_modes[idx].suspend_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700261 continue;
Stephen Boydd2059c32012-07-03 15:11:15 -0700262 sysfs_attr_init(&mode->kas[j].ka.attr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700263 mode->kas[j].cpu = cpu;
264 mode->kas[j].ka.attr.mode = 0644;
265 mode->kas[j].ka.show = msm_pm_mode_attr_show;
266 mode->kas[j].ka.store = msm_pm_mode_attr_store;
267 mode->kas[j].ka.attr.name = msm_pm_mode_attr_labels[k];
268 mode->attrs[j] = &mode->kas[j].ka.attr;
269 j++;
270 }
271 mode->attrs[j] = NULL;
272
273 mode->attr_group.attrs = mode->attrs;
274 ret = sysfs_create_group(mode->kobj, &mode->attr_group);
275 if (ret) {
276 pr_err("%s: cannot create kobject attribute group\n",
277 __func__);
278 goto mode_sysfs_add_cpu_exit;
279 }
280 }
281
282 ret = 0;
283
284mode_sysfs_add_cpu_exit:
Praveen Chidambaramd5ac2d32011-10-24 14:30:27 -0600285 if (ret) {
Praveen Chidambaram2cfda632011-10-11 16:58:09 -0600286 if (mode && mode->kobj)
287 kobject_del(mode->kobj);
288 kfree(mode);
289 }
290
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700291 return ret;
292}
293
294/*
295 * Add sysfs entries for the sleep modes.
296 */
297static int __init msm_pm_mode_sysfs_add(void)
298{
299 struct kobject *module_kobj;
300 struct kobject *modes_kobj;
301 unsigned int cpu;
302 int ret;
303
304 module_kobj = kset_find_obj(module_kset, KBUILD_MODNAME);
305 if (!module_kobj) {
306 pr_err("%s: cannot find kobject for module %s\n",
307 __func__, KBUILD_MODNAME);
308 ret = -ENOENT;
309 goto mode_sysfs_add_exit;
310 }
311
312 modes_kobj = kobject_create_and_add("modes", module_kobj);
313 if (!modes_kobj) {
314 pr_err("%s: cannot create modes kobject\n", __func__);
315 ret = -ENOMEM;
316 goto mode_sysfs_add_exit;
317 }
318
319 for_each_possible_cpu(cpu) {
320 ret = msm_pm_mode_sysfs_add_cpu(cpu, modes_kobj);
321 if (ret)
322 goto mode_sysfs_add_exit;
323 }
324
325 ret = 0;
326
327mode_sysfs_add_exit:
328 return ret;
329}
330
331/******************************************************************************
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700332 * Configure Hardware before/after Low Power Mode
333 *****************************************************************************/
334
335/*
336 * Configure hardware registers in preparation for Apps power down.
337 */
338static void msm_pm_config_hw_before_power_down(void)
339{
340 return;
341}
342
343/*
344 * Clear hardware registers after Apps powers up.
345 */
346static void msm_pm_config_hw_after_power_up(void)
347{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700348}
349
350/*
351 * Configure hardware registers in preparation for SWFI.
352 */
353static void msm_pm_config_hw_before_swfi(void)
354{
355 return;
356}
357
Girish Mahadevan63b87262012-08-15 09:21:23 -0600358/*
359 * Configure/Restore hardware registers in preparation for Retention.
360 */
361
362static void msm_pm_config_hw_after_retention(void)
363{
364 int ret;
365 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
366 WARN_ON(ret);
367}
368
369static void msm_pm_config_hw_before_retention(void)
370{
371 return;
372}
373
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700374
375/******************************************************************************
376 * Suspend Max Sleep Time
377 *****************************************************************************/
378
379#ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE
380static int msm_pm_sleep_time_override;
381module_param_named(sleep_time_override,
382 msm_pm_sleep_time_override, int, S_IRUGO | S_IWUSR | S_IWGRP);
383#endif
384
385#define SCLK_HZ (32768)
386#define MSM_PM_SLEEP_TICK_LIMIT (0x6DDD000)
387
388static uint32_t msm_pm_max_sleep_time;
389
390/*
391 * Convert time from nanoseconds to slow clock ticks, then cap it to the
392 * specified limit
393 */
394static int64_t msm_pm_convert_and_cap_time(int64_t time_ns, int64_t limit)
395{
396 do_div(time_ns, NSEC_PER_SEC / SCLK_HZ);
397 return (time_ns > limit) ? limit : time_ns;
398}
399
400/*
401 * Set the sleep time for suspend. 0 means infinite sleep time.
402 */
403void msm_pm_set_max_sleep_time(int64_t max_sleep_time_ns)
404{
405 if (max_sleep_time_ns == 0) {
406 msm_pm_max_sleep_time = 0;
407 } else {
408 msm_pm_max_sleep_time = (uint32_t)msm_pm_convert_and_cap_time(
409 max_sleep_time_ns, MSM_PM_SLEEP_TICK_LIMIT);
410
411 if (msm_pm_max_sleep_time == 0)
412 msm_pm_max_sleep_time = 1;
413 }
414
415 if (msm_pm_debug_mask & MSM_PM_DEBUG_SUSPEND)
416 pr_info("%s: Requested %lld ns Giving %u sclk ticks\n",
417 __func__, max_sleep_time_ns, msm_pm_max_sleep_time);
418}
419EXPORT_SYMBOL(msm_pm_set_max_sleep_time);
420
Praveen Chidambaram79f3b4a2012-09-18 19:48:29 -0600421struct reg_data {
422 uint32_t reg;
423 uint32_t val;
424};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700425
Praveen Chidambaram79f3b4a2012-09-18 19:48:29 -0600426static struct reg_data reg_saved_state[] = {
427 { .reg = 0x4501, },
428 { .reg = 0x5501, },
429 { .reg = 0x6501, },
430 { .reg = 0x7501, },
431 { .reg = 0x0500, },
432};
433
434static unsigned int active_vdd;
435static bool msm_pm_save_cp15;
436static const unsigned int pc_vdd = 0x98;
437
438static void msm_pm_save_cpu_reg(void)
439{
440 int i;
441
442 /* Only on core0 */
443 if (smp_processor_id())
444 return;
445
446 /**
447 * On some targets, L2 PC will turn off may reset the core
448 * configuration for the mux and the default may not make the core
449 * happy when it resumes.
450 * Save the active vdd, and set the core vdd to QSB max vdd, so that
451 * when the core resumes, it is capable of supporting the current QSB
452 * rate. Then restore the active vdd before switching the acpuclk rate.
453 */
454 if (msm_pm_get_l2_flush_flag() == 1) {
455 active_vdd = msm_spm_get_vdd(0);
456 for (i = 0; i < ARRAY_SIZE(reg_saved_state); i++)
457 reg_saved_state[i].val =
458 get_l2_indirect_reg(reg_saved_state[i].reg);
459 msm_spm_set_vdd(0, pc_vdd);
460 }
461}
462
463static void msm_pm_restore_cpu_reg(void)
464{
465 int i;
466
467 /* Only on core0 */
468 if (smp_processor_id())
469 return;
470
471 if (msm_pm_get_l2_flush_flag() == 1) {
472 for (i = 0; i < ARRAY_SIZE(reg_saved_state); i++)
473 set_l2_indirect_reg(reg_saved_state[i].reg,
474 reg_saved_state[i].val);
475 msm_spm_set_vdd(0, active_vdd);
476 }
477}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700478
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600479static void *msm_pm_idle_rs_limits;
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600480static bool msm_pm_use_qtimer;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700481
482static void msm_pm_swfi(void)
483{
484 msm_pm_config_hw_before_swfi();
485 msm_arch_idle();
486}
487
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600488
489static void msm_pm_retention(void)
490{
491 int ret = 0;
492
Girish Mahadevan63b87262012-08-15 09:21:23 -0600493 msm_pm_config_hw_before_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600494 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_POWER_RETENTION, false);
495 WARN_ON(ret);
Girish Mahadevan63b87262012-08-15 09:21:23 -0600496
497 if (msm_pm_retention_tz_call)
498 scm_call_atomic1(SCM_SVC_BOOT, SCM_CMD_TERMINATE_PC,
499 SCM_L2_RETENTION);
500 else
501 msm_arch_idle();
502
503 msm_pm_config_hw_after_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600504}
505
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600506#ifdef CONFIG_CACHE_L2X0
507static inline bool msm_pm_l2x0_power_collapse(void)
508{
509 bool collapsed = 0;
510
Taniya Das38a8c6e2012-05-09 20:34:39 +0530511 l2cc_suspend();
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600512 collapsed = msm_pm_collapse();
Taniya Das38a8c6e2012-05-09 20:34:39 +0530513 l2cc_resume();
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600514
515 return collapsed;
516}
517#else
518static inline bool msm_pm_l2x0_power_collapse(void)
519{
520 return msm_pm_collapse();
521}
522#endif
523
Stephen Boydb29750d2012-02-21 01:21:32 -0800524static bool __ref msm_pm_spm_power_collapse(
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700525 unsigned int cpu, bool from_idle, bool notify_rpm)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700526{
527 void *entry;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600528 bool collapsed = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700529 int ret;
Rohit Vaswanie78dfb62012-02-21 10:29:29 -0800530 unsigned int saved_gic_cpu_ctrl;
531
532 saved_gic_cpu_ctrl = readl_relaxed(MSM_QGIC_CPU_BASE + GIC_CPU_CTRL);
533 mb();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700534
535 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
536 pr_info("CPU%u: %s: notify_rpm %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700537 cpu, __func__, (int) notify_rpm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700538
539 ret = msm_spm_set_low_power_mode(
540 MSM_SPM_MODE_POWER_COLLAPSE, notify_rpm);
541 WARN_ON(ret);
542
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700543 entry = (!cpu || from_idle) ?
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700544 msm_pm_collapse_exit : msm_secondary_startup;
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700545 msm_pm_boot_config_before_pc(cpu, virt_to_phys(entry));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700546
547 if (MSM_PM_DEBUG_RESET_VECTOR & msm_pm_debug_mask)
548 pr_info("CPU%u: %s: program vector to %p\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700549 cpu, __func__, entry);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700550
551#ifdef CONFIG_VFP
Steve Mucklef132c6c2012-06-06 18:30:57 -0700552 vfp_pm_suspend();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700553#endif
Maheshkumar Sivasubramanianc6c55032011-10-25 16:01:32 -0600554 collapsed = msm_pm_l2x0_power_collapse();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700555
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700556 msm_pm_boot_config_after_pc(cpu);
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -0600557
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700558 if (collapsed) {
559#ifdef CONFIG_VFP
Steve Mucklef132c6c2012-06-06 18:30:57 -0700560 vfp_pm_resume();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700561#endif
562 cpu_init();
563 writel(0xF0, MSM_QGIC_CPU_BASE + GIC_CPU_PRIMASK);
Rohit Vaswanie78dfb62012-02-21 10:29:29 -0800564 writel_relaxed(saved_gic_cpu_ctrl,
565 MSM_QGIC_CPU_BASE + GIC_CPU_CTRL);
566 mb();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700567 local_fiq_enable();
568 }
569
570 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
571 pr_info("CPU%u: %s: msm_pm_collapse returned, collapsed %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700572 cpu, __func__, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700573
574 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
575 WARN_ON(ret);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600576 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700577}
578
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600579static bool msm_pm_power_collapse_standalone(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700580{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700581 unsigned int cpu = smp_processor_id();
Praveen Chidambaram2a618992012-11-08 17:53:34 -0700582 unsigned int avsdscr;
583 unsigned int avscsr;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600584 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700585
Praveen Chidambaram2a618992012-11-08 17:53:34 -0700586 avsdscr = avs_get_avsdscr();
587 avscsr = avs_get_avscsr();
588 avs_set_avscsr(0); /* Disable AVS */
589
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700590 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, false);
Praveen Chidambaram2a618992012-11-08 17:53:34 -0700591
592 avs_set_avsdscr(avsdscr);
593 avs_set_avscsr(avscsr);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600594 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700595}
596
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600597static bool msm_pm_power_collapse(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700598{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700599 unsigned int cpu = smp_processor_id();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700600 unsigned long saved_acpuclk_rate;
Praveen Chidambaram2a618992012-11-08 17:53:34 -0700601 unsigned int avsdscr;
602 unsigned int avscsr;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600603 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700604
605 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
606 pr_info("CPU%u: %s: idle %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700607 cpu, __func__, (int)from_idle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700608
609 msm_pm_config_hw_before_power_down();
610 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700611 pr_info("CPU%u: %s: pre power down\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700612
Praveen Chidambaram2a618992012-11-08 17:53:34 -0700613 avsdscr = avs_get_avsdscr();
614 avscsr = avs_get_avscsr();
615 avs_set_avscsr(0); /* Disable AVS */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700616
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700617 if (cpu_online(cpu))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700618 saved_acpuclk_rate = acpuclk_power_collapse();
619 else
620 saved_acpuclk_rate = 0;
621
622 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
623 pr_info("CPU%u: %s: change clock rate (old rate = %lu)\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700624 cpu, __func__, saved_acpuclk_rate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700625
Praveen Chidambaram79f3b4a2012-09-18 19:48:29 -0600626 if (msm_pm_save_cp15)
627 msm_pm_save_cpu_reg();
628
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700629 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700630
Praveen Chidambaram79f3b4a2012-09-18 19:48:29 -0600631 if (msm_pm_save_cp15)
632 msm_pm_restore_cpu_reg();
633
Girish Mahadevan4e025d92012-02-29 13:26:51 -0700634 if (cpu_online(cpu)) {
635 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
636 pr_info("CPU%u: %s: restore clock rate to %lu\n",
637 cpu, __func__, saved_acpuclk_rate);
638 if (acpuclk_set_rate(cpu, saved_acpuclk_rate, SETRATE_PC) < 0)
639 pr_err("CPU%u: %s: failed to restore clock rate(%lu)\n",
640 cpu, __func__, saved_acpuclk_rate);
641 } else {
642 unsigned int gic_dist_enabled;
643 unsigned int gic_dist_pending;
644 gic_dist_enabled = readl_relaxed(
645 MSM_QGIC_DIST_BASE + GIC_DIST_ENABLE_CLEAR);
646 gic_dist_pending = readl_relaxed(
647 MSM_QGIC_DIST_BASE + GIC_DIST_PENDING_SET);
648 mb();
649 gic_dist_pending &= gic_dist_enabled;
650
651 if (gic_dist_pending)
652 pr_err("CPU %d interrupted during hotplug.Pending int 0x%x\n",
653 cpu, gic_dist_pending);
654 }
655
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700656
Praveen Chidambaram2a618992012-11-08 17:53:34 -0700657 avs_set_avsdscr(avsdscr);
658 avs_set_avscsr(avscsr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700659 msm_pm_config_hw_after_power_up();
660 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700661 pr_info("CPU%u: %s: post power up\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700662
663 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700664 pr_info("CPU%u: %s: return\n", cpu, __func__);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600665 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700666}
667
Praveen Chidambaram79f3b4a2012-09-18 19:48:29 -0600668static void msm_pm_target_init(void)
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600669{
Praveen Chidambaram79f3b4a2012-09-18 19:48:29 -0600670 if (cpu_is_apq8064())
671 msm_pm_save_cp15 = true;
672
673 if (cpu_is_msm8974())
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600674 msm_pm_use_qtimer = true;
675}
676
677static int64_t msm_pm_timer_enter_idle(void)
678{
679 if (msm_pm_use_qtimer)
680 return ktime_to_ns(tick_nohz_get_sleep_length());
681
682 return msm_timer_enter_idle();
683}
684
685static void msm_pm_timer_exit_idle(bool timer_halted)
686{
687 if (msm_pm_use_qtimer)
688 return;
689
690 msm_timer_exit_idle((int) timer_halted);
691}
692
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530693static int64_t msm_pm_timer_enter_suspend(int64_t *period)
694{
Anji Jonnalac02367a2012-07-01 02:56:11 +0530695 int64_t time = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530696
697 if (msm_pm_use_qtimer)
698 return sched_clock();
699
700 time = msm_timer_get_sclk_time(period);
701 if (!time)
702 pr_err("%s: Unable to read sclk.\n", __func__);
703
704 return time;
705}
706
707static int64_t msm_pm_timer_exit_suspend(int64_t time, int64_t period)
708{
709 if (msm_pm_use_qtimer)
710 return sched_clock() - time;
711
712 if (time != 0) {
713 int64_t end_time = msm_timer_get_sclk_time(NULL);
714 if (end_time != 0) {
715 time = end_time - time;
716 if (time < 0)
717 time += period;
718 } else
719 time = 0;
720 }
721
722 return time;
723}
724
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600725/**
726 * pm_hrtimer_cb() : Callback function for hrtimer created if the
727 * core needs to be awake to handle an event.
728 * @hrtimer : Pointer to hrtimer
729 */
730static enum hrtimer_restart pm_hrtimer_cb(struct hrtimer *hrtimer)
731{
732 return HRTIMER_NORESTART;
733}
734
735/**
736 * msm_pm_set_timer() : Set an hrtimer to wakeup the core in time
737 * to handle an event.
738 */
739static void msm_pm_set_timer(uint32_t modified_time_us)
740{
741 u64 modified_time_ns = modified_time_us * NSEC_PER_USEC;
742 ktime_t modified_ktime = ns_to_ktime(modified_time_ns);
743 pm_hrtimer.function = pm_hrtimer_cb;
Venkat Devarasetty46881f12013-05-15 15:36:58 +0530744 hrtimer_start(&pm_hrtimer, modified_ktime, HRTIMER_MODE_REL);
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600745}
746
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700747/******************************************************************************
748 * External Idle/Suspend Functions
749 *****************************************************************************/
750
751void arch_idle(void)
752{
753 return;
754}
755
Steve Mucklef132c6c2012-06-06 18:30:57 -0700756int msm_pm_idle_prepare(struct cpuidle_device *dev,
757 struct cpuidle_driver *drv, int index)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700758{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700759 int i;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700760 unsigned int power_usage = -1;
761 int ret = 0;
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600762 uint32_t modified_time_us = 0;
763 struct msm_pm_time_params time_param;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700764
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600765 time_param.latency_us =
766 (uint32_t) pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
767 time_param.sleep_us =
768 (uint32_t) (ktime_to_us(tick_nohz_get_sleep_length())
769 & UINT_MAX);
770 time_param.modified_time_us = 0;
771
772 if (!dev->cpu)
773 time_param.next_event_us =
774 (uint32_t) (ktime_to_us(get_next_event_time())
775 & UINT_MAX);
776 else
777 time_param.next_event_us = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700778
779 for (i = 0; i < dev->state_count; i++) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700780 struct cpuidle_state *state = &drv->states[i];
781 struct cpuidle_state_usage *st_usage = &dev->states_usage[i];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700782 enum msm_pm_sleep_mode mode;
783 bool allow;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600784 void *rs_limits = NULL;
785 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700786 int idx;
787
Steve Mucklef132c6c2012-06-06 18:30:57 -0700788 mode = (enum msm_pm_sleep_mode) cpuidle_get_statedata(st_usage);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700789 idx = MSM_PM_MODE(dev->cpu, mode);
790
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600791 allow = msm_pm_sleep_modes[idx].idle_enabled &&
792 msm_pm_sleep_modes[idx].idle_supported;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700793
794 switch (mode) {
795 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
Priyanka Mathur5effc7c2012-10-03 15:15:01 -0700796 if (num_online_cpus() > 1) {
797 allow = false;
798 break;
799 }
800 /* fall through */
Mahesh Sivasubramanian96c0aa42012-09-04 18:22:51 -0600801 case MSM_PM_SLEEP_MODE_RETENTION:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700802 if (!allow)
803 break;
804
Priyanka Mathur5effc7c2012-10-03 15:15:01 -0700805 if (msm_pm_retention_tz_call &&
806 num_online_cpus() > 1) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700807 allow = false;
808 break;
809 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700810 /* fall through */
811
812 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
813 if (!allow)
814 break;
Jaeseong GIMfc880da2012-08-13 18:47:46 -0700815
816 if (!dev->cpu && msm_rpm_local_request_is_outstanding()) {
817 allow = false;
818 break;
819 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700820 /* fall through */
821
822 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
823 if (!allow)
824 break;
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600825 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700826
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600827 if (pm_sleep_ops.lowest_limits)
828 rs_limits = pm_sleep_ops.lowest_limits(true,
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600829 mode, &time_param, &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700830
831 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
832 pr_info("CPU%u: %s: %s, latency %uus, "
833 "sleep %uus, limit %p\n",
834 dev->cpu, __func__, state->desc,
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600835 time_param.latency_us,
836 time_param.sleep_us, rs_limits);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700837
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700838 if (!rs_limits)
839 allow = false;
840 break;
841
842 default:
843 allow = false;
844 break;
845 }
846
847 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
848 pr_info("CPU%u: %s: allow %s: %d\n",
849 dev->cpu, __func__, state->desc, (int)allow);
850
851 if (allow) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700852 if (power < power_usage) {
853 power_usage = power;
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600854 modified_time_us = time_param.modified_time_us;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700855 ret = mode;
856 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700857
858 if (MSM_PM_SLEEP_MODE_POWER_COLLAPSE == mode)
859 msm_pm_idle_rs_limits = rs_limits;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700860 }
861 }
862
Girish Mahadevan12aaabe2012-08-17 16:48:05 -0600863 if (modified_time_us && !dev->cpu)
864 msm_pm_set_timer(modified_time_us);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700865 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700866}
867
868int msm_pm_idle_enter(enum msm_pm_sleep_mode sleep_mode)
869{
870 int64_t time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700871 int exit_stat;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700872
873 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
874 pr_info("CPU%u: %s: mode %d\n",
875 smp_processor_id(), __func__, sleep_mode);
876
877 time = ktime_to_ns(ktime_get());
878
879 switch (sleep_mode) {
880 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
881 msm_pm_swfi();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700882 exit_stat = MSM_PM_STAT_IDLE_WFI;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700883 break;
884
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600885 case MSM_PM_SLEEP_MODE_RETENTION:
886 msm_pm_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600887 exit_stat = MSM_PM_STAT_RETENTION;
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600888 break;
889
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700890 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
891 msm_pm_power_collapse_standalone(true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700892 exit_stat = MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700893 break;
894
895 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE: {
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600896 int64_t timer_expiration = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700897 bool timer_halted = false;
898 uint32_t sleep_delay;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600899 int ret = -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700900 int notify_rpm =
901 (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600902 int collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700903
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600904 timer_expiration = msm_pm_timer_enter_idle();
905
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700906 sleep_delay = (uint32_t) msm_pm_convert_and_cap_time(
907 timer_expiration, MSM_PM_SLEEP_TICK_LIMIT);
908 if (sleep_delay == 0) /* 0 would mean infinite time */
909 sleep_delay = 1;
910
Karthik Parsha6fb932d2012-01-24 18:04:12 -0800911 if (MSM_PM_DEBUG_IDLE_CLK & msm_pm_debug_mask)
912 clock_debug_print_enabled();
913
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600914 if (pm_sleep_ops.enter_sleep)
915 ret = pm_sleep_ops.enter_sleep(sleep_delay,
916 msm_pm_idle_rs_limits,
917 true, notify_rpm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700918 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600919 collapsed = msm_pm_power_collapse(true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700920 timer_halted = true;
921
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600922 if (pm_sleep_ops.exit_sleep)
923 pm_sleep_ops.exit_sleep(msm_pm_idle_rs_limits,
924 true, notify_rpm, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700925 }
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600926 msm_pm_timer_exit_idle(timer_halted);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700927 exit_stat = MSM_PM_STAT_IDLE_POWER_COLLAPSE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700928 break;
929 }
930
931 default:
932 __WARN();
933 goto cpuidle_enter_bail;
934 }
935
936 time = ktime_to_ns(ktime_get()) - time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700937 msm_pm_add_stat(exit_stat, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700938
939 do_div(time, 1000);
940 return (int) time;
941
942cpuidle_enter_bail:
943 return 0;
944}
945
Anji Jonnalae3406bc2013-04-22 10:01:59 +0530946int msm_pm_wait_cpu_shutdown(unsigned int cpu)
947{
948 int timeout = 10;
949
950 if (!msm_pm_slp_sts)
951 return 0;
952 if (!msm_pm_slp_sts[cpu].base_addr)
953 return 0;
954 while (timeout--) {
955 /*
956 * Check for the SPM of the core being hotplugged to set
957 * its sleep state.The SPM sleep state indicates that the
958 * core has been power collapsed.
959 */
960 int acc_sts = __raw_readl(msm_pm_slp_sts[cpu].base_addr);
961
962 if (acc_sts & msm_pm_slp_sts[cpu].mask)
963 return 0;
964 udelay(100);
965 }
966
967 pr_info("%s(): Timed out waiting for CPU %u SPM to enter sleep state",
968 __func__, cpu);
969 return -EBUSY;
970}
971
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700972void msm_pm_cpu_enter_lowpower(unsigned int cpu)
973{
974 int i;
975 bool allow[MSM_PM_SLEEP_MODE_NR];
976
977 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
978 struct msm_pm_platform_data *mode;
979
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600980 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700981 allow[i] = mode->suspend_supported && mode->suspend_enabled;
982 }
983
984 if (MSM_PM_DEBUG_HOTPLUG & msm_pm_debug_mask)
985 pr_notice("CPU%u: %s: shutting down cpu\n", cpu, __func__);
986
Matt Wagantall5375aa72012-07-02 19:59:51 -0700987 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700988 msm_pm_power_collapse(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -0700989 else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700990 msm_pm_power_collapse_standalone(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -0700991 else if (allow[MSM_PM_SLEEP_MODE_RETENTION])
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600992 msm_pm_retention();
Stephen Boydbda74272012-08-09 14:01:27 -0700993 else
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700994 msm_pm_swfi();
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700995}
996
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700997static int msm_pm_enter(suspend_state_t state)
998{
999 bool allow[MSM_PM_SLEEP_MODE_NR];
1000 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001001 int64_t period = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301002 int64_t time = msm_pm_timer_enter_suspend(&period);
Girish Mahadevan12aaabe2012-08-17 16:48:05 -06001003 struct msm_pm_time_params time_param;
1004
1005 time_param.latency_us = -1;
1006 time_param.sleep_us = -1;
1007 time_param.next_event_us = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001008
1009 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1010 pr_info("%s\n", __func__);
1011
1012 if (smp_processor_id()) {
1013 __WARN();
1014 goto enter_exit;
1015 }
1016
1017
1018 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
1019 struct msm_pm_platform_data *mode;
1020
Praveen Chidambaram42da9d22012-03-30 12:16:34 -06001021 mode = &msm_pm_sleep_modes[MSM_PM_MODE(0, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001022 allow[i] = mode->suspend_supported && mode->suspend_enabled;
1023 }
1024
1025 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001026 void *rs_limits = NULL;
1027 int ret = -ENODEV;
1028 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001029
1030 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1031 pr_info("%s: power collapse\n", __func__);
1032
1033 clock_debug_print_enabled();
1034
1035#ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE
1036 if (msm_pm_sleep_time_override > 0) {
1037 int64_t ns = NSEC_PER_SEC *
1038 (int64_t) msm_pm_sleep_time_override;
1039 msm_pm_set_max_sleep_time(ns);
1040 msm_pm_sleep_time_override = 0;
1041 }
1042#endif /* CONFIG_MSM_SLEEP_TIME_OVERRIDE */
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001043 if (pm_sleep_ops.lowest_limits)
1044 rs_limits = pm_sleep_ops.lowest_limits(false,
Girish Mahadevan12aaabe2012-08-17 16:48:05 -06001045 MSM_PM_SLEEP_MODE_POWER_COLLAPSE, &time_param, &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001046
1047 if (rs_limits) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001048 if (pm_sleep_ops.enter_sleep)
1049 ret = pm_sleep_ops.enter_sleep(
1050 msm_pm_max_sleep_time,
1051 rs_limits, false, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001052 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -06001053 int collapsed = msm_pm_power_collapse(false);
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001054 if (pm_sleep_ops.exit_sleep) {
1055 pm_sleep_ops.exit_sleep(rs_limits,
1056 false, true, collapsed);
1057 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001058 }
1059 } else {
1060 pr_err("%s: cannot find the lowest power limit\n",
1061 __func__);
1062 }
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301063 time = msm_pm_timer_exit_suspend(time, period);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001064 msm_pm_add_stat(MSM_PM_STAT_SUSPEND, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001065 } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
1066 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1067 pr_info("%s: standalone power collapse\n", __func__);
1068 msm_pm_power_collapse_standalone(false);
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -06001069 } else if (allow[MSM_PM_SLEEP_MODE_RETENTION]) {
1070 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1071 pr_info("%s: retention\n", __func__);
1072 msm_pm_retention();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001073 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
1074 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1075 pr_info("%s: swfi\n", __func__);
1076 msm_pm_swfi();
1077 }
1078
1079
1080enter_exit:
1081 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1082 pr_info("%s: return\n", __func__);
1083
1084 return 0;
1085}
1086
1087static struct platform_suspend_ops msm_pm_ops = {
1088 .enter = msm_pm_enter,
1089 .valid = suspend_valid_only_mem,
1090};
1091
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001092/******************************************************************************
1093 * Initialization routine
1094 *****************************************************************************/
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001095void msm_pm_set_sleep_ops(struct msm_pm_sleep_ops *ops)
1096{
1097 if (ops)
1098 pm_sleep_ops = *ops;
1099}
1100
Girish Mahadevan63b87262012-08-15 09:21:23 -06001101void __init msm_pm_set_tz_retention_flag(unsigned int flag)
1102{
1103 msm_pm_retention_tz_call = flag;
1104}
1105
Mahesh Sivasubramanian24b2cea2012-03-14 14:50:37 -06001106static int __devinit msm_pc_debug_probe(struct platform_device *pdev)
1107{
1108 struct resource *res = NULL;
1109 int i ;
1110
1111 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1112 if (!res)
1113 goto fail;
1114
1115 msm_pc_debug_counters_phys = res->start;
1116 WARN_ON(resource_size(res) < SZ_64);
Mahesh Sivasubramanian7683d0a2012-10-17 13:39:40 -06001117 msm_pc_debug_counters = devm_ioremap_nocache(&pdev->dev, res->start,
Mahesh Sivasubramanian24b2cea2012-03-14 14:50:37 -06001118 resource_size(res));
1119
1120 if (!msm_pc_debug_counters)
1121 goto fail;
1122
1123 for (i = 0; i < resource_size(res)/4; i++)
1124 __raw_writel(0, msm_pc_debug_counters + i * 4);
1125 return 0;
1126fail:
1127 msm_pc_debug_counters = 0;
1128 msm_pc_debug_counters_phys = 0;
1129 return -EFAULT;
1130}
1131
1132static struct of_device_id msm_pc_debug_table[] = {
1133 {.compatible = "qcom,pc-cntr"},
1134 {},
1135};
Anji Jonnalae3406bc2013-04-22 10:01:59 +05301136static int __devinit msm_cpu_status_probe(struct platform_device *pdev)
1137{
1138 struct msm_pm_sleep_status_data *pdata;
1139 char *key;
1140 u32 cpu;
1141
1142 if (!pdev)
1143 return -EFAULT;
1144
1145 msm_pm_slp_sts =
1146 kzalloc(sizeof(*msm_pm_slp_sts) * num_possible_cpus(),
1147 GFP_KERNEL);
1148
1149 if (!msm_pm_slp_sts)
1150 return -ENOMEM;
1151
1152 if (pdev->dev.of_node) {
1153 struct resource *res;
1154 u32 offset;
1155 int rc;
1156 u32 mask;
1157
1158 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1159 if (!res)
1160 goto fail_free_mem;
1161
1162 key = "qcom,cpu-alias-addr";
1163 rc = of_property_read_u32(pdev->dev.of_node, key, &offset);
1164
1165 if (rc)
1166 goto fail_free_mem;
1167
1168 key = "qcom,sleep-status-mask";
1169 rc = of_property_read_u32(pdev->dev.of_node, key,
1170 &mask);
1171 if (rc)
1172 goto fail_free_mem;
1173
1174 for_each_possible_cpu(cpu) {
1175 msm_pm_slp_sts[cpu].base_addr =
1176 ioremap(res->start + cpu * offset,
1177 resource_size(res));
1178 msm_pm_slp_sts[cpu].mask = mask;
1179
1180 if (!msm_pm_slp_sts[cpu].base_addr)
1181 goto failed_of_node;
1182 }
1183
1184 } else {
1185 pdata = pdev->dev.platform_data;
1186 if (!pdev->dev.platform_data)
1187 goto fail_free_mem;
1188
1189 for_each_possible_cpu(cpu) {
1190 msm_pm_slp_sts[cpu].base_addr =
1191 pdata->base_addr + cpu * pdata->cpu_offset;
1192 msm_pm_slp_sts[cpu].mask = pdata->mask;
1193 }
1194 }
1195
1196 return 0;
1197
1198failed_of_node:
1199 pr_info("%s(): Failed to key=%s\n", __func__, key);
1200 for_each_possible_cpu(cpu) {
1201 if (msm_pm_slp_sts[cpu].base_addr)
1202 iounmap(msm_pm_slp_sts[cpu].base_addr);
1203 }
1204fail_free_mem:
1205 kfree(msm_pm_slp_sts);
1206 return -EINVAL;
1207
1208};
1209
1210static struct of_device_id msm_slp_sts_match_tbl[] = {
1211 {.compatible = "qcom,cpu-sleep-status"},
1212 {},
1213};
1214
1215static struct platform_driver msm_cpu_status_driver = {
1216 .probe = msm_cpu_status_probe,
1217 .driver = {
1218 .name = "cpu_slp_status",
1219 .owner = THIS_MODULE,
1220 .of_match_table = msm_slp_sts_match_tbl,
1221 },
1222};
Mahesh Sivasubramanian24b2cea2012-03-14 14:50:37 -06001223
1224static struct platform_driver msm_pc_counter_driver = {
1225 .probe = msm_pc_debug_probe,
1226 .driver = {
1227 .name = "pc-cntr",
1228 .owner = THIS_MODULE,
1229 .of_match_table = msm_pc_debug_table,
1230 },
1231};
1232
Praveen Chidambaram0ed2feb2013-02-26 21:05:41 -07001233static int __init msm_pm_setup_saved_state(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001234{
1235 pgd_t *pc_pgd;
1236 pmd_t *pmd;
1237 unsigned long pmdval;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001238 unsigned long exit_phys;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301239
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001240 /* Page table for cores to come back up safely. */
Mahesh Sivasubramanian24b2cea2012-03-14 14:50:37 -06001241
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001242 pc_pgd = pgd_alloc(&init_mm);
1243 if (!pc_pgd)
1244 return -ENOMEM;
1245
Steve Mucklef132c6c2012-06-06 18:30:57 -07001246 exit_phys = virt_to_phys(msm_pm_collapse_exit);
1247
1248 pmd = pmd_offset(pud_offset(pc_pgd + pgd_index(exit_phys),exit_phys),
1249 exit_phys);
1250 pmdval = (exit_phys & PGDIR_MASK) |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001251 PMD_TYPE_SECT | PMD_SECT_AP_WRITE;
1252 pmd[0] = __pmd(pmdval);
1253 pmd[1] = __pmd(pmdval + (1 << (PGDIR_SHIFT - 1)));
1254
Steve Mucklefcece052012-02-18 20:09:58 -08001255 msm_saved_state_phys =
1256 allocate_contiguous_ebi_nomap(CPU_SAVED_STATE_SIZE *
1257 num_possible_cpus(), 4);
1258 if (!msm_saved_state_phys)
1259 return -ENOMEM;
1260 msm_saved_state = ioremap_nocache(msm_saved_state_phys,
1261 CPU_SAVED_STATE_SIZE *
1262 num_possible_cpus());
1263 if (!msm_saved_state)
1264 return -ENOMEM;
1265
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001266 /* It is remotely possible that the code in msm_pm_collapse_exit()
1267 * which turns on the MMU with this mapping is in the
1268 * next even-numbered megabyte beyond the
1269 * start of msm_pm_collapse_exit().
1270 * Map this megabyte in as well.
1271 */
1272 pmd[2] = __pmd(pmdval + (2 << (PGDIR_SHIFT - 1)));
1273 flush_pmd_entry(pmd);
1274 msm_pm_pc_pgd = virt_to_phys(pc_pgd);
Steve Muckle730ad7a2012-02-21 15:26:37 -08001275 clean_caches((unsigned long)&msm_pm_pc_pgd, sizeof(msm_pm_pc_pgd),
1276 virt_to_phys(&msm_pm_pc_pgd));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001277
Praveen Chidambaram0ed2feb2013-02-26 21:05:41 -07001278 return 0;
1279}
1280core_initcall(msm_pm_setup_saved_state);
1281
1282static int __init msm_pm_init(void)
1283{
Anji Jonnalae3406bc2013-04-22 10:01:59 +05301284 int rc;
1285
Praveen Chidambaram0ed2feb2013-02-26 21:05:41 -07001286 enum msm_pm_time_stats_id enable_stats[] = {
1287 MSM_PM_STAT_IDLE_WFI,
1288 MSM_PM_STAT_RETENTION,
1289 MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE,
1290 MSM_PM_STAT_IDLE_POWER_COLLAPSE,
1291 MSM_PM_STAT_SUSPEND,
1292 };
1293
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001294 msm_pm_mode_sysfs_add();
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301295 msm_pm_add_stats(enable_stats, ARRAY_SIZE(enable_stats));
1296
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001297 suspend_set_ops(&msm_pm_ops);
Praveen Chidambaram79f3b4a2012-09-18 19:48:29 -06001298 msm_pm_target_init();
Venkat Devarasetty46881f12013-05-15 15:36:58 +05301299 hrtimer_init(&pm_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001300 msm_cpuidle_init();
Mahesh Sivasubramanian24b2cea2012-03-14 14:50:37 -06001301 platform_driver_register(&msm_pc_counter_driver);
Anji Jonnalae3406bc2013-04-22 10:01:59 +05301302 rc = platform_driver_register(&msm_cpu_status_driver);
1303
1304 if (rc) {
1305 pr_err("%s(): failed to register driver %s\n", __func__,
1306 msm_cpu_status_driver.driver.name);
1307 return rc;
1308 }
1309
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001310
1311 return 0;
1312}
1313
1314late_initcall(msm_pm_init);