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