Priyanka Mathur | 26b4a4b | 2012-11-05 13:45:45 -0800 | [diff] [blame^] | 1 | /* Copyright (c) 2010-2012, The Linux Foundation. All rights reserved. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2 | * |
| 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 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/completion.h> |
| 18 | #include <linux/cpuidle.h> |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/io.h> |
| 21 | #include <linux/ktime.h> |
| 22 | #include <linux/pm.h> |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 23 | #include <linux/pm_qos.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 24 | #include <linux/smp.h> |
| 25 | #include <linux/suspend.h> |
| 26 | #include <linux/tick.h> |
Mahesh Sivasubramanian | cb39662 | 2012-03-14 14:50:37 -0600 | [diff] [blame] | 27 | #include <linux/platform_device.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 28 | #include <mach/msm_iomap.h> |
Praveen Chidambaram | 192979f | 2012-04-25 18:30:23 -0600 | [diff] [blame] | 29 | #include <mach/socinfo.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 30 | #include <mach/system.h> |
Girish Mahadevan | d27ca4a | 2012-08-15 09:21:23 -0600 | [diff] [blame] | 31 | #include <mach/scm.h> |
| 32 | #include <mach/socinfo.h> |
Praveen Chidambaram | c594a09 | 2012-09-18 19:48:29 -0600 | [diff] [blame] | 33 | #include <mach/msm-krait-l2-accessors.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 34 | #include <asm/cacheflush.h> |
| 35 | #include <asm/hardware/gic.h> |
| 36 | #include <asm/pgtable.h> |
| 37 | #include <asm/pgalloc.h> |
Girish Mahadevan | 5594499 | 2012-10-26 11:03:07 -0600 | [diff] [blame] | 38 | #include <asm/outercache.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 39 | #ifdef CONFIG_VFP |
| 40 | #include <asm/vfp.h> |
| 41 | #endif |
| 42 | |
| 43 | #include "acpuclock.h" |
| 44 | #include "clock.h" |
| 45 | #include "avs.h" |
Abhijeet Dharmapurikar | efaca4f | 2011-12-27 16:24:07 -0800 | [diff] [blame] | 46 | #include <mach/cpuidle.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 47 | #include "idle.h" |
Matt Wagantall | 7cca464 | 2012-02-01 16:43:24 -0800 | [diff] [blame] | 48 | #include "pm.h" |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 49 | #include "scm-boot.h" |
| 50 | #include "spm.h" |
| 51 | #include "timer.h" |
Maheshkumar Sivasubramanian | 8ccc16e | 2011-10-25 15:59:57 -0600 | [diff] [blame] | 52 | #include "pm-boot.h" |
Girish Mahadevan | dc318fd | 2012-08-17 16:48:05 -0600 | [diff] [blame] | 53 | #include <mach/event_timer.h> |
Priyanka Mathur | 26b4a4b | 2012-11-05 13:45:45 -0800 | [diff] [blame^] | 54 | #define CREATE_TRACE_POINTS |
| 55 | #include "trace_msm_low_power.h" |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 56 | /****************************************************************************** |
| 57 | * Debug Definitions |
| 58 | *****************************************************************************/ |
| 59 | |
| 60 | enum { |
| 61 | MSM_PM_DEBUG_SUSPEND = BIT(0), |
| 62 | MSM_PM_DEBUG_POWER_COLLAPSE = BIT(1), |
| 63 | MSM_PM_DEBUG_SUSPEND_LIMITS = BIT(2), |
| 64 | MSM_PM_DEBUG_CLOCK = BIT(3), |
| 65 | MSM_PM_DEBUG_RESET_VECTOR = BIT(4), |
Karthik Parsha | 6fb932d | 2012-01-24 18:04:12 -0800 | [diff] [blame] | 66 | MSM_PM_DEBUG_IDLE_CLK = BIT(5), |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 67 | MSM_PM_DEBUG_IDLE = BIT(6), |
| 68 | MSM_PM_DEBUG_IDLE_LIMITS = BIT(7), |
| 69 | MSM_PM_DEBUG_HOTPLUG = BIT(8), |
| 70 | }; |
| 71 | |
| 72 | static int msm_pm_debug_mask = 1; |
| 73 | module_param_named( |
| 74 | debug_mask, msm_pm_debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP |
| 75 | ); |
Girish Mahadevan | d27ca4a | 2012-08-15 09:21:23 -0600 | [diff] [blame] | 76 | static int msm_pm_retention_tz_call; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 77 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 78 | /****************************************************************************** |
| 79 | * Sleep Modes and Parameters |
| 80 | *****************************************************************************/ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 81 | enum { |
| 82 | MSM_PM_MODE_ATTR_SUSPEND, |
| 83 | MSM_PM_MODE_ATTR_IDLE, |
| 84 | MSM_PM_MODE_ATTR_NR, |
| 85 | }; |
| 86 | |
Girish Mahadevan | d27ca4a | 2012-08-15 09:21:23 -0600 | [diff] [blame] | 87 | #define SCM_L2_RETENTION (0x2) |
| 88 | #define SCM_CMD_TERMINATE_PC (0x2) |
| 89 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 90 | static char *msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_NR] = { |
| 91 | [MSM_PM_MODE_ATTR_SUSPEND] = "suspend_enabled", |
| 92 | [MSM_PM_MODE_ATTR_IDLE] = "idle_enabled", |
| 93 | }; |
| 94 | |
| 95 | struct msm_pm_kobj_attribute { |
| 96 | unsigned int cpu; |
| 97 | struct kobj_attribute ka; |
| 98 | }; |
| 99 | |
| 100 | #define GET_CPU_OF_ATTR(attr) \ |
| 101 | (container_of(attr, struct msm_pm_kobj_attribute, ka)->cpu) |
| 102 | |
| 103 | struct msm_pm_sysfs_sleep_mode { |
| 104 | struct kobject *kobj; |
| 105 | struct attribute_group attr_group; |
| 106 | struct attribute *attrs[MSM_PM_MODE_ATTR_NR + 1]; |
| 107 | struct msm_pm_kobj_attribute kas[MSM_PM_MODE_ATTR_NR]; |
| 108 | }; |
| 109 | |
| 110 | static char *msm_pm_sleep_mode_labels[MSM_PM_SLEEP_MODE_NR] = { |
| 111 | [MSM_PM_SLEEP_MODE_POWER_COLLAPSE] = "power_collapse", |
| 112 | [MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT] = "wfi", |
Praveen Chidambaram | d3d844d | 2012-04-24 09:47:38 -0600 | [diff] [blame] | 113 | [MSM_PM_SLEEP_MODE_RETENTION] = "retention", |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 114 | [MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE] = |
| 115 | "standalone_power_collapse", |
| 116 | }; |
| 117 | |
Girish Mahadevan | 5594499 | 2012-10-26 11:03:07 -0600 | [diff] [blame] | 118 | static struct msm_pm_init_data_type msm_pm_init_data; |
Girish Mahadevan | dc318fd | 2012-08-17 16:48:05 -0600 | [diff] [blame] | 119 | static struct hrtimer pm_hrtimer; |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 120 | static struct msm_pm_sleep_ops pm_sleep_ops; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 121 | /* |
| 122 | * Write out the attribute. |
| 123 | */ |
| 124 | static ssize_t msm_pm_mode_attr_show( |
| 125 | struct kobject *kobj, struct kobj_attribute *attr, char *buf) |
| 126 | { |
| 127 | int ret = -EINVAL; |
| 128 | int i; |
| 129 | |
| 130 | for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) { |
| 131 | struct kernel_param kp; |
| 132 | unsigned int cpu; |
| 133 | struct msm_pm_platform_data *mode; |
| 134 | |
| 135 | if (msm_pm_sleep_mode_labels[i] == NULL) |
| 136 | continue; |
| 137 | |
| 138 | if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i])) |
| 139 | continue; |
| 140 | |
| 141 | cpu = GET_CPU_OF_ATTR(attr); |
Praveen Chidambaram | 42da9d2 | 2012-03-30 12:16:34 -0600 | [diff] [blame] | 142 | mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)]; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 143 | |
| 144 | if (!strcmp(attr->attr.name, |
| 145 | msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) { |
| 146 | u32 arg = mode->suspend_enabled; |
| 147 | kp.arg = &arg; |
| 148 | ret = param_get_ulong(buf, &kp); |
| 149 | } else if (!strcmp(attr->attr.name, |
| 150 | msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) { |
| 151 | u32 arg = mode->idle_enabled; |
| 152 | kp.arg = &arg; |
| 153 | ret = param_get_ulong(buf, &kp); |
| 154 | } |
| 155 | |
| 156 | break; |
| 157 | } |
| 158 | |
| 159 | if (ret > 0) { |
Praveen Chidambaram | 2b0fdd0 | 2011-10-28 16:40:58 -0600 | [diff] [blame] | 160 | strlcat(buf, "\n", PAGE_SIZE); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 161 | ret++; |
| 162 | } |
| 163 | |
| 164 | return ret; |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | * Read in the new attribute value. |
| 169 | */ |
| 170 | static ssize_t msm_pm_mode_attr_store(struct kobject *kobj, |
| 171 | struct kobj_attribute *attr, const char *buf, size_t count) |
| 172 | { |
| 173 | int ret = -EINVAL; |
| 174 | int i; |
| 175 | |
| 176 | for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) { |
| 177 | struct kernel_param kp; |
| 178 | unsigned int cpu; |
| 179 | struct msm_pm_platform_data *mode; |
| 180 | |
| 181 | if (msm_pm_sleep_mode_labels[i] == NULL) |
| 182 | continue; |
| 183 | |
| 184 | if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i])) |
| 185 | continue; |
| 186 | |
| 187 | cpu = GET_CPU_OF_ATTR(attr); |
Praveen Chidambaram | 42da9d2 | 2012-03-30 12:16:34 -0600 | [diff] [blame] | 188 | mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)]; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 189 | |
| 190 | if (!strcmp(attr->attr.name, |
| 191 | msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) { |
| 192 | kp.arg = &mode->suspend_enabled; |
| 193 | ret = param_set_byte(buf, &kp); |
| 194 | } else if (!strcmp(attr->attr.name, |
| 195 | msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) { |
| 196 | kp.arg = &mode->idle_enabled; |
| 197 | ret = param_set_byte(buf, &kp); |
| 198 | } |
| 199 | |
| 200 | break; |
| 201 | } |
| 202 | |
| 203 | return ret ? ret : count; |
| 204 | } |
| 205 | |
| 206 | /* |
| 207 | * Add sysfs entries for one cpu. |
| 208 | */ |
| 209 | static int __init msm_pm_mode_sysfs_add_cpu( |
| 210 | unsigned int cpu, struct kobject *modes_kobj) |
| 211 | { |
| 212 | char cpu_name[8]; |
| 213 | struct kobject *cpu_kobj; |
Praveen Chidambaram | 2b0fdd0 | 2011-10-28 16:40:58 -0600 | [diff] [blame] | 214 | struct msm_pm_sysfs_sleep_mode *mode = NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 215 | int i, j, k; |
| 216 | int ret; |
| 217 | |
| 218 | snprintf(cpu_name, sizeof(cpu_name), "cpu%u", cpu); |
| 219 | cpu_kobj = kobject_create_and_add(cpu_name, modes_kobj); |
| 220 | if (!cpu_kobj) { |
| 221 | pr_err("%s: cannot create %s kobject\n", __func__, cpu_name); |
| 222 | ret = -ENOMEM; |
| 223 | goto mode_sysfs_add_cpu_exit; |
| 224 | } |
| 225 | |
| 226 | for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) { |
| 227 | int idx = MSM_PM_MODE(cpu, i); |
| 228 | |
Praveen Chidambaram | 42da9d2 | 2012-03-30 12:16:34 -0600 | [diff] [blame] | 229 | if ((!msm_pm_sleep_modes[idx].suspend_supported) |
| 230 | && (!msm_pm_sleep_modes[idx].idle_supported)) |
| 231 | continue; |
| 232 | |
| 233 | if (!msm_pm_sleep_mode_labels[i] || |
| 234 | !msm_pm_sleep_mode_labels[i][0]) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 235 | continue; |
| 236 | |
| 237 | mode = kzalloc(sizeof(*mode), GFP_KERNEL); |
| 238 | if (!mode) { |
| 239 | pr_err("%s: cannot allocate memory for attributes\n", |
| 240 | __func__); |
| 241 | ret = -ENOMEM; |
| 242 | goto mode_sysfs_add_cpu_exit; |
| 243 | } |
| 244 | |
| 245 | mode->kobj = kobject_create_and_add( |
| 246 | msm_pm_sleep_mode_labels[i], cpu_kobj); |
| 247 | if (!mode->kobj) { |
| 248 | pr_err("%s: cannot create kobject\n", __func__); |
| 249 | ret = -ENOMEM; |
| 250 | goto mode_sysfs_add_cpu_exit; |
| 251 | } |
| 252 | |
| 253 | for (k = 0, j = 0; k < MSM_PM_MODE_ATTR_NR; k++) { |
| 254 | if ((k == MSM_PM_MODE_ATTR_IDLE) && |
Praveen Chidambaram | 42da9d2 | 2012-03-30 12:16:34 -0600 | [diff] [blame] | 255 | !msm_pm_sleep_modes[idx].idle_supported) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 256 | continue; |
| 257 | if ((k == MSM_PM_MODE_ATTR_SUSPEND) && |
Praveen Chidambaram | 42da9d2 | 2012-03-30 12:16:34 -0600 | [diff] [blame] | 258 | !msm_pm_sleep_modes[idx].suspend_supported) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 259 | continue; |
Stephen Boyd | d2059c3 | 2012-07-03 15:11:15 -0700 | [diff] [blame] | 260 | sysfs_attr_init(&mode->kas[j].ka.attr); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 261 | mode->kas[j].cpu = cpu; |
| 262 | mode->kas[j].ka.attr.mode = 0644; |
| 263 | mode->kas[j].ka.show = msm_pm_mode_attr_show; |
| 264 | mode->kas[j].ka.store = msm_pm_mode_attr_store; |
| 265 | mode->kas[j].ka.attr.name = msm_pm_mode_attr_labels[k]; |
| 266 | mode->attrs[j] = &mode->kas[j].ka.attr; |
| 267 | j++; |
| 268 | } |
| 269 | mode->attrs[j] = NULL; |
| 270 | |
| 271 | mode->attr_group.attrs = mode->attrs; |
| 272 | ret = sysfs_create_group(mode->kobj, &mode->attr_group); |
| 273 | if (ret) { |
| 274 | pr_err("%s: cannot create kobject attribute group\n", |
| 275 | __func__); |
| 276 | goto mode_sysfs_add_cpu_exit; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | ret = 0; |
| 281 | |
| 282 | mode_sysfs_add_cpu_exit: |
Praveen Chidambaram | d5ac2d3 | 2011-10-24 14:30:27 -0600 | [diff] [blame] | 283 | if (ret) { |
Praveen Chidambaram | 2cfda63 | 2011-10-11 16:58:09 -0600 | [diff] [blame] | 284 | if (mode && mode->kobj) |
| 285 | kobject_del(mode->kobj); |
| 286 | kfree(mode); |
| 287 | } |
| 288 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 289 | return ret; |
| 290 | } |
| 291 | |
| 292 | /* |
| 293 | * Add sysfs entries for the sleep modes. |
| 294 | */ |
| 295 | static int __init msm_pm_mode_sysfs_add(void) |
| 296 | { |
| 297 | struct kobject *module_kobj; |
| 298 | struct kobject *modes_kobj; |
| 299 | unsigned int cpu; |
| 300 | int ret; |
| 301 | |
| 302 | module_kobj = kset_find_obj(module_kset, KBUILD_MODNAME); |
| 303 | if (!module_kobj) { |
| 304 | pr_err("%s: cannot find kobject for module %s\n", |
| 305 | __func__, KBUILD_MODNAME); |
| 306 | ret = -ENOENT; |
| 307 | goto mode_sysfs_add_exit; |
| 308 | } |
| 309 | |
| 310 | modes_kobj = kobject_create_and_add("modes", module_kobj); |
| 311 | if (!modes_kobj) { |
| 312 | pr_err("%s: cannot create modes kobject\n", __func__); |
| 313 | ret = -ENOMEM; |
| 314 | goto mode_sysfs_add_exit; |
| 315 | } |
| 316 | |
| 317 | for_each_possible_cpu(cpu) { |
| 318 | ret = msm_pm_mode_sysfs_add_cpu(cpu, modes_kobj); |
| 319 | if (ret) |
| 320 | goto mode_sysfs_add_exit; |
| 321 | } |
| 322 | |
| 323 | ret = 0; |
| 324 | |
| 325 | mode_sysfs_add_exit: |
| 326 | return ret; |
| 327 | } |
| 328 | |
| 329 | /****************************************************************************** |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 330 | * Configure Hardware before/after Low Power Mode |
| 331 | *****************************************************************************/ |
| 332 | |
| 333 | /* |
| 334 | * Configure hardware registers in preparation for Apps power down. |
| 335 | */ |
| 336 | static void msm_pm_config_hw_before_power_down(void) |
| 337 | { |
| 338 | return; |
| 339 | } |
| 340 | |
| 341 | /* |
| 342 | * Clear hardware registers after Apps powers up. |
| 343 | */ |
| 344 | static void msm_pm_config_hw_after_power_up(void) |
| 345 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | /* |
| 349 | * Configure hardware registers in preparation for SWFI. |
| 350 | */ |
| 351 | static void msm_pm_config_hw_before_swfi(void) |
| 352 | { |
| 353 | return; |
| 354 | } |
| 355 | |
Girish Mahadevan | d27ca4a | 2012-08-15 09:21:23 -0600 | [diff] [blame] | 356 | /* |
| 357 | * Configure/Restore hardware registers in preparation for Retention. |
| 358 | */ |
| 359 | |
| 360 | static void msm_pm_config_hw_after_retention(void) |
| 361 | { |
| 362 | int ret; |
| 363 | ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false); |
| 364 | WARN_ON(ret); |
| 365 | } |
| 366 | |
| 367 | static void msm_pm_config_hw_before_retention(void) |
| 368 | { |
| 369 | return; |
| 370 | } |
| 371 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 372 | |
| 373 | /****************************************************************************** |
| 374 | * Suspend Max Sleep Time |
| 375 | *****************************************************************************/ |
| 376 | |
| 377 | #ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE |
| 378 | static int msm_pm_sleep_time_override; |
| 379 | module_param_named(sleep_time_override, |
| 380 | msm_pm_sleep_time_override, int, S_IRUGO | S_IWUSR | S_IWGRP); |
| 381 | #endif |
| 382 | |
| 383 | #define SCLK_HZ (32768) |
| 384 | #define MSM_PM_SLEEP_TICK_LIMIT (0x6DDD000) |
| 385 | |
| 386 | static uint32_t msm_pm_max_sleep_time; |
| 387 | |
| 388 | /* |
| 389 | * Convert time from nanoseconds to slow clock ticks, then cap it to the |
| 390 | * specified limit |
| 391 | */ |
| 392 | static int64_t msm_pm_convert_and_cap_time(int64_t time_ns, int64_t limit) |
| 393 | { |
| 394 | do_div(time_ns, NSEC_PER_SEC / SCLK_HZ); |
| 395 | return (time_ns > limit) ? limit : time_ns; |
| 396 | } |
| 397 | |
| 398 | /* |
| 399 | * Set the sleep time for suspend. 0 means infinite sleep time. |
| 400 | */ |
| 401 | void msm_pm_set_max_sleep_time(int64_t max_sleep_time_ns) |
| 402 | { |
| 403 | if (max_sleep_time_ns == 0) { |
| 404 | msm_pm_max_sleep_time = 0; |
| 405 | } else { |
| 406 | msm_pm_max_sleep_time = (uint32_t)msm_pm_convert_and_cap_time( |
| 407 | max_sleep_time_ns, MSM_PM_SLEEP_TICK_LIMIT); |
| 408 | |
| 409 | if (msm_pm_max_sleep_time == 0) |
| 410 | msm_pm_max_sleep_time = 1; |
| 411 | } |
| 412 | |
| 413 | if (msm_pm_debug_mask & MSM_PM_DEBUG_SUSPEND) |
| 414 | pr_info("%s: Requested %lld ns Giving %u sclk ticks\n", |
| 415 | __func__, max_sleep_time_ns, msm_pm_max_sleep_time); |
| 416 | } |
| 417 | EXPORT_SYMBOL(msm_pm_set_max_sleep_time); |
| 418 | |
Praveen Chidambaram | c594a09 | 2012-09-18 19:48:29 -0600 | [diff] [blame] | 419 | struct reg_data { |
| 420 | uint32_t reg; |
| 421 | uint32_t val; |
| 422 | }; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 423 | |
Praveen Chidambaram | c594a09 | 2012-09-18 19:48:29 -0600 | [diff] [blame] | 424 | static struct reg_data reg_saved_state[] = { |
| 425 | { .reg = 0x4501, }, |
| 426 | { .reg = 0x5501, }, |
| 427 | { .reg = 0x6501, }, |
| 428 | { .reg = 0x7501, }, |
| 429 | { .reg = 0x0500, }, |
| 430 | }; |
| 431 | |
| 432 | static unsigned int active_vdd; |
| 433 | static bool msm_pm_save_cp15; |
| 434 | static const unsigned int pc_vdd = 0x98; |
| 435 | |
| 436 | static void msm_pm_save_cpu_reg(void) |
| 437 | { |
| 438 | int i; |
| 439 | |
| 440 | /* Only on core0 */ |
| 441 | if (smp_processor_id()) |
| 442 | return; |
| 443 | |
| 444 | /** |
| 445 | * On some targets, L2 PC will turn off may reset the core |
| 446 | * configuration for the mux and the default may not make the core |
| 447 | * happy when it resumes. |
| 448 | * Save the active vdd, and set the core vdd to QSB max vdd, so that |
| 449 | * when the core resumes, it is capable of supporting the current QSB |
| 450 | * rate. Then restore the active vdd before switching the acpuclk rate. |
| 451 | */ |
| 452 | if (msm_pm_get_l2_flush_flag() == 1) { |
| 453 | active_vdd = msm_spm_get_vdd(0); |
| 454 | for (i = 0; i < ARRAY_SIZE(reg_saved_state); i++) |
| 455 | reg_saved_state[i].val = |
| 456 | get_l2_indirect_reg(reg_saved_state[i].reg); |
| 457 | msm_spm_set_vdd(0, pc_vdd); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | static void msm_pm_restore_cpu_reg(void) |
| 462 | { |
| 463 | int i; |
| 464 | |
| 465 | /* Only on core0 */ |
| 466 | if (smp_processor_id()) |
| 467 | return; |
| 468 | |
| 469 | if (msm_pm_get_l2_flush_flag() == 1) { |
| 470 | for (i = 0; i < ARRAY_SIZE(reg_saved_state); i++) |
| 471 | set_l2_indirect_reg(reg_saved_state[i].reg, |
| 472 | reg_saved_state[i].val); |
| 473 | msm_spm_set_vdd(0, active_vdd); |
| 474 | } |
| 475 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 476 | |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 477 | static void *msm_pm_idle_rs_limits; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 478 | |
| 479 | static void msm_pm_swfi(void) |
| 480 | { |
| 481 | msm_pm_config_hw_before_swfi(); |
| 482 | msm_arch_idle(); |
| 483 | } |
| 484 | |
Praveen Chidambaram | d3d844d | 2012-04-24 09:47:38 -0600 | [diff] [blame] | 485 | |
| 486 | static void msm_pm_retention(void) |
| 487 | { |
| 488 | int ret = 0; |
| 489 | |
Girish Mahadevan | d27ca4a | 2012-08-15 09:21:23 -0600 | [diff] [blame] | 490 | msm_pm_config_hw_before_retention(); |
Praveen Chidambaram | d3d844d | 2012-04-24 09:47:38 -0600 | [diff] [blame] | 491 | ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_POWER_RETENTION, false); |
| 492 | WARN_ON(ret); |
Girish Mahadevan | d27ca4a | 2012-08-15 09:21:23 -0600 | [diff] [blame] | 493 | |
| 494 | if (msm_pm_retention_tz_call) |
| 495 | scm_call_atomic1(SCM_SVC_BOOT, SCM_CMD_TERMINATE_PC, |
| 496 | SCM_L2_RETENTION); |
| 497 | else |
| 498 | msm_arch_idle(); |
| 499 | |
| 500 | msm_pm_config_hw_after_retention(); |
Praveen Chidambaram | d3d844d | 2012-04-24 09:47:38 -0600 | [diff] [blame] | 501 | } |
| 502 | |
Stephen Boyd | b29750d | 2012-02-21 01:21:32 -0800 | [diff] [blame] | 503 | static bool __ref msm_pm_spm_power_collapse( |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 504 | unsigned int cpu, bool from_idle, bool notify_rpm) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 505 | { |
| 506 | void *entry; |
Maheshkumar Sivasubramanian | dd93ecf | 2011-09-15 19:39:14 -0600 | [diff] [blame] | 507 | bool collapsed = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 508 | int ret; |
Rohit Vaswani | e78dfb6 | 2012-02-21 10:29:29 -0800 | [diff] [blame] | 509 | unsigned int saved_gic_cpu_ctrl; |
| 510 | |
| 511 | saved_gic_cpu_ctrl = readl_relaxed(MSM_QGIC_CPU_BASE + GIC_CPU_CTRL); |
| 512 | mb(); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 513 | |
| 514 | if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask) |
| 515 | pr_info("CPU%u: %s: notify_rpm %d\n", |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 516 | cpu, __func__, (int) notify_rpm); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 517 | |
| 518 | ret = msm_spm_set_low_power_mode( |
| 519 | MSM_SPM_MODE_POWER_COLLAPSE, notify_rpm); |
| 520 | WARN_ON(ret); |
| 521 | |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 522 | entry = (!cpu || from_idle) ? |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 523 | msm_pm_collapse_exit : msm_secondary_startup; |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 524 | msm_pm_boot_config_before_pc(cpu, virt_to_phys(entry)); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 525 | |
| 526 | if (MSM_PM_DEBUG_RESET_VECTOR & msm_pm_debug_mask) |
| 527 | pr_info("CPU%u: %s: program vector to %p\n", |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 528 | cpu, __func__, entry); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 529 | |
| 530 | #ifdef CONFIG_VFP |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 531 | vfp_pm_suspend(); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 532 | #endif |
Girish Mahadevan | 5594499 | 2012-10-26 11:03:07 -0600 | [diff] [blame] | 533 | collapsed = msm_pm_collapse(); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 534 | |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 535 | msm_pm_boot_config_after_pc(cpu); |
Maheshkumar Sivasubramanian | 8ccc16e | 2011-10-25 15:59:57 -0600 | [diff] [blame] | 536 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 537 | if (collapsed) { |
| 538 | #ifdef CONFIG_VFP |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 539 | vfp_pm_resume(); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 540 | #endif |
| 541 | cpu_init(); |
| 542 | writel(0xF0, MSM_QGIC_CPU_BASE + GIC_CPU_PRIMASK); |
Rohit Vaswani | e78dfb6 | 2012-02-21 10:29:29 -0800 | [diff] [blame] | 543 | writel_relaxed(saved_gic_cpu_ctrl, |
| 544 | MSM_QGIC_CPU_BASE + GIC_CPU_CTRL); |
| 545 | mb(); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 546 | local_fiq_enable(); |
| 547 | } |
| 548 | |
| 549 | if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask) |
| 550 | pr_info("CPU%u: %s: msm_pm_collapse returned, collapsed %d\n", |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 551 | cpu, __func__, collapsed); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 552 | |
| 553 | ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false); |
| 554 | WARN_ON(ret); |
Maheshkumar Sivasubramanian | dd93ecf | 2011-09-15 19:39:14 -0600 | [diff] [blame] | 555 | return collapsed; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 556 | } |
| 557 | |
Maheshkumar Sivasubramanian | dd93ecf | 2011-09-15 19:39:14 -0600 | [diff] [blame] | 558 | static bool msm_pm_power_collapse_standalone(bool from_idle) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 559 | { |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 560 | unsigned int cpu = smp_processor_id(); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 561 | unsigned int avsdscr_setting; |
Mahesh Sivasubramanian | ad138da | 2012-10-24 13:53:48 -0600 | [diff] [blame] | 562 | unsigned int avscsr_enable; |
Maheshkumar Sivasubramanian | dd93ecf | 2011-09-15 19:39:14 -0600 | [diff] [blame] | 563 | bool collapsed; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 564 | |
| 565 | avsdscr_setting = avs_get_avsdscr(); |
Mahesh Sivasubramanian | ad138da | 2012-10-24 13:53:48 -0600 | [diff] [blame] | 566 | avscsr_enable = avs_disable(); |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 567 | collapsed = msm_pm_spm_power_collapse(cpu, from_idle, false); |
Mahesh Sivasubramanian | ad138da | 2012-10-24 13:53:48 -0600 | [diff] [blame] | 568 | avs_enable(avscsr_enable); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 569 | avs_reset_delays(avsdscr_setting); |
Maheshkumar Sivasubramanian | dd93ecf | 2011-09-15 19:39:14 -0600 | [diff] [blame] | 570 | return collapsed; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 571 | } |
| 572 | |
Maheshkumar Sivasubramanian | dd93ecf | 2011-09-15 19:39:14 -0600 | [diff] [blame] | 573 | static bool msm_pm_power_collapse(bool from_idle) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 574 | { |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 575 | unsigned int cpu = smp_processor_id(); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 576 | unsigned long saved_acpuclk_rate; |
| 577 | unsigned int avsdscr_setting; |
Mahesh Sivasubramanian | ad138da | 2012-10-24 13:53:48 -0600 | [diff] [blame] | 578 | unsigned int avscsr_enable; |
Maheshkumar Sivasubramanian | dd93ecf | 2011-09-15 19:39:14 -0600 | [diff] [blame] | 579 | bool collapsed; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 580 | |
| 581 | if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask) |
| 582 | pr_info("CPU%u: %s: idle %d\n", |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 583 | cpu, __func__, (int)from_idle); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 584 | |
| 585 | msm_pm_config_hw_before_power_down(); |
| 586 | if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask) |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 587 | pr_info("CPU%u: %s: pre power down\n", cpu, __func__); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 588 | |
| 589 | avsdscr_setting = avs_get_avsdscr(); |
Mahesh Sivasubramanian | ad138da | 2012-10-24 13:53:48 -0600 | [diff] [blame] | 590 | avscsr_enable = avs_disable(); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 591 | |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 592 | if (cpu_online(cpu)) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 593 | saved_acpuclk_rate = acpuclk_power_collapse(); |
| 594 | else |
| 595 | saved_acpuclk_rate = 0; |
| 596 | |
| 597 | if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask) |
| 598 | pr_info("CPU%u: %s: change clock rate (old rate = %lu)\n", |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 599 | cpu, __func__, saved_acpuclk_rate); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 600 | |
Praveen Chidambaram | c594a09 | 2012-09-18 19:48:29 -0600 | [diff] [blame] | 601 | if (msm_pm_save_cp15) |
| 602 | msm_pm_save_cpu_reg(); |
| 603 | |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 604 | collapsed = msm_pm_spm_power_collapse(cpu, from_idle, true); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 605 | |
Praveen Chidambaram | c594a09 | 2012-09-18 19:48:29 -0600 | [diff] [blame] | 606 | if (msm_pm_save_cp15) |
| 607 | msm_pm_restore_cpu_reg(); |
| 608 | |
Girish Mahadevan | 4e025d9 | 2012-02-29 13:26:51 -0700 | [diff] [blame] | 609 | if (cpu_online(cpu)) { |
| 610 | if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask) |
| 611 | pr_info("CPU%u: %s: restore clock rate to %lu\n", |
| 612 | cpu, __func__, saved_acpuclk_rate); |
| 613 | if (acpuclk_set_rate(cpu, saved_acpuclk_rate, SETRATE_PC) < 0) |
| 614 | pr_err("CPU%u: %s: failed to restore clock rate(%lu)\n", |
| 615 | cpu, __func__, saved_acpuclk_rate); |
| 616 | } else { |
| 617 | unsigned int gic_dist_enabled; |
| 618 | unsigned int gic_dist_pending; |
| 619 | gic_dist_enabled = readl_relaxed( |
| 620 | MSM_QGIC_DIST_BASE + GIC_DIST_ENABLE_CLEAR); |
| 621 | gic_dist_pending = readl_relaxed( |
| 622 | MSM_QGIC_DIST_BASE + GIC_DIST_PENDING_SET); |
| 623 | mb(); |
| 624 | gic_dist_pending &= gic_dist_enabled; |
| 625 | |
| 626 | if (gic_dist_pending) |
| 627 | pr_err("CPU %d interrupted during hotplug.Pending int 0x%x\n", |
| 628 | cpu, gic_dist_pending); |
| 629 | } |
| 630 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 631 | |
Mahesh Sivasubramanian | ad138da | 2012-10-24 13:53:48 -0600 | [diff] [blame] | 632 | avs_enable(avscsr_enable); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 633 | avs_reset_delays(avsdscr_setting); |
| 634 | msm_pm_config_hw_after_power_up(); |
| 635 | if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask) |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 636 | pr_info("CPU%u: %s: post power up\n", cpu, __func__); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 637 | |
| 638 | if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask) |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 639 | pr_info("CPU%u: %s: return\n", cpu, __func__); |
Maheshkumar Sivasubramanian | dd93ecf | 2011-09-15 19:39:14 -0600 | [diff] [blame] | 640 | return collapsed; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 641 | } |
| 642 | |
Praveen Chidambaram | c594a09 | 2012-09-18 19:48:29 -0600 | [diff] [blame] | 643 | static void msm_pm_target_init(void) |
Praveen Chidambaram | 192979f | 2012-04-25 18:30:23 -0600 | [diff] [blame] | 644 | { |
Praveen Chidambaram | c594a09 | 2012-09-18 19:48:29 -0600 | [diff] [blame] | 645 | if (cpu_is_apq8064()) |
| 646 | msm_pm_save_cp15 = true; |
Praveen Chidambaram | 192979f | 2012-04-25 18:30:23 -0600 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | static int64_t msm_pm_timer_enter_idle(void) |
| 650 | { |
Girish Mahadevan | 5594499 | 2012-10-26 11:03:07 -0600 | [diff] [blame] | 651 | if (msm_pm_init_data.use_sync_timer) |
Praveen Chidambaram | 192979f | 2012-04-25 18:30:23 -0600 | [diff] [blame] | 652 | return ktime_to_ns(tick_nohz_get_sleep_length()); |
| 653 | |
| 654 | return msm_timer_enter_idle(); |
| 655 | } |
| 656 | |
| 657 | static void msm_pm_timer_exit_idle(bool timer_halted) |
| 658 | { |
Girish Mahadevan | 5594499 | 2012-10-26 11:03:07 -0600 | [diff] [blame] | 659 | if (msm_pm_init_data.use_sync_timer) |
Praveen Chidambaram | 192979f | 2012-04-25 18:30:23 -0600 | [diff] [blame] | 660 | return; |
| 661 | |
| 662 | msm_timer_exit_idle((int) timer_halted); |
| 663 | } |
| 664 | |
Praveen Chidambaram | 3895bde | 2012-05-14 19:42:40 +0530 | [diff] [blame] | 665 | static int64_t msm_pm_timer_enter_suspend(int64_t *period) |
| 666 | { |
Anji Jonnala | c02367a | 2012-07-01 02:56:11 +0530 | [diff] [blame] | 667 | int64_t time = 0; |
Praveen Chidambaram | 3895bde | 2012-05-14 19:42:40 +0530 | [diff] [blame] | 668 | |
Girish Mahadevan | 5594499 | 2012-10-26 11:03:07 -0600 | [diff] [blame] | 669 | if (msm_pm_init_data.use_sync_timer) |
Praveen Chidambaram | 3895bde | 2012-05-14 19:42:40 +0530 | [diff] [blame] | 670 | return sched_clock(); |
| 671 | |
| 672 | time = msm_timer_get_sclk_time(period); |
| 673 | if (!time) |
| 674 | pr_err("%s: Unable to read sclk.\n", __func__); |
| 675 | |
| 676 | return time; |
| 677 | } |
| 678 | |
| 679 | static int64_t msm_pm_timer_exit_suspend(int64_t time, int64_t period) |
| 680 | { |
Girish Mahadevan | 5594499 | 2012-10-26 11:03:07 -0600 | [diff] [blame] | 681 | if (msm_pm_init_data.use_sync_timer) |
Praveen Chidambaram | 3895bde | 2012-05-14 19:42:40 +0530 | [diff] [blame] | 682 | return sched_clock() - time; |
| 683 | |
| 684 | if (time != 0) { |
| 685 | int64_t end_time = msm_timer_get_sclk_time(NULL); |
| 686 | if (end_time != 0) { |
| 687 | time = end_time - time; |
| 688 | if (time < 0) |
| 689 | time += period; |
| 690 | } else |
| 691 | time = 0; |
| 692 | } |
| 693 | |
| 694 | return time; |
| 695 | } |
| 696 | |
Girish Mahadevan | dc318fd | 2012-08-17 16:48:05 -0600 | [diff] [blame] | 697 | /** |
| 698 | * pm_hrtimer_cb() : Callback function for hrtimer created if the |
| 699 | * core needs to be awake to handle an event. |
| 700 | * @hrtimer : Pointer to hrtimer |
| 701 | */ |
| 702 | static enum hrtimer_restart pm_hrtimer_cb(struct hrtimer *hrtimer) |
| 703 | { |
| 704 | return HRTIMER_NORESTART; |
| 705 | } |
| 706 | |
| 707 | /** |
| 708 | * msm_pm_set_timer() : Set an hrtimer to wakeup the core in time |
| 709 | * to handle an event. |
| 710 | */ |
| 711 | static void msm_pm_set_timer(uint32_t modified_time_us) |
| 712 | { |
| 713 | u64 modified_time_ns = modified_time_us * NSEC_PER_USEC; |
| 714 | ktime_t modified_ktime = ns_to_ktime(modified_time_ns); |
| 715 | pm_hrtimer.function = pm_hrtimer_cb; |
| 716 | hrtimer_start(&pm_hrtimer, modified_ktime, HRTIMER_MODE_ABS); |
| 717 | } |
| 718 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 719 | /****************************************************************************** |
| 720 | * External Idle/Suspend Functions |
| 721 | *****************************************************************************/ |
| 722 | |
| 723 | void arch_idle(void) |
| 724 | { |
| 725 | return; |
| 726 | } |
| 727 | |
Priyanka Mathur | 26b4a4b | 2012-11-05 13:45:45 -0800 | [diff] [blame^] | 728 | static inline void msm_pm_ftrace_lpm_enter(unsigned int cpu, |
| 729 | uint32_t latency, uint32_t sleep_us, |
| 730 | uint32_t wake_up, |
| 731 | enum msm_pm_sleep_mode mode) |
| 732 | { |
| 733 | switch (mode) { |
| 734 | case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT: |
| 735 | trace_msm_pm_enter_wfi(cpu, latency, sleep_us, wake_up); |
| 736 | break; |
| 737 | case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE: |
| 738 | trace_msm_pm_enter_spc(cpu, latency, sleep_us, wake_up); |
| 739 | break; |
| 740 | case MSM_PM_SLEEP_MODE_POWER_COLLAPSE: |
| 741 | trace_msm_pm_enter_pc(cpu, latency, sleep_us, wake_up); |
| 742 | break; |
| 743 | case MSM_PM_SLEEP_MODE_RETENTION: |
| 744 | trace_msm_pm_enter_ret(cpu, latency, sleep_us, wake_up); |
| 745 | break; |
| 746 | default: |
| 747 | break; |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | static inline void msm_pm_ftrace_lpm_exit(unsigned int cpu, |
| 752 | enum msm_pm_sleep_mode mode, |
| 753 | bool success) |
| 754 | { |
| 755 | switch (mode) { |
| 756 | case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT: |
| 757 | trace_msm_pm_exit_wfi(cpu, success); |
| 758 | break; |
| 759 | case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE: |
| 760 | trace_msm_pm_exit_spc(cpu, success); |
| 761 | break; |
| 762 | case MSM_PM_SLEEP_MODE_POWER_COLLAPSE: |
| 763 | trace_msm_pm_exit_pc(cpu, success); |
| 764 | break; |
| 765 | case MSM_PM_SLEEP_MODE_RETENTION: |
| 766 | trace_msm_pm_exit_ret(cpu, success); |
| 767 | break; |
| 768 | default: |
| 769 | break; |
| 770 | } |
| 771 | } |
| 772 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 773 | int msm_pm_idle_prepare(struct cpuidle_device *dev, |
| 774 | struct cpuidle_driver *drv, int index) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 775 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 776 | int i; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 777 | unsigned int power_usage = -1; |
| 778 | int ret = 0; |
Girish Mahadevan | dc318fd | 2012-08-17 16:48:05 -0600 | [diff] [blame] | 779 | uint32_t modified_time_us = 0; |
| 780 | struct msm_pm_time_params time_param; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 781 | |
Girish Mahadevan | dc318fd | 2012-08-17 16:48:05 -0600 | [diff] [blame] | 782 | time_param.latency_us = |
| 783 | (uint32_t) pm_qos_request(PM_QOS_CPU_DMA_LATENCY); |
| 784 | time_param.sleep_us = |
| 785 | (uint32_t) (ktime_to_us(tick_nohz_get_sleep_length()) |
| 786 | & UINT_MAX); |
| 787 | time_param.modified_time_us = 0; |
| 788 | |
| 789 | if (!dev->cpu) |
| 790 | time_param.next_event_us = |
| 791 | (uint32_t) (ktime_to_us(get_next_event_time()) |
| 792 | & UINT_MAX); |
| 793 | else |
| 794 | time_param.next_event_us = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 795 | |
| 796 | for (i = 0; i < dev->state_count; i++) { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 797 | struct cpuidle_state *state = &drv->states[i]; |
| 798 | struct cpuidle_state_usage *st_usage = &dev->states_usage[i]; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 799 | enum msm_pm_sleep_mode mode; |
| 800 | bool allow; |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 801 | void *rs_limits = NULL; |
| 802 | uint32_t power; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 803 | int idx; |
| 804 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 805 | mode = (enum msm_pm_sleep_mode) cpuidle_get_statedata(st_usage); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 806 | idx = MSM_PM_MODE(dev->cpu, mode); |
| 807 | |
Praveen Chidambaram | 42da9d2 | 2012-03-30 12:16:34 -0600 | [diff] [blame] | 808 | allow = msm_pm_sleep_modes[idx].idle_enabled && |
| 809 | msm_pm_sleep_modes[idx].idle_supported; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 810 | |
| 811 | switch (mode) { |
| 812 | case MSM_PM_SLEEP_MODE_POWER_COLLAPSE: |
Priyanka Mathur | 68a9bc5 | 2012-10-03 15:15:01 -0700 | [diff] [blame] | 813 | if (num_online_cpus() > 1) { |
| 814 | allow = false; |
| 815 | break; |
| 816 | } |
| 817 | /* fall through */ |
Mahesh Sivasubramanian | ba7e01c | 2012-09-04 18:22:51 -0600 | [diff] [blame] | 818 | case MSM_PM_SLEEP_MODE_RETENTION: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 819 | if (!allow) |
| 820 | break; |
| 821 | |
Priyanka Mathur | 68a9bc5 | 2012-10-03 15:15:01 -0700 | [diff] [blame] | 822 | if (msm_pm_retention_tz_call && |
| 823 | num_online_cpus() > 1) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 824 | allow = false; |
| 825 | break; |
| 826 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 827 | /* fall through */ |
| 828 | |
| 829 | case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE: |
| 830 | if (!allow) |
| 831 | break; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 832 | /* fall through */ |
| 833 | |
| 834 | case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT: |
| 835 | if (!allow) |
| 836 | break; |
Girish Mahadevan | dc318fd | 2012-08-17 16:48:05 -0600 | [diff] [blame] | 837 | /* fall through */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 838 | |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 839 | if (pm_sleep_ops.lowest_limits) |
| 840 | rs_limits = pm_sleep_ops.lowest_limits(true, |
Girish Mahadevan | dc318fd | 2012-08-17 16:48:05 -0600 | [diff] [blame] | 841 | mode, &time_param, &power); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 842 | |
| 843 | if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask) |
| 844 | pr_info("CPU%u: %s: %s, latency %uus, " |
| 845 | "sleep %uus, limit %p\n", |
| 846 | dev->cpu, __func__, state->desc, |
Girish Mahadevan | dc318fd | 2012-08-17 16:48:05 -0600 | [diff] [blame] | 847 | time_param.latency_us, |
| 848 | time_param.sleep_us, rs_limits); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 849 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 850 | if (!rs_limits) |
| 851 | allow = false; |
| 852 | break; |
| 853 | |
| 854 | default: |
| 855 | allow = false; |
| 856 | break; |
| 857 | } |
| 858 | |
| 859 | if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask) |
| 860 | pr_info("CPU%u: %s: allow %s: %d\n", |
| 861 | dev->cpu, __func__, state->desc, (int)allow); |
| 862 | |
| 863 | if (allow) { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 864 | if (power < power_usage) { |
| 865 | power_usage = power; |
Girish Mahadevan | dc318fd | 2012-08-17 16:48:05 -0600 | [diff] [blame] | 866 | modified_time_us = time_param.modified_time_us; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 867 | ret = mode; |
| 868 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 869 | |
| 870 | if (MSM_PM_SLEEP_MODE_POWER_COLLAPSE == mode) |
| 871 | msm_pm_idle_rs_limits = rs_limits; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 872 | } |
| 873 | } |
| 874 | |
Girish Mahadevan | dc318fd | 2012-08-17 16:48:05 -0600 | [diff] [blame] | 875 | if (modified_time_us && !dev->cpu) |
| 876 | msm_pm_set_timer(modified_time_us); |
Priyanka Mathur | 26b4a4b | 2012-11-05 13:45:45 -0800 | [diff] [blame^] | 877 | |
| 878 | msm_pm_ftrace_lpm_enter(dev->cpu, time_param.latency_us, |
| 879 | time_param.sleep_us, time_param.next_event_us, |
| 880 | ret); |
| 881 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 882 | return ret; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | int msm_pm_idle_enter(enum msm_pm_sleep_mode sleep_mode) |
| 886 | { |
| 887 | int64_t time; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 888 | int exit_stat; |
Priyanka Mathur | 26b4a4b | 2012-11-05 13:45:45 -0800 | [diff] [blame^] | 889 | bool collapsed = 1; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 890 | |
| 891 | if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask) |
| 892 | pr_info("CPU%u: %s: mode %d\n", |
| 893 | smp_processor_id(), __func__, sleep_mode); |
| 894 | |
| 895 | time = ktime_to_ns(ktime_get()); |
| 896 | |
| 897 | switch (sleep_mode) { |
| 898 | case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT: |
| 899 | msm_pm_swfi(); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 900 | exit_stat = MSM_PM_STAT_IDLE_WFI; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 901 | break; |
| 902 | |
Praveen Chidambaram | d3d844d | 2012-04-24 09:47:38 -0600 | [diff] [blame] | 903 | case MSM_PM_SLEEP_MODE_RETENTION: |
| 904 | msm_pm_retention(); |
Praveen Chidambaram | d3d844d | 2012-04-24 09:47:38 -0600 | [diff] [blame] | 905 | exit_stat = MSM_PM_STAT_RETENTION; |
Praveen Chidambaram | d3d844d | 2012-04-24 09:47:38 -0600 | [diff] [blame] | 906 | break; |
| 907 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 908 | case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE: |
Priyanka Mathur | 26b4a4b | 2012-11-05 13:45:45 -0800 | [diff] [blame^] | 909 | collapsed = msm_pm_power_collapse_standalone(true); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 910 | exit_stat = MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 911 | break; |
| 912 | |
| 913 | case MSM_PM_SLEEP_MODE_POWER_COLLAPSE: { |
Praveen Chidambaram | 192979f | 2012-04-25 18:30:23 -0600 | [diff] [blame] | 914 | int64_t timer_expiration = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 915 | bool timer_halted = false; |
| 916 | uint32_t sleep_delay; |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 917 | int ret = -ENODEV; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 918 | int notify_rpm = |
| 919 | (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE); |
Praveen Chidambaram | 192979f | 2012-04-25 18:30:23 -0600 | [diff] [blame] | 920 | timer_expiration = msm_pm_timer_enter_idle(); |
| 921 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 922 | sleep_delay = (uint32_t) msm_pm_convert_and_cap_time( |
| 923 | timer_expiration, MSM_PM_SLEEP_TICK_LIMIT); |
| 924 | if (sleep_delay == 0) /* 0 would mean infinite time */ |
| 925 | sleep_delay = 1; |
| 926 | |
Karthik Parsha | 6fb932d | 2012-01-24 18:04:12 -0800 | [diff] [blame] | 927 | if (MSM_PM_DEBUG_IDLE_CLK & msm_pm_debug_mask) |
| 928 | clock_debug_print_enabled(); |
| 929 | |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 930 | if (pm_sleep_ops.enter_sleep) |
| 931 | ret = pm_sleep_ops.enter_sleep(sleep_delay, |
| 932 | msm_pm_idle_rs_limits, |
| 933 | true, notify_rpm); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 934 | if (!ret) { |
Maheshkumar Sivasubramanian | dd93ecf | 2011-09-15 19:39:14 -0600 | [diff] [blame] | 935 | collapsed = msm_pm_power_collapse(true); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 936 | timer_halted = true; |
| 937 | |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 938 | if (pm_sleep_ops.exit_sleep) |
| 939 | pm_sleep_ops.exit_sleep(msm_pm_idle_rs_limits, |
| 940 | true, notify_rpm, collapsed); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 941 | } |
Praveen Chidambaram | 192979f | 2012-04-25 18:30:23 -0600 | [diff] [blame] | 942 | msm_pm_timer_exit_idle(timer_halted); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 943 | exit_stat = MSM_PM_STAT_IDLE_POWER_COLLAPSE; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 944 | break; |
| 945 | } |
| 946 | |
| 947 | default: |
| 948 | __WARN(); |
| 949 | goto cpuidle_enter_bail; |
| 950 | } |
| 951 | |
| 952 | time = ktime_to_ns(ktime_get()) - time; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 953 | msm_pm_add_stat(exit_stat, time); |
Priyanka Mathur | 26b4a4b | 2012-11-05 13:45:45 -0800 | [diff] [blame^] | 954 | msm_pm_ftrace_lpm_exit(smp_processor_id(), sleep_mode, |
| 955 | collapsed); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 956 | |
| 957 | do_div(time, 1000); |
| 958 | return (int) time; |
| 959 | |
| 960 | cpuidle_enter_bail: |
| 961 | return 0; |
| 962 | } |
| 963 | |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 964 | void msm_pm_cpu_enter_lowpower(unsigned int cpu) |
| 965 | { |
| 966 | int i; |
| 967 | bool allow[MSM_PM_SLEEP_MODE_NR]; |
| 968 | |
| 969 | for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) { |
| 970 | struct msm_pm_platform_data *mode; |
| 971 | |
Praveen Chidambaram | 42da9d2 | 2012-03-30 12:16:34 -0600 | [diff] [blame] | 972 | mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)]; |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 973 | allow[i] = mode->suspend_supported && mode->suspend_enabled; |
| 974 | } |
| 975 | |
| 976 | if (MSM_PM_DEBUG_HOTPLUG & msm_pm_debug_mask) |
| 977 | pr_notice("CPU%u: %s: shutting down cpu\n", cpu, __func__); |
| 978 | |
Matt Wagantall | 5375aa7 | 2012-07-02 19:59:51 -0700 | [diff] [blame] | 979 | if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 980 | msm_pm_power_collapse(false); |
Matt Wagantall | 5375aa7 | 2012-07-02 19:59:51 -0700 | [diff] [blame] | 981 | else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 982 | msm_pm_power_collapse_standalone(false); |
Matt Wagantall | 5375aa7 | 2012-07-02 19:59:51 -0700 | [diff] [blame] | 983 | else if (allow[MSM_PM_SLEEP_MODE_RETENTION]) |
Praveen Chidambaram | d3d844d | 2012-04-24 09:47:38 -0600 | [diff] [blame] | 984 | msm_pm_retention(); |
Stephen Boyd | bda7427 | 2012-08-09 14:01:27 -0700 | [diff] [blame] | 985 | else |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 986 | msm_pm_swfi(); |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 987 | } |
| 988 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 989 | static int msm_pm_enter(suspend_state_t state) |
| 990 | { |
| 991 | bool allow[MSM_PM_SLEEP_MODE_NR]; |
| 992 | int i; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 993 | int64_t period = 0; |
Praveen Chidambaram | 3895bde | 2012-05-14 19:42:40 +0530 | [diff] [blame] | 994 | int64_t time = msm_pm_timer_enter_suspend(&period); |
Girish Mahadevan | dc318fd | 2012-08-17 16:48:05 -0600 | [diff] [blame] | 995 | struct msm_pm_time_params time_param; |
| 996 | |
| 997 | time_param.latency_us = -1; |
| 998 | time_param.sleep_us = -1; |
| 999 | time_param.next_event_us = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1000 | |
| 1001 | if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask) |
| 1002 | pr_info("%s\n", __func__); |
| 1003 | |
| 1004 | if (smp_processor_id()) { |
| 1005 | __WARN(); |
| 1006 | goto enter_exit; |
| 1007 | } |
| 1008 | |
| 1009 | |
| 1010 | for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) { |
| 1011 | struct msm_pm_platform_data *mode; |
| 1012 | |
Praveen Chidambaram | 42da9d2 | 2012-03-30 12:16:34 -0600 | [diff] [blame] | 1013 | mode = &msm_pm_sleep_modes[MSM_PM_MODE(0, i)]; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1014 | allow[i] = mode->suspend_supported && mode->suspend_enabled; |
| 1015 | } |
| 1016 | |
| 1017 | if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) { |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 1018 | void *rs_limits = NULL; |
| 1019 | int ret = -ENODEV; |
| 1020 | uint32_t power; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1021 | |
| 1022 | if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask) |
| 1023 | pr_info("%s: power collapse\n", __func__); |
| 1024 | |
| 1025 | clock_debug_print_enabled(); |
| 1026 | |
| 1027 | #ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE |
| 1028 | if (msm_pm_sleep_time_override > 0) { |
| 1029 | int64_t ns = NSEC_PER_SEC * |
| 1030 | (int64_t) msm_pm_sleep_time_override; |
| 1031 | msm_pm_set_max_sleep_time(ns); |
| 1032 | msm_pm_sleep_time_override = 0; |
| 1033 | } |
| 1034 | #endif /* CONFIG_MSM_SLEEP_TIME_OVERRIDE */ |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 1035 | if (pm_sleep_ops.lowest_limits) |
| 1036 | rs_limits = pm_sleep_ops.lowest_limits(false, |
Girish Mahadevan | dc318fd | 2012-08-17 16:48:05 -0600 | [diff] [blame] | 1037 | MSM_PM_SLEEP_MODE_POWER_COLLAPSE, &time_param, &power); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1038 | |
| 1039 | if (rs_limits) { |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 1040 | if (pm_sleep_ops.enter_sleep) |
| 1041 | ret = pm_sleep_ops.enter_sleep( |
| 1042 | msm_pm_max_sleep_time, |
| 1043 | rs_limits, false, true); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1044 | if (!ret) { |
Maheshkumar Sivasubramanian | dd93ecf | 2011-09-15 19:39:14 -0600 | [diff] [blame] | 1045 | int collapsed = msm_pm_power_collapse(false); |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 1046 | if (pm_sleep_ops.exit_sleep) { |
| 1047 | pm_sleep_ops.exit_sleep(rs_limits, |
| 1048 | false, true, collapsed); |
| 1049 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1050 | } |
| 1051 | } else { |
| 1052 | pr_err("%s: cannot find the lowest power limit\n", |
| 1053 | __func__); |
| 1054 | } |
Praveen Chidambaram | 3895bde | 2012-05-14 19:42:40 +0530 | [diff] [blame] | 1055 | time = msm_pm_timer_exit_suspend(time, period); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1056 | msm_pm_add_stat(MSM_PM_STAT_SUSPEND, time); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1057 | } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) { |
| 1058 | if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask) |
| 1059 | pr_info("%s: standalone power collapse\n", __func__); |
| 1060 | msm_pm_power_collapse_standalone(false); |
Praveen Chidambaram | d3d844d | 2012-04-24 09:47:38 -0600 | [diff] [blame] | 1061 | } else if (allow[MSM_PM_SLEEP_MODE_RETENTION]) { |
| 1062 | if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask) |
| 1063 | pr_info("%s: retention\n", __func__); |
| 1064 | msm_pm_retention(); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1065 | } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) { |
| 1066 | if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask) |
| 1067 | pr_info("%s: swfi\n", __func__); |
| 1068 | msm_pm_swfi(); |
| 1069 | } |
| 1070 | |
| 1071 | |
| 1072 | enter_exit: |
| 1073 | if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask) |
| 1074 | pr_info("%s: return\n", __func__); |
| 1075 | |
| 1076 | return 0; |
| 1077 | } |
| 1078 | |
| 1079 | static struct platform_suspend_ops msm_pm_ops = { |
| 1080 | .enter = msm_pm_enter, |
| 1081 | .valid = suspend_valid_only_mem, |
| 1082 | }; |
| 1083 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1084 | /****************************************************************************** |
| 1085 | * Initialization routine |
| 1086 | *****************************************************************************/ |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 1087 | void msm_pm_set_sleep_ops(struct msm_pm_sleep_ops *ops) |
| 1088 | { |
| 1089 | if (ops) |
| 1090 | pm_sleep_ops = *ops; |
| 1091 | } |
| 1092 | |
Girish Mahadevan | d27ca4a | 2012-08-15 09:21:23 -0600 | [diff] [blame] | 1093 | void __init msm_pm_set_tz_retention_flag(unsigned int flag) |
| 1094 | { |
| 1095 | msm_pm_retention_tz_call = flag; |
| 1096 | } |
| 1097 | |
Mahesh Sivasubramanian | cb39662 | 2012-03-14 14:50:37 -0600 | [diff] [blame] | 1098 | static int __devinit msm_pc_debug_probe(struct platform_device *pdev) |
| 1099 | { |
| 1100 | struct resource *res = NULL; |
| 1101 | int i ; |
| 1102 | |
| 1103 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1104 | if (!res) |
| 1105 | goto fail; |
| 1106 | |
| 1107 | msm_pc_debug_counters_phys = res->start; |
| 1108 | WARN_ON(resource_size(res) < SZ_64); |
Mahesh Sivasubramanian | e6d3399 | 2012-10-17 13:39:40 -0600 | [diff] [blame] | 1109 | msm_pc_debug_counters = devm_ioremap_nocache(&pdev->dev, res->start, |
Mahesh Sivasubramanian | cb39662 | 2012-03-14 14:50:37 -0600 | [diff] [blame] | 1110 | resource_size(res)); |
| 1111 | |
| 1112 | if (!msm_pc_debug_counters) |
| 1113 | goto fail; |
| 1114 | |
| 1115 | for (i = 0; i < resource_size(res)/4; i++) |
| 1116 | __raw_writel(0, msm_pc_debug_counters + i * 4); |
| 1117 | return 0; |
| 1118 | fail: |
| 1119 | msm_pc_debug_counters = 0; |
| 1120 | msm_pc_debug_counters_phys = 0; |
| 1121 | return -EFAULT; |
| 1122 | } |
| 1123 | |
| 1124 | static struct of_device_id msm_pc_debug_table[] = { |
| 1125 | {.compatible = "qcom,pc-cntr"}, |
| 1126 | {}, |
| 1127 | }; |
| 1128 | |
| 1129 | static struct platform_driver msm_pc_counter_driver = { |
| 1130 | .probe = msm_pc_debug_probe, |
| 1131 | .driver = { |
| 1132 | .name = "pc-cntr", |
| 1133 | .owner = THIS_MODULE, |
| 1134 | .of_match_table = msm_pc_debug_table, |
| 1135 | }, |
| 1136 | }; |
| 1137 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1138 | static int __init msm_pm_init(void) |
| 1139 | { |
| 1140 | pgd_t *pc_pgd; |
| 1141 | pmd_t *pmd; |
| 1142 | unsigned long pmdval; |
Praveen Chidambaram | 3895bde | 2012-05-14 19:42:40 +0530 | [diff] [blame] | 1143 | enum msm_pm_time_stats_id enable_stats[] = { |
| 1144 | MSM_PM_STAT_IDLE_WFI, |
| 1145 | MSM_PM_STAT_RETENTION, |
| 1146 | MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE, |
| 1147 | MSM_PM_STAT_IDLE_POWER_COLLAPSE, |
| 1148 | MSM_PM_STAT_SUSPEND, |
| 1149 | }; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1150 | unsigned long exit_phys; |
Praveen Chidambaram | 3895bde | 2012-05-14 19:42:40 +0530 | [diff] [blame] | 1151 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1152 | /* Page table for cores to come back up safely. */ |
Mahesh Sivasubramanian | cb39662 | 2012-03-14 14:50:37 -0600 | [diff] [blame] | 1153 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1154 | pc_pgd = pgd_alloc(&init_mm); |
| 1155 | if (!pc_pgd) |
| 1156 | return -ENOMEM; |
| 1157 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1158 | exit_phys = virt_to_phys(msm_pm_collapse_exit); |
| 1159 | |
| 1160 | pmd = pmd_offset(pud_offset(pc_pgd + pgd_index(exit_phys),exit_phys), |
| 1161 | exit_phys); |
| 1162 | pmdval = (exit_phys & PGDIR_MASK) | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1163 | PMD_TYPE_SECT | PMD_SECT_AP_WRITE; |
| 1164 | pmd[0] = __pmd(pmdval); |
| 1165 | pmd[1] = __pmd(pmdval + (1 << (PGDIR_SHIFT - 1))); |
| 1166 | |
Steve Muckle | fcece05 | 2012-02-18 20:09:58 -0800 | [diff] [blame] | 1167 | msm_saved_state_phys = |
| 1168 | allocate_contiguous_ebi_nomap(CPU_SAVED_STATE_SIZE * |
| 1169 | num_possible_cpus(), 4); |
| 1170 | if (!msm_saved_state_phys) |
| 1171 | return -ENOMEM; |
| 1172 | msm_saved_state = ioremap_nocache(msm_saved_state_phys, |
| 1173 | CPU_SAVED_STATE_SIZE * |
| 1174 | num_possible_cpus()); |
| 1175 | if (!msm_saved_state) |
| 1176 | return -ENOMEM; |
| 1177 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1178 | /* It is remotely possible that the code in msm_pm_collapse_exit() |
| 1179 | * which turns on the MMU with this mapping is in the |
| 1180 | * next even-numbered megabyte beyond the |
| 1181 | * start of msm_pm_collapse_exit(). |
| 1182 | * Map this megabyte in as well. |
| 1183 | */ |
| 1184 | pmd[2] = __pmd(pmdval + (2 << (PGDIR_SHIFT - 1))); |
| 1185 | flush_pmd_entry(pmd); |
| 1186 | msm_pm_pc_pgd = virt_to_phys(pc_pgd); |
Steve Muckle | 730ad7a | 2012-02-21 15:26:37 -0800 | [diff] [blame] | 1187 | clean_caches((unsigned long)&msm_pm_pc_pgd, sizeof(msm_pm_pc_pgd), |
| 1188 | virt_to_phys(&msm_pm_pc_pgd)); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1189 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1190 | msm_pm_mode_sysfs_add(); |
Praveen Chidambaram | 3895bde | 2012-05-14 19:42:40 +0530 | [diff] [blame] | 1191 | msm_pm_add_stats(enable_stats, ARRAY_SIZE(enable_stats)); |
| 1192 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1193 | suspend_set_ops(&msm_pm_ops); |
Praveen Chidambaram | c594a09 | 2012-09-18 19:48:29 -0600 | [diff] [blame] | 1194 | msm_pm_target_init(); |
Girish Mahadevan | dc318fd | 2012-08-17 16:48:05 -0600 | [diff] [blame] | 1195 | hrtimer_init(&pm_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1196 | msm_cpuidle_init(); |
Mahesh Sivasubramanian | cb39662 | 2012-03-14 14:50:37 -0600 | [diff] [blame] | 1197 | platform_driver_register(&msm_pc_counter_driver); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1198 | |
| 1199 | return 0; |
| 1200 | } |
| 1201 | |
| 1202 | late_initcall(msm_pm_init); |
Girish Mahadevan | 5594499 | 2012-10-26 11:03:07 -0600 | [diff] [blame] | 1203 | |
| 1204 | static void __devinit msm_pm_set_flush_fn(uint32_t pc_mode) |
| 1205 | { |
| 1206 | msm_pm_disable_l2_fn = NULL; |
| 1207 | msm_pm_enable_l2_fn = NULL; |
| 1208 | msm_pm_flush_l2_fn = outer_flush_all; |
| 1209 | |
| 1210 | if (pc_mode == MSM_PM_PC_NOTZ_L2_EXT) { |
| 1211 | msm_pm_disable_l2_fn = outer_disable; |
| 1212 | msm_pm_enable_l2_fn = outer_resume; |
| 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | static int __devinit msm_pm_8x60_probe(struct platform_device *pdev) |
| 1217 | { |
| 1218 | char *key = NULL; |
| 1219 | uint32_t val = 0; |
| 1220 | int ret = 0; |
| 1221 | |
| 1222 | if (!pdev->dev.of_node) { |
| 1223 | struct msm_pm_init_data_type *d = pdev->dev.platform_data; |
| 1224 | |
| 1225 | if (!d) |
| 1226 | goto pm_8x60_probe_done; |
| 1227 | |
| 1228 | msm_pm_init_data.pc_mode = d->pc_mode; |
| 1229 | msm_pm_set_flush_fn(msm_pm_init_data.pc_mode); |
| 1230 | msm_pm_init_data.use_sync_timer = d->use_sync_timer; |
| 1231 | } else { |
| 1232 | key = "qcom,pc-mode"; |
| 1233 | ret = of_property_read_u32(pdev->dev.of_node, key, &val); |
| 1234 | |
| 1235 | if (ret) { |
| 1236 | pr_debug("%s: Cannot read %s,defaulting to 0", |
| 1237 | __func__, key); |
| 1238 | val = MSM_PM_PC_TZ_L2_INT; |
| 1239 | ret = 0; |
| 1240 | } |
| 1241 | |
| 1242 | msm_pm_init_data.pc_mode = val; |
| 1243 | msm_pm_set_flush_fn(msm_pm_init_data.pc_mode); |
| 1244 | |
| 1245 | key = "qcom,use-sync-timer"; |
| 1246 | msm_pm_init_data.use_sync_timer = |
| 1247 | of_property_read_bool(pdev->dev.of_node, key); |
| 1248 | } |
| 1249 | |
| 1250 | pm_8x60_probe_done: |
| 1251 | return ret; |
| 1252 | } |
| 1253 | |
| 1254 | static struct of_device_id msm_pm_8x60_table[] = { |
| 1255 | {.compatible = "qcom,pm-8x60"}, |
| 1256 | {}, |
| 1257 | }; |
| 1258 | |
| 1259 | static struct platform_driver msm_pm_8x60_driver = { |
| 1260 | .probe = msm_pm_8x60_probe, |
| 1261 | .driver = { |
| 1262 | .name = "pm-8x60", |
| 1263 | .owner = THIS_MODULE, |
| 1264 | .of_match_table = msm_pm_8x60_table, |
| 1265 | }, |
| 1266 | }; |
| 1267 | |
| 1268 | static int __init msm_pm_8x60_init(void) |
| 1269 | { |
| 1270 | return platform_driver_register(&msm_pm_8x60_driver); |
| 1271 | } |
| 1272 | module_init(msm_pm_8x60_init); |