blob: 146dc0b040c4f35fa8dc91a57aa80f1898a55e7c [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
Laura Abbott2ff1d122013-04-24 16:13:03 -070014#include <linux/dma-mapping.h>
Priyanka Mathur13bdad12013-01-28 15:52:56 -080015#include <linux/debugfs.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070016#include <linux/module.h>
17#include <linux/kernel.h>
18#include <linux/init.h>
19#include <linux/completion.h>
20#include <linux/cpuidle.h>
21#include <linux/interrupt.h>
22#include <linux/io.h>
23#include <linux/ktime.h>
24#include <linux/pm.h>
Steve Mucklef132c6c2012-06-06 18:30:57 -070025#include <linux/pm_qos.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070026#include <linux/smp.h>
27#include <linux/suspend.h>
28#include <linux/tick.h>
Anji Jonnala02dac8d2013-03-06 21:31:04 +053029#include <linux/delay.h>
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -060030#include <linux/platform_device.h>
Anji Jonnala02dac8d2013-03-06 21:31:04 +053031#include <linux/of_platform.h>
Abhijeet Dharmapurikare8f83852013-02-06 18:57:17 -080032#include <linux/regulator/krait-regulator.h>
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -060033#include <linux/cpu.h>
Badhri Jagan Sridharan9b186af2013-05-24 12:47:35 -070034#include <linux/clk.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070035#include <mach/msm_iomap.h>
Praveen Chidambaram192979f2012-04-25 18:30:23 -060036#include <mach/socinfo.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070037#include <mach/system.h>
Girish Mahadevand27ca4a2012-08-15 09:21:23 -060038#include <mach/scm.h>
39#include <mach/socinfo.h>
Hanumant Singhbd82a3e2013-02-26 13:51:17 -080040#define CREATE_TRACE_POINTS
41#include <mach/trace_msm_low_power.h>
Praveen Chidambaramc594a092012-09-18 19:48:29 -060042#include <mach/msm-krait-l2-accessors.h>
Girish Mahadevanec50d2d2013-05-15 12:07:38 -060043#include <mach/msm_bus.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070044#include <asm/cacheflush.h>
45#include <asm/hardware/gic.h>
46#include <asm/pgtable.h>
47#include <asm/pgalloc.h>
Girish Mahadevan55944992012-10-26 11:03:07 -060048#include <asm/outercache.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070049#ifdef CONFIG_VFP
50#include <asm/vfp.h>
51#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070052#include "acpuclock.h"
53#include "clock.h"
54#include "avs.h"
Abhijeet Dharmapurikarefaca4f2011-12-27 16:24:07 -080055#include <mach/cpuidle.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070056#include "idle.h"
Matt Wagantall7cca4642012-02-01 16:43:24 -080057#include "pm.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070058#include "scm-boot.h"
59#include "spm.h"
60#include "timer.h"
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -060061#include "pm-boot.h"
Girish Mahadevandc318fd2012-08-17 16:48:05 -060062#include <mach/event_timer.h>
Venkat Devarasetty722f91f2013-06-03 19:41:27 +053063#include <linux/cpu_pm.h>
Hanumant Singhbd82a3e2013-02-26 13:51:17 -080064
Praveen Chidambaramf27a5152013-02-01 11:44:53 -070065#define SCM_L2_RETENTION (0x2)
66#define SCM_CMD_TERMINATE_PC (0x2)
67
68#define GET_CPU_OF_ATTR(attr) \
69 (container_of(attr, struct msm_pm_kobj_attribute, ka)->cpu)
70
71#define SCLK_HZ (32768)
Praveen Chidambaramf27a5152013-02-01 11:44:53 -070072
Priyanka Mathur13bdad12013-01-28 15:52:56 -080073#define NUM_OF_COUNTERS 3
74#define MAX_BUF_SIZE 512
75
Praveen Chidambaramf27a5152013-02-01 11:44:53 -070076static int msm_pm_debug_mask = 1;
77module_param_named(
78 debug_mask, msm_pm_debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP
79);
80
81static int msm_pm_sleep_time_override;
82module_param_named(sleep_time_override,
83 msm_pm_sleep_time_override, int, S_IRUGO | S_IWUSR | S_IWGRP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070084
85enum {
86 MSM_PM_DEBUG_SUSPEND = BIT(0),
87 MSM_PM_DEBUG_POWER_COLLAPSE = BIT(1),
88 MSM_PM_DEBUG_SUSPEND_LIMITS = BIT(2),
89 MSM_PM_DEBUG_CLOCK = BIT(3),
90 MSM_PM_DEBUG_RESET_VECTOR = BIT(4),
Karthik Parsha6fb932d2012-01-24 18:04:12 -080091 MSM_PM_DEBUG_IDLE_CLK = BIT(5),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070092 MSM_PM_DEBUG_IDLE = BIT(6),
93 MSM_PM_DEBUG_IDLE_LIMITS = BIT(7),
94 MSM_PM_DEBUG_HOTPLUG = BIT(8),
95};
96
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070097enum {
98 MSM_PM_MODE_ATTR_SUSPEND,
99 MSM_PM_MODE_ATTR_IDLE,
100 MSM_PM_MODE_ATTR_NR,
101};
102
103static char *msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_NR] = {
104 [MSM_PM_MODE_ATTR_SUSPEND] = "suspend_enabled",
105 [MSM_PM_MODE_ATTR_IDLE] = "idle_enabled",
106};
107
108struct msm_pm_kobj_attribute {
109 unsigned int cpu;
110 struct kobj_attribute ka;
111};
112
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700113struct msm_pm_sysfs_sleep_mode {
114 struct kobject *kobj;
115 struct attribute_group attr_group;
116 struct attribute *attrs[MSM_PM_MODE_ATTR_NR + 1];
117 struct msm_pm_kobj_attribute kas[MSM_PM_MODE_ATTR_NR];
118};
119
120static char *msm_pm_sleep_mode_labels[MSM_PM_SLEEP_MODE_NR] = {
121 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE] = "power_collapse",
122 [MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT] = "wfi",
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600123 [MSM_PM_SLEEP_MODE_RETENTION] = "retention",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700124 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE] =
125 "standalone_power_collapse",
126};
127
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600128static struct hrtimer pm_hrtimer;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600129static struct msm_pm_sleep_ops pm_sleep_ops;
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -0700130static bool msm_pm_ldo_retention_enabled = true;
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700131static bool msm_pm_use_sync_timer;
132static struct msm_pm_cp15_save_data cp15_data;
133static bool msm_pm_retention_calls_tz;
Praveen Chidambaram4b8df032013-01-18 16:21:16 -0700134static bool msm_no_ramp_down_pc;
Anji Jonnala02dac8d2013-03-06 21:31:04 +0530135static struct msm_pm_sleep_status_data *msm_pm_slp_sts;
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -0600136static bool msm_pm_pc_reset_timer;
Badhri Jagan Sridharan9b186af2013-05-24 12:47:35 -0700137static struct clk *pnoc_clk;
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700138
Archana Sathyakumar2b91dc82013-02-01 17:38:23 -0700139static int msm_pm_get_pc_mode(struct device_node *node,
140 const char *key, uint32_t *pc_mode_val)
141{
142 struct pc_mode_of {
143 uint32_t mode;
144 char *mode_name;
145 };
146 int i;
147 struct pc_mode_of pc_modes[] = {
148 {MSM_PM_PC_TZ_L2_INT, "tz_l2_int"},
149 {MSM_PM_PC_NOTZ_L2_EXT, "no_tz_l2_ext"},
150 {MSM_PM_PC_TZ_L2_EXT , "tz_l2_ext"} };
151 int ret;
152 const char *pc_mode_str;
153
154 ret = of_property_read_string(node, key, &pc_mode_str);
155 if (ret) {
156 pr_debug("%s: Cannot read %s,defaulting to 0", __func__, key);
157 pc_mode_val = MSM_PM_PC_TZ_L2_INT;
158 ret = 0;
159 } else {
160 ret = -EINVAL;
161 for (i = 0; i < ARRAY_SIZE(pc_modes); i++) {
162 if (!strncmp(pc_mode_str, pc_modes[i].mode_name,
163 strlen(pc_modes[i].mode_name))) {
164 *pc_mode_val = pc_modes[i].mode;
165 ret = 0;
166 break;
167 }
168 }
169 }
170 return ret;
171}
172
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700173/*
174 * Write out the attribute.
175 */
176static ssize_t msm_pm_mode_attr_show(
177 struct kobject *kobj, struct kobj_attribute *attr, char *buf)
178{
179 int ret = -EINVAL;
180 int i;
181
182 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
183 struct kernel_param kp;
184 unsigned int cpu;
185 struct msm_pm_platform_data *mode;
186
187 if (msm_pm_sleep_mode_labels[i] == NULL)
188 continue;
189
190 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
191 continue;
192
193 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600194 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700195
196 if (!strcmp(attr->attr.name,
197 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
198 u32 arg = mode->suspend_enabled;
199 kp.arg = &arg;
200 ret = param_get_ulong(buf, &kp);
201 } else if (!strcmp(attr->attr.name,
202 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
203 u32 arg = mode->idle_enabled;
204 kp.arg = &arg;
205 ret = param_get_ulong(buf, &kp);
206 }
207
208 break;
209 }
210
211 if (ret > 0) {
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600212 strlcat(buf, "\n", PAGE_SIZE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700213 ret++;
214 }
215
216 return ret;
217}
218
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700219static ssize_t msm_pm_mode_attr_store(struct kobject *kobj,
220 struct kobj_attribute *attr, const char *buf, size_t count)
221{
222 int ret = -EINVAL;
223 int i;
224
225 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
226 struct kernel_param kp;
227 unsigned int cpu;
228 struct msm_pm_platform_data *mode;
229
230 if (msm_pm_sleep_mode_labels[i] == NULL)
231 continue;
232
233 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
234 continue;
235
236 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600237 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700238
239 if (!strcmp(attr->attr.name,
240 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
241 kp.arg = &mode->suspend_enabled;
242 ret = param_set_byte(buf, &kp);
243 } else if (!strcmp(attr->attr.name,
244 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
245 kp.arg = &mode->idle_enabled;
246 ret = param_set_byte(buf, &kp);
247 }
248
249 break;
250 }
251
252 return ret ? ret : count;
253}
254
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700255static int __devinit msm_pm_mode_sysfs_add_cpu(
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700256 unsigned int cpu, struct kobject *modes_kobj)
257{
258 char cpu_name[8];
259 struct kobject *cpu_kobj;
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600260 struct msm_pm_sysfs_sleep_mode *mode = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700261 int i, j, k;
262 int ret;
263
264 snprintf(cpu_name, sizeof(cpu_name), "cpu%u", cpu);
265 cpu_kobj = kobject_create_and_add(cpu_name, modes_kobj);
266 if (!cpu_kobj) {
267 pr_err("%s: cannot create %s kobject\n", __func__, cpu_name);
268 ret = -ENOMEM;
269 goto mode_sysfs_add_cpu_exit;
270 }
271
272 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
273 int idx = MSM_PM_MODE(cpu, i);
274
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600275 if ((!msm_pm_sleep_modes[idx].suspend_supported)
276 && (!msm_pm_sleep_modes[idx].idle_supported))
277 continue;
278
279 if (!msm_pm_sleep_mode_labels[i] ||
280 !msm_pm_sleep_mode_labels[i][0])
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700281 continue;
282
283 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
284 if (!mode) {
285 pr_err("%s: cannot allocate memory for attributes\n",
286 __func__);
287 ret = -ENOMEM;
288 goto mode_sysfs_add_cpu_exit;
289 }
290
291 mode->kobj = kobject_create_and_add(
292 msm_pm_sleep_mode_labels[i], cpu_kobj);
293 if (!mode->kobj) {
294 pr_err("%s: cannot create kobject\n", __func__);
295 ret = -ENOMEM;
296 goto mode_sysfs_add_cpu_exit;
297 }
298
299 for (k = 0, j = 0; k < MSM_PM_MODE_ATTR_NR; k++) {
300 if ((k == MSM_PM_MODE_ATTR_IDLE) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600301 !msm_pm_sleep_modes[idx].idle_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700302 continue;
303 if ((k == MSM_PM_MODE_ATTR_SUSPEND) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600304 !msm_pm_sleep_modes[idx].suspend_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700305 continue;
Stephen Boydd2059c32012-07-03 15:11:15 -0700306 sysfs_attr_init(&mode->kas[j].ka.attr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700307 mode->kas[j].cpu = cpu;
308 mode->kas[j].ka.attr.mode = 0644;
309 mode->kas[j].ka.show = msm_pm_mode_attr_show;
310 mode->kas[j].ka.store = msm_pm_mode_attr_store;
311 mode->kas[j].ka.attr.name = msm_pm_mode_attr_labels[k];
312 mode->attrs[j] = &mode->kas[j].ka.attr;
313 j++;
314 }
315 mode->attrs[j] = NULL;
316
317 mode->attr_group.attrs = mode->attrs;
318 ret = sysfs_create_group(mode->kobj, &mode->attr_group);
319 if (ret) {
320 pr_err("%s: cannot create kobject attribute group\n",
321 __func__);
322 goto mode_sysfs_add_cpu_exit;
323 }
324 }
325
326 ret = 0;
327
328mode_sysfs_add_cpu_exit:
Praveen Chidambaramd5ac2d32011-10-24 14:30:27 -0600329 if (ret) {
Praveen Chidambaram2cfda632011-10-11 16:58:09 -0600330 if (mode && mode->kobj)
331 kobject_del(mode->kobj);
332 kfree(mode);
333 }
334
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700335 return ret;
336}
337
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700338int __devinit msm_pm_mode_sysfs_add(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700339{
340 struct kobject *module_kobj;
341 struct kobject *modes_kobj;
342 unsigned int cpu;
343 int ret;
344
345 module_kobj = kset_find_obj(module_kset, KBUILD_MODNAME);
346 if (!module_kobj) {
347 pr_err("%s: cannot find kobject for module %s\n",
348 __func__, KBUILD_MODNAME);
349 ret = -ENOENT;
350 goto mode_sysfs_add_exit;
351 }
352
353 modes_kobj = kobject_create_and_add("modes", module_kobj);
354 if (!modes_kobj) {
355 pr_err("%s: cannot create modes kobject\n", __func__);
356 ret = -ENOMEM;
357 goto mode_sysfs_add_exit;
358 }
359
360 for_each_possible_cpu(cpu) {
361 ret = msm_pm_mode_sysfs_add_cpu(cpu, modes_kobj);
362 if (ret)
363 goto mode_sysfs_add_exit;
364 }
365
366 ret = 0;
367
368mode_sysfs_add_exit:
369 return ret;
370}
371
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700372/*
373 * Configure hardware registers in preparation for Apps power down.
374 */
375static void msm_pm_config_hw_before_power_down(void)
376{
377 return;
378}
379
380/*
381 * Clear hardware registers after Apps powers up.
382 */
383static void msm_pm_config_hw_after_power_up(void)
384{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700385}
386
387/*
388 * Configure hardware registers in preparation for SWFI.
389 */
390static void msm_pm_config_hw_before_swfi(void)
391{
392 return;
393}
394
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600395/*
396 * Configure/Restore hardware registers in preparation for Retention.
397 */
398
399static void msm_pm_config_hw_after_retention(void)
400{
401 int ret;
Abhijeet Dharmapurikare8f83852013-02-06 18:57:17 -0800402
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600403 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
404 WARN_ON(ret);
405}
406
407static void msm_pm_config_hw_before_retention(void)
408{
409 return;
410}
411
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600412static void msm_pm_save_cpu_reg(void)
413{
414 int i;
415
416 /* Only on core0 */
417 if (smp_processor_id())
418 return;
419
420 /**
421 * On some targets, L2 PC will turn off may reset the core
422 * configuration for the mux and the default may not make the core
423 * happy when it resumes.
424 * Save the active vdd, and set the core vdd to QSB max vdd, so that
425 * when the core resumes, it is capable of supporting the current QSB
426 * rate. Then restore the active vdd before switching the acpuclk rate.
427 */
428 if (msm_pm_get_l2_flush_flag() == 1) {
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700429 cp15_data.active_vdd = msm_spm_get_vdd(0);
430 for (i = 0; i < cp15_data.reg_saved_state_size; i++)
431 cp15_data.reg_val[i] =
432 get_l2_indirect_reg(
433 cp15_data.reg_data[i]);
434 msm_spm_set_vdd(0, cp15_data.qsb_pc_vdd);
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600435 }
436}
437
438static void msm_pm_restore_cpu_reg(void)
439{
440 int i;
441
442 /* Only on core0 */
443 if (smp_processor_id())
444 return;
445
446 if (msm_pm_get_l2_flush_flag() == 1) {
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700447 for (i = 0; i < cp15_data.reg_saved_state_size; i++)
448 set_l2_indirect_reg(
449 cp15_data.reg_data[i],
450 cp15_data.reg_val[i]);
451 msm_spm_set_vdd(0, cp15_data.active_vdd);
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600452 }
453}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700454
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700455static void msm_pm_swfi(void)
456{
457 msm_pm_config_hw_before_swfi();
458 msm_arch_idle();
459}
460
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600461static void msm_pm_retention(void)
462{
463 int ret = 0;
464
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600465 msm_pm_config_hw_before_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600466 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_POWER_RETENTION, false);
467 WARN_ON(ret);
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600468
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700469 if (msm_pm_retention_calls_tz)
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600470 scm_call_atomic1(SCM_SVC_BOOT, SCM_CMD_TERMINATE_PC,
471 SCM_L2_RETENTION);
472 else
473 msm_arch_idle();
474
475 msm_pm_config_hw_after_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600476}
477
Stephen Boydb29750d2012-02-21 01:21:32 -0800478static bool __ref msm_pm_spm_power_collapse(
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700479 unsigned int cpu, bool from_idle, bool notify_rpm)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700480{
481 void *entry;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600482 bool collapsed = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700483 int ret;
Mahesh Sivasubramanianda746252013-05-02 09:58:08 -0600484 bool save_cpu_regs = !cpu || from_idle;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700485
486 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
487 pr_info("CPU%u: %s: notify_rpm %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700488 cpu, __func__, (int) notify_rpm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700489
Venkat Devarasetty722f91f2013-06-03 19:41:27 +0530490 if (from_idle == true)
491 cpu_pm_enter();
492
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700493 ret = msm_spm_set_low_power_mode(
494 MSM_SPM_MODE_POWER_COLLAPSE, notify_rpm);
495 WARN_ON(ret);
496
Mahesh Sivasubramanianda746252013-05-02 09:58:08 -0600497 entry = save_cpu_regs ? msm_pm_collapse_exit : msm_secondary_startup;
498
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700499 msm_pm_boot_config_before_pc(cpu, virt_to_phys(entry));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700500
501 if (MSM_PM_DEBUG_RESET_VECTOR & msm_pm_debug_mask)
502 pr_info("CPU%u: %s: program vector to %p\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700503 cpu, __func__, entry);
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -0600504 if (from_idle && msm_pm_pc_reset_timer)
505 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700506
Mahesh Sivasubramanianda746252013-05-02 09:58:08 -0600507 collapsed = save_cpu_regs ? msm_pm_collapse() : msm_pm_pc_hotplug();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700508
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -0600509 if (from_idle && msm_pm_pc_reset_timer)
510 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu);
511
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700512 msm_pm_boot_config_after_pc(cpu);
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -0600513
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700514 if (collapsed) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700515 cpu_init();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700516 local_fiq_enable();
517 }
518
519 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
520 pr_info("CPU%u: %s: msm_pm_collapse returned, collapsed %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700521 cpu, __func__, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700522
523 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
524 WARN_ON(ret);
Venkat Devarasetty722f91f2013-06-03 19:41:27 +0530525
526 if (from_idle == true)
527 cpu_pm_exit();
528
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600529 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700530}
531
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600532static bool msm_pm_power_collapse_standalone(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700533{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700534 unsigned int cpu = smp_processor_id();
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700535 unsigned int avsdscr;
536 unsigned int avscsr;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600537 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700538
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700539 avsdscr = avs_get_avsdscr();
540 avscsr = avs_get_avscsr();
541 avs_set_avscsr(0); /* Disable AVS */
542
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700543 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, false);
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700544
545 avs_set_avsdscr(avsdscr);
546 avs_set_avscsr(avscsr);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600547 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700548}
549
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600550static bool msm_pm_power_collapse(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700551{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700552 unsigned int cpu = smp_processor_id();
Praveen Chidambaram4b8df032013-01-18 16:21:16 -0700553 unsigned long saved_acpuclk_rate = 0;
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700554 unsigned int avsdscr;
555 unsigned int avscsr;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600556 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700557
558 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
559 pr_info("CPU%u: %s: idle %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700560 cpu, __func__, (int)from_idle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700561
562 msm_pm_config_hw_before_power_down();
563 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700564 pr_info("CPU%u: %s: pre power down\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700565
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700566 avsdscr = avs_get_avsdscr();
567 avscsr = avs_get_avscsr();
568 avs_set_avscsr(0); /* Disable AVS */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700569
Praveen Chidambaram4b8df032013-01-18 16:21:16 -0700570 if (cpu_online(cpu) && !msm_no_ramp_down_pc)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700571 saved_acpuclk_rate = acpuclk_power_collapse();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700572
573 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
574 pr_info("CPU%u: %s: change clock rate (old rate = %lu)\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700575 cpu, __func__, saved_acpuclk_rate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700576
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700577 if (cp15_data.save_cp15)
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600578 msm_pm_save_cpu_reg();
579
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700580 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700581
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700582 if (cp15_data.save_cp15)
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600583 msm_pm_restore_cpu_reg();
584
Girish Mahadevan4e025d92012-02-29 13:26:51 -0700585 if (cpu_online(cpu)) {
586 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
587 pr_info("CPU%u: %s: restore clock rate to %lu\n",
588 cpu, __func__, saved_acpuclk_rate);
Praveen Chidambaram4b8df032013-01-18 16:21:16 -0700589 if (!msm_no_ramp_down_pc &&
590 acpuclk_set_rate(cpu, saved_acpuclk_rate, SETRATE_PC)
591 < 0)
Girish Mahadevan4e025d92012-02-29 13:26:51 -0700592 pr_err("CPU%u: %s: failed to restore clock rate(%lu)\n",
593 cpu, __func__, saved_acpuclk_rate);
594 } else {
595 unsigned int gic_dist_enabled;
596 unsigned int gic_dist_pending;
597 gic_dist_enabled = readl_relaxed(
598 MSM_QGIC_DIST_BASE + GIC_DIST_ENABLE_CLEAR);
599 gic_dist_pending = readl_relaxed(
600 MSM_QGIC_DIST_BASE + GIC_DIST_PENDING_SET);
601 mb();
602 gic_dist_pending &= gic_dist_enabled;
603
604 if (gic_dist_pending)
605 pr_err("CPU %d interrupted during hotplug.Pending int 0x%x\n",
606 cpu, gic_dist_pending);
607 }
608
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700609
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700610 avs_set_avsdscr(avsdscr);
611 avs_set_avscsr(avscsr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700612 msm_pm_config_hw_after_power_up();
613 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700614 pr_info("CPU%u: %s: post power up\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700615
616 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700617 pr_info("CPU%u: %s: return\n", cpu, __func__);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600618 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700619}
620
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600621static int64_t msm_pm_timer_enter_idle(void)
622{
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700623 if (msm_pm_use_sync_timer)
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600624 return ktime_to_ns(tick_nohz_get_sleep_length());
625
626 return msm_timer_enter_idle();
627}
628
629static void msm_pm_timer_exit_idle(bool timer_halted)
630{
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700631 if (msm_pm_use_sync_timer)
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600632 return;
633
634 msm_timer_exit_idle((int) timer_halted);
635}
636
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530637static int64_t msm_pm_timer_enter_suspend(int64_t *period)
638{
Anji Jonnalac02367a2012-07-01 02:56:11 +0530639 int64_t time = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530640
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700641 if (msm_pm_use_sync_timer)
Mahesh Sivasubramanian0eaeb302013-03-14 14:56:32 -0600642 return sched_clock();
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530643
644 time = msm_timer_get_sclk_time(period);
645 if (!time)
646 pr_err("%s: Unable to read sclk.\n", __func__);
647
648 return time;
649}
650
651static int64_t msm_pm_timer_exit_suspend(int64_t time, int64_t period)
652{
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700653 if (msm_pm_use_sync_timer)
Mahesh Sivasubramanian0eaeb302013-03-14 14:56:32 -0600654 return sched_clock() - time;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530655
656 if (time != 0) {
657 int64_t end_time = msm_timer_get_sclk_time(NULL);
658 if (end_time != 0) {
659 time = end_time - time;
660 if (time < 0)
661 time += period;
662 } else
663 time = 0;
664 }
665
666 return time;
667}
668
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600669/**
670 * pm_hrtimer_cb() : Callback function for hrtimer created if the
671 * core needs to be awake to handle an event.
672 * @hrtimer : Pointer to hrtimer
673 */
674static enum hrtimer_restart pm_hrtimer_cb(struct hrtimer *hrtimer)
675{
676 return HRTIMER_NORESTART;
677}
678
679/**
680 * msm_pm_set_timer() : Set an hrtimer to wakeup the core in time
681 * to handle an event.
682 */
683static void msm_pm_set_timer(uint32_t modified_time_us)
684{
685 u64 modified_time_ns = modified_time_us * NSEC_PER_USEC;
686 ktime_t modified_ktime = ns_to_ktime(modified_time_ns);
687 pm_hrtimer.function = pm_hrtimer_cb;
Girish Mahadevan32493612013-05-10 14:37:17 -0600688 hrtimer_start(&pm_hrtimer, modified_ktime, HRTIMER_MODE_REL);
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600689}
690
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700691/******************************************************************************
692 * External Idle/Suspend Functions
693 *****************************************************************************/
694
695void arch_idle(void)
696{
697 return;
698}
699
Priyanka Mathur26b4a4b2012-11-05 13:45:45 -0800700static inline void msm_pm_ftrace_lpm_enter(unsigned int cpu,
701 uint32_t latency, uint32_t sleep_us,
702 uint32_t wake_up,
703 enum msm_pm_sleep_mode mode)
704{
705 switch (mode) {
706 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
707 trace_msm_pm_enter_wfi(cpu, latency, sleep_us, wake_up);
708 break;
709 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
710 trace_msm_pm_enter_spc(cpu, latency, sleep_us, wake_up);
711 break;
712 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
713 trace_msm_pm_enter_pc(cpu, latency, sleep_us, wake_up);
714 break;
715 case MSM_PM_SLEEP_MODE_RETENTION:
716 trace_msm_pm_enter_ret(cpu, latency, sleep_us, wake_up);
717 break;
718 default:
719 break;
720 }
721}
722
723static inline void msm_pm_ftrace_lpm_exit(unsigned int cpu,
724 enum msm_pm_sleep_mode mode,
725 bool success)
726{
727 switch (mode) {
728 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
729 trace_msm_pm_exit_wfi(cpu, success);
730 break;
731 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
732 trace_msm_pm_exit_spc(cpu, success);
733 break;
734 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
735 trace_msm_pm_exit_pc(cpu, success);
736 break;
737 case MSM_PM_SLEEP_MODE_RETENTION:
738 trace_msm_pm_exit_ret(cpu, success);
739 break;
740 default:
741 break;
742 }
743}
744
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800745static int msm_pm_idle_prepare(struct cpuidle_device *dev,
746 struct cpuidle_driver *drv, int index,
747 void **msm_pm_idle_rs_limits)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700748{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700749 int i;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700750 unsigned int power_usage = -1;
Priyanka Mathur848bb4c2012-11-30 18:04:57 -0800751 int ret = MSM_PM_SLEEP_MODE_NOT_SELECTED;
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600752 uint32_t modified_time_us = 0;
753 struct msm_pm_time_params time_param;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700754
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600755 time_param.latency_us =
756 (uint32_t) pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
757 time_param.sleep_us =
758 (uint32_t) (ktime_to_us(tick_nohz_get_sleep_length())
759 & UINT_MAX);
760 time_param.modified_time_us = 0;
761
762 if (!dev->cpu)
763 time_param.next_event_us =
764 (uint32_t) (ktime_to_us(get_next_event_time())
765 & UINT_MAX);
766 else
767 time_param.next_event_us = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700768
769 for (i = 0; i < dev->state_count; i++) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700770 struct cpuidle_state *state = &drv->states[i];
771 struct cpuidle_state_usage *st_usage = &dev->states_usage[i];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700772 enum msm_pm_sleep_mode mode;
773 bool allow;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600774 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700775 int idx;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800776 void *rs_limits = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700777
Steve Mucklef132c6c2012-06-06 18:30:57 -0700778 mode = (enum msm_pm_sleep_mode) cpuidle_get_statedata(st_usage);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700779 idx = MSM_PM_MODE(dev->cpu, mode);
780
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600781 allow = msm_pm_sleep_modes[idx].idle_enabled &&
782 msm_pm_sleep_modes[idx].idle_supported;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700783
784 switch (mode) {
785 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700786 if (num_online_cpus() > 1)
Priyanka Mathur68a9bc52012-10-03 15:15:01 -0700787 allow = false;
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700788 break;
Mahesh Sivasubramanianba7e01c2012-09-04 18:22:51 -0600789 case MSM_PM_SLEEP_MODE_RETENTION:
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -0700790 /*
791 * The Krait BHS regulator doesn't have enough head
792 * room to drive the retention voltage on LDO and so
793 * has disabled retention
794 */
795 if (!msm_pm_ldo_retention_enabled)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700796 allow = false;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700797
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700798 if (msm_pm_retention_calls_tz && num_online_cpus() > 1)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700799 allow = false;
800 break;
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700801 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
802 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
803 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700804 default:
805 allow = false;
806 break;
807 }
808
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700809 if (!allow)
810 continue;
811
812 if (pm_sleep_ops.lowest_limits)
813 rs_limits = pm_sleep_ops.lowest_limits(true,
814 mode, &time_param, &power);
815
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700816 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700817 pr_info("CPU%u:%s:%s, latency %uus, slp %uus, lim %p\n",
818 dev->cpu, __func__, state->desc,
819 time_param.latency_us,
820 time_param.sleep_us, rs_limits);
821 if (!rs_limits)
822 continue;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700823
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700824 if (power < power_usage) {
825 power_usage = power;
826 modified_time_us = time_param.modified_time_us;
827 ret = mode;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800828 *msm_pm_idle_rs_limits = rs_limits;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700829 }
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700830
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700831 }
832
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600833 if (modified_time_us && !dev->cpu)
834 msm_pm_set_timer(modified_time_us);
Priyanka Mathur26b4a4b2012-11-05 13:45:45 -0800835
836 msm_pm_ftrace_lpm_enter(dev->cpu, time_param.latency_us,
837 time_param.sleep_us, time_param.next_event_us,
838 ret);
839
Steve Mucklef132c6c2012-06-06 18:30:57 -0700840 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700841}
842
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800843enum msm_pm_sleep_mode msm_pm_idle_enter(struct cpuidle_device *dev,
844 struct cpuidle_driver *drv, int index)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700845{
846 int64_t time;
Priyanka Mathur26b4a4b2012-11-05 13:45:45 -0800847 bool collapsed = 1;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800848 int exit_stat = -1;
849 enum msm_pm_sleep_mode sleep_mode;
850 void *msm_pm_idle_rs_limits = NULL;
Mahesh Sivasubramanian03698762013-04-18 16:18:46 -0600851 uint32_t sleep_delay = 1;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800852 int ret = -ENODEV;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800853 int notify_rpm = false;
854 bool timer_halted = false;
855
856 sleep_mode = msm_pm_idle_prepare(dev, drv, index,
857 &msm_pm_idle_rs_limits);
858
859 if (!msm_pm_idle_rs_limits) {
860 sleep_mode = MSM_PM_SLEEP_MODE_NOT_SELECTED;
861 goto cpuidle_enter_bail;
862 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700863
864 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
865 pr_info("CPU%u: %s: mode %d\n",
866 smp_processor_id(), __func__, sleep_mode);
867
868 time = ktime_to_ns(ktime_get());
869
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800870 if (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE) {
Mahesh Sivasubramanian2f415382013-05-17 16:29:05 -0600871 int64_t ns = msm_pm_timer_enter_idle();
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800872 notify_rpm = true;
Mahesh Sivasubramanian2f415382013-05-17 16:29:05 -0600873 do_div(ns, NSEC_PER_SEC / SCLK_HZ);
874 sleep_delay = (uint32_t)ns;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800875
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800876 if (sleep_delay == 0) /* 0 would mean infinite time */
877 sleep_delay = 1;
878 }
879
880 if (pm_sleep_ops.enter_sleep)
881 ret = pm_sleep_ops.enter_sleep(sleep_delay,
882 msm_pm_idle_rs_limits, true, notify_rpm);
883 if (ret)
884 goto cpuidle_enter_bail;
885
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700886 switch (sleep_mode) {
887 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
888 msm_pm_swfi();
889 exit_stat = MSM_PM_STAT_IDLE_WFI;
890 break;
891
892 case MSM_PM_SLEEP_MODE_RETENTION:
893 msm_pm_retention();
894 exit_stat = MSM_PM_STAT_RETENTION;
895 break;
896
897 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
898 collapsed = msm_pm_power_collapse_standalone(true);
Venkat Devarasetty849f47f2013-05-19 17:44:18 +0530899 if (collapsed)
900 exit_stat = MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE;
901 else
902 exit_stat
903 = MSM_PM_STAT_IDLE_FAILED_STANDALONE_POWER_COLLAPSE;
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700904 break;
905
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800906 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700907 if (MSM_PM_DEBUG_IDLE_CLK & msm_pm_debug_mask)
908 clock_debug_print_enabled();
Karthik Parsha6fb932d2012-01-24 18:04:12 -0800909
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800910 collapsed = msm_pm_power_collapse(true);
911 timer_halted = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700912
Venkat Devarasetty849f47f2013-05-19 17:44:18 +0530913 if (collapsed)
914 exit_stat = MSM_PM_STAT_IDLE_POWER_COLLAPSE;
915 else
916 exit_stat = MSM_PM_STAT_IDLE_FAILED_POWER_COLLAPSE;
917
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800918 msm_pm_timer_exit_idle(timer_halted);
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700919 break;
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700920
921 case MSM_PM_SLEEP_MODE_NOT_SELECTED:
922 goto cpuidle_enter_bail;
923 break;
924
925 default:
926 __WARN();
927 goto cpuidle_enter_bail;
928 break;
929 }
930
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800931 if (pm_sleep_ops.exit_sleep)
932 pm_sleep_ops.exit_sleep(msm_pm_idle_rs_limits, true,
933 notify_rpm, collapsed);
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700934
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800935 time = ktime_to_ns(ktime_get()) - time;
936 msm_pm_ftrace_lpm_exit(smp_processor_id(), sleep_mode, collapsed);
937 if (exit_stat >= 0)
938 msm_pm_add_stat(exit_stat, time);
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700939 do_div(time, 1000);
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800940 dev->last_residency = (int) time;
941 return sleep_mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700942
943cpuidle_enter_bail:
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800944 dev->last_residency = 0;
945 if (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE)
946 msm_pm_timer_exit_idle(timer_halted);
947 sleep_mode = MSM_PM_SLEEP_MODE_NOT_SELECTED;
948 return sleep_mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700949}
950
Anji Jonnala02dac8d2013-03-06 21:31:04 +0530951int msm_pm_wait_cpu_shutdown(unsigned int cpu)
952{
Mahesh Sivasubramanian32b9d422013-05-20 13:50:16 -0600953 int timeout = 0;
Anji Jonnala02dac8d2013-03-06 21:31:04 +0530954
955 if (!msm_pm_slp_sts)
956 return 0;
957 if (!msm_pm_slp_sts[cpu].base_addr)
958 return 0;
Mahesh Sivasubramanian32b9d422013-05-20 13:50:16 -0600959 while (1) {
Anji Jonnala02dac8d2013-03-06 21:31:04 +0530960 /*
961 * Check for the SPM of the core being hotplugged to set
962 * its sleep state.The SPM sleep state indicates that the
963 * core has been power collapsed.
964 */
965 int acc_sts = __raw_readl(msm_pm_slp_sts[cpu].base_addr);
966
967 if (acc_sts & msm_pm_slp_sts[cpu].mask)
968 return 0;
Mahesh Sivasubramanian65187f92013-03-19 15:14:06 -0600969 udelay(100);
Priyanka Mathure9dfe2b2013-05-24 15:25:57 -0700970 WARN(++timeout == 20, "CPU%u didn't collape within 2ms\n",
Mahesh Sivasubramanian32b9d422013-05-20 13:50:16 -0600971 cpu);
Anji Jonnala02dac8d2013-03-06 21:31:04 +0530972 }
973
Anji Jonnala02dac8d2013-03-06 21:31:04 +0530974 return -EBUSY;
975}
976
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700977void msm_pm_cpu_enter_lowpower(unsigned int cpu)
978{
979 int i;
980 bool allow[MSM_PM_SLEEP_MODE_NR];
981
982 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
983 struct msm_pm_platform_data *mode;
984
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600985 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700986 allow[i] = mode->suspend_supported && mode->suspend_enabled;
987 }
988
989 if (MSM_PM_DEBUG_HOTPLUG & msm_pm_debug_mask)
990 pr_notice("CPU%u: %s: shutting down cpu\n", cpu, __func__);
991
Matt Wagantall5375aa72012-07-02 19:59:51 -0700992 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700993 msm_pm_power_collapse(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -0700994 else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700995 msm_pm_power_collapse_standalone(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -0700996 else if (allow[MSM_PM_SLEEP_MODE_RETENTION])
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600997 msm_pm_retention();
Stephen Boydbda74272012-08-09 14:01:27 -0700998 else
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700999 msm_pm_swfi();
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -07001000}
1001
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -07001002static void msm_pm_ack_retention_disable(void *data)
1003{
1004 /*
1005 * This is a NULL function to ensure that the core has woken up
1006 * and is safe to disable retention.
1007 */
1008}
1009/**
1010 * msm_pm_enable_retention() - Disable/Enable retention on all cores
1011 * @enable: Enable/Disable retention
1012 *
1013 */
1014void msm_pm_enable_retention(bool enable)
1015{
Matt Wagantalld06f5c22013-03-14 19:31:14 -07001016 if (enable == msm_pm_ldo_retention_enabled)
1017 return;
1018
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -07001019 msm_pm_ldo_retention_enabled = enable;
1020 /*
1021 * If retention is being disabled, wakeup all online core to ensure
1022 * that it isn't executing retention. Offlined cores need not be woken
1023 * up as they enter the deepest sleep mode, namely RPM assited power
1024 * collapse
1025 */
Mahesh Sivasubramanian96f67d12013-03-14 22:44:42 -06001026 if (!enable) {
1027 preempt_disable();
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -07001028 smp_call_function_many(cpu_online_mask,
1029 msm_pm_ack_retention_disable,
1030 NULL, true);
Mahesh Sivasubramanian96f67d12013-03-14 22:44:42 -06001031 preempt_enable();
1032
1033
1034 }
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -07001035}
1036EXPORT_SYMBOL(msm_pm_enable_retention);
1037
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001038static int msm_pm_enter(suspend_state_t state)
1039{
1040 bool allow[MSM_PM_SLEEP_MODE_NR];
1041 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001042 int64_t period = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301043 int64_t time = msm_pm_timer_enter_suspend(&period);
Girish Mahadevandc318fd2012-08-17 16:48:05 -06001044 struct msm_pm_time_params time_param;
1045
1046 time_param.latency_us = -1;
1047 time_param.sleep_us = -1;
1048 time_param.next_event_us = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001049
1050 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1051 pr_info("%s\n", __func__);
1052
1053 if (smp_processor_id()) {
1054 __WARN();
1055 goto enter_exit;
1056 }
1057
1058
1059 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
1060 struct msm_pm_platform_data *mode;
1061
Praveen Chidambaram42da9d22012-03-30 12:16:34 -06001062 mode = &msm_pm_sleep_modes[MSM_PM_MODE(0, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001063 allow[i] = mode->suspend_supported && mode->suspend_enabled;
1064 }
1065
1066 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001067 void *rs_limits = NULL;
1068 int ret = -ENODEV;
1069 uint32_t power;
Mahesh Sivasubramanian03698762013-04-18 16:18:46 -06001070 uint32_t msm_pm_max_sleep_time = 0;
Venkat Devarasetty849f47f2013-05-19 17:44:18 +05301071 int collapsed = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001072
1073 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1074 pr_info("%s: power collapse\n", __func__);
1075
1076 clock_debug_print_enabled();
1077
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001078 if (msm_pm_sleep_time_override > 0) {
1079 int64_t ns = NSEC_PER_SEC *
1080 (int64_t) msm_pm_sleep_time_override;
Mahesh Sivasubramanian03698762013-04-18 16:18:46 -06001081 do_div(ns, NSEC_PER_SEC / SCLK_HZ);
1082 msm_pm_max_sleep_time = (uint32_t) ns;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001083 }
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001084
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001085 if (pm_sleep_ops.lowest_limits)
1086 rs_limits = pm_sleep_ops.lowest_limits(false,
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001087 MSM_PM_SLEEP_MODE_POWER_COLLAPSE, &time_param, &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001088
1089 if (rs_limits) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001090 if (pm_sleep_ops.enter_sleep)
1091 ret = pm_sleep_ops.enter_sleep(
1092 msm_pm_max_sleep_time,
1093 rs_limits, false, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001094 if (!ret) {
Venkat Devarasetty849f47f2013-05-19 17:44:18 +05301095 collapsed = msm_pm_power_collapse(false);
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001096 if (pm_sleep_ops.exit_sleep) {
1097 pm_sleep_ops.exit_sleep(rs_limits,
1098 false, true, collapsed);
1099 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001100 }
1101 } else {
1102 pr_err("%s: cannot find the lowest power limit\n",
1103 __func__);
1104 }
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301105 time = msm_pm_timer_exit_suspend(time, period);
Venkat Devarasetty849f47f2013-05-19 17:44:18 +05301106 if (collapsed)
1107 msm_pm_add_stat(MSM_PM_STAT_SUSPEND, time);
1108 else
1109 msm_pm_add_stat(MSM_PM_STAT_FAILED_SUSPEND, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001110 } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
1111 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1112 pr_info("%s: standalone power collapse\n", __func__);
1113 msm_pm_power_collapse_standalone(false);
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -06001114 } else if (allow[MSM_PM_SLEEP_MODE_RETENTION]) {
1115 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1116 pr_info("%s: retention\n", __func__);
1117 msm_pm_retention();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001118 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
1119 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1120 pr_info("%s: swfi\n", __func__);
1121 msm_pm_swfi();
1122 }
1123
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001124enter_exit:
1125 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1126 pr_info("%s: return\n", __func__);
1127
1128 return 0;
1129}
1130
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001131void msm_pm_set_sleep_ops(struct msm_pm_sleep_ops *ops)
1132{
1133 if (ops)
1134 pm_sleep_ops = *ops;
1135}
1136
Badhri Jagan Sridharan9b186af2013-05-24 12:47:35 -07001137int msm_suspend_prepare(void)
1138{
1139 if (pnoc_clk != NULL)
1140 clk_disable_unprepare(pnoc_clk);
1141 return 0;
1142}
1143
1144void msm_suspend_wake(void)
1145{
1146 if (pnoc_clk != NULL)
1147 clk_prepare_enable(pnoc_clk);
1148}
1149
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001150static const struct platform_suspend_ops msm_pm_ops = {
1151 .enter = msm_pm_enter,
1152 .valid = suspend_valid_only_mem,
Badhri Jagan Sridharan9b186af2013-05-24 12:47:35 -07001153 .prepare_late = msm_suspend_prepare,
1154 .wake = msm_suspend_wake,
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -06001155};
Girish Mahadevanec50d2d2013-05-15 12:07:38 -06001156
1157static int __devinit msm_pm_snoc_client_probe(struct platform_device *pdev)
1158{
1159 int rc = 0;
1160 static struct msm_bus_scale_pdata *msm_pm_bus_pdata;
1161 static uint32_t msm_pm_bus_client;
1162
1163 msm_pm_bus_pdata = msm_bus_cl_get_pdata(pdev);
1164
1165 if (msm_pm_bus_pdata) {
1166 msm_pm_bus_client =
1167 msm_bus_scale_register_client(msm_pm_bus_pdata);
1168
1169 if (!msm_pm_bus_client) {
1170 pr_err("%s: Failed to register SNOC client",
1171 __func__);
1172 rc = -ENXIO;
1173 goto snoc_cl_probe_done;
1174 }
1175
1176 rc = msm_bus_scale_client_update_request(msm_pm_bus_client, 1);
1177
1178 if (rc)
1179 pr_err("%s: Error setting bus rate", __func__);
1180 }
1181
1182snoc_cl_probe_done:
1183 return rc;
1184}
1185
Anji Jonnala02dac8d2013-03-06 21:31:04 +05301186static int __devinit msm_cpu_status_probe(struct platform_device *pdev)
1187{
1188 struct msm_pm_sleep_status_data *pdata;
1189 char *key;
1190 u32 cpu;
1191
1192 if (!pdev)
1193 return -EFAULT;
1194
1195 msm_pm_slp_sts =
1196 kzalloc(sizeof(*msm_pm_slp_sts) * num_possible_cpus(),
1197 GFP_KERNEL);
1198
1199 if (!msm_pm_slp_sts)
1200 return -ENOMEM;
1201
1202 if (pdev->dev.of_node) {
1203 struct resource *res;
1204 u32 offset;
1205 int rc;
1206 u32 mask;
1207
1208 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1209 if (!res)
1210 goto fail_free_mem;
1211
1212 key = "qcom,cpu-alias-addr";
1213 rc = of_property_read_u32(pdev->dev.of_node, key, &offset);
1214
1215 if (rc)
1216 goto fail_free_mem;
1217
1218 key = "qcom,sleep-status-mask";
1219 rc = of_property_read_u32(pdev->dev.of_node, key,
1220 &mask);
1221 if (rc)
1222 goto fail_free_mem;
1223
1224 for_each_possible_cpu(cpu) {
1225 msm_pm_slp_sts[cpu].base_addr =
1226 ioremap(res->start + cpu * offset,
1227 resource_size(res));
1228 msm_pm_slp_sts[cpu].mask = mask;
1229
1230 if (!msm_pm_slp_sts[cpu].base_addr)
1231 goto failed_of_node;
1232 }
1233
1234 } else {
1235 pdata = pdev->dev.platform_data;
1236 if (!pdev->dev.platform_data)
1237 goto fail_free_mem;
1238
1239 for_each_possible_cpu(cpu) {
1240 msm_pm_slp_sts[cpu].base_addr =
1241 pdata->base_addr + cpu * pdata->cpu_offset;
1242 msm_pm_slp_sts[cpu].mask = pdata->mask;
1243 }
1244 }
1245
1246 return 0;
1247
1248failed_of_node:
1249 pr_info("%s(): Failed to key=%s\n", __func__, key);
1250 for_each_possible_cpu(cpu) {
1251 if (msm_pm_slp_sts[cpu].base_addr)
1252 iounmap(msm_pm_slp_sts[cpu].base_addr);
1253 }
1254fail_free_mem:
1255 kfree(msm_pm_slp_sts);
1256 return -EINVAL;
1257
1258};
1259
1260static struct of_device_id msm_slp_sts_match_tbl[] = {
1261 {.compatible = "qcom,cpu-sleep-status"},
1262 {},
1263};
1264
1265static struct platform_driver msm_cpu_status_driver = {
1266 .probe = msm_cpu_status_probe,
1267 .driver = {
1268 .name = "cpu_slp_status",
1269 .owner = THIS_MODULE,
1270 .of_match_table = msm_slp_sts_match_tbl,
1271 },
1272};
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -06001273
Girish Mahadevanec50d2d2013-05-15 12:07:38 -06001274static struct of_device_id msm_snoc_clnt_match_tbl[] = {
1275 {.compatible = "qcom,pm-snoc-client"},
1276 {},
1277};
1278
1279static struct platform_driver msm_cpu_pm_snoc_client_driver = {
1280 .probe = msm_pm_snoc_client_probe,
1281 .driver = {
1282 .name = "pm_snoc_client",
1283 .owner = THIS_MODULE,
1284 .of_match_table = msm_snoc_clnt_match_tbl,
1285 },
1286};
1287
1288
Praveen Chidambaramede45d02013-02-20 17:45:21 -07001289static int __init msm_pm_setup_saved_state(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001290{
1291 pgd_t *pc_pgd;
1292 pmd_t *pmd;
1293 unsigned long pmdval;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001294 unsigned long exit_phys;
Laura Abbott2ff1d122013-04-24 16:13:03 -07001295 dma_addr_t temp_phys;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301296
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001297 /* Page table for cores to come back up safely. */
1298 pc_pgd = pgd_alloc(&init_mm);
1299 if (!pc_pgd)
1300 return -ENOMEM;
1301
Steve Mucklef132c6c2012-06-06 18:30:57 -07001302 exit_phys = virt_to_phys(msm_pm_collapse_exit);
1303
1304 pmd = pmd_offset(pud_offset(pc_pgd + pgd_index(exit_phys),exit_phys),
1305 exit_phys);
1306 pmdval = (exit_phys & PGDIR_MASK) |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001307 PMD_TYPE_SECT | PMD_SECT_AP_WRITE;
1308 pmd[0] = __pmd(pmdval);
1309 pmd[1] = __pmd(pmdval + (1 << (PGDIR_SHIFT - 1)));
1310
Laura Abbott2ff1d122013-04-24 16:13:03 -07001311 msm_saved_state = dma_zalloc_coherent(NULL, CPU_SAVED_STATE_SIZE *
1312 num_possible_cpus(),
1313 &temp_phys, 0);
1314
Steve Mucklefcece052012-02-18 20:09:58 -08001315 if (!msm_saved_state)
1316 return -ENOMEM;
1317
Laura Abbott2ff1d122013-04-24 16:13:03 -07001318 /*
1319 * Explicitly cast here since msm_saved_state_phys is defined
1320 * in assembly and we want to avoid any kind of truncation
1321 * or endian problems.
1322 */
1323 msm_saved_state_phys = (unsigned long)temp_phys;
1324
1325
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001326 /* It is remotely possible that the code in msm_pm_collapse_exit()
1327 * which turns on the MMU with this mapping is in the
1328 * next even-numbered megabyte beyond the
1329 * start of msm_pm_collapse_exit().
1330 * Map this megabyte in as well.
1331 */
1332 pmd[2] = __pmd(pmdval + (2 << (PGDIR_SHIFT - 1)));
1333 flush_pmd_entry(pmd);
1334 msm_pm_pc_pgd = virt_to_phys(pc_pgd);
Steve Muckle730ad7a2012-02-21 15:26:37 -08001335 clean_caches((unsigned long)&msm_pm_pc_pgd, sizeof(msm_pm_pc_pgd),
1336 virt_to_phys(&msm_pm_pc_pgd));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001337
Praveen Chidambaramede45d02013-02-20 17:45:21 -07001338 return 0;
1339}
Laura Abbott2ff1d122013-04-24 16:13:03 -07001340arch_initcall(msm_pm_setup_saved_state);
Praveen Chidambaramede45d02013-02-20 17:45:21 -07001341
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001342static void setup_broadcast_timer(void *arg)
1343{
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001344 int cpu = smp_processor_id();
1345
Mahesh Sivasubramanian738eaf62013-04-30 12:42:59 -06001346 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ON, &cpu);
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001347}
1348
1349static int setup_broadcast_cpuhp_notify(struct notifier_block *n,
1350 unsigned long action, void *hcpu)
1351{
Mahesh Sivasubramanian738eaf62013-04-30 12:42:59 -06001352 int cpu = (unsigned long)hcpu;
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001353
1354 switch (action & ~CPU_TASKS_FROZEN) {
1355 case CPU_ONLINE:
Mahesh Sivasubramanian738eaf62013-04-30 12:42:59 -06001356 smp_call_function_single(cpu, setup_broadcast_timer, NULL, 1);
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001357 break;
1358 }
1359
1360 return NOTIFY_OK;
1361}
1362
1363static struct notifier_block setup_broadcast_notifier = {
1364 .notifier_call = setup_broadcast_cpuhp_notify,
1365};
1366
Praveen Chidambaramede45d02013-02-20 17:45:21 -07001367static int __init msm_pm_init(void)
1368{
1369 enum msm_pm_time_stats_id enable_stats[] = {
1370 MSM_PM_STAT_IDLE_WFI,
1371 MSM_PM_STAT_RETENTION,
1372 MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE,
1373 MSM_PM_STAT_IDLE_POWER_COLLAPSE,
1374 MSM_PM_STAT_SUSPEND,
1375 };
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001376 msm_pm_mode_sysfs_add();
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301377 msm_pm_add_stats(enable_stats, ARRAY_SIZE(enable_stats));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001378 suspend_set_ops(&msm_pm_ops);
Girish Mahadevan32493612013-05-10 14:37:17 -06001379 hrtimer_init(&pm_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001380 msm_cpuidle_init();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001381
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001382 if (msm_pm_pc_reset_timer) {
Mahesh Sivasubramanian738eaf62013-04-30 12:42:59 -06001383 on_each_cpu(setup_broadcast_timer, NULL, 1);
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001384 register_cpu_notifier(&setup_broadcast_notifier);
1385 }
1386
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001387 return 0;
1388}
1389
Girish Mahadevan55944992012-10-26 11:03:07 -06001390static void __devinit msm_pm_set_flush_fn(uint32_t pc_mode)
1391{
1392 msm_pm_disable_l2_fn = NULL;
1393 msm_pm_enable_l2_fn = NULL;
1394 msm_pm_flush_l2_fn = outer_flush_all;
1395
1396 if (pc_mode == MSM_PM_PC_NOTZ_L2_EXT) {
1397 msm_pm_disable_l2_fn = outer_disable;
1398 msm_pm_enable_l2_fn = outer_resume;
1399 }
1400}
1401
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001402struct msm_pc_debug_counters_buffer {
1403 void __iomem *reg;
1404 u32 len;
1405 char buf[MAX_BUF_SIZE];
1406};
1407
1408static inline u32 msm_pc_debug_counters_read_register(
1409 void __iomem *reg, int index , int offset)
1410{
1411 return readl_relaxed(reg + (index * 4 + offset) * 4);
1412}
1413
1414static char *counter_name[] = {
1415 "PC Entry Counter",
1416 "Warmboot Entry Counter",
1417 "PC Bailout Counter"
1418};
1419
1420static int msm_pc_debug_counters_copy(
1421 struct msm_pc_debug_counters_buffer *data)
1422{
1423 int j;
1424 u32 stat;
1425 unsigned int cpu;
1426
1427 for_each_possible_cpu(cpu) {
1428 data->len += scnprintf(data->buf + data->len,
1429 sizeof(data->buf)-data->len,
1430 "CPU%d\n", cpu);
1431
1432 for (j = 0; j < NUM_OF_COUNTERS; j++) {
1433 stat = msm_pc_debug_counters_read_register(
1434 data->reg, cpu, j);
1435 data->len += scnprintf(data->buf + data->len,
1436 sizeof(data->buf)-data->len,
1437 "\t%s : %d\n", counter_name[j],
1438 stat);
1439 }
1440
1441 }
1442
1443 return data->len;
1444}
1445
1446static int msm_pc_debug_counters_file_read(struct file *file,
1447 char __user *bufu, size_t count, loff_t *ppos)
1448{
1449 struct msm_pc_debug_counters_buffer *data;
1450
1451 data = file->private_data;
1452
1453 if (!data)
1454 return -EINVAL;
1455
Mahesh Sivasubramanian6900fdc2013-04-18 14:02:52 -06001456 if (!bufu)
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001457 return -EINVAL;
1458
1459 if (!access_ok(VERIFY_WRITE, bufu, count))
1460 return -EFAULT;
1461
1462 if (*ppos >= data->len && data->len == 0)
1463 data->len = msm_pc_debug_counters_copy(data);
1464
1465 return simple_read_from_buffer(bufu, count, ppos,
1466 data->buf, data->len);
1467}
1468
1469static int msm_pc_debug_counters_file_open(struct inode *inode,
1470 struct file *file)
1471{
1472 struct msm_pc_debug_counters_buffer *buf;
1473 void __iomem *msm_pc_debug_counters_reg;
1474
1475 msm_pc_debug_counters_reg = inode->i_private;
1476
1477 if (!msm_pc_debug_counters_reg)
1478 return -EINVAL;
1479
1480 file->private_data = kzalloc(
1481 sizeof(struct msm_pc_debug_counters_buffer), GFP_KERNEL);
1482
1483 if (!file->private_data) {
1484 pr_err("%s: ERROR kmalloc failed to allocate %d bytes\n",
1485 __func__, sizeof(struct msm_pc_debug_counters_buffer));
1486
1487 return -ENOMEM;
1488 }
1489
1490 buf = file->private_data;
1491 buf->reg = msm_pc_debug_counters_reg;
1492
1493 return 0;
1494}
1495
1496static int msm_pc_debug_counters_file_close(struct inode *inode,
1497 struct file *file)
1498{
1499 kfree(file->private_data);
1500 return 0;
1501}
1502
1503static const struct file_operations msm_pc_debug_counters_fops = {
1504 .open = msm_pc_debug_counters_file_open,
1505 .read = msm_pc_debug_counters_file_read,
1506 .release = msm_pc_debug_counters_file_close,
1507 .llseek = no_llseek,
1508};
1509
Girish Mahadevan55944992012-10-26 11:03:07 -06001510static int __devinit msm_pm_8x60_probe(struct platform_device *pdev)
1511{
1512 char *key = NULL;
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001513 struct dentry *dent = NULL;
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001514 struct resource *res = NULL;
1515 int i ;
1516 struct msm_pm_init_data_type pdata_local;
Girish Mahadevan55944992012-10-26 11:03:07 -06001517 int ret = 0;
1518
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001519 memset(&pdata_local, 0, sizeof(struct msm_pm_init_data_type));
1520
1521 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1522 if (res) {
1523 msm_pc_debug_counters_phys = res->start;
1524 WARN_ON(resource_size(res) < SZ_64);
1525 msm_pc_debug_counters = devm_ioremap(&pdev->dev, res->start,
1526 resource_size(res));
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001527 if (msm_pc_debug_counters)
1528 for (i = 0; i < resource_size(res)/4; i++)
1529 __raw_writel(0, msm_pc_debug_counters + i * 4);
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001530
1531 }
1532
1533 if (!msm_pc_debug_counters) {
1534 msm_pc_debug_counters = 0;
1535 msm_pc_debug_counters_phys = 0;
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001536 } else {
1537 dent = debugfs_create_file("pc_debug_counter", S_IRUGO, NULL,
1538 msm_pc_debug_counters,
1539 &msm_pc_debug_counters_fops);
1540 if (!dent)
1541 pr_err("%s: ERROR debugfs_create_file failed\n",
1542 __func__);
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001543 }
1544
Girish Mahadevan55944992012-10-26 11:03:07 -06001545 if (!pdev->dev.of_node) {
1546 struct msm_pm_init_data_type *d = pdev->dev.platform_data;
1547
1548 if (!d)
1549 goto pm_8x60_probe_done;
1550
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001551 memcpy(&pdata_local, d, sizeof(struct msm_pm_init_data_type));
1552
Girish Mahadevan55944992012-10-26 11:03:07 -06001553 } else {
1554 key = "qcom,pc-mode";
Archana Sathyakumar2b91dc82013-02-01 17:38:23 -07001555 ret = msm_pm_get_pc_mode(pdev->dev.of_node,
1556 key,
1557 &pdata_local.pc_mode);
Girish Mahadevan55944992012-10-26 11:03:07 -06001558 if (ret) {
Archana Sathyakumar2b91dc82013-02-01 17:38:23 -07001559 pr_debug("%s: Error reading key %s",
Girish Mahadevan55944992012-10-26 11:03:07 -06001560 __func__, key);
Archana Sathyakumar2b91dc82013-02-01 17:38:23 -07001561 return -EINVAL;
Girish Mahadevan55944992012-10-26 11:03:07 -06001562 }
Girish Mahadevan55944992012-10-26 11:03:07 -06001563
1564 key = "qcom,use-sync-timer";
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001565 pdata_local.use_sync_timer =
Girish Mahadevan55944992012-10-26 11:03:07 -06001566 of_property_read_bool(pdev->dev.of_node, key);
Praveen Chidambaram4b8df032013-01-18 16:21:16 -07001567
1568 key = "qcom,saw-turns-off-pll";
1569 msm_no_ramp_down_pc = of_property_read_bool(pdev->dev.of_node,
1570 key);
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001571
1572 key = "qcom,pc-resets-timer";
1573 msm_pm_pc_reset_timer = of_property_read_bool(
1574 pdev->dev.of_node, key);
Girish Mahadevan55944992012-10-26 11:03:07 -06001575 }
1576
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001577 if (pdata_local.cp15_data.reg_data &&
1578 pdata_local.cp15_data.reg_saved_state_size > 0) {
1579 cp15_data.reg_data = kzalloc(sizeof(uint32_t) *
1580 pdata_local.cp15_data.reg_saved_state_size,
1581 GFP_KERNEL);
1582 if (!cp15_data.reg_data)
1583 return -ENOMEM;
1584
1585 cp15_data.reg_val = kzalloc(sizeof(uint32_t) *
1586 pdata_local.cp15_data.reg_saved_state_size,
1587 GFP_KERNEL);
1588 if (cp15_data.reg_val)
1589 return -ENOMEM;
1590
1591 memcpy(cp15_data.reg_data, pdata_local.cp15_data.reg_data,
1592 pdata_local.cp15_data.reg_saved_state_size *
1593 sizeof(uint32_t));
1594 }
1595
1596 msm_pm_set_flush_fn(pdata_local.pc_mode);
1597 msm_pm_use_sync_timer = pdata_local.use_sync_timer;
1598 msm_pm_retention_calls_tz = pdata_local.retention_calls_tz;
1599
Girish Mahadevan55944992012-10-26 11:03:07 -06001600pm_8x60_probe_done:
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001601 msm_pm_init();
Anji Jonnala02dac8d2013-03-06 21:31:04 +05301602 if (pdev->dev.of_node)
1603 of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
1604
Girish Mahadevan55944992012-10-26 11:03:07 -06001605 return ret;
1606}
1607
1608static struct of_device_id msm_pm_8x60_table[] = {
1609 {.compatible = "qcom,pm-8x60"},
1610 {},
1611};
1612
1613static struct platform_driver msm_pm_8x60_driver = {
1614 .probe = msm_pm_8x60_probe,
1615 .driver = {
1616 .name = "pm-8x60",
1617 .owner = THIS_MODULE,
1618 .of_match_table = msm_pm_8x60_table,
1619 },
1620};
1621
1622static int __init msm_pm_8x60_init(void)
1623{
Anji Jonnala02dac8d2013-03-06 21:31:04 +05301624 int rc;
1625
Girish Mahadevanec50d2d2013-05-15 12:07:38 -06001626 rc = platform_driver_register(&msm_cpu_pm_snoc_client_driver);
1627
1628 if (rc) {
1629 pr_err("%s(): failed to register driver %s\n", __func__,
1630 msm_cpu_pm_snoc_client_driver.driver.name);
1631 return rc;
1632 }
1633
Badhri Jagan Sridharan9b186af2013-05-24 12:47:35 -07001634 pnoc_clk = clk_get_sys("pm_8x60", "bus_clk");
1635
1636 if (IS_ERR(pnoc_clk))
1637 pnoc_clk = NULL;
1638 else {
1639 clk_set_rate(pnoc_clk, 19200000);
1640 rc = clk_prepare_enable(pnoc_clk);
1641
1642 if (rc)
1643 pr_err("%s: PNOC clock enable failed\n", __func__);
1644 }
1645
Girish Mahadevan55944992012-10-26 11:03:07 -06001646 return platform_driver_register(&msm_pm_8x60_driver);
1647}
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001648device_initcall(msm_pm_8x60_init);
Mahesh Sivasubramanian59ffcb02013-05-31 15:08:15 -06001649
1650void __init msm_pm_sleep_status_init(void)
1651{
1652 platform_driver_register(&msm_cpu_status_driver);
1653}