blob: af21584b0bf7f63467d538969927aac67960a8ce [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>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070034#include <mach/msm_iomap.h>
Praveen Chidambaram192979f2012-04-25 18:30:23 -060035#include <mach/socinfo.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070036#include <mach/system.h>
Girish Mahadevand27ca4a2012-08-15 09:21:23 -060037#include <mach/scm.h>
38#include <mach/socinfo.h>
Hanumant Singhbd82a3e2013-02-26 13:51:17 -080039#define CREATE_TRACE_POINTS
40#include <mach/trace_msm_low_power.h>
Praveen Chidambaramc594a092012-09-18 19:48:29 -060041#include <mach/msm-krait-l2-accessors.h>
Girish Mahadevanec50d2d2013-05-15 12:07:38 -060042#include <mach/msm_bus.h>
Mahesh Sivasubramanian3855da12013-04-26 11:40:27 -060043#include <mach/mpm.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;
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700137
Archana Sathyakumar2b91dc82013-02-01 17:38:23 -0700138static int msm_pm_get_pc_mode(struct device_node *node,
139 const char *key, uint32_t *pc_mode_val)
140{
141 struct pc_mode_of {
142 uint32_t mode;
143 char *mode_name;
144 };
145 int i;
146 struct pc_mode_of pc_modes[] = {
147 {MSM_PM_PC_TZ_L2_INT, "tz_l2_int"},
148 {MSM_PM_PC_NOTZ_L2_EXT, "no_tz_l2_ext"},
149 {MSM_PM_PC_TZ_L2_EXT , "tz_l2_ext"} };
150 int ret;
151 const char *pc_mode_str;
152
153 ret = of_property_read_string(node, key, &pc_mode_str);
154 if (ret) {
155 pr_debug("%s: Cannot read %s,defaulting to 0", __func__, key);
156 pc_mode_val = MSM_PM_PC_TZ_L2_INT;
157 ret = 0;
158 } else {
159 ret = -EINVAL;
160 for (i = 0; i < ARRAY_SIZE(pc_modes); i++) {
161 if (!strncmp(pc_mode_str, pc_modes[i].mode_name,
162 strlen(pc_modes[i].mode_name))) {
163 *pc_mode_val = pc_modes[i].mode;
164 ret = 0;
165 break;
166 }
167 }
168 }
169 return ret;
170}
171
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700172/*
173 * Write out the attribute.
174 */
175static ssize_t msm_pm_mode_attr_show(
176 struct kobject *kobj, struct kobj_attribute *attr, char *buf)
177{
178 int ret = -EINVAL;
179 int i;
180
181 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
182 struct kernel_param kp;
183 unsigned int cpu;
184 struct msm_pm_platform_data *mode;
185
186 if (msm_pm_sleep_mode_labels[i] == NULL)
187 continue;
188
189 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
190 continue;
191
192 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600193 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700194
195 if (!strcmp(attr->attr.name,
196 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
197 u32 arg = mode->suspend_enabled;
198 kp.arg = &arg;
199 ret = param_get_ulong(buf, &kp);
200 } else if (!strcmp(attr->attr.name,
201 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
202 u32 arg = mode->idle_enabled;
203 kp.arg = &arg;
204 ret = param_get_ulong(buf, &kp);
205 }
206
207 break;
208 }
209
210 if (ret > 0) {
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600211 strlcat(buf, "\n", PAGE_SIZE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700212 ret++;
213 }
214
215 return ret;
216}
217
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700218static ssize_t msm_pm_mode_attr_store(struct kobject *kobj,
219 struct kobj_attribute *attr, const char *buf, size_t count)
220{
221 int ret = -EINVAL;
222 int i;
223
224 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
225 struct kernel_param kp;
226 unsigned int cpu;
227 struct msm_pm_platform_data *mode;
228
229 if (msm_pm_sleep_mode_labels[i] == NULL)
230 continue;
231
232 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
233 continue;
234
235 cpu = GET_CPU_OF_ATTR(attr);
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600236 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700237
238 if (!strcmp(attr->attr.name,
239 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
240 kp.arg = &mode->suspend_enabled;
241 ret = param_set_byte(buf, &kp);
242 } else if (!strcmp(attr->attr.name,
243 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
244 kp.arg = &mode->idle_enabled;
245 ret = param_set_byte(buf, &kp);
246 }
247
248 break;
249 }
250
251 return ret ? ret : count;
252}
253
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700254static int __devinit msm_pm_mode_sysfs_add_cpu(
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700255 unsigned int cpu, struct kobject *modes_kobj)
256{
257 char cpu_name[8];
258 struct kobject *cpu_kobj;
Praveen Chidambaram2b0fdd02011-10-28 16:40:58 -0600259 struct msm_pm_sysfs_sleep_mode *mode = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700260 int i, j, k;
261 int ret;
262
263 snprintf(cpu_name, sizeof(cpu_name), "cpu%u", cpu);
264 cpu_kobj = kobject_create_and_add(cpu_name, modes_kobj);
265 if (!cpu_kobj) {
266 pr_err("%s: cannot create %s kobject\n", __func__, cpu_name);
267 ret = -ENOMEM;
268 goto mode_sysfs_add_cpu_exit;
269 }
270
271 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
272 int idx = MSM_PM_MODE(cpu, i);
273
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600274 if ((!msm_pm_sleep_modes[idx].suspend_supported)
275 && (!msm_pm_sleep_modes[idx].idle_supported))
276 continue;
277
278 if (!msm_pm_sleep_mode_labels[i] ||
279 !msm_pm_sleep_mode_labels[i][0])
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700280 continue;
281
282 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
283 if (!mode) {
284 pr_err("%s: cannot allocate memory for attributes\n",
285 __func__);
286 ret = -ENOMEM;
287 goto mode_sysfs_add_cpu_exit;
288 }
289
290 mode->kobj = kobject_create_and_add(
291 msm_pm_sleep_mode_labels[i], cpu_kobj);
292 if (!mode->kobj) {
293 pr_err("%s: cannot create kobject\n", __func__);
294 ret = -ENOMEM;
295 goto mode_sysfs_add_cpu_exit;
296 }
297
298 for (k = 0, j = 0; k < MSM_PM_MODE_ATTR_NR; k++) {
299 if ((k == MSM_PM_MODE_ATTR_IDLE) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600300 !msm_pm_sleep_modes[idx].idle_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700301 continue;
302 if ((k == MSM_PM_MODE_ATTR_SUSPEND) &&
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600303 !msm_pm_sleep_modes[idx].suspend_supported)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700304 continue;
Stephen Boydd2059c32012-07-03 15:11:15 -0700305 sysfs_attr_init(&mode->kas[j].ka.attr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700306 mode->kas[j].cpu = cpu;
307 mode->kas[j].ka.attr.mode = 0644;
308 mode->kas[j].ka.show = msm_pm_mode_attr_show;
309 mode->kas[j].ka.store = msm_pm_mode_attr_store;
310 mode->kas[j].ka.attr.name = msm_pm_mode_attr_labels[k];
311 mode->attrs[j] = &mode->kas[j].ka.attr;
312 j++;
313 }
314 mode->attrs[j] = NULL;
315
316 mode->attr_group.attrs = mode->attrs;
317 ret = sysfs_create_group(mode->kobj, &mode->attr_group);
318 if (ret) {
319 pr_err("%s: cannot create kobject attribute group\n",
320 __func__);
321 goto mode_sysfs_add_cpu_exit;
322 }
323 }
324
325 ret = 0;
326
327mode_sysfs_add_cpu_exit:
Praveen Chidambaramd5ac2d32011-10-24 14:30:27 -0600328 if (ret) {
Praveen Chidambaram2cfda632011-10-11 16:58:09 -0600329 if (mode && mode->kobj)
330 kobject_del(mode->kobj);
331 kfree(mode);
332 }
333
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700334 return ret;
335}
336
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700337int __devinit msm_pm_mode_sysfs_add(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700338{
339 struct kobject *module_kobj;
340 struct kobject *modes_kobj;
341 unsigned int cpu;
342 int ret;
343
344 module_kobj = kset_find_obj(module_kset, KBUILD_MODNAME);
345 if (!module_kobj) {
346 pr_err("%s: cannot find kobject for module %s\n",
347 __func__, KBUILD_MODNAME);
348 ret = -ENOENT;
349 goto mode_sysfs_add_exit;
350 }
351
352 modes_kobj = kobject_create_and_add("modes", module_kobj);
353 if (!modes_kobj) {
354 pr_err("%s: cannot create modes kobject\n", __func__);
355 ret = -ENOMEM;
356 goto mode_sysfs_add_exit;
357 }
358
359 for_each_possible_cpu(cpu) {
360 ret = msm_pm_mode_sysfs_add_cpu(cpu, modes_kobj);
361 if (ret)
362 goto mode_sysfs_add_exit;
363 }
364
365 ret = 0;
366
367mode_sysfs_add_exit:
368 return ret;
369}
370
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700371/*
372 * Configure hardware registers in preparation for Apps power down.
373 */
374static void msm_pm_config_hw_before_power_down(void)
375{
376 return;
377}
378
379/*
380 * Clear hardware registers after Apps powers up.
381 */
382static void msm_pm_config_hw_after_power_up(void)
383{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700384}
385
386/*
387 * Configure hardware registers in preparation for SWFI.
388 */
389static void msm_pm_config_hw_before_swfi(void)
390{
391 return;
392}
393
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600394/*
395 * Configure/Restore hardware registers in preparation for Retention.
396 */
397
398static void msm_pm_config_hw_after_retention(void)
399{
400 int ret;
Abhijeet Dharmapurikare8f83852013-02-06 18:57:17 -0800401
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600402 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
403 WARN_ON(ret);
404}
405
406static void msm_pm_config_hw_before_retention(void)
407{
408 return;
409}
410
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600411static void msm_pm_save_cpu_reg(void)
412{
413 int i;
414
415 /* Only on core0 */
416 if (smp_processor_id())
417 return;
418
419 /**
420 * On some targets, L2 PC will turn off may reset the core
421 * configuration for the mux and the default may not make the core
422 * happy when it resumes.
423 * Save the active vdd, and set the core vdd to QSB max vdd, so that
424 * when the core resumes, it is capable of supporting the current QSB
425 * rate. Then restore the active vdd before switching the acpuclk rate.
426 */
427 if (msm_pm_get_l2_flush_flag() == 1) {
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700428 cp15_data.active_vdd = msm_spm_get_vdd(0);
429 for (i = 0; i < cp15_data.reg_saved_state_size; i++)
430 cp15_data.reg_val[i] =
431 get_l2_indirect_reg(
432 cp15_data.reg_data[i]);
433 msm_spm_set_vdd(0, cp15_data.qsb_pc_vdd);
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600434 }
435}
436
437static void msm_pm_restore_cpu_reg(void)
438{
439 int i;
440
441 /* Only on core0 */
442 if (smp_processor_id())
443 return;
444
445 if (msm_pm_get_l2_flush_flag() == 1) {
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700446 for (i = 0; i < cp15_data.reg_saved_state_size; i++)
447 set_l2_indirect_reg(
448 cp15_data.reg_data[i],
449 cp15_data.reg_val[i]);
450 msm_spm_set_vdd(0, cp15_data.active_vdd);
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600451 }
452}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700453
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700454static void msm_pm_swfi(void)
455{
456 msm_pm_config_hw_before_swfi();
457 msm_arch_idle();
458}
459
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600460static void msm_pm_retention(void)
461{
462 int ret = 0;
463
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600464 msm_pm_config_hw_before_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600465 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_POWER_RETENTION, false);
466 WARN_ON(ret);
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600467
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700468 if (msm_pm_retention_calls_tz)
Girish Mahadevand27ca4a2012-08-15 09:21:23 -0600469 scm_call_atomic1(SCM_SVC_BOOT, SCM_CMD_TERMINATE_PC,
470 SCM_L2_RETENTION);
471 else
472 msm_arch_idle();
473
474 msm_pm_config_hw_after_retention();
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -0600475}
476
Stephen Boydb29750d2012-02-21 01:21:32 -0800477static bool __ref msm_pm_spm_power_collapse(
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700478 unsigned int cpu, bool from_idle, bool notify_rpm)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700479{
480 void *entry;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600481 bool collapsed = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700482 int ret;
Mahesh Sivasubramanianda746252013-05-02 09:58:08 -0600483 bool save_cpu_regs = !cpu || from_idle;
Matt Wagantallbbf79d72013-10-04 12:46:05 -0700484 unsigned int saved_gic_cpu_ctrl;
485
486 saved_gic_cpu_ctrl = readl_relaxed(MSM_QGIC_CPU_BASE + GIC_CPU_CTRL);
487 mb();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700488
489 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
490 pr_info("CPU%u: %s: notify_rpm %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700491 cpu, __func__, (int) notify_rpm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700492
Venkat Devarasetty722f91f2013-06-03 19:41:27 +0530493 if (from_idle == true)
494 cpu_pm_enter();
495
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700496 ret = msm_spm_set_low_power_mode(
497 MSM_SPM_MODE_POWER_COLLAPSE, notify_rpm);
498 WARN_ON(ret);
499
Mahesh Sivasubramanianda746252013-05-02 09:58:08 -0600500 entry = save_cpu_regs ? msm_pm_collapse_exit : msm_secondary_startup;
501
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700502 msm_pm_boot_config_before_pc(cpu, virt_to_phys(entry));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700503
504 if (MSM_PM_DEBUG_RESET_VECTOR & msm_pm_debug_mask)
505 pr_info("CPU%u: %s: program vector to %p\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700506 cpu, __func__, entry);
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -0600507 if (from_idle && msm_pm_pc_reset_timer)
508 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700509
Matt Wagantallbbf79d72013-10-04 12:46:05 -0700510#ifdef CONFIG_VFP
511 vfp_pm_suspend();
512#endif
Mahesh Sivasubramanianda746252013-05-02 09:58:08 -0600513 collapsed = save_cpu_regs ? msm_pm_collapse() : msm_pm_pc_hotplug();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700514
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -0600515 if (from_idle && msm_pm_pc_reset_timer)
516 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu);
517
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700518 msm_pm_boot_config_after_pc(cpu);
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -0600519
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700520 if (collapsed) {
Matt Wagantallbbf79d72013-10-04 12:46:05 -0700521#ifdef CONFIG_VFP
522 vfp_pm_resume();
523#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700524 cpu_init();
Matt Wagantallbbf79d72013-10-04 12:46:05 -0700525 writel(0xF0, MSM_QGIC_CPU_BASE + GIC_CPU_PRIMASK);
526 writel_relaxed(saved_gic_cpu_ctrl,
527 MSM_QGIC_CPU_BASE + GIC_CPU_CTRL);
528 mb();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700529 local_fiq_enable();
530 }
531
532 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
533 pr_info("CPU%u: %s: msm_pm_collapse returned, collapsed %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700534 cpu, __func__, collapsed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700535
536 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
537 WARN_ON(ret);
Venkat Devarasetty722f91f2013-06-03 19:41:27 +0530538
539 if (from_idle == true)
540 cpu_pm_exit();
541
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600542 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700543}
544
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600545static bool msm_pm_power_collapse_standalone(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700546{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700547 unsigned int cpu = smp_processor_id();
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700548 unsigned int avsdscr;
549 unsigned int avscsr;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600550 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700551
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700552 avsdscr = avs_get_avsdscr();
553 avscsr = avs_get_avscsr();
554 avs_set_avscsr(0); /* Disable AVS */
555
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700556 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, false);
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700557
558 avs_set_avsdscr(avsdscr);
559 avs_set_avscsr(avscsr);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600560 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700561}
562
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600563static bool msm_pm_power_collapse(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700564{
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700565 unsigned int cpu = smp_processor_id();
Praveen Chidambaram4b8df032013-01-18 16:21:16 -0700566 unsigned long saved_acpuclk_rate = 0;
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700567 unsigned int avsdscr;
568 unsigned int avscsr;
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600569 bool collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700570
571 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
572 pr_info("CPU%u: %s: idle %d\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700573 cpu, __func__, (int)from_idle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700574
575 msm_pm_config_hw_before_power_down();
576 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700577 pr_info("CPU%u: %s: pre power down\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700578
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700579 avsdscr = avs_get_avsdscr();
580 avscsr = avs_get_avscsr();
581 avs_set_avscsr(0); /* Disable AVS */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700582
Praveen Chidambaram4b8df032013-01-18 16:21:16 -0700583 if (cpu_online(cpu) && !msm_no_ramp_down_pc)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700584 saved_acpuclk_rate = acpuclk_power_collapse();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700585
586 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
587 pr_info("CPU%u: %s: change clock rate (old rate = %lu)\n",
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700588 cpu, __func__, saved_acpuclk_rate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700589
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700590 if (cp15_data.save_cp15)
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600591 msm_pm_save_cpu_reg();
592
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700593 collapsed = msm_pm_spm_power_collapse(cpu, from_idle, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700594
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700595 if (cp15_data.save_cp15)
Praveen Chidambaramc594a092012-09-18 19:48:29 -0600596 msm_pm_restore_cpu_reg();
597
Girish Mahadevan4e025d92012-02-29 13:26:51 -0700598 if (cpu_online(cpu)) {
599 if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask)
600 pr_info("CPU%u: %s: restore clock rate to %lu\n",
601 cpu, __func__, saved_acpuclk_rate);
Praveen Chidambaram4b8df032013-01-18 16:21:16 -0700602 if (!msm_no_ramp_down_pc &&
603 acpuclk_set_rate(cpu, saved_acpuclk_rate, SETRATE_PC)
604 < 0)
Girish Mahadevan4e025d92012-02-29 13:26:51 -0700605 pr_err("CPU%u: %s: failed to restore clock rate(%lu)\n",
606 cpu, __func__, saved_acpuclk_rate);
607 } else {
608 unsigned int gic_dist_enabled;
609 unsigned int gic_dist_pending;
610 gic_dist_enabled = readl_relaxed(
611 MSM_QGIC_DIST_BASE + GIC_DIST_ENABLE_CLEAR);
612 gic_dist_pending = readl_relaxed(
613 MSM_QGIC_DIST_BASE + GIC_DIST_PENDING_SET);
614 mb();
615 gic_dist_pending &= gic_dist_enabled;
616
617 if (gic_dist_pending)
618 pr_err("CPU %d interrupted during hotplug.Pending int 0x%x\n",
619 cpu, gic_dist_pending);
620 }
621
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700622
Praveen Chidambaram5e614112012-11-08 17:53:34 -0700623 avs_set_avsdscr(avsdscr);
624 avs_set_avscsr(avscsr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700625 msm_pm_config_hw_after_power_up();
626 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700627 pr_info("CPU%u: %s: post power up\n", cpu, __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700628
629 if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask)
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700630 pr_info("CPU%u: %s: return\n", cpu, __func__);
Maheshkumar Sivasubramaniandd93ecf2011-09-15 19:39:14 -0600631 return collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700632}
633
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600634static int64_t msm_pm_timer_enter_idle(void)
635{
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700636 if (msm_pm_use_sync_timer)
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600637 return ktime_to_ns(tick_nohz_get_sleep_length());
638
639 return msm_timer_enter_idle();
640}
641
642static void msm_pm_timer_exit_idle(bool timer_halted)
643{
Praveen Chidambaramf27a5152013-02-01 11:44:53 -0700644 if (msm_pm_use_sync_timer)
Praveen Chidambaram192979f2012-04-25 18:30:23 -0600645 return;
646
647 msm_timer_exit_idle((int) timer_halted);
648}
649
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530650static int64_t msm_pm_timer_enter_suspend(int64_t *period)
651{
Anji Jonnalac02367a2012-07-01 02:56:11 +0530652 int64_t time = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530653
Stephen Boyd40624472013-09-26 14:33:27 -0700654 if (msm_pm_use_sync_timer) {
655 struct timespec ts;
656 getnstimeofday(&ts);
657 return timespec_to_ns(&ts);
658 }
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530659
660 time = msm_timer_get_sclk_time(period);
661 if (!time)
662 pr_err("%s: Unable to read sclk.\n", __func__);
663
664 return time;
665}
666
667static int64_t msm_pm_timer_exit_suspend(int64_t time, int64_t period)
668{
Stephen Boyd40624472013-09-26 14:33:27 -0700669 if (msm_pm_use_sync_timer) {
670 struct timespec ts;
671 getnstimeofday(&ts);
672
673 return timespec_to_ns(&ts) - time;
674 }
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530675
676 if (time != 0) {
677 int64_t end_time = msm_timer_get_sclk_time(NULL);
678 if (end_time != 0) {
679 time = end_time - time;
680 if (time < 0)
681 time += period;
682 } else
683 time = 0;
684 }
685
686 return time;
687}
688
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600689/**
690 * pm_hrtimer_cb() : Callback function for hrtimer created if the
691 * core needs to be awake to handle an event.
692 * @hrtimer : Pointer to hrtimer
693 */
694static enum hrtimer_restart pm_hrtimer_cb(struct hrtimer *hrtimer)
695{
696 return HRTIMER_NORESTART;
697}
698
699/**
700 * msm_pm_set_timer() : Set an hrtimer to wakeup the core in time
701 * to handle an event.
702 */
703static void msm_pm_set_timer(uint32_t modified_time_us)
704{
705 u64 modified_time_ns = modified_time_us * NSEC_PER_USEC;
706 ktime_t modified_ktime = ns_to_ktime(modified_time_ns);
707 pm_hrtimer.function = pm_hrtimer_cb;
Girish Mahadevan32493612013-05-10 14:37:17 -0600708 hrtimer_start(&pm_hrtimer, modified_ktime, HRTIMER_MODE_REL);
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600709}
710
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700711/******************************************************************************
712 * External Idle/Suspend Functions
713 *****************************************************************************/
714
715void arch_idle(void)
716{
717 return;
718}
719
Priyanka Mathur26b4a4b2012-11-05 13:45:45 -0800720static inline void msm_pm_ftrace_lpm_enter(unsigned int cpu,
721 uint32_t latency, uint32_t sleep_us,
722 uint32_t wake_up,
723 enum msm_pm_sleep_mode mode)
724{
725 switch (mode) {
726 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
727 trace_msm_pm_enter_wfi(cpu, latency, sleep_us, wake_up);
728 break;
729 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
730 trace_msm_pm_enter_spc(cpu, latency, sleep_us, wake_up);
731 break;
732 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
733 trace_msm_pm_enter_pc(cpu, latency, sleep_us, wake_up);
734 break;
735 case MSM_PM_SLEEP_MODE_RETENTION:
736 trace_msm_pm_enter_ret(cpu, latency, sleep_us, wake_up);
737 break;
738 default:
739 break;
740 }
741}
742
743static inline void msm_pm_ftrace_lpm_exit(unsigned int cpu,
744 enum msm_pm_sleep_mode mode,
745 bool success)
746{
747 switch (mode) {
748 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
749 trace_msm_pm_exit_wfi(cpu, success);
750 break;
751 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
752 trace_msm_pm_exit_spc(cpu, success);
753 break;
754 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
755 trace_msm_pm_exit_pc(cpu, success);
756 break;
757 case MSM_PM_SLEEP_MODE_RETENTION:
758 trace_msm_pm_exit_ret(cpu, success);
759 break;
760 default:
761 break;
762 }
763}
764
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800765static int msm_pm_idle_prepare(struct cpuidle_device *dev,
766 struct cpuidle_driver *drv, int index,
767 void **msm_pm_idle_rs_limits)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700768{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700769 int i;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700770 unsigned int power_usage = -1;
Priyanka Mathur848bb4c2012-11-30 18:04:57 -0800771 int ret = MSM_PM_SLEEP_MODE_NOT_SELECTED;
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600772 uint32_t modified_time_us = 0;
773 struct msm_pm_time_params time_param;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700774
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600775 time_param.latency_us =
776 (uint32_t) pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
777 time_param.sleep_us =
778 (uint32_t) (ktime_to_us(tick_nohz_get_sleep_length())
779 & UINT_MAX);
780 time_param.modified_time_us = 0;
781
782 if (!dev->cpu)
783 time_param.next_event_us =
784 (uint32_t) (ktime_to_us(get_next_event_time())
785 & UINT_MAX);
786 else
787 time_param.next_event_us = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700788
789 for (i = 0; i < dev->state_count; i++) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700790 struct cpuidle_state *state = &drv->states[i];
791 struct cpuidle_state_usage *st_usage = &dev->states_usage[i];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700792 enum msm_pm_sleep_mode mode;
793 bool allow;
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -0600794 uint32_t power;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700795 int idx;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800796 void *rs_limits = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700797
Steve Mucklef132c6c2012-06-06 18:30:57 -0700798 mode = (enum msm_pm_sleep_mode) cpuidle_get_statedata(st_usage);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700799 idx = MSM_PM_MODE(dev->cpu, mode);
800
Praveen Chidambaram42da9d22012-03-30 12:16:34 -0600801 allow = msm_pm_sleep_modes[idx].idle_enabled &&
802 msm_pm_sleep_modes[idx].idle_supported;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700803
804 switch (mode) {
805 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700806 if (num_online_cpus() > 1)
Priyanka Mathur68a9bc52012-10-03 15:15:01 -0700807 allow = false;
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700808 break;
Mahesh Sivasubramanianba7e01c2012-09-04 18:22:51 -0600809 case MSM_PM_SLEEP_MODE_RETENTION:
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -0700810 /*
811 * The Krait BHS regulator doesn't have enough head
812 * room to drive the retention voltage on LDO and so
813 * has disabled retention
814 */
815 if (!msm_pm_ldo_retention_enabled)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700816 allow = false;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700817
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700818 if (msm_pm_retention_calls_tz && num_online_cpus() > 1)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700819 allow = false;
820 break;
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700821 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
822 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
823 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700824 default:
825 allow = false;
826 break;
827 }
828
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700829 if (!allow)
830 continue;
831
832 if (pm_sleep_ops.lowest_limits)
833 rs_limits = pm_sleep_ops.lowest_limits(true,
834 mode, &time_param, &power);
835
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700836 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700837 pr_info("CPU%u:%s:%s, latency %uus, slp %uus, lim %p\n",
838 dev->cpu, __func__, state->desc,
839 time_param.latency_us,
840 time_param.sleep_us, rs_limits);
841 if (!rs_limits)
842 continue;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700843
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700844 if (power < power_usage) {
845 power_usage = power;
846 modified_time_us = time_param.modified_time_us;
847 ret = mode;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800848 *msm_pm_idle_rs_limits = rs_limits;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700849 }
Mahesh Sivasubramanian87f579c2013-02-04 11:59:44 -0700850
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700851 }
852
Girish Mahadevandc318fd2012-08-17 16:48:05 -0600853 if (modified_time_us && !dev->cpu)
854 msm_pm_set_timer(modified_time_us);
Priyanka Mathur26b4a4b2012-11-05 13:45:45 -0800855
856 msm_pm_ftrace_lpm_enter(dev->cpu, time_param.latency_us,
857 time_param.sleep_us, time_param.next_event_us,
858 ret);
859
Steve Mucklef132c6c2012-06-06 18:30:57 -0700860 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700861}
862
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800863enum msm_pm_sleep_mode msm_pm_idle_enter(struct cpuidle_device *dev,
864 struct cpuidle_driver *drv, int index)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700865{
866 int64_t time;
Priyanka Mathur26b4a4b2012-11-05 13:45:45 -0800867 bool collapsed = 1;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800868 int exit_stat = -1;
869 enum msm_pm_sleep_mode sleep_mode;
870 void *msm_pm_idle_rs_limits = NULL;
Mahesh Sivasubramanian03698762013-04-18 16:18:46 -0600871 uint32_t sleep_delay = 1;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800872 int ret = -ENODEV;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800873 int notify_rpm = false;
874 bool timer_halted = false;
875
876 sleep_mode = msm_pm_idle_prepare(dev, drv, index,
877 &msm_pm_idle_rs_limits);
878
879 if (!msm_pm_idle_rs_limits) {
880 sleep_mode = MSM_PM_SLEEP_MODE_NOT_SELECTED;
881 goto cpuidle_enter_bail;
882 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700883
884 if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask)
885 pr_info("CPU%u: %s: mode %d\n",
886 smp_processor_id(), __func__, sleep_mode);
887
888 time = ktime_to_ns(ktime_get());
889
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800890 if (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE) {
Mahesh Sivasubramanian2f415382013-05-17 16:29:05 -0600891 int64_t ns = msm_pm_timer_enter_idle();
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800892 notify_rpm = true;
Mahesh Sivasubramanian2f415382013-05-17 16:29:05 -0600893 do_div(ns, NSEC_PER_SEC / SCLK_HZ);
894 sleep_delay = (uint32_t)ns;
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800895
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800896 if (sleep_delay == 0) /* 0 would mean infinite time */
897 sleep_delay = 1;
898 }
899
900 if (pm_sleep_ops.enter_sleep)
901 ret = pm_sleep_ops.enter_sleep(sleep_delay,
902 msm_pm_idle_rs_limits, true, notify_rpm);
903 if (ret)
904 goto cpuidle_enter_bail;
905
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700906 switch (sleep_mode) {
907 case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT:
908 msm_pm_swfi();
909 exit_stat = MSM_PM_STAT_IDLE_WFI;
910 break;
911
912 case MSM_PM_SLEEP_MODE_RETENTION:
913 msm_pm_retention();
914 exit_stat = MSM_PM_STAT_RETENTION;
915 break;
916
917 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE:
918 collapsed = msm_pm_power_collapse_standalone(true);
Venkat Devarasetty849f47f2013-05-19 17:44:18 +0530919 if (collapsed)
920 exit_stat = MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE;
921 else
922 exit_stat
923 = MSM_PM_STAT_IDLE_FAILED_STANDALONE_POWER_COLLAPSE;
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700924 break;
925
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800926 case MSM_PM_SLEEP_MODE_POWER_COLLAPSE:
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700927 if (MSM_PM_DEBUG_IDLE_CLK & msm_pm_debug_mask)
928 clock_debug_print_enabled();
Karthik Parsha6fb932d2012-01-24 18:04:12 -0800929
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800930 collapsed = msm_pm_power_collapse(true);
931 timer_halted = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700932
Venkat Devarasetty849f47f2013-05-19 17:44:18 +0530933 if (collapsed)
934 exit_stat = MSM_PM_STAT_IDLE_POWER_COLLAPSE;
935 else
936 exit_stat = MSM_PM_STAT_IDLE_FAILED_POWER_COLLAPSE;
937
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800938 msm_pm_timer_exit_idle(timer_halted);
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700939 break;
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700940
941 case MSM_PM_SLEEP_MODE_NOT_SELECTED:
942 goto cpuidle_enter_bail;
943 break;
944
945 default:
946 __WARN();
947 goto cpuidle_enter_bail;
948 break;
949 }
950
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800951 if (pm_sleep_ops.exit_sleep)
952 pm_sleep_ops.exit_sleep(msm_pm_idle_rs_limits, true,
953 notify_rpm, collapsed);
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700954
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800955 time = ktime_to_ns(ktime_get()) - time;
956 msm_pm_ftrace_lpm_exit(smp_processor_id(), sleep_mode, collapsed);
957 if (exit_stat >= 0)
958 msm_pm_add_stat(exit_stat, time);
Mahesh Sivasubramanian95a48e72013-02-15 14:23:26 -0700959 do_div(time, 1000);
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800960 dev->last_residency = (int) time;
961 return sleep_mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700962
963cpuidle_enter_bail:
Priyanka Mathur92fe5752013-01-17 10:58:04 -0800964 dev->last_residency = 0;
965 if (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE)
966 msm_pm_timer_exit_idle(timer_halted);
967 sleep_mode = MSM_PM_SLEEP_MODE_NOT_SELECTED;
968 return sleep_mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700969}
970
Anji Jonnala02dac8d2013-03-06 21:31:04 +0530971int msm_pm_wait_cpu_shutdown(unsigned int cpu)
972{
Mahesh Sivasubramanian32b9d422013-05-20 13:50:16 -0600973 int timeout = 0;
Anji Jonnala02dac8d2013-03-06 21:31:04 +0530974
975 if (!msm_pm_slp_sts)
976 return 0;
977 if (!msm_pm_slp_sts[cpu].base_addr)
978 return 0;
Mahesh Sivasubramanian32b9d422013-05-20 13:50:16 -0600979 while (1) {
Anji Jonnala02dac8d2013-03-06 21:31:04 +0530980 /*
981 * Check for the SPM of the core being hotplugged to set
982 * its sleep state.The SPM sleep state indicates that the
983 * core has been power collapsed.
984 */
985 int acc_sts = __raw_readl(msm_pm_slp_sts[cpu].base_addr);
986
987 if (acc_sts & msm_pm_slp_sts[cpu].mask)
988 return 0;
Mahesh Sivasubramanian65187f92013-03-19 15:14:06 -0600989 udelay(100);
Priyanka Mathure9dfe2b2013-05-24 15:25:57 -0700990 WARN(++timeout == 20, "CPU%u didn't collape within 2ms\n",
Mahesh Sivasubramanian32b9d422013-05-20 13:50:16 -0600991 cpu);
Anji Jonnala02dac8d2013-03-06 21:31:04 +0530992 }
993
Anji Jonnala02dac8d2013-03-06 21:31:04 +0530994 return -EBUSY;
995}
996
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -0700997void msm_pm_cpu_enter_lowpower(unsigned int cpu)
998{
999 int i;
1000 bool allow[MSM_PM_SLEEP_MODE_NR];
1001
1002 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
1003 struct msm_pm_platform_data *mode;
1004
Praveen Chidambaram42da9d22012-03-30 12:16:34 -06001005 mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)];
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -07001006 allow[i] = mode->suspend_supported && mode->suspend_enabled;
1007 }
1008
1009 if (MSM_PM_DEBUG_HOTPLUG & msm_pm_debug_mask)
1010 pr_notice("CPU%u: %s: shutting down cpu\n", cpu, __func__);
1011
Matt Wagantall5375aa72012-07-02 19:59:51 -07001012 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -07001013 msm_pm_power_collapse(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -07001014 else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE])
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -07001015 msm_pm_power_collapse_standalone(false);
Matt Wagantall5375aa72012-07-02 19:59:51 -07001016 else if (allow[MSM_PM_SLEEP_MODE_RETENTION])
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -06001017 msm_pm_retention();
Stephen Boydbda74272012-08-09 14:01:27 -07001018 else
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -07001019 msm_pm_swfi();
Mahesh Sivasubramaniand23add12011-11-18 14:30:11 -07001020}
1021
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -07001022static void msm_pm_ack_retention_disable(void *data)
1023{
1024 /*
1025 * This is a NULL function to ensure that the core has woken up
1026 * and is safe to disable retention.
1027 */
1028}
1029/**
1030 * msm_pm_enable_retention() - Disable/Enable retention on all cores
1031 * @enable: Enable/Disable retention
1032 *
1033 */
1034void msm_pm_enable_retention(bool enable)
1035{
Matt Wagantalld06f5c22013-03-14 19:31:14 -07001036 if (enable == msm_pm_ldo_retention_enabled)
1037 return;
1038
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -07001039 msm_pm_ldo_retention_enabled = enable;
1040 /*
1041 * If retention is being disabled, wakeup all online core to ensure
1042 * that it isn't executing retention. Offlined cores need not be woken
1043 * up as they enter the deepest sleep mode, namely RPM assited power
1044 * collapse
1045 */
Mahesh Sivasubramanian96f67d12013-03-14 22:44:42 -06001046 if (!enable) {
1047 preempt_disable();
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -07001048 smp_call_function_many(cpu_online_mask,
1049 msm_pm_ack_retention_disable,
1050 NULL, true);
Mahesh Sivasubramanian96f67d12013-03-14 22:44:42 -06001051 preempt_enable();
1052
1053
1054 }
Mahesh Sivasubramanian1b8601b2012-12-20 14:11:23 -07001055}
1056EXPORT_SYMBOL(msm_pm_enable_retention);
1057
Stephen Boyd40624472013-09-26 14:33:27 -07001058static int64_t suspend_time, suspend_period;
1059static int collapsed;
1060static int suspend_power_collapsed;
1061
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001062static int msm_pm_enter(suspend_state_t state)
1063{
1064 bool allow[MSM_PM_SLEEP_MODE_NR];
1065 int i;
Girish Mahadevandc318fd2012-08-17 16:48:05 -06001066 struct msm_pm_time_params time_param;
1067
1068 time_param.latency_us = -1;
1069 time_param.sleep_us = -1;
1070 time_param.next_event_us = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001071
1072 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1073 pr_info("%s\n", __func__);
1074
1075 if (smp_processor_id()) {
1076 __WARN();
1077 goto enter_exit;
1078 }
1079
1080
1081 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
1082 struct msm_pm_platform_data *mode;
1083
Praveen Chidambaram42da9d22012-03-30 12:16:34 -06001084 mode = &msm_pm_sleep_modes[MSM_PM_MODE(0, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001085 allow[i] = mode->suspend_supported && mode->suspend_enabled;
1086 }
1087
1088 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001089 void *rs_limits = NULL;
1090 int ret = -ENODEV;
1091 uint32_t power;
Mahesh Sivasubramanian03698762013-04-18 16:18:46 -06001092 uint32_t msm_pm_max_sleep_time = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001093
1094 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1095 pr_info("%s: power collapse\n", __func__);
1096
1097 clock_debug_print_enabled();
1098
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001099 if (msm_pm_sleep_time_override > 0) {
1100 int64_t ns = NSEC_PER_SEC *
1101 (int64_t) msm_pm_sleep_time_override;
Mahesh Sivasubramanian03698762013-04-18 16:18:46 -06001102 do_div(ns, NSEC_PER_SEC / SCLK_HZ);
1103 msm_pm_max_sleep_time = (uint32_t) ns;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001104 }
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001105
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001106 if (pm_sleep_ops.lowest_limits)
1107 rs_limits = pm_sleep_ops.lowest_limits(false,
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001108 MSM_PM_SLEEP_MODE_POWER_COLLAPSE, &time_param, &power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001109
1110 if (rs_limits) {
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001111 if (pm_sleep_ops.enter_sleep)
1112 ret = pm_sleep_ops.enter_sleep(
1113 msm_pm_max_sleep_time,
1114 rs_limits, false, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001115 if (!ret) {
Venkat Devarasetty849f47f2013-05-19 17:44:18 +05301116 collapsed = msm_pm_power_collapse(false);
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001117 if (pm_sleep_ops.exit_sleep) {
1118 pm_sleep_ops.exit_sleep(rs_limits,
1119 false, true, collapsed);
1120 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001121 }
1122 } else {
1123 pr_err("%s: cannot find the lowest power limit\n",
1124 __func__);
1125 }
Stephen Boyd40624472013-09-26 14:33:27 -07001126 suspend_power_collapsed = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001127 } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
1128 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1129 pr_info("%s: standalone power collapse\n", __func__);
1130 msm_pm_power_collapse_standalone(false);
Praveen Chidambaramd3d844d2012-04-24 09:47:38 -06001131 } else if (allow[MSM_PM_SLEEP_MODE_RETENTION]) {
1132 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1133 pr_info("%s: retention\n", __func__);
1134 msm_pm_retention();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001135 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
1136 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1137 pr_info("%s: swfi\n", __func__);
1138 msm_pm_swfi();
1139 }
1140
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001141enter_exit:
1142 if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask)
1143 pr_info("%s: return\n", __func__);
1144
1145 return 0;
1146}
1147
Mahesh Sivasubramanian6d06e3a2012-05-16 13:41:07 -06001148void msm_pm_set_sleep_ops(struct msm_pm_sleep_ops *ops)
1149{
1150 if (ops)
1151 pm_sleep_ops = *ops;
1152}
1153
Mahesh Sivasubramanian3855da12013-04-26 11:40:27 -06001154static int msm_suspend_prepare(void)
1155{
Stephen Boyd40624472013-09-26 14:33:27 -07001156 suspend_time = msm_pm_timer_enter_suspend(&suspend_period);
Mahesh Sivasubramanian3855da12013-04-26 11:40:27 -06001157 msm_mpm_suspend_prepare();
1158 return 0;
1159}
1160
1161static void msm_suspend_wake(void)
1162{
1163 msm_mpm_suspend_wake();
Stephen Boyd40624472013-09-26 14:33:27 -07001164 if (suspend_power_collapsed) {
1165 suspend_time = msm_pm_timer_exit_suspend(suspend_time,
1166 suspend_period);
1167 if (collapsed)
1168 msm_pm_add_stat(MSM_PM_STAT_SUSPEND, suspend_time);
1169 else
1170 msm_pm_add_stat(MSM_PM_STAT_FAILED_SUSPEND,
1171 suspend_time);
1172 suspend_power_collapsed = false;
1173 }
Mahesh Sivasubramanian3855da12013-04-26 11:40:27 -06001174}
1175
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001176static const struct platform_suspend_ops msm_pm_ops = {
1177 .enter = msm_pm_enter,
1178 .valid = suspend_valid_only_mem,
Mahesh Sivasubramanian3855da12013-04-26 11:40:27 -06001179 .prepare_late = msm_suspend_prepare,
1180 .wake = msm_suspend_wake,
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -06001181};
Girish Mahadevanec50d2d2013-05-15 12:07:38 -06001182
1183static int __devinit msm_pm_snoc_client_probe(struct platform_device *pdev)
1184{
1185 int rc = 0;
1186 static struct msm_bus_scale_pdata *msm_pm_bus_pdata;
1187 static uint32_t msm_pm_bus_client;
1188
1189 msm_pm_bus_pdata = msm_bus_cl_get_pdata(pdev);
1190
1191 if (msm_pm_bus_pdata) {
1192 msm_pm_bus_client =
1193 msm_bus_scale_register_client(msm_pm_bus_pdata);
1194
1195 if (!msm_pm_bus_client) {
1196 pr_err("%s: Failed to register SNOC client",
1197 __func__);
1198 rc = -ENXIO;
1199 goto snoc_cl_probe_done;
1200 }
1201
1202 rc = msm_bus_scale_client_update_request(msm_pm_bus_client, 1);
1203
1204 if (rc)
1205 pr_err("%s: Error setting bus rate", __func__);
1206 }
1207
1208snoc_cl_probe_done:
1209 return rc;
1210}
1211
Anji Jonnala02dac8d2013-03-06 21:31:04 +05301212static int __devinit msm_cpu_status_probe(struct platform_device *pdev)
1213{
1214 struct msm_pm_sleep_status_data *pdata;
1215 char *key;
1216 u32 cpu;
1217
1218 if (!pdev)
1219 return -EFAULT;
1220
1221 msm_pm_slp_sts =
1222 kzalloc(sizeof(*msm_pm_slp_sts) * num_possible_cpus(),
1223 GFP_KERNEL);
1224
1225 if (!msm_pm_slp_sts)
1226 return -ENOMEM;
1227
1228 if (pdev->dev.of_node) {
1229 struct resource *res;
1230 u32 offset;
1231 int rc;
1232 u32 mask;
1233
1234 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1235 if (!res)
1236 goto fail_free_mem;
1237
1238 key = "qcom,cpu-alias-addr";
1239 rc = of_property_read_u32(pdev->dev.of_node, key, &offset);
1240
1241 if (rc)
1242 goto fail_free_mem;
1243
1244 key = "qcom,sleep-status-mask";
1245 rc = of_property_read_u32(pdev->dev.of_node, key,
1246 &mask);
1247 if (rc)
1248 goto fail_free_mem;
1249
1250 for_each_possible_cpu(cpu) {
1251 msm_pm_slp_sts[cpu].base_addr =
1252 ioremap(res->start + cpu * offset,
1253 resource_size(res));
1254 msm_pm_slp_sts[cpu].mask = mask;
1255
1256 if (!msm_pm_slp_sts[cpu].base_addr)
1257 goto failed_of_node;
1258 }
1259
1260 } else {
1261 pdata = pdev->dev.platform_data;
1262 if (!pdev->dev.platform_data)
1263 goto fail_free_mem;
1264
1265 for_each_possible_cpu(cpu) {
1266 msm_pm_slp_sts[cpu].base_addr =
1267 pdata->base_addr + cpu * pdata->cpu_offset;
1268 msm_pm_slp_sts[cpu].mask = pdata->mask;
1269 }
1270 }
1271
1272 return 0;
1273
1274failed_of_node:
1275 pr_info("%s(): Failed to key=%s\n", __func__, key);
1276 for_each_possible_cpu(cpu) {
1277 if (msm_pm_slp_sts[cpu].base_addr)
1278 iounmap(msm_pm_slp_sts[cpu].base_addr);
1279 }
1280fail_free_mem:
1281 kfree(msm_pm_slp_sts);
1282 return -EINVAL;
1283
1284};
1285
1286static struct of_device_id msm_slp_sts_match_tbl[] = {
1287 {.compatible = "qcom,cpu-sleep-status"},
1288 {},
1289};
1290
1291static struct platform_driver msm_cpu_status_driver = {
1292 .probe = msm_cpu_status_probe,
1293 .driver = {
1294 .name = "cpu_slp_status",
1295 .owner = THIS_MODULE,
1296 .of_match_table = msm_slp_sts_match_tbl,
1297 },
1298};
Mahesh Sivasubramaniancb396622012-03-14 14:50:37 -06001299
Girish Mahadevanec50d2d2013-05-15 12:07:38 -06001300static struct of_device_id msm_snoc_clnt_match_tbl[] = {
1301 {.compatible = "qcom,pm-snoc-client"},
1302 {},
1303};
1304
1305static struct platform_driver msm_cpu_pm_snoc_client_driver = {
1306 .probe = msm_pm_snoc_client_probe,
1307 .driver = {
1308 .name = "pm_snoc_client",
1309 .owner = THIS_MODULE,
1310 .of_match_table = msm_snoc_clnt_match_tbl,
1311 },
1312};
1313
Laura Abbott184d8302013-06-17 17:26:40 -07001314#ifdef CONFIG_ARM_LPAE
1315static int msm_pm_idmap_add_pmd(pud_t *pud, unsigned long addr,
1316 unsigned long end, unsigned long prot)
1317{
1318 pmd_t *pmd;
1319 unsigned long next;
Girish Mahadevanec50d2d2013-05-15 12:07:38 -06001320
Laura Abbott184d8302013-06-17 17:26:40 -07001321 if (pud_none_or_clear_bad(pud) || (pud_val(*pud) & L_PGD_SWAPPER)) {
1322 pmd = pmd_alloc_one(&init_mm, addr);
1323 if (!pmd)
1324 return -ENOMEM;
1325
1326 pud_populate(&init_mm, pud, pmd);
1327 pmd += pmd_index(addr);
1328 } else {
1329 pmd = pmd_offset(pud, addr);
1330 }
1331
1332 do {
1333 next = pmd_addr_end(addr, end);
1334 *pmd = __pmd((addr & PMD_MASK) | prot);
1335 flush_pmd_entry(pmd);
1336 } while (pmd++, addr = next, addr != end);
1337
1338 return 0;
1339}
1340#else /* !CONFIG_ARM_LPAE */
1341static int msm_pm_idmap_add_pmd(pud_t *pud, unsigned long addr,
1342 unsigned long end, unsigned long prot)
1343{
1344 pmd_t *pmd = pmd_offset(pud, addr);
1345
1346 addr = (addr & PMD_MASK) | prot;
1347 pmd[0] = __pmd(addr);
1348 addr += SECTION_SIZE;
1349 pmd[1] = __pmd(addr);
1350 flush_pmd_entry(pmd);
1351
1352 return 0;
1353}
1354#endif /* CONFIG_ARM_LPAE */
1355
1356static int msm_pm_idmap_add_pud(pgd_t *pgd, unsigned long addr,
1357 unsigned long end,
1358 unsigned long prot)
1359{
1360 pud_t *pud = pud_offset(pgd, addr);
1361 unsigned long next;
1362 int ret;
1363
1364 do {
1365 next = pud_addr_end(addr, end);
1366 ret = msm_pm_idmap_add_pmd(pud, addr, next, prot);
1367 if (ret)
1368 return ret;
1369 } while (pud++, addr = next, addr != end);
1370
1371 return 0;
1372}
1373
1374static int msm_pm_add_idmap(pgd_t *pgd, unsigned long addr,
1375 unsigned long end,
1376 unsigned long prot)
1377{
1378 unsigned long next;
1379 int ret;
1380
1381 pgd += pgd_index(addr);
1382 do {
1383 next = pgd_addr_end(addr, end);
1384 ret = msm_pm_idmap_add_pud(pgd, addr, next, prot);
1385 if (ret)
1386 return ret;
1387 } while (pgd++, addr = next, addr != end);
1388
1389 return 0;
1390}
1391
1392static int msm_pm_setup_pagetable(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001393{
1394 pgd_t *pc_pgd;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001395 unsigned long exit_phys;
Laura Abbott184d8302013-06-17 17:26:40 -07001396 unsigned long end;
1397 int ret;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301398
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001399 /* Page table for cores to come back up safely. */
1400 pc_pgd = pgd_alloc(&init_mm);
1401 if (!pc_pgd)
1402 return -ENOMEM;
1403
Steve Mucklef132c6c2012-06-06 18:30:57 -07001404 exit_phys = virt_to_phys(msm_pm_collapse_exit);
1405
Laura Abbott184d8302013-06-17 17:26:40 -07001406 /*
1407 * Make the (hopefully) reasonable assumption that the code size of
1408 * msm_pm_collapse_exit won't be more than a section in size
1409 */
1410 end = exit_phys + SECTION_SIZE;
1411
1412 ret = msm_pm_add_idmap(pc_pgd, exit_phys, end,
Archana Sathyakumar20269432013-07-05 14:22:01 -06001413 PMD_TYPE_SECT | PMD_SECT_AP_WRITE | PMD_SECT_AF);
Laura Abbott184d8302013-06-17 17:26:40 -07001414
1415 if (ret)
1416 return ret;
1417
1418 msm_pm_pc_pgd = virt_to_phys(pc_pgd);
1419 clean_caches((unsigned long)&msm_pm_pc_pgd, sizeof(msm_pm_pc_pgd),
1420 virt_to_phys(&msm_pm_pc_pgd));
1421
1422 return 0;
1423}
1424
1425static int __init msm_pm_setup_saved_state(void)
1426{
1427 int ret;
1428 dma_addr_t temp_phys;
1429
1430 ret = msm_pm_setup_pagetable();
1431 if (ret)
1432 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001433
Laura Abbott2ff1d122013-04-24 16:13:03 -07001434 msm_saved_state = dma_zalloc_coherent(NULL, CPU_SAVED_STATE_SIZE *
1435 num_possible_cpus(),
1436 &temp_phys, 0);
1437
Steve Mucklefcece052012-02-18 20:09:58 -08001438 if (!msm_saved_state)
1439 return -ENOMEM;
1440
Laura Abbott2ff1d122013-04-24 16:13:03 -07001441 /*
1442 * Explicitly cast here since msm_saved_state_phys is defined
1443 * in assembly and we want to avoid any kind of truncation
1444 * or endian problems.
1445 */
1446 msm_saved_state_phys = (unsigned long)temp_phys;
1447
Praveen Chidambaramede45d02013-02-20 17:45:21 -07001448 return 0;
1449}
Laura Abbott2ff1d122013-04-24 16:13:03 -07001450arch_initcall(msm_pm_setup_saved_state);
Praveen Chidambaramede45d02013-02-20 17:45:21 -07001451
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001452static void setup_broadcast_timer(void *arg)
1453{
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001454 int cpu = smp_processor_id();
1455
Mahesh Sivasubramanian738eaf62013-04-30 12:42:59 -06001456 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ON, &cpu);
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001457}
1458
1459static int setup_broadcast_cpuhp_notify(struct notifier_block *n,
1460 unsigned long action, void *hcpu)
1461{
Mahesh Sivasubramanian738eaf62013-04-30 12:42:59 -06001462 int cpu = (unsigned long)hcpu;
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001463
1464 switch (action & ~CPU_TASKS_FROZEN) {
1465 case CPU_ONLINE:
Mahesh Sivasubramanian738eaf62013-04-30 12:42:59 -06001466 smp_call_function_single(cpu, setup_broadcast_timer, NULL, 1);
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001467 break;
1468 }
1469
1470 return NOTIFY_OK;
1471}
1472
1473static struct notifier_block setup_broadcast_notifier = {
1474 .notifier_call = setup_broadcast_cpuhp_notify,
1475};
1476
Praveen Chidambaramede45d02013-02-20 17:45:21 -07001477static int __init msm_pm_init(void)
1478{
1479 enum msm_pm_time_stats_id enable_stats[] = {
1480 MSM_PM_STAT_IDLE_WFI,
1481 MSM_PM_STAT_RETENTION,
1482 MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE,
1483 MSM_PM_STAT_IDLE_POWER_COLLAPSE,
1484 MSM_PM_STAT_SUSPEND,
1485 };
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001486 msm_pm_mode_sysfs_add();
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301487 msm_pm_add_stats(enable_stats, ARRAY_SIZE(enable_stats));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001488 suspend_set_ops(&msm_pm_ops);
Girish Mahadevan32493612013-05-10 14:37:17 -06001489 hrtimer_init(&pm_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001490 msm_cpuidle_init();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001491
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001492 if (msm_pm_pc_reset_timer) {
Mahesh Sivasubramanian738eaf62013-04-30 12:42:59 -06001493 on_each_cpu(setup_broadcast_timer, NULL, 1);
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001494 register_cpu_notifier(&setup_broadcast_notifier);
1495 }
1496
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001497 return 0;
1498}
1499
Girish Mahadevan55944992012-10-26 11:03:07 -06001500static void __devinit msm_pm_set_flush_fn(uint32_t pc_mode)
1501{
1502 msm_pm_disable_l2_fn = NULL;
1503 msm_pm_enable_l2_fn = NULL;
1504 msm_pm_flush_l2_fn = outer_flush_all;
1505
1506 if (pc_mode == MSM_PM_PC_NOTZ_L2_EXT) {
1507 msm_pm_disable_l2_fn = outer_disable;
1508 msm_pm_enable_l2_fn = outer_resume;
1509 }
1510}
1511
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001512struct msm_pc_debug_counters_buffer {
1513 void __iomem *reg;
1514 u32 len;
1515 char buf[MAX_BUF_SIZE];
1516};
1517
1518static inline u32 msm_pc_debug_counters_read_register(
1519 void __iomem *reg, int index , int offset)
1520{
1521 return readl_relaxed(reg + (index * 4 + offset) * 4);
1522}
1523
1524static char *counter_name[] = {
1525 "PC Entry Counter",
1526 "Warmboot Entry Counter",
1527 "PC Bailout Counter"
1528};
1529
1530static int msm_pc_debug_counters_copy(
1531 struct msm_pc_debug_counters_buffer *data)
1532{
1533 int j;
1534 u32 stat;
1535 unsigned int cpu;
1536
1537 for_each_possible_cpu(cpu) {
1538 data->len += scnprintf(data->buf + data->len,
1539 sizeof(data->buf)-data->len,
1540 "CPU%d\n", cpu);
1541
1542 for (j = 0; j < NUM_OF_COUNTERS; j++) {
1543 stat = msm_pc_debug_counters_read_register(
1544 data->reg, cpu, j);
1545 data->len += scnprintf(data->buf + data->len,
1546 sizeof(data->buf)-data->len,
1547 "\t%s : %d\n", counter_name[j],
1548 stat);
1549 }
1550
1551 }
1552
1553 return data->len;
1554}
1555
1556static int msm_pc_debug_counters_file_read(struct file *file,
1557 char __user *bufu, size_t count, loff_t *ppos)
1558{
1559 struct msm_pc_debug_counters_buffer *data;
1560
1561 data = file->private_data;
1562
1563 if (!data)
1564 return -EINVAL;
1565
Mahesh Sivasubramanian6900fdc2013-04-18 14:02:52 -06001566 if (!bufu)
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001567 return -EINVAL;
1568
1569 if (!access_ok(VERIFY_WRITE, bufu, count))
1570 return -EFAULT;
1571
1572 if (*ppos >= data->len && data->len == 0)
1573 data->len = msm_pc_debug_counters_copy(data);
1574
1575 return simple_read_from_buffer(bufu, count, ppos,
1576 data->buf, data->len);
1577}
1578
1579static int msm_pc_debug_counters_file_open(struct inode *inode,
1580 struct file *file)
1581{
1582 struct msm_pc_debug_counters_buffer *buf;
1583 void __iomem *msm_pc_debug_counters_reg;
1584
1585 msm_pc_debug_counters_reg = inode->i_private;
1586
1587 if (!msm_pc_debug_counters_reg)
1588 return -EINVAL;
1589
1590 file->private_data = kzalloc(
1591 sizeof(struct msm_pc_debug_counters_buffer), GFP_KERNEL);
1592
1593 if (!file->private_data) {
1594 pr_err("%s: ERROR kmalloc failed to allocate %d bytes\n",
1595 __func__, sizeof(struct msm_pc_debug_counters_buffer));
1596
1597 return -ENOMEM;
1598 }
1599
1600 buf = file->private_data;
1601 buf->reg = msm_pc_debug_counters_reg;
1602
1603 return 0;
1604}
1605
1606static int msm_pc_debug_counters_file_close(struct inode *inode,
1607 struct file *file)
1608{
1609 kfree(file->private_data);
1610 return 0;
1611}
1612
1613static const struct file_operations msm_pc_debug_counters_fops = {
1614 .open = msm_pc_debug_counters_file_open,
1615 .read = msm_pc_debug_counters_file_read,
1616 .release = msm_pc_debug_counters_file_close,
1617 .llseek = no_llseek,
1618};
1619
Girish Mahadevan55944992012-10-26 11:03:07 -06001620static int __devinit msm_pm_8x60_probe(struct platform_device *pdev)
1621{
1622 char *key = NULL;
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001623 struct dentry *dent = NULL;
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001624 struct resource *res = NULL;
1625 int i ;
1626 struct msm_pm_init_data_type pdata_local;
Girish Mahadevan55944992012-10-26 11:03:07 -06001627 int ret = 0;
1628
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001629 memset(&pdata_local, 0, sizeof(struct msm_pm_init_data_type));
1630
1631 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1632 if (res) {
1633 msm_pc_debug_counters_phys = res->start;
1634 WARN_ON(resource_size(res) < SZ_64);
1635 msm_pc_debug_counters = devm_ioremap(&pdev->dev, res->start,
1636 resource_size(res));
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001637 if (msm_pc_debug_counters)
1638 for (i = 0; i < resource_size(res)/4; i++)
1639 __raw_writel(0, msm_pc_debug_counters + i * 4);
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001640
1641 }
1642
1643 if (!msm_pc_debug_counters) {
1644 msm_pc_debug_counters = 0;
1645 msm_pc_debug_counters_phys = 0;
Priyanka Mathur13bdad12013-01-28 15:52:56 -08001646 } else {
1647 dent = debugfs_create_file("pc_debug_counter", S_IRUGO, NULL,
1648 msm_pc_debug_counters,
1649 &msm_pc_debug_counters_fops);
1650 if (!dent)
1651 pr_err("%s: ERROR debugfs_create_file failed\n",
1652 __func__);
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001653 }
1654
Girish Mahadevan55944992012-10-26 11:03:07 -06001655 if (!pdev->dev.of_node) {
1656 struct msm_pm_init_data_type *d = pdev->dev.platform_data;
1657
1658 if (!d)
1659 goto pm_8x60_probe_done;
1660
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001661 memcpy(&pdata_local, d, sizeof(struct msm_pm_init_data_type));
1662
Girish Mahadevan55944992012-10-26 11:03:07 -06001663 } else {
1664 key = "qcom,pc-mode";
Archana Sathyakumar2b91dc82013-02-01 17:38:23 -07001665 ret = msm_pm_get_pc_mode(pdev->dev.of_node,
1666 key,
1667 &pdata_local.pc_mode);
Girish Mahadevan55944992012-10-26 11:03:07 -06001668 if (ret) {
Archana Sathyakumar2b91dc82013-02-01 17:38:23 -07001669 pr_debug("%s: Error reading key %s",
Girish Mahadevan55944992012-10-26 11:03:07 -06001670 __func__, key);
Archana Sathyakumar2b91dc82013-02-01 17:38:23 -07001671 return -EINVAL;
Girish Mahadevan55944992012-10-26 11:03:07 -06001672 }
Girish Mahadevan55944992012-10-26 11:03:07 -06001673
1674 key = "qcom,use-sync-timer";
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001675 pdata_local.use_sync_timer =
Girish Mahadevan55944992012-10-26 11:03:07 -06001676 of_property_read_bool(pdev->dev.of_node, key);
Praveen Chidambaram4b8df032013-01-18 16:21:16 -07001677
1678 key = "qcom,saw-turns-off-pll";
1679 msm_no_ramp_down_pc = of_property_read_bool(pdev->dev.of_node,
1680 key);
Mahesh Sivasubramanianb7bda882013-04-08 09:55:48 -06001681
1682 key = "qcom,pc-resets-timer";
1683 msm_pm_pc_reset_timer = of_property_read_bool(
1684 pdev->dev.of_node, key);
Girish Mahadevan55944992012-10-26 11:03:07 -06001685 }
1686
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001687 if (pdata_local.cp15_data.reg_data &&
1688 pdata_local.cp15_data.reg_saved_state_size > 0) {
1689 cp15_data.reg_data = kzalloc(sizeof(uint32_t) *
1690 pdata_local.cp15_data.reg_saved_state_size,
1691 GFP_KERNEL);
1692 if (!cp15_data.reg_data)
1693 return -ENOMEM;
1694
1695 cp15_data.reg_val = kzalloc(sizeof(uint32_t) *
1696 pdata_local.cp15_data.reg_saved_state_size,
1697 GFP_KERNEL);
1698 if (cp15_data.reg_val)
1699 return -ENOMEM;
1700
1701 memcpy(cp15_data.reg_data, pdata_local.cp15_data.reg_data,
1702 pdata_local.cp15_data.reg_saved_state_size *
1703 sizeof(uint32_t));
1704 }
1705
1706 msm_pm_set_flush_fn(pdata_local.pc_mode);
1707 msm_pm_use_sync_timer = pdata_local.use_sync_timer;
1708 msm_pm_retention_calls_tz = pdata_local.retention_calls_tz;
1709
Girish Mahadevan55944992012-10-26 11:03:07 -06001710pm_8x60_probe_done:
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001711 msm_pm_init();
Anji Jonnala02dac8d2013-03-06 21:31:04 +05301712 if (pdev->dev.of_node)
1713 of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
1714
Girish Mahadevan55944992012-10-26 11:03:07 -06001715 return ret;
1716}
1717
1718static struct of_device_id msm_pm_8x60_table[] = {
1719 {.compatible = "qcom,pm-8x60"},
1720 {},
1721};
1722
1723static struct platform_driver msm_pm_8x60_driver = {
1724 .probe = msm_pm_8x60_probe,
1725 .driver = {
1726 .name = "pm-8x60",
1727 .owner = THIS_MODULE,
1728 .of_match_table = msm_pm_8x60_table,
1729 },
1730};
1731
1732static int __init msm_pm_8x60_init(void)
1733{
Anji Jonnala02dac8d2013-03-06 21:31:04 +05301734 int rc;
1735
Girish Mahadevanec50d2d2013-05-15 12:07:38 -06001736 rc = platform_driver_register(&msm_cpu_pm_snoc_client_driver);
1737
1738 if (rc) {
1739 pr_err("%s(): failed to register driver %s\n", __func__,
1740 msm_cpu_pm_snoc_client_driver.driver.name);
1741 return rc;
1742 }
1743
Girish Mahadevan55944992012-10-26 11:03:07 -06001744 return platform_driver_register(&msm_pm_8x60_driver);
1745}
Praveen Chidambaramf27a5152013-02-01 11:44:53 -07001746device_initcall(msm_pm_8x60_init);
Mahesh Sivasubramanian59ffcb02013-05-31 15:08:15 -06001747
1748void __init msm_pm_sleep_status_init(void)
1749{
1750 platform_driver_register(&msm_cpu_status_driver);
1751}