blob: 4fca346908f0fa710980c530332adf3a0e337bdf [file] [log] [blame]
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -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
Priyanka Mathur13bdad12013-01-28 15:52:56 -080014#include <linux/debugfs.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070015#include <linux/module.h>
16#include <linux/kernel.h>
17#include <linux/init.h>
18#include <linux/completion.h>
19#include <linux/cpuidle.h>
20#include <linux/interrupt.h>
21#include <linux/io.h>
22#include <linux/ktime.h>
23#include <linux/pm.h>
Steve Mucklef132c6c2012-06-06 18:30:57 -070024#include <linux/pm_qos.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070025#include <linux/smp.h>
26#include <linux/suspend.h>
27#include <linux/tick.h>
Anji Jonnala02dac8d2013-03-06 21:31:04 +053028#include <linux/delay.h>
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -060029#include <linux/platform_device.h>
Anji Jonnala02dac8d2013-03-06 21:31:04 +053030#include <linux/of_platform.h>
Abhijeet Dharmapurikare8f83852013-02-06 18:57:17 -080031#include <linux/regulator/krait-regulator.h>
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -060032#include <linux/cpu.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070033#include <mach/msm_iomap.h>
Praveen Chidambaram192979f2012-04-25 18:30:23 -060034#include <mach/socinfo.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070035#include <mach/system.h>
Girish Mahadevand27ca4a2012-08-15 09:21:23 -060036#include <mach/scm.h>
37#include <mach/socinfo.h>
Praveen Chidambaramc594a092012-09-18 19:48:29 -060038#include <mach/msm-krait-l2-accessors.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070039#include <asm/cacheflush.h>
40#include <asm/hardware/gic.h>
41#include <asm/pgtable.h>
42#include <asm/pgalloc.h>
Girish Mahadevan55944992012-10-26 11:03:07 -060043#include <asm/outercache.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070044#ifdef CONFIG_VFP
45#include <asm/vfp.h>
46#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070047#include "acpuclock.h"
48#include "clock.h"
49#include "avs.h"
Abhijeet Dharmapurikarefaca4f2011-12-27 16:24:07 -080050#include <mach/cpuidle.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070051#include "idle.h"
Matt Wagantall7cca4642012-02-01 16:43:24 -080052#include "pm.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070053#include "scm-boot.h"
54#include "spm.h"
55#include "timer.h"
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -060056#include "pm-boot.h"
Girish Mahadevandc318fd2012-08-17 16:48:05 -060057#include <mach/event_timer.h>
Priyanka Mathur26b4a4b2012-11-05 13:45:45 -080058#define CREATE_TRACE_POINTS
59#include "trace_msm_low_power.h"
Praveen Chidambaramf27a5152013-02-01 11:44:53 -070060#define SCM_L2_RETENTION (0x2)
61#define SCM_CMD_TERMINATE_PC (0x2)
62
63#define GET_CPU_OF_ATTR(attr) \
64 (container_of(attr, struct msm_pm_kobj_attribute, ka)->cpu)
65
66#define SCLK_HZ (32768)
Praveen Chidambaramf27a5152013-02-01 11:44:53 -070067
Priyanka Mathur13bdad12013-01-28 15:52:56 -080068#define NUM_OF_COUNTERS 3
69#define MAX_BUF_SIZE 512
70
Praveen Chidambaramf27a5152013-02-01 11:44:53 -070071static int msm_pm_debug_mask = 1;
72module_param_named(
73 debug_mask, msm_pm_debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP
74);
75
76static int msm_pm_sleep_time_override;
77module_param_named(sleep_time_override,
78 msm_pm_sleep_time_override, int, S_IRUGO | S_IWUSR | S_IWGRP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070079
80enum {
81 MSM_PM_DEBUG_SUSPEND = BIT(0),
82 MSM_PM_DEBUG_POWER_COLLAPSE = BIT(1),
83 MSM_PM_DEBUG_SUSPEND_LIMITS = BIT(2),
84 MSM_PM_DEBUG_CLOCK = BIT(3),
85 MSM_PM_DEBUG_RESET_VECTOR = BIT(4),
Karthik Parsha6fb932d2012-01-24 18:04:12 -080086 MSM_PM_DEBUG_IDLE_CLK = BIT(5),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070087 MSM_PM_DEBUG_IDLE = BIT(6),
88 MSM_PM_DEBUG_IDLE_LIMITS = BIT(7),
89 MSM_PM_DEBUG_HOTPLUG = BIT(8),
90};
91
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070092enum {
93 MSM_PM_MODE_ATTR_SUSPEND,
94 MSM_PM_MODE_ATTR_IDLE,
95 MSM_PM_MODE_ATTR_NR,
96};
97
98static char *msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_NR] = {
99 [MSM_PM_MODE_ATTR_SUSPEND] = "suspend_enabled",
100 [MSM_PM_MODE_ATTR_IDLE] = "idle_enabled",
101};
102
103struct msm_pm_kobj_attribute {
104 unsigned int cpu;
105 struct kobj_attribute ka;
106};
107
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700108struct msm_pm_sysfs_sleep_mode {
109 struct kobject *kobj;
110 struct attribute_group attr_group;
111 struct attribute *attrs[MSM_PM_MODE_ATTR_NR + 1];
112 struct msm_pm_kobj_attribute kas[MSM_PM_MODE_ATTR_NR];
113};
114
115static char *msm_pm_sleep_mode_labels[MSM_PM_SLEEP_MODE_NR] = {
116 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE] = "power_collapse",
117 [MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT] = "wfi",
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600118 [MSM_PM_SLEEP_MODE_RETENTION] = "retention",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700119 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE] =
120 "standalone_power_collapse",
121};
122
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600123static struct hrtimer pm_hrtimer;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600124static struct msm_pm_sleep_ops pm_sleep_ops;
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -0700125static bool msm_pm_ldo_retention_enabled = true;
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700126static bool msm_pm_use_sync_timer;
127static struct msm_pm_cp15_save_data cp15_data;
128static bool msm_pm_retention_calls_tz;
Praveen Chidambaram4b8df032013-01-18 16:21:16 -0700129static bool msm_no_ramp_down_pc;
Anji Jonnala02dac8d2013-03-06 21:31:04 +0530130static struct msm_pm_sleep_status_data *msm_pm_slp_sts;
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -0600131static bool msm_pm_pc_reset_timer;
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700132
Archana Sathyakumar2b91dc82013-02-01 17:38:23 -0700133static int msm_pm_get_pc_mode(struct device_node *node,
134 const char *key, uint32_t *pc_mode_val)
135{
136 struct pc_mode_of {
137 uint32_t mode;
138 char *mode_name;
139 };
140 int i;
141 struct pc_mode_of pc_modes[] = {
142 {MSM_PM_PC_TZ_L2_INT, "tz_l2_int"},
143 {MSM_PM_PC_NOTZ_L2_EXT, "no_tz_l2_ext"},
144 {MSM_PM_PC_TZ_L2_EXT , "tz_l2_ext"} };
145 int ret;
146 const char *pc_mode_str;
147
148 ret = of_property_read_string(node, key, &pc_mode_str);
149 if (ret) {
150 pr_debug("%s: Cannot read %s,defaulting to 0", __func__, key);
151 pc_mode_val = MSM_PM_PC_TZ_L2_INT;
152 ret = 0;
153 } else {
154 ret = -EINVAL;
155 for (i = 0; i < ARRAY_SIZE(pc_modes); i++) {
156 if (!strncmp(pc_mode_str, pc_modes[i].mode_name,
157 strlen(pc_modes[i].mode_name))) {
158 *pc_mode_val = pc_modes[i].mode;
159 ret = 0;
160 break;
161 }
162 }
163 }
164 return ret;
165}
166
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700167/*
168 * Write out the attribute.
169 */
170static ssize_t msm_pm_mode_attr_show(
171 struct kobject *kobj, struct kobj_attribute *attr, char *buf)
172{
173 int ret = -EINVAL;
174 int i;
175
176 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
177 struct kernel_param kp;
178 unsigned int cpu;
179 struct msm_pm_platform_data *mode;
180
181 if (msm_pm_sleep_mode_labels[i] == NULL)
182 continue;
183
184 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
185 continue;
186
187 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600188 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700189
190 if (!strcmp(attr->attr.name,
191 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
192 u32 arg = mode->suspend_enabled;
193 kp.arg = &arg;
194 ret = param_get_ulong(buf, &kp);
195 } else if (!strcmp(attr->attr.name,
196 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
197 u32 arg = mode->idle_enabled;
198 kp.arg = &arg;
199 ret = param_get_ulong(buf, &kp);
200 }
201
202 break;
203 }
204
205 if (ret > 0) {
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600206 strlcat(buf, "\n", PAGE_SIZE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700207 ret++;
208 }
209
210 return ret;
211}
212
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700213static ssize_t msm_pm_mode_attr_store(struct kobject *kobj,
214 struct kobj_attribute *attr, const char *buf, size_t count)
215{
216 int ret = -EINVAL;
217 int i;
218
219 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
220 struct kernel_param kp;
221 unsigned int cpu;
222 struct msm_pm_platform_data *mode;
223
224 if (msm_pm_sleep_mode_labels[i] == NULL)
225 continue;
226
227 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
228 continue;
229
230 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600231 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700232
233 if (!strcmp(attr->attr.name,
234 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
235 kp.arg = &mode->suspend_enabled;
236 ret = param_set_byte(buf, &kp);
237 } else if (!strcmp(attr->attr.name,
238 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
239 kp.arg = &mode->idle_enabled;
240 ret = param_set_byte(buf, &kp);
241 }
242
243 break;
244 }
245
246 return ret ? ret : count;
247}
248
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700249static int __devinit msm_pm_mode_sysfs_add_cpu(
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700250 unsigned int cpu, struct kobject *modes_kobj)
251{
252 char cpu_name[8];
253 struct kobject *cpu_kobj;
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600254 struct msm_pm_sysfs_sleep_mode *mode = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700255 int i, j, k;
256 int ret;
257
258 snprintf(cpu_name, sizeof(cpu_name), "cpu%u", cpu);
259 cpu_kobj = kobject_create_and_add(cpu_name, modes_kobj);
260 if (!cpu_kobj) {
261 pr_err("%s: cannot create %s kobject\n", __func__, cpu_name);
262 ret = -ENOMEM;
263 goto mode_sysfs_add_cpu_exit;
264 }
265
266 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
267 int idx = MSM_PM_MODE(cpu, i);
268
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600269 if ((!msm_pm_sleep_modes[idx].suspend_supported)
270 && (!msm_pm_sleep_modes[idx].idle_supported))
271 continue;
272
273 if (!msm_pm_sleep_mode_labels[i] ||
274 !msm_pm_sleep_mode_labels[i][0])
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700275 continue;
276
277 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
278 if (!mode) {
279 pr_err("%s: cannot allocate memory for attributes\n",
280 __func__);
281 ret = -ENOMEM;
282 goto mode_sysfs_add_cpu_exit;
283 }
284
285 mode->kobj = kobject_create_and_add(
286 msm_pm_sleep_mode_labels[i], cpu_kobj);
287 if (!mode->kobj) {
288 pr_err("%s: cannot create kobject\n", __func__);
289 ret = -ENOMEM;
290 goto mode_sysfs_add_cpu_exit;
291 }
292
293 for (k = 0, j = 0; k < MSM_PM_MODE_ATTR_NR; k++) {
294 if ((k == MSM_PM_MODE_ATTR_IDLE) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600295 !msm_pm_sleep_modes[idx].idle_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700296 continue;
297 if ((k == MSM_PM_MODE_ATTR_SUSPEND) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600298 !msm_pm_sleep_modes[idx].suspend_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700299 continue;
Stephen Boydd2059c32012-07-03 15:11:15 -0700300 sysfs_attr_init(&mode->kas[j].ka.attr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700301 mode->kas[j].cpu = cpu;
302 mode->kas[j].ka.attr.mode = 0644;
303 mode->kas[j].ka.show = msm_pm_mode_attr_show;
304 mode->kas[j].ka.store = msm_pm_mode_attr_store;
305 mode->kas[j].ka.attr.name = msm_pm_mode_attr_labels[k];
306 mode->attrs[j] = &mode->kas[j].ka.attr;
307 j++;
308 }
309 mode->attrs[j] = NULL;
310
311 mode->attr_group.attrs = mode->attrs;
312 ret = sysfs_create_group(mode->kobj, &mode->attr_group);
313 if (ret) {
314 pr_err("%s: cannot create kobject attribute group\n",
315 __func__);
316 goto mode_sysfs_add_cpu_exit;
317 }
318 }
319
320 ret = 0;
321
322mode_sysfs_add_cpu_exit:
Praveen Chidambaramd5ac2d32011-10-24 14:30:27 -0600323 if (ret) {
Praveen Chidambaram2cfda632011-10-11 16:58:09 -0600324 if (mode && mode->kobj)
325 kobject_del(mode->kobj);
326 kfree(mode);
327 }
328
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700329 return ret;
330}
331
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700332int __devinit msm_pm_mode_sysfs_add(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700333{
334 struct kobject *module_kobj;
335 struct kobject *modes_kobj;
336 unsigned int cpu;
337 int ret;
338
339 module_kobj = kset_find_obj(module_kset, KBUILD_MODNAME);
340 if (!module_kobj) {
341 pr_err("%s: cannot find kobject for module %s\n",
342 __func__, KBUILD_MODNAME);
343 ret = -ENOENT;
344 goto mode_sysfs_add_exit;
345 }
346
347 modes_kobj = kobject_create_and_add("modes", module_kobj);
348 if (!modes_kobj) {
349 pr_err("%s: cannot create modes kobject\n", __func__);
350 ret = -ENOMEM;
351 goto mode_sysfs_add_exit;
352 }
353
354 for_each_possible_cpu(cpu) {
355 ret = msm_pm_mode_sysfs_add_cpu(cpu, modes_kobj);
356 if (ret)
357 goto mode_sysfs_add_exit;
358 }
359
360 ret = 0;
361
362mode_sysfs_add_exit:
363 return ret;
364}
365
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700366/*
367 * Configure hardware registers in preparation for Apps power down.
368 */
369static void msm_pm_config_hw_before_power_down(void)
370{
371 return;
372}
373
374/*
375 * Clear hardware registers after Apps powers up.
376 */
377static void msm_pm_config_hw_after_power_up(void)
378{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700379}
380
381/*
382 * Configure hardware registers in preparation for SWFI.
383 */
384static void msm_pm_config_hw_before_swfi(void)
385{
386 return;
387}
388
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600389/*
390 * Configure/Restore hardware registers in preparation for Retention.
391 */
392
393static void msm_pm_config_hw_after_retention(void)
394{
395 int ret;
Abhijeet Dharmapurikare8f83852013-02-06 18:57:17 -0800396
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600397 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
398 WARN_ON(ret);
399}
400
401static void msm_pm_config_hw_before_retention(void)
402{
403 return;
404}
405
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600406static void msm_pm_save_cpu_reg(void)
407{
408 int i;
409
410 /* Only on core0 */
411 if (smp_processor_id())
412 return;
413
414 /**
415 * On some targets, L2 PC will turn off may reset the core
416 * configuration for the mux and the default may not make the core
417 * happy when it resumes.
418 * Save the active vdd, and set the core vdd to QSB max vdd, so that
419 * when the core resumes, it is capable of supporting the current QSB
420 * rate. Then restore the active vdd before switching the acpuclk rate.
421 */
422 if (msm_pm_get_l2_flush_flag() == 1) {
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700423 cp15_data.active_vdd = msm_spm_get_vdd(0);
424 for (i = 0; i < cp15_data.reg_saved_state_size; i++)
425 cp15_data.reg_val[i] =
426 get_l2_indirect_reg(
427 cp15_data.reg_data[i]);
428 msm_spm_set_vdd(0, cp15_data.qsb_pc_vdd);
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600429 }
430}
431
432static void msm_pm_restore_cpu_reg(void)
433{
434 int i;
435
436 /* Only on core0 */
437 if (smp_processor_id())
438 return;
439
440 if (msm_pm_get_l2_flush_flag() == 1) {
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700441 for (i = 0; i < cp15_data.reg_saved_state_size; i++)
442 set_l2_indirect_reg(
443 cp15_data.reg_data[i],
444 cp15_data.reg_val[i]);
445 msm_spm_set_vdd(0, cp15_data.active_vdd);
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600446 }
447}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700448
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700449static void msm_pm_swfi(void)
450{
451 msm_pm_config_hw_before_swfi();
452 msm_arch_idle();
453}
454
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600455static void msm_pm_retention(void)
456{
457 int ret = 0;
458
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600459 msm_pm_config_hw_before_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600460 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_POWER_RETENTION, false);
461 WARN_ON(ret);
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600462
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700463 if (msm_pm_retention_calls_tz)
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600464 scm_call_atomic1(SCM_SVC_BOOT, SCM_CMD_TERMINATE_PC,
465 SCM_L2_RETENTION);
466 else
467 msm_arch_idle();
468
469 msm_pm_config_hw_after_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600470}
471
Stephen Boydb29750d2012-02-21 01:21:32 -0800472static bool __ref msm_pm_spm_power_collapse(
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700473 unsigned int cpu, bool from_idle, bool notify_rpm)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700474{
475 void *entry;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600476 bool collapsed = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700477 int ret;
478
479 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
480 pr_info("CPU%u: %s: notify_rpm %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700481 cpu, __func__, (int) notify_rpm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700482
483 ret = msm_spm_set_low_power_mode(
484 MSM_SPM_MODE_POWER_COLLAPSE, notify_rpm);
485 WARN_ON(ret);
486
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700487 entry = (!cpu || from_idle) ?
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700488 msm_pm_collapse_exit : msm_secondary_startup;
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700489 msm_pm_boot_config_before_pc(cpu, virt_to_phys(entry));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700490
491 if (MSM_PM_DEBUG_RESET_VECTOR & msm_pm_debug_mask)
492 pr_info("CPU%u: %s: program vector to %p\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700493 cpu, __func__, entry);
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -0600494 if (from_idle && msm_pm_pc_reset_timer)
495 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700496
Girish Mahadevan55944992012-10-26 11:03:07 -0600497 collapsed = msm_pm_collapse();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700498
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -0600499 if (from_idle && msm_pm_pc_reset_timer)
500 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu);
501
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700502 msm_pm_boot_config_after_pc(cpu);
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -0600503
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700504 if (collapsed) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700505 cpu_init();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700506 local_fiq_enable();
507 }
508
509 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
510 pr_info("CPU%u: %s: msm_pm_collapse returned, collapsed %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700511 cpu, __func__, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700512
513 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
514 WARN_ON(ret);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600515 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700516}
517
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600518static bool msm_pm_power_collapse_standalone(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700519{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700520 unsigned int cpu = smp_processor_id();
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700521 unsigned int avsdscr;
522 unsigned int avscsr;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600523 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700524
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700525 avsdscr = avs_get_avsdscr();
526 avscsr = avs_get_avscsr();
527 avs_set_avscsr(0); /* Disable AVS */
528
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700529 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, false);
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700530
531 avs_set_avsdscr(avsdscr);
532 avs_set_avscsr(avscsr);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600533 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700534}
535
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600536static bool msm_pm_power_collapse(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700537{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700538 unsigned int cpu = smp_processor_id();
Praveen Chidambaram4b8df032013-01-18 16:21:16 -0700539 unsigned long saved_acpuclk_rate = 0;
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700540 unsigned int avsdscr;
541 unsigned int avscsr;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600542 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700543
544 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
545 pr_info("CPU%u: %s: idle %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700546 cpu, __func__, (int)from_idle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700547
548 msm_pm_config_hw_before_power_down();
549 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700550 pr_info("CPU%u: %s: pre power down\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700551
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700552 avsdscr = avs_get_avsdscr();
553 avscsr = avs_get_avscsr();
554 avs_set_avscsr(0); /* Disable AVS */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700555
Praveen Chidambaram4b8df032013-01-18 16:21:16 -0700556 if (cpu_online(cpu) && !msm_no_ramp_down_pc)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700557 saved_acpuclk_rate = acpuclk_power_collapse();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700558
559 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
560 pr_info("CPU%u: %s: change clock rate (old rate = %lu)\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700561 cpu, __func__, saved_acpuclk_rate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700562
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700563 if (cp15_data.save_cp15)
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600564 msm_pm_save_cpu_reg();
565
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700566 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700567
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700568 if (cp15_data.save_cp15)
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600569 msm_pm_restore_cpu_reg();
570
Girish Mahadevan4e025d92012-02-29 13:26:51 -0700571 if (cpu_online(cpu)) {
572 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
573 pr_info("CPU%u: %s: restore clock rate to %lu\n",
574 cpu, __func__, saved_acpuclk_rate);
Praveen Chidambaram4b8df032013-01-18 16:21:16 -0700575 if (!msm_no_ramp_down_pc &&
576 acpuclk_set_rate(cpu, saved_acpuclk_rate, SETRATE_PC)
577 < 0)
Girish Mahadevan4e025d92012-02-29 13:26:51 -0700578 pr_err("CPU%u: %s: failed to restore clock rate(%lu)\n",
579 cpu, __func__, saved_acpuclk_rate);
580 } else {
581 unsigned int gic_dist_enabled;
582 unsigned int gic_dist_pending;
583 gic_dist_enabled = readl_relaxed(
584 MSM_QGIC_DIST_BASE + GIC_DIST_ENABLE_CLEAR);
585 gic_dist_pending = readl_relaxed(
586 MSM_QGIC_DIST_BASE + GIC_DIST_PENDING_SET);
587 mb();
588 gic_dist_pending &= gic_dist_enabled;
589
590 if (gic_dist_pending)
591 pr_err("CPU %d interrupted during hotplug.Pending int 0x%x\n",
592 cpu, gic_dist_pending);
593 }
594
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700595
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700596 avs_set_avsdscr(avsdscr);
597 avs_set_avscsr(avscsr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700598 msm_pm_config_hw_after_power_up();
599 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700600 pr_info("CPU%u: %s: post power up\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700601
602 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700603 pr_info("CPU%u: %s: return\n", cpu, __func__);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600604 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700605}
606
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600607static int64_t msm_pm_timer_enter_idle(void)
608{
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700609 if (msm_pm_use_sync_timer)
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600610 return ktime_to_ns(tick_nohz_get_sleep_length());
611
612 return msm_timer_enter_idle();
613}
614
615static void msm_pm_timer_exit_idle(bool timer_halted)
616{
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700617 if (msm_pm_use_sync_timer)
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600618 return;
619
620 msm_timer_exit_idle((int) timer_halted);
621}
622
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530623static int64_t msm_pm_timer_enter_suspend(int64_t *period)
624{
Anji Jonnalac02367a2012-07-01 02:56:11 +0530625 int64_t time = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530626
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700627 if (msm_pm_use_sync_timer)
Mahesh Sivasubramanian0eaeb302013-03-14 14:56:32 -0600628 return sched_clock();
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530629
630 time = msm_timer_get_sclk_time(period);
631 if (!time)
632 pr_err("%s: Unable to read sclk.\n", __func__);
633
634 return time;
635}
636
637static int64_t msm_pm_timer_exit_suspend(int64_t time, int64_t period)
638{
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700639 if (msm_pm_use_sync_timer)
Mahesh Sivasubramanian0eaeb302013-03-14 14:56:32 -0600640 return sched_clock() - time;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530641
642 if (time != 0) {
643 int64_t end_time = msm_timer_get_sclk_time(NULL);
644 if (end_time != 0) {
645 time = end_time - time;
646 if (time < 0)
647 time += period;
648 } else
649 time = 0;
650 }
651
652 return time;
653}
654
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600655/**
656 * pm_hrtimer_cb() : Callback function for hrtimer created if the
657 * core needs to be awake to handle an event.
658 * @hrtimer : Pointer to hrtimer
659 */
660static enum hrtimer_restart pm_hrtimer_cb(struct hrtimer *hrtimer)
661{
662 return HRTIMER_NORESTART;
663}
664
665/**
666 * msm_pm_set_timer() : Set an hrtimer to wakeup the core in time
667 * to handle an event.
668 */
669static void msm_pm_set_timer(uint32_t modified_time_us)
670{
671 u64 modified_time_ns = modified_time_us * NSEC_PER_USEC;
672 ktime_t modified_ktime = ns_to_ktime(modified_time_ns);
673 pm_hrtimer.function = pm_hrtimer_cb;
674 hrtimer_start(&pm_hrtimer, modified_ktime, HRTIMER_MODE_ABS);
675}
676
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700677/******************************************************************************
678 * External Idle/Suspend Functions
679 *****************************************************************************/
680
681void arch_idle(void)
682{
683 return;
684}
685
Priyanka Mathur26b4a4b2012-11-05 13:45:45 -0800686static inline void msm_pm_ftrace_lpm_enter(unsigned int cpu,
687 uint32_t latency, uint32_t sleep_us,
688 uint32_t wake_up,
689 enum msm_pm_sleep_mode mode)
690{
691 switch (mode) {
692 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
693 trace_msm_pm_enter_wfi(cpu, latency, sleep_us, wake_up);
694 break;
695 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
696 trace_msm_pm_enter_spc(cpu, latency, sleep_us, wake_up);
697 break;
698 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
699 trace_msm_pm_enter_pc(cpu, latency, sleep_us, wake_up);
700 break;
701 case MSM_PM_SLEEP_MODE_RETENTION:
702 trace_msm_pm_enter_ret(cpu, latency, sleep_us, wake_up);
703 break;
704 default:
705 break;
706 }
707}
708
709static inline void msm_pm_ftrace_lpm_exit(unsigned int cpu,
710 enum msm_pm_sleep_mode mode,
711 bool success)
712{
713 switch (mode) {
714 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
715 trace_msm_pm_exit_wfi(cpu, success);
716 break;
717 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
718 trace_msm_pm_exit_spc(cpu, success);
719 break;
720 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
721 trace_msm_pm_exit_pc(cpu, success);
722 break;
723 case MSM_PM_SLEEP_MODE_RETENTION:
724 trace_msm_pm_exit_ret(cpu, success);
725 break;
726 default:
727 break;
728 }
729}
730
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800731static int msm_pm_idle_prepare(struct cpuidle_device *dev,
732 struct cpuidle_driver *drv, int index,
733 void **msm_pm_idle_rs_limits)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700734{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700735 int i;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700736 unsigned int power_usage = -1;
Priyanka Mathur848bb4c2012-11-30 18:04:57 -0800737 int ret = MSM_PM_SLEEP_MODE_NOT_SELECTED;
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600738 uint32_t modified_time_us = 0;
739 struct msm_pm_time_params time_param;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700740
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600741 time_param.latency_us =
742 (uint32_t) pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
743 time_param.sleep_us =
744 (uint32_t) (ktime_to_us(tick_nohz_get_sleep_length())
745 & UINT_MAX);
746 time_param.modified_time_us = 0;
747
748 if (!dev->cpu)
749 time_param.next_event_us =
750 (uint32_t) (ktime_to_us(get_next_event_time())
751 & UINT_MAX);
752 else
753 time_param.next_event_us = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700754
755 for (i = 0; i < dev->state_count; i++) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700756 struct cpuidle_state *state = &drv->states[i];
757 struct cpuidle_state_usage *st_usage = &dev->states_usage[i];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700758 enum msm_pm_sleep_mode mode;
759 bool allow;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600760 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700761 int idx;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800762 void *rs_limits = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700763
Steve Mucklef132c6c2012-06-06 18:30:57 -0700764 mode = (enum msm_pm_sleep_mode) cpuidle_get_statedata(st_usage);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700765 idx = MSM_PM_MODE(dev->cpu, mode);
766
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600767 allow = msm_pm_sleep_modes[idx].idle_enabled &&
768 msm_pm_sleep_modes[idx].idle_supported;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700769
770 switch (mode) {
771 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700772 if (num_online_cpus() > 1)
Priyanka Mathur68a9bc52012-10-03 15:15:01 -0700773 allow = false;
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700774 break;
Mahesh Sivasubramanianba7e01c2012-09-04 18:22:51 -0600775 case MSM_PM_SLEEP_MODE_RETENTION:
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -0700776 /*
777 * The Krait BHS regulator doesn't have enough head
778 * room to drive the retention voltage on LDO and so
779 * has disabled retention
780 */
781 if (!msm_pm_ldo_retention_enabled)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700782 allow = false;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700783
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700784 if (msm_pm_retention_calls_tz && num_online_cpus() > 1)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700785 allow = false;
786 break;
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700787 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
788 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
789 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700790 default:
791 allow = false;
792 break;
793 }
794
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700795 if (!allow)
796 continue;
797
798 if (pm_sleep_ops.lowest_limits)
799 rs_limits = pm_sleep_ops.lowest_limits(true,
800 mode, &time_param, &power);
801
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700802 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700803 pr_info("CPU%u:%s:%s, latency %uus, slp %uus, lim %p\n",
804 dev->cpu, __func__, state->desc,
805 time_param.latency_us,
806 time_param.sleep_us, rs_limits);
807 if (!rs_limits)
808 continue;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700809
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700810 if (power < power_usage) {
811 power_usage = power;
812 modified_time_us = time_param.modified_time_us;
813 ret = mode;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800814 *msm_pm_idle_rs_limits = rs_limits;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700815 }
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700816
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700817 }
818
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600819 if (modified_time_us && !dev->cpu)
820 msm_pm_set_timer(modified_time_us);
Priyanka Mathur26b4a4b2012-11-05 13:45:45 -0800821
822 msm_pm_ftrace_lpm_enter(dev->cpu, time_param.latency_us,
823 time_param.sleep_us, time_param.next_event_us,
824 ret);
825
Steve Mucklef132c6c2012-06-06 18:30:57 -0700826 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700827}
828
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800829enum msm_pm_sleep_mode msm_pm_idle_enter(struct cpuidle_device *dev,
830 struct cpuidle_driver *drv, int index)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700831{
832 int64_t time;
Priyanka Mathur26b4a4b2012-11-05 13:45:45 -0800833 bool collapsed = 1;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800834 int exit_stat = -1;
835 enum msm_pm_sleep_mode sleep_mode;
836 void *msm_pm_idle_rs_limits = NULL;
Mahesh Sivasubramanian03698762013-04-18 16:18:46 -0600837 uint32_t sleep_delay = 1;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800838 int ret = -ENODEV;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800839 int notify_rpm = false;
840 bool timer_halted = false;
841
842 sleep_mode = msm_pm_idle_prepare(dev, drv, index,
843 &msm_pm_idle_rs_limits);
844
845 if (!msm_pm_idle_rs_limits) {
846 sleep_mode = MSM_PM_SLEEP_MODE_NOT_SELECTED;
847 goto cpuidle_enter_bail;
848 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700849
850 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
851 pr_info("CPU%u: %s: mode %d\n",
852 smp_processor_id(), __func__, sleep_mode);
853
854 time = ktime_to_ns(ktime_get());
855
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800856 if (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE) {
857 notify_rpm = true;
Mahesh Sivasubramanian03698762013-04-18 16:18:46 -0600858 sleep_delay = (uint32_t)msm_pm_timer_enter_idle();
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800859
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800860 if (sleep_delay == 0) /* 0 would mean infinite time */
861 sleep_delay = 1;
862 }
863
864 if (pm_sleep_ops.enter_sleep)
865 ret = pm_sleep_ops.enter_sleep(sleep_delay,
866 msm_pm_idle_rs_limits, true, notify_rpm);
867 if (ret)
868 goto cpuidle_enter_bail;
869
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700870 switch (sleep_mode) {
871 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
872 msm_pm_swfi();
873 exit_stat = MSM_PM_STAT_IDLE_WFI;
874 break;
875
876 case MSM_PM_SLEEP_MODE_RETENTION:
877 msm_pm_retention();
878 exit_stat = MSM_PM_STAT_RETENTION;
879 break;
880
881 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
882 collapsed = msm_pm_power_collapse_standalone(true);
883 exit_stat = MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE;
884 break;
885
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800886 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700887 if (MSM_PM_DEBUG_IDLE_CLK & msm_pm_debug_mask)
888 clock_debug_print_enabled();
Karthik Parsha6fb932d2012-01-24 18:04:12 -0800889
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800890 collapsed = msm_pm_power_collapse(true);
891 timer_halted = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700892
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700893 exit_stat = MSM_PM_STAT_IDLE_POWER_COLLAPSE;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800894 msm_pm_timer_exit_idle(timer_halted);
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700895 break;
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700896
897 case MSM_PM_SLEEP_MODE_NOT_SELECTED:
898 goto cpuidle_enter_bail;
899 break;
900
901 default:
902 __WARN();
903 goto cpuidle_enter_bail;
904 break;
905 }
906
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800907 if (pm_sleep_ops.exit_sleep)
908 pm_sleep_ops.exit_sleep(msm_pm_idle_rs_limits, true,
909 notify_rpm, collapsed);
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700910
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800911 time = ktime_to_ns(ktime_get()) - time;
912 msm_pm_ftrace_lpm_exit(smp_processor_id(), sleep_mode, collapsed);
913 if (exit_stat >= 0)
914 msm_pm_add_stat(exit_stat, time);
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700915 do_div(time, 1000);
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800916 dev->last_residency = (int) time;
917 return sleep_mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700918
919cpuidle_enter_bail:
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800920 dev->last_residency = 0;
921 if (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE)
922 msm_pm_timer_exit_idle(timer_halted);
923 sleep_mode = MSM_PM_SLEEP_MODE_NOT_SELECTED;
924 return sleep_mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700925}
926
Anji Jonnala02dac8d2013-03-06 21:31:04 +0530927int msm_pm_wait_cpu_shutdown(unsigned int cpu)
928{
929 int timeout = 10;
930
931 if (!msm_pm_slp_sts)
932 return 0;
933 if (!msm_pm_slp_sts[cpu].base_addr)
934 return 0;
935 while (timeout--) {
936 /*
937 * Check for the SPM of the core being hotplugged to set
938 * its sleep state.The SPM sleep state indicates that the
939 * core has been power collapsed.
940 */
941 int acc_sts = __raw_readl(msm_pm_slp_sts[cpu].base_addr);
942
943 if (acc_sts & msm_pm_slp_sts[cpu].mask)
944 return 0;
Mahesh Sivasubramanian65187f92013-03-19 15:14:06 -0600945 udelay(100);
Anji Jonnala02dac8d2013-03-06 21:31:04 +0530946 }
947
948 pr_info("%s(): Timed out waiting for CPU %u SPM to enter sleep state",
949 __func__, cpu);
950 return -EBUSY;
951}
952
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700953void msm_pm_cpu_enter_lowpower(unsigned int cpu)
954{
955 int i;
956 bool allow[MSM_PM_SLEEP_MODE_NR];
957
958 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
959 struct msm_pm_platform_data *mode;
960
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600961 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700962 allow[i] = mode->suspend_supported && mode->suspend_enabled;
963 }
964
965 if (MSM_PM_DEBUG_HOTPLUG & msm_pm_debug_mask)
966 pr_notice("CPU%u: %s: shutting down cpu\n", cpu, __func__);
967
Matt Wagantall5375aa72012-07-02 19:59:51 -0700968 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700969 msm_pm_power_collapse(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -0700970 else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700971 msm_pm_power_collapse_standalone(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -0700972 else if (allow[MSM_PM_SLEEP_MODE_RETENTION])
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600973 msm_pm_retention();
Stephen Boydbda74272012-08-09 14:01:27 -0700974 else
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700975 msm_pm_swfi();
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700976}
977
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -0700978static void msm_pm_ack_retention_disable(void *data)
979{
980 /*
981 * This is a NULL function to ensure that the core has woken up
982 * and is safe to disable retention.
983 */
984}
985/**
986 * msm_pm_enable_retention() - Disable/Enable retention on all cores
987 * @enable: Enable/Disable retention
988 *
989 */
990void msm_pm_enable_retention(bool enable)
991{
Matt Wagantalld06f5c22013-03-14 19:31:14 -0700992 if (enable == msm_pm_ldo_retention_enabled)
993 return;
994
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -0700995 msm_pm_ldo_retention_enabled = enable;
996 /*
997 * If retention is being disabled, wakeup all online core to ensure
998 * that it isn't executing retention. Offlined cores need not be woken
999 * up as they enter the deepest sleep mode, namely RPM assited power
1000 * collapse
1001 */
Mahesh Sivasubramanian96f67d12013-03-14 22:44:42 -06001002 if (!enable) {
1003 preempt_disable();
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -07001004 smp_call_function_many(cpu_online_mask,
1005 msm_pm_ack_retention_disable,
1006 NULL, true);
Mahesh Sivasubramanian96f67d12013-03-14 22:44:42 -06001007 preempt_enable();
1008
1009
1010 }
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -07001011}
1012EXPORT_SYMBOL(msm_pm_enable_retention);
1013
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001014static int msm_pm_enter(suspend_state_t state)
1015{
1016 bool allow[MSM_PM_SLEEP_MODE_NR];
1017 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001018 int64_t period = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301019 int64_t time = msm_pm_timer_enter_suspend(&period);
Girish Mahadevandc318fd2012-08-17 16:48:05 -06001020 struct msm_pm_time_params time_param;
1021
1022 time_param.latency_us = -1;
1023 time_param.sleep_us = -1;
1024 time_param.next_event_us = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001025
1026 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1027 pr_info("%s\n", __func__);
1028
1029 if (smp_processor_id()) {
1030 __WARN();
1031 goto enter_exit;
1032 }
1033
1034
1035 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
1036 struct msm_pm_platform_data *mode;
1037
Praveen Chidambaram42da9d22012-03-30 12:16:34 -06001038 mode = &msm_pm_sleep_modes[MSM_PM_MODE(0, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001039 allow[i] = mode->suspend_supported && mode->suspend_enabled;
1040 }
1041
1042 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001043 void *rs_limits = NULL;
1044 int ret = -ENODEV;
1045 uint32_t power;
Mahesh Sivasubramanian03698762013-04-18 16:18:46 -06001046 uint32_t msm_pm_max_sleep_time = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001047
1048 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1049 pr_info("%s: power collapse\n", __func__);
1050
1051 clock_debug_print_enabled();
1052
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001053 if (msm_pm_sleep_time_override > 0) {
1054 int64_t ns = NSEC_PER_SEC *
1055 (int64_t) msm_pm_sleep_time_override;
Mahesh Sivasubramanian03698762013-04-18 16:18:46 -06001056 do_div(ns, NSEC_PER_SEC / SCLK_HZ);
1057 msm_pm_max_sleep_time = (uint32_t) ns;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001058 }
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001059
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001060 if (pm_sleep_ops.lowest_limits)
1061 rs_limits = pm_sleep_ops.lowest_limits(false,
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001062 MSM_PM_SLEEP_MODE_POWER_COLLAPSE, &time_param, &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001063
1064 if (rs_limits) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001065 if (pm_sleep_ops.enter_sleep)
1066 ret = pm_sleep_ops.enter_sleep(
1067 msm_pm_max_sleep_time,
1068 rs_limits, false, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001069 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -06001070 int collapsed = msm_pm_power_collapse(false);
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001071 if (pm_sleep_ops.exit_sleep) {
1072 pm_sleep_ops.exit_sleep(rs_limits,
1073 false, true, collapsed);
1074 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001075 }
1076 } else {
1077 pr_err("%s: cannot find the lowest power limit\n",
1078 __func__);
1079 }
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301080 time = msm_pm_timer_exit_suspend(time, period);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001081 msm_pm_add_stat(MSM_PM_STAT_SUSPEND, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001082 } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
1083 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1084 pr_info("%s: standalone power collapse\n", __func__);
1085 msm_pm_power_collapse_standalone(false);
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -06001086 } else if (allow[MSM_PM_SLEEP_MODE_RETENTION]) {
1087 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1088 pr_info("%s: retention\n", __func__);
1089 msm_pm_retention();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001090 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
1091 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1092 pr_info("%s: swfi\n", __func__);
1093 msm_pm_swfi();
1094 }
1095
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001096enter_exit:
1097 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1098 pr_info("%s: return\n", __func__);
1099
1100 return 0;
1101}
1102
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001103void msm_pm_set_sleep_ops(struct msm_pm_sleep_ops *ops)
1104{
1105 if (ops)
1106 pm_sleep_ops = *ops;
1107}
1108
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001109static const struct platform_suspend_ops msm_pm_ops = {
1110 .enter = msm_pm_enter,
1111 .valid = suspend_valid_only_mem,
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -06001112};
Anji Jonnala02dac8d2013-03-06 21:31:04 +05301113static int __devinit msm_cpu_status_probe(struct platform_device *pdev)
1114{
1115 struct msm_pm_sleep_status_data *pdata;
1116 char *key;
1117 u32 cpu;
1118
1119 if (!pdev)
1120 return -EFAULT;
1121
1122 msm_pm_slp_sts =
1123 kzalloc(sizeof(*msm_pm_slp_sts) * num_possible_cpus(),
1124 GFP_KERNEL);
1125
1126 if (!msm_pm_slp_sts)
1127 return -ENOMEM;
1128
1129 if (pdev->dev.of_node) {
1130 struct resource *res;
1131 u32 offset;
1132 int rc;
1133 u32 mask;
1134
1135 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1136 if (!res)
1137 goto fail_free_mem;
1138
1139 key = "qcom,cpu-alias-addr";
1140 rc = of_property_read_u32(pdev->dev.of_node, key, &offset);
1141
1142 if (rc)
1143 goto fail_free_mem;
1144
1145 key = "qcom,sleep-status-mask";
1146 rc = of_property_read_u32(pdev->dev.of_node, key,
1147 &mask);
1148 if (rc)
1149 goto fail_free_mem;
1150
1151 for_each_possible_cpu(cpu) {
1152 msm_pm_slp_sts[cpu].base_addr =
1153 ioremap(res->start + cpu * offset,
1154 resource_size(res));
1155 msm_pm_slp_sts[cpu].mask = mask;
1156
1157 if (!msm_pm_slp_sts[cpu].base_addr)
1158 goto failed_of_node;
1159 }
1160
1161 } else {
1162 pdata = pdev->dev.platform_data;
1163 if (!pdev->dev.platform_data)
1164 goto fail_free_mem;
1165
1166 for_each_possible_cpu(cpu) {
1167 msm_pm_slp_sts[cpu].base_addr =
1168 pdata->base_addr + cpu * pdata->cpu_offset;
1169 msm_pm_slp_sts[cpu].mask = pdata->mask;
1170 }
1171 }
1172
1173 return 0;
1174
1175failed_of_node:
1176 pr_info("%s(): Failed to key=%s\n", __func__, key);
1177 for_each_possible_cpu(cpu) {
1178 if (msm_pm_slp_sts[cpu].base_addr)
1179 iounmap(msm_pm_slp_sts[cpu].base_addr);
1180 }
1181fail_free_mem:
1182 kfree(msm_pm_slp_sts);
1183 return -EINVAL;
1184
1185};
1186
1187static struct of_device_id msm_slp_sts_match_tbl[] = {
1188 {.compatible = "qcom,cpu-sleep-status"},
1189 {},
1190};
1191
1192static struct platform_driver msm_cpu_status_driver = {
1193 .probe = msm_cpu_status_probe,
1194 .driver = {
1195 .name = "cpu_slp_status",
1196 .owner = THIS_MODULE,
1197 .of_match_table = msm_slp_sts_match_tbl,
1198 },
1199};
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -06001200
Praveen Chidambaramede45d02013-02-20 17:45:21 -07001201static int __init msm_pm_setup_saved_state(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001202{
1203 pgd_t *pc_pgd;
1204 pmd_t *pmd;
1205 unsigned long pmdval;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001206 unsigned long exit_phys;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301207
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001208 /* Page table for cores to come back up safely. */
1209 pc_pgd = pgd_alloc(&init_mm);
1210 if (!pc_pgd)
1211 return -ENOMEM;
1212
Steve Mucklef132c6c2012-06-06 18:30:57 -07001213 exit_phys = virt_to_phys(msm_pm_collapse_exit);
1214
1215 pmd = pmd_offset(pud_offset(pc_pgd + pgd_index(exit_phys),exit_phys),
1216 exit_phys);
1217 pmdval = (exit_phys & PGDIR_MASK) |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001218 PMD_TYPE_SECT | PMD_SECT_AP_WRITE;
1219 pmd[0] = __pmd(pmdval);
1220 pmd[1] = __pmd(pmdval + (1 << (PGDIR_SHIFT - 1)));
1221
Steve Mucklefcece052012-02-18 20:09:58 -08001222 msm_saved_state_phys =
1223 allocate_contiguous_ebi_nomap(CPU_SAVED_STATE_SIZE *
1224 num_possible_cpus(), 4);
1225 if (!msm_saved_state_phys)
1226 return -ENOMEM;
1227 msm_saved_state = ioremap_nocache(msm_saved_state_phys,
1228 CPU_SAVED_STATE_SIZE *
1229 num_possible_cpus());
1230 if (!msm_saved_state)
1231 return -ENOMEM;
1232
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001233 /* It is remotely possible that the code in msm_pm_collapse_exit()
1234 * which turns on the MMU with this mapping is in the
1235 * next even-numbered megabyte beyond the
1236 * start of msm_pm_collapse_exit().
1237 * Map this megabyte in as well.
1238 */
1239 pmd[2] = __pmd(pmdval + (2 << (PGDIR_SHIFT - 1)));
1240 flush_pmd_entry(pmd);
1241 msm_pm_pc_pgd = virt_to_phys(pc_pgd);
Steve Muckle730ad7a2012-02-21 15:26:37 -08001242 clean_caches((unsigned long)&msm_pm_pc_pgd, sizeof(msm_pm_pc_pgd),
1243 virt_to_phys(&msm_pm_pc_pgd));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001244
Praveen Chidambaramede45d02013-02-20 17:45:21 -07001245 return 0;
1246}
1247core_initcall(msm_pm_setup_saved_state);
1248
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001249static void setup_broadcast_timer(void *arg)
1250{
1251 unsigned long reason = (unsigned long)arg;
1252 int cpu = smp_processor_id();
1253
1254 reason = reason ?
1255 CLOCK_EVT_NOTIFY_BROADCAST_ON : CLOCK_EVT_NOTIFY_BROADCAST_OFF;
1256
1257 clockevents_notify(reason, &cpu);
1258}
1259
1260static int setup_broadcast_cpuhp_notify(struct notifier_block *n,
1261 unsigned long action, void *hcpu)
1262{
1263 int hotcpu = (unsigned long)hcpu;
1264
1265 switch (action & ~CPU_TASKS_FROZEN) {
1266 case CPU_ONLINE:
1267 smp_call_function_single(hotcpu, setup_broadcast_timer,
1268 (void *)true, 1);
1269 break;
1270 }
1271
1272 return NOTIFY_OK;
1273}
1274
1275static struct notifier_block setup_broadcast_notifier = {
1276 .notifier_call = setup_broadcast_cpuhp_notify,
1277};
1278
Praveen Chidambaramede45d02013-02-20 17:45:21 -07001279static int __init msm_pm_init(void)
1280{
1281 enum msm_pm_time_stats_id enable_stats[] = {
1282 MSM_PM_STAT_IDLE_WFI,
1283 MSM_PM_STAT_RETENTION,
1284 MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE,
1285 MSM_PM_STAT_IDLE_POWER_COLLAPSE,
1286 MSM_PM_STAT_SUSPEND,
1287 };
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001288 msm_pm_mode_sysfs_add();
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301289 msm_pm_add_stats(enable_stats, ARRAY_SIZE(enable_stats));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001290 suspend_set_ops(&msm_pm_ops);
Girish Mahadevandc318fd2012-08-17 16:48:05 -06001291 hrtimer_init(&pm_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001292 msm_cpuidle_init();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001293
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001294 if (msm_pm_pc_reset_timer) {
1295 get_cpu();
1296 smp_call_function_many(cpu_online_mask, setup_broadcast_timer,
1297 (void *)true, 1);
1298 put_cpu();
1299 register_cpu_notifier(&setup_broadcast_notifier);
1300 }
1301
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001302 return 0;
1303}
1304
Girish Mahadevan55944992012-10-26 11:03:07 -06001305static void __devinit msm_pm_set_flush_fn(uint32_t pc_mode)
1306{
1307 msm_pm_disable_l2_fn = NULL;
1308 msm_pm_enable_l2_fn = NULL;
1309 msm_pm_flush_l2_fn = outer_flush_all;
1310
1311 if (pc_mode == MSM_PM_PC_NOTZ_L2_EXT) {
1312 msm_pm_disable_l2_fn = outer_disable;
1313 msm_pm_enable_l2_fn = outer_resume;
1314 }
1315}
1316
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001317struct msm_pc_debug_counters_buffer {
1318 void __iomem *reg;
1319 u32 len;
1320 char buf[MAX_BUF_SIZE];
1321};
1322
1323static inline u32 msm_pc_debug_counters_read_register(
1324 void __iomem *reg, int index , int offset)
1325{
1326 return readl_relaxed(reg + (index * 4 + offset) * 4);
1327}
1328
1329static char *counter_name[] = {
1330 "PC Entry Counter",
1331 "Warmboot Entry Counter",
1332 "PC Bailout Counter"
1333};
1334
1335static int msm_pc_debug_counters_copy(
1336 struct msm_pc_debug_counters_buffer *data)
1337{
1338 int j;
1339 u32 stat;
1340 unsigned int cpu;
1341
1342 for_each_possible_cpu(cpu) {
1343 data->len += scnprintf(data->buf + data->len,
1344 sizeof(data->buf)-data->len,
1345 "CPU%d\n", cpu);
1346
1347 for (j = 0; j < NUM_OF_COUNTERS; j++) {
1348 stat = msm_pc_debug_counters_read_register(
1349 data->reg, cpu, j);
1350 data->len += scnprintf(data->buf + data->len,
1351 sizeof(data->buf)-data->len,
1352 "\t%s : %d\n", counter_name[j],
1353 stat);
1354 }
1355
1356 }
1357
1358 return data->len;
1359}
1360
1361static int msm_pc_debug_counters_file_read(struct file *file,
1362 char __user *bufu, size_t count, loff_t *ppos)
1363{
1364 struct msm_pc_debug_counters_buffer *data;
1365
1366 data = file->private_data;
1367
1368 if (!data)
1369 return -EINVAL;
1370
Mahesh Sivasubramanian6900fdc2013-04-18 14:02:52 -06001371 if (!bufu)
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001372 return -EINVAL;
1373
1374 if (!access_ok(VERIFY_WRITE, bufu, count))
1375 return -EFAULT;
1376
1377 if (*ppos >= data->len && data->len == 0)
1378 data->len = msm_pc_debug_counters_copy(data);
1379
1380 return simple_read_from_buffer(bufu, count, ppos,
1381 data->buf, data->len);
1382}
1383
1384static int msm_pc_debug_counters_file_open(struct inode *inode,
1385 struct file *file)
1386{
1387 struct msm_pc_debug_counters_buffer *buf;
1388 void __iomem *msm_pc_debug_counters_reg;
1389
1390 msm_pc_debug_counters_reg = inode->i_private;
1391
1392 if (!msm_pc_debug_counters_reg)
1393 return -EINVAL;
1394
1395 file->private_data = kzalloc(
1396 sizeof(struct msm_pc_debug_counters_buffer), GFP_KERNEL);
1397
1398 if (!file->private_data) {
1399 pr_err("%s: ERROR kmalloc failed to allocate %d bytes\n",
1400 __func__, sizeof(struct msm_pc_debug_counters_buffer));
1401
1402 return -ENOMEM;
1403 }
1404
1405 buf = file->private_data;
1406 buf->reg = msm_pc_debug_counters_reg;
1407
1408 return 0;
1409}
1410
1411static int msm_pc_debug_counters_file_close(struct inode *inode,
1412 struct file *file)
1413{
1414 kfree(file->private_data);
1415 return 0;
1416}
1417
1418static const struct file_operations msm_pc_debug_counters_fops = {
1419 .open = msm_pc_debug_counters_file_open,
1420 .read = msm_pc_debug_counters_file_read,
1421 .release = msm_pc_debug_counters_file_close,
1422 .llseek = no_llseek,
1423};
1424
Girish Mahadevan55944992012-10-26 11:03:07 -06001425static int __devinit msm_pm_8x60_probe(struct platform_device *pdev)
1426{
1427 char *key = NULL;
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001428 struct dentry *dent = NULL;
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001429 struct resource *res = NULL;
1430 int i ;
1431 struct msm_pm_init_data_type pdata_local;
Girish Mahadevan55944992012-10-26 11:03:07 -06001432 int ret = 0;
1433
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001434 memset(&pdata_local, 0, sizeof(struct msm_pm_init_data_type));
1435
1436 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1437 if (res) {
1438 msm_pc_debug_counters_phys = res->start;
1439 WARN_ON(resource_size(res) < SZ_64);
1440 msm_pc_debug_counters = devm_ioremap(&pdev->dev, res->start,
1441 resource_size(res));
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001442 if (msm_pc_debug_counters)
1443 for (i = 0; i < resource_size(res)/4; i++)
1444 __raw_writel(0, msm_pc_debug_counters + i * 4);
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001445
1446 }
1447
1448 if (!msm_pc_debug_counters) {
1449 msm_pc_debug_counters = 0;
1450 msm_pc_debug_counters_phys = 0;
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001451 } else {
1452 dent = debugfs_create_file("pc_debug_counter", S_IRUGO, NULL,
1453 msm_pc_debug_counters,
1454 &msm_pc_debug_counters_fops);
1455 if (!dent)
1456 pr_err("%s: ERROR debugfs_create_file failed\n",
1457 __func__);
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001458 }
1459
Girish Mahadevan55944992012-10-26 11:03:07 -06001460 if (!pdev->dev.of_node) {
1461 struct msm_pm_init_data_type *d = pdev->dev.platform_data;
1462
1463 if (!d)
1464 goto pm_8x60_probe_done;
1465
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001466 memcpy(&pdata_local, d, sizeof(struct msm_pm_init_data_type));
1467
Girish Mahadevan55944992012-10-26 11:03:07 -06001468 } else {
1469 key = "qcom,pc-mode";
Archana Sathyakumar2b91dc82013-02-01 17:38:23 -07001470 ret = msm_pm_get_pc_mode(pdev->dev.of_node,
1471 key,
1472 &pdata_local.pc_mode);
Girish Mahadevan55944992012-10-26 11:03:07 -06001473 if (ret) {
Archana Sathyakumar2b91dc82013-02-01 17:38:23 -07001474 pr_debug("%s: Error reading key %s",
Girish Mahadevan55944992012-10-26 11:03:07 -06001475 __func__, key);
Archana Sathyakumar2b91dc82013-02-01 17:38:23 -07001476 return -EINVAL;
Girish Mahadevan55944992012-10-26 11:03:07 -06001477 }
Girish Mahadevan55944992012-10-26 11:03:07 -06001478
1479 key = "qcom,use-sync-timer";
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001480 pdata_local.use_sync_timer =
Girish Mahadevan55944992012-10-26 11:03:07 -06001481 of_property_read_bool(pdev->dev.of_node, key);
Praveen Chidambaram4b8df032013-01-18 16:21:16 -07001482
1483 key = "qcom,saw-turns-off-pll";
1484 msm_no_ramp_down_pc = of_property_read_bool(pdev->dev.of_node,
1485 key);
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001486
1487 key = "qcom,pc-resets-timer";
1488 msm_pm_pc_reset_timer = of_property_read_bool(
1489 pdev->dev.of_node, key);
Girish Mahadevan55944992012-10-26 11:03:07 -06001490 }
1491
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001492 if (pdata_local.cp15_data.reg_data &&
1493 pdata_local.cp15_data.reg_saved_state_size > 0) {
1494 cp15_data.reg_data = kzalloc(sizeof(uint32_t) *
1495 pdata_local.cp15_data.reg_saved_state_size,
1496 GFP_KERNEL);
1497 if (!cp15_data.reg_data)
1498 return -ENOMEM;
1499
1500 cp15_data.reg_val = kzalloc(sizeof(uint32_t) *
1501 pdata_local.cp15_data.reg_saved_state_size,
1502 GFP_KERNEL);
1503 if (cp15_data.reg_val)
1504 return -ENOMEM;
1505
1506 memcpy(cp15_data.reg_data, pdata_local.cp15_data.reg_data,
1507 pdata_local.cp15_data.reg_saved_state_size *
1508 sizeof(uint32_t));
1509 }
1510
1511 msm_pm_set_flush_fn(pdata_local.pc_mode);
1512 msm_pm_use_sync_timer = pdata_local.use_sync_timer;
1513 msm_pm_retention_calls_tz = pdata_local.retention_calls_tz;
1514
Girish Mahadevan55944992012-10-26 11:03:07 -06001515pm_8x60_probe_done:
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001516 msm_pm_init();
Anji Jonnala02dac8d2013-03-06 21:31:04 +05301517 if (pdev->dev.of_node)
1518 of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
1519
Girish Mahadevan55944992012-10-26 11:03:07 -06001520 return ret;
1521}
1522
1523static struct of_device_id msm_pm_8x60_table[] = {
1524 {.compatible = "qcom,pm-8x60"},
1525 {},
1526};
1527
1528static struct platform_driver msm_pm_8x60_driver = {
1529 .probe = msm_pm_8x60_probe,
1530 .driver = {
1531 .name = "pm-8x60",
1532 .owner = THIS_MODULE,
1533 .of_match_table = msm_pm_8x60_table,
1534 },
1535};
1536
1537static int __init msm_pm_8x60_init(void)
1538{
Anji Jonnala02dac8d2013-03-06 21:31:04 +05301539 int rc;
1540
1541 rc = platform_driver_register(&msm_cpu_status_driver);
1542
1543 if (rc) {
1544 pr_err("%s(): failed to register driver %s\n", __func__,
1545 msm_cpu_status_driver.driver.name);
1546 return rc;
1547 }
1548
Girish Mahadevan55944992012-10-26 11:03:07 -06001549 return platform_driver_register(&msm_pm_8x60_driver);
1550}
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001551device_initcall(msm_pm_8x60_init);