blob: 3c50bc61ae6862e510784e929c7bd42c2abb165a [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>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070032#include <mach/msm_iomap.h>
Praveen Chidambaram192979f2012-04-25 18:30:23 -060033#include <mach/socinfo.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070034#include <mach/system.h>
Girish Mahadevand27ca4a2012-08-15 09:21:23 -060035#include <mach/scm.h>
36#include <mach/socinfo.h>
Praveen Chidambaramc594a092012-09-18 19:48:29 -060037#include <mach/msm-krait-l2-accessors.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070038#include <asm/cacheflush.h>
39#include <asm/hardware/gic.h>
40#include <asm/pgtable.h>
41#include <asm/pgalloc.h>
Girish Mahadevan55944992012-10-26 11:03:07 -060042#include <asm/outercache.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070043#ifdef CONFIG_VFP
44#include <asm/vfp.h>
45#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070046#include "acpuclock.h"
47#include "clock.h"
48#include "avs.h"
Abhijeet Dharmapurikarefaca4f2011-12-27 16:24:07 -080049#include <mach/cpuidle.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070050#include "idle.h"
Matt Wagantall7cca4642012-02-01 16:43:24 -080051#include "pm.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070052#include "scm-boot.h"
53#include "spm.h"
54#include "timer.h"
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -060055#include "pm-boot.h"
Girish Mahadevandc318fd2012-08-17 16:48:05 -060056#include <mach/event_timer.h>
Priyanka Mathur26b4a4b2012-11-05 13:45:45 -080057#define CREATE_TRACE_POINTS
58#include "trace_msm_low_power.h"
Praveen Chidambaramf27a5152013-02-01 11:44:53 -070059
60#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)
67#define MSM_PM_SLEEP_TICK_LIMIT (0x6DDD000)
68
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;
130static uint32_t msm_pm_max_sleep_time;
Praveen Chidambaram4b8df032013-01-18 16:21:16 -0700131static bool msm_no_ramp_down_pc;
Anji Jonnala02dac8d2013-03-06 21:31:04 +0530132static struct msm_pm_sleep_status_data *msm_pm_slp_sts;
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
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700407/*
408 * Convert time from nanoseconds to slow clock ticks, then cap it to the
409 * specified limit
410 */
411static int64_t msm_pm_convert_and_cap_time(int64_t time_ns, int64_t limit)
412{
413 do_div(time_ns, NSEC_PER_SEC / SCLK_HZ);
414 return (time_ns > limit) ? limit : time_ns;
415}
416
417/*
418 * Set the sleep time for suspend. 0 means infinite sleep time.
419 */
420void msm_pm_set_max_sleep_time(int64_t max_sleep_time_ns)
421{
422 if (max_sleep_time_ns == 0) {
423 msm_pm_max_sleep_time = 0;
424 } else {
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700425 msm_pm_max_sleep_time =
426 (uint32_t)msm_pm_convert_and_cap_time(
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700427 max_sleep_time_ns, MSM_PM_SLEEP_TICK_LIMIT);
428
429 if (msm_pm_max_sleep_time == 0)
430 msm_pm_max_sleep_time = 1;
431 }
432
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700433 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700434 pr_info("%s: Requested %lld ns Giving %u sclk ticks\n",
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700435 __func__, max_sleep_time_ns,
436 msm_pm_max_sleep_time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700437}
438EXPORT_SYMBOL(msm_pm_set_max_sleep_time);
439
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600440static void msm_pm_save_cpu_reg(void)
441{
442 int i;
443
444 /* Only on core0 */
445 if (smp_processor_id())
446 return;
447
448 /**
449 * On some targets, L2 PC will turn off may reset the core
450 * configuration for the mux and the default may not make the core
451 * happy when it resumes.
452 * Save the active vdd, and set the core vdd to QSB max vdd, so that
453 * when the core resumes, it is capable of supporting the current QSB
454 * rate. Then restore the active vdd before switching the acpuclk rate.
455 */
456 if (msm_pm_get_l2_flush_flag() == 1) {
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700457 cp15_data.active_vdd = msm_spm_get_vdd(0);
458 for (i = 0; i < cp15_data.reg_saved_state_size; i++)
459 cp15_data.reg_val[i] =
460 get_l2_indirect_reg(
461 cp15_data.reg_data[i]);
462 msm_spm_set_vdd(0, cp15_data.qsb_pc_vdd);
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600463 }
464}
465
466static void msm_pm_restore_cpu_reg(void)
467{
468 int i;
469
470 /* Only on core0 */
471 if (smp_processor_id())
472 return;
473
474 if (msm_pm_get_l2_flush_flag() == 1) {
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700475 for (i = 0; i < cp15_data.reg_saved_state_size; i++)
476 set_l2_indirect_reg(
477 cp15_data.reg_data[i],
478 cp15_data.reg_val[i]);
479 msm_spm_set_vdd(0, cp15_data.active_vdd);
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600480 }
481}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700482
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700483static void msm_pm_swfi(void)
484{
485 msm_pm_config_hw_before_swfi();
486 msm_arch_idle();
487}
488
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600489static void msm_pm_retention(void)
490{
491 int ret = 0;
492
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600493 msm_pm_config_hw_before_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600494 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_POWER_RETENTION, false);
495 WARN_ON(ret);
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600496
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700497 if (msm_pm_retention_calls_tz)
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600498 scm_call_atomic1(SCM_SVC_BOOT, SCM_CMD_TERMINATE_PC,
499 SCM_L2_RETENTION);
500 else
501 msm_arch_idle();
502
503 msm_pm_config_hw_after_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600504}
505
Stephen Boydb29750d2012-02-21 01:21:32 -0800506static bool __ref msm_pm_spm_power_collapse(
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700507 unsigned int cpu, bool from_idle, bool notify_rpm)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700508{
509 void *entry;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600510 bool collapsed = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700511 int ret;
512
513 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
514 pr_info("CPU%u: %s: notify_rpm %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700515 cpu, __func__, (int) notify_rpm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700516
517 ret = msm_spm_set_low_power_mode(
518 MSM_SPM_MODE_POWER_COLLAPSE, notify_rpm);
519 WARN_ON(ret);
520
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700521 entry = (!cpu || from_idle) ?
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700522 msm_pm_collapse_exit : msm_secondary_startup;
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700523 msm_pm_boot_config_before_pc(cpu, virt_to_phys(entry));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700524
525 if (MSM_PM_DEBUG_RESET_VECTOR & msm_pm_debug_mask)
526 pr_info("CPU%u: %s: program vector to %p\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700527 cpu, __func__, entry);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700528
Girish Mahadevan55944992012-10-26 11:03:07 -0600529 collapsed = msm_pm_collapse();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700530
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700531 msm_pm_boot_config_after_pc(cpu);
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -0600532
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700533 if (collapsed) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700534 cpu_init();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700535 local_fiq_enable();
536 }
537
538 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
539 pr_info("CPU%u: %s: msm_pm_collapse returned, collapsed %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700540 cpu, __func__, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700541
542 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
543 WARN_ON(ret);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600544 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700545}
546
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600547static bool msm_pm_power_collapse_standalone(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700548{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700549 unsigned int cpu = smp_processor_id();
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700550 unsigned int avsdscr;
551 unsigned int avscsr;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600552 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700553
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700554 avsdscr = avs_get_avsdscr();
555 avscsr = avs_get_avscsr();
556 avs_set_avscsr(0); /* Disable AVS */
557
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700558 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, false);
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700559
560 avs_set_avsdscr(avsdscr);
561 avs_set_avscsr(avscsr);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600562 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700563}
564
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600565static bool msm_pm_power_collapse(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700566{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700567 unsigned int cpu = smp_processor_id();
Praveen Chidambaram4b8df032013-01-18 16:21:16 -0700568 unsigned long saved_acpuclk_rate = 0;
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700569 unsigned int avsdscr;
570 unsigned int avscsr;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600571 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700572
573 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
574 pr_info("CPU%u: %s: idle %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700575 cpu, __func__, (int)from_idle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700576
577 msm_pm_config_hw_before_power_down();
578 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700579 pr_info("CPU%u: %s: pre power down\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700580
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700581 avsdscr = avs_get_avsdscr();
582 avscsr = avs_get_avscsr();
583 avs_set_avscsr(0); /* Disable AVS */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700584
Praveen Chidambaram4b8df032013-01-18 16:21:16 -0700585 if (cpu_online(cpu) && !msm_no_ramp_down_pc)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700586 saved_acpuclk_rate = acpuclk_power_collapse();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700587
588 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
589 pr_info("CPU%u: %s: change clock rate (old rate = %lu)\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700590 cpu, __func__, saved_acpuclk_rate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700591
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700592 if (cp15_data.save_cp15)
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600593 msm_pm_save_cpu_reg();
594
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700595 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700596
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700597 if (cp15_data.save_cp15)
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600598 msm_pm_restore_cpu_reg();
599
Girish Mahadevan4e025d92012-02-29 13:26:51 -0700600 if (cpu_online(cpu)) {
601 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
602 pr_info("CPU%u: %s: restore clock rate to %lu\n",
603 cpu, __func__, saved_acpuclk_rate);
Praveen Chidambaram4b8df032013-01-18 16:21:16 -0700604 if (!msm_no_ramp_down_pc &&
605 acpuclk_set_rate(cpu, saved_acpuclk_rate, SETRATE_PC)
606 < 0)
Girish Mahadevan4e025d92012-02-29 13:26:51 -0700607 pr_err("CPU%u: %s: failed to restore clock rate(%lu)\n",
608 cpu, __func__, saved_acpuclk_rate);
609 } else {
610 unsigned int gic_dist_enabled;
611 unsigned int gic_dist_pending;
612 gic_dist_enabled = readl_relaxed(
613 MSM_QGIC_DIST_BASE + GIC_DIST_ENABLE_CLEAR);
614 gic_dist_pending = readl_relaxed(
615 MSM_QGIC_DIST_BASE + GIC_DIST_PENDING_SET);
616 mb();
617 gic_dist_pending &= gic_dist_enabled;
618
619 if (gic_dist_pending)
620 pr_err("CPU %d interrupted during hotplug.Pending int 0x%x\n",
621 cpu, gic_dist_pending);
622 }
623
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700624
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700625 avs_set_avsdscr(avsdscr);
626 avs_set_avscsr(avscsr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700627 msm_pm_config_hw_after_power_up();
628 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700629 pr_info("CPU%u: %s: post power up\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700630
631 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700632 pr_info("CPU%u: %s: return\n", cpu, __func__);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600633 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700634}
635
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600636static int64_t msm_pm_timer_enter_idle(void)
637{
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700638 if (msm_pm_use_sync_timer)
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600639 return ktime_to_ns(tick_nohz_get_sleep_length());
640
641 return msm_timer_enter_idle();
642}
643
644static void msm_pm_timer_exit_idle(bool timer_halted)
645{
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700646 if (msm_pm_use_sync_timer)
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600647 return;
648
649 msm_timer_exit_idle((int) timer_halted);
650}
651
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530652static int64_t msm_pm_timer_enter_suspend(int64_t *period)
653{
Anji Jonnalac02367a2012-07-01 02:56:11 +0530654 int64_t time = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530655
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700656 if (msm_pm_use_sync_timer)
Mahesh Sivasubramanian0eaeb302013-03-14 14:56:32 -0600657 return sched_clock();
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530658
659 time = msm_timer_get_sclk_time(period);
660 if (!time)
661 pr_err("%s: Unable to read sclk.\n", __func__);
662
663 return time;
664}
665
666static int64_t msm_pm_timer_exit_suspend(int64_t time, int64_t period)
667{
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700668 if (msm_pm_use_sync_timer)
Mahesh Sivasubramanian0eaeb302013-03-14 14:56:32 -0600669 return sched_clock() - time;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530670
671 if (time != 0) {
672 int64_t end_time = msm_timer_get_sclk_time(NULL);
673 if (end_time != 0) {
674 time = end_time - time;
675 if (time < 0)
676 time += period;
677 } else
678 time = 0;
679 }
680
681 return time;
682}
683
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600684/**
685 * pm_hrtimer_cb() : Callback function for hrtimer created if the
686 * core needs to be awake to handle an event.
687 * @hrtimer : Pointer to hrtimer
688 */
689static enum hrtimer_restart pm_hrtimer_cb(struct hrtimer *hrtimer)
690{
691 return HRTIMER_NORESTART;
692}
693
694/**
695 * msm_pm_set_timer() : Set an hrtimer to wakeup the core in time
696 * to handle an event.
697 */
698static void msm_pm_set_timer(uint32_t modified_time_us)
699{
700 u64 modified_time_ns = modified_time_us * NSEC_PER_USEC;
701 ktime_t modified_ktime = ns_to_ktime(modified_time_ns);
702 pm_hrtimer.function = pm_hrtimer_cb;
703 hrtimer_start(&pm_hrtimer, modified_ktime, HRTIMER_MODE_ABS);
704}
705
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700706/******************************************************************************
707 * External Idle/Suspend Functions
708 *****************************************************************************/
709
710void arch_idle(void)
711{
712 return;
713}
714
Priyanka Mathur26b4a4b2012-11-05 13:45:45 -0800715static inline void msm_pm_ftrace_lpm_enter(unsigned int cpu,
716 uint32_t latency, uint32_t sleep_us,
717 uint32_t wake_up,
718 enum msm_pm_sleep_mode mode)
719{
720 switch (mode) {
721 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
722 trace_msm_pm_enter_wfi(cpu, latency, sleep_us, wake_up);
723 break;
724 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
725 trace_msm_pm_enter_spc(cpu, latency, sleep_us, wake_up);
726 break;
727 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
728 trace_msm_pm_enter_pc(cpu, latency, sleep_us, wake_up);
729 break;
730 case MSM_PM_SLEEP_MODE_RETENTION:
731 trace_msm_pm_enter_ret(cpu, latency, sleep_us, wake_up);
732 break;
733 default:
734 break;
735 }
736}
737
738static inline void msm_pm_ftrace_lpm_exit(unsigned int cpu,
739 enum msm_pm_sleep_mode mode,
740 bool success)
741{
742 switch (mode) {
743 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
744 trace_msm_pm_exit_wfi(cpu, success);
745 break;
746 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
747 trace_msm_pm_exit_spc(cpu, success);
748 break;
749 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
750 trace_msm_pm_exit_pc(cpu, success);
751 break;
752 case MSM_PM_SLEEP_MODE_RETENTION:
753 trace_msm_pm_exit_ret(cpu, success);
754 break;
755 default:
756 break;
757 }
758}
759
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800760static int msm_pm_idle_prepare(struct cpuidle_device *dev,
761 struct cpuidle_driver *drv, int index,
762 void **msm_pm_idle_rs_limits)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700763{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700764 int i;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700765 unsigned int power_usage = -1;
Priyanka Mathur848bb4c2012-11-30 18:04:57 -0800766 int ret = MSM_PM_SLEEP_MODE_NOT_SELECTED;
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600767 uint32_t modified_time_us = 0;
768 struct msm_pm_time_params time_param;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700769
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600770 time_param.latency_us =
771 (uint32_t) pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
772 time_param.sleep_us =
773 (uint32_t) (ktime_to_us(tick_nohz_get_sleep_length())
774 & UINT_MAX);
775 time_param.modified_time_us = 0;
776
777 if (!dev->cpu)
778 time_param.next_event_us =
779 (uint32_t) (ktime_to_us(get_next_event_time())
780 & UINT_MAX);
781 else
782 time_param.next_event_us = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700783
784 for (i = 0; i < dev->state_count; i++) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700785 struct cpuidle_state *state = &drv->states[i];
786 struct cpuidle_state_usage *st_usage = &dev->states_usage[i];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700787 enum msm_pm_sleep_mode mode;
788 bool allow;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600789 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700790 int idx;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800791 void *rs_limits = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700792
Steve Mucklef132c6c2012-06-06 18:30:57 -0700793 mode = (enum msm_pm_sleep_mode) cpuidle_get_statedata(st_usage);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700794 idx = MSM_PM_MODE(dev->cpu, mode);
795
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600796 allow = msm_pm_sleep_modes[idx].idle_enabled &&
797 msm_pm_sleep_modes[idx].idle_supported;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700798
799 switch (mode) {
800 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700801 if (num_online_cpus() > 1)
Priyanka Mathur68a9bc52012-10-03 15:15:01 -0700802 allow = false;
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700803 break;
Mahesh Sivasubramanianba7e01c2012-09-04 18:22:51 -0600804 case MSM_PM_SLEEP_MODE_RETENTION:
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -0700805 /*
806 * The Krait BHS regulator doesn't have enough head
807 * room to drive the retention voltage on LDO and so
808 * has disabled retention
809 */
810 if (!msm_pm_ldo_retention_enabled)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700811 allow = false;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700812
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700813 if (msm_pm_retention_calls_tz && num_online_cpus() > 1)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700814 allow = false;
815 break;
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700816 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
817 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
818 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700819 default:
820 allow = false;
821 break;
822 }
823
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700824 if (!allow)
825 continue;
826
827 if (pm_sleep_ops.lowest_limits)
828 rs_limits = pm_sleep_ops.lowest_limits(true,
829 mode, &time_param, &power);
830
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700831 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700832 pr_info("CPU%u:%s:%s, latency %uus, slp %uus, lim %p\n",
833 dev->cpu, __func__, state->desc,
834 time_param.latency_us,
835 time_param.sleep_us, rs_limits);
836 if (!rs_limits)
837 continue;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700838
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700839 if (power < power_usage) {
840 power_usage = power;
841 modified_time_us = time_param.modified_time_us;
842 ret = mode;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800843 *msm_pm_idle_rs_limits = rs_limits;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700844 }
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700845
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700846 }
847
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600848 if (modified_time_us && !dev->cpu)
849 msm_pm_set_timer(modified_time_us);
Priyanka Mathur26b4a4b2012-11-05 13:45:45 -0800850
851 msm_pm_ftrace_lpm_enter(dev->cpu, time_param.latency_us,
852 time_param.sleep_us, time_param.next_event_us,
853 ret);
854
Steve Mucklef132c6c2012-06-06 18:30:57 -0700855 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700856}
857
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800858enum msm_pm_sleep_mode msm_pm_idle_enter(struct cpuidle_device *dev,
859 struct cpuidle_driver *drv, int index)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700860{
861 int64_t time;
Priyanka Mathur26b4a4b2012-11-05 13:45:45 -0800862 bool collapsed = 1;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800863 int exit_stat = -1;
864 enum msm_pm_sleep_mode sleep_mode;
865 void *msm_pm_idle_rs_limits = NULL;
866 int sleep_delay = 1;
867 int ret = -ENODEV;
868 int64_t timer_expiration = 0;
869 int notify_rpm = false;
870 bool timer_halted = false;
871
872 sleep_mode = msm_pm_idle_prepare(dev, drv, index,
873 &msm_pm_idle_rs_limits);
874
875 if (!msm_pm_idle_rs_limits) {
876 sleep_mode = MSM_PM_SLEEP_MODE_NOT_SELECTED;
877 goto cpuidle_enter_bail;
878 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700879
880 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
881 pr_info("CPU%u: %s: mode %d\n",
882 smp_processor_id(), __func__, sleep_mode);
883
884 time = ktime_to_ns(ktime_get());
885
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800886 if (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE) {
887 notify_rpm = true;
888 timer_expiration = msm_pm_timer_enter_idle();
889
890 sleep_delay = (uint32_t) msm_pm_convert_and_cap_time(
891 timer_expiration, MSM_PM_SLEEP_TICK_LIMIT);
892 if (sleep_delay == 0) /* 0 would mean infinite time */
893 sleep_delay = 1;
894 }
895
896 if (pm_sleep_ops.enter_sleep)
897 ret = pm_sleep_ops.enter_sleep(sleep_delay,
898 msm_pm_idle_rs_limits, true, notify_rpm);
899 if (ret)
900 goto cpuidle_enter_bail;
901
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700902 switch (sleep_mode) {
903 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
904 msm_pm_swfi();
905 exit_stat = MSM_PM_STAT_IDLE_WFI;
906 break;
907
908 case MSM_PM_SLEEP_MODE_RETENTION:
909 msm_pm_retention();
910 exit_stat = MSM_PM_STAT_RETENTION;
911 break;
912
913 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
914 collapsed = msm_pm_power_collapse_standalone(true);
915 exit_stat = MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE;
916 break;
917
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800918 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700919 if (MSM_PM_DEBUG_IDLE_CLK & msm_pm_debug_mask)
920 clock_debug_print_enabled();
Karthik Parsha6fb932d2012-01-24 18:04:12 -0800921
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800922 collapsed = msm_pm_power_collapse(true);
923 timer_halted = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700924
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700925 exit_stat = MSM_PM_STAT_IDLE_POWER_COLLAPSE;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800926 msm_pm_timer_exit_idle(timer_halted);
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700927 break;
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700928
929 case MSM_PM_SLEEP_MODE_NOT_SELECTED:
930 goto cpuidle_enter_bail;
931 break;
932
933 default:
934 __WARN();
935 goto cpuidle_enter_bail;
936 break;
937 }
938
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800939 if (pm_sleep_ops.exit_sleep)
940 pm_sleep_ops.exit_sleep(msm_pm_idle_rs_limits, true,
941 notify_rpm, collapsed);
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700942
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800943 time = ktime_to_ns(ktime_get()) - time;
944 msm_pm_ftrace_lpm_exit(smp_processor_id(), sleep_mode, collapsed);
945 if (exit_stat >= 0)
946 msm_pm_add_stat(exit_stat, time);
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700947 do_div(time, 1000);
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800948 dev->last_residency = (int) time;
949 return sleep_mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700950
951cpuidle_enter_bail:
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800952 dev->last_residency = 0;
953 if (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE)
954 msm_pm_timer_exit_idle(timer_halted);
955 sleep_mode = MSM_PM_SLEEP_MODE_NOT_SELECTED;
956 return sleep_mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700957}
958
Anji Jonnala02dac8d2013-03-06 21:31:04 +0530959int msm_pm_wait_cpu_shutdown(unsigned int cpu)
960{
961 int timeout = 10;
962
963 if (!msm_pm_slp_sts)
964 return 0;
965 if (!msm_pm_slp_sts[cpu].base_addr)
966 return 0;
967 while (timeout--) {
968 /*
969 * Check for the SPM of the core being hotplugged to set
970 * its sleep state.The SPM sleep state indicates that the
971 * core has been power collapsed.
972 */
973 int acc_sts = __raw_readl(msm_pm_slp_sts[cpu].base_addr);
974
975 if (acc_sts & msm_pm_slp_sts[cpu].mask)
976 return 0;
Mahesh Sivasubramanian65187f92013-03-19 15:14:06 -0600977 udelay(100);
Anji Jonnala02dac8d2013-03-06 21:31:04 +0530978 }
979
980 pr_info("%s(): Timed out waiting for CPU %u SPM to enter sleep state",
981 __func__, cpu);
982 return -EBUSY;
983}
984
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700985void msm_pm_cpu_enter_lowpower(unsigned int cpu)
986{
987 int i;
988 bool allow[MSM_PM_SLEEP_MODE_NR];
989
990 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
991 struct msm_pm_platform_data *mode;
992
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600993 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700994 allow[i] = mode->suspend_supported && mode->suspend_enabled;
995 }
996
997 if (MSM_PM_DEBUG_HOTPLUG & msm_pm_debug_mask)
998 pr_notice("CPU%u: %s: shutting down cpu\n", cpu, __func__);
999
Matt Wagantall5375aa72012-07-02 19:59:51 -07001000 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -07001001 msm_pm_power_collapse(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -07001002 else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -07001003 msm_pm_power_collapse_standalone(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -07001004 else if (allow[MSM_PM_SLEEP_MODE_RETENTION])
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -06001005 msm_pm_retention();
Stephen Boydbda74272012-08-09 14:01:27 -07001006 else
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -07001007 msm_pm_swfi();
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -07001008}
1009
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -07001010static void msm_pm_ack_retention_disable(void *data)
1011{
1012 /*
1013 * This is a NULL function to ensure that the core has woken up
1014 * and is safe to disable retention.
1015 */
1016}
1017/**
1018 * msm_pm_enable_retention() - Disable/Enable retention on all cores
1019 * @enable: Enable/Disable retention
1020 *
1021 */
1022void msm_pm_enable_retention(bool enable)
1023{
Matt Wagantalld06f5c22013-03-14 19:31:14 -07001024 if (enable == msm_pm_ldo_retention_enabled)
1025 return;
1026
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -07001027 msm_pm_ldo_retention_enabled = enable;
1028 /*
1029 * If retention is being disabled, wakeup all online core to ensure
1030 * that it isn't executing retention. Offlined cores need not be woken
1031 * up as they enter the deepest sleep mode, namely RPM assited power
1032 * collapse
1033 */
Mahesh Sivasubramanian96f67d12013-03-14 22:44:42 -06001034 if (!enable) {
1035 preempt_disable();
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -07001036 smp_call_function_many(cpu_online_mask,
1037 msm_pm_ack_retention_disable,
1038 NULL, true);
Mahesh Sivasubramanian96f67d12013-03-14 22:44:42 -06001039 preempt_enable();
1040
1041
1042 }
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -07001043}
1044EXPORT_SYMBOL(msm_pm_enable_retention);
1045
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001046static int msm_pm_enter(suspend_state_t state)
1047{
1048 bool allow[MSM_PM_SLEEP_MODE_NR];
1049 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001050 int64_t period = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301051 int64_t time = msm_pm_timer_enter_suspend(&period);
Girish Mahadevandc318fd2012-08-17 16:48:05 -06001052 struct msm_pm_time_params time_param;
1053
1054 time_param.latency_us = -1;
1055 time_param.sleep_us = -1;
1056 time_param.next_event_us = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001057
1058 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1059 pr_info("%s\n", __func__);
1060
1061 if (smp_processor_id()) {
1062 __WARN();
1063 goto enter_exit;
1064 }
1065
1066
1067 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
1068 struct msm_pm_platform_data *mode;
1069
Praveen Chidambaram42da9d22012-03-30 12:16:34 -06001070 mode = &msm_pm_sleep_modes[MSM_PM_MODE(0, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001071 allow[i] = mode->suspend_supported && mode->suspend_enabled;
1072 }
1073
1074 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001075 void *rs_limits = NULL;
1076 int ret = -ENODEV;
1077 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001078
1079 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1080 pr_info("%s: power collapse\n", __func__);
1081
1082 clock_debug_print_enabled();
1083
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001084 if (msm_pm_sleep_time_override > 0) {
1085 int64_t ns = NSEC_PER_SEC *
1086 (int64_t) msm_pm_sleep_time_override;
1087 msm_pm_set_max_sleep_time(ns);
1088 msm_pm_sleep_time_override = 0;
1089 }
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001090
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001091 if (pm_sleep_ops.lowest_limits)
1092 rs_limits = pm_sleep_ops.lowest_limits(false,
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001093 MSM_PM_SLEEP_MODE_POWER_COLLAPSE, &time_param, &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001094
1095 if (rs_limits) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001096 if (pm_sleep_ops.enter_sleep)
1097 ret = pm_sleep_ops.enter_sleep(
1098 msm_pm_max_sleep_time,
1099 rs_limits, false, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001100 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -06001101 int collapsed = msm_pm_power_collapse(false);
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001102 if (pm_sleep_ops.exit_sleep) {
1103 pm_sleep_ops.exit_sleep(rs_limits,
1104 false, true, collapsed);
1105 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001106 }
1107 } else {
1108 pr_err("%s: cannot find the lowest power limit\n",
1109 __func__);
1110 }
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301111 time = msm_pm_timer_exit_suspend(time, period);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001112 msm_pm_add_stat(MSM_PM_STAT_SUSPEND, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001113 } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
1114 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1115 pr_info("%s: standalone power collapse\n", __func__);
1116 msm_pm_power_collapse_standalone(false);
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -06001117 } else if (allow[MSM_PM_SLEEP_MODE_RETENTION]) {
1118 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1119 pr_info("%s: retention\n", __func__);
1120 msm_pm_retention();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001121 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
1122 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1123 pr_info("%s: swfi\n", __func__);
1124 msm_pm_swfi();
1125 }
1126
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001127enter_exit:
1128 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1129 pr_info("%s: return\n", __func__);
1130
1131 return 0;
1132}
1133
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001134void msm_pm_set_sleep_ops(struct msm_pm_sleep_ops *ops)
1135{
1136 if (ops)
1137 pm_sleep_ops = *ops;
1138}
1139
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001140static const struct platform_suspend_ops msm_pm_ops = {
1141 .enter = msm_pm_enter,
1142 .valid = suspend_valid_only_mem,
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -06001143};
Anji Jonnala02dac8d2013-03-06 21:31:04 +05301144static int __devinit msm_cpu_status_probe(struct platform_device *pdev)
1145{
1146 struct msm_pm_sleep_status_data *pdata;
1147 char *key;
1148 u32 cpu;
1149
1150 if (!pdev)
1151 return -EFAULT;
1152
1153 msm_pm_slp_sts =
1154 kzalloc(sizeof(*msm_pm_slp_sts) * num_possible_cpus(),
1155 GFP_KERNEL);
1156
1157 if (!msm_pm_slp_sts)
1158 return -ENOMEM;
1159
1160 if (pdev->dev.of_node) {
1161 struct resource *res;
1162 u32 offset;
1163 int rc;
1164 u32 mask;
1165
1166 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1167 if (!res)
1168 goto fail_free_mem;
1169
1170 key = "qcom,cpu-alias-addr";
1171 rc = of_property_read_u32(pdev->dev.of_node, key, &offset);
1172
1173 if (rc)
1174 goto fail_free_mem;
1175
1176 key = "qcom,sleep-status-mask";
1177 rc = of_property_read_u32(pdev->dev.of_node, key,
1178 &mask);
1179 if (rc)
1180 goto fail_free_mem;
1181
1182 for_each_possible_cpu(cpu) {
1183 msm_pm_slp_sts[cpu].base_addr =
1184 ioremap(res->start + cpu * offset,
1185 resource_size(res));
1186 msm_pm_slp_sts[cpu].mask = mask;
1187
1188 if (!msm_pm_slp_sts[cpu].base_addr)
1189 goto failed_of_node;
1190 }
1191
1192 } else {
1193 pdata = pdev->dev.platform_data;
1194 if (!pdev->dev.platform_data)
1195 goto fail_free_mem;
1196
1197 for_each_possible_cpu(cpu) {
1198 msm_pm_slp_sts[cpu].base_addr =
1199 pdata->base_addr + cpu * pdata->cpu_offset;
1200 msm_pm_slp_sts[cpu].mask = pdata->mask;
1201 }
1202 }
1203
1204 return 0;
1205
1206failed_of_node:
1207 pr_info("%s(): Failed to key=%s\n", __func__, key);
1208 for_each_possible_cpu(cpu) {
1209 if (msm_pm_slp_sts[cpu].base_addr)
1210 iounmap(msm_pm_slp_sts[cpu].base_addr);
1211 }
1212fail_free_mem:
1213 kfree(msm_pm_slp_sts);
1214 return -EINVAL;
1215
1216};
1217
1218static struct of_device_id msm_slp_sts_match_tbl[] = {
1219 {.compatible = "qcom,cpu-sleep-status"},
1220 {},
1221};
1222
1223static struct platform_driver msm_cpu_status_driver = {
1224 .probe = msm_cpu_status_probe,
1225 .driver = {
1226 .name = "cpu_slp_status",
1227 .owner = THIS_MODULE,
1228 .of_match_table = msm_slp_sts_match_tbl,
1229 },
1230};
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -06001231
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001232static int __devinit msm_pm_init(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001233{
1234 pgd_t *pc_pgd;
1235 pmd_t *pmd;
1236 unsigned long pmdval;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301237 enum msm_pm_time_stats_id enable_stats[] = {
1238 MSM_PM_STAT_IDLE_WFI,
1239 MSM_PM_STAT_RETENTION,
1240 MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE,
1241 MSM_PM_STAT_IDLE_POWER_COLLAPSE,
1242 MSM_PM_STAT_SUSPEND,
1243 };
Steve Mucklef132c6c2012-06-06 18:30:57 -07001244 unsigned long exit_phys;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301245
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001246 /* Page table for cores to come back up safely. */
1247 pc_pgd = pgd_alloc(&init_mm);
1248 if (!pc_pgd)
1249 return -ENOMEM;
1250
Steve Mucklef132c6c2012-06-06 18:30:57 -07001251 exit_phys = virt_to_phys(msm_pm_collapse_exit);
1252
1253 pmd = pmd_offset(pud_offset(pc_pgd + pgd_index(exit_phys),exit_phys),
1254 exit_phys);
1255 pmdval = (exit_phys & PGDIR_MASK) |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001256 PMD_TYPE_SECT | PMD_SECT_AP_WRITE;
1257 pmd[0] = __pmd(pmdval);
1258 pmd[1] = __pmd(pmdval + (1 << (PGDIR_SHIFT - 1)));
1259
Steve Mucklefcece052012-02-18 20:09:58 -08001260 msm_saved_state_phys =
1261 allocate_contiguous_ebi_nomap(CPU_SAVED_STATE_SIZE *
1262 num_possible_cpus(), 4);
1263 if (!msm_saved_state_phys)
1264 return -ENOMEM;
1265 msm_saved_state = ioremap_nocache(msm_saved_state_phys,
1266 CPU_SAVED_STATE_SIZE *
1267 num_possible_cpus());
1268 if (!msm_saved_state)
1269 return -ENOMEM;
1270
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001271 /* It is remotely possible that the code in msm_pm_collapse_exit()
1272 * which turns on the MMU with this mapping is in the
1273 * next even-numbered megabyte beyond the
1274 * start of msm_pm_collapse_exit().
1275 * Map this megabyte in as well.
1276 */
1277 pmd[2] = __pmd(pmdval + (2 << (PGDIR_SHIFT - 1)));
1278 flush_pmd_entry(pmd);
1279 msm_pm_pc_pgd = virt_to_phys(pc_pgd);
Steve Muckle730ad7a2012-02-21 15:26:37 -08001280 clean_caches((unsigned long)&msm_pm_pc_pgd, sizeof(msm_pm_pc_pgd),
1281 virt_to_phys(&msm_pm_pc_pgd));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001282
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001283 msm_pm_mode_sysfs_add();
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301284 msm_pm_add_stats(enable_stats, ARRAY_SIZE(enable_stats));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001285 suspend_set_ops(&msm_pm_ops);
Girish Mahadevandc318fd2012-08-17 16:48:05 -06001286 hrtimer_init(&pm_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001287 msm_cpuidle_init();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001288
1289 return 0;
1290}
1291
Girish Mahadevan55944992012-10-26 11:03:07 -06001292static void __devinit msm_pm_set_flush_fn(uint32_t pc_mode)
1293{
1294 msm_pm_disable_l2_fn = NULL;
1295 msm_pm_enable_l2_fn = NULL;
1296 msm_pm_flush_l2_fn = outer_flush_all;
1297
1298 if (pc_mode == MSM_PM_PC_NOTZ_L2_EXT) {
1299 msm_pm_disable_l2_fn = outer_disable;
1300 msm_pm_enable_l2_fn = outer_resume;
1301 }
1302}
1303
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001304struct msm_pc_debug_counters_buffer {
1305 void __iomem *reg;
1306 u32 len;
1307 char buf[MAX_BUF_SIZE];
1308};
1309
1310static inline u32 msm_pc_debug_counters_read_register(
1311 void __iomem *reg, int index , int offset)
1312{
1313 return readl_relaxed(reg + (index * 4 + offset) * 4);
1314}
1315
1316static char *counter_name[] = {
1317 "PC Entry Counter",
1318 "Warmboot Entry Counter",
1319 "PC Bailout Counter"
1320};
1321
1322static int msm_pc_debug_counters_copy(
1323 struct msm_pc_debug_counters_buffer *data)
1324{
1325 int j;
1326 u32 stat;
1327 unsigned int cpu;
1328
1329 for_each_possible_cpu(cpu) {
1330 data->len += scnprintf(data->buf + data->len,
1331 sizeof(data->buf)-data->len,
1332 "CPU%d\n", cpu);
1333
1334 for (j = 0; j < NUM_OF_COUNTERS; j++) {
1335 stat = msm_pc_debug_counters_read_register(
1336 data->reg, cpu, j);
1337 data->len += scnprintf(data->buf + data->len,
1338 sizeof(data->buf)-data->len,
1339 "\t%s : %d\n", counter_name[j],
1340 stat);
1341 }
1342
1343 }
1344
1345 return data->len;
1346}
1347
1348static int msm_pc_debug_counters_file_read(struct file *file,
1349 char __user *bufu, size_t count, loff_t *ppos)
1350{
1351 struct msm_pc_debug_counters_buffer *data;
1352
1353 data = file->private_data;
1354
1355 if (!data)
1356 return -EINVAL;
1357
1358 if (!bufu || count < 0)
1359 return -EINVAL;
1360
1361 if (!access_ok(VERIFY_WRITE, bufu, count))
1362 return -EFAULT;
1363
1364 if (*ppos >= data->len && data->len == 0)
1365 data->len = msm_pc_debug_counters_copy(data);
1366
1367 return simple_read_from_buffer(bufu, count, ppos,
1368 data->buf, data->len);
1369}
1370
1371static int msm_pc_debug_counters_file_open(struct inode *inode,
1372 struct file *file)
1373{
1374 struct msm_pc_debug_counters_buffer *buf;
1375 void __iomem *msm_pc_debug_counters_reg;
1376
1377 msm_pc_debug_counters_reg = inode->i_private;
1378
1379 if (!msm_pc_debug_counters_reg)
1380 return -EINVAL;
1381
1382 file->private_data = kzalloc(
1383 sizeof(struct msm_pc_debug_counters_buffer), GFP_KERNEL);
1384
1385 if (!file->private_data) {
1386 pr_err("%s: ERROR kmalloc failed to allocate %d bytes\n",
1387 __func__, sizeof(struct msm_pc_debug_counters_buffer));
1388
1389 return -ENOMEM;
1390 }
1391
1392 buf = file->private_data;
1393 buf->reg = msm_pc_debug_counters_reg;
1394
1395 return 0;
1396}
1397
1398static int msm_pc_debug_counters_file_close(struct inode *inode,
1399 struct file *file)
1400{
1401 kfree(file->private_data);
1402 return 0;
1403}
1404
1405static const struct file_operations msm_pc_debug_counters_fops = {
1406 .open = msm_pc_debug_counters_file_open,
1407 .read = msm_pc_debug_counters_file_read,
1408 .release = msm_pc_debug_counters_file_close,
1409 .llseek = no_llseek,
1410};
1411
Girish Mahadevan55944992012-10-26 11:03:07 -06001412static int __devinit msm_pm_8x60_probe(struct platform_device *pdev)
1413{
1414 char *key = NULL;
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001415 struct dentry *dent = NULL;
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001416 struct resource *res = NULL;
1417 int i ;
1418 struct msm_pm_init_data_type pdata_local;
Girish Mahadevan55944992012-10-26 11:03:07 -06001419 int ret = 0;
1420
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001421 memset(&pdata_local, 0, sizeof(struct msm_pm_init_data_type));
1422
1423 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1424 if (res) {
1425 msm_pc_debug_counters_phys = res->start;
1426 WARN_ON(resource_size(res) < SZ_64);
1427 msm_pc_debug_counters = devm_ioremap(&pdev->dev, res->start,
1428 resource_size(res));
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001429 if (msm_pc_debug_counters)
1430 for (i = 0; i < resource_size(res)/4; i++)
1431 __raw_writel(0, msm_pc_debug_counters + i * 4);
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001432
1433 }
1434
1435 if (!msm_pc_debug_counters) {
1436 msm_pc_debug_counters = 0;
1437 msm_pc_debug_counters_phys = 0;
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001438 } else {
1439 dent = debugfs_create_file("pc_debug_counter", S_IRUGO, NULL,
1440 msm_pc_debug_counters,
1441 &msm_pc_debug_counters_fops);
1442 if (!dent)
1443 pr_err("%s: ERROR debugfs_create_file failed\n",
1444 __func__);
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001445 }
1446
Girish Mahadevan55944992012-10-26 11:03:07 -06001447 if (!pdev->dev.of_node) {
1448 struct msm_pm_init_data_type *d = pdev->dev.platform_data;
1449
1450 if (!d)
1451 goto pm_8x60_probe_done;
1452
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001453 memcpy(&pdata_local, d, sizeof(struct msm_pm_init_data_type));
1454
Girish Mahadevan55944992012-10-26 11:03:07 -06001455 } else {
1456 key = "qcom,pc-mode";
Archana Sathyakumar2b91dc82013-02-01 17:38:23 -07001457 ret = msm_pm_get_pc_mode(pdev->dev.of_node,
1458 key,
1459 &pdata_local.pc_mode);
Girish Mahadevan55944992012-10-26 11:03:07 -06001460 if (ret) {
Archana Sathyakumar2b91dc82013-02-01 17:38:23 -07001461 pr_debug("%s: Error reading key %s",
Girish Mahadevan55944992012-10-26 11:03:07 -06001462 __func__, key);
Archana Sathyakumar2b91dc82013-02-01 17:38:23 -07001463 return -EINVAL;
Girish Mahadevan55944992012-10-26 11:03:07 -06001464 }
Girish Mahadevan55944992012-10-26 11:03:07 -06001465
1466 key = "qcom,use-sync-timer";
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001467 pdata_local.use_sync_timer =
Girish Mahadevan55944992012-10-26 11:03:07 -06001468 of_property_read_bool(pdev->dev.of_node, key);
Praveen Chidambaram4b8df032013-01-18 16:21:16 -07001469
1470 key = "qcom,saw-turns-off-pll";
1471 msm_no_ramp_down_pc = of_property_read_bool(pdev->dev.of_node,
1472 key);
Girish Mahadevan55944992012-10-26 11:03:07 -06001473 }
1474
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001475 if (pdata_local.cp15_data.reg_data &&
1476 pdata_local.cp15_data.reg_saved_state_size > 0) {
1477 cp15_data.reg_data = kzalloc(sizeof(uint32_t) *
1478 pdata_local.cp15_data.reg_saved_state_size,
1479 GFP_KERNEL);
1480 if (!cp15_data.reg_data)
1481 return -ENOMEM;
1482
1483 cp15_data.reg_val = kzalloc(sizeof(uint32_t) *
1484 pdata_local.cp15_data.reg_saved_state_size,
1485 GFP_KERNEL);
1486 if (cp15_data.reg_val)
1487 return -ENOMEM;
1488
1489 memcpy(cp15_data.reg_data, pdata_local.cp15_data.reg_data,
1490 pdata_local.cp15_data.reg_saved_state_size *
1491 sizeof(uint32_t));
1492 }
1493
1494 msm_pm_set_flush_fn(pdata_local.pc_mode);
1495 msm_pm_use_sync_timer = pdata_local.use_sync_timer;
1496 msm_pm_retention_calls_tz = pdata_local.retention_calls_tz;
1497
Girish Mahadevan55944992012-10-26 11:03:07 -06001498pm_8x60_probe_done:
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001499 msm_pm_init();
Anji Jonnala02dac8d2013-03-06 21:31:04 +05301500 if (pdev->dev.of_node)
1501 of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
1502
Girish Mahadevan55944992012-10-26 11:03:07 -06001503 return ret;
1504}
1505
1506static struct of_device_id msm_pm_8x60_table[] = {
1507 {.compatible = "qcom,pm-8x60"},
1508 {},
1509};
1510
1511static struct platform_driver msm_pm_8x60_driver = {
1512 .probe = msm_pm_8x60_probe,
1513 .driver = {
1514 .name = "pm-8x60",
1515 .owner = THIS_MODULE,
1516 .of_match_table = msm_pm_8x60_table,
1517 },
1518};
1519
1520static int __init msm_pm_8x60_init(void)
1521{
Anji Jonnala02dac8d2013-03-06 21:31:04 +05301522 int rc;
1523
1524 rc = platform_driver_register(&msm_cpu_status_driver);
1525
1526 if (rc) {
1527 pr_err("%s(): failed to register driver %s\n", __func__,
1528 msm_cpu_status_driver.driver.name);
1529 return rc;
1530 }
1531
Girish Mahadevan55944992012-10-26 11:03:07 -06001532 return platform_driver_register(&msm_pm_8x60_driver);
1533}
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001534device_initcall(msm_pm_8x60_init);