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