blob: 123371d669f4b53bf000b1d645c08d4480affc54 [file] [log] [blame]
Karthik Parsha6fb932d2012-01-24 18:04:12 -08001/* Copyright (c) 2010-2012, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13
14#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/init.h>
17#include <linux/completion.h>
18#include <linux/cpuidle.h>
19#include <linux/interrupt.h>
20#include <linux/io.h>
21#include <linux/ktime.h>
22#include <linux/pm.h>
Steve Mucklef132c6c2012-06-06 18:30:57 -070023#include <linux/pm_qos.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070024#include <linux/smp.h>
25#include <linux/suspend.h>
26#include <linux/tick.h>
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -060027#include <linux/platform_device.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070028#include <mach/msm_iomap.h>
Praveen Chidambaram192979f2012-04-25 18:30:23 -060029#include <mach/socinfo.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070030#include <mach/system.h>
Girish Mahadevand27ca4a2012-08-15 09:21:23 -060031#include <mach/scm.h>
32#include <mach/socinfo.h>
Praveen Chidambaramc594a092012-09-18 19:48:29 -060033#include <mach/msm-krait-l2-accessors.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070034#include <asm/cacheflush.h>
35#include <asm/hardware/gic.h>
36#include <asm/pgtable.h>
37#include <asm/pgalloc.h>
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>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070054
55/******************************************************************************
56 * Debug Definitions
57 *****************************************************************************/
58
59enum {
60 MSM_PM_DEBUG_SUSPEND = BIT(0),
61 MSM_PM_DEBUG_POWER_COLLAPSE = BIT(1),
62 MSM_PM_DEBUG_SUSPEND_LIMITS = BIT(2),
63 MSM_PM_DEBUG_CLOCK = BIT(3),
64 MSM_PM_DEBUG_RESET_VECTOR = BIT(4),
Karthik Parsha6fb932d2012-01-24 18:04:12 -080065 MSM_PM_DEBUG_IDLE_CLK = BIT(5),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070066 MSM_PM_DEBUG_IDLE = BIT(6),
67 MSM_PM_DEBUG_IDLE_LIMITS = BIT(7),
68 MSM_PM_DEBUG_HOTPLUG = BIT(8),
69};
70
71static int msm_pm_debug_mask = 1;
72module_param_named(
73 debug_mask, msm_pm_debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP
74);
Girish Mahadevand27ca4a2012-08-15 09:21:23 -060075static int msm_pm_retention_tz_call;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070076
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070077/******************************************************************************
78 * Sleep Modes and Parameters
79 *****************************************************************************/
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070080enum {
81 MSM_PM_MODE_ATTR_SUSPEND,
82 MSM_PM_MODE_ATTR_IDLE,
83 MSM_PM_MODE_ATTR_NR,
84};
85
Girish Mahadevand27ca4a2012-08-15 09:21:23 -060086#define SCM_L2_RETENTION (0x2)
87#define SCM_CMD_TERMINATE_PC (0x2)
88
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070089static char *msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_NR] = {
90 [MSM_PM_MODE_ATTR_SUSPEND] = "suspend_enabled",
91 [MSM_PM_MODE_ATTR_IDLE] = "idle_enabled",
92};
93
94struct msm_pm_kobj_attribute {
95 unsigned int cpu;
96 struct kobj_attribute ka;
97};
98
99#define GET_CPU_OF_ATTR(attr) \
100 (container_of(attr, struct msm_pm_kobj_attribute, ka)->cpu)
101
102struct msm_pm_sysfs_sleep_mode {
103 struct kobject *kobj;
104 struct attribute_group attr_group;
105 struct attribute *attrs[MSM_PM_MODE_ATTR_NR + 1];
106 struct msm_pm_kobj_attribute kas[MSM_PM_MODE_ATTR_NR];
107};
108
109static char *msm_pm_sleep_mode_labels[MSM_PM_SLEEP_MODE_NR] = {
110 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE] = "power_collapse",
111 [MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT] = "wfi",
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600112 [MSM_PM_SLEEP_MODE_RETENTION] = "retention",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700113 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE] =
114 "standalone_power_collapse",
115};
116
Girish Mahadevan55944992012-10-26 11:03:07 -0600117static struct msm_pm_init_data_type msm_pm_init_data;
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600118static struct hrtimer pm_hrtimer;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600119static struct msm_pm_sleep_ops pm_sleep_ops;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700120/*
121 * Write out the attribute.
122 */
123static ssize_t msm_pm_mode_attr_show(
124 struct kobject *kobj, struct kobj_attribute *attr, char *buf)
125{
126 int ret = -EINVAL;
127 int i;
128
129 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
130 struct kernel_param kp;
131 unsigned int cpu;
132 struct msm_pm_platform_data *mode;
133
134 if (msm_pm_sleep_mode_labels[i] == NULL)
135 continue;
136
137 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
138 continue;
139
140 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600141 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700142
143 if (!strcmp(attr->attr.name,
144 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
145 u32 arg = mode->suspend_enabled;
146 kp.arg = &arg;
147 ret = param_get_ulong(buf, &kp);
148 } else if (!strcmp(attr->attr.name,
149 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
150 u32 arg = mode->idle_enabled;
151 kp.arg = &arg;
152 ret = param_get_ulong(buf, &kp);
153 }
154
155 break;
156 }
157
158 if (ret > 0) {
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600159 strlcat(buf, "\n", PAGE_SIZE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700160 ret++;
161 }
162
163 return ret;
164}
165
166/*
167 * Read in the new attribute value.
168 */
169static ssize_t msm_pm_mode_attr_store(struct kobject *kobj,
170 struct kobj_attribute *attr, const char *buf, size_t count)
171{
172 int ret = -EINVAL;
173 int i;
174
175 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
176 struct kernel_param kp;
177 unsigned int cpu;
178 struct msm_pm_platform_data *mode;
179
180 if (msm_pm_sleep_mode_labels[i] == NULL)
181 continue;
182
183 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
184 continue;
185
186 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600187 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700188
189 if (!strcmp(attr->attr.name,
190 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
191 kp.arg = &mode->suspend_enabled;
192 ret = param_set_byte(buf, &kp);
193 } else if (!strcmp(attr->attr.name,
194 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
195 kp.arg = &mode->idle_enabled;
196 ret = param_set_byte(buf, &kp);
197 }
198
199 break;
200 }
201
202 return ret ? ret : count;
203}
204
205/*
206 * Add sysfs entries for one cpu.
207 */
208static int __init msm_pm_mode_sysfs_add_cpu(
209 unsigned int cpu, struct kobject *modes_kobj)
210{
211 char cpu_name[8];
212 struct kobject *cpu_kobj;
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600213 struct msm_pm_sysfs_sleep_mode *mode = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700214 int i, j, k;
215 int ret;
216
217 snprintf(cpu_name, sizeof(cpu_name), "cpu%u", cpu);
218 cpu_kobj = kobject_create_and_add(cpu_name, modes_kobj);
219 if (!cpu_kobj) {
220 pr_err("%s: cannot create %s kobject\n", __func__, cpu_name);
221 ret = -ENOMEM;
222 goto mode_sysfs_add_cpu_exit;
223 }
224
225 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
226 int idx = MSM_PM_MODE(cpu, i);
227
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600228 if ((!msm_pm_sleep_modes[idx].suspend_supported)
229 && (!msm_pm_sleep_modes[idx].idle_supported))
230 continue;
231
232 if (!msm_pm_sleep_mode_labels[i] ||
233 !msm_pm_sleep_mode_labels[i][0])
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700234 continue;
235
236 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
237 if (!mode) {
238 pr_err("%s: cannot allocate memory for attributes\n",
239 __func__);
240 ret = -ENOMEM;
241 goto mode_sysfs_add_cpu_exit;
242 }
243
244 mode->kobj = kobject_create_and_add(
245 msm_pm_sleep_mode_labels[i], cpu_kobj);
246 if (!mode->kobj) {
247 pr_err("%s: cannot create kobject\n", __func__);
248 ret = -ENOMEM;
249 goto mode_sysfs_add_cpu_exit;
250 }
251
252 for (k = 0, j = 0; k < MSM_PM_MODE_ATTR_NR; k++) {
253 if ((k == MSM_PM_MODE_ATTR_IDLE) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600254 !msm_pm_sleep_modes[idx].idle_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700255 continue;
256 if ((k == MSM_PM_MODE_ATTR_SUSPEND) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600257 !msm_pm_sleep_modes[idx].suspend_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700258 continue;
Stephen Boydd2059c32012-07-03 15:11:15 -0700259 sysfs_attr_init(&mode->kas[j].ka.attr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700260 mode->kas[j].cpu = cpu;
261 mode->kas[j].ka.attr.mode = 0644;
262 mode->kas[j].ka.show = msm_pm_mode_attr_show;
263 mode->kas[j].ka.store = msm_pm_mode_attr_store;
264 mode->kas[j].ka.attr.name = msm_pm_mode_attr_labels[k];
265 mode->attrs[j] = &mode->kas[j].ka.attr;
266 j++;
267 }
268 mode->attrs[j] = NULL;
269
270 mode->attr_group.attrs = mode->attrs;
271 ret = sysfs_create_group(mode->kobj, &mode->attr_group);
272 if (ret) {
273 pr_err("%s: cannot create kobject attribute group\n",
274 __func__);
275 goto mode_sysfs_add_cpu_exit;
276 }
277 }
278
279 ret = 0;
280
281mode_sysfs_add_cpu_exit:
Praveen Chidambaramd5ac2d32011-10-24 14:30:27 -0600282 if (ret) {
Praveen Chidambaram2cfda632011-10-11 16:58:09 -0600283 if (mode && mode->kobj)
284 kobject_del(mode->kobj);
285 kfree(mode);
286 }
287
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700288 return ret;
289}
290
291/*
292 * Add sysfs entries for the sleep modes.
293 */
294static int __init msm_pm_mode_sysfs_add(void)
295{
296 struct kobject *module_kobj;
297 struct kobject *modes_kobj;
298 unsigned int cpu;
299 int ret;
300
301 module_kobj = kset_find_obj(module_kset, KBUILD_MODNAME);
302 if (!module_kobj) {
303 pr_err("%s: cannot find kobject for module %s\n",
304 __func__, KBUILD_MODNAME);
305 ret = -ENOENT;
306 goto mode_sysfs_add_exit;
307 }
308
309 modes_kobj = kobject_create_and_add("modes", module_kobj);
310 if (!modes_kobj) {
311 pr_err("%s: cannot create modes kobject\n", __func__);
312 ret = -ENOMEM;
313 goto mode_sysfs_add_exit;
314 }
315
316 for_each_possible_cpu(cpu) {
317 ret = msm_pm_mode_sysfs_add_cpu(cpu, modes_kobj);
318 if (ret)
319 goto mode_sysfs_add_exit;
320 }
321
322 ret = 0;
323
324mode_sysfs_add_exit:
325 return ret;
326}
327
328/******************************************************************************
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700329 * Configure Hardware before/after Low Power Mode
330 *****************************************************************************/
331
332/*
333 * Configure hardware registers in preparation for Apps power down.
334 */
335static void msm_pm_config_hw_before_power_down(void)
336{
337 return;
338}
339
340/*
341 * Clear hardware registers after Apps powers up.
342 */
343static void msm_pm_config_hw_after_power_up(void)
344{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700345}
346
347/*
348 * Configure hardware registers in preparation for SWFI.
349 */
350static void msm_pm_config_hw_before_swfi(void)
351{
352 return;
353}
354
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600355/*
356 * Configure/Restore hardware registers in preparation for Retention.
357 */
358
359static void msm_pm_config_hw_after_retention(void)
360{
361 int ret;
362 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
363 WARN_ON(ret);
364}
365
366static void msm_pm_config_hw_before_retention(void)
367{
368 return;
369}
370
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700371
372/******************************************************************************
373 * Suspend Max Sleep Time
374 *****************************************************************************/
375
376#ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE
377static int msm_pm_sleep_time_override;
378module_param_named(sleep_time_override,
379 msm_pm_sleep_time_override, int, S_IRUGO | S_IWUSR | S_IWGRP);
380#endif
381
382#define SCLK_HZ (32768)
383#define MSM_PM_SLEEP_TICK_LIMIT (0x6DDD000)
384
385static uint32_t msm_pm_max_sleep_time;
386
387/*
388 * Convert time from nanoseconds to slow clock ticks, then cap it to the
389 * specified limit
390 */
391static int64_t msm_pm_convert_and_cap_time(int64_t time_ns, int64_t limit)
392{
393 do_div(time_ns, NSEC_PER_SEC / SCLK_HZ);
394 return (time_ns > limit) ? limit : time_ns;
395}
396
397/*
398 * Set the sleep time for suspend. 0 means infinite sleep time.
399 */
400void msm_pm_set_max_sleep_time(int64_t max_sleep_time_ns)
401{
402 if (max_sleep_time_ns == 0) {
403 msm_pm_max_sleep_time = 0;
404 } else {
405 msm_pm_max_sleep_time = (uint32_t)msm_pm_convert_and_cap_time(
406 max_sleep_time_ns, MSM_PM_SLEEP_TICK_LIMIT);
407
408 if (msm_pm_max_sleep_time == 0)
409 msm_pm_max_sleep_time = 1;
410 }
411
412 if (msm_pm_debug_mask & MSM_PM_DEBUG_SUSPEND)
413 pr_info("%s: Requested %lld ns Giving %u sclk ticks\n",
414 __func__, max_sleep_time_ns, msm_pm_max_sleep_time);
415}
416EXPORT_SYMBOL(msm_pm_set_max_sleep_time);
417
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600418struct reg_data {
419 uint32_t reg;
420 uint32_t val;
421};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700422
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600423static struct reg_data reg_saved_state[] = {
424 { .reg = 0x4501, },
425 { .reg = 0x5501, },
426 { .reg = 0x6501, },
427 { .reg = 0x7501, },
428 { .reg = 0x0500, },
429};
430
431static unsigned int active_vdd;
432static bool msm_pm_save_cp15;
433static const unsigned int pc_vdd = 0x98;
434
435static void msm_pm_save_cpu_reg(void)
436{
437 int i;
438
439 /* Only on core0 */
440 if (smp_processor_id())
441 return;
442
443 /**
444 * On some targets, L2 PC will turn off may reset the core
445 * configuration for the mux and the default may not make the core
446 * happy when it resumes.
447 * Save the active vdd, and set the core vdd to QSB max vdd, so that
448 * when the core resumes, it is capable of supporting the current QSB
449 * rate. Then restore the active vdd before switching the acpuclk rate.
450 */
451 if (msm_pm_get_l2_flush_flag() == 1) {
452 active_vdd = msm_spm_get_vdd(0);
453 for (i = 0; i < ARRAY_SIZE(reg_saved_state); i++)
454 reg_saved_state[i].val =
455 get_l2_indirect_reg(reg_saved_state[i].reg);
456 msm_spm_set_vdd(0, pc_vdd);
457 }
458}
459
460static void msm_pm_restore_cpu_reg(void)
461{
462 int i;
463
464 /* Only on core0 */
465 if (smp_processor_id())
466 return;
467
468 if (msm_pm_get_l2_flush_flag() == 1) {
469 for (i = 0; i < ARRAY_SIZE(reg_saved_state); i++)
470 set_l2_indirect_reg(reg_saved_state[i].reg,
471 reg_saved_state[i].val);
472 msm_spm_set_vdd(0, active_vdd);
473 }
474}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700475
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600476static void *msm_pm_idle_rs_limits;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700477
478static void msm_pm_swfi(void)
479{
480 msm_pm_config_hw_before_swfi();
481 msm_arch_idle();
482}
483
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600484
485static void msm_pm_retention(void)
486{
487 int ret = 0;
488
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600489 msm_pm_config_hw_before_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600490 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_POWER_RETENTION, false);
491 WARN_ON(ret);
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600492
493 if (msm_pm_retention_tz_call)
494 scm_call_atomic1(SCM_SVC_BOOT, SCM_CMD_TERMINATE_PC,
495 SCM_L2_RETENTION);
496 else
497 msm_arch_idle();
498
499 msm_pm_config_hw_after_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600500}
501
Stephen Boydb29750d2012-02-21 01:21:32 -0800502static bool __ref msm_pm_spm_power_collapse(
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700503 unsigned int cpu, bool from_idle, bool notify_rpm)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700504{
505 void *entry;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600506 bool collapsed = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700507 int ret;
Rohit Vaswanie78dfb62012-02-21 10:29:29 -0800508 unsigned int saved_gic_cpu_ctrl;
509
510 saved_gic_cpu_ctrl = readl_relaxed(MSM_QGIC_CPU_BASE + GIC_CPU_CTRL);
511 mb();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700512
513 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
514 pr_info("CPU%u: %s: notify_rpm %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700515 cpu, __func__, (int) notify_rpm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700516
517 ret = msm_spm_set_low_power_mode(
518 MSM_SPM_MODE_POWER_COLLAPSE, notify_rpm);
519 WARN_ON(ret);
520
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700521 entry = (!cpu || from_idle) ?
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700522 msm_pm_collapse_exit : msm_secondary_startup;
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700523 msm_pm_boot_config_before_pc(cpu, virt_to_phys(entry));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700524
525 if (MSM_PM_DEBUG_RESET_VECTOR & msm_pm_debug_mask)
526 pr_info("CPU%u: %s: program vector to %p\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700527 cpu, __func__, entry);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700528
529#ifdef CONFIG_VFP
Steve Mucklef132c6c2012-06-06 18:30:57 -0700530 vfp_pm_suspend();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700531#endif
Girish Mahadevan55944992012-10-26 11:03:07 -0600532 collapsed = msm_pm_collapse();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700533
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700534 msm_pm_boot_config_after_pc(cpu);
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -0600535
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700536 if (collapsed) {
537#ifdef CONFIG_VFP
Steve Mucklef132c6c2012-06-06 18:30:57 -0700538 vfp_pm_resume();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700539#endif
540 cpu_init();
541 writel(0xF0, MSM_QGIC_CPU_BASE + GIC_CPU_PRIMASK);
Rohit Vaswanie78dfb62012-02-21 10:29:29 -0800542 writel_relaxed(saved_gic_cpu_ctrl,
543 MSM_QGIC_CPU_BASE + GIC_CPU_CTRL);
544 mb();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700545 local_fiq_enable();
546 }
547
548 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
549 pr_info("CPU%u: %s: msm_pm_collapse returned, collapsed %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700550 cpu, __func__, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700551
552 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
553 WARN_ON(ret);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600554 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700555}
556
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600557static bool msm_pm_power_collapse_standalone(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700558{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700559 unsigned int cpu = smp_processor_id();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700560 unsigned int avsdscr_setting;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600561 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700562
563 avsdscr_setting = avs_get_avsdscr();
564 avs_disable();
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700565 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700566 avs_reset_delays(avsdscr_setting);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600567 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700568}
569
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600570static bool msm_pm_power_collapse(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700571{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700572 unsigned int cpu = smp_processor_id();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700573 unsigned long saved_acpuclk_rate;
574 unsigned int avsdscr_setting;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600575 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700576
577 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
578 pr_info("CPU%u: %s: idle %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700579 cpu, __func__, (int)from_idle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700580
581 msm_pm_config_hw_before_power_down();
582 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700583 pr_info("CPU%u: %s: pre power down\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700584
585 avsdscr_setting = avs_get_avsdscr();
586 avs_disable();
587
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700588 if (cpu_online(cpu))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700589 saved_acpuclk_rate = acpuclk_power_collapse();
590 else
591 saved_acpuclk_rate = 0;
592
593 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
594 pr_info("CPU%u: %s: change clock rate (old rate = %lu)\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700595 cpu, __func__, saved_acpuclk_rate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700596
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600597 if (msm_pm_save_cp15)
598 msm_pm_save_cpu_reg();
599
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700600 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700601
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600602 if (msm_pm_save_cp15)
603 msm_pm_restore_cpu_reg();
604
Girish Mahadevan4e025d92012-02-29 13:26:51 -0700605 if (cpu_online(cpu)) {
606 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
607 pr_info("CPU%u: %s: restore clock rate to %lu\n",
608 cpu, __func__, saved_acpuclk_rate);
609 if (acpuclk_set_rate(cpu, saved_acpuclk_rate, SETRATE_PC) < 0)
610 pr_err("CPU%u: %s: failed to restore clock rate(%lu)\n",
611 cpu, __func__, saved_acpuclk_rate);
612 } else {
613 unsigned int gic_dist_enabled;
614 unsigned int gic_dist_pending;
615 gic_dist_enabled = readl_relaxed(
616 MSM_QGIC_DIST_BASE + GIC_DIST_ENABLE_CLEAR);
617 gic_dist_pending = readl_relaxed(
618 MSM_QGIC_DIST_BASE + GIC_DIST_PENDING_SET);
619 mb();
620 gic_dist_pending &= gic_dist_enabled;
621
622 if (gic_dist_pending)
623 pr_err("CPU %d interrupted during hotplug.Pending int 0x%x\n",
624 cpu, gic_dist_pending);
625 }
626
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700627
628 avs_reset_delays(avsdscr_setting);
629 msm_pm_config_hw_after_power_up();
630 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700631 pr_info("CPU%u: %s: post power up\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700632
633 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700634 pr_info("CPU%u: %s: return\n", cpu, __func__);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600635 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700636}
637
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600638static void msm_pm_target_init(void)
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600639{
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600640 if (cpu_is_apq8064())
641 msm_pm_save_cp15 = true;
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600642}
643
644static int64_t msm_pm_timer_enter_idle(void)
645{
Girish Mahadevan55944992012-10-26 11:03:07 -0600646 if (msm_pm_init_data.use_sync_timer)
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600647 return ktime_to_ns(tick_nohz_get_sleep_length());
648
649 return msm_timer_enter_idle();
650}
651
652static void msm_pm_timer_exit_idle(bool timer_halted)
653{
Girish Mahadevan55944992012-10-26 11:03:07 -0600654 if (msm_pm_init_data.use_sync_timer)
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600655 return;
656
657 msm_timer_exit_idle((int) timer_halted);
658}
659
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530660static int64_t msm_pm_timer_enter_suspend(int64_t *period)
661{
Anji Jonnalac02367a2012-07-01 02:56:11 +0530662 int64_t time = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530663
Girish Mahadevan55944992012-10-26 11:03:07 -0600664 if (msm_pm_init_data.use_sync_timer)
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530665 return sched_clock();
666
667 time = msm_timer_get_sclk_time(period);
668 if (!time)
669 pr_err("%s: Unable to read sclk.\n", __func__);
670
671 return time;
672}
673
674static int64_t msm_pm_timer_exit_suspend(int64_t time, int64_t period)
675{
Girish Mahadevan55944992012-10-26 11:03:07 -0600676 if (msm_pm_init_data.use_sync_timer)
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530677 return sched_clock() - time;
678
679 if (time != 0) {
680 int64_t end_time = msm_timer_get_sclk_time(NULL);
681 if (end_time != 0) {
682 time = end_time - time;
683 if (time < 0)
684 time += period;
685 } else
686 time = 0;
687 }
688
689 return time;
690}
691
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600692/**
693 * pm_hrtimer_cb() : Callback function for hrtimer created if the
694 * core needs to be awake to handle an event.
695 * @hrtimer : Pointer to hrtimer
696 */
697static enum hrtimer_restart pm_hrtimer_cb(struct hrtimer *hrtimer)
698{
699 return HRTIMER_NORESTART;
700}
701
702/**
703 * msm_pm_set_timer() : Set an hrtimer to wakeup the core in time
704 * to handle an event.
705 */
706static void msm_pm_set_timer(uint32_t modified_time_us)
707{
708 u64 modified_time_ns = modified_time_us * NSEC_PER_USEC;
709 ktime_t modified_ktime = ns_to_ktime(modified_time_ns);
710 pm_hrtimer.function = pm_hrtimer_cb;
711 hrtimer_start(&pm_hrtimer, modified_ktime, HRTIMER_MODE_ABS);
712}
713
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700714/******************************************************************************
715 * External Idle/Suspend Functions
716 *****************************************************************************/
717
718void arch_idle(void)
719{
720 return;
721}
722
Steve Mucklef132c6c2012-06-06 18:30:57 -0700723int msm_pm_idle_prepare(struct cpuidle_device *dev,
724 struct cpuidle_driver *drv, int index)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700725{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700726 int i;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700727 unsigned int power_usage = -1;
728 int ret = 0;
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600729 uint32_t modified_time_us = 0;
730 struct msm_pm_time_params time_param;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700731
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600732 time_param.latency_us =
733 (uint32_t) pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
734 time_param.sleep_us =
735 (uint32_t) (ktime_to_us(tick_nohz_get_sleep_length())
736 & UINT_MAX);
737 time_param.modified_time_us = 0;
738
739 if (!dev->cpu)
740 time_param.next_event_us =
741 (uint32_t) (ktime_to_us(get_next_event_time())
742 & UINT_MAX);
743 else
744 time_param.next_event_us = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700745
746 for (i = 0; i < dev->state_count; i++) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700747 struct cpuidle_state *state = &drv->states[i];
748 struct cpuidle_state_usage *st_usage = &dev->states_usage[i];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700749 enum msm_pm_sleep_mode mode;
750 bool allow;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600751 void *rs_limits = NULL;
752 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700753 int idx;
754
Steve Mucklef132c6c2012-06-06 18:30:57 -0700755 mode = (enum msm_pm_sleep_mode) cpuidle_get_statedata(st_usage);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700756 idx = MSM_PM_MODE(dev->cpu, mode);
757
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600758 allow = msm_pm_sleep_modes[idx].idle_enabled &&
759 msm_pm_sleep_modes[idx].idle_supported;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700760
761 switch (mode) {
762 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
Priyanka Mathur68a9bc52012-10-03 15:15:01 -0700763 if (num_online_cpus() > 1) {
764 allow = false;
765 break;
766 }
767 /* fall through */
Mahesh Sivasubramanianba7e01c2012-09-04 18:22:51 -0600768 case MSM_PM_SLEEP_MODE_RETENTION:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700769 if (!allow)
770 break;
771
Priyanka Mathur68a9bc52012-10-03 15:15:01 -0700772 if (msm_pm_retention_tz_call &&
773 num_online_cpus() > 1) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700774 allow = false;
775 break;
776 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700777 /* fall through */
778
779 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
780 if (!allow)
781 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700782 /* fall through */
783
784 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
785 if (!allow)
786 break;
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600787 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700788
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600789 if (pm_sleep_ops.lowest_limits)
790 rs_limits = pm_sleep_ops.lowest_limits(true,
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600791 mode, &time_param, &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700792
793 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
794 pr_info("CPU%u: %s: %s, latency %uus, "
795 "sleep %uus, limit %p\n",
796 dev->cpu, __func__, state->desc,
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600797 time_param.latency_us,
798 time_param.sleep_us, rs_limits);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700799
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700800 if (!rs_limits)
801 allow = false;
802 break;
803
804 default:
805 allow = false;
806 break;
807 }
808
809 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
810 pr_info("CPU%u: %s: allow %s: %d\n",
811 dev->cpu, __func__, state->desc, (int)allow);
812
813 if (allow) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700814 if (power < power_usage) {
815 power_usage = power;
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600816 modified_time_us = time_param.modified_time_us;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700817 ret = mode;
818 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700819
820 if (MSM_PM_SLEEP_MODE_POWER_COLLAPSE == mode)
821 msm_pm_idle_rs_limits = rs_limits;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700822 }
823 }
824
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600825 if (modified_time_us && !dev->cpu)
826 msm_pm_set_timer(modified_time_us);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700827 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700828}
829
830int msm_pm_idle_enter(enum msm_pm_sleep_mode sleep_mode)
831{
832 int64_t time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700833 int exit_stat;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700834
835 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
836 pr_info("CPU%u: %s: mode %d\n",
837 smp_processor_id(), __func__, sleep_mode);
838
839 time = ktime_to_ns(ktime_get());
840
841 switch (sleep_mode) {
842 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
843 msm_pm_swfi();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700844 exit_stat = MSM_PM_STAT_IDLE_WFI;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700845 break;
846
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600847 case MSM_PM_SLEEP_MODE_RETENTION:
848 msm_pm_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600849 exit_stat = MSM_PM_STAT_RETENTION;
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600850 break;
851
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700852 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
853 msm_pm_power_collapse_standalone(true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700854 exit_stat = MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700855 break;
856
857 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE: {
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600858 int64_t timer_expiration = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700859 bool timer_halted = false;
860 uint32_t sleep_delay;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600861 int ret = -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700862 int notify_rpm =
863 (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600864 int collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700865
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600866 timer_expiration = msm_pm_timer_enter_idle();
867
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700868 sleep_delay = (uint32_t) msm_pm_convert_and_cap_time(
869 timer_expiration, MSM_PM_SLEEP_TICK_LIMIT);
870 if (sleep_delay == 0) /* 0 would mean infinite time */
871 sleep_delay = 1;
872
Karthik Parsha6fb932d2012-01-24 18:04:12 -0800873 if (MSM_PM_DEBUG_IDLE_CLK & msm_pm_debug_mask)
874 clock_debug_print_enabled();
875
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600876 if (pm_sleep_ops.enter_sleep)
877 ret = pm_sleep_ops.enter_sleep(sleep_delay,
878 msm_pm_idle_rs_limits,
879 true, notify_rpm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700880 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600881 collapsed = msm_pm_power_collapse(true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700882 timer_halted = true;
883
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600884 if (pm_sleep_ops.exit_sleep)
885 pm_sleep_ops.exit_sleep(msm_pm_idle_rs_limits,
886 true, notify_rpm, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700887 }
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600888 msm_pm_timer_exit_idle(timer_halted);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700889 exit_stat = MSM_PM_STAT_IDLE_POWER_COLLAPSE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700890 break;
891 }
892
893 default:
894 __WARN();
895 goto cpuidle_enter_bail;
896 }
897
898 time = ktime_to_ns(ktime_get()) - time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700899 msm_pm_add_stat(exit_stat, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700900
901 do_div(time, 1000);
902 return (int) time;
903
904cpuidle_enter_bail:
905 return 0;
906}
907
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700908void msm_pm_cpu_enter_lowpower(unsigned int cpu)
909{
910 int i;
911 bool allow[MSM_PM_SLEEP_MODE_NR];
912
913 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
914 struct msm_pm_platform_data *mode;
915
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600916 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700917 allow[i] = mode->suspend_supported && mode->suspend_enabled;
918 }
919
920 if (MSM_PM_DEBUG_HOTPLUG & msm_pm_debug_mask)
921 pr_notice("CPU%u: %s: shutting down cpu\n", cpu, __func__);
922
Matt Wagantall5375aa72012-07-02 19:59:51 -0700923 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700924 msm_pm_power_collapse(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -0700925 else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700926 msm_pm_power_collapse_standalone(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -0700927 else if (allow[MSM_PM_SLEEP_MODE_RETENTION])
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600928 msm_pm_retention();
Stephen Boydbda74272012-08-09 14:01:27 -0700929 else
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700930 msm_pm_swfi();
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700931}
932
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700933static int msm_pm_enter(suspend_state_t state)
934{
935 bool allow[MSM_PM_SLEEP_MODE_NR];
936 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700937 int64_t period = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530938 int64_t time = msm_pm_timer_enter_suspend(&period);
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600939 struct msm_pm_time_params time_param;
940
941 time_param.latency_us = -1;
942 time_param.sleep_us = -1;
943 time_param.next_event_us = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700944
945 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
946 pr_info("%s\n", __func__);
947
948 if (smp_processor_id()) {
949 __WARN();
950 goto enter_exit;
951 }
952
953
954 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
955 struct msm_pm_platform_data *mode;
956
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600957 mode = &msm_pm_sleep_modes[MSM_PM_MODE(0, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700958 allow[i] = mode->suspend_supported && mode->suspend_enabled;
959 }
960
961 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600962 void *rs_limits = NULL;
963 int ret = -ENODEV;
964 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700965
966 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
967 pr_info("%s: power collapse\n", __func__);
968
969 clock_debug_print_enabled();
970
971#ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE
972 if (msm_pm_sleep_time_override > 0) {
973 int64_t ns = NSEC_PER_SEC *
974 (int64_t) msm_pm_sleep_time_override;
975 msm_pm_set_max_sleep_time(ns);
976 msm_pm_sleep_time_override = 0;
977 }
978#endif /* CONFIG_MSM_SLEEP_TIME_OVERRIDE */
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600979 if (pm_sleep_ops.lowest_limits)
980 rs_limits = pm_sleep_ops.lowest_limits(false,
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600981 MSM_PM_SLEEP_MODE_POWER_COLLAPSE, &time_param, &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700982
983 if (rs_limits) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600984 if (pm_sleep_ops.enter_sleep)
985 ret = pm_sleep_ops.enter_sleep(
986 msm_pm_max_sleep_time,
987 rs_limits, false, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700988 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600989 int collapsed = msm_pm_power_collapse(false);
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600990 if (pm_sleep_ops.exit_sleep) {
991 pm_sleep_ops.exit_sleep(rs_limits,
992 false, true, collapsed);
993 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700994 }
995 } else {
996 pr_err("%s: cannot find the lowest power limit\n",
997 __func__);
998 }
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530999 time = msm_pm_timer_exit_suspend(time, period);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001000 msm_pm_add_stat(MSM_PM_STAT_SUSPEND, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001001 } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
1002 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1003 pr_info("%s: standalone power collapse\n", __func__);
1004 msm_pm_power_collapse_standalone(false);
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -06001005 } else if (allow[MSM_PM_SLEEP_MODE_RETENTION]) {
1006 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1007 pr_info("%s: retention\n", __func__);
1008 msm_pm_retention();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001009 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
1010 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1011 pr_info("%s: swfi\n", __func__);
1012 msm_pm_swfi();
1013 }
1014
1015
1016enter_exit:
1017 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1018 pr_info("%s: return\n", __func__);
1019
1020 return 0;
1021}
1022
1023static struct platform_suspend_ops msm_pm_ops = {
1024 .enter = msm_pm_enter,
1025 .valid = suspend_valid_only_mem,
1026};
1027
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001028/******************************************************************************
1029 * Initialization routine
1030 *****************************************************************************/
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001031void msm_pm_set_sleep_ops(struct msm_pm_sleep_ops *ops)
1032{
1033 if (ops)
1034 pm_sleep_ops = *ops;
1035}
1036
Girish Mahadevand27ca4a2012-08-15 09:21:23 -06001037void __init msm_pm_set_tz_retention_flag(unsigned int flag)
1038{
1039 msm_pm_retention_tz_call = flag;
1040}
1041
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -06001042static int __devinit msm_pc_debug_probe(struct platform_device *pdev)
1043{
1044 struct resource *res = NULL;
1045 int i ;
1046
1047 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1048 if (!res)
1049 goto fail;
1050
1051 msm_pc_debug_counters_phys = res->start;
1052 WARN_ON(resource_size(res) < SZ_64);
Mahesh Sivasubramaniane6d33992012-10-17 13:39:40 -06001053 msm_pc_debug_counters = devm_ioremap_nocache(&pdev->dev, res->start,
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -06001054 resource_size(res));
1055
1056 if (!msm_pc_debug_counters)
1057 goto fail;
1058
1059 for (i = 0; i < resource_size(res)/4; i++)
1060 __raw_writel(0, msm_pc_debug_counters + i * 4);
1061 return 0;
1062fail:
1063 msm_pc_debug_counters = 0;
1064 msm_pc_debug_counters_phys = 0;
1065 return -EFAULT;
1066}
1067
1068static struct of_device_id msm_pc_debug_table[] = {
1069 {.compatible = "qcom,pc-cntr"},
1070 {},
1071};
1072
1073static struct platform_driver msm_pc_counter_driver = {
1074 .probe = msm_pc_debug_probe,
1075 .driver = {
1076 .name = "pc-cntr",
1077 .owner = THIS_MODULE,
1078 .of_match_table = msm_pc_debug_table,
1079 },
1080};
1081
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001082static int __init msm_pm_init(void)
1083{
1084 pgd_t *pc_pgd;
1085 pmd_t *pmd;
1086 unsigned long pmdval;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301087 enum msm_pm_time_stats_id enable_stats[] = {
1088 MSM_PM_STAT_IDLE_WFI,
1089 MSM_PM_STAT_RETENTION,
1090 MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE,
1091 MSM_PM_STAT_IDLE_POWER_COLLAPSE,
1092 MSM_PM_STAT_SUSPEND,
1093 };
Steve Mucklef132c6c2012-06-06 18:30:57 -07001094 unsigned long exit_phys;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301095
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001096 /* Page table for cores to come back up safely. */
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -06001097
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001098 pc_pgd = pgd_alloc(&init_mm);
1099 if (!pc_pgd)
1100 return -ENOMEM;
1101
Steve Mucklef132c6c2012-06-06 18:30:57 -07001102 exit_phys = virt_to_phys(msm_pm_collapse_exit);
1103
1104 pmd = pmd_offset(pud_offset(pc_pgd + pgd_index(exit_phys),exit_phys),
1105 exit_phys);
1106 pmdval = (exit_phys & PGDIR_MASK) |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001107 PMD_TYPE_SECT | PMD_SECT_AP_WRITE;
1108 pmd[0] = __pmd(pmdval);
1109 pmd[1] = __pmd(pmdval + (1 << (PGDIR_SHIFT - 1)));
1110
Steve Mucklefcece052012-02-18 20:09:58 -08001111 msm_saved_state_phys =
1112 allocate_contiguous_ebi_nomap(CPU_SAVED_STATE_SIZE *
1113 num_possible_cpus(), 4);
1114 if (!msm_saved_state_phys)
1115 return -ENOMEM;
1116 msm_saved_state = ioremap_nocache(msm_saved_state_phys,
1117 CPU_SAVED_STATE_SIZE *
1118 num_possible_cpus());
1119 if (!msm_saved_state)
1120 return -ENOMEM;
1121
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001122 /* It is remotely possible that the code in msm_pm_collapse_exit()
1123 * which turns on the MMU with this mapping is in the
1124 * next even-numbered megabyte beyond the
1125 * start of msm_pm_collapse_exit().
1126 * Map this megabyte in as well.
1127 */
1128 pmd[2] = __pmd(pmdval + (2 << (PGDIR_SHIFT - 1)));
1129 flush_pmd_entry(pmd);
1130 msm_pm_pc_pgd = virt_to_phys(pc_pgd);
Steve Muckle730ad7a2012-02-21 15:26:37 -08001131 clean_caches((unsigned long)&msm_pm_pc_pgd, sizeof(msm_pm_pc_pgd),
1132 virt_to_phys(&msm_pm_pc_pgd));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001133
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001134 msm_pm_mode_sysfs_add();
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301135 msm_pm_add_stats(enable_stats, ARRAY_SIZE(enable_stats));
1136
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001137 suspend_set_ops(&msm_pm_ops);
Praveen Chidambaramc594a092012-09-18 19:48:29 -06001138 msm_pm_target_init();
Girish Mahadevandc318fd2012-08-17 16:48:05 -06001139 hrtimer_init(&pm_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001140 msm_cpuidle_init();
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -06001141 platform_driver_register(&msm_pc_counter_driver);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001142
1143 return 0;
1144}
1145
1146late_initcall(msm_pm_init);
Girish Mahadevan55944992012-10-26 11:03:07 -06001147
1148static void __devinit msm_pm_set_flush_fn(uint32_t pc_mode)
1149{
1150 msm_pm_disable_l2_fn = NULL;
1151 msm_pm_enable_l2_fn = NULL;
1152 msm_pm_flush_l2_fn = outer_flush_all;
1153
1154 if (pc_mode == MSM_PM_PC_NOTZ_L2_EXT) {
1155 msm_pm_disable_l2_fn = outer_disable;
1156 msm_pm_enable_l2_fn = outer_resume;
1157 }
1158}
1159
1160static int __devinit msm_pm_8x60_probe(struct platform_device *pdev)
1161{
1162 char *key = NULL;
1163 uint32_t val = 0;
1164 int ret = 0;
1165
1166 if (!pdev->dev.of_node) {
1167 struct msm_pm_init_data_type *d = pdev->dev.platform_data;
1168
1169 if (!d)
1170 goto pm_8x60_probe_done;
1171
1172 msm_pm_init_data.pc_mode = d->pc_mode;
1173 msm_pm_set_flush_fn(msm_pm_init_data.pc_mode);
1174 msm_pm_init_data.use_sync_timer = d->use_sync_timer;
1175 } else {
1176 key = "qcom,pc-mode";
1177 ret = of_property_read_u32(pdev->dev.of_node, key, &val);
1178
1179 if (ret) {
1180 pr_debug("%s: Cannot read %s,defaulting to 0",
1181 __func__, key);
1182 val = MSM_PM_PC_TZ_L2_INT;
1183 ret = 0;
1184 }
1185
1186 msm_pm_init_data.pc_mode = val;
1187 msm_pm_set_flush_fn(msm_pm_init_data.pc_mode);
1188
1189 key = "qcom,use-sync-timer";
1190 msm_pm_init_data.use_sync_timer =
1191 of_property_read_bool(pdev->dev.of_node, key);
1192 }
1193
1194pm_8x60_probe_done:
1195 return ret;
1196}
1197
1198static struct of_device_id msm_pm_8x60_table[] = {
1199 {.compatible = "qcom,pm-8x60"},
1200 {},
1201};
1202
1203static struct platform_driver msm_pm_8x60_driver = {
1204 .probe = msm_pm_8x60_probe,
1205 .driver = {
1206 .name = "pm-8x60",
1207 .owner = THIS_MODULE,
1208 .of_match_table = msm_pm_8x60_table,
1209 },
1210};
1211
1212static int __init msm_pm_8x60_init(void)
1213{
1214 return platform_driver_register(&msm_pm_8x60_driver);
1215}
1216module_init(msm_pm_8x60_init);