Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* arch/arm/mach-msm/pm2.c |
| 2 | * |
| 3 | * MSM Power Management Routines |
| 4 | * |
| 5 | * Copyright (C) 2007 Google, Inc. |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 6 | * Copyright (c) 2008-2012 Code Aurora Forum. All rights reserved. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 7 | * |
| 8 | * This software is licensed under the terms of the GNU General Public |
| 9 | * License version 2, as published by the Free Software Foundation, and |
| 10 | * may be copied, distributed, and modified under those terms. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/clk.h> |
| 22 | #include <linux/delay.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/pm.h> |
| 25 | #include <linux/pm_qos_params.h> |
| 26 | #include <linux/proc_fs.h> |
| 27 | #include <linux/suspend.h> |
| 28 | #include <linux/reboot.h> |
| 29 | #include <linux/uaccess.h> |
| 30 | #include <linux/io.h> |
Murali Nalajala | 8fda449 | 2012-03-19 18:22:59 +0530 | [diff] [blame] | 31 | #include <linux/tick.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 32 | #include <linux/memory.h> |
| 33 | #ifdef CONFIG_HAS_WAKELOCK |
| 34 | #include <linux/wakelock.h> |
| 35 | #endif |
| 36 | #include <mach/msm_iomap.h> |
| 37 | #include <mach/system.h> |
| 38 | #ifdef CONFIG_CPU_V7 |
| 39 | #include <asm/pgtable.h> |
| 40 | #include <asm/pgalloc.h> |
| 41 | #endif |
| 42 | #ifdef CONFIG_CACHE_L2X0 |
| 43 | #include <asm/hardware/cache-l2x0.h> |
| 44 | #endif |
| 45 | #ifdef CONFIG_VFP |
| 46 | #include <asm/vfp.h> |
| 47 | #endif |
| 48 | |
| 49 | #ifdef CONFIG_MSM_MEMORY_LOW_POWER_MODE_SUSPEND_DEEP_POWER_DOWN |
| 50 | #include <mach/msm_migrate_pages.h> |
| 51 | #endif |
Murali Nalajala | 41786ab | 2012-03-06 10:47:32 +0530 | [diff] [blame] | 52 | #include <mach/socinfo.h> |
Anji jonnala | 1f2377c | 2012-03-27 14:35:55 +0530 | [diff] [blame] | 53 | #include <asm/smp_scu.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 54 | |
| 55 | #include "smd_private.h" |
| 56 | #include "smd_rpcrouter.h" |
| 57 | #include "acpuclock.h" |
| 58 | #include "clock.h" |
| 59 | #include "proc_comm.h" |
| 60 | #include "idle.h" |
| 61 | #include "irq.h" |
| 62 | #include "gpio.h" |
| 63 | #include "timer.h" |
Matt Wagantall | 7cca464 | 2012-02-01 16:43:24 -0800 | [diff] [blame] | 64 | #include "pm.h" |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 65 | #include "spm.h" |
| 66 | #include "sirc.h" |
Maheshkumar Sivasubramanian | 8ccc16e | 2011-10-25 15:59:57 -0600 | [diff] [blame] | 67 | #include "pm-boot.h" |
Anji jonnala | 1f2377c | 2012-03-27 14:35:55 +0530 | [diff] [blame] | 68 | #define MSM_CORE1_RESET 0xA8600590 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 69 | |
| 70 | /****************************************************************************** |
| 71 | * Debug Definitions |
| 72 | *****************************************************************************/ |
| 73 | |
| 74 | enum { |
Murali Nalajala | a7efba1 | 2012-02-23 18:13:52 +0530 | [diff] [blame] | 75 | MSM_PM_DEBUG_SUSPEND = BIT(0), |
| 76 | MSM_PM_DEBUG_POWER_COLLAPSE = BIT(1), |
| 77 | MSM_PM_DEBUG_STATE = BIT(2), |
| 78 | MSM_PM_DEBUG_CLOCK = BIT(3), |
| 79 | MSM_PM_DEBUG_RESET_VECTOR = BIT(4), |
| 80 | MSM_PM_DEBUG_SMSM_STATE = BIT(5), |
| 81 | MSM_PM_DEBUG_IDLE = BIT(6), |
| 82 | MSM_PM_DEBUG_HOTPLUG = BIT(7), |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | static int msm_pm_debug_mask; |
Taniya Das | e30a6b2 | 2012-03-20 11:37:45 +0530 | [diff] [blame] | 86 | int power_collapsed; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 87 | module_param_named( |
| 88 | debug_mask, msm_pm_debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP |
| 89 | ); |
| 90 | |
| 91 | #define MSM_PM_DPRINTK(mask, level, message, ...) \ |
| 92 | do { \ |
| 93 | if ((mask) & msm_pm_debug_mask) \ |
| 94 | printk(level message, ## __VA_ARGS__); \ |
| 95 | } while (0) |
| 96 | |
| 97 | #define MSM_PM_DEBUG_PRINT_STATE(tag) \ |
| 98 | do { \ |
| 99 | MSM_PM_DPRINTK(MSM_PM_DEBUG_STATE, \ |
| 100 | KERN_INFO, "%s: " \ |
| 101 | "APPS_CLK_SLEEP_EN %x, APPS_PWRDOWN %x, " \ |
| 102 | "SMSM_POWER_MASTER_DEM %x, SMSM_MODEM_STATE %x, " \ |
| 103 | "SMSM_APPS_DEM %x\n", \ |
| 104 | tag, \ |
| 105 | __raw_readl(APPS_CLK_SLEEP_EN), \ |
| 106 | __raw_readl(APPS_PWRDOWN), \ |
| 107 | smsm_get_state(SMSM_POWER_MASTER_DEM), \ |
| 108 | smsm_get_state(SMSM_MODEM_STATE), \ |
| 109 | smsm_get_state(SMSM_APPS_DEM)); \ |
| 110 | } while (0) |
| 111 | |
| 112 | #define MSM_PM_DEBUG_PRINT_SLEEP_INFO() \ |
| 113 | do { \ |
| 114 | if (msm_pm_debug_mask & MSM_PM_DEBUG_SMSM_STATE) \ |
| 115 | smsm_print_sleep_info(msm_pm_smem_data->sleep_time, \ |
| 116 | msm_pm_smem_data->resources_used, \ |
| 117 | msm_pm_smem_data->irq_mask, \ |
| 118 | msm_pm_smem_data->wakeup_reason, \ |
| 119 | msm_pm_smem_data->pending_irqs); \ |
| 120 | } while (0) |
| 121 | |
| 122 | |
| 123 | /****************************************************************************** |
| 124 | * Sleep Modes and Parameters |
| 125 | *****************************************************************************/ |
| 126 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 127 | static int msm_pm_idle_sleep_min_time = CONFIG_MSM7X00A_IDLE_SLEEP_MIN_TIME; |
| 128 | module_param_named( |
| 129 | idle_sleep_min_time, msm_pm_idle_sleep_min_time, |
| 130 | int, S_IRUGO | S_IWUSR | S_IWGRP |
| 131 | ); |
| 132 | |
| 133 | enum { |
| 134 | MSM_PM_MODE_ATTR_SUSPEND, |
| 135 | MSM_PM_MODE_ATTR_IDLE, |
| 136 | MSM_PM_MODE_ATTR_LATENCY, |
| 137 | MSM_PM_MODE_ATTR_RESIDENCY, |
| 138 | MSM_PM_MODE_ATTR_NR, |
| 139 | }; |
| 140 | |
| 141 | static char *msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_NR] = { |
| 142 | [MSM_PM_MODE_ATTR_SUSPEND] = "suspend_enabled", |
| 143 | [MSM_PM_MODE_ATTR_IDLE] = "idle_enabled", |
| 144 | [MSM_PM_MODE_ATTR_LATENCY] = "latency", |
| 145 | [MSM_PM_MODE_ATTR_RESIDENCY] = "residency", |
| 146 | }; |
| 147 | |
| 148 | static char *msm_pm_sleep_mode_labels[MSM_PM_SLEEP_MODE_NR] = { |
| 149 | [MSM_PM_SLEEP_MODE_POWER_COLLAPSE_SUSPEND] = " ", |
| 150 | [MSM_PM_SLEEP_MODE_POWER_COLLAPSE] = "power_collapse", |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 151 | [MSM_PM_SLEEP_MODE_RAMP_DOWN_AND_WAIT_FOR_INTERRUPT] = |
| 152 | "ramp_down_and_wfi", |
| 153 | [MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT] = "wfi", |
| 154 | [MSM_PM_SLEEP_MODE_POWER_COLLAPSE_NO_XO_SHUTDOWN] = |
| 155 | "power_collapse_no_xo_shutdown", |
| 156 | [MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE] = |
| 157 | "standalone_power_collapse", |
| 158 | }; |
| 159 | |
| 160 | static struct msm_pm_platform_data *msm_pm_modes; |
Murali Nalajala | 2a0bbda | 2012-03-28 12:12:54 +0530 | [diff] [blame] | 161 | static struct msm_pm_irq_calls *msm_pm_irq_extns; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 162 | |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 163 | struct msm_pm_kobj_attribute { |
| 164 | unsigned int cpu; |
| 165 | struct kobj_attribute ka; |
| 166 | }; |
| 167 | |
| 168 | #define GET_CPU_OF_ATTR(attr) \ |
| 169 | (container_of(attr, struct msm_pm_kobj_attribute, ka)->cpu) |
| 170 | |
| 171 | struct msm_pm_sysfs_sleep_mode { |
| 172 | struct kobject *kobj; |
| 173 | struct attribute_group attr_group; |
| 174 | struct attribute *attrs[MSM_PM_MODE_ATTR_NR + 1]; |
| 175 | struct msm_pm_kobj_attribute kas[MSM_PM_MODE_ATTR_NR]; |
| 176 | }; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 177 | |
| 178 | /* |
| 179 | * Write out the attribute. |
| 180 | */ |
| 181 | static ssize_t msm_pm_mode_attr_show( |
| 182 | struct kobject *kobj, struct kobj_attribute *attr, char *buf) |
| 183 | { |
| 184 | int ret = -EINVAL; |
| 185 | int i; |
| 186 | |
| 187 | for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) { |
| 188 | struct kernel_param kp; |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 189 | unsigned int cpu; |
| 190 | struct msm_pm_platform_data *mode; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 191 | |
| 192 | if (msm_pm_sleep_mode_labels[i] == NULL) |
| 193 | continue; |
| 194 | |
| 195 | if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i])) |
| 196 | continue; |
| 197 | |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 198 | cpu = GET_CPU_OF_ATTR(attr); |
| 199 | mode = &msm_pm_modes[MSM_PM_MODE(cpu, i)]; |
| 200 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 201 | if (!strcmp(attr->attr.name, |
| 202 | msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) { |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 203 | u32 arg = mode->suspend_enabled; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 204 | kp.arg = &arg; |
| 205 | ret = param_get_ulong(buf, &kp); |
| 206 | } else if (!strcmp(attr->attr.name, |
| 207 | msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) { |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 208 | u32 arg = mode->idle_enabled; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 209 | kp.arg = &arg; |
| 210 | ret = param_get_ulong(buf, &kp); |
| 211 | } else if (!strcmp(attr->attr.name, |
| 212 | msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_LATENCY])) { |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 213 | u32 arg = mode->latency; |
| 214 | kp.arg = &arg; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 215 | ret = param_get_ulong(buf, &kp); |
| 216 | } else if (!strcmp(attr->attr.name, |
| 217 | msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_RESIDENCY])) { |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 218 | u32 arg = mode->residency; |
| 219 | kp.arg = &arg; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 220 | ret = param_get_ulong(buf, &kp); |
| 221 | } |
| 222 | |
| 223 | break; |
| 224 | } |
| 225 | |
| 226 | if (ret > 0) { |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 227 | strlcat(buf, "\n", PAGE_SIZE); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 228 | ret++; |
| 229 | } |
| 230 | |
| 231 | return ret; |
| 232 | } |
| 233 | |
| 234 | /* |
| 235 | * Read in the new attribute value. |
| 236 | */ |
| 237 | static ssize_t msm_pm_mode_attr_store(struct kobject *kobj, |
| 238 | struct kobj_attribute *attr, const char *buf, size_t count) |
| 239 | { |
| 240 | int ret = -EINVAL; |
| 241 | int i; |
| 242 | |
| 243 | for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) { |
| 244 | struct kernel_param kp; |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 245 | unsigned int cpu; |
| 246 | struct msm_pm_platform_data *mode; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 247 | |
| 248 | if (msm_pm_sleep_mode_labels[i] == NULL) |
| 249 | continue; |
| 250 | |
| 251 | if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i])) |
| 252 | continue; |
| 253 | |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 254 | cpu = GET_CPU_OF_ATTR(attr); |
| 255 | mode = &msm_pm_modes[MSM_PM_MODE(cpu, i)]; |
| 256 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 257 | if (!strcmp(attr->attr.name, |
| 258 | msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) { |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 259 | kp.arg = &mode->suspend_enabled; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 260 | ret = param_set_byte(buf, &kp); |
| 261 | } else if (!strcmp(attr->attr.name, |
| 262 | msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) { |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 263 | kp.arg = &mode->idle_enabled; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 264 | ret = param_set_byte(buf, &kp); |
| 265 | } else if (!strcmp(attr->attr.name, |
| 266 | msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_LATENCY])) { |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 267 | kp.arg = &mode->latency; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 268 | ret = param_set_ulong(buf, &kp); |
| 269 | } else if (!strcmp(attr->attr.name, |
| 270 | msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_RESIDENCY])) { |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 271 | kp.arg = &mode->residency; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 272 | ret = param_set_ulong(buf, &kp); |
| 273 | } |
| 274 | |
| 275 | break; |
| 276 | } |
| 277 | |
| 278 | return ret ? ret : count; |
| 279 | } |
| 280 | |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 281 | /* Add sysfs entries for one cpu. */ |
| 282 | static int __init msm_pm_mode_sysfs_add_cpu( |
| 283 | unsigned int cpu, struct kobject *modes_kobj) |
| 284 | { |
| 285 | char cpu_name[8]; |
| 286 | struct kobject *cpu_kobj; |
| 287 | struct msm_pm_sysfs_sleep_mode *mode = NULL; |
| 288 | int i, j, k; |
| 289 | int ret; |
| 290 | |
| 291 | snprintf(cpu_name, sizeof(cpu_name), "cpu%u", cpu); |
| 292 | cpu_kobj = kobject_create_and_add(cpu_name, modes_kobj); |
| 293 | if (!cpu_kobj) { |
| 294 | pr_err("%s: cannot create %s kobject\n", __func__, cpu_name); |
| 295 | ret = -ENOMEM; |
| 296 | goto mode_sysfs_add_cpu_exit; |
| 297 | } |
| 298 | |
| 299 | for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) { |
| 300 | int idx = MSM_PM_MODE(cpu, i); |
| 301 | |
| 302 | if ((!msm_pm_modes[idx].suspend_supported) && |
| 303 | (!msm_pm_modes[idx].idle_supported)) |
| 304 | continue; |
| 305 | |
| 306 | mode = kzalloc(sizeof(*mode), GFP_KERNEL); |
| 307 | if (!mode) { |
| 308 | pr_err("%s: cannot allocate memory for attributes\n", |
| 309 | __func__); |
| 310 | ret = -ENOMEM; |
| 311 | goto mode_sysfs_add_cpu_exit; |
| 312 | } |
| 313 | |
| 314 | mode->kobj = kobject_create_and_add( |
| 315 | msm_pm_sleep_mode_labels[i], cpu_kobj); |
| 316 | if (!mode->kobj) { |
| 317 | pr_err("%s: cannot create kobject\n", __func__); |
| 318 | ret = -ENOMEM; |
| 319 | goto mode_sysfs_add_cpu_exit; |
| 320 | } |
| 321 | |
| 322 | for (k = 0, j = 0; k < MSM_PM_MODE_ATTR_NR; k++) { |
| 323 | if ((k == MSM_PM_MODE_ATTR_IDLE) && |
| 324 | !msm_pm_modes[idx].idle_supported) |
| 325 | continue; |
| 326 | if ((k == MSM_PM_MODE_ATTR_SUSPEND) && |
| 327 | !msm_pm_modes[idx].suspend_supported) |
| 328 | continue; |
| 329 | mode->kas[j].cpu = cpu; |
| 330 | mode->kas[j].ka.attr.mode = 0644; |
| 331 | mode->kas[j].ka.show = msm_pm_mode_attr_show; |
| 332 | mode->kas[j].ka.store = msm_pm_mode_attr_store; |
| 333 | mode->kas[j].ka.attr.name = msm_pm_mode_attr_labels[k]; |
| 334 | mode->attrs[j] = &mode->kas[j].ka.attr; |
| 335 | j++; |
| 336 | } |
| 337 | mode->attrs[j] = NULL; |
| 338 | |
| 339 | mode->attr_group.attrs = mode->attrs; |
| 340 | ret = sysfs_create_group(mode->kobj, &mode->attr_group); |
| 341 | if (ret) { |
| 342 | printk(KERN_ERR |
| 343 | "%s: cannot create kobject attribute group\n", |
| 344 | __func__); |
| 345 | goto mode_sysfs_add_cpu_exit; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | ret = 0; |
| 350 | |
| 351 | mode_sysfs_add_cpu_exit: |
| 352 | if (ret) { |
| 353 | if (mode && mode->kobj) |
| 354 | kobject_del(mode->kobj); |
| 355 | kfree(mode); |
| 356 | } |
| 357 | |
| 358 | return ret; |
| 359 | } |
| 360 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 361 | /* |
| 362 | * Add sysfs entries for the sleep modes. |
| 363 | */ |
| 364 | static int __init msm_pm_mode_sysfs_add(void) |
| 365 | { |
| 366 | struct kobject *module_kobj = NULL; |
| 367 | struct kobject *modes_kobj = NULL; |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 368 | unsigned int cpu; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 369 | int ret; |
| 370 | |
| 371 | module_kobj = kset_find_obj(module_kset, KBUILD_MODNAME); |
| 372 | if (!module_kobj) { |
| 373 | printk(KERN_ERR "%s: cannot find kobject for module %s\n", |
| 374 | __func__, KBUILD_MODNAME); |
| 375 | ret = -ENOENT; |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 376 | goto mode_sysfs_add_exit; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | modes_kobj = kobject_create_and_add("modes", module_kobj); |
| 380 | if (!modes_kobj) { |
| 381 | printk(KERN_ERR "%s: cannot create modes kobject\n", __func__); |
| 382 | ret = -ENOMEM; |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 383 | goto mode_sysfs_add_exit; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 386 | for_each_possible_cpu(cpu) { |
| 387 | ret = msm_pm_mode_sysfs_add_cpu(cpu, modes_kobj); |
| 388 | if (ret) |
| 389 | goto mode_sysfs_add_exit; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 392 | ret = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 393 | |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 394 | mode_sysfs_add_exit: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 395 | return ret; |
| 396 | } |
| 397 | |
| 398 | void __init msm_pm_set_platform_data( |
| 399 | struct msm_pm_platform_data *data, int count) |
| 400 | { |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 401 | BUG_ON(MSM_PM_SLEEP_MODE_NR * num_possible_cpus() > count); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 402 | msm_pm_modes = data; |
| 403 | } |
| 404 | |
Murali Nalajala | 2a0bbda | 2012-03-28 12:12:54 +0530 | [diff] [blame] | 405 | void __init msm_pm_set_irq_extns(struct msm_pm_irq_calls *irq_calls) |
| 406 | { |
| 407 | /* sanity check */ |
| 408 | BUG_ON(irq_calls == NULL || irq_calls->irq_pending == NULL || |
| 409 | irq_calls->idle_sleep_allowed == NULL || |
| 410 | irq_calls->enter_sleep1 == NULL || |
| 411 | irq_calls->enter_sleep2 == NULL || |
| 412 | irq_calls->exit_sleep1 == NULL || |
| 413 | irq_calls->exit_sleep2 == NULL || |
| 414 | irq_calls->exit_sleep3 == NULL); |
| 415 | |
| 416 | msm_pm_irq_extns = irq_calls; |
| 417 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 418 | |
| 419 | /****************************************************************************** |
| 420 | * Sleep Limitations |
| 421 | *****************************************************************************/ |
| 422 | enum { |
| 423 | SLEEP_LIMIT_NONE = 0, |
| 424 | SLEEP_LIMIT_NO_TCXO_SHUTDOWN = 2, |
| 425 | SLEEP_LIMIT_MASK = 0x03, |
| 426 | }; |
| 427 | |
| 428 | #ifdef CONFIG_MSM_MEMORY_LOW_POWER_MODE |
| 429 | enum { |
| 430 | SLEEP_RESOURCE_MEMORY_BIT0 = 0x0200, |
| 431 | SLEEP_RESOURCE_MEMORY_BIT1 = 0x0010, |
| 432 | }; |
| 433 | #endif |
| 434 | |
| 435 | |
| 436 | /****************************************************************************** |
| 437 | * Configure Hardware for Power Down/Up |
| 438 | *****************************************************************************/ |
| 439 | |
| 440 | #if defined(CONFIG_ARCH_MSM7X30) |
Taniya Das | 298de8c | 2012-02-16 11:45:31 +0530 | [diff] [blame] | 441 | #define APPS_CLK_SLEEP_EN (MSM_APCS_GCC_BASE + 0x020) |
| 442 | #define APPS_PWRDOWN (MSM_ACC0_BASE + 0x01c) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 443 | #define APPS_SECOP (MSM_TCSR_BASE + 0x038) |
Murali Nalajala | 1bc1d7c | 2012-02-09 11:18:42 +0530 | [diff] [blame] | 444 | #define APPS_STANDBY_CTL NULL |
| 445 | #else |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 446 | #define APPS_CLK_SLEEP_EN (MSM_CSR_BASE + 0x11c) |
| 447 | #define APPS_PWRDOWN (MSM_CSR_BASE + 0x440) |
| 448 | #define APPS_STANDBY_CTL (MSM_CSR_BASE + 0x108) |
Murali Nalajala | 1bc1d7c | 2012-02-09 11:18:42 +0530 | [diff] [blame] | 449 | #define APPS_SECOP NULL |
| 450 | #endif |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 451 | |
| 452 | /* |
| 453 | * Configure hardware registers in preparation for Apps power down. |
| 454 | */ |
| 455 | static void msm_pm_config_hw_before_power_down(void) |
| 456 | { |
Murali Nalajala | 1bc1d7c | 2012-02-09 11:18:42 +0530 | [diff] [blame] | 457 | if (cpu_is_msm7x30() || cpu_is_msm8x55()) { |
| 458 | __raw_writel(1, APPS_PWRDOWN); |
| 459 | mb(); |
| 460 | __raw_writel(4, APPS_SECOP); |
| 461 | mb(); |
| 462 | } else if (cpu_is_msm7x27()) { |
| 463 | __raw_writel(0x1f, APPS_CLK_SLEEP_EN); |
| 464 | mb(); |
| 465 | __raw_writel(1, APPS_PWRDOWN); |
| 466 | mb(); |
| 467 | } else if (cpu_is_msm7x27a() || cpu_is_msm7x27aa() || |
| 468 | cpu_is_msm7x25a() || cpu_is_msm7x25aa()) { |
| 469 | __raw_writel(0x7, APPS_CLK_SLEEP_EN); |
| 470 | mb(); |
| 471 | __raw_writel(1, APPS_PWRDOWN); |
| 472 | mb(); |
| 473 | } else { |
| 474 | __raw_writel(0x1f, APPS_CLK_SLEEP_EN); |
| 475 | mb(); |
| 476 | __raw_writel(1, APPS_PWRDOWN); |
| 477 | mb(); |
| 478 | __raw_writel(0, APPS_STANDBY_CTL); |
| 479 | mb(); |
| 480 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | /* |
Anji jonnala | 1f2377c | 2012-03-27 14:35:55 +0530 | [diff] [blame] | 484 | * Program the top csr from core0 context to put the |
| 485 | * core1 into GDFS, as core1 is not running yet. |
| 486 | */ |
| 487 | static void configure_top_csr(void) |
| 488 | { |
| 489 | void __iomem *base_ptr; |
| 490 | unsigned int value = 0; |
| 491 | |
| 492 | base_ptr = ioremap_nocache(MSM_CORE1_RESET, SZ_4); |
| 493 | if (!base_ptr) |
| 494 | return; |
| 495 | |
| 496 | /* bring the core1 out of reset */ |
| 497 | __raw_writel(0x3, base_ptr); |
| 498 | mb(); |
| 499 | /* |
| 500 | * override DBGNOPOWERDN and program the GDFS |
| 501 | * count val |
| 502 | */ |
| 503 | |
| 504 | __raw_writel(0x00030002, (MSM_CFG_CTL_BASE + 0x38)); |
| 505 | mb(); |
| 506 | |
| 507 | /* Initialize the SPM0 and SPM1 registers */ |
| 508 | msm_spm_reinit(); |
| 509 | |
| 510 | /* enable TCSR for core1 */ |
| 511 | value = __raw_readl((MSM_CFG_CTL_BASE + MPA5_CFG_CTL_REG)); |
| 512 | value |= BIT(22); |
| 513 | __raw_writel(value, MSM_CFG_CTL_BASE + MPA5_CFG_CTL_REG); |
| 514 | mb(); |
| 515 | |
| 516 | /* set reset bit for SPM1 */ |
| 517 | value = __raw_readl((MSM_CFG_CTL_BASE + MPA5_CFG_CTL_REG)); |
| 518 | value |= BIT(20); |
| 519 | __raw_writel(value, MSM_CFG_CTL_BASE + MPA5_CFG_CTL_REG); |
| 520 | mb(); |
| 521 | |
| 522 | /* set CLK_OFF bit */ |
| 523 | value = __raw_readl((MSM_CFG_CTL_BASE + MPA5_CFG_CTL_REG)); |
| 524 | value |= BIT(18); |
| 525 | __raw_writel(value, MSM_CFG_CTL_BASE + MPA5_CFG_CTL_REG); |
| 526 | mb(); |
| 527 | |
| 528 | /* set clamps bit */ |
| 529 | value = __raw_readl((MSM_CFG_CTL_BASE + MPA5_CFG_CTL_REG)); |
| 530 | value |= BIT(21); |
| 531 | __raw_writel(value, MSM_CFG_CTL_BASE + MPA5_CFG_CTL_REG); |
| 532 | mb(); |
| 533 | |
| 534 | /* set power_up bit */ |
| 535 | value = __raw_readl((MSM_CFG_CTL_BASE + MPA5_CFG_CTL_REG)); |
| 536 | value |= BIT(19); |
| 537 | __raw_writel(value, MSM_CFG_CTL_BASE + MPA5_CFG_CTL_REG); |
| 538 | mb(); |
| 539 | |
| 540 | /* Disable TSCR for core0 */ |
| 541 | value = __raw_readl((MSM_CFG_CTL_BASE + MPA5_CFG_CTL_REG)); |
| 542 | value &= ~BIT(22); |
| 543 | __raw_writel(value, MSM_CFG_CTL_BASE + MPA5_CFG_CTL_REG); |
| 544 | mb(); |
| 545 | __raw_writel(0x0, base_ptr); |
| 546 | mb(); |
| 547 | iounmap(base_ptr); |
| 548 | } |
| 549 | |
| 550 | /* |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 551 | * Clear hardware registers after Apps powers up. |
| 552 | */ |
| 553 | static void msm_pm_config_hw_after_power_up(void) |
| 554 | { |
Anji jonnala | 1f2377c | 2012-03-27 14:35:55 +0530 | [diff] [blame] | 555 | |
Murali Nalajala | 1bc1d7c | 2012-02-09 11:18:42 +0530 | [diff] [blame] | 556 | if (cpu_is_msm7x30() || cpu_is_msm8x55()) { |
| 557 | __raw_writel(0, APPS_SECOP); |
| 558 | mb(); |
| 559 | __raw_writel(0, APPS_PWRDOWN); |
| 560 | mb(); |
| 561 | msm_spm_reinit(); |
| 562 | } else { |
| 563 | __raw_writel(0, APPS_PWRDOWN); |
| 564 | mb(); |
| 565 | __raw_writel(0, APPS_CLK_SLEEP_EN); |
| 566 | mb(); |
Anji jonnala | 1f2377c | 2012-03-27 14:35:55 +0530 | [diff] [blame] | 567 | |
| 568 | if (cpu_is_msm8625()) { |
| 569 | /* |
| 570 | * enable the SCU while coming out of power |
| 571 | * collapse. |
| 572 | */ |
| 573 | scu_enable(MSM_SCU_BASE); |
| 574 | /* |
| 575 | * Program the top csr to put the core1 into GDFS. |
| 576 | */ |
| 577 | configure_top_csr(); |
| 578 | } |
Murali Nalajala | 1bc1d7c | 2012-02-09 11:18:42 +0530 | [diff] [blame] | 579 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | /* |
| 583 | * Configure hardware registers in preparation for SWFI. |
| 584 | */ |
| 585 | static void msm_pm_config_hw_before_swfi(void) |
| 586 | { |
Murali Nalajala | 1bc1d7c | 2012-02-09 11:18:42 +0530 | [diff] [blame] | 587 | if (cpu_is_qsd8x50()) { |
| 588 | __raw_writel(0x1f, APPS_CLK_SLEEP_EN); |
| 589 | mb(); |
| 590 | } else if (cpu_is_msm7x27()) { |
| 591 | __raw_writel(0x0f, APPS_CLK_SLEEP_EN); |
| 592 | mb(); |
| 593 | } else if (cpu_is_msm7x27a() || cpu_is_msm7x27aa() || |
| 594 | cpu_is_msm7x25a() || cpu_is_msm7x25aa()) { |
| 595 | __raw_writel(0x7, APPS_CLK_SLEEP_EN); |
| 596 | mb(); |
| 597 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | /* |
| 601 | * Respond to timing out waiting for Modem |
| 602 | * |
| 603 | * NOTE: The function never returns. |
| 604 | */ |
| 605 | static void msm_pm_timeout(void) |
| 606 | { |
| 607 | #if defined(CONFIG_MSM_PM_TIMEOUT_RESET_CHIP) |
| 608 | printk(KERN_EMERG "%s(): resetting chip\n", __func__); |
| 609 | msm_proc_comm(PCOM_RESET_CHIP_IMM, NULL, NULL); |
| 610 | #elif defined(CONFIG_MSM_PM_TIMEOUT_RESET_MODEM) |
| 611 | printk(KERN_EMERG "%s(): resetting modem\n", __func__); |
| 612 | msm_proc_comm_reset_modem_now(); |
| 613 | #elif defined(CONFIG_MSM_PM_TIMEOUT_HALT) |
| 614 | printk(KERN_EMERG "%s(): halting\n", __func__); |
| 615 | #endif |
| 616 | for (;;) |
| 617 | ; |
| 618 | } |
| 619 | |
| 620 | |
| 621 | /****************************************************************************** |
| 622 | * State Polling Definitions |
| 623 | *****************************************************************************/ |
| 624 | |
| 625 | struct msm_pm_polled_group { |
| 626 | uint32_t group_id; |
| 627 | |
| 628 | uint32_t bits_all_set; |
| 629 | uint32_t bits_all_clear; |
| 630 | uint32_t bits_any_set; |
| 631 | uint32_t bits_any_clear; |
| 632 | |
| 633 | uint32_t value_read; |
| 634 | }; |
| 635 | |
| 636 | /* |
| 637 | * Return true if all bits indicated by flag are set in source. |
| 638 | */ |
| 639 | static inline bool msm_pm_all_set(uint32_t source, uint32_t flag) |
| 640 | { |
| 641 | return (source & flag) == flag; |
| 642 | } |
| 643 | |
| 644 | /* |
| 645 | * Return true if any bit indicated by flag are set in source. |
| 646 | */ |
| 647 | static inline bool msm_pm_any_set(uint32_t source, uint32_t flag) |
| 648 | { |
| 649 | return !flag || (source & flag); |
| 650 | } |
| 651 | |
| 652 | /* |
| 653 | * Return true if all bits indicated by flag are cleared in source. |
| 654 | */ |
| 655 | static inline bool msm_pm_all_clear(uint32_t source, uint32_t flag) |
| 656 | { |
| 657 | return (~source & flag) == flag; |
| 658 | } |
| 659 | |
| 660 | /* |
| 661 | * Return true if any bit indicated by flag are cleared in source. |
| 662 | */ |
| 663 | static inline bool msm_pm_any_clear(uint32_t source, uint32_t flag) |
| 664 | { |
| 665 | return !flag || (~source & flag); |
| 666 | } |
| 667 | |
| 668 | /* |
| 669 | * Poll the shared memory states as indicated by the poll groups. |
| 670 | * |
| 671 | * nr_grps: number of groups in the array |
| 672 | * grps: array of groups |
| 673 | * |
| 674 | * The function returns when conditions specified by any of the poll |
| 675 | * groups become true. The conditions specified by a poll group are |
| 676 | * deemed true when 1) at least one bit from bits_any_set is set OR one |
| 677 | * bit from bits_any_clear is cleared; and 2) all bits in bits_all_set |
| 678 | * are set; and 3) all bits in bits_all_clear are cleared. |
| 679 | * |
| 680 | * Return value: |
| 681 | * >=0: index of the poll group whose conditions have become true |
| 682 | * -ETIMEDOUT: timed out |
| 683 | */ |
| 684 | static int msm_pm_poll_state(int nr_grps, struct msm_pm_polled_group *grps) |
| 685 | { |
| 686 | int i, k; |
| 687 | |
| 688 | for (i = 0; i < 50000; i++) { |
| 689 | for (k = 0; k < nr_grps; k++) { |
| 690 | bool all_set, all_clear; |
| 691 | bool any_set, any_clear; |
| 692 | |
| 693 | grps[k].value_read = smsm_get_state(grps[k].group_id); |
| 694 | |
| 695 | all_set = msm_pm_all_set(grps[k].value_read, |
| 696 | grps[k].bits_all_set); |
| 697 | all_clear = msm_pm_all_clear(grps[k].value_read, |
| 698 | grps[k].bits_all_clear); |
| 699 | any_set = msm_pm_any_set(grps[k].value_read, |
| 700 | grps[k].bits_any_set); |
| 701 | any_clear = msm_pm_any_clear(grps[k].value_read, |
| 702 | grps[k].bits_any_clear); |
| 703 | |
| 704 | if (all_set && all_clear && (any_set || any_clear)) |
| 705 | return k; |
| 706 | } |
| 707 | udelay(50); |
| 708 | } |
| 709 | |
| 710 | printk(KERN_ERR "%s failed:\n", __func__); |
| 711 | for (k = 0; k < nr_grps; k++) |
| 712 | printk(KERN_ERR "(%x, %x, %x, %x) %x\n", |
| 713 | grps[k].bits_all_set, grps[k].bits_all_clear, |
| 714 | grps[k].bits_any_set, grps[k].bits_any_clear, |
| 715 | grps[k].value_read); |
| 716 | |
| 717 | return -ETIMEDOUT; |
| 718 | } |
| 719 | |
| 720 | |
| 721 | /****************************************************************************** |
| 722 | * Suspend Max Sleep Time |
| 723 | *****************************************************************************/ |
| 724 | |
| 725 | #define SCLK_HZ (32768) |
| 726 | #define MSM_PM_SLEEP_TICK_LIMIT (0x6DDD000) |
| 727 | |
| 728 | #ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE |
| 729 | static int msm_pm_sleep_time_override; |
| 730 | module_param_named(sleep_time_override, |
| 731 | msm_pm_sleep_time_override, int, S_IRUGO | S_IWUSR | S_IWGRP); |
| 732 | #endif |
| 733 | |
| 734 | static uint32_t msm_pm_max_sleep_time; |
| 735 | |
| 736 | /* |
| 737 | * Convert time from nanoseconds to slow clock ticks, then cap it to the |
| 738 | * specified limit |
| 739 | */ |
| 740 | static int64_t msm_pm_convert_and_cap_time(int64_t time_ns, int64_t limit) |
| 741 | { |
| 742 | do_div(time_ns, NSEC_PER_SEC / SCLK_HZ); |
| 743 | return (time_ns > limit) ? limit : time_ns; |
| 744 | } |
| 745 | |
| 746 | /* |
| 747 | * Set the sleep time for suspend. 0 means infinite sleep time. |
| 748 | */ |
| 749 | void msm_pm_set_max_sleep_time(int64_t max_sleep_time_ns) |
| 750 | { |
| 751 | unsigned long flags; |
| 752 | |
| 753 | local_irq_save(flags); |
| 754 | if (max_sleep_time_ns == 0) { |
| 755 | msm_pm_max_sleep_time = 0; |
| 756 | } else { |
| 757 | msm_pm_max_sleep_time = (uint32_t)msm_pm_convert_and_cap_time( |
| 758 | max_sleep_time_ns, MSM_PM_SLEEP_TICK_LIMIT); |
| 759 | |
| 760 | if (msm_pm_max_sleep_time == 0) |
| 761 | msm_pm_max_sleep_time = 1; |
| 762 | } |
| 763 | |
| 764 | MSM_PM_DPRINTK(MSM_PM_DEBUG_SUSPEND, KERN_INFO, |
| 765 | "%s(): Requested %lld ns Giving %u sclk ticks\n", __func__, |
| 766 | max_sleep_time_ns, msm_pm_max_sleep_time); |
| 767 | local_irq_restore(flags); |
| 768 | } |
| 769 | EXPORT_SYMBOL(msm_pm_set_max_sleep_time); |
| 770 | |
| 771 | |
| 772 | /****************************************************************************** |
| 773 | * CONFIG_MSM_IDLE_STATS |
| 774 | *****************************************************************************/ |
| 775 | |
| 776 | #ifdef CONFIG_MSM_IDLE_STATS |
| 777 | enum msm_pm_time_stats_id { |
| 778 | MSM_PM_STAT_REQUESTED_IDLE, |
| 779 | MSM_PM_STAT_IDLE_SPIN, |
| 780 | MSM_PM_STAT_IDLE_WFI, |
| 781 | MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE, |
| 782 | MSM_PM_STAT_IDLE_FAILED_STANDALONE_POWER_COLLAPSE, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 783 | MSM_PM_STAT_IDLE_POWER_COLLAPSE, |
| 784 | MSM_PM_STAT_IDLE_FAILED_POWER_COLLAPSE, |
| 785 | MSM_PM_STAT_SUSPEND, |
| 786 | MSM_PM_STAT_FAILED_SUSPEND, |
| 787 | MSM_PM_STAT_NOT_IDLE, |
| 788 | MSM_PM_STAT_COUNT |
| 789 | }; |
| 790 | |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 791 | struct msm_pm_time_stats { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 792 | const char *name; |
| 793 | int64_t first_bucket_time; |
| 794 | int bucket[CONFIG_MSM_IDLE_STATS_BUCKET_COUNT]; |
| 795 | int64_t min_time[CONFIG_MSM_IDLE_STATS_BUCKET_COUNT]; |
| 796 | int64_t max_time[CONFIG_MSM_IDLE_STATS_BUCKET_COUNT]; |
| 797 | int count; |
| 798 | int64_t total_time; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 799 | }; |
| 800 | |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 801 | struct msm_pm_cpu_time_stats { |
| 802 | struct msm_pm_time_stats stats[MSM_PM_STAT_COUNT]; |
| 803 | }; |
| 804 | |
| 805 | static DEFINE_PER_CPU_SHARED_ALIGNED( |
| 806 | struct msm_pm_cpu_time_stats, msm_pm_stats); |
| 807 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 808 | static uint32_t msm_pm_sleep_limit = SLEEP_LIMIT_NONE; |
| 809 | |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 810 | static DEFINE_SPINLOCK(msm_pm_stats_lock); |
| 811 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 812 | /* |
| 813 | * Add the given time data to the statistics collection. |
| 814 | */ |
| 815 | static void msm_pm_add_stat(enum msm_pm_time_stats_id id, int64_t t) |
| 816 | { |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 817 | unsigned long flags; |
| 818 | struct msm_pm_time_stats *stats; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 819 | int i; |
| 820 | int64_t bt; |
| 821 | |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 822 | spin_lock_irqsave(&msm_pm_stats_lock, flags); |
| 823 | stats = __get_cpu_var(msm_pm_stats).stats; |
| 824 | |
| 825 | stats[id].total_time += t; |
| 826 | stats[id].count++; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 827 | |
| 828 | bt = t; |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 829 | do_div(bt, stats[id].first_bucket_time); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 830 | |
| 831 | if (bt < 1ULL << (CONFIG_MSM_IDLE_STATS_BUCKET_SHIFT * |
| 832 | (CONFIG_MSM_IDLE_STATS_BUCKET_COUNT - 1))) |
| 833 | i = DIV_ROUND_UP(fls((uint32_t)bt), |
| 834 | CONFIG_MSM_IDLE_STATS_BUCKET_SHIFT); |
| 835 | else |
| 836 | i = CONFIG_MSM_IDLE_STATS_BUCKET_COUNT - 1; |
| 837 | |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 838 | if (i >= CONFIG_MSM_IDLE_STATS_BUCKET_COUNT) |
| 839 | i = CONFIG_MSM_IDLE_STATS_BUCKET_COUNT - 1; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 840 | |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 841 | stats[id].bucket[i]++; |
| 842 | |
| 843 | if (t < stats[id].min_time[i] || !stats[id].max_time[i]) |
| 844 | stats[id].min_time[i] = t; |
| 845 | if (t > stats[id].max_time[i]) |
| 846 | stats[id].max_time[i] = t; |
| 847 | |
| 848 | spin_unlock_irqrestore(&msm_pm_stats_lock, flags); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | /* |
| 852 | * Helper function of snprintf where buf is auto-incremented, size is auto- |
| 853 | * decremented, and there is no return value. |
| 854 | * |
| 855 | * NOTE: buf and size must be l-values (e.g. variables) |
| 856 | */ |
| 857 | #define SNPRINTF(buf, size, format, ...) \ |
| 858 | do { \ |
| 859 | if (size > 0) { \ |
| 860 | int ret; \ |
| 861 | ret = snprintf(buf, size, format, ## __VA_ARGS__); \ |
| 862 | if (ret > size) { \ |
| 863 | buf += size; \ |
| 864 | size = 0; \ |
| 865 | } else { \ |
| 866 | buf += ret; \ |
| 867 | size -= ret; \ |
| 868 | } \ |
| 869 | } \ |
| 870 | } while (0) |
| 871 | |
| 872 | /* |
| 873 | * Write out the power management statistics. |
| 874 | */ |
| 875 | static int msm_pm_read_proc |
| 876 | (char *page, char **start, off_t off, int count, int *eof, void *data) |
| 877 | { |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 878 | unsigned int cpu = off / MSM_PM_STAT_COUNT; |
| 879 | int id = off % MSM_PM_STAT_COUNT; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 880 | char *p = page; |
| 881 | |
| 882 | if (count < 1024) { |
| 883 | *start = (char *) 0; |
| 884 | *eof = 0; |
| 885 | return 0; |
| 886 | } |
| 887 | |
| 888 | if (!off) { |
| 889 | SNPRINTF(p, count, "Last power collapse voted "); |
| 890 | if ((msm_pm_sleep_limit & SLEEP_LIMIT_MASK) == |
| 891 | SLEEP_LIMIT_NONE) |
| 892 | SNPRINTF(p, count, "for TCXO shutdown\n\n"); |
| 893 | else |
| 894 | SNPRINTF(p, count, "against TCXO shutdown\n\n"); |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 895 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 896 | |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 897 | if (cpu < num_possible_cpus()) { |
| 898 | unsigned long flags; |
| 899 | struct msm_pm_time_stats *stats; |
| 900 | int i; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 901 | int64_t bucket_time; |
| 902 | int64_t s; |
| 903 | uint32_t ns; |
| 904 | |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 905 | spin_lock_irqsave(&msm_pm_stats_lock, flags); |
| 906 | stats = per_cpu(msm_pm_stats, cpu).stats; |
| 907 | |
| 908 | s = stats[id].total_time; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 909 | ns = do_div(s, NSEC_PER_SEC); |
| 910 | SNPRINTF(p, count, |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 911 | "[cpu %u] %s:\n" |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 912 | " count: %7d\n" |
| 913 | " total_time: %lld.%09u\n", |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 914 | cpu, stats[id].name, |
| 915 | stats[id].count, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 916 | s, ns); |
| 917 | |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 918 | bucket_time = stats[id].first_bucket_time; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 919 | for (i = 0; i < CONFIG_MSM_IDLE_STATS_BUCKET_COUNT - 1; i++) { |
| 920 | s = bucket_time; |
| 921 | ns = do_div(s, NSEC_PER_SEC); |
| 922 | SNPRINTF(p, count, |
| 923 | " <%6lld.%09u: %7d (%lld-%lld)\n", |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 924 | s, ns, stats[id].bucket[i], |
| 925 | stats[id].min_time[i], |
| 926 | stats[id].max_time[i]); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 927 | |
| 928 | bucket_time <<= CONFIG_MSM_IDLE_STATS_BUCKET_SHIFT; |
| 929 | } |
| 930 | |
| 931 | SNPRINTF(p, count, " >=%6lld.%09u: %7d (%lld-%lld)\n", |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 932 | s, ns, stats[id].bucket[i], |
| 933 | stats[id].min_time[i], |
| 934 | stats[id].max_time[i]); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 935 | |
| 936 | *start = (char *) 1; |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 937 | *eof = (off + 1 >= MSM_PM_STAT_COUNT * num_possible_cpus()); |
| 938 | |
| 939 | spin_unlock_irqrestore(&msm_pm_stats_lock, flags); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | return p - page; |
| 943 | } |
| 944 | #undef SNPRINTF |
| 945 | |
| 946 | #define MSM_PM_STATS_RESET "reset" |
| 947 | |
| 948 | /* |
| 949 | * Reset the power management statistics values. |
| 950 | */ |
| 951 | static int msm_pm_write_proc(struct file *file, const char __user *buffer, |
| 952 | unsigned long count, void *data) |
| 953 | { |
| 954 | char buf[sizeof(MSM_PM_STATS_RESET)]; |
| 955 | int ret; |
| 956 | unsigned long flags; |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 957 | unsigned int cpu; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 958 | |
| 959 | if (count < strlen(MSM_PM_STATS_RESET)) { |
| 960 | ret = -EINVAL; |
| 961 | goto write_proc_failed; |
| 962 | } |
| 963 | |
| 964 | if (copy_from_user(buf, buffer, strlen(MSM_PM_STATS_RESET))) { |
| 965 | ret = -EFAULT; |
| 966 | goto write_proc_failed; |
| 967 | } |
| 968 | |
| 969 | if (memcmp(buf, MSM_PM_STATS_RESET, strlen(MSM_PM_STATS_RESET))) { |
| 970 | ret = -EINVAL; |
| 971 | goto write_proc_failed; |
| 972 | } |
| 973 | |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 974 | spin_lock_irqsave(&msm_pm_stats_lock, flags); |
| 975 | for_each_possible_cpu(cpu) { |
| 976 | struct msm_pm_time_stats *stats; |
| 977 | int i; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 978 | |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 979 | stats = per_cpu(msm_pm_stats, cpu).stats; |
| 980 | for (i = 0; i < MSM_PM_STAT_COUNT; i++) { |
| 981 | memset(stats[i].bucket, |
| 982 | 0, sizeof(stats[i].bucket)); |
| 983 | memset(stats[i].min_time, |
| 984 | 0, sizeof(stats[i].min_time)); |
| 985 | memset(stats[i].max_time, |
| 986 | 0, sizeof(stats[i].max_time)); |
| 987 | stats[i].count = 0; |
| 988 | stats[i].total_time = 0; |
| 989 | } |
| 990 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 991 | msm_pm_sleep_limit = SLEEP_LIMIT_NONE; |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 992 | spin_unlock_irqrestore(&msm_pm_stats_lock, flags); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 993 | |
| 994 | return count; |
| 995 | |
| 996 | write_proc_failed: |
| 997 | return ret; |
| 998 | } |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 999 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1000 | #undef MSM_PM_STATS_RESET |
| 1001 | #endif /* CONFIG_MSM_IDLE_STATS */ |
| 1002 | |
| 1003 | |
| 1004 | /****************************************************************************** |
| 1005 | * Shared Memory Bits |
| 1006 | *****************************************************************************/ |
| 1007 | |
| 1008 | #define DEM_MASTER_BITS_PER_CPU 6 |
| 1009 | |
| 1010 | /* Power Master State Bits - Per CPU */ |
| 1011 | #define DEM_MASTER_SMSM_RUN \ |
| 1012 | (0x01UL << (DEM_MASTER_BITS_PER_CPU * SMSM_APPS_STATE)) |
| 1013 | #define DEM_MASTER_SMSM_RSA \ |
| 1014 | (0x02UL << (DEM_MASTER_BITS_PER_CPU * SMSM_APPS_STATE)) |
| 1015 | #define DEM_MASTER_SMSM_PWRC_EARLY_EXIT \ |
| 1016 | (0x04UL << (DEM_MASTER_BITS_PER_CPU * SMSM_APPS_STATE)) |
| 1017 | #define DEM_MASTER_SMSM_SLEEP_EXIT \ |
| 1018 | (0x08UL << (DEM_MASTER_BITS_PER_CPU * SMSM_APPS_STATE)) |
| 1019 | #define DEM_MASTER_SMSM_READY \ |
| 1020 | (0x10UL << (DEM_MASTER_BITS_PER_CPU * SMSM_APPS_STATE)) |
| 1021 | #define DEM_MASTER_SMSM_SLEEP \ |
| 1022 | (0x20UL << (DEM_MASTER_BITS_PER_CPU * SMSM_APPS_STATE)) |
| 1023 | |
| 1024 | /* Power Slave State Bits */ |
| 1025 | #define DEM_SLAVE_SMSM_RUN (0x0001) |
| 1026 | #define DEM_SLAVE_SMSM_PWRC (0x0002) |
| 1027 | #define DEM_SLAVE_SMSM_PWRC_DELAY (0x0004) |
| 1028 | #define DEM_SLAVE_SMSM_PWRC_EARLY_EXIT (0x0008) |
| 1029 | #define DEM_SLAVE_SMSM_WFPI (0x0010) |
| 1030 | #define DEM_SLAVE_SMSM_SLEEP (0x0020) |
| 1031 | #define DEM_SLAVE_SMSM_SLEEP_EXIT (0x0040) |
| 1032 | #define DEM_SLAVE_SMSM_MSGS_REDUCED (0x0080) |
| 1033 | #define DEM_SLAVE_SMSM_RESET (0x0100) |
| 1034 | #define DEM_SLAVE_SMSM_PWRC_SUSPEND (0x0200) |
| 1035 | |
| 1036 | |
| 1037 | /****************************************************************************** |
| 1038 | * Shared Memory Data |
| 1039 | *****************************************************************************/ |
| 1040 | |
| 1041 | #define DEM_MAX_PORT_NAME_LEN (20) |
| 1042 | |
| 1043 | struct msm_pm_smem_t { |
| 1044 | uint32_t sleep_time; |
| 1045 | uint32_t irq_mask; |
| 1046 | uint32_t resources_used; |
| 1047 | uint32_t reserved1; |
| 1048 | |
| 1049 | uint32_t wakeup_reason; |
| 1050 | uint32_t pending_irqs; |
| 1051 | uint32_t rpc_prog; |
| 1052 | uint32_t rpc_proc; |
| 1053 | char smd_port_name[DEM_MAX_PORT_NAME_LEN]; |
| 1054 | uint32_t reserved2; |
| 1055 | }; |
| 1056 | |
| 1057 | |
| 1058 | /****************************************************************************** |
| 1059 | * |
| 1060 | *****************************************************************************/ |
| 1061 | static struct msm_pm_smem_t *msm_pm_smem_data; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1062 | static atomic_t msm_pm_init_done = ATOMIC_INIT(0); |
| 1063 | |
| 1064 | static int msm_pm_modem_busy(void) |
| 1065 | { |
| 1066 | if (!(smsm_get_state(SMSM_POWER_MASTER_DEM) & DEM_MASTER_SMSM_READY)) { |
| 1067 | MSM_PM_DPRINTK(MSM_PM_DEBUG_POWER_COLLAPSE, |
| 1068 | KERN_INFO, "%s(): master not ready\n", __func__); |
| 1069 | return -EBUSY; |
| 1070 | } |
| 1071 | |
| 1072 | return 0; |
| 1073 | } |
| 1074 | |
| 1075 | /* |
| 1076 | * Power collapse the Apps processor. This function executes the handshake |
| 1077 | * protocol with Modem. |
| 1078 | * |
| 1079 | * Return value: |
| 1080 | * -EAGAIN: modem reset occurred or early exit from power collapse |
| 1081 | * -EBUSY: modem not ready for our power collapse -- no power loss |
| 1082 | * -ETIMEDOUT: timed out waiting for modem's handshake -- no power loss |
| 1083 | * 0: success |
| 1084 | */ |
| 1085 | static int msm_pm_power_collapse |
| 1086 | (bool from_idle, uint32_t sleep_delay, uint32_t sleep_limit) |
| 1087 | { |
| 1088 | struct msm_pm_polled_group state_grps[2]; |
| 1089 | unsigned long saved_acpuclk_rate; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1090 | int collapsed = 0; |
| 1091 | int ret; |
| 1092 | |
| 1093 | MSM_PM_DPRINTK(MSM_PM_DEBUG_SUSPEND|MSM_PM_DEBUG_POWER_COLLAPSE, |
| 1094 | KERN_INFO, "%s(): idle %d, delay %u, limit %u\n", __func__, |
| 1095 | (int)from_idle, sleep_delay, sleep_limit); |
| 1096 | |
| 1097 | if (!(smsm_get_state(SMSM_POWER_MASTER_DEM) & DEM_MASTER_SMSM_READY)) { |
| 1098 | MSM_PM_DPRINTK( |
| 1099 | MSM_PM_DEBUG_SUSPEND | MSM_PM_DEBUG_POWER_COLLAPSE, |
| 1100 | KERN_INFO, "%s(): master not ready\n", __func__); |
| 1101 | ret = -EBUSY; |
| 1102 | goto power_collapse_bail; |
| 1103 | } |
| 1104 | |
| 1105 | memset(msm_pm_smem_data, 0, sizeof(*msm_pm_smem_data)); |
| 1106 | |
Murali Nalajala | 41786ab | 2012-03-06 10:47:32 +0530 | [diff] [blame] | 1107 | if (cpu_is_msm8625()) { |
| 1108 | /* Program the SPM */ |
| 1109 | ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_POWER_COLLAPSE, |
| 1110 | false); |
| 1111 | WARN_ON(ret); |
| 1112 | } |
| 1113 | |
Murali Nalajala | 2a0bbda | 2012-03-28 12:12:54 +0530 | [diff] [blame] | 1114 | msm_pm_irq_extns->enter_sleep1(true, from_idle, |
| 1115 | &msm_pm_smem_data->irq_mask); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1116 | msm_sirc_enter_sleep(); |
| 1117 | msm_gpio_enter_sleep(from_idle); |
| 1118 | |
| 1119 | msm_pm_smem_data->sleep_time = sleep_delay; |
| 1120 | msm_pm_smem_data->resources_used = sleep_limit; |
| 1121 | |
| 1122 | /* Enter PWRC/PWRC_SUSPEND */ |
| 1123 | |
| 1124 | if (from_idle) |
| 1125 | smsm_change_state(SMSM_APPS_DEM, DEM_SLAVE_SMSM_RUN, |
| 1126 | DEM_SLAVE_SMSM_PWRC); |
| 1127 | else |
| 1128 | smsm_change_state(SMSM_APPS_DEM, DEM_SLAVE_SMSM_RUN, |
| 1129 | DEM_SLAVE_SMSM_PWRC | DEM_SLAVE_SMSM_PWRC_SUSPEND); |
| 1130 | |
| 1131 | MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): PWRC"); |
| 1132 | MSM_PM_DEBUG_PRINT_SLEEP_INFO(); |
| 1133 | |
| 1134 | memset(state_grps, 0, sizeof(state_grps)); |
| 1135 | state_grps[0].group_id = SMSM_POWER_MASTER_DEM; |
| 1136 | state_grps[0].bits_all_set = DEM_MASTER_SMSM_RSA; |
| 1137 | state_grps[1].group_id = SMSM_MODEM_STATE; |
| 1138 | state_grps[1].bits_all_set = SMSM_RESET; |
| 1139 | |
| 1140 | ret = msm_pm_poll_state(ARRAY_SIZE(state_grps), state_grps); |
| 1141 | |
| 1142 | if (ret < 0) { |
| 1143 | printk(KERN_EMERG "%s(): power collapse entry " |
| 1144 | "timed out waiting for Modem's response\n", __func__); |
| 1145 | msm_pm_timeout(); |
| 1146 | } |
| 1147 | |
| 1148 | if (ret == 1) { |
| 1149 | MSM_PM_DPRINTK( |
| 1150 | MSM_PM_DEBUG_SUSPEND|MSM_PM_DEBUG_POWER_COLLAPSE, |
| 1151 | KERN_INFO, |
| 1152 | "%s(): msm_pm_poll_state detected Modem reset\n", |
| 1153 | __func__); |
| 1154 | goto power_collapse_early_exit; |
| 1155 | } |
| 1156 | |
| 1157 | /* DEM Master in RSA */ |
| 1158 | |
| 1159 | MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): PWRC RSA"); |
| 1160 | |
Murali Nalajala | 2a0bbda | 2012-03-28 12:12:54 +0530 | [diff] [blame] | 1161 | ret = msm_pm_irq_extns->enter_sleep2(true, from_idle); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1162 | if (ret < 0) { |
| 1163 | MSM_PM_DPRINTK( |
| 1164 | MSM_PM_DEBUG_SUSPEND|MSM_PM_DEBUG_POWER_COLLAPSE, |
| 1165 | KERN_INFO, |
| 1166 | "%s(): msm_irq_enter_sleep2 aborted, %d\n", __func__, |
| 1167 | ret); |
| 1168 | goto power_collapse_early_exit; |
| 1169 | } |
| 1170 | |
| 1171 | msm_pm_config_hw_before_power_down(); |
| 1172 | MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): pre power down"); |
| 1173 | |
| 1174 | saved_acpuclk_rate = acpuclk_power_collapse(); |
| 1175 | MSM_PM_DPRINTK(MSM_PM_DEBUG_CLOCK, KERN_INFO, |
| 1176 | "%s(): change clock rate (old rate = %lu)\n", __func__, |
| 1177 | saved_acpuclk_rate); |
| 1178 | |
| 1179 | if (saved_acpuclk_rate == 0) { |
| 1180 | msm_pm_config_hw_after_power_up(); |
| 1181 | goto power_collapse_early_exit; |
| 1182 | } |
| 1183 | |
Maheshkumar Sivasubramanian | 8ccc16e | 2011-10-25 15:59:57 -0600 | [diff] [blame] | 1184 | msm_pm_boot_config_before_pc(smp_processor_id(), |
| 1185 | virt_to_phys(msm_pm_collapse_exit)); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1186 | |
| 1187 | #ifdef CONFIG_VFP |
| 1188 | if (from_idle) |
| 1189 | vfp_flush_context(); |
| 1190 | #endif |
| 1191 | |
| 1192 | #ifdef CONFIG_CACHE_L2X0 |
| 1193 | l2x0_suspend(); |
| 1194 | #endif |
| 1195 | |
| 1196 | collapsed = msm_pm_collapse(); |
Anji jonnala | 393ff61 | 2012-03-29 20:21:39 +0530 | [diff] [blame] | 1197 | if (collapsed) |
| 1198 | power_collapsed = 1; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1199 | |
| 1200 | #ifdef CONFIG_CACHE_L2X0 |
| 1201 | l2x0_resume(collapsed); |
| 1202 | #endif |
| 1203 | |
Maheshkumar Sivasubramanian | 8ccc16e | 2011-10-25 15:59:57 -0600 | [diff] [blame] | 1204 | msm_pm_boot_config_after_pc(smp_processor_id()); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1205 | |
| 1206 | if (collapsed) { |
| 1207 | #ifdef CONFIG_VFP |
| 1208 | if (from_idle) |
| 1209 | vfp_reinit(); |
| 1210 | #endif |
| 1211 | cpu_init(); |
| 1212 | local_fiq_enable(); |
| 1213 | } |
| 1214 | |
| 1215 | MSM_PM_DPRINTK(MSM_PM_DEBUG_SUSPEND | MSM_PM_DEBUG_POWER_COLLAPSE, |
| 1216 | KERN_INFO, |
| 1217 | "%s(): msm_pm_collapse returned %d\n", __func__, collapsed); |
| 1218 | |
| 1219 | MSM_PM_DPRINTK(MSM_PM_DEBUG_CLOCK, KERN_INFO, |
| 1220 | "%s(): restore clock rate to %lu\n", __func__, |
| 1221 | saved_acpuclk_rate); |
| 1222 | if (acpuclk_set_rate(smp_processor_id(), saved_acpuclk_rate, |
| 1223 | SETRATE_PC) < 0) |
| 1224 | printk(KERN_ERR "%s(): failed to restore clock rate(%lu)\n", |
| 1225 | __func__, saved_acpuclk_rate); |
| 1226 | |
Murali Nalajala | 2a0bbda | 2012-03-28 12:12:54 +0530 | [diff] [blame] | 1227 | msm_pm_irq_extns->exit_sleep1(msm_pm_smem_data->irq_mask, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1228 | msm_pm_smem_data->wakeup_reason, |
| 1229 | msm_pm_smem_data->pending_irqs); |
| 1230 | |
| 1231 | msm_pm_config_hw_after_power_up(); |
| 1232 | MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): post power up"); |
| 1233 | |
| 1234 | memset(state_grps, 0, sizeof(state_grps)); |
| 1235 | state_grps[0].group_id = SMSM_POWER_MASTER_DEM; |
| 1236 | state_grps[0].bits_any_set = |
| 1237 | DEM_MASTER_SMSM_RSA | DEM_MASTER_SMSM_PWRC_EARLY_EXIT; |
| 1238 | state_grps[1].group_id = SMSM_MODEM_STATE; |
| 1239 | state_grps[1].bits_all_set = SMSM_RESET; |
| 1240 | |
| 1241 | ret = msm_pm_poll_state(ARRAY_SIZE(state_grps), state_grps); |
| 1242 | |
| 1243 | if (ret < 0) { |
| 1244 | printk(KERN_EMERG "%s(): power collapse exit " |
| 1245 | "timed out waiting for Modem's response\n", __func__); |
| 1246 | msm_pm_timeout(); |
| 1247 | } |
| 1248 | |
| 1249 | if (ret == 1) { |
| 1250 | MSM_PM_DPRINTK( |
| 1251 | MSM_PM_DEBUG_SUSPEND|MSM_PM_DEBUG_POWER_COLLAPSE, |
| 1252 | KERN_INFO, |
| 1253 | "%s(): msm_pm_poll_state detected Modem reset\n", |
| 1254 | __func__); |
| 1255 | goto power_collapse_early_exit; |
| 1256 | } |
| 1257 | |
| 1258 | /* Sanity check */ |
| 1259 | if (collapsed) { |
| 1260 | BUG_ON(!(state_grps[0].value_read & DEM_MASTER_SMSM_RSA)); |
| 1261 | } else { |
| 1262 | BUG_ON(!(state_grps[0].value_read & |
| 1263 | DEM_MASTER_SMSM_PWRC_EARLY_EXIT)); |
| 1264 | goto power_collapse_early_exit; |
| 1265 | } |
| 1266 | |
| 1267 | /* Enter WFPI */ |
| 1268 | |
| 1269 | smsm_change_state(SMSM_APPS_DEM, |
| 1270 | DEM_SLAVE_SMSM_PWRC | DEM_SLAVE_SMSM_PWRC_SUSPEND, |
| 1271 | DEM_SLAVE_SMSM_WFPI); |
| 1272 | |
| 1273 | MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): WFPI"); |
| 1274 | |
| 1275 | memset(state_grps, 0, sizeof(state_grps)); |
| 1276 | state_grps[0].group_id = SMSM_POWER_MASTER_DEM; |
| 1277 | state_grps[0].bits_all_set = DEM_MASTER_SMSM_RUN; |
| 1278 | state_grps[1].group_id = SMSM_MODEM_STATE; |
| 1279 | state_grps[1].bits_all_set = SMSM_RESET; |
| 1280 | |
| 1281 | ret = msm_pm_poll_state(ARRAY_SIZE(state_grps), state_grps); |
| 1282 | |
| 1283 | if (ret < 0) { |
| 1284 | printk(KERN_EMERG "%s(): power collapse WFPI " |
| 1285 | "timed out waiting for Modem's response\n", __func__); |
| 1286 | msm_pm_timeout(); |
| 1287 | } |
| 1288 | |
| 1289 | if (ret == 1) { |
| 1290 | MSM_PM_DPRINTK( |
| 1291 | MSM_PM_DEBUG_SUSPEND|MSM_PM_DEBUG_POWER_COLLAPSE, |
| 1292 | KERN_INFO, |
| 1293 | "%s(): msm_pm_poll_state detected Modem reset\n", |
| 1294 | __func__); |
| 1295 | ret = -EAGAIN; |
| 1296 | goto power_collapse_restore_gpio_bail; |
| 1297 | } |
| 1298 | |
| 1299 | /* DEM Master == RUN */ |
| 1300 | |
| 1301 | MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): WFPI RUN"); |
| 1302 | MSM_PM_DEBUG_PRINT_SLEEP_INFO(); |
| 1303 | |
Murali Nalajala | 2a0bbda | 2012-03-28 12:12:54 +0530 | [diff] [blame] | 1304 | msm_pm_irq_extns->exit_sleep2(msm_pm_smem_data->irq_mask, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1305 | msm_pm_smem_data->wakeup_reason, |
| 1306 | msm_pm_smem_data->pending_irqs); |
Murali Nalajala | 2a0bbda | 2012-03-28 12:12:54 +0530 | [diff] [blame] | 1307 | msm_pm_irq_extns->exit_sleep3(msm_pm_smem_data->irq_mask, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1308 | msm_pm_smem_data->wakeup_reason, |
| 1309 | msm_pm_smem_data->pending_irqs); |
| 1310 | msm_gpio_exit_sleep(); |
| 1311 | msm_sirc_exit_sleep(); |
| 1312 | |
| 1313 | smsm_change_state(SMSM_APPS_DEM, |
| 1314 | DEM_SLAVE_SMSM_WFPI, DEM_SLAVE_SMSM_RUN); |
| 1315 | |
| 1316 | MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): RUN"); |
| 1317 | |
| 1318 | smd_sleep_exit(); |
Murali Nalajala | 41786ab | 2012-03-06 10:47:32 +0530 | [diff] [blame] | 1319 | |
| 1320 | if (cpu_is_msm8625()) { |
| 1321 | ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, |
| 1322 | false); |
| 1323 | WARN_ON(ret); |
| 1324 | } |
| 1325 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1326 | return 0; |
| 1327 | |
| 1328 | power_collapse_early_exit: |
| 1329 | /* Enter PWRC_EARLY_EXIT */ |
| 1330 | |
| 1331 | smsm_change_state(SMSM_APPS_DEM, |
| 1332 | DEM_SLAVE_SMSM_PWRC | DEM_SLAVE_SMSM_PWRC_SUSPEND, |
| 1333 | DEM_SLAVE_SMSM_PWRC_EARLY_EXIT); |
| 1334 | |
| 1335 | MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): EARLY_EXIT"); |
| 1336 | |
| 1337 | memset(state_grps, 0, sizeof(state_grps)); |
| 1338 | state_grps[0].group_id = SMSM_POWER_MASTER_DEM; |
| 1339 | state_grps[0].bits_all_set = DEM_MASTER_SMSM_PWRC_EARLY_EXIT; |
| 1340 | state_grps[1].group_id = SMSM_MODEM_STATE; |
| 1341 | state_grps[1].bits_all_set = SMSM_RESET; |
| 1342 | |
| 1343 | ret = msm_pm_poll_state(ARRAY_SIZE(state_grps), state_grps); |
| 1344 | MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): EARLY_EXIT EE"); |
| 1345 | |
| 1346 | if (ret < 0) { |
| 1347 | printk(KERN_EMERG "%s(): power collapse EARLY_EXIT " |
| 1348 | "timed out waiting for Modem's response\n", __func__); |
| 1349 | msm_pm_timeout(); |
| 1350 | } |
| 1351 | |
| 1352 | if (ret == 1) { |
| 1353 | MSM_PM_DPRINTK( |
| 1354 | MSM_PM_DEBUG_SUSPEND|MSM_PM_DEBUG_POWER_COLLAPSE, |
| 1355 | KERN_INFO, |
| 1356 | "%s(): msm_pm_poll_state detected Modem reset\n", |
| 1357 | __func__); |
| 1358 | } |
| 1359 | |
| 1360 | /* DEM Master == RESET or PWRC_EARLY_EXIT */ |
| 1361 | |
| 1362 | ret = -EAGAIN; |
| 1363 | |
| 1364 | power_collapse_restore_gpio_bail: |
| 1365 | msm_gpio_exit_sleep(); |
| 1366 | msm_sirc_exit_sleep(); |
| 1367 | |
| 1368 | /* Enter RUN */ |
| 1369 | smsm_change_state(SMSM_APPS_DEM, |
| 1370 | DEM_SLAVE_SMSM_PWRC | DEM_SLAVE_SMSM_PWRC_SUSPEND | |
| 1371 | DEM_SLAVE_SMSM_PWRC_EARLY_EXIT, DEM_SLAVE_SMSM_RUN); |
| 1372 | |
| 1373 | MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): RUN"); |
| 1374 | |
| 1375 | if (collapsed) |
| 1376 | smd_sleep_exit(); |
| 1377 | |
| 1378 | power_collapse_bail: |
Murali Nalajala | 41786ab | 2012-03-06 10:47:32 +0530 | [diff] [blame] | 1379 | if (cpu_is_msm8625()) { |
| 1380 | ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, |
| 1381 | false); |
| 1382 | WARN_ON(ret); |
| 1383 | } |
| 1384 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1385 | return ret; |
| 1386 | } |
| 1387 | |
| 1388 | /* |
| 1389 | * Power collapse the Apps processor without involving Modem. |
| 1390 | * |
| 1391 | * Return value: |
| 1392 | * 0: success |
| 1393 | */ |
Murali Nalajala | 41786ab | 2012-03-06 10:47:32 +0530 | [diff] [blame] | 1394 | static int msm_pm_power_collapse_standalone(bool from_idle) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1395 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1396 | int collapsed = 0; |
| 1397 | int ret; |
Murali Nalajala | 41786ab | 2012-03-06 10:47:32 +0530 | [diff] [blame] | 1398 | void *entry; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1399 | |
| 1400 | MSM_PM_DPRINTK(MSM_PM_DEBUG_SUSPEND|MSM_PM_DEBUG_POWER_COLLAPSE, |
| 1401 | KERN_INFO, "%s()\n", __func__); |
| 1402 | |
| 1403 | ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_POWER_COLLAPSE, false); |
| 1404 | WARN_ON(ret); |
| 1405 | |
Murali Nalajala | 41786ab | 2012-03-06 10:47:32 +0530 | [diff] [blame] | 1406 | entry = (!smp_processor_id() || from_idle) ? |
| 1407 | msm_pm_collapse_exit : msm_secondary_startup; |
| 1408 | |
Maheshkumar Sivasubramanian | 8ccc16e | 2011-10-25 15:59:57 -0600 | [diff] [blame] | 1409 | msm_pm_boot_config_before_pc(smp_processor_id(), |
Murali Nalajala | 41786ab | 2012-03-06 10:47:32 +0530 | [diff] [blame] | 1410 | virt_to_phys(entry)); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1411 | |
| 1412 | #ifdef CONFIG_VFP |
| 1413 | vfp_flush_context(); |
| 1414 | #endif |
| 1415 | |
| 1416 | #ifdef CONFIG_CACHE_L2X0 |
Murali Nalajala | 41786ab | 2012-03-06 10:47:32 +0530 | [diff] [blame] | 1417 | if (!cpu_is_msm8625()) |
| 1418 | l2x0_suspend(); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1419 | #endif |
| 1420 | |
| 1421 | collapsed = msm_pm_collapse(); |
| 1422 | |
| 1423 | #ifdef CONFIG_CACHE_L2X0 |
Murali Nalajala | 41786ab | 2012-03-06 10:47:32 +0530 | [diff] [blame] | 1424 | if (!cpu_is_msm8625()) |
| 1425 | l2x0_resume(collapsed); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1426 | #endif |
| 1427 | |
Maheshkumar Sivasubramanian | 8ccc16e | 2011-10-25 15:59:57 -0600 | [diff] [blame] | 1428 | msm_pm_boot_config_after_pc(smp_processor_id()); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1429 | |
| 1430 | if (collapsed) { |
| 1431 | #ifdef CONFIG_VFP |
| 1432 | vfp_reinit(); |
| 1433 | #endif |
| 1434 | cpu_init(); |
| 1435 | local_fiq_enable(); |
| 1436 | } |
| 1437 | |
| 1438 | MSM_PM_DPRINTK(MSM_PM_DEBUG_SUSPEND | MSM_PM_DEBUG_POWER_COLLAPSE, |
| 1439 | KERN_INFO, |
| 1440 | "%s(): msm_pm_collapse returned %d\n", __func__, collapsed); |
| 1441 | |
| 1442 | ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false); |
| 1443 | WARN_ON(ret); |
| 1444 | |
Anji jonnala | c681622 | 2012-03-31 10:55:14 +0530 | [diff] [blame] | 1445 | return !collapsed; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1446 | } |
| 1447 | |
| 1448 | /* |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1449 | * Bring the Apps processor to SWFI. |
| 1450 | * |
| 1451 | * Return value: |
| 1452 | * -EIO: could not ramp Apps processor clock |
| 1453 | * 0: success |
| 1454 | */ |
| 1455 | static int msm_pm_swfi(bool ramp_acpu) |
| 1456 | { |
| 1457 | unsigned long saved_acpuclk_rate = 0; |
| 1458 | |
| 1459 | if (ramp_acpu) { |
| 1460 | saved_acpuclk_rate = acpuclk_wait_for_irq(); |
| 1461 | MSM_PM_DPRINTK(MSM_PM_DEBUG_CLOCK, KERN_INFO, |
| 1462 | "%s(): change clock rate (old rate = %lu)\n", __func__, |
| 1463 | saved_acpuclk_rate); |
| 1464 | |
| 1465 | if (!saved_acpuclk_rate) |
| 1466 | return -EIO; |
| 1467 | } |
| 1468 | |
Murali Nalajala | 41786ab | 2012-03-06 10:47:32 +0530 | [diff] [blame] | 1469 | if (!cpu_is_msm8625()) |
| 1470 | msm_pm_config_hw_before_swfi(); |
| 1471 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1472 | msm_arch_idle(); |
| 1473 | |
| 1474 | if (ramp_acpu) { |
| 1475 | MSM_PM_DPRINTK(MSM_PM_DEBUG_CLOCK, KERN_INFO, |
| 1476 | "%s(): restore clock rate to %lu\n", __func__, |
| 1477 | saved_acpuclk_rate); |
| 1478 | if (acpuclk_set_rate(smp_processor_id(), saved_acpuclk_rate, |
| 1479 | SETRATE_SWFI) < 0) |
| 1480 | printk(KERN_ERR |
| 1481 | "%s(): failed to restore clock rate(%lu)\n", |
| 1482 | __func__, saved_acpuclk_rate); |
| 1483 | } |
| 1484 | |
| 1485 | return 0; |
| 1486 | } |
| 1487 | |
| 1488 | |
| 1489 | /****************************************************************************** |
| 1490 | * External Idle/Suspend Functions |
| 1491 | *****************************************************************************/ |
| 1492 | |
| 1493 | /* |
| 1494 | * Put CPU in low power mode. |
| 1495 | */ |
| 1496 | void arch_idle(void) |
| 1497 | { |
| 1498 | bool allow[MSM_PM_SLEEP_MODE_NR]; |
| 1499 | uint32_t sleep_limit = SLEEP_LIMIT_NONE; |
| 1500 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1501 | int64_t timer_expiration; |
Murali Nalajala | 8fda449 | 2012-03-19 18:22:59 +0530 | [diff] [blame] | 1502 | int latency_qos; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1503 | int ret; |
| 1504 | int i; |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 1505 | unsigned int cpu; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1506 | |
| 1507 | #ifdef CONFIG_MSM_IDLE_STATS |
| 1508 | int64_t t1; |
Murali Nalajala | b86f370 | 2012-03-30 17:54:57 +0530 | [diff] [blame] | 1509 | static DEFINE_PER_CPU(int64_t, t2); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1510 | int exit_stat; |
Murali Nalajala | 41786ab | 2012-03-06 10:47:32 +0530 | [diff] [blame] | 1511 | #endif |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1512 | |
| 1513 | if (!atomic_read(&msm_pm_init_done)) |
| 1514 | return; |
| 1515 | |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 1516 | cpu = smp_processor_id(); |
| 1517 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1518 | latency_qos = pm_qos_request(PM_QOS_CPU_DMA_LATENCY); |
Murali Nalajala | 8fda449 | 2012-03-19 18:22:59 +0530 | [diff] [blame] | 1519 | /* get the next timer expiration */ |
| 1520 | timer_expiration = ktime_to_ns(tick_nohz_get_sleep_length()); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1521 | |
| 1522 | #ifdef CONFIG_MSM_IDLE_STATS |
| 1523 | t1 = ktime_to_ns(ktime_get()); |
Murali Nalajala | b86f370 | 2012-03-30 17:54:57 +0530 | [diff] [blame] | 1524 | msm_pm_add_stat(MSM_PM_STAT_NOT_IDLE, t1 - __get_cpu_var(t2)); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1525 | msm_pm_add_stat(MSM_PM_STAT_REQUESTED_IDLE, timer_expiration); |
Murali Nalajala | 7744d16 | 2012-01-13 13:06:03 +0530 | [diff] [blame] | 1526 | exit_stat = MSM_PM_STAT_IDLE_SPIN; |
Murali Nalajala | 41786ab | 2012-03-06 10:47:32 +0530 | [diff] [blame] | 1527 | #endif |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1528 | |
| 1529 | for (i = 0; i < ARRAY_SIZE(allow); i++) |
| 1530 | allow[i] = true; |
| 1531 | |
Murali Nalajala | 41786ab | 2012-03-06 10:47:32 +0530 | [diff] [blame] | 1532 | if (num_online_cpus() > 1 || |
| 1533 | (timer_expiration < msm_pm_idle_sleep_min_time) || |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1534 | #ifdef CONFIG_HAS_WAKELOCK |
| 1535 | has_wake_lock(WAKE_LOCK_IDLE) || |
| 1536 | #endif |
Murali Nalajala | 2a0bbda | 2012-03-28 12:12:54 +0530 | [diff] [blame] | 1537 | !msm_pm_irq_extns->idle_sleep_allowed()) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1538 | allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE] = false; |
| 1539 | allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_NO_XO_SHUTDOWN] = false; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1540 | } |
| 1541 | |
| 1542 | for (i = 0; i < ARRAY_SIZE(allow); i++) { |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 1543 | struct msm_pm_platform_data *mode = |
| 1544 | &msm_pm_modes[MSM_PM_MODE(cpu, i)]; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1545 | if (!mode->idle_supported || !mode->idle_enabled || |
| 1546 | mode->latency >= latency_qos || |
| 1547 | mode->residency * 1000ULL >= timer_expiration) |
| 1548 | allow[i] = false; |
| 1549 | } |
| 1550 | |
| 1551 | if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE] || |
| 1552 | allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_NO_XO_SHUTDOWN]) { |
| 1553 | uint32_t wait_us = CONFIG_MSM_IDLE_WAIT_ON_MODEM; |
| 1554 | while (msm_pm_modem_busy() && wait_us) { |
| 1555 | if (wait_us > 100) { |
| 1556 | udelay(100); |
| 1557 | wait_us -= 100; |
| 1558 | } else { |
| 1559 | udelay(wait_us); |
| 1560 | wait_us = 0; |
| 1561 | } |
| 1562 | } |
| 1563 | |
| 1564 | if (msm_pm_modem_busy()) { |
| 1565 | allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE] = false; |
| 1566 | allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_NO_XO_SHUTDOWN] |
| 1567 | = false; |
| 1568 | } |
| 1569 | } |
| 1570 | |
| 1571 | MSM_PM_DPRINTK(MSM_PM_DEBUG_IDLE, KERN_INFO, |
| 1572 | "%s(): latency qos %d, next timer %lld, sleep limit %u\n", |
| 1573 | __func__, latency_qos, timer_expiration, sleep_limit); |
| 1574 | |
| 1575 | for (i = 0; i < ARRAY_SIZE(allow); i++) |
| 1576 | MSM_PM_DPRINTK(MSM_PM_DEBUG_IDLE, KERN_INFO, |
| 1577 | "%s(): allow %s: %d\n", __func__, |
| 1578 | msm_pm_sleep_mode_labels[i], (int)allow[i]); |
| 1579 | |
| 1580 | if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE] || |
| 1581 | allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_NO_XO_SHUTDOWN]) { |
Murali Nalajala | 8fda449 | 2012-03-19 18:22:59 +0530 | [diff] [blame] | 1582 | /* Sync the timer with SCLK, it is needed only for modem |
| 1583 | * assissted pollapse case. |
| 1584 | */ |
| 1585 | int64_t next_timer_exp = msm_timer_enter_idle(); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1586 | uint32_t sleep_delay; |
Murali Nalajala | 8fda449 | 2012-03-19 18:22:59 +0530 | [diff] [blame] | 1587 | bool low_power = false; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1588 | |
| 1589 | sleep_delay = (uint32_t) msm_pm_convert_and_cap_time( |
Murali Nalajala | 8fda449 | 2012-03-19 18:22:59 +0530 | [diff] [blame] | 1590 | next_timer_exp, MSM_PM_SLEEP_TICK_LIMIT); |
| 1591 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1592 | if (sleep_delay == 0) /* 0 would mean infinite time */ |
| 1593 | sleep_delay = 1; |
| 1594 | |
| 1595 | if (!allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) |
| 1596 | sleep_limit = SLEEP_LIMIT_NO_TCXO_SHUTDOWN; |
| 1597 | |
| 1598 | #if defined(CONFIG_MSM_MEMORY_LOW_POWER_MODE_IDLE_ACTIVE) |
| 1599 | sleep_limit |= SLEEP_RESOURCE_MEMORY_BIT1; |
| 1600 | #elif defined(CONFIG_MSM_MEMORY_LOW_POWER_MODE_IDLE_RETENTION) |
| 1601 | sleep_limit |= SLEEP_RESOURCE_MEMORY_BIT0; |
| 1602 | #endif |
| 1603 | |
| 1604 | ret = msm_pm_power_collapse(true, sleep_delay, sleep_limit); |
| 1605 | low_power = (ret != -EBUSY && ret != -ETIMEDOUT); |
Murali Nalajala | 8fda449 | 2012-03-19 18:22:59 +0530 | [diff] [blame] | 1606 | msm_timer_exit_idle(low_power); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1607 | |
| 1608 | #ifdef CONFIG_MSM_IDLE_STATS |
| 1609 | if (ret) |
| 1610 | exit_stat = MSM_PM_STAT_IDLE_FAILED_POWER_COLLAPSE; |
| 1611 | else { |
| 1612 | exit_stat = MSM_PM_STAT_IDLE_POWER_COLLAPSE; |
| 1613 | msm_pm_sleep_limit = sleep_limit; |
| 1614 | } |
Murali Nalajala | 41786ab | 2012-03-06 10:47:32 +0530 | [diff] [blame] | 1615 | #endif |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1616 | } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) { |
Murali Nalajala | 41786ab | 2012-03-06 10:47:32 +0530 | [diff] [blame] | 1617 | ret = msm_pm_power_collapse_standalone(true); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1618 | #ifdef CONFIG_MSM_IDLE_STATS |
| 1619 | exit_stat = ret ? |
| 1620 | MSM_PM_STAT_IDLE_FAILED_STANDALONE_POWER_COLLAPSE : |
| 1621 | MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE; |
Murali Nalajala | 41786ab | 2012-03-06 10:47:32 +0530 | [diff] [blame] | 1622 | #endif |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1623 | } else if (allow[MSM_PM_SLEEP_MODE_RAMP_DOWN_AND_WAIT_FOR_INTERRUPT]) { |
| 1624 | ret = msm_pm_swfi(true); |
| 1625 | if (ret) |
Murali Nalajala | 2a0bbda | 2012-03-28 12:12:54 +0530 | [diff] [blame] | 1626 | while (!msm_pm_irq_extns->irq_pending()) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1627 | udelay(1); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1628 | #ifdef CONFIG_MSM_IDLE_STATS |
| 1629 | exit_stat = ret ? MSM_PM_STAT_IDLE_SPIN : MSM_PM_STAT_IDLE_WFI; |
Murali Nalajala | 41786ab | 2012-03-06 10:47:32 +0530 | [diff] [blame] | 1630 | #endif |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1631 | } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) { |
| 1632 | msm_pm_swfi(false); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1633 | #ifdef CONFIG_MSM_IDLE_STATS |
| 1634 | exit_stat = MSM_PM_STAT_IDLE_WFI; |
Murali Nalajala | 41786ab | 2012-03-06 10:47:32 +0530 | [diff] [blame] | 1635 | #endif |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1636 | } else { |
Murali Nalajala | 2a0bbda | 2012-03-28 12:12:54 +0530 | [diff] [blame] | 1637 | while (!msm_pm_irq_extns->irq_pending()) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1638 | udelay(1); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1639 | #ifdef CONFIG_MSM_IDLE_STATS |
| 1640 | exit_stat = MSM_PM_STAT_IDLE_SPIN; |
Murali Nalajala | 41786ab | 2012-03-06 10:47:32 +0530 | [diff] [blame] | 1641 | #endif |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1642 | } |
| 1643 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1644 | #ifdef CONFIG_MSM_IDLE_STATS |
Murali Nalajala | b86f370 | 2012-03-30 17:54:57 +0530 | [diff] [blame] | 1645 | __get_cpu_var(t2) = ktime_to_ns(ktime_get()); |
| 1646 | msm_pm_add_stat(exit_stat, __get_cpu_var(t2) - t1); |
Murali Nalajala | 41786ab | 2012-03-06 10:47:32 +0530 | [diff] [blame] | 1647 | #endif |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1648 | } |
| 1649 | |
| 1650 | /* |
| 1651 | * Suspend the Apps processor. |
| 1652 | * |
| 1653 | * Return value: |
Murali Nalajala | 41786ab | 2012-03-06 10:47:32 +0530 | [diff] [blame] | 1654 | * -EPERM: Suspend happened by a not permitted core |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1655 | * -EAGAIN: modem reset occurred or early exit from suspend |
| 1656 | * -EBUSY: modem not ready for our suspend |
| 1657 | * -EINVAL: invalid sleep mode |
| 1658 | * -EIO: could not ramp Apps processor clock |
| 1659 | * -ETIMEDOUT: timed out waiting for modem's handshake |
| 1660 | * 0: success |
| 1661 | */ |
| 1662 | static int msm_pm_enter(suspend_state_t state) |
| 1663 | { |
| 1664 | bool allow[MSM_PM_SLEEP_MODE_NR]; |
| 1665 | uint32_t sleep_limit = SLEEP_LIMIT_NONE; |
Murali Nalajala | 41786ab | 2012-03-06 10:47:32 +0530 | [diff] [blame] | 1666 | int ret = -EPERM; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1667 | int i; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1668 | #ifdef CONFIG_MSM_IDLE_STATS |
| 1669 | int64_t period = 0; |
| 1670 | int64_t time = 0; |
Murali Nalajala | 41786ab | 2012-03-06 10:47:32 +0530 | [diff] [blame] | 1671 | #endif |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1672 | |
Murali Nalajala | 41786ab | 2012-03-06 10:47:32 +0530 | [diff] [blame] | 1673 | /* Must executed by CORE0 */ |
| 1674 | if (smp_processor_id()) { |
| 1675 | __WARN(); |
| 1676 | goto suspend_exit; |
| 1677 | } |
| 1678 | |
| 1679 | #ifdef CONFIG_MSM_IDLE_STATS |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1680 | time = msm_timer_get_sclk_time(&period); |
| 1681 | #endif |
| 1682 | |
| 1683 | MSM_PM_DPRINTK(MSM_PM_DEBUG_SUSPEND, KERN_INFO, |
| 1684 | "%s(): sleep limit %u\n", __func__, sleep_limit); |
| 1685 | |
| 1686 | for (i = 0; i < ARRAY_SIZE(allow); i++) |
| 1687 | allow[i] = true; |
| 1688 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1689 | for (i = 0; i < ARRAY_SIZE(allow); i++) { |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 1690 | struct msm_pm_platform_data *mode; |
| 1691 | mode = &msm_pm_modes[MSM_PM_MODE(0, i)]; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1692 | if (!mode->suspend_supported || !mode->suspend_enabled) |
| 1693 | allow[i] = false; |
| 1694 | } |
| 1695 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1696 | if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE] || |
| 1697 | allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_NO_XO_SHUTDOWN]) { |
| 1698 | #ifdef CONFIG_MSM_IDLE_STATS |
| 1699 | enum msm_pm_time_stats_id id; |
| 1700 | int64_t end_time; |
| 1701 | #endif |
| 1702 | |
| 1703 | clock_debug_print_enabled(); |
| 1704 | |
| 1705 | #ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE |
| 1706 | if (msm_pm_sleep_time_override > 0) { |
| 1707 | int64_t ns; |
| 1708 | ns = NSEC_PER_SEC * (int64_t)msm_pm_sleep_time_override; |
| 1709 | msm_pm_set_max_sleep_time(ns); |
| 1710 | msm_pm_sleep_time_override = 0; |
| 1711 | } |
| 1712 | #endif |
| 1713 | if (!allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) |
| 1714 | sleep_limit = SLEEP_LIMIT_NO_TCXO_SHUTDOWN; |
| 1715 | |
| 1716 | #if defined(CONFIG_MSM_MEMORY_LOW_POWER_MODE_SUSPEND_ACTIVE) |
| 1717 | sleep_limit |= SLEEP_RESOURCE_MEMORY_BIT1; |
| 1718 | #elif defined(CONFIG_MSM_MEMORY_LOW_POWER_MODE_SUSPEND_RETENTION) |
| 1719 | sleep_limit |= SLEEP_RESOURCE_MEMORY_BIT0; |
| 1720 | #elif defined(CONFIG_MSM_MEMORY_LOW_POWER_MODE_SUSPEND_DEEP_POWER_DOWN) |
| 1721 | if (get_msm_migrate_pages_status() != MEM_OFFLINE) |
| 1722 | sleep_limit |= SLEEP_RESOURCE_MEMORY_BIT0; |
| 1723 | #endif |
| 1724 | |
| 1725 | for (i = 0; i < 30 && msm_pm_modem_busy(); i++) |
| 1726 | udelay(500); |
| 1727 | |
| 1728 | ret = msm_pm_power_collapse( |
| 1729 | false, msm_pm_max_sleep_time, sleep_limit); |
| 1730 | |
| 1731 | #ifdef CONFIG_MSM_IDLE_STATS |
| 1732 | if (ret) |
| 1733 | id = MSM_PM_STAT_FAILED_SUSPEND; |
| 1734 | else { |
| 1735 | id = MSM_PM_STAT_SUSPEND; |
| 1736 | msm_pm_sleep_limit = sleep_limit; |
| 1737 | } |
| 1738 | |
| 1739 | if (time != 0) { |
| 1740 | end_time = msm_timer_get_sclk_time(NULL); |
| 1741 | if (end_time != 0) { |
| 1742 | time = end_time - time; |
| 1743 | if (time < 0) |
| 1744 | time += period; |
| 1745 | } else |
| 1746 | time = 0; |
| 1747 | } |
| 1748 | |
| 1749 | msm_pm_add_stat(id, time); |
| 1750 | #endif |
| 1751 | } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) { |
Murali Nalajala | 41786ab | 2012-03-06 10:47:32 +0530 | [diff] [blame] | 1752 | ret = msm_pm_power_collapse_standalone(false); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1753 | } else if (allow[MSM_PM_SLEEP_MODE_RAMP_DOWN_AND_WAIT_FOR_INTERRUPT]) { |
| 1754 | ret = msm_pm_swfi(true); |
| 1755 | if (ret) |
Murali Nalajala | 2a0bbda | 2012-03-28 12:12:54 +0530 | [diff] [blame] | 1756 | while (!msm_pm_irq_extns->irq_pending()) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1757 | udelay(1); |
| 1758 | } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) { |
| 1759 | msm_pm_swfi(false); |
| 1760 | } |
| 1761 | |
Murali Nalajala | 41786ab | 2012-03-06 10:47:32 +0530 | [diff] [blame] | 1762 | suspend_exit: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1763 | MSM_PM_DPRINTK(MSM_PM_DEBUG_SUSPEND, KERN_INFO, |
| 1764 | "%s(): return %d\n", __func__, ret); |
| 1765 | |
| 1766 | return ret; |
| 1767 | } |
| 1768 | |
| 1769 | static struct platform_suspend_ops msm_pm_ops = { |
| 1770 | .enter = msm_pm_enter, |
| 1771 | .valid = suspend_valid_only_mem, |
| 1772 | }; |
| 1773 | |
Murali Nalajala | c89f2f3 | 2012-02-07 19:23:52 +0530 | [diff] [blame] | 1774 | /* Hotplug the "non boot" CPU's and put |
| 1775 | * the cores into low power mode |
| 1776 | */ |
| 1777 | void msm_pm_cpu_enter_lowpower(unsigned int cpu) |
| 1778 | { |
Murali Nalajala | a7efba1 | 2012-02-23 18:13:52 +0530 | [diff] [blame] | 1779 | bool allow[MSM_PM_SLEEP_MODE_NR]; |
| 1780 | int i; |
| 1781 | |
| 1782 | for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) { |
| 1783 | struct msm_pm_platform_data *mode; |
| 1784 | |
| 1785 | mode = &msm_pm_modes[MSM_PM_MODE(cpu, i)]; |
| 1786 | allow[i] = mode->suspend_supported && mode->suspend_enabled; |
| 1787 | } |
| 1788 | |
| 1789 | MSM_PM_DPRINTK(MSM_PM_DEBUG_HOTPLUG, KERN_INFO, |
| 1790 | "CPU%u: %s: shutting down cpu\n", cpu, __func__); |
| 1791 | |
| 1792 | if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) { |
| 1793 | msm_pm_power_collapse_standalone(false); |
| 1794 | } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) { |
| 1795 | msm_pm_swfi(false); |
| 1796 | } else { |
| 1797 | MSM_PM_DPRINTK(MSM_PM_DEBUG_HOTPLUG, KERN_INFO, |
| 1798 | "CPU%u: %s: shutting down failed!!!\n", cpu, __func__); |
| 1799 | } |
Murali Nalajala | c89f2f3 | 2012-02-07 19:23:52 +0530 | [diff] [blame] | 1800 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1801 | |
| 1802 | /****************************************************************************** |
| 1803 | * Restart Definitions |
| 1804 | *****************************************************************************/ |
| 1805 | |
| 1806 | static uint32_t restart_reason = 0x776655AA; |
| 1807 | |
| 1808 | static void msm_pm_power_off(void) |
| 1809 | { |
| 1810 | msm_rpcrouter_close(); |
| 1811 | msm_proc_comm(PCOM_POWER_DOWN, 0, 0); |
| 1812 | for (;;) |
| 1813 | ; |
| 1814 | } |
| 1815 | |
| 1816 | static void msm_pm_restart(char str, const char *cmd) |
| 1817 | { |
| 1818 | msm_rpcrouter_close(); |
| 1819 | msm_proc_comm(PCOM_RESET_CHIP, &restart_reason, 0); |
| 1820 | |
| 1821 | for (;;) |
| 1822 | ; |
| 1823 | } |
| 1824 | |
| 1825 | static int msm_reboot_call |
| 1826 | (struct notifier_block *this, unsigned long code, void *_cmd) |
| 1827 | { |
| 1828 | if ((code == SYS_RESTART) && _cmd) { |
| 1829 | char *cmd = _cmd; |
| 1830 | if (!strcmp(cmd, "bootloader")) { |
| 1831 | restart_reason = 0x77665500; |
| 1832 | } else if (!strcmp(cmd, "recovery")) { |
| 1833 | restart_reason = 0x77665502; |
| 1834 | } else if (!strcmp(cmd, "eraseflash")) { |
| 1835 | restart_reason = 0x776655EF; |
| 1836 | } else if (!strncmp(cmd, "oem-", 4)) { |
| 1837 | unsigned code = simple_strtoul(cmd + 4, 0, 16) & 0xff; |
| 1838 | restart_reason = 0x6f656d00 | code; |
| 1839 | } else { |
| 1840 | restart_reason = 0x77665501; |
| 1841 | } |
| 1842 | } |
| 1843 | return NOTIFY_DONE; |
| 1844 | } |
| 1845 | |
| 1846 | static struct notifier_block msm_reboot_notifier = { |
| 1847 | .notifier_call = msm_reboot_call, |
| 1848 | }; |
| 1849 | |
| 1850 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1851 | /* |
| 1852 | * Initialize the power management subsystem. |
| 1853 | * |
| 1854 | * Return value: |
| 1855 | * -ENODEV: initialization failed |
| 1856 | * 0: success |
| 1857 | */ |
| 1858 | static int __init msm_pm_init(void) |
| 1859 | { |
| 1860 | #ifdef CONFIG_MSM_IDLE_STATS |
| 1861 | struct proc_dir_entry *d_entry; |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 1862 | unsigned int cpu; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1863 | #endif |
| 1864 | int ret; |
| 1865 | #ifdef CONFIG_CPU_V7 |
| 1866 | pgd_t *pc_pgd; |
| 1867 | pmd_t *pmd; |
| 1868 | unsigned long pmdval; |
| 1869 | |
| 1870 | /* Page table for cores to come back up safely. */ |
| 1871 | pc_pgd = pgd_alloc(&init_mm); |
| 1872 | if (!pc_pgd) |
| 1873 | return -ENOMEM; |
| 1874 | pmd = pmd_offset(pc_pgd + |
| 1875 | pgd_index(virt_to_phys(msm_pm_collapse_exit)), |
| 1876 | virt_to_phys(msm_pm_collapse_exit)); |
| 1877 | pmdval = (virt_to_phys(msm_pm_collapse_exit) & PGDIR_MASK) | |
| 1878 | PMD_TYPE_SECT | PMD_SECT_AP_WRITE; |
| 1879 | pmd[0] = __pmd(pmdval); |
| 1880 | pmd[1] = __pmd(pmdval + (1 << (PGDIR_SHIFT - 1))); |
| 1881 | |
| 1882 | /* It is remotely possible that the code in msm_pm_collapse_exit() |
| 1883 | * which turns on the MMU with this mapping is in the |
| 1884 | * next even-numbered megabyte beyond the |
| 1885 | * start of msm_pm_collapse_exit(). |
| 1886 | * Map this megabyte in as well. |
| 1887 | */ |
| 1888 | pmd[2] = __pmd(pmdval + (2 << (PGDIR_SHIFT - 1))); |
| 1889 | flush_pmd_entry(pmd); |
| 1890 | msm_pm_pc_pgd = virt_to_phys(pc_pgd); |
| 1891 | #endif |
| 1892 | |
| 1893 | pm_power_off = msm_pm_power_off; |
| 1894 | arm_pm_restart = msm_pm_restart; |
| 1895 | register_reboot_notifier(&msm_reboot_notifier); |
| 1896 | |
| 1897 | msm_pm_smem_data = smem_alloc(SMEM_APPS_DEM_SLAVE_DATA, |
| 1898 | sizeof(*msm_pm_smem_data)); |
| 1899 | if (msm_pm_smem_data == NULL) { |
| 1900 | printk(KERN_ERR "%s: failed to get smsm_data\n", __func__); |
| 1901 | return -ENODEV; |
| 1902 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1903 | |
| 1904 | ret = msm_timer_init_time_sync(msm_pm_timeout); |
| 1905 | if (ret) |
| 1906 | return ret; |
| 1907 | |
| 1908 | ret = smsm_change_intr_mask(SMSM_POWER_MASTER_DEM, 0xFFFFFFFF, 0); |
| 1909 | if (ret) { |
| 1910 | printk(KERN_ERR "%s: failed to clear interrupt mask, %d\n", |
| 1911 | __func__, ret); |
| 1912 | return ret; |
| 1913 | } |
| 1914 | |
| 1915 | #ifdef CONFIG_MSM_MEMORY_LOW_POWER_MODE |
| 1916 | /* The wakeup_reason field is overloaded during initialization time |
| 1917 | to signal Modem that Apps will control the low power modes of |
| 1918 | the memory. |
| 1919 | */ |
| 1920 | msm_pm_smem_data->wakeup_reason = 1; |
| 1921 | smsm_change_state(SMSM_APPS_DEM, 0, DEM_SLAVE_SMSM_RUN); |
| 1922 | #endif |
| 1923 | |
| 1924 | BUG_ON(msm_pm_modes == NULL); |
| 1925 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1926 | suspend_set_ops(&msm_pm_ops); |
| 1927 | |
| 1928 | msm_pm_mode_sysfs_add(); |
| 1929 | #ifdef CONFIG_MSM_IDLE_STATS |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 1930 | for_each_possible_cpu(cpu) { |
| 1931 | struct msm_pm_time_stats *stats = |
| 1932 | per_cpu(msm_pm_stats, cpu).stats; |
| 1933 | |
| 1934 | stats[MSM_PM_STAT_REQUESTED_IDLE].name = "idle-request"; |
| 1935 | stats[MSM_PM_STAT_REQUESTED_IDLE].first_bucket_time = |
| 1936 | CONFIG_MSM_IDLE_STATS_FIRST_BUCKET; |
| 1937 | |
| 1938 | stats[MSM_PM_STAT_IDLE_SPIN].name = "idle-spin"; |
| 1939 | stats[MSM_PM_STAT_IDLE_SPIN].first_bucket_time = |
| 1940 | CONFIG_MSM_IDLE_STATS_FIRST_BUCKET; |
| 1941 | |
| 1942 | stats[MSM_PM_STAT_IDLE_WFI].name = "idle-wfi"; |
| 1943 | stats[MSM_PM_STAT_IDLE_WFI].first_bucket_time = |
| 1944 | CONFIG_MSM_IDLE_STATS_FIRST_BUCKET; |
| 1945 | |
| 1946 | stats[MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE].name = |
| 1947 | "idle-standalone-power-collapse"; |
| 1948 | stats[MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE]. |
| 1949 | first_bucket_time = |
| 1950 | CONFIG_MSM_IDLE_STATS_FIRST_BUCKET; |
| 1951 | |
| 1952 | stats[MSM_PM_STAT_IDLE_FAILED_STANDALONE_POWER_COLLAPSE].name = |
| 1953 | "idle-failed-standalone-power-collapse"; |
| 1954 | stats[MSM_PM_STAT_IDLE_FAILED_STANDALONE_POWER_COLLAPSE]. |
| 1955 | first_bucket_time = |
| 1956 | CONFIG_MSM_IDLE_STATS_FIRST_BUCKET; |
| 1957 | |
Murali Nalajala | 0df9fee | 2012-01-12 15:26:09 +0530 | [diff] [blame] | 1958 | stats[MSM_PM_STAT_IDLE_POWER_COLLAPSE].name = |
| 1959 | "idle-power-collapse"; |
| 1960 | stats[MSM_PM_STAT_IDLE_POWER_COLLAPSE].first_bucket_time = |
| 1961 | CONFIG_MSM_IDLE_STATS_FIRST_BUCKET; |
| 1962 | |
| 1963 | stats[MSM_PM_STAT_IDLE_FAILED_POWER_COLLAPSE].name = |
| 1964 | "idle-failed-power-collapse"; |
| 1965 | stats[MSM_PM_STAT_IDLE_FAILED_POWER_COLLAPSE]. |
| 1966 | first_bucket_time = |
| 1967 | CONFIG_MSM_IDLE_STATS_FIRST_BUCKET; |
| 1968 | |
| 1969 | stats[MSM_PM_STAT_SUSPEND].name = "suspend"; |
| 1970 | stats[MSM_PM_STAT_SUSPEND].first_bucket_time = |
| 1971 | CONFIG_MSM_SUSPEND_STATS_FIRST_BUCKET; |
| 1972 | |
| 1973 | stats[MSM_PM_STAT_FAILED_SUSPEND].name = "failed-suspend"; |
| 1974 | stats[MSM_PM_STAT_FAILED_SUSPEND].first_bucket_time = |
| 1975 | CONFIG_MSM_IDLE_STATS_FIRST_BUCKET; |
| 1976 | |
| 1977 | stats[MSM_PM_STAT_NOT_IDLE].name = "not-idle"; |
| 1978 | stats[MSM_PM_STAT_NOT_IDLE].first_bucket_time = |
| 1979 | CONFIG_MSM_IDLE_STATS_FIRST_BUCKET; |
| 1980 | } |
Murali Nalajala | 558c0ce | 2012-03-29 19:42:08 +0530 | [diff] [blame] | 1981 | |
| 1982 | atomic_set(&msm_pm_init_done, 1); |
| 1983 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1984 | d_entry = create_proc_entry("msm_pm_stats", |
| 1985 | S_IRUGO | S_IWUSR | S_IWGRP, NULL); |
| 1986 | if (d_entry) { |
| 1987 | d_entry->read_proc = msm_pm_read_proc; |
| 1988 | d_entry->write_proc = msm_pm_write_proc; |
| 1989 | d_entry->data = NULL; |
| 1990 | } |
| 1991 | #endif |
| 1992 | |
| 1993 | return 0; |
| 1994 | } |
| 1995 | |
| 1996 | late_initcall_sync(msm_pm_init); |