blob: c09d77c3fd7c7893685ed19a14df18dad7bf2a1b [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>
Venkat Devarasetty722f91f2013-06-03 19:41:27 +053062#include <linux/cpu_pm.h>
Hanumant Singhbd82a3e2013-02-26 13:51:17 -080063
Praveen Chidambaramf27a5152013-02-01 11:44:53 -070064#define SCM_L2_RETENTION (0x2)
65#define SCM_CMD_TERMINATE_PC (0x2)
66
67#define GET_CPU_OF_ATTR(attr) \
68 (container_of(attr, struct msm_pm_kobj_attribute, ka)->cpu)
69
70#define SCLK_HZ (32768)
Praveen Chidambaramf27a5152013-02-01 11:44:53 -070071
Priyanka Mathur13bdad12013-01-28 15:52:56 -080072#define NUM_OF_COUNTERS 3
73#define MAX_BUF_SIZE 512
74
Praveen Chidambaramf27a5152013-02-01 11:44:53 -070075static int msm_pm_debug_mask = 1;
76module_param_named(
77 debug_mask, msm_pm_debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP
78);
79
80static int msm_pm_sleep_time_override;
81module_param_named(sleep_time_override,
82 msm_pm_sleep_time_override, int, S_IRUGO | S_IWUSR | S_IWGRP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070083
84enum {
85 MSM_PM_DEBUG_SUSPEND = BIT(0),
86 MSM_PM_DEBUG_POWER_COLLAPSE = BIT(1),
87 MSM_PM_DEBUG_SUSPEND_LIMITS = BIT(2),
88 MSM_PM_DEBUG_CLOCK = BIT(3),
89 MSM_PM_DEBUG_RESET_VECTOR = BIT(4),
Karthik Parsha6fb932d2012-01-24 18:04:12 -080090 MSM_PM_DEBUG_IDLE_CLK = BIT(5),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070091 MSM_PM_DEBUG_IDLE = BIT(6),
92 MSM_PM_DEBUG_IDLE_LIMITS = BIT(7),
93 MSM_PM_DEBUG_HOTPLUG = BIT(8),
94};
95
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070096enum {
97 MSM_PM_MODE_ATTR_SUSPEND,
98 MSM_PM_MODE_ATTR_IDLE,
99 MSM_PM_MODE_ATTR_NR,
100};
101
102static char *msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_NR] = {
103 [MSM_PM_MODE_ATTR_SUSPEND] = "suspend_enabled",
104 [MSM_PM_MODE_ATTR_IDLE] = "idle_enabled",
105};
106
107struct msm_pm_kobj_attribute {
108 unsigned int cpu;
109 struct kobj_attribute ka;
110};
111
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700112struct msm_pm_sysfs_sleep_mode {
113 struct kobject *kobj;
114 struct attribute_group attr_group;
115 struct attribute *attrs[MSM_PM_MODE_ATTR_NR + 1];
116 struct msm_pm_kobj_attribute kas[MSM_PM_MODE_ATTR_NR];
117};
118
119static char *msm_pm_sleep_mode_labels[MSM_PM_SLEEP_MODE_NR] = {
120 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE] = "power_collapse",
121 [MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT] = "wfi",
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600122 [MSM_PM_SLEEP_MODE_RETENTION] = "retention",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700123 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE] =
124 "standalone_power_collapse",
125};
126
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600127static struct hrtimer pm_hrtimer;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600128static struct msm_pm_sleep_ops pm_sleep_ops;
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -0700129static bool msm_pm_ldo_retention_enabled = true;
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700130static bool msm_pm_use_sync_timer;
131static struct msm_pm_cp15_save_data cp15_data;
132static bool msm_pm_retention_calls_tz;
Praveen Chidambaram4b8df032013-01-18 16:21:16 -0700133static bool msm_no_ramp_down_pc;
Anji Jonnala02dac8d2013-03-06 21:31:04 +0530134static struct msm_pm_sleep_status_data *msm_pm_slp_sts;
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -0600135static bool msm_pm_pc_reset_timer;
Badhri Jagan Sridharan9b186af2013-05-24 12:47:35 -0700136static struct clk *pnoc_clk;
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700137
Archana Sathyakumar2b91dc82013-02-01 17:38:23 -0700138static int msm_pm_get_pc_mode(struct device_node *node,
139 const char *key, uint32_t *pc_mode_val)
140{
141 struct pc_mode_of {
142 uint32_t mode;
143 char *mode_name;
144 };
145 int i;
146 struct pc_mode_of pc_modes[] = {
147 {MSM_PM_PC_TZ_L2_INT, "tz_l2_int"},
148 {MSM_PM_PC_NOTZ_L2_EXT, "no_tz_l2_ext"},
149 {MSM_PM_PC_TZ_L2_EXT , "tz_l2_ext"} };
150 int ret;
151 const char *pc_mode_str;
152
153 ret = of_property_read_string(node, key, &pc_mode_str);
154 if (ret) {
155 pr_debug("%s: Cannot read %s,defaulting to 0", __func__, key);
156 pc_mode_val = MSM_PM_PC_TZ_L2_INT;
157 ret = 0;
158 } else {
159 ret = -EINVAL;
160 for (i = 0; i < ARRAY_SIZE(pc_modes); i++) {
161 if (!strncmp(pc_mode_str, pc_modes[i].mode_name,
162 strlen(pc_modes[i].mode_name))) {
163 *pc_mode_val = pc_modes[i].mode;
164 ret = 0;
165 break;
166 }
167 }
168 }
169 return ret;
170}
171
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700172/*
173 * Write out the attribute.
174 */
175static ssize_t msm_pm_mode_attr_show(
176 struct kobject *kobj, struct kobj_attribute *attr, char *buf)
177{
178 int ret = -EINVAL;
179 int i;
180
181 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
182 struct kernel_param kp;
183 unsigned int cpu;
184 struct msm_pm_platform_data *mode;
185
186 if (msm_pm_sleep_mode_labels[i] == NULL)
187 continue;
188
189 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
190 continue;
191
192 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600193 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700194
195 if (!strcmp(attr->attr.name,
196 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
197 u32 arg = mode->suspend_enabled;
198 kp.arg = &arg;
199 ret = param_get_ulong(buf, &kp);
200 } else if (!strcmp(attr->attr.name,
201 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
202 u32 arg = mode->idle_enabled;
203 kp.arg = &arg;
204 ret = param_get_ulong(buf, &kp);
205 }
206
207 break;
208 }
209
210 if (ret > 0) {
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600211 strlcat(buf, "\n", PAGE_SIZE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700212 ret++;
213 }
214
215 return ret;
216}
217
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700218static ssize_t msm_pm_mode_attr_store(struct kobject *kobj,
219 struct kobj_attribute *attr, const char *buf, size_t count)
220{
221 int ret = -EINVAL;
222 int i;
223
224 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
225 struct kernel_param kp;
226 unsigned int cpu;
227 struct msm_pm_platform_data *mode;
228
229 if (msm_pm_sleep_mode_labels[i] == NULL)
230 continue;
231
232 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
233 continue;
234
235 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600236 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700237
238 if (!strcmp(attr->attr.name,
239 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
240 kp.arg = &mode->suspend_enabled;
241 ret = param_set_byte(buf, &kp);
242 } else if (!strcmp(attr->attr.name,
243 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
244 kp.arg = &mode->idle_enabled;
245 ret = param_set_byte(buf, &kp);
246 }
247
248 break;
249 }
250
251 return ret ? ret : count;
252}
253
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700254static int __devinit msm_pm_mode_sysfs_add_cpu(
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700255 unsigned int cpu, struct kobject *modes_kobj)
256{
257 char cpu_name[8];
258 struct kobject *cpu_kobj;
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600259 struct msm_pm_sysfs_sleep_mode *mode = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700260 int i, j, k;
261 int ret;
262
263 snprintf(cpu_name, sizeof(cpu_name), "cpu%u", cpu);
264 cpu_kobj = kobject_create_and_add(cpu_name, modes_kobj);
265 if (!cpu_kobj) {
266 pr_err("%s: cannot create %s kobject\n", __func__, cpu_name);
267 ret = -ENOMEM;
268 goto mode_sysfs_add_cpu_exit;
269 }
270
271 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
272 int idx = MSM_PM_MODE(cpu, i);
273
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600274 if ((!msm_pm_sleep_modes[idx].suspend_supported)
275 && (!msm_pm_sleep_modes[idx].idle_supported))
276 continue;
277
278 if (!msm_pm_sleep_mode_labels[i] ||
279 !msm_pm_sleep_mode_labels[i][0])
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700280 continue;
281
282 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
283 if (!mode) {
284 pr_err("%s: cannot allocate memory for attributes\n",
285 __func__);
286 ret = -ENOMEM;
287 goto mode_sysfs_add_cpu_exit;
288 }
289
290 mode->kobj = kobject_create_and_add(
291 msm_pm_sleep_mode_labels[i], cpu_kobj);
292 if (!mode->kobj) {
293 pr_err("%s: cannot create kobject\n", __func__);
294 ret = -ENOMEM;
295 goto mode_sysfs_add_cpu_exit;
296 }
297
298 for (k = 0, j = 0; k < MSM_PM_MODE_ATTR_NR; k++) {
299 if ((k == MSM_PM_MODE_ATTR_IDLE) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600300 !msm_pm_sleep_modes[idx].idle_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700301 continue;
302 if ((k == MSM_PM_MODE_ATTR_SUSPEND) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600303 !msm_pm_sleep_modes[idx].suspend_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700304 continue;
Stephen Boydd2059c32012-07-03 15:11:15 -0700305 sysfs_attr_init(&mode->kas[j].ka.attr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700306 mode->kas[j].cpu = cpu;
307 mode->kas[j].ka.attr.mode = 0644;
308 mode->kas[j].ka.show = msm_pm_mode_attr_show;
309 mode->kas[j].ka.store = msm_pm_mode_attr_store;
310 mode->kas[j].ka.attr.name = msm_pm_mode_attr_labels[k];
311 mode->attrs[j] = &mode->kas[j].ka.attr;
312 j++;
313 }
314 mode->attrs[j] = NULL;
315
316 mode->attr_group.attrs = mode->attrs;
317 ret = sysfs_create_group(mode->kobj, &mode->attr_group);
318 if (ret) {
319 pr_err("%s: cannot create kobject attribute group\n",
320 __func__);
321 goto mode_sysfs_add_cpu_exit;
322 }
323 }
324
325 ret = 0;
326
327mode_sysfs_add_cpu_exit:
Praveen Chidambaramd5ac2d32011-10-24 14:30:27 -0600328 if (ret) {
Praveen Chidambaram2cfda632011-10-11 16:58:09 -0600329 if (mode && mode->kobj)
330 kobject_del(mode->kobj);
331 kfree(mode);
332 }
333
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700334 return ret;
335}
336
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700337int __devinit msm_pm_mode_sysfs_add(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700338{
339 struct kobject *module_kobj;
340 struct kobject *modes_kobj;
341 unsigned int cpu;
342 int ret;
343
344 module_kobj = kset_find_obj(module_kset, KBUILD_MODNAME);
345 if (!module_kobj) {
346 pr_err("%s: cannot find kobject for module %s\n",
347 __func__, KBUILD_MODNAME);
348 ret = -ENOENT;
349 goto mode_sysfs_add_exit;
350 }
351
352 modes_kobj = kobject_create_and_add("modes", module_kobj);
353 if (!modes_kobj) {
354 pr_err("%s: cannot create modes kobject\n", __func__);
355 ret = -ENOMEM;
356 goto mode_sysfs_add_exit;
357 }
358
359 for_each_possible_cpu(cpu) {
360 ret = msm_pm_mode_sysfs_add_cpu(cpu, modes_kobj);
361 if (ret)
362 goto mode_sysfs_add_exit;
363 }
364
365 ret = 0;
366
367mode_sysfs_add_exit:
368 return ret;
369}
370
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700371/*
372 * Configure hardware registers in preparation for Apps power down.
373 */
374static void msm_pm_config_hw_before_power_down(void)
375{
376 return;
377}
378
379/*
380 * Clear hardware registers after Apps powers up.
381 */
382static void msm_pm_config_hw_after_power_up(void)
383{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700384}
385
386/*
387 * Configure hardware registers in preparation for SWFI.
388 */
389static void msm_pm_config_hw_before_swfi(void)
390{
391 return;
392}
393
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600394/*
395 * Configure/Restore hardware registers in preparation for Retention.
396 */
397
398static void msm_pm_config_hw_after_retention(void)
399{
400 int ret;
Abhijeet Dharmapurikare8f83852013-02-06 18:57:17 -0800401
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600402 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
403 WARN_ON(ret);
404}
405
406static void msm_pm_config_hw_before_retention(void)
407{
408 return;
409}
410
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600411static void msm_pm_save_cpu_reg(void)
412{
413 int i;
414
415 /* Only on core0 */
416 if (smp_processor_id())
417 return;
418
419 /**
420 * On some targets, L2 PC will turn off may reset the core
421 * configuration for the mux and the default may not make the core
422 * happy when it resumes.
423 * Save the active vdd, and set the core vdd to QSB max vdd, so that
424 * when the core resumes, it is capable of supporting the current QSB
425 * rate. Then restore the active vdd before switching the acpuclk rate.
426 */
427 if (msm_pm_get_l2_flush_flag() == 1) {
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700428 cp15_data.active_vdd = msm_spm_get_vdd(0);
429 for (i = 0; i < cp15_data.reg_saved_state_size; i++)
430 cp15_data.reg_val[i] =
431 get_l2_indirect_reg(
432 cp15_data.reg_data[i]);
433 msm_spm_set_vdd(0, cp15_data.qsb_pc_vdd);
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600434 }
435}
436
437static void msm_pm_restore_cpu_reg(void)
438{
439 int i;
440
441 /* Only on core0 */
442 if (smp_processor_id())
443 return;
444
445 if (msm_pm_get_l2_flush_flag() == 1) {
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700446 for (i = 0; i < cp15_data.reg_saved_state_size; i++)
447 set_l2_indirect_reg(
448 cp15_data.reg_data[i],
449 cp15_data.reg_val[i]);
450 msm_spm_set_vdd(0, cp15_data.active_vdd);
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600451 }
452}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700453
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700454static void msm_pm_swfi(void)
455{
456 msm_pm_config_hw_before_swfi();
457 msm_arch_idle();
458}
459
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600460static void msm_pm_retention(void)
461{
462 int ret = 0;
463
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600464 msm_pm_config_hw_before_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600465 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_POWER_RETENTION, false);
466 WARN_ON(ret);
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600467
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700468 if (msm_pm_retention_calls_tz)
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600469 scm_call_atomic1(SCM_SVC_BOOT, SCM_CMD_TERMINATE_PC,
470 SCM_L2_RETENTION);
471 else
472 msm_arch_idle();
473
474 msm_pm_config_hw_after_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600475}
476
Stephen Boydb29750d2012-02-21 01:21:32 -0800477static bool __ref msm_pm_spm_power_collapse(
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700478 unsigned int cpu, bool from_idle, bool notify_rpm)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700479{
480 void *entry;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600481 bool collapsed = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700482 int ret;
Mahesh Sivasubramanianda746252013-05-02 09:58:08 -0600483 bool save_cpu_regs = !cpu || from_idle;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700484
485 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
486 pr_info("CPU%u: %s: notify_rpm %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700487 cpu, __func__, (int) notify_rpm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700488
Venkat Devarasetty722f91f2013-06-03 19:41:27 +0530489 if (from_idle == true)
490 cpu_pm_enter();
491
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700492 ret = msm_spm_set_low_power_mode(
493 MSM_SPM_MODE_POWER_COLLAPSE, notify_rpm);
494 WARN_ON(ret);
495
Mahesh Sivasubramanianda746252013-05-02 09:58:08 -0600496 entry = save_cpu_regs ? msm_pm_collapse_exit : msm_secondary_startup;
497
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700498 msm_pm_boot_config_before_pc(cpu, virt_to_phys(entry));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700499
500 if (MSM_PM_DEBUG_RESET_VECTOR & msm_pm_debug_mask)
501 pr_info("CPU%u: %s: program vector to %p\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700502 cpu, __func__, entry);
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -0600503 if (from_idle && msm_pm_pc_reset_timer)
504 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700505
Mahesh Sivasubramanianda746252013-05-02 09:58:08 -0600506 collapsed = save_cpu_regs ? msm_pm_collapse() : msm_pm_pc_hotplug();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700507
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -0600508 if (from_idle && msm_pm_pc_reset_timer)
509 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu);
510
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700511 msm_pm_boot_config_after_pc(cpu);
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -0600512
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700513 if (collapsed) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700514 cpu_init();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700515 local_fiq_enable();
516 }
517
518 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
519 pr_info("CPU%u: %s: msm_pm_collapse returned, collapsed %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700520 cpu, __func__, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700521
522 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
523 WARN_ON(ret);
Venkat Devarasetty722f91f2013-06-03 19:41:27 +0530524
525 if (from_idle == true)
526 cpu_pm_exit();
527
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600528 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700529}
530
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600531static bool msm_pm_power_collapse_standalone(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700532{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700533 unsigned int cpu = smp_processor_id();
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700534 unsigned int avsdscr;
535 unsigned int avscsr;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600536 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700537
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700538 avsdscr = avs_get_avsdscr();
539 avscsr = avs_get_avscsr();
540 avs_set_avscsr(0); /* Disable AVS */
541
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700542 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, false);
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700543
544 avs_set_avsdscr(avsdscr);
545 avs_set_avscsr(avscsr);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600546 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700547}
548
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600549static bool msm_pm_power_collapse(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700550{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700551 unsigned int cpu = smp_processor_id();
Praveen Chidambaram4b8df032013-01-18 16:21:16 -0700552 unsigned long saved_acpuclk_rate = 0;
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700553 unsigned int avsdscr;
554 unsigned int avscsr;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600555 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700556
557 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
558 pr_info("CPU%u: %s: idle %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700559 cpu, __func__, (int)from_idle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700560
561 msm_pm_config_hw_before_power_down();
562 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700563 pr_info("CPU%u: %s: pre power down\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700564
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700565 avsdscr = avs_get_avsdscr();
566 avscsr = avs_get_avscsr();
567 avs_set_avscsr(0); /* Disable AVS */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700568
Praveen Chidambaram4b8df032013-01-18 16:21:16 -0700569 if (cpu_online(cpu) && !msm_no_ramp_down_pc)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700570 saved_acpuclk_rate = acpuclk_power_collapse();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700571
572 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
573 pr_info("CPU%u: %s: change clock rate (old rate = %lu)\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700574 cpu, __func__, saved_acpuclk_rate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700575
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700576 if (cp15_data.save_cp15)
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600577 msm_pm_save_cpu_reg();
578
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700579 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700580
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700581 if (cp15_data.save_cp15)
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600582 msm_pm_restore_cpu_reg();
583
Girish Mahadevan4e025d92012-02-29 13:26:51 -0700584 if (cpu_online(cpu)) {
585 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
586 pr_info("CPU%u: %s: restore clock rate to %lu\n",
587 cpu, __func__, saved_acpuclk_rate);
Praveen Chidambaram4b8df032013-01-18 16:21:16 -0700588 if (!msm_no_ramp_down_pc &&
589 acpuclk_set_rate(cpu, saved_acpuclk_rate, SETRATE_PC)
590 < 0)
Girish Mahadevan4e025d92012-02-29 13:26:51 -0700591 pr_err("CPU%u: %s: failed to restore clock rate(%lu)\n",
592 cpu, __func__, saved_acpuclk_rate);
593 } else {
594 unsigned int gic_dist_enabled;
595 unsigned int gic_dist_pending;
596 gic_dist_enabled = readl_relaxed(
597 MSM_QGIC_DIST_BASE + GIC_DIST_ENABLE_CLEAR);
598 gic_dist_pending = readl_relaxed(
599 MSM_QGIC_DIST_BASE + GIC_DIST_PENDING_SET);
600 mb();
601 gic_dist_pending &= gic_dist_enabled;
602
603 if (gic_dist_pending)
604 pr_err("CPU %d interrupted during hotplug.Pending int 0x%x\n",
605 cpu, gic_dist_pending);
606 }
607
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700608
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700609 avs_set_avsdscr(avsdscr);
610 avs_set_avscsr(avscsr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700611 msm_pm_config_hw_after_power_up();
612 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700613 pr_info("CPU%u: %s: post power up\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700614
615 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700616 pr_info("CPU%u: %s: return\n", cpu, __func__);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600617 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700618}
619
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600620static int64_t msm_pm_timer_enter_idle(void)
621{
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700622 if (msm_pm_use_sync_timer)
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600623 return ktime_to_ns(tick_nohz_get_sleep_length());
624
625 return msm_timer_enter_idle();
626}
627
628static void msm_pm_timer_exit_idle(bool timer_halted)
629{
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700630 if (msm_pm_use_sync_timer)
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600631 return;
632
633 msm_timer_exit_idle((int) timer_halted);
634}
635
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530636static int64_t msm_pm_timer_enter_suspend(int64_t *period)
637{
Anji Jonnalac02367a2012-07-01 02:56:11 +0530638 int64_t time = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530639
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700640 if (msm_pm_use_sync_timer)
Mahesh Sivasubramanian0eaeb302013-03-14 14:56:32 -0600641 return sched_clock();
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530642
643 time = msm_timer_get_sclk_time(period);
644 if (!time)
645 pr_err("%s: Unable to read sclk.\n", __func__);
646
647 return time;
648}
649
650static int64_t msm_pm_timer_exit_suspend(int64_t time, int64_t period)
651{
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700652 if (msm_pm_use_sync_timer)
Mahesh Sivasubramanian0eaeb302013-03-14 14:56:32 -0600653 return sched_clock() - time;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530654
655 if (time != 0) {
656 int64_t end_time = msm_timer_get_sclk_time(NULL);
657 if (end_time != 0) {
658 time = end_time - time;
659 if (time < 0)
660 time += period;
661 } else
662 time = 0;
663 }
664
665 return time;
666}
667
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600668/**
669 * pm_hrtimer_cb() : Callback function for hrtimer created if the
670 * core needs to be awake to handle an event.
671 * @hrtimer : Pointer to hrtimer
672 */
673static enum hrtimer_restart pm_hrtimer_cb(struct hrtimer *hrtimer)
674{
675 return HRTIMER_NORESTART;
676}
677
678/**
679 * msm_pm_set_timer() : Set an hrtimer to wakeup the core in time
680 * to handle an event.
681 */
682static void msm_pm_set_timer(uint32_t modified_time_us)
683{
684 u64 modified_time_ns = modified_time_us * NSEC_PER_USEC;
685 ktime_t modified_ktime = ns_to_ktime(modified_time_ns);
686 pm_hrtimer.function = pm_hrtimer_cb;
Girish Mahadevan32493612013-05-10 14:37:17 -0600687 hrtimer_start(&pm_hrtimer, modified_ktime, HRTIMER_MODE_REL);
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600688}
689
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700690/******************************************************************************
691 * External Idle/Suspend Functions
692 *****************************************************************************/
693
694void arch_idle(void)
695{
696 return;
697}
698
Priyanka Mathur26b4a4b2012-11-05 13:45:45 -0800699static inline void msm_pm_ftrace_lpm_enter(unsigned int cpu,
700 uint32_t latency, uint32_t sleep_us,
701 uint32_t wake_up,
702 enum msm_pm_sleep_mode mode)
703{
704 switch (mode) {
705 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
706 trace_msm_pm_enter_wfi(cpu, latency, sleep_us, wake_up);
707 break;
708 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
709 trace_msm_pm_enter_spc(cpu, latency, sleep_us, wake_up);
710 break;
711 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
712 trace_msm_pm_enter_pc(cpu, latency, sleep_us, wake_up);
713 break;
714 case MSM_PM_SLEEP_MODE_RETENTION:
715 trace_msm_pm_enter_ret(cpu, latency, sleep_us, wake_up);
716 break;
717 default:
718 break;
719 }
720}
721
722static inline void msm_pm_ftrace_lpm_exit(unsigned int cpu,
723 enum msm_pm_sleep_mode mode,
724 bool success)
725{
726 switch (mode) {
727 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
728 trace_msm_pm_exit_wfi(cpu, success);
729 break;
730 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
731 trace_msm_pm_exit_spc(cpu, success);
732 break;
733 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
734 trace_msm_pm_exit_pc(cpu, success);
735 break;
736 case MSM_PM_SLEEP_MODE_RETENTION:
737 trace_msm_pm_exit_ret(cpu, success);
738 break;
739 default:
740 break;
741 }
742}
743
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800744static int msm_pm_idle_prepare(struct cpuidle_device *dev,
745 struct cpuidle_driver *drv, int index,
746 void **msm_pm_idle_rs_limits)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700747{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700748 int i;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700749 unsigned int power_usage = -1;
Priyanka Mathur848bb4c2012-11-30 18:04:57 -0800750 int ret = MSM_PM_SLEEP_MODE_NOT_SELECTED;
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600751 uint32_t modified_time_us = 0;
752 struct msm_pm_time_params time_param;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700753
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600754 time_param.latency_us =
755 (uint32_t) pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
756 time_param.sleep_us =
757 (uint32_t) (ktime_to_us(tick_nohz_get_sleep_length())
758 & UINT_MAX);
759 time_param.modified_time_us = 0;
760
761 if (!dev->cpu)
762 time_param.next_event_us =
763 (uint32_t) (ktime_to_us(get_next_event_time())
764 & UINT_MAX);
765 else
766 time_param.next_event_us = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700767
768 for (i = 0; i < dev->state_count; i++) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700769 struct cpuidle_state *state = &drv->states[i];
770 struct cpuidle_state_usage *st_usage = &dev->states_usage[i];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700771 enum msm_pm_sleep_mode mode;
772 bool allow;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600773 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700774 int idx;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800775 void *rs_limits = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700776
Steve Mucklef132c6c2012-06-06 18:30:57 -0700777 mode = (enum msm_pm_sleep_mode) cpuidle_get_statedata(st_usage);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700778 idx = MSM_PM_MODE(dev->cpu, mode);
779
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600780 allow = msm_pm_sleep_modes[idx].idle_enabled &&
781 msm_pm_sleep_modes[idx].idle_supported;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700782
783 switch (mode) {
784 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700785 if (num_online_cpus() > 1)
Priyanka Mathur68a9bc52012-10-03 15:15:01 -0700786 allow = false;
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700787 break;
Mahesh Sivasubramanianba7e01c2012-09-04 18:22:51 -0600788 case MSM_PM_SLEEP_MODE_RETENTION:
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -0700789 /*
790 * The Krait BHS regulator doesn't have enough head
791 * room to drive the retention voltage on LDO and so
792 * has disabled retention
793 */
794 if (!msm_pm_ldo_retention_enabled)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700795 allow = false;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700796
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700797 if (msm_pm_retention_calls_tz && num_online_cpus() > 1)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700798 allow = false;
799 break;
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700800 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
801 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
802 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700803 default:
804 allow = false;
805 break;
806 }
807
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700808 if (!allow)
809 continue;
810
811 if (pm_sleep_ops.lowest_limits)
812 rs_limits = pm_sleep_ops.lowest_limits(true,
813 mode, &time_param, &power);
814
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700815 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700816 pr_info("CPU%u:%s:%s, latency %uus, slp %uus, lim %p\n",
817 dev->cpu, __func__, state->desc,
818 time_param.latency_us,
819 time_param.sleep_us, rs_limits);
820 if (!rs_limits)
821 continue;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700822
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700823 if (power < power_usage) {
824 power_usage = power;
825 modified_time_us = time_param.modified_time_us;
826 ret = mode;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800827 *msm_pm_idle_rs_limits = rs_limits;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700828 }
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700829
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700830 }
831
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600832 if (modified_time_us && !dev->cpu)
833 msm_pm_set_timer(modified_time_us);
Priyanka Mathur26b4a4b2012-11-05 13:45:45 -0800834
835 msm_pm_ftrace_lpm_enter(dev->cpu, time_param.latency_us,
836 time_param.sleep_us, time_param.next_event_us,
837 ret);
838
Steve Mucklef132c6c2012-06-06 18:30:57 -0700839 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700840}
841
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800842enum msm_pm_sleep_mode msm_pm_idle_enter(struct cpuidle_device *dev,
843 struct cpuidle_driver *drv, int index)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700844{
845 int64_t time;
Priyanka Mathur26b4a4b2012-11-05 13:45:45 -0800846 bool collapsed = 1;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800847 int exit_stat = -1;
848 enum msm_pm_sleep_mode sleep_mode;
849 void *msm_pm_idle_rs_limits = NULL;
Mahesh Sivasubramanian03698762013-04-18 16:18:46 -0600850 uint32_t sleep_delay = 1;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800851 int ret = -ENODEV;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800852 int notify_rpm = false;
853 bool timer_halted = false;
854
855 sleep_mode = msm_pm_idle_prepare(dev, drv, index,
856 &msm_pm_idle_rs_limits);
857
858 if (!msm_pm_idle_rs_limits) {
859 sleep_mode = MSM_PM_SLEEP_MODE_NOT_SELECTED;
860 goto cpuidle_enter_bail;
861 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700862
863 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
864 pr_info("CPU%u: %s: mode %d\n",
865 smp_processor_id(), __func__, sleep_mode);
866
867 time = ktime_to_ns(ktime_get());
868
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800869 if (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE) {
Mahesh Sivasubramanian2f415382013-05-17 16:29:05 -0600870 int64_t ns = msm_pm_timer_enter_idle();
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800871 notify_rpm = true;
Mahesh Sivasubramanian2f415382013-05-17 16:29:05 -0600872 do_div(ns, NSEC_PER_SEC / SCLK_HZ);
873 sleep_delay = (uint32_t)ns;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800874
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800875 if (sleep_delay == 0) /* 0 would mean infinite time */
876 sleep_delay = 1;
877 }
878
879 if (pm_sleep_ops.enter_sleep)
880 ret = pm_sleep_ops.enter_sleep(sleep_delay,
881 msm_pm_idle_rs_limits, true, notify_rpm);
882 if (ret)
883 goto cpuidle_enter_bail;
884
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700885 switch (sleep_mode) {
886 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
887 msm_pm_swfi();
888 exit_stat = MSM_PM_STAT_IDLE_WFI;
889 break;
890
891 case MSM_PM_SLEEP_MODE_RETENTION:
892 msm_pm_retention();
893 exit_stat = MSM_PM_STAT_RETENTION;
894 break;
895
896 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
897 collapsed = msm_pm_power_collapse_standalone(true);
Venkat Devarasetty849f47f2013-05-19 17:44:18 +0530898 if (collapsed)
899 exit_stat = MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE;
900 else
901 exit_stat
902 = MSM_PM_STAT_IDLE_FAILED_STANDALONE_POWER_COLLAPSE;
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700903 break;
904
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800905 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700906 if (MSM_PM_DEBUG_IDLE_CLK & msm_pm_debug_mask)
907 clock_debug_print_enabled();
Karthik Parsha6fb932d2012-01-24 18:04:12 -0800908
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800909 collapsed = msm_pm_power_collapse(true);
910 timer_halted = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700911
Venkat Devarasetty849f47f2013-05-19 17:44:18 +0530912 if (collapsed)
913 exit_stat = MSM_PM_STAT_IDLE_POWER_COLLAPSE;
914 else
915 exit_stat = MSM_PM_STAT_IDLE_FAILED_POWER_COLLAPSE;
916
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800917 msm_pm_timer_exit_idle(timer_halted);
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700918 break;
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700919
920 case MSM_PM_SLEEP_MODE_NOT_SELECTED:
921 goto cpuidle_enter_bail;
922 break;
923
924 default:
925 __WARN();
926 goto cpuidle_enter_bail;
927 break;
928 }
929
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800930 if (pm_sleep_ops.exit_sleep)
931 pm_sleep_ops.exit_sleep(msm_pm_idle_rs_limits, true,
932 notify_rpm, collapsed);
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700933
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800934 time = ktime_to_ns(ktime_get()) - time;
935 msm_pm_ftrace_lpm_exit(smp_processor_id(), sleep_mode, collapsed);
936 if (exit_stat >= 0)
937 msm_pm_add_stat(exit_stat, time);
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700938 do_div(time, 1000);
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800939 dev->last_residency = (int) time;
940 return sleep_mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700941
942cpuidle_enter_bail:
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800943 dev->last_residency = 0;
944 if (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE)
945 msm_pm_timer_exit_idle(timer_halted);
946 sleep_mode = MSM_PM_SLEEP_MODE_NOT_SELECTED;
947 return sleep_mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700948}
949
Anji Jonnala02dac8d2013-03-06 21:31:04 +0530950int msm_pm_wait_cpu_shutdown(unsigned int cpu)
951{
Mahesh Sivasubramanian32b9d422013-05-20 13:50:16 -0600952 int timeout = 0;
Anji Jonnala02dac8d2013-03-06 21:31:04 +0530953
954 if (!msm_pm_slp_sts)
955 return 0;
956 if (!msm_pm_slp_sts[cpu].base_addr)
957 return 0;
Mahesh Sivasubramanian32b9d422013-05-20 13:50:16 -0600958 while (1) {
Anji Jonnala02dac8d2013-03-06 21:31:04 +0530959 /*
960 * Check for the SPM of the core being hotplugged to set
961 * its sleep state.The SPM sleep state indicates that the
962 * core has been power collapsed.
963 */
964 int acc_sts = __raw_readl(msm_pm_slp_sts[cpu].base_addr);
965
966 if (acc_sts & msm_pm_slp_sts[cpu].mask)
967 return 0;
Mahesh Sivasubramanian65187f92013-03-19 15:14:06 -0600968 udelay(100);
Mahesh Sivasubramanian32b9d422013-05-20 13:50:16 -0600969 WARN(++timeout == 10, "CPU%u didn't collape within 1ms\n",
970 cpu);
Anji Jonnala02dac8d2013-03-06 21:31:04 +0530971 }
972
Anji Jonnala02dac8d2013-03-06 21:31:04 +0530973 return -EBUSY;
974}
975
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700976void msm_pm_cpu_enter_lowpower(unsigned int cpu)
977{
978 int i;
979 bool allow[MSM_PM_SLEEP_MODE_NR];
980
981 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
982 struct msm_pm_platform_data *mode;
983
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600984 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700985 allow[i] = mode->suspend_supported && mode->suspend_enabled;
986 }
987
988 if (MSM_PM_DEBUG_HOTPLUG & msm_pm_debug_mask)
989 pr_notice("CPU%u: %s: shutting down cpu\n", cpu, __func__);
990
Matt Wagantall5375aa72012-07-02 19:59:51 -0700991 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700992 msm_pm_power_collapse(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -0700993 else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700994 msm_pm_power_collapse_standalone(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -0700995 else if (allow[MSM_PM_SLEEP_MODE_RETENTION])
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600996 msm_pm_retention();
Stephen Boydbda74272012-08-09 14:01:27 -0700997 else
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700998 msm_pm_swfi();
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700999}
1000
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -07001001static void msm_pm_ack_retention_disable(void *data)
1002{
1003 /*
1004 * This is a NULL function to ensure that the core has woken up
1005 * and is safe to disable retention.
1006 */
1007}
1008/**
1009 * msm_pm_enable_retention() - Disable/Enable retention on all cores
1010 * @enable: Enable/Disable retention
1011 *
1012 */
1013void msm_pm_enable_retention(bool enable)
1014{
Matt Wagantalld06f5c22013-03-14 19:31:14 -07001015 if (enable == msm_pm_ldo_retention_enabled)
1016 return;
1017
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -07001018 msm_pm_ldo_retention_enabled = enable;
1019 /*
1020 * If retention is being disabled, wakeup all online core to ensure
1021 * that it isn't executing retention. Offlined cores need not be woken
1022 * up as they enter the deepest sleep mode, namely RPM assited power
1023 * collapse
1024 */
Mahesh Sivasubramanian96f67d12013-03-14 22:44:42 -06001025 if (!enable) {
1026 preempt_disable();
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -07001027 smp_call_function_many(cpu_online_mask,
1028 msm_pm_ack_retention_disable,
1029 NULL, true);
Mahesh Sivasubramanian96f67d12013-03-14 22:44:42 -06001030 preempt_enable();
1031
1032
1033 }
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -07001034}
1035EXPORT_SYMBOL(msm_pm_enable_retention);
1036
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001037static int msm_pm_enter(suspend_state_t state)
1038{
1039 bool allow[MSM_PM_SLEEP_MODE_NR];
1040 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001041 int64_t period = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301042 int64_t time = msm_pm_timer_enter_suspend(&period);
Girish Mahadevandc318fd2012-08-17 16:48:05 -06001043 struct msm_pm_time_params time_param;
1044
1045 time_param.latency_us = -1;
1046 time_param.sleep_us = -1;
1047 time_param.next_event_us = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001048
1049 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1050 pr_info("%s\n", __func__);
1051
1052 if (smp_processor_id()) {
1053 __WARN();
1054 goto enter_exit;
1055 }
1056
1057
1058 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
1059 struct msm_pm_platform_data *mode;
1060
Praveen Chidambaram42da9d22012-03-30 12:16:34 -06001061 mode = &msm_pm_sleep_modes[MSM_PM_MODE(0, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001062 allow[i] = mode->suspend_supported && mode->suspend_enabled;
1063 }
1064
1065 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001066 void *rs_limits = NULL;
1067 int ret = -ENODEV;
1068 uint32_t power;
Mahesh Sivasubramanian03698762013-04-18 16:18:46 -06001069 uint32_t msm_pm_max_sleep_time = 0;
Venkat Devarasetty849f47f2013-05-19 17:44:18 +05301070 int collapsed = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001071
1072 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1073 pr_info("%s: power collapse\n", __func__);
1074
1075 clock_debug_print_enabled();
1076
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001077 if (msm_pm_sleep_time_override > 0) {
1078 int64_t ns = NSEC_PER_SEC *
1079 (int64_t) msm_pm_sleep_time_override;
Mahesh Sivasubramanian03698762013-04-18 16:18:46 -06001080 do_div(ns, NSEC_PER_SEC / SCLK_HZ);
1081 msm_pm_max_sleep_time = (uint32_t) ns;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001082 }
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001083
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001084 if (pm_sleep_ops.lowest_limits)
1085 rs_limits = pm_sleep_ops.lowest_limits(false,
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001086 MSM_PM_SLEEP_MODE_POWER_COLLAPSE, &time_param, &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001087
1088 if (rs_limits) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001089 if (pm_sleep_ops.enter_sleep)
1090 ret = pm_sleep_ops.enter_sleep(
1091 msm_pm_max_sleep_time,
1092 rs_limits, false, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001093 if (!ret) {
Venkat Devarasetty849f47f2013-05-19 17:44:18 +05301094 collapsed = msm_pm_power_collapse(false);
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001095 if (pm_sleep_ops.exit_sleep) {
1096 pm_sleep_ops.exit_sleep(rs_limits,
1097 false, true, collapsed);
1098 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001099 }
1100 } else {
1101 pr_err("%s: cannot find the lowest power limit\n",
1102 __func__);
1103 }
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301104 time = msm_pm_timer_exit_suspend(time, period);
Venkat Devarasetty849f47f2013-05-19 17:44:18 +05301105 if (collapsed)
1106 msm_pm_add_stat(MSM_PM_STAT_SUSPEND, time);
1107 else
1108 msm_pm_add_stat(MSM_PM_STAT_FAILED_SUSPEND, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001109 } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
1110 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1111 pr_info("%s: standalone power collapse\n", __func__);
1112 msm_pm_power_collapse_standalone(false);
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -06001113 } else if (allow[MSM_PM_SLEEP_MODE_RETENTION]) {
1114 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1115 pr_info("%s: retention\n", __func__);
1116 msm_pm_retention();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001117 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
1118 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1119 pr_info("%s: swfi\n", __func__);
1120 msm_pm_swfi();
1121 }
1122
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001123enter_exit:
1124 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1125 pr_info("%s: return\n", __func__);
1126
1127 return 0;
1128}
1129
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001130void msm_pm_set_sleep_ops(struct msm_pm_sleep_ops *ops)
1131{
1132 if (ops)
1133 pm_sleep_ops = *ops;
1134}
1135
Badhri Jagan Sridharan9b186af2013-05-24 12:47:35 -07001136int msm_suspend_prepare(void)
1137{
1138 if (pnoc_clk != NULL)
1139 clk_disable_unprepare(pnoc_clk);
1140 return 0;
1141}
1142
1143void msm_suspend_wake(void)
1144{
1145 if (pnoc_clk != NULL)
1146 clk_prepare_enable(pnoc_clk);
1147}
1148
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001149static const struct platform_suspend_ops msm_pm_ops = {
1150 .enter = msm_pm_enter,
1151 .valid = suspend_valid_only_mem,
Badhri Jagan Sridharan9b186af2013-05-24 12:47:35 -07001152 .prepare_late = msm_suspend_prepare,
1153 .wake = msm_suspend_wake,
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -06001154};
Girish Mahadevanec50d2d2013-05-15 12:07:38 -06001155
1156static int __devinit msm_pm_snoc_client_probe(struct platform_device *pdev)
1157{
1158 int rc = 0;
1159 static struct msm_bus_scale_pdata *msm_pm_bus_pdata;
1160 static uint32_t msm_pm_bus_client;
1161
1162 msm_pm_bus_pdata = msm_bus_cl_get_pdata(pdev);
1163
1164 if (msm_pm_bus_pdata) {
1165 msm_pm_bus_client =
1166 msm_bus_scale_register_client(msm_pm_bus_pdata);
1167
1168 if (!msm_pm_bus_client) {
1169 pr_err("%s: Failed to register SNOC client",
1170 __func__);
1171 rc = -ENXIO;
1172 goto snoc_cl_probe_done;
1173 }
1174
1175 rc = msm_bus_scale_client_update_request(msm_pm_bus_client, 1);
1176
1177 if (rc)
1178 pr_err("%s: Error setting bus rate", __func__);
1179 }
1180
1181snoc_cl_probe_done:
1182 return rc;
1183}
1184
Anji Jonnala02dac8d2013-03-06 21:31:04 +05301185static int __devinit msm_cpu_status_probe(struct platform_device *pdev)
1186{
1187 struct msm_pm_sleep_status_data *pdata;
1188 char *key;
1189 u32 cpu;
1190
1191 if (!pdev)
1192 return -EFAULT;
1193
1194 msm_pm_slp_sts =
1195 kzalloc(sizeof(*msm_pm_slp_sts) * num_possible_cpus(),
1196 GFP_KERNEL);
1197
1198 if (!msm_pm_slp_sts)
1199 return -ENOMEM;
1200
1201 if (pdev->dev.of_node) {
1202 struct resource *res;
1203 u32 offset;
1204 int rc;
1205 u32 mask;
1206
1207 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1208 if (!res)
1209 goto fail_free_mem;
1210
1211 key = "qcom,cpu-alias-addr";
1212 rc = of_property_read_u32(pdev->dev.of_node, key, &offset);
1213
1214 if (rc)
1215 goto fail_free_mem;
1216
1217 key = "qcom,sleep-status-mask";
1218 rc = of_property_read_u32(pdev->dev.of_node, key,
1219 &mask);
1220 if (rc)
1221 goto fail_free_mem;
1222
1223 for_each_possible_cpu(cpu) {
1224 msm_pm_slp_sts[cpu].base_addr =
1225 ioremap(res->start + cpu * offset,
1226 resource_size(res));
1227 msm_pm_slp_sts[cpu].mask = mask;
1228
1229 if (!msm_pm_slp_sts[cpu].base_addr)
1230 goto failed_of_node;
1231 }
1232
1233 } else {
1234 pdata = pdev->dev.platform_data;
1235 if (!pdev->dev.platform_data)
1236 goto fail_free_mem;
1237
1238 for_each_possible_cpu(cpu) {
1239 msm_pm_slp_sts[cpu].base_addr =
1240 pdata->base_addr + cpu * pdata->cpu_offset;
1241 msm_pm_slp_sts[cpu].mask = pdata->mask;
1242 }
1243 }
1244
1245 return 0;
1246
1247failed_of_node:
1248 pr_info("%s(): Failed to key=%s\n", __func__, key);
1249 for_each_possible_cpu(cpu) {
1250 if (msm_pm_slp_sts[cpu].base_addr)
1251 iounmap(msm_pm_slp_sts[cpu].base_addr);
1252 }
1253fail_free_mem:
1254 kfree(msm_pm_slp_sts);
1255 return -EINVAL;
1256
1257};
1258
1259static struct of_device_id msm_slp_sts_match_tbl[] = {
1260 {.compatible = "qcom,cpu-sleep-status"},
1261 {},
1262};
1263
1264static struct platform_driver msm_cpu_status_driver = {
1265 .probe = msm_cpu_status_probe,
1266 .driver = {
1267 .name = "cpu_slp_status",
1268 .owner = THIS_MODULE,
1269 .of_match_table = msm_slp_sts_match_tbl,
1270 },
1271};
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -06001272
Girish Mahadevanec50d2d2013-05-15 12:07:38 -06001273static struct of_device_id msm_snoc_clnt_match_tbl[] = {
1274 {.compatible = "qcom,pm-snoc-client"},
1275 {},
1276};
1277
1278static struct platform_driver msm_cpu_pm_snoc_client_driver = {
1279 .probe = msm_pm_snoc_client_probe,
1280 .driver = {
1281 .name = "pm_snoc_client",
1282 .owner = THIS_MODULE,
1283 .of_match_table = msm_snoc_clnt_match_tbl,
1284 },
1285};
1286
1287
Praveen Chidambaramede45d02013-02-20 17:45:21 -07001288static int __init msm_pm_setup_saved_state(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001289{
1290 pgd_t *pc_pgd;
1291 pmd_t *pmd;
1292 unsigned long pmdval;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001293 unsigned long exit_phys;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301294
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001295 /* Page table for cores to come back up safely. */
1296 pc_pgd = pgd_alloc(&init_mm);
1297 if (!pc_pgd)
1298 return -ENOMEM;
1299
Steve Mucklef132c6c2012-06-06 18:30:57 -07001300 exit_phys = virt_to_phys(msm_pm_collapse_exit);
1301
1302 pmd = pmd_offset(pud_offset(pc_pgd + pgd_index(exit_phys),exit_phys),
1303 exit_phys);
1304 pmdval = (exit_phys & PGDIR_MASK) |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001305 PMD_TYPE_SECT | PMD_SECT_AP_WRITE;
1306 pmd[0] = __pmd(pmdval);
1307 pmd[1] = __pmd(pmdval + (1 << (PGDIR_SHIFT - 1)));
1308
Steve Mucklefcece052012-02-18 20:09:58 -08001309 msm_saved_state_phys =
1310 allocate_contiguous_ebi_nomap(CPU_SAVED_STATE_SIZE *
1311 num_possible_cpus(), 4);
1312 if (!msm_saved_state_phys)
1313 return -ENOMEM;
1314 msm_saved_state = ioremap_nocache(msm_saved_state_phys,
1315 CPU_SAVED_STATE_SIZE *
1316 num_possible_cpus());
1317 if (!msm_saved_state)
1318 return -ENOMEM;
1319
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001320 /* It is remotely possible that the code in msm_pm_collapse_exit()
1321 * which turns on the MMU with this mapping is in the
1322 * next even-numbered megabyte beyond the
1323 * start of msm_pm_collapse_exit().
1324 * Map this megabyte in as well.
1325 */
1326 pmd[2] = __pmd(pmdval + (2 << (PGDIR_SHIFT - 1)));
1327 flush_pmd_entry(pmd);
1328 msm_pm_pc_pgd = virt_to_phys(pc_pgd);
Steve Muckle730ad7a2012-02-21 15:26:37 -08001329 clean_caches((unsigned long)&msm_pm_pc_pgd, sizeof(msm_pm_pc_pgd),
1330 virt_to_phys(&msm_pm_pc_pgd));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001331
Praveen Chidambaramede45d02013-02-20 17:45:21 -07001332 return 0;
1333}
1334core_initcall(msm_pm_setup_saved_state);
1335
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001336static void setup_broadcast_timer(void *arg)
1337{
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001338 int cpu = smp_processor_id();
1339
Mahesh Sivasubramanian738eaf62013-04-30 12:42:59 -06001340 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ON, &cpu);
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001341}
1342
1343static int setup_broadcast_cpuhp_notify(struct notifier_block *n,
1344 unsigned long action, void *hcpu)
1345{
Mahesh Sivasubramanian738eaf62013-04-30 12:42:59 -06001346 int cpu = (unsigned long)hcpu;
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001347
1348 switch (action & ~CPU_TASKS_FROZEN) {
1349 case CPU_ONLINE:
Mahesh Sivasubramanian738eaf62013-04-30 12:42:59 -06001350 smp_call_function_single(cpu, setup_broadcast_timer, NULL, 1);
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001351 break;
1352 }
1353
1354 return NOTIFY_OK;
1355}
1356
1357static struct notifier_block setup_broadcast_notifier = {
1358 .notifier_call = setup_broadcast_cpuhp_notify,
1359};
1360
Praveen Chidambaramede45d02013-02-20 17:45:21 -07001361static int __init msm_pm_init(void)
1362{
1363 enum msm_pm_time_stats_id enable_stats[] = {
1364 MSM_PM_STAT_IDLE_WFI,
1365 MSM_PM_STAT_RETENTION,
1366 MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE,
1367 MSM_PM_STAT_IDLE_POWER_COLLAPSE,
1368 MSM_PM_STAT_SUSPEND,
1369 };
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001370 msm_pm_mode_sysfs_add();
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301371 msm_pm_add_stats(enable_stats, ARRAY_SIZE(enable_stats));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001372 suspend_set_ops(&msm_pm_ops);
Girish Mahadevan32493612013-05-10 14:37:17 -06001373 hrtimer_init(&pm_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001374 msm_cpuidle_init();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001375
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001376 if (msm_pm_pc_reset_timer) {
Mahesh Sivasubramanian738eaf62013-04-30 12:42:59 -06001377 on_each_cpu(setup_broadcast_timer, NULL, 1);
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001378 register_cpu_notifier(&setup_broadcast_notifier);
1379 }
1380
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001381 return 0;
1382}
1383
Girish Mahadevan55944992012-10-26 11:03:07 -06001384static void __devinit msm_pm_set_flush_fn(uint32_t pc_mode)
1385{
1386 msm_pm_disable_l2_fn = NULL;
1387 msm_pm_enable_l2_fn = NULL;
1388 msm_pm_flush_l2_fn = outer_flush_all;
1389
1390 if (pc_mode == MSM_PM_PC_NOTZ_L2_EXT) {
1391 msm_pm_disable_l2_fn = outer_disable;
1392 msm_pm_enable_l2_fn = outer_resume;
1393 }
1394}
1395
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001396struct msm_pc_debug_counters_buffer {
1397 void __iomem *reg;
1398 u32 len;
1399 char buf[MAX_BUF_SIZE];
1400};
1401
1402static inline u32 msm_pc_debug_counters_read_register(
1403 void __iomem *reg, int index , int offset)
1404{
1405 return readl_relaxed(reg + (index * 4 + offset) * 4);
1406}
1407
1408static char *counter_name[] = {
1409 "PC Entry Counter",
1410 "Warmboot Entry Counter",
1411 "PC Bailout Counter"
1412};
1413
1414static int msm_pc_debug_counters_copy(
1415 struct msm_pc_debug_counters_buffer *data)
1416{
1417 int j;
1418 u32 stat;
1419 unsigned int cpu;
1420
1421 for_each_possible_cpu(cpu) {
1422 data->len += scnprintf(data->buf + data->len,
1423 sizeof(data->buf)-data->len,
1424 "CPU%d\n", cpu);
1425
1426 for (j = 0; j < NUM_OF_COUNTERS; j++) {
1427 stat = msm_pc_debug_counters_read_register(
1428 data->reg, cpu, j);
1429 data->len += scnprintf(data->buf + data->len,
1430 sizeof(data->buf)-data->len,
1431 "\t%s : %d\n", counter_name[j],
1432 stat);
1433 }
1434
1435 }
1436
1437 return data->len;
1438}
1439
1440static int msm_pc_debug_counters_file_read(struct file *file,
1441 char __user *bufu, size_t count, loff_t *ppos)
1442{
1443 struct msm_pc_debug_counters_buffer *data;
1444
1445 data = file->private_data;
1446
1447 if (!data)
1448 return -EINVAL;
1449
Mahesh Sivasubramanian6900fdc2013-04-18 14:02:52 -06001450 if (!bufu)
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001451 return -EINVAL;
1452
1453 if (!access_ok(VERIFY_WRITE, bufu, count))
1454 return -EFAULT;
1455
1456 if (*ppos >= data->len && data->len == 0)
1457 data->len = msm_pc_debug_counters_copy(data);
1458
1459 return simple_read_from_buffer(bufu, count, ppos,
1460 data->buf, data->len);
1461}
1462
1463static int msm_pc_debug_counters_file_open(struct inode *inode,
1464 struct file *file)
1465{
1466 struct msm_pc_debug_counters_buffer *buf;
1467 void __iomem *msm_pc_debug_counters_reg;
1468
1469 msm_pc_debug_counters_reg = inode->i_private;
1470
1471 if (!msm_pc_debug_counters_reg)
1472 return -EINVAL;
1473
1474 file->private_data = kzalloc(
1475 sizeof(struct msm_pc_debug_counters_buffer), GFP_KERNEL);
1476
1477 if (!file->private_data) {
1478 pr_err("%s: ERROR kmalloc failed to allocate %d bytes\n",
1479 __func__, sizeof(struct msm_pc_debug_counters_buffer));
1480
1481 return -ENOMEM;
1482 }
1483
1484 buf = file->private_data;
1485 buf->reg = msm_pc_debug_counters_reg;
1486
1487 return 0;
1488}
1489
1490static int msm_pc_debug_counters_file_close(struct inode *inode,
1491 struct file *file)
1492{
1493 kfree(file->private_data);
1494 return 0;
1495}
1496
1497static const struct file_operations msm_pc_debug_counters_fops = {
1498 .open = msm_pc_debug_counters_file_open,
1499 .read = msm_pc_debug_counters_file_read,
1500 .release = msm_pc_debug_counters_file_close,
1501 .llseek = no_llseek,
1502};
1503
Girish Mahadevan55944992012-10-26 11:03:07 -06001504static int __devinit msm_pm_8x60_probe(struct platform_device *pdev)
1505{
1506 char *key = NULL;
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001507 struct dentry *dent = NULL;
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001508 struct resource *res = NULL;
1509 int i ;
1510 struct msm_pm_init_data_type pdata_local;
Girish Mahadevan55944992012-10-26 11:03:07 -06001511 int ret = 0;
1512
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001513 memset(&pdata_local, 0, sizeof(struct msm_pm_init_data_type));
1514
1515 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1516 if (res) {
1517 msm_pc_debug_counters_phys = res->start;
1518 WARN_ON(resource_size(res) < SZ_64);
1519 msm_pc_debug_counters = devm_ioremap(&pdev->dev, res->start,
1520 resource_size(res));
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001521 if (msm_pc_debug_counters)
1522 for (i = 0; i < resource_size(res)/4; i++)
1523 __raw_writel(0, msm_pc_debug_counters + i * 4);
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001524
1525 }
1526
1527 if (!msm_pc_debug_counters) {
1528 msm_pc_debug_counters = 0;
1529 msm_pc_debug_counters_phys = 0;
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001530 } else {
1531 dent = debugfs_create_file("pc_debug_counter", S_IRUGO, NULL,
1532 msm_pc_debug_counters,
1533 &msm_pc_debug_counters_fops);
1534 if (!dent)
1535 pr_err("%s: ERROR debugfs_create_file failed\n",
1536 __func__);
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001537 }
1538
Girish Mahadevan55944992012-10-26 11:03:07 -06001539 if (!pdev->dev.of_node) {
1540 struct msm_pm_init_data_type *d = pdev->dev.platform_data;
1541
1542 if (!d)
1543 goto pm_8x60_probe_done;
1544
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001545 memcpy(&pdata_local, d, sizeof(struct msm_pm_init_data_type));
1546
Girish Mahadevan55944992012-10-26 11:03:07 -06001547 } else {
1548 key = "qcom,pc-mode";
Archana Sathyakumar2b91dc82013-02-01 17:38:23 -07001549 ret = msm_pm_get_pc_mode(pdev->dev.of_node,
1550 key,
1551 &pdata_local.pc_mode);
Girish Mahadevan55944992012-10-26 11:03:07 -06001552 if (ret) {
Archana Sathyakumar2b91dc82013-02-01 17:38:23 -07001553 pr_debug("%s: Error reading key %s",
Girish Mahadevan55944992012-10-26 11:03:07 -06001554 __func__, key);
Archana Sathyakumar2b91dc82013-02-01 17:38:23 -07001555 return -EINVAL;
Girish Mahadevan55944992012-10-26 11:03:07 -06001556 }
Girish Mahadevan55944992012-10-26 11:03:07 -06001557
1558 key = "qcom,use-sync-timer";
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001559 pdata_local.use_sync_timer =
Girish Mahadevan55944992012-10-26 11:03:07 -06001560 of_property_read_bool(pdev->dev.of_node, key);
Praveen Chidambaram4b8df032013-01-18 16:21:16 -07001561
1562 key = "qcom,saw-turns-off-pll";
1563 msm_no_ramp_down_pc = of_property_read_bool(pdev->dev.of_node,
1564 key);
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001565
1566 key = "qcom,pc-resets-timer";
1567 msm_pm_pc_reset_timer = of_property_read_bool(
1568 pdev->dev.of_node, key);
Girish Mahadevan55944992012-10-26 11:03:07 -06001569 }
1570
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001571 if (pdata_local.cp15_data.reg_data &&
1572 pdata_local.cp15_data.reg_saved_state_size > 0) {
1573 cp15_data.reg_data = kzalloc(sizeof(uint32_t) *
1574 pdata_local.cp15_data.reg_saved_state_size,
1575 GFP_KERNEL);
1576 if (!cp15_data.reg_data)
1577 return -ENOMEM;
1578
1579 cp15_data.reg_val = kzalloc(sizeof(uint32_t) *
1580 pdata_local.cp15_data.reg_saved_state_size,
1581 GFP_KERNEL);
1582 if (cp15_data.reg_val)
1583 return -ENOMEM;
1584
1585 memcpy(cp15_data.reg_data, pdata_local.cp15_data.reg_data,
1586 pdata_local.cp15_data.reg_saved_state_size *
1587 sizeof(uint32_t));
1588 }
1589
1590 msm_pm_set_flush_fn(pdata_local.pc_mode);
1591 msm_pm_use_sync_timer = pdata_local.use_sync_timer;
1592 msm_pm_retention_calls_tz = pdata_local.retention_calls_tz;
1593
Girish Mahadevan55944992012-10-26 11:03:07 -06001594pm_8x60_probe_done:
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001595 msm_pm_init();
Anji Jonnala02dac8d2013-03-06 21:31:04 +05301596 if (pdev->dev.of_node)
1597 of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
1598
Girish Mahadevan55944992012-10-26 11:03:07 -06001599 return ret;
1600}
1601
1602static struct of_device_id msm_pm_8x60_table[] = {
1603 {.compatible = "qcom,pm-8x60"},
1604 {},
1605};
1606
1607static struct platform_driver msm_pm_8x60_driver = {
1608 .probe = msm_pm_8x60_probe,
1609 .driver = {
1610 .name = "pm-8x60",
1611 .owner = THIS_MODULE,
1612 .of_match_table = msm_pm_8x60_table,
1613 },
1614};
1615
1616static int __init msm_pm_8x60_init(void)
1617{
Anji Jonnala02dac8d2013-03-06 21:31:04 +05301618 int rc;
1619
Girish Mahadevanec50d2d2013-05-15 12:07:38 -06001620 rc = platform_driver_register(&msm_cpu_pm_snoc_client_driver);
1621
1622 if (rc) {
1623 pr_err("%s(): failed to register driver %s\n", __func__,
1624 msm_cpu_pm_snoc_client_driver.driver.name);
1625 return rc;
1626 }
1627
Badhri Jagan Sridharan9b186af2013-05-24 12:47:35 -07001628 pnoc_clk = clk_get_sys("pm_8x60", "bus_clk");
1629
1630 if (IS_ERR(pnoc_clk))
1631 pnoc_clk = NULL;
1632 else {
1633 clk_set_rate(pnoc_clk, 19200000);
1634 rc = clk_prepare_enable(pnoc_clk);
1635
1636 if (rc)
1637 pr_err("%s: PNOC clock enable failed\n", __func__);
1638 }
1639
Girish Mahadevan55944992012-10-26 11:03:07 -06001640 return platform_driver_register(&msm_pm_8x60_driver);
1641}
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001642device_initcall(msm_pm_8x60_init);
Mahesh Sivasubramanian59ffcb02013-05-31 15:08:15 -06001643
1644void __init msm_pm_sleep_status_init(void)
1645{
1646 platform_driver_register(&msm_cpu_status_driver);
1647}