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