blob: 5a6e66ab51becf8a352443b08d397460875ea64b [file] [log] [blame]
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -07001/* Copyright (c) 2010-2013, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13
Priyanka Mathur13bdad12013-01-28 15:52:56 -080014#include <linux/debugfs.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070015#include <linux/module.h>
16#include <linux/kernel.h>
17#include <linux/init.h>
18#include <linux/completion.h>
19#include <linux/cpuidle.h>
20#include <linux/interrupt.h>
21#include <linux/io.h>
22#include <linux/ktime.h>
23#include <linux/pm.h>
Steve Mucklef132c6c2012-06-06 18:30:57 -070024#include <linux/pm_qos.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070025#include <linux/smp.h>
26#include <linux/suspend.h>
27#include <linux/tick.h>
Anji Jonnala02dac8d2013-03-06 21:31:04 +053028#include <linux/delay.h>
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -060029#include <linux/platform_device.h>
Anji Jonnala02dac8d2013-03-06 21:31:04 +053030#include <linux/of_platform.h>
Abhijeet Dharmapurikare8f83852013-02-06 18:57:17 -080031#include <linux/regulator/krait-regulator.h>
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -060032#include <linux/cpu.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070033#include <mach/msm_iomap.h>
Praveen Chidambaram192979f2012-04-25 18:30:23 -060034#include <mach/socinfo.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070035#include <mach/system.h>
Girish Mahadevand27ca4a2012-08-15 09:21:23 -060036#include <mach/scm.h>
37#include <mach/socinfo.h>
Praveen Chidambaramc594a092012-09-18 19:48:29 -060038#include <mach/msm-krait-l2-accessors.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070039#include <asm/cacheflush.h>
40#include <asm/hardware/gic.h>
41#include <asm/pgtable.h>
42#include <asm/pgalloc.h>
Girish Mahadevan55944992012-10-26 11:03:07 -060043#include <asm/outercache.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070044#ifdef CONFIG_VFP
45#include <asm/vfp.h>
46#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070047#include "acpuclock.h"
48#include "clock.h"
49#include "avs.h"
Abhijeet Dharmapurikarefaca4f2011-12-27 16:24:07 -080050#include <mach/cpuidle.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070051#include "idle.h"
Matt Wagantall7cca4642012-02-01 16:43:24 -080052#include "pm.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070053#include "scm-boot.h"
54#include "spm.h"
55#include "timer.h"
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -060056#include "pm-boot.h"
Girish Mahadevandc318fd2012-08-17 16:48:05 -060057#include <mach/event_timer.h>
Priyanka Mathur26b4a4b2012-11-05 13:45:45 -080058#define CREATE_TRACE_POINTS
59#include "trace_msm_low_power.h"
Praveen Chidambaramf27a5152013-02-01 11:44:53 -070060#define SCM_L2_RETENTION (0x2)
61#define SCM_CMD_TERMINATE_PC (0x2)
62
63#define GET_CPU_OF_ATTR(attr) \
64 (container_of(attr, struct msm_pm_kobj_attribute, ka)->cpu)
65
66#define SCLK_HZ (32768)
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;
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -0600133static bool msm_pm_pc_reset_timer;
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700134
Archana Sathyakumar2b91dc82013-02-01 17:38:23 -0700135static int msm_pm_get_pc_mode(struct device_node *node,
136 const char *key, uint32_t *pc_mode_val)
137{
138 struct pc_mode_of {
139 uint32_t mode;
140 char *mode_name;
141 };
142 int i;
143 struct pc_mode_of pc_modes[] = {
144 {MSM_PM_PC_TZ_L2_INT, "tz_l2_int"},
145 {MSM_PM_PC_NOTZ_L2_EXT, "no_tz_l2_ext"},
146 {MSM_PM_PC_TZ_L2_EXT , "tz_l2_ext"} };
147 int ret;
148 const char *pc_mode_str;
149
150 ret = of_property_read_string(node, key, &pc_mode_str);
151 if (ret) {
152 pr_debug("%s: Cannot read %s,defaulting to 0", __func__, key);
153 pc_mode_val = MSM_PM_PC_TZ_L2_INT;
154 ret = 0;
155 } else {
156 ret = -EINVAL;
157 for (i = 0; i < ARRAY_SIZE(pc_modes); i++) {
158 if (!strncmp(pc_mode_str, pc_modes[i].mode_name,
159 strlen(pc_modes[i].mode_name))) {
160 *pc_mode_val = pc_modes[i].mode;
161 ret = 0;
162 break;
163 }
164 }
165 }
166 return ret;
167}
168
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700169/*
170 * Write out the attribute.
171 */
172static ssize_t msm_pm_mode_attr_show(
173 struct kobject *kobj, struct kobj_attribute *attr, char *buf)
174{
175 int ret = -EINVAL;
176 int i;
177
178 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
179 struct kernel_param kp;
180 unsigned int cpu;
181 struct msm_pm_platform_data *mode;
182
183 if (msm_pm_sleep_mode_labels[i] == NULL)
184 continue;
185
186 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
187 continue;
188
189 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600190 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700191
192 if (!strcmp(attr->attr.name,
193 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
194 u32 arg = mode->suspend_enabled;
195 kp.arg = &arg;
196 ret = param_get_ulong(buf, &kp);
197 } else if (!strcmp(attr->attr.name,
198 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
199 u32 arg = mode->idle_enabled;
200 kp.arg = &arg;
201 ret = param_get_ulong(buf, &kp);
202 }
203
204 break;
205 }
206
207 if (ret > 0) {
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600208 strlcat(buf, "\n", PAGE_SIZE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700209 ret++;
210 }
211
212 return ret;
213}
214
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700215static ssize_t msm_pm_mode_attr_store(struct kobject *kobj,
216 struct kobj_attribute *attr, const char *buf, size_t count)
217{
218 int ret = -EINVAL;
219 int i;
220
221 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
222 struct kernel_param kp;
223 unsigned int cpu;
224 struct msm_pm_platform_data *mode;
225
226 if (msm_pm_sleep_mode_labels[i] == NULL)
227 continue;
228
229 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
230 continue;
231
232 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600233 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700234
235 if (!strcmp(attr->attr.name,
236 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
237 kp.arg = &mode->suspend_enabled;
238 ret = param_set_byte(buf, &kp);
239 } else if (!strcmp(attr->attr.name,
240 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
241 kp.arg = &mode->idle_enabled;
242 ret = param_set_byte(buf, &kp);
243 }
244
245 break;
246 }
247
248 return ret ? ret : count;
249}
250
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700251static int __devinit msm_pm_mode_sysfs_add_cpu(
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700252 unsigned int cpu, struct kobject *modes_kobj)
253{
254 char cpu_name[8];
255 struct kobject *cpu_kobj;
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600256 struct msm_pm_sysfs_sleep_mode *mode = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700257 int i, j, k;
258 int ret;
259
260 snprintf(cpu_name, sizeof(cpu_name), "cpu%u", cpu);
261 cpu_kobj = kobject_create_and_add(cpu_name, modes_kobj);
262 if (!cpu_kobj) {
263 pr_err("%s: cannot create %s kobject\n", __func__, cpu_name);
264 ret = -ENOMEM;
265 goto mode_sysfs_add_cpu_exit;
266 }
267
268 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
269 int idx = MSM_PM_MODE(cpu, i);
270
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600271 if ((!msm_pm_sleep_modes[idx].suspend_supported)
272 && (!msm_pm_sleep_modes[idx].idle_supported))
273 continue;
274
275 if (!msm_pm_sleep_mode_labels[i] ||
276 !msm_pm_sleep_mode_labels[i][0])
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700277 continue;
278
279 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
280 if (!mode) {
281 pr_err("%s: cannot allocate memory for attributes\n",
282 __func__);
283 ret = -ENOMEM;
284 goto mode_sysfs_add_cpu_exit;
285 }
286
287 mode->kobj = kobject_create_and_add(
288 msm_pm_sleep_mode_labels[i], cpu_kobj);
289 if (!mode->kobj) {
290 pr_err("%s: cannot create kobject\n", __func__);
291 ret = -ENOMEM;
292 goto mode_sysfs_add_cpu_exit;
293 }
294
295 for (k = 0, j = 0; k < MSM_PM_MODE_ATTR_NR; k++) {
296 if ((k == MSM_PM_MODE_ATTR_IDLE) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600297 !msm_pm_sleep_modes[idx].idle_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700298 continue;
299 if ((k == MSM_PM_MODE_ATTR_SUSPEND) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600300 !msm_pm_sleep_modes[idx].suspend_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700301 continue;
Stephen Boydd2059c32012-07-03 15:11:15 -0700302 sysfs_attr_init(&mode->kas[j].ka.attr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700303 mode->kas[j].cpu = cpu;
304 mode->kas[j].ka.attr.mode = 0644;
305 mode->kas[j].ka.show = msm_pm_mode_attr_show;
306 mode->kas[j].ka.store = msm_pm_mode_attr_store;
307 mode->kas[j].ka.attr.name = msm_pm_mode_attr_labels[k];
308 mode->attrs[j] = &mode->kas[j].ka.attr;
309 j++;
310 }
311 mode->attrs[j] = NULL;
312
313 mode->attr_group.attrs = mode->attrs;
314 ret = sysfs_create_group(mode->kobj, &mode->attr_group);
315 if (ret) {
316 pr_err("%s: cannot create kobject attribute group\n",
317 __func__);
318 goto mode_sysfs_add_cpu_exit;
319 }
320 }
321
322 ret = 0;
323
324mode_sysfs_add_cpu_exit:
Praveen Chidambaramd5ac2d32011-10-24 14:30:27 -0600325 if (ret) {
Praveen Chidambaram2cfda632011-10-11 16:58:09 -0600326 if (mode && mode->kobj)
327 kobject_del(mode->kobj);
328 kfree(mode);
329 }
330
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700331 return ret;
332}
333
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700334int __devinit msm_pm_mode_sysfs_add(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700335{
336 struct kobject *module_kobj;
337 struct kobject *modes_kobj;
338 unsigned int cpu;
339 int ret;
340
341 module_kobj = kset_find_obj(module_kset, KBUILD_MODNAME);
342 if (!module_kobj) {
343 pr_err("%s: cannot find kobject for module %s\n",
344 __func__, KBUILD_MODNAME);
345 ret = -ENOENT;
346 goto mode_sysfs_add_exit;
347 }
348
349 modes_kobj = kobject_create_and_add("modes", module_kobj);
350 if (!modes_kobj) {
351 pr_err("%s: cannot create modes kobject\n", __func__);
352 ret = -ENOMEM;
353 goto mode_sysfs_add_exit;
354 }
355
356 for_each_possible_cpu(cpu) {
357 ret = msm_pm_mode_sysfs_add_cpu(cpu, modes_kobj);
358 if (ret)
359 goto mode_sysfs_add_exit;
360 }
361
362 ret = 0;
363
364mode_sysfs_add_exit:
365 return ret;
366}
367
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700368/*
369 * Configure hardware registers in preparation for Apps power down.
370 */
371static void msm_pm_config_hw_before_power_down(void)
372{
373 return;
374}
375
376/*
377 * Clear hardware registers after Apps powers up.
378 */
379static void msm_pm_config_hw_after_power_up(void)
380{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700381}
382
383/*
384 * Configure hardware registers in preparation for SWFI.
385 */
386static void msm_pm_config_hw_before_swfi(void)
387{
388 return;
389}
390
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600391/*
392 * Configure/Restore hardware registers in preparation for Retention.
393 */
394
395static void msm_pm_config_hw_after_retention(void)
396{
397 int ret;
Abhijeet Dharmapurikare8f83852013-02-06 18:57:17 -0800398
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600399 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
400 WARN_ON(ret);
401}
402
403static void msm_pm_config_hw_before_retention(void)
404{
405 return;
406}
407
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700408/*
409 * Convert time from nanoseconds to slow clock ticks, then cap it to the
410 * specified limit
411 */
412static int64_t msm_pm_convert_and_cap_time(int64_t time_ns, int64_t limit)
413{
414 do_div(time_ns, NSEC_PER_SEC / SCLK_HZ);
415 return (time_ns > limit) ? limit : time_ns;
416}
417
418/*
419 * Set the sleep time for suspend. 0 means infinite sleep time.
420 */
421void msm_pm_set_max_sleep_time(int64_t max_sleep_time_ns)
422{
423 if (max_sleep_time_ns == 0) {
424 msm_pm_max_sleep_time = 0;
425 } else {
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700426 msm_pm_max_sleep_time =
427 (uint32_t)msm_pm_convert_and_cap_time(
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700428 max_sleep_time_ns, MSM_PM_SLEEP_TICK_LIMIT);
429
430 if (msm_pm_max_sleep_time == 0)
431 msm_pm_max_sleep_time = 1;
432 }
433
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700434 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700435 pr_info("%s: Requested %lld ns Giving %u sclk ticks\n",
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700436 __func__, max_sleep_time_ns,
437 msm_pm_max_sleep_time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700438}
439EXPORT_SYMBOL(msm_pm_set_max_sleep_time);
440
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600441static void msm_pm_save_cpu_reg(void)
442{
443 int i;
444
445 /* Only on core0 */
446 if (smp_processor_id())
447 return;
448
449 /**
450 * On some targets, L2 PC will turn off may reset the core
451 * configuration for the mux and the default may not make the core
452 * happy when it resumes.
453 * Save the active vdd, and set the core vdd to QSB max vdd, so that
454 * when the core resumes, it is capable of supporting the current QSB
455 * rate. Then restore the active vdd before switching the acpuclk rate.
456 */
457 if (msm_pm_get_l2_flush_flag() == 1) {
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700458 cp15_data.active_vdd = msm_spm_get_vdd(0);
459 for (i = 0; i < cp15_data.reg_saved_state_size; i++)
460 cp15_data.reg_val[i] =
461 get_l2_indirect_reg(
462 cp15_data.reg_data[i]);
463 msm_spm_set_vdd(0, cp15_data.qsb_pc_vdd);
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600464 }
465}
466
467static void msm_pm_restore_cpu_reg(void)
468{
469 int i;
470
471 /* Only on core0 */
472 if (smp_processor_id())
473 return;
474
475 if (msm_pm_get_l2_flush_flag() == 1) {
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700476 for (i = 0; i < cp15_data.reg_saved_state_size; i++)
477 set_l2_indirect_reg(
478 cp15_data.reg_data[i],
479 cp15_data.reg_val[i]);
480 msm_spm_set_vdd(0, cp15_data.active_vdd);
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600481 }
482}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700483
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700484static void msm_pm_swfi(void)
485{
486 msm_pm_config_hw_before_swfi();
487 msm_arch_idle();
488}
489
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600490static void msm_pm_retention(void)
491{
492 int ret = 0;
493
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600494 msm_pm_config_hw_before_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600495 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_POWER_RETENTION, false);
496 WARN_ON(ret);
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600497
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700498 if (msm_pm_retention_calls_tz)
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600499 scm_call_atomic1(SCM_SVC_BOOT, SCM_CMD_TERMINATE_PC,
500 SCM_L2_RETENTION);
501 else
502 msm_arch_idle();
503
504 msm_pm_config_hw_after_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600505}
506
Stephen Boydb29750d2012-02-21 01:21:32 -0800507static bool __ref msm_pm_spm_power_collapse(
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700508 unsigned int cpu, bool from_idle, bool notify_rpm)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700509{
510 void *entry;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600511 bool collapsed = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700512 int ret;
513
514 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
515 pr_info("CPU%u: %s: notify_rpm %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700516 cpu, __func__, (int) notify_rpm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700517
518 ret = msm_spm_set_low_power_mode(
519 MSM_SPM_MODE_POWER_COLLAPSE, notify_rpm);
520 WARN_ON(ret);
521
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700522 entry = (!cpu || from_idle) ?
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700523 msm_pm_collapse_exit : msm_secondary_startup;
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700524 msm_pm_boot_config_before_pc(cpu, virt_to_phys(entry));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700525
526 if (MSM_PM_DEBUG_RESET_VECTOR & msm_pm_debug_mask)
527 pr_info("CPU%u: %s: program vector to %p\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700528 cpu, __func__, entry);
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -0600529 if (from_idle && msm_pm_pc_reset_timer)
530 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700531
Girish Mahadevan55944992012-10-26 11:03:07 -0600532 collapsed = msm_pm_collapse();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700533
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -0600534 if (from_idle && msm_pm_pc_reset_timer)
535 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu);
536
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700537 msm_pm_boot_config_after_pc(cpu);
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -0600538
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700539 if (collapsed) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700540 cpu_init();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700541 local_fiq_enable();
542 }
543
544 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
545 pr_info("CPU%u: %s: msm_pm_collapse returned, collapsed %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700546 cpu, __func__, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700547
548 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
549 WARN_ON(ret);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600550 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700551}
552
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600553static bool msm_pm_power_collapse_standalone(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700554{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700555 unsigned int cpu = smp_processor_id();
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700556 unsigned int avsdscr;
557 unsigned int avscsr;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600558 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700559
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700560 avsdscr = avs_get_avsdscr();
561 avscsr = avs_get_avscsr();
562 avs_set_avscsr(0); /* Disable AVS */
563
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700564 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, false);
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700565
566 avs_set_avsdscr(avsdscr);
567 avs_set_avscsr(avscsr);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600568 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700569}
570
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600571static bool msm_pm_power_collapse(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700572{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700573 unsigned int cpu = smp_processor_id();
Praveen Chidambaram4b8df032013-01-18 16:21:16 -0700574 unsigned long saved_acpuclk_rate = 0;
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700575 unsigned int avsdscr;
576 unsigned int avscsr;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600577 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700578
579 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
580 pr_info("CPU%u: %s: idle %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700581 cpu, __func__, (int)from_idle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700582
583 msm_pm_config_hw_before_power_down();
584 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700585 pr_info("CPU%u: %s: pre power down\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700586
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700587 avsdscr = avs_get_avsdscr();
588 avscsr = avs_get_avscsr();
589 avs_set_avscsr(0); /* Disable AVS */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700590
Praveen Chidambaram4b8df032013-01-18 16:21:16 -0700591 if (cpu_online(cpu) && !msm_no_ramp_down_pc)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700592 saved_acpuclk_rate = acpuclk_power_collapse();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700593
594 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
595 pr_info("CPU%u: %s: change clock rate (old rate = %lu)\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700596 cpu, __func__, saved_acpuclk_rate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700597
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700598 if (cp15_data.save_cp15)
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600599 msm_pm_save_cpu_reg();
600
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700601 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700602
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700603 if (cp15_data.save_cp15)
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600604 msm_pm_restore_cpu_reg();
605
Girish Mahadevan4e025d92012-02-29 13:26:51 -0700606 if (cpu_online(cpu)) {
607 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
608 pr_info("CPU%u: %s: restore clock rate to %lu\n",
609 cpu, __func__, saved_acpuclk_rate);
Praveen Chidambaram4b8df032013-01-18 16:21:16 -0700610 if (!msm_no_ramp_down_pc &&
611 acpuclk_set_rate(cpu, saved_acpuclk_rate, SETRATE_PC)
612 < 0)
Girish Mahadevan4e025d92012-02-29 13:26:51 -0700613 pr_err("CPU%u: %s: failed to restore clock rate(%lu)\n",
614 cpu, __func__, saved_acpuclk_rate);
615 } else {
616 unsigned int gic_dist_enabled;
617 unsigned int gic_dist_pending;
618 gic_dist_enabled = readl_relaxed(
619 MSM_QGIC_DIST_BASE + GIC_DIST_ENABLE_CLEAR);
620 gic_dist_pending = readl_relaxed(
621 MSM_QGIC_DIST_BASE + GIC_DIST_PENDING_SET);
622 mb();
623 gic_dist_pending &= gic_dist_enabled;
624
625 if (gic_dist_pending)
626 pr_err("CPU %d interrupted during hotplug.Pending int 0x%x\n",
627 cpu, gic_dist_pending);
628 }
629
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700630
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700631 avs_set_avsdscr(avsdscr);
632 avs_set_avscsr(avscsr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700633 msm_pm_config_hw_after_power_up();
634 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700635 pr_info("CPU%u: %s: post power up\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700636
637 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700638 pr_info("CPU%u: %s: return\n", cpu, __func__);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600639 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700640}
641
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600642static int64_t msm_pm_timer_enter_idle(void)
643{
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700644 if (msm_pm_use_sync_timer)
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600645 return ktime_to_ns(tick_nohz_get_sleep_length());
646
647 return msm_timer_enter_idle();
648}
649
650static void msm_pm_timer_exit_idle(bool timer_halted)
651{
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700652 if (msm_pm_use_sync_timer)
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600653 return;
654
655 msm_timer_exit_idle((int) timer_halted);
656}
657
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530658static int64_t msm_pm_timer_enter_suspend(int64_t *period)
659{
Anji Jonnalac02367a2012-07-01 02:56:11 +0530660 int64_t time = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530661
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700662 if (msm_pm_use_sync_timer)
Mahesh Sivasubramanian0eaeb302013-03-14 14:56:32 -0600663 return sched_clock();
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530664
665 time = msm_timer_get_sclk_time(period);
666 if (!time)
667 pr_err("%s: Unable to read sclk.\n", __func__);
668
669 return time;
670}
671
672static int64_t msm_pm_timer_exit_suspend(int64_t time, int64_t period)
673{
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700674 if (msm_pm_use_sync_timer)
Mahesh Sivasubramanian0eaeb302013-03-14 14:56:32 -0600675 return sched_clock() - time;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530676
677 if (time != 0) {
678 int64_t end_time = msm_timer_get_sclk_time(NULL);
679 if (end_time != 0) {
680 time = end_time - time;
681 if (time < 0)
682 time += period;
683 } else
684 time = 0;
685 }
686
687 return time;
688}
689
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600690/**
691 * pm_hrtimer_cb() : Callback function for hrtimer created if the
692 * core needs to be awake to handle an event.
693 * @hrtimer : Pointer to hrtimer
694 */
695static enum hrtimer_restart pm_hrtimer_cb(struct hrtimer *hrtimer)
696{
697 return HRTIMER_NORESTART;
698}
699
700/**
701 * msm_pm_set_timer() : Set an hrtimer to wakeup the core in time
702 * to handle an event.
703 */
704static void msm_pm_set_timer(uint32_t modified_time_us)
705{
706 u64 modified_time_ns = modified_time_us * NSEC_PER_USEC;
707 ktime_t modified_ktime = ns_to_ktime(modified_time_ns);
708 pm_hrtimer.function = pm_hrtimer_cb;
709 hrtimer_start(&pm_hrtimer, modified_ktime, HRTIMER_MODE_ABS);
710}
711
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700712/******************************************************************************
713 * External Idle/Suspend Functions
714 *****************************************************************************/
715
716void arch_idle(void)
717{
718 return;
719}
720
Priyanka Mathur26b4a4b2012-11-05 13:45:45 -0800721static inline void msm_pm_ftrace_lpm_enter(unsigned int cpu,
722 uint32_t latency, uint32_t sleep_us,
723 uint32_t wake_up,
724 enum msm_pm_sleep_mode mode)
725{
726 switch (mode) {
727 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
728 trace_msm_pm_enter_wfi(cpu, latency, sleep_us, wake_up);
729 break;
730 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
731 trace_msm_pm_enter_spc(cpu, latency, sleep_us, wake_up);
732 break;
733 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
734 trace_msm_pm_enter_pc(cpu, latency, sleep_us, wake_up);
735 break;
736 case MSM_PM_SLEEP_MODE_RETENTION:
737 trace_msm_pm_enter_ret(cpu, latency, sleep_us, wake_up);
738 break;
739 default:
740 break;
741 }
742}
743
744static inline void msm_pm_ftrace_lpm_exit(unsigned int cpu,
745 enum msm_pm_sleep_mode mode,
746 bool success)
747{
748 switch (mode) {
749 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
750 trace_msm_pm_exit_wfi(cpu, success);
751 break;
752 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
753 trace_msm_pm_exit_spc(cpu, success);
754 break;
755 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
756 trace_msm_pm_exit_pc(cpu, success);
757 break;
758 case MSM_PM_SLEEP_MODE_RETENTION:
759 trace_msm_pm_exit_ret(cpu, success);
760 break;
761 default:
762 break;
763 }
764}
765
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800766static int msm_pm_idle_prepare(struct cpuidle_device *dev,
767 struct cpuidle_driver *drv, int index,
768 void **msm_pm_idle_rs_limits)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700769{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700770 int i;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700771 unsigned int power_usage = -1;
Priyanka Mathur848bb4c2012-11-30 18:04:57 -0800772 int ret = MSM_PM_SLEEP_MODE_NOT_SELECTED;
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600773 uint32_t modified_time_us = 0;
774 struct msm_pm_time_params time_param;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700775
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600776 time_param.latency_us =
777 (uint32_t) pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
778 time_param.sleep_us =
779 (uint32_t) (ktime_to_us(tick_nohz_get_sleep_length())
780 & UINT_MAX);
781 time_param.modified_time_us = 0;
782
783 if (!dev->cpu)
784 time_param.next_event_us =
785 (uint32_t) (ktime_to_us(get_next_event_time())
786 & UINT_MAX);
787 else
788 time_param.next_event_us = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700789
790 for (i = 0; i < dev->state_count; i++) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700791 struct cpuidle_state *state = &drv->states[i];
792 struct cpuidle_state_usage *st_usage = &dev->states_usage[i];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700793 enum msm_pm_sleep_mode mode;
794 bool allow;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600795 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700796 int idx;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800797 void *rs_limits = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700798
Steve Mucklef132c6c2012-06-06 18:30:57 -0700799 mode = (enum msm_pm_sleep_mode) cpuidle_get_statedata(st_usage);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700800 idx = MSM_PM_MODE(dev->cpu, mode);
801
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600802 allow = msm_pm_sleep_modes[idx].idle_enabled &&
803 msm_pm_sleep_modes[idx].idle_supported;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700804
805 switch (mode) {
806 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700807 if (num_online_cpus() > 1)
Priyanka Mathur68a9bc52012-10-03 15:15:01 -0700808 allow = false;
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700809 break;
Mahesh Sivasubramanianba7e01c2012-09-04 18:22:51 -0600810 case MSM_PM_SLEEP_MODE_RETENTION:
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -0700811 /*
812 * The Krait BHS regulator doesn't have enough head
813 * room to drive the retention voltage on LDO and so
814 * has disabled retention
815 */
816 if (!msm_pm_ldo_retention_enabled)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700817 allow = false;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700818
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700819 if (msm_pm_retention_calls_tz && num_online_cpus() > 1)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700820 allow = false;
821 break;
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700822 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
823 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
824 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700825 default:
826 allow = false;
827 break;
828 }
829
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700830 if (!allow)
831 continue;
832
833 if (pm_sleep_ops.lowest_limits)
834 rs_limits = pm_sleep_ops.lowest_limits(true,
835 mode, &time_param, &power);
836
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700837 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700838 pr_info("CPU%u:%s:%s, latency %uus, slp %uus, lim %p\n",
839 dev->cpu, __func__, state->desc,
840 time_param.latency_us,
841 time_param.sleep_us, rs_limits);
842 if (!rs_limits)
843 continue;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700844
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700845 if (power < power_usage) {
846 power_usage = power;
847 modified_time_us = time_param.modified_time_us;
848 ret = mode;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800849 *msm_pm_idle_rs_limits = rs_limits;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700850 }
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700851
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700852 }
853
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600854 if (modified_time_us && !dev->cpu)
855 msm_pm_set_timer(modified_time_us);
Priyanka Mathur26b4a4b2012-11-05 13:45:45 -0800856
857 msm_pm_ftrace_lpm_enter(dev->cpu, time_param.latency_us,
858 time_param.sleep_us, time_param.next_event_us,
859 ret);
860
Steve Mucklef132c6c2012-06-06 18:30:57 -0700861 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700862}
863
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800864enum msm_pm_sleep_mode msm_pm_idle_enter(struct cpuidle_device *dev,
865 struct cpuidle_driver *drv, int index)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700866{
867 int64_t time;
Priyanka Mathur26b4a4b2012-11-05 13:45:45 -0800868 bool collapsed = 1;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800869 int exit_stat = -1;
870 enum msm_pm_sleep_mode sleep_mode;
871 void *msm_pm_idle_rs_limits = NULL;
872 int sleep_delay = 1;
873 int ret = -ENODEV;
874 int64_t timer_expiration = 0;
875 int notify_rpm = false;
876 bool timer_halted = false;
877
878 sleep_mode = msm_pm_idle_prepare(dev, drv, index,
879 &msm_pm_idle_rs_limits);
880
881 if (!msm_pm_idle_rs_limits) {
882 sleep_mode = MSM_PM_SLEEP_MODE_NOT_SELECTED;
883 goto cpuidle_enter_bail;
884 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700885
886 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
887 pr_info("CPU%u: %s: mode %d\n",
888 smp_processor_id(), __func__, sleep_mode);
889
890 time = ktime_to_ns(ktime_get());
891
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800892 if (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE) {
893 notify_rpm = true;
894 timer_expiration = msm_pm_timer_enter_idle();
895
896 sleep_delay = (uint32_t) msm_pm_convert_and_cap_time(
897 timer_expiration, MSM_PM_SLEEP_TICK_LIMIT);
898 if (sleep_delay == 0) /* 0 would mean infinite time */
899 sleep_delay = 1;
900 }
901
902 if (pm_sleep_ops.enter_sleep)
903 ret = pm_sleep_ops.enter_sleep(sleep_delay,
904 msm_pm_idle_rs_limits, true, notify_rpm);
905 if (ret)
906 goto cpuidle_enter_bail;
907
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700908 switch (sleep_mode) {
909 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
910 msm_pm_swfi();
911 exit_stat = MSM_PM_STAT_IDLE_WFI;
912 break;
913
914 case MSM_PM_SLEEP_MODE_RETENTION:
915 msm_pm_retention();
916 exit_stat = MSM_PM_STAT_RETENTION;
917 break;
918
919 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
920 collapsed = msm_pm_power_collapse_standalone(true);
921 exit_stat = MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE;
922 break;
923
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800924 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700925 if (MSM_PM_DEBUG_IDLE_CLK & msm_pm_debug_mask)
926 clock_debug_print_enabled();
Karthik Parsha6fb932d2012-01-24 18:04:12 -0800927
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800928 collapsed = msm_pm_power_collapse(true);
929 timer_halted = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700930
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700931 exit_stat = MSM_PM_STAT_IDLE_POWER_COLLAPSE;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800932 msm_pm_timer_exit_idle(timer_halted);
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700933 break;
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700934
935 case MSM_PM_SLEEP_MODE_NOT_SELECTED:
936 goto cpuidle_enter_bail;
937 break;
938
939 default:
940 __WARN();
941 goto cpuidle_enter_bail;
942 break;
943 }
944
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800945 if (pm_sleep_ops.exit_sleep)
946 pm_sleep_ops.exit_sleep(msm_pm_idle_rs_limits, true,
947 notify_rpm, collapsed);
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700948
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800949 time = ktime_to_ns(ktime_get()) - time;
950 msm_pm_ftrace_lpm_exit(smp_processor_id(), sleep_mode, collapsed);
951 if (exit_stat >= 0)
952 msm_pm_add_stat(exit_stat, time);
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700953 do_div(time, 1000);
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800954 dev->last_residency = (int) time;
955 return sleep_mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700956
957cpuidle_enter_bail:
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800958 dev->last_residency = 0;
959 if (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE)
960 msm_pm_timer_exit_idle(timer_halted);
961 sleep_mode = MSM_PM_SLEEP_MODE_NOT_SELECTED;
962 return sleep_mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700963}
964
Anji Jonnala02dac8d2013-03-06 21:31:04 +0530965int msm_pm_wait_cpu_shutdown(unsigned int cpu)
966{
967 int timeout = 10;
968
969 if (!msm_pm_slp_sts)
970 return 0;
971 if (!msm_pm_slp_sts[cpu].base_addr)
972 return 0;
973 while (timeout--) {
974 /*
975 * Check for the SPM of the core being hotplugged to set
976 * its sleep state.The SPM sleep state indicates that the
977 * core has been power collapsed.
978 */
979 int acc_sts = __raw_readl(msm_pm_slp_sts[cpu].base_addr);
980
981 if (acc_sts & msm_pm_slp_sts[cpu].mask)
982 return 0;
Mahesh Sivasubramanian65187f92013-03-19 15:14:06 -0600983 udelay(100);
Anji Jonnala02dac8d2013-03-06 21:31:04 +0530984 }
985
986 pr_info("%s(): Timed out waiting for CPU %u SPM to enter sleep state",
987 __func__, cpu);
988 return -EBUSY;
989}
990
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700991void msm_pm_cpu_enter_lowpower(unsigned int cpu)
992{
993 int i;
994 bool allow[MSM_PM_SLEEP_MODE_NR];
995
996 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
997 struct msm_pm_platform_data *mode;
998
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600999 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -07001000 allow[i] = mode->suspend_supported && mode->suspend_enabled;
1001 }
1002
1003 if (MSM_PM_DEBUG_HOTPLUG & msm_pm_debug_mask)
1004 pr_notice("CPU%u: %s: shutting down cpu\n", cpu, __func__);
1005
Matt Wagantall5375aa72012-07-02 19:59:51 -07001006 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -07001007 msm_pm_power_collapse(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -07001008 else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -07001009 msm_pm_power_collapse_standalone(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -07001010 else if (allow[MSM_PM_SLEEP_MODE_RETENTION])
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -06001011 msm_pm_retention();
Stephen Boydbda74272012-08-09 14:01:27 -07001012 else
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -07001013 msm_pm_swfi();
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -07001014}
1015
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -07001016static void msm_pm_ack_retention_disable(void *data)
1017{
1018 /*
1019 * This is a NULL function to ensure that the core has woken up
1020 * and is safe to disable retention.
1021 */
1022}
1023/**
1024 * msm_pm_enable_retention() - Disable/Enable retention on all cores
1025 * @enable: Enable/Disable retention
1026 *
1027 */
1028void msm_pm_enable_retention(bool enable)
1029{
Matt Wagantalld06f5c22013-03-14 19:31:14 -07001030 if (enable == msm_pm_ldo_retention_enabled)
1031 return;
1032
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -07001033 msm_pm_ldo_retention_enabled = enable;
1034 /*
1035 * If retention is being disabled, wakeup all online core to ensure
1036 * that it isn't executing retention. Offlined cores need not be woken
1037 * up as they enter the deepest sleep mode, namely RPM assited power
1038 * collapse
1039 */
Mahesh Sivasubramanian96f67d12013-03-14 22:44:42 -06001040 if (!enable) {
1041 preempt_disable();
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -07001042 smp_call_function_many(cpu_online_mask,
1043 msm_pm_ack_retention_disable,
1044 NULL, true);
Mahesh Sivasubramanian96f67d12013-03-14 22:44:42 -06001045 preempt_enable();
1046
1047
1048 }
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -07001049}
1050EXPORT_SYMBOL(msm_pm_enable_retention);
1051
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001052static int msm_pm_enter(suspend_state_t state)
1053{
1054 bool allow[MSM_PM_SLEEP_MODE_NR];
1055 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001056 int64_t period = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301057 int64_t time = msm_pm_timer_enter_suspend(&period);
Girish Mahadevandc318fd2012-08-17 16:48:05 -06001058 struct msm_pm_time_params time_param;
1059
1060 time_param.latency_us = -1;
1061 time_param.sleep_us = -1;
1062 time_param.next_event_us = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001063
1064 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1065 pr_info("%s\n", __func__);
1066
1067 if (smp_processor_id()) {
1068 __WARN();
1069 goto enter_exit;
1070 }
1071
1072
1073 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
1074 struct msm_pm_platform_data *mode;
1075
Praveen Chidambaram42da9d22012-03-30 12:16:34 -06001076 mode = &msm_pm_sleep_modes[MSM_PM_MODE(0, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001077 allow[i] = mode->suspend_supported && mode->suspend_enabled;
1078 }
1079
1080 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001081 void *rs_limits = NULL;
1082 int ret = -ENODEV;
1083 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001084
1085 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1086 pr_info("%s: power collapse\n", __func__);
1087
1088 clock_debug_print_enabled();
1089
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001090 if (msm_pm_sleep_time_override > 0) {
1091 int64_t ns = NSEC_PER_SEC *
1092 (int64_t) msm_pm_sleep_time_override;
1093 msm_pm_set_max_sleep_time(ns);
1094 msm_pm_sleep_time_override = 0;
1095 }
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001096
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001097 if (pm_sleep_ops.lowest_limits)
1098 rs_limits = pm_sleep_ops.lowest_limits(false,
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001099 MSM_PM_SLEEP_MODE_POWER_COLLAPSE, &time_param, &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001100
1101 if (rs_limits) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001102 if (pm_sleep_ops.enter_sleep)
1103 ret = pm_sleep_ops.enter_sleep(
1104 msm_pm_max_sleep_time,
1105 rs_limits, false, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001106 if (!ret) {
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -06001107 int collapsed = msm_pm_power_collapse(false);
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001108 if (pm_sleep_ops.exit_sleep) {
1109 pm_sleep_ops.exit_sleep(rs_limits,
1110 false, true, collapsed);
1111 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001112 }
1113 } else {
1114 pr_err("%s: cannot find the lowest power limit\n",
1115 __func__);
1116 }
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301117 time = msm_pm_timer_exit_suspend(time, period);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001118 msm_pm_add_stat(MSM_PM_STAT_SUSPEND, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001119 } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
1120 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1121 pr_info("%s: standalone power collapse\n", __func__);
1122 msm_pm_power_collapse_standalone(false);
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -06001123 } else if (allow[MSM_PM_SLEEP_MODE_RETENTION]) {
1124 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1125 pr_info("%s: retention\n", __func__);
1126 msm_pm_retention();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001127 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
1128 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1129 pr_info("%s: swfi\n", __func__);
1130 msm_pm_swfi();
1131 }
1132
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001133enter_exit:
1134 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1135 pr_info("%s: return\n", __func__);
1136
1137 return 0;
1138}
1139
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001140void msm_pm_set_sleep_ops(struct msm_pm_sleep_ops *ops)
1141{
1142 if (ops)
1143 pm_sleep_ops = *ops;
1144}
1145
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001146static const struct platform_suspend_ops msm_pm_ops = {
1147 .enter = msm_pm_enter,
1148 .valid = suspend_valid_only_mem,
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -06001149};
Anji Jonnala02dac8d2013-03-06 21:31:04 +05301150static int __devinit msm_cpu_status_probe(struct platform_device *pdev)
1151{
1152 struct msm_pm_sleep_status_data *pdata;
1153 char *key;
1154 u32 cpu;
1155
1156 if (!pdev)
1157 return -EFAULT;
1158
1159 msm_pm_slp_sts =
1160 kzalloc(sizeof(*msm_pm_slp_sts) * num_possible_cpus(),
1161 GFP_KERNEL);
1162
1163 if (!msm_pm_slp_sts)
1164 return -ENOMEM;
1165
1166 if (pdev->dev.of_node) {
1167 struct resource *res;
1168 u32 offset;
1169 int rc;
1170 u32 mask;
1171
1172 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1173 if (!res)
1174 goto fail_free_mem;
1175
1176 key = "qcom,cpu-alias-addr";
1177 rc = of_property_read_u32(pdev->dev.of_node, key, &offset);
1178
1179 if (rc)
1180 goto fail_free_mem;
1181
1182 key = "qcom,sleep-status-mask";
1183 rc = of_property_read_u32(pdev->dev.of_node, key,
1184 &mask);
1185 if (rc)
1186 goto fail_free_mem;
1187
1188 for_each_possible_cpu(cpu) {
1189 msm_pm_slp_sts[cpu].base_addr =
1190 ioremap(res->start + cpu * offset,
1191 resource_size(res));
1192 msm_pm_slp_sts[cpu].mask = mask;
1193
1194 if (!msm_pm_slp_sts[cpu].base_addr)
1195 goto failed_of_node;
1196 }
1197
1198 } else {
1199 pdata = pdev->dev.platform_data;
1200 if (!pdev->dev.platform_data)
1201 goto fail_free_mem;
1202
1203 for_each_possible_cpu(cpu) {
1204 msm_pm_slp_sts[cpu].base_addr =
1205 pdata->base_addr + cpu * pdata->cpu_offset;
1206 msm_pm_slp_sts[cpu].mask = pdata->mask;
1207 }
1208 }
1209
1210 return 0;
1211
1212failed_of_node:
1213 pr_info("%s(): Failed to key=%s\n", __func__, key);
1214 for_each_possible_cpu(cpu) {
1215 if (msm_pm_slp_sts[cpu].base_addr)
1216 iounmap(msm_pm_slp_sts[cpu].base_addr);
1217 }
1218fail_free_mem:
1219 kfree(msm_pm_slp_sts);
1220 return -EINVAL;
1221
1222};
1223
1224static struct of_device_id msm_slp_sts_match_tbl[] = {
1225 {.compatible = "qcom,cpu-sleep-status"},
1226 {},
1227};
1228
1229static struct platform_driver msm_cpu_status_driver = {
1230 .probe = msm_cpu_status_probe,
1231 .driver = {
1232 .name = "cpu_slp_status",
1233 .owner = THIS_MODULE,
1234 .of_match_table = msm_slp_sts_match_tbl,
1235 },
1236};
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -06001237
Praveen Chidambaramede45d02013-02-20 17:45:21 -07001238static int __init msm_pm_setup_saved_state(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001239{
1240 pgd_t *pc_pgd;
1241 pmd_t *pmd;
1242 unsigned long pmdval;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001243 unsigned long exit_phys;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301244
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001245 /* Page table for cores to come back up safely. */
1246 pc_pgd = pgd_alloc(&init_mm);
1247 if (!pc_pgd)
1248 return -ENOMEM;
1249
Steve Mucklef132c6c2012-06-06 18:30:57 -07001250 exit_phys = virt_to_phys(msm_pm_collapse_exit);
1251
1252 pmd = pmd_offset(pud_offset(pc_pgd + pgd_index(exit_phys),exit_phys),
1253 exit_phys);
1254 pmdval = (exit_phys & PGDIR_MASK) |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001255 PMD_TYPE_SECT | PMD_SECT_AP_WRITE;
1256 pmd[0] = __pmd(pmdval);
1257 pmd[1] = __pmd(pmdval + (1 << (PGDIR_SHIFT - 1)));
1258
Steve Mucklefcece052012-02-18 20:09:58 -08001259 msm_saved_state_phys =
1260 allocate_contiguous_ebi_nomap(CPU_SAVED_STATE_SIZE *
1261 num_possible_cpus(), 4);
1262 if (!msm_saved_state_phys)
1263 return -ENOMEM;
1264 msm_saved_state = ioremap_nocache(msm_saved_state_phys,
1265 CPU_SAVED_STATE_SIZE *
1266 num_possible_cpus());
1267 if (!msm_saved_state)
1268 return -ENOMEM;
1269
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001270 /* It is remotely possible that the code in msm_pm_collapse_exit()
1271 * which turns on the MMU with this mapping is in the
1272 * next even-numbered megabyte beyond the
1273 * start of msm_pm_collapse_exit().
1274 * Map this megabyte in as well.
1275 */
1276 pmd[2] = __pmd(pmdval + (2 << (PGDIR_SHIFT - 1)));
1277 flush_pmd_entry(pmd);
1278 msm_pm_pc_pgd = virt_to_phys(pc_pgd);
Steve Muckle730ad7a2012-02-21 15:26:37 -08001279 clean_caches((unsigned long)&msm_pm_pc_pgd, sizeof(msm_pm_pc_pgd),
1280 virt_to_phys(&msm_pm_pc_pgd));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001281
Praveen Chidambaramede45d02013-02-20 17:45:21 -07001282 return 0;
1283}
1284core_initcall(msm_pm_setup_saved_state);
1285
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001286static void setup_broadcast_timer(void *arg)
1287{
1288 unsigned long reason = (unsigned long)arg;
1289 int cpu = smp_processor_id();
1290
1291 reason = reason ?
1292 CLOCK_EVT_NOTIFY_BROADCAST_ON : CLOCK_EVT_NOTIFY_BROADCAST_OFF;
1293
1294 clockevents_notify(reason, &cpu);
1295}
1296
1297static int setup_broadcast_cpuhp_notify(struct notifier_block *n,
1298 unsigned long action, void *hcpu)
1299{
1300 int hotcpu = (unsigned long)hcpu;
1301
1302 switch (action & ~CPU_TASKS_FROZEN) {
1303 case CPU_ONLINE:
1304 smp_call_function_single(hotcpu, setup_broadcast_timer,
1305 (void *)true, 1);
1306 break;
1307 }
1308
1309 return NOTIFY_OK;
1310}
1311
1312static struct notifier_block setup_broadcast_notifier = {
1313 .notifier_call = setup_broadcast_cpuhp_notify,
1314};
1315
Praveen Chidambaramede45d02013-02-20 17:45:21 -07001316static int __init msm_pm_init(void)
1317{
1318 enum msm_pm_time_stats_id enable_stats[] = {
1319 MSM_PM_STAT_IDLE_WFI,
1320 MSM_PM_STAT_RETENTION,
1321 MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE,
1322 MSM_PM_STAT_IDLE_POWER_COLLAPSE,
1323 MSM_PM_STAT_SUSPEND,
1324 };
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001325 msm_pm_mode_sysfs_add();
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301326 msm_pm_add_stats(enable_stats, ARRAY_SIZE(enable_stats));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001327 suspend_set_ops(&msm_pm_ops);
Girish Mahadevandc318fd2012-08-17 16:48:05 -06001328 hrtimer_init(&pm_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001329 msm_cpuidle_init();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001330
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001331 if (msm_pm_pc_reset_timer) {
1332 get_cpu();
1333 smp_call_function_many(cpu_online_mask, setup_broadcast_timer,
1334 (void *)true, 1);
1335 put_cpu();
1336 register_cpu_notifier(&setup_broadcast_notifier);
1337 }
1338
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001339 return 0;
1340}
1341
Girish Mahadevan55944992012-10-26 11:03:07 -06001342static void __devinit msm_pm_set_flush_fn(uint32_t pc_mode)
1343{
1344 msm_pm_disable_l2_fn = NULL;
1345 msm_pm_enable_l2_fn = NULL;
1346 msm_pm_flush_l2_fn = outer_flush_all;
1347
1348 if (pc_mode == MSM_PM_PC_NOTZ_L2_EXT) {
1349 msm_pm_disable_l2_fn = outer_disable;
1350 msm_pm_enable_l2_fn = outer_resume;
1351 }
1352}
1353
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001354struct msm_pc_debug_counters_buffer {
1355 void __iomem *reg;
1356 u32 len;
1357 char buf[MAX_BUF_SIZE];
1358};
1359
1360static inline u32 msm_pc_debug_counters_read_register(
1361 void __iomem *reg, int index , int offset)
1362{
1363 return readl_relaxed(reg + (index * 4 + offset) * 4);
1364}
1365
1366static char *counter_name[] = {
1367 "PC Entry Counter",
1368 "Warmboot Entry Counter",
1369 "PC Bailout Counter"
1370};
1371
1372static int msm_pc_debug_counters_copy(
1373 struct msm_pc_debug_counters_buffer *data)
1374{
1375 int j;
1376 u32 stat;
1377 unsigned int cpu;
1378
1379 for_each_possible_cpu(cpu) {
1380 data->len += scnprintf(data->buf + data->len,
1381 sizeof(data->buf)-data->len,
1382 "CPU%d\n", cpu);
1383
1384 for (j = 0; j < NUM_OF_COUNTERS; j++) {
1385 stat = msm_pc_debug_counters_read_register(
1386 data->reg, cpu, j);
1387 data->len += scnprintf(data->buf + data->len,
1388 sizeof(data->buf)-data->len,
1389 "\t%s : %d\n", counter_name[j],
1390 stat);
1391 }
1392
1393 }
1394
1395 return data->len;
1396}
1397
1398static int msm_pc_debug_counters_file_read(struct file *file,
1399 char __user *bufu, size_t count, loff_t *ppos)
1400{
1401 struct msm_pc_debug_counters_buffer *data;
1402
1403 data = file->private_data;
1404
1405 if (!data)
1406 return -EINVAL;
1407
1408 if (!bufu || count < 0)
1409 return -EINVAL;
1410
1411 if (!access_ok(VERIFY_WRITE, bufu, count))
1412 return -EFAULT;
1413
1414 if (*ppos >= data->len && data->len == 0)
1415 data->len = msm_pc_debug_counters_copy(data);
1416
1417 return simple_read_from_buffer(bufu, count, ppos,
1418 data->buf, data->len);
1419}
1420
1421static int msm_pc_debug_counters_file_open(struct inode *inode,
1422 struct file *file)
1423{
1424 struct msm_pc_debug_counters_buffer *buf;
1425 void __iomem *msm_pc_debug_counters_reg;
1426
1427 msm_pc_debug_counters_reg = inode->i_private;
1428
1429 if (!msm_pc_debug_counters_reg)
1430 return -EINVAL;
1431
1432 file->private_data = kzalloc(
1433 sizeof(struct msm_pc_debug_counters_buffer), GFP_KERNEL);
1434
1435 if (!file->private_data) {
1436 pr_err("%s: ERROR kmalloc failed to allocate %d bytes\n",
1437 __func__, sizeof(struct msm_pc_debug_counters_buffer));
1438
1439 return -ENOMEM;
1440 }
1441
1442 buf = file->private_data;
1443 buf->reg = msm_pc_debug_counters_reg;
1444
1445 return 0;
1446}
1447
1448static int msm_pc_debug_counters_file_close(struct inode *inode,
1449 struct file *file)
1450{
1451 kfree(file->private_data);
1452 return 0;
1453}
1454
1455static const struct file_operations msm_pc_debug_counters_fops = {
1456 .open = msm_pc_debug_counters_file_open,
1457 .read = msm_pc_debug_counters_file_read,
1458 .release = msm_pc_debug_counters_file_close,
1459 .llseek = no_llseek,
1460};
1461
Girish Mahadevan55944992012-10-26 11:03:07 -06001462static int __devinit msm_pm_8x60_probe(struct platform_device *pdev)
1463{
1464 char *key = NULL;
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001465 struct dentry *dent = NULL;
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001466 struct resource *res = NULL;
1467 int i ;
1468 struct msm_pm_init_data_type pdata_local;
Girish Mahadevan55944992012-10-26 11:03:07 -06001469 int ret = 0;
1470
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001471 memset(&pdata_local, 0, sizeof(struct msm_pm_init_data_type));
1472
1473 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1474 if (res) {
1475 msm_pc_debug_counters_phys = res->start;
1476 WARN_ON(resource_size(res) < SZ_64);
1477 msm_pc_debug_counters = devm_ioremap(&pdev->dev, res->start,
1478 resource_size(res));
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001479 if (msm_pc_debug_counters)
1480 for (i = 0; i < resource_size(res)/4; i++)
1481 __raw_writel(0, msm_pc_debug_counters + i * 4);
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001482
1483 }
1484
1485 if (!msm_pc_debug_counters) {
1486 msm_pc_debug_counters = 0;
1487 msm_pc_debug_counters_phys = 0;
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001488 } else {
1489 dent = debugfs_create_file("pc_debug_counter", S_IRUGO, NULL,
1490 msm_pc_debug_counters,
1491 &msm_pc_debug_counters_fops);
1492 if (!dent)
1493 pr_err("%s: ERROR debugfs_create_file failed\n",
1494 __func__);
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001495 }
1496
Girish Mahadevan55944992012-10-26 11:03:07 -06001497 if (!pdev->dev.of_node) {
1498 struct msm_pm_init_data_type *d = pdev->dev.platform_data;
1499
1500 if (!d)
1501 goto pm_8x60_probe_done;
1502
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001503 memcpy(&pdata_local, d, sizeof(struct msm_pm_init_data_type));
1504
Girish Mahadevan55944992012-10-26 11:03:07 -06001505 } else {
1506 key = "qcom,pc-mode";
Archana Sathyakumar2b91dc82013-02-01 17:38:23 -07001507 ret = msm_pm_get_pc_mode(pdev->dev.of_node,
1508 key,
1509 &pdata_local.pc_mode);
Girish Mahadevan55944992012-10-26 11:03:07 -06001510 if (ret) {
Archana Sathyakumar2b91dc82013-02-01 17:38:23 -07001511 pr_debug("%s: Error reading key %s",
Girish Mahadevan55944992012-10-26 11:03:07 -06001512 __func__, key);
Archana Sathyakumar2b91dc82013-02-01 17:38:23 -07001513 return -EINVAL;
Girish Mahadevan55944992012-10-26 11:03:07 -06001514 }
Girish Mahadevan55944992012-10-26 11:03:07 -06001515
1516 key = "qcom,use-sync-timer";
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001517 pdata_local.use_sync_timer =
Girish Mahadevan55944992012-10-26 11:03:07 -06001518 of_property_read_bool(pdev->dev.of_node, key);
Praveen Chidambaram4b8df032013-01-18 16:21:16 -07001519
1520 key = "qcom,saw-turns-off-pll";
1521 msm_no_ramp_down_pc = of_property_read_bool(pdev->dev.of_node,
1522 key);
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001523
1524 key = "qcom,pc-resets-timer";
1525 msm_pm_pc_reset_timer = of_property_read_bool(
1526 pdev->dev.of_node, key);
Girish Mahadevan55944992012-10-26 11:03:07 -06001527 }
1528
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001529 if (pdata_local.cp15_data.reg_data &&
1530 pdata_local.cp15_data.reg_saved_state_size > 0) {
1531 cp15_data.reg_data = kzalloc(sizeof(uint32_t) *
1532 pdata_local.cp15_data.reg_saved_state_size,
1533 GFP_KERNEL);
1534 if (!cp15_data.reg_data)
1535 return -ENOMEM;
1536
1537 cp15_data.reg_val = kzalloc(sizeof(uint32_t) *
1538 pdata_local.cp15_data.reg_saved_state_size,
1539 GFP_KERNEL);
1540 if (cp15_data.reg_val)
1541 return -ENOMEM;
1542
1543 memcpy(cp15_data.reg_data, pdata_local.cp15_data.reg_data,
1544 pdata_local.cp15_data.reg_saved_state_size *
1545 sizeof(uint32_t));
1546 }
1547
1548 msm_pm_set_flush_fn(pdata_local.pc_mode);
1549 msm_pm_use_sync_timer = pdata_local.use_sync_timer;
1550 msm_pm_retention_calls_tz = pdata_local.retention_calls_tz;
1551
Girish Mahadevan55944992012-10-26 11:03:07 -06001552pm_8x60_probe_done:
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001553 msm_pm_init();
Anji Jonnala02dac8d2013-03-06 21:31:04 +05301554 if (pdev->dev.of_node)
1555 of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
1556
Girish Mahadevan55944992012-10-26 11:03:07 -06001557 return ret;
1558}
1559
1560static struct of_device_id msm_pm_8x60_table[] = {
1561 {.compatible = "qcom,pm-8x60"},
1562 {},
1563};
1564
1565static struct platform_driver msm_pm_8x60_driver = {
1566 .probe = msm_pm_8x60_probe,
1567 .driver = {
1568 .name = "pm-8x60",
1569 .owner = THIS_MODULE,
1570 .of_match_table = msm_pm_8x60_table,
1571 },
1572};
1573
1574static int __init msm_pm_8x60_init(void)
1575{
Anji Jonnala02dac8d2013-03-06 21:31:04 +05301576 int rc;
1577
1578 rc = platform_driver_register(&msm_cpu_status_driver);
1579
1580 if (rc) {
1581 pr_err("%s(): failed to register driver %s\n", __func__,
1582 msm_cpu_status_driver.driver.name);
1583 return rc;
1584 }
1585
Girish Mahadevan55944992012-10-26 11:03:07 -06001586 return platform_driver_register(&msm_pm_8x60_driver);
1587}
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001588device_initcall(msm_pm_8x60_init);