blob: 779227654b1ca5e32df6971f29fce31f21beca78 [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>
Badhri Jagan Sridharan9b186af2013-05-24 12:47:35 -070033#include <linux/clk.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070034#include <mach/msm_iomap.h>
Praveen Chidambaram192979f2012-04-25 18:30:23 -060035#include <mach/socinfo.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070036#include <mach/system.h>
Girish Mahadevand27ca4a2012-08-15 09:21:23 -060037#include <mach/scm.h>
38#include <mach/socinfo.h>
Hanumant Singhbd82a3e2013-02-26 13:51:17 -080039#define CREATE_TRACE_POINTS
40#include <mach/trace_msm_low_power.h>
Praveen Chidambaramc594a092012-09-18 19:48:29 -060041#include <mach/msm-krait-l2-accessors.h>
Girish Mahadevanec50d2d2013-05-15 12:07:38 -060042#include <mach/msm_bus.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070043#include <asm/cacheflush.h>
44#include <asm/hardware/gic.h>
45#include <asm/pgtable.h>
46#include <asm/pgalloc.h>
Girish Mahadevan55944992012-10-26 11:03:07 -060047#include <asm/outercache.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070048#ifdef CONFIG_VFP
49#include <asm/vfp.h>
50#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070051#include "acpuclock.h"
52#include "clock.h"
53#include "avs.h"
Abhijeet Dharmapurikarefaca4f2011-12-27 16:24:07 -080054#include <mach/cpuidle.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070055#include "idle.h"
Matt Wagantall7cca4642012-02-01 16:43:24 -080056#include "pm.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070057#include "scm-boot.h"
58#include "spm.h"
59#include "timer.h"
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -060060#include "pm-boot.h"
Girish Mahadevandc318fd2012-08-17 16:48:05 -060061#include <mach/event_timer.h>
Hanumant Singhbd82a3e2013-02-26 13:51:17 -080062
Praveen Chidambaramf27a5152013-02-01 11:44:53 -070063#define SCM_L2_RETENTION (0x2)
64#define SCM_CMD_TERMINATE_PC (0x2)
65
66#define GET_CPU_OF_ATTR(attr) \
67 (container_of(attr, struct msm_pm_kobj_attribute, ka)->cpu)
68
69#define SCLK_HZ (32768)
Praveen Chidambaramf27a5152013-02-01 11:44:53 -070070
Priyanka Mathur13bdad12013-01-28 15:52:56 -080071#define NUM_OF_COUNTERS 3
72#define MAX_BUF_SIZE 512
73
Praveen Chidambaramf27a5152013-02-01 11:44:53 -070074static int msm_pm_debug_mask = 1;
75module_param_named(
76 debug_mask, msm_pm_debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP
77);
78
79static int msm_pm_sleep_time_override;
80module_param_named(sleep_time_override,
81 msm_pm_sleep_time_override, int, S_IRUGO | S_IWUSR | S_IWGRP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070082
83enum {
84 MSM_PM_DEBUG_SUSPEND = BIT(0),
85 MSM_PM_DEBUG_POWER_COLLAPSE = BIT(1),
86 MSM_PM_DEBUG_SUSPEND_LIMITS = BIT(2),
87 MSM_PM_DEBUG_CLOCK = BIT(3),
88 MSM_PM_DEBUG_RESET_VECTOR = BIT(4),
Karthik Parsha6fb932d2012-01-24 18:04:12 -080089 MSM_PM_DEBUG_IDLE_CLK = BIT(5),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070090 MSM_PM_DEBUG_IDLE = BIT(6),
91 MSM_PM_DEBUG_IDLE_LIMITS = BIT(7),
92 MSM_PM_DEBUG_HOTPLUG = BIT(8),
93};
94
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070095enum {
96 MSM_PM_MODE_ATTR_SUSPEND,
97 MSM_PM_MODE_ATTR_IDLE,
98 MSM_PM_MODE_ATTR_NR,
99};
100
101static char *msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_NR] = {
102 [MSM_PM_MODE_ATTR_SUSPEND] = "suspend_enabled",
103 [MSM_PM_MODE_ATTR_IDLE] = "idle_enabled",
104};
105
106struct msm_pm_kobj_attribute {
107 unsigned int cpu;
108 struct kobj_attribute ka;
109};
110
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700111struct msm_pm_sysfs_sleep_mode {
112 struct kobject *kobj;
113 struct attribute_group attr_group;
114 struct attribute *attrs[MSM_PM_MODE_ATTR_NR + 1];
115 struct msm_pm_kobj_attribute kas[MSM_PM_MODE_ATTR_NR];
116};
117
118static char *msm_pm_sleep_mode_labels[MSM_PM_SLEEP_MODE_NR] = {
119 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE] = "power_collapse",
120 [MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT] = "wfi",
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600121 [MSM_PM_SLEEP_MODE_RETENTION] = "retention",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700122 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE] =
123 "standalone_power_collapse",
124};
125
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600126static struct hrtimer pm_hrtimer;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600127static struct msm_pm_sleep_ops pm_sleep_ops;
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -0700128static bool msm_pm_ldo_retention_enabled = true;
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700129static bool msm_pm_use_sync_timer;
130static struct msm_pm_cp15_save_data cp15_data;
131static bool msm_pm_retention_calls_tz;
Praveen Chidambaram4b8df032013-01-18 16:21:16 -0700132static bool msm_no_ramp_down_pc;
Anji Jonnala02dac8d2013-03-06 21:31:04 +0530133static struct msm_pm_sleep_status_data *msm_pm_slp_sts;
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -0600134static bool msm_pm_pc_reset_timer;
Badhri Jagan Sridharan9b186af2013-05-24 12:47:35 -0700135static struct clk *pnoc_clk;
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700136
Archana Sathyakumar2b91dc82013-02-01 17:38:23 -0700137static int msm_pm_get_pc_mode(struct device_node *node,
138 const char *key, uint32_t *pc_mode_val)
139{
140 struct pc_mode_of {
141 uint32_t mode;
142 char *mode_name;
143 };
144 int i;
145 struct pc_mode_of pc_modes[] = {
146 {MSM_PM_PC_TZ_L2_INT, "tz_l2_int"},
147 {MSM_PM_PC_NOTZ_L2_EXT, "no_tz_l2_ext"},
148 {MSM_PM_PC_TZ_L2_EXT , "tz_l2_ext"} };
149 int ret;
150 const char *pc_mode_str;
151
152 ret = of_property_read_string(node, key, &pc_mode_str);
153 if (ret) {
154 pr_debug("%s: Cannot read %s,defaulting to 0", __func__, key);
155 pc_mode_val = MSM_PM_PC_TZ_L2_INT;
156 ret = 0;
157 } else {
158 ret = -EINVAL;
159 for (i = 0; i < ARRAY_SIZE(pc_modes); i++) {
160 if (!strncmp(pc_mode_str, pc_modes[i].mode_name,
161 strlen(pc_modes[i].mode_name))) {
162 *pc_mode_val = pc_modes[i].mode;
163 ret = 0;
164 break;
165 }
166 }
167 }
168 return ret;
169}
170
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700171/*
172 * Write out the attribute.
173 */
174static ssize_t msm_pm_mode_attr_show(
175 struct kobject *kobj, struct kobj_attribute *attr, char *buf)
176{
177 int ret = -EINVAL;
178 int i;
179
180 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
181 struct kernel_param kp;
182 unsigned int cpu;
183 struct msm_pm_platform_data *mode;
184
185 if (msm_pm_sleep_mode_labels[i] == NULL)
186 continue;
187
188 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
189 continue;
190
191 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600192 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700193
194 if (!strcmp(attr->attr.name,
195 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
196 u32 arg = mode->suspend_enabled;
197 kp.arg = &arg;
198 ret = param_get_ulong(buf, &kp);
199 } else if (!strcmp(attr->attr.name,
200 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
201 u32 arg = mode->idle_enabled;
202 kp.arg = &arg;
203 ret = param_get_ulong(buf, &kp);
204 }
205
206 break;
207 }
208
209 if (ret > 0) {
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600210 strlcat(buf, "\n", PAGE_SIZE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700211 ret++;
212 }
213
214 return ret;
215}
216
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700217static ssize_t msm_pm_mode_attr_store(struct kobject *kobj,
218 struct kobj_attribute *attr, const char *buf, size_t count)
219{
220 int ret = -EINVAL;
221 int i;
222
223 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
224 struct kernel_param kp;
225 unsigned int cpu;
226 struct msm_pm_platform_data *mode;
227
228 if (msm_pm_sleep_mode_labels[i] == NULL)
229 continue;
230
231 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
232 continue;
233
234 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600235 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700236
237 if (!strcmp(attr->attr.name,
238 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
239 kp.arg = &mode->suspend_enabled;
240 ret = param_set_byte(buf, &kp);
241 } else if (!strcmp(attr->attr.name,
242 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
243 kp.arg = &mode->idle_enabled;
244 ret = param_set_byte(buf, &kp);
245 }
246
247 break;
248 }
249
250 return ret ? ret : count;
251}
252
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700253static int __devinit msm_pm_mode_sysfs_add_cpu(
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700254 unsigned int cpu, struct kobject *modes_kobj)
255{
256 char cpu_name[8];
257 struct kobject *cpu_kobj;
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600258 struct msm_pm_sysfs_sleep_mode *mode = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700259 int i, j, k;
260 int ret;
261
262 snprintf(cpu_name, sizeof(cpu_name), "cpu%u", cpu);
263 cpu_kobj = kobject_create_and_add(cpu_name, modes_kobj);
264 if (!cpu_kobj) {
265 pr_err("%s: cannot create %s kobject\n", __func__, cpu_name);
266 ret = -ENOMEM;
267 goto mode_sysfs_add_cpu_exit;
268 }
269
270 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
271 int idx = MSM_PM_MODE(cpu, i);
272
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600273 if ((!msm_pm_sleep_modes[idx].suspend_supported)
274 && (!msm_pm_sleep_modes[idx].idle_supported))
275 continue;
276
277 if (!msm_pm_sleep_mode_labels[i] ||
278 !msm_pm_sleep_mode_labels[i][0])
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700279 continue;
280
281 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
282 if (!mode) {
283 pr_err("%s: cannot allocate memory for attributes\n",
284 __func__);
285 ret = -ENOMEM;
286 goto mode_sysfs_add_cpu_exit;
287 }
288
289 mode->kobj = kobject_create_and_add(
290 msm_pm_sleep_mode_labels[i], cpu_kobj);
291 if (!mode->kobj) {
292 pr_err("%s: cannot create kobject\n", __func__);
293 ret = -ENOMEM;
294 goto mode_sysfs_add_cpu_exit;
295 }
296
297 for (k = 0, j = 0; k < MSM_PM_MODE_ATTR_NR; k++) {
298 if ((k == MSM_PM_MODE_ATTR_IDLE) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600299 !msm_pm_sleep_modes[idx].idle_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700300 continue;
301 if ((k == MSM_PM_MODE_ATTR_SUSPEND) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600302 !msm_pm_sleep_modes[idx].suspend_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700303 continue;
Stephen Boydd2059c32012-07-03 15:11:15 -0700304 sysfs_attr_init(&mode->kas[j].ka.attr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700305 mode->kas[j].cpu = cpu;
306 mode->kas[j].ka.attr.mode = 0644;
307 mode->kas[j].ka.show = msm_pm_mode_attr_show;
308 mode->kas[j].ka.store = msm_pm_mode_attr_store;
309 mode->kas[j].ka.attr.name = msm_pm_mode_attr_labels[k];
310 mode->attrs[j] = &mode->kas[j].ka.attr;
311 j++;
312 }
313 mode->attrs[j] = NULL;
314
315 mode->attr_group.attrs = mode->attrs;
316 ret = sysfs_create_group(mode->kobj, &mode->attr_group);
317 if (ret) {
318 pr_err("%s: cannot create kobject attribute group\n",
319 __func__);
320 goto mode_sysfs_add_cpu_exit;
321 }
322 }
323
324 ret = 0;
325
326mode_sysfs_add_cpu_exit:
Praveen Chidambaramd5ac2d32011-10-24 14:30:27 -0600327 if (ret) {
Praveen Chidambaram2cfda632011-10-11 16:58:09 -0600328 if (mode && mode->kobj)
329 kobject_del(mode->kobj);
330 kfree(mode);
331 }
332
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700333 return ret;
334}
335
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700336int __devinit msm_pm_mode_sysfs_add(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700337{
338 struct kobject *module_kobj;
339 struct kobject *modes_kobj;
340 unsigned int cpu;
341 int ret;
342
343 module_kobj = kset_find_obj(module_kset, KBUILD_MODNAME);
344 if (!module_kobj) {
345 pr_err("%s: cannot find kobject for module %s\n",
346 __func__, KBUILD_MODNAME);
347 ret = -ENOENT;
348 goto mode_sysfs_add_exit;
349 }
350
351 modes_kobj = kobject_create_and_add("modes", module_kobj);
352 if (!modes_kobj) {
353 pr_err("%s: cannot create modes kobject\n", __func__);
354 ret = -ENOMEM;
355 goto mode_sysfs_add_exit;
356 }
357
358 for_each_possible_cpu(cpu) {
359 ret = msm_pm_mode_sysfs_add_cpu(cpu, modes_kobj);
360 if (ret)
361 goto mode_sysfs_add_exit;
362 }
363
364 ret = 0;
365
366mode_sysfs_add_exit:
367 return ret;
368}
369
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700370/*
371 * Configure hardware registers in preparation for Apps power down.
372 */
373static void msm_pm_config_hw_before_power_down(void)
374{
375 return;
376}
377
378/*
379 * Clear hardware registers after Apps powers up.
380 */
381static void msm_pm_config_hw_after_power_up(void)
382{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700383}
384
385/*
386 * Configure hardware registers in preparation for SWFI.
387 */
388static void msm_pm_config_hw_before_swfi(void)
389{
390 return;
391}
392
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600393/*
394 * Configure/Restore hardware registers in preparation for Retention.
395 */
396
397static void msm_pm_config_hw_after_retention(void)
398{
399 int ret;
Abhijeet Dharmapurikare8f83852013-02-06 18:57:17 -0800400
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600401 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
402 WARN_ON(ret);
403}
404
405static void msm_pm_config_hw_before_retention(void)
406{
407 return;
408}
409
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600410static void msm_pm_save_cpu_reg(void)
411{
412 int i;
413
414 /* Only on core0 */
415 if (smp_processor_id())
416 return;
417
418 /**
419 * On some targets, L2 PC will turn off may reset the core
420 * configuration for the mux and the default may not make the core
421 * happy when it resumes.
422 * Save the active vdd, and set the core vdd to QSB max vdd, so that
423 * when the core resumes, it is capable of supporting the current QSB
424 * rate. Then restore the active vdd before switching the acpuclk rate.
425 */
426 if (msm_pm_get_l2_flush_flag() == 1) {
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700427 cp15_data.active_vdd = msm_spm_get_vdd(0);
428 for (i = 0; i < cp15_data.reg_saved_state_size; i++)
429 cp15_data.reg_val[i] =
430 get_l2_indirect_reg(
431 cp15_data.reg_data[i]);
432 msm_spm_set_vdd(0, cp15_data.qsb_pc_vdd);
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600433 }
434}
435
436static void msm_pm_restore_cpu_reg(void)
437{
438 int i;
439
440 /* Only on core0 */
441 if (smp_processor_id())
442 return;
443
444 if (msm_pm_get_l2_flush_flag() == 1) {
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700445 for (i = 0; i < cp15_data.reg_saved_state_size; i++)
446 set_l2_indirect_reg(
447 cp15_data.reg_data[i],
448 cp15_data.reg_val[i]);
449 msm_spm_set_vdd(0, cp15_data.active_vdd);
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600450 }
451}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700452
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700453static void msm_pm_swfi(void)
454{
455 msm_pm_config_hw_before_swfi();
456 msm_arch_idle();
457}
458
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600459static void msm_pm_retention(void)
460{
461 int ret = 0;
462
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600463 msm_pm_config_hw_before_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600464 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_POWER_RETENTION, false);
465 WARN_ON(ret);
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600466
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700467 if (msm_pm_retention_calls_tz)
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600468 scm_call_atomic1(SCM_SVC_BOOT, SCM_CMD_TERMINATE_PC,
469 SCM_L2_RETENTION);
470 else
471 msm_arch_idle();
472
473 msm_pm_config_hw_after_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600474}
475
Stephen Boydb29750d2012-02-21 01:21:32 -0800476static bool __ref msm_pm_spm_power_collapse(
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700477 unsigned int cpu, bool from_idle, bool notify_rpm)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700478{
479 void *entry;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600480 bool collapsed = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700481 int ret;
482
483 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
484 pr_info("CPU%u: %s: notify_rpm %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700485 cpu, __func__, (int) notify_rpm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700486
487 ret = msm_spm_set_low_power_mode(
488 MSM_SPM_MODE_POWER_COLLAPSE, notify_rpm);
489 WARN_ON(ret);
490
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700491 entry = (!cpu || from_idle) ?
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700492 msm_pm_collapse_exit : msm_secondary_startup;
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700493 msm_pm_boot_config_before_pc(cpu, virt_to_phys(entry));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700494
495 if (MSM_PM_DEBUG_RESET_VECTOR & msm_pm_debug_mask)
496 pr_info("CPU%u: %s: program vector to %p\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700497 cpu, __func__, entry);
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -0600498 if (from_idle && msm_pm_pc_reset_timer)
499 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700500
Girish Mahadevan55944992012-10-26 11:03:07 -0600501 collapsed = msm_pm_collapse();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700502
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -0600503 if (from_idle && msm_pm_pc_reset_timer)
504 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu);
505
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700506 msm_pm_boot_config_after_pc(cpu);
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -0600507
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700508 if (collapsed) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700509 cpu_init();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700510 local_fiq_enable();
511 }
512
513 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
514 pr_info("CPU%u: %s: msm_pm_collapse returned, collapsed %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700515 cpu, __func__, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700516
517 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
518 WARN_ON(ret);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600519 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700520}
521
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600522static bool msm_pm_power_collapse_standalone(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700523{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700524 unsigned int cpu = smp_processor_id();
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700525 unsigned int avsdscr;
526 unsigned int avscsr;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600527 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700528
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700529 avsdscr = avs_get_avsdscr();
530 avscsr = avs_get_avscsr();
531 avs_set_avscsr(0); /* Disable AVS */
532
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700533 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, false);
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700534
535 avs_set_avsdscr(avsdscr);
536 avs_set_avscsr(avscsr);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600537 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700538}
539
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600540static bool msm_pm_power_collapse(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700541{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700542 unsigned int cpu = smp_processor_id();
Praveen Chidambaram4b8df032013-01-18 16:21:16 -0700543 unsigned long saved_acpuclk_rate = 0;
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700544 unsigned int avsdscr;
545 unsigned int avscsr;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600546 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700547
548 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
549 pr_info("CPU%u: %s: idle %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700550 cpu, __func__, (int)from_idle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700551
552 msm_pm_config_hw_before_power_down();
553 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700554 pr_info("CPU%u: %s: pre power down\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700555
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700556 avsdscr = avs_get_avsdscr();
557 avscsr = avs_get_avscsr();
558 avs_set_avscsr(0); /* Disable AVS */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700559
Praveen Chidambaram4b8df032013-01-18 16:21:16 -0700560 if (cpu_online(cpu) && !msm_no_ramp_down_pc)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700561 saved_acpuclk_rate = acpuclk_power_collapse();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700562
563 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
564 pr_info("CPU%u: %s: change clock rate (old rate = %lu)\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700565 cpu, __func__, saved_acpuclk_rate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700566
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700567 if (cp15_data.save_cp15)
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600568 msm_pm_save_cpu_reg();
569
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700570 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700571
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700572 if (cp15_data.save_cp15)
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600573 msm_pm_restore_cpu_reg();
574
Girish Mahadevan4e025d92012-02-29 13:26:51 -0700575 if (cpu_online(cpu)) {
576 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
577 pr_info("CPU%u: %s: restore clock rate to %lu\n",
578 cpu, __func__, saved_acpuclk_rate);
Praveen Chidambaram4b8df032013-01-18 16:21:16 -0700579 if (!msm_no_ramp_down_pc &&
580 acpuclk_set_rate(cpu, saved_acpuclk_rate, SETRATE_PC)
581 < 0)
Girish Mahadevan4e025d92012-02-29 13:26:51 -0700582 pr_err("CPU%u: %s: failed to restore clock rate(%lu)\n",
583 cpu, __func__, saved_acpuclk_rate);
584 } else {
585 unsigned int gic_dist_enabled;
586 unsigned int gic_dist_pending;
587 gic_dist_enabled = readl_relaxed(
588 MSM_QGIC_DIST_BASE + GIC_DIST_ENABLE_CLEAR);
589 gic_dist_pending = readl_relaxed(
590 MSM_QGIC_DIST_BASE + GIC_DIST_PENDING_SET);
591 mb();
592 gic_dist_pending &= gic_dist_enabled;
593
594 if (gic_dist_pending)
595 pr_err("CPU %d interrupted during hotplug.Pending int 0x%x\n",
596 cpu, gic_dist_pending);
597 }
598
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700599
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700600 avs_set_avsdscr(avsdscr);
601 avs_set_avscsr(avscsr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700602 msm_pm_config_hw_after_power_up();
603 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700604 pr_info("CPU%u: %s: post power up\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700605
606 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700607 pr_info("CPU%u: %s: return\n", cpu, __func__);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600608 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700609}
610
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600611static int64_t msm_pm_timer_enter_idle(void)
612{
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700613 if (msm_pm_use_sync_timer)
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600614 return ktime_to_ns(tick_nohz_get_sleep_length());
615
616 return msm_timer_enter_idle();
617}
618
619static void msm_pm_timer_exit_idle(bool timer_halted)
620{
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700621 if (msm_pm_use_sync_timer)
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600622 return;
623
624 msm_timer_exit_idle((int) timer_halted);
625}
626
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530627static int64_t msm_pm_timer_enter_suspend(int64_t *period)
628{
Anji Jonnalac02367a2012-07-01 02:56:11 +0530629 int64_t time = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530630
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700631 if (msm_pm_use_sync_timer)
Mahesh Sivasubramanian0eaeb302013-03-14 14:56:32 -0600632 return sched_clock();
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530633
634 time = msm_timer_get_sclk_time(period);
635 if (!time)
636 pr_err("%s: Unable to read sclk.\n", __func__);
637
638 return time;
639}
640
641static int64_t msm_pm_timer_exit_suspend(int64_t time, int64_t period)
642{
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700643 if (msm_pm_use_sync_timer)
Mahesh Sivasubramanian0eaeb302013-03-14 14:56:32 -0600644 return sched_clock() - time;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530645
646 if (time != 0) {
647 int64_t end_time = msm_timer_get_sclk_time(NULL);
648 if (end_time != 0) {
649 time = end_time - time;
650 if (time < 0)
651 time += period;
652 } else
653 time = 0;
654 }
655
656 return time;
657}
658
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600659/**
660 * pm_hrtimer_cb() : Callback function for hrtimer created if the
661 * core needs to be awake to handle an event.
662 * @hrtimer : Pointer to hrtimer
663 */
664static enum hrtimer_restart pm_hrtimer_cb(struct hrtimer *hrtimer)
665{
666 return HRTIMER_NORESTART;
667}
668
669/**
670 * msm_pm_set_timer() : Set an hrtimer to wakeup the core in time
671 * to handle an event.
672 */
673static void msm_pm_set_timer(uint32_t modified_time_us)
674{
675 u64 modified_time_ns = modified_time_us * NSEC_PER_USEC;
676 ktime_t modified_ktime = ns_to_ktime(modified_time_ns);
677 pm_hrtimer.function = pm_hrtimer_cb;
Girish Mahadevan32493612013-05-10 14:37:17 -0600678 hrtimer_start(&pm_hrtimer, modified_ktime, HRTIMER_MODE_REL);
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600679}
680
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700681/******************************************************************************
682 * External Idle/Suspend Functions
683 *****************************************************************************/
684
685void arch_idle(void)
686{
687 return;
688}
689
Priyanka Mathur26b4a4b2012-11-05 13:45:45 -0800690static inline void msm_pm_ftrace_lpm_enter(unsigned int cpu,
691 uint32_t latency, uint32_t sleep_us,
692 uint32_t wake_up,
693 enum msm_pm_sleep_mode mode)
694{
695 switch (mode) {
696 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
697 trace_msm_pm_enter_wfi(cpu, latency, sleep_us, wake_up);
698 break;
699 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
700 trace_msm_pm_enter_spc(cpu, latency, sleep_us, wake_up);
701 break;
702 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
703 trace_msm_pm_enter_pc(cpu, latency, sleep_us, wake_up);
704 break;
705 case MSM_PM_SLEEP_MODE_RETENTION:
706 trace_msm_pm_enter_ret(cpu, latency, sleep_us, wake_up);
707 break;
708 default:
709 break;
710 }
711}
712
713static inline void msm_pm_ftrace_lpm_exit(unsigned int cpu,
714 enum msm_pm_sleep_mode mode,
715 bool success)
716{
717 switch (mode) {
718 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
719 trace_msm_pm_exit_wfi(cpu, success);
720 break;
721 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
722 trace_msm_pm_exit_spc(cpu, success);
723 break;
724 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
725 trace_msm_pm_exit_pc(cpu, success);
726 break;
727 case MSM_PM_SLEEP_MODE_RETENTION:
728 trace_msm_pm_exit_ret(cpu, success);
729 break;
730 default:
731 break;
732 }
733}
734
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800735static int msm_pm_idle_prepare(struct cpuidle_device *dev,
736 struct cpuidle_driver *drv, int index,
737 void **msm_pm_idle_rs_limits)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700738{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700739 int i;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700740 unsigned int power_usage = -1;
Priyanka Mathur848bb4c2012-11-30 18:04:57 -0800741 int ret = MSM_PM_SLEEP_MODE_NOT_SELECTED;
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600742 uint32_t modified_time_us = 0;
743 struct msm_pm_time_params time_param;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700744
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600745 time_param.latency_us =
746 (uint32_t) pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
747 time_param.sleep_us =
748 (uint32_t) (ktime_to_us(tick_nohz_get_sleep_length())
749 & UINT_MAX);
750 time_param.modified_time_us = 0;
751
752 if (!dev->cpu)
753 time_param.next_event_us =
754 (uint32_t) (ktime_to_us(get_next_event_time())
755 & UINT_MAX);
756 else
757 time_param.next_event_us = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700758
759 for (i = 0; i < dev->state_count; i++) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700760 struct cpuidle_state *state = &drv->states[i];
761 struct cpuidle_state_usage *st_usage = &dev->states_usage[i];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700762 enum msm_pm_sleep_mode mode;
763 bool allow;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600764 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700765 int idx;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800766 void *rs_limits = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700767
Steve Mucklef132c6c2012-06-06 18:30:57 -0700768 mode = (enum msm_pm_sleep_mode) cpuidle_get_statedata(st_usage);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700769 idx = MSM_PM_MODE(dev->cpu, mode);
770
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600771 allow = msm_pm_sleep_modes[idx].idle_enabled &&
772 msm_pm_sleep_modes[idx].idle_supported;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700773
774 switch (mode) {
775 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700776 if (num_online_cpus() > 1)
Priyanka Mathur68a9bc52012-10-03 15:15:01 -0700777 allow = false;
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700778 break;
Mahesh Sivasubramanianba7e01c2012-09-04 18:22:51 -0600779 case MSM_PM_SLEEP_MODE_RETENTION:
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -0700780 /*
781 * The Krait BHS regulator doesn't have enough head
782 * room to drive the retention voltage on LDO and so
783 * has disabled retention
784 */
785 if (!msm_pm_ldo_retention_enabled)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700786 allow = false;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700787
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700788 if (msm_pm_retention_calls_tz && num_online_cpus() > 1)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700789 allow = false;
790 break;
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700791 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
792 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
793 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700794 default:
795 allow = false;
796 break;
797 }
798
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700799 if (!allow)
800 continue;
801
802 if (pm_sleep_ops.lowest_limits)
803 rs_limits = pm_sleep_ops.lowest_limits(true,
804 mode, &time_param, &power);
805
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700806 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700807 pr_info("CPU%u:%s:%s, latency %uus, slp %uus, lim %p\n",
808 dev->cpu, __func__, state->desc,
809 time_param.latency_us,
810 time_param.sleep_us, rs_limits);
811 if (!rs_limits)
812 continue;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700813
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700814 if (power < power_usage) {
815 power_usage = power;
816 modified_time_us = time_param.modified_time_us;
817 ret = mode;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800818 *msm_pm_idle_rs_limits = rs_limits;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700819 }
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700820
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700821 }
822
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600823 if (modified_time_us && !dev->cpu)
824 msm_pm_set_timer(modified_time_us);
Priyanka Mathur26b4a4b2012-11-05 13:45:45 -0800825
826 msm_pm_ftrace_lpm_enter(dev->cpu, time_param.latency_us,
827 time_param.sleep_us, time_param.next_event_us,
828 ret);
829
Steve Mucklef132c6c2012-06-06 18:30:57 -0700830 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700831}
832
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800833enum msm_pm_sleep_mode msm_pm_idle_enter(struct cpuidle_device *dev,
834 struct cpuidle_driver *drv, int index)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700835{
836 int64_t time;
Priyanka Mathur26b4a4b2012-11-05 13:45:45 -0800837 bool collapsed = 1;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800838 int exit_stat = -1;
839 enum msm_pm_sleep_mode sleep_mode;
840 void *msm_pm_idle_rs_limits = NULL;
Mahesh Sivasubramanian03698762013-04-18 16:18:46 -0600841 uint32_t sleep_delay = 1;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800842 int ret = -ENODEV;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800843 int notify_rpm = false;
844 bool timer_halted = false;
845
846 sleep_mode = msm_pm_idle_prepare(dev, drv, index,
847 &msm_pm_idle_rs_limits);
848
849 if (!msm_pm_idle_rs_limits) {
850 sleep_mode = MSM_PM_SLEEP_MODE_NOT_SELECTED;
851 goto cpuidle_enter_bail;
852 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700853
854 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
855 pr_info("CPU%u: %s: mode %d\n",
856 smp_processor_id(), __func__, sleep_mode);
857
858 time = ktime_to_ns(ktime_get());
859
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800860 if (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE) {
Mahesh Sivasubramanian2f415382013-05-17 16:29:05 -0600861 int64_t ns = msm_pm_timer_enter_idle();
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800862 notify_rpm = true;
Mahesh Sivasubramanian2f415382013-05-17 16:29:05 -0600863 do_div(ns, NSEC_PER_SEC / SCLK_HZ);
864 sleep_delay = (uint32_t)ns;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800865
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800866 if (sleep_delay == 0) /* 0 would mean infinite time */
867 sleep_delay = 1;
868 }
869
870 if (pm_sleep_ops.enter_sleep)
871 ret = pm_sleep_ops.enter_sleep(sleep_delay,
872 msm_pm_idle_rs_limits, true, notify_rpm);
873 if (ret)
874 goto cpuidle_enter_bail;
875
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700876 switch (sleep_mode) {
877 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
878 msm_pm_swfi();
879 exit_stat = MSM_PM_STAT_IDLE_WFI;
880 break;
881
882 case MSM_PM_SLEEP_MODE_RETENTION:
883 msm_pm_retention();
884 exit_stat = MSM_PM_STAT_RETENTION;
885 break;
886
887 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
888 collapsed = msm_pm_power_collapse_standalone(true);
Venkat Devarasetty849f47f2013-05-19 17:44:18 +0530889 if (collapsed)
890 exit_stat = MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE;
891 else
892 exit_stat
893 = MSM_PM_STAT_IDLE_FAILED_STANDALONE_POWER_COLLAPSE;
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700894 break;
895
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800896 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700897 if (MSM_PM_DEBUG_IDLE_CLK & msm_pm_debug_mask)
898 clock_debug_print_enabled();
Karthik Parsha6fb932d2012-01-24 18:04:12 -0800899
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800900 collapsed = msm_pm_power_collapse(true);
901 timer_halted = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700902
Venkat Devarasetty849f47f2013-05-19 17:44:18 +0530903 if (collapsed)
904 exit_stat = MSM_PM_STAT_IDLE_POWER_COLLAPSE;
905 else
906 exit_stat = MSM_PM_STAT_IDLE_FAILED_POWER_COLLAPSE;
907
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800908 msm_pm_timer_exit_idle(timer_halted);
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700909 break;
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700910
911 case MSM_PM_SLEEP_MODE_NOT_SELECTED:
912 goto cpuidle_enter_bail;
913 break;
914
915 default:
916 __WARN();
917 goto cpuidle_enter_bail;
918 break;
919 }
920
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800921 if (pm_sleep_ops.exit_sleep)
922 pm_sleep_ops.exit_sleep(msm_pm_idle_rs_limits, true,
923 notify_rpm, collapsed);
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700924
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800925 time = ktime_to_ns(ktime_get()) - time;
926 msm_pm_ftrace_lpm_exit(smp_processor_id(), sleep_mode, collapsed);
927 if (exit_stat >= 0)
928 msm_pm_add_stat(exit_stat, time);
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700929 do_div(time, 1000);
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800930 dev->last_residency = (int) time;
931 return sleep_mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700932
933cpuidle_enter_bail:
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800934 dev->last_residency = 0;
935 if (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE)
936 msm_pm_timer_exit_idle(timer_halted);
937 sleep_mode = MSM_PM_SLEEP_MODE_NOT_SELECTED;
938 return sleep_mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700939}
940
Anji Jonnala02dac8d2013-03-06 21:31:04 +0530941int msm_pm_wait_cpu_shutdown(unsigned int cpu)
942{
Mahesh Sivasubramanian32b9d422013-05-20 13:50:16 -0600943 int timeout = 0;
Anji Jonnala02dac8d2013-03-06 21:31:04 +0530944
945 if (!msm_pm_slp_sts)
946 return 0;
947 if (!msm_pm_slp_sts[cpu].base_addr)
948 return 0;
Mahesh Sivasubramanian32b9d422013-05-20 13:50:16 -0600949 while (1) {
Anji Jonnala02dac8d2013-03-06 21:31:04 +0530950 /*
951 * Check for the SPM of the core being hotplugged to set
952 * its sleep state.The SPM sleep state indicates that the
953 * core has been power collapsed.
954 */
955 int acc_sts = __raw_readl(msm_pm_slp_sts[cpu].base_addr);
956
957 if (acc_sts & msm_pm_slp_sts[cpu].mask)
958 return 0;
Mahesh Sivasubramanian65187f92013-03-19 15:14:06 -0600959 udelay(100);
Mahesh Sivasubramanian32b9d422013-05-20 13:50:16 -0600960 WARN(++timeout == 10, "CPU%u didn't collape within 1ms\n",
961 cpu);
Anji Jonnala02dac8d2013-03-06 21:31:04 +0530962 }
963
Anji Jonnala02dac8d2013-03-06 21:31:04 +0530964 return -EBUSY;
965}
966
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700967void msm_pm_cpu_enter_lowpower(unsigned int cpu)
968{
969 int i;
970 bool allow[MSM_PM_SLEEP_MODE_NR];
971
972 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
973 struct msm_pm_platform_data *mode;
974
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600975 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700976 allow[i] = mode->suspend_supported && mode->suspend_enabled;
977 }
978
979 if (MSM_PM_DEBUG_HOTPLUG & msm_pm_debug_mask)
980 pr_notice("CPU%u: %s: shutting down cpu\n", cpu, __func__);
981
Matt Wagantall5375aa72012-07-02 19:59:51 -0700982 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700983 msm_pm_power_collapse(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -0700984 else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700985 msm_pm_power_collapse_standalone(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -0700986 else if (allow[MSM_PM_SLEEP_MODE_RETENTION])
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600987 msm_pm_retention();
Stephen Boydbda74272012-08-09 14:01:27 -0700988 else
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700989 msm_pm_swfi();
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700990}
991
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -0700992static void msm_pm_ack_retention_disable(void *data)
993{
994 /*
995 * This is a NULL function to ensure that the core has woken up
996 * and is safe to disable retention.
997 */
998}
999/**
1000 * msm_pm_enable_retention() - Disable/Enable retention on all cores
1001 * @enable: Enable/Disable retention
1002 *
1003 */
1004void msm_pm_enable_retention(bool enable)
1005{
Matt Wagantalld06f5c22013-03-14 19:31:14 -07001006 if (enable == msm_pm_ldo_retention_enabled)
1007 return;
1008
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -07001009 msm_pm_ldo_retention_enabled = enable;
1010 /*
1011 * If retention is being disabled, wakeup all online core to ensure
1012 * that it isn't executing retention. Offlined cores need not be woken
1013 * up as they enter the deepest sleep mode, namely RPM assited power
1014 * collapse
1015 */
Mahesh Sivasubramanian96f67d12013-03-14 22:44:42 -06001016 if (!enable) {
1017 preempt_disable();
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -07001018 smp_call_function_many(cpu_online_mask,
1019 msm_pm_ack_retention_disable,
1020 NULL, true);
Mahesh Sivasubramanian96f67d12013-03-14 22:44:42 -06001021 preempt_enable();
1022
1023
1024 }
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -07001025}
1026EXPORT_SYMBOL(msm_pm_enable_retention);
1027
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001028static int msm_pm_enter(suspend_state_t state)
1029{
1030 bool allow[MSM_PM_SLEEP_MODE_NR];
1031 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001032 int64_t period = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301033 int64_t time = msm_pm_timer_enter_suspend(&period);
Girish Mahadevandc318fd2012-08-17 16:48:05 -06001034 struct msm_pm_time_params time_param;
1035
1036 time_param.latency_us = -1;
1037 time_param.sleep_us = -1;
1038 time_param.next_event_us = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001039
1040 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1041 pr_info("%s\n", __func__);
1042
1043 if (smp_processor_id()) {
1044 __WARN();
1045 goto enter_exit;
1046 }
1047
1048
1049 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
1050 struct msm_pm_platform_data *mode;
1051
Praveen Chidambaram42da9d22012-03-30 12:16:34 -06001052 mode = &msm_pm_sleep_modes[MSM_PM_MODE(0, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001053 allow[i] = mode->suspend_supported && mode->suspend_enabled;
1054 }
1055
1056 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001057 void *rs_limits = NULL;
1058 int ret = -ENODEV;
1059 uint32_t power;
Mahesh Sivasubramanian03698762013-04-18 16:18:46 -06001060 uint32_t msm_pm_max_sleep_time = 0;
Venkat Devarasetty849f47f2013-05-19 17:44:18 +05301061 int collapsed = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001062
1063 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1064 pr_info("%s: power collapse\n", __func__);
1065
1066 clock_debug_print_enabled();
1067
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001068 if (msm_pm_sleep_time_override > 0) {
1069 int64_t ns = NSEC_PER_SEC *
1070 (int64_t) msm_pm_sleep_time_override;
Mahesh Sivasubramanian03698762013-04-18 16:18:46 -06001071 do_div(ns, NSEC_PER_SEC / SCLK_HZ);
1072 msm_pm_max_sleep_time = (uint32_t) ns;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001073 }
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001074
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001075 if (pm_sleep_ops.lowest_limits)
1076 rs_limits = pm_sleep_ops.lowest_limits(false,
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001077 MSM_PM_SLEEP_MODE_POWER_COLLAPSE, &time_param, &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001078
1079 if (rs_limits) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001080 if (pm_sleep_ops.enter_sleep)
1081 ret = pm_sleep_ops.enter_sleep(
1082 msm_pm_max_sleep_time,
1083 rs_limits, false, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001084 if (!ret) {
Venkat Devarasetty849f47f2013-05-19 17:44:18 +05301085 collapsed = msm_pm_power_collapse(false);
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001086 if (pm_sleep_ops.exit_sleep) {
1087 pm_sleep_ops.exit_sleep(rs_limits,
1088 false, true, collapsed);
1089 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001090 }
1091 } else {
1092 pr_err("%s: cannot find the lowest power limit\n",
1093 __func__);
1094 }
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301095 time = msm_pm_timer_exit_suspend(time, period);
Venkat Devarasetty849f47f2013-05-19 17:44:18 +05301096 if (collapsed)
1097 msm_pm_add_stat(MSM_PM_STAT_SUSPEND, time);
1098 else
1099 msm_pm_add_stat(MSM_PM_STAT_FAILED_SUSPEND, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001100 } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
1101 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1102 pr_info("%s: standalone power collapse\n", __func__);
1103 msm_pm_power_collapse_standalone(false);
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -06001104 } else if (allow[MSM_PM_SLEEP_MODE_RETENTION]) {
1105 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1106 pr_info("%s: retention\n", __func__);
1107 msm_pm_retention();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001108 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
1109 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1110 pr_info("%s: swfi\n", __func__);
1111 msm_pm_swfi();
1112 }
1113
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001114enter_exit:
1115 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1116 pr_info("%s: return\n", __func__);
1117
1118 return 0;
1119}
1120
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001121void msm_pm_set_sleep_ops(struct msm_pm_sleep_ops *ops)
1122{
1123 if (ops)
1124 pm_sleep_ops = *ops;
1125}
1126
Badhri Jagan Sridharan9b186af2013-05-24 12:47:35 -07001127int msm_suspend_prepare(void)
1128{
1129 if (pnoc_clk != NULL)
1130 clk_disable_unprepare(pnoc_clk);
1131 return 0;
1132}
1133
1134void msm_suspend_wake(void)
1135{
1136 if (pnoc_clk != NULL)
1137 clk_prepare_enable(pnoc_clk);
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,
Badhri Jagan Sridharan9b186af2013-05-24 12:47:35 -07001143 .prepare_late = msm_suspend_prepare,
1144 .wake = msm_suspend_wake,
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -06001145};
Girish Mahadevanec50d2d2013-05-15 12:07:38 -06001146
1147static int __devinit msm_pm_snoc_client_probe(struct platform_device *pdev)
1148{
1149 int rc = 0;
1150 static struct msm_bus_scale_pdata *msm_pm_bus_pdata;
1151 static uint32_t msm_pm_bus_client;
1152
1153 msm_pm_bus_pdata = msm_bus_cl_get_pdata(pdev);
1154
1155 if (msm_pm_bus_pdata) {
1156 msm_pm_bus_client =
1157 msm_bus_scale_register_client(msm_pm_bus_pdata);
1158
1159 if (!msm_pm_bus_client) {
1160 pr_err("%s: Failed to register SNOC client",
1161 __func__);
1162 rc = -ENXIO;
1163 goto snoc_cl_probe_done;
1164 }
1165
1166 rc = msm_bus_scale_client_update_request(msm_pm_bus_client, 1);
1167
1168 if (rc)
1169 pr_err("%s: Error setting bus rate", __func__);
1170 }
1171
1172snoc_cl_probe_done:
1173 return rc;
1174}
1175
Anji Jonnala02dac8d2013-03-06 21:31:04 +05301176static int __devinit msm_cpu_status_probe(struct platform_device *pdev)
1177{
1178 struct msm_pm_sleep_status_data *pdata;
1179 char *key;
1180 u32 cpu;
1181
1182 if (!pdev)
1183 return -EFAULT;
1184
1185 msm_pm_slp_sts =
1186 kzalloc(sizeof(*msm_pm_slp_sts) * num_possible_cpus(),
1187 GFP_KERNEL);
1188
1189 if (!msm_pm_slp_sts)
1190 return -ENOMEM;
1191
1192 if (pdev->dev.of_node) {
1193 struct resource *res;
1194 u32 offset;
1195 int rc;
1196 u32 mask;
1197
1198 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1199 if (!res)
1200 goto fail_free_mem;
1201
1202 key = "qcom,cpu-alias-addr";
1203 rc = of_property_read_u32(pdev->dev.of_node, key, &offset);
1204
1205 if (rc)
1206 goto fail_free_mem;
1207
1208 key = "qcom,sleep-status-mask";
1209 rc = of_property_read_u32(pdev->dev.of_node, key,
1210 &mask);
1211 if (rc)
1212 goto fail_free_mem;
1213
1214 for_each_possible_cpu(cpu) {
1215 msm_pm_slp_sts[cpu].base_addr =
1216 ioremap(res->start + cpu * offset,
1217 resource_size(res));
1218 msm_pm_slp_sts[cpu].mask = mask;
1219
1220 if (!msm_pm_slp_sts[cpu].base_addr)
1221 goto failed_of_node;
1222 }
1223
1224 } else {
1225 pdata = pdev->dev.platform_data;
1226 if (!pdev->dev.platform_data)
1227 goto fail_free_mem;
1228
1229 for_each_possible_cpu(cpu) {
1230 msm_pm_slp_sts[cpu].base_addr =
1231 pdata->base_addr + cpu * pdata->cpu_offset;
1232 msm_pm_slp_sts[cpu].mask = pdata->mask;
1233 }
1234 }
1235
1236 return 0;
1237
1238failed_of_node:
1239 pr_info("%s(): Failed to key=%s\n", __func__, key);
1240 for_each_possible_cpu(cpu) {
1241 if (msm_pm_slp_sts[cpu].base_addr)
1242 iounmap(msm_pm_slp_sts[cpu].base_addr);
1243 }
1244fail_free_mem:
1245 kfree(msm_pm_slp_sts);
1246 return -EINVAL;
1247
1248};
1249
1250static struct of_device_id msm_slp_sts_match_tbl[] = {
1251 {.compatible = "qcom,cpu-sleep-status"},
1252 {},
1253};
1254
1255static struct platform_driver msm_cpu_status_driver = {
1256 .probe = msm_cpu_status_probe,
1257 .driver = {
1258 .name = "cpu_slp_status",
1259 .owner = THIS_MODULE,
1260 .of_match_table = msm_slp_sts_match_tbl,
1261 },
1262};
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -06001263
Girish Mahadevanec50d2d2013-05-15 12:07:38 -06001264static struct of_device_id msm_snoc_clnt_match_tbl[] = {
1265 {.compatible = "qcom,pm-snoc-client"},
1266 {},
1267};
1268
1269static struct platform_driver msm_cpu_pm_snoc_client_driver = {
1270 .probe = msm_pm_snoc_client_probe,
1271 .driver = {
1272 .name = "pm_snoc_client",
1273 .owner = THIS_MODULE,
1274 .of_match_table = msm_snoc_clnt_match_tbl,
1275 },
1276};
1277
1278
Praveen Chidambaramede45d02013-02-20 17:45:21 -07001279static int __init msm_pm_setup_saved_state(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001280{
1281 pgd_t *pc_pgd;
1282 pmd_t *pmd;
1283 unsigned long pmdval;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001284 unsigned long exit_phys;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301285
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001286 /* Page table for cores to come back up safely. */
1287 pc_pgd = pgd_alloc(&init_mm);
1288 if (!pc_pgd)
1289 return -ENOMEM;
1290
Steve Mucklef132c6c2012-06-06 18:30:57 -07001291 exit_phys = virt_to_phys(msm_pm_collapse_exit);
1292
1293 pmd = pmd_offset(pud_offset(pc_pgd + pgd_index(exit_phys),exit_phys),
1294 exit_phys);
1295 pmdval = (exit_phys & PGDIR_MASK) |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001296 PMD_TYPE_SECT | PMD_SECT_AP_WRITE;
1297 pmd[0] = __pmd(pmdval);
1298 pmd[1] = __pmd(pmdval + (1 << (PGDIR_SHIFT - 1)));
1299
Steve Mucklefcece052012-02-18 20:09:58 -08001300 msm_saved_state_phys =
1301 allocate_contiguous_ebi_nomap(CPU_SAVED_STATE_SIZE *
1302 num_possible_cpus(), 4);
1303 if (!msm_saved_state_phys)
1304 return -ENOMEM;
1305 msm_saved_state = ioremap_nocache(msm_saved_state_phys,
1306 CPU_SAVED_STATE_SIZE *
1307 num_possible_cpus());
1308 if (!msm_saved_state)
1309 return -ENOMEM;
1310
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001311 /* It is remotely possible that the code in msm_pm_collapse_exit()
1312 * which turns on the MMU with this mapping is in the
1313 * next even-numbered megabyte beyond the
1314 * start of msm_pm_collapse_exit().
1315 * Map this megabyte in as well.
1316 */
1317 pmd[2] = __pmd(pmdval + (2 << (PGDIR_SHIFT - 1)));
1318 flush_pmd_entry(pmd);
1319 msm_pm_pc_pgd = virt_to_phys(pc_pgd);
Steve Muckle730ad7a2012-02-21 15:26:37 -08001320 clean_caches((unsigned long)&msm_pm_pc_pgd, sizeof(msm_pm_pc_pgd),
1321 virt_to_phys(&msm_pm_pc_pgd));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001322
Praveen Chidambaramede45d02013-02-20 17:45:21 -07001323 return 0;
1324}
1325core_initcall(msm_pm_setup_saved_state);
1326
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001327static void setup_broadcast_timer(void *arg)
1328{
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001329 int cpu = smp_processor_id();
1330
Mahesh Sivasubramanian738eaf62013-04-30 12:42:59 -06001331 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ON, &cpu);
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001332}
1333
1334static int setup_broadcast_cpuhp_notify(struct notifier_block *n,
1335 unsigned long action, void *hcpu)
1336{
Mahesh Sivasubramanian738eaf62013-04-30 12:42:59 -06001337 int cpu = (unsigned long)hcpu;
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001338
1339 switch (action & ~CPU_TASKS_FROZEN) {
1340 case CPU_ONLINE:
Mahesh Sivasubramanian738eaf62013-04-30 12:42:59 -06001341 smp_call_function_single(cpu, setup_broadcast_timer, NULL, 1);
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001342 break;
1343 }
1344
1345 return NOTIFY_OK;
1346}
1347
1348static struct notifier_block setup_broadcast_notifier = {
1349 .notifier_call = setup_broadcast_cpuhp_notify,
1350};
1351
Praveen Chidambaramede45d02013-02-20 17:45:21 -07001352static int __init msm_pm_init(void)
1353{
1354 enum msm_pm_time_stats_id enable_stats[] = {
1355 MSM_PM_STAT_IDLE_WFI,
1356 MSM_PM_STAT_RETENTION,
1357 MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE,
1358 MSM_PM_STAT_IDLE_POWER_COLLAPSE,
1359 MSM_PM_STAT_SUSPEND,
1360 };
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001361 msm_pm_mode_sysfs_add();
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301362 msm_pm_add_stats(enable_stats, ARRAY_SIZE(enable_stats));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001363 suspend_set_ops(&msm_pm_ops);
Girish Mahadevan32493612013-05-10 14:37:17 -06001364 hrtimer_init(&pm_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001365 msm_cpuidle_init();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001366
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001367 if (msm_pm_pc_reset_timer) {
Mahesh Sivasubramanian738eaf62013-04-30 12:42:59 -06001368 on_each_cpu(setup_broadcast_timer, NULL, 1);
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001369 register_cpu_notifier(&setup_broadcast_notifier);
1370 }
1371
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001372 return 0;
1373}
1374
Girish Mahadevan55944992012-10-26 11:03:07 -06001375static void __devinit msm_pm_set_flush_fn(uint32_t pc_mode)
1376{
1377 msm_pm_disable_l2_fn = NULL;
1378 msm_pm_enable_l2_fn = NULL;
1379 msm_pm_flush_l2_fn = outer_flush_all;
1380
1381 if (pc_mode == MSM_PM_PC_NOTZ_L2_EXT) {
1382 msm_pm_disable_l2_fn = outer_disable;
1383 msm_pm_enable_l2_fn = outer_resume;
1384 }
1385}
1386
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001387struct msm_pc_debug_counters_buffer {
1388 void __iomem *reg;
1389 u32 len;
1390 char buf[MAX_BUF_SIZE];
1391};
1392
1393static inline u32 msm_pc_debug_counters_read_register(
1394 void __iomem *reg, int index , int offset)
1395{
1396 return readl_relaxed(reg + (index * 4 + offset) * 4);
1397}
1398
1399static char *counter_name[] = {
1400 "PC Entry Counter",
1401 "Warmboot Entry Counter",
1402 "PC Bailout Counter"
1403};
1404
1405static int msm_pc_debug_counters_copy(
1406 struct msm_pc_debug_counters_buffer *data)
1407{
1408 int j;
1409 u32 stat;
1410 unsigned int cpu;
1411
1412 for_each_possible_cpu(cpu) {
1413 data->len += scnprintf(data->buf + data->len,
1414 sizeof(data->buf)-data->len,
1415 "CPU%d\n", cpu);
1416
1417 for (j = 0; j < NUM_OF_COUNTERS; j++) {
1418 stat = msm_pc_debug_counters_read_register(
1419 data->reg, cpu, j);
1420 data->len += scnprintf(data->buf + data->len,
1421 sizeof(data->buf)-data->len,
1422 "\t%s : %d\n", counter_name[j],
1423 stat);
1424 }
1425
1426 }
1427
1428 return data->len;
1429}
1430
1431static int msm_pc_debug_counters_file_read(struct file *file,
1432 char __user *bufu, size_t count, loff_t *ppos)
1433{
1434 struct msm_pc_debug_counters_buffer *data;
1435
1436 data = file->private_data;
1437
1438 if (!data)
1439 return -EINVAL;
1440
Mahesh Sivasubramanian6900fdc2013-04-18 14:02:52 -06001441 if (!bufu)
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001442 return -EINVAL;
1443
1444 if (!access_ok(VERIFY_WRITE, bufu, count))
1445 return -EFAULT;
1446
1447 if (*ppos >= data->len && data->len == 0)
1448 data->len = msm_pc_debug_counters_copy(data);
1449
1450 return simple_read_from_buffer(bufu, count, ppos,
1451 data->buf, data->len);
1452}
1453
1454static int msm_pc_debug_counters_file_open(struct inode *inode,
1455 struct file *file)
1456{
1457 struct msm_pc_debug_counters_buffer *buf;
1458 void __iomem *msm_pc_debug_counters_reg;
1459
1460 msm_pc_debug_counters_reg = inode->i_private;
1461
1462 if (!msm_pc_debug_counters_reg)
1463 return -EINVAL;
1464
1465 file->private_data = kzalloc(
1466 sizeof(struct msm_pc_debug_counters_buffer), GFP_KERNEL);
1467
1468 if (!file->private_data) {
1469 pr_err("%s: ERROR kmalloc failed to allocate %d bytes\n",
1470 __func__, sizeof(struct msm_pc_debug_counters_buffer));
1471
1472 return -ENOMEM;
1473 }
1474
1475 buf = file->private_data;
1476 buf->reg = msm_pc_debug_counters_reg;
1477
1478 return 0;
1479}
1480
1481static int msm_pc_debug_counters_file_close(struct inode *inode,
1482 struct file *file)
1483{
1484 kfree(file->private_data);
1485 return 0;
1486}
1487
1488static const struct file_operations msm_pc_debug_counters_fops = {
1489 .open = msm_pc_debug_counters_file_open,
1490 .read = msm_pc_debug_counters_file_read,
1491 .release = msm_pc_debug_counters_file_close,
1492 .llseek = no_llseek,
1493};
1494
Girish Mahadevan55944992012-10-26 11:03:07 -06001495static int __devinit msm_pm_8x60_probe(struct platform_device *pdev)
1496{
1497 char *key = NULL;
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001498 struct dentry *dent = NULL;
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001499 struct resource *res = NULL;
1500 int i ;
1501 struct msm_pm_init_data_type pdata_local;
Girish Mahadevan55944992012-10-26 11:03:07 -06001502 int ret = 0;
1503
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001504 memset(&pdata_local, 0, sizeof(struct msm_pm_init_data_type));
1505
1506 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1507 if (res) {
1508 msm_pc_debug_counters_phys = res->start;
1509 WARN_ON(resource_size(res) < SZ_64);
1510 msm_pc_debug_counters = devm_ioremap(&pdev->dev, res->start,
1511 resource_size(res));
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001512 if (msm_pc_debug_counters)
1513 for (i = 0; i < resource_size(res)/4; i++)
1514 __raw_writel(0, msm_pc_debug_counters + i * 4);
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001515
1516 }
1517
1518 if (!msm_pc_debug_counters) {
1519 msm_pc_debug_counters = 0;
1520 msm_pc_debug_counters_phys = 0;
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001521 } else {
1522 dent = debugfs_create_file("pc_debug_counter", S_IRUGO, NULL,
1523 msm_pc_debug_counters,
1524 &msm_pc_debug_counters_fops);
1525 if (!dent)
1526 pr_err("%s: ERROR debugfs_create_file failed\n",
1527 __func__);
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001528 }
1529
Girish Mahadevan55944992012-10-26 11:03:07 -06001530 if (!pdev->dev.of_node) {
1531 struct msm_pm_init_data_type *d = pdev->dev.platform_data;
1532
1533 if (!d)
1534 goto pm_8x60_probe_done;
1535
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001536 memcpy(&pdata_local, d, sizeof(struct msm_pm_init_data_type));
1537
Girish Mahadevan55944992012-10-26 11:03:07 -06001538 } else {
1539 key = "qcom,pc-mode";
Archana Sathyakumar2b91dc82013-02-01 17:38:23 -07001540 ret = msm_pm_get_pc_mode(pdev->dev.of_node,
1541 key,
1542 &pdata_local.pc_mode);
Girish Mahadevan55944992012-10-26 11:03:07 -06001543 if (ret) {
Archana Sathyakumar2b91dc82013-02-01 17:38:23 -07001544 pr_debug("%s: Error reading key %s",
Girish Mahadevan55944992012-10-26 11:03:07 -06001545 __func__, key);
Archana Sathyakumar2b91dc82013-02-01 17:38:23 -07001546 return -EINVAL;
Girish Mahadevan55944992012-10-26 11:03:07 -06001547 }
Girish Mahadevan55944992012-10-26 11:03:07 -06001548
1549 key = "qcom,use-sync-timer";
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001550 pdata_local.use_sync_timer =
Girish Mahadevan55944992012-10-26 11:03:07 -06001551 of_property_read_bool(pdev->dev.of_node, key);
Praveen Chidambaram4b8df032013-01-18 16:21:16 -07001552
1553 key = "qcom,saw-turns-off-pll";
1554 msm_no_ramp_down_pc = of_property_read_bool(pdev->dev.of_node,
1555 key);
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001556
1557 key = "qcom,pc-resets-timer";
1558 msm_pm_pc_reset_timer = of_property_read_bool(
1559 pdev->dev.of_node, key);
Girish Mahadevan55944992012-10-26 11:03:07 -06001560 }
1561
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001562 if (pdata_local.cp15_data.reg_data &&
1563 pdata_local.cp15_data.reg_saved_state_size > 0) {
1564 cp15_data.reg_data = kzalloc(sizeof(uint32_t) *
1565 pdata_local.cp15_data.reg_saved_state_size,
1566 GFP_KERNEL);
1567 if (!cp15_data.reg_data)
1568 return -ENOMEM;
1569
1570 cp15_data.reg_val = kzalloc(sizeof(uint32_t) *
1571 pdata_local.cp15_data.reg_saved_state_size,
1572 GFP_KERNEL);
1573 if (cp15_data.reg_val)
1574 return -ENOMEM;
1575
1576 memcpy(cp15_data.reg_data, pdata_local.cp15_data.reg_data,
1577 pdata_local.cp15_data.reg_saved_state_size *
1578 sizeof(uint32_t));
1579 }
1580
1581 msm_pm_set_flush_fn(pdata_local.pc_mode);
1582 msm_pm_use_sync_timer = pdata_local.use_sync_timer;
1583 msm_pm_retention_calls_tz = pdata_local.retention_calls_tz;
1584
Girish Mahadevan55944992012-10-26 11:03:07 -06001585pm_8x60_probe_done:
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001586 msm_pm_init();
Anji Jonnala02dac8d2013-03-06 21:31:04 +05301587 if (pdev->dev.of_node)
1588 of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
1589
Girish Mahadevan55944992012-10-26 11:03:07 -06001590 return ret;
1591}
1592
1593static struct of_device_id msm_pm_8x60_table[] = {
1594 {.compatible = "qcom,pm-8x60"},
1595 {},
1596};
1597
1598static struct platform_driver msm_pm_8x60_driver = {
1599 .probe = msm_pm_8x60_probe,
1600 .driver = {
1601 .name = "pm-8x60",
1602 .owner = THIS_MODULE,
1603 .of_match_table = msm_pm_8x60_table,
1604 },
1605};
1606
1607static int __init msm_pm_8x60_init(void)
1608{
Anji Jonnala02dac8d2013-03-06 21:31:04 +05301609 int rc;
1610
1611 rc = platform_driver_register(&msm_cpu_status_driver);
1612
1613 if (rc) {
1614 pr_err("%s(): failed to register driver %s\n", __func__,
1615 msm_cpu_status_driver.driver.name);
1616 return rc;
1617 }
1618
Girish Mahadevanec50d2d2013-05-15 12:07:38 -06001619 rc = platform_driver_register(&msm_cpu_pm_snoc_client_driver);
1620
1621 if (rc) {
1622 pr_err("%s(): failed to register driver %s\n", __func__,
1623 msm_cpu_pm_snoc_client_driver.driver.name);
1624 return rc;
1625 }
1626
Badhri Jagan Sridharan9b186af2013-05-24 12:47:35 -07001627 pnoc_clk = clk_get_sys("pm_8x60", "bus_clk");
1628
1629 if (IS_ERR(pnoc_clk))
1630 pnoc_clk = NULL;
1631 else {
1632 clk_set_rate(pnoc_clk, 19200000);
1633 rc = clk_prepare_enable(pnoc_clk);
1634
1635 if (rc)
1636 pr_err("%s: PNOC clock enable failed\n", __func__);
1637 }
1638
Girish Mahadevan55944992012-10-26 11:03:07 -06001639 return platform_driver_register(&msm_pm_8x60_driver);
1640}
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001641device_initcall(msm_pm_8x60_init);