Mahesh Sivasubramanian | 1b8601b | 2012-12-20 14:11:23 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2010-2013, The Linux Foundation. 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 | |
Priyanka Mathur | 13bdad1 | 2013-01-28 15:52:56 -0800 | [diff] [blame] | 14 | #include <linux/debugfs.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 15 | #include <linux/module.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/completion.h> |
| 19 | #include <linux/cpuidle.h> |
| 20 | #include <linux/interrupt.h> |
| 21 | #include <linux/io.h> |
| 22 | #include <linux/ktime.h> |
| 23 | #include <linux/pm.h> |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 24 | #include <linux/pm_qos.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 25 | #include <linux/smp.h> |
| 26 | #include <linux/suspend.h> |
| 27 | #include <linux/tick.h> |
Anji Jonnala | 02dac8d | 2013-03-06 21:31:04 +0530 | [diff] [blame] | 28 | #include <linux/delay.h> |
Mahesh Sivasubramanian | cb39662 | 2012-03-14 14:50:37 -0600 | [diff] [blame] | 29 | #include <linux/platform_device.h> |
Anji Jonnala | 02dac8d | 2013-03-06 21:31:04 +0530 | [diff] [blame] | 30 | #include <linux/of_platform.h> |
Abhijeet Dharmapurikar | e8f8385 | 2013-02-06 18:57:17 -0800 | [diff] [blame] | 31 | #include <linux/regulator/krait-regulator.h> |
Mahesh Sivasubramanian | b7bda88 | 2013-04-08 09:55:48 -0600 | [diff] [blame^] | 32 | #include <linux/cpu.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 33 | #include <mach/msm_iomap.h> |
Praveen Chidambaram | 192979f | 2012-04-25 18:30:23 -0600 | [diff] [blame] | 34 | #include <mach/socinfo.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 35 | #include <mach/system.h> |
Girish Mahadevan | d27ca4a | 2012-08-15 09:21:23 -0600 | [diff] [blame] | 36 | #include <mach/scm.h> |
| 37 | #include <mach/socinfo.h> |
Praveen Chidambaram | c594a09 | 2012-09-18 19:48:29 -0600 | [diff] [blame] | 38 | #include <mach/msm-krait-l2-accessors.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 39 | #include <asm/cacheflush.h> |
| 40 | #include <asm/hardware/gic.h> |
| 41 | #include <asm/pgtable.h> |
| 42 | #include <asm/pgalloc.h> |
Girish Mahadevan | 5594499 | 2012-10-26 11:03:07 -0600 | [diff] [blame] | 43 | #include <asm/outercache.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 44 | #ifdef CONFIG_VFP |
| 45 | #include <asm/vfp.h> |
| 46 | #endif |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 47 | #include "acpuclock.h" |
| 48 | #include "clock.h" |
| 49 | #include "avs.h" |
Abhijeet Dharmapurikar | efaca4f | 2011-12-27 16:24:07 -0800 | [diff] [blame] | 50 | #include <mach/cpuidle.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 51 | #include "idle.h" |
Matt Wagantall | 7cca464 | 2012-02-01 16:43:24 -0800 | [diff] [blame] | 52 | #include "pm.h" |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 53 | #include "scm-boot.h" |
| 54 | #include "spm.h" |
| 55 | #include "timer.h" |
Maheshkumar Sivasubramanian | 8ccc16e | 2011-10-25 15:59:57 -0600 | [diff] [blame] | 56 | #include "pm-boot.h" |
Girish Mahadevan | dc318fd | 2012-08-17 16:48:05 -0600 | [diff] [blame] | 57 | #include <mach/event_timer.h> |
Priyanka Mathur | 26b4a4b | 2012-11-05 13:45:45 -0800 | [diff] [blame] | 58 | #define CREATE_TRACE_POINTS |
| 59 | #include "trace_msm_low_power.h" |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 60 | #define SCM_L2_RETENTION (0x2) |
| 61 | #define SCM_CMD_TERMINATE_PC (0x2) |
| 62 | |
| 63 | #define GET_CPU_OF_ATTR(attr) \ |
| 64 | (container_of(attr, struct msm_pm_kobj_attribute, ka)->cpu) |
| 65 | |
| 66 | #define SCLK_HZ (32768) |
| 67 | #define MSM_PM_SLEEP_TICK_LIMIT (0x6DDD000) |
| 68 | |
Priyanka Mathur | 13bdad1 | 2013-01-28 15:52:56 -0800 | [diff] [blame] | 69 | #define NUM_OF_COUNTERS 3 |
| 70 | #define MAX_BUF_SIZE 512 |
| 71 | |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 72 | static int msm_pm_debug_mask = 1; |
| 73 | module_param_named( |
| 74 | debug_mask, msm_pm_debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP |
| 75 | ); |
| 76 | |
| 77 | static int msm_pm_sleep_time_override; |
| 78 | module_param_named(sleep_time_override, |
| 79 | msm_pm_sleep_time_override, int, S_IRUGO | S_IWUSR | S_IWGRP); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 80 | |
| 81 | enum { |
| 82 | MSM_PM_DEBUG_SUSPEND = BIT(0), |
| 83 | MSM_PM_DEBUG_POWER_COLLAPSE = BIT(1), |
| 84 | MSM_PM_DEBUG_SUSPEND_LIMITS = BIT(2), |
| 85 | MSM_PM_DEBUG_CLOCK = BIT(3), |
| 86 | MSM_PM_DEBUG_RESET_VECTOR = BIT(4), |
Karthik Parsha | 6fb932d | 2012-01-24 18:04:12 -0800 | [diff] [blame] | 87 | MSM_PM_DEBUG_IDLE_CLK = BIT(5), |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 88 | MSM_PM_DEBUG_IDLE = BIT(6), |
| 89 | MSM_PM_DEBUG_IDLE_LIMITS = BIT(7), |
| 90 | MSM_PM_DEBUG_HOTPLUG = BIT(8), |
| 91 | }; |
| 92 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 93 | enum { |
| 94 | MSM_PM_MODE_ATTR_SUSPEND, |
| 95 | MSM_PM_MODE_ATTR_IDLE, |
| 96 | MSM_PM_MODE_ATTR_NR, |
| 97 | }; |
| 98 | |
| 99 | static char *msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_NR] = { |
| 100 | [MSM_PM_MODE_ATTR_SUSPEND] = "suspend_enabled", |
| 101 | [MSM_PM_MODE_ATTR_IDLE] = "idle_enabled", |
| 102 | }; |
| 103 | |
| 104 | struct msm_pm_kobj_attribute { |
| 105 | unsigned int cpu; |
| 106 | struct kobj_attribute ka; |
| 107 | }; |
| 108 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 109 | struct msm_pm_sysfs_sleep_mode { |
| 110 | struct kobject *kobj; |
| 111 | struct attribute_group attr_group; |
| 112 | struct attribute *attrs[MSM_PM_MODE_ATTR_NR + 1]; |
| 113 | struct msm_pm_kobj_attribute kas[MSM_PM_MODE_ATTR_NR]; |
| 114 | }; |
| 115 | |
| 116 | static char *msm_pm_sleep_mode_labels[MSM_PM_SLEEP_MODE_NR] = { |
| 117 | [MSM_PM_SLEEP_MODE_POWER_COLLAPSE] = "power_collapse", |
| 118 | [MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT] = "wfi", |
Praveen Chidambaram | d3d844d | 2012-04-24 09:47:38 -0600 | [diff] [blame] | 119 | [MSM_PM_SLEEP_MODE_RETENTION] = "retention", |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 120 | [MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE] = |
| 121 | "standalone_power_collapse", |
| 122 | }; |
| 123 | |
Girish Mahadevan | dc318fd | 2012-08-17 16:48:05 -0600 | [diff] [blame] | 124 | static struct hrtimer pm_hrtimer; |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 125 | static struct msm_pm_sleep_ops pm_sleep_ops; |
Mahesh Sivasubramanian | 1b8601b | 2012-12-20 14:11:23 -0700 | [diff] [blame] | 126 | static bool msm_pm_ldo_retention_enabled = true; |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 127 | static bool msm_pm_use_sync_timer; |
| 128 | static struct msm_pm_cp15_save_data cp15_data; |
| 129 | static bool msm_pm_retention_calls_tz; |
| 130 | static uint32_t msm_pm_max_sleep_time; |
Praveen Chidambaram | 4b8df03 | 2013-01-18 16:21:16 -0700 | [diff] [blame] | 131 | static bool msm_no_ramp_down_pc; |
Anji Jonnala | 02dac8d | 2013-03-06 21:31:04 +0530 | [diff] [blame] | 132 | static struct msm_pm_sleep_status_data *msm_pm_slp_sts; |
Mahesh Sivasubramanian | b7bda88 | 2013-04-08 09:55:48 -0600 | [diff] [blame^] | 133 | static bool msm_pm_pc_reset_timer; |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 134 | |
Archana Sathyakumar | 2b91dc8 | 2013-02-01 17:38:23 -0700 | [diff] [blame] | 135 | static int msm_pm_get_pc_mode(struct device_node *node, |
| 136 | const char *key, uint32_t *pc_mode_val) |
| 137 | { |
| 138 | struct pc_mode_of { |
| 139 | uint32_t mode; |
| 140 | char *mode_name; |
| 141 | }; |
| 142 | int i; |
| 143 | struct pc_mode_of pc_modes[] = { |
| 144 | {MSM_PM_PC_TZ_L2_INT, "tz_l2_int"}, |
| 145 | {MSM_PM_PC_NOTZ_L2_EXT, "no_tz_l2_ext"}, |
| 146 | {MSM_PM_PC_TZ_L2_EXT , "tz_l2_ext"} }; |
| 147 | int ret; |
| 148 | const char *pc_mode_str; |
| 149 | |
| 150 | ret = of_property_read_string(node, key, &pc_mode_str); |
| 151 | if (ret) { |
| 152 | pr_debug("%s: Cannot read %s,defaulting to 0", __func__, key); |
| 153 | pc_mode_val = MSM_PM_PC_TZ_L2_INT; |
| 154 | ret = 0; |
| 155 | } else { |
| 156 | ret = -EINVAL; |
| 157 | for (i = 0; i < ARRAY_SIZE(pc_modes); i++) { |
| 158 | if (!strncmp(pc_mode_str, pc_modes[i].mode_name, |
| 159 | strlen(pc_modes[i].mode_name))) { |
| 160 | *pc_mode_val = pc_modes[i].mode; |
| 161 | ret = 0; |
| 162 | break; |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | return ret; |
| 167 | } |
| 168 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 169 | /* |
| 170 | * Write out the attribute. |
| 171 | */ |
| 172 | static ssize_t msm_pm_mode_attr_show( |
| 173 | struct kobject *kobj, struct kobj_attribute *attr, char *buf) |
| 174 | { |
| 175 | int ret = -EINVAL; |
| 176 | int i; |
| 177 | |
| 178 | for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) { |
| 179 | struct kernel_param kp; |
| 180 | unsigned int cpu; |
| 181 | struct msm_pm_platform_data *mode; |
| 182 | |
| 183 | if (msm_pm_sleep_mode_labels[i] == NULL) |
| 184 | continue; |
| 185 | |
| 186 | if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i])) |
| 187 | continue; |
| 188 | |
| 189 | cpu = GET_CPU_OF_ATTR(attr); |
Praveen Chidambaram | 42da9d2 | 2012-03-30 12:16:34 -0600 | [diff] [blame] | 190 | mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)]; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 191 | |
| 192 | if (!strcmp(attr->attr.name, |
| 193 | msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) { |
| 194 | u32 arg = mode->suspend_enabled; |
| 195 | kp.arg = &arg; |
| 196 | ret = param_get_ulong(buf, &kp); |
| 197 | } else if (!strcmp(attr->attr.name, |
| 198 | msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) { |
| 199 | u32 arg = mode->idle_enabled; |
| 200 | kp.arg = &arg; |
| 201 | ret = param_get_ulong(buf, &kp); |
| 202 | } |
| 203 | |
| 204 | break; |
| 205 | } |
| 206 | |
| 207 | if (ret > 0) { |
Praveen Chidambaram | 2b0fdd0 | 2011-10-28 16:40:58 -0600 | [diff] [blame] | 208 | strlcat(buf, "\n", PAGE_SIZE); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 209 | ret++; |
| 210 | } |
| 211 | |
| 212 | return ret; |
| 213 | } |
| 214 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 215 | static ssize_t msm_pm_mode_attr_store(struct kobject *kobj, |
| 216 | struct kobj_attribute *attr, const char *buf, size_t count) |
| 217 | { |
| 218 | int ret = -EINVAL; |
| 219 | int i; |
| 220 | |
| 221 | for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) { |
| 222 | struct kernel_param kp; |
| 223 | unsigned int cpu; |
| 224 | struct msm_pm_platform_data *mode; |
| 225 | |
| 226 | if (msm_pm_sleep_mode_labels[i] == NULL) |
| 227 | continue; |
| 228 | |
| 229 | if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i])) |
| 230 | continue; |
| 231 | |
| 232 | cpu = GET_CPU_OF_ATTR(attr); |
Praveen Chidambaram | 42da9d2 | 2012-03-30 12:16:34 -0600 | [diff] [blame] | 233 | mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)]; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 234 | |
| 235 | if (!strcmp(attr->attr.name, |
| 236 | msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) { |
| 237 | kp.arg = &mode->suspend_enabled; |
| 238 | ret = param_set_byte(buf, &kp); |
| 239 | } else if (!strcmp(attr->attr.name, |
| 240 | msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) { |
| 241 | kp.arg = &mode->idle_enabled; |
| 242 | ret = param_set_byte(buf, &kp); |
| 243 | } |
| 244 | |
| 245 | break; |
| 246 | } |
| 247 | |
| 248 | return ret ? ret : count; |
| 249 | } |
| 250 | |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 251 | static int __devinit msm_pm_mode_sysfs_add_cpu( |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 252 | unsigned int cpu, struct kobject *modes_kobj) |
| 253 | { |
| 254 | char cpu_name[8]; |
| 255 | struct kobject *cpu_kobj; |
Praveen Chidambaram | 2b0fdd0 | 2011-10-28 16:40:58 -0600 | [diff] [blame] | 256 | struct msm_pm_sysfs_sleep_mode *mode = NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 257 | int i, j, k; |
| 258 | int ret; |
| 259 | |
| 260 | snprintf(cpu_name, sizeof(cpu_name), "cpu%u", cpu); |
| 261 | cpu_kobj = kobject_create_and_add(cpu_name, modes_kobj); |
| 262 | if (!cpu_kobj) { |
| 263 | pr_err("%s: cannot create %s kobject\n", __func__, cpu_name); |
| 264 | ret = -ENOMEM; |
| 265 | goto mode_sysfs_add_cpu_exit; |
| 266 | } |
| 267 | |
| 268 | for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) { |
| 269 | int idx = MSM_PM_MODE(cpu, i); |
| 270 | |
Praveen Chidambaram | 42da9d2 | 2012-03-30 12:16:34 -0600 | [diff] [blame] | 271 | if ((!msm_pm_sleep_modes[idx].suspend_supported) |
| 272 | && (!msm_pm_sleep_modes[idx].idle_supported)) |
| 273 | continue; |
| 274 | |
| 275 | if (!msm_pm_sleep_mode_labels[i] || |
| 276 | !msm_pm_sleep_mode_labels[i][0]) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 277 | continue; |
| 278 | |
| 279 | mode = kzalloc(sizeof(*mode), GFP_KERNEL); |
| 280 | if (!mode) { |
| 281 | pr_err("%s: cannot allocate memory for attributes\n", |
| 282 | __func__); |
| 283 | ret = -ENOMEM; |
| 284 | goto mode_sysfs_add_cpu_exit; |
| 285 | } |
| 286 | |
| 287 | mode->kobj = kobject_create_and_add( |
| 288 | msm_pm_sleep_mode_labels[i], cpu_kobj); |
| 289 | if (!mode->kobj) { |
| 290 | pr_err("%s: cannot create kobject\n", __func__); |
| 291 | ret = -ENOMEM; |
| 292 | goto mode_sysfs_add_cpu_exit; |
| 293 | } |
| 294 | |
| 295 | for (k = 0, j = 0; k < MSM_PM_MODE_ATTR_NR; k++) { |
| 296 | if ((k == MSM_PM_MODE_ATTR_IDLE) && |
Praveen Chidambaram | 42da9d2 | 2012-03-30 12:16:34 -0600 | [diff] [blame] | 297 | !msm_pm_sleep_modes[idx].idle_supported) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 298 | continue; |
| 299 | if ((k == MSM_PM_MODE_ATTR_SUSPEND) && |
Praveen Chidambaram | 42da9d2 | 2012-03-30 12:16:34 -0600 | [diff] [blame] | 300 | !msm_pm_sleep_modes[idx].suspend_supported) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 301 | continue; |
Stephen Boyd | d2059c3 | 2012-07-03 15:11:15 -0700 | [diff] [blame] | 302 | sysfs_attr_init(&mode->kas[j].ka.attr); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 303 | mode->kas[j].cpu = cpu; |
| 304 | mode->kas[j].ka.attr.mode = 0644; |
| 305 | mode->kas[j].ka.show = msm_pm_mode_attr_show; |
| 306 | mode->kas[j].ka.store = msm_pm_mode_attr_store; |
| 307 | mode->kas[j].ka.attr.name = msm_pm_mode_attr_labels[k]; |
| 308 | mode->attrs[j] = &mode->kas[j].ka.attr; |
| 309 | j++; |
| 310 | } |
| 311 | mode->attrs[j] = NULL; |
| 312 | |
| 313 | mode->attr_group.attrs = mode->attrs; |
| 314 | ret = sysfs_create_group(mode->kobj, &mode->attr_group); |
| 315 | if (ret) { |
| 316 | pr_err("%s: cannot create kobject attribute group\n", |
| 317 | __func__); |
| 318 | goto mode_sysfs_add_cpu_exit; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | ret = 0; |
| 323 | |
| 324 | mode_sysfs_add_cpu_exit: |
Praveen Chidambaram | d5ac2d3 | 2011-10-24 14:30:27 -0600 | [diff] [blame] | 325 | if (ret) { |
Praveen Chidambaram | 2cfda63 | 2011-10-11 16:58:09 -0600 | [diff] [blame] | 326 | if (mode && mode->kobj) |
| 327 | kobject_del(mode->kobj); |
| 328 | kfree(mode); |
| 329 | } |
| 330 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 331 | return ret; |
| 332 | } |
| 333 | |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 334 | int __devinit msm_pm_mode_sysfs_add(void) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 335 | { |
| 336 | struct kobject *module_kobj; |
| 337 | struct kobject *modes_kobj; |
| 338 | unsigned int cpu; |
| 339 | int ret; |
| 340 | |
| 341 | module_kobj = kset_find_obj(module_kset, KBUILD_MODNAME); |
| 342 | if (!module_kobj) { |
| 343 | pr_err("%s: cannot find kobject for module %s\n", |
| 344 | __func__, KBUILD_MODNAME); |
| 345 | ret = -ENOENT; |
| 346 | goto mode_sysfs_add_exit; |
| 347 | } |
| 348 | |
| 349 | modes_kobj = kobject_create_and_add("modes", module_kobj); |
| 350 | if (!modes_kobj) { |
| 351 | pr_err("%s: cannot create modes kobject\n", __func__); |
| 352 | ret = -ENOMEM; |
| 353 | goto mode_sysfs_add_exit; |
| 354 | } |
| 355 | |
| 356 | for_each_possible_cpu(cpu) { |
| 357 | ret = msm_pm_mode_sysfs_add_cpu(cpu, modes_kobj); |
| 358 | if (ret) |
| 359 | goto mode_sysfs_add_exit; |
| 360 | } |
| 361 | |
| 362 | ret = 0; |
| 363 | |
| 364 | mode_sysfs_add_exit: |
| 365 | return ret; |
| 366 | } |
| 367 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 368 | /* |
| 369 | * Configure hardware registers in preparation for Apps power down. |
| 370 | */ |
| 371 | static void msm_pm_config_hw_before_power_down(void) |
| 372 | { |
| 373 | return; |
| 374 | } |
| 375 | |
| 376 | /* |
| 377 | * Clear hardware registers after Apps powers up. |
| 378 | */ |
| 379 | static void msm_pm_config_hw_after_power_up(void) |
| 380 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | /* |
| 384 | * Configure hardware registers in preparation for SWFI. |
| 385 | */ |
| 386 | static void msm_pm_config_hw_before_swfi(void) |
| 387 | { |
| 388 | return; |
| 389 | } |
| 390 | |
Girish Mahadevan | d27ca4a | 2012-08-15 09:21:23 -0600 | [diff] [blame] | 391 | /* |
| 392 | * Configure/Restore hardware registers in preparation for Retention. |
| 393 | */ |
| 394 | |
| 395 | static void msm_pm_config_hw_after_retention(void) |
| 396 | { |
| 397 | int ret; |
Abhijeet Dharmapurikar | e8f8385 | 2013-02-06 18:57:17 -0800 | [diff] [blame] | 398 | |
Girish Mahadevan | d27ca4a | 2012-08-15 09:21:23 -0600 | [diff] [blame] | 399 | ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false); |
| 400 | WARN_ON(ret); |
| 401 | } |
| 402 | |
| 403 | static void msm_pm_config_hw_before_retention(void) |
| 404 | { |
| 405 | return; |
| 406 | } |
| 407 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 408 | /* |
| 409 | * Convert time from nanoseconds to slow clock ticks, then cap it to the |
| 410 | * specified limit |
| 411 | */ |
| 412 | static int64_t msm_pm_convert_and_cap_time(int64_t time_ns, int64_t limit) |
| 413 | { |
| 414 | do_div(time_ns, NSEC_PER_SEC / SCLK_HZ); |
| 415 | return (time_ns > limit) ? limit : time_ns; |
| 416 | } |
| 417 | |
| 418 | /* |
| 419 | * Set the sleep time for suspend. 0 means infinite sleep time. |
| 420 | */ |
| 421 | void msm_pm_set_max_sleep_time(int64_t max_sleep_time_ns) |
| 422 | { |
| 423 | if (max_sleep_time_ns == 0) { |
| 424 | msm_pm_max_sleep_time = 0; |
| 425 | } else { |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 426 | msm_pm_max_sleep_time = |
| 427 | (uint32_t)msm_pm_convert_and_cap_time( |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 428 | max_sleep_time_ns, MSM_PM_SLEEP_TICK_LIMIT); |
| 429 | |
| 430 | if (msm_pm_max_sleep_time == 0) |
| 431 | msm_pm_max_sleep_time = 1; |
| 432 | } |
| 433 | |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 434 | if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 435 | pr_info("%s: Requested %lld ns Giving %u sclk ticks\n", |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 436 | __func__, max_sleep_time_ns, |
| 437 | msm_pm_max_sleep_time); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 438 | } |
| 439 | EXPORT_SYMBOL(msm_pm_set_max_sleep_time); |
| 440 | |
Praveen Chidambaram | c594a09 | 2012-09-18 19:48:29 -0600 | [diff] [blame] | 441 | static void msm_pm_save_cpu_reg(void) |
| 442 | { |
| 443 | int i; |
| 444 | |
| 445 | /* Only on core0 */ |
| 446 | if (smp_processor_id()) |
| 447 | return; |
| 448 | |
| 449 | /** |
| 450 | * On some targets, L2 PC will turn off may reset the core |
| 451 | * configuration for the mux and the default may not make the core |
| 452 | * happy when it resumes. |
| 453 | * Save the active vdd, and set the core vdd to QSB max vdd, so that |
| 454 | * when the core resumes, it is capable of supporting the current QSB |
| 455 | * rate. Then restore the active vdd before switching the acpuclk rate. |
| 456 | */ |
| 457 | if (msm_pm_get_l2_flush_flag() == 1) { |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 458 | cp15_data.active_vdd = msm_spm_get_vdd(0); |
| 459 | for (i = 0; i < cp15_data.reg_saved_state_size; i++) |
| 460 | cp15_data.reg_val[i] = |
| 461 | get_l2_indirect_reg( |
| 462 | cp15_data.reg_data[i]); |
| 463 | msm_spm_set_vdd(0, cp15_data.qsb_pc_vdd); |
Praveen Chidambaram | c594a09 | 2012-09-18 19:48:29 -0600 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | |
| 467 | static void msm_pm_restore_cpu_reg(void) |
| 468 | { |
| 469 | int i; |
| 470 | |
| 471 | /* Only on core0 */ |
| 472 | if (smp_processor_id()) |
| 473 | return; |
| 474 | |
| 475 | if (msm_pm_get_l2_flush_flag() == 1) { |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 476 | for (i = 0; i < cp15_data.reg_saved_state_size; i++) |
| 477 | set_l2_indirect_reg( |
| 478 | cp15_data.reg_data[i], |
| 479 | cp15_data.reg_val[i]); |
| 480 | msm_spm_set_vdd(0, cp15_data.active_vdd); |
Praveen Chidambaram | c594a09 | 2012-09-18 19:48:29 -0600 | [diff] [blame] | 481 | } |
| 482 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 483 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 484 | static void msm_pm_swfi(void) |
| 485 | { |
| 486 | msm_pm_config_hw_before_swfi(); |
| 487 | msm_arch_idle(); |
| 488 | } |
| 489 | |
Praveen Chidambaram | d3d844d | 2012-04-24 09:47:38 -0600 | [diff] [blame] | 490 | static void msm_pm_retention(void) |
| 491 | { |
| 492 | int ret = 0; |
| 493 | |
Girish Mahadevan | d27ca4a | 2012-08-15 09:21:23 -0600 | [diff] [blame] | 494 | msm_pm_config_hw_before_retention(); |
Praveen Chidambaram | d3d844d | 2012-04-24 09:47:38 -0600 | [diff] [blame] | 495 | ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_POWER_RETENTION, false); |
| 496 | WARN_ON(ret); |
Girish Mahadevan | d27ca4a | 2012-08-15 09:21:23 -0600 | [diff] [blame] | 497 | |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 498 | if (msm_pm_retention_calls_tz) |
Girish Mahadevan | d27ca4a | 2012-08-15 09:21:23 -0600 | [diff] [blame] | 499 | scm_call_atomic1(SCM_SVC_BOOT, SCM_CMD_TERMINATE_PC, |
| 500 | SCM_L2_RETENTION); |
| 501 | else |
| 502 | msm_arch_idle(); |
| 503 | |
| 504 | msm_pm_config_hw_after_retention(); |
Praveen Chidambaram | d3d844d | 2012-04-24 09:47:38 -0600 | [diff] [blame] | 505 | } |
| 506 | |
Stephen Boyd | b29750d | 2012-02-21 01:21:32 -0800 | [diff] [blame] | 507 | static bool __ref msm_pm_spm_power_collapse( |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 508 | unsigned int cpu, bool from_idle, bool notify_rpm) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 509 | { |
| 510 | void *entry; |
Maheshkumar Sivasubramanian | dd93ecf | 2011-09-15 19:39:14 -0600 | [diff] [blame] | 511 | bool collapsed = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 512 | int ret; |
| 513 | |
| 514 | if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask) |
| 515 | pr_info("CPU%u: %s: notify_rpm %d\n", |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 516 | cpu, __func__, (int) notify_rpm); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 517 | |
| 518 | ret = msm_spm_set_low_power_mode( |
| 519 | MSM_SPM_MODE_POWER_COLLAPSE, notify_rpm); |
| 520 | WARN_ON(ret); |
| 521 | |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 522 | entry = (!cpu || from_idle) ? |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 523 | msm_pm_collapse_exit : msm_secondary_startup; |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 524 | msm_pm_boot_config_before_pc(cpu, virt_to_phys(entry)); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 525 | |
| 526 | if (MSM_PM_DEBUG_RESET_VECTOR & msm_pm_debug_mask) |
| 527 | pr_info("CPU%u: %s: program vector to %p\n", |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 528 | cpu, __func__, entry); |
Mahesh Sivasubramanian | b7bda88 | 2013-04-08 09:55:48 -0600 | [diff] [blame^] | 529 | if (from_idle && msm_pm_pc_reset_timer) |
| 530 | clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 531 | |
Girish Mahadevan | 5594499 | 2012-10-26 11:03:07 -0600 | [diff] [blame] | 532 | collapsed = msm_pm_collapse(); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 533 | |
Mahesh Sivasubramanian | b7bda88 | 2013-04-08 09:55:48 -0600 | [diff] [blame^] | 534 | if (from_idle && msm_pm_pc_reset_timer) |
| 535 | clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu); |
| 536 | |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 537 | msm_pm_boot_config_after_pc(cpu); |
Maheshkumar Sivasubramanian | 8ccc16e | 2011-10-25 15:59:57 -0600 | [diff] [blame] | 538 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 539 | if (collapsed) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 540 | cpu_init(); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 541 | local_fiq_enable(); |
| 542 | } |
| 543 | |
| 544 | if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask) |
| 545 | pr_info("CPU%u: %s: msm_pm_collapse returned, collapsed %d\n", |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 546 | cpu, __func__, collapsed); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 547 | |
| 548 | ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false); |
| 549 | WARN_ON(ret); |
Maheshkumar Sivasubramanian | dd93ecf | 2011-09-15 19:39:14 -0600 | [diff] [blame] | 550 | return collapsed; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 551 | } |
| 552 | |
Maheshkumar Sivasubramanian | dd93ecf | 2011-09-15 19:39:14 -0600 | [diff] [blame] | 553 | static bool msm_pm_power_collapse_standalone(bool from_idle) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 554 | { |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 555 | unsigned int cpu = smp_processor_id(); |
Praveen Chidambaram | 5e61411 | 2012-11-08 17:53:34 -0700 | [diff] [blame] | 556 | unsigned int avsdscr; |
| 557 | unsigned int avscsr; |
Maheshkumar Sivasubramanian | dd93ecf | 2011-09-15 19:39:14 -0600 | [diff] [blame] | 558 | bool collapsed; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 559 | |
Praveen Chidambaram | 5e61411 | 2012-11-08 17:53:34 -0700 | [diff] [blame] | 560 | avsdscr = avs_get_avsdscr(); |
| 561 | avscsr = avs_get_avscsr(); |
| 562 | avs_set_avscsr(0); /* Disable AVS */ |
| 563 | |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 564 | collapsed = msm_pm_spm_power_collapse(cpu, from_idle, false); |
Praveen Chidambaram | 5e61411 | 2012-11-08 17:53:34 -0700 | [diff] [blame] | 565 | |
| 566 | avs_set_avsdscr(avsdscr); |
| 567 | avs_set_avscsr(avscsr); |
Maheshkumar Sivasubramanian | dd93ecf | 2011-09-15 19:39:14 -0600 | [diff] [blame] | 568 | return collapsed; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 569 | } |
| 570 | |
Maheshkumar Sivasubramanian | dd93ecf | 2011-09-15 19:39:14 -0600 | [diff] [blame] | 571 | static bool msm_pm_power_collapse(bool from_idle) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 572 | { |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 573 | unsigned int cpu = smp_processor_id(); |
Praveen Chidambaram | 4b8df03 | 2013-01-18 16:21:16 -0700 | [diff] [blame] | 574 | unsigned long saved_acpuclk_rate = 0; |
Praveen Chidambaram | 5e61411 | 2012-11-08 17:53:34 -0700 | [diff] [blame] | 575 | unsigned int avsdscr; |
| 576 | unsigned int avscsr; |
Maheshkumar Sivasubramanian | dd93ecf | 2011-09-15 19:39:14 -0600 | [diff] [blame] | 577 | bool collapsed; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 578 | |
| 579 | if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask) |
| 580 | pr_info("CPU%u: %s: idle %d\n", |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 581 | cpu, __func__, (int)from_idle); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 582 | |
| 583 | msm_pm_config_hw_before_power_down(); |
| 584 | if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask) |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 585 | pr_info("CPU%u: %s: pre power down\n", cpu, __func__); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 586 | |
Praveen Chidambaram | 5e61411 | 2012-11-08 17:53:34 -0700 | [diff] [blame] | 587 | avsdscr = avs_get_avsdscr(); |
| 588 | avscsr = avs_get_avscsr(); |
| 589 | avs_set_avscsr(0); /* Disable AVS */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 590 | |
Praveen Chidambaram | 4b8df03 | 2013-01-18 16:21:16 -0700 | [diff] [blame] | 591 | if (cpu_online(cpu) && !msm_no_ramp_down_pc) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 592 | saved_acpuclk_rate = acpuclk_power_collapse(); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 593 | |
| 594 | if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask) |
| 595 | pr_info("CPU%u: %s: change clock rate (old rate = %lu)\n", |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 596 | cpu, __func__, saved_acpuclk_rate); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 597 | |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 598 | if (cp15_data.save_cp15) |
Praveen Chidambaram | c594a09 | 2012-09-18 19:48:29 -0600 | [diff] [blame] | 599 | msm_pm_save_cpu_reg(); |
| 600 | |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 601 | collapsed = msm_pm_spm_power_collapse(cpu, from_idle, true); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 602 | |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 603 | if (cp15_data.save_cp15) |
Praveen Chidambaram | c594a09 | 2012-09-18 19:48:29 -0600 | [diff] [blame] | 604 | msm_pm_restore_cpu_reg(); |
| 605 | |
Girish Mahadevan | 4e025d9 | 2012-02-29 13:26:51 -0700 | [diff] [blame] | 606 | if (cpu_online(cpu)) { |
| 607 | if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask) |
| 608 | pr_info("CPU%u: %s: restore clock rate to %lu\n", |
| 609 | cpu, __func__, saved_acpuclk_rate); |
Praveen Chidambaram | 4b8df03 | 2013-01-18 16:21:16 -0700 | [diff] [blame] | 610 | if (!msm_no_ramp_down_pc && |
| 611 | acpuclk_set_rate(cpu, saved_acpuclk_rate, SETRATE_PC) |
| 612 | < 0) |
Girish Mahadevan | 4e025d9 | 2012-02-29 13:26:51 -0700 | [diff] [blame] | 613 | pr_err("CPU%u: %s: failed to restore clock rate(%lu)\n", |
| 614 | cpu, __func__, saved_acpuclk_rate); |
| 615 | } else { |
| 616 | unsigned int gic_dist_enabled; |
| 617 | unsigned int gic_dist_pending; |
| 618 | gic_dist_enabled = readl_relaxed( |
| 619 | MSM_QGIC_DIST_BASE + GIC_DIST_ENABLE_CLEAR); |
| 620 | gic_dist_pending = readl_relaxed( |
| 621 | MSM_QGIC_DIST_BASE + GIC_DIST_PENDING_SET); |
| 622 | mb(); |
| 623 | gic_dist_pending &= gic_dist_enabled; |
| 624 | |
| 625 | if (gic_dist_pending) |
| 626 | pr_err("CPU %d interrupted during hotplug.Pending int 0x%x\n", |
| 627 | cpu, gic_dist_pending); |
| 628 | } |
| 629 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 630 | |
Praveen Chidambaram | 5e61411 | 2012-11-08 17:53:34 -0700 | [diff] [blame] | 631 | avs_set_avsdscr(avsdscr); |
| 632 | avs_set_avscsr(avscsr); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 633 | msm_pm_config_hw_after_power_up(); |
| 634 | if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask) |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 635 | pr_info("CPU%u: %s: post power up\n", cpu, __func__); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 636 | |
| 637 | if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask) |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 638 | pr_info("CPU%u: %s: return\n", cpu, __func__); |
Maheshkumar Sivasubramanian | dd93ecf | 2011-09-15 19:39:14 -0600 | [diff] [blame] | 639 | return collapsed; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 640 | } |
| 641 | |
Praveen Chidambaram | 192979f | 2012-04-25 18:30:23 -0600 | [diff] [blame] | 642 | static int64_t msm_pm_timer_enter_idle(void) |
| 643 | { |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 644 | if (msm_pm_use_sync_timer) |
Praveen Chidambaram | 192979f | 2012-04-25 18:30:23 -0600 | [diff] [blame] | 645 | return ktime_to_ns(tick_nohz_get_sleep_length()); |
| 646 | |
| 647 | return msm_timer_enter_idle(); |
| 648 | } |
| 649 | |
| 650 | static void msm_pm_timer_exit_idle(bool timer_halted) |
| 651 | { |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 652 | if (msm_pm_use_sync_timer) |
Praveen Chidambaram | 192979f | 2012-04-25 18:30:23 -0600 | [diff] [blame] | 653 | return; |
| 654 | |
| 655 | msm_timer_exit_idle((int) timer_halted); |
| 656 | } |
| 657 | |
Praveen Chidambaram | 3895bde | 2012-05-14 19:42:40 +0530 | [diff] [blame] | 658 | static int64_t msm_pm_timer_enter_suspend(int64_t *period) |
| 659 | { |
Anji Jonnala | c02367a | 2012-07-01 02:56:11 +0530 | [diff] [blame] | 660 | int64_t time = 0; |
Praveen Chidambaram | 3895bde | 2012-05-14 19:42:40 +0530 | [diff] [blame] | 661 | |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 662 | if (msm_pm_use_sync_timer) |
Mahesh Sivasubramanian | 0eaeb30 | 2013-03-14 14:56:32 -0600 | [diff] [blame] | 663 | return sched_clock(); |
Praveen Chidambaram | 3895bde | 2012-05-14 19:42:40 +0530 | [diff] [blame] | 664 | |
| 665 | time = msm_timer_get_sclk_time(period); |
| 666 | if (!time) |
| 667 | pr_err("%s: Unable to read sclk.\n", __func__); |
| 668 | |
| 669 | return time; |
| 670 | } |
| 671 | |
| 672 | static int64_t msm_pm_timer_exit_suspend(int64_t time, int64_t period) |
| 673 | { |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 674 | if (msm_pm_use_sync_timer) |
Mahesh Sivasubramanian | 0eaeb30 | 2013-03-14 14:56:32 -0600 | [diff] [blame] | 675 | return sched_clock() - time; |
Praveen Chidambaram | 3895bde | 2012-05-14 19:42:40 +0530 | [diff] [blame] | 676 | |
| 677 | if (time != 0) { |
| 678 | int64_t end_time = msm_timer_get_sclk_time(NULL); |
| 679 | if (end_time != 0) { |
| 680 | time = end_time - time; |
| 681 | if (time < 0) |
| 682 | time += period; |
| 683 | } else |
| 684 | time = 0; |
| 685 | } |
| 686 | |
| 687 | return time; |
| 688 | } |
| 689 | |
Girish Mahadevan | dc318fd | 2012-08-17 16:48:05 -0600 | [diff] [blame] | 690 | /** |
| 691 | * pm_hrtimer_cb() : Callback function for hrtimer created if the |
| 692 | * core needs to be awake to handle an event. |
| 693 | * @hrtimer : Pointer to hrtimer |
| 694 | */ |
| 695 | static enum hrtimer_restart pm_hrtimer_cb(struct hrtimer *hrtimer) |
| 696 | { |
| 697 | return HRTIMER_NORESTART; |
| 698 | } |
| 699 | |
| 700 | /** |
| 701 | * msm_pm_set_timer() : Set an hrtimer to wakeup the core in time |
| 702 | * to handle an event. |
| 703 | */ |
| 704 | static void msm_pm_set_timer(uint32_t modified_time_us) |
| 705 | { |
| 706 | u64 modified_time_ns = modified_time_us * NSEC_PER_USEC; |
| 707 | ktime_t modified_ktime = ns_to_ktime(modified_time_ns); |
| 708 | pm_hrtimer.function = pm_hrtimer_cb; |
| 709 | hrtimer_start(&pm_hrtimer, modified_ktime, HRTIMER_MODE_ABS); |
| 710 | } |
| 711 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 712 | /****************************************************************************** |
| 713 | * External Idle/Suspend Functions |
| 714 | *****************************************************************************/ |
| 715 | |
| 716 | void arch_idle(void) |
| 717 | { |
| 718 | return; |
| 719 | } |
| 720 | |
Priyanka Mathur | 26b4a4b | 2012-11-05 13:45:45 -0800 | [diff] [blame] | 721 | static inline void msm_pm_ftrace_lpm_enter(unsigned int cpu, |
| 722 | uint32_t latency, uint32_t sleep_us, |
| 723 | uint32_t wake_up, |
| 724 | enum msm_pm_sleep_mode mode) |
| 725 | { |
| 726 | switch (mode) { |
| 727 | case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT: |
| 728 | trace_msm_pm_enter_wfi(cpu, latency, sleep_us, wake_up); |
| 729 | break; |
| 730 | case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE: |
| 731 | trace_msm_pm_enter_spc(cpu, latency, sleep_us, wake_up); |
| 732 | break; |
| 733 | case MSM_PM_SLEEP_MODE_POWER_COLLAPSE: |
| 734 | trace_msm_pm_enter_pc(cpu, latency, sleep_us, wake_up); |
| 735 | break; |
| 736 | case MSM_PM_SLEEP_MODE_RETENTION: |
| 737 | trace_msm_pm_enter_ret(cpu, latency, sleep_us, wake_up); |
| 738 | break; |
| 739 | default: |
| 740 | break; |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | static inline void msm_pm_ftrace_lpm_exit(unsigned int cpu, |
| 745 | enum msm_pm_sleep_mode mode, |
| 746 | bool success) |
| 747 | { |
| 748 | switch (mode) { |
| 749 | case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT: |
| 750 | trace_msm_pm_exit_wfi(cpu, success); |
| 751 | break; |
| 752 | case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE: |
| 753 | trace_msm_pm_exit_spc(cpu, success); |
| 754 | break; |
| 755 | case MSM_PM_SLEEP_MODE_POWER_COLLAPSE: |
| 756 | trace_msm_pm_exit_pc(cpu, success); |
| 757 | break; |
| 758 | case MSM_PM_SLEEP_MODE_RETENTION: |
| 759 | trace_msm_pm_exit_ret(cpu, success); |
| 760 | break; |
| 761 | default: |
| 762 | break; |
| 763 | } |
| 764 | } |
| 765 | |
Priyanka Mathur | 92fe575 | 2013-01-17 10:58:04 -0800 | [diff] [blame] | 766 | static int msm_pm_idle_prepare(struct cpuidle_device *dev, |
| 767 | struct cpuidle_driver *drv, int index, |
| 768 | void **msm_pm_idle_rs_limits) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 769 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 770 | int i; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 771 | unsigned int power_usage = -1; |
Priyanka Mathur | 848bb4c | 2012-11-30 18:04:57 -0800 | [diff] [blame] | 772 | int ret = MSM_PM_SLEEP_MODE_NOT_SELECTED; |
Girish Mahadevan | dc318fd | 2012-08-17 16:48:05 -0600 | [diff] [blame] | 773 | uint32_t modified_time_us = 0; |
| 774 | struct msm_pm_time_params time_param; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 775 | |
Girish Mahadevan | dc318fd | 2012-08-17 16:48:05 -0600 | [diff] [blame] | 776 | time_param.latency_us = |
| 777 | (uint32_t) pm_qos_request(PM_QOS_CPU_DMA_LATENCY); |
| 778 | time_param.sleep_us = |
| 779 | (uint32_t) (ktime_to_us(tick_nohz_get_sleep_length()) |
| 780 | & UINT_MAX); |
| 781 | time_param.modified_time_us = 0; |
| 782 | |
| 783 | if (!dev->cpu) |
| 784 | time_param.next_event_us = |
| 785 | (uint32_t) (ktime_to_us(get_next_event_time()) |
| 786 | & UINT_MAX); |
| 787 | else |
| 788 | time_param.next_event_us = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 789 | |
| 790 | for (i = 0; i < dev->state_count; i++) { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 791 | struct cpuidle_state *state = &drv->states[i]; |
| 792 | struct cpuidle_state_usage *st_usage = &dev->states_usage[i]; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 793 | enum msm_pm_sleep_mode mode; |
| 794 | bool allow; |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 795 | uint32_t power; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 796 | int idx; |
Priyanka Mathur | 92fe575 | 2013-01-17 10:58:04 -0800 | [diff] [blame] | 797 | void *rs_limits = NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 798 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 799 | mode = (enum msm_pm_sleep_mode) cpuidle_get_statedata(st_usage); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 800 | idx = MSM_PM_MODE(dev->cpu, mode); |
| 801 | |
Praveen Chidambaram | 42da9d2 | 2012-03-30 12:16:34 -0600 | [diff] [blame] | 802 | allow = msm_pm_sleep_modes[idx].idle_enabled && |
| 803 | msm_pm_sleep_modes[idx].idle_supported; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 804 | |
| 805 | switch (mode) { |
| 806 | case MSM_PM_SLEEP_MODE_POWER_COLLAPSE: |
Mahesh Sivasubramanian | 87f579c | 2013-02-04 11:59:44 -0700 | [diff] [blame] | 807 | if (num_online_cpus() > 1) |
Priyanka Mathur | 68a9bc5 | 2012-10-03 15:15:01 -0700 | [diff] [blame] | 808 | allow = false; |
Mahesh Sivasubramanian | 87f579c | 2013-02-04 11:59:44 -0700 | [diff] [blame] | 809 | break; |
Mahesh Sivasubramanian | ba7e01c | 2012-09-04 18:22:51 -0600 | [diff] [blame] | 810 | case MSM_PM_SLEEP_MODE_RETENTION: |
Mahesh Sivasubramanian | 1b8601b | 2012-12-20 14:11:23 -0700 | [diff] [blame] | 811 | /* |
| 812 | * The Krait BHS regulator doesn't have enough head |
| 813 | * room to drive the retention voltage on LDO and so |
| 814 | * has disabled retention |
| 815 | */ |
| 816 | if (!msm_pm_ldo_retention_enabled) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 817 | allow = false; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 818 | |
Mahesh Sivasubramanian | 87f579c | 2013-02-04 11:59:44 -0700 | [diff] [blame] | 819 | if (msm_pm_retention_calls_tz && num_online_cpus() > 1) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 820 | allow = false; |
| 821 | break; |
Mahesh Sivasubramanian | 87f579c | 2013-02-04 11:59:44 -0700 | [diff] [blame] | 822 | case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE: |
| 823 | case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT: |
| 824 | break; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 825 | default: |
| 826 | allow = false; |
| 827 | break; |
| 828 | } |
| 829 | |
Mahesh Sivasubramanian | 87f579c | 2013-02-04 11:59:44 -0700 | [diff] [blame] | 830 | if (!allow) |
| 831 | continue; |
| 832 | |
| 833 | if (pm_sleep_ops.lowest_limits) |
| 834 | rs_limits = pm_sleep_ops.lowest_limits(true, |
| 835 | mode, &time_param, &power); |
| 836 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 837 | if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask) |
Mahesh Sivasubramanian | 87f579c | 2013-02-04 11:59:44 -0700 | [diff] [blame] | 838 | pr_info("CPU%u:%s:%s, latency %uus, slp %uus, lim %p\n", |
| 839 | dev->cpu, __func__, state->desc, |
| 840 | time_param.latency_us, |
| 841 | time_param.sleep_us, rs_limits); |
| 842 | if (!rs_limits) |
| 843 | continue; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 844 | |
Mahesh Sivasubramanian | 87f579c | 2013-02-04 11:59:44 -0700 | [diff] [blame] | 845 | if (power < power_usage) { |
| 846 | power_usage = power; |
| 847 | modified_time_us = time_param.modified_time_us; |
| 848 | ret = mode; |
Priyanka Mathur | 92fe575 | 2013-01-17 10:58:04 -0800 | [diff] [blame] | 849 | *msm_pm_idle_rs_limits = rs_limits; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 850 | } |
Mahesh Sivasubramanian | 87f579c | 2013-02-04 11:59:44 -0700 | [diff] [blame] | 851 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 852 | } |
| 853 | |
Girish Mahadevan | dc318fd | 2012-08-17 16:48:05 -0600 | [diff] [blame] | 854 | if (modified_time_us && !dev->cpu) |
| 855 | msm_pm_set_timer(modified_time_us); |
Priyanka Mathur | 26b4a4b | 2012-11-05 13:45:45 -0800 | [diff] [blame] | 856 | |
| 857 | msm_pm_ftrace_lpm_enter(dev->cpu, time_param.latency_us, |
| 858 | time_param.sleep_us, time_param.next_event_us, |
| 859 | ret); |
| 860 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 861 | return ret; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 862 | } |
| 863 | |
Priyanka Mathur | 92fe575 | 2013-01-17 10:58:04 -0800 | [diff] [blame] | 864 | enum msm_pm_sleep_mode msm_pm_idle_enter(struct cpuidle_device *dev, |
| 865 | struct cpuidle_driver *drv, int index) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 866 | { |
| 867 | int64_t time; |
Priyanka Mathur | 26b4a4b | 2012-11-05 13:45:45 -0800 | [diff] [blame] | 868 | bool collapsed = 1; |
Priyanka Mathur | 92fe575 | 2013-01-17 10:58:04 -0800 | [diff] [blame] | 869 | int exit_stat = -1; |
| 870 | enum msm_pm_sleep_mode sleep_mode; |
| 871 | void *msm_pm_idle_rs_limits = NULL; |
| 872 | int sleep_delay = 1; |
| 873 | int ret = -ENODEV; |
| 874 | int64_t timer_expiration = 0; |
| 875 | int notify_rpm = false; |
| 876 | bool timer_halted = false; |
| 877 | |
| 878 | sleep_mode = msm_pm_idle_prepare(dev, drv, index, |
| 879 | &msm_pm_idle_rs_limits); |
| 880 | |
| 881 | if (!msm_pm_idle_rs_limits) { |
| 882 | sleep_mode = MSM_PM_SLEEP_MODE_NOT_SELECTED; |
| 883 | goto cpuidle_enter_bail; |
| 884 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 885 | |
| 886 | if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask) |
| 887 | pr_info("CPU%u: %s: mode %d\n", |
| 888 | smp_processor_id(), __func__, sleep_mode); |
| 889 | |
| 890 | time = ktime_to_ns(ktime_get()); |
| 891 | |
Priyanka Mathur | 92fe575 | 2013-01-17 10:58:04 -0800 | [diff] [blame] | 892 | if (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE) { |
| 893 | notify_rpm = true; |
| 894 | timer_expiration = msm_pm_timer_enter_idle(); |
| 895 | |
| 896 | sleep_delay = (uint32_t) msm_pm_convert_and_cap_time( |
| 897 | timer_expiration, MSM_PM_SLEEP_TICK_LIMIT); |
| 898 | if (sleep_delay == 0) /* 0 would mean infinite time */ |
| 899 | sleep_delay = 1; |
| 900 | } |
| 901 | |
| 902 | if (pm_sleep_ops.enter_sleep) |
| 903 | ret = pm_sleep_ops.enter_sleep(sleep_delay, |
| 904 | msm_pm_idle_rs_limits, true, notify_rpm); |
| 905 | if (ret) |
| 906 | goto cpuidle_enter_bail; |
| 907 | |
Mahesh Sivasubramanian | 95a48e7 | 2013-02-15 14:23:26 -0700 | [diff] [blame] | 908 | switch (sleep_mode) { |
| 909 | case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT: |
| 910 | msm_pm_swfi(); |
| 911 | exit_stat = MSM_PM_STAT_IDLE_WFI; |
| 912 | break; |
| 913 | |
| 914 | case MSM_PM_SLEEP_MODE_RETENTION: |
| 915 | msm_pm_retention(); |
| 916 | exit_stat = MSM_PM_STAT_RETENTION; |
| 917 | break; |
| 918 | |
| 919 | case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE: |
| 920 | collapsed = msm_pm_power_collapse_standalone(true); |
| 921 | exit_stat = MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE; |
| 922 | break; |
| 923 | |
Priyanka Mathur | 92fe575 | 2013-01-17 10:58:04 -0800 | [diff] [blame] | 924 | case MSM_PM_SLEEP_MODE_POWER_COLLAPSE: |
Mahesh Sivasubramanian | 95a48e7 | 2013-02-15 14:23:26 -0700 | [diff] [blame] | 925 | if (MSM_PM_DEBUG_IDLE_CLK & msm_pm_debug_mask) |
| 926 | clock_debug_print_enabled(); |
Karthik Parsha | 6fb932d | 2012-01-24 18:04:12 -0800 | [diff] [blame] | 927 | |
Priyanka Mathur | 92fe575 | 2013-01-17 10:58:04 -0800 | [diff] [blame] | 928 | collapsed = msm_pm_power_collapse(true); |
| 929 | timer_halted = true; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 930 | |
Mahesh Sivasubramanian | 95a48e7 | 2013-02-15 14:23:26 -0700 | [diff] [blame] | 931 | exit_stat = MSM_PM_STAT_IDLE_POWER_COLLAPSE; |
Priyanka Mathur | 92fe575 | 2013-01-17 10:58:04 -0800 | [diff] [blame] | 932 | msm_pm_timer_exit_idle(timer_halted); |
Mahesh Sivasubramanian | 95a48e7 | 2013-02-15 14:23:26 -0700 | [diff] [blame] | 933 | break; |
Mahesh Sivasubramanian | 95a48e7 | 2013-02-15 14:23:26 -0700 | [diff] [blame] | 934 | |
| 935 | case MSM_PM_SLEEP_MODE_NOT_SELECTED: |
| 936 | goto cpuidle_enter_bail; |
| 937 | break; |
| 938 | |
| 939 | default: |
| 940 | __WARN(); |
| 941 | goto cpuidle_enter_bail; |
| 942 | break; |
| 943 | } |
| 944 | |
Priyanka Mathur | 92fe575 | 2013-01-17 10:58:04 -0800 | [diff] [blame] | 945 | if (pm_sleep_ops.exit_sleep) |
| 946 | pm_sleep_ops.exit_sleep(msm_pm_idle_rs_limits, true, |
| 947 | notify_rpm, collapsed); |
Mahesh Sivasubramanian | 95a48e7 | 2013-02-15 14:23:26 -0700 | [diff] [blame] | 948 | |
Priyanka Mathur | 92fe575 | 2013-01-17 10:58:04 -0800 | [diff] [blame] | 949 | time = ktime_to_ns(ktime_get()) - time; |
| 950 | msm_pm_ftrace_lpm_exit(smp_processor_id(), sleep_mode, collapsed); |
| 951 | if (exit_stat >= 0) |
| 952 | msm_pm_add_stat(exit_stat, time); |
Mahesh Sivasubramanian | 95a48e7 | 2013-02-15 14:23:26 -0700 | [diff] [blame] | 953 | do_div(time, 1000); |
Priyanka Mathur | 92fe575 | 2013-01-17 10:58:04 -0800 | [diff] [blame] | 954 | dev->last_residency = (int) time; |
| 955 | return sleep_mode; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 956 | |
| 957 | cpuidle_enter_bail: |
Priyanka Mathur | 92fe575 | 2013-01-17 10:58:04 -0800 | [diff] [blame] | 958 | dev->last_residency = 0; |
| 959 | if (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE) |
| 960 | msm_pm_timer_exit_idle(timer_halted); |
| 961 | sleep_mode = MSM_PM_SLEEP_MODE_NOT_SELECTED; |
| 962 | return sleep_mode; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 963 | } |
| 964 | |
Anji Jonnala | 02dac8d | 2013-03-06 21:31:04 +0530 | [diff] [blame] | 965 | int msm_pm_wait_cpu_shutdown(unsigned int cpu) |
| 966 | { |
| 967 | int timeout = 10; |
| 968 | |
| 969 | if (!msm_pm_slp_sts) |
| 970 | return 0; |
| 971 | if (!msm_pm_slp_sts[cpu].base_addr) |
| 972 | return 0; |
| 973 | while (timeout--) { |
| 974 | /* |
| 975 | * Check for the SPM of the core being hotplugged to set |
| 976 | * its sleep state.The SPM sleep state indicates that the |
| 977 | * core has been power collapsed. |
| 978 | */ |
| 979 | int acc_sts = __raw_readl(msm_pm_slp_sts[cpu].base_addr); |
| 980 | |
| 981 | if (acc_sts & msm_pm_slp_sts[cpu].mask) |
| 982 | return 0; |
Mahesh Sivasubramanian | 65187f9 | 2013-03-19 15:14:06 -0600 | [diff] [blame] | 983 | udelay(100); |
Anji Jonnala | 02dac8d | 2013-03-06 21:31:04 +0530 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | pr_info("%s(): Timed out waiting for CPU %u SPM to enter sleep state", |
| 987 | __func__, cpu); |
| 988 | return -EBUSY; |
| 989 | } |
| 990 | |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 991 | void msm_pm_cpu_enter_lowpower(unsigned int cpu) |
| 992 | { |
| 993 | int i; |
| 994 | bool allow[MSM_PM_SLEEP_MODE_NR]; |
| 995 | |
| 996 | for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) { |
| 997 | struct msm_pm_platform_data *mode; |
| 998 | |
Praveen Chidambaram | 42da9d2 | 2012-03-30 12:16:34 -0600 | [diff] [blame] | 999 | mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)]; |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 1000 | allow[i] = mode->suspend_supported && mode->suspend_enabled; |
| 1001 | } |
| 1002 | |
| 1003 | if (MSM_PM_DEBUG_HOTPLUG & msm_pm_debug_mask) |
| 1004 | pr_notice("CPU%u: %s: shutting down cpu\n", cpu, __func__); |
| 1005 | |
Matt Wagantall | 5375aa7 | 2012-07-02 19:59:51 -0700 | [diff] [blame] | 1006 | if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 1007 | msm_pm_power_collapse(false); |
Matt Wagantall | 5375aa7 | 2012-07-02 19:59:51 -0700 | [diff] [blame] | 1008 | else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 1009 | msm_pm_power_collapse_standalone(false); |
Matt Wagantall | 5375aa7 | 2012-07-02 19:59:51 -0700 | [diff] [blame] | 1010 | else if (allow[MSM_PM_SLEEP_MODE_RETENTION]) |
Praveen Chidambaram | d3d844d | 2012-04-24 09:47:38 -0600 | [diff] [blame] | 1011 | msm_pm_retention(); |
Stephen Boyd | bda7427 | 2012-08-09 14:01:27 -0700 | [diff] [blame] | 1012 | else |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 1013 | msm_pm_swfi(); |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 1014 | } |
| 1015 | |
Mahesh Sivasubramanian | 1b8601b | 2012-12-20 14:11:23 -0700 | [diff] [blame] | 1016 | static void msm_pm_ack_retention_disable(void *data) |
| 1017 | { |
| 1018 | /* |
| 1019 | * This is a NULL function to ensure that the core has woken up |
| 1020 | * and is safe to disable retention. |
| 1021 | */ |
| 1022 | } |
| 1023 | /** |
| 1024 | * msm_pm_enable_retention() - Disable/Enable retention on all cores |
| 1025 | * @enable: Enable/Disable retention |
| 1026 | * |
| 1027 | */ |
| 1028 | void msm_pm_enable_retention(bool enable) |
| 1029 | { |
Matt Wagantall | d06f5c2 | 2013-03-14 19:31:14 -0700 | [diff] [blame] | 1030 | if (enable == msm_pm_ldo_retention_enabled) |
| 1031 | return; |
| 1032 | |
Mahesh Sivasubramanian | 1b8601b | 2012-12-20 14:11:23 -0700 | [diff] [blame] | 1033 | msm_pm_ldo_retention_enabled = enable; |
| 1034 | /* |
| 1035 | * If retention is being disabled, wakeup all online core to ensure |
| 1036 | * that it isn't executing retention. Offlined cores need not be woken |
| 1037 | * up as they enter the deepest sleep mode, namely RPM assited power |
| 1038 | * collapse |
| 1039 | */ |
Mahesh Sivasubramanian | 96f67d1 | 2013-03-14 22:44:42 -0600 | [diff] [blame] | 1040 | if (!enable) { |
| 1041 | preempt_disable(); |
Mahesh Sivasubramanian | 1b8601b | 2012-12-20 14:11:23 -0700 | [diff] [blame] | 1042 | smp_call_function_many(cpu_online_mask, |
| 1043 | msm_pm_ack_retention_disable, |
| 1044 | NULL, true); |
Mahesh Sivasubramanian | 96f67d1 | 2013-03-14 22:44:42 -0600 | [diff] [blame] | 1045 | preempt_enable(); |
| 1046 | |
| 1047 | |
| 1048 | } |
Mahesh Sivasubramanian | 1b8601b | 2012-12-20 14:11:23 -0700 | [diff] [blame] | 1049 | } |
| 1050 | EXPORT_SYMBOL(msm_pm_enable_retention); |
| 1051 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1052 | static int msm_pm_enter(suspend_state_t state) |
| 1053 | { |
| 1054 | bool allow[MSM_PM_SLEEP_MODE_NR]; |
| 1055 | int i; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1056 | int64_t period = 0; |
Praveen Chidambaram | 3895bde | 2012-05-14 19:42:40 +0530 | [diff] [blame] | 1057 | int64_t time = msm_pm_timer_enter_suspend(&period); |
Girish Mahadevan | dc318fd | 2012-08-17 16:48:05 -0600 | [diff] [blame] | 1058 | struct msm_pm_time_params time_param; |
| 1059 | |
| 1060 | time_param.latency_us = -1; |
| 1061 | time_param.sleep_us = -1; |
| 1062 | time_param.next_event_us = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1063 | |
| 1064 | if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask) |
| 1065 | pr_info("%s\n", __func__); |
| 1066 | |
| 1067 | if (smp_processor_id()) { |
| 1068 | __WARN(); |
| 1069 | goto enter_exit; |
| 1070 | } |
| 1071 | |
| 1072 | |
| 1073 | for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) { |
| 1074 | struct msm_pm_platform_data *mode; |
| 1075 | |
Praveen Chidambaram | 42da9d2 | 2012-03-30 12:16:34 -0600 | [diff] [blame] | 1076 | mode = &msm_pm_sleep_modes[MSM_PM_MODE(0, i)]; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1077 | allow[i] = mode->suspend_supported && mode->suspend_enabled; |
| 1078 | } |
| 1079 | |
| 1080 | if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) { |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 1081 | void *rs_limits = NULL; |
| 1082 | int ret = -ENODEV; |
| 1083 | uint32_t power; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1084 | |
| 1085 | if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask) |
| 1086 | pr_info("%s: power collapse\n", __func__); |
| 1087 | |
| 1088 | clock_debug_print_enabled(); |
| 1089 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1090 | if (msm_pm_sleep_time_override > 0) { |
| 1091 | int64_t ns = NSEC_PER_SEC * |
| 1092 | (int64_t) msm_pm_sleep_time_override; |
| 1093 | msm_pm_set_max_sleep_time(ns); |
| 1094 | msm_pm_sleep_time_override = 0; |
| 1095 | } |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 1096 | |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 1097 | if (pm_sleep_ops.lowest_limits) |
| 1098 | rs_limits = pm_sleep_ops.lowest_limits(false, |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 1099 | MSM_PM_SLEEP_MODE_POWER_COLLAPSE, &time_param, &power); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1100 | |
| 1101 | if (rs_limits) { |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 1102 | if (pm_sleep_ops.enter_sleep) |
| 1103 | ret = pm_sleep_ops.enter_sleep( |
| 1104 | msm_pm_max_sleep_time, |
| 1105 | rs_limits, false, true); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1106 | if (!ret) { |
Maheshkumar Sivasubramanian | dd93ecf | 2011-09-15 19:39:14 -0600 | [diff] [blame] | 1107 | int collapsed = msm_pm_power_collapse(false); |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 1108 | if (pm_sleep_ops.exit_sleep) { |
| 1109 | pm_sleep_ops.exit_sleep(rs_limits, |
| 1110 | false, true, collapsed); |
| 1111 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1112 | } |
| 1113 | } else { |
| 1114 | pr_err("%s: cannot find the lowest power limit\n", |
| 1115 | __func__); |
| 1116 | } |
Praveen Chidambaram | 3895bde | 2012-05-14 19:42:40 +0530 | [diff] [blame] | 1117 | time = msm_pm_timer_exit_suspend(time, period); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1118 | msm_pm_add_stat(MSM_PM_STAT_SUSPEND, time); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1119 | } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) { |
| 1120 | if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask) |
| 1121 | pr_info("%s: standalone power collapse\n", __func__); |
| 1122 | msm_pm_power_collapse_standalone(false); |
Praveen Chidambaram | d3d844d | 2012-04-24 09:47:38 -0600 | [diff] [blame] | 1123 | } else if (allow[MSM_PM_SLEEP_MODE_RETENTION]) { |
| 1124 | if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask) |
| 1125 | pr_info("%s: retention\n", __func__); |
| 1126 | msm_pm_retention(); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1127 | } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) { |
| 1128 | if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask) |
| 1129 | pr_info("%s: swfi\n", __func__); |
| 1130 | msm_pm_swfi(); |
| 1131 | } |
| 1132 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1133 | enter_exit: |
| 1134 | if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask) |
| 1135 | pr_info("%s: return\n", __func__); |
| 1136 | |
| 1137 | return 0; |
| 1138 | } |
| 1139 | |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 1140 | void msm_pm_set_sleep_ops(struct msm_pm_sleep_ops *ops) |
| 1141 | { |
| 1142 | if (ops) |
| 1143 | pm_sleep_ops = *ops; |
| 1144 | } |
| 1145 | |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 1146 | static const struct platform_suspend_ops msm_pm_ops = { |
| 1147 | .enter = msm_pm_enter, |
| 1148 | .valid = suspend_valid_only_mem, |
Mahesh Sivasubramanian | cb39662 | 2012-03-14 14:50:37 -0600 | [diff] [blame] | 1149 | }; |
Anji Jonnala | 02dac8d | 2013-03-06 21:31:04 +0530 | [diff] [blame] | 1150 | static int __devinit msm_cpu_status_probe(struct platform_device *pdev) |
| 1151 | { |
| 1152 | struct msm_pm_sleep_status_data *pdata; |
| 1153 | char *key; |
| 1154 | u32 cpu; |
| 1155 | |
| 1156 | if (!pdev) |
| 1157 | return -EFAULT; |
| 1158 | |
| 1159 | msm_pm_slp_sts = |
| 1160 | kzalloc(sizeof(*msm_pm_slp_sts) * num_possible_cpus(), |
| 1161 | GFP_KERNEL); |
| 1162 | |
| 1163 | if (!msm_pm_slp_sts) |
| 1164 | return -ENOMEM; |
| 1165 | |
| 1166 | if (pdev->dev.of_node) { |
| 1167 | struct resource *res; |
| 1168 | u32 offset; |
| 1169 | int rc; |
| 1170 | u32 mask; |
| 1171 | |
| 1172 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1173 | if (!res) |
| 1174 | goto fail_free_mem; |
| 1175 | |
| 1176 | key = "qcom,cpu-alias-addr"; |
| 1177 | rc = of_property_read_u32(pdev->dev.of_node, key, &offset); |
| 1178 | |
| 1179 | if (rc) |
| 1180 | goto fail_free_mem; |
| 1181 | |
| 1182 | key = "qcom,sleep-status-mask"; |
| 1183 | rc = of_property_read_u32(pdev->dev.of_node, key, |
| 1184 | &mask); |
| 1185 | if (rc) |
| 1186 | goto fail_free_mem; |
| 1187 | |
| 1188 | for_each_possible_cpu(cpu) { |
| 1189 | msm_pm_slp_sts[cpu].base_addr = |
| 1190 | ioremap(res->start + cpu * offset, |
| 1191 | resource_size(res)); |
| 1192 | msm_pm_slp_sts[cpu].mask = mask; |
| 1193 | |
| 1194 | if (!msm_pm_slp_sts[cpu].base_addr) |
| 1195 | goto failed_of_node; |
| 1196 | } |
| 1197 | |
| 1198 | } else { |
| 1199 | pdata = pdev->dev.platform_data; |
| 1200 | if (!pdev->dev.platform_data) |
| 1201 | goto fail_free_mem; |
| 1202 | |
| 1203 | for_each_possible_cpu(cpu) { |
| 1204 | msm_pm_slp_sts[cpu].base_addr = |
| 1205 | pdata->base_addr + cpu * pdata->cpu_offset; |
| 1206 | msm_pm_slp_sts[cpu].mask = pdata->mask; |
| 1207 | } |
| 1208 | } |
| 1209 | |
| 1210 | return 0; |
| 1211 | |
| 1212 | failed_of_node: |
| 1213 | pr_info("%s(): Failed to key=%s\n", __func__, key); |
| 1214 | for_each_possible_cpu(cpu) { |
| 1215 | if (msm_pm_slp_sts[cpu].base_addr) |
| 1216 | iounmap(msm_pm_slp_sts[cpu].base_addr); |
| 1217 | } |
| 1218 | fail_free_mem: |
| 1219 | kfree(msm_pm_slp_sts); |
| 1220 | return -EINVAL; |
| 1221 | |
| 1222 | }; |
| 1223 | |
| 1224 | static struct of_device_id msm_slp_sts_match_tbl[] = { |
| 1225 | {.compatible = "qcom,cpu-sleep-status"}, |
| 1226 | {}, |
| 1227 | }; |
| 1228 | |
| 1229 | static struct platform_driver msm_cpu_status_driver = { |
| 1230 | .probe = msm_cpu_status_probe, |
| 1231 | .driver = { |
| 1232 | .name = "cpu_slp_status", |
| 1233 | .owner = THIS_MODULE, |
| 1234 | .of_match_table = msm_slp_sts_match_tbl, |
| 1235 | }, |
| 1236 | }; |
Mahesh Sivasubramanian | cb39662 | 2012-03-14 14:50:37 -0600 | [diff] [blame] | 1237 | |
Praveen Chidambaram | ede45d0 | 2013-02-20 17:45:21 -0700 | [diff] [blame] | 1238 | static int __init msm_pm_setup_saved_state(void) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1239 | { |
| 1240 | pgd_t *pc_pgd; |
| 1241 | pmd_t *pmd; |
| 1242 | unsigned long pmdval; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1243 | unsigned long exit_phys; |
Praveen Chidambaram | 3895bde | 2012-05-14 19:42:40 +0530 | [diff] [blame] | 1244 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1245 | /* Page table for cores to come back up safely. */ |
| 1246 | pc_pgd = pgd_alloc(&init_mm); |
| 1247 | if (!pc_pgd) |
| 1248 | return -ENOMEM; |
| 1249 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1250 | exit_phys = virt_to_phys(msm_pm_collapse_exit); |
| 1251 | |
| 1252 | pmd = pmd_offset(pud_offset(pc_pgd + pgd_index(exit_phys),exit_phys), |
| 1253 | exit_phys); |
| 1254 | pmdval = (exit_phys & PGDIR_MASK) | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1255 | PMD_TYPE_SECT | PMD_SECT_AP_WRITE; |
| 1256 | pmd[0] = __pmd(pmdval); |
| 1257 | pmd[1] = __pmd(pmdval + (1 << (PGDIR_SHIFT - 1))); |
| 1258 | |
Steve Muckle | fcece05 | 2012-02-18 20:09:58 -0800 | [diff] [blame] | 1259 | msm_saved_state_phys = |
| 1260 | allocate_contiguous_ebi_nomap(CPU_SAVED_STATE_SIZE * |
| 1261 | num_possible_cpus(), 4); |
| 1262 | if (!msm_saved_state_phys) |
| 1263 | return -ENOMEM; |
| 1264 | msm_saved_state = ioremap_nocache(msm_saved_state_phys, |
| 1265 | CPU_SAVED_STATE_SIZE * |
| 1266 | num_possible_cpus()); |
| 1267 | if (!msm_saved_state) |
| 1268 | return -ENOMEM; |
| 1269 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1270 | /* It is remotely possible that the code in msm_pm_collapse_exit() |
| 1271 | * which turns on the MMU with this mapping is in the |
| 1272 | * next even-numbered megabyte beyond the |
| 1273 | * start of msm_pm_collapse_exit(). |
| 1274 | * Map this megabyte in as well. |
| 1275 | */ |
| 1276 | pmd[2] = __pmd(pmdval + (2 << (PGDIR_SHIFT - 1))); |
| 1277 | flush_pmd_entry(pmd); |
| 1278 | msm_pm_pc_pgd = virt_to_phys(pc_pgd); |
Steve Muckle | 730ad7a | 2012-02-21 15:26:37 -0800 | [diff] [blame] | 1279 | clean_caches((unsigned long)&msm_pm_pc_pgd, sizeof(msm_pm_pc_pgd), |
| 1280 | virt_to_phys(&msm_pm_pc_pgd)); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1281 | |
Praveen Chidambaram | ede45d0 | 2013-02-20 17:45:21 -0700 | [diff] [blame] | 1282 | return 0; |
| 1283 | } |
| 1284 | core_initcall(msm_pm_setup_saved_state); |
| 1285 | |
Mahesh Sivasubramanian | b7bda88 | 2013-04-08 09:55:48 -0600 | [diff] [blame^] | 1286 | static void setup_broadcast_timer(void *arg) |
| 1287 | { |
| 1288 | unsigned long reason = (unsigned long)arg; |
| 1289 | int cpu = smp_processor_id(); |
| 1290 | |
| 1291 | reason = reason ? |
| 1292 | CLOCK_EVT_NOTIFY_BROADCAST_ON : CLOCK_EVT_NOTIFY_BROADCAST_OFF; |
| 1293 | |
| 1294 | clockevents_notify(reason, &cpu); |
| 1295 | } |
| 1296 | |
| 1297 | static int setup_broadcast_cpuhp_notify(struct notifier_block *n, |
| 1298 | unsigned long action, void *hcpu) |
| 1299 | { |
| 1300 | int hotcpu = (unsigned long)hcpu; |
| 1301 | |
| 1302 | switch (action & ~CPU_TASKS_FROZEN) { |
| 1303 | case CPU_ONLINE: |
| 1304 | smp_call_function_single(hotcpu, setup_broadcast_timer, |
| 1305 | (void *)true, 1); |
| 1306 | break; |
| 1307 | } |
| 1308 | |
| 1309 | return NOTIFY_OK; |
| 1310 | } |
| 1311 | |
| 1312 | static struct notifier_block setup_broadcast_notifier = { |
| 1313 | .notifier_call = setup_broadcast_cpuhp_notify, |
| 1314 | }; |
| 1315 | |
Praveen Chidambaram | ede45d0 | 2013-02-20 17:45:21 -0700 | [diff] [blame] | 1316 | static int __init msm_pm_init(void) |
| 1317 | { |
| 1318 | enum msm_pm_time_stats_id enable_stats[] = { |
| 1319 | MSM_PM_STAT_IDLE_WFI, |
| 1320 | MSM_PM_STAT_RETENTION, |
| 1321 | MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE, |
| 1322 | MSM_PM_STAT_IDLE_POWER_COLLAPSE, |
| 1323 | MSM_PM_STAT_SUSPEND, |
| 1324 | }; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1325 | msm_pm_mode_sysfs_add(); |
Praveen Chidambaram | 3895bde | 2012-05-14 19:42:40 +0530 | [diff] [blame] | 1326 | msm_pm_add_stats(enable_stats, ARRAY_SIZE(enable_stats)); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1327 | suspend_set_ops(&msm_pm_ops); |
Girish Mahadevan | dc318fd | 2012-08-17 16:48:05 -0600 | [diff] [blame] | 1328 | hrtimer_init(&pm_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1329 | msm_cpuidle_init(); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1330 | |
Mahesh Sivasubramanian | b7bda88 | 2013-04-08 09:55:48 -0600 | [diff] [blame^] | 1331 | if (msm_pm_pc_reset_timer) { |
| 1332 | get_cpu(); |
| 1333 | smp_call_function_many(cpu_online_mask, setup_broadcast_timer, |
| 1334 | (void *)true, 1); |
| 1335 | put_cpu(); |
| 1336 | register_cpu_notifier(&setup_broadcast_notifier); |
| 1337 | } |
| 1338 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1339 | return 0; |
| 1340 | } |
| 1341 | |
Girish Mahadevan | 5594499 | 2012-10-26 11:03:07 -0600 | [diff] [blame] | 1342 | static void __devinit msm_pm_set_flush_fn(uint32_t pc_mode) |
| 1343 | { |
| 1344 | msm_pm_disable_l2_fn = NULL; |
| 1345 | msm_pm_enable_l2_fn = NULL; |
| 1346 | msm_pm_flush_l2_fn = outer_flush_all; |
| 1347 | |
| 1348 | if (pc_mode == MSM_PM_PC_NOTZ_L2_EXT) { |
| 1349 | msm_pm_disable_l2_fn = outer_disable; |
| 1350 | msm_pm_enable_l2_fn = outer_resume; |
| 1351 | } |
| 1352 | } |
| 1353 | |
Priyanka Mathur | 13bdad1 | 2013-01-28 15:52:56 -0800 | [diff] [blame] | 1354 | struct msm_pc_debug_counters_buffer { |
| 1355 | void __iomem *reg; |
| 1356 | u32 len; |
| 1357 | char buf[MAX_BUF_SIZE]; |
| 1358 | }; |
| 1359 | |
| 1360 | static inline u32 msm_pc_debug_counters_read_register( |
| 1361 | void __iomem *reg, int index , int offset) |
| 1362 | { |
| 1363 | return readl_relaxed(reg + (index * 4 + offset) * 4); |
| 1364 | } |
| 1365 | |
| 1366 | static char *counter_name[] = { |
| 1367 | "PC Entry Counter", |
| 1368 | "Warmboot Entry Counter", |
| 1369 | "PC Bailout Counter" |
| 1370 | }; |
| 1371 | |
| 1372 | static int msm_pc_debug_counters_copy( |
| 1373 | struct msm_pc_debug_counters_buffer *data) |
| 1374 | { |
| 1375 | int j; |
| 1376 | u32 stat; |
| 1377 | unsigned int cpu; |
| 1378 | |
| 1379 | for_each_possible_cpu(cpu) { |
| 1380 | data->len += scnprintf(data->buf + data->len, |
| 1381 | sizeof(data->buf)-data->len, |
| 1382 | "CPU%d\n", cpu); |
| 1383 | |
| 1384 | for (j = 0; j < NUM_OF_COUNTERS; j++) { |
| 1385 | stat = msm_pc_debug_counters_read_register( |
| 1386 | data->reg, cpu, j); |
| 1387 | data->len += scnprintf(data->buf + data->len, |
| 1388 | sizeof(data->buf)-data->len, |
| 1389 | "\t%s : %d\n", counter_name[j], |
| 1390 | stat); |
| 1391 | } |
| 1392 | |
| 1393 | } |
| 1394 | |
| 1395 | return data->len; |
| 1396 | } |
| 1397 | |
| 1398 | static int msm_pc_debug_counters_file_read(struct file *file, |
| 1399 | char __user *bufu, size_t count, loff_t *ppos) |
| 1400 | { |
| 1401 | struct msm_pc_debug_counters_buffer *data; |
| 1402 | |
| 1403 | data = file->private_data; |
| 1404 | |
| 1405 | if (!data) |
| 1406 | return -EINVAL; |
| 1407 | |
| 1408 | if (!bufu || count < 0) |
| 1409 | return -EINVAL; |
| 1410 | |
| 1411 | if (!access_ok(VERIFY_WRITE, bufu, count)) |
| 1412 | return -EFAULT; |
| 1413 | |
| 1414 | if (*ppos >= data->len && data->len == 0) |
| 1415 | data->len = msm_pc_debug_counters_copy(data); |
| 1416 | |
| 1417 | return simple_read_from_buffer(bufu, count, ppos, |
| 1418 | data->buf, data->len); |
| 1419 | } |
| 1420 | |
| 1421 | static int msm_pc_debug_counters_file_open(struct inode *inode, |
| 1422 | struct file *file) |
| 1423 | { |
| 1424 | struct msm_pc_debug_counters_buffer *buf; |
| 1425 | void __iomem *msm_pc_debug_counters_reg; |
| 1426 | |
| 1427 | msm_pc_debug_counters_reg = inode->i_private; |
| 1428 | |
| 1429 | if (!msm_pc_debug_counters_reg) |
| 1430 | return -EINVAL; |
| 1431 | |
| 1432 | file->private_data = kzalloc( |
| 1433 | sizeof(struct msm_pc_debug_counters_buffer), GFP_KERNEL); |
| 1434 | |
| 1435 | if (!file->private_data) { |
| 1436 | pr_err("%s: ERROR kmalloc failed to allocate %d bytes\n", |
| 1437 | __func__, sizeof(struct msm_pc_debug_counters_buffer)); |
| 1438 | |
| 1439 | return -ENOMEM; |
| 1440 | } |
| 1441 | |
| 1442 | buf = file->private_data; |
| 1443 | buf->reg = msm_pc_debug_counters_reg; |
| 1444 | |
| 1445 | return 0; |
| 1446 | } |
| 1447 | |
| 1448 | static int msm_pc_debug_counters_file_close(struct inode *inode, |
| 1449 | struct file *file) |
| 1450 | { |
| 1451 | kfree(file->private_data); |
| 1452 | return 0; |
| 1453 | } |
| 1454 | |
| 1455 | static const struct file_operations msm_pc_debug_counters_fops = { |
| 1456 | .open = msm_pc_debug_counters_file_open, |
| 1457 | .read = msm_pc_debug_counters_file_read, |
| 1458 | .release = msm_pc_debug_counters_file_close, |
| 1459 | .llseek = no_llseek, |
| 1460 | }; |
| 1461 | |
Girish Mahadevan | 5594499 | 2012-10-26 11:03:07 -0600 | [diff] [blame] | 1462 | static int __devinit msm_pm_8x60_probe(struct platform_device *pdev) |
| 1463 | { |
| 1464 | char *key = NULL; |
Priyanka Mathur | 13bdad1 | 2013-01-28 15:52:56 -0800 | [diff] [blame] | 1465 | struct dentry *dent = NULL; |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 1466 | struct resource *res = NULL; |
| 1467 | int i ; |
| 1468 | struct msm_pm_init_data_type pdata_local; |
Girish Mahadevan | 5594499 | 2012-10-26 11:03:07 -0600 | [diff] [blame] | 1469 | int ret = 0; |
| 1470 | |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 1471 | memset(&pdata_local, 0, sizeof(struct msm_pm_init_data_type)); |
| 1472 | |
| 1473 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1474 | if (res) { |
| 1475 | msm_pc_debug_counters_phys = res->start; |
| 1476 | WARN_ON(resource_size(res) < SZ_64); |
| 1477 | msm_pc_debug_counters = devm_ioremap(&pdev->dev, res->start, |
| 1478 | resource_size(res)); |
Priyanka Mathur | 13bdad1 | 2013-01-28 15:52:56 -0800 | [diff] [blame] | 1479 | if (msm_pc_debug_counters) |
| 1480 | for (i = 0; i < resource_size(res)/4; i++) |
| 1481 | __raw_writel(0, msm_pc_debug_counters + i * 4); |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 1482 | |
| 1483 | } |
| 1484 | |
| 1485 | if (!msm_pc_debug_counters) { |
| 1486 | msm_pc_debug_counters = 0; |
| 1487 | msm_pc_debug_counters_phys = 0; |
Priyanka Mathur | 13bdad1 | 2013-01-28 15:52:56 -0800 | [diff] [blame] | 1488 | } else { |
| 1489 | dent = debugfs_create_file("pc_debug_counter", S_IRUGO, NULL, |
| 1490 | msm_pc_debug_counters, |
| 1491 | &msm_pc_debug_counters_fops); |
| 1492 | if (!dent) |
| 1493 | pr_err("%s: ERROR debugfs_create_file failed\n", |
| 1494 | __func__); |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 1495 | } |
| 1496 | |
Girish Mahadevan | 5594499 | 2012-10-26 11:03:07 -0600 | [diff] [blame] | 1497 | if (!pdev->dev.of_node) { |
| 1498 | struct msm_pm_init_data_type *d = pdev->dev.platform_data; |
| 1499 | |
| 1500 | if (!d) |
| 1501 | goto pm_8x60_probe_done; |
| 1502 | |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 1503 | memcpy(&pdata_local, d, sizeof(struct msm_pm_init_data_type)); |
| 1504 | |
Girish Mahadevan | 5594499 | 2012-10-26 11:03:07 -0600 | [diff] [blame] | 1505 | } else { |
| 1506 | key = "qcom,pc-mode"; |
Archana Sathyakumar | 2b91dc8 | 2013-02-01 17:38:23 -0700 | [diff] [blame] | 1507 | ret = msm_pm_get_pc_mode(pdev->dev.of_node, |
| 1508 | key, |
| 1509 | &pdata_local.pc_mode); |
Girish Mahadevan | 5594499 | 2012-10-26 11:03:07 -0600 | [diff] [blame] | 1510 | if (ret) { |
Archana Sathyakumar | 2b91dc8 | 2013-02-01 17:38:23 -0700 | [diff] [blame] | 1511 | pr_debug("%s: Error reading key %s", |
Girish Mahadevan | 5594499 | 2012-10-26 11:03:07 -0600 | [diff] [blame] | 1512 | __func__, key); |
Archana Sathyakumar | 2b91dc8 | 2013-02-01 17:38:23 -0700 | [diff] [blame] | 1513 | return -EINVAL; |
Girish Mahadevan | 5594499 | 2012-10-26 11:03:07 -0600 | [diff] [blame] | 1514 | } |
Girish Mahadevan | 5594499 | 2012-10-26 11:03:07 -0600 | [diff] [blame] | 1515 | |
| 1516 | key = "qcom,use-sync-timer"; |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 1517 | pdata_local.use_sync_timer = |
Girish Mahadevan | 5594499 | 2012-10-26 11:03:07 -0600 | [diff] [blame] | 1518 | of_property_read_bool(pdev->dev.of_node, key); |
Praveen Chidambaram | 4b8df03 | 2013-01-18 16:21:16 -0700 | [diff] [blame] | 1519 | |
| 1520 | key = "qcom,saw-turns-off-pll"; |
| 1521 | msm_no_ramp_down_pc = of_property_read_bool(pdev->dev.of_node, |
| 1522 | key); |
Mahesh Sivasubramanian | b7bda88 | 2013-04-08 09:55:48 -0600 | [diff] [blame^] | 1523 | |
| 1524 | key = "qcom,pc-resets-timer"; |
| 1525 | msm_pm_pc_reset_timer = of_property_read_bool( |
| 1526 | pdev->dev.of_node, key); |
Girish Mahadevan | 5594499 | 2012-10-26 11:03:07 -0600 | [diff] [blame] | 1527 | } |
| 1528 | |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 1529 | if (pdata_local.cp15_data.reg_data && |
| 1530 | pdata_local.cp15_data.reg_saved_state_size > 0) { |
| 1531 | cp15_data.reg_data = kzalloc(sizeof(uint32_t) * |
| 1532 | pdata_local.cp15_data.reg_saved_state_size, |
| 1533 | GFP_KERNEL); |
| 1534 | if (!cp15_data.reg_data) |
| 1535 | return -ENOMEM; |
| 1536 | |
| 1537 | cp15_data.reg_val = kzalloc(sizeof(uint32_t) * |
| 1538 | pdata_local.cp15_data.reg_saved_state_size, |
| 1539 | GFP_KERNEL); |
| 1540 | if (cp15_data.reg_val) |
| 1541 | return -ENOMEM; |
| 1542 | |
| 1543 | memcpy(cp15_data.reg_data, pdata_local.cp15_data.reg_data, |
| 1544 | pdata_local.cp15_data.reg_saved_state_size * |
| 1545 | sizeof(uint32_t)); |
| 1546 | } |
| 1547 | |
| 1548 | msm_pm_set_flush_fn(pdata_local.pc_mode); |
| 1549 | msm_pm_use_sync_timer = pdata_local.use_sync_timer; |
| 1550 | msm_pm_retention_calls_tz = pdata_local.retention_calls_tz; |
| 1551 | |
Girish Mahadevan | 5594499 | 2012-10-26 11:03:07 -0600 | [diff] [blame] | 1552 | pm_8x60_probe_done: |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 1553 | msm_pm_init(); |
Anji Jonnala | 02dac8d | 2013-03-06 21:31:04 +0530 | [diff] [blame] | 1554 | if (pdev->dev.of_node) |
| 1555 | of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev); |
| 1556 | |
Girish Mahadevan | 5594499 | 2012-10-26 11:03:07 -0600 | [diff] [blame] | 1557 | return ret; |
| 1558 | } |
| 1559 | |
| 1560 | static struct of_device_id msm_pm_8x60_table[] = { |
| 1561 | {.compatible = "qcom,pm-8x60"}, |
| 1562 | {}, |
| 1563 | }; |
| 1564 | |
| 1565 | static struct platform_driver msm_pm_8x60_driver = { |
| 1566 | .probe = msm_pm_8x60_probe, |
| 1567 | .driver = { |
| 1568 | .name = "pm-8x60", |
| 1569 | .owner = THIS_MODULE, |
| 1570 | .of_match_table = msm_pm_8x60_table, |
| 1571 | }, |
| 1572 | }; |
| 1573 | |
| 1574 | static int __init msm_pm_8x60_init(void) |
| 1575 | { |
Anji Jonnala | 02dac8d | 2013-03-06 21:31:04 +0530 | [diff] [blame] | 1576 | int rc; |
| 1577 | |
| 1578 | rc = platform_driver_register(&msm_cpu_status_driver); |
| 1579 | |
| 1580 | if (rc) { |
| 1581 | pr_err("%s(): failed to register driver %s\n", __func__, |
| 1582 | msm_cpu_status_driver.driver.name); |
| 1583 | return rc; |
| 1584 | } |
| 1585 | |
Girish Mahadevan | 5594499 | 2012-10-26 11:03:07 -0600 | [diff] [blame] | 1586 | return platform_driver_register(&msm_pm_8x60_driver); |
| 1587 | } |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 1588 | device_initcall(msm_pm_8x60_init); |