blob: 143612fb2a8c5e4e12780aa41819263b09bbdff9 [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>
Hanumant Singhbd82a3e2013-02-26 13:51:17 -080038#define CREATE_TRACE_POINTS
39#include <mach/trace_msm_low_power.h>
Praveen Chidambaramc594a092012-09-18 19:48:29 -060040#include <mach/msm-krait-l2-accessors.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070041#include <asm/cacheflush.h>
42#include <asm/hardware/gic.h>
43#include <asm/pgtable.h>
44#include <asm/pgalloc.h>
Girish Mahadevan55944992012-10-26 11:03:07 -060045#include <asm/outercache.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070046#ifdef CONFIG_VFP
47#include <asm/vfp.h>
48#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070049#include "acpuclock.h"
50#include "clock.h"
51#include "avs.h"
Abhijeet Dharmapurikarefaca4f2011-12-27 16:24:07 -080052#include <mach/cpuidle.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070053#include "idle.h"
Matt Wagantall7cca4642012-02-01 16:43:24 -080054#include "pm.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070055#include "scm-boot.h"
56#include "spm.h"
57#include "timer.h"
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -060058#include "pm-boot.h"
Girish Mahadevandc318fd2012-08-17 16:48:05 -060059#include <mach/event_timer.h>
Hanumant Singhbd82a3e2013-02-26 13:51:17 -080060
Praveen Chidambaramf27a5152013-02-01 11:44:53 -070061#define SCM_L2_RETENTION (0x2)
62#define SCM_CMD_TERMINATE_PC (0x2)
63
64#define GET_CPU_OF_ATTR(attr) \
65 (container_of(attr, struct msm_pm_kobj_attribute, ka)->cpu)
66
67#define SCLK_HZ (32768)
Praveen Chidambaramf27a5152013-02-01 11:44:53 -070068
Priyanka Mathur13bdad12013-01-28 15:52:56 -080069#define NUM_OF_COUNTERS 3
70#define MAX_BUF_SIZE 512
71
Praveen Chidambaramf27a5152013-02-01 11:44:53 -070072static int msm_pm_debug_mask = 1;
73module_param_named(
74 debug_mask, msm_pm_debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP
75);
76
77static int msm_pm_sleep_time_override;
78module_param_named(sleep_time_override,
79 msm_pm_sleep_time_override, int, S_IRUGO | S_IWUSR | S_IWGRP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070080
81enum {
82 MSM_PM_DEBUG_SUSPEND = BIT(0),
83 MSM_PM_DEBUG_POWER_COLLAPSE = BIT(1),
84 MSM_PM_DEBUG_SUSPEND_LIMITS = BIT(2),
85 MSM_PM_DEBUG_CLOCK = BIT(3),
86 MSM_PM_DEBUG_RESET_VECTOR = BIT(4),
Karthik Parsha6fb932d2012-01-24 18:04:12 -080087 MSM_PM_DEBUG_IDLE_CLK = BIT(5),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070088 MSM_PM_DEBUG_IDLE = BIT(6),
89 MSM_PM_DEBUG_IDLE_LIMITS = BIT(7),
90 MSM_PM_DEBUG_HOTPLUG = BIT(8),
91};
92
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070093enum {
94 MSM_PM_MODE_ATTR_SUSPEND,
95 MSM_PM_MODE_ATTR_IDLE,
96 MSM_PM_MODE_ATTR_NR,
97};
98
99static char *msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_NR] = {
100 [MSM_PM_MODE_ATTR_SUSPEND] = "suspend_enabled",
101 [MSM_PM_MODE_ATTR_IDLE] = "idle_enabled",
102};
103
104struct msm_pm_kobj_attribute {
105 unsigned int cpu;
106 struct kobj_attribute ka;
107};
108
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700109struct msm_pm_sysfs_sleep_mode {
110 struct kobject *kobj;
111 struct attribute_group attr_group;
112 struct attribute *attrs[MSM_PM_MODE_ATTR_NR + 1];
113 struct msm_pm_kobj_attribute kas[MSM_PM_MODE_ATTR_NR];
114};
115
116static char *msm_pm_sleep_mode_labels[MSM_PM_SLEEP_MODE_NR] = {
117 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE] = "power_collapse",
118 [MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT] = "wfi",
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600119 [MSM_PM_SLEEP_MODE_RETENTION] = "retention",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700120 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE] =
121 "standalone_power_collapse",
122};
123
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600124static struct hrtimer pm_hrtimer;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600125static struct msm_pm_sleep_ops pm_sleep_ops;
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -0700126static bool msm_pm_ldo_retention_enabled = true;
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700127static bool msm_pm_use_sync_timer;
128static struct msm_pm_cp15_save_data cp15_data;
129static bool msm_pm_retention_calls_tz;
Praveen Chidambaram4b8df032013-01-18 16:21:16 -0700130static bool msm_no_ramp_down_pc;
Anji Jonnala02dac8d2013-03-06 21:31:04 +0530131static struct msm_pm_sleep_status_data *msm_pm_slp_sts;
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -0600132static bool msm_pm_pc_reset_timer;
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700133
Archana Sathyakumar2b91dc82013-02-01 17:38:23 -0700134static int msm_pm_get_pc_mode(struct device_node *node,
135 const char *key, uint32_t *pc_mode_val)
136{
137 struct pc_mode_of {
138 uint32_t mode;
139 char *mode_name;
140 };
141 int i;
142 struct pc_mode_of pc_modes[] = {
143 {MSM_PM_PC_TZ_L2_INT, "tz_l2_int"},
144 {MSM_PM_PC_NOTZ_L2_EXT, "no_tz_l2_ext"},
145 {MSM_PM_PC_TZ_L2_EXT , "tz_l2_ext"} };
146 int ret;
147 const char *pc_mode_str;
148
149 ret = of_property_read_string(node, key, &pc_mode_str);
150 if (ret) {
151 pr_debug("%s: Cannot read %s,defaulting to 0", __func__, key);
152 pc_mode_val = MSM_PM_PC_TZ_L2_INT;
153 ret = 0;
154 } else {
155 ret = -EINVAL;
156 for (i = 0; i < ARRAY_SIZE(pc_modes); i++) {
157 if (!strncmp(pc_mode_str, pc_modes[i].mode_name,
158 strlen(pc_modes[i].mode_name))) {
159 *pc_mode_val = pc_modes[i].mode;
160 ret = 0;
161 break;
162 }
163 }
164 }
165 return ret;
166}
167
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700168/*
169 * Write out the attribute.
170 */
171static ssize_t msm_pm_mode_attr_show(
172 struct kobject *kobj, struct kobj_attribute *attr, char *buf)
173{
174 int ret = -EINVAL;
175 int i;
176
177 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
178 struct kernel_param kp;
179 unsigned int cpu;
180 struct msm_pm_platform_data *mode;
181
182 if (msm_pm_sleep_mode_labels[i] == NULL)
183 continue;
184
185 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
186 continue;
187
188 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600189 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700190
191 if (!strcmp(attr->attr.name,
192 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
193 u32 arg = mode->suspend_enabled;
194 kp.arg = &arg;
195 ret = param_get_ulong(buf, &kp);
196 } else if (!strcmp(attr->attr.name,
197 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
198 u32 arg = mode->idle_enabled;
199 kp.arg = &arg;
200 ret = param_get_ulong(buf, &kp);
201 }
202
203 break;
204 }
205
206 if (ret > 0) {
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600207 strlcat(buf, "\n", PAGE_SIZE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700208 ret++;
209 }
210
211 return ret;
212}
213
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700214static ssize_t msm_pm_mode_attr_store(struct kobject *kobj,
215 struct kobj_attribute *attr, const char *buf, size_t count)
216{
217 int ret = -EINVAL;
218 int i;
219
220 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
221 struct kernel_param kp;
222 unsigned int cpu;
223 struct msm_pm_platform_data *mode;
224
225 if (msm_pm_sleep_mode_labels[i] == NULL)
226 continue;
227
228 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
229 continue;
230
231 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600232 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700233
234 if (!strcmp(attr->attr.name,
235 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
236 kp.arg = &mode->suspend_enabled;
237 ret = param_set_byte(buf, &kp);
238 } else if (!strcmp(attr->attr.name,
239 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
240 kp.arg = &mode->idle_enabled;
241 ret = param_set_byte(buf, &kp);
242 }
243
244 break;
245 }
246
247 return ret ? ret : count;
248}
249
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700250static int __devinit msm_pm_mode_sysfs_add_cpu(
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700251 unsigned int cpu, struct kobject *modes_kobj)
252{
253 char cpu_name[8];
254 struct kobject *cpu_kobj;
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600255 struct msm_pm_sysfs_sleep_mode *mode = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700256 int i, j, k;
257 int ret;
258
259 snprintf(cpu_name, sizeof(cpu_name), "cpu%u", cpu);
260 cpu_kobj = kobject_create_and_add(cpu_name, modes_kobj);
261 if (!cpu_kobj) {
262 pr_err("%s: cannot create %s kobject\n", __func__, cpu_name);
263 ret = -ENOMEM;
264 goto mode_sysfs_add_cpu_exit;
265 }
266
267 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
268 int idx = MSM_PM_MODE(cpu, i);
269
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600270 if ((!msm_pm_sleep_modes[idx].suspend_supported)
271 && (!msm_pm_sleep_modes[idx].idle_supported))
272 continue;
273
274 if (!msm_pm_sleep_mode_labels[i] ||
275 !msm_pm_sleep_mode_labels[i][0])
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700276 continue;
277
278 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
279 if (!mode) {
280 pr_err("%s: cannot allocate memory for attributes\n",
281 __func__);
282 ret = -ENOMEM;
283 goto mode_sysfs_add_cpu_exit;
284 }
285
286 mode->kobj = kobject_create_and_add(
287 msm_pm_sleep_mode_labels[i], cpu_kobj);
288 if (!mode->kobj) {
289 pr_err("%s: cannot create kobject\n", __func__);
290 ret = -ENOMEM;
291 goto mode_sysfs_add_cpu_exit;
292 }
293
294 for (k = 0, j = 0; k < MSM_PM_MODE_ATTR_NR; k++) {
295 if ((k == MSM_PM_MODE_ATTR_IDLE) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600296 !msm_pm_sleep_modes[idx].idle_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700297 continue;
298 if ((k == MSM_PM_MODE_ATTR_SUSPEND) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600299 !msm_pm_sleep_modes[idx].suspend_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700300 continue;
Stephen Boydd2059c32012-07-03 15:11:15 -0700301 sysfs_attr_init(&mode->kas[j].ka.attr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700302 mode->kas[j].cpu = cpu;
303 mode->kas[j].ka.attr.mode = 0644;
304 mode->kas[j].ka.show = msm_pm_mode_attr_show;
305 mode->kas[j].ka.store = msm_pm_mode_attr_store;
306 mode->kas[j].ka.attr.name = msm_pm_mode_attr_labels[k];
307 mode->attrs[j] = &mode->kas[j].ka.attr;
308 j++;
309 }
310 mode->attrs[j] = NULL;
311
312 mode->attr_group.attrs = mode->attrs;
313 ret = sysfs_create_group(mode->kobj, &mode->attr_group);
314 if (ret) {
315 pr_err("%s: cannot create kobject attribute group\n",
316 __func__);
317 goto mode_sysfs_add_cpu_exit;
318 }
319 }
320
321 ret = 0;
322
323mode_sysfs_add_cpu_exit:
Praveen Chidambaramd5ac2d32011-10-24 14:30:27 -0600324 if (ret) {
Praveen Chidambaram2cfda632011-10-11 16:58:09 -0600325 if (mode && mode->kobj)
326 kobject_del(mode->kobj);
327 kfree(mode);
328 }
329
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700330 return ret;
331}
332
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700333int __devinit msm_pm_mode_sysfs_add(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700334{
335 struct kobject *module_kobj;
336 struct kobject *modes_kobj;
337 unsigned int cpu;
338 int ret;
339
340 module_kobj = kset_find_obj(module_kset, KBUILD_MODNAME);
341 if (!module_kobj) {
342 pr_err("%s: cannot find kobject for module %s\n",
343 __func__, KBUILD_MODNAME);
344 ret = -ENOENT;
345 goto mode_sysfs_add_exit;
346 }
347
348 modes_kobj = kobject_create_and_add("modes", module_kobj);
349 if (!modes_kobj) {
350 pr_err("%s: cannot create modes kobject\n", __func__);
351 ret = -ENOMEM;
352 goto mode_sysfs_add_exit;
353 }
354
355 for_each_possible_cpu(cpu) {
356 ret = msm_pm_mode_sysfs_add_cpu(cpu, modes_kobj);
357 if (ret)
358 goto mode_sysfs_add_exit;
359 }
360
361 ret = 0;
362
363mode_sysfs_add_exit:
364 return ret;
365}
366
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700367/*
368 * Configure hardware registers in preparation for Apps power down.
369 */
370static void msm_pm_config_hw_before_power_down(void)
371{
372 return;
373}
374
375/*
376 * Clear hardware registers after Apps powers up.
377 */
378static void msm_pm_config_hw_after_power_up(void)
379{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700380}
381
382/*
383 * Configure hardware registers in preparation for SWFI.
384 */
385static void msm_pm_config_hw_before_swfi(void)
386{
387 return;
388}
389
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600390/*
391 * Configure/Restore hardware registers in preparation for Retention.
392 */
393
394static void msm_pm_config_hw_after_retention(void)
395{
396 int ret;
Abhijeet Dharmapurikare8f83852013-02-06 18:57:17 -0800397
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600398 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
399 WARN_ON(ret);
400}
401
402static void msm_pm_config_hw_before_retention(void)
403{
404 return;
405}
406
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600407static void msm_pm_save_cpu_reg(void)
408{
409 int i;
410
411 /* Only on core0 */
412 if (smp_processor_id())
413 return;
414
415 /**
416 * On some targets, L2 PC will turn off may reset the core
417 * configuration for the mux and the default may not make the core
418 * happy when it resumes.
419 * Save the active vdd, and set the core vdd to QSB max vdd, so that
420 * when the core resumes, it is capable of supporting the current QSB
421 * rate. Then restore the active vdd before switching the acpuclk rate.
422 */
423 if (msm_pm_get_l2_flush_flag() == 1) {
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700424 cp15_data.active_vdd = msm_spm_get_vdd(0);
425 for (i = 0; i < cp15_data.reg_saved_state_size; i++)
426 cp15_data.reg_val[i] =
427 get_l2_indirect_reg(
428 cp15_data.reg_data[i]);
429 msm_spm_set_vdd(0, cp15_data.qsb_pc_vdd);
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600430 }
431}
432
433static void msm_pm_restore_cpu_reg(void)
434{
435 int i;
436
437 /* Only on core0 */
438 if (smp_processor_id())
439 return;
440
441 if (msm_pm_get_l2_flush_flag() == 1) {
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700442 for (i = 0; i < cp15_data.reg_saved_state_size; i++)
443 set_l2_indirect_reg(
444 cp15_data.reg_data[i],
445 cp15_data.reg_val[i]);
446 msm_spm_set_vdd(0, cp15_data.active_vdd);
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600447 }
448}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700449
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700450static void msm_pm_swfi(void)
451{
452 msm_pm_config_hw_before_swfi();
453 msm_arch_idle();
454}
455
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600456static void msm_pm_retention(void)
457{
458 int ret = 0;
459
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600460 msm_pm_config_hw_before_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600461 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_POWER_RETENTION, false);
462 WARN_ON(ret);
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600463
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700464 if (msm_pm_retention_calls_tz)
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600465 scm_call_atomic1(SCM_SVC_BOOT, SCM_CMD_TERMINATE_PC,
466 SCM_L2_RETENTION);
467 else
468 msm_arch_idle();
469
470 msm_pm_config_hw_after_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600471}
472
Stephen Boydb29750d2012-02-21 01:21:32 -0800473static bool __ref msm_pm_spm_power_collapse(
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700474 unsigned int cpu, bool from_idle, bool notify_rpm)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700475{
476 void *entry;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600477 bool collapsed = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700478 int ret;
479
480 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
481 pr_info("CPU%u: %s: notify_rpm %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700482 cpu, __func__, (int) notify_rpm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700483
484 ret = msm_spm_set_low_power_mode(
485 MSM_SPM_MODE_POWER_COLLAPSE, notify_rpm);
486 WARN_ON(ret);
487
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700488 entry = (!cpu || from_idle) ?
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700489 msm_pm_collapse_exit : msm_secondary_startup;
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700490 msm_pm_boot_config_before_pc(cpu, virt_to_phys(entry));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700491
492 if (MSM_PM_DEBUG_RESET_VECTOR & msm_pm_debug_mask)
493 pr_info("CPU%u: %s: program vector to %p\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700494 cpu, __func__, entry);
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -0600495 if (from_idle && msm_pm_pc_reset_timer)
496 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700497
Girish Mahadevan55944992012-10-26 11:03:07 -0600498 collapsed = msm_pm_collapse();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700499
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -0600500 if (from_idle && msm_pm_pc_reset_timer)
501 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu);
502
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700503 msm_pm_boot_config_after_pc(cpu);
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -0600504
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700505 if (collapsed) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700506 cpu_init();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700507 local_fiq_enable();
508 }
509
510 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
511 pr_info("CPU%u: %s: msm_pm_collapse returned, collapsed %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700512 cpu, __func__, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700513
514 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
515 WARN_ON(ret);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600516 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700517}
518
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600519static bool msm_pm_power_collapse_standalone(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700520{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700521 unsigned int cpu = smp_processor_id();
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700522 unsigned int avsdscr;
523 unsigned int avscsr;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600524 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700525
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700526 avsdscr = avs_get_avsdscr();
527 avscsr = avs_get_avscsr();
528 avs_set_avscsr(0); /* Disable AVS */
529
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700530 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, false);
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700531
532 avs_set_avsdscr(avsdscr);
533 avs_set_avscsr(avscsr);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600534 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700535}
536
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600537static bool msm_pm_power_collapse(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700538{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700539 unsigned int cpu = smp_processor_id();
Praveen Chidambaram4b8df032013-01-18 16:21:16 -0700540 unsigned long saved_acpuclk_rate = 0;
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700541 unsigned int avsdscr;
542 unsigned int avscsr;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600543 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700544
545 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
546 pr_info("CPU%u: %s: idle %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700547 cpu, __func__, (int)from_idle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700548
549 msm_pm_config_hw_before_power_down();
550 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700551 pr_info("CPU%u: %s: pre power down\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700552
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700553 avsdscr = avs_get_avsdscr();
554 avscsr = avs_get_avscsr();
555 avs_set_avscsr(0); /* Disable AVS */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700556
Praveen Chidambaram4b8df032013-01-18 16:21:16 -0700557 if (cpu_online(cpu) && !msm_no_ramp_down_pc)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700558 saved_acpuclk_rate = acpuclk_power_collapse();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700559
560 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
561 pr_info("CPU%u: %s: change clock rate (old rate = %lu)\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700562 cpu, __func__, saved_acpuclk_rate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700563
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700564 if (cp15_data.save_cp15)
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600565 msm_pm_save_cpu_reg();
566
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700567 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700568
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700569 if (cp15_data.save_cp15)
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600570 msm_pm_restore_cpu_reg();
571
Girish Mahadevan4e025d92012-02-29 13:26:51 -0700572 if (cpu_online(cpu)) {
573 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
574 pr_info("CPU%u: %s: restore clock rate to %lu\n",
575 cpu, __func__, saved_acpuclk_rate);
Praveen Chidambaram4b8df032013-01-18 16:21:16 -0700576 if (!msm_no_ramp_down_pc &&
577 acpuclk_set_rate(cpu, saved_acpuclk_rate, SETRATE_PC)
578 < 0)
Girish Mahadevan4e025d92012-02-29 13:26:51 -0700579 pr_err("CPU%u: %s: failed to restore clock rate(%lu)\n",
580 cpu, __func__, saved_acpuclk_rate);
581 } else {
582 unsigned int gic_dist_enabled;
583 unsigned int gic_dist_pending;
584 gic_dist_enabled = readl_relaxed(
585 MSM_QGIC_DIST_BASE + GIC_DIST_ENABLE_CLEAR);
586 gic_dist_pending = readl_relaxed(
587 MSM_QGIC_DIST_BASE + GIC_DIST_PENDING_SET);
588 mb();
589 gic_dist_pending &= gic_dist_enabled;
590
591 if (gic_dist_pending)
592 pr_err("CPU %d interrupted during hotplug.Pending int 0x%x\n",
593 cpu, gic_dist_pending);
594 }
595
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700596
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700597 avs_set_avsdscr(avsdscr);
598 avs_set_avscsr(avscsr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700599 msm_pm_config_hw_after_power_up();
600 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700601 pr_info("CPU%u: %s: post power up\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700602
603 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700604 pr_info("CPU%u: %s: return\n", cpu, __func__);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600605 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700606}
607
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600608static int64_t msm_pm_timer_enter_idle(void)
609{
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700610 if (msm_pm_use_sync_timer)
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600611 return ktime_to_ns(tick_nohz_get_sleep_length());
612
613 return msm_timer_enter_idle();
614}
615
616static void msm_pm_timer_exit_idle(bool timer_halted)
617{
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700618 if (msm_pm_use_sync_timer)
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600619 return;
620
621 msm_timer_exit_idle((int) timer_halted);
622}
623
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530624static int64_t msm_pm_timer_enter_suspend(int64_t *period)
625{
Anji Jonnalac02367a2012-07-01 02:56:11 +0530626 int64_t time = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530627
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700628 if (msm_pm_use_sync_timer)
Mahesh Sivasubramanian0eaeb302013-03-14 14:56:32 -0600629 return sched_clock();
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530630
631 time = msm_timer_get_sclk_time(period);
632 if (!time)
633 pr_err("%s: Unable to read sclk.\n", __func__);
634
635 return time;
636}
637
638static int64_t msm_pm_timer_exit_suspend(int64_t time, int64_t period)
639{
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700640 if (msm_pm_use_sync_timer)
Mahesh Sivasubramanian0eaeb302013-03-14 14:56:32 -0600641 return sched_clock() - time;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530642
643 if (time != 0) {
644 int64_t end_time = msm_timer_get_sclk_time(NULL);
645 if (end_time != 0) {
646 time = end_time - time;
647 if (time < 0)
648 time += period;
649 } else
650 time = 0;
651 }
652
653 return time;
654}
655
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600656/**
657 * pm_hrtimer_cb() : Callback function for hrtimer created if the
658 * core needs to be awake to handle an event.
659 * @hrtimer : Pointer to hrtimer
660 */
661static enum hrtimer_restart pm_hrtimer_cb(struct hrtimer *hrtimer)
662{
663 return HRTIMER_NORESTART;
664}
665
666/**
667 * msm_pm_set_timer() : Set an hrtimer to wakeup the core in time
668 * to handle an event.
669 */
670static void msm_pm_set_timer(uint32_t modified_time_us)
671{
672 u64 modified_time_ns = modified_time_us * NSEC_PER_USEC;
673 ktime_t modified_ktime = ns_to_ktime(modified_time_ns);
674 pm_hrtimer.function = pm_hrtimer_cb;
675 hrtimer_start(&pm_hrtimer, modified_ktime, HRTIMER_MODE_ABS);
676}
677
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700678/******************************************************************************
679 * External Idle/Suspend Functions
680 *****************************************************************************/
681
682void arch_idle(void)
683{
684 return;
685}
686
Priyanka Mathur26b4a4b2012-11-05 13:45:45 -0800687static inline void msm_pm_ftrace_lpm_enter(unsigned int cpu,
688 uint32_t latency, uint32_t sleep_us,
689 uint32_t wake_up,
690 enum msm_pm_sleep_mode mode)
691{
692 switch (mode) {
693 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
694 trace_msm_pm_enter_wfi(cpu, latency, sleep_us, wake_up);
695 break;
696 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
697 trace_msm_pm_enter_spc(cpu, latency, sleep_us, wake_up);
698 break;
699 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
700 trace_msm_pm_enter_pc(cpu, latency, sleep_us, wake_up);
701 break;
702 case MSM_PM_SLEEP_MODE_RETENTION:
703 trace_msm_pm_enter_ret(cpu, latency, sleep_us, wake_up);
704 break;
705 default:
706 break;
707 }
708}
709
710static inline void msm_pm_ftrace_lpm_exit(unsigned int cpu,
711 enum msm_pm_sleep_mode mode,
712 bool success)
713{
714 switch (mode) {
715 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
716 trace_msm_pm_exit_wfi(cpu, success);
717 break;
718 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
719 trace_msm_pm_exit_spc(cpu, success);
720 break;
721 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
722 trace_msm_pm_exit_pc(cpu, success);
723 break;
724 case MSM_PM_SLEEP_MODE_RETENTION:
725 trace_msm_pm_exit_ret(cpu, success);
726 break;
727 default:
728 break;
729 }
730}
731
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800732static int msm_pm_idle_prepare(struct cpuidle_device *dev,
733 struct cpuidle_driver *drv, int index,
734 void **msm_pm_idle_rs_limits)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700735{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700736 int i;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700737 unsigned int power_usage = -1;
Priyanka Mathur848bb4c2012-11-30 18:04:57 -0800738 int ret = MSM_PM_SLEEP_MODE_NOT_SELECTED;
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600739 uint32_t modified_time_us = 0;
740 struct msm_pm_time_params time_param;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700741
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600742 time_param.latency_us =
743 (uint32_t) pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
744 time_param.sleep_us =
745 (uint32_t) (ktime_to_us(tick_nohz_get_sleep_length())
746 & UINT_MAX);
747 time_param.modified_time_us = 0;
748
749 if (!dev->cpu)
750 time_param.next_event_us =
751 (uint32_t) (ktime_to_us(get_next_event_time())
752 & UINT_MAX);
753 else
754 time_param.next_event_us = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700755
756 for (i = 0; i < dev->state_count; i++) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700757 struct cpuidle_state *state = &drv->states[i];
758 struct cpuidle_state_usage *st_usage = &dev->states_usage[i];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700759 enum msm_pm_sleep_mode mode;
760 bool allow;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600761 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700762 int idx;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800763 void *rs_limits = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700764
Steve Mucklef132c6c2012-06-06 18:30:57 -0700765 mode = (enum msm_pm_sleep_mode) cpuidle_get_statedata(st_usage);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700766 idx = MSM_PM_MODE(dev->cpu, mode);
767
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600768 allow = msm_pm_sleep_modes[idx].idle_enabled &&
769 msm_pm_sleep_modes[idx].idle_supported;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700770
771 switch (mode) {
772 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700773 if (num_online_cpus() > 1)
Priyanka Mathur68a9bc52012-10-03 15:15:01 -0700774 allow = false;
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700775 break;
Mahesh Sivasubramanianba7e01c2012-09-04 18:22:51 -0600776 case MSM_PM_SLEEP_MODE_RETENTION:
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -0700777 /*
778 * The Krait BHS regulator doesn't have enough head
779 * room to drive the retention voltage on LDO and so
780 * has disabled retention
781 */
782 if (!msm_pm_ldo_retention_enabled)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700783 allow = false;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700784
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700785 if (msm_pm_retention_calls_tz && num_online_cpus() > 1)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700786 allow = false;
787 break;
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700788 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
789 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
790 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700791 default:
792 allow = false;
793 break;
794 }
795
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700796 if (!allow)
797 continue;
798
799 if (pm_sleep_ops.lowest_limits)
800 rs_limits = pm_sleep_ops.lowest_limits(true,
801 mode, &time_param, &power);
802
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700803 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700804 pr_info("CPU%u:%s:%s, latency %uus, slp %uus, lim %p\n",
805 dev->cpu, __func__, state->desc,
806 time_param.latency_us,
807 time_param.sleep_us, rs_limits);
808 if (!rs_limits)
809 continue;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700810
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700811 if (power < power_usage) {
812 power_usage = power;
813 modified_time_us = time_param.modified_time_us;
814 ret = mode;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800815 *msm_pm_idle_rs_limits = rs_limits;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700816 }
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700817
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700818 }
819
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600820 if (modified_time_us && !dev->cpu)
821 msm_pm_set_timer(modified_time_us);
Priyanka Mathur26b4a4b2012-11-05 13:45:45 -0800822
823 msm_pm_ftrace_lpm_enter(dev->cpu, time_param.latency_us,
824 time_param.sleep_us, time_param.next_event_us,
825 ret);
826
Steve Mucklef132c6c2012-06-06 18:30:57 -0700827 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700828}
829
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800830enum msm_pm_sleep_mode msm_pm_idle_enter(struct cpuidle_device *dev,
831 struct cpuidle_driver *drv, int index)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700832{
833 int64_t time;
Priyanka Mathur26b4a4b2012-11-05 13:45:45 -0800834 bool collapsed = 1;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800835 int exit_stat = -1;
836 enum msm_pm_sleep_mode sleep_mode;
837 void *msm_pm_idle_rs_limits = NULL;
Mahesh Sivasubramanian03698762013-04-18 16:18:46 -0600838 uint32_t sleep_delay = 1;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800839 int ret = -ENODEV;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800840 int notify_rpm = false;
841 bool timer_halted = false;
842
843 sleep_mode = msm_pm_idle_prepare(dev, drv, index,
844 &msm_pm_idle_rs_limits);
845
846 if (!msm_pm_idle_rs_limits) {
847 sleep_mode = MSM_PM_SLEEP_MODE_NOT_SELECTED;
848 goto cpuidle_enter_bail;
849 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700850
851 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
852 pr_info("CPU%u: %s: mode %d\n",
853 smp_processor_id(), __func__, sleep_mode);
854
855 time = ktime_to_ns(ktime_get());
856
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800857 if (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE) {
858 notify_rpm = true;
Mahesh Sivasubramanian03698762013-04-18 16:18:46 -0600859 sleep_delay = (uint32_t)msm_pm_timer_enter_idle();
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800860
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800861 if (sleep_delay == 0) /* 0 would mean infinite time */
862 sleep_delay = 1;
863 }
864
865 if (pm_sleep_ops.enter_sleep)
866 ret = pm_sleep_ops.enter_sleep(sleep_delay,
867 msm_pm_idle_rs_limits, true, notify_rpm);
868 if (ret)
869 goto cpuidle_enter_bail;
870
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700871 switch (sleep_mode) {
872 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
873 msm_pm_swfi();
874 exit_stat = MSM_PM_STAT_IDLE_WFI;
875 break;
876
877 case MSM_PM_SLEEP_MODE_RETENTION:
878 msm_pm_retention();
879 exit_stat = MSM_PM_STAT_RETENTION;
880 break;
881
882 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
883 collapsed = msm_pm_power_collapse_standalone(true);
884 exit_stat = MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE;
885 break;
886
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800887 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700888 if (MSM_PM_DEBUG_IDLE_CLK & msm_pm_debug_mask)
889 clock_debug_print_enabled();
Karthik Parsha6fb932d2012-01-24 18:04:12 -0800890
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800891 collapsed = msm_pm_power_collapse(true);
892 timer_halted = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700893
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700894 exit_stat = MSM_PM_STAT_IDLE_POWER_COLLAPSE;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800895 msm_pm_timer_exit_idle(timer_halted);
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700896 break;
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700897
898 case MSM_PM_SLEEP_MODE_NOT_SELECTED:
899 goto cpuidle_enter_bail;
900 break;
901
902 default:
903 __WARN();
904 goto cpuidle_enter_bail;
905 break;
906 }
907
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800908 if (pm_sleep_ops.exit_sleep)
909 pm_sleep_ops.exit_sleep(msm_pm_idle_rs_limits, true,
910 notify_rpm, collapsed);
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700911
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800912 time = ktime_to_ns(ktime_get()) - time;
913 msm_pm_ftrace_lpm_exit(smp_processor_id(), sleep_mode, collapsed);
914 if (exit_stat >= 0)
915 msm_pm_add_stat(exit_stat, time);
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700916 do_div(time, 1000);
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800917 dev->last_residency = (int) time;
918 return sleep_mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700919
920cpuidle_enter_bail:
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800921 dev->last_residency = 0;
922 if (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE)
923 msm_pm_timer_exit_idle(timer_halted);
924 sleep_mode = MSM_PM_SLEEP_MODE_NOT_SELECTED;
925 return sleep_mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700926}
927
Anji Jonnala02dac8d2013-03-06 21:31:04 +0530928int msm_pm_wait_cpu_shutdown(unsigned int cpu)
929{
930 int timeout = 10;
931
932 if (!msm_pm_slp_sts)
933 return 0;
934 if (!msm_pm_slp_sts[cpu].base_addr)
935 return 0;
936 while (timeout--) {
937 /*
938 * Check for the SPM of the core being hotplugged to set
939 * its sleep state.The SPM sleep state indicates that the
940 * core has been power collapsed.
941 */
942 int acc_sts = __raw_readl(msm_pm_slp_sts[cpu].base_addr);
943
944 if (acc_sts & msm_pm_slp_sts[cpu].mask)
945 return 0;
Mahesh Sivasubramanian65187f92013-03-19 15:14:06 -0600946 udelay(100);
Anji Jonnala02dac8d2013-03-06 21:31:04 +0530947 }
948
949 pr_info("%s(): Timed out waiting for CPU %u SPM to enter sleep state",
950 __func__, cpu);
951 return -EBUSY;
952}
953
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700954void msm_pm_cpu_enter_lowpower(unsigned int cpu)
955{
956 int i;
957 bool allow[MSM_PM_SLEEP_MODE_NR];
958
959 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
960 struct msm_pm_platform_data *mode;
961
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600962 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700963 allow[i] = mode->suspend_supported && mode->suspend_enabled;
964 }
965
966 if (MSM_PM_DEBUG_HOTPLUG & msm_pm_debug_mask)
967 pr_notice("CPU%u: %s: shutting down cpu\n", cpu, __func__);
968
Matt Wagantall5375aa72012-07-02 19:59:51 -0700969 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700970 msm_pm_power_collapse(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -0700971 else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700972 msm_pm_power_collapse_standalone(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -0700973 else if (allow[MSM_PM_SLEEP_MODE_RETENTION])
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600974 msm_pm_retention();
Stephen Boydbda74272012-08-09 14:01:27 -0700975 else
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700976 msm_pm_swfi();
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700977}
978
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -0700979static void msm_pm_ack_retention_disable(void *data)
980{
981 /*
982 * This is a NULL function to ensure that the core has woken up
983 * and is safe to disable retention.
984 */
985}
986/**
987 * msm_pm_enable_retention() - Disable/Enable retention on all cores
988 * @enable: Enable/Disable retention
989 *
990 */
991void msm_pm_enable_retention(bool enable)
992{
Matt Wagantalld06f5c22013-03-14 19:31:14 -0700993 if (enable == msm_pm_ldo_retention_enabled)
994 return;
995
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -0700996 msm_pm_ldo_retention_enabled = enable;
997 /*
998 * If retention is being disabled, wakeup all online core to ensure
999 * that it isn't executing retention. Offlined cores need not be woken
1000 * up as they enter the deepest sleep mode, namely RPM assited power
1001 * collapse
1002 */
Mahesh Sivasubramanian96f67d12013-03-14 22:44:42 -06001003 if (!enable) {
1004 preempt_disable();
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -07001005 smp_call_function_many(cpu_online_mask,
1006 msm_pm_ack_retention_disable,
1007 NULL, true);
Mahesh Sivasubramanian96f67d12013-03-14 22:44:42 -06001008 preempt_enable();
1009
1010
1011 }
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -07001012}
1013EXPORT_SYMBOL(msm_pm_enable_retention);
1014
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001015static int msm_pm_enter(suspend_state_t state)
1016{
1017 bool allow[MSM_PM_SLEEP_MODE_NR];
1018 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001019 int64_t period = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301020 int64_t time = msm_pm_timer_enter_suspend(&period);
Girish Mahadevandc318fd2012-08-17 16:48:05 -06001021 struct msm_pm_time_params time_param;
1022
1023 time_param.latency_us = -1;
1024 time_param.sleep_us = -1;
1025 time_param.next_event_us = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001026
1027 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1028 pr_info("%s\n", __func__);
1029
1030 if (smp_processor_id()) {
1031 __WARN();
1032 goto enter_exit;
1033 }
1034
1035
1036 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
1037 struct msm_pm_platform_data *mode;
1038
Praveen Chidambaram42da9d22012-03-30 12:16:34 -06001039 mode = &msm_pm_sleep_modes[MSM_PM_MODE(0, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001040 allow[i] = mode->suspend_supported && mode->suspend_enabled;
1041 }
1042
1043 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001044 void *rs_limits = NULL;
1045 int ret = -ENODEV;
1046 uint32_t power;
Mahesh Sivasubramanian03698762013-04-18 16:18:46 -06001047 uint32_t msm_pm_max_sleep_time = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001048
1049 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1050 pr_info("%s: power collapse\n", __func__);
1051
1052 clock_debug_print_enabled();
1053
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001054 if (msm_pm_sleep_time_override > 0) {
1055 int64_t ns = NSEC_PER_SEC *
1056 (int64_t) msm_pm_sleep_time_override;
Mahesh Sivasubramanian03698762013-04-18 16:18:46 -06001057 do_div(ns, NSEC_PER_SEC / SCLK_HZ);
1058 msm_pm_max_sleep_time = (uint32_t) ns;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001059 }
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001060
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001061 if (pm_sleep_ops.lowest_limits)
1062 rs_limits = pm_sleep_ops.lowest_limits(false,
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001063 MSM_PM_SLEEP_MODE_POWER_COLLAPSE, &time_param, &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001064
1065 if (rs_limits) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001066 if (pm_sleep_ops.enter_sleep)
1067 ret = pm_sleep_ops.enter_sleep(
1068 msm_pm_max_sleep_time,
1069 rs_limits, false, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001070 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -06001071 int collapsed = msm_pm_power_collapse(false);
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001072 if (pm_sleep_ops.exit_sleep) {
1073 pm_sleep_ops.exit_sleep(rs_limits,
1074 false, true, collapsed);
1075 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001076 }
1077 } else {
1078 pr_err("%s: cannot find the lowest power limit\n",
1079 __func__);
1080 }
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301081 time = msm_pm_timer_exit_suspend(time, period);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001082 msm_pm_add_stat(MSM_PM_STAT_SUSPEND, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001083 } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
1084 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1085 pr_info("%s: standalone power collapse\n", __func__);
1086 msm_pm_power_collapse_standalone(false);
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -06001087 } else if (allow[MSM_PM_SLEEP_MODE_RETENTION]) {
1088 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1089 pr_info("%s: retention\n", __func__);
1090 msm_pm_retention();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001091 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
1092 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1093 pr_info("%s: swfi\n", __func__);
1094 msm_pm_swfi();
1095 }
1096
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001097enter_exit:
1098 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1099 pr_info("%s: return\n", __func__);
1100
1101 return 0;
1102}
1103
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001104void msm_pm_set_sleep_ops(struct msm_pm_sleep_ops *ops)
1105{
1106 if (ops)
1107 pm_sleep_ops = *ops;
1108}
1109
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001110static const struct platform_suspend_ops msm_pm_ops = {
1111 .enter = msm_pm_enter,
1112 .valid = suspend_valid_only_mem,
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -06001113};
Anji Jonnala02dac8d2013-03-06 21:31:04 +05301114static int __devinit msm_cpu_status_probe(struct platform_device *pdev)
1115{
1116 struct msm_pm_sleep_status_data *pdata;
1117 char *key;
1118 u32 cpu;
1119
1120 if (!pdev)
1121 return -EFAULT;
1122
1123 msm_pm_slp_sts =
1124 kzalloc(sizeof(*msm_pm_slp_sts) * num_possible_cpus(),
1125 GFP_KERNEL);
1126
1127 if (!msm_pm_slp_sts)
1128 return -ENOMEM;
1129
1130 if (pdev->dev.of_node) {
1131 struct resource *res;
1132 u32 offset;
1133 int rc;
1134 u32 mask;
1135
1136 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1137 if (!res)
1138 goto fail_free_mem;
1139
1140 key = "qcom,cpu-alias-addr";
1141 rc = of_property_read_u32(pdev->dev.of_node, key, &offset);
1142
1143 if (rc)
1144 goto fail_free_mem;
1145
1146 key = "qcom,sleep-status-mask";
1147 rc = of_property_read_u32(pdev->dev.of_node, key,
1148 &mask);
1149 if (rc)
1150 goto fail_free_mem;
1151
1152 for_each_possible_cpu(cpu) {
1153 msm_pm_slp_sts[cpu].base_addr =
1154 ioremap(res->start + cpu * offset,
1155 resource_size(res));
1156 msm_pm_slp_sts[cpu].mask = mask;
1157
1158 if (!msm_pm_slp_sts[cpu].base_addr)
1159 goto failed_of_node;
1160 }
1161
1162 } else {
1163 pdata = pdev->dev.platform_data;
1164 if (!pdev->dev.platform_data)
1165 goto fail_free_mem;
1166
1167 for_each_possible_cpu(cpu) {
1168 msm_pm_slp_sts[cpu].base_addr =
1169 pdata->base_addr + cpu * pdata->cpu_offset;
1170 msm_pm_slp_sts[cpu].mask = pdata->mask;
1171 }
1172 }
1173
1174 return 0;
1175
1176failed_of_node:
1177 pr_info("%s(): Failed to key=%s\n", __func__, key);
1178 for_each_possible_cpu(cpu) {
1179 if (msm_pm_slp_sts[cpu].base_addr)
1180 iounmap(msm_pm_slp_sts[cpu].base_addr);
1181 }
1182fail_free_mem:
1183 kfree(msm_pm_slp_sts);
1184 return -EINVAL;
1185
1186};
1187
1188static struct of_device_id msm_slp_sts_match_tbl[] = {
1189 {.compatible = "qcom,cpu-sleep-status"},
1190 {},
1191};
1192
1193static struct platform_driver msm_cpu_status_driver = {
1194 .probe = msm_cpu_status_probe,
1195 .driver = {
1196 .name = "cpu_slp_status",
1197 .owner = THIS_MODULE,
1198 .of_match_table = msm_slp_sts_match_tbl,
1199 },
1200};
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -06001201
Praveen Chidambaramede45d02013-02-20 17:45:21 -07001202static int __init msm_pm_setup_saved_state(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001203{
1204 pgd_t *pc_pgd;
1205 pmd_t *pmd;
1206 unsigned long pmdval;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001207 unsigned long exit_phys;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301208
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001209 /* Page table for cores to come back up safely. */
1210 pc_pgd = pgd_alloc(&init_mm);
1211 if (!pc_pgd)
1212 return -ENOMEM;
1213
Steve Mucklef132c6c2012-06-06 18:30:57 -07001214 exit_phys = virt_to_phys(msm_pm_collapse_exit);
1215
1216 pmd = pmd_offset(pud_offset(pc_pgd + pgd_index(exit_phys),exit_phys),
1217 exit_phys);
1218 pmdval = (exit_phys & PGDIR_MASK) |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001219 PMD_TYPE_SECT | PMD_SECT_AP_WRITE;
1220 pmd[0] = __pmd(pmdval);
1221 pmd[1] = __pmd(pmdval + (1 << (PGDIR_SHIFT - 1)));
1222
Steve Mucklefcece052012-02-18 20:09:58 -08001223 msm_saved_state_phys =
1224 allocate_contiguous_ebi_nomap(CPU_SAVED_STATE_SIZE *
1225 num_possible_cpus(), 4);
1226 if (!msm_saved_state_phys)
1227 return -ENOMEM;
1228 msm_saved_state = ioremap_nocache(msm_saved_state_phys,
1229 CPU_SAVED_STATE_SIZE *
1230 num_possible_cpus());
1231 if (!msm_saved_state)
1232 return -ENOMEM;
1233
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001234 /* It is remotely possible that the code in msm_pm_collapse_exit()
1235 * which turns on the MMU with this mapping is in the
1236 * next even-numbered megabyte beyond the
1237 * start of msm_pm_collapse_exit().
1238 * Map this megabyte in as well.
1239 */
1240 pmd[2] = __pmd(pmdval + (2 << (PGDIR_SHIFT - 1)));
1241 flush_pmd_entry(pmd);
1242 msm_pm_pc_pgd = virt_to_phys(pc_pgd);
Steve Muckle730ad7a2012-02-21 15:26:37 -08001243 clean_caches((unsigned long)&msm_pm_pc_pgd, sizeof(msm_pm_pc_pgd),
1244 virt_to_phys(&msm_pm_pc_pgd));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001245
Praveen Chidambaramede45d02013-02-20 17:45:21 -07001246 return 0;
1247}
1248core_initcall(msm_pm_setup_saved_state);
1249
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001250static void setup_broadcast_timer(void *arg)
1251{
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001252 int cpu = smp_processor_id();
1253
Mahesh Sivasubramanian738eaf62013-04-30 12:42:59 -06001254 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ON, &cpu);
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001255}
1256
1257static int setup_broadcast_cpuhp_notify(struct notifier_block *n,
1258 unsigned long action, void *hcpu)
1259{
Mahesh Sivasubramanian738eaf62013-04-30 12:42:59 -06001260 int cpu = (unsigned long)hcpu;
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001261
1262 switch (action & ~CPU_TASKS_FROZEN) {
1263 case CPU_ONLINE:
Mahesh Sivasubramanian738eaf62013-04-30 12:42:59 -06001264 smp_call_function_single(cpu, setup_broadcast_timer, NULL, 1);
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001265 break;
1266 }
1267
1268 return NOTIFY_OK;
1269}
1270
1271static struct notifier_block setup_broadcast_notifier = {
1272 .notifier_call = setup_broadcast_cpuhp_notify,
1273};
1274
Praveen Chidambaramede45d02013-02-20 17:45:21 -07001275static int __init msm_pm_init(void)
1276{
1277 enum msm_pm_time_stats_id enable_stats[] = {
1278 MSM_PM_STAT_IDLE_WFI,
1279 MSM_PM_STAT_RETENTION,
1280 MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE,
1281 MSM_PM_STAT_IDLE_POWER_COLLAPSE,
1282 MSM_PM_STAT_SUSPEND,
1283 };
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001284 msm_pm_mode_sysfs_add();
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301285 msm_pm_add_stats(enable_stats, ARRAY_SIZE(enable_stats));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001286 suspend_set_ops(&msm_pm_ops);
Girish Mahadevandc318fd2012-08-17 16:48:05 -06001287 hrtimer_init(&pm_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001288 msm_cpuidle_init();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001289
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001290 if (msm_pm_pc_reset_timer) {
Mahesh Sivasubramanian738eaf62013-04-30 12:42:59 -06001291 on_each_cpu(setup_broadcast_timer, NULL, 1);
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001292 register_cpu_notifier(&setup_broadcast_notifier);
1293 }
1294
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001295 return 0;
1296}
1297
Girish Mahadevan55944992012-10-26 11:03:07 -06001298static void __devinit msm_pm_set_flush_fn(uint32_t pc_mode)
1299{
1300 msm_pm_disable_l2_fn = NULL;
1301 msm_pm_enable_l2_fn = NULL;
1302 msm_pm_flush_l2_fn = outer_flush_all;
1303
1304 if (pc_mode == MSM_PM_PC_NOTZ_L2_EXT) {
1305 msm_pm_disable_l2_fn = outer_disable;
1306 msm_pm_enable_l2_fn = outer_resume;
1307 }
1308}
1309
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001310struct msm_pc_debug_counters_buffer {
1311 void __iomem *reg;
1312 u32 len;
1313 char buf[MAX_BUF_SIZE];
1314};
1315
1316static inline u32 msm_pc_debug_counters_read_register(
1317 void __iomem *reg, int index , int offset)
1318{
1319 return readl_relaxed(reg + (index * 4 + offset) * 4);
1320}
1321
1322static char *counter_name[] = {
1323 "PC Entry Counter",
1324 "Warmboot Entry Counter",
1325 "PC Bailout Counter"
1326};
1327
1328static int msm_pc_debug_counters_copy(
1329 struct msm_pc_debug_counters_buffer *data)
1330{
1331 int j;
1332 u32 stat;
1333 unsigned int cpu;
1334
1335 for_each_possible_cpu(cpu) {
1336 data->len += scnprintf(data->buf + data->len,
1337 sizeof(data->buf)-data->len,
1338 "CPU%d\n", cpu);
1339
1340 for (j = 0; j < NUM_OF_COUNTERS; j++) {
1341 stat = msm_pc_debug_counters_read_register(
1342 data->reg, cpu, j);
1343 data->len += scnprintf(data->buf + data->len,
1344 sizeof(data->buf)-data->len,
1345 "\t%s : %d\n", counter_name[j],
1346 stat);
1347 }
1348
1349 }
1350
1351 return data->len;
1352}
1353
1354static int msm_pc_debug_counters_file_read(struct file *file,
1355 char __user *bufu, size_t count, loff_t *ppos)
1356{
1357 struct msm_pc_debug_counters_buffer *data;
1358
1359 data = file->private_data;
1360
1361 if (!data)
1362 return -EINVAL;
1363
Mahesh Sivasubramanian6900fdc2013-04-18 14:02:52 -06001364 if (!bufu)
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001365 return -EINVAL;
1366
1367 if (!access_ok(VERIFY_WRITE, bufu, count))
1368 return -EFAULT;
1369
1370 if (*ppos >= data->len && data->len == 0)
1371 data->len = msm_pc_debug_counters_copy(data);
1372
1373 return simple_read_from_buffer(bufu, count, ppos,
1374 data->buf, data->len);
1375}
1376
1377static int msm_pc_debug_counters_file_open(struct inode *inode,
1378 struct file *file)
1379{
1380 struct msm_pc_debug_counters_buffer *buf;
1381 void __iomem *msm_pc_debug_counters_reg;
1382
1383 msm_pc_debug_counters_reg = inode->i_private;
1384
1385 if (!msm_pc_debug_counters_reg)
1386 return -EINVAL;
1387
1388 file->private_data = kzalloc(
1389 sizeof(struct msm_pc_debug_counters_buffer), GFP_KERNEL);
1390
1391 if (!file->private_data) {
1392 pr_err("%s: ERROR kmalloc failed to allocate %d bytes\n",
1393 __func__, sizeof(struct msm_pc_debug_counters_buffer));
1394
1395 return -ENOMEM;
1396 }
1397
1398 buf = file->private_data;
1399 buf->reg = msm_pc_debug_counters_reg;
1400
1401 return 0;
1402}
1403
1404static int msm_pc_debug_counters_file_close(struct inode *inode,
1405 struct file *file)
1406{
1407 kfree(file->private_data);
1408 return 0;
1409}
1410
1411static const struct file_operations msm_pc_debug_counters_fops = {
1412 .open = msm_pc_debug_counters_file_open,
1413 .read = msm_pc_debug_counters_file_read,
1414 .release = msm_pc_debug_counters_file_close,
1415 .llseek = no_llseek,
1416};
1417
Girish Mahadevan55944992012-10-26 11:03:07 -06001418static int __devinit msm_pm_8x60_probe(struct platform_device *pdev)
1419{
1420 char *key = NULL;
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001421 struct dentry *dent = NULL;
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001422 struct resource *res = NULL;
1423 int i ;
1424 struct msm_pm_init_data_type pdata_local;
Girish Mahadevan55944992012-10-26 11:03:07 -06001425 int ret = 0;
1426
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001427 memset(&pdata_local, 0, sizeof(struct msm_pm_init_data_type));
1428
1429 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1430 if (res) {
1431 msm_pc_debug_counters_phys = res->start;
1432 WARN_ON(resource_size(res) < SZ_64);
1433 msm_pc_debug_counters = devm_ioremap(&pdev->dev, res->start,
1434 resource_size(res));
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001435 if (msm_pc_debug_counters)
1436 for (i = 0; i < resource_size(res)/4; i++)
1437 __raw_writel(0, msm_pc_debug_counters + i * 4);
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001438
1439 }
1440
1441 if (!msm_pc_debug_counters) {
1442 msm_pc_debug_counters = 0;
1443 msm_pc_debug_counters_phys = 0;
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001444 } else {
1445 dent = debugfs_create_file("pc_debug_counter", S_IRUGO, NULL,
1446 msm_pc_debug_counters,
1447 &msm_pc_debug_counters_fops);
1448 if (!dent)
1449 pr_err("%s: ERROR debugfs_create_file failed\n",
1450 __func__);
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001451 }
1452
Girish Mahadevan55944992012-10-26 11:03:07 -06001453 if (!pdev->dev.of_node) {
1454 struct msm_pm_init_data_type *d = pdev->dev.platform_data;
1455
1456 if (!d)
1457 goto pm_8x60_probe_done;
1458
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001459 memcpy(&pdata_local, d, sizeof(struct msm_pm_init_data_type));
1460
Girish Mahadevan55944992012-10-26 11:03:07 -06001461 } else {
1462 key = "qcom,pc-mode";
Archana Sathyakumar2b91dc82013-02-01 17:38:23 -07001463 ret = msm_pm_get_pc_mode(pdev->dev.of_node,
1464 key,
1465 &pdata_local.pc_mode);
Girish Mahadevan55944992012-10-26 11:03:07 -06001466 if (ret) {
Archana Sathyakumar2b91dc82013-02-01 17:38:23 -07001467 pr_debug("%s: Error reading key %s",
Girish Mahadevan55944992012-10-26 11:03:07 -06001468 __func__, key);
Archana Sathyakumar2b91dc82013-02-01 17:38:23 -07001469 return -EINVAL;
Girish Mahadevan55944992012-10-26 11:03:07 -06001470 }
Girish Mahadevan55944992012-10-26 11:03:07 -06001471
1472 key = "qcom,use-sync-timer";
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001473 pdata_local.use_sync_timer =
Girish Mahadevan55944992012-10-26 11:03:07 -06001474 of_property_read_bool(pdev->dev.of_node, key);
Praveen Chidambaram4b8df032013-01-18 16:21:16 -07001475
1476 key = "qcom,saw-turns-off-pll";
1477 msm_no_ramp_down_pc = of_property_read_bool(pdev->dev.of_node,
1478 key);
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001479
1480 key = "qcom,pc-resets-timer";
1481 msm_pm_pc_reset_timer = of_property_read_bool(
1482 pdev->dev.of_node, key);
Girish Mahadevan55944992012-10-26 11:03:07 -06001483 }
1484
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001485 if (pdata_local.cp15_data.reg_data &&
1486 pdata_local.cp15_data.reg_saved_state_size > 0) {
1487 cp15_data.reg_data = kzalloc(sizeof(uint32_t) *
1488 pdata_local.cp15_data.reg_saved_state_size,
1489 GFP_KERNEL);
1490 if (!cp15_data.reg_data)
1491 return -ENOMEM;
1492
1493 cp15_data.reg_val = kzalloc(sizeof(uint32_t) *
1494 pdata_local.cp15_data.reg_saved_state_size,
1495 GFP_KERNEL);
1496 if (cp15_data.reg_val)
1497 return -ENOMEM;
1498
1499 memcpy(cp15_data.reg_data, pdata_local.cp15_data.reg_data,
1500 pdata_local.cp15_data.reg_saved_state_size *
1501 sizeof(uint32_t));
1502 }
1503
1504 msm_pm_set_flush_fn(pdata_local.pc_mode);
1505 msm_pm_use_sync_timer = pdata_local.use_sync_timer;
1506 msm_pm_retention_calls_tz = pdata_local.retention_calls_tz;
1507
Girish Mahadevan55944992012-10-26 11:03:07 -06001508pm_8x60_probe_done:
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001509 msm_pm_init();
Anji Jonnala02dac8d2013-03-06 21:31:04 +05301510 if (pdev->dev.of_node)
1511 of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
1512
Girish Mahadevan55944992012-10-26 11:03:07 -06001513 return ret;
1514}
1515
1516static struct of_device_id msm_pm_8x60_table[] = {
1517 {.compatible = "qcom,pm-8x60"},
1518 {},
1519};
1520
1521static struct platform_driver msm_pm_8x60_driver = {
1522 .probe = msm_pm_8x60_probe,
1523 .driver = {
1524 .name = "pm-8x60",
1525 .owner = THIS_MODULE,
1526 .of_match_table = msm_pm_8x60_table,
1527 },
1528};
1529
1530static int __init msm_pm_8x60_init(void)
1531{
Anji Jonnala02dac8d2013-03-06 21:31:04 +05301532 int rc;
1533
1534 rc = platform_driver_register(&msm_cpu_status_driver);
1535
1536 if (rc) {
1537 pr_err("%s(): failed to register driver %s\n", __func__,
1538 msm_cpu_status_driver.driver.name);
1539 return rc;
1540 }
1541
Girish Mahadevan55944992012-10-26 11:03:07 -06001542 return platform_driver_register(&msm_pm_8x60_driver);
1543}
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001544device_initcall(msm_pm_8x60_init);