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