blob: aebd61ffb9b19b16e3d7aa81cb6ad685bdbe19d6 [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>
Mahesh Sivasubramanian3cf21562013-07-19 10:44:26 -060019#include <linux/clk.h>
20#include <linux/clkdev.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070021#include <linux/completion.h>
22#include <linux/cpuidle.h>
23#include <linux/interrupt.h>
24#include <linux/io.h>
25#include <linux/ktime.h>
26#include <linux/pm.h>
Steve Mucklef132c6c2012-06-06 18:30:57 -070027#include <linux/pm_qos.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070028#include <linux/smp.h>
29#include <linux/suspend.h>
30#include <linux/tick.h>
Anji Jonnala02dac8d2013-03-06 21:31:04 +053031#include <linux/delay.h>
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -060032#include <linux/platform_device.h>
Anji Jonnala02dac8d2013-03-06 21:31:04 +053033#include <linux/of_platform.h>
Abhijeet Dharmapurikare8f83852013-02-06 18:57:17 -080034#include <linux/regulator/krait-regulator.h>
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -060035#include <linux/cpu.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070036#include <mach/msm_iomap.h>
Praveen Chidambaram192979f2012-04-25 18:30:23 -060037#include <mach/socinfo.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070038#include <mach/system.h>
Girish Mahadevand27ca4a2012-08-15 09:21:23 -060039#include <mach/scm.h>
40#include <mach/socinfo.h>
Hanumant Singhbd82a3e2013-02-26 13:51:17 -080041#define CREATE_TRACE_POINTS
42#include <mach/trace_msm_low_power.h>
Praveen Chidambaramc594a092012-09-18 19:48:29 -060043#include <mach/msm-krait-l2-accessors.h>
Girish Mahadevanec50d2d2013-05-15 12:07:38 -060044#include <mach/msm_bus.h>
Mahesh Sivasubramanian3855da12013-04-26 11:40:27 -060045#include <mach/mpm.h>
Mahesh Sivasubramanian56a35432013-10-15 14:36:58 -060046#include <mach/jtag.h>
47#include <asm/suspend.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070048#include <asm/cacheflush.h>
49#include <asm/hardware/gic.h>
50#include <asm/pgtable.h>
51#include <asm/pgalloc.h>
Mahesh Sivasubramanian56a35432013-10-15 14:36:58 -060052#include <asm/barrier.h>
Girish Mahadevan55944992012-10-26 11:03:07 -060053#include <asm/outercache.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070054#ifdef CONFIG_VFP
55#include <asm/vfp.h>
56#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070057#include "acpuclock.h"
58#include "clock.h"
59#include "avs.h"
Abhijeet Dharmapurikarefaca4f2011-12-27 16:24:07 -080060#include <mach/cpuidle.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070061#include "idle.h"
Matt Wagantall7cca4642012-02-01 16:43:24 -080062#include "pm.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070063#include "scm-boot.h"
64#include "spm.h"
65#include "timer.h"
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -060066#include "pm-boot.h"
Girish Mahadevandc318fd2012-08-17 16:48:05 -060067#include <mach/event_timer.h>
Venkat Devarasetty722f91f2013-06-03 19:41:27 +053068#include <linux/cpu_pm.h>
Hanumant Singhbd82a3e2013-02-26 13:51:17 -080069
Praveen Chidambaramf27a5152013-02-01 11:44:53 -070070#define SCM_CMD_TERMINATE_PC (0x2)
Mahesh Sivasubramanian56a35432013-10-15 14:36:58 -060071#define SCM_CMD_CORE_HOTPLUGGED (0x10)
Praveen Chidambaramf27a5152013-02-01 11:44:53 -070072
73#define GET_CPU_OF_ATTR(attr) \
74 (container_of(attr, struct msm_pm_kobj_attribute, ka)->cpu)
75
76#define SCLK_HZ (32768)
Praveen Chidambaramf27a5152013-02-01 11:44:53 -070077
Priyanka Mathur13bdad12013-01-28 15:52:56 -080078#define MAX_BUF_SIZE 512
79
Praveen Chidambaramf27a5152013-02-01 11:44:53 -070080static int msm_pm_debug_mask = 1;
81module_param_named(
82 debug_mask, msm_pm_debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP
83);
84
85static int msm_pm_sleep_time_override;
86module_param_named(sleep_time_override,
87 msm_pm_sleep_time_override, int, S_IRUGO | S_IWUSR | S_IWGRP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070088
Mahesh Sivasubramanian3cf21562013-07-19 10:44:26 -060089static bool use_acpuclk_apis;
90
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070091enum {
92 MSM_PM_DEBUG_SUSPEND = BIT(0),
93 MSM_PM_DEBUG_POWER_COLLAPSE = BIT(1),
94 MSM_PM_DEBUG_SUSPEND_LIMITS = BIT(2),
95 MSM_PM_DEBUG_CLOCK = BIT(3),
96 MSM_PM_DEBUG_RESET_VECTOR = BIT(4),
Karthik Parsha6fb932d2012-01-24 18:04:12 -080097 MSM_PM_DEBUG_IDLE_CLK = BIT(5),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070098 MSM_PM_DEBUG_IDLE = BIT(6),
99 MSM_PM_DEBUG_IDLE_LIMITS = BIT(7),
100 MSM_PM_DEBUG_HOTPLUG = BIT(8),
101};
102
Mahesh Sivasubramanian56a35432013-10-15 14:36:58 -0600103enum msm_pc_count_offsets {
104 MSM_PC_ENTRY_COUNTER,
105 MSM_PC_EXIT_COUNTER,
106 MSM_PC_FALLTHRU_COUNTER,
107 MSM_PC_NUM_COUNTERS,
108};
109
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700110enum {
111 MSM_PM_MODE_ATTR_SUSPEND,
112 MSM_PM_MODE_ATTR_IDLE,
113 MSM_PM_MODE_ATTR_NR,
114};
115
116static char *msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_NR] = {
117 [MSM_PM_MODE_ATTR_SUSPEND] = "suspend_enabled",
118 [MSM_PM_MODE_ATTR_IDLE] = "idle_enabled",
119};
120
121struct msm_pm_kobj_attribute {
122 unsigned int cpu;
123 struct kobj_attribute ka;
124};
125
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700126struct msm_pm_sysfs_sleep_mode {
127 struct kobject *kobj;
128 struct attribute_group attr_group;
129 struct attribute *attrs[MSM_PM_MODE_ATTR_NR + 1];
130 struct msm_pm_kobj_attribute kas[MSM_PM_MODE_ATTR_NR];
131};
132
133static char *msm_pm_sleep_mode_labels[MSM_PM_SLEEP_MODE_NR] = {
134 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE] = "power_collapse",
135 [MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT] = "wfi",
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600136 [MSM_PM_SLEEP_MODE_RETENTION] = "retention",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700137 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE] =
138 "standalone_power_collapse",
139};
140
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600141static struct hrtimer pm_hrtimer;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600142static struct msm_pm_sleep_ops pm_sleep_ops;
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -0700143static bool msm_pm_ldo_retention_enabled = true;
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700144static bool msm_pm_use_sync_timer;
145static struct msm_pm_cp15_save_data cp15_data;
146static bool msm_pm_retention_calls_tz;
Praveen Chidambaram4b8df032013-01-18 16:21:16 -0700147static bool msm_no_ramp_down_pc;
Anji Jonnala02dac8d2013-03-06 21:31:04 +0530148static struct msm_pm_sleep_status_data *msm_pm_slp_sts;
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -0600149static bool msm_pm_pc_reset_timer;
Mahesh Sivasubramanian3cf21562013-07-19 10:44:26 -0600150DEFINE_PER_CPU(struct clk *, cpu_clks);
151static struct clk *l2_clk;
152
Mahesh Sivasubramanian56a35432013-10-15 14:36:58 -0600153static void (*msm_pm_disable_l2_fn)(void);
154static void (*msm_pm_enable_l2_fn)(void);
155static void (*msm_pm_flush_l2_fn)(void);
156static void __iomem *msm_pc_debug_counters;
157
158/*
159 * Default the l2 flush flag to OFF so the caches are flushed during power
160 * collapse unless the explicitly voted by lpm driver.
161 */
162static enum msm_pm_l2_scm_flag msm_pm_flush_l2_flag = MSM_SCM_L2_OFF;
163
164void msm_pm_set_l2_flush_flag(enum msm_pm_l2_scm_flag flag)
165{
166 msm_pm_flush_l2_flag = flag;
167}
168EXPORT_SYMBOL(msm_pm_set_l2_flush_flag);
169
170static enum msm_pm_l2_scm_flag msm_pm_get_l2_flush_flag(void)
171{
172 return msm_pm_flush_l2_flag;
173}
174
Archana Sathyakumar2b91dc82013-02-01 17:38:23 -0700175static int msm_pm_get_pc_mode(struct device_node *node,
176 const char *key, uint32_t *pc_mode_val)
177{
178 struct pc_mode_of {
179 uint32_t mode;
180 char *mode_name;
181 };
182 int i;
183 struct pc_mode_of pc_modes[] = {
184 {MSM_PM_PC_TZ_L2_INT, "tz_l2_int"},
185 {MSM_PM_PC_NOTZ_L2_EXT, "no_tz_l2_ext"},
186 {MSM_PM_PC_TZ_L2_EXT , "tz_l2_ext"} };
187 int ret;
188 const char *pc_mode_str;
189
190 ret = of_property_read_string(node, key, &pc_mode_str);
191 if (ret) {
192 pr_debug("%s: Cannot read %s,defaulting to 0", __func__, key);
193 pc_mode_val = MSM_PM_PC_TZ_L2_INT;
194 ret = 0;
195 } else {
196 ret = -EINVAL;
197 for (i = 0; i < ARRAY_SIZE(pc_modes); i++) {
198 if (!strncmp(pc_mode_str, pc_modes[i].mode_name,
199 strlen(pc_modes[i].mode_name))) {
200 *pc_mode_val = pc_modes[i].mode;
201 ret = 0;
202 break;
203 }
204 }
205 }
206 return ret;
207}
208
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700209/*
210 * Write out the attribute.
211 */
212static ssize_t msm_pm_mode_attr_show(
213 struct kobject *kobj, struct kobj_attribute *attr, char *buf)
214{
215 int ret = -EINVAL;
216 int i;
217
218 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
219 struct kernel_param kp;
220 unsigned int cpu;
221 struct msm_pm_platform_data *mode;
222
223 if (msm_pm_sleep_mode_labels[i] == NULL)
224 continue;
225
226 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
227 continue;
228
229 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600230 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700231
232 if (!strcmp(attr->attr.name,
233 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
234 u32 arg = mode->suspend_enabled;
235 kp.arg = &arg;
236 ret = param_get_ulong(buf, &kp);
237 } else if (!strcmp(attr->attr.name,
238 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
239 u32 arg = mode->idle_enabled;
240 kp.arg = &arg;
241 ret = param_get_ulong(buf, &kp);
242 }
243
244 break;
245 }
246
247 if (ret > 0) {
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600248 strlcat(buf, "\n", PAGE_SIZE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700249 ret++;
250 }
251
252 return ret;
253}
254
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700255static ssize_t msm_pm_mode_attr_store(struct kobject *kobj,
256 struct kobj_attribute *attr, const char *buf, size_t count)
257{
258 int ret = -EINVAL;
259 int i;
260
261 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
262 struct kernel_param kp;
263 unsigned int cpu;
264 struct msm_pm_platform_data *mode;
265
266 if (msm_pm_sleep_mode_labels[i] == NULL)
267 continue;
268
269 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
270 continue;
271
272 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600273 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700274
275 if (!strcmp(attr->attr.name,
276 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
277 kp.arg = &mode->suspend_enabled;
278 ret = param_set_byte(buf, &kp);
279 } else if (!strcmp(attr->attr.name,
280 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
281 kp.arg = &mode->idle_enabled;
282 ret = param_set_byte(buf, &kp);
283 }
284
285 break;
286 }
287
288 return ret ? ret : count;
289}
290
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700291static int __devinit msm_pm_mode_sysfs_add_cpu(
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700292 unsigned int cpu, struct kobject *modes_kobj)
293{
294 char cpu_name[8];
295 struct kobject *cpu_kobj;
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600296 struct msm_pm_sysfs_sleep_mode *mode = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700297 int i, j, k;
298 int ret;
299
300 snprintf(cpu_name, sizeof(cpu_name), "cpu%u", cpu);
301 cpu_kobj = kobject_create_and_add(cpu_name, modes_kobj);
302 if (!cpu_kobj) {
303 pr_err("%s: cannot create %s kobject\n", __func__, cpu_name);
304 ret = -ENOMEM;
305 goto mode_sysfs_add_cpu_exit;
306 }
307
308 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
309 int idx = MSM_PM_MODE(cpu, i);
310
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600311 if ((!msm_pm_sleep_modes[idx].suspend_supported)
312 && (!msm_pm_sleep_modes[idx].idle_supported))
313 continue;
314
315 if (!msm_pm_sleep_mode_labels[i] ||
316 !msm_pm_sleep_mode_labels[i][0])
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700317 continue;
318
319 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
320 if (!mode) {
321 pr_err("%s: cannot allocate memory for attributes\n",
322 __func__);
323 ret = -ENOMEM;
324 goto mode_sysfs_add_cpu_exit;
325 }
326
327 mode->kobj = kobject_create_and_add(
328 msm_pm_sleep_mode_labels[i], cpu_kobj);
329 if (!mode->kobj) {
330 pr_err("%s: cannot create kobject\n", __func__);
331 ret = -ENOMEM;
332 goto mode_sysfs_add_cpu_exit;
333 }
334
335 for (k = 0, j = 0; k < MSM_PM_MODE_ATTR_NR; k++) {
336 if ((k == MSM_PM_MODE_ATTR_IDLE) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600337 !msm_pm_sleep_modes[idx].idle_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700338 continue;
339 if ((k == MSM_PM_MODE_ATTR_SUSPEND) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600340 !msm_pm_sleep_modes[idx].suspend_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700341 continue;
Stephen Boydd2059c32012-07-03 15:11:15 -0700342 sysfs_attr_init(&mode->kas[j].ka.attr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700343 mode->kas[j].cpu = cpu;
344 mode->kas[j].ka.attr.mode = 0644;
345 mode->kas[j].ka.show = msm_pm_mode_attr_show;
346 mode->kas[j].ka.store = msm_pm_mode_attr_store;
347 mode->kas[j].ka.attr.name = msm_pm_mode_attr_labels[k];
348 mode->attrs[j] = &mode->kas[j].ka.attr;
349 j++;
350 }
351 mode->attrs[j] = NULL;
352
353 mode->attr_group.attrs = mode->attrs;
354 ret = sysfs_create_group(mode->kobj, &mode->attr_group);
355 if (ret) {
356 pr_err("%s: cannot create kobject attribute group\n",
357 __func__);
358 goto mode_sysfs_add_cpu_exit;
359 }
360 }
361
362 ret = 0;
363
364mode_sysfs_add_cpu_exit:
Praveen Chidambaramd5ac2d32011-10-24 14:30:27 -0600365 if (ret) {
Praveen Chidambaram2cfda632011-10-11 16:58:09 -0600366 if (mode && mode->kobj)
367 kobject_del(mode->kobj);
368 kfree(mode);
369 }
370
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700371 return ret;
372}
373
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700374int __devinit msm_pm_mode_sysfs_add(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700375{
376 struct kobject *module_kobj;
377 struct kobject *modes_kobj;
378 unsigned int cpu;
379 int ret;
380
381 module_kobj = kset_find_obj(module_kset, KBUILD_MODNAME);
382 if (!module_kobj) {
383 pr_err("%s: cannot find kobject for module %s\n",
384 __func__, KBUILD_MODNAME);
385 ret = -ENOENT;
386 goto mode_sysfs_add_exit;
387 }
388
389 modes_kobj = kobject_create_and_add("modes", module_kobj);
390 if (!modes_kobj) {
391 pr_err("%s: cannot create modes kobject\n", __func__);
392 ret = -ENOMEM;
393 goto mode_sysfs_add_exit;
394 }
395
396 for_each_possible_cpu(cpu) {
397 ret = msm_pm_mode_sysfs_add_cpu(cpu, modes_kobj);
398 if (ret)
399 goto mode_sysfs_add_exit;
400 }
401
402 ret = 0;
403
404mode_sysfs_add_exit:
405 return ret;
406}
407
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700408/*
409 * Configure hardware registers in preparation for Apps power down.
410 */
411static void msm_pm_config_hw_before_power_down(void)
412{
413 return;
414}
415
416/*
417 * Clear hardware registers after Apps powers up.
418 */
419static void msm_pm_config_hw_after_power_up(void)
420{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700421}
422
423/*
424 * Configure hardware registers in preparation for SWFI.
425 */
426static void msm_pm_config_hw_before_swfi(void)
427{
428 return;
429}
430
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600431/*
432 * Configure/Restore hardware registers in preparation for Retention.
433 */
434
435static void msm_pm_config_hw_after_retention(void)
436{
437 int ret;
Abhijeet Dharmapurikare8f83852013-02-06 18:57:17 -0800438
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600439 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
440 WARN_ON(ret);
441}
442
443static void msm_pm_config_hw_before_retention(void)
444{
445 return;
446}
447
Mahesh Sivasubramanian56a35432013-10-15 14:36:58 -0600448static bool msm_pm_is_L1_writeback(void)
449{
450 u32 sel = 0, cache_id;
451
452 asm volatile ("mcr p15, 2, %[ccselr], c0, c0, 0\n\t"
453 "isb\n\t"
454 "mrc p15, 1, %[ccsidr], c0, c0, 0\n\t"
455 :[ccsidr]"=r" (cache_id)
456 :[ccselr]"r" (sel)
457 );
458 return cache_id & BIT(31);
459}
460
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600461static void msm_pm_save_cpu_reg(void)
462{
463 int i;
464
465 /* Only on core0 */
466 if (smp_processor_id())
467 return;
468
469 /**
470 * On some targets, L2 PC will turn off may reset the core
471 * configuration for the mux and the default may not make the core
472 * happy when it resumes.
473 * Save the active vdd, and set the core vdd to QSB max vdd, so that
474 * when the core resumes, it is capable of supporting the current QSB
475 * rate. Then restore the active vdd before switching the acpuclk rate.
476 */
Mahesh Sivasubramanian56a35432013-10-15 14:36:58 -0600477 if (msm_pm_get_l2_flush_flag() == MSM_SCM_L2_OFF) {
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700478 cp15_data.active_vdd = msm_spm_get_vdd(0);
479 for (i = 0; i < cp15_data.reg_saved_state_size; i++)
480 cp15_data.reg_val[i] =
481 get_l2_indirect_reg(
482 cp15_data.reg_data[i]);
483 msm_spm_set_vdd(0, cp15_data.qsb_pc_vdd);
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600484 }
485}
486
487static void msm_pm_restore_cpu_reg(void)
488{
489 int i;
490
491 /* Only on core0 */
492 if (smp_processor_id())
493 return;
494
Mahesh Sivasubramanian56a35432013-10-15 14:36:58 -0600495 if (msm_pm_get_l2_flush_flag() == MSM_SCM_L2_OFF) {
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700496 for (i = 0; i < cp15_data.reg_saved_state_size; i++)
497 set_l2_indirect_reg(
498 cp15_data.reg_data[i],
499 cp15_data.reg_val[i]);
500 msm_spm_set_vdd(0, cp15_data.active_vdd);
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600501 }
502}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700503
Mahesh Sivasubramanian56a35432013-10-15 14:36:58 -0600504static inline void msm_arch_idle(void)
505{
506 mb();
507 wfi();
508}
509
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700510static void msm_pm_swfi(void)
511{
512 msm_pm_config_hw_before_swfi();
513 msm_arch_idle();
514}
515
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600516static void msm_pm_retention(void)
517{
518 int ret = 0;
519
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600520 msm_pm_config_hw_before_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600521 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_POWER_RETENTION, false);
522 WARN_ON(ret);
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600523
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700524 if (msm_pm_retention_calls_tz)
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600525 scm_call_atomic1(SCM_SVC_BOOT, SCM_CMD_TERMINATE_PC,
Mahesh Sivasubramanian56a35432013-10-15 14:36:58 -0600526 MSM_SCM_L2_RET);
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600527 else
528 msm_arch_idle();
529
530 msm_pm_config_hw_after_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600531}
532
Mahesh Sivasubramanian56a35432013-10-15 14:36:58 -0600533static inline void msm_pc_inc_debug_count(uint32_t cpu,
534 enum msm_pc_count_offsets offset)
535{
536 uint32_t cnt;
537
538 if (!msm_pc_debug_counters)
539 return;
540
541 cnt = readl_relaxed(msm_pc_debug_counters + cpu * 4 + offset * 4);
542 writel_relaxed(++cnt, msm_pc_debug_counters + cpu * 4 + offset * 4);
543 mb();
544}
545
546static bool msm_pm_pc_hotplug(void)
547{
548 uint32_t cpu = smp_processor_id();
549
550 if (msm_pm_is_L1_writeback())
551 flush_cache_louis();
552
553 msm_pc_inc_debug_count(cpu, MSM_PC_ENTRY_COUNTER);
554
555 scm_call_atomic1(SCM_SVC_BOOT, SCM_CMD_TERMINATE_PC,
556 SCM_CMD_CORE_HOTPLUGGED);
557
558 /* Should not return here */
559 msm_pc_inc_debug_count(cpu, MSM_PC_FALLTHRU_COUNTER);
560 return 0;
561}
562
563static int msm_pm_collapse(unsigned long unused)
564{
565 uint32_t cpu = smp_processor_id();
566
567 if (msm_pm_get_l2_flush_flag() == MSM_SCM_L2_OFF) {
568 flush_cache_all();
569 if (msm_pm_flush_l2_fn)
570 msm_pm_flush_l2_fn();
571 } else if (msm_pm_is_L1_writeback())
572 flush_cache_louis();
573
574 if (msm_pm_disable_l2_fn)
575 msm_pm_disable_l2_fn();
576
577 msm_pc_inc_debug_count(cpu, MSM_PC_ENTRY_COUNTER);
578
579 scm_call_atomic1(SCM_SVC_BOOT, SCM_CMD_TERMINATE_PC,
580 msm_pm_get_l2_flush_flag());
581
582 msm_pc_inc_debug_count(cpu, MSM_PC_FALLTHRU_COUNTER);
583
584 if (msm_pm_enable_l2_fn)
585 msm_pm_enable_l2_fn();
586
587 return 0;
588}
589
Stephen Boydb29750d2012-02-21 01:21:32 -0800590static bool __ref msm_pm_spm_power_collapse(
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700591 unsigned int cpu, bool from_idle, bool notify_rpm)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700592{
593 void *entry;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600594 bool collapsed = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700595 int ret;
Mahesh Sivasubramanianda746252013-05-02 09:58:08 -0600596 bool save_cpu_regs = !cpu || from_idle;
Matt Wagantallbbf79d72013-10-04 12:46:05 -0700597 unsigned int saved_gic_cpu_ctrl;
598
599 saved_gic_cpu_ctrl = readl_relaxed(MSM_QGIC_CPU_BASE + GIC_CPU_CTRL);
600 mb();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700601
602 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
603 pr_info("CPU%u: %s: notify_rpm %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700604 cpu, __func__, (int) notify_rpm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700605
Venkat Devarasetty722f91f2013-06-03 19:41:27 +0530606 if (from_idle == true)
607 cpu_pm_enter();
608
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700609 ret = msm_spm_set_low_power_mode(
610 MSM_SPM_MODE_POWER_COLLAPSE, notify_rpm);
611 WARN_ON(ret);
612
Mahesh Sivasubramanian56a35432013-10-15 14:36:58 -0600613 entry = save_cpu_regs ? cpu_resume : msm_secondary_startup;
Mahesh Sivasubramanianda746252013-05-02 09:58:08 -0600614
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700615 msm_pm_boot_config_before_pc(cpu, virt_to_phys(entry));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700616
617 if (MSM_PM_DEBUG_RESET_VECTOR & msm_pm_debug_mask)
618 pr_info("CPU%u: %s: program vector to %p\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700619 cpu, __func__, entry);
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -0600620 if (from_idle && msm_pm_pc_reset_timer)
621 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700622
Mahesh Sivasubramanian56a35432013-10-15 14:36:58 -0600623 msm_jtag_save_state();
624 collapsed = save_cpu_regs ?
625 !cpu_suspend(0, msm_pm_collapse) : msm_pm_pc_hotplug();
626 msm_jtag_restore_state();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700627
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -0600628 if (from_idle && msm_pm_pc_reset_timer)
629 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu);
630
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700631 msm_pm_boot_config_after_pc(cpu);
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -0600632
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700633 if (collapsed) {
Matt Wagantallbbf79d72013-10-04 12:46:05 -0700634#ifdef CONFIG_VFP
635 vfp_pm_resume();
636#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700637 cpu_init();
Matt Wagantallbbf79d72013-10-04 12:46:05 -0700638 writel(0xF0, MSM_QGIC_CPU_BASE + GIC_CPU_PRIMASK);
639 writel_relaxed(saved_gic_cpu_ctrl,
640 MSM_QGIC_CPU_BASE + GIC_CPU_CTRL);
641 mb();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700642 local_fiq_enable();
643 }
644
645 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
646 pr_info("CPU%u: %s: msm_pm_collapse returned, collapsed %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700647 cpu, __func__, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700648
649 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
650 WARN_ON(ret);
Venkat Devarasetty722f91f2013-06-03 19:41:27 +0530651
652 if (from_idle == true)
653 cpu_pm_exit();
654
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600655 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700656}
657
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600658static bool msm_pm_power_collapse_standalone(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700659{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700660 unsigned int cpu = smp_processor_id();
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700661 unsigned int avsdscr;
662 unsigned int avscsr;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600663 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700664
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700665 avsdscr = avs_get_avsdscr();
666 avscsr = avs_get_avscsr();
667 avs_set_avscsr(0); /* Disable AVS */
668
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700669 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, false);
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700670
671 avs_set_avsdscr(avsdscr);
672 avs_set_avscsr(avscsr);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600673 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700674}
675
Mahesh Sivasubramanian3cf21562013-07-19 10:44:26 -0600676static int ramp_down_last_cpu(int cpu)
677{
678 struct clk *cpu_clk = per_cpu(cpu_clks, cpu);
679 int ret = 0;
680
681 if (use_acpuclk_apis) {
682 ret = acpuclk_power_collapse();
683 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
684 pr_info("CPU%u: %s: change clk rate(old rate = %d)\n",
685 cpu, __func__, ret);
686 } else {
687 clk_disable(cpu_clk);
688 clk_disable(l2_clk);
689 }
690 return ret;
691}
692
693static int ramp_up_first_cpu(int cpu, int saved_rate)
694{
695 struct clk *cpu_clk = per_cpu(cpu_clks, cpu);
696 int rc = 0;
697
698 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
699 pr_info("CPU%u: %s: restore clock rate\n",
700 cpu, __func__);
701
702 if (use_acpuclk_apis) {
703 rc = acpuclk_set_rate(cpu, saved_rate, SETRATE_PC);
704 if (rc)
705 pr_err("CPU:%u: Error restoring cpu clk\n", cpu);
706 } else {
707 if (l2_clk) {
708 rc = clk_enable(l2_clk);
709 if (rc)
710 pr_err("%s(): Error restoring l2 clk\n",
711 __func__);
712 }
713
714 if (cpu_clk) {
715 int ret = clk_enable(cpu_clk);
716
717 if (ret) {
718 pr_err("%s(): Error restoring cpu clk\n",
719 __func__);
720 return ret;
721 }
722 }
723 }
724
725 return rc;
726}
727
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600728static bool msm_pm_power_collapse(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700729{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700730 unsigned int cpu = smp_processor_id();
Praveen Chidambaram4b8df032013-01-18 16:21:16 -0700731 unsigned long saved_acpuclk_rate = 0;
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700732 unsigned int avsdscr;
733 unsigned int avscsr;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600734 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700735
736 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
737 pr_info("CPU%u: %s: idle %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700738 cpu, __func__, (int)from_idle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700739
740 msm_pm_config_hw_before_power_down();
741 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700742 pr_info("CPU%u: %s: pre power down\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700743
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700744 avsdscr = avs_get_avsdscr();
745 avscsr = avs_get_avscsr();
746 avs_set_avscsr(0); /* Disable AVS */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700747
Praveen Chidambaram4b8df032013-01-18 16:21:16 -0700748 if (cpu_online(cpu) && !msm_no_ramp_down_pc)
Mahesh Sivasubramanian3cf21562013-07-19 10:44:26 -0600749 saved_acpuclk_rate = ramp_down_last_cpu(cpu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700750
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700751 if (cp15_data.save_cp15)
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600752 msm_pm_save_cpu_reg();
753
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700754 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700755
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700756 if (cp15_data.save_cp15)
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600757 msm_pm_restore_cpu_reg();
758
Mahesh Sivasubramanian3cf21562013-07-19 10:44:26 -0600759 if (cpu_online(cpu) && !msm_no_ramp_down_pc) {
760 ramp_up_first_cpu(cpu, saved_acpuclk_rate);
Girish Mahadevan4e025d92012-02-29 13:26:51 -0700761 } else {
762 unsigned int gic_dist_enabled;
763 unsigned int gic_dist_pending;
764 gic_dist_enabled = readl_relaxed(
765 MSM_QGIC_DIST_BASE + GIC_DIST_ENABLE_CLEAR);
766 gic_dist_pending = readl_relaxed(
767 MSM_QGIC_DIST_BASE + GIC_DIST_PENDING_SET);
768 mb();
769 gic_dist_pending &= gic_dist_enabled;
770
771 if (gic_dist_pending)
772 pr_err("CPU %d interrupted during hotplug.Pending int 0x%x\n",
773 cpu, gic_dist_pending);
774 }
775
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700776
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700777 avs_set_avsdscr(avsdscr);
778 avs_set_avscsr(avscsr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700779 msm_pm_config_hw_after_power_up();
780 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700781 pr_info("CPU%u: %s: post power up\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700782
783 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700784 pr_info("CPU%u: %s: return\n", cpu, __func__);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600785 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700786}
787
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600788static int64_t msm_pm_timer_enter_idle(void)
789{
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700790 if (msm_pm_use_sync_timer)
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600791 return ktime_to_ns(tick_nohz_get_sleep_length());
792
793 return msm_timer_enter_idle();
794}
795
796static void msm_pm_timer_exit_idle(bool timer_halted)
797{
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700798 if (msm_pm_use_sync_timer)
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600799 return;
800
801 msm_timer_exit_idle((int) timer_halted);
802}
803
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530804static int64_t msm_pm_timer_enter_suspend(int64_t *period)
805{
Anji Jonnalac02367a2012-07-01 02:56:11 +0530806 int64_t time = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530807
Stephen Boyd40624472013-09-26 14:33:27 -0700808 if (msm_pm_use_sync_timer) {
809 struct timespec ts;
810 getnstimeofday(&ts);
811 return timespec_to_ns(&ts);
812 }
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530813
814 time = msm_timer_get_sclk_time(period);
815 if (!time)
816 pr_err("%s: Unable to read sclk.\n", __func__);
817
818 return time;
819}
820
821static int64_t msm_pm_timer_exit_suspend(int64_t time, int64_t period)
822{
Stephen Boyd40624472013-09-26 14:33:27 -0700823 if (msm_pm_use_sync_timer) {
824 struct timespec ts;
825 getnstimeofday(&ts);
826
827 return timespec_to_ns(&ts) - time;
828 }
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530829
830 if (time != 0) {
831 int64_t end_time = msm_timer_get_sclk_time(NULL);
832 if (end_time != 0) {
833 time = end_time - time;
834 if (time < 0)
835 time += period;
836 } else
837 time = 0;
838 }
839
840 return time;
841}
842
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600843/**
844 * pm_hrtimer_cb() : Callback function for hrtimer created if the
845 * core needs to be awake to handle an event.
846 * @hrtimer : Pointer to hrtimer
847 */
848static enum hrtimer_restart pm_hrtimer_cb(struct hrtimer *hrtimer)
849{
850 return HRTIMER_NORESTART;
851}
852
853/**
854 * msm_pm_set_timer() : Set an hrtimer to wakeup the core in time
855 * to handle an event.
856 */
857static void msm_pm_set_timer(uint32_t modified_time_us)
858{
859 u64 modified_time_ns = modified_time_us * NSEC_PER_USEC;
860 ktime_t modified_ktime = ns_to_ktime(modified_time_ns);
861 pm_hrtimer.function = pm_hrtimer_cb;
Girish Mahadevan32493612013-05-10 14:37:17 -0600862 hrtimer_start(&pm_hrtimer, modified_ktime, HRTIMER_MODE_REL);
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600863}
864
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700865/******************************************************************************
866 * External Idle/Suspend Functions
867 *****************************************************************************/
868
869void arch_idle(void)
870{
871 return;
872}
873
Priyanka Mathur26b4a4b2012-11-05 13:45:45 -0800874static inline void msm_pm_ftrace_lpm_enter(unsigned int cpu,
875 uint32_t latency, uint32_t sleep_us,
876 uint32_t wake_up,
877 enum msm_pm_sleep_mode mode)
878{
879 switch (mode) {
880 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
881 trace_msm_pm_enter_wfi(cpu, latency, sleep_us, wake_up);
882 break;
883 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
884 trace_msm_pm_enter_spc(cpu, latency, sleep_us, wake_up);
885 break;
886 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
887 trace_msm_pm_enter_pc(cpu, latency, sleep_us, wake_up);
888 break;
889 case MSM_PM_SLEEP_MODE_RETENTION:
890 trace_msm_pm_enter_ret(cpu, latency, sleep_us, wake_up);
891 break;
892 default:
893 break;
894 }
895}
896
897static inline void msm_pm_ftrace_lpm_exit(unsigned int cpu,
898 enum msm_pm_sleep_mode mode,
899 bool success)
900{
901 switch (mode) {
902 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
903 trace_msm_pm_exit_wfi(cpu, success);
904 break;
905 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
906 trace_msm_pm_exit_spc(cpu, success);
907 break;
908 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
909 trace_msm_pm_exit_pc(cpu, success);
910 break;
911 case MSM_PM_SLEEP_MODE_RETENTION:
912 trace_msm_pm_exit_ret(cpu, success);
913 break;
914 default:
915 break;
916 }
917}
918
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800919static int msm_pm_idle_prepare(struct cpuidle_device *dev,
920 struct cpuidle_driver *drv, int index,
921 void **msm_pm_idle_rs_limits)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700922{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700923 int i;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700924 unsigned int power_usage = -1;
Priyanka Mathur848bb4c2012-11-30 18:04:57 -0800925 int ret = MSM_PM_SLEEP_MODE_NOT_SELECTED;
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600926 uint32_t modified_time_us = 0;
927 struct msm_pm_time_params time_param;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700928
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600929 time_param.latency_us =
930 (uint32_t) pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
931 time_param.sleep_us =
932 (uint32_t) (ktime_to_us(tick_nohz_get_sleep_length())
933 & UINT_MAX);
934 time_param.modified_time_us = 0;
935
936 if (!dev->cpu)
937 time_param.next_event_us =
938 (uint32_t) (ktime_to_us(get_next_event_time())
939 & UINT_MAX);
940 else
941 time_param.next_event_us = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700942
943 for (i = 0; i < dev->state_count; i++) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700944 struct cpuidle_state *state = &drv->states[i];
945 struct cpuidle_state_usage *st_usage = &dev->states_usage[i];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700946 enum msm_pm_sleep_mode mode;
947 bool allow;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600948 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700949 int idx;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800950 void *rs_limits = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700951
Steve Mucklef132c6c2012-06-06 18:30:57 -0700952 mode = (enum msm_pm_sleep_mode) cpuidle_get_statedata(st_usage);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700953 idx = MSM_PM_MODE(dev->cpu, mode);
954
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600955 allow = msm_pm_sleep_modes[idx].idle_enabled &&
956 msm_pm_sleep_modes[idx].idle_supported;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700957
958 switch (mode) {
959 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700960 if (num_online_cpus() > 1)
Priyanka Mathur68a9bc52012-10-03 15:15:01 -0700961 allow = false;
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700962 break;
Mahesh Sivasubramanianba7e01c2012-09-04 18:22:51 -0600963 case MSM_PM_SLEEP_MODE_RETENTION:
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -0700964 /*
965 * The Krait BHS regulator doesn't have enough head
966 * room to drive the retention voltage on LDO and so
967 * has disabled retention
968 */
969 if (!msm_pm_ldo_retention_enabled)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700970 allow = false;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700971
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700972 if (msm_pm_retention_calls_tz && num_online_cpus() > 1)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700973 allow = false;
974 break;
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700975 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
976 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
977 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700978 default:
979 allow = false;
980 break;
981 }
982
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700983 if (!allow)
984 continue;
985
986 if (pm_sleep_ops.lowest_limits)
987 rs_limits = pm_sleep_ops.lowest_limits(true,
988 mode, &time_param, &power);
989
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700990 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700991 pr_info("CPU%u:%s:%s, latency %uus, slp %uus, lim %p\n",
992 dev->cpu, __func__, state->desc,
993 time_param.latency_us,
994 time_param.sleep_us, rs_limits);
995 if (!rs_limits)
996 continue;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700997
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700998 if (power < power_usage) {
999 power_usage = power;
1000 modified_time_us = time_param.modified_time_us;
1001 ret = mode;
Priyanka Mathur92fe5752013-01-17 10:58:04 -08001002 *msm_pm_idle_rs_limits = rs_limits;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001003 }
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -07001004
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001005 }
1006
Girish Mahadevandc318fd2012-08-17 16:48:05 -06001007 if (modified_time_us && !dev->cpu)
1008 msm_pm_set_timer(modified_time_us);
Priyanka Mathur26b4a4b2012-11-05 13:45:45 -08001009
1010 msm_pm_ftrace_lpm_enter(dev->cpu, time_param.latency_us,
1011 time_param.sleep_us, time_param.next_event_us,
1012 ret);
1013
Steve Mucklef132c6c2012-06-06 18:30:57 -07001014 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001015}
1016
Priyanka Mathur92fe5752013-01-17 10:58:04 -08001017enum msm_pm_sleep_mode msm_pm_idle_enter(struct cpuidle_device *dev,
1018 struct cpuidle_driver *drv, int index)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001019{
1020 int64_t time;
Priyanka Mathur26b4a4b2012-11-05 13:45:45 -08001021 bool collapsed = 1;
Priyanka Mathur92fe5752013-01-17 10:58:04 -08001022 int exit_stat = -1;
1023 enum msm_pm_sleep_mode sleep_mode;
1024 void *msm_pm_idle_rs_limits = NULL;
Mahesh Sivasubramanian03698762013-04-18 16:18:46 -06001025 uint32_t sleep_delay = 1;
Priyanka Mathur92fe5752013-01-17 10:58:04 -08001026 int ret = -ENODEV;
Priyanka Mathur92fe5752013-01-17 10:58:04 -08001027 int notify_rpm = false;
1028 bool timer_halted = false;
1029
1030 sleep_mode = msm_pm_idle_prepare(dev, drv, index,
1031 &msm_pm_idle_rs_limits);
1032
1033 if (!msm_pm_idle_rs_limits) {
1034 sleep_mode = MSM_PM_SLEEP_MODE_NOT_SELECTED;
1035 goto cpuidle_enter_bail;
1036 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001037
1038 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
1039 pr_info("CPU%u: %s: mode %d\n",
1040 smp_processor_id(), __func__, sleep_mode);
1041
1042 time = ktime_to_ns(ktime_get());
1043
Priyanka Mathur92fe5752013-01-17 10:58:04 -08001044 if (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE) {
Mahesh Sivasubramanian2f415382013-05-17 16:29:05 -06001045 int64_t ns = msm_pm_timer_enter_idle();
Priyanka Mathur92fe5752013-01-17 10:58:04 -08001046 notify_rpm = true;
Mahesh Sivasubramanian2f415382013-05-17 16:29:05 -06001047 do_div(ns, NSEC_PER_SEC / SCLK_HZ);
1048 sleep_delay = (uint32_t)ns;
Priyanka Mathur92fe5752013-01-17 10:58:04 -08001049
Priyanka Mathur92fe5752013-01-17 10:58:04 -08001050 if (sleep_delay == 0) /* 0 would mean infinite time */
1051 sleep_delay = 1;
1052 }
1053
1054 if (pm_sleep_ops.enter_sleep)
1055 ret = pm_sleep_ops.enter_sleep(sleep_delay,
1056 msm_pm_idle_rs_limits, true, notify_rpm);
1057 if (ret)
1058 goto cpuidle_enter_bail;
1059
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -07001060 switch (sleep_mode) {
1061 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
1062 msm_pm_swfi();
1063 exit_stat = MSM_PM_STAT_IDLE_WFI;
1064 break;
1065
1066 case MSM_PM_SLEEP_MODE_RETENTION:
1067 msm_pm_retention();
1068 exit_stat = MSM_PM_STAT_RETENTION;
1069 break;
1070
1071 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
1072 collapsed = msm_pm_power_collapse_standalone(true);
Venkat Devarasetty849f47f2013-05-19 17:44:18 +05301073 if (collapsed)
1074 exit_stat = MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE;
1075 else
1076 exit_stat
1077 = MSM_PM_STAT_IDLE_FAILED_STANDALONE_POWER_COLLAPSE;
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -07001078 break;
1079
Priyanka Mathur92fe5752013-01-17 10:58:04 -08001080 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -07001081 if (MSM_PM_DEBUG_IDLE_CLK & msm_pm_debug_mask)
1082 clock_debug_print_enabled();
Karthik Parsha6fb932d2012-01-24 18:04:12 -08001083
Priyanka Mathur92fe5752013-01-17 10:58:04 -08001084 collapsed = msm_pm_power_collapse(true);
1085 timer_halted = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001086
Venkat Devarasetty849f47f2013-05-19 17:44:18 +05301087 if (collapsed)
1088 exit_stat = MSM_PM_STAT_IDLE_POWER_COLLAPSE;
1089 else
1090 exit_stat = MSM_PM_STAT_IDLE_FAILED_POWER_COLLAPSE;
1091
Priyanka Mathur92fe5752013-01-17 10:58:04 -08001092 msm_pm_timer_exit_idle(timer_halted);
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -07001093 break;
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -07001094
1095 case MSM_PM_SLEEP_MODE_NOT_SELECTED:
1096 goto cpuidle_enter_bail;
1097 break;
1098
1099 default:
1100 __WARN();
1101 goto cpuidle_enter_bail;
1102 break;
1103 }
1104
Priyanka Mathur92fe5752013-01-17 10:58:04 -08001105 if (pm_sleep_ops.exit_sleep)
1106 pm_sleep_ops.exit_sleep(msm_pm_idle_rs_limits, true,
1107 notify_rpm, collapsed);
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -07001108
Priyanka Mathur92fe5752013-01-17 10:58:04 -08001109 time = ktime_to_ns(ktime_get()) - time;
1110 msm_pm_ftrace_lpm_exit(smp_processor_id(), sleep_mode, collapsed);
1111 if (exit_stat >= 0)
1112 msm_pm_add_stat(exit_stat, time);
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -07001113 do_div(time, 1000);
Priyanka Mathur92fe5752013-01-17 10:58:04 -08001114 dev->last_residency = (int) time;
1115 return sleep_mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001116
1117cpuidle_enter_bail:
Priyanka Mathur92fe5752013-01-17 10:58:04 -08001118 dev->last_residency = 0;
1119 if (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE)
1120 msm_pm_timer_exit_idle(timer_halted);
1121 sleep_mode = MSM_PM_SLEEP_MODE_NOT_SELECTED;
1122 return sleep_mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001123}
1124
Anji Jonnala02dac8d2013-03-06 21:31:04 +05301125int msm_pm_wait_cpu_shutdown(unsigned int cpu)
1126{
Mahesh Sivasubramanian32b9d422013-05-20 13:50:16 -06001127 int timeout = 0;
Anji Jonnala02dac8d2013-03-06 21:31:04 +05301128
1129 if (!msm_pm_slp_sts)
1130 return 0;
1131 if (!msm_pm_slp_sts[cpu].base_addr)
1132 return 0;
Mahesh Sivasubramanian32b9d422013-05-20 13:50:16 -06001133 while (1) {
Anji Jonnala02dac8d2013-03-06 21:31:04 +05301134 /*
1135 * Check for the SPM of the core being hotplugged to set
1136 * its sleep state.The SPM sleep state indicates that the
1137 * core has been power collapsed.
1138 */
1139 int acc_sts = __raw_readl(msm_pm_slp_sts[cpu].base_addr);
1140
1141 if (acc_sts & msm_pm_slp_sts[cpu].mask)
1142 return 0;
Mahesh Sivasubramanian65187f92013-03-19 15:14:06 -06001143 udelay(100);
Priyanka Mathure9dfe2b2013-05-24 15:25:57 -07001144 WARN(++timeout == 20, "CPU%u didn't collape within 2ms\n",
Mahesh Sivasubramanian32b9d422013-05-20 13:50:16 -06001145 cpu);
Anji Jonnala02dac8d2013-03-06 21:31:04 +05301146 }
1147
Anji Jonnala02dac8d2013-03-06 21:31:04 +05301148 return -EBUSY;
1149}
1150
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -07001151void msm_pm_cpu_enter_lowpower(unsigned int cpu)
1152{
1153 int i;
1154 bool allow[MSM_PM_SLEEP_MODE_NR];
1155
1156 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
1157 struct msm_pm_platform_data *mode;
1158
Praveen Chidambaram42da9d22012-03-30 12:16:34 -06001159 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -07001160 allow[i] = mode->suspend_supported && mode->suspend_enabled;
1161 }
1162
1163 if (MSM_PM_DEBUG_HOTPLUG & msm_pm_debug_mask)
1164 pr_notice("CPU%u: %s: shutting down cpu\n", cpu, __func__);
1165
Matt Wagantall5375aa72012-07-02 19:59:51 -07001166 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -07001167 msm_pm_power_collapse(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -07001168 else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -07001169 msm_pm_power_collapse_standalone(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -07001170 else if (allow[MSM_PM_SLEEP_MODE_RETENTION])
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -06001171 msm_pm_retention();
Stephen Boydbda74272012-08-09 14:01:27 -07001172 else
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -07001173 msm_pm_swfi();
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -07001174}
1175
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -07001176static void msm_pm_ack_retention_disable(void *data)
1177{
1178 /*
1179 * This is a NULL function to ensure that the core has woken up
1180 * and is safe to disable retention.
1181 */
1182}
1183/**
1184 * msm_pm_enable_retention() - Disable/Enable retention on all cores
1185 * @enable: Enable/Disable retention
1186 *
1187 */
1188void msm_pm_enable_retention(bool enable)
1189{
Matt Wagantalld06f5c22013-03-14 19:31:14 -07001190 if (enable == msm_pm_ldo_retention_enabled)
1191 return;
1192
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -07001193 msm_pm_ldo_retention_enabled = enable;
1194 /*
1195 * If retention is being disabled, wakeup all online core to ensure
1196 * that it isn't executing retention. Offlined cores need not be woken
1197 * up as they enter the deepest sleep mode, namely RPM assited power
1198 * collapse
1199 */
Mahesh Sivasubramanian96f67d12013-03-14 22:44:42 -06001200 if (!enable) {
1201 preempt_disable();
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -07001202 smp_call_function_many(cpu_online_mask,
1203 msm_pm_ack_retention_disable,
1204 NULL, true);
Mahesh Sivasubramanian96f67d12013-03-14 22:44:42 -06001205 preempt_enable();
1206
1207
1208 }
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -07001209}
1210EXPORT_SYMBOL(msm_pm_enable_retention);
1211
Stephen Boyd40624472013-09-26 14:33:27 -07001212static int64_t suspend_time, suspend_period;
1213static int collapsed;
1214static int suspend_power_collapsed;
1215
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001216static int msm_pm_enter(suspend_state_t state)
1217{
1218 bool allow[MSM_PM_SLEEP_MODE_NR];
1219 int i;
Girish Mahadevandc318fd2012-08-17 16:48:05 -06001220 struct msm_pm_time_params time_param;
1221
1222 time_param.latency_us = -1;
1223 time_param.sleep_us = -1;
1224 time_param.next_event_us = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001225
1226 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1227 pr_info("%s\n", __func__);
1228
1229 if (smp_processor_id()) {
1230 __WARN();
1231 goto enter_exit;
1232 }
1233
1234
1235 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
1236 struct msm_pm_platform_data *mode;
1237
Praveen Chidambaram42da9d22012-03-30 12:16:34 -06001238 mode = &msm_pm_sleep_modes[MSM_PM_MODE(0, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001239 allow[i] = mode->suspend_supported && mode->suspend_enabled;
1240 }
1241
1242 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001243 void *rs_limits = NULL;
1244 int ret = -ENODEV;
1245 uint32_t power;
Mahesh Sivasubramanian03698762013-04-18 16:18:46 -06001246 uint32_t msm_pm_max_sleep_time = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001247
1248 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1249 pr_info("%s: power collapse\n", __func__);
1250
1251 clock_debug_print_enabled();
1252
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001253 if (msm_pm_sleep_time_override > 0) {
1254 int64_t ns = NSEC_PER_SEC *
1255 (int64_t) msm_pm_sleep_time_override;
Mahesh Sivasubramanian03698762013-04-18 16:18:46 -06001256 do_div(ns, NSEC_PER_SEC / SCLK_HZ);
1257 msm_pm_max_sleep_time = (uint32_t) ns;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001258 }
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001259
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001260 if (pm_sleep_ops.lowest_limits)
1261 rs_limits = pm_sleep_ops.lowest_limits(false,
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001262 MSM_PM_SLEEP_MODE_POWER_COLLAPSE, &time_param, &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001263
1264 if (rs_limits) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001265 if (pm_sleep_ops.enter_sleep)
1266 ret = pm_sleep_ops.enter_sleep(
1267 msm_pm_max_sleep_time,
1268 rs_limits, false, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001269 if (!ret) {
Venkat Devarasetty849f47f2013-05-19 17:44:18 +05301270 collapsed = msm_pm_power_collapse(false);
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001271 if (pm_sleep_ops.exit_sleep) {
1272 pm_sleep_ops.exit_sleep(rs_limits,
1273 false, true, collapsed);
1274 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001275 }
1276 } else {
1277 pr_err("%s: cannot find the lowest power limit\n",
1278 __func__);
1279 }
Stephen Boyd40624472013-09-26 14:33:27 -07001280 suspend_power_collapsed = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001281 } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
1282 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1283 pr_info("%s: standalone power collapse\n", __func__);
1284 msm_pm_power_collapse_standalone(false);
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -06001285 } else if (allow[MSM_PM_SLEEP_MODE_RETENTION]) {
1286 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1287 pr_info("%s: retention\n", __func__);
1288 msm_pm_retention();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001289 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
1290 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1291 pr_info("%s: swfi\n", __func__);
1292 msm_pm_swfi();
1293 }
1294
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001295enter_exit:
1296 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1297 pr_info("%s: return\n", __func__);
1298
1299 return 0;
1300}
1301
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001302void msm_pm_set_sleep_ops(struct msm_pm_sleep_ops *ops)
1303{
1304 if (ops)
1305 pm_sleep_ops = *ops;
1306}
1307
Mahesh Sivasubramanian3855da12013-04-26 11:40:27 -06001308static int msm_suspend_prepare(void)
1309{
Stephen Boyd40624472013-09-26 14:33:27 -07001310 suspend_time = msm_pm_timer_enter_suspend(&suspend_period);
Mahesh Sivasubramanian3855da12013-04-26 11:40:27 -06001311 msm_mpm_suspend_prepare();
1312 return 0;
1313}
1314
1315static void msm_suspend_wake(void)
1316{
1317 msm_mpm_suspend_wake();
Stephen Boyd40624472013-09-26 14:33:27 -07001318 if (suspend_power_collapsed) {
1319 suspend_time = msm_pm_timer_exit_suspend(suspend_time,
1320 suspend_period);
1321 if (collapsed)
1322 msm_pm_add_stat(MSM_PM_STAT_SUSPEND, suspend_time);
1323 else
1324 msm_pm_add_stat(MSM_PM_STAT_FAILED_SUSPEND,
1325 suspend_time);
1326 suspend_power_collapsed = false;
1327 }
Mahesh Sivasubramanian3855da12013-04-26 11:40:27 -06001328}
1329
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001330static const struct platform_suspend_ops msm_pm_ops = {
1331 .enter = msm_pm_enter,
1332 .valid = suspend_valid_only_mem,
Mahesh Sivasubramanian3855da12013-04-26 11:40:27 -06001333 .prepare_late = msm_suspend_prepare,
1334 .wake = msm_suspend_wake,
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -06001335};
Girish Mahadevanec50d2d2013-05-15 12:07:38 -06001336
1337static int __devinit msm_pm_snoc_client_probe(struct platform_device *pdev)
1338{
1339 int rc = 0;
1340 static struct msm_bus_scale_pdata *msm_pm_bus_pdata;
1341 static uint32_t msm_pm_bus_client;
1342
1343 msm_pm_bus_pdata = msm_bus_cl_get_pdata(pdev);
1344
1345 if (msm_pm_bus_pdata) {
1346 msm_pm_bus_client =
1347 msm_bus_scale_register_client(msm_pm_bus_pdata);
1348
1349 if (!msm_pm_bus_client) {
1350 pr_err("%s: Failed to register SNOC client",
1351 __func__);
1352 rc = -ENXIO;
1353 goto snoc_cl_probe_done;
1354 }
1355
1356 rc = msm_bus_scale_client_update_request(msm_pm_bus_client, 1);
1357
1358 if (rc)
1359 pr_err("%s: Error setting bus rate", __func__);
1360 }
1361
1362snoc_cl_probe_done:
1363 return rc;
1364}
1365
Anji Jonnala02dac8d2013-03-06 21:31:04 +05301366static int __devinit msm_cpu_status_probe(struct platform_device *pdev)
1367{
1368 struct msm_pm_sleep_status_data *pdata;
1369 char *key;
1370 u32 cpu;
1371
1372 if (!pdev)
1373 return -EFAULT;
1374
1375 msm_pm_slp_sts =
1376 kzalloc(sizeof(*msm_pm_slp_sts) * num_possible_cpus(),
1377 GFP_KERNEL);
1378
1379 if (!msm_pm_slp_sts)
1380 return -ENOMEM;
1381
1382 if (pdev->dev.of_node) {
1383 struct resource *res;
1384 u32 offset;
1385 int rc;
1386 u32 mask;
1387
1388 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1389 if (!res)
1390 goto fail_free_mem;
1391
1392 key = "qcom,cpu-alias-addr";
1393 rc = of_property_read_u32(pdev->dev.of_node, key, &offset);
1394
1395 if (rc)
1396 goto fail_free_mem;
1397
1398 key = "qcom,sleep-status-mask";
1399 rc = of_property_read_u32(pdev->dev.of_node, key,
1400 &mask);
1401 if (rc)
1402 goto fail_free_mem;
1403
1404 for_each_possible_cpu(cpu) {
1405 msm_pm_slp_sts[cpu].base_addr =
1406 ioremap(res->start + cpu * offset,
1407 resource_size(res));
1408 msm_pm_slp_sts[cpu].mask = mask;
1409
1410 if (!msm_pm_slp_sts[cpu].base_addr)
1411 goto failed_of_node;
1412 }
1413
1414 } else {
1415 pdata = pdev->dev.platform_data;
1416 if (!pdev->dev.platform_data)
1417 goto fail_free_mem;
1418
1419 for_each_possible_cpu(cpu) {
1420 msm_pm_slp_sts[cpu].base_addr =
1421 pdata->base_addr + cpu * pdata->cpu_offset;
1422 msm_pm_slp_sts[cpu].mask = pdata->mask;
1423 }
1424 }
1425
1426 return 0;
1427
1428failed_of_node:
1429 pr_info("%s(): Failed to key=%s\n", __func__, key);
1430 for_each_possible_cpu(cpu) {
1431 if (msm_pm_slp_sts[cpu].base_addr)
1432 iounmap(msm_pm_slp_sts[cpu].base_addr);
1433 }
1434fail_free_mem:
1435 kfree(msm_pm_slp_sts);
1436 return -EINVAL;
1437
1438};
1439
1440static struct of_device_id msm_slp_sts_match_tbl[] = {
1441 {.compatible = "qcom,cpu-sleep-status"},
1442 {},
1443};
1444
1445static struct platform_driver msm_cpu_status_driver = {
1446 .probe = msm_cpu_status_probe,
1447 .driver = {
1448 .name = "cpu_slp_status",
1449 .owner = THIS_MODULE,
1450 .of_match_table = msm_slp_sts_match_tbl,
1451 },
1452};
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -06001453
Girish Mahadevanec50d2d2013-05-15 12:07:38 -06001454static struct of_device_id msm_snoc_clnt_match_tbl[] = {
1455 {.compatible = "qcom,pm-snoc-client"},
1456 {},
1457};
1458
1459static struct platform_driver msm_cpu_pm_snoc_client_driver = {
1460 .probe = msm_pm_snoc_client_probe,
1461 .driver = {
1462 .name = "pm_snoc_client",
1463 .owner = THIS_MODULE,
1464 .of_match_table = msm_snoc_clnt_match_tbl,
1465 },
1466};
1467
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001468static void setup_broadcast_timer(void *arg)
1469{
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001470 int cpu = smp_processor_id();
1471
Mahesh Sivasubramanian738eaf62013-04-30 12:42:59 -06001472 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ON, &cpu);
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001473}
1474
1475static int setup_broadcast_cpuhp_notify(struct notifier_block *n,
1476 unsigned long action, void *hcpu)
1477{
Mahesh Sivasubramanian738eaf62013-04-30 12:42:59 -06001478 int cpu = (unsigned long)hcpu;
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001479
1480 switch (action & ~CPU_TASKS_FROZEN) {
1481 case CPU_ONLINE:
Mahesh Sivasubramanian738eaf62013-04-30 12:42:59 -06001482 smp_call_function_single(cpu, setup_broadcast_timer, NULL, 1);
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001483 break;
1484 }
1485
1486 return NOTIFY_OK;
1487}
1488
1489static struct notifier_block setup_broadcast_notifier = {
1490 .notifier_call = setup_broadcast_cpuhp_notify,
1491};
1492
Praveen Chidambaramede45d02013-02-20 17:45:21 -07001493static int __init msm_pm_init(void)
1494{
1495 enum msm_pm_time_stats_id enable_stats[] = {
1496 MSM_PM_STAT_IDLE_WFI,
1497 MSM_PM_STAT_RETENTION,
1498 MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE,
1499 MSM_PM_STAT_IDLE_POWER_COLLAPSE,
1500 MSM_PM_STAT_SUSPEND,
1501 };
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001502 msm_pm_mode_sysfs_add();
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301503 msm_pm_add_stats(enable_stats, ARRAY_SIZE(enable_stats));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001504 suspend_set_ops(&msm_pm_ops);
Girish Mahadevan32493612013-05-10 14:37:17 -06001505 hrtimer_init(&pm_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001506 msm_cpuidle_init();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001507
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001508 if (msm_pm_pc_reset_timer) {
Mahesh Sivasubramanian738eaf62013-04-30 12:42:59 -06001509 on_each_cpu(setup_broadcast_timer, NULL, 1);
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001510 register_cpu_notifier(&setup_broadcast_notifier);
1511 }
1512
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001513 return 0;
1514}
1515
Girish Mahadevan55944992012-10-26 11:03:07 -06001516static void __devinit msm_pm_set_flush_fn(uint32_t pc_mode)
1517{
1518 msm_pm_disable_l2_fn = NULL;
1519 msm_pm_enable_l2_fn = NULL;
1520 msm_pm_flush_l2_fn = outer_flush_all;
1521
1522 if (pc_mode == MSM_PM_PC_NOTZ_L2_EXT) {
1523 msm_pm_disable_l2_fn = outer_disable;
1524 msm_pm_enable_l2_fn = outer_resume;
1525 }
1526}
1527
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001528struct msm_pc_debug_counters_buffer {
1529 void __iomem *reg;
1530 u32 len;
1531 char buf[MAX_BUF_SIZE];
1532};
1533
1534static inline u32 msm_pc_debug_counters_read_register(
1535 void __iomem *reg, int index , int offset)
1536{
1537 return readl_relaxed(reg + (index * 4 + offset) * 4);
1538}
1539
1540static char *counter_name[] = {
1541 "PC Entry Counter",
1542 "Warmboot Entry Counter",
1543 "PC Bailout Counter"
1544};
1545
1546static int msm_pc_debug_counters_copy(
1547 struct msm_pc_debug_counters_buffer *data)
1548{
1549 int j;
1550 u32 stat;
1551 unsigned int cpu;
1552
1553 for_each_possible_cpu(cpu) {
1554 data->len += scnprintf(data->buf + data->len,
1555 sizeof(data->buf)-data->len,
1556 "CPU%d\n", cpu);
1557
Mahesh Sivasubramanian56a35432013-10-15 14:36:58 -06001558 for (j = 0; j < MSM_PC_NUM_COUNTERS; j++) {
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001559 stat = msm_pc_debug_counters_read_register(
1560 data->reg, cpu, j);
1561 data->len += scnprintf(data->buf + data->len,
1562 sizeof(data->buf)-data->len,
1563 "\t%s : %d\n", counter_name[j],
1564 stat);
1565 }
1566
1567 }
1568
1569 return data->len;
1570}
1571
1572static int msm_pc_debug_counters_file_read(struct file *file,
1573 char __user *bufu, size_t count, loff_t *ppos)
1574{
1575 struct msm_pc_debug_counters_buffer *data;
1576
1577 data = file->private_data;
1578
1579 if (!data)
1580 return -EINVAL;
1581
Mahesh Sivasubramanian6900fdc2013-04-18 14:02:52 -06001582 if (!bufu)
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001583 return -EINVAL;
1584
1585 if (!access_ok(VERIFY_WRITE, bufu, count))
1586 return -EFAULT;
1587
1588 if (*ppos >= data->len && data->len == 0)
1589 data->len = msm_pc_debug_counters_copy(data);
1590
1591 return simple_read_from_buffer(bufu, count, ppos,
1592 data->buf, data->len);
1593}
1594
1595static int msm_pc_debug_counters_file_open(struct inode *inode,
1596 struct file *file)
1597{
1598 struct msm_pc_debug_counters_buffer *buf;
1599 void __iomem *msm_pc_debug_counters_reg;
1600
1601 msm_pc_debug_counters_reg = inode->i_private;
1602
1603 if (!msm_pc_debug_counters_reg)
1604 return -EINVAL;
1605
1606 file->private_data = kzalloc(
1607 sizeof(struct msm_pc_debug_counters_buffer), GFP_KERNEL);
1608
1609 if (!file->private_data) {
1610 pr_err("%s: ERROR kmalloc failed to allocate %d bytes\n",
1611 __func__, sizeof(struct msm_pc_debug_counters_buffer));
1612
1613 return -ENOMEM;
1614 }
1615
1616 buf = file->private_data;
1617 buf->reg = msm_pc_debug_counters_reg;
1618
1619 return 0;
1620}
1621
1622static int msm_pc_debug_counters_file_close(struct inode *inode,
1623 struct file *file)
1624{
1625 kfree(file->private_data);
1626 return 0;
1627}
1628
1629static const struct file_operations msm_pc_debug_counters_fops = {
1630 .open = msm_pc_debug_counters_file_open,
1631 .read = msm_pc_debug_counters_file_read,
1632 .release = msm_pc_debug_counters_file_close,
1633 .llseek = no_llseek,
1634};
1635
Mahesh Sivasubramanian3cf21562013-07-19 10:44:26 -06001636static int msm_pm_clk_init(struct platform_device *pdev)
1637{
1638 bool synced_clocks;
1639 u32 cpu;
1640 char clk_name[] = "cpu??_clk";
1641 bool cpu_as_clocks;
1642 char *key;
1643
1644 key = "qcom,cpus-as-clocks";
1645 cpu_as_clocks = of_property_read_bool(pdev->dev.of_node, key);
1646
1647 if (!cpu_as_clocks) {
1648 use_acpuclk_apis = true;
1649 return 0;
1650 }
1651
1652 key = "qcom,synced-clocks";
1653 synced_clocks = of_property_read_bool(pdev->dev.of_node, key);
1654
1655 for_each_possible_cpu(cpu) {
1656 struct clk *clk;
1657 snprintf(clk_name, sizeof(clk_name), "cpu%d_clk", cpu);
1658 clk = devm_clk_get(&pdev->dev, clk_name);
1659 if (IS_ERR(clk)) {
1660 if (cpu && synced_clocks)
1661 return 0;
1662 else
1663 return PTR_ERR(clk);
1664 }
1665 per_cpu(cpu_clks, cpu) = clk;
1666 }
1667
1668 if (synced_clocks)
1669 return 0;
1670
1671 l2_clk = devm_clk_get(&pdev->dev, "l2_clk");
1672
1673 return PTR_RET(l2_clk);
1674}
1675
Girish Mahadevan55944992012-10-26 11:03:07 -06001676static int __devinit msm_pm_8x60_probe(struct platform_device *pdev)
1677{
1678 char *key = NULL;
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001679 struct dentry *dent = NULL;
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001680 struct resource *res = NULL;
1681 int i ;
1682 struct msm_pm_init_data_type pdata_local;
Girish Mahadevan55944992012-10-26 11:03:07 -06001683 int ret = 0;
1684
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001685 memset(&pdata_local, 0, sizeof(struct msm_pm_init_data_type));
1686
1687 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1688 if (res) {
1689 msm_pc_debug_counters_phys = res->start;
1690 WARN_ON(resource_size(res) < SZ_64);
1691 msm_pc_debug_counters = devm_ioremap(&pdev->dev, res->start,
1692 resource_size(res));
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001693 if (msm_pc_debug_counters)
1694 for (i = 0; i < resource_size(res)/4; i++)
1695 __raw_writel(0, msm_pc_debug_counters + i * 4);
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001696
1697 }
1698
1699 if (!msm_pc_debug_counters) {
1700 msm_pc_debug_counters = 0;
1701 msm_pc_debug_counters_phys = 0;
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001702 } else {
1703 dent = debugfs_create_file("pc_debug_counter", S_IRUGO, NULL,
1704 msm_pc_debug_counters,
1705 &msm_pc_debug_counters_fops);
1706 if (!dent)
1707 pr_err("%s: ERROR debugfs_create_file failed\n",
1708 __func__);
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001709 }
1710
Girish Mahadevan55944992012-10-26 11:03:07 -06001711 if (!pdev->dev.of_node) {
1712 struct msm_pm_init_data_type *d = pdev->dev.platform_data;
1713
1714 if (!d)
1715 goto pm_8x60_probe_done;
1716
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001717 memcpy(&pdata_local, d, sizeof(struct msm_pm_init_data_type));
1718
Girish Mahadevan55944992012-10-26 11:03:07 -06001719 } else {
Mahesh Sivasubramanian3cf21562013-07-19 10:44:26 -06001720 ret = msm_pm_clk_init(pdev);
1721 if (ret) {
1722 pr_info("msm_pm_clk_init returned error\n");
1723 return ret;
1724 }
1725
Girish Mahadevan55944992012-10-26 11:03:07 -06001726 key = "qcom,pc-mode";
Archana Sathyakumar2b91dc82013-02-01 17:38:23 -07001727 ret = msm_pm_get_pc_mode(pdev->dev.of_node,
1728 key,
1729 &pdata_local.pc_mode);
Girish Mahadevan55944992012-10-26 11:03:07 -06001730 if (ret) {
Archana Sathyakumar2b91dc82013-02-01 17:38:23 -07001731 pr_debug("%s: Error reading key %s",
Girish Mahadevan55944992012-10-26 11:03:07 -06001732 __func__, key);
Archana Sathyakumar2b91dc82013-02-01 17:38:23 -07001733 return -EINVAL;
Girish Mahadevan55944992012-10-26 11:03:07 -06001734 }
Girish Mahadevan55944992012-10-26 11:03:07 -06001735
1736 key = "qcom,use-sync-timer";
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001737 pdata_local.use_sync_timer =
Girish Mahadevan55944992012-10-26 11:03:07 -06001738 of_property_read_bool(pdev->dev.of_node, key);
Praveen Chidambaram4b8df032013-01-18 16:21:16 -07001739
1740 key = "qcom,saw-turns-off-pll";
1741 msm_no_ramp_down_pc = of_property_read_bool(pdev->dev.of_node,
1742 key);
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001743
1744 key = "qcom,pc-resets-timer";
1745 msm_pm_pc_reset_timer = of_property_read_bool(
1746 pdev->dev.of_node, key);
Girish Mahadevan55944992012-10-26 11:03:07 -06001747 }
1748
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001749 if (pdata_local.cp15_data.reg_data &&
1750 pdata_local.cp15_data.reg_saved_state_size > 0) {
1751 cp15_data.reg_data = kzalloc(sizeof(uint32_t) *
1752 pdata_local.cp15_data.reg_saved_state_size,
1753 GFP_KERNEL);
1754 if (!cp15_data.reg_data)
1755 return -ENOMEM;
1756
1757 cp15_data.reg_val = kzalloc(sizeof(uint32_t) *
1758 pdata_local.cp15_data.reg_saved_state_size,
1759 GFP_KERNEL);
1760 if (cp15_data.reg_val)
1761 return -ENOMEM;
1762
1763 memcpy(cp15_data.reg_data, pdata_local.cp15_data.reg_data,
1764 pdata_local.cp15_data.reg_saved_state_size *
1765 sizeof(uint32_t));
1766 }
1767
1768 msm_pm_set_flush_fn(pdata_local.pc_mode);
1769 msm_pm_use_sync_timer = pdata_local.use_sync_timer;
1770 msm_pm_retention_calls_tz = pdata_local.retention_calls_tz;
1771
Girish Mahadevan55944992012-10-26 11:03:07 -06001772pm_8x60_probe_done:
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001773 msm_pm_init();
Anji Jonnala02dac8d2013-03-06 21:31:04 +05301774 if (pdev->dev.of_node)
1775 of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
1776
Girish Mahadevan55944992012-10-26 11:03:07 -06001777 return ret;
1778}
1779
1780static struct of_device_id msm_pm_8x60_table[] = {
1781 {.compatible = "qcom,pm-8x60"},
1782 {},
1783};
1784
1785static struct platform_driver msm_pm_8x60_driver = {
1786 .probe = msm_pm_8x60_probe,
1787 .driver = {
1788 .name = "pm-8x60",
1789 .owner = THIS_MODULE,
1790 .of_match_table = msm_pm_8x60_table,
1791 },
1792};
1793
1794static int __init msm_pm_8x60_init(void)
1795{
Anji Jonnala02dac8d2013-03-06 21:31:04 +05301796 int rc;
1797
Girish Mahadevanec50d2d2013-05-15 12:07:38 -06001798 rc = platform_driver_register(&msm_cpu_pm_snoc_client_driver);
1799
1800 if (rc) {
1801 pr_err("%s(): failed to register driver %s\n", __func__,
1802 msm_cpu_pm_snoc_client_driver.driver.name);
1803 return rc;
1804 }
1805
Girish Mahadevan55944992012-10-26 11:03:07 -06001806 return platform_driver_register(&msm_pm_8x60_driver);
1807}
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001808device_initcall(msm_pm_8x60_init);
Mahesh Sivasubramanian59ffcb02013-05-31 15:08:15 -06001809
1810void __init msm_pm_sleep_status_init(void)
1811{
1812 platform_driver_register(&msm_cpu_status_driver);
1813}